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