1 /* 3c501.c: A 3Com 3c501 Ethernet driver for Linux. */
2 /*
3 Written 1992,1993,1994 Donald Becker
4
5 Copyright 1993 United States Government as represented by the
6 Director, National Security Agency. This software may be used and
7 distributed according to the terms of the GNU Public License,
8 incorporated herein by reference.
9
10 This is a device driver for the 3Com Etherlink 3c501.
11 Do not purchase this card, even as a joke. It's performance is horrible,
12 and it breaks in many ways.
13
14 The author may be reached as becker@CESDIS.gsfc.nasa.gov, or C/O
15 Center of Excellence in Space Data and Information Sciences
16 Code 930.5, Goddard Space Flight Center, Greenbelt MD 20771
17
18 Fixed (again!) the missing interrupt locking on TX/RX shifting.
19 Alan Cox <Alan.Cox@linux.org>
20
21 Removed calls to init_etherdev since they are no longer needed, and
22 cleaned up modularization just a bit. The driver still allows only
23 the default address for cards when loaded as a module, but that's
24 really less braindead than anyone using a 3c501 board. :)
25 19950208 (invid@msen.com)
26
27 Added traps for interrupts hitting the window as we clear and TX load
28 the board. Now getting 150K/second FTP with a 3c501 card. Still playing
29 with a TX-TX optimisation to see if we can touch 180-200K/second as seems
30 theoretically maximum.
31 19950402 Alan Cox <Alan.Cox@linux.org>
32
33 Cleaned up for 2.3.x because we broke SMP now.
34 20000208 Alan Cox <alan@redhat.com>
35
36 */
37
38
39 /**
40 * DOC: 3c501 Card Notes
41 *
42 * Some notes on this thing if you have to hack it. [Alan]
43 *
44 * Some documentation is available from 3Com. Due to the boards age
45 * standard responses when you ask for this will range from 'be serious'
46 * to 'give it to a museum'. The documentation is incomplete and mostly
47 * of historical interest anyway.
48 *
49 * The basic system is a single buffer which can be used to receive or
50 * transmit a packet. A third command mode exists when you are setting
51 * things up.
52 *
53 * If it's transmitting it's not receiving and vice versa. In fact the
54 * time to get the board back into useful state after an operation is
55 * quite large.
56 *
57 * The driver works by keeping the board in receive mode waiting for a
58 * packet to arrive. When one arrives it is copied out of the buffer
59 * and delivered to the kernel. The card is reloaded and off we go.
60 *
61 * When transmitting lp->txing is set and the card is reset (from
62 * receive mode) [possibly losing a packet just received] to command
63 * mode. A packet is loaded and transmit mode triggered. The interrupt
64 * handler runs different code for transmit interrupts and can handle
65 * returning to receive mode or retransmissions (yes you have to help
66 * out with those too).
67 *
68 * DOC: Problems
69 *
70 * There are a wide variety of undocumented error returns from the card
71 * and you basically have to kick the board and pray if they turn up. Most
72 * only occur under extreme load or if you do something the board doesn't
73 * like (eg touching a register at the wrong time).
74 *
75 * The driver is less efficient than it could be. It switches through
76 * receive mode even if more transmits are queued. If this worries you buy
77 * a real Ethernet card.
78 *
79 * The combination of slow receive restart and no real multicast
80 * filter makes the board unusable with a kernel compiled for IP
81 * multicasting in a real multicast environment. That's down to the board,
82 * but even with no multicast programs running a multicast IP kernel is
83 * in group 224.0.0.1 and you will therefore be listening to all multicasts.
84 * One nv conference running over that Ethernet and you can give up.
85 *
86 */
87
88 static const char *version =
89 "3c501.c: 2000/02/08 Alan Cox (alan@redhat.com).\n";
90
91 /*
92 * Braindamage remaining:
93 * The 3c501 board.
94 */
95
96 #include <linux/module.h>
97
98 #include <linux/kernel.h>
99 #include <linux/sched.h>
100 #include <linux/ptrace.h>
101 #include <linux/fcntl.h>
102 #include <linux/ioport.h>
103 #include <linux/interrupt.h>
104 #include <linux/malloc.h>
105 #include <linux/string.h>
106 #include <linux/errno.h>
107 #include <linux/config.h> /* for CONFIG_IP_MULTICAST */
108 #include <linux/spinlock.h>
109
110 #include <asm/bitops.h>
111 #include <asm/io.h>
112
113 #include <linux/netdevice.h>
114 #include <linux/etherdevice.h>
115 #include <linux/skbuff.h>
116 #include <linux/init.h>
117
118 /* A zero-terminated list of I/O addresses to be probed.
119 The 3c501 can be at many locations, but here are the popular ones. */
120 static unsigned int netcard_portlist[] __initdata = {
121 0x280, 0x300, 0
122 };
123
124
125 /*
126 * Index to functions.
127 */
128
129 int el1_probe(struct net_device *dev);
130 static int el1_probe1(struct net_device *dev, int ioaddr);
131 static int el_open(struct net_device *dev);
132 static void el_timeout(struct net_device *dev);
133 static int el_start_xmit(struct sk_buff *skb, struct net_device *dev);
134 static void el_interrupt(int irq, void *dev_id, struct pt_regs *regs);
135 static void el_receive(struct net_device *dev);
136 static void el_reset(struct net_device *dev);
137 static int el1_close(struct net_device *dev);
138 static struct net_device_stats *el1_get_stats(struct net_device *dev);
139 static void set_multicast_list(struct net_device *dev);
140
141 #define EL1_IO_EXTENT 16
142
143 #ifndef EL_DEBUG
144 #define EL_DEBUG 0 /* use 0 for production, 1 for devel., >2 for debug */
145 #endif /* Anything above 5 is wordy death! */
146 static int el_debug = EL_DEBUG;
147
148 /*
149 * Board-specific info in dev->priv.
150 */
151
152 struct net_local
153 {
154 struct net_device_stats stats;
155 int tx_pkt_start; /* The length of the current Tx packet. */
156 int collisions; /* Tx collisions this packet */
157 int loading; /* Spot buffer load collisions */
158 int txing; /* True if card is in TX mode */
159 spinlock_t lock; /* Serializing lock */
160 };
161
162
163 #define RX_STATUS (ioaddr + 0x06)
164 #define RX_CMD RX_STATUS
165 #define TX_STATUS (ioaddr + 0x07)
166 #define TX_CMD TX_STATUS
167 #define GP_LOW (ioaddr + 0x08)
168 #define GP_HIGH (ioaddr + 0x09)
169 #define RX_BUF_CLR (ioaddr + 0x0A)
170 #define RX_LOW (ioaddr + 0x0A)
171 #define RX_HIGH (ioaddr + 0x0B)
172 #define SAPROM (ioaddr + 0x0C)
173 #define AX_STATUS (ioaddr + 0x0E)
174 #define AX_CMD AX_STATUS
175 #define DATAPORT (ioaddr + 0x0F)
176 #define TX_RDY 0x08 /* In TX_STATUS */
177
178 #define EL1_DATAPTR 0x08
179 #define EL1_RXPTR 0x0A
180 #define EL1_SAPROM 0x0C
181 #define EL1_DATAPORT 0x0f
182
183 /*
184 * Writes to the ax command register.
185 */
186
187 #define AX_OFF 0x00 /* Irq off, buffer access on */
188 #define AX_SYS 0x40 /* Load the buffer */
189 #define AX_XMIT 0x44 /* Transmit a packet */
190 #define AX_RX 0x48 /* Receive a packet */
191 #define AX_LOOP 0x0C /* Loopback mode */
192 #define AX_RESET 0x80
193
194 /*
195 * Normal receive mode written to RX_STATUS. We must intr on short packets
196 * to avoid bogus rx lockups.
197 */
198
199 #define RX_NORM 0xA8 /* 0x68 == all addrs, 0xA8 only to me. */
200 #define RX_PROM 0x68 /* Senior Prom, uhmm promiscuous mode. */
201 #define RX_MULT 0xE8 /* Accept multicast packets. */
202 #define TX_NORM 0x0A /* Interrupt on everything that might hang the chip */
203
204 /*
205 * TX_STATUS register.
206 */
207
208 #define TX_COLLISION 0x02
209 #define TX_16COLLISIONS 0x04
210 #define TX_READY 0x08
211
212 #define RX_RUNT 0x08
213 #define RX_MISSED 0x01 /* Missed a packet due to 3c501 braindamage. */
214 #define RX_GOOD 0x30 /* Good packet 0x20, or simple overflow 0x10. */
215
216
217 /*
218 * The boilerplate probe code.
219 */
220
221 /**
222 * el1_probe:
223 * @dev: The device structure passed in to probe.
224 *
225 * This can be called from two places. The network layer will probe using
226 * a device structure passed in with the probe information completed. For a
227 * modular driver we use #init_module to fill in our own structure and probe
228 * for it.
229 *
230 * Returns 0 on success. ENXIO if asked not to probe and ENODEV if asked to
231 * probe and failing to find anything.
232 */
233
234 int __init el1_probe(struct net_device *dev)
235 {
236 int i;
237 int base_addr = dev->base_addr;
238
239 SET_MODULE_OWNER(dev);
240
241 if (base_addr > 0x1ff) /* Check a single specified location. */
242 return el1_probe1(dev, base_addr);
243 else if (base_addr != 0) /* Don't probe at all. */
244 return -ENXIO;
245
246 for (i = 0; netcard_portlist[i]; i++)
247 if (el1_probe1(dev, netcard_portlist[i]) == 0)
248 return 0;
249
250 return -ENODEV;
251 }
252
253 /**
254 * el1_probe:
255 * @dev: The device structure to use
256 * @ioaddr: An I/O address to probe at.
257 *
258 * The actual probe. This is iterated over by #el1_probe in order to
259 * check all the applicable device locations.
260 *
261 * Returns 0 for a success, in which case the device is activated,
262 * EAGAIN if the IRQ is in use by another driver, and ENODEV if the
263 * board cannot be found.
264 */
265
266 static int __init el1_probe1(struct net_device *dev, int ioaddr)
267 {
268 struct net_local *lp;
269 const char *mname; /* Vendor name */
270 unsigned char station_addr[6];
271 int autoirq = 0;
272 int i;
273
274 /*
275 * Reserve I/O resource for exclusive use by this driver
276 */
277
278 if (!request_region(ioaddr, EL1_IO_EXTENT, dev->name))
279 return -ENODEV;
280
281 /*
282 * Read the station address PROM data from the special port.
283 */
284
285 for (i = 0; i < 6; i++)
286 {
287 outw(i, ioaddr + EL1_DATAPTR);
288 station_addr[i] = inb(ioaddr + EL1_SAPROM);
289 }
290 /*
291 * Check the first three octets of the S.A. for 3Com's prefix, or
292 * for the Sager NP943 prefix.
293 */
294
295 if (station_addr[0] == 0x02 && station_addr[1] == 0x60
296 && station_addr[2] == 0x8c)
297 {
298 mname = "3c501";
299 } else if (station_addr[0] == 0x00 && station_addr[1] == 0x80
300 && station_addr[2] == 0xC8)
301 {
302 mname = "NP943";
303 }
304 else {
305 release_region(ioaddr, EL1_IO_EXTENT);
306 return -ENODEV;
307 }
308
309 /*
310 * We auto-IRQ by shutting off the interrupt line and letting it float
311 * high.
312 */
313
314 if (dev->irq < 2)
315 {
316 autoirq_setup(2);
317 inb(RX_STATUS); /* Clear pending interrupts. */
318 inb(TX_STATUS);
319 outb(AX_LOOP + 1, AX_CMD);
320
321 outb(0x00, AX_CMD);
322
323 autoirq = autoirq_report(1);
324
325 if (autoirq == 0)
326 {
327 printk("%s probe at %#x failed to detect IRQ line.\n",
328 mname, ioaddr);
329 release_region(ioaddr, EL1_IO_EXTENT);
330 return -EAGAIN;
331 }
332 }
333
334 outb(AX_RESET+AX_LOOP, AX_CMD); /* Loopback mode. */
335 dev->base_addr = ioaddr;
336 memcpy(dev->dev_addr, station_addr, ETH_ALEN);
337
338 if (dev->mem_start & 0xf)
339 el_debug = dev->mem_start & 0x7;
340 if (autoirq)
341 dev->irq = autoirq;
342
343 printk(KERN_INFO "%s: %s EtherLink at %#lx, using %sIRQ %d.\n", dev->name, mname, dev->base_addr,
344 autoirq ? "auto":"assigned ", dev->irq);
345
346 #ifdef CONFIG_IP_MULTICAST
347 printk(KERN_WARNING "WARNING: Use of the 3c501 in a multicast kernel is NOT recommended.\n");
348 #endif
349
350 if (el_debug)
351 printk("%s", version);
352
353 /*
354 * Initialize the device structure.
355 */
356
357 dev->priv = kmalloc(sizeof(struct net_local), GFP_KERNEL);
358 if (dev->priv == NULL) {
359 release_region(ioaddr, EL1_IO_EXTENT);
360 return -ENOMEM;
361 }
362 memset(dev->priv, 0, sizeof(struct net_local));
363
364 lp=dev->priv;
365 spin_lock_init(&lp->lock);
366
367 /*
368 * The EL1-specific entries in the device structure.
369 */
370
371 dev->open = &el_open;
372 dev->hard_start_xmit = &el_start_xmit;
373 dev->tx_timeout = &el_timeout;
374 dev->watchdog_timeo = HZ;
375 dev->stop = &el1_close;
376 dev->get_stats = &el1_get_stats;
377 dev->set_multicast_list = &set_multicast_list;
378
379 /*
380 * Setup the generic properties
381 */
382
383 ether_setup(dev);
384
385 return 0;
386 }
387
388 /**
389 * el1_open:
390 * @dev: device that is being opened
391 *
392 * When an ifconfig is issued which changes the device flags to include
393 * IFF_UP this function is called. It is only called when the change
394 * occurs, not when the interface remains up. #el1_close will be called
395 * when it goes down.
396 *
397 * Returns 0 for a successful open, or -EAGAIN if someone has run off
398 * with our interrupt line.
399 */
400
401 static int el_open(struct net_device *dev)
402 {
403 int retval;
404 int ioaddr = dev->base_addr;
405 struct net_local *lp = (struct net_local *)dev->priv;
406 unsigned long flags;
407
408 if (el_debug > 2)
409 printk("%s: Doing el_open()...", dev->name);
410
411 if ((retval = request_irq(dev->irq, &el_interrupt, 0, dev->name, dev)))
412 return retval;
413
414 spin_lock_irqsave(&lp->lock, flags);
415 el_reset(dev);
416 spin_unlock_irqrestore(&lp->lock, flags);
417
418 lp->txing = 0; /* Board in RX mode */
419 outb(AX_RX, AX_CMD); /* Aux control, irq and receive enabled */
420 netif_start_queue(dev);
421 return 0;
422 }
423
424 /**
425 * el_timeout:
426 * @dev: The 3c501 card that has timed out
427 *
428 * Attempt to restart the board. This is basically a mixture of extreme
429 * violence and prayer
430 *
431 */
432
433 static void el_timeout(struct net_device *dev)
434 {
435 struct net_local *lp = (struct net_local *)dev->priv;
436 int ioaddr = dev->base_addr;
437
438 if (el_debug)
439 printk (KERN_DEBUG "%s: transmit timed out, txsr %#2x axsr=%02x rxsr=%02x.\n",
440 dev->name, inb(TX_STATUS), inb(AX_STATUS), inb(RX_STATUS));
441 lp->stats.tx_errors++;
442 outb(TX_NORM, TX_CMD);
443 outb(RX_NORM, RX_CMD);
444 outb(AX_OFF, AX_CMD); /* Just trigger a false interrupt. */
445 outb(AX_RX, AX_CMD); /* Aux control, irq and receive enabled */
446 lp->txing = 0; /* Ripped back in to RX */
447 netif_wake_queue(dev);
448 }
449
450
451 /**
452 * el_start_xmit:
453 * @skb: The packet that is queued to be sent
454 * @dev: The 3c501 card we want to throw it down
455 *
456 * Attempt to send a packet to a 3c501 card. There are some interesting
457 * catches here because the 3c501 is an extremely old and therefore
458 * stupid piece of technology.
459 *
460 * If we are handling an interrupt on the other CPU we cannot load a packet
461 * as we may still be attempting to retrieve the last RX packet buffer.
462 *
463 * When a transmit times out we dump the card into control mode and just
464 * start again. It happens enough that it isnt worth logging.
465 *
466 * We avoid holding the spin locks when doing the packet load to the board.
467 * The device is very slow, and its DMA mode is even slower. If we held the
468 * lock while loading 1500 bytes onto the controller we would drop a lot of
469 * serial port characters. This requires we do extra locking, but we have
470 * no real choice.
471 */
472
473 static int el_start_xmit(struct sk_buff *skb, struct net_device *dev)
474 {
475 struct net_local *lp = (struct net_local *)dev->priv;
476 int ioaddr = dev->base_addr;
477 unsigned long flags;
478
479 /*
480 * Avoid incoming interrupts between us flipping txing and flipping
481 * mode as the driver assumes txing is a faithful indicator of card
482 * state
483 */
484
485 spin_lock_irqsave(&lp->lock, flags);
486
487 /*
488 * Avoid timer-based retransmission conflicts.
489 */
490
491 netif_stop_queue(dev);
492
493 do
494 {
495 int gp_start = 0x800 - (ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN);
496 unsigned char *buf = skb->data;
497
498 lp->tx_pkt_start = gp_start;
499 lp->collisions = 0;
500
501 lp->stats.tx_bytes += skb->len;
502
503 /*
504 * Command mode with status cleared should [in theory]
505 * mean no more interrupts can be pending on the card.
506 */
507
508 outb_p(AX_SYS, AX_CMD);
509 inb_p(RX_STATUS);
510 inb_p(TX_STATUS);
511
512 lp->loading = 1;
513 lp->txing = 1;
514
515 /*
516 * Turn interrupts back on while we spend a pleasant afternoon
517 * loading bytes into the board
518 */
519
520 spin_unlock_irqrestore(&lp->lock, flags);
521
522 outw(0x00, RX_BUF_CLR); /* Set rx packet area to 0. */
523 outw(gp_start, GP_LOW); /* aim - packet will be loaded into buffer start */
524 outsb(DATAPORT,buf,skb->len); /* load buffer (usual thing each byte increments the pointer) */
525 outw(gp_start, GP_LOW); /* the board reuses the same register */
526
527 if(lp->loading != 2)
528 {
529 outb(AX_XMIT, AX_CMD); /* fire ... Trigger xmit. */
530 lp->loading=0;
531 dev->trans_start = jiffies;
532 if (el_debug > 2)
533 printk(" queued xmit.\n");
534 dev_kfree_skb (skb);
535 return 0;
536 }
537 /* A receive upset our load, despite our best efforts */
538 if(el_debug>2)
539 printk("%s: burped during tx load.\n", dev->name);
540 spin_lock_irqsave(&lp->lock, flags);
541 }
542 while(1);
543
544 }
545
546
547 /**
548 * el_interrupt:
549 * @irq: Interrupt number
550 * @dev_id: The 3c501 that burped
551 * @regs: Register data (surplus to our requirements)
552 *
553 * Handle the ether interface interrupts. The 3c501 needs a lot more
554 * hand holding than most cards. In paticular we get a transmit interrupt
555 * with a collision error because the board firmware isnt capable of rewinding
556 * its own transmit buffer pointers. It can however count to 16 for us.
557 *
558 * On the receive side the card is also very dumb. It has no buffering to
559 * speak of. We simply pull the packet out of its PIO buffer (which is slow)
560 * and queue it for the kernel. Then we reset the card for the next packet.
561 *
562 * We sometimes get suprise interrupts late both because the SMP IRQ delivery
563 * is message passing and because the card sometimes seems to deliver late. I
564 * think if it is part way through a receive and the mode is changed it carries
565 * on receiving and sends us an interrupt. We have to band aid all these cases
566 * to get a sensible 150kbytes/second performance. Even then you want a small
567 * TCP window.
568 */
569
570 static void el_interrupt(int irq, void *dev_id, struct pt_regs *regs)
571 {
572 struct net_device *dev = dev_id;
573 struct net_local *lp;
574 int ioaddr;
575 int axsr; /* Aux. status reg. */
576
577 ioaddr = dev->base_addr;
578 lp = (struct net_local *)dev->priv;
579
580 spin_lock(&lp->lock);
581
582 /*
583 * What happened ?
584 */
585
586 axsr = inb(AX_STATUS);
587
588 /*
589 * Log it
590 */
591
592 if (el_debug > 3)
593 printk(KERN_DEBUG "%s: el_interrupt() aux=%#02x", dev->name, axsr);
594
595 if(lp->loading==1 && !lp->txing)
596 printk(KERN_WARNING "%s: Inconsistent state loading while not in tx\n",
597 dev->name);
598
599 if (lp->txing)
600 {
601
602 /*
603 * Board in transmit mode. May be loading. If we are
604 * loading we shouldn't have got this.
605 */
606
607 int txsr = inb(TX_STATUS);
608
609 if(lp->loading==1)
610 {
611 if(el_debug > 2)
612 {
613 printk(KERN_DEBUG "%s: Interrupt while loading [", dev->name);
614 printk(" txsr=%02x gp=%04x rp=%04x]\n", txsr, inw(GP_LOW),inw(RX_LOW));
615 }
616 lp->loading=2; /* Force a reload */
617 spin_unlock(&lp->lock);
618 return;
619 }
620
621 if (el_debug > 6)
622 printk(KERN_DEBUG " txsr=%02x gp=%04x rp=%04x", txsr, inw(GP_LOW),inw(RX_LOW));
623
624 if ((axsr & 0x80) && (txsr & TX_READY) == 0)
625 {
626 /*
627 * FIXME: is there a logic to whether to keep on trying or
628 * reset immediately ?
629 */
630 if(el_debug>1)
631 printk("%s: Unusual interrupt during Tx, txsr=%02x axsr=%02x"
632 " gp=%03x rp=%03x.\n", dev->name, txsr, axsr,
633 inw(ioaddr + EL1_DATAPTR), inw(ioaddr + EL1_RXPTR));
634 lp->txing = 0;
635 netif_wake_queue(dev);
636 }
637 else if (txsr & TX_16COLLISIONS)
638 {
639 /*
640 * Timed out
641 */
642 if (el_debug)
643 printk("%s: Transmit failed 16 times, Ethernet jammed?\n",dev->name);
644 outb(AX_SYS, AX_CMD);
645 lp->txing = 0;
646 lp->stats.tx_aborted_errors++;
647 netif_wake_queue(dev);
648 }
649 else if (txsr & TX_COLLISION)
650 {
651 /*
652 * Retrigger xmit.
653 */
654
655 if (el_debug > 6)
656 printk(" retransmitting after a collision.\n");
657 /*
658 * Poor little chip can't reset its own start pointer
659 */
660
661 outb(AX_SYS, AX_CMD);
662 outw(lp->tx_pkt_start, GP_LOW);
663 outb(AX_XMIT, AX_CMD);
664 lp->stats.collisions++;
665 spin_unlock(&lp->lock);
666 return;
667 }
668 else
669 {
670 /*
671 * It worked.. we will now fall through and receive
672 */
673 lp->stats.tx_packets++;
674 if (el_debug > 6)
675 printk(" Tx succeeded %s\n",
676 (txsr & TX_RDY) ? "." : "but tx is busy!");
677 /*
678 * This is safe the interrupt is atomic WRT itself.
679 */
680
681 lp->txing = 0;
682 netif_wake_queue(dev); /* In case more to transmit */
683 }
684 }
685 else
686 {
687 /*
688 * In receive mode.
689 */
690
691 int rxsr = inb(RX_STATUS);
692 if (el_debug > 5)
693 printk(" rxsr=%02x txsr=%02x rp=%04x", rxsr, inb(TX_STATUS),inw(RX_LOW));
694 /*
695 * Just reading rx_status fixes most errors.
696 */
697 if (rxsr & RX_MISSED)
698 lp->stats.rx_missed_errors++;
699 else if (rxsr & RX_RUNT)
700 { /* Handled to avoid board lock-up. */
701 lp->stats.rx_length_errors++;
702 if (el_debug > 5)
703 printk(" runt.\n");
704 }
705 else if (rxsr & RX_GOOD)
706 {
707 /*
708 * Receive worked.
709 */
710 el_receive(dev);
711 }
712 else
713 {
714 /*
715 * Nothing? Something is broken!
716 */
717 if (el_debug > 2)
718 printk("%s: No packet seen, rxsr=%02x **resetting 3c501***\n",
719 dev->name, rxsr);
720 el_reset(dev);
721 }
722 if (el_debug > 3)
723 printk(".\n");
724 }
725
726 /*
727 * Move into receive mode
728 */
729
730 outb(AX_RX, AX_CMD);
731 outw(0x00, RX_BUF_CLR);
732 inb(RX_STATUS); /* Be certain that interrupts are cleared. */
733 inb(TX_STATUS);
734 spin_unlock(&lp->lock);
735 return;
736 }
737
738
739 /**
740 * el_receive:
741 * @dev: Device to pull the packets from
742 *
743 * We have a good packet. Well, not really "good", just mostly not broken.
744 * We must check everything to see if it is good. In paticular we occasionally
745 * get wild packet sizes from the card. If the packet seems sane we PIO it
746 * off the card and queue it for the protocol layers.
747 */
748
749 static void el_receive(struct net_device *dev)
750 {
751 struct net_local *lp = (struct net_local *)dev->priv;
752 int ioaddr = dev->base_addr;
753 int pkt_len;
754 struct sk_buff *skb;
755
756 pkt_len = inw(RX_LOW);
757
758 if (el_debug > 4)
759 printk(" el_receive %d.\n", pkt_len);
760
761 if ((pkt_len < 60) || (pkt_len > 1536))
762 {
763 if (el_debug)
764 printk("%s: bogus packet, length=%d\n", dev->name, pkt_len);
765 lp->stats.rx_over_errors++;
766 return;
767 }
768
769 /*
770 * Command mode so we can empty the buffer
771 */
772
773 outb(AX_SYS, AX_CMD);
774 skb = dev_alloc_skb(pkt_len+2);
775
776 /*
777 * Start of frame
778 */
779
780 outw(0x00, GP_LOW);
781 if (skb == NULL)
782 {
783 printk("%s: Memory squeeze, dropping packet.\n", dev->name);
784 lp->stats.rx_dropped++;
785 return;
786 }
787 else
788 {
789 skb_reserve(skb,2); /* Force 16 byte alignment */
790 skb->dev = dev;
791 /*
792 * The read increments through the bytes. The interrupt
793 * handler will fix the pointer when it returns to
794 * receive mode.
795 */
796 insb(DATAPORT, skb_put(skb,pkt_len), pkt_len);
797 skb->protocol=eth_type_trans(skb,dev);
798 netif_rx(skb);
799 lp->stats.rx_packets++;
800 lp->stats.rx_bytes+=pkt_len;
801 }
802 return;
803 }
804
805 /**
806 * el_reset: Reset a 3c501 card
807 * @dev: The 3c501 card about to get zapped
808 *
809 * Even resetting a 3c501 isnt simple. When you activate reset it loses all
810 * its configuration. You must hold the lock when doing this. The function
811 * cannot take the lock itself as it is callable from the irq handler.
812 */
813
814 static void el_reset(struct net_device *dev)
815 {
816 struct net_local *lp = (struct net_local *)dev->priv;
817 int ioaddr = dev->base_addr;
818
819 if (el_debug> 2)
820 printk("3c501 reset...");
821 outb(AX_RESET, AX_CMD); /* Reset the chip */
822 outb(AX_LOOP, AX_CMD); /* Aux control, irq and loopback enabled */
823 {
824 int i;
825 for (i = 0; i < 6; i++) /* Set the station address. */
826 outb(dev->dev_addr[i], ioaddr + i);
827 }
828
829 outw(0, RX_BUF_CLR); /* Set rx packet area to 0. */
830 outb(TX_NORM, TX_CMD); /* tx irq on done, collision */
831 outb(RX_NORM, RX_CMD); /* Set Rx commands. */
832 inb(RX_STATUS); /* Clear status. */
833 inb(TX_STATUS);
834 lp->txing = 0;
835 }
836
837 /**
838 * el1_close:
839 * @dev: 3c501 card to shut down
840 *
841 * Close a 3c501 card. The IFF_UP flag has been cleared by the user via
842 * the SIOCSIFFLAGS ioctl. We stop any further transmissions being queued,
843 * and then disable the interrupts. Finally we reset the chip. The effects
844 * of the rest will be cleaned up by #el1_open. Always returns 0 indicating
845 * a success.
846 */
847
848 static int el1_close(struct net_device *dev)
849 {
850 int ioaddr = dev->base_addr;
851
852 if (el_debug > 2)
853 printk("%s: Shutting down Ethernet card at %#x.\n", dev->name, ioaddr);
854
855 netif_stop_queue(dev);
856
857 /*
858 * Free and disable the IRQ.
859 */
860
861 free_irq(dev->irq, dev);
862 outb(AX_RESET, AX_CMD); /* Reset the chip */
863
864 return 0;
865 }
866
867 /**
868 * el1_get_stats:
869 * @dev: The card to get the statistics for
870 *
871 * In smarter devices this function is needed to pull statistics off the
872 * board itself. The 3c501 has no hardware statistics. We maintain them all
873 * so they are by definition always up to date.
874 *
875 * Returns the statistics for the card from the card private data
876 */
877
878 static struct net_device_stats *el1_get_stats(struct net_device *dev)
879 {
880 struct net_local *lp = (struct net_local *)dev->priv;
881 return &lp->stats;
882 }
883
884 /**
885 * set_multicast_list:
886 * @dev: The device to adjust
887 *
888 * Set or clear the multicast filter for this adaptor to use the best-effort
889 * filtering supported. The 3c501 supports only three modes of filtering.
890 * It always receives broadcasts and packets for itself. You can choose to
891 * optionally receive all packets, or all multicast packets on top of this.
892 */
893
894 static void set_multicast_list(struct net_device *dev)
895 {
896 int ioaddr = dev->base_addr;
897
898 if(dev->flags&IFF_PROMISC)
899 {
900 outb(RX_PROM, RX_CMD);
901 inb(RX_STATUS);
902 }
903 else if (dev->mc_list || dev->flags&IFF_ALLMULTI)
904 {
905 outb(RX_MULT, RX_CMD); /* Multicast or all multicast is the same */
906 inb(RX_STATUS); /* Clear status. */
907 }
908 else
909 {
910 outb(RX_NORM, RX_CMD);
911 inb(RX_STATUS);
912 }
913 }
914
915 #ifdef MODULE
916
917 static struct net_device dev_3c501 = {
918 init: el1_probe,
919 base_addr: 0x280,
920 irq: 5,
921 };
922
923 static int io=0x280;
924 static int irq=5;
925 MODULE_PARM(io, "i");
926 MODULE_PARM(irq, "i");
927
928 /**
929 * init_module:
930 *
931 * When the driver is loaded as a module this function is called. We fake up
932 * a device structure with the base I/O and interrupt set as if it was being
933 * called from Space.c. This minimises the extra code that would otherwise
934 * be required.
935 *
936 * Returns 0 for success or -EIO if a card is not found. Returning an error
937 * here also causes the module to be unloaded
938 */
939
940 int init_module(void)
941 {
942 dev_3c501.irq=irq;
943 dev_3c501.base_addr=io;
944 if (register_netdev(&dev_3c501) != 0)
945 return -EIO;
946 return 0;
947 }
948
949 /**
950 * cleanup_module:
951 *
952 * The module is being unloaded. We unhook our network device from the system
953 * and then free up the resources we took when the card was found.
954 */
955
956 void cleanup_module(void)
957 {
958 /*
959 * No need to check MOD_IN_USE, as sys_delete_module() checks.
960 */
961
962 unregister_netdev(&dev_3c501);
963
964 /*
965 * Free up the private structure, or leak memory :-)
966 */
967
968 kfree(dev_3c501.priv);
969 dev_3c501.priv = NULL; /* gets re-allocated by el1_probe1 */
970
971 /*
972 * If we don't do this, we can't re-insmod it later.
973 */
974 release_region(dev_3c501.base_addr, EL1_IO_EXTENT);
975 }
976
977 #endif /* MODULE */
978
979 /*
980 * Local variables:
981 * compile-command: "gcc -D__KERNEL__ -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer -m486 -c -o 3c501.o 3c501.c"
982 * kept-new-versions: 5
983 * End:
984 */
985
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.