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