* add p cc
[mascara-docs.git] / i386 / linux / linux-0.99 / drivers / net / 3c501.c
blobd0141b0e5f286dd71e8933fd83f66c1f9af3002c
1 /* 3c501.c: A 3Com 3c501 ethernet driver for linux. */
2 /*
3 Copyright (C) 1992,1993 Donald Becker
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.
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.
14 The Author may be reached as becker@super.org or
15 C/O Supercomputing Research Ctr., 17100 Science Dr., Bowie MD 20715
16 I'll only accept bug fixes, not reports, for the 3c501 driver.
19 static char *version =
20 "3c501.c:v13.13 1993 Donald Becker (becker@super.org).\n";
23 Braindamage remaining:
24 The 3c501 board.
27 #include <linux/config.h>
28 #include <linux/kernel.h>
29 #include <linux/sched.h>
30 #include <linux/ptrace.h>
31 #include <linux/fcntl.h>
32 #include <linux/ioport.h>
33 #include <linux/interrupt.h>
34 #include <linux/malloc.h>
35 #include <linux/ioport.h>
36 #include <asm/bitops.h>
37 #include <asm/io.h>
38 #include <errno.h>
40 #include "dev.h"
41 #include "eth.h"
42 #include "skbuff.h"
43 #include "arp.h"
45 #ifndef HAVE_AUTOIRQ
46 /* From auto_irq.c, should be in a *.h file. */
47 extern void autoirq_setup(int waittime);
48 extern int autoirq_report(int waittime);
49 extern struct device *irq2dev_map[16];
50 #endif
52 #ifndef HAVE_ALLOC_SKB
53 #define alloc_skb(size, priority) (struct sk_buff *) kmalloc(size,priority)
54 #define kfree_skbmem(addr, size) kfree_s(addr,size);
55 #endif
58 /* Index to functions. */
59 int el1_probe(struct device *dev);
60 static int el_open(struct device *dev);
61 static int el_start_xmit(struct sk_buff *skb, struct device *dev);
62 static void el_interrupt(int reg_ptr);
63 static void el_receive(struct device *dev);
64 static void el_reset(struct device *dev);
65 static int el1_close(struct device *dev);
66 static struct enet_statistics *el1_get_stats(struct device *dev);
67 #ifdef HAVE_MULTICAST
68 static void set_multicast_list(struct device *dev, int num_addrs, void *addrs);
69 #endif
71 #define EL_NAME "EtherLink 3c501"
73 #ifndef EL_DEBUG
74 #define EL_DEBUG 2 /* use 0 for production, 1 for devel., >2 for debug */
75 #endif /* Anything above 5 is wordy death! */
76 static int el_debug = EL_DEBUG;
77 static int el_base;
78 static struct device *eldev; /* Only for consistency checking. */
80 /* We could easily have this struct kmalloc()ed per-board, but
81 who would want more than one 3c501?. */
82 static struct {
83 struct enet_statistics stats;
84 int tx_pkt_start; /* The length of the current Tx packet. */
85 int collisions; /* Tx collisions this packet */
86 } el_status; /* This should be stored per-board */
89 #define RX_STATUS (el_base + 0x06)
90 #define RX_CMD RX_STATUS
91 #define TX_STATUS (el_base + 0x07)
92 #define TX_CMD TX_STATUS
93 #define GP_LOW (el_base + 0x08)
94 #define GP_HIGH (el_base + 0x09)
95 #define RX_BUF_CLR (el_base + 0x0A)
96 #define RX_LOW (el_base + 0x0A)
97 #define RX_HIGH (el_base + 0x0B)
98 #define SAPROM (el_base + 0x0C)
99 #define AX_STATUS (el_base + 0x0E)
100 #define AX_CMD AX_STATUS
101 #define DATAPORT (el_base + 0x0F)
102 #define TX_RDY 0x08 /* In TX_STATUS */
104 #define EL1_DATAPTR 0x08
105 #define EL1_RXPTR 0x0A
106 #define EL1_SAPROM 0x0C
107 #define EL1_DATAPORT 0x0f
109 /* Writes to the ax command register. */
110 #define AX_OFF 0x00 /* Irq off, buffer access on */
111 #define AX_SYS 0x40 /* Load the buffer */
112 #define AX_XMIT 0x44 /* Transmit a packet */
113 #define AX_RX 0x48 /* Receive a packet */
114 #define AX_LOOP 0x0C /* Loopback mode */
115 #define AX_RESET 0x80
117 /* Normal receive mode written to RX_STATUS. We must intr on short packets
118 to avoid bogus rx lockups. */
119 #define RX_NORM 0xA8 /* 0x68 == all addrs, 0xA8 only to me. */
120 #define RX_PROM 0x68 /* Senior Prom, uhmm promiscuous mode. */
121 #define RX_MULT 0xE8 /* Accept multicast packets. */
122 #define TX_NORM 0x0A /* Interrupt on everything that might hang the chip */
124 /* TX_STATUS register. */
125 #define TX_COLLISION 0x02
126 #define TX_16COLLISIONS 0x04
127 #define TX_READY 0x08
129 #define RX_RUNT 0x08
130 #define RX_MISSED 0x01 /* Missed a packet due to 3c501 braindamage. */
131 #define RX_GOOD 0x30 /* Good packet 0x20, or simple overflow 0x10. */
135 el1_probe(struct device *dev)
137 int i;
138 int ioaddr;
139 unsigned char station_addr[6];
140 int autoirq = 0;
142 eldev = dev; /* Store for debugging. */
143 el_base = dev->base_addr;
145 if (el_base < 0x40) /* Invalid? Probe for it. */
146 el_base = 0x280;
148 ioaddr = el_base;
150 /* Read the station address PROM data from the special port. */
151 for (i = 0; i < 6; i++) {
152 outw(i, ioaddr + EL1_DATAPTR);
153 station_addr[i] = inb(ioaddr + EL1_SAPROM);
155 /* Check the first three octets of the S.A. for 3Com's code. */
156 if (station_addr[0] != 0x02 || station_addr[1] != 0x60
157 || station_addr[2] != 0x8c) {
158 return ENODEV;
161 #ifdef HAVE_PORTRESERVE
162 /* Grab the region so we can find the another board if autoIRQ fails. */
163 snarf_region(ioaddr, 16);
164 #endif
166 /* We auto-IRQ by shutting off the interrupt line and letting it float
167 high. */
168 if (dev->irq < 2) {
170 autoirq_setup(2);
172 inb(RX_STATUS); /* Clear pending interrupts. */
173 inb(TX_STATUS);
174 outb(AX_LOOP + 1, AX_CMD);
176 outb(0x00, AX_CMD);
178 autoirq = autoirq_report(1);
180 if (autoirq == 0) {
181 printk("%s: 3c501 probe failed to detect IRQ line.\n", dev->name);
182 return EAGAIN;
184 dev->irq = autoirq;
187 outb(AX_RESET+AX_LOOP, AX_CMD); /* Loopback mode. */
189 dev->base_addr = el_base;
190 memcpy(dev->dev_addr, station_addr, ETH_ALEN);
191 if (dev->mem_start & 0xf)
192 el_debug = dev->mem_start & 0x7;
194 printk("%s: 3c501 EtherLink at %#x, using %sIRQ %d, melting ethernet.\n",
195 dev->name, dev->base_addr, autoirq ? "auto":"assigned ", dev->irq);
197 if (el_debug)
198 printk("%s", version);
200 /* The EL1-specific entries in the device structure. */
201 dev->open = &el_open;
202 dev->hard_start_xmit = &el_start_xmit;
203 dev->stop = &el1_close;
204 dev->get_stats = &el1_get_stats;
205 #ifdef HAVE_MULTICAST
206 dev->set_multicast_list = &set_multicast_list;
207 #endif
209 /* Fill in the generic field of the device structure. */
210 for (i = 0; i < DEV_NUMBUFFS; i++)
211 dev->buffs[i] = NULL;
213 dev->hard_header = eth_header;
214 dev->add_arp = eth_add_arp;
215 dev->queue_xmit = dev_queue_xmit;
216 dev->rebuild_header = eth_rebuild_header;
217 dev->type_trans = eth_type_trans;
219 dev->type = ARPHRD_ETHER;
220 dev->hard_header_len = ETH_HLEN;
221 dev->mtu = 1500; /* eth_mtu */
222 dev->addr_len = ETH_ALEN;
223 for (i = 0; i < ETH_ALEN; i++) {
224 dev->broadcast[i]=0xff;
227 /* New-style flags. */
228 dev->flags = IFF_BROADCAST;
229 dev->family = AF_INET;
230 dev->pa_addr = 0;
231 dev->pa_brdaddr = 0;
232 dev->pa_mask = 0;
233 dev->pa_alen = sizeof(unsigned long);
235 return 0;
238 /* Open/initialize the board. */
239 static int
240 el_open(struct device *dev)
243 if (el_debug > 2)
244 printk("%s: Doing el_open()...", dev->name);
246 if (request_irq(dev->irq, &el_interrupt)) {
247 if (el_debug > 2)
248 printk("interrupt busy, exiting el_open().\n");
249 return -EAGAIN;
251 irq2dev_map[dev->irq] = dev;
253 el_reset(dev);
255 dev->start = 1;
257 outb(AX_RX, AX_CMD); /* Aux control, irq and receive enabled */
258 if (el_debug > 2)
259 printk("finished el_open().\n");
260 return (0);
263 static int
264 el_start_xmit(struct sk_buff *skb, struct device *dev)
267 if (dev->tbusy) {
268 if (jiffies - dev->trans_start < 20) {
269 if (el_debug > 2)
270 printk(" transmitter busy, deferred.\n");
271 return 1;
273 if (el_debug)
274 printk ("%s: transmit timed out, txsr %#2x axsr=%02x rxsr=%02x.\n",
275 dev->name, inb(TX_STATUS), inb(AX_STATUS), inb(RX_STATUS));
276 el_status.stats.tx_errors++;
277 #ifdef oldway
278 el_reset(dev);
279 #else
280 outb(TX_NORM, TX_CMD);
281 outb(RX_NORM, RX_CMD);
282 outb(AX_OFF, AX_CMD); /* Just trigger a false interrupt. */
283 #endif
284 outb(AX_RX, AX_CMD); /* Aux control, irq and receive enabled */
285 dev->tbusy = 0;
286 dev->trans_start = jiffies;
289 if (skb == NULL) {
290 dev_tint(dev);
291 return 0;
294 /* Fill in the ethernet header. */
295 if (!skb->arp && dev->rebuild_header(skb->data, dev)) {
296 skb->dev = dev;
297 arp_queue (skb);
298 return 0;
300 skb->arp=1;
302 if (skb->len <= 0)
303 return 0;
305 if (el_debug > 2)
306 printk("%s: el_start_xmit(%d)...", dev->name, skb->len);
308 /* Avoid timer-based retransmission conflicts. */
309 if (set_bit(0, (void*)&dev->tbusy) != 0)
310 printk("%s: Transmitter access conflict.\n", dev->name);
311 else {
312 int gp_start = 0x800 - (ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN);
313 unsigned char *buf = skb->data;
315 el_status.tx_pkt_start = gp_start;
316 el_status.collisions = 0;
318 outb(AX_SYS, AX_CMD);
319 inb(RX_STATUS);
320 inb(TX_STATUS);
321 outb(0x00, RX_BUF_CLR); /* Set rx packet area to 0. */
322 outw(gp_start, GP_LOW);
323 outsb(DATAPORT,buf,skb->len);
324 outw(gp_start, GP_LOW);
325 outb(AX_XMIT, AX_CMD); /* Trigger xmit. */
326 dev->trans_start = jiffies;
329 if (el_debug > 2)
330 printk(" queued xmit.\n");
331 if (skb->free)
332 kfree_skb (skb, FREE_WRITE);
333 return 0;
337 /* The typical workload of the driver:
338 Handle the ether interface interrupts. */
339 static void
340 el_interrupt(int reg_ptr)
342 int irq = -(((struct pt_regs *)reg_ptr)->orig_eax+2);
343 /*struct device *dev = (struct device *)(irq2dev_map[irq]);*/
344 struct device *dev = eldev;
345 int axsr; /* Aux. status reg. */
346 short ioaddr;
348 if (eldev->irq != irq) {
349 printk (EL_NAME ": irq %d for unknown device\n", irq);
350 return;
353 ioaddr = dev->base_addr;
355 axsr = inb(AX_STATUS);
357 if (el_debug > 3)
358 printk("%s: el_interrupt() aux=%#02x", dev->name, axsr);
359 if (dev->interrupt)
360 printk("%s: Reentering the interrupt driver!\n", dev->name);
361 dev->interrupt = 1;
363 if (dev->tbusy) {
364 int txsr = inb(TX_STATUS);
366 if (el_debug > 6)
367 printk(" txsr=%02x gp=%04x rp=%04x", txsr, inw(GP_LOW),
368 inw(RX_LOW));
370 if ((axsr & 0x80) && (txsr & TX_READY) == 0) {
371 printk("%s: Unusual interrupt during Tx, txsr=%02x axsr=%02x"
372 " gp=%03x rp=%03x.\n", dev->name, txsr, axsr,
373 inw(ioaddr + EL1_DATAPTR), inw(ioaddr + EL1_RXPTR));
374 dev->tbusy = 0;
375 mark_bh(INET_BH);
376 } else if (txsr & TX_16COLLISIONS) {
377 if (el_debug)
378 printk("%s: Transmit failed 16 times, ethernet jammed?\n",
379 dev->name);
380 outb(AX_SYS, AX_CMD);
381 el_status.stats.tx_aborted_errors++;
382 } else if (txsr & TX_COLLISION) { /* Retrigger xmit. */
383 if (el_debug > 6)
384 printk(" retransmitting after a collision.\n");
385 outb(AX_SYS, AX_CMD);
386 outw(el_status.tx_pkt_start, GP_LOW);
387 outb(AX_XMIT, AX_CMD);
388 el_status.stats.collisions++;
389 dev->interrupt = 0;
390 return;
391 } else {
392 el_status.stats.tx_packets++;
393 if (el_debug > 6)
394 printk(" Tx succeeded %s\n",
395 (txsr & TX_RDY) ? "." : "but tx is busy!");
396 dev->tbusy = 0;
397 mark_bh(INET_BH);
399 } else {
400 int rxsr = inb(RX_STATUS);
401 if (el_debug > 5)
402 printk(" rxsr=%02x txsr=%02x rp=%04x", rxsr, inb(TX_STATUS),
403 inw(RX_LOW));
405 /* Just reading rx_status fixes most errors. */
406 if (rxsr & RX_MISSED)
407 el_status.stats.rx_missed_errors++;
408 if (rxsr & RX_RUNT) { /* Handled to avoid board lock-up. */
409 el_status.stats.rx_length_errors++;
410 if (el_debug > 5) printk(" runt.\n");
411 } else if (rxsr & RX_GOOD) {
412 el_receive(eldev);
413 } else { /* Nothing? Something is broken! */
414 if (el_debug > 2)
415 printk("%s: No packet seen, rxsr=%02x **resetting 3c501***\n",
416 dev->name, rxsr);
417 el_reset(eldev);
419 if (el_debug > 3)
420 printk(".\n");
423 outb(AX_RX, AX_CMD);
424 outb(0x00, RX_BUF_CLR);
425 inb(RX_STATUS); /* Be certain that interrupts are cleared. */
426 inb(TX_STATUS);
427 dev->interrupt = 0;
428 return;
432 /* We have a good packet. Well, not really "good", just mostly not broken.
433 We must check everything to see if it is good. */
434 static void
435 el_receive(struct device *dev)
437 int sksize, pkt_len;
438 struct sk_buff *skb;
440 pkt_len = inw(RX_LOW);
442 if (el_debug > 4)
443 printk(" el_receive %d.\n", pkt_len);
445 if ((pkt_len < 60) || (pkt_len > 1536)) {
446 if (el_debug)
447 printk("%s: bogus packet, length=%d\n", dev->name, pkt_len);
448 el_status.stats.rx_over_errors++;
449 return;
451 outb(AX_SYS, AX_CMD);
453 sksize = sizeof(struct sk_buff) + pkt_len;
454 skb = alloc_skb(sksize, GFP_ATOMIC);
455 outw(0x00, GP_LOW);
456 if (skb == NULL) {
457 printk("%s: Memory squeeze, dropping packet.\n", dev->name);
458 el_status.stats.rx_dropped++;
459 return;
460 } else {
461 skb->mem_len = sksize;
462 skb->mem_addr = skb;
463 skb->len = pkt_len;
464 skb->dev = dev;
466 insb(DATAPORT, skb->data, pkt_len);
468 #ifdef HAVE_NETIF_RX
469 netif_rx(skb);
470 #else
471 skb->lock = 0;
472 if (dev_rint((unsigned char*)skb, pkt_len, IN_SKBUFF, dev) != 0) {
473 kfree_skbmem(skb, sksize);
474 lp->stats.rx_dropped++;
475 break;
477 #endif
478 el_status.stats.rx_packets++;
480 return;
483 static void
484 el_reset(struct device *dev)
486 if (el_debug> 2)
487 printk("3c501 reset...");
488 outb(AX_RESET, AX_CMD); /* Reset the chip */
489 outb(AX_LOOP, AX_CMD); /* Aux control, irq and loopback enabled */
491 int i;
492 for (i = 0; i < 6; i++) /* Set the station address. */
493 outb(dev->dev_addr[i], el_base + i);
496 outb(0, RX_BUF_CLR); /* Set rx packet area to 0. */
497 cli(); /* Avoid glitch on writes to CMD regs */
498 outb(TX_NORM, TX_CMD); /* tx irq on done, collision */
499 outb(RX_NORM, RX_CMD); /* Set Rx commands. */
500 inb(RX_STATUS); /* Clear status. */
501 inb(TX_STATUS);
502 dev->interrupt = 0;
503 dev->tbusy = 0;
504 sti();
507 static int
508 el1_close(struct device *dev)
510 int ioaddr = dev->base_addr;
512 if (el_debug > 2)
513 printk("%s: Shutting down ethercard at %#x.\n", dev->name, ioaddr);
515 dev->tbusy = 1;
516 dev->start = 0;
518 /* Free and disable the IRQ. */
519 free_irq(dev->irq);
520 outb(AX_RESET, AX_CMD); /* Reset the chip */
521 irq2dev_map[dev->irq] = 0;
523 return 0;
526 static struct enet_statistics *
527 el1_get_stats(struct device *dev)
529 return &el_status.stats;
532 #ifdef HAVE_MULTICAST
533 /* Set or clear the multicast filter for this adaptor.
534 num_addrs == -1 Promiscuous mode, receive all packets
535 num_addrs == 0 Normal mode, clear multicast list
536 num_addrs > 0 Multicast mode, receive normal and MC packets, and do
537 best-effort filtering.
539 static void
540 set_multicast_list(struct device *dev, int num_addrs, void *addrs)
542 if (num_addrs > 0) {
543 outb(RX_MULT, RX_CMD);
544 inb(RX_STATUS); /* Clear status. */
545 } else if (num_addrs < 0) {
546 outb(RX_PROM, RX_CMD);
547 inb(RX_STATUS);
548 } else {
549 outb(RX_NORM, RX_CMD);
550 inb(RX_STATUS);
553 #endif
556 * Local variables:
557 * compile-command: "gcc -D__KERNEL__ -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer -m486 -c -o 3c501.o 3c501.c"
558 * kept-new-versions: 5
559 * End: