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