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