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