sff: move SFP module management into sfp_fdt state machine

Move ~900 lines of SFP management code (GPIO polling, EEPROM reading,
RollBall I2C-MDIO PHY probing, auto-negotiation, link monitoring) from
the dtsec MAC driver into the sfp_fdt framework driver.

sfp_fdt now implements a full state machine (EMPTY → PROBE → PRESENT →
PHY_PROBE/WAIT_LOS → PHY_AN/DDM_POLL → LINK_UP) that handles module
detection, identification, and link management for any MAC driver with
SFP+ cages. MAC drivers register a callback table (sfp_upstream_ops)
via SFF_REGISTER_UPSTREAM() and receive module_insert, module_remove,
link_up, and link_down events.

dtsec is reduced to ~50 lines of callback implementations that update
interface link state and store module info for sysctl reporting.

Tested on hardware with both fiber (FLEXOPTIX DAC) and copper (FS.com
10GBASE-T with AQR113C PHY via RollBall) modules at 10 Gbps.
This commit is contained in:
Tomaz Zaman 2026-03-15 23:31:32 +01:00 • committed by Maurice Walker
parent cce1f2579d
commit 107d135ff0
7 changed files with 1326 additions and 1089 deletions

File diff suppressed because it is too large Load diff

View file

@ -30,7 +30,6 @@
#include "opt_dpaa.h"
#include <sys/taskqueue.h>
#include <dev/gpio/gpiobusvar.h>
/**
* @group dTSEC common API.
@ -143,27 +142,13 @@ struct dtsec_softc {
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 */
/* SFP module management (10G ports only, via sfp_fdt callbacks) */
device_t sc_sfp_dev; /* sfp_fdt driver instance */
device_t sc_sfp_i2c; /* I2C bus (for sysctl diag) */
uint8_t sc_sfp_id[64]; /* EEPROM A0h base ID */
struct task sc_sfp_task; /* deferred insert handler */
/* SFP embedded PHY (10GBASE-T copper modules) */
bool sc_sfp_has_phy; /* PHY detected via I2C-MDIO */
#define DTSEC_SFP_PHY_ROLLBALL 1 /* RollBall protocol at 0x51 */
#define DTSEC_SFP_PHY_MDIOI2C 2 /* Standard I2C-MDIO at 0x56 */
int sc_sfp_phy_proto; /* access protocol */
uint32_t sc_sfp_phy_id; /* PHY OUI+model from DEVID1/2 */
bool sc_sfp_phy_link; /* PHY link status */
int sc_sfp_phy_speed; /* negotiated speed (Mbps) */
struct task sc_sfp_phy_task; /* deferred PHY poll */
struct task sc_sfp_ddm_task; /* deferred DDM poll */
bool sc_sfp_modpresent; /* module present */
bool sc_sfp_phy_link; /* link state */
int sc_sfp_phy_speed; /* speed in Mbps */
};
/** @} */
@ -217,4 +202,12 @@ int dtsec_miibus_writereg(device_t dev, int phy, int reg,
void dtsec_miibus_statchg(device_t dev);
/** @} */
/**
* @group SFP upstream callbacks.
* @{
*/
struct sfp_upstream_ops;
extern const struct sfp_upstream_ops dtsec_sfp_ops;
/** @} */
#endif /* IF_DTSEC_H_ */

View file

@ -49,9 +49,9 @@
#include <dev/ofw/ofw_bus_subr.h>
#include <dev/ofw/openfirm.h>
#include <sys/gpio.h>
#include "miibus_if.h"
#include <dev/sff/sfp_fdt.h>
#include "sff_if.h"
#include <contrib/ncsw/inc/Peripherals/fm_port_ext.h>
@ -186,52 +186,24 @@ dtsec_fdt_attach(device_t dev)
sc->sc_mdio = phy_dev;
skip_phy:
/* Parse SFP phandle for 10G ports */
/* Register with SFP framework for 10G ports */
if (sc->sc_eth_dev_type == ETH_10GSEC) {
phandle_t sfp_xref, sfp_node;
phandle_t sfp_xref;
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;
}
/* Keep I2C bus handle for sysctl diag */
SFF_GET_I2C_BUS(sfp_dev, &sc->sc_sfp_i2c);
/* 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);
/* Configure TX disable as output, hold asserted
* until module detected. Without setflags the
* pin stays in INPUT mode and writes are
* silently ignored. */
if (sc->sc_sfp_txdis != NULL) {
gpio_pin_setflags(sc->sc_sfp_txdis,
GPIO_PIN_OUTPUT);
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");
/* Register to receive module/link events */
SFF_REGISTER_UPSTREAM(sfp_dev,
&dtsec_sfp_ops, sc);
} else {
device_printf(dev,
"SFP: sff driver not found\n");

View file

@ -26,6 +26,7 @@
#
#include <machine/bus.h>
#include <dev/sff/sfp_fdt.h>
INTERFACE sff;
@ -33,3 +34,14 @@ METHOD int get_i2c_bus {
device_t dev;
device_t *i2c_bus;
};
#
# Register upstream MAC driver to receive module/link event callbacks.
# ops: function pointer table (sfp_upstream_ops from sfp_fdt.h)
# arg: opaque context passed to each callback (typically MAC softc)
#
METHOD int register_upstream {
device_t dev;
const struct sfp_upstream_ops *ops;
void *arg;
};

File diff suppressed because it is too large Load diff

78
sys/dev/sff/sfp_fdt.h Normal file
View file

@ -0,0 +1,78 @@
/*-
* SPDX-License-Identifier: BSD-2-Clause
*
* Copyright (c) 2026 Mono Technologies Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef _SFP_FDT_H_
#define _SFP_FDT_H_
/*
* SFP upstream operations — callbacks from sfp_fdt to MAC driver.
* MAC driver registers these via SFF_REGISTER_UPSTREAM().
* All callbacks run in taskqueue_thread context (sleepable).
*/
struct sfp_upstream_ops {
/*
* Module inserted. Called after EEPROM read succeeds.
* id points to 64-byte SFP EEPROM A0h base ID data.
* Return 0 to accept, non-zero to reject the module.
*/
int (*module_insert)(void *arg, const uint8_t *id, int id_len);
/*
* Module removed. Clean up any per-module state.
*/
void (*module_remove)(void *arg);
/*
* Link came up. speed is in Mbps (10000, 5000, 2500, 1000).
* For fiber/DAC modules speed is 0 (unknown/fixed).
*/
void (*link_up)(void *arg, int speed);
/*
* Link went down.
*/
void (*link_down)(void *arg);
};
/* SFF-8024 connector types */
#define SFP_CONNECTOR_LC 0x07
#define SFP_CONNECTOR_RJ45 0x22
/* 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 addresses */
#define SFP_EEPROM_ADDR 0x50 /* EEPROM A0h page (7-bit) */
#define SFP_DIAG_ADDR 0x51 /* A2h / RollBall (7-bit) */
#define SFP_PHY_ADDR 0x56 /* Standard I2C-MDIO (7-bit) */
#endif /* _SFP_FDT_H_ */

View file

@ -6,8 +6,9 @@ SRCS+= sff_if.c sff_if.h
SRCS+= bus_if.h device_if.h
.if !empty(OPT_FDT)
SRCS+= sfp_fdt.c \
ofw_bus_if.h
SRCS+= sfp_fdt.c sfp_fdt.h \
ofw_bus_if.h \
gpio_if.h iicbus_if.h
.endif
.include <bsd.kmod.mk>