diff --git a/sys/arm64/conf/dpaa/files.dpaa_arm64 b/sys/arm64/conf/dpaa/files.dpaa_arm64 index 7dd2ee83ee9..1872254322a 100644 --- a/sys/arm64/conf/dpaa/files.dpaa_arm64 +++ b/sys/arm64/conf/dpaa/files.dpaa_arm64 @@ -111,3 +111,15 @@ dev/dpaa/fman_chardev.c optional dpaa fdt \ no-depend compile-with "${FMCD_COMPILE_CMD}" dev/dpaa/fman_fdt.c optional dpaa fdt \ no-depend compile-with "${DPAA_COMPILE_CMD}" +# mEMAC external MDIO controller (fsl,fman-memac-mdio) +dev/dpaa/memac_mdio.c optional dpaa fdt \ + no-depend compile-with "${DPAA_COMPILE_CMD}" +# Ethernet driver (reuses dTSEC wrapper with mEMAC via NCSW FM_MAC API) +dev/dpaa/if_dtsec.c optional dpaa fdt \ + no-depend compile-with "${DPAA_COMPILE_CMD}" +dev/dpaa/if_dtsec_fdt.c optional dpaa fdt \ + no-depend compile-with "${DPAA_COMPILE_CMD}" +dev/dpaa/if_dtsec_im.c optional dpaa fdt \ + no-depend compile-with "${DPAA_COMPILE_CMD}" +dev/dpaa/if_dtsec_rm.c optional dpaa fdt \ + no-depend compile-with "${DPAA_COMPILE_CMD}" diff --git a/sys/dev/dpaa/if_dtsec.c b/sys/dev/dpaa/if_dtsec.c index dfa76a47662..e98e2321f3e 100644 --- a/sys/dev/dpaa/if_dtsec.c +++ b/sys/dev/dpaa/if_dtsec.c @@ -55,12 +55,23 @@ #include "miibus_if.h" +#include "opt_dpaa.h" + #include #include #include +#include +#if (DPAA_VERSION < 11) +#include +#endif #include #include "fman.h" +#include "bman.h" + +/* FM port internal header — needed for RCCB register access in stall diag */ +#include +#include "qman.h" #include "if_dtsec.h" #include "if_dtsec_im.h" #include "if_dtsec_rm.h" @@ -68,7 +79,12 @@ #define DTSEC_MIN_FRAME_SIZE 64 #define DTSEC_MAX_FRAME_SIZE 9600 +#if (DPAA_VERSION < 11) #define DTSEC_REG_MAXFRM 0x110 +#define DTSEC_REG_GADDR(i) (0x0a0 + 4*(i)) +#else +#define MEMAC_REG_MAXFRM 0x014 +#endif /** * @group dTSEC private defines. @@ -156,7 +172,7 @@ dtsec_fm_mac_mdio_event_callback(t_Handle h_App, struct dtsec_softc *sc; sc = h_App; - device_printf(sc->sc_dev, "MDIO event %i: %s.\n", exception, + if_printf(sc->sc_ifnet, "MDIO event %i: %s.\n", exception, dtsec_fm_mac_ex_to_str(exception)); } @@ -166,7 +182,7 @@ dtsec_fm_mac_exception_callback(t_Handle app, e_FmMacExceptions exception) struct dtsec_softc *sc; sc = app; - device_printf(sc->sc_dev, "MAC exception %i: %s.\n", exception, + if_printf(sc->sc_ifnet, "MAC exception %i: %s.\n", exception, dtsec_fm_mac_ex_to_str(exception)); } @@ -214,7 +230,9 @@ dtsec_fm_mac_init(struct dtsec_softc *sc, uint8_t *mac) return (ENXIO); } - /* Do not inform about pause frames */ +#if (DPAA_VERSION < 11) + /* Do not inform about pause frames (dTSEC only; MEMAC has no + * equivalent interrupt — it simply never generates this event). */ error = FM_MAC_ConfigException(sc->sc_mach, e_FM_MAC_EX_1G_RX_CTL, FALSE); if (error != E_OK) { @@ -223,6 +241,7 @@ dtsec_fm_mac_init(struct dtsec_softc *sc, uint8_t *mac) dtsec_fm_mac_free(sc); return (ENXIO); } +#endif error = FM_MAC_Init(sc->sc_mach); if (error != E_OK) { @@ -232,6 +251,18 @@ dtsec_fm_mac_init(struct dtsec_softc *sc, uint8_t *mac) return (ENXIO); } + /* Diagnostic: dump MEMAC IF_MODE and IF_STATUS registers */ + { + uint32_t ifmode = be32toh(bus_read_4(sc->sc_mem, 0x300)); + uint32_t ifstat = be32toh(bus_read_4(sc->sc_mem, 0x304)); + device_printf(sc->sc_dev, + "MEMAC IF_MODE=0x%08x IF_STATUS=0x%08x mode=%s\n", + ifmode, ifstat, + (ifmode & 0x3) == 0 ? "XGMII(10G)" : + (ifmode & 0x3) == 2 ? "GMII(1G)" : + (ifmode & 0x3) == 4 ? "RGMII" : "unknown"); + } + return (0); } /** @} */ @@ -260,7 +291,7 @@ dtsec_fm_port_rx_exception_callback(t_Handle app, struct dtsec_softc *sc; sc = app; - device_printf(sc->sc_dev, "RX exception: %i: %s.\n", exception, + if_printf(sc->sc_ifnet, "RX exception: %i: %s.\n", exception, dtsec_fm_port_ex_to_str(exception)); } @@ -271,7 +302,7 @@ dtsec_fm_port_tx_exception_callback(t_Handle app, struct dtsec_softc *sc; sc = app; - device_printf(sc->sc_dev, "TX exception: %i: %s.\n", exception, + if_printf(sc->sc_ifnet, "TX exception: %i: %s.\n", exception, dtsec_fm_port_ex_to_str(exception)); } @@ -305,6 +336,11 @@ dtsec_fm_port_tx_type(enum eth_dev_type type) static void dtsec_fm_port_free_both(struct dtsec_softc *sc) { + if (sc->sc_vsph) { + FM_VSP_Free(sc->sc_vsph); + sc->sc_vsph = NULL; + } + if (sc->sc_rxph) { FM_PORT_Free(sc->sc_rxph); sc->sc_rxph = NULL; @@ -331,13 +367,50 @@ dtsec_set_mtu(struct dtsec_softc *sc, unsigned int mtu) DTSEC_LOCK_ASSERT(sc); if (mtu >= DTSEC_MIN_FRAME_SIZE && mtu <= DTSEC_MAX_FRAME_SIZE) { +#if (DPAA_VERSION < 11) bus_write_4(sc->sc_mem, DTSEC_REG_MAXFRM, mtu); +#else + bus_write_4(sc->sc_mem, MEMAC_REG_MAXFRM, + htobe32((uint32_t)mtu)); +#endif return (mtu); } return (0); } +static u_int +dtsec_hash_maddr(void *arg, struct sockaddr_dl *sdl, u_int cnt) +{ + struct dtsec_softc *sc = arg; + + FM_MAC_AddHashMacAddr(sc->sc_mach, (t_EnetAddr *)LLADDR(sdl)); + + return (1); +} + +static void +dtsec_setup_multicast(struct dtsec_softc *sc) +{ +#if (DPAA_VERSION < 11) + int i; + + if (if_getflags(sc->sc_ifnet) & IFF_ALLMULTI) { + for (i = 0; i < 8; i++) + bus_write_4(sc->sc_mem, DTSEC_REG_GADDR(i), 0xFFFFFFFF); + + return; + } + + fman_dtsec_reset_filter_table(rman_get_virtual(sc->sc_mem), + true, false); +#else + /* mEMAC: no hash reset API — SetPromiscuous(false) clears filter */ + FM_MAC_SetPromiscuous(sc->sc_mach, false); +#endif + if_foreach_llmaddr(sc->sc_ifnet, dtsec_hash_maddr, sc); +} + static int dtsec_if_enable_locked(struct dtsec_softc *sc) { @@ -357,6 +430,8 @@ dtsec_if_enable_locked(struct dtsec_softc *sc) if (error != E_OK) return (EIO); + dtsec_setup_multicast(sc); + if_setdrvflagbits(sc->sc_ifnet, IFF_DRV_RUNNING, 0); /* Refresh link state */ @@ -423,10 +498,39 @@ dtsec_if_ioctl(if_t ifp, u_long command, caddr_t data) case SIOCGIFMEDIA: case SIOCSIFMEDIA: - error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii->mii_media, - command); + if (sc->sc_mii != NULL) + error = ifmedia_ioctl(ifp, ifr, + &sc->sc_mii->mii_media, command); + else + error = ENOTTY; break; + case SIOCSIFCAP: { + int mask; + + mask = ifr->ifr_reqcap ^ if_getcapenable(ifp); + + if (mask & IFCAP_TXCSUM) { + if_togglecapenable(ifp, IFCAP_TXCSUM); + if (if_getcapenable(ifp) & IFCAP_TXCSUM) + if_sethwassist(ifp, if_gethwassist(ifp) | + (CSUM_IP | CSUM_IP_TCP | CSUM_IP_UDP)); + else + if_sethwassist(ifp, if_gethwassist(ifp) & + ~(CSUM_IP | CSUM_IP_TCP | CSUM_IP_UDP)); + } + if (mask & IFCAP_TXCSUM_IPV6) { + if_togglecapenable(ifp, IFCAP_TXCSUM_IPV6); + if (if_getcapenable(ifp) & IFCAP_TXCSUM_IPV6) + if_sethwassist(ifp, if_gethwassist(ifp) | + (CSUM_IP6_TCP | CSUM_IP6_UDP)); + else + if_sethwassist(ifp, if_gethwassist(ifp) & + ~(CSUM_IP6_TCP | CSUM_IP6_UDP)); + } + break; + } + default: error = ether_ioctl(ifp, command, data); } @@ -441,10 +545,11 @@ dtsec_if_tick(void *arg) sc = arg; - /* TODO */ DTSEC_LOCK(sc); - mii_tick(sc->sc_mii); + if (sc->sc_mii != NULL) + mii_tick(sc->sc_mii); + callout_reset(&sc->sc_tick_callout, hz, dtsec_if_tick, sc); DTSEC_UNLOCK(sc); @@ -472,13 +577,12 @@ dtsec_if_init_locked(struct dtsec_softc *sc) error = FM_MAC_ModifyMacAddr(sc->sc_mach, (t_EnetAddr *)if_getlladdr(sc->sc_ifnet)); if (error != E_OK) { - device_printf(sc->sc_dev, "couldn't set MAC address.\n"); + if_printf(sc->sc_ifnet, "couldn't set MAC address.\n"); goto err; } - /* Start MII polling */ - if (sc->sc_mii) - callout_reset(&sc->sc_tick_callout, hz, dtsec_if_tick, sc); + /* Start MII polling / periodic diagnostics */ + callout_reset(&sc->sc_tick_callout, hz, dtsec_if_tick, sc); if (if_getflags(sc->sc_ifnet) & IFF_UP) { error = dtsec_if_enable_locked(sc); @@ -494,7 +598,7 @@ dtsec_if_init_locked(struct dtsec_softc *sc) err: dtsec_if_deinit_locked(sc); - device_printf(sc->sc_dev, "initialization error.\n"); + if_printf(sc->sc_ifnet, "initialization error.\n"); return; } @@ -521,6 +625,12 @@ dtsec_if_start(if_t ifp) DTSEC_UNLOCK(sc); } +static void +dtsec_if_qflush(if_t ifp) +{ + /* No software queue — nothing to flush */ +} + static void dtsec_if_watchdog(if_t ifp) { @@ -539,7 +649,8 @@ dtsec_ifmedia_upd(if_t ifp) struct dtsec_softc *sc = if_getsoftc(ifp); DTSEC_LOCK(sc); - mii_mediachg(sc->sc_mii); + if (sc->sc_mii != NULL) + mii_mediachg(sc->sc_mii); DTSEC_UNLOCK(sc); return (0); @@ -552,10 +663,15 @@ dtsec_ifmedia_sts(if_t ifp, struct ifmediareq *ifmr) DTSEC_LOCK(sc); - mii_pollstat(sc->sc_mii); - - ifmr->ifm_active = sc->sc_mii->mii_media_active; - ifmr->ifm_status = sc->sc_mii->mii_media_status; + if (sc->sc_mii != NULL) { + mii_pollstat(sc->sc_mii); + ifmr->ifm_active = sc->sc_mii->mii_media_active; + ifmr->ifm_status = sc->sc_mii->mii_media_status; + } else { + /* 10G port with no PHY — report fixed 10G media, always up */ + ifmr->ifm_active = IFM_ETHER | IFM_10G_SR | IFM_FDX; + ifmr->ifm_status = IFM_AVALID | IFM_ACTIVE; + } DTSEC_UNLOCK(sc); } @@ -580,7 +696,7 @@ dtsec_configure_mode(struct dtsec_softc *sc) if (sc->sc_mode == DTSEC_MODE_REGULAR) { sc->sc_port_rx_init = dtsec_rm_fm_port_rx_init; sc->sc_port_tx_init = dtsec_rm_fm_port_tx_init; - sc->sc_start_locked = dtsec_rm_if_start_locked; + /* RM uses if_transmit, no sc_start_locked needed */ } else { sc->sc_port_rx_init = dtsec_im_fm_port_rx_init; sc->sc_port_tx_init = dtsec_im_fm_port_tx_init; @@ -591,6 +707,129 @@ dtsec_configure_mode(struct dtsec_softc *sc) (sc->sc_mode == DTSEC_MODE_REGULAR) ? "regular" : "independent"); } +static int +dtsec_sysctl_bman_free(SYSCTL_HANDLER_ARGS) +{ + struct dtsec_softc *sc = (struct dtsec_softc *)arg1; + uint32_t count; + + if (sc->sc_rx_pool == NULL) + count = 0; + else + count = bman_count(sc->sc_rx_pool); + + return (sysctl_handle_int(oidp, &count, 0, req)); +} + +static int +dtsec_sysctl_diag(SYSCTL_HANDLER_ARGS) +{ + struct dtsec_softc *sc = (struct dtsec_softc *)arg1; + int val = 0; + int error; + + error = sysctl_handle_int(oidp, &val, 0, req); + if (error || req->newptr == NULL) + return (error); + + if (val != 0) { + uint32_t bcount = sc->sc_rx_pool ? + bman_count(sc->sc_rx_pool) : 0; + printf("%s: DIAG bman_free=%u bman_total=%u bpid=%u " + "rx_fqid=%u\n", + if_name(sc->sc_ifnet), bcount, + sc->sc_rx_buf_total, sc->sc_rx_bpid, + sc->sc_rx_fqr[0] ? qman_fqr_get_base_fqid(sc->sc_rx_fqr[0]) : 0); + qman_portal_dqrr_diag(); + if (sc->sc_rxph != NULL) { + printf("%s: FMan RX: frame=%u discard=%u " + "bad=%u filter=%u oobd=%u enq=%u\n", + if_name(sc->sc_ifnet), + FM_PORT_GetCounter(sc->sc_rxph, + e_FM_PORT_COUNTERS_FRAME), + FM_PORT_GetCounter(sc->sc_rxph, + e_FM_PORT_COUNTERS_DISCARD_FRAME), + FM_PORT_GetCounter(sc->sc_rxph, + e_FM_PORT_COUNTERS_RX_BAD_FRAME), + FM_PORT_GetCounter(sc->sc_rxph, + e_FM_PORT_COUNTERS_RX_FILTER_FRAME), + FM_PORT_GetCounter(sc->sc_rxph, + e_FM_PORT_COUNTERS_RX_OUT_OF_BUFFERS_DISCARD), + FM_PORT_GetCounter(sc->sc_rxph, + e_FM_PORT_COUNTERS_ENQ_TOTAL)); + } + if (sc->sc_txph != NULL) { + printf("%s: FMan TX: frame=%u discard=%u " + "len_err=%u unsup=%u deq=%u deq_dflt=%u " + "deq_conf=%u\n", + if_name(sc->sc_ifnet), + FM_PORT_GetCounter(sc->sc_txph, + e_FM_PORT_COUNTERS_FRAME), + FM_PORT_GetCounter(sc->sc_txph, + e_FM_PORT_COUNTERS_DISCARD_FRAME), + FM_PORT_GetCounter(sc->sc_txph, + e_FM_PORT_COUNTERS_LENGTH_ERR), + FM_PORT_GetCounter(sc->sc_txph, + e_FM_PORT_COUNTERS_UNSUPPRTED_FORMAT), + FM_PORT_GetCounter(sc->sc_txph, + e_FM_PORT_COUNTERS_DEQ_TOTAL), + FM_PORT_GetCounter(sc->sc_txph, + e_FM_PORT_COUNTERS_DEQ_FROM_DEFAULT), + FM_PORT_GetCounter(sc->sc_txph, + e_FM_PORT_COUNTERS_DEQ_CONFIRM)); + } + if (sc->sc_mach != NULL) { + t_FmMacStatistics ms; + if (FM_MAC_GetStatistics(sc->sc_mach, &ms) == E_OK) { + printf("%s: MAC: rxPkts=%llu rxBytes=%llu " + "rxDiscard=%llu rxErr=%llu " + "txPkts=%llu txBytes=%llu " + "txErr=%llu\n", + if_name(sc->sc_ifnet), + (unsigned long long)ms.ifInPkts, + (unsigned long long)ms.ifInOctets, + (unsigned long long)ms.ifInDiscards, + (unsigned long long)ms.ifInErrors, + (unsigned long long)ms.ifOutPkts, + (unsigned long long)ms.ifOutOctets, + (unsigned long long)ms.ifOutErrors); + } + } + } + + return (0); +} + +static uint64_t +dtsec_get_counter(if_t ifp, ift_counter cnt) +{ + struct dtsec_softc *sc = if_getsoftc(ifp); + t_FmMacStatistics ms; + + if (sc->sc_mach != NULL && + FM_MAC_GetStatistics(sc->sc_mach, &ms) == E_OK) { + switch (cnt) { + case IFCOUNTER_IPACKETS: + return (ms.ifInPkts); + case IFCOUNTER_OPACKETS: + return (ms.ifOutPkts); + case IFCOUNTER_IBYTES: + return (ms.ifInOctets); + case IFCOUNTER_OBYTES: + return (ms.ifOutOctets); + case IFCOUNTER_IERRORS: + return (ms.ifInErrors); + case IFCOUNTER_OERRORS: + return (ms.ifOutErrors); + case IFCOUNTER_IMCASTS: + return (ms.ifInMcastPkts); + default: + break; + } + } + return (if_get_counter_default(ifp, cnt)); +} + int dtsec_attach(device_t dev) { @@ -629,6 +868,9 @@ dtsec_attach(device_t dev) if ((error = fman_get_bushandle(parent, &sc->sc_fm_base)) != 0) return (error); + fman_get_pcd_handle(parent, &sc->sc_pcdh); + fman_get_netenv_handle(parent, &sc->sc_netenvh); + /* Configure working mode */ dtsec_configure_mode(sc); @@ -676,21 +918,33 @@ dtsec_attach(device_t dev) return (ENXIO); } + if (sc->sc_mode == DTSEC_MODE_REGULAR) { + error = dtsec_rm_pcd_init(sc); + if (error != 0) + device_printf(dev, + "PCD init failed, continuing without RSS\n"); + } + /* Create network interface for upper layers */ ifp = sc->sc_ifnet = if_alloc(IFT_ETHER); if_setsoftc(ifp, sc); - if_setflags(ifp, IFF_SIMPLEX | IFF_BROADCAST); + if_setflags(ifp, IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST); if_setinitfn(ifp, dtsec_if_init); - if_setstartfn(ifp, dtsec_if_start); if_setioctlfn(ifp, dtsec_if_ioctl); - if_setsendqlen(ifp, IFQ_MAXLEN); + if_setgetcounterfn(ifp, dtsec_get_counter); - if (sc->sc_phy_addr >= 0) - if_initname(ifp, device_get_name(sc->sc_dev), - device_get_unit(sc->sc_dev)); - else - if_initname(ifp, "dtsec_phy", device_get_unit(sc->sc_dev)); + if (sc->sc_mode == DTSEC_MODE_REGULAR) { + /* Modern if_transmit: per-CPU TX FQs, no locks, no sendq */ + if_settransmitfn(ifp, dtsec_rm_if_transmit); + if_setqflushfn(ifp, dtsec_if_qflush); + } else { + /* Legacy if_start for Independent Mode */ + if_setstartfn(ifp, dtsec_if_start); + if_setsendqlen(ifp, IFQ_MAXLEN); + } + + if_initname(ifp, "dtsec", device_get_unit(sc->sc_dev)); /* TODO */ #if 0 @@ -698,22 +952,59 @@ dtsec_attach(device_t dev) if_setsendqready(ifp); #endif + if_setcapabilities(ifp, IFCAP_JUMBO_MTU | IFCAP_VLAN_MTU + | IFCAP_TXCSUM | IFCAP_TXCSUM_IPV6 + | IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6); if_setcapenable(ifp, if_getcapabilities(ifp)); + if_sethwassist(ifp, CSUM_IP | CSUM_IP_TCP | CSUM_IP_UDP | + CSUM_IP6_TCP | CSUM_IP6_UDP); - /* Attach PHY(s) */ - error = mii_attach(sc->sc_dev, &sc->sc_mii_dev, ifp, dtsec_ifmedia_upd, - dtsec_ifmedia_sts, BMSR_DEFCAPMASK, sc->sc_phy_addr, - MII_OFFSET_ANY, 0); - if (error) { - device_printf(sc->sc_dev, "attaching PHYs failed: %d\n", error); - dtsec_detach(sc->sc_dev); - return (error); + /* Attach PHY(s) — skip for 10G ports with no external PHY + * (SFP+ uses in-band status, sc_phy_addr set to -1). */ + if (sc->sc_phy_addr >= 0) { + error = mii_attach(sc->sc_dev, &sc->sc_mii_dev, ifp, + dtsec_ifmedia_upd, dtsec_ifmedia_sts, BMSR_DEFCAPMASK, + sc->sc_phy_addr, MII_OFFSET_ANY, 0); + if (error) { + if_printf(sc->sc_ifnet, "attaching PHYs failed: " + "%d\n", error); + dtsec_detach(sc->sc_dev); + return (error); + } + sc->sc_mii = device_get_softc(sc->sc_mii_dev); } - sc->sc_mii = device_get_softc(sc->sc_mii_dev); /* Attach to stack */ ether_ifattach(ifp, sc->sc_mac_addr); + /* Set baudrate */ + if (sc->sc_eth_dev_type == ETH_10GSEC) + if_setbaudrate(ifp, IF_Gbps(10ULL)); + else + if_setbaudrate(ifp, IF_Gbps(1ULL)); + + /* 10G SFP+ direct-attach: link is always up (no PHY) */ + if (sc->sc_phy_addr < 0) + if_link_state_change(ifp, LINK_STATE_UP); + + /* Add diagnostic sysctls */ + if (sc->sc_mode == DTSEC_MODE_REGULAR) { + struct sysctl_ctx_list *ctx; + struct sysctl_oid *tree; + + ctx = device_get_sysctl_ctx(dev); + tree = device_get_sysctl_tree(dev); + + SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, + "bman_free", CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_MPSAFE, + sc, 0, dtsec_sysctl_bman_free, "IU", + "BMan free buffer count"); + SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, + "diag", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, + sc, 0, dtsec_sysctl_diag, "I", + "Write 1 to dump DQRR + BMan state to dmesg"); + } + return (0); } @@ -740,6 +1031,9 @@ dtsec_detach(device_t dev) } if (sc->sc_mode == DTSEC_MODE_REGULAR) { + /* Free PCD (must happen before port free) */ + dtsec_rm_pcd_free(sc); + /* Free RX/TX FQRs */ dtsec_rm_fqr_rx_free(sc); dtsec_rm_fqr_tx_free(sc); @@ -820,6 +1114,9 @@ dtsec_miibus_statchg(device_t dev) DTSEC_LOCK_ASSERT(sc); + if (sc->sc_mii == NULL) + return; /* 10G port, no PHY — nothing to adjust */ + duplex = ((sc->sc_mii->mii_media_active & IFM_GMASK) == IFM_FDX); switch (IFM_SUBTYPE(sc->sc_mii->mii_media_active)) { @@ -842,6 +1139,6 @@ dtsec_miibus_statchg(device_t dev) error = FM_MAC_AdjustLink(sc->sc_mach, speed, duplex); if (error != E_OK) - device_printf(sc->sc_dev, "error while adjusting MAC speed.\n"); + if_printf(sc->sc_ifnet, "error while adjusting MAC speed.\n"); } /** @} */ diff --git a/sys/dev/dpaa/if_dtsec.h b/sys/dev/dpaa/if_dtsec.h index 4de0b776e9f..48ee00f0ea7 100644 --- a/sys/dev/dpaa/if_dtsec.h +++ b/sys/dev/dpaa/if_dtsec.h @@ -27,6 +27,8 @@ #ifndef IF_DTSEC_H_ #define IF_DTSEC_H_ +#include "opt_dpaa.h" + /** * @group dTSEC common API. * @{ @@ -34,6 +36,9 @@ #define DTSEC_MODE_REGULAR 0 #define DTSEC_MODE_INDEPENDENT 1 +#define DTSEC_TX_NUM_FQS 4 /* One per CPU (LS1046A quad-core) */ +#define DTSEC_RX_NUM_FQS 128 /* RSS hash distribution (power of 2) */ + #define DTSEC_LOCK(sc) mtx_lock(&(sc)->sc_lock) #define DTSEC_UNLOCK(sc) mtx_unlock(&(sc)->sc_lock) #define DTSEC_LOCK_ASSERT(sc) mtx_assert(&(sc)->sc_lock, MA_OWNED) @@ -65,6 +70,7 @@ struct dtsec_softc { /* dTSEC data */ enum eth_dev_type sc_eth_dev_type; uint8_t sc_eth_id; /* Ethernet ID within its frame manager */ + uint8_t sc_mac_cell_index; /* raw DT cell-index */ uintptr_t sc_mac_mem_offset; e_EnetMode sc_mac_enet_mode; int sc_mac_mdio_irq; @@ -97,20 +103,42 @@ struct dtsec_softc { uint8_t sc_rx_bpid; uma_zone_t sc_rx_zone; char sc_rx_zname[64]; + uint32_t sc_rx_buf_total; /* total buffers alloc'd from UMA */ - /* RX Frame Queue */ - t_Handle sc_rx_fqr; + /* RX Frame Queues — RSS hash distribution via dedicated channels */ + t_Handle sc_rx_fqr[DTSEC_RX_NUM_FQS]; uint32_t sc_rx_fqid; - /* TX Frame Queue */ - t_Handle sc_tx_fqr; - bool sc_tx_fqr_full; + /* Per-CPU TX Frame Queues */ + t_Handle sc_tx_fqs[DTSEC_TX_NUM_FQS]; + volatile int sc_tx_inflight[DTSEC_TX_NUM_FQS]; + + /* TX Confirmation Frame Queue (single, on pool channel) */ t_Handle sc_tx_conf_fqr; uint32_t sc_tx_conf_fqid; /* Frame Info Zone */ uma_zone_t sc_fi_zone; char sc_fi_zname[64]; + + /* TX buffer prefix data offset (from FM_PORT_GetBufferDataOffset) */ + uint32_t sc_tx_data_offset; + + /* SG table buffer zone (prefix + SG entries) */ + uma_zone_t sc_sgt_zone; + char sc_sgt_zname[64]; + uint32_t sc_sgt_buf_size; + + /* PCD (Parse-Classify-Distribute) for RSS + RX checksum */ + t_Handle sc_pcdh; + t_Handle sc_netenvh; + t_Handle sc_scheme; + uint32_t sc_rx_data_offset; + t_Handle sc_vsph; /* default VSP handle */ + + /* CEETM DSCP-based egress QoS (populated by CDX module) */ + volatile int sc_ceetm_en; + uint32_t sc_ceetm_dscp_fqid[64]; }; /** @} */ @@ -124,7 +152,7 @@ enum dtsec_fm_port_params { FM_PORT_LIODN_OFFSET = 0, FM_PORT_MEM_ID = 0, FM_PORT_MEM_ATTR = MEMORY_ATTR_CACHEABLE, - FM_PORT_BUFFER_SIZE = MCLBYTES, + FM_PORT_BUFFER_SIZE = (9600 + 64), /* DTSEC_MAX_FRAME_SIZE + headroom */ }; e_FmPortType dtsec_fm_port_rx_type(enum eth_dev_type type); diff --git a/sys/dev/dpaa/if_dtsec_fdt.c b/sys/dev/dpaa/if_dtsec_fdt.c index 4bab2aa2b48..9d5b071e3d3 100644 --- a/sys/dev/dpaa/if_dtsec_fdt.c +++ b/sys/dev/dpaa/if_dtsec_fdt.c @@ -35,7 +35,9 @@ #include +#ifdef __powerpc__ #include +#endif #include #include @@ -100,11 +102,11 @@ dtsec_fdt_probe(device_t dev) return (ENXIO); if (!ofw_bus_is_compatible(dev, "fsl,fman-dtsec") && - !ofw_bus_is_compatible(dev, "fsl,fman-xgec")) + !ofw_bus_is_compatible(dev, "fsl,fman-xgec") && + !ofw_bus_is_compatible(dev, "fsl,fman-memac")) return (ENXIO); - device_set_desc(dev, "Freescale Data Path Triple Speed Ethernet " - "Controller"); + device_set_desc(dev, "Freescale Data Path Ethernet Controller"); return (BUS_PROBE_DEFAULT); } @@ -116,7 +118,7 @@ dtsec_fdt_attach(device_t dev) device_t phy_dev; phandle_t enet_node, phy_node; phandle_t fman_rxtx_node[2]; - char phy_type[6]; + char phy_type[16]; pcell_t fman_tx_cell, mac_id; int rid; @@ -130,26 +132,47 @@ dtsec_fdt_attach(device_t dev) return (ENXIO); } - /* Get link speed */ - if (ofw_bus_is_compatible(dev, "fsl,fman-dtsec") != 0) + /* Determine MAC type / link speed */ + if (ofw_bus_is_compatible(dev, "fsl,fman-memac") != 0) { + /* mEMAC: detect 10G from phy-connection-type */ + if (OF_getprop(enet_node, "phy-connection-type", + (void *)phy_type, sizeof(phy_type)) > 0 && + (!strcmp(phy_type, "xgmii") || + !strcmp(phy_type, "10gbase-r"))) + sc->sc_eth_dev_type = ETH_10GSEC; + else + sc->sc_eth_dev_type = ETH_DTSEC; + } else if (ofw_bus_is_compatible(dev, "fsl,fman-dtsec") != 0) sc->sc_eth_dev_type = ETH_DTSEC; else if (ofw_bus_is_compatible(dev, "fsl,fman-xgec") != 0) sc->sc_eth_dev_type = ETH_10GSEC; - else + else { + device_printf(dev, "unknown MAC compatible\n"); return(ENXIO); + } /* Get PHY address */ - if (OF_getprop(enet_node, "phy-handle", (void *)&phy_node, - sizeof(phy_node)) <= 0) + if (OF_getencprop(enet_node, "phy-handle", (void *)&phy_node, + sizeof(phy_node)) <= 0) { + /* 10G ports may use managed="in-band-status" with no PHY */ + if (sc->sc_eth_dev_type == ETH_10GSEC) { + sc->sc_phy_addr = -1; + sc->sc_mdio = NULL; + goto skip_phy; + } + device_printf(dev, "missing phy-handle\n"); return (ENXIO); + } phy_node = OF_node_from_xref(phy_node); - if (OF_getprop(phy_node, "reg", (void *)&sc->sc_phy_addr, - sizeof(sc->sc_phy_addr)) <= 0) + if (OF_getencprop(phy_node, "reg", (void *)&sc->sc_phy_addr, + sizeof(sc->sc_phy_addr)) <= 0) { + device_printf(dev, "missing phy reg\n"); return (ENXIO); + } - phy_dev = OF_device_from_xref(OF_parent(phy_node)); + phy_dev = OF_device_from_xref(OF_xref_from_node(OF_parent(phy_node))); if (phy_dev == NULL) { device_printf(dev, "No PHY found.\n"); @@ -157,68 +180,106 @@ dtsec_fdt_attach(device_t dev) } sc->sc_mdio = phy_dev; +skip_phy: /* Get MAC memory offset in SoC */ rid = 0; sc->sc_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE); - if (sc->sc_mem == NULL) + if (sc->sc_mem == NULL) { + device_printf(dev, "could not alloc memory resource\n"); return (ENXIO); + } /* Get PHY connection type */ if (OF_getprop(enet_node, "phy-connection-type", (void *)phy_type, - sizeof(phy_type)) <= 0) + sizeof(phy_type)) <= 0) { + device_printf(dev, "missing phy-connection-type\n"); return (ENXIO); + } if (!strcmp(phy_type, "sgmii")) sc->sc_mac_enet_mode = e_ENET_MODE_SGMII_1000; else if (!strcmp(phy_type, "rgmii")) sc->sc_mac_enet_mode = e_ENET_MODE_RGMII_1000; else if (!strcmp(phy_type, "xgmii")) - /* We set 10 Gigabit mode flag however we don't support it */ sc->sc_mac_enet_mode = e_ENET_MODE_XGMII_10000; - else + else if (!strcmp(phy_type, "10gbase-r")) + sc->sc_mac_enet_mode = e_ENET_MODE_XGMII_10000; + else { + device_printf(dev, "unsupported phy-connection-type: %s\n", + phy_type); return (ENXIO); + } if (OF_getencprop(enet_node, "cell-index", - (void *)&mac_id, sizeof(mac_id)) <= 0) + (void *)&mac_id, sizeof(mac_id)) <= 0) { + device_printf(dev, "missing cell-index\n"); return (ENXIO); + } sc->sc_eth_id = mac_id; + sc->sc_mac_cell_index = mac_id; + /* ncsw expects 0-based per-type MAC IDs: 0..5 for 1G, 0..1 for 10G. + * FManV3 device trees use cell-index 0-5 for 1G MACs but 8-9 for + * 10G MACs (hardware MEMAC slot numbering). Remap to 0-based. */ + if (sc->sc_eth_dev_type == ETH_10GSEC && mac_id >= 8) + sc->sc_eth_id = mac_id - 8; /* Get RX/TX port handles */ - if (OF_getprop(enet_node, "fsl,fman-ports", (void *)fman_rxtx_node, - sizeof(fman_rxtx_node)) <= 0) + if (OF_getencprop(enet_node, "fsl,fman-ports", (void *)fman_rxtx_node, + sizeof(fman_rxtx_node)) <= 0) { + device_printf(dev, "missing fsl,fman-ports\n"); return (ENXIO); + } - if (fman_rxtx_node[0] == 0) + if (fman_rxtx_node[0] == 0) { + device_printf(dev, "fsl,fman-ports[0] is zero\n"); return (ENXIO); + } - if (fman_rxtx_node[1] == 0) + if (fman_rxtx_node[1] == 0) { + device_printf(dev, "fsl,fman-ports[1] is zero\n"); return (ENXIO); + } - fman_rxtx_node[0] = OF_instance_to_package(fman_rxtx_node[0]); - fman_rxtx_node[1] = OF_instance_to_package(fman_rxtx_node[1]); + /* fsl,fman-ports are phandle references on ARM64, ihandles on PPC */ + fman_rxtx_node[0] = OF_node_from_xref(fman_rxtx_node[0]); + fman_rxtx_node[1] = OF_node_from_xref(fman_rxtx_node[1]); if (ofw_bus_node_is_compatible(fman_rxtx_node[0], - "fsl,fman-v2-port-rx") == 0) + "fsl,fman-v2-port-rx") == 0 && + ofw_bus_node_is_compatible(fman_rxtx_node[0], + "fsl,fman-v3-port-rx") == 0) { + device_printf(dev, "RX port incompatible\n"); return (ENXIO); + } if (ofw_bus_node_is_compatible(fman_rxtx_node[1], - "fsl,fman-v2-port-tx") == 0) + "fsl,fman-v2-port-tx") == 0 && + ofw_bus_node_is_compatible(fman_rxtx_node[1], + "fsl,fman-v3-port-tx") == 0) { + device_printf(dev, "TX port incompatible\n"); return (ENXIO); + } /* Get RX port HW id */ - if (OF_getprop(fman_rxtx_node[0], "reg", (void *)&sc->sc_port_rx_hw_id, - sizeof(sc->sc_port_rx_hw_id)) <= 0) + if (OF_getencprop(fman_rxtx_node[0], "reg", (void *)&sc->sc_port_rx_hw_id, + sizeof(sc->sc_port_rx_hw_id)) <= 0) { + device_printf(dev, "missing RX port reg\n"); return (ENXIO); + } /* Get TX port HW id */ - if (OF_getprop(fman_rxtx_node[1], "reg", (void *)&sc->sc_port_tx_hw_id, - sizeof(sc->sc_port_tx_hw_id)) <= 0) + if (OF_getencprop(fman_rxtx_node[1], "reg", (void *)&sc->sc_port_tx_hw_id, + sizeof(sc->sc_port_tx_hw_id)) <= 0) { + device_printf(dev, "missing TX port reg\n"); return (ENXIO); + } - if (OF_getprop(fman_rxtx_node[1], "cell-index", &fman_tx_cell, - sizeof(fman_tx_cell)) <= 0) + if (OF_getencprop(fman_rxtx_node[1], "cell-index", &fman_tx_cell, + sizeof(fman_tx_cell)) <= 0) { + device_printf(dev, "missing TX port cell-index\n"); return (ENXIO); + } /* Get QMan channel */ sc->sc_port_tx_qman_chan = fman_qman_channel_id(device_get_parent(dev), fman_tx_cell); diff --git a/sys/dev/dpaa/if_dtsec_im.c b/sys/dev/dpaa/if_dtsec_im.c index fcde0df6030..0cd55dbc7c7 100644 --- a/sys/dev/dpaa/if_dtsec_im.c +++ b/sys/dev/dpaa/if_dtsec_im.c @@ -229,7 +229,8 @@ dtsec_im_if_start_locked(struct dtsec_softc *sc) DTSEC_LOCK_ASSERT(sc); /* TODO: IFF_DRV_OACTIVE */ - if ((sc->sc_mii->mii_media_status & IFM_ACTIVE) == 0) + if (sc->sc_mii != NULL && + (sc->sc_mii->mii_media_status & IFM_ACTIVE) == 0) return; if ((if_getdrvflags(sc->sc_ifnet) & IFF_DRV_RUNNING) != IFF_DRV_RUNNING) diff --git a/sys/dev/dpaa/if_dtsec_rm.c b/sys/dev/dpaa/if_dtsec_rm.c index b213a259b5b..8228378360d 100644 --- a/sys/dev/dpaa/if_dtsec_rm.c +++ b/sys/dev/dpaa/if_dtsec_rm.c @@ -36,6 +36,8 @@ #include #include #include +#include +#include #include #include @@ -49,13 +51,25 @@ #include "miibus_if.h" +#include "opt_dpaa.h" + +#include +#include +#include +#include +#include + #include #include #include #include +#include +#include #include +#include #include "fman.h" +#include "fman_chardev.h" #include "bman.h" #include "qman.h" #include "if_dtsec.h" @@ -69,23 +83,57 @@ #define DTSEC_BPOOLS_USED (1) #define DTSEC_MAX_TX_QUEUE_LEN 256 +#define DTSEC_RX_QUEUES 128 /* Power of 2, 7-bit hash mask like Linux */ + +/* FMan parse result L3/L4 type indicators (big-endian in parse result) */ +#define FM_L3_PARSE_RESULT_IPV4 0x8000 +#define FM_L3_PARSE_RESULT_IPV6 0x4000 +#define FM_L4_PARSE_RESULT_UDP 0x40 +#define FM_L4_PARSE_RESULT_TCP 0x20 + struct dtsec_rm_frame_info { struct mbuf *fi_mbuf; t_DpaaSGTE fi_sgt[DPAA_NUM_OF_SG_TABLE_ENTRY]; + void *fi_tx_buf; /* SG table buffer (prefix + SG entries) */ + uint8_t fi_cpu; /* CPU index for in-flight tracking */ }; enum dtsec_rm_pool_params { - DTSEC_RM_POOL_RX_LOW_MARK = 16, - DTSEC_RM_POOL_RX_HIGH_MARK = 64, - DTSEC_RM_POOL_RX_MAX_SIZE = 256, + DTSEC_RM_POOL_RX_LOW_MARK = 256, + DTSEC_RM_POOL_RX_HIGH_MARK = 2048, + DTSEC_RM_POOL_RX_MAX_SIZE = 2048, + /* + * Maximum in-flight RX buffers per port. + * + * Buffers are freed back to UMA when the mbuf is released + * (consumption model), so this bounds simultaneous allocation, + * not permanent memory commitment. The original KVA pointer + * is stashed in the buffer's privData area before handing to + * BMan, then recovered via the DMAP address after the hardware + * round-trip to pass back to uma_zfree. + * + * 4096 × 9664 = ~38 MB peak per port. + */ + DTSEC_RM_POOL_RX_MAX_TOTAL = 4096, + /* + * Active refill threshold: when free buffer count drops below + * this, the RX callback allocates new buffers. FMan acquires + * buffers directly from BMan hardware, bypassing software + * depletion callbacks — without active refill the pool exhausts + * under sustained RX load. + */ + DTSEC_RM_POOL_RX_REFILL_THRESH = 1024, + DTSEC_RM_POOL_RX_REFILL_COUNT = 256, DTSEC_RM_POOL_FI_LOW_MARK = 16, DTSEC_RM_POOL_FI_HIGH_MARK = 64, DTSEC_RM_POOL_FI_MAX_SIZE = 256, }; +#ifndef __aarch64__ #define DTSEC_RM_FQR_RX_CHANNEL e_QM_FQ_CHANNEL_POOL1 -#define DTSEC_RM_FQR_TX_CONF_CHANNEL e_QM_FQ_CHANNEL_SWPORTAL0 +#endif +#define DTSEC_RM_FQR_TX_CONF_CHANNEL e_QM_FQ_CHANNEL_POOL1 enum dtsec_rm_fqr_params { DTSEC_RM_FQR_RX_WQ = 1, DTSEC_RM_FQR_TX_WQ = 1, @@ -104,6 +152,8 @@ dtsec_rm_fi_pool_free(struct dtsec_softc *sc) if (sc->sc_fi_zone != NULL) uma_zdestroy(sc->sc_fi_zone); + if (sc->sc_sgt_zone != NULL) + uma_zdestroy(sc->sc_sgt_zone); } int @@ -178,13 +228,167 @@ dtsec_rm_fm_port_rx_init(struct dtsec_softc *sc, int unit) return (ENXIO); } + if (sc->sc_pcdh != NULL) { + /* + * Configure RX buffer prefix: parse result for checksum + * validation and hash result for RSS distribution. + * + * manipExtraSpace = 96 matches the Linux SDK DTS + * buffer-layout = <0x60 0x40> which reserves 96 bytes + * for header manipulation (NAT, encap) by CDX/FMan. + */ + t_FmBufferPrefixContent prefix; + + memset(&prefix, 0, sizeof(prefix)); + prefix.privDataSize = 16; + prefix.passPrsResult = TRUE; + prefix.passHashResult = TRUE; + prefix.dataAlign = 64; + prefix.manipExtraSpace = 96; + + error = FM_PORT_ConfigBufferPrefixContent(sc->sc_rxph, + &prefix); + if (error != E_OK) { + device_printf(sc->sc_dev, + "couldn't configure RX buffer prefix.\n"); + FM_PORT_Free(sc->sc_rxph); + sc->sc_rxph = NULL; + return (ENXIO); + } + } + +#ifdef FM_HEAVY_TRAFFIC_HANG_ERRATA_FMAN_A005669 + /* Errata A005669: discard frames with physical errors (FCS, SGMII + * disparity, FIFO overflow) in FMan hardware and increase FIFO by + * 4KB to prevent FMan hang under heavy traffic. */ + FM_PORT_ConfigBCBWorkaround(sc->sc_rxph); +#endif + error = FM_PORT_Init(sc->sc_rxph); if (error != E_OK) { device_printf(sc->sc_dev, "couldn't initialize FM Port RX.\n"); FM_PORT_Free(sc->sc_rxph); + sc->sc_rxph = NULL; return (ENXIO); } + /* Enable BMI statistics counters for PCD diagnostics */ + FM_PORT_SetStatisticsCounters(sc->sc_rxph, TRUE); + + /* + * Reserve 1 port-private policer profile slot for this RX port. + * CDX QoS creates the actual profile via FM_PCD_PlcrProfileSet() + * when the CDX module loads (before dpa_app calls FM_PORT_SetPCD). + * Must be called after FM_PORT_Init() and before FM_PORT_SetPCD(). + */ + error = FM_PORT_PcdPlcrAllocProfiles(sc->sc_rxph, 1); + if (error != E_OK) + device_printf(sc->sc_dev, + "FM_PORT_PcdPlcrAllocProfiles failed: %d\n", error); + + /* + * Allocate Virtual Storage Profiles for this RX port. + * FManv3+ (DPAA >= 11) requires per-port VSP allocation for + * the CC/PCD pipeline to work correctly after CDX/dpa_app + * replaces the default PCD. This matches the Linux SDK driver + * which reads vsp-window = <2 0> from the DTS extended-args. + * + * Must be called after FM_PORT_Init() and before FM_PORT_SetPCD() + * and FM_PORT_Enable(). + */ +#if (DPAA_VERSION >= 11) + if (sc->sc_pcdh != NULL && sc->sc_txph != NULL) { + t_FmPortVSPAllocParams vsp_alloc; + t_FmVspParams vsp_params; + t_FmBufferPrefixContent vsp_prefix; + t_Handle h_vsp; + + /* Step 1: Allocate 2 VSP slots for this port */ + memset(&vsp_alloc, 0, sizeof(vsp_alloc)); + vsp_alloc.numOfProfiles = 2; + vsp_alloc.dfltRelativeId = 0; + vsp_alloc.h_FmTxPort = sc->sc_txph; + + error = FM_PORT_VSPAlloc(sc->sc_rxph, &vsp_alloc); + if (error != E_OK) { + device_printf(sc->sc_dev, + "couldn't allocate VSP: error %d\n", error); + goto vsp_done; + } + + /* Step 2: Configure default VSP (profile 0) */ + memset(&vsp_params, 0, sizeof(vsp_params)); + vsp_params.h_Fm = sc->sc_fmh; + vsp_params.portParams.portType = + dtsec_fm_port_rx_type(sc->sc_eth_dev_type); + vsp_params.portParams.portId = sc->sc_eth_id; + vsp_params.relativeProfileId = 0; + vsp_params.liodnOffset = 0; + vsp_params.extBufPools.numOfPoolsUsed = DTSEC_BPOOLS_USED; + vsp_params.extBufPools.extBufPool[0].id = sc->sc_rx_bpid; + vsp_params.extBufPools.extBufPool[0].size = FM_PORT_BUFFER_SIZE; + + h_vsp = FM_VSP_Config(&vsp_params); + if (h_vsp == NULL) { + device_printf(sc->sc_dev, + "couldn't configure VSP.\n"); + goto vsp_done; + } + + /* Step 3: Set VSP buffer prefix (matching SDK DTS + * buffer-layout = <0x60 0x40>) */ + memset(&vsp_prefix, 0, sizeof(vsp_prefix)); + vsp_prefix.privDataSize = 16; + vsp_prefix.passPrsResult = TRUE; + vsp_prefix.passHashResult = TRUE; + vsp_prefix.dataAlign = 64; + vsp_prefix.manipExtraSpace = 96; + + error = FM_VSP_ConfigBufferPrefixContent(h_vsp, &vsp_prefix); + if (error != E_OK) { + device_printf(sc->sc_dev, + "couldn't configure VSP buffer prefix: %d\n", + error); + FM_VSP_Free(h_vsp); + goto vsp_done; + } + + /* Step 4: Initialize VSP hardware registers */ + error = FM_VSP_Init(h_vsp); + if (error != E_OK) { + device_printf(sc->sc_dev, + "couldn't initialize VSP: %d\n", error); + FM_VSP_Free(h_vsp); + goto vsp_done; + } + + sc->sc_vsph = h_vsp; + device_printf(sc->sc_dev, + "VSP allocated: 2 profiles, default ID 0\n"); +vsp_done: ; + } +#endif /* DPAA_VERSION >= 11 */ + + /* Register RX port with FMD chardev for userspace PCD control */ + { + struct fmcd_softc *fmcd; + + fmcd = fman_get_fmcd(device_get_parent(sc->sc_dev)); + if (fmcd != NULL) + fmcd_register_rx_port(fmcd, + sc->sc_mac_cell_index, sc->sc_rxph, + sc->sc_rx_fqid); + } + + if (sc->sc_pcdh != NULL) { + sc->sc_rx_data_offset = + FM_PORT_GetBufferDataOffset(sc->sc_rxph); + if (bootverbose) + device_printf(sc->sc_dev, + "RX buffer data offset: %u\n", + sc->sc_rx_data_offset); + } + if (bootverbose) device_printf(sc->sc_dev, "RX hw port 0x%02x initialized.\n", sc->sc_port_rx_hw_id); @@ -224,13 +428,57 @@ dtsec_rm_fm_port_tx_init(struct dtsec_softc *sc, int unit) return (ENXIO); } + /* Configure TX buffer prefix to include parse results area. + * When TX checksum offload is requested, the driver fills in the + * parse result and sets FM_FD_CMD_RPD | FM_FD_CMD_DTC. + * + * manipExtraSpace = 96 matches the Linux SDK DTS + * buffer-layout = <0x60 0x40> for TX port extended-args. + */ + { + t_FmBufferPrefixContent prefix; + + memset(&prefix, 0, sizeof(prefix)); + prefix.privDataSize = 16; + prefix.passPrsResult = TRUE; + prefix.passTimeStamp = FALSE; + prefix.passHashResult = FALSE; + prefix.passAllOtherPCDInfo = FALSE; + prefix.dataAlign = 64; + prefix.manipExtraSpace = 96; + + error = FM_PORT_ConfigBufferPrefixContent(sc->sc_txph, + &prefix); + if (error != E_OK) { + device_printf(sc->sc_dev, + "couldn't configure TX buffer prefix.\n"); + FM_PORT_Free(sc->sc_txph); + sc->sc_txph = NULL; + return (ENXIO); + } + } + error = FM_PORT_Init(sc->sc_txph); if (error != E_OK) { device_printf(sc->sc_dev, "couldn't initialize FM Port TX.\n"); FM_PORT_Free(sc->sc_txph); + sc->sc_txph = NULL; return (ENXIO); } + sc->sc_tx_data_offset = FM_PORT_GetBufferDataOffset(sc->sc_txph); + if (bootverbose) + device_printf(sc->sc_dev, "TX buffer data offset: %u\n", + sc->sc_tx_data_offset); + + /* Create UMA zone for SG table buffers (prefix + SG entries) */ + sc->sc_sgt_buf_size = sc->sc_tx_data_offset + + DPAA_NUM_OF_SG_TABLE_ENTRY * sizeof(t_DpaaSGTE); + snprintf(sc->sc_sgt_zname, sizeof(sc->sc_sgt_zname), "%s: SGT buf", + device_get_nameunit(sc->sc_dev)); + sc->sc_sgt_zone = uma_zcreate(sc->sc_sgt_zname, sc->sc_sgt_buf_size, + NULL, NULL, NULL, NULL, 63 /* 64-byte align */, 0); + if (bootverbose) device_printf(sc->sc_dev, "TX hw port 0x%02x initialized.\n", sc->sc_port_tx_hw_id); @@ -244,6 +492,31 @@ dtsec_rm_fm_port_tx_init(struct dtsec_softc *sc, int unit) * @group dTSEC buffer pools routines. * @{ */ + +/* + * Store the original KVA pointer from uma_zalloc in the buffer's + * private data area (first 8 bytes). FMan never touches privData + * (privDataSize=16), so this survives the BMan/FMan/QMan round-trip. + * + * After the round-trip, XX_PhysToVirt returns a DMAP address — a + * different virtual address for the same physical memory. We read + * back the original KVA pointer via the DMAP mapping to pass to + * uma_zfree, which requires the exact address it originally returned. + */ +static inline void +dtsec_rm_buf_stash_ptr(void *buf) +{ + + *(uintptr_t *)buf = (uintptr_t)buf; +} + +static inline void * +dtsec_rm_buf_recover_ptr(void *buf) +{ + + return ((void *)(*(uintptr_t *)buf)); +} + static t_Error dtsec_rm_pool_rx_put_buffer(t_Handle h_BufferPool, uint8_t *buffer, t_Handle context) @@ -251,7 +524,7 @@ dtsec_rm_pool_rx_put_buffer(t_Handle h_BufferPool, uint8_t *buffer, struct dtsec_softc *sc; sc = h_BufferPool; - uma_zfree(sc->sc_rx_zone, buffer); + uma_zfree(sc->sc_rx_zone, dtsec_rm_buf_recover_ptr(buffer)); return (E_OK); } @@ -264,27 +537,65 @@ dtsec_rm_pool_rx_get_buffer(t_Handle h_BufferPool, t_Handle *context) sc = h_BufferPool; buffer = uma_zalloc(sc->sc_rx_zone, M_NOWAIT); + if (buffer != NULL) + dtsec_rm_buf_stash_ptr(buffer); return (buffer); } +/* + * Refill the BMan buffer pool directly from UMA, bypassing + * BM_POOL_FillBufs. This avoids the NCSW RETURN_ERROR printf + * when uma_zalloc(M_NOWAIT) transiently fails — a single printf + * to serial console at 115200 baud takes ~5ms, blocking the CPU + * in interrupt context and causing cascading buffer exhaustion. + * + * Returns the number of buffers actually allocated. + */ +static unsigned int +dtsec_rm_pool_rx_refill(struct dtsec_softc *sc, unsigned int nbufs) +{ + unsigned int i; + uint8_t *buf; + + for (i = 0; i < nbufs; i++) { + buf = uma_zalloc(sc->sc_rx_zone, M_NOWAIT); + if (buf == NULL) + break; + dtsec_rm_buf_stash_ptr(buf); + if (bman_put_buffer(sc->sc_rx_pool, buf) != 0) { + uma_zfree(sc->sc_rx_zone, buf); + break; + } + } + return (i); +} + static void dtsec_rm_pool_rx_depleted(t_Handle h_App, bool in) { struct dtsec_softc *sc; - unsigned int count; + unsigned int count, headroom, nbufs; sc = h_App; if (!in) return; - while (1) { - count = bman_count(sc->sc_rx_pool); - if (count > DTSEC_RM_POOL_RX_HIGH_MARK) - return; + /* Only refill up to the per-port cap */ + if (atomic_load_32(&sc->sc_rx_buf_total) >= + DTSEC_RM_POOL_RX_MAX_TOTAL) + return; - bman_pool_fill(sc->sc_rx_pool, DTSEC_RM_POOL_RX_HIGH_MARK); + count = bman_count(sc->sc_rx_pool); + if (count < DTSEC_RM_POOL_RX_HIGH_MARK) { + headroom = DTSEC_RM_POOL_RX_MAX_TOTAL - + atomic_load_32(&sc->sc_rx_buf_total); + nbufs = DTSEC_RM_POOL_RX_HIGH_MARK - count; + if (nbufs > headroom) + nbufs = headroom; + nbufs = dtsec_rm_pool_rx_refill(sc, nbufs); + atomic_add_32(&sc->sc_rx_buf_total, nbufs); } } @@ -303,14 +614,11 @@ int dtsec_rm_pool_rx_init(struct dtsec_softc *sc) { - /* FM_PORT_BUFFER_SIZE must be less than PAGE_SIZE */ - CTASSERT(FM_PORT_BUFFER_SIZE < PAGE_SIZE); - snprintf(sc->sc_rx_zname, sizeof(sc->sc_rx_zname), "%s: RX Buffers", device_get_nameunit(sc->sc_dev)); sc->sc_rx_zone = uma_zcreate(sc->sc_rx_zname, FM_PORT_BUFFER_SIZE, NULL, - NULL, NULL, NULL, FM_PORT_BUFFER_SIZE - 1, 0); + NULL, NULL, NULL, 255 /* 256-byte align for DMA */, 0); sc->sc_rx_pool = bman_pool_create(&sc->sc_rx_bpid, FM_PORT_BUFFER_SIZE, 0, 0, DTSEC_RM_POOL_RX_MAX_SIZE, dtsec_rm_pool_rx_get_buffer, @@ -323,6 +631,8 @@ dtsec_rm_pool_rx_init(struct dtsec_softc *sc) return (EIO); } + sc->sc_rx_buf_total = DTSEC_RM_POOL_RX_MAX_SIZE; + return (0); } /** @} */ @@ -335,15 +645,21 @@ dtsec_rm_pool_rx_init(struct dtsec_softc *sc) static void dtsec_rm_fqr_mext_free(struct mbuf *m) { - struct dtsec_softc *sc; void *buffer; + struct dtsec_softc *sc; buffer = m->m_ext.ext_arg1; sc = m->m_ext.ext_arg2; - if (bman_count(sc->sc_rx_pool) <= DTSEC_RM_POOL_RX_MAX_SIZE) - bman_put_buffer(sc->sc_rx_pool, buffer); - else - dtsec_rm_pool_rx_put_buffer(sc, buffer, NULL); + + /* + * Consumption model: free the buffer back to UMA using the + * original KVA pointer stashed in the privData area. The + * buffer address here is a DMAP address (from XX_PhysToVirt), + * but the stashed pointer at offset 0 is the original KVA + * address that uma_zalloc returned. + */ + uma_zfree(sc->sc_rx_zone, dtsec_rm_buf_recover_ptr(buffer)); + atomic_subtract_32(&sc->sc_rx_buf_total, 1); } static e_RxStoreResponse @@ -357,20 +673,66 @@ dtsec_rm_fqr_rx_callback(t_Handle app, t_Handle fqr, t_Handle portal, m = NULL; sc = app; + /* + * Refill BMan buffer pool if running low. + * FMan acquires buffers directly from BMan hardware, bypassing + * software depletion callbacks. Without active refill, the pool + * exhausts under sustained RX load: all buffers end up in mbufs + * held by the TCP stack, FMan drops every subsequent frame, and + * retransmissions are also dropped — permanent deadlock. + */ + if (__predict_false(bman_count(sc->sc_rx_pool) < + DTSEC_RM_POOL_RX_REFILL_THRESH) && + atomic_load_32(&sc->sc_rx_buf_total) < + DTSEC_RM_POOL_RX_MAX_TOTAL) { + unsigned int n, added; + uint32_t total; + + total = atomic_load_32(&sc->sc_rx_buf_total); + n = DTSEC_RM_POOL_RX_REFILL_COUNT; + if (n > DTSEC_RM_POOL_RX_MAX_TOTAL - total) + n = DTSEC_RM_POOL_RX_MAX_TOTAL - total; + added = dtsec_rm_pool_rx_refill(sc, n); + atomic_add_32(&sc->sc_rx_buf_total, added); + } + frame_va = DPAA_FD_GET_ADDR(frame); - KASSERT(DPAA_FD_GET_FORMAT(frame) == e_DPAA_FD_FORMAT_TYPE_SHORT_SBSF, - ("%s(): Got unsupported frame format 0x%02X!", __func__, - DPAA_FD_GET_FORMAT(frame))); + if (frame_va == NULL) + return (e_RX_STORE_RESPONSE_CONTINUE); - KASSERT(DPAA_FD_GET_OFFSET(frame) == 0, - ("%s(): Only offset 0 is supported!", __func__)); - - if (DPAA_FD_GET_STATUS(frame) != 0) { - device_printf(sc->sc_dev, "RX error: 0x%08X\n", - DPAA_FD_GET_STATUS(frame)); + if (DPAA_FD_GET_FORMAT(frame) != e_DPAA_FD_FORMAT_TYPE_SHORT_SBSF) { + /* SG/compound FDs should not occur with our buffer pool size. + * Log and drop rather than panic. */ + if_printf(sc->sc_ifnet, + "RX: unexpected FD format 0x%02X, dropping\n", + DPAA_FD_GET_FORMAT(frame)); goto err; } + { + uint32_t fd_st = DPAA_FD_GET_STATUS(frame); + uint32_t err_bits = fd_st & + (FM_FD_RX_STATUS_ERR_MASK & ~FM_FD_ERR_NO_SCHEME); + if (err_bits) { + static volatile uint32_t rx_err_cnt; + uint32_t n = atomic_fetchadd_32(&rx_err_cnt, 1) + 1; + if (n <= 10 || (n % 100) == 0) + if_printf(sc->sc_ifnet, + "RX error #%u: fd_status=0x%08X" + " (phy=%d cls=%d red=%d yel=%d" + " plcr=%d prs=%d ext=%d)\n", + n, fd_st, + !!(fd_st & FM_FD_ERR_PHYSICAL), + !!(fd_st & FM_FD_ERR_CLS_DISCARD), + !!(fd_st & FM_FD_ERR_COLOR_RED), + !!(fd_st & FM_FD_ERR_COLOR_YELLOW), + !!(fd_st & FM_FD_ERR_ILL_PLCR), + !!(fd_st & FM_FD_ERR_PRS_HDR_ERR), + !!(fd_st & FM_FD_ERR_EXTRACTION)); + goto err; + } + } + m = m_gethdr(M_NOWAIT, MT_HEADER); if (m == NULL) goto err; @@ -380,15 +742,69 @@ dtsec_rm_fqr_rx_callback(t_Handle app, t_Handle fqr, t_Handle portal, EXT_NET_DRV); m->m_pkthdr.rcvif = sc->sc_ifnet; + m->m_data = (char *)frame_va + DPAA_FD_GET_OFFSET(frame); m->m_len = DPAA_FD_GET_LENGTH(frame); - m_fixhdr(m); + m->m_pkthdr.len = m->m_len; + /* Extract RX checksum result from parse result in buffer prefix */ + if (sc->sc_rx_data_offset > 0) { + t_FmPrsResult *prs; + + prs = FM_PORT_GetBufferPrsResult(sc->sc_rxph, + (char *)frame_va); + if (prs != NULL) { + uint16_t l3r = be16toh(prs->l3r); + + if (l3r & (FM_L3_PARSE_RESULT_IPV4 | + FM_L3_PARSE_RESULT_IPV6)) { + m->m_pkthdr.csum_flags |= + CSUM_L3_CALC | CSUM_L3_VALID; + + if (prs->l4r & (FM_L4_PARSE_RESULT_TCP | + FM_L4_PARSE_RESULT_UDP)) { + uint32_t fd_status = + DPAA_FD_GET_STATUS(frame); + /* + * DCL4C = "Didn't Compute L4 Checksum". + * Only claim HW-verified when clear. + * When set, leave flags alone so the + * stack verifies L4 csum in software. + * + * csum_data = 0xffff: tells the stack + * the full checksum (incl pseudo-hdr) + * is valid. Without this, the stack + * reads csum_data=0 and drops the + * packet as a checksum failure. + */ + if (!(fd_status & FM_FD_CMD_DCL4C)) { + m->m_pkthdr.csum_flags |= + CSUM_L4_CALC | + CSUM_L4_VALID; + m->m_pkthdr.csum_data = + 0xffff; + } + } + } + } + } + + if_inc_counter(sc->sc_ifnet, IFCOUNTER_IPACKETS, 1); + if_inc_counter(sc->sc_ifnet, IFCOUNTER_IBYTES, m->m_pkthdr.len); +#ifdef __aarch64__ + qman_rx_defer(m); +#else if_input(sc->sc_ifnet, m); +#endif return (e_RX_STORE_RESPONSE_CONTINUE); err: - bman_put_buffer(sc->sc_rx_pool, frame_va); + if_inc_counter(sc->sc_ifnet, IFCOUNTER_IERRORS, 1); + if (frame_va != NULL) { + uma_zfree(sc->sc_rx_zone, + dtsec_rm_buf_recover_ptr(frame_va)); + atomic_subtract_32(&sc->sc_rx_buf_total, 1); + } if (m != NULL) m_freem(m); @@ -401,49 +817,93 @@ dtsec_rm_fqr_tx_confirm_callback(t_Handle app, t_Handle fqr, t_Handle portal, { struct dtsec_rm_frame_info *fi; struct dtsec_softc *sc; - unsigned int qlen; t_DpaaSGTE *sgt0; + void *buf; + int cpu; sc = app; - if (DPAA_FD_GET_STATUS(frame) != 0) - device_printf(sc->sc_dev, "TX error: 0x%08X\n", - DPAA_FD_GET_STATUS(frame)); + buf = DPAA_FD_GET_ADDR(frame); + if (buf == NULL) + return (e_RX_STORE_RESPONSE_CONTINUE); - /* - * We are storing struct dtsec_rm_frame_info in first entry - * of scatter-gather table. - */ - sgt0 = DPAA_FD_GET_ADDR(frame); - fi = DPAA_SGTE_GET_ADDR(sgt0); - - /* Free transmitted frame */ - m_freem(fi->fi_mbuf); - dtsec_rm_fi_free(sc, fi); - - qlen = qman_fqr_get_counter(sc->sc_tx_conf_fqr, 0, - e_QM_FQR_COUNTERS_FRAME); - - if (qlen == 0) { - DTSEC_LOCK(sc); - - if (sc->sc_tx_fqr_full) { - sc->sc_tx_fqr_full = 0; - dtsec_rm_if_start_locked(sc); - } - - DTSEC_UNLOCK(sc); + if (DPAA_FD_GET_FORMAT(frame) == e_DPAA_FD_FORMAT_TYPE_SHORT_MBSF && + DPAA_FD_GET_OFFSET(frame) > 0) { + /* SG+prefix FD (checksum offload path). + * fi pointer stored in private data area at start of buf. + * SG table buffer allocated from sc_sgt_zone. */ + fi = *(struct dtsec_rm_frame_info **)buf; + cpu = fi->fi_cpu; + m_freem(fi->fi_mbuf); + uma_zfree(sc->sc_sgt_zone, buf); + dtsec_rm_fi_free(sc, fi); + } else { + /* Legacy SG FD (no prefix, offset=0). + * fi pointer stored in first SG entry. */ + sgt0 = buf; + fi = DPAA_SGTE_GET_ADDR(sgt0); + cpu = fi->fi_cpu; + m_freem(fi->fi_mbuf); + dtsec_rm_fi_free(sc, fi); } + atomic_subtract_int(&sc->sc_tx_inflight[cpu], 1); + return (e_RX_STORE_RESPONSE_CONTINUE); } void dtsec_rm_fqr_rx_free(struct dtsec_softc *sc) { + int i; - if (sc->sc_rx_fqr) - qman_fqr_free(sc->sc_rx_fqr); + for (i = 0; i < DTSEC_RX_QUEUES; i++) { + if (sc->sc_rx_fqr[i] != NULL) { + qman_fqr_free(sc->sc_rx_fqr[i]); + sc->sc_rx_fqr[i] = NULL; + } + } +} + +/* + * dtsec_release_rss_fqrs — Free RSS FQRs 1..127 from all dtsec ports. + * + * Called by CDX module during takeover, before creating CDX distribution + * FQRs. After CDX replaces the driver's PCD, the RSS FQRs are orphaned + * (no KeyGen scheme directs traffic to them). FQR 0 (base FQID) is kept + * alive — CDX miss-action routes unmatched frames there for stack delivery. + * + * Safe to call at boot before any traffic has flowed (instant, no drain). + */ +void +dtsec_release_rss_fqrs(void) +{ + devclass_t dc; + device_t dev; + struct dtsec_softc *sc; + int i, unit; + + dc = devclass_find("dtsec"); + if (dc == NULL) + return; + + for (unit = 0; unit < devclass_get_maxunit(dc); unit++) { + dev = devclass_get_device(dc, unit); + if (dev == NULL) + continue; + sc = device_get_softc(dev); + if (sc == NULL || sc->sc_rx_fqr[1] == NULL) + continue; + for (i = 1; i < DTSEC_RX_QUEUES; i++) { + if (sc->sc_rx_fqr[i] != NULL) { + qman_fqr_free(sc->sc_rx_fqr[i]); + sc->sc_rx_fqr[i] = NULL; + } + } + device_printf(dev, + "released %d RSS FQRs for CDX (base FQID %u kept)\n", + DTSEC_RX_QUEUES - 1, sc->sc_rx_fqid); + } } int @@ -451,17 +911,103 @@ dtsec_rm_fqr_rx_init(struct dtsec_softc *sc) { t_Error error; t_Handle fqr; + uint32_t base_fqid; + int i; - /* Default Frame Queue */ - fqr = qman_fqr_create(1, DTSEC_RM_FQR_RX_CHANNEL, DTSEC_RM_FQR_RX_WQ, - false, 0, false, false, true, false, 0, 0, 0); +#ifdef __aarch64__ + /* + * RSS via per-CPU dedicated QMan channels (matching Linux). + * + * Create DTSEC_RX_QUEUES (128) single-FQID FQRs with contiguous + * FQIDs, round-robin assigned to dedicated SW portal channels: + * FQID base+0 -> SWPORTAL0, base+1 -> SWPORTAL1, ... + * base+4 -> SWPORTAL0, base+5 -> SWPORTAL1, etc. + * + * KeyGen hashes 4-tuple -> baseFqid + (hash & 127). With a + * 7-bit hash mask, different flows distribute well across CPUs. + * Same flow always hashes to same FQID -> same CPU (no reorder). + * + * The first FQR is allocated with alignment = DTSEC_RX_QUEUES + * to satisfy the KeyGen alignment requirement. The remaining + * 127 FQRs use force_fqid for contiguous FQIDs. + */ + + /* FQID 0: allocate with alignment to get contiguous base */ + fqr = qman_fqr_create(1, + (e_QmFQChannel)(e_QM_FQ_CHANNEL_SWPORTAL0), + DTSEC_RM_FQR_RX_WQ, false, + DTSEC_RX_QUEUES, /* alignment */ + false, false, true, false, 0, 0, 0); if (fqr == NULL) { - device_printf(sc->sc_dev, "could not create default RX queue" - "\n"); + device_printf(sc->sc_dev, + "could not create aligned RX FQR for FQID 0\n"); return (EIO); } - sc->sc_rx_fqr = fqr; + sc->sc_rx_fqr[0] = fqr; + base_fqid = qman_fqr_get_base_fqid(fqr); + sc->sc_rx_fqid = base_fqid; + + error = qman_fqr_register_cb(fqr, dtsec_rm_fqr_rx_callback, sc); + if (error != E_OK) { + device_printf(sc->sc_dev, + "could not register RX callback for FQID 0\n"); + qman_fqr_free(fqr); + sc->sc_rx_fqr[0] = NULL; + return (EIO); + } + + /* FQIDs 1..N-1: force-allocate, round-robin across CPUs */ + for (i = 1; i < DTSEC_RX_QUEUES; i++) { + e_QmFQChannel channel = + (e_QmFQChannel)(e_QM_FQ_CHANNEL_SWPORTAL0 + + (i % DTSEC_TX_NUM_FQS)); + + fqr = qman_fqr_create(1, channel, DTSEC_RM_FQR_RX_WQ, + true, base_fqid + i, + false, false, true, false, 0, 0, 0); + if (fqr == NULL) { + device_printf(sc->sc_dev, + "could not create RX FQR %d " + "(FQID %u, channel %d)\n", + i, base_fqid + i, (int)channel); + goto fail; + } + + sc->sc_rx_fqr[i] = fqr; + + error = qman_fqr_register_cb(fqr, + dtsec_rm_fqr_rx_callback, sc); + if (error != E_OK) { + device_printf(sc->sc_dev, + "could not register RX callback for FQ %d\n", i); + qman_fqr_free(fqr); + sc->sc_rx_fqr[i] = NULL; + goto fail; + } + } + + device_printf(sc->sc_dev, + "RX RSS: base FQID %u, %d queues across %d CPUs\n", + base_fqid, DTSEC_RX_QUEUES, DTSEC_TX_NUM_FQS); + + return (0); + +fail: + dtsec_rm_fqr_rx_free(sc); + return (EIO); + +#else /* !__aarch64__ — PowerPC: single FQR on pool channel */ + fqr = qman_fqr_create(DTSEC_RX_QUEUES, DTSEC_RM_FQR_RX_CHANNEL, + DTSEC_RM_FQR_RX_WQ, false, + DTSEC_RX_QUEUES > 1 ? DTSEC_RX_QUEUES : 0, + false, false, true, false, 0, 0, 0); + if (fqr == NULL) { + device_printf(sc->sc_dev, "could not create RX queue range\n"); + return (EIO); + } + + sc->sc_rx_fqr[0] = fqr; sc->sc_rx_fqid = qman_fqr_get_base_fqid(fqr); error = qman_fqr_register_cb(fqr, dtsec_rm_fqr_rx_callback, sc); @@ -471,18 +1017,30 @@ dtsec_rm_fqr_rx_init(struct dtsec_softc *sc) return (EIO); } + if (bootverbose) + device_printf(sc->sc_dev, "RX FQR: base FQID %u, %d queues\n", + sc->sc_rx_fqid, DTSEC_RX_QUEUES); + return (0); +#endif } void dtsec_rm_fqr_tx_free(struct dtsec_softc *sc) { + int i; - if (sc->sc_tx_fqr) - qman_fqr_free(sc->sc_tx_fqr); + for (i = 0; i < DTSEC_TX_NUM_FQS; i++) { + if (sc->sc_tx_fqs[i] != NULL) { + qman_fqr_free(sc->sc_tx_fqs[i]); + sc->sc_tx_fqs[i] = NULL; + } + } - if (sc->sc_tx_conf_fqr) + if (sc->sc_tx_conf_fqr != NULL) { qman_fqr_free(sc->sc_tx_conf_fqr); + sc->sc_tx_conf_fqr = NULL; + } } int @@ -490,19 +1048,24 @@ dtsec_rm_fqr_tx_init(struct dtsec_softc *sc) { t_Error error; t_Handle fqr; + int i; - /* TX Frame Queue */ - fqr = qman_fqr_create(1, sc->sc_port_tx_qman_chan, - DTSEC_RM_FQR_TX_WQ, false, 0, false, false, true, false, 0, 0, 0); - if (fqr == NULL) { - device_printf(sc->sc_dev, "could not create default TX queue" - "\n"); - return (EIO); + /* Per-CPU TX Frame Queues — all target the same FMan TX port channel */ + for (i = 0; i < DTSEC_TX_NUM_FQS; i++) { + fqr = qman_fqr_create(1, sc->sc_port_tx_qman_chan, + DTSEC_RM_FQR_TX_WQ, false, 0, false, false, true, false, + 0, 0, 0); + if (fqr == NULL) { + device_printf(sc->sc_dev, + "could not create TX queue for CPU %d\n", i); + dtsec_rm_fqr_tx_free(sc); + return (EIO); + } + sc->sc_tx_fqs[i] = fqr; + sc->sc_tx_inflight[i] = 0; } - sc->sc_tx_fqr = fqr; - - /* TX Confirmation Frame Queue */ + /* TX Confirmation Frame Queue (single, on pool channel) */ fqr = qman_fqr_create(1, DTSEC_RM_FQR_TX_CONF_CHANNEL, DTSEC_RM_FQR_TX_CONF_WQ, false, 0, false, false, true, false, 0, 0, 0); @@ -524,129 +1087,613 @@ dtsec_rm_fqr_tx_init(struct dtsec_softc *sc) return (EIO); } + if (bootverbose) + device_printf(sc->sc_dev, + "TX FQs: %d per-CPU queues, confirm FQID %u\n", + DTSEC_TX_NUM_FQS, sc->sc_tx_conf_fqid); + return (0); } /** @} */ +/** + * @group dTSEC PCD (Parse-Classify-Distribute) routines. + * @{ + */ + +/** + * Initialize PCD on the RX port: create a KeyGen scheme for RSS + * hash distribution across DTSEC_RX_QUEUES, then attach PCD + * (Parser + KeyGen) to the RX port. + * + * Called from dtsec_attach() after RX port and FQR are initialized. + * Non-fatal: returns 0 on success or if PCD is unavailable. + */ +int +dtsec_rm_pcd_init(struct dtsec_softc *sc) +{ + t_FmPcdKgSchemeParams scheme; + t_FmPortPcdParams port_pcd; + t_FmPortPcdPrsParams prs; + t_FmPortPcdKgParams kg; + t_Error error; + int i; + + if (sc->sc_pcdh == NULL) + return (0); + + /* --- Create KeyGen scheme --- */ + memset(&scheme, 0, sizeof(scheme)); + scheme.modify = FALSE; + scheme.id.relativeSchemeId = device_get_unit(sc->sc_dev); + scheme.alwaysDirect = TRUE; + scheme.useHash = TRUE; + scheme.bypassFqidGeneration = FALSE; + scheme.baseFqid = sc->sc_rx_fqid; + scheme.nextEngine = e_FM_PCD_DONE; + scheme.kgNextEngineParams.doneAction = e_FM_PCD_ENQ_FRAME; + + /* + * Extract 4-tuple for hash: IPv4 SRC/DST + L4 SRC/DST port. + * Using HEADER_TYPE_TCP for L4 ports — the KeyGen's known-field + * registers (KG_SCH_KN_L4PSRC/L4PDST) are protocol-agnostic and + * extract L4 ports for both TCP and UDP. For non-IP/non-L4 + * frames, the default value (zeros) is used, so they all hash + * to baseFqid + 0. + */ + i = 0; + + /* IPv4 source IP */ + scheme.keyExtractAndHashParams.extractArray[i].type = + e_FM_PCD_EXTRACT_BY_HDR; + scheme.keyExtractAndHashParams.extractArray[i].extractByHdr.hdr = + HEADER_TYPE_IPv4; + scheme.keyExtractAndHashParams.extractArray[i].extractByHdr.hdrIndex = + e_FM_PCD_HDR_INDEX_NONE; + scheme.keyExtractAndHashParams.extractArray[i].extractByHdr + .ignoreProtocolValidation = FALSE; + scheme.keyExtractAndHashParams.extractArray[i].extractByHdr.type = + e_FM_PCD_EXTRACT_FULL_FIELD; + scheme.keyExtractAndHashParams.extractArray[i].extractByHdr + .extractByHdrType.fullField.ipv4 = NET_HEADER_FIELD_IPv4_SRC_IP; + i++; + + /* IPv4 destination IP */ + scheme.keyExtractAndHashParams.extractArray[i].type = + e_FM_PCD_EXTRACT_BY_HDR; + scheme.keyExtractAndHashParams.extractArray[i].extractByHdr.hdr = + HEADER_TYPE_IPv4; + scheme.keyExtractAndHashParams.extractArray[i].extractByHdr.hdrIndex = + e_FM_PCD_HDR_INDEX_NONE; + scheme.keyExtractAndHashParams.extractArray[i].extractByHdr + .ignoreProtocolValidation = FALSE; + scheme.keyExtractAndHashParams.extractArray[i].extractByHdr.type = + e_FM_PCD_EXTRACT_FULL_FIELD; + scheme.keyExtractAndHashParams.extractArray[i].extractByHdr + .extractByHdrType.fullField.ipv4 = NET_HEADER_FIELD_IPv4_DST_IP; + i++; + + /* TCP/UDP source port (KG_SCH_KN_L4PSRC covers both TCP and UDP) */ + scheme.keyExtractAndHashParams.extractArray[i].type = + e_FM_PCD_EXTRACT_BY_HDR; + scheme.keyExtractAndHashParams.extractArray[i].extractByHdr.hdr = + HEADER_TYPE_TCP; + scheme.keyExtractAndHashParams.extractArray[i].extractByHdr.hdrIndex = + e_FM_PCD_HDR_INDEX_NONE; + scheme.keyExtractAndHashParams.extractArray[i].extractByHdr + .ignoreProtocolValidation = FALSE; + scheme.keyExtractAndHashParams.extractArray[i].extractByHdr.type = + e_FM_PCD_EXTRACT_FULL_FIELD; + scheme.keyExtractAndHashParams.extractArray[i].extractByHdr + .extractByHdrType.fullField.tcp = NET_HEADER_FIELD_TCP_PORT_SRC; + i++; + + /* TCP/UDP destination port (KG_SCH_KN_L4PDST covers both TCP and UDP) */ + scheme.keyExtractAndHashParams.extractArray[i].type = + e_FM_PCD_EXTRACT_BY_HDR; + scheme.keyExtractAndHashParams.extractArray[i].extractByHdr.hdr = + HEADER_TYPE_TCP; + scheme.keyExtractAndHashParams.extractArray[i].extractByHdr.hdrIndex = + e_FM_PCD_HDR_INDEX_NONE; + scheme.keyExtractAndHashParams.extractArray[i].extractByHdr + .ignoreProtocolValidation = FALSE; + scheme.keyExtractAndHashParams.extractArray[i].extractByHdr.type = + e_FM_PCD_EXTRACT_FULL_FIELD; + scheme.keyExtractAndHashParams.extractArray[i].extractByHdr + .extractByHdrType.fullField.tcp = NET_HEADER_FIELD_TCP_PORT_DST; + i++; + + scheme.keyExtractAndHashParams.numOfUsedExtracts = i; + scheme.keyExtractAndHashParams.hashDistributionNumOfFqids = + DTSEC_RX_QUEUES; + scheme.keyExtractAndHashParams.hashShift = 0; + scheme.keyExtractAndHashParams.hashDistributionFqidsShift = 0; + scheme.keyExtractAndHashParams.symmetricHash = FALSE; + + /* Defaults for missing headers: use privateDflt0 (all zeros) */ + scheme.keyExtractAndHashParams.numOfUsedDflts = 2; + scheme.keyExtractAndHashParams.dflts[0].type = e_FM_PCD_KG_IP_ADDR; + scheme.keyExtractAndHashParams.dflts[0].dfltSelect = + e_FM_PCD_KG_DFLT_PRIVATE_0; + scheme.keyExtractAndHashParams.dflts[1].type = e_FM_PCD_KG_L4_PORT; + scheme.keyExtractAndHashParams.dflts[1].dfltSelect = + e_FM_PCD_KG_DFLT_PRIVATE_0; + + sc->sc_scheme = FM_PCD_KgSchemeSet(sc->sc_pcdh, &scheme); + if (sc->sc_scheme == NULL) { + device_printf(sc->sc_dev, "couldn't create KeyGen scheme\n"); + return (ENXIO); + } + + device_printf(sc->sc_dev, + "KeyGen scheme created: baseFqid=%u, queues=%d\n", + sc->sc_rx_fqid, DTSEC_RX_QUEUES); + + /* --- Attach PCD to RX port --- */ + memset(&port_pcd, 0, sizeof(port_pcd)); + port_pcd.pcdSupport = e_FM_PORT_PCD_SUPPORT_PRS_AND_KG; + port_pcd.h_NetEnv = sc->sc_netenvh; + + memset(&prs, 0, sizeof(prs)); + prs.firstPrsHdr = HEADER_TYPE_ETH; + port_pcd.p_PrsParams = &prs; + + memset(&kg, 0, sizeof(kg)); + kg.numOfSchemes = 1; + kg.h_Schemes[0] = sc->sc_scheme; + kg.directScheme = TRUE; + kg.h_DirectScheme = sc->sc_scheme; + port_pcd.p_KgParams = &kg; + + error = FM_PORT_SetPCD(sc->sc_rxph, &port_pcd); + if (error != E_OK) { + device_printf(sc->sc_dev, + "couldn't set PCD on RX port: %d\n", error); + FM_PCD_KgSchemeDelete(sc->sc_scheme); + sc->sc_scheme = NULL; + return (ENXIO); + } + + device_printf(sc->sc_dev, "PCD enabled: PRS+KG on RX port\n"); + + + /* Register scheme with chardev so it can tear down PCD when + * userspace (FMC/dpa_app) takes over PCD management. */ + { + struct fmcd_softc *fmcd; + + fmcd = fman_get_fmcd(device_get_parent(sc->sc_dev)); + if (fmcd != NULL) + fmcd_register_rx_port_scheme(fmcd, + sc->sc_mac_cell_index, + sc->sc_scheme, &sc->sc_scheme); + } + + return (0); +} + +void +dtsec_rm_pcd_free(struct dtsec_softc *sc) +{ + + if (sc->sc_scheme != NULL) { + FM_PORT_DeletePCD(sc->sc_rxph); + FM_PCD_KgSchemeDelete(sc->sc_scheme); + sc->sc_scheme = NULL; + } +} +/** @} */ + + +/** + * Fill parse result for TX checksum offload. + * Returns 0 on success, non-zero if the frame can't be checksummed by HW. + */ +static int +dtsec_rm_tx_csum_fill(struct dtsec_softc *sc, struct mbuf *m, + t_FmPrsResult *prs) +{ + struct ether_header *eh; + struct ip *ip; + struct ip6_hdr *ip6; + uint16_t etype; + int ehlen; + + if (m->m_len < sizeof(*eh)) + return (EINVAL); + + eh = mtod(m, struct ether_header *); + etype = ntohs(eh->ether_type); + ehlen = sizeof(*eh); + + /* Handle VLAN-tagged frames */ + if (etype == ETHERTYPE_VLAN) { + if (m->m_len < sizeof(*eh) + 4) + return (EINVAL); + etype = ntohs(*(uint16_t *)((char *)eh + sizeof(*eh) + 2)); + ehlen += 4; + } + + memset(prs, 0, sizeof(*prs)); + + switch (etype) { + case ETHERTYPE_IP: + if (m->m_len < ehlen + sizeof(*ip)) + return (EINVAL); + prs->l3r = htobe16(FM_L3_PARSE_RESULT_IPV4); + ip = (struct ip *)((char *)eh + ehlen); + prs->ip_off[0] = ehlen; + prs->l4_off = ehlen + (ip->ip_hl << 2); + switch (ip->ip_p) { + case IPPROTO_TCP: + prs->l4r = FM_L4_PARSE_RESULT_TCP; + break; + case IPPROTO_UDP: + prs->l4r = FM_L4_PARSE_RESULT_UDP; + break; + default: + return (EINVAL); + } + break; + + case ETHERTYPE_IPV6: + if (m->m_len < ehlen + sizeof(*ip6)) + return (EINVAL); + prs->l3r = htobe16(FM_L3_PARSE_RESULT_IPV6); + ip6 = (struct ip6_hdr *)((char *)eh + ehlen); + prs->ip_off[0] = ehlen; + prs->l4_off = ehlen + sizeof(*ip6); + switch (ip6->ip6_nxt) { + case IPPROTO_TCP: + prs->l4r = FM_L4_PARSE_RESULT_TCP; + break; + case IPPROTO_UDP: + prs->l4r = FM_L4_PARSE_RESULT_UDP; + break; + default: + return (EINVAL); + } + break; + + default: + return (EINVAL); + } + + return (0); +} + /** * @group dTSEC IFnet routines. * @{ */ -void -dtsec_rm_if_start_locked(struct dtsec_softc *sc) + +/** + * Extract DSCP from the IP header of an Ethernet frame. + * Returns 0 for non-IP or unparseable frames (DSCP 0 maps to + * unmapped in the CEETM array, falling back to the default FQ). + */ +static inline uint8_t +dtsec_rm_extract_dscp(struct mbuf *m) { - vm_size_t dsize, psize, ssize; + struct ether_header *eh; + uint16_t etype; + int ehlen; + + if (__predict_false(m->m_len < (int)sizeof(*eh))) + return (0); + + eh = mtod(m, struct ether_header *); + etype = ntohs(eh->ether_type); + ehlen = sizeof(*eh); + + if (etype == ETHERTYPE_VLAN) { + if (__predict_false(m->m_len < ehlen + 4)) + return (0); + etype = ntohs(*(uint16_t *)((char *)eh + ehlen + 2)); + ehlen += 4; + } + + if (etype == ETHERTYPE_IP) { + struct ip *ip; + if (__predict_false(m->m_len < ehlen + (int)sizeof(*ip))) + return (0); + ip = (struct ip *)((char *)eh + ehlen); + return ((ip->ip_tos >> 2) & 0x3f); + } + + if (etype == ETHERTYPE_IPV6) { + struct ip6_hdr *ip6; + if (__predict_false(m->m_len < ehlen + (int)sizeof(*ip6))) + return (0); + ip6 = (struct ip6_hdr *)((char *)eh + ehlen); + return ((ntohl(ip6->ip6_flow) >> 22) & 0x3f); + } + + return (0); +} + +/** + * Modern if_transmit entry point for RM (Regular Mode). + * + * Each CPU builds the FD and enqueues to its own per-CPU TX FQ on its + * own QMan portal. No locks, no sendq, no contention. The stack + * calls this directly with one mbuf at a time. + */ +int +dtsec_rm_if_transmit(if_t ifp, struct mbuf *m0) +{ + struct dtsec_softc *sc; struct dtsec_rm_frame_info *fi; - unsigned int qlen, i; - struct mbuf *m0, *m; + vm_size_t dsize, psize, ssize; + unsigned int i; + struct mbuf *m; vm_offset_t vaddr; t_DpaaFD fd; + int cpu, error; + t_DpaaSGTE *sgt; + void *sgt_buf; - DTSEC_LOCK_ASSERT(sc); - /* TODO: IFF_DRV_OACTIVE */ + sc = if_getsoftc(ifp); - if ((sc->sc_mii->mii_media_status & IFM_ACTIVE) == 0) - return; + if (__predict_false(!(if_getdrvflags(ifp) & IFF_DRV_RUNNING))) { + m_freem(m0); + return (ENETDOWN); + } - if ((if_getdrvflags(sc->sc_ifnet) & IFF_DRV_RUNNING) != IFF_DRV_RUNNING) - return; + /* Pin to current CPU for consistent FQ selection and portal use */ + sched_pin(); + cpu = curcpu % DTSEC_TX_NUM_FQS; - while (!if_sendq_empty(sc->sc_ifnet)) { - /* Check length of the TX queue */ - qlen = qman_fqr_get_counter(sc->sc_tx_fqr, 0, - e_QM_FQR_COUNTERS_FRAME); + /* CEETM DSCP-based FQ selection for software-path packets */ + uint32_t ceetm_fqid = 0; + if (__predict_false(sc->sc_ceetm_en)) { + uint8_t dscp = dtsec_rm_extract_dscp(m0); + ceetm_fqid = sc->sc_ceetm_dscp_fqid[dscp]; + } - if (qlen >= DTSEC_MAX_TX_QUEUE_LEN) { - sc->sc_tx_fqr_full = 1; - return; + /* Check per-CPU in-flight limit (software counter, no portal read) */ + if (__predict_false(atomic_load_int(&sc->sc_tx_inflight[cpu]) >= + DTSEC_MAX_TX_QUEUE_LEN)) { + sched_unpin(); + m_freem(m0); + if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1); + return (ENOBUFS); + } + + fi = dtsec_rm_fi_alloc(sc); + if (__predict_false(fi == NULL)) { + sched_unpin(); + m_freem(m0); + if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); + return (ENOBUFS); + } + + fi->fi_cpu = cpu; + + fi->fi_tx_buf = NULL; + + /* + * TX checksum offload: build SG FD with buffer prefix + * containing parse result. SG entries point directly to + * mbuf fragment data — zero-copy, no linearization. + */ + if ((m0->m_pkthdr.csum_flags & + (CSUM_IP | CSUM_IP_TCP | CSUM_IP_UDP | + CSUM_IP6_TCP | CSUM_IP6_UDP)) && + sc->sc_tx_data_offset > 0) { + t_FmPrsResult prs; + t_FmPrsResult *buf_prs; + + /* Fill parse result for hardware checksum */ + if (dtsec_rm_tx_csum_fill(sc, m0, &prs) != 0) { + /* Can't offload L4 (e.g. ICMP) — compute + * IP header checksum in software since the + * stack left ip_sum=0 relying on CSUM_IP. */ + if (m0->m_pkthdr.csum_flags & CSUM_IP) { + struct ether_header *eh2; + struct ip *ip2; + uint16_t et; + int ehl; + + eh2 = mtod(m0, struct ether_header *); + et = ntohs(eh2->ether_type); + ehl = sizeof(*eh2); + if (et == ETHERTYPE_VLAN) + ehl += 4; + if (m0->m_len >= ehl + (int)sizeof(*ip2)) { + ip2 = (struct ip *) + (mtod(m0, char *) + ehl); + ip2->ip_sum = 0; + ip2->ip_sum = in_cksum_hdr(ip2); + } + } + m0->m_pkthdr.csum_flags = 0; + goto tx_sg_path; } - fi = dtsec_rm_fi_alloc(sc); - if (fi == NULL) - return; - - m0 = if_dequeue(sc->sc_ifnet); - if (m0 == NULL) { + /* Allocate SG table buffer: [prefix | SG entries] */ + sgt_buf = uma_zalloc(sc->sc_sgt_zone, M_NOWAIT); + if (sgt_buf == NULL) { + sched_unpin(); dtsec_rm_fi_free(sc, fi); - return; + m_freem(m0); + if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); + return (ENOBUFS); } + memset(sgt_buf, 0, sc->sc_sgt_buf_size); + /* Store fi pointer in private data area (offset 0) */ + *(struct dtsec_rm_frame_info **)sgt_buf = fi; + + /* Copy parse result into prefix */ + buf_prs = FM_PORT_GetBufferPrsResult( + sc->sc_txph, (char *)sgt_buf); + if (buf_prs != NULL) + memcpy(buf_prs, &prs, sizeof(prs)); + + /* Build SG entries at sgt_buf + tx_data_offset */ + sgt = (t_DpaaSGTE *)((char *)sgt_buf + + sc->sc_tx_data_offset); i = 0; - m = m0; psize = 0; - dsize = 0; - fi->fi_mbuf = m0; - while (m && i < DPAA_NUM_OF_SG_TABLE_ENTRY) { + for (m = m0; m != NULL; m = m->m_next) { if (m->m_len == 0) continue; - - /* - * First entry in scatter-gather table is used to keep - * pointer to frame info structure. - */ - DPAA_SGTE_SET_ADDR(&fi->fi_sgt[i], (void *)fi); - DPAA_SGTE_SET_LENGTH(&fi->fi_sgt[i], 0); - - DPAA_SGTE_SET_EXTENSION(&fi->fi_sgt[i], 0); - DPAA_SGTE_SET_FINAL(&fi->fi_sgt[i], 0); - DPAA_SGTE_SET_BPID(&fi->fi_sgt[i], 0); - DPAA_SGTE_SET_OFFSET(&fi->fi_sgt[i], 0); - i++; - dsize = m->m_len; vaddr = (vm_offset_t)m->m_data; - while (dsize > 0 && i < DPAA_NUM_OF_SG_TABLE_ENTRY) { - ssize = PAGE_SIZE - (vaddr & PAGE_MASK); - if (m->m_len < ssize) - ssize = m->m_len; + while (dsize > 0 && + i < DPAA_NUM_OF_SG_TABLE_ENTRY) { + ssize = PAGE_SIZE - + (vaddr & PAGE_MASK); + if (ssize > dsize) + ssize = dsize; - DPAA_SGTE_SET_ADDR(&fi->fi_sgt[i], + DPAA_SGTE_SET_ADDR(&sgt[i], (void *)vaddr); - DPAA_SGTE_SET_LENGTH(&fi->fi_sgt[i], ssize); - - DPAA_SGTE_SET_EXTENSION(&fi->fi_sgt[i], 0); - DPAA_SGTE_SET_FINAL(&fi->fi_sgt[i], 0); - DPAA_SGTE_SET_BPID(&fi->fi_sgt[i], 0); - DPAA_SGTE_SET_OFFSET(&fi->fi_sgt[i], 0); + DPAA_SGTE_SET_LENGTH(&sgt[i], ssize); + DPAA_SGTE_SET_EXTENSION(&sgt[i], 0); + DPAA_SGTE_SET_FINAL(&sgt[i], 0); + DPAA_SGTE_SET_BPID(&sgt[i], 0); + DPAA_SGTE_SET_OFFSET(&sgt[i], 0); dsize -= ssize; vaddr += ssize; psize += ssize; i++; } - if (dsize > 0) break; - - m = m->m_next; } - /* Check if SG table was constructed properly */ - if (m != NULL || dsize != 0) { + if (m != NULL || i == 0) { + /* SG table overflow or empty — drop */ + sched_unpin(); + uma_zfree(sc->sc_sgt_zone, sgt_buf); dtsec_rm_fi_free(sc, fi); m_freem(m0); - continue; + if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); + return (EIO); } - DPAA_SGTE_SET_FINAL(&fi->fi_sgt[i-1], 1); + DPAA_SGTE_SET_FINAL(&sgt[i - 1], 1); - DPAA_FD_SET_ADDR(&fd, fi->fi_sgt); + fi->fi_mbuf = m0; + fi->fi_tx_buf = sgt_buf; + + memset(&fd, 0, sizeof(fd)); + DPAA_FD_SET_ADDR(&fd, sgt_buf); DPAA_FD_SET_LENGTH(&fd, psize); - DPAA_FD_SET_FORMAT(&fd, e_DPAA_FD_FORMAT_TYPE_SHORT_MBSF); + DPAA_FD_SET_FORMAT(&fd, + e_DPAA_FD_FORMAT_TYPE_SHORT_MBSF); + DPAA_FD_SET_OFFSET(&fd, sc->sc_tx_data_offset); + DPAA_FD_SET_STATUS(&fd, + FM_FD_CMD_RPD | FM_FD_CMD_DTC); - fd.liodn = 0; - fd.bpid = 0; - fd.elion = 0; - DPAA_FD_SET_OFFSET(&fd, 0); - DPAA_FD_SET_STATUS(&fd, 0); - - DTSEC_UNLOCK(sc); - if (qman_fqr_enqueue(sc->sc_tx_fqr, 0, &fd) != E_OK) { + if (ceetm_fqid) + error = qman_enqueue_fqid(ceetm_fqid, &fd); + else + error = qman_fqr_enqueue(sc->sc_tx_fqs[cpu], + 0, &fd); + if (__predict_false(error != E_OK)) { + sched_unpin(); + if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); + uma_zfree(sc->sc_sgt_zone, fi->fi_tx_buf); dtsec_rm_fi_free(sc, fi); m_freem(m0); + return (EIO); } - DTSEC_LOCK(sc); + + atomic_add_int(&sc->sc_tx_inflight[cpu], 1); + sched_unpin(); + if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); + if_inc_counter(ifp, IFCOUNTER_OBYTES, psize); + return (0); } +tx_sg_path: + + /* + * Legacy SG path (no checksum offload). + * First SG entry stores fi self-reference for TX confirm. + */ + i = 0; + psize = 0; + fi->fi_mbuf = m0; + + /* SGT[0]: fi pointer (metadata, zero-length) */ + DPAA_SGTE_SET_ADDR(&fi->fi_sgt[0], (void *)fi); + DPAA_SGTE_SET_LENGTH(&fi->fi_sgt[0], 0); + DPAA_SGTE_SET_EXTENSION(&fi->fi_sgt[0], 0); + DPAA_SGTE_SET_FINAL(&fi->fi_sgt[0], 0); + DPAA_SGTE_SET_BPID(&fi->fi_sgt[0], 0); + DPAA_SGTE_SET_OFFSET(&fi->fi_sgt[0], 0); + i = 1; + + /* SGT[1..N]: data entries from mbuf chain */ + for (m = m0; m != NULL && i < DPAA_NUM_OF_SG_TABLE_ENTRY; + m = m->m_next) { + if (m->m_len == 0) + continue; + + dsize = m->m_len; + vaddr = (vm_offset_t)m->m_data; + while (dsize > 0 && i < DPAA_NUM_OF_SG_TABLE_ENTRY) { + ssize = PAGE_SIZE - (vaddr & PAGE_MASK); + if (ssize > dsize) + ssize = dsize; + + DPAA_SGTE_SET_ADDR(&fi->fi_sgt[i], + (void *)vaddr); + DPAA_SGTE_SET_LENGTH(&fi->fi_sgt[i], ssize); + DPAA_SGTE_SET_EXTENSION(&fi->fi_sgt[i], 0); + DPAA_SGTE_SET_FINAL(&fi->fi_sgt[i], 0); + DPAA_SGTE_SET_BPID(&fi->fi_sgt[i], 0); + DPAA_SGTE_SET_OFFSET(&fi->fi_sgt[i], 0); + + dsize -= ssize; + vaddr += ssize; + psize += ssize; + i++; + } + + if (dsize > 0) + break; + } + + /* Check if SG table was constructed properly */ + if (m != NULL || psize == 0) { + sched_unpin(); + dtsec_rm_fi_free(sc, fi); + m_freem(m0); + if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); + return (EIO); + } + + DPAA_SGTE_SET_FINAL(&fi->fi_sgt[i - 1], 1); + + memset(&fd, 0, sizeof(fd)); + DPAA_FD_SET_ADDR(&fd, fi->fi_sgt); + DPAA_FD_SET_LENGTH(&fd, psize); + DPAA_FD_SET_FORMAT(&fd, e_DPAA_FD_FORMAT_TYPE_SHORT_MBSF); + DPAA_FD_SET_OFFSET(&fd, 0); + DPAA_FD_SET_STATUS(&fd, 0); + + if (ceetm_fqid) + error = qman_enqueue_fqid(ceetm_fqid, &fd); + else + error = qman_fqr_enqueue(sc->sc_tx_fqs[cpu], 0, &fd); + if (__predict_false(error != E_OK)) { + sched_unpin(); + if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); + dtsec_rm_fi_free(sc, fi); + m_freem(m0); + return (EIO); + } + + atomic_add_int(&sc->sc_tx_inflight[cpu], 1); + sched_unpin(); + if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); + if_inc_counter(ifp, IFCOUNTER_OBYTES, psize); + return (0); } /** @} */ diff --git a/sys/dev/dpaa/if_dtsec_rm.h b/sys/dev/dpaa/if_dtsec_rm.h index 28f779a1138..97a9ceb4be3 100644 --- a/sys/dev/dpaa/if_dtsec_rm.h +++ b/sys/dev/dpaa/if_dtsec_rm.h @@ -34,7 +34,7 @@ int dtsec_rm_fm_port_rx_init(struct dtsec_softc *sc, int unit); int dtsec_rm_fm_port_tx_init(struct dtsec_softc *sc, int unit); -void dtsec_rm_if_start_locked(struct dtsec_softc *sc); +int dtsec_rm_if_transmit(if_t ifp, struct mbuf *m); int dtsec_rm_pool_rx_init(struct dtsec_softc *sc); void dtsec_rm_pool_rx_free(struct dtsec_softc *sc); @@ -46,6 +46,10 @@ int dtsec_rm_fqr_rx_init(struct dtsec_softc *sc); int dtsec_rm_fqr_tx_init(struct dtsec_softc *sc); void dtsec_rm_fqr_rx_free(struct dtsec_softc *sc); void dtsec_rm_fqr_tx_free(struct dtsec_softc *sc); +void dtsec_release_rss_fqrs(void); + +int dtsec_rm_pcd_init(struct dtsec_softc *sc); +void dtsec_rm_pcd_free(struct dtsec_softc *sc); /** @} */ #endif /* IF_DTSEC_RM_H_ */ diff --git a/sys/dev/dpaa/memac_mdio.c b/sys/dev/dpaa/memac_mdio.c new file mode 100644 index 00000000000..cda5daae448 --- /dev/null +++ b/sys/dev/dpaa/memac_mdio.c @@ -0,0 +1,328 @@ +/*- + * Copyright (c) 2026 Mono Technologies Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* + * mEMAC external MDIO controller driver for FManv3 (fsl,fman-memac-mdio). + * + * On LS1046A, external PHYs are accessed through dedicated MDIO controllers + * (mdio@fc000, mdio@fd000) rather than through each MAC's internal MDIO. + * Register layout matches NXP's "xgmac_mdio" Linux driver. + * + * Modeled after fman_mdio.c (pqmdio) which handles the dTSEC MDIO layout. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include +#include +#include +#include + +#include +#include + +#include +#include + +#include "miibus_if.h" + +/* + * MDIO registers at offset 0x30 from the MDIO controller base. + * See struct memac_mii_access_mem_map in fsl_fman_memac_mii_acc.h. + */ +#define MEMAC_MDIO_CFG 0x030 +#define MEMAC_MDIO_CTRL 0x034 +#define MEMAC_MDIO_DATA 0x038 +#define MEMAC_MDIO_ADDR 0x03c + +/* MDIO_CFG bits */ +#define MDIO_CFG_BSY 0x80000000 +#define MDIO_CFG_READ_ERR 0x00000002 +#define MDIO_CFG_ENC45 0x00000040 +#define MDIO_CFG_CLK_DIV_MASK 0x0080ff80 +#define MDIO_CFG_HOLD_MASK 0x0000001c + +/* MDIO_CTRL bits */ +#define MDIO_CTL_READ 0x00008000 +#define MDIO_CTL_PHY_ADDR_SHIFT 5 + +/* MDIO_DATA bits */ +#define MDIO_DATA_BSY 0x80000000 + +#define MDIO_TIMEOUT 1000 /* iterations */ + +struct memac_mdio_softc { + device_t sc_dev; + struct mtx sc_lock; + struct resource *sc_mem; +}; + +/* + * FMan registers are big-endian. bus_read_4/write_4 on ARM64 give + * native (little-endian) access, so we byte-swap. + */ +static inline uint32_t +memac_mdio_read(struct memac_mdio_softc *sc, bus_size_t off) +{ + + return (be32toh(bus_read_4(sc->sc_mem, off))); +} + +static inline void +memac_mdio_write(struct memac_mdio_softc *sc, bus_size_t off, uint32_t val) +{ + + bus_write_4(sc->sc_mem, off, htobe32(val)); +} + +#define MDIO_LOCK() mtx_lock(&sc->sc_lock) +#define MDIO_UNLOCK() mtx_unlock(&sc->sc_lock) + +static int memac_mdio_probe(device_t dev); +static int memac_mdio_attach(device_t dev); +static int memac_mdio_detach(device_t dev); +static int memac_mdio_readreg(device_t dev, int phy, int reg); +static int memac_mdio_writereg(device_t dev, int phy, int reg, int value); + +static device_method_t memac_mdio_methods[] = { + DEVMETHOD(device_probe, memac_mdio_probe), + DEVMETHOD(device_attach, memac_mdio_attach), + DEVMETHOD(device_detach, memac_mdio_detach), + + /* MII interface */ + DEVMETHOD(miibus_readreg, memac_mdio_readreg), + DEVMETHOD(miibus_writereg, memac_mdio_writereg), + + DEVMETHOD_END +}; + +static struct ofw_compat_data memac_mdio_compat[] = { + {"fsl,fman-memac-mdio", 1}, + {NULL, 0} +}; + +static driver_t memac_mdio_driver = { + "memac_mdio", + memac_mdio_methods, + sizeof(struct memac_mdio_softc), +}; + +static int +memac_mdio_wait_free(struct memac_mdio_softc *sc) +{ + int timeout; + + for (timeout = MDIO_TIMEOUT; timeout > 0; timeout--) { + if (!(memac_mdio_read(sc, MEMAC_MDIO_CFG) & MDIO_CFG_BSY)) + return (0); + DELAY(1); + } + return (ETIMEDOUT); +} + +static int +memac_mdio_probe(device_t dev) +{ + + if (!ofw_bus_status_okay(dev)) + return (ENXIO); + + if (!ofw_bus_search_compatible(dev, memac_mdio_compat)->ocd_str) + return (ENXIO); + + device_set_desc(dev, "Freescale mEMAC MDIO Controller"); + + return (BUS_PROBE_DEFAULT); +} + +static int +memac_mdio_attach(device_t dev) +{ + struct memac_mdio_softc *sc; + int rid; + + sc = device_get_softc(dev); + sc->sc_dev = dev; + + rid = 0; + sc->sc_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, + RF_ACTIVE); + if (sc->sc_mem == NULL) { + device_printf(dev, "could not allocate memory resource\n"); + return (ENXIO); + } + + OF_device_register_xref(OF_xref_from_node(ofw_bus_get_node(dev)), dev); + + mtx_init(&sc->sc_lock, device_get_nameunit(dev), + "mEMAC MDIO lock", MTX_DEF); + + return (0); +} + +static int +memac_mdio_detach(device_t dev) +{ + struct memac_mdio_softc *sc; + + sc = device_get_softc(dev); + + if (sc->sc_mem != NULL) + bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem); + + mtx_destroy(&sc->sc_lock); + + return (0); +} + +/* + * Clause 22 (1G) MDIO read. + * Register protocol matches fman_memac_mii_acc.c:read_phy_reg_1g(). + */ +static int +memac_mdio_readreg(device_t dev, int phy, int reg) +{ + struct memac_mdio_softc *sc; + uint32_t cfg, ctl, data; + + sc = device_get_softc(dev); + + MDIO_LOCK(); + + /* Clause 22: clear ENC45, preserve clock divider and hold */ + cfg = memac_mdio_read(sc, MEMAC_MDIO_CFG); + cfg &= (MDIO_CFG_CLK_DIV_MASK | MDIO_CFG_HOLD_MASK); + memac_mdio_write(sc, MEMAC_MDIO_CFG, cfg); + + if (memac_mdio_wait_free(sc) != 0) { + MDIO_UNLOCK(); + return (0xffff); + } + + /* Initiate read: phy in bits [9:5], register in bits [4:0] */ + ctl = ((phy & 0x1f) << MDIO_CTL_PHY_ADDR_SHIFT) | (reg & 0x1f); + ctl |= MDIO_CTL_READ; + memac_mdio_write(sc, MEMAC_MDIO_CTRL, ctl); + + if (memac_mdio_wait_free(sc) != 0) { + MDIO_UNLOCK(); + return (0xffff); + } + + /* Wait for data valid */ + { + int timeout; + for (timeout = MDIO_TIMEOUT; timeout > 0; timeout--) { + data = memac_mdio_read(sc, MEMAC_MDIO_DATA); + if (!(data & MDIO_DATA_BSY)) + break; + DELAY(1); + } + if (timeout <= 0) { + MDIO_UNLOCK(); + return (0xffff); + } + } + + /* Check for read error */ + if (memac_mdio_read(sc, MEMAC_MDIO_CFG) & MDIO_CFG_READ_ERR) { + MDIO_UNLOCK(); + return (0xffff); + } + + MDIO_UNLOCK(); + + return (data & 0xffff); +} + +/* + * Clause 22 (1G) MDIO write. + * Register protocol matches fman_memac_mii_acc.c:write_phy_reg_1g(). + */ +static int +memac_mdio_writereg(device_t dev, int phy, int reg, int value) +{ + struct memac_mdio_softc *sc; + uint32_t cfg, ctl; + + sc = device_get_softc(dev); + + MDIO_LOCK(); + + /* Clause 22: clear ENC45, preserve clock divider and hold */ + cfg = memac_mdio_read(sc, MEMAC_MDIO_CFG); + cfg &= (MDIO_CFG_CLK_DIV_MASK | MDIO_CFG_HOLD_MASK); + memac_mdio_write(sc, MEMAC_MDIO_CFG, cfg); + + if (memac_mdio_wait_free(sc) != 0) { + MDIO_UNLOCK(); + return (EIO); + } + + /* Set phy address and register */ + ctl = ((phy & 0x1f) << MDIO_CTL_PHY_ADDR_SHIFT) | (reg & 0x1f); + memac_mdio_write(sc, MEMAC_MDIO_CTRL, ctl); + + if (memac_mdio_wait_free(sc) != 0) { + MDIO_UNLOCK(); + return (EIO); + } + + /* Write data */ + memac_mdio_write(sc, MEMAC_MDIO_DATA, (uint32_t)(value & 0xffff)); + + /* Wait for write to complete */ + { + int timeout; + for (timeout = MDIO_TIMEOUT; timeout > 0; timeout--) { + if (!(memac_mdio_read(sc, MEMAC_MDIO_DATA) & + MDIO_DATA_BSY)) + break; + DELAY(1); + } + } + + MDIO_UNLOCK(); + + return (0); +} + +/* Probe before dtsec (BUS_PASS_SUPPORTDEV) so sc_mdio lookup succeeds */ +EARLY_DRIVER_MODULE(memac_mdio, fman, memac_mdio_driver, 0, 0, + BUS_PASS_SUPPORTDEV); +DRIVER_MODULE(miibus, memac_mdio, miibus_driver, 0, 0); +MODULE_DEPEND(memac_mdio, miibus, 1, 1, 1);