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