blob: 3aef3c10d56ff1c2e3170fdbecd064cd5da17bee [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Amiga Linux/m68k Ariadne Ethernet Driver
3 *
4 * © Copyright 1995-2003 by Geert Uytterhoeven (geert@linux-m68k.org)
5 * Peter De Schrijver (p2@mind.be)
6 *
7 * ---------------------------------------------------------------------------
8 *
9 * This program is based on
10 *
11 * lance.c: An AMD LANCE ethernet driver for linux.
12 * Written 1993-94 by Donald Becker.
13 *
14 * Am79C960: PCnet(tm)-ISA Single-Chip Ethernet Controller
15 * Advanced Micro Devices
16 * Publication #16907, Rev. B, Amendment/0, May 1994
17 *
18 * MC68230: Parallel Interface/Timer (PI/T)
19 * Motorola Semiconductors, December, 1983
20 *
21 * ---------------------------------------------------------------------------
22 *
23 * This file is subject to the terms and conditions of the GNU General Public
24 * License. See the file COPYING in the main directory of the Linux
25 * distribution for more details.
26 *
27 * ---------------------------------------------------------------------------
28 *
29 * The Ariadne is a Zorro-II board made by Village Tronic. It contains:
30 *
31 * - an Am79C960 PCnet-ISA Single-Chip Ethernet Controller with both
32 * 10BASE-2 (thin coax) and 10BASE-T (UTP) connectors
33 *
34 * - an MC68230 Parallel Interface/Timer configured as 2 parallel ports
35 */
36
37#include <linux/module.h>
38#include <linux/stddef.h>
39#include <linux/kernel.h>
40#include <linux/string.h>
41#include <linux/errno.h>
42#include <linux/ioport.h>
43#include <linux/slab.h>
44#include <linux/netdevice.h>
45#include <linux/etherdevice.h>
46#include <linux/interrupt.h>
47#include <linux/skbuff.h>
48#include <linux/init.h>
49#include <linux/zorro.h>
50#include <linux/bitops.h>
51
52#include <asm/amigaints.h>
53#include <asm/amigahw.h>
54#include <asm/irq.h>
55
56#include "ariadne.h"
57
58
59#ifdef ARIADNE_DEBUG
60int ariadne_debug = ARIADNE_DEBUG;
61#else
62int ariadne_debug = 1;
63#endif
64
65
66 /*
67 * Macros to Fix Endianness problems
68 */
69
70 /* Swap the Bytes in a WORD */
71#define swapw(x) (((x>>8)&0x00ff)|((x<<8)&0xff00))
72 /* Get the Low BYTE in a WORD */
73#define lowb(x) (x&0xff)
74 /* Get the Swapped High WORD in a LONG */
75#define swhighw(x) ((((x)>>8)&0xff00)|(((x)>>24)&0x00ff))
76 /* Get the Swapped Low WORD in a LONG */
77#define swloww(x) ((((x)<<8)&0xff00)|(((x)>>8)&0x00ff))
78
79
80 /*
81 * Transmit/Receive Ring Definitions
82 */
83
84#define TX_RING_SIZE 5
85#define RX_RING_SIZE 16
86
87#define PKT_BUF_SIZE 1520
88
89
90 /*
91 * Private Device Data
92 */
93
94struct ariadne_private {
95 volatile struct TDRE *tx_ring[TX_RING_SIZE];
96 volatile struct RDRE *rx_ring[RX_RING_SIZE];
97 volatile u_short *tx_buff[TX_RING_SIZE];
98 volatile u_short *rx_buff[RX_RING_SIZE];
99 int cur_tx, cur_rx; /* The next free ring entry */
100 int dirty_tx; /* The ring entries to be free()ed. */
101 struct net_device_stats stats;
102 char tx_full;
103};
104
105
106 /*
107 * Structure Created in the Ariadne's RAM Buffer
108 */
109
110struct lancedata {
111 struct TDRE tx_ring[TX_RING_SIZE];
112 struct RDRE rx_ring[RX_RING_SIZE];
113 u_short tx_buff[TX_RING_SIZE][PKT_BUF_SIZE/sizeof(u_short)];
114 u_short rx_buff[RX_RING_SIZE][PKT_BUF_SIZE/sizeof(u_short)];
115};
116
117static int ariadne_open(struct net_device *dev);
118static void ariadne_init_ring(struct net_device *dev);
119static int ariadne_start_xmit(struct sk_buff *skb, struct net_device *dev);
120static void ariadne_tx_timeout(struct net_device *dev);
121static int ariadne_rx(struct net_device *dev);
122static void ariadne_reset(struct net_device *dev);
123static irqreturn_t ariadne_interrupt(int irq, void *data, struct pt_regs *fp);
124static int ariadne_close(struct net_device *dev);
125static struct net_device_stats *ariadne_get_stats(struct net_device *dev);
126#ifdef HAVE_MULTICAST
127static void set_multicast_list(struct net_device *dev);
128#endif
129
130
131static void memcpyw(volatile u_short *dest, u_short *src, int len)
132{
133 while (len >= 2) {
134 *(dest++) = *(src++);
135 len -= 2;
136 }
137 if (len == 1)
138 *dest = (*(u_char *)src)<<8;
139}
140
141
142static int __devinit ariadne_init_one(struct zorro_dev *z,
143 const struct zorro_device_id *ent);
144static void __devexit ariadne_remove_one(struct zorro_dev *z);
145
146
147static struct zorro_device_id ariadne_zorro_tbl[] __devinitdata = {
148 { ZORRO_PROD_VILLAGE_TRONIC_ARIADNE },
149 { 0 }
150};
151
152static struct zorro_driver ariadne_driver = {
153 .name = "ariadne",
154 .id_table = ariadne_zorro_tbl,
155 .probe = ariadne_init_one,
156 .remove = __devexit_p(ariadne_remove_one),
157};
158
159static int __devinit ariadne_init_one(struct zorro_dev *z,
160 const struct zorro_device_id *ent)
161{
162 unsigned long board = z->resource.start;
163 unsigned long base_addr = board+ARIADNE_LANCE;
164 unsigned long mem_start = board+ARIADNE_RAM;
165 struct resource *r1, *r2;
166 struct net_device *dev;
167 struct ariadne_private *priv;
168 int err;
169
170 r1 = request_mem_region(base_addr, sizeof(struct Am79C960), "Am79C960");
171 if (!r1)
172 return -EBUSY;
173 r2 = request_mem_region(mem_start, ARIADNE_RAM_SIZE, "RAM");
174 if (!r2) {
175 release_resource(r1);
176 return -EBUSY;
177 }
178
179 dev = alloc_etherdev(sizeof(struct ariadne_private));
180 if (dev == NULL) {
181 release_resource(r1);
182 release_resource(r2);
183 return -ENOMEM;
184 }
185
186 SET_MODULE_OWNER(dev);
187 priv = netdev_priv(dev);
188
189 r1->name = dev->name;
190 r2->name = dev->name;
191
192 dev->dev_addr[0] = 0x00;
193 dev->dev_addr[1] = 0x60;
194 dev->dev_addr[2] = 0x30;
195 dev->dev_addr[3] = (z->rom.er_SerialNumber>>16) & 0xff;
196 dev->dev_addr[4] = (z->rom.er_SerialNumber>>8) & 0xff;
197 dev->dev_addr[5] = z->rom.er_SerialNumber & 0xff;
198 dev->base_addr = ZTWO_VADDR(base_addr);
199 dev->mem_start = ZTWO_VADDR(mem_start);
200 dev->mem_end = dev->mem_start+ARIADNE_RAM_SIZE;
201
202 dev->open = &ariadne_open;
203 dev->stop = &ariadne_close;
204 dev->hard_start_xmit = &ariadne_start_xmit;
205 dev->tx_timeout = &ariadne_tx_timeout;
206 dev->watchdog_timeo = 5*HZ;
207 dev->get_stats = &ariadne_get_stats;
208 dev->set_multicast_list = &set_multicast_list;
209
210 err = register_netdev(dev);
211 if (err) {
212 release_resource(r1);
213 release_resource(r2);
214 free_netdev(dev);
215 return err;
216 }
217 zorro_set_drvdata(z, dev);
218
219 printk(KERN_INFO "%s: Ariadne at 0x%08lx, Ethernet Address "
220 "%02x:%02x:%02x:%02x:%02x:%02x\n", dev->name, board,
221 dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
222 dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
223
224 return 0;
225}
226
227
228static int ariadne_open(struct net_device *dev)
229{
230 volatile struct Am79C960 *lance = (struct Am79C960*)dev->base_addr;
231 u_short in;
232 u_long version;
233 int i;
234
235 /* Reset the LANCE */
236 in = lance->Reset;
237
238 /* Stop the LANCE */
239 lance->RAP = CSR0; /* PCnet-ISA Controller Status */
240 lance->RDP = STOP;
241
242 /* Check the LANCE version */
243 lance->RAP = CSR88; /* Chip ID */
244 version = swapw(lance->RDP);
245 lance->RAP = CSR89; /* Chip ID */
246 version |= swapw(lance->RDP)<<16;
247 if ((version & 0x00000fff) != 0x00000003) {
248 printk(KERN_WARNING "ariadne_open: Couldn't find AMD Ethernet Chip\n");
249 return -EAGAIN;
250 }
251 if ((version & 0x0ffff000) != 0x00003000) {
252 printk(KERN_WARNING "ariadne_open: Couldn't find Am79C960 (Wrong part "
253 "number = %ld)\n", (version & 0x0ffff000)>>12);
254 return -EAGAIN;
255 }
256#if 0
257 printk(KERN_DEBUG "ariadne_open: Am79C960 (PCnet-ISA) Revision %ld\n",
258 (version & 0xf0000000)>>28);
259#endif
260
261 ariadne_init_ring(dev);
262
263 /* Miscellaneous Stuff */
264 lance->RAP = CSR3; /* Interrupt Masks and Deferral Control */
265 lance->RDP = 0x0000;
266 lance->RAP = CSR4; /* Test and Features Control */
267 lance->RDP = DPOLL|APAD_XMT|MFCOM|RCVCCOM|TXSTRTM|JABM;
268
269 /* Set the Multicast Table */
270 lance->RAP = CSR8; /* Logical Address Filter, LADRF[15:0] */
271 lance->RDP = 0x0000;
272 lance->RAP = CSR9; /* Logical Address Filter, LADRF[31:16] */
273 lance->RDP = 0x0000;
274 lance->RAP = CSR10; /* Logical Address Filter, LADRF[47:32] */
275 lance->RDP = 0x0000;
276 lance->RAP = CSR11; /* Logical Address Filter, LADRF[63:48] */
277 lance->RDP = 0x0000;
278
279 /* Set the Ethernet Hardware Address */
280 lance->RAP = CSR12; /* Physical Address Register, PADR[15:0] */
281 lance->RDP = ((u_short *)&dev->dev_addr[0])[0];
282 lance->RAP = CSR13; /* Physical Address Register, PADR[31:16] */
283 lance->RDP = ((u_short *)&dev->dev_addr[0])[1];
284 lance->RAP = CSR14; /* Physical Address Register, PADR[47:32] */
285 lance->RDP = ((u_short *)&dev->dev_addr[0])[2];
286
287 /* Set the Init Block Mode */
288 lance->RAP = CSR15; /* Mode Register */
289 lance->RDP = 0x0000;
290
291 /* Set the Transmit Descriptor Ring Pointer */
292 lance->RAP = CSR30; /* Base Address of Transmit Ring */
293 lance->RDP = swloww(ARIADNE_RAM+offsetof(struct lancedata, tx_ring));
294 lance->RAP = CSR31; /* Base Address of transmit Ring */
295 lance->RDP = swhighw(ARIADNE_RAM+offsetof(struct lancedata, tx_ring));
296
297 /* Set the Receive Descriptor Ring Pointer */
298 lance->RAP = CSR24; /* Base Address of Receive Ring */
299 lance->RDP = swloww(ARIADNE_RAM+offsetof(struct lancedata, rx_ring));
300 lance->RAP = CSR25; /* Base Address of Receive Ring */
301 lance->RDP = swhighw(ARIADNE_RAM+offsetof(struct lancedata, rx_ring));
302
303 /* Set the Number of RX and TX Ring Entries */
304 lance->RAP = CSR76; /* Receive Ring Length */
305 lance->RDP = swapw(((u_short)-RX_RING_SIZE));
306 lance->RAP = CSR78; /* Transmit Ring Length */
307 lance->RDP = swapw(((u_short)-TX_RING_SIZE));
308
309 /* Enable Media Interface Port Auto Select (10BASE-2/10BASE-T) */
310 lance->RAP = ISACSR2; /* Miscellaneous Configuration */
311 lance->IDP = ASEL;
312
313 /* LED Control */
314 lance->RAP = ISACSR5; /* LED1 Status */
315 lance->IDP = PSE|XMTE;
316 lance->RAP = ISACSR6; /* LED2 Status */
317 lance->IDP = PSE|COLE;
318 lance->RAP = ISACSR7; /* LED3 Status */
319 lance->IDP = PSE|RCVE;
320
321 netif_start_queue(dev);
322
Thomas Gleixner1fb9df52006-07-01 19:29:39 -0700323 i = request_irq(IRQ_AMIGA_PORTS, ariadne_interrupt, IRQF_SHARED,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 dev->name, dev);
325 if (i) return i;
326
327 lance->RAP = CSR0; /* PCnet-ISA Controller Status */
328 lance->RDP = INEA|STRT;
329
330 return 0;
331}
332
333
334static void ariadne_init_ring(struct net_device *dev)
335{
336 struct ariadne_private *priv = netdev_priv(dev);
337 volatile struct lancedata *lancedata = (struct lancedata *)dev->mem_start;
338 int i;
339
340 netif_stop_queue(dev);
341
342 priv->tx_full = 0;
343 priv->cur_rx = priv->cur_tx = 0;
344 priv->dirty_tx = 0;
345
346 /* Set up TX Ring */
347 for (i = 0; i < TX_RING_SIZE; i++) {
348 volatile struct TDRE *t = &lancedata->tx_ring[i];
349 t->TMD0 = swloww(ARIADNE_RAM+offsetof(struct lancedata, tx_buff[i]));
350 t->TMD1 = swhighw(ARIADNE_RAM+offsetof(struct lancedata, tx_buff[i])) |
351 TF_STP | TF_ENP;
352 t->TMD2 = swapw((u_short)-PKT_BUF_SIZE);
353 t->TMD3 = 0;
354 priv->tx_ring[i] = &lancedata->tx_ring[i];
355 priv->tx_buff[i] = lancedata->tx_buff[i];
356#if 0
357 printk(KERN_DEBUG "TX Entry %2d at %p, Buf at %p\n", i,
358 &lancedata->tx_ring[i], lancedata->tx_buff[i]);
359#endif
360 }
361
362 /* Set up RX Ring */
363 for (i = 0; i < RX_RING_SIZE; i++) {
364 volatile struct RDRE *r = &lancedata->rx_ring[i];
365 r->RMD0 = swloww(ARIADNE_RAM+offsetof(struct lancedata, rx_buff[i]));
366 r->RMD1 = swhighw(ARIADNE_RAM+offsetof(struct lancedata, rx_buff[i])) |
367 RF_OWN;
368 r->RMD2 = swapw((u_short)-PKT_BUF_SIZE);
369 r->RMD3 = 0x0000;
370 priv->rx_ring[i] = &lancedata->rx_ring[i];
371 priv->rx_buff[i] = lancedata->rx_buff[i];
372#if 0
373 printk(KERN_DEBUG "RX Entry %2d at %p, Buf at %p\n", i,
374 &lancedata->rx_ring[i], lancedata->rx_buff[i]);
375#endif
376 }
377}
378
379
380static int ariadne_close(struct net_device *dev)
381{
382 struct ariadne_private *priv = netdev_priv(dev);
383 volatile struct Am79C960 *lance = (struct Am79C960*)dev->base_addr;
384
385 netif_stop_queue(dev);
386
387 lance->RAP = CSR112; /* Missed Frame Count */
388 priv->stats.rx_missed_errors = swapw(lance->RDP);
389 lance->RAP = CSR0; /* PCnet-ISA Controller Status */
390
391 if (ariadne_debug > 1) {
392 printk(KERN_DEBUG "%s: Shutting down ethercard, status was %2.2x.\n",
393 dev->name, lance->RDP);
394 printk(KERN_DEBUG "%s: %lu packets missed\n", dev->name,
395 priv->stats.rx_missed_errors);
396 }
397
398 /* We stop the LANCE here -- it occasionally polls memory if we don't. */
399 lance->RDP = STOP;
400
401 free_irq(IRQ_AMIGA_PORTS, dev);
402
403 return 0;
404}
405
406
407static inline void ariadne_reset(struct net_device *dev)
408{
409 volatile struct Am79C960 *lance = (struct Am79C960*)dev->base_addr;
410
411 lance->RAP = CSR0; /* PCnet-ISA Controller Status */
412 lance->RDP = STOP;
413 ariadne_init_ring(dev);
414 lance->RDP = INEA|STRT;
415 netif_start_queue(dev);
416}
417
418
419static irqreturn_t ariadne_interrupt(int irq, void *data, struct pt_regs *fp)
420{
421 struct net_device *dev = (struct net_device *)data;
422 volatile struct Am79C960 *lance = (struct Am79C960*)dev->base_addr;
423 struct ariadne_private *priv;
424 int csr0, boguscnt;
425 int handled = 0;
426
427 if (dev == NULL) {
428 printk(KERN_WARNING "ariadne_interrupt(): irq for unknown device.\n");
429 return IRQ_NONE;
430 }
431
432 lance->RAP = CSR0; /* PCnet-ISA Controller Status */
433
434 if (!(lance->RDP & INTR)) /* Check if any interrupt has been */
435 return IRQ_NONE; /* generated by the board. */
436
437 priv = netdev_priv(dev);
438
439 boguscnt = 10;
440 while ((csr0 = lance->RDP) & (ERR|RINT|TINT) && --boguscnt >= 0) {
441 /* Acknowledge all of the current interrupt sources ASAP. */
442 lance->RDP = csr0 & ~(INEA|TDMD|STOP|STRT|INIT);
443
444#if 0
445 if (ariadne_debug > 5) {
446 printk(KERN_DEBUG "%s: interrupt csr0=%#2.2x new csr=%#2.2x.",
447 dev->name, csr0, lance->RDP);
448 printk("[");
449 if (csr0 & INTR)
450 printk(" INTR");
451 if (csr0 & INEA)
452 printk(" INEA");
453 if (csr0 & RXON)
454 printk(" RXON");
455 if (csr0 & TXON)
456 printk(" TXON");
457 if (csr0 & TDMD)
458 printk(" TDMD");
459 if (csr0 & STOP)
460 printk(" STOP");
461 if (csr0 & STRT)
462 printk(" STRT");
463 if (csr0 & INIT)
464 printk(" INIT");
465 if (csr0 & ERR)
466 printk(" ERR");
467 if (csr0 & BABL)
468 printk(" BABL");
469 if (csr0 & CERR)
470 printk(" CERR");
471 if (csr0 & MISS)
472 printk(" MISS");
473 if (csr0 & MERR)
474 printk(" MERR");
475 if (csr0 & RINT)
476 printk(" RINT");
477 if (csr0 & TINT)
478 printk(" TINT");
479 if (csr0 & IDON)
480 printk(" IDON");
481 printk(" ]\n");
482 }
483#endif
484
485 if (csr0 & RINT) { /* Rx interrupt */
486 handled = 1;
487 ariadne_rx(dev);
488 }
489
490 if (csr0 & TINT) { /* Tx-done interrupt */
491 int dirty_tx = priv->dirty_tx;
492
493 handled = 1;
494 while (dirty_tx < priv->cur_tx) {
495 int entry = dirty_tx % TX_RING_SIZE;
496 int status = lowb(priv->tx_ring[entry]->TMD1);
497
498 if (status & TF_OWN)
499 break; /* It still hasn't been Txed */
500
501 priv->tx_ring[entry]->TMD1 &= 0xff00;
502
503 if (status & TF_ERR) {
504 /* There was an major error, log it. */
505 int err_status = priv->tx_ring[entry]->TMD3;
506 priv->stats.tx_errors++;
507 if (err_status & EF_RTRY)
508 priv->stats.tx_aborted_errors++;
509 if (err_status & EF_LCAR)
510 priv->stats.tx_carrier_errors++;
511 if (err_status & EF_LCOL)
512 priv->stats.tx_window_errors++;
513 if (err_status & EF_UFLO) {
514 /* Ackk! On FIFO errors the Tx unit is turned off! */
515 priv->stats.tx_fifo_errors++;
516 /* Remove this verbosity later! */
517 printk(KERN_ERR "%s: Tx FIFO error! Status %4.4x.\n",
518 dev->name, csr0);
519 /* Restart the chip. */
520 lance->RDP = STRT;
521 }
522 } else {
523 if (status & (TF_MORE|TF_ONE))
524 priv->stats.collisions++;
525 priv->stats.tx_packets++;
526 }
527 dirty_tx++;
528 }
529
530#ifndef final_version
531 if (priv->cur_tx - dirty_tx >= TX_RING_SIZE) {
532 printk(KERN_ERR "out-of-sync dirty pointer, %d vs. %d, "
533 "full=%d.\n", dirty_tx, priv->cur_tx, priv->tx_full);
534 dirty_tx += TX_RING_SIZE;
535 }
536#endif
537
538 if (priv->tx_full && netif_queue_stopped(dev) &&
539 dirty_tx > priv->cur_tx - TX_RING_SIZE + 2) {
540 /* The ring is no longer full. */
541 priv->tx_full = 0;
542 netif_wake_queue(dev);
543 }
544
545 priv->dirty_tx = dirty_tx;
546 }
547
548 /* Log misc errors. */
549 if (csr0 & BABL) {
550 handled = 1;
551 priv->stats.tx_errors++; /* Tx babble. */
552 }
553 if (csr0 & MISS) {
554 handled = 1;
555 priv->stats.rx_errors++; /* Missed a Rx frame. */
556 }
557 if (csr0 & MERR) {
558 handled = 1;
559 printk(KERN_ERR "%s: Bus master arbitration failure, status "
560 "%4.4x.\n", dev->name, csr0);
561 /* Restart the chip. */
562 lance->RDP = STRT;
563 }
564 }
565
566 /* Clear any other interrupt, and set interrupt enable. */
567 lance->RAP = CSR0; /* PCnet-ISA Controller Status */
568 lance->RDP = INEA|BABL|CERR|MISS|MERR|IDON;
569
570#if 0
571 if (ariadne_debug > 4)
572 printk(KERN_DEBUG "%s: exiting interrupt, csr%d=%#4.4x.\n", dev->name,
573 lance->RAP, lance->RDP);
574#endif
575 return IRQ_RETVAL(handled);
576}
577
578
579static void ariadne_tx_timeout(struct net_device *dev)
580{
581 volatile struct Am79C960 *lance = (struct Am79C960*)dev->base_addr;
582
583 printk(KERN_ERR "%s: transmit timed out, status %4.4x, resetting.\n",
584 dev->name, lance->RDP);
585 ariadne_reset(dev);
586 netif_wake_queue(dev);
587}
588
589
590static int ariadne_start_xmit(struct sk_buff *skb, struct net_device *dev)
591{
592 struct ariadne_private *priv = netdev_priv(dev);
593 volatile struct Am79C960 *lance = (struct Am79C960*)dev->base_addr;
594 int entry;
595 unsigned long flags;
596 int len = skb->len;
597
598#if 0
599 if (ariadne_debug > 3) {
600 lance->RAP = CSR0; /* PCnet-ISA Controller Status */
601 printk(KERN_DEBUG "%s: ariadne_start_xmit() called, csr0 %4.4x.\n",
602 dev->name, lance->RDP);
603 lance->RDP = 0x0000;
604 }
605#endif
606
607 /* FIXME: is the 79C960 new enough to do its own padding right ? */
608 if (skb->len < ETH_ZLEN)
609 {
Herbert Xu5b057c62006-06-23 02:06:41 -0700610 if (skb_padto(skb, ETH_ZLEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 return 0;
612 len = ETH_ZLEN;
613 }
614
615 /* Fill in a Tx ring entry */
616
617#if 0
618 printk(KERN_DEBUG "TX pkt type 0x%04x from ", ((u_short *)skb->data)[6]);
619 {
620 int i;
621 u_char *ptr = &((u_char *)skb->data)[6];
622 for (i = 0; i < 6; i++)
623 printk("%02x", ptr[i]);
624 }
625 printk(" to ");
626 {
627 int i;
628 u_char *ptr = (u_char *)skb->data;
629 for (i = 0; i < 6; i++)
630 printk("%02x", ptr[i]);
631 }
632 printk(" data 0x%08x len %d\n", (int)skb->data, (int)skb->len);
633#endif
634
635 local_irq_save(flags);
636
637 entry = priv->cur_tx % TX_RING_SIZE;
638
639 /* Caution: the write order is important here, set the base address with
640 the "ownership" bits last. */
641
642 priv->tx_ring[entry]->TMD2 = swapw((u_short)-skb->len);
643 priv->tx_ring[entry]->TMD3 = 0x0000;
644 memcpyw(priv->tx_buff[entry], (u_short *)skb->data, len);
645
646#if 0
647 {
648 int i, len;
649
650 len = skb->len > 64 ? 64 : skb->len;
651 len >>= 1;
652 for (i = 0; i < len; i += 8) {
653 int j;
654 printk(KERN_DEBUG "%04x:", i);
655 for (j = 0; (j < 8) && ((i+j) < len); j++) {
656 if (!(j & 1))
657 printk(" ");
658 printk("%04x", priv->tx_buff[entry][i+j]);
659 }
660 printk("\n");
661 }
662 }
663#endif
664
665 priv->tx_ring[entry]->TMD1 = (priv->tx_ring[entry]->TMD1&0xff00)|TF_OWN|TF_STP|TF_ENP;
666
667 dev_kfree_skb(skb);
668
669 priv->cur_tx++;
670 if ((priv->cur_tx >= TX_RING_SIZE) && (priv->dirty_tx >= TX_RING_SIZE)) {
671
672#if 0
673 printk(KERN_DEBUG "*** Subtracting TX_RING_SIZE from cur_tx (%d) and "
674 "dirty_tx (%d)\n", priv->cur_tx, priv->dirty_tx);
675#endif
676
677 priv->cur_tx -= TX_RING_SIZE;
678 priv->dirty_tx -= TX_RING_SIZE;
679 }
680
681 /* Trigger an immediate send poll. */
682 lance->RAP = CSR0; /* PCnet-ISA Controller Status */
683 lance->RDP = INEA|TDMD;
684
685 dev->trans_start = jiffies;
686
687 if (lowb(priv->tx_ring[(entry+1) % TX_RING_SIZE]->TMD1) != 0) {
688 netif_stop_queue(dev);
689 priv->tx_full = 1;
690 }
691 local_irq_restore(flags);
692
693 return 0;
694}
695
696
697static int ariadne_rx(struct net_device *dev)
698{
699 struct ariadne_private *priv = netdev_priv(dev);
700 int entry = priv->cur_rx % RX_RING_SIZE;
701 int i;
702
703 /* If we own the next entry, it's a new packet. Send it up. */
704 while (!(lowb(priv->rx_ring[entry]->RMD1) & RF_OWN)) {
705 int status = lowb(priv->rx_ring[entry]->RMD1);
706
707 if (status != (RF_STP|RF_ENP)) { /* There was an error. */
708 /* There is a tricky error noted by John Murphy,
709 <murf@perftech.com> to Russ Nelson: Even with full-sized
710 buffers it's possible for a jabber packet to use two
711 buffers, with only the last correctly noting the error. */
712 if (status & RF_ENP)
713 /* Only count a general error at the end of a packet.*/
714 priv->stats.rx_errors++;
715 if (status & RF_FRAM)
716 priv->stats.rx_frame_errors++;
717 if (status & RF_OFLO)
718 priv->stats.rx_over_errors++;
719 if (status & RF_CRC)
720 priv->stats.rx_crc_errors++;
721 if (status & RF_BUFF)
722 priv->stats.rx_fifo_errors++;
723 priv->rx_ring[entry]->RMD1 &= 0xff00|RF_STP|RF_ENP;
724 } else {
725 /* Malloc up new buffer, compatible with net-3. */
726 short pkt_len = swapw(priv->rx_ring[entry]->RMD3);
727 struct sk_buff *skb;
728
729 skb = dev_alloc_skb(pkt_len+2);
730 if (skb == NULL) {
731 printk(KERN_WARNING "%s: Memory squeeze, deferring packet.\n",
732 dev->name);
733 for (i = 0; i < RX_RING_SIZE; i++)
734 if (lowb(priv->rx_ring[(entry+i) % RX_RING_SIZE]->RMD1) & RF_OWN)
735 break;
736
737 if (i > RX_RING_SIZE-2) {
738 priv->stats.rx_dropped++;
739 priv->rx_ring[entry]->RMD1 |= RF_OWN;
740 priv->cur_rx++;
741 }
742 break;
743 }
744
745
746 skb->dev = dev;
747 skb_reserve(skb,2); /* 16 byte align */
748 skb_put(skb,pkt_len); /* Make room */
749 eth_copy_and_sum(skb, (char *)priv->rx_buff[entry], pkt_len,0);
750 skb->protocol=eth_type_trans(skb,dev);
751#if 0
752 printk(KERN_DEBUG "RX pkt type 0x%04x from ",
753 ((u_short *)skb->data)[6]);
754 {
755 int i;
756 u_char *ptr = &((u_char *)skb->data)[6];
757 for (i = 0; i < 6; i++)
758 printk("%02x", ptr[i]);
759 }
760 printk(" to ");
761 {
762 int i;
763 u_char *ptr = (u_char *)skb->data;
764 for (i = 0; i < 6; i++)
765 printk("%02x", ptr[i]);
766 }
767 printk(" data 0x%08x len %d\n", (int)skb->data, (int)skb->len);
768#endif
769
770 netif_rx(skb);
771 dev->last_rx = jiffies;
772 priv->stats.rx_packets++;
773 priv->stats.rx_bytes += pkt_len;
774 }
775
776 priv->rx_ring[entry]->RMD1 |= RF_OWN;
777 entry = (++priv->cur_rx) % RX_RING_SIZE;
778 }
779
780 priv->cur_rx = priv->cur_rx % RX_RING_SIZE;
781
782 /* We should check that at least two ring entries are free. If not,
783 we should free one and mark stats->rx_dropped++. */
784
785 return 0;
786}
787
788
789static struct net_device_stats *ariadne_get_stats(struct net_device *dev)
790{
791 struct ariadne_private *priv = netdev_priv(dev);
792 volatile struct Am79C960 *lance = (struct Am79C960*)dev->base_addr;
793 short saved_addr;
794 unsigned long flags;
795
796 local_irq_save(flags);
797 saved_addr = lance->RAP;
798 lance->RAP = CSR112; /* Missed Frame Count */
799 priv->stats.rx_missed_errors = swapw(lance->RDP);
800 lance->RAP = saved_addr;
801 local_irq_restore(flags);
802
803 return &priv->stats;
804}
805
806
807/* Set or clear the multicast filter for this adaptor.
808 num_addrs == -1 Promiscuous mode, receive all packets
809 num_addrs == 0 Normal mode, clear multicast list
810 num_addrs > 0 Multicast mode, receive normal and MC packets, and do
811 best-effort filtering.
812 */
813static void set_multicast_list(struct net_device *dev)
814{
815 volatile struct Am79C960 *lance = (struct Am79C960*)dev->base_addr;
816
817 if (!netif_running(dev))
818 return;
819
820 netif_stop_queue(dev);
821
822 /* We take the simple way out and always enable promiscuous mode. */
823 lance->RAP = CSR0; /* PCnet-ISA Controller Status */
824 lance->RDP = STOP; /* Temporarily stop the lance. */
825 ariadne_init_ring(dev);
826
827 if (dev->flags & IFF_PROMISC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 lance->RAP = CSR15; /* Mode Register */
829 lance->RDP = PROM; /* Set promiscuous mode */
830 } else {
831 short multicast_table[4];
832 int num_addrs = dev->mc_count;
833 int i;
834 /* We don't use the multicast table, but rely on upper-layer filtering. */
835 memset(multicast_table, (num_addrs == 0) ? 0 : -1,
836 sizeof(multicast_table));
837 for (i = 0; i < 4; i++) {
838 lance->RAP = CSR8+(i<<8); /* Logical Address Filter */
839 lance->RDP = swapw(multicast_table[i]);
840 }
841 lance->RAP = CSR15; /* Mode Register */
842 lance->RDP = 0x0000; /* Unset promiscuous mode */
843 }
844
845 lance->RAP = CSR0; /* PCnet-ISA Controller Status */
846 lance->RDP = INEA|STRT|IDON; /* Resume normal operation. */
847
848 netif_wake_queue(dev);
849}
850
851
852static void __devexit ariadne_remove_one(struct zorro_dev *z)
853{
854 struct net_device *dev = zorro_get_drvdata(z);
855
856 unregister_netdev(dev);
857 release_mem_region(ZTWO_PADDR(dev->base_addr), sizeof(struct Am79C960));
858 release_mem_region(ZTWO_PADDR(dev->mem_start), ARIADNE_RAM_SIZE);
859 free_netdev(dev);
860}
861
862static int __init ariadne_init_module(void)
863{
Bjorn Helgaas33d86752006-03-25 03:07:20 -0800864 return zorro_register_driver(&ariadne_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865}
866
867static void __exit ariadne_cleanup_module(void)
868{
869 zorro_unregister_driver(&ariadne_driver);
870}
871
872module_init(ariadne_init_module);
873module_exit(ariadne_cleanup_module);
874
875MODULE_LICENSE("GPL");