dpaa: add SFP module detection and link management for 10GbE ports

Wire SFP infrastructure into dtsec so 10GbE ports detect module
insertion/removal via mod-def0 GPIO polling, read the SFP EEPROM
for identification, control TX enable/disable, and report accurate
link status via LOS GPIO instead of hardcoding link as always up.

Module presence is polled at 1Hz in the existing tick callout.
EEPROM reads are deferred to taskqueue_thread since I2C can sleep.
Insert task re-checks module presence under DTSEC_LOCK to handle
races with hot-removal during EEPROM read.

Adds dev.dtsec.N.sfp_info sysctl for module vendor and part number.

No DTS changes needed — sfp phandle, GPIOs, and i2c-bus are already
defined in mono-gateway-dk.dts.
This commit is contained in:
Tomaz Zaman 2026-03-15 13:27:36 +01:00 • committed by Maurice Walker
parent 5065099c0d
commit 43eb531be0
3 changed files with 327 additions and 4 deletions

View file

@ -52,6 +52,8 @@
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>
#include <dev/ofw/openfirm.h>
#include <dev/iicbus/iic.h>
#include <dev/iicbus/iiconf.h>
#include "miibus_if.h"
@ -79,6 +81,8 @@
#define DTSEC_MIN_FRAME_SIZE 64
#define DTSEC_MAX_FRAME_SIZE 9600
static void dtsec_sfp_poll(struct dtsec_softc *sc);
#if (DPAA_VERSION < 11)
#define DTSEC_REG_MAXFRM 0x110
#define DTSEC_REG_GADDR(i) (0x0a0 + 4*(i))
@ -569,6 +573,8 @@ dtsec_if_tick(void *arg)
if (sc->sc_mii != NULL)
mii_tick(sc->sc_mii);
dtsec_sfp_poll(sc);
callout_reset(&sc->sc_tick_callout, hz, dtsec_if_tick, sc);
DTSEC_UNLOCK(sc);
@ -582,6 +588,7 @@ dtsec_if_deinit_locked(struct dtsec_softc *sc)
DTSEC_UNLOCK(sc);
callout_drain(&sc->sc_tick_callout);
taskqueue_drain(taskqueue_thread, &sc->sc_sfp_task);
DTSEC_LOCK(sc);
}
@ -658,6 +665,210 @@ dtsec_if_watchdog(if_t ifp)
/** @} */
/**
* @group SFP module management.
* @{
*/
/* SFF-8024 connector types */
#define SFP_CONNECTOR_LC 0x07
#define SFP_CONNECTOR_RJ45 0x22 /* copper RJ45 */
/* SFF-8472 EEPROM A0h field offsets */
#define SFP_ID_OFFSET 0
#define SFP_CONNECTOR_OFFSET 2
#define SFP_VENDOR_OFFSET 20
#define SFP_VENDOR_LEN 16
#define SFP_PARTNUM_OFFSET 40
#define SFP_PARTNUM_LEN 16
/* I2C address for SFP EEPROM (A0h page, 7-bit) */
#define SFP_I2C_ADDR 0x50
static int
dtsec_sfp_read_eeprom(struct dtsec_softc *sc)
{
struct iic_msg msgs[2];
uint8_t offset;
int error;
if (sc->sc_sfp_i2c == NULL)
return (ENXIO);
offset = 0;
/* Write offset byte */
msgs[0].slave = SFP_I2C_ADDR << 1;
msgs[0].flags = IIC_M_WR;
msgs[0].len = 1;
msgs[0].buf = &offset;
/* Read 64 bytes of base ID */
msgs[1].slave = SFP_I2C_ADDR << 1;
msgs[1].flags = IIC_M_RD;
msgs[1].len = sizeof(sc->sc_sfp_id);
msgs[1].buf = sc->sc_sfp_id;
/* sc_sfp_i2c is the iicbus device (from SFF_GET_I2C_BUS),
* not a slave child, so use the bus-level API directly.
* iicbus_request_bus(bus, owner, how) acquires exclusive access,
* iicbus_transfer(bus, ...) routes to the controller. */
error = iicbus_request_bus(sc->sc_sfp_i2c, sc->sc_dev,
IIC_INTRWAIT);
if (error != 0) {
device_printf(sc->sc_dev,
"SFP: failed to acquire I2C bus: %d\n", error);
return (error);
}
error = iicbus_transfer(sc->sc_sfp_i2c, msgs, 2);
iicbus_release_bus(sc->sc_sfp_i2c, sc->sc_dev);
if (error != 0) {
device_printf(sc->sc_dev, "SFP EEPROM read failed: %d\n",
error);
memset(sc->sc_sfp_id, 0, sizeof(sc->sc_sfp_id));
}
return (error);
}
static void
dtsec_sfp_trim(char *dst, const uint8_t *src, int len)
{
int i;
memcpy(dst, src, len);
dst[len] = '\0';
/* Trim trailing spaces */
for (i = len - 1; i >= 0 && dst[i] == ' '; i--)
dst[i] = '\0';
}
static void
dtsec_sfp_log_module(struct dtsec_softc *sc)
{
char vendor[SFP_VENDOR_LEN + 1];
char partnum[SFP_PARTNUM_LEN + 1];
uint8_t connector;
dtsec_sfp_trim(vendor, &sc->sc_sfp_id[SFP_VENDOR_OFFSET],
SFP_VENDOR_LEN);
dtsec_sfp_trim(partnum, &sc->sc_sfp_id[SFP_PARTNUM_OFFSET],
SFP_PARTNUM_LEN);
connector = sc->sc_sfp_id[SFP_CONNECTOR_OFFSET];
device_printf(sc->sc_dev,
"SFP+ module inserted: %s %s (connector 0x%02x%s)\n",
vendor, partnum, connector,
connector == SFP_CONNECTOR_RJ45 ? " RJ45" : "");
}
static void
dtsec_sfp_insert_task(void *arg, int pending)
{
struct dtsec_softc *sc = arg;
bool present, los;
/* Read EEPROM (sleeps — runs in taskqueue thread context) */
dtsec_sfp_read_eeprom(sc);
/*
* Take the lock to commit state atomically. Re-check module
* presence under lock — the poll callout may have already
* processed a removal (modstate reset to 0) while we were
* reading the EEPROM. Only proceed if modstate is still -1
* (probing, set by the poll that enqueued us).
*/
DTSEC_LOCK(sc);
if (sc->sc_sfp_modstate != -1) {
/* Module was removed while we were reading EEPROM */
DTSEC_UNLOCK(sc);
return;
}
/* Verify module is still physically present */
gpio_pin_is_active(sc->sc_sfp_moddef0, &present);
if (!present) {
sc->sc_sfp_modstate = 0;
memset(sc->sc_sfp_id, 0, sizeof(sc->sc_sfp_id));
DTSEC_UNLOCK(sc);
return;
}
/* Log module info */
if (sc->sc_sfp_id[SFP_ID_OFFSET] != 0)
dtsec_sfp_log_module(sc);
/* De-assert TX disable */
if (sc->sc_sfp_txdis != NULL)
gpio_pin_set_active(sc->sc_sfp_txdis, false);
sc->sc_sfp_modstate = 1;
/* Check initial LOS state */
if (sc->sc_sfp_los != NULL) {
gpio_pin_is_active(sc->sc_sfp_los, &los);
sc->sc_sfp_los_prev = los;
if (!los)
if_link_state_change(sc->sc_ifnet, LINK_STATE_UP);
} else {
/* No LOS GPIO — assume link up when module present */
sc->sc_sfp_los_prev = false;
if_link_state_change(sc->sc_ifnet, LINK_STATE_UP);
}
DTSEC_UNLOCK(sc);
}
static void
dtsec_sfp_poll(struct dtsec_softc *sc)
{
bool present, los;
DTSEC_LOCK_ASSERT(sc);
if (sc->sc_sfp_moddef0 == NULL)
return;
gpio_pin_is_active(sc->sc_sfp_moddef0, &present);
if (present && sc->sc_sfp_modstate == 0) {
/* Module just inserted — defer EEPROM read to thread.
* Set modstate to -1 (probing) to prevent re-enqueue
* on the next tick before the task completes. */
sc->sc_sfp_modstate = -1;
taskqueue_enqueue(taskqueue_thread, &sc->sc_sfp_task);
return;
}
if (!present && sc->sc_sfp_modstate != 0) {
/* Module removed */
if (sc->sc_sfp_txdis != NULL)
gpio_pin_set_active(sc->sc_sfp_txdis, true);
if_link_state_change(sc->sc_ifnet, LINK_STATE_DOWN);
sc->sc_sfp_modstate = 0;
sc->sc_sfp_los_prev = true;
memset(sc->sc_sfp_id, 0, sizeof(sc->sc_sfp_id));
device_printf(sc->sc_dev, "SFP+ module removed\n");
return;
}
/* Steady state — poll LOS for link changes */
if (sc->sc_sfp_modstate == 1 && sc->sc_sfp_los != NULL) {
gpio_pin_is_active(sc->sc_sfp_los, &los);
if (los != sc->sc_sfp_los_prev) {
sc->sc_sfp_los_prev = los;
if_link_state_change(sc->sc_ifnet,
los ? LINK_STATE_DOWN : LINK_STATE_UP);
}
}
}
/** @} */
/**
* @group IFmedia routines.
* @{
@ -686,8 +897,15 @@ dtsec_ifmedia_sts(if_t ifp, struct ifmediareq *ifmr)
mii_pollstat(sc->sc_mii);
ifmr->ifm_active = sc->sc_mii->mii_media_active;
ifmr->ifm_status = sc->sc_mii->mii_media_status;
} else if (sc->sc_sfp_moddef0 != NULL) {
/* SFP port with GPIO-based status */
ifmr->ifm_active = IFM_ETHER | IFM_10G_SR | IFM_FDX;
if (sc->sc_sfp_modstate != 0 && !sc->sc_sfp_los_prev)
ifmr->ifm_status = IFM_AVALID | IFM_ACTIVE;
else
ifmr->ifm_status = IFM_AVALID;
} else {
/* 10G port with no PHY — report fixed 10G media, always up */
/* 10G port with no SFP GPIOs — legacy always-up */
ifmr->ifm_active = IFM_ETHER | IFM_10G_SR | IFM_FDX;
ifmr->ifm_status = IFM_AVALID | IFM_ACTIVE;
}
@ -819,6 +1037,32 @@ dtsec_sysctl_diag(SYSCTL_HANDLER_ARGS)
return (0);
}
static int
dtsec_sysctl_sfp_info(SYSCTL_HANDLER_ARGS)
{
struct dtsec_softc *sc = (struct dtsec_softc *)arg1;
char buf[48];
DTSEC_LOCK(sc);
if (sc->sc_sfp_modstate != 1) {
strlcpy(buf, "empty", sizeof(buf));
} else {
char vendor[SFP_VENDOR_LEN + 1];
char partnum[SFP_PARTNUM_LEN + 1];
dtsec_sfp_trim(vendor, &sc->sc_sfp_id[SFP_VENDOR_OFFSET],
SFP_VENDOR_LEN);
dtsec_sfp_trim(partnum, &sc->sc_sfp_id[SFP_PARTNUM_OFFSET],
SFP_PARTNUM_LEN);
snprintf(buf, sizeof(buf), "%s %s", vendor, partnum);
}
DTSEC_UNLOCK(sc);
return (sysctl_handle_string(oidp, buf, sizeof(buf), req));
}
static uint64_t
dtsec_get_counter(if_t ifp, ift_counter cnt)
{
@ -877,6 +1121,9 @@ dtsec_attach(device_t dev)
/* Init callouts */
callout_init(&sc->sc_tick_callout, CALLOUT_MPSAFE);
/* Init SFP insert task */
TASK_INIT(&sc->sc_sfp_task, 0, dtsec_sfp_insert_task, sc);
/* Read configuraton */
if ((error = fman_get_handle(parent, &sc->sc_fmh)) != 0)
return (error);
@ -1002,9 +1249,14 @@ dtsec_attach(device_t dev)
else
if_setbaudrate(ifp, IF_Gbps(1ULL));
/* 10G SFP+ direct-attach: link is always up (no PHY) */
if (sc->sc_phy_addr < 0)
if_link_state_change(ifp, LINK_STATE_UP);
/* 10G ports: SFP starts with link down (poll brings it up),
* non-SFP assumes always up (legacy) */
if (sc->sc_phy_addr < 0) {
if (sc->sc_sfp_moddef0 != NULL)
if_link_state_change(ifp, LINK_STATE_DOWN);
else
if_link_state_change(ifp, LINK_STATE_UP);
}
/* Add diagnostic sysctls */
if (sc->sc_mode == DTSEC_MODE_REGULAR) {
@ -1022,6 +1274,14 @@ dtsec_attach(device_t dev)
"diag", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
sc, 0, dtsec_sysctl_diag, "I",
"Write 1 to dump DQRR + BMan state to dmesg");
if (sc->sc_sfp_moddef0 != NULL) {
SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree),
OID_AUTO, "sfp_info",
CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
sc, 0, dtsec_sysctl_sfp_info, "A",
"SFP+ module vendor and part number");
}
}
return (0);

