Commit 9ff2aa42 authored by Vladimir Oltean's avatar Vladimir Oltean Committed by Tony Nguyen
Browse files

net: ethtool: mm: extract stmmac verification logic into common library



It appears that stmmac is not the only hardware which requires a
software-driven verification state machine for the MAC Merge layer.

While on the one hand it's good to encourage hardware implementations,
on the other hand it's quite difficult to tolerate multiple drivers
implementing independently fairly non-trivial logic.

Extract the hardware-independent logic from stmmac into library code and
put it in ethtool. Name the state structure "mmsv" for MAC Merge
Software Verification. Let this expose an operations structure for
executing the hardware stuff: sync hardware with the tx_active boolean
(result of verification process), enable/disable the pMAC, send mPackets,
notify library of external events (reception of mPackets), as well as
link state changes.

Note that it is assumed that the external events are received in hardirq
context. If they are not, it is probably a good idea to disable hardirqs
when calling ethtool_mmsv_event_handle(), because the library does not
do so.

Also, the MM software verification process has no business with the
tx_min_frag_size, that is all the driver's to handle.

Signed-off-by: default avatarVladimir Oltean <vladimir.oltean@nxp.com>
Co-developed-by: default avatarChoong Yong Liang <yong.liang.choong@linux.intel.com>
Signed-off-by: default avatarChoong Yong Liang <yong.liang.choong@linux.intel.com>
Tested-by: default avatarChoong Yong Liang <yong.liang.choong@linux.intel.com>
Tested-by: default avatarFurong Xu <0x1207@gmail.com>
Reviewed-by: default avatarVladimir Oltean <vladimir.oltean@nxp.com>
Signed-off-by: default avatarFaizal Rahim <faizal.abdul.rahim@linux.intel.com>
Signed-off-by: default avatarTony Nguyen <anthony.l.nguyen@intel.com>
parent b375984f
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@ config STMMAC_ETH
	tristate "STMicroelectronics Multi-Gigabit Ethernet driver"
	depends on HAS_IOMEM && HAS_DMA
	depends on PTP_1588_CLOCK_OPTIONAL
	depends on ETHTOOL_NETLINK
	select MII
	select PCS_XPCS
	select PAGE_POOL
+2 −14
Original line number Diff line number Diff line
@@ -149,21 +149,9 @@ struct stmmac_channel {
};

struct stmmac_fpe_cfg {
	/* Serialize access to MAC Merge state between ethtool requests
	 * and link state updates.
	 */
	spinlock_t lock;

	struct ethtool_mmsv mmsv;
	const struct stmmac_fpe_reg *reg;
	u32 fpe_csr;	/* MAC_FPE_CTRL_STS reg cache */

	enum ethtool_mm_verify_status status;
	struct timer_list verify_timer;
	bool verify_enabled;
	int verify_retries;
	bool pmac_enabled;
	u32 verify_time;
	bool tx_enabled;
};

struct stmmac_tc_entry {
+2 −40
Original line number Diff line number Diff line
@@ -1210,7 +1210,6 @@ static int stmmac_get_mm(struct net_device *ndev,
			 struct ethtool_mm_state *state)
{
	struct stmmac_priv *priv = netdev_priv(ndev);
	unsigned long flags;
	u32 frag_size;

