Forward error correction (FEC) is a bit error correction mode.
It adds error correction information to data packets at the
transmit end, and uses the error correction information to correct
the bit errors generated during data packet transmission at the
receive end. This improves signal quality but also brings a delay
to signals. This function can be enabled or disabled as required.

This patch adds FEC support for ethdev.Introduce ethdev
operations which support query and config FEC information in
hardware.

Signed-off-by: Min Hu (Connor) <humi...@huawei.com>
Reviewed-by: Wei Hu (Xavier) <xavier.hu...@huawei.com>
Reviewed-by: Chengwen Feng <fengcheng...@huawei.com>
Reviewed-by: Chengchang Tang <tangchengch...@huawei.com>

---
v2->v3:
adjust the return value of function.
---
 drivers/net/hns3/hns3_cmd.h    |  19 ++++-
 drivers/net/hns3/hns3_ethdev.c | 156 +++++++++++++++++++++++++++++++++++++++++
 drivers/net/hns3/hns3_ethdev.h |   1 +
 3 files changed, 175 insertions(+), 1 deletion(-)

diff --git a/drivers/net/hns3/hns3_cmd.h b/drivers/net/hns3/hns3_cmd.h
index d70f42e..8b80353 100644
--- a/drivers/net/hns3/hns3_cmd.h
+++ b/drivers/net/hns3/hns3_cmd.h
@@ -99,6 +99,7 @@ enum hns3_opcode_type {
        HNS3_OPC_CONFIG_MAX_FRM_SIZE    = 0x0308,
        HNS3_OPC_CONFIG_SPEED_DUP       = 0x0309,
        HNS3_MAC_COMMON_INT_EN          = 0x030E,
+       HNS3_OPC_CONFIG_FEC_MODE        = 0x031A,
 
        /* PFC/Pause commands */
        HNS3_OPC_CFG_MAC_PAUSE_EN       = 0x0701,
@@ -653,9 +654,25 @@ struct hns3_config_auto_neg_cmd {
        uint8_t   rsv[20];
 };
 
+#define HNS3_MAC_CFG_FEC_AUTO_EN_B     0
+#define HNS3_MAC_CFG_FEC_MODE_S                1
+#define HNS3_MAC_CFG_FEC_MODE_M        GENMASK(3, 1)
+#define HNS3_MAC_FEC_OFF               0
+#define HNS3_MAC_FEC_BASER             1
+#define HNS3_MAC_FEC_RS                        2
+
 struct hns3_sfp_speed_cmd {
        uint32_t  sfp_speed;
-       uint32_t  rsv[5];
+       uint8_t   query_type; /* 0: sfp speed, 1: active fec */
+       uint8_t   active_fec; /* current FEC mode */
+       uint16_t  rsv1;
+       uint32_t  rsv2[4];
+};
+
+/* Configure FEC mode, opcode:0x031A */
+struct hns3_config_fec_cmd {
+       uint8_t fec_mode;
+       uint8_t rsv[23];
 };
 
 #define HNS3_MAC_MGR_MASK_VLAN_B               BIT(0)
diff --git a/drivers/net/hns3/hns3_ethdev.c b/drivers/net/hns3/hns3_ethdev.c
index 81e7730..9b5a7c8 100644
--- a/drivers/net/hns3/hns3_ethdev.c
+++ b/drivers/net/hns3/hns3_ethdev.c
@@ -78,6 +78,8 @@ static int hns3_add_mc_addr(struct hns3_hw *hw,
                            struct rte_ether_addr *mac_addr);
 static int hns3_remove_mc_addr(struct hns3_hw *hw,
                            struct rte_ether_addr *mac_addr);
+static int hns3_restore_fec(struct hns3_hw *hw);
+static int hns3_query_dev_fec_info(struct rte_eth_dev *dev);
 
 static void
 hns3_pf_disable_irq0(struct hns3_hw *hw)
@@ -2819,6 +2821,13 @@ hns3_get_capability(struct hns3_hw *hw)
            device_id == HNS3_DEV_ID_200G_RDMA)
                hns3_set_bit(hw->capability, HNS3_DEV_SUPPORT_DCB_B, 1);
 