View file

@ -29,6 +29,9 @@
#include "opt_dpaa.h"
#include <sys/taskqueue.h>
#include <dev/gpio/gpiobusvar.h>
/**
* @group dTSEC common API.
* @{
@ -139,6 +142,17 @@ struct dtsec_softc {
/* CEETM DSCP-based egress QoS (populated by CDX module) */
volatile int sc_ceetm_en;
uint32_t sc_ceetm_dscp_fqid[64];
/* SFP module management (10G ports only) */
device_t sc_sfp_dev; /* sff driver instance */
device_t sc_sfp_i2c; /* I2C bus for EEPROM */
gpio_pin_t sc_sfp_moddef0; /* module detect GPIO */
gpio_pin_t sc_sfp_los; /* loss-of-signal GPIO */
gpio_pin_t sc_sfp_txdis; /* TX disable GPIO */
int sc_sfp_modstate; /* 0=absent, 1=present */
bool sc_sfp_los_prev; /* previous LOS for edge detect */
uint8_t sc_sfp_id[64]; /* EEPROM A0h base ID */
struct task sc_sfp_task; /* deferred insert handler */
};
/** @} */

View file

@ -50,6 +50,7 @@
#include <dev/ofw/openfirm.h>
#include "miibus_if.h"
#include "sff_if.h"
#include <contrib/ncsw/inc/Peripherals/fm_port_ext.h>
#include <contrib/ncsw/inc/xx_ext.h>
@ -93,6 +94,7 @@ DRIVER_MODULE(dtsec, fman, dtsec_driver, 0, 0);
DRIVER_MODULE(miibus, dtsec, miibus_driver, 0, 0);
MODULE_DEPEND(dtsec, ether, 1, 1, 1);
MODULE_DEPEND(dtsec, miibus, 1, 1, 1);
MODULE_DEPEND(dtsec, sff, 1, 1, 1);
static int
dtsec_fdt_probe(device_t dev)
@ -182,6 +184,53 @@ dtsec_fdt_attach(device_t dev)
sc->sc_mdio = phy_dev;
skip_phy:
/* Parse SFP phandle for 10G ports */
if (sc->sc_eth_dev_type == ETH_10GSEC) {
phandle_t sfp_xref, sfp_node;
device_t sfp_dev;
if (OF_getencprop(enet_node, "sfp", &sfp_xref,
sizeof(sfp_xref)) > 0) {
sfp_node = OF_node_from_xref(sfp_xref);
sfp_dev = OF_device_from_xref(sfp_xref);
if (sfp_dev != NULL) {
sc->sc_sfp_dev = sfp_dev;
/* Get I2C bus from sff driver */
if (SFF_GET_I2C_BUS(sfp_dev,
&sc->sc_sfp_i2c) != 0) {
device_printf(dev,
"SFP: failed to get I2C bus\n");
sc->sc_sfp_i2c = NULL;
}
/* Acquire GPIOs from SFP DT node */
gpio_pin_get_by_ofw_property(dev, sfp_node,
"mod-def0-gpios", &sc->sc_sfp_moddef0);
gpio_pin_get_by_ofw_property(dev, sfp_node,
"los-gpios", &sc->sc_sfp_los);
gpio_pin_get_by_ofw_property(dev, sfp_node,
"tx-disable-gpios", &sc->sc_sfp_txdis);
/* Hold TX disabled until module detected */
if (sc->sc_sfp_txdis != NULL)
gpio_pin_set_active(sc->sc_sfp_txdis,
true);
if (sc->sc_sfp_moddef0 != NULL)
device_printf(dev,
"SFP+ cage detected (GPIOs OK)\n");
else
device_printf(dev,
"SFP: no mod-def0 GPIO\n");
} else {
device_printf(dev,
"SFP: sff driver not found\n");
}
}
}
/* Get MAC memory offset in SoC */
rid = 0;
sc->sc_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);