if_vr.c revision 1.46.2.1 1 /* $NetBSD: if_vr.c,v 1.46.2.1 2001/06/21 20:04:53 nathanw 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 <sys/param.h>
107 #include <sys/systm.h>
108 #include <sys/callout.h>
109 #include <sys/sockio.h>
110 #include <sys/mbuf.h>
111 #include <sys/malloc.h>
112 #include <sys/kernel.h>
113 #include <sys/socket.h>
114 #include <sys/device.h>
115
116 #include <uvm/uvm_extern.h> /* for PAGE_SIZE */
117
118 #include <net/if.h>
119 #include <net/if_arp.h>
120 #include <net/if_dl.h>
121 #include <net/if_media.h>
122 #include <net/if_ether.h>
123
124 #include "bpfilter.h"
125 #if NBPFILTER > 0
126 #include <net/bpf.h>
127 #endif
128
129 #include <machine/bus.h>
130 #include <machine/intr.h>
131 #include <machine/endian.h>
132
133 #include <dev/mii/mii.h>
134 #include <dev/mii/miivar.h>
135 #include <dev/mii/mii_bitbang.h>
136
137 #include <dev/pci/pcireg.h>
138 #include <dev/pci/pcivar.h>
139 #include <dev/pci/pcidevs.h>
140
141 #include <dev/pci/if_vrreg.h>
142
143 #define VR_USEIOSPACE
144
145 /*
146 * Various supported device vendors/types and their names.
147 */
148 static struct vr_type {
149 pci_vendor_id_t vr_vid;
150 pci_product_id_t vr_did;
151 const char *vr_name;
152 } vr_devs[] = {
153 { PCI_VENDOR_VIATECH, PCI_PRODUCT_VIATECH_VT3043,
154 "VIA VT3043 (Rhine) 10/100" },
155 { PCI_VENDOR_VIATECH, PCI_PRODUCT_VIATECH_VT6102,
156 "VIA VT6102 (Rhine II) 10/100" },
157 { PCI_VENDOR_VIATECH, PCI_PRODUCT_VIATECH_VT86C100A,
158 "VIA VT86C100A (Rhine-II) 10/100" },
159 { 0, 0, NULL }
160 };
161
162 /*
163 * Transmit descriptor list size.
164 */
165 #define VR_NTXDESC 64
166 #define VR_NTXDESC_MASK (VR_NTXDESC - 1)
167 #define VR_NEXTTX(x) (((x) + 1) & VR_NTXDESC_MASK)
168
169 /*
170 * Receive descriptor list size.
171 */
172 #define VR_NRXDESC 64
173 #define VR_NRXDESC_MASK (VR_NRXDESC - 1)
174 #define VR_NEXTRX(x) (((x) + 1) & VR_NRXDESC_MASK)
175
176 /*
177 * Control data structres that are DMA'd to the Rhine chip. We allocate
178 * them in a single clump that maps to a single DMA segment to make several
179 * things easier.
180 *
181 * Note that since we always copy outgoing packets to aligned transmit
182 * buffers, we can reduce the transmit descriptors to one per packet.
183 */
184 struct vr_control_data {
185 struct vr_desc vr_txdescs[VR_NTXDESC];
186 struct vr_desc vr_rxdescs[VR_NRXDESC];
187 };
188
189 #define VR_CDOFF(x) offsetof(struct vr_control_data, x)
190 #define VR_CDTXOFF(x) VR_CDOFF(vr_txdescs[(x)])
191 #define VR_CDRXOFF(x) VR_CDOFF(vr_rxdescs[(x)])
192
193 /*
194 * Software state of transmit and receive descriptors.
195 */
196 struct vr_descsoft {
197 struct mbuf *ds_mbuf; /* head of mbuf chain */
198 bus_dmamap_t ds_dmamap; /* our DMA map */
199 };
200
201 struct vr_softc {
202 struct device vr_dev; /* generic device glue */
203 void *vr_ih; /* interrupt cookie */
204 void *vr_ats; /* shutdown hook */
205 bus_space_tag_t vr_bst; /* bus space tag */
206 bus_space_handle_t vr_bsh; /* bus space handle */
207 bus_dma_tag_t vr_dmat; /* bus DMA tag */
208 pci_chipset_tag_t vr_pc; /* PCI chipset info */
209 struct ethercom vr_ec; /* Ethernet common info */
210 u_int8_t vr_enaddr[ETHER_ADDR_LEN];
211 struct mii_data vr_mii; /* MII/media info */
212
213 struct callout vr_tick_ch; /* tick callout */
214
215 bus_dmamap_t vr_cddmamap; /* control data DMA map */
216 #define vr_cddma vr_cddmamap->dm_segs[0].ds_addr
217
218 /*
219 * Software state for transmit and receive descriptors.
220 */
221 struct vr_descsoft vr_txsoft[VR_NTXDESC];
222 struct vr_descsoft vr_rxsoft[VR_NRXDESC];
223
224 /*
225 * Control data structures.
226 */
227 struct vr_control_data *vr_control_data;
228
229 int vr_txpending; /* number of TX requests pending */
230 int vr_txdirty; /* first dirty TX descriptor */
231 int vr_txlast; /* last used TX descriptor */
232
233 int vr_rxptr; /* next ready RX descriptor */
234 };
235
236 #define VR_CDTXADDR(sc, x) ((sc)->vr_cddma + VR_CDTXOFF((x)))
237 #define VR_CDRXADDR(sc, x) ((sc)->vr_cddma + VR_CDRXOFF((x)))
238
239 #define VR_CDTX(sc, x) (&(sc)->vr_control_data->vr_txdescs[(x)])
240 #define VR_CDRX(sc, x) (&(sc)->vr_control_data->vr_rxdescs[(x)])
241
242 #define VR_DSTX(sc, x) (&(sc)->vr_txsoft[(x)])
243 #define VR_DSRX(sc, x) (&(sc)->vr_rxsoft[(x)])
244
245 #define VR_CDTXSYNC(sc, x, ops) \
246 bus_dmamap_sync((sc)->vr_dmat, (sc)->vr_cddmamap, \
247 VR_CDTXOFF((x)), sizeof(struct vr_desc), (ops))
248
249 #define VR_CDRXSYNC(sc, x, ops) \
250 bus_dmamap_sync((sc)->vr_dmat, (sc)->vr_cddmamap, \
251 VR_CDRXOFF((x)), sizeof(struct vr_desc), (ops))
252
253 /*
254 * Note we rely on MCLBYTES being a power of two below.
255 */
256 #define VR_INIT_RXDESC(sc, i) \
257 do { \
258 struct vr_desc *__d = VR_CDRX((sc), (i)); \
259 struct vr_descsoft *__ds = VR_DSRX((sc), (i)); \
260 \
261 __d->vr_next = htole32(VR_CDRXADDR((sc), VR_NEXTRX((i)))); \
262 __d->vr_status = htole32(VR_RXSTAT_FIRSTFRAG | \
263 VR_RXSTAT_LASTFRAG | VR_RXSTAT_OWN); \
264 __d->vr_data = htole32(__ds->ds_dmamap->dm_segs[0].ds_addr); \
265 __d->vr_ctl = htole32(VR_RXCTL_CHAIN | VR_RXCTL_RX_INTR | \
266 ((MCLBYTES - 1) & VR_RXCTL_BUFLEN)); \
267 VR_CDRXSYNC((sc), (i), BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); \
268 } while (0)
269
270 /*
271 * register space access macros
272 */
273 #define CSR_WRITE_4(sc, reg, val) \
274 bus_space_write_4(sc->vr_bst, sc->vr_bsh, reg, val)
275 #define CSR_WRITE_2(sc, reg, val) \
276 bus_space_write_2(sc->vr_bst, sc->vr_bsh, reg, val)
277 #define CSR_WRITE_1(sc, reg, val) \
278 bus_space_write_1(sc->vr_bst, sc->vr_bsh, reg, val)
279
280 #define CSR_READ_4(sc, reg) \
281 bus_space_read_4(sc->vr_bst, sc->vr_bsh, reg)
282 #define CSR_READ_2(sc, reg) \
283 bus_space_read_2(sc->vr_bst, sc->vr_bsh, reg)
284 #define CSR_READ_1(sc, reg) \
285 bus_space_read_1(sc->vr_bst, sc->vr_bsh, reg)
286
287 #define VR_TIMEOUT 1000
288
289 static int vr_add_rxbuf __P((struct vr_softc *, int));
290
291 static void vr_rxeof __P((struct vr_softc *));
292 static void vr_rxeoc __P((struct vr_softc *));
293 static void vr_txeof __P((struct vr_softc *));
294 static int vr_intr __P((void *));
295 static void vr_start __P((struct ifnet *));
296 static int vr_ioctl __P((struct ifnet *, u_long, caddr_t));
297 static int vr_init __P((struct ifnet *));
298 static void vr_stop __P((struct ifnet *, int));
299 static void vr_rxdrain __P((struct vr_softc *));
300 static void vr_watchdog __P((struct ifnet *));
301 static void vr_tick __P((void *));
302
303 static int vr_ifmedia_upd __P((struct ifnet *));
304 static void vr_ifmedia_sts __P((struct ifnet *, struct ifmediareq *));
305
306 static int vr_mii_readreg __P((struct device *, int, int));
307 static void vr_mii_writereg __P((struct device *, int, int, int));
308 static void vr_mii_statchg __P((struct device *));
309
310 static void vr_setmulti __P((struct vr_softc *));
311 static void vr_reset __P((struct vr_softc *));
312
313 int vr_copy_small = 0;
314
315 #define VR_SETBIT(sc, reg, x) \
316 CSR_WRITE_1(sc, reg, \
317 CSR_READ_1(sc, reg) | x)
318
319 #define VR_CLRBIT(sc, reg, x) \
320 CSR_WRITE_1(sc, reg, \
321 CSR_READ_1(sc, reg) & ~x)
322
323 #define VR_SETBIT16(sc, reg, x) \
324 CSR_WRITE_2(sc, reg, \
325 CSR_READ_2(sc, reg) | x)
326
327 #define VR_CLRBIT16(sc, reg, x) \
328 CSR_WRITE_2(sc, reg, \
329 CSR_READ_2(sc, reg) & ~x)
330
331 #define VR_SETBIT32(sc, reg, x) \
332 CSR_WRITE_4(sc, reg, \
333 CSR_READ_4(sc, reg) | x)
334
335 #define VR_CLRBIT32(sc, reg, x) \
336 CSR_WRITE_4(sc, reg, \
337 CSR_READ_4(sc, reg) & ~x)
338
339 /*
340 * MII bit-bang glue.
341 */
342 u_int32_t vr_mii_bitbang_read __P((struct device *));
343 void vr_mii_bitbang_write __P((struct device *, u_int32_t));
344
345 const struct mii_bitbang_ops vr_mii_bitbang_ops = {
346 vr_mii_bitbang_read,
347 vr_mii_bitbang_write,
348 {
349 VR_MIICMD_DATAOUT, /* MII_BIT_MDO */
350 VR_MIICMD_DATAIN, /* MII_BIT_MDI */
351 VR_MIICMD_CLK, /* MII_BIT_MDC */
352 VR_MIICMD_DIR, /* MII_BIT_DIR_HOST_PHY */
353 0, /* MII_BIT_DIR_PHY_HOST */
354 }
355 };
356
357 u_int32_t
358 vr_mii_bitbang_read(self)
359 struct device *self;
360 {
361 struct vr_softc *sc = (void *) self;
362
363 return (CSR_READ_1(sc, VR_MIICMD));
364 }
365
366 void
367 vr_mii_bitbang_write(self, val)
368 struct device *self;
369 u_int32_t val;
370 {
371 struct vr_softc *sc = (void *) self;
372
373 CSR_WRITE_1(sc, VR_MIICMD, (val & 0xff) | VR_MIICMD_DIRECTPGM);
374 }
375
376 /*
377 * Read an PHY register through the MII.
378 */
379 static int
380 vr_mii_readreg(self, phy, reg)
381 struct device *self;
382 int phy, reg;
383 {
384 struct vr_softc *sc = (void *) self;
385
386 CSR_WRITE_1(sc, VR_MIICMD, VR_MIICMD_DIRECTPGM);
387 return (mii_bitbang_readreg(self, &vr_mii_bitbang_ops, phy, reg));
388 }
389
390 /*
391 * Write to a PHY register through the MII.
392 */
393 static void
394 vr_mii_writereg(self, phy, reg, val)
395 struct device *self;
396 int phy, reg, val;
397 {
398 struct vr_softc *sc = (void *) self;
399
400 CSR_WRITE_1(sc, VR_MIICMD, VR_MIICMD_DIRECTPGM);
401 mii_bitbang_writereg(self, &vr_mii_bitbang_ops, phy, reg, val);
402 }
403
404 static void
405 vr_mii_statchg(self)
406 struct device *self;
407 {
408 struct vr_softc *sc = (struct vr_softc *)self;
409
410 /*
411 * In order to fiddle with the 'full-duplex' bit in the netconfig
412 * register, we first have to put the transmit and/or receive logic
413 * in the idle state.
414 */
415 VR_CLRBIT16(sc, VR_COMMAND, (VR_CMD_TX_ON|VR_CMD_RX_ON));
416
417 if (sc->vr_mii.mii_media_active & IFM_FDX)
418 VR_SETBIT16(sc, VR_COMMAND, VR_CMD_FULLDUPLEX);
419 else
420 VR_CLRBIT16(sc, VR_COMMAND, VR_CMD_FULLDUPLEX);
421
422 if (sc->vr_ec.ec_if.if_flags & IFF_RUNNING)
423 VR_SETBIT16(sc, VR_COMMAND, VR_CMD_TX_ON|VR_CMD_RX_ON);
424 }
425
426 #define vr_calchash(addr) \
427 (ether_crc32_be((addr), ETHER_ADDR_LEN) >> 26)
428
429 /*
430 * Program the 64-bit multicast hash filter.
431 */
432 static void
433 vr_setmulti(sc)
434 struct vr_softc *sc;
435 {
436 struct ifnet *ifp;
437 int h = 0;
438 u_int32_t hashes[2] = { 0, 0 };
439 struct ether_multistep step;
440 struct ether_multi *enm;
441 int mcnt = 0;
442 u_int8_t rxfilt;
443
444 ifp = &sc->vr_ec.ec_if;
445
446 rxfilt = CSR_READ_1(sc, VR_RXCFG);
447
448 if (ifp->if_flags & IFF_PROMISC) {
449 allmulti:
450 ifp->if_flags |= IFF_ALLMULTI;
451 rxfilt |= VR_RXCFG_RX_MULTI;
452 CSR_WRITE_1(sc, VR_RXCFG, rxfilt);
453 CSR_WRITE_4(sc, VR_MAR0, 0xFFFFFFFF);
454 CSR_WRITE_4(sc, VR_MAR1, 0xFFFFFFFF);
455 return;
456 }
457
458 /* first, zot all the existing hash bits */
459 CSR_WRITE_4(sc, VR_MAR0, 0);
460 CSR_WRITE_4(sc, VR_MAR1, 0);
461
462 /* now program new ones */
463 ETHER_FIRST_MULTI(step, &sc->vr_ec, enm);
464 while (enm != NULL) {
465 if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
466 ETHER_ADDR_LEN) != 0)
467 goto allmulti;
468
469 h = vr_calchash(enm->enm_addrlo);
470
471 if (h < 32)
472 hashes[0] |= (1 << h);
473 else
474 hashes[1] |= (1 << (h - 32));
475 ETHER_NEXT_MULTI(step, enm);
476 mcnt++;
477 }
478
479 ifp->if_flags &= ~IFF_ALLMULTI;
480
481 if (mcnt)
482 rxfilt |= VR_RXCFG_RX_MULTI;
483 else
484 rxfilt &= ~VR_RXCFG_RX_MULTI;
485
486 CSR_WRITE_4(sc, VR_MAR0, hashes[0]);
487 CSR_WRITE_4(sc, VR_MAR1, hashes[1]);
488 CSR_WRITE_1(sc, VR_RXCFG, rxfilt);
489 }
490
491 static void
492 vr_reset(sc)
493 struct vr_softc *sc;
494 {
495 int i;
496
497 VR_SETBIT16(sc, VR_COMMAND, VR_CMD_RESET);
498
499 for (i = 0; i < VR_TIMEOUT; i++) {
500 DELAY(10);
501 if (!(CSR_READ_2(sc, VR_COMMAND) & VR_CMD_RESET))
502 break;
503 }
504 if (i == VR_TIMEOUT)
505 printf("%s: reset never completed!\n",
506 sc->vr_dev.dv_xname);
507
508 /* Wait a little while for the chip to get its brains in order. */
509 DELAY(1000);
510 }
511
512 /*
513 * Initialize an RX descriptor and attach an MBUF cluster.
514 * Note: the length fields are only 11 bits wide, which means the
515 * largest size we can specify is 2047. This is important because
516 * MCLBYTES is 2048, so we have to subtract one otherwise we'll
517 * overflow the field and make a mess.
518 */
519 static int
520 vr_add_rxbuf(sc, i)
521 struct vr_softc *sc;
522 int i;
523 {
524 struct vr_descsoft *ds = VR_DSRX(sc, i);
525 struct mbuf *m_new;
526 int error;
527
528 MGETHDR(m_new, M_DONTWAIT, MT_DATA);
529 if (m_new == NULL)
530 return (ENOBUFS);
531
532 MCLGET(m_new, M_DONTWAIT);
533 if ((m_new->m_flags & M_EXT) == 0) {
534 m_freem(m_new);
535 return (ENOBUFS);
536 }
537
538 if (ds->ds_mbuf != NULL)
539 bus_dmamap_unload(sc->vr_dmat, ds->ds_dmamap);
540
541 ds->ds_mbuf = m_new;
542
543 error = bus_dmamap_load(sc->vr_dmat, ds->ds_dmamap,
544 m_new->m_ext.ext_buf, m_new->m_ext.ext_size, NULL, BUS_DMA_NOWAIT);
545 if (error) {
546 printf("%s: unable to load rx DMA map %d, error = %d\n",
547 sc->vr_dev.dv_xname, i, error);
548 panic("vr_add_rxbuf"); /* XXX */
549 }
550
551 bus_dmamap_sync(sc->vr_dmat, ds->ds_dmamap, 0,
552 ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
553
554 VR_INIT_RXDESC(sc, i);
555
556 return (0);
557 }
558
559 /*
560 * A frame has been uploaded: pass the resulting mbuf chain up to
561 * the higher level protocols.
562 */
563 static void
564 vr_rxeof(sc)
565 struct vr_softc *sc;
566 {
567 struct mbuf *m;
568 struct ifnet *ifp;
569 struct vr_desc *d;
570 struct vr_descsoft *ds;
571 int i, total_len;
572 u_int32_t rxstat;
573
574 ifp = &sc->vr_ec.ec_if;
575
576 for (i = sc->vr_rxptr;; i = VR_NEXTRX(i)) {
577 d = VR_CDRX(sc, i);
578 ds = VR_DSRX(sc, i);
579
580 VR_CDRXSYNC(sc, i, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
581
582 rxstat = le32toh(d->vr_status);
583
584 if (rxstat & VR_RXSTAT_OWN) {
585 /*
586 * We have processed all of the receive buffers.
587 */
588 break;
589 }
590
591 /*
592 * If an error occurs, update stats, clear the
593 * status word and leave the mbuf cluster in place:
594 * it should simply get re-used next time this descriptor
595 * comes up in the ring.
596 */
597 if (rxstat & VR_RXSTAT_RXERR) {
598 const char *errstr;
599
600 ifp->if_ierrors++;
601 switch (rxstat & 0x000000FF) {
602 case VR_RXSTAT_CRCERR:
603 errstr = "crc error";
604 break;
605 case VR_RXSTAT_FRAMEALIGNERR:
606 errstr = "frame alignment error";
607 break;
608 case VR_RXSTAT_FIFOOFLOW:
609 errstr = "FIFO overflow";
610 break;
611 case VR_RXSTAT_GIANT:
612 errstr = "received giant packet";
613 break;
614 case VR_RXSTAT_RUNT:
615 errstr = "received runt packet";
616 break;
617 case VR_RXSTAT_BUSERR:
618 errstr = "system bus error";
619 break;
620 case VR_RXSTAT_BUFFERR:
621 errstr = "rx buffer error";
622 break;
623 default:
624 errstr = "unknown rx error";
625 break;
626 }
627 printf("%s: receive error: %s\n", sc->vr_dev.dv_xname,
628 errstr);
629
630 VR_INIT_RXDESC(sc, i);
631
632 continue;
633 }
634
635 bus_dmamap_sync(sc->vr_dmat, ds->ds_dmamap, 0,
636 ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
637
638 /* No errors; receive the packet. */
639 total_len = VR_RXBYTES(le32toh(d->vr_status));
640
641 #ifdef __NO_STRICT_ALIGNMENT
642 /*
643 * If the packet is small enough to fit in a
644 * single header mbuf, allocate one and copy
645 * the data into it. This greatly reduces
646 * memory consumption when we receive lots
647 * of small packets.
648 *
649 * Otherwise, we add a new buffer to the receive
650 * chain. If this fails, we drop the packet and
651 * recycle the old buffer.
652 */
653 if (vr_copy_small != 0 && total_len <= MHLEN) {
654 MGETHDR(m, M_DONTWAIT, MT_DATA);
655 if (m == NULL)
656 goto dropit;
657 memcpy(mtod(m, caddr_t),
658 mtod(ds->ds_mbuf, caddr_t), total_len);
659 VR_INIT_RXDESC(sc, i);
660 bus_dmamap_sync(sc->vr_dmat, ds->ds_dmamap, 0,
661 ds->ds_dmamap->dm_mapsize,
662 BUS_DMASYNC_PREREAD);
663 } else {
664 m = ds->ds_mbuf;
665 if (vr_add_rxbuf(sc, i) == ENOBUFS) {
666 dropit:
667 ifp->if_ierrors++;
668 VR_INIT_RXDESC(sc, i);
669 bus_dmamap_sync(sc->vr_dmat,
670 ds->ds_dmamap, 0,
671 ds->ds_dmamap->dm_mapsize,
672 BUS_DMASYNC_PREREAD);
673 continue;
674 }
675 }
676 #else
677 /*
678 * The Rhine's packet buffers must be 4-byte aligned.
679 * But this means that the data after the Ethernet header
680 * is misaligned. We must allocate a new buffer and
681 * copy the data, shifted forward 2 bytes.
682 */
683 MGETHDR(m, M_DONTWAIT, MT_DATA);
684 if (m == NULL) {
685 dropit:
686 ifp->if_ierrors++;
687 VR_INIT_RXDESC(sc, i);
688 bus_dmamap_sync(sc->vr_dmat, ds->ds_dmamap, 0,
689 ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
690 continue;
691 }
692 if (total_len > (MHLEN - 2)) {
693 MCLGET(m, M_DONTWAIT);
694 if ((m->m_flags & M_EXT) == 0) {
695 m_freem(m);
696 goto dropit;
697 }
698 }
699 m->m_data += 2;
700
701 /*
702 * Note that we use clusters for incoming frames, so the
703 * buffer is virtually contiguous.
704 */
705 memcpy(mtod(m, caddr_t), mtod(ds->ds_mbuf, caddr_t),
706 total_len);
707
708 /* Allow the receive descriptor to continue using its mbuf. */
709 VR_INIT_RXDESC(sc, i);
710 bus_dmamap_sync(sc->vr_dmat, ds->ds_dmamap, 0,
711 ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
712 #endif /* __NO_STRICT_ALIGNMENT */
713
714 /*
715 * The Rhine chip includes the FCS with every
716 * received packet.
717 */
718 m->m_flags |= M_HASFCS;
719
720 ifp->if_ipackets++;
721 m->m_pkthdr.rcvif = ifp;
722 m->m_pkthdr.len = m->m_len = total_len;
723 #if NBPFILTER > 0
724 /*
725 * Handle BPF listeners. Let the BPF user see the packet, but
726 * don't pass it up to the ether_input() layer unless it's
727 * a broadcast packet, multicast packet, matches our ethernet
728 * address or the interface is in promiscuous mode.
729 */
730 if (ifp->if_bpf)
731 bpf_mtap(ifp->if_bpf, m);
732 #endif
733 /* Pass it on. */
734 (*ifp->if_input)(ifp, m);
735 }
736
737 /* Update the receive pointer. */
738 sc->vr_rxptr = i;
739 }
740
741 void
742 vr_rxeoc(sc)
743 struct vr_softc *sc;
744 {
745
746 vr_rxeof(sc);
747 VR_CLRBIT16(sc, VR_COMMAND, VR_CMD_RX_ON);
748 CSR_WRITE_4(sc, VR_RXADDR, VR_CDRXADDR(sc, sc->vr_rxptr));
749 VR_SETBIT16(sc, VR_COMMAND, VR_CMD_RX_ON);
750 VR_SETBIT16(sc, VR_COMMAND, VR_CMD_RX_GO);
751 }
752
753 /*
754 * A frame was downloaded to the chip. It's safe for us to clean up
755 * the list buffers.
756 */
757 static void
758 vr_txeof(sc)
759 struct vr_softc *sc;
760 {
761 struct ifnet *ifp = &sc->vr_ec.ec_if;
762 struct vr_desc *d;
763 struct vr_descsoft *ds;
764 u_int32_t txstat;
765 int i;
766
767 ifp->if_flags &= ~IFF_OACTIVE;
768
769 /*
770 * Go through our tx list and free mbufs for those
771 * frames that have been transmitted.
772 */
773 for (i = sc->vr_txdirty; sc->vr_txpending != 0;
774 i = VR_NEXTTX(i), sc->vr_txpending--) {
775 d = VR_CDTX(sc, i);
776 ds = VR_DSTX(sc, i);
777
778 VR_CDTXSYNC(sc, i, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
779
780 txstat = le32toh(d->vr_status);
781 if (txstat & VR_TXSTAT_OWN)
782 break;
783
784 bus_dmamap_sync(sc->vr_dmat, ds->ds_dmamap,
785 0, ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
786 bus_dmamap_unload(sc->vr_dmat, ds->ds_dmamap);
787 m_freem(ds->ds_mbuf);
788 ds->ds_mbuf = NULL;
789
790 if (txstat & VR_TXSTAT_ERRSUM) {
791 ifp->if_oerrors++;
792 if (txstat & VR_TXSTAT_DEFER)
793 ifp->if_collisions++;
794 if (txstat & VR_TXSTAT_LATECOLL)
795 ifp->if_collisions++;
796 }
797
798 ifp->if_collisions += (txstat & VR_TXSTAT_COLLCNT) >> 3;
799 ifp->if_opackets++;
800 }
801
802 /* Update the dirty transmit buffer pointer. */
803 sc->vr_txdirty = i;
804
805 /*
806 * Cancel the watchdog timer if there are no pending
807 * transmissions.
808 */
809 if (sc->vr_txpending == 0)
810 ifp->if_timer = 0;
811 }
812
813 static int
814 vr_intr(arg)
815 void *arg;
816 {
817 struct vr_softc *sc;
818 struct ifnet *ifp;
819 u_int16_t status;
820 int handled = 0, dotx = 0;
821
822 sc = arg;
823 ifp = &sc->vr_ec.ec_if;
824
825 /* Suppress unwanted interrupts. */
826 if ((ifp->if_flags & IFF_UP) == 0) {
827 vr_stop(ifp, 1);
828 return (0);
829 }
830
831 /* Disable interrupts. */
832 CSR_WRITE_2(sc, VR_IMR, 0x0000);
833
834 for (;;) {
835 status = CSR_READ_2(sc, VR_ISR);
836 if (status)
837 CSR_WRITE_2(sc, VR_ISR, status);
838
839 if ((status & VR_INTRS) == 0)
840 break;
841
842 handled = 1;
843
844 if (status & VR_ISR_RX_OK)
845 vr_rxeof(sc);
846
847 if (status &
848 (VR_ISR_RX_ERR | VR_ISR_RX_NOBUF | VR_ISR_RX_OFLOW |
849 VR_ISR_RX_DROPPED))
850 vr_rxeoc(sc);
851
852 if (status & VR_ISR_TX_OK) {
853 dotx = 1;
854 vr_txeof(sc);
855 }
856
857 if (status & (VR_ISR_TX_UNDERRUN | VR_ISR_TX_ABRT)) {
858 if (status & VR_ISR_TX_UNDERRUN)
859 printf("%s: transmit underrun\n",
860 sc->vr_dev.dv_xname);
861 if (status & VR_ISR_TX_ABRT)
862 printf("%s: transmit aborted\n",
863 sc->vr_dev.dv_xname);
864 ifp->if_oerrors++;
865 dotx = 1;
866 vr_txeof(sc);
867 if (sc->vr_txpending) {
868 VR_SETBIT16(sc, VR_COMMAND, VR_CMD_TX_ON);
869 VR_SETBIT16(sc, VR_COMMAND, VR_CMD_TX_GO);
870 }
871 }
872
873 if (status & VR_ISR_BUSERR) {
874 printf("%s: PCI bus error\n", sc->vr_dev.dv_xname);
875 /* vr_init() calls vr_start() */
876 dotx = 0;
877 (void) vr_init(ifp);
878 }
879 }
880
881 /* Re-enable interrupts. */
882 CSR_WRITE_2(sc, VR_IMR, VR_INTRS);
883
884 if (dotx)
885 vr_start(ifp);
886
887 return (handled);
888 }
889
890 /*
891 * Main transmit routine. To avoid having to do mbuf copies, we put pointers
892 * to the mbuf data regions directly in the transmit lists. We also save a
893 * copy of the pointers since the transmit list fragment pointers are
894 * physical addresses.
895 */
896 static void
897 vr_start(ifp)
898 struct ifnet *ifp;
899 {
900 struct vr_softc *sc = ifp->if_softc;
901 struct mbuf *m0, *m;
902 struct vr_desc *d;
903 struct vr_descsoft *ds;
904 int error, firsttx, nexttx, opending;
905
906 /*
907 * Remember the previous txpending and the first transmit
908 * descriptor we use.
909 */
910 opending = sc->vr_txpending;
911 firsttx = VR_NEXTTX(sc->vr_txlast);
912
913 /*
914 * Loop through the send queue, setting up transmit descriptors
915 * until we drain the queue, or use up all available transmit
916 * descriptors.
917 */
918 while (sc->vr_txpending < VR_NTXDESC) {
919 /*
920 * Grab a packet off the queue.
921 */
922 IFQ_POLL(&ifp->if_snd, m0);
923 if (m0 == NULL)
924 break;
925 m = NULL;
926
927 /*
928 * Get the next available transmit descriptor.
929 */
930 nexttx = VR_NEXTTX(sc->vr_txlast);
931 d = VR_CDTX(sc, nexttx);
932 ds = VR_DSTX(sc, nexttx);
933
934 /*
935 * Load the DMA map. If this fails, the packet didn't
936 * fit in one DMA segment, and we need to copy. Note,
937 * the packet must also be aligned.
938 */
939 if ((mtod(m0, bus_addr_t) & 3) != 0 ||
940 bus_dmamap_load_mbuf(sc->vr_dmat, ds->ds_dmamap, m0,
941 BUS_DMA_NOWAIT) != 0) {
942 MGETHDR(m, M_DONTWAIT, MT_DATA);
943 if (m == NULL) {
944 printf("%s: unable to allocate Tx mbuf\n",
945 sc->vr_dev.dv_xname);
946 break;
947 }
948 if (m0->m_pkthdr.len > MHLEN) {
949 MCLGET(m, M_DONTWAIT);
950 if ((m->m_flags & M_EXT) == 0) {
951 printf("%s: unable to allocate Tx "
952 "cluster\n", sc->vr_dev.dv_xname);
953 m_freem(m);
954 break;
955 }
956 }
957 m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t));
958 m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
959 error = bus_dmamap_load_mbuf(sc->vr_dmat,
960 ds->ds_dmamap, m, BUS_DMA_NOWAIT);
961 if (error) {
962 printf("%s: unable to load Tx buffer, "
963 "error = %d\n", sc->vr_dev.dv_xname, error);
964 break;
965 }
966 }
967
968 IFQ_DEQUEUE(&ifp->if_snd, m0);
969 if (m != NULL) {
970 m_freem(m0);
971 m0 = m;
972 }
973
974 /* Sync the DMA map. */
975 bus_dmamap_sync(sc->vr_dmat, ds->ds_dmamap, 0,
976 ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_PREWRITE);
977
978 /*
979 * Store a pointer to the packet so we can free it later.
980 */
981 ds->ds_mbuf = m0;
982
983 #if NBPFILTER > 0
984 /*
985 * If there's a BPF listener, bounce a copy of this frame
986 * to him.
987 */
988 if (ifp->if_bpf)
989 bpf_mtap(ifp->if_bpf, m0);
990 #endif
991
992 /*
993 * Fill in the transmit descriptor. The Rhine
994 * doesn't auto-pad, so we have to do this ourselves.
995 */
996 d->vr_data = htole32(ds->ds_dmamap->dm_segs[0].ds_addr);
997 d->vr_ctl = htole32(m0->m_pkthdr.len < VR_MIN_FRAMELEN ?
998 VR_MIN_FRAMELEN : m0->m_pkthdr.len);
999 d->vr_ctl |=
1000 htole32(VR_TXCTL_TLINK|VR_TXCTL_FIRSTFRAG|
1001 VR_TXCTL_LASTFRAG);
1002
1003 /*
1004 * If this is the first descriptor we're enqueuing,
1005 * don't give it to the Rhine yet. That could cause
1006 * a race condition. We'll do it below.
1007 */
1008 if (nexttx == firsttx)
1009 d->vr_status = 0;
1010 else
1011 d->vr_status = htole32(VR_TXSTAT_OWN);
1012
1013 VR_CDTXSYNC(sc, nexttx,
1014 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1015
1016 /* Advance the tx pointer. */
1017 sc->vr_txpending++;
1018 sc->vr_txlast = nexttx;
1019 }
1020
1021 if (sc->vr_txpending == VR_NTXDESC) {
1022 /* No more slots left; notify upper layer. */
1023 ifp->if_flags |= IFF_OACTIVE;
1024 }
1025
1026 if (sc->vr_txpending != opending) {
1027 /*
1028 * We enqueued packets. If the transmitter was idle,
1029 * reset the txdirty pointer.
1030 */
1031 if (opending == 0)
1032 sc->vr_txdirty = firsttx;
1033
1034 /*
1035 * Cause a transmit interrupt to happen on the
1036 * last packet we enqueued.
1037 */
1038 VR_CDTX(sc, sc->vr_txlast)->vr_ctl |= htole32(VR_TXCTL_FINT);
1039 VR_CDTXSYNC(sc, sc->vr_txlast,
1040 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1041
1042 /*
1043 * The entire packet chain is set up. Give the
1044 * first descriptor to the Rhine now.
1045 */
1046 VR_CDTX(sc, firsttx)->vr_status = htole32(VR_TXSTAT_OWN);
1047 VR_CDTXSYNC(sc, firsttx,
1048 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1049
1050 /* Start the transmitter. */
1051 VR_SETBIT16(sc, VR_COMMAND, VR_CMD_TX_ON|VR_CMD_TX_GO);
1052
1053 /* Set the watchdog timer in case the chip flakes out. */
1054 ifp->if_timer = 5;
1055 }
1056 }
1057
1058 /*
1059 * Initialize the interface. Must be called at splnet.
1060 */
1061 static int
1062 vr_init(ifp)
1063 struct ifnet *ifp;
1064 {
1065 struct vr_softc *sc = ifp->if_softc;
1066 struct vr_desc *d;
1067 struct vr_descsoft *ds;
1068 int i, error = 0;
1069
1070 /* Cancel pending I/O. */
1071 vr_stop(ifp, 0);
1072
1073 /* Reset the Rhine to a known state. */
1074 vr_reset(sc);
1075
1076 VR_CLRBIT(sc, VR_RXCFG, VR_RXCFG_RX_THRESH);
1077 VR_SETBIT(sc, VR_RXCFG, VR_RXTHRESH_STORENFWD);
1078
1079 VR_CLRBIT(sc, VR_TXCFG, VR_TXCFG_TX_THRESH);
1080 VR_SETBIT(sc, VR_TXCFG, VR_TXTHRESH_STORENFWD);
1081
1082 /*
1083 * Initialize the transmit desciptor ring. txlast is initialized
1084 * to the end of the list so that it will wrap around to the first
1085 * descriptor when the first packet is transmitted.
1086 */
1087 for (i = 0; i < VR_NTXDESC; i++) {
1088 d = VR_CDTX(sc, i);
1089 memset(d, 0, sizeof(struct vr_desc));
1090 d->vr_next = htole32(VR_CDTXADDR(sc, VR_NEXTTX(i)));
1091 VR_CDTXSYNC(sc, i, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1092 }
1093 sc->vr_txpending = 0;
1094 sc->vr_txdirty = 0;
1095 sc->vr_txlast = VR_NTXDESC - 1;
1096
1097 /*
1098 * Initialize the receive descriptor ring.
1099 */
1100 for (i = 0; i < VR_NRXDESC; i++) {
1101 ds = VR_DSRX(sc, i);
1102 if (ds->ds_mbuf == NULL) {
1103 if ((error = vr_add_rxbuf(sc, i)) != 0) {
1104 printf("%s: unable to allocate or map rx "
1105 "buffer %d, error = %d\n",
1106 sc->vr_dev.dv_xname, i, error);
1107 /*
1108 * XXX Should attempt to run with fewer receive
1109 * XXX buffers instead of just failing.
1110 */
1111 vr_rxdrain(sc);
1112 goto out;
1113 }
1114 }
1115 }
1116 sc->vr_rxptr = 0;
1117
1118 /* If we want promiscuous mode, set the allframes bit. */
1119 if (ifp->if_flags & IFF_PROMISC)
1120 VR_SETBIT(sc, VR_RXCFG, VR_RXCFG_RX_PROMISC);
1121 else
1122 VR_CLRBIT(sc, VR_RXCFG, VR_RXCFG_RX_PROMISC);
1123
1124 /* Set capture broadcast bit to capture broadcast frames. */
1125 if (ifp->if_flags & IFF_BROADCAST)
1126 VR_SETBIT(sc, VR_RXCFG, VR_RXCFG_RX_BROAD);
1127 else
1128 VR_CLRBIT(sc, VR_RXCFG, VR_RXCFG_RX_BROAD);
1129
1130 /* Program the multicast filter, if necessary. */
1131 vr_setmulti(sc);
1132
1133 /* Give the transmit and receive rings to the Rhine. */
1134 CSR_WRITE_4(sc, VR_RXADDR, VR_CDRXADDR(sc, sc->vr_rxptr));
1135 CSR_WRITE_4(sc, VR_TXADDR, VR_CDTXADDR(sc, VR_NEXTTX(sc->vr_txlast)));
1136
1137 /* Set current media. */
1138 mii_mediachg(&sc->vr_mii);
1139
1140 /* Enable receiver and transmitter. */
1141 CSR_WRITE_2(sc, VR_COMMAND, VR_CMD_TX_NOPOLL|VR_CMD_START|
1142 VR_CMD_TX_ON|VR_CMD_RX_ON|
1143 VR_CMD_RX_GO);
1144
1145 /* Enable interrupts. */
1146 CSR_WRITE_2(sc, VR_ISR, 0xFFFF);
1147 CSR_WRITE_2(sc, VR_IMR, VR_INTRS);
1148
1149 ifp->if_flags |= IFF_RUNNING;
1150 ifp->if_flags &= ~IFF_OACTIVE;
1151
1152 /* Start one second timer. */
1153 callout_reset(&sc->vr_tick_ch, hz, vr_tick, sc);
1154
1155 /* Attempt to start output on the interface. */
1156 vr_start(ifp);
1157
1158 out:
1159 if (error)
1160 printf("%s: interface not running\n", sc->vr_dev.dv_xname);
1161 return (error);
1162 }
1163
1164 /*
1165 * Set media options.
1166 */
1167 static int
1168 vr_ifmedia_upd(ifp)
1169 struct ifnet *ifp;
1170 {
1171 struct vr_softc *sc = ifp->if_softc;
1172
1173 if (ifp->if_flags & IFF_UP)
1174 mii_mediachg(&sc->vr_mii);
1175 return (0);
1176 }
1177
1178 /*
1179 * Report current media status.
1180 */
1181 static void
1182 vr_ifmedia_sts(ifp, ifmr)
1183 struct ifnet *ifp;
1184 struct ifmediareq *ifmr;
1185 {
1186 struct vr_softc *sc = ifp->if_softc;
1187
1188 mii_pollstat(&sc->vr_mii);
1189 ifmr->ifm_status = sc->vr_mii.mii_media_status;
1190 ifmr->ifm_active = sc->vr_mii.mii_media_active;
1191 }
1192
1193 static int
1194 vr_ioctl(ifp, command, data)
1195 struct ifnet *ifp;
1196 u_long command;
1197 caddr_t data;
1198 {
1199 struct vr_softc *sc = ifp->if_softc;
1200 struct ifreq *ifr = (struct ifreq *)data;
1201 int s, error = 0;
1202
1203 s = splnet();
1204
1205 switch (command) {
1206 case SIOCGIFMEDIA:
1207 case SIOCSIFMEDIA:
1208 error = ifmedia_ioctl(ifp, ifr, &sc->vr_mii.mii_media, command);
1209 break;
1210
1211 default:
1212 error = ether_ioctl(ifp, command, data);
1213 if (error == ENETRESET) {
1214 /*
1215 * Multicast list has changed; set the hardware filter
1216 * accordingly.
1217 */
1218 vr_setmulti(sc);
1219 error = 0;
1220 }
1221 break;
1222 }
1223
1224 splx(s);
1225 return (error);
1226 }
1227
1228 static void
1229 vr_watchdog(ifp)
1230 struct ifnet *ifp;
1231 {
1232 struct vr_softc *sc = ifp->if_softc;
1233
1234 printf("%s: device timeout\n", sc->vr_dev.dv_xname);
1235 ifp->if_oerrors++;
1236
1237 (void) vr_init(ifp);
1238 }
1239
1240 /*
1241 * One second timer, used to tick MII.
1242 */
1243 static void
1244 vr_tick(arg)
1245 void *arg;
1246 {
1247 struct vr_softc *sc = arg;
1248 int s;
1249
1250 s = splnet();
1251 mii_tick(&sc->vr_mii);
1252 splx(s);
1253
1254 callout_reset(&sc->vr_tick_ch, hz, vr_tick, sc);
1255 }
1256
1257 /*
1258 * Drain the receive queue.
1259 */
1260 static void
1261 vr_rxdrain(sc)
1262 struct vr_softc *sc;
1263 {
1264 struct vr_descsoft *ds;
1265 int i;
1266
1267 for (i = 0; i < VR_NRXDESC; i++) {
1268 ds = VR_DSRX(sc, i);
1269 if (ds->ds_mbuf != NULL) {
1270 bus_dmamap_unload(sc->vr_dmat, ds->ds_dmamap);
1271 m_freem(ds->ds_mbuf);
1272 ds->ds_mbuf = NULL;
1273 }
1274 }
1275 }
1276
1277 /*
1278 * Stop the adapter and free any mbufs allocated to the
1279 * transmit lists.
1280 */
1281 static void
1282 vr_stop(ifp, disable)
1283 struct ifnet *ifp;
1284 int disable;
1285 {
1286 struct vr_softc *sc = ifp->if_softc;
1287 struct vr_descsoft *ds;
1288 int i;
1289
1290 /* Cancel one second timer. */
1291 callout_stop(&sc->vr_tick_ch);
1292
1293 /* Down the MII. */
1294 mii_down(&sc->vr_mii);
1295
1296 ifp = &sc->vr_ec.ec_if;
1297 ifp->if_timer = 0;
1298
1299 VR_SETBIT16(sc, VR_COMMAND, VR_CMD_STOP);
1300 VR_CLRBIT16(sc, VR_COMMAND, (VR_CMD_RX_ON|VR_CMD_TX_ON));
1301 CSR_WRITE_2(sc, VR_IMR, 0x0000);
1302 CSR_WRITE_4(sc, VR_TXADDR, 0x00000000);
1303 CSR_WRITE_4(sc, VR_RXADDR, 0x00000000);
1304
1305 /*
1306 * Release any queued transmit buffers.
1307 */
1308 for (i = 0; i < VR_NTXDESC; i++) {
1309 ds = VR_DSTX(sc, i);
1310 if (ds->ds_mbuf != NULL) {
1311 bus_dmamap_unload(sc->vr_dmat, ds->ds_dmamap);
1312 m_freem(ds->ds_mbuf);
1313 ds->ds_mbuf = NULL;
1314 }
1315 }
1316
1317 if (disable)
1318 vr_rxdrain(sc);
1319
1320 /*
1321 * Mark the interface down and cancel the watchdog timer.
1322 */
1323 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1324 ifp->if_timer = 0;
1325 }
1326
1327 static struct vr_type *vr_lookup __P((struct pci_attach_args *));
1328 static int vr_probe __P((struct device *, struct cfdata *, void *));
1329 static void vr_attach __P((struct device *, struct device *, void *));
1330 static void vr_shutdown __P((void *));
1331
1332 struct cfattach vr_ca = {
1333 sizeof (struct vr_softc), vr_probe, vr_attach
1334 };
1335
1336 static struct vr_type *
1337 vr_lookup(pa)
1338 struct pci_attach_args *pa;
1339 {
1340 struct vr_type *vrt;
1341
1342 for (vrt = vr_devs; vrt->vr_name != NULL; vrt++) {
1343 if (PCI_VENDOR(pa->pa_id) == vrt->vr_vid &&
1344 PCI_PRODUCT(pa->pa_id) == vrt->vr_did)
1345 return (vrt);
1346 }
1347 return (NULL);
1348 }
1349
1350 static int
1351 vr_probe(parent, match, aux)
1352 struct device *parent;
1353 struct cfdata *match;
1354 void *aux;
1355 {
1356 struct pci_attach_args *pa = (struct pci_attach_args *)aux;
1357
1358 if (vr_lookup(pa) != NULL)
1359 return (1);
1360
1361 return (0);
1362 }
1363
1364 /*
1365 * Stop all chip I/O so that the kernel's probe routines don't
1366 * get confused by errant DMAs when rebooting.
1367 */
1368 static void
1369 vr_shutdown(arg)
1370 void *arg;
1371 {
1372 struct vr_softc *sc = (struct vr_softc *)arg;
1373
1374 vr_stop(&sc->vr_ec.ec_if, 1);
1375 }
1376
1377 /*
1378 * Attach the interface. Allocate softc structures, do ifmedia
1379 * setup and ethernet/BPF attach.
1380 */
1381 static void
1382 vr_attach(parent, self, aux)
1383 struct device *parent;
1384 struct device *self;
1385 void *aux;
1386 {
1387 struct vr_softc *sc = (struct vr_softc *) self;
1388 struct pci_attach_args *pa = (struct pci_attach_args *) aux;
1389 bus_dma_segment_t seg;
1390 struct vr_type *vrt;
1391 u_int32_t command;
1392 struct ifnet *ifp;
1393 u_char eaddr[ETHER_ADDR_LEN];
1394 int i, rseg, error;
1395
1396 #define PCI_CONF_WRITE(r, v) pci_conf_write(pa->pa_pc, pa->pa_tag, (r), (v))
1397 #define PCI_CONF_READ(r) pci_conf_read(pa->pa_pc, pa->pa_tag, (r))
1398
1399 callout_init(&sc->vr_tick_ch);
1400
1401 vrt = vr_lookup(pa);
1402 if (vrt == NULL) {
1403 printf("\n");
1404 panic("vr_attach: impossible");
1405 }
1406
1407 printf(": %s Ethernet\n", vrt->vr_name);
1408
1409 /*
1410 * Handle power management nonsense.
1411 */
1412
1413 command = PCI_CONF_READ(VR_PCI_CAPID) & 0x000000FF;
1414 if (command == 0x01) {
1415 command = PCI_CONF_READ(VR_PCI_PWRMGMTCTRL);
1416 if (command & VR_PSTATE_MASK) {
1417 u_int32_t iobase, membase, irq;
1418
1419 /* Save important PCI config data. */
1420 iobase = PCI_CONF_READ(VR_PCI_LOIO);
1421 membase = PCI_CONF_READ(VR_PCI_LOMEM);
1422 irq = PCI_CONF_READ(VR_PCI_INTLINE);
1423
1424 /* Reset the power state. */
1425 printf("%s: chip is in D%d power mode "
1426 "-- setting to D0\n",
1427 sc->vr_dev.dv_xname, command & VR_PSTATE_MASK);
1428 command &= 0xFFFFFFFC;
1429 PCI_CONF_WRITE(VR_PCI_PWRMGMTCTRL, command);
1430
1431 /* Restore PCI config data. */
1432 PCI_CONF_WRITE(VR_PCI_LOIO, iobase);
1433 PCI_CONF_WRITE(VR_PCI_LOMEM, membase);
1434 PCI_CONF_WRITE(VR_PCI_INTLINE, irq);
1435 }
1436 }
1437
1438 /* Make sure bus mastering is enabled. */
1439 command = PCI_CONF_READ(PCI_COMMAND_STATUS_REG);
1440 command |= PCI_COMMAND_MASTER_ENABLE;
1441 PCI_CONF_WRITE(PCI_COMMAND_STATUS_REG, command);
1442
1443 /*
1444 * Map control/status registers.
1445 */
1446 {
1447 bus_space_tag_t iot, memt;
1448 bus_space_handle_t ioh, memh;
1449 int ioh_valid, memh_valid;
1450 pci_intr_handle_t intrhandle;
1451 const char *intrstr;
1452
1453 ioh_valid = (pci_mapreg_map(pa, VR_PCI_LOIO,
1454 PCI_MAPREG_TYPE_IO, 0,
1455 &iot, &ioh, NULL, NULL) == 0);
1456 memh_valid = (pci_mapreg_map(pa, VR_PCI_LOMEM,
1457 PCI_MAPREG_TYPE_MEM |
1458 PCI_MAPREG_MEM_TYPE_32BIT,
1459 0, &memt, &memh, NULL, NULL) == 0);
1460 #if defined(VR_USEIOSPACE)
1461 if (ioh_valid) {
1462 sc->vr_bst = iot;
1463 sc->vr_bsh = ioh;
1464 } else if (memh_valid) {
1465 sc->vr_bst = memt;
1466 sc->vr_bsh = memh;
1467 }
1468 #else
1469 if (memh_valid) {
1470 sc->vr_bst = memt;
1471 sc->vr_bsh = memh;
1472 } else if (ioh_valid) {
1473 sc->vr_bst = iot;
1474 sc->vr_bsh = ioh;
1475 }
1476 #endif
1477 else {
1478 printf(": unable to map device registers\n");
1479 return;
1480 }
1481
1482 /* Allocate interrupt */
1483 if (pci_intr_map(pa, &intrhandle)) {
1484 printf("%s: couldn't map interrupt\n",
1485 sc->vr_dev.dv_xname);
1486 return;
1487 }
1488 intrstr = pci_intr_string(pa->pa_pc, intrhandle);
1489 sc->vr_ih = pci_intr_establish(pa->pa_pc, intrhandle, IPL_NET,
1490 vr_intr, sc);
1491 if (sc->vr_ih == NULL) {
1492 printf("%s: couldn't establish interrupt",
1493 sc->vr_dev.dv_xname);
1494 if (intrstr != NULL)
1495 printf(" at %s", intrstr);
1496 printf("\n");
1497 }
1498 printf("%s: interrupting at %s\n",
1499 sc->vr_dev.dv_xname, intrstr);
1500 }
1501
1502 /* Reset the adapter. */
1503 vr_reset(sc);
1504
1505 /*
1506 * Get station address. The way the Rhine chips work,
1507 * you're not allowed to directly access the EEPROM once
1508 * they've been programmed a special way. Consequently,
1509 * we need to read the node address from the PAR0 and PAR1
1510 * registers.
1511 */
1512 VR_SETBIT(sc, VR_EECSR, VR_EECSR_LOAD);
1513 DELAY(200);
1514 for (i = 0; i < ETHER_ADDR_LEN; i++)
1515 eaddr[i] = CSR_READ_1(sc, VR_PAR0 + i);
1516
1517 /*
1518 * A Rhine chip was detected. Inform the world.
1519 */
1520 printf("%s: Ethernet address: %s\n",
1521 sc->vr_dev.dv_xname, ether_sprintf(eaddr));
1522
1523 bcopy(eaddr, sc->vr_enaddr, ETHER_ADDR_LEN);
1524
1525 sc->vr_dmat = pa->pa_dmat;
1526
1527 /*
1528 * Allocate the control data structures, and create and load
1529 * the DMA map for it.
1530 */
1531 if ((error = bus_dmamem_alloc(sc->vr_dmat,
1532 sizeof(struct vr_control_data), PAGE_SIZE, 0, &seg, 1, &rseg,
1533 0)) != 0) {
1534 printf("%s: unable to allocate control data, error = %d\n",
1535 sc->vr_dev.dv_xname, error);
1536 goto fail_0;
1537 }
1538
1539 if ((error = bus_dmamem_map(sc->vr_dmat, &seg, rseg,
1540 sizeof(struct vr_control_data), (caddr_t *)&sc->vr_control_data,
1541 BUS_DMA_COHERENT)) != 0) {
1542 printf("%s: unable to map control data, error = %d\n",
1543 sc->vr_dev.dv_xname, error);
1544 goto fail_1;
1545 }
1546
1547 if ((error = bus_dmamap_create(sc->vr_dmat,
1548 sizeof(struct vr_control_data), 1,
1549 sizeof(struct vr_control_data), 0, 0,
1550 &sc->vr_cddmamap)) != 0) {
1551 printf("%s: unable to create control data DMA map, "
1552 "error = %d\n", sc->vr_dev.dv_xname, error);
1553 goto fail_2;
1554 }
1555
1556 if ((error = bus_dmamap_load(sc->vr_dmat, sc->vr_cddmamap,
1557 sc->vr_control_data, sizeof(struct vr_control_data), NULL,
1558 0)) != 0) {
1559 printf("%s: unable to load control data DMA map, error = %d\n",
1560 sc->vr_dev.dv_xname, error);
1561 goto fail_3;
1562 }
1563
1564 /*
1565 * Create the transmit buffer DMA maps.
1566 */
1567 for (i = 0; i < VR_NTXDESC; i++) {
1568 if ((error = bus_dmamap_create(sc->vr_dmat, MCLBYTES,
1569 1, MCLBYTES, 0, 0,
1570 &VR_DSTX(sc, i)->ds_dmamap)) != 0) {
1571 printf("%s: unable to create tx DMA map %d, "
1572 "error = %d\n", sc->vr_dev.dv_xname, i, error);
1573 goto fail_4;
1574 }
1575 }
1576
1577 /*
1578 * Create the receive buffer DMA maps.
1579 */
1580 for (i = 0; i < VR_NRXDESC; i++) {
1581 if ((error = bus_dmamap_create(sc->vr_dmat, MCLBYTES, 1,
1582 MCLBYTES, 0, 0,
1583 &VR_DSRX(sc, i)->ds_dmamap)) != 0) {
1584 printf("%s: unable to create rx DMA map %d, "
1585 "error = %d\n", sc->vr_dev.dv_xname, i, error);
1586 goto fail_5;
1587 }
1588 VR_DSRX(sc, i)->ds_mbuf = NULL;
1589 }
1590
1591 ifp = &sc->vr_ec.ec_if;
1592 ifp->if_softc = sc;
1593 ifp->if_mtu = ETHERMTU;
1594 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1595 ifp->if_ioctl = vr_ioctl;
1596 ifp->if_start = vr_start;
1597 ifp->if_watchdog = vr_watchdog;
1598 ifp->if_init = vr_init;
1599 ifp->if_stop = vr_stop;
1600 IFQ_SET_READY(&ifp->if_snd);
1601
1602 bcopy(sc->vr_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
1603
1604 /*
1605 * Initialize MII/media info.
1606 */
1607 sc->vr_mii.mii_ifp = ifp;
1608 sc->vr_mii.mii_readreg = vr_mii_readreg;
1609 sc->vr_mii.mii_writereg = vr_mii_writereg;
1610 sc->vr_mii.mii_statchg = vr_mii_statchg;
1611 ifmedia_init(&sc->vr_mii.mii_media, 0, vr_ifmedia_upd, vr_ifmedia_sts);
1612 mii_attach(&sc->vr_dev, &sc->vr_mii, 0xffffffff, MII_PHY_ANY,
1613 MII_OFFSET_ANY, 0);
1614 if (LIST_FIRST(&sc->vr_mii.mii_phys) == NULL) {
1615 ifmedia_add(&sc->vr_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
1616 ifmedia_set(&sc->vr_mii.mii_media, IFM_ETHER|IFM_NONE);
1617 } else
1618 ifmedia_set(&sc->vr_mii.mii_media, IFM_ETHER|IFM_AUTO);
1619
1620 /*
1621 * Call MI attach routines.
1622 */
1623 if_attach(ifp);
1624 ether_ifattach(ifp, sc->vr_enaddr);
1625
1626 sc->vr_ats = shutdownhook_establish(vr_shutdown, sc);
1627 if (sc->vr_ats == NULL)
1628 printf("%s: warning: couldn't establish shutdown hook\n",
1629 sc->vr_dev.dv_xname);
1630 return;
1631
1632 fail_5:
1633 for (i = 0; i < VR_NRXDESC; i++) {
1634 if (sc->vr_rxsoft[i].ds_dmamap != NULL)
1635 bus_dmamap_destroy(sc->vr_dmat,
1636 sc->vr_rxsoft[i].ds_dmamap);
1637 }
1638 fail_4:
1639 for (i = 0; i < VR_NTXDESC; i++) {
1640 if (sc->vr_txsoft[i].ds_dmamap != NULL)
1641 bus_dmamap_destroy(sc->vr_dmat,
1642 sc->vr_txsoft[i].ds_dmamap);
1643 }
1644 bus_dmamap_unload(sc->vr_dmat, sc->vr_cddmamap);
1645 fail_3:
1646 bus_dmamap_destroy(sc->vr_dmat, sc->vr_cddmamap);
1647 fail_2:
1648 bus_dmamem_unmap(sc->vr_dmat, (caddr_t)sc->vr_control_data,
1649 sizeof(struct vr_control_data));
1650 fail_1:
1651 bus_dmamem_free(sc->vr_dmat, &seg, rseg);
1652 fail_0:
1653 return;
1654 }
1655