if_vge.c revision 135091
1104483Ssam/* 2104483Ssam * Copyright (c) 2004 3104483Ssam * Bill Paul <wpaul@windriver.com>. All rights reserved. 4104483Ssam * 5112127Ssam * Redistribution and use in source and binary forms, with or without 6104483Ssam * modification, are permitted provided that the following conditions 7106863Smux * are met: 8104483Ssam * 1. Redistributions of source code must retain the above copyright 9112127Ssam * notice, this list of conditions and the following disclaimer. 10112127Ssam * 2. Redistributions in binary form must reproduce the above copyright 11112127Ssam * notice, this list of conditions and the following disclaimer in the 12104483Ssam * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by Bill Paul. 16 * 4. Neither the name of the author nor the names of any co-contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD 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 30 * THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33#include <sys/cdefs.h> 34__FBSDID("$FreeBSD: head/sys/dev/vge/if_vge.c 135091 2004-09-11 22:13:25Z wpaul $"); 35 36/* 37 * VIA Networking Technologies VT612x PCI gigabit ethernet NIC driver. 38 * 39 * Written by Bill Paul <wpaul@windriver.com> 40 * Senior Networking Software Engineer 41 * Wind River Systems 42 */ 43 44/* 45 * The VIA Networking VT6122 is a 32bit, 33/66Mhz PCI device that 46 * combines a tri-speed ethernet MAC and PHY, with the following 47 * features: 48 * 49 * o Jumbo frame support up to 16K 50 * o Transmit and receive flow control 51 * o IPv4 checksum offload 52 * o VLAN tag insertion and stripping 53 * o TCP large send 54 * o 64-bit multicast hash table filter 55 * o 64 entry CAM filter 56 * o 16K RX FIFO and 48K TX FIFO memory 57 * o Interrupt moderation 58 * 59 * The VT6122 supports up to four transmit DMA queues. The descriptors 60 * in the transmit ring can address up to 7 data fragments; frames which 61 * span more than 7 data buffers must be coalesced, but in general the 62 * BSD TCP/IP stack rarely generates frames more than 2 or 3 fragments 63 * long. The receive descriptors address only a single buffer. 64 * 65 * There are two peculiar design issues with the VT6122. One is that 66 * receive data buffers must be aligned on a 32-bit boundary. This is 67 * not a problem where the VT6122 is used as a LOM device in x86-based 68 * systems, but on architectures that generate unaligned access traps, we 69 * have to do some copying. 70 * 71 * The other issue has to do with the way 64-bit addresses are handled. 72 * The DMA descriptors only allow you to specify 48 bits of addressing 73 * information. The remaining 16 bits are specified using one of the 74 * I/O registers. If you only have a 32-bit system, then this isn't 75 * an issue, but if you have a 64-bit system and more than 4GB of 76 * memory, you must have to make sure your network data buffers reside 77 * in the same 48-bit 'segment.' 78 * 79 * Special thanks to Ryan Fu at VIA Networking for providing documentation 80 * and sample NICs for testing. 81 */ 82 83#include <sys/param.h> 84#include <sys/endian.h> 85#include <sys/systm.h> 86#include <sys/sockio.h> 87#include <sys/mbuf.h> 88#include <sys/malloc.h> 89#include <sys/module.h> 90#include <sys/kernel.h> 91#include <sys/socket.h> 92#include <sys/taskqueue.h> 93 94#include <net/if.h> 95#include <net/if_arp.h> 96#include <net/ethernet.h> 97#include <net/if_dl.h> 98#include <net/if_media.h> 99#include <net/if_vlan_var.h> 100#include <net/route.h> 101 102#include <net/bpf.h> 103 104#include <machine/bus_pio.h> 105#include <machine/bus_memio.h> 106#include <machine/bus.h> 107#include <machine/resource.h> 108#include <sys/bus.h> 109#include <sys/rman.h> 110 111#include <dev/mii/mii.h> 112#include <dev/mii/miivar.h> 113 114#include <dev/pci/pcireg.h> 115#include <dev/pci/pcivar.h> 116 117MODULE_DEPEND(vge, pci, 1, 1, 1); 118MODULE_DEPEND(vge, ether, 1, 1, 1); 119MODULE_DEPEND(vge, miibus, 1, 1, 1); 120 121/* "controller miibus0" required. See GENERIC if you get errors here. */ 122#include "miibus_if.h" 123 124#include <dev/vge/if_vgereg.h> 125#include <dev/vge/if_vgevar.h> 126 127#define VGE_CSUM_FEATURES (CSUM_IP | CSUM_TCP | CSUM_UDP) 128 129/* 130 * Various supported device vendors/types and their names. 131 */ 132static struct vge_type vge_devs[] = { 133 { VIA_VENDORID, VIA_DEVICEID_61XX, 134 "VIA Networking Gigabit Ethernet" }, 135 { 0, 0, NULL } 136}; 137 138static int vge_probe (device_t); 139static int vge_attach (device_t); 140static int vge_detach (device_t); 141 142static int vge_encap (struct vge_softc *, struct mbuf *, int); 143 144static void vge_dma_map_addr (void *, bus_dma_segment_t *, int, int); 145static void vge_dma_map_rx_desc (void *, bus_dma_segment_t *, int, 146 bus_size_t, int); 147static void vge_dma_map_tx_desc (void *, bus_dma_segment_t *, int, 148 bus_size_t, int); 149static int vge_allocmem (device_t, struct vge_softc *); 150static int vge_newbuf (struct vge_softc *, int, struct mbuf *); 151static int vge_rx_list_init (struct vge_softc *); 152static int vge_tx_list_init (struct vge_softc *); 153#ifdef VGE_FIXUP_RX 154static __inline void vge_fixup_rx 155 (struct mbuf *); 156#endif 157static void vge_rxeof (struct vge_softc *); 158static void vge_txeof (struct vge_softc *); 159static void vge_intr (void *); 160static void vge_tick (void *); 161static void vge_tx_task (void *, int); 162static void vge_start (struct ifnet *); 163static int vge_ioctl (struct ifnet *, u_long, caddr_t); 164static void vge_init (void *); 165static void vge_stop (struct vge_softc *); 166static void vge_watchdog (struct ifnet *); 167static int vge_suspend (device_t); 168static int vge_resume (device_t); 169static void vge_shutdown (device_t); 170static int vge_ifmedia_upd (struct ifnet *); 171static void vge_ifmedia_sts (struct ifnet *, struct ifmediareq *); 172 173static void vge_eeprom_getword (struct vge_softc *, int, u_int16_t *); 174static void vge_read_eeprom (struct vge_softc *, caddr_t, int, int, int); 175 176static void vge_miipoll_start (struct vge_softc *); 177static void vge_miipoll_stop (struct vge_softc *); 178static int vge_miibus_readreg (device_t, int, int); 179static int vge_miibus_writereg (device_t, int, int, int); 180static void vge_miibus_statchg (device_t); 181 182static void vge_cam_clear (struct vge_softc *); 183static int vge_cam_set (struct vge_softc *, uint8_t *); 184#if __FreeBSD_version < 502113 185static uint32_t vge_mchash (uint8_t *); 186#endif 187static void vge_setmulti (struct vge_softc *); 188static void vge_reset (struct vge_softc *); 189 190#define VGE_PCI_LOIO 0x10 191#define VGE_PCI_LOMEM 0x14 192 193static device_method_t vge_methods[] = { 194 /* Device interface */ 195 DEVMETHOD(device_probe, vge_probe), 196 DEVMETHOD(device_attach, vge_attach), 197 DEVMETHOD(device_detach, vge_detach), 198 DEVMETHOD(device_suspend, vge_suspend), 199 DEVMETHOD(device_resume, vge_resume), 200 DEVMETHOD(device_shutdown, vge_shutdown), 201 202 /* bus interface */ 203 DEVMETHOD(bus_print_child, bus_generic_print_child), 204 DEVMETHOD(bus_driver_added, bus_generic_driver_added), 205 206 /* MII interface */ 207 DEVMETHOD(miibus_readreg, vge_miibus_readreg), 208 DEVMETHOD(miibus_writereg, vge_miibus_writereg), 209 DEVMETHOD(miibus_statchg, vge_miibus_statchg), 210 211 { 0, 0 } 212}; 213 214static driver_t vge_driver = { 215 "vge", 216 vge_methods, 217 sizeof(struct vge_softc) 218}; 219 220static devclass_t vge_devclass; 221 222DRIVER_MODULE(vge, pci, vge_driver, vge_devclass, 0, 0); 223DRIVER_MODULE(vge, cardbus, vge_driver, vge_devclass, 0, 0); 224DRIVER_MODULE(miibus, vge, miibus_driver, miibus_devclass, 0, 0); 225 226/* 227 * Read a word of data stored in the EEPROM at address 'addr.' 228 */ 229static void 230vge_eeprom_getword(sc, addr, dest) 231 struct vge_softc *sc; 232 int addr; 233 u_int16_t *dest; 234{ 235 register int i; 236 u_int16_t word = 0; 237 238 /* 239 * Enter EEPROM embedded programming mode. In order to 240 * access the EEPROM at all, we first have to set the 241 * EELOAD bit in the CHIPCFG2 register. 242 */ 243 CSR_SETBIT_1(sc, VGE_CHIPCFG2, VGE_CHIPCFG2_EELOAD); 244 CSR_SETBIT_1(sc, VGE_EECSR, VGE_EECSR_EMBP/*|VGE_EECSR_ECS*/); 245 246 /* Select the address of the word we want to read */ 247 CSR_WRITE_1(sc, VGE_EEADDR, addr); 248 249 /* Issue read command */ 250 CSR_SETBIT_1(sc, VGE_EECMD, VGE_EECMD_ERD); 251 252 /* Wait for the done bit to be set. */ 253 for (i = 0; i < VGE_TIMEOUT; i++) { 254 if (CSR_READ_1(sc, VGE_EECMD) & VGE_EECMD_EDONE) 255 break; 256 } 257 258 if (i == VGE_TIMEOUT) { 259 device_printf(sc->vge_dev, "EEPROM read timed out\n"); 260 *dest = 0; 261 return; 262 } 263 264 /* Read the result */ 265 word = CSR_READ_2(sc, VGE_EERDDAT); 266 267 /* Turn off EEPROM access mode. */ 268 CSR_CLRBIT_1(sc, VGE_EECSR, VGE_EECSR_EMBP/*|VGE_EECSR_ECS*/); 269 CSR_CLRBIT_1(sc, VGE_CHIPCFG2, VGE_CHIPCFG2_EELOAD); 270 271 *dest = word; 272 273 return; 274} 275 276/* 277 * Read a sequence of words from the EEPROM. 278 */ 279static void 280vge_read_eeprom(sc, dest, off, cnt, swap) 281 struct vge_softc *sc; 282 caddr_t dest; 283 int off; 284 int cnt; 285 int swap; 286{ 287 int i; 288 u_int16_t word = 0, *ptr; 289 290 for (i = 0; i < cnt; i++) { 291 vge_eeprom_getword(sc, off + i, &word); 292 ptr = (u_int16_t *)(dest + (i * 2)); 293 if (swap) 294 *ptr = ntohs(word); 295 else 296 *ptr = word; 297 } 298} 299 300static void 301vge_miipoll_stop(sc) 302 struct vge_softc *sc; 303{ 304 int i; 305 306 CSR_WRITE_1(sc, VGE_MIICMD, 0); 307 308 for (i = 0; i < VGE_TIMEOUT; i++) { 309 DELAY(1); 310 if (CSR_READ_1(sc, VGE_MIISTS) & VGE_MIISTS_IIDL) 311 break; 312 } 313 314 if (i == VGE_TIMEOUT) 315 device_printf(sc->vge_dev, "failed to idle MII autopoll\n"); 316 317 return; 318} 319 320static void 321vge_miipoll_start(sc) 322 struct vge_softc *sc; 323{ 324 int i; 325 326 /* First, make sure we're idle. */ 327 328 CSR_WRITE_1(sc, VGE_MIICMD, 0); 329 CSR_WRITE_1(sc, VGE_MIIADDR, VGE_MIIADDR_SWMPL); 330 331 for (i = 0; i < VGE_TIMEOUT; i++) { 332 DELAY(1); 333 if (CSR_READ_1(sc, VGE_MIISTS) & VGE_MIISTS_IIDL) 334 break; 335 } 336 337 if (i == VGE_TIMEOUT) { 338 device_printf(sc->vge_dev, "failed to idle MII autopoll\n"); 339 return; 340 } 341 342 /* Now enable auto poll mode. */ 343 344 CSR_WRITE_1(sc, VGE_MIICMD, VGE_MIICMD_MAUTO); 345 346 /* And make sure it started. */ 347 348 for (i = 0; i < VGE_TIMEOUT; i++) { 349 DELAY(1); 350 if ((CSR_READ_1(sc, VGE_MIISTS) & VGE_MIISTS_IIDL) == 0) 351 break; 352 } 353 354 if (i == VGE_TIMEOUT) 355 device_printf(sc->vge_dev, "failed to start MII autopoll\n"); 356 357 return; 358} 359 360static int 361vge_miibus_readreg(dev, phy, reg) 362 device_t dev; 363 int phy, reg; 364{ 365 struct vge_softc *sc; 366 int i; 367 u_int16_t rval = 0; 368 369 sc = device_get_softc(dev); 370 371 if (phy != (CSR_READ_1(sc, VGE_MIICFG) & 0x1F)) 372 return(0); 373 374 VGE_LOCK(sc); 375 vge_miipoll_stop(sc); 376 377 /* Specify the register we want to read. */ 378 CSR_WRITE_1(sc, VGE_MIIADDR, reg); 379 380 /* Issue read command. */ 381 CSR_SETBIT_1(sc, VGE_MIICMD, VGE_MIICMD_RCMD); 382 383 /* Wait for the read command bit to self-clear. */ 384 for (i = 0; i < VGE_TIMEOUT; i++) { 385 DELAY(1); 386 if ((CSR_READ_1(sc, VGE_MIICMD) & VGE_MIICMD_RCMD) == 0) 387 break; 388 } 389 390 if (i == VGE_TIMEOUT) 391 device_printf(sc->vge_dev, "MII read timed out\n"); 392 else 393 rval = CSR_READ_2(sc, VGE_MIIDATA); 394 395 vge_miipoll_start(sc); 396 VGE_UNLOCK(sc); 397 398 return (rval); 399} 400 401static int 402vge_miibus_writereg(dev, phy, reg, data) 403 device_t dev; 404 int phy, reg, data; 405{ 406 struct vge_softc *sc; 407 int i, rval = 0; 408 409 sc = device_get_softc(dev); 410 411 if (phy != (CSR_READ_1(sc, VGE_MIICFG) & 0x1F)) 412 return(0); 413 414 VGE_LOCK(sc); 415 vge_miipoll_stop(sc); 416 417 /* Specify the register we want to write. */ 418 CSR_WRITE_1(sc, VGE_MIIADDR, reg); 419 420 /* Specify the data we want to write. */ 421 CSR_WRITE_2(sc, VGE_MIIDATA, data); 422 423 /* Issue write command. */ 424 CSR_SETBIT_1(sc, VGE_MIICMD, VGE_MIICMD_WCMD); 425 426 /* Wait for the write command bit to self-clear. */ 427 for (i = 0; i < VGE_TIMEOUT; i++) { 428 DELAY(1); 429 if ((CSR_READ_1(sc, VGE_MIICMD) & VGE_MIICMD_WCMD) == 0) 430 break; 431 } 432 433 if (i == VGE_TIMEOUT) { 434 device_printf(sc->vge_dev, "MII write timed out\n"); 435 rval = EIO; 436 } 437 438 vge_miipoll_start(sc); 439 VGE_UNLOCK(sc); 440 441 return (rval); 442} 443 444static void 445vge_cam_clear(sc) 446 struct vge_softc *sc; 447{ 448 int i; 449 450 /* 451 * Turn off all the mask bits. This tells the chip 452 * that none of the entries in the CAM filter are valid. 453 * desired entries will be enabled as we fill the filter in. 454 */ 455 456 CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL); 457 CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_CAMMASK); 458 CSR_WRITE_1(sc, VGE_CAMADDR, VGE_CAMADDR_ENABLE); 459 for (i = 0; i < 8; i++) 460 CSR_WRITE_1(sc, VGE_CAM0 + i, 0); 461 462 /* Clear the VLAN filter too. */ 463 464 CSR_WRITE_1(sc, VGE_CAMADDR, VGE_CAMADDR_ENABLE|VGE_CAMADDR_AVSEL|0); 465 for (i = 0; i < 8; i++) 466 CSR_WRITE_1(sc, VGE_CAM0 + i, 0); 467 468 CSR_WRITE_1(sc, VGE_CAMADDR, 0); 469 CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL); 470 CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_MAR); 471 472 sc->vge_camidx = 0; 473 474 return; 475} 476 477static int 478vge_cam_set(sc, addr) 479 struct vge_softc *sc; 480 uint8_t *addr; 481{ 482 int i, error = 0; 483 484 if (sc->vge_camidx == VGE_CAM_MAXADDRS) 485 return(ENOSPC); 486 487 /* Select the CAM data page. */ 488 CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL); 489 CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_CAMDATA); 490 491 /* Set the filter entry we want to update and enable writing. */ 492 CSR_WRITE_1(sc, VGE_CAMADDR, VGE_CAMADDR_ENABLE|sc->vge_camidx); 493 494 /* Write the address to the CAM registers */ 495 for (i = 0; i < ETHER_ADDR_LEN; i++) 496 CSR_WRITE_1(sc, VGE_CAM0 + i, addr[i]); 497 498 /* Issue a write command. */ 499 CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_WRITE); 500 501 /* Wake for it to clear. */ 502 for (i = 0; i < VGE_TIMEOUT; i++) { 503 DELAY(1); 504 if ((CSR_READ_1(sc, VGE_CAMCTL) & VGE_CAMCTL_WRITE) == 0) 505 break; 506 } 507 508 if (i == VGE_TIMEOUT) { 509 device_printf(sc->vge_dev, "setting CAM filter failed\n"); 510 error = EIO; 511 goto fail; 512 } 513 514 /* Select the CAM mask page. */ 515 CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL); 516 CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_CAMMASK); 517 518 /* Set the mask bit that enables this filter. */ 519 CSR_SETBIT_1(sc, VGE_CAM0 + (sc->vge_camidx/8), 520 1<<(sc->vge_camidx & 7)); 521 522 sc->vge_camidx++; 523 524fail: 525 /* Turn off access to CAM. */ 526 CSR_WRITE_1(sc, VGE_CAMADDR, 0); 527 CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL); 528 CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_MAR); 529 530 return (error); 531} 532 533#if __FreeBSD_version < 502113 534static uint32_t 535vge_mchash(addr) 536 uint8_t *addr; 537{ 538 uint32_t crc, carry; 539 int idx, bit; 540 uint8_t data; 541 542 /* Compute CRC for the address value. */ 543 crc = 0xFFFFFFFF; /* initial value */ 544 545 for (idx = 0; idx < 6; idx++) { 546 for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1) { 547 carry = ((crc & 0x80000000) ? 1 : 0) ^ (data & 0x01); 548 crc <<= 1; 549 if (carry) 550 crc = (crc ^ 0x04c11db6) | carry; 551 } 552 } 553 554 return(crc); 555} 556#endif 557 558/* 559 * Program the multicast filter. We use the 64-entry CAM filter 560 * for perfect filtering. If there's more than 64 multicast addresses, 561 * we use the hash filter insted. 562 */ 563static void 564vge_setmulti(sc) 565 struct vge_softc *sc; 566{ 567 struct ifnet *ifp; 568 int error = 0/*, h = 0*/; 569 struct ifmultiaddr *ifma; 570 u_int32_t h, hashes[2] = { 0, 0 }; 571 572 ifp = &sc->arpcom.ac_if; 573 574 /* First, zot all the multicast entries. */ 575 vge_cam_clear(sc); 576 CSR_WRITE_4(sc, VGE_MAR0, 0); 577 CSR_WRITE_4(sc, VGE_MAR1, 0); 578 579 /* 580 * If the user wants allmulti or promisc mode, enable reception 581 * of all multicast frames. 582 */ 583 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) { 584 CSR_WRITE_4(sc, VGE_MAR0, 0xFFFFFFFF); 585 CSR_WRITE_4(sc, VGE_MAR1, 0xFFFFFFFF); 586 return; 587 } 588 589 /* Now program new ones */ 590 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 591 if (ifma->ifma_addr->sa_family != AF_LINK) 592 continue; 593 error = vge_cam_set(sc, 594 LLADDR((struct sockaddr_dl *)ifma->ifma_addr)); 595 if (error) 596 break; 597 } 598 599 /* If there were too many addresses, use the hash filter. */ 600 if (error) { 601 vge_cam_clear(sc); 602 603 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 604 if (ifma->ifma_addr->sa_family != AF_LINK) 605 continue; 606#if __FreeBSD_version < 502113 607 h = vge_mchash(LLADDR((struct sockaddr_dl *) 608 ifma->ifma_addr)) >> 26; 609#else 610 h = ether_crc32_be(LLADDR((struct sockaddr_dl *) 611 ifma->ifma_addr), ETHER_ADDR_LEN) >> 26; 612#endif 613 if (h < 32) 614 hashes[0] |= (1 << h); 615 else 616 hashes[1] |= (1 << (h - 32)); 617 } 618 619 CSR_WRITE_4(sc, VGE_MAR0, hashes[0]); 620 CSR_WRITE_4(sc, VGE_MAR1, hashes[1]); 621 } 622 623 return; 624} 625 626static void 627vge_reset(sc) 628 struct vge_softc *sc; 629{ 630 register int i; 631 632 CSR_WRITE_1(sc, VGE_CRS1, VGE_CR1_SOFTRESET); 633 634 for (i = 0; i < VGE_TIMEOUT; i++) { 635 DELAY(5); 636 if ((CSR_READ_1(sc, VGE_CRS1) & VGE_CR1_SOFTRESET) == 0) 637 break; 638 } 639 640 if (i == VGE_TIMEOUT) { 641 device_printf(sc->vge_dev, "soft reset timed out"); 642 CSR_WRITE_1(sc, VGE_CRS3, VGE_CR3_STOP_FORCE); 643 DELAY(2000); 644 } 645 646 DELAY(5000); 647 648 CSR_SETBIT_1(sc, VGE_EECSR, VGE_EECSR_RELOAD); 649 650 for (i = 0; i < VGE_TIMEOUT; i++) { 651 DELAY(5); 652 if ((CSR_READ_1(sc, VGE_EECSR) & VGE_EECSR_RELOAD) == 0) 653 break; 654 } 655 656 if (i == VGE_TIMEOUT) { 657 device_printf(sc->vge_dev, "EEPROM reload timed out\n"); 658 return; 659 } 660 661 CSR_CLRBIT_1(sc, VGE_CHIPCFG0, VGE_CHIPCFG0_PACPI); 662 663 return; 664} 665 666/* 667 * Probe for a VIA gigabit chip. Check the PCI vendor and device 668 * IDs against our list and return a device name if we find a match. 669 */ 670static int 671vge_probe(dev) 672 device_t dev; 673{ 674 struct vge_type *t; 675 struct vge_softc *sc; 676 677 t = vge_devs; 678 sc = device_get_softc(dev); 679 680 while (t->vge_name != NULL) { 681 if ((pci_get_vendor(dev) == t->vge_vid) && 682 (pci_get_device(dev) == t->vge_did)) { 683 device_set_desc(dev, t->vge_name); 684 return (0); 685 } 686 t++; 687 } 688 689 return (ENXIO); 690} 691 692static void 693vge_dma_map_rx_desc(arg, segs, nseg, mapsize, error) 694 void *arg; 695 bus_dma_segment_t *segs; 696 int nseg; 697 bus_size_t mapsize; 698 int error; 699{ 700 701 struct vge_dmaload_arg *ctx; 702 struct vge_rx_desc *d = NULL; 703 704 if (error) 705 return; 706 707 ctx = arg; 708 709 /* Signal error to caller if there's too many segments */ 710 if (nseg > ctx->vge_maxsegs) { 711 ctx->vge_maxsegs = 0; 712 return; 713 } 714 715 /* 716 * Map the segment array into descriptors. 717 */ 718 719 d = &ctx->sc->vge_ldata.vge_rx_list[ctx->vge_idx]; 720 721 /* If this descriptor is still owned by the chip, bail. */ 722 723 if (le32toh(d->vge_sts) & VGE_RDSTS_OWN) { 724 device_printf(ctx->sc->vge_dev, 725 "tried to map busy descriptor\n"); 726 ctx->vge_maxsegs = 0; 727 return; 728 } 729 730 d->vge_buflen = htole16(VGE_BUFLEN(segs[0].ds_len) | VGE_RXDESC_I); 731 d->vge_addrlo = htole32(VGE_ADDR_LO(segs[0].ds_addr)); 732 d->vge_addrhi = htole16(VGE_ADDR_HI(segs[0].ds_addr) & 0xFFFF); 733 d->vge_sts = 0; 734 d->vge_ctl = 0; 735 736 ctx->vge_maxsegs = 1; 737 738 return; 739} 740 741static void 742vge_dma_map_tx_desc(arg, segs, nseg, mapsize, error) 743 void *arg; 744 bus_dma_segment_t *segs; 745 int nseg; 746 bus_size_t mapsize; 747 int error; 748{ 749 struct vge_dmaload_arg *ctx; 750 struct vge_tx_desc *d = NULL; 751 struct vge_tx_frag *f; 752 int i = 0; 753 754 if (error) 755 return; 756 757 ctx = arg; 758 759 /* Signal error to caller if there's too many segments */ 760 if (nseg > ctx->vge_maxsegs) { 761 ctx->vge_maxsegs = 0; 762 return; 763 } 764 765 /* Map the segment array into descriptors. */ 766 767 d = &ctx->sc->vge_ldata.vge_tx_list[ctx->vge_idx]; 768 769 /* If this descriptor is still owned by the chip, bail. */ 770 771 if (le32toh(d->vge_sts) & VGE_TDSTS_OWN) { 772 ctx->vge_maxsegs = 0; 773 return; 774 } 775 776 for (i = 0; i < nseg; i++) { 777 f = &d->vge_frag[i]; 778 f->vge_buflen = htole16(VGE_BUFLEN(segs[i].ds_len)); 779 f->vge_addrlo = htole32(VGE_ADDR_LO(segs[i].ds_addr)); 780 f->vge_addrhi = htole16(VGE_ADDR_HI(segs[i].ds_addr) & 0xFFFF); 781 } 782 783 /* Argh. This chip does not autopad short frames */ 784 785 if (ctx->vge_m0->m_pkthdr.len < VGE_MIN_FRAMELEN) { 786 f = &d->vge_frag[i]; 787 f->vge_buflen = htole16(VGE_BUFLEN(VGE_MIN_FRAMELEN - 788 ctx->vge_m0->m_pkthdr.len)); 789 f->vge_addrlo = htole32(VGE_ADDR_LO(segs[0].ds_addr)); 790 f->vge_addrhi = htole16(VGE_ADDR_HI(segs[0].ds_addr) & 0xFFFF); 791 ctx->vge_m0->m_pkthdr.len = VGE_MIN_FRAMELEN; 792 i++; 793 } 794 795 /* 796 * When telling the chip how many segments there are, we 797 * must use nsegs + 1 instead of just nsegs. Darned if I 798 * know why. 799 */ 800 i++; 801 802 d->vge_sts = ctx->vge_m0->m_pkthdr.len << 16; 803 d->vge_ctl = ctx->vge_flags|(i << 28)|VGE_TD_LS_NORM; 804 805 if (ctx->vge_m0->m_pkthdr.len > ETHERMTU + ETHER_HDR_LEN) 806 d->vge_ctl |= VGE_TDCTL_JUMBO; 807 808 ctx->vge_maxsegs = nseg; 809 810 return; 811} 812 813/* 814 * Map a single buffer address. 815 */ 816 817static void 818vge_dma_map_addr(arg, segs, nseg, error) 819 void *arg; 820 bus_dma_segment_t *segs; 821 int nseg; 822 int error; 823{ 824 bus_addr_t *addr; 825 826 if (error) 827 return; 828 829 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg)); 830 addr = arg; 831 *addr = segs->ds_addr; 832 833 return; 834} 835 836static int 837vge_allocmem(dev, sc) 838 device_t dev; 839 struct vge_softc *sc; 840{ 841 int error; 842 int nseg; 843 int i; 844 845 /* 846 * Allocate map for RX mbufs. 847 */ 848 nseg = 32; 849 error = bus_dma_tag_create(sc->vge_parent_tag, ETHER_ALIGN, 0, 850 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, 851 NULL, MCLBYTES * nseg, nseg, MCLBYTES, BUS_DMA_ALLOCNOW, 852 NULL, NULL, &sc->vge_ldata.vge_mtag); 853 if (error) { 854 device_printf(dev, "could not allocate dma tag\n"); 855 return (ENOMEM); 856 } 857 858 /* 859 * Allocate map for TX descriptor list. 860 */ 861 error = bus_dma_tag_create(sc->vge_parent_tag, VGE_RING_ALIGN, 862 0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, 863 NULL, VGE_TX_LIST_SZ, 1, VGE_TX_LIST_SZ, BUS_DMA_ALLOCNOW, 864 NULL, NULL, &sc->vge_ldata.vge_tx_list_tag); 865 if (error) { 866 device_printf(dev, "could not allocate dma tag\n"); 867 return (ENOMEM); 868 } 869 870 /* Allocate DMA'able memory for the TX ring */ 871 872 error = bus_dmamem_alloc(sc->vge_ldata.vge_tx_list_tag, 873 (void **)&sc->vge_ldata.vge_tx_list, BUS_DMA_NOWAIT | BUS_DMA_ZERO, 874 &sc->vge_ldata.vge_tx_list_map); 875 if (error) 876 return (ENOMEM); 877 878 /* Load the map for the TX ring. */ 879 880 error = bus_dmamap_load(sc->vge_ldata.vge_tx_list_tag, 881 sc->vge_ldata.vge_tx_list_map, sc->vge_ldata.vge_tx_list, 882 VGE_TX_LIST_SZ, vge_dma_map_addr, 883 &sc->vge_ldata.vge_tx_list_addr, BUS_DMA_NOWAIT); 884 885 /* Create DMA maps for TX buffers */ 886 887 for (i = 0; i < VGE_TX_DESC_CNT; i++) { 888 error = bus_dmamap_create(sc->vge_ldata.vge_mtag, 0, 889 &sc->vge_ldata.vge_tx_dmamap[i]); 890 if (error) { 891 device_printf(dev, "can't create DMA map for TX\n"); 892 return (ENOMEM); 893 } 894 } 895 896 /* 897 * Allocate map for RX descriptor list. 898 */ 899 error = bus_dma_tag_create(sc->vge_parent_tag, VGE_RING_ALIGN, 900 0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, 901 NULL, VGE_TX_LIST_SZ, 1, VGE_TX_LIST_SZ, BUS_DMA_ALLOCNOW, 902 NULL, NULL, &sc->vge_ldata.vge_rx_list_tag); 903 if (error) { 904 device_printf(dev, "could not allocate dma tag\n"); 905 return (ENOMEM); 906 } 907 908 /* Allocate DMA'able memory for the RX ring */ 909 910 error = bus_dmamem_alloc(sc->vge_ldata.vge_rx_list_tag, 911 (void **)&sc->vge_ldata.vge_rx_list, BUS_DMA_NOWAIT | BUS_DMA_ZERO, 912 &sc->vge_ldata.vge_rx_list_map); 913 if (error) 914 return (ENOMEM); 915 916 /* Load the map for the RX ring. */ 917 918 error = bus_dmamap_load(sc->vge_ldata.vge_rx_list_tag, 919 sc->vge_ldata.vge_rx_list_map, sc->vge_ldata.vge_rx_list, 920 VGE_TX_LIST_SZ, vge_dma_map_addr, 921 &sc->vge_ldata.vge_rx_list_addr, BUS_DMA_NOWAIT); 922 923 /* Create DMA maps for RX buffers */ 924 925 for (i = 0; i < VGE_RX_DESC_CNT; i++) { 926 error = bus_dmamap_create(sc->vge_ldata.vge_mtag, 0, 927 &sc->vge_ldata.vge_rx_dmamap[i]); 928 if (error) { 929 device_printf(dev, "can't create DMA map for RX\n"); 930 return (ENOMEM); 931 } 932 } 933 934 return (0); 935} 936 937/* 938 * Attach the interface. Allocate softc structures, do ifmedia 939 * setup and ethernet/BPF attach. 940 */ 941static int 942vge_attach(dev) 943 device_t dev; 944{ 945 u_char eaddr[ETHER_ADDR_LEN]; 946 struct vge_softc *sc; 947 struct ifnet *ifp; 948 int unit, error = 0, rid; 949 950 sc = device_get_softc(dev); 951 unit = device_get_unit(dev); 952 sc->vge_dev = dev; 953 954 mtx_init(&sc->vge_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK, 955 MTX_DEF | MTX_RECURSE); 956 /* 957 * Map control/status registers. 958 */ 959 pci_enable_busmaster(dev); 960 961 rid = VGE_PCI_LOMEM; 962 sc->vge_res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid, 963 0, ~0, 1, RF_ACTIVE); 964 965 if (sc->vge_res == NULL) { 966 printf ("vge%d: couldn't map ports/memory\n", unit); 967 error = ENXIO; 968 goto fail; 969 } 970 971 sc->vge_btag = rman_get_bustag(sc->vge_res); 972 sc->vge_bhandle = rman_get_bushandle(sc->vge_res); 973 974 /* Allocate interrupt */ 975 rid = 0; 976 sc->vge_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 977 0, ~0, 1, RF_SHAREABLE | RF_ACTIVE); 978 979 if (sc->vge_irq == NULL) { 980 printf("vge%d: couldn't map interrupt\n", unit); 981 error = ENXIO; 982 goto fail; 983 } 984 985 /* Reset the adapter. */ 986 vge_reset(sc); 987 988 /* 989 * Get station address from the EEPROM. 990 */ 991 vge_read_eeprom(sc, (caddr_t)eaddr, VGE_EE_EADDR, 3, 0); 992 993 sc->vge_unit = unit; 994 bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN); 995 996#if __FreeBSD_version < 502113 997 printf("vge%d: Ethernet address: %6D\n", unit, eaddr, ":"); 998#endif 999 1000 /* 1001 * Allocate the parent bus DMA tag appropriate for PCI. 1002 */ 1003#define VGE_NSEG_NEW 32 1004 error = bus_dma_tag_create(NULL, /* parent */ 1005 1, 0, /* alignment, boundary */ 1006 BUS_SPACE_MAXADDR_32BIT,/* lowaddr */ 1007 BUS_SPACE_MAXADDR, /* highaddr */ 1008 NULL, NULL, /* filter, filterarg */ 1009 MAXBSIZE, VGE_NSEG_NEW, /* maxsize, nsegments */ 1010 BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */ 1011 BUS_DMA_ALLOCNOW, /* flags */ 1012 NULL, NULL, /* lockfunc, lockarg */ 1013 &sc->vge_parent_tag); 1014 if (error) 1015 goto fail; 1016 1017 error = vge_allocmem(dev, sc); 1018 1019 if (error) 1020 goto fail; 1021 1022 /* Do MII setup */ 1023 if (mii_phy_probe(dev, &sc->vge_miibus, 1024 vge_ifmedia_upd, vge_ifmedia_sts)) { 1025 printf("vge%d: MII without any phy!\n", sc->vge_unit); 1026 error = ENXIO; 1027 goto fail; 1028 } 1029 1030 ifp = &sc->arpcom.ac_if; 1031 ifp->if_softc = sc; 1032 if_initname(ifp, device_get_name(dev), device_get_unit(dev)); 1033 ifp->if_mtu = ETHERMTU; 1034 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 1035 ifp->if_ioctl = vge_ioctl; 1036 ifp->if_capabilities = IFCAP_VLAN_MTU; 1037 ifp->if_start = vge_start; 1038 ifp->if_hwassist = VGE_CSUM_FEATURES; 1039 ifp->if_capabilities |= IFCAP_HWCSUM|IFCAP_VLAN_HWTAGGING; 1040#ifdef DEVICE_POLLING 1041#ifdef IFCAP_POLLING 1042 ifp->if_capabilities |= IFCAP_POLLING; 1043#endif 1044#endif 1045 ifp->if_watchdog = vge_watchdog; 1046 ifp->if_init = vge_init; 1047 ifp->if_baudrate = 1000000000; 1048 ifp->if_snd.ifq_maxlen = VGE_IFQ_MAXLEN; 1049 ifp->if_capenable = ifp->if_capabilities; 1050 1051 TASK_INIT(&sc->vge_txtask, 0, vge_tx_task, ifp); 1052 1053 /* 1054 * Call MI attach routine. 1055 */ 1056 ether_ifattach(ifp, eaddr); 1057 1058 /* Hook interrupt last to avoid having to lock softc */ 1059 error = bus_setup_intr(dev, sc->vge_irq, INTR_TYPE_NET|INTR_MPSAFE, 1060 vge_intr, sc, &sc->vge_intrhand); 1061 1062 if (error) { 1063 printf("vge%d: couldn't set up irq\n", unit); 1064 ether_ifdetach(ifp); 1065 goto fail; 1066 } 1067 1068fail: 1069 if (error) 1070 vge_detach(dev); 1071 1072 return (error); 1073} 1074 1075/* 1076 * Shutdown hardware and free up resources. This can be called any 1077 * time after the mutex has been initialized. It is called in both 1078 * the error case in attach and the normal detach case so it needs 1079 * to be careful about only freeing resources that have actually been 1080 * allocated. 1081 */ 1082static int 1083vge_detach(dev) 1084 device_t dev; 1085{ 1086 struct vge_softc *sc; 1087 struct ifnet *ifp; 1088 int i; 1089 1090 sc = device_get_softc(dev); 1091 KASSERT(mtx_initialized(&sc->vge_mtx), ("vge mutex not initialized")); 1092 ifp = &sc->arpcom.ac_if; 1093 1094 /* These should only be active if attach succeeded */ 1095 if (device_is_attached(dev)) { 1096 vge_stop(sc); 1097 /* 1098 * Force off the IFF_UP flag here, in case someone 1099 * still had a BPF descriptor attached to this 1100 * interface. If they do, ether_ifattach() will cause 1101 * the BPF code to try and clear the promisc mode 1102 * flag, which will bubble down to vge_ioctl(), 1103 * which will try to call vge_init() again. This will 1104 * turn the NIC back on and restart the MII ticker, 1105 * which will panic the system when the kernel tries 1106 * to invoke the vge_tick() function that isn't there 1107 * anymore. 1108 */ 1109 ifp->if_flags &= ~IFF_UP; 1110 ether_ifdetach(ifp); 1111 } 1112 if (sc->vge_miibus) 1113 device_delete_child(dev, sc->vge_miibus); 1114 bus_generic_detach(dev); 1115 1116 if (sc->vge_intrhand) 1117 bus_teardown_intr(dev, sc->vge_irq, sc->vge_intrhand); 1118 if (sc->vge_irq) 1119 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->vge_irq); 1120 if (sc->vge_res) 1121 bus_release_resource(dev, SYS_RES_MEMORY, 1122 VGE_PCI_LOMEM, sc->vge_res); 1123 1124 /* Unload and free the RX DMA ring memory and map */ 1125 1126 if (sc->vge_ldata.vge_rx_list_tag) { 1127 bus_dmamap_unload(sc->vge_ldata.vge_rx_list_tag, 1128 sc->vge_ldata.vge_rx_list_map); 1129 bus_dmamem_free(sc->vge_ldata.vge_rx_list_tag, 1130 sc->vge_ldata.vge_rx_list, 1131 sc->vge_ldata.vge_rx_list_map); 1132 bus_dma_tag_destroy(sc->vge_ldata.vge_rx_list_tag); 1133 } 1134 1135 /* Unload and free the TX DMA ring memory and map */ 1136 1137 if (sc->vge_ldata.vge_tx_list_tag) { 1138 bus_dmamap_unload(sc->vge_ldata.vge_tx_list_tag, 1139 sc->vge_ldata.vge_tx_list_map); 1140 bus_dmamem_free(sc->vge_ldata.vge_tx_list_tag, 1141 sc->vge_ldata.vge_tx_list, 1142 sc->vge_ldata.vge_tx_list_map); 1143 bus_dma_tag_destroy(sc->vge_ldata.vge_tx_list_tag); 1144 } 1145 1146 /* Destroy all the RX and TX buffer maps */ 1147 1148 if (sc->vge_ldata.vge_mtag) { 1149 for (i = 0; i < VGE_TX_DESC_CNT; i++) 1150 bus_dmamap_destroy(sc->vge_ldata.vge_mtag, 1151 sc->vge_ldata.vge_tx_dmamap[i]); 1152 for (i = 0; i < VGE_RX_DESC_CNT; i++) 1153 bus_dmamap_destroy(sc->vge_ldata.vge_mtag, 1154 sc->vge_ldata.vge_rx_dmamap[i]); 1155 bus_dma_tag_destroy(sc->vge_ldata.vge_mtag); 1156 } 1157 1158 if (sc->vge_parent_tag) 1159 bus_dma_tag_destroy(sc->vge_parent_tag); 1160 1161 mtx_destroy(&sc->vge_mtx); 1162 1163 return (0); 1164} 1165 1166static int 1167vge_newbuf(sc, idx, m) 1168 struct vge_softc *sc; 1169 int idx; 1170 struct mbuf *m; 1171{ 1172 struct vge_dmaload_arg arg; 1173 struct mbuf *n = NULL; 1174 int i, error; 1175 1176 if (m == NULL) { 1177 n = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 1178 if (n == NULL) 1179 return (ENOBUFS); 1180 m = n; 1181 } else 1182 m->m_data = m->m_ext.ext_buf; 1183 1184 1185#ifdef VGE_FIXUP_RX 1186 /* 1187 * This is part of an evil trick to deal with non-x86 platforms. 1188 * The VIA chip requires RX buffers to be aligned on 32-bit 1189 * boundaries, but that will hose non-x86 machines. To get around 1190 * this, we leave some empty space at the start of each buffer 1191 * and for non-x86 hosts, we copy the buffer back two bytes 1192 * to achieve word alignment. This is slightly more efficient 1193 * than allocating a new buffer, copying the contents, and 1194 * discarding the old buffer. 1195 */ 1196 m->m_len = m->m_pkthdr.len = MCLBYTES - VGE_ETHER_ALIGN; 1197 m_adj(m, VGE_ETHER_ALIGN); 1198#else 1199 m->m_len = m->m_pkthdr.len = MCLBYTES; 1200#endif 1201 1202 arg.sc = sc; 1203 arg.vge_idx = idx; 1204 arg.vge_maxsegs = 1; 1205 arg.vge_flags = 0; 1206 1207 error = bus_dmamap_load_mbuf(sc->vge_ldata.vge_mtag, 1208 sc->vge_ldata.vge_rx_dmamap[idx], m, vge_dma_map_rx_desc, 1209 &arg, BUS_DMA_NOWAIT); 1210 if (error || arg.vge_maxsegs != 1) { 1211 if (n != NULL) 1212 m_freem(n); 1213 return (ENOMEM); 1214 } 1215 1216 /* 1217 * Note: the manual fails to document the fact that for 1218 * proper opration, the driver needs to replentish the RX 1219 * DMA ring 4 descriptors at a time (rather than one at a 1220 * time, like most chips). We can allocate the new buffers 1221 * but we should not set the OWN bits until we're ready 1222 * to hand back 4 of them in one shot. 1223 */ 1224 1225#define VGE_RXCHUNK 4 1226 sc->vge_rx_consumed++; 1227 if (sc->vge_rx_consumed == VGE_RXCHUNK) { 1228 for (i = idx; i != idx - sc->vge_rx_consumed; i--) 1229 sc->vge_ldata.vge_rx_list[i].vge_sts |= 1230 htole32(VGE_RDSTS_OWN); 1231 sc->vge_rx_consumed = 0; 1232 } 1233 1234 sc->vge_ldata.vge_rx_mbuf[idx] = m; 1235 1236 bus_dmamap_sync(sc->vge_ldata.vge_mtag, 1237 sc->vge_ldata.vge_rx_dmamap[idx], 1238 BUS_DMASYNC_PREREAD); 1239 1240 return (0); 1241} 1242 1243static int 1244vge_tx_list_init(sc) 1245 struct vge_softc *sc; 1246{ 1247 bzero ((char *)sc->vge_ldata.vge_tx_list, VGE_TX_LIST_SZ); 1248 bzero ((char *)&sc->vge_ldata.vge_tx_mbuf, 1249 (VGE_TX_DESC_CNT * sizeof(struct mbuf *))); 1250 1251 bus_dmamap_sync(sc->vge_ldata.vge_tx_list_tag, 1252 sc->vge_ldata.vge_tx_list_map, BUS_DMASYNC_PREWRITE); 1253 sc->vge_ldata.vge_tx_prodidx = 0; 1254 sc->vge_ldata.vge_tx_considx = 0; 1255 sc->vge_ldata.vge_tx_free = VGE_TX_DESC_CNT; 1256 1257 return (0); 1258} 1259 1260static int 1261vge_rx_list_init(sc) 1262 struct vge_softc *sc; 1263{ 1264 int i; 1265 1266 bzero ((char *)sc->vge_ldata.vge_rx_list, VGE_RX_LIST_SZ); 1267 bzero ((char *)&sc->vge_ldata.vge_rx_mbuf, 1268 (VGE_RX_DESC_CNT * sizeof(struct mbuf *))); 1269 1270 sc->vge_rx_consumed = 0; 1271 1272 for (i = 0; i < VGE_RX_DESC_CNT; i++) { 1273 if (vge_newbuf(sc, i, NULL) == ENOBUFS) 1274 return (ENOBUFS); 1275 } 1276 1277 /* Flush the RX descriptors */ 1278 1279 bus_dmamap_sync(sc->vge_ldata.vge_rx_list_tag, 1280 sc->vge_ldata.vge_rx_list_map, 1281 BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD); 1282 1283 sc->vge_ldata.vge_rx_prodidx = 0; 1284 sc->vge_rx_consumed = 0; 1285 sc->vge_head = sc->vge_tail = NULL; 1286 1287 return (0); 1288} 1289 1290#ifdef VGE_FIXUP_RX 1291static __inline void 1292vge_fixup_rx(m) 1293 struct mbuf *m; 1294{ 1295 int i; 1296 uint16_t *src, *dst; 1297 1298 src = mtod(m, uint16_t *); 1299 dst = src - 1; 1300 1301 for (i = 0; i < (m->m_len / sizeof(uint16_t) + 1); i++) 1302 *dst++ = *src++; 1303 1304 m->m_data -= ETHER_ALIGN; 1305 1306 return; 1307} 1308#endif 1309 1310/* 1311 * RX handler. We support the reception of jumbo frames that have 1312 * been fragmented across multiple 2K mbuf cluster buffers. 1313 */ 1314static void 1315vge_rxeof(sc) 1316 struct vge_softc *sc; 1317{ 1318 struct mbuf *m; 1319 struct ifnet *ifp; 1320 int i, total_len; 1321 int lim = 0; 1322 struct vge_rx_desc *cur_rx; 1323 u_int32_t rxstat, rxctl; 1324 1325 VGE_LOCK_ASSERT(sc); 1326 ifp = &sc->arpcom.ac_if; 1327 i = sc->vge_ldata.vge_rx_prodidx; 1328 1329 /* Invalidate the descriptor memory */ 1330 1331 bus_dmamap_sync(sc->vge_ldata.vge_rx_list_tag, 1332 sc->vge_ldata.vge_rx_list_map, 1333 BUS_DMASYNC_POSTREAD); 1334 1335 while (!VGE_OWN(&sc->vge_ldata.vge_rx_list[i])) { 1336 1337#ifdef DEVICE_POLLING 1338 if (ifp->if_flags & IFF_POLLING) { 1339 if (sc->rxcycles <= 0) 1340 break; 1341 sc->rxcycles--; 1342 } 1343#endif /* DEVICE_POLLING */ 1344 1345 cur_rx = &sc->vge_ldata.vge_rx_list[i]; 1346 m = sc->vge_ldata.vge_rx_mbuf[i]; 1347 total_len = VGE_RXBYTES(cur_rx); 1348 rxstat = le32toh(cur_rx->vge_sts); 1349 rxctl = le32toh(cur_rx->vge_ctl); 1350 1351 /* Invalidate the RX mbuf and unload its map */ 1352 1353 bus_dmamap_sync(sc->vge_ldata.vge_mtag, 1354 sc->vge_ldata.vge_rx_dmamap[i], 1355 BUS_DMASYNC_POSTWRITE); 1356 bus_dmamap_unload(sc->vge_ldata.vge_mtag, 1357 sc->vge_ldata.vge_rx_dmamap[i]); 1358 1359 /* 1360 * If the 'start of frame' bit is set, this indicates 1361 * either the first fragment in a multi-fragment receive, 1362 * or an intermediate fragment. Either way, we want to 1363 * accumulate the buffers. 1364 */ 1365 if (rxstat & VGE_RXPKT_SOF) { 1366 m->m_len = MCLBYTES - VGE_ETHER_ALIGN; 1367 if (sc->vge_head == NULL) 1368 sc->vge_head = sc->vge_tail = m; 1369 else { 1370 m->m_flags &= ~M_PKTHDR; 1371 sc->vge_tail->m_next = m; 1372 sc->vge_tail = m; 1373 } 1374 vge_newbuf(sc, i, NULL); 1375 VGE_RX_DESC_INC(i); 1376 continue; 1377 } 1378 1379 /* 1380 * Bad/error frames will have the RXOK bit cleared. 1381 * However, there's one error case we want to allow: 1382 * if a VLAN tagged frame arrives and the chip can't 1383 * match it against the CAM filter, it considers this 1384 * a 'VLAN CAM filter miss' and clears the 'RXOK' bit. 1385 * We don't want to drop the frame though: our VLAN 1386 * filtering is done in software. 1387 */ 1388 if (!(rxstat & VGE_RDSTS_RXOK) && !(rxstat & VGE_RDSTS_VIDM) 1389 && !(rxstat & VGE_RDSTS_CSUMERR)) { 1390 ifp->if_ierrors++; 1391 /* 1392 * If this is part of a multi-fragment packet, 1393 * discard all the pieces. 1394 */ 1395 if (sc->vge_head != NULL) { 1396 m_freem(sc->vge_head); 1397 sc->vge_head = sc->vge_tail = NULL; 1398 } 1399 vge_newbuf(sc, i, m); 1400 VGE_RX_DESC_INC(i); 1401 continue; 1402 } 1403 1404 /* 1405 * If allocating a replacement mbuf fails, 1406 * reload the current one. 1407 */ 1408 1409 if (vge_newbuf(sc, i, NULL)) { 1410 ifp->if_ierrors++; 1411 if (sc->vge_head != NULL) { 1412 m_freem(sc->vge_head); 1413 sc->vge_head = sc->vge_tail = NULL; 1414 } 1415 vge_newbuf(sc, i, m); 1416 VGE_RX_DESC_INC(i); 1417 continue; 1418 } 1419 1420 VGE_RX_DESC_INC(i); 1421 1422 if (sc->vge_head != NULL) { 1423 m->m_len = total_len % (MCLBYTES - VGE_ETHER_ALIGN); 1424 /* 1425 * Special case: if there's 4 bytes or less 1426 * in this buffer, the mbuf can be discarded: 1427 * the last 4 bytes is the CRC, which we don't 1428 * care about anyway. 1429 */ 1430 if (m->m_len <= ETHER_CRC_LEN) { 1431 sc->vge_tail->m_len -= 1432 (ETHER_CRC_LEN - m->m_len); 1433 m_freem(m); 1434 } else { 1435 m->m_len -= ETHER_CRC_LEN; 1436 m->m_flags &= ~M_PKTHDR; 1437 sc->vge_tail->m_next = m; 1438 } 1439 m = sc->vge_head; 1440 sc->vge_head = sc->vge_tail = NULL; 1441 m->m_pkthdr.len = total_len - ETHER_CRC_LEN; 1442 } else 1443 m->m_pkthdr.len = m->m_len = 1444 (total_len - ETHER_CRC_LEN); 1445 1446#ifdef VGE_FIXUP_RX 1447 vge_fixup_rx(m); 1448#endif 1449 ifp->if_ipackets++; 1450 m->m_pkthdr.rcvif = ifp; 1451 1452 /* Do RX checksumming if enabled */ 1453 if (ifp->if_capenable & IFCAP_RXCSUM) { 1454 1455 /* Check IP header checksum */ 1456 if (rxctl & VGE_RDCTL_IPPKT) 1457 m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED; 1458 if (rxctl & VGE_RDCTL_IPCSUMOK) 1459 m->m_pkthdr.csum_flags |= CSUM_IP_VALID; 1460 1461 /* Check TCP/UDP checksum */ 1462 if (rxctl & (VGE_RDCTL_TCPPKT|VGE_RDCTL_UDPPKT) && 1463 rxctl & VGE_RDCTL_PROTOCSUMOK) { 1464 m->m_pkthdr.csum_flags |= 1465 CSUM_DATA_VALID|CSUM_PSEUDO_HDR; 1466 m->m_pkthdr.csum_data = 0xffff; 1467 } 1468 } 1469 1470 if (rxstat & VGE_RDSTS_VTAG) 1471 VLAN_INPUT_TAG(ifp, m, 1472 ntohs((rxctl & VGE_RDCTL_VLANID)), continue); 1473 1474 VGE_UNLOCK(sc); 1475 (*ifp->if_input)(ifp, m); 1476 VGE_LOCK(sc); 1477 1478 lim++; 1479 if (lim == VGE_RX_DESC_CNT) 1480 break; 1481 1482 } 1483 1484 /* Flush the RX DMA ring */ 1485 1486 bus_dmamap_sync(sc->vge_ldata.vge_rx_list_tag, 1487 sc->vge_ldata.vge_rx_list_map, 1488 BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD); 1489 1490 sc->vge_ldata.vge_rx_prodidx = i; 1491 CSR_WRITE_2(sc, VGE_RXDESC_RESIDUECNT, lim); 1492 1493 1494 return; 1495} 1496 1497static void 1498vge_txeof(sc) 1499 struct vge_softc *sc; 1500{ 1501 struct ifnet *ifp; 1502 u_int32_t txstat; 1503 int idx; 1504 1505 ifp = &sc->arpcom.ac_if; 1506 idx = sc->vge_ldata.vge_tx_considx; 1507 1508 /* Invalidate the TX descriptor list */ 1509 1510 bus_dmamap_sync(sc->vge_ldata.vge_tx_list_tag, 1511 sc->vge_ldata.vge_tx_list_map, 1512 BUS_DMASYNC_POSTREAD); 1513 1514 while (idx != sc->vge_ldata.vge_tx_prodidx) { 1515 1516 txstat = le32toh(sc->vge_ldata.vge_tx_list[idx].vge_sts); 1517 if (txstat & VGE_TDSTS_OWN) 1518 break; 1519 1520 m_freem(sc->vge_ldata.vge_tx_mbuf[idx]); 1521 sc->vge_ldata.vge_tx_mbuf[idx] = NULL; 1522 bus_dmamap_unload(sc->vge_ldata.vge_mtag, 1523 sc->vge_ldata.vge_tx_dmamap[idx]); 1524 if (txstat & (VGE_TDSTS_EXCESSCOLL|VGE_TDSTS_COLL)) 1525 ifp->if_collisions++; 1526 if (txstat & VGE_TDSTS_TXERR) 1527 ifp->if_oerrors++; 1528 else 1529 ifp->if_opackets++; 1530 1531 sc->vge_ldata.vge_tx_free++; 1532 VGE_TX_DESC_INC(idx); 1533 } 1534 1535 /* No changes made to the TX ring, so no flush needed */ 1536 1537 if (idx != sc->vge_ldata.vge_tx_considx) { 1538 sc->vge_ldata.vge_tx_considx = idx; 1539 ifp->if_flags &= ~IFF_OACTIVE; 1540 ifp->if_timer = 0; 1541 } 1542 1543 /* 1544 * If not all descriptors have been released reaped yet, 1545 * reload the timer so that we will eventually get another 1546 * interrupt that will cause us to re-enter this routine. 1547 * This is done in case the transmitter has gone idle. 1548 */ 1549 if (sc->vge_ldata.vge_tx_free != VGE_TX_DESC_CNT) { 1550 CSR_WRITE_1(sc, VGE_CRS1, VGE_CR1_TIMER0_ENABLE); 1551 } 1552 1553 return; 1554} 1555 1556static void 1557vge_tick(xsc) 1558 void *xsc; 1559{ 1560 struct vge_softc *sc; 1561 struct ifnet *ifp; 1562 struct mii_data *mii; 1563 1564 sc = xsc; 1565 ifp = &sc->arpcom.ac_if; 1566 VGE_LOCK(sc); 1567 mii = device_get_softc(sc->vge_miibus); 1568 1569 mii_tick(mii); 1570 if (sc->vge_link) { 1571 if (!(mii->mii_media_status & IFM_ACTIVE)) { 1572 sc->vge_link = 0; 1573#ifdef LINK_STATE_UP 1574 sc->arpcom.ac_if.if_link_state = LINK_STATE_UP; 1575 rt_ifmsg(&(sc->arpcom.ac_if)); 1576#endif /* LINK_STATE_UP */ 1577 } 1578 } else { 1579 if (mii->mii_media_status & IFM_ACTIVE && 1580 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) { 1581 sc->vge_link = 1; 1582#ifdef LINK_STATE_DOWN 1583 sc->arpcom.ac_if.if_link_state = LINK_STATE_DOWN; 1584 rt_ifmsg(&(sc->arpcom.ac_if)); 1585#endif /* LINK_STATE_DOWN */ 1586#if __FreeBSD_version < 502114 1587 if (ifp->if_snd.ifq_head != NULL) 1588#else 1589 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 1590#endif 1591 taskqueue_enqueue(taskqueue_swi, 1592 &sc->vge_txtask); 1593 } 1594 } 1595 1596 VGE_UNLOCK(sc); 1597 1598 return; 1599} 1600 1601#ifdef DEVICE_POLLING 1602static void 1603vge_poll (struct ifnet *ifp, enum poll_cmd cmd, int count) 1604{ 1605 struct vge_softc *sc = ifp->if_softc; 1606 1607 VGE_LOCK(sc); 1608#ifdef IFCAP_POLLING 1609 if (!(ifp->if_capenable & IFCAP_POLLING)) { 1610 ether_poll_deregister(ifp); 1611 cmd = POLL_DEREGISTER; 1612 } 1613#endif 1614 if (cmd == POLL_DEREGISTER) { /* final call, enable interrupts */ 1615 CSR_WRITE_4(sc, VGE_IMR, VGE_INTRS); 1616 CSR_WRITE_4(sc, VGE_ISR, 0xFFFFFFFF); 1617 CSR_WRITE_1(sc, VGE_CRS3, VGE_CR3_INT_GMSK); 1618 goto done; 1619 } 1620 1621 sc->rxcycles = count; 1622 vge_rxeof(sc); 1623 vge_txeof(sc); 1624 1625#if __FreeBSD_version < 502114 1626 if (ifp->if_snd.ifq_head != NULL) 1627#else 1628 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 1629#endif 1630 taskqueue_enqueue(taskqueue_swi, &sc->vge_txtask); 1631 1632 if (cmd == POLL_AND_CHECK_STATUS) { /* also check status register */ 1633 u_int32_t status; 1634 status = CSR_READ_4(sc, VGE_ISR); 1635 if (status == 0xFFFFFFFF) 1636 goto done; 1637 if (status) 1638 CSR_WRITE_4(sc, VGE_ISR, status); 1639 1640 /* 1641 * XXX check behaviour on receiver stalls. 1642 */ 1643 1644 if (status & VGE_ISR_TXDMA_STALL || 1645 status & VGE_ISR_RXDMA_STALL) 1646 vge_init(sc); 1647 1648 if (status & (VGE_ISR_RXOFLOW|VGE_ISR_RXNODESC)) { 1649 vge_rxeof(sc); 1650 ifp->if_ierrors++; 1651 CSR_WRITE_1(sc, VGE_RXQCSRS, VGE_RXQCSR_RUN); 1652 CSR_WRITE_1(sc, VGE_RXQCSRS, VGE_RXQCSR_WAK); 1653 } 1654 } 1655done: 1656 VGE_UNLOCK(sc); 1657} 1658#endif /* DEVICE_POLLING */ 1659 1660static void 1661vge_intr(arg) 1662 void *arg; 1663{ 1664 struct vge_softc *sc; 1665 struct ifnet *ifp; 1666 u_int32_t status; 1667 1668 sc = arg; 1669 1670 if (sc->suspended) { 1671 return; 1672 } 1673 1674 VGE_LOCK(sc); 1675 ifp = &sc->arpcom.ac_if; 1676 1677 if (!(ifp->if_flags & IFF_UP)) { 1678 VGE_UNLOCK(sc); 1679 return; 1680 } 1681 1682#ifdef DEVICE_POLLING 1683 if (ifp->if_flags & IFF_POLLING) 1684 goto done; 1685 if ( 1686#ifdef IFCAP_POLLING 1687 (ifp->if_capenable & IFCAP_POLLING) && 1688#endif 1689 ether_poll_register(vge_poll, ifp)) { /* ok, disable interrupts */ 1690 CSR_WRITE_4(sc, VGE_IMR, 0); 1691 CSR_WRITE_1(sc, VGE_CRC3, VGE_CR3_INT_GMSK); 1692 vge_poll(ifp, 0, 1); 1693 goto done; 1694 } 1695 1696#endif /* DEVICE_POLLING */ 1697 1698 /* Disable interrupts */ 1699 CSR_WRITE_1(sc, VGE_CRC3, VGE_CR3_INT_GMSK); 1700 1701 for (;;) { 1702 1703 status = CSR_READ_4(sc, VGE_ISR); 1704 /* If the card has gone away the read returns 0xffff. */ 1705 if (status == 0xFFFFFFFF) 1706 break; 1707 1708 if (status) 1709 CSR_WRITE_4(sc, VGE_ISR, status); 1710 1711 if ((status & VGE_INTRS) == 0) 1712 break; 1713 1714 if (status & (VGE_ISR_RXOK|VGE_ISR_RXOK_HIPRIO)) 1715 vge_rxeof(sc); 1716 1717 if (status & (VGE_ISR_RXOFLOW|VGE_ISR_RXNODESC)) { 1718 vge_rxeof(sc); 1719 CSR_WRITE_1(sc, VGE_RXQCSRS, VGE_RXQCSR_RUN); 1720 CSR_WRITE_1(sc, VGE_RXQCSRS, VGE_RXQCSR_WAK); 1721 } 1722 1723 if (status & (VGE_ISR_TXOK0|VGE_ISR_TIMER0)) 1724 vge_txeof(sc); 1725 1726 if (status & (VGE_ISR_TXDMA_STALL|VGE_ISR_RXDMA_STALL)) 1727 vge_init(sc); 1728 1729 if (status & VGE_ISR_LINKSTS) 1730 vge_tick(sc); 1731 } 1732 1733 /* Re-enable interrupts */ 1734 CSR_WRITE_1(sc, VGE_CRS3, VGE_CR3_INT_GMSK); 1735 1736#ifdef DEVICE_POLLING 1737done: 1738#endif 1739 VGE_UNLOCK(sc); 1740 1741#if __FreeBSD_version < 502114 1742 if (ifp->if_snd.ifq_head != NULL) 1743#else 1744 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 1745#endif 1746 taskqueue_enqueue(taskqueue_swi, &sc->vge_txtask); 1747 1748 return; 1749} 1750 1751static int 1752vge_encap(sc, m_head, idx) 1753 struct vge_softc *sc; 1754 struct mbuf *m_head; 1755 int idx; 1756{ 1757 struct mbuf *m_new = NULL; 1758 struct vge_dmaload_arg arg; 1759 bus_dmamap_t map; 1760 int error; 1761 struct m_tag *mtag; 1762 1763 if (sc->vge_ldata.vge_tx_free <= 2) 1764 return (EFBIG); 1765 1766 arg.vge_flags = 0; 1767 1768 if (m_head->m_pkthdr.csum_flags & CSUM_IP) 1769 arg.vge_flags |= VGE_TDCTL_IPCSUM; 1770 if (m_head->m_pkthdr.csum_flags & CSUM_TCP) 1771 arg.vge_flags |= VGE_TDCTL_TCPCSUM; 1772 if (m_head->m_pkthdr.csum_flags & CSUM_UDP) 1773 arg.vge_flags |= VGE_TDCTL_UDPCSUM; 1774 1775 arg.sc = sc; 1776 arg.vge_idx = idx; 1777 arg.vge_m0 = m_head; 1778 arg.vge_maxsegs = VGE_TX_FRAGS; 1779 1780 map = sc->vge_ldata.vge_tx_dmamap[idx]; 1781 error = bus_dmamap_load_mbuf(sc->vge_ldata.vge_mtag, map, 1782 m_head, vge_dma_map_tx_desc, &arg, BUS_DMA_NOWAIT); 1783 1784 if (error && error != EFBIG) { 1785 printf("vge%d: can't map mbuf (error %d)\n", 1786 sc->vge_unit, error); 1787 return (ENOBUFS); 1788 } 1789 1790 /* Too many segments to map, coalesce into a single mbuf */ 1791 1792 if (error || arg.vge_maxsegs == 0) { 1793 m_new = m_defrag(m_head, M_DONTWAIT); 1794 if (m_new == NULL) 1795 return (1); 1796 else 1797 m_head = m_new; 1798 1799 arg.sc = sc; 1800 arg.vge_m0 = m_head; 1801 arg.vge_idx = idx; 1802 arg.vge_maxsegs = 1; 1803 1804 error = bus_dmamap_load_mbuf(sc->vge_ldata.vge_mtag, map, 1805 m_head, vge_dma_map_tx_desc, &arg, BUS_DMA_NOWAIT); 1806 if (error) { 1807 printf("vge%d: can't map mbuf (error %d)\n", 1808 sc->vge_unit, error); 1809 return (EFBIG); 1810 } 1811 } 1812 1813 sc->vge_ldata.vge_tx_mbuf[idx] = m_head; 1814 sc->vge_ldata.vge_tx_free--; 1815 1816 /* 1817 * Set up hardware VLAN tagging. 1818 */ 1819 1820 mtag = VLAN_OUTPUT_TAG(&sc->arpcom.ac_if, m_head); 1821 if (mtag != NULL) 1822 sc->vge_ldata.vge_tx_list[idx].vge_ctl |= 1823 htole32(htons(VLAN_TAG_VALUE(mtag)) | VGE_TDCTL_VTAG); 1824 1825 sc->vge_ldata.vge_tx_list[idx].vge_sts |= htole32(VGE_TDSTS_OWN); 1826 1827 return (0); 1828} 1829 1830static void 1831vge_tx_task(arg, npending) 1832 void *arg; 1833 int npending; 1834{ 1835 struct ifnet *ifp; 1836 1837 ifp = arg; 1838 vge_start(ifp); 1839 1840 return; 1841} 1842 1843/* 1844 * Main transmit routine. 1845 */ 1846 1847static void 1848vge_start(ifp) 1849 struct ifnet *ifp; 1850{ 1851 struct vge_softc *sc; 1852 struct mbuf *m_head = NULL; 1853 int idx, pidx = 0; 1854 1855 sc = ifp->if_softc; 1856 VGE_LOCK(sc); 1857 1858 if (!sc->vge_link || ifp->if_flags & IFF_OACTIVE) { 1859 VGE_UNLOCK(sc); 1860 return; 1861 } 1862 1863#if __FreeBSD_version < 502114 1864 if (ifp->if_snd.ifq_head == NULL) { 1865#else 1866 if (IFQ_DRV_IS_EMPTY(&ifp->if_snd)) { 1867#endif 1868 VGE_UNLOCK(sc); 1869 return; 1870 } 1871 1872 idx = sc->vge_ldata.vge_tx_prodidx; 1873 1874 pidx = idx - 1; 1875 if (pidx < 0) 1876 pidx = VGE_TX_DESC_CNT - 1; 1877 1878 1879 while (sc->vge_ldata.vge_tx_mbuf[idx] == NULL) { 1880#if __FreeBSD_version < 502114 1881 IF_DEQUEUE(&ifp->if_snd, m_head); 1882#else 1883 IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head); 1884#endif 1885 if (m_head == NULL) 1886 break; 1887 1888 if (vge_encap(sc, m_head, idx)) { 1889#if __FreeBSD_version >= 502114 1890 IFQ_DRV_PREPEND(&ifp->if_snd, m_head); 1891#else 1892 IF_PREPEND(&ifp->if_snd, m_head); 1893#endif 1894 ifp->if_flags |= IFF_OACTIVE; 1895 break; 1896 } 1897 1898 sc->vge_ldata.vge_tx_list[pidx].vge_frag[0].vge_buflen |= 1899 htole16(VGE_TXDESC_Q); 1900 1901 pidx = idx; 1902 VGE_TX_DESC_INC(idx); 1903 1904 /* 1905 * If there's a BPF listener, bounce a copy of this frame 1906 * to him. 1907 */ 1908 BPF_MTAP(ifp, m_head); 1909 } 1910 1911 if (idx == sc->vge_ldata.vge_tx_prodidx) { 1912 VGE_UNLOCK(sc); 1913 return; 1914 } 1915 1916 /* Flush the TX descriptors */ 1917 1918 bus_dmamap_sync(sc->vge_ldata.vge_tx_list_tag, 1919 sc->vge_ldata.vge_tx_list_map, 1920 BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD); 1921 1922 /* Issue a transmit command. */ 1923 CSR_WRITE_2(sc, VGE_TXQCSRS, VGE_TXQCSR_WAK0); 1924 1925 sc->vge_ldata.vge_tx_prodidx = idx; 1926 1927 /* 1928 * Use the countdown timer for interrupt moderation. 1929 * 'TX done' interrupts are disabled. Instead, we reset the 1930 * countdown timer, which will begin counting until it hits 1931 * the value in the SSTIMER register, and then trigger an 1932 * interrupt. Each time we set the TIMER0_ENABLE bit, the 1933 * the timer count is reloaded. Only when the transmitter 1934 * is idle will the timer hit 0 and an interrupt fire. 1935 */ 1936 CSR_WRITE_1(sc, VGE_CRS1, VGE_CR1_TIMER0_ENABLE); 1937 1938 VGE_UNLOCK(sc); 1939 1940 /* 1941 * Set a timeout in case the chip goes out to lunch. 1942 */ 1943 ifp->if_timer = 5; 1944 1945 return; 1946} 1947 1948static void 1949vge_init(xsc) 1950 void *xsc; 1951{ 1952 struct vge_softc *sc = xsc; 1953 struct ifnet *ifp = &sc->arpcom.ac_if; 1954 struct mii_data *mii; 1955 int i; 1956 1957 VGE_LOCK(sc); 1958 mii = device_get_softc(sc->vge_miibus); 1959 1960 /* 1961 * Cancel pending I/O and free all RX/TX buffers. 1962 */ 1963 vge_stop(sc); 1964 vge_reset(sc); 1965 1966 /* 1967 * Initialize the RX and TX descriptors and mbufs. 1968 */ 1969 1970 vge_rx_list_init(sc); 1971 vge_tx_list_init(sc); 1972 1973 /* Set our station address */ 1974 for (i = 0; i < ETHER_ADDR_LEN; i++) 1975 CSR_WRITE_1(sc, VGE_PAR0 + i, sc->arpcom.ac_enaddr[i]); 1976 1977 /* 1978 * Set receive FIFO threshold. Also allow transmission and 1979 * reception of VLAN tagged frames. 1980 */ 1981 CSR_CLRBIT_1(sc, VGE_RXCFG, VGE_RXCFG_FIFO_THR|VGE_RXCFG_VTAGOPT); 1982 CSR_SETBIT_1(sc, VGE_RXCFG, VGE_RXFIFOTHR_128BYTES|VGE_VTAG_OPT2); 1983 1984 /* Set DMA burst length */ 1985 CSR_CLRBIT_1(sc, VGE_DMACFG0, VGE_DMACFG0_BURSTLEN); 1986 CSR_SETBIT_1(sc, VGE_DMACFG0, VGE_DMABURST_128); 1987 1988 CSR_SETBIT_1(sc, VGE_TXCFG, VGE_TXCFG_ARB_PRIO|VGE_TXCFG_NONBLK); 1989 1990 /* Set collision backoff algorithm */ 1991 CSR_CLRBIT_1(sc, VGE_CHIPCFG1, VGE_CHIPCFG1_CRANDOM| 1992 VGE_CHIPCFG1_CAP|VGE_CHIPCFG1_MBA|VGE_CHIPCFG1_BAKOPT); 1993 CSR_SETBIT_1(sc, VGE_CHIPCFG1, VGE_CHIPCFG1_OFSET); 1994 1995 /* Disable LPSEL field in priority resolution */ 1996 CSR_SETBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_LPSEL_DIS); 1997 1998 /* 1999 * Load the addresses of the DMA queues into the chip. 2000 * Note that we only use one transmit queue. 2001 */ 2002 2003 CSR_WRITE_4(sc, VGE_TXDESC_ADDR_LO0, 2004 VGE_ADDR_LO(sc->vge_ldata.vge_tx_list_addr)); 2005 CSR_WRITE_2(sc, VGE_TXDESCNUM, VGE_TX_DESC_CNT - 1); 2006 2007 CSR_WRITE_4(sc, VGE_RXDESC_ADDR_LO, 2008 VGE_ADDR_LO(sc->vge_ldata.vge_rx_list_addr)); 2009 CSR_WRITE_2(sc, VGE_RXDESCNUM, VGE_RX_DESC_CNT - 1); 2010 CSR_WRITE_2(sc, VGE_RXDESC_RESIDUECNT, VGE_RX_DESC_CNT); 2011 2012 /* Enable and wake up the RX descriptor queue */ 2013 CSR_WRITE_1(sc, VGE_RXQCSRS, VGE_RXQCSR_RUN); 2014 CSR_WRITE_1(sc, VGE_RXQCSRS, VGE_RXQCSR_WAK); 2015 2016 /* Enable the TX descriptor queue */ 2017 CSR_WRITE_2(sc, VGE_TXQCSRS, VGE_TXQCSR_RUN0); 2018 2019 /* Set up the receive filter -- allow large frames for VLANs. */ 2020 CSR_WRITE_1(sc, VGE_RXCTL, VGE_RXCTL_RX_UCAST|VGE_RXCTL_RX_GIANT); 2021 2022 /* If we want promiscuous mode, set the allframes bit. */ 2023 if (ifp->if_flags & IFF_PROMISC) { 2024 CSR_SETBIT_1(sc, VGE_RXCTL, VGE_RXCTL_RX_PROMISC); 2025 } 2026 2027 /* Set capture broadcast bit to capture broadcast frames. */ 2028 if (ifp->if_flags & IFF_BROADCAST) { 2029 CSR_SETBIT_1(sc, VGE_RXCTL, VGE_RXCTL_RX_BCAST); 2030 } 2031 2032 /* Set multicast bit to capture multicast frames. */ 2033 if (ifp->if_flags & IFF_MULTICAST) { 2034 CSR_SETBIT_1(sc, VGE_RXCTL, VGE_RXCTL_RX_MCAST); 2035 } 2036 2037 /* Init the cam filter. */ 2038 vge_cam_clear(sc); 2039 2040 /* Init the multicast filter. */ 2041 vge_setmulti(sc); 2042 2043 /* Enable flow control */ 2044 2045 CSR_WRITE_1(sc, VGE_CRS2, 0x8B); 2046 2047 /* Enable jumbo frame reception (if desired) */ 2048 2049 /* Start the MAC. */ 2050 CSR_WRITE_1(sc, VGE_CRC0, VGE_CR0_STOP); 2051 CSR_WRITE_1(sc, VGE_CRS1, VGE_CR1_NOPOLL); 2052 CSR_WRITE_1(sc, VGE_CRS0, 2053 VGE_CR0_TX_ENABLE|VGE_CR0_RX_ENABLE|VGE_CR0_START); 2054 2055 /* 2056 * Configure one-shot timer for microsecond 2057 * resulution and load it for 500 usecs. 2058 */ 2059 CSR_SETBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_TIMER0_RES); 2060 CSR_WRITE_2(sc, VGE_SSTIMER, 400); 2061 2062 /* 2063 * Configure interrupt moderation for receive. Enable 2064 * the holdoff counter and load it, and set the RX 2065 * suppression count to the number of descriptors we 2066 * want to allow before triggering an interrupt. 2067 * The holdoff timer is in units of 20 usecs. 2068 */ 2069 2070#ifdef notyet 2071 CSR_WRITE_1(sc, VGE_INTCTL1, VGE_INTCTL_TXINTSUP_DISABLE); 2072 /* Select the interrupt holdoff timer page. */ 2073 CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL); 2074 CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_INTHLDOFF); 2075 CSR_WRITE_1(sc, VGE_INTHOLDOFF, 10); /* ~200 usecs */ 2076 2077 /* Enable use of the holdoff timer. */ 2078 CSR_WRITE_1(sc, VGE_CRS3, VGE_CR3_INT_HOLDOFF); 2079 CSR_WRITE_1(sc, VGE_INTCTL1, VGE_INTCTL_SC_RELOAD); 2080 2081 /* Select the RX suppression threshold page. */ 2082 CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL); 2083 CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_RXSUPPTHR); 2084 CSR_WRITE_1(sc, VGE_RXSUPPTHR, 64); /* interrupt after 64 packets */ 2085 2086 /* Restore the page select bits. */ 2087 CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL); 2088 CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_MAR); 2089#endif 2090 2091#ifdef DEVICE_POLLING 2092 /* 2093 * Disable interrupts if we are polling. 2094 */ 2095 if (ifp->if_flags & IFF_POLLING) { 2096 CSR_WRITE_4(sc, VGE_IMR, 0); 2097 CSR_WRITE_1(sc, VGE_CRC3, VGE_CR3_INT_GMSK); 2098 } else /* otherwise ... */ 2099#endif /* DEVICE_POLLING */ 2100 { 2101 /* 2102 * Enable interrupts. 2103 */ 2104 CSR_WRITE_4(sc, VGE_IMR, VGE_INTRS); 2105 CSR_WRITE_4(sc, VGE_ISR, 0); 2106 CSR_WRITE_1(sc, VGE_CRS3, VGE_CR3_INT_GMSK); 2107 } 2108 2109 mii_mediachg(mii); 2110 2111 ifp->if_flags |= IFF_RUNNING; 2112 ifp->if_flags &= ~IFF_OACTIVE; 2113 2114 sc->vge_if_flags = 0; 2115 sc->vge_link = 0; 2116 2117 VGE_UNLOCK(sc); 2118 2119 return; 2120} 2121 2122/* 2123 * Set media options. 2124 */ 2125static int 2126vge_ifmedia_upd(ifp) 2127 struct ifnet *ifp; 2128{ 2129 struct vge_softc *sc; 2130 struct mii_data *mii; 2131 2132 sc = ifp->if_softc; 2133 mii = device_get_softc(sc->vge_miibus); 2134 mii_mediachg(mii); 2135 2136 return (0); 2137} 2138 2139/* 2140 * Report current media status. 2141 */ 2142static void 2143vge_ifmedia_sts(ifp, ifmr) 2144 struct ifnet *ifp; 2145 struct ifmediareq *ifmr; 2146{ 2147 struct vge_softc *sc; 2148 struct mii_data *mii; 2149 2150 sc = ifp->if_softc; 2151 mii = device_get_softc(sc->vge_miibus); 2152 2153 mii_pollstat(mii); 2154 ifmr->ifm_active = mii->mii_media_active; 2155 ifmr->ifm_status = mii->mii_media_status; 2156 2157 return; 2158} 2159 2160static void 2161vge_miibus_statchg(dev) 2162 device_t dev; 2163{ 2164 struct vge_softc *sc; 2165 struct mii_data *mii; 2166 struct ifmedia_entry *ife; 2167 2168 sc = device_get_softc(dev); 2169 mii = device_get_softc(sc->vge_miibus); 2170 ife = mii->mii_media.ifm_cur; 2171 2172 /* 2173 * If the user manually selects a media mode, we need to turn 2174 * on the forced MAC mode bit in the DIAGCTL register. If the 2175 * user happens to choose a full duplex mode, we also need to 2176 * set the 'force full duplex' bit. This applies only to 2177 * 10Mbps and 100Mbps speeds. In autoselect mode, forced MAC 2178 * mode is disabled, and in 1000baseT mode, full duplex is 2179 * always implied, so we turn on the forced mode bit but leave 2180 * the FDX bit cleared. 2181 */ 2182 2183 switch (IFM_SUBTYPE(ife->ifm_media)) { 2184 case IFM_AUTO: 2185 CSR_CLRBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_MACFORCE); 2186 CSR_CLRBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_FDXFORCE); 2187 break; 2188 case IFM_1000_T: 2189 CSR_SETBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_MACFORCE); 2190 CSR_CLRBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_FDXFORCE); 2191 break; 2192 case IFM_100_TX: 2193 case IFM_10_T: 2194 CSR_SETBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_MACFORCE); 2195 if ((ife->ifm_media & IFM_GMASK) == IFM_FDX) { 2196 CSR_SETBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_FDXFORCE); 2197 } else { 2198 CSR_CLRBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_FDXFORCE); 2199 } 2200 break; 2201 default: 2202 device_printf(dev, "unknown media type: %x\n", 2203 IFM_SUBTYPE(ife->ifm_media)); 2204 break; 2205 } 2206 2207 return; 2208} 2209 2210static int 2211vge_ioctl(ifp, command, data) 2212 struct ifnet *ifp; 2213 u_long command; 2214 caddr_t data; 2215{ 2216 struct vge_softc *sc = ifp->if_softc; 2217 struct ifreq *ifr = (struct ifreq *) data; 2218 struct mii_data *mii; 2219 int error = 0; 2220 2221 switch (command) { 2222 case SIOCSIFMTU: 2223 if (ifr->ifr_mtu > VGE_JUMBO_MTU) 2224 error = EINVAL; 2225 ifp->if_mtu = ifr->ifr_mtu; 2226 break; 2227 case SIOCSIFFLAGS: 2228 if (ifp->if_flags & IFF_UP) { 2229 if (ifp->if_flags & IFF_RUNNING && 2230 ifp->if_flags & IFF_PROMISC && 2231 !(sc->vge_if_flags & IFF_PROMISC)) { 2232 CSR_SETBIT_1(sc, VGE_RXCTL, 2233 VGE_RXCTL_RX_PROMISC); 2234 vge_setmulti(sc); 2235 } else if (ifp->if_flags & IFF_RUNNING && 2236 !(ifp->if_flags & IFF_PROMISC) && 2237 sc->vge_if_flags & IFF_PROMISC) { 2238 CSR_CLRBIT_1(sc, VGE_RXCTL, 2239 VGE_RXCTL_RX_PROMISC); 2240 vge_setmulti(sc); 2241 } else 2242 vge_init(sc); 2243 } else { 2244 if (ifp->if_flags & IFF_RUNNING) 2245 vge_stop(sc); 2246 } 2247 sc->vge_if_flags = ifp->if_flags; 2248 break; 2249 case SIOCADDMULTI: 2250 case SIOCDELMULTI: 2251 vge_setmulti(sc); 2252 break; 2253 case SIOCGIFMEDIA: 2254 case SIOCSIFMEDIA: 2255 mii = device_get_softc(sc->vge_miibus); 2256 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command); 2257 break; 2258 case SIOCSIFCAP: 2259#ifdef IFCAP_POLLING 2260 ifp->if_capenable &= ~(IFCAP_HWCSUM | IFCAP_POLLING); 2261#else 2262 ifp->if_capenable &= ~(IFCAP_HWCSUM); 2263#endif 2264 ifp->if_capenable |= 2265#ifdef IFCAP_POLLING 2266 ifr->ifr_reqcap & (IFCAP_HWCSUM | IFCAP_POLLING); 2267#else 2268 ifr->ifr_reqcap & (IFCAP_HWCSUM); 2269#endif 2270 if (ifp->if_capenable & IFCAP_TXCSUM) 2271 ifp->if_hwassist = VGE_CSUM_FEATURES; 2272 else 2273 ifp->if_hwassist = 0; 2274 if (ifp->if_flags & IFF_RUNNING) 2275 vge_init(sc); 2276 break; 2277 default: 2278 error = ether_ioctl(ifp, command, data); 2279 break; 2280 } 2281 2282 return (error); 2283} 2284 2285static void 2286vge_watchdog(ifp) 2287 struct ifnet *ifp; 2288{ 2289 struct vge_softc *sc; 2290 2291 sc = ifp->if_softc; 2292 VGE_LOCK(sc); 2293 printf("vge%d: watchdog timeout\n", sc->vge_unit); 2294 ifp->if_oerrors++; 2295 2296 vge_txeof(sc); 2297 vge_rxeof(sc); 2298 2299 vge_init(sc); 2300 2301 VGE_UNLOCK(sc); 2302 2303 return; 2304} 2305 2306/* 2307 * Stop the adapter and free any mbufs allocated to the 2308 * RX and TX lists. 2309 */ 2310static void 2311vge_stop(sc) 2312 struct vge_softc *sc; 2313{ 2314 register int i; 2315 struct ifnet *ifp; 2316 2317 VGE_LOCK(sc); 2318 ifp = &sc->arpcom.ac_if; 2319 ifp->if_timer = 0; 2320 2321 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 2322#ifdef DEVICE_POLLING 2323 ether_poll_deregister(ifp); 2324#endif /* DEVICE_POLLING */ 2325 2326 CSR_WRITE_1(sc, VGE_CRC3, VGE_CR3_INT_GMSK); 2327 CSR_WRITE_1(sc, VGE_CRS0, VGE_CR0_STOP); 2328 CSR_WRITE_4(sc, VGE_ISR, 0xFFFFFFFF); 2329 CSR_WRITE_2(sc, VGE_TXQCSRC, 0xFFFF); 2330 CSR_WRITE_1(sc, VGE_RXQCSRC, 0xFF); 2331 CSR_WRITE_4(sc, VGE_RXDESC_ADDR_LO, 0); 2332 2333 if (sc->vge_head != NULL) { 2334 m_freem(sc->vge_head); 2335 sc->vge_head = sc->vge_tail = NULL; 2336 } 2337 2338 /* Free the TX list buffers. */ 2339 2340 for (i = 0; i < VGE_TX_DESC_CNT; i++) { 2341 if (sc->vge_ldata.vge_tx_mbuf[i] != NULL) { 2342 bus_dmamap_unload(sc->vge_ldata.vge_mtag, 2343 sc->vge_ldata.vge_tx_dmamap[i]); 2344 m_freem(sc->vge_ldata.vge_tx_mbuf[i]); 2345 sc->vge_ldata.vge_tx_mbuf[i] = NULL; 2346 } 2347 } 2348 2349 /* Free the RX list buffers. */ 2350 2351 for (i = 0; i < VGE_RX_DESC_CNT; i++) { 2352 if (sc->vge_ldata.vge_rx_mbuf[i] != NULL) { 2353 bus_dmamap_unload(sc->vge_ldata.vge_mtag, 2354 sc->vge_ldata.vge_rx_dmamap[i]); 2355 m_freem(sc->vge_ldata.vge_rx_mbuf[i]); 2356 sc->vge_ldata.vge_rx_mbuf[i] = NULL; 2357 } 2358 } 2359 2360 VGE_UNLOCK(sc); 2361 2362 return; 2363} 2364 2365/* 2366 * Device suspend routine. Stop the interface and save some PCI 2367 * settings in case the BIOS doesn't restore them properly on 2368 * resume. 2369 */ 2370static int 2371vge_suspend(dev) 2372 device_t dev; 2373{ 2374 struct vge_softc *sc; 2375 int i; 2376 2377 sc = device_get_softc(dev); 2378 2379 vge_stop(sc); 2380 2381 for (i = 0; i < 5; i++) 2382 sc->saved_maps[i] = pci_read_config(dev, PCIR_MAPS + i * 4, 4); 2383 sc->saved_biosaddr = pci_read_config(dev, PCIR_BIOS, 4); 2384 sc->saved_intline = pci_read_config(dev, PCIR_INTLINE, 1); 2385 sc->saved_cachelnsz = pci_read_config(dev, PCIR_CACHELNSZ, 1); 2386 sc->saved_lattimer = pci_read_config(dev, PCIR_LATTIMER, 1); 2387 2388 sc->suspended = 1; 2389 2390 return (0); 2391} 2392 2393/* 2394 * Device resume routine. Restore some PCI settings in case the BIOS 2395 * doesn't, re-enable busmastering, and restart the interface if 2396 * appropriate. 2397 */ 2398static int 2399vge_resume(dev) 2400 device_t dev; 2401{ 2402 struct vge_softc *sc; 2403 struct ifnet *ifp; 2404 int i; 2405 2406 sc = device_get_softc(dev); 2407 ifp = &sc->arpcom.ac_if; 2408 2409 /* better way to do this? */ 2410 for (i = 0; i < 5; i++) 2411 pci_write_config(dev, PCIR_MAPS + i * 4, sc->saved_maps[i], 4); 2412 pci_write_config(dev, PCIR_BIOS, sc->saved_biosaddr, 4); 2413 pci_write_config(dev, PCIR_INTLINE, sc->saved_intline, 1); 2414 pci_write_config(dev, PCIR_CACHELNSZ, sc->saved_cachelnsz, 1); 2415 pci_write_config(dev, PCIR_LATTIMER, sc->saved_lattimer, 1); 2416 2417 /* reenable busmastering */ 2418 pci_enable_busmaster(dev); 2419 pci_enable_io(dev, SYS_RES_MEMORY); 2420 2421 /* reinitialize interface if necessary */ 2422 if (ifp->if_flags & IFF_UP) 2423 vge_init(sc); 2424 2425 sc->suspended = 0; 2426 2427 return (0); 2428} 2429 2430/* 2431 * Stop all chip I/O so that the kernel's probe routines don't 2432 * get confused by errant DMAs when rebooting. 2433 */ 2434static void 2435vge_shutdown(dev) 2436 device_t dev; 2437{ 2438 struct vge_softc *sc; 2439 2440 sc = device_get_softc(dev); 2441 2442 vge_stop(sc); 2443} 2444