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