xref: /csrg-svn/sys/vax/if/if_qe.c (revision 35762)
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.8 (Berkeley) 10/04/88
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 #include "../vax/pte.h"
155 #include "../vax/cpu.h"
156 #include "../vax/mtpr.h"
157 #include "if_qereg.h"
158 #include "if_uba.h"
159 #include "../vaxuba/ubareg.h"
160 #include "../vaxuba/ubavar.h"
161 
162 #if NQE > 1
163 #define NRCV	15	 		/* Receive descriptors		*/
164 #else
165 #define NRCV	20	 		/* Receive descriptors		*/
166 #endif
167 #define NXMT	5	 		/* Transmit descriptors		*/
168 #define NTOT	(NXMT + NRCV)
169 
170 #define	QETIMEOUT	2		/* transmit timeout, must be > 1 */
171 
172 /*
173  * This constant should really be 60 because the qna adds 4 bytes of crc.
174  * However when set to 60 our packets are ignored by deuna's , 3coms are
175  * okay ??????????????????????????????????????????
176  */
177 #define MINDATA 64
178 
179 /*
180  * Ethernet software status per interface.
181  *
182  * Each interface is referenced by a network interface structure,
183  * qe_if, which the routing code uses to locate the interface.
184  * This structure contains the output queue for the interface, its address, ...
185  */
186 struct	qe_softc {
187 	struct	arpcom qe_ac;		/* Ethernet common part 	*/
188 #define	qe_if	qe_ac.ac_if		/* network-visible interface 	*/
189 #define	qe_addr	qe_ac.ac_enaddr		/* hardware Ethernet address 	*/
190 	struct	ifubinfo qe_uba;	/* Q-bus resources 		*/
191 	struct	ifrw qe_ifr[NRCV];	/*	for receive buffers;	*/
192 	struct	ifxmt qe_ifw[NXMT];	/*	for xmit buffers;	*/
193 	int	qe_flags;		/* software state		*/
194 #define	QEF_RUNNING	0x01
195 #define	QEF_SETADDR	0x02
196 	int	setupaddr;		/* mapping info for setup pkts  */
197 	struct	qe_ring *rringaddr;	/* mapping info for rings	*/
198 	struct	qe_ring *tringaddr;	/*       ""			*/
199 	struct	qe_ring rring[NRCV+1];	/* Receive ring descriptors 	*/
200 	struct	qe_ring tring[NXMT+1];	/* Transmit ring descriptors 	*/
201 	u_char	setup_pkt[16][8];	/* Setup packet			*/
202 	int	rindex;			/* Receive index		*/
203 	int	tindex;			/* Transmit index		*/
204 	int	otindex;		/* Old transmit index		*/
205 	int	qe_intvec;		/* Interrupt vector 		*/
206 	struct	qedevice *addr;		/* device addr			*/
207 	int 	setupqueued;		/* setup packet queued		*/
208 	int	nxmit;			/* Transmits in progress	*/
209 	int	qe_restarts;		/* timeouts			*/
210 } qe_softc[NQE];
211 
212 struct	uba_device *qeinfo[NQE];
213 
214 extern struct timeval time;
215 
216 int	qeprobe(), qeattach(), qeintr(), qetimeout();
217 int	qeinit(), qeoutput(), qeioctl(), qereset();
218 
219 u_short qestd[] = { 0 };
220 struct	uba_driver qedriver =
221 	{ qeprobe, 0, qeattach, 0, qestd, "qe", qeinfo };
222 
223 #define QE_TIMEO	(15)
224 #define	QEUNIT(x)	minor(x)
225 static int mask = 0x3ffff;		/* address mask		*/
226 /*
227  * The deqna shouldn't receive more than ETHERMTU + sizeof(struct ether_header)
228  * but will actually take in up to 2048 bytes. To guard against the receiver
229  * chaining buffers (which we aren't prepared to handle) we allocate 2kb
230  * size buffers.
231  */
232 #define MAXPACKETSIZE 2048		/* Should really be ETHERMTU	*/
233 /*
234  * Probe the QNA to see if it's there
235  */
236 qeprobe(reg)
237 	caddr_t reg;
238 {
239 	register int br, cvec;		/* r11, r10 value-result */
240 	register struct qedevice *addr = (struct qedevice *)reg;
241 	register struct qe_ring *rp;
242 	register struct qe_ring *prp; 	/* physical rp 		*/
243 	register int i, j;
244 	static int next=0;		/* softc index		*/
245 	register struct qe_softc *sc = &qe_softc[next++];
246 
247 #ifdef lint
248 	br = 0; cvec = br; br = cvec;
249 	qeintr(0);
250 #endif
251 	/*
252 	 * Set the address mask for the particular cpu
253 	 */
254 	mask = 0x3ffff;
255 
256 	/*
257 	 * The QNA interrupts on i/o operations. To do an I/O operation
258 	 * we have to setup the interface by transmitting a setup  packet.
259 	 */
260 	addr->qe_csr = QE_RESET;
261 	addr->qe_csr &= ~QE_RESET;
262 	addr->qe_vector = (uba_hd[numuba].uh_lastiv -= 4);
263 
264 	/*
265 	 * Map the communications area and the setup packet.
266 	 */
267 	sc->setupaddr =
268 		uballoc(0, (caddr_t)sc->setup_pkt, sizeof(sc->setup_pkt), 0);
269 	sc->rringaddr = (struct qe_ring *) uballoc(0, (caddr_t)sc->rring,
270 		sizeof(struct qe_ring) * (NTOT+2), 0);
271 	prp = (struct qe_ring *)((int)sc->rringaddr & mask);
272 
273 	/*
274 	 * The QNA will loop the setup packet back to the receive ring
275 	 * for verification, therefore we initialize the first
276 	 * receive & transmit ring descriptors and link the setup packet
277 	 * to them.
278 	 */
279 	qeinitdesc(sc->tring, (caddr_t)(sc->setupaddr & mask),
280 	    sizeof(sc->setup_pkt));
281 	qeinitdesc(sc->rring, (caddr_t)(sc->setupaddr & mask),
282 	    sizeof(sc->setup_pkt));
283 
284 	rp = (struct qe_ring *)sc->tring;
285 	rp->qe_setup = 1;
286 	rp->qe_eomsg = 1;
287 	rp->qe_flag = rp->qe_status1 = QE_NOTYET;
288 	rp->qe_valid = 1;
289 
290 	rp = (struct qe_ring *)sc->rring;
291 	rp->qe_flag = rp->qe_status1 = QE_NOTYET;
292 	rp->qe_valid = 1;
293 
294 	/*
295 	 * Get the addr off of the interface and place it into the setup
296 	 * packet. This code looks strange due to the fact that the address
297 	 * is placed in the setup packet in col. major order.
298 	 */
299 	for( i = 0 ; i < 6 ; i++ )
300 		sc->setup_pkt[i][1] = addr->qe_sta_addr[i];
301 
302 	qesetup( sc );
303 	/*
304 	 * Start the interface and wait for the packet.
305 	 */
306 	j = cvec;
307 	addr->qe_csr = QE_INT_ENABLE | QE_XMIT_INT | QE_RCV_INT;
308 	addr->qe_rcvlist_lo = (short)prp;
309 	addr->qe_rcvlist_hi = (short)((int)prp >> 16);
310 	prp += NRCV+1;
311 	addr->qe_xmtlist_lo = (short)prp;
312 	addr->qe_xmtlist_hi = (short)((int)prp >> 16);
313 	DELAY(10000);
314 	/*
315 	 * All done with the bus resources.
316 	 */
317 	ubarelse(0, &sc->setupaddr);
318 	ubarelse(0, (int *)&sc->rringaddr);
319 	if( cvec == j )
320 		return 0;		/* didn't interrupt	*/
321 
322 	br = 0x15;			/* q-bus doesn't get level */
323 	return( sizeof(struct qedevice) );
324 }
325 
326 /*
327  * Interface exists: make available by filling in network interface
328  * record.  System will initialize the interface when it is ready
329  * to accept packets.
330  */
331 qeattach(ui)
332 	struct uba_device *ui;
333 {
334 	register struct qe_softc *sc = &qe_softc[ui->ui_unit];
335 	register struct ifnet *ifp = &sc->qe_if;
336 	register struct qedevice *addr = (struct qedevice *)ui->ui_addr;
337 	register int i;
338 
339 	ifp->if_unit = ui->ui_unit;
340 	ifp->if_name = "qe";
341 	ifp->if_mtu = ETHERMTU;
342 	ifp->if_flags = IFF_BROADCAST;
343 
344 	/*
345 	 * Read the address from the prom and save it.
346 	 */
347 	for( i=0 ; i<6 ; i++ )
348 		sc->setup_pkt[i][1] = sc->qe_addr[i] = addr->qe_sta_addr[i] & 0xff;
349 	addr->qe_vector |= 1;
350 	printf("qe%d: %s, hardware address %s\n", ui->ui_unit,
351 		addr->qe_vector&01 ? "delqa":"deqna",
352 		ether_sprintf(sc->qe_addr));
353 	addr->qe_vector &= ~1;
354 
355 	/*
356 	 * Save the vector for initialization at reset time.
357 	 */
358 	sc->qe_intvec = addr->qe_vector;
359 
360 	ifp->if_init = qeinit;
361 	ifp->if_output = qeoutput;
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 		sc->rringaddr = (struct qe_ring *)
410 		    ((int) uballoc(0, (caddr_t)sc->rring,
411 		    sizeof(struct qe_ring) * (NTOT+2), 0) & mask);
412 		sc->tringaddr = sc->rringaddr + NRCV + 1;
413 		sc->setupaddr =	uballoc(0, (caddr_t)sc->setup_pkt,
414 		    sizeof(sc->setup_pkt), 0) & mask;
415 		/*
416 		 * init buffers and maps
417 		 */
418 		if (if_ubaminit(&sc->qe_uba, ui->ui_ubanum,
419 		    sizeof (struct ether_header), (int)btoc(MAXPACKETSIZE),
420 		    sc->qe_ifr, NRCV, sc->qe_ifw, NXMT) == 0) {
421 			printf("qe%d: can't initialize\n", unit);
422 			sc->qe_if.if_flags &= ~IFF_UP;
423 			return;
424 		}
425 	}
426 	/*
427 	 * Init the buffer descriptors and indexes for each of the lists and
428 	 * loop them back to form a ring.
429 	 */
430 	for (i = 0; i < NRCV; i++) {
431 		qeinitdesc( &sc->rring[i],
432 		    (caddr_t)(sc->qe_ifr[i].ifrw_info & mask), MAXPACKETSIZE);
433 		sc->rring[i].qe_flag = sc->rring[i].qe_status1 = QE_NOTYET;
434 		sc->rring[i].qe_valid = 1;
435 	}
436 	qeinitdesc(&sc->rring[i], (caddr_t)NULL, 0);
437 
438 	sc->rring[i].qe_addr_lo = (short)sc->rringaddr;
439 	sc->rring[i].qe_addr_hi = (short)((int)sc->rringaddr >> 16);
440 	sc->rring[i].qe_chain = 1;
441 	sc->rring[i].qe_flag = sc->rring[i].qe_status1 = QE_NOTYET;
442 	sc->rring[i].qe_valid = 1;
443 
444 	for( i = 0 ; i <= NXMT ; i++ )
445 		qeinitdesc(&sc->tring[i], (caddr_t)NULL, 0);
446 	i--;
447 
448 	sc->tring[i].qe_addr_lo = (short)sc->tringaddr;
449 	sc->tring[i].qe_addr_hi = (short)((int)sc->tringaddr >> 16);
450 	sc->tring[i].qe_chain = 1;
451 	sc->tring[i].qe_flag = sc->tring[i].qe_status1 = QE_NOTYET;
452 	sc->tring[i].qe_valid = 1;
453 
454 	sc->nxmit = sc->otindex = sc->tindex = sc->rindex = 0;
455 
456 	/*
457 	 * Take the interface out of reset, program the vector,
458 	 * enable interrupts, and tell the world we are up.
459 	 */
460 	s = splimp();
461 	addr->qe_vector = sc->qe_intvec;
462 	sc->addr = addr;
463 	addr->qe_csr = QE_RCV_ENABLE | QE_INT_ENABLE | QE_XMIT_INT |
464 	    QE_RCV_INT | QE_ILOOP;
465 	addr->qe_rcvlist_lo = (short)sc->rringaddr;
466 	addr->qe_rcvlist_hi = (short)((int)sc->rringaddr >> 16);
467 	ifp->if_flags |= IFF_UP | IFF_RUNNING;
468 	sc->qe_flags |= QEF_RUNNING;
469 	qesetup( sc );
470 	qestart( unit );
471 	splx( s );
472 
473 }
474 
475 /*
476  * Start output on interface.
477  *
478  */
479 qestart(unit)
480 	int unit;
481 {
482 	struct uba_device *ui = qeinfo[unit];
483 	register struct qe_softc *sc = &qe_softc[unit];
484 	register struct qedevice *addr;
485 	register struct qe_ring *rp;
486 	register index;
487 	struct mbuf *m;
488 	int buf_addr, len, s;
489 
490 
491 	s = splimp();
492 	addr = (struct qedevice *)ui->ui_addr;
493 	/*
494 	 * The deqna doesn't look at anything but the valid bit
495 	 * to determine if it should transmit this packet. If you have
496 	 * a ring and fill it the device will loop indefinately on the
497 	 * packet and continue to flood the net with packets until you
498 	 * break the ring. For this reason we never queue more than n-1
499 	 * packets in the transmit ring.
500 	 *
501 	 * The microcoders should have obeyed their own defination of the
502 	 * flag and status words, but instead we have to compensate.
503 	 */
504 	for( index = sc->tindex;
505 		sc->tring[index].qe_valid == 0 && sc->nxmit < (NXMT-1) ;
506 		sc->tindex = index = ++index % NXMT){
507 		rp = &sc->tring[index];
508 		if( sc->setupqueued ) {
509 			buf_addr = sc->setupaddr;
510 			len = 128;
511 			rp->qe_setup = 1;
512 			sc->setupqueued = 0;
513 		} else {
514 			IF_DEQUEUE(&sc->qe_if.if_snd, m);
515 			if( m == 0 ){
516 				splx(s);
517 				return;
518 			}
519 			buf_addr = sc->qe_ifw[index].ifw_info;
520 			len = if_ubaput(&sc->qe_uba, &sc->qe_ifw[index], m);
521 		}
522 		/*
523 		 *  Does buffer end on odd byte ?
524 		 */
525 		if( len & 1 ) {
526 			len++;
527 			rp->qe_odd_end = 1;
528 		}
529 		if( len < MINDATA )
530 			len = MINDATA;
531 		rp->qe_buf_len = -(len/2);
532 		buf_addr &= mask;
533 		rp->qe_flag = rp->qe_status1 = QE_NOTYET;
534 		rp->qe_addr_lo = (short)buf_addr;
535 		rp->qe_addr_hi = (short)(buf_addr >> 16);
536 		rp->qe_eomsg = 1;
537 		rp->qe_flag = rp->qe_status1 = QE_NOTYET;
538 		rp->qe_valid = 1;
539 		sc->nxmit++;
540 		sc->qe_if.if_timer = QETIMEOUT;
541 
542 		/*
543 		 * See if the xmit list is invalid.
544 		 */
545 		if( addr->qe_csr & QE_XL_INVALID ) {
546 			buf_addr = (int)(sc->tringaddr+index);
547 			addr->qe_xmtlist_lo = (short)buf_addr;
548 			addr->qe_xmtlist_hi = (short)(buf_addr >> 16);
549 		}
550 	}
551 	splx( s );
552 }
553 
554 /*
555  * Ethernet interface interrupt processor
556  */
557 qeintr(unit)
558 	int unit;
559 {
560 	register struct qe_softc *sc = &qe_softc[unit];
561 	struct qedevice *addr = (struct qedevice *)qeinfo[unit]->ui_addr;
562 	int s, buf_addr, csr;
563 
564 	s = splimp();
565 	csr = addr->qe_csr;
566 	addr->qe_csr = QE_RCV_ENABLE | QE_INT_ENABLE | QE_XMIT_INT | QE_RCV_INT | QE_ILOOP;
567 	if( csr & QE_RCV_INT )
568 		qerint( unit );
569 	if( csr & QE_XMIT_INT )
570 		qetint( unit );
571 	if( csr & QE_NEX_MEM_INT )
572 		panic("qe: Non existant memory interrupt");
573 
574 	if( addr->qe_csr & QE_RL_INVALID && sc->rring[sc->rindex].qe_status1 == QE_NOTYET ) {
575 		buf_addr = (int)&sc->rringaddr[sc->rindex];
576 		addr->qe_rcvlist_lo = (short)buf_addr;
577 		addr->qe_rcvlist_hi = (short)(buf_addr >> 16);
578 	}
579 	splx( s );
580 }
581 
582 /*
583  * Ethernet interface transmit interrupt.
584  */
585 
586 qetint(unit)
587 	int unit;
588 {
589 	register struct qe_softc *sc = &qe_softc[unit];
590 	register struct qe_ring *rp;
591 	register struct ifxmt *ifxp;
592 	int status1, setupflag;
593 	short len;
594 
595 
596 	while( sc->otindex != sc->tindex && sc->tring[sc->otindex].qe_status1 != QE_NOTYET && sc->nxmit > 0 ) {
597 		/*
598 		 * Save the status words from the descriptor so that it can
599 		 * be released.
600 		 */
601 		rp = &sc->tring[sc->otindex];
602 		status1 = rp->qe_status1;
603 		setupflag = rp->qe_setup;
604 		len = (-rp->qe_buf_len) * 2;
605 		if( rp->qe_odd_end )
606 			len++;
607 		/*
608 		 * Init the buffer descriptor
609 		 */
610 		bzero((caddr_t)rp, sizeof(struct qe_ring));
611 		if( --sc->nxmit == 0 )
612 			sc->qe_if.if_timer = 0;
613 		if( !setupflag ) {
614 			/*
615 			 * Do some statistics.
616 			 */
617 			sc->qe_if.if_opackets++;
618 			sc->qe_if.if_collisions += ( status1 & QE_CCNT ) >> 4;
619 			if (status1 & QE_ERROR)
620 				sc->qe_if.if_oerrors++;
621 			/*
622 			 * If this was a broadcast packet loop it
623 			 * back because the hardware can't hear its own
624 			 * transmits.
625 			 */
626 			ifxp = &sc->qe_ifw[sc->otindex];
627 			if (bcmp((caddr_t)((struct ether_header *)ifxp->ifw_addr)->ether_dhost,
628 			   (caddr_t)etherbroadcastaddr,
629 			   sizeof(etherbroadcastaddr)) == 0)
630 				qeread(sc, &ifxp->ifrw,
631 				    len - sizeof(struct ether_header));
632 			if (ifxp->ifw_xtofree) {
633 				m_freem(ifxp->ifw_xtofree);
634 				ifxp->ifw_xtofree = 0;
635 			}
636 		}
637 		sc->otindex = ++sc->otindex % NXMT;
638 	}
639 	qestart( unit );
640 }
641 
642 /*
643  * Ethernet interface receiver interrupt.
644  * If can't determine length from type, then have to drop packet.
645  * Othewise decapsulate packet based on type and pass to type specific
646  * higher-level input routine.
647  */
648 qerint(unit)
649 	int unit;
650 {
651 	register struct qe_softc *sc = &qe_softc[unit];
652 	register struct qe_ring *rp;
653 	int len, status1, status2;
654 	int bufaddr;
655 
656 	/*
657 	 * Traverse the receive ring looking for packets to pass back.
658 	 * The search is complete when we find a descriptor not in use.
659 	 *
660 	 * As in the transmit case the deqna doesn't honor it's own protocols
661 	 * so there exists the possibility that the device can beat us around
662 	 * the ring. The proper way to guard against this is to insure that
663 	 * there is always at least one invalid descriptor. We chose instead
664 	 * to make the ring large enough to minimize the problem. With a ring
665 	 * size of 4 we haven't been able to see the problem. To be safe we
666 	 * doubled that to 8.
667 	 *
668 	 */
669 	for( ; sc->rring[sc->rindex].qe_status1 != QE_NOTYET ; sc->rindex = ++sc->rindex % NRCV ){
670 		rp = &sc->rring[sc->rindex];
671 		status1 = rp->qe_status1;
672 		status2 = rp->qe_status2;
673 		bzero((caddr_t)rp, sizeof(struct qe_ring));
674 		if( (status1 & QE_MASK) == QE_MASK )
675 			panic("qe: chained packet");
676 		len = ((status1 & QE_RBL_HI) | (status2 & QE_RBL_LO)) + 60;
677 		sc->qe_if.if_ipackets++;
678 
679 		if (status1 & QE_ERROR) {
680 			if ((status1 & QE_RUNT) == 0)
681 				sc->qe_if.if_ierrors++;
682 		} else {
683 			/*
684 			 * We don't process setup packets.
685 			 */
686 			if( !(status1 & QE_ESETUP) )
687 				qeread(sc, &sc->qe_ifr[sc->rindex],
688 					len - sizeof(struct ether_header));
689 		}
690 		/*
691 		 * Return the buffer to the ring
692 		 */
693 		bufaddr = (int)sc->qe_ifr[sc->rindex].ifrw_info & mask;
694 		rp->qe_buf_len = -((MAXPACKETSIZE)/2);
695 		rp->qe_addr_lo = (short)bufaddr;
696 		rp->qe_addr_hi = (short)((int)bufaddr >> 16);
697 		rp->qe_flag = rp->qe_status1 = QE_NOTYET;
698 		rp->qe_valid = 1;
699 	}
700 }
701 /*
702  * Ethernet output routine.
703  * Encapsulate a packet of type family for the local net.
704  * Use trailer local net encapsulation if enough data in first
705  * packet leaves a multiple of 512 bytes of data in remainder.
706  */
707 qeoutput(ifp, m0, dst)
708 	struct ifnet *ifp;
709 	struct mbuf *m0;
710 	struct sockaddr *dst;
711 {
712 	int type, s, error;
713 	u_char edst[6];
714 	struct in_addr idst;
715 	register struct qe_softc *is = &qe_softc[ifp->if_unit];
716 	register struct mbuf *m = m0;
717 	register struct ether_header *eh;
718 	register int off;
719 	int usetrailers;
720 
721 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
722 		error = ENETDOWN;
723 		goto bad;
724 	}
725 
726 	switch (dst->sa_family) {
727 
728 #ifdef INET
729 	case AF_INET:
730 		idst = ((struct sockaddr_in *)dst)->sin_addr;
731 		if (!arpresolve(&is->qe_ac, m, &idst, edst, &usetrailers))
732 			return (0);	/* if not yet resolved */
733 		off = ntohs((u_short)mtod(m, struct ip *)->ip_len) - m->m_len;
734 		if (usetrailers && off > 0 && (off & 0x1ff) == 0 &&
735 		    m->m_off >= MMINOFF + 2 * sizeof (u_short)) {
736 			type = ETHERTYPE_TRAIL + (off>>9);
737 			m->m_off -= 2 * sizeof (u_short);
738 			m->m_len += 2 * sizeof (u_short);
739 			*mtod(m, u_short *) = htons((u_short)ETHERTYPE_IP);
740 			*(mtod(m, u_short *) + 1) = htons((u_short)m->m_len);
741 			goto gottrailertype;
742 		}
743 		type = ETHERTYPE_IP;
744 		off = 0;
745 		goto gottype;
746 #endif
747 #ifdef NS
748 	case AF_NS:
749 		type = ETHERTYPE_NS;
750  		bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host),
751 		    (caddr_t)edst, sizeof (edst));
752 		off = 0;
753 		goto gottype;
754 #endif
755 
756 
757 	case AF_UNSPEC:
758 		eh = (struct ether_header *)dst->sa_data;
759  		bcopy((caddr_t)eh->ether_dhost, (caddr_t)edst, sizeof (edst));
760 		type = eh->ether_type;
761 		goto gottype;
762 
763 	default:
764 		printf("qe%d: can't handle af%d\n", ifp->if_unit,
765 			dst->sa_family);
766 		error = EAFNOSUPPORT;
767 		goto bad;
768 	}
769 
770 gottrailertype:
771 	/*
772 	 * Packet to be sent as trailer: move first packet
773 	 * (control information) to end of chain.
774 	 */
775 	while (m->m_next)
776 		m = m->m_next;
777 	m->m_next = m0;
778 	m = m0->m_next;
779 	m0->m_next = 0;
780 	m0 = m;
781 
782 gottype:
783 	/*
784 	 * Add local net header.  If no space in first mbuf,
785 	 * allocate another.
786 	 */
787 	if (m->m_off > MMAXOFF ||
788 	    MMINOFF + sizeof (struct ether_header) > m->m_off) {
789 		m = m_get(M_DONTWAIT, MT_HEADER);
790 		if (m == 0) {
791 			error = ENOBUFS;
792 			goto bad;
793 		}
794 		m->m_next = m0;
795 		m->m_off = MMINOFF;
796 		m->m_len = sizeof (struct ether_header);
797 	} else {
798 		m->m_off -= sizeof (struct ether_header);
799 		m->m_len += sizeof (struct ether_header);
800 	}
801 	eh = mtod(m, struct ether_header *);
802 	eh->ether_type = htons((u_short)type);
803  	bcopy((caddr_t)edst, (caddr_t)eh->ether_dhost, sizeof (edst));
804  	bcopy((caddr_t)is->qe_addr, (caddr_t)eh->ether_shost, sizeof (is->qe_addr));
805 
806 	/*
807 	 * Queue message on interface, and start output if interface
808 	 * not yet active.
809 	 */
810 	s = splimp();
811 	if (IF_QFULL(&ifp->if_snd)) {
812 		IF_DROP(&ifp->if_snd);
813 		splx(s);
814 		m_freem(m);
815 		return (ENOBUFS);
816 	}
817 	IF_ENQUEUE(&ifp->if_snd, m);
818 	qestart(ifp->if_unit);
819 	splx(s);
820 	return (0);
821 
822 bad:
823 	m_freem(m0);
824 	return (error);
825 }
826 
827 
828 /*
829  * Process an ioctl request.
830  */
831 qeioctl(ifp, cmd, data)
832 	register struct ifnet *ifp;
833 	int cmd;
834 	caddr_t data;
835 {
836 	struct qe_softc *sc = &qe_softc[ifp->if_unit];
837 	struct ifaddr *ifa = (struct ifaddr *)data;
838 	int s = splimp(), error = 0;
839 
840 	switch (cmd) {
841 
842 	case SIOCSIFADDR:
843 		ifp->if_flags |= IFF_UP;
844 		qeinit(ifp->if_unit);
845 		switch(ifa->ifa_addr.sa_family) {
846 #ifdef INET
847 		case AF_INET:
848 			((struct arpcom *)ifp)->ac_ipaddr =
849 				IA_SIN(ifa)->sin_addr;
850 			arpwhohas((struct arpcom *)ifp, &IA_SIN(ifa)->sin_addr);
851 			break;
852 #endif
853 #ifdef NS
854 		case AF_NS:
855 		    {
856 			register struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
857 
858 			if (ns_nullhost(*ina))
859 				ina->x_host = *(union ns_host *)(sc->qe_addr);
860 			else
861 				qe_setaddr(ina->x_host.c_host, ifp->if_unit);
862 			break;
863 		    }
864 #endif
865 		}
866 		break;
867 
868 	case SIOCSIFFLAGS:
869 		if ((ifp->if_flags & IFF_UP) == 0 &&
870 		    sc->qe_flags & QEF_RUNNING) {
871 			((struct qedevice *)
872 			   (qeinfo[ifp->if_unit]->ui_addr))->qe_csr = QE_RESET;
873 			sc->qe_flags &= ~QEF_RUNNING;
874 		} else if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) ==
875 		    IFF_RUNNING && (sc->qe_flags & QEF_RUNNING) == 0)
876 			qerestart(sc);
877 		break;
878 
879 	default:
880 		error = EINVAL;
881 
882 	}
883 	splx(s);
884 	return (error);
885 }
886 
887 /*
888  * set ethernet address for unit
889  */
890 qe_setaddr(physaddr, unit)
891 	u_char *physaddr;
892 	int unit;
893 {
894 	register struct qe_softc *sc = &qe_softc[unit];
895 	register int i;
896 
897 	for (i = 0; i < 6; i++)
898 		sc->setup_pkt[i][1] = sc->qe_addr[i] = physaddr[i];
899 	sc->qe_flags |= QEF_SETADDR;
900 	if (sc->qe_if.if_flags & IFF_RUNNING)
901 		qesetup(sc);
902 	qeinit(unit);
903 }
904 
905 
906 /*
907  * Initialize a ring descriptor with mbuf allocation side effects
908  */
909 qeinitdesc(rp, addr, len)
910 	register struct qe_ring *rp;
911 	caddr_t addr; 			/* mapped address */
912 	int len;
913 {
914 	/*
915 	 * clear the entire descriptor
916 	 */
917 	bzero((caddr_t)rp, sizeof(struct qe_ring));
918 
919 	if( len ) {
920 		rp->qe_buf_len = -(len/2);
921 		rp->qe_addr_lo = (short)addr;
922 		rp->qe_addr_hi = (short)((int)addr >> 16);
923 	}
924 }
925 /*
926  * Build a setup packet - the physical address will already be present
927  * in first column.
928  */
929 qesetup( sc )
930 struct qe_softc *sc;
931 {
932 	register i, j;
933 
934 	/*
935 	 * Copy the target address to the rest of the entries in this row.
936 	 */
937 	 for ( j = 0; j < 6 ; j++ )
938 		for ( i = 2 ; i < 8 ; i++ )
939 			sc->setup_pkt[j][i] = sc->setup_pkt[j][1];
940 	/*
941 	 * Duplicate the first half.
942 	 */
943 	bcopy((caddr_t)sc->setup_pkt[0], (caddr_t)sc->setup_pkt[8], 64);
944 	/*
945 	 * Fill in the broadcast address.
946 	 */
947 	for ( i = 0; i < 6 ; i++ )
948 		sc->setup_pkt[i][2] = 0xff;
949 	sc->setupqueued++;
950 }
951 
952 /*
953  * Pass a packet to the higher levels.
954  * We deal with the trailer protocol here.
955  */
956 qeread(sc, ifrw, len)
957 	register struct qe_softc *sc;
958 	struct ifrw *ifrw;
959 	int len;
960 {
961 	struct ether_header *eh;
962     	struct mbuf *m;
963 	int off, resid;
964 	struct ifqueue *inq;
965 
966 	/*
967 	 * Deal with trailer protocol: if type is INET trailer
968 	 * get true type from first 16-bit word past data.
969 	 * Remember that type was trailer by setting off.
970 	 */
971 
972 	eh = (struct ether_header *)ifrw->ifrw_addr;
973 	eh->ether_type = ntohs((u_short)eh->ether_type);
974 #define	qedataaddr(eh, off, type)	((type)(((caddr_t)((eh)+1)+(off))))
975 	if (eh->ether_type >= ETHERTYPE_TRAIL &&
976 	    eh->ether_type < ETHERTYPE_TRAIL+ETHERTYPE_NTRAILER) {
977 		off = (eh->ether_type - ETHERTYPE_TRAIL) * 512;
978 		if (off >= ETHERMTU)
979 			return;		/* sanity */
980 		eh->ether_type = ntohs(*qedataaddr(eh,off, u_short *));
981 		resid = ntohs(*(qedataaddr(eh, off+2, u_short *)));
982 		if (off + resid > len)
983 		     return;		/* sanity */
984 		len = off + resid;
985 	} else
986 		off = 0;
987 	if (len == 0)
988 		return;
989 
990 	/*
991 	 * Pull packet off interface.  Off is nonzero if packet
992 	 * has trailing header; qeget will then force this header
993 	 * information to be at the front, but we still have to drop
994 	 * the type and length which are at the front of any trailer data.
995 	 */
996 	m = if_ubaget(&sc->qe_uba, ifrw, len, off, &sc->qe_if);
997 
998 	if (m == 0)
999 		return;
1000 
1001 	if (off) {
1002 		struct ifnet *ifp;
1003 
1004 		ifp = *(mtod(m, struct ifnet **));
1005 		m->m_off += 2 * sizeof (u_short);
1006 		m->m_len -= 2 * sizeof (u_short);
1007 		*(mtod(m, struct ifnet **)) = ifp;
1008 	}
1009 	switch (eh->ether_type) {
1010 
1011 #ifdef INET
1012 	case ETHERTYPE_IP:
1013 		schednetisr(NETISR_IP);
1014 		inq = &ipintrq;
1015 		break;
1016 
1017 	case ETHERTYPE_ARP:
1018 		arpinput(&sc->qe_ac, m);
1019 		return;
1020 #endif
1021 #ifdef NS
1022 	case ETHERTYPE_NS:
1023 		schednetisr(NETISR_NS);
1024 		inq = &nsintrq;
1025 		break;
1026 
1027 #endif
1028 
1029 	default:
1030 		m_freem(m);
1031 		return;
1032 	}
1033 
1034 	if (IF_QFULL(inq)) {
1035 		IF_DROP(inq);
1036 		m_freem(m);
1037 		return;
1038 	}
1039 	IF_ENQUEUE(inq, m);
1040 }
1041 
1042 /*
1043  * Watchdog timeout routine. There is a condition in the hardware that
1044  * causes the board to lock up under heavy load. This routine detects
1045  * the hang up and restarts the device.
1046  */
1047 qetimeout(unit)
1048 	int unit;
1049 {
1050 	register struct qe_softc *sc;
1051 
1052 	sc = &qe_softc[unit];
1053 	log(LOG_ERR, "qe%d: transmit timeout, restarted %d\n",
1054 	     unit, ++sc->qe_restarts);
1055 	qerestart(sc);
1056 }
1057 /*
1058  * Restart for board lockup problem.
1059  */
1060 qerestart(sc)
1061 	register struct qe_softc *sc;
1062 {
1063 	register struct ifnet *ifp = &sc->qe_if;
1064 	register struct qedevice *addr = sc->addr;
1065 	register struct qe_ring *rp;
1066 	register i;
1067 
1068 	addr->qe_csr = QE_RESET;
1069 	qesetup( sc );
1070 	for (i = 0, rp = sc->tring; i < NXMT; rp++, i++) {
1071 		rp->qe_flag = rp->qe_status1 = QE_NOTYET;
1072 		rp->qe_valid = 0;
1073 	}
1074 	sc->nxmit = sc->otindex = sc->tindex = sc->rindex = 0;
1075 	addr->qe_csr = QE_RCV_ENABLE | QE_INT_ENABLE | QE_XMIT_INT |
1076 	    QE_RCV_INT | QE_ILOOP;
1077 	addr->qe_rcvlist_lo = (short)sc->rringaddr;
1078 	addr->qe_rcvlist_hi = (short)((int)sc->rringaddr >> 16);
1079 	sc->qe_flags |= QEF_RUNNING;
1080 	qestart(ifp->if_unit);
1081 }
1082 #endif
1083