Home | History | Annotate | Line # | Download | only in qbus
if_qe.c revision 1.36
      1 /*	$NetBSD: if_qe.c,v 1.36 1999/06/06 19:26:44 ragge Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1988 Regents of the University of California.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Digital Equipment Corp.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  *
     38  *	@(#)if_qe.c	7.20 (Berkeley) 3/28/91
     39  */
     40 
     41 /* from	 @(#)if_qe.c	1.15	(ULTRIX)	4/16/86 */
     42 
     43 /****************************************************************
     44  *								*
     45  *	  Licensed from Digital Equipment Corporation		*
     46  *			 Copyright (c)				*
     47  *		 Digital Equipment Corporation			*
     48  *		     Maynard, Massachusetts			*
     49  *			   1985, 1986				*
     50  *		      All rights reserved.			*
     51  *								*
     52  *	  The Information in this software is subject to change *
     53  *   without notice and should not be construed as a commitment *
     54  *   by	 Digital  Equipment  Corporation.   Digital   makes  no *
     55  *   representations about the suitability of this software for *
     56  *   any purpose.  It is supplied "As Is" without expressed  or *
     57  *   implied  warranty.						*
     58  *								*
     59  *	  If the Regents of the University of California or its *
     60  *   licensees modify the software in a manner creating		*
     61  *   derivative copyright rights, appropriate copyright		*
     62  *   legends may be placed on the derivative work in addition	*
     63  *   to that set forth above.					*
     64  *								*
     65  ****************************************************************/
     66 /* ---------------------------------------------------------------------
     67  * Modification History
     68  *
     69  * 15-Apr-86  -- afd
     70  *	Rename "unused_multi" to "qunused_multi" for extending Generic
     71  *	kernel to MicroVAXen.
     72  *
     73  * 18-mar-86  -- jaw	 br/cvec changed to NOT use registers.
     74  *
     75  * 12 March 86 -- Jeff Chase
     76  *	Modified to handle the new MCLGET macro
     77  *	Changed if_qe_data.c to use more receive buffers
     78  *	Added a flag to poke with adb to log qe_restarts on console
     79  *
     80  * 19 Oct 85 -- rjl
     81  *	Changed the watch dog timer from 30 seconds to 3.  VMS is using
     82  *	less than 1 second in their's. Also turned the printf into an
     83  *	mprintf.
     84  *
     85  *  09/16/85 -- Larry Cohen
     86  *		Add 43bsd alpha tape changes for subnet routing
     87  *
     88  *  1 Aug 85 -- rjl
     89  *	Panic on a non-existent memory interrupt and the case where a packet
     90  *	was chained.  The first should never happen because non-existant
     91  *	memory interrupts cause a bus reset. The second should never happen
     92  *	because we hang 2k input buffers on the device.
     93  *
     94  *  1 Aug 85 -- rich
     95  *	Fixed the broadcast loopback code to handle Clusters without
     96  *	wedging the system.
     97  *
     98  *  27 Feb. 85 -- ejf
     99  *	Return default hardware address on ioctl request.
    100  *
    101  *  12 Feb. 85 -- ejf
    102  *	Added internal extended loopback capability.
    103  *
    104  *  27 Dec. 84 -- rjl
    105  *	Fixed bug that caused every other transmit descriptor to be used
    106  *	instead of every descriptor.
    107  *
    108  *  21 Dec. 84 -- rjl
    109  *	Added watchdog timer to mask hardware bug that causes device lockup.
    110  *
    111  *  18 Dec. 84 -- rjl
    112  *	Reworked driver to use q-bus mapping routines.	MicroVAX-I now does
    113  *	copying instead of m-buf shuffleing.
    114  *	A number of deficencies in the hardware/firmware were compensated
    115  *	for. See comments in qestart and qerint.
    116  *
    117  *  14 Nov. 84 -- jf
    118  *	Added usage counts for multicast addresses.
    119  *	Updated general protocol support to allow access to the Ethernet
    120  *	header.
    121  *
    122  *  04 Oct. 84 -- jf
    123  *	Added support for new ioctls to add and delete multicast addresses
    124  *	and set the physical address.
    125  *	Add support for general protocols.
    126  *
    127  *  14 Aug. 84 -- rjl
    128  *	Integrated Shannon changes. (allow arp above 1024 and ? )
    129  *
    130  *  13 Feb. 84 -- rjl
    131  *
    132  *	Initial version of driver. derived from IL driver.
    133  *
    134  * ---------------------------------------------------------------------
    135  */
    136 
    137 /*
    138  * Digital Q-BUS to NI Adapter
    139  * supports DEQNA and DELQA in DEQNA-mode.
    140  */
    141 
    142 #include "opt_inet.h"
    143 #include "opt_ccitt.h"
    144 #include "opt_llc.h"
    145 #include "opt_iso.h"
    146 #include "opt_ns.h"
    147 #include "bpfilter.h"
    148 
    149 #include <sys/param.h>
    150 #include <sys/systm.h>
    151 #include <sys/mbuf.h>
    152 #include <sys/buf.h>
    153 #include <sys/protosw.h>
    154 #include <sys/socket.h>
    155 #include <sys/ioctl.h>
    156 #include <sys/errno.h>
    157 #include <sys/syslog.h>
    158 #include <sys/device.h>
    159 #include <sys/time.h>
    160 #include <sys/kernel.h>
    161 #include <sys/reboot.h>
    162 
    163 #include <net/if.h>
    164 #include <net/if_ether.h>
    165 #include <net/if_dl.h>
    166 
    167 #ifdef INET
    168 #include <netinet/in.h>
    169 #include <netinet/in_systm.h>
    170 #include <netinet/in_var.h>
    171 #include <netinet/ip.h>
    172 #include <netinet/if_inarp.h>
    173 #endif
    174 
    175 #ifdef NS
    176 #include <netns/ns.h>
    177 #include <netns/ns_if.h>
    178 #endif
    179 
    180 #ifdef ISO
    181 #include <netiso/iso.h>
    182 #include <netiso/iso_var.h>
    183 extern char all_es_snpa[], all_is_snpa[], all_l1is_snpa[], all_l2is_snpa[];
    184 #endif
    185 
    186 #if defined(CCITT) && defined(LLC)
    187 #include <sys/socketvar.h>
    188 #include <netccitt/x25.h>
    189 #include <netccitt/pk.h>
    190 #include <netccitt/pk_var.h>
    191 #include <netccitt/pk_extern.h>
    192 #endif
    193 
    194 #if NBPFILTER > 0
    195 #include <net/bpf.h>
    196 #include <net/bpfdesc.h>
    197 #endif
    198 
    199 #include <machine/pte.h>
    200 #include <machine/cpu.h>
    201 #include <machine/rpb.h>
    202 
    203 #include <vax/if/if_qereg.h>
    204 #include <vax/if/if_uba.h>
    205 #include <vax/uba/ubareg.h>
    206 #include <vax/uba/ubavar.h>
    207 
    208 #define NRCV	15			/* Receive descriptors		*/
    209 #define NXMT	5			/* Transmit descriptors		*/
    210 #define NTOT	(NXMT + NRCV)
    211 
    212 #define QETIMEOUT	2		/* transmit timeout, must be > 1 */
    213 #define QESLOWTIMEOUT	40		/* timeout when no xmits in progress */
    214 
    215 #define MINDATA 60
    216 
    217 /*
    218  * Ethernet software status per interface.
    219  *
    220  * Each interface is referenced by a network interface structure,
    221  * qe_if, which the routing code uses to locate the interface.
    222  * This structure contains the output queue for the interface, its address, ...
    223  */
    224 struct	qe_softc {
    225 	struct	device qe_dev;		/* Configuration common part	*/
    226 	struct	ethercom qe_ec;		/* Ethernet common part		*/
    227 #define qe_if	qe_ec.ec_if		/* network-visible interface	*/
    228 	struct	ifubinfo qe_uba;	/* Q-bus resources		*/
    229 	struct	ifrw qe_ifr[NRCV];	/*	for receive buffers;	*/
    230 	struct	ifxmt qe_ifw[NXMT];	/*	for xmit buffers;	*/
    231 	struct	qedevice *qe_vaddr;
    232 	int	qe_flags;		/* software state		*/
    233 #define QEF_RUNNING	0x01
    234 #define QEF_SETADDR	0x02
    235 #define QEF_FASTTIMEO	0x04
    236 	int	setupaddr;		/* mapping info for setup pkts	*/
    237 	int	ipl;			/* interrupt priority		*/
    238 	struct	qe_ring *rringaddr;	/* mapping info for rings	*/
    239 	struct	qe_ring *tringaddr;	/*	 ""			*/
    240 	struct	qe_ring rring[NRCV+1];	/* Receive ring descriptors	*/
    241 	struct	qe_ring tring[NXMT+1];	/* Xmit ring descriptors	*/
    242 	u_char	setup_pkt[16][8];	/* Setup packet			*/
    243 	int	rindex;			/* Receive index		*/
    244 	int	tindex;			/* Transmit index		*/
    245 	int	otindex;		/* Old transmit index		*/
    246 	int	qe_intvec;		/* Interrupt vector		*/
    247 	struct	qedevice *addr;		/* device addr			*/
    248 	int	setupqueued;		/* setup packet queued		*/
    249 	int	setuplength;		/* length of setup packet	*/
    250 	int	nxmit;			/* Transmits in progress	*/
    251 	int	qe_restarts;		/* timeouts			*/
    252 };
    253 
    254 int	qematch __P((struct device *, struct cfdata *, void *));
    255 void	qeattach __P((struct device *, struct device *, void *));
    256 void	qereset __P((int));
    257 void	qeinit __P((struct qe_softc *));
    258 void	qestart __P((struct ifnet *));
    259 void	qeintr __P((int));
    260 void	qetint __P((int));
    261 void	qerint __P((int));
    262 int	qeioctl __P((struct ifnet *, u_long, caddr_t));
    263 void	qe_setaddr __P((u_char *, struct qe_softc *));
    264 void	qeinitdesc __P((struct qe_ring *, caddr_t, int));
    265 void	qesetup __P((struct qe_softc *));
    266 void	qeread __P((struct qe_softc *, struct ifrw *, int));
    267 void	qetimeout __P((struct ifnet *));
    268 void	qerestart __P((struct qe_softc *));
    269 
    270 struct	cfattach qe_ca = {
    271 	sizeof(struct qe_softc), qematch, qeattach
    272 };
    273 
    274 extern struct cfdriver qe_cd;
    275 
    276 #define QEUNIT(x)	minor(x)
    277 /*
    278  * The deqna shouldn't receive more than ETHERMTU + sizeof(struct ether_header)
    279  * but will actually take in up to 2048 bytes. To guard against the receiver
    280  * chaining buffers (which we aren't prepared to handle) we allocate 2kb
    281  * size buffers.
    282  */
    283 #define MAXPACKETSIZE 2048		/* Should really be ETHERMTU	*/
    284 
    285 /*
    286  * Probe the QNA to see if it's there
    287  */
    288 int
    289 qematch(parent, cf, aux)
    290 	struct	device *parent;
    291 	struct	cfdata *cf;
    292 	void	*aux;
    293 {
    294 	struct	qe_softc sc;
    295 	struct	uba_attach_args *ua = aux;
    296 	struct	uba_softc *ubasc = (struct uba_softc *)parent;
    297 	struct	qe_ring *rp;
    298 	struct	qe_ring *prp;	/* physical rp		*/
    299 	volatile struct qedevice *addr = (struct qedevice *)ua->ua_addr;
    300 	int i;
    301 
    302 	/*
    303 	 * The QNA interrupts on i/o operations. To do an I/O operation
    304 	 * we have to setup the interface by transmitting a setup  packet.
    305 	 */
    306 
    307 	addr->qe_csr = QE_RESET;
    308 	addr->qe_csr &= ~QE_RESET;
    309 	addr->qe_vector = (ubasc->uh_lastiv -= 4);
    310 
    311 	bzero(&sc, sizeof(struct qe_softc));
    312 	/*
    313 	 * Map the communications area and the setup packet.
    314 	 */
    315 	sc.setupaddr =
    316 	    uballoc(ubasc, (caddr_t)sc.setup_pkt, sizeof(sc.setup_pkt), 0);
    317 	sc.rringaddr = (struct qe_ring *) uballoc(ubasc, (caddr_t)sc.rring,
    318 	    sizeof(struct qe_ring) * (NTOT+2), 0);
    319 	prp = (struct qe_ring *)UBAI_ADDR((int)sc.rringaddr);
    320 
    321 	/*
    322 	 * The QNA will loop the setup packet back to the receive ring
    323 	 * for verification, therefore we initialize the first
    324 	 * receive & transmit ring descriptors and link the setup packet
    325 	 * to them.
    326 	 */
    327 	qeinitdesc(sc.tring, (caddr_t)UBAI_ADDR(sc.setupaddr),
    328 	    sizeof(sc.setup_pkt));
    329 	qeinitdesc(sc.rring, (caddr_t)UBAI_ADDR(sc.setupaddr),
    330 	    sizeof(sc.setup_pkt));
    331 
    332 	rp = (struct qe_ring *)sc.tring;
    333 	rp->qe_setup = 1;
    334 	rp->qe_eomsg = 1;
    335 	rp->qe_flag = rp->qe_status1 = QE_NOTYET;
    336 	rp->qe_valid = 1;
    337 
    338 	rp = (struct qe_ring *)sc.rring;
    339 	rp->qe_flag = rp->qe_status1 = QE_NOTYET;
    340 	rp->qe_valid = 1;
    341 
    342 	/*
    343 	 * Get the addr off of the interface and place it into the setup
    344 	 * packet. This code looks strange due to the fact that the address
    345 	 * is placed in the setup packet in col. major order.
    346 	 */
    347 	for (i = 0; i < 6; i++)
    348 		sc.setup_pkt[i][1] = addr->qe_sta_addr[i];
    349 
    350 	qesetup(&sc);
    351 	/*
    352 	 * Start the interface and wait for the packet.
    353 	 */
    354 	addr->qe_csr = QE_INT_ENABLE | QE_XMIT_INT | QE_RCV_INT;
    355 	addr->qe_rcvlist_lo = (short)((int)prp);
    356 	addr->qe_rcvlist_hi = (short)((int)prp >> 16);
    357 	prp += NRCV+1;
    358 	addr->qe_xmtlist_lo = (short)((int)prp);
    359 	addr->qe_xmtlist_hi = (short)((int)prp >> 16);
    360 	DELAY(10000);
    361 	/*
    362 	 * All done with the bus resources.
    363 	 */
    364 	ubarelse(ubasc, &sc.setupaddr);
    365 	ubarelse(ubasc, (int *)&sc.rringaddr);
    366 	ua->ua_ivec = qeintr;
    367 	return 1;
    368 }
    369 
    370 /*
    371  * Interface exists: make available by filling in network interface
    372  * record.  System will initialize the interface when it is ready
    373  * to accept packets.
    374  */
    375 void
    376 qeattach(parent, self, aux)
    377 	struct	device *parent, *self;
    378 	void	*aux;
    379 {
    380 	struct	uba_attach_args *ua = aux;
    381 	struct	qe_softc *sc = (struct qe_softc *)self;
    382 	struct	ifnet *ifp = (struct ifnet *)&sc->qe_if;
    383 	struct qedevice *addr =(struct qedevice *)ua->ua_addr;
    384 	int i;
    385 	u_int8_t myaddr[ETHER_ADDR_LEN];
    386 
    387 	printf("\n");
    388 	sc->ipl = 0x15;
    389 	sc->qe_vaddr = addr;
    390 	bcopy(sc->qe_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
    391 	ifp->if_softc = sc;
    392 	/*
    393 	 * The Deqna is cable of transmitting broadcasts, but
    394 	 * doesn't listen to its own.
    395 	 */
    396 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS |
    397 	    IFF_MULTICAST;
    398 
    399 	/*
    400 	 * Read the address from the prom and save it.
    401 	 */
    402 	for (i = 0; i < 6; i++)
    403 		sc->setup_pkt[i][1] = myaddr[i] =
    404 		    addr->qe_sta_addr[i] & 0xff;
    405 	addr->qe_vector |= 1;
    406 	printf("qe%d: %s, hardware address %s\n", sc->qe_dev.dv_unit,
    407 		addr->qe_vector&01 ? "delqa":"deqna",
    408 		ether_sprintf(myaddr));
    409 	addr->qe_vector &= ~1;
    410 
    411 	/*
    412 	 * Save the vector for initialization at reset time.
    413 	 */
    414 	sc->qe_intvec = addr->qe_vector;
    415 
    416 	ifp->if_start = qestart;
    417 	ifp->if_ioctl = qeioctl;
    418 	ifp->if_watchdog = qetimeout;
    419 	sc->qe_uba.iff_flags = UBA_CANTWAIT;
    420 	if_attach(ifp);
    421 	ether_ifattach(ifp, myaddr);
    422 
    423 #if NBPFILTER > 0
    424 	bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
    425 #endif
    426 	if (B_TYPE(bootdev) == BDEV_QE)
    427 		booted_from = self;
    428 }
    429 
    430 /*
    431  * Reset of interface after UNIBUS reset.
    432  */
    433 void
    434 qereset(unit)
    435 	int unit;
    436 {
    437 	struct	qe_softc *sc = qe_cd.cd_devs[unit];
    438 
    439 	printf(" %s", sc->qe_dev.dv_xname);
    440 	sc->qe_if.if_flags &= ~IFF_RUNNING;
    441 	qeinit(sc);
    442 }
    443 
    444 /*
    445  * Initialization of interface.
    446  */
    447 void
    448 qeinit(sc)
    449 	struct qe_softc *sc;
    450 {
    451 	struct qedevice *addr = sc->qe_vaddr;
    452 	struct uba_softc *ubasc = (void *)sc->qe_dev.dv_parent;
    453 	struct ifnet *ifp = (struct ifnet *)&sc->qe_if;
    454 	int i;
    455 	int s;
    456 
    457 	/* address not known */
    458 	if (ifp->if_addrlist.tqh_first == (struct ifaddr *)0)
    459 			return;
    460 	if (sc->qe_flags & QEF_RUNNING)
    461 		return;
    462 
    463 	if ((ifp->if_flags & IFF_RUNNING) == 0) {
    464 		/*
    465 		 * map the communications area onto the device
    466 		 */
    467 		i = uballoc(ubasc, (caddr_t)sc->rring,
    468 		    sizeof(struct qe_ring) * (NTOT+2), 0);
    469 		if (i == 0)
    470 			goto fail;
    471 		sc->rringaddr = (struct qe_ring *)UBAI_ADDR(i);
    472 		sc->tringaddr = sc->rringaddr + NRCV + 1;
    473 		i = uballoc(ubasc, (caddr_t)sc->setup_pkt,
    474 		    sizeof(sc->setup_pkt), 0);
    475 		if (i == 0)
    476 			goto fail;
    477 		sc->setupaddr = UBAI_ADDR(i);
    478 		/*
    479 		 * init buffers and maps
    480 		 */
    481 		if (if_ubaminit(&sc->qe_uba, (void *)sc->qe_dev.dv_parent,
    482 		    sizeof (struct ether_header), (int)vax_btoc(MAXPACKETSIZE),
    483 		    sc->qe_ifr, NRCV, sc->qe_ifw, NXMT) == 0) {
    484 	fail:
    485 			printf("%s: can't allocate uba resources\n",
    486 			    sc->qe_dev.dv_xname);
    487 			sc->qe_if.if_flags &= ~IFF_UP;
    488 			return;
    489 		}
    490 	}
    491 	/*
    492 	 * Init the buffer descriptors and indexes for each of the lists and
    493 	 * loop them back to form a ring.
    494 	 */
    495 	for (i = 0; i < NRCV; i++) {
    496 		qeinitdesc( &sc->rring[i],
    497 		    (caddr_t)UBAI_ADDR(sc->qe_ifr[i].ifrw_info), MAXPACKETSIZE);
    498 		sc->rring[i].qe_flag = sc->rring[i].qe_status1 = QE_NOTYET;
    499 		sc->rring[i].qe_valid = 1;
    500 	}
    501 	qeinitdesc(&sc->rring[i], (caddr_t)NULL, 0);
    502 
    503 	sc->rring[i].qe_addr_lo = (short)((int)sc->rringaddr);
    504 	sc->rring[i].qe_addr_hi = (short)((int)sc->rringaddr >> 16);
    505 	sc->rring[i].qe_chain = 1;
    506 	sc->rring[i].qe_flag = sc->rring[i].qe_status1 = QE_NOTYET;
    507 	sc->rring[i].qe_valid = 1;
    508 
    509 	for( i = 0 ; i <= NXMT ; i++ )
    510 		qeinitdesc(&sc->tring[i], (caddr_t)NULL, 0);
    511 	i--;
    512 
    513 	sc->tring[i].qe_addr_lo = (short)((int)sc->tringaddr);
    514 	sc->tring[i].qe_addr_hi = (short)((int)sc->tringaddr >> 16);
    515 	sc->tring[i].qe_chain = 1;
    516 	sc->tring[i].qe_flag = sc->tring[i].qe_status1 = QE_NOTYET;
    517 	sc->tring[i].qe_valid = 1;
    518 
    519 	sc->nxmit = sc->otindex = sc->tindex = sc->rindex = 0;
    520 
    521 	/*
    522 	 * Take the interface out of reset, program the vector,
    523 	 * enable interrupts, and tell the world we are up.
    524 	 */
    525 	s = splnet();
    526 	addr->qe_vector = sc->qe_intvec;
    527 	sc->addr = addr;
    528 	addr->qe_csr = QE_RCV_ENABLE | QE_INT_ENABLE | QE_XMIT_INT |
    529 	    QE_RCV_INT | QE_ILOOP;
    530 	addr->qe_rcvlist_lo = (short)((int)sc->rringaddr);
    531 	addr->qe_rcvlist_hi = (short)((int)sc->rringaddr >> 16);
    532 	ifp->if_flags |= IFF_UP | IFF_RUNNING;
    533 	sc->qe_flags |= QEF_RUNNING;
    534 	qesetup( sc );
    535 	qestart( ifp );
    536 	sc->qe_if.if_timer = QESLOWTIMEOUT;	/* Start watchdog */
    537 	splx( s );
    538 }
    539 
    540 /*
    541  * Start output on interface.
    542  *
    543  */
    544 void
    545 qestart(ifp)
    546 	struct ifnet *ifp;
    547 {
    548 	register struct qe_softc *sc = ifp->if_softc;
    549 	volatile struct qedevice *addr = sc->qe_vaddr;
    550 	register struct qe_ring *rp;
    551 	register int index;
    552 	struct mbuf *m;
    553 	int buf_addr, len, s;
    554 
    555 
    556 	s = splnet();
    557 	/*
    558 	 * The deqna doesn't look at anything but the valid bit
    559 	 * to determine if it should transmit this packet. If you have
    560 	 * a ring and fill it the device will loop indefinately on the
    561 	 * packet and continue to flood the net with packets until you
    562 	 * break the ring. For this reason we never queue more than n-1
    563 	 * packets in the transmit ring.
    564 	 *
    565 	 * The microcoders should have obeyed their own defination of the
    566 	 * flag and status words, but instead we have to compensate.
    567 	 */
    568 	for( index = sc->tindex;
    569 		sc->tring[index].qe_valid == 0 && sc->nxmit < (NXMT-1) ;
    570 		sc->tindex = index = ++index % NXMT){
    571 		rp = &sc->tring[index];
    572 		if( sc->setupqueued ) {
    573 			buf_addr = sc->setupaddr;
    574 			len = sc->setuplength;
    575 			rp->qe_setup = 1;
    576 			sc->setupqueued = 0;
    577 		} else {
    578 			IF_DEQUEUE(&sc->qe_if.if_snd, m);
    579 			if (m == 0) {
    580 				splx(s);
    581 				return;
    582 			}
    583 #if NBPFILTER > 0
    584 			if (ifp->if_bpf)
    585 				bpf_mtap(ifp->if_bpf, m);
    586 #endif
    587 			buf_addr = sc->qe_ifw[index].ifw_info;
    588 			len = if_ubaput(&sc->qe_uba, &sc->qe_ifw[index], m);
    589 		}
    590 		if( len < MINDATA )
    591 			len = MINDATA;
    592 		/*
    593 		 *  Does buffer end on odd byte ?
    594 		 */
    595 		if( len & 1 ) {
    596 			len++;
    597 			rp->qe_odd_end = 1;
    598 		}
    599 		rp->qe_buf_len = -(len/2);
    600 		buf_addr = UBAI_ADDR(buf_addr);
    601 		rp->qe_flag = rp->qe_status1 = QE_NOTYET;
    602 		rp->qe_addr_lo = (short)buf_addr;
    603 		rp->qe_addr_hi = (short)(buf_addr >> 16);
    604 		rp->qe_eomsg = 1;
    605 		rp->qe_flag = rp->qe_status1 = QE_NOTYET;
    606 		rp->qe_valid = 1;
    607 		if (sc->nxmit++ == 0) {
    608 			sc->qe_flags |= QEF_FASTTIMEO;
    609 			sc->qe_if.if_timer = QETIMEOUT;
    610 		}
    611 
    612 		/*
    613 		 * See if the xmit list is invalid.
    614 		 */
    615 		if( addr->qe_csr & QE_XL_INVALID ) {
    616 			buf_addr = (int)(sc->tringaddr+index);
    617 			addr->qe_xmtlist_lo = (short)buf_addr;
    618 			addr->qe_xmtlist_hi = (short)(buf_addr >> 16);
    619 		}
    620 	}
    621 	splx(s);
    622 	return;
    623 }
    624 
    625 /*
    626  * Ethernet interface interrupt processor
    627  */
    628 void
    629 qeintr(unit)
    630 	int	unit;
    631 {
    632 	register struct qe_softc *sc;
    633 	volatile struct qedevice *addr;
    634 	int buf_addr, csr;
    635 
    636 	sc = qe_cd.cd_devs[unit];
    637 	addr = sc->qe_vaddr;
    638 	splx(sc->ipl);
    639 	if (!(sc->qe_flags & QEF_FASTTIMEO))
    640 		sc->qe_if.if_timer = QESLOWTIMEOUT; /* Restart timer clock */
    641 	csr = addr->qe_csr;
    642 	addr->qe_csr = QE_RCV_ENABLE | QE_INT_ENABLE |
    643 	    QE_XMIT_INT | QE_RCV_INT | QE_ILOOP;
    644 	if (csr & QE_RCV_INT)
    645 		qerint(unit);
    646 	if (csr & QE_XMIT_INT)
    647 		qetint(unit );
    648 	if (csr & QE_NEX_MEM_INT)
    649 		printf("qe%d: Nonexistent memory interrupt\n", unit);
    650 
    651 	if (addr->qe_csr & QE_RL_INVALID && sc->rring[sc->rindex].qe_status1 ==
    652 	    QE_NOTYET) {
    653 		buf_addr = (int)&sc->rringaddr[sc->rindex];
    654 		addr->qe_rcvlist_lo = (short)buf_addr;
    655 		addr->qe_rcvlist_hi = (short)(buf_addr >> 16);
    656 	}
    657 }
    658 
    659 /*
    660  * Ethernet interface transmit interrupt.
    661  */
    662 void
    663 qetint(unit)
    664 	int unit;
    665 {
    666 	register struct qe_softc *sc = qe_cd.cd_devs[unit];
    667 	register struct qe_ring *rp;
    668 	register struct ifxmt *ifxp;
    669 	int status1, setupflag;
    670 	short len;
    671 
    672 
    673 	while (sc->otindex != sc->tindex && sc->tring[sc->otindex].qe_status1
    674 	    != QE_NOTYET && sc->nxmit > 0) {
    675 		/*
    676 		 * Save the status words from the descriptor so that it can
    677 		 * be released.
    678 		 */
    679 		rp = &sc->tring[sc->otindex];
    680 		status1 = rp->qe_status1;
    681 		setupflag = rp->qe_setup;
    682 		len = (-rp->qe_buf_len) * 2;
    683 		if( rp->qe_odd_end )
    684 			len++;
    685 		/*
    686 		 * Init the buffer descriptor
    687 		 */
    688 		bzero((caddr_t)rp, sizeof(struct qe_ring));
    689 		if( --sc->nxmit == 0 ) {
    690 			sc->qe_flags &= ~QEF_FASTTIMEO;
    691 			sc->qe_if.if_timer = QESLOWTIMEOUT;
    692 		}
    693 		if( !setupflag ) {
    694 			/*
    695 			 * Do some statistics.
    696 			 */
    697 			sc->qe_if.if_opackets++;
    698 			sc->qe_if.if_collisions += ( status1 & QE_CCNT ) >> 4;
    699 			if (status1 & QE_ERROR)
    700 				sc->qe_if.if_oerrors++;
    701 			ifxp = &sc->qe_ifw[sc->otindex];
    702 			if (ifxp->ifw_xtofree) {
    703 				m_freem(ifxp->ifw_xtofree);
    704 				ifxp->ifw_xtofree = 0;
    705 			}
    706 		}
    707 		sc->otindex = ++sc->otindex % NXMT;
    708 	}
    709 	qestart(&sc->qe_if);
    710 }
    711 
    712 /*
    713  * Ethernet interface receiver interrupt.
    714  * If can't determine length from type, then have to drop packet.
    715  * Othewise decapsulate packet based on type and pass to type specific
    716  * higher-level input routine.
    717  */
    718 void
    719 qerint(unit)
    720 	int unit;
    721 {
    722 	register struct qe_softc *sc = qe_cd.cd_devs[unit];
    723 	register struct qe_ring *rp;
    724 	register int nrcv = 0;
    725 	int len, status1, status2;
    726 	int bufaddr;
    727 
    728 	/*
    729 	 * Traverse the receive ring looking for packets to pass back.
    730 	 * The search is complete when we find a descriptor not in use.
    731 	 *
    732 	 * As in the transmit case the deqna doesn't honor it's own protocols
    733 	 * so there exists the possibility that the device can beat us around
    734 	 * the ring. The proper way to guard against this is to insure that
    735 	 * there is always at least one invalid descriptor. We chose instead
    736 	 * to make the ring large enough to minimize the problem. With a ring
    737 	 * size of 4 we haven't been able to see the problem. To be safe we
    738 	 * doubled that to 8.
    739 	 *
    740 	 */
    741 	while (sc->rring[sc->rindex].qe_status1 == QE_NOTYET && nrcv < NRCV) {
    742 		/*
    743 		 * We got an interrupt but did not find an input packet
    744 		 * where we expected one to be, probably because the ring
    745 		 * was overrun.
    746 		 * We search forward to find a valid packet and start
    747 		 * processing from there.  If no valid packet is found it
    748 		 * means we processed all the packets during a previous
    749 		 * interrupt and that the QE_RCV_INT bit was set while
    750 		 * we were processing one of these earlier packets.  In
    751 		 * this case we can safely ignore the interrupt (by dropping
    752 		 * through the code below).
    753 		 */
    754 		sc->rindex = (sc->rindex + 1) % NRCV;
    755 		nrcv++;
    756 	}
    757 #ifndef QE_NO_OVERRUN_WARNINGS
    758 	if (nrcv && nrcv < NRCV)
    759 		log(LOG_ERR, "qe%d: ring overrun, resync'd by skipping %d\n",
    760 		    unit, nrcv);
    761 #endif
    762 
    763 	for (; sc->rring[sc->rindex].qe_status1 != QE_NOTYET;
    764 	    sc->rindex = ++sc->rindex % NRCV) {
    765 		rp = &sc->rring[sc->rindex];
    766 		status1 = rp->qe_status1;
    767 		status2 = rp->qe_status2;
    768 		bzero((caddr_t)rp, sizeof(struct qe_ring));
    769 		if( (status1 & QE_MASK) == QE_MASK )
    770 			panic("qe: chained packet");
    771 		len = ((status1 & QE_RBL_HI) | (status2 & QE_RBL_LO)) + 60;
    772 		sc->qe_if.if_ipackets++;
    773 
    774 		if (status1 & QE_ERROR) {
    775 			if ((status1 & QE_RUNT) == 0)
    776 				sc->qe_if.if_ierrors++;
    777 		} else {
    778 			/*
    779 			 * We don't process setup packets.
    780 			 */
    781 			if (!(status1 & QE_ESETUP))
    782 				qeread(sc, &sc->qe_ifr[sc->rindex],
    783 					len - sizeof(struct ether_header));
    784 		}
    785 		/*
    786 		 * Return the buffer to the ring
    787 		 */
    788 		bufaddr = (int)UBAI_ADDR(sc->qe_ifr[sc->rindex].ifrw_info);
    789 		rp->qe_buf_len = -((MAXPACKETSIZE)/2);
    790 		rp->qe_addr_lo = (short)bufaddr;
    791 		rp->qe_addr_hi = (short)((int)bufaddr >> 16);
    792 		rp->qe_flag = rp->qe_status1 = QE_NOTYET;
    793 		rp->qe_valid = 1;
    794 	}
    795 }
    796 
    797 /*
    798  * Process an ioctl request.
    799  */
    800 int
    801 qeioctl(ifp, cmd, data)
    802 	register struct ifnet *ifp;
    803 	u_long cmd;
    804 	caddr_t data;
    805 {
    806 	struct qe_softc *sc = ifp->if_softc;
    807 	struct ifaddr *ifa = (struct ifaddr *)data;
    808 	struct ifreq *ifr = (struct ifreq *)data;
    809 	int s = splnet(), error = 0;
    810 
    811 	switch (cmd) {
    812 
    813 	case SIOCSIFADDR:
    814 		ifp->if_flags |= IFF_UP;
    815 		qeinit(sc);
    816 		switch(ifa->ifa_addr->sa_family) {
    817 #ifdef INET
    818 		case AF_INET:
    819 			arp_ifinit(ifp, ifa);
    820 			break;
    821 #endif
    822 #ifdef NS
    823 		case AF_NS:
    824 		    {
    825 			register struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
    826 
    827 			if (ns_nullhost(*ina))
    828 				ina->x_host =
    829 				    *(union ns_host *)LLADDR(ifp->if_sadl);
    830 			else
    831 				qe_setaddr(ina->x_host.c_host, sc);
    832 			break;
    833 		    }
    834 #endif
    835 		}
    836 		break;
    837 
    838 	case SIOCSIFFLAGS:
    839 		if ((ifp->if_flags & IFF_UP) == 0 &&
    840 		    sc->qe_flags & QEF_RUNNING) {
    841 			sc->qe_vaddr->qe_csr = QE_RESET;
    842 			sc->qe_flags &= ~QEF_RUNNING;
    843 		} else if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) ==
    844 		    IFF_RUNNING && (sc->qe_flags & QEF_RUNNING) == 0)
    845 			qerestart(sc);
    846 		else
    847 			qeinit(sc);
    848 
    849 		break;
    850 
    851 	case SIOCADDMULTI:
    852 	case SIOCDELMULTI:
    853 		/*
    854 		 * Update our multicast list.
    855 		 */
    856 		error = (cmd == SIOCADDMULTI) ?
    857 			ether_addmulti(ifr, &sc->qe_ec):
    858 			ether_delmulti(ifr, &sc->qe_ec);
    859 
    860 		if (error == ENETRESET) {
    861 			/*
    862 			 * Multicast list has changed; set the hardware filter
    863 			 * accordingly.
    864 			 */
    865 			qeinit(sc);
    866 			error = 0;
    867 		}
    868 		break;
    869 
    870 	default:
    871 		error = EINVAL;
    872 
    873 	}
    874 	splx(s);
    875 	return (error);
    876 }
    877 
    878 /*
    879  * set ethernet address for unit
    880  */
    881 void
    882 qe_setaddr(physaddr, sc)
    883 	u_char *physaddr;
    884 	struct qe_softc *sc;
    885 {
    886 	register int i;
    887 
    888 	for (i = 0; i < 6; i++)
    889 		sc->setup_pkt[i][1] = LLADDR(sc->qe_if.if_sadl)[i]
    890 		    = physaddr[i];
    891 	sc->qe_flags |= QEF_SETADDR;
    892 	if (sc->qe_if.if_flags & IFF_RUNNING)
    893 		qesetup(sc);
    894 	qeinit(sc);
    895 }
    896 
    897 
    898 /*
    899  * Initialize a ring descriptor with mbuf allocation side effects
    900  */
    901 void
    902 qeinitdesc(rp, addr, len)
    903 	register struct qe_ring *rp;
    904 	caddr_t addr;			/* mapped address */
    905 	int len;
    906 {
    907 	/*
    908 	 * clear the entire descriptor
    909 	 */
    910 	bzero((caddr_t)rp, sizeof(struct qe_ring));
    911 
    912 	if (len) {
    913 		rp->qe_buf_len = -(len/2);
    914 		rp->qe_addr_lo = (short)((int)addr);
    915 		rp->qe_addr_hi = (short)((int)addr >> 16);
    916 	}
    917 }
    918 /*
    919  * Build a setup packet - the physical address will already be present
    920  * in first column.
    921  */
    922 void
    923 qesetup(sc)
    924 	struct qe_softc *sc;
    925 {
    926 	register int i, j;
    927 
    928 	/*
    929 	 * Copy the target address to the rest of the entries in this row.
    930 	 */
    931 	 for (j = 0; j < 6; j++)
    932 		for (i = 2; i < 8; i++)
    933 			sc->setup_pkt[j][i] = sc->setup_pkt[j][1];
    934 	/*
    935 	 * Duplicate the first half.
    936 	 */
    937 	bcopy((caddr_t)sc->setup_pkt[0], (caddr_t)sc->setup_pkt[8], 64);
    938 	/*
    939 	 * Fill in the broadcast (and ISO multicast) address(es).
    940 	 */
    941 	for (i = 0; i < 6; i++) {
    942 		sc->setup_pkt[i][2] = 0xff;
    943 #ifdef ISO
    944 		/*
    945 		 * XXX layer violation, should use SIOCADDMULTI.
    946 		 * Will definitely break with IPmulticast.
    947 		 */
    948 
    949 		sc->setup_pkt[i][3] = all_es_snpa[i];
    950 		sc->setup_pkt[i][4] = all_is_snpa[i];
    951 		sc->setup_pkt[i][5] = all_l1is_snpa[i];
    952 		sc->setup_pkt[i][6] = all_l2is_snpa[i];
    953 #endif
    954 	}
    955 	if (sc->qe_if.if_flags & IFF_PROMISC) {
    956 		sc->setuplength = QE_PROMISC;
    957 	/* XXX no IFF_ALLMULTI support in 4.4bsd */
    958 	} else if (sc->qe_if.if_flags & IFF_ALLMULTI) {
    959 		sc->setuplength = QE_ALLMULTI;
    960 	} else {
    961 		register int k;
    962 		struct ether_multi *enm;
    963 		struct ether_multistep step;
    964 		/*
    965 		 * Step through our list of multicast addresses, putting them
    966 		 * in the third through fourteenth address slots of the setup
    967 		 * packet.  (See the DEQNA manual to understand the peculiar
    968 		 * layout of the bytes within the setup packet.)  If we have
    969 		 * too many multicast addresses, or if we have to listen to
    970 		 * a range of multicast addresses, turn on reception of all
    971 		 * multicasts.
    972 		 */
    973 		sc->setuplength = QE_SOMEMULTI;
    974 		i = 2;
    975 		k = 0;
    976 		ETHER_FIRST_MULTI(step, &sc->qe_ec, enm);
    977 		while (enm != NULL) {
    978 			if ((++i > 7 && k != 0) ||
    979 			    bcmp(enm->enm_addrlo, enm->enm_addrhi, 6) != 0) {
    980 				sc->setuplength = QE_ALLMULTI;
    981 				break;
    982 			}
    983 			if (i > 7) {
    984 				i = 1;
    985 				k = 8;
    986 			}
    987 			for (j = 0; j < 6; j++)
    988 				sc->setup_pkt[j+k][i] = enm->enm_addrlo[j];
    989 			ETHER_NEXT_MULTI(step, enm);
    990 		}
    991 	}
    992 	sc->setupqueued++;
    993 }
    994 
    995 /*
    996  * Pass a packet to the higher levels.
    997  * We deal with the trailer protocol here.
    998  */
    999 void
   1000 qeread(sc, ifrw, len)
   1001 	register struct qe_softc *sc;
   1002 	struct ifrw *ifrw;
   1003 	int len;
   1004 {
   1005 	struct ether_header *eh;
   1006 	struct mbuf *m;
   1007 
   1008 	/*
   1009 	 * Deal with trailer protocol: if type is INET trailer
   1010 	 * get true type from first 16-bit word past data.
   1011 	 * Remember that type was trailer by setting off.
   1012 	 */
   1013 
   1014 	eh = (struct ether_header *)ifrw->ifrw_addr;
   1015 	if (len == 0)
   1016 		return;
   1017 
   1018 	/*
   1019 	 * Pull packet off interface.  Off is nonzero if packet
   1020 	 * has trailing header; qeget will then force this header
   1021 	 * information to be at the front, but we still have to drop
   1022 	 * the type and length which are at the front of any trailer data.
   1023 	 */
   1024 	m = if_ubaget(&sc->qe_uba, ifrw, len, &sc->qe_if);
   1025 #ifdef notdef
   1026 if (m) {
   1027 *(((u_long *)m->m_data)+0),
   1028 *(((u_long *)m->m_data)+1),
   1029 *(((u_long *)m->m_data)+2),
   1030 *(((u_long *)m->m_data)+3)
   1031 ; }
   1032 #endif
   1033 	if (m == NULL)
   1034 		return;
   1035 
   1036 	/*
   1037 	 * XXX I'll let ragge make this sane.  I'm not entirely
   1038 	 * XXX sure what's going on in if_ubaget().
   1039 	 */
   1040 	M_PREPEND(m, sizeof(struct ether_header), M_DONTWAIT);
   1041 	if (m == NULL)
   1042 		return;
   1043 	bcopy(eh, mtod(m, caddr_t), sizeof(struct ether_header));
   1044 
   1045 #if NBPFILTER > 0
   1046 	/*
   1047 	 * Check for a BPF filter; if so, hand it up.
   1048 	 * Note that we have to stick an extra mbuf up front, because
   1049 	 * bpf_mtap expects to have the ether header at the front.
   1050 	 * It doesn't matter that this results in an ill-formatted mbuf chain,
   1051 	 * since BPF just looks at the data.  (It doesn't try to free the mbuf,
   1052 	 * tho' it will make a copy for tcpdump.)
   1053 	 */
   1054 	if (sc->qe_if.if_bpf) {
   1055 		/* Pass it up */
   1056 		bpf_mtap(sc->qe_if.if_bpf, m);
   1057 
   1058 		/*
   1059 		 * Note that the interface cannot be in promiscuous mode if
   1060 		 * there are no BPF listeners.	And if we are in promiscuous
   1061 		 * mode, we have to check if this packet is really ours.
   1062 		 */
   1063 		if ((sc->qe_if.if_flags & IFF_PROMISC) &&
   1064 		    (eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */
   1065 		    bcmp(eh->ether_dhost, LLADDR(sc->qe_if.if_sadl),
   1066 			    sizeof(eh->ether_dhost)) != 0) {
   1067 			m_freem(m);
   1068 			return;
   1069 		}
   1070 	}
   1071 #endif /* NBPFILTER > 0 */
   1072 
   1073 	(*sc->qe_if.if_input)(&sc->qe_if, m);
   1074 }
   1075 
   1076 /*
   1077  * Watchdog timeout routine. There is a condition in the hardware that
   1078  * causes the board to lock up under heavy load. This routine detects
   1079  * the hang up and restarts the device.
   1080  */
   1081 void
   1082 qetimeout(ifp)
   1083 	struct ifnet *ifp;
   1084 {
   1085 	register struct qe_softc *sc = ifp->if_softc;
   1086 
   1087 #ifdef notdef
   1088 	log(LOG_ERR, "%s: transmit timeout, restarted %d\n",
   1089 	     sc->sc_dev.dv_xname, sc->qe_restarts++);
   1090 #endif
   1091 	qerestart(sc);
   1092 }
   1093 /*
   1094  * Restart for board lockup problem.
   1095  */
   1096 void
   1097 qerestart(sc)
   1098 	struct qe_softc *sc;
   1099 {
   1100 	register struct ifnet *ifp = (struct ifnet *)&sc->qe_if;
   1101 	register struct qedevice *addr = sc->addr;
   1102 	register struct qe_ring *rp;
   1103 	register int i;
   1104 
   1105 	addr->qe_csr = QE_RESET;
   1106 	addr->qe_csr &= ~QE_RESET;
   1107 	qesetup(sc);
   1108 	for (i = 0, rp = sc->tring; i < NXMT; rp++, i++) {
   1109 		rp->qe_flag = rp->qe_status1 = QE_NOTYET;
   1110 		rp->qe_valid = 0;
   1111 	}
   1112 	sc->nxmit = sc->otindex = sc->tindex = sc->rindex = 0;
   1113 	addr->qe_csr = QE_RCV_ENABLE | QE_INT_ENABLE | QE_XMIT_INT |
   1114 	    QE_RCV_INT | QE_ILOOP;
   1115 	addr->qe_rcvlist_lo = (short)((int)sc->rringaddr);
   1116 	addr->qe_rcvlist_hi = (short)((int)sc->rringaddr >> 16);
   1117 	sc->qe_flags |= QEF_RUNNING;
   1118 	qestart(ifp);
   1119 }
   1120