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