From: Wei Fang When a VF is driven by DPDK, the VF relies on the PF to provide accurate link speed information so that the VF-side user space application can make correct forwarding and configuration decisions. Therefore, add link speed message support for DPDK-owned VF. The PF will reply the current link speed when it receives the get link speed message from VF. With this enhancement, VFs controlled by DPDK can obtain real-time link speed information from the PF, improving overall link state visibility, synchronization between PF/VF, and enabling more accurate performance adjustments in VF-side applications. Note that currently the link speed change notification is not supported. Signed-off-by: Wei Fang --- .../ethernet/freescale/enetc/enetc_mailbox.h | 30 +++++++ .../net/ethernet/freescale/enetc/enetc_msg.c | 85 +++++++++++++++++++ 2 files changed, 115 insertions(+) diff --git a/drivers/net/ethernet/freescale/enetc/enetc_mailbox.h b/drivers/net/ethernet/freescale/enetc/enetc_mailbox.h index 43b9ee2ab3e4..46446330e222 100644 --- a/drivers/net/ethernet/freescale/enetc/enetc_mailbox.h +++ b/drivers/net/ethernet/freescale/enetc/enetc_mailbox.h @@ -108,6 +108,7 @@ enum enetc_msg_class_id { /* Common Class ID for PSI-to-VSI and VSI-to-PSI messages */ ENETC_MSG_CLASS_ID_MAC_FILTER = 0x20, ENETC_MSG_CLASS_ID_LINK_STATUS = 0x80, + ENETC_MSG_CLASS_ID_LINK_SPEED = 0x81, ENETC_MSG_CLASS_ID_IP_REVISION = 0xf0, }; @@ -125,6 +126,13 @@ enum enetc_msg_link_status_cmd_id { ENETC_MSG_UNREGISTER_LINK_CHANGE_NOTIFIER, }; +enum enetc_msg_link_speed_cmd_id { + ENETC_MSG_GET_CURRENT_LINK_SPEED, + /* The following command IDs are not currently supported */ + ENETC_MSG_REGISTER_SPEED_CHANGE_NOTIFIER, + ENETC_MSG_UNREGISTER_SPEED_CHANGE_NOTIFIER, +}; + /* Class-specific error return codes of MAC filter */ enum enetc_mac_filter_class_code { ENETC_MF_CLASS_CODE_INVALID_MAC, @@ -136,6 +144,23 @@ enum enetc_link_status_class_code { ENETC_LINK_STATUS_CLASS_CODE_DOWN, }; +/* Class-specific notifications/codes of link speed */ +enum enetc_link_speed_class_code { + ENETC_MSG_SPEED_UNKNOWN, + ENETC_MSG_SPEED_10M_HD, + ENETC_MSG_SPEED_10M_FD, + ENETC_MSG_SPEED_100M_HD, + ENETC_MSG_SPEED_100M_FD, + ENETC_MSG_SPEED_1000M, + ENETC_MSG_SPEED_2500M, + ENETC_MSG_SPEED_5G, + ENETC_MSG_SPEED_10G, + ENETC_MSG_SPEED_25G, + ENETC_MSG_SPEED_40G, + ENETC_MSG_SPEED_50G, + ENETC_MSG_SPEED_100G, +}; + struct enetc_msg_swbd { void *vaddr; dma_addr_t dma; @@ -179,6 +204,11 @@ struct enetc_msg_mac_exact_filter { * cmd_id 0x0: get the current link status * cmd_id 0x1: register link status change notification * cmd_id 0x2: unregister link status change notification + * + * Link speed message, class_id 0x81. + * cmd_id 0x0: get the current link speed. + * cmd_id 0x1: register link speed change notification, not supported yet + * cmd_id 0x2: unregister link speed change notification, not supported yet */ struct enetc_msg_generic { struct enetc_msg_header hdr; diff --git a/drivers/net/ethernet/freescale/enetc/enetc_msg.c b/drivers/net/ethernet/freescale/enetc/enetc_msg.c index eac71f90d80b..b24903b23a61 100644 --- a/drivers/net/ethernet/freescale/enetc/enetc_msg.c +++ b/drivers/net/ethernet/freescale/enetc/enetc_msg.c @@ -9,6 +9,9 @@ ENETC_MSG_CLASS_ID_CMD_NOT_SUPPORT) #define ENETC_PF_MSG_PERM_DENY FIELD_PREP(ENETC_PF_MSG_CLASS_ID, \ ENETC_MSG_CLASS_ID_PERMISSION_DENY) +#define ENETC_PF_MSG_SPEED(s) (FIELD_PREP(ENETC_PF_MSG_CLASS_ID, \ + ENETC_MSG_CLASS_ID_LINK_SPEED) | \ + FIELD_PREP(ENETC_PF_MSG_CLASS_CODE, (s))) static void enetc_msg_disable_mr_int(struct enetc_pf *pf) { @@ -274,6 +277,84 @@ static u16 enetc_msg_handle_link_status(struct enetc_pf *pf, int vf_id, } } +static u16 enetc_msg_get_link_speed(struct enetc_pf *pf, int vf_id) +{ + struct enetc_ndev_priv *priv = netdev_priv(pf->si->ndev); + struct enetc_vf_state *vf_state = &pf->vf_state[vf_id]; + struct ethtool_link_ksettings link_info = {}; + + /* A malicious or malfunctioning VM could potentially spam these + * messages in a tight loop causing global rtnl_lock contention, + * which may severely starve other processes on the host that + * require rtnl_lock for routine network configuration, resulting + * in a system-wide control-plane denial of service. Therefore, + * we expect the VF query for link speed to be trusted. There's no + * need to consider the transition from trusted to untrusted here, + * as this won't cause rntl_lock() to be called frequently. + */ + mutex_lock(&vf_state->lock); + if (!(vf_state->flags & ENETC_VF_FLAG_TRUSTED)) { + mutex_unlock(&vf_state->lock); + + return ENETC_PF_MSG_PERM_DENY; + } + mutex_unlock(&vf_state->lock); + + rtnl_lock(); + phylink_ethtool_ksettings_get(priv->phylink, &link_info); + rtnl_unlock(); + + switch (link_info.base.speed) { + case SPEED_10: + if (link_info.base.duplex == DUPLEX_HALF) + return ENETC_PF_MSG_SPEED(ENETC_MSG_SPEED_10M_HD); + else if (link_info.base.duplex == DUPLEX_FULL) + return ENETC_PF_MSG_SPEED(ENETC_MSG_SPEED_10M_FD); + else + return ENETC_PF_MSG_SPEED(ENETC_MSG_SPEED_UNKNOWN); + case SPEED_100: + if (link_info.base.duplex == DUPLEX_HALF) + return ENETC_PF_MSG_SPEED(ENETC_MSG_SPEED_100M_HD); + else if (link_info.base.duplex == DUPLEX_FULL) + return ENETC_PF_MSG_SPEED(ENETC_MSG_SPEED_100M_FD); + else + return ENETC_PF_MSG_SPEED(ENETC_MSG_SPEED_UNKNOWN); + case SPEED_1000: + return ENETC_PF_MSG_SPEED(ENETC_MSG_SPEED_1000M); + case SPEED_2500: + return ENETC_PF_MSG_SPEED(ENETC_MSG_SPEED_2500M); + case SPEED_5000: + return ENETC_PF_MSG_SPEED(ENETC_MSG_SPEED_5G); + case SPEED_10000: + return ENETC_PF_MSG_SPEED(ENETC_MSG_SPEED_10G); + case SPEED_25000: + return ENETC_PF_MSG_SPEED(ENETC_MSG_SPEED_25G); + case SPEED_40000: + return ENETC_PF_MSG_SPEED(ENETC_MSG_SPEED_40G); + case SPEED_50000: + return ENETC_PF_MSG_SPEED(ENETC_MSG_SPEED_50G); + case SPEED_100000: + return ENETC_PF_MSG_SPEED(ENETC_MSG_SPEED_100G); + default: + return ENETC_PF_MSG_SPEED(ENETC_MSG_SPEED_UNKNOWN); + } +} + +static u16 enetc_msg_handle_link_speed(struct enetc_pf *pf, int vf_id, + void *vf_msg) +{ + struct enetc_msg_header *msg_hdr = vf_msg; + + switch (msg_hdr->cmd_id) { + case ENETC_MSG_GET_CURRENT_LINK_SPEED: + return enetc_msg_get_link_speed(pf, vf_id); + case ENETC_MSG_REGISTER_SPEED_CHANGE_NOTIFIER: + case ENETC_MSG_UNREGISTER_SPEED_CHANGE_NOTIFIER: + default: + return ENETC_PF_MSG_NOTSUPP; + } +} + /* If *pf_msg is set to 0, it means that PF has responded to VF in * enetc_msg_handle_rxmsg() through enetc_pf_reply_msg(), which also * clears the corresponding VF MR bit in PSIIDR. @@ -356,6 +437,9 @@ static void enetc_msg_handle_rxmsg(struct enetc_pf *pf, int vf_id, case ENETC_MSG_CLASS_ID_LINK_STATUS: *pf_msg = enetc_msg_handle_link_status(pf, vf_id, msg); break; + case ENETC_MSG_CLASS_ID_LINK_SPEED: + *pf_msg = enetc_msg_handle_link_speed(pf, vf_id, msg); + break; default: dev_err_ratelimited(dev, "Unsupported message class ID: 0x%x\n", @@ -540,6 +624,7 @@ int enetc_sriov_configure(struct pci_dev *pdev, int num_vfs) dev_err(&pdev->dev, "pci_enable_sriov err %d\n", err); goto err_en_sriov; } + } return num_vfs; -- 2.34.1