1 /* $NetBSD: dp8390.c,v 1.71 2009/05/12 14:25:17 cegger 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.
16 #include <sys/cdefs.h>
17 __KERNEL_RCSID(0, "$NetBSD: dp8390.c,v 1.71 2009/05/12 14:25:17 cegger Exp $");
19 #include "opt_ipkdb.h"
24 #include <sys/param.h>
25 #include <sys/systm.h>
26 #include <sys/device.h>
27 #include <sys/errno.h>
28 #include <sys/ioctl.h>
30 #include <sys/socket.h>
31 #include <sys/syslog.h>
38 #include <net/if_dl.h>
39 #include <net/if_types.h>
40 #include <net/if_media.h>
41 #include <net/if_ether.h>
44 #include <netinet/in.h>
45 #include <netinet/in_systm.h>
46 #include <netinet/in_var.h>
47 #include <netinet/ip.h>
48 #include <netinet/if_inarp.h>
54 #include <net/bpfdesc.h>
60 #include <ipkdb/ipkdb.h>
63 #include <dev/ic/dp8390reg.h>
64 #include <dev/ic/dp8390var.h>
67 #define inline /* XXX for debugging porpoises */
71 static inline void dp8390_xmit(struct dp8390_softc
*);
73 static inline void dp8390_read_hdr(struct dp8390_softc
*,
74 int, struct dp8390_ring
*);
75 static inline int dp8390_ring_copy(struct dp8390_softc
*,
76 int, void *, u_short
);
77 static inline int dp8390_write_mbuf(struct dp8390_softc
*,
80 static int dp8390_test_mem(struct dp8390_softc
*);
83 * Standard media init routine for the dp8390.
86 dp8390_media_init(struct dp8390_softc
*sc
)
89 ifmedia_init(&sc
->sc_media
, 0, dp8390_mediachange
, dp8390_mediastatus
);
90 ifmedia_add(&sc
->sc_media
, IFM_ETHER
|IFM_MANUAL
, 0, NULL
);
91 ifmedia_set(&sc
->sc_media
, IFM_ETHER
|IFM_MANUAL
);
95 * Do bus-independent setup.
98 dp8390_config(struct dp8390_softc
*sc
)
100 struct ifnet
*ifp
= &sc
->sc_ec
.ec_if
;
106 sc
->test_mem
= dp8390_test_mem
;
108 /* Allocate one xmit buffer if < 16k, two buffers otherwise. */
109 if ((sc
->mem_size
< 16384) ||
110 (sc
->sc_flags
& DP8390_NO_MULTI_BUFFERING
))
112 else if (sc
->mem_size
< 8192 * 3)
117 sc
->tx_page_start
= sc
->mem_start
>> ED_PAGE_SHIFT
;
118 sc
->rec_page_start
= sc
->tx_page_start
+ sc
->txb_cnt
* ED_TXBUF_SIZE
;
119 sc
->rec_page_stop
= sc
->tx_page_start
+ (sc
->mem_size
>> ED_PAGE_SHIFT
);
120 sc
->mem_ring
= sc
->mem_start
+ (sc
->rec_page_start
<< ED_PAGE_SHIFT
);
121 sc
->mem_end
= sc
->mem_start
+ sc
->mem_size
;
123 /* Now zero memory and verify that it is clear. */
124 if ((*sc
->test_mem
)(sc
))
127 /* Set interface to stopped condition (reset). */
130 /* Initialize ifnet structure. */
131 strcpy(ifp
->if_xname
, device_xname(sc
->sc_dev
));
133 ifp
->if_start
= dp8390_start
;
134 ifp
->if_ioctl
= dp8390_ioctl
;
135 if (!ifp
->if_watchdog
)
136 ifp
->if_watchdog
= dp8390_watchdog
;
138 IFF_BROADCAST
| IFF_SIMPLEX
| IFF_NOTRAILERS
| IFF_MULTICAST
;
139 IFQ_SET_READY(&ifp
->if_snd
);
141 /* Print additional info when attached. */
142 aprint_normal_dev(sc
->sc_dev
, "Ethernet address %s\n",
143 ether_sprintf(sc
->sc_enaddr
));
145 /* Initialize media goo. */
146 (*sc
->sc_media_init
)(sc
);
149 * We can support 802.1Q VLAN-sized frames.
151 sc
->sc_ec
.ec_capabilities
|= ETHERCAP_VLAN_MTU
;
153 /* Attach the interface. */
155 ether_ifattach(ifp
, sc
->sc_enaddr
);
158 rnd_attach_source(&sc
->rnd_source
, device_xname(sc
->sc_dev
),
162 /* The attach is successful. */
163 sc
->sc_flags
|= DP8390_ATTACHED
;
171 * Media change callback.
174 dp8390_mediachange(struct ifnet
*ifp
)
176 struct dp8390_softc
*sc
= ifp
->if_softc
;
178 if (sc
->sc_mediachange
)
179 return ((*sc
->sc_mediachange
)(sc
));
184 * Media status callback.
187 dp8390_mediastatus(struct ifnet
*ifp
, struct ifmediareq
*ifmr
)
189 struct dp8390_softc
*sc
= ifp
->if_softc
;
191 if (sc
->sc_enabled
== 0) {
192 ifmr
->ifm_active
= IFM_ETHER
| IFM_NONE
;
193 ifmr
->ifm_status
= 0;
197 if (sc
->sc_mediastatus
)
198 (*sc
->sc_mediastatus
)(sc
, ifmr
);
205 dp8390_reset(struct dp8390_softc
*sc
)
216 * Take interface offline.
219 dp8390_stop(struct dp8390_softc
*sc
)
221 bus_space_tag_t regt
= sc
->sc_regt
;
222 bus_space_handle_t regh
= sc
->sc_regh
;
225 /* Stop everything on the interface, and select page 0 registers. */
226 NIC_BARRIER(regt
, regh
);
227 NIC_PUT(regt
, regh
, ED_P0_CR
,
228 sc
->cr_proto
| ED_CR_PAGE_0
| ED_CR_STP
);
229 NIC_BARRIER(regt
, regh
);
232 * Wait for interface to enter stopped state, but limit # of checks to
233 * 'n' (about 5ms). It shouldn't even take 5us on modern DS8390's, but
234 * just in case it's an old one.
236 while (((NIC_GET(regt
, regh
,
237 ED_P0_ISR
) & ED_ISR_RST
) == 0) && --n
)
240 if (sc
->stop_card
!= NULL
)
241 (*sc
->stop_card
)(sc
);
245 * Device timeout/watchdog routine. Entered if the device neglects to generate
246 * an interrupt after a transmit has been started on it.
250 dp8390_watchdog(struct ifnet
*ifp
)
252 struct dp8390_softc
*sc
= ifp
->if_softc
;
254 log(LOG_ERR
, "%s: device timeout\n", device_xname(sc
->sc_dev
));
255 ++sc
->sc_ec
.ec_if
.if_oerrors
;
264 dp8390_init(struct dp8390_softc
*sc
)
266 bus_space_tag_t regt
= sc
->sc_regt
;
267 bus_space_handle_t regh
= sc
->sc_regh
;
268 struct ifnet
*ifp
= &sc
->sc_ec
.ec_if
;
273 * Initialize the NIC in the exact order outlined in the NS manual.
274 * This init procedure is "mandatory"...don't change what or when
278 /* Reset transmitter flags. */
285 /* Set interface for page 0, remote DMA complete, stopped. */
286 NIC_BARRIER(regt
, regh
);
287 NIC_PUT(regt
, regh
, ED_P0_CR
,
288 sc
->cr_proto
| ED_CR_PAGE_0
| ED_CR_STP
);
289 NIC_BARRIER(regt
, regh
);
291 if (sc
->dcr_reg
& ED_DCR_LS
) {
292 NIC_PUT(regt
, regh
, ED_P0_DCR
, sc
->dcr_reg
);
295 * Set FIFO threshold to 8, No auto-init Remote DMA, byte
296 * order=80x86, byte-wide DMA xfers,
298 NIC_PUT(regt
, regh
, ED_P0_DCR
, ED_DCR_FT1
| ED_DCR_LS
);
301 /* Clear remote byte count registers. */
302 NIC_PUT(regt
, regh
, ED_P0_RBCR0
, 0);
303 NIC_PUT(regt
, regh
, ED_P0_RBCR1
, 0);
305 /* Tell RCR to do nothing for now. */
306 NIC_PUT(regt
, regh
, ED_P0_RCR
, ED_RCR_MON
| sc
->rcr_proto
);
308 /* Place NIC in internal loopback mode. */
309 NIC_PUT(regt
, regh
, ED_P0_TCR
, ED_TCR_LB0
);
311 /* Set lower bits of byte addressable framing to 0. */
313 NIC_PUT(regt
, regh
, 0x09, 0);
315 /* Initialize receive buffer ring. */
316 NIC_PUT(regt
, regh
, ED_P0_BNRY
, sc
->rec_page_start
);
317 NIC_PUT(regt
, regh
, ED_P0_PSTART
, sc
->rec_page_start
);
318 NIC_PUT(regt
, regh
, ED_P0_PSTOP
, sc
->rec_page_stop
);
321 * Enable the following interrupts: receive/transmit complete,
322 * receive/transmit error, and Receiver OverWrite.
324 * Counter overflow and Remote DMA complete are *not* enabled.
326 NIC_PUT(regt
, regh
, ED_P0_IMR
,
327 ED_IMR_PRXE
| ED_IMR_PTXE
| ED_IMR_RXEE
| ED_IMR_TXEE
|
331 * Clear all interrupts. A '1' in each bit position clears the
332 * corresponding flag.
334 NIC_PUT(regt
, regh
, ED_P0_ISR
, 0xff);
336 /* Program command register for page 1. */
337 NIC_BARRIER(regt
, regh
);
338 NIC_PUT(regt
, regh
, ED_P0_CR
,
339 sc
->cr_proto
| ED_CR_PAGE_1
| ED_CR_STP
);
340 NIC_BARRIER(regt
, regh
);
342 /* Copy out our station address. */
343 for (i
= 0; i
< ETHER_ADDR_LEN
; ++i
)
344 NIC_PUT(regt
, regh
, ED_P1_PAR0
+ i
,
345 CLLADDR(ifp
->if_sadl
)[i
]);
347 /* Set multicast filter on chip. */
348 dp8390_getmcaf(&sc
->sc_ec
, mcaf
);
349 for (i
= 0; i
< 8; i
++)
350 NIC_PUT(regt
, regh
, ED_P1_MAR0
+ i
, mcaf
[i
]);
353 * Set current page pointer to one page after the boundary pointer, as
354 * recommended in the National manual.
356 sc
->next_packet
= sc
->rec_page_start
+ 1;
357 NIC_PUT(regt
, regh
, ED_P1_CURR
, sc
->next_packet
);
359 /* Program command register for page 0. */
360 NIC_BARRIER(regt
, regh
);
361 NIC_PUT(regt
, regh
, ED_P1_CR
,
362 sc
->cr_proto
| ED_CR_PAGE_0
| ED_CR_STP
);
363 NIC_BARRIER(regt
, regh
);
365 /* Accept broadcast and multicast packets by default. */
366 i
= ED_RCR_AB
| ED_RCR_AM
| sc
->rcr_proto
;
367 if (ifp
->if_flags
& IFF_PROMISC
) {
369 * Set promiscuous mode. Multicast filter was set earlier so
370 * that we should receive all multicast packets.
372 i
|= ED_RCR_PRO
| ED_RCR_AR
| ED_RCR_SEP
;
374 NIC_PUT(regt
, regh
, ED_P0_RCR
, i
);
376 /* Take interface out of loopback. */
377 NIC_PUT(regt
, regh
, ED_P0_TCR
, 0);
379 /* Do any card-specific initialization, if applicable. */
381 (*sc
->init_card
)(sc
);
383 /* Fire up the interface. */
384 NIC_BARRIER(regt
, regh
);
385 NIC_PUT(regt
, regh
, ED_P0_CR
,
386 sc
->cr_proto
| ED_CR_PAGE_0
| ED_CR_STA
);
388 /* Set 'running' flag, and clear output active flag. */
389 ifp
->if_flags
|= IFF_RUNNING
;
390 ifp
->if_flags
&= ~IFF_OACTIVE
;
392 /* ...and attempt to start output. */
397 * This routine actually starts the transmission on the interface.
400 dp8390_xmit(struct dp8390_softc
*sc
)
402 bus_space_tag_t regt
= sc
->sc_regt
;
403 bus_space_handle_t regh
= sc
->sc_regh
;
404 struct ifnet
*ifp
= &sc
->sc_ec
.ec_if
;
408 if ((sc
->txb_next_tx
+ sc
->txb_inuse
) % sc
->txb_cnt
!= sc
->txb_new
)
409 panic("dp8390_xmit: desync, next_tx=%d inuse=%d cnt=%d new=%d",
410 sc
->txb_next_tx
, sc
->txb_inuse
, sc
->txb_cnt
, sc
->txb_new
);
412 if (sc
->txb_inuse
== 0)
413 panic("dp8390_xmit: no packets to xmit");
416 len
= sc
->txb_len
[sc
->txb_next_tx
];
418 /* Set NIC for page 0 register access. */
419 NIC_BARRIER(regt
, regh
);
420 NIC_PUT(regt
, regh
, ED_P0_CR
,
421 sc
->cr_proto
| ED_CR_PAGE_0
| ED_CR_STA
);
422 NIC_BARRIER(regt
, regh
);
424 /* Set TX buffer start page. */
425 NIC_PUT(regt
, regh
, ED_P0_TPSR
, sc
->tx_page_start
+
426 sc
->txb_next_tx
* ED_TXBUF_SIZE
);
429 NIC_PUT(regt
, regh
, ED_P0_TBCR0
, len
);
430 NIC_PUT(regt
, regh
, ED_P0_TBCR1
, len
>> 8);
432 /* Set page 0, remote DMA complete, transmit packet, and *start*. */
433 NIC_BARRIER(regt
, regh
);
434 NIC_PUT(regt
, regh
, ED_P0_CR
,
435 sc
->cr_proto
| ED_CR_PAGE_0
| ED_CR_TXP
| ED_CR_STA
);
437 /* Point to next transmit buffer slot and wrap if necessary. */
438 if (++sc
->txb_next_tx
== sc
->txb_cnt
)
441 /* Set a timer just in case we never hear from the board again. */
446 * Start output on interface.
447 * We make two assumptions here:
448 * 1) that the current priority is set to splnet _before_ this code
449 * is called *and* is returned to the appropriate priority after
451 * 2) that the IFF_OACTIVE flag is checked before this code is called
452 * (i.e. that the output part of the interface is idle)
455 dp8390_start(struct ifnet
*ifp
)
457 struct dp8390_softc
*sc
= ifp
->if_softc
;
462 if ((ifp
->if_flags
& (IFF_RUNNING
| IFF_OACTIVE
)) != IFF_RUNNING
)
466 /* See if there is room to put another packet in the buffer. */
467 if (sc
->txb_inuse
== sc
->txb_cnt
) {
468 /* No room. Indicate this to the outside world and exit. */
469 ifp
->if_flags
|= IFF_OACTIVE
;
472 IFQ_DEQUEUE(&ifp
->if_snd
, m0
);
476 /* We need to use m->m_pkthdr.len, so require the header */
477 if ((m0
->m_flags
& M_PKTHDR
) == 0)
478 panic("dp8390_start: no header mbuf");
481 /* Tap off here if there is a BPF listener. */
483 bpf_mtap(ifp
->if_bpf
, m0
);
486 /* txb_new points to next open buffer slot. */
487 buffer
= sc
->mem_start
+
488 ((sc
->txb_new
* ED_TXBUF_SIZE
) << ED_PAGE_SHIFT
);
491 len
= (*sc
->write_mbuf
)(sc
, m0
, buffer
);
493 len
= dp8390_write_mbuf(sc
, m0
, buffer
);
496 sc
->txb_len
[sc
->txb_new
] = len
;
498 /* Point to next buffer slot and wrap if necessary. */
499 if (++sc
->txb_new
== sc
->txb_cnt
)
502 /* Start the first packet transmitting. */
503 if (sc
->txb_inuse
++ == 0)
506 /* Loop back to the top to possibly buffer more packets. */
511 * Ethernet interface receiver interrupt.
514 dp8390_rint(struct dp8390_softc
*sc
)
516 bus_space_tag_t regt
= sc
->sc_regt
;
517 bus_space_handle_t regh
= sc
->sc_regh
;
518 struct dp8390_ring packet_hdr
;
521 u_char boundary
, current
;
525 /* Set NIC to page 1 registers to get 'current' pointer. */
526 NIC_BARRIER(regt
, regh
);
527 NIC_PUT(regt
, regh
, ED_P0_CR
,
528 sc
->cr_proto
| ED_CR_PAGE_1
| ED_CR_STA
);
529 NIC_BARRIER(regt
, regh
);
532 * 'sc->next_packet' is the logical beginning of the ring-buffer - i.e.
533 * it points to where new data has been buffered. The 'CURR' (current)
534 * register points to the logical end of the ring-buffer - i.e. it
535 * points to where additional new data will be added. We loop here
536 * until the logical beginning equals the logical end (or in other
537 * words, until the ring-buffer is empty).
539 current
= NIC_GET(regt
, regh
, ED_P1_CURR
);
540 if (sc
->next_packet
== current
)
543 /* Set NIC to page 0 registers to update boundary register. */
544 NIC_BARRIER(regt
, regh
);
545 NIC_PUT(regt
, regh
, ED_P1_CR
,
546 sc
->cr_proto
| ED_CR_PAGE_0
| ED_CR_STA
);
547 NIC_BARRIER(regt
, regh
);
550 /* Get pointer to this buffer's header structure. */
551 packet_ptr
= sc
->mem_ring
+
552 ((sc
->next_packet
- sc
->rec_page_start
) << ED_PAGE_SHIFT
);
555 (*sc
->read_hdr
)(sc
, packet_ptr
, &packet_hdr
);
557 dp8390_read_hdr(sc
, packet_ptr
, &packet_hdr
);
558 len
= packet_hdr
.count
;
561 * Try do deal with old, buggy chips that sometimes duplicate
562 * the low byte of the length into the high byte. We do this
563 * by simply ignoring the high byte of the length and always
566 * NOTE: sc->next_packet is pointing at the current packet.
568 if (packet_hdr
.next_packet
>= sc
->next_packet
)
569 nlen
= (packet_hdr
.next_packet
- sc
->next_packet
);
571 nlen
= ((packet_hdr
.next_packet
- sc
->rec_page_start
) +
572 (sc
->rec_page_stop
- sc
->next_packet
));
574 if ((len
& ED_PAGE_MASK
) + sizeof(packet_hdr
) > ED_PAGE_SIZE
)
576 len
= (len
& ED_PAGE_MASK
) | (nlen
<< ED_PAGE_SHIFT
);
578 if (len
!= packet_hdr
.count
) {
579 aprint_verbose_dev(sc
->sc_dev
, "length does not match "
580 "next packet pointer\n");
581 aprint_verbose_dev(sc
->sc_dev
, "len %04x nlen %04x "
582 "start %02x first %02x curr %02x next %02x "
583 "stop %02x\n", packet_hdr
.count
, len
,
584 sc
->rec_page_start
, sc
->next_packet
, current
,
585 packet_hdr
.next_packet
, sc
->rec_page_stop
);
590 * Be fairly liberal about what we allow as a "reasonable"
591 * length so that a [crufty] packet will make it to BPF (and
592 * can thus be analyzed). Note that all that is really
593 * important is that we have a length that will fit into one
594 * mbuf cluster or less; the upper layer protocols can then
595 * figure out the length from their own length field(s).
597 if (len
<= MCLBYTES
&&
598 packet_hdr
.next_packet
>= sc
->rec_page_start
&&
599 packet_hdr
.next_packet
< sc
->rec_page_stop
) {
602 packet_ptr
+ sizeof(struct dp8390_ring
),
603 len
- sizeof(struct dp8390_ring
));
605 /* Really BAD. The ring pointers are corrupted. */
606 log(LOG_ERR
, "%s: NIC memory corrupt - "
607 "invalid packet length %d\n",
608 device_xname(sc
->sc_dev
), len
);
609 ++sc
->sc_ec
.ec_if
.if_ierrors
;
614 /* Update next packet pointer. */
615 sc
->next_packet
= packet_hdr
.next_packet
;
618 * Update NIC boundary pointer - being careful to keep it one
619 * buffer behind (as recommended by NS databook).
621 boundary
= sc
->next_packet
- 1;
622 if (boundary
< sc
->rec_page_start
)
623 boundary
= sc
->rec_page_stop
- 1;
624 NIC_PUT(regt
, regh
, ED_P0_BNRY
, boundary
);
625 } while (sc
->next_packet
!= current
);
630 /* Ethernet interface interrupt processor. */
632 dp8390_intr(void *arg
)
634 struct dp8390_softc
*sc
= (struct dp8390_softc
*)arg
;
635 bus_space_tag_t regt
= sc
->sc_regt
;
636 bus_space_handle_t regh
= sc
->sc_regh
;
637 struct ifnet
*ifp
= &sc
->sc_ec
.ec_if
;
643 if (sc
->sc_enabled
== 0 ||
644 !device_is_active(sc
->sc_dev
))
647 /* Set NIC to page 0 registers. */
648 NIC_BARRIER(regt
, regh
);
649 NIC_PUT(regt
, regh
, ED_P0_CR
,
650 sc
->cr_proto
| ED_CR_PAGE_0
| ED_CR_STA
);
651 NIC_BARRIER(regt
, regh
);
653 isr
= NIC_GET(regt
, regh
, ED_P0_ISR
);
661 /* Loop until there are no more new interrupts. */
664 * Reset all the bits that we are 'acknowledging' by writing a
665 * '1' to each bit position that was set.
666 * (Writing a '1' *clears* the bit.)
668 NIC_PUT(regt
, regh
, ED_P0_ISR
, isr
);
670 /* Work around for AX88190 bug */
671 if ((sc
->sc_flags
& DP8390_DO_AX88190_WORKAROUND
) != 0)
672 while ((NIC_GET(regt
, regh
, ED_P0_ISR
) & isr
) != 0) {
673 NIC_PUT(regt
, regh
, ED_P0_ISR
, 0);
674 NIC_PUT(regt
, regh
, ED_P0_ISR
, isr
);
678 * Handle transmitter interrupts. Handle these first because
679 * the receiver will reset the board under some conditions.
681 * If the chip was reset while a packet was transmitting, it
682 * may still deliver a TX interrupt. In this case, just ignore
685 if (isr
& (ED_ISR_PTX
| ED_ISR_TXE
) &&
686 sc
->txb_inuse
!= 0) {
688 NIC_GET(regt
, regh
, ED_P0_NCR
) & 0x0f;
691 * Check for transmit error. If a TX completed with an
692 * error, we end up throwing the packet away. Really
693 * the only error that is possible is excessive
694 * collisions, and in this case it is best to allow the
695 * automatic mechanisms of TCP to backoff the flow. Of
696 * course, with UDP we're screwed, but this is expected
697 * when a network is heavily loaded.
699 if (isr
& ED_ISR_TXE
) {
701 * Excessive collisions (16).
703 if ((NIC_GET(regt
, regh
, ED_P0_TSR
)
704 & ED_TSR_ABT
) && (collisions
== 0)) {
706 * When collisions total 16, the P0_NCR
707 * will indicate 0, and the TSR_ABT is
713 /* Update output errors counter. */
717 * Throw away the non-error status bits.
720 * It may be useful to detect loss of carrier
721 * and late collisions here.
723 (void)NIC_GET(regt
, regh
, ED_P0_TSR
);
726 * Update total number of successfully
727 * transmitted packets.
732 /* Clear watchdog timer. */
734 ifp
->if_flags
&= ~IFF_OACTIVE
;
737 * Add in total number of collisions on last
740 ifp
->if_collisions
+= collisions
;
743 * Decrement buffer in-use count if not zero (can only
744 * be zero if a transmitter interrupt occurred while not
745 * actually transmitting).
746 * If data is ready to transmit, start it transmitting,
747 * otherwise defer until after handling receiver.
749 if (--sc
->txb_inuse
!= 0)
753 /* Handle receiver interrupts. */
754 if (isr
& (ED_ISR_PRX
| ED_ISR_RXE
| ED_ISR_OVW
)) {
756 * Overwrite warning. In order to make sure that a
757 * lockup of the local DMA hasn't occurred, we reset
758 * and re-init the NIC. The NSC manual suggests only a
759 * partial reset/re-init is necessary - but some chips
760 * seem to want more. The DMA lockup has been seen
761 * only with early rev chips - Methinks this bug was
762 * fixed in later revs. -DG
764 if (isr
& ED_ISR_OVW
) {
767 log(LOG_WARNING
, "%s: warning - receiver "
768 "ring buffer overrun\n",
769 device_xname(sc
->sc_dev
));
771 /* Stop/reset/re-init NIC. */
775 * Receiver Error. One or more of: CRC error,
776 * frame alignment error FIFO overrun, or
779 if (isr
& ED_ISR_RXE
) {
783 printf("%s: receive error %x\n",
784 device_xname(sc
->sc_dev
),
792 * Go get the packet(s)
793 * XXX - Doing this on an error is dubious
794 * because there shouldn't be any data to get
795 * (we've configured the interface to not
796 * accept packets with errors).
806 * If it looks like the transmitter can take more data, attempt
807 * to start output on the interface. This is done after
808 * handling the receiver to give the receiver priority.
813 * Return NIC CR to standard state: page 0, remote DMA
814 * complete, start (toggling the TXP bit off, even if was just
815 * set in the transmit routine, is *okay* - it is 'edge'
816 * triggered from low to high).
818 NIC_BARRIER(regt
, regh
);
819 NIC_PUT(regt
, regh
, ED_P0_CR
,
820 sc
->cr_proto
| ED_CR_PAGE_0
| ED_CR_STA
);
821 NIC_BARRIER(regt
, regh
);
824 * If the Network Talley Counters overflow, read them to reset
825 * them. It appears that old 8390's won't clear the ISR flag
826 * otherwise - resulting in an infinite loop.
828 if (isr
& ED_ISR_CNT
) {
829 (void)NIC_GET(regt
, regh
, ED_P0_CNTR0
);
830 (void)NIC_GET(regt
, regh
, ED_P0_CNTR1
);
831 (void)NIC_GET(regt
, regh
, ED_P0_CNTR2
);
834 isr
= NIC_GET(regt
, regh
, ED_P0_ISR
);
841 rnd_add_uint32(&sc
->rnd_source
, rndisr
);
847 * Process an ioctl request. This code needs some work - it looks pretty ugly.
850 dp8390_ioctl(struct ifnet
*ifp
, u_long cmd
, void *data
)
852 struct dp8390_softc
*sc
= ifp
->if_softc
;
853 struct ifaddr
*ifa
= (struct ifaddr
*) data
;
854 struct ifreq
*ifr
= (struct ifreq
*) data
;
862 if ((error
= dp8390_enable(sc
)) != 0)
864 ifp
->if_flags
|= IFF_UP
;
867 switch (ifa
->ifa_addr
->sa_family
) {
870 arp_ifinit(ifp
, ifa
);
879 if ((error
= ifioctl_common(ifp
, cmd
, data
)) != 0)
881 switch (ifp
->if_flags
& (IFF_UP
|IFF_RUNNING
)) {
884 * If interface is marked down and it is running, then
888 ifp
->if_flags
&= ~IFF_RUNNING
;
893 * If interface is marked up and it is stopped, then
896 if ((error
= dp8390_enable(sc
)) != 0)
900 case IFF_UP
|IFF_RUNNING
:
902 * Reset the interface to pick up changes in any other
903 * flags that affect hardware registers.
915 if (sc
->sc_enabled
== 0) {
920 /* Update our multicast list. */
921 if ((error
= ether_ioctl(ifp
, cmd
, data
)) == ENETRESET
) {
923 * Multicast list has changed; set the hardware filter
926 if (ifp
->if_flags
& IFF_RUNNING
) {
927 dp8390_stop(sc
); /* XXX for ds_setmcaf? */
936 error
= ifmedia_ioctl(ifp
, ifr
, &sc
->sc_media
, cmd
);
940 error
= ether_ioctl(ifp
, cmd
, data
);
949 * Retrieve packet from buffer memory and send to the next level up via
950 * ether_input(). If there is a BPF listener, give a copy to BPF, too.
953 dp8390_read(struct dp8390_softc
*sc
, int buf
, u_short len
)
955 struct ifnet
*ifp
= &sc
->sc_ec
.ec_if
;
958 /* Pull packet off interface. */
959 m
= dp8390_get(sc
, buf
, len
);
969 * Check if there's a BPF listener on this interface.
970 * If so, hand off the raw packet to bpf.
973 bpf_mtap(ifp
->if_bpf
, m
);
976 (*ifp
->if_input
)(ifp
, m
);
981 * Supporting routines.
985 * Compute the multicast address filter from the list of multicast addresses we
989 dp8390_getmcaf(struct ethercom
*ec
, u_int8_t
*af
)
991 struct ifnet
*ifp
= &ec
->ec_if
;
992 struct ether_multi
*enm
;
995 struct ether_multistep step
;
998 * Set up multicast address filter by passing all multicast addresses
999 * through a crc generator, and then using the high order 6 bits as an
1000 * index into the 64 bit logical address filter. The high order bit
1001 * selects the word, while the rest of the bits select the bit within
1005 if (ifp
->if_flags
& IFF_PROMISC
) {
1006 ifp
->if_flags
|= IFF_ALLMULTI
;
1007 for (i
= 0; i
< 8; i
++)
1011 for (i
= 0; i
< 8; i
++)
1013 ETHER_FIRST_MULTI(step
, ec
, enm
);
1014 while (enm
!= NULL
) {
1015 if (memcmp(enm
->enm_addrlo
, enm
->enm_addrhi
,
1016 sizeof(enm
->enm_addrlo
)) != 0) {
1018 * We must listen to a range of multicast addresses.
1019 * For now, just accept all multicasts, rather than
1020 * trying to set only those filter bits needed to match
1021 * the range. (At this time, the only use of address
1022 * ranges is for IP multicast routing, for which the
1023 * range is big enough to require all bits set.)
1025 ifp
->if_flags
|= IFF_ALLMULTI
;
1026 for (i
= 0; i
< 8; i
++)
1031 crc
= ether_crc32_be(enm
->enm_addrlo
, ETHER_ADDR_LEN
);
1033 /* Just want the 6 most significant bits. */
1036 /* Turn on the corresponding bit in the filter. */
1037 af
[crc
>> 3] |= 1 << (crc
& 0x7);
1039 ETHER_NEXT_MULTI(step
, enm
);
1041 ifp
->if_flags
&= ~IFF_ALLMULTI
;
1045 * Copy data from receive buffer to a new mbuf chain allocating mbufs
1046 * as needed. Return pointer to first mbuf in chain.
1047 * sc = dp8390 info (softc)
1048 * src = pointer in dp8390 ring buffer
1049 * total_len = amount of data to copy
1052 dp8390_get(struct dp8390_softc
*sc
, int src
, u_short total_len
)
1054 struct ifnet
*ifp
= &sc
->sc_ec
.ec_if
;
1055 struct mbuf
*m
, *m0
, *newm
;
1058 MGETHDR(m0
, M_DONTWAIT
, MT_DATA
);
1061 m0
->m_pkthdr
.rcvif
= ifp
;
1062 m0
->m_pkthdr
.len
= total_len
;
1066 while (total_len
> 0) {
1067 if (total_len
>= MINCLSIZE
) {
1068 MCLGET(m
, M_DONTWAIT
);
1069 if ((m
->m_flags
& M_EXT
) == 0)
1075 * Make sure the data after the Ethernet header is aligned.
1078 char *newdata
= (char *)
1079 ALIGN(m
->m_data
+ sizeof(struct ether_header
)) -
1080 sizeof(struct ether_header
);
1081 len
-= newdata
- m
->m_data
;
1082 m
->m_data
= newdata
;
1085 m
->m_len
= len
= min(total_len
, len
);
1087 src
= (*sc
->ring_copy
)(sc
, src
, mtod(m
, void *), len
);
1089 src
= dp8390_ring_copy(sc
, src
, mtod(m
, void *), len
);
1092 if (total_len
> 0) {
1093 MGET(newm
, M_DONTWAIT
, MT_DATA
);
1097 m
= m
->m_next
= newm
;
1110 * Default driver support functions.
1112 * NOTE: all support functions assume 8-bit shared memory.
1115 * Zero NIC buffer memory and verify that it is clear.
1118 dp8390_test_mem(struct dp8390_softc
*sc
)
1120 bus_space_tag_t buft
= sc
->sc_buft
;
1121 bus_space_handle_t bufh
= sc
->sc_bufh
;
1124 bus_space_set_region_1(buft
, bufh
, sc
->mem_start
, 0, sc
->mem_size
);
1126 for (i
= 0; i
< sc
->mem_size
; ++i
) {
1127 if (bus_space_read_1(buft
, bufh
, sc
->mem_start
+ i
)) {
1128 printf(": failed to clear NIC buffer at offset %x - "
1129 "check configuration\n", (sc
->mem_start
+ i
));
1138 * Read a packet header from the ring, given the source offset.
1141 dp8390_read_hdr(struct dp8390_softc
*sc
, int src
, struct dp8390_ring
*hdrp
)
1143 bus_space_tag_t buft
= sc
->sc_buft
;
1144 bus_space_handle_t bufh
= sc
->sc_bufh
;
1147 * The byte count includes a 4 byte header that was added by
1150 hdrp
->rsr
= bus_space_read_1(buft
, bufh
, src
);
1151 hdrp
->next_packet
= bus_space_read_1(buft
, bufh
, src
+ 1);
1152 hdrp
->count
= bus_space_read_1(buft
, bufh
, src
+ 2) |
1153 (bus_space_read_1(buft
, bufh
, src
+ 3) << 8);
1157 * Copy `amount' bytes from a packet in the ring buffer to a linear
1158 * destination buffer, given a source offset and destination address.
1159 * Takes into account ring-wrap.
1162 dp8390_ring_copy(struct dp8390_softc
*sc
, int src
, void *dst
, u_short amount
)
1164 bus_space_tag_t buft
= sc
->sc_buft
;
1165 bus_space_handle_t bufh
= sc
->sc_bufh
;
1168 /* Does copy wrap to lower addr in ring buffer? */
1169 if (src
+ amount
> sc
->mem_end
) {
1170 tmp_amount
= sc
->mem_end
- src
;
1172 /* Copy amount up to end of NIC memory. */
1173 bus_space_read_region_1(buft
, bufh
, src
, dst
, tmp_amount
);
1175 amount
-= tmp_amount
;
1177 dst
= (char *)dst
+ tmp_amount
;
1179 bus_space_read_region_1(buft
, bufh
, src
, dst
, amount
);
1181 return (src
+ amount
);
1185 * Copy a packet from an mbuf to the transmit buffer on the card.
1187 * Currently uses an extra buffer/extra memory copy, unless the whole
1188 * packet fits in one mbuf.
1191 dp8390_write_mbuf(struct dp8390_softc
*sc
, struct mbuf
*m
, int buf
)
1193 bus_space_tag_t buft
= sc
->sc_buft
;
1194 bus_space_handle_t bufh
= sc
->sc_bufh
;
1196 int len
, totlen
= 0;
1198 for (; m
; m
= m
->m_next
) {
1199 data
= mtod(m
, u_char
*);
1202 bus_space_write_region_1(buft
, bufh
, buf
, data
, len
);
1207 if (totlen
< ETHER_MIN_LEN
- ETHER_CRC_LEN
) {
1208 bus_space_set_region_1(buft
, bufh
, buf
, 0,
1209 ETHER_MIN_LEN
- ETHER_CRC_LEN
- totlen
);
1210 totlen
= ETHER_MIN_LEN
- ETHER_CRC_LEN
;
1216 * Enable power on the interface.
1219 dp8390_enable(struct dp8390_softc
*sc
)
1222 if (sc
->sc_enabled
== 0 && sc
->sc_enable
!= NULL
) {
1223 if ((*sc
->sc_enable
)(sc
) != 0) {
1224 aprint_error_dev(sc
->sc_dev
,
1225 "device enable failed\n");
1235 * Disable power on the interface.
1238 dp8390_disable(struct dp8390_softc
*sc
)
1241 if (sc
->sc_enabled
!= 0 && sc
->sc_disable
!= NULL
) {
1242 (*sc
->sc_disable
)(sc
);
1248 dp8390_activate(device_t self
, enum devact act
)
1250 struct dp8390_softc
*sc
= device_private(self
);
1253 case DVACT_DEACTIVATE
:
1254 if_deactivate(&sc
->sc_ec
.ec_if
);
1262 dp8390_detach(struct dp8390_softc
*sc
, int flags
)
1264 struct ifnet
*ifp
= &sc
->sc_ec
.ec_if
;
1266 /* Succeed now if there's no work to do. */
1267 if ((sc
->sc_flags
& DP8390_ATTACHED
) == 0)
1270 /* dp8390_disable() checks sc->sc_enabled */
1273 if (sc
->sc_media_fini
!= NULL
)
1274 (*sc
->sc_media_fini
)(sc
);
1276 /* Delete all remaining media. */
1277 ifmedia_delete_instance(&sc
->sc_media
, IFM_INST_ANY
);
1280 rnd_detach_source(&sc
->rnd_source
);
1282 ether_ifdetach(ifp
);
1289 static void dp8390_ipkdb_hwinit(struct ipkdb_if
*);
1290 static void dp8390_ipkdb_init(struct ipkdb_if
*);
1291 static void dp8390_ipkdb_leave(struct ipkdb_if
*);
1292 static int dp8390_ipkdb_rcv(struct ipkdb_if
*, u_char
*, int);
1293 static void dp8390_ipkdb_send(struct ipkdb_if
*, u_char
*, int);
1296 * This is essentially similar to dp8390_config above.
1299 dp8390_ipkdb_attach(struct ipkdb_if
*kip
)
1301 struct dp8390_softc
*sc
= kip
->port
;
1303 if (sc
->mem_size
< 8192 * 2)
1305 else if (sc
->mem_size
< 8192 * 3)
1310 sc
->tx_page_start
= sc
->mem_start
>> ED_PAGE_SHIFT
;
1311 sc
->rec_page_start
= sc
->tx_page_start
+ sc
->txb_cnt
* ED_TXBUF_SIZE
;
1312 sc
->rec_page_stop
= sc
->tx_page_start
+ (sc
->mem_size
>> ED_PAGE_SHIFT
);
1313 sc
->mem_ring
= sc
->mem_start
+ (sc
->rec_page_start
<< ED_PAGE_SHIFT
);
1314 sc
->mem_end
= sc
->mem_start
+ sc
->mem_size
;
1318 kip
->start
= dp8390_ipkdb_init
;
1319 kip
->leave
= dp8390_ipkdb_leave
;
1320 kip
->receive
= dp8390_ipkdb_rcv
;
1321 kip
->send
= dp8390_ipkdb_send
;
1327 * Similar to dp8390_init above.
1330 dp8390_ipkdb_hwinit(struct ipkdb_if
*kip
)
1332 struct dp8390_softc
*sc
= kip
->port
;
1333 struct ifnet
*ifp
= &sc
->sc_ec
.ec_if
;
1334 bus_space_tag_t regt
= sc
->sc_regt
;
1335 bus_space_handle_t regh
= sc
->sc_regh
;
1340 sc
->txb_next_tx
= 0;
1343 if (sc
->dcr_reg
& ED_DCR_LS
)
1344 NIC_PUT(regt
, regh
, ED_P0_DCR
, sc
->dcr_reg
);
1346 NIC_PUT(regt
, regh
, ED_P0_DCR
, ED_DCR_FT1
| ED_DCR_LS
);
1347 NIC_PUT(regt
, regh
, ED_P0_RBCR0
, 0);
1348 NIC_PUT(regt
, regh
, ED_P0_RBCR1
, 0);
1349 NIC_PUT(regt
, regh
, ED_P0_RCR
, ED_RCR_MON
| sc
->rcr_proto
);
1350 NIC_PUT(regt
, regh
, ED_P0_TCR
, ED_TCR_LB0
);
1352 NIC_PUT(regt
, regh
, 0x09, 0);
1353 NIC_PUT(regt
, regh
, ED_P0_BNRY
, sc
->rec_page_start
);
1354 NIC_PUT(regt
, regh
, ED_P0_PSTART
, sc
->rec_page_start
);
1355 NIC_PUT(regt
, regh
, ED_P0_PSTOP
, sc
->rec_page_stop
);
1356 NIC_PUT(regt
, regh
, ED_P0_IMR
, 0);
1357 NIC_BARRIER(regt
, regh
);
1358 NIC_PUT(regt
, regh
, ED_P0_ISR
, 0xff);
1360 NIC_BARRIER(regt
, regh
);
1361 NIC_PUT(regt
, regh
, ED_P0_CR
,
1362 sc
->cr_proto
| ED_CR_PAGE_1
| ED_CR_STP
);
1363 NIC_BARRIER(regt
, regh
);
1365 for (i
= 0; i
< sizeof kip
->myenetaddr
; i
++)
1366 NIC_PUT(regt
, regh
, ED_P1_PAR0
+ i
, kip
->myenetaddr
[i
]);
1367 /* multicast filter? */
1369 sc
->next_packet
= sc
->rec_page_start
+ 1;
1370 NIC_PUT(regt
, regh
, ED_P1_CURR
, sc
->next_packet
);
1372 NIC_BARRIER(regt
, regh
);
1373 NIC_PUT(regt
, regh
, ED_P1_CR
,
1374 sc
->cr_proto
| ED_CR_PAGE_0
| ED_CR_STP
);
1375 NIC_BARRIER(regt
, regh
);
1377 /* promiscuous mode? */
1378 NIC_PUT(regt
, regh
, ED_P0_RCR
, ED_RCR_AB
| ED_RCR_AM
| sc
->rcr_proto
);
1379 NIC_PUT(regt
, regh
, ED_P0_TCR
, 0);
1381 /* card-specific initialization? */
1383 NIC_BARRIER(regt
, regh
);
1384 NIC_PUT(regt
, regh
, ED_P0_CR
,
1385 sc
->cr_proto
| ED_CR_PAGE_0
| ED_CR_STA
);
1387 ifp
->if_flags
&= ~IFF_OACTIVE
;
1391 dp8390_ipkdb_init(struct ipkdb_if
*kip
)
1393 struct dp8390_softc
*sc
= kip
->port
;
1394 bus_space_tag_t regt
= sc
->sc_regt
;
1395 bus_space_handle_t regh
= sc
->sc_regh
;
1398 cmd
= NIC_GET(regt
, regh
, ED_P0_CR
) & ~(ED_CR_PAGE_3
| ED_CR_STA
);
1401 NIC_BARRIER(regt
, regh
);
1402 NIC_PUT(regt
, regh
, ED_P0_CR
, cmd
| ED_CR_PAGE_0
| ED_CR_STP
);
1403 NIC_BARRIER(regt
, regh
);
1405 /* If not started, init chip */
1406 if (cmd
& ED_CR_STP
)
1407 dp8390_ipkdb_hwinit(kip
);
1409 /* If output active, wait for packets to drain */
1410 while (sc
->txb_inuse
) {
1411 while (!(cmd
= (NIC_GET(regt
, regh
, ED_P0_ISR
)
1412 & (ED_ISR_PTX
| ED_ISR_TXE
))))
1414 NIC_PUT(regt
, regh
, ED_P0_ISR
, cmd
);
1415 if (--sc
->txb_inuse
)
1421 dp8390_ipkdb_leave(struct ipkdb_if
*kip
)
1423 struct dp8390_softc
*sc
= kip
->port
;
1424 struct ifnet
*ifp
= &sc
->sc_ec
.ec_if
;
1430 * Similar to dp8390_intr above.
1433 dp8390_ipkdb_rcv(struct ipkdb_if
*kip
, u_char
*buf
, int poll
)
1435 struct dp8390_softc
*sc
= kip
->port
;
1436 bus_space_tag_t regt
= sc
->sc_regt
;
1437 bus_space_handle_t regh
= sc
->sc_regh
;
1438 u_char bnry
, current
, isr
;
1439 int len
, nlen
, packet_ptr
;
1440 struct dp8390_ring packet_hdr
;
1442 /* Switch to page 0. */
1443 NIC_BARRIER(regt
, regh
);
1444 NIC_PUT(regt
, regh
, ED_P0_CR
,
1445 sc
->cr_proto
| ED_CR_PAGE_0
| ED_CR_STA
);
1446 NIC_BARRIER(regt
, regh
);
1449 isr
= NIC_GET(regt
, regh
, ED_P0_ISR
);
1450 NIC_PUT(regt
, regh
, ED_P0_ISR
, isr
);
1452 if (isr
& (ED_ISR_PRX
| ED_ISR_TXE
)) {
1453 NIC_GET(regt
, regh
, ED_P0_NCR
);
1454 NIC_GET(regt
, regh
, ED_P0_TSR
);
1457 if (isr
& ED_ISR_OVW
) {
1458 dp8390_ipkdb_hwinit(kip
);
1462 if (isr
& ED_ISR_CNT
) {
1463 NIC_GET(regt
, regh
, ED_P0_CNTR0
);
1464 NIC_GET(regt
, regh
, ED_P0_CNTR1
);
1465 NIC_GET(regt
, regh
, ED_P0_CNTR2
);
1468 /* Similar to dp8390_rint above. */
1469 NIC_BARRIER(regt
, regh
);
1470 NIC_PUT(regt
, regh
, ED_P0_CR
,
1471 sc
->cr_proto
| ED_CR_PAGE_1
| ED_CR_STA
);
1472 NIC_BARRIER(regt
, regh
);
1474 current
= NIC_GET(regt
, regh
, ED_P1_CURR
);
1476 NIC_BARRIER(regt
, regh
);
1477 NIC_PUT(regt
, regh
, ED_P1_CR
,
1478 sc
->cr_proto
| ED_CR_PAGE_0
| ED_CR_STA
);
1479 NIC_BARRIER(regt
, regh
);
1481 if (sc
->next_packet
== current
) {
1487 packet_ptr
= sc
->mem_ring
1488 + ((sc
->next_packet
- sc
->rec_page_start
) << ED_PAGE_SHIFT
);
1489 sc
->read_hdr(sc
, packet_ptr
, &packet_hdr
);
1490 len
= packet_hdr
.count
;
1491 nlen
= packet_hdr
.next_packet
- sc
->next_packet
;
1493 nlen
+= sc
->rec_page_stop
- sc
->rec_page_start
;
1495 if ((len
& ED_PAGE_MASK
) + sizeof(packet_hdr
) > ED_PAGE_SIZE
)
1497 len
= (len
& ED_PAGE_MASK
) | (nlen
<< ED_PAGE_SHIFT
);
1498 len
-= sizeof(packet_hdr
);
1501 && packet_hdr
.next_packet
>= sc
->rec_page_start
1502 && packet_hdr
.next_packet
< sc
->rec_page_stop
) {
1503 sc
->ring_copy(sc
, packet_ptr
+ sizeof(packet_hdr
),
1505 sc
->next_packet
= packet_hdr
.next_packet
;
1506 bnry
= sc
->next_packet
- 1;
1507 if (bnry
< sc
->rec_page_start
)
1508 bnry
= sc
->rec_page_stop
- 1;
1509 NIC_PUT(regt
, regh
, ED_P0_BNRY
, bnry
);
1513 dp8390_ipkdb_hwinit(kip
);
1518 dp8390_ipkdb_send(struct ipkdb_if
*kip
, u_char
*buf
, int l
)
1520 struct dp8390_softc
*sc
= kip
->port
;
1521 bus_space_tag_t regt
= sc
->sc_regt
;
1522 bus_space_handle_t regh
= sc
->sc_regh
;
1526 mb
.m_pkthdr
.len
= mb
.m_len
= l
;
1528 mb
.m_flags
= M_EXT
| M_PKTHDR
;
1529 mb
.m_type
= MT_DATA
;
1531 l
= sc
->write_mbuf(sc
, &mb
,
1532 sc
->mem_start
+ ((sc
->txb_new
* ED_TXBUF_SIZE
) << ED_PAGE_SHIFT
));
1533 sc
->txb_len
[sc
->txb_new
] = max(l
, ETHER_MIN_LEN
- ETHER_CRC_LEN
);
1535 if (++sc
->txb_new
== sc
->txb_cnt
)
1541 while (!(NIC_GET(regt
, regh
, ED_P0_ISR
) & (ED_ISR_PTX
| ED_ISR_TXE
)))