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