+       ret = hns3_query_dev_fec_info(eth_dev);
+       if (ret) {
+               PMD_INIT_LOG(ERR,
+                            "failed to query FEC information, ret = %d", ret);
+               return ret;
+       }
+
        /* Get PCI revision id */
        ret = rte_pci_read_config(pci_dev, &revision, HNS3_PCI_REVISION_ID_LEN,
                                  HNS3_PCI_REVISION_ID);
@@ -5311,6 +5320,10 @@ hns3_restore_conf(struct hns3_adapter *hns)
        if (ret)
                goto err_promisc;
 
+       ret = hns3_restore_fec(hw);
+       if (ret)
+               goto err_promisc;
+
        if (hns->hw.adapter_state == HNS3_NIC_STARTED) {
                ret = hns3_do_start(hns, false);
                if (ret)
@@ -5389,6 +5402,146 @@ hns3_reset_service(void *param)
                hns3_msix_process(hns, reset_level);
 }
 
+static int
+hns3_fec_get_capability(struct rte_eth_dev *dev, uint8_t *fec_cap)
+{
+       struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
+       struct hns3_mac *mac = &hw->mac;
+       uint8_t mode;
+
+       switch (mac->link_speed) {
+       case ETH_SPEED_NUM_10G:
+       case ETH_SPEED_NUM_40G:
+               mode = BIT(ETH_FEC_NOFEC) | BIT(ETH_FEC_BASER) |
+                       BIT(ETH_FEC_AUTO);
+               break;
+       case ETH_SPEED_NUM_25G:
+       case ETH_SPEED_NUM_50G:
+               mode = BIT(ETH_FEC_NOFEC) | BIT(ETH_FEC_BASER) |
+                       BIT(ETH_FEC_RS) | BIT(ETH_FEC_AUTO);
+               break;
+       case ETH_SPEED_NUM_100G:
+       case ETH_SPEED_NUM_200G:
+               mode = BIT(ETH_FEC_NOFEC) | BIT(ETH_FEC_RS) | BIT(ETH_FEC_AUTO);
+               break;
+       default:
+               mode = 0;
+               break;
+       }
+
+       *fec_cap = mode;
+       return 0;
+}
+
+static int
+hns3_fec_get(struct rte_eth_dev *dev, enum rte_fec_mode *mode)
+{
+#define QUERY_ACTIVE_SPEED     1
+       struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
+       struct hns3_sfp_speed_cmd *resp;
+       struct hns3_cmd_desc desc;
+       int ret;
+
+       hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_SFP_GET_SPEED, true);
+       resp = (struct hns3_sfp_speed_cmd *)desc.data;
+       resp->query_type = QUERY_ACTIVE_SPEED;
+
+       ret = hns3_cmd_send(hw, &desc, 1);
+       if (ret == -EOPNOTSUPP) {
+               hns3_err(hw, "IMP do not support get FEC, ret = %d", ret);
+               return ret;
+       } else if (ret) {
+               hns3_err(hw, "get FEC failed, ret = %d", ret);
+               return ret;
+       }
+
+       *mode = resp->active_fec;
+       return 0;
+}
+
+static int
+hns3_set_fec_hw(struct hns3_hw *hw, enum rte_fec_mode mode)
+{
+       struct hns3_config_fec_cmd *req;
+       struct hns3_cmd_desc desc;
+       int ret;
+
+       hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CONFIG_FEC_MODE, false);
+
+       req = (struct hns3_config_fec_cmd *)desc.data;
+       switch (mode) {
+       case ETH_FEC_NOFEC:
+               hns3_set_field(req->fec_mode, HNS3_MAC_CFG_FEC_MODE_M,
+                               HNS3_MAC_CFG_FEC_MODE_S, HNS3_MAC_FEC_OFF);
+               break;
+       case ETH_FEC_BASER:
+               hns3_set_field(req->fec_mode, HNS3_MAC_CFG_FEC_MODE_M,
+                               HNS3_MAC_CFG_FEC_MODE_S, HNS3_MAC_FEC_BASER);
+               break;
+       case ETH_FEC_RS:
+               hns3_set_field(req->fec_mode, HNS3_MAC_CFG_FEC_MODE_M,
+                               HNS3_MAC_CFG_FEC_MODE_S, HNS3_MAC_FEC_RS);
+               break;
+       case ETH_FEC_AUTO:
+               hns3_set_bit(req->fec_mode, HNS3_MAC_CFG_FEC_AUTO_EN_B, 1);
+               break;
+       default:
+               return 0;
+       }
+       ret = hns3_cmd_send(hw, &desc, 1);
+       if (ret)
+               hns3_err(hw, "set fec mode failed, ret = %d", ret);
+
+       return ret;
+}
+
+static int
+hns3_fec_set(struct rte_eth_dev *dev, enum rte_fec_mode mode)
+{
+       struct hns3_adapter *hns = dev->data->dev_private;
+       struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(hns);
+       struct hns3_pf *pf = &hns->pf;
+       int ret;
+
+       ret = hns3_set_fec_hw(hw, mode);
+       if (ret)
+               return ret;
+
+       pf->fec_mode = mode;
+       return 0;
+}
+
+static int
+hns3_restore_fec(struct hns3_hw *hw)
+{
+       struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
+       struct hns3_pf *pf = &hns->pf;
+       enum rte_fec_mode mode = pf->fec_mode;
+       int ret;
+
+       ret = hns3_set_fec_hw(hw, mode);
+       if (ret)
+               hns3_err(hw, "restore fec mode(%d) failed, ret = %d",
+                        mode, ret);
+
+       return ret;
+}
+
+static int
+hns3_query_dev_fec_info(struct rte_eth_dev *dev)
+{
+       struct hns3_adapter *hns = dev->data->dev_private;
+       struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(hns);
+       struct hns3_pf *pf = &hns->pf;
+       int ret;
+
+       ret = hns3_fec_get(dev, &pf->fec_mode);
+       if (ret)
+               hns3_err(hw, "query device FEC info failed, ret = %d", ret);
+
+       return ret;
+}
+
 static const struct eth_dev_ops hns3_eth_dev_ops = {
        .dev_start          = hns3_dev_start,
        .dev_stop           = hns3_dev_stop,
@@ -5434,6 +5587,9 @@ static const struct eth_dev_ops hns3_eth_dev_ops = {
        .get_reg                = hns3_get_regs,
        .get_dcb_info           = hns3_get_dcb_info,
        .dev_supported_ptypes_get = hns3_dev_supported_ptypes_get,
+       .fec_get_capability     = hns3_fec_get_capability,
+       .fec_get                = hns3_fec_get,
+       .fec_set                = hns3_fec_set,
 };
 
 static const struct hns3_reset_ops hns3_reset_ops = {
diff --git a/drivers/net/hns3/hns3_ethdev.h b/drivers/net/hns3/hns3_ethdev.h
index 0e665e5..daf723e 100644
--- a/drivers/net/hns3/hns3_ethdev.h
+++ b/drivers/net/hns3/hns3_ethdev.h
@@ -510,6 +510,7 @@ struct hns3_pf {
        struct hns3_err_msix_intr_stats abn_int_stats;
 
        bool support_sfp_query;
+       enum rte_fec_mode fec_mode; /* current FEC mode for ethdev */
 
        struct hns3_vtag_cfg vtag_config;
        LIST_HEAD(vlan_tbl, hns3_user_vlan_table) vlan_list;
-- 
2.7.4

Reply via email to