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