if_vr.c revision 1.22 1 /* $NetBSD: if_vr.c,v 1.22 1999/05/18 23:52:58 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 void vr_init __P((void *));
307 static void vr_stop __P((struct vr_softc *));
308 static void vr_watchdog __P((struct ifnet *));
309 static void vr_tick __P((void *));
310
311 static int vr_ifmedia_upd __P((struct ifnet *));
312 static void vr_ifmedia_sts __P((struct ifnet *, struct ifmediareq *));
313
314 static void vr_mii_sync __P((struct vr_softc *));
315 static void vr_mii_send __P((struct vr_softc *, u_int32_t, int));
316 static int vr_mii_readreg __P((struct device *, int, int));
317 static void vr_mii_writereg __P((struct device *, int, int, int));
318 static void vr_mii_statchg __P((struct device *));
319
320 static u_int8_t vr_calchash __P((u_int8_t *));
321 static void vr_setmulti __P((struct vr_softc *));
322 static void vr_reset __P((struct vr_softc *));
323
324 #define VR_SETBIT(sc, reg, x) \
325 CSR_WRITE_1(sc, reg, \
326 CSR_READ_1(sc, reg) | x)
327
328 #define VR_CLRBIT(sc, reg, x) \
329 CSR_WRITE_1(sc, reg, \
330 CSR_READ_1(sc, reg) & ~x)
331
332 #define VR_SETBIT16(sc, reg, x) \
333 CSR_WRITE_2(sc, reg, \
334 CSR_READ_2(sc, reg) | x)
335
336 #define VR_CLRBIT16(sc, reg, x) \
337 CSR_WRITE_2(sc, reg, \
338 CSR_READ_2(sc, reg) & ~x)
339
340 #define VR_SETBIT32(sc, reg, x) \
341 CSR_WRITE_4(sc, reg, \
342 CSR_READ_4(sc, reg) | x)
343
344 #define VR_CLRBIT32(sc, reg, x) \
345 CSR_WRITE_4(sc, reg, \
346 CSR_READ_4(sc, reg) & ~x)
347
348 #define SIO_SET(x) \
349 CSR_WRITE_1(sc, VR_MIICMD, \
350 CSR_READ_1(sc, VR_MIICMD) | x)
351
352 #define SIO_CLR(x) \
353 CSR_WRITE_1(sc, VR_MIICMD, \
354 CSR_READ_1(sc, VR_MIICMD) & ~x)
355
356 /*
357 * Sync the PHYs by setting data bit and strobing the clock 32 times.
358 */
359 static void
360 vr_mii_sync(sc)
361 struct vr_softc *sc;
362 {
363 int i;
364
365 SIO_SET(VR_MIICMD_DIR|VR_MIICMD_DATAOUT);
366
367 for (i = 0; i < 32; i++) {
368 SIO_SET(VR_MIICMD_CLK);
369 DELAY(1);
370 SIO_CLR(VR_MIICMD_CLK);
371 DELAY(1);
372 }
373 }
374
375 /*
376 * Clock a series of bits through the MII.
377 */
378 static void
379 vr_mii_send(sc, bits, cnt)
380 struct vr_softc *sc;
381 u_int32_t bits;
382 int cnt;
383 {
384 int i;
385
386 SIO_CLR(VR_MIICMD_CLK);
387
388 for (i = (0x1 << (cnt - 1)); i; i >>= 1) {
389 if (bits & i) {
390 SIO_SET(VR_MIICMD_DATAOUT);
391 } else {
392 SIO_CLR(VR_MIICMD_DATAOUT);
393 }
394 DELAY(1);
395 SIO_CLR(VR_MIICMD_CLK);
396 DELAY(1);
397 SIO_SET(VR_MIICMD_CLK);
398 }
399 }
400
401 /*
402 * Read an PHY register through the MII.
403 */
404 static int
405 vr_mii_readreg(self, phy, reg)
406 struct device *self;
407 int phy, reg;
408 {
409 struct vr_softc *sc = (struct vr_softc *)self;
410 int i, ack, val = 0;
411
412 CSR_WRITE_1(sc, VR_MIICMD, 0);
413 VR_SETBIT(sc, VR_MIICMD, VR_MIICMD_DIRECTPGM);
414
415 /*
416 * Turn on data xmit.
417 */
418 SIO_SET(VR_MIICMD_DIR);
419
420 vr_mii_sync(sc);
421
422 /*
423 * Send command/address info.
424 */
425 vr_mii_send(sc, MII_COMMAND_START, 2);
426 vr_mii_send(sc, MII_COMMAND_READ, 2);
427 vr_mii_send(sc, phy, 5);
428 vr_mii_send(sc, reg, 5);
429
430 /* Idle bit */
431 SIO_CLR((VR_MIICMD_CLK|VR_MIICMD_DATAOUT));
432 DELAY(1);
433 SIO_SET(VR_MIICMD_CLK);
434 DELAY(1);
435
436 /* Turn off xmit. */
437 SIO_CLR(VR_MIICMD_DIR);
438
439 /* Check for ack */
440 SIO_CLR(VR_MIICMD_CLK);
441 DELAY(1);
442 SIO_SET(VR_MIICMD_CLK);
443 DELAY(1);
444 ack = CSR_READ_4(sc, VR_MIICMD) & VR_MIICMD_DATAIN;
445
446 /*
447 * Now try reading data bits. If the ack failed, we still
448 * need to clock through 16 cycles to keep the PHY(s) in sync.
449 */
450 if (ack) {
451 for (i = 0; i < 16; i++) {
452 SIO_CLR(VR_MIICMD_CLK);
453 DELAY(1);
454 SIO_SET(VR_MIICMD_CLK);
455 DELAY(1);
456 }
457 goto fail;
458 }
459
460 for (i = 0x8000; i; i >>= 1) {
461 SIO_CLR(VR_MIICMD_CLK);
462 DELAY(1);
463 if (!ack) {
464 if (CSR_READ_4(sc, VR_MIICMD) & VR_MIICMD_DATAIN)
465 val |= i;
466 DELAY(1);
467 }
468 SIO_SET(VR_MIICMD_CLK);
469 DELAY(1);
470 }
471
472 fail:
473
474 SIO_CLR(VR_MIICMD_CLK);
475 DELAY(1);
476 SIO_SET(VR_MIICMD_CLK);
477 DELAY(1);
478
479 return (val);
480 }
481
482 /*
483 * Write to a PHY register through the MII.
484 */
485 static void
486 vr_mii_writereg(self, phy, reg, val)
487 struct device *self;
488 int phy, reg, val;
489 {
490 struct vr_softc *sc = (struct vr_softc *)self;
491
492 CSR_WRITE_1(sc, VR_MIICMD, 0);
493 VR_SETBIT(sc, VR_MIICMD, VR_MIICMD_DIRECTPGM);
494
495 /*
496 * Turn on data output.
497 */
498 SIO_SET(VR_MIICMD_DIR);
499
500 vr_mii_sync(sc);
501
502 vr_mii_send(sc, MII_COMMAND_START, 2);
503 vr_mii_send(sc, MII_COMMAND_WRITE, 2);
504 vr_mii_send(sc, phy, 5);
505 vr_mii_send(sc, reg, 5);
506 vr_mii_send(sc, MII_COMMAND_ACK, 2);
507 vr_mii_send(sc, val, 16);
508
509 /* Idle bit. */
510 SIO_SET(VR_MIICMD_CLK);
511 DELAY(1);
512 SIO_CLR(VR_MIICMD_CLK);
513 DELAY(1);
514
515 /*
516 * Turn off xmit.
517 */
518 SIO_CLR(VR_MIICMD_DIR);
519 }
520
521 static void
522 vr_mii_statchg(self)
523 struct device *self;
524 {
525 struct vr_softc *sc = (struct vr_softc *)self;
526
527 /*
528 * In order to fiddle with the 'full-duplex' bit in the netconfig
529 * register, we first have to put the transmit and/or receive logic
530 * in the idle state.
531 */
532 VR_CLRBIT16(sc, VR_COMMAND, (VR_CMD_TX_ON|VR_CMD_RX_ON));
533
534 if (sc->vr_mii.mii_media_active & IFM_FDX)
535 VR_SETBIT16(sc, VR_COMMAND, VR_CMD_FULLDUPLEX);
536 else
537 VR_CLRBIT16(sc, VR_COMMAND, VR_CMD_FULLDUPLEX);
538
539 if (sc->vr_ec.ec_if.if_flags & IFF_RUNNING)
540 VR_SETBIT16(sc, VR_COMMAND, VR_CMD_TX_ON|VR_CMD_RX_ON);
541
542 /* XXX Update ifp->if_baudrate */
543 }
544
545 /*
546 * Calculate CRC of a multicast group address, return the lower 6 bits.
547 */
548 static u_int8_t
549 vr_calchash(addr)
550 u_int8_t *addr;
551 {
552 u_int32_t crc, carry;
553 int i, j;
554 u_int8_t c;
555
556 /* Compute CRC for the address value. */
557 crc = 0xFFFFFFFF; /* initial value */
558
559 for (i = 0; i < 6; i++) {
560 c = *(addr + i);
561 for (j = 0; j < 8; j++) {
562 carry = ((crc & 0x80000000) ? 1 : 0) ^ (c & 0x01);
563 crc <<= 1;
564 c >>= 1;
565 if (carry)
566 crc = (crc ^ 0x04c11db6) | carry;
567 }
568 }
569
570 /* return the filter bit position */
571 return ((crc >> 26) & 0x0000003F);
572 }
573
574 /*
575 * Program the 64-bit multicast hash filter.
576 */
577 static void
578 vr_setmulti(sc)
579 struct vr_softc *sc;
580 {
581 struct ifnet *ifp;
582 int h = 0;
583 u_int32_t hashes[2] = { 0, 0 };
584 struct ether_multistep step;
585 struct ether_multi *enm;
586 int mcnt = 0;
587 u_int8_t rxfilt;
588
589 ifp = &sc->vr_ec.ec_if;
590
591 rxfilt = CSR_READ_1(sc, VR_RXCFG);
592
593 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
594 rxfilt |= VR_RXCFG_RX_MULTI;
595 CSR_WRITE_1(sc, VR_RXCFG, rxfilt);
596 CSR_WRITE_4(sc, VR_MAR0, 0xFFFFFFFF);
597 CSR_WRITE_4(sc, VR_MAR1, 0xFFFFFFFF);
598 return;
599 }
600
601 /* first, zot all the existing hash bits */
602 CSR_WRITE_4(sc, VR_MAR0, 0);
603 CSR_WRITE_4(sc, VR_MAR1, 0);
604
605 /* now program new ones */
606 ETHER_FIRST_MULTI(step, &sc->vr_ec, enm);
607 while (enm != NULL) {
608 if (memcmp(enm->enm_addrlo, enm->enm_addrhi, 6) != 0)
609 continue;
610
611 h = vr_calchash(enm->enm_addrlo);
612
613 if (h < 32)
614 hashes[0] |= (1 << h);
615 else
616 hashes[1] |= (1 << (h - 32));
617 ETHER_NEXT_MULTI(step, enm);
618 mcnt++;
619 }
620
621 if (mcnt)
622 rxfilt |= VR_RXCFG_RX_MULTI;
623 else
624 rxfilt &= ~VR_RXCFG_RX_MULTI;
625
626 CSR_WRITE_4(sc, VR_MAR0, hashes[0]);
627 CSR_WRITE_4(sc, VR_MAR1, hashes[1]);
628 CSR_WRITE_1(sc, VR_RXCFG, rxfilt);
629 }
630
631 static void
632 vr_reset(sc)
633 struct vr_softc *sc;
634 {
635 int i;
636
637 VR_SETBIT16(sc, VR_COMMAND, VR_CMD_RESET);
638
639 for (i = 0; i < VR_TIMEOUT; i++) {
640 DELAY(10);
641 if (!(CSR_READ_2(sc, VR_COMMAND) & VR_CMD_RESET))
642 break;
643 }
644 if (i == VR_TIMEOUT)
645 printf("%s: reset never completed!\n",
646 sc->vr_dev.dv_xname);
647
648 /* Wait a little while for the chip to get its brains in order. */
649 DELAY(1000);
650 }
651
652 /*
653 * Initialize an RX descriptor and attach an MBUF cluster.
654 * Note: the length fields are only 11 bits wide, which means the
655 * largest size we can specify is 2047. This is important because
656 * MCLBYTES is 2048, so we have to subtract one otherwise we'll
657 * overflow the field and make a mess.
658 */
659 static int
660 vr_add_rxbuf(sc, i)
661 struct vr_softc *sc;
662 int i;
663 {
664 struct vr_descsoft *ds = VR_DSRX(sc, i);
665 struct mbuf *m_new;
666 int error;
667
668 MGETHDR(m_new, M_DONTWAIT, MT_DATA);
669 if (m_new == NULL)
670 return (ENOBUFS);
671
672 MCLGET(m_new, M_DONTWAIT);
673 if ((m_new->m_flags & M_EXT) == 0) {
674 m_freem(m_new);
675 return (ENOBUFS);
676 }
677
678 if (ds->ds_mbuf != NULL)
679 bus_dmamap_unload(sc->vr_dmat, ds->ds_dmamap);
680
681 ds->ds_mbuf = m_new;
682
683 error = bus_dmamap_load(sc->vr_dmat, ds->ds_dmamap,
684 m_new->m_ext.ext_buf, m_new->m_ext.ext_size, NULL, BUS_DMA_NOWAIT);
685 if (error) {
686 printf("%s: unable to load rx DMA map %d, error = %d\n",
687 sc->vr_dev.dv_xname, i, error);
688 panic("vr_add_rxbuf"); /* XXX */
689 }
690
691 bus_dmamap_sync(sc->vr_dmat, ds->ds_dmamap, 0,
692 ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
693
694 VR_INIT_RXDESC(sc, i);
695
696 return (0);
697 }
698
699 /*
700 * A frame has been uploaded: pass the resulting mbuf chain up to
701 * the higher level protocols.
702 */
703 static void
704 vr_rxeof(sc)
705 struct vr_softc *sc;
706 {
707 struct ether_header *eh;
708 struct mbuf *m;
709 struct ifnet *ifp;
710 struct vr_desc *d;
711 struct vr_descsoft *ds;
712 int i, total_len;
713 u_int32_t rxstat;
714
715 ifp = &sc->vr_ec.ec_if;
716
717 for (i = sc->vr_rxptr;; i = VR_NEXTRX(i)) {
718 d = VR_CDRX(sc, i);
719 ds = VR_DSRX(sc, i);
720
721 VR_CDRXSYNC(sc, i, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
722
723 rxstat = pcitoh(d->vr_status);
724
725 if (rxstat & VR_RXSTAT_OWN) {
726 /*
727 * We have processed all of the receive buffers.
728 */
729 break;
730 }
731
732 /*
733 * If an error occurs, update stats, clear the
734 * status word and leave the mbuf cluster in place:
735 * it should simply get re-used next time this descriptor
736 * comes up in the ring.
737 */
738 if (rxstat & VR_RXSTAT_RXERR) {
739 const char *errstr;
740
741 ifp->if_ierrors++;
742 switch (rxstat & 0x000000FF) {
743 case VR_RXSTAT_CRCERR:
744 errstr = "crc error";
745 break;
746 case VR_RXSTAT_FRAMEALIGNERR:
747 errstr = "frame alignment error";
748 break;
749 case VR_RXSTAT_FIFOOFLOW:
750 errstr = "FIFO overflow";
751 break;
752 case VR_RXSTAT_GIANT:
753 errstr = "received giant packet";
754 break;
755 case VR_RXSTAT_RUNT:
756 errstr = "received runt packet";
757 break;
758 case VR_RXSTAT_BUSERR:
759 errstr = "system bus error";
760 break;
761 case VR_RXSTAT_BUFFERR:
762 errstr = "rx buffer error";
763 break;
764 default:
765 errstr = "unknown rx error";
766 break;
767 }
768 printf("%s: receive error: %s\n", sc->vr_dev.dv_xname,
769 errstr);
770
771 VR_INIT_RXDESC(sc, i);
772
773 continue;
774 }
775
776 bus_dmamap_sync(sc->vr_dmat, ds->ds_dmamap, 0,
777 ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
778
779 /* No errors; receive the packet. */
780 total_len = VR_RXBYTES(pcitoh(d->vr_status));
781
782 /*
783 * XXX The VIA Rhine chip includes the CRC with every
784 * received frame, and there's no way to turn this
785 * behavior off (at least, I can't find anything in
786 * the manual that explains how to do it) so we have
787 * to trim off the CRC manually.
788 */
789 total_len -= ETHER_CRC_LEN;
790
791 #ifdef __NO_STRICT_ALIGNMENT
792 /*
793 * Try to conjure up a new mbuf cluster. If that
794 * fails, it means we have an out of memory condition and
795 * should leave the buffer in place and continue. This will
796 * result in a lost packet, but there's little else we
797 * can do in this situation.
798 */
799 m = ds->ds_mbuf;
800 if (vr_add_rxbuf(sc, i) == ENOBUFS) {
801 ifp->if_ierrors++;
802 VR_INIT_RXDESC(sc, i);
803 bus_dmamap_sync(sc->vr_dmat, ds->ds_dmamap, 0,
804 ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
805 continue;
806 }
807 #else
808 /*
809 * The Rhine's packet buffers must be 4-byte aligned.
810 * But this means that the data after the Ethernet header
811 * is misaligned. We must allocate a new buffer and
812 * copy the data, shifted forward 2 bytes.
813 */
814 MGETHDR(m, M_DONTWAIT, MT_DATA);
815 if (m == NULL) {
816 dropit:
817 ifp->if_ierrors++;
818 VR_INIT_RXDESC(sc, i);
819 bus_dmamap_sync(sc->vr_dmat, ds->ds_dmamap, 0,
820 ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
821 continue;
822 }
823 if (total_len > (MHLEN - 2)) {
824 MCLGET(m, M_DONTWAIT);
825 if ((m->m_flags & M_EXT) == 0) {
826 m_freem(m);
827 goto dropit;
828 }
829 }
830 m->m_data += 2;
831
832 /*
833 * Note that we use clusters for incoming frames, so the
834 * buffer is virtually contiguous.
835 */
836 memcpy(mtod(m, caddr_t), mtod(ds->ds_mbuf, caddr_t),
837 total_len);
838
839 /* Allow the recieve descriptor to continue using its mbuf. */
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 #endif /* __NO_STRICT_ALIGNMENT */
844
845 ifp->if_ipackets++;
846 eh = mtod(m, struct ether_header *);
847 m->m_pkthdr.rcvif = ifp;
848 m->m_pkthdr.len = m->m_len = total_len;
849 #if NBPFILTER > 0
850 /*
851 * Handle BPF listeners. Let the BPF user see the packet, but
852 * don't pass it up to the ether_input() layer unless it's
853 * a broadcast packet, multicast packet, matches our ethernet
854 * address or the interface is in promiscuous mode.
855 */
856 if (ifp->if_bpf) {
857 bpf_mtap(ifp->if_bpf, m);
858 if ((ifp->if_flags & IFF_PROMISC) != 0 &&
859 (rxstat & (VR_RXSTAT_RX_PHYS | VR_RXSTAT_RX_BROAD |
860 VR_RXSTAT_RX_MULTI)) == 0) {
861 m_freem(m);
862 continue;
863 }
864 }
865 #endif
866 /* Pass it on. */
867 (*ifp->if_input)(ifp, m);
868 }
869
870 /* Update the receive pointer. */
871 sc->vr_rxptr = i;
872 }
873
874 void
875 vr_rxeoc(sc)
876 struct vr_softc *sc;
877 {
878
879 vr_rxeof(sc);
880 VR_CLRBIT16(sc, VR_COMMAND, VR_CMD_RX_ON);
881 CSR_WRITE_4(sc, VR_RXADDR, VR_CDRXADDR(sc, sc->vr_rxptr));
882 VR_SETBIT16(sc, VR_COMMAND, VR_CMD_RX_ON);
883 VR_SETBIT16(sc, VR_COMMAND, VR_CMD_RX_GO);
884 }
885
886 /*
887 * A frame was downloaded to the chip. It's safe for us to clean up
888 * the list buffers.
889 */
890 static void
891 vr_txeof(sc)
892 struct vr_softc *sc;
893 {
894 struct ifnet *ifp = &sc->vr_ec.ec_if;
895 struct vr_desc *d;
896 struct vr_descsoft *ds;
897 u_int32_t txstat;
898 int i;
899
900 ifp->if_flags &= ~IFF_OACTIVE;
901
902 /*
903 * Go through our tx list and free mbufs for those
904 * frames that have been transmitted.
905 */
906 for (i = sc->vr_txdirty; sc->vr_txpending != 0;
907 i = VR_NEXTTX(i), sc->vr_txpending--) {
908 d = VR_CDTX(sc, i);
909 ds = VR_DSTX(sc, i);
910
911 VR_CDTXSYNC(sc, i, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
912
913 txstat = pcitoh(d->vr_status);
914 if (txstat & VR_TXSTAT_OWN)
915 break;
916
917 bus_dmamap_sync(sc->vr_dmat, ds->ds_dmamap,
918 0, ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
919 bus_dmamap_unload(sc->vr_dmat, ds->ds_dmamap);
920 m_freem(ds->ds_mbuf);
921 ds->ds_mbuf = NULL;
922
923 if (txstat & VR_TXSTAT_ERRSUM) {
924 ifp->if_oerrors++;
925 if (txstat & VR_TXSTAT_DEFER)
926 ifp->if_collisions++;
927 if (txstat & VR_TXSTAT_LATECOLL)
928 ifp->if_collisions++;
929 }
930
931 ifp->if_collisions += (txstat & VR_TXSTAT_COLLCNT) >> 3;
932 ifp->if_opackets++;
933 }
934
935 /* Update the dirty transmit buffer pointer. */
936 sc->vr_txdirty = i;
937
938 /*
939 * Cancel the watchdog timer if there are no pending
940 * transmissions.
941 */
942 if (sc->vr_txpending == 0)
943 ifp->if_timer = 0;
944 }
945
946 static int
947 vr_intr(arg)
948 void *arg;
949 {
950 struct vr_softc *sc;
951 struct ifnet *ifp;
952 u_int16_t status;
953 int handled = 0, dotx = 0;
954
955 sc = arg;
956 ifp = &sc->vr_ec.ec_if;
957
958 /* Suppress unwanted interrupts. */
959 if ((ifp->if_flags & IFF_UP) == 0) {
960 vr_stop(sc);
961 return (0);
962 }
963
964 /* Disable interrupts. */
965 CSR_WRITE_2(sc, VR_IMR, 0x0000);
966
967 for (;;) {
968 status = CSR_READ_2(sc, VR_ISR);
969 if (status)
970 CSR_WRITE_2(sc, VR_ISR, status);
971
972 if ((status & VR_INTRS) == 0)
973 break;
974
975 handled = 1;
976
977 if (status & VR_ISR_RX_OK)
978 vr_rxeof(sc);
979
980 if (status &
981 (VR_ISR_RX_ERR | VR_ISR_RX_NOBUF | VR_ISR_RX_OFLOW |
982 VR_ISR_RX_DROPPED))
983 vr_rxeoc(sc);
984
985 if (status & VR_ISR_TX_OK) {
986 dotx = 1;
987 vr_txeof(sc);
988 }
989
990 if (status & (VR_ISR_TX_UNDERRUN | VR_ISR_TX_ABRT)) {
991 if (status & VR_ISR_TX_UNDERRUN)
992 printf("%s: transmit underrun\n",
993 sc->vr_dev.dv_xname);
994 if (status & VR_ISR_TX_ABRT)
995 printf("%s: transmit aborted\n",
996 sc->vr_dev.dv_xname);
997 ifp->if_oerrors++;
998 dotx = 1;
999 vr_txeof(sc);
1000 if (sc->vr_txpending) {
1001 VR_SETBIT16(sc, VR_COMMAND, VR_CMD_TX_ON);
1002 VR_SETBIT16(sc, VR_COMMAND, VR_CMD_TX_GO);
1003 }
1004 }
1005
1006 if (status & VR_ISR_BUSERR) {
1007 printf("%s: PCI bus error\n", sc->vr_dev.dv_xname);
1008 /* vr_init() calls vr_start() */
1009 dotx = 0;
1010 vr_init(sc);
1011 }
1012 }
1013
1014 /* Re-enable interrupts. */
1015 CSR_WRITE_2(sc, VR_IMR, VR_INTRS);
1016
1017 if (dotx)
1018 vr_start(ifp);
1019
1020 return (handled);
1021 }
1022
1023 /*
1024 * Main transmit routine. To avoid having to do mbuf copies, we put pointers
1025 * to the mbuf data regions directly in the transmit lists. We also save a
1026 * copy of the pointers since the transmit list fragment pointers are
1027 * physical addresses.
1028 */
1029 static void
1030 vr_start(ifp)
1031 struct ifnet *ifp;
1032 {
1033 struct vr_softc *sc = ifp->if_softc;
1034 struct mbuf *m0, *m;
1035 struct vr_desc *d;
1036 struct vr_descsoft *ds;
1037 int error, firsttx, nexttx, opending;
1038
1039 /*
1040 * Remember the previous txpending and the first transmit
1041 * descriptor we use.
1042 */
1043 opending = sc->vr_txpending;
1044 firsttx = VR_NEXTTX(sc->vr_txlast);
1045
1046 /*
1047 * Loop through the send queue, setting up transmit descriptors
1048 * until we drain the queue, or use up all available transmit
1049 * descriptors.
1050 */
1051 while (sc->vr_txpending < VR_NTXDESC) {
1052 /*
1053 * Grab a packet off the queue.
1054 */
1055 IF_DEQUEUE(&ifp->if_snd, m0);
1056 if (m0 == NULL)
1057 break;
1058
1059 /*
1060 * Get the next available transmit descriptor.
1061 */
1062 nexttx = VR_NEXTTX(sc->vr_txlast);
1063 d = VR_CDTX(sc, nexttx);
1064 ds = VR_DSTX(sc, nexttx);
1065
1066 /*
1067 * Load the DMA map. If this fails, the packet didn't
1068 * fit in one DMA segment, and we need to copy. Note,
1069 * the packet must also be aligned.
1070 */
1071 if ((mtod(m0, bus_addr_t) & 3) != 0 ||
1072 bus_dmamap_load_mbuf(sc->vr_dmat, ds->ds_dmamap, m0,
1073 BUS_DMA_NOWAIT) != 0) {
1074 MGETHDR(m, M_DONTWAIT, MT_DATA);
1075 if (m == NULL) {
1076 printf("%s: unable to allocate Tx mbuf\n",
1077 sc->vr_dev.dv_xname);
1078 IF_PREPEND(&ifp->if_snd, m0);
1079 break;
1080 }
1081 if (m0->m_pkthdr.len > MHLEN) {
1082 MCLGET(m, M_DONTWAIT);
1083 if ((m->m_flags & M_EXT) == 0) {
1084 printf("%s: unable to allocate Tx "
1085 "cluster\n", sc->vr_dev.dv_xname);
1086 m_freem(m);
1087 IF_PREPEND(&ifp->if_snd, m0);
1088 break;
1089 }
1090 }
1091 m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t));
1092 m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
1093 m_freem(m0);
1094 m0 = m;
1095 error = bus_dmamap_load_mbuf(sc->vr_dmat,
1096 ds->ds_dmamap, m0, BUS_DMA_NOWAIT);
1097 if (error) {
1098 printf("%s: unable to load Tx buffer, "
1099 "error = %d\n", sc->vr_dev.dv_xname, error);
1100 IF_PREPEND(&ifp->if_snd, m0);
1101 break;
1102 }
1103 }
1104
1105 /* Sync the DMA map. */
1106 bus_dmamap_sync(sc->vr_dmat, ds->ds_dmamap, 0,
1107 ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_PREWRITE);
1108
1109 /*
1110 * Store a pointer to the packet so we can free it later.
1111 */
1112 ds->ds_mbuf = m0;
1113
1114 #if NBPFILTER > 0
1115 /*
1116 * If there's a BPF listener, bounce a copy of this frame
1117 * to him.
1118 */
1119 if (ifp->if_bpf)
1120 bpf_mtap(ifp->if_bpf, m0);
1121 #endif
1122
1123 /*
1124 * Fill in the transmit descriptor. The Rhine
1125 * doesn't auto-pad, so we have to do this ourselves.
1126 */
1127 d->vr_data = htopci(ds->ds_dmamap->dm_segs[0].ds_addr);
1128 d->vr_ctl = htopci(m0->m_pkthdr.len < VR_MIN_FRAMELEN ?
1129 VR_MIN_FRAMELEN : m0->m_pkthdr.len);
1130 d->vr_ctl |=
1131 htopci(VR_TXCTL_TLINK|VR_TXCTL_FIRSTFRAG|VR_TXCTL_LASTFRAG);
1132
1133 /*
1134 * If this is the first descriptor we're enqueuing,
1135 * don't give it to the Rhine yet. That could cause
1136 * a race condition. We'll do it below.
1137 */
1138 if (nexttx == firsttx)
1139 d->vr_status = 0;
1140 else
1141 d->vr_status = htopci(VR_TXSTAT_OWN);
1142
1143 VR_CDTXSYNC(sc, nexttx,
1144 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1145
1146 /* Advance the tx pointer. */
1147 sc->vr_txpending++;
1148 sc->vr_txlast = nexttx;
1149 }
1150
1151 if (sc->vr_txpending == VR_NTXDESC) {
1152 /* No more slots left; notify upper layer. */
1153 ifp->if_flags |= IFF_OACTIVE;
1154 }
1155
1156 if (sc->vr_txpending != opending) {
1157 /*
1158 * We enqueued packets. If the transmitter was idle,
1159 * reset the txdirty pointer.
1160 */
1161 if (opending == 0)
1162 sc->vr_txdirty = firsttx;
1163
1164 /*
1165 * Cause a transmit interrupt to happen on the
1166 * last packet we enqueued.
1167 */
1168 VR_CDTX(sc, sc->vr_txlast)->vr_ctl |= htopci(VR_TXCTL_FINT);
1169 VR_CDTXSYNC(sc, sc->vr_txlast,
1170 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1171
1172 /*
1173 * The entire packet chain is set up. Give the
1174 * first descriptor to the Rhine now.
1175 */
1176 VR_CDTX(sc, firsttx)->vr_status = htopci(VR_TXSTAT_OWN);
1177 VR_CDTXSYNC(sc, firsttx,
1178 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1179
1180 /* Start the transmitter. */
1181 VR_SETBIT16(sc, VR_COMMAND, VR_CMD_TX_ON|VR_CMD_TX_GO);
1182
1183 /* Set the watchdog timer in case the chip flakes out. */
1184 ifp->if_timer = 5;
1185 }
1186 }
1187
1188 /*
1189 * Initialize the interface. Must be called at splnet.
1190 */
1191 static void
1192 vr_init(xsc)
1193 void *xsc;
1194 {
1195 struct vr_softc *sc = xsc;
1196 struct ifnet *ifp = &sc->vr_ec.ec_if;
1197 struct vr_desc *d;
1198 int i;
1199
1200 /* Cancel pending I/O. */
1201 vr_stop(sc);
1202
1203 /* Reset the Rhine to a known state. */
1204 vr_reset(sc);
1205
1206 VR_CLRBIT(sc, VR_RXCFG, VR_RXCFG_RX_THRESH);
1207 VR_SETBIT(sc, VR_RXCFG, VR_RXTHRESH_STORENFWD);
1208
1209 VR_CLRBIT(sc, VR_TXCFG, VR_TXCFG_TX_THRESH);
1210 VR_SETBIT(sc, VR_TXCFG, VR_TXTHRESH_STORENFWD);
1211
1212 /*
1213 * Initialize the transmit desciptor ring. txlast is initialized
1214 * to the end of the list so that it will wrap around to the first
1215 * descriptor when the first packet is transmitted.
1216 */
1217 for (i = 0; i < VR_NTXDESC; i++) {
1218 d = VR_CDTX(sc, i);
1219 memset(d, 0, sizeof(struct vr_desc));
1220 d->vr_next = htopci(VR_CDTXADDR(sc, VR_NEXTTX(i)));
1221 VR_CDTXSYNC(sc, i, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1222 }
1223 sc->vr_txpending = 0;
1224 sc->vr_txdirty = 0;
1225 sc->vr_txlast = VR_NTXDESC - 1;
1226
1227 /*
1228 * Initialize the receive descriptor ring. The buffers are
1229 * already allocated.
1230 */
1231 for (i = 0; i < VR_NRXDESC; i++)
1232 VR_INIT_RXDESC(sc, i);
1233 sc->vr_rxptr = 0;
1234
1235 /* If we want promiscuous mode, set the allframes bit. */
1236 if (ifp->if_flags & IFF_PROMISC)
1237 VR_SETBIT(sc, VR_RXCFG, VR_RXCFG_RX_PROMISC);
1238 else
1239 VR_CLRBIT(sc, VR_RXCFG, VR_RXCFG_RX_PROMISC);
1240
1241 /* Set capture broadcast bit to capture broadcast frames. */
1242 if (ifp->if_flags & IFF_BROADCAST)
1243 VR_SETBIT(sc, VR_RXCFG, VR_RXCFG_RX_BROAD);
1244 else
1245 VR_CLRBIT(sc, VR_RXCFG, VR_RXCFG_RX_BROAD);
1246
1247 /* Program the multicast filter, if necessary. */
1248 vr_setmulti(sc);
1249
1250 /* Give the transmit and recieve rings to the Rhine. */
1251 CSR_WRITE_4(sc, VR_RXADDR, VR_CDRXADDR(sc, sc->vr_rxptr));
1252 CSR_WRITE_4(sc, VR_TXADDR, VR_CDTXADDR(sc, VR_NEXTTX(sc->vr_txlast)));
1253
1254 /* Set current media. */
1255 mii_mediachg(&sc->vr_mii);
1256
1257 /* Enable receiver and transmitter. */
1258 CSR_WRITE_2(sc, VR_COMMAND, VR_CMD_TX_NOPOLL|VR_CMD_START|
1259 VR_CMD_TX_ON|VR_CMD_RX_ON|
1260 VR_CMD_RX_GO);
1261
1262 /* Enable interrupts. */
1263 CSR_WRITE_2(sc, VR_ISR, 0xFFFF);
1264 CSR_WRITE_2(sc, VR_IMR, VR_INTRS);
1265
1266 ifp->if_flags |= IFF_RUNNING;
1267 ifp->if_flags &= ~IFF_OACTIVE;
1268
1269 /* Start one second timer. */
1270 timeout(vr_tick, sc, hz);
1271
1272 /* Attempt to start output on the interface. */
1273 vr_start(ifp);
1274 }
1275
1276 /*
1277 * Set media options.
1278 */
1279 static int
1280 vr_ifmedia_upd(ifp)
1281 struct ifnet *ifp;
1282 {
1283 struct vr_softc *sc = ifp->if_softc;
1284
1285 if (ifp->if_flags & IFF_UP)
1286 mii_mediachg(&sc->vr_mii);
1287 return (0);
1288 }
1289
1290 /*
1291 * Report current media status.
1292 */
1293 static void
1294 vr_ifmedia_sts(ifp, ifmr)
1295 struct ifnet *ifp;
1296 struct ifmediareq *ifmr;
1297 {
1298 struct vr_softc *sc = ifp->if_softc;
1299
1300 mii_pollstat(&sc->vr_mii);
1301 ifmr->ifm_status = sc->vr_mii.mii_media_status;
1302 ifmr->ifm_active = sc->vr_mii.mii_media_active;
1303 }
1304
1305 static int
1306 vr_ioctl(ifp, command, data)
1307 struct ifnet *ifp;
1308 u_long command;
1309 caddr_t data;
1310 {
1311 struct vr_softc *sc = ifp->if_softc;
1312 struct ifreq *ifr = (struct ifreq *)data;
1313 struct ifaddr *ifa = (struct ifaddr *)data;
1314 int s, error = 0;
1315
1316 s = splnet();
1317
1318 switch (command) {
1319 case SIOCSIFADDR:
1320 ifp->if_flags |= IFF_UP;
1321
1322 switch (ifa->ifa_addr->sa_family) {
1323 #ifdef INET
1324 case AF_INET:
1325 vr_init(sc);
1326 arp_ifinit(ifp, ifa);
1327 break;
1328 #endif /* INET */
1329 default:
1330 vr_init(sc);
1331 break;
1332 }
1333 break;
1334
1335 case SIOCGIFADDR:
1336 bcopy((caddr_t) sc->vr_enaddr,
1337 (caddr_t) ((struct sockaddr *)&ifr->ifr_data)->sa_data,
1338 ETHER_ADDR_LEN);
1339 break;
1340
1341 case SIOCSIFMTU:
1342 if (ifr->ifr_mtu > ETHERMTU)
1343 error = EINVAL;
1344 else
1345 ifp->if_mtu = ifr->ifr_mtu;
1346 break;
1347
1348 case SIOCSIFFLAGS:
1349 if ((ifp->if_flags & IFF_UP) == 0 &&
1350 (ifp->if_flags & IFF_RUNNING) != 0) {
1351 /*
1352 * If interface is marked down and it is running, then
1353 * stop it.
1354 */
1355 vr_stop(sc);
1356 } else if ((ifp->if_flags & IFF_UP) != 0 &&
1357 (ifp->if_flags & IFF_RUNNING) == 0) {
1358 /*
1359 * If interface is marked up and it is stopped, then
1360 * start it.
1361 */
1362 vr_init(sc);
1363 } else if ((ifp->if_flags & IFF_UP) != 0) {
1364 /*
1365 * Reset the interface to pick up changes in any other
1366 * flags that affect the hardware state.
1367 */
1368 vr_init(sc);
1369 }
1370 break;
1371
1372 case SIOCADDMULTI:
1373 case SIOCDELMULTI:
1374 if (command == SIOCADDMULTI)
1375 error = ether_addmulti(ifr, &sc->vr_ec);
1376 else
1377 error = ether_delmulti(ifr, &sc->vr_ec);
1378
1379 if (error == ENETRESET) {
1380 /*
1381 * Multicast list has changed; set the hardware filter
1382 * accordingly.
1383 */
1384 vr_setmulti(sc);
1385 error = 0;
1386 }
1387 break;
1388
1389 case SIOCGIFMEDIA:
1390 case SIOCSIFMEDIA:
1391 error = ifmedia_ioctl(ifp, ifr, &sc->vr_mii.mii_media, command);
1392 break;
1393
1394 default:
1395 error = EINVAL;
1396 break;
1397 }
1398
1399 splx(s);
1400 return (error);
1401 }
1402
1403 static void
1404 vr_watchdog(ifp)
1405 struct ifnet *ifp;
1406 {
1407 struct vr_softc *sc = ifp->if_softc;
1408
1409 printf("%s: device timeout\n", sc->vr_dev.dv_xname);
1410 ifp->if_oerrors++;
1411
1412 vr_init(sc);
1413 }
1414
1415 /*
1416 * One second timer, used to tick MII.
1417 */
1418 static void
1419 vr_tick(arg)
1420 void *arg;
1421 {
1422 struct vr_softc *sc = arg;
1423 int s;
1424
1425 s = splnet();
1426 mii_tick(&sc->vr_mii);
1427 splx(s);
1428
1429 timeout(vr_tick, sc, hz);
1430 }
1431
1432 /*
1433 * Stop the adapter and free any mbufs allocated to the
1434 * transmit lists.
1435 */
1436 static void
1437 vr_stop(sc)
1438 struct vr_softc *sc;
1439 {
1440 struct vr_descsoft *ds;
1441 struct ifnet *ifp;
1442 int i;
1443
1444 /* Cancel one second timer. */
1445 untimeout(vr_tick, sc);
1446
1447 ifp = &sc->vr_ec.ec_if;
1448 ifp->if_timer = 0;
1449
1450 VR_SETBIT16(sc, VR_COMMAND, VR_CMD_STOP);
1451 VR_CLRBIT16(sc, VR_COMMAND, (VR_CMD_RX_ON|VR_CMD_TX_ON));
1452 CSR_WRITE_2(sc, VR_IMR, 0x0000);
1453 CSR_WRITE_4(sc, VR_TXADDR, 0x00000000);
1454 CSR_WRITE_4(sc, VR_RXADDR, 0x00000000);
1455
1456 /*
1457 * Release any queued transmit buffers.
1458 */
1459 for (i = 0; i < VR_NTXDESC; i++) {
1460 ds = VR_DSTX(sc, i);
1461 if (ds->ds_mbuf != NULL) {
1462 bus_dmamap_unload(sc->vr_dmat, ds->ds_dmamap);
1463 m_freem(ds->ds_mbuf);
1464 ds->ds_mbuf = NULL;
1465 }
1466 }
1467
1468 /*
1469 * Mark the interface down and cancel the watchdog timer.
1470 */
1471 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1472 ifp->if_timer = 0;
1473 }
1474
1475 static struct vr_type *vr_lookup __P((struct pci_attach_args *));
1476 static int vr_probe __P((struct device *, struct cfdata *, void *));
1477 static void vr_attach __P((struct device *, struct device *, void *));
1478 static void vr_shutdown __P((void *));
1479
1480 struct cfattach vr_ca = {
1481 sizeof (struct vr_softc), vr_probe, vr_attach
1482 };
1483
1484 static struct vr_type *
1485 vr_lookup(pa)
1486 struct pci_attach_args *pa;
1487 {
1488 struct vr_type *vrt;
1489
1490 for (vrt = vr_devs; vrt->vr_name != NULL; vrt++) {
1491 if (PCI_VENDOR(pa->pa_id) == vrt->vr_vid &&
1492 PCI_PRODUCT(pa->pa_id) == vrt->vr_did)
1493 return (vrt);
1494 }
1495 return (NULL);
1496 }
1497
1498 static int
1499 vr_probe(parent, match, aux)
1500 struct device *parent;
1501 struct cfdata *match;
1502 void *aux;
1503 {
1504 struct pci_attach_args *pa = (struct pci_attach_args *)aux;
1505
1506 if (vr_lookup(pa) != NULL)
1507 return (1);
1508
1509 return (0);
1510 }
1511
1512 /*
1513 * Stop all chip I/O so that the kernel's probe routines don't
1514 * get confused by errant DMAs when rebooting.
1515 */
1516 static void
1517 vr_shutdown(arg)
1518 void *arg;
1519 {
1520 struct vr_softc *sc = (struct vr_softc *)arg;
1521
1522 vr_stop(sc);
1523 }
1524
1525 /*
1526 * Attach the interface. Allocate softc structures, do ifmedia
1527 * setup and ethernet/BPF attach.
1528 */
1529 static void
1530 vr_attach(parent, self, aux)
1531 struct device *parent;
1532 struct device *self;
1533 void *aux;
1534 {
1535 struct vr_softc *sc = (struct vr_softc *) self;
1536 struct pci_attach_args *pa = (struct pci_attach_args *) aux;
1537 bus_dma_segment_t seg;
1538 struct vr_type *vrt;
1539 u_int32_t command;
1540 struct ifnet *ifp;
1541 u_char eaddr[ETHER_ADDR_LEN];
1542 int i, rseg, error;
1543
1544 #define PCI_CONF_WRITE(r, v) pci_conf_write(pa->pa_pc, pa->pa_tag, (r), (v))
1545 #define PCI_CONF_READ(r) pci_conf_read(pa->pa_pc, pa->pa_tag, (r))
1546
1547 vrt = vr_lookup(pa);
1548 if (vrt == NULL) {
1549 printf("\n");
1550 panic("vr_attach: impossible");
1551 }
1552
1553 printf(": %s Ethernet\n", vrt->vr_name);
1554
1555 /*
1556 * Handle power management nonsense.
1557 */
1558
1559 command = PCI_CONF_READ(VR_PCI_CAPID) & 0x000000FF;
1560 if (command == 0x01) {
1561 command = PCI_CONF_READ(VR_PCI_PWRMGMTCTRL);
1562 if (command & VR_PSTATE_MASK) {
1563 u_int32_t iobase, membase, irq;
1564
1565 /* Save important PCI config data. */
1566 iobase = PCI_CONF_READ(VR_PCI_LOIO);
1567 membase = PCI_CONF_READ(VR_PCI_LOMEM);
1568 irq = PCI_CONF_READ(VR_PCI_INTLINE);
1569
1570 /* Reset the power state. */
1571 printf("%s: chip is in D%d power mode "
1572 "-- setting to D0\n",
1573 sc->vr_dev.dv_xname, command & VR_PSTATE_MASK);
1574 command &= 0xFFFFFFFC;
1575 PCI_CONF_WRITE(VR_PCI_PWRMGMTCTRL, command);
1576
1577 /* Restore PCI config data. */
1578 PCI_CONF_WRITE(VR_PCI_LOIO, iobase);
1579 PCI_CONF_WRITE(VR_PCI_LOMEM, membase);
1580 PCI_CONF_WRITE(VR_PCI_INTLINE, irq);
1581 }
1582 }
1583
1584 /* Make sure bus mastering is enabled. */
1585 command = PCI_CONF_READ(PCI_COMMAND_STATUS_REG);
1586 command |= PCI_COMMAND_MASTER_ENABLE;
1587 PCI_CONF_WRITE(PCI_COMMAND_STATUS_REG, command);
1588
1589 /*
1590 * Map control/status registers.
1591 */
1592 {
1593 bus_space_tag_t iot, memt;
1594 bus_space_handle_t ioh, memh;
1595 int ioh_valid, memh_valid;
1596 pci_intr_handle_t intrhandle;
1597 const char *intrstr;
1598
1599 ioh_valid = (pci_mapreg_map(pa, VR_PCI_LOIO,
1600 PCI_MAPREG_TYPE_IO, 0,
1601 &iot, &ioh, NULL, NULL) == 0);
1602 memh_valid = (pci_mapreg_map(pa, VR_PCI_LOMEM,
1603 PCI_MAPREG_TYPE_MEM |
1604 PCI_MAPREG_MEM_TYPE_32BIT,
1605 0, &memt, &memh, NULL, NULL) == 0);
1606 #if defined(VR_USEIOSPACE)
1607 if (ioh_valid) {
1608 sc->vr_bst = iot;
1609 sc->vr_bsh = ioh;
1610 } else if (memh_valid) {
1611 sc->vr_bst = memt;
1612 sc->vr_bsh = memh;
1613 }
1614 #else
1615 if (memh_valid) {
1616 sc->vr_bst = memt;
1617 sc->vr_bsh = memh;
1618 } else if (ioh_valid) {
1619 sc->vr_bst = iot;
1620 sc->vr_bsh = ioh;
1621 }
1622 #endif
1623 else {
1624 printf(": unable to map device registers\n");
1625 return;
1626 }
1627
1628 /* Allocate interrupt */
1629 if (pci_intr_map(pa->pa_pc, pa->pa_intrtag, pa->pa_intrpin,
1630 pa->pa_intrline, &intrhandle)) {
1631 printf("%s: couldn't map interrupt\n",
1632 sc->vr_dev.dv_xname);
1633 return;
1634 }
1635 intrstr = pci_intr_string(pa->pa_pc, intrhandle);
1636 sc->vr_ih = pci_intr_establish(pa->pa_pc, intrhandle, IPL_NET,
1637 vr_intr, sc);
1638 if (sc->vr_ih == NULL) {
1639 printf("%s: couldn't establish interrupt",
1640 sc->vr_dev.dv_xname);
1641 if (intrstr != NULL)
1642 printf(" at %s", intrstr);
1643 printf("\n");
1644 }
1645 printf("%s: interrupting at %s\n",
1646 sc->vr_dev.dv_xname, intrstr);
1647 }
1648
1649 /* Reset the adapter. */
1650 vr_reset(sc);
1651
1652 /*
1653 * Get station address. The way the Rhine chips work,
1654 * you're not allowed to directly access the EEPROM once
1655 * they've been programmed a special way. Consequently,
1656 * we need to read the node address from the PAR0 and PAR1
1657 * registers.
1658 */
1659 VR_SETBIT(sc, VR_EECSR, VR_EECSR_LOAD);
1660 DELAY(200);
1661 for (i = 0; i < ETHER_ADDR_LEN; i++)
1662 eaddr[i] = CSR_READ_1(sc, VR_PAR0 + i);
1663
1664 /*
1665 * A Rhine chip was detected. Inform the world.
1666 */
1667 printf("%s: Ethernet address: %s\n",
1668 sc->vr_dev.dv_xname, ether_sprintf(eaddr));
1669
1670 bcopy(eaddr, sc->vr_enaddr, ETHER_ADDR_LEN);
1671
1672 sc->vr_dmat = pa->pa_dmat;
1673
1674 /*
1675 * Allocate the control data structures, and create and load
1676 * the DMA map for it.
1677 */
1678 if ((error = bus_dmamem_alloc(sc->vr_dmat,
1679 sizeof(struct vr_control_data), PAGE_SIZE, 0, &seg, 1, &rseg,
1680 0)) != 0) {
1681 printf("%s: unable to allocate control data, error = %d\n",
1682 sc->vr_dev.dv_xname, error);
1683 goto fail_0;
1684 }
1685
1686 if ((error = bus_dmamem_map(sc->vr_dmat, &seg, rseg,
1687 sizeof(struct vr_control_data), (caddr_t *)&sc->vr_control_data,
1688 BUS_DMA_COHERENT)) != 0) {
1689 printf("%s: unable to map control data, error = %d\n",
1690 sc->vr_dev.dv_xname, error);
1691 goto fail_1;
1692 }
1693
1694 if ((error = bus_dmamap_create(sc->vr_dmat,
1695 sizeof(struct vr_control_data), 1,
1696 sizeof(struct vr_control_data), 0, 0,
1697 &sc->vr_cddmamap)) != 0) {
1698 printf("%s: unable to create control data DMA map, "
1699 "error = %d\n", sc->vr_dev.dv_xname, error);
1700 goto fail_2;
1701 }
1702
1703 if ((error = bus_dmamap_load(sc->vr_dmat, sc->vr_cddmamap,
1704 sc->vr_control_data, sizeof(struct vr_control_data), NULL,
1705 0)) != 0) {
1706 printf("%s: unable to load control data DMA map, error = %d\n",
1707 sc->vr_dev.dv_xname, error);
1708 goto fail_3;
1709 }
1710
1711 /*
1712 * Create the transmit buffer DMA maps.
1713 */
1714 for (i = 0; i < VR_NTXDESC; i++) {
1715 if ((error = bus_dmamap_create(sc->vr_dmat, MCLBYTES,
1716 1, MCLBYTES, 0, 0,
1717 &VR_DSTX(sc, i)->ds_dmamap)) != 0) {
1718 printf("%s: unable to create tx DMA map %d, "
1719 "error = %d\n", sc->vr_dev.dv_xname, i, error);
1720 goto fail_4;
1721 }
1722 }
1723
1724 /*
1725 * Create the receive buffer DMA maps.
1726 */
1727 for (i = 0; i < VR_NRXDESC; i++) {
1728 if ((error = bus_dmamap_create(sc->vr_dmat, MCLBYTES, 1,
1729 MCLBYTES, 0, 0,
1730 &VR_DSRX(sc, i)->ds_dmamap)) != 0) {
1731 printf("%s: unable to create rx DMA map %d, "
1732 "error = %d\n", sc->vr_dev.dv_xname, i, error);
1733 goto fail_5;
1734 }
1735 }
1736
1737 /*
1738 * Pre-allocate the receive buffers.
1739 */
1740 for (i = 0; i < VR_NRXDESC; i++) {
1741 if ((error = vr_add_rxbuf(sc, i)) != 0) {
1742 printf("%s: unable to allocate or map rx buffer %d, "
1743 "error = %d\n", sc->vr_dev.dv_xname, i, error);
1744 goto fail_6;
1745 }
1746 }
1747
1748 ifp = &sc->vr_ec.ec_if;
1749 ifp->if_softc = sc;
1750 ifp->if_mtu = ETHERMTU;
1751 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1752 ifp->if_ioctl = vr_ioctl;
1753 ifp->if_start = vr_start;
1754 ifp->if_watchdog = vr_watchdog;
1755 ifp->if_baudrate = 10000000;
1756 bcopy(sc->vr_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
1757
1758 /*
1759 * Initialize MII/media info.
1760 */
1761 sc->vr_mii.mii_ifp = ifp;
1762 sc->vr_mii.mii_readreg = vr_mii_readreg;
1763 sc->vr_mii.mii_writereg = vr_mii_writereg;
1764 sc->vr_mii.mii_statchg = vr_mii_statchg;
1765 ifmedia_init(&sc->vr_mii.mii_media, 0, vr_ifmedia_upd, vr_ifmedia_sts);
1766 mii_phy_probe(&sc->vr_dev, &sc->vr_mii, 0xffffffff);
1767 if (LIST_FIRST(&sc->vr_mii.mii_phys) == NULL) {
1768 ifmedia_add(&sc->vr_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
1769 ifmedia_set(&sc->vr_mii.mii_media, IFM_ETHER|IFM_NONE);
1770 } else
1771 ifmedia_set(&sc->vr_mii.mii_media, IFM_ETHER|IFM_AUTO);
1772
1773 /*
1774 * Call MI attach routines.
1775 */
1776 if_attach(ifp);
1777 ether_ifattach(ifp, sc->vr_enaddr);
1778
1779 #if NBPFILTER > 0
1780 bpfattach(&sc->vr_ec.ec_if.if_bpf,
1781 ifp, DLT_EN10MB, sizeof (struct ether_header));
1782 #endif
1783
1784 sc->vr_ats = shutdownhook_establish(vr_shutdown, sc);
1785 if (sc->vr_ats == NULL)
1786 printf("%s: warning: couldn't establish shutdown hook\n",
1787 sc->vr_dev.dv_xname);
1788 return;
1789
1790 fail_6:
1791 for (i = 0; i < VR_NRXDESC; i++) {
1792 if (sc->vr_rxsoft[i].ds_mbuf != NULL) {
1793 bus_dmamap_unload(sc->vr_dmat,
1794 sc->vr_rxsoft[i].ds_dmamap);
1795 (void) m_freem(sc->vr_rxsoft[i].ds_mbuf);
1796 }
1797 }
1798 fail_5:
1799 for (i = 0; i < VR_NRXDESC; i++) {
1800 if (sc->vr_rxsoft[i].ds_dmamap != NULL)
1801 bus_dmamap_destroy(sc->vr_dmat,
1802 sc->vr_rxsoft[i].ds_dmamap);
1803 }
1804 fail_4:
1805 for (i = 0; i < VR_NTXDESC; i++) {
1806 if (sc->vr_txsoft[i].ds_dmamap != NULL)
1807 bus_dmamap_destroy(sc->vr_dmat,
1808 sc->vr_txsoft[i].ds_dmamap);
1809 }
1810 bus_dmamap_unload(sc->vr_dmat, sc->vr_cddmamap);
1811 fail_3:
1812 bus_dmamap_destroy(sc->vr_dmat, sc->vr_cddmamap);
1813 fail_2:
1814 bus_dmamem_unmap(sc->vr_dmat, (caddr_t)sc->vr_control_data,
1815 sizeof(struct vr_control_data));
1816 fail_1:
1817 bus_dmamem_free(sc->vr_dmat, &seg, rseg);
1818 fail_0:
1819 return;
1820 }
1821