1 /* 2 * Copyright (c) 2017 Stormshield. 3 * Copyright (c) 2017 Semihalf. 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 19 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 23 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 24 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 * POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #include "opt_platform.h" 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/endian.h> 35 #include <sys/mbuf.h> 36 #include <sys/lock.h> 37 #include <sys/mutex.h> 38 #include <sys/kernel.h> 39 #include <sys/module.h> 40 #include <sys/socket.h> 41 #include <sys/sysctl.h> 42 #include <sys/smp.h> 43 #include <sys/taskqueue.h> 44 #ifdef MVNETA_KTR 45 #include <sys/ktr.h> 46 #endif 47 48 #include <net/ethernet.h> 49 #include <net/bpf.h> 50 #include <net/if.h> 51 #include <net/if_arp.h> 52 #include <net/if_dl.h> 53 #include <net/if_media.h> 54 #include <net/if_types.h> 55 #include <net/if_vlan_var.h> 56 57 #include <netinet/in_systm.h> 58 #include <netinet/in.h> 59 #include <netinet/ip.h> 60 #include <netinet/tcp_lro.h> 61 62 #include <sys/sockio.h> 63 #include <sys/bus.h> 64 #include <machine/bus.h> 65 #include <sys/rman.h> 66 #include <machine/resource.h> 67 68 #include <dev/mii/mii.h> 69 #include <dev/mii/miivar.h> 70 71 #include <dev/ofw/openfirm.h> 72 #include <dev/ofw/ofw_bus.h> 73 #include <dev/ofw/ofw_bus_subr.h> 74 75 #include <dev/mdio/mdio.h> 76 77 #include <arm/mv/mvreg.h> 78 #include <arm/mv/mvvar.h> 79 #include <arm/mv/mvwin.h> 80 81 #include "if_mvnetareg.h" 82 #include "if_mvnetavar.h" 83 84 #include "miibus_if.h" 85 #include "mdio_if.h" 86 87 #ifdef MVNETA_DEBUG 88 #define STATIC /* nothing */ 89 #else 90 #define STATIC static 91 #endif 92 93 #define DASSERT(x) KASSERT((x), (#x)) 94 95 /* Device Register Initialization */ 96 STATIC int mvneta_initreg(struct ifnet *); 97 98 /* Descriptor Ring Control for each of queues */ 99 STATIC int mvneta_ring_alloc_rx_queue(struct mvneta_softc *, int); 100 STATIC int mvneta_ring_alloc_tx_queue(struct mvneta_softc *, int); 101 STATIC void mvneta_ring_dealloc_rx_queue(struct mvneta_softc *, int); 102 STATIC void mvneta_ring_dealloc_tx_queue(struct mvneta_softc *, int); 103 STATIC int mvneta_ring_init_rx_queue(struct mvneta_softc *, int); 104 STATIC int mvneta_ring_init_tx_queue(struct mvneta_softc *, int); 105 STATIC void mvneta_ring_flush_rx_queue(struct mvneta_softc *, int); 106 STATIC void mvneta_ring_flush_tx_queue(struct mvneta_softc *, int); 107 STATIC void mvneta_dmamap_cb(void *, bus_dma_segment_t *, int, int); 108 STATIC int mvneta_dma_create(struct mvneta_softc *); 109 110 /* Rx/Tx Queue Control */ 111 STATIC int mvneta_rx_queue_init(struct ifnet *, int); 112 STATIC int mvneta_tx_queue_init(struct ifnet *, int); 113 STATIC int mvneta_rx_queue_enable(struct ifnet *, int); 114 STATIC int mvneta_tx_queue_enable(struct ifnet *, int); 115 STATIC void mvneta_rx_lockq(struct mvneta_softc *, int); 116 STATIC void mvneta_rx_unlockq(struct mvneta_softc *, int); 117 STATIC void mvneta_tx_lockq(struct mvneta_softc *, int); 118 STATIC void mvneta_tx_unlockq(struct mvneta_softc *, int); 119 120 /* Interrupt Handlers */ 121 STATIC void mvneta_disable_intr(struct mvneta_softc *); 122 STATIC void mvneta_enable_intr(struct mvneta_softc *); 123 STATIC void mvneta_rxtxth_intr(void *); 124 STATIC int mvneta_misc_intr(struct mvneta_softc *); 125 STATIC void mvneta_tick(void *); 126 /* struct ifnet and mii callbacks*/ 127 STATIC int mvneta_xmitfast_locked(struct mvneta_softc *, int, struct mbuf **); 128 STATIC int mvneta_xmit_locked(struct mvneta_softc *, int); 129 #ifdef MVNETA_MULTIQUEUE 130 STATIC int mvneta_transmit(struct ifnet *, struct mbuf *); 131 #else /* !MVNETA_MULTIQUEUE */ 132 STATIC void mvneta_start(struct ifnet *); 133 #endif 134 STATIC void mvneta_qflush(struct ifnet *); 135 STATIC void mvneta_tx_task(void *, int); 136 STATIC int mvneta_ioctl(struct ifnet *, u_long, caddr_t); 137 STATIC void mvneta_init(void *); 138 STATIC void mvneta_init_locked(void *); 139 STATIC void mvneta_stop(struct mvneta_softc *); 140 STATIC void mvneta_stop_locked(struct mvneta_softc *); 141 STATIC int mvneta_mediachange(struct ifnet *); 142 STATIC void mvneta_mediastatus(struct ifnet *, struct ifmediareq *); 143 STATIC void mvneta_portup(struct mvneta_softc *); 144 STATIC void mvneta_portdown(struct mvneta_softc *); 145 146 /* Link State Notify */ 147 STATIC void mvneta_update_autoneg(struct mvneta_softc *, int); 148 STATIC int mvneta_update_media(struct mvneta_softc *, int); 149 STATIC void mvneta_adjust_link(struct mvneta_softc *); 150 STATIC void mvneta_update_eee(struct mvneta_softc *); 151 STATIC void mvneta_update_fc(struct mvneta_softc *); 152 STATIC void mvneta_link_isr(struct mvneta_softc *); 153 STATIC void mvneta_linkupdate(struct mvneta_softc *, boolean_t); 154 STATIC void mvneta_linkup(struct mvneta_softc *); 155 STATIC void mvneta_linkdown(struct mvneta_softc *); 156 STATIC void mvneta_linkreset(struct mvneta_softc *); 157 158 /* Tx Subroutines */ 159 STATIC int mvneta_tx_queue(struct mvneta_softc *, struct mbuf **, int); 160 STATIC void mvneta_tx_set_csumflag(struct ifnet *, 161 struct mvneta_tx_desc *, struct mbuf *); 162 STATIC void mvneta_tx_queue_complete(struct mvneta_softc *, int); 163 STATIC void mvneta_tx_drain(struct mvneta_softc *); 164 165 /* Rx Subroutines */ 166 STATIC int mvneta_rx(struct mvneta_softc *, int, int); 167 STATIC void mvneta_rx_queue(struct mvneta_softc *, int, int); 168 STATIC void mvneta_rx_queue_refill(struct mvneta_softc *, int); 169 STATIC void mvneta_rx_set_csumflag(struct ifnet *, 170 struct mvneta_rx_desc *, struct mbuf *); 171 STATIC void mvneta_rx_buf_free(struct mvneta_softc *, struct mvneta_buf *); 172 173 /* MAC address filter */ 174 STATIC void mvneta_filter_setup(struct mvneta_softc *); 175 176 /* sysctl(9) */ 177 STATIC int sysctl_read_mib(SYSCTL_HANDLER_ARGS); 178 STATIC int sysctl_clear_mib(SYSCTL_HANDLER_ARGS); 179 STATIC int sysctl_set_queue_rxthtime(SYSCTL_HANDLER_ARGS); 180 STATIC void sysctl_mvneta_init(struct mvneta_softc *); 181 182 /* MIB */ 183 STATIC void mvneta_clear_mib(struct mvneta_softc *); 184 STATIC void mvneta_update_mib(struct mvneta_softc *); 185 186 /* Switch */ 187 STATIC boolean_t mvneta_has_switch(device_t); 188 189 #define mvneta_sc_lock(sc) mtx_lock(&sc->mtx) 190 #define mvneta_sc_unlock(sc) mtx_unlock(&sc->mtx) 191 192 STATIC struct mtx mii_mutex; 193 STATIC int mii_init = 0; 194 195 /* Device */ 196 STATIC int mvneta_detach(device_t); 197 /* MII */ 198 STATIC int mvneta_miibus_readreg(device_t, int, int); 199 STATIC int mvneta_miibus_writereg(device_t, int, int, int); 200 201 static device_method_t mvneta_methods[] = { 202 /* Device interface */ 203 DEVMETHOD(device_detach, mvneta_detach), 204 /* MII interface */ 205 DEVMETHOD(miibus_readreg, mvneta_miibus_readreg), 206 DEVMETHOD(miibus_writereg, mvneta_miibus_writereg), 207 /* MDIO interface */ 208 DEVMETHOD(mdio_readreg, mvneta_miibus_readreg), 209 DEVMETHOD(mdio_writereg, mvneta_miibus_writereg), 210 211 /* End */ 212 DEVMETHOD_END 213 }; 214 215 DEFINE_CLASS_0(mvneta, mvneta_driver, mvneta_methods, sizeof(struct mvneta_softc)); 216 217 DRIVER_MODULE(miibus, mvneta, miibus_driver, miibus_devclass, 0, 0); 218 DRIVER_MODULE(mdio, mvneta, mdio_driver, mdio_devclass, 0, 0); 219 MODULE_DEPEND(mvneta, mdio, 1, 1, 1); 220 MODULE_DEPEND(mvneta, ether, 1, 1, 1); 221 MODULE_DEPEND(mvneta, miibus, 1, 1, 1); 222 MODULE_DEPEND(mvneta, mvxpbm, 1, 1, 1); 223 224 /* 225 * List of MIB register and names 226 */ 227 enum mvneta_mib_idx 228 { 229 MVNETA_MIB_RX_GOOD_OCT_IDX, 230 MVNETA_MIB_RX_BAD_OCT_IDX, 231 MVNETA_MIB_TX_MAC_TRNS_ERR_IDX, 232 MVNETA_MIB_RX_GOOD_FRAME_IDX, 233 MVNETA_MIB_RX_BAD_FRAME_IDX, 234 MVNETA_MIB_RX_BCAST_FRAME_IDX, 235 MVNETA_MIB_RX_MCAST_FRAME_IDX, 236 MVNETA_MIB_RX_FRAME64_OCT_IDX, 237 MVNETA_MIB_RX_FRAME127_OCT_IDX, 238 MVNETA_MIB_RX_FRAME255_OCT_IDX, 239 MVNETA_MIB_RX_FRAME511_OCT_IDX, 240 MVNETA_MIB_RX_FRAME1023_OCT_IDX, 241 MVNETA_MIB_RX_FRAMEMAX_OCT_IDX, 242 MVNETA_MIB_TX_GOOD_OCT_IDX, 243 MVNETA_MIB_TX_GOOD_FRAME_IDX, 244 MVNETA_MIB_TX_EXCES_COL_IDX, 245 MVNETA_MIB_TX_MCAST_FRAME_IDX, 246 MVNETA_MIB_TX_BCAST_FRAME_IDX, 247 MVNETA_MIB_TX_MAC_CTL_ERR_IDX, 248 MVNETA_MIB_FC_SENT_IDX, 249 MVNETA_MIB_FC_GOOD_IDX, 250 MVNETA_MIB_FC_BAD_IDX, 251 MVNETA_MIB_PKT_UNDERSIZE_IDX, 252 MVNETA_MIB_PKT_FRAGMENT_IDX, 253 MVNETA_MIB_PKT_OVERSIZE_IDX, 254 MVNETA_MIB_PKT_JABBER_IDX, 255 MVNETA_MIB_MAC_RX_ERR_IDX, 256 MVNETA_MIB_MAC_CRC_ERR_IDX, 257 MVNETA_MIB_MAC_COL_IDX, 258 MVNETA_MIB_MAC_LATE_COL_IDX, 259 }; 260 261 STATIC struct mvneta_mib_def { 262 uint32_t regnum; 263 int reg64; 264 const char *sysctl_name; 265 const char *desc; 266 } mvneta_mib_list[] = { 267 [MVNETA_MIB_RX_GOOD_OCT_IDX] = {MVNETA_MIB_RX_GOOD_OCT, 1, 268 "rx_good_oct", "Good Octets Rx"}, 269 [MVNETA_MIB_RX_BAD_OCT_IDX] = {MVNETA_MIB_RX_BAD_OCT, 0, 270 "rx_bad_oct", "Bad Octets Rx"}, 271 [MVNETA_MIB_TX_MAC_TRNS_ERR_IDX] = {MVNETA_MIB_TX_MAC_TRNS_ERR, 0, 272 "tx_mac_err", "MAC Transmit Error"}, 273 [MVNETA_MIB_RX_GOOD_FRAME_IDX] = {MVNETA_MIB_RX_GOOD_FRAME, 0, 274 "rx_good_frame", "Good Frames Rx"}, 275 [MVNETA_MIB_RX_BAD_FRAME_IDX] = {MVNETA_MIB_RX_BAD_FRAME, 0, 276 "rx_bad_frame", "Bad Frames Rx"}, 277 [MVNETA_MIB_RX_BCAST_FRAME_IDX] = {MVNETA_MIB_RX_BCAST_FRAME, 0, 278 "rx_bcast_frame", "Broadcast Frames Rx"}, 279 [MVNETA_MIB_RX_MCAST_FRAME_IDX] = {MVNETA_MIB_RX_MCAST_FRAME, 0, 280 "rx_mcast_frame", "Multicast Frames Rx"}, 281 [MVNETA_MIB_RX_FRAME64_OCT_IDX] = {MVNETA_MIB_RX_FRAME64_OCT, 0, 282 "rx_frame_1_64", "Frame Size 1 - 64"}, 283 [MVNETA_MIB_RX_FRAME127_OCT_IDX] = {MVNETA_MIB_RX_FRAME127_OCT, 0, 284 "rx_frame_65_127", "Frame Size 65 - 127"}, 285 [MVNETA_MIB_RX_FRAME255_OCT_IDX] = {MVNETA_MIB_RX_FRAME255_OCT, 0, 286 "rx_frame_128_255", "Frame Size 128 - 255"}, 287 [MVNETA_MIB_RX_FRAME511_OCT_IDX] = {MVNETA_MIB_RX_FRAME511_OCT, 0, 288 "rx_frame_256_511", "Frame Size 256 - 511"}, 289 [MVNETA_MIB_RX_FRAME1023_OCT_IDX] = {MVNETA_MIB_RX_FRAME1023_OCT, 0, 290 "rx_frame_512_1023", "Frame Size 512 - 1023"}, 291 [MVNETA_MIB_RX_FRAMEMAX_OCT_IDX] = {MVNETA_MIB_RX_FRAMEMAX_OCT, 0, 292 "rx_fame_1024_max", "Frame Size 1024 - Max"}, 293 [MVNETA_MIB_TX_GOOD_OCT_IDX] = {MVNETA_MIB_TX_GOOD_OCT, 1, 294 "tx_good_oct", "Good Octets Tx"}, 295 [MVNETA_MIB_TX_GOOD_FRAME_IDX] = {MVNETA_MIB_TX_GOOD_FRAME, 0, 296 "tx_good_frame", "Good Frames Tx"}, 297 [MVNETA_MIB_TX_EXCES_COL_IDX] = {MVNETA_MIB_TX_EXCES_COL, 0, 298 "tx_exces_collision", "Excessive Collision"}, 299 [MVNETA_MIB_TX_MCAST_FRAME_IDX] = {MVNETA_MIB_TX_MCAST_FRAME, 0, 300 "tx_mcast_frame", "Multicast Frames Tx"}, 301 [MVNETA_MIB_TX_BCAST_FRAME_IDX] = {MVNETA_MIB_TX_BCAST_FRAME, 0, 302 "tx_bcast_frame", "Broadcast Frames Tx"}, 303 [MVNETA_MIB_TX_MAC_CTL_ERR_IDX] = {MVNETA_MIB_TX_MAC_CTL_ERR, 0, 304 "tx_mac_ctl_err", "Unknown MAC Control"}, 305 [MVNETA_MIB_FC_SENT_IDX] = {MVNETA_MIB_FC_SENT, 0, 306 "fc_tx", "Flow Control Tx"}, 307 [MVNETA_MIB_FC_GOOD_IDX] = {MVNETA_MIB_FC_GOOD, 0, 308 "fc_rx_good", "Good Flow Control Rx"}, 309 [MVNETA_MIB_FC_BAD_IDX] = {MVNETA_MIB_FC_BAD, 0, 310 "fc_rx_bad", "Bad Flow Control Rx"}, 311 [MVNETA_MIB_PKT_UNDERSIZE_IDX] = {MVNETA_MIB_PKT_UNDERSIZE, 0, 312 "pkt_undersize", "Undersized Packets Rx"}, 313 [MVNETA_MIB_PKT_FRAGMENT_IDX] = {MVNETA_MIB_PKT_FRAGMENT, 0, 314 "pkt_fragment", "Fragmented Packets Rx"}, 315 [MVNETA_MIB_PKT_OVERSIZE_IDX] = {MVNETA_MIB_PKT_OVERSIZE, 0, 316 "pkt_oversize", "Oversized Packets Rx"}, 317 [MVNETA_MIB_PKT_JABBER_IDX] = {MVNETA_MIB_PKT_JABBER, 0, 318 "pkt_jabber", "Jabber Packets Rx"}, 319 [MVNETA_MIB_MAC_RX_ERR_IDX] = {MVNETA_MIB_MAC_RX_ERR, 0, 320 "mac_rx_err", "MAC Rx Errors"}, 321 [MVNETA_MIB_MAC_CRC_ERR_IDX] = {MVNETA_MIB_MAC_CRC_ERR, 0, 322 "mac_crc_err", "MAC CRC Errors"}, 323 [MVNETA_MIB_MAC_COL_IDX] = {MVNETA_MIB_MAC_COL, 0, 324 "mac_collision", "MAC Collision"}, 325 [MVNETA_MIB_MAC_LATE_COL_IDX] = {MVNETA_MIB_MAC_LATE_COL, 0, 326 "mac_late_collision", "MAC Late Collision"}, 327 }; 328 329 static struct resource_spec res_spec[] = { 330 { SYS_RES_MEMORY, 0, RF_ACTIVE }, 331 { SYS_RES_IRQ, 0, RF_ACTIVE }, 332 { -1, 0} 333 }; 334 335 static struct { 336 driver_intr_t *handler; 337 char * description; 338 } mvneta_intrs[] = { 339 { mvneta_rxtxth_intr, "MVNETA aggregated interrupt" }, 340 }; 341 342 static int 343 mvneta_set_mac_address(struct mvneta_softc *sc, uint8_t *addr) 344 { 345 unsigned int mac_h; 346 unsigned int mac_l; 347 348 mac_l = (addr[4] << 8) | (addr[5]); 349 mac_h = (addr[0] << 24) | (addr[1] << 16) | 350 (addr[2] << 8) | (addr[3] << 0); 351 352 MVNETA_WRITE(sc, MVNETA_MACAL, mac_l); 353 MVNETA_WRITE(sc, MVNETA_MACAH, mac_h); 354 return (0); 355 } 356 357 static int 358 mvneta_get_mac_address(struct mvneta_softc *sc, uint8_t *addr) 359 { 360 uint32_t mac_l, mac_h; 361 362 #ifdef FDT 363 if (mvneta_fdt_mac_address(sc, addr) == 0) 364 return (0); 365 #endif 366 /* 367 * Fall back -- use the currently programmed address. 368 */ 369 mac_l = MVNETA_READ(sc, MVNETA_MACAL); 370 mac_h = MVNETA_READ(sc, MVNETA_MACAH); 371 if (mac_l == 0 && mac_h == 0) { 372 /* 373 * Generate pseudo-random MAC. 374 * Set lower part to random number | unit number. 375 */ 376 mac_l = arc4random() & ~0xff; 377 mac_l |= device_get_unit(sc->dev) & 0xff; 378 mac_h = arc4random(); 379 mac_h &= ~(3 << 24); /* Clear multicast and LAA bits */ 380 if (bootverbose) { 381 device_printf(sc->dev, 382 "Could not acquire MAC address. " 383 "Using randomized one.\n"); 384 } 385 } 386 387 addr[0] = (mac_h & 0xff000000) >> 24; 388 addr[1] = (mac_h & 0x00ff0000) >> 16; 389 addr[2] = (mac_h & 0x0000ff00) >> 8; 390 addr[3] = (mac_h & 0x000000ff); 391 addr[4] = (mac_l & 0x0000ff00) >> 8; 392 addr[5] = (mac_l & 0x000000ff); 393 return (0); 394 } 395 396 STATIC boolean_t 397 mvneta_has_switch(device_t self) 398 { 399 phandle_t node, switch_node, switch_eth, switch_eth_handle; 400 401 node = ofw_bus_get_node(self); 402 switch_node = 403 ofw_bus_find_compatible(OF_finddevice("/"), "marvell,dsa"); 404 switch_eth = 0; 405 406 OF_getencprop(switch_node, "dsa,ethernet", 407 (void*)&switch_eth_handle, sizeof(switch_eth_handle)); 408 409 if (switch_eth_handle > 0) 410 switch_eth = OF_node_from_xref(switch_eth_handle); 411 412 /* Return true if dsa,ethernet cell points to us */ 413 return (node == switch_eth); 414 } 415 416 STATIC int 417 mvneta_dma_create(struct mvneta_softc *sc) 418 { 419 size_t maxsize, maxsegsz; 420 size_t q; 421 int error; 422 423 /* 424 * Create Tx DMA 425 */ 426 maxsize = maxsegsz = sizeof(struct mvneta_tx_desc) * MVNETA_TX_RING_CNT; 427 428 error = bus_dma_tag_create( 429 bus_get_dma_tag(sc->dev), /* parent */ 430 16, 0, /* alignment, boundary */ 431 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */ 432 BUS_SPACE_MAXADDR, /* highaddr */ 433 NULL, NULL, /* filtfunc, filtfuncarg */ 434 maxsize, /* maxsize */ 435 1, /* nsegments */ 436 maxsegsz, /* maxsegsz */ 437 0, /* flags */ 438 NULL, NULL, /* lockfunc, lockfuncarg */ 439 &sc->tx_dtag); /* dmat */ 440 if (error != 0) { 441 device_printf(sc->dev, 442 "Failed to create DMA tag for Tx descriptors.\n"); 443 goto fail; 444 } 445 error = bus_dma_tag_create( 446 bus_get_dma_tag(sc->dev), /* parent */ 447 1, 0, /* alignment, boundary */ 448 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */ 449 BUS_SPACE_MAXADDR, /* highaddr */ 450 NULL, NULL, /* filtfunc, filtfuncarg */ 451 MVNETA_PACKET_SIZE, /* maxsize */ 452 MVNETA_TX_SEGLIMIT, /* nsegments */ 453 MVNETA_PACKET_SIZE, /* maxsegsz */ 454 BUS_DMA_ALLOCNOW, /* flags */ 455 NULL, NULL, /* lockfunc, lockfuncarg */ 456 &sc->txmbuf_dtag); 457 if (error != 0) { 458 device_printf(sc->dev, 459 "Failed to create DMA tag for Tx mbufs.\n"); 460 goto fail; 461 } 462 463 for (q = 0; q < MVNETA_TX_QNUM_MAX; q++) { 464 error = mvneta_ring_alloc_tx_queue(sc, q); 465 if (error != 0) { 466 device_printf(sc->dev, 467 "Failed to allocate DMA safe memory for TxQ: %d\n", q); 468 goto fail; 469 } 470 } 471 472 /* 473 * Create Rx DMA. 474 */ 475 /* Create tag for Rx descripors */ 476 error = bus_dma_tag_create( 477 bus_get_dma_tag(sc->dev), /* parent */ 478 32, 0, /* alignment, boundary */ 479 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */ 480 BUS_SPACE_MAXADDR, /* highaddr */ 481 NULL, NULL, /* filtfunc, filtfuncarg */ 482 sizeof(struct mvneta_rx_desc) * MVNETA_RX_RING_CNT, /* maxsize */ 483 1, /* nsegments */ 484 sizeof(struct mvneta_rx_desc) * MVNETA_RX_RING_CNT, /* maxsegsz */ 485 0, /* flags */ 486 NULL, NULL, /* lockfunc, lockfuncarg */ 487 &sc->rx_dtag); /* dmat */ 488 if (error != 0) { 489 device_printf(sc->dev, 490 "Failed to create DMA tag for Rx descriptors.\n"); 491 goto fail; 492 } 493 494 /* Create tag for Rx buffers */ 495 error = bus_dma_tag_create( 496 bus_get_dma_tag(sc->dev), /* parent */ 497 32, 0, /* alignment, boundary */ 498 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */ 499 BUS_SPACE_MAXADDR, /* highaddr */ 500 NULL, NULL, /* filtfunc, filtfuncarg */ 501 MVNETA_PACKET_SIZE, 1, /* maxsize, nsegments */ 502 MVNETA_PACKET_SIZE, /* maxsegsz */ 503 0, /* flags */ 504 NULL, NULL, /* lockfunc, lockfuncarg */ 505 &sc->rxbuf_dtag); /* dmat */ 506 if (error != 0) { 507 device_printf(sc->dev, 508 "Failed to create DMA tag for Rx buffers.\n"); 509 goto fail; 510 } 511 512 for (q = 0; q < MVNETA_RX_QNUM_MAX; q++) { 513 if (mvneta_ring_alloc_rx_queue(sc, q) != 0) { 514 device_printf(sc->dev, 515 "Failed to allocate DMA safe memory for RxQ: %d\n", q); 516 goto fail; 517 } 518 } 519 520 return (0); 521 fail: 522 mvneta_detach(sc->dev); 523 524 return (error); 525 } 526 527 /* ARGSUSED */ 528 int 529 mvneta_attach(device_t self) 530 { 531 struct mvneta_softc *sc; 532 struct ifnet *ifp; 533 device_t child; 534 int ifm_target; 535 int q, error; 536 uint32_t reg; 537 538 sc = device_get_softc(self); 539 sc->dev = self; 540 541 mtx_init(&sc->mtx, "mvneta_sc", NULL, MTX_DEF); 542 543 error = bus_alloc_resources(self, res_spec, sc->res); 544 if (error) { 545 device_printf(self, "could not allocate resources\n"); 546 return (ENXIO); 547 } 548 549 sc->version = MVNETA_READ(sc, MVNETA_PV); 550 device_printf(self, "version is %x\n", sc->version); 551 callout_init(&sc->tick_ch, 0); 552 553 /* 554 * make sure DMA engines are in reset state 555 */ 556 MVNETA_WRITE(sc, MVNETA_PRXINIT, 0x00000001); 557 MVNETA_WRITE(sc, MVNETA_PTXINIT, 0x00000001); 558 559 /* 560 * Disable port snoop for buffers and descriptors 561 * to avoid L2 caching of both without DRAM copy. 562 * Obtain coherency settings from the first MBUS 563 * window attribute. 564 */ 565 if ((MVNETA_READ(sc, MV_WIN_NETA_BASE(0)) & IO_WIN_COH_ATTR_MASK) == 0) { 566 reg = MVNETA_READ(sc, MVNETA_PSNPCFG); 567 reg &= ~MVNETA_PSNPCFG_DESCSNP_MASK; 568 reg &= ~MVNETA_PSNPCFG_BUFSNP_MASK; 569 MVNETA_WRITE(sc, MVNETA_PSNPCFG, reg); 570 } 571 572 /* 573 * MAC address 574 */ 575 if (mvneta_get_mac_address(sc, sc->enaddr)) { 576 device_printf(self, "no mac address.\n"); 577 return (ENXIO); 578 } 579 mvneta_set_mac_address(sc, sc->enaddr); 580 581 mvneta_disable_intr(sc); 582 583 /* Allocate network interface */ 584 ifp = sc->ifp = if_alloc(IFT_ETHER); 585 if (ifp == NULL) { 586 device_printf(self, "if_alloc() failed\n"); 587 mvneta_detach(self); 588 return (ENOMEM); 589 } 590 if_initname(ifp, device_get_name(self), device_get_unit(self)); 591 592 /* 593 * We can support 802.1Q VLAN-sized frames and jumbo 594 * Ethernet frames. 595 */ 596 ifp->if_capabilities |= IFCAP_VLAN_MTU | IFCAP_JUMBO_MTU; 597 598 ifp->if_softc = sc; 599 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 600 #ifdef MVNETA_MULTIQUEUE 601 ifp->if_transmit = mvneta_transmit; 602 ifp->if_qflush = mvneta_qflush; 603 #else /* !MVNETA_MULTIQUEUE */ 604 ifp->if_start = mvneta_start; 605 ifp->if_snd.ifq_drv_maxlen = MVNETA_TX_RING_CNT - 1; 606 IFQ_SET_MAXLEN(&ifp->if_snd, ifp->if_snd.ifq_drv_maxlen); 607 IFQ_SET_READY(&ifp->if_snd); 608 #endif 609 ifp->if_init = mvneta_init; 610 ifp->if_ioctl = mvneta_ioctl; 611 612 /* 613 * We can do IPv4/TCPv4/UDPv4/TCPv6/UDPv6 checksums in hardware. 614 */ 615 ifp->if_capabilities |= IFCAP_HWCSUM; 616 617 /* 618 * As VLAN hardware tagging is not supported 619 * but is necessary to perform VLAN hardware checksums, 620 * it is done in the driver 621 */ 622 ifp->if_capabilities |= IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_HWCSUM; 623 624 /* 625 * Currently IPv6 HW checksum is broken, so make sure it is disabled. 626 */ 627 ifp->if_capabilities &= ~IFCAP_HWCSUM_IPV6; 628 ifp->if_capenable = ifp->if_capabilities; 629 630 /* 631 * Disabled option(s): 632 * - Support for Large Receive Offload 633 */ 634 ifp->if_capabilities |= IFCAP_LRO; 635 636 ifp->if_hwassist = CSUM_IP | CSUM_TCP | CSUM_UDP; 637 638 /* 639 * Device DMA Buffer allocation. 640 * Handles resource deallocation in case of failure. 641 */ 642 error = mvneta_dma_create(sc); 643 if (error != 0) { 644 mvneta_detach(self); 645 return (error); 646 } 647 648 /* Initialize queues */ 649 for (q = 0; q < MVNETA_TX_QNUM_MAX; q++) { 650 error = mvneta_ring_init_tx_queue(sc, q); 651 if (error != 0) { 652 mvneta_detach(self); 653 return (error); 654 } 655 } 656 657 for (q = 0; q < MVNETA_RX_QNUM_MAX; q++) { 658 error = mvneta_ring_init_rx_queue(sc, q); 659 if (error != 0) { 660 mvneta_detach(self); 661 return (error); 662 } 663 } 664 665 ether_ifattach(ifp, sc->enaddr); 666 667 /* 668 * Enable DMA engines and Initialize Device Registers. 669 */ 670 MVNETA_WRITE(sc, MVNETA_PRXINIT, 0x00000000); 671 MVNETA_WRITE(sc, MVNETA_PTXINIT, 0x00000000); 672 MVNETA_WRITE(sc, MVNETA_PACC, MVNETA_PACC_ACCELERATIONMODE_EDM); 673 mvneta_sc_lock(sc); 674 mvneta_filter_setup(sc); 675 mvneta_sc_unlock(sc); 676 mvneta_initreg(ifp); 677 678 /* 679 * Now MAC is working, setup MII. 680 */ 681 if (mii_init == 0) { 682 /* 683 * MII bus is shared by all MACs and all PHYs in SoC. 684 * serializing the bus access should be safe. 685 */ 686 mtx_init(&mii_mutex, "mvneta_mii", NULL, MTX_DEF); 687 mii_init = 1; 688 } 689 690 /* Attach PHY(s) */ 691 if ((sc->phy_addr != MII_PHY_ANY) && (!sc->use_inband_status)) { 692 error = mii_attach(self, &sc->miibus, ifp, mvneta_mediachange, 693 mvneta_mediastatus, BMSR_DEFCAPMASK, sc->phy_addr, 694 MII_OFFSET_ANY, 0); 695 if (error != 0) { 696 if (bootverbose) { 697 device_printf(self, 698 "MII attach failed, error: %d\n", error); 699 } 700 ether_ifdetach(sc->ifp); 701 mvneta_detach(self); 702 return (error); 703 } 704 sc->mii = device_get_softc(sc->miibus); 705 sc->phy_attached = 1; 706 707 /* Disable auto-negotiation in MAC - rely on PHY layer */ 708 mvneta_update_autoneg(sc, FALSE); 709 } else if (sc->use_inband_status == TRUE) { 710 /* In-band link status */ 711 ifmedia_init(&sc->mvneta_ifmedia, 0, mvneta_mediachange, 712 mvneta_mediastatus); 713 714 /* Configure media */ 715 ifmedia_add(&sc->mvneta_ifmedia, IFM_ETHER | IFM_1000_T | IFM_FDX, 716 0, NULL); 717 ifmedia_add(&sc->mvneta_ifmedia, IFM_ETHER | IFM_100_TX, 0, NULL); 718 ifmedia_add(&sc->mvneta_ifmedia, IFM_ETHER | IFM_100_TX | IFM_FDX, 719 0, NULL); 720 ifmedia_add(&sc->mvneta_ifmedia, IFM_ETHER | IFM_10_T, 0, NULL); 721 ifmedia_add(&sc->mvneta_ifmedia, IFM_ETHER | IFM_10_T | IFM_FDX, 722 0, NULL); 723 ifmedia_add(&sc->mvneta_ifmedia, IFM_ETHER | IFM_AUTO, 0, NULL); 724 ifmedia_set(&sc->mvneta_ifmedia, IFM_ETHER | IFM_AUTO); 725 726 /* Enable auto-negotiation */ 727 mvneta_update_autoneg(sc, TRUE); 728 729 mvneta_sc_lock(sc); 730 if (MVNETA_IS_LINKUP(sc)) 731 mvneta_linkup(sc); 732 else 733 mvneta_linkdown(sc); 734 mvneta_sc_unlock(sc); 735 736 } else { 737 /* Fixed-link, use predefined values */ 738 ifmedia_init(&sc->mvneta_ifmedia, 0, mvneta_mediachange, 739 mvneta_mediastatus); 740 741 ifm_target = IFM_ETHER; 742 switch (sc->phy_speed) { 743 case 2500: 744 if (sc->phy_mode != MVNETA_PHY_SGMII && 745 sc->phy_mode != MVNETA_PHY_QSGMII) { 746 device_printf(self, 747 "2.5G speed can work only in (Q)SGMII mode\n"); 748 ether_ifdetach(sc->ifp); 749 mvneta_detach(self); 750 return (ENXIO); 751 } 752 ifm_target |= IFM_2500_T; 753 break; 754 case 1000: 755 ifm_target |= IFM_1000_T; 756 break; 757 case 100: 758 ifm_target |= IFM_100_TX; 759 break; 760 case 10: 761 ifm_target |= IFM_10_T; 762 break; 763 default: 764 ether_ifdetach(sc->ifp); 765 mvneta_detach(self); 766 return (ENXIO); 767 } 768 769 if (sc->phy_fdx) 770 ifm_target |= IFM_FDX; 771 else 772 ifm_target |= IFM_HDX; 773 774 ifmedia_add(&sc->mvneta_ifmedia, ifm_target, 0, NULL); 775 ifmedia_set(&sc->mvneta_ifmedia, ifm_target); 776 if_link_state_change(sc->ifp, LINK_STATE_UP); 777 778 if (mvneta_has_switch(self)) { 779 child = device_add_child(sc->dev, "mdio", -1); 780 if (child == NULL) { 781 ether_ifdetach(sc->ifp); 782 mvneta_detach(self); 783 return (ENXIO); 784 } 785 bus_generic_attach(sc->dev); 786 bus_generic_attach(child); 787 } 788 789 /* Configure MAC media */ 790 mvneta_update_media(sc, ifm_target); 791 } 792 793 sysctl_mvneta_init(sc); 794 795 callout_reset(&sc->tick_ch, 0, mvneta_tick, sc); 796 797 error = bus_setup_intr(self, sc->res[1], 798 INTR_TYPE_NET | INTR_MPSAFE, NULL, mvneta_intrs[0].handler, sc, 799 &sc->ih_cookie[0]); 800 if (error) { 801 device_printf(self, "could not setup %s\n", 802 mvneta_intrs[0].description); 803 ether_ifdetach(sc->ifp); 804 mvneta_detach(self); 805 return (error); 806 } 807 808 return (0); 809 } 810 811 STATIC int 812 mvneta_detach(device_t dev) 813 { 814 struct mvneta_softc *sc; 815 struct ifnet *ifp; 816 int q; 817 818 sc = device_get_softc(dev); 819 ifp = sc->ifp; 820 821 mvneta_stop(sc); 822 /* Detach network interface */ 823 if (sc->ifp) 824 if_free(sc->ifp); 825 826 for (q = 0; q < MVNETA_RX_QNUM_MAX; q++) 827 mvneta_ring_dealloc_rx_queue(sc, q); 828 for (q = 0; q < MVNETA_TX_QNUM_MAX; q++) 829 mvneta_ring_dealloc_tx_queue(sc, q); 830 831 if (sc->tx_dtag != NULL) 832 bus_dma_tag_destroy(sc->tx_dtag); 833 if (sc->rx_dtag != NULL) 834 bus_dma_tag_destroy(sc->rx_dtag); 835 if (sc->txmbuf_dtag != NULL) 836 bus_dma_tag_destroy(sc->txmbuf_dtag); 837 838 bus_release_resources(dev, res_spec, sc->res); 839 return (0); 840 } 841 842 /* 843 * MII 844 */ 845 STATIC int 846 mvneta_miibus_readreg(device_t dev, int phy, int reg) 847 { 848 struct mvneta_softc *sc; 849 struct ifnet *ifp; 850 uint32_t smi, val; 851 int i; 852 853 sc = device_get_softc(dev); 854 ifp = sc->ifp; 855 856 mtx_lock(&mii_mutex); 857 858 for (i = 0; i < MVNETA_PHY_TIMEOUT; i++) { 859 if ((MVNETA_READ(sc, MVNETA_SMI) & MVNETA_SMI_BUSY) == 0) 860 break; 861 DELAY(1); 862 } 863 if (i == MVNETA_PHY_TIMEOUT) { 864 if_printf(ifp, "SMI busy timeout\n"); 865 mtx_unlock(&mii_mutex); 866 return (-1); 867 } 868 869 smi = MVNETA_SMI_PHYAD(phy) | 870 MVNETA_SMI_REGAD(reg) | MVNETA_SMI_OPCODE_READ; 871 MVNETA_WRITE(sc, MVNETA_SMI, smi); 872 873 for (i = 0; i < MVNETA_PHY_TIMEOUT; i++) { 874 if ((MVNETA_READ(sc, MVNETA_SMI) & MVNETA_SMI_BUSY) == 0) 875 break; 876 DELAY(1); 877 } 878 879 if (i == MVNETA_PHY_TIMEOUT) { 880 if_printf(ifp, "SMI busy timeout\n"); 881 mtx_unlock(&mii_mutex); 882 return (-1); 883 } 884 for (i = 0; i < MVNETA_PHY_TIMEOUT; i++) { 885 smi = MVNETA_READ(sc, MVNETA_SMI); 886 if (smi & MVNETA_SMI_READVALID) 887 break; 888 DELAY(1); 889 } 890 891 if (i == MVNETA_PHY_TIMEOUT) { 892 if_printf(ifp, "SMI busy timeout\n"); 893 mtx_unlock(&mii_mutex); 894 return (-1); 895 } 896 897 mtx_unlock(&mii_mutex); 898 899 #ifdef MVNETA_KTR 900 CTR3(KTR_SPARE2, "%s i=%d, timeout=%d\n", ifp->if_xname, i, 901 MVNETA_PHY_TIMEOUT); 902 #endif 903 904 val = smi & MVNETA_SMI_DATA_MASK; 905 906 #ifdef MVNETA_KTR 907 CTR4(KTR_SPARE2, "%s phy=%d, reg=%#x, val=%#x\n", ifp->if_xname, phy, 908 reg, val); 909 #endif 910 return (val); 911 } 912 913 STATIC int 914 mvneta_miibus_writereg(device_t dev, int phy, int reg, int val) 915 { 916 struct mvneta_softc *sc; 917 struct ifnet *ifp; 918 uint32_t smi; 919 int i; 920 921 sc = device_get_softc(dev); 922 ifp = sc->ifp; 923 #ifdef MVNETA_KTR 924 CTR4(KTR_SPARE2, "%s phy=%d, reg=%#x, val=%#x\n", ifp->if_xname, 925 phy, reg, val); 926 #endif 927 928 mtx_lock(&mii_mutex); 929 930 for (i = 0; i < MVNETA_PHY_TIMEOUT; i++) { 931 if ((MVNETA_READ(sc, MVNETA_SMI) & MVNETA_SMI_BUSY) == 0) 932 break; 933 DELAY(1); 934 } 935 if (i == MVNETA_PHY_TIMEOUT) { 936 if_printf(ifp, "SMI busy timeout\n"); 937 mtx_unlock(&mii_mutex); 938 return (0); 939 } 940 941 smi = MVNETA_SMI_PHYAD(phy) | MVNETA_SMI_REGAD(reg) | 942 MVNETA_SMI_OPCODE_WRITE | (val & MVNETA_SMI_DATA_MASK); 943 MVNETA_WRITE(sc, MVNETA_SMI, smi); 944 945 for (i = 0; i < MVNETA_PHY_TIMEOUT; i++) { 946 if ((MVNETA_READ(sc, MVNETA_SMI) & MVNETA_SMI_BUSY) == 0) 947 break; 948 DELAY(1); 949 } 950 951 mtx_unlock(&mii_mutex); 952 953 if (i == MVNETA_PHY_TIMEOUT) 954 if_printf(ifp, "phy write timed out\n"); 955 956 return (0); 957 } 958 959 STATIC void 960 mvneta_portup(struct mvneta_softc *sc) 961 { 962 int q; 963 964 for (q = 0; q < MVNETA_RX_QNUM_MAX; q++) { 965 mvneta_rx_lockq(sc, q); 966 mvneta_rx_queue_enable(sc->ifp, q); 967 mvneta_rx_unlockq(sc, q); 968 } 969 970 for (q = 0; q < MVNETA_TX_QNUM_MAX; q++) { 971 mvneta_tx_lockq(sc, q); 972 mvneta_tx_queue_enable(sc->ifp, q); 973 mvneta_tx_unlockq(sc, q); 974 } 975 976 } 977 978 STATIC void 979 mvneta_portdown(struct mvneta_softc *sc) 980 { 981 struct mvneta_rx_ring *rx; 982 struct mvneta_tx_ring *tx; 983 int q, cnt; 984 uint32_t reg; 985 986 for (q = 0; q < MVNETA_RX_QNUM_MAX; q++) { 987 rx = MVNETA_RX_RING(sc, q); 988 mvneta_rx_lockq(sc, q); 989 rx->queue_status = MVNETA_QUEUE_DISABLED; 990 mvneta_rx_unlockq(sc, q); 991 } 992 993 for (q = 0; q < MVNETA_TX_QNUM_MAX; q++) { 994 tx = MVNETA_TX_RING(sc, q); 995 mvneta_tx_lockq(sc, q); 996 tx->queue_status = MVNETA_QUEUE_DISABLED; 997 mvneta_tx_unlockq(sc, q); 998 } 999 1000 /* Wait for all Rx activity to terminate. */ 1001 reg = MVNETA_READ(sc, MVNETA_RQC) & MVNETA_RQC_EN_MASK; 1002 reg = MVNETA_RQC_DIS(reg); 1003 MVNETA_WRITE(sc, MVNETA_RQC, reg); 1004 cnt = 0; 1005 do { 1006 if (cnt >= RX_DISABLE_TIMEOUT) { 1007 if_printf(sc->ifp, 1008 "timeout for RX stopped. rqc 0x%x\n", reg); 1009 break; 1010 } 1011 cnt++; 1012 reg = MVNETA_READ(sc, MVNETA_RQC); 1013 } while ((reg & MVNETA_RQC_EN_MASK) != 0); 1014 1015 /* Wait for all Tx activity to terminate. */ 1016 reg = MVNETA_READ(sc, MVNETA_PIE); 1017 reg &= ~MVNETA_PIE_TXPKTINTRPTENB_MASK; 1018 MVNETA_WRITE(sc, MVNETA_PIE, reg); 1019 1020 reg = MVNETA_READ(sc, MVNETA_PRXTXTIM); 1021 reg &= ~MVNETA_PRXTXTI_TBTCQ_MASK; 1022 MVNETA_WRITE(sc, MVNETA_PRXTXTIM, reg); 1023 1024 reg = MVNETA_READ(sc, MVNETA_TQC) & MVNETA_TQC_EN_MASK; 1025 reg = MVNETA_TQC_DIS(reg); 1026 MVNETA_WRITE(sc, MVNETA_TQC, reg); 1027 cnt = 0; 1028 do { 1029 if (cnt >= TX_DISABLE_TIMEOUT) { 1030 if_printf(sc->ifp, 1031 "timeout for TX stopped. tqc 0x%x\n", reg); 1032 break; 1033 } 1034 cnt++; 1035 reg = MVNETA_READ(sc, MVNETA_TQC); 1036 } while ((reg & MVNETA_TQC_EN_MASK) != 0); 1037 1038 /* Wait for all Tx FIFO is empty */ 1039 cnt = 0; 1040 do { 1041 if (cnt >= TX_FIFO_EMPTY_TIMEOUT) { 1042 if_printf(sc->ifp, 1043 "timeout for TX FIFO drained. ps0 0x%x\n", reg); 1044 break; 1045 } 1046 cnt++; 1047 reg = MVNETA_READ(sc, MVNETA_PS0); 1048 } while (((reg & MVNETA_PS0_TXFIFOEMP) == 0) && 1049 ((reg & MVNETA_PS0_TXINPROG) != 0)); 1050 } 1051 1052 /* 1053 * Device Register Initialization 1054 * reset device registers to device driver default value. 1055 * the device is not enabled here. 1056 */ 1057 STATIC int 1058 mvneta_initreg(struct ifnet *ifp) 1059 { 1060 struct mvneta_softc *sc; 1061 int q, i; 1062 uint32_t reg; 1063 1064 sc = ifp->if_softc; 1065 #ifdef MVNETA_KTR 1066 CTR1(KTR_SPARE2, "%s initializing device register", ifp->if_xname); 1067 #endif 1068 1069 /* Disable Legacy WRR, Disable EJP, Release from reset. */ 1070 MVNETA_WRITE(sc, MVNETA_TQC_1, 0); 1071 /* Enable mbus retry. */ 1072 MVNETA_WRITE(sc, MVNETA_MBUS_CONF, MVNETA_MBUS_RETRY_EN); 1073 1074 /* Init TX/RX Queue Registers */ 1075 for (q = 0; q < MVNETA_RX_QNUM_MAX; q++) { 1076 mvneta_rx_lockq(sc, q); 1077 if (mvneta_rx_queue_init(ifp, q) != 0) { 1078 device_printf(sc->dev, 1079 "initialization failed: cannot initialize queue\n"); 1080 mvneta_rx_unlockq(sc, q); 1081 return (ENOBUFS); 1082 } 1083 mvneta_rx_unlockq(sc, q); 1084 } 1085 for (q = 0; q < MVNETA_TX_QNUM_MAX; q++) { 1086 mvneta_tx_lockq(sc, q); 1087 if (mvneta_tx_queue_init(ifp, q) != 0) { 1088 device_printf(sc->dev, 1089 "initialization failed: cannot initialize queue\n"); 1090 mvneta_tx_unlockq(sc, q); 1091 return (ENOBUFS); 1092 } 1093 mvneta_tx_unlockq(sc, q); 1094 } 1095 1096 /* 1097 * Ethernet Unit Control - disable automatic PHY management by HW. 1098 * In case the port uses SMI-controlled PHY, poll its status with 1099 * mii_tick() and update MAC settings accordingly. 1100 */ 1101 reg = MVNETA_READ(sc, MVNETA_EUC); 1102 reg &= ~MVNETA_EUC_POLLING; 1103 MVNETA_WRITE(sc, MVNETA_EUC, reg); 1104 1105 /* EEE: Low Power Idle */ 1106 reg = MVNETA_LPIC0_LILIMIT(MVNETA_LPI_LI); 1107 reg |= MVNETA_LPIC0_TSLIMIT(MVNETA_LPI_TS); 1108 MVNETA_WRITE(sc, MVNETA_LPIC0, reg); 1109 1110 reg = MVNETA_LPIC1_TWLIMIT(MVNETA_LPI_TW); 1111 MVNETA_WRITE(sc, MVNETA_LPIC1, reg); 1112 1113 reg = MVNETA_LPIC2_MUSTSET; 1114 MVNETA_WRITE(sc, MVNETA_LPIC2, reg); 1115 1116 /* Port MAC Control set 0 */ 1117 reg = MVNETA_PMACC0_MUSTSET; /* must write 0x1 */ 1118 reg &= ~MVNETA_PMACC0_PORTEN; /* port is still disabled */ 1119 reg |= MVNETA_PMACC0_FRAMESIZELIMIT(MVNETA_MAX_FRAME); 1120 MVNETA_WRITE(sc, MVNETA_PMACC0, reg); 1121 1122 /* Port MAC Control set 2 */ 1123 reg = MVNETA_READ(sc, MVNETA_PMACC2); 1124 switch (sc->phy_mode) { 1125 case MVNETA_PHY_QSGMII: 1126 reg |= (MVNETA_PMACC2_PCSEN | MVNETA_PMACC2_RGMIIEN); 1127 MVNETA_WRITE(sc, MVNETA_PSERDESCFG, MVNETA_PSERDESCFG_QSGMII); 1128 break; 1129 case MVNETA_PHY_SGMII: 1130 reg |= (MVNETA_PMACC2_PCSEN | MVNETA_PMACC2_RGMIIEN); 1131 MVNETA_WRITE(sc, MVNETA_PSERDESCFG, MVNETA_PSERDESCFG_SGMII); 1132 break; 1133 case MVNETA_PHY_RGMII: 1134 case MVNETA_PHY_RGMII_ID: 1135 reg |= MVNETA_PMACC2_RGMIIEN; 1136 break; 1137 } 1138 reg |= MVNETA_PMACC2_MUSTSET; 1139 reg &= ~MVNETA_PMACC2_PORTMACRESET; 1140 MVNETA_WRITE(sc, MVNETA_PMACC2, reg); 1141 1142 /* Port Configuration Extended: enable Tx CRC generation */ 1143 reg = MVNETA_READ(sc, MVNETA_PXCX); 1144 reg &= ~MVNETA_PXCX_TXCRCDIS; 1145 MVNETA_WRITE(sc, MVNETA_PXCX, reg); 1146 1147 /* clear MIB counter registers(clear by read) */ 1148 for (i = 0; i < nitems(mvneta_mib_list); i++) { 1149 if (mvneta_mib_list[i].reg64) 1150 MVNETA_READ_MIB_8(sc, mvneta_mib_list[i].regnum); 1151 else 1152 MVNETA_READ_MIB_4(sc, mvneta_mib_list[i].regnum); 1153 } 1154 MVNETA_READ(sc, MVNETA_PDFC); 1155 MVNETA_READ(sc, MVNETA_POFC); 1156 1157 /* Set SDC register except IPGINT bits */ 1158 reg = MVNETA_SDC_RXBSZ_16_64BITWORDS; 1159 reg |= MVNETA_SDC_TXBSZ_16_64BITWORDS; 1160 reg |= MVNETA_SDC_BLMR; 1161 reg |= MVNETA_SDC_BLMT; 1162 MVNETA_WRITE(sc, MVNETA_SDC, reg); 1163 1164 return (0); 1165 } 1166 1167 STATIC void 1168 mvneta_dmamap_cb(void *arg, bus_dma_segment_t * segs, int nseg, int error) 1169 { 1170 1171 if (error != 0) 1172 return; 1173 *(bus_addr_t *)arg = segs->ds_addr; 1174 } 1175 1176 STATIC int 1177 mvneta_ring_alloc_rx_queue(struct mvneta_softc *sc, int q) 1178 { 1179 struct mvneta_rx_ring *rx; 1180 struct mvneta_buf *rxbuf; 1181 bus_dmamap_t dmap; 1182 int i, error; 1183 1184 if (q >= MVNETA_RX_QNUM_MAX) 1185 return (EINVAL); 1186 1187 rx = MVNETA_RX_RING(sc, q); 1188 mtx_init(&rx->ring_mtx, "mvneta_rx", NULL, MTX_DEF); 1189 /* Allocate DMA memory for Rx descriptors */ 1190 error = bus_dmamem_alloc(sc->rx_dtag, 1191 (void**)&(rx->desc), 1192 BUS_DMA_NOWAIT | BUS_DMA_ZERO, 1193 &rx->desc_map); 1194 if (error != 0 || rx->desc == NULL) 1195 goto fail; 1196 error = bus_dmamap_load(sc->rx_dtag, rx->desc_map, 1197 rx->desc, 1198 sizeof(struct mvneta_rx_desc) * MVNETA_RX_RING_CNT, 1199 mvneta_dmamap_cb, &rx->desc_pa, BUS_DMA_NOWAIT); 1200 if (error != 0) 1201 goto fail; 1202 1203 for (i = 0; i < MVNETA_RX_RING_CNT; i++) { 1204 error = bus_dmamap_create(sc->rxbuf_dtag, 0, &dmap); 1205 if (error != 0) { 1206 device_printf(sc->dev, 1207 "Failed to create DMA map for Rx buffer num: %d\n", i); 1208 goto fail; 1209 } 1210 rxbuf = &rx->rxbuf[i]; 1211 rxbuf->dmap = dmap; 1212 rxbuf->m = NULL; 1213 } 1214 1215 return (0); 1216 fail: 1217 mvneta_ring_dealloc_rx_queue(sc, q); 1218 device_printf(sc->dev, "DMA Ring buffer allocation failure.\n"); 1219 return (error); 1220 } 1221 1222 STATIC int 1223 mvneta_ring_alloc_tx_queue(struct mvneta_softc *sc, int q) 1224 { 1225 struct mvneta_tx_ring *tx; 1226 int error; 1227 1228 if (q >= MVNETA_TX_QNUM_MAX) 1229 return (EINVAL); 1230 tx = MVNETA_TX_RING(sc, q); 1231 mtx_init(&tx->ring_mtx, "mvneta_tx", NULL, MTX_DEF); 1232 error = bus_dmamem_alloc(sc->tx_dtag, 1233 (void**)&(tx->desc), 1234 BUS_DMA_NOWAIT | BUS_DMA_ZERO, 1235 &tx->desc_map); 1236 if (error != 0 || tx->desc == NULL) 1237 goto fail; 1238 error = bus_dmamap_load(sc->tx_dtag, tx->desc_map, 1239 tx->desc, 1240 sizeof(struct mvneta_tx_desc) * MVNETA_TX_RING_CNT, 1241 mvneta_dmamap_cb, &tx->desc_pa, BUS_DMA_NOWAIT); 1242 if (error != 0) 1243 goto fail; 1244 1245 #ifdef MVNETA_MULTIQUEUE 1246 tx->br = buf_ring_alloc(MVNETA_BUFRING_SIZE, M_DEVBUF, M_NOWAIT, 1247 &tx->ring_mtx); 1248 if (tx->br == NULL) { 1249 device_printf(sc->dev, 1250 "Could not setup buffer ring for TxQ(%d)\n", q); 1251 error = ENOMEM; 1252 goto fail; 1253 } 1254 #endif 1255 1256 return (0); 1257 fail: 1258 mvneta_ring_dealloc_tx_queue(sc, q); 1259 device_printf(sc->dev, "DMA Ring buffer allocation failure.\n"); 1260 return (error); 1261 } 1262 1263 STATIC void 1264 mvneta_ring_dealloc_tx_queue(struct mvneta_softc *sc, int q) 1265 { 1266 struct mvneta_tx_ring *tx; 1267 struct mvneta_buf *txbuf; 1268 void *kva; 1269 int error; 1270 int i; 1271 1272 if (q >= MVNETA_TX_QNUM_MAX) 1273 return; 1274 tx = MVNETA_TX_RING(sc, q); 1275 1276 if (tx->taskq != NULL) { 1277 /* Remove task */ 1278 while (taskqueue_cancel(tx->taskq, &tx->task, NULL) != 0) 1279 taskqueue_drain(tx->taskq, &tx->task); 1280 } 1281 #ifdef MVNETA_MULTIQUEUE 1282 if (tx->br != NULL) 1283 drbr_free(tx->br, M_DEVBUF); 1284 #endif 1285 1286 if (sc->txmbuf_dtag != NULL) { 1287 if (mtx_name(&tx->ring_mtx) != NULL) { 1288 /* 1289 * It is assumed that maps are being loaded after mutex 1290 * is initialized. Therefore we can skip unloading maps 1291 * when mutex is empty. 1292 */ 1293 mvneta_tx_lockq(sc, q); 1294 mvneta_ring_flush_tx_queue(sc, q); 1295 mvneta_tx_unlockq(sc, q); 1296 } 1297 for (i = 0; i < MVNETA_TX_RING_CNT; i++) { 1298 txbuf = &tx->txbuf[i]; 1299 if (txbuf->dmap != NULL) { 1300 error = bus_dmamap_destroy(sc->txmbuf_dtag, 1301 txbuf->dmap); 1302 if (error != 0) { 1303 panic("%s: map busy for Tx descriptor (Q%d, %d)", 1304 __func__, q, i); 1305 } 1306 } 1307 } 1308 } 1309 1310 if (tx->desc_pa != 0) 1311 bus_dmamap_unload(sc->tx_dtag, tx->desc_map); 1312 1313 kva = (void *)tx->desc; 1314 if (kva != NULL) 1315 bus_dmamem_free(sc->tx_dtag, tx->desc, tx->desc_map); 1316 1317 if (mtx_name(&tx->ring_mtx) != NULL) 1318 mtx_destroy(&tx->ring_mtx); 1319 1320 memset(tx, 0, sizeof(*tx)); 1321 } 1322 1323 STATIC void 1324 mvneta_ring_dealloc_rx_queue(struct mvneta_softc *sc, int q) 1325 { 1326 struct mvneta_rx_ring *rx; 1327 struct lro_ctrl *lro; 1328 void *kva; 1329 1330 if (q >= MVNETA_RX_QNUM_MAX) 1331 return; 1332 1333 rx = MVNETA_RX_RING(sc, q); 1334 1335 mvneta_ring_flush_rx_queue(sc, q); 1336 1337 if (rx->desc_pa != 0) 1338 bus_dmamap_unload(sc->rx_dtag, rx->desc_map); 1339 1340 kva = (void *)rx->desc; 1341 if (kva != NULL) 1342 bus_dmamem_free(sc->rx_dtag, rx->desc, rx->desc_map); 1343 1344 lro = &rx->lro; 1345 tcp_lro_free(lro); 1346 1347 if (mtx_name(&rx->ring_mtx) != NULL) 1348 mtx_destroy(&rx->ring_mtx); 1349 1350 memset(rx, 0, sizeof(*rx)); 1351 } 1352 1353 STATIC int 1354 mvneta_ring_init_rx_queue(struct mvneta_softc *sc, int q) 1355 { 1356 struct mvneta_rx_ring *rx; 1357 struct lro_ctrl *lro; 1358 int error; 1359 1360 if (q >= MVNETA_RX_QNUM_MAX) 1361 return (0); 1362 1363 rx = MVNETA_RX_RING(sc, q); 1364 rx->dma = rx->cpu = 0; 1365 rx->queue_th_received = MVNETA_RXTH_COUNT; 1366 rx->queue_th_time = (get_tclk() / 1000) / 10; /* 0.1 [ms] */ 1367 1368 /* Initialize LRO */ 1369 rx->lro_enabled = FALSE; 1370 if ((sc->ifp->if_capenable & IFCAP_LRO) != 0) { 1371 lro = &rx->lro; 1372 error = tcp_lro_init(lro); 1373 if (error != 0) 1374 device_printf(sc->dev, "LRO Initialization failed!\n"); 1375 else { 1376 rx->lro_enabled = TRUE; 1377 lro->ifp = sc->ifp; 1378 } 1379 } 1380 1381 return (0); 1382 } 1383 1384 STATIC int 1385 mvneta_ring_init_tx_queue(struct mvneta_softc *sc, int q) 1386 { 1387 struct mvneta_tx_ring *tx; 1388 struct mvneta_buf *txbuf; 1389 int i, error; 1390 1391 if (q >= MVNETA_TX_QNUM_MAX) 1392 return (0); 1393 1394 tx = MVNETA_TX_RING(sc, q); 1395 1396 /* Tx handle */ 1397 for (i = 0; i < MVNETA_TX_RING_CNT; i++) { 1398 txbuf = &tx->txbuf[i]; 1399 txbuf->m = NULL; 1400 /* Tx handle needs DMA map for busdma_load_mbuf() */ 1401 error = bus_dmamap_create(sc->txmbuf_dtag, 0, 1402 &txbuf->dmap); 1403 if (error != 0) { 1404 device_printf(sc->dev, 1405 "can't create dma map (tx ring %d)\n", i); 1406 return (error); 1407 } 1408 } 1409 tx->dma = tx->cpu = 0; 1410 tx->used = 0; 1411 tx->drv_error = 0; 1412 tx->queue_status = MVNETA_QUEUE_DISABLED; 1413 tx->queue_hung = FALSE; 1414 1415 tx->ifp = sc->ifp; 1416 tx->qidx = q; 1417 TASK_INIT(&tx->task, 0, mvneta_tx_task, tx); 1418 tx->taskq = taskqueue_create_fast("mvneta_tx_taskq", M_WAITOK, 1419 taskqueue_thread_enqueue, &tx->taskq); 1420 taskqueue_start_threads(&tx->taskq, 1, PI_NET, "%s: tx_taskq(%d)", 1421 device_get_nameunit(sc->dev), q); 1422 1423 return (0); 1424 } 1425 1426 STATIC void 1427 mvneta_ring_flush_tx_queue(struct mvneta_softc *sc, int q) 1428 { 1429 struct mvneta_tx_ring *tx; 1430 struct mvneta_buf *txbuf; 1431 int i; 1432 1433 tx = MVNETA_TX_RING(sc, q); 1434 KASSERT_TX_MTX(sc, q); 1435 1436 /* Tx handle */ 1437 for (i = 0; i < MVNETA_TX_RING_CNT; i++) { 1438 txbuf = &tx->txbuf[i]; 1439 bus_dmamap_unload(sc->txmbuf_dtag, txbuf->dmap); 1440 if (txbuf->m != NULL) { 1441 m_freem(txbuf->m); 1442 txbuf->m = NULL; 1443 } 1444 } 1445 tx->dma = tx->cpu = 0; 1446 tx->used = 0; 1447 } 1448 1449 STATIC void 1450 mvneta_ring_flush_rx_queue(struct mvneta_softc *sc, int q) 1451 { 1452 struct mvneta_rx_ring *rx; 1453 struct mvneta_buf *rxbuf; 1454 int i; 1455 1456 rx = MVNETA_RX_RING(sc, q); 1457 KASSERT_RX_MTX(sc, q); 1458 1459 /* Rx handle */ 1460 for (i = 0; i < MVNETA_RX_RING_CNT; i++) { 1461 rxbuf = &rx->rxbuf[i]; 1462 mvneta_rx_buf_free(sc, rxbuf); 1463 } 1464 rx->dma = rx->cpu = 0; 1465 } 1466 1467 /* 1468 * Rx/Tx Queue Control 1469 */ 1470 STATIC int 1471 mvneta_rx_queue_init(struct ifnet *ifp, int q) 1472 { 1473 struct mvneta_softc *sc; 1474 struct mvneta_rx_ring *rx; 1475 uint32_t reg; 1476 1477 sc = ifp->if_softc; 1478 KASSERT_RX_MTX(sc, q); 1479 rx = MVNETA_RX_RING(sc, q); 1480 DASSERT(rx->desc_pa != 0); 1481 1482 /* descriptor address */ 1483 MVNETA_WRITE(sc, MVNETA_PRXDQA(q), rx->desc_pa); 1484 1485 /* Rx buffer size and descriptor ring size */ 1486 reg = MVNETA_PRXDQS_BUFFERSIZE(MVNETA_PACKET_SIZE >> 3); 1487 reg |= MVNETA_PRXDQS_DESCRIPTORSQUEUESIZE(MVNETA_RX_RING_CNT); 1488 MVNETA_WRITE(sc, MVNETA_PRXDQS(q), reg); 1489 #ifdef MVNETA_KTR 1490 CTR3(KTR_SPARE2, "%s PRXDQS(%d): %#x", ifp->if_xname, q, 1491 MVNETA_READ(sc, MVNETA_PRXDQS(q))); 1492 #endif 1493 /* Rx packet offset address */ 1494 reg = MVNETA_PRXC_PACKETOFFSET(MVNETA_PACKET_OFFSET >> 3); 1495 MVNETA_WRITE(sc, MVNETA_PRXC(q), reg); 1496 #ifdef MVNETA_KTR 1497 CTR3(KTR_SPARE2, "%s PRXC(%d): %#x", ifp->if_xname, q, 1498 MVNETA_READ(sc, MVNETA_PRXC(q))); 1499 #endif 1500 1501 /* if DMA is not working, register is not updated */ 1502 DASSERT(MVNETA_READ(sc, MVNETA_PRXDQA(q)) == rx->desc_pa); 1503 return (0); 1504 } 1505 1506 STATIC int 1507 mvneta_tx_queue_init(struct ifnet *ifp, int q) 1508 { 1509 struct mvneta_softc *sc; 1510 struct mvneta_tx_ring *tx; 1511 uint32_t reg; 1512 1513 sc = ifp->if_softc; 1514 KASSERT_TX_MTX(sc, q); 1515 tx = MVNETA_TX_RING(sc, q); 1516 DASSERT(tx->desc_pa != 0); 1517 1518 /* descriptor address */ 1519 MVNETA_WRITE(sc, MVNETA_PTXDQA(q), tx->desc_pa); 1520 1521 /* descriptor ring size */ 1522 reg = MVNETA_PTXDQS_DQS(MVNETA_TX_RING_CNT); 1523 MVNETA_WRITE(sc, MVNETA_PTXDQS(q), reg); 1524 1525 /* if DMA is not working, register is not updated */ 1526 DASSERT(MVNETA_READ(sc, MVNETA_PTXDQA(q)) == tx->desc_pa); 1527 return (0); 1528 } 1529 1530 STATIC int 1531 mvneta_rx_queue_enable(struct ifnet *ifp, int q) 1532 { 1533 struct mvneta_softc *sc; 1534 struct mvneta_rx_ring *rx; 1535 uint32_t reg; 1536 1537 sc = ifp->if_softc; 1538 rx = MVNETA_RX_RING(sc, q); 1539 KASSERT_RX_MTX(sc, q); 1540 1541 /* Set Rx interrupt threshold */ 1542 reg = MVNETA_PRXDQTH_ODT(rx->queue_th_received); 1543 MVNETA_WRITE(sc, MVNETA_PRXDQTH(q), reg); 1544 1545 reg = MVNETA_PRXITTH_RITT(rx->queue_th_time); 1546 MVNETA_WRITE(sc, MVNETA_PRXITTH(q), reg); 1547 1548 /* Unmask RXTX_TH Intr. */ 1549 reg = MVNETA_READ(sc, MVNETA_PRXTXTIM); 1550 reg |= MVNETA_PRXTXTI_RBICTAPQ(q); /* Rx Buffer Interrupt Coalese */ 1551 MVNETA_WRITE(sc, MVNETA_PRXTXTIM, reg); 1552 1553 /* Enable Rx queue */ 1554 reg = MVNETA_READ(sc, MVNETA_RQC) & MVNETA_RQC_EN_MASK; 1555 reg |= MVNETA_RQC_ENQ(q); 1556 MVNETA_WRITE(sc, MVNETA_RQC, reg); 1557 1558 rx->queue_status = MVNETA_QUEUE_WORKING; 1559 return (0); 1560 } 1561 1562 STATIC int 1563 mvneta_tx_queue_enable(struct ifnet *ifp, int q) 1564 { 1565 struct mvneta_softc *sc; 1566 struct mvneta_tx_ring *tx; 1567 1568 sc = ifp->if_softc; 1569 tx = MVNETA_TX_RING(sc, q); 1570 KASSERT_TX_MTX(sc, q); 1571 1572 /* Enable Tx queue */ 1573 MVNETA_WRITE(sc, MVNETA_TQC, MVNETA_TQC_ENQ(q)); 1574 1575 tx->queue_status = MVNETA_QUEUE_IDLE; 1576 tx->queue_hung = FALSE; 1577 return (0); 1578 } 1579 1580 STATIC __inline void 1581 mvneta_rx_lockq(struct mvneta_softc *sc, int q) 1582 { 1583 1584 DASSERT(q >= 0); 1585 DASSERT(q < MVNETA_RX_QNUM_MAX); 1586 mtx_lock(&sc->rx_ring[q].ring_mtx); 1587 } 1588 1589 STATIC __inline void 1590 mvneta_rx_unlockq(struct mvneta_softc *sc, int q) 1591 { 1592 1593 DASSERT(q >= 0); 1594 DASSERT(q < MVNETA_RX_QNUM_MAX); 1595 mtx_unlock(&sc->rx_ring[q].ring_mtx); 1596 } 1597 1598 STATIC __inline int __unused 1599 mvneta_tx_trylockq(struct mvneta_softc *sc, int q) 1600 { 1601 1602 DASSERT(q >= 0); 1603 DASSERT(q < MVNETA_TX_QNUM_MAX); 1604 return (mtx_trylock(&sc->tx_ring[q].ring_mtx)); 1605 } 1606 1607 STATIC __inline void 1608 mvneta_tx_lockq(struct mvneta_softc *sc, int q) 1609 { 1610 1611 DASSERT(q >= 0); 1612 DASSERT(q < MVNETA_TX_QNUM_MAX); 1613 mtx_lock(&sc->tx_ring[q].ring_mtx); 1614 } 1615 1616 STATIC __inline void 1617 mvneta_tx_unlockq(struct mvneta_softc *sc, int q) 1618 { 1619 1620 DASSERT(q >= 0); 1621 DASSERT(q < MVNETA_TX_QNUM_MAX); 1622 mtx_unlock(&sc->tx_ring[q].ring_mtx); 1623 } 1624 1625 /* 1626 * Interrupt Handlers 1627 */ 1628 STATIC void 1629 mvneta_disable_intr(struct mvneta_softc *sc) 1630 { 1631 1632 MVNETA_WRITE(sc, MVNETA_EUIM, 0); 1633 MVNETA_WRITE(sc, MVNETA_EUIC, 0); 1634 MVNETA_WRITE(sc, MVNETA_PRXTXTIM, 0); 1635 MVNETA_WRITE(sc, MVNETA_PRXTXTIC, 0); 1636 MVNETA_WRITE(sc, MVNETA_PRXTXIM, 0); 1637 MVNETA_WRITE(sc, MVNETA_PRXTXIC, 0); 1638 MVNETA_WRITE(sc, MVNETA_PMIM, 0); 1639 MVNETA_WRITE(sc, MVNETA_PMIC, 0); 1640 MVNETA_WRITE(sc, MVNETA_PIE, 0); 1641 } 1642 1643 STATIC void 1644 mvneta_enable_intr(struct mvneta_softc *sc) 1645 { 1646 uint32_t reg; 1647 1648 /* Enable Summary Bit to check all interrupt cause. */ 1649 reg = MVNETA_READ(sc, MVNETA_PRXTXTIM); 1650 reg |= MVNETA_PRXTXTI_PMISCICSUMMARY; 1651 MVNETA_WRITE(sc, MVNETA_PRXTXTIM, reg); 1652 1653 if (sc->use_inband_status) { 1654 /* Enable Port MISC Intr. (via RXTX_TH_Summary bit) */ 1655 MVNETA_WRITE(sc, MVNETA_PMIM, MVNETA_PMI_PHYSTATUSCHNG | 1656 MVNETA_PMI_LINKCHANGE | MVNETA_PMI_PSCSYNCCHANGE); 1657 } 1658 1659 /* Enable All Queue Interrupt */ 1660 reg = MVNETA_READ(sc, MVNETA_PIE); 1661 reg |= MVNETA_PIE_RXPKTINTRPTENB_MASK; 1662 reg |= MVNETA_PIE_TXPKTINTRPTENB_MASK; 1663 MVNETA_WRITE(sc, MVNETA_PIE, reg); 1664 } 1665 1666 STATIC void 1667 mvneta_rxtxth_intr(void *arg) 1668 { 1669 struct mvneta_softc *sc; 1670 struct ifnet *ifp; 1671 uint32_t ic, queues; 1672 1673 sc = arg; 1674 ifp = sc->ifp; 1675 #ifdef MVNETA_KTR 1676 CTR1(KTR_SPARE2, "%s got RXTX_TH_Intr", ifp->if_xname); 1677 #endif 1678 ic = MVNETA_READ(sc, MVNETA_PRXTXTIC); 1679 if (ic == 0) 1680 return; 1681 MVNETA_WRITE(sc, MVNETA_PRXTXTIC, ~ic); 1682 1683 /* Ack maintance interrupt first */ 1684 if (__predict_false((ic & MVNETA_PRXTXTI_PMISCICSUMMARY) && 1685 sc->use_inband_status)) { 1686 mvneta_sc_lock(sc); 1687 mvneta_misc_intr(sc); 1688 mvneta_sc_unlock(sc); 1689 } 1690 if (__predict_false(!(ifp->if_drv_flags & IFF_DRV_RUNNING))) 1691 return; 1692 /* RxTxTH interrupt */ 1693 queues = MVNETA_PRXTXTI_GET_RBICTAPQ(ic); 1694 if (__predict_true(queues)) { 1695 #ifdef MVNETA_KTR 1696 CTR1(KTR_SPARE2, "%s got PRXTXTIC: +RXEOF", ifp->if_xname); 1697 #endif 1698 /* At the moment the driver support only one RX queue. */ 1699 DASSERT(MVNETA_IS_QUEUE_SET(queues, 0)); 1700 mvneta_rx(sc, 0, 0); 1701 } 1702 } 1703 1704 STATIC int 1705 mvneta_misc_intr(struct mvneta_softc *sc) 1706 { 1707 uint32_t ic; 1708 int claimed = 0; 1709 1710 #ifdef MVNETA_KTR 1711 CTR1(KTR_SPARE2, "%s got MISC_INTR", sc->ifp->if_xname); 1712 #endif 1713 KASSERT_SC_MTX(sc); 1714 1715 for (;;) { 1716 ic = MVNETA_READ(sc, MVNETA_PMIC); 1717 ic &= MVNETA_READ(sc, MVNETA_PMIM); 1718 if (ic == 0) 1719 break; 1720 MVNETA_WRITE(sc, MVNETA_PMIC, ~ic); 1721 claimed = 1; 1722 1723 if (ic & (MVNETA_PMI_PHYSTATUSCHNG | 1724 MVNETA_PMI_LINKCHANGE | MVNETA_PMI_PSCSYNCCHANGE)) 1725 mvneta_link_isr(sc); 1726 } 1727 return (claimed); 1728 } 1729 1730 STATIC void 1731 mvneta_tick(void *arg) 1732 { 1733 struct mvneta_softc *sc; 1734 struct mvneta_tx_ring *tx; 1735 struct mvneta_rx_ring *rx; 1736 int q; 1737 uint32_t fc_prev, fc_curr; 1738 1739 sc = arg; 1740 1741 /* 1742 * This is done before mib update to get the right stats 1743 * for this tick. 1744 */ 1745 mvneta_tx_drain(sc); 1746 1747 /* Extract previous flow-control frame received counter. */ 1748 fc_prev = sc->sysctl_mib[MVNETA_MIB_FC_GOOD_IDX].counter; 1749 /* Read mib registers (clear by read). */ 1750 mvneta_update_mib(sc); 1751 /* Extract current flow-control frame received counter. */ 1752 fc_curr = sc->sysctl_mib[MVNETA_MIB_FC_GOOD_IDX].counter; 1753 1754 1755 if (sc->phy_attached && sc->ifp->if_flags & IFF_UP) { 1756 mvneta_sc_lock(sc); 1757 mii_tick(sc->mii); 1758 1759 /* Adjust MAC settings */ 1760 mvneta_adjust_link(sc); 1761 mvneta_sc_unlock(sc); 1762 } 1763 1764 /* 1765 * We were unable to refill the rx queue and left the rx func, leaving 1766 * the ring without mbuf and no way to call the refill func. 1767 */ 1768 for (q = 0; q < MVNETA_RX_QNUM_MAX; q++) { 1769 rx = MVNETA_RX_RING(sc, q); 1770 if (rx->needs_refill == TRUE) { 1771 mvneta_rx_lockq(sc, q); 1772 mvneta_rx_queue_refill(sc, q); 1773 mvneta_rx_unlockq(sc, q); 1774 } 1775 } 1776 1777 /* 1778 * Watchdog: 1779 * - check if queue is mark as hung. 1780 * - ignore hung status if we received some pause frame 1781 * as hardware may have paused packet transmit. 1782 */ 1783 for (q = 0; q < MVNETA_TX_QNUM_MAX; q++) { 1784 /* 1785 * We should take queue lock, but as we only read 1786 * queue status we can do it without lock, we may 1787 * only missdetect queue status for one tick. 1788 */ 1789 tx = MVNETA_TX_RING(sc, q); 1790 1791 if (tx->queue_hung && (fc_curr - fc_prev) == 0) 1792 goto timeout; 1793 } 1794 1795 callout_schedule(&sc->tick_ch, hz); 1796 return; 1797 1798 timeout: 1799 if_printf(sc->ifp, "watchdog timeout\n"); 1800 1801 mvneta_sc_lock(sc); 1802 sc->counter_watchdog++; 1803 sc->counter_watchdog_mib++; 1804 /* Trigger reinitialize sequence. */ 1805 mvneta_stop_locked(sc); 1806 mvneta_init_locked(sc); 1807 mvneta_sc_unlock(sc); 1808 } 1809 1810 STATIC void 1811 mvneta_qflush(struct ifnet *ifp) 1812 { 1813 #ifdef MVNETA_MULTIQUEUE 1814 struct mvneta_softc *sc; 1815 struct mvneta_tx_ring *tx; 1816 struct mbuf *m; 1817 size_t q; 1818 1819 sc = ifp->if_softc; 1820 1821 for (q = 0; q < MVNETA_TX_QNUM_MAX; q++) { 1822 tx = MVNETA_TX_RING(sc, q); 1823 mvneta_tx_lockq(sc, q); 1824 while ((m = buf_ring_dequeue_sc(tx->br)) != NULL) 1825 m_freem(m); 1826 mvneta_tx_unlockq(sc, q); 1827 } 1828 #endif 1829 if_qflush(ifp); 1830 } 1831 1832 STATIC void 1833 mvneta_tx_task(void *arg, int pending) 1834 { 1835 struct mvneta_softc *sc; 1836 struct mvneta_tx_ring *tx; 1837 struct ifnet *ifp; 1838 int error; 1839 1840 tx = arg; 1841 ifp = tx->ifp; 1842 sc = ifp->if_softc; 1843 1844 mvneta_tx_lockq(sc, tx->qidx); 1845 error = mvneta_xmit_locked(sc, tx->qidx); 1846 mvneta_tx_unlockq(sc, tx->qidx); 1847 1848 /* Try again */ 1849 if (__predict_false(error != 0 && error != ENETDOWN)) { 1850 pause("mvneta_tx_task_sleep", 1); 1851 taskqueue_enqueue(tx->taskq, &tx->task); 1852 } 1853 } 1854 1855 STATIC int 1856 mvneta_xmitfast_locked(struct mvneta_softc *sc, int q, struct mbuf **m) 1857 { 1858 struct mvneta_tx_ring *tx; 1859 struct ifnet *ifp; 1860 int error; 1861 1862 KASSERT_TX_MTX(sc, q); 1863 tx = MVNETA_TX_RING(sc, q); 1864 error = 0; 1865 1866 ifp = sc->ifp; 1867 1868 /* Dont enqueue packet if the queue is disabled. */ 1869 if (__predict_false(tx->queue_status == MVNETA_QUEUE_DISABLED)) { 1870 m_freem(*m); 1871 *m = NULL; 1872 return (ENETDOWN); 1873 } 1874 1875 /* Reclaim mbuf if above threshold. */ 1876 if (__predict_true(tx->used > MVNETA_TX_RECLAIM_COUNT)) 1877 mvneta_tx_queue_complete(sc, q); 1878 1879 /* Do not call transmit path if queue is already too full. */ 1880 if (__predict_false(tx->used > 1881 MVNETA_TX_RING_CNT - MVNETA_TX_SEGLIMIT)) 1882 return (ENOBUFS); 1883 1884 error = mvneta_tx_queue(sc, m, q); 1885 if (__predict_false(error != 0)) 1886 return (error); 1887 1888 /* Send a copy of the frame to the BPF listener */ 1889 ETHER_BPF_MTAP(ifp, *m); 1890 1891 /* Set watchdog on */ 1892 tx->watchdog_time = ticks; 1893 tx->queue_status = MVNETA_QUEUE_WORKING; 1894 1895 return (error); 1896 } 1897 1898 #ifdef MVNETA_MULTIQUEUE 1899 STATIC int 1900 mvneta_transmit(struct ifnet *ifp, struct mbuf *m) 1901 { 1902 struct mvneta_softc *sc; 1903 struct mvneta_tx_ring *tx; 1904 int error; 1905 int q; 1906 1907 sc = ifp->if_softc; 1908 1909 /* Use default queue if there is no flow id as thread can migrate. */ 1910 if (__predict_true(M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)) 1911 q = m->m_pkthdr.flowid % MVNETA_TX_QNUM_MAX; 1912 else 1913 q = 0; 1914 1915 tx = MVNETA_TX_RING(sc, q); 1916 1917 /* If buf_ring is full start transmit immediatly. */ 1918 if (buf_ring_full(tx->br)) { 1919 mvneta_tx_lockq(sc, q); 1920 mvneta_xmit_locked(sc, q); 1921 mvneta_tx_unlockq(sc, q); 1922 } 1923 1924 /* 1925 * If the buf_ring is empty we will not reorder packets. 1926 * If the lock is available transmit without using buf_ring. 1927 */ 1928 if (buf_ring_empty(tx->br) && mvneta_tx_trylockq(sc, q) != 0) { 1929 error = mvneta_xmitfast_locked(sc, q, &m); 1930 mvneta_tx_unlockq(sc, q); 1931 if (__predict_true(error == 0)) 1932 return (0); 1933 1934 /* Transmit can fail in fastpath. */ 1935 if (__predict_false(m == NULL)) 1936 return (error); 1937 } 1938 1939 /* Enqueue then schedule taskqueue. */ 1940 error = drbr_enqueue(ifp, tx->br, m); 1941 if (__predict_false(error != 0)) 1942 return (error); 1943 1944 taskqueue_enqueue(tx->taskq, &tx->task); 1945 return (0); 1946 } 1947 1948 STATIC int 1949 mvneta_xmit_locked(struct mvneta_softc *sc, int q) 1950 { 1951 struct ifnet *ifp; 1952 struct mvneta_tx_ring *tx; 1953 struct mbuf *m; 1954 int error; 1955 1956 KASSERT_TX_MTX(sc, q); 1957 ifp = sc->ifp; 1958 tx = MVNETA_TX_RING(sc, q); 1959 error = 0; 1960 1961 while ((m = drbr_peek(ifp, tx->br)) != NULL) { 1962 error = mvneta_xmitfast_locked(sc, q, &m); 1963 if (__predict_false(error != 0)) { 1964 if (m != NULL) 1965 drbr_putback(ifp, tx->br, m); 1966 else 1967 drbr_advance(ifp, tx->br); 1968 break; 1969 } 1970 drbr_advance(ifp, tx->br); 1971 } 1972 1973 return (error); 1974 } 1975 #else /* !MVNETA_MULTIQUEUE */ 1976 STATIC void 1977 mvneta_start(struct ifnet *ifp) 1978 { 1979 struct mvneta_softc *sc; 1980 struct mvneta_tx_ring *tx; 1981 int error; 1982 1983 sc = ifp->if_softc; 1984 tx = MVNETA_TX_RING(sc, 0); 1985 1986 mvneta_tx_lockq(sc, 0); 1987 error = mvneta_xmit_locked(sc, 0); 1988 mvneta_tx_unlockq(sc, 0); 1989 /* Handle retransmit in the background taskq. */ 1990 if (__predict_false(error != 0 && error != ENETDOWN)) 1991 taskqueue_enqueue(tx->taskq, &tx->task); 1992 } 1993 1994 STATIC int 1995 mvneta_xmit_locked(struct mvneta_softc *sc, int q) 1996 { 1997 struct ifnet *ifp; 1998 struct mvneta_tx_ring *tx; 1999 struct mbuf *m; 2000 int error; 2001 2002 KASSERT_TX_MTX(sc, q); 2003 ifp = sc->ifp; 2004 tx = MVNETA_TX_RING(sc, 0); 2005 error = 0; 2006 2007 while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) { 2008 IFQ_DRV_DEQUEUE(&ifp->if_snd, m); 2009 if (m == NULL) 2010 break; 2011 2012 error = mvneta_xmitfast_locked(sc, q, &m); 2013 if (__predict_false(error != 0)) { 2014 if (m != NULL) 2015 IFQ_DRV_PREPEND(&ifp->if_snd, m); 2016 break; 2017 } 2018 } 2019 2020 return (error); 2021 } 2022 #endif 2023 2024 STATIC int 2025 mvneta_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 2026 { 2027 struct mvneta_softc *sc; 2028 struct mvneta_rx_ring *rx; 2029 struct ifreq *ifr; 2030 int error, mask; 2031 uint32_t flags; 2032 int q; 2033 2034 error = 0; 2035 sc = ifp->if_softc; 2036 ifr = (struct ifreq *)data; 2037 switch (cmd) { 2038 case SIOCSIFFLAGS: 2039 mvneta_sc_lock(sc); 2040 if (ifp->if_flags & IFF_UP) { 2041 if (ifp->if_drv_flags & IFF_DRV_RUNNING) { 2042 flags = ifp->if_flags ^ sc->mvneta_if_flags; 2043 2044 if (flags != 0) 2045 sc->mvneta_if_flags = ifp->if_flags; 2046 2047 if ((flags & IFF_PROMISC) != 0) 2048 mvneta_filter_setup(sc); 2049 } else { 2050 mvneta_init_locked(sc); 2051 sc->mvneta_if_flags = ifp->if_flags; 2052 if (sc->phy_attached) 2053 mii_mediachg(sc->mii); 2054 mvneta_sc_unlock(sc); 2055 break; 2056 } 2057 } else if (ifp->if_drv_flags & IFF_DRV_RUNNING) 2058 mvneta_stop_locked(sc); 2059 2060 sc->mvneta_if_flags = ifp->if_flags; 2061 mvneta_sc_unlock(sc); 2062 break; 2063 case SIOCSIFCAP: 2064 if (ifp->if_mtu > MVNETA_MAX_CSUM_MTU && 2065 ifr->ifr_reqcap & IFCAP_TXCSUM) 2066 ifr->ifr_reqcap &= ~IFCAP_TXCSUM; 2067 mask = ifp->if_capenable ^ ifr->ifr_reqcap; 2068 if (mask & IFCAP_HWCSUM) { 2069 ifp->if_capenable &= ~IFCAP_HWCSUM; 2070 ifp->if_capenable |= IFCAP_HWCSUM & ifr->ifr_reqcap; 2071 if (ifp->if_capenable & IFCAP_TXCSUM) 2072 ifp->if_hwassist = CSUM_IP | CSUM_TCP | 2073 CSUM_UDP; 2074 else 2075 ifp->if_hwassist = 0; 2076 } 2077 if (mask & IFCAP_LRO) { 2078 mvneta_sc_lock(sc); 2079 ifp->if_capenable ^= IFCAP_LRO; 2080 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) { 2081 for (q = 0; q < MVNETA_RX_QNUM_MAX; q++) { 2082 rx = MVNETA_RX_RING(sc, q); 2083 rx->lro_enabled = !rx->lro_enabled; 2084 } 2085 } 2086 mvneta_sc_unlock(sc); 2087 } 2088 VLAN_CAPABILITIES(ifp); 2089 break; 2090 case SIOCSIFMEDIA: 2091 if ((IFM_SUBTYPE(ifr->ifr_media) == IFM_1000_T || 2092 IFM_SUBTYPE(ifr->ifr_media) == IFM_2500_T) && 2093 (ifr->ifr_media & IFM_FDX) == 0) { 2094 device_printf(sc->dev, 2095 "%s half-duplex unsupported\n", 2096 IFM_SUBTYPE(ifr->ifr_media) == IFM_1000_T ? 2097 "1000Base-T" : 2098 "2500Base-T"); 2099 error = EINVAL; 2100 break; 2101 } 2102 case SIOCGIFMEDIA: /* FALLTHROUGH */ 2103 if (!sc->phy_attached) 2104 error = ifmedia_ioctl(ifp, ifr, &sc->mvneta_ifmedia, 2105 cmd); 2106 else 2107 error = ifmedia_ioctl(ifp, ifr, &sc->mii->mii_media, 2108 cmd); 2109 break; 2110 case SIOCSIFMTU: 2111 if (ifr->ifr_mtu < 68 || ifr->ifr_mtu > MVNETA_MAX_FRAME - 2112 MVNETA_ETHER_SIZE) { 2113 error = EINVAL; 2114 } else { 2115 ifp->if_mtu = ifr->ifr_mtu; 2116 mvneta_sc_lock(sc); 2117 if (ifp->if_mtu > MVNETA_MAX_CSUM_MTU) { 2118 ifp->if_capenable &= ~IFCAP_TXCSUM; 2119 ifp->if_hwassist = 0; 2120 } else { 2121 ifp->if_capenable |= IFCAP_TXCSUM; 2122 ifp->if_hwassist = CSUM_IP | CSUM_TCP | 2123 CSUM_UDP; 2124 } 2125 2126 if (ifp->if_drv_flags & IFF_DRV_RUNNING) { 2127 /* Trigger reinitialize sequence */ 2128 mvneta_stop_locked(sc); 2129 mvneta_init_locked(sc); 2130 } 2131 mvneta_sc_unlock(sc); 2132 } 2133 break; 2134 2135 default: 2136 error = ether_ioctl(ifp, cmd, data); 2137 break; 2138 } 2139 2140 return (error); 2141 } 2142 2143 STATIC void 2144 mvneta_init_locked(void *arg) 2145 { 2146 struct mvneta_softc *sc; 2147 struct ifnet *ifp; 2148 uint32_t reg; 2149 int q, cpu; 2150 2151 sc = arg; 2152 ifp = sc->ifp; 2153 2154 if (!device_is_attached(sc->dev) || 2155 (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) 2156 return; 2157 2158 mvneta_disable_intr(sc); 2159 callout_stop(&sc->tick_ch); 2160 2161 /* Get the latest mac address */ 2162 bcopy(IF_LLADDR(ifp), sc->enaddr, ETHER_ADDR_LEN); 2163 mvneta_set_mac_address(sc, sc->enaddr); 2164 mvneta_filter_setup(sc); 2165 2166 /* Start DMA Engine */ 2167 MVNETA_WRITE(sc, MVNETA_PRXINIT, 0x00000000); 2168 MVNETA_WRITE(sc, MVNETA_PTXINIT, 0x00000000); 2169 MVNETA_WRITE(sc, MVNETA_PACC, MVNETA_PACC_ACCELERATIONMODE_EDM); 2170 2171 /* Enable port */ 2172 reg = MVNETA_READ(sc, MVNETA_PMACC0); 2173 reg |= MVNETA_PMACC0_PORTEN; 2174 MVNETA_WRITE(sc, MVNETA_PMACC0, reg); 2175 2176 /* Allow access to each TXQ/RXQ from both CPU's */ 2177 for (cpu = 0; cpu < mp_ncpus; ++cpu) 2178 MVNETA_WRITE(sc, MVNETA_PCP2Q(cpu), 2179 MVNETA_PCP2Q_TXQEN_MASK | MVNETA_PCP2Q_RXQEN_MASK); 2180 2181 for (q = 0; q < MVNETA_RX_QNUM_MAX; q++) { 2182 mvneta_rx_lockq(sc, q); 2183 mvneta_rx_queue_refill(sc, q); 2184 mvneta_rx_unlockq(sc, q); 2185 } 2186 2187 if (!sc->phy_attached) 2188 mvneta_linkup(sc); 2189 2190 /* Enable interrupt */ 2191 mvneta_enable_intr(sc); 2192 2193 /* Set Counter */ 2194 callout_schedule(&sc->tick_ch, hz); 2195 2196 ifp->if_drv_flags |= IFF_DRV_RUNNING; 2197 } 2198 2199 STATIC void 2200 mvneta_init(void *arg) 2201 { 2202 struct mvneta_softc *sc; 2203 2204 sc = arg; 2205 mvneta_sc_lock(sc); 2206 mvneta_init_locked(sc); 2207 if (sc->phy_attached) 2208 mii_mediachg(sc->mii); 2209 mvneta_sc_unlock(sc); 2210 } 2211 2212 /* ARGSUSED */ 2213 STATIC void 2214 mvneta_stop_locked(struct mvneta_softc *sc) 2215 { 2216 struct ifnet *ifp; 2217 struct mvneta_rx_ring *rx; 2218 struct mvneta_tx_ring *tx; 2219 uint32_t reg; 2220 int q; 2221 2222 ifp = sc->ifp; 2223 if (ifp == NULL || (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) 2224 return; 2225 2226 mvneta_disable_intr(sc); 2227 2228 callout_stop(&sc->tick_ch); 2229 2230 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 2231 2232 /* Link down */ 2233 if (sc->linkup == TRUE) 2234 mvneta_linkdown(sc); 2235 2236 /* Reset the MAC Port Enable bit */ 2237 reg = MVNETA_READ(sc, MVNETA_PMACC0); 2238 reg &= ~MVNETA_PMACC0_PORTEN; 2239 MVNETA_WRITE(sc, MVNETA_PMACC0, reg); 2240 2241 /* Disable each of queue */ 2242 for (q = 0; q < MVNETA_RX_QNUM_MAX; q++) { 2243 rx = MVNETA_RX_RING(sc, q); 2244 2245 mvneta_rx_lockq(sc, q); 2246 mvneta_ring_flush_rx_queue(sc, q); 2247 mvneta_rx_unlockq(sc, q); 2248 } 2249 2250 /* 2251 * Hold Reset state of DMA Engine 2252 * (must write 0x0 to restart it) 2253 */ 2254 MVNETA_WRITE(sc, MVNETA_PRXINIT, 0x00000001); 2255 MVNETA_WRITE(sc, MVNETA_PTXINIT, 0x00000001); 2256 2257 for (q = 0; q < MVNETA_TX_QNUM_MAX; q++) { 2258 tx = MVNETA_TX_RING(sc, q); 2259 2260 mvneta_tx_lockq(sc, q); 2261 mvneta_ring_flush_tx_queue(sc, q); 2262 mvneta_tx_unlockq(sc, q); 2263 } 2264 } 2265 2266 STATIC void 2267 mvneta_stop(struct mvneta_softc *sc) 2268 { 2269 2270 mvneta_sc_lock(sc); 2271 mvneta_stop_locked(sc); 2272 mvneta_sc_unlock(sc); 2273 } 2274 2275 STATIC int 2276 mvneta_mediachange(struct ifnet *ifp) 2277 { 2278 struct mvneta_softc *sc; 2279 2280 sc = ifp->if_softc; 2281 2282 if (!sc->phy_attached && !sc->use_inband_status) { 2283 /* We shouldn't be here */ 2284 if_printf(ifp, "Cannot change media in fixed-link mode!\n"); 2285 return (0); 2286 } 2287 2288 if (sc->use_inband_status) { 2289 mvneta_update_media(sc, sc->mvneta_ifmedia.ifm_media); 2290 return (0); 2291 } 2292 2293 mvneta_sc_lock(sc); 2294 2295 /* Update PHY */ 2296 mii_mediachg(sc->mii); 2297 2298 mvneta_sc_unlock(sc); 2299 2300 return (0); 2301 } 2302 2303 STATIC void 2304 mvneta_get_media(struct mvneta_softc *sc, struct ifmediareq *ifmr) 2305 { 2306 uint32_t psr; 2307 2308 psr = MVNETA_READ(sc, MVNETA_PSR); 2309 2310 /* Speed */ 2311 if (psr & MVNETA_PSR_GMIISPEED) 2312 ifmr->ifm_active = IFM_ETHER_SUBTYPE_SET(IFM_1000_T); 2313 else if (psr & MVNETA_PSR_MIISPEED) 2314 ifmr->ifm_active = IFM_ETHER_SUBTYPE_SET(IFM_100_TX); 2315 else if (psr & MVNETA_PSR_LINKUP) 2316 ifmr->ifm_active = IFM_ETHER_SUBTYPE_SET(IFM_10_T); 2317 2318 /* Duplex */ 2319 if (psr & MVNETA_PSR_FULLDX) 2320 ifmr->ifm_active |= IFM_FDX; 2321 2322 /* Link */ 2323 ifmr->ifm_status = IFM_AVALID; 2324 if (psr & MVNETA_PSR_LINKUP) 2325 ifmr->ifm_status |= IFM_ACTIVE; 2326 } 2327 2328 STATIC void 2329 mvneta_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr) 2330 { 2331 struct mvneta_softc *sc; 2332 struct mii_data *mii; 2333 2334 sc = ifp->if_softc; 2335 2336 if (!sc->phy_attached && !sc->use_inband_status) { 2337 ifmr->ifm_status = IFM_AVALID | IFM_ACTIVE; 2338 return; 2339 } 2340 2341 mvneta_sc_lock(sc); 2342 2343 if (sc->use_inband_status) { 2344 mvneta_get_media(sc, ifmr); 2345 mvneta_sc_unlock(sc); 2346 return; 2347 } 2348 2349 mii = sc->mii; 2350 mii_pollstat(mii); 2351 2352 ifmr->ifm_active = mii->mii_media_active; 2353 ifmr->ifm_status = mii->mii_media_status; 2354 2355 mvneta_sc_unlock(sc); 2356 } 2357 2358 /* 2359 * Link State Notify 2360 */ 2361 STATIC void 2362 mvneta_update_autoneg(struct mvneta_softc *sc, int enable) 2363 { 2364 int reg; 2365 2366 if (enable) { 2367 reg = MVNETA_READ(sc, MVNETA_PANC); 2368 reg &= ~(MVNETA_PANC_FORCELINKFAIL | MVNETA_PANC_FORCELINKPASS | 2369 MVNETA_PANC_ANFCEN); 2370 reg |= MVNETA_PANC_ANDUPLEXEN | MVNETA_PANC_ANSPEEDEN | 2371 MVNETA_PANC_INBANDANEN; 2372 MVNETA_WRITE(sc, MVNETA_PANC, reg); 2373 2374 reg = MVNETA_READ(sc, MVNETA_PMACC2); 2375 reg |= MVNETA_PMACC2_INBANDANMODE; 2376 MVNETA_WRITE(sc, MVNETA_PMACC2, reg); 2377 2378 reg = MVNETA_READ(sc, MVNETA_PSOMSCD); 2379 reg |= MVNETA_PSOMSCD_ENABLE; 2380 MVNETA_WRITE(sc, MVNETA_PSOMSCD, reg); 2381 } else { 2382 reg = MVNETA_READ(sc, MVNETA_PANC); 2383 reg &= ~(MVNETA_PANC_FORCELINKFAIL | MVNETA_PANC_FORCELINKPASS | 2384 MVNETA_PANC_ANDUPLEXEN | MVNETA_PANC_ANSPEEDEN | 2385 MVNETA_PANC_INBANDANEN); 2386 MVNETA_WRITE(sc, MVNETA_PANC, reg); 2387 2388 reg = MVNETA_READ(sc, MVNETA_PMACC2); 2389 reg &= ~MVNETA_PMACC2_INBANDANMODE; 2390 MVNETA_WRITE(sc, MVNETA_PMACC2, reg); 2391 2392 reg = MVNETA_READ(sc, MVNETA_PSOMSCD); 2393 reg &= ~MVNETA_PSOMSCD_ENABLE; 2394 MVNETA_WRITE(sc, MVNETA_PSOMSCD, reg); 2395 } 2396 } 2397 2398 STATIC int 2399 mvneta_update_media(struct mvneta_softc *sc, int media) 2400 { 2401 int reg, err; 2402 boolean_t running; 2403 2404 err = 0; 2405 2406 mvneta_sc_lock(sc); 2407 2408 mvneta_linkreset(sc); 2409 2410 running = (sc->ifp->if_drv_flags & IFF_DRV_RUNNING) != 0; 2411 if (running) 2412 mvneta_stop_locked(sc); 2413 2414 sc->autoneg = (IFM_SUBTYPE(media) == IFM_AUTO); 2415 2416 if (sc->use_inband_status) 2417 mvneta_update_autoneg(sc, IFM_SUBTYPE(media) == IFM_AUTO); 2418 2419 mvneta_update_eee(sc); 2420 mvneta_update_fc(sc); 2421 2422 if (IFM_SUBTYPE(media) != IFM_AUTO) { 2423 reg = MVNETA_READ(sc, MVNETA_PANC); 2424 reg &= ~(MVNETA_PANC_SETGMIISPEED | 2425 MVNETA_PANC_SETMIISPEED | 2426 MVNETA_PANC_SETFULLDX); 2427 if (IFM_SUBTYPE(media) == IFM_1000_T || 2428 IFM_SUBTYPE(media) == IFM_2500_T) { 2429 if ((media & IFM_FDX) == 0) { 2430 device_printf(sc->dev, 2431 "%s half-duplex unsupported\n", 2432 IFM_SUBTYPE(media) == IFM_1000_T ? 2433 "1000Base-T" : 2434 "2500Base-T"); 2435 err = EINVAL; 2436 goto out; 2437 } 2438 reg |= MVNETA_PANC_SETGMIISPEED; 2439 } else if (IFM_SUBTYPE(media) == IFM_100_TX) 2440 reg |= MVNETA_PANC_SETMIISPEED; 2441 2442 if (media & IFM_FDX) 2443 reg |= MVNETA_PANC_SETFULLDX; 2444 2445 MVNETA_WRITE(sc, MVNETA_PANC, reg); 2446 } 2447 out: 2448 if (running) 2449 mvneta_init_locked(sc); 2450 mvneta_sc_unlock(sc); 2451 return (err); 2452 } 2453 2454 STATIC void 2455 mvneta_adjust_link(struct mvneta_softc *sc) 2456 { 2457 boolean_t phy_linkup; 2458 int reg; 2459 2460 /* Update eee/fc */ 2461 mvneta_update_eee(sc); 2462 mvneta_update_fc(sc); 2463 2464 /* Check for link change */ 2465 phy_linkup = (sc->mii->mii_media_status & 2466 (IFM_AVALID | IFM_ACTIVE)) == (IFM_AVALID | IFM_ACTIVE); 2467 2468 if (sc->linkup != phy_linkup) 2469 mvneta_linkupdate(sc, phy_linkup); 2470 2471 /* Don't update media on disabled link */ 2472 if (!phy_linkup ) 2473 return; 2474 2475 /* Check for media type change */ 2476 if (sc->mvneta_media != sc->mii->mii_media_active) { 2477 sc->mvneta_media = sc->mii->mii_media_active; 2478 2479 reg = MVNETA_READ(sc, MVNETA_PANC); 2480 reg &= ~(MVNETA_PANC_SETGMIISPEED | 2481 MVNETA_PANC_SETMIISPEED | 2482 MVNETA_PANC_SETFULLDX); 2483 if (IFM_SUBTYPE(sc->mvneta_media) == IFM_1000_T || 2484 IFM_SUBTYPE(sc->mvneta_media) == IFM_2500_T) { 2485 reg |= MVNETA_PANC_SETGMIISPEED; 2486 } else if (IFM_SUBTYPE(sc->mvneta_media) == IFM_100_TX) 2487 reg |= MVNETA_PANC_SETMIISPEED; 2488 2489 if (sc->mvneta_media & IFM_FDX) 2490 reg |= MVNETA_PANC_SETFULLDX; 2491 2492 MVNETA_WRITE(sc, MVNETA_PANC, reg); 2493 } 2494 } 2495 2496 STATIC void 2497 mvneta_link_isr(struct mvneta_softc *sc) 2498 { 2499 int linkup; 2500 2501 KASSERT_SC_MTX(sc); 2502 2503 linkup = MVNETA_IS_LINKUP(sc) ? TRUE : FALSE; 2504 if (sc->linkup == linkup) 2505 return; 2506 2507 if (linkup == TRUE) 2508 mvneta_linkup(sc); 2509 else 2510 mvneta_linkdown(sc); 2511 2512 #ifdef DEBUG 2513 log(LOG_DEBUG, 2514 "%s: link %s\n", device_xname(sc->dev), linkup ? "up" : "down"); 2515 #endif 2516 } 2517 2518 STATIC void 2519 mvneta_linkupdate(struct mvneta_softc *sc, boolean_t linkup) 2520 { 2521 2522 KASSERT_SC_MTX(sc); 2523 2524 if (linkup == TRUE) 2525 mvneta_linkup(sc); 2526 else 2527 mvneta_linkdown(sc); 2528 2529 #ifdef DEBUG 2530 log(LOG_DEBUG, 2531 "%s: link %s\n", device_xname(sc->dev), linkup ? "up" : "down"); 2532 #endif 2533 } 2534 2535 STATIC void 2536 mvneta_update_eee(struct mvneta_softc *sc) 2537 { 2538 uint32_t reg; 2539 2540 KASSERT_SC_MTX(sc); 2541 2542 /* set EEE parameters */ 2543 reg = MVNETA_READ(sc, MVNETA_LPIC1); 2544 if (sc->cf_lpi) 2545 reg |= MVNETA_LPIC1_LPIRE; 2546 else 2547 reg &= ~MVNETA_LPIC1_LPIRE; 2548 MVNETA_WRITE(sc, MVNETA_LPIC1, reg); 2549 } 2550 2551 STATIC void 2552 mvneta_update_fc(struct mvneta_softc *sc) 2553 { 2554 uint32_t reg; 2555 2556 KASSERT_SC_MTX(sc); 2557 2558 reg = MVNETA_READ(sc, MVNETA_PANC); 2559 if (sc->cf_fc) { 2560 /* Flow control negotiation */ 2561 reg |= MVNETA_PANC_PAUSEADV; 2562 reg |= MVNETA_PANC_ANFCEN; 2563 } else { 2564 /* Disable flow control negotiation */ 2565 reg &= ~MVNETA_PANC_PAUSEADV; 2566 reg &= ~MVNETA_PANC_ANFCEN; 2567 } 2568 2569 MVNETA_WRITE(sc, MVNETA_PANC, reg); 2570 } 2571 2572 STATIC void 2573 mvneta_linkup(struct mvneta_softc *sc) 2574 { 2575 uint32_t reg; 2576 2577 KASSERT_SC_MTX(sc); 2578 2579 if (!sc->use_inband_status) { 2580 reg = MVNETA_READ(sc, MVNETA_PANC); 2581 reg |= MVNETA_PANC_FORCELINKPASS; 2582 reg &= ~MVNETA_PANC_FORCELINKFAIL; 2583 MVNETA_WRITE(sc, MVNETA_PANC, reg); 2584 } 2585 2586 mvneta_qflush(sc->ifp); 2587 mvneta_portup(sc); 2588 sc->linkup = TRUE; 2589 if_link_state_change(sc->ifp, LINK_STATE_UP); 2590 } 2591 2592 STATIC void 2593 mvneta_linkdown(struct mvneta_softc *sc) 2594 { 2595 uint32_t reg; 2596 2597 KASSERT_SC_MTX(sc); 2598 2599 if (!sc->use_inband_status) { 2600 reg = MVNETA_READ(sc, MVNETA_PANC); 2601 reg &= ~MVNETA_PANC_FORCELINKPASS; 2602 reg |= MVNETA_PANC_FORCELINKFAIL; 2603 MVNETA_WRITE(sc, MVNETA_PANC, reg); 2604 } 2605 2606 mvneta_portdown(sc); 2607 mvneta_qflush(sc->ifp); 2608 sc->linkup = FALSE; 2609 if_link_state_change(sc->ifp, LINK_STATE_DOWN); 2610 } 2611 2612 STATIC void 2613 mvneta_linkreset(struct mvneta_softc *sc) 2614 { 2615 struct mii_softc *mii; 2616 2617 if (sc->phy_attached) { 2618 /* Force reset PHY */ 2619 mii = LIST_FIRST(&sc->mii->mii_phys); 2620 if (mii) 2621 mii_phy_reset(mii); 2622 } 2623 } 2624 2625 /* 2626 * Tx Subroutines 2627 */ 2628 STATIC int 2629 mvneta_tx_queue(struct mvneta_softc *sc, struct mbuf **mbufp, int q) 2630 { 2631 struct ifnet *ifp; 2632 bus_dma_segment_t txsegs[MVNETA_TX_SEGLIMIT]; 2633 struct mbuf *mtmp, *mbuf; 2634 struct mvneta_tx_ring *tx; 2635 struct mvneta_buf *txbuf; 2636 struct mvneta_tx_desc *t; 2637 uint32_t ptxsu; 2638 int start, used, error, i, txnsegs; 2639 2640 mbuf = *mbufp; 2641 tx = MVNETA_TX_RING(sc, q); 2642 DASSERT(tx->used >= 0); 2643 DASSERT(tx->used <= MVNETA_TX_RING_CNT); 2644 t = NULL; 2645 ifp = sc->ifp; 2646 2647 if (__predict_false(mbuf->m_flags & M_VLANTAG)) { 2648 mbuf = ether_vlanencap(mbuf, mbuf->m_pkthdr.ether_vtag); 2649 if (mbuf == NULL) { 2650 tx->drv_error++; 2651 *mbufp = NULL; 2652 return (ENOBUFS); 2653 } 2654 mbuf->m_flags &= ~M_VLANTAG; 2655 *mbufp = mbuf; 2656 } 2657 2658 if (__predict_false(mbuf->m_next != NULL && 2659 (mbuf->m_pkthdr.csum_flags & 2660 (CSUM_IP | CSUM_TCP | CSUM_UDP)) != 0)) { 2661 if (M_WRITABLE(mbuf) == 0) { 2662 mtmp = m_dup(mbuf, M_NOWAIT); 2663 m_freem(mbuf); 2664 if (mtmp == NULL) { 2665 tx->drv_error++; 2666 *mbufp = NULL; 2667 return (ENOBUFS); 2668 } 2669 *mbufp = mbuf = mtmp; 2670 } 2671 } 2672 2673 /* load mbuf using dmamap of 1st descriptor */ 2674 txbuf = &tx->txbuf[tx->cpu]; 2675 error = bus_dmamap_load_mbuf_sg(sc->txmbuf_dtag, 2676 txbuf->dmap, mbuf, txsegs, &txnsegs, 2677 BUS_DMA_NOWAIT); 2678 if (__predict_false(error != 0)) { 2679 #ifdef MVNETA_KTR 2680 CTR3(KTR_SPARE2, "%s:%u bus_dmamap_load_mbuf_sg error=%d", ifp->if_xname, q, error); 2681 #endif 2682 /* This is the only recoverable error (except EFBIG). */ 2683 if (error != ENOMEM) { 2684 tx->drv_error++; 2685 m_freem(mbuf); 2686 *mbufp = NULL; 2687 return (ENOBUFS); 2688 } 2689 return (error); 2690 } 2691 2692 if (__predict_false(txnsegs <= 0 2693 || (txnsegs + tx->used) > MVNETA_TX_RING_CNT)) { 2694 /* we have no enough descriptors or mbuf is broken */ 2695 #ifdef MVNETA_KTR 2696 CTR3(KTR_SPARE2, "%s:%u not enough descriptors txnsegs=%d", 2697 ifp->if_xname, q, txnsegs); 2698 #endif 2699 bus_dmamap_unload(sc->txmbuf_dtag, txbuf->dmap); 2700 return (ENOBUFS); 2701 } 2702 DASSERT(txbuf->m == NULL); 2703 2704 /* remember mbuf using 1st descriptor */ 2705 txbuf->m = mbuf; 2706 bus_dmamap_sync(sc->txmbuf_dtag, txbuf->dmap, 2707 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); 2708 2709 /* load to tx descriptors */ 2710 start = tx->cpu; 2711 used = 0; 2712 for (i = 0; i < txnsegs; i++) { 2713 t = &tx->desc[tx->cpu]; 2714 t->command = 0; 2715 t->l4ichk = 0; 2716 t->flags = 0; 2717 if (__predict_true(i == 0)) { 2718 /* 1st descriptor */ 2719 t->command |= MVNETA_TX_CMD_W_PACKET_OFFSET(0); 2720 t->command |= MVNETA_TX_CMD_F; 2721 mvneta_tx_set_csumflag(ifp, t, mbuf); 2722 } 2723 t->bufptr_pa = txsegs[i].ds_addr; 2724 t->bytecnt = txsegs[i].ds_len; 2725 tx->cpu = tx_counter_adv(tx->cpu, 1); 2726 2727 tx->used++; 2728 used++; 2729 } 2730 /* t is last descriptor here */ 2731 DASSERT(t != NULL); 2732 t->command |= MVNETA_TX_CMD_L|MVNETA_TX_CMD_PADDING; 2733 2734 bus_dmamap_sync(sc->tx_dtag, tx->desc_map, 2735 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); 2736 2737 while (__predict_false(used > 255)) { 2738 ptxsu = MVNETA_PTXSU_NOWD(255); 2739 MVNETA_WRITE(sc, MVNETA_PTXSU(q), ptxsu); 2740 used -= 255; 2741 } 2742 if (__predict_true(used > 0)) { 2743 ptxsu = MVNETA_PTXSU_NOWD(used); 2744 MVNETA_WRITE(sc, MVNETA_PTXSU(q), ptxsu); 2745 } 2746 return (0); 2747 } 2748 2749 STATIC void 2750 mvneta_tx_set_csumflag(struct ifnet *ifp, 2751 struct mvneta_tx_desc *t, struct mbuf *m) 2752 { 2753 struct ether_header *eh; 2754 int csum_flags; 2755 uint32_t iphl, ipoff; 2756 struct ip *ip; 2757 2758 iphl = ipoff = 0; 2759 csum_flags = ifp->if_hwassist & m->m_pkthdr.csum_flags; 2760 eh = mtod(m, struct ether_header *); 2761 switch (ntohs(eh->ether_type)) { 2762 case ETHERTYPE_IP: 2763 ipoff = ETHER_HDR_LEN; 2764 break; 2765 case ETHERTYPE_IPV6: 2766 return; 2767 case ETHERTYPE_VLAN: 2768 ipoff = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN; 2769 break; 2770 } 2771 2772 if (__predict_true(csum_flags & (CSUM_IP|CSUM_IP_TCP|CSUM_IP_UDP))) { 2773 ip = (struct ip *)(m->m_data + ipoff); 2774 iphl = ip->ip_hl<<2; 2775 t->command |= MVNETA_TX_CMD_L3_IP4; 2776 } else { 2777 t->command |= MVNETA_TX_CMD_L4_CHECKSUM_NONE; 2778 return; 2779 } 2780 2781 2782 /* L3 */ 2783 if (csum_flags & CSUM_IP) { 2784 t->command |= MVNETA_TX_CMD_IP4_CHECKSUM; 2785 } 2786 2787 /* L4 */ 2788 if (csum_flags & CSUM_IP_TCP) { 2789 t->command |= MVNETA_TX_CMD_L4_CHECKSUM_NOFRAG; 2790 t->command |= MVNETA_TX_CMD_L4_TCP; 2791 } else if (csum_flags & CSUM_IP_UDP) { 2792 t->command |= MVNETA_TX_CMD_L4_CHECKSUM_NOFRAG; 2793 t->command |= MVNETA_TX_CMD_L4_UDP; 2794 } else 2795 t->command |= MVNETA_TX_CMD_L4_CHECKSUM_NONE; 2796 2797 t->l4ichk = 0; 2798 t->command |= MVNETA_TX_CMD_IP_HEADER_LEN(iphl >> 2); 2799 t->command |= MVNETA_TX_CMD_L3_OFFSET(ipoff); 2800 } 2801 2802 STATIC void 2803 mvneta_tx_queue_complete(struct mvneta_softc *sc, int q) 2804 { 2805 struct mvneta_tx_ring *tx; 2806 struct mvneta_buf *txbuf; 2807 struct mvneta_tx_desc *t; 2808 uint32_t ptxs, ptxsu, ndesc; 2809 int i; 2810 2811 KASSERT_TX_MTX(sc, q); 2812 2813 tx = MVNETA_TX_RING(sc, q); 2814 if (__predict_false(tx->queue_status == MVNETA_QUEUE_DISABLED)) 2815 return; 2816 2817 ptxs = MVNETA_READ(sc, MVNETA_PTXS(q)); 2818 ndesc = MVNETA_PTXS_GET_TBC(ptxs); 2819 2820 if (__predict_false(ndesc == 0)) { 2821 if (tx->used == 0) 2822 tx->queue_status = MVNETA_QUEUE_IDLE; 2823 else if (tx->queue_status == MVNETA_QUEUE_WORKING && 2824 ((ticks - tx->watchdog_time) > MVNETA_WATCHDOG)) 2825 tx->queue_hung = TRUE; 2826 return; 2827 } 2828 2829 #ifdef MVNETA_KTR 2830 CTR3(KTR_SPARE2, "%s:%u tx_complete begin ndesc=%u", 2831 sc->ifp->if_xname, q, ndesc); 2832 #endif 2833 2834 bus_dmamap_sync(sc->tx_dtag, tx->desc_map, 2835 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE); 2836 2837 for (i = 0; i < ndesc; i++) { 2838 t = &tx->desc[tx->dma]; 2839 #ifdef MVNETA_KTR 2840 if (t->flags & MVNETA_TX_F_ES) 2841 CTR3(KTR_SPARE2, "%s tx error queue %d desc %d", 2842 sc->ifp->if_xname, q, tx->dma); 2843 #endif 2844 txbuf = &tx->txbuf[tx->dma]; 2845 if (__predict_true(txbuf->m != NULL)) { 2846 DASSERT((t->command & MVNETA_TX_CMD_F) != 0); 2847 bus_dmamap_unload(sc->txmbuf_dtag, txbuf->dmap); 2848 m_freem(txbuf->m); 2849 txbuf->m = NULL; 2850 } 2851 else 2852 DASSERT((t->flags & MVNETA_TX_CMD_F) == 0); 2853 tx->dma = tx_counter_adv(tx->dma, 1); 2854 tx->used--; 2855 } 2856 DASSERT(tx->used >= 0); 2857 DASSERT(tx->used <= MVNETA_TX_RING_CNT); 2858 while (__predict_false(ndesc > 255)) { 2859 ptxsu = MVNETA_PTXSU_NORB(255); 2860 MVNETA_WRITE(sc, MVNETA_PTXSU(q), ptxsu); 2861 ndesc -= 255; 2862 } 2863 if (__predict_true(ndesc > 0)) { 2864 ptxsu = MVNETA_PTXSU_NORB(ndesc); 2865 MVNETA_WRITE(sc, MVNETA_PTXSU(q), ptxsu); 2866 } 2867 #ifdef MVNETA_KTR 2868 CTR5(KTR_SPARE2, "%s:%u tx_complete tx_cpu=%d tx_dma=%d tx_used=%d", 2869 sc->ifp->if_xname, q, tx->cpu, tx->dma, tx->used); 2870 #endif 2871 2872 tx->watchdog_time = ticks; 2873 2874 if (tx->used == 0) 2875 tx->queue_status = MVNETA_QUEUE_IDLE; 2876 } 2877 2878 /* 2879 * Do a final TX complete when TX is idle. 2880 */ 2881 STATIC void 2882 mvneta_tx_drain(struct mvneta_softc *sc) 2883 { 2884 struct mvneta_tx_ring *tx; 2885 int q; 2886 2887 /* 2888 * Handle trailing mbuf on TX queue. 2889 * Check is done lockess to avoid TX path contention. 2890 */ 2891 for (q = 0; q < MVNETA_TX_QNUM_MAX; q++) { 2892 tx = MVNETA_TX_RING(sc, q); 2893 if ((ticks - tx->watchdog_time) > MVNETA_WATCHDOG_TXCOMP && 2894 tx->used > 0) { 2895 mvneta_tx_lockq(sc, q); 2896 mvneta_tx_queue_complete(sc, q); 2897 mvneta_tx_unlockq(sc, q); 2898 } 2899 } 2900 } 2901 2902 /* 2903 * Rx Subroutines 2904 */ 2905 STATIC int 2906 mvneta_rx(struct mvneta_softc *sc, int q, int count) 2907 { 2908 uint32_t prxs, npkt; 2909 int more; 2910 2911 more = 0; 2912 mvneta_rx_lockq(sc, q); 2913 prxs = MVNETA_READ(sc, MVNETA_PRXS(q)); 2914 npkt = MVNETA_PRXS_GET_ODC(prxs); 2915 if (__predict_false(npkt == 0)) 2916 goto out; 2917 2918 if (count > 0 && npkt > count) { 2919 more = 1; 2920 npkt = count; 2921 } 2922 mvneta_rx_queue(sc, q, npkt); 2923 out: 2924 mvneta_rx_unlockq(sc, q); 2925 return more; 2926 } 2927 2928 /* 2929 * Helper routine for updating PRXSU register of a given queue. 2930 * Handles number of processed descriptors bigger than maximum acceptable value. 2931 */ 2932 STATIC __inline void 2933 mvneta_prxsu_update(struct mvneta_softc *sc, int q, int processed) 2934 { 2935 uint32_t prxsu; 2936 2937 while (__predict_false(processed > 255)) { 2938 prxsu = MVNETA_PRXSU_NOOFPROCESSEDDESCRIPTORS(255); 2939 MVNETA_WRITE(sc, MVNETA_PRXSU(q), prxsu); 2940 processed -= 255; 2941 } 2942 prxsu = MVNETA_PRXSU_NOOFPROCESSEDDESCRIPTORS(processed); 2943 MVNETA_WRITE(sc, MVNETA_PRXSU(q), prxsu); 2944 } 2945 2946 static __inline void 2947 mvneta_prefetch(void *p) 2948 { 2949 2950 __builtin_prefetch(p); 2951 } 2952 2953 STATIC void 2954 mvneta_rx_queue(struct mvneta_softc *sc, int q, int npkt) 2955 { 2956 struct ifnet *ifp; 2957 struct mvneta_rx_ring *rx; 2958 struct mvneta_rx_desc *r; 2959 struct mvneta_buf *rxbuf; 2960 struct mbuf *m; 2961 struct lro_ctrl *lro; 2962 struct lro_entry *queued; 2963 void *pktbuf; 2964 int i, pktlen, processed, ndma; 2965 2966 KASSERT_RX_MTX(sc, q); 2967 2968 ifp = sc->ifp; 2969 rx = MVNETA_RX_RING(sc, q); 2970 processed = 0; 2971 2972 if (__predict_false(rx->queue_status == MVNETA_QUEUE_DISABLED)) 2973 return; 2974 2975 bus_dmamap_sync(sc->rx_dtag, rx->desc_map, 2976 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE); 2977 2978 for (i = 0; i < npkt; i++) { 2979 /* Prefetch next desc, rxbuf. */ 2980 ndma = rx_counter_adv(rx->dma, 1); 2981 mvneta_prefetch(&rx->desc[ndma]); 2982 mvneta_prefetch(&rx->rxbuf[ndma]); 2983 2984 /* get descriptor and packet */ 2985 r = &rx->desc[rx->dma]; 2986 rxbuf = &rx->rxbuf[rx->dma]; 2987 m = rxbuf->m; 2988 rxbuf->m = NULL; 2989 DASSERT(m != NULL); 2990 bus_dmamap_sync(sc->rxbuf_dtag, rxbuf->dmap, 2991 BUS_DMASYNC_POSTREAD); 2992 bus_dmamap_unload(sc->rxbuf_dtag, rxbuf->dmap); 2993 /* Prefetch mbuf header. */ 2994 mvneta_prefetch(m); 2995 2996 processed++; 2997 /* Drop desc with error status or not in a single buffer. */ 2998 DASSERT((r->status & (MVNETA_RX_F|MVNETA_RX_L)) == 2999 (MVNETA_RX_F|MVNETA_RX_L)); 3000 if (__predict_false((r->status & MVNETA_RX_ES) || 3001 (r->status & (MVNETA_RX_F|MVNETA_RX_L)) != 3002 (MVNETA_RX_F|MVNETA_RX_L))) 3003 goto rx_error; 3004 3005 /* 3006 * [ OFF | MH | PKT | CRC ] 3007 * bytecnt cover MH, PKT, CRC 3008 */ 3009 pktlen = r->bytecnt - ETHER_CRC_LEN - MVNETA_HWHEADER_SIZE; 3010 pktbuf = (uint8_t *)r->bufptr_va + MVNETA_PACKET_OFFSET + 3011 MVNETA_HWHEADER_SIZE; 3012 3013 /* Prefetch mbuf data. */ 3014 mvneta_prefetch(pktbuf); 3015 3016 /* Write value to mbuf (avoid read). */ 3017 m->m_data = pktbuf; 3018 m->m_len = m->m_pkthdr.len = pktlen; 3019 m->m_pkthdr.rcvif = ifp; 3020 mvneta_rx_set_csumflag(ifp, r, m); 3021 3022 /* Increase rx_dma before releasing the lock. */ 3023 rx->dma = ndma; 3024 3025 if (__predict_false(rx->lro_enabled && 3026 ((r->status & MVNETA_RX_L3_IP) != 0) && 3027 ((r->status & MVNETA_RX_L4_MASK) == MVNETA_RX_L4_TCP) && 3028 (m->m_pkthdr.csum_flags & 3029 (CSUM_DATA_VALID | CSUM_PSEUDO_HDR)) == 3030 (CSUM_DATA_VALID | CSUM_PSEUDO_HDR))) { 3031 if (rx->lro.lro_cnt != 0) { 3032 if (tcp_lro_rx(&rx->lro, m, 0) == 0) 3033 goto rx_done; 3034 } 3035 } 3036 3037 mvneta_rx_unlockq(sc, q); 3038 (*ifp->if_input)(ifp, m); 3039 mvneta_rx_lockq(sc, q); 3040 /* 3041 * Check whether this queue has been disabled in the 3042 * meantime. If yes, then clear LRO and exit. 3043 */ 3044 if(__predict_false(rx->queue_status == MVNETA_QUEUE_DISABLED)) 3045 goto rx_lro; 3046 rx_done: 3047 /* Refresh receive ring to avoid stall and minimize jitter. */ 3048 if (processed >= MVNETA_RX_REFILL_COUNT) { 3049 mvneta_prxsu_update(sc, q, processed); 3050 mvneta_rx_queue_refill(sc, q); 3051 processed = 0; 3052 } 3053 continue; 3054 rx_error: 3055 m_freem(m); 3056 rx->dma = ndma; 3057 /* Refresh receive ring to avoid stall and minimize jitter. */ 3058 if (processed >= MVNETA_RX_REFILL_COUNT) { 3059 mvneta_prxsu_update(sc, q, processed); 3060 mvneta_rx_queue_refill(sc, q); 3061 processed = 0; 3062 } 3063 } 3064 #ifdef MVNETA_KTR 3065 CTR3(KTR_SPARE2, "%s:%u %u packets received", ifp->if_xname, q, npkt); 3066 #endif 3067 /* DMA status update */ 3068 mvneta_prxsu_update(sc, q, processed); 3069 /* Refill the rest of buffers if there are any to refill */ 3070 mvneta_rx_queue_refill(sc, q); 3071 3072 rx_lro: 3073 /* 3074 * Flush any outstanding LRO work 3075 */ 3076 lro = &rx->lro; 3077 while (__predict_false((queued = LIST_FIRST(&lro->lro_active)) != NULL)) { 3078 LIST_REMOVE(LIST_FIRST((&lro->lro_active)), next); 3079 tcp_lro_flush(lro, queued); 3080 } 3081 } 3082 3083 STATIC void 3084 mvneta_rx_buf_free(struct mvneta_softc *sc, struct mvneta_buf *rxbuf) 3085 { 3086 3087 bus_dmamap_unload(sc->rxbuf_dtag, rxbuf->dmap); 3088 /* This will remove all data at once */ 3089 m_freem(rxbuf->m); 3090 } 3091 3092 STATIC void 3093 mvneta_rx_queue_refill(struct mvneta_softc *sc, int q) 3094 { 3095 struct mvneta_rx_ring *rx; 3096 struct mvneta_rx_desc *r; 3097 struct mvneta_buf *rxbuf; 3098 bus_dma_segment_t segs; 3099 struct mbuf *m; 3100 uint32_t prxs, prxsu, ndesc; 3101 int npkt, refill, nsegs, error; 3102 3103 KASSERT_RX_MTX(sc, q); 3104 3105 rx = MVNETA_RX_RING(sc, q); 3106 prxs = MVNETA_READ(sc, MVNETA_PRXS(q)); 3107 ndesc = MVNETA_PRXS_GET_NODC(prxs) + MVNETA_PRXS_GET_ODC(prxs); 3108 refill = MVNETA_RX_RING_CNT - ndesc; 3109 #ifdef MVNETA_KTR 3110 CTR3(KTR_SPARE2, "%s:%u refill %u packets", sc->ifp->if_xname, q, 3111 refill); 3112 #endif 3113 if (__predict_false(refill <= 0)) 3114 return; 3115 3116 for (npkt = 0; npkt < refill; npkt++) { 3117 rxbuf = &rx->rxbuf[rx->cpu]; 3118 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 3119 if (__predict_false(m == NULL)) { 3120 error = ENOBUFS; 3121 break; 3122 } 3123 m->m_len = m->m_pkthdr.len = m->m_ext.ext_size; 3124 3125 error = bus_dmamap_load_mbuf_sg(sc->rxbuf_dtag, rxbuf->dmap, 3126 m, &segs, &nsegs, BUS_DMA_NOWAIT); 3127 if (__predict_false(error != 0 || nsegs != 1)) { 3128 KASSERT(1, ("Failed to load Rx mbuf DMA map")); 3129 m_freem(m); 3130 break; 3131 } 3132 3133 /* Add the packet to the ring */ 3134 rxbuf->m = m; 3135 r = &rx->desc[rx->cpu]; 3136 r->bufptr_pa = segs.ds_addr; 3137 r->bufptr_va = (uint32_t)m->m_data; 3138 3139 rx->cpu = rx_counter_adv(rx->cpu, 1); 3140 } 3141 if (npkt == 0) { 3142 if (refill == MVNETA_RX_RING_CNT) 3143 rx->needs_refill = TRUE; 3144 return; 3145 } 3146 3147 rx->needs_refill = FALSE; 3148 bus_dmamap_sync(sc->rx_dtag, rx->desc_map, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); 3149 3150 while (__predict_false(npkt > 255)) { 3151 prxsu = MVNETA_PRXSU_NOOFNEWDESCRIPTORS(255); 3152 MVNETA_WRITE(sc, MVNETA_PRXSU(q), prxsu); 3153 npkt -= 255; 3154 } 3155 if (__predict_true(npkt > 0)) { 3156 prxsu = MVNETA_PRXSU_NOOFNEWDESCRIPTORS(npkt); 3157 MVNETA_WRITE(sc, MVNETA_PRXSU(q), prxsu); 3158 } 3159 } 3160 3161 STATIC __inline void 3162 mvneta_rx_set_csumflag(struct ifnet *ifp, 3163 struct mvneta_rx_desc *r, struct mbuf *m) 3164 { 3165 uint32_t csum_flags; 3166 3167 csum_flags = 0; 3168 if (__predict_false((r->status & 3169 (MVNETA_RX_IP_HEADER_OK|MVNETA_RX_L3_IP)) == 0)) 3170 return; /* not a IP packet */ 3171 3172 /* L3 */ 3173 if (__predict_true((r->status & MVNETA_RX_IP_HEADER_OK) == 3174 MVNETA_RX_IP_HEADER_OK)) 3175 csum_flags |= CSUM_L3_CALC|CSUM_L3_VALID; 3176 3177 if (__predict_true((r->status & (MVNETA_RX_IP_HEADER_OK|MVNETA_RX_L3_IP)) == 3178 (MVNETA_RX_IP_HEADER_OK|MVNETA_RX_L3_IP))) { 3179 /* L4 */ 3180 switch (r->status & MVNETA_RX_L4_MASK) { 3181 case MVNETA_RX_L4_TCP: 3182 case MVNETA_RX_L4_UDP: 3183 csum_flags |= CSUM_L4_CALC; 3184 if (__predict_true((r->status & 3185 MVNETA_RX_L4_CHECKSUM_OK) == MVNETA_RX_L4_CHECKSUM_OK)) { 3186 csum_flags |= CSUM_L4_VALID; 3187 m->m_pkthdr.csum_data = htons(0xffff); 3188 } 3189 break; 3190 case MVNETA_RX_L4_OTH: 3191 default: 3192 break; 3193 } 3194 } 3195 m->m_pkthdr.csum_flags = csum_flags; 3196 } 3197 3198 /* 3199 * MAC address filter 3200 */ 3201 STATIC void 3202 mvneta_filter_setup(struct mvneta_softc *sc) 3203 { 3204 struct ifnet *ifp; 3205 uint32_t dfut[MVNETA_NDFUT], dfsmt[MVNETA_NDFSMT], dfomt[MVNETA_NDFOMT]; 3206 uint32_t pxc; 3207 int i; 3208 3209 KASSERT_SC_MTX(sc); 3210 3211 memset(dfut, 0, sizeof(dfut)); 3212 memset(dfsmt, 0, sizeof(dfsmt)); 3213 memset(dfomt, 0, sizeof(dfomt)); 3214 3215 ifp = sc->ifp; 3216 ifp->if_flags |= IFF_ALLMULTI; 3217 if (ifp->if_flags & (IFF_ALLMULTI|IFF_PROMISC)) { 3218 for (i = 0; i < MVNETA_NDFSMT; i++) { 3219 dfsmt[i] = dfomt[i] = 3220 MVNETA_DF(0, MVNETA_DF_QUEUE(0) | MVNETA_DF_PASS) | 3221 MVNETA_DF(1, MVNETA_DF_QUEUE(0) | MVNETA_DF_PASS) | 3222 MVNETA_DF(2, MVNETA_DF_QUEUE(0) | MVNETA_DF_PASS) | 3223 MVNETA_DF(3, MVNETA_DF_QUEUE(0) | MVNETA_DF_PASS); 3224 } 3225 } 3226 3227 pxc = MVNETA_READ(sc, MVNETA_PXC); 3228 pxc &= ~(MVNETA_PXC_UPM | MVNETA_PXC_RXQ_MASK | MVNETA_PXC_RXQARP_MASK | 3229 MVNETA_PXC_TCPQ_MASK | MVNETA_PXC_UDPQ_MASK | MVNETA_PXC_BPDUQ_MASK); 3230 pxc |= MVNETA_PXC_RXQ(MVNETA_RX_QNUM_MAX-1); 3231 pxc |= MVNETA_PXC_RXQARP(MVNETA_RX_QNUM_MAX-1); 3232 pxc |= MVNETA_PXC_TCPQ(MVNETA_RX_QNUM_MAX-1); 3233 pxc |= MVNETA_PXC_UDPQ(MVNETA_RX_QNUM_MAX-1); 3234 pxc |= MVNETA_PXC_BPDUQ(MVNETA_RX_QNUM_MAX-1); 3235 pxc |= MVNETA_PXC_RB | MVNETA_PXC_RBIP | MVNETA_PXC_RBARP; 3236 if (ifp->if_flags & IFF_BROADCAST) { 3237 pxc &= ~(MVNETA_PXC_RB | MVNETA_PXC_RBIP | MVNETA_PXC_RBARP); 3238 } 3239 if (ifp->if_flags & IFF_PROMISC) { 3240 pxc |= MVNETA_PXC_UPM; 3241 } 3242 MVNETA_WRITE(sc, MVNETA_PXC, pxc); 3243 3244 /* Set Destination Address Filter Unicast Table */ 3245 if (ifp->if_flags & IFF_PROMISC) { 3246 /* pass all unicast addresses */ 3247 for (i = 0; i < MVNETA_NDFUT; i++) { 3248 dfut[i] = 3249 MVNETA_DF(0, MVNETA_DF_QUEUE(0) | MVNETA_DF_PASS) | 3250 MVNETA_DF(1, MVNETA_DF_QUEUE(0) | MVNETA_DF_PASS) | 3251 MVNETA_DF(2, MVNETA_DF_QUEUE(0) | MVNETA_DF_PASS) | 3252 MVNETA_DF(3, MVNETA_DF_QUEUE(0) | MVNETA_DF_PASS); 3253 } 3254 } else { 3255 i = sc->enaddr[5] & 0xf; /* last nibble */ 3256 dfut[i>>2] = MVNETA_DF(i&3, MVNETA_DF_QUEUE(0) | MVNETA_DF_PASS); 3257 } 3258 MVNETA_WRITE_REGION(sc, MVNETA_DFUT(0), dfut, MVNETA_NDFUT); 3259 3260 /* Set Destination Address Filter Multicast Tables */ 3261 MVNETA_WRITE_REGION(sc, MVNETA_DFSMT(0), dfsmt, MVNETA_NDFSMT); 3262 MVNETA_WRITE_REGION(sc, MVNETA_DFOMT(0), dfomt, MVNETA_NDFOMT); 3263 } 3264 3265 /* 3266 * sysctl(9) 3267 */ 3268 STATIC int 3269 sysctl_read_mib(SYSCTL_HANDLER_ARGS) 3270 { 3271 struct mvneta_sysctl_mib *arg; 3272 struct mvneta_softc *sc; 3273 uint64_t val; 3274 3275 arg = (struct mvneta_sysctl_mib *)arg1; 3276 if (arg == NULL) 3277 return (EINVAL); 3278 3279 sc = arg->sc; 3280 if (sc == NULL) 3281 return (EINVAL); 3282 if (arg->index < 0 || arg->index > MVNETA_PORTMIB_NOCOUNTER) 3283 return (EINVAL); 3284 3285 mvneta_sc_lock(sc); 3286 val = arg->counter; 3287 mvneta_sc_unlock(sc); 3288 return sysctl_handle_64(oidp, &val, 0, req); 3289 } 3290 3291 3292 STATIC int 3293 sysctl_clear_mib(SYSCTL_HANDLER_ARGS) 3294 { 3295 struct mvneta_softc *sc; 3296 int err, val; 3297 3298 val = 0; 3299 sc = (struct mvneta_softc *)arg1; 3300 if (sc == NULL) 3301 return (EINVAL); 3302 3303 err = sysctl_handle_int(oidp, &val, 0, req); 3304 if (err != 0) 3305 return (err); 3306 3307 if (val < 0 || val > 1) 3308 return (EINVAL); 3309 3310 if (val == 1) { 3311 mvneta_sc_lock(sc); 3312 mvneta_clear_mib(sc); 3313 mvneta_sc_unlock(sc); 3314 } 3315 3316 return (0); 3317 } 3318 3319 STATIC int 3320 sysctl_set_queue_rxthtime(SYSCTL_HANDLER_ARGS) 3321 { 3322 struct mvneta_sysctl_queue *arg; 3323 struct mvneta_rx_ring *rx; 3324 struct mvneta_softc *sc; 3325 uint32_t reg, time_mvtclk; 3326 int err, time_us; 3327 3328 rx = NULL; 3329 arg = (struct mvneta_sysctl_queue *)arg1; 3330 if (arg == NULL) 3331 return (EINVAL); 3332 if (arg->queue < 0 || arg->queue > MVNETA_RX_RING_CNT) 3333 return (EINVAL); 3334 if (arg->rxtx != MVNETA_SYSCTL_RX) 3335 return (EINVAL); 3336 3337 sc = arg->sc; 3338 if (sc == NULL) 3339 return (EINVAL); 3340 3341 /* read queue length */ 3342 mvneta_sc_lock(sc); 3343 mvneta_rx_lockq(sc, arg->queue); 3344 rx = MVNETA_RX_RING(sc, arg->queue); 3345 time_mvtclk = rx->queue_th_time; 3346 time_us = ((uint64_t)time_mvtclk * 1000ULL * 1000ULL) / get_tclk(); 3347 mvneta_rx_unlockq(sc, arg->queue); 3348 mvneta_sc_unlock(sc); 3349 3350 err = sysctl_handle_int(oidp, &time_us, 0, req); 3351 if (err != 0) 3352 return (err); 3353 3354 mvneta_sc_lock(sc); 3355 mvneta_rx_lockq(sc, arg->queue); 3356 3357 /* update queue length (0[sec] - 1[sec]) */ 3358 if (time_us < 0 || time_us > (1000 * 1000)) { 3359 mvneta_rx_unlockq(sc, arg->queue); 3360 mvneta_sc_unlock(sc); 3361 return (EINVAL); 3362 } 3363 time_mvtclk = 3364 (uint64_t)get_tclk() * (uint64_t)time_us / (1000ULL * 1000ULL); 3365 rx->queue_th_time = time_mvtclk; 3366 reg = MVNETA_PRXITTH_RITT(rx->queue_th_time); 3367 MVNETA_WRITE(sc, MVNETA_PRXITTH(arg->queue), reg); 3368 mvneta_rx_unlockq(sc, arg->queue); 3369 mvneta_sc_unlock(sc); 3370 3371 return (0); 3372 } 3373 3374 STATIC void 3375 sysctl_mvneta_init(struct mvneta_softc *sc) 3376 { 3377 struct sysctl_ctx_list *ctx; 3378 struct sysctl_oid_list *children; 3379 struct sysctl_oid_list *rxchildren; 3380 struct sysctl_oid_list *qchildren, *mchildren; 3381 struct sysctl_oid *tree; 3382 int i, q; 3383 struct mvneta_sysctl_queue *rxarg; 3384 #define MVNETA_SYSCTL_NAME(num) "queue" # num 3385 static const char *sysctl_queue_names[] = { 3386 MVNETA_SYSCTL_NAME(0), MVNETA_SYSCTL_NAME(1), 3387 MVNETA_SYSCTL_NAME(2), MVNETA_SYSCTL_NAME(3), 3388 MVNETA_SYSCTL_NAME(4), MVNETA_SYSCTL_NAME(5), 3389 MVNETA_SYSCTL_NAME(6), MVNETA_SYSCTL_NAME(7), 3390 }; 3391 #undef MVNETA_SYSCTL_NAME 3392 3393 #define MVNETA_SYSCTL_DESCR(num) "configuration parameters for queue " # num 3394 static const char *sysctl_queue_descrs[] = { 3395 MVNETA_SYSCTL_DESCR(0), MVNETA_SYSCTL_DESCR(1), 3396 MVNETA_SYSCTL_DESCR(2), MVNETA_SYSCTL_DESCR(3), 3397 MVNETA_SYSCTL_DESCR(4), MVNETA_SYSCTL_DESCR(5), 3398 MVNETA_SYSCTL_DESCR(6), MVNETA_SYSCTL_DESCR(7), 3399 }; 3400 #undef MVNETA_SYSCTL_DESCR 3401 3402 3403 ctx = device_get_sysctl_ctx(sc->dev); 3404 children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)); 3405 3406 tree = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "rx", 3407 CTLFLAG_RD, 0, "NETA RX"); 3408 rxchildren = SYSCTL_CHILDREN(tree); 3409 tree = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "mib", 3410 CTLFLAG_RD, 0, "NETA MIB"); 3411 mchildren = SYSCTL_CHILDREN(tree); 3412 3413 3414 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "flow_control", 3415 CTLFLAG_RW, &sc->cf_fc, 0, "flow control"); 3416 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "lpi", 3417 CTLFLAG_RW, &sc->cf_lpi, 0, "Low Power Idle"); 3418 3419 /* 3420 * MIB access 3421 */ 3422 /* dev.mvneta.[unit].mib.<mibs> */ 3423 for (i = 0; i < MVNETA_PORTMIB_NOCOUNTER; i++) { 3424 const char *name = mvneta_mib_list[i].sysctl_name; 3425 const char *desc = mvneta_mib_list[i].desc; 3426 struct mvneta_sysctl_mib *mib_arg = &sc->sysctl_mib[i]; 3427 3428 mib_arg->sc = sc; 3429 mib_arg->index = i; 3430 SYSCTL_ADD_PROC(ctx, mchildren, OID_AUTO, name, 3431 CTLTYPE_U64|CTLFLAG_RD, (void *)mib_arg, 0, 3432 sysctl_read_mib, "I", desc); 3433 } 3434 SYSCTL_ADD_UQUAD(ctx, mchildren, OID_AUTO, "rx_discard", 3435 CTLFLAG_RD, &sc->counter_pdfc, "Port Rx Discard Frame Counter"); 3436 SYSCTL_ADD_UQUAD(ctx, mchildren, OID_AUTO, "overrun", 3437 CTLFLAG_RD, &sc->counter_pofc, "Port Overrun Frame Counter"); 3438 SYSCTL_ADD_UINT(ctx, mchildren, OID_AUTO, "watchdog", 3439 CTLFLAG_RD, &sc->counter_watchdog, 0, "TX Watchdog Counter"); 3440 3441 SYSCTL_ADD_PROC(ctx, mchildren, OID_AUTO, "reset", 3442 CTLTYPE_INT|CTLFLAG_RW, (void *)sc, 0, 3443 sysctl_clear_mib, "I", "Reset MIB counters"); 3444 3445 for (q = 0; q < MVNETA_RX_QNUM_MAX; q++) { 3446 rxarg = &sc->sysctl_rx_queue[q]; 3447 3448 rxarg->sc = sc; 3449 rxarg->queue = q; 3450 rxarg->rxtx = MVNETA_SYSCTL_RX; 3451 3452 /* hw.mvneta.mvneta[unit].rx.[queue] */ 3453 tree = SYSCTL_ADD_NODE(ctx, rxchildren, OID_AUTO, 3454 sysctl_queue_names[q], CTLFLAG_RD, 0, 3455 sysctl_queue_descrs[q]); 3456 qchildren = SYSCTL_CHILDREN(tree); 3457 3458 /* hw.mvneta.mvneta[unit].rx.[queue].threshold_timer_us */ 3459 SYSCTL_ADD_PROC(ctx, qchildren, OID_AUTO, "threshold_timer_us", 3460 CTLTYPE_UINT | CTLFLAG_RW, rxarg, 0, 3461 sysctl_set_queue_rxthtime, "I", 3462 "interrupt coalescing threshold timer [us]"); 3463 } 3464 } 3465 3466 /* 3467 * MIB 3468 */ 3469 STATIC void 3470 mvneta_clear_mib(struct mvneta_softc *sc) 3471 { 3472 int i; 3473 3474 KASSERT_SC_MTX(sc); 3475 3476 for (i = 0; i < nitems(mvneta_mib_list); i++) { 3477 if (mvneta_mib_list[i].reg64) 3478 MVNETA_READ_MIB_8(sc, mvneta_mib_list[i].regnum); 3479 else 3480 MVNETA_READ_MIB_4(sc, mvneta_mib_list[i].regnum); 3481 sc->sysctl_mib[i].counter = 0; 3482 } 3483 MVNETA_READ(sc, MVNETA_PDFC); 3484 sc->counter_pdfc = 0; 3485 MVNETA_READ(sc, MVNETA_POFC); 3486 sc->counter_pofc = 0; 3487 sc->counter_watchdog = 0; 3488 } 3489 3490 STATIC void 3491 mvneta_update_mib(struct mvneta_softc *sc) 3492 { 3493 struct mvneta_tx_ring *tx; 3494 int i; 3495 uint64_t val; 3496 uint32_t reg; 3497 3498 for (i = 0; i < nitems(mvneta_mib_list); i++) { 3499 3500 if (mvneta_mib_list[i].reg64) 3501 val = MVNETA_READ_MIB_8(sc, mvneta_mib_list[i].regnum); 3502 else 3503 val = MVNETA_READ_MIB_4(sc, mvneta_mib_list[i].regnum); 3504 3505 if (val == 0) 3506 continue; 3507 3508 sc->sysctl_mib[i].counter += val; 3509 switch (mvneta_mib_list[i].regnum) { 3510 case MVNETA_MIB_RX_GOOD_OCT: 3511 if_inc_counter(sc->ifp, IFCOUNTER_IBYTES, val); 3512 break; 3513 case MVNETA_MIB_RX_BAD_FRAME: 3514 if_inc_counter(sc->ifp, IFCOUNTER_IERRORS, val); 3515 break; 3516 case MVNETA_MIB_RX_GOOD_FRAME: 3517 if_inc_counter(sc->ifp, IFCOUNTER_IPACKETS, val); 3518 break; 3519 case MVNETA_MIB_RX_MCAST_FRAME: 3520 if_inc_counter(sc->ifp, IFCOUNTER_IMCASTS, val); 3521 break; 3522 case MVNETA_MIB_TX_GOOD_OCT: 3523 if_inc_counter(sc->ifp, IFCOUNTER_OBYTES, val); 3524 break; 3525 case MVNETA_MIB_TX_GOOD_FRAME: 3526 if_inc_counter(sc->ifp, IFCOUNTER_OPACKETS, val); 3527 break; 3528 case MVNETA_MIB_TX_MCAST_FRAME: 3529 if_inc_counter(sc->ifp, IFCOUNTER_OMCASTS, val); 3530 break; 3531 case MVNETA_MIB_MAC_COL: 3532 if_inc_counter(sc->ifp, IFCOUNTER_COLLISIONS, val); 3533 break; 3534 case MVNETA_MIB_TX_MAC_TRNS_ERR: 3535 case MVNETA_MIB_TX_EXCES_COL: 3536 case MVNETA_MIB_MAC_LATE_COL: 3537 if_inc_counter(sc->ifp, IFCOUNTER_OERRORS, val); 3538 break; 3539 } 3540 } 3541 3542 reg = MVNETA_READ(sc, MVNETA_PDFC); 3543 sc->counter_pdfc += reg; 3544 if_inc_counter(sc->ifp, IFCOUNTER_IQDROPS, reg); 3545 reg = MVNETA_READ(sc, MVNETA_POFC); 3546 sc->counter_pofc += reg; 3547 if_inc_counter(sc->ifp, IFCOUNTER_IQDROPS, reg); 3548 3549 /* TX watchdog. */ 3550 if (sc->counter_watchdog_mib > 0 ) { 3551 if_inc_counter(sc->ifp, IFCOUNTER_OERRORS, sc->counter_watchdog_mib); 3552 sc->counter_watchdog_mib = 0; 3553 } 3554 /* 3555 * TX driver errors: 3556 * We do not take queue locks to not disrupt TX path. 3557 * We may only miss one drv error which will be fixed at 3558 * next mib update. We may also clear counter when TX path 3559 * is incrementing it but we only do it if counter was not zero 3560 * thus we may only loose one error. 3561 */ 3562 for (i = 0; i < MVNETA_TX_QNUM_MAX; i++) { 3563 tx = MVNETA_TX_RING(sc, i); 3564 3565 if (tx->drv_error > 0) { 3566 if_inc_counter(sc->ifp, IFCOUNTER_OERRORS, tx->drv_error); 3567 tx->drv_error = 0; 3568 } 3569 } 3570 } 3571