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