From 89ce353b4005452578b5a82c9742256ca451c028 Mon Sep 17 00:00:00 2001 From: Tomaz Zaman Date: Wed, 4 Mar 2026 21:16:51 +0100 Subject: [PATCH] dpaa: BMan/QMan/FMan portal integration and bus drivers Add the FreeBSD bus driver wrappers that bridge NCSW to the kernel device tree. BMan and QMan portal drivers handle multi-CPU initialization with explicit CPU binding (sched_bind unavailable during attach). FMan driver configures the frame manager hardware, reserved-memory regions (PFDR/FQD/FBPR via no-map DT nodes), and MURAM allocation. Portal common infrastructure provides shared init and cache-inhibited mapping for both BMan and QMan portals. --- sys/arm64/conf/dpaa/files.dpaa_arm64 | 22 + sys/dev/dpaa/bman.c | 76 +- sys/dev/dpaa/bman.h | 9 +- sys/dev/dpaa/bman_fdt.c | 21 +- sys/dev/dpaa/bman_portals.c | 71 +- sys/dev/dpaa/fman.c | 480 ++++++- sys/dev/dpaa/fman.h | 17 + sys/dev/dpaa/fman_chardev.c | 1777 ++++++++++++++++++++++++++ sys/dev/dpaa/fman_chardev.h | 155 +++ sys/dev/dpaa/fman_fdt.c | 27 +- sys/dev/dpaa/portals.h | 5 + sys/dev/dpaa/portals_common.c | 93 ++ sys/dev/dpaa/qman.c | 379 +++++- sys/dev/dpaa/qman.h | 47 +- sys/dev/dpaa/qman_fdt.c | 19 +- sys/dev/dpaa/qman_portals.c | 256 +++- 16 files changed, 3392 insertions(+), 62 deletions(-) create mode 100644 sys/dev/dpaa/fman_chardev.c create mode 100644 sys/dev/dpaa/fman_chardev.h diff --git a/sys/arm64/conf/dpaa/files.dpaa_arm64 b/sys/arm64/conf/dpaa/files.dpaa_arm64 index a27432e3545..7dd2ee83ee9 100644 --- a/sys/arm64/conf/dpaa/files.dpaa_arm64 +++ b/sys/arm64/conf/dpaa/files.dpaa_arm64 @@ -89,3 +89,25 @@ contrib/ncsw/user/env/xx_arm64.c optional dpaa \ no-depend compile-with "${DPAA_COMPILE_CMD}" contrib/ncsw/user/env/core_arm64.c optional dpaa \ no-depend compile-with "${DPAA_COMPILE_CMD}" + +# FreeBSD Wrappers +dev/dpaa/portals_common.c optional dpaa \ + no-depend compile-with "${DPAA_COMPILE_CMD}" +dev/dpaa/bman_portals.c optional dpaa fdt \ + no-depend compile-with "${DPAA_COMPILE_CMD}" +dev/dpaa/bman.c optional dpaa \ + no-depend compile-with "${DPAA_COMPILE_CMD}" +dev/dpaa/bman_fdt.c optional dpaa fdt \ + no-depend compile-with "${DPAA_COMPILE_CMD}" +dev/dpaa/qman_portals.c optional dpaa fdt \ + no-depend compile-with "${DPAA_COMPILE_CMD}" +dev/dpaa/qman.c optional dpaa \ + no-depend compile-with "${DPAA_COMPILE_CMD}" +dev/dpaa/qman_fdt.c optional dpaa fdt \ + no-depend compile-with "${DPAA_COMPILE_CMD}" +dev/dpaa/fman.c optional dpaa fdt \ + no-depend compile-with "${DPAA_COMPILE_CMD}" +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}" diff --git a/sys/dev/dpaa/bman.c b/sys/dev/dpaa/bman.c index 0dbcecb5d7a..3d098a4e3d5 100644 --- a/sys/dev/dpaa/bman.c +++ b/sys/dev/dpaa/bman.c @@ -36,12 +36,25 @@ #include #include #include +#include +#include +#include +#ifdef __powerpc__ #include +#endif + +#ifdef __aarch64__ +#include +#include +#endif #include "bman.h" +#define FBPR_ENTRY_SIZE 64 /* matches NCSW bm.h */ + static struct bman_softc *bman_sc; +static t_Handle bman_pool_by_bpid[BM_MAX_NUM_OF_POOLS]; extern t_Handle bman_portal_setup(struct bman_softc *bsc); @@ -74,6 +87,7 @@ bman_exception(t_Handle h_App, e_BmExceptions exception) device_printf(sc->sc_dev, "BMAN Exception: %s.\n", message); } + int bman_attach(device_t dev) { @@ -114,12 +128,47 @@ bman_attach(device_t dev) bp.partBpidBase = 0; bp.partNumOfPools = BM_MAX_NUM_OF_POOLS; +#ifdef __aarch64__ + /* + * Parse DT memory-region phandle for FBPR, matching Linux. + * DT: memory-region = <&bman_fbpr>; + * bman-fbpr: size=0x1000000 (16MB) + */ + { + extern void *qbman_alloc_reserved_mem(device_t, phandle_t, + uint32_t *, const char *); + phandle_t node, mem_node; + pcell_t mem_handle; + + node = ofw_bus_get_node(dev); + if (node > 0 && OF_getencprop(node, "memory-region", + (void *)&mem_handle, sizeof(mem_handle)) == + sizeof(mem_handle)) { + mem_node = OF_node_from_xref(mem_handle); + if (mem_node > 0) { + bp.p_FbprBase = + qbman_alloc_reserved_mem(dev, + mem_node, &bp.fbprSize, "FBPR"); + if (bp.p_FbprBase != NULL) + bp.totalNumOfBuffers = + bp.fbprSize / FBPR_ENTRY_SIZE * 8; + } + } else { + device_printf(dev, + "no memory-region property, using defaults\n"); + } + } +#endif + + device_printf(dev, "CCSR va=%#lx totalBufs=%u\n", + (unsigned long)bp.baseAddress, bp.totalNumOfBuffers); + sc->sc_bh = BM_Config(&bp); if (sc->sc_bh == NULL) goto err; - /* Warn if there is less than 5% free FPBR's in pool */ - error = BM_ConfigFbprThreshold(sc->sc_bh, (BMAN_MAX_BUFFERS / 8) / 20); + /* Warn if there is less than 5% free FBPR's in pool */ + error = BM_ConfigFbprThreshold(sc->sc_bh, (bp.totalNumOfBuffers / 8) / 20); if (error != E_OK) goto err; @@ -134,6 +183,18 @@ bman_attach(device_t dev) device_printf(dev, "Hardware version: %d.%d.\n", rev.majorRev, rev.minorRev); +#ifdef __aarch64__ + /* + * Initialize BMan portals for all CPUs from boot CPU. + * ARM64: pmap_mapdev creates global kernel VA, no sched_bind needed. + */ + for (int cpu = 0; cpu < mp_ncpus; cpu++) { + if (bman_portal_init_cpu(sc, cpu) == NULL) + device_printf(dev, + "could not setup BMan portal on CPU %d\n", cpu); + } +#endif + return (0); err: @@ -255,6 +316,7 @@ bman_pool_create(uint8_t *bpid, uint16_t bufferSize, uint16_t maxBuffers, *bpid = BM_POOL_GetId(pool); sc->sc_bpool_cpu[*bpid] = PCPU_GET(cpuid); + bman_pool_by_bpid[*bpid] = pool; sched_unpin(); @@ -275,6 +337,7 @@ bman_pool_destroy(t_Handle pool) struct bman_softc *sc; sc = bman_sc; + bman_pool_by_bpid[BM_POOL_GetId(pool)] = NULL; thread_lock(curthread); sched_bind(curthread, sc->sc_bpool_cpu[BM_POOL_GetId(pool)]); thread_unlock(curthread); @@ -363,3 +426,12 @@ bman_count(t_Handle pool) return (BM_POOL_GetCounter(pool, e_BM_POOL_COUNTERS_CONTENT)); } + +t_Handle +bman_pool_for_bpid(uint8_t bpid) +{ + + if (bpid >= BM_MAX_NUM_OF_POOLS) + return (NULL); + return (bman_pool_by_bpid[bpid]); +} diff --git a/sys/dev/dpaa/bman.h b/sys/dev/dpaa/bman.h index 01c09489890..36ecd2590c4 100644 --- a/sys/dev/dpaa/bman.h +++ b/sys/dev/dpaa/bman.h @@ -35,8 +35,8 @@ * BMAN Configuration */ -/* Maximum number of buffers in all BMAN pools */ -#define BMAN_MAX_BUFFERS 4096 +/* Maximum number of buffers in all BMAN pools (FBPR ring entries) */ +#define BMAN_MAX_BUFFERS 65536 /* * Portal definitions @@ -189,6 +189,7 @@ int bman_put_buffer(t_Handle pool, void *buffer); * @returns Number of free buffers in pool. */ uint32_t bman_count(t_Handle pool); +t_Handle bman_pool_for_bpid(uint8_t bpid); /* * Bus i/f @@ -199,4 +200,8 @@ int bman_suspend(device_t dev); int bman_resume(device_t dev); int bman_shutdown(device_t dev); +#ifdef __aarch64__ +t_Handle bman_portal_init_cpu(struct bman_softc *, int); +#endif + #endif /* BMAN_H */ diff --git a/sys/dev/dpaa/bman_fdt.c b/sys/dev/dpaa/bman_fdt.c index 8f117cacd66..c9ca00cb12e 100644 --- a/sys/dev/dpaa/bman_fdt.c +++ b/sys/dev/dpaa/bman_fdt.c @@ -34,6 +34,8 @@ #include #include +#include +#include #include #include @@ -103,7 +105,10 @@ static driver_t bm_portals_driver = { }; EARLY_DRIVER_MODULE(bman_portals, ofwbus, bm_portals_driver, 0, 0, - BUS_PASS_BUS); + BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LAST); +/* On ARM64, portal bus nodes are children of soc (simplebus), not ofwbus */ +EARLY_DRIVER_MODULE(bman_portals, simplebus, bm_portals_driver, 0, 0, + BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LAST); static void get_addr_props(phandle_t node, uint32_t *addrp, uint32_t *sizep) @@ -147,7 +152,7 @@ bman_portal_find_cpu(int cpu) return (node); for (node = OF_child(node); node != 0; node = OF_peer(node)) { - if (OF_getprop(node, "reg", ®, sizeof(reg)) <= 0) + if (OF_getencprop(node, "reg", ®, sizeof(reg)) <= 0) continue; if (reg == cpu) return (node); @@ -184,14 +189,16 @@ bman_portals_fdt_attach(device_t dev) continue; } /* Checkout related cpu */ - if (OF_getprop(child, "cpu-handle", (void *)&cpu, + if (OF_getencprop(child, "cpu-handle", (void *)&cpu, sizeof(cpu)) <= 0) { - cpu = bman_portal_find_cpu(cpus); - if (cpu <= 0) + /* No cpu-handle (ARM64): find CPU node directly */ + cpu_node = bman_portal_find_cpu(cpus); + if (cpu_node <= 0) continue; + } else { + /* cpu-handle is an ihandle (PowerPC) */ + cpu_node = OF_instance_to_package(cpu); } - /* Acquire cpu number */ - cpu_node = OF_instance_to_package(cpu); if (OF_getencprop(cpu_node, "reg", &cpu_num, sizeof(cpu_num)) <= 0) { device_printf(dev, "Could not retrieve CPU number.\n"); return (ENXIO); diff --git a/sys/dev/dpaa/bman_portals.c b/sys/dev/dpaa/bman_portals.c index 89bcbf64ce5..47f1d13f04c 100644 --- a/sys/dev/dpaa/bman_portals.c +++ b/sys/dev/dpaa/bman_portals.c @@ -38,13 +38,16 @@ #include #include +#include +#include +#ifdef __powerpc__ #include +#include +#endif #include #include -#include - #include "bman.h" #include "portals.h" @@ -55,15 +58,16 @@ struct dpaa_portals_softc *bp_sc; int bman_portals_attach(device_t dev) { - struct dpaa_portals_softc *sc; + bp_sc = device_get_softc(dev); - sc = bp_sc = device_get_softc(dev); - - /* Map bman portal to physical address space */ +#ifdef __powerpc__ + struct dpaa_portals_softc *sc = bp_sc; + /* Map bman portal to physical address space (PowerPC LAW) */ if (law_enable(OCP85XX_TGTIF_BMAN, sc->sc_dp_pa, sc->sc_dp_size)) { bman_portals_detach(dev); return (ENXIO); } +#endif /* Set portal properties for XX_VirtToPhys() */ XX_PortalSetInfo(dev); @@ -146,8 +150,8 @@ bman_portal_setup(struct bman_softc *bsc) dpaa_portal_map_registers(sc); /* Configure and initialize portal */ - bpp.ceBaseAddress = rman_get_bushandle(sc->sc_rres[0]); - bpp.ciBaseAddress = rman_get_bushandle(sc->sc_rres[1]); + bpp.ceBaseAddress = sc->sc_dp[cpu].dp_ce_va; + bpp.ciBaseAddress = sc->sc_dp[cpu].dp_ci_va; bpp.h_Bm = bsc->sc_bh; bpp.swPortalId = cpu; bpp.irq = (uintptr_t)sc->sc_dp[cpu].dp_ires; @@ -174,3 +178,54 @@ err: return (NULL); } + +#ifdef __aarch64__ +/* + * Initialize a BMan portal for a specific CPU from any CPU context. + * Unlike bman_portal_setup() which uses PCPU_GET(cpuid), this takes + * an explicit CPU parameter. On ARM64, pmap_mapdev creates global + * kernel VA, so no sched_bind/migration is needed. + * + * BMan has no SDEST configuration (confirmed from Linux reference). + */ +t_Handle +bman_portal_init_cpu(struct bman_softc *bsc, int cpu) +{ + struct dpaa_portals_softc *sc; + t_BmPortalParam bpp; + t_Handle portal; + + if (bp_sc == NULL) + return (NULL); + sc = bp_sc; + + /* Skip if already initialized */ + if (sc->sc_dp[cpu].dp_ph != NULL) + return (sc->sc_dp[cpu].dp_ph); + + /* Map CE/CI registers (global kernel VA, accessible from any CPU) */ + dpaa_portal_map_registers_cpu(sc, cpu); + + if (sc->sc_dp[cpu].dp_ce_va == 0 || sc->sc_dp[cpu].dp_ci_va == 0) + return (NULL); + + memset(&bpp, 0, sizeof(bpp)); + bpp.ceBaseAddress = sc->sc_dp[cpu].dp_ce_va; + bpp.ciBaseAddress = sc->sc_dp[cpu].dp_ci_va; + bpp.h_Bm = bsc->sc_bh; + bpp.swPortalId = cpu; + bpp.irq = (uintptr_t)sc->sc_dp[cpu].dp_ires; + + portal = BM_PORTAL_Config(&bpp); + if (portal == NULL) + return (NULL); + + if (BM_PORTAL_Init(portal) != E_OK) { + BM_PORTAL_Free(portal); + return (NULL); + } + + sc->sc_dp[cpu].dp_ph = portal; + return (portal); +} +#endif diff --git a/sys/dev/dpaa/fman.c b/sys/dev/dpaa/fman.c index f14ca45e1fc..42079c28282 100644 --- a/sys/dev/dpaa/fman.c +++ b/sys/dev/dpaa/fman.c @@ -38,19 +38,33 @@ #include #include +#include + +#include +#include #include "opt_platform.h" +#include "opt_dpaa.h" #include #include +#include #include +#include #include #include "fman.h" +#include "fman_chardev.h" +#include + +/* NCSW internal headers — needed for hot-adding HC to existing PCD */ +#include +#include static MALLOC_DEFINE(M_FMAN, "fman", "fman devices information"); + /** * @group FMan private defines. * @{ @@ -62,7 +76,11 @@ enum fman_irq_enum { enum fman_mu_ram_map { FMAN_MURAM_OFF = 0x0, - FMAN_MURAM_SIZE = 0x28000 +#ifdef __aarch64__ + FMAN_MURAM_SIZE = 0x60000 /* LS1046A FManv3: 384KB */ +#else + FMAN_MURAM_SIZE = 0x28000 /* P5020 FManv2: 160KB */ +#endif }; struct fman_config { @@ -81,11 +99,13 @@ struct fman_config { */ /** * Frame Manager firmware. - * We use the same firmware for both P3041 and P2041 devices. + * On ARM64 (LS1046A), U-Boot preloads FManv3 microcode into IRAM, + * so we skip embedding firmware. On PowerPC, use the P3041 image. */ +#ifndef __aarch64__ const uint32_t fman_firmware[] = FMAN_UC_IMG; const uint32_t fman_firmware_size = sizeof(fman_firmware); - +#endif int fman_activate_resource(device_t bus, device_t child, int type, int rid, struct resource *res) @@ -260,6 +280,110 @@ fman_fill_ranges(phandle_t node, struct simplebus_softc *sc) return (sc->nranges); } +static void +fman_pcd_exception_callback(t_Handle app_handle, + e_FmPcdExceptions exception) +{ + struct fman_softc *sc; + + sc = app_handle; + device_printf(sc->sc_base.dev, "PCD exception %d occurred.\n", + exception); +} + +static void +fman_pcd_id_exception_callback(t_Handle app_handle, + e_FmPcdExceptions exception, uint16_t index) +{ + struct fman_softc *sc; + + sc = app_handle; + device_printf(sc->sc_base.dev, + "PCD indexed exception %d, index %u.\n", exception, index); +} + +/* + * Host Command (HC) port parameters. + * + * The HC port uses OH port cell-index 0x2 (the first offline port), + * which is reserved in the Mono Gateway DTS with status="disabled" + * so dpaa_oh doesn't claim it. FMANv3 requires portId=0 for HC. + */ +#define FMAN_HC_OH_REG_OFFSET 0x82000 /* OH port 0x2 register offset */ +#define FMAN_HC_PORT_ID 0 /* MUST be 0 for FMANv3 */ +#define FMAN_HC_OH_CELL_INDEX 0x02 /* DT cell-index for QMan channel */ +#define FMAN_HC_LIODN_BASE 0 /* ARM64 SMMU handles LIODN */ +#define FMAN_HC_FQ_WQ 1 /* Work queue priority */ + +/* + * Byte-swap every uint32_t in a buffer. + * + * FMan is big-endian and reads HC frame data via DMA with no hardware + * swap (DMA_NO_SWAP). On PPC (big-endian), native uint32_t values + * are already in the correct byte order. On ARM64 (little-endian), + * every uint32_t must be byte-swapped so FMan reads correct values. + * + * The Linux NXP SDK does exactly this in QmEnqueueCB (before enqueue) + * and qm_tx_conf_dqrr_cb (after confirmation) in lnxwrp_fm_port.c. + */ +static __inline void +fman_hc_bswap_frame(void *buf, uint32_t len) +{ +#if _BYTE_ORDER == _LITTLE_ENDIAN + uint32_t *p = (uint32_t *)buf; + uint32_t i; + + for (i = 0; i < len / 4; i++) + p[i] = __builtin_bswap32(p[i]); +#endif +} + +/* + * QMan enqueue callback for the HC port. + * Called by NCSW HC code (hc.c EnQFrm) to send command frames + * through QMan to the FMan HC offline port. + */ +static t_Error +fman_hc_qm_enqueue(t_Handle h_arg, void *p_fd) +{ + struct fman_softc *sc; + t_DpaaFD *fd = (t_DpaaFD *)p_fd; + + sc = h_arg; + + /* Byte-swap HC frame data for FMan big-endian DMA */ + fman_hc_bswap_frame(DPAA_FD_GET_ADDR(fd), DPAA_FD_GET_LENGTH(fd)); + + return (qman_fqr_enqueue(sc->hc_tx_fqr, 0, fd)); +} + +/* + * QMan receive callback for HC confirmation frames. + * + * FMan sends HC command completions on the confirmation FQ. QMan + * delivers them here; we forward to FM_PCD_HcTxConf() which clears + * the "enqueued" flag in hc.c, unblocking the spinning EnQFrm(). + * + * The HC frame data was byte-swapped before enqueue (for FMan BE DMA). + * Swap it back to native byte order so FmHcTxConf can read fields + * like commandSequence in native format. + */ +static e_RxStoreResponse +fman_hc_conf_callback(t_Handle app, t_Handle qm_fqr, t_Handle qm_portal, + uint32_t fqid_offset, t_DpaaFD *frame) +{ + struct fman_softc *sc; + + sc = app; + + /* Byte-swap HC frame data back to native byte order */ + fman_hc_bswap_frame(DPAA_FD_GET_ADDR(frame), + DPAA_FD_GET_LENGTH(frame)); + + FM_PCD_HcTxConf(sc->pcd_handle, frame); + return (e_RX_STORE_RESPONSE_CONTINUE); +} + static t_Handle fman_init(struct fman_softc *sc, struct fman_config *cfg) { @@ -271,6 +395,8 @@ fman_init(struct fman_softc *sc, struct fman_config *cfg) uint16_t clock; uint32_t tmp, mod; + memset(&fm_params, 0, sizeof(fm_params)); + /* MURAM configuration */ muram_handle = FM_MURAM_ConfigAndInit(cfg->mem_base_addr + FMAN_MURAM_OFF, FMAN_MURAM_SIZE); @@ -281,6 +407,12 @@ fman_init(struct fman_softc *sc, struct fman_config *cfg) } sc->muram_handle = muram_handle; + /* + * MURAM stays as Device-nGnRnE (the default bus resource mapping), + * matching Linux devm_ioremap(). All MURAM accesses must use + * WRITE_UINT32/GET_UINT32/IOMemSet32 — never plain memset/memcpy. + */ + /* Fill in FM configuration */ fm_params.fmId = cfg->fm_id; /* XXX we support only one partition thus each fman has master id */ @@ -307,8 +439,22 @@ fman_init(struct fman_softc *sc, struct fman_config *cfg) fm_params.irq = cfg->irq_num; fm_params.errIrq = cfg->err_irq_num; +#ifdef __aarch64__ + /* U-Boot preloads FMan microcode into IRAM; skip firmware loading */ + fm_params.firmware.size = 0; + fm_params.firmware.p_Code = NULL; +#else fm_params.firmware.size = fman_firmware_size; fm_params.firmware.p_Code = (uint32_t*)fman_firmware; +#endif + +#if (DPAA_VERSION >= 11) + /* FManv3 Virtual Storage Profiles — master owns all 64 entries. + * VSP register block at FMan base + 0xdc000 (from fm_common.h FM_MM_SP). */ + fm_params.partVSPBase = 0; + fm_params.partNumOfVSPs = FM_VSP_MAX_NUM_OF_ENTRIES; + fm_params.vspBaseAddr = cfg->mem_base_addr + 0xdc000; +#endif fm_handle = FM_Config(&fm_params); if (fm_handle == NULL) { @@ -317,7 +463,12 @@ fman_init(struct fman_softc *sc, struct fman_config *cfg) goto err; } +#ifdef __aarch64__ + /* Don't reset FMan — would wipe U-Boot's pre-loaded microcode */ + FM_ConfigResetOnInit(fm_handle, FALSE); +#else FM_ConfigResetOnInit(fm_handle, TRUE); +#endif error = FM_Init(fm_handle); if (error != E_OK) { @@ -334,6 +485,100 @@ fman_init(struct fman_softc *sc, struct fman_config *cfg) device_printf(cfg->fman_device, "Hardware version: %d.%d.\n", revision_info.majorRev, revision_info.minorRev); + { + t_FmCtrlCodeRevisionInfo ucode_rev; + if (FM_GetFmanCtrlCodeRevision(fm_handle, &ucode_rev) == E_OK) + device_printf(cfg->fman_device, + "Microcode: package %u, version %u.%u%s\n", + ucode_rev.packageRev, + ucode_rev.majorRev, ucode_rev.minorRev, + ucode_rev.packageRev >= 209 ? " (CDX/ASK)" : + ucode_rev.packageRev == 106 ? " (IP offload)" : + " (unknown)"); + } + + /* + * Initialize PCD (Parse-Classify-Distribute) with Parser + + * KeyGen + CC for RSS/multi-queue RX distribution and + * classification support. + * + * CC is enabled here so CDX can use it for hash tables. + * Host Command (HC) is NOT enabled here because QMan portals + * aren't ready during early FMan attach. CDX calls + * fman_reinit_pcd_with_hc() later to hot-add HC to this PCD + * without rebuilding it — preserving existing dtsec schemes. + */ + { + t_FmPcdParams pcd_params; + t_FmPcdNetEnvParams netenv_params; + + sc->pcd_handle = NULL; + sc->netenv_handle = NULL; + sc->hc_tx_fqr = NULL; + sc->hc_err_fqr = NULL; + sc->hc_conf_fqr = NULL; + + memset(&pcd_params, 0, sizeof(pcd_params)); + pcd_params.h_Fm = fm_handle; + pcd_params.prsSupport = TRUE; + pcd_params.ccSupport = TRUE; + pcd_params.kgSupport = TRUE; + pcd_params.plcrSupport = TRUE; + pcd_params.numOfSchemes = FM_PCD_KG_NUM_OF_SCHEMES; + pcd_params.f_Exception = fman_pcd_exception_callback; + pcd_params.f_ExceptionId = fman_pcd_id_exception_callback; + pcd_params.h_App = sc; + + sc->pcd_handle = FM_PCD_Config(&pcd_params); + if (sc->pcd_handle == NULL) { + device_printf(cfg->fman_device, + "couldn't configure PCD\n"); + goto skip_pcd; + } + + FM_PCD_ConfigPlcrNumOfSharedProfiles(sc->pcd_handle, 16); + + error = FM_PCD_Init(sc->pcd_handle); + if (error != E_OK) { + device_printf(cfg->fman_device, + "couldn't init PCD: %d\n", error); + FM_PCD_Free(sc->pcd_handle); + sc->pcd_handle = NULL; + goto skip_pcd; + } + + error = FM_PCD_Enable(sc->pcd_handle); + if (error != E_OK) { + device_printf(cfg->fman_device, + "couldn't enable PCD: %d\n", error); + FM_PCD_Free(sc->pcd_handle); + sc->pcd_handle = NULL; + goto skip_pcd; + } + + /* + * Network environment: IPv4/IPv6 + TCP/UDP distinction. + * This tells PCD which protocol layers to recognize. + */ + memset(&netenv_params, 0, sizeof(netenv_params)); + netenv_params.numOfDistinctionUnits = 2; + netenv_params.units[0].hdrs[0].hdr = HEADER_TYPE_IPv4; + netenv_params.units[0].hdrs[1].hdr = HEADER_TYPE_IPv6; + netenv_params.units[1].hdrs[0].hdr = HEADER_TYPE_TCP; + netenv_params.units[1].hdrs[1].hdr = HEADER_TYPE_UDP; + + sc->netenv_handle = FM_PCD_NetEnvCharacteristicsSet( + sc->pcd_handle, &netenv_params); + if (sc->netenv_handle == NULL) + device_printf(cfg->fman_device, + "couldn't set NetEnv characteristics\n"); + + device_printf(cfg->fman_device, + "PCD initialized: Parser + KeyGen\n"); +skip_pcd: + ; + } + /* Initialize the simplebus part of things */ simplebus_init(sc->sc_base.dev, 0); @@ -352,6 +597,7 @@ err2: FM_Free(fm_handle); err: FM_MURAM_Free(muram_handle); + sc->muram_handle = NULL; return (NULL); } @@ -361,7 +607,7 @@ fman_exception_callback(t_Handle app_handle, e_FmExceptions exception) struct fman_softc *sc; sc = app_handle; - device_printf(sc->sc_base.dev, "FMan exception occurred.\n"); + device_printf(sc->sc_base.dev, "FMan exception %d\n", exception); } static void @@ -371,8 +617,11 @@ fman_error_callback(t_Handle app_handle, e_FmPortType port_type, struct fman_softc *sc; sc = app_handle; - device_printf(sc->sc_base.dev, "FMan error occurred.\n"); + device_printf(sc->sc_base.dev, + "FMan error: portType=%d portId=%u addr=0x%llx tnum=%u liodn=%u\n", + port_type, port_id, (unsigned long long)addr, tnum, liodn); } + /** @} */ @@ -411,6 +660,34 @@ fman_get_bushandle(device_t dev, vm_offset_t *fm_base) return (0); } +int +fman_get_pcd_handle(device_t dev, t_Handle *pcdh) +{ + struct fman_softc *sc = device_get_softc(dev); + + *pcdh = sc->pcd_handle; + + return (0); +} + +int +fman_get_netenv_handle(device_t dev, t_Handle *netenvh) +{ + struct fman_softc *sc = device_get_softc(dev); + + *netenvh = sc->netenv_handle; + + return (0); +} + +struct fmcd_softc * +fman_get_fmcd(device_t dev) +{ + struct fman_softc *sc = device_get_softc(dev); + + return (sc->fmcd); +} + int fman_attach(device_t dev) { @@ -475,7 +752,20 @@ fman_attach(device_t dev) goto err; } - return (bus_generic_attach(dev)); + /* Initialize FMD chardev interface for userspace PCD control */ + if (sc->pcd_handle != NULL) { + sc->fmcd = malloc(sizeof(struct fmcd_softc), M_FMAN, + M_WAITOK | M_ZERO); + if (fmcd_init(dev, sc->fmcd) != 0) { + free(sc->fmcd, M_FMAN); + sc->fmcd = NULL; + device_printf(dev, + "fmcd: chardev init failed (non-fatal)\n"); + } + } + + bus_attach_children(dev); + return (0); err: fman_detach(dev); @@ -489,12 +779,29 @@ fman_detach(device_t dev) sc = device_get_softc(dev); - if (sc->muram_handle) { - FM_MURAM_Free(sc->muram_handle); + if (sc->fmcd) { + fmcd_destroy(sc->fmcd); + free(sc->fmcd, M_FMAN); + sc->fmcd = NULL; + } + if (sc->netenv_handle) { + FM_PCD_NetEnvCharacteristicsDelete(sc->netenv_handle); + sc->netenv_handle = NULL; + } + if (sc->pcd_handle) { + FM_PCD_Disable(sc->pcd_handle); + FM_PCD_Free(sc->pcd_handle); + sc->pcd_handle = NULL; } if (sc->fm_handle) { FM_Free(sc->fm_handle); + sc->fm_handle = NULL; + } + + if (sc->muram_handle) { + FM_MURAM_Free(sc->muram_handle); + sc->muram_handle = NULL; } if (sc->mem_res) { @@ -507,7 +814,7 @@ fman_detach(device_t dev) sc->irq_res); } - if (sc->irq_res) { + if (sc->err_irq_res) { bus_release_resource(dev, SYS_RES_IRQ, sc->err_irq_rid, sc->err_irq_res); } @@ -540,8 +847,15 @@ int fman_qman_channel_id(device_t dev, int port) { struct fman_softc *sc; +#ifdef __aarch64__ + /* FMan v3 / Major >= 6 (LS1046A): 2x 10G + 6x 1G + 6x OH ports. + * Order matches Linux fman_get_qman_channel_id() for Major >= 6. */ + int qman_port_id[] = {0x30, 0x31, 0x28, 0x29, 0x2a, 0x2b, + 0x2c, 0x2d, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}; +#else int qman_port_id[] = {0x31, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}; +#endif int i; sc = device_get_softc(dev); @@ -553,4 +867,152 @@ fman_qman_channel_id(device_t dev, int port) return (0); } +/* + * Hot-add Host Command port to existing PCD for advanced offload. + * + * Called by CDX module init (after QMan portals are ready) to add HC + * support to the existing PCD. Unlike the previous approach of + * destroying and rebuilding PCD (which invalidated dtsec schemes and + * caused FM_FD_ERR_NO_SCHEME), this preserves the PCD, all existing + * KeyGen schemes, and dtsec port bindings. + * + * Sequence: + * 1. Create QMan FQRs for HC TX/confirmation/error + * 2. Disable PCD (brief pause — schemes survive) + * 3. Create HC port via FmHcConfigAndInit + * 4. Set p_FmPcd->h_Hc to enable HC in the PCD + * 5. FM_PCD_SetAdvancedOffloadSupport (requires HC + PCD disabled) + * 6. Re-enable PCD + * + * Returns 0 on success, errno on failure. + */ +int +fman_reinit_pcd_with_hc(device_t dev) +{ + struct fman_softc *sc; + t_FmPcd *p_FmPcd; + t_FmHcParams hc_params; + t_Handle hc_handle; + uint32_t hc_qman_chan; + t_Handle fqr; + t_Error error; + + sc = device_get_softc(dev); + + if (sc->pcd_handle == NULL) { + device_printf(dev, "PCD not initialized\n"); + return (ENXIO); + } + + /* Resolve QMan channel for HC offline port (cell-index 0x2) */ + hc_qman_chan = fman_qman_channel_id(dev, FMAN_HC_OH_CELL_INDEX); + if (hc_qman_chan == 0) { + device_printf(dev, + "couldn't get QMan channel for HC port\n"); + return (ENXIO); + } + + /* TX FQR — HC enqueues command frames here for FMan */ + fqr = qman_fqr_create(1, hc_qman_chan, FMAN_HC_FQ_WQ, + false, 0, false, false, true, false, 0, 0, 0); + if (fqr == NULL) { + device_printf(dev, "couldn't create HC TX FQR\n"); + return (ENOMEM); + } + sc->hc_tx_fqr = fqr; + + /* Confirmation FQR — FMan sends HC completions here */ + fqr = qman_fqr_create(1, e_QM_FQ_CHANNEL_POOL1, + FMAN_HC_FQ_WQ, false, 0, false, false, true, false, + 0, 0, 0); + if (fqr == NULL) { + device_printf(dev, "couldn't create HC confirmation FQR\n"); + return (ENOMEM); + } + sc->hc_conf_fqr = fqr; + + /* + * Register callback for HC confirmation frames. + * FMan sends command completions on confFqid; without this + * callback, they hit QMan's dummy handler and EnQFrm() in + * hc.c spins until timeout. + */ + error = qman_fqr_register_cb(sc->hc_conf_fqr, + fman_hc_conf_callback, sc); + if (error != E_OK) { + device_printf(dev, + "couldn't register HC confirmation callback: %d\n", + error); + return (ENXIO); + } + + /* Error FQR — FMan sends HC errors here */ + fqr = qman_fqr_create(1, e_QM_FQ_CHANNEL_POOL1, + FMAN_HC_FQ_WQ, false, 0, false, false, true, false, + 0, 0, 0); + if (fqr == NULL) { + device_printf(dev, "couldn't create HC error FQR\n"); + return (ENOMEM); + } + sc->hc_err_fqr = fqr; + + /* + * Temporarily disable PCD to add HC and enable advanced + * offload. Existing KeyGen schemes and port bindings survive + * the Disable/Enable cycle — only the PRS/KG/PLCR hardware + * engines are paused. + */ + error = FM_PCD_Disable(sc->pcd_handle); + if (error != E_OK) { + device_printf(dev, "couldn't disable PCD: %d\n", error); + return (ENXIO); + } + + /* Create HC port and attach to existing PCD */ + memset(&hc_params, 0, sizeof(hc_params)); + hc_params.h_Fm = sc->fm_handle; + hc_params.h_FmPcd = sc->pcd_handle; + hc_params.params.portBaseAddr = rman_get_bushandle(sc->mem_res) + + FMAN_HC_OH_REG_OFFSET; + hc_params.params.portId = FMAN_HC_PORT_ID; + hc_params.params.liodnBase = FMAN_HC_LIODN_BASE; + hc_params.params.errFqid = + qman_fqr_get_base_fqid(sc->hc_err_fqr); + hc_params.params.confFqid = + qman_fqr_get_base_fqid(sc->hc_conf_fqr); + hc_params.params.qmChannel = hc_qman_chan; + hc_params.params.f_QmEnqueue = fman_hc_qm_enqueue; + hc_params.params.h_QmArg = sc; + + hc_handle = FmHcConfigAndInit(&hc_params); + if (hc_handle == NULL) { + device_printf(dev, "couldn't create HC port\n"); + FM_PCD_Enable(sc->pcd_handle); + return (ENXIO); + } + + /* Wire HC into the existing PCD */ + p_FmPcd = (t_FmPcd *)sc->pcd_handle; + p_FmPcd->h_Hc = hc_handle; + + /* Enable advanced offload (requires HC + PCD disabled) */ + error = FM_PCD_SetAdvancedOffloadSupport(sc->pcd_handle); + if (error != E_OK) { + device_printf(dev, + "couldn't set advanced offload support: %d\n", error); + /* Non-fatal — HC still works, just no offload flag */ + } + + /* Re-enable PCD — resumes Parser/KeyGen/CC processing */ + error = FM_PCD_Enable(sc->pcd_handle); + if (error != E_OK) { + device_printf(dev, "couldn't re-enable PCD: %d\n", error); + return (ENXIO); + } + + device_printf(dev, "PCD upgraded: HC port added, " + "existing schemes preserved\n"); + return (0); +} + /** @} */ diff --git a/sys/dev/dpaa/fman.h b/sys/dev/dpaa/fman.h index c7b57c60171..affb2dc6a75 100644 --- a/sys/dev/dpaa/fman.h +++ b/sys/dev/dpaa/fman.h @@ -27,8 +27,11 @@ #ifndef FMAN_H_ #define FMAN_H_ +#include #include +struct fmcd_softc; /* Forward declaration */ + /** * FMan driver instance data. */ @@ -46,6 +49,16 @@ struct fman_softc { t_Handle fm_handle; t_Handle muram_handle; + t_Handle pcd_handle; + t_Handle netenv_handle; + + /* Host Command port state (for advanced offload) */ + t_Handle hc_tx_fqr; /* TX FQR for HC enqueue */ + t_Handle hc_err_fqr; /* Error FQR for HC port */ + t_Handle hc_conf_fqr; /* Confirmation FQR for HC port */ + + /* FMD chardev interface (for userspace PCD control via fmlib/FMC) */ + struct fmcd_softc *fmcd; }; @@ -73,5 +86,9 @@ uint32_t fman_get_clock(struct fman_softc *sc); int fman_get_handle(device_t dev, t_Handle *fmh); int fman_get_muram_handle(device_t dev, t_Handle *muramh); int fman_get_bushandle(device_t dev, vm_offset_t *fm_base); +int fman_get_pcd_handle(device_t dev, t_Handle *pcdh); +int fman_get_netenv_handle(device_t dev, t_Handle *netenvh); +int fman_reinit_pcd_with_hc(device_t dev); +struct fmcd_softc *fman_get_fmcd(device_t dev); #endif /* FMAN_H_ */ diff --git a/sys/dev/dpaa/fman_chardev.c b/sys/dev/dpaa/fman_chardev.c new file mode 100644 index 00000000000..af23617ef01 --- /dev/null +++ b/sys/dev/dpaa/fman_chardev.c @@ -0,0 +1,1777 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2026 Mono Technologies Inc. + * All rights reserved. + * + * FMan character device — exposes NCSW PCD API to userspace via ioctls. + * + * Creates /dev/fm0, /dev/fm0-pcd, /dev/fm0-port-rx*, /dev/fm0-port-oh* + * character devices. Userspace tools (fmlib, FMC, dpa_app) open these devices + * and issue ioctls that map 1:1 to NCSW kernel functions: + * + * FM_PCD_IOC_KG_SCHEME_SET → FM_PCD_KgSchemeSet() + * FM_PCD_IOC_HASH_TABLE_SET → FM_PCD_HashTableSet() + * FM_PCD_IOC_CC_ROOT_BUILD → FM_PCD_CcRootBuild() + * FM_PORT_IOC_SET_PCD → FM_PORT_SetPCD() + * ... etc. + * + * Handle management: NCSW returns opaque kernel pointers (t_Handle). + * A per-device handle table maps these to opaque integer IDs that are + * exposed to userspace — kernel pointers are never leaked. The ioc + * structs have identical binary layouts to the NCSW structs (plus a + * trailing void *id for output handles); the kernel translates handle + * IDs to/from kernel pointers at the ioctl boundary. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "opt_dpaa.h" + +#include +#include +#include +#include +#include +#include + +/* + * fmlib ioctl ABI headers — authoritative struct/ioctl definitions. + * These are included via FMCD_COMPILE_CMD -I paths (see config.dpaa_arm64). + */ +#include "fm_pcd_ioctls.h" + +#include "fman.h" +#include "fman_chardev.h" + +static MALLOC_DEFINE(M_FMCD, "fmcd", "FMan chardev data"); + +/* + * Port PCD types and ioctl commands. + * + * These come from fm_port_ioctls.h in fmlib, but we cannot include that + * header because it pulls in fmlib's "enet_ext.h" which conflicts with + * the NCSW kernel copy (contrib/ncsw/inc/enet_ext.h). The types below + * are copied verbatim from fm_port_ioctls.h and must stay in sync. + */ +typedef enum ioc_fm_port_pcd_support { + e_IOC_FM_PORT_PCD_SUPPORT_NONE = 0, + e_IOC_FM_PORT_PCD_SUPPORT_PRS_ONLY, + e_IOC_FM_PORT_PCD_SUPPORT_PLCR_ONLY, + e_IOC_FM_PORT_PCD_SUPPORT_PRS_AND_PLCR, + e_IOC_FM_PORT_PCD_SUPPORT_PRS_AND_KG, + e_IOC_FM_PORT_PCD_SUPPORT_PRS_AND_KG_AND_CC, + e_IOC_FM_PORT_PCD_SUPPORT_PRS_AND_KG_AND_CC_AND_PLCR, + e_IOC_FM_PORT_PCD_SUPPORT_PRS_AND_KG_AND_PLCR, + e_IOC_FM_PORT_PCD_SUPPORT_PRS_AND_CC, + e_IOC_FM_PORT_PCD_SUPPORT_PRS_AND_CC_AND_PLCR, + e_IOC_FM_PORT_PCD_SUPPORT_CC_ONLY, +} ioc_fm_port_pcd_support; + +typedef union ioc_fm_pcd_hdr_prs_opts_u { + struct { + bool label_interpretation_enable; + ioc_net_header_type next_parse; + } mpls_prs_options; + struct { + uint16_t tag_protocol_id1; + uint16_t tag_protocol_id2; + } vlan_prs_options; + struct { + bool enable_mtu_check; + } pppoe_prs_options; + struct { + bool routing_hdr_disable; + } ipv6_prs_options; + struct { + bool pad_ignore_checksum; + } udp_prs_options; + struct { + bool pad_ignore_checksum; + } tcp_prs_options; +} ioc_fm_pcd_hdr_prs_opts_u; + +typedef struct ioc_fm_pcd_prs_additional_hdr_params_t { + ioc_net_header_type hdr; + bool err_disable; + bool soft_prs_enable; + uint8_t index_per_hdr; + bool use_prs_opts; + ioc_fm_pcd_hdr_prs_opts_u prs_opts; +} ioc_fm_pcd_prs_additional_hdr_params_t; + +typedef struct ioc_fm_port_pcd_prs_params_t { + uint8_t prs_res_priv_info; + uint8_t parsing_offset; + ioc_net_header_type first_prs_hdr; + bool include_in_prs_statistics; + uint8_t num_of_hdrs_with_additional_params; + ioc_fm_pcd_prs_additional_hdr_params_t + additional_params[IOC_FM_PCD_PRS_NUM_OF_HDRS]; + bool set_vlan_tpid1; + uint16_t vlan_tpid1; + bool set_vlan_tpid2; + uint16_t vlan_tpid2; +} ioc_fm_port_pcd_prs_params_t; + +typedef struct ioc_fm_port_pcd_cc_params_t { + void *cc_tree_id; +} ioc_fm_port_pcd_cc_params_t; + +typedef struct ioc_fm_port_pcd_kg_params_t { + uint8_t num_of_schemes; + void *scheme_ids[FM_PCD_KG_NUM_OF_SCHEMES]; + bool direct_scheme; + void *direct_scheme_id; +} ioc_fm_port_pcd_kg_params_t; + +typedef struct ioc_fm_port_pcd_plcr_params_t { + void *plcr_profile_id; +} ioc_fm_port_pcd_plcr_params_t; + +typedef struct ioc_fm_port_pcd_params_t { + ioc_fm_port_pcd_support pcd_support; + void *net_env_id; + ioc_fm_port_pcd_prs_params_t *p_prs_params; + ioc_fm_port_pcd_cc_params_t *p_cc_params; + ioc_fm_port_pcd_kg_params_t *p_kg_params; + ioc_fm_port_pcd_plcr_params_t *p_plcr_params; + void *p_ip_reassembly_manip; + void *p_capwap_reassembly_manip; +} ioc_fm_port_pcd_params_t; + +#define FM_PORT_IOC_DISABLE _IO(FM_IOC_TYPE_BASE, FM_PORT_IOC_NUM(1)) +#define FM_PORT_IOC_ENABLE _IO(FM_IOC_TYPE_BASE, FM_PORT_IOC_NUM(2)) +#define FM_PORT_IOC_SET_PCD \ + _IOW(FM_IOC_TYPE_BASE, FM_PORT_IOC_NUM(20), ioc_fm_port_pcd_params_t) +#define FM_PORT_IOC_DELETE_PCD _IO(FM_IOC_TYPE_BASE, FM_PORT_IOC_NUM(21)) + +/* ------------------------------------------------------------------ */ +/* Struct layout assertions */ +/* ------------------------------------------------------------------ */ + +/* + * The ioc structs must have identical binary layout to the corresponding + * NCSW structs, with only a trailing void *id output field appended. + * This is verified at compile time, matching Linux's ASSERT_COND pattern. + * + * roundup2() accounts for alignment padding the compiler may insert + * before the trailing void *id pointer (e.g. NetEnvParams: NCSW=964, + * ioc=976 because of 4 bytes padding to align void *id to 8 bytes). + */ +#define FMCD_IOC_SIZE(ncsw_t) \ + roundup2(sizeof(ncsw_t) + sizeof(void *), _Alignof(void *)) + +_Static_assert( + sizeof(ioc_fm_pcd_net_env_params_t) == FMCD_IOC_SIZE(t_FmPcdNetEnvParams), + "NetEnvParams size mismatch — ioc and NCSW struct layouts diverged"); + +_Static_assert( + sizeof(ioc_fm_pcd_kg_scheme_params_t) == + FMCD_IOC_SIZE(t_FmPcdKgSchemeParams), + "KgSchemeParams size mismatch — ioc and NCSW struct layouts diverged"); + +_Static_assert( + sizeof(ioc_fm_pcd_cc_tree_params_t) == + FMCD_IOC_SIZE(t_FmPcdCcTreeParams), + "CcTreeParams size mismatch — ioc and NCSW struct layouts diverged"); + +_Static_assert( + sizeof(ioc_fm_pcd_cc_node_params_t) == + FMCD_IOC_SIZE(t_FmPcdCcNodeParams), + "CcNodeParams size mismatch — ioc and NCSW struct layouts diverged"); + +_Static_assert( + sizeof(ioc_fm_pcd_hash_table_params_t) == + FMCD_IOC_SIZE(t_FmPcdHashTableParams), + "HashTableParams size mismatch — ioc and NCSW struct layouts diverged"); + +_Static_assert( + sizeof(ioc_fm_pcd_manip_params_t) == + FMCD_IOC_SIZE(t_FmPcdManipParams), + "ManipParams size mismatch — ioc and NCSW struct layouts diverged"); + +_Static_assert( + sizeof(ioc_fm_pcd_plcr_profile_params_t) == + FMCD_IOC_SIZE(t_FmPcdPlcrProfileParams), + "PlcrProfileParams size mismatch — ioc and NCSW struct layouts diverged"); + +#if (DPAA_VERSION >= 11) +_Static_assert( + sizeof(ioc_fm_pcd_frm_replic_group_params_t) == + FMCD_IOC_SIZE(t_FmPcdFrmReplicGroupParams), + "FrmReplicGroupParams size mismatch — ioc and NCSW struct layouts diverged"); +#endif + +/* Port PCD struct: no trailing id field, sizes must match exactly. */ +_Static_assert( + sizeof(ioc_fm_port_pcd_params_t) == sizeof(t_FmPortPcdParams), + "PortPcdParams size mismatch — ioc and NCSW struct layouts diverged"); + +/* ------------------------------------------------------------------ */ +/* Port registration */ +/* ------------------------------------------------------------------ */ + +/* + * Convert DT cell-index to fmlib RX port index. + * + * fmlib convention (matching Linux SDK): + * 1G MACs: cell-index 0-5 → port index 0-5 + * 10G MACs: cell-index 8-9 → port index 6-7 + */ +#define FMCD_1G_RX_PORTS 6 + +static inline int +fmcd_cell_to_rxidx(int cell_index) +{ + if (cell_index >= 8) /* 10G MAC */ + return (FMCD_1G_RX_PORTS + (cell_index - 8)); + return (cell_index); /* 1G MAC */ +} + +void +fmcd_register_rx_port(struct fmcd_softc *sc, int cell_index, t_Handle h_port, + uint32_t dflt_fqid) +{ + int index = fmcd_cell_to_rxidx(cell_index); + + if (sc == NULL || index < 0 || index >= 8) + return; + + sx_xlock(&sc->port_lock); + sc->rx_port_handles[index] = h_port; + sc->rx_port_fqids[index] = dflt_fqid; + if (sc->cdev_port_rx[index] != NULL) + sc->cdev_port_rx[index]->si_drv2 = h_port; + sx_xunlock(&sc->port_lock); + + device_printf(sc->fman_dev, + "fmcd: rx port registered: cell=%d idx=%d fqid=%u\n", + cell_index, index, dflt_fqid); +} + +uint32_t +fmcd_get_rx_dflt_fqid(struct fmcd_softc *sc, int index) +{ + + if (sc == NULL || index < 0 || index >= 8) + return (0); + return (sc->rx_port_fqids[index]); +} + +void +fmcd_register_rx_port_scheme(struct fmcd_softc *sc, int cell_index, + t_Handle h_scheme, t_Handle *p_scheme) +{ + int index = fmcd_cell_to_rxidx(cell_index); + + if (sc == NULL || index < 0 || index >= 8) + return; + + sx_xlock(&sc->port_lock); + sc->rx_port_schemes[index] = h_scheme; + sc->rx_port_scheme_ptrs[index] = p_scheme; + sx_xunlock(&sc->port_lock); +} + +void +fmcd_register_oh_port(struct fmcd_softc *sc, int index, t_Handle h_port) +{ + if (sc == NULL || index < 0 || index >= 6) + return; + + sx_xlock(&sc->port_lock); + sc->oh_port_handles[index] = h_port; + if (sc->cdev_port_oh[index] != NULL) + sc->cdev_port_oh[index]->si_drv2 = h_port; + sx_xunlock(&sc->port_lock); +} + +void +fmcd_pcd_mark_transferred(struct fmcd_softc *sc) +{ + + sx_xlock(&sc->port_lock); + sc->pcd_transferred = true; + sx_xunlock(&sc->port_lock); +} + +/* ------------------------------------------------------------------ */ +/* Handle table helpers */ +/* ------------------------------------------------------------------ */ + +/* + * Allocate a handle slot, store the kernel pointer and type. + * Returns the opaque handle ID, or 0 on failure (table full). + * Caller must hold sc->port_lock. + */ +static uint64_t +fmcd_handle_alloc(struct fmcd_softc *sc, t_Handle ptr, + enum fmcd_handle_type type) +{ + int i; + + KASSERT(ptr != NULL, ("fmcd_handle_alloc: NULL ptr")); + KASSERT(type != FMCD_HDL_NONE, ("fmcd_handle_alloc: NONE type")); + + for (i = 0; i < FMCD_MAX_HANDLES; i++) { + if (sc->handles[i].ptr == NULL) { + sc->handles[i].ptr = ptr; + sc->handles[i].type = type; + return (((uint64_t)sc->handle_gen << + FMCD_HANDLE_SHIFT) | (uint64_t)i); + } + } + printf("fmcd: handle table full (%d entries)\n", FMCD_MAX_HANDLES); + return (0); +} + +/* + * Look up a handle ID and return the kernel pointer. + * If expected_type is not FMCD_HDL_NONE, validates the type matches. + * Returns NULL if the handle is invalid, stale, or type mismatches. + * Caller must hold sc->port_lock. + */ +static t_Handle +fmcd_handle_lookup(struct fmcd_softc *sc, uint64_t id, + enum fmcd_handle_type expected_type) +{ + uint32_t slot, gen; + + if (id == 0) + return (NULL); + + slot = (uint32_t)(id & FMCD_HANDLE_MASK); + gen = (uint32_t)(id >> FMCD_HANDLE_SHIFT); + + if (slot >= FMCD_MAX_HANDLES) + return (NULL); + if (gen != sc->handle_gen) + return (NULL); + if (sc->handles[slot].ptr == NULL) + return (NULL); + if (expected_type != FMCD_HDL_NONE && + sc->handles[slot].type != expected_type) + return (NULL); + + return (sc->handles[slot].ptr); +} + +/* + * Free a handle slot. Returns the kernel pointer for the caller + * to pass to NCSW delete, or NULL if the handle is invalid. + * Caller must hold sc->port_lock. + */ +static t_Handle +fmcd_handle_free(struct fmcd_softc *sc, uint64_t id, + enum fmcd_handle_type expected_type) +{ + uint32_t slot, gen; + t_Handle ptr; + + if (id == 0) + return (NULL); + + slot = (uint32_t)(id & FMCD_HANDLE_MASK); + gen = (uint32_t)(id >> FMCD_HANDLE_SHIFT); + + if (slot >= FMCD_MAX_HANDLES) + return (NULL); + if (gen != sc->handle_gen) + return (NULL); + if (sc->handles[slot].ptr == NULL) + return (NULL); + if (expected_type != FMCD_HDL_NONE && + sc->handles[slot].type != expected_type) + return (NULL); + + ptr = sc->handles[slot].ptr; + sc->handles[slot].ptr = NULL; + sc->handles[slot].type = FMCD_HDL_NONE; + return (ptr); +} + +/* + * Resolve a handle ID to a kernel pointer without freeing the slot. + * Used by the CDX module to translate hash table handles received + * from userspace (dpa_app) into NCSW kernel pointers. + * Returns NULL if the handle is invalid or stale. + */ +t_Handle +fmcd_handle_resolve(struct fmcd_softc *sc, uint64_t id, + enum fmcd_handle_type expected_type) +{ + t_Handle ptr; + + sx_xlock(&sc->port_lock); + ptr = fmcd_handle_lookup(sc, id, expected_type); + sx_xunlock(&sc->port_lock); + return (ptr); +} + +/* + * Translate a void* field that holds an opaque handle ID to a kernel + * pointer. Returns 0 on success (field updated in-place), EINVAL on + * bad handle. NULL fields are left alone (returns 0). + * Caller must hold sc->port_lock. + */ +static int +fmcd_xlate(struct fmcd_softc *sc, void **field, + enum fmcd_handle_type expected_type) +{ + t_Handle ptr; + + if (*field == NULL) + return (0); + + ptr = fmcd_handle_lookup(sc, (uint64_t)(uintptr_t)*field, + expected_type); + if (ptr == NULL) + return (EINVAL); + + *field = ptr; + return (0); +} + +/* + * Translate handle IDs embedded within a cc_next_engine_params_t + * to kernel pointers. Returns 0 on success, EINVAL on bad handle. + * Caller must hold sc->port_lock. + */ +static int +fmcd_translate_next_engine(struct fmcd_softc *sc, + ioc_fm_pcd_cc_next_engine_params_t *ne) +{ + int error; + + switch (ne->next_engine) { + case e_IOC_FM_PCD_HASH: + case e_IOC_FM_PCD_CC: + error = fmcd_xlate(sc, + &ne->params.cc_params.cc_node_id, FMCD_HDL_NONE); + if (error) + return (error); + break; + case e_IOC_FM_PCD_KG: + error = fmcd_xlate(sc, + &ne->params.kg_params.p_direct_scheme, + FMCD_HDL_KG_SCHEME); + if (error) + return (error); + break; +#if (DPAA_VERSION >= 11) + case e_IOC_FM_PCD_FR: + error = fmcd_xlate(sc, + &ne->params.fr_params.frm_replic_id, + FMCD_HDL_FRM_REPLIC); + if (error) + return (error); + break; +#endif + default: + break; + } + + /* manip_id can appear regardless of next_engine */ + error = fmcd_xlate(sc, &ne->manip_id, FMCD_HDL_MANIP); + return (error); +} + +/* + * Translate handle IDs in a policer profile's color action params. + * Caller must hold sc->port_lock. + */ +static int +fmcd_translate_plcr_color(struct fmcd_softc *sc, + ioc_fm_pcd_engine next_engine, + ioc_fm_pcd_plcr_next_engine_params_u *p) +{ + int error; + + switch (next_engine) { + case e_IOC_FM_PCD_PLCR: + error = fmcd_xlate(sc, &p->p_profile, + FMCD_HDL_PLCR_PROFILE); + return (error); + case e_IOC_FM_PCD_KG: + error = fmcd_xlate(sc, &p->p_direct_scheme, + FMCD_HDL_KG_SCHEME); + return (error); + default: + return (0); + } +} + +/* ------------------------------------------------------------------ */ +/* PCD ioctl handlers */ +/* ------------------------------------------------------------------ */ + +static int +fmcd_ioc_pcd_enable(struct fmcd_softc *sc) +{ + t_Error err; + + err = FM_PCD_Enable(sc->pcd_handle); + return (err == E_OK ? 0 : EIO); +} + +static int +fmcd_ioc_pcd_disable(struct fmcd_softc *sc) +{ + t_Error err; + + err = FM_PCD_Disable(sc->pcd_handle); + return (err == E_OK ? 0 : EIO); +} + +static int +fmcd_ioc_get_api_version(struct fmcd_softc *sc, caddr_t data) +{ + ioc_fm_api_version_t *p = (ioc_fm_api_version_t *)data; + + p->version.major = FMD_API_VERSION_MAJOR; + p->version.minor = FMD_API_VERSION_MINOR; + p->version.respin = FMD_API_VERSION_RESPIN; + p->version.reserved = 0; + return (0); +} + +/* + * NetEnvCharacteristicsSet — _IOWR, framework copies in/out. + * Direct cast: ioc struct layout == NCSW struct layout. + */ +static int +fmcd_ioc_net_env_set(struct fmcd_softc *sc, caddr_t data) +{ + ioc_fm_pcd_net_env_params_t *param = + (ioc_fm_pcd_net_env_params_t *)data; + t_Handle ptr; + uint64_t id; + + ptr = FM_PCD_NetEnvCharacteristicsSet(sc->pcd_handle, + (t_FmPcdNetEnvParams *)param); + if (ptr == NULL) + return (EIO); + + sx_xlock(&sc->port_lock); + id = fmcd_handle_alloc(sc, ptr, FMCD_HDL_NET_ENV); + sx_xunlock(&sc->port_lock); + if (id == 0) { + FM_PCD_NetEnvCharacteristicsDelete(ptr); + return (ENOMEM); + } + + param->id = (void *)(uintptr_t)id; + return (0); +} + +static int +fmcd_ioc_net_env_delete(struct fmcd_softc *sc, caddr_t data) +{ + ioc_fm_obj_t *ioc = (ioc_fm_obj_t *)data; + uint64_t id = (uint64_t)(uintptr_t)ioc->obj; + t_Handle ptr; + + sx_xlock(&sc->port_lock); + ptr = fmcd_handle_free(sc, id, FMCD_HDL_NET_ENV); + sx_xunlock(&sc->port_lock); + + if (ptr == NULL) + return (EINVAL); + + return (FM_PCD_NetEnvCharacteristicsDelete(ptr) + == E_OK ? 0 : EIO); +} + +/* + * KgSchemeSet — _IOWR, framework copies in/out. + * Direct cast: ioc struct layout == NCSW struct layout + * (after adding bool shared to t_FmPcdKgSchemeParams). + */ +static int +fmcd_ioc_kg_scheme_set(struct fmcd_softc *sc, caddr_t data) +{ + ioc_fm_pcd_kg_scheme_params_t *param = + (ioc_fm_pcd_kg_scheme_params_t *)data; + t_FmPcdKgSchemeParams *ncsw = (t_FmPcdKgSchemeParams *)param; + t_Handle ptr; + uint64_t id; + int error; + + /* Translate input handle references */ + sx_xlock(&sc->port_lock); + if (param->modify) { + error = fmcd_xlate(sc, ¶m->scm_id.scheme_id, + FMCD_HDL_KG_SCHEME); + if (error) + goto unlock; + } + error = fmcd_xlate(sc, ¶m->net_env_params.net_env_id, + FMCD_HDL_NET_ENV); + if (error) + goto unlock; + if (param->next_engine == e_IOC_FM_PCD_CC) { + error = fmcd_xlate(sc, + ¶m->kg_next_engine_params.cc.tree_id, + FMCD_HDL_CC_ROOT); + if (error) + goto unlock; + } + /* PLCR profile in KG scheme context uses a numeric index + * (profile_select), not a handle — no translation needed. */ + sx_xunlock(&sc->port_lock); + + ptr = FM_PCD_KgSchemeSet(sc->pcd_handle, ncsw); + if (ptr == NULL) + return (EIO); + + sx_xlock(&sc->port_lock); + id = fmcd_handle_alloc(sc, ptr, FMCD_HDL_KG_SCHEME); + sx_xunlock(&sc->port_lock); + if (id == 0) { + FM_PCD_KgSchemeDelete(ptr); + return (ENOMEM); + } + + param->id = (void *)(uintptr_t)id; + return (0); + +unlock: + sx_xunlock(&sc->port_lock); + return (error); +} + +static int +fmcd_ioc_kg_scheme_delete(struct fmcd_softc *sc, caddr_t data) +{ + ioc_fm_obj_t *ioc = (ioc_fm_obj_t *)data; + uint64_t id = (uint64_t)(uintptr_t)ioc->obj; + t_Handle ptr; + + sx_xlock(&sc->port_lock); + ptr = fmcd_handle_free(sc, id, FMCD_HDL_KG_SCHEME); + sx_xunlock(&sc->port_lock); + + if (ptr == NULL) + return (EINVAL); + + return (FM_PCD_KgSchemeDelete(ptr) == E_OK ? 0 : EIO); +} + +/* + * CcRootBuild — _IO on FreeBSD (struct exceeds IOCPARM_MAX). + * Manual copyin/copyout, direct cast. + */ +static int +fmcd_ioc_cc_root_build(struct fmcd_softc *sc, caddr_t data) +{ + ioc_fm_pcd_cc_tree_params_t *param; + void *uptr; + t_Handle ptr; + uint64_t hid; + int i, j, error; + + uptr = *(void **)data; + if (uptr == NULL) + return (EINVAL); + + param = malloc(sizeof(*param), M_FMCD, M_WAITOK); + error = copyin(uptr, param, sizeof(*param)); + if (error) { + free(param, M_FMCD); + return (error); + } + + /* Translate input handle references */ + sx_xlock(&sc->port_lock); + error = fmcd_xlate(sc, ¶m->net_env_id, FMCD_HDL_NET_ENV); + if (error) + goto unlock; + + for (i = 0; i < param->num_of_groups && + i < IOC_FM_PCD_MAX_NUM_OF_CC_GROUPS; i++) { + uint8_t ndu = param->fm_pcd_cc_group_params[i]. + num_of_distinction_units; + int n_entries = (1 << ndu); /* 2^ndu entries per group */ + for (j = 0; j < n_entries && + j < IOC_FM_PCD_MAX_NUM_OF_CC_ENTRIES_IN_GRP; j++) { + ioc_fm_pcd_cc_next_engine_params_t *ne = + ¶m->fm_pcd_cc_group_params[i]. + next_engine_per_entries_in_grp[j]; + error = fmcd_translate_next_engine(sc, ne); + if (error) + goto unlock; + } + } + sx_xunlock(&sc->port_lock); + + ptr = FM_PCD_CcRootBuild(sc->pcd_handle, + (t_FmPcdCcTreeParams *)param); + if (ptr == NULL) { + free(param, M_FMCD); + return (EIO); + } + + sx_xlock(&sc->port_lock); + hid = fmcd_handle_alloc(sc, ptr, FMCD_HDL_CC_ROOT); + sx_xunlock(&sc->port_lock); + if (hid == 0) { + FM_PCD_CcRootDelete(ptr); + free(param, M_FMCD); + return (ENOMEM); + } + + param->id = (void *)(uintptr_t)hid; + error = copyout(param, uptr, sizeof(*param)); + free(param, M_FMCD); + return (error); + +unlock: + sx_xunlock(&sc->port_lock); + free(param, M_FMCD); + return (error); +} + +static int +fmcd_ioc_cc_root_delete(struct fmcd_softc *sc, caddr_t data) +{ + ioc_fm_obj_t *ioc = (ioc_fm_obj_t *)data; + uint64_t id = (uint64_t)(uintptr_t)ioc->obj; + t_Handle ptr; + + sx_xlock(&sc->port_lock); + ptr = fmcd_handle_free(sc, id, FMCD_HDL_CC_ROOT); + sx_xunlock(&sc->port_lock); + + if (ptr == NULL) + return (EINVAL); + + return (FM_PCD_CcRootDelete(ptr) == E_OK ? 0 : EIO); +} + +/* + * HashTableSet — _IOWR, framework copies in/out. + * Direct cast. + */ +static int +fmcd_ioc_hash_table_set(struct fmcd_softc *sc, caddr_t data) +{ + ioc_fm_pcd_hash_table_params_t *param = + (ioc_fm_pcd_hash_table_params_t *)data; + t_Handle ptr; + uint64_t id; + int error; + + /* Translate miss next-engine handle references */ + sx_xlock(&sc->port_lock); + error = fmcd_translate_next_engine(sc, + ¶m->cc_next_engine_params_for_miss); + sx_xunlock(&sc->port_lock); + if (error) + return (error); + + ptr = FM_PCD_HashTableSet(sc->pcd_handle, + (t_FmPcdHashTableParams *)param); + if (ptr == NULL) + return (EIO); + + sx_xlock(&sc->port_lock); + id = fmcd_handle_alloc(sc, ptr, FMCD_HDL_HASH_TABLE); + sx_xunlock(&sc->port_lock); + if (id == 0) { + FM_PCD_HashTableDelete(ptr); + return (ENOMEM); + } + + param->id = (void *)(uintptr_t)id; + return (0); +} + +static int +fmcd_ioc_hash_table_delete(struct fmcd_softc *sc, caddr_t data) +{ + ioc_fm_obj_t *ioc = (ioc_fm_obj_t *)data; + uint64_t id = (uint64_t)(uintptr_t)ioc->obj; + t_Handle ptr; + + sx_xlock(&sc->port_lock); + ptr = fmcd_handle_free(sc, id, FMCD_HDL_HASH_TABLE); + sx_xunlock(&sc->port_lock); + + if (ptr == NULL) + return (EINVAL); + + return (FM_PCD_HashTableDelete(ptr) == E_OK ? 0 : EIO); +} + +/* + * MatchTableSet — _IO on FreeBSD (struct exceeds IOCPARM_MAX). + * Manual copyin/copyout, direct cast. + * + * Special handling: key_params[i].p_key and p_mask point to userspace + * memory after copyin of the outer struct. Must copyin each key/mask + * buffer and redirect the pointers before calling NCSW. + */ +static int +fmcd_ioc_match_table_set(struct fmcd_softc *sc, caddr_t data) +{ + ioc_fm_pcd_cc_node_params_t *param; + void *uptr; + uint8_t key_size; + int i, error; + int processed_keys = 0; + + uptr = *(void **)data; + if (uptr == NULL) + return (EINVAL); + + param = malloc(sizeof(*param), M_FMCD, M_WAITOK); + error = copyin(uptr, param, sizeof(*param)); + if (error) { + free(param, M_FMCD); + return (error); + } + + key_size = param->keys_params.key_size; + + /* + * Copyin key and mask buffers from userspace. + * After copyin of the outer struct, p_key/p_mask still reference + * userspace addresses. Allocate kernel buffers, copyin, redirect. + * + * Track processed_keys so the cleanup path only frees keys whose + * pointers were replaced with kernel allocations. Keys beyond + * processed_keys still hold raw userspace addresses from the + * outer copyin and must NOT be freed. + */ + for (i = 0; i < param->keys_params.num_of_keys && + i < IOC_FM_PCD_MAX_NUM_OF_KEYS; i++) { + ioc_fm_pcd_cc_key_params_t *kp = + ¶m->keys_params.key_params[i]; + void *umask = kp->p_mask; + + /* + * Clear p_mask before processing p_key so that if + * p_key copyin fails, the cleanup path won't call + * free() on the userspace address still in p_mask. + */ + kp->p_mask = NULL; + + if (kp->p_key != NULL && key_size > 0) { + void *usrc = kp->p_key; + kp->p_key = malloc(key_size, M_FMCD, M_WAITOK); + processed_keys = i + 1; + error = copyin(usrc, kp->p_key, key_size); + if (error) + goto fail; + } + + if (umask != NULL && key_size > 0) { + kp->p_mask = malloc(key_size, M_FMCD, M_WAITOK); + processed_keys = i + 1; + error = copyin(umask, kp->p_mask, key_size); + if (error) + goto fail; + } + + processed_keys = i + 1; + } + + /* Translate handle references in next-engine params */ + sx_xlock(&sc->port_lock); + for (i = 0; i < param->keys_params.num_of_keys && + i < IOC_FM_PCD_MAX_NUM_OF_KEYS; i++) { + error = fmcd_translate_next_engine(sc, + ¶m->keys_params.key_params[i].cc_next_engine_params); + if (error) { + sx_xunlock(&sc->port_lock); + goto fail; + } + } + error = fmcd_translate_next_engine(sc, + ¶m->keys_params.cc_next_engine_params_for_miss); + sx_xunlock(&sc->port_lock); + if (error) + goto fail; + + { + t_Handle ptr; + uint64_t hid; + + ptr = FM_PCD_MatchTableSet(sc->pcd_handle, + (t_FmPcdCcNodeParams *)param); + if (ptr == NULL) { + error = EIO; + goto fail; + } + + sx_xlock(&sc->port_lock); + hid = fmcd_handle_alloc(sc, ptr, FMCD_HDL_MATCH_TABLE); + sx_xunlock(&sc->port_lock); + if (hid == 0) { + FM_PCD_MatchTableDelete(ptr); + error = ENOMEM; + goto fail; + } + + param->id = (void *)(uintptr_t)hid; + } + + error = copyout(param, uptr, sizeof(*param)); + +fail: + /* + * Free only key/mask buffers that were replaced with kernel + * allocations (indices 0..processed_keys-1). Entries beyond + * processed_keys still hold userspace addresses and must not + * be passed to free(). + */ + for (i = 0; i < processed_keys; i++) { + ioc_fm_pcd_cc_key_params_t *kp = + ¶m->keys_params.key_params[i]; + if (kp->p_key != NULL) + free(kp->p_key, M_FMCD); + if (kp->p_mask != NULL) + free(kp->p_mask, M_FMCD); + } + free(param, M_FMCD); + return (error); +} + +static int +fmcd_ioc_match_table_delete(struct fmcd_softc *sc, caddr_t data) +{ + ioc_fm_obj_t *ioc = (ioc_fm_obj_t *)data; + uint64_t id = (uint64_t)(uintptr_t)ioc->obj; + t_Handle ptr; + + sx_xlock(&sc->port_lock); + ptr = fmcd_handle_free(sc, id, FMCD_HDL_MATCH_TABLE); + sx_xunlock(&sc->port_lock); + + if (ptr == NULL) + return (EINVAL); + + return (FM_PCD_MatchTableDelete(ptr) == E_OK ? 0 : EIO); +} + +/* + * ManipNodeSet — _IOWR, framework copies in/out. + * Direct cast. + */ +static int +fmcd_ioc_manip_node_set(struct fmcd_softc *sc, caddr_t data) +{ + ioc_fm_pcd_manip_params_t *param = + (ioc_fm_pcd_manip_params_t *)data; + t_Handle ptr; + uint64_t id; + int error; + + /* Translate chained manipulation handle */ + sx_xlock(&sc->port_lock); + error = fmcd_xlate(sc, ¶m->p_next_manip, FMCD_HDL_MANIP); + sx_xunlock(&sc->port_lock); + if (error) + return (error); + + ptr = FM_PCD_ManipNodeSet(sc->pcd_handle, + (t_FmPcdManipParams *)param); + if (ptr == NULL) + return (EIO); + + sx_xlock(&sc->port_lock); + id = fmcd_handle_alloc(sc, ptr, FMCD_HDL_MANIP); + sx_xunlock(&sc->port_lock); + if (id == 0) { + FM_PCD_ManipNodeDelete(ptr); + return (ENOMEM); + } + + param->id = (void *)(uintptr_t)id; + return (0); +} + +static int +fmcd_ioc_manip_node_delete(struct fmcd_softc *sc, caddr_t data) +{ + ioc_fm_obj_t *ioc = (ioc_fm_obj_t *)data; + uint64_t id = (uint64_t)(uintptr_t)ioc->obj; + t_Handle ptr; + + sx_xlock(&sc->port_lock); + ptr = fmcd_handle_free(sc, id, FMCD_HDL_MANIP); + sx_xunlock(&sc->port_lock); + + if (ptr == NULL) + return (EINVAL); + + return (FM_PCD_ManipNodeDelete(ptr) == E_OK ? 0 : EIO); +} + +#if (DPAA_VERSION >= 11) +/* + * FrmReplicSetGroup — _IO on FreeBSD (struct exceeds IOCPARM_MAX). + * Manual copyin/copyout, direct cast. + */ +static int +fmcd_ioc_frm_replic_set_group(struct fmcd_softc *sc, caddr_t data) +{ + ioc_fm_pcd_frm_replic_group_params_t *param; + void *uptr; + t_Handle ptr; + uint64_t hid; + int i, error; + + uptr = *(void **)data; + if (uptr == NULL) + return (EINVAL); + + param = malloc(sizeof(*param), M_FMCD, M_WAITOK); + error = copyin(uptr, param, sizeof(*param)); + if (error) { + free(param, M_FMCD); + return (error); + } + + /* Translate handle references in each entry's next-engine params */ + sx_xlock(&sc->port_lock); + for (i = 0; i < param->num_of_entries && + i < IOC_FM_PCD_FRM_REPLIC_MAX_NUM_OF_ENTRIES; i++) { + error = fmcd_translate_next_engine(sc, + ¶m->next_engine_params[i]); + if (error) { + sx_xunlock(&sc->port_lock); + free(param, M_FMCD); + return (error); + } + } + sx_xunlock(&sc->port_lock); + + ptr = FM_PCD_FrmReplicSetGroup(sc->pcd_handle, + (t_FmPcdFrmReplicGroupParams *)param); + if (ptr == NULL) { + free(param, M_FMCD); + return (EIO); + } + + sx_xlock(&sc->port_lock); + hid = fmcd_handle_alloc(sc, ptr, FMCD_HDL_FRM_REPLIC); + sx_xunlock(&sc->port_lock); + if (hid == 0) { + FM_PCD_FrmReplicDeleteGroup(ptr); + free(param, M_FMCD); + return (ENOMEM); + } + + param->id = (void *)(uintptr_t)hid; + error = copyout(param, uptr, sizeof(*param)); + free(param, M_FMCD); + return (error); +} + +static int +fmcd_ioc_frm_replic_delete_group(struct fmcd_softc *sc, caddr_t data) +{ + ioc_fm_obj_t *ioc = (ioc_fm_obj_t *)data; + uint64_t id = (uint64_t)(uintptr_t)ioc->obj; + t_Handle ptr; + + sx_xlock(&sc->port_lock); + ptr = fmcd_handle_free(sc, id, FMCD_HDL_FRM_REPLIC); + sx_xunlock(&sc->port_lock); + + if (ptr == NULL) + return (EINVAL); + + return (FM_PCD_FrmReplicDeleteGroup(ptr) == E_OK ? 0 : EIO); +} +#endif /* DPAA_VERSION >= 11 */ + +/* + * PrsLoadSw — _IOW, framework copies in. + * Direct cast, but p_code pointer needs copyin + redirect. + */ +static int +fmcd_ioc_prs_load_sw(struct fmcd_softc *sc, caddr_t data) +{ + ioc_fm_pcd_prs_sw_params_t *param = + (ioc_fm_pcd_prs_sw_params_t *)data; + uint8_t *code_buf; + t_Error err; + + if (param->size == 0 || param->p_code == NULL) + return (EINVAL); + if (param->size > FM_PCD_SW_PRS_SIZE) + return (EINVAL); + + /* Copyin the parser microcode from userspace */ + code_buf = malloc(param->size, M_FMCD, M_WAITOK); + if (copyin(param->p_code, code_buf, param->size) != 0) { + free(code_buf, M_FMCD); + return (EFAULT); + } + + /* Redirect pointer to kernel buffer */ + param->p_code = code_buf; + + err = FM_PCD_PrsLoadSw(sc->pcd_handle, (t_FmPcdPrsSwParams *)param); + free(code_buf, M_FMCD); + + return (err == E_OK ? 0 : EIO); +} + +/* + * PlcrProfileSet — _IOWR, framework copies in/out. + * Direct cast. + */ +static int +fmcd_ioc_plcr_profile_set(struct fmcd_softc *sc, caddr_t data) +{ + ioc_fm_pcd_plcr_profile_params_t *param = + (ioc_fm_pcd_plcr_profile_params_t *)data; + t_Handle ptr; + uint64_t id; + int error; + + /* Translate input handle references */ + sx_xlock(&sc->port_lock); + if (param->modify) { + error = fmcd_xlate(sc, ¶m->profile_select.p_profile, + FMCD_HDL_PLCR_PROFILE); + if (error) + goto unlock; + } + error = fmcd_translate_plcr_color(sc, param->next_engine_on_green, + ¶m->params_on_green); + if (error) + goto unlock; + error = fmcd_translate_plcr_color(sc, param->next_engine_on_yellow, + ¶m->params_on_yellow); + if (error) + goto unlock; + error = fmcd_translate_plcr_color(sc, param->next_engine_on_red, + ¶m->params_on_red); + if (error) + goto unlock; + sx_xunlock(&sc->port_lock); + + ptr = FM_PCD_PlcrProfileSet(sc->pcd_handle, + (t_FmPcdPlcrProfileParams *)param); + if (ptr == NULL) + return (EIO); + + sx_xlock(&sc->port_lock); + id = fmcd_handle_alloc(sc, ptr, FMCD_HDL_PLCR_PROFILE); + sx_xunlock(&sc->port_lock); + if (id == 0) { + FM_PCD_PlcrProfileDelete(ptr); + return (ENOMEM); + } + + param->id = (void *)(uintptr_t)id; + return (0); + +unlock: + sx_xunlock(&sc->port_lock); + return (error); +} + +static int +fmcd_ioc_plcr_profile_delete(struct fmcd_softc *sc, caddr_t data) +{ + ioc_fm_obj_t *ioc = (ioc_fm_obj_t *)data; + uint64_t id = (uint64_t)(uintptr_t)ioc->obj; + t_Handle ptr; + + sx_xlock(&sc->port_lock); + ptr = fmcd_handle_free(sc, id, FMCD_HDL_PLCR_PROFILE); + sx_xunlock(&sc->port_lock); + + if (ptr == NULL) + return (EINVAL); + + return (FM_PCD_PlcrProfileDelete(ptr) == E_OK ? 0 : EIO); +} + +/* ------------------------------------------------------------------ */ +/* Port ioctl handlers */ +/* ------------------------------------------------------------------ */ + +/* + * PortSetPCD — _IOW, framework copies in the outer struct. + * The pointer members (p_prs_params, p_cc_params, p_kg_params, + * p_plcr_params) still reference userspace sub-structs after copyin. + * Copyin each and redirect, then direct-cast to NCSW type. + * + * Handle references (net_env_id, cc_tree_id, scheme_ids[], etc.) + * are translated from opaque IDs to kernel pointers. + */ +static int +fmcd_ioc_port_set_pcd(struct fmcd_softc *sc, t_Handle h_port, caddr_t data) +{ + ioc_fm_port_pcd_params_t *param = (ioc_fm_port_pcd_params_t *)data; + ioc_fm_port_pcd_prs_params_t *prs = NULL; + ioc_fm_port_pcd_cc_params_t *cc_p = NULL; + ioc_fm_port_pcd_kg_params_t *kg_p = NULL; + ioc_fm_port_pcd_plcr_params_t *plcr = NULL; + t_Error err; + int i, error = 0; + + if (h_port == NULL) + return (ENXIO); + + /* Copyin sub-structs from userspace pointers */ + if (param->p_prs_params != NULL) { + prs = malloc(sizeof(*prs), M_FMCD, M_WAITOK); + error = copyin(param->p_prs_params, prs, sizeof(*prs)); + if (error) + goto out; + param->p_prs_params = prs; + } + + if (param->p_cc_params != NULL) { + cc_p = malloc(sizeof(*cc_p), M_FMCD, M_WAITOK); + error = copyin(param->p_cc_params, cc_p, sizeof(*cc_p)); + if (error) + goto out; + param->p_cc_params = cc_p; + } + + if (param->p_kg_params != NULL) { + kg_p = malloc(sizeof(*kg_p), M_FMCD, M_WAITOK); + error = copyin(param->p_kg_params, kg_p, sizeof(*kg_p)); + if (error) + goto out; + param->p_kg_params = kg_p; + } + + if (param->p_plcr_params != NULL) { + plcr = malloc(sizeof(*plcr), M_FMCD, M_WAITOK); + error = copyin(param->p_plcr_params, plcr, sizeof(*plcr)); + if (error) + goto out; + param->p_plcr_params = plcr; + } + + /* Translate handle references in copyin'd sub-structs */ + sx_xlock(&sc->port_lock); + error = fmcd_xlate(sc, ¶m->net_env_id, FMCD_HDL_NET_ENV); + if (error) + goto unlock; + + if (cc_p != NULL) { + error = fmcd_xlate(sc, &cc_p->cc_tree_id, FMCD_HDL_CC_ROOT); + if (error) + goto unlock; + } + + if (kg_p != NULL) { + for (i = 0; i < kg_p->num_of_schemes && + i < FM_PCD_KG_NUM_OF_SCHEMES; i++) { + error = fmcd_xlate(sc, &kg_p->scheme_ids[i], + FMCD_HDL_KG_SCHEME); + if (error) + goto unlock; + } + if (kg_p->direct_scheme) { + error = fmcd_xlate(sc, &kg_p->direct_scheme_id, + FMCD_HDL_KG_SCHEME); + if (error) + goto unlock; + } + } + + if (plcr != NULL) { + error = fmcd_xlate(sc, &plcr->plcr_profile_id, + FMCD_HDL_PLCR_PROFILE); + if (error) + goto unlock; + } + + error = fmcd_xlate(sc, ¶m->p_ip_reassembly_manip, + FMCD_HDL_MANIP); + if (error) + goto unlock; + error = fmcd_xlate(sc, ¶m->p_capwap_reassembly_manip, + FMCD_HDL_MANIP); + if (error) + goto unlock; + sx_xunlock(&sc->port_lock); + + err = FM_PORT_SetPCD(h_port, (t_FmPortPcdParams *)param); + if (err != E_OK) { + printf("fmcd: SetPCD FAILED err=%d\n", (int)err); + error = EIO; + goto out; + } + +out: + free(prs, M_FMCD); + free(cc_p, M_FMCD); + free(kg_p, M_FMCD); + free(plcr, M_FMCD); + return (error); + +unlock: + sx_xunlock(&sc->port_lock); + goto out; +} + +static int +fmcd_ioc_port_delete_pcd(struct fmcd_softc *sc, t_Handle h_port) +{ + + if (h_port == NULL) + return (ENXIO); + + { + t_Error err = FM_PORT_DeletePCD(h_port); + return (err == E_OK ? 0 : EIO); + } +} + +static int +fmcd_ioc_port_enable(struct fmcd_softc *sc, t_Handle h_port) +{ + t_Error err; + + if (h_port == NULL) { + printf("fmcd: port_enable: h_port=NULL!\n"); + return (ENXIO); + } + + err = FM_PORT_Enable(h_port); + return (err == E_OK ? 0 : EIO); +} + +static int +fmcd_ioc_port_disable(struct fmcd_softc *sc, t_Handle h_port) +{ + t_Error err; + + if (h_port == NULL) { + printf("fmcd: port_disable: h_port=NULL!\n"); + return (ENXIO); + } + + err = FM_PORT_Disable(h_port); + return (err == E_OK ? 0 : EIO); +} + +/* ------------------------------------------------------------------ */ +/* Chardev operations */ +/* ------------------------------------------------------------------ */ + +static int +fmcd_open(struct cdev *dev, int flags, int fmt, struct thread *td) +{ + return (0); +} + +/* + * PCD device open handler. + * + * When userspace opens /dev/fm0-pcd, it is taking over PCD management + * (e.g., FMC/dpa_app is starting). Tear down any existing PCD set up + * by the dtsec driver (RSS KG schemes) to free KG scheme IDs for FMC. + * Also reset the handle table and bump the generation counter to + * invalidate any stale handles from a previous session. + */ +static int +fmcd_pcd_open(struct cdev *dev, int flags, int fmt, struct thread *td) +{ + struct fmcd_softc *sc = dev->si_drv1; + int i; + + sx_xlock(&sc->port_lock); + + for (i = 0; i < 8; i++) { + if (sc->rx_port_schemes[i] != NULL) { + if (sc->rx_port_handles[i] != NULL) + FM_PORT_DeletePCD(sc->rx_port_handles[i]); + FM_PCD_KgSchemeDelete(sc->rx_port_schemes[i]); + sc->rx_port_schemes[i] = NULL; + /* Clear dtsec's handle so pcd_free() won't + * double-free */ + if (sc->rx_port_scheme_ptrs[i] != NULL) { + *sc->rx_port_scheme_ptrs[i] = NULL; + sc->rx_port_scheme_ptrs[i] = NULL; + } + } + } + + /* + * Clear handle table and bump generation. We don't delete + * NCSW objects here — the previous session should have cleaned + * up via close, and the PCD enable/disable cycle that + * fmc/dpa_app does on startup resets PCD state. + */ + for (i = 0; i < FMCD_MAX_HANDLES; i++) { + sc->handles[i].ptr = NULL; + sc->handles[i].type = FMCD_HDL_NONE; + } + sc->handle_gen++; + if (sc->handle_gen == 0) + sc->handle_gen = 1; + + sx_xunlock(&sc->port_lock); + + return (0); +} + +/* + * PCD device close handler. + * + * Delete all PCD objects created during this session in dependency + * order (reverse of creation). This prevents kernel memory leaks + * if userspace crashes without explicit cleanup. + */ +static int +fmcd_pcd_close(struct cdev *dev, int flags, int fmt, struct thread *td) +{ + struct fmcd_softc *sc = dev->si_drv1; + int i; + + sx_xlock(&sc->port_lock); + + if (sc->pcd_transferred) { + /* + * CDX has taken ownership of all PCD resources via + * CDX_CTRL_DPA_SET_PARAMS. The NCSW objects (hash tables, + * CC trees, KG schemes, net envs) are bound to FMan ports + * and must not be deleted. Just clear the handle table. + */ + for (i = 0; i < FMCD_MAX_HANDLES; i++) { + sc->handles[i].ptr = NULL; + sc->handles[i].type = FMCD_HDL_NONE; + } + } else { + /* + * No CDX takeover — dpa_app exited before CDX claimed + * the PCD resources. Delete all NCSW objects in + * dependency order to prevent kernel memory leaks. + */ + static const enum fmcd_handle_type teardown_order[] = { + FMCD_HDL_FRM_REPLIC, + FMCD_HDL_PLCR_PROFILE, + FMCD_HDL_MATCH_TABLE, + FMCD_HDL_HASH_TABLE, + FMCD_HDL_CC_ROOT, + FMCD_HDL_KG_SCHEME, + FMCD_HDL_MANIP, + FMCD_HDL_NET_ENV, + }; + int phase; + + for (phase = 0; phase < nitems(teardown_order); phase++) { + for (i = 0; i < FMCD_MAX_HANDLES; i++) { + t_Handle ptr = sc->handles[i].ptr; + t_Error err; + + if (ptr == NULL || + sc->handles[i].type != + teardown_order[phase]) + continue; + + sc->handles[i].ptr = NULL; + sc->handles[i].type = FMCD_HDL_NONE; + + switch (teardown_order[phase]) { + case FMCD_HDL_NET_ENV: + err = FM_PCD_NetEnvCharacteristicsDelete(ptr); + break; + case FMCD_HDL_KG_SCHEME: + err = FM_PCD_KgSchemeDelete(ptr); + break; + case FMCD_HDL_CC_ROOT: + err = FM_PCD_CcRootDelete(ptr); + break; + case FMCD_HDL_MATCH_TABLE: + err = FM_PCD_MatchTableDelete(ptr); + break; + case FMCD_HDL_HASH_TABLE: + err = FM_PCD_HashTableDelete(ptr); + break; + case FMCD_HDL_MANIP: + err = FM_PCD_ManipNodeDelete(ptr); + break; + case FMCD_HDL_PLCR_PROFILE: + err = FM_PCD_PlcrProfileDelete(ptr); + break; + case FMCD_HDL_FRM_REPLIC: + err = FM_PCD_FrmReplicDeleteGroup(ptr); + break; + default: + err = E_OK; + break; + } + + if (err != E_OK) + printf("fmcd: close: failed to " + "delete slot %d type %d\n", + i, teardown_order[phase]); + } + } + } + + /* Bump generation to invalidate any lingering handle IDs */ + sc->handle_gen++; + if (sc->handle_gen == 0) + sc->handle_gen = 1; + + sc->pcd_transferred = false; + sx_xunlock(&sc->port_lock); + return (0); +} + +static int +fmcd_close(struct cdev *dev, int flags, int fmt, struct thread *td) +{ + return (0); +} + +/* + * PCD device ioctl handler (/dev/fm0-pcd). + */ +static int +fmcd_pcd_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, + struct thread *td) +{ + struct fmcd_softc *sc = dev->si_drv1; + int error; + + /* PCD operations require root */ + error = priv_check(td, PRIV_DRIVER); + if (error) + return (error); + + if (sc->pcd_handle == NULL) + return (ENXIO); + + switch (cmd) { + case FM_PCD_IOC_ENABLE: + return (fmcd_ioc_pcd_enable(sc)); + case FM_PCD_IOC_DISABLE: + return (fmcd_ioc_pcd_disable(sc)); + case FM_PCD_IOC_NET_ENV_CHARACTERISTICS_SET: + return (fmcd_ioc_net_env_set(sc, data)); + case FM_PCD_IOC_NET_ENV_CHARACTERISTICS_DELETE: + return (fmcd_ioc_net_env_delete(sc, data)); + case FM_PCD_IOC_KG_SCHEME_SET: + return (fmcd_ioc_kg_scheme_set(sc, data)); + case FM_PCD_IOC_KG_SCHEME_DELETE: + return (fmcd_ioc_kg_scheme_delete(sc, data)); + case FM_PCD_IOC_CC_ROOT_BUILD: + return (fmcd_ioc_cc_root_build(sc, data)); + case FM_PCD_IOC_CC_ROOT_DELETE: + return (fmcd_ioc_cc_root_delete(sc, data)); + case FM_PCD_IOC_HASH_TABLE_SET: + return (fmcd_ioc_hash_table_set(sc, data)); + case FM_PCD_IOC_HASH_TABLE_DELETE: + return (fmcd_ioc_hash_table_delete(sc, data)); + case FM_PCD_IOC_MATCH_TABLE_SET: + return (fmcd_ioc_match_table_set(sc, data)); + case FM_PCD_IOC_MATCH_TABLE_DELETE: + return (fmcd_ioc_match_table_delete(sc, data)); + case FM_PCD_IOC_MANIP_NODE_SET: + return (fmcd_ioc_manip_node_set(sc, data)); + case FM_PCD_IOC_MANIP_NODE_DELETE: + return (fmcd_ioc_manip_node_delete(sc, data)); +#if (DPAA_VERSION >= 11) + case FM_PCD_IOC_FRM_REPLIC_GROUP_SET: + return (fmcd_ioc_frm_replic_set_group(sc, data)); + case FM_PCD_IOC_FRM_REPLIC_GROUP_DELETE: + return (fmcd_ioc_frm_replic_delete_group(sc, data)); +#endif + case FM_PCD_IOC_PRS_LOAD_SW: + return (fmcd_ioc_prs_load_sw(sc, data)); + case FM_PCD_IOC_PLCR_PROFILE_SET: + return (fmcd_ioc_plcr_profile_set(sc, data)); + case FM_PCD_IOC_PLCR_PROFILE_DELETE: + return (fmcd_ioc_plcr_profile_delete(sc, data)); + case FM_PCD_IOC_SET_ADVANCED_OFFLOAD_SUPPORT: + return (FM_PCD_SetAdvancedOffloadSupport(sc->pcd_handle) + == E_OK ? 0 : EIO); + case FM_PCD_IOC_ALLOW_HC_USAGE: + /* HC is already configured by fman.c — just accept */ + return (0); + default: + printf("fmcd: unknown PCD ioctl 0x%lx\n", cmd); + return (ENOTTY); + } +} + +/* + * FM device ioctl handler (/dev/fm0). + */ +static int +fmcd_fm_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, + struct thread *td) +{ + struct fmcd_softc *sc = dev->si_drv1; + + switch (cmd) { + case FM_IOC_GET_API_VERSION: + return (fmcd_ioc_get_api_version(sc, data)); + default: + printf("fmcd: unknown FM ioctl 0x%lx\n", cmd); + return (ENOTTY); + } +} + +/* + * Port device ioctl handler (/dev/fm0-port-rx*, /dev/fm0-port-oh*). + */ +static int +fmcd_port_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, + struct thread *td) +{ + struct fmcd_softc *sc = dev->si_drv1; + t_Handle h_port = dev->si_drv2; + int error; + + error = priv_check(td, PRIV_DRIVER); + if (error) + return (error); + + switch (cmd) { + case FM_PORT_IOC_SET_PCD: + return (fmcd_ioc_port_set_pcd(sc, h_port, data)); + case FM_PORT_IOC_DELETE_PCD: + return (fmcd_ioc_port_delete_pcd(sc, h_port)); + case FM_PORT_IOC_ENABLE: + return (fmcd_ioc_port_enable(sc, h_port)); + case FM_PORT_IOC_DISABLE: + return (fmcd_ioc_port_disable(sc, h_port)); + default: + printf("fmcd: unknown PORT ioctl 0x%lx\n", cmd); + return (ENOTTY); + } +} + +static struct cdevsw fmcd_fm_cdevsw = { + .d_version = D_VERSION, + .d_open = fmcd_open, + .d_close = fmcd_close, + .d_ioctl = fmcd_fm_ioctl, + .d_name = "fm", +}; + +static struct cdevsw fmcd_pcd_cdevsw = { + .d_version = D_VERSION, + .d_open = fmcd_pcd_open, + .d_close = fmcd_pcd_close, + .d_ioctl = fmcd_pcd_ioctl, + .d_name = "fm-pcd", +}; + +static struct cdevsw fmcd_port_cdevsw = { + .d_version = D_VERSION, + .d_open = fmcd_open, + .d_close = fmcd_close, + .d_ioctl = fmcd_port_ioctl, + .d_name = "fm-port", +}; + +/* ------------------------------------------------------------------ */ +/* Init / Destroy */ +/* ------------------------------------------------------------------ */ + +int +fmcd_init(device_t fman_dev, struct fmcd_softc *sc) +{ + struct fman_softc *fsc; + int i; + + fsc = device_get_softc(fman_dev); + + memset(sc, 0, sizeof(*sc)); + sc->fman_dev = fman_dev; + sc->fm_handle = fsc->fm_handle; + sc->pcd_handle = fsc->pcd_handle; + sc->handle_gen = 1; /* start at 1 so slot 0's first ID != 0 */ + + sx_init(&sc->port_lock, "fmcd_port"); + + /* /dev/fman0 */ + sc->cdev_fm = make_dev(&fmcd_fm_cdevsw, FMD_MINOR_FM, + UID_ROOT, GID_WHEEL, 0600, "fm%d", device_get_unit(fman_dev)); + if (sc->cdev_fm == NULL) { + device_printf(fman_dev, "fmcd: couldn't create /dev/fm\n"); + goto fail; + } + sc->cdev_fm->si_drv1 = sc; + + /* /dev/fman0-pcd */ + sc->cdev_pcd = make_dev(&fmcd_pcd_cdevsw, FMD_MINOR_PCD, + UID_ROOT, GID_WHEEL, 0600, "fm%d-pcd", + device_get_unit(fman_dev)); + if (sc->cdev_pcd == NULL) { + device_printf(fman_dev, + "fmcd: couldn't create /dev/fm-pcd\n"); + goto fail; + } + sc->cdev_pcd->si_drv1 = sc; + + /* + * Port devices are created eagerly — the handles are filled in + * when dtsec/oh drivers attach via fmcd_register_rx_port/oh_port. + */ + for (i = 0; i < 8; i++) { + sc->cdev_port_rx[i] = make_dev(&fmcd_port_cdevsw, + FMD_MINOR_RX_BASE + i, UID_ROOT, GID_WHEEL, 0600, + "fm%d-port-rx%d", device_get_unit(fman_dev), i); + if (sc->cdev_port_rx[i]) { + sc->cdev_port_rx[i]->si_drv1 = sc; + sc->cdev_port_rx[i]->si_drv2 = NULL; + } + } + + for (i = 0; i < 6; i++) { + sc->cdev_port_oh[i] = make_dev(&fmcd_port_cdevsw, + FMD_MINOR_OH_BASE + i, UID_ROOT, GID_WHEEL, 0600, + "fm%d-port-oh%d", device_get_unit(fman_dev), i); + if (sc->cdev_port_oh[i]) { + sc->cdev_port_oh[i]->si_drv1 = sc; + sc->cdev_port_oh[i]->si_drv2 = NULL; + } + } + + device_printf(fman_dev, "fmcd: chardev interface ready\n"); + return (0); + +fail: + fmcd_destroy(sc); + return (ENXIO); +} + +void +fmcd_destroy(struct fmcd_softc *sc) +{ + int i; + + for (i = 0; i < 6; i++) { + if (sc->cdev_port_oh[i]) + destroy_dev(sc->cdev_port_oh[i]); + } + for (i = 0; i < 8; i++) { + if (sc->cdev_port_rx[i]) + destroy_dev(sc->cdev_port_rx[i]); + } + if (sc->cdev_pcd) + destroy_dev(sc->cdev_pcd); + if (sc->cdev_fm) + destroy_dev(sc->cdev_fm); + + sx_destroy(&sc->port_lock); +} diff --git a/sys/dev/dpaa/fman_chardev.h b/sys/dev/dpaa/fman_chardev.h new file mode 100644 index 00000000000..523ba7ad8c0 --- /dev/null +++ b/sys/dev/dpaa/fman_chardev.h @@ -0,0 +1,155 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2026 Mono Technologies Inc. + * All rights reserved. + * + * FMan character device interface — exposes NCSW PCD API to userspace. + * + * Ioctl command numbers and data structures are ABI-compatible with the + * Linux NXP SDK FMD driver so that the same fmlib/FMC userspace tools + * work unchanged. + * + * Handle management: NCSW returns opaque kernel pointers (t_Handle). + * Userspace receives opaque integer handle IDs via a kernel handle + * table — kernel pointers are never exposed. The ioc structs have + * identical binary layouts to the NCSW structs (with a trailing + * void *id field); the kernel translates handle IDs to/from kernel + * pointers at the ioctl boundary. + * + * The ioctl struct definitions come from the fmlib ioctl headers + * (fm_pcd_ioctls.h, fm_port_ioctls.h) which are included directly + * by fman_chardev.c via the FMCD_COMPILE_CMD include paths. + * This header only exposes kernel-private definitions. + */ + +#ifndef FMAN_CHARDEV_H_ +#define FMAN_CHARDEV_H_ + +/* + * Device minor number layout. + * For FMan instance 0: + * minor 0 = /dev/fm0 (FM management) + * minor 1 = /dev/fm0-pcd (PCD operations) + * minor 2 = /dev/fm0-port-oh0 (OH port, cell-index 3) + * minor 3 = /dev/fm0-port-oh1 (OH port, cell-index 4) + * minor 8-15 = /dev/fm0-port-rx0..rx7 (RX ports, fmlib convention: + * 0-5 = 1G, 6-7 = 10G) + */ +#define FMD_MINOR_FM 0 +#define FMD_MINOR_PCD 1 +#define FMD_MINOR_OH_BASE 2 +#define FMD_MINOR_RX_BASE 8 /* after 6 OH slots */ +#define FMD_MINOR_MAX 16 /* 8 + 8 RX port slots */ + +/* ------------------------------------------------------------------ */ +/* Kernel-only: chardev state and functions */ +/* ------------------------------------------------------------------ */ + +#ifdef _KERNEL + +#include +#include +#include + +/* + * PCD handle table — maps opaque integer IDs (exposed to userspace) + * to kernel pointers (used internally by NCSW). + * + * Handle ID encoding (uint64_t, same width as void* on arm64): + * bits [6:0] = slot index (0..FMCD_MAX_HANDLES-1) + * bits [31:7] = generation counter (invalidates stale handles) + * bits [63:32] = 0 (ensures ID never resembles a kernel pointer) + * + * Value 0 is reserved as "invalid/NULL handle". handle_gen starts + * at 1 so slot 0's first ID is (1 << 7) | 0 = 128, not 0. + */ +enum fmcd_handle_type { + FMCD_HDL_NONE = 0, + FMCD_HDL_NET_ENV, + FMCD_HDL_KG_SCHEME, + FMCD_HDL_CC_ROOT, + FMCD_HDL_MATCH_TABLE, + FMCD_HDL_HASH_TABLE, + FMCD_HDL_MANIP, + FMCD_HDL_PLCR_PROFILE, + FMCD_HDL_FRM_REPLIC, +}; + +struct fmcd_handle_entry { + t_Handle ptr; /* kernel pointer (NULL = free slot) */ + enum fmcd_handle_type type; +}; + +#define FMCD_MAX_HANDLES 128 +#define FMCD_HANDLE_MASK 0x7f /* slot index in lower 7 bits */ +#define FMCD_HANDLE_SHIFT 7 /* generation counter above */ + +/* + * Per-FMan chardev state. + */ +struct fmcd_softc { + device_t fman_dev; /* parent fman device */ + struct cdev *cdev_fm; /* /dev/fm0 */ + struct cdev *cdev_pcd; /* /dev/fm0-pcd */ + struct cdev *cdev_port_rx[8]; /* /dev/fm0-port-rx[0-7] */ + struct cdev *cdev_port_oh[6]; /* /dev/fm0-port-oh[0-5] */ + + t_Handle fm_handle; /* NCSW FM handle */ + t_Handle pcd_handle; /* NCSW PCD handle */ + + /* Port handle registry — indexed by fmlib convention: + * 0-5 = 1G MACs (cell-index 0-5), 6-7 = 10G MACs (cell-index 8-9). + * fmcd_cell_to_rxidx() in fman_chardev.c converts DT cell-index. */ + struct sx port_lock; + t_Handle rx_port_handles[8]; + t_Handle rx_port_schemes[8]; /* dtsec KG scheme handles */ + t_Handle *rx_port_scheme_ptrs[8]; /* ptr to dtsec sc_scheme */ + uint32_t rx_port_fqids[8]; /* dtsec default RX FQIDs */ + t_Handle oh_port_handles[6]; + + /* PCD handle table — protected by port_lock */ + struct fmcd_handle_entry handles[FMCD_MAX_HANDLES]; + uint32_t handle_gen; /* generation counter */ + bool pcd_transferred; /* CDX has taken PCD ownership */ +}; + +/* + * Chardev lifecycle. + */ +int fmcd_init(device_t fman_dev, struct fmcd_softc *sc); +void fmcd_destroy(struct fmcd_softc *sc); + +/* + * Port registration (called by dtsec and dpaa_oh during attach). + */ +void fmcd_register_rx_port(struct fmcd_softc *sc, int index, + t_Handle h_port, uint32_t dflt_fqid); +void fmcd_register_oh_port(struct fmcd_softc *sc, int index, + t_Handle h_port); +uint32_t fmcd_get_rx_dflt_fqid(struct fmcd_softc *sc, int index); + +/* + * Register dtsec's KG scheme handle for PCD teardown. + * p_scheme points to dtsec_softc->sc_scheme so the chardev can + * clear it when userspace takes over PCD management. + */ +void fmcd_register_rx_port_scheme(struct fmcd_softc *sc, int index, + t_Handle h_scheme, t_Handle *p_scheme); + +/* + * Mark PCD resources as transferred to CDX. After this call, + * fmcd_pcd_close() will not attempt to delete NCSW PCD objects. + */ +void fmcd_pcd_mark_transferred(struct fmcd_softc *sc); + +/* + * Resolve a handle ID to a kernel pointer (read-only, does not free). + * Used by CDX to translate userspace handle IDs to NCSW pointers. + */ +t_Handle fmcd_handle_resolve(struct fmcd_softc *sc, uint64_t id, + enum fmcd_handle_type expected_type); + +#endif /* _KERNEL */ + +#endif /* FMAN_CHARDEV_H_ */ diff --git a/sys/dev/dpaa/fman_fdt.c b/sys/dev/dpaa/fman_fdt.c index 82c4e80eb1e..7416e2bd3b3 100644 --- a/sys/dev/dpaa/fman_fdt.c +++ b/sys/dev/dpaa/fman_fdt.c @@ -31,6 +31,7 @@ #include #include +#include #include #include #include @@ -62,8 +63,11 @@ static device_method_t fman_methods[] = { DEFINE_CLASS_1(fman, fman_driver, fman_methods, sizeof(struct fman_softc), simplebus_driver); +/* Probe after GIC (BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE = 45) + * so interrupt allocation works on the first attempt. + * Also after clockgen (BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE) for clk_get_freq(). */ EARLY_DRIVER_MODULE(fman, simplebus, fman_driver, 0, 0, - BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE); + BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LAST); static int @@ -87,18 +91,29 @@ fman_get_clock(struct fman_softc *sc) device_t dev; phandle_t node; pcell_t fman_clock; + clk_t clk; + uint64_t freq; dev = sc->sc_base.dev; node = ofw_bus_get_node(dev); + /* Try static clock-frequency property first (PowerPC style) */ if ((OF_getprop(node, "clock-frequency", &fman_clock, - sizeof(fman_clock)) <= 0) || (fman_clock == 0)) { - device_printf(dev, "could not acquire correct frequency " - "from DTS\n"); + sizeof(fman_clock)) > 0) && (fman_clock != 0)) + return ((uint32_t)fman_clock); - return (0); + /* Fall back to clock framework (ARM64 style) */ + if (clk_get_by_ofw_index(dev, node, 0, &clk) == 0) { + if (clk_enable(clk) == 0 && + clk_get_freq(clk, &freq) == 0 && freq != 0) { + device_printf(dev, "FMan clock: %ju Hz\n", + (uintmax_t)freq); + return ((uint32_t)freq); + } + clk_release(clk); } - return ((uint32_t)fman_clock); + device_printf(dev, "could not acquire correct frequency from DTS\n"); + return (0); } diff --git a/sys/dev/dpaa/portals.h b/sys/dev/dpaa/portals.h index a2d6294f3d8..3e921f6ae1b 100644 --- a/sys/dev/dpaa/portals.h +++ b/sys/dev/dpaa/portals.h @@ -35,6 +35,8 @@ typedef struct dpaa_portal { vm_paddr_t dp_ci_pa; /* portal's CI area PA */ uint32_t dp_ce_size; /* portal's CE area size */ uint32_t dp_ci_size; /* portal's CI area size */ + vm_offset_t dp_ce_va; /* per-CPU CE virtual addr */ + vm_offset_t dp_ci_va; /* per-CPU CI virtual addr */ uintptr_t dp_intr_num; /* portal's intr. number */ } dpaa_portal_t; @@ -60,3 +62,6 @@ int qman_portals_detach(device_t); int dpaa_portal_alloc_res(device_t, struct dpaa_portals_devinfo *, int); void dpaa_portal_map_registers(struct dpaa_portals_softc *); +#ifdef __aarch64__ +void dpaa_portal_map_registers_cpu(struct dpaa_portals_softc *, int); +#endif diff --git a/sys/dev/dpaa/portals_common.c b/sys/dev/dpaa/portals_common.c index 5ef28570b48..58e63d0f405 100644 --- a/sys/dev/dpaa/portals_common.c +++ b/sys/dev/dpaa/portals_common.c @@ -39,7 +39,12 @@ #include #include +#ifdef __powerpc__ #include +#endif +#ifdef __aarch64__ +#include +#endif #include #include @@ -70,6 +75,11 @@ dpaa_portal_alloc_res(device_t dev, struct dpaa_portals_devinfo *di, int cpu) /* Cache enabled area */ rle = resource_list_find(res, SYS_RES_MEMORY, 0); sc->sc_rrid[0] = 0; + device_printf(dev, "CE alloc: pa=%#lx-%#lx size=%#lx dp_pa=%#lx\n", + (unsigned long)(rle->start + sc->sc_dp_pa), + (unsigned long)(rle->end + sc->sc_dp_pa), + (unsigned long)rle->count, + (unsigned long)sc->sc_dp_pa); sc->sc_rres[0] = bus_alloc_resource(dev, SYS_RES_MEMORY, &sc->sc_rrid[0], rle->start + sc->sc_dp_pa, rle->end + sc->sc_dp_pa, rle->count, RF_ACTIVE); @@ -78,11 +88,37 @@ dpaa_portal_alloc_res(device_t dev, struct dpaa_portals_devinfo *di, int cpu) "Could not allocate cache enabled memory.\n"); return (ENXIO); } + device_printf(dev, "CE mapped: va=%#lx\n", + (unsigned long)rman_get_bushandle(sc->sc_rres[0])); +#ifdef __powerpc__ tlb1_set_entry(rman_get_bushandle(sc->sc_rres[0]), rle->start + sc->sc_dp_pa, rle->count, _TLB_ENTRY_MEM); +#endif +#ifdef __aarch64__ + /* + * ARM64: The CE portal region must be Normal Non-Cacheable. + * Linux maps this as MEMREMAP_WC (pgprot_writecombine = + * Normal-NC) on ARM64. Stores go directly to the portal + * hardware without cache involvement. dc zva works on + * Normal-NC (zeros written as burst), dc cvac is a no-op. + */ + pmap_change_attr( + (vm_offset_t)rman_get_bushandle(sc->sc_rres[0]), + rle->count, VM_MEMATTR_UNCACHEABLE); + device_printf(dev, "CE pa_verify: va=%#lx -> pa=%#lx " + "(expected %#lx)\n", + (unsigned long)rman_get_bushandle(sc->sc_rres[0]), + (unsigned long)pmap_kextract( + (vm_offset_t)rman_get_bushandle(sc->sc_rres[0])), + (unsigned long)(rle->start + sc->sc_dp_pa)); +#endif /* Cache inhibited area */ rle = resource_list_find(res, SYS_RES_MEMORY, 1); sc->sc_rrid[1] = 1; + device_printf(dev, "CI alloc: pa=%#lx-%#lx size=%#lx\n", + (unsigned long)(rle->start + sc->sc_dp_pa), + (unsigned long)(rle->end + sc->sc_dp_pa), + (unsigned long)rle->count); sc->sc_rres[1] = bus_alloc_resource(dev, SYS_RES_MEMORY, &sc->sc_rrid[1], rle->start + sc->sc_dp_pa, rle->end + sc->sc_dp_pa, rle->count, RF_ACTIVE); @@ -93,8 +129,16 @@ dpaa_portal_alloc_res(device_t dev, struct dpaa_portals_devinfo *di, int cpu) sc->sc_rrid[0], sc->sc_rres[0]); return (ENXIO); } + device_printf(dev, "CI mapped: va=%#lx\n", + (unsigned long)rman_get_bushandle(sc->sc_rres[1])); +#ifdef __powerpc__ tlb1_set_entry(rman_get_bushandle(sc->sc_rres[1]), rle->start + sc->sc_dp_pa, rle->count, _TLB_ENTRY_IO); +#endif + sc->sc_dp[cpu].dp_ce_va = + rman_get_bushandle(sc->sc_rres[0]); + sc->sc_dp[cpu].dp_ci_va = + rman_get_bushandle(sc->sc_rres[1]); sc->sc_dp[cpu].dp_regs_mapped = 1; } /* Acquire portal's CE_PA and CI_PA */ @@ -153,15 +197,64 @@ dpaa_portal_map_registers(struct dpaa_portals_softc *sc) if (sc->sc_dp[cpu].dp_regs_mapped) goto out; +#ifdef __powerpc__ + /* PowerPC: remap the shared VA to this CPU's portal PA via TLB */ tlb1_set_entry(rman_get_bushandle(sc->sc_rres[0]), sc->sc_dp[cpu].dp_ce_pa, sc->sc_dp[cpu].dp_ce_size, _TLB_ENTRY_MEM); tlb1_set_entry(rman_get_bushandle(sc->sc_rres[1]), sc->sc_dp[cpu].dp_ci_pa, sc->sc_dp[cpu].dp_ci_size, _TLB_ENTRY_IO); + sc->sc_dp[cpu].dp_ce_va = rman_get_bushandle(sc->sc_rres[0]); + sc->sc_dp[cpu].dp_ci_va = rman_get_bushandle(sc->sc_rres[1]); +#endif +#ifdef __aarch64__ + /* + * ARM64: Each CPU needs its own VA→PA mapping because we can't + * remap a shared VA per-CPU like PowerPC does with TLB1 entries. + * Map each portal's CE as Normal Non-Cacheable, CI as Device. + */ + if (sc->sc_dp[cpu].dp_ce_pa == 0) { + printf("dpaa_portal_map_registers: cpu %u has no portal allocated\n", cpu); + goto out; + } + sc->sc_dp[cpu].dp_ce_va = (vm_offset_t)pmap_mapdev_attr( + sc->sc_dp[cpu].dp_ce_pa, sc->sc_dp[cpu].dp_ce_size, + VM_MEMATTR_UNCACHEABLE); + sc->sc_dp[cpu].dp_ci_va = (vm_offset_t)pmap_mapdev( + sc->sc_dp[cpu].dp_ci_pa, sc->sc_dp[cpu].dp_ci_size); +#endif sc->sc_dp[cpu].dp_regs_mapped = 1; out: sched_unpin(); } + +#ifdef __aarch64__ +/* + * Map portal CE/CI registers for a specific CPU. + * Unlike dpaa_portal_map_registers() which uses PCPU_GET(cpuid), + * this takes an explicit CPU parameter so it can be called from + * any CPU context during early boot (before smp_started=1). + * pmap_mapdev_attr/pmap_mapdev create global kernel VA mappings. + */ +void +dpaa_portal_map_registers_cpu(struct dpaa_portals_softc *sc, int cpu) +{ + + if (sc->sc_dp[cpu].dp_regs_mapped) + return; + if (sc->sc_dp[cpu].dp_ce_pa == 0) { + printf("dpaa_portal_map_registers_cpu: " + "cpu %d has no portal allocated\n", cpu); + return; + } + sc->sc_dp[cpu].dp_ce_va = (vm_offset_t)pmap_mapdev_attr( + sc->sc_dp[cpu].dp_ce_pa, sc->sc_dp[cpu].dp_ce_size, + VM_MEMATTR_UNCACHEABLE); + sc->sc_dp[cpu].dp_ci_va = (vm_offset_t)pmap_mapdev( + sc->sc_dp[cpu].dp_ci_pa, sc->sc_dp[cpu].dp_ci_size); + sc->sc_dp[cpu].dp_regs_mapped = 1; +} +#endif diff --git a/sys/dev/dpaa/qman.c b/sys/dev/dpaa/qman.c index e9159f25d74..c51511ee455 100644 --- a/sys/dev/dpaa/qman.c +++ b/sys/dev/dpaa/qman.c @@ -25,6 +25,8 @@ */ #include +#include "opt_dpaa.h" + #include #include #include @@ -37,16 +39,33 @@ #include #include #include +#include #include #include +#ifdef __powerpc__ #include +#endif +#ifdef __aarch64__ +#include +#include +#include +#include +#endif + +#include "bman.h" #include "qman.h" #include "portals.h" +#ifdef QMAN_CEETM_SUPPORT +#include "qman_ceetm.h" +#endif + +#define FQD_ENTRY_SIZE 64 /* matches NCSW qm.h */ + extern struct dpaa_portals_softc *qp_sc; -static struct qman_softc *qman_sc; +struct qman_softc *qman_sc; extern t_Handle qman_portal_setup(struct qman_softc *qsc); @@ -126,11 +145,28 @@ e_RxStoreResponse qman_received_frame_callback(t_Handle app, t_Handle qm_fqr, t_Handle qm_portal, uint32_t fqid_offset, t_DpaaFD *frame) { - struct qman_softc *sc; + static volatile int dflt_cb_count = 0; + t_Handle pool; + int n = ++dflt_cb_count; - sc = app; - - device_printf(sc->sc_dev, "dummy callback for received frame.\n"); + /* + * This callback fires for frames dequeued from FQs that have no + * registered callback (e.g., FQIDs created by FMC PCD schemes). + * When fqr==NULL, fqid_offset is the raw FQID, not an offset. + * + * Return the buffer to its BMan pool to prevent pool depletion. + */ + pool = bman_pool_for_bpid(frame->bpid); + if (pool != NULL) { + bman_put_buffer(pool, DPAA_FD_GET_ADDR(frame)); + } else if (n <= 50 || (n % 500) == 0) { + printf("qman: DEFAULT CB #%d: fqr=%p fqid=%u bpid=%u " + "status=0x%08x len=%u addr=%p [NO POOL]\n", + n, qm_fqr, fqid_offset, frame->bpid, + DPAA_FD_GET_STATUS(frame), + DPAA_FD_GET_LENGTH(frame), + DPAA_FD_GET_ADDR(frame)); + } return (e_RX_STORE_RESPONSE_CONTINUE); } @@ -144,14 +180,114 @@ qman_rejected_frame_callback(t_Handle app, t_Handle qm_fqr, t_Handle qm_portal, uint32_t fqid_offset, t_DpaaFD *frame, t_QmRejectedFrameInfo *qm_rejected_frame_info) { + static int ern_count; struct qman_softc *sc; sc = app; - device_printf(sc->sc_dev, "dummy callback for rejected frame.\n"); + if (ern_count++ < 20) + device_printf(sc->sc_dev, + "ERN: rejected frame fqid_off=%u rc=%d\n", + fqid_offset, + qm_rejected_frame_info ? + qm_rejected_frame_info->rejectionCode : -1); return (e_RX_STORE_RESPONSE_CONTINUE); } +/* + * Map a DT reserved-memory region for QBMan use. + * + * If the reserved-memory node has a 'reg' property (fixed physical address), + * the region is already excluded from phys_avail[] by early boot code + * (fdt_foreach_reserved_mem + no-map). We map it with pmap_mapdev_attr() + * as write-back cacheable — LS1046A CCI-400 provides HW coherency. + * + * Falls back to contigmalloc for nodes with only 'size' (no fixed address). + * + * Returns mapped VA, or NULL on failure. *out_size receives the size. + */ +#ifdef __aarch64__ +MALLOC_DECLARE(M_NETCOMMSW); + +void *qbman_alloc_reserved_mem(device_t dev, phandle_t mem_node, + uint32_t *out_size, const char *name); +void * +qbman_alloc_reserved_mem(device_t dev, phandle_t mem_node, + uint32_t *out_size, const char *name) +{ + pcell_t reg[4]; /* addr_hi, addr_lo, size_hi, size_lo */ + pcell_t cells[2]; + uint64_t pa, size, alignment; + void *va; + + /* + * Prefer fixed physical address from 'reg' property. + * DT reserved-memory with no-map + reg is excluded from phys_avail[] + * at early boot, so this memory is never touched by the VM system. + */ + if (OF_getencprop(mem_node, "reg", reg, sizeof(reg)) == + sizeof(reg)) { + pa = ((uint64_t)reg[0] << 32) | reg[1]; + size = ((uint64_t)reg[2] << 32) | reg[3]; + + if (size == 0 || size > 64 * 1024 * 1024) { + device_printf(dev, "%s: invalid reg size %#jx\n", + name, (uintmax_t)size); + return (NULL); + } + + va = pmap_mapdev_attr(pa, size, VM_MEMATTR_WRITE_BACK); + if (va == NULL) { + device_printf(dev, + "%s: pmap_mapdev_attr(%#jx, %#jx) failed\n", + name, (uintmax_t)pa, (uintmax_t)size); + return (NULL); + } + + memset(va, 0, size); + + *out_size = (uint32_t)size; + device_printf(dev, + "%s: %uMB at pa=%#jx va=%p (no-map reserved)\n", + name, (unsigned)(size >> 20), (uintmax_t)pa, va); + return (va); + } + + /* + * Fallback: dynamic allocation from 'size' property. + */ + if (OF_getencprop(mem_node, "size", cells, sizeof(cells)) <= 0) { + device_printf(dev, "%s: no 'reg' or 'size' property\n", name); + return (NULL); + } + size = ((uint64_t)cells[0] << 32) | cells[1]; + + if (OF_getencprop(mem_node, "alignment", cells, sizeof(cells)) <= 0) + alignment = size; + else + alignment = ((uint64_t)cells[0] << 32) | cells[1]; + + if (size == 0 || size > 64 * 1024 * 1024) { + device_printf(dev, "%s: invalid size %#jx\n", + name, (uintmax_t)size); + return (NULL); + } + + va = contigmalloc(size, M_NETCOMMSW, M_WAITOK | M_ZERO, + 0, 0xFFFFFFFFULL, alignment, 0); + if (va == NULL) { + device_printf(dev, "%s: contigmalloc(%#jx, align=%#jx) failed\n", + name, (uintmax_t)size, (uintmax_t)alignment); + return (NULL); + } + + *out_size = (uint32_t)size; + device_printf(dev, "%s: %uMB at %p (contigmalloc, align=%#jx)\n", + name, (unsigned)(size >> 20), va, (uintmax_t)alignment); + return (va); +} +#endif + int qman_attach(device_t dev) { @@ -191,9 +327,12 @@ qman_attach(device_t dev) if (qp_sc == NULL) goto err; +#ifndef __aarch64__ dpaa_portal_map_registers(qp_sc); +#endif /* Initialize QMan */ + memset(&qp, 0, sizeof(qp)); qp.guestId = NCSW_MASTER_ID; qp.baseAddress = rman_get_bushandle(sc->sc_rres); qp.swPortalsBaseAddress = rman_get_bushandle(qp_sc->sc_rres[0]); @@ -209,6 +348,48 @@ qman_attach(device_t dev) qp.partCgsBase = 0; qp.partNumOfCgs = 0; +#ifdef __aarch64__ + /* + * Parse DT memory-region phandles to allocate FQD and PFDR memory + * from reserved-memory nodes, matching Linux's qbman_init_private_mem(). + * DT: memory-region = <&qman_fqd &qman_pfdr>; + * qman-fqd: size=0x800000 (8MB) → 131072 FQIDs + * qman-pfdr: size=0x2000000 (32MB) → 524288 PFDRs + */ + { + phandle_t node, mem_node; + pcell_t mem_handles[2]; + + node = ofw_bus_get_node(dev); + if (node > 0 && OF_getencprop(node, "memory-region", + (void *)mem_handles, sizeof(mem_handles)) == + sizeof(mem_handles)) { + /* Index 0 = FQD, Index 1 = PFDR */ + mem_node = OF_node_from_xref(mem_handles[0]); + if (mem_node > 0) { + qp.p_FqdBase = qbman_alloc_reserved_mem(dev, + mem_node, &qp.fqdSize, "FQD"); + if (qp.p_FqdBase != NULL) { + qp.totalNumOfFqids = + qp.fqdSize / FQD_ENTRY_SIZE; + qp.partNumOfFqids = + qp.totalNumOfFqids; + } + } + /* Index 1 = PFDR */ + mem_node = OF_node_from_xref(mem_handles[1]); + if (mem_node > 0) { + qp.p_PfdrBase = + qbman_alloc_reserved_mem(dev, + mem_node, &qp.pfdrSize, "PFDR"); + } + } else { + device_printf(dev, + "no memory-region property, using defaults\n"); + } + } +#endif + sc->sc_qh = QM_Config(&qp); if (sc->sc_qh == NULL) { device_printf(dev, "could not be configured\n"); @@ -230,9 +411,34 @@ qman_attach(device_t dev) device_printf(dev, "Hardware version: %d.%d.\n", rev.majorRev, rev.minorRev); +#ifndef __aarch64__ + qman_portal_setup(sc); +#endif + sched_unpin(); - qman_portal_setup(sc); +#ifdef __aarch64__ + /* + * Initialize portals for all CPUs from boot CPU. + * ARM64: pmap_mapdev creates global kernel VA, no sched_bind needed. + * SDEST configured per-portal via QM_PORTAL_ConfigStash. + * All portals use NAPI-style interrupt handling (FILTER + taskqueue). + */ + for (int cpu = 0; cpu < mp_ncpus; cpu++) { + if (qman_portal_init_cpu(sc, cpu) == NULL) + device_printf(dev, + "could not setup QMan portal on CPU %d\n", cpu); + else + device_printf(dev, + "QMan portal %d initialized (SDEST=%d)\n", + cpu, cpu / 2); + } + +#ifdef QMAN_CEETM_SUPPORT + /* Initialize CEETM if DT node is present */ + qman_ceetm_init(); +#endif +#endif return (0); @@ -360,7 +566,73 @@ qman_fqr_create(uint32_t fqids_num, e_QmFQChannel channel, uint8_t wq, goto err; } - sc->sc_fqr_cpu[QM_FQR_GetFqid(fqrh)] = PCPU_GET(cpuid); + if (QM_FQR_GetFqid(fqrh) < QMAN_MAX_FQIDS) + sc->sc_fqr_cpu[QM_FQR_GetFqid(fqrh)] = PCPU_GET(cpuid); + + sched_unpin(); + + return (fqrh); + +err: + sched_unpin(); + + return (NULL); +} + +t_Handle +qman_fqr_create_ctx(uint32_t fqids_num, e_QmFQChannel channel, uint8_t wq, + bool force_fqid, uint32_t fqid_or_align, bool prefer_in_cache, + t_QmContextA *p_context_a, t_QmContextB *p_context_b) +{ + struct qman_softc *sc; + t_QmFqrParams fqr; + t_Handle fqrh, portal; + + sc = qman_sc; + + sched_pin(); + + portal = qman_portal_setup(sc); + if (portal == NULL) { + device_printf(sc->sc_dev, "could not setup QMan portal\n"); + goto err; + } + + fqr.h_Qm = sc->sc_qh; + fqr.h_QmPortal = portal; + fqr.initParked = FALSE; + fqr.holdActive = FALSE; + fqr.preferInCache = prefer_in_cache; + + fqr.useContextAForStash = FALSE; + fqr.p_ContextA = p_context_a; + fqr.p_ContextB = p_context_b; + + fqr.channel = channel; + fqr.wq = wq; + fqr.shadowMode = FALSE; + fqr.numOfFqids = fqids_num; + + fqr.useForce = force_fqid; + if (force_fqid) + fqr.qs.frcQ.fqid = fqid_or_align; + else + fqr.qs.nonFrcQs.align = fqid_or_align; + + fqr.congestionAvoidanceEnable = FALSE; + fqr.congestionAvoidanceParams.h_QmCg = 0; + fqr.congestionAvoidanceParams.overheadAccountingLength = 0; + fqr.congestionAvoidanceParams.fqTailDropThreshold = 0; + + fqrh = QM_FQR_Create(&fqr); + if (fqrh == NULL) { + device_printf(sc->sc_dev, + "could not create Frame Queue Range (ctx)\n"); + goto err; + } + + if (QM_FQR_GetFqid(fqrh) < QMAN_MAX_FQIDS) + sc->sc_fqr_cpu[QM_FQR_GetFqid(fqrh)] = PCPU_GET(cpuid); sched_unpin(); @@ -379,9 +651,11 @@ qman_fqr_free(t_Handle fqr) t_Error error; sc = qman_sc; - thread_lock(curthread); - sched_bind(curthread, sc->sc_fqr_cpu[QM_FQR_GetFqid(fqr)]); - thread_unlock(curthread); + if (QM_FQR_GetFqid(fqr) < QMAN_MAX_FQIDS) { + thread_lock(curthread); + sched_bind(curthread, sc->sc_fqr_cpu[QM_FQR_GetFqid(fqr)]); + thread_unlock(curthread); + } error = QM_FQR_Free(fqr); @@ -443,6 +717,30 @@ qman_fqr_enqueue(t_Handle fqr, uint32_t fqid_off, t_DpaaFD *frame) return (error); } +t_Error +qman_enqueue_fqid(uint32_t fqid, t_DpaaFD *frame) +{ + struct qman_softc *sc; + t_Error error; + t_Handle portal; + + sc = qman_sc; + sched_pin(); + + portal = qman_portal_setup(sc); + if (portal == NULL) { + device_printf(sc->sc_dev, "could not setup QMan portal\n"); + sched_unpin(); + return (E_NOT_SUPPORTED); + } + + error = QM_PORTAL_EnqueueFqid(portal, fqid, frame); + + sched_unpin(); + + return (error); +} + uint32_t qman_fqr_get_counter(t_Handle fqr, uint32_t fqid_off, e_QmFqrCounters counter) @@ -545,7 +843,64 @@ qman_poll(e_QmPortalPollSource source) } /* - * TODO: add polling and/or congestion support. + * QMan diagnostic counters — expose QM_GetCounter to dtsec driver. */ +uint32_t +qman_get_pfdr_in_use(void) +{ + struct qman_softc *sc; + + sc = qman_sc; + if (sc == NULL || sc->sc_qh == NULL) + return (0); + + return (QM_GetCounter(sc->sc_qh, e_QM_COUNTERS_PFDR_IN_USE)); +} + +uint32_t +qman_get_sfdr_in_use(void) +{ + struct qman_softc *sc; + + sc = qman_sc; + if (sc == NULL || sc->sc_qh == NULL) + return (0); + + return (QM_GetCounter(sc->sc_qh, e_QM_COUNTERS_SFDR_IN_USE)); +} + +#ifdef __aarch64__ +void +qman_ccsr_diag(void) +{ + struct qman_softc *sc; + uint32_t err_isr, ecsr, ecir, pfdr_fpc, sfdr_in_use; + uint32_t idle_stat, dcp0_cfg, dcp0_dlm_avg; + uint32_t dcp_dd_ihrsr, dcp_dd_hasr; + + sc = qman_sc; + if (sc == NULL || sc->sc_qh == NULL) + return; + + QM_ErrorDiag(sc->sc_qh, &err_isr, &ecsr, &ecir, &pfdr_fpc, + &sfdr_in_use, &idle_stat, &dcp0_cfg, &dcp0_dlm_avg, + &dcp_dd_ihrsr, &dcp_dd_hasr); + + printf("qman: CCSR err_isr=0x%08x ecsr=0x%08x ecir=0x%08x\n", + err_isr, ecsr, ecir); + if (ecir) { + printf("qman: ecir: %s portal %u fqid %u\n", + (ecir & 0x20000000) ? "DCP" : "SWP", + (ecir >> 24) & 0x1f, + ecir & 0x00ffffff); + } + printf("qman: pfdr_free=%u sfdr_in_use=%u idle=0x%08x\n", + pfdr_fpc, sfdr_in_use, idle_stat); + printf("qman: dcp0: cfg=0x%08x dlm_avg=0x%08x\n", + dcp0_cfg, dcp0_dlm_avg); + printf("qman: dcp_dd: ihrsr=0x%08x hasr=0x%08x\n", + dcp_dd_ihrsr, dcp_dd_hasr); +} +#endif /** @} */ diff --git a/sys/dev/dpaa/qman.h b/sys/dev/dpaa/qman.h index 815ef1f6d33..ebf2124996a 100644 --- a/sys/dev/dpaa/qman.h +++ b/sys/dev/dpaa/qman.h @@ -39,7 +39,7 @@ /** * Maximum number of frame queues in all QMans. */ -#define QMAN_MAX_FQIDS 16 +#define QMAN_MAX_FQIDS 4096 /** * Pool channel common to all software portals. @@ -85,6 +85,8 @@ struct qman_softc { * @group QMan bus interface * @{ */ +extern struct qman_softc *qman_sc; + int qman_attach(device_t dev); int qman_detach(device_t dev); int qman_suspend(device_t dev); @@ -185,6 +187,16 @@ t_Error qman_fqr_register_cb(t_Handle fqr, t_QmReceivedFrameCallback *callback, */ t_Error qman_fqr_enqueue(t_Handle fqr, uint32_t fqid_off, t_DpaaFD *frame); +/** + * Enqueue a frame to a specific FQID (bypassing FQR). + * Used for CEETM frame queues managed by CEETM hardware. + * + * @param fqid Target Frame Queue ID. + * @param frame A frame to be enqueued. + * @return E_OK on success; error code otherwise. + */ +t_Error qman_enqueue_fqid(uint32_t fqid, t_DpaaFD *frame); + /** * Get one of the FQR counter's value. * @@ -239,6 +251,39 @@ e_RxStoreResponse qman_rejected_frame_callback(t_Handle app, t_Handle qm_fqr, t_Handle qm_portal, uint32_t fqid_offset, t_DpaaFD *frame, t_QmRejectedFrameInfo *qm_rejected_frame_info); +/** + * Create Frame Queue Range with explicit contextA/B. + * + * Same as qman_fqr_create() but allows setting raw FQD contextA and + * contextB fields. Used by CDX to create forwarding TX FQs with + * OVFQ/EBD bits set for hardware flow offload. + * + * @param fqids_num Number of frame queues in the range. + * @param channel Dedicated channel (e.g. FMan TX port). + * @param wq Work Queue Number within the channel. + * @param force_fqid If TRUE, force allocation of specific FQID. + * @param fqid_or_align FQID if force_fqid, alignment otherwise. + * @param prefer_in_cache If TRUE, prefer QMan internal cache. + * @param p_context_a Raw contextA (8 bytes), or NULL. + * @param p_context_b Raw contextB (4 bytes), or NULL. + * @return NCSW FQR handle, or NULL on failure. + */ +t_Handle qman_fqr_create_ctx(uint32_t fqids_num, e_QmFQChannel channel, + uint8_t wq, bool force_fqid, uint32_t fqid_or_align, + bool prefer_in_cache, t_QmContextA *p_context_a, + t_QmContextB *p_context_b); + +uint32_t qman_get_pfdr_in_use(void); +uint32_t qman_get_sfdr_in_use(void); + +#ifdef __aarch64__ +t_Handle qman_portal_init_cpu(struct qman_softc *, int); +void qman_portal_dqrr_diag(void); +void qman_ccsr_diag(void); +struct mbuf; +void qman_rx_defer(struct mbuf *); +#endif + /** @} */ #endif /* QMAN_H */ diff --git a/sys/dev/dpaa/qman_fdt.c b/sys/dev/dpaa/qman_fdt.c index 15b8d16f59b..35d31a233b1 100644 --- a/sys/dev/dpaa/qman_fdt.c +++ b/sys/dev/dpaa/qman_fdt.c @@ -103,7 +103,10 @@ static driver_t qm_portals_driver = { }; EARLY_DRIVER_MODULE(qman_portals, ofwbus, qm_portals_driver, 0, 0, - BUS_PASS_BUS); + BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LAST); +/* On ARM64, portal bus nodes are children of soc (simplebus), not ofwbus */ +EARLY_DRIVER_MODULE(qman_portals, simplebus, qm_portals_driver, 0, 0, + BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LAST); static void get_addr_props(phandle_t node, uint32_t *addrp, uint32_t *sizep) @@ -147,7 +150,7 @@ qman_portal_find_cpu(int cpu) return (-1); for (node = OF_child(node); node != 0; node = OF_peer(node)) { - if (OF_getprop(node, "reg", ®, sizeof(reg)) <= 0) + if (OF_getencprop(node, "reg", ®, sizeof(reg)) <= 0) continue; if (reg == cpu) return (node); @@ -212,14 +215,16 @@ qman_portals_fdt_attach(device_t dev) continue; } /* Checkout related cpu */ - if (OF_getprop(child, "cpu-handle", (void *)&cpu, + if (OF_getencprop(child, "cpu-handle", (void *)&cpu, sizeof(cpu)) <= 0) { - cpu = qman_portal_find_cpu(cpus); - if (cpu <= 0) + /* No cpu-handle (ARM64): find CPU node directly */ + cpu_node = qman_portal_find_cpu(cpus); + if (cpu_node <= 0) continue; + } else { + /* cpu-handle is an ihandle (PowerPC) */ + cpu_node = OF_instance_to_package(cpu); } - /* Acquire cpu number */ - cpu_node = OF_instance_to_package(cpu); if (OF_getencprop(cpu_node, "reg", &cpu_num, sizeof(cpu_num)) <= 0) { device_printf(dev, "Could not retrieve CPU number.\n"); return (ENXIO); diff --git a/sys/dev/dpaa/qman_portals.c b/sys/dev/dpaa/qman_portals.c index 76d89784ab5..625471026ff 100644 --- a/sys/dev/dpaa/qman_portals.c +++ b/sys/dev/dpaa/qman_portals.c @@ -36,15 +36,19 @@ #include #include #include +#include #include +#include +#include +#ifdef __powerpc__ #include +#include +#endif #include #include -#include - #include "qman.h" #include "portals.h" @@ -60,15 +64,16 @@ struct dpaa_portals_softc *qp_sc; int qman_portals_attach(device_t dev) { - struct dpaa_portals_softc *sc; + qp_sc = device_get_softc(dev); - sc = qp_sc = device_get_softc(dev); - - /* Map bman portal to physical address space */ +#ifdef __powerpc__ + struct dpaa_portals_softc *sc = qp_sc; + /* Map qman portal to physical address space (PowerPC LAW) */ if (law_enable(OCP85XX_TGTIF_QMAN, sc->sc_dp_pa, sc->sc_dp_size)) { qman_portals_detach(dev); return (ENXIO); } +#endif /* Set portal properties for XX_VirtToPhys() */ XX_PortalSetInfo(dev); @@ -128,6 +133,19 @@ qman_portal_setup(struct qman_softc *qsc) sc = qp_sc; +#ifdef __aarch64__ + /* + * On ARM64, all portals are pre-initialized with NAPI-style + * interrupt handling via qman_portal_init_cpu() in qman_attach(). + * Just return the portal for the current CPU. + */ + sched_pin(); + cpu = PCPU_GET(cpuid); + portal = sc->sc_dp[cpu].dp_ph; + sched_unpin(); + return (portal); +#endif + sched_pin(); portal = NULL; cpu = PCPU_GET(cpuid); @@ -150,9 +168,14 @@ qman_portal_setup(struct qman_softc *qsc) /* Map portal registers */ dpaa_portal_map_registers(sc); + + /* Bail if portal mapping failed (e.g. no portal allocated for this CPU) */ + if (sc->sc_dp[cpu].dp_ce_va == 0 || sc->sc_dp[cpu].dp_ci_va == 0) + goto err; + /* Configure and initialize portal */ - qpp.ceBaseAddress = rman_get_bushandle(sc->sc_rres[0]); - qpp.ciBaseAddress = rman_get_bushandle(sc->sc_rres[1]); + qpp.ceBaseAddress = sc->sc_dp[cpu].dp_ce_va; + qpp.ciBaseAddress = sc->sc_dp[cpu].dp_ci_va; qpp.h_Qm = qsc->sc_qh; qpp.swPortalId = cpu; qpp.irq = (uintptr_t)sc->sc_dp[cpu].dp_ires; @@ -186,3 +209,220 @@ err: return (NULL); } + +#ifdef __aarch64__ + +#include +#include +#include +#include +#include +#include + +/* Per-portal NAPI state */ +static struct { + struct task task; + struct taskqueue *tq; + struct mbufq rxq; /* deferred RX mbufs */ +} qman_napi[MAXCPU]; + +/* Declared in xx_arm64.c */ +extern void XX_ConfigPortalNapi(uintptr_t irq, struct taskqueue *tq, + struct task *task); +extern void XX_SetPortalNapiHandle(uintptr_t irq, t_Handle portal); + +/* + * Enqueue an mbuf for deferred delivery after QM_PORTAL_Poll returns. + * Called from the DQRR RX callback (inside NCSW_PLOCK) to avoid + * re-entering the portal via if_input -> TCP -> TX -> QM_FQR_Enqueue. + */ +void +qman_rx_defer(struct mbuf *m) +{ + int cpu = PCPU_GET(cpuid); + + if (mbufq_enqueue(&qman_napi[cpu].rxq, m) != 0) { + static volatile uint32_t defer_drop_cnt; + uint32_t n = atomic_fetchadd_32(&defer_drop_cnt, 1) + 1; + if (n <= 5) + printf("qman: rx_defer queue full on CPU %d (#%u)," + " dropping\n", cpu, n); + m_freem(m); + } +} + +/* + * NAPI-style poll task for QMan portals (FreeBSD equivalent of Linux + * dpaa_eth_poll). Runs in a per-CPU taskqueue thread at PI_NET, + * scheduled by the FILTER interrupt handler via XX_PortalFilter. + * + * Drains the entire DQRR (max 16 entries) and MR in one shot via + * QM_PORTAL_Poll, then delivers deferred RX mbufs outside NCSW_PLOCK + * to prevent nested portal enqueue from the TX path. + */ +static void +qman_portal_poll_task(void *arg, int pending) +{ + t_Handle portal = arg; + struct mbuf *m; + int cpu = PCPU_GET(cpuid); + + QM_PORTAL_Poll(portal, e_QM_PORTAL_POLL_SOURCE_BOTH); + QM_PORTAL_Uninhibit(portal); + + /* Deliver deferred RX mbufs outside NCSW_PLOCK */ + while ((m = mbufq_dequeue(&qman_napi[cpu].rxq)) != NULL) + if_input(m->m_pkthdr.rcvif, m); +} + +/* + * Initialize a QMan portal for a specific CPU with NAPI-style + * interrupt handling. Like Linux's qman_create_affine_portal + + * dpaa_eth_add_channel: + * + * 1. Create a per-CPU taskqueue pinned to the portal's CPU + * 2. Configure the portal interrupt for FILTER mode (via + * XX_ConfigPortalNapi) — the FILTER handler inhibits the + * portal and enqueues the taskqueue task + * 3. Initialize the NCSW portal (Config + Stash + Init) + * 4. Subscribe to the pool channel so QMan distributes RX + * frames across all portals (round-robin) + * + * On ARM64, pmap_mapdev creates global kernel VA, so no + * sched_bind/migration is needed. + */ +t_Handle +qman_portal_init_cpu(struct qman_softc *qsc, int cpu) +{ + struct dpaa_portals_softc *sc; + t_QmPortalParam qpp; + t_QmPortalStashParam stash; + t_Handle portal; + cpuset_t cpumask; + + if (qp_sc == NULL) + return (NULL); + sc = qp_sc; + + /* Skip if already initialized */ + if (sc->sc_dp[cpu].dp_ph != NULL) + return (sc->sc_dp[cpu].dp_ph); + + /* Map CE/CI registers (global kernel VA, accessible from any CPU) */ + dpaa_portal_map_registers_cpu(sc, cpu); + + if (sc->sc_dp[cpu].dp_ce_va == 0 || sc->sc_dp[cpu].dp_ci_va == 0) + return (NULL); + + if (sc->sc_dp[cpu].dp_ires == NULL) { + printf("qman: portal %d has no IRQ resource\n", cpu); + return (NULL); + } + + /* Init deferred RX mbuf queue (unlimited — DQRR ring is the natural cap) */ + mbufq_init(&qman_napi[cpu].rxq, 0); + + /* + * Create per-CPU taskqueue for NAPI-style polling. + * The task arg is set to the portal handle after Init. + */ + CPU_ZERO(&cpumask); + CPU_SET(cpu, &cpumask); + NET_TASK_INIT(&qman_napi[cpu].task, 0, qman_portal_poll_task, NULL); + qman_napi[cpu].tq = taskqueue_create_fast("qman_poll", M_WAITOK, + taskqueue_thread_enqueue, &qman_napi[cpu].tq); + taskqueue_start_threads_cpuset(&qman_napi[cpu].tq, 1, PI_NET, + &cpumask, "qman%d", cpu); + + /* + * Configure NAPI mode before QM_PORTAL_Init — Init calls + * XX_SetIntr internally, which checks for NAPI mode and + * registers a FILTER handler instead of an ithread handler. + */ + XX_ConfigPortalNapi((uintptr_t)sc->sc_dp[cpu].dp_ires, + qman_napi[cpu].tq, &qman_napi[cpu].task); + + /* Configure portal */ + memset(&qpp, 0, sizeof(qpp)); + qpp.ceBaseAddress = sc->sc_dp[cpu].dp_ce_va; + qpp.ciBaseAddress = sc->sc_dp[cpu].dp_ci_va; + qpp.h_Qm = qsc->sc_qh; + qpp.swPortalId = cpu; + qpp.irq = (uintptr_t)sc->sc_dp[cpu].dp_ires; + qpp.fdLiodnOffset = 0; + qpp.f_DfltFrame = qman_received_frame_callback; + qpp.f_RejectedFrame = qman_rejected_frame_callback; + qpp.h_App = qsc; + + portal = QM_PORTAL_Config(&qpp); + if (portal == NULL) + return (NULL); + + /* + * Configure stash destination (SDEST) for this CPU. + * Linux: qman_set_sdest(channel, cpu_idx / 2) for QMan v3+. + * Each pair of cores shares one Stash Request Queue (SRQ) in + * the CoreNet fabric. LS1046A: SRQ0=CPU0-1, SRQ1=CPU2-3. + */ + memset(&stash, 0, sizeof(stash)); + stash.stashDestQueue = cpu / 2; + QM_PORTAL_ConfigStash(portal, &stash); + + if (QM_PORTAL_Init(portal) != E_OK) { + QM_PORTAL_Free(portal); + return (NULL); + } + + /* + * Set portal handle for the FILTER handler and task. + * The FILTER handler uses this to call QM_PORTAL_Inhibit. + * The task function receives it as the arg parameter. + */ + XX_SetPortalNapiHandle((uintptr_t)sc->sc_dp[cpu].dp_ires, portal); + qman_napi[cpu].task.ta_context = portal; + + /* + * Subscribe to pool channel — all portals get RX frames. + * QMan hardware distributes frames round-robin across all + * portals subscribed to the pool channel, matching Linux's + * dpaa_eth_add_channel() which calls qman_p_static_dequeue_add + * for every CPU's portal. + * + * RX FQs are on per-CPU dedicated channels (RSS), so pool + * channel round-robin no longer causes TCP reordering. Pool + * channel is still needed for TX confirm, CDX dist FQs, and + * CAAM QI response FQs. + */ + QM_PORTAL_AddPoolChannel(portal, QMAN_COMMON_POOL_CHANNEL); + + sc->sc_dp[cpu].dp_ph = portal; + return (portal); +} + +void +qman_portal_dqrr_diag(void) +{ + struct dpaa_portals_softc *sc; + t_Handle portal; + uint8_t sw_pi, sw_ci, sw_fill, hw_pi, hw_ci; + uint32_t isr, ier, iir; + int cpu; + + if (qp_sc == NULL) + return; + sc = qp_sc; + + for (cpu = 0; cpu < mp_ncpus; cpu++) { + portal = sc->sc_dp[cpu].dp_ph; + if (portal == NULL) + continue; + QM_PORTAL_DqrrDiag(portal, &sw_pi, &sw_ci, &sw_fill, + &hw_pi, &hw_ci); + QM_PORTAL_IsrDiag(portal, &isr, &ier, &iir); + printf("qman: portal%d: dqrr sw_pi=%u sw_ci=%u fill=%u " + "hw_pi=%u hw_ci=%u isr=0x%05x ier=0x%05x iir=%u\n", + cpu, sw_pi, sw_ci, sw_fill, hw_pi, hw_ci, + isr, ier, iir); + } +} +#endif