1 /* 3c509.c: A 3c509 EtherLink3 ethernet driver for linux. */
3 Written 1993 by 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 driver is for the 3Com EtherLinkIII series.
12 The author may be reached as becker@super.org or
13 C/O Supercomputing Research Ctr., 17100 Science Dr., Bowie MD 20715
16 static char *version
= "3c509.c:pl13t 11/24/93 becker@super.org\n";
18 #include <linux/config.h>
19 #include <linux/kernel.h>
20 #include <linux/sched.h>
21 #include <linux/string.h>
22 #include <linux/interrupt.h>
23 #include <linux/ptrace.h>
24 #include <linux/errno.h>
26 #include <linux/malloc.h>
27 #include <linux/ioport.h>
28 #include <asm/bitops.h>
36 #ifndef HAVE_ALLOC_SKB
37 #define alloc_skb(size, priority) (struct sk_buff *) kmalloc(size,priority)
42 int el3_debug
= EL3_DEBUG
;
47 /* To minimize the size of the driver source I only define operating
48 constants if they are used several times. You'll need the manual
49 if you want to understand driver details. */
50 /* Offsets from base I/O address. */
53 #define EL3_STATUS 0x0e
55 #define EEPROM_READ 0x80
57 #define EL3WINDOW(win_num) outw(0x0800+(win_num), ioaddr + EL3_CMD)
59 /* Register window 1 offsets, the window used in normal operation. */
62 #define RX_STATUS 0x08
63 #define TX_STATUS 0x0B
64 #define TX_FREE 0x0C /* Remaining free bytes in Tx buffer. */
66 #define WN4_MEDIA 0x0A /* Window 4: Various transceiver/media bits. */
67 #define MEDIA_TP 0x00C0 /* Enable link beat and jabber for 10baseT. */
70 struct enet_statistics stats
;
73 static ushort
id_read_eeprom(int index
);
74 static ushort
read_eeprom(short ioaddr
, int index
);
75 static int el3_open(struct device
*dev
);
76 static int el3_start_xmit(struct sk_buff
*skb
, struct device
*dev
);
77 static void el3_interrupt(int reg_ptr
);
78 static void update_stats(int addr
, struct device
*dev
);
79 static struct enet_statistics
*el3_get_stats(struct device
*dev
);
80 static int el3_rx(struct device
*dev
);
81 static int el3_close(struct device
*dev
);
83 static void set_multicast_list(struct device
*dev
, int num_addrs
, void *addrs
);
88 int el3_probe(struct device
*dev
)
90 short lrs_state
= 0xff, i
;
91 ushort ioaddr
, irq
, if_port
;
92 short *phys_addr
= (short *)dev
->dev_addr
;
93 static int current_tag
= 0;
95 /* First check for a board on the EISA bus. */
97 for (ioaddr
= 0x1000; ioaddr
< 0x9000; ioaddr
+= 0x1000) {
98 if (inw(ioaddr
) != 0x6d50)
101 irq
= inw(ioaddr
+ 8) >> 12;
102 if_port
= inw(ioaddr
+ 6)>>14;
103 for (i
= 0; i
< 3; i
++)
104 phys_addr
[i
] = htons(read_eeprom(ioaddr
, i
));
106 /* Restore the "Manufacturer ID" to the EEPROM read register. */
107 /* The manual says to restore "Product ID" (reg. 3). !???! */
108 read_eeprom(ioaddr
, 7);
110 /* Was the EISA code an add-on hack? Nahhhhh... */
117 mca_adaptor_select_mode(1);
118 for (i
= 0; i
< 8; i
++)
119 if ((mca_adaptor_id(i
) | 1) == 0x627c) {
120 ioaddr
= mca_pos_base_addr(i
);
121 irq
= inw(ioaddr
+ 8) >> 12;
122 if_port
= inw(ioaddr
+ 6)>>14;
123 for (i
= 0; i
< 3; i
++)
124 phys_addr
[i
] = htons(read_eeprom(ioaddr
, i
));
126 mca_adaptor_select_mode(0);
129 mca_adaptor_select_mode(0);
134 /* Send the ID sequence to the ID_PORT. */
137 for(i
= 0; i
< 255; i
++) {
138 outb(lrs_state
, ID_PORT
);
140 lrs_state
= lrs_state
& 0x100 ? lrs_state
^ 0xcf : lrs_state
;
143 /* For the first probe, clear all board's tag registers. */
144 if (current_tag
== 0)
146 else /* Otherwise kill off already-found boards. */
149 if (id_read_eeprom(7) != 0x6d50) {
153 /* Read in EEPROM data, which does contention-select.
154 Only the lowest address board will stay "on-line".
155 3Com got the byte order backwards. */
156 for (i
= 0; i
< 3; i
++) {
157 phys_addr
[i
] = htons(id_read_eeprom(i
));
161 unsigned short iobase
= id_read_eeprom(8);
162 if_port
= iobase
>> 14;
163 ioaddr
= 0x200 + ((iobase
& 0x1f) << 4);
165 irq
= id_read_eeprom(9) >> 12;
167 /* The current Space.c structure makes it difficult to have more
168 than one adaptor initialized. Send me email if you have a need for
169 multiple adaptors, and we'll work out something. -becker@super.org */
170 if (dev
->base_addr
!= 0
171 && dev
->base_addr
!= (unsigned short)ioaddr
) {
175 /* Set the adaptor tag so that the next card can be found. */
176 outb(0xd0 + ++current_tag
, ID_PORT
);
178 /* Activate the adaptor at the EEPROM location. */
182 if (inw(ioaddr
) != 0x6d50)
186 dev
->base_addr
= ioaddr
;
188 dev
->if_port
= if_port
;
189 snarf_region(dev
->base_addr
, 16);
192 char *if_names
[] = {"10baseT", "AUI", "undefined", "BNC"};
193 printk("%s: 3c509 at %#3.3x tag %d, %s port, address ",
194 dev
->name
, dev
->base_addr
, current_tag
, if_names
[dev
->if_port
]);
197 /* Read in the station address. */
198 for (i
= 0; i
< 6; i
++)
199 printk(" %2.2x", dev
->dev_addr
[i
]);
200 printk(", IRQ %d.\n", dev
->irq
);
202 /* Make up a EL3-specific-data structure. */
203 dev
->priv
= kmalloc(sizeof(struct el3_private
), GFP_KERNEL
);
204 memset(dev
->priv
, 0, sizeof(struct el3_private
));
209 /* The EL3-specific entries in the device structure. */
210 dev
->open
= &el3_open
;
211 dev
->hard_start_xmit
= &el3_start_xmit
;
212 dev
->stop
= &el3_close
;
213 dev
->get_stats
= &el3_get_stats
;
214 #ifdef HAVE_MULTICAST
215 dev
->set_multicast_list
= &set_multicast_list
;
218 /* Fill in the generic fields of the device structure. */
219 for (i
= 0; i
< DEV_NUMBUFFS
; i
++)
220 dev
->buffs
[i
] = NULL
;
222 dev
->hard_header
= eth_header
;
223 dev
->add_arp
= eth_add_arp
;
224 dev
->queue_xmit
= dev_queue_xmit
;
225 dev
->rebuild_header
= eth_rebuild_header
;
226 dev
->type_trans
= eth_type_trans
;
228 dev
->type
= ARPHRD_ETHER
;
229 dev
->hard_header_len
= ETH_HLEN
;
230 dev
->mtu
= 1500; /* eth_mtu */
231 dev
->addr_len
= ETH_ALEN
;
232 for (i
= 0; i
< ETH_ALEN
; i
++) {
233 dev
->broadcast
[i
]=0xff;
236 /* New-style flags. */
237 dev
->flags
= IFF_BROADCAST
;
238 dev
->family
= AF_INET
;
242 dev
->pa_alen
= sizeof(unsigned long);
247 /* Read a word from the EEPROM using the regular EEPROM access register.
248 Assume that we are in register window zero.
250 static ushort
read_eeprom(short ioaddr
, int index
)
254 outw(EEPROM_READ
+ index
, ioaddr
+ 10);
255 /* Pause for at least 162 us. for the read to take place. */
256 for (timer
= 0; timer
< 162*4 + 400; timer
++)
258 return inw(ioaddr
+ 12);
261 /* Read a word from the EEPROM when in the ISA ID probe state. */
262 static ushort
id_read_eeprom(int index
)
264 int timer
, bit
, word
= 0;
266 /* Issue read command, and pause for at least 162 us. for it to complete.
267 Assume extra-fast 16Mhz bus. */
268 outb(EEPROM_READ
+ index
, ID_PORT
);
270 /* This should really be done by looking at one of the timer channels. */
271 for (timer
= 0; timer
< 162*4 + 400; timer
++)
274 for (bit
= 15; bit
>= 0; bit
--)
275 word
= (word
<< 1) + (inb(ID_PORT
) & 0x01);
278 printk(" 3c509 EEPROM word %d %#4.4x.\n", index
, word
);
286 el3_open(struct device
*dev
)
288 int ioaddr
= dev
->base_addr
;
291 if (request_irq(dev
->irq
, &el3_interrupt
)) {
297 printk("%s: Opening, IRQ %d status@%x %4.4x.\n", dev
->name
,
298 dev
->irq
, ioaddr
+ EL3_STATUS
, inw(ioaddr
+ EL3_STATUS
));
300 /* Activate board: this is probably unnecessary. */
301 outw(0x0001, ioaddr
+ 4);
303 irq2dev_map
[dev
->irq
] = dev
;
305 /* Set the IRQ line. */
306 outw((dev
->irq
<< 12) | 0x0f00, ioaddr
+ 8);
308 /* Set the station address in window 2 each time opened. */
311 for (i
= 0; i
< 6; i
++)
312 outb(dev
->dev_addr
[i
], ioaddr
+ i
);
314 if (dev
->if_port
== 3)
315 /* Start the thinnet transceiver. We should really wait 50ms...*/
316 outw(0x1000, ioaddr
+ EL3_CMD
);
317 else if (dev
->if_port
== 0) {
318 /* 10baseT interface, enabled link beat and jabber check. */
320 outw(inw(ioaddr
+ WN4_MEDIA
) | MEDIA_TP
, ioaddr
+ WN4_MEDIA
);
323 /* Switch to register set 1 for normal use. */
326 outw(0x8005, ioaddr
+ EL3_CMD
); /* Accept b-case and phys addr only. */
327 outw(0xA800, ioaddr
+ EL3_CMD
); /* Turn on statistics. */
328 outw(0x2000, ioaddr
+ EL3_CMD
); /* Enable the receiver. */
329 outw(0x4800, ioaddr
+ EL3_CMD
); /* Enable transmitter. */
330 outw(0x78ff, ioaddr
+ EL3_CMD
); /* Allow all status bits to be seen. */
334 outw(0x7098, ioaddr
+ EL3_CMD
); /* Set interrupt mask. */
337 printk("%s: Opened 3c509 IRQ %d status %4.4x.\n",
338 dev
->name
, dev
->irq
, inw(ioaddr
+ EL3_STATUS
));
340 return 0; /* Always succeed */
344 el3_start_xmit(struct sk_buff
*skb
, struct device
*dev
)
346 struct el3_private
*lp
= (struct el3_private
*)dev
->priv
;
347 int ioaddr
= dev
->base_addr
;
349 /* Transmitter timeout, serious problems. */
351 int tickssofar
= jiffies
- dev
->trans_start
;
354 printk("%s: transmit timed out, tx_status %2.2x status %4.4x.\n",
355 dev
->name
, inb(ioaddr
+ TX_STATUS
), inw(ioaddr
+ EL3_STATUS
));
356 dev
->trans_start
= jiffies
;
357 /* Issue TX_RESET and TX_START commands. */
358 outw(0x5800, ioaddr
+ EL3_CMD
); /* TX_RESET */
359 outw(0x4800, ioaddr
+ EL3_CMD
); /* TX_START */
368 /* Fill in the ethernet header. */
369 if (!skb
->arp
&& dev
->rebuild_header(skb
->data
, dev
)) {
380 printk("%s: el3_start_xmit(lenght = %d) called, status %4.4x.\n",
381 dev
->name
, skb
->len
, inw(ioaddr
+ EL3_STATUS
));
383 #ifndef final_version
384 { /* Error-checking code, delete for 1.00. */
385 ushort status
= inw(ioaddr
+ EL3_STATUS
);
386 if (status
& 0x0001 /* IRQ line active, missed one. */
387 && inw(ioaddr
+ EL3_STATUS
) & 1) { /* Make sure. */
388 printk("%s: Missed interrupt, status then %04x now %04x"
389 " Tx %2.2x Rx %4.4x.\n", dev
->name
, status
,
390 inw(ioaddr
+ EL3_STATUS
), inb(ioaddr
+ TX_STATUS
),
391 inw(ioaddr
+ RX_STATUS
));
392 outw(0x7800, ioaddr
+ EL3_CMD
); /* Fake interrupt trigger. */
393 outw(0x6899, ioaddr
+ EL3_CMD
); /* Ack IRQ */
394 outw(0x78ff, ioaddr
+ EL3_CMD
); /* Set all status bits visible. */
399 /* Avoid timer-based retransmission conflicts. */
400 if (set_bit(0, (void*)&dev
->tbusy
) != 0)
401 printk("%s: Transmitter access conflict.\n", dev
->name
);
403 /* Put out the doubleword header... */
404 outw(skb
->len
, ioaddr
+ TX_FIFO
);
405 outw(0x00, ioaddr
+ TX_FIFO
);
406 /* ... and the packet rounded to a doubleword. */
407 outsl(ioaddr
+ TX_FIFO
, skb
->data
, (skb
->len
+ 3) >> 2);
409 dev
->trans_start
= jiffies
;
410 if (inw(ioaddr
+ TX_FREE
) > 1536) {
413 /* Interrupt us when the FIFO has room for max-sized packet. */
414 outw(0x9000 + 1536, ioaddr
+ EL3_CMD
);
418 kfree_skb (skb
, FREE_WRITE
);
420 /* Clear the Tx status stack. */
425 while (--i
> 0 && (tx_status
= inb(ioaddr
+ TX_STATUS
)) > 0) {
427 printk(" Tx status %4.4x.\n", tx_status
);
428 if (tx_status
& 0x38) lp
->stats
.tx_aborted_errors
++;
429 if (tx_status
& 0x30) outw(0x5800, ioaddr
+ EL3_CMD
);
430 if (tx_status
& 0x3C) outw(0x4800, ioaddr
+ EL3_CMD
);
431 outb(0x00, ioaddr
+ TX_STATUS
); /* Pop the status stack. */
437 /* The EL3 interrupt handler. */
439 el3_interrupt(int reg_ptr
)
441 int irq
= -(((struct pt_regs
*)reg_ptr
)->orig_eax
+2);
442 struct device
*dev
= (struct device
*)(irq2dev_map
[irq
]);
447 printk ("el3_interrupt(): irq %d for unknown device.\n", irq
);
452 printk("%s: Re-entering the interrupt handler.\n", dev
->name
);
455 ioaddr
= dev
->base_addr
;
456 status
= inw(ioaddr
+ EL3_STATUS
);
459 printk("%s: interrupt, status %4.4x.\n", dev
->name
, status
);
461 while ((status
= inw(ioaddr
+ EL3_STATUS
)) & 0x01) {
468 printk(" TX room bit was handled.\n");
469 /* There's room in the FIFO for a full-sized packet. */
470 outw(0x6808, ioaddr
+ EL3_CMD
); /* Ack IRQ */
474 if (status
& 0x80) /* Statistics full. */
475 update_stats(ioaddr
, dev
);
478 printk("%s: Infinite loop in interrupt, status %4.4x.\n",
482 /* Clear the other interrupts we have handled. */
483 outw(0x6899, ioaddr
+ EL3_CMD
); /* Ack IRQ */
487 printk("%s: exiting interrupt, status %4.4x.\n", dev
->name
,
488 inw(ioaddr
+ EL3_STATUS
));
496 static struct enet_statistics
*
497 el3_get_stats(struct device
*dev
)
499 struct el3_private
*lp
= (struct el3_private
*)dev
->priv
;
502 update_stats(dev
->base_addr
, dev
);
507 /* Update statistics. We change to register window 6, so this should be run
508 single-threaded if the device is active. This is expected to be a rare
509 operation, and it's simpler for the rest of the driver to assume that
510 window 1 is always valid rather than use a special window-state variable.
512 static void update_stats(int ioaddr
, struct device
*dev
)
514 struct el3_private
*lp
= (struct el3_private
*)dev
->priv
;
517 printk(" Updating the statistics.\n");
518 /* Turn off statistics updates while reading. */
519 outw(0xB000, ioaddr
+ EL3_CMD
);
520 /* Switch to the stats window, and read everything. */
522 lp
->stats
.tx_carrier_errors
+= inb(ioaddr
+ 0);
523 lp
->stats
.tx_heartbeat_errors
+= inb(ioaddr
+ 1);
524 /* Multiple collisions. */ inb(ioaddr
+ 2);
525 lp
->stats
.collisions
+= inb(ioaddr
+ 3);
526 lp
->stats
.tx_window_errors
+= inb(ioaddr
+ 4);
527 lp
->stats
.rx_fifo_errors
+= inb(ioaddr
+ 5);
528 lp
->stats
.tx_packets
+= inb(ioaddr
+ 6);
529 lp
->stats
.rx_packets
+= inb(ioaddr
+ 7);
530 /* Tx deferrals */ inb(ioaddr
+ 8);
531 inw(ioaddr
+ 10); /* Total Rx and Tx octets. */
534 /* Back to window 1, and turn statistics back on. */
536 outw(0xA800, ioaddr
+ EL3_CMD
);
541 el3_rx(struct device
*dev
)
543 struct el3_private
*lp
= (struct el3_private
*)dev
->priv
;
544 int ioaddr
= dev
->base_addr
;
548 printk(" In rx_packet(), status %4.4x, rx_status %4.4x.\n",
549 inw(ioaddr
+EL3_STATUS
), inw(ioaddr
+RX_STATUS
));
550 while ((rx_status
= inw(ioaddr
+ RX_STATUS
)) > 0) {
551 if (rx_status
& 0x4000) { /* Error, update stats. */
552 short error
= rx_status
& 0x3C00;
553 lp
->stats
.rx_errors
++;
555 case 0x2000: lp
->stats
.rx_over_errors
++; break;
556 case 0x2C00: lp
->stats
.rx_length_errors
++; break;
557 case 0x3400: lp
->stats
.rx_crc_errors
++; break;
558 case 0x2400: lp
->stats
.rx_length_errors
++; break;
559 case 0x3000: lp
->stats
.rx_frame_errors
++; break;
560 case 0x0800: lp
->stats
.rx_frame_errors
++; break;
563 if ( (! (rx_status
& 0x4000))
564 || ! (rx_status
& 0x2000)) { /* Dribble bits are OK. */
565 short pkt_len
= rx_status
& 0x7ff;
566 int sksize
= sizeof(struct sk_buff
) + pkt_len
+ 3;
569 skb
= alloc_skb(sksize
, GFP_ATOMIC
);
571 printk(" Receiving packet size %d status %4.4x.\n",
574 skb
->mem_len
= sksize
;
579 /* 'skb->data' points to the start of sk_buff data area. */
580 insl(ioaddr
+RX_FIFO
, skb
->data
,
585 outw(0x4000, ioaddr
+ EL3_CMD
); /* Rx discard */
589 if (dev_rint((unsigned char *)skb
, pkt_len
,
592 printk(" dev_rint() happy, status %4.4x.\n",
593 inb(ioaddr
+ EL3_STATUS
));
594 outw(0x4000, ioaddr
+ EL3_CMD
); /* Rx discard */
595 while (inw(ioaddr
+ EL3_STATUS
) & 0x1000)
596 printk(" Waiting for 3c509 to discard packet, status %x.\n",
597 inw(ioaddr
+ EL3_STATUS
) );
599 printk(" discarded packet, status %4.4x.\n",
600 inb(ioaddr
+ EL3_STATUS
));
603 printk("%s: receive buffers full.\n", dev
->name
);
604 kfree_s(skb
, sksize
);
607 } else if (el3_debug
)
608 printk("%s: Couldn't allocate a sk_buff of size %d.\n",
611 lp
->stats
.rx_dropped
++;
612 outw(0x4000, ioaddr
+ EL3_CMD
); /* Rx discard */
613 while (inw(ioaddr
+ EL3_STATUS
) & 0x1000)
614 printk(" Waiting for 3c509 to discard packet, status %x.\n",
615 inw(ioaddr
+ EL3_STATUS
) );
619 printk(" Exiting rx_packet(), status %4.4x, rx_status %4.4x.\n",
620 inw(ioaddr
+EL3_STATUS
), inw(ioaddr
+8));
625 #ifdef HAVE_MULTICAST
626 /* Set or clear the multicast filter for this adaptor.
627 num_addrs == -1 Promiscuous mode, receive all packets
628 num_addrs == 0 Normal mode, clear multicast list
629 num_addrs > 0 Multicast mode, receive normal and MC packets, and do
630 best-effort filtering.
633 set_multicast_list(struct device
*dev
, int num_addrs
, void *addrs
)
635 short ioaddr
= dev
->base_addr
;
637 outw(0x8007, ioaddr
+ EL3_CMD
);
638 } else if (num_addrs
< 0) {
639 outw(0x8008, ioaddr
+ EL3_CMD
);
641 outw(0x8005, ioaddr
+ EL3_CMD
);
646 el3_close(struct device
*dev
)
648 int ioaddr
= dev
->base_addr
;
651 printk("%s: Shutting down ethercard.\n", dev
->name
);
656 /* Turn off statistics. We update lp->stats below. */
657 outw(0xB000, ioaddr
+ EL3_CMD
);
659 /* Disable the receiver and transmitter. */
660 outw(0x1800, ioaddr
+ EL3_CMD
);
661 outw(0x5000, ioaddr
+ EL3_CMD
);
663 if (dev
->if_port
== 3)
664 /* Turn off thinnet power. */
665 outw(0xb800, ioaddr
+ EL3_CMD
);
666 else if (dev
->if_port
== 0) {
667 /* Disable link beat and jabber, if_port may change ere next open(). */
669 outw(inw(ioaddr
+ WN4_MEDIA
) & ~MEDIA_TP
, ioaddr
+ WN4_MEDIA
);
673 /* Switching back to window 0 disables the IRQ. */
675 /* But we explicitly zero the IRQ line select anyway. */
676 outw(0x0f00, ioaddr
+ 8);
679 irq2dev_map
[dev
->irq
] = 0;
681 update_stats(ioaddr
, dev
);
687 * compile-command: "gcc -D__KERNEL__ -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O6 -m486 -c 3c509.c"
689 * kept-new-versions: 5