1 /* $OpenBSD: if_sk.c,v 2.33 2003/08/12 05:23:06 nate Exp $ */ 2 3 /*- 4 * Copyright (c) 1997, 1998, 1999, 2000 5 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by Bill Paul. 18 * 4. Neither the name of the author nor the names of any co-contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD 26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 32 * THE POSSIBILITY OF SUCH DAMAGE. 33 */ 34 /*- 35 * Copyright (c) 2003 Nathan L. Binkert <binkertn@umich.edu> 36 * 37 * Permission to use, copy, modify, and distribute this software for any 38 * purpose with or without fee is hereby granted, provided that the above 39 * copyright notice and this permission notice appear in all copies. 40 * 41 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 42 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 43 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 44 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 45 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 46 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 47 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 48 */ 49 50 #include <sys/cdefs.h> 51 __FBSDID("$FreeBSD$"); 52 53 /* 54 * SysKonnect SK-NET gigabit ethernet driver for FreeBSD. Supports 55 * the SK-984x series adapters, both single port and dual port. 56 * References: 57 * The XaQti XMAC II datasheet, 58 * http://www.freebsd.org/~wpaul/SysKonnect/xmacii_datasheet_rev_c_9-29.pdf 59 * The SysKonnect GEnesis manual, http://www.syskonnect.com 60 * 61 * Note: XaQti has been aquired by Vitesse, and Vitesse does not have the 62 * XMAC II datasheet online. I have put my copy at people.freebsd.org as a 63 * convenience to others until Vitesse corrects this problem: 64 * 65 * http://people.freebsd.org/~wpaul/SysKonnect/xmacii_datasheet_rev_c_9-29.pdf 66 * 67 * Written by Bill Paul <wpaul@ee.columbia.edu> 68 * Department of Electrical Engineering 69 * Columbia University, New York City 70 */ 71 /* 72 * The SysKonnect gigabit ethernet adapters consist of two main 73 * components: the SysKonnect GEnesis controller chip and the XaQti Corp. 74 * XMAC II gigabit ethernet MAC. The XMAC provides all of the MAC 75 * components and a PHY while the GEnesis controller provides a PCI 76 * interface with DMA support. Each card may have between 512K and 77 * 2MB of SRAM on board depending on the configuration. 78 * 79 * The SysKonnect GEnesis controller can have either one or two XMAC 80 * chips connected to it, allowing single or dual port NIC configurations. 81 * SysKonnect has the distinction of being the only vendor on the market 82 * with a dual port gigabit ethernet NIC. The GEnesis provides dual FIFOs, 83 * dual DMA queues, packet/MAC/transmit arbiters and direct access to the 84 * XMAC registers. This driver takes advantage of these features to allow 85 * both XMACs to operate as independent interfaces. 86 */ 87 88 #include <sys/param.h> 89 #include <sys/systm.h> 90 #include <sys/bus.h> 91 #include <sys/endian.h> 92 #include <sys/mbuf.h> 93 #include <sys/malloc.h> 94 #include <sys/kernel.h> 95 #include <sys/module.h> 96 #include <sys/socket.h> 97 #include <sys/sockio.h> 98 #include <sys/queue.h> 99 #include <sys/sysctl.h> 100 101 #include <net/bpf.h> 102 #include <net/ethernet.h> 103 #include <net/if.h> 104 #include <net/if_arp.h> 105 #include <net/if_dl.h> 106 #include <net/if_media.h> 107 #include <net/if_types.h> 108 #include <net/if_vlan_var.h> 109 110 #include <netinet/in.h> 111 #include <netinet/in_systm.h> 112 #include <netinet/ip.h> 113 114 #include <machine/bus.h> 115 #include <machine/in_cksum.h> 116 #include <machine/resource.h> 117 #include <sys/rman.h> 118 119 #include <dev/mii/mii.h> 120 #include <dev/mii/miivar.h> 121 #include <dev/mii/brgphyreg.h> 122 123 #include <dev/pci/pcireg.h> 124 #include <dev/pci/pcivar.h> 125 126 #if 0 127 #define SK_USEIOSPACE 128 #endif 129 130 #include <dev/sk/if_skreg.h> 131 #include <dev/sk/xmaciireg.h> 132 #include <dev/sk/yukonreg.h> 133 134 MODULE_DEPEND(sk, pci, 1, 1, 1); 135 MODULE_DEPEND(sk, ether, 1, 1, 1); 136 MODULE_DEPEND(sk, miibus, 1, 1, 1); 137 138 /* "device miibus" required. See GENERIC if you get errors here. */ 139 #include "miibus_if.h" 140 141 #ifndef lint 142 static const char rcsid[] = 143 "$FreeBSD$"; 144 #endif 145 146 static struct sk_type sk_devs[] = { 147 { 148 VENDORID_SK, 149 DEVICEID_SK_V1, 150 "SysKonnect Gigabit Ethernet (V1.0)" 151 }, 152 { 153 VENDORID_SK, 154 DEVICEID_SK_V2, 155 "SysKonnect Gigabit Ethernet (V2.0)" 156 }, 157 { 158 VENDORID_MARVELL, 159 DEVICEID_SK_V2, 160 "Marvell Gigabit Ethernet" 161 }, 162 { 163 VENDORID_MARVELL, 164 DEVICEID_BELKIN_5005, 165 "Belkin F5D5005 Gigabit Ethernet" 166 }, 167 { 168 VENDORID_3COM, 169 DEVICEID_3COM_3C940, 170 "3Com 3C940 Gigabit Ethernet" 171 }, 172 { 173 VENDORID_LINKSYS, 174 DEVICEID_LINKSYS_EG1032, 175 "Linksys EG1032 Gigabit Ethernet" 176 }, 177 { 178 VENDORID_DLINK, 179 DEVICEID_DLINK_DGE530T_A1, 180 "D-Link DGE-530T Gigabit Ethernet" 181 }, 182 { 183 VENDORID_DLINK, 184 DEVICEID_DLINK_DGE530T_B1, 185 "D-Link DGE-530T Gigabit Ethernet" 186 }, 187 { 0, 0, NULL } 188 }; 189 190 static int skc_probe(device_t); 191 static int skc_attach(device_t); 192 static int skc_detach(device_t); 193 static void skc_shutdown(device_t); 194 static int skc_suspend(device_t); 195 static int skc_resume(device_t); 196 static int sk_detach(device_t); 197 static int sk_probe(device_t); 198 static int sk_attach(device_t); 199 static void sk_tick(void *); 200 static void sk_yukon_tick(void *); 201 static void sk_intr(void *); 202 static void sk_intr_xmac(struct sk_if_softc *); 203 static void sk_intr_bcom(struct sk_if_softc *); 204 static void sk_intr_yukon(struct sk_if_softc *); 205 static __inline void sk_rxcksum(struct ifnet *, struct mbuf *, u_int32_t); 206 static __inline int sk_rxvalid(struct sk_softc *, u_int32_t, u_int32_t); 207 static void sk_rxeof(struct sk_if_softc *); 208 static void sk_jumbo_rxeof(struct sk_if_softc *); 209 static void sk_txeof(struct sk_if_softc *); 210 static void sk_txcksum(struct ifnet *, struct mbuf *, struct sk_tx_desc *); 211 static int sk_encap(struct sk_if_softc *, struct mbuf **); 212 static void sk_start(struct ifnet *); 213 static void sk_start_locked(struct ifnet *); 214 static int sk_ioctl(struct ifnet *, u_long, caddr_t); 215 static void sk_init(void *); 216 static void sk_init_locked(struct sk_if_softc *); 217 static void sk_init_xmac(struct sk_if_softc *); 218 static void sk_init_yukon(struct sk_if_softc *); 219 static void sk_stop(struct sk_if_softc *); 220 static void sk_watchdog(struct ifnet *); 221 static int sk_ifmedia_upd(struct ifnet *); 222 static void sk_ifmedia_sts(struct ifnet *, struct ifmediareq *); 223 static void sk_reset(struct sk_softc *); 224 static __inline void sk_discard_rxbuf(struct sk_if_softc *, int); 225 static __inline void sk_discard_jumbo_rxbuf(struct sk_if_softc *, int); 226 static int sk_newbuf(struct sk_if_softc *, int); 227 static int sk_jumbo_newbuf(struct sk_if_softc *, int); 228 static void sk_dmamap_cb(void *, bus_dma_segment_t *, int, int); 229 static int sk_dma_alloc(struct sk_if_softc *); 230 static void sk_dma_free(struct sk_if_softc *); 231 static void *sk_jalloc(struct sk_if_softc *); 232 static void sk_jfree(void *, void *); 233 static int sk_init_rx_ring(struct sk_if_softc *); 234 static int sk_init_jumbo_rx_ring(struct sk_if_softc *); 235 static void sk_init_tx_ring(struct sk_if_softc *); 236 static u_int32_t sk_win_read_4(struct sk_softc *, int); 237 static u_int16_t sk_win_read_2(struct sk_softc *, int); 238 static u_int8_t sk_win_read_1(struct sk_softc *, int); 239 static void sk_win_write_4(struct sk_softc *, int, u_int32_t); 240 static void sk_win_write_2(struct sk_softc *, int, u_int32_t); 241 static void sk_win_write_1(struct sk_softc *, int, u_int32_t); 242 243 static int sk_miibus_readreg(device_t, int, int); 244 static int sk_miibus_writereg(device_t, int, int, int); 245 static void sk_miibus_statchg(device_t); 246 247 static int sk_xmac_miibus_readreg(struct sk_if_softc *, int, int); 248 static int sk_xmac_miibus_writereg(struct sk_if_softc *, int, int, 249 int); 250 static void sk_xmac_miibus_statchg(struct sk_if_softc *); 251 252 static int sk_marv_miibus_readreg(struct sk_if_softc *, int, int); 253 static int sk_marv_miibus_writereg(struct sk_if_softc *, int, int, 254 int); 255 static void sk_marv_miibus_statchg(struct sk_if_softc *); 256 257 static uint32_t sk_xmchash(const uint8_t *); 258 static uint32_t sk_gmchash(const uint8_t *); 259 static void sk_setfilt(struct sk_if_softc *, u_int16_t *, int); 260 static void sk_setmulti(struct sk_if_softc *); 261 static void sk_setpromisc(struct sk_if_softc *); 262 263 static int sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high); 264 static int sysctl_hw_sk_int_mod(SYSCTL_HANDLER_ARGS); 265 266 /* 267 * It seems that SK-NET GENESIS supports very simple checksum offload 268 * capability for Tx and I believe it can generate 0 checksum value for 269 * UDP packets in Tx as the hardware can't differenciate UDP packets from 270 * TCP packets. 0 chcecksum value for UDP packet is an invalid one as it 271 * means sender didn't perforam checksum computation. For the safety I 272 * disabled UDP checksum offload capability at the moment. Alternatively 273 * we can intrduce a LINK0/LINK1 flag as hme(4) did in its Tx checksum 274 * offload routine. 275 */ 276 #define SK_CSUM_FEATURES (CSUM_TCP) 277 278 /* 279 * Note that we have newbus methods for both the GEnesis controller 280 * itself and the XMAC(s). The XMACs are children of the GEnesis, and 281 * the miibus code is a child of the XMACs. We need to do it this way 282 * so that the miibus drivers can access the PHY registers on the 283 * right PHY. It's not quite what I had in mind, but it's the only 284 * design that achieves the desired effect. 285 */ 286 static device_method_t skc_methods[] = { 287 /* Device interface */ 288 DEVMETHOD(device_probe, skc_probe), 289 DEVMETHOD(device_attach, skc_attach), 290 DEVMETHOD(device_detach, skc_detach), 291 DEVMETHOD(device_suspend, skc_suspend), 292 DEVMETHOD(device_resume, skc_resume), 293 DEVMETHOD(device_shutdown, skc_shutdown), 294 295 /* bus interface */ 296 DEVMETHOD(bus_print_child, bus_generic_print_child), 297 DEVMETHOD(bus_driver_added, bus_generic_driver_added), 298 299 { 0, 0 } 300 }; 301 302 static driver_t skc_driver = { 303 "skc", 304 skc_methods, 305 sizeof(struct sk_softc) 306 }; 307 308 static devclass_t skc_devclass; 309 310 static device_method_t sk_methods[] = { 311 /* Device interface */ 312 DEVMETHOD(device_probe, sk_probe), 313 DEVMETHOD(device_attach, sk_attach), 314 DEVMETHOD(device_detach, sk_detach), 315 DEVMETHOD(device_shutdown, bus_generic_shutdown), 316 317 /* bus interface */ 318 DEVMETHOD(bus_print_child, bus_generic_print_child), 319 DEVMETHOD(bus_driver_added, bus_generic_driver_added), 320 321 /* MII interface */ 322 DEVMETHOD(miibus_readreg, sk_miibus_readreg), 323 DEVMETHOD(miibus_writereg, sk_miibus_writereg), 324 DEVMETHOD(miibus_statchg, sk_miibus_statchg), 325 326 { 0, 0 } 327 }; 328 329 static driver_t sk_driver = { 330 "sk", 331 sk_methods, 332 sizeof(struct sk_if_softc) 333 }; 334 335 static devclass_t sk_devclass; 336 337 DRIVER_MODULE(skc, pci, skc_driver, skc_devclass, 0, 0); 338 DRIVER_MODULE(sk, skc, sk_driver, sk_devclass, 0, 0); 339 DRIVER_MODULE(miibus, sk, miibus_driver, miibus_devclass, 0, 0); 340 341 static struct resource_spec sk_res_spec_io[] = { 342 { SYS_RES_IOPORT, PCIR_BAR(1), RF_ACTIVE }, 343 { SYS_RES_IRQ, 0, RF_ACTIVE | RF_SHAREABLE }, 344 { -1, 0, 0 } 345 }; 346 347 static struct resource_spec sk_res_spec_mem[] = { 348 { SYS_RES_MEMORY, PCIR_BAR(0), RF_ACTIVE }, 349 { SYS_RES_IRQ, 0, RF_ACTIVE | RF_SHAREABLE }, 350 { -1, 0, 0 } 351 }; 352 353 #define SK_SETBIT(sc, reg, x) \ 354 CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) | x) 355 356 #define SK_CLRBIT(sc, reg, x) \ 357 CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) & ~x) 358 359 #define SK_WIN_SETBIT_4(sc, reg, x) \ 360 sk_win_write_4(sc, reg, sk_win_read_4(sc, reg) | x) 361 362 #define SK_WIN_CLRBIT_4(sc, reg, x) \ 363 sk_win_write_4(sc, reg, sk_win_read_4(sc, reg) & ~x) 364 365 #define SK_WIN_SETBIT_2(sc, reg, x) \ 366 sk_win_write_2(sc, reg, sk_win_read_2(sc, reg) | x) 367 368 #define SK_WIN_CLRBIT_2(sc, reg, x) \ 369 sk_win_write_2(sc, reg, sk_win_read_2(sc, reg) & ~x) 370 371 static u_int32_t 372 sk_win_read_4(sc, reg) 373 struct sk_softc *sc; 374 int reg; 375 { 376 #ifdef SK_USEIOSPACE 377 CSR_WRITE_4(sc, SK_RAP, SK_WIN(reg)); 378 return(CSR_READ_4(sc, SK_WIN_BASE + SK_REG(reg))); 379 #else 380 return(CSR_READ_4(sc, reg)); 381 #endif 382 } 383 384 static u_int16_t 385 sk_win_read_2(sc, reg) 386 struct sk_softc *sc; 387 int reg; 388 { 389 #ifdef SK_USEIOSPACE 390 CSR_WRITE_4(sc, SK_RAP, SK_WIN(reg)); 391 return(CSR_READ_2(sc, SK_WIN_BASE + SK_REG(reg))); 392 #else 393 return(CSR_READ_2(sc, reg)); 394 #endif 395 } 396 397 static u_int8_t 398 sk_win_read_1(sc, reg) 399 struct sk_softc *sc; 400 int reg; 401 { 402 #ifdef SK_USEIOSPACE 403 CSR_WRITE_4(sc, SK_RAP, SK_WIN(reg)); 404 return(CSR_READ_1(sc, SK_WIN_BASE + SK_REG(reg))); 405 #else 406 return(CSR_READ_1(sc, reg)); 407 #endif 408 } 409 410 static void 411 sk_win_write_4(sc, reg, val) 412 struct sk_softc *sc; 413 int reg; 414 u_int32_t val; 415 { 416 #ifdef SK_USEIOSPACE 417 CSR_WRITE_4(sc, SK_RAP, SK_WIN(reg)); 418 CSR_WRITE_4(sc, SK_WIN_BASE + SK_REG(reg), val); 419 #else 420 CSR_WRITE_4(sc, reg, val); 421 #endif 422 return; 423 } 424 425 static void 426 sk_win_write_2(sc, reg, val) 427 struct sk_softc *sc; 428 int reg; 429 u_int32_t val; 430 { 431 #ifdef SK_USEIOSPACE 432 CSR_WRITE_4(sc, SK_RAP, SK_WIN(reg)); 433 CSR_WRITE_2(sc, SK_WIN_BASE + SK_REG(reg), val); 434 #else 435 CSR_WRITE_2(sc, reg, val); 436 #endif 437 return; 438 } 439 440 static void 441 sk_win_write_1(sc, reg, val) 442 struct sk_softc *sc; 443 int reg; 444 u_int32_t val; 445 { 446 #ifdef SK_USEIOSPACE 447 CSR_WRITE_4(sc, SK_RAP, SK_WIN(reg)); 448 CSR_WRITE_1(sc, SK_WIN_BASE + SK_REG(reg), val); 449 #else 450 CSR_WRITE_1(sc, reg, val); 451 #endif 452 return; 453 } 454 455 static int 456 sk_miibus_readreg(dev, phy, reg) 457 device_t dev; 458 int phy, reg; 459 { 460 struct sk_if_softc *sc_if; 461 int v; 462 463 sc_if = device_get_softc(dev); 464 465 SK_IF_MII_LOCK(sc_if); 466 switch(sc_if->sk_softc->sk_type) { 467 case SK_GENESIS: 468 v = sk_xmac_miibus_readreg(sc_if, phy, reg); 469 break; 470 case SK_YUKON: 471 case SK_YUKON_LITE: 472 case SK_YUKON_LP: 473 v = sk_marv_miibus_readreg(sc_if, phy, reg); 474 break; 475 default: 476 v = 0; 477 break; 478 } 479 SK_IF_MII_UNLOCK(sc_if); 480 481 return (v); 482 } 483 484 static int 485 sk_miibus_writereg(dev, phy, reg, val) 486 device_t dev; 487 int phy, reg, val; 488 { 489 struct sk_if_softc *sc_if; 490 int v; 491 492 sc_if = device_get_softc(dev); 493 494 SK_IF_MII_LOCK(sc_if); 495 switch(sc_if->sk_softc->sk_type) { 496 case SK_GENESIS: 497 v = sk_xmac_miibus_writereg(sc_if, phy, reg, val); 498 break; 499 case SK_YUKON: 500 case SK_YUKON_LITE: 501 case SK_YUKON_LP: 502 v = sk_marv_miibus_writereg(sc_if, phy, reg, val); 503 break; 504 default: 505 v = 0; 506 break; 507 } 508 SK_IF_MII_UNLOCK(sc_if); 509 510 return (v); 511 } 512 513 static void 514 sk_miibus_statchg(dev) 515 device_t dev; 516 { 517 struct sk_if_softc *sc_if; 518 519 sc_if = device_get_softc(dev); 520 521 SK_IF_MII_LOCK(sc_if); 522 switch(sc_if->sk_softc->sk_type) { 523 case SK_GENESIS: 524 sk_xmac_miibus_statchg(sc_if); 525 break; 526 case SK_YUKON: 527 case SK_YUKON_LITE: 528 case SK_YUKON_LP: 529 sk_marv_miibus_statchg(sc_if); 530 break; 531 } 532 SK_IF_MII_UNLOCK(sc_if); 533 534 return; 535 } 536 537 static int 538 sk_xmac_miibus_readreg(sc_if, phy, reg) 539 struct sk_if_softc *sc_if; 540 int phy, reg; 541 { 542 int i; 543 544 if (sc_if->sk_phytype == SK_PHYTYPE_XMAC && phy != 0) 545 return(0); 546 547 SK_XM_WRITE_2(sc_if, XM_PHY_ADDR, reg|(phy << 8)); 548 SK_XM_READ_2(sc_if, XM_PHY_DATA); 549 if (sc_if->sk_phytype != SK_PHYTYPE_XMAC) { 550 for (i = 0; i < SK_TIMEOUT; i++) { 551 DELAY(1); 552 if (SK_XM_READ_2(sc_if, XM_MMUCMD) & 553 XM_MMUCMD_PHYDATARDY) 554 break; 555 } 556 557 if (i == SK_TIMEOUT) { 558 if_printf(sc_if->sk_ifp, "phy failed to come ready\n"); 559 return(0); 560 } 561 } 562 DELAY(1); 563 i = SK_XM_READ_2(sc_if, XM_PHY_DATA); 564 565 return(i); 566 } 567 568 static int 569 sk_xmac_miibus_writereg(sc_if, phy, reg, val) 570 struct sk_if_softc *sc_if; 571 int phy, reg, val; 572 { 573 int i; 574 575 SK_XM_WRITE_2(sc_if, XM_PHY_ADDR, reg|(phy << 8)); 576 for (i = 0; i < SK_TIMEOUT; i++) { 577 if (!(SK_XM_READ_2(sc_if, XM_MMUCMD) & XM_MMUCMD_PHYBUSY)) 578 break; 579 } 580 581 if (i == SK_TIMEOUT) { 582 if_printf(sc_if->sk_ifp, "phy failed to come ready\n"); 583 return (ETIMEDOUT); 584 } 585 586 SK_XM_WRITE_2(sc_if, XM_PHY_DATA, val); 587 for (i = 0; i < SK_TIMEOUT; i++) { 588 DELAY(1); 589 if (!(SK_XM_READ_2(sc_if, XM_MMUCMD) & XM_MMUCMD_PHYBUSY)) 590 break; 591 } 592 if (i == SK_TIMEOUT) 593 if_printf(sc_if->sk_ifp, "phy write timed out\n"); 594 595 return(0); 596 } 597 598 static void 599 sk_xmac_miibus_statchg(sc_if) 600 struct sk_if_softc *sc_if; 601 { 602 struct mii_data *mii; 603 604 mii = device_get_softc(sc_if->sk_miibus); 605 606 /* 607 * If this is a GMII PHY, manually set the XMAC's 608 * duplex mode accordingly. 609 */ 610 if (sc_if->sk_phytype != SK_PHYTYPE_XMAC) { 611 if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) { 612 SK_XM_SETBIT_2(sc_if, XM_MMUCMD, XM_MMUCMD_GMIIFDX); 613 } else { 614 SK_XM_CLRBIT_2(sc_if, XM_MMUCMD, XM_MMUCMD_GMIIFDX); 615 } 616 } 617 } 618 619 static int 620 sk_marv_miibus_readreg(sc_if, phy, reg) 621 struct sk_if_softc *sc_if; 622 int phy, reg; 623 { 624 u_int16_t val; 625 int i; 626 627 if (phy != 0 || 628 (sc_if->sk_phytype != SK_PHYTYPE_MARV_COPPER && 629 sc_if->sk_phytype != SK_PHYTYPE_MARV_FIBER)) { 630 return(0); 631 } 632 633 SK_YU_WRITE_2(sc_if, YUKON_SMICR, YU_SMICR_PHYAD(phy) | 634 YU_SMICR_REGAD(reg) | YU_SMICR_OP_READ); 635 636 for (i = 0; i < SK_TIMEOUT; i++) { 637 DELAY(1); 638 val = SK_YU_READ_2(sc_if, YUKON_SMICR); 639 if (val & YU_SMICR_READ_VALID) 640 break; 641 } 642 643 if (i == SK_TIMEOUT) { 644 if_printf(sc_if->sk_ifp, "phy failed to come ready\n"); 645 return(0); 646 } 647 648 val = SK_YU_READ_2(sc_if, YUKON_SMIDR); 649 650 return(val); 651 } 652 653 static int 654 sk_marv_miibus_writereg(sc_if, phy, reg, val) 655 struct sk_if_softc *sc_if; 656 int phy, reg, val; 657 { 658 int i; 659 660 SK_YU_WRITE_2(sc_if, YUKON_SMIDR, val); 661 SK_YU_WRITE_2(sc_if, YUKON_SMICR, YU_SMICR_PHYAD(phy) | 662 YU_SMICR_REGAD(reg) | YU_SMICR_OP_WRITE); 663 664 for (i = 0; i < SK_TIMEOUT; i++) { 665 DELAY(1); 666 if ((SK_YU_READ_2(sc_if, YUKON_SMICR) & YU_SMICR_BUSY) == 0) 667 break; 668 } 669 if (i == SK_TIMEOUT) 670 if_printf(sc_if->sk_ifp, "phy write timeout\n"); 671 672 return(0); 673 } 674 675 static void 676 sk_marv_miibus_statchg(sc_if) 677 struct sk_if_softc *sc_if; 678 { 679 return; 680 } 681 682 #define HASH_BITS 6 683 684 static u_int32_t 685 sk_xmchash(addr) 686 const uint8_t *addr; 687 { 688 uint32_t crc; 689 690 /* Compute CRC for the address value. */ 691 crc = ether_crc32_le(addr, ETHER_ADDR_LEN); 692 693 return (~crc & ((1 << HASH_BITS) - 1)); 694 } 695 696 /* gmchash is just a big endian crc */ 697 static u_int32_t 698 sk_gmchash(addr) 699 const uint8_t *addr; 700 { 701 uint32_t crc; 702 703 /* Compute CRC for the address value. */ 704 crc = ether_crc32_be(addr, ETHER_ADDR_LEN); 705 706 return (crc & ((1 << HASH_BITS) - 1)); 707 } 708 709 static void 710 sk_setfilt(sc_if, addr, slot) 711 struct sk_if_softc *sc_if; 712 u_int16_t *addr; 713 int slot; 714 { 715 int base; 716 717 base = XM_RXFILT_ENTRY(slot); 718 719 SK_XM_WRITE_2(sc_if, base, addr[0]); 720 SK_XM_WRITE_2(sc_if, base + 2, addr[1]); 721 SK_XM_WRITE_2(sc_if, base + 4, addr[2]); 722 723 return; 724 } 725 726 static void 727 sk_setmulti(sc_if) 728 struct sk_if_softc *sc_if; 729 { 730 struct sk_softc *sc = sc_if->sk_softc; 731 struct ifnet *ifp = sc_if->sk_ifp; 732 u_int32_t hashes[2] = { 0, 0 }; 733 int h = 0, i; 734 struct ifmultiaddr *ifma; 735 u_int16_t dummy[] = { 0, 0, 0 }; 736 u_int16_t maddr[(ETHER_ADDR_LEN+1)/2]; 737 738 SK_IF_LOCK_ASSERT(sc_if); 739 740 /* First, zot all the existing filters. */ 741 switch(sc->sk_type) { 742 case SK_GENESIS: 743 for (i = 1; i < XM_RXFILT_MAX; i++) 744 sk_setfilt(sc_if, dummy, i); 745 746 SK_XM_WRITE_4(sc_if, XM_MAR0, 0); 747 SK_XM_WRITE_4(sc_if, XM_MAR2, 0); 748 break; 749 case SK_YUKON: 750 case SK_YUKON_LITE: 751 case SK_YUKON_LP: 752 SK_YU_WRITE_2(sc_if, YUKON_MCAH1, 0); 753 SK_YU_WRITE_2(sc_if, YUKON_MCAH2, 0); 754 SK_YU_WRITE_2(sc_if, YUKON_MCAH3, 0); 755 SK_YU_WRITE_2(sc_if, YUKON_MCAH4, 0); 756 break; 757 } 758 759 /* Now program new ones. */ 760 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) { 761 hashes[0] = 0xFFFFFFFF; 762 hashes[1] = 0xFFFFFFFF; 763 } else { 764 i = 1; 765 IF_ADDR_LOCK(ifp); 766 TAILQ_FOREACH_REVERSE(ifma, &ifp->if_multiaddrs, ifmultihead, ifma_link) { 767 if (ifma->ifma_addr->sa_family != AF_LINK) 768 continue; 769 /* 770 * Program the first XM_RXFILT_MAX multicast groups 771 * into the perfect filter. For all others, 772 * use the hash table. 773 */ 774 if (sc->sk_type == SK_GENESIS && i < XM_RXFILT_MAX) { 775 bcopy(LLADDR( 776 (struct sockaddr_dl *)ifma->ifma_addr), 777 maddr, ETHER_ADDR_LEN); 778 sk_setfilt(sc_if, maddr, i); 779 i++; 780 continue; 781 } 782 783 switch(sc->sk_type) { 784 case SK_GENESIS: 785 bcopy(LLADDR( 786 (struct sockaddr_dl *)ifma->ifma_addr), 787 maddr, ETHER_ADDR_LEN); 788 h = sk_xmchash((const uint8_t *)maddr); 789 break; 790 case SK_YUKON: 791 case SK_YUKON_LITE: 792 case SK_YUKON_LP: 793 bcopy(LLADDR( 794 (struct sockaddr_dl *)ifma->ifma_addr), 795 maddr, ETHER_ADDR_LEN); 796 h = sk_gmchash((const uint8_t *)maddr); 797 break; 798 } 799 if (h < 32) 800 hashes[0] |= (1 << h); 801 else 802 hashes[1] |= (1 << (h - 32)); 803 } 804 IF_ADDR_UNLOCK(ifp); 805 } 806 807 switch(sc->sk_type) { 808 case SK_GENESIS: 809 SK_XM_SETBIT_4(sc_if, XM_MODE, XM_MODE_RX_USE_HASH| 810 XM_MODE_RX_USE_PERFECT); 811 SK_XM_WRITE_4(sc_if, XM_MAR0, hashes[0]); 812 SK_XM_WRITE_4(sc_if, XM_MAR2, hashes[1]); 813 break; 814 case SK_YUKON: 815 case SK_YUKON_LITE: 816 case SK_YUKON_LP: 817 SK_YU_WRITE_2(sc_if, YUKON_MCAH1, hashes[0] & 0xffff); 818 SK_YU_WRITE_2(sc_if, YUKON_MCAH2, (hashes[0] >> 16) & 0xffff); 819 SK_YU_WRITE_2(sc_if, YUKON_MCAH3, hashes[1] & 0xffff); 820 SK_YU_WRITE_2(sc_if, YUKON_MCAH4, (hashes[1] >> 16) & 0xffff); 821 break; 822 } 823 824 return; 825 } 826 827 static void 828 sk_setpromisc(sc_if) 829 struct sk_if_softc *sc_if; 830 { 831 struct sk_softc *sc = sc_if->sk_softc; 832 struct ifnet *ifp = sc_if->sk_ifp; 833 834 SK_IF_LOCK_ASSERT(sc_if); 835 836 switch(sc->sk_type) { 837 case SK_GENESIS: 838 if (ifp->if_flags & IFF_PROMISC) { 839 SK_XM_SETBIT_4(sc_if, XM_MODE, XM_MODE_RX_PROMISC); 840 } else { 841 SK_XM_CLRBIT_4(sc_if, XM_MODE, XM_MODE_RX_PROMISC); 842 } 843 break; 844 case SK_YUKON: 845 case SK_YUKON_LITE: 846 case SK_YUKON_LP: 847 if (ifp->if_flags & IFF_PROMISC) { 848 SK_YU_CLRBIT_2(sc_if, YUKON_RCR, 849 YU_RCR_UFLEN | YU_RCR_MUFLEN); 850 } else { 851 SK_YU_SETBIT_2(sc_if, YUKON_RCR, 852 YU_RCR_UFLEN | YU_RCR_MUFLEN); 853 } 854 break; 855 } 856 857 return; 858 } 859 860 static int 861 sk_init_rx_ring(sc_if) 862 struct sk_if_softc *sc_if; 863 { 864 struct sk_ring_data *rd; 865 bus_addr_t addr; 866 u_int32_t csum_start; 867 int i; 868 869 sc_if->sk_cdata.sk_rx_cons = 0; 870 871 csum_start = (ETHER_HDR_LEN + sizeof(struct ip)) << 16 | 872 ETHER_HDR_LEN; 873 rd = &sc_if->sk_rdata; 874 bzero(rd->sk_rx_ring, sizeof(struct sk_rx_desc) * SK_RX_RING_CNT); 875 for (i = 0; i < SK_RX_RING_CNT; i++) { 876 if (sk_newbuf(sc_if, i) != 0) 877 return (ENOBUFS); 878 if (i == (SK_RX_RING_CNT - 1)) 879 addr = SK_RX_RING_ADDR(sc_if, 0); 880 else 881 addr = SK_RX_RING_ADDR(sc_if, i + 1); 882 rd->sk_rx_ring[i].sk_next = htole32(SK_ADDR_LO(addr)); 883 rd->sk_rx_ring[i].sk_csum_start = htole32(csum_start); 884 } 885 886 bus_dmamap_sync(sc_if->sk_cdata.sk_rx_ring_tag, 887 sc_if->sk_cdata.sk_rx_ring_map, 888 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 889 890 return(0); 891 } 892 893 static int 894 sk_init_jumbo_rx_ring(sc_if) 895 struct sk_if_softc *sc_if; 896 { 897 struct sk_ring_data *rd; 898 bus_addr_t addr; 899 u_int32_t csum_start; 900 int i; 901 902 sc_if->sk_cdata.sk_jumbo_rx_cons = 0; 903 904 csum_start = ((ETHER_HDR_LEN + sizeof(struct ip)) << 16) | 905 ETHER_HDR_LEN; 906 rd = &sc_if->sk_rdata; 907 bzero(rd->sk_jumbo_rx_ring, 908 sizeof(struct sk_rx_desc) * SK_JUMBO_RX_RING_CNT); 909 for (i = 0; i < SK_JUMBO_RX_RING_CNT; i++) { 910 if (sk_jumbo_newbuf(sc_if, i) != 0) 911 return (ENOBUFS); 912 if (i == (SK_JUMBO_RX_RING_CNT - 1)) 913 addr = SK_JUMBO_RX_RING_ADDR(sc_if, 0); 914 else 915 addr = SK_JUMBO_RX_RING_ADDR(sc_if, i + 1); 916 rd->sk_jumbo_rx_ring[i].sk_next = htole32(SK_ADDR_LO(addr)); 917 rd->sk_jumbo_rx_ring[i].sk_csum_start = htole32(csum_start); 918 } 919 920 bus_dmamap_sync(sc_if->sk_cdata.sk_jumbo_rx_ring_tag, 921 sc_if->sk_cdata.sk_jumbo_rx_ring_map, 922 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 923 924 return (0); 925 } 926 927 static void 928 sk_init_tx_ring(sc_if) 929 struct sk_if_softc *sc_if; 930 { 931 struct sk_ring_data *rd; 932 struct sk_txdesc *txd; 933 bus_addr_t addr; 934 int i; 935 936 STAILQ_INIT(&sc_if->sk_cdata.sk_txfreeq); 937 STAILQ_INIT(&sc_if->sk_cdata.sk_txbusyq); 938 939 sc_if->sk_cdata.sk_tx_prod = 0; 940 sc_if->sk_cdata.sk_tx_cons = 0; 941 sc_if->sk_cdata.sk_tx_cnt = 0; 942 943 rd = &sc_if->sk_rdata; 944 bzero(rd->sk_tx_ring, sizeof(struct sk_tx_desc) * SK_TX_RING_CNT); 945 for (i = 0; i < SK_TX_RING_CNT; i++) { 946 if (i == (SK_TX_RING_CNT - 1)) 947 addr = SK_TX_RING_ADDR(sc_if, 0); 948 else 949 addr = SK_TX_RING_ADDR(sc_if, i + 1); 950 rd->sk_tx_ring[i].sk_next = htole32(SK_ADDR_LO(addr)); 951 txd = &sc_if->sk_cdata.sk_txdesc[i]; 952 STAILQ_INSERT_TAIL(&sc_if->sk_cdata.sk_txfreeq, txd, tx_q); 953 } 954 955 bus_dmamap_sync(sc_if->sk_cdata.sk_tx_ring_tag, 956 sc_if->sk_cdata.sk_tx_ring_map, 957 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 958 } 959 960 static __inline void 961 sk_discard_rxbuf(sc_if, idx) 962 struct sk_if_softc *sc_if; 963 int idx; 964 { 965 struct sk_rx_desc *r; 966 struct sk_rxdesc *rxd; 967 struct mbuf *m; 968 969 970 r = &sc_if->sk_rdata.sk_rx_ring[idx]; 971 rxd = &sc_if->sk_cdata.sk_rxdesc[idx]; 972 m = rxd->rx_m; 973 r->sk_ctl = htole32(m->m_len | SK_RXSTAT | SK_OPCODE_CSUM); 974 } 975 976 static __inline void 977 sk_discard_jumbo_rxbuf(sc_if, idx) 978 struct sk_if_softc *sc_if; 979 int idx; 980 { 981 struct sk_rx_desc *r; 982 struct sk_rxdesc *rxd; 983 struct mbuf *m; 984 985 r = &sc_if->sk_rdata.sk_jumbo_rx_ring[idx]; 986 rxd = &sc_if->sk_cdata.sk_jumbo_rxdesc[idx]; 987 m = rxd->rx_m; 988 r->sk_ctl = htole32(m->m_len | SK_RXSTAT | SK_OPCODE_CSUM); 989 } 990 991 static int 992 sk_newbuf(sc_if, idx) 993 struct sk_if_softc *sc_if; 994 int idx; 995 { 996 struct sk_rx_desc *r; 997 struct sk_rxdesc *rxd; 998 struct mbuf *m; 999 bus_dma_segment_t segs[1]; 1000 bus_dmamap_t map; 1001 int nsegs; 1002 1003 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 1004 if (m == NULL) 1005 return (ENOBUFS); 1006 m->m_len = m->m_pkthdr.len = MCLBYTES; 1007 m_adj(m, ETHER_ALIGN); 1008 1009 if (bus_dmamap_load_mbuf_sg(sc_if->sk_cdata.sk_rx_tag, 1010 sc_if->sk_cdata.sk_rx_sparemap, m, segs, &nsegs, 0) != 0) { 1011 m_freem(m); 1012 return (ENOBUFS); 1013 } 1014 KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs)); 1015 1016 rxd = &sc_if->sk_cdata.sk_rxdesc[idx]; 1017 if (rxd->rx_m != NULL) { 1018 bus_dmamap_sync(sc_if->sk_cdata.sk_rx_tag, rxd->rx_dmamap, 1019 BUS_DMASYNC_POSTREAD); 1020 bus_dmamap_unload(sc_if->sk_cdata.sk_rx_tag, rxd->rx_dmamap); 1021 } 1022 map = rxd->rx_dmamap; 1023 rxd->rx_dmamap = sc_if->sk_cdata.sk_rx_sparemap; 1024 sc_if->sk_cdata.sk_rx_sparemap = map; 1025 bus_dmamap_sync(sc_if->sk_cdata.sk_rx_tag, rxd->rx_dmamap, 1026 BUS_DMASYNC_PREREAD); 1027 rxd->rx_m = m; 1028 r = &sc_if->sk_rdata.sk_rx_ring[idx]; 1029 r->sk_data_lo = htole32(SK_ADDR_LO(segs[0].ds_addr)); 1030 r->sk_data_hi = htole32(SK_ADDR_HI(segs[0].ds_addr)); 1031 r->sk_ctl = htole32(segs[0].ds_len | SK_RXSTAT | SK_OPCODE_CSUM); 1032 1033 return (0); 1034 } 1035 1036 static int 1037 sk_jumbo_newbuf(sc_if, idx) 1038 struct sk_if_softc *sc_if; 1039 int idx; 1040 { 1041 struct sk_rx_desc *r; 1042 struct sk_rxdesc *rxd; 1043 struct mbuf *m; 1044 bus_dma_segment_t segs[1]; 1045 bus_dmamap_t map; 1046 int nsegs; 1047 void *buf; 1048 1049 MGETHDR(m, M_DONTWAIT, MT_DATA); 1050 if (m == NULL) 1051 return (ENOBUFS); 1052 buf = sk_jalloc(sc_if); 1053 if (buf == NULL) { 1054 m_freem(m); 1055 return (ENOBUFS); 1056 } 1057 /* Attach the buffer to the mbuf */ 1058 MEXTADD(m, buf, SK_JLEN, sk_jfree, (struct sk_if_softc *)sc_if, 0, 1059 EXT_NET_DRV); 1060 if ((m->m_flags & M_EXT) == 0) { 1061 m_freem(m); 1062 return (ENOBUFS); 1063 } 1064 m->m_pkthdr.len = m->m_len = SK_JLEN; 1065 /* 1066 * Adjust alignment so packet payload begins on a 1067 * longword boundary. Mandatory for Alpha, useful on 1068 * x86 too. 1069 */ 1070 m_adj(m, ETHER_ALIGN); 1071 1072 if (bus_dmamap_load_mbuf_sg(sc_if->sk_cdata.sk_jumbo_rx_tag, 1073 sc_if->sk_cdata.sk_jumbo_rx_sparemap, m, segs, &nsegs, 0) != 0) { 1074 m_freem(m); 1075 return (ENOBUFS); 1076 } 1077 KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs)); 1078 1079 rxd = &sc_if->sk_cdata.sk_jumbo_rxdesc[idx]; 1080 if (rxd->rx_m != NULL) { 1081 bus_dmamap_sync(sc_if->sk_cdata.sk_jumbo_rx_tag, rxd->rx_dmamap, 1082 BUS_DMASYNC_POSTREAD); 1083 bus_dmamap_unload(sc_if->sk_cdata.sk_jumbo_rx_tag, 1084 rxd->rx_dmamap); 1085 } 1086 map = rxd->rx_dmamap; 1087 rxd->rx_dmamap = sc_if->sk_cdata.sk_jumbo_rx_sparemap; 1088 sc_if->sk_cdata.sk_jumbo_rx_sparemap = map; 1089 bus_dmamap_sync(sc_if->sk_cdata.sk_jumbo_rx_tag, rxd->rx_dmamap, 1090 BUS_DMASYNC_PREREAD); 1091 rxd->rx_m = m; 1092 r = &sc_if->sk_rdata.sk_jumbo_rx_ring[idx]; 1093 r->sk_data_lo = htole32(SK_ADDR_LO(segs[0].ds_addr)); 1094 r->sk_data_hi = htole32(SK_ADDR_HI(segs[0].ds_addr)); 1095 r->sk_ctl = htole32(segs[0].ds_len | SK_RXSTAT | SK_OPCODE_CSUM); 1096 1097 return (0); 1098 } 1099 1100 /* 1101 * Set media options. 1102 */ 1103 static int 1104 sk_ifmedia_upd(ifp) 1105 struct ifnet *ifp; 1106 { 1107 struct sk_if_softc *sc_if = ifp->if_softc; 1108 struct mii_data *mii; 1109 1110 mii = device_get_softc(sc_if->sk_miibus); 1111 sk_init(sc_if); 1112 mii_mediachg(mii); 1113 1114 return(0); 1115 } 1116 1117 /* 1118 * Report current media status. 1119 */ 1120 static void 1121 sk_ifmedia_sts(ifp, ifmr) 1122 struct ifnet *ifp; 1123 struct ifmediareq *ifmr; 1124 { 1125 struct sk_if_softc *sc_if; 1126 struct mii_data *mii; 1127 1128 sc_if = ifp->if_softc; 1129 mii = device_get_softc(sc_if->sk_miibus); 1130 1131 mii_pollstat(mii); 1132 ifmr->ifm_active = mii->mii_media_active; 1133 ifmr->ifm_status = mii->mii_media_status; 1134 1135 return; 1136 } 1137 1138 static int 1139 sk_ioctl(ifp, command, data) 1140 struct ifnet *ifp; 1141 u_long command; 1142 caddr_t data; 1143 { 1144 struct sk_if_softc *sc_if = ifp->if_softc; 1145 struct ifreq *ifr = (struct ifreq *) data; 1146 int error, mask; 1147 struct mii_data *mii; 1148 1149 error = 0; 1150 switch(command) { 1151 case SIOCSIFMTU: 1152 SK_IF_LOCK(sc_if); 1153 if (ifr->ifr_mtu > SK_JUMBO_MTU) 1154 error = EINVAL; 1155 else { 1156 ifp->if_mtu = ifr->ifr_mtu; 1157 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 1158 sk_init_locked(sc_if); 1159 } 1160 SK_IF_UNLOCK(sc_if); 1161 break; 1162 case SIOCSIFFLAGS: 1163 SK_IF_LOCK(sc_if); 1164 if (ifp->if_flags & IFF_UP) { 1165 if (ifp->if_drv_flags & IFF_DRV_RUNNING) { 1166 if ((ifp->if_flags ^ sc_if->sk_if_flags) 1167 & IFF_PROMISC) { 1168 sk_setpromisc(sc_if); 1169 sk_setmulti(sc_if); 1170 } 1171 } else 1172 sk_init_locked(sc_if); 1173 } else { 1174 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 1175 sk_stop(sc_if); 1176 } 1177 sc_if->sk_if_flags = ifp->if_flags; 1178 SK_IF_UNLOCK(sc_if); 1179 break; 1180 case SIOCADDMULTI: 1181 case SIOCDELMULTI: 1182 SK_IF_LOCK(sc_if); 1183 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 1184 sk_setmulti(sc_if); 1185 SK_IF_UNLOCK(sc_if); 1186 break; 1187 case SIOCGIFMEDIA: 1188 case SIOCSIFMEDIA: 1189 mii = device_get_softc(sc_if->sk_miibus); 1190 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command); 1191 break; 1192 case SIOCSIFCAP: 1193 SK_IF_LOCK(sc_if); 1194 if (sc_if->sk_softc->sk_type == SK_GENESIS) { 1195 SK_IF_UNLOCK(sc_if); 1196 break; 1197 } 1198 mask = ifr->ifr_reqcap ^ ifp->if_capenable; 1199 if (mask & IFCAP_HWCSUM) { 1200 ifp->if_capenable ^= IFCAP_HWCSUM; 1201 if (IFCAP_HWCSUM & ifp->if_capenable && 1202 IFCAP_HWCSUM & ifp->if_capabilities) 1203 ifp->if_hwassist = SK_CSUM_FEATURES; 1204 else 1205 ifp->if_hwassist = 0; 1206 } 1207 SK_IF_UNLOCK(sc_if); 1208 break; 1209 default: 1210 error = ether_ioctl(ifp, command, data); 1211 break; 1212 } 1213 1214 return (error); 1215 } 1216 1217 /* 1218 * Probe for a SysKonnect GEnesis chip. Check the PCI vendor and device 1219 * IDs against our list and return a device name if we find a match. 1220 */ 1221 static int 1222 skc_probe(dev) 1223 device_t dev; 1224 { 1225 struct sk_type *t = sk_devs; 1226 1227 while(t->sk_name != NULL) { 1228 if ((pci_get_vendor(dev) == t->sk_vid) && 1229 (pci_get_device(dev) == t->sk_did)) { 1230 /* 1231 * Only attach to rev. 2 of the Linksys EG1032 adapter. 1232 * Rev. 3 is supported by re(4). 1233 */ 1234 if ((t->sk_vid == VENDORID_LINKSYS) && 1235 (t->sk_did == DEVICEID_LINKSYS_EG1032) && 1236 (pci_get_subdevice(dev) != 1237 SUBDEVICEID_LINKSYS_EG1032_REV2)) { 1238 t++; 1239 continue; 1240 } 1241 device_set_desc(dev, t->sk_name); 1242 return (BUS_PROBE_DEFAULT); 1243 } 1244 t++; 1245 } 1246 1247 return(ENXIO); 1248 } 1249 1250 /* 1251 * Force the GEnesis into reset, then bring it out of reset. 1252 */ 1253 static void 1254 sk_reset(sc) 1255 struct sk_softc *sc; 1256 { 1257 1258 CSR_WRITE_2(sc, SK_CSR, SK_CSR_SW_RESET); 1259 CSR_WRITE_2(sc, SK_CSR, SK_CSR_MASTER_RESET); 1260 if (SK_YUKON_FAMILY(sc->sk_type)) 1261 CSR_WRITE_2(sc, SK_LINK_CTRL, SK_LINK_RESET_SET); 1262 1263 DELAY(1000); 1264 CSR_WRITE_2(sc, SK_CSR, SK_CSR_SW_UNRESET); 1265 DELAY(2); 1266 CSR_WRITE_2(sc, SK_CSR, SK_CSR_MASTER_UNRESET); 1267 if (SK_YUKON_FAMILY(sc->sk_type)) 1268 CSR_WRITE_2(sc, SK_LINK_CTRL, SK_LINK_RESET_CLEAR); 1269 1270 if (sc->sk_type == SK_GENESIS) { 1271 /* Configure packet arbiter */ 1272 sk_win_write_2(sc, SK_PKTARB_CTL, SK_PKTARBCTL_UNRESET); 1273 sk_win_write_2(sc, SK_RXPA1_TINIT, SK_PKTARB_TIMEOUT); 1274 sk_win_write_2(sc, SK_TXPA1_TINIT, SK_PKTARB_TIMEOUT); 1275 sk_win_write_2(sc, SK_RXPA2_TINIT, SK_PKTARB_TIMEOUT); 1276 sk_win_write_2(sc, SK_TXPA2_TINIT, SK_PKTARB_TIMEOUT); 1277 } 1278 1279 /* Enable RAM interface */ 1280 sk_win_write_4(sc, SK_RAMCTL, SK_RAMCTL_UNRESET); 1281 1282 /* 1283 * Configure interrupt moderation. The moderation timer 1284 * defers interrupts specified in the interrupt moderation 1285 * timer mask based on the timeout specified in the interrupt 1286 * moderation timer init register. Each bit in the timer 1287 * register represents one tick, so to specify a timeout in 1288 * microseconds, we have to multiply by the correct number of 1289 * ticks-per-microsecond. 1290 */ 1291 switch (sc->sk_type) { 1292 case SK_GENESIS: 1293 sc->sk_int_ticks = SK_IMTIMER_TICKS_GENESIS; 1294 break; 1295 default: 1296 sc->sk_int_ticks = SK_IMTIMER_TICKS_YUKON; 1297 break; 1298 } 1299 if (bootverbose) 1300 device_printf(sc->sk_dev, "interrupt moderation is %d us\n", 1301 sc->sk_int_mod); 1302 sk_win_write_4(sc, SK_IMTIMERINIT, SK_IM_USECS(sc->sk_int_mod, 1303 sc->sk_int_ticks)); 1304 sk_win_write_4(sc, SK_IMMR, SK_ISR_TX1_S_EOF|SK_ISR_TX2_S_EOF| 1305 SK_ISR_RX1_EOF|SK_ISR_RX2_EOF); 1306 sk_win_write_1(sc, SK_IMTIMERCTL, SK_IMCTL_START); 1307 1308 return; 1309 } 1310 1311 static int 1312 sk_probe(dev) 1313 device_t dev; 1314 { 1315 struct sk_softc *sc; 1316 1317 sc = device_get_softc(device_get_parent(dev)); 1318 1319 /* 1320 * Not much to do here. We always know there will be 1321 * at least one XMAC present, and if there are two, 1322 * skc_attach() will create a second device instance 1323 * for us. 1324 */ 1325 switch (sc->sk_type) { 1326 case SK_GENESIS: 1327 device_set_desc(dev, "XaQti Corp. XMAC II"); 1328 break; 1329 case SK_YUKON: 1330 case SK_YUKON_LITE: 1331 case SK_YUKON_LP: 1332 device_set_desc(dev, "Marvell Semiconductor, Inc. Yukon"); 1333 break; 1334 } 1335 1336 return (BUS_PROBE_DEFAULT); 1337 } 1338 1339 /* 1340 * Each XMAC chip is attached as a separate logical IP interface. 1341 * Single port cards will have only one logical interface of course. 1342 */ 1343 static int 1344 sk_attach(dev) 1345 device_t dev; 1346 { 1347 struct sk_softc *sc; 1348 struct sk_if_softc *sc_if; 1349 struct ifnet *ifp; 1350 int i, port, error; 1351 u_char eaddr[6]; 1352 1353 if (dev == NULL) 1354 return(EINVAL); 1355 1356 error = 0; 1357 sc_if = device_get_softc(dev); 1358 sc = device_get_softc(device_get_parent(dev)); 1359 port = *(int *)device_get_ivars(dev); 1360 1361 sc_if->sk_if_dev = dev; 1362 sc_if->sk_port = port; 1363 sc_if->sk_softc = sc; 1364 sc->sk_if[port] = sc_if; 1365 if (port == SK_PORT_A) 1366 sc_if->sk_tx_bmu = SK_BMU_TXS_CSR0; 1367 if (port == SK_PORT_B) 1368 sc_if->sk_tx_bmu = SK_BMU_TXS_CSR1; 1369 1370 callout_init_mtx(&sc_if->sk_tick_ch, &sc_if->sk_softc->sk_mtx, 0); 1371 1372 if (sk_dma_alloc(sc_if) != 0) { 1373 error = ENOMEM; 1374 goto fail; 1375 } 1376 1377 ifp = sc_if->sk_ifp = if_alloc(IFT_ETHER); 1378 if (ifp == NULL) { 1379 device_printf(sc_if->sk_if_dev, "can not if_alloc()\n"); 1380 error = ENOSPC; 1381 goto fail; 1382 } 1383 ifp->if_softc = sc_if; 1384 if_initname(ifp, device_get_name(dev), device_get_unit(dev)); 1385 ifp->if_mtu = ETHERMTU; 1386 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 1387 /* 1388 * SK_GENESIS has a bug in checksum offload - From linux. 1389 */ 1390 if (sc_if->sk_softc->sk_type != SK_GENESIS) { 1391 ifp->if_capabilities = IFCAP_HWCSUM; 1392 ifp->if_hwassist = SK_CSUM_FEATURES; 1393 } else { 1394 ifp->if_capabilities = 0; 1395 ifp->if_hwassist = 0; 1396 } 1397 ifp->if_capenable = ifp->if_capabilities; 1398 ifp->if_ioctl = sk_ioctl; 1399 ifp->if_start = sk_start; 1400 ifp->if_watchdog = sk_watchdog; 1401 ifp->if_init = sk_init; 1402 IFQ_SET_MAXLEN(&ifp->if_snd, SK_TX_RING_CNT - 1); 1403 ifp->if_snd.ifq_drv_maxlen = SK_TX_RING_CNT - 1; 1404 IFQ_SET_READY(&ifp->if_snd); 1405 1406 /* 1407 * Get station address for this interface. Note that 1408 * dual port cards actually come with three station 1409 * addresses: one for each port, plus an extra. The 1410 * extra one is used by the SysKonnect driver software 1411 * as a 'virtual' station address for when both ports 1412 * are operating in failover mode. Currently we don't 1413 * use this extra address. 1414 */ 1415 SK_IF_LOCK(sc_if); 1416 for (i = 0; i < ETHER_ADDR_LEN; i++) 1417 eaddr[i] = 1418 sk_win_read_1(sc, SK_MAC0_0 + (port * 8) + i); 1419 1420 /* 1421 * Set up RAM buffer addresses. The NIC will have a certain 1422 * amount of SRAM on it, somewhere between 512K and 2MB. We 1423 * need to divide this up a) between the transmitter and 1424 * receiver and b) between the two XMACs, if this is a 1425 * dual port NIC. Our algotithm is to divide up the memory 1426 * evenly so that everyone gets a fair share. 1427 * 1428 * Just to be contrary, Yukon2 appears to have separate memory 1429 * for each MAC. 1430 */ 1431 if (sk_win_read_1(sc, SK_CONFIG) & SK_CONFIG_SINGLEMAC) { 1432 u_int32_t chunk, val; 1433 1434 chunk = sc->sk_ramsize / 2; 1435 val = sc->sk_rboff / sizeof(u_int64_t); 1436 sc_if->sk_rx_ramstart = val; 1437 val += (chunk / sizeof(u_int64_t)); 1438 sc_if->sk_rx_ramend = val - 1; 1439 sc_if->sk_tx_ramstart = val; 1440 val += (chunk / sizeof(u_int64_t)); 1441 sc_if->sk_tx_ramend = val - 1; 1442 } else { 1443 u_int32_t chunk, val; 1444 1445 chunk = sc->sk_ramsize / 4; 1446 val = (sc->sk_rboff + (chunk * 2 * sc_if->sk_port)) / 1447 sizeof(u_int64_t); 1448 sc_if->sk_rx_ramstart = val; 1449 val += (chunk / sizeof(u_int64_t)); 1450 sc_if->sk_rx_ramend = val - 1; 1451 sc_if->sk_tx_ramstart = val; 1452 val += (chunk / sizeof(u_int64_t)); 1453 sc_if->sk_tx_ramend = val - 1; 1454 } 1455 1456 /* Read and save PHY type and set PHY address */ 1457 sc_if->sk_phytype = sk_win_read_1(sc, SK_EPROM1) & 0xF; 1458 if (!SK_YUKON_FAMILY(sc->sk_type)) { 1459 switch(sc_if->sk_phytype) { 1460 case SK_PHYTYPE_XMAC: 1461 sc_if->sk_phyaddr = SK_PHYADDR_XMAC; 1462 break; 1463 case SK_PHYTYPE_BCOM: 1464 sc_if->sk_phyaddr = SK_PHYADDR_BCOM; 1465 break; 1466 default: 1467 device_printf(sc->sk_dev, "unsupported PHY type: %d\n", 1468 sc_if->sk_phytype); 1469 error = ENODEV; 1470 SK_IF_UNLOCK(sc_if); 1471 goto fail; 1472 } 1473 } else { 1474 if (sc_if->sk_phytype < SK_PHYTYPE_MARV_COPPER && 1475 sc->sk_pmd != 'S') { 1476 /* not initialized, punt */ 1477 sc_if->sk_phytype = SK_PHYTYPE_MARV_COPPER; 1478 sc->sk_coppertype = 1; 1479 } 1480 1481 sc_if->sk_phyaddr = SK_PHYADDR_MARV; 1482 1483 if (!(sc->sk_coppertype)) 1484 sc_if->sk_phytype = SK_PHYTYPE_MARV_FIBER; 1485 } 1486 1487 /* 1488 * Call MI attach routine. Can't hold locks when calling into ether_*. 1489 */ 1490 SK_IF_UNLOCK(sc_if); 1491 ether_ifattach(ifp, eaddr); 1492 SK_IF_LOCK(sc_if); 1493 1494 /* 1495 * The hardware should be ready for VLAN_MTU by default: 1496 * XMAC II has 0x8100 in VLAN Tag Level 1 register initially; 1497 * YU_SMR_MFL_VLAN is set by this driver in Yukon. 1498 * 1499 */ 1500 ifp->if_capabilities |= IFCAP_VLAN_MTU; 1501 ifp->if_capenable |= IFCAP_VLAN_MTU; 1502 /* 1503 * Tell the upper layer(s) we support long frames. 1504 * Must appear after the call to ether_ifattach() because 1505 * ether_ifattach() sets ifi_hdrlen to the default value. 1506 */ 1507 ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header); 1508 1509 /* 1510 * Do miibus setup. 1511 */ 1512 switch (sc->sk_type) { 1513 case SK_GENESIS: 1514 sk_init_xmac(sc_if); 1515 break; 1516 case SK_YUKON: 1517 case SK_YUKON_LITE: 1518 case SK_YUKON_LP: 1519 sk_init_yukon(sc_if); 1520 break; 1521 } 1522 1523 SK_IF_UNLOCK(sc_if); 1524 if (mii_phy_probe(dev, &sc_if->sk_miibus, 1525 sk_ifmedia_upd, sk_ifmedia_sts)) { 1526 device_printf(sc_if->sk_if_dev, "no PHY found!\n"); 1527 ether_ifdetach(ifp); 1528 error = ENXIO; 1529 goto fail; 1530 } 1531 1532 fail: 1533 if (error) { 1534 /* Access should be ok even though lock has been dropped */ 1535 sc->sk_if[port] = NULL; 1536 sk_detach(dev); 1537 } 1538 1539 return(error); 1540 } 1541 1542 /* 1543 * Attach the interface. Allocate softc structures, do ifmedia 1544 * setup and ethernet/BPF attach. 1545 */ 1546 static int 1547 skc_attach(dev) 1548 device_t dev; 1549 { 1550 struct sk_softc *sc; 1551 int error = 0, *port; 1552 uint8_t skrs; 1553 const char *pname; 1554 char *revstr; 1555 1556 sc = device_get_softc(dev); 1557 sc->sk_dev = dev; 1558 1559 mtx_init(&sc->sk_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK, 1560 MTX_DEF); 1561 mtx_init(&sc->sk_mii_mtx, "sk_mii_mutex", NULL, MTX_DEF); 1562 /* 1563 * Map control/status registers. 1564 */ 1565 pci_enable_busmaster(dev); 1566 1567 /* Allocate resources */ 1568 #ifdef SK_USEIOSPACE 1569 sc->sk_res_spec = sk_res_spec_io; 1570 #else 1571 sc->sk_res_spec = sk_res_spec_mem; 1572 #endif 1573 error = bus_alloc_resources(dev, sc->sk_res_spec, sc->sk_res); 1574 if (error) { 1575 if (sc->sk_res_spec == sk_res_spec_mem) 1576 sc->sk_res_spec = sk_res_spec_io; 1577 else 1578 sc->sk_res_spec = sk_res_spec_mem; 1579 error = bus_alloc_resources(dev, sc->sk_res_spec, sc->sk_res); 1580 if (error) { 1581 device_printf(dev, "couldn't allocate %s resources\n", 1582 sc->sk_res_spec == sk_res_spec_mem ? "memory" : 1583 "I/O"); 1584 goto fail; 1585 } 1586 } 1587 1588 sc->sk_type = sk_win_read_1(sc, SK_CHIPVER); 1589 sc->sk_rev = (sk_win_read_1(sc, SK_CONFIG) >> 4) & 0xf; 1590 1591 /* Bail out if chip is not recognized. */ 1592 if (sc->sk_type != SK_GENESIS && !SK_YUKON_FAMILY(sc->sk_type)) { 1593 device_printf(dev, "unknown device: chipver=%02x, rev=%x\n", 1594 sc->sk_type, sc->sk_rev); 1595 error = ENXIO; 1596 goto fail; 1597 } 1598 1599 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), 1600 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), 1601 OID_AUTO, "int_mod", CTLTYPE_INT|CTLFLAG_RW, 1602 &sc->sk_int_mod, 0, sysctl_hw_sk_int_mod, "I", 1603 "SK interrupt moderation"); 1604 1605 /* Pull in device tunables. */ 1606 sc->sk_int_mod = SK_IM_DEFAULT; 1607 error = resource_int_value(device_get_name(dev), device_get_unit(dev), 1608 "int_mod", &sc->sk_int_mod); 1609 if (error == 0) { 1610 if (sc->sk_int_mod < SK_IM_MIN || 1611 sc->sk_int_mod > SK_IM_MAX) { 1612 device_printf(dev, "int_mod value out of range; " 1613 "using default: %d\n", SK_IM_DEFAULT); 1614 sc->sk_int_mod = SK_IM_DEFAULT; 1615 } 1616 } 1617 1618 /* Reset the adapter. */ 1619 sk_reset(sc); 1620 1621 skrs = sk_win_read_1(sc, SK_EPROM0); 1622 if (sc->sk_type == SK_GENESIS) { 1623 /* Read and save RAM size and RAMbuffer offset */ 1624 switch(skrs) { 1625 case SK_RAMSIZE_512K_64: 1626 sc->sk_ramsize = 0x80000; 1627 sc->sk_rboff = SK_RBOFF_0; 1628 break; 1629 case SK_RAMSIZE_1024K_64: 1630 sc->sk_ramsize = 0x100000; 1631 sc->sk_rboff = SK_RBOFF_80000; 1632 break; 1633 case SK_RAMSIZE_1024K_128: 1634 sc->sk_ramsize = 0x100000; 1635 sc->sk_rboff = SK_RBOFF_0; 1636 break; 1637 case SK_RAMSIZE_2048K_128: 1638 sc->sk_ramsize = 0x200000; 1639 sc->sk_rboff = SK_RBOFF_0; 1640 break; 1641 default: 1642 device_printf(dev, "unknown ram size: %d\n", skrs); 1643 error = ENXIO; 1644 goto fail; 1645 } 1646 } else { /* SK_YUKON_FAMILY */ 1647 if (skrs == 0x00) 1648 sc->sk_ramsize = 0x20000; 1649 else 1650 sc->sk_ramsize = skrs * (1<<12); 1651 sc->sk_rboff = SK_RBOFF_0; 1652 } 1653 1654 /* Read and save physical media type */ 1655 sc->sk_pmd = sk_win_read_1(sc, SK_PMDTYPE); 1656 1657 if (sc->sk_pmd == 'T' || sc->sk_pmd == '1') 1658 sc->sk_coppertype = 1; 1659 else 1660 sc->sk_coppertype = 0; 1661 1662 /* Determine whether to name it with VPD PN or just make it up. 1663 * Marvell Yukon VPD PN seems to freqently be bogus. */ 1664 switch (pci_get_device(dev)) { 1665 case DEVICEID_SK_V1: 1666 case DEVICEID_BELKIN_5005: 1667 case DEVICEID_3COM_3C940: 1668 case DEVICEID_LINKSYS_EG1032: 1669 case DEVICEID_DLINK_DGE530T_A1: 1670 case DEVICEID_DLINK_DGE530T_B1: 1671 /* Stay with VPD PN. */ 1672 if (pci_get_vpd_ident(dev, &pname)) 1673 goto vpdfailed; 1674 break; 1675 case DEVICEID_SK_V2: 1676 /* YUKON VPD PN might bear no resemblance to reality. */ 1677 switch (sc->sk_type) { 1678 case SK_GENESIS: 1679 /* Stay with VPD PN. */ 1680 if (pci_get_vpd_ident(dev, &pname)) 1681 goto vpdfailed; 1682 break; 1683 case SK_YUKON: 1684 pname = "Marvell Yukon Gigabit Ethernet"; 1685 break; 1686 case SK_YUKON_LITE: 1687 pname = "Marvell Yukon Lite Gigabit Ethernet"; 1688 break; 1689 case SK_YUKON_LP: 1690 pname = "Marvell Yukon LP Gigabit Ethernet"; 1691 break; 1692 default: 1693 pname = "Marvell Yukon (Unknown) Gigabit Ethernet"; 1694 break; 1695 } 1696 1697 /* Yukon Lite Rev. A0 needs special test. */ 1698 if (sc->sk_type == SK_YUKON || sc->sk_type == SK_YUKON_LP) { 1699 u_int32_t far; 1700 u_int8_t testbyte; 1701 1702 /* Save flash address register before testing. */ 1703 far = sk_win_read_4(sc, SK_EP_ADDR); 1704 1705 sk_win_write_1(sc, SK_EP_ADDR+0x03, 0xff); 1706 testbyte = sk_win_read_1(sc, SK_EP_ADDR+0x03); 1707 1708 if (testbyte != 0x00) { 1709 /* Yukon Lite Rev. A0 detected. */ 1710 sc->sk_type = SK_YUKON_LITE; 1711 sc->sk_rev = SK_YUKON_LITE_REV_A0; 1712 /* Restore flash address register. */ 1713 sk_win_write_4(sc, SK_EP_ADDR, far); 1714 } 1715 } 1716 break; 1717 default: 1718 vpdfailed: 1719 device_printf(dev, "unknown device: vendor=%04x, device=%04x, " 1720 "chipver=%02x, rev=%x\n", 1721 pci_get_vendor(dev), pci_get_device(dev), 1722 sc->sk_type, sc->sk_rev); 1723 error = ENXIO; 1724 goto fail; 1725 } 1726 1727 if (sc->sk_type == SK_YUKON_LITE) { 1728 switch (sc->sk_rev) { 1729 case SK_YUKON_LITE_REV_A0: 1730 revstr = "A0"; 1731 break; 1732 case SK_YUKON_LITE_REV_A1: 1733 revstr = "A1"; 1734 break; 1735 case SK_YUKON_LITE_REV_A3: 1736 revstr = "A3"; 1737 break; 1738 default: 1739 revstr = ""; 1740 break; 1741 } 1742 } else { 1743 revstr = ""; 1744 } 1745 1746 /* Announce the product name and more VPD data if there. */ 1747 device_printf(dev, "%s rev. %s(0x%x)\n", 1748 pname != NULL ? pname : "<unknown>", revstr, sc->sk_rev); 1749 1750 if (bootverbose) { 1751 device_printf(dev, "chip ver = 0x%02x\n", sc->sk_type); 1752 device_printf(dev, "chip rev = 0x%02x\n", sc->sk_rev); 1753 device_printf(dev, "SK_EPROM0 = 0x%02x\n", skrs); 1754 device_printf(dev, "SRAM size = 0x%06x\n", sc->sk_ramsize); 1755 } 1756 1757 sc->sk_devs[SK_PORT_A] = device_add_child(dev, "sk", -1); 1758 if (sc->sk_devs[SK_PORT_A] == NULL) { 1759 device_printf(dev, "failed to add child for PORT_A\n"); 1760 error = ENXIO; 1761 goto fail; 1762 } 1763 port = malloc(sizeof(int), M_DEVBUF, M_NOWAIT); 1764 if (port == NULL) { 1765 device_printf(dev, "failed to allocate memory for " 1766 "ivars of PORT_A\n"); 1767 error = ENXIO; 1768 goto fail; 1769 } 1770 *port = SK_PORT_A; 1771 device_set_ivars(sc->sk_devs[SK_PORT_A], port); 1772 1773 if (!(sk_win_read_1(sc, SK_CONFIG) & SK_CONFIG_SINGLEMAC)) { 1774 sc->sk_devs[SK_PORT_B] = device_add_child(dev, "sk", -1); 1775 if (sc->sk_devs[SK_PORT_B] == NULL) { 1776 device_printf(dev, "failed to add child for PORT_B\n"); 1777 error = ENXIO; 1778 goto fail; 1779 } 1780 port = malloc(sizeof(int), M_DEVBUF, M_NOWAIT); 1781 if (port == NULL) { 1782 device_printf(dev, "failed to allocate memory for " 1783 "ivars of PORT_B\n"); 1784 error = ENXIO; 1785 goto fail; 1786 } 1787 *port = SK_PORT_B; 1788 device_set_ivars(sc->sk_devs[SK_PORT_B], port); 1789 } 1790 1791 /* Turn on the 'driver is loaded' LED. */ 1792 CSR_WRITE_2(sc, SK_LED, SK_LED_GREEN_ON); 1793 1794 error = bus_generic_attach(dev); 1795 if (error) { 1796 device_printf(dev, "failed to attach port(s)\n"); 1797 goto fail; 1798 } 1799 1800 /* Hook interrupt last to avoid having to lock softc */ 1801 error = bus_setup_intr(dev, sc->sk_res[1], INTR_TYPE_NET|INTR_MPSAFE, 1802 sk_intr, sc, &sc->sk_intrhand); 1803 1804 if (error) { 1805 device_printf(dev, "couldn't set up irq\n"); 1806 goto fail; 1807 } 1808 1809 fail: 1810 if (error) 1811 skc_detach(dev); 1812 1813 return(error); 1814 } 1815 1816 /* 1817 * Shutdown hardware and free up resources. This can be called any 1818 * time after the mutex has been initialized. It is called in both 1819 * the error case in attach and the normal detach case so it needs 1820 * to be careful about only freeing resources that have actually been 1821 * allocated. 1822 */ 1823 static int 1824 sk_detach(dev) 1825 device_t dev; 1826 { 1827 struct sk_if_softc *sc_if; 1828 struct ifnet *ifp; 1829 1830 sc_if = device_get_softc(dev); 1831 KASSERT(mtx_initialized(&sc_if->sk_softc->sk_mtx), 1832 ("sk mutex not initialized in sk_detach")); 1833 SK_IF_LOCK(sc_if); 1834 1835 ifp = sc_if->sk_ifp; 1836 /* These should only be active if attach_xmac succeeded */ 1837 if (device_is_attached(dev)) { 1838 sk_stop(sc_if); 1839 /* Can't hold locks while calling detach */ 1840 SK_IF_UNLOCK(sc_if); 1841 callout_drain(&sc_if->sk_tick_ch); 1842 ether_ifdetach(ifp); 1843 SK_IF_LOCK(sc_if); 1844 } 1845 if (ifp) 1846 if_free(ifp); 1847 /* 1848 * We're generally called from skc_detach() which is using 1849 * device_delete_child() to get to here. It's already trashed 1850 * miibus for us, so don't do it here or we'll panic. 1851 */ 1852 /* 1853 if (sc_if->sk_miibus != NULL) 1854 device_delete_child(dev, sc_if->sk_miibus); 1855 */ 1856 bus_generic_detach(dev); 1857 sk_dma_free(sc_if); 1858 SK_IF_UNLOCK(sc_if); 1859 1860 return(0); 1861 } 1862 1863 static int 1864 skc_detach(dev) 1865 device_t dev; 1866 { 1867 struct sk_softc *sc; 1868 1869 sc = device_get_softc(dev); 1870 KASSERT(mtx_initialized(&sc->sk_mtx), ("sk mutex not initialized")); 1871 1872 if (device_is_alive(dev)) { 1873 if (sc->sk_devs[SK_PORT_A] != NULL) { 1874 free(device_get_ivars(sc->sk_devs[SK_PORT_A]), M_DEVBUF); 1875 device_delete_child(dev, sc->sk_devs[SK_PORT_A]); 1876 } 1877 if (sc->sk_devs[SK_PORT_B] != NULL) { 1878 free(device_get_ivars(sc->sk_devs[SK_PORT_B]), M_DEVBUF); 1879 device_delete_child(dev, sc->sk_devs[SK_PORT_B]); 1880 } 1881 bus_generic_detach(dev); 1882 } 1883 1884 if (sc->sk_intrhand) 1885 bus_teardown_intr(dev, sc->sk_res[1], sc->sk_intrhand); 1886 bus_release_resources(dev, sc->sk_res_spec, sc->sk_res); 1887 1888 mtx_destroy(&sc->sk_mii_mtx); 1889 mtx_destroy(&sc->sk_mtx); 1890 1891 return(0); 1892 } 1893 1894 struct sk_dmamap_arg { 1895 bus_addr_t sk_busaddr; 1896 }; 1897 1898 static void 1899 sk_dmamap_cb(arg, segs, nseg, error) 1900 void *arg; 1901 bus_dma_segment_t *segs; 1902 int nseg; 1903 int error; 1904 { 1905 struct sk_dmamap_arg *ctx; 1906 1907 if (error != 0) 1908 return; 1909 1910 ctx = arg; 1911 ctx->sk_busaddr = segs[0].ds_addr; 1912 } 1913 1914 /* 1915 * Allocate jumbo buffer storage. The SysKonnect adapters support 1916 * "jumbograms" (9K frames), although SysKonnect doesn't currently 1917 * use them in their drivers. In order for us to use them, we need 1918 * large 9K receive buffers, however standard mbuf clusters are only 1919 * 2048 bytes in size. Consequently, we need to allocate and manage 1920 * our own jumbo buffer pool. Fortunately, this does not require an 1921 * excessive amount of additional code. 1922 */ 1923 static int 1924 sk_dma_alloc(sc_if) 1925 struct sk_if_softc *sc_if; 1926 { 1927 struct sk_dmamap_arg ctx; 1928 struct sk_txdesc *txd; 1929 struct sk_rxdesc *rxd; 1930 struct sk_rxdesc *jrxd; 1931 u_int8_t *ptr; 1932 struct sk_jpool_entry *entry; 1933 int error, i; 1934 1935 mtx_init(&sc_if->sk_jlist_mtx, "sk_jlist_mtx", NULL, MTX_DEF); 1936 SLIST_INIT(&sc_if->sk_jfree_listhead); 1937 SLIST_INIT(&sc_if->sk_jinuse_listhead); 1938 1939 /* create parent tag */ 1940 /* 1941 * XXX 1942 * This driver should use BUS_SPACE_MAXADDR for lowaddr argument 1943 * in bus_dma_tag_create(9) as the NIC would support DAC mode. 1944 * However bz@ reported that it does not work on amd64 with > 4GB 1945 * RAM. Until we have more clues of the breakage, disable DAC mode 1946 * by limiting DMA address to be in 32bit address space. 1947 */ 1948 error = bus_dma_tag_create(NULL, /* parent */ 1949 1, 0, /* algnmnt, boundary */ 1950 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */ 1951 BUS_SPACE_MAXADDR, /* highaddr */ 1952 NULL, NULL, /* filter, filterarg */ 1953 BUS_SPACE_MAXSIZE_32BIT, /* maxsize */ 1954 0, /* nsegments */ 1955 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */ 1956 0, /* flags */ 1957 NULL, NULL, /* lockfunc, lockarg */ 1958 &sc_if->sk_cdata.sk_parent_tag); 1959 if (error != 0) { 1960 device_printf(sc_if->sk_if_dev, 1961 "failed to create parent DMA tag\n"); 1962 goto fail; 1963 } 1964 /* create tag for Tx ring */ 1965 error = bus_dma_tag_create(sc_if->sk_cdata.sk_parent_tag,/* parent */ 1966 SK_RING_ALIGN, 0, /* algnmnt, boundary */ 1967 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */ 1968 BUS_SPACE_MAXADDR, /* highaddr */ 1969 NULL, NULL, /* filter, filterarg */ 1970 SK_TX_RING_SZ, /* maxsize */ 1971 1, /* nsegments */ 1972 SK_TX_RING_SZ, /* maxsegsize */ 1973 0, /* flags */ 1974 NULL, NULL, /* lockfunc, lockarg */ 1975 &sc_if->sk_cdata.sk_tx_ring_tag); 1976 if (error != 0) { 1977 device_printf(sc_if->sk_if_dev, 1978 "failed to allocate Tx ring DMA tag\n"); 1979 goto fail; 1980 } 1981 1982 /* create tag for Rx ring */ 1983 error = bus_dma_tag_create(sc_if->sk_cdata.sk_parent_tag,/* parent */ 1984 SK_RING_ALIGN, 0, /* algnmnt, boundary */ 1985 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */ 1986 BUS_SPACE_MAXADDR, /* highaddr */ 1987 NULL, NULL, /* filter, filterarg */ 1988 SK_RX_RING_SZ, /* maxsize */ 1989 1, /* nsegments */ 1990 SK_RX_RING_SZ, /* maxsegsize */ 1991 0, /* flags */ 1992 NULL, NULL, /* lockfunc, lockarg */ 1993 &sc_if->sk_cdata.sk_rx_ring_tag); 1994 if (error != 0) { 1995 device_printf(sc_if->sk_if_dev, 1996 "failed to allocate Rx ring DMA tag\n"); 1997 goto fail; 1998 } 1999 2000 /* create tag for jumbo Rx ring */ 2001 error = bus_dma_tag_create(sc_if->sk_cdata.sk_parent_tag,/* parent */ 2002 SK_RING_ALIGN, 0, /* algnmnt, boundary */ 2003 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */ 2004 BUS_SPACE_MAXADDR, /* highaddr */ 2005 NULL, NULL, /* filter, filterarg */ 2006 SK_JUMBO_RX_RING_SZ, /* maxsize */ 2007 1, /* nsegments */ 2008 SK_JUMBO_RX_RING_SZ, /* maxsegsize */ 2009 0, /* flags */ 2010 NULL, NULL, /* lockfunc, lockarg */ 2011 &sc_if->sk_cdata.sk_jumbo_rx_ring_tag); 2012 if (error != 0) { 2013 device_printf(sc_if->sk_if_dev, 2014 "failed to allocate jumbo Rx ring DMA tag\n"); 2015 goto fail; 2016 } 2017 2018 /* create tag for jumbo buffer blocks */ 2019 error = bus_dma_tag_create(sc_if->sk_cdata.sk_parent_tag,/* parent */ 2020 PAGE_SIZE, 0, /* algnmnt, boundary */ 2021 BUS_SPACE_MAXADDR, /* lowaddr */ 2022 BUS_SPACE_MAXADDR, /* highaddr */ 2023 NULL, NULL, /* filter, filterarg */ 2024 SK_JMEM, /* maxsize */ 2025 1, /* nsegments */ 2026 SK_JMEM, /* maxsegsize */ 2027 0, /* flags */ 2028 NULL, NULL, /* lockfunc, lockarg */ 2029 &sc_if->sk_cdata.sk_jumbo_tag); 2030 if (error != 0) { 2031 device_printf(sc_if->sk_if_dev, 2032 "failed to allocate jumbo Rx buffer block DMA tag\n"); 2033 goto fail; 2034 } 2035 2036 /* create tag for Tx buffers */ 2037 error = bus_dma_tag_create(sc_if->sk_cdata.sk_parent_tag,/* parent */ 2038 1, 0, /* algnmnt, boundary */ 2039 BUS_SPACE_MAXADDR, /* lowaddr */ 2040 BUS_SPACE_MAXADDR, /* highaddr */ 2041 NULL, NULL, /* filter, filterarg */ 2042 MCLBYTES * SK_MAXTXSEGS, /* maxsize */ 2043 SK_MAXTXSEGS, /* nsegments */ 2044 MCLBYTES, /* maxsegsize */ 2045 0, /* flags */ 2046 NULL, NULL, /* lockfunc, lockarg */ 2047 &sc_if->sk_cdata.sk_tx_tag); 2048 if (error != 0) { 2049 device_printf(sc_if->sk_if_dev, 2050 "failed to allocate Tx DMA tag\n"); 2051 goto fail; 2052 } 2053 2054 /* create tag for Rx buffers */ 2055 error = bus_dma_tag_create(sc_if->sk_cdata.sk_parent_tag,/* parent */ 2056 1, 0, /* algnmnt, boundary */ 2057 BUS_SPACE_MAXADDR, /* lowaddr */ 2058 BUS_SPACE_MAXADDR, /* highaddr */ 2059 NULL, NULL, /* filter, filterarg */ 2060 MCLBYTES, /* maxsize */ 2061 1, /* nsegments */ 2062 MCLBYTES, /* maxsegsize */ 2063 0, /* flags */ 2064 NULL, NULL, /* lockfunc, lockarg */ 2065 &sc_if->sk_cdata.sk_rx_tag); 2066 if (error != 0) { 2067 device_printf(sc_if->sk_if_dev, 2068 "failed to allocate Rx DMA tag\n"); 2069 goto fail; 2070 } 2071 2072 /* create tag for jumbo Rx buffers */ 2073 error = bus_dma_tag_create(sc_if->sk_cdata.sk_parent_tag,/* parent */ 2074 PAGE_SIZE, 0, /* algnmnt, boundary */ 2075 BUS_SPACE_MAXADDR, /* lowaddr */ 2076 BUS_SPACE_MAXADDR, /* highaddr */ 2077 NULL, NULL, /* filter, filterarg */ 2078 MCLBYTES * SK_MAXRXSEGS, /* maxsize */ 2079 SK_MAXRXSEGS, /* nsegments */ 2080 SK_JLEN, /* maxsegsize */ 2081 0, /* flags */ 2082 NULL, NULL, /* lockfunc, lockarg */ 2083 &sc_if->sk_cdata.sk_jumbo_rx_tag); 2084 if (error != 0) { 2085 device_printf(sc_if->sk_if_dev, 2086 "failed to allocate jumbo Rx DMA tag\n"); 2087 goto fail; 2088 } 2089 2090 /* allocate DMA'able memory and load the DMA map for Tx ring */ 2091 error = bus_dmamem_alloc(sc_if->sk_cdata.sk_tx_ring_tag, 2092 (void **)&sc_if->sk_rdata.sk_tx_ring, BUS_DMA_NOWAIT | BUS_DMA_ZERO, 2093 &sc_if->sk_cdata.sk_tx_ring_map); 2094 if (error != 0) { 2095 device_printf(sc_if->sk_if_dev, 2096 "failed to allocate DMA'able memory for Tx ring\n"); 2097 goto fail; 2098 } 2099 2100 ctx.sk_busaddr = 0; 2101 error = bus_dmamap_load(sc_if->sk_cdata.sk_tx_ring_tag, 2102 sc_if->sk_cdata.sk_tx_ring_map, sc_if->sk_rdata.sk_tx_ring, 2103 SK_TX_RING_SZ, sk_dmamap_cb, &ctx, BUS_DMA_NOWAIT); 2104 if (error != 0) { 2105 device_printf(sc_if->sk_if_dev, 2106 "failed to load DMA'able memory for Tx ring\n"); 2107 goto fail; 2108 } 2109 sc_if->sk_rdata.sk_tx_ring_paddr = ctx.sk_busaddr; 2110 2111 /* allocate DMA'able memory and load the DMA map for Rx ring */ 2112 error = bus_dmamem_alloc(sc_if->sk_cdata.sk_rx_ring_tag, 2113 (void **)&sc_if->sk_rdata.sk_rx_ring, BUS_DMA_NOWAIT | BUS_DMA_ZERO, 2114 &sc_if->sk_cdata.sk_rx_ring_map); 2115 if (error != 0) { 2116 device_printf(sc_if->sk_if_dev, 2117 "failed to allocate DMA'able memory for Rx ring\n"); 2118 goto fail; 2119 } 2120 2121 ctx.sk_busaddr = 0; 2122 error = bus_dmamap_load(sc_if->sk_cdata.sk_rx_ring_tag, 2123 sc_if->sk_cdata.sk_rx_ring_map, sc_if->sk_rdata.sk_rx_ring, 2124 SK_RX_RING_SZ, sk_dmamap_cb, &ctx, BUS_DMA_NOWAIT); 2125 if (error != 0) { 2126 device_printf(sc_if->sk_if_dev, 2127 "failed to load DMA'able memory for Rx ring\n"); 2128 goto fail; 2129 } 2130 sc_if->sk_rdata.sk_rx_ring_paddr = ctx.sk_busaddr; 2131 2132 /* allocate DMA'able memory and load the DMA map for jumbo Rx ring */ 2133 error = bus_dmamem_alloc(sc_if->sk_cdata.sk_jumbo_rx_ring_tag, 2134 (void **)&sc_if->sk_rdata.sk_jumbo_rx_ring, 2135 BUS_DMA_NOWAIT|BUS_DMA_ZERO, &sc_if->sk_cdata.sk_jumbo_rx_ring_map); 2136 if (error != 0) { 2137 device_printf(sc_if->sk_if_dev, 2138 "failed to allocate DMA'able memory for jumbo Rx ring\n"); 2139 goto fail; 2140 } 2141 2142 ctx.sk_busaddr = 0; 2143 error = bus_dmamap_load(sc_if->sk_cdata.sk_jumbo_rx_ring_tag, 2144 sc_if->sk_cdata.sk_jumbo_rx_ring_map, 2145 sc_if->sk_rdata.sk_jumbo_rx_ring, SK_JUMBO_RX_RING_SZ, sk_dmamap_cb, 2146 &ctx, BUS_DMA_NOWAIT); 2147 if (error != 0) { 2148 device_printf(sc_if->sk_if_dev, 2149 "failed to load DMA'able memory for jumbo Rx ring\n"); 2150 goto fail; 2151 } 2152 sc_if->sk_rdata.sk_jumbo_rx_ring_paddr = ctx.sk_busaddr; 2153 2154 /* create DMA maps for Tx buffers */ 2155 for (i = 0; i < SK_TX_RING_CNT; i++) { 2156 txd = &sc_if->sk_cdata.sk_txdesc[i]; 2157 txd->tx_m = NULL; 2158 txd->tx_dmamap = 0; 2159 error = bus_dmamap_create(sc_if->sk_cdata.sk_tx_tag, 0, 2160 &txd->tx_dmamap); 2161 if (error != 0) { 2162 device_printf(sc_if->sk_if_dev, 2163 "failed to create Tx dmamap\n"); 2164 goto fail; 2165 } 2166 } 2167 /* create DMA maps for Rx buffers */ 2168 if ((error = bus_dmamap_create(sc_if->sk_cdata.sk_rx_tag, 0, 2169 &sc_if->sk_cdata.sk_rx_sparemap)) != 0) { 2170 device_printf(sc_if->sk_if_dev, 2171 "failed to create spare Rx dmamap\n"); 2172 goto fail; 2173 } 2174 for (i = 0; i < SK_RX_RING_CNT; i++) { 2175 rxd = &sc_if->sk_cdata.sk_rxdesc[i]; 2176 rxd->rx_m = NULL; 2177 rxd->rx_dmamap = 0; 2178 error = bus_dmamap_create(sc_if->sk_cdata.sk_rx_tag, 0, 2179 &rxd->rx_dmamap); 2180 if (error != 0) { 2181 device_printf(sc_if->sk_if_dev, 2182 "failed to create Rx dmamap\n"); 2183 goto fail; 2184 } 2185 } 2186 /* create DMA maps for jumbo Rx buffers */ 2187 if ((error = bus_dmamap_create(sc_if->sk_cdata.sk_jumbo_rx_tag, 0, 2188 &sc_if->sk_cdata.sk_jumbo_rx_sparemap)) != 0) { 2189 device_printf(sc_if->sk_if_dev, 2190 "failed to create spare jumbo Rx dmamap\n"); 2191 goto fail; 2192 } 2193 for (i = 0; i < SK_JUMBO_RX_RING_CNT; i++) { 2194 jrxd = &sc_if->sk_cdata.sk_jumbo_rxdesc[i]; 2195 jrxd->rx_m = NULL; 2196 jrxd->rx_dmamap = 0; 2197 error = bus_dmamap_create(sc_if->sk_cdata.sk_jumbo_rx_tag, 0, 2198 &jrxd->rx_dmamap); 2199 if (error != 0) { 2200 device_printf(sc_if->sk_if_dev, 2201 "failed to create jumbo Rx dmamap\n"); 2202 goto fail; 2203 } 2204 } 2205 2206 /* allocate DMA'able memory and load the DMA map for jumbo buf */ 2207 error = bus_dmamem_alloc(sc_if->sk_cdata.sk_jumbo_tag, 2208 (void **)&sc_if->sk_rdata.sk_jumbo_buf, 2209 BUS_DMA_NOWAIT|BUS_DMA_ZERO, &sc_if->sk_cdata.sk_jumbo_map); 2210 if (error != 0) { 2211 device_printf(sc_if->sk_if_dev, 2212 "failed to allocate DMA'able memory for jumbo buf\n"); 2213 goto fail; 2214 } 2215 2216 ctx.sk_busaddr = 0; 2217 error = bus_dmamap_load(sc_if->sk_cdata.sk_jumbo_tag, 2218 sc_if->sk_cdata.sk_jumbo_map, 2219 sc_if->sk_rdata.sk_jumbo_buf, SK_JMEM, sk_dmamap_cb, 2220 &ctx, BUS_DMA_NOWAIT); 2221 if (error != 0) { 2222 device_printf(sc_if->sk_if_dev, 2223 "failed to load DMA'able memory for jumbobuf\n"); 2224 goto fail; 2225 } 2226 sc_if->sk_rdata.sk_jumbo_buf_paddr = ctx.sk_busaddr; 2227 2228 /* 2229 * Now divide it up into 9K pieces and save the addresses 2230 * in an array. 2231 */ 2232 ptr = sc_if->sk_rdata.sk_jumbo_buf; 2233 for (i = 0; i < SK_JSLOTS; i++) { 2234 sc_if->sk_cdata.sk_jslots[i] = ptr; 2235 ptr += SK_JLEN; 2236 entry = malloc(sizeof(struct sk_jpool_entry), 2237 M_DEVBUF, M_NOWAIT); 2238 if (entry == NULL) { 2239 device_printf(sc_if->sk_if_dev, 2240 "no memory for jumbo buffers!\n"); 2241 error = ENOMEM; 2242 goto fail; 2243 } 2244 entry->slot = i; 2245 SLIST_INSERT_HEAD(&sc_if->sk_jfree_listhead, entry, 2246 jpool_entries); 2247 } 2248 2249 fail: 2250 return (error); 2251 } 2252 2253 static void 2254 sk_dma_free(sc_if) 2255 struct sk_if_softc *sc_if; 2256 { 2257 struct sk_txdesc *txd; 2258 struct sk_rxdesc *rxd; 2259 struct sk_rxdesc *jrxd; 2260 struct sk_jpool_entry *entry; 2261 int i; 2262 2263 SK_JLIST_LOCK(sc_if); 2264 while ((entry = SLIST_FIRST(&sc_if->sk_jinuse_listhead))) { 2265 device_printf(sc_if->sk_if_dev, 2266 "asked to free buffer that is in use!\n"); 2267 SLIST_REMOVE_HEAD(&sc_if->sk_jinuse_listhead, jpool_entries); 2268 SLIST_INSERT_HEAD(&sc_if->sk_jfree_listhead, entry, 2269 jpool_entries); 2270 } 2271 2272 while (!SLIST_EMPTY(&sc_if->sk_jfree_listhead)) { 2273 entry = SLIST_FIRST(&sc_if->sk_jfree_listhead); 2274 SLIST_REMOVE_HEAD(&sc_if->sk_jfree_listhead, jpool_entries); 2275 free(entry, M_DEVBUF); 2276 } 2277 SK_JLIST_UNLOCK(sc_if); 2278 2279 /* destroy jumbo buffer block */ 2280 if (sc_if->sk_cdata.sk_jumbo_map) 2281 bus_dmamap_unload(sc_if->sk_cdata.sk_jumbo_tag, 2282 sc_if->sk_cdata.sk_jumbo_map); 2283 2284 if (sc_if->sk_rdata.sk_jumbo_buf) { 2285 bus_dmamem_free(sc_if->sk_cdata.sk_jumbo_tag, 2286 sc_if->sk_rdata.sk_jumbo_buf, 2287 sc_if->sk_cdata.sk_jumbo_map); 2288 sc_if->sk_rdata.sk_jumbo_buf = NULL; 2289 sc_if->sk_cdata.sk_jumbo_map = 0; 2290 } 2291 2292 /* Tx ring */ 2293 if (sc_if->sk_cdata.sk_tx_ring_tag) { 2294 if (sc_if->sk_cdata.sk_tx_ring_map) 2295 bus_dmamap_unload(sc_if->sk_cdata.sk_tx_ring_tag, 2296 sc_if->sk_cdata.sk_tx_ring_map); 2297 if (sc_if->sk_cdata.sk_tx_ring_map && 2298 sc_if->sk_rdata.sk_tx_ring) 2299 bus_dmamem_free(sc_if->sk_cdata.sk_tx_ring_tag, 2300 sc_if->sk_rdata.sk_tx_ring, 2301 sc_if->sk_cdata.sk_tx_ring_map); 2302 sc_if->sk_rdata.sk_tx_ring = NULL; 2303 sc_if->sk_cdata.sk_tx_ring_map = 0; 2304 bus_dma_tag_destroy(sc_if->sk_cdata.sk_tx_ring_tag); 2305 sc_if->sk_cdata.sk_tx_ring_tag = NULL; 2306 } 2307 /* Rx ring */ 2308 if (sc_if->sk_cdata.sk_rx_ring_tag) { 2309 if (sc_if->sk_cdata.sk_rx_ring_map) 2310 bus_dmamap_unload(sc_if->sk_cdata.sk_rx_ring_tag, 2311 sc_if->sk_cdata.sk_rx_ring_map); 2312 if (sc_if->sk_cdata.sk_rx_ring_map && 2313 sc_if->sk_rdata.sk_rx_ring) 2314 bus_dmamem_free(sc_if->sk_cdata.sk_rx_ring_tag, 2315 sc_if->sk_rdata.sk_rx_ring, 2316 sc_if->sk_cdata.sk_rx_ring_map); 2317 sc_if->sk_rdata.sk_rx_ring = NULL; 2318 sc_if->sk_cdata.sk_rx_ring_map = 0; 2319 bus_dma_tag_destroy(sc_if->sk_cdata.sk_rx_ring_tag); 2320 sc_if->sk_cdata.sk_rx_ring_tag = NULL; 2321 } 2322 /* jumbo Rx ring */ 2323 if (sc_if->sk_cdata.sk_jumbo_rx_ring_tag) { 2324 if (sc_if->sk_cdata.sk_jumbo_rx_ring_map) 2325 bus_dmamap_unload(sc_if->sk_cdata.sk_jumbo_rx_ring_tag, 2326 sc_if->sk_cdata.sk_jumbo_rx_ring_map); 2327 if (sc_if->sk_cdata.sk_jumbo_rx_ring_map && 2328 sc_if->sk_rdata.sk_jumbo_rx_ring) 2329 bus_dmamem_free(sc_if->sk_cdata.sk_jumbo_rx_ring_tag, 2330 sc_if->sk_rdata.sk_jumbo_rx_ring, 2331 sc_if->sk_cdata.sk_jumbo_rx_ring_map); 2332 sc_if->sk_rdata.sk_jumbo_rx_ring = NULL; 2333 sc_if->sk_cdata.sk_jumbo_rx_ring_map = 0; 2334 bus_dma_tag_destroy(sc_if->sk_cdata.sk_jumbo_rx_ring_tag); 2335 sc_if->sk_cdata.sk_jumbo_rx_ring_tag = NULL; 2336 } 2337 /* Tx buffers */ 2338 if (sc_if->sk_cdata.sk_tx_tag) { 2339 for (i = 0; i < SK_TX_RING_CNT; i++) { 2340 txd = &sc_if->sk_cdata.sk_txdesc[i]; 2341 if (txd->tx_dmamap) { 2342 bus_dmamap_destroy(sc_if->sk_cdata.sk_tx_tag, 2343 txd->tx_dmamap); 2344 txd->tx_dmamap = 0; 2345 } 2346 } 2347 bus_dma_tag_destroy(sc_if->sk_cdata.sk_tx_tag); 2348 sc_if->sk_cdata.sk_tx_tag = NULL; 2349 } 2350 /* Rx buffers */ 2351 if (sc_if->sk_cdata.sk_rx_tag) { 2352 for (i = 0; i < SK_RX_RING_CNT; i++) { 2353 rxd = &sc_if->sk_cdata.sk_rxdesc[i]; 2354 if (rxd->rx_dmamap) { 2355 bus_dmamap_destroy(sc_if->sk_cdata.sk_rx_tag, 2356 rxd->rx_dmamap); 2357 rxd->rx_dmamap = 0; 2358 } 2359 } 2360 if (sc_if->sk_cdata.sk_rx_sparemap) { 2361 bus_dmamap_destroy(sc_if->sk_cdata.sk_rx_tag, 2362 sc_if->sk_cdata.sk_rx_sparemap); 2363 sc_if->sk_cdata.sk_rx_sparemap = 0; 2364 } 2365 bus_dma_tag_destroy(sc_if->sk_cdata.sk_rx_tag); 2366 sc_if->sk_cdata.sk_rx_tag = NULL; 2367 } 2368 /* jumbo Rx buffers */ 2369 if (sc_if->sk_cdata.sk_jumbo_rx_tag) { 2370 for (i = 0; i < SK_JUMBO_RX_RING_CNT; i++) { 2371 jrxd = &sc_if->sk_cdata.sk_jumbo_rxdesc[i]; 2372 if (jrxd->rx_dmamap) { 2373 bus_dmamap_destroy( 2374 sc_if->sk_cdata.sk_jumbo_rx_tag, 2375 jrxd->rx_dmamap); 2376 jrxd->rx_dmamap = 0; 2377 } 2378 } 2379 if (sc_if->sk_cdata.sk_jumbo_rx_sparemap) { 2380 bus_dmamap_destroy(sc_if->sk_cdata.sk_jumbo_rx_tag, 2381 sc_if->sk_cdata.sk_jumbo_rx_sparemap); 2382 sc_if->sk_cdata.sk_jumbo_rx_sparemap = 0; 2383 } 2384 bus_dma_tag_destroy(sc_if->sk_cdata.sk_jumbo_rx_tag); 2385 sc_if->sk_cdata.sk_jumbo_rx_tag = NULL; 2386 } 2387 2388 if (sc_if->sk_cdata.sk_parent_tag) { 2389 bus_dma_tag_destroy(sc_if->sk_cdata.sk_parent_tag); 2390 sc_if->sk_cdata.sk_parent_tag = NULL; 2391 } 2392 mtx_destroy(&sc_if->sk_jlist_mtx); 2393 } 2394 2395 /* 2396 * Allocate a jumbo buffer. 2397 */ 2398 static void * 2399 sk_jalloc(sc_if) 2400 struct sk_if_softc *sc_if; 2401 { 2402 struct sk_jpool_entry *entry; 2403 2404 SK_JLIST_LOCK(sc_if); 2405 2406 entry = SLIST_FIRST(&sc_if->sk_jfree_listhead); 2407 2408 if (entry == NULL) { 2409 SK_JLIST_UNLOCK(sc_if); 2410 return (NULL); 2411 } 2412 2413 SLIST_REMOVE_HEAD(&sc_if->sk_jfree_listhead, jpool_entries); 2414 SLIST_INSERT_HEAD(&sc_if->sk_jinuse_listhead, entry, jpool_entries); 2415 2416 SK_JLIST_UNLOCK(sc_if); 2417 2418 return (sc_if->sk_cdata.sk_jslots[entry->slot]); 2419 } 2420 2421 /* 2422 * Release a jumbo buffer. 2423 */ 2424 static void 2425 sk_jfree(buf, args) 2426 void *buf; 2427 void *args; 2428 { 2429 struct sk_if_softc *sc_if; 2430 struct sk_jpool_entry *entry; 2431 int i; 2432 2433 /* Extract the softc struct pointer. */ 2434 sc_if = (struct sk_if_softc *)args; 2435 KASSERT(sc_if != NULL, ("%s: can't find softc pointer!", __func__)); 2436 2437 SK_JLIST_LOCK(sc_if); 2438 /* calculate the slot this buffer belongs to */ 2439 i = ((vm_offset_t)buf 2440 - (vm_offset_t)sc_if->sk_rdata.sk_jumbo_buf) / SK_JLEN; 2441 KASSERT(i >= 0 && i < SK_JSLOTS, 2442 ("%s: asked to free buffer that we don't manage!", __func__)); 2443 2444 entry = SLIST_FIRST(&sc_if->sk_jinuse_listhead); 2445 KASSERT(entry != NULL, ("%s: buffer not in use!", __func__)); 2446 entry->slot = i; 2447 SLIST_REMOVE_HEAD(&sc_if->sk_jinuse_listhead, jpool_entries); 2448 SLIST_INSERT_HEAD(&sc_if->sk_jfree_listhead, entry, jpool_entries); 2449 if (SLIST_EMPTY(&sc_if->sk_jinuse_listhead)) 2450 wakeup(sc_if); 2451 2452 SK_JLIST_UNLOCK(sc_if); 2453 } 2454 2455 static void 2456 sk_txcksum(ifp, m, f) 2457 struct ifnet *ifp; 2458 struct mbuf *m; 2459 struct sk_tx_desc *f; 2460 { 2461 struct ip *ip; 2462 u_int16_t offset; 2463 u_int8_t *p; 2464 2465 offset = sizeof(struct ip) + ETHER_HDR_LEN; 2466 for(; m && m->m_len == 0; m = m->m_next) 2467 ; 2468 if (m == NULL || m->m_len < ETHER_HDR_LEN) { 2469 if_printf(ifp, "%s: m_len < ETHER_HDR_LEN\n", __func__); 2470 /* checksum may be corrupted */ 2471 goto sendit; 2472 } 2473 if (m->m_len < ETHER_HDR_LEN + sizeof(u_int32_t)) { 2474 if (m->m_len != ETHER_HDR_LEN) { 2475 if_printf(ifp, "%s: m_len != ETHER_HDR_LEN\n", 2476 __func__); 2477 /* checksum may be corrupted */ 2478 goto sendit; 2479 } 2480 for(m = m->m_next; m && m->m_len == 0; m = m->m_next) 2481 ; 2482 if (m == NULL) { 2483 offset = sizeof(struct ip) + ETHER_HDR_LEN; 2484 /* checksum may be corrupted */ 2485 goto sendit; 2486 } 2487 ip = mtod(m, struct ip *); 2488 } else { 2489 p = mtod(m, u_int8_t *); 2490 p += ETHER_HDR_LEN; 2491 ip = (struct ip *)p; 2492 } 2493 offset = (ip->ip_hl << 2) + ETHER_HDR_LEN; 2494 2495 sendit: 2496 f->sk_csum_startval = 0; 2497 f->sk_csum_start = htole32(((offset + m->m_pkthdr.csum_data) & 0xffff) | 2498 (offset << 16)); 2499 } 2500 2501 static int 2502 sk_encap(sc_if, m_head) 2503 struct sk_if_softc *sc_if; 2504 struct mbuf **m_head; 2505 { 2506 struct sk_txdesc *txd; 2507 struct sk_tx_desc *f = NULL; 2508 struct mbuf *m; 2509 bus_dma_segment_t txsegs[SK_MAXTXSEGS]; 2510 u_int32_t cflags, frag, si, sk_ctl; 2511 int error, i, nseg; 2512 2513 SK_IF_LOCK_ASSERT(sc_if); 2514 2515 if ((txd = STAILQ_FIRST(&sc_if->sk_cdata.sk_txfreeq)) == NULL) 2516 return (ENOBUFS); 2517 2518 error = bus_dmamap_load_mbuf_sg(sc_if->sk_cdata.sk_tx_tag, 2519 txd->tx_dmamap, *m_head, txsegs, &nseg, 0); 2520 if (error == EFBIG) { 2521 m = m_defrag(*m_head, M_DONTWAIT); 2522 if (m == NULL) { 2523 m_freem(*m_head); 2524 *m_head = NULL; 2525 return (ENOMEM); 2526 } 2527 *m_head = m; 2528 error = bus_dmamap_load_mbuf_sg(sc_if->sk_cdata.sk_tx_tag, 2529 txd->tx_dmamap, *m_head, txsegs, &nseg, 0); 2530 if (error != 0) { 2531 m_freem(*m_head); 2532 *m_head = NULL; 2533 return (error); 2534 } 2535 } else if (error != 0) 2536 return (error); 2537 if (nseg == 0) { 2538 m_freem(*m_head); 2539 *m_head = NULL; 2540 return (EIO); 2541 } 2542 if (sc_if->sk_cdata.sk_tx_cnt + nseg >= SK_TX_RING_CNT) { 2543 bus_dmamap_unload(sc_if->sk_cdata.sk_tx_tag, txd->tx_dmamap); 2544 return (ENOBUFS); 2545 } 2546 2547 m = *m_head; 2548 if ((m->m_pkthdr.csum_flags & sc_if->sk_ifp->if_hwassist) != 0) 2549 cflags = SK_OPCODE_CSUM; 2550 else 2551 cflags = SK_OPCODE_DEFAULT; 2552 si = frag = sc_if->sk_cdata.sk_tx_prod; 2553 for (i = 0; i < nseg; i++) { 2554 f = &sc_if->sk_rdata.sk_tx_ring[frag]; 2555 f->sk_data_lo = htole32(SK_ADDR_LO(txsegs[i].ds_addr)); 2556 f->sk_data_hi = htole32(SK_ADDR_HI(txsegs[i].ds_addr)); 2557 sk_ctl = txsegs[i].ds_len | cflags; 2558 if (i == 0) { 2559 if (cflags == SK_OPCODE_CSUM) 2560 sk_txcksum(sc_if->sk_ifp, m, f); 2561 sk_ctl |= SK_TXCTL_FIRSTFRAG; 2562 } else 2563 sk_ctl |= SK_TXCTL_OWN; 2564 f->sk_ctl = htole32(sk_ctl); 2565 sc_if->sk_cdata.sk_tx_cnt++; 2566 SK_INC(frag, SK_TX_RING_CNT); 2567 } 2568 sc_if->sk_cdata.sk_tx_prod = frag; 2569 2570 /* set EOF on the last desciptor */ 2571 frag = (frag + SK_TX_RING_CNT - 1) % SK_TX_RING_CNT; 2572 f = &sc_if->sk_rdata.sk_tx_ring[frag]; 2573 f->sk_ctl |= htole32(SK_TXCTL_LASTFRAG | SK_TXCTL_EOF_INTR); 2574 2575 /* turn the first descriptor ownership to NIC */ 2576 f = &sc_if->sk_rdata.sk_tx_ring[si]; 2577 f->sk_ctl |= htole32(SK_TXCTL_OWN); 2578 2579 STAILQ_REMOVE_HEAD(&sc_if->sk_cdata.sk_txfreeq, tx_q); 2580 STAILQ_INSERT_TAIL(&sc_if->sk_cdata.sk_txbusyq, txd, tx_q); 2581 txd->tx_m = m; 2582 2583 /* sync descriptors */ 2584 bus_dmamap_sync(sc_if->sk_cdata.sk_tx_tag, txd->tx_dmamap, 2585 BUS_DMASYNC_PREWRITE); 2586 bus_dmamap_sync(sc_if->sk_cdata.sk_tx_ring_tag, 2587 sc_if->sk_cdata.sk_tx_ring_map, 2588 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 2589 2590 return (0); 2591 } 2592 2593 static void 2594 sk_start(ifp) 2595 struct ifnet *ifp; 2596 { 2597 struct sk_if_softc *sc_if; 2598 2599 sc_if = ifp->if_softc; 2600 2601 SK_IF_LOCK(sc_if); 2602 sk_start_locked(ifp); 2603 SK_IF_UNLOCK(sc_if); 2604 2605 return; 2606 } 2607 2608 static void 2609 sk_start_locked(ifp) 2610 struct ifnet *ifp; 2611 { 2612 struct sk_softc *sc; 2613 struct sk_if_softc *sc_if; 2614 struct mbuf *m_head; 2615 int enq; 2616 2617 sc_if = ifp->if_softc; 2618 sc = sc_if->sk_softc; 2619 2620 SK_IF_LOCK_ASSERT(sc_if); 2621 2622 for (enq = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd) && 2623 sc_if->sk_cdata.sk_tx_cnt < SK_TX_RING_CNT - 1; ) { 2624 IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head); 2625 if (m_head == NULL) 2626 break; 2627 2628 /* 2629 * Pack the data into the transmit ring. If we 2630 * don't have room, set the OACTIVE flag and wait 2631 * for the NIC to drain the ring. 2632 */ 2633 if (sk_encap(sc_if, &m_head)) { 2634 if (m_head == NULL) 2635 break; 2636 IFQ_DRV_PREPEND(&ifp->if_snd, m_head); 2637 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 2638 break; 2639 } 2640 2641 enq++; 2642 /* 2643 * If there's a BPF listener, bounce a copy of this frame 2644 * to him. 2645 */ 2646 BPF_MTAP(ifp, m_head); 2647 } 2648 2649 if (enq > 0) { 2650 /* Transmit */ 2651 CSR_WRITE_4(sc, sc_if->sk_tx_bmu, SK_TXBMU_TX_START); 2652 2653 /* Set a timeout in case the chip goes out to lunch. */ 2654 ifp->if_timer = 5; 2655 } 2656 } 2657 2658 2659 static void 2660 sk_watchdog(ifp) 2661 struct ifnet *ifp; 2662 { 2663 struct sk_if_softc *sc_if; 2664 2665 sc_if = ifp->if_softc; 2666 2667 SK_IF_LOCK(sc_if); 2668 /* 2669 * Reclaim first as there is a possibility of losing Tx completion 2670 * interrupts. 2671 */ 2672 sk_txeof(sc_if); 2673 if (sc_if->sk_cdata.sk_tx_cnt != 0) { 2674 if_printf(sc_if->sk_ifp, "watchdog timeout\n"); 2675 ifp->if_oerrors++; 2676 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 2677 sk_init_locked(sc_if); 2678 } 2679 SK_IF_UNLOCK(sc_if); 2680 2681 return; 2682 } 2683 2684 static void 2685 skc_shutdown(dev) 2686 device_t dev; 2687 { 2688 struct sk_softc *sc; 2689 2690 sc = device_get_softc(dev); 2691 SK_LOCK(sc); 2692 2693 /* Turn off the 'driver is loaded' LED. */ 2694 CSR_WRITE_2(sc, SK_LED, SK_LED_GREEN_OFF); 2695 2696 /* 2697 * Reset the GEnesis controller. Doing this should also 2698 * assert the resets on the attached XMAC(s). 2699 */ 2700 sk_reset(sc); 2701 SK_UNLOCK(sc); 2702 2703 return; 2704 } 2705 2706 static int 2707 skc_suspend(dev) 2708 device_t dev; 2709 { 2710 struct sk_softc *sc; 2711 struct sk_if_softc *sc_if0, *sc_if1; 2712 struct ifnet *ifp0 = NULL, *ifp1 = NULL; 2713 2714 sc = device_get_softc(dev); 2715 2716 SK_LOCK(sc); 2717 2718 sc_if0 = sc->sk_if[SK_PORT_A]; 2719 sc_if1 = sc->sk_if[SK_PORT_B]; 2720 if (sc_if0 != NULL) 2721 ifp0 = sc_if0->sk_ifp; 2722 if (sc_if1 != NULL) 2723 ifp1 = sc_if1->sk_ifp; 2724 if (ifp0 != NULL) 2725 sk_stop(sc_if0); 2726 if (ifp1 != NULL) 2727 sk_stop(sc_if1); 2728 sc->sk_suspended = 1; 2729 2730 SK_UNLOCK(sc); 2731 2732 return (0); 2733 } 2734 2735 static int 2736 skc_resume(dev) 2737 device_t dev; 2738 { 2739 struct sk_softc *sc; 2740 struct sk_if_softc *sc_if0, *sc_if1; 2741 struct ifnet *ifp0 = NULL, *ifp1 = NULL; 2742 2743 sc = device_get_softc(dev); 2744 2745 SK_LOCK(sc); 2746 2747 sc_if0 = sc->sk_if[SK_PORT_A]; 2748 sc_if1 = sc->sk_if[SK_PORT_B]; 2749 if (sc_if0 != NULL) 2750 ifp0 = sc_if0->sk_ifp; 2751 if (sc_if1 != NULL) 2752 ifp1 = sc_if1->sk_ifp; 2753 if (ifp0 != NULL && ifp0->if_flags & IFF_UP) 2754 sk_init_locked(sc_if0); 2755 if (ifp1 != NULL && ifp1->if_flags & IFF_UP) 2756 sk_init_locked(sc_if1); 2757 sc->sk_suspended = 0; 2758 2759 SK_UNLOCK(sc); 2760 2761 return (0); 2762 } 2763 2764 /* 2765 * According to the data sheet from SK-NET GENESIS the hardware can compute 2766 * two Rx checksums at the same time(Each checksum start position is 2767 * programmed in Rx descriptors). However it seems that TCP/UDP checksum 2768 * does not work at least on my Yukon hardware. I tried every possible ways 2769 * to get correct checksum value but couldn't get correct one. So TCP/UDP 2770 * checksum offload was disabled at the moment and only IP checksum offload 2771 * was enabled. 2772 * As nomral IP header size is 20 bytes I can't expect it would give an 2773 * increase in throughput. However it seems it doesn't hurt performance in 2774 * my testing. If there is a more detailed information for checksum secret 2775 * of the hardware in question please contact yongari@FreeBSD.org to add 2776 * TCP/UDP checksum offload support. 2777 */ 2778 static __inline void 2779 sk_rxcksum(ifp, m, csum) 2780 struct ifnet *ifp; 2781 struct mbuf *m; 2782 u_int32_t csum; 2783 { 2784 struct ether_header *eh; 2785 struct ip *ip; 2786 int32_t hlen, len, pktlen; 2787 u_int16_t csum1, csum2, ipcsum; 2788 2789 pktlen = m->m_pkthdr.len; 2790 if (pktlen < sizeof(struct ether_header) + sizeof(struct ip)) 2791 return; 2792 eh = mtod(m, struct ether_header *); 2793 if (eh->ether_type != htons(ETHERTYPE_IP)) 2794 return; 2795 ip = (struct ip *)(eh + 1); 2796 if (ip->ip_v != IPVERSION) 2797 return; 2798 hlen = ip->ip_hl << 2; 2799 pktlen -= sizeof(struct ether_header); 2800 if (hlen < sizeof(struct ip)) 2801 return; 2802 if (ntohs(ip->ip_len) < hlen) 2803 return; 2804 if (ntohs(ip->ip_len) != pktlen) 2805 return; 2806 2807 csum1 = htons(csum & 0xffff); 2808 csum2 = htons((csum >> 16) & 0xffff); 2809 ipcsum = in_addword(csum1, ~csum2 & 0xffff); 2810 /* checksum fixup for IP options */ 2811 len = hlen - sizeof(struct ip); 2812 if (len > 0) { 2813 /* 2814 * If the second checksum value is correct we can compute IP 2815 * checksum with simple math. Unfortunately the second checksum 2816 * value is wrong so we can't verify the checksum from the 2817 * value(It seems there is some magic here to get correct 2818 * value). If the second checksum value is correct it also 2819 * means we can get TCP/UDP checksum) here. However, it still 2820 * needs pseudo header checksum calculation due to hardware 2821 * limitations. 2822 */ 2823 return; 2824 } 2825 m->m_pkthdr.csum_flags = CSUM_IP_CHECKED; 2826 if (ipcsum == 0xffff) 2827 m->m_pkthdr.csum_flags |= CSUM_IP_VALID; 2828 } 2829 2830 static __inline int 2831 sk_rxvalid(sc, stat, len) 2832 struct sk_softc *sc; 2833 u_int32_t stat, len; 2834 { 2835 2836 if (sc->sk_type == SK_GENESIS) { 2837 if ((stat & XM_RXSTAT_ERRFRAME) == XM_RXSTAT_ERRFRAME || 2838 XM_RXSTAT_BYTES(stat) != len) 2839 return (0); 2840 } else { 2841 if ((stat & (YU_RXSTAT_CRCERR | YU_RXSTAT_LONGERR | 2842 YU_RXSTAT_MIIERR | YU_RXSTAT_BADFC | YU_RXSTAT_GOODFC | 2843 YU_RXSTAT_JABBER)) != 0 || 2844 (stat & YU_RXSTAT_RXOK) != YU_RXSTAT_RXOK || 2845 YU_RXSTAT_BYTES(stat) != len) 2846 return (0); 2847 } 2848 2849 return (1); 2850 } 2851 2852 static void 2853 sk_rxeof(sc_if) 2854 struct sk_if_softc *sc_if; 2855 { 2856 struct sk_softc *sc; 2857 struct mbuf *m; 2858 struct ifnet *ifp; 2859 struct sk_rx_desc *cur_rx; 2860 struct sk_rxdesc *rxd; 2861 int cons, prog; 2862 u_int32_t csum, rxstat, sk_ctl; 2863 2864 sc = sc_if->sk_softc; 2865 ifp = sc_if->sk_ifp; 2866 2867 SK_IF_LOCK_ASSERT(sc_if); 2868 2869 bus_dmamap_sync(sc_if->sk_cdata.sk_rx_ring_tag, 2870 sc_if->sk_cdata.sk_rx_ring_map, BUS_DMASYNC_POSTREAD); 2871 2872 prog = 0; 2873 for (cons = sc_if->sk_cdata.sk_rx_cons; prog < SK_RX_RING_CNT; 2874 prog++, SK_INC(cons, SK_RX_RING_CNT)) { 2875 cur_rx = &sc_if->sk_rdata.sk_rx_ring[cons]; 2876 sk_ctl = le32toh(cur_rx->sk_ctl); 2877 if ((sk_ctl & SK_RXCTL_OWN) != 0) 2878 break; 2879 rxd = &sc_if->sk_cdata.sk_rxdesc[cons]; 2880 rxstat = le32toh(cur_rx->sk_xmac_rxstat); 2881 2882 if ((sk_ctl & (SK_RXCTL_STATUS_VALID | SK_RXCTL_FIRSTFRAG | 2883 SK_RXCTL_LASTFRAG)) != (SK_RXCTL_STATUS_VALID | 2884 SK_RXCTL_FIRSTFRAG | SK_RXCTL_LASTFRAG) || 2885 SK_RXBYTES(sk_ctl) < SK_MIN_FRAMELEN || 2886 SK_RXBYTES(sk_ctl) > SK_MAX_FRAMELEN || 2887 sk_rxvalid(sc, rxstat, SK_RXBYTES(sk_ctl)) == 0) { 2888 ifp->if_ierrors++; 2889 sk_discard_rxbuf(sc_if, cons); 2890 continue; 2891 } 2892 2893 m = rxd->rx_m; 2894 csum = le32toh(cur_rx->sk_csum); 2895 if (sk_newbuf(sc_if, cons) != 0) { 2896 ifp->if_iqdrops++; 2897 /* reuse old buffer */ 2898 sk_discard_rxbuf(sc_if, cons); 2899 continue; 2900 } 2901 m->m_pkthdr.rcvif = ifp; 2902 m->m_pkthdr.len = m->m_len = SK_RXBYTES(sk_ctl); 2903 ifp->if_ipackets++; 2904 if ((ifp->if_capenable & IFCAP_RXCSUM) != 0) 2905 sk_rxcksum(ifp, m, csum); 2906 SK_IF_UNLOCK(sc_if); 2907 (*ifp->if_input)(ifp, m); 2908 SK_IF_LOCK(sc_if); 2909 } 2910 2911 if (prog > 0) { 2912 sc_if->sk_cdata.sk_rx_cons = cons; 2913 bus_dmamap_sync(sc_if->sk_cdata.sk_rx_ring_tag, 2914 sc_if->sk_cdata.sk_rx_ring_map, 2915 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 2916 } 2917 } 2918 2919 static void 2920 sk_jumbo_rxeof(sc_if) 2921 struct sk_if_softc *sc_if; 2922 { 2923 struct sk_softc *sc; 2924 struct mbuf *m; 2925 struct ifnet *ifp; 2926 struct sk_rx_desc *cur_rx; 2927 struct sk_rxdesc *jrxd; 2928 int cons, prog; 2929 u_int32_t csum, rxstat, sk_ctl; 2930 2931 sc = sc_if->sk_softc; 2932 ifp = sc_if->sk_ifp; 2933 2934 SK_IF_LOCK_ASSERT(sc_if); 2935 2936 bus_dmamap_sync(sc_if->sk_cdata.sk_jumbo_rx_ring_tag, 2937 sc_if->sk_cdata.sk_jumbo_rx_ring_map, BUS_DMASYNC_POSTREAD); 2938 2939 prog = 0; 2940 for (cons = sc_if->sk_cdata.sk_jumbo_rx_cons; 2941 prog < SK_JUMBO_RX_RING_CNT; 2942 prog++, SK_INC(cons, SK_JUMBO_RX_RING_CNT)) { 2943 cur_rx = &sc_if->sk_rdata.sk_jumbo_rx_ring[cons]; 2944 sk_ctl = le32toh(cur_rx->sk_ctl); 2945 if ((sk_ctl & SK_RXCTL_OWN) != 0) 2946 break; 2947 jrxd = &sc_if->sk_cdata.sk_jumbo_rxdesc[cons]; 2948 rxstat = le32toh(cur_rx->sk_xmac_rxstat); 2949 2950 if ((sk_ctl & (SK_RXCTL_STATUS_VALID | SK_RXCTL_FIRSTFRAG | 2951 SK_RXCTL_LASTFRAG)) != (SK_RXCTL_STATUS_VALID | 2952 SK_RXCTL_FIRSTFRAG | SK_RXCTL_LASTFRAG) || 2953 SK_RXBYTES(sk_ctl) < SK_MIN_FRAMELEN || 2954 SK_RXBYTES(sk_ctl) > SK_JUMBO_FRAMELEN || 2955 sk_rxvalid(sc, rxstat, SK_RXBYTES(sk_ctl)) == 0) { 2956 ifp->if_ierrors++; 2957 sk_discard_jumbo_rxbuf(sc_if, cons); 2958 continue; 2959 } 2960 2961 m = jrxd->rx_m; 2962 csum = le32toh(cur_rx->sk_csum); 2963 if (sk_jumbo_newbuf(sc_if, cons) != 0) { 2964 ifp->if_iqdrops++; 2965 /* reuse old buffer */ 2966 sk_discard_jumbo_rxbuf(sc_if, cons); 2967 continue; 2968 } 2969 m->m_pkthdr.rcvif = ifp; 2970 m->m_pkthdr.len = m->m_len = SK_RXBYTES(sk_ctl); 2971 ifp->if_ipackets++; 2972 if ((ifp->if_capenable & IFCAP_RXCSUM) != 0) 2973 sk_rxcksum(ifp, m, csum); 2974 SK_IF_UNLOCK(sc_if); 2975 (*ifp->if_input)(ifp, m); 2976 SK_IF_LOCK(sc_if); 2977 } 2978 2979 if (prog > 0) { 2980 sc_if->sk_cdata.sk_jumbo_rx_cons = cons; 2981 bus_dmamap_sync(sc_if->sk_cdata.sk_jumbo_rx_ring_tag, 2982 sc_if->sk_cdata.sk_jumbo_rx_ring_map, 2983 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 2984 } 2985 } 2986 2987 static void 2988 sk_txeof(sc_if) 2989 struct sk_if_softc *sc_if; 2990 { 2991 struct sk_softc *sc; 2992 struct sk_txdesc *txd; 2993 struct sk_tx_desc *cur_tx; 2994 struct ifnet *ifp; 2995 u_int32_t idx, sk_ctl; 2996 2997 sc = sc_if->sk_softc; 2998 ifp = sc_if->sk_ifp; 2999 3000 txd = STAILQ_FIRST(&sc_if->sk_cdata.sk_txbusyq); 3001 if (txd == NULL) 3002 return; 3003 bus_dmamap_sync(sc_if->sk_cdata.sk_tx_ring_tag, 3004 sc_if->sk_cdata.sk_tx_ring_map, BUS_DMASYNC_POSTREAD); 3005 /* 3006 * Go through our tx ring and free mbufs for those 3007 * frames that have been sent. 3008 */ 3009 for (idx = sc_if->sk_cdata.sk_tx_cons;; SK_INC(idx, SK_TX_RING_CNT)) { 3010 if (sc_if->sk_cdata.sk_tx_cnt <= 0) 3011 break; 3012 cur_tx = &sc_if->sk_rdata.sk_tx_ring[idx]; 3013 sk_ctl = le32toh(cur_tx->sk_ctl); 3014 if (sk_ctl & SK_TXCTL_OWN) 3015 break; 3016 sc_if->sk_cdata.sk_tx_cnt--; 3017 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 3018 if ((sk_ctl & SK_TXCTL_LASTFRAG) == 0) 3019 continue; 3020 bus_dmamap_sync(sc_if->sk_cdata.sk_tx_tag, txd->tx_dmamap, 3021 BUS_DMASYNC_POSTWRITE); 3022 bus_dmamap_unload(sc_if->sk_cdata.sk_tx_tag, txd->tx_dmamap); 3023 3024 ifp->if_opackets++; 3025 m_freem(txd->tx_m); 3026 txd->tx_m = NULL; 3027 STAILQ_REMOVE_HEAD(&sc_if->sk_cdata.sk_txbusyq, tx_q); 3028 STAILQ_INSERT_TAIL(&sc_if->sk_cdata.sk_txfreeq, txd, tx_q); 3029 txd = STAILQ_FIRST(&sc_if->sk_cdata.sk_txbusyq); 3030 } 3031 sc_if->sk_cdata.sk_tx_cons = idx; 3032 ifp->if_timer = sc_if->sk_cdata.sk_tx_cnt > 0 ? 5 : 0; 3033 3034 bus_dmamap_sync(sc_if->sk_cdata.sk_tx_ring_tag, 3035 sc_if->sk_cdata.sk_tx_ring_map, 3036 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 3037 } 3038 3039 static void 3040 sk_tick(xsc_if) 3041 void *xsc_if; 3042 { 3043 struct sk_if_softc *sc_if; 3044 struct mii_data *mii; 3045 struct ifnet *ifp; 3046 int i; 3047 3048 sc_if = xsc_if; 3049 ifp = sc_if->sk_ifp; 3050 mii = device_get_softc(sc_if->sk_miibus); 3051 3052 if (!(ifp->if_flags & IFF_UP)) 3053 return; 3054 3055 if (sc_if->sk_phytype == SK_PHYTYPE_BCOM) { 3056 sk_intr_bcom(sc_if); 3057 return; 3058 } 3059 3060 /* 3061 * According to SysKonnect, the correct way to verify that 3062 * the link has come back up is to poll bit 0 of the GPIO 3063 * register three times. This pin has the signal from the 3064 * link_sync pin connected to it; if we read the same link 3065 * state 3 times in a row, we know the link is up. 3066 */ 3067 for (i = 0; i < 3; i++) { 3068 if (SK_XM_READ_2(sc_if, XM_GPIO) & XM_GPIO_GP0_SET) 3069 break; 3070 } 3071 3072 if (i != 3) { 3073 callout_reset(&sc_if->sk_tick_ch, hz, sk_tick, sc_if); 3074 return; 3075 } 3076 3077 /* Turn the GP0 interrupt back on. */ 3078 SK_XM_CLRBIT_2(sc_if, XM_IMR, XM_IMR_GP0_SET); 3079 SK_XM_READ_2(sc_if, XM_ISR); 3080 mii_tick(mii); 3081 callout_stop(&sc_if->sk_tick_ch); 3082 } 3083 3084 static void 3085 sk_yukon_tick(xsc_if) 3086 void *xsc_if; 3087 { 3088 struct sk_if_softc *sc_if; 3089 struct mii_data *mii; 3090 3091 sc_if = xsc_if; 3092 mii = device_get_softc(sc_if->sk_miibus); 3093 3094 mii_tick(mii); 3095 callout_reset(&sc_if->sk_tick_ch, hz, sk_yukon_tick, sc_if); 3096 } 3097 3098 static void 3099 sk_intr_bcom(sc_if) 3100 struct sk_if_softc *sc_if; 3101 { 3102 struct mii_data *mii; 3103 struct ifnet *ifp; 3104 int status; 3105 mii = device_get_softc(sc_if->sk_miibus); 3106 ifp = sc_if->sk_ifp; 3107 3108 SK_XM_CLRBIT_2(sc_if, XM_MMUCMD, XM_MMUCMD_TX_ENB|XM_MMUCMD_RX_ENB); 3109 3110 /* 3111 * Read the PHY interrupt register to make sure 3112 * we clear any pending interrupts. 3113 */ 3114 status = sk_xmac_miibus_readreg(sc_if, SK_PHYADDR_BCOM, BRGPHY_MII_ISR); 3115 3116 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) { 3117 sk_init_xmac(sc_if); 3118 return; 3119 } 3120 3121 if (status & (BRGPHY_ISR_LNK_CHG|BRGPHY_ISR_AN_PR)) { 3122 int lstat; 3123 lstat = sk_xmac_miibus_readreg(sc_if, SK_PHYADDR_BCOM, 3124 BRGPHY_MII_AUXSTS); 3125 3126 if (!(lstat & BRGPHY_AUXSTS_LINK) && sc_if->sk_link) { 3127 mii_mediachg(mii); 3128 /* Turn off the link LED. */ 3129 SK_IF_WRITE_1(sc_if, 0, 3130 SK_LINKLED1_CTL, SK_LINKLED_OFF); 3131 sc_if->sk_link = 0; 3132 } else if (status & BRGPHY_ISR_LNK_CHG) { 3133 sk_xmac_miibus_writereg(sc_if, SK_PHYADDR_BCOM, 3134 BRGPHY_MII_IMR, 0xFF00); 3135 mii_tick(mii); 3136 sc_if->sk_link = 1; 3137 /* Turn on the link LED. */ 3138 SK_IF_WRITE_1(sc_if, 0, SK_LINKLED1_CTL, 3139 SK_LINKLED_ON|SK_LINKLED_LINKSYNC_OFF| 3140 SK_LINKLED_BLINK_OFF); 3141 } else { 3142 mii_tick(mii); 3143 callout_reset(&sc_if->sk_tick_ch, hz, sk_tick, sc_if); 3144 } 3145 } 3146 3147 SK_XM_SETBIT_2(sc_if, XM_MMUCMD, XM_MMUCMD_TX_ENB|XM_MMUCMD_RX_ENB); 3148 3149 return; 3150 } 3151 3152 static void 3153 sk_intr_xmac(sc_if) 3154 struct sk_if_softc *sc_if; 3155 { 3156 struct sk_softc *sc; 3157 u_int16_t status; 3158 3159 sc = sc_if->sk_softc; 3160 status = SK_XM_READ_2(sc_if, XM_ISR); 3161 3162 /* 3163 * Link has gone down. Start MII tick timeout to 3164 * watch for link resync. 3165 */ 3166 if (sc_if->sk_phytype == SK_PHYTYPE_XMAC) { 3167 if (status & XM_ISR_GP0_SET) { 3168 SK_XM_SETBIT_2(sc_if, XM_IMR, XM_IMR_GP0_SET); 3169 callout_reset(&sc_if->sk_tick_ch, hz, sk_tick, sc_if); 3170 } 3171 3172 if (status & XM_ISR_AUTONEG_DONE) { 3173 callout_reset(&sc_if->sk_tick_ch, hz, sk_tick, sc_if); 3174 } 3175 } 3176 3177 if (status & XM_IMR_TX_UNDERRUN) 3178 SK_XM_SETBIT_4(sc_if, XM_MODE, XM_MODE_FLUSH_TXFIFO); 3179 3180 if (status & XM_IMR_RX_OVERRUN) 3181 SK_XM_SETBIT_4(sc_if, XM_MODE, XM_MODE_FLUSH_RXFIFO); 3182 3183 status = SK_XM_READ_2(sc_if, XM_ISR); 3184 3185 return; 3186 } 3187 3188 static void 3189 sk_intr_yukon(sc_if) 3190 struct sk_if_softc *sc_if; 3191 { 3192 u_int8_t status; 3193 3194 status = SK_IF_READ_1(sc_if, 0, SK_GMAC_ISR); 3195 /* RX overrun */ 3196 if ((status & SK_GMAC_INT_RX_OVER) != 0) { 3197 SK_IF_WRITE_1(sc_if, 0, SK_RXMF1_CTRL_TEST, 3198 SK_RFCTL_RX_FIFO_OVER); 3199 } 3200 /* TX underrun */ 3201 if ((status & SK_GMAC_INT_TX_UNDER) != 0) { 3202 SK_IF_WRITE_1(sc_if, 0, SK_RXMF1_CTRL_TEST, 3203 SK_TFCTL_TX_FIFO_UNDER); 3204 } 3205 } 3206 3207 static void 3208 sk_intr(xsc) 3209 void *xsc; 3210 { 3211 struct sk_softc *sc = xsc; 3212 struct sk_if_softc *sc_if0, *sc_if1; 3213 struct ifnet *ifp0 = NULL, *ifp1 = NULL; 3214 u_int32_t status; 3215 3216 SK_LOCK(sc); 3217 3218 status = CSR_READ_4(sc, SK_ISSR); 3219 if (status == 0 || status == 0xffffffff || sc->sk_suspended) 3220 goto done_locked; 3221 3222 sc_if0 = sc->sk_if[SK_PORT_A]; 3223 sc_if1 = sc->sk_if[SK_PORT_B]; 3224 3225 if (sc_if0 != NULL) 3226 ifp0 = sc_if0->sk_ifp; 3227 if (sc_if1 != NULL) 3228 ifp1 = sc_if1->sk_ifp; 3229 3230 for (; (status &= sc->sk_intrmask) != 0;) { 3231 /* Handle receive interrupts first. */ 3232 if (status & SK_ISR_RX1_EOF) { 3233 if (ifp0->if_mtu > SK_MAX_FRAMELEN) 3234 sk_jumbo_rxeof(sc_if0); 3235 else 3236 sk_rxeof(sc_if0); 3237 CSR_WRITE_4(sc, SK_BMU_RX_CSR0, 3238 SK_RXBMU_CLR_IRQ_EOF|SK_RXBMU_RX_START); 3239 } 3240 if (status & SK_ISR_RX2_EOF) { 3241 if (ifp1->if_mtu > SK_MAX_FRAMELEN) 3242 sk_jumbo_rxeof(sc_if1); 3243 else 3244 sk_rxeof(sc_if1); 3245 CSR_WRITE_4(sc, SK_BMU_RX_CSR1, 3246 SK_RXBMU_CLR_IRQ_EOF|SK_RXBMU_RX_START); 3247 } 3248 3249 /* Then transmit interrupts. */ 3250 if (status & SK_ISR_TX1_S_EOF) { 3251 sk_txeof(sc_if0); 3252 CSR_WRITE_4(sc, SK_BMU_TXS_CSR0, SK_TXBMU_CLR_IRQ_EOF); 3253 } 3254 if (status & SK_ISR_TX2_S_EOF) { 3255 sk_txeof(sc_if1); 3256 CSR_WRITE_4(sc, SK_BMU_TXS_CSR1, SK_TXBMU_CLR_IRQ_EOF); 3257 } 3258 3259 /* Then MAC interrupts. */ 3260 if (status & SK_ISR_MAC1 && 3261 ifp0->if_drv_flags & IFF_DRV_RUNNING) { 3262 if (sc->sk_type == SK_GENESIS) 3263 sk_intr_xmac(sc_if0); 3264 else 3265 sk_intr_yukon(sc_if0); 3266 } 3267 3268 if (status & SK_ISR_MAC2 && 3269 ifp1->if_drv_flags & IFF_DRV_RUNNING) { 3270 if (sc->sk_type == SK_GENESIS) 3271 sk_intr_xmac(sc_if1); 3272 else 3273 sk_intr_yukon(sc_if1); 3274 } 3275 3276 if (status & SK_ISR_EXTERNAL_REG) { 3277 if (ifp0 != NULL && 3278 sc_if0->sk_phytype == SK_PHYTYPE_BCOM) 3279 sk_intr_bcom(sc_if0); 3280 if (ifp1 != NULL && 3281 sc_if1->sk_phytype == SK_PHYTYPE_BCOM) 3282 sk_intr_bcom(sc_if1); 3283 } 3284 status = CSR_READ_4(sc, SK_ISSR); 3285 } 3286 3287 CSR_WRITE_4(sc, SK_IMR, sc->sk_intrmask); 3288 3289 if (ifp0 != NULL && !IFQ_DRV_IS_EMPTY(&ifp0->if_snd)) 3290 sk_start_locked(ifp0); 3291 if (ifp1 != NULL && !IFQ_DRV_IS_EMPTY(&ifp1->if_snd)) 3292 sk_start_locked(ifp1); 3293 3294 done_locked: 3295 SK_UNLOCK(sc); 3296 } 3297 3298 static void 3299 sk_init_xmac(sc_if) 3300 struct sk_if_softc *sc_if; 3301 { 3302 struct sk_softc *sc; 3303 struct ifnet *ifp; 3304 u_int16_t eaddr[(ETHER_ADDR_LEN+1)/2]; 3305 struct sk_bcom_hack bhack[] = { 3306 { 0x18, 0x0c20 }, { 0x17, 0x0012 }, { 0x15, 0x1104 }, { 0x17, 0x0013 }, 3307 { 0x15, 0x0404 }, { 0x17, 0x8006 }, { 0x15, 0x0132 }, { 0x17, 0x8006 }, 3308 { 0x15, 0x0232 }, { 0x17, 0x800D }, { 0x15, 0x000F }, { 0x18, 0x0420 }, 3309 { 0, 0 } }; 3310 3311 SK_IF_LOCK_ASSERT(sc_if); 3312 3313 sc = sc_if->sk_softc; 3314 ifp = sc_if->sk_ifp; 3315 3316 /* Unreset the XMAC. */ 3317 SK_IF_WRITE_2(sc_if, 0, SK_TXF1_MACCTL, SK_TXMACCTL_XMAC_UNRESET); 3318 DELAY(1000); 3319 3320 /* Reset the XMAC's internal state. */ 3321 SK_XM_SETBIT_2(sc_if, XM_GPIO, XM_GPIO_RESETMAC); 3322 3323 /* Save the XMAC II revision */ 3324 sc_if->sk_xmac_rev = XM_XMAC_REV(SK_XM_READ_4(sc_if, XM_DEVID)); 3325 3326 /* 3327 * Perform additional initialization for external PHYs, 3328 * namely for the 1000baseTX cards that use the XMAC's 3329 * GMII mode. 3330 */ 3331 if (sc_if->sk_phytype == SK_PHYTYPE_BCOM) { 3332 int i = 0; 3333 u_int32_t val; 3334 3335 /* Take PHY out of reset. */ 3336 val = sk_win_read_4(sc, SK_GPIO); 3337 if (sc_if->sk_port == SK_PORT_A) 3338 val |= SK_GPIO_DIR0|SK_GPIO_DAT0; 3339 else 3340 val |= SK_GPIO_DIR2|SK_GPIO_DAT2; 3341 sk_win_write_4(sc, SK_GPIO, val); 3342 3343 /* Enable GMII mode on the XMAC. */ 3344 SK_XM_SETBIT_2(sc_if, XM_HWCFG, XM_HWCFG_GMIIMODE); 3345 3346 sk_xmac_miibus_writereg(sc_if, SK_PHYADDR_BCOM, 3347 BRGPHY_MII_BMCR, BRGPHY_BMCR_RESET); 3348 DELAY(10000); 3349 sk_xmac_miibus_writereg(sc_if, SK_PHYADDR_BCOM, 3350 BRGPHY_MII_IMR, 0xFFF0); 3351 3352 /* 3353 * Early versions of the BCM5400 apparently have 3354 * a bug that requires them to have their reserved 3355 * registers initialized to some magic values. I don't 3356 * know what the numbers do, I'm just the messenger. 3357 */ 3358 if (sk_xmac_miibus_readreg(sc_if, SK_PHYADDR_BCOM, 0x03) 3359 == 0x6041) { 3360 while(bhack[i].reg) { 3361 sk_xmac_miibus_writereg(sc_if, SK_PHYADDR_BCOM, 3362 bhack[i].reg, bhack[i].val); 3363 i++; 3364 } 3365 } 3366 } 3367 3368 /* Set station address */ 3369 bcopy(IF_LLADDR(sc_if->sk_ifp), eaddr, ETHER_ADDR_LEN); 3370 SK_XM_WRITE_2(sc_if, XM_PAR0, eaddr[0]); 3371 SK_XM_WRITE_2(sc_if, XM_PAR1, eaddr[1]); 3372 SK_XM_WRITE_2(sc_if, XM_PAR2, eaddr[2]); 3373 SK_XM_SETBIT_4(sc_if, XM_MODE, XM_MODE_RX_USE_STATION); 3374 3375 if (ifp->if_flags & IFF_BROADCAST) { 3376 SK_XM_CLRBIT_4(sc_if, XM_MODE, XM_MODE_RX_NOBROAD); 3377 } else { 3378 SK_XM_SETBIT_4(sc_if, XM_MODE, XM_MODE_RX_NOBROAD); 3379 } 3380 3381 /* We don't need the FCS appended to the packet. */ 3382 SK_XM_SETBIT_2(sc_if, XM_RXCMD, XM_RXCMD_STRIPFCS); 3383 3384 /* We want short frames padded to 60 bytes. */ 3385 SK_XM_SETBIT_2(sc_if, XM_TXCMD, XM_TXCMD_AUTOPAD); 3386 3387 /* 3388 * Enable the reception of all error frames. This is is 3389 * a necessary evil due to the design of the XMAC. The 3390 * XMAC's receive FIFO is only 8K in size, however jumbo 3391 * frames can be up to 9000 bytes in length. When bad 3392 * frame filtering is enabled, the XMAC's RX FIFO operates 3393 * in 'store and forward' mode. For this to work, the 3394 * entire frame has to fit into the FIFO, but that means 3395 * that jumbo frames larger than 8192 bytes will be 3396 * truncated. Disabling all bad frame filtering causes 3397 * the RX FIFO to operate in streaming mode, in which 3398 * case the XMAC will start transfering frames out of the 3399 * RX FIFO as soon as the FIFO threshold is reached. 3400 */ 3401 if (ifp->if_mtu > SK_MAX_FRAMELEN) { 3402 SK_XM_SETBIT_4(sc_if, XM_MODE, XM_MODE_RX_BADFRAMES| 3403 XM_MODE_RX_GIANTS|XM_MODE_RX_RUNTS|XM_MODE_RX_CRCERRS| 3404 XM_MODE_RX_INRANGELEN); 3405 SK_XM_SETBIT_2(sc_if, XM_RXCMD, XM_RXCMD_BIGPKTOK); 3406 } else 3407 SK_XM_CLRBIT_2(sc_if, XM_RXCMD, XM_RXCMD_BIGPKTOK); 3408 3409 /* 3410 * Bump up the transmit threshold. This helps hold off transmit 3411 * underruns when we're blasting traffic from both ports at once. 3412 */ 3413 SK_XM_WRITE_2(sc_if, XM_TX_REQTHRESH, SK_XM_TX_FIFOTHRESH); 3414 3415 /* Set promiscuous mode */ 3416 sk_setpromisc(sc_if); 3417 3418 /* Set multicast filter */ 3419 sk_setmulti(sc_if); 3420 3421 /* Clear and enable interrupts */ 3422 SK_XM_READ_2(sc_if, XM_ISR); 3423 if (sc_if->sk_phytype == SK_PHYTYPE_XMAC) 3424 SK_XM_WRITE_2(sc_if, XM_IMR, XM_INTRS); 3425 else 3426 SK_XM_WRITE_2(sc_if, XM_IMR, 0xFFFF); 3427 3428 /* Configure MAC arbiter */ 3429 switch(sc_if->sk_xmac_rev) { 3430 case XM_XMAC_REV_B2: 3431 sk_win_write_1(sc, SK_RCINIT_RX1, SK_RCINIT_XMAC_B2); 3432 sk_win_write_1(sc, SK_RCINIT_TX1, SK_RCINIT_XMAC_B2); 3433 sk_win_write_1(sc, SK_RCINIT_RX2, SK_RCINIT_XMAC_B2); 3434 sk_win_write_1(sc, SK_RCINIT_TX2, SK_RCINIT_XMAC_B2); 3435 sk_win_write_1(sc, SK_MINIT_RX1, SK_MINIT_XMAC_B2); 3436 sk_win_write_1(sc, SK_MINIT_TX1, SK_MINIT_XMAC_B2); 3437 sk_win_write_1(sc, SK_MINIT_RX2, SK_MINIT_XMAC_B2); 3438 sk_win_write_1(sc, SK_MINIT_TX2, SK_MINIT_XMAC_B2); 3439 sk_win_write_1(sc, SK_RECOVERY_CTL, SK_RECOVERY_XMAC_B2); 3440 break; 3441 case XM_XMAC_REV_C1: 3442 sk_win_write_1(sc, SK_RCINIT_RX1, SK_RCINIT_XMAC_C1); 3443 sk_win_write_1(sc, SK_RCINIT_TX1, SK_RCINIT_XMAC_C1); 3444 sk_win_write_1(sc, SK_RCINIT_RX2, SK_RCINIT_XMAC_C1); 3445 sk_win_write_1(sc, SK_RCINIT_TX2, SK_RCINIT_XMAC_C1); 3446 sk_win_write_1(sc, SK_MINIT_RX1, SK_MINIT_XMAC_C1); 3447 sk_win_write_1(sc, SK_MINIT_TX1, SK_MINIT_XMAC_C1); 3448 sk_win_write_1(sc, SK_MINIT_RX2, SK_MINIT_XMAC_C1); 3449 sk_win_write_1(sc, SK_MINIT_TX2, SK_MINIT_XMAC_C1); 3450 sk_win_write_1(sc, SK_RECOVERY_CTL, SK_RECOVERY_XMAC_B2); 3451 break; 3452 default: 3453 break; 3454 } 3455 sk_win_write_2(sc, SK_MACARB_CTL, 3456 SK_MACARBCTL_UNRESET|SK_MACARBCTL_FASTOE_OFF); 3457 3458 sc_if->sk_link = 1; 3459 3460 return; 3461 } 3462 3463 static void 3464 sk_init_yukon(sc_if) 3465 struct sk_if_softc *sc_if; 3466 { 3467 u_int32_t phy, v; 3468 u_int16_t reg; 3469 struct sk_softc *sc; 3470 struct ifnet *ifp; 3471 int i; 3472 3473 SK_IF_LOCK_ASSERT(sc_if); 3474 3475 sc = sc_if->sk_softc; 3476 ifp = sc_if->sk_ifp; 3477 3478 if (sc->sk_type == SK_YUKON_LITE && 3479 sc->sk_rev >= SK_YUKON_LITE_REV_A3) { 3480 /* 3481 * Workaround code for COMA mode, set PHY reset. 3482 * Otherwise it will not correctly take chip out of 3483 * powerdown (coma) 3484 */ 3485 v = sk_win_read_4(sc, SK_GPIO); 3486 v |= SK_GPIO_DIR9 | SK_GPIO_DAT9; 3487 sk_win_write_4(sc, SK_GPIO, v); 3488 } 3489 3490 /* GMAC and GPHY Reset */ 3491 SK_IF_WRITE_4(sc_if, 0, SK_GPHY_CTRL, SK_GPHY_RESET_SET); 3492 SK_IF_WRITE_4(sc_if, 0, SK_GMAC_CTRL, SK_GMAC_RESET_SET); 3493 DELAY(1000); 3494 3495 if (sc->sk_type == SK_YUKON_LITE && 3496 sc->sk_rev >= SK_YUKON_LITE_REV_A3) { 3497 /* 3498 * Workaround code for COMA mode, clear PHY reset 3499 */ 3500 v = sk_win_read_4(sc, SK_GPIO); 3501 v |= SK_GPIO_DIR9; 3502 v &= ~SK_GPIO_DAT9; 3503 sk_win_write_4(sc, SK_GPIO, v); 3504 } 3505 3506 phy = SK_GPHY_INT_POL_HI | SK_GPHY_DIS_FC | SK_GPHY_DIS_SLEEP | 3507 SK_GPHY_ENA_XC | SK_GPHY_ANEG_ALL | SK_GPHY_ENA_PAUSE; 3508 3509 if (sc->sk_coppertype) 3510 phy |= SK_GPHY_COPPER; 3511 else 3512 phy |= SK_GPHY_FIBER; 3513 3514 SK_IF_WRITE_4(sc_if, 0, SK_GPHY_CTRL, phy | SK_GPHY_RESET_SET); 3515 DELAY(1000); 3516 SK_IF_WRITE_4(sc_if, 0, SK_GPHY_CTRL, phy | SK_GPHY_RESET_CLEAR); 3517 SK_IF_WRITE_4(sc_if, 0, SK_GMAC_CTRL, SK_GMAC_LOOP_OFF | 3518 SK_GMAC_PAUSE_ON | SK_GMAC_RESET_CLEAR); 3519 3520 /* unused read of the interrupt source register */ 3521 SK_IF_READ_2(sc_if, 0, SK_GMAC_ISR); 3522 3523 reg = SK_YU_READ_2(sc_if, YUKON_PAR); 3524 3525 /* MIB Counter Clear Mode set */ 3526 reg |= YU_PAR_MIB_CLR; 3527 SK_YU_WRITE_2(sc_if, YUKON_PAR, reg); 3528 3529 /* MIB Counter Clear Mode clear */ 3530 reg &= ~YU_PAR_MIB_CLR; 3531 SK_YU_WRITE_2(sc_if, YUKON_PAR, reg); 3532 3533 /* receive control reg */ 3534 SK_YU_WRITE_2(sc_if, YUKON_RCR, YU_RCR_CRCR); 3535 3536 /* transmit parameter register */ 3537 SK_YU_WRITE_2(sc_if, YUKON_TPR, YU_TPR_JAM_LEN(0x3) | 3538 YU_TPR_JAM_IPG(0xb) | YU_TPR_JAM2DATA_IPG(0x1a) ); 3539 3540 /* serial mode register */ 3541 reg = YU_SMR_DATA_BLIND(0x1c) | YU_SMR_MFL_VLAN | YU_SMR_IPG_DATA(0x1e); 3542 if (ifp->if_mtu > SK_MAX_FRAMELEN) 3543 reg |= YU_SMR_MFL_JUMBO; 3544 SK_YU_WRITE_2(sc_if, YUKON_SMR, reg); 3545 3546 /* Setup Yukon's address */ 3547 for (i = 0; i < 3; i++) { 3548 /* Write Source Address 1 (unicast filter) */ 3549 SK_YU_WRITE_2(sc_if, YUKON_SAL1 + i * 4, 3550 IF_LLADDR(sc_if->sk_ifp)[i * 2] | 3551 IF_LLADDR(sc_if->sk_ifp)[i * 2 + 1] << 8); 3552 } 3553 3554 for (i = 0; i < 3; i++) { 3555 reg = sk_win_read_2(sc_if->sk_softc, 3556 SK_MAC1_0 + i * 2 + sc_if->sk_port * 8); 3557 SK_YU_WRITE_2(sc_if, YUKON_SAL2 + i * 4, reg); 3558 } 3559 3560 /* Set promiscuous mode */ 3561 sk_setpromisc(sc_if); 3562 3563 /* Set multicast filter */ 3564 sk_setmulti(sc_if); 3565 3566 /* enable interrupt mask for counter overflows */ 3567 SK_YU_WRITE_2(sc_if, YUKON_TIMR, 0); 3568 SK_YU_WRITE_2(sc_if, YUKON_RIMR, 0); 3569 SK_YU_WRITE_2(sc_if, YUKON_TRIMR, 0); 3570 3571 /* Configure RX MAC FIFO Flush Mask */ 3572 v = YU_RXSTAT_FOFL | YU_RXSTAT_CRCERR | YU_RXSTAT_MIIERR | 3573 YU_RXSTAT_BADFC | YU_RXSTAT_GOODFC | YU_RXSTAT_RUNT | 3574 YU_RXSTAT_JABBER; 3575 SK_IF_WRITE_2(sc_if, 0, SK_RXMF1_FLUSH_MASK, v); 3576 3577 /* Disable RX MAC FIFO Flush for YUKON-Lite Rev. A0 only */ 3578 if (sc->sk_type == SK_YUKON_LITE && sc->sk_rev == SK_YUKON_LITE_REV_A0) 3579 v = SK_TFCTL_OPERATION_ON; 3580 else 3581 v = SK_TFCTL_OPERATION_ON | SK_RFCTL_FIFO_FLUSH_ON; 3582 /* Configure RX MAC FIFO */ 3583 SK_IF_WRITE_1(sc_if, 0, SK_RXMF1_CTRL_TEST, SK_RFCTL_RESET_CLEAR); 3584 SK_IF_WRITE_2(sc_if, 0, SK_RXMF1_CTRL_TEST, v); 3585 3586 /* Increase flush threshould to 64 bytes */ 3587 SK_IF_WRITE_2(sc_if, 0, SK_RXMF1_FLUSH_THRESHOLD, 3588 SK_RFCTL_FIFO_THRESHOLD + 1); 3589 3590 /* Configure TX MAC FIFO */ 3591 SK_IF_WRITE_1(sc_if, 0, SK_TXMF1_CTRL_TEST, SK_TFCTL_RESET_CLEAR); 3592 SK_IF_WRITE_2(sc_if, 0, SK_TXMF1_CTRL_TEST, SK_TFCTL_OPERATION_ON); 3593 } 3594 3595 /* 3596 * Note that to properly initialize any part of the GEnesis chip, 3597 * you first have to take it out of reset mode. 3598 */ 3599 static void 3600 sk_init(xsc) 3601 void *xsc; 3602 { 3603 struct sk_if_softc *sc_if = xsc; 3604 3605 SK_IF_LOCK(sc_if); 3606 sk_init_locked(sc_if); 3607 SK_IF_UNLOCK(sc_if); 3608 3609 return; 3610 } 3611 3612 static void 3613 sk_init_locked(sc_if) 3614 struct sk_if_softc *sc_if; 3615 { 3616 struct sk_softc *sc; 3617 struct ifnet *ifp; 3618 struct mii_data *mii; 3619 u_int16_t reg; 3620 u_int32_t imr; 3621 int error; 3622 3623 SK_IF_LOCK_ASSERT(sc_if); 3624 3625 ifp = sc_if->sk_ifp; 3626 sc = sc_if->sk_softc; 3627 mii = device_get_softc(sc_if->sk_miibus); 3628 3629 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 3630 return; 3631 3632 /* Cancel pending I/O and free all RX/TX buffers. */ 3633 sk_stop(sc_if); 3634 3635 if (sc->sk_type == SK_GENESIS) { 3636 /* Configure LINK_SYNC LED */ 3637 SK_IF_WRITE_1(sc_if, 0, SK_LINKLED1_CTL, SK_LINKLED_ON); 3638 SK_IF_WRITE_1(sc_if, 0, SK_LINKLED1_CTL, 3639 SK_LINKLED_LINKSYNC_ON); 3640 3641 /* Configure RX LED */ 3642 SK_IF_WRITE_1(sc_if, 0, SK_RXLED1_CTL, 3643 SK_RXLEDCTL_COUNTER_START); 3644 3645 /* Configure TX LED */ 3646 SK_IF_WRITE_1(sc_if, 0, SK_TXLED1_CTL, 3647 SK_TXLEDCTL_COUNTER_START); 3648 } 3649 3650 /* 3651 * Configure descriptor poll timer 3652 * 3653 * SK-NET GENESIS data sheet says that possibility of losing Start 3654 * transmit command due to CPU/cache related interim storage problems 3655 * under certain conditions. The document recommends a polling 3656 * mechanism to send a Start transmit command to initiate transfer 3657 * of ready descriptors regulary. To cope with this issue sk(4) now 3658 * enables descriptor poll timer to initiate descriptor processing 3659 * periodically as defined by SK_DPT_TIMER_MAX. However sk(4) still 3660 * issue SK_TXBMU_TX_START to Tx BMU to get fast execution of Tx 3661 * command instead of waiting for next descriptor polling time. 3662 * The same rule may apply to Rx side too but it seems that is not 3663 * needed at the moment. 3664 * Since sk(4) uses descriptor polling as a last resort there is no 3665 * need to set smaller polling time than maximum allowable one. 3666 */ 3667 SK_IF_WRITE_4(sc_if, 0, SK_DPT_INIT, SK_DPT_TIMER_MAX); 3668 3669 /* Configure I2C registers */ 3670 3671 /* Configure XMAC(s) */ 3672 switch (sc->sk_type) { 3673 case SK_GENESIS: 3674 sk_init_xmac(sc_if); 3675 break; 3676 case SK_YUKON: 3677 case SK_YUKON_LITE: 3678 case SK_YUKON_LP: 3679 sk_init_yukon(sc_if); 3680 break; 3681 } 3682 mii_mediachg(mii); 3683 3684 if (sc->sk_type == SK_GENESIS) { 3685 /* Configure MAC FIFOs */ 3686 SK_IF_WRITE_4(sc_if, 0, SK_RXF1_CTL, SK_FIFO_UNRESET); 3687 SK_IF_WRITE_4(sc_if, 0, SK_RXF1_END, SK_FIFO_END); 3688 SK_IF_WRITE_4(sc_if, 0, SK_RXF1_CTL, SK_FIFO_ON); 3689 3690 SK_IF_WRITE_4(sc_if, 0, SK_TXF1_CTL, SK_FIFO_UNRESET); 3691 SK_IF_WRITE_4(sc_if, 0, SK_TXF1_END, SK_FIFO_END); 3692 SK_IF_WRITE_4(sc_if, 0, SK_TXF1_CTL, SK_FIFO_ON); 3693 } 3694 3695 /* Configure transmit arbiter(s) */ 3696 SK_IF_WRITE_1(sc_if, 0, SK_TXAR1_COUNTERCTL, 3697 SK_TXARCTL_ON|SK_TXARCTL_FSYNC_ON); 3698 3699 /* Configure RAMbuffers */ 3700 SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_CTLTST, SK_RBCTL_UNRESET); 3701 SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_START, sc_if->sk_rx_ramstart); 3702 SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_WR_PTR, sc_if->sk_rx_ramstart); 3703 SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_RD_PTR, sc_if->sk_rx_ramstart); 3704 SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_END, sc_if->sk_rx_ramend); 3705 SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_CTLTST, SK_RBCTL_ON); 3706 3707 SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_CTLTST, SK_RBCTL_UNRESET); 3708 SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_CTLTST, SK_RBCTL_STORENFWD_ON); 3709 SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_START, sc_if->sk_tx_ramstart); 3710 SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_WR_PTR, sc_if->sk_tx_ramstart); 3711 SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_RD_PTR, sc_if->sk_tx_ramstart); 3712 SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_END, sc_if->sk_tx_ramend); 3713 SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_CTLTST, SK_RBCTL_ON); 3714 3715 /* Configure BMUs */ 3716 SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_BMU_CSR, SK_RXBMU_ONLINE); 3717 if (ifp->if_mtu > SK_MAX_FRAMELEN) { 3718 SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_CURADDR_LO, 3719 SK_ADDR_LO(SK_JUMBO_RX_RING_ADDR(sc_if, 0))); 3720 SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_CURADDR_HI, 3721 SK_ADDR_HI(SK_JUMBO_RX_RING_ADDR(sc_if, 0))); 3722 } else { 3723 SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_CURADDR_LO, 3724 SK_ADDR_LO(SK_RX_RING_ADDR(sc_if, 0))); 3725 SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_CURADDR_HI, 3726 SK_ADDR_HI(SK_RX_RING_ADDR(sc_if, 0))); 3727 } 3728 3729 SK_IF_WRITE_4(sc_if, 1, SK_TXQS1_BMU_CSR, SK_TXBMU_ONLINE); 3730 SK_IF_WRITE_4(sc_if, 1, SK_TXQS1_CURADDR_LO, 3731 SK_ADDR_LO(SK_TX_RING_ADDR(sc_if, 0))); 3732 SK_IF_WRITE_4(sc_if, 1, SK_TXQS1_CURADDR_HI, 3733 SK_ADDR_HI(SK_TX_RING_ADDR(sc_if, 0))); 3734 3735 /* Init descriptors */ 3736 if (ifp->if_mtu > SK_MAX_FRAMELEN) 3737 error = sk_init_jumbo_rx_ring(sc_if); 3738 else 3739 error = sk_init_rx_ring(sc_if); 3740 if (error != 0) { 3741 device_printf(sc_if->sk_if_dev, 3742 "initialization failed: no memory for rx buffers\n"); 3743 sk_stop(sc_if); 3744 return; 3745 } 3746 sk_init_tx_ring(sc_if); 3747 3748 /* Set interrupt moderation if changed via sysctl. */ 3749 imr = sk_win_read_4(sc, SK_IMTIMERINIT); 3750 if (imr != SK_IM_USECS(sc->sk_int_mod, sc->sk_int_ticks)) { 3751 sk_win_write_4(sc, SK_IMTIMERINIT, SK_IM_USECS(sc->sk_int_mod, 3752 sc->sk_int_ticks)); 3753 if (bootverbose) 3754 device_printf(sc_if->sk_if_dev, 3755 "interrupt moderation is %d us.\n", 3756 sc->sk_int_mod); 3757 } 3758 3759 /* Configure interrupt handling */ 3760 CSR_READ_4(sc, SK_ISSR); 3761 if (sc_if->sk_port == SK_PORT_A) 3762 sc->sk_intrmask |= SK_INTRS1; 3763 else 3764 sc->sk_intrmask |= SK_INTRS2; 3765 3766 sc->sk_intrmask |= SK_ISR_EXTERNAL_REG; 3767 3768 CSR_WRITE_4(sc, SK_IMR, sc->sk_intrmask); 3769 3770 /* Start BMUs. */ 3771 SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_BMU_CSR, SK_RXBMU_RX_START); 3772 3773 switch(sc->sk_type) { 3774 case SK_GENESIS: 3775 /* Enable XMACs TX and RX state machines */ 3776 SK_XM_CLRBIT_2(sc_if, XM_MMUCMD, XM_MMUCMD_IGNPAUSE); 3777 SK_XM_SETBIT_2(sc_if, XM_MMUCMD, XM_MMUCMD_TX_ENB|XM_MMUCMD_RX_ENB); 3778 break; 3779 case SK_YUKON: 3780 case SK_YUKON_LITE: 3781 case SK_YUKON_LP: 3782 reg = SK_YU_READ_2(sc_if, YUKON_GPCR); 3783 reg |= YU_GPCR_TXEN | YU_GPCR_RXEN; 3784 #if 0 3785 /* XXX disable 100Mbps and full duplex mode? */ 3786 reg &= ~(YU_GPCR_SPEED | YU_GPCR_DPLX_DIS); 3787 #endif 3788 SK_YU_WRITE_2(sc_if, YUKON_GPCR, reg); 3789 } 3790 3791 /* Activate descriptor polling timer */ 3792 SK_IF_WRITE_4(sc_if, 0, SK_DPT_TIMER_CTRL, SK_DPT_TCTL_START); 3793 /* start transfer of Tx descriptors */ 3794 CSR_WRITE_4(sc, sc_if->sk_tx_bmu, SK_TXBMU_TX_START); 3795 3796 ifp->if_drv_flags |= IFF_DRV_RUNNING; 3797 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 3798 3799 switch (sc->sk_type) { 3800 case SK_YUKON: 3801 case SK_YUKON_LITE: 3802 case SK_YUKON_LP: 3803 callout_reset(&sc_if->sk_tick_ch, hz, sk_yukon_tick, sc_if); 3804 break; 3805 } 3806 3807 return; 3808 } 3809 3810 static void 3811 sk_stop(sc_if) 3812 struct sk_if_softc *sc_if; 3813 { 3814 int i; 3815 struct sk_softc *sc; 3816 struct sk_txdesc *txd; 3817 struct sk_rxdesc *rxd; 3818 struct sk_rxdesc *jrxd; 3819 struct ifnet *ifp; 3820 u_int32_t val; 3821 3822 SK_IF_LOCK_ASSERT(sc_if); 3823 sc = sc_if->sk_softc; 3824 ifp = sc_if->sk_ifp; 3825 3826 callout_stop(&sc_if->sk_tick_ch); 3827 3828 /* stop Tx descriptor polling timer */ 3829 SK_IF_WRITE_4(sc_if, 0, SK_DPT_TIMER_CTRL, SK_DPT_TCTL_STOP); 3830 /* stop transfer of Tx descriptors */ 3831 CSR_WRITE_4(sc, sc_if->sk_tx_bmu, SK_TXBMU_TX_STOP); 3832 for (i = 0; i < SK_TIMEOUT; i++) { 3833 val = CSR_READ_4(sc, sc_if->sk_tx_bmu); 3834 if ((val & SK_TXBMU_TX_STOP) == 0) 3835 break; 3836 DELAY(1); 3837 } 3838 if (i == SK_TIMEOUT) 3839 device_printf(sc_if->sk_if_dev, 3840 "can not stop transfer of Tx descriptor\n"); 3841 /* stop transfer of Rx descriptors */ 3842 SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_BMU_CSR, SK_RXBMU_RX_STOP); 3843 for (i = 0; i < SK_TIMEOUT; i++) { 3844 val = SK_IF_READ_4(sc_if, 0, SK_RXQ1_BMU_CSR); 3845 if ((val & SK_RXBMU_RX_STOP) == 0) 3846 break; 3847 DELAY(1); 3848 } 3849 if (i == SK_TIMEOUT) 3850 device_printf(sc_if->sk_if_dev, 3851 "can not stop transfer of Rx descriptor\n"); 3852 3853 if (sc_if->sk_phytype == SK_PHYTYPE_BCOM) { 3854 /* Put PHY back into reset. */ 3855 val = sk_win_read_4(sc, SK_GPIO); 3856 if (sc_if->sk_port == SK_PORT_A) { 3857 val |= SK_GPIO_DIR0; 3858 val &= ~SK_GPIO_DAT0; 3859 } else { 3860 val |= SK_GPIO_DIR2; 3861 val &= ~SK_GPIO_DAT2; 3862 } 3863 sk_win_write_4(sc, SK_GPIO, val); 3864 } 3865 3866 /* Turn off various components of this interface. */ 3867 SK_XM_SETBIT_2(sc_if, XM_GPIO, XM_GPIO_RESETMAC); 3868 switch (sc->sk_type) { 3869 case SK_GENESIS: 3870 SK_IF_WRITE_2(sc_if, 0, SK_TXF1_MACCTL, SK_TXMACCTL_XMAC_RESET); 3871 SK_IF_WRITE_4(sc_if, 0, SK_RXF1_CTL, SK_FIFO_RESET); 3872 break; 3873 case SK_YUKON: 3874 case SK_YUKON_LITE: 3875 case SK_YUKON_LP: 3876 SK_IF_WRITE_1(sc_if,0, SK_RXMF1_CTRL_TEST, SK_RFCTL_RESET_SET); 3877 SK_IF_WRITE_1(sc_if,0, SK_TXMF1_CTRL_TEST, SK_TFCTL_RESET_SET); 3878 break; 3879 } 3880 SK_IF_WRITE_4(sc_if, 0, SK_RXQ1_BMU_CSR, SK_RXBMU_OFFLINE); 3881 SK_IF_WRITE_4(sc_if, 0, SK_RXRB1_CTLTST, SK_RBCTL_RESET|SK_RBCTL_OFF); 3882 SK_IF_WRITE_4(sc_if, 1, SK_TXQS1_BMU_CSR, SK_TXBMU_OFFLINE); 3883 SK_IF_WRITE_4(sc_if, 1, SK_TXRBS1_CTLTST, SK_RBCTL_RESET|SK_RBCTL_OFF); 3884 SK_IF_WRITE_1(sc_if, 0, SK_TXAR1_COUNTERCTL, SK_TXARCTL_OFF); 3885 SK_IF_WRITE_1(sc_if, 0, SK_RXLED1_CTL, SK_RXLEDCTL_COUNTER_STOP); 3886 SK_IF_WRITE_1(sc_if, 0, SK_TXLED1_CTL, SK_RXLEDCTL_COUNTER_STOP); 3887 SK_IF_WRITE_1(sc_if, 0, SK_LINKLED1_CTL, SK_LINKLED_OFF); 3888 SK_IF_WRITE_1(sc_if, 0, SK_LINKLED1_CTL, SK_LINKLED_LINKSYNC_OFF); 3889 3890 /* Disable interrupts */ 3891 if (sc_if->sk_port == SK_PORT_A) 3892 sc->sk_intrmask &= ~SK_INTRS1; 3893 else 3894 sc->sk_intrmask &= ~SK_INTRS2; 3895 CSR_WRITE_4(sc, SK_IMR, sc->sk_intrmask); 3896 3897 SK_XM_READ_2(sc_if, XM_ISR); 3898 SK_XM_WRITE_2(sc_if, XM_IMR, 0xFFFF); 3899 3900 /* Free RX and TX mbufs still in the queues. */ 3901 for (i = 0; i < SK_RX_RING_CNT; i++) { 3902 rxd = &sc_if->sk_cdata.sk_rxdesc[i]; 3903 if (rxd->rx_m != NULL) { 3904 bus_dmamap_sync(sc_if->sk_cdata.sk_rx_tag, 3905 rxd->rx_dmamap, BUS_DMASYNC_POSTREAD); 3906 bus_dmamap_unload(sc_if->sk_cdata.sk_rx_tag, 3907 rxd->rx_dmamap); 3908 m_freem(rxd->rx_m); 3909 rxd->rx_m = NULL; 3910 } 3911 } 3912 for (i = 0; i < SK_JUMBO_RX_RING_CNT; i++) { 3913 jrxd = &sc_if->sk_cdata.sk_jumbo_rxdesc[i]; 3914 if (jrxd->rx_m != NULL) { 3915 bus_dmamap_sync(sc_if->sk_cdata.sk_jumbo_rx_tag, 3916 jrxd->rx_dmamap, BUS_DMASYNC_POSTREAD); 3917 bus_dmamap_unload(sc_if->sk_cdata.sk_jumbo_rx_tag, 3918 jrxd->rx_dmamap); 3919 m_freem(jrxd->rx_m); 3920 jrxd->rx_m = NULL; 3921 } 3922 } 3923 for (i = 0; i < SK_TX_RING_CNT; i++) { 3924 txd = &sc_if->sk_cdata.sk_txdesc[i]; 3925 if (txd->tx_m != NULL) { 3926 bus_dmamap_sync(sc_if->sk_cdata.sk_tx_tag, 3927 txd->tx_dmamap, BUS_DMASYNC_POSTWRITE); 3928 bus_dmamap_unload(sc_if->sk_cdata.sk_tx_tag, 3929 txd->tx_dmamap); 3930 m_freem(txd->tx_m); 3931 txd->tx_m = NULL; 3932 } 3933 } 3934 3935 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING|IFF_DRV_OACTIVE); 3936 3937 return; 3938 } 3939 3940 static int 3941 sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high) 3942 { 3943 int error, value; 3944 3945 if (!arg1) 3946 return (EINVAL); 3947 value = *(int *)arg1; 3948 error = sysctl_handle_int(oidp, &value, 0, req); 3949 if (error || !req->newptr) 3950 return (error); 3951 if (value < low || value > high) 3952 return (EINVAL); 3953 *(int *)arg1 = value; 3954 return (0); 3955 } 3956 3957 static int 3958 sysctl_hw_sk_int_mod(SYSCTL_HANDLER_ARGS) 3959 { 3960 return (sysctl_int_range(oidp, arg1, arg2, req, SK_IM_MIN, SK_IM_MAX)); 3961 } 3962