FreeBSD/Linux Kernel Cross Reference
sys/dev/vx/if_vx.c
1 /*
2 * Copyright (c) 1994 Herb Peyerl <hpeyerl@novatel.ca>
3 * All rights reserved.
4 *
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.
18 *
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.
29 *
30 * $FreeBSD$
31 *
32 */
33
34 /*
35 * Created from if_ep.c driver by Fred Gray (fgray@rice.edu) to support
36 * the 3c590 family.
37 */
38
39 /*
40 * Modified from the FreeBSD 1.1.5.1 version by:
41 * Andres Vega Garcia
42 * INRIA - Sophia Antipolis, France
43 * avega@sophia.inria.fr
44 */
45
46 /*
47 * Promiscuous mode added and interrupt logic slightly changed
48 * to reduce the number of adapter failures. Transceiver select
49 * logic changed to use value from EEPROM. Autoconfiguration
50 * features added.
51 * Done by:
52 * Serge Babkin
53 * Chelindbank (Chelyabinsk, Russia)
54 * babkin@hq.icb.chel.su
55 */
56
57 #include "vx.h"
58
59 #if NVX < 4 /* These cost 4 bytes apiece, so give us 4 */
60 #undef NVX
61 #define NVX 4
62 #endif
63
64
65 #include <sys/param.h>
66 #include <sys/systm.h>
67 #include <sys/sockio.h>
68 #include <sys/malloc.h>
69 #include <sys/mbuf.h>
70 #include <sys/socket.h>
71
72 #include <net/if.h>
73
74 #include <net/ethernet.h>
75 #include <net/if_arp.h>
76
77 #include <net/bpf.h>
78
79 #include <machine/clock.h>
80
81 #include <dev/vx/if_vxreg.h>
82
83 #define ETHER_MAX_LEN 1518
84 #define ETHER_ADDR_LEN 6
85
86 struct vx_softc *vx_softc[NVX];
87
88 u_long vx_count; /* both PCI and EISA */
89
90 static struct connector_entry {
91 int bit;
92 char *name;
93 } conn_tab[VX_CONNECTORS] = {
94 #define CONNECTOR_UTP 0
95 { 0x08, "utp"},
96 #define CONNECTOR_AUI 1
97 { 0x20, "aui"},
98 /* dummy */
99 { 0, "???"},
100 #define CONNECTOR_BNC 3
101 { 0x10, "bnc"},
102 #define CONNECTOR_TX 4
103 { 0x02, "tx"},
104 #define CONNECTOR_FX 5
105 { 0x04, "fx"},
106 #define CONNECTOR_MII 6
107 { 0x40, "mii"},
108 { 0, "???"}
109 };
110
111 /* struct vx_softc *vxalloc __P((int)); */
112 /* void *vxfree __P((struct vx_softc *)); */
113 /* int vxattach __P((struct vx_softc *)); */
114 static void vxtxstat __P((struct vx_softc *));
115 static int vxstatus __P((struct vx_softc *));
116 static void vxinit __P((void *));
117 static int vxioctl __P((struct ifnet *, u_long, caddr_t));
118 static void vxstart __P((struct ifnet *ifp));
119 static void vxwatchdog __P((struct ifnet *));
120 static void vxreset __P((struct vx_softc *));
121 /* void vxstop __P((struct vx_softc *)); */
122 static void vxread __P((struct vx_softc *));
123 static struct mbuf *vxget __P((struct vx_softc *, u_int));
124 static void vxmbuffill __P((void *));
125 static void vxmbufempty __P((struct vx_softc *));
126 static void vxsetfilter __P((struct vx_softc *));
127 static void vxgetlink __P((struct vx_softc *));
128 static void vxsetlink __P((struct vx_softc *));
129 /* int vxbusyeeprom __P((struct vx_softc *)); */
130
131 struct vx_softc *
132 vxalloc(unit)
133 int unit;
134 {
135 struct vx_softc *sc;
136
137 if (unit >= NVX) {
138 printf("vx%d: unit number too high.\n", unit);
139 return NULL;
140 }
141
142 if (vx_softc[unit]) {
143 printf("vx%d: already allocated.\n", unit);
144 return NULL;
145 }
146
147 sc = malloc(sizeof(struct vx_softc), M_DEVBUF, M_NOWAIT);
148 if (sc == NULL) {
149 printf("vx%d: cannot malloc.\n", unit);
150 return NULL;
151 }
152 bzero(sc, sizeof(struct vx_softc));
153 callout_handle_init(&sc->ch);
154
155 vx_softc[unit] = sc;
156 sc->unit = unit;
157 return (sc);
158 }
159
160 void
161 vxfree(sc)
162 struct vx_softc *sc;
163 {
164 vx_softc[sc->unit] = NULL;
165 free(sc, M_DEVBUF);
166 return;
167 }
168
169 int
170 vxattach(sc)
171 struct vx_softc *sc;
172 {
173 struct ifnet *ifp = &sc->arpcom.ac_if;
174 int i;
175
176 GO_WINDOW(0);
177 outw(VX_COMMAND, GLOBAL_RESET);
178 VX_BUSY_WAIT;
179
180 vxgetlink(sc);
181
182 /*
183 * Read the station address from the eeprom
184 */
185 GO_WINDOW(0);
186 for (i = 0; i < 3; i++) {
187 int x;
188 if (vxbusyeeprom(sc))
189 return 0;
190 outw(BASE + VX_W0_EEPROM_COMMAND, EEPROM_CMD_RD
191 | (EEPROM_OEM_ADDR_0 + i));
192 if (vxbusyeeprom(sc))
193 return 0;
194 x = inw(BASE + VX_W0_EEPROM_DATA);
195 sc->arpcom.ac_enaddr[(i << 1)] = x >> 8;
196 sc->arpcom.ac_enaddr[(i << 1) + 1] = x;
197 }
198
199 printf(" address %6D\n", sc->arpcom.ac_enaddr, ":");
200
201 ifp->if_unit = sc->unit;
202 ifp->if_name = "vx";
203 ifp->if_mtu = ETHERMTU;
204 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
205 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
206 ifp->if_output = ether_output;
207 ifp->if_start = vxstart;
208 ifp->if_ioctl = vxioctl;
209 ifp->if_init = vxinit;
210 ifp->if_watchdog = vxwatchdog;
211 ifp->if_softc = sc;
212
213 ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
214
215 sc->tx_start_thresh = 20; /* probably a good starting point. */
216
217 vxstop(sc);
218
219 return 1;
220 }
221
222
223
224 /*
225 * The order in here seems important. Otherwise we may not receive
226 * interrupts. ?!
227 */
228 static void
229 vxinit(xsc)
230 void *xsc;
231 {
232 struct vx_softc *sc = (struct vx_softc *) xsc;
233 struct ifnet *ifp = &sc->arpcom.ac_if;
234 int i;
235
236 VX_BUSY_WAIT;
237
238 GO_WINDOW(2);
239
240 for (i = 0; i < 6; i++) /* Reload the ether_addr. */
241 outb(BASE + VX_W2_ADDR_0 + i, sc->arpcom.ac_enaddr[i]);
242
243 outw(BASE + VX_COMMAND, RX_RESET);
244 VX_BUSY_WAIT;
245 outw(BASE + VX_COMMAND, TX_RESET);
246 VX_BUSY_WAIT;
247
248 GO_WINDOW(1); /* Window 1 is operating window */
249 for (i = 0; i < 31; i++)
250 inb(BASE + VX_W1_TX_STATUS);
251
252 outw(BASE + VX_COMMAND,SET_RD_0_MASK | S_CARD_FAILURE |
253 S_RX_COMPLETE | S_TX_COMPLETE | S_TX_AVAIL);
254 outw(BASE + VX_COMMAND,SET_INTR_MASK | S_CARD_FAILURE |
255 S_RX_COMPLETE | S_TX_COMPLETE | S_TX_AVAIL);
256
257 /*
258 * Attempt to get rid of any stray interrupts that occured during
259 * configuration. On the i386 this isn't possible because one may
260 * already be queued. However, a single stray interrupt is
261 * unimportant.
262 */
263 outw(BASE + VX_COMMAND, ACK_INTR | 0xff);
264
265 vxsetfilter(sc);
266 vxsetlink(sc);
267
268 outw(BASE + VX_COMMAND, RX_ENABLE);
269 outw(BASE + VX_COMMAND, TX_ENABLE);
270
271 vxmbuffill((caddr_t) sc);
272
273 /* Interface is now `running', with no output active. */
274 ifp->if_flags |= IFF_RUNNING;
275 ifp->if_flags &= ~IFF_OACTIVE;
276
277 /* Attempt to start output, if any. */
278 vxstart(ifp);
279 }
280
281 static void
282 vxsetfilter(sc)
283 struct vx_softc *sc;
284 {
285 register struct ifnet *ifp = &sc->arpcom.ac_if;
286
287 GO_WINDOW(1); /* Window 1 is operating window */
288 outw(BASE + VX_COMMAND, SET_RX_FILTER | FIL_INDIVIDUAL | FIL_BRDCST |
289 FIL_MULTICAST |
290 ((ifp->if_flags & IFF_PROMISC) ? FIL_PROMISC : 0 ));
291 }
292
293 static void
294 vxgetlink(sc)
295 struct vx_softc *sc;
296 {
297 int n, k;
298
299 GO_WINDOW(3);
300 sc->vx_connectors = inw(BASE + VX_W3_RESET_OPT) & 0x7f;
301 for (n = 0, k = 0; k < VX_CONNECTORS; k++) {
302 if (sc->vx_connectors & conn_tab[k].bit) {
303 if (n > 0) {
304 printf("/");
305 }
306 printf("%s", conn_tab[k].name);
307 n++;
308 }
309 }
310 if (sc->vx_connectors == 0) {
311 printf("no connectors!");
312 return;
313 }
314 GO_WINDOW(3);
315 sc->vx_connector = (inl(BASE + VX_W3_INTERNAL_CFG)
316 & INTERNAL_CONNECTOR_MASK)
317 >> INTERNAL_CONNECTOR_BITS;
318 if (sc->vx_connector & 0x10) {
319 sc->vx_connector &= 0x0f;
320 printf("[*%s*]", conn_tab[(int)sc->vx_connector].name);
321 printf(": disable 'auto select' with DOS util!");
322 } else {
323 printf("[*%s*]", conn_tab[(int)sc->vx_connector].name);
324 }
325 }
326
327 static void
328 vxsetlink(sc)
329 struct vx_softc *sc;
330 {
331 register struct ifnet *ifp = &sc->arpcom.ac_if;
332 int i, j, k;
333 char *reason, *warning;
334 static short prev_flags;
335 static char prev_conn = -1;
336
337 if (prev_conn == -1) {
338 prev_conn = sc->vx_connector;
339 }
340
341 /*
342 * S.B.
343 *
344 * Now behavior was slightly changed:
345 *
346 * if any of flags link[0-2] is used and its connector is
347 * physically present the following connectors are used:
348 *
349 * link0 - AUI * highest precedence
350 * link1 - BNC
351 * link2 - UTP * lowest precedence
352 *
353 * If none of them is specified then
354 * connector specified in the EEPROM is used
355 * (if present on card or UTP if not).
356 */
357
358 i = sc->vx_connector; /* default in EEPROM */
359 reason = "default";
360 warning = 0;
361
362 if (ifp->if_flags & IFF_LINK0) {
363 if (sc->vx_connectors & conn_tab[CONNECTOR_AUI].bit) {
364 i = CONNECTOR_AUI;
365 reason = "link0";
366 } else {
367 warning = "aui not present! (link0)";
368 }
369 } else if (ifp->if_flags & IFF_LINK1) {
370 if (sc->vx_connectors & conn_tab[CONNECTOR_BNC].bit) {
371 i = CONNECTOR_BNC;
372 reason = "link1";
373 } else {
374 warning = "bnc not present! (link1)";
375 }
376 } else if (ifp->if_flags & IFF_LINK2) {
377 if (sc->vx_connectors & conn_tab[CONNECTOR_UTP].bit) {
378 i = CONNECTOR_UTP;
379 reason = "link2";
380 } else {
381 warning = "utp not present! (link2)";
382 }
383 } else if ((sc->vx_connectors & conn_tab[(int)sc->vx_connector].bit) == 0) {
384 warning = "strange connector type in EEPROM.";
385 reason = "forced";
386 i = CONNECTOR_UTP;
387 }
388
389 /* Avoid unnecessary message. */
390 k = (prev_flags ^ ifp->if_flags) & (IFF_LINK0 | IFF_LINK1 | IFF_LINK2);
391 if ((k != 0) || (prev_conn != i)) {
392 if (warning != 0) {
393 printf("vx%d: warning: %s\n", sc->unit, warning);
394 }
395 printf("vx%d: selected %s. (%s)\n",
396 sc->unit, conn_tab[i].name, reason);
397 }
398
399 /* Set the selected connector. */
400 GO_WINDOW(3);
401 j = inl(BASE + VX_W3_INTERNAL_CFG) & ~INTERNAL_CONNECTOR_MASK;
402 outl(BASE + VX_W3_INTERNAL_CFG, j | (i <<INTERNAL_CONNECTOR_BITS));
403
404 /* First, disable all. */
405 outw(BASE + VX_COMMAND, STOP_TRANSCEIVER);
406 DELAY(800);
407 GO_WINDOW(4);
408 outw(BASE + VX_W4_MEDIA_TYPE, 0);
409
410 /* Second, enable the selected one. */
411 switch(i) {
412 case CONNECTOR_UTP:
413 GO_WINDOW(4);
414 outw(BASE + VX_W4_MEDIA_TYPE, ENABLE_UTP);
415 break;
416 case CONNECTOR_BNC:
417 outw(BASE + VX_COMMAND, START_TRANSCEIVER);
418 DELAY(800);
419 break;
420 case CONNECTOR_TX:
421 case CONNECTOR_FX:
422 GO_WINDOW(4);
423 outw(BASE + VX_W4_MEDIA_TYPE, LINKBEAT_ENABLE);
424 break;
425 default: /* AUI and MII fall here */
426 break;
427 }
428 GO_WINDOW(1);
429
430 prev_flags = ifp->if_flags;
431 prev_conn = i;
432 }
433
434 static void
435 vxstart(ifp)
436 struct ifnet *ifp;
437 {
438 register struct vx_softc *sc = vx_softc[ifp->if_unit];
439 register struct mbuf *m0;
440 int sh, len, pad;
441
442 /* Don't transmit if interface is busy or not running */
443 if ((sc->arpcom.ac_if.if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
444 return;
445
446 startagain:
447 /* Sneak a peek at the next packet */
448 m0 = ifp->if_snd.ifq_head;
449 if (m0 == 0) {
450 return;
451 }
452 /* We need to use m->m_pkthdr.len, so require the header */
453 if ((m0->m_flags & M_PKTHDR) == 0)
454 panic("vxstart: no header mbuf");
455 len = m0->m_pkthdr.len;
456
457 pad = (4 - len) & 3;
458
459 /*
460 * The 3c509 automatically pads short packets to minimum ethernet length,
461 * but we drop packets that are too large. Perhaps we should truncate
462 * them instead?
463 */
464 if (len + pad > ETHER_MAX_LEN) {
465 /* packet is obviously too large: toss it */
466 ++ifp->if_oerrors;
467 IF_DEQUEUE(&ifp->if_snd, m0);
468 m_freem(m0);
469 goto readcheck;
470 }
471 VX_BUSY_WAIT;
472 if (inw(BASE + VX_W1_FREE_TX) < len + pad + 4) {
473 outw(BASE + VX_COMMAND, SET_TX_AVAIL_THRESH | ((len + pad + 4) >> 2));
474 /* not enough room in FIFO */
475 if (inw(BASE + VX_W1_FREE_TX) < len + pad + 4) { /* make sure */
476 ifp->if_flags |= IFF_OACTIVE;
477 ifp->if_timer = 1;
478 return;
479 }
480 }
481 outw(BASE + VX_COMMAND, SET_TX_AVAIL_THRESH | (8188 >> 2));
482 IF_DEQUEUE(&ifp->if_snd, m0);
483 if (m0 == NULL) { /* not really needed */
484 return;
485 }
486
487 VX_BUSY_WAIT;
488 outw(BASE + VX_COMMAND, SET_TX_START_THRESH |
489 ((len / 4 + sc->tx_start_thresh) >> 2));
490
491 if (sc->arpcom.ac_if.if_bpf) {
492 bpf_mtap(&sc->arpcom.ac_if, m0);
493 }
494
495 /*
496 * Do the output at splhigh() so that an interrupt from another device
497 * won't cause a FIFO underrun.
498 */
499 sh = splhigh();
500
501 outl(BASE + VX_W1_TX_PIO_WR_1, len | TX_INDICATE);
502
503 while (m0) {
504 if (m0->m_len > 3)
505 outsl(BASE + VX_W1_TX_PIO_WR_1, mtod(m0, caddr_t), m0->m_len / 4);
506 if (m0->m_len & 3)
507 outsb(BASE + VX_W1_TX_PIO_WR_1,
508 mtod(m0, caddr_t) + (m0->m_len & ~3) , m0->m_len & 3);
509 m0 = m_free(m0);
510 }
511 while (pad--)
512 outb(BASE + VX_W1_TX_PIO_WR_1, 0); /* Padding */
513
514 splx(sh);
515
516 ++ifp->if_opackets;
517 ifp->if_timer = 1;
518
519 readcheck:
520 if ((inw(BASE + VX_W1_RX_STATUS) & ERR_INCOMPLETE) == 0) {
521 /* We received a complete packet. */
522
523 if ((inw(BASE + VX_STATUS) & S_INTR_LATCH) == 0) {
524 /*
525 * No interrupt, read the packet and continue
526 * Is this supposed to happen? Is my motherboard
527 * completely busted?
528 */
529 vxread(sc);
530 } else
531 /* Got an interrupt, return so that it gets serviced. */
532 return;
533 } else {
534 /* Check if we are stuck and reset [see XXX comment] */
535 if (vxstatus(sc)) {
536 if (ifp->if_flags & IFF_DEBUG)
537 printf("vx%d: adapter reset\n", ifp->if_unit);
538 vxreset(sc);
539 }
540 }
541
542 goto startagain;
543 }
544
545 /*
546 * XXX: The 3c509 card can get in a mode where both the fifo status bit
547 * FIFOS_RX_OVERRUN and the status bit ERR_INCOMPLETE are set
548 * We detect this situation and we reset the adapter.
549 * It happens at times when there is a lot of broadcast traffic
550 * on the cable (once in a blue moon).
551 */
552 static int
553 vxstatus(sc)
554 struct vx_softc *sc;
555 {
556 int fifost;
557
558 /*
559 * Check the FIFO status and act accordingly
560 */
561 GO_WINDOW(4);
562 fifost = inw(BASE + VX_W4_FIFO_DIAG);
563 GO_WINDOW(1);
564
565 if (fifost & FIFOS_RX_UNDERRUN) {
566 if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
567 printf("vx%d: RX underrun\n", sc->unit);
568 vxreset(sc);
569 return 0;
570 }
571
572 if (fifost & FIFOS_RX_STATUS_OVERRUN) {
573 if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
574 printf("vx%d: RX Status overrun\n", sc->unit);
575 return 1;
576 }
577
578 if (fifost & FIFOS_RX_OVERRUN) {
579 if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
580 printf("vx%d: RX overrun\n", sc->unit);
581 return 1;
582 }
583
584 if (fifost & FIFOS_TX_OVERRUN) {
585 if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
586 printf("vx%d: TX overrun\n", sc->unit);
587 vxreset(sc);
588 return 0;
589 }
590
591 return 0;
592 }
593
594 static void
595 vxtxstat(sc)
596 struct vx_softc *sc;
597 {
598 int i;
599
600 /*
601 * We need to read+write TX_STATUS until we get a 0 status
602 * in order to turn off the interrupt flag.
603 */
604 while ((i = inb(BASE + VX_W1_TX_STATUS)) & TXS_COMPLETE) {
605 outb(BASE + VX_W1_TX_STATUS, 0x0);
606
607 if (i & TXS_JABBER) {
608 ++sc->arpcom.ac_if.if_oerrors;
609 if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
610 printf("vx%d: jabber (%x)\n", sc->unit, i);
611 vxreset(sc);
612 } else if (i & TXS_UNDERRUN) {
613 ++sc->arpcom.ac_if.if_oerrors;
614 if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
615 printf("vx%d: fifo underrun (%x) @%d\n",
616 sc->unit, i, sc->tx_start_thresh);
617 if (sc->tx_succ_ok < 100)
618 sc->tx_start_thresh = min(ETHER_MAX_LEN, sc->tx_start_thresh + 20);
619 sc->tx_succ_ok = 0;
620 vxreset(sc);
621 } else if (i & TXS_MAX_COLLISION) {
622 ++sc->arpcom.ac_if.if_collisions;
623 outw(BASE + VX_COMMAND, TX_ENABLE);
624 sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
625 } else
626 sc->tx_succ_ok = (sc->tx_succ_ok+1) & 127;
627 }
628 }
629
630 void
631 vxintr(voidsc)
632 void *voidsc;
633 {
634 register short status;
635 struct vx_softc *sc = voidsc;
636 struct ifnet *ifp = &sc->arpcom.ac_if;
637
638 for (;;) {
639 outw(BASE + VX_COMMAND, C_INTR_LATCH);
640
641 status = inw(BASE + VX_STATUS);
642
643 if ((status & (S_TX_COMPLETE | S_TX_AVAIL |
644 S_RX_COMPLETE | S_CARD_FAILURE)) == 0)
645 break;
646
647 /*
648 * Acknowledge any interrupts. It's important that we do this
649 * first, since there would otherwise be a race condition.
650 * Due to the i386 interrupt queueing, we may get spurious
651 * interrupts occasionally.
652 */
653 outw(BASE + VX_COMMAND, ACK_INTR | status);
654
655 if (status & S_RX_COMPLETE)
656 vxread(sc);
657 if (status & S_TX_AVAIL) {
658 ifp->if_timer = 0;
659 sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
660 vxstart(&sc->arpcom.ac_if);
661 }
662 if (status & S_CARD_FAILURE) {
663 printf("vx%d: adapter failure (%x)\n", sc->unit, status);
664 ifp->if_timer = 0;
665 vxreset(sc);
666 return;
667 }
668 if (status & S_TX_COMPLETE) {
669 ifp->if_timer = 0;
670 vxtxstat(sc);
671 vxstart(ifp);
672 }
673 }
674
675 /* no more interrupts */
676 return;
677 }
678
679 static void
680 vxread(sc)
681 struct vx_softc *sc;
682 {
683 struct ifnet *ifp = &sc->arpcom.ac_if;
684 struct mbuf *m;
685 struct ether_header *eh;
686 u_int len;
687
688 len = inw(BASE + VX_W1_RX_STATUS);
689
690 again:
691
692 if (ifp->if_flags & IFF_DEBUG) {
693 int err = len & ERR_MASK;
694 char *s = NULL;
695
696 if (len & ERR_INCOMPLETE)
697 s = "incomplete packet";
698 else if (err == ERR_OVERRUN)
699 s = "packet overrun";
700 else if (err == ERR_RUNT)
701 s = "runt packet";
702 else if (err == ERR_ALIGNMENT)
703 s = "bad alignment";
704 else if (err == ERR_CRC)
705 s = "bad crc";
706 else if (err == ERR_OVERSIZE)
707 s = "oversized packet";
708 else if (err == ERR_DRIBBLE)
709 s = "dribble bits";
710
711 if (s)
712 printf("vx%d: %s\n", sc->unit, s);
713 }
714
715 if (len & ERR_INCOMPLETE)
716 return;
717
718 if (len & ERR_RX) {
719 ++ifp->if_ierrors;
720 goto abort;
721 }
722
723 len &= RX_BYTES_MASK; /* Lower 11 bits = RX bytes. */
724
725 /* Pull packet off interface. */
726 m = vxget(sc, len);
727 if (m == 0) {
728 ifp->if_ierrors++;
729 goto abort;
730 }
731
732 ++ifp->if_ipackets;
733
734 /* We assume the header fit entirely in one mbuf. */
735 eh = mtod(m, struct ether_header *);
736
737 /*
738 * XXX: Some cards seem to be in promiscous mode all the time.
739 * we need to make sure we only get our own stuff always.
740 * bleah!
741 */
742
743 if ((eh->ether_dhost[0] & 1) == 0 /* !mcast and !bcast */
744 && bcmp(eh->ether_dhost, sc->arpcom.ac_enaddr, ETHER_ADDR_LEN) != 0) {
745 m_freem(m);
746 return;
747 }
748
749 m_adj(m, sizeof(struct ether_header));
750 ether_input(ifp, eh, m);
751
752 /*
753 * In periods of high traffic we can actually receive enough
754 * packets so that the fifo overrun bit will be set at this point,
755 * even though we just read a packet. In this case we
756 * are not going to receive any more interrupts. We check for
757 * this condition and read again until the fifo is not full.
758 * We could simplify this test by not using vxstatus(), but
759 * rechecking the RX_STATUS register directly. This test could
760 * result in unnecessary looping in cases where there is a new
761 * packet but the fifo is not full, but it will not fix the
762 * stuck behavior.
763 *
764 * Even with this improvement, we still get packet overrun errors
765 * which are hurting performance. Maybe when I get some more time
766 * I'll modify vxread() so that it can handle RX_EARLY interrupts.
767 */
768 if (vxstatus(sc)) {
769 len = inw(BASE + VX_W1_RX_STATUS);
770 /* Check if we are stuck and reset [see XXX comment] */
771 if (len & ERR_INCOMPLETE) {
772 if (ifp->if_flags & IFF_DEBUG)
773 printf("vx%d: adapter reset\n", sc->unit);
774 vxreset(sc);
775 return;
776 }
777 goto again;
778 }
779
780 return;
781
782 abort:
783 outw(BASE + VX_COMMAND, RX_DISCARD_TOP_PACK);
784 }
785
786 static struct mbuf *
787 vxget(sc, totlen)
788 struct vx_softc *sc;
789 u_int totlen;
790 {
791 struct ifnet *ifp = &sc->arpcom.ac_if;
792 struct mbuf *top, **mp, *m;
793 int len;
794 int sh;
795
796 m = sc->mb[sc->next_mb];
797 sc->mb[sc->next_mb] = 0;
798 if (m == 0) {
799 MGETHDR(m, M_DONTWAIT, MT_DATA);
800 if (m == 0)
801 return 0;
802 } else {
803 /* If the queue is no longer full, refill. */
804 if (sc->last_mb == sc->next_mb && sc->buffill_pending == 0) {
805 sc->ch = timeout(vxmbuffill, sc, 1);
806 sc->buffill_pending = 1;
807 }
808 /* Convert one of our saved mbuf's. */
809 sc->next_mb = (sc->next_mb + 1) % MAX_MBS;
810 m->m_data = m->m_pktdat;
811 m->m_flags = M_PKTHDR;
812 bzero(&m->m_pkthdr, sizeof(m->m_pkthdr));
813 }
814 m->m_pkthdr.rcvif = ifp;
815 m->m_pkthdr.len = totlen;
816 len = MHLEN;
817 top = 0;
818 mp = ⊤
819
820 /*
821 * We read the packet at splhigh() so that an interrupt from another
822 * device doesn't cause the card's buffer to overflow while we're
823 * reading it. We may still lose packets at other times.
824 */
825 sh = splhigh();
826
827 /*
828 * Since we don't set allowLargePackets bit in MacControl register,
829 * we can assume that totlen <= 1500bytes.
830 * The while loop will be performed iff we have a packet with
831 * MLEN < m_len < MINCLSIZE.
832 */
833 while (totlen > 0) {
834 if (top) {
835 m = sc->mb[sc->next_mb];
836 sc->mb[sc->next_mb] = 0;
837 if (m == 0) {
838 MGET(m, M_DONTWAIT, MT_DATA);
839 if (m == 0) {
840 splx(sh);
841 m_freem(top);
842 return 0;
843 }
844 } else {
845 sc->next_mb = (sc->next_mb + 1) % MAX_MBS;
846 }
847 len = MLEN;
848 }
849 if (totlen >= MINCLSIZE) {
850 MCLGET(m, M_DONTWAIT);
851 if (m->m_flags & M_EXT)
852 len = MCLBYTES;
853 }
854 len = min(totlen, len);
855 if (len > 3)
856 insl(BASE + VX_W1_RX_PIO_RD_1, mtod(m, u_int32_t *), len / 4);
857 if (len & 3) {
858 insb(BASE + VX_W1_RX_PIO_RD_1, mtod(m, u_int8_t *) + (len & ~3),
859 len & 3);
860 }
861 m->m_len = len;
862 totlen -= len;
863 *mp = m;
864 mp = &m->m_next;
865 }
866
867 outw(BASE +VX_COMMAND, RX_DISCARD_TOP_PACK);
868
869 splx(sh);
870
871 return top;
872 }
873
874
875 static int
876 vxioctl(ifp, cmd, data)
877 register struct ifnet *ifp;
878 u_long cmd;
879 caddr_t data;
880 {
881 struct vx_softc *sc = vx_softc[ifp->if_unit];
882 struct ifreq *ifr = (struct ifreq *) data;
883 int s, error = 0;
884
885 s = splimp();
886
887 switch (cmd) {
888 case SIOCSIFADDR:
889 case SIOCGIFADDR:
890 ether_ioctl(ifp, cmd, data);
891 break;
892
893 case SIOCSIFFLAGS:
894 if ((ifp->if_flags & IFF_UP) == 0 &&
895 (ifp->if_flags & IFF_RUNNING) != 0) {
896 /*
897 * If interface is marked up and it is stopped, then
898 * start it.
899 */
900 vxstop(sc);
901 ifp->if_flags &= ~IFF_RUNNING;
902 } else if ((ifp->if_flags & IFF_UP) != 0 &&
903 (ifp->if_flags & IFF_RUNNING) == 0) {
904 /*
905 * If interface is marked up and it is stopped, then
906 * start it.
907 */
908 vxinit(sc);
909 } else {
910 /*
911 * deal with flags changes:
912 * IFF_MULTICAST, IFF_PROMISC,
913 * IFF_LINK0, IFF_LINK1,
914 */
915 vxsetfilter(sc);
916 vxsetlink(sc);
917 }
918 break;
919
920 case SIOCSIFMTU:
921 /*
922 * Set the interface MTU.
923 */
924 if (ifr->ifr_mtu > ETHERMTU) {
925 error = EINVAL;
926 } else {
927 ifp->if_mtu = ifr->ifr_mtu;
928 }
929 break;
930
931 case SIOCADDMULTI:
932 case SIOCDELMULTI:
933 /*
934 * Multicast list has changed; set the hardware filter
935 * accordingly.
936 */
937 vxreset(sc);
938 error = 0;
939 break;
940
941
942 default:
943 error = EINVAL;
944 }
945
946 splx(s);
947
948 return (error);
949 }
950
951 static void
952 vxreset(sc)
953 struct vx_softc *sc;
954 {
955 int s;
956 s = splimp();
957
958 vxstop(sc);
959 vxinit(sc);
960 splx(s);
961 }
962
963 static void
964 vxwatchdog(ifp)
965 struct ifnet *ifp;
966 {
967 struct vx_softc *sc = vx_softc[ifp->if_unit];
968
969 if (ifp->if_flags & IFF_DEBUG)
970 printf("vx%d: device timeout\n", ifp->if_unit);
971 ifp->if_flags &= ~IFF_OACTIVE;
972 vxstart(ifp);
973 vxintr(sc);
974 }
975
976 void
977 vxstop(sc)
978 struct vx_softc *sc;
979 {
980 struct ifnet *ifp = &sc->arpcom.ac_if;
981
982 ifp->if_timer = 0;
983
984 outw(BASE + VX_COMMAND, RX_DISABLE);
985 outw(BASE + VX_COMMAND, RX_DISCARD_TOP_PACK);
986 VX_BUSY_WAIT;
987 outw(BASE + VX_COMMAND, TX_DISABLE);
988 outw(BASE + VX_COMMAND, STOP_TRANSCEIVER);
989 DELAY(800);
990 outw(BASE + VX_COMMAND, RX_RESET);
991 VX_BUSY_WAIT;
992 outw(BASE + VX_COMMAND, TX_RESET);
993 VX_BUSY_WAIT;
994 outw(BASE + VX_COMMAND, C_INTR_LATCH);
995 outw(BASE + VX_COMMAND, SET_RD_0_MASK);
996 outw(BASE + VX_COMMAND, SET_INTR_MASK);
997 outw(BASE + VX_COMMAND, SET_RX_FILTER);
998
999 vxmbufempty(sc);
1000 }
1001
1002 int
1003 vxbusyeeprom(sc)
1004 struct vx_softc *sc;
1005 {
1006 int j, i = 100;
1007
1008 while (i--) {
1009 j = inw(BASE + VX_W0_EEPROM_COMMAND);
1010 if (j & EEPROM_BUSY)
1011 DELAY(100);
1012 else
1013 break;
1014 }
1015 if (!i) {
1016 printf("vx%d: eeprom failed to come ready\n", sc->unit);
1017 return (1);
1018 }
1019 return (0);
1020 }
1021
1022 static void
1023 vxmbuffill(sp)
1024 void *sp;
1025 {
1026 struct vx_softc *sc = (struct vx_softc *) sp;
1027 int s, i;
1028
1029 s = splimp();
1030 i = sc->last_mb;
1031 do {
1032 if (sc->mb[i] == NULL)
1033 MGET(sc->mb[i], M_DONTWAIT, MT_DATA);
1034 if (sc->mb[i] == NULL)
1035 break;
1036 i = (i + 1) % MAX_MBS;
1037 } while (i != sc->next_mb);
1038 sc->last_mb = i;
1039 /* If the queue was not filled, try again. */
1040 if (sc->last_mb != sc->next_mb) {
1041 sc->ch = timeout(vxmbuffill, sc, 1);
1042 sc->buffill_pending = 1;
1043 } else {
1044 sc->buffill_pending = 0;
1045 }
1046 splx(s);
1047 }
1048
1049 static void
1050 vxmbufempty(sc)
1051 struct vx_softc *sc;
1052 {
1053 int s, i;
1054
1055 s = splimp();
1056 for (i = 0; i < MAX_MBS; i++) {
1057 if (sc->mb[i]) {
1058 m_freem(sc->mb[i]);
1059 sc->mb[i] = NULL;
1060 }
1061 }
1062 sc->last_mb = sc->next_mb = 0;
1063 if (sc->buffill_pending != 0)
1064 untimeout(vxmbuffill, sc, sc->ch);
1065 splx(s);
1066 }
Cache object: eb98a4ba8adf819342d5c54e8e64b479
|