Home | History | Annotate | Line # | Download | only in dev
mb8795.c revision 1.24.8.4
      1  1.24.8.4  nathanw /*	$NetBSD: mb8795.c,v 1.24.8.4 2002/09/17 21:16:26 nathanw Exp $	*/
      2  1.24.8.2  nathanw /*
      3  1.24.8.2  nathanw  * Copyright (c) 1998 Darrin B. Jewell
      4  1.24.8.2  nathanw  * All rights reserved.
      5  1.24.8.2  nathanw  *
      6  1.24.8.2  nathanw  * Redistribution and use in source and binary forms, with or without
      7  1.24.8.2  nathanw  * modification, are permitted provided that the following conditions
      8  1.24.8.2  nathanw  * are met:
      9  1.24.8.2  nathanw  * 1. Redistributions of source code must retain the above copyright
     10  1.24.8.2  nathanw  *    notice, this list of conditions and the following disclaimer.
     11  1.24.8.2  nathanw  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.24.8.2  nathanw  *    notice, this list of conditions and the following disclaimer in the
     13  1.24.8.2  nathanw  *    documentation and/or other materials provided with the distribution.
     14  1.24.8.2  nathanw  * 3. All advertising materials mentioning features or use of this software
     15  1.24.8.2  nathanw  *    must display the following acknowledgement:
     16  1.24.8.2  nathanw  *      This product includes software developed by Darrin B. Jewell
     17  1.24.8.2  nathanw  * 4. The name of the author may not be used to endorse or promote products
     18  1.24.8.2  nathanw  *    derived from this software without specific prior written permission
     19  1.24.8.2  nathanw  *
     20  1.24.8.2  nathanw  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  1.24.8.2  nathanw  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  1.24.8.2  nathanw  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  1.24.8.2  nathanw  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  1.24.8.2  nathanw  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  1.24.8.2  nathanw  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  1.24.8.2  nathanw  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  1.24.8.2  nathanw  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  1.24.8.2  nathanw  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  1.24.8.2  nathanw  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  1.24.8.2  nathanw  */
     31  1.24.8.2  nathanw 
     32  1.24.8.2  nathanw #include "opt_inet.h"
     33  1.24.8.2  nathanw #include "opt_ccitt.h"
     34  1.24.8.2  nathanw #include "opt_llc.h"
     35  1.24.8.2  nathanw #include "opt_ns.h"
     36  1.24.8.2  nathanw #include "bpfilter.h"
     37  1.24.8.2  nathanw #include "rnd.h"
     38  1.24.8.2  nathanw 
     39  1.24.8.2  nathanw #include <sys/param.h>
     40  1.24.8.2  nathanw #include <sys/systm.h>
     41  1.24.8.2  nathanw #include <sys/mbuf.h>
     42  1.24.8.2  nathanw #include <sys/syslog.h>
     43  1.24.8.2  nathanw #include <sys/socket.h>
     44  1.24.8.2  nathanw #include <sys/device.h>
     45  1.24.8.2  nathanw #include <sys/malloc.h>
     46  1.24.8.2  nathanw #include <sys/ioctl.h>
     47  1.24.8.2  nathanw #include <sys/errno.h>
     48  1.24.8.2  nathanw #if NRND > 0
     49  1.24.8.2  nathanw #include <sys/rnd.h>
     50  1.24.8.2  nathanw #endif
     51  1.24.8.2  nathanw 
     52  1.24.8.2  nathanw #include <net/if.h>
     53  1.24.8.2  nathanw #include <net/if_dl.h>
     54  1.24.8.2  nathanw #include <net/if_ether.h>
     55  1.24.8.2  nathanw 
     56  1.24.8.2  nathanw #include <net/if_media.h>
     57  1.24.8.2  nathanw 
     58  1.24.8.2  nathanw #ifdef INET
     59  1.24.8.2  nathanw #include <netinet/in.h>
     60  1.24.8.2  nathanw #include <netinet/if_inarp.h>
     61  1.24.8.2  nathanw #include <netinet/in_systm.h>
     62  1.24.8.2  nathanw #include <netinet/in_var.h>
     63  1.24.8.2  nathanw #include <netinet/ip.h>
     64  1.24.8.2  nathanw #endif
     65  1.24.8.2  nathanw 
     66  1.24.8.2  nathanw #ifdef NS
     67  1.24.8.2  nathanw #include <netns/ns.h>
     68  1.24.8.2  nathanw #include <netns/ns_if.h>
     69  1.24.8.2  nathanw #endif
     70  1.24.8.2  nathanw 
     71  1.24.8.2  nathanw #if defined(CCITT) && defined(LLC)
     72  1.24.8.2  nathanw #include <sys/socketvar.h>
     73  1.24.8.2  nathanw #include <netccitt/x25.h>
     74  1.24.8.2  nathanw #include <netccitt/pk.h>
     75  1.24.8.2  nathanw #include <netccitt/pk_var.h>
     76  1.24.8.2  nathanw #include <netccitt/pk_extern.h>
     77  1.24.8.2  nathanw #endif
     78  1.24.8.2  nathanw 
     79  1.24.8.2  nathanw #if NBPFILTER > 0
     80  1.24.8.2  nathanw #include <net/bpf.h>
     81  1.24.8.2  nathanw #include <net/bpfdesc.h>
     82  1.24.8.2  nathanw #endif
     83  1.24.8.2  nathanw 
     84  1.24.8.2  nathanw #include <machine/cpu.h>
     85  1.24.8.2  nathanw #include <machine/bus.h>
     86  1.24.8.2  nathanw #include <machine/intr.h>
     87  1.24.8.2  nathanw 
     88  1.24.8.2  nathanw /* @@@ this is here for the REALIGN_DMABUF hack below */
     89  1.24.8.2  nathanw #include "nextdmareg.h"
     90  1.24.8.2  nathanw #include "nextdmavar.h"
     91  1.24.8.2  nathanw 
     92  1.24.8.2  nathanw #include "mb8795reg.h"
     93  1.24.8.2  nathanw #include "mb8795var.h"
     94  1.24.8.2  nathanw 
     95  1.24.8.3  nathanw #include "bmapreg.h"
     96  1.24.8.3  nathanw 
     97  1.24.8.4  nathanw #ifdef DEBUG
     98  1.24.8.4  nathanw #define MB8795_DEBUG
     99  1.24.8.2  nathanw #endif
    100  1.24.8.2  nathanw 
    101  1.24.8.3  nathanw #define PRINTF(x) printf x;
    102  1.24.8.4  nathanw #ifdef MB8795_DEBUG
    103  1.24.8.4  nathanw int mb8795_debug = 0;
    104  1.24.8.4  nathanw #define DPRINTF(x) if (mb8795_debug) printf x;
    105  1.24.8.2  nathanw #else
    106  1.24.8.2  nathanw #define DPRINTF(x)
    107  1.24.8.2  nathanw #endif
    108  1.24.8.2  nathanw 
    109  1.24.8.4  nathanw extern int turbo;
    110  1.24.8.2  nathanw 
    111  1.24.8.2  nathanw /*
    112  1.24.8.2  nathanw  * Support for
    113  1.24.8.2  nathanw  * Fujitsu Ethernet Data Link Controller (MB8795)
    114  1.24.8.2  nathanw  * and the Fujitsu Manchester Encoder/Decoder (MB502).
    115  1.24.8.2  nathanw  */
    116  1.24.8.2  nathanw 
    117  1.24.8.2  nathanw void mb8795_shutdown __P((void *));
    118  1.24.8.2  nathanw 
    119  1.24.8.2  nathanw bus_dmamap_t mb8795_txdma_restart __P((bus_dmamap_t,void *));
    120  1.24.8.4  nathanw void mb8795_start_dma __P((struct mb8795_softc *));
    121  1.24.8.2  nathanw 
    122  1.24.8.3  nathanw int	mb8795_mediachange __P((struct ifnet *));
    123  1.24.8.3  nathanw void	mb8795_mediastatus __P((struct ifnet *, struct ifmediareq *));
    124  1.24.8.3  nathanw 
    125  1.24.8.2  nathanw void
    126  1.24.8.3  nathanw mb8795_config(sc, media, nmedia, defmedia)
    127  1.24.8.3  nathanw 	struct mb8795_softc *sc;
    128  1.24.8.3  nathanw 	int *media, nmedia, defmedia;
    129  1.24.8.2  nathanw {
    130  1.24.8.3  nathanw 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    131  1.24.8.2  nathanw 
    132  1.24.8.2  nathanw 	DPRINTF(("%s: mb8795_config()\n",sc->sc_dev.dv_xname));
    133  1.24.8.2  nathanw 
    134  1.24.8.4  nathanw 	/* Initialize ifnet structure. */
    135  1.24.8.4  nathanw 	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
    136  1.24.8.4  nathanw 	ifp->if_softc = sc;
    137  1.24.8.4  nathanw 	ifp->if_start = mb8795_start;
    138  1.24.8.4  nathanw 	ifp->if_ioctl = mb8795_ioctl;
    139  1.24.8.4  nathanw 	ifp->if_watchdog = mb8795_watchdog;
    140  1.24.8.4  nathanw 	ifp->if_flags =
    141  1.24.8.4  nathanw 		IFF_BROADCAST | IFF_NOTRAILERS;
    142  1.24.8.2  nathanw 
    143  1.24.8.3  nathanw 	/* Initialize media goo. */
    144  1.24.8.3  nathanw 	ifmedia_init(&sc->sc_media, 0, mb8795_mediachange,
    145  1.24.8.4  nathanw 		     mb8795_mediastatus);
    146  1.24.8.3  nathanw 	if (media != NULL) {
    147  1.24.8.4  nathanw 		int i;
    148  1.24.8.3  nathanw 		for (i = 0; i < nmedia; i++)
    149  1.24.8.3  nathanw 			ifmedia_add(&sc->sc_media, media[i], 0, NULL);
    150  1.24.8.3  nathanw 		ifmedia_set(&sc->sc_media, defmedia);
    151  1.24.8.3  nathanw 	} else {
    152  1.24.8.3  nathanw 		ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
    153  1.24.8.3  nathanw 		ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
    154  1.24.8.3  nathanw 	}
    155  1.24.8.3  nathanw 
    156  1.24.8.2  nathanw   /* Attach the interface. */
    157  1.24.8.2  nathanw   if_attach(ifp);
    158  1.24.8.2  nathanw   ether_ifattach(ifp, sc->sc_enaddr);
    159  1.24.8.2  nathanw 
    160  1.24.8.2  nathanw   sc->sc_sh = shutdownhook_establish(mb8795_shutdown, sc);
    161  1.24.8.2  nathanw   if (sc->sc_sh == NULL)
    162  1.24.8.2  nathanw     panic("mb8795_config: can't establish shutdownhook");
    163  1.24.8.2  nathanw 
    164  1.24.8.2  nathanw #if NRND > 0
    165  1.24.8.2  nathanw   rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
    166  1.24.8.2  nathanw                     RND_TYPE_NET, 0);
    167  1.24.8.2  nathanw #endif
    168  1.24.8.2  nathanw 
    169  1.24.8.2  nathanw 	DPRINTF(("%s: leaving mb8795_config()\n",sc->sc_dev.dv_xname));
    170  1.24.8.2  nathanw }
    171  1.24.8.2  nathanw 
    172  1.24.8.3  nathanw /*
    173  1.24.8.3  nathanw  * Media change callback.
    174  1.24.8.3  nathanw  */
    175  1.24.8.3  nathanw int
    176  1.24.8.3  nathanw mb8795_mediachange(ifp)
    177  1.24.8.3  nathanw 	struct ifnet *ifp;
    178  1.24.8.3  nathanw {
    179  1.24.8.3  nathanw 	struct mb8795_softc *sc = ifp->if_softc;
    180  1.24.8.3  nathanw 	int data;
    181  1.24.8.3  nathanw 
    182  1.24.8.4  nathanw 	if (turbo)
    183  1.24.8.4  nathanw 		return (0);
    184  1.24.8.4  nathanw 
    185  1.24.8.3  nathanw 	switch IFM_SUBTYPE(sc->sc_media.ifm_media) {
    186  1.24.8.3  nathanw 	case IFM_AUTO:
    187  1.24.8.4  nathanw 		if ((bus_space_read_1(sc->sc_bmap_bst, sc->sc_bmap_bsh, BMAP_DATA) &
    188  1.24.8.4  nathanw 		     BMAP_DATA_UTPENABLED_MASK) ||
    189  1.24.8.4  nathanw 		    !(bus_space_read_1(sc->sc_bmap_bst, sc->sc_bmap_bsh, BMAP_DATA) &
    190  1.24.8.4  nathanw 		      BMAP_DATA_UTPCARRIER_MASK)) {
    191  1.24.8.3  nathanw 			data = BMAP_DATA_UTPENABLE;
    192  1.24.8.3  nathanw 			sc->sc_media.ifm_cur->ifm_data = IFM_ETHER|IFM_10_T;
    193  1.24.8.4  nathanw 		} else {
    194  1.24.8.4  nathanw 			data = BMAP_DATA_BNCENABLE;
    195  1.24.8.4  nathanw 			sc->sc_media.ifm_cur->ifm_data = IFM_ETHER|IFM_10_2;
    196  1.24.8.3  nathanw 		}
    197  1.24.8.3  nathanw 		break;
    198  1.24.8.3  nathanw 	case IFM_10_T:
    199  1.24.8.3  nathanw 		data = BMAP_DATA_UTPENABLE;
    200  1.24.8.3  nathanw 		break;
    201  1.24.8.3  nathanw 	case IFM_10_2:
    202  1.24.8.4  nathanw 		data = BMAP_DATA_BNCENABLE;
    203  1.24.8.3  nathanw 		break;
    204  1.24.8.3  nathanw 	default:
    205  1.24.8.3  nathanw 		return (1);
    206  1.24.8.3  nathanw 		break;
    207  1.24.8.3  nathanw 	}
    208  1.24.8.3  nathanw 
    209  1.24.8.3  nathanw 	bus_space_write_1(sc->sc_bmap_bst, sc->sc_bmap_bsh,
    210  1.24.8.3  nathanw 			  BMAP_DDIR, BMAP_DDIR_UTPENABLE_MASK);
    211  1.24.8.3  nathanw 	bus_space_write_1(sc->sc_bmap_bst, sc->sc_bmap_bsh,
    212  1.24.8.3  nathanw 			  BMAP_DATA, data);
    213  1.24.8.3  nathanw 
    214  1.24.8.3  nathanw 	return (0);
    215  1.24.8.3  nathanw }
    216  1.24.8.3  nathanw 
    217  1.24.8.3  nathanw /*
    218  1.24.8.3  nathanw  * Media status callback.
    219  1.24.8.3  nathanw  */
    220  1.24.8.3  nathanw void
    221  1.24.8.3  nathanw mb8795_mediastatus(ifp, ifmr)
    222  1.24.8.3  nathanw 	struct ifnet *ifp;
    223  1.24.8.3  nathanw 	struct ifmediareq *ifmr;
    224  1.24.8.3  nathanw {
    225  1.24.8.3  nathanw 	struct mb8795_softc *sc = ifp->if_softc;
    226  1.24.8.4  nathanw 
    227  1.24.8.4  nathanw 	if (turbo)
    228  1.24.8.4  nathanw 		return;
    229  1.24.8.3  nathanw 
    230  1.24.8.3  nathanw 	if (IFM_SUBTYPE(ifmr->ifm_active) == IFM_AUTO) {
    231  1.24.8.3  nathanw 		ifmr->ifm_active = sc->sc_media.ifm_cur->ifm_data;
    232  1.24.8.3  nathanw 	}
    233  1.24.8.3  nathanw 	if (IFM_SUBTYPE(ifmr->ifm_active) == IFM_10_T) {
    234  1.24.8.3  nathanw 		ifmr->ifm_status = IFM_AVALID;
    235  1.24.8.3  nathanw 		if (!(bus_space_read_1(sc->sc_bmap_bst, sc->sc_bmap_bsh, BMAP_DATA) &
    236  1.24.8.3  nathanw 		      BMAP_DATA_UTPCARRIER_MASK))
    237  1.24.8.3  nathanw 			ifmr->ifm_status |= IFM_ACTIVE;
    238  1.24.8.3  nathanw 	} else {
    239  1.24.8.3  nathanw 		ifmr->ifm_status &= ~IFM_AVALID; /* don't know for 10_2 */
    240  1.24.8.3  nathanw 	}
    241  1.24.8.3  nathanw 	return;
    242  1.24.8.3  nathanw }
    243  1.24.8.2  nathanw 
    244  1.24.8.2  nathanw /****************************************************************/
    245  1.24.8.4  nathanw #ifdef MB8795_DEBUG
    246  1.24.8.2  nathanw #define XCHR(x) "0123456789abcdef"[(x) & 0xf]
    247  1.24.8.2  nathanw static void
    248  1.24.8.4  nathanw mb8795_hex_dump(unsigned char *pkt, size_t len)
    249  1.24.8.2  nathanw {
    250  1.24.8.2  nathanw 	size_t i, j;
    251  1.24.8.2  nathanw 
    252  1.24.8.2  nathanw 	printf("00000000  ");
    253  1.24.8.2  nathanw 	for(i=0; i<len; i++) {
    254  1.24.8.2  nathanw 		printf("%c%c ", XCHR(pkt[i]>>4), XCHR(pkt[i]));
    255  1.24.8.2  nathanw 		if ((i+1) % 16 == 8) {
    256  1.24.8.2  nathanw 			printf(" ");
    257  1.24.8.2  nathanw 		}
    258  1.24.8.2  nathanw 		if ((i+1) % 16 == 0) {
    259  1.24.8.2  nathanw 			printf(" %c", '|');
    260  1.24.8.2  nathanw 			for(j=0; j<16; j++) {
    261  1.24.8.2  nathanw 				printf("%c", pkt[i-15+j]>=32 && pkt[i-15+j]<127?pkt[i-15+j]:'.');
    262  1.24.8.2  nathanw 			}
    263  1.24.8.2  nathanw 			printf("%c\n%c%c%c%c%c%c%c%c  ", '|',
    264  1.24.8.2  nathanw 					XCHR((i+1)>>28),XCHR((i+1)>>24),XCHR((i+1)>>20),XCHR((i+1)>>16),
    265  1.24.8.2  nathanw 					XCHR((i+1)>>12), XCHR((i+1)>>8), XCHR((i+1)>>4), XCHR(i+1));
    266  1.24.8.2  nathanw 		}
    267  1.24.8.2  nathanw 	}
    268  1.24.8.2  nathanw 	printf("\n");
    269  1.24.8.2  nathanw }
    270  1.24.8.2  nathanw #undef XCHR
    271  1.24.8.2  nathanw #endif
    272  1.24.8.2  nathanw 
    273  1.24.8.2  nathanw /*
    274  1.24.8.2  nathanw  * Controller receive interrupt.
    275  1.24.8.2  nathanw  */
    276  1.24.8.2  nathanw void
    277  1.24.8.2  nathanw mb8795_rint(sc)
    278  1.24.8.4  nathanw 	struct mb8795_softc *sc;
    279  1.24.8.2  nathanw {
    280  1.24.8.2  nathanw 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    281  1.24.8.2  nathanw 	int error = 0;
    282  1.24.8.2  nathanw 	u_char rxstat;
    283  1.24.8.2  nathanw 	u_char rxmask;
    284  1.24.8.2  nathanw 
    285  1.24.8.4  nathanw 	rxstat = MB_READ_REG(sc, MB8795_RXSTAT);
    286  1.24.8.4  nathanw 	rxmask = MB_READ_REG(sc, MB8795_RXMASK);
    287  1.24.8.2  nathanw 
    288  1.24.8.4  nathanw 	MB_WRITE_REG(sc, MB8795_RXSTAT, MB8795_RXSTAT_CLEAR);
    289  1.24.8.2  nathanw 
    290  1.24.8.4  nathanw 	if (rxstat & MB8795_RXSTAT_RESET) {
    291  1.24.8.2  nathanw 		DPRINTF(("%s: rx reset packet\n",
    292  1.24.8.2  nathanw 				sc->sc_dev.dv_xname));
    293  1.24.8.2  nathanw 		error++;
    294  1.24.8.2  nathanw 	}
    295  1.24.8.4  nathanw 	if (rxstat & MB8795_RXSTAT_SHORT) {
    296  1.24.8.2  nathanw 		DPRINTF(("%s: rx short packet\n",
    297  1.24.8.2  nathanw 				sc->sc_dev.dv_xname));
    298  1.24.8.2  nathanw 		error++;
    299  1.24.8.2  nathanw 	}
    300  1.24.8.4  nathanw 	if (rxstat & MB8795_RXSTAT_ALIGNERR) {
    301  1.24.8.2  nathanw 		DPRINTF(("%s: rx alignment error\n",
    302  1.24.8.2  nathanw 				sc->sc_dev.dv_xname));
    303  1.24.8.2  nathanw #if 0
    304  1.24.8.2  nathanw 		error++;
    305  1.24.8.2  nathanw #endif
    306  1.24.8.2  nathanw 	}
    307  1.24.8.4  nathanw 	if (rxstat & MB8795_RXSTAT_CRCERR) {
    308  1.24.8.2  nathanw 		DPRINTF(("%s: rx CRC error\n",
    309  1.24.8.2  nathanw 				sc->sc_dev.dv_xname));
    310  1.24.8.2  nathanw #if 0
    311  1.24.8.2  nathanw 		error++;
    312  1.24.8.2  nathanw #endif
    313  1.24.8.2  nathanw 	}
    314  1.24.8.4  nathanw 	if (rxstat & MB8795_RXSTAT_OVERFLOW) {
    315  1.24.8.2  nathanw 		DPRINTF(("%s: rx overflow error\n",
    316  1.24.8.2  nathanw 				sc->sc_dev.dv_xname));
    317  1.24.8.2  nathanw #if 0
    318  1.24.8.2  nathanw 		error++;
    319  1.24.8.2  nathanw #endif
    320  1.24.8.2  nathanw 	}
    321  1.24.8.2  nathanw 
    322  1.24.8.2  nathanw 	if (error) {
    323  1.24.8.2  nathanw 		ifp->if_ierrors++;
    324  1.24.8.2  nathanw 		/* @@@ handle more gracefully, free memory, etc. */
    325  1.24.8.2  nathanw 	}
    326  1.24.8.2  nathanw 
    327  1.24.8.4  nathanw 	if (rxstat & MB8795_RXSTAT_OK) {
    328  1.24.8.4  nathanw 		struct mbuf *m;
    329  1.24.8.2  nathanw 		int s;
    330  1.24.8.2  nathanw 		s = spldma();
    331  1.24.8.2  nathanw 
    332  1.24.8.4  nathanw 		while ((m = MBDMA_RX_MBUF (sc))) {
    333  1.24.8.4  nathanw 			m->m_pkthdr.len = m->m_len;
    334  1.24.8.2  nathanw 			m->m_flags |= M_HASFCS;
    335  1.24.8.2  nathanw 			m->m_pkthdr.rcvif = ifp;
    336  1.24.8.2  nathanw 
    337  1.24.8.2  nathanw 			/* Find receive length, keep crc */
    338  1.24.8.2  nathanw 			/* enable dma interrupts while we process the packet */
    339  1.24.8.2  nathanw 			splx(s);
    340  1.24.8.2  nathanw 
    341  1.24.8.4  nathanw #if defined(MB8795_DEBUG)
    342  1.24.8.2  nathanw 			/* Peek at the packet */
    343  1.24.8.2  nathanw 			DPRINTF(("%s: received packet, at VA %p-%p,len %d\n",
    344  1.24.8.2  nathanw 					sc->sc_dev.dv_xname,mtod(m,u_char *),mtod(m,u_char *)+m->m_len,m->m_len));
    345  1.24.8.4  nathanw 			if (mb8795_debug > 3) {
    346  1.24.8.4  nathanw 				mb8795_hex_dump(mtod(m,u_char *), m->m_pkthdr.len);
    347  1.24.8.4  nathanw 			} else if (mb8795_debug > 2) {
    348  1.24.8.4  nathanw 				mb8795_hex_dump(mtod(m,u_char *), m->m_pkthdr.len < 255 ? m->m_pkthdr.len : 128 );
    349  1.24.8.2  nathanw 			}
    350  1.24.8.2  nathanw #endif
    351  1.24.8.2  nathanw 
    352  1.24.8.2  nathanw #if NBPFILTER > 0
    353  1.24.8.2  nathanw 			/*
    354  1.24.8.2  nathanw 			 * Pass packet to bpf if there is a listener.
    355  1.24.8.2  nathanw 			 */
    356  1.24.8.2  nathanw 			if (ifp->if_bpf)
    357  1.24.8.2  nathanw 				bpf_mtap(ifp->if_bpf, m);
    358  1.24.8.2  nathanw #endif
    359  1.24.8.2  nathanw 
    360  1.24.8.2  nathanw 			{
    361  1.24.8.2  nathanw 				ifp->if_ipackets++;
    362  1.24.8.2  nathanw 
    363  1.24.8.2  nathanw 				/* Pass the packet up. */
    364  1.24.8.2  nathanw 				(*ifp->if_input)(ifp, m);
    365  1.24.8.2  nathanw 			}
    366  1.24.8.2  nathanw 
    367  1.24.8.2  nathanw 			s = spldma();
    368  1.24.8.2  nathanw 
    369  1.24.8.2  nathanw 		}
    370  1.24.8.2  nathanw 
    371  1.24.8.2  nathanw 		splx(s);
    372  1.24.8.2  nathanw 
    373  1.24.8.2  nathanw 	}
    374  1.24.8.2  nathanw 
    375  1.24.8.4  nathanw #ifdef MB8795_DEBUG
    376  1.24.8.4  nathanw 	if (mb8795_debug) {
    377  1.24.8.2  nathanw 		char sbuf[256];
    378  1.24.8.2  nathanw 
    379  1.24.8.4  nathanw 		bitmask_snprintf(rxstat, MB8795_RXSTAT_BITS, sbuf, sizeof(sbuf));
    380  1.24.8.2  nathanw 		printf("%s: rx interrupt, rxstat = %s\n",
    381  1.24.8.2  nathanw 		       sc->sc_dev.dv_xname, sbuf);
    382  1.24.8.2  nathanw 
    383  1.24.8.4  nathanw 		bitmask_snprintf(MB_READ_REG(sc, MB8795_RXSTAT),
    384  1.24.8.4  nathanw 				 MB8795_RXSTAT_BITS, sbuf, sizeof(sbuf));
    385  1.24.8.2  nathanw 		printf("rxstat = 0x%s\n", sbuf);
    386  1.24.8.2  nathanw 
    387  1.24.8.4  nathanw 		bitmask_snprintf(MB_READ_REG(sc, MB8795_RXMASK),
    388  1.24.8.4  nathanw 				 MB8795_RXMASK_BITS, sbuf, sizeof(sbuf));
    389  1.24.8.2  nathanw 		printf("rxmask = 0x%s\n", sbuf);
    390  1.24.8.2  nathanw 
    391  1.24.8.4  nathanw 		bitmask_snprintf(MB_READ_REG(sc, MB8795_RXMODE),
    392  1.24.8.4  nathanw 				 MB8795_RXMODE_BITS, sbuf, sizeof(sbuf));
    393  1.24.8.2  nathanw 		printf("rxmode = 0x%s\n", sbuf);
    394  1.24.8.2  nathanw 	}
    395  1.24.8.2  nathanw #endif
    396  1.24.8.2  nathanw 
    397  1.24.8.2  nathanw 	return;
    398  1.24.8.2  nathanw }
    399  1.24.8.2  nathanw 
    400  1.24.8.2  nathanw /*
    401  1.24.8.2  nathanw  * Controller transmit interrupt.
    402  1.24.8.2  nathanw  */
    403  1.24.8.2  nathanw void
    404  1.24.8.2  nathanw mb8795_tint(sc)
    405  1.24.8.4  nathanw 	struct mb8795_softc *sc;
    406  1.24.8.2  nathanw {
    407  1.24.8.2  nathanw 	u_char txstat;
    408  1.24.8.2  nathanw 	u_char txmask;
    409  1.24.8.2  nathanw 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    410  1.24.8.2  nathanw 
    411  1.24.8.4  nathanw 	panic ("tint");
    412  1.24.8.4  nathanw 	txstat = MB_READ_REG(sc, MB8795_TXSTAT);
    413  1.24.8.4  nathanw 	txmask = MB_READ_REG(sc, MB8795_TXMASK);
    414  1.24.8.4  nathanw 
    415  1.24.8.4  nathanw 	if ((txstat & MB8795_TXSTAT_READY) ||
    416  1.24.8.4  nathanw 	    (txstat & MB8795_TXSTAT_TXRECV)) {
    417  1.24.8.4  nathanw 		/* printf("X"); */
    418  1.24.8.4  nathanw 		MB_WRITE_REG(sc, MB8795_TXSTAT, MB8795_TXSTAT_CLEAR);
    419  1.24.8.4  nathanw 		/* MB_WRITE_REG(sc, MB8795_TXMASK, txmask & ~MB8795_TXMASK_READYIE); */
    420  1.24.8.4  nathanw 		/* MB_WRITE_REG(sc, MB8795_TXMASK, txmask & ~MB8795_TXMASK_TXRXIE); */
    421  1.24.8.4  nathanw 		MB_WRITE_REG(sc, MB8795_TXMASK, 0);
    422  1.24.8.4  nathanw 		if ((ifp->if_flags & IFF_RUNNING) && !IF_IS_EMPTY(&sc->sc_tx_snd)) {
    423  1.24.8.4  nathanw 			void mb8795_start_dma __P((struct mb8795_softc *)); /* XXXX */
    424  1.24.8.4  nathanw 			/* printf ("Z"); */
    425  1.24.8.4  nathanw 			mb8795_start_dma(sc);
    426  1.24.8.4  nathanw 		}
    427  1.24.8.4  nathanw 		return;
    428  1.24.8.4  nathanw 	}
    429  1.24.8.2  nathanw 
    430  1.24.8.4  nathanw 	if (txstat & MB8795_TXSTAT_SHORTED) {
    431  1.24.8.2  nathanw 		printf("%s: tx cable shorted\n", sc->sc_dev.dv_xname);
    432  1.24.8.2  nathanw 		ifp->if_oerrors++;
    433  1.24.8.2  nathanw 	}
    434  1.24.8.4  nathanw 	if (txstat & MB8795_TXSTAT_UNDERFLOW) {
    435  1.24.8.2  nathanw 		printf("%s: tx underflow\n", sc->sc_dev.dv_xname);
    436  1.24.8.2  nathanw 		ifp->if_oerrors++;
    437  1.24.8.2  nathanw 	}
    438  1.24.8.4  nathanw 	if (txstat & MB8795_TXSTAT_COLLERR) {
    439  1.24.8.2  nathanw 		DPRINTF(("%s: tx collision\n", sc->sc_dev.dv_xname));
    440  1.24.8.2  nathanw 		ifp->if_collisions++;
    441  1.24.8.2  nathanw 	}
    442  1.24.8.4  nathanw 	if (txstat & MB8795_TXSTAT_COLLERR16) {
    443  1.24.8.2  nathanw 		printf("%s: tx 16th collision\n", sc->sc_dev.dv_xname);
    444  1.24.8.2  nathanw 		ifp->if_oerrors++;
    445  1.24.8.2  nathanw 		ifp->if_collisions += 16;
    446  1.24.8.2  nathanw 	}
    447  1.24.8.2  nathanw 
    448  1.24.8.2  nathanw #if 0
    449  1.24.8.4  nathanw 	if (txstat & MB8795_TXSTAT_READY) {
    450  1.24.8.2  nathanw 		char sbuf[256];
    451  1.24.8.2  nathanw 
    452  1.24.8.4  nathanw 		bitmask_snprintf(txstat, MB8795_TXSTAT_BITS, sbuf, sizeof(sbuf));
    453  1.24.8.2  nathanw 		panic("%s: unexpected tx interrupt %s",
    454  1.24.8.2  nathanw 				sc->sc_dev.dv_xname, sbuf);
    455  1.24.8.2  nathanw 
    456  1.24.8.2  nathanw 		/* turn interrupt off */
    457  1.24.8.4  nathanw 		MB_WRITE_REG(sc, MB8795_TXMASK, txmask & ~MB8795_TXMASK_READYIE);
    458  1.24.8.2  nathanw 	}
    459  1.24.8.2  nathanw #endif
    460  1.24.8.2  nathanw 
    461  1.24.8.4  nathanw 	return;
    462  1.24.8.2  nathanw }
    463  1.24.8.2  nathanw 
    464  1.24.8.2  nathanw /****************************************************************/
    465  1.24.8.2  nathanw 
    466  1.24.8.2  nathanw void
    467  1.24.8.2  nathanw mb8795_reset(sc)
    468  1.24.8.2  nathanw 	struct mb8795_softc *sc;
    469  1.24.8.2  nathanw {
    470  1.24.8.2  nathanw 	int s;
    471  1.24.8.3  nathanw 	int i;
    472  1.24.8.2  nathanw 
    473  1.24.8.2  nathanw 	s = splnet();
    474  1.24.8.3  nathanw 
    475  1.24.8.3  nathanw 	DPRINTF (("%s: mb8795_reset()\n",sc->sc_dev.dv_xname));
    476  1.24.8.3  nathanw 
    477  1.24.8.3  nathanw 	sc->sc_ethercom.ec_if.if_flags &= ~(IFF_RUNNING|IFF_OACTIVE);
    478  1.24.8.3  nathanw 	sc->sc_ethercom.ec_if.if_timer = 0;
    479  1.24.8.3  nathanw 
    480  1.24.8.4  nathanw 	MBDMA_RESET(sc);
    481  1.24.8.3  nathanw 
    482  1.24.8.4  nathanw 	MB_WRITE_REG(sc, MB8795_RESET,  MB8795_RESET_MODE);
    483  1.24.8.3  nathanw 
    484  1.24.8.3  nathanw 	mb8795_mediachange(&sc->sc_ethercom.ec_if);
    485  1.24.8.3  nathanw 
    486  1.24.8.3  nathanw #if 0 /* This interrupt was sometimes failing to ack correctly
    487  1.24.8.3  nathanw        * causing a loop @@@
    488  1.24.8.3  nathanw        */
    489  1.24.8.4  nathanw 	MB_WRITE_REG(sc, MB8795_TXMASK,
    490  1.24.8.4  nathanw 			  MB8795_TXMASK_UNDERFLOWIE | MB8795_TXMASK_COLLIE | MB8795_TXMASK_COLL16IE
    491  1.24.8.4  nathanw 			  | MB8795_TXMASK_PARERRIE);
    492  1.24.8.3  nathanw #else
    493  1.24.8.4  nathanw 	MB_WRITE_REG(sc, MB8795_TXMASK, 0);
    494  1.24.8.3  nathanw #endif
    495  1.24.8.4  nathanw 	MB_WRITE_REG(sc, MB8795_TXSTAT, MB8795_TXSTAT_CLEAR);
    496  1.24.8.3  nathanw 
    497  1.24.8.3  nathanw #if 0
    498  1.24.8.4  nathanw 	MB_WRITE_REG(sc, MB8795_RXMASK,
    499  1.24.8.4  nathanw 			  MB8795_RXMASK_OKIE | MB8795_RXMASK_RESETIE | MB8795_RXMASK_SHORTIE	|
    500  1.24.8.4  nathanw 			  MB8795_RXMASK_ALIGNERRIE	|  MB8795_RXMASK_CRCERRIE | MB8795_RXMASK_OVERFLOWIE);
    501  1.24.8.3  nathanw #else
    502  1.24.8.4  nathanw 	MB_WRITE_REG(sc, MB8795_RXMASK,
    503  1.24.8.4  nathanw 			  MB8795_RXMASK_OKIE | MB8795_RXMASK_RESETIE | MB8795_RXMASK_SHORTIE);
    504  1.24.8.3  nathanw #endif
    505  1.24.8.3  nathanw 
    506  1.24.8.4  nathanw 	MB_WRITE_REG(sc, MB8795_RXSTAT, MB8795_RXSTAT_CLEAR);
    507  1.24.8.3  nathanw 
    508  1.24.8.3  nathanw 	for(i=0;i<sizeof(sc->sc_enaddr);i++) {
    509  1.24.8.4  nathanw 		MB_WRITE_REG(sc, MB8795_ENADDR+i, sc->sc_enaddr[i]);
    510  1.24.8.3  nathanw 	}
    511  1.24.8.3  nathanw 
    512  1.24.8.3  nathanw 	DPRINTF(("%s: initializing ethernet %02x:%02x:%02x:%02x:%02x:%02x, size=%d\n",
    513  1.24.8.3  nathanw 		 sc->sc_dev.dv_xname,
    514  1.24.8.3  nathanw 		 sc->sc_enaddr[0],sc->sc_enaddr[1],sc->sc_enaddr[2],
    515  1.24.8.3  nathanw 		 sc->sc_enaddr[3],sc->sc_enaddr[4],sc->sc_enaddr[5],
    516  1.24.8.3  nathanw 		 sizeof(sc->sc_enaddr)));
    517  1.24.8.3  nathanw 
    518  1.24.8.4  nathanw 	MB_WRITE_REG(sc, MB8795_RESET, 0);
    519  1.24.8.3  nathanw 
    520  1.24.8.2  nathanw 	splx(s);
    521  1.24.8.2  nathanw }
    522  1.24.8.2  nathanw 
    523  1.24.8.2  nathanw void
    524  1.24.8.2  nathanw mb8795_watchdog(ifp)
    525  1.24.8.2  nathanw 	struct ifnet *ifp;
    526  1.24.8.2  nathanw {
    527  1.24.8.2  nathanw 	struct mb8795_softc *sc = ifp->if_softc;
    528  1.24.8.2  nathanw 
    529  1.24.8.2  nathanw 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
    530  1.24.8.2  nathanw 	++ifp->if_oerrors;
    531  1.24.8.2  nathanw 
    532  1.24.8.2  nathanw 	DPRINTF(("%s: %lld input errors, %lld input packets\n",
    533  1.24.8.2  nathanw 			sc->sc_dev.dv_xname, ifp->if_ierrors, ifp->if_ipackets));
    534  1.24.8.2  nathanw 
    535  1.24.8.3  nathanw 	ifp->if_flags &= ~IFF_RUNNING;
    536  1.24.8.3  nathanw 	mb8795_init(sc);
    537  1.24.8.2  nathanw }
    538  1.24.8.2  nathanw 
    539  1.24.8.2  nathanw /*
    540  1.24.8.2  nathanw  * Initialization of interface; set up initialization block
    541  1.24.8.2  nathanw  * and transmit/receive descriptor rings.
    542  1.24.8.2  nathanw  */
    543  1.24.8.2  nathanw void
    544  1.24.8.2  nathanw mb8795_init(sc)
    545  1.24.8.4  nathanw 	struct mb8795_softc *sc;
    546  1.24.8.2  nathanw {
    547  1.24.8.3  nathanw 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    548  1.24.8.3  nathanw 	int s;
    549  1.24.8.2  nathanw 
    550  1.24.8.3  nathanw 	DPRINTF (("%s: mb8795_init()\n",sc->sc_dev.dv_xname));
    551  1.24.8.4  nathanw 
    552  1.24.8.3  nathanw 	if (ifp->if_flags & IFF_UP) {
    553  1.24.8.3  nathanw 		int rxmode;
    554  1.24.8.2  nathanw 
    555  1.24.8.4  nathanw 		s = spldma();
    556  1.24.8.4  nathanw 		if ((ifp->if_flags & IFF_RUNNING) == 0)
    557  1.24.8.3  nathanw 			mb8795_reset(sc);
    558  1.24.8.4  nathanw 
    559  1.24.8.4  nathanw 		if (ifp->if_flags & IFF_PROMISC)
    560  1.24.8.4  nathanw 			rxmode = MB8795_RXMODE_PROMISCUOUS;
    561  1.24.8.4  nathanw 		else
    562  1.24.8.4  nathanw 			rxmode = MB8795_RXMODE_NORMAL;
    563  1.24.8.4  nathanw 		/* XXX add support for multicast */
    564  1.24.8.4  nathanw 		if (turbo)
    565  1.24.8.4  nathanw 			rxmode |= MB8795_RXMODE_TEST;
    566  1.24.8.3  nathanw 
    567  1.24.8.4  nathanw 		/* switching mode probably borken now with turbo */
    568  1.24.8.4  nathanw 		MB_WRITE_REG(sc, MB8795_TXMODE,
    569  1.24.8.4  nathanw 			     turbo ? MB8795_TXMODE_TURBO1 : MB8795_TXMODE_LB_DISABLE);
    570  1.24.8.4  nathanw 		MB_WRITE_REG(sc, MB8795_RXMODE, rxmode);
    571  1.24.8.3  nathanw 
    572  1.24.8.3  nathanw 		if ((ifp->if_flags & IFF_RUNNING) == 0) {
    573  1.24.8.4  nathanw 			MBDMA_RX_SETUP(sc);
    574  1.24.8.4  nathanw 			MBDMA_TX_SETUP(sc);
    575  1.24.8.2  nathanw 
    576  1.24.8.3  nathanw 			ifp->if_flags |= IFF_RUNNING;
    577  1.24.8.3  nathanw 			ifp->if_flags &= ~IFF_OACTIVE;
    578  1.24.8.3  nathanw 			ifp->if_timer = 0;
    579  1.24.8.3  nathanw 
    580  1.24.8.4  nathanw 			MBDMA_RX_GO(sc);
    581  1.24.8.3  nathanw 		}
    582  1.24.8.3  nathanw 		splx(s);
    583  1.24.8.2  nathanw #if 0
    584  1.24.8.3  nathanw 		s = spldma();
    585  1.24.8.3  nathanw 		if (! IF_IS_EMPTY(&sc->sc_tx_snd)) {
    586  1.24.8.3  nathanw 			mb8795_start_dma(ifp);
    587  1.24.8.2  nathanw 		}
    588  1.24.8.3  nathanw 		splx(s);
    589  1.24.8.3  nathanw #endif
    590  1.24.8.3  nathanw 	} else {
    591  1.24.8.3  nathanw 		mb8795_reset(sc);
    592  1.24.8.2  nathanw 	}
    593  1.24.8.2  nathanw }
    594  1.24.8.2  nathanw 
    595  1.24.8.2  nathanw void
    596  1.24.8.2  nathanw mb8795_shutdown(arg)
    597  1.24.8.2  nathanw 	void *arg;
    598  1.24.8.2  nathanw {
    599  1.24.8.3  nathanw 	struct mb8795_softc *sc = (struct mb8795_softc *)arg;
    600  1.24.8.4  nathanw 
    601  1.24.8.4  nathanw 	DPRINTF(("%s: mb8795_shutdown()\n",sc->sc_dev.dv_xname));
    602  1.24.8.4  nathanw 
    603  1.24.8.3  nathanw 	mb8795_reset(sc);
    604  1.24.8.2  nathanw }
    605  1.24.8.2  nathanw 
    606  1.24.8.2  nathanw /****************************************************************/
    607  1.24.8.2  nathanw int
    608  1.24.8.2  nathanw mb8795_ioctl(ifp, cmd, data)
    609  1.24.8.2  nathanw 	register struct ifnet *ifp;
    610  1.24.8.2  nathanw 	u_long cmd;
    611  1.24.8.2  nathanw 	caddr_t data;
    612  1.24.8.2  nathanw {
    613  1.24.8.2  nathanw 	register struct mb8795_softc *sc = ifp->if_softc;
    614  1.24.8.2  nathanw 	struct ifaddr *ifa = (struct ifaddr *)data;
    615  1.24.8.2  nathanw 	struct ifreq *ifr = (struct ifreq *)data;
    616  1.24.8.2  nathanw 	int s, error = 0;
    617  1.24.8.2  nathanw 
    618  1.24.8.2  nathanw 	s = splnet();
    619  1.24.8.2  nathanw 
    620  1.24.8.4  nathanw 	DPRINTF(("%s: mb8795_ioctl()\n",sc->sc_dev.dv_xname));
    621  1.24.8.4  nathanw 
    622  1.24.8.2  nathanw 	switch (cmd) {
    623  1.24.8.2  nathanw 
    624  1.24.8.2  nathanw 	case SIOCSIFADDR:
    625  1.24.8.4  nathanw 		DPRINTF(("%s: mb8795_ioctl() SIOCSIFADDR\n",sc->sc_dev.dv_xname));
    626  1.24.8.2  nathanw 		ifp->if_flags |= IFF_UP;
    627  1.24.8.2  nathanw 
    628  1.24.8.2  nathanw 		switch (ifa->ifa_addr->sa_family) {
    629  1.24.8.2  nathanw #ifdef INET
    630  1.24.8.2  nathanw 		case AF_INET:
    631  1.24.8.2  nathanw 			mb8795_init(sc);
    632  1.24.8.2  nathanw 			arp_ifinit(ifp, ifa);
    633  1.24.8.2  nathanw 			break;
    634  1.24.8.2  nathanw #endif
    635  1.24.8.2  nathanw #ifdef NS
    636  1.24.8.2  nathanw 		case AF_NS:
    637  1.24.8.2  nathanw 		    {
    638  1.24.8.2  nathanw 			register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
    639  1.24.8.2  nathanw 
    640  1.24.8.2  nathanw 			if (ns_nullhost(*ina))
    641  1.24.8.2  nathanw 				ina->x_host =
    642  1.24.8.2  nathanw 				    *(union ns_host *)LLADDR(ifp->if_sadl);
    643  1.24.8.2  nathanw 			else {
    644  1.24.8.2  nathanw 				bcopy(ina->x_host.c_host,
    645  1.24.8.2  nathanw 				    LLADDR(ifp->if_sadl),
    646  1.24.8.2  nathanw 				    sizeof(sc->sc_enaddr));
    647  1.24.8.2  nathanw 			}
    648  1.24.8.2  nathanw 			/* Set new address. */
    649  1.24.8.2  nathanw 			mb8795_init(sc);
    650  1.24.8.2  nathanw 			break;
    651  1.24.8.2  nathanw 		    }
    652  1.24.8.2  nathanw #endif
    653  1.24.8.2  nathanw 		default:
    654  1.24.8.2  nathanw 			mb8795_init(sc);
    655  1.24.8.2  nathanw 			break;
    656  1.24.8.2  nathanw 		}
    657  1.24.8.2  nathanw 		break;
    658  1.24.8.2  nathanw 
    659  1.24.8.2  nathanw #if defined(CCITT) && defined(LLC)
    660  1.24.8.2  nathanw 	case SIOCSIFCONF_X25:
    661  1.24.8.2  nathanw 		ifp->if_flags |= IFF_UP;
    662  1.24.8.2  nathanw 		ifa->ifa_rtrequest = cons_rtrequest; /* XXX */
    663  1.24.8.2  nathanw 		error = x25_llcglue(PRC_IFUP, ifa->ifa_addr);
    664  1.24.8.2  nathanw 		if (error == 0)
    665  1.24.8.2  nathanw 			mb8795_init(sc);
    666  1.24.8.2  nathanw 		break;
    667  1.24.8.2  nathanw #endif /* CCITT && LLC */
    668  1.24.8.2  nathanw 
    669  1.24.8.2  nathanw 	case SIOCSIFFLAGS:
    670  1.24.8.4  nathanw 		DPRINTF(("%s: mb8795_ioctl() SIOCSIFFLAGS\n",sc->sc_dev.dv_xname));
    671  1.24.8.2  nathanw 		if ((ifp->if_flags & IFF_UP) == 0 &&
    672  1.24.8.2  nathanw 		    (ifp->if_flags & IFF_RUNNING) != 0) {
    673  1.24.8.2  nathanw 			/*
    674  1.24.8.2  nathanw 			 * If interface is marked down and it is running, then
    675  1.24.8.2  nathanw 			 * stop it.
    676  1.24.8.2  nathanw 			 */
    677  1.24.8.3  nathanw /* 			ifp->if_flags &= ~IFF_RUNNING; */
    678  1.24.8.3  nathanw 			mb8795_reset(sc);
    679  1.24.8.2  nathanw 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
    680  1.24.8.2  nathanw 		    	   (ifp->if_flags & IFF_RUNNING) == 0) {
    681  1.24.8.2  nathanw 			/*
    682  1.24.8.2  nathanw 			 * If interface is marked up and it is stopped, then
    683  1.24.8.2  nathanw 			 * start it.
    684  1.24.8.2  nathanw 			 */
    685  1.24.8.2  nathanw 			mb8795_init(sc);
    686  1.24.8.2  nathanw 		} else {
    687  1.24.8.2  nathanw 			/*
    688  1.24.8.2  nathanw 			 * Reset the interface to pick up changes in any other
    689  1.24.8.2  nathanw 			 * flags that affect hardware registers.
    690  1.24.8.2  nathanw 			 */
    691  1.24.8.2  nathanw 			mb8795_init(sc);
    692  1.24.8.2  nathanw 		}
    693  1.24.8.4  nathanw #ifdef MB8795_DEBUG
    694  1.24.8.2  nathanw 		if (ifp->if_flags & IFF_DEBUG)
    695  1.24.8.2  nathanw 			sc->sc_debug = 1;
    696  1.24.8.2  nathanw 		else
    697  1.24.8.2  nathanw 			sc->sc_debug = 0;
    698  1.24.8.2  nathanw #endif
    699  1.24.8.2  nathanw 		break;
    700  1.24.8.2  nathanw 
    701  1.24.8.2  nathanw 	case SIOCADDMULTI:
    702  1.24.8.2  nathanw 	case SIOCDELMULTI:
    703  1.24.8.4  nathanw 		DPRINTF(("%s: mb8795_ioctl() SIOCADDMULTI\n",sc->sc_dev.dv_xname));
    704  1.24.8.2  nathanw 		error = (cmd == SIOCADDMULTI) ?
    705  1.24.8.2  nathanw 		    ether_addmulti(ifr, &sc->sc_ethercom) :
    706  1.24.8.2  nathanw 		    ether_delmulti(ifr, &sc->sc_ethercom);
    707  1.24.8.2  nathanw 
    708  1.24.8.2  nathanw 		if (error == ENETRESET) {
    709  1.24.8.2  nathanw 			/*
    710  1.24.8.2  nathanw 			 * Multicast list has changed; set the hardware filter
    711  1.24.8.2  nathanw 			 * accordingly.
    712  1.24.8.2  nathanw 			 */
    713  1.24.8.3  nathanw 			mb8795_init(sc);
    714  1.24.8.2  nathanw 			error = 0;
    715  1.24.8.2  nathanw 		}
    716  1.24.8.2  nathanw 		break;
    717  1.24.8.2  nathanw 
    718  1.24.8.2  nathanw 	case SIOCGIFMEDIA:
    719  1.24.8.2  nathanw 	case SIOCSIFMEDIA:
    720  1.24.8.4  nathanw 		DPRINTF(("%s: mb8795_ioctl() SIOCSIFMEDIA\n",sc->sc_dev.dv_xname));
    721  1.24.8.2  nathanw 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
    722  1.24.8.2  nathanw 		break;
    723  1.24.8.2  nathanw 
    724  1.24.8.2  nathanw 	default:
    725  1.24.8.2  nathanw 		error = EINVAL;
    726  1.24.8.2  nathanw 		break;
    727  1.24.8.2  nathanw 	}
    728  1.24.8.2  nathanw 
    729  1.24.8.2  nathanw 	splx(s);
    730  1.24.8.2  nathanw 
    731  1.24.8.2  nathanw #if 0
    732  1.24.8.2  nathanw 	DPRINTF(("DEBUG: mb8795_ioctl(0x%lx) returning %d\n",
    733  1.24.8.2  nathanw 			cmd,error));
    734  1.24.8.2  nathanw #endif
    735  1.24.8.2  nathanw 
    736  1.24.8.2  nathanw 	return (error);
    737  1.24.8.2  nathanw }
    738  1.24.8.2  nathanw 
    739  1.24.8.2  nathanw /*
    740  1.24.8.2  nathanw  * Setup output on interface.
    741  1.24.8.2  nathanw  * Get another datagram to send off of the interface queue, and map it to the
    742  1.24.8.2  nathanw  * interface before starting the output.
    743  1.24.8.2  nathanw  * Called only at splnet or interrupt level.
    744  1.24.8.2  nathanw  */
    745  1.24.8.2  nathanw void
    746  1.24.8.2  nathanw mb8795_start(ifp)
    747  1.24.8.2  nathanw 	struct ifnet *ifp;
    748  1.24.8.2  nathanw {
    749  1.24.8.2  nathanw 	struct mb8795_softc *sc = ifp->if_softc;
    750  1.24.8.2  nathanw 	struct mbuf *m;
    751  1.24.8.2  nathanw 	int s;
    752  1.24.8.2  nathanw 
    753  1.24.8.2  nathanw 	DPRINTF(("%s: mb8795_start()\n",sc->sc_dev.dv_xname));
    754  1.24.8.2  nathanw 
    755  1.24.8.2  nathanw #ifdef DIAGNOSTIC
    756  1.24.8.2  nathanw 	IFQ_POLL(&ifp->if_snd, m);
    757  1.24.8.2  nathanw 	if (m == 0) {
    758  1.24.8.2  nathanw 		panic("%s: No packet to start\n",
    759  1.24.8.2  nathanw 		      sc->sc_dev.dv_xname);
    760  1.24.8.2  nathanw 	}
    761  1.24.8.2  nathanw #endif
    762  1.24.8.2  nathanw 
    763  1.24.8.2  nathanw 	while (1) {
    764  1.24.8.2  nathanw 		if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
    765  1.24.8.2  nathanw 			return;
    766  1.24.8.2  nathanw 
    767  1.24.8.2  nathanw #if 0
    768  1.24.8.2  nathanw 		return;	/* @@@ Turn off xmit for debugging */
    769  1.24.8.2  nathanw #endif
    770  1.24.8.2  nathanw 
    771  1.24.8.2  nathanw 		ifp->if_flags |= IFF_OACTIVE;
    772  1.24.8.2  nathanw 
    773  1.24.8.2  nathanw 		IFQ_DEQUEUE(&ifp->if_snd, m);
    774  1.24.8.2  nathanw 		if (m == 0) {
    775  1.24.8.2  nathanw 			ifp->if_flags &= ~IFF_OACTIVE;
    776  1.24.8.2  nathanw 			return;
    777  1.24.8.2  nathanw 		}
    778  1.24.8.2  nathanw 
    779  1.24.8.2  nathanw #if NBPFILTER > 0
    780  1.24.8.2  nathanw 		/*
    781  1.24.8.2  nathanw 		 * Pass packet to bpf if there is a listener.
    782  1.24.8.2  nathanw 		 */
    783  1.24.8.2  nathanw 		if (ifp->if_bpf)
    784  1.24.8.2  nathanw 			bpf_mtap(ifp->if_bpf, m);
    785  1.24.8.2  nathanw #endif
    786  1.24.8.2  nathanw 
    787  1.24.8.2  nathanw 		s = spldma();
    788  1.24.8.2  nathanw 		IF_ENQUEUE(&sc->sc_tx_snd, m);
    789  1.24.8.4  nathanw 		if (!MBDMA_TX_ISACTIVE(sc))
    790  1.24.8.4  nathanw 			mb8795_start_dma(sc);
    791  1.24.8.2  nathanw 		splx(s);
    792  1.24.8.2  nathanw 
    793  1.24.8.2  nathanw 		ifp->if_flags &= ~IFF_OACTIVE;
    794  1.24.8.2  nathanw 	}
    795  1.24.8.2  nathanw 
    796  1.24.8.2  nathanw }
    797  1.24.8.2  nathanw 
    798  1.24.8.2  nathanw void
    799  1.24.8.4  nathanw mb8795_start_dma(sc)
    800  1.24.8.4  nathanw 	struct mb8795_softc *sc;
    801  1.24.8.2  nathanw {
    802  1.24.8.4  nathanw 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    803  1.24.8.4  nathanw 	struct mbuf *m;
    804  1.24.8.4  nathanw 	u_char txmask;
    805  1.24.8.2  nathanw 
    806  1.24.8.2  nathanw 	DPRINTF(("%s: mb8795_start_dma()\n",sc->sc_dev.dv_xname));
    807  1.24.8.2  nathanw 
    808  1.24.8.2  nathanw #if (defined(DIAGNOSTIC))
    809  1.24.8.2  nathanw 	{
    810  1.24.8.2  nathanw 		u_char txstat;
    811  1.24.8.4  nathanw 		txstat = MB_READ_REG(sc, MB8795_TXSTAT);
    812  1.24.8.4  nathanw 		if (!turbo && !(txstat & MB8795_TXSTAT_READY)) {
    813  1.24.8.2  nathanw 			/* @@@ I used to panic here, but then it paniced once.
    814  1.24.8.2  nathanw 			 * Let's see if I can just reset instead. [ dbj 980706.1900 ]
    815  1.24.8.2  nathanw 			 */
    816  1.24.8.2  nathanw 			printf("%s: transmitter not ready\n",
    817  1.24.8.2  nathanw 				sc->sc_dev.dv_xname);
    818  1.24.8.3  nathanw 			ifp->if_flags &= ~IFF_RUNNING;
    819  1.24.8.3  nathanw 			mb8795_init(sc);
    820  1.24.8.2  nathanw 			return;
    821  1.24.8.2  nathanw 		}
    822  1.24.8.2  nathanw 	}
    823  1.24.8.2  nathanw #endif
    824  1.24.8.2  nathanw 
    825  1.24.8.2  nathanw #if 0
    826  1.24.8.2  nathanw 	return;	/* @@@ Turn off xmit for debugging */
    827  1.24.8.2  nathanw #endif
    828  1.24.8.2  nathanw 
    829  1.24.8.4  nathanw 	IF_DEQUEUE(&sc->sc_tx_snd, m);
    830  1.24.8.4  nathanw 	if (m == 0) {
    831  1.24.8.2  nathanw #ifdef DIAGNOSTIC
    832  1.24.8.2  nathanw 		panic("%s: No packet to start_dma\n",
    833  1.24.8.2  nathanw 		      sc->sc_dev.dv_xname);
    834  1.24.8.2  nathanw #endif
    835  1.24.8.2  nathanw 		return;
    836  1.24.8.2  nathanw 	}
    837  1.24.8.2  nathanw 
    838  1.24.8.4  nathanw 	MB_WRITE_REG(sc, MB8795_TXSTAT, MB8795_TXSTAT_CLEAR);
    839  1.24.8.4  nathanw 	txmask = MB_READ_REG(sc, MB8795_TXMASK);
    840  1.24.8.4  nathanw 	/* MB_WRITE_REG(sc, MB8795_TXMASK, txmask | MB8795_TXMASK_READYIE); */
    841  1.24.8.4  nathanw 	/* MB_WRITE_REG(sc, MB8795_TXMASK, txmask | MB8795_TXMASK_TXRXIE); */
    842  1.24.8.2  nathanw 
    843  1.24.8.2  nathanw 	ifp->if_timer = 5;
    844  1.24.8.2  nathanw 
    845  1.24.8.4  nathanw 	if (MBDMA_TX_MBUF(sc, m))
    846  1.24.8.2  nathanw 		return;
    847  1.24.8.2  nathanw 
    848  1.24.8.4  nathanw 	MBDMA_TX_GO(sc);
    849  1.24.8.4  nathanw 	if (turbo)
    850  1.24.8.4  nathanw 		MB_WRITE_REG(sc, MB8795_TXMODE, MB8795_TXMODE_TURBO1 | MB8795_TXMODE_TURBOSTART);
    851  1.24.8.2  nathanw 
    852  1.24.8.2  nathanw 	ifp->if_opackets++;
    853  1.24.8.2  nathanw }
    854  1.24.8.2  nathanw 
    855  1.24.8.2  nathanw /****************************************************************/
    856