xref: /netbsd-src/sys/dev/qbus/if_qt.c (revision dbd28266c503fc21be4c1fd116ad4b6dc24049e5)
1 /*	$NetBSD: if_qt.c,v 1.29 2025/01/08 18:36:08 tsutsui Exp $	*/
2 /*
3  * Copyright (c) 1992 Steven M. Schultz
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  *	@(#)if_qt.c     1.2 (2.11BSD) 2/20/93
29  */
30 /*
31  *
32  * Modification History
33  * 23-Feb-92 -- sms
34  *	Rewrite the buffer handling so that fewer than the maximum number of
35  *	buffers may be used (32 receive and 12 transmit buffers consume 66+kb
36  *	of main system memory in addition to the internal structures in the
37  *	networking code).  A freelist of available buffers is maintained now.
38  *	When I/O operations complete the associated buffer is placed on the
39  *	freelist (a single linked list for simplicity) and when an I/O is
40  *	started a buffer is pulled off the list.
41  *
42  * 20-Feb-92 -- sms
43  *	It works!  Darned board couldn't handle "short" rings - those rings
44  *	where only half the entries were made available to the board (the
45  *	ring descriptors were the full size, merely half the entries were
46  *	flagged as belonging always to the driver).  Grrrr.  Would have thought
47  *	the board could skip over those entries reserved by the driver.
48  *	Now to find a way not to have to allocated 32+12 times 1.5kb worth of
49  *	buffers...
50  *
51  * 03-Feb-92 -- sms
52  *	Released but still not working.  The driver now responds to arp and
53  *	ping requests.  The board is apparently not returning ring descriptors
54  *	to the driver so eventually we run out of buffers.  Back to the
55  *	drawing board.
56  *
57  * 28-Dec-92 -- sms
58  *	Still not released.  Hiatus in finding free time and thin-netting
59  *	the systems (many thanks Terry!).
60  *	Added logic to dynamically allocate a vector and initialize it.
61  *
62  * 23-Oct-92 -- sms
63  *	The INIT block must (apparently) be quadword aligned [no thanks to
64  *	the manual for not mentioning that fact].  The necessary alignment
65  *	is achieved by allocating the INIT block from main memory ('malloc'
66  *	guarantees click alignment) and mapping it as needed (which is _very_
67  *	infrequently).  A check for quadword alignment of the ring descriptors
68  *	was added - at present the descriptors are properly aligned, if this
69  *	should change then something will have to be done (like do it "right").
70  *	Darned alignment restrictions!
71  *
72  *	A couple of typos were corrected (missing parentheses, reversed
73  *	arguments to printf calls, etc).
74  *
75  * 13-Oct-92 -- sms@wlv.iipo.gtegsc.com
76  *	Created based on the DELQA-PLUS addendum to DELQA User's Guide.
77  *	This driver ('qt') is selected at system configuration time.  If the
78  *	board *	is not a DELQA-YM an error message will be printed and the
79  *	interface will not be attached.
80  */
81 
82 #include <sys/cdefs.h>
83 __KERNEL_RCSID(0, "$NetBSD: if_qt.c,v 1.29 2025/01/08 18:36:08 tsutsui Exp $");
84 
85 #include "opt_inet.h"
86 
87 #include <sys/param.h>
88 #include <sys/systm.h>
89 #include <sys/mbuf.h>
90 #include <sys/protosw.h>
91 #include <sys/socket.h>
92 #include <sys/ioctl.h>
93 #include <sys/device.h>
94 #include <sys/errno.h>
95 #include <sys/syslog.h>
96 #include <sys/time.h>
97 #include <sys/kernel.h>
98 
99 #include <net/if.h>
100 #include <net/if_ether.h>
101 #include <net/route.h>
102 #include <net/bpf.h>
103 
104 #ifdef INET
105 #include <sys/domain.h>
106 #include <netinet/in.h>
107 #include <netinet/in_systm.h>
108 #include <netinet/in_var.h>
109 #include <netinet/ip.h>
110 #endif
111 
112 #include <sys/bus.h>
113 
114 #include <dev/qbus/ubavar.h>
115 #include <dev/qbus/if_uba.h>
116 #include <dev/qbus/if_qtreg.h>
117 
118 #define NRCV	QT_MAX_RCV	/* Receive descriptors (must be == 32) */
119 #define NXMT	QT_MAX_XMT	/* Transmit descriptors	(must be == 12) */
120 #if	NRCV != 32 || NXMT != 12
121 	hardware requires these sizes.
122 #endif
123 
124 /*
125  * Control data structures, must be in DMA-friendly memory.
126  */
127 struct	qt_cdata {
128 	struct	qt_init qc_init;	/* Init block			*/
129 	struct	qt_rring qc_r[NRCV];	/* Receive descriptor ring	*/
130 	struct	qt_tring qc_t[NXMT];	/* Transmit descriptor ring	*/
131 };
132 
133 struct	qt_softc {
134 	device_t sc_dev;		/* Configuration common part */
135 	struct	ethercom is_ec;		/* common part - must be first  */
136 	struct uba_softc *sc_uh;
137 	struct	evcnt sc_intrcnt;	/* Interrupt counting */
138 #define	is_if	is_ec.ec_if		/* network-visible interface	*/
139 	u_int8_t is_addr[ETHER_ADDR_LEN]; /* hardware Ethernet address	*/
140 	bus_space_tag_t	sc_iot;
141 	bus_addr_t	sc_ioh;
142 
143 	struct	ubinfo	sc_ui;		/* control block address desc	*/
144 	struct	qt_cdata *sc_ib;	/* virt address of ctrl block	*/
145 	struct	qt_cdata *sc_pib;	/* phys address of ctrl block	*/
146 
147 	struct	ifubinfo sc_ifuba;	/* UNIBUS resources */
148 	struct	ifrw sc_ifr[NRCV];	/* UNIBUS receive buffer maps */
149 	struct	ifxmt sc_ifw[NXMT];	/* UNIBUS receive buffer maps */
150 
151 	int	rindex;			/* Receive Completed Index	*/
152 	int	nxtrcv;			/* Next Receive Index		*/
153 	int	nrcv;			/* Number of Receives active	*/
154 
155 	int	xnext;			/* Next descriptor to transmit	*/
156 	int	xlast;			/* Last descriptor transmitted	*/
157 	int	nxmit;			/* # packets in send queue	*/
158 
159 	short	vector;			/* Interrupt vector assigned	*/
160 };
161 
162 static	int qtmatch(device_t, cfdata_t, void *);
163 static	void qtattach(device_t, device_t, void *);
164 static	void lance_setladrf(struct ethercom *ec, uint16_t *af);
165 static	void qtintr(void *);
166 static	int qtinit(struct ifnet *);
167 static	int qtioctl(struct ifnet *, u_long, void *);
168 static	int qtturbo(struct qt_softc *);
169 static	void qtstart(struct ifnet *ifp);
170 static	void qtstop(struct ifnet *ifp, int disable);
171 static	void qtsrr(struct qt_softc *, int);
172 static	void qtrint(struct qt_softc *sc);
173 static	void qttint(struct qt_softc *sc);
174 
175 #ifdef notyet
176 static	void qtreset(struct qt_softc *sc);
177 #endif
178 
179 CFATTACH_DECL_NEW(qt, sizeof(struct qt_softc),
180     qtmatch, qtattach, NULL, NULL);
181 
182 /*
183  * Maximum packet size needs to include 4 bytes for the CRC
184  * on received packets.
185  */
186 #define MAXPACKETSIZE (ETHERMTU + sizeof (struct ether_header) + ETHER_CRC_LEN)
187 #define	MINPACKETSIZE 64
188 
189 #define QT_WCSR(csr, val) \
190 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, csr, val)
191 #define QT_RCSR(csr) \
192 	bus_space_read_2(sc->sc_iot, sc->sc_ioh, csr)
193 
194 
195 #define loint(x)	((int)(x) & 0xffff)
196 #define hiint(x)	(((int)(x) >> 16) & 0x3f)
197 
198 /*
199  * Check if this card is a turbo delqa.
200  */
201 int
202 qtmatch(device_t parent, cfdata_t cf, void *aux)
203 {
204 	struct qt_softc ssc;
205 	struct qt_softc *sc = &ssc;
206 	struct uba_attach_args *ua = aux;
207 	struct uba_softc *uh = device_private(parent);
208 	struct qt_init *qi;
209 	struct ubinfo ui;
210 
211 	sc->sc_iot = ua->ua_iot;
212 	sc->sc_ioh = ua->ua_ioh;
213 	if (qtturbo(sc) == 0)
214 		return 0; /* Not a turbo card */
215 
216 	/* Force the card to interrupt */
217 	ui.ui_size = sizeof(struct qt_init);
218 	if (ubmemalloc(uh, &ui, 0))
219 		return 0; /* Failed */
220 	qi = (struct qt_init *)ui.ui_vaddr;
221 	memset(qi, 0, sizeof(struct qt_init));
222 	qi->vector = uh->uh_lastiv - 4;
223 	qi->options = INIT_OPTIONS_INT;
224 
225 	QT_WCSR(CSR_IBAL, loint(ui.ui_baddr));
226 	QT_WCSR(CSR_IBAH, hiint(ui.ui_baddr));
227 	QT_WCSR(CSR_SRQR, 2);
228 	delay(100000); /* Wait some time for interrupt */
229 	QT_WCSR(CSR_SRQR, 3); /* Stop card */
230 
231 	ubmemfree(uh, &ui);
232 
233 	return 10;
234 }
235 
236 
237 /*
238  * Interface exists.  More accurately, something exists at the CSR (see
239  * sys/sys_net.c) -- there's no guarantee it's a DELQA-YM.
240  *
241  * The ring descriptors are initialized, the buffers allocated using first the
242  * DMA region allocated at network load time and then later main memory.  The
243  * INIT block is filled in and the device is poked/probed to see if it really
244  * is a DELQA-YM.  If the device is not a -YM then a message is printed and
245  * the 'if_attach' call is skipped.  For a -YM the START command is issued,
246  * but the device is not marked as running|up - that happens at interrupt level
247  * when the device interrupts to say it has started.
248  */
249 
250 void
251 qtattach(device_t parent, device_t self, void *aux)
252 {
253 	struct qt_softc *sc = device_private(self);
254 	struct ifnet *ifp = &sc->is_if;
255 	struct uba_attach_args *ua = aux;
256 
257 	sc->sc_dev = self;
258 
259 	uba_intr_establish(ua->ua_icookie, ua->ua_cvec, qtintr, sc,
260 	    &sc->sc_intrcnt);
261 	evcnt_attach_dynamic(&sc->sc_intrcnt, EVCNT_TYPE_INTR, ua->ua_evcnt,
262 	    device_xname(sc->sc_dev), "intr");
263 
264 	sc->sc_uh = device_private(parent);
265 	sc->sc_iot = ua->ua_iot;
266 	sc->sc_ioh = ua->ua_ioh;
267 	sc->sc_uh->uh_lastiv -= 4;
268 	sc->vector = sc->sc_uh->uh_lastiv;
269 
270 /*
271  * Now allocate the buffers and initialize the buffers.  This should _never_
272  * fail because main memory is allocated after the DMA pool is used up.
273  */
274 
275 	sc->is_addr[0] = QT_RCSR(0);
276 	sc->is_addr[1] = QT_RCSR(2);
277 	sc->is_addr[2] = QT_RCSR(4);
278 	sc->is_addr[3] = QT_RCSR(6);
279 	sc->is_addr[4] = QT_RCSR(8);
280 	sc->is_addr[5] = QT_RCSR(10);
281 
282 	strcpy(ifp->if_xname, device_xname(sc->sc_dev));
283 	ifp->if_softc = sc;
284 	ifp->if_flags = IFF_BROADCAST|IFF_MULTICAST;
285 	ifp->if_ioctl = qtioctl;
286 	ifp->if_start = qtstart;
287 	ifp->if_init = qtinit;
288 	ifp->if_stop = qtstop;
289 	IFQ_SET_READY(&ifp->if_snd);
290 
291 	printf("\n%s: delqa-plus in Turbo mode, hardware address %s\n",
292 	    device_xname(sc->sc_dev), ether_sprintf(sc->is_addr));
293 	if_attach(ifp);
294 	ether_ifattach(ifp, sc->is_addr);
295 }
296 
297 int
298 qtturbo(struct qt_softc *sc)
299 {
300 	int i;
301 
302 /*
303  * Issue the software reset.  Delay 150us.  The board should now be in
304  * DELQA-Normal mode.  Set ITB and DEQTA select.  If both bits do not
305  * stay turned on then the board is not a DELQA-YM.
306  */
307 	QT_WCSR(CSR_ARQR, ARQR_SR);
308 	QT_WCSR(CSR_ARQR, 0);
309 	delay(150L);
310 
311 	QT_WCSR(CSR_SRR, 0x8001);	/* MS | ITB */
312 	i = QT_RCSR(CSR_SRR);
313 	QT_WCSR(CSR_SRR, 0x8000);	/* Turn off ITB, set DELQA select */
314 	if (i != 0x8001)
315 		return 0;
316 /*
317  * Board is a DELQA-YM.  Send the commands to enable Turbo mode.  Delay
318  * 1 second, testing the SRR register every millisecond to see if the
319  * board has shifted to Turbo mode.
320  */
321 	QT_WCSR(CSR_XCR0, 0x0baf);
322 	QT_WCSR(CSR_XCR1, 0xff00);
323 	for (i = 0; i < 1000; i++) {
324 		if ((QT_RCSR(CSR_SRR) & SRR_RESP) == 1)
325 			break;
326 		delay(1000L);
327 	}
328 	if (i >= 1000) {
329 		printf("qt !Turbo\n");
330 		return 0;
331 	}
332 	return 1;
333 }
334 
335 #define ETHER_CMP(a,b)	memcmp((a), (b), 6)
336 
337 /*
338  * Set up the logical address filter.
339  */
340 void
341 lance_setladrf(struct ethercom *ec, uint16_t *af)
342 {
343 	struct ifnet *ifp = &ec->ec_if;
344 	struct ether_multi *enm;
345 	uint32_t crc;
346 	struct ether_multistep step;
347 
348 	/*
349 	 * Set up multicast address filter by passing all multicast addresses
350 	 * through a crc generator, and then using the high order 6 bits as an
351 	 * index into the 64 bit logical address filter.  The high order bit
352 	 * selects the word, while the rest of the bits select the bit within
353 	 * the word.
354 	 */
355 
356 	if (ifp->if_flags & IFF_PROMISC)
357 		goto allmulti;
358 
359 	af[0] = af[1] = af[2] = af[3] = 0x0000;
360 
361 	ETHER_LOCK(ec);
362 	ETHER_FIRST_MULTI(step, ec, enm);
363 	while (enm != NULL) {
364 		if (ETHER_CMP(enm->enm_addrlo, enm->enm_addrhi)) {
365 			/*
366 			 * We must listen to a range of multicast addresses.
367 			 * For now, just accept all multicasts, rather than
368 			 * trying to set only those filter bits needed to match
369 			 * the range.  (At this time, the only use of address
370 			 * ranges is for IP multicast routing, for which the
371 			 * range is big enough to require all bits set.)
372 			 */
373 			ETHER_UNLOCK(ec);
374 			goto allmulti;
375 		}
376 
377 		crc = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN);
378 
379 		/* Just want the 6 most significant bits. */
380 		crc >>= 26;
381 
382 		/* Set the corresponding bit in the filter. */
383 		af[crc >> 4] |= 1 << (crc & 0xf);
384 
385 		ETHER_NEXT_MULTI(step, enm);
386 	}
387 	ETHER_UNLOCK(ec);
388 	ifp->if_flags &= ~IFF_ALLMULTI;
389 	return;
390 
391 allmulti:
392 	ifp->if_flags |= IFF_ALLMULTI;
393 	af[0] = af[1] = af[2] = af[3] = 0xffff;
394 }
395 
396 int
397 qtinit(struct ifnet *ifp)
398 {
399 	struct qt_softc *sc = ifp->if_softc;
400 	struct qt_init *iniblk;
401 	struct ifrw *ifrw;
402 	struct ifxmt *ifxp;
403 	struct qt_rring *rp;
404 	struct qt_tring *tp;
405 	int i, error;
406 
407 	if (ifp->if_flags & IFF_RUNNING) {
408 		/* Cancel any pending I/O. */
409 		qtstop(ifp, 0);
410 	}
411 
412 	if (sc->sc_ib == NULL) {
413 		if (if_ubaminit(&sc->sc_ifuba, sc->sc_uh,
414 		    MCLBYTES, sc->sc_ifr, NRCV, sc->sc_ifw, NXMT)) {
415 			printf("%s: can't initialize\n",
416 			    device_xname(sc->sc_dev));
417 			ifp->if_flags &= ~IFF_UP;
418 			return 0;
419 		}
420 		sc->sc_ui.ui_size = sizeof(struct qt_cdata);
421 		if ((error = ubmemalloc(sc->sc_uh, &sc->sc_ui, 0))) {
422 			printf(": failed ubmemalloc(), error = %d\n", error);
423 			return error;
424 		}
425 		sc->sc_ib = (struct qt_cdata *)sc->sc_ui.ui_vaddr;
426 		sc->sc_pib = (struct qt_cdata *)sc->sc_ui.ui_baddr;
427 
428 /*
429  * Fill in most of the INIT block: vector, options (interrupt enable), ring
430  * locations.  The physical address is copied from the ROMs as part of the
431  * -YM testing procedure.  The CSR is saved here rather than in qtinit()
432  * because the qtturbo() routine needs it.
433  *
434  * The INIT block must be quadword aligned.  Using malloc() guarantees click
435  * (64 byte) alignment.  Since the only time the INIT block is referenced is
436  * at 'startup' or 'reset' time there is really no time penalty (and a modest
437  * D space savings) involved.
438  */
439 		memset(sc->sc_ib, 0, sizeof(struct qt_cdata));
440 		iniblk = &sc->sc_ib->qc_init;
441 
442 		iniblk->vector = sc->vector;
443 		memcpy(iniblk->paddr, sc->is_addr, 6);
444 
445 		iniblk->options = INIT_OPTIONS_INT;
446 		iniblk->rx_lo = loint(&sc->sc_pib->qc_r);
447 		iniblk->rx_hi = hiint(&sc->sc_pib->qc_r);
448 		iniblk->tx_lo = loint(&sc->sc_pib->qc_t);
449 		iniblk->tx_hi = hiint(&sc->sc_pib->qc_t);
450 	}
451 	iniblk = &sc->sc_ib->qc_init;
452 	iniblk->mode = ifp->if_flags & IFF_PROMISC ? INIT_MODE_PRO : 0;
453 /*
454  * The multicast filter works "like LANCE".
455  */
456 	lance_setladrf(&sc->is_ec, iniblk->laddr);
457 
458 /*
459  * Now initialize the receive ring descriptors.  Because this routine can be
460  * called with outstanding I/O operations we check the ring descriptors for
461  * a non-zero 'rhost0' (or 'thost0') word and place those buffers back on
462  * the free list.
463  */
464 	for (i = 0; i < NRCV; i++) {
465 		rp = &sc->sc_ib->qc_r[i];
466 		ifrw = &sc->sc_ifr[i];
467 		rp->rmd1 = MCLBYTES;
468 		rp->rmd4 = loint(ifrw->ifrw_info);
469 		rp->rmd5 = hiint(ifrw->ifrw_info);
470 		rp->rmd3 = 0;			/* clear RMD3_OWN */
471 	}
472 	for (i = 0; i < NXMT; i++) {
473 		tp = &sc->sc_ib->qc_t[i];
474 		ifxp = &sc->sc_ifw[i];
475 		tp->tmd4 = loint(ifxp->ifw_info);
476 		tp->tmd5 = hiint(ifxp->ifw_info);
477 		tp->tmd3 = TMD3_OWN;
478 	}
479 
480 	sc->xnext = sc->xlast = sc->nxmit = 0;
481 	sc->rindex = 0;
482 	sc->nxtrcv = 0;
483 	sc->nrcv = 0;
484 
485 /*
486  * Now we tell the device the address of the INIT block.  The device
487  * _must_ be in the Turbo mode at this time.  The "START" command is
488  * then issued to the device.  A 1 second timeout is then started.
489  * When the interrupt occurs the IFF_UP|IFF_RUNNING state is entered and
490  * full operations will proceed.  If the timeout expires without an interrupt
491  * being received an error is printed, the flags cleared and the device left
492  * marked down.
493  */
494 	QT_WCSR(CSR_IBAL, loint(&sc->sc_pib->qc_init));
495 	QT_WCSR(CSR_IBAH, hiint(&sc->sc_pib->qc_init));
496 	QT_WCSR(CSR_SRQR, 2);
497 
498 	sc->is_if.if_flags |= IFF_RUNNING;
499 	return 0;
500 }
501 
502 /*
503  * Start output on interface.
504  */
505 
506 void
507 qtstart(struct ifnet *ifp)
508 {
509 	int len, nxmit;
510 	struct qt_softc *sc = ifp->if_softc;
511 	struct qt_tring *rp;
512 	struct mbuf *m = NULL;
513 
514 	for (nxmit = sc->nxmit; nxmit < NXMT; nxmit++) {
515 		IF_DEQUEUE(&sc->is_if.if_snd, m);
516 		if (m == NULL)
517 			break;
518 
519 		rp = &sc->sc_ib->qc_t[sc->xnext];
520 		if ((rp->tmd3 & TMD3_OWN) == 0)
521 			panic("qtstart");
522 
523 		bpf_mtap(ifp, m, BPF_D_OUT);
524 
525 		len = if_ubaput(&sc->sc_ifuba, &sc->sc_ifw[sc->xnext], m);
526 		if (len < MINPACKETSIZE)
527 			len = MINPACKETSIZE;
528 		rp->tmd3 = len & TMD3_BCT;	/* set length,clear ownership */
529 		QT_WCSR(CSR_ARQR, ARQR_TRQ);	/* tell device it has buffer */
530 
531 		if (++sc->xnext >= NXMT)
532 			sc->xnext = 0;
533 	}
534 	if (sc->nxmit != nxmit)
535 		sc->nxmit = nxmit;
536 	/* XXX - set OACTIVE */
537 }
538 
539 /*
540  * General interrupt service routine.  Receive, transmit, device start
541  * interrupts and timeouts come here.  Check for hard device errors and print a
542  * message if any errors are found.  If we are waiting for the device to
543  * START then check if the device is now running.
544  */
545 
546 void
547 qtintr(void *arg)
548 {
549 	struct qt_softc *sc = arg;
550 	struct ifnet *ifp = &sc->is_if;
551 	short status;
552 
553 	status = QT_RCSR(CSR_SRR);
554 	if (status < 0) {
555 		/* should we reset the device after a bunch of these errs? */
556 		qtsrr(sc, status);
557 	}
558 	if ((ifp->if_flags & IFF_UP) == 0)
559 		return; /* Unwanted interrupt */
560 	qtrint(sc);
561 	qttint(sc);
562 	qtstart(&sc->is_ec.ec_if);
563 }
564 
565 /*
566  * Transmit interrupt service.  Only called if there are outstanding transmit
567  * requests which could have completed.  The DELQA-YM doesn't provide the
568  * status bits telling the kind (receive, transmit) of interrupt.
569  */
570 
571 #define BBLMIS (TMD2_BBL|TMD2_MIS)
572 
573 void
574 qttint(struct qt_softc *sc)
575 {
576 	struct qt_tring *rp;
577 
578 	while (sc->nxmit > 0) {
579 		rp = &sc->sc_ib->qc_t[sc->xlast];
580 		if ((rp->tmd3 & TMD3_OWN) == 0)
581 			break;
582 		if_statinc(&sc->is_if, if_opackets);
583 /*
584  * Collisions don't count as output errors, but babbling and missing packets
585  * do count as output errors.
586  */
587 		if (rp->tmd2 & TMD2_CER)
588 			if_statinc(&sc->is_if, if_collisions);
589 		if ((rp->tmd0 & TMD0_ERR1) ||
590 		    ((rp->tmd2 & TMD2_ERR2) && (rp->tmd2 & BBLMIS))) {
591 #ifdef QTDEBUG
592 			char buf[100];
593 			snprintb(buf, sizeof(buf), TMD2_BITS, rp->tmd2);
594 			printf("%s: tmd2 %s\n", device_xname(sc->sc_dev), buf);
595 #endif
596 			if_statinc(&sc->is_if, if_oerrors);
597 		}
598 		if_ubaend(&sc->sc_ifuba, &sc->sc_ifw[sc->xlast]);
599 		if (++sc->xlast >= NXMT)
600 			sc->xlast = 0;
601 		sc->nxmit--;
602 	}
603 }
604 
605 /*
606  * Receive interrupt service.  Pull packet off the interface and put into
607  * a mbuf chain for processing later.
608  */
609 
610 void
611 qtrint(struct qt_softc *sc)
612 {
613 	struct qt_rring *rp;
614 	struct ifnet *ifp = &sc->is_ec.ec_if;
615 	struct mbuf *m;
616 	int len;
617 
618 	while (sc->sc_ib->qc_r[(int)sc->rindex].rmd3 & RMD3_OWN) {
619 		rp = &sc->sc_ib->qc_r[(int)sc->rindex];
620 		if ((rp->rmd0 & (RMD0_STP|RMD0_ENP)) != (RMD0_STP|RMD0_ENP)) {
621 			printf("%s: chained packet\n",
622 			    device_xname(sc->sc_dev));
623 			if_statinc(&sc->is_if, if_ierrors);
624 			goto rnext;
625 		}
626 		len = (rp->rmd1 & RMD1_MCNT) - ETHER_CRC_LEN;
627 
628 		if ((rp->rmd0 & RMD0_ERR3) || (rp->rmd2 & RMD2_ERR4)) {
629 #ifdef QTDEBUG
630 			char buf[100];
631 			snprintb(buf, sizeof(buf), RMD0_BITS, rp->rmd0);
632 			printf("%s: rmd0 %s\n", device_xname(sc->sc_dev), buf);
633 			snprintb(buf, sizeof(buf), RMD2_BITS, rp->rmd2);
634 			printf("%s: rmd2 %s\n", device_xname(sc->sc_dev), buf);
635 #endif
636 			if_statinc(&sc->is_if, if_ierrors);
637 			goto rnext;
638 		}
639 		m = if_ubaget(&sc->sc_ifuba, &sc->sc_ifr[(int)sc->rindex],
640 		    ifp, len);
641 		if (m == NULL) {
642 			if_statinc(&sc->is_if, if_ierrors);
643 			goto rnext;
644 		}
645 		if_percpuq_enqueue(ifp->if_percpuq, m);
646 rnext:
647 		--sc->nrcv;
648 		rp->rmd3 = 0;
649 		rp->rmd1 = MCLBYTES;
650 		if (++sc->rindex >= NRCV)
651 			sc->rindex = 0;
652 	}
653 	QT_WCSR(CSR_ARQR, ARQR_RRQ);	/* tell device it has buffer */
654 }
655 
656 int
657 qtioctl(struct ifnet *ifp, u_long cmd, void *data)
658 {
659 	int s, error;
660 
661 	s = splnet();
662 
663 	error = ether_ioctl(ifp, cmd, data);
664 	if (error == ENETRESET) {
665 		if (ifp->if_flags & IFF_RUNNING)
666 			error = qtinit(ifp);
667 		else
668 			error = 0;
669 	}
670 	splx(s);
671 	return error;
672 }
673 
674 void
675 qtsrr(struct qt_softc *sc, int srrbits)
676 {
677 	char buf[100];
678 
679 	snprintb(buf, sizeof(buf), SRR_BITS, srrbits);
680 	printf("%s: srr=%s\n", device_xname(sc->sc_dev), buf);
681 }
682 
683 /*
684  * Stop activity on the interface.
685  * Lose outstanding transmit requests. XXX - not good for multicast.
686  */
687 void
688 qtstop(struct ifnet *ifp, int disable)
689 {
690 	struct qt_softc *sc = ifp->if_softc;
691 	int i;
692 
693 	QT_WCSR(CSR_SRQR, 3);
694 	for (i = 0; i < 100; i++) {
695 		if ((QT_RCSR(CSR_SRR) & SRR_RESP) == 3)
696 			break;
697 	}
698 	if (QT_RCSR(CSR_SRR) & SRR_FES)
699 		qtsrr(sc, QT_RCSR(CSR_SRR));
700 	/* Forget already queued transmit requests */
701 	while (sc->nxmit > 0) {
702 		if_ubaend(&sc->sc_ifuba, &sc->sc_ifw[sc->xlast]);
703 		if (++sc->xlast >= NXMT)
704 			sc->xlast = 0;
705 		sc->nxmit--;
706 	}
707 	/* Handle late received packets */
708 	qtrint(sc);
709 	ifp->if_flags &= ~IFF_RUNNING;
710 }
711 
712 #ifdef notyet
713 /*
714  * Reset the device.  This moves it from DELQA-T mode to DELQA-Normal mode.
715  * After the reset put the device back in -T mode.  Then call qtinit() to
716  * reinitialize the ring structures and issue the 'timeout' for the "device
717  * started interrupt".
718  */
719 
720 void
721 qtreset(struct qt_softc *sc)
722 {
723 
724 	qtturbo(sc);
725 	qtinit(&sc->is_ec.ec_if);
726 }
727 #endif
728