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