i386 removal, part 55/x: Remove if_sbni(4) remains.
[dragonfly.git] / sys / dev / netif / vx / if_vx.c
blob4a15c85a3290d84383d1fab1646cce989cbeccf2
1 /*
2 * Copyright (c) 1994 Herb Peyerl <hpeyerl@novatel.ca>
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Herb Peyerl.
16 * 4. The name of Herb Peyerl may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * $FreeBSD: src/sys/dev/vx/if_vx.c,v 1.25.2.6 2002/02/13 00:43:10 dillon Exp $
34 * Created from if_ep.c driver by Fred Gray (fgray@rice.edu) to support
35 * the 3c590 family.
39 * Modified from the FreeBSD 1.1.5.1 version by:
40 * Andres Vega Garcia
41 * INRIA - Sophia Antipolis, France
42 * avega@sophia.inria.fr
46 * Promiscuous mode added and interrupt logic slightly changed
47 * to reduce the number of adapter failures. Transceiver select
48 * logic changed to use value from EEPROM. Autoconfiguration
49 * features added.
50 * Done by:
51 * Serge Babkin
52 * Chelindbank (Chelyabinsk, Russia)
53 * babkin@hq.icb.chel.su
56 #include <sys/param.h>
57 #include <sys/bus.h>
58 #include <sys/systm.h>
59 #include <sys/sockio.h>
60 #include <sys/malloc.h>
61 #include <sys/mbuf.h>
62 #include <sys/socket.h>
63 #include <sys/linker_set.h>
64 #include <sys/module.h>
65 #include <sys/serialize.h>
66 #include <sys/thread2.h>
68 #include <net/if.h>
69 #include <net/ifq_var.h>
70 #include <net/ethernet.h>
71 #include <net/if_arp.h>
73 #include <net/bpf.h>
75 #include "if_vxreg.h"
77 DECLARE_DUMMY_MODULE(if_vx);
79 static struct connector_entry {
80 int bit;
81 const char *name;
82 } conn_tab[VX_CONNECTORS] = {
83 #define CONNECTOR_UTP 0
84 { 0x08, "utp"},
85 #define CONNECTOR_AUI 1
86 { 0x20, "aui"},
87 /* dummy */
88 { 0, "???"},
89 #define CONNECTOR_BNC 3
90 { 0x10, "bnc"},
91 #define CONNECTOR_TX 4
92 { 0x02, "tx"},
93 #define CONNECTOR_FX 5
94 { 0x04, "fx"},
95 #define CONNECTOR_MII 6
96 { 0x40, "mii"},
97 { 0, "???"}
100 /* int vxattach (struct vx_softc *); */
101 static void vxtxstat (struct vx_softc *);
102 static int vxstatus (struct vx_softc *);
103 static void vxinit (void *);
104 static int vxioctl (struct ifnet *, u_long, caddr_t, struct ucred *);
105 static void vxstart (struct ifnet *ifp, struct ifaltq_subque *);
106 static void vxwatchdog (struct ifnet *);
107 static void vxreset (struct vx_softc *);
108 /* void vxstop (struct vx_softc *); */
109 static void vxread (struct vx_softc *);
110 static struct mbuf *vxget (struct vx_softc *, u_int);
111 static void vxmbuffill (void *);
112 static void vxmbuffill_serialized (void *);
113 static void vxmbufempty (struct vx_softc *);
114 static void vxsetfilter (struct vx_softc *);
115 static void vxgetlink (struct vx_softc *);
116 static void vxsetlink (struct vx_softc *);
117 /* int vxbusyeeprom (struct vx_softc *); */
120 vxattach(device_t dev)
122 struct vx_softc *sc;
123 struct ifnet *ifp;
124 uint8_t eaddr[ETHER_ADDR_LEN];
125 int i;
127 sc = device_get_softc(dev);
129 callout_init(&sc->vx_timer);
130 GO_WINDOW(0);
131 CSR_WRITE_2(sc, VX_COMMAND, GLOBAL_RESET);
132 VX_BUSY_WAIT;
134 ifp = &sc->arpcom.ac_if;
135 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
137 vxgetlink(sc);
140 * Read the station address from the eeprom
142 GO_WINDOW(0);
143 for (i = 0; i < 3; i++) {
144 int x;
145 if (vxbusyeeprom(sc))
146 return 0;
147 CSR_WRITE_2(sc, VX_W0_EEPROM_COMMAND, EEPROM_CMD_RD
148 | (EEPROM_OEM_ADDR_0 + i));
149 if (vxbusyeeprom(sc))
150 return 0;
151 x = CSR_READ_2(sc, VX_W0_EEPROM_DATA);
152 eaddr[(i << 1)] = x >> 8;
153 eaddr[(i << 1) + 1] = x;
156 ifp->if_mtu = ETHERMTU;
157 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
158 ifq_set_ready(&ifp->if_snd);
159 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
160 ifp->if_start = vxstart;
161 ifp->if_ioctl = vxioctl;
162 ifp->if_init = vxinit;
163 ifp->if_watchdog = vxwatchdog;
164 ifp->if_softc = sc;
166 ether_ifattach(ifp, eaddr, NULL);
168 sc->tx_start_thresh = 20; /* probably a good starting point. */
170 vxstop(sc);
172 return 1;
178 * The order in here seems important. Otherwise we may not receive
179 * interrupts. ?!
181 static void
182 vxinit(void *xsc)
184 struct vx_softc *sc = (struct vx_softc *) xsc;
185 struct ifnet *ifp = &sc->arpcom.ac_if;
186 int i;
188 VX_BUSY_WAIT;
190 GO_WINDOW(2);
192 for (i = 0; i < 6; i++) /* Reload the ether_addr. */
193 CSR_WRITE_1(sc, VX_W2_ADDR_0 + i, sc->arpcom.ac_enaddr[i]);
195 CSR_WRITE_2(sc, VX_COMMAND, RX_RESET);
196 VX_BUSY_WAIT;
197 CSR_WRITE_2(sc, VX_COMMAND, TX_RESET);
198 VX_BUSY_WAIT;
200 GO_WINDOW(1); /* Window 1 is operating window */
201 for (i = 0; i < 31; i++)
202 CSR_READ_1(sc, VX_W1_TX_STATUS);
204 CSR_WRITE_2(sc, VX_COMMAND,SET_RD_0_MASK | S_CARD_FAILURE |
205 S_RX_COMPLETE | S_TX_COMPLETE | S_TX_AVAIL);
206 CSR_WRITE_2(sc, VX_COMMAND,SET_INTR_MASK | S_CARD_FAILURE |
207 S_RX_COMPLETE | S_TX_COMPLETE | S_TX_AVAIL);
210 * Attempt to get rid of any stray interrupts that occured during
211 * configuration. On the i386 this isn't possible because one may
212 * already be queued. However, a single stray interrupt is
213 * unimportant.
215 CSR_WRITE_2(sc, VX_COMMAND, ACK_INTR | 0xff);
217 vxsetfilter(sc);
218 vxsetlink(sc);
220 CSR_WRITE_2(sc, VX_COMMAND, RX_ENABLE);
221 CSR_WRITE_2(sc, VX_COMMAND, TX_ENABLE);
223 vxmbuffill_serialized((caddr_t) sc);
225 /* Interface is now `running', with no output active. */
226 ifp->if_flags |= IFF_RUNNING;
227 ifq_clr_oactive(&ifp->if_snd);
229 /* Attempt to start output, if any. */
230 if_devstart(ifp);
233 static void
234 vxsetfilter(struct vx_softc *sc)
236 struct ifnet *ifp = &sc->arpcom.ac_if;
238 GO_WINDOW(1); /* Window 1 is operating window */
239 CSR_WRITE_2(sc, VX_COMMAND, SET_RX_FILTER | FIL_INDIVIDUAL | FIL_BRDCST |
240 FIL_MULTICAST |
241 ((ifp->if_flags & IFF_PROMISC) ? FIL_PROMISC : 0 ));
244 static void
245 vxgetlink(struct vx_softc *sc)
247 int n, k;
249 GO_WINDOW(3);
250 sc->vx_connectors = CSR_READ_2(sc, VX_W3_RESET_OPT) & 0x7f;
251 for (n = 0, k = 0; k < VX_CONNECTORS; k++) {
252 if (sc->vx_connectors & conn_tab[k].bit) {
253 if (n == 0)
254 if_printf(&sc->arpcom.ac_if, "%s", conn_tab[k].name);
255 else
256 kprintf("/%s", conn_tab[k].name);
257 n++;
260 if (n == 0) {
261 if_printf(&sc->arpcom.ac_if, "no connectors!\n");
262 return;
264 GO_WINDOW(3);
265 sc->vx_connector = (CSR_READ_4(sc, VX_W3_INTERNAL_CFG)
266 & INTERNAL_CONNECTOR_MASK)
267 >> INTERNAL_CONNECTOR_BITS;
268 if (sc->vx_connector & 0x10) {
269 sc->vx_connector &= 0x0f;
270 kprintf("[*%s*]", conn_tab[(int)sc->vx_connector].name);
271 kprintf(": disable 'auto select' with DOS util!\n");
272 } else {
273 kprintf("[*%s*]\n", conn_tab[(int)sc->vx_connector].name);
277 static void
278 vxsetlink(struct vx_softc *sc)
280 struct ifnet *ifp = &sc->arpcom.ac_if;
281 int i, j, k;
282 const char *reason, *warning;
283 static short prev_flags;
284 static char prev_conn = -1;
286 if (prev_conn == -1) {
287 prev_conn = sc->vx_connector;
291 * S.B.
293 * Now behavior was slightly changed:
295 * if any of flags link[0-2] is used and its connector is
296 * physically present the following connectors are used:
298 * link0 - AUI * highest precedence
299 * link1 - BNC
300 * link2 - UTP * lowest precedence
302 * If none of them is specified then
303 * connector specified in the EEPROM is used
304 * (if present on card or UTP if not).
307 i = sc->vx_connector; /* default in EEPROM */
308 reason = "default";
309 warning = NULL;
311 if (ifp->if_flags & IFF_LINK0) {
312 if (sc->vx_connectors & conn_tab[CONNECTOR_AUI].bit) {
313 i = CONNECTOR_AUI;
314 reason = "link0";
315 } else {
316 warning = "aui not present! (link0)";
318 } else if (ifp->if_flags & IFF_LINK1) {
319 if (sc->vx_connectors & conn_tab[CONNECTOR_BNC].bit) {
320 i = CONNECTOR_BNC;
321 reason = "link1";
322 } else {
323 warning = "bnc not present! (link1)";
325 } else if (ifp->if_flags & IFF_LINK2) {
326 if (sc->vx_connectors & conn_tab[CONNECTOR_UTP].bit) {
327 i = CONNECTOR_UTP;
328 reason = "link2";
329 } else {
330 warning = "utp not present! (link2)";
332 } else if ((sc->vx_connectors & conn_tab[(int)sc->vx_connector].bit) == 0) {
333 warning = "strange connector type in EEPROM.";
334 reason = "forced";
335 i = CONNECTOR_UTP;
338 /* Avoid unnecessary message. */
339 k = (prev_flags ^ ifp->if_flags) & (IFF_LINK0 | IFF_LINK1 | IFF_LINK2);
340 if ((k != 0) || (prev_conn != i)) {
341 if (warning != NULL) {
342 if_printf(ifp, "warning: %s\n", warning);
344 if_printf(ifp, "selected %s. (%s)\n", conn_tab[i].name, reason);
347 /* Set the selected connector. */
348 GO_WINDOW(3);
349 j = CSR_READ_4(sc, VX_W3_INTERNAL_CFG) & ~INTERNAL_CONNECTOR_MASK;
350 CSR_WRITE_4(sc, VX_W3_INTERNAL_CFG, j | (i <<INTERNAL_CONNECTOR_BITS));
352 /* First, disable all. */
353 CSR_WRITE_2(sc,VX_COMMAND, STOP_TRANSCEIVER);
354 DELAY(800);
355 GO_WINDOW(4);
356 CSR_WRITE_2(sc, VX_W4_MEDIA_TYPE, 0);
358 /* Second, enable the selected one. */
359 switch(i) {
360 case CONNECTOR_UTP:
361 GO_WINDOW(4);
362 CSR_WRITE_2(sc, VX_W4_MEDIA_TYPE, ENABLE_UTP);
363 break;
364 case CONNECTOR_BNC:
365 CSR_WRITE_2(sc, VX_COMMAND, START_TRANSCEIVER);
366 DELAY(800);
367 break;
368 case CONNECTOR_TX:
369 case CONNECTOR_FX:
370 GO_WINDOW(4);
371 CSR_WRITE_2(sc, VX_W4_MEDIA_TYPE, LINKBEAT_ENABLE);
372 break;
373 default: /* AUI and MII fall here */
374 break;
376 GO_WINDOW(1);
378 prev_flags = ifp->if_flags;
379 prev_conn = i;
382 static void
383 vxstart(struct ifnet *ifp, struct ifaltq_subque *ifsq)
385 struct vx_softc *sc = ifp->if_softc;
386 struct mbuf *m0;
387 int len, pad;
389 ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
391 /* Don't transmit if interface is busy or not running */
392 if ((ifp->if_flags & IFF_RUNNING) == 0 || ifq_is_oactive(&ifp->if_snd))
393 return;
395 startagain:
396 /* Sneak a peek at the next packet */
397 m0 = ifq_dequeue(&ifp->if_snd);
398 if (m0 == NULL)
399 return;
401 /* We need to use m->m_pkthdr.len, so require the header */
402 M_ASSERTPKTHDR(m0);
403 len = m0->m_pkthdr.len;
405 pad = (4 - len) & 3;
408 * The 3c509 automatically pads short packets to minimum ethernet length,
409 * but we drop packets that are too large. Perhaps we should truncate
410 * them instead?
412 if (len + pad > ETHER_MAX_LEN) {
413 /* packet is obviously too large: toss it */
414 IFNET_STAT_INC(ifp, oerrors, 1);
415 m_freem(m0);
416 goto readcheck;
418 VX_BUSY_WAIT;
419 if (CSR_READ_2(sc, VX_W1_FREE_TX) < len + pad + 4) {
420 CSR_WRITE_2(sc, VX_COMMAND, SET_TX_AVAIL_THRESH | ((len + pad + 4) >> 2));
421 /* not enough room in FIFO */
422 if (CSR_READ_2(sc, VX_W1_FREE_TX) < len + pad + 4) { /* make sure */
423 ifq_set_oactive(&ifp->if_snd);
424 ifp->if_timer = 1;
425 ifq_prepend(&ifp->if_snd, m0);
426 return;
429 CSR_WRITE_2(sc, VX_COMMAND, SET_TX_AVAIL_THRESH | (8188 >> 2));
431 VX_BUSY_WAIT;
432 CSR_WRITE_2(sc, VX_COMMAND, SET_TX_START_THRESH |
433 ((len / 4 + sc->tx_start_thresh) >> 2));
435 BPF_MTAP(ifp, m0);
438 * Do the output in a critical section so that an interrupt from
439 * another device won't cause a FIFO underrun.
441 crit_enter();
443 CSR_WRITE_4(sc, VX_W1_TX_PIO_WR_1, len | TX_INDICATE);
445 while (m0) {
446 if (m0->m_len > 3)
447 bus_space_write_multi_4(sc->vx_btag, sc->vx_bhandle,
448 VX_W1_TX_PIO_WR_1,
449 (u_int32_t *)mtod(m0, caddr_t), m0->m_len / 4);
450 if (m0->m_len & 3)
451 bus_space_write_multi_1(sc->vx_btag, sc->vx_bhandle,
452 VX_W1_TX_PIO_WR_1,
453 mtod(m0, caddr_t) + (m0->m_len & ~3), m0->m_len & 3);
454 m0 = m_free(m0);
456 while (pad--)
457 CSR_WRITE_1(sc, VX_W1_TX_PIO_WR_1, 0); /* Padding */
459 crit_exit();
461 IFNET_STAT_INC(ifp, opackets, 1);
462 ifp->if_timer = 1;
464 readcheck:
465 if ((CSR_READ_2(sc, VX_W1_RX_STATUS) & ERR_INCOMPLETE) == 0) {
466 /* We received a complete packet. */
468 if ((CSR_READ_2(sc, VX_STATUS) & S_INTR_LATCH) != 0) {
469 /* Got an interrupt, return so that it gets serviced. */
470 return;
473 * No interrupt, read the packet and continue
474 * Is this supposed to happen? Is my motherboard
475 * completely busted?
477 vxread(sc);
478 } else {
479 /* Check if we are stuck and reset [see XXX comment] */
480 if (vxstatus(sc)) {
481 if (ifp->if_flags & IFF_DEBUG)
482 if_printf(ifp, "adapter reset\n");
483 vxreset(sc);
487 goto startagain;
491 * XXX: The 3c509 card can get in a mode where both the fifo status bit
492 * FIFOS_RX_OVERRUN and the status bit ERR_INCOMPLETE are set
493 * We detect this situation and we reset the adapter.
494 * It happens at times when there is a lot of broadcast traffic
495 * on the cable (once in a blue moon).
497 static int
498 vxstatus(struct vx_softc *sc)
500 int fifost;
501 struct ifnet *ifp = &sc->arpcom.ac_if;
504 * Check the FIFO status and act accordingly
506 GO_WINDOW(4);
507 fifost = CSR_READ_2(sc, VX_W4_FIFO_DIAG);
508 GO_WINDOW(1);
510 if (fifost & FIFOS_RX_UNDERRUN) {
511 if (ifp->if_flags & IFF_DEBUG)
512 if_printf(ifp, "RX underrun\n");
513 vxreset(sc);
514 return 0;
517 if (fifost & FIFOS_RX_STATUS_OVERRUN) {
518 if (ifp->if_flags & IFF_DEBUG)
519 if_printf(ifp, "RX Status overrun\n");
520 return 1;
523 if (fifost & FIFOS_RX_OVERRUN) {
524 if (ifp->if_flags & IFF_DEBUG)
525 if_printf(ifp, "RX overrun\n");
526 return 1;
529 if (fifost & FIFOS_TX_OVERRUN) {
530 if (ifp->if_flags & IFF_DEBUG)
531 if_printf(ifp, "TX overrun\n");
532 vxreset(sc);
535 return 0;
538 static void
539 vxtxstat(struct vx_softc *sc)
541 int i;
542 struct ifnet *ifp = &sc->arpcom.ac_if;
545 * We need to read+write TX_STATUS until we get a 0 status
546 * in order to turn off the interrupt flag.
548 while ((i = CSR_READ_1(sc, VX_W1_TX_STATUS)) & TXS_COMPLETE) {
549 CSR_WRITE_1(sc, VX_W1_TX_STATUS, 0x0);
551 if (i & TXS_JABBER) {
552 IFNET_STAT_INC(ifp, oerrors, 1);
553 if (ifp->if_flags & IFF_DEBUG)
554 if_printf(ifp, "jabber (%x)\n", i);
555 vxreset(sc);
556 } else if (i & TXS_UNDERRUN) {
557 IFNET_STAT_INC(ifp, oerrors, 1);
558 if (ifp->if_flags & IFF_DEBUG) {
559 if_printf(ifp, "fifo underrun (%x) @%d\n",
560 i, sc->tx_start_thresh);
562 if (sc->tx_succ_ok < 100) {
563 sc->tx_start_thresh = min(
564 ETHER_MAX_LEN, sc->tx_start_thresh + 20);
566 sc->tx_succ_ok = 0;
567 vxreset(sc);
568 } else if (i & TXS_MAX_COLLISION) {
569 IFNET_STAT_INC(ifp, collisions, 1);
570 CSR_WRITE_2(sc, VX_COMMAND, TX_ENABLE);
571 ifq_clr_oactive(&ifp->if_snd);
572 } else {
573 sc->tx_succ_ok = (sc->tx_succ_ok+1) & 127;
578 void
579 vxintr(void *voidsc)
581 short status;
582 struct vx_softc *sc = voidsc;
583 struct ifnet *ifp = &sc->arpcom.ac_if;
585 for (;;) {
586 CSR_WRITE_2(sc, VX_COMMAND, C_INTR_LATCH);
588 status = CSR_READ_2(sc, VX_STATUS);
590 if ((status & (S_TX_COMPLETE | S_TX_AVAIL |
591 S_RX_COMPLETE | S_CARD_FAILURE)) == 0)
592 break;
595 * Acknowledge any interrupts. It's important that we do this
596 * first, since there would otherwise be a race condition.
597 * Due to the i386 interrupt queueing, we may get spurious
598 * interrupts occasionally.
600 CSR_WRITE_2(sc, VX_COMMAND, ACK_INTR | status);
602 if (status & S_RX_COMPLETE)
603 vxread(sc);
604 if (status & S_TX_AVAIL) {
605 ifp->if_timer = 0;
606 ifq_clr_oactive(&ifp->if_snd);
607 if_devstart(ifp);
609 if (status & S_CARD_FAILURE) {
610 if_printf(ifp, "adapter failure (%x)\n", status);
611 ifp->if_timer = 0;
612 vxreset(sc);
613 return;
615 if (status & S_TX_COMPLETE) {
616 ifp->if_timer = 0;
617 vxtxstat(sc);
618 if_devstart(ifp);
621 /* no more interrupts */
624 static void
625 vxread(struct vx_softc *sc)
627 struct ifnet *ifp = &sc->arpcom.ac_if;
628 struct mbuf *m;
629 struct ether_header *eh;
630 u_int len;
632 len = CSR_READ_2(sc, VX_W1_RX_STATUS);
634 again:
636 if (ifp->if_flags & IFF_DEBUG) {
637 int err = len & ERR_MASK;
638 const char *s = NULL;
640 if (len & ERR_INCOMPLETE)
641 s = "incomplete packet";
642 else if (err == ERR_OVERRUN)
643 s = "packet overrun";
644 else if (err == ERR_RUNT)
645 s = "runt packet";
646 else if (err == ERR_ALIGNMENT)
647 s = "bad alignment";
648 else if (err == ERR_CRC)
649 s = "bad crc";
650 else if (err == ERR_OVERSIZE)
651 s = "oversized packet";
652 else if (err == ERR_DRIBBLE)
653 s = "dribble bits";
655 if (s)
656 if_printf(ifp, "%s\n", s);
659 if (len & ERR_INCOMPLETE)
660 return;
662 if (len & ERR_RX) {
663 IFNET_STAT_INC(ifp, ierrors, 1);
664 goto abort;
667 len &= RX_BYTES_MASK; /* Lower 11 bits = RX bytes. */
669 /* Pull packet off interface. */
670 m = vxget(sc, len);
671 if (m == NULL) {
672 IFNET_STAT_INC(ifp, ierrors, 1);
673 goto abort;
676 IFNET_STAT_INC(ifp, ipackets, 1);
678 /* We assume the header fit entirely in one mbuf. */
679 eh = mtod(m, struct ether_header *);
682 * XXX: Some cards seem to be in promiscous mode all the time.
683 * we need to make sure we only get our own stuff always.
684 * bleah!
687 if ((eh->ether_dhost[0] & 1) == 0 /* !mcast and !bcast */
688 && bcmp(eh->ether_dhost, sc->arpcom.ac_enaddr, ETHER_ADDR_LEN) != 0) {
689 m_freem(m);
690 return;
693 ifp->if_input(ifp, m, NULL, -1);
696 * In periods of high traffic we can actually receive enough
697 * packets so that the fifo overrun bit will be set at this point,
698 * even though we just read a packet. In this case we
699 * are not going to receive any more interrupts. We check for
700 * this condition and read again until the fifo is not full.
701 * We could simplify this test by not using vxstatus(), but
702 * rechecking the RX_STATUS register directly. This test could
703 * result in unnecessary looping in cases where there is a new
704 * packet but the fifo is not full, but it will not fix the
705 * stuck behavior.
707 * Even with this improvement, we still get packet overrun errors
708 * which are hurting performance. Maybe when I get some more time
709 * I'll modify vxread() so that it can handle RX_EARLY interrupts.
711 if (vxstatus(sc)) {
712 len = CSR_READ_2(sc, VX_W1_RX_STATUS);
713 /* Check if we are stuck and reset [see XXX comment] */
714 if (len & ERR_INCOMPLETE) {
715 if (ifp->if_flags & IFF_DEBUG)
716 if_printf(ifp, "adapter reset\n");
717 vxreset(sc);
718 return;
720 goto again;
723 return;
725 abort:
726 CSR_WRITE_2(sc, VX_COMMAND, RX_DISCARD_TOP_PACK);
729 static struct mbuf *
730 vxget(struct vx_softc *sc, u_int totlen)
732 struct ifnet *ifp = &sc->arpcom.ac_if;
733 struct mbuf *top, **mp, *m;
734 int len;
736 m = sc->mb[sc->next_mb];
737 sc->mb[sc->next_mb] = NULL;
738 if (m == NULL) {
739 MGETHDR(m, M_NOWAIT, MT_DATA);
740 if (m == NULL)
741 return NULL;
742 } else {
743 /* If the queue is no longer full, refill. */
744 if (sc->last_mb == sc->next_mb && sc->buffill_pending == 0) {
745 callout_reset(&sc->vx_timer, 1, vxmbuffill, sc);
746 sc->buffill_pending = 1;
748 /* Convert one of our saved mbuf's. */
749 sc->next_mb = (sc->next_mb + 1) % MAX_MBS;
750 m->m_data = m->m_pktdat;
751 m->m_flags = M_PKTHDR;
752 bzero(&m->m_pkthdr, sizeof(m->m_pkthdr));
754 m->m_pkthdr.rcvif = ifp;
755 m->m_pkthdr.len = totlen;
756 len = MHLEN;
757 top = NULL;
758 mp = &top;
761 * We read the packet in a critical section so that an interrupt
762 * from another device doesn't cause the card's buffer to overflow
763 * while we're reading it. We may still lose packets at other times.
765 crit_enter();
768 * Since we don't set allowLargePackets bit in MacControl register,
769 * we can assume that totlen <= 1500bytes.
770 * The while loop will be performed if we have a packet with
771 * MLEN < m_len < MINCLSIZE.
773 while (totlen > 0) {
774 if (top) {
775 m = sc->mb[sc->next_mb];
776 sc->mb[sc->next_mb] = NULL;
777 if (m == NULL) {
778 MGET(m, M_NOWAIT, MT_DATA);
779 if (m == NULL) {
780 crit_exit();
781 m_freem(top);
782 return NULL;
784 } else {
785 sc->next_mb = (sc->next_mb + 1) % MAX_MBS;
787 len = MLEN;
789 if (totlen >= MINCLSIZE) {
790 MCLGET(m, M_NOWAIT);
791 if (m->m_flags & M_EXT)
792 len = MCLBYTES;
794 len = min(totlen, len);
795 if (len > 3)
796 bus_space_read_multi_4(sc->vx_btag, sc->vx_bhandle,
797 VX_W1_RX_PIO_RD_1, mtod(m, u_int32_t *), len / 4);
798 if (len & 3) {
799 bus_space_read_multi_1(sc->vx_btag, sc->vx_bhandle,
800 VX_W1_RX_PIO_RD_1, mtod(m, u_int8_t *) + (len & ~3),
801 len & 3);
803 m->m_len = len;
804 totlen -= len;
805 *mp = m;
806 mp = &m->m_next;
809 CSR_WRITE_2(sc, VX_COMMAND, RX_DISCARD_TOP_PACK);
811 crit_exit();
813 return top;
817 static int
818 vxioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
820 struct vx_softc *sc = ifp->if_softc;
821 struct ifreq *ifr = (struct ifreq *) data;
822 int error = 0;
824 switch (cmd) {
825 case SIOCSIFFLAGS:
826 if ((ifp->if_flags & IFF_UP) == 0 &&
827 (ifp->if_flags & IFF_RUNNING) != 0) {
829 * If interface is marked up and it is stopped, then
830 * start it.
832 vxstop(sc);
833 ifp->if_flags &= ~IFF_RUNNING;
834 } else if ((ifp->if_flags & IFF_UP) != 0 &&
835 (ifp->if_flags & IFF_RUNNING) == 0) {
837 * If interface is marked up and it is stopped, then
838 * start it.
840 vxinit(sc);
841 } else {
843 * deal with flags changes:
844 * IFF_MULTICAST, IFF_PROMISC,
845 * IFF_LINK0, IFF_LINK1,
847 vxsetfilter(sc);
848 vxsetlink(sc);
850 break;
852 case SIOCSIFMTU:
854 * Set the interface MTU.
856 if (ifr->ifr_mtu > ETHERMTU) {
857 error = EINVAL;
858 } else {
859 ifp->if_mtu = ifr->ifr_mtu;
861 break;
863 case SIOCADDMULTI:
864 case SIOCDELMULTI:
866 * Multicast list has changed; set the hardware filter
867 * accordingly.
869 vxreset(sc);
870 error = 0;
871 break;
874 default:
875 ether_ioctl(ifp, cmd, data);
876 break;
878 return (error);
881 static void
882 vxreset(struct vx_softc *sc)
884 vxstop(sc);
885 vxinit(sc);
888 static void
889 vxwatchdog(struct ifnet *ifp)
891 struct vx_softc *sc = ifp->if_softc;
893 if (ifp->if_flags & IFF_DEBUG)
894 if_printf(ifp, "device timeout\n");
895 ifq_clr_oactive(&ifp->if_snd);
896 if_devstart(ifp);
897 vxintr(sc);
900 void
901 vxstop(struct vx_softc *sc)
903 struct ifnet *ifp = &sc->arpcom.ac_if;
905 ifp->if_timer = 0;
907 CSR_WRITE_2(sc, VX_COMMAND, RX_DISABLE);
908 CSR_WRITE_2(sc, VX_COMMAND, RX_DISCARD_TOP_PACK);
909 VX_BUSY_WAIT;
910 CSR_WRITE_2(sc, VX_COMMAND, TX_DISABLE);
911 CSR_WRITE_2(sc, VX_COMMAND, STOP_TRANSCEIVER);
912 DELAY(800);
913 CSR_WRITE_2(sc, VX_COMMAND, RX_RESET);
914 VX_BUSY_WAIT;
915 CSR_WRITE_2(sc, VX_COMMAND, TX_RESET);
916 VX_BUSY_WAIT;
917 CSR_WRITE_2(sc, VX_COMMAND, C_INTR_LATCH);
918 CSR_WRITE_2(sc, VX_COMMAND, SET_RD_0_MASK);
919 CSR_WRITE_2(sc, VX_COMMAND, SET_INTR_MASK);
920 CSR_WRITE_2(sc, VX_COMMAND, SET_RX_FILTER);
922 vxmbufempty(sc);
926 vxbusyeeprom(struct vx_softc *sc)
928 int j, i = 100;
930 while (i--) {
931 j = CSR_READ_2(sc, VX_W0_EEPROM_COMMAND);
932 if (j & EEPROM_BUSY)
933 DELAY(100);
934 else
935 break;
937 if (!i) {
938 if_printf(&sc->arpcom.ac_if, "eeprom failed to come ready\n");
939 return (1);
941 return (0);
944 static void
945 vxmbuffill(void *sp)
947 struct vx_softc *sc = (struct vx_softc *) sp;
948 struct ifnet *ifp = &sc->arpcom.ac_if;
950 lwkt_serialize_enter(ifp->if_serializer);
951 vxmbuffill_serialized(sp);
952 lwkt_serialize_exit(ifp->if_serializer);
955 static void
956 vxmbuffill_serialized(void *sp)
958 struct vx_softc *sc = (struct vx_softc *) sp;
959 int i;
961 i = sc->last_mb;
962 do {
963 if (sc->mb[i] == NULL)
964 MGET(sc->mb[i], M_NOWAIT, MT_DATA);
965 if (sc->mb[i] == NULL)
966 break;
967 i = (i + 1) % MAX_MBS;
968 } while (i != sc->next_mb);
969 sc->last_mb = i;
970 /* If the queue was not filled, try again. */
971 if (sc->last_mb != sc->next_mb) {
972 callout_reset(&sc->vx_timer, 1, vxmbuffill, sc);
973 sc->buffill_pending = 1;
974 } else {
975 sc->buffill_pending = 0;
979 static void
980 vxmbufempty(struct vx_softc *sc)
982 int i;
984 for (i = 0; i < MAX_MBS; i++) {
985 if (sc->mb[i]) {
986 m_freem(sc->mb[i]);
987 sc->mb[i] = NULL;
990 sc->last_mb = sc->next_mb = 0;
991 if (sc->buffill_pending != 0)
992 callout_stop(&sc->vx_timer);