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