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