if_vr.c revision 1.27 1 /* $NetBSD: if_vr.c,v 1.27 1999/11/04 00:24:33 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * Copyright (c) 1997, 1998
42 * Bill Paul <wpaul (at) ctr.columbia.edu>. All rights reserved.
43 *
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
46 * are met:
47 * 1. Redistributions of source code must retain the above copyright
48 * notice, this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 * notice, this list of conditions and the following disclaimer in the
51 * documentation and/or other materials provided with the distribution.
52 * 3. All advertising materials mentioning features or use of this software
53 * must display the following acknowledgement:
54 * This product includes software developed by Bill Paul.
55 * 4. Neither the name of the author nor the names of any co-contributors
56 * may be used to endorse or promote products derived from this software
57 * without specific prior written permission.
58 *
59 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
63 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
64 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
65 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
66 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
67 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
68 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
69 * THE POSSIBILITY OF SUCH DAMAGE.
70 *
71 * $FreeBSD: if_vr.c,v 1.7 1999/01/10 18:51:49 wpaul Exp $
72 */
73
74 /*
75 * VIA Rhine fast ethernet PCI NIC driver
76 *
77 * Supports various network adapters based on the VIA Rhine
78 * and Rhine II PCI controllers, including the D-Link DFE530TX.
79 * Datasheets are available at http://www.via.com.tw.
80 *
81 * Written by Bill Paul <wpaul (at) ctr.columbia.edu>
82 * Electrical Engineering Department
83 * Columbia University, New York City
84 */
85
86 /*
87 * The VIA Rhine controllers are similar in some respects to the
88 * the DEC tulip chips, except less complicated. The controller
89 * uses an MII bus and an external physical layer interface. The
90 * receiver has a one entry perfect filter and a 64-bit hash table
91 * multicast filter. Transmit and receive descriptors are similar
92 * to the tulip.
93 *
94 * The Rhine has a serious flaw in its transmit DMA mechanism:
95 * transmit buffers must be longword aligned. Unfortunately,
96 * the kernel doesn't guarantee that mbufs will be filled in starting
97 * at longword boundaries, so we have to do a buffer copy before
98 * transmission.
99 *
100 * Apparently, the receive DMA mechanism also has the same flaw. This
101 * means that on systems with struct alignment requirements, incoming
102 * frames must be copied to a new buffer which shifts the data forward
103 * 2 bytes so that the payload is aligned on a 4-byte boundary.
104 */
105
106 #include "opt_inet.h"
107
108 #include <sys/param.h>
109 #include <sys/systm.h>
110 #include <sys/sockio.h>
111 #include <sys/mbuf.h>
112 #include <sys/malloc.h>
113 #include <sys/kernel.h>
114 #include <sys/socket.h>
115 #include <sys/device.h>
116
117 #include <vm/vm.h> /* for PAGE_SIZE */
118
119 #include <net/if.h>
120 #include <net/if_arp.h>
121 #include <net/if_dl.h>
122 #include <net/if_media.h>
123 #include <net/if_ether.h>
124
125 #if defined(INET)
126 #include <netinet/in.h>
127 #include <netinet/if_inarp.h>
128 #endif
129
130 #include "bpfilter.h"
131 #if NBPFILTER > 0
132 #include <net/bpf.h>
133 #endif
134
135 #include <machine/bus.h>
136 #include <machine/intr.h>
137
138 #include <dev/mii/mii.h>
139 #include <dev/mii/miivar.h>
140
141 #include <dev/pci/pcireg.h>
142 #include <dev/pci/pcivar.h>
143 #include <dev/pci/pcidevs.h>
144
145 #include <dev/pci/if_vrreg.h>
146
147 #if BYTE_ORDER == BIG_ENDIAN
148 #include <machine/bswap.h>
149 #define htopci(x) bswap32(x)
150 #define pcitoh(x) bswap32(x)
151 #else
152 #define htopci(x) (x)
153 #define pcitoh(x) (x)
154 #endif
155
156 #define VR_USEIOSPACE
157
158 /*
159 * Various supported device vendors/types and their names.
160 */
161 static struct vr_type {
162 pci_vendor_id_t vr_vid;
163 pci_product_id_t vr_did;
164 const char *vr_name;
165 } vr_devs[] = {
166 { PCI_VENDOR_VIATECH, PCI_PRODUCT_VIATECH_VT3043,
167 "VIA VT3043 (Rhine) 10/100" },
168 { PCI_VENDOR_VIATECH, PCI_PRODUCT_VIATECH_VT86C100A,
169 "VIA VT86C100A (Rhine-II) 10/100" },
170 { 0, 0, NULL }
171 };
172
173 /*
174 * Transmit descriptor list size.
175 */
176 #define VR_NTXDESC 64
177 #define VR_NTXDESC_MASK (VR_NTXDESC - 1)
178 #define VR_NEXTTX(x) (((x) + 1) & VR_NTXDESC_MASK)
179
180 /*
181 * Receive descriptor list size.
182 */
183 #define VR_NRXDESC 64
184 #define VR_NRXDESC_MASK (VR_NRXDESC - 1)
185 #define VR_NEXTRX(x) (((x) + 1) & VR_NRXDESC_MASK)
186
187 /*
188 * Control data structres that are DMA'd to the Rhine chip. We allocate
189 * them in a single clump that maps to a single DMA segment to make several
190 * things easier.
191 *
192 * Note that since we always copy outgoing packets to aligned transmit
193 * buffers, we can reduce the transmit descriptors to one per packet.
194 */
195 struct vr_control_data {
196 struct vr_desc vr_txdescs[VR_NTXDESC];
197 struct vr_desc vr_rxdescs[VR_NRXDESC];
198 };
199
200 #define VR_CDOFF(x) offsetof(struct vr_control_data, x)
201 #define VR_CDTXOFF(x) VR_CDOFF(vr_txdescs[(x)])
202 #define VR_CDRXOFF(x) VR_CDOFF(vr_rxdescs[(x)])
203
204 /*
205 * Software state of transmit and receive descriptors.
206 */
207 struct vr_descsoft {
208 struct mbuf *ds_mbuf; /* head of mbuf chain */
209 bus_dmamap_t ds_dmamap; /* our DMA map */
210 };
211
212 struct vr_softc {
213 struct device vr_dev; /* generic device glue */
214 void *vr_ih; /* interrupt cookie */
215 void *vr_ats; /* shutdown hook */
216 bus_space_tag_t vr_bst; /* bus space tag */
217 bus_space_handle_t vr_bsh; /* bus space handle */
218 bus_dma_tag_t vr_dmat; /* bus DMA tag */
219 pci_chipset_tag_t vr_pc; /* PCI chipset info */
220 struct ethercom vr_ec; /* Ethernet common info */
221 u_int8_t vr_enaddr[ETHER_ADDR_LEN];
222 struct mii_data vr_mii; /* MII/media info */
223
224 bus_dmamap_t vr_cddmamap; /* control data DMA map */
225 #define vr_cddma vr_cddmamap->dm_segs[0].ds_addr
226
227 /*
228 * Software state for transmit and receive descriptors.
229 */
230 struct vr_descsoft vr_txsoft[VR_NTXDESC];
231 struct vr_descsoft vr_rxsoft[VR_NRXDESC];
232
233 /*
234 * Control data structures.
235 */
236 struct vr_control_data *vr_control_data;
237
238 int vr_txpending; /* number of TX requests pending */
239 int vr_txdirty; /* first dirty TX descriptor */
240 int vr_txlast; /* last used TX descriptor */
241
242 int vr_rxptr; /* next ready RX descriptor */
243 };
244
245 #define VR_CDTXADDR(sc, x) ((sc)->vr_cddma + VR_CDTXOFF((x)))
246 #define VR_CDRXADDR(sc, x) ((sc)->vr_cddma + VR_CDRXOFF((x)))
247
248 #define VR_CDTX(sc, x) (&(sc)->vr_control_data->vr_txdescs[(x)])
249 #define VR_CDRX(sc, x) (&(sc)->vr_control_data->vr_rxdescs[(x)])
250
251 #define VR_DSTX(sc, x) (&(sc)->vr_txsoft[(x)])
252 #define VR_DSRX(sc, x) (&(sc)->vr_rxsoft[(x)])
253
254 #define VR_CDTXSYNC(sc, x, ops) \
255 bus_dmamap_sync((sc)->vr_dmat, (sc)->vr_cddmamap, \
256 VR_CDTXOFF((x)), sizeof(struct vr_desc), (ops))
257
258 #define VR_CDRXSYNC(sc, x, ops) \
259 bus_dmamap_sync((sc)->vr_dmat, (sc)->vr_cddmamap, \
260 VR_CDRXOFF((x)), sizeof(struct vr_desc), (ops))
261
262 /*
263 * Note we rely on MCLBYTES being a power of two below.
264 */
265 #define VR_INIT_RXDESC(sc, i) \
266 do { \
267 struct vr_desc *__d = VR_CDRX((sc), (i)); \
268 struct vr_descsoft *__ds = VR_DSRX((sc), (i)); \
269 \
270 __d->vr_next = htopci(VR_CDRXADDR((sc), VR_NEXTRX((i)))); \
271 __d->vr_status = htopci(VR_RXSTAT_FIRSTFRAG | \
272 VR_RXSTAT_LASTFRAG | VR_RXSTAT_OWN); \
273 __d->vr_data = htopci(__ds->ds_dmamap->dm_segs[0].ds_addr); \
274 __d->vr_ctl = htopci(VR_RXCTL_CHAIN | VR_RXCTL_RX_INTR | \
275 ((MCLBYTES - 1) & VR_RXCTL_BUFLEN)); \
276 VR_CDRXSYNC((sc), (i), BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); \
277 } while (0)
278
279 /*
280 * register space access macros
281 */
282 #define CSR_WRITE_4(sc, reg, val) \
283 bus_space_write_4(sc->vr_bst, sc->vr_bsh, reg, val)
284 #define CSR_WRITE_2(sc, reg, val) \
285 bus_space_write_2(sc->vr_bst, sc->vr_bsh, reg, val)
286 #define CSR_WRITE_1(sc, reg, val) \
287 bus_space_write_1(sc->vr_bst, sc->vr_bsh, reg, val)
288
289 #define CSR_READ_4(sc, reg) \
290 bus_space_read_4(sc->vr_bst, sc->vr_bsh, reg)
291 #define CSR_READ_2(sc, reg) \
292 bus_space_read_2(sc->vr_bst, sc->vr_bsh, reg)
293 #define CSR_READ_1(sc, reg) \
294 bus_space_read_1(sc->vr_bst, sc->vr_bsh, reg)
295
296 #define VR_TIMEOUT 1000
297
298 static int vr_add_rxbuf __P((struct vr_softc *, int));
299
300 static void vr_rxeof __P((struct vr_softc *));
301 static void vr_rxeoc __P((struct vr_softc *));
302 static void vr_txeof __P((struct vr_softc *));
303 static int vr_intr __P((void *));
304 static void vr_start __P((struct ifnet *));
305 static int vr_ioctl __P((struct ifnet *, u_long, caddr_t));
306 static int vr_init __P((struct vr_softc *));
307 static void vr_stop __P((struct vr_softc *, int));
308 static void vr_rxdrain __P((struct vr_softc *));
309 static void vr_watchdog __P((struct ifnet *));
310 static void vr_tick __P((void *));
311
312 static int vr_ifmedia_upd __P((struct ifnet *));
313 static void vr_ifmedia_sts __P((struct ifnet *, struct ifmediareq *));
314
315 static void vr_mii_sync __P((struct vr_softc *));
316 static void vr_mii_send __P((struct vr_softc *, u_int32_t, int));
317 static int vr_mii_readreg __P((struct device *, int, int));
318 static void vr_mii_writereg __P((struct device *, int, int, int));
319 static void vr_mii_statchg __P((struct device *));
320
321 static u_int8_t vr_calchash __P((u_int8_t *));
322 static void vr_setmulti __P((struct vr_softc *));
323 static void vr_reset __P((struct vr_softc *));
324
325 int vr_copy_small = 0;
326
327 #define VR_SETBIT(sc, reg, x) \
328 CSR_WRITE_1(sc, reg, \
329 CSR_READ_1(sc, reg) | x)
330
331 #define VR_CLRBIT(sc, reg, x) \
332 CSR_WRITE_1(sc, reg, \
333 CSR_READ_1(sc, reg) & ~x)
334
335 #define VR_SETBIT16(sc, reg, x) \
336 CSR_WRITE_2(sc, reg, \
337 CSR_READ_2(sc, reg) | x)
338
339 #define VR_CLRBIT16(sc, reg, x) \
340 CSR_WRITE_2(sc, reg, \
341 CSR_READ_2(sc, reg) & ~x)
342
343 #define VR_SETBIT32(sc, reg, x) \
344 CSR_WRITE_4(sc, reg, \
345 CSR_READ_4(sc, reg) | x)
346
347 #define VR_CLRBIT32(sc, reg, x) \
348 CSR_WRITE_4(sc, reg, \
349 CSR_READ_4(sc, reg) & ~x)
350
351 #define SIO_SET(x) \
352 CSR_WRITE_1(sc, VR_MIICMD, \
353 CSR_READ_1(sc, VR_MIICMD) | x)
354
355 #define SIO_CLR(x) \
356 CSR_WRITE_1(sc, VR_MIICMD, \
357 CSR_READ_1(sc, VR_MIICMD) & ~x)
358
359 /*
360 * Sync the PHYs by setting data bit and strobing the clock 32 times.
361 */
362 static void
363 vr_mii_sync(sc)
364 struct vr_softc *sc;
365 {
366 int i;
367
368 SIO_SET(VR_MIICMD_DIR|VR_MIICMD_DATAOUT);
369
370 for (i = 0; i < 32; i++) {
371 SIO_SET(VR_MIICMD_CLK);
372 DELAY(1);
373 SIO_CLR(VR_MIICMD_CLK);
374 DELAY(1);
375 }
376 }
377
378 /*
379 * Clock a series of bits through the MII.
380 */
381 static void
382 vr_mii_send(sc, bits, cnt)
383 struct vr_softc *sc;
384 u_int32_t bits;
385 int cnt;
386 {
387 int i;
388
389 SIO_CLR(VR_MIICMD_CLK);
390
391 for (i = (0x1 << (cnt - 1)); i; i >>= 1) {
392 if (bits & i) {
393 SIO_SET(VR_MIICMD_DATAOUT);
394 } else {
395 SIO_CLR(VR_MIICMD_DATAOUT);
396 }
397 DELAY(1);
398 SIO_CLR(VR_MIICMD_CLK);
399 DELAY(1);
400 SIO_SET(VR_MIICMD_CLK);
401 }
402 }
403
404 /*
405 * Read an PHY register through the MII.
406 */
407 static int
408 vr_mii_readreg(self, phy, reg)
409 struct device *self;
410 int phy, reg;
411 {
412 struct vr_softc *sc = (struct vr_softc *)self;
413 int i, ack, val = 0;
414
415 CSR_WRITE_1(sc, VR_MIICMD, 0);
416 VR_SETBIT(sc, VR_MIICMD, VR_MIICMD_DIRECTPGM);
417
418 /*
419 * Turn on data xmit.
420 */
421 SIO_SET(VR_MIICMD_DIR);
422
423 vr_mii_sync(sc);
424
425 /*
426 * Send command/address info.
427 */
428 vr_mii_send(sc, MII_COMMAND_START, 2);
429 vr_mii_send(sc, MII_COMMAND_READ, 2);
430 vr_mii_send(sc, phy, 5);
431 vr_mii_send(sc, reg, 5);
432
433 /* Idle bit */
434 SIO_CLR((VR_MIICMD_CLK|VR_MIICMD_DATAOUT));
435 DELAY(1);
436 SIO_SET(VR_MIICMD_CLK);
437 DELAY(1);
438
439 /* Turn off xmit. */
440 SIO_CLR(VR_MIICMD_DIR);
441
442 /* Check for ack */
443 SIO_CLR(VR_MIICMD_CLK);
444 DELAY(1);
445 SIO_SET(VR_MIICMD_CLK);
446 DELAY(1);
447 ack = CSR_READ_4(sc, VR_MIICMD) & VR_MIICMD_DATAIN;
448
449 /*
450 * Now try reading data bits. If the ack failed, we still
451 * need to clock through 16 cycles to keep the PHY(s) in sync.
452 */
453 if (ack) {
454 for (i = 0; i < 16; i++) {
455 SIO_CLR(VR_MIICMD_CLK);
456 DELAY(1);
457 SIO_SET(VR_MIICMD_CLK);
458 DELAY(1);
459 }
460 goto fail;
461 }
462
463 for (i = 0x8000; i; i >>= 1) {
464 SIO_CLR(VR_MIICMD_CLK);
465 DELAY(1);
466 if (!ack) {
467 if (CSR_READ_4(sc, VR_MIICMD) & VR_MIICMD_DATAIN)
468 val |= i;
469 DELAY(1);
470 }
471 SIO_SET(VR_MIICMD_CLK);
472 DELAY(1);
473 }
474
475 fail:
476
477 SIO_CLR(VR_MIICMD_CLK);
478 DELAY(1);
479 SIO_SET(VR_MIICMD_CLK);
480 DELAY(1);
481
482 return (val);
483 }
484
485 /*
486 * Write to a PHY register through the MII.
487 */
488 static void
489 vr_mii_writereg(self, phy, reg, val)
490 struct device *self;
491 int phy, reg, val;
492 {
493 struct vr_softc *sc = (struct vr_softc *)self;
494
495 CSR_WRITE_1(sc, VR_MIICMD, 0);
496 VR_SETBIT(sc, VR_MIICMD, VR_MIICMD_DIRECTPGM);
497
498 /*
499 * Turn on data output.
500 */
501 SIO_SET(VR_MIICMD_DIR);
502
503 vr_mii_sync(sc);
504
505 vr_mii_send(sc, MII_COMMAND_START, 2);
506 vr_mii_send(sc, MII_COMMAND_WRITE, 2);
507 vr_mii_send(sc, phy, 5);
508 vr_mii_send(sc, reg, 5);
509 vr_mii_send(sc, MII_COMMAND_ACK, 2);
510 vr_mii_send(sc, val, 16);
511
512 /* Idle bit. */
513 SIO_SET(VR_MIICMD_CLK);
514 DELAY(1);
515 SIO_CLR(VR_MIICMD_CLK);
516 DELAY(1);
517
518 /*
519 * Turn off xmit.
520 */
521 SIO_CLR(VR_MIICMD_DIR);
522 }
523
524 static void
525 vr_mii_statchg(self)
526 struct device *self;
527 {
528 struct vr_softc *sc = (struct vr_softc *)self;
529
530 /*
531 * In order to fiddle with the 'full-duplex' bit in the netconfig
532 * register, we first have to put the transmit and/or receive logic
533 * in the idle state.
534 */
535 VR_CLRBIT16(sc, VR_COMMAND, (VR_CMD_TX_ON|VR_CMD_RX_ON));
536
537 if (sc->vr_mii.mii_media_active & IFM_FDX)
538 VR_SETBIT16(sc, VR_COMMAND, VR_CMD_FULLDUPLEX);
539 else
540 VR_CLRBIT16(sc, VR_COMMAND, VR_CMD_FULLDUPLEX);
541
542 if (sc->vr_ec.ec_if.if_flags & IFF_RUNNING)
543 VR_SETBIT16(sc, VR_COMMAND, VR_CMD_TX_ON|VR_CMD_RX_ON);
544
545 /* XXX Update ifp->if_baudrate */
546 }
547
548 /*
549 * Calculate CRC of a multicast group address, return the lower 6 bits.
550 */
551 static u_int8_t
552 vr_calchash(addr)
553 u_int8_t *addr;
554 {
555 u_int32_t crc, carry;
556 int i, j;
557 u_int8_t c;
558
559 /* Compute CRC for the address value. */
560 crc = 0xFFFFFFFF; /* initial value */
561
562 for (i = 0; i < 6; i++) {
563 c = *(addr + i);
564 for (j = 0; j < 8; j++) {
565 carry = ((crc & 0x80000000) ? 1 : 0) ^ (c & 0x01);
566 crc <<= 1;
567 c >>= 1;
568 if (carry)
569 crc = (crc ^ 0x04c11db6) | carry;
570 }
571 }
572
573 /* return the filter bit position */
574 return ((crc >> 26) & 0x0000003F);
575 }
576
577 /*
578 * Program the 64-bit multicast hash filter.
579 */
580 static void
581 vr_setmulti(sc)
582 struct vr_softc *sc;
583 {
584 struct ifnet *ifp;
585 int h = 0;
586 u_int32_t hashes[2] = { 0, 0 };
587 struct ether_multistep step;
588 struct ether_multi *enm;
589 int mcnt = 0;
590 u_int8_t rxfilt;
591
592 ifp = &sc->vr_ec.ec_if;
593
594 rxfilt = CSR_READ_1(sc, VR_RXCFG);
595
596 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
597 rxfilt |= VR_RXCFG_RX_MULTI;
598 CSR_WRITE_1(sc, VR_RXCFG, rxfilt);
599 CSR_WRITE_4(sc, VR_MAR0, 0xFFFFFFFF);
600 CSR_WRITE_4(sc, VR_MAR1, 0xFFFFFFFF);
601 return;
602 }
603
604 /* first, zot all the existing hash bits */
605 CSR_WRITE_4(sc, VR_MAR0, 0);
606 CSR_WRITE_4(sc, VR_MAR1, 0);
607
608 /* now program new ones */
609 ETHER_FIRST_MULTI(step, &sc->vr_ec, enm);
610 while (enm != NULL) {
611 if (memcmp(enm->enm_addrlo, enm->enm_addrhi, 6) != 0)
612 continue;
613
614 h = vr_calchash(enm->enm_addrlo);
615
616 if (h < 32)
617 hashes[0] |= (1 << h);
618 else
619 hashes[1] |= (1 << (h - 32));
620 ETHER_NEXT_MULTI(step, enm);
621 mcnt++;
622 }
623
624 if (mcnt)
625 rxfilt |= VR_RXCFG_RX_MULTI;
626 else
627 rxfilt &= ~VR_RXCFG_RX_MULTI;
628
629 CSR_WRITE_4(sc, VR_MAR0, hashes[0]);
630 CSR_WRITE_4(sc, VR_MAR1, hashes[1]);
631 CSR_WRITE_1(sc, VR_RXCFG, rxfilt);
632 }
633
634 static void
635 vr_reset(sc)
636 struct vr_softc *sc;
637 {
638 int i;
639
640 VR_SETBIT16(sc, VR_COMMAND, VR_CMD_RESET);
641
642 for (i = 0; i < VR_TIMEOUT; i++) {
643 DELAY(10);
644 if (!(CSR_READ_2(sc, VR_COMMAND) & VR_CMD_RESET))
645 break;
646 }
647 if (i == VR_TIMEOUT)
648 printf("%s: reset never completed!\n",
649 sc->vr_dev.dv_xname);
650
651 /* Wait a little while for the chip to get its brains in order. */
652 DELAY(1000);
653 }
654
655 /*
656 * Initialize an RX descriptor and attach an MBUF cluster.
657 * Note: the length fields are only 11 bits wide, which means the
658 * largest size we can specify is 2047. This is important because
659 * MCLBYTES is 2048, so we have to subtract one otherwise we'll
660 * overflow the field and make a mess.
661 */
662 static int
663 vr_add_rxbuf(sc, i)
664 struct vr_softc *sc;
665 int i;
666 {
667 struct vr_descsoft *ds = VR_DSRX(sc, i);
668 struct mbuf *m_new;
669 int error;
670
671 MGETHDR(m_new, M_DONTWAIT, MT_DATA);
672 if (m_new == NULL)
673 return (ENOBUFS);
674
675 MCLGET(m_new, M_DONTWAIT);
676 if ((m_new->m_flags & M_EXT) == 0) {
677 m_freem(m_new);
678 return (ENOBUFS);
679 }
680
681 if (ds->ds_mbuf != NULL)
682 bus_dmamap_unload(sc->vr_dmat, ds->ds_dmamap);
683
684 ds->ds_mbuf = m_new;
685
686 error = bus_dmamap_load(sc->vr_dmat, ds->ds_dmamap,
687 m_new->m_ext.ext_buf, m_new->m_ext.ext_size, NULL, BUS_DMA_NOWAIT);
688 if (error) {
689 printf("%s: unable to load rx DMA map %d, error = %d\n",
690 sc->vr_dev.dv_xname, i, error);
691 panic("vr_add_rxbuf"); /* XXX */
692 }
693
694 bus_dmamap_sync(sc->vr_dmat, ds->ds_dmamap, 0,
695 ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
696
697 VR_INIT_RXDESC(sc, i);
698
699 return (0);
700 }
701
702 /*
703 * A frame has been uploaded: pass the resulting mbuf chain up to
704 * the higher level protocols.
705 */
706 static void
707 vr_rxeof(sc)
708 struct vr_softc *sc;
709 {
710 struct ether_header *eh;
711 struct mbuf *m;
712 struct ifnet *ifp;
713 struct vr_desc *d;
714 struct vr_descsoft *ds;
715 int i, total_len;
716 u_int32_t rxstat;
717
718 ifp = &sc->vr_ec.ec_if;
719
720 for (i = sc->vr_rxptr;; i = VR_NEXTRX(i)) {
721 d = VR_CDRX(sc, i);
722 ds = VR_DSRX(sc, i);
723
724 VR_CDRXSYNC(sc, i, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
725
726 rxstat = pcitoh(d->vr_status);
727
728 if (rxstat & VR_RXSTAT_OWN) {
729 /*
730 * We have processed all of the receive buffers.
731 */
732 break;
733 }
734
735 /*
736 * If an error occurs, update stats, clear the
737 * status word and leave the mbuf cluster in place:
738 * it should simply get re-used next time this descriptor
739 * comes up in the ring.
740 */
741 if (rxstat & VR_RXSTAT_RXERR) {
742 const char *errstr;
743
744 ifp->if_ierrors++;
745 switch (rxstat & 0x000000FF) {
746 case VR_RXSTAT_CRCERR:
747 errstr = "crc error";
748 break;
749 case VR_RXSTAT_FRAMEALIGNERR:
750 errstr = "frame alignment error";
751 break;
752 case VR_RXSTAT_FIFOOFLOW:
753 errstr = "FIFO overflow";
754 break;
755 case VR_RXSTAT_GIANT:
756 errstr = "received giant packet";
757 break;
758 case VR_RXSTAT_RUNT:
759 errstr = "received runt packet";
760 break;
761 case VR_RXSTAT_BUSERR:
762 errstr = "system bus error";
763 break;
764 case VR_RXSTAT_BUFFERR:
765 errstr = "rx buffer error";
766 break;
767 default:
768 errstr = "unknown rx error";
769 break;
770 }
771 printf("%s: receive error: %s\n", sc->vr_dev.dv_xname,
772 errstr);
773
774 VR_INIT_RXDESC(sc, i);
775
776 continue;
777 }
778
779 bus_dmamap_sync(sc->vr_dmat, ds->ds_dmamap, 0,
780 ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
781
782 /* No errors; receive the packet. */
783 total_len = VR_RXBYTES(pcitoh(d->vr_status));
784
785 /*
786 * XXX The VIA Rhine chip includes the CRC with every
787 * received frame, and there's no way to turn this
788 * behavior off (at least, I can't find anything in
789 * the manual that explains how to do it) so we have
790 * to trim off the CRC manually.
791 */
792 total_len -= ETHER_CRC_LEN;
793
794 #ifdef __NO_STRICT_ALIGNMENT
795 /*
796 * If the packet is small enough to fit in a
797 * single header mbuf, allocate one and copy
798 * the data into it. This greatly reduces
799 * memory consumption when we receive lots
800 * of small packets.
801 *
802 * Otherwise, we add a new buffer to the receive
803 * chain. If this fails, we drop the packet and
804 * recycle the old buffer.
805 */
806 if (vr_copy_small != 0 && total_len <= MHLEN) {
807 MGETHDR(m, M_DONTWAIT, MT_DATA);
808 if (m == NULL)
809 goto dropit;
810 memcpy(mtod(m, caddr_t),
811 mtod(ds->ds_mbuf, caddr_t), total_len);
812 VR_INIT_RXDESC(sc, i);
813 bus_dmamap_sync(sc->vr_dmat, ds->ds_dmamap, 0,
814 ds->ds_dmamap->dm_mapsize,
815 BUS_DMASYNC_PREREAD);
816 } else {
817 m = ds->ds_mbuf;
818 if (vr_add_rxbuf(sc, i) == ENOBUFS) {
819 dropit:
820 ifp->if_ierrors++;
821 VR_INIT_RXDESC(sc, i);
822 bus_dmamap_sync(sc->vr_dmat,
823 ds->ds_dmamap, 0,
824 ds->ds_dmamap->dm_mapsize,
825 BUS_DMASYNC_PREREAD);
826 continue;
827 }
828 }
829 #else
830 /*
831 * The Rhine's packet buffers must be 4-byte aligned.
832 * But this means that the data after the Ethernet header
833 * is misaligned. We must allocate a new buffer and
834 * copy the data, shifted forward 2 bytes.
835 */
836 MGETHDR(m, M_DONTWAIT, MT_DATA);
837 if (m == NULL) {
838 dropit:
839 ifp->if_ierrors++;
840 VR_INIT_RXDESC(sc, i);
841 bus_dmamap_sync(sc->vr_dmat, ds->ds_dmamap, 0,
842 ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
843 continue;
844 }
845 if (total_len > (MHLEN - 2)) {
846 MCLGET(m, M_DONTWAIT);
847 if ((m->m_flags & M_EXT) == 0) {
848 m_freem(m);
849 goto dropit;
850 }
851 }
852 m->m_data += 2;
853
854 /*
855 * Note that we use clusters for incoming frames, so the
856 * buffer is virtually contiguous.
857 */
858 memcpy(mtod(m, caddr_t), mtod(ds->ds_mbuf, caddr_t),
859 total_len);
860
861 /* Allow the recieve descriptor to continue using its mbuf. */
862 VR_INIT_RXDESC(sc, i);
863 bus_dmamap_sync(sc->vr_dmat, ds->ds_dmamap, 0,
864 ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
865 #endif /* __NO_STRICT_ALIGNMENT */
866
867 ifp->if_ipackets++;
868 eh = mtod(m, struct ether_header *);
869 m->m_pkthdr.rcvif = ifp;
870 m->m_pkthdr.len = m->m_len = total_len;
871 #if NBPFILTER > 0
872 /*
873 * Handle BPF listeners. Let the BPF user see the packet, but
874 * don't pass it up to the ether_input() layer unless it's
875 * a broadcast packet, multicast packet, matches our ethernet
876 * address or the interface is in promiscuous mode.
877 */
878 if (ifp->if_bpf) {
879 bpf_mtap(ifp->if_bpf, m);
880 if ((ifp->if_flags & IFF_PROMISC) != 0 &&
881 ETHER_IS_MULTICAST(eh->ether_dhost) == 0 &&
882 memcmp(eh->ether_dhost, LLADDR(ifp->if_sadl),
883 ETHER_ADDR_LEN) != 0) {
884 m_freem(m);
885 continue;
886 }
887 }
888 #endif
889 /* Pass it on. */
890 (*ifp->if_input)(ifp, m);
891 }
892
893 /* Update the receive pointer. */
894 sc->vr_rxptr = i;
895 }
896
897 void
898 vr_rxeoc(sc)
899 struct vr_softc *sc;
900 {
901
902 vr_rxeof(sc);
903 VR_CLRBIT16(sc, VR_COMMAND, VR_CMD_RX_ON);
904 CSR_WRITE_4(sc, VR_RXADDR, VR_CDRXADDR(sc, sc->vr_rxptr));
905 VR_SETBIT16(sc, VR_COMMAND, VR_CMD_RX_ON);
906 VR_SETBIT16(sc, VR_COMMAND, VR_CMD_RX_GO);
907 }
908
909 /*
910 * A frame was downloaded to the chip. It's safe for us to clean up
911 * the list buffers.
912 */
913 static void
914 vr_txeof(sc)
915 struct vr_softc *sc;
916 {
917 struct ifnet *ifp = &sc->vr_ec.ec_if;
918 struct vr_desc *d;
919 struct vr_descsoft *ds;
920 u_int32_t txstat;
921 int i;
922
923 ifp->if_flags &= ~IFF_OACTIVE;
924
925 /*
926 * Go through our tx list and free mbufs for those
927 * frames that have been transmitted.
928 */
929 for (i = sc->vr_txdirty; sc->vr_txpending != 0;
930 i = VR_NEXTTX(i), sc->vr_txpending--) {
931 d = VR_CDTX(sc, i);
932 ds = VR_DSTX(sc, i);
933
934 VR_CDTXSYNC(sc, i, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
935
936 txstat = pcitoh(d->vr_status);
937 if (txstat & VR_TXSTAT_OWN)
938 break;
939
940 bus_dmamap_sync(sc->vr_dmat, ds->ds_dmamap,
941 0, ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
942 bus_dmamap_unload(sc->vr_dmat, ds->ds_dmamap);
943 m_freem(ds->ds_mbuf);
944 ds->ds_mbuf = NULL;
945
946 if (txstat & VR_TXSTAT_ERRSUM) {
947 ifp->if_oerrors++;
948 if (txstat & VR_TXSTAT_DEFER)
949 ifp->if_collisions++;
950 if (txstat & VR_TXSTAT_LATECOLL)
951 ifp->if_collisions++;
952 }
953
954 ifp->if_collisions += (txstat & VR_TXSTAT_COLLCNT) >> 3;
955 ifp->if_opackets++;
956 }
957
958 /* Update the dirty transmit buffer pointer. */
959 sc->vr_txdirty = i;
960
961 /*
962 * Cancel the watchdog timer if there are no pending
963 * transmissions.
964 */
965 if (sc->vr_txpending == 0)
966 ifp->if_timer = 0;
967 }
968
969 static int
970 vr_intr(arg)
971 void *arg;
972 {
973 struct vr_softc *sc;
974 struct ifnet *ifp;
975 u_int16_t status;
976 int handled = 0, dotx = 0;
977
978 sc = arg;
979 ifp = &sc->vr_ec.ec_if;
980
981 /* Suppress unwanted interrupts. */
982 if ((ifp->if_flags & IFF_UP) == 0) {
983 vr_stop(sc, 1);
984 return (0);
985 }
986
987 /* Disable interrupts. */
988 CSR_WRITE_2(sc, VR_IMR, 0x0000);
989
990 for (;;) {
991 status = CSR_READ_2(sc, VR_ISR);
992 if (status)
993 CSR_WRITE_2(sc, VR_ISR, status);
994
995 if ((status & VR_INTRS) == 0)
996 break;
997
998 handled = 1;
999
1000 if (status & VR_ISR_RX_OK)
1001 vr_rxeof(sc);
1002
1003 if (status &
1004 (VR_ISR_RX_ERR | VR_ISR_RX_NOBUF | VR_ISR_RX_OFLOW |
1005 VR_ISR_RX_DROPPED))
1006 vr_rxeoc(sc);
1007
1008 if (status & VR_ISR_TX_OK) {
1009 dotx = 1;
1010 vr_txeof(sc);
1011 }
1012
1013 if (status & (VR_ISR_TX_UNDERRUN | VR_ISR_TX_ABRT)) {
1014 if (status & VR_ISR_TX_UNDERRUN)
1015 printf("%s: transmit underrun\n",
1016 sc->vr_dev.dv_xname);
1017 if (status & VR_ISR_TX_ABRT)
1018 printf("%s: transmit aborted\n",
1019 sc->vr_dev.dv_xname);
1020 ifp->if_oerrors++;
1021 dotx = 1;
1022 vr_txeof(sc);
1023 if (sc->vr_txpending) {
1024 VR_SETBIT16(sc, VR_COMMAND, VR_CMD_TX_ON);
1025 VR_SETBIT16(sc, VR_COMMAND, VR_CMD_TX_GO);
1026 }
1027 }
1028
1029 if (status & VR_ISR_BUSERR) {
1030 printf("%s: PCI bus error\n", sc->vr_dev.dv_xname);
1031 /* vr_init() calls vr_start() */
1032 dotx = 0;
1033 (void) vr_init(sc);
1034 }
1035 }
1036
1037 /* Re-enable interrupts. */
1038 CSR_WRITE_2(sc, VR_IMR, VR_INTRS);
1039
1040 if (dotx)
1041 vr_start(ifp);
1042
1043 return (handled);
1044 }
1045
1046 /*
1047 * Main transmit routine. To avoid having to do mbuf copies, we put pointers
1048 * to the mbuf data regions directly in the transmit lists. We also save a
1049 * copy of the pointers since the transmit list fragment pointers are
1050 * physical addresses.
1051 */
1052 static void
1053 vr_start(ifp)
1054 struct ifnet *ifp;
1055 {
1056 struct vr_softc *sc = ifp->if_softc;
1057 struct mbuf *m0, *m;
1058 struct vr_desc *d;
1059 struct vr_descsoft *ds;
1060 int error, firsttx, nexttx, opending;
1061
1062 /*
1063 * Remember the previous txpending and the first transmit
1064 * descriptor we use.
1065 */
1066 opending = sc->vr_txpending;
1067 firsttx = VR_NEXTTX(sc->vr_txlast);
1068
1069 /*
1070 * Loop through the send queue, setting up transmit descriptors
1071 * until we drain the queue, or use up all available transmit
1072 * descriptors.
1073 */
1074 while (sc->vr_txpending < VR_NTXDESC) {
1075 /*
1076 * Grab a packet off the queue.
1077 */
1078 IF_DEQUEUE(&ifp->if_snd, m0);
1079 if (m0 == NULL)
1080 break;
1081
1082 /*
1083 * Get the next available transmit descriptor.
1084 */
1085 nexttx = VR_NEXTTX(sc->vr_txlast);
1086 d = VR_CDTX(sc, nexttx);
1087 ds = VR_DSTX(sc, nexttx);
1088
1089 /*
1090 * Load the DMA map. If this fails, the packet didn't
1091 * fit in one DMA segment, and we need to copy. Note,
1092 * the packet must also be aligned.
1093 */
1094 if ((mtod(m0, bus_addr_t) & 3) != 0 ||
1095 bus_dmamap_load_mbuf(sc->vr_dmat, ds->ds_dmamap, m0,
1096 BUS_DMA_NOWAIT) != 0) {
1097 MGETHDR(m, M_DONTWAIT, MT_DATA);
1098 if (m == NULL) {
1099 printf("%s: unable to allocate Tx mbuf\n",
1100 sc->vr_dev.dv_xname);
1101 IF_PREPEND(&ifp->if_snd, m0);
1102 break;
1103 }
1104 if (m0->m_pkthdr.len > MHLEN) {
1105 MCLGET(m, M_DONTWAIT);
1106 if ((m->m_flags & M_EXT) == 0) {
1107 printf("%s: unable to allocate Tx "
1108 "cluster\n", sc->vr_dev.dv_xname);
1109 m_freem(m);
1110 IF_PREPEND(&ifp->if_snd, m0);
1111 break;
1112 }
1113 }
1114 m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t));
1115 m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
1116 m_freem(m0);
1117 m0 = m;
1118 error = bus_dmamap_load_mbuf(sc->vr_dmat,
1119 ds->ds_dmamap, m0, BUS_DMA_NOWAIT);
1120 if (error) {
1121 printf("%s: unable to load Tx buffer, "
1122 "error = %d\n", sc->vr_dev.dv_xname, error);
1123 IF_PREPEND(&ifp->if_snd, m0);
1124 break;
1125 }
1126 }
1127
1128 /* Sync the DMA map. */
1129 bus_dmamap_sync(sc->vr_dmat, ds->ds_dmamap, 0,
1130 ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_PREWRITE);
1131
1132 /*
1133 * Store a pointer to the packet so we can free it later.
1134 */
1135 ds->ds_mbuf = m0;
1136
1137 #if NBPFILTER > 0
1138 /*
1139 * If there's a BPF listener, bounce a copy of this frame
1140 * to him.
1141 */
1142 if (ifp->if_bpf)
1143 bpf_mtap(ifp->if_bpf, m0);
1144 #endif
1145
1146 /*
1147 * Fill in the transmit descriptor. The Rhine
1148 * doesn't auto-pad, so we have to do this ourselves.
1149 */
1150 d->vr_data = htopci(ds->ds_dmamap->dm_segs[0].ds_addr);
1151 d->vr_ctl = htopci(m0->m_pkthdr.len < VR_MIN_FRAMELEN ?
1152 VR_MIN_FRAMELEN : m0->m_pkthdr.len);
1153 d->vr_ctl |=
1154 htopci(VR_TXCTL_TLINK|VR_TXCTL_FIRSTFRAG|VR_TXCTL_LASTFRAG);
1155
1156 /*
1157 * If this is the first descriptor we're enqueuing,
1158 * don't give it to the Rhine yet. That could cause
1159 * a race condition. We'll do it below.
1160 */
1161 if (nexttx == firsttx)
1162 d->vr_status = 0;
1163 else
1164 d->vr_status = htopci(VR_TXSTAT_OWN);
1165
1166 VR_CDTXSYNC(sc, nexttx,
1167 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1168
1169 /* Advance the tx pointer. */
1170 sc->vr_txpending++;
1171 sc->vr_txlast = nexttx;
1172 }
1173
1174 if (sc->vr_txpending == VR_NTXDESC) {
1175 /* No more slots left; notify upper layer. */
1176 ifp->if_flags |= IFF_OACTIVE;
1177 }
1178
1179 if (sc->vr_txpending != opending) {
1180 /*
1181 * We enqueued packets. If the transmitter was idle,
1182 * reset the txdirty pointer.
1183 */
1184 if (opending == 0)
1185 sc->vr_txdirty = firsttx;
1186
1187 /*
1188 * Cause a transmit interrupt to happen on the
1189 * last packet we enqueued.
1190 */
1191 VR_CDTX(sc, sc->vr_txlast)->vr_ctl |= htopci(VR_TXCTL_FINT);
1192 VR_CDTXSYNC(sc, sc->vr_txlast,
1193 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1194
1195 /*
1196 * The entire packet chain is set up. Give the
1197 * first descriptor to the Rhine now.
1198 */
1199 VR_CDTX(sc, firsttx)->vr_status = htopci(VR_TXSTAT_OWN);
1200 VR_CDTXSYNC(sc, firsttx,
1201 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1202
1203 /* Start the transmitter. */
1204 VR_SETBIT16(sc, VR_COMMAND, VR_CMD_TX_ON|VR_CMD_TX_GO);
1205
1206 /* Set the watchdog timer in case the chip flakes out. */
1207 ifp->if_timer = 5;
1208 }
1209 }
1210
1211 /*
1212 * Initialize the interface. Must be called at splnet.
1213 */
1214 static int
1215 vr_init(sc)
1216 struct vr_softc *sc;
1217 {
1218 struct ifnet *ifp = &sc->vr_ec.ec_if;
1219 struct vr_desc *d;
1220 struct vr_descsoft *ds;
1221 int i, error = 0;
1222
1223 /* Cancel pending I/O. */
1224 vr_stop(sc, 0);
1225
1226 /* Reset the Rhine to a known state. */
1227 vr_reset(sc);
1228
1229 VR_CLRBIT(sc, VR_RXCFG, VR_RXCFG_RX_THRESH);
1230 VR_SETBIT(sc, VR_RXCFG, VR_RXTHRESH_STORENFWD);
1231
1232 VR_CLRBIT(sc, VR_TXCFG, VR_TXCFG_TX_THRESH);
1233 VR_SETBIT(sc, VR_TXCFG, VR_TXTHRESH_STORENFWD);
1234
1235 /*
1236 * Initialize the transmit desciptor ring. txlast is initialized
1237 * to the end of the list so that it will wrap around to the first
1238 * descriptor when the first packet is transmitted.
1239 */
1240 for (i = 0; i < VR_NTXDESC; i++) {
1241 d = VR_CDTX(sc, i);
1242 memset(d, 0, sizeof(struct vr_desc));
1243 d->vr_next = htopci(VR_CDTXADDR(sc, VR_NEXTTX(i)));
1244 VR_CDTXSYNC(sc, i, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1245 }
1246 sc->vr_txpending = 0;
1247 sc->vr_txdirty = 0;
1248 sc->vr_txlast = VR_NTXDESC - 1;
1249
1250 /*
1251 * Initialize the receive descriptor ring.
1252 */
1253 for (i = 0; i < VR_NRXDESC; i++) {
1254 ds = VR_DSRX(sc, i);
1255 if (ds->ds_mbuf == NULL) {
1256 if ((error = vr_add_rxbuf(sc, i)) != 0) {
1257 printf("%s: unable to allocate or map rx "
1258 "buffer %d, error = %d\n",
1259 sc->vr_dev.dv_xname, i, error);
1260 /*
1261 * XXX Should attempt to run with fewer receive
1262 * XXX buffers instead of just failing.
1263 */
1264 vr_rxdrain(sc);
1265 goto out;
1266 }
1267 }
1268 }
1269 sc->vr_rxptr = 0;
1270
1271 /* If we want promiscuous mode, set the allframes bit. */
1272 if (ifp->if_flags & IFF_PROMISC)
1273 VR_SETBIT(sc, VR_RXCFG, VR_RXCFG_RX_PROMISC);
1274 else
1275 VR_CLRBIT(sc, VR_RXCFG, VR_RXCFG_RX_PROMISC);
1276
1277 /* Set capture broadcast bit to capture broadcast frames. */
1278 if (ifp->if_flags & IFF_BROADCAST)
1279 VR_SETBIT(sc, VR_RXCFG, VR_RXCFG_RX_BROAD);
1280 else
1281 VR_CLRBIT(sc, VR_RXCFG, VR_RXCFG_RX_BROAD);
1282
1283 /* Program the multicast filter, if necessary. */
1284 vr_setmulti(sc);
1285
1286 /* Give the transmit and recieve rings to the Rhine. */
1287 CSR_WRITE_4(sc, VR_RXADDR, VR_CDRXADDR(sc, sc->vr_rxptr));
1288 CSR_WRITE_4(sc, VR_TXADDR, VR_CDTXADDR(sc, VR_NEXTTX(sc->vr_txlast)));
1289
1290 /* Set current media. */
1291 mii_mediachg(&sc->vr_mii);
1292
1293 /* Enable receiver and transmitter. */
1294 CSR_WRITE_2(sc, VR_COMMAND, VR_CMD_TX_NOPOLL|VR_CMD_START|
1295 VR_CMD_TX_ON|VR_CMD_RX_ON|
1296 VR_CMD_RX_GO);
1297
1298 /* Enable interrupts. */
1299 CSR_WRITE_2(sc, VR_ISR, 0xFFFF);
1300 CSR_WRITE_2(sc, VR_IMR, VR_INTRS);
1301
1302 ifp->if_flags |= IFF_RUNNING;
1303 ifp->if_flags &= ~IFF_OACTIVE;
1304
1305 /* Start one second timer. */
1306 timeout(vr_tick, sc, hz);
1307
1308 /* Attempt to start output on the interface. */
1309 vr_start(ifp);
1310
1311 out:
1312 if (error)
1313 printf("%s: interface not running\n", sc->vr_dev.dv_xname);
1314 return (error);
1315 }
1316
1317 /*
1318 * Set media options.
1319 */
1320 static int
1321 vr_ifmedia_upd(ifp)
1322 struct ifnet *ifp;
1323 {
1324 struct vr_softc *sc = ifp->if_softc;
1325
1326 if (ifp->if_flags & IFF_UP)
1327 mii_mediachg(&sc->vr_mii);
1328 return (0);
1329 }
1330
1331 /*
1332 * Report current media status.
1333 */
1334 static void
1335 vr_ifmedia_sts(ifp, ifmr)
1336 struct ifnet *ifp;
1337 struct ifmediareq *ifmr;
1338 {
1339 struct vr_softc *sc = ifp->if_softc;
1340
1341 mii_pollstat(&sc->vr_mii);
1342 ifmr->ifm_status = sc->vr_mii.mii_media_status;
1343 ifmr->ifm_active = sc->vr_mii.mii_media_active;
1344 }
1345
1346 static int
1347 vr_ioctl(ifp, command, data)
1348 struct ifnet *ifp;
1349 u_long command;
1350 caddr_t data;
1351 {
1352 struct vr_softc *sc = ifp->if_softc;
1353 struct ifreq *ifr = (struct ifreq *)data;
1354 struct ifaddr *ifa = (struct ifaddr *)data;
1355 int s, error = 0;
1356
1357 s = splnet();
1358
1359 switch (command) {
1360 case SIOCSIFADDR:
1361 ifp->if_flags |= IFF_UP;
1362
1363 switch (ifa->ifa_addr->sa_family) {
1364 #ifdef INET
1365 case AF_INET:
1366 if ((error = vr_init(sc)) != 0)
1367 break;
1368 arp_ifinit(ifp, ifa);
1369 break;
1370 #endif /* INET */
1371 default:
1372 error = vr_init(sc);
1373 break;
1374 }
1375 break;
1376
1377 case SIOCGIFADDR:
1378 bcopy((caddr_t) sc->vr_enaddr,
1379 (caddr_t) ((struct sockaddr *)&ifr->ifr_data)->sa_data,
1380 ETHER_ADDR_LEN);
1381 break;
1382
1383 case SIOCSIFMTU:
1384 if (ifr->ifr_mtu > ETHERMTU)
1385 error = EINVAL;
1386 else
1387 ifp->if_mtu = ifr->ifr_mtu;
1388 break;
1389
1390 case SIOCSIFFLAGS:
1391 if ((ifp->if_flags & IFF_UP) == 0 &&
1392 (ifp->if_flags & IFF_RUNNING) != 0) {
1393 /*
1394 * If interface is marked down and it is running, then
1395 * stop it.
1396 */
1397 vr_stop(sc, 1);
1398 } else if ((ifp->if_flags & IFF_UP) != 0 &&
1399 (ifp->if_flags & IFF_RUNNING) == 0) {
1400 /*
1401 * If interface is marked up and it is stopped, then
1402 * start it.
1403 */
1404 error = vr_init(sc);
1405 } else if ((ifp->if_flags & IFF_UP) != 0) {
1406 /*
1407 * Reset the interface to pick up changes in any other
1408 * flags that affect the hardware state.
1409 */
1410 error = vr_init(sc);
1411 }
1412 break;
1413
1414 case SIOCADDMULTI:
1415 case SIOCDELMULTI:
1416 if (command == SIOCADDMULTI)
1417 error = ether_addmulti(ifr, &sc->vr_ec);
1418 else
1419 error = ether_delmulti(ifr, &sc->vr_ec);
1420
1421 if (error == ENETRESET) {
1422 /*
1423 * Multicast list has changed; set the hardware filter
1424 * accordingly.
1425 */
1426 vr_setmulti(sc);
1427 error = 0;
1428 }
1429 break;
1430
1431 case SIOCGIFMEDIA:
1432 case SIOCSIFMEDIA:
1433 error = ifmedia_ioctl(ifp, ifr, &sc->vr_mii.mii_media, command);
1434 break;
1435
1436 default:
1437 error = EINVAL;
1438 break;
1439 }
1440
1441 splx(s);
1442 return (error);
1443 }
1444
1445 static void
1446 vr_watchdog(ifp)
1447 struct ifnet *ifp;
1448 {
1449 struct vr_softc *sc = ifp->if_softc;
1450
1451 printf("%s: device timeout\n", sc->vr_dev.dv_xname);
1452 ifp->if_oerrors++;
1453
1454 (void) vr_init(sc);
1455 }
1456
1457 /*
1458 * One second timer, used to tick MII.
1459 */
1460 static void
1461 vr_tick(arg)
1462 void *arg;
1463 {
1464 struct vr_softc *sc = arg;
1465 int s;
1466
1467 s = splnet();
1468 mii_tick(&sc->vr_mii);
1469 splx(s);
1470
1471 timeout(vr_tick, sc, hz);
1472 }
1473
1474 /*
1475 * Drain the receive queue.
1476 */
1477 static void
1478 vr_rxdrain(sc)
1479 struct vr_softc *sc;
1480 {
1481 struct vr_descsoft *ds;
1482 int i;
1483
1484 for (i = 0; i < VR_NRXDESC; i++) {
1485 ds = VR_DSRX(sc, i);
1486 if (ds->ds_mbuf != NULL) {
1487 bus_dmamap_unload(sc->vr_dmat, ds->ds_dmamap);
1488 m_freem(ds->ds_mbuf);
1489 ds->ds_mbuf = NULL;
1490 }
1491 }
1492 }
1493
1494 /*
1495 * Stop the adapter and free any mbufs allocated to the
1496 * transmit lists.
1497 */
1498 static void
1499 vr_stop(sc, drain)
1500 struct vr_softc *sc;
1501 int drain;
1502 {
1503 struct vr_descsoft *ds;
1504 struct ifnet *ifp;
1505 int i;
1506
1507 /* Cancel one second timer. */
1508 untimeout(vr_tick, sc);
1509
1510 ifp = &sc->vr_ec.ec_if;
1511 ifp->if_timer = 0;
1512
1513 VR_SETBIT16(sc, VR_COMMAND, VR_CMD_STOP);
1514 VR_CLRBIT16(sc, VR_COMMAND, (VR_CMD_RX_ON|VR_CMD_TX_ON));
1515 CSR_WRITE_2(sc, VR_IMR, 0x0000);
1516 CSR_WRITE_4(sc, VR_TXADDR, 0x00000000);
1517 CSR_WRITE_4(sc, VR_RXADDR, 0x00000000);
1518
1519 /*
1520 * Release any queued transmit buffers.
1521 */
1522 for (i = 0; i < VR_NTXDESC; i++) {
1523 ds = VR_DSTX(sc, i);
1524 if (ds->ds_mbuf != NULL) {
1525 bus_dmamap_unload(sc->vr_dmat, ds->ds_dmamap);
1526 m_freem(ds->ds_mbuf);
1527 ds->ds_mbuf = NULL;
1528 }
1529 }
1530
1531 if (drain) {
1532 /*
1533 * Release the receive buffers.
1534 */
1535 vr_rxdrain(sc);
1536 }
1537
1538 /*
1539 * Mark the interface down and cancel the watchdog timer.
1540 */
1541 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1542 ifp->if_timer = 0;
1543 }
1544
1545 static struct vr_type *vr_lookup __P((struct pci_attach_args *));
1546 static int vr_probe __P((struct device *, struct cfdata *, void *));
1547 static void vr_attach __P((struct device *, struct device *, void *));
1548 static void vr_shutdown __P((void *));
1549
1550 struct cfattach vr_ca = {
1551 sizeof (struct vr_softc), vr_probe, vr_attach
1552 };
1553
1554 static struct vr_type *
1555 vr_lookup(pa)
1556 struct pci_attach_args *pa;
1557 {
1558 struct vr_type *vrt;
1559
1560 for (vrt = vr_devs; vrt->vr_name != NULL; vrt++) {
1561 if (PCI_VENDOR(pa->pa_id) == vrt->vr_vid &&
1562 PCI_PRODUCT(pa->pa_id) == vrt->vr_did)
1563 return (vrt);
1564 }
1565 return (NULL);
1566 }
1567
1568 static int
1569 vr_probe(parent, match, aux)
1570 struct device *parent;
1571 struct cfdata *match;
1572 void *aux;
1573 {
1574 struct pci_attach_args *pa = (struct pci_attach_args *)aux;
1575
1576 if (vr_lookup(pa) != NULL)
1577 return (1);
1578
1579 return (0);
1580 }
1581
1582 /*
1583 * Stop all chip I/O so that the kernel's probe routines don't
1584 * get confused by errant DMAs when rebooting.
1585 */
1586 static void
1587 vr_shutdown(arg)
1588 void *arg;
1589 {
1590 struct vr_softc *sc = (struct vr_softc *)arg;
1591
1592 vr_stop(sc, 1);
1593 }
1594
1595 /*
1596 * Attach the interface. Allocate softc structures, do ifmedia
1597 * setup and ethernet/BPF attach.
1598 */
1599 static void
1600 vr_attach(parent, self, aux)
1601 struct device *parent;
1602 struct device *self;
1603 void *aux;
1604 {
1605 struct vr_softc *sc = (struct vr_softc *) self;
1606 struct pci_attach_args *pa = (struct pci_attach_args *) aux;
1607 bus_dma_segment_t seg;
1608 struct vr_type *vrt;
1609 u_int32_t command;
1610 struct ifnet *ifp;
1611 u_char eaddr[ETHER_ADDR_LEN];
1612 int i, rseg, error;
1613
1614 #define PCI_CONF_WRITE(r, v) pci_conf_write(pa->pa_pc, pa->pa_tag, (r), (v))
1615 #define PCI_CONF_READ(r) pci_conf_read(pa->pa_pc, pa->pa_tag, (r))
1616
1617 vrt = vr_lookup(pa);
1618 if (vrt == NULL) {
1619 printf("\n");
1620 panic("vr_attach: impossible");
1621 }
1622
1623 printf(": %s Ethernet\n", vrt->vr_name);
1624
1625 /*
1626 * Handle power management nonsense.
1627 */
1628
1629 command = PCI_CONF_READ(VR_PCI_CAPID) & 0x000000FF;
1630 if (command == 0x01) {
1631 command = PCI_CONF_READ(VR_PCI_PWRMGMTCTRL);
1632 if (command & VR_PSTATE_MASK) {
1633 u_int32_t iobase, membase, irq;
1634
1635 /* Save important PCI config data. */
1636 iobase = PCI_CONF_READ(VR_PCI_LOIO);
1637 membase = PCI_CONF_READ(VR_PCI_LOMEM);
1638 irq = PCI_CONF_READ(VR_PCI_INTLINE);
1639
1640 /* Reset the power state. */
1641 printf("%s: chip is in D%d power mode "
1642 "-- setting to D0\n",
1643 sc->vr_dev.dv_xname, command & VR_PSTATE_MASK);
1644 command &= 0xFFFFFFFC;
1645 PCI_CONF_WRITE(VR_PCI_PWRMGMTCTRL, command);
1646
1647 /* Restore PCI config data. */
1648 PCI_CONF_WRITE(VR_PCI_LOIO, iobase);
1649 PCI_CONF_WRITE(VR_PCI_LOMEM, membase);
1650 PCI_CONF_WRITE(VR_PCI_INTLINE, irq);
1651 }
1652 }
1653
1654 /* Make sure bus mastering is enabled. */
1655 command = PCI_CONF_READ(PCI_COMMAND_STATUS_REG);
1656 command |= PCI_COMMAND_MASTER_ENABLE;
1657 PCI_CONF_WRITE(PCI_COMMAND_STATUS_REG, command);
1658
1659 /*
1660 * Map control/status registers.
1661 */
1662 {
1663 bus_space_tag_t iot, memt;
1664 bus_space_handle_t ioh, memh;
1665 int ioh_valid, memh_valid;
1666 pci_intr_handle_t intrhandle;
1667 const char *intrstr;
1668
1669 ioh_valid = (pci_mapreg_map(pa, VR_PCI_LOIO,
1670 PCI_MAPREG_TYPE_IO, 0,
1671 &iot, &ioh, NULL, NULL) == 0);
1672 memh_valid = (pci_mapreg_map(pa, VR_PCI_LOMEM,
1673 PCI_MAPREG_TYPE_MEM |
1674 PCI_MAPREG_MEM_TYPE_32BIT,
1675 0, &memt, &memh, NULL, NULL) == 0);
1676 #if defined(VR_USEIOSPACE)
1677 if (ioh_valid) {
1678 sc->vr_bst = iot;
1679 sc->vr_bsh = ioh;
1680 } else if (memh_valid) {
1681 sc->vr_bst = memt;
1682 sc->vr_bsh = memh;
1683 }
1684 #else
1685 if (memh_valid) {
1686 sc->vr_bst = memt;
1687 sc->vr_bsh = memh;
1688 } else if (ioh_valid) {
1689 sc->vr_bst = iot;
1690 sc->vr_bsh = ioh;
1691 }
1692 #endif
1693 else {
1694 printf(": unable to map device registers\n");
1695 return;
1696 }
1697
1698 /* Allocate interrupt */
1699 if (pci_intr_map(pa->pa_pc, pa->pa_intrtag, pa->pa_intrpin,
1700 pa->pa_intrline, &intrhandle)) {
1701 printf("%s: couldn't map interrupt\n",
1702 sc->vr_dev.dv_xname);
1703 return;
1704 }
1705 intrstr = pci_intr_string(pa->pa_pc, intrhandle);
1706 sc->vr_ih = pci_intr_establish(pa->pa_pc, intrhandle, IPL_NET,
1707 vr_intr, sc);
1708 if (sc->vr_ih == NULL) {
1709 printf("%s: couldn't establish interrupt",
1710 sc->vr_dev.dv_xname);
1711 if (intrstr != NULL)
1712 printf(" at %s", intrstr);
1713 printf("\n");
1714 }
1715 printf("%s: interrupting at %s\n",
1716 sc->vr_dev.dv_xname, intrstr);
1717 }
1718
1719 /* Reset the adapter. */
1720 vr_reset(sc);
1721
1722 /*
1723 * Get station address. The way the Rhine chips work,
1724 * you're not allowed to directly access the EEPROM once
1725 * they've been programmed a special way. Consequently,
1726 * we need to read the node address from the PAR0 and PAR1
1727 * registers.
1728 */
1729 VR_SETBIT(sc, VR_EECSR, VR_EECSR_LOAD);
1730 DELAY(200);
1731 for (i = 0; i < ETHER_ADDR_LEN; i++)
1732 eaddr[i] = CSR_READ_1(sc, VR_PAR0 + i);
1733
1734 /*
1735 * A Rhine chip was detected. Inform the world.
1736 */
1737 printf("%s: Ethernet address: %s\n",
1738 sc->vr_dev.dv_xname, ether_sprintf(eaddr));
1739
1740 bcopy(eaddr, sc->vr_enaddr, ETHER_ADDR_LEN);
1741
1742 sc->vr_dmat = pa->pa_dmat;
1743
1744 /*
1745 * Allocate the control data structures, and create and load
1746 * the DMA map for it.
1747 */
1748 if ((error = bus_dmamem_alloc(sc->vr_dmat,
1749 sizeof(struct vr_control_data), PAGE_SIZE, 0, &seg, 1, &rseg,
1750 0)) != 0) {
1751 printf("%s: unable to allocate control data, error = %d\n",
1752 sc->vr_dev.dv_xname, error);
1753 goto fail_0;
1754 }
1755
1756 if ((error = bus_dmamem_map(sc->vr_dmat, &seg, rseg,
1757 sizeof(struct vr_control_data), (caddr_t *)&sc->vr_control_data,
1758 BUS_DMA_COHERENT)) != 0) {
1759 printf("%s: unable to map control data, error = %d\n",
1760 sc->vr_dev.dv_xname, error);
1761 goto fail_1;
1762 }
1763
1764 if ((error = bus_dmamap_create(sc->vr_dmat,
1765 sizeof(struct vr_control_data), 1,
1766 sizeof(struct vr_control_data), 0, 0,
1767 &sc->vr_cddmamap)) != 0) {
1768 printf("%s: unable to create control data DMA map, "
1769 "error = %d\n", sc->vr_dev.dv_xname, error);
1770 goto fail_2;
1771 }
1772
1773 if ((error = bus_dmamap_load(sc->vr_dmat, sc->vr_cddmamap,
1774 sc->vr_control_data, sizeof(struct vr_control_data), NULL,
1775 0)) != 0) {
1776 printf("%s: unable to load control data DMA map, error = %d\n",
1777 sc->vr_dev.dv_xname, error);
1778 goto fail_3;
1779 }
1780
1781 /*
1782 * Create the transmit buffer DMA maps.
1783 */
1784 for (i = 0; i < VR_NTXDESC; i++) {
1785 if ((error = bus_dmamap_create(sc->vr_dmat, MCLBYTES,
1786 1, MCLBYTES, 0, 0,
1787 &VR_DSTX(sc, i)->ds_dmamap)) != 0) {
1788 printf("%s: unable to create tx DMA map %d, "
1789 "error = %d\n", sc->vr_dev.dv_xname, i, error);
1790 goto fail_4;
1791 }
1792 }
1793
1794 /*
1795 * Create the receive buffer DMA maps.
1796 */
1797 for (i = 0; i < VR_NRXDESC; i++) {
1798 if ((error = bus_dmamap_create(sc->vr_dmat, MCLBYTES, 1,
1799 MCLBYTES, 0, 0,
1800 &VR_DSRX(sc, i)->ds_dmamap)) != 0) {
1801 printf("%s: unable to create rx DMA map %d, "
1802 "error = %d\n", sc->vr_dev.dv_xname, i, error);
1803 goto fail_5;
1804 }
1805 VR_DSRX(sc, i)->ds_mbuf = NULL;
1806 }
1807
1808 ifp = &sc->vr_ec.ec_if;
1809 ifp->if_softc = sc;
1810 ifp->if_mtu = ETHERMTU;
1811 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1812 ifp->if_ioctl = vr_ioctl;
1813 ifp->if_start = vr_start;
1814 ifp->if_watchdog = vr_watchdog;
1815 ifp->if_baudrate = 10000000;
1816 bcopy(sc->vr_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
1817
1818 /*
1819 * Initialize MII/media info.
1820 */
1821 sc->vr_mii.mii_ifp = ifp;
1822 sc->vr_mii.mii_readreg = vr_mii_readreg;
1823 sc->vr_mii.mii_writereg = vr_mii_writereg;
1824 sc->vr_mii.mii_statchg = vr_mii_statchg;
1825 ifmedia_init(&sc->vr_mii.mii_media, 0, vr_ifmedia_upd, vr_ifmedia_sts);
1826 mii_phy_probe(&sc->vr_dev, &sc->vr_mii, 0xffffffff, MII_PHY_ANY,
1827 MII_OFFSET_ANY);
1828 if (LIST_FIRST(&sc->vr_mii.mii_phys) == NULL) {
1829 ifmedia_add(&sc->vr_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
1830 ifmedia_set(&sc->vr_mii.mii_media, IFM_ETHER|IFM_NONE);
1831 } else
1832 ifmedia_set(&sc->vr_mii.mii_media, IFM_ETHER|IFM_AUTO);
1833
1834 /*
1835 * Call MI attach routines.
1836 */
1837 if_attach(ifp);
1838 ether_ifattach(ifp, sc->vr_enaddr);
1839
1840 #if NBPFILTER > 0
1841 bpfattach(&sc->vr_ec.ec_if.if_bpf,
1842 ifp, DLT_EN10MB, sizeof (struct ether_header));
1843 #endif
1844
1845 sc->vr_ats = shutdownhook_establish(vr_shutdown, sc);
1846 if (sc->vr_ats == NULL)
1847 printf("%s: warning: couldn't establish shutdown hook\n",
1848 sc->vr_dev.dv_xname);
1849 return;
1850
1851 fail_5:
1852 for (i = 0; i < VR_NRXDESC; i++) {
1853 if (sc->vr_rxsoft[i].ds_dmamap != NULL)
1854 bus_dmamap_destroy(sc->vr_dmat,
1855 sc->vr_rxsoft[i].ds_dmamap);
1856 }
1857 fail_4:
1858 for (i = 0; i < VR_NTXDESC; i++) {
1859 if (sc->vr_txsoft[i].ds_dmamap != NULL)
1860 bus_dmamap_destroy(sc->vr_dmat,
1861 sc->vr_txsoft[i].ds_dmamap);
1862 }
1863 bus_dmamap_unload(sc->vr_dmat, sc->vr_cddmamap);
1864 fail_3:
1865 bus_dmamap_destroy(sc->vr_dmat, sc->vr_cddmamap);
1866 fail_2:
1867 bus_dmamem_unmap(sc->vr_dmat, (caddr_t)sc->vr_control_data,
1868 sizeof(struct vr_control_data));
1869 fail_1:
1870 bus_dmamem_free(sc->vr_dmat, &seg, rseg);
1871 fail_0:
1872 return;
1873 }
1874