1 /* $NetBSD: if_ed.c,v 1.59 2009/05/19 18:39:26 phx Exp $ */
4 * Device driver for National Semiconductor DS8390/WD83C690 based ethernet
7 * Copyright (c) 1994, 1995 Charles M. Hannum. All rights reserved.
9 * Copyright (C) 1993, David Greenman. This software may be used, modified,
10 * copied, distributed, and sold, in both source and binary form provided that
11 * the above copyright and these terms are retained. Under no circumstances is
12 * the author responsible for the proper functioning of this software, nor does
13 * the author assume any responsibility for damages incurred with its use.
15 * Currently supports the Hydra Systems ethernet card.
21 #include <sys/cdefs.h>
22 __KERNEL_RCSID(0, "$NetBSD: if_ed.c,v 1.59 2009/05/19 18:39:26 phx Exp $");
26 #include <sys/param.h>
27 #include <sys/systm.h>
28 #include <sys/errno.h>
29 #include <sys/ioctl.h>
31 #include <sys/socket.h>
32 #include <sys/syslog.h>
33 #include <sys/device.h>
36 #include <net/if_dl.h>
37 #include <net/if_types.h>
38 #include <net/if_ether.h>
41 #include <netinet/in.h>
42 #include <netinet/in_systm.h>
43 #include <netinet/in_var.h>
44 #include <netinet/ip.h>
45 #include <netinet/if_inarp.h>
50 #include <netns/ns_if.h>
55 #include <net/bpfdesc.h>
58 #include <machine/cpu.h>
60 #include <amiga/amiga/device.h>
61 #include <amiga/amiga/isr.h>
62 #include <amiga/dev/zbusvar.h>
63 #include <dev/ic/dp8390reg.h>
64 #include <amiga/dev/if_edreg.h>
66 #define HYDRA_MANID 2121
67 #define HYDRA_PRODID 1
69 #define ASDG_MANID 1023
70 #define ASDG_PRODID 254
73 * ed_softc: per line info and status
79 struct ethercom sc_ethercom
; /* ethernet common */
81 u_char
volatile *nic_addr
; /* NIC (DS8390) I/O address */
83 u_char cr_proto
; /* values always set in CR */
85 void * mem_start
; /* NIC memory start address */
86 void * mem_end
; /* NIC memory end address */
87 u_long mem_size
; /* total NIC memory size */
88 void * mem_ring
; /* start of RX ring-buffer (in NIC mem) */
90 u_char xmit_busy
; /* transmitter is busy */
91 u_char txb_cnt
; /* number of transmit buffers */
92 u_char txb_inuse
; /* number of TX buffers currently in-use*/
94 u_char txb_new
; /* pointer to where new buffer will be added */
95 u_char txb_next_tx
; /* pointer to next buffer ready to xmit */
96 u_short txb_len
[8]; /* buffered xmit buffer lengths */
97 u_char tx_page_start
; /* first page of TX buffer area */
98 u_char rec_page_start
; /* first page of RX ring-buffer */
99 u_char rec_page_stop
; /* last page of RX ring-buffer */
100 u_char next_packet
; /* pointer to next unread RX packet */
103 int ed_zbus_match(struct device
*, struct cfdata
*, void *);
104 void ed_zbus_attach(struct device
*, struct device
*, void *);
106 int ed_ioctl(struct ifnet
*, u_long
, void *);
107 void ed_start(struct ifnet
*);
108 void ed_watchdog(struct ifnet
*);
109 void ed_reset(struct ed_softc
*);
110 void ed_init(struct ed_softc
*);
111 void ed_stop(struct ed_softc
*);
112 void ed_getmcaf(struct ethercom
*, u_long
*);
113 u_short
ed_put(struct ed_softc
*, struct mbuf
*, void *);
115 #define inline /* XXX for debugging porpoises */
117 void ed_get_packet(struct ed_softc
*, void *, u_short
);
118 static inline void ed_rint(struct ed_softc
*);
119 static inline void ed_xmit(struct ed_softc
*);
120 static inline void *ed_ring_copy(struct ed_softc
*, void *, void *,
123 static inline void NIC_PUT(struct ed_softc
*, int, u_char
);
124 static inline u_char
NIC_GET(struct ed_softc
*, int);
125 static inline void word_copy(void *, void *, int);
126 static inline void word_zero(void *, int);
127 struct mbuf
*ed_ring_to_mbuf(struct ed_softc
*, void *, struct mbuf
*,
130 CFATTACH_DECL(ed_zbus
, sizeof(struct ed_softc
),
131 ed_zbus_match
, ed_zbus_attach
, NULL
, NULL
);
134 NIC_PUT(struct ed_softc
*sc
, int off
, u_char val
)
136 sc
->nic_addr
[off
* 2] = val
;
139 * This was being used to *slow* access to the bus. I don't
140 * believe it is needed but I'll leave it around incase probelms
148 NIC_GET(struct ed_softc
*sc
, int off
)
152 val
= sc
->nic_addr
[off
* 2];
155 * This was being used to *slow* access to the bus. I don't
156 * believe it is needed but I'll leave it around incase probelms
165 * Memory copy, copies word at time.
168 word_copy(void *a
, void *b
, int len
)
170 u_short
*x
= (u_short
*)a
,
179 * zero memory, one word at time.
182 word_zero(void *a
, int len
)
184 u_short
*x
= (u_short
*)a
;
192 ed_zbus_match(struct device
*parent
, struct cfdata
*cfp
, void *aux
)
194 struct zbus_args
*zap
= aux
;
196 if (zap
->manid
== HYDRA_MANID
&& zap
->prodid
== HYDRA_PRODID
)
198 else if (zap
->manid
== ASDG_MANID
&& zap
->prodid
== ASDG_PRODID
)
204 ed_zbus_attach(struct device
*parent
, struct device
*self
, void *aux
)
206 struct ed_softc
*sc
= (void *)self
;
207 struct zbus_args
*zap
= aux
;
208 struct cfdata
*cf
= device_cfdata(&sc
->sc_dev
);
209 struct ifnet
*ifp
= &sc
->sc_ethercom
.ec_if
;
210 volatile u_char
*prom
;
212 u_int8_t myaddr
[ETHER_ADDR_LEN
];
214 if (zap
->manid
== HYDRA_MANID
) {
215 sc
->mem_start
= zap
->va
;
216 sc
->mem_size
= 16384;
217 sc
->nic_addr
= (u_char
*)sc
->mem_start
+ HYDRA_NIC_BASE
;
218 prom
= (u_char
*)sc
->mem_start
+ HYDRA_ADDRPROM
;
220 sc
->mem_start
= (u_char
*)zap
->va
+ 0x8000;
221 sc
->mem_size
= 16384;
222 sc
->nic_addr
= (volatile u_char
*)zap
->va
+ ASDG_NIC_BASE
;
223 prom
= (volatile u_char
*)sc
->nic_addr
+ ASDG_ADDRPROM
;
225 sc
->cr_proto
= ED_CR_RD2
;
226 sc
->tx_page_start
= 0;
228 sc
->mem_end
= (u_char
*)sc
->mem_start
+ sc
->mem_size
;
231 * Use one xmit buffer if < 16k, two buffers otherwise (if not told
234 if ((sc
->mem_size
< 16384) || zap
->manid
== ASDG_MANID
235 || (cf
->cf_flags
& ED_FLAGS_NO_MULTI_BUFFERING
))
240 sc
->rec_page_start
= sc
->tx_page_start
+ sc
->txb_cnt
* ED_TXBUF_SIZE
;
241 sc
->rec_page_stop
= sc
->tx_page_start
+ (sc
->mem_size
>> ED_PAGE_SHIFT
);
244 (u_char
*)sc
->mem_start
+
245 ((sc
->txb_cnt
* ED_TXBUF_SIZE
) << ED_PAGE_SHIFT
);
248 * Interrupts must be inactive when reading the prom, as the interrupt
249 * line is shared with one of its address lines.
252 NIC_PUT(sc
, ED_P0_IMR
, 0x00); /* disable ints */
253 NIC_PUT(sc
, ED_P0_ISR
, 0xff); /* clear ints */
256 * read the ethernet address from the board
258 for (i
= 0; i
< ETHER_ADDR_LEN
; i
++)
259 myaddr
[i
] = *(prom
+ 2 * i
);
261 /* Set interface to stopped condition (reset). */
264 /* Initialize ifnet structure. */
265 memcpy(ifp
->if_xname
, sc
->sc_dev
.dv_xname
, IFNAMSIZ
);
267 ifp
->if_start
= ed_start
;
268 ifp
->if_ioctl
= ed_ioctl
;
269 ifp
->if_watchdog
= ed_watchdog
;
271 IFF_BROADCAST
| IFF_SIMPLEX
| IFF_NOTRAILERS
| IFF_MULTICAST
;
273 /* Attach the interface. */
275 ether_ifattach(ifp
, myaddr
);
277 /* Print additional info when attached. */
278 printf(": address %s\n", ether_sprintf(myaddr
));
280 sc
->sc_isr
.isr_intr
= edintr
;
281 sc
->sc_isr
.isr_arg
= sc
;
282 sc
->sc_isr
.isr_ipl
= 2;
283 add_isr(&sc
->sc_isr
);
290 ed_reset(struct ed_softc
*sc
)
298 log(LOG_ERR
, "%s: reset\n", sc
->sc_dev
.dv_xname
);
302 * Take interface offline.
305 ed_stop(struct ed_softc
*sc
)
309 /* Stop everything on the interface, and select page 0 registers. */
310 NIC_PUT(sc
, ED_P0_CR
, sc
->cr_proto
| ED_CR_PAGE_0
| ED_CR_STP
);
313 * Wait for interface to enter stopped state, but limit # of checks to
314 * 'n' (about 5ms). It shouldn't even take 5us on modern DS8390's, but
315 * just in case it's an old one.
317 while (((NIC_GET(sc
, ED_P0_ISR
) & ED_ISR_RST
) == 0) && --n
);
321 * Device timeout/watchdog routine. Entered if the device neglects to generate
322 * an interrupt after a transmit has been started on it.
325 ed_watchdog(struct ifnet
*ifp
)
327 struct ed_softc
*sc
= ifp
->if_softc
;
329 log(LOG_ERR
, "%s: device timeout\n", sc
->sc_dev
.dv_xname
);
339 ed_init(struct ed_softc
*sc
)
341 struct ifnet
*ifp
= &sc
->sc_ethercom
.ec_if
;
346 * Initialize the NIC in the exact order outlined in the NS manual.
347 * This init procedure is "mandatory"...don't change what or when
352 /* Reset transmitter flags. */
360 /* Set interface for page 0, remote DMA complete, stopped. */
361 NIC_PUT(sc
, ED_P0_CR
, sc
->cr_proto
| ED_CR_PAGE_0
| ED_CR_STP
);
364 * Set FIFO threshold to 8, No auto-init Remote DMA, byte
365 * order=68k, word-wide DMA xfers,
366 * XXX changed to use 2 word threshhold
368 NIC_PUT(sc
, ED_P0_DCR
,
369 ED_DCR_FT0
| ED_DCR_WTS
| ED_DCR_LS
| ED_DCR_BOS
);
371 /* Clear remote byte count registers. */
372 NIC_PUT(sc
, ED_P0_RBCR0
, 0);
373 NIC_PUT(sc
, ED_P0_RBCR1
, 0);
375 /* Tell RCR to do nothing for now. */
376 NIC_PUT(sc
, ED_P0_RCR
, ED_RCR_MON
);
378 /* Place NIC in internal loopback mode. */
379 NIC_PUT(sc
, ED_P0_TCR
, ED_TCR_LB0
);
381 /* Initialize receive buffer ring. */
382 NIC_PUT(sc
, ED_P0_BNRY
, sc
->rec_page_start
);
383 NIC_PUT(sc
, ED_P0_PSTART
, sc
->rec_page_start
);
384 NIC_PUT(sc
, ED_P0_PSTOP
, sc
->rec_page_stop
);
387 * Clear all interrupts. A '1' in each bit position clears the
388 * corresponding flag.
390 NIC_PUT(sc
, ED_P0_ISR
, 0xff);
393 * Enable the following interrupts: receive/transmit complete,
394 * receive/transmit error, and Receiver OverWrite.
396 * Counter overflow and Remote DMA complete are *not* enabled.
398 NIC_PUT(sc
, ED_P0_IMR
,
399 ED_IMR_PRXE
| ED_IMR_PTXE
| ED_IMR_RXEE
| ED_IMR_TXEE
|
402 /* Program command register for page 1. */
403 NIC_PUT(sc
, ED_P0_CR
, sc
->cr_proto
| ED_CR_PAGE_1
| ED_CR_STP
);
405 /* Copy out our station address. */
406 for (i
= 0; i
< ETHER_ADDR_LEN
; ++i
)
407 NIC_PUT(sc
, ED_P1_PAR0
+ i
, CLLADDR(ifp
->if_sadl
)[i
]);
409 /* Set multicast filter on chip. */
410 ed_getmcaf(&sc
->sc_ethercom
, mcaf
);
411 for (i
= 0; i
< 8; i
++)
412 NIC_PUT(sc
, ED_P1_MAR0
+ i
, ((u_char
*)mcaf
)[i
]);
415 * Set current page pointer to one page after the boundary pointer, as
416 * recommended in the National manual.
418 sc
->next_packet
= sc
->rec_page_start
+ 1;
419 NIC_PUT(sc
, ED_P1_CURR
, sc
->next_packet
);
421 /* Program command register for page 0. */
422 NIC_PUT(sc
, ED_P1_CR
, sc
->cr_proto
| ED_CR_PAGE_0
| ED_CR_STP
);
424 i
= ED_RCR_AB
| ED_RCR_AM
;
425 if (ifp
->if_flags
& IFF_PROMISC
) {
427 * Set promiscuous mode. Multicast filter was set earlier so
428 * that we should receive all multicast packets.
430 i
|= ED_RCR_PRO
| ED_RCR_AR
| ED_RCR_SEP
;
432 NIC_PUT(sc
, ED_P0_RCR
, i
);
434 /* Take interface out of loopback. */
435 NIC_PUT(sc
, ED_P0_TCR
, 0);
437 /* Fire up the interface. */
438 NIC_PUT(sc
, ED_P0_CR
, sc
->cr_proto
| ED_CR_PAGE_0
| ED_CR_STA
);
440 /* Set 'running' flag, and clear output active flag. */
441 ifp
->if_flags
|= IFF_RUNNING
;
442 ifp
->if_flags
&= ~IFF_OACTIVE
;
444 /* ...and attempt to start output. */
451 * This routine actually starts the transmission on the interface.
454 ed_xmit(struct ed_softc
*sc
)
456 struct ifnet
*ifp
= &sc
->sc_ethercom
.ec_if
;
459 len
= sc
->txb_len
[sc
->txb_next_tx
];
461 /* Set NIC for page 0 register access. */
462 NIC_PUT(sc
, ED_P0_CR
, sc
->cr_proto
| ED_CR_PAGE_0
| ED_CR_STA
);
464 /* Set TX buffer start page. */
465 NIC_PUT(sc
, ED_P0_TPSR
, sc
->tx_page_start
+
466 sc
->txb_next_tx
* ED_TXBUF_SIZE
);
469 NIC_PUT(sc
, ED_P0_TBCR0
, len
);
470 NIC_PUT(sc
, ED_P0_TBCR1
, len
>> 8);
472 /* Set page 0, remote DMA complete, transmit packet, and *start*. */
473 NIC_PUT(sc
, ED_P0_CR
, sc
->cr_proto
| ED_CR_PAGE_0
| ED_CR_TXP
| ED_CR_STA
);
476 /* Point to next transmit buffer slot and wrap if necessary. */
478 if (sc
->txb_next_tx
== sc
->txb_cnt
)
481 /* Set a timer just in case we never hear from the board again. */
486 * Start output on interface.
487 * We make two assumptions here:
488 * 1) that the current priority is set to splnet _before_ this code
489 * is called *and* is returned to the appropriate priority after
491 * 2) that the IFF_OACTIVE flag is checked before this code is called
492 * (i.e. that the output part of the interface is idle)
495 ed_start(struct ifnet
*ifp
)
497 struct ed_softc
*sc
= ifp
->if_softc
;
504 * First, see if there are buffered packets and an idle transmitter -
505 * should never happen at this point.
507 if (sc
->txb_inuse
&& (sc
->xmit_busy
== 0)) {
508 printf("%s: packets buffered, but transmitter idle\n",
509 sc
->sc_dev
.dv_xname
);
513 /* See if there is room to put another packet in the buffer. */
514 if (sc
->txb_inuse
== sc
->txb_cnt
) {
515 /* No room. Indicate this to the outside world and exit. */
516 ifp
->if_flags
|= IFF_OACTIVE
;
520 IF_DEQUEUE(&ifp
->if_snd
, m
);
523 * We are using the !OACTIVE flag to indicate to the outside
524 * world that we can accept an additional packet rather than
525 * that the transmitter is _actually_ active. Indeed, the
526 * transmitter may be active, but if we haven't filled all the
527 * buffers with data then we still want to accept more.
529 ifp
->if_flags
&= ~IFF_OACTIVE
;
533 /* Copy the mbuf chain into the transmit buffer. */
536 /* txb_new points to next open buffer slot. */
537 buffer
= (char*)sc
->mem_start
+
538 ((sc
->txb_new
* ED_TXBUF_SIZE
) << ED_PAGE_SHIFT
);
540 len
= ed_put(sc
, m
, buffer
);
542 sc
->txb_len
[sc
->txb_new
] = len
;
545 /* Point to next buffer slot and wrap if necessary. */
546 if (++sc
->txb_new
== sc
->txb_cnt
)
549 if (sc
->xmit_busy
== 0)
553 /* Tap off here if there is a BPF listener. */
555 bpf_mtap(ifp
->if_bpf
, m0
);
560 /* Loop back to the top to possibly buffer more packets. */
565 * Ethernet interface receiver interrupt.
568 ed_rint(struct ed_softc
*sc
)
574 u_char boundary
, current
;
575 struct ed_ring packet_hdr
;
577 ifp
= &sc
->sc_ethercom
.ec_if
;
579 /* Set NIC to page 1 registers to get 'current' pointer. */
580 NIC_PUT(sc
, ED_P0_CR
, sc
->cr_proto
| ED_CR_PAGE_1
| ED_CR_STA
);
583 * 'sc->next_packet' is the logical beginning of the ring-buffer - i.e.
584 * it points to where new data has been buffered. The 'CURR' (current)
585 * register points to the logical end of the ring-buffer - i.e. it
586 * points to where additional new data will be added. We loop here
587 * until the logical beginning equals the logical end (or in other
588 * words, until the ring-buffer is empty).
590 current
= NIC_GET(sc
, ED_P1_CURR
);
591 if (sc
->next_packet
== current
)
594 /* Set NIC to page 0 registers to update boundary register. */
595 NIC_PUT(sc
, ED_P1_CR
, sc
->cr_proto
| ED_CR_PAGE_0
| ED_CR_STA
);
598 /* Get pointer to this buffer's header structure. */
599 packet_ptr
= (char*)sc
->mem_ring
+
600 ((sc
->next_packet
- sc
->rec_page_start
) << ED_PAGE_SHIFT
);
603 * The byte count includes a 4 byte header that was added by
606 packet_hdr
= *(struct ed_ring
*)packet_ptr
;
607 packet_hdr
.count
= ((packet_hdr
.count
>> 8) & 0xff)
608 | ((packet_hdr
.count
& 0xff) << 8);
609 len
= packet_hdr
.count
;
611 * Try do deal with old, buggy chips that sometimes duplicate
612 * the low byte of the length into the high byte. We do this
613 * by simply ignoring the high byte of the length and always
616 * NOTE: sc->next_packet is pointing at the current packet.
618 if (packet_hdr
.next_packet
>= sc
->next_packet
)
619 nlen
= (packet_hdr
.next_packet
- sc
->next_packet
);
621 nlen
= ((packet_hdr
.next_packet
- sc
->rec_page_start
) +
622 (sc
->rec_page_stop
- sc
->next_packet
));
624 if ((len
& ED_PAGE_MASK
) + sizeof(packet_hdr
) > ED_PAGE_SIZE
)
626 len
= (len
& ED_PAGE_MASK
) | (nlen
<< ED_PAGE_SHIFT
);
628 if (len
!= packet_hdr
.count
) {
629 printf("%s: length does not match next packet pointer\n",
630 sc
->sc_dev
.dv_xname
);
631 printf("%s: len %04x nlen %04x start %02x first %02x curr %02x next %02x stop %02x\n",
632 sc
->sc_dev
.dv_xname
, packet_hdr
.count
, len
,
633 sc
->rec_page_start
, sc
->next_packet
, current
,
634 packet_hdr
.next_packet
, sc
->rec_page_stop
);
639 * Be fairly liberal about what we allow as a "reasonable"
640 * length so that a [crufty] packet will make it to BPF (and
641 * can thus be analyzed). Note that all that is really
642 * important is that we have a length that will fit into one
643 * mbuf cluster or less; the upper layer protocols can then
644 * figure out the length from their own length field(s).
646 * MCLBYTES may be less than a valid packet len. Thus
647 * we use a constant that is large enough.
650 packet_hdr
.next_packet
>= sc
->rec_page_start
&&
651 packet_hdr
.next_packet
< sc
->rec_page_stop
) {
653 ed_get_packet(sc
, (char*)packet_ptr
+
654 sizeof(struct ed_ring
),
655 len
- sizeof(struct ed_ring
));
658 /* Really BAD. The ring pointers are corrupted. */
659 #ifdef DEBUG_AMIGA_IF_ED
661 "%s: NIC memory corrupt - invalid packet length %d\n",
662 sc
->sc_dev
.dv_xname
, len
);
669 /* Update next packet pointer. */
670 sc
->next_packet
= packet_hdr
.next_packet
;
673 * Update NIC boundary pointer - being careful to keep it one
674 * buffer behind (as recommended by NS databook).
676 boundary
= sc
->next_packet
- 1;
677 if (boundary
< sc
->rec_page_start
)
678 boundary
= sc
->rec_page_stop
- 1;
679 NIC_PUT(sc
, ED_P0_BNRY
, boundary
);
680 } while (sc
->next_packet
!= current
);
685 /* Ethernet interface interrupt processor. */
689 struct ed_softc
*sc
= arg
;
690 struct ifnet
*ifp
= &sc
->sc_ethercom
.ec_if
;
693 /* Set NIC to page 0 registers. */
694 NIC_PUT(sc
, ED_P0_CR
, sc
->cr_proto
| ED_CR_PAGE_0
| ED_CR_STA
);
696 isr
= NIC_GET(sc
, ED_P0_ISR
);
700 /* Loop until there are no more new interrupts. */
703 * Reset all the bits that we are 'acknowledging' by writing a
704 * '1' to each bit position that was set.
705 * (Writing a '1' *clears* the bit.)
707 NIC_PUT(sc
, ED_P0_ISR
, isr
);
710 * Handle transmitter interrupts. Handle these first because
711 * the receiver will reset the board under some conditions.
713 if (isr
& (ED_ISR_PTX
| ED_ISR_TXE
)) {
714 u_char collisions
= NIC_GET(sc
, ED_P0_NCR
) & 0x0f;
717 * Check for transmit error. If a TX completed with an
718 * error, we end up throwing the packet away. Really
719 * the only error that is possible is excessive
720 * collisions, and in this case it is best to allow the
721 * automatic mechanisms of TCP to backoff the flow. Of
722 * course, with UDP we're screwed, but this is expected
723 * when a network is heavily loaded.
725 (void) NIC_GET(sc
, ED_P0_TSR
);
726 if (isr
& ED_ISR_TXE
) {
728 * Excessive collisions (16).
730 if ((NIC_GET(sc
, ED_P0_TSR
) & ED_TSR_ABT
)
731 && (collisions
== 0)) {
733 * When collisions total 16, the P0_NCR
734 * will indicate 0, and the TSR_ABT is
740 /* Update output errors counter. */
744 * Update total number of successfully
745 * transmitted packets.
750 /* Reset TX busy and output active flags. */
752 ifp
->if_flags
&= ~IFF_OACTIVE
;
754 /* Clear watchdog timer. */
758 * Add in total number of collisions on last
761 ifp
->if_collisions
+= collisions
;
764 * Decrement buffer in-use count if not zero (can only
765 * be zero if a transmitter interrupt occurred while not
766 * actually transmitting).
767 * If data is ready to transmit, start it transmitting,
768 * otherwise defer until after handling receiver.
770 if (sc
->txb_inuse
&& --sc
->txb_inuse
)
774 /* Handle receiver interrupts. */
775 if (isr
& (ED_ISR_PRX
| ED_ISR_RXE
| ED_ISR_OVW
)) {
777 * Overwrite warning. In order to make sure that a
778 * lockup of the local DMA hasn't occurred, we reset
779 * and re-init the NIC. The NSC manual suggests only a
780 * partial reset/re-init is necessary - but some chips
781 * seem to want more. The DMA lockup has been seen
782 * only with early rev chips - Methinks this bug was
783 * fixed in later revs. -DG
785 if (isr
& ED_ISR_OVW
) {
789 "%s: warning - receiver ring buffer overrun\n",
790 sc
->sc_dev
.dv_xname
);
792 /* Stop/reset/re-init NIC. */
796 * Receiver Error. One or more of: CRC error,
797 * frame alignment error FIFO overrun, or
800 if (isr
& ED_ISR_RXE
) {
803 printf("%s: receive error %x\n",
805 NIC_GET(sc
, ED_P0_RSR
));
810 * Go get the packet(s).
811 * XXX - Doing this on an error is dubious
812 * because there shouldn't be any data to get
813 * (we've configured the interface to not
814 * accept packets with errors).
821 * If it looks like the transmitter can take more data, attempt
822 * to start output on the interface. This is done after
823 * handling the receiver to give the receiver priority.
825 if ((ifp
->if_flags
& IFF_OACTIVE
) == 0)
829 * Return NIC CR to standard state: page 0, remote DMA
830 * complete, start (toggling the TXP bit off, even if was just
831 * set in the transmit routine, is *okay* - it is 'edge'
832 * triggered from low to high).
834 NIC_PUT(sc
, ED_P0_CR
, sc
->cr_proto
| ED_CR_PAGE_0
| ED_CR_STA
);
837 * If the Network Talley Counters overflow, read them to reset
838 * them. It appears that old 8390's won't clear the ISR flag
839 * otherwise - resulting in an infinite loop.
841 if (isr
& ED_ISR_CNT
) {
842 (void) NIC_GET(sc
, ED_P0_CNTR0
);
843 (void) NIC_GET(sc
, ED_P0_CNTR1
);
844 (void) NIC_GET(sc
, ED_P0_CNTR2
);
847 isr
= NIC_GET(sc
, ED_P0_ISR
);
854 * Process an ioctl request. This code needs some work - it looks pretty ugly.
857 ed_ioctl(register struct ifnet
*ifp
, u_long cmd
, void *data
)
859 struct ed_softc
*sc
= ifp
->if_softc
;
860 register struct ifaddr
*ifa
= (struct ifaddr
*)data
;
868 ifp
->if_flags
|= IFF_UP
;
870 switch (ifa
->ifa_addr
->sa_family
) {
874 arp_ifinit(ifp
, ifa
);
878 /* XXX - This code is probably wrong. */
881 register struct ns_addr
*ina
= &IA_SNS(ifa
)->sns_addr
;
883 if (ns_nullhost(*ina
))
885 *(union ns_host
*)LLADDR(ifp
->if_sadl
);
887 bcopy(ina
->x_host
.c_host
,
888 LLADDR(ifp
->if_sadl
), ETHER_ADDR_LEN
);
889 /* Set new address. */
901 if ((error
= ifioctl_common(ifp
, cmd
, data
)) != 0)
903 /* XXX if ed_init() would ed_stop(, 0), first, perhaps we
904 * can re-use the code in ether_ioctl().
906 if ((ifp
->if_flags
& IFF_UP
) == 0 &&
907 (ifp
->if_flags
& IFF_RUNNING
) != 0) {
909 * If interface is marked down and it is running, then
913 ifp
->if_flags
&= ~IFF_RUNNING
;
914 } else if ((ifp
->if_flags
& IFF_UP
) != 0 &&
915 (ifp
->if_flags
& IFF_RUNNING
) == 0) {
917 * If interface is marked up and it is stopped, then
923 * Reset the interface to pick up changes in any other
924 * flags that affect hardware registers.
933 /* Update our multicast list. */
934 if ((error
= ether_ioctl(ifp
, cmd
, data
)) == ENETRESET
) {
936 * Multicast list has changed; set the hardware filter
939 if (ifp
->if_flags
& IFF_RUNNING
) {
940 ed_stop(sc
); /* XXX for ds_setmcaf? */
948 error
= ether_ioctl(ifp
, cmd
, data
);
957 * Retrieve packet from shared memory and send to the next level up via
958 * ether_input(). If there is a BPF listener, give a copy to BPF, too.
961 ed_get_packet(struct ed_softc
*sc
, void *buf
, u_short len
)
966 ifp
= &sc
->sc_ethercom
.ec_if
;
968 /* round length to word boundry */
969 len
= (len
+ 1) & ~1;
971 /* Allocate a header mbuf. */
972 MGETHDR(m
, M_DONTWAIT
, MT_DATA
);
975 m
->m_pkthdr
.rcvif
= ifp
;
976 m
->m_pkthdr
.len
= len
;
979 /* The following silliness is to make NFS happy. */
980 #define EROUND ((sizeof(struct ether_header) + 3) & ~3)
981 #define EOFF (EROUND - sizeof(struct ether_header))
984 * The following assumes there is room for the ether header in the
989 word_copy(buf
, mtod(m
, void *), sizeof(struct ether_header
));
990 buf
= (char*)buf
+ sizeof(struct ether_header
);
991 m
->m_len
+= sizeof(struct ether_header
);
992 len
-= sizeof(struct ether_header
);
994 /* Pull packet off interface. */
995 if (ed_ring_to_mbuf(sc
, buf
, m
, len
) == 0) {
1002 * Check if there's a BPF listener on this interface. If so, hand off
1003 * the raw packet to bpf.
1006 bpf_mtap(ifp
->if_bpf
, m
);
1009 (*ifp
->if_input
)(ifp
, m
);
1013 * Supporting routines.
1017 * Given a source and destination address, copy 'amount' of a packet from the
1018 * ring buffer into a linear destination buffer. Takes into account ring-wrap.
1020 static inline void *
1021 ed_ring_copy(struct ed_softc
*sc
, void *src
, void *dst
, u_short amount
)
1025 /* Does copy wrap to lower addr in ring buffer? */
1026 if ((char*)src
+ amount
> (char*)sc
->mem_end
) {
1027 tmp_amount
= (char*)sc
->mem_end
- (char*)src
;
1029 /* Copy amount up to end of NIC memory. */
1030 word_copy(src
, dst
, tmp_amount
);
1032 amount
-= tmp_amount
;
1034 dst
= (char*)dst
+ tmp_amount
;
1037 word_copy(src
, dst
, amount
);
1039 return ((char*)src
+ amount
);
1043 * Copy data from receive buffer to end of mbuf chain allocate additional mbufs
1044 * as needed. Return pointer to last mbuf in chain.
1045 * sc = ed info (softc)
1046 * src = pointer in ed ring buffer
1047 * dst = pointer to last mbuf in mbuf chain to copy to
1048 * amount = amount of data to copy
1051 ed_ring_to_mbuf(struct ed_softc
*sc
, void *src
, struct mbuf
*dst
,
1054 register struct mbuf
*m
= dst
;
1056 /* Round the length to a word boundary. */
1057 /* total_len = (total_len + 1) & ~1; */
1060 register u_short amount
= min(total_len
, M_TRAILINGSPACE(m
));
1064 * No more data in this mbuf; alloc another.
1066 * If there is enough data for an mbuf cluster, attempt
1067 * to allocate one of those, otherwise, a regular mbuf
1069 * Note that a regular mbuf is always required, even if
1070 * we get a cluster - getting a cluster does not
1071 * allocate any mbufs, and one is needed to assign the
1072 * cluster to. The mbuf that has a cluster extension
1073 * can not be used to contain data - only the cluster
1077 MGET(m
, M_DONTWAIT
, MT_DATA
);
1081 if (total_len
>= MINCLSIZE
)
1082 MCLGET(m
, M_DONTWAIT
);
1086 amount
= min(total_len
, M_TRAILINGSPACE(m
));
1089 src
= ed_ring_copy(sc
, src
, mtod(m
, char *) + m
->m_len
,
1093 total_len
-= amount
;
1099 * Compute the multicast address filter from the list of multicast addresses we
1100 * need to listen to.
1103 ed_getmcaf(struct ethercom
*ac
, u_long
*af
)
1105 struct ifnet
*ifp
= &ac
->ec_if
;
1106 struct ether_multi
*enm
;
1107 register u_char
*cp
, c
;
1108 register u_long crc
;
1109 register int i
, len
;
1110 struct ether_multistep step
;
1113 * Set up multicast address filter by passing all multicast addresses
1114 * through a crc generator, and then using the high order 6 bits as an
1115 * index into the 64 bit logical address filter. The high order bit
1116 * selects the word, while the rest of the bits select the bit within
1120 if (ifp
->if_flags
& IFF_PROMISC
) {
1121 ifp
->if_flags
|= IFF_ALLMULTI
;
1122 af
[0] = af
[1] = 0xffffffff;
1127 ETHER_FIRST_MULTI(step
, ac
, enm
);
1128 while (enm
!= NULL
) {
1129 if (memcmp(enm
->enm_addrlo
, enm
->enm_addrhi
,
1130 sizeof(enm
->enm_addrlo
)) != 0) {
1132 * We must listen to a range of multicast addresses.
1133 * For now, just accept all multicasts, rather than
1134 * trying to set only those filter bits needed to match
1135 * the range. (At this time, the only use of address
1136 * ranges is for IP multicast routing, for which the
1137 * range is big enough to require all bits set.)
1139 ifp
->if_flags
|= IFF_ALLMULTI
;
1140 af
[0] = af
[1] = 0xffffffff;
1144 cp
= enm
->enm_addrlo
;
1146 for (len
= sizeof(enm
->enm_addrlo
); --len
>= 0;) {
1148 for (i
= 8; --i
>= 0;) {
1149 if (((crc
& 0x80000000) ? 1 : 0) ^ (c
& 0x01)) {
1151 crc
^= 0x04c11db6 | 1;
1157 /* Just want the 6 most significant bits. */
1160 /* Turn on the corresponding bit in the filter. */
1161 af
[crc
>> 5] |= 1 << ((crc
& 0x1f) ^ 0);
1163 ETHER_NEXT_MULTI(step
, enm
);
1165 ifp
->if_flags
&= ~IFF_ALLMULTI
;
1169 * Copy packet from mbuf to the board memory
1171 * Currently uses an extra buffer/extra memory copy,
1172 * unless the whole packet fits in one mbuf.
1176 ed_put(struct ed_softc
*sc
, struct mbuf
*m
, void *buf
)
1178 u_char
*data
, savebyte
[2];
1182 totlen
= wantbyte
= 0;
1184 for (; m
!= 0; m
= m
->m_next
) {
1185 data
= mtod(m
, u_char
*);
1189 /* Finish the last word. */
1191 savebyte
[1] = *data
;
1192 word_copy(savebyte
, buf
, 2);
1193 buf
= (char*)buf
+ 2;
1198 /* Output contiguous words. */
1200 word_copy(data
, buf
, len
);
1201 buf
= (char*)buf
+ (len
& ~1);
1205 /* Save last byte, if necessary. */
1207 savebyte
[0] = *data
;
1215 word_copy(savebyte
, buf
, 2);
1216 buf
= (char*)buf
+ 2;
1219 if (totlen
< ETHER_MIN_LEN
- ETHER_CRC_LEN
) {
1220 word_zero(buf
, ETHER_MIN_LEN
- ETHER_CRC_LEN
- totlen
);
1221 totlen
= ETHER_MIN_LEN
- ETHER_CRC_LEN
;