Home | History | Annotate | Line # | Download | only in dev
mb8795.c revision 1.2
      1 /*	$NetBSD: mb8795.c,v 1.2 1998/07/05 00:51:12 jonathan Exp $	*/
      2 /*
      3  * Copyright (c) 1998 Darrin B. Jewell
      4  * All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *      This product includes software developed by Darrin B. Jewell
     17  * 4. The name of the author may not be used to endorse or promote products
     18  *    derived from this software without specific prior written permission
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include "opt_inet.h"
     33 #include "bpfilter.h"
     34 #include "rnd.h"
     35 
     36 #include <sys/param.h>
     37 #include <sys/systm.h>
     38 #include <sys/mbuf.h>
     39 #include <sys/syslog.h>
     40 #include <sys/socket.h>
     41 #include <sys/device.h>
     42 #include <sys/malloc.h>
     43 #include <sys/ioctl.h>
     44 #include <sys/errno.h>
     45 #if NRND > 0
     46 #include <sys/rnd.h>
     47 #endif
     48 
     49 #include <net/if.h>
     50 #include <net/if_dl.h>
     51 #include <net/if_ether.h>
     52 
     53 #if 0
     54 #include <net/if_media.h>
     55 #endif
     56 
     57 #ifdef INET
     58 #include <netinet/in.h>
     59 #include <netinet/if_inarp.h>
     60 #include <netinet/in_systm.h>
     61 #include <netinet/in_var.h>
     62 #include <netinet/ip.h>
     63 #endif
     64 
     65 #ifdef NS
     66 #include <netns/ns.h>
     67 #include <netns/ns_if.h>
     68 #endif
     69 
     70 #if defined(CCITT) && defined(LLC)
     71 #include <sys/socketvar.h>
     72 #include <netccitt/x25.h>
     73 #include <netccitt/pk.h>
     74 #include <netccitt/pk_var.h>
     75 #include <netccitt/pk_extern.h>
     76 #endif
     77 
     78 #if NBPFILTER > 0
     79 #include <net/bpf.h>
     80 #include <net/bpfdesc.h>
     81 #endif
     82 
     83 #include <machine/cpu.h>
     84 #include <machine/bus.h>
     85 #include <machine/intr.h>
     86 
     87 /* @@@ this is here for the REALIGN_DMABUF hack below */
     88 #include "nextdmareg.h"
     89 #include "nextdmavar.h"
     90 
     91 #include "mb8795reg.h"
     92 #include "mb8795var.h"
     93 
     94 #if 0
     95 #define XE_DEBUG
     96 #endif
     97 
     98 #ifdef XE_DEBUG
     99 #define DPRINTF(x) printf x;
    100 #else
    101 #define DPRINTF(x)
    102 #endif
    103 
    104 
    105 /*
    106  * Support for
    107  * Fujitsu Ethernet Data Link Controller (MB8795)
    108  * and the Fujitsu Manchester Encoder/Decoder (MB502).
    109  */
    110 
    111 int debugipkt = 0;
    112 
    113 
    114 void mb8795_shutdown __P((void *));
    115 
    116 #if 0
    117 int mb8795_mediachange __P((struct ifnet *));
    118 void mb8795_mediastatus __P((struct ifnet *, struct ifmediareq *));
    119 #endif
    120 
    121 struct mbuf * mb8795_rxdmamap_load __P((struct mb8795_softc *,
    122 		bus_dmamap_t map));
    123 
    124 bus_dmamap_t mb8795_rxdma_continue __P((void *));
    125 void mb8795_rxdma_completed __P((bus_dmamap_t,void *));
    126 bus_dmamap_t mb8795_txdma_continue __P((void *));
    127 void mb8795_txdma_completed __P((bus_dmamap_t,void *));
    128 void mb8795_rxdma_shutdown __P((void *));
    129 void mb8795_txdma_shutdown __P((void *));
    130 bus_dmamap_t mb8795_txdma_restart __P((bus_dmamap_t,void *));
    131 
    132 void
    133 mb8795_config(sc)
    134      struct mb8795_softc *sc;
    135 {
    136   struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    137 
    138 	DPRINTF(("%s: mb8795_config()\n",sc->sc_dev.dv_xname));
    139 
    140   /* Initialize ifnet structure. */
    141   bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
    142   ifp->if_softc = sc;
    143   ifp->if_start = mb8795_start;
    144   ifp->if_ioctl = mb8795_ioctl;
    145   ifp->if_watchdog = mb8795_watchdog;
    146   ifp->if_flags =
    147     IFF_BROADCAST | IFF_NOTRAILERS;
    148 
    149 #if 0
    150   /* Initialize ifmedia structures. */
    151   ifmedia_init(&sc->sc_media, 0, mb8795_mediachange, mb8795_mediastatus);
    152   if (sc->sc_supmedia != NULL) {
    153     int i;
    154     for (i = 0; i < sc->sc_nsupmedia; i++)
    155       ifmedia_add(&sc->sc_media, sc->sc_supmedia[i],
    156                   0, NULL);
    157     ifmedia_set(&sc->sc_media, sc->sc_defaultmedia);
    158   } else {
    159     ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
    160     ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
    161   }
    162 #endif
    163 
    164   /* Attach the interface. */
    165   if_attach(ifp);
    166   ether_ifattach(ifp, sc->sc_enaddr);
    167 
    168 	/* decrease the mtu on this interface to deal with
    169 	 * alignment problems
    170 	 */
    171 	ifp->if_mtu -= 16;
    172 
    173 #if NBPFILTER > 0
    174   bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
    175 #endif
    176 
    177   sc->sc_sh = shutdownhook_establish(mb8795_shutdown, sc);
    178   if (sc->sc_sh == NULL)
    179     panic("mb8795_config: can't establish shutdownhook");
    180 
    181 #if NRND > 0
    182   rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
    183                     RND_TYPE_NET);
    184 #endif
    185 
    186   /* Initialize the dma maps */
    187   {
    188     int error;
    189     if ((error = bus_dmamap_create(sc->sc_tx_dmat, MCLBYTES,
    190 		    (MCLBYTES/MSIZE), MCLBYTES, 0, BUS_DMA_ALLOCNOW,
    191 				&sc->sc_tx_dmamap)) != 0) {
    192       panic("%s: can't create tx DMA map, error = %d\n",
    193 					sc->sc_dev.dv_xname, error);
    194     }
    195 		{
    196 			int i;
    197 			for(i=0;i<MB8795_NRXBUFS;i++) {
    198 				if ((error = bus_dmamap_create(sc->sc_rx_dmat, MCLBYTES,
    199 						(MCLBYTES/MSIZE), MCLBYTES, 0, BUS_DMA_ALLOCNOW,
    200 						&sc->sc_rx_dmamap[i])) != 0) {
    201 					panic("%s: can't create rx DMA map, error = %d\n",
    202 							sc->sc_dev.dv_xname, error);
    203 				}
    204 				sc->sc_rx_mb_head[i] = NULL;
    205 			}
    206 			sc->sc_rx_loaded_idx = 0;
    207 			sc->sc_rx_completed_idx = 0;
    208 			sc->sc_rx_handled_idx = 0;
    209     }
    210   }
    211 
    212 	/* @@@ more next hacks
    213 	 * the  2000 covers at least a 1500 mtu + headers
    214 	 * + DMA_BEGINALIGNMENT+ ENDMA_ENDALIGNMENT
    215 	 */
    216 	sc->sc_txbuf = malloc(2000, M_DEVBUF, M_NOWAIT);
    217 	if (!sc->sc_txbuf) panic("%s: can't malloc tx DMA buffer",
    218 			sc->sc_dev.dv_xname);
    219 
    220 	sc->sc_tx_mb_head = NULL;
    221 	sc->sc_tx_loaded = 0;
    222 
    223 	sc->sc_tx_nd->nd_chaining_flag = 0;
    224 	sc->sc_tx_nd->nd_shutdown_cb = mb8795_txdma_shutdown;
    225 	sc->sc_tx_nd->nd_continue_cb = mb8795_txdma_continue;
    226 	sc->sc_tx_nd->nd_completed_cb = mb8795_txdma_completed;
    227 	sc->sc_tx_nd->nd_cb_arg = sc;
    228 
    229 	sc->sc_rx_nd->nd_chaining_flag = 1;
    230 	sc->sc_rx_nd->nd_shutdown_cb = mb8795_rxdma_shutdown;
    231 	sc->sc_rx_nd->nd_continue_cb = mb8795_rxdma_continue;
    232 	sc->sc_rx_nd->nd_completed_cb = mb8795_rxdma_completed;
    233 	sc->sc_rx_nd->nd_cb_arg = sc;
    234 
    235 	DPRINTF(("%s: leaving mb8795_config()\n",sc->sc_dev.dv_xname));
    236 }
    237 
    238 
    239 /****************************************************************/
    240 
    241 #define XCHR(x) "0123456789abcdef"[(x) & 0xf]
    242 static void
    243 hex_dump(unsigned char *pkt, size_t len)
    244 {
    245 	size_t i, j;
    246 
    247 	printf("0000: ");
    248 	for(i=0; i<len; i++) {
    249 		printf("%c%c ", XCHR(pkt[i]>>4), XCHR(pkt[i]));
    250 		if ((i+1) % 16 == 0) {
    251 			printf("  %c", '"');
    252 			for(j=0; j<16; j++)
    253 				printf("%c", pkt[i-15+j]>=32 && pkt[i-15+j]<127?pkt[i-15+j]:'.');
    254 			printf("%c\n%c%c%c%c: ", '"', XCHR((i+1)>>12),
    255 				XCHR((i+1)>>8), XCHR((i+1)>>4), XCHR(i+1));
    256 		}
    257 	}
    258 	printf("\n");
    259 }
    260 
    261 
    262 /*
    263  * Controller receive interrupt.
    264  */
    265 void
    266 mb8795_rint(sc)
    267      struct mb8795_softc *sc;
    268 {
    269 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    270 	int error = 0;
    271 	u_char rxstat;
    272 	u_char rxmask;
    273 
    274 	rxstat = bus_space_read_1(sc->sc_bst,sc->sc_bsh, XE_RXSTAT);
    275 	rxmask = bus_space_read_1(sc->sc_bst,sc->sc_bsh, XE_RXMASK);
    276 
    277 	bus_space_write_1(sc->sc_bst,sc->sc_bsh, XE_RXSTAT, XE_RXSTAT_CLEAR);
    278 
    279 #if 0
    280 	DPRINTF(("%s: rx interrupt, rxstat = %b\n",
    281 			sc->sc_dev.dv_xname, rxstat, XE_RXSTAT_BITS));
    282 #endif
    283 
    284 	if (rxstat & XE_RXSTAT_RESET) {
    285 		DPRINTF(("%s: rx reset packet\n",
    286 				sc->sc_dev.dv_xname));
    287 		error++;
    288 	}
    289 	if (rxstat & XE_RXSTAT_SHORT) {
    290 		DPRINTF(("%s: rx short packet\n",
    291 				sc->sc_dev.dv_xname));
    292 		error++;
    293 	}
    294 	if (rxstat & XE_RXSTAT_ALIGNERR) {
    295 		DPRINTF(("%s: rx alignment error\n",
    296 				sc->sc_dev.dv_xname));
    297 		error++;
    298 	}
    299 	if (rxstat & XE_RXSTAT_CRCERR) {
    300 		DPRINTF(("%s: rx CRC error\n",
    301 				sc->sc_dev.dv_xname));
    302 		error++;
    303 	}
    304 	if (rxstat & XE_RXSTAT_OVERFLOW) {
    305 		DPRINTF(("%s: rx overflow error\n",
    306 				sc->sc_dev.dv_xname));
    307 		error++;
    308 	}
    309 
    310 	if (error) {
    311 		ifp->if_ierrors++;
    312 		/* @@@ handle more gracefully, free memory, etc. */
    313 	}
    314 
    315 	if (rxstat & XE_RXSTAT_OK) {
    316 		int s;
    317 		s = spldma();
    318 
    319 		while(sc->sc_rx_handled_idx != sc->sc_rx_completed_idx) {
    320 			struct mbuf *m;
    321 			bus_dmamap_t map;
    322 
    323 			sc->sc_rx_handled_idx++;
    324 			sc->sc_rx_handled_idx %= MB8795_NRXBUFS;
    325 
    326 			/* Should probably not do this much while interrupts
    327 			 * are disabled, but for now we will.
    328 			 */
    329 
    330 			map = sc->sc_rx_dmamap[sc->sc_rx_handled_idx];
    331 			m = sc->sc_rx_mb_head[sc->sc_rx_handled_idx];
    332 
    333 			bus_dmamap_sync(sc->sc_rx_dmat, map,
    334 					0, map->dm_mapsize, BUS_DMASYNC_POSTREAD);
    335 
    336 
    337 			/* Find receive length and chop off CRC */
    338 			/* @@@ assumes packet is all in first segment
    339 			 * also assumes segment length is length of packet.
    340 			 * see comment in nextdma.c nextdma_intr();
    341 			 */
    342 			m->m_pkthdr.len = map->dm_segs[0].ds_len-4;
    343 			m->m_len = map->dm_segs[0].ds_len-4;
    344 			m->m_pkthdr.rcvif = ifp;
    345 
    346 			bus_dmamap_unload(sc->sc_rx_dmat, map);
    347 
    348 			/* Install a fresh mbuf for next packet */
    349 
    350 			sc->sc_rx_mb_head[sc->sc_rx_handled_idx] =
    351 					mb8795_rxdmamap_load(sc,map);
    352 
    353 			/* enable interrupts while we process the packet */
    354 			splx(s);
    355 
    356 #if defined(XE_DEBUG)
    357 			/* Peek at the packet */
    358 			DPRINTF(("%s: received packet, at VA 0x%08x-0x%08x,len %d\n",
    359 					sc->sc_dev.dv_xname,mtod(m,u_char *),mtod(m,u_char *)+m->m_len,m->m_len));
    360 #if 0
    361 			hex_dump(mtod(m,u_char *), m->m_pkthdr.len < 255 ? m->m_pkthdr.len : 128 );
    362 #endif
    363 #endif
    364 
    365 			{
    366 				struct ether_header *eh;
    367 
    368 				ifp->if_ipackets++;
    369 				debugipkt++;
    370 
    371 				/* We assume that the header fit entirely in one mbuf. */
    372 				eh = mtod(m, struct ether_header *);
    373 
    374 				/* Pass the packet up, with the ether header sort-of removed. */
    375 				m_adj(m, sizeof(struct ether_header));
    376 				ether_input(ifp, eh, m);
    377 			}
    378 
    379 			s = spldma();
    380 
    381 		}
    382 
    383 		splx(s);
    384 
    385 	}
    386 
    387 	DPRINTF(("%s: rx interrupt, rxstat = %b\n",
    388 			sc->sc_dev.dv_xname, rxstat, XE_RXSTAT_BITS));
    389 
    390 #if 0 && defined(XE_DEBUG)
    391 	{
    392 		DPRINTF(("rxstat = 0x%b\n",
    393 				bus_space_read_1(sc->sc_bst,sc->sc_bsh, XE_RXSTAT), XE_RXSTAT_BITS));
    394 		DPRINTF(("rxmask = 0x%b\n",
    395 				bus_space_read_1(sc->sc_bst,sc->sc_bsh, XE_RXMASK), XE_RXMASK_BITS));
    396 		DPRINTF(("rxmode = 0x%b\n",
    397 				bus_space_read_1(sc->sc_bst,sc->sc_bsh, XE_RXMODE), XE_RXMODE_BITS));
    398 	}
    399 #endif
    400 
    401 	return;
    402 }
    403 
    404 /*
    405  * Controller transmit interrupt.
    406  */
    407 void
    408 mb8795_tint(sc)
    409      struct mb8795_softc *sc;
    410 
    411 {
    412 	int reset = 0;
    413 	u_char txstat;
    414 	u_char txmask;
    415 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    416 
    417 	txstat = bus_space_read_1(sc->sc_bst,sc->sc_bsh, XE_TXSTAT);
    418 	txmask = bus_space_read_1(sc->sc_bst,sc->sc_bsh, XE_TXMASK);
    419 
    420 #if 0
    421 	DPRINTF(("%s: tx interrupt, txstat = %b\n",
    422 			sc->sc_dev.dv_xname, txstat, XE_TXSTAT_BITS));
    423 #endif
    424 
    425 	if (txstat & XE_TXSTAT_SHORTED) {
    426 		printf("%s: tx cable shorted\n", sc->sc_dev.dv_xname);
    427 		ifp->if_oerrors++;
    428 	}
    429 	if (txstat & XE_TXSTAT_UNDERFLOW) {
    430 		printf("%s: tx underflow\n", sc->sc_dev.dv_xname);
    431 		ifp->if_oerrors++;
    432 	}
    433 	if (txstat & XE_TXSTAT_COLLERR) {
    434 		DPRINTF(("%s: tx collision\n", sc->sc_dev.dv_xname));
    435 		ifp->if_collisions++;
    436 	}
    437 	if (txstat & XE_TXSTAT_COLLERR16) {
    438 		printf("%s: tx 16th collision\n", sc->sc_dev.dv_xname);
    439 		ifp->if_oerrors++;
    440 		ifp->if_collisions += 16;
    441 	}
    442 
    443 	if (reset) {
    444 		mb8795_reset(sc);
    445 		return;
    446 	}
    447 
    448 #if 0
    449 	if (txstat & XE_TXSTAT_READY) {
    450 
    451 		panic("%s: unexpected tx interrupt %b",
    452 				sc->sc_dev.dv_xname,txstat,XE_TXSTAT_BITS);
    453 
    454 		/* turn interrupt off */
    455 		bus_space_write_1(sc->sc_bst,sc->sc_bsh, XE_TXMASK,
    456 				txmask & ~XE_TXMASK_READYIE);
    457 	}
    458 #endif
    459 
    460   return;
    461 }
    462 
    463 /****************************************************************/
    464 
    465 void
    466 mb8795_reset(sc)
    467 	struct mb8795_softc *sc;
    468 {
    469 	int s;
    470 
    471 	s = splimp();
    472 	mb8795_init(sc);
    473 	splx(s);
    474 }
    475 
    476 void
    477 mb8795_watchdog(ifp)
    478 	struct ifnet *ifp;
    479 {
    480 	struct mb8795_softc *sc = ifp->if_softc;
    481 
    482 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
    483 	++ifp->if_oerrors;
    484 
    485 	DPRINTF(("%s: %d input errors, %d input packets\n",
    486 			sc->sc_dev.dv_xname, ifp->if_ierrors, ifp->if_ipackets));
    487 
    488 	mb8795_reset(sc);
    489 }
    490 
    491 /*
    492  * Initialization of interface; set up initialization block
    493  * and transmit/receive descriptor rings.
    494  * @@@ error handling is bogus in here. memory leaks
    495  */
    496 void
    497 mb8795_init(sc)
    498      struct mb8795_softc *sc;
    499 {
    500   struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    501 
    502 	m_freem(sc->sc_tx_mb_head);
    503 	sc->sc_tx_mb_head = NULL;
    504 	sc->sc_tx_loaded = 0;
    505 
    506 	{
    507 		int i;
    508 		for(i=0;i<MB8795_NRXBUFS;i++) {
    509 			if (sc->sc_rx_mb_head[i]) {
    510 				bus_dmamap_unload(sc->sc_rx_dmat, sc->sc_rx_dmamap[i]);
    511 				m_freem(sc->sc_rx_mb_head[i]);
    512 			}
    513 			sc->sc_rx_mb_head[i] =
    514 					mb8795_rxdmamap_load(sc, sc->sc_rx_dmamap[i]);
    515 		}
    516 		sc->sc_rx_loaded_idx = 0;
    517 		sc->sc_rx_completed_idx = 0;
    518 		sc->sc_rx_handled_idx = 0;
    519 	}
    520 
    521   bus_space_write_1(sc->sc_bst,sc->sc_bsh, XE_RESET,  XE_RESET_MODE);
    522 
    523   bus_space_write_1(sc->sc_bst,sc->sc_bsh, XE_TXMODE, XE_TXMODE_LB_DISABLE);
    524 #if 0 /* This interrupt was sometimes failing to ack correctly
    525 			 * causing a loop @@@
    526 			 */
    527   bus_space_write_1(sc->sc_bst,sc->sc_bsh, XE_TXMASK,
    528 			XE_TXMASK_UNDERFLOWIE | XE_TXMASK_COLLIE | XE_TXMASK_COLL16IE
    529 			| XE_TXMASK_PARERRIE);
    530 #else
    531   bus_space_write_1(sc->sc_bst,sc->sc_bsh, XE_TXMASK, 0);
    532 #endif
    533   bus_space_write_1(sc->sc_bst,sc->sc_bsh, XE_TXSTAT, XE_TXSTAT_CLEAR);
    534 
    535   bus_space_write_1(sc->sc_bst,sc->sc_bsh, XE_RXMODE, XE_RXMODE_NORMAL);
    536 
    537   bus_space_write_1(sc->sc_bst,sc->sc_bsh, XE_RXMASK,
    538 			XE_RXMASK_OKIE | XE_RXMASK_RESETIE | XE_RXMASK_SHORTIE	|
    539 			XE_RXMASK_ALIGNERRIE	|  XE_RXMASK_CRCERRIE | XE_RXMASK_OVERFLOWIE);
    540 
    541   bus_space_write_1(sc->sc_bst,sc->sc_bsh, XE_RXSTAT, XE_RXSTAT_CLEAR);
    542 
    543 	{
    544 		int i;
    545 		for(i=0;i<sizeof(sc->sc_enaddr);i++) {
    546 			bus_space_write_1(sc->sc_bst,sc->sc_bsh,XE_ENADDR+i,sc->sc_enaddr[i]);
    547 		}
    548 	}
    549 
    550 	DPRINTF(("%s: initializing ethernet %02x:%02x:%02x:%02x:%02x:%02x, size=%d\n",
    551 			sc->sc_dev.dv_xname,
    552 			sc->sc_enaddr[0],sc->sc_enaddr[1],sc->sc_enaddr[2],
    553 			sc->sc_enaddr[3],sc->sc_enaddr[4],sc->sc_enaddr[5],
    554 			sizeof(sc->sc_enaddr)));
    555 
    556   bus_space_write_1(sc->sc_bst,sc->sc_bsh, XE_RESET, 0);
    557 
    558   ifp->if_flags |= IFF_RUNNING;
    559   ifp->if_flags &= ~IFF_OACTIVE;
    560   ifp->if_timer = 0;
    561 
    562 	nextdma_init(sc->sc_tx_nd);
    563 	nextdma_init(sc->sc_rx_nd);
    564 
    565 	nextdma_start(sc->sc_rx_nd, DMACSR_READ);
    566 
    567 	if (ifp->if_snd.ifq_head != NULL) {
    568 		mb8795_start(ifp);
    569 	}
    570 }
    571 
    572 void
    573 mb8795_stop(sc)
    574 	struct mb8795_softc *sc;
    575 {
    576   printf("%s: stop not implemented\n", sc->sc_dev.dv_xname);
    577 }
    578 
    579 
    580 void
    581 mb8795_shutdown(arg)
    582 	void *arg;
    583 {
    584   struct mb8795_softc *sc = (struct mb8795_softc *)arg;
    585   mb8795_stop(sc);
    586 }
    587 
    588 /****************************************************************/
    589 int
    590 mb8795_ioctl(ifp, cmd, data)
    591 	register struct ifnet *ifp;
    592 	u_long cmd;
    593 	caddr_t data;
    594 {
    595 	register struct mb8795_softc *sc = ifp->if_softc;
    596 	struct ifaddr *ifa = (struct ifaddr *)data;
    597 	struct ifreq *ifr = (struct ifreq *)data;
    598 	int s, error = 0;
    599 
    600 	s = splimp();
    601 
    602 	switch (cmd) {
    603 
    604 	case SIOCSIFADDR:
    605 		ifp->if_flags |= IFF_UP;
    606 
    607 		switch (ifa->ifa_addr->sa_family) {
    608 #ifdef INET
    609 		case AF_INET:
    610 			mb8795_init(sc);
    611 			arp_ifinit(ifp, ifa);
    612 			break;
    613 #endif
    614 #ifdef NS
    615 		case AF_NS:
    616 		    {
    617 			register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
    618 
    619 			if (ns_nullhost(*ina))
    620 				ina->x_host =
    621 				    *(union ns_host *)LLADDR(ifp->if_sadl);
    622 			else {
    623 				bcopy(ina->x_host.c_host,
    624 				    LLADDR(ifp->if_sadl),
    625 				    sizeof(sc->sc_enaddr));
    626 			}
    627 			/* Set new address. */
    628 			mb8795_init(sc);
    629 			break;
    630 		    }
    631 #endif
    632 		default:
    633 			mb8795_init(sc);
    634 			break;
    635 		}
    636 		break;
    637 
    638 #if defined(CCITT) && defined(LLC)
    639 	case SIOCSIFCONF_X25:
    640 		ifp->if_flags |= IFF_UP;
    641 		ifa->ifa_rtrequest = cons_rtrequest; /* XXX */
    642 		error = x25_llcglue(PRC_IFUP, ifa->ifa_addr);
    643 		if (error == 0)
    644 			mb8795_init(sc);
    645 		break;
    646 #endif /* CCITT && LLC */
    647 
    648 	case SIOCSIFFLAGS:
    649 		if ((ifp->if_flags & IFF_UP) == 0 &&
    650 		    (ifp->if_flags & IFF_RUNNING) != 0) {
    651 			/*
    652 			 * If interface is marked down and it is running, then
    653 			 * stop it.
    654 			 */
    655 			mb8795_stop(sc);
    656 			ifp->if_flags &= ~IFF_RUNNING;
    657 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
    658 		    	   (ifp->if_flags & IFF_RUNNING) == 0) {
    659 			/*
    660 			 * If interface is marked up and it is stopped, then
    661 			 * start it.
    662 			 */
    663 			mb8795_init(sc);
    664 		} else {
    665 			/*
    666 			 * Reset the interface to pick up changes in any other
    667 			 * flags that affect hardware registers.
    668 			 */
    669 			/*mb8795_stop(sc);*/
    670 			mb8795_init(sc);
    671 		}
    672 #ifdef XE_DEBUG
    673 		if (ifp->if_flags & IFF_DEBUG)
    674 			sc->sc_debug = 1;
    675 		else
    676 			sc->sc_debug = 0;
    677 #endif
    678 		break;
    679 
    680 	case SIOCADDMULTI:
    681 	case SIOCDELMULTI:
    682 		error = (cmd == SIOCADDMULTI) ?
    683 		    ether_addmulti(ifr, &sc->sc_ethercom) :
    684 		    ether_delmulti(ifr, &sc->sc_ethercom);
    685 
    686 		if (error == ENETRESET) {
    687 			/*
    688 			 * Multicast list has changed; set the hardware filter
    689 			 * accordingly.
    690 			 */
    691 			mb8795_reset(sc);
    692 			error = 0;
    693 		}
    694 		break;
    695 
    696 #if 0
    697 	case SIOCGIFMEDIA:
    698 	case SIOCSIFMEDIA:
    699 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
    700 		break;
    701 #endif
    702 
    703 	default:
    704 		error = EINVAL;
    705 		break;
    706 	}
    707 
    708 	splx(s);
    709 
    710 #if 0
    711 	DPRINTF(("DEBUG: mb8795_ioctl(0x%lx) returning %d\n",
    712 			cmd,error));
    713 #endif
    714 
    715 	return (error);
    716 }
    717 
    718 /*
    719  * Setup output on interface.
    720  * Get another datagram to send off of the interface queue, and map it to the
    721  * interface before starting the output.
    722  * Called only at splimp or interrupt level.
    723  */
    724 void
    725 mb8795_start(ifp)
    726      struct ifnet *ifp;
    727 {
    728   int error;
    729   struct mb8795_softc *sc = ifp->if_softc;
    730 
    731 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
    732 		return;
    733 
    734   DPRINTF(("%s: mb8795_start()\n",sc->sc_dev.dv_xname));
    735 
    736 #if (defined(DIAGNOSTIC))
    737   {
    738     u_char txstat;
    739     txstat = bus_space_read_1(sc->sc_bst,sc->sc_bsh, XE_TXSTAT);
    740     if (!(txstat & XE_TXSTAT_READY)) {
    741       panic("%s: transmitter not ready\n", sc->sc_dev.dv_xname);
    742     }
    743   }
    744 #endif
    745 
    746 #if 0
    747 	return;	/* @@@ Turn off xmit for debugging */
    748 #endif
    749 
    750   bus_space_write_1(sc->sc_bst,sc->sc_bsh, XE_TXSTAT, XE_TXSTAT_CLEAR);
    751 
    752   IF_DEQUEUE(&ifp->if_snd, sc->sc_tx_mb_head);
    753   if (sc->sc_tx_mb_head == 0) {
    754     printf("%s: No packet to start\n",
    755 				sc->sc_dev.dv_xname);
    756     return;
    757   }
    758 
    759 	ifp->if_timer = 5;
    760 
    761 /* The following is a next specific hack that should
    762  * probably be moved out of MI code.
    763  * This macro assumes it can move forward as needed
    764  * in the buffer.  Perhaps it should zero the extra buffer.
    765  */
    766 #define REALIGN_DMABUF(s,l) \
    767 	{ (s) = ((u_char *)(((unsigned)(s)+DMA_BEGINALIGNMENT-1) \
    768 			&~(DMA_BEGINALIGNMENT-1))); \
    769     (l) = ((u_char *)(((unsigned)((s)+(l))+ENDMA_ENDALIGNMENT-1) \
    770 				&~(ENDMA_ENDALIGNMENT-1)))-(s);}
    771 
    772 #if 0
    773   error = bus_dmamap_load_mbuf(sc->sc_tx_dmat,
    774 			sc->sc_tx_dmamap,
    775 			sc->sc_tx_mb_head,
    776 			BUS_DMA_NOWAIT);
    777 #else
    778 	{
    779 		u_char *buf = sc->sc_txbuf;
    780 		int buflen = 0;
    781 		struct mbuf *m = sc->sc_tx_mb_head;
    782 		buflen = m->m_pkthdr.len;
    783 
    784 		/* Fix runt packets,  @@@ memory overrun */
    785 		if (buflen < ETHERMIN+sizeof(struct ether_header)) {
    786 			buflen = ETHERMIN+sizeof(struct ether_header);
    787 		}
    788 
    789 		buflen += 15;
    790 		REALIGN_DMABUF(buf,buflen);
    791 		if (buflen > 1520) {
    792 			panic("%s: packet too long\n",sc->sc_dev.dv_xname);
    793 		}
    794 
    795 		{
    796 			u_char *p = buf;
    797 			for (m=sc->sc_tx_mb_head; m; m = m->m_next) {
    798 				if (m->m_len == 0) continue;
    799 				bcopy(mtod(m, u_char *), p, m->m_len);
    800 				p += m->m_len;
    801 			}
    802 		}
    803 
    804 		error = bus_dmamap_load(sc->sc_tx_dmat, sc->sc_tx_dmamap,
    805 				buf,buflen,NULL,BUS_DMA_NOWAIT);
    806 	}
    807 #endif
    808   if (error) {
    809     printf("%s: can't load mbuf chain, error = %d\n",
    810 				sc->sc_dev.dv_xname, error);
    811     m_freem(sc->sc_tx_mb_head);
    812     sc->sc_tx_mb_head = NULL;
    813     return;
    814   }
    815 	sc->sc_tx_loaded++;
    816 
    817 #ifdef DIAGNOSTIC
    818 	if (sc->sc_tx_loaded != 1) {
    819 			panic("%s: sc->sc_tx_loaded is %d",sc->sc_dev.dv_xname,
    820 					sc->sc_tx_loaded);
    821 	}
    822 #endif
    823 
    824 	ifp->if_flags |= IFF_OACTIVE;
    825 
    826   bus_dmamap_sync(sc->sc_tx_dmat, sc->sc_tx_dmamap, 0,
    827 			sc->sc_tx_dmamap->dm_mapsize, BUS_DMASYNC_PREWRITE);
    828 
    829 	nextdma_start(sc->sc_tx_nd, DMACSR_WRITE);
    830 
    831 #if NBPFILTER > 0
    832   /*
    833    * Pass packet to bpf if there is a listener.
    834    */
    835   if (ifp->if_bpf)
    836     bpf_mtap(ifp->if_bpf, sc->sc_tx_mb_head);
    837 #endif
    838 
    839 }
    840 
    841 /****************************************************************/
    842 
    843 void
    844 mb8795_txdma_completed(map, arg)
    845 	bus_dmamap_t map;
    846 	void *arg;
    847 {
    848 	struct mb8795_softc *sc = arg;
    849 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    850 
    851   DPRINTF(("%s: mb8795_txdma_completed()\n",sc->sc_dev.dv_xname));
    852 
    853 #ifdef DIAGNOSTIC
    854 	if (!sc->sc_tx_loaded) {
    855 		panic("%s: tx completed never loaded ",sc->sc_dev.dv_xname);
    856 	}
    857 	if (map != sc->sc_tx_dmamap) {
    858 		panic("%s: unexpected tx completed map",sc->sc_dev.dv_xname);
    859 	}
    860 
    861 #endif
    862 }
    863 
    864 void
    865 mb8795_txdma_shutdown(arg)
    866 	void *arg;
    867 {
    868 	struct mb8795_softc *sc = arg;
    869 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    870 
    871   DPRINTF(("%s: mb8795_txdma_shutdown()\n",sc->sc_dev.dv_xname));
    872 
    873 #ifdef DIAGNOSTIC
    874 	if (!sc->sc_tx_loaded) {
    875 		panic("%s: tx shutdown never loaded ",sc->sc_dev.dv_xname);
    876 	}
    877 #endif
    878 
    879 	{
    880 
    881 		if (sc->sc_tx_loaded) {
    882 			bus_dmamap_sync(sc->sc_tx_dmat, sc->sc_tx_dmamap,
    883 					0, sc->sc_tx_dmamap->dm_mapsize,
    884 					BUS_DMASYNC_POSTWRITE);
    885 			bus_dmamap_unload(sc->sc_tx_dmat, sc->sc_tx_dmamap);
    886 			m_freem(sc->sc_tx_mb_head);
    887 			sc->sc_tx_mb_head = NULL;
    888 
    889 			sc->sc_tx_loaded--;
    890 		}
    891 
    892 #ifdef DIAGNOSTIC
    893 		if (sc->sc_tx_loaded != 0) {
    894 			panic("%s: sc->sc_tx_loaded is %d",sc->sc_dev.dv_xname,
    895 					sc->sc_tx_loaded);
    896 		}
    897 #endif
    898 
    899 		ifp->if_flags &= ~IFF_OACTIVE;
    900 
    901 		ifp->if_timer = 0;
    902 
    903 		if (ifp->if_snd.ifq_head != NULL) {
    904 			mb8795_start(ifp);
    905 		}
    906 
    907 	}
    908 
    909 #if 0
    910 	/* Enable ready interrupt */
    911 	bus_space_write_1(sc->sc_bst,sc->sc_bsh, XE_TXMASK,
    912 			bus_space_read_1(sc->sc_bst,sc->sc_bsh, XE_TXMASK)
    913 			| XE_TXMASK_READYIE);
    914 #endif
    915 }
    916 
    917 
    918 void
    919 mb8795_rxdma_completed(map, arg)
    920 	bus_dmamap_t map;
    921 	void *arg;
    922 {
    923 	struct mb8795_softc *sc = arg;
    924 
    925 	sc->sc_rx_completed_idx++;
    926 	sc->sc_rx_completed_idx %= MB8795_NRXBUFS;
    927 
    928   DPRINTF(("%s: mb8795_rxdma_completed(), sc->sc_rx_completed_idx = %d\n",
    929 			sc->sc_dev.dv_xname, sc->sc_rx_completed_idx));
    930 
    931 #if (defined(DIAGNOSTIC))
    932 	if (map != sc->sc_rx_dmamap[sc->sc_rx_completed_idx]) {
    933 		panic("%s: Unexpected rx dmamap completed\n",
    934 				sc->sc_dev.dv_xname);
    935 	}
    936 #endif
    937 }
    938 
    939 void
    940 mb8795_rxdma_shutdown(arg)
    941 	void *arg;
    942 {
    943 	struct mb8795_softc *sc = arg;
    944 
    945 	printf("%s: mb8795_rxdma_shutdown(), resetting the interface\n",
    946 			sc->sc_dev.dv_xname);
    947 
    948   /* we might want to just reset the DMA here instead,
    949 	 * but this will do.
    950 	 */
    951 	mb8795_init(sc);
    952 }
    953 
    954 
    955 /*
    956  * load a dmamap with a freshly allocated mbuf
    957  */
    958 struct mbuf *
    959 mb8795_rxdmamap_load(sc,map)
    960 	struct mb8795_softc *sc;
    961 	bus_dmamap_t map;
    962 {
    963 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    964 	struct mbuf *m;
    965 	int error;
    966 
    967 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    968 	if (m) {
    969 		MCLGET(m, M_DONTWAIT);
    970 		if ((m->m_flags & M_EXT) == 0) {
    971 			m_freem(m);
    972 			m = NULL;
    973 		} else {
    974 			m->m_len = MCLBYTES;
    975 		}
    976 	}
    977 	if (!m) {
    978 		/* @@@ Handle this gracefully by reusing a scratch buffer
    979 		 * or something.
    980 		 */
    981 		panic("Unable to get memory for incoming ethernet\n");
    982 	}
    983 
    984 	/* Align buffer, @@@ next specific.
    985 	 * perhaps should be using M_ALIGN here instead?
    986 	 * First we give us a little room to align with.
    987 	 */
    988 	{
    989 		u_char *buf = m->m_data;
    990 		int buflen = m->m_len;
    991 		buflen -= ENDMA_ENDALIGNMENT+DMA_BEGINALIGNMENT;
    992 		REALIGN_DMABUF(buf, buflen);
    993 		m->m_data = buf;
    994 		m->m_len = buflen;
    995 	}
    996 
    997 	m->m_pkthdr.rcvif = ifp;
    998 	m->m_pkthdr.len = m->m_len;
    999 
   1000   error = bus_dmamap_load_mbuf(sc->sc_rx_dmat,
   1001 			map, m, BUS_DMA_NOWAIT);
   1002 
   1003   bus_dmamap_sync(sc->sc_rx_dmat, map, 0,
   1004 			map->dm_mapsize, BUS_DMASYNC_PREREAD);
   1005 
   1006   if (error) {
   1007 		DPRINTF(("DEBUG: m->m_data = 0x%08x, m->m_len = %d\n",
   1008 				m->m_data, m->m_len));
   1009 		DPRINTF(("DEBUG: MCLBYTES = %d, map->_dm_size = %d\n",
   1010 				MCLBYTES, map->_dm_size));
   1011 
   1012     panic("%s: can't load rx mbuf chain, error = %d\n",
   1013 				sc->sc_dev.dv_xname, error);
   1014     m_freem(m);
   1015 		m = NULL;
   1016   }
   1017 
   1018 	return(m);
   1019 }
   1020 
   1021 bus_dmamap_t
   1022 mb8795_rxdma_continue(arg)
   1023 	void *arg;
   1024 {
   1025 	struct mb8795_softc *sc = arg;
   1026 	bus_dmamap_t map = NULL;
   1027 
   1028 	/*
   1029 	 * Currently, starts dumping new packets if the buffers
   1030 	 * fill up.  This should probably reclaim unhandled
   1031 	 * buffers instead so we drop older packets instead
   1032 	 * of newer ones.
   1033 	 */
   1034 	if (((sc->sc_rx_loaded_idx+1)%MB8795_NRXBUFS) != sc->sc_rx_handled_idx) {
   1035 		sc->sc_rx_loaded_idx++;
   1036 		sc->sc_rx_loaded_idx %= MB8795_NRXBUFS;
   1037 		map = sc->sc_rx_dmamap[sc->sc_rx_loaded_idx];
   1038 
   1039 		DPRINTF(("%s: mb8795_rxdma_continue() sc->sc_rx_loaded_idx = %d\nn",
   1040 				sc->sc_dev.dv_xname,sc->sc_rx_loaded_idx));
   1041 	}
   1042 #if (defined(DIAGNOSTIC))
   1043 	else {
   1044 		panic("%s: out of receive DMA buffers\n",sc->sc_dev.dv_xname);
   1045 	}
   1046 #endif
   1047 
   1048 	return(map);
   1049 }
   1050 
   1051 bus_dmamap_t
   1052 mb8795_txdma_continue(arg)
   1053 	void *arg;
   1054 {
   1055 	struct mb8795_softc *sc = arg;
   1056 	bus_dmamap_t map = sc->sc_tx_dmamap;
   1057 
   1058   DPRINTF(("%s: mb8795_txdma_continue()\n",sc->sc_dev.dv_xname));
   1059 
   1060 #ifdef DIAGNOSTIC
   1061 	if (sc->sc_tx_loaded != 1) {
   1062 			panic("%s: sc->sc_tx_loaded is %d",sc->sc_dev.dv_xname,
   1063 					sc->sc_tx_loaded);
   1064 	}
   1065 #endif
   1066 
   1067 	return(map);
   1068 }
   1069 
   1070 
   1071 /****************************************************************/
   1072 #if 0
   1073 int
   1074 mb8795_mediachange(ifp)
   1075 	struct ifnet *ifp;
   1076 {
   1077 	struct mb8795_softc *sc = ifp->if_softc;
   1078 
   1079 	if (sc->sc_mediachange)
   1080 		return ((*sc->sc_mediachange)(sc));
   1081 	return (EINVAL);
   1082 }
   1083 
   1084 void
   1085 mb8795_mediastatus(ifp, ifmr)
   1086 	struct ifnet *ifp;
   1087 	struct ifmediareq *ifmr;
   1088 {
   1089 	struct mb8795_softc *sc = ifp->if_softc;
   1090 
   1091 	if ((ifp->if_flags & IFF_UP) == 0)
   1092 		return;
   1093 
   1094 	ifmr->ifm_status = IFM_AVALID;
   1095 	if (sc->sc_havecarrier)
   1096 		ifmr->ifm_status |= IFM_ACTIVE;
   1097 
   1098 	if (sc->sc_mediastatus)
   1099 		(*sc->sc_mediastatus)(sc, ifmr);
   1100 }
   1101 #endif
   1102 /****************************************************************/
   1103