xref: /csrg-svn/sys/vax/if/if_qe.c (revision 38985)
1 /*
2  * Copyright (c) 1988 Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Digital Equipment Corp.
7  *
8  * Redistribution and use in source and binary forms are permitted
9  * provided that the above copyright notice and this paragraph are
10  * duplicated in all such forms and that any documentation,
11  * advertising materials, and other materials related to such
12  * distribution and use acknowledge that the software was developed
13  * by the University of California, Berkeley.  The name of the
14  * University may not be used to endorse or promote products derived
15  * from this software without specific prior written permission.
16  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19  *
20  *	@(#)if_qe.c	7.12 (Berkeley) 09/04/89
21  */
22 
23 /* from  @(#)if_qe.c	1.15	(ULTRIX)	4/16/86 */
24 
25 /****************************************************************
26  *								*
27  *        Licensed from Digital Equipment Corporation 		*
28  *                       Copyright (c) 				*
29  *               Digital Equipment Corporation			*
30  *                   Maynard, Massachusetts 			*
31  *                         1985, 1986 				*
32  *                    All rights reserved. 			*
33  *								*
34  *        The Information in this software is subject to change *
35  *   without notice and should not be construed as a commitment *
36  *   by  Digital  Equipment  Corporation.   Digital   makes  no *
37  *   representations about the suitability of this software for *
38  *   any purpose.  It is supplied "As Is" without expressed  or *
39  *   implied  warranty. 					*
40  *								*
41  *        If the Regents of the University of California or its *
42  *   licensees modify the software in a manner creating  	*
43  *   derivative copyright rights, appropriate copyright  	*
44  *   legends may be placed on the derivative work in addition   *
45  *   to that set forth above. 					*
46  *								*
47  ****************************************************************/
48 /* ---------------------------------------------------------------------
49  * Modification History
50  *
51  * 15-Apr-86  -- afd
52  *	Rename "unused_multi" to "qunused_multi" for extending Generic
53  *	kernel to MicroVAXen.
54  *
55  * 18-mar-86  -- jaw     br/cvec changed to NOT use registers.
56  *
57  * 12 March 86 -- Jeff Chase
58  *	Modified to handle the new MCLGET macro
59  *	Changed if_qe_data.c to use more receive buffers
60  *	Added a flag to poke with adb to log qe_restarts on console
61  *
62  * 19 Oct 85 -- rjl
63  *	Changed the watch dog timer from 30 seconds to 3.  VMS is using
64  * 	less than 1 second in their's. Also turned the printf into an
65  *	mprintf.
66  *
67  *  09/16/85 -- Larry Cohen
68  * 		Add 43bsd alpha tape changes for subnet routing
69  *
70  *  1 Aug 85 -- rjl
71  *	Panic on a non-existent memory interrupt and the case where a packet
72  *	was chained.  The first should never happen because non-existant
73  *	memory interrupts cause a bus reset. The second should never happen
74  *	because we hang 2k input buffers on the device.
75  *
76  *  1 Aug 85 -- rich
77  *      Fixed the broadcast loopback code to handle Clusters without
78  *      wedging the system.
79  *
80  *  27 Feb. 85 -- ejf
81  *	Return default hardware address on ioctl request.
82  *
83  *  12 Feb. 85 -- ejf
84  *	Added internal extended loopback capability.
85  *
86  *  27 Dec. 84 -- rjl
87  *	Fixed bug that caused every other transmit descriptor to be used
88  *	instead of every descriptor.
89  *
90  *  21 Dec. 84 -- rjl
91  *	Added watchdog timer to mask hardware bug that causes device lockup.
92  *
93  *  18 Dec. 84 -- rjl
94  *	Reworked driver to use q-bus mapping routines.  MicroVAX-I now does
95  *	copying instead of m-buf shuffleing.
96  *	A number of deficencies in the hardware/firmware were compensated
97  *	for. See comments in qestart and qerint.
98  *
99  *  14 Nov. 84 -- jf
100  *	Added usage counts for multicast addresses.
101  *	Updated general protocol support to allow access to the Ethernet
102  *	header.
103  *
104  *  04 Oct. 84 -- jf
105  *	Added support for new ioctls to add and delete multicast addresses
106  *	and set the physical address.
107  *	Add support for general protocols.
108  *
109  *  14 Aug. 84 -- rjl
110  *	Integrated Shannon changes. (allow arp above 1024 and ? )
111  *
112  *  13 Feb. 84 -- rjl
113  *
114  *	Initial version of driver. derived from IL driver.
115  *
116  * ---------------------------------------------------------------------
117  */
118 
119 #include "qe.h"
120 #if	NQE > 0
121 /*
122  * Digital Q-BUS to NI Adapter
123  */
124 #include "param.h"
125 #include "systm.h"
126 #include "mbuf.h"
127 #include "buf.h"
128 #include "protosw.h"
129 #include "socket.h"
130 #include "vmmac.h"
131 #include "ioctl.h"
132 #include "errno.h"
133 #include "syslog.h"
134 #include "time.h"
135 #include "kernel.h"
136 
137 #include "../net/if.h"
138 #include "../net/netisr.h"
139 #include "../net/route.h"
140 
141 #ifdef INET
142 #include "../netinet/in.h"
143 #include "../netinet/in_systm.h"
144 #include "../netinet/in_var.h"
145 #include "../netinet/ip.h"
146 #include "../netinet/if_ether.h"
147 #endif
148 
149 #ifdef NS
150 #include "../netns/ns.h"
151 #include "../netns/ns_if.h"
152 #endif
153 
154 #ifdef ISO
155 #include "../netiso/iso.h"
156 #include "../netiso/iso_var.h"
157 #include "../netiso/iso_snpac.h"
158 extern struct snpa_cache all_es, all_is;
159 #endif
160 
161 #include "../vax/pte.h"
162 #include "../vax/cpu.h"
163 #include "../vax/mtpr.h"
164 #include "if_qereg.h"
165 #include "if_uba.h"
166 #include "../vaxuba/ubareg.h"
167 #include "../vaxuba/ubavar.h"
168 
169 #if NQE == 1 && !defined(QNIVERT)
170 #define NRCV	15	 		/* Receive descriptors		*/
171 #else
172 #define NRCV	10	 		/* Receive descriptors		*/
173 #endif
174 #define NXMT	5	 		/* Transmit descriptors		*/
175 #define NTOT	(NXMT + NRCV)
176 
177 #define	QETIMEOUT	2		/* transmit timeout, must be > 1 */
178 
179 /*
180  * This constant should really be 60 because the qna adds 4 bytes of crc.
181  * However when set to 60 our packets are ignored by deuna's , 3coms are
182  * okay ??????????????????????????????????????????
183  */
184 #define MINDATA 64
185 
186 /*
187  * Ethernet software status per interface.
188  *
189  * Each interface is referenced by a network interface structure,
190  * qe_if, which the routing code uses to locate the interface.
191  * This structure contains the output queue for the interface, its address, ...
192  */
193 struct	qe_softc {
194 	struct	arpcom qe_ac;		/* Ethernet common part 	*/
195 #define	qe_if	qe_ac.ac_if		/* network-visible interface 	*/
196 #define	qe_addr	qe_ac.ac_enaddr		/* hardware Ethernet address 	*/
197 	struct	ifubinfo qe_uba;	/* Q-bus resources 		*/
198 	struct	ifrw qe_ifr[NRCV];	/*	for receive buffers;	*/
199 	struct	ifxmt qe_ifw[NXMT];	/*	for xmit buffers;	*/
200 	int	qe_flags;		/* software state		*/
201 #define	QEF_RUNNING	0x01
202 #define	QEF_SETADDR	0x02
203 	int	setupaddr;		/* mapping info for setup pkts  */
204 	int	ipl;			/* interrupt priority		*/
205 	struct	qe_ring *rringaddr;	/* mapping info for rings	*/
206 	struct	qe_ring *tringaddr;	/*       ""			*/
207 	struct	qe_ring rring[NRCV+1];	/* Receive ring descriptors 	*/
208 	struct	qe_ring tring[NXMT+1];	/* Transmit ring descriptors 	*/
209 	u_char	setup_pkt[16][8];	/* Setup packet			*/
210 	int	rindex;			/* Receive index		*/
211 	int	tindex;			/* Transmit index		*/
212 	int	otindex;		/* Old transmit index		*/
213 	int	qe_intvec;		/* Interrupt vector 		*/
214 	struct	qedevice *addr;		/* device addr			*/
215 	int 	setupqueued;		/* setup packet queued		*/
216 	int	nxmit;			/* Transmits in progress	*/
217 	int	qe_restarts;		/* timeouts			*/
218 } qe_softc[NQE];
219 
220 struct	uba_device *qeinfo[NQE];
221 
222 extern struct timeval time;
223 
224 int	qeprobe(), qeattach(), qeintr(), qetimeout();
225 int	qeinit(), qeioctl(), qereset(), qestart();
226 
227 u_short qestd[] = { 0 };
228 struct	uba_driver qedriver =
229 	{ qeprobe, 0, qeattach, 0, qestd, "qe", qeinfo };
230 
231 #define	QEUNIT(x)	minor(x)
232 /*
233  * The deqna shouldn't receive more than ETHERMTU + sizeof(struct ether_header)
234  * but will actually take in up to 2048 bytes. To guard against the receiver
235  * chaining buffers (which we aren't prepared to handle) we allocate 2kb
236  * size buffers.
237  */
238 #define MAXPACKETSIZE 2048		/* Should really be ETHERMTU	*/
239 /*
240  * Probe the QNA to see if it's there
241  */
242 qeprobe(reg, ui)
243 	caddr_t reg;
244 	struct uba_device *ui;
245 {
246 	register int br, cvec;		/* r11, r10 value-result */
247 	register struct qedevice *addr = (struct qedevice *)reg;
248 	register struct qe_ring *rp;
249 	register struct qe_ring *prp; 	/* physical rp 		*/
250 	register int i;
251 	register struct qe_softc *sc = &qe_softc[ui->ui_unit];
252 
253 #ifdef lint
254 	br = 0; cvec = br; br = cvec;
255 	qeintr(0);
256 #endif
257 
258 	/*
259 	 * The QNA interrupts on i/o operations. To do an I/O operation
260 	 * we have to setup the interface by transmitting a setup  packet.
261 	 */
262 	addr->qe_csr = QE_RESET;
263 	addr->qe_csr &= ~QE_RESET;
264 	addr->qe_vector = (uba_hd[numuba].uh_lastiv -= 4);
265 
266 	/*
267 	 * Map the communications area and the setup packet.
268 	 */
269 	sc->setupaddr =
270 		uballoc(0, (caddr_t)sc->setup_pkt, sizeof(sc->setup_pkt), 0);
271 	sc->rringaddr = (struct qe_ring *) uballoc(0, (caddr_t)sc->rring,
272 		sizeof(struct qe_ring) * (NTOT+2), 0);
273 	prp = (struct qe_ring *)UBAI_ADDR((int)sc->rringaddr);
274 
275 	/*
276 	 * The QNA will loop the setup packet back to the receive ring
277 	 * for verification, therefore we initialize the first
278 	 * receive & transmit ring descriptors and link the setup packet
279 	 * to them.
280 	 */
281 	qeinitdesc(sc->tring, (caddr_t)UBAI_ADDR(sc->setupaddr),
282 	    sizeof(sc->setup_pkt));
283 	qeinitdesc(sc->rring, (caddr_t)UBAI_ADDR(sc->setupaddr),
284 	    sizeof(sc->setup_pkt));
285 
286 	rp = (struct qe_ring *)sc->tring;
287 	rp->qe_setup = 1;
288 	rp->qe_eomsg = 1;
289 	rp->qe_flag = rp->qe_status1 = QE_NOTYET;
290 	rp->qe_valid = 1;
291 
292 	rp = (struct qe_ring *)sc->rring;
293 	rp->qe_flag = rp->qe_status1 = QE_NOTYET;
294 	rp->qe_valid = 1;
295 
296 	/*
297 	 * Get the addr off of the interface and place it into the setup
298 	 * packet. This code looks strange due to the fact that the address
299 	 * is placed in the setup packet in col. major order.
300 	 */
301 	for( i = 0 ; i < 6 ; i++ )
302 		sc->setup_pkt[i][1] = addr->qe_sta_addr[i];
303 
304 	qesetup( sc );
305 	/*
306 	 * Start the interface and wait for the packet.
307 	 */
308 	(void) spl6();
309 	addr->qe_csr = QE_INT_ENABLE | QE_XMIT_INT | QE_RCV_INT;
310 	addr->qe_rcvlist_lo = (short)prp;
311 	addr->qe_rcvlist_hi = (short)((int)prp >> 16);
312 	prp += NRCV+1;
313 	addr->qe_xmtlist_lo = (short)prp;
314 	addr->qe_xmtlist_hi = (short)((int)prp >> 16);
315 	DELAY(10000);
316 	/*
317 	 * All done with the bus resources.
318 	 */
319 	ubarelse(0, &sc->setupaddr);
320 	ubarelse(0, (int *)&sc->rringaddr);
321 	sc->ipl = br = qbgetpri();
322 	return( sizeof(struct qedevice) );
323 }
324 
325 /*
326  * Interface exists: make available by filling in network interface
327  * record.  System will initialize the interface when it is ready
328  * to accept packets.
329  */
330 qeattach(ui)
331 	struct uba_device *ui;
332 {
333 	register struct qe_softc *sc = &qe_softc[ui->ui_unit];
334 	register struct ifnet *ifp = &sc->qe_if;
335 	register struct qedevice *addr = (struct qedevice *)ui->ui_addr;
336 	register int i;
337 
338 	ifp->if_unit = ui->ui_unit;
339 	ifp->if_name = "qe";
340 	ifp->if_mtu = ETHERMTU;
341 	ifp->if_flags = IFF_BROADCAST;
342 
343 	/*
344 	 * Read the address from the prom and save it.
345 	 */
346 	for( i=0 ; i<6 ; i++ )
347 		sc->setup_pkt[i][1] = sc->qe_addr[i] = addr->qe_sta_addr[i] & 0xff;
348 	addr->qe_vector |= 1;
349 	printf("qe%d: %s, hardware address %s\n", ui->ui_unit,
350 		addr->qe_vector&01 ? "delqa":"deqna",
351 		ether_sprintf(sc->qe_addr));
352 	addr->qe_vector &= ~1;
353 
354 	/*
355 	 * Save the vector for initialization at reset time.
356 	 */
357 	sc->qe_intvec = addr->qe_vector;
358 
359 	ifp->if_init = qeinit;
360 	ifp->if_output = ether_output;
361 	ifp->if_start = qestart;
362 	ifp->if_ioctl = qeioctl;
363 	ifp->if_reset = qereset;
364 	ifp->if_watchdog = qetimeout;
365 	sc->qe_uba.iff_flags = UBA_CANTWAIT;
366 	if_attach(ifp);
367 }
368 
369 /*
370  * Reset of interface after UNIBUS reset.
371  * If interface is on specified uba, reset its state.
372  */
373 qereset(unit, uban)
374 	int unit, uban;
375 {
376 	register struct uba_device *ui;
377 
378 	if (unit >= NQE || (ui = qeinfo[unit]) == 0 || ui->ui_alive == 0 ||
379 		ui->ui_ubanum != uban)
380 		return;
381 	printf(" qe%d", unit);
382 	qe_softc[unit].qe_if.if_flags &= ~IFF_RUNNING;
383 	qeinit(unit);
384 }
385 
386 /*
387  * Initialization of interface.
388  */
389 qeinit(unit)
390 	int unit;
391 {
392 	register struct qe_softc *sc = &qe_softc[unit];
393 	register struct uba_device *ui = qeinfo[unit];
394 	register struct qedevice *addr = (struct qedevice *)ui->ui_addr;
395 	register struct ifnet *ifp = &sc->qe_if;
396 	register i;
397 	int s;
398 
399 	/* address not known */
400 	if (ifp->if_addrlist == (struct ifaddr *)0)
401 			return;
402 	if (sc->qe_flags & QEF_RUNNING)
403 		return;
404 
405 	if ((ifp->if_flags & IFF_RUNNING) == 0) {
406 		/*
407 		 * map the communications area onto the device
408 		 */
409 		i = uballoc(0, (caddr_t)sc->rring,
410 		    sizeof(struct qe_ring) * (NTOT+2), 0);
411 		if (i == 0)
412 			goto fail;
413 		sc->rringaddr = (struct qe_ring *)UBAI_ADDR(i);
414 		sc->tringaddr = sc->rringaddr + NRCV + 1;
415 		i = uballoc(0, (caddr_t)sc->setup_pkt,
416 		    sizeof(sc->setup_pkt), 0);
417 		if (i == 0)
418 			goto fail;
419 		sc->setupaddr =	UBAI_ADDR(i);
420 		/*
421 		 * init buffers and maps
422 		 */
423 		if (if_ubaminit(&sc->qe_uba, ui->ui_ubanum,
424 		    sizeof (struct ether_header), (int)btoc(MAXPACKETSIZE),
425 		    sc->qe_ifr, NRCV, sc->qe_ifw, NXMT) == 0) {
426 	fail:
427 			printf("qe%d: can't allocate uba resources\n", unit);
428 			sc->qe_if.if_flags &= ~IFF_UP;
429 			return;
430 		}
431 	}
432 	/*
433 	 * Init the buffer descriptors and indexes for each of the lists and
434 	 * loop them back to form a ring.
435 	 */
436 	for (i = 0; i < NRCV; i++) {
437 		qeinitdesc( &sc->rring[i],
438 		    (caddr_t)UBAI_ADDR(sc->qe_ifr[i].ifrw_info), MAXPACKETSIZE);
439 		sc->rring[i].qe_flag = sc->rring[i].qe_status1 = QE_NOTYET;
440 		sc->rring[i].qe_valid = 1;
441 	}
442 	qeinitdesc(&sc->rring[i], (caddr_t)NULL, 0);
443 
444 	sc->rring[i].qe_addr_lo = (short)sc->rringaddr;
445 	sc->rring[i].qe_addr_hi = (short)((int)sc->rringaddr >> 16);
446 	sc->rring[i].qe_chain = 1;
447 	sc->rring[i].qe_flag = sc->rring[i].qe_status1 = QE_NOTYET;
448 	sc->rring[i].qe_valid = 1;
449 
450 	for( i = 0 ; i <= NXMT ; i++ )
451 		qeinitdesc(&sc->tring[i], (caddr_t)NULL, 0);
452 	i--;
453 
454 	sc->tring[i].qe_addr_lo = (short)sc->tringaddr;
455 	sc->tring[i].qe_addr_hi = (short)((int)sc->tringaddr >> 16);
456 	sc->tring[i].qe_chain = 1;
457 	sc->tring[i].qe_flag = sc->tring[i].qe_status1 = QE_NOTYET;
458 	sc->tring[i].qe_valid = 1;
459 
460 	sc->nxmit = sc->otindex = sc->tindex = sc->rindex = 0;
461 
462 	/*
463 	 * Take the interface out of reset, program the vector,
464 	 * enable interrupts, and tell the world we are up.
465 	 */
466 	s = splimp();
467 	addr->qe_vector = sc->qe_intvec;
468 	sc->addr = addr;
469 	addr->qe_csr = QE_RCV_ENABLE | QE_INT_ENABLE | QE_XMIT_INT |
470 	    QE_RCV_INT | QE_ILOOP;
471 	addr->qe_rcvlist_lo = (short)sc->rringaddr;
472 	addr->qe_rcvlist_hi = (short)((int)sc->rringaddr >> 16);
473 	ifp->if_flags |= IFF_UP | IFF_RUNNING;
474 	sc->qe_flags |= QEF_RUNNING;
475 	qesetup( sc );
476 	(void) qestart( ifp );
477 	splx( s );
478 }
479 
480 /*
481  * Start output on interface.
482  *
483  */
484 qestart(ifp)
485 	struct ifnet *ifp;
486 {
487 	int unit =  ifp->if_unit;
488 	struct uba_device *ui = qeinfo[unit];
489 	register struct qe_softc *sc = &qe_softc[unit];
490 	register struct qedevice *addr;
491 	register struct qe_ring *rp;
492 	register index;
493 	struct mbuf *m;
494 	int buf_addr, len, s;
495 
496 
497 	s = splimp();
498 	addr = (struct qedevice *)ui->ui_addr;
499 	/*
500 	 * The deqna doesn't look at anything but the valid bit
501 	 * to determine if it should transmit this packet. If you have
502 	 * a ring and fill it the device will loop indefinately on the
503 	 * packet and continue to flood the net with packets until you
504 	 * break the ring. For this reason we never queue more than n-1
505 	 * packets in the transmit ring.
506 	 *
507 	 * The microcoders should have obeyed their own defination of the
508 	 * flag and status words, but instead we have to compensate.
509 	 */
510 	for( index = sc->tindex;
511 		sc->tring[index].qe_valid == 0 && sc->nxmit < (NXMT-1) ;
512 		sc->tindex = index = ++index % NXMT){
513 		rp = &sc->tring[index];
514 		if( sc->setupqueued ) {
515 			buf_addr = sc->setupaddr;
516 			len = 128;
517 			rp->qe_setup = 1;
518 			sc->setupqueued = 0;
519 		} else {
520 			IF_DEQUEUE(&sc->qe_if.if_snd, m);
521 			if( m == 0 ){
522 				splx(s);
523 				return (0);
524 			}
525 			buf_addr = sc->qe_ifw[index].ifw_info;
526 			len = if_ubaput(&sc->qe_uba, &sc->qe_ifw[index], m);
527 		}
528 		/*
529 		 *  Does buffer end on odd byte ?
530 		 */
531 		if( len & 1 ) {
532 			len++;
533 			rp->qe_odd_end = 1;
534 		}
535 		if( len < MINDATA )
536 			len = MINDATA;
537 		rp->qe_buf_len = -(len/2);
538 		buf_addr = UBAI_ADDR(buf_addr);
539 		rp->qe_flag = rp->qe_status1 = QE_NOTYET;
540 		rp->qe_addr_lo = (short)buf_addr;
541 		rp->qe_addr_hi = (short)(buf_addr >> 16);
542 		rp->qe_eomsg = 1;
543 		rp->qe_flag = rp->qe_status1 = QE_NOTYET;
544 		rp->qe_valid = 1;
545 		if (sc->nxmit++ == 0)
546 			sc->qe_if.if_timer = QETIMEOUT;
547 
548 		/*
549 		 * See if the xmit list is invalid.
550 		 */
551 		if( addr->qe_csr & QE_XL_INVALID ) {
552 			buf_addr = (int)(sc->tringaddr+index);
553 			addr->qe_xmtlist_lo = (short)buf_addr;
554 			addr->qe_xmtlist_hi = (short)(buf_addr >> 16);
555 		}
556 	}
557 	splx( s );
558 	return (0);
559 }
560 
561 /*
562  * Ethernet interface interrupt processor
563  */
564 qeintr(unit)
565 	int unit;
566 {
567 	register struct qe_softc *sc = &qe_softc[unit];
568 	struct qedevice *addr = (struct qedevice *)qeinfo[unit]->ui_addr;
569 	int buf_addr, csr;
570 
571 #ifdef notdef
572 	splx(sc->ipl);
573 #else
574 	(void) splimp();
575 #endif
576 	csr = addr->qe_csr;
577 	addr->qe_csr = QE_RCV_ENABLE | QE_INT_ENABLE | QE_XMIT_INT | QE_RCV_INT | QE_ILOOP;
578 	if( csr & QE_RCV_INT )
579 		qerint( unit );
580 	if( csr & QE_XMIT_INT )
581 		qetint( unit );
582 	if( csr & QE_NEX_MEM_INT )
583 		printf("qe%d: Nonexistent memory interrupt\n", unit);
584 
585 	if( addr->qe_csr & QE_RL_INVALID && sc->rring[sc->rindex].qe_status1 == QE_NOTYET ) {
586 		buf_addr = (int)&sc->rringaddr[sc->rindex];
587 		addr->qe_rcvlist_lo = (short)buf_addr;
588 		addr->qe_rcvlist_hi = (short)(buf_addr >> 16);
589 	}
590 }
591 
592 /*
593  * Ethernet interface transmit interrupt.
594  */
595 
596 qetint(unit)
597 	int unit;
598 {
599 	register struct qe_softc *sc = &qe_softc[unit];
600 	register struct qe_ring *rp;
601 	register struct ifxmt *ifxp;
602 	int status1, setupflag;
603 	short len;
604 
605 
606 	while( sc->otindex != sc->tindex && sc->tring[sc->otindex].qe_status1 != QE_NOTYET && sc->nxmit > 0 ) {
607 		/*
608 		 * Save the status words from the descriptor so that it can
609 		 * be released.
610 		 */
611 		rp = &sc->tring[sc->otindex];
612 		status1 = rp->qe_status1;
613 		setupflag = rp->qe_setup;
614 		len = (-rp->qe_buf_len) * 2;
615 		if( rp->qe_odd_end )
616 			len++;
617 		/*
618 		 * Init the buffer descriptor
619 		 */
620 		bzero((caddr_t)rp, sizeof(struct qe_ring));
621 		if( --sc->nxmit == 0 )
622 			sc->qe_if.if_timer = 0;
623 		if( !setupflag ) {
624 			/*
625 			 * Do some statistics.
626 			 */
627 			sc->qe_if.if_opackets++;
628 			sc->qe_if.if_collisions += ( status1 & QE_CCNT ) >> 4;
629 			if (status1 & QE_ERROR)
630 				sc->qe_if.if_oerrors++;
631 			/*
632 			 * If this was a broadcast packet loop it
633 			 * back because the hardware can't hear its own
634 			 * transmits.
635 			 */
636 			ifxp = &sc->qe_ifw[sc->otindex];
637 			if (bcmp((caddr_t)((struct ether_header *)ifxp->ifw_addr)->ether_dhost,
638 			   (caddr_t)etherbroadcastaddr,
639 			   sizeof(etherbroadcastaddr)) == 0)
640 				qeread(sc, &ifxp->ifrw,
641 				    len - sizeof(struct ether_header));
642 			if (ifxp->ifw_xtofree) {
643 				m_freem(ifxp->ifw_xtofree);
644 				ifxp->ifw_xtofree = 0;
645 			}
646 		}
647 		sc->otindex = ++sc->otindex % NXMT;
648 	}
649 	(void) qestart( &sc->qe_if );
650 }
651 
652 /*
653  * Ethernet interface receiver interrupt.
654  * If can't determine length from type, then have to drop packet.
655  * Othewise decapsulate packet based on type and pass to type specific
656  * higher-level input routine.
657  */
658 qerint(unit)
659 	int unit;
660 {
661 	register struct qe_softc *sc = &qe_softc[unit];
662 	register struct qe_ring *rp;
663 	int len, status1, status2;
664 	int bufaddr;
665 
666 	/*
667 	 * Traverse the receive ring looking for packets to pass back.
668 	 * The search is complete when we find a descriptor not in use.
669 	 *
670 	 * As in the transmit case the deqna doesn't honor it's own protocols
671 	 * so there exists the possibility that the device can beat us around
672 	 * the ring. The proper way to guard against this is to insure that
673 	 * there is always at least one invalid descriptor. We chose instead
674 	 * to make the ring large enough to minimize the problem. With a ring
675 	 * size of 4 we haven't been able to see the problem. To be safe we
676 	 * doubled that to 8.
677 	 *
678 	 */
679 	for( ; sc->rring[sc->rindex].qe_status1 != QE_NOTYET ; sc->rindex = ++sc->rindex % NRCV ){
680 		rp = &sc->rring[sc->rindex];
681 		status1 = rp->qe_status1;
682 		status2 = rp->qe_status2;
683 		bzero((caddr_t)rp, sizeof(struct qe_ring));
684 		if( (status1 & QE_MASK) == QE_MASK )
685 			panic("qe: chained packet");
686 		len = ((status1 & QE_RBL_HI) | (status2 & QE_RBL_LO)) + 60;
687 		sc->qe_if.if_ipackets++;
688 
689 		if (status1 & QE_ERROR) {
690 			if ((status1 & QE_RUNT) == 0)
691 				sc->qe_if.if_ierrors++;
692 		} else {
693 			/*
694 			 * We don't process setup packets.
695 			 */
696 			if( !(status1 & QE_ESETUP) )
697 				qeread(sc, &sc->qe_ifr[sc->rindex],
698 					len - sizeof(struct ether_header));
699 		}
700 		/*
701 		 * Return the buffer to the ring
702 		 */
703 		bufaddr = (int)UBAI_ADDR(sc->qe_ifr[sc->rindex].ifrw_info);
704 		rp->qe_buf_len = -((MAXPACKETSIZE)/2);
705 		rp->qe_addr_lo = (short)bufaddr;
706 		rp->qe_addr_hi = (short)((int)bufaddr >> 16);
707 		rp->qe_flag = rp->qe_status1 = QE_NOTYET;
708 		rp->qe_valid = 1;
709 	}
710 }
711 
712 /*
713  * Process an ioctl request.
714  */
715 qeioctl(ifp, cmd, data)
716 	register struct ifnet *ifp;
717 	int cmd;
718 	caddr_t data;
719 {
720 	struct qe_softc *sc = &qe_softc[ifp->if_unit];
721 	struct ifaddr *ifa = (struct ifaddr *)data;
722 	int s = splimp(), error = 0;
723 
724 	switch (cmd) {
725 
726 	case SIOCSIFADDR:
727 		ifp->if_flags |= IFF_UP;
728 		qeinit(ifp->if_unit);
729 		switch(ifa->ifa_addr->sa_family) {
730 #ifdef INET
731 		case AF_INET:
732 			((struct arpcom *)ifp)->ac_ipaddr =
733 				IA_SIN(ifa)->sin_addr;
734 			arpwhohas((struct arpcom *)ifp, &IA_SIN(ifa)->sin_addr);
735 			break;
736 #endif
737 #ifdef NS
738 		case AF_NS:
739 		    {
740 			register struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
741 
742 			if (ns_nullhost(*ina))
743 				ina->x_host = *(union ns_host *)(sc->qe_addr);
744 			else
745 				qe_setaddr(ina->x_host.c_host, ifp->if_unit);
746 			break;
747 		    }
748 #endif
749 		}
750 		break;
751 
752 	case SIOCSIFFLAGS:
753 		if ((ifp->if_flags & IFF_UP) == 0 &&
754 		    sc->qe_flags & QEF_RUNNING) {
755 			((struct qedevice *)
756 			   (qeinfo[ifp->if_unit]->ui_addr))->qe_csr = QE_RESET;
757 			sc->qe_flags &= ~QEF_RUNNING;
758 		} else if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) ==
759 		    IFF_RUNNING && (sc->qe_flags & QEF_RUNNING) == 0)
760 			qerestart(sc);
761 		break;
762 
763 	default:
764 		error = EINVAL;
765 
766 	}
767 	splx(s);
768 	return (error);
769 }
770 
771 /*
772  * set ethernet address for unit
773  */
774 qe_setaddr(physaddr, unit)
775 	u_char *physaddr;
776 	int unit;
777 {
778 	register struct qe_softc *sc = &qe_softc[unit];
779 	register int i;
780 
781 	for (i = 0; i < 6; i++)
782 		sc->setup_pkt[i][1] = sc->qe_addr[i] = physaddr[i];
783 	sc->qe_flags |= QEF_SETADDR;
784 	if (sc->qe_if.if_flags & IFF_RUNNING)
785 		qesetup(sc);
786 	qeinit(unit);
787 }
788 
789 
790 /*
791  * Initialize a ring descriptor with mbuf allocation side effects
792  */
793 qeinitdesc(rp, addr, len)
794 	register struct qe_ring *rp;
795 	caddr_t addr; 			/* mapped address */
796 	int len;
797 {
798 	/*
799 	 * clear the entire descriptor
800 	 */
801 	bzero((caddr_t)rp, sizeof(struct qe_ring));
802 
803 	if( len ) {
804 		rp->qe_buf_len = -(len/2);
805 		rp->qe_addr_lo = (short)addr;
806 		rp->qe_addr_hi = (short)((int)addr >> 16);
807 	}
808 }
809 /*
810  * Build a setup packet - the physical address will already be present
811  * in first column.
812  */
813 qesetup( sc )
814 struct qe_softc *sc;
815 {
816 	register i, j;
817 
818 	/*
819 	 * Copy the target address to the rest of the entries in this row.
820 	 */
821 	 for ( j = 0; j < 6 ; j++ )
822 		for ( i = 2 ; i < 8 ; i++ )
823 			sc->setup_pkt[j][i] = sc->setup_pkt[j][1];
824 	/*
825 	 * Duplicate the first half.
826 	 */
827 	bcopy((caddr_t)sc->setup_pkt[0], (caddr_t)sc->setup_pkt[8], 64);
828 	/*
829 	 * Fill in the broadcast (and ISO multicast) address(es).
830 	 */
831 	for ( i = 0; i < 6 ; i++ ) {
832 		sc->setup_pkt[i][2] = 0xff;
833 #ifdef ISO
834 		sc->setup_pkt[i][3] = all_es.sc_snpa[i];
835 		sc->setup_pkt[i][4] = all_is.sc_snpa[i];
836 #endif
837 	}
838 	sc->setupqueued++;
839 }
840 
841 /*
842  * Pass a packet to the higher levels.
843  * We deal with the trailer protocol here.
844  */
845 qeread(sc, ifrw, len)
846 	register struct qe_softc *sc;
847 	struct ifrw *ifrw;
848 	int len;
849 {
850 	struct ether_header *eh;
851     	struct mbuf *m;
852 	int off, resid, s;
853 	struct ifqueue *inq;
854 
855 	/*
856 	 * Deal with trailer protocol: if type is INET trailer
857 	 * get true type from first 16-bit word past data.
858 	 * Remember that type was trailer by setting off.
859 	 */
860 
861 	eh = (struct ether_header *)ifrw->ifrw_addr;
862 	eh->ether_type = ntohs((u_short)eh->ether_type);
863 #define	qedataaddr(eh, off, type)	((type)(((caddr_t)((eh)+1)+(off))))
864 	if (eh->ether_type >= ETHERTYPE_TRAIL &&
865 	    eh->ether_type < ETHERTYPE_TRAIL+ETHERTYPE_NTRAILER) {
866 		off = (eh->ether_type - ETHERTYPE_TRAIL) * 512;
867 		if (off >= ETHERMTU)
868 			return;		/* sanity */
869 		eh->ether_type = ntohs(*qedataaddr(eh,off, u_short *));
870 		resid = ntohs(*(qedataaddr(eh, off+2, u_short *)));
871 		if (off + resid > len)
872 		     return;		/* sanity */
873 		len = off + resid;
874 	} else
875 		off = 0;
876 	if (len == 0)
877 		return;
878 
879 	/*
880 	 * Pull packet off interface.  Off is nonzero if packet
881 	 * has trailing header; qeget will then force this header
882 	 * information to be at the front, but we still have to drop
883 	 * the type and length which are at the front of any trailer data.
884 	 */
885 	m = if_ubaget(&sc->qe_uba, ifrw, len, off, &sc->qe_if);
886 
887 	if (m)
888 		ether_input(&sc->qe_if, eh, m);
889 }
890 
891 /*
892  * Watchdog timeout routine. There is a condition in the hardware that
893  * causes the board to lock up under heavy load. This routine detects
894  * the hang up and restarts the device.
895  */
896 qetimeout(unit)
897 	int unit;
898 {
899 	register struct qe_softc *sc;
900 
901 	sc = &qe_softc[unit];
902 	log(LOG_ERR, "qe%d: transmit timeout, restarted %d\n",
903 	     unit, sc->qe_restarts++);
904 	qerestart(sc);
905 }
906 /*
907  * Restart for board lockup problem.
908  */
909 qerestart(sc)
910 	register struct qe_softc *sc;
911 {
912 	register struct ifnet *ifp = &sc->qe_if;
913 	register struct qedevice *addr = sc->addr;
914 	register struct qe_ring *rp;
915 	register i;
916 
917 	addr->qe_csr = QE_RESET;
918 	addr->qe_csr &= ~QE_RESET;
919 	qesetup( sc );
920 	for (i = 0, rp = sc->tring; i < NXMT; rp++, i++) {
921 		rp->qe_flag = rp->qe_status1 = QE_NOTYET;
922 		rp->qe_valid = 0;
923 	}
924 	sc->nxmit = sc->otindex = sc->tindex = sc->rindex = 0;
925 	addr->qe_csr = QE_RCV_ENABLE | QE_INT_ENABLE | QE_XMIT_INT |
926 	    QE_RCV_INT | QE_ILOOP;
927 	addr->qe_rcvlist_lo = (short)sc->rringaddr;
928 	addr->qe_rcvlist_hi = (short)((int)sc->rringaddr >> 16);
929 	sc->qe_flags |= QEF_RUNNING;
930 	(void) qestart(ifp);
931 }
932 #endif
933