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