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