elink3.c revision 1.1 1 /* $NetBSD: elink3.c,v 1.1 1996/04/25 02:17:34 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1994 Herb Peyerl <hpeyerl (at) novatel.ca>
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 Herb Peyerl.
18 * 4. The name of Herb Peyerl 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 #include "bpfilter.h"
34
35 #include <sys/param.h>
36 #include <sys/mbuf.h>
37 #include <sys/socket.h>
38 #include <sys/ioctl.h>
39 #include <sys/errno.h>
40 #include <sys/syslog.h>
41 #include <sys/select.h>
42 #include <sys/device.h>
43
44 #include <net/if.h>
45 #include <net/netisr.h>
46 #include <net/if_dl.h>
47 #include <net/if_types.h>
48 #include <net/netisr.h>
49
50 #ifdef INET
51 #include <netinet/in.h>
52 #include <netinet/in_systm.h>
53 #include <netinet/in_var.h>
54 #include <netinet/ip.h>
55 #include <netinet/if_ether.h>
56 #endif
57
58 #ifdef NS
59 #include <netns/ns.h>
60 #include <netns/ns_if.h>
61 #endif
62
63 #if NBPFILTER > 0
64 #include <net/bpf.h>
65 #include <net/bpfdesc.h>
66 #endif
67
68 #include <machine/cpu.h>
69 #include <machine/pio.h>
70
71 #include <dev/ic/elink3var.h>
72 #include <dev/ic/elink3reg.h>
73
74 #define ETHER_MIN_LEN 64
75 #define ETHER_MAX_LEN 1518
76 #define ETHER_ADDR_LEN 6
77
78 struct cfdriver ep_cd = {
79 NULL, "ep", DV_IFNET
80 };
81
82 static void epxstat __P((struct ep_softc *));
83 static int epstatus __P((struct ep_softc *));
84 void epinit __P((struct ep_softc *));
85 int epioctl __P((struct ifnet *, u_long, caddr_t));
86 void epstart __P((struct ifnet *));
87 void epwatchdog __P((int));
88 void epreset __P((struct ep_softc *));
89 void epread __P((struct ep_softc *));
90 struct mbuf *epget __P((struct ep_softc *, int));
91 void epmbuffill __P((struct ep_softc *));
92 void epmbufempty __P((struct ep_softc *));
93 void epstop __P((struct ep_softc *));
94 void epsetfilter __P((struct ep_softc *));
95 void epsetlink __P((struct ep_softc *));
96 u_short epreadeeprom __P((int id_port, int offset));
97
98 static int epbusyeeprom __P((struct ep_softc *));
99
100 void
101 epconfig(sc, conn)
102 struct ep_softc *sc;
103 u_int conn;
104 {
105 u_short i;
106
107 struct ifnet *ifp = &sc->sc_arpcom.ac_if;
108
109 sc->ep_connectors = 0;
110 printf("%s: ", sc->sc_dev.dv_xname);
111 if (conn & IS_AUI) {
112 printf("aui");
113 sc->ep_connectors |= AUI;
114 }
115 if (conn & IS_BNC) {
116 if (sc->ep_connectors)
117 printf("/");
118 printf("bnc");
119 sc->ep_connectors |= BNC;
120 }
121 if (conn & IS_UTP) {
122 if (sc->ep_connectors)
123 printf("/");
124 printf("utp");
125 sc->ep_connectors |= UTP;
126 }
127 if (!sc->ep_connectors)
128 printf("no connectors!");
129
130 /*
131 * Read the station address from the eeprom
132 */
133 for (i = 0; i < 3; i++) {
134 u_short x;
135 if (epbusyeeprom(sc))
136 return;
137 outw(BASE + EP_W0_EEPROM_COMMAND, READ_EEPROM | i);
138 if (epbusyeeprom(sc))
139 return;
140 x = inw(BASE + EP_W0_EEPROM_DATA);
141 sc->sc_arpcom.ac_enaddr[(i << 1)] = x >> 8;
142 sc->sc_arpcom.ac_enaddr[(i << 1) + 1] = x;
143 }
144
145 printf(" address %s\n", ether_sprintf(sc->sc_arpcom.ac_enaddr));
146
147 ifp->if_unit = sc->sc_dev.dv_unit;
148 ifp->if_name = ep_cd.cd_name;
149 ifp->if_start = epstart;
150 ifp->if_ioctl = epioctl;
151 ifp->if_watchdog = epwatchdog;
152 ifp->if_flags =
153 IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
154
155 if_attach(ifp);
156 ether_ifattach(ifp);
157
158 #if NBPFILTER > 0
159 bpfattach(&sc->sc_arpcom.ac_if.if_bpf, ifp, DLT_EN10MB,
160 sizeof(struct ether_header));
161 #endif
162
163 sc->tx_start_thresh = 20; /* probably a good starting point. */
164 }
165
166 /*
167 * The order in here seems important. Otherwise we may not receive
168 * interrupts. ?!
169 */
170 void
171 epinit(sc)
172 register struct ep_softc *sc;
173 {
174 register struct ifnet *ifp = &sc->sc_arpcom.ac_if;
175 int i;
176
177 while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS)
178 ;
179
180 if (sc->bustype != EP_BUS_PCI) {
181 GO_WINDOW(0);
182 outw(BASE + EP_W0_CONFIG_CTRL, 0);
183 outw(BASE + EP_W0_CONFIG_CTRL, ENABLE_DRQ_IRQ);
184 }
185
186 if (sc->bustype == EP_BUS_PCMCIA) {
187 #ifdef EP_COAX_DEFAULT
188 outw(BASE + EP_W0_ADDRESS_CFG,3<<14);
189 #else
190 outw(BASE + EP_W0_ADDRESS_CFG,0<<14);
191 #endif
192 outw(BASE + EP_W0_RESOURCE_CFG, 0x3f00);
193 }
194
195 GO_WINDOW(2);
196 for (i = 0; i < 6; i++) /* Reload the ether_addr. */
197 outb(BASE + EP_W2_ADDR_0 + i, sc->sc_arpcom.ac_enaddr[i]);
198
199 outw(BASE + EP_COMMAND, RX_RESET);
200 outw(BASE + EP_COMMAND, TX_RESET);
201
202 GO_WINDOW(1); /* Window 1 is operating window */
203 for (i = 0; i < 31; i++)
204 inb(BASE + EP_W1_TX_STATUS);
205
206 outw(BASE + EP_COMMAND, SET_RD_0_MASK | S_CARD_FAILURE |
207 S_RX_COMPLETE | S_TX_COMPLETE | S_TX_AVAIL);
208 outw(BASE + EP_COMMAND, SET_INTR_MASK | S_CARD_FAILURE |
209 S_RX_COMPLETE | S_TX_COMPLETE | S_TX_AVAIL);
210
211 /*
212 * Attempt to get rid of any stray interrupts that occured during
213 * configuration. On the i386 this isn't possible because one may
214 * already be queued. However, a single stray interrupt is
215 * unimportant.
216 */
217 outw(BASE + EP_COMMAND, ACK_INTR | 0xff);
218
219 epsetfilter(sc);
220 epsetlink(sc);
221
222 outw(BASE + EP_COMMAND, RX_ENABLE);
223 outw(BASE + EP_COMMAND, TX_ENABLE);
224
225 epmbuffill(sc);
226
227 /* Interface is now `running', with no output active. */
228 ifp->if_flags |= IFF_RUNNING;
229 ifp->if_flags &= ~IFF_OACTIVE;
230
231 /* Attempt to start output, if any. */
232 epstart(ifp);
233 }
234
235 void
236 epsetfilter(sc)
237 register struct ep_softc *sc;
238 {
239 register struct ifnet *ifp = &sc->sc_arpcom.ac_if;
240
241 GO_WINDOW(1); /* Window 1 is operating window */
242 outw(BASE + EP_COMMAND, SET_RX_FILTER |
243 FIL_INDIVIDUAL | FIL_BRDCST |
244 ((ifp->if_flags & IFF_MULTICAST) ? FIL_MULTICAST : 0 ) |
245 ((ifp->if_flags & IFF_PROMISC) ? FIL_PROMISC : 0 ));
246 }
247
248 void
249 epsetlink(sc)
250 register struct ep_softc *sc;
251 {
252 register struct ifnet *ifp = &sc->sc_arpcom.ac_if;
253
254 /*
255 * you can `ifconfig (link0|-link0) ep0' to get the following
256 * behaviour:
257 * -link0 disable AUI/UTP. enable BNC.
258 * link0 disable BNC. enable AUI.
259 * link1 if the card has a UTP connector, and link0 is
260 * set too, then you get the UTP port.
261 */
262 GO_WINDOW(4);
263 outw(BASE + EP_W4_MEDIA_TYPE, DISABLE_UTP);
264 if (!(ifp->if_flags & IFF_LINK0) && (sc->ep_connectors & BNC)) {
265 if (sc->bustype == EP_BUS_PCMCIA) {
266 GO_WINDOW(0);
267 outw(BASE + EP_W0_ADDRESS_CFG,3<<14);
268 GO_WINDOW(1);
269 }
270 outw(BASE + EP_COMMAND, START_TRANSCEIVER);
271 delay(1000);
272 }
273 if (ifp->if_flags & IFF_LINK0) {
274 outw(BASE + EP_COMMAND, STOP_TRANSCEIVER);
275 delay(1000);
276 if ((ifp->if_flags & IFF_LINK1) && (sc->ep_connectors & UTP)) {
277 if (sc->bustype == EP_BUS_PCMCIA) {
278 GO_WINDOW(0);
279 outw(BASE + EP_W0_ADDRESS_CFG,0<<14);
280 GO_WINDOW(4);
281 }
282 outw(BASE + EP_W4_MEDIA_TYPE, ENABLE_UTP);
283 }
284 }
285 GO_WINDOW(1);
286 }
287
288 /*
289 * Start outputting on the interface.
290 * Always called as splnet().
291 */
292 void
293 epstart(ifp)
294 struct ifnet *ifp;
295 {
296 register struct ep_softc *sc = ep_cd.cd_devs[ifp->if_unit];
297 struct mbuf *m, *m0;
298 int sh, len, pad;
299
300 /* Don't transmit if interface is busy or not running */
301 if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
302 return;
303
304 startagain:
305 /* Sneak a peek at the next packet */
306 m0 = ifp->if_snd.ifq_head;
307 if (m0 == 0)
308 return;
309
310 /* We need to use m->m_pkthdr.len, so require the header */
311 if ((m0->m_flags & M_PKTHDR) == 0)
312 panic("epstart: no header mbuf");
313 len = m0->m_pkthdr.len;
314
315 pad = (4 - len) & 3;
316
317 /*
318 * The 3c509 automatically pads short packets to minimum ethernet
319 * length, but we drop packets that are too large. Perhaps we should
320 * truncate them instead?
321 */
322 if (len + pad > ETHER_MAX_LEN) {
323 /* packet is obviously too large: toss it */
324 ++ifp->if_oerrors;
325 IF_DEQUEUE(&ifp->if_snd, m0);
326 m_freem(m0);
327 goto readcheck;
328 }
329
330 if (inw(BASE + EP_W1_FREE_TX) < len + pad + 4) {
331 outw(BASE + EP_COMMAND, SET_TX_AVAIL_THRESH | (len + pad + 4));
332 /* not enough room in FIFO */
333 ifp->if_flags |= IFF_OACTIVE;
334 return;
335 } else {
336 outw(BASE + EP_COMMAND, SET_TX_AVAIL_THRESH | 2044);
337 }
338
339 IF_DEQUEUE(&ifp->if_snd, m0);
340 if (m0 == 0) /* not really needed */
341 return;
342
343 outw(BASE + EP_COMMAND, SET_TX_START_THRESH |
344 (len / 4 + sc->tx_start_thresh));
345
346 #if NBPFILTER > 0
347 if (ifp->if_bpf)
348 bpf_mtap(ifp->if_bpf, m0);
349 #endif
350
351 /*
352 * Do the output at splhigh() so that an interrupt from another device
353 * won't cause a FIFO underrun.
354 */
355 sh = splhigh();
356
357 outw(BASE + EP_W1_TX_PIO_WR_1, len);
358 outw(BASE + EP_W1_TX_PIO_WR_1, 0xffff); /* Second dword meaningless */
359 if (EP_IS_BUS_32(sc->bustype)) {
360 for (m = m0; m; ) {
361 if (m->m_len > 3)
362 outsl(BASE + EP_W1_TX_PIO_WR_1,
363 mtod(m, caddr_t), m->m_len / 4);
364 if (m->m_len & 3)
365 outsb(BASE + EP_W1_TX_PIO_WR_1,
366 mtod(m, caddr_t) + (m->m_len & ~3),
367 m->m_len & 3);
368 MFREE(m, m0);
369 m = m0;
370 }
371 } else {
372 for (m = m0; m; ) {
373 if (m->m_len > 1)
374 outsw(BASE + EP_W1_TX_PIO_WR_1,
375 mtod(m, caddr_t), m->m_len / 2);
376 if (m->m_len & 1)
377 outb(BASE + EP_W1_TX_PIO_WR_1,
378 *(mtod(m, caddr_t) + m->m_len - 1));
379 MFREE(m, m0);
380 m = m0;
381 }
382 }
383 while (pad--)
384 outb(BASE + EP_W1_TX_PIO_WR_1, 0);
385
386 splx(sh);
387
388 ++ifp->if_opackets;
389
390 readcheck:
391 if ((inw(BASE + EP_W1_RX_STATUS) & ERR_INCOMPLETE) == 0) {
392 /* We received a complete packet. */
393 u_short status = inw(BASE + EP_STATUS);
394
395 if ((status & S_INTR_LATCH) == 0) {
396 /*
397 * No interrupt, read the packet and continue
398 * Is this supposed to happen? Is my motherboard
399 * completely busted?
400 */
401 epread(sc);
402 }
403 else
404 /* Got an interrupt, return so that it gets serviced. */
405 return;
406 }
407 else {
408 /* Check if we are stuck and reset [see XXX comment] */
409 if (epstatus(sc)) {
410 if (ifp->if_flags & IFF_DEBUG)
411 printf("%s: adapter reset\n",
412 sc->sc_dev.dv_xname);
413 epreset(sc);
414 }
415 }
416
417 goto startagain;
418 }
419
420
421 /*
422 * XXX: The 3c509 card can get in a mode where both the fifo status bit
423 * FIFOS_RX_OVERRUN and the status bit ERR_INCOMPLETE are set
424 * We detect this situation and we reset the adapter.
425 * It happens at times when there is a lot of broadcast traffic
426 * on the cable (once in a blue moon).
427 */
428 static int
429 epstatus(sc)
430 register struct ep_softc *sc;
431 {
432 u_short fifost;
433
434 /*
435 * Check the FIFO status and act accordingly
436 */
437 GO_WINDOW(4);
438 fifost = inw(BASE + EP_W4_FIFO_DIAG);
439 GO_WINDOW(1);
440
441 if (fifost & FIFOS_RX_UNDERRUN) {
442 if (sc->sc_arpcom.ac_if.if_flags & IFF_DEBUG)
443 printf("%s: RX underrun\n", sc->sc_dev.dv_xname);
444 epreset(sc);
445 return 0;
446 }
447
448 if (fifost & FIFOS_RX_STATUS_OVERRUN) {
449 if (sc->sc_arpcom.ac_if.if_flags & IFF_DEBUG)
450 printf("%s: RX Status overrun\n", sc->sc_dev.dv_xname);
451 return 1;
452 }
453
454 if (fifost & FIFOS_RX_OVERRUN) {
455 if (sc->sc_arpcom.ac_if.if_flags & IFF_DEBUG)
456 printf("%s: RX overrun\n", sc->sc_dev.dv_xname);
457 return 1;
458 }
459
460 if (fifost & FIFOS_TX_OVERRUN) {
461 if (sc->sc_arpcom.ac_if.if_flags & IFF_DEBUG)
462 printf("%s: TX overrun\n", sc->sc_dev.dv_xname);
463 epreset(sc);
464 return 0;
465 }
466
467 return 0;
468 }
469
470
471 static void
472 eptxstat(sc)
473 register struct ep_softc *sc;
474 {
475 int i;
476
477 /*
478 * We need to read+write TX_STATUS until we get a 0 status
479 * in order to turn off the interrupt flag.
480 */
481 while ((i = inb(BASE + EP_W1_TX_STATUS)) & TXS_COMPLETE) {
482 outb(BASE + EP_W1_TX_STATUS, 0x0);
483
484 if (i & TXS_JABBER) {
485 ++sc->sc_arpcom.ac_if.if_oerrors;
486 if (sc->sc_arpcom.ac_if.if_flags & IFF_DEBUG)
487 printf("%s: jabber (%x)\n",
488 sc->sc_dev.dv_xname, i);
489 epreset(sc);
490 } else if (i & TXS_UNDERRUN) {
491 ++sc->sc_arpcom.ac_if.if_oerrors;
492 if (sc->sc_arpcom.ac_if.if_flags & IFF_DEBUG)
493 printf("%s: fifo underrun (%x) @%d\n",
494 sc->sc_dev.dv_xname, i,
495 sc->tx_start_thresh);
496 if (sc->tx_succ_ok < 100)
497 sc->tx_start_thresh = min(ETHER_MAX_LEN,
498 sc->tx_start_thresh + 20);
499 sc->tx_succ_ok = 0;
500 epreset(sc);
501 } else if (i & TXS_MAX_COLLISION) {
502 ++sc->sc_arpcom.ac_if.if_collisions;
503 outw(BASE + EP_COMMAND, TX_ENABLE);
504 sc->sc_arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
505 } else
506 sc->tx_succ_ok = (sc->tx_succ_ok+1) & 127;
507
508 }
509 }
510
511 int
512 epintr(arg)
513 void *arg;
514 {
515 register struct ep_softc *sc = arg;
516 struct ifnet *ifp = &sc->sc_arpcom.ac_if;
517 u_short status;
518 int ret = 0;
519
520 for (;;) {
521 outw(BASE + EP_COMMAND, C_INTR_LATCH);
522
523 status = inw(BASE + EP_STATUS);
524
525 if ((status & (S_TX_COMPLETE | S_TX_AVAIL |
526 S_RX_COMPLETE | S_CARD_FAILURE)) == 0)
527 break;
528
529 ret = 1;
530
531 /*
532 * Acknowledge any interrupts. It's important that we do this
533 * first, since there would otherwise be a race condition.
534 * Due to the i386 interrupt queueing, we may get spurious
535 * interrupts occasionally.
536 */
537 outw(BASE + EP_COMMAND, ACK_INTR | status);
538
539 if (status & S_RX_COMPLETE)
540 epread(sc);
541 if (status & S_TX_AVAIL) {
542 sc->sc_arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
543 epstart(&sc->sc_arpcom.ac_if);
544 }
545 if (status & S_CARD_FAILURE) {
546 printf("%s: adapter failure (%x)\n",
547 sc->sc_dev.dv_xname, status);
548 epreset(sc);
549 return (1);
550 }
551 if (status & S_TX_COMPLETE) {
552 eptxstat(sc);
553 epstart(ifp);
554 }
555 }
556
557 /* no more interrupts */
558 return (ret);
559 }
560
561 void
562 epread(sc)
563 register struct ep_softc *sc;
564 {
565 struct ifnet *ifp = &sc->sc_arpcom.ac_if;
566 struct mbuf *m;
567 struct ether_header *eh;
568 int len;
569
570 len = inw(BASE + EP_W1_RX_STATUS);
571
572 again:
573 if (ifp->if_flags & IFF_DEBUG) {
574 int err = len & ERR_MASK;
575 char *s = NULL;
576
577 if (len & ERR_INCOMPLETE)
578 s = "incomplete packet";
579 else if (err == ERR_OVERRUN)
580 s = "packet overrun";
581 else if (err == ERR_RUNT)
582 s = "runt packet";
583 else if (err == ERR_ALIGNMENT)
584 s = "bad alignment";
585 else if (err == ERR_CRC)
586 s = "bad crc";
587 else if (err == ERR_OVERSIZE)
588 s = "oversized packet";
589 else if (err == ERR_DRIBBLE)
590 s = "dribble bits";
591
592 if (s)
593 printf("%s: %s\n", sc->sc_dev.dv_xname, s);
594 }
595
596 if (len & ERR_INCOMPLETE)
597 return;
598
599 if (len & ERR_RX) {
600 ++ifp->if_ierrors;
601 goto abort;
602 }
603
604 len &= RX_BYTES_MASK; /* Lower 11 bits = RX bytes. */
605
606 /* Pull packet off interface. */
607 m = epget(sc, len);
608 if (m == 0) {
609 ifp->if_ierrors++;
610 goto abort;
611 }
612
613 ++ifp->if_ipackets;
614
615 /* We assume the header fit entirely in one mbuf. */
616 eh = mtod(m, struct ether_header *);
617
618 #if NBPFILTER > 0
619 /*
620 * Check if there's a BPF listener on this interface.
621 * If so, hand off the raw packet to BPF.
622 */
623 if (ifp->if_bpf) {
624 bpf_mtap(ifp->if_bpf, m);
625
626 /*
627 * Note that the interface cannot be in promiscuous mode if
628 * there are no BPF listeners. And if we are in promiscuous
629 * mode, we have to check if this packet is really ours.
630 */
631 if ((ifp->if_flags & IFF_PROMISC) &&
632 (eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */
633 bcmp(eh->ether_dhost, sc->sc_arpcom.ac_enaddr,
634 sizeof(eh->ether_dhost)) != 0) {
635 m_freem(m);
636 return;
637 }
638 }
639 #endif
640
641 /* We assume the header fit entirely in one mbuf. */
642 m_adj(m, sizeof(struct ether_header));
643 ether_input(ifp, eh, m);
644
645 /*
646 * In periods of high traffic we can actually receive enough
647 * packets so that the fifo overrun bit will be set at this point,
648 * even though we just read a packet. In this case we
649 * are not going to receive any more interrupts. We check for
650 * this condition and read again until the fifo is not full.
651 * We could simplify this test by not using epstatus(), but
652 * rechecking the RX_STATUS register directly. This test could
653 * result in unnecessary looping in cases where there is a new
654 * packet but the fifo is not full, but it will not fix the
655 * stuck behavior.
656 *
657 * Even with this improvement, we still get packet overrun errors
658 * which are hurting performance. Maybe when I get some more time
659 * I'll modify epread() so that it can handle RX_EARLY interrupts.
660 */
661 if (epstatus(sc)) {
662 len = inw(BASE + EP_W1_RX_STATUS);
663 /* Check if we are stuck and reset [see XXX comment] */
664 if (len & ERR_INCOMPLETE) {
665 if (ifp->if_flags & IFF_DEBUG)
666 printf("%s: adapter reset\n",
667 sc->sc_dev.dv_xname);
668 epreset(sc);
669 return;
670 }
671 goto again;
672 }
673
674 return;
675
676 abort:
677 outw(BASE + EP_COMMAND, RX_DISCARD_TOP_PACK);
678 while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS)
679 ;
680 }
681
682 struct mbuf *
683 epget(sc, totlen)
684 struct ep_softc *sc;
685 int totlen;
686 {
687 struct ifnet *ifp = &sc->sc_arpcom.ac_if;
688 struct mbuf *top, **mp, *m;
689 int len;
690 int sh;
691
692 m = sc->mb[sc->next_mb];
693 sc->mb[sc->next_mb] = 0;
694 if (m == 0) {
695 MGETHDR(m, M_DONTWAIT, MT_DATA);
696 if (m == 0)
697 return 0;
698 } else {
699 /* If the queue is no longer full, refill. */
700 if (sc->last_mb == sc->next_mb)
701 timeout(epmbuffill, sc, 1);
702 /* Convert one of our saved mbuf's. */
703 sc->next_mb = (sc->next_mb + 1) % MAX_MBS;
704 m->m_data = m->m_pktdat;
705 m->m_flags = M_PKTHDR;
706 }
707 m->m_pkthdr.rcvif = ifp;
708 m->m_pkthdr.len = totlen;
709 len = MHLEN;
710 top = 0;
711 mp = ⊤
712
713 /*
714 * We read the packet at splhigh() so that an interrupt from another
715 * device doesn't cause the card's buffer to overflow while we're
716 * reading it. We may still lose packets at other times.
717 */
718 sh = splhigh();
719
720 while (totlen > 0) {
721 if (top) {
722 m = sc->mb[sc->next_mb];
723 sc->mb[sc->next_mb] = 0;
724 if (m == 0) {
725 MGET(m, M_DONTWAIT, MT_DATA);
726 if (m == 0) {
727 splx(sh);
728 m_freem(top);
729 return 0;
730 }
731 } else {
732 sc->next_mb = (sc->next_mb + 1) % MAX_MBS;
733 }
734 len = MLEN;
735 }
736 if (totlen >= MINCLSIZE) {
737 MCLGET(m, M_DONTWAIT);
738 if (m->m_flags & M_EXT)
739 len = MCLBYTES;
740 }
741 len = min(totlen, len);
742 if (EP_IS_BUS_32(sc->bustype)) {
743 if (len > 3) {
744 len &= ~3;
745 insl(BASE + EP_W1_RX_PIO_RD_1,
746 mtod(m, caddr_t), len / 4);
747 } else
748 insb(BASE + EP_W1_RX_PIO_RD_1,
749 mtod(m, caddr_t), len);
750 } else {
751 if (len > 1) {
752 len &= ~1;
753 insw(BASE + EP_W1_RX_PIO_RD_1,
754 mtod(m, caddr_t), len / 2);
755 } else
756 *(mtod(m, caddr_t)) =
757 inb(BASE + EP_W1_RX_PIO_RD_1);
758 }
759 m->m_len = len;
760 totlen -= len;
761 *mp = m;
762 mp = &m->m_next;
763 }
764
765 outw(BASE + EP_COMMAND, RX_DISCARD_TOP_PACK);
766 while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS)
767 ;
768
769 splx(sh);
770
771 return top;
772 }
773
774 int
775 epioctl(ifp, cmd, data)
776 register struct ifnet *ifp;
777 u_long cmd;
778 caddr_t data;
779 {
780 struct ep_softc *sc = ep_cd.cd_devs[ifp->if_unit];
781 struct ifaddr *ifa = (struct ifaddr *)data;
782 struct ifreq *ifr = (struct ifreq *)data;
783 int s, error = 0;
784
785 s = splnet();
786
787 switch (cmd) {
788
789 case SIOCSIFADDR:
790 ifp->if_flags |= IFF_UP;
791
792 switch (ifa->ifa_addr->sa_family) {
793 #ifdef INET
794 case AF_INET:
795 epinit(sc);
796 arp_ifinit(&sc->sc_arpcom, ifa);
797 break;
798 #endif
799 #ifdef NS
800 case AF_NS:
801 {
802 register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
803
804 if (ns_nullhost(*ina))
805 ina->x_host =
806 *(union ns_host *)(sc->sc_arpcom.ac_enaddr);
807 else
808 bcopy(ina->x_host.c_host,
809 sc->sc_arpcom.ac_enaddr,
810 sizeof(sc->sc_arpcom.ac_enaddr));
811 /* Set new address. */
812 epinit(sc);
813 break;
814 }
815 #endif
816 default:
817 epinit(sc);
818 break;
819 }
820 break;
821
822 case SIOCSIFFLAGS:
823 if ((ifp->if_flags & IFF_UP) == 0 &&
824 (ifp->if_flags & IFF_RUNNING) != 0) {
825 /*
826 * If interface is marked down and it is running, then
827 * stop it.
828 */
829 epstop(sc);
830 ifp->if_flags &= ~IFF_RUNNING;
831 } else if ((ifp->if_flags & IFF_UP) != 0 &&
832 (ifp->if_flags & IFF_RUNNING) == 0) {
833 /*
834 * If interface is marked up and it is stopped, then
835 * start it.
836 */
837 epinit(sc);
838 } else {
839 /*
840 * deal with flags changes:
841 * IFF_MULTICAST, IFF_PROMISC,
842 * IFF_LINK0, IFF_LINK1,
843 */
844 epsetfilter(sc);
845 epsetlink(sc);
846 }
847 break;
848
849 case SIOCADDMULTI:
850 case SIOCDELMULTI:
851 error = (cmd == SIOCADDMULTI) ?
852 ether_addmulti(ifr, &sc->sc_arpcom) :
853 ether_delmulti(ifr, &sc->sc_arpcom);
854
855 if (error == ENETRESET) {
856 /*
857 * Multicast list has changed; set the hardware filter
858 * accordingly.
859 */
860 epreset(sc);
861 error = 0;
862 }
863 break;
864
865 default:
866 error = EINVAL;
867 break;
868 }
869
870 splx(s);
871 return (error);
872 }
873
874 void
875 epreset(sc)
876 struct ep_softc *sc;
877 {
878 int s;
879
880 s = splnet();
881 epstop(sc);
882 epinit(sc);
883 splx(s);
884 }
885
886 void
887 epwatchdog(unit)
888 int unit;
889 {
890 struct ep_softc *sc = ep_cd.cd_devs[unit];
891
892 log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
893 ++sc->sc_arpcom.ac_if.if_oerrors;
894
895 epreset(sc);
896 }
897
898 void
899 epstop(sc)
900 register struct ep_softc *sc;
901 {
902
903 outw(BASE + EP_COMMAND, RX_DISABLE);
904 outw(BASE + EP_COMMAND, RX_DISCARD_TOP_PACK);
905 while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS)
906 ;
907 outw(BASE + EP_COMMAND, TX_DISABLE);
908 outw(BASE + EP_COMMAND, STOP_TRANSCEIVER);
909 outw(BASE + EP_COMMAND, RX_RESET);
910 outw(BASE + EP_COMMAND, TX_RESET);
911 outw(BASE + EP_COMMAND, C_INTR_LATCH);
912 outw(BASE + EP_COMMAND, SET_RD_0_MASK);
913 outw(BASE + EP_COMMAND, SET_INTR_MASK);
914 outw(BASE + EP_COMMAND, SET_RX_FILTER);
915
916 epmbufempty(sc);
917 }
918
919 /*
920 * We get eeprom data from the id_port given an offset into the
921 * eeprom. Basically; after the ID_sequence is sent to all of
922 * the cards; they enter the ID_CMD state where they will accept
923 * command requests. 0x80-0xbf loads the eeprom data. We then
924 * read the port 16 times and with every read; the cards check
925 * for contention (ie: if one card writes a 0 bit and another
926 * writes a 1 bit then the host sees a 0. At the end of the cycle;
927 * each card compares the data on the bus; if there is a difference
928 * then that card goes into ID_WAIT state again). In the meantime;
929 * one bit of data is returned in the AX register which is conveniently
930 * returned to us by inb(). Hence; we read 16 times getting one
931 * bit of data with each read.
932 */
933 u_short
934 epreadeeprom(id_port, offset)
935 int id_port;
936 int offset;
937 {
938 int i, data = 0;
939
940 outb(id_port, 0x80 + offset);
941 delay(1000);
942 for (i = 0; i < 16; i++)
943 data = (data << 1) | (inw(id_port) & 1);
944 return (data);
945 }
946
947 static int
948 epbusyeeprom(sc)
949 struct ep_softc *sc;
950 {
951 int i = 100, j;
952
953 if (sc->bustype == EP_BUS_PCMCIA) {
954 delay(1000);
955 return 0;
956 }
957
958 while (i--) {
959 j = inw(BASE + EP_W0_EEPROM_COMMAND);
960 if (j & EEPROM_BUSY)
961 delay(100);
962 else
963 break;
964 }
965 if (!i) {
966 printf("\n%s: eeprom failed to come ready\n",
967 sc->sc_dev.dv_xname);
968 return (1);
969 }
970 if (j & EEPROM_TST_MODE) {
971 printf("\n%s: erase pencil mark, or disable plug-n-play mode!\n",
972 sc->sc_dev.dv_xname);
973 return (1);
974 }
975 return (0);
976 }
977
978 void
979 epmbuffill(sc)
980 struct ep_softc *sc;
981 {
982 int s, i;
983
984 s = splnet();
985 i = sc->last_mb;
986 do {
987 if (sc->mb[i] == NULL)
988 MGET(sc->mb[i], M_DONTWAIT, MT_DATA);
989 if (sc->mb[i] == NULL)
990 break;
991 i = (i + 1) % MAX_MBS;
992 } while (i != sc->next_mb);
993 sc->last_mb = i;
994 /* If the queue was not filled, try again. */
995 if (sc->last_mb != sc->next_mb)
996 timeout(epmbuffill, sc, 1);
997 splx(s);
998 }
999
1000 void
1001 epmbufempty(sc)
1002 struct ep_softc *sc;
1003 {
1004 int s, i;
1005
1006 s = splnet();
1007 for (i = 0; i<MAX_MBS; i++) {
1008 if (sc->mb[i]) {
1009 m_freem(sc->mb[i]);
1010 sc->mb[i] = NULL;
1011 }
1012 }
1013 sc->last_mb = sc->next_mb = 0;
1014 untimeout(epmbuffill, sc);
1015 splx(s);
1016 }
1017