1 /****************************************************************************** 2 * 3 * Name : sky2.c 4 * Project: Gigabit Ethernet Driver for FreeBSD 5.x/6.x 5 * Version: $Revision: 1.23 $ 6 * Date : $Date: 2005/12/22 09:04:11 $ 7 * Purpose: Main driver source file 8 * 9 *****************************************************************************/ 10 11 /****************************************************************************** 12 * 13 * LICENSE: 14 * Copyright (C) Marvell International Ltd. and/or its affiliates 15 * 16 * The computer program files contained in this folder ("Files") 17 * are provided to you under the BSD-type license terms provided 18 * below, and any use of such Files and any derivative works 19 * thereof created by you shall be governed by the following terms 20 * and conditions: 21 * 22 * - Redistributions of source code must retain the above copyright 23 * notice, this list of conditions and the following disclaimer. 24 * - Redistributions in binary form must reproduce the above 25 * copyright notice, this list of conditions and the following 26 * disclaimer in the documentation and/or other materials provided 27 * with the distribution. 28 * - Neither the name of Marvell nor the names of its contributors 29 * may be used to endorse or promote products derived from this 30 * software without specific prior written permission. 31 * 32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 35 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 36 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 37 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 38 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 39 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 40 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 41 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 42 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 43 * OF THE POSSIBILITY OF SUCH DAMAGE. 44 * /LICENSE 45 * 46 *****************************************************************************/ 47 48 /*- 49 * SPDX-License-Identifier: BSD-4-Clause AND BSD-3-Clause 50 * 51 * Copyright (c) 1997, 1998, 1999, 2000 52 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved. 53 * 54 * Redistribution and use in source and binary forms, with or without 55 * modification, are permitted provided that the following conditions 56 * are met: 57 * 1. Redistributions of source code must retain the above copyright 58 * notice, this list of conditions and the following disclaimer. 59 * 2. Redistributions in binary form must reproduce the above copyright 60 * notice, this list of conditions and the following disclaimer in the 61 * documentation and/or other materials provided with the distribution. 62 * 3. All advertising materials mentioning features or use of this software 63 * must display the following acknowledgement: 64 * This product includes software developed by Bill Paul. 65 * 4. Neither the name of the author nor the names of any co-contributors 66 * may be used to endorse or promote products derived from this software 67 * without specific prior written permission. 68 * 69 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 70 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 71 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 72 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD 73 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 74 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 75 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 76 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 77 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 78 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 79 * THE POSSIBILITY OF SUCH DAMAGE. 80 */ 81 /*- 82 * Copyright (c) 2003 Nathan L. Binkert <binkertn@umich.edu> 83 * 84 * Permission to use, copy, modify, and distribute this software for any 85 * purpose with or without fee is hereby granted, provided that the above 86 * copyright notice and this permission notice appear in all copies. 87 * 88 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 89 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 90 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 91 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 92 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 93 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 94 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 95 */ 96 97 /* 98 * Device driver for the Marvell Yukon II Ethernet controller. 99 * Due to lack of documentation, this driver is based on the code from 100 * sk(4) and Marvell's myk(4) driver for FreeBSD 5.x. 101 */ 102 103 #include <sys/cdefs.h> 104 __FBSDID("$FreeBSD$"); 105 106 #include <sys/param.h> 107 #include <sys/systm.h> 108 #include <sys/bus.h> 109 #include <sys/endian.h> 110 #include <sys/mbuf.h> 111 #include <sys/malloc.h> 112 #include <sys/kernel.h> 113 #include <sys/module.h> 114 #include <sys/socket.h> 115 #include <sys/sockio.h> 116 #include <sys/queue.h> 117 #include <sys/sysctl.h> 118 119 #include <net/bpf.h> 120 #include <net/ethernet.h> 121 #include <net/if.h> 122 #include <net/if_var.h> 123 #include <net/if_arp.h> 124 #include <net/if_dl.h> 125 #include <net/if_media.h> 126 #include <net/if_types.h> 127 #include <net/if_vlan_var.h> 128 129 #include <netinet/in.h> 130 #include <netinet/in_systm.h> 131 #include <netinet/ip.h> 132 #include <netinet/tcp.h> 133 #include <netinet/udp.h> 134 135 #include <machine/bus.h> 136 #include <machine/in_cksum.h> 137 #include <machine/resource.h> 138 #include <sys/rman.h> 139 140 #include <dev/mii/mii.h> 141 #include <dev/mii/miivar.h> 142 143 #include <dev/pci/pcireg.h> 144 #include <dev/pci/pcivar.h> 145 146 #include <dev/msk/if_mskreg.h> 147 148 MODULE_DEPEND(msk, pci, 1, 1, 1); 149 MODULE_DEPEND(msk, ether, 1, 1, 1); 150 MODULE_DEPEND(msk, miibus, 1, 1, 1); 151 152 /* "device miibus" required. See GENERIC if you get errors here. */ 153 #include "miibus_if.h" 154 155 /* Tunables. */ 156 static int msi_disable = 0; 157 TUNABLE_INT("hw.msk.msi_disable", &msi_disable); 158 static int legacy_intr = 0; 159 TUNABLE_INT("hw.msk.legacy_intr", &legacy_intr); 160 static int jumbo_disable = 0; 161 TUNABLE_INT("hw.msk.jumbo_disable", &jumbo_disable); 162 163 #define MSK_CSUM_FEATURES (CSUM_TCP | CSUM_UDP) 164 165 /* 166 * Devices supported by this driver. 167 */ 168 static const struct msk_product { 169 uint16_t msk_vendorid; 170 uint16_t msk_deviceid; 171 const char *msk_name; 172 } msk_products[] = { 173 { VENDORID_SK, DEVICEID_SK_YUKON2, 174 "SK-9Sxx Gigabit Ethernet" }, 175 { VENDORID_SK, DEVICEID_SK_YUKON2_EXPR, 176 "SK-9Exx Gigabit Ethernet"}, 177 { VENDORID_MARVELL, DEVICEID_MRVL_8021CU, 178 "Marvell Yukon 88E8021CU Gigabit Ethernet" }, 179 { VENDORID_MARVELL, DEVICEID_MRVL_8021X, 180 "Marvell Yukon 88E8021 SX/LX Gigabit Ethernet" }, 181 { VENDORID_MARVELL, DEVICEID_MRVL_8022CU, 182 "Marvell Yukon 88E8022CU Gigabit Ethernet" }, 183 { VENDORID_MARVELL, DEVICEID_MRVL_8022X, 184 "Marvell Yukon 88E8022 SX/LX Gigabit Ethernet" }, 185 { VENDORID_MARVELL, DEVICEID_MRVL_8061CU, 186 "Marvell Yukon 88E8061CU Gigabit Ethernet" }, 187 { VENDORID_MARVELL, DEVICEID_MRVL_8061X, 188 "Marvell Yukon 88E8061 SX/LX Gigabit Ethernet" }, 189 { VENDORID_MARVELL, DEVICEID_MRVL_8062CU, 190 "Marvell Yukon 88E8062CU Gigabit Ethernet" }, 191 { VENDORID_MARVELL, DEVICEID_MRVL_8062X, 192 "Marvell Yukon 88E8062 SX/LX Gigabit Ethernet" }, 193 { VENDORID_MARVELL, DEVICEID_MRVL_8035, 194 "Marvell Yukon 88E8035 Fast Ethernet" }, 195 { VENDORID_MARVELL, DEVICEID_MRVL_8036, 196 "Marvell Yukon 88E8036 Fast Ethernet" }, 197 { VENDORID_MARVELL, DEVICEID_MRVL_8038, 198 "Marvell Yukon 88E8038 Fast Ethernet" }, 199 { VENDORID_MARVELL, DEVICEID_MRVL_8039, 200 "Marvell Yukon 88E8039 Fast Ethernet" }, 201 { VENDORID_MARVELL, DEVICEID_MRVL_8040, 202 "Marvell Yukon 88E8040 Fast Ethernet" }, 203 { VENDORID_MARVELL, DEVICEID_MRVL_8040T, 204 "Marvell Yukon 88E8040T Fast Ethernet" }, 205 { VENDORID_MARVELL, DEVICEID_MRVL_8042, 206 "Marvell Yukon 88E8042 Fast Ethernet" }, 207 { VENDORID_MARVELL, DEVICEID_MRVL_8048, 208 "Marvell Yukon 88E8048 Fast Ethernet" }, 209 { VENDORID_MARVELL, DEVICEID_MRVL_4361, 210 "Marvell Yukon 88E8050 Gigabit Ethernet" }, 211 { VENDORID_MARVELL, DEVICEID_MRVL_4360, 212 "Marvell Yukon 88E8052 Gigabit Ethernet" }, 213 { VENDORID_MARVELL, DEVICEID_MRVL_4362, 214 "Marvell Yukon 88E8053 Gigabit Ethernet" }, 215 { VENDORID_MARVELL, DEVICEID_MRVL_4363, 216 "Marvell Yukon 88E8055 Gigabit Ethernet" }, 217 { VENDORID_MARVELL, DEVICEID_MRVL_4364, 218 "Marvell Yukon 88E8056 Gigabit Ethernet" }, 219 { VENDORID_MARVELL, DEVICEID_MRVL_4365, 220 "Marvell Yukon 88E8070 Gigabit Ethernet" }, 221 { VENDORID_MARVELL, DEVICEID_MRVL_436A, 222 "Marvell Yukon 88E8058 Gigabit Ethernet" }, 223 { VENDORID_MARVELL, DEVICEID_MRVL_436B, 224 "Marvell Yukon 88E8071 Gigabit Ethernet" }, 225 { VENDORID_MARVELL, DEVICEID_MRVL_436C, 226 "Marvell Yukon 88E8072 Gigabit Ethernet" }, 227 { VENDORID_MARVELL, DEVICEID_MRVL_436D, 228 "Marvell Yukon 88E8055 Gigabit Ethernet" }, 229 { VENDORID_MARVELL, DEVICEID_MRVL_4370, 230 "Marvell Yukon 88E8075 Gigabit Ethernet" }, 231 { VENDORID_MARVELL, DEVICEID_MRVL_4380, 232 "Marvell Yukon 88E8057 Gigabit Ethernet" }, 233 { VENDORID_MARVELL, DEVICEID_MRVL_4381, 234 "Marvell Yukon 88E8059 Gigabit Ethernet" }, 235 { VENDORID_DLINK, DEVICEID_DLINK_DGE550SX, 236 "D-Link 550SX Gigabit Ethernet" }, 237 { VENDORID_DLINK, DEVICEID_DLINK_DGE560SX, 238 "D-Link 560SX Gigabit Ethernet" }, 239 { VENDORID_DLINK, DEVICEID_DLINK_DGE560T, 240 "D-Link 560T Gigabit Ethernet" } 241 }; 242 243 static const char *model_name[] = { 244 "Yukon XL", 245 "Yukon EC Ultra", 246 "Yukon EX", 247 "Yukon EC", 248 "Yukon FE", 249 "Yukon FE+", 250 "Yukon Supreme", 251 "Yukon Ultra 2", 252 "Yukon Unknown", 253 "Yukon Optima", 254 }; 255 256 static int mskc_probe(device_t); 257 static int mskc_attach(device_t); 258 static int mskc_detach(device_t); 259 static int mskc_shutdown(device_t); 260 static int mskc_setup_rambuffer(struct msk_softc *); 261 static int mskc_suspend(device_t); 262 static int mskc_resume(device_t); 263 static bus_dma_tag_t mskc_get_dma_tag(device_t, device_t); 264 static void mskc_reset(struct msk_softc *); 265 266 static int msk_probe(device_t); 267 static int msk_attach(device_t); 268 static int msk_detach(device_t); 269 270 static void msk_tick(void *); 271 static void msk_intr(void *); 272 static void msk_intr_phy(struct msk_if_softc *); 273 static void msk_intr_gmac(struct msk_if_softc *); 274 static __inline void msk_rxput(struct msk_if_softc *); 275 static int msk_handle_events(struct msk_softc *); 276 static void msk_handle_hwerr(struct msk_if_softc *, uint32_t); 277 static void msk_intr_hwerr(struct msk_softc *); 278 #ifndef __NO_STRICT_ALIGNMENT 279 static __inline void msk_fixup_rx(struct mbuf *); 280 #endif 281 static __inline void msk_rxcsum(struct msk_if_softc *, uint32_t, struct mbuf *); 282 static void msk_rxeof(struct msk_if_softc *, uint32_t, uint32_t, int); 283 static void msk_jumbo_rxeof(struct msk_if_softc *, uint32_t, uint32_t, int); 284 static void msk_txeof(struct msk_if_softc *, int); 285 static int msk_encap(struct msk_if_softc *, struct mbuf **); 286 static void msk_start(if_t); 287 static void msk_start_locked(if_t); 288 static int msk_ioctl(if_t, u_long, caddr_t); 289 static void msk_set_prefetch(struct msk_softc *, int, bus_addr_t, uint32_t); 290 static void msk_set_rambuffer(struct msk_if_softc *); 291 static void msk_set_tx_stfwd(struct msk_if_softc *); 292 static void msk_init(void *); 293 static void msk_init_locked(struct msk_if_softc *); 294 static void msk_stop(struct msk_if_softc *); 295 static void msk_watchdog(struct msk_if_softc *); 296 static int msk_mediachange(if_t); 297 static void msk_mediastatus(if_t, struct ifmediareq *); 298 static void msk_phy_power(struct msk_softc *, int); 299 static void msk_dmamap_cb(void *, bus_dma_segment_t *, int, int); 300 static int msk_status_dma_alloc(struct msk_softc *); 301 static void msk_status_dma_free(struct msk_softc *); 302 static int msk_txrx_dma_alloc(struct msk_if_softc *); 303 static int msk_rx_dma_jalloc(struct msk_if_softc *); 304 static void msk_txrx_dma_free(struct msk_if_softc *); 305 static void msk_rx_dma_jfree(struct msk_if_softc *); 306 static int msk_rx_fill(struct msk_if_softc *, int); 307 static int msk_init_rx_ring(struct msk_if_softc *); 308 static int msk_init_jumbo_rx_ring(struct msk_if_softc *); 309 static void msk_init_tx_ring(struct msk_if_softc *); 310 static __inline void msk_discard_rxbuf(struct msk_if_softc *, int); 311 static __inline void msk_discard_jumbo_rxbuf(struct msk_if_softc *, int); 312 static int msk_newbuf(struct msk_if_softc *, int); 313 static int msk_jumbo_newbuf(struct msk_if_softc *, int); 314 315 static int msk_phy_readreg(struct msk_if_softc *, int, int); 316 static int msk_phy_writereg(struct msk_if_softc *, int, int, int); 317 static int msk_miibus_readreg(device_t, int, int); 318 static int msk_miibus_writereg(device_t, int, int, int); 319 static void msk_miibus_statchg(device_t); 320 321 static void msk_rxfilter(struct msk_if_softc *); 322 static void msk_setvlan(struct msk_if_softc *, if_t); 323 324 static void msk_stats_clear(struct msk_if_softc *); 325 static void msk_stats_update(struct msk_if_softc *); 326 static int msk_sysctl_stat32(SYSCTL_HANDLER_ARGS); 327 static int msk_sysctl_stat64(SYSCTL_HANDLER_ARGS); 328 static void msk_sysctl_node(struct msk_if_softc *); 329 static int sysctl_int_range(SYSCTL_HANDLER_ARGS, int, int); 330 static int sysctl_hw_msk_proc_limit(SYSCTL_HANDLER_ARGS); 331 332 static device_method_t mskc_methods[] = { 333 /* Device interface */ 334 DEVMETHOD(device_probe, mskc_probe), 335 DEVMETHOD(device_attach, mskc_attach), 336 DEVMETHOD(device_detach, mskc_detach), 337 DEVMETHOD(device_suspend, mskc_suspend), 338 DEVMETHOD(device_resume, mskc_resume), 339 DEVMETHOD(device_shutdown, mskc_shutdown), 340 341 DEVMETHOD(bus_get_dma_tag, mskc_get_dma_tag), 342 343 DEVMETHOD_END 344 }; 345 346 static driver_t mskc_driver = { 347 "mskc", 348 mskc_methods, 349 sizeof(struct msk_softc) 350 }; 351 352 static device_method_t msk_methods[] = { 353 /* Device interface */ 354 DEVMETHOD(device_probe, msk_probe), 355 DEVMETHOD(device_attach, msk_attach), 356 DEVMETHOD(device_detach, msk_detach), 357 DEVMETHOD(device_shutdown, bus_generic_shutdown), 358 359 /* MII interface */ 360 DEVMETHOD(miibus_readreg, msk_miibus_readreg), 361 DEVMETHOD(miibus_writereg, msk_miibus_writereg), 362 DEVMETHOD(miibus_statchg, msk_miibus_statchg), 363 364 DEVMETHOD_END 365 }; 366 367 static driver_t msk_driver = { 368 "msk", 369 msk_methods, 370 sizeof(struct msk_if_softc) 371 }; 372 373 DRIVER_MODULE(mskc, pci, mskc_driver, NULL, NULL); 374 DRIVER_MODULE(msk, mskc, msk_driver, NULL, NULL); 375 DRIVER_MODULE(miibus, msk, miibus_driver, NULL, NULL); 376 377 static struct resource_spec msk_res_spec_io[] = { 378 { SYS_RES_IOPORT, PCIR_BAR(1), RF_ACTIVE }, 379 { -1, 0, 0 } 380 }; 381 382 static struct resource_spec msk_res_spec_mem[] = { 383 { SYS_RES_MEMORY, PCIR_BAR(0), RF_ACTIVE }, 384 { -1, 0, 0 } 385 }; 386 387 static struct resource_spec msk_irq_spec_legacy[] = { 388 { SYS_RES_IRQ, 0, RF_ACTIVE | RF_SHAREABLE }, 389 { -1, 0, 0 } 390 }; 391 392 static struct resource_spec msk_irq_spec_msi[] = { 393 { SYS_RES_IRQ, 1, RF_ACTIVE }, 394 { -1, 0, 0 } 395 }; 396 397 static int 398 msk_miibus_readreg(device_t dev, int phy, int reg) 399 { 400 struct msk_if_softc *sc_if; 401 402 sc_if = device_get_softc(dev); 403 404 return (msk_phy_readreg(sc_if, phy, reg)); 405 } 406 407 static int 408 msk_phy_readreg(struct msk_if_softc *sc_if, int phy, int reg) 409 { 410 struct msk_softc *sc; 411 int i, val; 412 413 sc = sc_if->msk_softc; 414 415 GMAC_WRITE_2(sc, sc_if->msk_port, GM_SMI_CTRL, 416 GM_SMI_CT_PHY_AD(phy) | GM_SMI_CT_REG_AD(reg) | GM_SMI_CT_OP_RD); 417 418 for (i = 0; i < MSK_TIMEOUT; i++) { 419 DELAY(1); 420 val = GMAC_READ_2(sc, sc_if->msk_port, GM_SMI_CTRL); 421 if ((val & GM_SMI_CT_RD_VAL) != 0) { 422 val = GMAC_READ_2(sc, sc_if->msk_port, GM_SMI_DATA); 423 break; 424 } 425 } 426 427 if (i == MSK_TIMEOUT) { 428 if_printf(sc_if->msk_ifp, "phy failed to come ready\n"); 429 val = 0; 430 } 431 432 return (val); 433 } 434 435 static int 436 msk_miibus_writereg(device_t dev, int phy, int reg, int val) 437 { 438 struct msk_if_softc *sc_if; 439 440 sc_if = device_get_softc(dev); 441 442 return (msk_phy_writereg(sc_if, phy, reg, val)); 443 } 444 445 static int 446 msk_phy_writereg(struct msk_if_softc *sc_if, int phy, int reg, int val) 447 { 448 struct msk_softc *sc; 449 int i; 450 451 sc = sc_if->msk_softc; 452 453 GMAC_WRITE_2(sc, sc_if->msk_port, GM_SMI_DATA, val); 454 GMAC_WRITE_2(sc, sc_if->msk_port, GM_SMI_CTRL, 455 GM_SMI_CT_PHY_AD(phy) | GM_SMI_CT_REG_AD(reg)); 456 for (i = 0; i < MSK_TIMEOUT; i++) { 457 DELAY(1); 458 if ((GMAC_READ_2(sc, sc_if->msk_port, GM_SMI_CTRL) & 459 GM_SMI_CT_BUSY) == 0) 460 break; 461 } 462 if (i == MSK_TIMEOUT) 463 if_printf(sc_if->msk_ifp, "phy write timeout\n"); 464 465 return (0); 466 } 467 468 static void 469 msk_miibus_statchg(device_t dev) 470 { 471 struct msk_softc *sc; 472 struct msk_if_softc *sc_if; 473 struct mii_data *mii; 474 if_t ifp; 475 uint32_t gmac; 476 477 sc_if = device_get_softc(dev); 478 sc = sc_if->msk_softc; 479 480 MSK_IF_LOCK_ASSERT(sc_if); 481 482 mii = device_get_softc(sc_if->msk_miibus); 483 ifp = sc_if->msk_ifp; 484 if (mii == NULL || ifp == NULL || 485 (if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0) 486 return; 487 488 sc_if->msk_flags &= ~MSK_FLAG_LINK; 489 if ((mii->mii_media_status & (IFM_AVALID | IFM_ACTIVE)) == 490 (IFM_AVALID | IFM_ACTIVE)) { 491 switch (IFM_SUBTYPE(mii->mii_media_active)) { 492 case IFM_10_T: 493 case IFM_100_TX: 494 sc_if->msk_flags |= MSK_FLAG_LINK; 495 break; 496 case IFM_1000_T: 497 case IFM_1000_SX: 498 case IFM_1000_LX: 499 case IFM_1000_CX: 500 if ((sc_if->msk_flags & MSK_FLAG_FASTETHER) == 0) 501 sc_if->msk_flags |= MSK_FLAG_LINK; 502 break; 503 default: 504 break; 505 } 506 } 507 508 if ((sc_if->msk_flags & MSK_FLAG_LINK) != 0) { 509 /* Enable Tx FIFO Underrun. */ 510 CSR_WRITE_1(sc, MR_ADDR(sc_if->msk_port, GMAC_IRQ_MSK), 511 GM_IS_TX_FF_UR | GM_IS_RX_FF_OR); 512 /* 513 * Because mii(4) notify msk(4) that it detected link status 514 * change, there is no need to enable automatic 515 * speed/flow-control/duplex updates. 516 */ 517 gmac = GM_GPCR_AU_ALL_DIS; 518 switch (IFM_SUBTYPE(mii->mii_media_active)) { 519 case IFM_1000_SX: 520 case IFM_1000_T: 521 gmac |= GM_GPCR_SPEED_1000; 522 break; 523 case IFM_100_TX: 524 gmac |= GM_GPCR_SPEED_100; 525 break; 526 case IFM_10_T: 527 break; 528 } 529 530 if ((IFM_OPTIONS(mii->mii_media_active) & 531 IFM_ETH_RXPAUSE) == 0) 532 gmac |= GM_GPCR_FC_RX_DIS; 533 if ((IFM_OPTIONS(mii->mii_media_active) & 534 IFM_ETH_TXPAUSE) == 0) 535 gmac |= GM_GPCR_FC_TX_DIS; 536 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) 537 gmac |= GM_GPCR_DUP_FULL; 538 else 539 gmac |= GM_GPCR_FC_RX_DIS | GM_GPCR_FC_TX_DIS; 540 gmac |= GM_GPCR_RX_ENA | GM_GPCR_TX_ENA; 541 GMAC_WRITE_2(sc, sc_if->msk_port, GM_GP_CTRL, gmac); 542 /* Read again to ensure writing. */ 543 GMAC_READ_2(sc, sc_if->msk_port, GM_GP_CTRL); 544 gmac = GMC_PAUSE_OFF; 545 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) { 546 if ((IFM_OPTIONS(mii->mii_media_active) & 547 IFM_ETH_RXPAUSE) != 0) 548 gmac = GMC_PAUSE_ON; 549 } 550 CSR_WRITE_4(sc, MR_ADDR(sc_if->msk_port, GMAC_CTRL), gmac); 551 552 /* Enable PHY interrupt for FIFO underrun/overflow. */ 553 msk_phy_writereg(sc_if, PHY_ADDR_MARV, 554 PHY_MARV_INT_MASK, PHY_M_IS_FIFO_ERROR); 555 } else { 556 /* 557 * Link state changed to down. 558 * Disable PHY interrupts. 559 */ 560 msk_phy_writereg(sc_if, PHY_ADDR_MARV, PHY_MARV_INT_MASK, 0); 561 /* Disable Rx/Tx MAC. */ 562 gmac = GMAC_READ_2(sc, sc_if->msk_port, GM_GP_CTRL); 563 if ((gmac & (GM_GPCR_RX_ENA | GM_GPCR_TX_ENA)) != 0) { 564 gmac &= ~(GM_GPCR_RX_ENA | GM_GPCR_TX_ENA); 565 GMAC_WRITE_2(sc, sc_if->msk_port, GM_GP_CTRL, gmac); 566 /* Read again to ensure writing. */ 567 GMAC_READ_2(sc, sc_if->msk_port, GM_GP_CTRL); 568 } 569 } 570 } 571 572 static u_int 573 msk_hash_maddr(void *arg, struct sockaddr_dl *sdl, u_int cnt) 574 { 575 uint32_t *mchash = arg; 576 uint32_t crc; 577 578 crc = ether_crc32_be(LLADDR(sdl), ETHER_ADDR_LEN); 579 /* Just want the 6 least significant bits. */ 580 crc &= 0x3f; 581 /* Set the corresponding bit in the hash table. */ 582 mchash[crc >> 5] |= 1 << (crc & 0x1f); 583 584 return (1); 585 } 586 587 static void 588 msk_rxfilter(struct msk_if_softc *sc_if) 589 { 590 struct msk_softc *sc; 591 if_t ifp; 592 uint32_t mchash[2]; 593 uint16_t mode; 594 595 sc = sc_if->msk_softc; 596 597 MSK_IF_LOCK_ASSERT(sc_if); 598 599 ifp = sc_if->msk_ifp; 600 601 bzero(mchash, sizeof(mchash)); 602 mode = GMAC_READ_2(sc, sc_if->msk_port, GM_RX_CTRL); 603 if ((if_getflags(ifp) & IFF_PROMISC) != 0) 604 mode &= ~(GM_RXCR_UCF_ENA | GM_RXCR_MCF_ENA); 605 else if ((if_getflags(ifp) & IFF_ALLMULTI) != 0) { 606 mode |= GM_RXCR_UCF_ENA | GM_RXCR_MCF_ENA; 607 mchash[0] = 0xffff; 608 mchash[1] = 0xffff; 609 } else { 610 mode |= GM_RXCR_UCF_ENA; 611 if_foreach_llmaddr(ifp, msk_hash_maddr, mchash); 612 if (mchash[0] != 0 || mchash[1] != 0) 613 mode |= GM_RXCR_MCF_ENA; 614 } 615 616 GMAC_WRITE_2(sc, sc_if->msk_port, GM_MC_ADDR_H1, 617 mchash[0] & 0xffff); 618 GMAC_WRITE_2(sc, sc_if->msk_port, GM_MC_ADDR_H2, 619 (mchash[0] >> 16) & 0xffff); 620 GMAC_WRITE_2(sc, sc_if->msk_port, GM_MC_ADDR_H3, 621 mchash[1] & 0xffff); 622 GMAC_WRITE_2(sc, sc_if->msk_port, GM_MC_ADDR_H4, 623 (mchash[1] >> 16) & 0xffff); 624 GMAC_WRITE_2(sc, sc_if->msk_port, GM_RX_CTRL, mode); 625 } 626 627 static void 628 msk_setvlan(struct msk_if_softc *sc_if, if_t ifp) 629 { 630 struct msk_softc *sc; 631 632 sc = sc_if->msk_softc; 633 if ((if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING) != 0) { 634 CSR_WRITE_4(sc, MR_ADDR(sc_if->msk_port, RX_GMF_CTRL_T), 635 RX_VLAN_STRIP_ON); 636 CSR_WRITE_4(sc, MR_ADDR(sc_if->msk_port, TX_GMF_CTRL_T), 637 TX_VLAN_TAG_ON); 638 } else { 639 CSR_WRITE_4(sc, MR_ADDR(sc_if->msk_port, RX_GMF_CTRL_T), 640 RX_VLAN_STRIP_OFF); 641 CSR_WRITE_4(sc, MR_ADDR(sc_if->msk_port, TX_GMF_CTRL_T), 642 TX_VLAN_TAG_OFF); 643 } 644 } 645 646 static int 647 msk_rx_fill(struct msk_if_softc *sc_if, int jumbo) 648 { 649 uint16_t idx; 650 int i; 651 652 if ((sc_if->msk_flags & MSK_FLAG_DESCV2) == 0 && 653 (if_getcapenable(sc_if->msk_ifp) & IFCAP_RXCSUM) != 0) { 654 /* Wait until controller executes OP_TCPSTART command. */ 655 for (i = 100; i > 0; i--) { 656 DELAY(100); 657 idx = CSR_READ_2(sc_if->msk_softc, 658 Y2_PREF_Q_ADDR(sc_if->msk_rxq, 659 PREF_UNIT_GET_IDX_REG)); 660 if (idx != 0) 661 break; 662 } 663 if (i == 0) { 664 device_printf(sc_if->msk_if_dev, 665 "prefetch unit stuck?\n"); 666 return (ETIMEDOUT); 667 } 668 /* 669 * Fill consumed LE with free buffer. This can be done 670 * in Rx handler but we don't want to add special code 671 * in fast handler. 672 */ 673 if (jumbo > 0) { 674 if (msk_jumbo_newbuf(sc_if, 0) != 0) 675 return (ENOBUFS); 676 bus_dmamap_sync(sc_if->msk_cdata.msk_jumbo_rx_ring_tag, 677 sc_if->msk_cdata.msk_jumbo_rx_ring_map, 678 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 679 } else { 680 if (msk_newbuf(sc_if, 0) != 0) 681 return (ENOBUFS); 682 bus_dmamap_sync(sc_if->msk_cdata.msk_rx_ring_tag, 683 sc_if->msk_cdata.msk_rx_ring_map, 684 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 685 } 686 sc_if->msk_cdata.msk_rx_prod = 0; 687 CSR_WRITE_2(sc_if->msk_softc, 688 Y2_PREF_Q_ADDR(sc_if->msk_rxq, PREF_UNIT_PUT_IDX_REG), 689 sc_if->msk_cdata.msk_rx_prod); 690 } 691 return (0); 692 } 693 694 static int 695 msk_init_rx_ring(struct msk_if_softc *sc_if) 696 { 697 struct msk_ring_data *rd; 698 struct msk_rxdesc *rxd; 699 int i, nbuf, prod; 700 701 MSK_IF_LOCK_ASSERT(sc_if); 702 703 sc_if->msk_cdata.msk_rx_cons = 0; 704 sc_if->msk_cdata.msk_rx_prod = 0; 705 sc_if->msk_cdata.msk_rx_putwm = MSK_PUT_WM; 706 707 rd = &sc_if->msk_rdata; 708 bzero(rd->msk_rx_ring, sizeof(struct msk_rx_desc) * MSK_RX_RING_CNT); 709 for (i = prod = 0; i < MSK_RX_RING_CNT; i++) { 710 rxd = &sc_if->msk_cdata.msk_rxdesc[prod]; 711 rxd->rx_m = NULL; 712 rxd->rx_le = &rd->msk_rx_ring[prod]; 713 MSK_INC(prod, MSK_RX_RING_CNT); 714 } 715 nbuf = MSK_RX_BUF_CNT; 716 prod = 0; 717 /* Have controller know how to compute Rx checksum. */ 718 if ((sc_if->msk_flags & MSK_FLAG_DESCV2) == 0 && 719 (if_getcapenable(sc_if->msk_ifp) & IFCAP_RXCSUM) != 0) { 720 #ifdef MSK_64BIT_DMA 721 rxd = &sc_if->msk_cdata.msk_rxdesc[prod]; 722 rxd->rx_m = NULL; 723 rxd->rx_le = &rd->msk_rx_ring[prod]; 724 rxd->rx_le->msk_addr = htole32(ETHER_HDR_LEN << 16 | 725 ETHER_HDR_LEN); 726 rxd->rx_le->msk_control = htole32(OP_TCPSTART | HW_OWNER); 727 MSK_INC(prod, MSK_RX_RING_CNT); 728 MSK_INC(sc_if->msk_cdata.msk_rx_cons, MSK_RX_RING_CNT); 729 #endif 730 rxd = &sc_if->msk_cdata.msk_rxdesc[prod]; 731 rxd->rx_m = NULL; 732 rxd->rx_le = &rd->msk_rx_ring[prod]; 733 rxd->rx_le->msk_addr = htole32(ETHER_HDR_LEN << 16 | 734 ETHER_HDR_LEN); 735 rxd->rx_le->msk_control = htole32(OP_TCPSTART | HW_OWNER); 736 MSK_INC(prod, MSK_RX_RING_CNT); 737 MSK_INC(sc_if->msk_cdata.msk_rx_cons, MSK_RX_RING_CNT); 738 nbuf--; 739 } 740 for (i = 0; i < nbuf; i++) { 741 if (msk_newbuf(sc_if, prod) != 0) 742 return (ENOBUFS); 743 MSK_RX_INC(prod, MSK_RX_RING_CNT); 744 } 745 746 bus_dmamap_sync(sc_if->msk_cdata.msk_rx_ring_tag, 747 sc_if->msk_cdata.msk_rx_ring_map, 748 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 749 750 /* Update prefetch unit. */ 751 sc_if->msk_cdata.msk_rx_prod = prod; 752 CSR_WRITE_2(sc_if->msk_softc, 753 Y2_PREF_Q_ADDR(sc_if->msk_rxq, PREF_UNIT_PUT_IDX_REG), 754 (sc_if->msk_cdata.msk_rx_prod + MSK_RX_RING_CNT - 1) % 755 MSK_RX_RING_CNT); 756 if (msk_rx_fill(sc_if, 0) != 0) 757 return (ENOBUFS); 758 return (0); 759 } 760 761 static int 762 msk_init_jumbo_rx_ring(struct msk_if_softc *sc_if) 763 { 764 struct msk_ring_data *rd; 765 struct msk_rxdesc *rxd; 766 int i, nbuf, prod; 767 768 MSK_IF_LOCK_ASSERT(sc_if); 769 770 sc_if->msk_cdata.msk_rx_cons = 0; 771 sc_if->msk_cdata.msk_rx_prod = 0; 772 sc_if->msk_cdata.msk_rx_putwm = MSK_PUT_WM; 773 774 rd = &sc_if->msk_rdata; 775 bzero(rd->msk_jumbo_rx_ring, 776 sizeof(struct msk_rx_desc) * MSK_JUMBO_RX_RING_CNT); 777 for (i = prod = 0; i < MSK_JUMBO_RX_RING_CNT; i++) { 778 rxd = &sc_if->msk_cdata.msk_jumbo_rxdesc[prod]; 779 rxd->rx_m = NULL; 780 rxd->rx_le = &rd->msk_jumbo_rx_ring[prod]; 781 MSK_INC(prod, MSK_JUMBO_RX_RING_CNT); 782 } 783 nbuf = MSK_RX_BUF_CNT; 784 prod = 0; 785 /* Have controller know how to compute Rx checksum. */ 786 if ((sc_if->msk_flags & MSK_FLAG_DESCV2) == 0 && 787 (if_getcapenable(sc_if->msk_ifp) & IFCAP_RXCSUM) != 0) { 788 #ifdef MSK_64BIT_DMA 789 rxd = &sc_if->msk_cdata.msk_jumbo_rxdesc[prod]; 790 rxd->rx_m = NULL; 791 rxd->rx_le = &rd->msk_jumbo_rx_ring[prod]; 792 rxd->rx_le->msk_addr = htole32(ETHER_HDR_LEN << 16 | 793 ETHER_HDR_LEN); 794 rxd->rx_le->msk_control = htole32(OP_TCPSTART | HW_OWNER); 795 MSK_INC(prod, MSK_JUMBO_RX_RING_CNT); 796 MSK_INC(sc_if->msk_cdata.msk_rx_cons, MSK_JUMBO_RX_RING_CNT); 797 #endif 798 rxd = &sc_if->msk_cdata.msk_jumbo_rxdesc[prod]; 799 rxd->rx_m = NULL; 800 rxd->rx_le = &rd->msk_jumbo_rx_ring[prod]; 801 rxd->rx_le->msk_addr = htole32(ETHER_HDR_LEN << 16 | 802 ETHER_HDR_LEN); 803 rxd->rx_le->msk_control = htole32(OP_TCPSTART | HW_OWNER); 804 MSK_INC(prod, MSK_JUMBO_RX_RING_CNT); 805 MSK_INC(sc_if->msk_cdata.msk_rx_cons, MSK_JUMBO_RX_RING_CNT); 806 nbuf--; 807 } 808 for (i = 0; i < nbuf; i++) { 809 if (msk_jumbo_newbuf(sc_if, prod) != 0) 810 return (ENOBUFS); 811 MSK_RX_INC(prod, MSK_JUMBO_RX_RING_CNT); 812 } 813 814 bus_dmamap_sync(sc_if->msk_cdata.msk_jumbo_rx_ring_tag, 815 sc_if->msk_cdata.msk_jumbo_rx_ring_map, 816 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 817 818 /* Update prefetch unit. */ 819 sc_if->msk_cdata.msk_rx_prod = prod; 820 CSR_WRITE_2(sc_if->msk_softc, 821 Y2_PREF_Q_ADDR(sc_if->msk_rxq, PREF_UNIT_PUT_IDX_REG), 822 (sc_if->msk_cdata.msk_rx_prod + MSK_JUMBO_RX_RING_CNT - 1) % 823 MSK_JUMBO_RX_RING_CNT); 824 if (msk_rx_fill(sc_if, 1) != 0) 825 return (ENOBUFS); 826 return (0); 827 } 828 829 static void 830 msk_init_tx_ring(struct msk_if_softc *sc_if) 831 { 832 struct msk_ring_data *rd; 833 struct msk_txdesc *txd; 834 int i; 835 836 sc_if->msk_cdata.msk_tso_mtu = 0; 837 sc_if->msk_cdata.msk_last_csum = 0; 838 sc_if->msk_cdata.msk_tx_prod = 0; 839 sc_if->msk_cdata.msk_tx_cons = 0; 840 sc_if->msk_cdata.msk_tx_cnt = 0; 841 sc_if->msk_cdata.msk_tx_high_addr = 0; 842 843 rd = &sc_if->msk_rdata; 844 bzero(rd->msk_tx_ring, sizeof(struct msk_tx_desc) * MSK_TX_RING_CNT); 845 for (i = 0; i < MSK_TX_RING_CNT; i++) { 846 txd = &sc_if->msk_cdata.msk_txdesc[i]; 847 txd->tx_m = NULL; 848 txd->tx_le = &rd->msk_tx_ring[i]; 849 } 850 851 bus_dmamap_sync(sc_if->msk_cdata.msk_tx_ring_tag, 852 sc_if->msk_cdata.msk_tx_ring_map, 853 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 854 } 855 856 static __inline void 857 msk_discard_rxbuf(struct msk_if_softc *sc_if, int idx) 858 { 859 struct msk_rx_desc *rx_le; 860 struct msk_rxdesc *rxd; 861 struct mbuf *m; 862 863 #ifdef MSK_64BIT_DMA 864 rxd = &sc_if->msk_cdata.msk_rxdesc[idx]; 865 rx_le = rxd->rx_le; 866 rx_le->msk_control = htole32(OP_ADDR64 | HW_OWNER); 867 MSK_INC(idx, MSK_RX_RING_CNT); 868 #endif 869 rxd = &sc_if->msk_cdata.msk_rxdesc[idx]; 870 m = rxd->rx_m; 871 rx_le = rxd->rx_le; 872 rx_le->msk_control = htole32(m->m_len | OP_PACKET | HW_OWNER); 873 } 874 875 static __inline void 876 msk_discard_jumbo_rxbuf(struct msk_if_softc *sc_if, int idx) 877 { 878 struct msk_rx_desc *rx_le; 879 struct msk_rxdesc *rxd; 880 struct mbuf *m; 881 882 #ifdef MSK_64BIT_DMA 883 rxd = &sc_if->msk_cdata.msk_jumbo_rxdesc[idx]; 884 rx_le = rxd->rx_le; 885 rx_le->msk_control = htole32(OP_ADDR64 | HW_OWNER); 886 MSK_INC(idx, MSK_JUMBO_RX_RING_CNT); 887 #endif 888 rxd = &sc_if->msk_cdata.msk_jumbo_rxdesc[idx]; 889 m = rxd->rx_m; 890 rx_le = rxd->rx_le; 891 rx_le->msk_control = htole32(m->m_len | OP_PACKET | HW_OWNER); 892 } 893 894 static int 895 msk_newbuf(struct msk_if_softc *sc_if, int idx) 896 { 897 struct msk_rx_desc *rx_le; 898 struct msk_rxdesc *rxd; 899 struct mbuf *m; 900 bus_dma_segment_t segs[1]; 901 bus_dmamap_t map; 902 int nsegs; 903 904 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 905 if (m == NULL) 906 return (ENOBUFS); 907 908 m->m_len = m->m_pkthdr.len = MCLBYTES; 909 if ((sc_if->msk_flags & MSK_FLAG_RAMBUF) == 0) 910 m_adj(m, ETHER_ALIGN); 911 #ifndef __NO_STRICT_ALIGNMENT 912 else 913 m_adj(m, MSK_RX_BUF_ALIGN); 914 #endif 915 916 if (bus_dmamap_load_mbuf_sg(sc_if->msk_cdata.msk_rx_tag, 917 sc_if->msk_cdata.msk_rx_sparemap, m, segs, &nsegs, 918 BUS_DMA_NOWAIT) != 0) { 919 m_freem(m); 920 return (ENOBUFS); 921 } 922 KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs)); 923 924 rxd = &sc_if->msk_cdata.msk_rxdesc[idx]; 925 #ifdef MSK_64BIT_DMA 926 rx_le = rxd->rx_le; 927 rx_le->msk_addr = htole32(MSK_ADDR_HI(segs[0].ds_addr)); 928 rx_le->msk_control = htole32(OP_ADDR64 | HW_OWNER); 929 MSK_INC(idx, MSK_RX_RING_CNT); 930 rxd = &sc_if->msk_cdata.msk_rxdesc[idx]; 931 #endif 932 if (rxd->rx_m != NULL) { 933 bus_dmamap_sync(sc_if->msk_cdata.msk_rx_tag, rxd->rx_dmamap, 934 BUS_DMASYNC_POSTREAD); 935 bus_dmamap_unload(sc_if->msk_cdata.msk_rx_tag, rxd->rx_dmamap); 936 rxd->rx_m = NULL; 937 } 938 map = rxd->rx_dmamap; 939 rxd->rx_dmamap = sc_if->msk_cdata.msk_rx_sparemap; 940 sc_if->msk_cdata.msk_rx_sparemap = map; 941 bus_dmamap_sync(sc_if->msk_cdata.msk_rx_tag, rxd->rx_dmamap, 942 BUS_DMASYNC_PREREAD); 943 rxd->rx_m = m; 944 rx_le = rxd->rx_le; 945 rx_le->msk_addr = htole32(MSK_ADDR_LO(segs[0].ds_addr)); 946 rx_le->msk_control = 947 htole32(segs[0].ds_len | OP_PACKET | HW_OWNER); 948 949 return (0); 950 } 951 952 static int 953 msk_jumbo_newbuf(struct msk_if_softc *sc_if, int idx) 954 { 955 struct msk_rx_desc *rx_le; 956 struct msk_rxdesc *rxd; 957 struct mbuf *m; 958 bus_dma_segment_t segs[1]; 959 bus_dmamap_t map; 960 int nsegs; 961 962 m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUM9BYTES); 963 if (m == NULL) 964 return (ENOBUFS); 965 m->m_len = m->m_pkthdr.len = MJUM9BYTES; 966 if ((sc_if->msk_flags & MSK_FLAG_RAMBUF) == 0) 967 m_adj(m, ETHER_ALIGN); 968 #ifndef __NO_STRICT_ALIGNMENT 969 else 970 m_adj(m, MSK_RX_BUF_ALIGN); 971 #endif 972 973 if (bus_dmamap_load_mbuf_sg(sc_if->msk_cdata.msk_jumbo_rx_tag, 974 sc_if->msk_cdata.msk_jumbo_rx_sparemap, m, segs, &nsegs, 975 BUS_DMA_NOWAIT) != 0) { 976 m_freem(m); 977 return (ENOBUFS); 978 } 979 KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs)); 980 981 rxd = &sc_if->msk_cdata.msk_jumbo_rxdesc[idx]; 982 #ifdef MSK_64BIT_DMA 983 rx_le = rxd->rx_le; 984 rx_le->msk_addr = htole32(MSK_ADDR_HI(segs[0].ds_addr)); 985 rx_le->msk_control = htole32(OP_ADDR64 | HW_OWNER); 986 MSK_INC(idx, MSK_JUMBO_RX_RING_CNT); 987 rxd = &sc_if->msk_cdata.msk_jumbo_rxdesc[idx]; 988 #endif 989 if (rxd->rx_m != NULL) { 990 bus_dmamap_sync(sc_if->msk_cdata.msk_jumbo_rx_tag, 991 rxd->rx_dmamap, BUS_DMASYNC_POSTREAD); 992 bus_dmamap_unload(sc_if->msk_cdata.msk_jumbo_rx_tag, 993 rxd->rx_dmamap); 994 rxd->rx_m = NULL; 995 } 996 map = rxd->rx_dmamap; 997 rxd->rx_dmamap = sc_if->msk_cdata.msk_jumbo_rx_sparemap; 998 sc_if->msk_cdata.msk_jumbo_rx_sparemap = map; 999 bus_dmamap_sync(sc_if->msk_cdata.msk_jumbo_rx_tag, rxd->rx_dmamap, 1000 BUS_DMASYNC_PREREAD); 1001 rxd->rx_m = m; 1002 rx_le = rxd->rx_le; 1003 rx_le->msk_addr = htole32(MSK_ADDR_LO(segs[0].ds_addr)); 1004 rx_le->msk_control = 1005 htole32(segs[0].ds_len | OP_PACKET | HW_OWNER); 1006 1007 return (0); 1008 } 1009 1010 /* 1011 * Set media options. 1012 */ 1013 static int 1014 msk_mediachange(if_t ifp) 1015 { 1016 struct msk_if_softc *sc_if; 1017 struct mii_data *mii; 1018 int error; 1019 1020 sc_if = if_getsoftc(ifp); 1021 1022 MSK_IF_LOCK(sc_if); 1023 mii = device_get_softc(sc_if->msk_miibus); 1024 error = mii_mediachg(mii); 1025 MSK_IF_UNLOCK(sc_if); 1026 1027 return (error); 1028 } 1029 1030 /* 1031 * Report current media status. 1032 */ 1033 static void 1034 msk_mediastatus(if_t ifp, struct ifmediareq *ifmr) 1035 { 1036 struct msk_if_softc *sc_if; 1037 struct mii_data *mii; 1038 1039 sc_if = if_getsoftc(ifp); 1040 MSK_IF_LOCK(sc_if); 1041 if ((if_getflags(ifp) & IFF_UP) == 0) { 1042 MSK_IF_UNLOCK(sc_if); 1043 return; 1044 } 1045 mii = device_get_softc(sc_if->msk_miibus); 1046 1047 mii_pollstat(mii); 1048 ifmr->ifm_active = mii->mii_media_active; 1049 ifmr->ifm_status = mii->mii_media_status; 1050 MSK_IF_UNLOCK(sc_if); 1051 } 1052 1053 static int 1054 msk_ioctl(if_t ifp, u_long command, caddr_t data) 1055 { 1056 struct msk_if_softc *sc_if; 1057 struct ifreq *ifr; 1058 struct mii_data *mii; 1059 int error, mask, reinit; 1060 1061 sc_if = if_getsoftc(ifp); 1062 ifr = (struct ifreq *)data; 1063 error = 0; 1064 1065 switch(command) { 1066 case SIOCSIFMTU: 1067 MSK_IF_LOCK(sc_if); 1068 if (ifr->ifr_mtu > MSK_JUMBO_MTU || ifr->ifr_mtu < ETHERMIN) 1069 error = EINVAL; 1070 else if (if_getmtu(ifp) != ifr->ifr_mtu) { 1071 if (ifr->ifr_mtu > ETHERMTU) { 1072 if ((sc_if->msk_flags & MSK_FLAG_JUMBO) == 0) { 1073 error = EINVAL; 1074 MSK_IF_UNLOCK(sc_if); 1075 break; 1076 } 1077 if ((sc_if->msk_flags & 1078 MSK_FLAG_JUMBO_NOCSUM) != 0) { 1079 if_sethwassistbits(ifp, 0, 1080 MSK_CSUM_FEATURES | CSUM_TSO); 1081 if_setcapenablebit(ifp, 0, 1082 IFCAP_TSO4 | IFCAP_TXCSUM); 1083 VLAN_CAPABILITIES(ifp); 1084 } 1085 } 1086 if_setmtu(ifp, ifr->ifr_mtu); 1087 if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0) { 1088 if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING); 1089 msk_init_locked(sc_if); 1090 } 1091 } 1092 MSK_IF_UNLOCK(sc_if); 1093 break; 1094 case SIOCSIFFLAGS: 1095 MSK_IF_LOCK(sc_if); 1096 if ((if_getflags(ifp) & IFF_UP) != 0) { 1097 if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0 && 1098 ((if_getflags(ifp) ^ sc_if->msk_if_flags) & 1099 (IFF_PROMISC | IFF_ALLMULTI)) != 0) 1100 msk_rxfilter(sc_if); 1101 else if ((sc_if->msk_flags & MSK_FLAG_DETACH) == 0) 1102 msk_init_locked(sc_if); 1103 } else if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0) 1104 msk_stop(sc_if); 1105 sc_if->msk_if_flags = if_getflags(ifp); 1106 MSK_IF_UNLOCK(sc_if); 1107 break; 1108 case SIOCADDMULTI: 1109 case SIOCDELMULTI: 1110 MSK_IF_LOCK(sc_if); 1111 if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0) 1112 msk_rxfilter(sc_if); 1113 MSK_IF_UNLOCK(sc_if); 1114 break; 1115 case SIOCGIFMEDIA: 1116 case SIOCSIFMEDIA: 1117 mii = device_get_softc(sc_if->msk_miibus); 1118 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command); 1119 break; 1120 case SIOCSIFCAP: 1121 reinit = 0; 1122 MSK_IF_LOCK(sc_if); 1123 mask = ifr->ifr_reqcap ^ if_getcapenable(ifp); 1124 if ((mask & IFCAP_TXCSUM) != 0 && 1125 (IFCAP_TXCSUM & if_getcapabilities(ifp)) != 0) { 1126 if_togglecapenable(ifp, IFCAP_TXCSUM); 1127 if ((IFCAP_TXCSUM & if_getcapenable(ifp)) != 0) 1128 if_sethwassistbits(ifp, MSK_CSUM_FEATURES, 0); 1129 else 1130 if_sethwassistbits(ifp, 0, MSK_CSUM_FEATURES); 1131 } 1132 if ((mask & IFCAP_RXCSUM) != 0 && 1133 (IFCAP_RXCSUM & if_getcapabilities(ifp)) != 0) { 1134 if_togglecapenable(ifp, IFCAP_RXCSUM); 1135 if ((sc_if->msk_flags & MSK_FLAG_DESCV2) == 0) 1136 reinit = 1; 1137 } 1138 if ((mask & IFCAP_VLAN_HWCSUM) != 0 && 1139 (IFCAP_VLAN_HWCSUM & if_getcapabilities(ifp)) != 0) 1140 if_togglecapenable(ifp, IFCAP_VLAN_HWCSUM); 1141 if ((mask & IFCAP_TSO4) != 0 && 1142 (IFCAP_TSO4 & if_getcapabilities(ifp)) != 0) { 1143 if_togglecapenable(ifp, IFCAP_TSO4); 1144 if ((IFCAP_TSO4 & if_getcapenable(ifp)) != 0) 1145 if_sethwassistbits(ifp, CSUM_TSO, 0); 1146 else 1147 if_sethwassistbits(ifp, 0, CSUM_TSO); 1148 } 1149 if ((mask & IFCAP_VLAN_HWTSO) != 0 && 1150 (IFCAP_VLAN_HWTSO & if_getcapabilities(ifp)) != 0) 1151 if_togglecapenable(ifp, IFCAP_VLAN_HWTSO); 1152 if ((mask & IFCAP_VLAN_HWTAGGING) != 0 && 1153 (IFCAP_VLAN_HWTAGGING & if_getcapabilities(ifp)) != 0) { 1154 if_togglecapenable(ifp, IFCAP_VLAN_HWTAGGING); 1155 if ((IFCAP_VLAN_HWTAGGING & if_getcapenable(ifp)) == 0) 1156 if_setcapenablebit(ifp, 0, 1157 IFCAP_VLAN_HWTSO | IFCAP_VLAN_HWCSUM); 1158 msk_setvlan(sc_if, ifp); 1159 } 1160 if (if_getmtu(ifp) > ETHERMTU && 1161 (sc_if->msk_flags & MSK_FLAG_JUMBO_NOCSUM) != 0) { 1162 if_sethwassistbits(ifp, 0, (MSK_CSUM_FEATURES | CSUM_TSO)); 1163 if_setcapenablebit(ifp, 0, (IFCAP_TSO4 | IFCAP_TXCSUM)); 1164 } 1165 VLAN_CAPABILITIES(ifp); 1166 if (reinit > 0 && (if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0) { 1167 if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING); 1168 msk_init_locked(sc_if); 1169 } 1170 MSK_IF_UNLOCK(sc_if); 1171 break; 1172 default: 1173 error = ether_ioctl(ifp, command, data); 1174 break; 1175 } 1176 1177 return (error); 1178 } 1179 1180 static int 1181 mskc_probe(device_t dev) 1182 { 1183 const struct msk_product *mp; 1184 uint16_t vendor, devid; 1185 int i; 1186 1187 vendor = pci_get_vendor(dev); 1188 devid = pci_get_device(dev); 1189 mp = msk_products; 1190 for (i = 0; i < nitems(msk_products); i++, mp++) { 1191 if (vendor == mp->msk_vendorid && devid == mp->msk_deviceid) { 1192 device_set_desc(dev, mp->msk_name); 1193 return (BUS_PROBE_DEFAULT); 1194 } 1195 } 1196 1197 return (ENXIO); 1198 } 1199 1200 static int 1201 mskc_setup_rambuffer(struct msk_softc *sc) 1202 { 1203 int next; 1204 int i; 1205 1206 /* Get adapter SRAM size. */ 1207 sc->msk_ramsize = CSR_READ_1(sc, B2_E_0) * 4; 1208 if (bootverbose) 1209 device_printf(sc->msk_dev, 1210 "RAM buffer size : %dKB\n", sc->msk_ramsize); 1211 if (sc->msk_ramsize == 0) 1212 return (0); 1213 1214 sc->msk_pflags |= MSK_FLAG_RAMBUF; 1215 /* 1216 * Give receiver 2/3 of memory and round down to the multiple 1217 * of 1024. Tx/Rx RAM buffer size of Yukon II should be multiple 1218 * of 1024. 1219 */ 1220 sc->msk_rxqsize = rounddown((sc->msk_ramsize * 1024 * 2) / 3, 1024); 1221 sc->msk_txqsize = (sc->msk_ramsize * 1024) - sc->msk_rxqsize; 1222 for (i = 0, next = 0; i < sc->msk_num_port; i++) { 1223 sc->msk_rxqstart[i] = next; 1224 sc->msk_rxqend[i] = next + sc->msk_rxqsize - 1; 1225 next = sc->msk_rxqend[i] + 1; 1226 sc->msk_txqstart[i] = next; 1227 sc->msk_txqend[i] = next + sc->msk_txqsize - 1; 1228 next = sc->msk_txqend[i] + 1; 1229 if (bootverbose) { 1230 device_printf(sc->msk_dev, 1231 "Port %d : Rx Queue %dKB(0x%08x:0x%08x)\n", i, 1232 sc->msk_rxqsize / 1024, sc->msk_rxqstart[i], 1233 sc->msk_rxqend[i]); 1234 device_printf(sc->msk_dev, 1235 "Port %d : Tx Queue %dKB(0x%08x:0x%08x)\n", i, 1236 sc->msk_txqsize / 1024, sc->msk_txqstart[i], 1237 sc->msk_txqend[i]); 1238 } 1239 } 1240 1241 return (0); 1242 } 1243 1244 static void 1245 msk_phy_power(struct msk_softc *sc, int mode) 1246 { 1247 uint32_t our, val; 1248 int i; 1249 1250 switch (mode) { 1251 case MSK_PHY_POWERUP: 1252 /* Switch power to VCC (WA for VAUX problem). */ 1253 CSR_WRITE_1(sc, B0_POWER_CTRL, 1254 PC_VAUX_ENA | PC_VCC_ENA | PC_VAUX_OFF | PC_VCC_ON); 1255 /* Disable Core Clock Division, set Clock Select to 0. */ 1256 CSR_WRITE_4(sc, B2_Y2_CLK_CTRL, Y2_CLK_DIV_DIS); 1257 1258 val = 0; 1259 if (sc->msk_hw_id == CHIP_ID_YUKON_XL && 1260 sc->msk_hw_rev > CHIP_REV_YU_XL_A1) { 1261 /* Enable bits are inverted. */ 1262 val = Y2_PCI_CLK_LNK1_DIS | Y2_COR_CLK_LNK1_DIS | 1263 Y2_CLK_GAT_LNK1_DIS | Y2_PCI_CLK_LNK2_DIS | 1264 Y2_COR_CLK_LNK2_DIS | Y2_CLK_GAT_LNK2_DIS; 1265 } 1266 /* 1267 * Enable PCI & Core Clock, enable clock gating for both Links. 1268 */ 1269 CSR_WRITE_1(sc, B2_Y2_CLK_GATE, val); 1270 1271 our = CSR_PCI_READ_4(sc, PCI_OUR_REG_1); 1272 our &= ~(PCI_Y2_PHY1_POWD | PCI_Y2_PHY2_POWD); 1273 if (sc->msk_hw_id == CHIP_ID_YUKON_XL) { 1274 if (sc->msk_hw_rev > CHIP_REV_YU_XL_A1) { 1275 /* Deassert Low Power for 1st PHY. */ 1276 our |= PCI_Y2_PHY1_COMA; 1277 if (sc->msk_num_port > 1) 1278 our |= PCI_Y2_PHY2_COMA; 1279 } 1280 } 1281 if (sc->msk_hw_id == CHIP_ID_YUKON_EC_U || 1282 sc->msk_hw_id == CHIP_ID_YUKON_EX || 1283 sc->msk_hw_id >= CHIP_ID_YUKON_FE_P) { 1284 val = CSR_PCI_READ_4(sc, PCI_OUR_REG_4); 1285 val &= (PCI_FORCE_ASPM_REQUEST | 1286 PCI_ASPM_GPHY_LINK_DOWN | PCI_ASPM_INT_FIFO_EMPTY | 1287 PCI_ASPM_CLKRUN_REQUEST); 1288 /* Set all bits to 0 except bits 15..12. */ 1289 CSR_PCI_WRITE_4(sc, PCI_OUR_REG_4, val); 1290 val = CSR_PCI_READ_4(sc, PCI_OUR_REG_5); 1291 val &= PCI_CTL_TIM_VMAIN_AV_MSK; 1292 CSR_PCI_WRITE_4(sc, PCI_OUR_REG_5, val); 1293 CSR_PCI_WRITE_4(sc, PCI_CFG_REG_1, 0); 1294 CSR_WRITE_2(sc, B0_CTST, Y2_HW_WOL_ON); 1295 /* 1296 * Disable status race, workaround for 1297 * Yukon EC Ultra & Yukon EX. 1298 */ 1299 val = CSR_READ_4(sc, B2_GP_IO); 1300 val |= GLB_GPIO_STAT_RACE_DIS; 1301 CSR_WRITE_4(sc, B2_GP_IO, val); 1302 CSR_READ_4(sc, B2_GP_IO); 1303 } 1304 /* Release PHY from PowerDown/COMA mode. */ 1305 CSR_PCI_WRITE_4(sc, PCI_OUR_REG_1, our); 1306 1307 for (i = 0; i < sc->msk_num_port; i++) { 1308 CSR_WRITE_2(sc, MR_ADDR(i, GMAC_LINK_CTRL), 1309 GMLC_RST_SET); 1310 CSR_WRITE_2(sc, MR_ADDR(i, GMAC_LINK_CTRL), 1311 GMLC_RST_CLR); 1312 } 1313 break; 1314 case MSK_PHY_POWERDOWN: 1315 val = CSR_PCI_READ_4(sc, PCI_OUR_REG_1); 1316 val |= PCI_Y2_PHY1_POWD | PCI_Y2_PHY2_POWD; 1317 if (sc->msk_hw_id == CHIP_ID_YUKON_XL && 1318 sc->msk_hw_rev > CHIP_REV_YU_XL_A1) { 1319 val &= ~PCI_Y2_PHY1_COMA; 1320 if (sc->msk_num_port > 1) 1321 val &= ~PCI_Y2_PHY2_COMA; 1322 } 1323 CSR_PCI_WRITE_4(sc, PCI_OUR_REG_1, val); 1324 1325 val = Y2_PCI_CLK_LNK1_DIS | Y2_COR_CLK_LNK1_DIS | 1326 Y2_CLK_GAT_LNK1_DIS | Y2_PCI_CLK_LNK2_DIS | 1327 Y2_COR_CLK_LNK2_DIS | Y2_CLK_GAT_LNK2_DIS; 1328 if (sc->msk_hw_id == CHIP_ID_YUKON_XL && 1329 sc->msk_hw_rev > CHIP_REV_YU_XL_A1) { 1330 /* Enable bits are inverted. */ 1331 val = 0; 1332 } 1333 /* 1334 * Disable PCI & Core Clock, disable clock gating for 1335 * both Links. 1336 */ 1337 CSR_WRITE_1(sc, B2_Y2_CLK_GATE, val); 1338 CSR_WRITE_1(sc, B0_POWER_CTRL, 1339 PC_VAUX_ENA | PC_VCC_ENA | PC_VAUX_ON | PC_VCC_OFF); 1340 break; 1341 default: 1342 break; 1343 } 1344 } 1345 1346 static void 1347 mskc_reset(struct msk_softc *sc) 1348 { 1349 bus_addr_t addr; 1350 uint16_t status; 1351 uint32_t val; 1352 int i, initram; 1353 1354 /* Disable ASF. */ 1355 if (sc->msk_hw_id >= CHIP_ID_YUKON_XL && 1356 sc->msk_hw_id <= CHIP_ID_YUKON_SUPR) { 1357 if (sc->msk_hw_id == CHIP_ID_YUKON_EX || 1358 sc->msk_hw_id == CHIP_ID_YUKON_SUPR) { 1359 CSR_WRITE_4(sc, B28_Y2_CPU_WDOG, 0); 1360 status = CSR_READ_2(sc, B28_Y2_ASF_HCU_CCSR); 1361 /* Clear AHB bridge & microcontroller reset. */ 1362 status &= ~(Y2_ASF_HCU_CCSR_AHB_RST | 1363 Y2_ASF_HCU_CCSR_CPU_RST_MODE); 1364 /* Clear ASF microcontroller state. */ 1365 status &= ~Y2_ASF_HCU_CCSR_UC_STATE_MSK; 1366 status &= ~Y2_ASF_HCU_CCSR_CPU_CLK_DIVIDE_MSK; 1367 CSR_WRITE_2(sc, B28_Y2_ASF_HCU_CCSR, status); 1368 CSR_WRITE_4(sc, B28_Y2_CPU_WDOG, 0); 1369 } else 1370 CSR_WRITE_1(sc, B28_Y2_ASF_STAT_CMD, Y2_ASF_RESET); 1371 CSR_WRITE_2(sc, B0_CTST, Y2_ASF_DISABLE); 1372 /* 1373 * Since we disabled ASF, S/W reset is required for 1374 * Power Management. 1375 */ 1376 CSR_WRITE_2(sc, B0_CTST, CS_RST_SET); 1377 CSR_WRITE_2(sc, B0_CTST, CS_RST_CLR); 1378 } 1379 1380 /* Clear all error bits in the PCI status register. */ 1381 status = pci_read_config(sc->msk_dev, PCIR_STATUS, 2); 1382 CSR_WRITE_1(sc, B2_TST_CTRL1, TST_CFG_WRITE_ON); 1383 1384 pci_write_config(sc->msk_dev, PCIR_STATUS, status | 1385 PCIM_STATUS_PERR | PCIM_STATUS_SERR | PCIM_STATUS_RMABORT | 1386 PCIM_STATUS_RTABORT | PCIM_STATUS_MDPERR, 2); 1387 CSR_WRITE_2(sc, B0_CTST, CS_MRST_CLR); 1388 1389 switch (sc->msk_bustype) { 1390 case MSK_PEX_BUS: 1391 /* Clear all PEX errors. */ 1392 CSR_PCI_WRITE_4(sc, PEX_UNC_ERR_STAT, 0xffffffff); 1393 val = CSR_PCI_READ_4(sc, PEX_UNC_ERR_STAT); 1394 if ((val & PEX_RX_OV) != 0) { 1395 sc->msk_intrmask &= ~Y2_IS_HW_ERR; 1396 sc->msk_intrhwemask &= ~Y2_IS_PCI_EXP; 1397 } 1398 break; 1399 case MSK_PCI_BUS: 1400 case MSK_PCIX_BUS: 1401 /* Set Cache Line Size to 2(8bytes) if configured to 0. */ 1402 val = pci_read_config(sc->msk_dev, PCIR_CACHELNSZ, 1); 1403 if (val == 0) 1404 pci_write_config(sc->msk_dev, PCIR_CACHELNSZ, 2, 1); 1405 if (sc->msk_bustype == MSK_PCIX_BUS) { 1406 /* Set Cache Line Size opt. */ 1407 val = pci_read_config(sc->msk_dev, PCI_OUR_REG_1, 4); 1408 val |= PCI_CLS_OPT; 1409 pci_write_config(sc->msk_dev, PCI_OUR_REG_1, val, 4); 1410 } 1411 break; 1412 } 1413 /* Set PHY power state. */ 1414 msk_phy_power(sc, MSK_PHY_POWERUP); 1415 1416 /* Reset GPHY/GMAC Control */ 1417 for (i = 0; i < sc->msk_num_port; i++) { 1418 /* GPHY Control reset. */ 1419 CSR_WRITE_1(sc, MR_ADDR(i, GPHY_CTRL), GPC_RST_SET); 1420 CSR_WRITE_1(sc, MR_ADDR(i, GPHY_CTRL), GPC_RST_CLR); 1421 /* GMAC Control reset. */ 1422 CSR_WRITE_4(sc, MR_ADDR(i, GMAC_CTRL), GMC_RST_SET); 1423 CSR_WRITE_4(sc, MR_ADDR(i, GMAC_CTRL), GMC_RST_CLR); 1424 CSR_WRITE_4(sc, MR_ADDR(i, GMAC_CTRL), GMC_F_LOOPB_OFF); 1425 if (sc->msk_hw_id == CHIP_ID_YUKON_EX || 1426 sc->msk_hw_id == CHIP_ID_YUKON_SUPR) 1427 CSR_WRITE_4(sc, MR_ADDR(i, GMAC_CTRL), 1428 GMC_BYP_MACSECRX_ON | GMC_BYP_MACSECTX_ON | 1429 GMC_BYP_RETR_ON); 1430 } 1431 1432 if (sc->msk_hw_id == CHIP_ID_YUKON_SUPR && 1433 sc->msk_hw_rev > CHIP_REV_YU_SU_B0) 1434 CSR_PCI_WRITE_4(sc, PCI_OUR_REG_3, PCI_CLK_MACSEC_DIS); 1435 if (sc->msk_hw_id == CHIP_ID_YUKON_OPT && sc->msk_hw_rev == 0) { 1436 /* Disable PCIe PHY powerdown(reg 0x80, bit7). */ 1437 CSR_WRITE_4(sc, Y2_PEX_PHY_DATA, (0x0080 << 16) | 0x0080); 1438 } 1439 CSR_WRITE_1(sc, B2_TST_CTRL1, TST_CFG_WRITE_OFF); 1440 1441 /* LED On. */ 1442 CSR_WRITE_2(sc, B0_CTST, Y2_LED_STAT_ON); 1443 1444 /* Clear TWSI IRQ. */ 1445 CSR_WRITE_4(sc, B2_I2C_IRQ, I2C_CLR_IRQ); 1446 1447 /* Turn off hardware timer. */ 1448 CSR_WRITE_1(sc, B2_TI_CTRL, TIM_STOP); 1449 CSR_WRITE_1(sc, B2_TI_CTRL, TIM_CLR_IRQ); 1450 1451 /* Turn off descriptor polling. */ 1452 CSR_WRITE_1(sc, B28_DPT_CTRL, DPT_STOP); 1453 1454 /* Turn off time stamps. */ 1455 CSR_WRITE_1(sc, GMAC_TI_ST_CTRL, GMT_ST_STOP); 1456 CSR_WRITE_1(sc, GMAC_TI_ST_CTRL, GMT_ST_CLR_IRQ); 1457 1458 initram = 0; 1459 if (sc->msk_hw_id == CHIP_ID_YUKON_XL || 1460 sc->msk_hw_id == CHIP_ID_YUKON_EC || 1461 sc->msk_hw_id == CHIP_ID_YUKON_FE) 1462 initram++; 1463 1464 /* Configure timeout values. */ 1465 for (i = 0; initram > 0 && i < sc->msk_num_port; i++) { 1466 CSR_WRITE_2(sc, SELECT_RAM_BUFFER(i, B3_RI_CTRL), RI_RST_SET); 1467 CSR_WRITE_2(sc, SELECT_RAM_BUFFER(i, B3_RI_CTRL), RI_RST_CLR); 1468 CSR_WRITE_1(sc, SELECT_RAM_BUFFER(i, B3_RI_WTO_R1), 1469 MSK_RI_TO_53); 1470 CSR_WRITE_1(sc, SELECT_RAM_BUFFER(i, B3_RI_WTO_XA1), 1471 MSK_RI_TO_53); 1472 CSR_WRITE_1(sc, SELECT_RAM_BUFFER(i, B3_RI_WTO_XS1), 1473 MSK_RI_TO_53); 1474 CSR_WRITE_1(sc, SELECT_RAM_BUFFER(i, B3_RI_RTO_R1), 1475 MSK_RI_TO_53); 1476 CSR_WRITE_1(sc, SELECT_RAM_BUFFER(i, B3_RI_RTO_XA1), 1477 MSK_RI_TO_53); 1478 CSR_WRITE_1(sc, SELECT_RAM_BUFFER(i, B3_RI_RTO_XS1), 1479 MSK_RI_TO_53); 1480 CSR_WRITE_1(sc, SELECT_RAM_BUFFER(i, B3_RI_WTO_R2), 1481 MSK_RI_TO_53); 1482 CSR_WRITE_1(sc, SELECT_RAM_BUFFER(i, B3_RI_WTO_XA2), 1483 MSK_RI_TO_53); 1484 CSR_WRITE_1(sc, SELECT_RAM_BUFFER(i, B3_RI_WTO_XS2), 1485 MSK_RI_TO_53); 1486 CSR_WRITE_1(sc, SELECT_RAM_BUFFER(i, B3_RI_RTO_R2), 1487 MSK_RI_TO_53); 1488 CSR_WRITE_1(sc, SELECT_RAM_BUFFER(i, B3_RI_RTO_XA2), 1489 MSK_RI_TO_53); 1490 CSR_WRITE_1(sc, SELECT_RAM_BUFFER(i, B3_RI_RTO_XS2), 1491 MSK_RI_TO_53); 1492 } 1493 1494 /* Disable all interrupts. */ 1495 CSR_WRITE_4(sc, B0_HWE_IMSK, 0); 1496 CSR_READ_4(sc, B0_HWE_IMSK); 1497 CSR_WRITE_4(sc, B0_IMSK, 0); 1498 CSR_READ_4(sc, B0_IMSK); 1499 1500 /* 1501 * On dual port PCI-X card, there is an problem where status 1502 * can be received out of order due to split transactions. 1503 */ 1504 if (sc->msk_pcixcap != 0 && sc->msk_num_port > 1) { 1505 uint16_t pcix_cmd; 1506 1507 pcix_cmd = pci_read_config(sc->msk_dev, 1508 sc->msk_pcixcap + PCIXR_COMMAND, 2); 1509 /* Clear Max Outstanding Split Transactions. */ 1510 pcix_cmd &= ~PCIXM_COMMAND_MAX_SPLITS; 1511 CSR_WRITE_1(sc, B2_TST_CTRL1, TST_CFG_WRITE_ON); 1512 pci_write_config(sc->msk_dev, 1513 sc->msk_pcixcap + PCIXR_COMMAND, pcix_cmd, 2); 1514 CSR_WRITE_1(sc, B2_TST_CTRL1, TST_CFG_WRITE_OFF); 1515 } 1516 if (sc->msk_expcap != 0) { 1517 /* Change Max. Read Request Size to 2048 bytes. */ 1518 if (pci_get_max_read_req(sc->msk_dev) == 512) 1519 pci_set_max_read_req(sc->msk_dev, 2048); 1520 } 1521 1522 /* Clear status list. */ 1523 bzero(sc->msk_stat_ring, 1524 sizeof(struct msk_stat_desc) * sc->msk_stat_count); 1525 sc->msk_stat_cons = 0; 1526 bus_dmamap_sync(sc->msk_stat_tag, sc->msk_stat_map, 1527 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 1528 CSR_WRITE_4(sc, STAT_CTRL, SC_STAT_RST_SET); 1529 CSR_WRITE_4(sc, STAT_CTRL, SC_STAT_RST_CLR); 1530 /* Set the status list base address. */ 1531 addr = sc->msk_stat_ring_paddr; 1532 CSR_WRITE_4(sc, STAT_LIST_ADDR_LO, MSK_ADDR_LO(addr)); 1533 CSR_WRITE_4(sc, STAT_LIST_ADDR_HI, MSK_ADDR_HI(addr)); 1534 /* Set the status list last index. */ 1535 CSR_WRITE_2(sc, STAT_LAST_IDX, sc->msk_stat_count - 1); 1536 if (sc->msk_hw_id == CHIP_ID_YUKON_EC && 1537 sc->msk_hw_rev == CHIP_REV_YU_EC_A1) { 1538 /* WA for dev. #4.3 */ 1539 CSR_WRITE_2(sc, STAT_TX_IDX_TH, ST_TXTH_IDX_MASK); 1540 /* WA for dev. #4.18 */ 1541 CSR_WRITE_1(sc, STAT_FIFO_WM, 0x21); 1542 CSR_WRITE_1(sc, STAT_FIFO_ISR_WM, 0x07); 1543 } else { 1544 CSR_WRITE_2(sc, STAT_TX_IDX_TH, 0x0a); 1545 CSR_WRITE_1(sc, STAT_FIFO_WM, 0x10); 1546 if (sc->msk_hw_id == CHIP_ID_YUKON_XL && 1547 sc->msk_hw_rev == CHIP_REV_YU_XL_A0) 1548 CSR_WRITE_1(sc, STAT_FIFO_ISR_WM, 0x04); 1549 else 1550 CSR_WRITE_1(sc, STAT_FIFO_ISR_WM, 0x10); 1551 CSR_WRITE_4(sc, STAT_ISR_TIMER_INI, 0x0190); 1552 } 1553 /* 1554 * Use default value for STAT_ISR_TIMER_INI, STAT_LEV_TIMER_INI. 1555 */ 1556 CSR_WRITE_4(sc, STAT_TX_TIMER_INI, MSK_USECS(sc, 1000)); 1557 1558 /* Enable status unit. */ 1559 CSR_WRITE_4(sc, STAT_CTRL, SC_STAT_OP_ON); 1560 1561 CSR_WRITE_1(sc, STAT_TX_TIMER_CTRL, TIM_START); 1562 CSR_WRITE_1(sc, STAT_LEV_TIMER_CTRL, TIM_START); 1563 CSR_WRITE_1(sc, STAT_ISR_TIMER_CTRL, TIM_START); 1564 } 1565 1566 static int 1567 msk_probe(device_t dev) 1568 { 1569 struct msk_softc *sc; 1570 char desc[100]; 1571 1572 sc = device_get_softc(device_get_parent(dev)); 1573 /* 1574 * Not much to do here. We always know there will be 1575 * at least one GMAC present, and if there are two, 1576 * mskc_attach() will create a second device instance 1577 * for us. 1578 */ 1579 snprintf(desc, sizeof(desc), 1580 "Marvell Technology Group Ltd. %s Id 0x%02x Rev 0x%02x", 1581 model_name[sc->msk_hw_id - CHIP_ID_YUKON_XL], sc->msk_hw_id, 1582 sc->msk_hw_rev); 1583 device_set_desc_copy(dev, desc); 1584 1585 return (BUS_PROBE_DEFAULT); 1586 } 1587 1588 static int 1589 msk_attach(device_t dev) 1590 { 1591 struct msk_softc *sc; 1592 struct msk_if_softc *sc_if; 1593 if_t ifp; 1594 struct msk_mii_data *mmd; 1595 int i, port, error; 1596 uint8_t eaddr[6]; 1597 1598 if (dev == NULL) 1599 return (EINVAL); 1600 1601 error = 0; 1602 sc_if = device_get_softc(dev); 1603 sc = device_get_softc(device_get_parent(dev)); 1604 mmd = device_get_ivars(dev); 1605 port = mmd->port; 1606 1607 sc_if->msk_if_dev = dev; 1608 sc_if->msk_port = port; 1609 sc_if->msk_softc = sc; 1610 sc_if->msk_flags = sc->msk_pflags; 1611 sc->msk_if[port] = sc_if; 1612 /* Setup Tx/Rx queue register offsets. */ 1613 if (port == MSK_PORT_A) { 1614 sc_if->msk_txq = Q_XA1; 1615 sc_if->msk_txsq = Q_XS1; 1616 sc_if->msk_rxq = Q_R1; 1617 } else { 1618 sc_if->msk_txq = Q_XA2; 1619 sc_if->msk_txsq = Q_XS2; 1620 sc_if->msk_rxq = Q_R2; 1621 } 1622 1623 callout_init_mtx(&sc_if->msk_tick_ch, &sc_if->msk_softc->msk_mtx, 0); 1624 msk_sysctl_node(sc_if); 1625 1626 if ((error = msk_txrx_dma_alloc(sc_if)) != 0) 1627 goto fail; 1628 msk_rx_dma_jalloc(sc_if); 1629 1630 ifp = sc_if->msk_ifp = if_alloc(IFT_ETHER); 1631 if (ifp == NULL) { 1632 device_printf(sc_if->msk_if_dev, "can not if_alloc()\n"); 1633 error = ENOSPC; 1634 goto fail; 1635 } 1636 if_setsoftc(ifp, sc_if); 1637 if_initname(ifp, device_get_name(dev), device_get_unit(dev)); 1638 if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST); 1639 if_setcapabilities(ifp, IFCAP_TXCSUM | IFCAP_TSO4); 1640 /* 1641 * Enable Rx checksum offloading if controller supports 1642 * new descriptor formant and controller is not Yukon XL. 1643 */ 1644 if ((sc_if->msk_flags & MSK_FLAG_DESCV2) == 0 && 1645 sc->msk_hw_id != CHIP_ID_YUKON_XL) 1646 if_setcapabilitiesbit(ifp, IFCAP_RXCSUM, 0); 1647 if ((sc_if->msk_flags & MSK_FLAG_DESCV2) != 0 && 1648 (sc_if->msk_flags & MSK_FLAG_NORX_CSUM) == 0) 1649 if_setcapabilitiesbit(ifp, IFCAP_RXCSUM, 0); 1650 if_sethwassist(ifp, MSK_CSUM_FEATURES | CSUM_TSO); 1651 if_setcapenable(ifp, if_getcapabilities(ifp)); 1652 if_setioctlfn(ifp, msk_ioctl); 1653 if_setstartfn(ifp, msk_start); 1654 if_setinitfn(ifp, msk_init); 1655 if_setsendqlen(ifp, MSK_TX_RING_CNT - 1); 1656 if_setsendqready(ifp); 1657 /* 1658 * Get station address for this interface. Note that 1659 * dual port cards actually come with three station 1660 * addresses: one for each port, plus an extra. The 1661 * extra one is used by the SysKonnect driver software 1662 * as a 'virtual' station address for when both ports 1663 * are operating in failover mode. Currently we don't 1664 * use this extra address. 1665 */ 1666 MSK_IF_LOCK(sc_if); 1667 for (i = 0; i < ETHER_ADDR_LEN; i++) 1668 eaddr[i] = CSR_READ_1(sc, B2_MAC_1 + (port * 8) + i); 1669 1670 /* 1671 * Call MI attach routine. Can't hold locks when calling into ether_*. 1672 */ 1673 MSK_IF_UNLOCK(sc_if); 1674 ether_ifattach(ifp, eaddr); 1675 MSK_IF_LOCK(sc_if); 1676 1677 /* VLAN capability setup */ 1678 if_setcapabilitiesbit(ifp, IFCAP_VLAN_MTU, 0); 1679 if ((sc_if->msk_flags & MSK_FLAG_NOHWVLAN) == 0) { 1680 /* 1681 * Due to Tx checksum offload hardware bugs, msk(4) manually 1682 * computes checksum for short frames. For VLAN tagged frames 1683 * this workaround does not work so disable checksum offload 1684 * for VLAN interface. 1685 */ 1686 if_setcapabilitiesbit(ifp, IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_HWTSO, 0); 1687 /* 1688 * Enable Rx checksum offloading for VLAN tagged frames 1689 * if controller support new descriptor format. 1690 */ 1691 if ((sc_if->msk_flags & MSK_FLAG_DESCV2) != 0 && 1692 (sc_if->msk_flags & MSK_FLAG_NORX_CSUM) == 0) 1693 if_setcapabilitiesbit(ifp, IFCAP_VLAN_HWCSUM, 0); 1694 } 1695 if_setcapenable(ifp, if_getcapabilities(ifp)); 1696 /* 1697 * Disable RX checksum offloading on controllers that don't use 1698 * new descriptor format but give chance to enable it. 1699 */ 1700 if ((sc_if->msk_flags & MSK_FLAG_DESCV2) == 0) 1701 if_setcapenablebit(ifp, 0, IFCAP_RXCSUM); 1702 1703 /* 1704 * Tell the upper layer(s) we support long frames. 1705 * Must appear after the call to ether_ifattach() because 1706 * ether_ifattach() sets ifi_hdrlen to the default value. 1707 */ 1708 if_setifheaderlen(ifp, sizeof(struct ether_vlan_header)); 1709 1710 /* 1711 * Do miibus setup. 1712 */ 1713 MSK_IF_UNLOCK(sc_if); 1714 error = mii_attach(dev, &sc_if->msk_miibus, ifp, msk_mediachange, 1715 msk_mediastatus, BMSR_DEFCAPMASK, PHY_ADDR_MARV, MII_OFFSET_ANY, 1716 mmd->mii_flags); 1717 if (error != 0) { 1718 device_printf(sc_if->msk_if_dev, "attaching PHYs failed\n"); 1719 ether_ifdetach(ifp); 1720 error = ENXIO; 1721 goto fail; 1722 } 1723 1724 fail: 1725 if (error != 0) { 1726 /* Access should be ok even though lock has been dropped */ 1727 sc->msk_if[port] = NULL; 1728 msk_detach(dev); 1729 } 1730 1731 return (error); 1732 } 1733 1734 /* 1735 * Attach the interface. Allocate softc structures, do ifmedia 1736 * setup and ethernet/BPF attach. 1737 */ 1738 static int 1739 mskc_attach(device_t dev) 1740 { 1741 struct msk_softc *sc; 1742 struct msk_mii_data *mmd; 1743 int error, msic, msir, reg; 1744 1745 sc = device_get_softc(dev); 1746 sc->msk_dev = dev; 1747 mtx_init(&sc->msk_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK, 1748 MTX_DEF); 1749 1750 /* 1751 * Map control/status registers. 1752 */ 1753 pci_enable_busmaster(dev); 1754 1755 /* Allocate I/O resource */ 1756 #ifdef MSK_USEIOSPACE 1757 sc->msk_res_spec = msk_res_spec_io; 1758 #else 1759 sc->msk_res_spec = msk_res_spec_mem; 1760 #endif 1761 sc->msk_irq_spec = msk_irq_spec_legacy; 1762 error = bus_alloc_resources(dev, sc->msk_res_spec, sc->msk_res); 1763 if (error) { 1764 if (sc->msk_res_spec == msk_res_spec_mem) 1765 sc->msk_res_spec = msk_res_spec_io; 1766 else 1767 sc->msk_res_spec = msk_res_spec_mem; 1768 error = bus_alloc_resources(dev, sc->msk_res_spec, sc->msk_res); 1769 if (error) { 1770 device_printf(dev, "couldn't allocate %s resources\n", 1771 sc->msk_res_spec == msk_res_spec_mem ? "memory" : 1772 "I/O"); 1773 mtx_destroy(&sc->msk_mtx); 1774 return (ENXIO); 1775 } 1776 } 1777 1778 /* Enable all clocks before accessing any registers. */ 1779 CSR_PCI_WRITE_4(sc, PCI_OUR_REG_3, 0); 1780 1781 CSR_WRITE_2(sc, B0_CTST, CS_RST_CLR); 1782 sc->msk_hw_id = CSR_READ_1(sc, B2_CHIP_ID); 1783 sc->msk_hw_rev = (CSR_READ_1(sc, B2_MAC_CFG) >> 4) & 0x0f; 1784 /* Bail out if chip is not recognized. */ 1785 if (sc->msk_hw_id < CHIP_ID_YUKON_XL || 1786 sc->msk_hw_id > CHIP_ID_YUKON_OPT || 1787 sc->msk_hw_id == CHIP_ID_YUKON_UNKNOWN) { 1788 device_printf(dev, "unknown device: id=0x%02x, rev=0x%02x\n", 1789 sc->msk_hw_id, sc->msk_hw_rev); 1790 mtx_destroy(&sc->msk_mtx); 1791 return (ENXIO); 1792 } 1793 1794 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), 1795 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), 1796 OID_AUTO, "process_limit", 1797 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 1798 &sc->msk_process_limit, 0, sysctl_hw_msk_proc_limit, "I", 1799 "max number of Rx events to process"); 1800 1801 sc->msk_process_limit = MSK_PROC_DEFAULT; 1802 error = resource_int_value(device_get_name(dev), device_get_unit(dev), 1803 "process_limit", &sc->msk_process_limit); 1804 if (error == 0) { 1805 if (sc->msk_process_limit < MSK_PROC_MIN || 1806 sc->msk_process_limit > MSK_PROC_MAX) { 1807 device_printf(dev, "process_limit value out of range; " 1808 "using default: %d\n", MSK_PROC_DEFAULT); 1809 sc->msk_process_limit = MSK_PROC_DEFAULT; 1810 } 1811 } 1812 1813 sc->msk_int_holdoff = MSK_INT_HOLDOFF_DEFAULT; 1814 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev), 1815 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, 1816 "int_holdoff", CTLFLAG_RW, &sc->msk_int_holdoff, 0, 1817 "Maximum number of time to delay interrupts"); 1818 resource_int_value(device_get_name(dev), device_get_unit(dev), 1819 "int_holdoff", &sc->msk_int_holdoff); 1820 1821 sc->msk_pmd = CSR_READ_1(sc, B2_PMD_TYP); 1822 /* Check number of MACs. */ 1823 sc->msk_num_port = 1; 1824 if ((CSR_READ_1(sc, B2_Y2_HW_RES) & CFG_DUAL_MAC_MSK) == 1825 CFG_DUAL_MAC_MSK) { 1826 if (!(CSR_READ_1(sc, B2_Y2_CLK_GATE) & Y2_STATUS_LNK2_INAC)) 1827 sc->msk_num_port++; 1828 } 1829 1830 /* Check bus type. */ 1831 if (pci_find_cap(sc->msk_dev, PCIY_EXPRESS, ®) == 0) { 1832 sc->msk_bustype = MSK_PEX_BUS; 1833 sc->msk_expcap = reg; 1834 } else if (pci_find_cap(sc->msk_dev, PCIY_PCIX, ®) == 0) { 1835 sc->msk_bustype = MSK_PCIX_BUS; 1836 sc->msk_pcixcap = reg; 1837 } else 1838 sc->msk_bustype = MSK_PCI_BUS; 1839 1840 switch (sc->msk_hw_id) { 1841 case CHIP_ID_YUKON_EC: 1842 sc->msk_clock = 125; /* 125 MHz */ 1843 sc->msk_pflags |= MSK_FLAG_JUMBO; 1844 break; 1845 case CHIP_ID_YUKON_EC_U: 1846 sc->msk_clock = 125; /* 125 MHz */ 1847 sc->msk_pflags |= MSK_FLAG_JUMBO | MSK_FLAG_JUMBO_NOCSUM; 1848 break; 1849 case CHIP_ID_YUKON_EX: 1850 sc->msk_clock = 125; /* 125 MHz */ 1851 sc->msk_pflags |= MSK_FLAG_JUMBO | MSK_FLAG_DESCV2 | 1852 MSK_FLAG_AUTOTX_CSUM; 1853 /* 1854 * Yukon Extreme seems to have silicon bug for 1855 * automatic Tx checksum calculation capability. 1856 */ 1857 if (sc->msk_hw_rev == CHIP_REV_YU_EX_B0) 1858 sc->msk_pflags &= ~MSK_FLAG_AUTOTX_CSUM; 1859 /* 1860 * Yukon Extreme A0 could not use store-and-forward 1861 * for jumbo frames, so disable Tx checksum 1862 * offloading for jumbo frames. 1863 */ 1864 if (sc->msk_hw_rev == CHIP_REV_YU_EX_A0) 1865 sc->msk_pflags |= MSK_FLAG_JUMBO_NOCSUM; 1866 break; 1867 case CHIP_ID_YUKON_FE: 1868 sc->msk_clock = 100; /* 100 MHz */ 1869 sc->msk_pflags |= MSK_FLAG_FASTETHER; 1870 break; 1871 case CHIP_ID_YUKON_FE_P: 1872 sc->msk_clock = 50; /* 50 MHz */ 1873 sc->msk_pflags |= MSK_FLAG_FASTETHER | MSK_FLAG_DESCV2 | 1874 MSK_FLAG_AUTOTX_CSUM; 1875 if (sc->msk_hw_rev == CHIP_REV_YU_FE_P_A0) { 1876 /* 1877 * XXX 1878 * FE+ A0 has status LE writeback bug so msk(4) 1879 * does not rely on status word of received frame 1880 * in msk_rxeof() which in turn disables all 1881 * hardware assistance bits reported by the status 1882 * word as well as validity of the received frame. 1883 * Just pass received frames to upper stack with 1884 * minimal test and let upper stack handle them. 1885 */ 1886 sc->msk_pflags |= MSK_FLAG_NOHWVLAN | 1887 MSK_FLAG_NORXCHK | MSK_FLAG_NORX_CSUM; 1888 } 1889 break; 1890 case CHIP_ID_YUKON_XL: 1891 sc->msk_clock = 156; /* 156 MHz */ 1892 sc->msk_pflags |= MSK_FLAG_JUMBO; 1893 break; 1894 case CHIP_ID_YUKON_SUPR: 1895 sc->msk_clock = 125; /* 125 MHz */ 1896 sc->msk_pflags |= MSK_FLAG_JUMBO | MSK_FLAG_DESCV2 | 1897 MSK_FLAG_AUTOTX_CSUM; 1898 break; 1899 case CHIP_ID_YUKON_UL_2: 1900 sc->msk_clock = 125; /* 125 MHz */ 1901 sc->msk_pflags |= MSK_FLAG_JUMBO; 1902 break; 1903 case CHIP_ID_YUKON_OPT: 1904 sc->msk_clock = 125; /* 125 MHz */ 1905 sc->msk_pflags |= MSK_FLAG_JUMBO | MSK_FLAG_DESCV2; 1906 break; 1907 default: 1908 sc->msk_clock = 156; /* 156 MHz */ 1909 break; 1910 } 1911 1912 /* Allocate IRQ resources. */ 1913 msic = pci_msi_count(dev); 1914 if (bootverbose) 1915 device_printf(dev, "MSI count : %d\n", msic); 1916 if (legacy_intr != 0) 1917 msi_disable = 1; 1918 if (msi_disable == 0 && msic > 0) { 1919 msir = 1; 1920 if (pci_alloc_msi(dev, &msir) == 0) { 1921 if (msir == 1) { 1922 sc->msk_pflags |= MSK_FLAG_MSI; 1923 sc->msk_irq_spec = msk_irq_spec_msi; 1924 } else 1925 pci_release_msi(dev); 1926 } 1927 } 1928 1929 error = bus_alloc_resources(dev, sc->msk_irq_spec, sc->msk_irq); 1930 if (error) { 1931 device_printf(dev, "couldn't allocate IRQ resources\n"); 1932 goto fail; 1933 } 1934 1935 if ((error = msk_status_dma_alloc(sc)) != 0) 1936 goto fail; 1937 1938 /* Set base interrupt mask. */ 1939 sc->msk_intrmask = Y2_IS_HW_ERR | Y2_IS_STAT_BMU; 1940 sc->msk_intrhwemask = Y2_IS_TIST_OV | Y2_IS_MST_ERR | 1941 Y2_IS_IRQ_STAT | Y2_IS_PCI_EXP | Y2_IS_PCI_NEXP; 1942 1943 /* Reset the adapter. */ 1944 mskc_reset(sc); 1945 1946 if ((error = mskc_setup_rambuffer(sc)) != 0) 1947 goto fail; 1948 1949 sc->msk_devs[MSK_PORT_A] = device_add_child(dev, "msk", -1); 1950 if (sc->msk_devs[MSK_PORT_A] == NULL) { 1951 device_printf(dev, "failed to add child for PORT_A\n"); 1952 error = ENXIO; 1953 goto fail; 1954 } 1955 mmd = malloc(sizeof(struct msk_mii_data), M_DEVBUF, M_WAITOK | M_ZERO); 1956 mmd->port = MSK_PORT_A; 1957 mmd->pmd = sc->msk_pmd; 1958 mmd->mii_flags |= MIIF_DOPAUSE; 1959 if (sc->msk_pmd == 'L' || sc->msk_pmd == 'S') 1960 mmd->mii_flags |= MIIF_HAVEFIBER; 1961 if (sc->msk_pmd == 'P') 1962 mmd->mii_flags |= MIIF_HAVEFIBER | MIIF_MACPRIV0; 1963 device_set_ivars(sc->msk_devs[MSK_PORT_A], mmd); 1964 1965 if (sc->msk_num_port > 1) { 1966 sc->msk_devs[MSK_PORT_B] = device_add_child(dev, "msk", -1); 1967 if (sc->msk_devs[MSK_PORT_B] == NULL) { 1968 device_printf(dev, "failed to add child for PORT_B\n"); 1969 error = ENXIO; 1970 goto fail; 1971 } 1972 mmd = malloc(sizeof(struct msk_mii_data), M_DEVBUF, M_WAITOK | 1973 M_ZERO); 1974 mmd->port = MSK_PORT_B; 1975 mmd->pmd = sc->msk_pmd; 1976 if (sc->msk_pmd == 'L' || sc->msk_pmd == 'S') 1977 mmd->mii_flags |= MIIF_HAVEFIBER; 1978 if (sc->msk_pmd == 'P') 1979 mmd->mii_flags |= MIIF_HAVEFIBER | MIIF_MACPRIV0; 1980 device_set_ivars(sc->msk_devs[MSK_PORT_B], mmd); 1981 } 1982 1983 error = bus_generic_attach(dev); 1984 if (error) { 1985 device_printf(dev, "failed to attach port(s)\n"); 1986 goto fail; 1987 } 1988 1989 /* Hook interrupt last to avoid having to lock softc. */ 1990 error = bus_setup_intr(dev, sc->msk_irq[0], INTR_TYPE_NET | 1991 INTR_MPSAFE, NULL, msk_intr, sc, &sc->msk_intrhand); 1992 if (error != 0) { 1993 device_printf(dev, "couldn't set up interrupt handler\n"); 1994 goto fail; 1995 } 1996 fail: 1997 if (error != 0) 1998 mskc_detach(dev); 1999 2000 return (error); 2001 } 2002 2003 /* 2004 * Shutdown hardware and free up resources. This can be called any 2005 * time after the mutex has been initialized. It is called in both 2006 * the error case in attach and the normal detach case so it needs 2007 * to be careful about only freeing resources that have actually been 2008 * allocated. 2009 */ 2010 static int 2011 msk_detach(device_t dev) 2012 { 2013 struct msk_softc *sc; 2014 struct msk_if_softc *sc_if; 2015 if_t ifp; 2016 2017 sc_if = device_get_softc(dev); 2018 KASSERT(mtx_initialized(&sc_if->msk_softc->msk_mtx), 2019 ("msk mutex not initialized in msk_detach")); 2020 MSK_IF_LOCK(sc_if); 2021 2022 ifp = sc_if->msk_ifp; 2023 if (device_is_attached(dev)) { 2024 /* XXX */ 2025 sc_if->msk_flags |= MSK_FLAG_DETACH; 2026 msk_stop(sc_if); 2027 /* Can't hold locks while calling detach. */ 2028 MSK_IF_UNLOCK(sc_if); 2029 callout_drain(&sc_if->msk_tick_ch); 2030 if (ifp) 2031 ether_ifdetach(ifp); 2032 MSK_IF_LOCK(sc_if); 2033 } 2034 2035 /* 2036 * We're generally called from mskc_detach() which is using 2037 * device_delete_child() to get to here. It's already trashed 2038 * miibus for us, so don't do it here or we'll panic. 2039 * 2040 * if (sc_if->msk_miibus != NULL) { 2041 * device_delete_child(dev, sc_if->msk_miibus); 2042 * sc_if->msk_miibus = NULL; 2043 * } 2044 */ 2045 2046 msk_rx_dma_jfree(sc_if); 2047 msk_txrx_dma_free(sc_if); 2048 bus_generic_detach(dev); 2049 2050 sc = sc_if->msk_softc; 2051 sc->msk_if[sc_if->msk_port] = NULL; 2052 MSK_IF_UNLOCK(sc_if); 2053 if (ifp) 2054 if_free(ifp); 2055 2056 return (0); 2057 } 2058 2059 static int 2060 mskc_detach(device_t dev) 2061 { 2062 struct msk_softc *sc; 2063 2064 sc = device_get_softc(dev); 2065 KASSERT(mtx_initialized(&sc->msk_mtx), ("msk mutex not initialized")); 2066 2067 if (device_is_alive(dev)) { 2068 if (sc->msk_devs[MSK_PORT_A] != NULL) { 2069 free(device_get_ivars(sc->msk_devs[MSK_PORT_A]), 2070 M_DEVBUF); 2071 device_delete_child(dev, sc->msk_devs[MSK_PORT_A]); 2072 } 2073 if (sc->msk_devs[MSK_PORT_B] != NULL) { 2074 free(device_get_ivars(sc->msk_devs[MSK_PORT_B]), 2075 M_DEVBUF); 2076 device_delete_child(dev, sc->msk_devs[MSK_PORT_B]); 2077 } 2078 bus_generic_detach(dev); 2079 } 2080 2081 /* Disable all interrupts. */ 2082 CSR_WRITE_4(sc, B0_IMSK, 0); 2083 CSR_READ_4(sc, B0_IMSK); 2084 CSR_WRITE_4(sc, B0_HWE_IMSK, 0); 2085 CSR_READ_4(sc, B0_HWE_IMSK); 2086 2087 /* LED Off. */ 2088 CSR_WRITE_2(sc, B0_CTST, Y2_LED_STAT_OFF); 2089 2090 /* Put hardware reset. */ 2091 CSR_WRITE_2(sc, B0_CTST, CS_RST_SET); 2092 2093 msk_status_dma_free(sc); 2094 2095 if (sc->msk_intrhand) { 2096 bus_teardown_intr(dev, sc->msk_irq[0], sc->msk_intrhand); 2097 sc->msk_intrhand = NULL; 2098 } 2099 bus_release_resources(dev, sc->msk_irq_spec, sc->msk_irq); 2100 if ((sc->msk_pflags & MSK_FLAG_MSI) != 0) 2101 pci_release_msi(dev); 2102 bus_release_resources(dev, sc->msk_res_spec, sc->msk_res); 2103 mtx_destroy(&sc->msk_mtx); 2104 2105 return (0); 2106 } 2107 2108 static bus_dma_tag_t 2109 mskc_get_dma_tag(device_t bus, device_t child __unused) 2110 { 2111 2112 return (bus_get_dma_tag(bus)); 2113 } 2114 2115 struct msk_dmamap_arg { 2116 bus_addr_t msk_busaddr; 2117 }; 2118 2119 static void 2120 msk_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error) 2121 { 2122 struct msk_dmamap_arg *ctx; 2123 2124 if (error != 0) 2125 return; 2126 ctx = arg; 2127 ctx->msk_busaddr = segs[0].ds_addr; 2128 } 2129 2130 /* Create status DMA region. */ 2131 static int 2132 msk_status_dma_alloc(struct msk_softc *sc) 2133 { 2134 struct msk_dmamap_arg ctx; 2135 bus_size_t stat_sz; 2136 int count, error; 2137 2138 /* 2139 * It seems controller requires number of status LE entries 2140 * is power of 2 and the maximum number of status LE entries 2141 * is 4096. For dual-port controllers, the number of status 2142 * LE entries should be large enough to hold both port's 2143 * status updates. 2144 */ 2145 count = 3 * MSK_RX_RING_CNT + MSK_TX_RING_CNT; 2146 count = imin(4096, roundup2(count, 1024)); 2147 sc->msk_stat_count = count; 2148 stat_sz = count * sizeof(struct msk_stat_desc); 2149 error = bus_dma_tag_create( 2150 bus_get_dma_tag(sc->msk_dev), /* parent */ 2151 MSK_STAT_ALIGN, 0, /* alignment, boundary */ 2152 BUS_SPACE_MAXADDR, /* lowaddr */ 2153 BUS_SPACE_MAXADDR, /* highaddr */ 2154 NULL, NULL, /* filter, filterarg */ 2155 stat_sz, /* maxsize */ 2156 1, /* nsegments */ 2157 stat_sz, /* maxsegsize */ 2158 0, /* flags */ 2159 NULL, NULL, /* lockfunc, lockarg */ 2160 &sc->msk_stat_tag); 2161 if (error != 0) { 2162 device_printf(sc->msk_dev, 2163 "failed to create status DMA tag\n"); 2164 return (error); 2165 } 2166 2167 /* Allocate DMA'able memory and load the DMA map for status ring. */ 2168 error = bus_dmamem_alloc(sc->msk_stat_tag, 2169 (void **)&sc->msk_stat_ring, BUS_DMA_WAITOK | BUS_DMA_COHERENT | 2170 BUS_DMA_ZERO, &sc->msk_stat_map); 2171 if (error != 0) { 2172 device_printf(sc->msk_dev, 2173 "failed to allocate DMA'able memory for status ring\n"); 2174 return (error); 2175 } 2176 2177 ctx.msk_busaddr = 0; 2178 error = bus_dmamap_load(sc->msk_stat_tag, sc->msk_stat_map, 2179 sc->msk_stat_ring, stat_sz, msk_dmamap_cb, &ctx, BUS_DMA_NOWAIT); 2180 if (error != 0) { 2181 device_printf(sc->msk_dev, 2182 "failed to load DMA'able memory for status ring\n"); 2183 return (error); 2184 } 2185 sc->msk_stat_ring_paddr = ctx.msk_busaddr; 2186 2187 return (0); 2188 } 2189 2190 static void 2191 msk_status_dma_free(struct msk_softc *sc) 2192 { 2193 2194 /* Destroy status block. */ 2195 if (sc->msk_stat_tag) { 2196 if (sc->msk_stat_ring_paddr) { 2197 bus_dmamap_unload(sc->msk_stat_tag, sc->msk_stat_map); 2198 sc->msk_stat_ring_paddr = 0; 2199 } 2200 if (sc->msk_stat_ring) { 2201 bus_dmamem_free(sc->msk_stat_tag, 2202 sc->msk_stat_ring, sc->msk_stat_map); 2203 sc->msk_stat_ring = NULL; 2204 } 2205 bus_dma_tag_destroy(sc->msk_stat_tag); 2206 sc->msk_stat_tag = NULL; 2207 } 2208 } 2209 2210 static int 2211 msk_txrx_dma_alloc(struct msk_if_softc *sc_if) 2212 { 2213 struct msk_dmamap_arg ctx; 2214 struct msk_txdesc *txd; 2215 struct msk_rxdesc *rxd; 2216 bus_size_t rxalign; 2217 int error, i; 2218 2219 /* Create parent DMA tag. */ 2220 error = bus_dma_tag_create( 2221 bus_get_dma_tag(sc_if->msk_if_dev), /* parent */ 2222 1, 0, /* alignment, boundary */ 2223 BUS_SPACE_MAXADDR, /* lowaddr */ 2224 BUS_SPACE_MAXADDR, /* highaddr */ 2225 NULL, NULL, /* filter, filterarg */ 2226 BUS_SPACE_MAXSIZE_32BIT, /* maxsize */ 2227 0, /* nsegments */ 2228 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */ 2229 0, /* flags */ 2230 NULL, NULL, /* lockfunc, lockarg */ 2231 &sc_if->msk_cdata.msk_parent_tag); 2232 if (error != 0) { 2233 device_printf(sc_if->msk_if_dev, 2234 "failed to create parent DMA tag\n"); 2235 goto fail; 2236 } 2237 /* Create tag for Tx ring. */ 2238 error = bus_dma_tag_create(sc_if->msk_cdata.msk_parent_tag,/* parent */ 2239 MSK_RING_ALIGN, 0, /* alignment, boundary */ 2240 BUS_SPACE_MAXADDR, /* lowaddr */ 2241 BUS_SPACE_MAXADDR, /* highaddr */ 2242 NULL, NULL, /* filter, filterarg */ 2243 MSK_TX_RING_SZ, /* maxsize */ 2244 1, /* nsegments */ 2245 MSK_TX_RING_SZ, /* maxsegsize */ 2246 0, /* flags */ 2247 NULL, NULL, /* lockfunc, lockarg */ 2248 &sc_if->msk_cdata.msk_tx_ring_tag); 2249 if (error != 0) { 2250 device_printf(sc_if->msk_if_dev, 2251 "failed to create Tx ring DMA tag\n"); 2252 goto fail; 2253 } 2254 2255 /* Create tag for Rx ring. */ 2256 error = bus_dma_tag_create(sc_if->msk_cdata.msk_parent_tag,/* parent */ 2257 MSK_RING_ALIGN, 0, /* alignment, boundary */ 2258 BUS_SPACE_MAXADDR, /* lowaddr */ 2259 BUS_SPACE_MAXADDR, /* highaddr */ 2260 NULL, NULL, /* filter, filterarg */ 2261 MSK_RX_RING_SZ, /* maxsize */ 2262 1, /* nsegments */ 2263 MSK_RX_RING_SZ, /* maxsegsize */ 2264 0, /* flags */ 2265 NULL, NULL, /* lockfunc, lockarg */ 2266 &sc_if->msk_cdata.msk_rx_ring_tag); 2267 if (error != 0) { 2268 device_printf(sc_if->msk_if_dev, 2269 "failed to create Rx ring DMA tag\n"); 2270 goto fail; 2271 } 2272 2273 /* Create tag for Tx buffers. */ 2274 error = bus_dma_tag_create(sc_if->msk_cdata.msk_parent_tag,/* parent */ 2275 1, 0, /* alignment, boundary */ 2276 BUS_SPACE_MAXADDR, /* lowaddr */ 2277 BUS_SPACE_MAXADDR, /* highaddr */ 2278 NULL, NULL, /* filter, filterarg */ 2279 MSK_TSO_MAXSIZE, /* maxsize */ 2280 MSK_MAXTXSEGS, /* nsegments */ 2281 MSK_TSO_MAXSGSIZE, /* maxsegsize */ 2282 0, /* flags */ 2283 NULL, NULL, /* lockfunc, lockarg */ 2284 &sc_if->msk_cdata.msk_tx_tag); 2285 if (error != 0) { 2286 device_printf(sc_if->msk_if_dev, 2287 "failed to create Tx DMA tag\n"); 2288 goto fail; 2289 } 2290 2291 rxalign = 1; 2292 /* 2293 * Workaround hardware hang which seems to happen when Rx buffer 2294 * is not aligned on multiple of FIFO word(8 bytes). 2295 */ 2296 if ((sc_if->msk_flags & MSK_FLAG_RAMBUF) != 0) 2297 rxalign = MSK_RX_BUF_ALIGN; 2298 /* Create tag for Rx buffers. */ 2299 error = bus_dma_tag_create(sc_if->msk_cdata.msk_parent_tag,/* parent */ 2300 rxalign, 0, /* alignment, boundary */ 2301 BUS_SPACE_MAXADDR, /* lowaddr */ 2302 BUS_SPACE_MAXADDR, /* highaddr */ 2303 NULL, NULL, /* filter, filterarg */ 2304 MCLBYTES, /* maxsize */ 2305 1, /* nsegments */ 2306 MCLBYTES, /* maxsegsize */ 2307 0, /* flags */ 2308 NULL, NULL, /* lockfunc, lockarg */ 2309 &sc_if->msk_cdata.msk_rx_tag); 2310 if (error != 0) { 2311 device_printf(sc_if->msk_if_dev, 2312 "failed to create Rx DMA tag\n"); 2313 goto fail; 2314 } 2315 2316 /* Allocate DMA'able memory and load the DMA map for Tx ring. */ 2317 error = bus_dmamem_alloc(sc_if->msk_cdata.msk_tx_ring_tag, 2318 (void **)&sc_if->msk_rdata.msk_tx_ring, BUS_DMA_WAITOK | 2319 BUS_DMA_COHERENT | BUS_DMA_ZERO, &sc_if->msk_cdata.msk_tx_ring_map); 2320 if (error != 0) { 2321 device_printf(sc_if->msk_if_dev, 2322 "failed to allocate DMA'able memory for Tx ring\n"); 2323 goto fail; 2324 } 2325 2326 ctx.msk_busaddr = 0; 2327 error = bus_dmamap_load(sc_if->msk_cdata.msk_tx_ring_tag, 2328 sc_if->msk_cdata.msk_tx_ring_map, sc_if->msk_rdata.msk_tx_ring, 2329 MSK_TX_RING_SZ, msk_dmamap_cb, &ctx, BUS_DMA_NOWAIT); 2330 if (error != 0) { 2331 device_printf(sc_if->msk_if_dev, 2332 "failed to load DMA'able memory for Tx ring\n"); 2333 goto fail; 2334 } 2335 sc_if->msk_rdata.msk_tx_ring_paddr = ctx.msk_busaddr; 2336 2337 /* Allocate DMA'able memory and load the DMA map for Rx ring. */ 2338 error = bus_dmamem_alloc(sc_if->msk_cdata.msk_rx_ring_tag, 2339 (void **)&sc_if->msk_rdata.msk_rx_ring, BUS_DMA_WAITOK | 2340 BUS_DMA_COHERENT | BUS_DMA_ZERO, &sc_if->msk_cdata.msk_rx_ring_map); 2341 if (error != 0) { 2342 device_printf(sc_if->msk_if_dev, 2343 "failed to allocate DMA'able memory for Rx ring\n"); 2344 goto fail; 2345 } 2346 2347 ctx.msk_busaddr = 0; 2348 error = bus_dmamap_load(sc_if->msk_cdata.msk_rx_ring_tag, 2349 sc_if->msk_cdata.msk_rx_ring_map, sc_if->msk_rdata.msk_rx_ring, 2350 MSK_RX_RING_SZ, msk_dmamap_cb, &ctx, BUS_DMA_NOWAIT); 2351 if (error != 0) { 2352 device_printf(sc_if->msk_if_dev, 2353 "failed to load DMA'able memory for Rx ring\n"); 2354 goto fail; 2355 } 2356 sc_if->msk_rdata.msk_rx_ring_paddr = ctx.msk_busaddr; 2357 2358 /* Create DMA maps for Tx buffers. */ 2359 for (i = 0; i < MSK_TX_RING_CNT; i++) { 2360 txd = &sc_if->msk_cdata.msk_txdesc[i]; 2361 txd->tx_m = NULL; 2362 txd->tx_dmamap = NULL; 2363 error = bus_dmamap_create(sc_if->msk_cdata.msk_tx_tag, 0, 2364 &txd->tx_dmamap); 2365 if (error != 0) { 2366 device_printf(sc_if->msk_if_dev, 2367 "failed to create Tx dmamap\n"); 2368 goto fail; 2369 } 2370 } 2371 /* Create DMA maps for Rx buffers. */ 2372 if ((error = bus_dmamap_create(sc_if->msk_cdata.msk_rx_tag, 0, 2373 &sc_if->msk_cdata.msk_rx_sparemap)) != 0) { 2374 device_printf(sc_if->msk_if_dev, 2375 "failed to create spare Rx dmamap\n"); 2376 goto fail; 2377 } 2378 for (i = 0; i < MSK_RX_RING_CNT; i++) { 2379 rxd = &sc_if->msk_cdata.msk_rxdesc[i]; 2380 rxd->rx_m = NULL; 2381 rxd->rx_dmamap = NULL; 2382 error = bus_dmamap_create(sc_if->msk_cdata.msk_rx_tag, 0, 2383 &rxd->rx_dmamap); 2384 if (error != 0) { 2385 device_printf(sc_if->msk_if_dev, 2386 "failed to create Rx dmamap\n"); 2387 goto fail; 2388 } 2389 } 2390 2391 fail: 2392 return (error); 2393 } 2394 2395 static int 2396 msk_rx_dma_jalloc(struct msk_if_softc *sc_if) 2397 { 2398 struct msk_dmamap_arg ctx; 2399 struct msk_rxdesc *jrxd; 2400 bus_size_t rxalign; 2401 int error, i; 2402 2403 if (jumbo_disable != 0 || (sc_if->msk_flags & MSK_FLAG_JUMBO) == 0) { 2404 sc_if->msk_flags &= ~MSK_FLAG_JUMBO; 2405 device_printf(sc_if->msk_if_dev, 2406 "disabling jumbo frame support\n"); 2407 return (0); 2408 } 2409 /* Create tag for jumbo Rx ring. */ 2410 error = bus_dma_tag_create(sc_if->msk_cdata.msk_parent_tag,/* parent */ 2411 MSK_RING_ALIGN, 0, /* alignment, boundary */ 2412 BUS_SPACE_MAXADDR, /* lowaddr */ 2413 BUS_SPACE_MAXADDR, /* highaddr */ 2414 NULL, NULL, /* filter, filterarg */ 2415 MSK_JUMBO_RX_RING_SZ, /* maxsize */ 2416 1, /* nsegments */ 2417 MSK_JUMBO_RX_RING_SZ, /* maxsegsize */ 2418 0, /* flags */ 2419 NULL, NULL, /* lockfunc, lockarg */ 2420 &sc_if->msk_cdata.msk_jumbo_rx_ring_tag); 2421 if (error != 0) { 2422 device_printf(sc_if->msk_if_dev, 2423 "failed to create jumbo Rx ring DMA tag\n"); 2424 goto jumbo_fail; 2425 } 2426 2427 rxalign = 1; 2428 /* 2429 * Workaround hardware hang which seems to happen when Rx buffer 2430 * is not aligned on multiple of FIFO word(8 bytes). 2431 */ 2432 if ((sc_if->msk_flags & MSK_FLAG_RAMBUF) != 0) 2433 rxalign = MSK_RX_BUF_ALIGN; 2434 /* Create tag for jumbo Rx buffers. */ 2435 error = bus_dma_tag_create(sc_if->msk_cdata.msk_parent_tag,/* parent */ 2436 rxalign, 0, /* alignment, boundary */ 2437 BUS_SPACE_MAXADDR, /* lowaddr */ 2438 BUS_SPACE_MAXADDR, /* highaddr */ 2439 NULL, NULL, /* filter, filterarg */ 2440 MJUM9BYTES, /* maxsize */ 2441 1, /* nsegments */ 2442 MJUM9BYTES, /* maxsegsize */ 2443 0, /* flags */ 2444 NULL, NULL, /* lockfunc, lockarg */ 2445 &sc_if->msk_cdata.msk_jumbo_rx_tag); 2446 if (error != 0) { 2447 device_printf(sc_if->msk_if_dev, 2448 "failed to create jumbo Rx DMA tag\n"); 2449 goto jumbo_fail; 2450 } 2451 2452 /* Allocate DMA'able memory and load the DMA map for jumbo Rx ring. */ 2453 error = bus_dmamem_alloc(sc_if->msk_cdata.msk_jumbo_rx_ring_tag, 2454 (void **)&sc_if->msk_rdata.msk_jumbo_rx_ring, 2455 BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO, 2456 &sc_if->msk_cdata.msk_jumbo_rx_ring_map); 2457 if (error != 0) { 2458 device_printf(sc_if->msk_if_dev, 2459 "failed to allocate DMA'able memory for jumbo Rx ring\n"); 2460 goto jumbo_fail; 2461 } 2462 2463 ctx.msk_busaddr = 0; 2464 error = bus_dmamap_load(sc_if->msk_cdata.msk_jumbo_rx_ring_tag, 2465 sc_if->msk_cdata.msk_jumbo_rx_ring_map, 2466 sc_if->msk_rdata.msk_jumbo_rx_ring, MSK_JUMBO_RX_RING_SZ, 2467 msk_dmamap_cb, &ctx, BUS_DMA_NOWAIT); 2468 if (error != 0) { 2469 device_printf(sc_if->msk_if_dev, 2470 "failed to load DMA'able memory for jumbo Rx ring\n"); 2471 goto jumbo_fail; 2472 } 2473 sc_if->msk_rdata.msk_jumbo_rx_ring_paddr = ctx.msk_busaddr; 2474 2475 /* Create DMA maps for jumbo Rx buffers. */ 2476 if ((error = bus_dmamap_create(sc_if->msk_cdata.msk_jumbo_rx_tag, 0, 2477 &sc_if->msk_cdata.msk_jumbo_rx_sparemap)) != 0) { 2478 device_printf(sc_if->msk_if_dev, 2479 "failed to create spare jumbo Rx dmamap\n"); 2480 goto jumbo_fail; 2481 } 2482 for (i = 0; i < MSK_JUMBO_RX_RING_CNT; i++) { 2483 jrxd = &sc_if->msk_cdata.msk_jumbo_rxdesc[i]; 2484 jrxd->rx_m = NULL; 2485 jrxd->rx_dmamap = NULL; 2486 error = bus_dmamap_create(sc_if->msk_cdata.msk_jumbo_rx_tag, 0, 2487 &jrxd->rx_dmamap); 2488 if (error != 0) { 2489 device_printf(sc_if->msk_if_dev, 2490 "failed to create jumbo Rx dmamap\n"); 2491 goto jumbo_fail; 2492 } 2493 } 2494 2495 return (0); 2496 2497 jumbo_fail: 2498 msk_rx_dma_jfree(sc_if); 2499 device_printf(sc_if->msk_if_dev, "disabling jumbo frame support " 2500 "due to resource shortage\n"); 2501 sc_if->msk_flags &= ~MSK_FLAG_JUMBO; 2502 return (error); 2503 } 2504 2505 static void 2506 msk_txrx_dma_free(struct msk_if_softc *sc_if) 2507 { 2508 struct msk_txdesc *txd; 2509 struct msk_rxdesc *rxd; 2510 int i; 2511 2512 /* Tx ring. */ 2513 if (sc_if->msk_cdata.msk_tx_ring_tag) { 2514 if (sc_if->msk_rdata.msk_tx_ring_paddr) 2515 bus_dmamap_unload(sc_if->msk_cdata.msk_tx_ring_tag, 2516 sc_if->msk_cdata.msk_tx_ring_map); 2517 if (sc_if->msk_rdata.msk_tx_ring) 2518 bus_dmamem_free(sc_if->msk_cdata.msk_tx_ring_tag, 2519 sc_if->msk_rdata.msk_tx_ring, 2520 sc_if->msk_cdata.msk_tx_ring_map); 2521 sc_if->msk_rdata.msk_tx_ring = NULL; 2522 sc_if->msk_rdata.msk_tx_ring_paddr = 0; 2523 bus_dma_tag_destroy(sc_if->msk_cdata.msk_tx_ring_tag); 2524 sc_if->msk_cdata.msk_tx_ring_tag = NULL; 2525 } 2526 /* Rx ring. */ 2527 if (sc_if->msk_cdata.msk_rx_ring_tag) { 2528 if (sc_if->msk_rdata.msk_rx_ring_paddr) 2529 bus_dmamap_unload(sc_if->msk_cdata.msk_rx_ring_tag, 2530 sc_if->msk_cdata.msk_rx_ring_map); 2531 if (sc_if->msk_rdata.msk_rx_ring) 2532 bus_dmamem_free(sc_if->msk_cdata.msk_rx_ring_tag, 2533 sc_if->msk_rdata.msk_rx_ring, 2534 sc_if->msk_cdata.msk_rx_ring_map); 2535 sc_if->msk_rdata.msk_rx_ring = NULL; 2536 sc_if->msk_rdata.msk_rx_ring_paddr = 0; 2537 bus_dma_tag_destroy(sc_if->msk_cdata.msk_rx_ring_tag); 2538 sc_if->msk_cdata.msk_rx_ring_tag = NULL; 2539 } 2540 /* Tx buffers. */ 2541 if (sc_if->msk_cdata.msk_tx_tag) { 2542 for (i = 0; i < MSK_TX_RING_CNT; i++) { 2543 txd = &sc_if->msk_cdata.msk_txdesc[i]; 2544 if (txd->tx_dmamap) { 2545 bus_dmamap_destroy(sc_if->msk_cdata.msk_tx_tag, 2546 txd->tx_dmamap); 2547 txd->tx_dmamap = NULL; 2548 } 2549 } 2550 bus_dma_tag_destroy(sc_if->msk_cdata.msk_tx_tag); 2551 sc_if->msk_cdata.msk_tx_tag = NULL; 2552 } 2553 /* Rx buffers. */ 2554 if (sc_if->msk_cdata.msk_rx_tag) { 2555 for (i = 0; i < MSK_RX_RING_CNT; i++) { 2556 rxd = &sc_if->msk_cdata.msk_rxdesc[i]; 2557 if (rxd->rx_dmamap) { 2558 bus_dmamap_destroy(sc_if->msk_cdata.msk_rx_tag, 2559 rxd->rx_dmamap); 2560 rxd->rx_dmamap = NULL; 2561 } 2562 } 2563 if (sc_if->msk_cdata.msk_rx_sparemap) { 2564 bus_dmamap_destroy(sc_if->msk_cdata.msk_rx_tag, 2565 sc_if->msk_cdata.msk_rx_sparemap); 2566 sc_if->msk_cdata.msk_rx_sparemap = 0; 2567 } 2568 bus_dma_tag_destroy(sc_if->msk_cdata.msk_rx_tag); 2569 sc_if->msk_cdata.msk_rx_tag = NULL; 2570 } 2571 if (sc_if->msk_cdata.msk_parent_tag) { 2572 bus_dma_tag_destroy(sc_if->msk_cdata.msk_parent_tag); 2573 sc_if->msk_cdata.msk_parent_tag = NULL; 2574 } 2575 } 2576 2577 static void 2578 msk_rx_dma_jfree(struct msk_if_softc *sc_if) 2579 { 2580 struct msk_rxdesc *jrxd; 2581 int i; 2582 2583 /* Jumbo Rx ring. */ 2584 if (sc_if->msk_cdata.msk_jumbo_rx_ring_tag) { 2585 if (sc_if->msk_rdata.msk_jumbo_rx_ring_paddr) 2586 bus_dmamap_unload(sc_if->msk_cdata.msk_jumbo_rx_ring_tag, 2587 sc_if->msk_cdata.msk_jumbo_rx_ring_map); 2588 if (sc_if->msk_rdata.msk_jumbo_rx_ring) 2589 bus_dmamem_free(sc_if->msk_cdata.msk_jumbo_rx_ring_tag, 2590 sc_if->msk_rdata.msk_jumbo_rx_ring, 2591 sc_if->msk_cdata.msk_jumbo_rx_ring_map); 2592 sc_if->msk_rdata.msk_jumbo_rx_ring = NULL; 2593 sc_if->msk_rdata.msk_jumbo_rx_ring_paddr = 0; 2594 bus_dma_tag_destroy(sc_if->msk_cdata.msk_jumbo_rx_ring_tag); 2595 sc_if->msk_cdata.msk_jumbo_rx_ring_tag = NULL; 2596 } 2597 /* Jumbo Rx buffers. */ 2598 if (sc_if->msk_cdata.msk_jumbo_rx_tag) { 2599 for (i = 0; i < MSK_JUMBO_RX_RING_CNT; i++) { 2600 jrxd = &sc_if->msk_cdata.msk_jumbo_rxdesc[i]; 2601 if (jrxd->rx_dmamap) { 2602 bus_dmamap_destroy( 2603 sc_if->msk_cdata.msk_jumbo_rx_tag, 2604 jrxd->rx_dmamap); 2605 jrxd->rx_dmamap = NULL; 2606 } 2607 } 2608 if (sc_if->msk_cdata.msk_jumbo_rx_sparemap) { 2609 bus_dmamap_destroy(sc_if->msk_cdata.msk_jumbo_rx_tag, 2610 sc_if->msk_cdata.msk_jumbo_rx_sparemap); 2611 sc_if->msk_cdata.msk_jumbo_rx_sparemap = 0; 2612 } 2613 bus_dma_tag_destroy(sc_if->msk_cdata.msk_jumbo_rx_tag); 2614 sc_if->msk_cdata.msk_jumbo_rx_tag = NULL; 2615 } 2616 } 2617 2618 static int 2619 msk_encap(struct msk_if_softc *sc_if, struct mbuf **m_head) 2620 { 2621 struct msk_txdesc *txd, *txd_last; 2622 struct msk_tx_desc *tx_le; 2623 struct mbuf *m; 2624 bus_dmamap_t map; 2625 bus_dma_segment_t txsegs[MSK_MAXTXSEGS]; 2626 uint32_t control, csum, prod, si; 2627 uint16_t offset, tcp_offset, tso_mtu; 2628 int error, i, nseg, tso; 2629 2630 MSK_IF_LOCK_ASSERT(sc_if); 2631 2632 tcp_offset = offset = 0; 2633 m = *m_head; 2634 if (((sc_if->msk_flags & MSK_FLAG_AUTOTX_CSUM) == 0 && 2635 (m->m_pkthdr.csum_flags & MSK_CSUM_FEATURES) != 0) || 2636 ((sc_if->msk_flags & MSK_FLAG_DESCV2) == 0 && 2637 (m->m_pkthdr.csum_flags & CSUM_TSO) != 0)) { 2638 /* 2639 * Since mbuf has no protocol specific structure information 2640 * in it we have to inspect protocol information here to 2641 * setup TSO and checksum offload. I don't know why Marvell 2642 * made a such decision in chip design because other GigE 2643 * hardwares normally takes care of all these chores in 2644 * hardware. However, TSO performance of Yukon II is very 2645 * good such that it's worth to implement it. 2646 */ 2647 struct ether_header *eh; 2648 struct ip *ip; 2649 struct tcphdr *tcp; 2650 2651 if (M_WRITABLE(m) == 0) { 2652 /* Get a writable copy. */ 2653 m = m_dup(*m_head, M_NOWAIT); 2654 m_freem(*m_head); 2655 if (m == NULL) { 2656 *m_head = NULL; 2657 return (ENOBUFS); 2658 } 2659 *m_head = m; 2660 } 2661 2662 offset = sizeof(struct ether_header); 2663 m = m_pullup(m, offset); 2664 if (m == NULL) { 2665 *m_head = NULL; 2666 return (ENOBUFS); 2667 } 2668 eh = mtod(m, struct ether_header *); 2669 /* Check if hardware VLAN insertion is off. */ 2670 if (eh->ether_type == htons(ETHERTYPE_VLAN)) { 2671 offset = sizeof(struct ether_vlan_header); 2672 m = m_pullup(m, offset); 2673 if (m == NULL) { 2674 *m_head = NULL; 2675 return (ENOBUFS); 2676 } 2677 } 2678 m = m_pullup(m, offset + sizeof(struct ip)); 2679 if (m == NULL) { 2680 *m_head = NULL; 2681 return (ENOBUFS); 2682 } 2683 ip = (struct ip *)(mtod(m, char *) + offset); 2684 offset += (ip->ip_hl << 2); 2685 tcp_offset = offset; 2686 if ((m->m_pkthdr.csum_flags & CSUM_TSO) != 0) { 2687 m = m_pullup(m, offset + sizeof(struct tcphdr)); 2688 if (m == NULL) { 2689 *m_head = NULL; 2690 return (ENOBUFS); 2691 } 2692 tcp = (struct tcphdr *)(mtod(m, char *) + offset); 2693 offset += (tcp->th_off << 2); 2694 } else if ((sc_if->msk_flags & MSK_FLAG_AUTOTX_CSUM) == 0 && 2695 (m->m_pkthdr.len < MSK_MIN_FRAMELEN) && 2696 (m->m_pkthdr.csum_flags & CSUM_TCP) != 0) { 2697 /* 2698 * It seems that Yukon II has Tx checksum offload bug 2699 * for small TCP packets that's less than 60 bytes in 2700 * size (e.g. TCP window probe packet, pure ACK packet). 2701 * Common work around like padding with zeros to make 2702 * the frame minimum ethernet frame size didn't work at 2703 * all. 2704 * Instead of disabling checksum offload completely we 2705 * resort to S/W checksum routine when we encounter 2706 * short TCP frames. 2707 * Short UDP packets appear to be handled correctly by 2708 * Yukon II. Also I assume this bug does not happen on 2709 * controllers that use newer descriptor format or 2710 * automatic Tx checksum calculation. 2711 */ 2712 m = m_pullup(m, offset + sizeof(struct tcphdr)); 2713 if (m == NULL) { 2714 *m_head = NULL; 2715 return (ENOBUFS); 2716 } 2717 *(uint16_t *)(m->m_data + offset + 2718 m->m_pkthdr.csum_data) = in_cksum_skip(m, 2719 m->m_pkthdr.len, offset); 2720 m->m_pkthdr.csum_flags &= ~CSUM_TCP; 2721 } 2722 *m_head = m; 2723 } 2724 2725 prod = sc_if->msk_cdata.msk_tx_prod; 2726 txd = &sc_if->msk_cdata.msk_txdesc[prod]; 2727 txd_last = txd; 2728 map = txd->tx_dmamap; 2729 error = bus_dmamap_load_mbuf_sg(sc_if->msk_cdata.msk_tx_tag, map, 2730 *m_head, txsegs, &nseg, BUS_DMA_NOWAIT); 2731 if (error == EFBIG) { 2732 m = m_collapse(*m_head, M_NOWAIT, MSK_MAXTXSEGS); 2733 if (m == NULL) { 2734 m_freem(*m_head); 2735 *m_head = NULL; 2736 return (ENOBUFS); 2737 } 2738 *m_head = m; 2739 error = bus_dmamap_load_mbuf_sg(sc_if->msk_cdata.msk_tx_tag, 2740 map, *m_head, txsegs, &nseg, BUS_DMA_NOWAIT); 2741 if (error != 0) { 2742 m_freem(*m_head); 2743 *m_head = NULL; 2744 return (error); 2745 } 2746 } else if (error != 0) 2747 return (error); 2748 if (nseg == 0) { 2749 m_freem(*m_head); 2750 *m_head = NULL; 2751 return (EIO); 2752 } 2753 2754 /* Check number of available descriptors. */ 2755 if (sc_if->msk_cdata.msk_tx_cnt + nseg >= 2756 (MSK_TX_RING_CNT - MSK_RESERVED_TX_DESC_CNT)) { 2757 bus_dmamap_unload(sc_if->msk_cdata.msk_tx_tag, map); 2758 return (ENOBUFS); 2759 } 2760 2761 control = 0; 2762 tso = 0; 2763 tx_le = NULL; 2764 2765 /* Check TSO support. */ 2766 if ((m->m_pkthdr.csum_flags & CSUM_TSO) != 0) { 2767 if ((sc_if->msk_flags & MSK_FLAG_DESCV2) != 0) 2768 tso_mtu = m->m_pkthdr.tso_segsz; 2769 else 2770 tso_mtu = offset + m->m_pkthdr.tso_segsz; 2771 if (tso_mtu != sc_if->msk_cdata.msk_tso_mtu) { 2772 tx_le = &sc_if->msk_rdata.msk_tx_ring[prod]; 2773 tx_le->msk_addr = htole32(tso_mtu); 2774 if ((sc_if->msk_flags & MSK_FLAG_DESCV2) != 0) 2775 tx_le->msk_control = htole32(OP_MSS | HW_OWNER); 2776 else 2777 tx_le->msk_control = 2778 htole32(OP_LRGLEN | HW_OWNER); 2779 sc_if->msk_cdata.msk_tx_cnt++; 2780 MSK_INC(prod, MSK_TX_RING_CNT); 2781 sc_if->msk_cdata.msk_tso_mtu = tso_mtu; 2782 } 2783 tso++; 2784 } 2785 /* Check if we have a VLAN tag to insert. */ 2786 if ((m->m_flags & M_VLANTAG) != 0) { 2787 if (tx_le == NULL) { 2788 tx_le = &sc_if->msk_rdata.msk_tx_ring[prod]; 2789 tx_le->msk_addr = htole32(0); 2790 tx_le->msk_control = htole32(OP_VLAN | HW_OWNER | 2791 htons(m->m_pkthdr.ether_vtag)); 2792 sc_if->msk_cdata.msk_tx_cnt++; 2793 MSK_INC(prod, MSK_TX_RING_CNT); 2794 } else { 2795 tx_le->msk_control |= htole32(OP_VLAN | 2796 htons(m->m_pkthdr.ether_vtag)); 2797 } 2798 control |= INS_VLAN; 2799 } 2800 /* Check if we have to handle checksum offload. */ 2801 if (tso == 0 && (m->m_pkthdr.csum_flags & MSK_CSUM_FEATURES) != 0) { 2802 if ((sc_if->msk_flags & MSK_FLAG_AUTOTX_CSUM) != 0) 2803 control |= CALSUM; 2804 else { 2805 control |= CALSUM | WR_SUM | INIT_SUM | LOCK_SUM; 2806 if ((m->m_pkthdr.csum_flags & CSUM_UDP) != 0) 2807 control |= UDPTCP; 2808 /* Checksum write position. */ 2809 csum = (tcp_offset + m->m_pkthdr.csum_data) & 0xffff; 2810 /* Checksum start position. */ 2811 csum |= (uint32_t)tcp_offset << 16; 2812 if (csum != sc_if->msk_cdata.msk_last_csum) { 2813 tx_le = &sc_if->msk_rdata.msk_tx_ring[prod]; 2814 tx_le->msk_addr = htole32(csum); 2815 tx_le->msk_control = htole32(1 << 16 | 2816 (OP_TCPLISW | HW_OWNER)); 2817 sc_if->msk_cdata.msk_tx_cnt++; 2818 MSK_INC(prod, MSK_TX_RING_CNT); 2819 sc_if->msk_cdata.msk_last_csum = csum; 2820 } 2821 } 2822 } 2823 2824 #ifdef MSK_64BIT_DMA 2825 if (MSK_ADDR_HI(txsegs[0].ds_addr) != 2826 sc_if->msk_cdata.msk_tx_high_addr) { 2827 sc_if->msk_cdata.msk_tx_high_addr = 2828 MSK_ADDR_HI(txsegs[0].ds_addr); 2829 tx_le = &sc_if->msk_rdata.msk_tx_ring[prod]; 2830 tx_le->msk_addr = htole32(MSK_ADDR_HI(txsegs[0].ds_addr)); 2831 tx_le->msk_control = htole32(OP_ADDR64 | HW_OWNER); 2832 sc_if->msk_cdata.msk_tx_cnt++; 2833 MSK_INC(prod, MSK_TX_RING_CNT); 2834 } 2835 #endif 2836 si = prod; 2837 tx_le = &sc_if->msk_rdata.msk_tx_ring[prod]; 2838 tx_le->msk_addr = htole32(MSK_ADDR_LO(txsegs[0].ds_addr)); 2839 if (tso == 0) 2840 tx_le->msk_control = htole32(txsegs[0].ds_len | control | 2841 OP_PACKET); 2842 else 2843 tx_le->msk_control = htole32(txsegs[0].ds_len | control | 2844 OP_LARGESEND); 2845 sc_if->msk_cdata.msk_tx_cnt++; 2846 MSK_INC(prod, MSK_TX_RING_CNT); 2847 2848 for (i = 1; i < nseg; i++) { 2849 tx_le = &sc_if->msk_rdata.msk_tx_ring[prod]; 2850 #ifdef MSK_64BIT_DMA 2851 if (MSK_ADDR_HI(txsegs[i].ds_addr) != 2852 sc_if->msk_cdata.msk_tx_high_addr) { 2853 sc_if->msk_cdata.msk_tx_high_addr = 2854 MSK_ADDR_HI(txsegs[i].ds_addr); 2855 tx_le = &sc_if->msk_rdata.msk_tx_ring[prod]; 2856 tx_le->msk_addr = 2857 htole32(MSK_ADDR_HI(txsegs[i].ds_addr)); 2858 tx_le->msk_control = htole32(OP_ADDR64 | HW_OWNER); 2859 sc_if->msk_cdata.msk_tx_cnt++; 2860 MSK_INC(prod, MSK_TX_RING_CNT); 2861 tx_le = &sc_if->msk_rdata.msk_tx_ring[prod]; 2862 } 2863 #endif 2864 tx_le->msk_addr = htole32(MSK_ADDR_LO(txsegs[i].ds_addr)); 2865 tx_le->msk_control = htole32(txsegs[i].ds_len | control | 2866 OP_BUFFER | HW_OWNER); 2867 sc_if->msk_cdata.msk_tx_cnt++; 2868 MSK_INC(prod, MSK_TX_RING_CNT); 2869 } 2870 /* Update producer index. */ 2871 sc_if->msk_cdata.msk_tx_prod = prod; 2872 2873 /* Set EOP on the last descriptor. */ 2874 prod = (prod + MSK_TX_RING_CNT - 1) % MSK_TX_RING_CNT; 2875 tx_le = &sc_if->msk_rdata.msk_tx_ring[prod]; 2876 tx_le->msk_control |= htole32(EOP); 2877 2878 /* Turn the first descriptor ownership to hardware. */ 2879 tx_le = &sc_if->msk_rdata.msk_tx_ring[si]; 2880 tx_le->msk_control |= htole32(HW_OWNER); 2881 2882 txd = &sc_if->msk_cdata.msk_txdesc[prod]; 2883 map = txd_last->tx_dmamap; 2884 txd_last->tx_dmamap = txd->tx_dmamap; 2885 txd->tx_dmamap = map; 2886 txd->tx_m = m; 2887 2888 /* Sync descriptors. */ 2889 bus_dmamap_sync(sc_if->msk_cdata.msk_tx_tag, map, BUS_DMASYNC_PREWRITE); 2890 bus_dmamap_sync(sc_if->msk_cdata.msk_tx_ring_tag, 2891 sc_if->msk_cdata.msk_tx_ring_map, 2892 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 2893 2894 return (0); 2895 } 2896 2897 static void 2898 msk_start(if_t ifp) 2899 { 2900 struct msk_if_softc *sc_if; 2901 2902 sc_if = if_getsoftc(ifp); 2903 MSK_IF_LOCK(sc_if); 2904 msk_start_locked(ifp); 2905 MSK_IF_UNLOCK(sc_if); 2906 } 2907 2908 static void 2909 msk_start_locked(if_t ifp) 2910 { 2911 struct msk_if_softc *sc_if; 2912 struct mbuf *m_head; 2913 int enq; 2914 2915 sc_if = if_getsoftc(ifp); 2916 MSK_IF_LOCK_ASSERT(sc_if); 2917 2918 if ((if_getdrvflags(ifp) & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) != 2919 IFF_DRV_RUNNING || (sc_if->msk_flags & MSK_FLAG_LINK) == 0) 2920 return; 2921 2922 for (enq = 0; !if_sendq_empty(ifp) && 2923 sc_if->msk_cdata.msk_tx_cnt < 2924 (MSK_TX_RING_CNT - MSK_RESERVED_TX_DESC_CNT); ) { 2925 m_head = if_dequeue(ifp); 2926 if (m_head == NULL) 2927 break; 2928 /* 2929 * Pack the data into the transmit ring. If we 2930 * don't have room, set the OACTIVE flag and wait 2931 * for the NIC to drain the ring. 2932 */ 2933 if (msk_encap(sc_if, &m_head) != 0) { 2934 if (m_head == NULL) 2935 break; 2936 if_sendq_prepend(ifp, m_head); 2937 if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0); 2938 break; 2939 } 2940 2941 enq++; 2942 /* 2943 * If there's a BPF listener, bounce a copy of this frame 2944 * to him. 2945 */ 2946 ETHER_BPF_MTAP(ifp, m_head); 2947 } 2948 2949 if (enq > 0) { 2950 /* Transmit */ 2951 CSR_WRITE_2(sc_if->msk_softc, 2952 Y2_PREF_Q_ADDR(sc_if->msk_txq, PREF_UNIT_PUT_IDX_REG), 2953 sc_if->msk_cdata.msk_tx_prod); 2954 2955 /* Set a timeout in case the chip goes out to lunch. */ 2956 sc_if->msk_watchdog_timer = MSK_TX_TIMEOUT; 2957 } 2958 } 2959 2960 static void 2961 msk_watchdog(struct msk_if_softc *sc_if) 2962 { 2963 if_t ifp; 2964 2965 MSK_IF_LOCK_ASSERT(sc_if); 2966 2967 if (sc_if->msk_watchdog_timer == 0 || --sc_if->msk_watchdog_timer) 2968 return; 2969 ifp = sc_if->msk_ifp; 2970 if ((sc_if->msk_flags & MSK_FLAG_LINK) == 0) { 2971 if (bootverbose) 2972 if_printf(sc_if->msk_ifp, "watchdog timeout " 2973 "(missed link)\n"); 2974 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 2975 if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING); 2976 msk_init_locked(sc_if); 2977 return; 2978 } 2979 2980 if_printf(ifp, "watchdog timeout\n"); 2981 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 2982 if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING); 2983 msk_init_locked(sc_if); 2984 if (!if_sendq_empty(ifp)) 2985 msk_start_locked(ifp); 2986 } 2987 2988 static int 2989 mskc_shutdown(device_t dev) 2990 { 2991 struct msk_softc *sc; 2992 int i; 2993 2994 sc = device_get_softc(dev); 2995 MSK_LOCK(sc); 2996 for (i = 0; i < sc->msk_num_port; i++) { 2997 if (sc->msk_if[i] != NULL && sc->msk_if[i]->msk_ifp != NULL && 2998 ((if_getdrvflags(sc->msk_if[i]->msk_ifp) & 2999 IFF_DRV_RUNNING) != 0)) 3000 msk_stop(sc->msk_if[i]); 3001 } 3002 MSK_UNLOCK(sc); 3003 3004 /* Put hardware reset. */ 3005 CSR_WRITE_2(sc, B0_CTST, CS_RST_SET); 3006 return (0); 3007 } 3008 3009 static int 3010 mskc_suspend(device_t dev) 3011 { 3012 struct msk_softc *sc; 3013 int i; 3014 3015 sc = device_get_softc(dev); 3016 3017 MSK_LOCK(sc); 3018 3019 for (i = 0; i < sc->msk_num_port; i++) { 3020 if (sc->msk_if[i] != NULL && sc->msk_if[i]->msk_ifp != NULL && 3021 ((if_getdrvflags(sc->msk_if[i]->msk_ifp) & 3022 IFF_DRV_RUNNING) != 0)) 3023 msk_stop(sc->msk_if[i]); 3024 } 3025 3026 /* Disable all interrupts. */ 3027 CSR_WRITE_4(sc, B0_IMSK, 0); 3028 CSR_READ_4(sc, B0_IMSK); 3029 CSR_WRITE_4(sc, B0_HWE_IMSK, 0); 3030 CSR_READ_4(sc, B0_HWE_IMSK); 3031 3032 msk_phy_power(sc, MSK_PHY_POWERDOWN); 3033 3034 /* Put hardware reset. */ 3035 CSR_WRITE_2(sc, B0_CTST, CS_RST_SET); 3036 sc->msk_pflags |= MSK_FLAG_SUSPEND; 3037 3038 MSK_UNLOCK(sc); 3039 3040 return (0); 3041 } 3042 3043 static int 3044 mskc_resume(device_t dev) 3045 { 3046 struct msk_softc *sc; 3047 int i; 3048 3049 sc = device_get_softc(dev); 3050 3051 MSK_LOCK(sc); 3052 3053 CSR_PCI_WRITE_4(sc, PCI_OUR_REG_3, 0); 3054 mskc_reset(sc); 3055 for (i = 0; i < sc->msk_num_port; i++) { 3056 if (sc->msk_if[i] != NULL && sc->msk_if[i]->msk_ifp != NULL && 3057 ((if_getflags(sc->msk_if[i]->msk_ifp) & IFF_UP) != 0)) { 3058 if_setdrvflagbits(sc->msk_if[i]->msk_ifp, 0, 3059 IFF_DRV_RUNNING); 3060 msk_init_locked(sc->msk_if[i]); 3061 } 3062 } 3063 sc->msk_pflags &= ~MSK_FLAG_SUSPEND; 3064 3065 MSK_UNLOCK(sc); 3066 3067 return (0); 3068 } 3069 3070 #ifndef __NO_STRICT_ALIGNMENT 3071 static __inline void 3072 msk_fixup_rx(struct mbuf *m) 3073 { 3074 int i; 3075 uint16_t *src, *dst; 3076 3077 src = mtod(m, uint16_t *); 3078 dst = src - 3; 3079 3080 for (i = 0; i < (m->m_len / sizeof(uint16_t) + 1); i++) 3081 *dst++ = *src++; 3082 3083 m->m_data -= (MSK_RX_BUF_ALIGN - ETHER_ALIGN); 3084 } 3085 #endif 3086 3087 static __inline void 3088 msk_rxcsum(struct msk_if_softc *sc_if, uint32_t control, struct mbuf *m) 3089 { 3090 struct ether_header *eh; 3091 struct ip *ip; 3092 struct udphdr *uh; 3093 int32_t hlen, len, pktlen, temp32; 3094 uint16_t csum, *opts; 3095 3096 if ((sc_if->msk_flags & MSK_FLAG_DESCV2) != 0) { 3097 if ((control & (CSS_IPV4 | CSS_IPFRAG)) == CSS_IPV4) { 3098 m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED; 3099 if ((control & CSS_IPV4_CSUM_OK) != 0) 3100 m->m_pkthdr.csum_flags |= CSUM_IP_VALID; 3101 if ((control & (CSS_TCP | CSS_UDP)) != 0 && 3102 (control & (CSS_TCPUDP_CSUM_OK)) != 0) { 3103 m->m_pkthdr.csum_flags |= CSUM_DATA_VALID | 3104 CSUM_PSEUDO_HDR; 3105 m->m_pkthdr.csum_data = 0xffff; 3106 } 3107 } 3108 return; 3109 } 3110 /* 3111 * Marvell Yukon controllers that support OP_RXCHKS has known 3112 * to have various Rx checksum offloading bugs. These 3113 * controllers can be configured to compute simple checksum 3114 * at two different positions. So we can compute IP and TCP/UDP 3115 * checksum at the same time. We intentionally have controller 3116 * compute TCP/UDP checksum twice by specifying the same 3117 * checksum start position and compare the result. If the value 3118 * is different it would indicate the hardware logic was wrong. 3119 */ 3120 if ((sc_if->msk_csum & 0xFFFF) != (sc_if->msk_csum >> 16)) { 3121 if (bootverbose) 3122 device_printf(sc_if->msk_if_dev, 3123 "Rx checksum value mismatch!\n"); 3124 return; 3125 } 3126 pktlen = m->m_pkthdr.len; 3127 if (pktlen < sizeof(struct ether_header) + sizeof(struct ip)) 3128 return; 3129 eh = mtod(m, struct ether_header *); 3130 if (eh->ether_type != htons(ETHERTYPE_IP)) 3131 return; 3132 ip = (struct ip *)(eh + 1); 3133 if (ip->ip_v != IPVERSION) 3134 return; 3135 3136 hlen = ip->ip_hl << 2; 3137 pktlen -= sizeof(struct ether_header); 3138 if (hlen < sizeof(struct ip)) 3139 return; 3140 if (ntohs(ip->ip_len) < hlen) 3141 return; 3142 if (ntohs(ip->ip_len) != pktlen) 3143 return; 3144 if (ip->ip_off & htons(IP_MF | IP_OFFMASK)) 3145 return; /* can't handle fragmented packet. */ 3146 3147 switch (ip->ip_p) { 3148 case IPPROTO_TCP: 3149 if (pktlen < (hlen + sizeof(struct tcphdr))) 3150 return; 3151 break; 3152 case IPPROTO_UDP: 3153 if (pktlen < (hlen + sizeof(struct udphdr))) 3154 return; 3155 uh = (struct udphdr *)((caddr_t)ip + hlen); 3156 if (uh->uh_sum == 0) 3157 return; /* no checksum */ 3158 break; 3159 default: 3160 return; 3161 } 3162 csum = bswap16(sc_if->msk_csum & 0xFFFF); 3163 /* Checksum fixup for IP options. */ 3164 len = hlen - sizeof(struct ip); 3165 if (len > 0) { 3166 opts = (uint16_t *)(ip + 1); 3167 for (; len > 0; len -= sizeof(uint16_t), opts++) { 3168 temp32 = csum - *opts; 3169 temp32 = (temp32 >> 16) + (temp32 & 65535); 3170 csum = temp32 & 65535; 3171 } 3172 } 3173 m->m_pkthdr.csum_flags |= CSUM_DATA_VALID; 3174 m->m_pkthdr.csum_data = csum; 3175 } 3176 3177 static void 3178 msk_rxeof(struct msk_if_softc *sc_if, uint32_t status, uint32_t control, 3179 int len) 3180 { 3181 struct mbuf *m; 3182 if_t ifp; 3183 struct msk_rxdesc *rxd; 3184 int cons, rxlen; 3185 3186 ifp = sc_if->msk_ifp; 3187 3188 MSK_IF_LOCK_ASSERT(sc_if); 3189 3190 cons = sc_if->msk_cdata.msk_rx_cons; 3191 do { 3192 rxlen = status >> 16; 3193 if ((status & GMR_FS_VLAN) != 0 && 3194 (if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING) != 0) 3195 rxlen -= ETHER_VLAN_ENCAP_LEN; 3196 if ((sc_if->msk_flags & MSK_FLAG_NORXCHK) != 0) { 3197 /* 3198 * For controllers that returns bogus status code 3199 * just do minimal check and let upper stack 3200 * handle this frame. 3201 */ 3202 if (len > MSK_MAX_FRAMELEN || len < ETHER_HDR_LEN) { 3203 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 3204 msk_discard_rxbuf(sc_if, cons); 3205 break; 3206 } 3207 } else if (len > sc_if->msk_framesize || 3208 ((status & GMR_FS_ANY_ERR) != 0) || 3209 ((status & GMR_FS_RX_OK) == 0) || (rxlen != len)) { 3210 /* Don't count flow-control packet as errors. */ 3211 if ((status & GMR_FS_GOOD_FC) == 0) 3212 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 3213 msk_discard_rxbuf(sc_if, cons); 3214 break; 3215 } 3216 #ifdef MSK_64BIT_DMA 3217 rxd = &sc_if->msk_cdata.msk_rxdesc[(cons + 1) % 3218 MSK_RX_RING_CNT]; 3219 #else 3220 rxd = &sc_if->msk_cdata.msk_rxdesc[cons]; 3221 #endif 3222 m = rxd->rx_m; 3223 if (msk_newbuf(sc_if, cons) != 0) { 3224 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1); 3225 /* Reuse old buffer. */ 3226 msk_discard_rxbuf(sc_if, cons); 3227 break; 3228 } 3229 m->m_pkthdr.rcvif = ifp; 3230 m->m_pkthdr.len = m->m_len = len; 3231 #ifndef __NO_STRICT_ALIGNMENT 3232 if ((sc_if->msk_flags & MSK_FLAG_RAMBUF) != 0) 3233 msk_fixup_rx(m); 3234 #endif 3235 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1); 3236 if ((if_getcapenable(ifp) & IFCAP_RXCSUM) != 0) 3237 msk_rxcsum(sc_if, control, m); 3238 /* Check for VLAN tagged packets. */ 3239 if ((status & GMR_FS_VLAN) != 0 && 3240 (if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING) != 0) { 3241 m->m_pkthdr.ether_vtag = sc_if->msk_vtag; 3242 m->m_flags |= M_VLANTAG; 3243 } 3244 MSK_IF_UNLOCK(sc_if); 3245 if_input(ifp, m); 3246 MSK_IF_LOCK(sc_if); 3247 } while (0); 3248 3249 MSK_RX_INC(sc_if->msk_cdata.msk_rx_cons, MSK_RX_RING_CNT); 3250 MSK_RX_INC(sc_if->msk_cdata.msk_rx_prod, MSK_RX_RING_CNT); 3251 } 3252 3253 static void 3254 msk_jumbo_rxeof(struct msk_if_softc *sc_if, uint32_t status, uint32_t control, 3255 int len) 3256 { 3257 struct mbuf *m; 3258 if_t ifp; 3259 struct msk_rxdesc *jrxd; 3260 int cons, rxlen; 3261 3262 ifp = sc_if->msk_ifp; 3263 3264 MSK_IF_LOCK_ASSERT(sc_if); 3265 3266 cons = sc_if->msk_cdata.msk_rx_cons; 3267 do { 3268 rxlen = status >> 16; 3269 if ((status & GMR_FS_VLAN) != 0 && 3270 (if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING) != 0) 3271 rxlen -= ETHER_VLAN_ENCAP_LEN; 3272 if (len > sc_if->msk_framesize || 3273 ((status & GMR_FS_ANY_ERR) != 0) || 3274 ((status & GMR_FS_RX_OK) == 0) || (rxlen != len)) { 3275 /* Don't count flow-control packet as errors. */ 3276 if ((status & GMR_FS_GOOD_FC) == 0) 3277 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 3278 msk_discard_jumbo_rxbuf(sc_if, cons); 3279 break; 3280 } 3281 #ifdef MSK_64BIT_DMA 3282 jrxd = &sc_if->msk_cdata.msk_jumbo_rxdesc[(cons + 1) % 3283 MSK_JUMBO_RX_RING_CNT]; 3284 #else 3285 jrxd = &sc_if->msk_cdata.msk_jumbo_rxdesc[cons]; 3286 #endif 3287 m = jrxd->rx_m; 3288 if (msk_jumbo_newbuf(sc_if, cons) != 0) { 3289 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1); 3290 /* Reuse old buffer. */ 3291 msk_discard_jumbo_rxbuf(sc_if, cons); 3292 break; 3293 } 3294 m->m_pkthdr.rcvif = ifp; 3295 m->m_pkthdr.len = m->m_len = len; 3296 #ifndef __NO_STRICT_ALIGNMENT 3297 if ((sc_if->msk_flags & MSK_FLAG_RAMBUF) != 0) 3298 msk_fixup_rx(m); 3299 #endif 3300 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1); 3301 if ((if_getcapenable(ifp) & IFCAP_RXCSUM) != 0) 3302 msk_rxcsum(sc_if, control, m); 3303 /* Check for VLAN tagged packets. */ 3304 if ((status & GMR_FS_VLAN) != 0 && 3305 (if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING) != 0) { 3306 m->m_pkthdr.ether_vtag = sc_if->msk_vtag; 3307 m->m_flags |= M_VLANTAG; 3308 } 3309 MSK_IF_UNLOCK(sc_if); 3310 if_input(ifp, m); 3311 MSK_IF_LOCK(sc_if); 3312 } while (0); 3313 3314 MSK_RX_INC(sc_if->msk_cdata.msk_rx_cons, MSK_JUMBO_RX_RING_CNT); 3315 MSK_RX_INC(sc_if->msk_cdata.msk_rx_prod, MSK_JUMBO_RX_RING_CNT); 3316 } 3317 3318 static void 3319 msk_txeof(struct msk_if_softc *sc_if, int idx) 3320 { 3321 struct msk_txdesc *txd; 3322 struct msk_tx_desc *cur_tx; 3323 if_t ifp; 3324 uint32_t control; 3325 int cons, prog; 3326 3327 MSK_IF_LOCK_ASSERT(sc_if); 3328 3329 ifp = sc_if->msk_ifp; 3330 3331 bus_dmamap_sync(sc_if->msk_cdata.msk_tx_ring_tag, 3332 sc_if->msk_cdata.msk_tx_ring_map, 3333 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 3334 /* 3335 * Go through our tx ring and free mbufs for those 3336 * frames that have been sent. 3337 */ 3338 cons = sc_if->msk_cdata.msk_tx_cons; 3339 prog = 0; 3340 for (; cons != idx; MSK_INC(cons, MSK_TX_RING_CNT)) { 3341 if (sc_if->msk_cdata.msk_tx_cnt <= 0) 3342 break; 3343 prog++; 3344 cur_tx = &sc_if->msk_rdata.msk_tx_ring[cons]; 3345 control = le32toh(cur_tx->msk_control); 3346 sc_if->msk_cdata.msk_tx_cnt--; 3347 if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE); 3348 if ((control & EOP) == 0) 3349 continue; 3350 txd = &sc_if->msk_cdata.msk_txdesc[cons]; 3351 bus_dmamap_sync(sc_if->msk_cdata.msk_tx_tag, txd->tx_dmamap, 3352 BUS_DMASYNC_POSTWRITE); 3353 bus_dmamap_unload(sc_if->msk_cdata.msk_tx_tag, txd->tx_dmamap); 3354 3355 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); 3356 KASSERT(txd->tx_m != NULL, ("%s: freeing NULL mbuf!", 3357 __func__)); 3358 m_freem(txd->tx_m); 3359 txd->tx_m = NULL; 3360 } 3361 3362 if (prog > 0) { 3363 sc_if->msk_cdata.msk_tx_cons = cons; 3364 if (sc_if->msk_cdata.msk_tx_cnt == 0) 3365 sc_if->msk_watchdog_timer = 0; 3366 /* No need to sync LEs as we didn't update LEs. */ 3367 } 3368 } 3369 3370 static void 3371 msk_tick(void *xsc_if) 3372 { 3373 struct epoch_tracker et; 3374 struct msk_if_softc *sc_if; 3375 struct mii_data *mii; 3376 3377 sc_if = xsc_if; 3378 3379 MSK_IF_LOCK_ASSERT(sc_if); 3380 3381 mii = device_get_softc(sc_if->msk_miibus); 3382 3383 mii_tick(mii); 3384 if ((sc_if->msk_flags & MSK_FLAG_LINK) == 0) 3385 msk_miibus_statchg(sc_if->msk_if_dev); 3386 NET_EPOCH_ENTER(et); 3387 msk_handle_events(sc_if->msk_softc); 3388 NET_EPOCH_EXIT(et); 3389 msk_watchdog(sc_if); 3390 callout_reset(&sc_if->msk_tick_ch, hz, msk_tick, sc_if); 3391 } 3392 3393 static void 3394 msk_intr_phy(struct msk_if_softc *sc_if) 3395 { 3396 uint16_t status; 3397 3398 msk_phy_readreg(sc_if, PHY_ADDR_MARV, PHY_MARV_INT_STAT); 3399 status = msk_phy_readreg(sc_if, PHY_ADDR_MARV, PHY_MARV_INT_STAT); 3400 /* Handle FIFO Underrun/Overflow? */ 3401 if ((status & PHY_M_IS_FIFO_ERROR)) 3402 device_printf(sc_if->msk_if_dev, 3403 "PHY FIFO underrun/overflow.\n"); 3404 } 3405 3406 static void 3407 msk_intr_gmac(struct msk_if_softc *sc_if) 3408 { 3409 struct msk_softc *sc; 3410 uint8_t status; 3411 3412 sc = sc_if->msk_softc; 3413 status = CSR_READ_1(sc, MR_ADDR(sc_if->msk_port, GMAC_IRQ_SRC)); 3414 3415 /* GMAC Rx FIFO overrun. */ 3416 if ((status & GM_IS_RX_FF_OR) != 0) 3417 CSR_WRITE_4(sc, MR_ADDR(sc_if->msk_port, RX_GMF_CTRL_T), 3418 GMF_CLI_RX_FO); 3419 /* GMAC Tx FIFO underrun. */ 3420 if ((status & GM_IS_TX_FF_UR) != 0) { 3421 CSR_WRITE_4(sc, MR_ADDR(sc_if->msk_port, TX_GMF_CTRL_T), 3422 GMF_CLI_TX_FU); 3423 device_printf(sc_if->msk_if_dev, "Tx FIFO underrun!\n"); 3424 /* 3425 * XXX 3426 * In case of Tx underrun, we may need to flush/reset 3427 * Tx MAC but that would also require resynchronization 3428 * with status LEs. Reinitializing status LEs would 3429 * affect other port in dual MAC configuration so it 3430 * should be avoided as possible as we can. 3431 * Due to lack of documentation it's all vague guess but 3432 * it needs more investigation. 3433 */ 3434 } 3435 } 3436 3437 static void 3438 msk_handle_hwerr(struct msk_if_softc *sc_if, uint32_t status) 3439 { 3440 struct msk_softc *sc; 3441 3442 sc = sc_if->msk_softc; 3443 if ((status & Y2_IS_PAR_RD1) != 0) { 3444 device_printf(sc_if->msk_if_dev, 3445 "RAM buffer read parity error\n"); 3446 /* Clear IRQ. */ 3447 CSR_WRITE_2(sc, SELECT_RAM_BUFFER(sc_if->msk_port, B3_RI_CTRL), 3448 RI_CLR_RD_PERR); 3449 } 3450 if ((status & Y2_IS_PAR_WR1) != 0) { 3451 device_printf(sc_if->msk_if_dev, 3452 "RAM buffer write parity error\n"); 3453 /* Clear IRQ. */ 3454 CSR_WRITE_2(sc, SELECT_RAM_BUFFER(sc_if->msk_port, B3_RI_CTRL), 3455 RI_CLR_WR_PERR); 3456 } 3457 if ((status & Y2_IS_PAR_MAC1) != 0) { 3458 device_printf(sc_if->msk_if_dev, "Tx MAC parity error\n"); 3459 /* Clear IRQ. */ 3460 CSR_WRITE_4(sc, MR_ADDR(sc_if->msk_port, TX_GMF_CTRL_T), 3461 GMF_CLI_TX_PE); 3462 } 3463 if ((status & Y2_IS_PAR_RX1) != 0) { 3464 device_printf(sc_if->msk_if_dev, "Rx parity error\n"); 3465 /* Clear IRQ. */ 3466 CSR_WRITE_4(sc, Q_ADDR(sc_if->msk_rxq, Q_CSR), BMU_CLR_IRQ_PAR); 3467 } 3468 if ((status & (Y2_IS_TCP_TXS1 | Y2_IS_TCP_TXA1)) != 0) { 3469 device_printf(sc_if->msk_if_dev, "TCP segmentation error\n"); 3470 /* Clear IRQ. */ 3471 CSR_WRITE_4(sc, Q_ADDR(sc_if->msk_txq, Q_CSR), BMU_CLR_IRQ_TCP); 3472 } 3473 } 3474 3475 static void 3476 msk_intr_hwerr(struct msk_softc *sc) 3477 { 3478 uint32_t status; 3479 uint32_t tlphead[4]; 3480 3481 status = CSR_READ_4(sc, B0_HWE_ISRC); 3482 /* Time Stamp timer overflow. */ 3483 if ((status & Y2_IS_TIST_OV) != 0) 3484 CSR_WRITE_1(sc, GMAC_TI_ST_CTRL, GMT_ST_CLR_IRQ); 3485 if ((status & Y2_IS_PCI_NEXP) != 0) { 3486 /* 3487 * PCI Express Error occurred which is not described in PEX 3488 * spec. 3489 * This error is also mapped either to Master Abort( 3490 * Y2_IS_MST_ERR) or Target Abort (Y2_IS_IRQ_STAT) bit and 3491 * can only be cleared there. 3492 */ 3493 device_printf(sc->msk_dev, 3494 "PCI Express protocol violation error\n"); 3495 } 3496 3497 if ((status & (Y2_IS_MST_ERR | Y2_IS_IRQ_STAT)) != 0) { 3498 uint16_t v16; 3499 3500 if ((status & Y2_IS_MST_ERR) != 0) 3501 device_printf(sc->msk_dev, 3502 "unexpected IRQ Status error\n"); 3503 else 3504 device_printf(sc->msk_dev, 3505 "unexpected IRQ Master error\n"); 3506 /* Reset all bits in the PCI status register. */ 3507 v16 = pci_read_config(sc->msk_dev, PCIR_STATUS, 2); 3508 CSR_WRITE_1(sc, B2_TST_CTRL1, TST_CFG_WRITE_ON); 3509 pci_write_config(sc->msk_dev, PCIR_STATUS, v16 | 3510 PCIM_STATUS_PERR | PCIM_STATUS_SERR | PCIM_STATUS_RMABORT | 3511 PCIM_STATUS_RTABORT | PCIM_STATUS_MDPERR, 2); 3512 CSR_WRITE_1(sc, B2_TST_CTRL1, TST_CFG_WRITE_OFF); 3513 } 3514 3515 /* Check for PCI Express Uncorrectable Error. */ 3516 if ((status & Y2_IS_PCI_EXP) != 0) { 3517 uint32_t v32; 3518 3519 /* 3520 * On PCI Express bus bridges are called root complexes (RC). 3521 * PCI Express errors are recognized by the root complex too, 3522 * which requests the system to handle the problem. After 3523 * error occurrence it may be that no access to the adapter 3524 * may be performed any longer. 3525 */ 3526 3527 v32 = CSR_PCI_READ_4(sc, PEX_UNC_ERR_STAT); 3528 if ((v32 & PEX_UNSUP_REQ) != 0) { 3529 /* Ignore unsupported request error. */ 3530 device_printf(sc->msk_dev, 3531 "Uncorrectable PCI Express error\n"); 3532 } 3533 if ((v32 & (PEX_FATAL_ERRORS | PEX_POIS_TLP)) != 0) { 3534 int i; 3535 3536 /* Get TLP header form Log Registers. */ 3537 for (i = 0; i < 4; i++) 3538 tlphead[i] = CSR_PCI_READ_4(sc, 3539 PEX_HEADER_LOG + i * 4); 3540 /* Check for vendor defined broadcast message. */ 3541 if (!(tlphead[0] == 0x73004001 && tlphead[1] == 0x7f)) { 3542 sc->msk_intrhwemask &= ~Y2_IS_PCI_EXP; 3543 CSR_WRITE_4(sc, B0_HWE_IMSK, 3544 sc->msk_intrhwemask); 3545 CSR_READ_4(sc, B0_HWE_IMSK); 3546 } 3547 } 3548 /* Clear the interrupt. */ 3549 CSR_WRITE_1(sc, B2_TST_CTRL1, TST_CFG_WRITE_ON); 3550 CSR_PCI_WRITE_4(sc, PEX_UNC_ERR_STAT, 0xffffffff); 3551 CSR_WRITE_1(sc, B2_TST_CTRL1, TST_CFG_WRITE_OFF); 3552 } 3553 3554 if ((status & Y2_HWE_L1_MASK) != 0 && sc->msk_if[MSK_PORT_A] != NULL) 3555 msk_handle_hwerr(sc->msk_if[MSK_PORT_A], status); 3556 if ((status & Y2_HWE_L2_MASK) != 0 && sc->msk_if[MSK_PORT_B] != NULL) 3557 msk_handle_hwerr(sc->msk_if[MSK_PORT_B], status >> 8); 3558 } 3559 3560 static __inline void 3561 msk_rxput(struct msk_if_softc *sc_if) 3562 { 3563 struct msk_softc *sc; 3564 3565 sc = sc_if->msk_softc; 3566 if (sc_if->msk_framesize > (MCLBYTES - MSK_RX_BUF_ALIGN)) 3567 bus_dmamap_sync( 3568 sc_if->msk_cdata.msk_jumbo_rx_ring_tag, 3569 sc_if->msk_cdata.msk_jumbo_rx_ring_map, 3570 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 3571 else 3572 bus_dmamap_sync( 3573 sc_if->msk_cdata.msk_rx_ring_tag, 3574 sc_if->msk_cdata.msk_rx_ring_map, 3575 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 3576 CSR_WRITE_2(sc, Y2_PREF_Q_ADDR(sc_if->msk_rxq, 3577 PREF_UNIT_PUT_IDX_REG), sc_if->msk_cdata.msk_rx_prod); 3578 } 3579 3580 static int 3581 msk_handle_events(struct msk_softc *sc) 3582 { 3583 struct msk_if_softc *sc_if; 3584 int rxput[2]; 3585 struct msk_stat_desc *sd; 3586 uint32_t control, status; 3587 int cons, len, port, rxprog; 3588 3589 if (sc->msk_stat_cons == CSR_READ_2(sc, STAT_PUT_IDX)) 3590 return (0); 3591 3592 /* Sync status LEs. */ 3593 bus_dmamap_sync(sc->msk_stat_tag, sc->msk_stat_map, 3594 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 3595 3596 rxput[MSK_PORT_A] = rxput[MSK_PORT_B] = 0; 3597 rxprog = 0; 3598 cons = sc->msk_stat_cons; 3599 for (;;) { 3600 sd = &sc->msk_stat_ring[cons]; 3601 control = le32toh(sd->msk_control); 3602 if ((control & HW_OWNER) == 0) 3603 break; 3604 control &= ~HW_OWNER; 3605 sd->msk_control = htole32(control); 3606 status = le32toh(sd->msk_status); 3607 len = control & STLE_LEN_MASK; 3608 port = (control >> 16) & 0x01; 3609 sc_if = sc->msk_if[port]; 3610 if (sc_if == NULL) { 3611 device_printf(sc->msk_dev, "invalid port opcode " 3612 "0x%08x\n", control & STLE_OP_MASK); 3613 continue; 3614 } 3615 3616 switch (control & STLE_OP_MASK) { 3617 case OP_RXVLAN: 3618 sc_if->msk_vtag = ntohs(len); 3619 break; 3620 case OP_RXCHKSVLAN: 3621 sc_if->msk_vtag = ntohs(len); 3622 /* FALLTHROUGH */ 3623 case OP_RXCHKS: 3624 sc_if->msk_csum = status; 3625 break; 3626 case OP_RXSTAT: 3627 if (!(if_getdrvflags(sc_if->msk_ifp) & IFF_DRV_RUNNING)) 3628 break; 3629 if (sc_if->msk_framesize > 3630 (MCLBYTES - MSK_RX_BUF_ALIGN)) 3631 msk_jumbo_rxeof(sc_if, status, control, len); 3632 else 3633 msk_rxeof(sc_if, status, control, len); 3634 rxprog++; 3635 /* 3636 * Because there is no way to sync single Rx LE 3637 * put the DMA sync operation off until the end of 3638 * event processing. 3639 */ 3640 rxput[port]++; 3641 /* Update prefetch unit if we've passed water mark. */ 3642 if (rxput[port] >= sc_if->msk_cdata.msk_rx_putwm) { 3643 msk_rxput(sc_if); 3644 rxput[port] = 0; 3645 } 3646 break; 3647 case OP_TXINDEXLE: 3648 if (sc->msk_if[MSK_PORT_A] != NULL) 3649 msk_txeof(sc->msk_if[MSK_PORT_A], 3650 status & STLE_TXA1_MSKL); 3651 if (sc->msk_if[MSK_PORT_B] != NULL) 3652 msk_txeof(sc->msk_if[MSK_PORT_B], 3653 ((status & STLE_TXA2_MSKL) >> 3654 STLE_TXA2_SHIFTL) | 3655 ((len & STLE_TXA2_MSKH) << 3656 STLE_TXA2_SHIFTH)); 3657 break; 3658 default: 3659 device_printf(sc->msk_dev, "unhandled opcode 0x%08x\n", 3660 control & STLE_OP_MASK); 3661 break; 3662 } 3663 MSK_INC(cons, sc->msk_stat_count); 3664 if (rxprog > sc->msk_process_limit) 3665 break; 3666 } 3667 3668 sc->msk_stat_cons = cons; 3669 bus_dmamap_sync(sc->msk_stat_tag, sc->msk_stat_map, 3670 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 3671 3672 if (rxput[MSK_PORT_A] > 0) 3673 msk_rxput(sc->msk_if[MSK_PORT_A]); 3674 if (rxput[MSK_PORT_B] > 0) 3675 msk_rxput(sc->msk_if[MSK_PORT_B]); 3676 3677 return (sc->msk_stat_cons != CSR_READ_2(sc, STAT_PUT_IDX)); 3678 } 3679 3680 static void 3681 msk_intr(void *xsc) 3682 { 3683 struct msk_softc *sc; 3684 struct msk_if_softc *sc_if0, *sc_if1; 3685 if_t ifp0, ifp1; 3686 uint32_t status; 3687 int domore; 3688 3689 sc = xsc; 3690 MSK_LOCK(sc); 3691 3692 /* Reading B0_Y2_SP_ISRC2 masks further interrupts. */ 3693 status = CSR_READ_4(sc, B0_Y2_SP_ISRC2); 3694 if (status == 0 || status == 0xffffffff || 3695 (sc->msk_pflags & MSK_FLAG_SUSPEND) != 0 || 3696 (status & sc->msk_intrmask) == 0) { 3697 CSR_WRITE_4(sc, B0_Y2_SP_ICR, 2); 3698 MSK_UNLOCK(sc); 3699 return; 3700 } 3701 3702 sc_if0 = sc->msk_if[MSK_PORT_A]; 3703 sc_if1 = sc->msk_if[MSK_PORT_B]; 3704 ifp0 = ifp1 = NULL; 3705 if (sc_if0 != NULL) 3706 ifp0 = sc_if0->msk_ifp; 3707 if (sc_if1 != NULL) 3708 ifp1 = sc_if1->msk_ifp; 3709 3710 if ((status & Y2_IS_IRQ_PHY1) != 0 && sc_if0 != NULL) 3711 msk_intr_phy(sc_if0); 3712 if ((status & Y2_IS_IRQ_PHY2) != 0 && sc_if1 != NULL) 3713 msk_intr_phy(sc_if1); 3714 if ((status & Y2_IS_IRQ_MAC1) != 0 && sc_if0 != NULL) 3715 msk_intr_gmac(sc_if0); 3716 if ((status & Y2_IS_IRQ_MAC2) != 0 && sc_if1 != NULL) 3717 msk_intr_gmac(sc_if1); 3718 if ((status & (Y2_IS_CHK_RX1 | Y2_IS_CHK_RX2)) != 0) { 3719 device_printf(sc->msk_dev, "Rx descriptor error\n"); 3720 sc->msk_intrmask &= ~(Y2_IS_CHK_RX1 | Y2_IS_CHK_RX2); 3721 CSR_WRITE_4(sc, B0_IMSK, sc->msk_intrmask); 3722 CSR_READ_4(sc, B0_IMSK); 3723 } 3724 if ((status & (Y2_IS_CHK_TXA1 | Y2_IS_CHK_TXA2)) != 0) { 3725 device_printf(sc->msk_dev, "Tx descriptor error\n"); 3726 sc->msk_intrmask &= ~(Y2_IS_CHK_TXA1 | Y2_IS_CHK_TXA2); 3727 CSR_WRITE_4(sc, B0_IMSK, sc->msk_intrmask); 3728 CSR_READ_4(sc, B0_IMSK); 3729 } 3730 if ((status & Y2_IS_HW_ERR) != 0) 3731 msk_intr_hwerr(sc); 3732 3733 domore = msk_handle_events(sc); 3734 if ((status & Y2_IS_STAT_BMU) != 0 && domore == 0) 3735 CSR_WRITE_4(sc, STAT_CTRL, SC_STAT_CLR_IRQ); 3736 3737 /* Reenable interrupts. */ 3738 CSR_WRITE_4(sc, B0_Y2_SP_ICR, 2); 3739 3740 if (ifp0 != NULL && (if_getdrvflags(ifp0) & IFF_DRV_RUNNING) != 0 && 3741 !if_sendq_empty(ifp0)) 3742 msk_start_locked(ifp0); 3743 if (ifp1 != NULL && (if_getdrvflags(ifp1) & IFF_DRV_RUNNING) != 0 && 3744 !if_sendq_empty(ifp1)) 3745 msk_start_locked(ifp1); 3746 3747 MSK_UNLOCK(sc); 3748 } 3749 3750 static void 3751 msk_set_tx_stfwd(struct msk_if_softc *sc_if) 3752 { 3753 struct msk_softc *sc; 3754 if_t ifp; 3755 3756 ifp = sc_if->msk_ifp; 3757 sc = sc_if->msk_softc; 3758 if ((sc->msk_hw_id == CHIP_ID_YUKON_EX && 3759 sc->msk_hw_rev != CHIP_REV_YU_EX_A0) || 3760 sc->msk_hw_id >= CHIP_ID_YUKON_SUPR) { 3761 CSR_WRITE_4(sc, MR_ADDR(sc_if->msk_port, TX_GMF_CTRL_T), 3762 TX_STFW_ENA); 3763 } else { 3764 if (if_getmtu(ifp) > ETHERMTU) { 3765 /* Set Tx GMAC FIFO Almost Empty Threshold. */ 3766 CSR_WRITE_4(sc, 3767 MR_ADDR(sc_if->msk_port, TX_GMF_AE_THR), 3768 MSK_ECU_JUMBO_WM << 16 | MSK_ECU_AE_THR); 3769 /* Disable Store & Forward mode for Tx. */ 3770 CSR_WRITE_4(sc, MR_ADDR(sc_if->msk_port, TX_GMF_CTRL_T), 3771 TX_STFW_DIS); 3772 } else { 3773 CSR_WRITE_4(sc, MR_ADDR(sc_if->msk_port, TX_GMF_CTRL_T), 3774 TX_STFW_ENA); 3775 } 3776 } 3777 } 3778 3779 static void 3780 msk_init(void *xsc) 3781 { 3782 struct msk_if_softc *sc_if = xsc; 3783 3784 MSK_IF_LOCK(sc_if); 3785 msk_init_locked(sc_if); 3786 MSK_IF_UNLOCK(sc_if); 3787 } 3788 3789 static void 3790 msk_init_locked(struct msk_if_softc *sc_if) 3791 { 3792 struct msk_softc *sc; 3793 if_t ifp; 3794 struct mii_data *mii; 3795 uint8_t *eaddr; 3796 uint16_t gmac; 3797 uint32_t reg; 3798 int error; 3799 3800 MSK_IF_LOCK_ASSERT(sc_if); 3801 3802 ifp = sc_if->msk_ifp; 3803 sc = sc_if->msk_softc; 3804 mii = device_get_softc(sc_if->msk_miibus); 3805 3806 if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0) 3807 return; 3808 3809 error = 0; 3810 /* Cancel pending I/O and free all Rx/Tx buffers. */ 3811 msk_stop(sc_if); 3812 3813 if (if_getmtu(ifp) < ETHERMTU) 3814 sc_if->msk_framesize = ETHERMTU; 3815 else 3816 sc_if->msk_framesize = if_getmtu(ifp); 3817 sc_if->msk_framesize += ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN; 3818 if (if_getmtu(ifp) > ETHERMTU && 3819 (sc_if->msk_flags & MSK_FLAG_JUMBO_NOCSUM) != 0) { 3820 if_sethwassistbits(ifp, 0, (MSK_CSUM_FEATURES | CSUM_TSO)); 3821 if_setcapenablebit(ifp, 0, (IFCAP_TSO4 | IFCAP_TXCSUM)); 3822 } 3823 3824 /* GMAC Control reset. */ 3825 CSR_WRITE_4(sc, MR_ADDR(sc_if->msk_port, GMAC_CTRL), GMC_RST_SET); 3826 CSR_WRITE_4(sc, MR_ADDR(sc_if->msk_port, GMAC_CTRL), GMC_RST_CLR); 3827 CSR_WRITE_4(sc, MR_ADDR(sc_if->msk_port, GMAC_CTRL), GMC_F_LOOPB_OFF); 3828 if (sc->msk_hw_id == CHIP_ID_YUKON_EX || 3829 sc->msk_hw_id == CHIP_ID_YUKON_SUPR) 3830 CSR_WRITE_4(sc, MR_ADDR(sc_if->msk_port, GMAC_CTRL), 3831 GMC_BYP_MACSECRX_ON | GMC_BYP_MACSECTX_ON | 3832 GMC_BYP_RETR_ON); 3833 3834 /* 3835 * Initialize GMAC first such that speed/duplex/flow-control 3836 * parameters are renegotiated when interface is brought up. 3837 */ 3838 GMAC_WRITE_2(sc, sc_if->msk_port, GM_GP_CTRL, 0); 3839 3840 /* Dummy read the Interrupt Source Register. */ 3841 CSR_READ_1(sc, MR_ADDR(sc_if->msk_port, GMAC_IRQ_SRC)); 3842 3843 /* Clear MIB stats. */ 3844 msk_stats_clear(sc_if); 3845 3846 /* Disable FCS. */ 3847 GMAC_WRITE_2(sc, sc_if->msk_port, GM_RX_CTRL, GM_RXCR_CRC_DIS); 3848 3849 /* Setup Transmit Control Register. */ 3850 GMAC_WRITE_2(sc, sc_if->msk_port, GM_TX_CTRL, TX_COL_THR(TX_COL_DEF)); 3851 3852 /* Setup Transmit Flow Control Register. */ 3853 GMAC_WRITE_2(sc, sc_if->msk_port, GM_TX_FLOW_CTRL, 0xffff); 3854 3855 /* Setup Transmit Parameter Register. */ 3856 GMAC_WRITE_2(sc, sc_if->msk_port, GM_TX_PARAM, 3857 TX_JAM_LEN_VAL(TX_JAM_LEN_DEF) | TX_JAM_IPG_VAL(TX_JAM_IPG_DEF) | 3858 TX_IPG_JAM_DATA(TX_IPG_JAM_DEF) | TX_BACK_OFF_LIM(TX_BOF_LIM_DEF)); 3859 3860 gmac = DATA_BLIND_VAL(DATA_BLIND_DEF) | 3861 GM_SMOD_VLAN_ENA | IPG_DATA_VAL(IPG_DATA_DEF); 3862 3863 if (if_getmtu(ifp) > ETHERMTU) 3864 gmac |= GM_SMOD_JUMBO_ENA; 3865 GMAC_WRITE_2(sc, sc_if->msk_port, GM_SERIAL_MODE, gmac); 3866 3867 /* Set station address. */ 3868 eaddr = if_getlladdr(ifp); 3869 GMAC_WRITE_2(sc, sc_if->msk_port, GM_SRC_ADDR_1L, 3870 eaddr[0] | (eaddr[1] << 8)); 3871 GMAC_WRITE_2(sc, sc_if->msk_port, GM_SRC_ADDR_1M, 3872 eaddr[2] | (eaddr[3] << 8)); 3873 GMAC_WRITE_2(sc, sc_if->msk_port, GM_SRC_ADDR_1H, 3874 eaddr[4] | (eaddr[5] << 8)); 3875 GMAC_WRITE_2(sc, sc_if->msk_port, GM_SRC_ADDR_2L, 3876 eaddr[0] | (eaddr[1] << 8)); 3877 GMAC_WRITE_2(sc, sc_if->msk_port, GM_SRC_ADDR_2M, 3878 eaddr[2] | (eaddr[3] << 8)); 3879 GMAC_WRITE_2(sc, sc_if->msk_port, GM_SRC_ADDR_2H, 3880 eaddr[4] | (eaddr[5] << 8)); 3881 3882 /* Disable interrupts for counter overflows. */ 3883 GMAC_WRITE_2(sc, sc_if->msk_port, GM_TX_IRQ_MSK, 0); 3884 GMAC_WRITE_2(sc, sc_if->msk_port, GM_RX_IRQ_MSK, 0); 3885 GMAC_WRITE_2(sc, sc_if->msk_port, GM_TR_IRQ_MSK, 0); 3886 3887 /* Configure Rx MAC FIFO. */ 3888 CSR_WRITE_4(sc, MR_ADDR(sc_if->msk_port, RX_GMF_CTRL_T), GMF_RST_SET); 3889 CSR_WRITE_4(sc, MR_ADDR(sc_if->msk_port, RX_GMF_CTRL_T), GMF_RST_CLR); 3890 reg = GMF_OPER_ON | GMF_RX_F_FL_ON; 3891 if (sc->msk_hw_id == CHIP_ID_YUKON_FE_P || 3892 sc->msk_hw_id == CHIP_ID_YUKON_EX) 3893 reg |= GMF_RX_OVER_ON; 3894 CSR_WRITE_4(sc, MR_ADDR(sc_if->msk_port, RX_GMF_CTRL_T), reg); 3895 3896 /* Set receive filter. */ 3897 msk_rxfilter(sc_if); 3898 3899 if (sc->msk_hw_id == CHIP_ID_YUKON_XL) { 3900 /* Clear flush mask - HW bug. */ 3901 CSR_WRITE_4(sc, MR_ADDR(sc_if->msk_port, RX_GMF_FL_MSK), 0); 3902 } else { 3903 /* Flush Rx MAC FIFO on any flow control or error. */ 3904 CSR_WRITE_4(sc, MR_ADDR(sc_if->msk_port, RX_GMF_FL_MSK), 3905 GMR_FS_ANY_ERR); 3906 } 3907 3908 /* 3909 * Set Rx FIFO flush threshold to 64 bytes + 1 FIFO word 3910 * due to hardware hang on receipt of pause frames. 3911 */ 3912 reg = RX_GMF_FL_THR_DEF + 1; 3913 /* Another magic for Yukon FE+ - From Linux. */ 3914 if (sc->msk_hw_id == CHIP_ID_YUKON_FE_P && 3915 sc->msk_hw_rev == CHIP_REV_YU_FE_P_A0) 3916 reg = 0x178; 3917 CSR_WRITE_2(sc, MR_ADDR(sc_if->msk_port, RX_GMF_FL_THR), reg); 3918 3919 /* Configure Tx MAC FIFO. */ 3920 CSR_WRITE_4(sc, MR_ADDR(sc_if->msk_port, TX_GMF_CTRL_T), GMF_RST_SET); 3921 CSR_WRITE_4(sc, MR_ADDR(sc_if->msk_port, TX_GMF_CTRL_T), GMF_RST_CLR); 3922 CSR_WRITE_4(sc, MR_ADDR(sc_if->msk_port, TX_GMF_CTRL_T), GMF_OPER_ON); 3923 3924 /* Configure hardware VLAN tag insertion/stripping. */ 3925 msk_setvlan(sc_if, ifp); 3926 3927 if ((sc_if->msk_flags & MSK_FLAG_RAMBUF) == 0) { 3928 /* Set Rx Pause threshold. */ 3929 CSR_WRITE_2(sc, MR_ADDR(sc_if->msk_port, RX_GMF_LP_THR), 3930 MSK_ECU_LLPP); 3931 CSR_WRITE_2(sc, MR_ADDR(sc_if->msk_port, RX_GMF_UP_THR), 3932 MSK_ECU_ULPP); 3933 /* Configure store-and-forward for Tx. */ 3934 msk_set_tx_stfwd(sc_if); 3935 } 3936 3937 if (sc->msk_hw_id == CHIP_ID_YUKON_FE_P && 3938 sc->msk_hw_rev == CHIP_REV_YU_FE_P_A0) { 3939 /* Disable dynamic watermark - from Linux. */ 3940 reg = CSR_READ_4(sc, MR_ADDR(sc_if->msk_port, TX_GMF_EA)); 3941 reg &= ~0x03; 3942 CSR_WRITE_4(sc, MR_ADDR(sc_if->msk_port, TX_GMF_EA), reg); 3943 } 3944 3945 /* 3946 * Disable Force Sync bit and Alloc bit in Tx RAM interface 3947 * arbiter as we don't use Sync Tx queue. 3948 */ 3949 CSR_WRITE_1(sc, MR_ADDR(sc_if->msk_port, TXA_CTRL), 3950 TXA_DIS_FSYNC | TXA_DIS_ALLOC | TXA_STOP_RC); 3951 /* Enable the RAM Interface Arbiter. */ 3952 CSR_WRITE_1(sc, MR_ADDR(sc_if->msk_port, TXA_CTRL), TXA_ENA_ARB); 3953 3954 /* Setup RAM buffer. */ 3955 msk_set_rambuffer(sc_if); 3956 3957 /* Disable Tx sync Queue. */ 3958 CSR_WRITE_1(sc, RB_ADDR(sc_if->msk_txsq, RB_CTRL), RB_RST_SET); 3959 3960 /* Setup Tx Queue Bus Memory Interface. */ 3961 CSR_WRITE_4(sc, Q_ADDR(sc_if->msk_txq, Q_CSR), BMU_CLR_RESET); 3962 CSR_WRITE_4(sc, Q_ADDR(sc_if->msk_txq, Q_CSR), BMU_OPER_INIT); 3963 CSR_WRITE_4(sc, Q_ADDR(sc_if->msk_txq, Q_CSR), BMU_FIFO_OP_ON); 3964 CSR_WRITE_2(sc, Q_ADDR(sc_if->msk_txq, Q_WM), MSK_BMU_TX_WM); 3965 switch (sc->msk_hw_id) { 3966 case CHIP_ID_YUKON_EC_U: 3967 if (sc->msk_hw_rev == CHIP_REV_YU_EC_U_A0) { 3968 /* Fix for Yukon-EC Ultra: set BMU FIFO level */ 3969 CSR_WRITE_2(sc, Q_ADDR(sc_if->msk_txq, Q_AL), 3970 MSK_ECU_TXFF_LEV); 3971 } 3972 break; 3973 case CHIP_ID_YUKON_EX: 3974 /* 3975 * Yukon Extreme seems to have silicon bug for 3976 * automatic Tx checksum calculation capability. 3977 */ 3978 if (sc->msk_hw_rev == CHIP_REV_YU_EX_B0) 3979 CSR_WRITE_4(sc, Q_ADDR(sc_if->msk_txq, Q_F), 3980 F_TX_CHK_AUTO_OFF); 3981 break; 3982 } 3983 3984 /* Setup Rx Queue Bus Memory Interface. */ 3985 CSR_WRITE_4(sc, Q_ADDR(sc_if->msk_rxq, Q_CSR), BMU_CLR_RESET); 3986 CSR_WRITE_4(sc, Q_ADDR(sc_if->msk_rxq, Q_CSR), BMU_OPER_INIT); 3987 CSR_WRITE_4(sc, Q_ADDR(sc_if->msk_rxq, Q_CSR), BMU_FIFO_OP_ON); 3988 CSR_WRITE_2(sc, Q_ADDR(sc_if->msk_rxq, Q_WM), MSK_BMU_RX_WM); 3989 if (sc->msk_hw_id == CHIP_ID_YUKON_EC_U && 3990 sc->msk_hw_rev >= CHIP_REV_YU_EC_U_A1) { 3991 /* MAC Rx RAM Read is controlled by hardware. */ 3992 CSR_WRITE_4(sc, Q_ADDR(sc_if->msk_rxq, Q_F), F_M_RX_RAM_DIS); 3993 } 3994 3995 msk_set_prefetch(sc, sc_if->msk_txq, 3996 sc_if->msk_rdata.msk_tx_ring_paddr, MSK_TX_RING_CNT - 1); 3997 msk_init_tx_ring(sc_if); 3998 3999 /* Disable Rx checksum offload and RSS hash. */ 4000 reg = BMU_DIS_RX_RSS_HASH; 4001 if ((sc_if->msk_flags & MSK_FLAG_DESCV2) == 0 && 4002 (if_getcapenable(ifp) & IFCAP_RXCSUM) != 0) 4003 reg |= BMU_ENA_RX_CHKSUM; 4004 else 4005 reg |= BMU_DIS_RX_CHKSUM; 4006 CSR_WRITE_4(sc, Q_ADDR(sc_if->msk_rxq, Q_CSR), reg); 4007 if (sc_if->msk_framesize > (MCLBYTES - MSK_RX_BUF_ALIGN)) { 4008 msk_set_prefetch(sc, sc_if->msk_rxq, 4009 sc_if->msk_rdata.msk_jumbo_rx_ring_paddr, 4010 MSK_JUMBO_RX_RING_CNT - 1); 4011 error = msk_init_jumbo_rx_ring(sc_if); 4012 } else { 4013 msk_set_prefetch(sc, sc_if->msk_rxq, 4014 sc_if->msk_rdata.msk_rx_ring_paddr, 4015 MSK_RX_RING_CNT - 1); 4016 error = msk_init_rx_ring(sc_if); 4017 } 4018 if (error != 0) { 4019 device_printf(sc_if->msk_if_dev, 4020 "initialization failed: no memory for Rx buffers\n"); 4021 msk_stop(sc_if); 4022 return; 4023 } 4024 if (sc->msk_hw_id == CHIP_ID_YUKON_EX || 4025 sc->msk_hw_id == CHIP_ID_YUKON_SUPR) { 4026 /* Disable flushing of non-ASF packets. */ 4027 CSR_WRITE_4(sc, MR_ADDR(sc_if->msk_port, RX_GMF_CTRL_T), 4028 GMF_RX_MACSEC_FLUSH_OFF); 4029 } 4030 4031 /* Configure interrupt handling. */ 4032 if (sc_if->msk_port == MSK_PORT_A) { 4033 sc->msk_intrmask |= Y2_IS_PORT_A; 4034 sc->msk_intrhwemask |= Y2_HWE_L1_MASK; 4035 } else { 4036 sc->msk_intrmask |= Y2_IS_PORT_B; 4037 sc->msk_intrhwemask |= Y2_HWE_L2_MASK; 4038 } 4039 /* Configure IRQ moderation mask. */ 4040 CSR_WRITE_4(sc, B2_IRQM_MSK, sc->msk_intrmask); 4041 if (sc->msk_int_holdoff > 0) { 4042 /* Configure initial IRQ moderation timer value. */ 4043 CSR_WRITE_4(sc, B2_IRQM_INI, 4044 MSK_USECS(sc, sc->msk_int_holdoff)); 4045 CSR_WRITE_4(sc, B2_IRQM_VAL, 4046 MSK_USECS(sc, sc->msk_int_holdoff)); 4047 /* Start IRQ moderation. */ 4048 CSR_WRITE_1(sc, B2_IRQM_CTRL, TIM_START); 4049 } 4050 CSR_WRITE_4(sc, B0_HWE_IMSK, sc->msk_intrhwemask); 4051 CSR_READ_4(sc, B0_HWE_IMSK); 4052 CSR_WRITE_4(sc, B0_IMSK, sc->msk_intrmask); 4053 CSR_READ_4(sc, B0_IMSK); 4054 4055 if_setdrvflagbits(ifp, IFF_DRV_RUNNING, 0); 4056 if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE); 4057 4058 sc_if->msk_flags &= ~MSK_FLAG_LINK; 4059 mii_mediachg(mii); 4060 4061 callout_reset(&sc_if->msk_tick_ch, hz, msk_tick, sc_if); 4062 } 4063 4064 static void 4065 msk_set_rambuffer(struct msk_if_softc *sc_if) 4066 { 4067 struct msk_softc *sc; 4068 int ltpp, utpp; 4069 4070 sc = sc_if->msk_softc; 4071 if ((sc_if->msk_flags & MSK_FLAG_RAMBUF) == 0) 4072 return; 4073 4074 /* Setup Rx Queue. */ 4075 CSR_WRITE_1(sc, RB_ADDR(sc_if->msk_rxq, RB_CTRL), RB_RST_CLR); 4076 CSR_WRITE_4(sc, RB_ADDR(sc_if->msk_rxq, RB_START), 4077 sc->msk_rxqstart[sc_if->msk_port] / 8); 4078 CSR_WRITE_4(sc, RB_ADDR(sc_if->msk_rxq, RB_END), 4079 sc->msk_rxqend[sc_if->msk_port] / 8); 4080 CSR_WRITE_4(sc, RB_ADDR(sc_if->msk_rxq, RB_WP), 4081 sc->msk_rxqstart[sc_if->msk_port] / 8); 4082 CSR_WRITE_4(sc, RB_ADDR(sc_if->msk_rxq, RB_RP), 4083 sc->msk_rxqstart[sc_if->msk_port] / 8); 4084 4085 utpp = (sc->msk_rxqend[sc_if->msk_port] + 1 - 4086 sc->msk_rxqstart[sc_if->msk_port] - MSK_RB_ULPP) / 8; 4087 ltpp = (sc->msk_rxqend[sc_if->msk_port] + 1 - 4088 sc->msk_rxqstart[sc_if->msk_port] - MSK_RB_LLPP_B) / 8; 4089 if (sc->msk_rxqsize < MSK_MIN_RXQ_SIZE) 4090 ltpp += (MSK_RB_LLPP_B - MSK_RB_LLPP_S) / 8; 4091 CSR_WRITE_4(sc, RB_ADDR(sc_if->msk_rxq, RB_RX_UTPP), utpp); 4092 CSR_WRITE_4(sc, RB_ADDR(sc_if->msk_rxq, RB_RX_LTPP), ltpp); 4093 /* Set Rx priority(RB_RX_UTHP/RB_RX_LTHP) thresholds? */ 4094 4095 CSR_WRITE_1(sc, RB_ADDR(sc_if->msk_rxq, RB_CTRL), RB_ENA_OP_MD); 4096 CSR_READ_1(sc, RB_ADDR(sc_if->msk_rxq, RB_CTRL)); 4097 4098 /* Setup Tx Queue. */ 4099 CSR_WRITE_1(sc, RB_ADDR(sc_if->msk_txq, RB_CTRL), RB_RST_CLR); 4100 CSR_WRITE_4(sc, RB_ADDR(sc_if->msk_txq, RB_START), 4101 sc->msk_txqstart[sc_if->msk_port] / 8); 4102 CSR_WRITE_4(sc, RB_ADDR(sc_if->msk_txq, RB_END), 4103 sc->msk_txqend[sc_if->msk_port] / 8); 4104 CSR_WRITE_4(sc, RB_ADDR(sc_if->msk_txq, RB_WP), 4105 sc->msk_txqstart[sc_if->msk_port] / 8); 4106 CSR_WRITE_4(sc, RB_ADDR(sc_if->msk_txq, RB_RP), 4107 sc->msk_txqstart[sc_if->msk_port] / 8); 4108 /* Enable Store & Forward for Tx side. */ 4109 CSR_WRITE_1(sc, RB_ADDR(sc_if->msk_txq, RB_CTRL), RB_ENA_STFWD); 4110 CSR_WRITE_1(sc, RB_ADDR(sc_if->msk_txq, RB_CTRL), RB_ENA_OP_MD); 4111 CSR_READ_1(sc, RB_ADDR(sc_if->msk_txq, RB_CTRL)); 4112 } 4113 4114 static void 4115 msk_set_prefetch(struct msk_softc *sc, int qaddr, bus_addr_t addr, 4116 uint32_t count) 4117 { 4118 4119 /* Reset the prefetch unit. */ 4120 CSR_WRITE_4(sc, Y2_PREF_Q_ADDR(qaddr, PREF_UNIT_CTRL_REG), 4121 PREF_UNIT_RST_SET); 4122 CSR_WRITE_4(sc, Y2_PREF_Q_ADDR(qaddr, PREF_UNIT_CTRL_REG), 4123 PREF_UNIT_RST_CLR); 4124 /* Set LE base address. */ 4125 CSR_WRITE_4(sc, Y2_PREF_Q_ADDR(qaddr, PREF_UNIT_ADDR_LOW_REG), 4126 MSK_ADDR_LO(addr)); 4127 CSR_WRITE_4(sc, Y2_PREF_Q_ADDR(qaddr, PREF_UNIT_ADDR_HI_REG), 4128 MSK_ADDR_HI(addr)); 4129 /* Set the list last index. */ 4130 CSR_WRITE_2(sc, Y2_PREF_Q_ADDR(qaddr, PREF_UNIT_LAST_IDX_REG), 4131 count); 4132 /* Turn on prefetch unit. */ 4133 CSR_WRITE_4(sc, Y2_PREF_Q_ADDR(qaddr, PREF_UNIT_CTRL_REG), 4134 PREF_UNIT_OP_ON); 4135 /* Dummy read to ensure write. */ 4136 CSR_READ_4(sc, Y2_PREF_Q_ADDR(qaddr, PREF_UNIT_CTRL_REG)); 4137 } 4138 4139 static void 4140 msk_stop(struct msk_if_softc *sc_if) 4141 { 4142 struct msk_softc *sc; 4143 struct msk_txdesc *txd; 4144 struct msk_rxdesc *rxd; 4145 struct msk_rxdesc *jrxd; 4146 if_t ifp; 4147 uint32_t val; 4148 int i; 4149 4150 MSK_IF_LOCK_ASSERT(sc_if); 4151 sc = sc_if->msk_softc; 4152 ifp = sc_if->msk_ifp; 4153 4154 callout_stop(&sc_if->msk_tick_ch); 4155 sc_if->msk_watchdog_timer = 0; 4156 4157 /* Disable interrupts. */ 4158 if (sc_if->msk_port == MSK_PORT_A) { 4159 sc->msk_intrmask &= ~Y2_IS_PORT_A; 4160 sc->msk_intrhwemask &= ~Y2_HWE_L1_MASK; 4161 } else { 4162 sc->msk_intrmask &= ~Y2_IS_PORT_B; 4163 sc->msk_intrhwemask &= ~Y2_HWE_L2_MASK; 4164 } 4165 CSR_WRITE_4(sc, B0_HWE_IMSK, sc->msk_intrhwemask); 4166 CSR_READ_4(sc, B0_HWE_IMSK); 4167 CSR_WRITE_4(sc, B0_IMSK, sc->msk_intrmask); 4168 CSR_READ_4(sc, B0_IMSK); 4169 4170 /* Disable Tx/Rx MAC. */ 4171 val = GMAC_READ_2(sc, sc_if->msk_port, GM_GP_CTRL); 4172 val &= ~(GM_GPCR_RX_ENA | GM_GPCR_TX_ENA); 4173 GMAC_WRITE_2(sc, sc_if->msk_port, GM_GP_CTRL, val); 4174 /* Read again to ensure writing. */ 4175 GMAC_READ_2(sc, sc_if->msk_port, GM_GP_CTRL); 4176 /* Update stats and clear counters. */ 4177 msk_stats_update(sc_if); 4178 4179 /* Stop Tx BMU. */ 4180 CSR_WRITE_4(sc, Q_ADDR(sc_if->msk_txq, Q_CSR), BMU_STOP); 4181 val = CSR_READ_4(sc, Q_ADDR(sc_if->msk_txq, Q_CSR)); 4182 for (i = 0; i < MSK_TIMEOUT; i++) { 4183 if ((val & (BMU_STOP | BMU_IDLE)) == 0) { 4184 CSR_WRITE_4(sc, Q_ADDR(sc_if->msk_txq, Q_CSR), 4185 BMU_STOP); 4186 val = CSR_READ_4(sc, Q_ADDR(sc_if->msk_txq, Q_CSR)); 4187 } else 4188 break; 4189 DELAY(1); 4190 } 4191 if (i == MSK_TIMEOUT) 4192 device_printf(sc_if->msk_if_dev, "Tx BMU stop failed\n"); 4193 CSR_WRITE_1(sc, RB_ADDR(sc_if->msk_txq, RB_CTRL), 4194 RB_RST_SET | RB_DIS_OP_MD); 4195 4196 /* Disable all GMAC interrupt. */ 4197 CSR_WRITE_1(sc, MR_ADDR(sc_if->msk_port, GMAC_IRQ_MSK), 0); 4198 /* Disable PHY interrupt. */ 4199 msk_phy_writereg(sc_if, PHY_ADDR_MARV, PHY_MARV_INT_MASK, 0); 4200 4201 /* Disable the RAM Interface Arbiter. */ 4202 CSR_WRITE_1(sc, MR_ADDR(sc_if->msk_port, TXA_CTRL), TXA_DIS_ARB); 4203 4204 /* Reset the PCI FIFO of the async Tx queue */ 4205 CSR_WRITE_4(sc, Q_ADDR(sc_if->msk_txq, Q_CSR), 4206 BMU_RST_SET | BMU_FIFO_RST); 4207 4208 /* Reset the Tx prefetch units. */ 4209 CSR_WRITE_4(sc, Y2_PREF_Q_ADDR(sc_if->msk_txq, PREF_UNIT_CTRL_REG), 4210 PREF_UNIT_RST_SET); 4211 4212 /* Reset the RAM Buffer async Tx queue. */ 4213 CSR_WRITE_1(sc, RB_ADDR(sc_if->msk_txq, RB_CTRL), RB_RST_SET); 4214 4215 /* Reset Tx MAC FIFO. */ 4216 CSR_WRITE_4(sc, MR_ADDR(sc_if->msk_port, TX_GMF_CTRL_T), GMF_RST_SET); 4217 /* Set Pause Off. */ 4218 CSR_WRITE_4(sc, MR_ADDR(sc_if->msk_port, GMAC_CTRL), GMC_PAUSE_OFF); 4219 4220 /* 4221 * The Rx Stop command will not work for Yukon-2 if the BMU does not 4222 * reach the end of packet and since we can't make sure that we have 4223 * incoming data, we must reset the BMU while it is not during a DMA 4224 * transfer. Since it is possible that the Rx path is still active, 4225 * the Rx RAM buffer will be stopped first, so any possible incoming 4226 * data will not trigger a DMA. After the RAM buffer is stopped, the 4227 * BMU is polled until any DMA in progress is ended and only then it 4228 * will be reset. 4229 */ 4230 4231 /* Disable the RAM Buffer receive queue. */ 4232 CSR_WRITE_1(sc, RB_ADDR(sc_if->msk_rxq, RB_CTRL), RB_DIS_OP_MD); 4233 for (i = 0; i < MSK_TIMEOUT; i++) { 4234 if (CSR_READ_1(sc, RB_ADDR(sc_if->msk_rxq, Q_RSL)) == 4235 CSR_READ_1(sc, RB_ADDR(sc_if->msk_rxq, Q_RL))) 4236 break; 4237 DELAY(1); 4238 } 4239 if (i == MSK_TIMEOUT) 4240 device_printf(sc_if->msk_if_dev, "Rx BMU stop failed\n"); 4241 CSR_WRITE_4(sc, Q_ADDR(sc_if->msk_rxq, Q_CSR), 4242 BMU_RST_SET | BMU_FIFO_RST); 4243 /* Reset the Rx prefetch unit. */ 4244 CSR_WRITE_4(sc, Y2_PREF_Q_ADDR(sc_if->msk_rxq, PREF_UNIT_CTRL_REG), 4245 PREF_UNIT_RST_SET); 4246 /* Reset the RAM Buffer receive queue. */ 4247 CSR_WRITE_1(sc, RB_ADDR(sc_if->msk_rxq, RB_CTRL), RB_RST_SET); 4248 /* Reset Rx MAC FIFO. */ 4249 CSR_WRITE_4(sc, MR_ADDR(sc_if->msk_port, RX_GMF_CTRL_T), GMF_RST_SET); 4250 4251 /* Free Rx and Tx mbufs still in the queues. */ 4252 for (i = 0; i < MSK_RX_RING_CNT; i++) { 4253 rxd = &sc_if->msk_cdata.msk_rxdesc[i]; 4254 if (rxd->rx_m != NULL) { 4255 bus_dmamap_sync(sc_if->msk_cdata.msk_rx_tag, 4256 rxd->rx_dmamap, BUS_DMASYNC_POSTREAD); 4257 bus_dmamap_unload(sc_if->msk_cdata.msk_rx_tag, 4258 rxd->rx_dmamap); 4259 m_freem(rxd->rx_m); 4260 rxd->rx_m = NULL; 4261 } 4262 } 4263 for (i = 0; i < MSK_JUMBO_RX_RING_CNT; i++) { 4264 jrxd = &sc_if->msk_cdata.msk_jumbo_rxdesc[i]; 4265 if (jrxd->rx_m != NULL) { 4266 bus_dmamap_sync(sc_if->msk_cdata.msk_jumbo_rx_tag, 4267 jrxd->rx_dmamap, BUS_DMASYNC_POSTREAD); 4268 bus_dmamap_unload(sc_if->msk_cdata.msk_jumbo_rx_tag, 4269 jrxd->rx_dmamap); 4270 m_freem(jrxd->rx_m); 4271 jrxd->rx_m = NULL; 4272 } 4273 } 4274 for (i = 0; i < MSK_TX_RING_CNT; i++) { 4275 txd = &sc_if->msk_cdata.msk_txdesc[i]; 4276 if (txd->tx_m != NULL) { 4277 bus_dmamap_sync(sc_if->msk_cdata.msk_tx_tag, 4278 txd->tx_dmamap, BUS_DMASYNC_POSTWRITE); 4279 bus_dmamap_unload(sc_if->msk_cdata.msk_tx_tag, 4280 txd->tx_dmamap); 4281 m_freem(txd->tx_m); 4282 txd->tx_m = NULL; 4283 } 4284 } 4285 4286 /* 4287 * Mark the interface down. 4288 */ 4289 if_setdrvflagbits(ifp, 0, (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)); 4290 sc_if->msk_flags &= ~MSK_FLAG_LINK; 4291 } 4292 4293 /* 4294 * When GM_PAR_MIB_CLR bit of GM_PHY_ADDR is set, reading lower 4295 * counter clears high 16 bits of the counter such that accessing 4296 * lower 16 bits should be the last operation. 4297 */ 4298 #define MSK_READ_MIB32(x, y) \ 4299 ((((uint32_t)GMAC_READ_2(sc, x, (y) + 4)) << 16) + \ 4300 (uint32_t)GMAC_READ_2(sc, x, y)) 4301 #define MSK_READ_MIB64(x, y) \ 4302 ((((uint64_t)MSK_READ_MIB32(x, (y) + 8)) << 32) + \ 4303 (uint64_t)MSK_READ_MIB32(x, y)) 4304 4305 static void 4306 msk_stats_clear(struct msk_if_softc *sc_if) 4307 { 4308 struct msk_softc *sc; 4309 uint16_t gmac; 4310 int i; 4311 4312 MSK_IF_LOCK_ASSERT(sc_if); 4313 4314 sc = sc_if->msk_softc; 4315 /* Set MIB Clear Counter Mode. */ 4316 gmac = GMAC_READ_2(sc, sc_if->msk_port, GM_PHY_ADDR); 4317 GMAC_WRITE_2(sc, sc_if->msk_port, GM_PHY_ADDR, gmac | GM_PAR_MIB_CLR); 4318 /* Read all MIB Counters with Clear Mode set. */ 4319 for (i = GM_RXF_UC_OK; i <= GM_TXE_FIFO_UR; i += sizeof(uint32_t)) 4320 (void)MSK_READ_MIB32(sc_if->msk_port, i); 4321 /* Clear MIB Clear Counter Mode. */ 4322 gmac &= ~GM_PAR_MIB_CLR; 4323 GMAC_WRITE_2(sc, sc_if->msk_port, GM_PHY_ADDR, gmac); 4324 } 4325 4326 static void 4327 msk_stats_update(struct msk_if_softc *sc_if) 4328 { 4329 struct msk_softc *sc; 4330 if_t ifp; 4331 struct msk_hw_stats *stats; 4332 uint16_t gmac; 4333 4334 MSK_IF_LOCK_ASSERT(sc_if); 4335 4336 ifp = sc_if->msk_ifp; 4337 if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0) 4338 return; 4339 sc = sc_if->msk_softc; 4340 stats = &sc_if->msk_stats; 4341 /* Set MIB Clear Counter Mode. */ 4342 gmac = GMAC_READ_2(sc, sc_if->msk_port, GM_PHY_ADDR); 4343 GMAC_WRITE_2(sc, sc_if->msk_port, GM_PHY_ADDR, gmac | GM_PAR_MIB_CLR); 4344 4345 /* Rx stats. */ 4346 stats->rx_ucast_frames += 4347 MSK_READ_MIB32(sc_if->msk_port, GM_RXF_UC_OK); 4348 stats->rx_bcast_frames += 4349 MSK_READ_MIB32(sc_if->msk_port, GM_RXF_BC_OK); 4350 stats->rx_pause_frames += 4351 MSK_READ_MIB32(sc_if->msk_port, GM_RXF_MPAUSE); 4352 stats->rx_mcast_frames += 4353 MSK_READ_MIB32(sc_if->msk_port, GM_RXF_MC_OK); 4354 stats->rx_crc_errs += 4355 MSK_READ_MIB32(sc_if->msk_port, GM_RXF_FCS_ERR); 4356 stats->rx_good_octets += 4357 MSK_READ_MIB64(sc_if->msk_port, GM_RXO_OK_LO); 4358 stats->rx_bad_octets += 4359 MSK_READ_MIB64(sc_if->msk_port, GM_RXO_ERR_LO); 4360 stats->rx_runts += 4361 MSK_READ_MIB32(sc_if->msk_port, GM_RXF_SHT); 4362 stats->rx_runt_errs += 4363 MSK_READ_MIB32(sc_if->msk_port, GM_RXE_FRAG); 4364 stats->rx_pkts_64 += 4365 MSK_READ_MIB32(sc_if->msk_port, GM_RXF_64B); 4366 stats->rx_pkts_65_127 += 4367 MSK_READ_MIB32(sc_if->msk_port, GM_RXF_127B); 4368 stats->rx_pkts_128_255 += 4369 MSK_READ_MIB32(sc_if->msk_port, GM_RXF_255B); 4370 stats->rx_pkts_256_511 += 4371 MSK_READ_MIB32(sc_if->msk_port, GM_RXF_511B); 4372 stats->rx_pkts_512_1023 += 4373 MSK_READ_MIB32(sc_if->msk_port, GM_RXF_1023B); 4374 stats->rx_pkts_1024_1518 += 4375 MSK_READ_MIB32(sc_if->msk_port, GM_RXF_1518B); 4376 stats->rx_pkts_1519_max += 4377 MSK_READ_MIB32(sc_if->msk_port, GM_RXF_MAX_SZ); 4378 stats->rx_pkts_too_long += 4379 MSK_READ_MIB32(sc_if->msk_port, GM_RXF_LNG_ERR); 4380 stats->rx_pkts_jabbers += 4381 MSK_READ_MIB32(sc_if->msk_port, GM_RXF_JAB_PKT); 4382 stats->rx_fifo_oflows += 4383 MSK_READ_MIB32(sc_if->msk_port, GM_RXE_FIFO_OV); 4384 4385 /* Tx stats. */ 4386 stats->tx_ucast_frames += 4387 MSK_READ_MIB32(sc_if->msk_port, GM_TXF_UC_OK); 4388 stats->tx_bcast_frames += 4389 MSK_READ_MIB32(sc_if->msk_port, GM_TXF_BC_OK); 4390 stats->tx_pause_frames += 4391 MSK_READ_MIB32(sc_if->msk_port, GM_TXF_MPAUSE); 4392 stats->tx_mcast_frames += 4393 MSK_READ_MIB32(sc_if->msk_port, GM_TXF_MC_OK); 4394 stats->tx_octets += 4395 MSK_READ_MIB64(sc_if->msk_port, GM_TXO_OK_LO); 4396 stats->tx_pkts_64 += 4397 MSK_READ_MIB32(sc_if->msk_port, GM_TXF_64B); 4398 stats->tx_pkts_65_127 += 4399 MSK_READ_MIB32(sc_if->msk_port, GM_TXF_127B); 4400 stats->tx_pkts_128_255 += 4401 MSK_READ_MIB32(sc_if->msk_port, GM_TXF_255B); 4402 stats->tx_pkts_256_511 += 4403 MSK_READ_MIB32(sc_if->msk_port, GM_TXF_511B); 4404 stats->tx_pkts_512_1023 += 4405 MSK_READ_MIB32(sc_if->msk_port, GM_TXF_1023B); 4406 stats->tx_pkts_1024_1518 += 4407 MSK_READ_MIB32(sc_if->msk_port, GM_TXF_1518B); 4408 stats->tx_pkts_1519_max += 4409 MSK_READ_MIB32(sc_if->msk_port, GM_TXF_MAX_SZ); 4410 stats->tx_colls += 4411 MSK_READ_MIB32(sc_if->msk_port, GM_TXF_COL); 4412 stats->tx_late_colls += 4413 MSK_READ_MIB32(sc_if->msk_port, GM_TXF_LAT_COL); 4414 stats->tx_excess_colls += 4415 MSK_READ_MIB32(sc_if->msk_port, GM_TXF_ABO_COL); 4416 stats->tx_multi_colls += 4417 MSK_READ_MIB32(sc_if->msk_port, GM_TXF_MUL_COL); 4418 stats->tx_single_colls += 4419 MSK_READ_MIB32(sc_if->msk_port, GM_TXF_SNG_COL); 4420 stats->tx_underflows += 4421 MSK_READ_MIB32(sc_if->msk_port, GM_TXE_FIFO_UR); 4422 /* Clear MIB Clear Counter Mode. */ 4423 gmac &= ~GM_PAR_MIB_CLR; 4424 GMAC_WRITE_2(sc, sc_if->msk_port, GM_PHY_ADDR, gmac); 4425 } 4426 4427 static int 4428 msk_sysctl_stat32(SYSCTL_HANDLER_ARGS) 4429 { 4430 struct msk_softc *sc; 4431 struct msk_if_softc *sc_if; 4432 uint32_t result, *stat; 4433 int off; 4434 4435 sc_if = (struct msk_if_softc *)arg1; 4436 sc = sc_if->msk_softc; 4437 off = arg2; 4438 stat = (uint32_t *)((uint8_t *)&sc_if->msk_stats + off); 4439 4440 MSK_IF_LOCK(sc_if); 4441 result = MSK_READ_MIB32(sc_if->msk_port, GM_MIB_CNT_BASE + off * 2); 4442 result += *stat; 4443 MSK_IF_UNLOCK(sc_if); 4444 4445 return (sysctl_handle_int(oidp, &result, 0, req)); 4446 } 4447 4448 static int 4449 msk_sysctl_stat64(SYSCTL_HANDLER_ARGS) 4450 { 4451 struct msk_softc *sc; 4452 struct msk_if_softc *sc_if; 4453 uint64_t result, *stat; 4454 int off; 4455 4456 sc_if = (struct msk_if_softc *)arg1; 4457 sc = sc_if->msk_softc; 4458 off = arg2; 4459 stat = (uint64_t *)((uint8_t *)&sc_if->msk_stats + off); 4460 4461 MSK_IF_LOCK(sc_if); 4462 result = MSK_READ_MIB64(sc_if->msk_port, GM_MIB_CNT_BASE + off * 2); 4463 result += *stat; 4464 MSK_IF_UNLOCK(sc_if); 4465 4466 return (sysctl_handle_64(oidp, &result, 0, req)); 4467 } 4468 4469 #undef MSK_READ_MIB32 4470 #undef MSK_READ_MIB64 4471 4472 #define MSK_SYSCTL_STAT32(sc, c, o, p, n, d) \ 4473 SYSCTL_ADD_PROC(c, p, OID_AUTO, o, \ 4474 CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_NEEDGIANT, \ 4475 sc, offsetof(struct msk_hw_stats, n), msk_sysctl_stat32, \ 4476 "IU", d) 4477 #define MSK_SYSCTL_STAT64(sc, c, o, p, n, d) \ 4478 SYSCTL_ADD_PROC(c, p, OID_AUTO, o, \ 4479 CTLTYPE_U64 | CTLFLAG_RD | CTLFLAG_NEEDGIANT, \ 4480 sc, offsetof(struct msk_hw_stats, n), msk_sysctl_stat64, \ 4481 "QU", d) 4482 4483 static void 4484 msk_sysctl_node(struct msk_if_softc *sc_if) 4485 { 4486 struct sysctl_ctx_list *ctx; 4487 struct sysctl_oid_list *child, *schild; 4488 struct sysctl_oid *tree; 4489 4490 ctx = device_get_sysctl_ctx(sc_if->msk_if_dev); 4491 child = SYSCTL_CHILDREN(device_get_sysctl_tree(sc_if->msk_if_dev)); 4492 4493 tree = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "stats", 4494 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "MSK Statistics"); 4495 schild = SYSCTL_CHILDREN(tree); 4496 tree = SYSCTL_ADD_NODE(ctx, schild, OID_AUTO, "rx", 4497 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "MSK RX Statistics"); 4498 child = SYSCTL_CHILDREN(tree); 4499 MSK_SYSCTL_STAT32(sc_if, ctx, "ucast_frames", 4500 child, rx_ucast_frames, "Good unicast frames"); 4501 MSK_SYSCTL_STAT32(sc_if, ctx, "bcast_frames", 4502 child, rx_bcast_frames, "Good broadcast frames"); 4503 MSK_SYSCTL_STAT32(sc_if, ctx, "pause_frames", 4504 child, rx_pause_frames, "Pause frames"); 4505 MSK_SYSCTL_STAT32(sc_if, ctx, "mcast_frames", 4506 child, rx_mcast_frames, "Multicast frames"); 4507 MSK_SYSCTL_STAT32(sc_if, ctx, "crc_errs", 4508 child, rx_crc_errs, "CRC errors"); 4509 MSK_SYSCTL_STAT64(sc_if, ctx, "good_octets", 4510 child, rx_good_octets, "Good octets"); 4511 MSK_SYSCTL_STAT64(sc_if, ctx, "bad_octets", 4512 child, rx_bad_octets, "Bad octets"); 4513 MSK_SYSCTL_STAT32(sc_if, ctx, "frames_64", 4514 child, rx_pkts_64, "64 bytes frames"); 4515 MSK_SYSCTL_STAT32(sc_if, ctx, "frames_65_127", 4516 child, rx_pkts_65_127, "65 to 127 bytes frames"); 4517 MSK_SYSCTL_STAT32(sc_if, ctx, "frames_128_255", 4518 child, rx_pkts_128_255, "128 to 255 bytes frames"); 4519 MSK_SYSCTL_STAT32(sc_if, ctx, "frames_256_511", 4520 child, rx_pkts_256_511, "256 to 511 bytes frames"); 4521 MSK_SYSCTL_STAT32(sc_if, ctx, "frames_512_1023", 4522 child, rx_pkts_512_1023, "512 to 1023 bytes frames"); 4523 MSK_SYSCTL_STAT32(sc_if, ctx, "frames_1024_1518", 4524 child, rx_pkts_1024_1518, "1024 to 1518 bytes frames"); 4525 MSK_SYSCTL_STAT32(sc_if, ctx, "frames_1519_max", 4526 child, rx_pkts_1519_max, "1519 to max frames"); 4527 MSK_SYSCTL_STAT32(sc_if, ctx, "frames_too_long", 4528 child, rx_pkts_too_long, "frames too long"); 4529 MSK_SYSCTL_STAT32(sc_if, ctx, "jabbers", 4530 child, rx_pkts_jabbers, "Jabber errors"); 4531 MSK_SYSCTL_STAT32(sc_if, ctx, "overflows", 4532 child, rx_fifo_oflows, "FIFO overflows"); 4533 4534 tree = SYSCTL_ADD_NODE(ctx, schild, OID_AUTO, "tx", 4535 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "MSK TX Statistics"); 4536 child = SYSCTL_CHILDREN(tree); 4537 MSK_SYSCTL_STAT32(sc_if, ctx, "ucast_frames", 4538 child, tx_ucast_frames, "Unicast frames"); 4539 MSK_SYSCTL_STAT32(sc_if, ctx, "bcast_frames", 4540 child, tx_bcast_frames, "Broadcast frames"); 4541 MSK_SYSCTL_STAT32(sc_if, ctx, "pause_frames", 4542 child, tx_pause_frames, "Pause frames"); 4543 MSK_SYSCTL_STAT32(sc_if, ctx, "mcast_frames", 4544 child, tx_mcast_frames, "Multicast frames"); 4545 MSK_SYSCTL_STAT64(sc_if, ctx, "octets", 4546 child, tx_octets, "Octets"); 4547 MSK_SYSCTL_STAT32(sc_if, ctx, "frames_64", 4548 child, tx_pkts_64, "64 bytes frames"); 4549 MSK_SYSCTL_STAT32(sc_if, ctx, "frames_65_127", 4550 child, tx_pkts_65_127, "65 to 127 bytes frames"); 4551 MSK_SYSCTL_STAT32(sc_if, ctx, "frames_128_255", 4552 child, tx_pkts_128_255, "128 to 255 bytes frames"); 4553 MSK_SYSCTL_STAT32(sc_if, ctx, "frames_256_511", 4554 child, tx_pkts_256_511, "256 to 511 bytes frames"); 4555 MSK_SYSCTL_STAT32(sc_if, ctx, "frames_512_1023", 4556 child, tx_pkts_512_1023, "512 to 1023 bytes frames"); 4557 MSK_SYSCTL_STAT32(sc_if, ctx, "frames_1024_1518", 4558 child, tx_pkts_1024_1518, "1024 to 1518 bytes frames"); 4559 MSK_SYSCTL_STAT32(sc_if, ctx, "frames_1519_max", 4560 child, tx_pkts_1519_max, "1519 to max frames"); 4561 MSK_SYSCTL_STAT32(sc_if, ctx, "colls", 4562 child, tx_colls, "Collisions"); 4563 MSK_SYSCTL_STAT32(sc_if, ctx, "late_colls", 4564 child, tx_late_colls, "Late collisions"); 4565 MSK_SYSCTL_STAT32(sc_if, ctx, "excess_colls", 4566 child, tx_excess_colls, "Excessive collisions"); 4567 MSK_SYSCTL_STAT32(sc_if, ctx, "multi_colls", 4568 child, tx_multi_colls, "Multiple collisions"); 4569 MSK_SYSCTL_STAT32(sc_if, ctx, "single_colls", 4570 child, tx_single_colls, "Single collisions"); 4571 MSK_SYSCTL_STAT32(sc_if, ctx, "underflows", 4572 child, tx_underflows, "FIFO underflows"); 4573 } 4574 4575 #undef MSK_SYSCTL_STAT32 4576 #undef MSK_SYSCTL_STAT64 4577 4578 static int 4579 sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high) 4580 { 4581 int error, value; 4582 4583 if (!arg1) 4584 return (EINVAL); 4585 value = *(int *)arg1; 4586 error = sysctl_handle_int(oidp, &value, 0, req); 4587 if (error || !req->newptr) 4588 return (error); 4589 if (value < low || value > high) 4590 return (EINVAL); 4591 *(int *)arg1 = value; 4592 4593 return (0); 4594 } 4595 4596 static int 4597 sysctl_hw_msk_proc_limit(SYSCTL_HANDLER_ARGS) 4598 { 4599 4600 return (sysctl_int_range(oidp, arg1, arg2, req, MSK_PROC_MIN, 4601 MSK_PROC_MAX)); 4602 } 4603