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