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