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