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