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