if_vr.c revision 1.23 1 /* $NetBSD: if_vr.c,v 1.23 1999/08/03 17:25:52 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 Ethernet" },
168 { PCI_VENDOR_VIATECH, PCI_PRODUCT_VIATECH_VT86C100A,
169 "VIA VT86C100A (Rhine-II) 10/100 Ethernet" },
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 (rxstat & (VR_RXSTAT_RX_PHYS | VR_RXSTAT_RX_BROAD |
882 VR_RXSTAT_RX_MULTI)) == 0) {
883 m_freem(m);
884 continue;
885 }
886 }
887 #endif
888 /* Pass it on. */
889 (*ifp->if_input)(ifp, m);
890 }
891
892 /* Update the receive pointer. */
893 sc->vr_rxptr = i;
894 }
895
896 void
897 vr_rxeoc(sc)
898 struct vr_softc *sc;
899 {
900
901 vr_rxeof(sc);
902 VR_CLRBIT16(sc, VR_COMMAND, VR_CMD_RX_ON);
903 CSR_WRITE_4(sc, VR_RXADDR, VR_CDRXADDR(sc, sc->vr_rxptr));
904 VR_SETBIT16(sc, VR_COMMAND, VR_CMD_RX_ON);
905 VR_SETBIT16(sc, VR_COMMAND, VR_CMD_RX_GO);
906 }
907
908 /*
909 * A frame was downloaded to the chip. It's safe for us to clean up
910 * the list buffers.
911 */
912 static void
913 vr_txeof(sc)
914 struct vr_softc *sc;
915 {
916 struct ifnet *ifp = &sc->vr_ec.ec_if;
917 struct vr_desc *d;
918 struct vr_descsoft *ds;
919 u_int32_t txstat;
920 int i;
921
922 ifp->if_flags &= ~IFF_OACTIVE;
923
924 /*
925 * Go through our tx list and free mbufs for those
926 * frames that have been transmitted.
927 */
928 for (i = sc->vr_txdirty; sc->vr_txpending != 0;
929 i = VR_NEXTTX(i), sc->vr_txpending--) {
930 d = VR_CDTX(sc, i);
931 ds = VR_DSTX(sc, i);
932
933 VR_CDTXSYNC(sc, i, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
934
935 txstat = pcitoh(d->vr_status);
936 if (txstat & VR_TXSTAT_OWN)
937 break;
938
939 bus_dmamap_sync(sc->vr_dmat, ds->ds_dmamap,
940 0, ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
941 bus_dmamap_unload(sc->vr_dmat, ds->ds_dmamap);
942 m_freem(ds->ds_mbuf);
943 ds->ds_mbuf = NULL;
944
945 if (txstat & VR_TXSTAT_ERRSUM) {
946 ifp->if_oerrors++;
947 if (txstat & VR_TXSTAT_DEFER)
948 ifp->if_collisions++;
949 if (txstat & VR_TXSTAT_LATECOLL)
950 ifp->if_collisions++;
951 }
952
953 ifp->if_collisions += (txstat & VR_TXSTAT_COLLCNT) >> 3;
954 ifp->if_opackets++;
955 }
956
957 /* Update the dirty transmit buffer pointer. */
958 sc->vr_txdirty = i;
959
960 /*
961 * Cancel the watchdog timer if there are no pending
962 * transmissions.
963 */
964 if (sc->vr_txpending == 0)
965 ifp->if_timer = 0;
966 }
967
968 static int
969 vr_intr(arg)
970 void *arg;
971 {
972 struct vr_softc *sc;
973 struct ifnet *ifp;
974 u_int16_t status;
975 int handled = 0, dotx = 0;
976
977 sc = arg;
978 ifp = &sc->vr_ec.ec_if;
979
980 /* Suppress unwanted interrupts. */
981 if ((ifp->if_flags & IFF_UP) == 0) {
982 vr_stop(sc, 1);
983 return (0);
984 }
985
986 /* Disable interrupts. */
987 CSR_WRITE_2(sc, VR_IMR, 0x0000);
988
989 for (;;) {
990 status = CSR_READ_2(sc, VR_ISR);
991 if (status)
992 CSR_WRITE_2(sc, VR_ISR, status);
993
994 if ((status & VR_INTRS) == 0)
995 break;
996
997 handled = 1;
998
999 if (status & VR_ISR_RX_OK)
1000 vr_rxeof(sc);
1001
1002 if (status &
1003 (VR_ISR_RX_ERR | VR_ISR_RX_NOBUF | VR_ISR_RX_OFLOW |
1004 VR_ISR_RX_DROPPED))
1005 vr_rxeoc(sc);
1006
1007 if (status & VR_ISR_TX_OK) {
1008 dotx = 1;
1009 vr_txeof(sc);
1010 }
1011
1012 if (status & (VR_ISR_TX_UNDERRUN | VR_ISR_TX_ABRT)) {
1013 if (status & VR_ISR_TX_UNDERRUN)
1014 printf("%s: transmit underrun\n",
1015 sc->vr_dev.dv_xname);
1016 if (status & VR_ISR_TX_ABRT)
1017 printf("%s: transmit aborted\n",
1018 sc->vr_dev.dv_xname);
1019 ifp->if_oerrors++;
1020 dotx = 1;
1021 vr_txeof(sc);
1022 if (sc->vr_txpending) {
1023 VR_SETBIT16(sc, VR_COMMAND, VR_CMD_TX_ON);
1024 VR_SETBIT16(sc, VR_COMMAND, VR_CMD_TX_GO);
1025 }
1026 }
1027
1028 if (status & VR_ISR_BUSERR) {
1029 printf("%s: PCI bus error\n", sc->vr_dev.dv_xname);
1030 /* vr_init() calls vr_start() */
1031 dotx = 0;
1032 (void) vr_init(sc);
1033 }
1034 }
1035
1036 /* Re-enable interrupts. */
1037 CSR_WRITE_2(sc, VR_IMR, VR_INTRS);
1038
1039 if (dotx)
1040 vr_start(ifp);
1041
1042 return (handled);
1043 }
1044
1045 /*
1046 * Main transmit routine. To avoid having to do mbuf copies, we put pointers
1047 * to the mbuf data regions directly in the transmit lists. We also save a
1048 * copy of the pointers since the transmit list fragment pointers are
1049 * physical addresses.
1050 */
1051 static void
1052 vr_start(ifp)
1053 struct ifnet *ifp;
1054 {
1055 struct vr_softc *sc = ifp->if_softc;
1056 struct mbuf *m0, *m;
1057 struct vr_desc *d;
1058 struct vr_descsoft *ds;
1059 int error, firsttx, nexttx, opending;
1060
1061 /*
1062 * Remember the previous txpending and the first transmit
1063 * descriptor we use.
1064 */
1065 opending = sc->vr_txpending;
1066 firsttx = VR_NEXTTX(sc->vr_txlast);
1067
1068 /*
1069 * Loop through the send queue, setting up transmit descriptors
1070 * until we drain the queue, or use up all available transmit
1071 * descriptors.
1072 */
1073 while (sc->vr_txpending < VR_NTXDESC) {
1074 /*
1075 * Grab a packet off the queue.
1076 */
1077 IF_DEQUEUE(&ifp->if_snd, m0);
1078 if (m0 == NULL)
1079 break;
1080
1081 /*
1082 * Get the next available transmit descriptor.
1083 */
1084 nexttx = VR_NEXTTX(sc->vr_txlast);
1085 d = VR_CDTX(sc, nexttx);
1086 ds = VR_DSTX(sc, nexttx);
1087
1088 /*
1089 * Load the DMA map. If this fails, the packet didn't
1090 * fit in one DMA segment, and we need to copy. Note,
1091 * the packet must also be aligned.
1092 */
1093 if ((mtod(m0, bus_addr_t) & 3) != 0 ||
1094 bus_dmamap_load_mbuf(sc->vr_dmat, ds->ds_dmamap, m0,
1095 BUS_DMA_NOWAIT) != 0) {
1096 MGETHDR(m, M_DONTWAIT, MT_DATA);
1097 if (m == NULL) {
1098 printf("%s: unable to allocate Tx mbuf\n",
1099 sc->vr_dev.dv_xname);
1100 IF_PREPEND(&ifp->if_snd, m0);
1101 break;
1102 }
1103 if (m0->m_pkthdr.len > MHLEN) {
1104 MCLGET(m, M_DONTWAIT);
1105 if ((m->m_flags & M_EXT) == 0) {
1106 printf("%s: unable to allocate Tx "
1107 "cluster\n", sc->vr_dev.dv_xname);
1108 m_freem(m);
1109 IF_PREPEND(&ifp->if_snd, m0);
1110 break;
1111 }
1112 }
1113 m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t));
1114 m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
1115 m_freem(m0);
1116 m0 = m;
1117 error = bus_dmamap_load_mbuf(sc->vr_dmat,
1118 ds->ds_dmamap, m0, BUS_DMA_NOWAIT);
1119 if (error) {
1120 printf("%s: unable to load Tx buffer, "
1121 "error = %d\n", sc->vr_dev.dv_xname, error);
1122 IF_PREPEND(&ifp->if_snd, m0);
1123 break;
1124 }
1125 }
1126
1127 /* Sync the DMA map. */
1128 bus_dmamap_sync(sc->vr_dmat, ds->ds_dmamap, 0,
1129 ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_PREWRITE);
1130
1131 /*
1132 * Store a pointer to the packet so we can free it later.
1133 */
1134 ds->ds_mbuf = m0;
1135
1136 #if NBPFILTER > 0
1137 /*
1138 * If there's a BPF listener, bounce a copy of this frame
1139 * to him.
1140 */
1141 if (ifp->if_bpf)
1142 bpf_mtap(ifp->if_bpf, m0);
1143 #endif
1144
1145 /*
1146 * Fill in the transmit descriptor. The Rhine
1147 * doesn't auto-pad, so we have to do this ourselves.
1148 */
1149 d->vr_data = htopci(ds->ds_dmamap->dm_segs[0].ds_addr);
1150 d->vr_ctl = htopci(m0->m_pkthdr.len < VR_MIN_FRAMELEN ?
1151 VR_MIN_FRAMELEN : m0->m_pkthdr.len);
1152 d->vr_ctl |=
1153 htopci(VR_TXCTL_TLINK|VR_TXCTL_FIRSTFRAG|VR_TXCTL_LASTFRAG);
1154
1155 /*
1156 * If this is the first descriptor we're enqueuing,
1157 * don't give it to the Rhine yet. That could cause
1158 * a race condition. We'll do it below.
1159 */
1160 if (nexttx == firsttx)
1161 d->vr_status = 0;
1162 else
1163 d->vr_status = htopci(VR_TXSTAT_OWN);
1164
1165 VR_CDTXSYNC(sc, nexttx,
1166 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1167
1168 /* Advance the tx pointer. */
1169 sc->vr_txpending++;
1170 sc->vr_txlast = nexttx;
1171 }
1172
1173 if (sc->vr_txpending == VR_NTXDESC) {
1174 /* No more slots left; notify upper layer. */
1175 ifp->if_flags |= IFF_OACTIVE;
1176 }
1177
1178 if (sc->vr_txpending != opending) {
1179 /*
1180 * We enqueued packets. If the transmitter was idle,
1181 * reset the txdirty pointer.
1182 */
1183 if (opending == 0)
1184 sc->vr_txdirty = firsttx;
1185
1186 /*
1187 * Cause a transmit interrupt to happen on the
1188 * last packet we enqueued.
1189 */
1190 VR_CDTX(sc, sc->vr_txlast)->vr_ctl |= htopci(VR_TXCTL_FINT);
1191 VR_CDTXSYNC(sc, sc->vr_txlast,
1192 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1193
1194 /*
1195 * The entire packet chain is set up. Give the
1196 * first descriptor to the Rhine now.
1197 */
1198 VR_CDTX(sc, firsttx)->vr_status = htopci(VR_TXSTAT_OWN);
1199 VR_CDTXSYNC(sc, firsttx,
1200 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1201
1202 /* Start the transmitter. */
1203 VR_SETBIT16(sc, VR_COMMAND, VR_CMD_TX_ON|VR_CMD_TX_GO);
1204
1205 /* Set the watchdog timer in case the chip flakes out. */
1206 ifp->if_timer = 5;
1207 }
1208 }
1209
1210 /*
1211 * Initialize the interface. Must be called at splnet.
1212 */
1213 static int
1214 vr_init(sc)
1215 struct vr_softc *sc;
1216 {
1217 struct ifnet *ifp = &sc->vr_ec.ec_if;
1218 struct vr_desc *d;
1219 struct vr_descsoft *ds;
1220 int i, error;
1221
1222 /* Cancel pending I/O. */
1223 vr_stop(sc, 0);
1224
1225 /* Reset the Rhine to a known state. */
1226 vr_reset(sc);
1227
1228 VR_CLRBIT(sc, VR_RXCFG, VR_RXCFG_RX_THRESH);
1229 VR_SETBIT(sc, VR_RXCFG, VR_RXTHRESH_STORENFWD);
1230
1231 VR_CLRBIT(sc, VR_TXCFG, VR_TXCFG_TX_THRESH);
1232 VR_SETBIT(sc, VR_TXCFG, VR_TXTHRESH_STORENFWD);
1233
1234 /*
1235 * Initialize the transmit desciptor ring. txlast is initialized
1236 * to the end of the list so that it will wrap around to the first
1237 * descriptor when the first packet is transmitted.
1238 */
1239 for (i = 0; i < VR_NTXDESC; i++) {
1240 d = VR_CDTX(sc, i);
1241 memset(d, 0, sizeof(struct vr_desc));
1242 d->vr_next = htopci(VR_CDTXADDR(sc, VR_NEXTTX(i)));
1243 VR_CDTXSYNC(sc, i, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1244 }
1245 sc->vr_txpending = 0;
1246 sc->vr_txdirty = 0;
1247 sc->vr_txlast = VR_NTXDESC - 1;
1248
1249 /*
1250 * Initialize the receive descriptor ring.
1251 */
1252 for (i = 0; i < VR_NRXDESC; i++) {
1253 ds = VR_DSRX(sc, i);
1254 if (ds->ds_mbuf == NULL) {
1255 if ((error = vr_add_rxbuf(sc, i)) != 0) {
1256 printf("%s: unable to allocate or map rx "
1257 "buffer %d, error = %d\n",
1258 sc->vr_dev.dv_xname, i, error);
1259 /*
1260 * XXX Should attempt to run with fewer receive
1261 * XXX buffers instead of just failing.
1262 */
1263 vr_rxdrain(sc);
1264 goto out;
1265 }
1266 }
1267 }
1268 sc->vr_rxptr = 0;
1269
1270 /* If we want promiscuous mode, set the allframes bit. */
1271 if (ifp->if_flags & IFF_PROMISC)
1272 VR_SETBIT(sc, VR_RXCFG, VR_RXCFG_RX_PROMISC);
1273 else
1274 VR_CLRBIT(sc, VR_RXCFG, VR_RXCFG_RX_PROMISC);
1275
1276 /* Set capture broadcast bit to capture broadcast frames. */
1277 if (ifp->if_flags & IFF_BROADCAST)
1278 VR_SETBIT(sc, VR_RXCFG, VR_RXCFG_RX_BROAD);
1279 else
1280 VR_CLRBIT(sc, VR_RXCFG, VR_RXCFG_RX_BROAD);
1281
1282 /* Program the multicast filter, if necessary. */
1283 vr_setmulti(sc);
1284
1285 /* Give the transmit and recieve rings to the Rhine. */
1286 CSR_WRITE_4(sc, VR_RXADDR, VR_CDRXADDR(sc, sc->vr_rxptr));
1287 CSR_WRITE_4(sc, VR_TXADDR, VR_CDTXADDR(sc, VR_NEXTTX(sc->vr_txlast)));
1288
1289 /* Set current media. */
1290 mii_mediachg(&sc->vr_mii);
1291
1292 /* Enable receiver and transmitter. */
1293 CSR_WRITE_2(sc, VR_COMMAND, VR_CMD_TX_NOPOLL|VR_CMD_START|
1294 VR_CMD_TX_ON|VR_CMD_RX_ON|
1295 VR_CMD_RX_GO);
1296
1297 /* Enable interrupts. */
1298 CSR_WRITE_2(sc, VR_ISR, 0xFFFF);
1299 CSR_WRITE_2(sc, VR_IMR, VR_INTRS);
1300
1301 ifp->if_flags |= IFF_RUNNING;
1302 ifp->if_flags &= ~IFF_OACTIVE;
1303
1304 /* Start one second timer. */
1305 timeout(vr_tick, sc, hz);
1306
1307 /* Attempt to start output on the interface. */
1308 vr_start(ifp);
1309
1310 out:
1311 if (error)
1312 printf("%s: interface not running\n", sc->vr_dev.dv_xname);
1313 return (error);
1314 }
1315
1316 /*
1317 * Set media options.
1318 */
1319 static int
1320 vr_ifmedia_upd(ifp)
1321 struct ifnet *ifp;
1322 {
1323 struct vr_softc *sc = ifp->if_softc;
1324
1325 if (ifp->if_flags & IFF_UP)
1326 mii_mediachg(&sc->vr_mii);
1327 return (0);
1328 }
1329
1330 /*
1331 * Report current media status.
1332 */
1333 static void
1334 vr_ifmedia_sts(ifp, ifmr)
1335 struct ifnet *ifp;
1336 struct ifmediareq *ifmr;
1337 {
1338 struct vr_softc *sc = ifp->if_softc;
1339
1340 mii_pollstat(&sc->vr_mii);
1341 ifmr->ifm_status = sc->vr_mii.mii_media_status;
1342 ifmr->ifm_active = sc->vr_mii.mii_media_active;
1343 }
1344
1345 static int
1346 vr_ioctl(ifp, command, data)
1347 struct ifnet *ifp;
1348 u_long command;
1349 caddr_t data;
1350 {
1351 struct vr_softc *sc = ifp->if_softc;
1352 struct ifreq *ifr = (struct ifreq *)data;
1353 struct ifaddr *ifa = (struct ifaddr *)data;
1354 int s, error = 0;
1355
1356 s = splnet();
1357
1358 switch (command) {
1359 case SIOCSIFADDR:
1360 ifp->if_flags |= IFF_UP;
1361
1362 switch (ifa->ifa_addr->sa_family) {
1363 #ifdef INET
1364 case AF_INET:
1365 if ((error = vr_init(sc)) != 0)
1366 break;
1367 arp_ifinit(ifp, ifa);
1368 break;
1369 #endif /* INET */
1370 default:
1371 error = vr_init(sc);
1372 break;
1373 }
1374 break;
1375
1376 case SIOCGIFADDR:
1377 bcopy((caddr_t) sc->vr_enaddr,
1378 (caddr_t) ((struct sockaddr *)&ifr->ifr_data)->sa_data,
1379 ETHER_ADDR_LEN);
1380 break;
1381
1382 case SIOCSIFMTU:
1383 if (ifr->ifr_mtu > ETHERMTU)
1384 error = EINVAL;
1385 else
1386 ifp->if_mtu = ifr->ifr_mtu;
1387 break;
1388
1389 case SIOCSIFFLAGS:
1390 if ((ifp->if_flags & IFF_UP) == 0 &&
1391 (ifp->if_flags & IFF_RUNNING) != 0) {
1392 /*
1393 * If interface is marked down and it is running, then
1394 * stop it.
1395 */
1396 vr_stop(sc, 1);
1397 } else if ((ifp->if_flags & IFF_UP) != 0 &&
1398 (ifp->if_flags & IFF_RUNNING) == 0) {
1399 /*
1400 * If interface is marked up and it is stopped, then
1401 * start it.
1402 */
1403 error = vr_init(sc);
1404 } else if ((ifp->if_flags & IFF_UP) != 0) {
1405 /*
1406 * Reset the interface to pick up changes in any other
1407 * flags that affect the hardware state.
1408 */
1409 error = vr_init(sc);
1410 }
1411 break;
1412
1413 case SIOCADDMULTI:
1414 case SIOCDELMULTI:
1415 if (command == SIOCADDMULTI)
1416 error = ether_addmulti(ifr, &sc->vr_ec);
1417 else
1418 error = ether_delmulti(ifr, &sc->vr_ec);
1419
1420 if (error == ENETRESET) {
1421 /*
1422 * Multicast list has changed; set the hardware filter
1423 * accordingly.
1424 */
1425 vr_setmulti(sc);
1426 error = 0;
1427 }
1428 break;
1429
1430 case SIOCGIFMEDIA:
1431 case SIOCSIFMEDIA:
1432 error = ifmedia_ioctl(ifp, ifr, &sc->vr_mii.mii_media, command);
1433 break;
1434
1435 default:
1436 error = EINVAL;
1437 break;
1438 }
1439
1440 splx(s);
1441 return (error);
1442 }
1443
1444 static void
1445 vr_watchdog(ifp)
1446 struct ifnet *ifp;
1447 {
1448 struct vr_softc *sc = ifp->if_softc;
1449
1450 printf("%s: device timeout\n", sc->vr_dev.dv_xname);
1451 ifp->if_oerrors++;
1452
1453 (void) vr_init(sc);
1454 }
1455
1456 /*
1457 * One second timer, used to tick MII.
1458 */
1459 static void
1460 vr_tick(arg)
1461 void *arg;
1462 {
1463 struct vr_softc *sc = arg;
1464 int s;
1465
1466 s = splnet();
1467 mii_tick(&sc->vr_mii);
1468 splx(s);
1469
1470 timeout(vr_tick, sc, hz);
1471 }
1472
1473 /*
1474 * Drain the receive queue.
1475 */
1476 static void
1477 vr_rxdrain(sc)
1478 struct vr_softc *sc;
1479 {
1480 struct vr_descsoft *ds;
1481 int i;
1482
1483 for (i = 0; i < VR_NRXDESC; i++) {
1484 ds = VR_DSRX(sc, i);
1485 if (ds->ds_mbuf != NULL) {
1486 bus_dmamap_unload(sc->vr_dmat, ds->ds_dmamap);
1487 m_freem(ds->ds_mbuf);
1488 ds->ds_mbuf = NULL;
1489 }
1490 }
1491 }
1492
1493 /*
1494 * Stop the adapter and free any mbufs allocated to the
1495 * transmit lists.
1496 */
1497 static void
1498 vr_stop(sc, drain)
1499 struct vr_softc *sc;
1500 int drain;
1501 {
1502 struct vr_descsoft *ds;
1503 struct ifnet *ifp;
1504 int i;
1505
1506 /* Cancel one second timer. */
1507 untimeout(vr_tick, sc);
1508
1509 ifp = &sc->vr_ec.ec_if;
1510 ifp->if_timer = 0;
1511
1512 VR_SETBIT16(sc, VR_COMMAND, VR_CMD_STOP);
1513 VR_CLRBIT16(sc, VR_COMMAND, (VR_CMD_RX_ON|VR_CMD_TX_ON));
1514 CSR_WRITE_2(sc, VR_IMR, 0x0000);
1515 CSR_WRITE_4(sc, VR_TXADDR, 0x00000000);
1516 CSR_WRITE_4(sc, VR_RXADDR, 0x00000000);
1517
1518 /*
1519 * Release any queued transmit buffers.
1520 */
1521 for (i = 0; i < VR_NTXDESC; i++) {
1522 ds = VR_DSTX(sc, i);
1523 if (ds->ds_mbuf != NULL) {
1524 bus_dmamap_unload(sc->vr_dmat, ds->ds_dmamap);
1525 m_freem(ds->ds_mbuf);
1526 ds->ds_mbuf = NULL;
1527 }
1528 }
1529
1530 if (drain) {
1531 /*
1532 * Release the receive buffers.
1533 */
1534 vr_rxdrain(sc);
1535 }
1536
1537 /*
1538 * Mark the interface down and cancel the watchdog timer.
1539 */
1540 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1541 ifp->if_timer = 0;
1542 }
1543
1544 static struct vr_type *vr_lookup __P((struct pci_attach_args *));
1545 static int vr_probe __P((struct device *, struct cfdata *, void *));
1546 static void vr_attach __P((struct device *, struct device *, void *));
1547 static void vr_shutdown __P((void *));
1548
1549 struct cfattach vr_ca = {
1550 sizeof (struct vr_softc), vr_probe, vr_attach
1551 };
1552
1553 static struct vr_type *
1554 vr_lookup(pa)
1555 struct pci_attach_args *pa;
1556 {
1557 struct vr_type *vrt;
1558
1559 for (vrt = vr_devs; vrt->vr_name != NULL; vrt++) {
1560 if (PCI_VENDOR(pa->pa_id) == vrt->vr_vid &&
1561 PCI_PRODUCT(pa->pa_id) == vrt->vr_did)
1562 return (vrt);
1563 }
1564 return (NULL);
1565 }
1566
1567 static int
1568 vr_probe(parent, match, aux)
1569 struct device *parent;
1570 struct cfdata *match;
1571 void *aux;
1572 {
1573 struct pci_attach_args *pa = (struct pci_attach_args *)aux;
1574
1575 if (vr_lookup(pa) != NULL)
1576 return (1);
1577
1578 return (0);
1579 }
1580
1581 /*
1582 * Stop all chip I/O so that the kernel's probe routines don't
1583 * get confused by errant DMAs when rebooting.
1584 */
1585 static void
1586 vr_shutdown(arg)
1587 void *arg;
1588 {
1589 struct vr_softc *sc = (struct vr_softc *)arg;
1590
1591 vr_stop(sc, 1);
1592 }
1593
1594 /*
1595 * Attach the interface. Allocate softc structures, do ifmedia
1596 * setup and ethernet/BPF attach.
1597 */
1598 static void
1599 vr_attach(parent, self, aux)
1600 struct device *parent;
1601 struct device *self;
1602 void *aux;
1603 {
1604 struct vr_softc *sc = (struct vr_softc *) self;
1605 struct pci_attach_args *pa = (struct pci_attach_args *) aux;
1606 bus_dma_segment_t seg;
1607 struct vr_type *vrt;
1608 u_int32_t command;
1609 struct ifnet *ifp;
1610 u_char eaddr[ETHER_ADDR_LEN];
1611 int i, rseg, error;
1612
1613 #define PCI_CONF_WRITE(r, v) pci_conf_write(pa->pa_pc, pa->pa_tag, (r), (v))
1614 #define PCI_CONF_READ(r) pci_conf_read(pa->pa_pc, pa->pa_tag, (r))
1615
1616 vrt = vr_lookup(pa);
1617 if (vrt == NULL) {
1618 printf("\n");
1619 panic("vr_attach: impossible");
1620 }
1621
1622 printf(": %s Ethernet\n", vrt->vr_name);
1623
1624 /*
1625 * Handle power management nonsense.
1626 */
1627
1628 command = PCI_CONF_READ(VR_PCI_CAPID) & 0x000000FF;
1629 if (command == 0x01) {
1630 command = PCI_CONF_READ(VR_PCI_PWRMGMTCTRL);
1631 if (command & VR_PSTATE_MASK) {
1632 u_int32_t iobase, membase, irq;
1633
1634 /* Save important PCI config data. */
1635 iobase = PCI_CONF_READ(VR_PCI_LOIO);
1636 membase = PCI_CONF_READ(VR_PCI_LOMEM);
1637 irq = PCI_CONF_READ(VR_PCI_INTLINE);
1638
1639 /* Reset the power state. */
1640 printf("%s: chip is in D%d power mode "
1641 "-- setting to D0\n",
1642 sc->vr_dev.dv_xname, command & VR_PSTATE_MASK);
1643 command &= 0xFFFFFFFC;
1644 PCI_CONF_WRITE(VR_PCI_PWRMGMTCTRL, command);
1645
1646 /* Restore PCI config data. */
1647 PCI_CONF_WRITE(VR_PCI_LOIO, iobase);
1648 PCI_CONF_WRITE(VR_PCI_LOMEM, membase);
1649 PCI_CONF_WRITE(VR_PCI_INTLINE, irq);
1650 }
1651 }
1652
1653 /* Make sure bus mastering is enabled. */
1654 command = PCI_CONF_READ(PCI_COMMAND_STATUS_REG);
1655 command |= PCI_COMMAND_MASTER_ENABLE;
1656 PCI_CONF_WRITE(PCI_COMMAND_STATUS_REG, command);
1657
1658 /*
1659 * Map control/status registers.
1660 */
1661 {
1662 bus_space_tag_t iot, memt;
1663 bus_space_handle_t ioh, memh;
1664 int ioh_valid, memh_valid;
1665 pci_intr_handle_t intrhandle;
1666 const char *intrstr;
1667
1668 ioh_valid = (pci_mapreg_map(pa, VR_PCI_LOIO,
1669 PCI_MAPREG_TYPE_IO, 0,
1670 &iot, &ioh, NULL, NULL) == 0);
1671 memh_valid = (pci_mapreg_map(pa, VR_PCI_LOMEM,
1672 PCI_MAPREG_TYPE_MEM |
1673 PCI_MAPREG_MEM_TYPE_32BIT,
1674 0, &memt, &memh, NULL, NULL) == 0);
1675 #if defined(VR_USEIOSPACE)
1676 if (ioh_valid) {
1677 sc->vr_bst = iot;
1678 sc->vr_bsh = ioh;
1679 } else if (memh_valid) {
1680 sc->vr_bst = memt;
1681 sc->vr_bsh = memh;
1682 }
1683 #else
1684 if (memh_valid) {
1685 sc->vr_bst = memt;
1686 sc->vr_bsh = memh;
1687 } else if (ioh_valid) {
1688 sc->vr_bst = iot;
1689 sc->vr_bsh = ioh;
1690 }
1691 #endif
1692 else {
1693 printf(": unable to map device registers\n");
1694 return;
1695 }
1696
1697 /* Allocate interrupt */
1698 if (pci_intr_map(pa->pa_pc, pa->pa_intrtag, pa->pa_intrpin,
1699 pa->pa_intrline, &intrhandle)) {
1700 printf("%s: couldn't map interrupt\n",
1701 sc->vr_dev.dv_xname);
1702 return;
1703 }
1704 intrstr = pci_intr_string(pa->pa_pc, intrhandle);
1705 sc->vr_ih = pci_intr_establish(pa->pa_pc, intrhandle, IPL_NET,
1706 vr_intr, sc);
1707 if (sc->vr_ih == NULL) {
1708 printf("%s: couldn't establish interrupt",
1709 sc->vr_dev.dv_xname);
1710 if (intrstr != NULL)
1711 printf(" at %s", intrstr);
1712 printf("\n");
1713 }
1714 printf("%s: interrupting at %s\n",
1715 sc->vr_dev.dv_xname, intrstr);
1716 }
1717
1718 /* Reset the adapter. */
1719 vr_reset(sc);
1720
1721 /*
1722 * Get station address. The way the Rhine chips work,
1723 * you're not allowed to directly access the EEPROM once
1724 * they've been programmed a special way. Consequently,
1725 * we need to read the node address from the PAR0 and PAR1
1726 * registers.
1727 */
1728 VR_SETBIT(sc, VR_EECSR, VR_EECSR_LOAD);
1729 DELAY(200);
1730 for (i = 0; i < ETHER_ADDR_LEN; i++)
1731 eaddr[i] = CSR_READ_1(sc, VR_PAR0 + i);
1732
1733 /*
1734 * A Rhine chip was detected. Inform the world.
1735 */
1736 printf("%s: Ethernet address: %s\n",
1737 sc->vr_dev.dv_xname, ether_sprintf(eaddr));
1738
1739 bcopy(eaddr, sc->vr_enaddr, ETHER_ADDR_LEN);
1740
1741 sc->vr_dmat = pa->pa_dmat;
1742
1743 /*
1744 * Allocate the control data structures, and create and load
1745 * the DMA map for it.
1746 */
1747 if ((error = bus_dmamem_alloc(sc->vr_dmat,
1748 sizeof(struct vr_control_data), PAGE_SIZE, 0, &seg, 1, &rseg,
1749 0)) != 0) {
1750 printf("%s: unable to allocate control data, error = %d\n",
1751 sc->vr_dev.dv_xname, error);
1752 goto fail_0;
1753 }
1754
1755 if ((error = bus_dmamem_map(sc->vr_dmat, &seg, rseg,
1756 sizeof(struct vr_control_data), (caddr_t *)&sc->vr_control_data,
1757 BUS_DMA_COHERENT)) != 0) {
1758 printf("%s: unable to map control data, error = %d\n",
1759 sc->vr_dev.dv_xname, error);
1760 goto fail_1;
1761 }
1762
1763 if ((error = bus_dmamap_create(sc->vr_dmat,
1764 sizeof(struct vr_control_data), 1,
1765 sizeof(struct vr_control_data), 0, 0,
1766 &sc->vr_cddmamap)) != 0) {
1767 printf("%s: unable to create control data DMA map, "
1768 "error = %d\n", sc->vr_dev.dv_xname, error);
1769 goto fail_2;
1770 }
1771
1772 if ((error = bus_dmamap_load(sc->vr_dmat, sc->vr_cddmamap,
1773 sc->vr_control_data, sizeof(struct vr_control_data), NULL,
1774 0)) != 0) {
1775 printf("%s: unable to load control data DMA map, error = %d\n",
1776 sc->vr_dev.dv_xname, error);
1777 goto fail_3;
1778 }
1779
1780 /*
1781 * Create the transmit buffer DMA maps.
1782 */
1783 for (i = 0; i < VR_NTXDESC; i++) {
1784 if ((error = bus_dmamap_create(sc->vr_dmat, MCLBYTES,
1785 1, MCLBYTES, 0, 0,
1786 &VR_DSTX(sc, i)->ds_dmamap)) != 0) {
1787 printf("%s: unable to create tx DMA map %d, "
1788 "error = %d\n", sc->vr_dev.dv_xname, i, error);
1789 goto fail_4;
1790 }
1791 }
1792
1793 /*
1794 * Create the receive buffer DMA maps.
1795 */
1796 for (i = 0; i < VR_NRXDESC; i++) {
1797 if ((error = bus_dmamap_create(sc->vr_dmat, MCLBYTES, 1,
1798 MCLBYTES, 0, 0,
1799 &VR_DSRX(sc, i)->ds_dmamap)) != 0) {
1800 printf("%s: unable to create rx DMA map %d, "
1801 "error = %d\n", sc->vr_dev.dv_xname, i, error);
1802 goto fail_5;
1803 }
1804 VR_DSRX(sc, i)->ds_mbuf = NULL;
1805 }
1806
1807 ifp = &sc->vr_ec.ec_if;
1808 ifp->if_softc = sc;
1809 ifp->if_mtu = ETHERMTU;
1810 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1811 ifp->if_ioctl = vr_ioctl;
1812 ifp->if_start = vr_start;
1813 ifp->if_watchdog = vr_watchdog;
1814 ifp->if_baudrate = 10000000;
1815 bcopy(sc->vr_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
1816
1817 /*
1818 * Initialize MII/media info.
1819 */
1820 sc->vr_mii.mii_ifp = ifp;
1821 sc->vr_mii.mii_readreg = vr_mii_readreg;
1822 sc->vr_mii.mii_writereg = vr_mii_writereg;
1823 sc->vr_mii.mii_statchg = vr_mii_statchg;
1824 ifmedia_init(&sc->vr_mii.mii_media, 0, vr_ifmedia_upd, vr_ifmedia_sts);
1825 mii_phy_probe(&sc->vr_dev, &sc->vr_mii, 0xffffffff);
1826 if (LIST_FIRST(&sc->vr_mii.mii_phys) == NULL) {
1827 ifmedia_add(&sc->vr_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
1828 ifmedia_set(&sc->vr_mii.mii_media, IFM_ETHER|IFM_NONE);
1829 } else
1830 ifmedia_set(&sc->vr_mii.mii_media, IFM_ETHER|IFM_AUTO);
1831
1832 /*
1833 * Call MI attach routines.
1834 */
1835 if_attach(ifp);
1836 ether_ifattach(ifp, sc->vr_enaddr);
1837
1838 #if NBPFILTER > 0
1839 bpfattach(&sc->vr_ec.ec_if.if_bpf,
1840 ifp, DLT_EN10MB, sizeof (struct ether_header));
1841 #endif
1842
1843 sc->vr_ats = shutdownhook_establish(vr_shutdown, sc);
1844 if (sc->vr_ats == NULL)
1845 printf("%s: warning: couldn't establish shutdown hook\n",
1846 sc->vr_dev.dv_xname);
1847 return;
1848
1849 fail_5:
1850 for (i = 0; i < VR_NRXDESC; i++) {
1851 if (sc->vr_rxsoft[i].ds_dmamap != NULL)
1852 bus_dmamap_destroy(sc->vr_dmat,
1853 sc->vr_rxsoft[i].ds_dmamap);
1854 }
1855 fail_4:
1856 for (i = 0; i < VR_NTXDESC; i++) {
1857 if (sc->vr_txsoft[i].ds_dmamap != NULL)
1858 bus_dmamap_destroy(sc->vr_dmat,
1859 sc->vr_txsoft[i].ds_dmamap);
1860 }
1861 bus_dmamap_unload(sc->vr_dmat, sc->vr_cddmamap);
1862 fail_3:
1863 bus_dmamap_destroy(sc->vr_dmat, sc->vr_cddmamap);
1864 fail_2:
1865 bus_dmamem_unmap(sc->vr_dmat, (caddr_t)sc->vr_control_data,
1866 sizeof(struct vr_control_data));
1867 fail_1:
1868 bus_dmamem_free(sc->vr_dmat, &seg, rseg);
1869 fail_0:
1870 return;
1871 }
1872