xref: /netbsd/sys/arch/amiga/dev/if_es.c (revision bf9ec67e)
1 /*	$NetBSD: if_es.c,v 1.31 2002/03/06 22:07:39 mhitch Exp $ */
2 
3 /*
4  * Copyright (c) 1995 Michael L. Hitch
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *      This product includes software developed by Michael L. Hitch.
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34  * SMC 91C90 Single-Chip Ethernet Controller
35  */
36 #include "opt_ddb.h"
37 #include "opt_inet.h"
38 #include "opt_ns.h"
39 
40 #include <sys/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: if_es.c,v 1.31 2002/03/06 22:07:39 mhitch Exp $");
42 
43 #include "bpfilter.h"
44 
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/mbuf.h>
48 #include <sys/buf.h>
49 #include <sys/protosw.h>
50 #include <sys/socket.h>
51 #include <sys/syslog.h>
52 #include <sys/ioctl.h>
53 #include <sys/errno.h>
54 #include <sys/device.h>
55 
56 #include <net/if.h>
57 #include <net/if_dl.h>
58 #include <net/if_ether.h>
59 #include <net/if_media.h>
60 
61 #ifdef INET
62 #include <netinet/in.h>
63 #include <netinet/in_systm.h>
64 #include <netinet/in_var.h>
65 #include <netinet/ip.h>
66 #include <netinet/if_inarp.h>
67 #endif
68 
69 #ifdef NS
70 #include <netns/ns.h>
71 #include <netns/ns_if.h>
72 #endif
73 
74 #include <machine/cpu.h>
75 #include <machine/mtpr.h>
76 #include <amiga/amiga/device.h>
77 #include <amiga/amiga/isr.h>
78 #include <amiga/dev/zbusvar.h>
79 #include <amiga/dev/if_esreg.h>
80 
81 #define SWAP(x) (((x & 0xff) << 8) | ((x >> 8) & 0xff))
82 
83 #define	USEPKTBUF
84 
85 /*
86  * Ethernet software status per interface.
87  *
88  * Each interface is referenced by a network interface structure,
89  * es_if, which the routing code uses to locate the interface.
90  * This structure contains the output queue for the interface, its address, ...
91  */
92 struct	es_softc {
93 	struct	device sc_dev;
94 	struct	isr sc_isr;
95 	struct	ethercom sc_ethercom;	/* common Ethernet structures */
96 	struct	ifmedia sc_media;	/* our supported media */
97 	void	*sc_base;		/* base address of board */
98 	short	sc_iflags;
99 	unsigned short sc_intctl;
100 #ifdef ESDEBUG
101 	int	sc_debug;
102 	short	sc_intbusy;		/* counter for interrupt rentered */
103 	short	sc_smcbusy;		/* counter for other rentry checks */
104 #endif
105 };
106 
107 #if NBPFILTER > 0
108 #include <net/bpf.h>
109 #include <net/bpfdesc.h>
110 #endif
111 
112 #ifdef ESDEBUG
113 /* console error messages */
114 int	esdebug = 0;
115 int	estxints = 0;	/* IST_TX with TX enabled */
116 int	estxint2 = 0;	/* IST_TX active after IST_TX_EMPTY */
117 int	estxint3 = 0;	/* IST_TX interrupt processed */
118 int	estxint4 = 0;	/* ~TEMPTY counts */
119 int	estxint5 = 0;	/* IST_TX_EMPTY interrupts */
120 void	es_dump_smcregs(char *, union smcregs *);
121 #endif
122 
123 int esintr(void *);
124 void esstart(struct ifnet *);
125 void eswatchdog(struct ifnet *);
126 int esioctl(struct ifnet *, u_long, caddr_t);
127 void esrint(struct es_softc *);
128 void estint(struct es_softc *);
129 void esinit(struct es_softc *);
130 void esreset(struct es_softc *);
131 void esstop(struct es_softc *);
132 int esmediachange(struct ifnet *);
133 void esmediastatus(struct ifnet *, struct ifmediareq *);
134 
135 int esmatch(struct device *, struct cfdata *, void *);
136 void esattach(struct device *, struct device *, void *);
137 
138 struct cfattach es_ca = {
139 	sizeof(struct es_softc), esmatch, esattach
140 };
141 
142 int
143 esmatch(struct device *parent, struct cfdata *cfp, void *aux)
144 {
145 	struct zbus_args *zap = aux;
146 
147 	/* Ameristar A4066 ethernet card */
148 	if (zap->manid == 1053 && zap->prodid == 10)
149 		return(1);
150 
151 	return (0);
152 }
153 
154 /*
155  * Interface exists: make available by filling in network interface
156  * record.  System will initialize the interface when it is ready
157  * to accept packets.
158  */
159 void
160 esattach(struct device *parent, struct device *self, void *aux)
161 {
162 	struct es_softc *sc = (void *)self;
163 	struct zbus_args *zap = aux;
164 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
165 	unsigned long ser;
166 	u_int8_t myaddr[ETHER_ADDR_LEN];
167 
168 	sc->sc_base = zap->va;
169 
170 	/*
171 	 * Manufacturer decides the 3 first bytes, i.e. ethernet vendor ID.
172 	 * (Currently only Ameristar.)
173 	 */
174 	myaddr[0] = 0x00;
175 	myaddr[1] = 0x00;
176 	myaddr[2] = 0x9f;
177 
178 	/*
179 	 * Serial number for board contains last 3 bytes.
180 	 */
181 	ser = (unsigned long) zap->serno;
182 
183 	myaddr[3] = (ser >> 16) & 0xff;
184 	myaddr[4] = (ser >>  8) & 0xff;
185 	myaddr[5] = (ser      ) & 0xff;
186 
187 	/* Initialize ifnet structure. */
188 	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
189 	ifp->if_softc = sc;
190 	ifp->if_ioctl = esioctl;
191 	ifp->if_start = esstart;
192 	ifp->if_watchdog = eswatchdog;
193 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS |
194 	    IFF_MULTICAST;
195 
196 	ifmedia_init(&sc->sc_media, 0, esmediachange, esmediastatus);
197 	ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
198 	ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
199 
200 	/* Attach the interface. */
201 	if_attach(ifp);
202 	ether_ifattach(ifp, myaddr);
203 
204 	/* Print additional info when attached. */
205 	printf(": address %s\n", ether_sprintf(myaddr));
206 
207 	sc->sc_isr.isr_intr = esintr;
208 	sc->sc_isr.isr_arg = sc;
209 	sc->sc_isr.isr_ipl = 2;
210 	add_isr(&sc->sc_isr);
211 }
212 
213 #ifdef ESDEBUG
214 void
215 es_dump_smcregs(char *where, union smcregs *smc)
216 {
217 	u_short cur_bank = smc->b0.bsr & BSR_MASK;
218 
219 	printf("SMC registers %p from %s bank %04x\n", smc, where,
220 	    smc->b0.bsr);
221 	smc->b0.bsr = BSR_BANK0;
222 	printf("TCR %04x EPHSR %04x RCR %04x ECR %04x MIR %04x MCR %04x\n",
223 	    SWAP(smc->b0.tcr), SWAP(smc->b0.ephsr), SWAP(smc->b0.rcr),
224 	    SWAP(smc->b0.ecr), SWAP(smc->b0.mir), SWAP(smc->b0.mcr));
225 	smc->b1.bsr = BSR_BANK1;
226 	printf("CR %04x BAR %04x IAR %04x %04x %04x GPR %04x CTR %04x\n",
227 	    SWAP(smc->b1.cr), SWAP(smc->b1.bar), smc->b1.iar[0], smc->b1.iar[1],
228 	    smc->b1.iar[2], smc->b1.gpr, SWAP(smc->b1.ctr));
229 	smc->b2.bsr = BSR_BANK2;
230 	printf("MMUCR %04x PNR %02x ARR %02x FIFO %04x PTR %04x",
231 	    SWAP(smc->b2.mmucr), smc->b2.pnr, smc->b2.arr, smc->b2.fifo,
232 	    SWAP(smc->b2.ptr));
233 	printf(" DATA %04x %04x IST %02x MSK %02x\n", smc->b2.data,
234 	    smc->b2.datax, smc->b2.ist, smc->b2.msk);
235 	smc->b3.bsr = BSR_BANK3;
236 	printf("MT %04x %04x %04x %04x\n",
237 	    smc->b3.mt[0], smc->b3.mt[1], smc->b3.mt[2], smc->b3.mt[3]);
238 	smc->b3.bsr = cur_bank;
239 }
240 #endif
241 
242 void
243 esstop(struct es_softc *sc)
244 {
245 	union smcregs *smc = sc->sc_base;
246 
247 	/*
248 	 * Clear interrupt mask; disable all interrupts.
249 	 */
250 	smc->b2.bsr = BSR_BANK2;
251 	smc->b2.msk = 0;
252 
253 	/*
254 	 * Disable transmitter and receiver.
255 	 */
256 	smc->b0.bsr = BSR_BANK0;
257 	smc->b0.rcr = 0;
258 	smc->b0.tcr = 0;
259 
260 	/*
261 	 * Cancel watchdog timer.
262 	 */
263 	sc->sc_ethercom.ec_if.if_timer = 0;
264 }
265 
266 void
267 esinit(struct es_softc *sc)
268 {
269 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
270 	union smcregs *smc = sc->sc_base;
271 	int s;
272 
273 	s = splnet();
274 
275 #ifdef ESDEBUG
276 	if (ifp->if_flags & IFF_RUNNING)
277 		es_dump_smcregs("esinit", smc);
278 #endif
279 	smc->b0.bsr = BSR_BANK0;	/* Select bank 0 */
280 	smc->b0.rcr = RCR_EPH_RST;
281 	smc->b0.rcr = 0;
282 	smc->b3.bsr = BSR_BANK3;	/* Select bank 3 */
283 	smc->b3.mt[0] = 0;		/* clear Multicast table */
284 	smc->b3.mt[1] = 0;
285 	smc->b3.mt[2] = 0;
286 	smc->b3.mt[3] = 0;
287 	/* XXX set Multicast table from Multicast list */
288 	smc->b1.bsr = BSR_BANK1;	/* Select bank 1 */
289 	smc->b1.cr = CR_RAM32K | CR_NO_WAIT_ST | CR_SET_SQLCH;
290 	smc->b1.ctr = CTR_AUTO_RLSE | CTR_TE_ENA;
291 	smc->b1.iar[0] = *((unsigned short *) &LLADDR(ifp->if_sadl)[0]);
292 	smc->b1.iar[1] = *((unsigned short *) &LLADDR(ifp->if_sadl)[2]);
293 	smc->b1.iar[2] = *((unsigned short *) &LLADDR(ifp->if_sadl)[4]);
294 	smc->b2.bsr = BSR_BANK2;	/* Select bank 2 */
295 	smc->b2.mmucr = MMUCR_RESET;
296 	smc->b0.bsr = BSR_BANK0;	/* Select bank 0 */
297 	smc->b0.mcr = SWAP(0x0020);	/* reserve 8K for transmit buffers */
298 	smc->b0.tcr = TCR_PAD_EN | (TCR_TXENA + TCR_MON_CSN);
299 	smc->b0.rcr = RCR_FILT_CAR | RCR_STRIP_CRC | RCR_RXEN | RCR_ALLMUL;
300 	/* XXX add promiscuous flags */
301 	smc->b2.bsr = BSR_BANK2;	/* Select bank 2 */
302 	smc->b2.msk = sc->sc_intctl = MSK_RX_OVRN | MSK_RX | MSK_EPHINT;
303 
304 	/* Interface is now 'running', with no output active. */
305 	ifp->if_flags |= IFF_RUNNING;
306 	ifp->if_flags &= ~IFF_OACTIVE;
307 
308 	/* Attempt to start output, if any. */
309 	esstart(ifp);
310 
311 	splx(s);
312 }
313 
314 int
315 esintr(void *arg)
316 {
317 	struct es_softc *sc = arg;
318 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
319 	u_short intsts, intact;
320 	union smcregs *smc;
321 	int s = splnet();
322 
323 	smc = sc->sc_base;
324 #ifdef ESDEBUG
325 	while ((smc->b2.bsr & BSR_MASK) != BSR_BANK2 &&
326 	    ifp->if_flags & IFF_RUNNING) {
327 		printf("%s: intr BSR not 2: %04x\n", sc->sc_dev.dv_xname,
328 		    smc->b2.bsr);
329 		smc->b2.bsr = BSR_BANK2;
330 	}
331 #endif
332 	intsts = smc->b2.ist;
333 	intact = smc->b2.msk & intsts;
334 	if ((intact) == 0) {
335 		splx(s);
336 		return (0);
337 	}
338 #ifdef ESDEBUG
339 	if (esdebug)
340 		printf ("%s: esintr ist %02x msk %02x",
341 		    sc->sc_dev.dv_xname, intsts, smc->b2.msk);
342 	if (sc->sc_intbusy++) {
343 		printf("%s: esintr re-entered\n", sc->sc_dev.dv_xname);
344 		panic("esintr re-entered");
345 	}
346 	if (sc->sc_smcbusy)
347 		printf("%s: esintr interrupted busy %d\n", sc->sc_dev.dv_xname,
348 		    sc->sc_smcbusy);
349 #endif
350 	smc->b2.msk = 0;
351 #ifdef ESDEBUG
352 	if (esdebug)
353 		printf ("=>%02x%02x pnr %02x arr %02x fifo %04x\n",
354 		    smc->b2.ist, smc->b2.ist, smc->b2.pnr, smc->b2.arr,
355 		    smc->b2.fifo);
356 #endif
357 	if (intact & IST_ALLOC) {
358 		sc->sc_intctl &= ~MSK_ALLOC;
359 #ifdef ESDEBUG
360 		if (esdebug || 1)
361 			printf ("%s: ist %02x", sc->sc_dev.dv_xname,
362 			    intsts);
363 #endif
364 		if ((smc->b2.arr & ARR_FAILED) == 0) {
365 			u_char save_pnr;
366 #ifdef ESDEBUG
367 			if (esdebug || 1)
368 				printf (" arr %02x\n", smc->b2.arr);
369 #endif
370 			save_pnr = smc->b2.pnr;
371 			smc->b2.pnr = smc->b2.arr;
372 			smc->b2.mmucr = MMUCR_RLSPKT;
373 			while (smc->b2.mmucr & MMUCR_BUSY)
374 				;
375 			smc->b2.pnr = save_pnr;
376 			ifp->if_flags &= ~IFF_OACTIVE;
377 			ifp->if_timer = 0;
378 		}
379 #ifdef ESDEBUG
380 		else if (esdebug || 1)
381 			printf (" IST_ALLOC with ARR_FAILED?\n");
382 #endif
383 	}
384 #ifdef ESDEBUG
385 	while ((smc->b2.bsr & BSR_MASK) != BSR_BANK2) {
386 		printf("%s: intr+ BSR not 2: %04x\n", sc->sc_dev.dv_xname,
387 		    smc->b2.bsr);
388 		smc->b2.bsr = BSR_BANK2;
389 	}
390 #endif
391 	while ((smc->b2.fifo & FIFO_REMPTY) == 0) {
392 		esrint(sc);
393 	}
394 #ifdef ESDEBUG
395 	while ((smc->b2.bsr & BSR_MASK) != BSR_BANK2) {
396 		printf("%s: intr++ BSR not 2: %04x\n", sc->sc_dev.dv_xname,
397 		    smc->b2.bsr);
398 		smc->b2.bsr = BSR_BANK2;
399 	}
400 #endif
401 	if (intact & IST_RX_OVRN) {
402 		printf ("%s: Overrun ist %02x", sc->sc_dev.dv_xname,
403 		    intsts);
404 		smc->b2.ist = ACK_RX_OVRN;
405 		printf ("->%02x\n", smc->b2.ist);
406 		ifp->if_ierrors++;
407 	}
408 	if (intact & IST_TX_EMPTY) {
409 		u_short ecr;
410 #ifdef ESDEBUG
411 		if (esdebug)
412 			printf ("%s: TX EMPTY %02x",
413 			    sc->sc_dev.dv_xname, intsts);
414 		++estxint5;		/* count # IST_TX_EMPTY ints */
415 #endif
416 		smc->b2.ist = ACK_TX_EMPTY;
417 		sc->sc_intctl &= ~(MSK_TX_EMPTY | MSK_TX);
418 		ifp->if_timer = 0;
419 #ifdef ESDEBUG
420 		if (esdebug)
421 			printf ("->%02x intcl %x pnr %02x arr %02x\n",
422 			    smc->b2.ist, sc->sc_intctl, smc->b2.pnr,
423 			    smc->b2.arr);
424 #endif
425 		if (smc->b2.ist & IST_TX) {
426 			intact |= IST_TX;
427 #ifdef ESDEBUG
428 			++estxint2;		/* count # TX after TX_EMPTY */
429 #endif
430 		} else {
431 			smc->b0.bsr = BSR_BANK0;
432 			ecr = smc->b0.ecr;	/* Get error counters */
433 			if (ecr & 0xff00)
434 				ifp->if_collisions += ((ecr >> 8) & 15) +
435 				    ((ecr >> 11) & 0x1e);
436 			smc->b2.bsr = BSR_BANK2;
437 #if 0
438 			smc->b2.mmucr = MMUCR_RESET_TX; /* XXX reset TX FIFO */
439 #endif
440 		}
441 	}
442 	if (intact & IST_TX) {
443 		u_char tx_pnr, save_pnr;
444 		u_short save_ptr, ephsr, tcr;
445 		int n = 0;
446 #ifdef ESDEBUG
447 		if (esdebug) {
448 			printf ("%s: TX INT ist %02x",
449 			    sc->sc_dev.dv_xname, intsts);
450 			printf ("->%02x\n", smc->b2.ist);
451 		}
452 		++estxint3;			/* count # IST_TX */
453 #endif
454 zzzz:
455 #ifdef ESDEBUG
456 		++estxint4;			/* count # ~TEMPTY */
457 #endif
458 		smc->b0.bsr = BSR_BANK0;
459 		ephsr = smc->b0.ephsr;		/* get EPHSR */
460 		tcr = smc->b0.tcr;		/* and TCR */
461 		smc->b2.bsr = BSR_BANK2;
462 		save_ptr = smc->b2.ptr;
463 		save_pnr = smc->b2.pnr;
464 		tx_pnr = smc->b2.fifo >> 8;	/* pktno from completion fifo */
465 		smc->b2.pnr = tx_pnr;		/* set TX packet number */
466 		smc->b2.ptr = PTR_READ;		/* point to status word */
467 #if 0 /* XXXX */
468 		printf("%s: esintr TXINT IST %02x PNR %02x(%d)",
469 		    sc->sc_dev.dv_xname, smc->b2.ist,
470 		    tx_pnr, n);
471 		printf(" Status %04x", smc->b2.data);
472 		printf(" EPHSR %04x\n", ephsr);
473 #endif
474 		if ((smc->b2.data & EPHSR_TX_SUC) == 0 && (tcr & TCR_TXENA) == 0) {
475 			/*
476 			 * Transmitter was stopped for some error.  Enqueue
477 			 * the packet again and restart the transmitter.
478 			 * May need some check to limit the number of retries.
479 			 */
480 			smc->b2.mmucr = MMUCR_ENQ_TX;
481 			smc->b0.bsr = BSR_BANK0;
482 			smc->b0.tcr |= TCR_TXENA;
483 			smc->b2.bsr = BSR_BANK2;
484 			ifp->if_oerrors++;
485 			sc->sc_intctl |= MSK_TX_EMPTY | MSK_TX;
486 		} else {
487 			/*
488 			 * This shouldn't have happened:  IST_TX indicates
489 			 * the TX completion FIFO is not empty, but the
490 			 * status for the packet on the completion FIFO
491 			 * shows that the transmit was sucessful.  Since
492 			 * AutoRelease is being used, a sucessful transmit
493 			 * should not result in a packet on the completion
494 			 * FIFO.  Also, that packet doesn't seem to want
495 			 * to be acknowledged.  If this occurs, just reset
496 			 * the TX FIFOs.
497 			 */
498 #if 1
499 			if (smc->b2.ist & IST_TX_EMPTY) {
500 				smc->b2.mmucr = MMUCR_RESET_TX;
501 				sc->sc_intctl &= ~(MSK_TX_EMPTY | MSK_TX);
502 				ifp->if_timer = 0;
503 			}
504 #endif
505 #ifdef ESDEBUG
506 			++estxints;	/* count IST_TX with TX enabled */
507 #endif
508 		}
509 		smc->b2.pnr = save_pnr;
510 		smc->b2.ptr = save_ptr;
511 		smc->b2.ist = ACK_TX;
512 
513 		if ((smc->b2.fifo & FIFO_TEMPTY) == 0 && n++ < 32) {
514 #if 0 /* XXXX */
515 			printf("%s: multiple TX int(%2d) pnr %02x ist %02x fifo %04x",
516 			    sc->sc_dev.dv_xname, n, tx_pnr, smc->b2.ist, smc->b2.fifo);
517 			smc->w2.istmsk = ACK_TX << 8;
518 			printf(" %04x\n", smc->b2.fifo);
519 #endif
520 			if (tx_pnr != (smc->b2.fifo >> 8))
521 				goto zzzz;
522 		}
523 	}
524 	if (intact & IST_EPHINT) {
525 		ifp->if_oerrors++;
526 		esreset(sc);
527 	}
528 	/* output packets */
529 	estint(sc);
530 #ifdef ESDEBUG
531 	while ((smc->b2.bsr & BSR_MASK) != BSR_BANK2) {
532 		printf("%s: intr+++ BSR not 2: %04x\n", sc->sc_dev.dv_xname,
533 		    smc->b2.bsr);
534 		smc->b2.bsr = BSR_BANK2;
535 	}
536 #endif
537 	smc->b2.msk = sc->sc_intctl;
538 #ifdef ESDEBUG
539 	if (--sc->sc_intbusy) {
540 		printf("%s: esintr busy on exit\n", sc->sc_dev.dv_xname);
541 		panic("esintr busy on exit");
542 	}
543 #endif
544 	splx(s);
545 	return (1);
546 }
547 
548 void
549 esrint(struct es_softc *sc)
550 {
551 	union smcregs *smc = sc->sc_base;
552 	u_short len;
553 	short cnt;
554 	u_short pktctlw, pktlen, *buf;
555 	volatile u_short *data;
556 #if 0
557 	u_long *lbuf;
558 	volatile u_long *ldata;
559 #endif
560 	struct ifnet *ifp;
561 	struct mbuf *top, **mp, *m;
562 #ifdef USEPKTBUF
563 	u_char *b, pktbuf[1530];
564 #endif
565 #ifdef ESDEBUG
566 	int i;
567 #endif
568 
569 	ifp = &sc->sc_ethercom.ec_if;
570 #ifdef ESDEBUG
571 	if (esdebug)
572 		printf ("%s: esrint fifo %04x", sc->sc_dev.dv_xname,
573 		    smc->b2.fifo);
574 	if (sc->sc_smcbusy++) {
575 		printf("%s: esrint re-entered\n", sc->sc_dev.dv_xname);
576 		panic("esrint re-entered");
577 	}
578 	while ((smc->b2.bsr & BSR_MASK) != BSR_BANK2) {
579 		printf("%s: rint BSR not 2: %04x\n", sc->sc_dev.dv_xname,
580 		    smc->b2.bsr);
581 		smc->b2.bsr = BSR_BANK2;
582 	}
583 #endif
584 	data = (u_short *)&smc->b2.data;
585 	smc->b2.ptr = PTR_RCV | PTR_AUTOINCR | PTR_READ | SWAP(0x0002);
586 	(void) smc->b2.mmucr;
587 #ifdef ESDEBUG
588 	if (esdebug)
589 		printf ("->%04x", smc->b2.fifo);
590 #endif
591 	len = *data;
592 	len = SWAP(len);	/* Careful of macro side-effects */
593 #ifdef ESDEBUG
594 	if (esdebug)
595 		printf (" length %d", len);
596 #endif
597 	smc->b2.ptr = PTR_RCV | (PTR_AUTOINCR + PTR_READ) | SWAP(0x0000);
598 	(void) smc->b2.mmucr;
599 	pktctlw = *data;
600 	pktlen = *data;
601 	pktctlw = SWAP(pktctlw);
602 	pktlen = SWAP(pktlen) - 6;
603 	if (pktctlw & RFSW_ODDFRM)
604 		pktlen++;
605 	if (len > 1530) {
606 		printf("%s: Corrupted packet length-sts %04x bytcnt %04x len %04x bank %04x\n",
607 		    sc->sc_dev.dv_xname, pktctlw, pktlen, len, smc->b2.bsr);
608 		/* XXX ignore packet, or just truncate? */
609 #if defined(ESDEBUG) && defined(DDB)
610 		if ((smc->b2.bsr & BSR_MASK) != BSR_BANK2)
611 			Debugger();
612 #endif
613 		smc->b2.bsr = BSR_BANK2;
614 		smc->b2.mmucr = MMUCR_REMRLS_RX;
615 		while (smc->b2.mmucr & MMUCR_BUSY)
616 			;
617 		++ifp->if_ierrors;
618 #ifdef ESDEBUG
619 		if (--sc->sc_smcbusy) {
620 			printf("%s: esrintr busy on bad packet exit\n",
621 			    sc->sc_dev.dv_xname);
622 			panic("esrintr busy on exit");
623 		}
624 #endif
625 		return;
626 	}
627 #ifdef USEPKTBUF
628 #if 0
629 	lbuf = (u_long *) pktbuf;
630 	ldata = (u_long *)data;
631 	cnt = (len - 4) / 4;
632 	while (cnt--)
633 		*lbuf++ = *ldata;
634 	if ((len - 4) & 2) {
635 		buf = (u_short *) lbuf;
636 		*buf = *data;
637 	}
638 #else
639 	buf = (u_short *)pktbuf;
640 	cnt = (len - 4) / 2;
641 	while (cnt--)
642 		*buf++ = *data;
643 #endif
644 	smc->b2.mmucr = MMUCR_REMRLS_RX;
645 	while (smc->b2.mmucr & MMUCR_BUSY)
646 		;
647 #ifdef ESDEBUG
648 	if (pktctlw & (RFSW_ALGNERR | RFSW_BADCRC | RFSW_TOOLNG | RFSW_TOOSHORT)) {
649 		printf ("%s: Packet error %04x\n", sc->sc_dev.dv_xname, pktctlw);
650 		/* count input error? */
651 	}
652 	if (esdebug) {
653 		printf (" pktctlw %04x pktlen %04x fifo %04x\n", pktctlw, pktlen,
654 		    smc->b2.fifo);
655 		for (i = 0; i < pktlen; ++i)
656 			printf ("%02x%s", pktbuf[i], ((i & 31) == 31) ? "\n" :
657 			    "");
658 		if (i & 31)
659 			printf ("\n");
660 	}
661 #endif
662 #else	/* USEPKTBUF */
663 	/* XXX copy directly from controller to mbuf */
664 #ifdef ESDEBUG
665 	if (pktctlw & (RFSW_ALGNERR | RFSW_BADCRC | RFSW_TOOLNG | RFSW_TOOSHORT)) {
666 		printf ("%s: Packet error %04x\n", sc->sc_dev.dv_xname, pktctlw);
667 		/* count input error? */
668 	}
669 	if (esdebug) {
670 		printf (" pktctlw %04x pktlen %04x fifo %04x\n", pktctlw, pktlen,
671 		    smc->b2.fifo);
672 	}
673 #endif
674 #endif /* USEPKTBUF */
675 	ifp->if_ipackets++;
676 	MGETHDR(m, M_DONTWAIT, MT_DATA);
677 	if (m == NULL)
678 		return;
679 	m->m_pkthdr.rcvif = ifp;
680 	m->m_pkthdr.len = pktlen;
681 	len = MHLEN;
682 	top = NULL;
683 	mp = &top;
684 #ifdef USEPKTBUF
685 	b = pktbuf;
686 #endif
687 	while (pktlen > 0) {
688 		if (top) {
689 			MGET(m, M_DONTWAIT, MT_DATA);
690 			if (m == 0) {
691 				m_freem(top);
692 				return;
693 			}
694 			len = MLEN;
695 		}
696 		if (pktlen >= MINCLSIZE) {
697 			MCLGET(m, M_DONTWAIT);
698 			if (m->m_flags & M_EXT)
699 				len = MCLBYTES;
700 		}
701 		m->m_len = len = min(pktlen, len);
702 #ifdef USEPKTBUF
703 		bcopy((caddr_t)b, mtod(m, caddr_t), len);
704 		b += len;
705 #else	/* USEPKTBUF */
706 		buf = mtod(m, u_short *);
707 		cnt = len / 2;
708 		while (cnt--)
709 			*buf++ = *data;
710 		if (len & 1)
711 			*buf = *data;	/* XXX should be byte store */
712 #ifdef ESDEBUG
713 		if (esdebug) {
714 			buf = mtod(m, u_short *);
715 			for (i = 0; i < len; ++i)
716 				printf ("%02x%s", ((u_char *)buf)[i],
717 				    ((i & 31) == 31) ? "\n" : "");
718 			if (i & 31)
719 				printf ("\n");
720 		}
721 #endif
722 #endif	/* USEPKTBUF */
723 		pktlen -= len;
724 		*mp = m;
725 		mp = &m->m_next;
726 	}
727 #ifndef USEPKTBUF
728 	smc->b2.mmucr = MMUCR_REMRLS_RX;
729 	while (smc->b2.mmucr & MMUCR_BUSY)
730 		;
731 #endif
732 #if NBPFILTER > 0
733 	/*
734 	 * Check if there's a BPF listener on this interface.  If so, hand off
735 	 * the raw packet to bpf.
736 	 */
737 	if (ifp->if_bpf)
738 		bpf_mtap(ifp->if_bpf, top);
739 #endif
740 	(*ifp->if_input)(ifp, top);
741 #ifdef ESDEBUG
742 	if (--sc->sc_smcbusy) {
743 		printf("%s: esintr busy on exit\n", sc->sc_dev.dv_xname);
744 		panic("esintr busy on exit");
745 	}
746 #endif
747 }
748 
749 void
750 estint(struct es_softc *sc)
751 {
752 
753 	esstart(&sc->sc_ethercom.ec_if);
754 }
755 
756 void
757 esstart(struct ifnet *ifp)
758 {
759 	struct es_softc *sc = ifp->if_softc;
760 	union smcregs *smc = sc->sc_base;
761 	struct mbuf *m0, *m;
762 #ifdef USEPKTBUF
763 	u_short pktbuf[ETHERMTU + 2];
764 #else
765 	u_short oddbyte, needbyte;
766 #endif
767 	u_short pktctlw, pktlen;
768 	u_short *buf;
769 	volatile u_short *data;
770 #if 0
771 	u_long *lbuf;
772 	volatile u_long *ldata;
773 #endif
774 	short cnt;
775 	int i;
776 	u_char active_pnr;
777 
778 	if ((sc->sc_ethercom.ec_if.if_flags & (IFF_RUNNING | IFF_OACTIVE)) !=
779 	    IFF_RUNNING)
780 		return;
781 
782 #ifdef ESDEBUG
783 	if (sc->sc_smcbusy++) {
784 		printf("%s: esstart re-entered\n", sc->sc_dev.dv_xname);
785 		panic("esstart re-entred");
786 	}
787 	while ((smc->b2.bsr & BSR_MASK) != BSR_BANK2) {
788 		printf("%s: esstart BSR not 2: %04x\n", sc->sc_dev.dv_xname,
789 		    smc->b2.bsr);
790 		smc->b2.bsr = BSR_BANK2;
791 	}
792 #endif
793 	for (;;) {
794 #ifdef ESDEBUG
795 		u_short start_ptr, end_ptr;
796 #endif
797 		/*
798 		 * Sneak a peek at the next packet to get the length
799 		 * and see if the SMC 91C90 can accept it.
800 		 */
801 		m = sc->sc_ethercom.ec_if.if_snd.ifq_head;
802 		if (!m)
803 			break;
804 #ifdef ESDEBUG
805 		if (esdebug && (m->m_next || m->m_len & 1))
806 			printf("%s: esstart m_next %p m_len %d\n",
807 			    sc->sc_dev.dv_xname, m->m_next, m->m_len);
808 #endif
809 		for (m0 = m, pktlen = 0; m0; m0 = m0->m_next)
810 			pktlen += m0->m_len;
811 		pktctlw = 0;
812 		pktlen += 4;
813 		if (pktlen & 1)
814 			++pktlen;	/* control byte after last byte */
815 		else
816 			pktlen += 2;	/* control byte after pad byte */
817 		smc->b2.mmucr = MMUCR_ALLOC | (pktlen & 0x0700);
818 		for (i = 0; i <= 5; ++i)
819 			if ((smc->b2.arr & ARR_FAILED) == 0)
820 				break;
821 		if (smc->b2.arr & ARR_FAILED) {
822 			sc->sc_ethercom.ec_if.if_flags |= IFF_OACTIVE;
823 			sc->sc_intctl |= MSK_ALLOC;
824 			sc->sc_ethercom.ec_if.if_timer = 5;
825 			break;
826 		}
827 		active_pnr = smc->b2.pnr = smc->b2.arr;
828 
829 #ifdef ESDEBUG
830 		while ((smc->b2.bsr & BSR_MASK) != BSR_BANK2) {
831 			printf("%s: esstart+ BSR not 2: %04x\n", sc->sc_dev.dv_xname,
832 			    smc->b2.bsr);
833 			smc->b2.bsr = BSR_BANK2;
834 		}
835 #endif
836 		IF_DEQUEUE(&sc->sc_ethercom.ec_if.if_snd, m);
837 		smc->b2.ptr = PTR_AUTOINCR;
838 		(void) smc->b2.mmucr;
839 		data = (u_short *)&smc->b2.data;
840 		*data = SWAP(pktctlw);
841 		*data = SWAP(pktlen);
842 #ifdef ESDEBUG
843 		while ((smc->b2.bsr & BSR_MASK) != BSR_BANK2) {
844 			printf("%s: esstart++ BSR not 2: %04x\n", sc->sc_dev.dv_xname,
845 			    smc->b2.bsr);
846 			smc->b2.bsr = BSR_BANK2;
847 		}
848 #endif
849 #ifdef USEPKTBUF
850 		i = 0;
851 		for (m0 = m; m; m = m->m_next) {
852 			bcopy(mtod(m, caddr_t), (char *)pktbuf + i, m->m_len);
853 			i += m->m_len;
854 		}
855 
856 		if (i & 1)	/* Figure out where to put control byte */
857 			pktbuf[i/2] = (pktbuf[i/2] & 0xff00) | CTLB_ODD;
858 		else
859 			pktbuf[i/2] = 0;
860 		pktlen -= 4;
861 #ifdef ESDEBUG
862 		if (pktlen > sizeof(pktbuf) && i > (sizeof(pktbuf) * 2))
863 			printf("%s: esstart packet longer than pktbuf\n",
864 			    sc->sc_dev.dv_xname);
865 #endif
866 #if 0 /* doesn't quite work? */
867 		lbuf = (u_long *)(pktbuf);
868 		ldata = (u_long *)data;
869 		cnt = pktlen / 4;
870 		while(cnt--)
871 			*ldata = *lbuf++;
872 		if (pktlen & 2) {
873 			buf = (u_short *)lbuf;
874 			*data = *buf;
875 		}
876 #else
877 #ifdef ESDEBUG
878 		while ((smc->b2.bsr & BSR_MASK) != BSR_BANK2) {
879 			printf("%s: esstart++2 BSR not 2: %04x\n", sc->sc_dev.dv_xname,
880 			    smc->b2.bsr);
881 			smc->b2.bsr = BSR_BANK2;
882 		}
883 		start_ptr = SWAP(smc->b2.ptr);	/* save PTR before copy */
884 #endif
885 		buf = pktbuf;
886 		cnt = pktlen / 2;
887 		while (cnt--)
888 			*data = *buf++;
889 #ifdef ESDEBUG
890 		end_ptr = SWAP(smc->b2.ptr);	/* save PTR after copy */
891 #endif
892 #endif
893 #else	/* USEPKTBUF */
894 		pktctlw = 0;
895 		oddbyte = needbyte = 0;
896 		for (m0 = m; m; m = m->m_next) {
897 			buf = mtod(m, u_short *);
898 			cnt = m->m_len / 2;
899 			if (needbyte) {
900 				oddbyte |= *buf >> 8;
901 				*data = oddbyte;
902 			}
903 			while (cnt--)
904 				*data = *buf++;
905 			if (m->m_len & 1)
906 				pktctlw = (*buf & 0xff00) | CTLB_ODD;
907 			if (m->m_len & 1 && m->m_next)
908 				printf("%s: esstart odd byte count in mbuf\n",
909 				    sc->sc_dev.dv_xname);
910 		}
911 		*data = pktctlw;
912 #endif	/* USEPKTBUF */
913 		while ((smc->b2.bsr & BSR_MASK) != BSR_BANK2) {
914 			/*
915 			 * The bank select register has changed.  This seems
916 			 * to happen with my A2000/Zeus once in a while.  It
917 			 * appears that the Ethernet chip resets while
918 			 * copying the transmit buffer.  Requeue the current
919 			 * transmit buffer and reinitialize the interface.
920 			 * The initialize routine will take care of
921 			 * retransmitting the buffer.  mhitch
922 			 */
923 #ifdef DIAGNOSTIC
924 			printf("%s: esstart+++ BSR not 2: %04x\n",
925 			    sc->sc_dev.dv_xname, smc->b2.bsr);
926 #endif
927 			smc->b2.bsr = BSR_BANK2;
928 #ifdef ESDEBUG
929 			printf("start_ptr %04x end_ptr %04x cur ptr %04x\n",
930 			    start_ptr, end_ptr, SWAP(smc->b2.ptr));
931 			--sc->sc_smcbusy;
932 #endif
933 			IF_PREPEND(&sc->sc_ethercom.ec_if.if_snd, m0);
934 			esinit(sc);	/* It's really hosed - reset */
935 			return;
936 		}
937 		smc->b2.mmucr = MMUCR_ENQ_TX;
938 		if (smc->b2.pnr != active_pnr)
939 			printf("%s: esstart - PNR changed %x->%x\n",
940 			    sc->sc_dev.dv_xname, active_pnr, smc->b2.pnr);
941 #if NBPFILTER > 0
942 		if (sc->sc_ethercom.ec_if.if_bpf)
943 			bpf_mtap(sc->sc_ethercom.ec_if.if_bpf, m0);
944 #endif
945 		m_freem(m0);
946 		sc->sc_ethercom.ec_if.if_opackets++;	/* move to interrupt? */
947 		sc->sc_intctl |= MSK_TX_EMPTY | MSK_TX;
948 		sc->sc_ethercom.ec_if.if_timer = 5;
949 	}
950 	smc->b2.msk = sc->sc_intctl;
951 #ifdef ESDEBUG
952 	while ((smc->b2.bsr & BSR_MASK) != BSR_BANK2) {
953 		printf("%s: esstart++++ BSR not 2: %04x\n", sc->sc_dev.dv_xname,
954 		    smc->b2.bsr);
955 		smc->b2.bsr = BSR_BANK2;
956 	}
957 	if (--sc->sc_smcbusy) {
958 		printf("%s: esstart busy on exit\n", sc->sc_dev.dv_xname);
959 		panic("esstart busy on exit");
960 	}
961 #endif
962 }
963 
964 int
965 esioctl(register struct ifnet *ifp, u_long command, caddr_t data)
966 {
967 	struct es_softc *sc = ifp->if_softc;
968 	register struct ifaddr *ifa = (struct ifaddr *)data;
969 	struct ifreq *ifr = (struct ifreq *)data;
970 	int s, error = 0;
971 
972 	s = splnet();
973 
974 	switch (command) {
975 
976 	case SIOCSIFADDR:
977 		ifp->if_flags |= IFF_UP;
978 
979 		switch (ifa->ifa_addr->sa_family) {
980 #ifdef INET
981 		case AF_INET:
982 			esinit(sc);
983 			arp_ifinit(ifp, ifa);
984 			break;
985 #endif
986 #ifdef NS
987 		case AF_NS:
988 		    {
989 			register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
990 
991 			if (ns_nullhost(*ina))
992 				ina->x_host =
993 				    *(union ns_host *)LLADDR(ifp->if_sadl);
994 			else
995 				bcopy(ina->x_host.c_host,
996 				    LLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
997 			/* Set new address. */
998 			esinit(sc);
999 			break;
1000 		    }
1001 #endif
1002 		default:
1003 			esinit(sc);
1004 			break;
1005 		}
1006 		break;
1007 
1008 	case SIOCSIFFLAGS:
1009 		/*
1010 		 * If interface is marked down and it is running, then stop it
1011 		 */
1012 		if ((ifp->if_flags & IFF_UP) == 0 &&
1013 		    (ifp->if_flags & IFF_RUNNING) != 0) {
1014 			/*
1015 			 * If interface is marked down and it is running, then
1016 			 * stop it.
1017 			 */
1018 			esstop(sc);
1019 			ifp->if_flags &= ~IFF_RUNNING;
1020 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
1021 		    	   (ifp->if_flags & IFF_RUNNING) == 0) {
1022 			/*
1023 			 * If interface is marked up and it is stopped, then
1024 			 * start it.
1025 			 */
1026 			esinit(sc);
1027 		} else {
1028 			/*
1029 			 * Reset the interface to pick up changes in any other
1030 			 * flags that affect hardware registers.
1031 			 */
1032 			esstop(sc);
1033 			esinit(sc);
1034 		}
1035 #ifdef ESDEBUG
1036 		if (ifp->if_flags & IFF_DEBUG)
1037 			esdebug = sc->sc_debug = 1;
1038 		else
1039 			esdebug = sc->sc_debug = 0;
1040 #endif
1041 		break;
1042 
1043 	case SIOCADDMULTI:
1044 	case SIOCDELMULTI:
1045 		error = (command == SIOCADDMULTI) ?
1046 		    ether_addmulti(ifr, &sc->sc_ethercom) :
1047 		    ether_delmulti(ifr, &sc->sc_ethercom);
1048 
1049 		if (error == ENETRESET) {
1050 			/*
1051 			 * Multicast list has changed; set the hardware filter
1052 			 * accordingly.
1053 			 */
1054 			/* XXX */
1055 			error = 0;
1056 		}
1057 		break;
1058 
1059 	case SIOCGIFMEDIA:
1060 	case SIOCSIFMEDIA:
1061 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, command);
1062 		break;
1063 
1064 	default:
1065 		error = EINVAL;
1066 	}
1067 
1068 	splx(s);
1069 	return (error);
1070 }
1071 
1072 /*
1073  * Reset the interface.
1074  */
1075 void
1076 esreset(struct es_softc *sc)
1077 {
1078 	int s;
1079 
1080 	s = splnet();
1081 	esstop(sc);
1082 	esinit(sc);
1083 	splx(s);
1084 }
1085 
1086 void
1087 eswatchdog(struct ifnet *ifp)
1088 {
1089 	struct es_softc *sc = ifp->if_softc;
1090 
1091 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
1092 	++ifp->if_oerrors;
1093 
1094 	esreset(sc);
1095 }
1096 
1097 int
1098 esmediachange(struct ifnet *ifp)
1099 {
1100 	return 0;
1101 }
1102 
1103 void
1104 esmediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
1105 {
1106 }
1107