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