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