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