	if (!stmmac_fpe_supported(priv))
@@ -1220,26 +1219,7 @@ static int stmmac_get_mm(struct net_device *ndev,
	frag_size = stmmac_fpe_get_add_frag_size(priv);
	state->tx_min_frag_size = ethtool_mm_frag_size_add_to_min(frag_size);

	spin_lock_irqsave(&priv->fpe_cfg.lock, flags);

	state->max_verify_time = STMMAC_FPE_MM_MAX_VERIFY_TIME_MS;
	state->verify_enabled = priv->fpe_cfg.verify_enabled;
	state->pmac_enabled = priv->fpe_cfg.pmac_enabled;
	state->verify_time = priv->fpe_cfg.verify_time;
	state->tx_enabled = priv->fpe_cfg.tx_enabled;
	state->verify_status = priv->fpe_cfg.status;

	/* FPE active if common tx_enabled and
	 * (verification success or disabled(forced))
	 */
	if (state->tx_enabled &&
	    (state->verify_status == ETHTOOL_MM_VERIFY_STATUS_SUCCEEDED ||
	     state->verify_status == ETHTOOL_MM_VERIFY_STATUS_DISABLED))
		state->tx_active = true;
	else
		state->tx_active = false;

	spin_unlock_irqrestore(&priv->fpe_cfg.lock, flags);
	ethtool_mmsv_get_mm(&priv->fpe_cfg.mmsv, state);

	return 0;
}
@@ -1248,8 +1228,6 @@ static int stmmac_set_mm(struct net_device *ndev, struct ethtool_mm_cfg *cfg,
			 struct netlink_ext_ack *extack)
{
	struct stmmac_priv *priv = netdev_priv(ndev);
	struct stmmac_fpe_cfg *fpe_cfg = &priv->fpe_cfg;
	unsigned long flags;
	u32 frag_size;
	int err;

@@ -1259,23 +1237,7 @@ static int stmmac_set_mm(struct net_device *ndev, struct ethtool_mm_cfg *cfg,
		return err;

	stmmac_fpe_set_add_frag_size(priv, frag_size);

	/* Wait for the verification that's currently in progress to finish */
	timer_shutdown_sync(&fpe_cfg->verify_timer);

	spin_lock_irqsave(&fpe_cfg->lock, flags);

	fpe_cfg->verify_enabled = cfg->verify_enabled;
	fpe_cfg->pmac_enabled = cfg->pmac_enabled;
	fpe_cfg->verify_time = cfg->verify_time;
	fpe_cfg->tx_enabled = cfg->tx_enabled;

	if (!cfg->verify_enabled)
		fpe_cfg->status = ETHTOOL_MM_VERIFY_STATUS_DISABLED;

	stmmac_fpe_apply(priv);

	spin_unlock_irqrestore(&fpe_cfg->lock, flags);
	ethtool_mmsv_set_mm(&priv->fpe_cfg.mmsv, cfg);

	return 0;
}
+36 −138
Original line number Diff line number Diff line
@@ -27,12 +27,6 @@
#define STMMAC_MAC_FPE_CTRL_STS_SVER	BIT(1)
#define STMMAC_MAC_FPE_CTRL_STS_EFPE	BIT(0)

/* FPE link-partner hand-shaking mPacket type */
enum stmmac_mpacket_type {
	MPACKET_VERIFY = 0,
	MPACKET_RESPONSE = 1,
};

struct stmmac_fpe_reg {
	const u32 mac_fpe_reg;		/* offset of MAC_FPE_CTRL_STS */
	const u32 mtl_fpe_reg;		/* offset of MTL_FPE_CTRL_STS */
@@ -48,10 +42,10 @@ bool stmmac_fpe_supported(struct stmmac_priv *priv)
		priv->hw->mac->fpe_map_preemption_class;
}

static void stmmac_fpe_configure(struct stmmac_priv *priv, bool tx_enable,
				 bool pmac_enable)
static void stmmac_fpe_configure_tx(struct ethtool_mmsv *mmsv, bool tx_enable)
{
	struct stmmac_fpe_cfg *cfg = &priv->fpe_cfg;
	struct stmmac_fpe_cfg *cfg = container_of(mmsv, struct stmmac_fpe_cfg, mmsv);
	struct stmmac_priv *priv = container_of(cfg, struct stmmac_priv, fpe_cfg);
	const struct stmmac_fpe_reg *reg = cfg->reg;
	u32 num_rxq = priv->plat->rx_queues_to_use;
	void __iomem *ioaddr = priv->ioaddr;
@@ -68,6 +62,15 @@ static void stmmac_fpe_configure(struct stmmac_priv *priv, bool tx_enable,
		cfg->fpe_csr = 0;
	}
	writel(cfg->fpe_csr, ioaddr + reg->mac_fpe_reg);
}

static void stmmac_fpe_configure_pmac(struct ethtool_mmsv *mmsv, bool pmac_enable)
{
	struct stmmac_fpe_cfg *cfg = container_of(mmsv, struct stmmac_fpe_cfg, mmsv);
	struct stmmac_priv *priv = container_of(cfg, struct stmmac_priv, fpe_cfg);
	const struct stmmac_fpe_reg *reg = cfg->reg;
	void __iomem *ioaddr = priv->ioaddr;
	u32 value;

	value = readl(ioaddr + reg->int_en_reg);

@@ -85,47 +88,45 @@ static void stmmac_fpe_configure(struct stmmac_priv *priv, bool tx_enable,
	writel(value, ioaddr + reg->int_en_reg);
}

static void stmmac_fpe_send_mpacket(struct stmmac_priv *priv,
				    enum stmmac_mpacket_type type)
static void stmmac_fpe_send_mpacket(struct ethtool_mmsv *mmsv,
				    enum ethtool_mpacket type)
{
	const struct stmmac_fpe_reg *reg = priv->fpe_cfg.reg;
	struct stmmac_fpe_cfg *cfg = container_of(mmsv, struct stmmac_fpe_cfg, mmsv);
	struct stmmac_priv *priv = container_of(cfg, struct stmmac_priv, fpe_cfg);
	const struct stmmac_fpe_reg *reg = cfg->reg;
	void __iomem *ioaddr = priv->ioaddr;
	u32 value = priv->fpe_cfg.fpe_csr;
	u32 value = cfg->fpe_csr;

	if (type == MPACKET_VERIFY)
	if (type == ETHTOOL_MPACKET_VERIFY)
		value |= STMMAC_MAC_FPE_CTRL_STS_SVER;
	else if (type == MPACKET_RESPONSE)
	else if (type == ETHTOOL_MPACKET_RESPONSE)
		value |= STMMAC_MAC_FPE_CTRL_STS_SRSP;

	writel(value, ioaddr + reg->mac_fpe_reg);
}

static const struct ethtool_mmsv_ops stmmac_mmsv_ops = {
	.configure_tx = stmmac_fpe_configure_tx,
	.configure_pmac = stmmac_fpe_configure_pmac,
	.send_mpacket = stmmac_fpe_send_mpacket,
};

static void stmmac_fpe_event_status(struct stmmac_priv *priv, int status)
{
	struct stmmac_fpe_cfg *fpe_cfg = &priv->fpe_cfg;
	struct ethtool_mmsv *mmsv = &fpe_cfg->mmsv;

	/* This is interrupt context, just spin_lock() */
	spin_lock(&fpe_cfg->lock);

	if (!fpe_cfg->pmac_enabled || status == FPE_EVENT_UNKNOWN)
		goto unlock_out;
	if (status == FPE_EVENT_UNKNOWN)
		return;

	/* LP has sent verify mPacket */
	if ((status & FPE_EVENT_RVER) == FPE_EVENT_RVER)
		stmmac_fpe_send_mpacket(priv, MPACKET_RESPONSE);
		ethtool_mmsv_event_handle(mmsv, ETHTOOL_MMSV_LP_SENT_VERIFY_MPACKET);

	/* Local has sent verify mPacket */
	if ((status & FPE_EVENT_TVER) == FPE_EVENT_TVER &&
	    fpe_cfg->status != ETHTOOL_MM_VERIFY_STATUS_SUCCEEDED)
		fpe_cfg->status = ETHTOOL_MM_VERIFY_STATUS_VERIFYING;
	if ((status & FPE_EVENT_TVER) == FPE_EVENT_TVER)
		ethtool_mmsv_event_handle(mmsv, ETHTOOL_MMSV_LD_SENT_VERIFY_MPACKET);

	/* LP has sent response mPacket */
	if ((status & FPE_EVENT_RRSP) == FPE_EVENT_RRSP &&
	    fpe_cfg->status == ETHTOOL_MM_VERIFY_STATUS_VERIFYING)
		fpe_cfg->status = ETHTOOL_MM_VERIFY_STATUS_SUCCEEDED;

unlock_out:
	spin_unlock(&fpe_cfg->lock);
	if ((status & FPE_EVENT_RRSP) == FPE_EVENT_RRSP)
		ethtool_mmsv_event_handle(mmsv, ETHTOOL_MMSV_LP_SENT_RESPONSE_MPACKET);
}

void stmmac_fpe_irq_status(struct stmmac_priv *priv)
@@ -164,119 +165,16 @@ void stmmac_fpe_irq_status(struct stmmac_priv *priv)
	stmmac_fpe_event_status(priv, status);
}

/**
 * stmmac_fpe_verify_timer - Timer for MAC Merge verification
 * @t:  timer_list struct containing private info
 *
 * Verify the MAC Merge capability in the local TX direction, by
 * transmitting Verify mPackets up to 3 times. Wait until link
 * partner responds with a Response mPacket, otherwise fail.
 */
static void stmmac_fpe_verify_timer(struct timer_list *t)
{
	struct stmmac_fpe_cfg *fpe_cfg = from_timer(fpe_cfg, t, verify_timer);
	struct stmmac_priv *priv = container_of(fpe_cfg, struct stmmac_priv,
						fpe_cfg);
	unsigned long flags;
	bool rearm = false;

	spin_lock_irqsave(&fpe_cfg->lock, flags);

	switch (fpe_cfg->status) {
	case ETHTOOL_MM_VERIFY_STATUS_INITIAL:
	case ETHTOOL_MM_VERIFY_STATUS_VERIFYING:
		if (fpe_cfg->verify_retries != 0) {
			stmmac_fpe_send_mpacket(priv, MPACKET_VERIFY);
			rearm = true;
		} else {
			fpe_cfg->status = ETHTOOL_MM_VERIFY_STATUS_FAILED;
		}

		fpe_cfg->verify_retries--;
		break;

	case ETHTOOL_MM_VERIFY_STATUS_SUCCEEDED:
		stmmac_fpe_configure(priv, true, true);
		break;

	default:
		break;
	}

	if (rearm) {
		mod_timer(&fpe_cfg->verify_timer,
			  jiffies + msecs_to_jiffies(fpe_cfg->verify_time));
	}

	spin_unlock_irqrestore(&fpe_cfg->lock, flags);
}

static void stmmac_fpe_verify_timer_arm(struct stmmac_fpe_cfg *fpe_cfg)
{
	if (fpe_cfg->pmac_enabled && fpe_cfg->tx_enabled &&
	    fpe_cfg->verify_enabled &&
	    fpe_cfg->status != ETHTOOL_MM_VERIFY_STATUS_FAILED &&
	    fpe_cfg->status != ETHTOOL_MM_VERIFY_STATUS_SUCCEEDED) {
		timer_setup(&fpe_cfg->verify_timer, stmmac_fpe_verify_timer, 0);
		mod_timer(&fpe_cfg->verify_timer, jiffies);
	}
}

void stmmac_fpe_init(struct stmmac_priv *priv)
{
	priv->fpe_cfg.verify_retries = STMMAC_FPE_MM_MAX_VERIFY_RETRIES;
	priv->fpe_cfg.verify_time = STMMAC_FPE_MM_MAX_VERIFY_TIME_MS;
	priv->fpe_cfg.status = ETHTOOL_MM_VERIFY_STATUS_DISABLED;
	timer_setup(&priv->fpe_cfg.verify_timer, stmmac_fpe_verify_timer, 0);
	spin_lock_init(&priv->fpe_cfg.lock);
	ethtool_mmsv_init(&priv->fpe_cfg.mmsv, priv->dev,
			  &stmmac_mmsv_ops);

	if ((!priv->fpe_cfg.reg || !priv->hw->mac->fpe_map_preemption_class) &&
	    priv->dma_cap.fpesel)
		dev_info(priv->device, "FPE is not supported by driver.\n");
}

void stmmac_fpe_apply(struct stmmac_priv *priv)
{
	struct stmmac_fpe_cfg *fpe_cfg = &priv->fpe_cfg;

	/* If verification is disabled, configure FPE right away.
	 * Otherwise let the timer code do it.
	 */
	if (!fpe_cfg->verify_enabled) {
		stmmac_fpe_configure(priv, fpe_cfg->tx_enabled,
				     fpe_cfg->pmac_enabled);
	} else {
		fpe_cfg->status = ETHTOOL_MM_VERIFY_STATUS_INITIAL;
		fpe_cfg->verify_retries = STMMAC_FPE_MM_MAX_VERIFY_RETRIES;

		if (netif_running(priv->dev))
			stmmac_fpe_verify_timer_arm(fpe_cfg);
	}
}

void stmmac_fpe_link_state_handle(struct stmmac_priv *priv, bool is_up)
{
	struct stmmac_fpe_cfg *fpe_cfg = &priv->fpe_cfg;
	unsigned long flags;

	timer_shutdown_sync(&fpe_cfg->verify_timer);

	spin_lock_irqsave(&fpe_cfg->lock, flags);

	if (is_up && fpe_cfg->pmac_enabled) {
		/* VERIFY process requires pmac enabled when NIC comes up */
		stmmac_fpe_configure(priv, false, true);

		/* New link => maybe new partner => new verification process */
		stmmac_fpe_apply(priv);
	} else {
		/* No link => turn off EFPE */
		stmmac_fpe_configure(priv, false, false);
	}

	spin_unlock_irqrestore(&fpe_cfg->lock, flags);
}

int stmmac_fpe_get_add_frag_size(struct stmmac_priv *priv)
{
	const struct stmmac_fpe_reg *reg = priv->fpe_cfg.reg;
+0 −5
Original line number Diff line number Diff line
@@ -9,15 +9,10 @@
#include <linux/types.h>
#include <linux/netdevice.h>

#define STMMAC_FPE_MM_MAX_VERIFY_RETRIES	3
#define STMMAC_FPE_MM_MAX_VERIFY_TIME_MS	128

struct stmmac_priv;

void stmmac_fpe_link_state_handle(struct stmmac_priv *priv, bool is_up);
bool stmmac_fpe_supported(struct stmmac_priv *priv);
void stmmac_fpe_init(struct stmmac_priv *priv);
void stmmac_fpe_apply(struct stmmac_priv *priv);
void stmmac_fpe_irq_status(struct stmmac_priv *priv);
int stmmac_fpe_get_add_frag_size(struct stmmac_priv *priv);
void stmmac_fpe_set_add_frag_size(struct stmmac_priv *priv, u32 add_frag_size);
Loading