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