xref: /netbsd-src/sys/arch/emips/ebus/if_le_ebus.c (revision 6db267571823ee3b0a1d61478df085a087f2e990)
1 /*	$NetBSD: if_le_ebus.c,v 1.24 2022/09/18 15:53:24 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 2010 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code was written by Alessandro Forin and Neil Pittman
8  * at Microsoft Research and contributed to The NetBSD Foundation
9  * by Microsoft Corporation.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: if_le_ebus.c,v 1.24 2022/09/18 15:53:24 thorpej Exp $");
35 
36 #include "opt_inet.h"
37 
38 
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/mbuf.h>
43 #include <sys/syslog.h>
44 #include <sys/socket.h>
45 #include <sys/device.h>
46 #include <sys/malloc.h>
47 #include <sys/ioctl.h>
48 #include <sys/errno.h>
49 
50 #include <net/if.h>
51 #include <net/if_dl.h>
52 #include <net/if_ether.h>
53 #include <net/if_media.h>
54 #include <net/bpf.h>
55 
56 #ifdef INET
57 #include <netinet/in.h>
58 #include <netinet/if_inarp.h>
59 #endif
60 
61 #include <sys/rndsource.h>
62 
63 #include <emips/ebus/ebusvar.h>
64 #include <emips/emips/machdep.h>
65 #include <machine/emipsreg.h>
66 
67 extern paddr_t kvtophys(vaddr_t);
68 
69 struct bufmap {
70 	struct mbuf *mbuf;
71 	paddr_t phys;
72 };
73 
74 struct enic_softc {
75 	device_t sc_dev;		/* base device glue */
76 	struct	ethercom sc_ethercom;	/* Ethernet common part */
77 	struct	ifmedia sc_media;	/* our supported media */
78 
79 	struct _Enic *sc_regs;		/* hw registers */
80 
81 	int	sc_havecarrier;		/* carrier status */
82 	void	*sc_sh;			/* shutdownhook cookie */
83 	int inited;
84 
85 	int sc_no_rd;
86 	int sc_n_recv;
87 	int sc_recv_h;
88 	/* BUGBUG really should be malloc-ed */
89 #define SC_MAX_N_RECV 64
90 	struct bufmap sc_recv[SC_MAX_N_RECV];
91 
92 	int sc_no_td;
93 	int sc_n_xmit;
94 	int sc_xmit_h;
95 	/* BUGBUG really should be malloc-ed */
96 #define SC_MAX_N_XMIT 16
97 	struct bufmap sc_xmit[SC_MAX_N_XMIT];
98 	bool sc_txbusy;
99 
100 #if DEBUG
101 	int xhit;
102 	int xmiss;
103 	int tfull;
104 	int tfull2;
105 	int brh;
106 	int rf;
107 	int bxh;
108 
109 	int it;
110 #endif
111 
112 	uint8_t sc_enaddr[ETHER_ADDR_LEN];
113 	uint8_t sc_pad[2];
114 	krndsource_t	rnd_source;
115 };
116 
117 void enic_reset(struct ifnet *);
118 int enic_init(struct ifnet *);
119 void enic_stop(struct ifnet *, int);
120 void enic_start(struct ifnet *);
121 void enic_shutdown(void *);
122 void enic_watchdog(struct ifnet *);
123 int enic_mediachange(struct ifnet *);
124 void enic_mediastatus(struct ifnet *, struct ifmediareq *);
125 int enic_ioctl(struct ifnet *, u_long, void *);
126 int enic_intr(void *, void *);
127 void enic_rint(struct enic_softc *, uint32_t, paddr_t);
128 void enic_tint(struct enic_softc *, uint32_t, paddr_t);
129 void enic_kill_xmit(struct enic_softc *);
130 void enic_post_recv(struct enic_softc *, struct mbuf *);
131 void enic_refill(struct enic_softc *);
132 static int enic_gethwinfo(struct enic_softc *);
133 int enic_put(struct enic_softc *, struct mbuf **);
134 
135 static int enic_match(device_t, cfdata_t, void *);
136 static void enic_attach(device_t, device_t, void *);
137 
138 CFATTACH_DECL_NEW(enic_emips, sizeof(struct enic_softc),
139     enic_match, enic_attach, NULL, NULL);
140 
141 int
142 enic_match(device_t parent, cfdata_t cf, void *aux)
143 {
144 	struct ebus_attach_args *d = aux;
145 	/* donno yet */
146 	struct _Enic *et = (struct _Enic *)d->ia_vaddr;
147 
148 	if (strcmp("enic", d->ia_name) != 0)
149 		return 0;
150 	if ((et == NULL) || (et->Tag != PMTTAG_ETHERNET))
151 		return 0;
152 	return 1;
153 }
154 
155 void
156 enic_attach(device_t parent, device_t self, void *aux)
157 {
158 	struct enic_softc *sc = device_private(self);
159 	struct ebus_attach_args *ia = aux;
160 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
161 
162 	sc->sc_regs = (struct _Enic *)(ia->ia_vaddr);
163 #if DEBUG
164 	printf(" virt=%p ", (void *)sc->sc_regs);
165 #endif
166 
167 	/* Get the MAC and the depth of the FIFOs */
168 	if (!enic_gethwinfo(sc)) {
169 		printf(" <cannot get hw info> DISABLED.\n");
170 		/*
171 		 * NB: caveat maintainer: make sure what we
172 		 * did NOT do below does not hurt the system
173 		 */
174 		return;
175 	}
176 
177 	sc->sc_dev = self;
178 	sc->sc_no_td = 0;
179 	sc->sc_havecarrier = 1; /* BUGBUG */
180 	sc->sc_recv_h = 0;
181 	sc->sc_xmit_h = 0;
182 	/* uhmm do I need to do this? */
183 	memset(sc->sc_recv, 0, sizeof sc->sc_recv);
184 	memset(sc->sc_xmit, 0, sizeof sc->sc_xmit);
185 
186 	/* Initialize ifnet structure. */
187 	strcpy(ifp->if_xname, device_xname(sc->sc_dev));
188 	ifp->if_softc = sc;
189 	ifp->if_start = enic_start;
190 	ifp->if_ioctl = enic_ioctl;
191 	ifp->if_watchdog = enic_watchdog;
192 	ifp->if_init = enic_init;
193 	ifp->if_stop = enic_stop;
194 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
195 	IFQ_SET_READY(&ifp->if_snd);
196 
197 	/* Initialize ifmedia structures. */
198 	sc->sc_ethercom.ec_ifmedia = &sc->sc_media;
199 	ifmedia_init(&sc->sc_media, 0, enic_mediachange, enic_mediastatus);
200 	ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_MANUAL, 0, NULL);
201 	ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_MANUAL);
202 
203 	/* Make sure the chip is stopped. */
204 	enic_stop(ifp, 0);
205 	sc->inited = 0;
206 
207 	/* Get the mac address and print it */
208 	printf(": eNIC [%d %d], address %s\n",
209 	    sc->sc_n_recv, sc->sc_n_xmit, ether_sprintf(sc->sc_enaddr));
210 
211 	/* claim 802.1q capability */
212 #if 0
213 	sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
214 #endif
215 
216 	/* Attach the interface. */
217 	if_attach(ifp);
218 	if_deferred_start_init(ifp, NULL);
219 	ether_ifattach(ifp, sc->sc_enaddr);
220 
221 	sc->sc_sh = shutdownhook_establish(enic_shutdown, ifp);
222 	if (sc->sc_sh == NULL)
223 		panic("enic_attach: cannot establish shutdown hook");
224 
225 	rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
226 			  RND_TYPE_NET, RND_FLAG_DEFAULT);
227 
228 	ebus_intr_establish(parent, (void *)ia->ia_cookie, IPL_NET,
229 	    enic_intr, sc);
230 }
231 
232 /*
233  * Beware: does not work while the nic is running
234  */
235 static int enic_gethwinfo(struct enic_softc *sc)
236 {
237 	uint8_t buffer[8];	/* 64bits max */
238 	PENIC_INFO hw = (PENIC_INFO)buffer;
239 	paddr_t phys = kvtophys((vaddr_t)&buffer[0]), phys2;
240 	int i;
241 
242 	/*
243 	 * First thing first, get the MAC address
244 	 */
245 	memset(buffer, 0, sizeof buffer);
246 	buffer[0] = ENIC_CMD_GET_ADDRESS;
247 	buffer[3] = ENIC_CMD_GET_ADDRESS;	/* bswap bug */
248 	sc->sc_regs->SizeAndFlags = (sizeof buffer) | ES_F_CMD;
249 	sc->sc_regs->BufferAddressHi32 = 0;
250 	sc->sc_regs->BufferAddressLo32 = phys; /* go! */
251 
252 	for (i = 0; i < 100; i++) {
253 		DELAY(100);
254 		if ((sc->sc_regs->Control & EC_OF_EMPTY) == 0)
255 			break;
256 	}
257 	if (i == 100)
258 		return 0;
259 
260 	phys2 = sc->sc_regs->BufferAddressLo32;
261 	if (phys2 != phys) {
262 		printf("enic uhu? %llx != %llx?\n",
263 		    (long long)phys, (long long)phys2);
264 		return 0;
265 	}
266 	memcpy(sc->sc_enaddr, buffer, ETHER_ADDR_LEN);
267 
268 	/*
269 	 * Next get the HW parameters
270 	 */
271 	memset(buffer, 0, sizeof buffer);
272 	buffer[0] = ENIC_CMD_GET_INFO;
273 	buffer[3] = ENIC_CMD_GET_INFO;	/* bswap bug */
274 	sc->sc_regs->SizeAndFlags = (sizeof buffer) | ES_F_CMD;
275 	sc->sc_regs->BufferAddressHi32 = 0;
276 	sc->sc_regs->BufferAddressLo32 = phys; /* go! */
277 
278 	for (i = 0; i < 100; i++) {
279 		DELAY(100);
280 		if ((sc->sc_regs->Control & EC_OF_EMPTY) == 0)
281 			break;
282 	}
283 	if (i == 100)
284 		return 0;
285 
286 	phys2 = sc->sc_regs->BufferAddressLo32;
287 	if (phys2 != phys) {
288 		printf("enic uhu2? %llx != %llx?\n",
289 		    (long long)phys, (long long)phys2);
290 		return 0;
291 	}
292 #if 0
293 	printf("enic: hwinfo: %x %x %x %x %x %x \n",
294 	    hw->InputFifoSize, hw->OutputFifoSize, hw->CompletionFifoSize,
295 	    hw->ErrorCount, hw->FramesDropped, hw->Reserved);
296 #endif
297 
298 	/*
299 	 * Get FIFO depths and cap them
300 	 */
301 	sc->sc_n_recv = hw->InputFifoSize;
302 	if (sc->sc_n_recv > SC_MAX_N_RECV)
303 		sc->sc_n_recv = SC_MAX_N_RECV;
304 	if (sc->sc_n_recv == 0) { /* sanity and compat with old hw/simulator */
305 		sc->sc_n_recv = 8;
306 		sc->sc_n_xmit = 4;
307 	} else {
308 		sc->sc_n_xmit = hw->OutputFifoSize;
309 		if (sc->sc_n_xmit > SC_MAX_N_XMIT)
310 			sc->sc_n_xmit = SC_MAX_N_XMIT;
311 	}
312 
313 	return 1;
314 }
315 
316 void
317 enic_reset(struct ifnet *ifp)
318 {
319 	int s;
320 
321 	s = splnet();
322 	enic_stop(ifp, 0);
323 	enic_init(ifp);
324 	splx(s);
325 }
326 
327 void
328 enic_stop(struct ifnet *ifp, int suspend)
329 {
330 	struct enic_softc *sc = ifp->if_softc;
331 
332 #if 0
333 	printf("enic_stop %x\n", sc->sc_regs->Control);
334 #endif
335 
336 	/*
337 	 * NB: only "ifconfig down" says suspend=1 (then "up" calls init)
338 	 * Could simply set RXDIS in this case
339 	 */
340 	sc->inited = 2;
341 	sc->sc_regs->Control = EC_RESET;
342 	sc->sc_no_rd = 0; /* they are gone */
343 	sc->sc_no_td = 0; /* they are gone */
344 }
345 
346 void
347 enic_shutdown(void *arg)
348 {
349 	struct ifnet *ifp = arg;
350 
351 	enic_stop(ifp, 0);
352 }
353 
354 void
355 enic_kill_xmit(struct enic_softc *sc)
356 {
357 	int i;
358 	struct mbuf *m;
359 
360 	for (i = 0; i < sc->sc_n_xmit; i++) {
361 		if ((m = sc->sc_xmit[i].mbuf) != NULL) {
362 			sc->sc_xmit[i].mbuf = NULL;
363 			sc->sc_xmit[i].phys = ~0;
364 			m_freem(m);
365 		}
366 	}
367 	sc->sc_no_td = 0;
368 	sc->sc_xmit_h = 0;
369 }
370 
371 void
372 enic_post_recv(struct enic_softc *sc, struct mbuf *m)
373 {
374 	int i, waitmode = M_DONTWAIT;
375 	paddr_t phys;
376 
377 #define rpostone(_p_) \
378     sc->sc_regs->SizeAndFlags = ES_F_RECV | MCLBYTES; \
379     sc->sc_regs->BufferAddressHi32 = 0; \
380     sc->sc_regs->BufferAddressLo32 = _p_;
381 #define tpostone(_p_,_s_) \
382     sc->sc_regs->SizeAndFlags = ES_F_XMIT | (_s_); \
383     sc->sc_regs->BufferAddressHi32 = 0; \
384     sc->sc_regs->BufferAddressLo32 = _p_;
385 
386 	/* Operational reload? */
387 	if (m != NULL) {
388 		/* But is the hw ready for it */
389 		if (sc->sc_regs->Control & EC_IF_FULL)
390 			goto no_room;
391 		/* Yes, find a spot. Include empty q case. */
392 		for (i = sc->sc_recv_h; i < sc->sc_n_recv; i++)
393 			if (sc->sc_recv[i].mbuf == NULL)
394 				goto found;
395 		for (i = 0; i < sc->sc_recv_h; i++)
396 			if (sc->sc_recv[i].mbuf == NULL)
397 				goto found;
398 		/* no spot, drop it (sigh) */
399  no_room:
400 #if DEBUG
401 		sc->rf++;
402 #endif
403 		m_freem(m);
404 		return;
405  found:
406 		phys = kvtophys((vaddr_t)m->m_data);
407 		sc->sc_recv[i].mbuf = m;
408 		sc->sc_recv[i].phys = phys;
409 		rpostone(phys);
410 		sc->sc_no_rd++;
411 		return;
412 	}
413 
414 	/* Repost after reset? */
415 	if (sc->inited) {
416 		/* order doesnt matter, might as well keep it clean */
417 		int j = 0;
418 		sc->sc_recv_h = 0;
419 		for (i = 0; i < sc->sc_n_recv; i++)
420 			if ((m = sc->sc_recv[i].mbuf) != NULL) {
421 				phys = sc->sc_recv[i].phys;
422 				sc->sc_recv[i].mbuf = NULL;
423 				sc->sc_recv[i].phys = ~0;
424 				sc->sc_recv[j].mbuf = m;
425 				sc->sc_recv[j].phys = phys;
426 #if DEBUG
427 				if (sc->sc_regs->Control & EC_IF_FULL)
428 					printf("?uhu? postrecv full? %d\n",
429 					    sc->sc_no_rd);
430 #endif
431 				sc->sc_no_rd++;
432 				rpostone(phys);
433 				j++;
434 			}
435 		/* Any holes left? */
436 		sc->inited = 1;
437 		if (j >= sc->sc_n_recv)
438 			return;	/* no, we are done */
439 		/* continue on with the loop below */
440 		i = j; m = NULL;
441 		goto fillem;
442 	}
443 
444 	/* Initial refill, we can wait */
445 	waitmode = M_WAIT;
446 	sc->sc_recv_h = 0;
447 	memset(sc->sc_recv, 0, sizeof(sc->sc_recv[0]) * sc->sc_n_recv);
448 	i = 0;
449  fillem:
450 	for (; i < sc->sc_n_recv; i++) {
451 		MGETHDR(m, waitmode, MT_DATA);
452 		if (m == 0)
453 			break;
454 		m_set_rcvif(m, &sc->sc_ethercom.ec_if);
455 		m->m_pkthdr.len = 0;
456 
457 		MCLGET(m, waitmode);
458 		if ((m->m_flags & M_EXT) == 0)
459 			break;
460 
461 		/*
462 		 * This offset aligns IP/TCP headers and helps performance
463 		 */
464 #if 1
465 #define ADJUST_MBUF_OFFSET(_m_) { \
466 	(_m_)->m_data += 2; \
467 	(_m_)->m_len -= 2; \
468 }
469 #else
470 #define ADJUST_MBUF_OFFSET(_m_)
471 #endif
472 
473 		m->m_len = MCLBYTES;
474 
475 		ADJUST_MBUF_OFFSET(m);
476 		phys = kvtophys((vaddr_t)m->m_data);
477 		sc->sc_recv[i].mbuf = m;
478 		sc->sc_recv[i].phys = phys;
479 #if DEBUG
480 		if (sc->sc_regs->Control & EC_IF_FULL)
481 			printf("?uhu? postrecv2 full? %d\n", sc->sc_no_rd);
482 #endif
483 		sc->sc_no_rd++;
484 		rpostone(phys);
485 		m = NULL;
486 	}
487 
488 	if (m)
489 		m_freem(m);
490 	sc->inited = 1;
491 }
492 
493 void enic_refill(struct enic_softc *sc)
494 {
495 	struct mbuf *m;
496 	int waitmode = M_DONTWAIT;
497 
498 	MGETHDR(m, waitmode, MT_DATA);
499 	if (m == NULL)
500 		return;
501 	m_set_rcvif(m, &sc->sc_ethercom.ec_if);
502 	m->m_pkthdr.len = 0;
503 
504 	MCLGET(m, waitmode);
505 	if ((m->m_flags & M_EXT) == 0) {
506 		m_freem(m);
507 		return;
508 	}
509 
510 	m->m_len = MCLBYTES;
511 	ADJUST_MBUF_OFFSET(m);
512 
513 	enic_post_recv(sc, m);
514 }
515 
516 int
517 enic_init(struct ifnet *ifp)
518 {
519 	struct enic_softc *sc = ifp->if_softc;
520 	uint32_t ctl;
521 
522 	/* no need to init many times unless we are in reset */
523 	if (sc->inited != 1) {
524 
525 		/* Cancel all xmit buffers */
526 		enic_kill_xmit(sc);
527 
528 		/* Re-post all recv buffers */
529 		enic_post_recv(sc, NULL);
530 	}
531 
532 	/* Start the eNIC */
533 	ifp->if_flags |= IFF_RUNNING;
534 	sc->sc_txbusy = false;
535 	ifp->if_timer = 0;
536 	ctl = sc->sc_regs->Control | EC_INTEN;
537 	ctl &= ~EC_RXDIS;
538 	sc->sc_regs->Control = ctl;
539 #if 0
540 	printf("enic_init <- %x\n", ctl);
541 #endif
542 
543 	if_schedule_deferred_start(ifp);
544 
545 	return 0;
546 }
547 
548 
549 void
550 enic_watchdog(struct ifnet *ifp)
551 {
552 	struct enic_softc *sc = ifp->if_softc;
553 
554 #if 0
555 	printf("enic_watch ctl=%x\n", sc->sc_regs->Control);
556 #endif
557 	log(LOG_ERR, "%s: device timeout\n", device_xname(sc->sc_dev));
558 	if_statinc(ifp, if_oerrors);
559 
560 	enic_reset(ifp);
561 }
562 
563 int
564 enic_mediachange(struct ifnet *ifp)
565 {
566 #if 0
567 	struct enic_softc *sc = ifp->if_softc;
568 #endif
569 	/* more code here.. */
570 
571 	return 0;
572 }
573 
574 void
575 enic_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
576 {
577 	struct enic_softc *sc = ifp->if_softc;
578 
579 	if ((ifp->if_flags & IFF_UP) == 0)
580 		return;
581 
582 	ifmr->ifm_status = IFM_AVALID;
583 	if (sc->sc_havecarrier)
584 		ifmr->ifm_status |= IFM_ACTIVE;
585 
586 	/* more code here someday.. */
587 }
588 
589 /*
590  * Process an ioctl request.
591  */
592 int
593 enic_ioctl(struct ifnet *ifp, u_long cmd, void *data)
594 {
595 	int s, error = 0;
596 
597 	s = splnet();
598 
599 	switch (cmd) {
600 	default:
601 		error = ether_ioctl(ifp, cmd, data);
602 		if (cmd == SIOCSIFADDR) {
603 			/*
604 			 * hackattack: NFS does not turn us back
605 			 * on after a stop. So.
606 			 */
607 #if 0
608 			printf("enic_ioctl(%lx)\n", cmd);
609 #endif
610 			enic_init(ifp);
611 		}
612 		if (error != ENETRESET)
613 			break;
614 		error = 0;
615 		if (cmd != SIOCADDMULTI && cmd != SIOCDELMULTI)
616 			break;
617 		if (ifp->if_flags & IFF_RUNNING) {
618 			enic_reset(ifp);
619 		}
620 		break;
621 	}
622 	splx(s);
623 
624 	return error;
625 }
626 
627 int
628 enic_intr(void *cookie, void *f)
629 {
630 	struct enic_softc *sc = cookie;
631 	uint32_t isr, saf, hi, lo, fl;
632 
633 	isr = sc->sc_regs->Control;
634 
635 	/* Make sure there is one and that we should take it */
636 	if ((isr & (EC_INTEN | EC_DONE)) != (EC_INTEN | EC_DONE))
637 		return 0;
638 
639 	if (isr & EC_ERROR) {
640 		printf("%s: internal error\n", device_xname(sc->sc_dev));
641 		enic_reset(&sc->sc_ethercom.ec_if);
642 		return 1;
643 	}
644 
645 	/*
646 	 * pull out all completed buffers
647 	 */
648 	while ((isr & EC_OF_EMPTY) == 0) {
649 
650 		/* beware, order matters */
651 		saf = sc->sc_regs->SizeAndFlags;
652 		hi  = sc->sc_regs->BufferAddressHi32; /* BUGBUG 64bit */
653 		lo  = sc->sc_regs->BufferAddressLo32; /* this pops the fifo */
654 		__USE(hi);
655 
656 		fl = saf & (ES_F_MASK &~ ES_F_DONE);
657 		if (fl == ES_F_RECV)
658 			enic_rint(sc, saf, lo);
659 		else if (fl == ES_F_XMIT)
660 			enic_tint(sc, saf, lo);
661 		else {
662 			/*
663 			 * we do not currently expect or care for
664 			 * command completions?
665 			 */
666 			if (fl != ES_F_CMD)
667 				printf("%s: invalid saf=x%x (lo=%x)\n",
668 				    device_xname(sc->sc_dev), saf, lo);
669 		}
670 
671 		isr = sc->sc_regs->Control;
672 	}
673 
674 	rnd_add_uint32(&sc->rnd_source, isr);
675 
676 	return 1;
677 }
678 
679 void
680 enic_rint(struct enic_softc *sc, uint32_t saf, paddr_t phys)
681 {
682 	struct mbuf *m;
683 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
684 	int len = saf & ES_S_MASK, i;
685 
686 	/* Find what buffer it is. Should be the first. */
687 	for (i = sc->sc_recv_h; i < sc->sc_n_recv; i++)
688 		if (sc->sc_recv[i].phys == phys)
689 			goto found;
690 	for (i = 0; i < sc->sc_recv_h; i++)
691 		if (sc->sc_recv[i].phys == phys)
692 			goto found;
693 
694 	/* uhu?? */
695 	printf("%s: bad recv phys %llx\n", device_xname(sc->sc_dev),
696 	    (long long)phys);
697 	if_statinc(ifp, if_ierrors);
698 	return;
699 
700 	/* got it, pop it */
701  found:
702 	sc->sc_no_rd--;
703 	m = sc->sc_recv[i].mbuf;
704 	sc->sc_recv[i].mbuf = NULL;
705 	sc->sc_recv[i].phys = ~0;
706 	if (i == sc->sc_recv_h) { /* should be */
707 		sc->sc_recv_h = (++i == sc->sc_n_recv) ? 0 : i;
708 	}
709 #if DEBUG
710 	else
711 		sc->brh++;
712 #endif
713 
714 	if (len <= sizeof(struct ether_header) ||
715 	    len > ((sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU) ?
716 		ETHER_VLAN_ENCAP_LEN + ETHERMTU + sizeof(struct ether_header) :
717 		ETHERMTU + sizeof(struct ether_header))) {
718 		if_statinc(ifp, if_ierrors);
719 
720 		/* reuse it */
721 		enic_post_recv(sc, m);
722 		return;
723 	}
724 
725 	/* Adjust size */
726 	m->m_pkthdr.len = len;
727 	m->m_len = len; /* recheck */
728 
729 	/* Pass the packet up. */
730 	if_percpuq_enqueue(ifp->if_percpuq, m);
731 
732 	/* Need to refill now */
733 	enic_refill(sc);
734 }
735 
736 void enic_tint(struct enic_softc *sc, uint32_t saf, paddr_t phys)
737 {
738 	struct mbuf *m;
739 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
740 	int i;
741 
742 #if DEBUG
743 	sc->it = 1;
744 #endif
745 
746 	/* BUGBUG should there be a per-buffer error bit in SAF? */
747 
748 	/* Find what buffer it is. Should be the first. */
749 	for (i = sc->sc_xmit_h; i < sc->sc_n_xmit; i++)
750 		if (sc->sc_xmit[i].phys == phys)
751 			goto found;
752 	for (i = 0; i < sc->sc_xmit_h; i++)
753 		if (sc->sc_xmit[i].phys == phys)
754 			goto found;
755 
756 	/* uhu?? */
757 	printf("%s: bad xmit phys %llx\n", device_xname(sc->sc_dev),
758 	    (long long)phys);
759 	if_statinc(ifp, if_oerrors);
760 	return;
761 
762 	/* got it, pop it */
763  found:
764 	m = sc->sc_xmit[i].mbuf;
765 	sc->sc_xmit[i].mbuf = NULL;
766 	sc->sc_xmit[i].phys = ~0;
767 	if (i == sc->sc_xmit_h) { /* should be */
768 		sc->sc_xmit_h = (++i == sc->sc_n_xmit) ? 0 : i;
769 	}
770 #if DEBUG
771 	else
772 		sc->bxh++;
773 #endif
774 	m_freem(m);
775 	if_statinc(ifp, if_opackets);
776 
777 	if (--sc->sc_no_td == 0)
778 		ifp->if_timer = 0;
779 
780 	sc->sc_txbusy = false;
781 	if_schedule_deferred_start(ifp);
782 #if DEBUG
783 	sc->it = 1;
784 #endif
785 }
786 
787 /*
788  * Setup output on interface.
789  * Get another datagram to send off of the interface queue, and map it to the
790  * interface before starting the output.
791  * Called only at splnet or interrupt level.
792  */
793 void
794 enic_start(struct ifnet *ifp)
795 {
796 	struct enic_softc *sc = ifp->if_softc;
797 	struct mbuf *m;
798 	int len, ix, s;
799 	paddr_t phys;
800 
801 #if DEBUG
802 	sc->it = 0;
803 #endif
804 
805 #if 0
806 	printf("enic_start(%x)\n", ifp->if_flags);
807 #endif
808 
809 	if ((ifp->if_flags & IFF_RUNNING) == 0)
810 		return;
811 
812 	s = splnet();	/* I know, I dont trust people.. */
813 
814 	ix = sc->sc_xmit_h;
815 	for (;;) {
816 
817 		/* Anything to do? */
818 		IFQ_POLL(&ifp->if_snd, m);
819 		if (m == NULL)
820 			break;
821 
822 		/* find a spot, if any */
823 		for (; ix < sc->sc_n_xmit; ix++)
824 			if (sc->sc_xmit[ix].mbuf == NULL)
825 				goto found;
826 		for (ix = 0; ix < sc->sc_xmit_h; ix++)
827 			if (sc->sc_xmit[ix].mbuf == NULL)
828 				goto found;
829 		/* oh well */
830 		sc->sc_txbusy = true;
831 #if DEBUG
832 		sc->tfull++;
833 #endif
834 		break;
835 
836  found:
837 		IFQ_DEQUEUE(&ifp->if_snd, m);
838 		if (m == NULL)
839 			break;
840 
841 		/*
842 		 * If BPF is listening on this interface, let it see the packet
843 		 * before we commit it to the wire.
844 		 */
845 		bpf_mtap(ifp, m, BPF_D_OUT);
846 
847 		/*
848 		 * Copy the mbuf chain into a contiguous transmit buffer.
849 		 */
850 		len = enic_put(sc, &m);
851 		if (len == 0)
852 			break;	/* sanity */
853 		if (len > (ETHERMTU + sizeof(struct ether_header))) {
854 			printf("enic? tlen %d > %d\n",
855 			    len, ETHERMTU + sizeof(struct ether_header));
856 			len = ETHERMTU + sizeof(struct ether_header);
857 		}
858 
859 		ifp->if_timer = 5;
860 
861 		/*
862 		 * Remember and post the buffer
863 		 */
864 		phys = kvtophys((vaddr_t)m->m_data);
865 		sc->sc_xmit[ix].mbuf = m;
866 		sc->sc_xmit[ix].phys = phys;
867 
868 		sc->sc_no_td++;
869 
870 		tpostone(phys, len);
871 
872 		if (sc->sc_regs->Control & EC_IF_FULL) {
873 			sc->sc_txbusy = true;
874 #if DEBUG
875 			sc->tfull2++;
876 #endif
877 			break;
878 		}
879 
880 		ix++;
881 	}
882 
883 	splx(s);
884 }
885 
886 int enic_put(struct enic_softc *sc, struct mbuf **pm)
887 {
888 	struct mbuf *n, *m = *pm, *mm;
889 	int len, tlen = 0, xlen = m->m_pkthdr.len;
890 	uint8_t *cp;
891 
892 #if 0
893 	/* drop garbage */
894 	tlen = xlen;
895 	for (; m; m = n) {
896 		len = m->m_len;
897 		if (len == 0) {
898 			n = m_free(m);
899 			if (m == *pm)
900 				*pm = n;
901 			continue;
902 		}
903 		tlen -= len;
904 		KASSERT(m != m->m_next);
905 		n = m->m_next;
906 		if (tlen <= 0)
907 			break;
908 	}
909 
910 	/*
911 	 * We might be done:
912 	 * (a) empty chain (b) only one segment (c) bad chain
913 	 */
914 	if (((m = *pm) == NULL) || (tlen > 0))
915 		xlen = 0;
916 #endif
917 
918 	if ((xlen == 0) || (xlen <= m->m_len)) {
919 #if DEBUG
920 		sc->xhit++;
921 #endif
922 		return xlen;
923 	}
924 
925 	/* Nope, true chain. Copy to contig :-(( */
926 	tlen = xlen;
927 	MGETHDR(n, M_NOWAIT, MT_DATA);
928 	if (n == NULL)
929 		goto Bad;
930 	m_set_rcvif(n, &sc->sc_ethercom.ec_if);
931 	n->m_pkthdr.len = tlen;
932 
933 	MCLGET(n, M_NOWAIT);
934 	if ((n->m_flags & M_EXT) == 0) {
935 		m_freem(n);
936 		goto Bad;
937 	}
938 
939 	n->m_len = tlen;
940 	cp = mtod(n, uint8_t *);
941 	for (; m && tlen; m = mm) {
942 
943 		len = m->m_len;
944 		if (len > tlen)
945 			len = tlen;
946 		if (len)
947 			memcpy(cp, mtod(m, void *), len);
948 
949 		cp += len;
950 		tlen -= len;
951 		mm = m_free(m);
952 
953 	}
954 
955 	*pm = n;
956 #if DEBUG
957 	sc->xmiss++;
958 #endif
959 	return (xlen);
960 
961  Bad:
962 	printf("enic_put: no mem?\n");
963 	m_freem(m);
964 	return 0;
965 }
966