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