]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/net/znet.c
tg3: Add 5717 NVRAM detection routines
[net-next-2.6.git] / drivers / net / znet.c
CommitLineData
1da177e4
LT
1/* znet.c: An Zenith Z-Note ethernet driver for linux. */
2
3/*
4 Written by Donald Becker.
5
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.
10
11 Thanks to Mike Hollick for alpha testing and suggestions.
12
13 References:
14 The Crynwr packet driver.
15
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.
22
23 Theory of Operation
24
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.
27
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.)
37
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.)
44
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.
53
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.
61 */
62
63/* 10/2002
64
65 o Resurected for Linux 2.5+ by Marc Zyngier <maz@wild-wind.fr.eu.org> :
66
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.
76
77 Some code was taken from wavelan_cs.
6aa20a22 78
1da177e4
LT
79 Tested on a vintage Zenith Z-Note 433Lnp+. Probably broken on
80 anything else. Testers (and detailed bug reports) are welcome :-).
81
82 o TODO :
83
84 - Properly handle multicast
85 - Understand why some traffic patterns add a 1s latency...
86 */
87
1da177e4
LT
88#include <linux/module.h>
89#include <linux/kernel.h>
90#include <linux/string.h>
91#include <linux/errno.h>
92#include <linux/interrupt.h>
93#include <linux/ioport.h>
94#include <linux/init.h>
95#include <linux/delay.h>
96#include <linux/netdevice.h>
97#include <linux/etherdevice.h>
98#include <linux/skbuff.h>
99#include <linux/if_arp.h>
100#include <linux/bitops.h>
101
102#include <asm/system.h>
103#include <asm/io.h>
104#include <asm/dma.h>
105
106/* This include could be elsewhere, since it is not wireless specific */
107#include "wireless/i82593.h"
108
109static char version[] __initdata = "znet.c:v1.02 9/23/94 becker@scyld.com\n";
110
111#ifndef ZNET_DEBUG
112#define ZNET_DEBUG 1
113#endif
114static unsigned int znet_debug = ZNET_DEBUG;
115module_param (znet_debug, int, 0);
116MODULE_PARM_DESC (znet_debug, "ZNet debug level");
117MODULE_LICENSE("GPL");
118
119/* The DMA modes we need aren't in <dma.h>. */
120#define DMA_RX_MODE 0x14 /* Auto init, I/O to mem, ++, demand. */
121#define DMA_TX_MODE 0x18 /* Auto init, Mem to I/O, ++, demand. */
122#define dma_page_eq(ptr1, ptr2) ((long)(ptr1)>>17 == (long)(ptr2)>>17)
123#define RX_BUF_SIZE 8192
124#define TX_BUF_SIZE 8192
125#define DMA_BUF_SIZE (RX_BUF_SIZE + 16) /* 8k + 16 bytes for trailers */
126
127#define TX_TIMEOUT 10
128
129struct znet_private {
130 int rx_dma, tx_dma;
1da177e4
LT
131 spinlock_t lock;
132 short sia_base, sia_size, io_size;
133 struct i82593_conf_block i593_init;
134 /* The starting, current, and end pointers for the packet buffers. */
135 ushort *rx_start, *rx_cur, *rx_end;
136 ushort *tx_start, *tx_cur, *tx_end;
137 ushort tx_buf_len; /* Tx buffer length, in words. */
138};
139
140/* Only one can be built-in;-> */
141static struct net_device *znet_dev;
142
143struct netidblk {
144 char magic[8]; /* The magic number (string) "NETIDBLK" */
145 unsigned char netid[8]; /* The physical station address */
146 char nettype, globalopt;
147 char vendor[8]; /* The machine vendor and product name. */
148 char product[8];
149 char irq1, irq2; /* Interrupts, only one is currently used. */
150 char dma1, dma2;
151 short dma_mem_misc[8]; /* DMA buffer locations (unused in Linux). */
152 short iobase1, iosize1;
153 short iobase2, iosize2; /* Second iobase unused. */
154 char driver_options; /* Misc. bits */
155 char pad;
156};
157
158static int znet_open(struct net_device *dev);
61357325
SH
159static netdev_tx_t znet_send_packet(struct sk_buff *skb,
160 struct net_device *dev);
7d12e780 161static irqreturn_t znet_interrupt(int irq, void *dev_id);
1da177e4
LT
162static void znet_rx(struct net_device *dev);
163static int znet_close(struct net_device *dev);
1da177e4
LT
164static void hardware_init(struct net_device *dev);
165static void update_stop_hit(short ioaddr, unsigned short rx_stop_offset);
166static void znet_tx_timeout (struct net_device *dev);
167
168/* Request needed resources */
169static int znet_request_resources (struct net_device *dev)
170{
524ad0a7 171 struct znet_private *znet = netdev_priv(dev);
1da177e4 172 unsigned long flags;
6aa20a22 173
1da177e4
LT
174 if (request_irq (dev->irq, &znet_interrupt, 0, "ZNet", dev))
175 goto failed;
176 if (request_dma (znet->rx_dma, "ZNet rx"))
177 goto free_irq;
178 if (request_dma (znet->tx_dma, "ZNet tx"))
179 goto free_rx_dma;
180 if (!request_region (znet->sia_base, znet->sia_size, "ZNet SIA"))
181 goto free_tx_dma;
182 if (!request_region (dev->base_addr, znet->io_size, "ZNet I/O"))
183 goto free_sia;
184
185 return 0; /* Happy ! */
186
187 free_sia:
188 release_region (znet->sia_base, znet->sia_size);
189 free_tx_dma:
190 flags = claim_dma_lock();
191 free_dma (znet->tx_dma);
192 release_dma_lock (flags);
193 free_rx_dma:
194 flags = claim_dma_lock();
195 free_dma (znet->rx_dma);
196 release_dma_lock (flags);
197 free_irq:
198 free_irq (dev->irq, dev);
199 failed:
200 return -1;
201}
202
203static void znet_release_resources (struct net_device *dev)
204{
524ad0a7 205 struct znet_private *znet = netdev_priv(dev);
1da177e4 206 unsigned long flags;
6aa20a22 207
1da177e4
LT
208 release_region (znet->sia_base, znet->sia_size);
209 release_region (dev->base_addr, znet->io_size);
210 flags = claim_dma_lock();
211 free_dma (znet->tx_dma);
212 free_dma (znet->rx_dma);
213 release_dma_lock (flags);
214 free_irq (dev->irq, dev);
215}
216
217/* Keep the magical SIA stuff in a single function... */
218static void znet_transceiver_power (struct net_device *dev, int on)
219{
524ad0a7 220 struct znet_private *znet = netdev_priv(dev);
1da177e4
LT
221 unsigned char v;
222
223 /* Turn on/off the 82501 SIA, using zenith-specific magic. */
224 /* Select LAN control register */
225 outb(0x10, znet->sia_base);
226
227 if (on)
228 v = inb(znet->sia_base + 1) | 0x84;
229 else
230 v = inb(znet->sia_base + 1) & ~0x84;
6aa20a22 231
1da177e4
LT
232 outb(v, znet->sia_base+1); /* Turn on/off LAN power (bit 2). */
233}
234
235/* Init the i82593, with current promisc/mcast configuration.
236 Also used from hardware_init. */
237static void znet_set_multicast_list (struct net_device *dev)
238{
524ad0a7 239 struct znet_private *znet = netdev_priv(dev);
1da177e4
LT
240 short ioaddr = dev->base_addr;
241 struct i82593_conf_block *cfblk = &znet->i593_init;
242
243 memset(cfblk, 0x00, sizeof(struct i82593_conf_block));
6aa20a22 244
1da177e4
LT
245 /* The configuration block. What an undocumented nightmare.
246 The first set of values are those suggested (without explanation)
247 for ethernet in the Intel 82586 databook. The rest appear to be
248 completely undocumented, except for cryptic notes in the Crynwr
249 packet driver. This driver uses the Crynwr values verbatim. */
250
251 /* maz : Rewritten to take advantage of the wanvelan includes.
252 At least we have names, not just blind values */
6aa20a22 253
1da177e4
LT
254 /* Byte 0 */
255 cfblk->fifo_limit = 10; /* = 16 B rx and 80 B tx fifo thresholds */
256 cfblk->forgnesi = 0; /* 0=82C501, 1=AMD7992B compatibility */
257 cfblk->fifo_32 = 1;
258 cfblk->d6mod = 0; /* Run in i82593 advanced mode */
259 cfblk->throttle_enb = 1;
260
261 /* Byte 1 */
262 cfblk->throttle = 8; /* Continuous w/interrupts, 128-clock DMA. */
263 cfblk->cntrxint = 0; /* enable continuous mode receive interrupts */
264 cfblk->contin = 1; /* enable continuous mode */
265
266 /* Byte 2 */
267 cfblk->addr_len = ETH_ALEN;
268 cfblk->acloc = 1; /* Disable source addr insertion by i82593 */
269 cfblk->preamb_len = 2; /* 8 bytes preamble */
270 cfblk->loopback = 0; /* Loopback off */
6aa20a22 271
1da177e4
LT
272 /* Byte 3 */
273 cfblk->lin_prio = 0; /* Default priorities & backoff methods. */
274 cfblk->tbofstop = 0;
275 cfblk->exp_prio = 0;
276 cfblk->bof_met = 0;
6aa20a22 277
1da177e4
LT
278 /* Byte 4 */
279 cfblk->ifrm_spc = 6; /* 96 bit times interframe spacing */
6aa20a22 280
1da177e4
LT
281 /* Byte 5 */
282 cfblk->slottim_low = 0; /* 512 bit times slot time (low) */
6aa20a22 283
1da177e4
LT
284 /* Byte 6 */
285 cfblk->slottim_hi = 2; /* 512 bit times slot time (high) */
286 cfblk->max_retr = 15; /* 15 collisions retries */
6aa20a22 287
1da177e4
LT
288 /* Byte 7 */
289 cfblk->prmisc = ((dev->flags & IFF_PROMISC) ? 1 : 0); /* Promiscuous mode */
290 cfblk->bc_dis = 0; /* Enable broadcast reception */
291 cfblk->crs_1 = 0; /* Don't transmit without carrier sense */
292 cfblk->nocrc_ins = 0; /* i82593 generates CRC */
293 cfblk->crc_1632 = 0; /* 32-bit Autodin-II CRC */
294 cfblk->crs_cdt = 0; /* CD not to be interpreted as CS */
6aa20a22 295
1da177e4
LT
296 /* Byte 8 */
297 cfblk->cs_filter = 0; /* CS is recognized immediately */
298 cfblk->crs_src = 0; /* External carrier sense */
299 cfblk->cd_filter = 0; /* CD is recognized immediately */
6aa20a22 300
1da177e4
LT
301 /* Byte 9 */
302 cfblk->min_fr_len = ETH_ZLEN >> 2; /* Minimum frame length */
6aa20a22 303
1da177e4
LT
304 /* Byte A */
305 cfblk->lng_typ = 1; /* Type/length checks OFF */
306 cfblk->lng_fld = 1; /* Disable 802.3 length field check */
307 cfblk->rxcrc_xf = 1; /* Don't transfer CRC to memory */
308 cfblk->artx = 1; /* Disable automatic retransmission */
309 cfblk->sarec = 1; /* Disable source addr trig of CD */
310 cfblk->tx_jabber = 0; /* Disable jabber jam sequence */
311 cfblk->hash_1 = 1; /* Use bits 0-5 in mc address hash */
312 cfblk->lbpkpol = 0; /* Loopback pin active high */
6aa20a22 313
1da177e4
LT
314 /* Byte B */
315 cfblk->fdx = 0; /* Disable full duplex operation */
6aa20a22 316
1da177e4
LT
317 /* Byte C */
318 cfblk->dummy_6 = 0x3f; /* all ones, Default multicast addresses & backoff. */
319 cfblk->mult_ia = 0; /* No multiple individual addresses */
320 cfblk->dis_bof = 0; /* Disable the backoff algorithm ?! */
6aa20a22 321
1da177e4
LT
322 /* Byte D */
323 cfblk->dummy_1 = 1; /* set to 1 */
324 cfblk->tx_ifs_retrig = 3; /* Hmm... Disabled */
325 cfblk->mc_all = (dev->mc_list || (dev->flags&IFF_ALLMULTI));/* multicast all mode */
326 cfblk->rcv_mon = 0; /* Monitor mode disabled */
327 cfblk->frag_acpt = 0; /* Do not accept fragments */
328 cfblk->tstrttrs = 0; /* No start transmission threshold */
6aa20a22 329
1da177e4
LT
330 /* Byte E */
331 cfblk->fretx = 1; /* FIFO automatic retransmission */
332 cfblk->runt_eop = 0; /* drop "runt" packets */
333 cfblk->hw_sw_pin = 0; /* ?? */
334 cfblk->big_endn = 0; /* Big Endian ? no... */
335 cfblk->syncrqs = 1; /* Synchronous DRQ deassertion... */
336 cfblk->sttlen = 1; /* 6 byte status registers */
337 cfblk->rx_eop = 0; /* Signal EOP on packet reception */
338 cfblk->tx_eop = 0; /* Signal EOP on packet transmission */
339
340 /* Byte F */
341 cfblk->rbuf_size = RX_BUF_SIZE >> 12; /* Set receive buffer size */
342 cfblk->rcvstop = 1; /* Enable Receive Stop Register */
343
344 if (znet_debug > 2) {
345 int i;
346 unsigned char *c;
347
348 for (i = 0, c = (char *) cfblk; i < sizeof (*cfblk); i++)
349 printk ("%02X ", c[i]);
350 printk ("\n");
351 }
6aa20a22 352
1da177e4
LT
353 *znet->tx_cur++ = sizeof(struct i82593_conf_block);
354 memcpy(znet->tx_cur, cfblk, sizeof(struct i82593_conf_block));
355 znet->tx_cur += sizeof(struct i82593_conf_block)/2;
356 outb(OP0_CONFIGURE | CR0_CHNL, ioaddr);
357
358 /* XXX FIXME maz : Add multicast addresses here, so having a
359 * multicast address configured isn't equal to IFF_ALLMULTI */
360}
6aa20a22 361
bc0443fc
SH
362static const struct net_device_ops znet_netdev_ops = {
363 .ndo_open = znet_open,
364 .ndo_stop = znet_close,
365 .ndo_start_xmit = znet_send_packet,
366 .ndo_set_multicast_list = znet_set_multicast_list,
367 .ndo_tx_timeout = znet_tx_timeout,
368 .ndo_change_mtu = eth_change_mtu,
369 .ndo_set_mac_address = eth_mac_addr,
370 .ndo_validate_addr = eth_validate_addr,
371};
372
1da177e4
LT
373/* The Z-Note probe is pretty easy. The NETIDBLK exists in the safe-to-probe
374 BIOS area. We just scan for the signature, and pull the vital parameters
375 out of the structure. */
376
377static int __init znet_probe (void)
378{
379 int i;
380 struct netidblk *netinfo;
381 struct znet_private *znet;
382 struct net_device *dev;
383 char *p;
384 int err = -ENOMEM;
385
386 /* This code scans the region 0xf0000 to 0xfffff for a "NETIDBLK". */
387 for(p = (char *)phys_to_virt(0xf0000); p < (char *)phys_to_virt(0x100000); p++)
388 if (*p == 'N' && strncmp(p, "NETIDBLK", 8) == 0)
389 break;
390
391 if (p >= (char *)phys_to_virt(0x100000)) {
392 if (znet_debug > 1)
393 printk(KERN_INFO "No Z-Note ethernet adaptor found.\n");
394 return -ENODEV;
395 }
396
397 dev = alloc_etherdev(sizeof(struct znet_private));
398 if (!dev)
399 return -ENOMEM;
400
524ad0a7 401 znet = netdev_priv(dev);
1da177e4
LT
402
403 netinfo = (struct netidblk *)p;
404 dev->base_addr = netinfo->iobase1;
405 dev->irq = netinfo->irq1;
406
1da177e4
LT
407 /* The station address is in the "netidblk" at 0x0f0000. */
408 for (i = 0; i < 6; i++)
0795af57 409 dev->dev_addr[i] = netinfo->netid[i];
1da177e4 410
e174961c 411 printk(KERN_INFO "%s: ZNET at %#3lx, %pM"
0795af57 412 ", using IRQ %d DMA %d and %d.\n",
e174961c 413 dev->name, dev->base_addr, dev->dev_addr,
0795af57 414 dev->irq, netinfo->dma1, netinfo->dma2);
1da177e4
LT
415
416 if (znet_debug > 1) {
417 printk(KERN_INFO "%s: vendor '%16.16s' IRQ1 %d IRQ2 %d DMA1 %d DMA2 %d.\n",
418 dev->name, netinfo->vendor,
419 netinfo->irq1, netinfo->irq2,
420 netinfo->dma1, netinfo->dma2);
421 printk(KERN_INFO "%s: iobase1 %#x size %d iobase2 %#x size %d net type %2.2x.\n",
422 dev->name, netinfo->iobase1, netinfo->iosize1,
423 netinfo->iobase2, netinfo->iosize2, netinfo->nettype);
424 }
425
426 if (znet_debug > 0)
427 printk(KERN_INFO "%s", version);
428
429 znet->rx_dma = netinfo->dma1;
430 znet->tx_dma = netinfo->dma2;
431 spin_lock_init(&znet->lock);
432 znet->sia_base = 0xe6; /* Magic address for the 82501 SIA */
433 znet->sia_size = 2;
434 /* maz: Despite the '593 being advertised above as using a
435 * single 8bits I/O port, this driver does many 16bits
436 * access. So set io_size accordingly */
437 znet->io_size = 2;
438
439 if (!(znet->rx_start = kmalloc (DMA_BUF_SIZE, GFP_KERNEL | GFP_DMA)))
440 goto free_dev;
441 if (!(znet->tx_start = kmalloc (DMA_BUF_SIZE, GFP_KERNEL | GFP_DMA)))
442 goto free_rx;
443
444 if (!dma_page_eq (znet->rx_start, znet->rx_start + (RX_BUF_SIZE/2-1)) ||
445 !dma_page_eq (znet->tx_start, znet->tx_start + (TX_BUF_SIZE/2-1))) {
446 printk (KERN_WARNING "tx/rx crossing DMA frontiers, giving up\n");
447 goto free_tx;
448 }
6aa20a22 449
1da177e4
LT
450 znet->rx_end = znet->rx_start + RX_BUF_SIZE/2;
451 znet->tx_buf_len = TX_BUF_SIZE/2;
452 znet->tx_end = znet->tx_start + znet->tx_buf_len;
453
454 /* The ZNET-specific entries in the device structure. */
bc0443fc 455 dev->netdev_ops = &znet_netdev_ops;
1da177e4
LT
456 dev->watchdog_timeo = TX_TIMEOUT;
457 err = register_netdev(dev);
458 if (err)
459 goto free_tx;
460 znet_dev = dev;
461 return 0;
462
463 free_tx:
464 kfree(znet->tx_start);
465 free_rx:
466 kfree(znet->rx_start);
467 free_dev:
468 free_netdev(dev);
469 return err;
470}
471
6aa20a22 472
1da177e4
LT
473static int znet_open(struct net_device *dev)
474{
475 int ioaddr = dev->base_addr;
476
477 if (znet_debug > 2)
478 printk(KERN_DEBUG "%s: znet_open() called.\n", dev->name);
479
480 /* These should never fail. You can't add devices to a sealed box! */
481 if (znet_request_resources (dev)) {
482 printk(KERN_WARNING "%s: Not opened -- resource busy?!?\n", dev->name);
483 return -EBUSY;
484 }
485
486 znet_transceiver_power (dev, 1);
6aa20a22 487
1da177e4
LT
488 /* According to the Crynwr driver we should wait 50 msec. for the
489 LAN clock to stabilize. My experiments indicates that the '593 can
490 be initialized immediately. The delay is probably needed for the
491 DC-to-DC converter to come up to full voltage, and for the oscillator
492 to be spot-on at 20Mhz before transmitting.
493 Until this proves to be a problem we rely on the higher layers for the
494 delay and save allocating a timer entry. */
495
496 /* maz : Well, I'm getting every time the following message
497 * without the delay on a 486@33. This machine is much too
498 * fast... :-) So maybe the Crynwr driver wasn't wrong after
499 * all, even if the message is completly harmless on my
500 * setup. */
501 mdelay (50);
6aa20a22 502
1da177e4
LT
503 /* This follows the packet driver's lead, and checks for success. */
504 if (inb(ioaddr) != 0x10 && inb(ioaddr) != 0x00)
505 printk(KERN_WARNING "%s: Problem turning on the transceiver power.\n",
506 dev->name);
507
508 hardware_init(dev);
509 netif_start_queue (dev);
510
511 return 0;
512}
513
514
515static void znet_tx_timeout (struct net_device *dev)
516{
517 int ioaddr = dev->base_addr;
518 ushort event, tx_status, rx_offset, state;
519
520 outb (CR0_STATUS_0, ioaddr);
521 event = inb (ioaddr);
522 outb (CR0_STATUS_1, ioaddr);
523 tx_status = inw (ioaddr);
524 outb (CR0_STATUS_2, ioaddr);
525 rx_offset = inw (ioaddr);
526 outb (CR0_STATUS_3, ioaddr);
527 state = inb (ioaddr);
528 printk (KERN_WARNING "%s: transmit timed out, status %02x %04x %04x %02x,"
529 " resetting.\n", dev->name, event, tx_status, rx_offset, state);
530 if (tx_status == TX_LOST_CRS)
531 printk (KERN_WARNING "%s: Tx carrier error, check transceiver cable.\n",
532 dev->name);
533 outb (OP0_RESET, ioaddr);
534 hardware_init (dev);
535 netif_wake_queue (dev);
536}
537
61357325 538static netdev_tx_t znet_send_packet(struct sk_buff *skb, struct net_device *dev)
1da177e4
LT
539{
540 int ioaddr = dev->base_addr;
524ad0a7 541 struct znet_private *znet = netdev_priv(dev);
1da177e4
LT
542 unsigned long flags;
543 short length = skb->len;
544
545 if (znet_debug > 4)
546 printk(KERN_DEBUG "%s: ZNet_send_packet.\n", dev->name);
547
548 if (length < ETH_ZLEN) {
5b057c6b 549 if (skb_padto(skb, ETH_ZLEN))
6ed10654 550 return NETDEV_TX_OK;
1da177e4
LT
551 length = ETH_ZLEN;
552 }
6aa20a22 553
1da177e4 554 netif_stop_queue (dev);
6aa20a22 555
1da177e4
LT
556 /* Check that the part hasn't reset itself, probably from suspend. */
557 outb(CR0_STATUS_0, ioaddr);
558 if (inw(ioaddr) == 0x0010 &&
559 inw(ioaddr) == 0x0000 &&
560 inw(ioaddr) == 0x0010) {
561 if (znet_debug > 1)
562 printk (KERN_WARNING "%s : waking up\n", dev->name);
563 hardware_init(dev);
564 znet_transceiver_power (dev, 1);
565 }
566
567 if (1) {
568 unsigned char *buf = (void *)skb->data;
569 ushort *tx_link = znet->tx_cur - 1;
570 ushort rnd_len = (length + 1)>>1;
6aa20a22 571
09f75cd7 572 dev->stats.tx_bytes+=length;
1da177e4
LT
573
574 if (znet->tx_cur >= znet->tx_end)
575 znet->tx_cur = znet->tx_start;
576 *znet->tx_cur++ = length;
577 if (znet->tx_cur + rnd_len + 1 > znet->tx_end) {
578 int semi_cnt = (znet->tx_end - znet->tx_cur)<<1; /* Cvrt to byte cnt. */
579 memcpy(znet->tx_cur, buf, semi_cnt);
580 rnd_len -= semi_cnt>>1;
581 memcpy(znet->tx_start, buf + semi_cnt, length - semi_cnt);
582 znet->tx_cur = znet->tx_start + rnd_len;
583 } else {
584 memcpy(znet->tx_cur, buf, skb->len);
585 znet->tx_cur += rnd_len;
586 }
587 *znet->tx_cur++ = 0;
588
589 spin_lock_irqsave(&znet->lock, flags);
590 {
591 *tx_link = OP0_TRANSMIT | CR0_CHNL;
592 /* Is this always safe to do? */
593 outb(OP0_TRANSMIT | CR0_CHNL, ioaddr);
594 }
595 spin_unlock_irqrestore (&znet->lock, flags);
596
597 dev->trans_start = jiffies;
598 netif_start_queue (dev);
599
600 if (znet_debug > 4)
601 printk(KERN_DEBUG "%s: Transmitter queued, length %d.\n", dev->name, length);
602 }
6aa20a22 603 dev_kfree_skb(skb);
6ed10654 604 return NETDEV_TX_OK;
1da177e4
LT
605}
606
607/* The ZNET interrupt handler. */
7d12e780 608static irqreturn_t znet_interrupt(int irq, void *dev_id)
1da177e4
LT
609{
610 struct net_device *dev = dev_id;
524ad0a7 611 struct znet_private *znet = netdev_priv(dev);
1da177e4
LT
612 int ioaddr;
613 int boguscnt = 20;
614 int handled = 0;
615
1da177e4 616 spin_lock (&znet->lock);
6aa20a22 617
1da177e4
LT
618 ioaddr = dev->base_addr;
619
620 outb(CR0_STATUS_0, ioaddr);
621 do {
622 ushort status = inb(ioaddr);
623 if (znet_debug > 5) {
624 ushort result, rx_ptr, running;
625 outb(CR0_STATUS_1, ioaddr);
626 result = inw(ioaddr);
627 outb(CR0_STATUS_2, ioaddr);
628 rx_ptr = inw(ioaddr);
629 outb(CR0_STATUS_3, ioaddr);
630 running = inb(ioaddr);
631 printk(KERN_DEBUG "%s: interrupt, status %02x, %04x %04x %02x serial %d.\n",
632 dev->name, status, result, rx_ptr, running, boguscnt);
633 }
634 if ((status & SR0_INTERRUPT) == 0)
635 break;
636
637 handled = 1;
638
639 if ((status & SR0_EVENT_MASK) == SR0_TRANSMIT_DONE ||
640 (status & SR0_EVENT_MASK) == SR0_RETRANSMIT_DONE ||
641 (status & SR0_EVENT_MASK) == SR0_TRANSMIT_NO_CRC_DONE) {
642 int tx_status;
643 outb(CR0_STATUS_1, ioaddr);
644 tx_status = inw(ioaddr);
645 /* It's undocumented, but tx_status seems to match the i82586. */
646 if (tx_status & TX_OK) {
09f75cd7
JG
647 dev->stats.tx_packets++;
648 dev->stats.collisions += tx_status & TX_NCOL_MASK;
1da177e4
LT
649 } else {
650 if (tx_status & (TX_LOST_CTS | TX_LOST_CRS))
09f75cd7 651 dev->stats.tx_carrier_errors++;
1da177e4 652 if (tx_status & TX_UND_RUN)
09f75cd7 653 dev->stats.tx_fifo_errors++;
1da177e4 654 if (!(tx_status & TX_HRT_BEAT))
09f75cd7 655 dev->stats.tx_heartbeat_errors++;
1da177e4 656 if (tx_status & TX_MAX_COL)
09f75cd7 657 dev->stats.tx_aborted_errors++;
1da177e4
LT
658 /* ...and the catch-all. */
659 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))
09f75cd7 660 dev->stats.tx_errors++;
1da177e4
LT
661
662 /* Transceiver may be stuck if cable
663 * was removed while emiting a
664 * packet. Flip it off, then on to
665 * reset it. This is very empirical,
666 * but it seems to work. */
6aa20a22 667
1da177e4
LT
668 znet_transceiver_power (dev, 0);
669 znet_transceiver_power (dev, 1);
670 }
671 netif_wake_queue (dev);
672 }
673
674 if ((status & SR0_RECEPTION) ||
675 (status & SR0_EVENT_MASK) == SR0_STOP_REG_HIT) {
676 znet_rx(dev);
677 }
678 /* Clear the interrupts we've handled. */
679 outb(CR0_INT_ACK, ioaddr);
680 } while (boguscnt--);
681
682 spin_unlock (&znet->lock);
6aa20a22 683
1da177e4
LT
684 return IRQ_RETVAL(handled);
685}
686
687static void znet_rx(struct net_device *dev)
688{
524ad0a7 689 struct znet_private *znet = netdev_priv(dev);
1da177e4
LT
690 int ioaddr = dev->base_addr;
691 int boguscount = 1;
692 short next_frame_end_offset = 0; /* Offset of next frame start. */
693 short *cur_frame_end;
694 short cur_frame_end_offset;
695
696 outb(CR0_STATUS_2, ioaddr);
697 cur_frame_end_offset = inw(ioaddr);
698
699 if (cur_frame_end_offset == znet->rx_cur - znet->rx_start) {
700 printk(KERN_WARNING "%s: Interrupted, but nothing to receive, offset %03x.\n",
701 dev->name, cur_frame_end_offset);
702 return;
703 }
704
705 /* Use same method as the Crynwr driver: construct a forward list in
706 the same area of the backwards links we now have. This allows us to
707 pass packets to the upper layers in the order they were received --
708 important for fast-path sequential operations. */
709 while (znet->rx_start + cur_frame_end_offset != znet->rx_cur
710 && ++boguscount < 5) {
711 unsigned short hi_cnt, lo_cnt, hi_status, lo_status;
712 int count, status;
713
714 if (cur_frame_end_offset < 4) {
715 /* Oh no, we have a special case: the frame trailer wraps around
716 the end of the ring buffer. We've saved space at the end of
717 the ring buffer for just this problem. */
718 memcpy(znet->rx_end, znet->rx_start, 8);
719 cur_frame_end_offset += (RX_BUF_SIZE/2);
720 }
721 cur_frame_end = znet->rx_start + cur_frame_end_offset - 4;
722
723 lo_status = *cur_frame_end++;
724 hi_status = *cur_frame_end++;
725 status = ((hi_status & 0xff) << 8) + (lo_status & 0xff);
726 lo_cnt = *cur_frame_end++;
727 hi_cnt = *cur_frame_end++;
728 count = ((hi_cnt & 0xff) << 8) + (lo_cnt & 0xff);
729
730 if (znet_debug > 5)
731 printk(KERN_DEBUG "Constructing trailer at location %03x, %04x %04x %04x %04x"
732 " count %#x status %04x.\n",
733 cur_frame_end_offset<<1, lo_status, hi_status, lo_cnt, hi_cnt,
734 count, status);
735 cur_frame_end[-4] = status;
736 cur_frame_end[-3] = next_frame_end_offset;
737 cur_frame_end[-2] = count;
738 next_frame_end_offset = cur_frame_end_offset;
739 cur_frame_end_offset -= ((count + 1)>>1) + 3;
740 if (cur_frame_end_offset < 0)
741 cur_frame_end_offset += RX_BUF_SIZE/2;
742 };
743
744 /* Now step forward through the list. */
745 do {
746 ushort *this_rfp_ptr = znet->rx_start + next_frame_end_offset;
747 int status = this_rfp_ptr[-4];
748 int pkt_len = this_rfp_ptr[-2];
6aa20a22 749
1da177e4
LT
750 if (znet_debug > 5)
751 printk(KERN_DEBUG "Looking at trailer ending at %04x status %04x length %03x"
752 " next %04x.\n", next_frame_end_offset<<1, status, pkt_len,
753 this_rfp_ptr[-3]<<1);
754 /* Once again we must assume that the i82586 docs apply. */
755 if ( ! (status & RX_RCV_OK)) { /* There was an error. */
09f75cd7
JG
756 dev->stats.rx_errors++;
757 if (status & RX_CRC_ERR) dev->stats.rx_crc_errors++;
758 if (status & RX_ALG_ERR) dev->stats.rx_frame_errors++;
1da177e4 759#if 0
09f75cd7
JG
760 if (status & 0x0200) dev->stats.rx_over_errors++; /* Wrong. */
761 if (status & 0x0100) dev->stats.rx_fifo_errors++;
1da177e4
LT
762#else
763 /* maz : Wild guess... */
09f75cd7 764 if (status & RX_OVRRUN) dev->stats.rx_over_errors++;
1da177e4 765#endif
09f75cd7 766 if (status & RX_SRT_FRM) dev->stats.rx_length_errors++;
1da177e4 767 } else if (pkt_len > 1536) {
09f75cd7 768 dev->stats.rx_length_errors++;
1da177e4
LT
769 } else {
770 /* Malloc up new buffer. */
771 struct sk_buff *skb;
772
773 skb = dev_alloc_skb(pkt_len);
774 if (skb == NULL) {
775 if (znet_debug)
776 printk(KERN_WARNING "%s: Memory squeeze, dropping packet.\n", dev->name);
09f75cd7 777 dev->stats.rx_dropped++;
1da177e4
LT
778 break;
779 }
1da177e4
LT
780
781 if (&znet->rx_cur[(pkt_len+1)>>1] > znet->rx_end) {
782 int semi_cnt = (znet->rx_end - znet->rx_cur)<<1;
783 memcpy(skb_put(skb,semi_cnt), znet->rx_cur, semi_cnt);
784 memcpy(skb_put(skb,pkt_len-semi_cnt), znet->rx_start,
785 pkt_len - semi_cnt);
786 } else {
787 memcpy(skb_put(skb,pkt_len), znet->rx_cur, pkt_len);
788 if (znet_debug > 6) {
789 unsigned int *packet = (unsigned int *) skb->data;
790 printk(KERN_DEBUG "Packet data is %08x %08x %08x %08x.\n", packet[0],
791 packet[1], packet[2], packet[3]);
792 }
793 }
794 skb->protocol=eth_type_trans(skb,dev);
795 netif_rx(skb);
09f75cd7
JG
796 dev->stats.rx_packets++;
797 dev->stats.rx_bytes += pkt_len;
1da177e4
LT
798 }
799 znet->rx_cur = this_rfp_ptr;
800 if (znet->rx_cur >= znet->rx_end)
801 znet->rx_cur -= RX_BUF_SIZE/2;
802 update_stop_hit(ioaddr, (znet->rx_cur - znet->rx_start)<<1);
803 next_frame_end_offset = this_rfp_ptr[-3];
804 if (next_frame_end_offset == 0) /* Read all the frames? */
805 break; /* Done for now */
806 this_rfp_ptr = znet->rx_start + next_frame_end_offset;
807 } while (--boguscount);
808
809 /* If any worth-while packets have been received, dev_rint()
810 has done a mark_bh(INET_BH) for us and will work on them
811 when we get to the bottom-half routine. */
812 return;
813}
814
815/* The inverse routine to znet_open(). */
816static int znet_close(struct net_device *dev)
817{
818 int ioaddr = dev->base_addr;
819
820 netif_stop_queue (dev);
821
822 outb(OP0_RESET, ioaddr); /* CMD0_RESET */
823
824 if (znet_debug > 1)
825 printk(KERN_DEBUG "%s: Shutting down ethercard.\n", dev->name);
826 /* Turn off transceiver power. */
827 znet_transceiver_power (dev, 0);
6aa20a22 828
1da177e4 829 znet_release_resources (dev);
6aa20a22 830
1da177e4
LT
831 return 0;
832}
833
1da177e4
LT
834static void show_dma(struct net_device *dev)
835{
836 short ioaddr = dev->base_addr;
837 unsigned char stat = inb (ioaddr);
524ad0a7 838 struct znet_private *znet = netdev_priv(dev);
1da177e4
LT
839 unsigned long flags;
840 short dma_port = ((znet->tx_dma&3)<<2) + IO_DMA2_BASE;
841 unsigned addr = inb(dma_port);
842 short residue;
843
844 addr |= inb(dma_port) << 8;
845 residue = get_dma_residue(znet->tx_dma);
6aa20a22 846
1da177e4
LT
847 if (znet_debug > 1) {
848 flags=claim_dma_lock();
849 printk(KERN_DEBUG "Stat:%02x Addr: %04x cnt:%3x\n",
850 stat, addr<<1, residue);
851 release_dma_lock(flags);
852 }
853}
854
855/* Initialize the hardware. We have to do this when the board is open()ed
856 or when we come out of suspend mode. */
857static void hardware_init(struct net_device *dev)
858{
859 unsigned long flags;
860 short ioaddr = dev->base_addr;
524ad0a7 861 struct znet_private *znet = netdev_priv(dev);
1da177e4
LT
862
863 znet->rx_cur = znet->rx_start;
864 znet->tx_cur = znet->tx_start;
865
866 /* Reset the chip, and start it up. */
867 outb(OP0_RESET, ioaddr);
868
869 flags=claim_dma_lock();
870 disable_dma(znet->rx_dma); /* reset by an interrupting task. */
871 clear_dma_ff(znet->rx_dma);
872 set_dma_mode(znet->rx_dma, DMA_RX_MODE);
873 set_dma_addr(znet->rx_dma, (unsigned int) znet->rx_start);
874 set_dma_count(znet->rx_dma, RX_BUF_SIZE);
875 enable_dma(znet->rx_dma);
876 /* Now set up the Tx channel. */
877 disable_dma(znet->tx_dma);
878 clear_dma_ff(znet->tx_dma);
879 set_dma_mode(znet->tx_dma, DMA_TX_MODE);
880 set_dma_addr(znet->tx_dma, (unsigned int) znet->tx_start);
881 set_dma_count(znet->tx_dma, znet->tx_buf_len<<1);
882 enable_dma(znet->tx_dma);
883 release_dma_lock(flags);
6aa20a22 884
1da177e4
LT
885 if (znet_debug > 1)
886 printk(KERN_DEBUG "%s: Initializing the i82593, rx buf %p tx buf %p\n",
887 dev->name, znet->rx_start,znet->tx_start);
888 /* Do an empty configure command, just like the Crynwr driver. This
889 resets to chip to its default values. */
890 *znet->tx_cur++ = 0;
891 *znet->tx_cur++ = 0;
892 show_dma(dev);
893 outb(OP0_CONFIGURE | CR0_CHNL, ioaddr);
894
895 znet_set_multicast_list (dev);
896
897 *znet->tx_cur++ = 6;
898 memcpy(znet->tx_cur, dev->dev_addr, 6);
899 znet->tx_cur += 3;
900 show_dma(dev);
901 outb(OP0_IA_SETUP | CR0_CHNL, ioaddr);
902 show_dma(dev);
903
904 update_stop_hit(ioaddr, 8192);
905 if (znet_debug > 1) printk(KERN_DEBUG "enabling Rx.\n");
906 outb(OP0_RCV_ENABLE, ioaddr);
907 netif_start_queue (dev);
908}
909
910static void update_stop_hit(short ioaddr, unsigned short rx_stop_offset)
911{
912 outb(OP0_SWIT_TO_PORT_1 | CR0_CHNL, ioaddr);
913 if (znet_debug > 5)
914 printk(KERN_DEBUG "Updating stop hit with value %02x.\n",
915 (rx_stop_offset >> 6) | CR1_STOP_REG_UPDATE);
916 outb((rx_stop_offset >> 6) | CR1_STOP_REG_UPDATE, ioaddr);
917 outb(OP1_SWIT_TO_PORT_0, ioaddr);
918}
919
920static __exit void znet_cleanup (void)
921{
922 if (znet_dev) {
524ad0a7 923 struct znet_private *znet = netdev_priv(znet_dev);
1da177e4
LT
924
925 unregister_netdev (znet_dev);
926 kfree (znet->rx_start);
927 kfree (znet->tx_start);
928 free_netdev (znet_dev);
929 }
930}
931
932module_init (znet_probe);
933module_exit (znet_cleanup);