net/hns3: fix flow control mode
[dpdk.git] / drivers / net / hns3 / hns3_ethdev.c
index a4d17ed..cd6b159 100644 (file)
@@ -981,7 +981,7 @@ hns3_vlan_pvid_configure(struct hns3_adapter *hns, uint16_t pvid, int on)
 {
        struct hns3_hw *hw = &hns->hw;
        uint16_t port_base_vlan_state;
-       int ret;
+       int ret, err;
 
        if (on == 0 && pvid != hw->port_base_vlan_cfg.pvid) {
                if (hw->port_base_vlan_cfg.pvid != HNS3_INVALID_PVID)
@@ -1004,7 +1004,7 @@ hns3_vlan_pvid_configure(struct hns3_adapter *hns, uint16_t pvid, int on)
        if (ret) {
                hns3_err(hw, "failed to config rx vlan strip for pvid, "
                         "ret = %d", ret);
-               return ret;
+               goto pvid_vlan_strip_fail;
        }
 
        if (pvid == HNS3_INVALID_PVID)
@@ -1013,13 +1013,27 @@ hns3_vlan_pvid_configure(struct hns3_adapter *hns, uint16_t pvid, int on)
        if (ret) {
                hns3_err(hw, "failed to update vlan filter entries, ret = %d",
                         ret);
-               return ret;
+               goto vlan_filter_set_fail;
        }
 
 out:
        hw->port_base_vlan_cfg.state = port_base_vlan_state;
        hw->port_base_vlan_cfg.pvid = on ? pvid : HNS3_INVALID_PVID;
        return ret;
+
+vlan_filter_set_fail:
+       err = hns3_en_pvid_strip(hns, hw->port_base_vlan_cfg.state ==
+                                       HNS3_PORT_BASE_VLAN_ENABLE);
+       if (err)
+               hns3_err(hw, "fail to rollback pvid strip, ret = %d", err);
+
+pvid_vlan_strip_fail:
+       err = hns3_vlan_txvlan_cfg(hns, hw->port_base_vlan_cfg.state,
+                                       hw->port_base_vlan_cfg.pvid);
+       if (err)
+               hns3_err(hw, "fail to rollback txvlan status, ret = %d", err);
+
+       return ret;
 }
 
 static int
@@ -2296,11 +2310,11 @@ hns3_bind_ring_with_vector(struct hns3_hw *hw, uint16_t vector_id, bool en,
        struct hns3_cmd_desc desc;
        struct hns3_ctrl_vector_chain_cmd *req =
                (struct hns3_ctrl_vector_chain_cmd *)desc.data;
-       enum hns3_cmd_status status;
        enum hns3_opcode_type op;
        uint16_t tqp_type_and_id = 0;
        uint16_t type;
        uint16_t gl;
+       int ret;
 
        op = en ? HNS3_OPC_ADD_RING_TO_VECTOR : HNS3_OPC_DEL_RING_TO_VECTOR;
        hns3_cmd_setup_basic_desc(&desc, op, false);
@@ -2323,11 +2337,11 @@ hns3_bind_ring_with_vector(struct hns3_hw *hw, uint16_t vector_id, bool en,
                       gl);
        req->tqp_type_and_id[0] = rte_cpu_to_le_16(tqp_type_and_id);
        req->int_cause_num = 1;
-       status = hns3_cmd_send(hw, &desc, 1);
-       if (status) {
-               hns3_err(hw, "%s TQP %u fail, vector_id is %u, status is %d.",
-                        en ? "Map" : "Unmap", queue_id, vector_id, status);
-               return status;
+       ret = hns3_cmd_send(hw, &desc, 1);
+       if (ret) {
+               hns3_err(hw, "%s TQP %u fail, vector_id = %u, ret = %d.",
+                        en ? "Map" : "Unmap", queue_id, vector_id, ret);
+               return ret;
        }
 
        return 0;
@@ -2499,11 +2513,6 @@ hns3_dev_configure(struct rte_eth_dev *dev)
        if (ret)
                goto cfg_err;
 
-       hns->rx_simple_allowed = true;
-       hns->rx_vec_allowed = true;
-       hns->tx_simple_allowed = true;
-       hns->tx_vec_allowed = true;
-
        hns3_init_rx_ptype_tble(dev);
        hw->adapter_state = HNS3_NIC_CONFIGURED;
 
@@ -2739,10 +2748,15 @@ static int
 hns3_update_port_link_info(struct rte_eth_dev *eth_dev)
 {
        struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
+       int ret;
 
        (void)hns3_update_link_status(hw);
 
-       return hns3_update_link_info(eth_dev);
+       ret = hns3_update_link_info(eth_dev);
+       if (ret)
+               hw->mac.link_status = ETH_LINK_DOWN;
+
+       return ret;
 }
 
 static void
@@ -2793,7 +2807,6 @@ hns3_dev_link_update(struct rte_eth_dev *eth_dev, int wait_to_complete)
        do {
                ret = hns3_update_port_link_info(eth_dev);
                if (ret) {
-                       mac->link_status = ETH_LINK_DOWN;
                        hns3_err(hw, "failed to get port link info, ret = %d.",
                                 ret);
                        break;
@@ -4599,7 +4612,10 @@ hns3_update_fiber_link_info(struct hns3_hw *hw)
 static void
 hns3_parse_copper_phy_params(struct hns3_cmd_desc *desc, struct hns3_mac *mac)
 {
+#define HNS3_PHY_SUPPORTED_SPEED_MASK   0x2f
+
        struct hns3_phy_params_bd0_cmd *req;
+       uint32_t supported;
 
        req = (struct hns3_phy_params_bd0_cmd *)desc[0].data;
        mac->link_speed = rte_le_to_cpu_32(req->speed);
@@ -4607,11 +4623,11 @@ hns3_parse_copper_phy_params(struct hns3_cmd_desc *desc, struct hns3_mac *mac)
                                           HNS3_PHY_DUPLEX_CFG_B);
        mac->link_autoneg = hns3_get_bit(req->autoneg,
                                           HNS3_PHY_AUTONEG_CFG_B);
-       mac->supported_capa = rte_le_to_cpu_32(req->supported);
        mac->advertising = rte_le_to_cpu_32(req->advertising);
        mac->lp_advertising = rte_le_to_cpu_32(req->lp_advertising);
-       mac->support_autoneg = !!(mac->supported_capa &
-                               HNS3_PHY_LINK_MODE_AUTONEG_BIT);
+       supported = rte_le_to_cpu_32(req->supported);
+       mac->supported_speed = supported & HNS3_PHY_SUPPORTED_SPEED_MASK;
+       mac->support_autoneg = !!(supported & HNS3_PHY_LINK_MODE_AUTONEG_BIT);
 }
 
 static int
@@ -4660,7 +4676,7 @@ hns3_update_copper_link_info(struct hns3_hw *hw)
        mac->link_speed = mac_info.link_speed;
        mac->link_duplex = mac_info.link_duplex;
        mac->link_autoneg = mac_info.link_autoneg;
-       mac->supported_capa = mac_info.supported_capa;
+       mac->supported_speed = mac_info.supported_speed;
        mac->advertising = mac_info.advertising;
        mac->lp_advertising = mac_info.lp_advertising;
        mac->support_autoneg = mac_info.support_autoneg;
@@ -4766,30 +4782,22 @@ hns3_update_link_status(struct hns3_hw *hw)
        return false;
 }
 
-/*
- * Current, the PF driver get link status by two ways:
- * 1) Periodic polling in the intr thread context, driver call
- *    hns3_update_link_status to update link status.
- * 2) Firmware report async interrupt, driver process the event in the intr
- *    thread context, and call hns3_update_link_status to update link status.
- *
- * If detect link status changed, driver need report LSE. One method is add the
- * report LSE logic in hns3_update_link_status.
- *
- * But the PF driver ops(link_update) also call hns3_update_link_status to
- * update link status.
- * If we report LSE in hns3_update_link_status, it may lead to deadlock in the
- * bonding application.
- *
- * So add the one new API which used only in intr thread context.
- */
 void
-hns3_update_link_status_and_event(struct hns3_hw *hw)
+hns3_update_linkstatus_and_event(struct hns3_hw *hw, bool query)
 {
        struct rte_eth_dev *dev = &rte_eth_devices[hw->data->port_id];
-       bool changed = hns3_update_link_status(hw);
-       if (changed)
-               rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL);
+       struct rte_eth_link new_link;
+       int ret;
+
+       if (query)
+               hns3_update_port_link_info(dev);
+
+       memset(&new_link, 0, sizeof(new_link));
+       hns3_setup_linkstatus(dev, &new_link);
+
+       ret = rte_eth_linkstatus_set(dev, &new_link);
+       if (ret == 0 && dev->data->dev_conf.intr_conf.lsc != 0)
+               hns3_start_report_lse(dev);
 }
 
 static void
@@ -4799,16 +4807,36 @@ hns3_service_handler(void *param)
        struct hns3_adapter *hns = eth_dev->data->dev_private;
        struct hns3_hw *hw = &hns->hw;
 
-       if (!hns3_is_reset_pending(hns)) {
-               hns3_update_link_status_and_event(hw);
-               hns3_update_link_info(eth_dev);
-       } else {
+       if (!hns3_is_reset_pending(hns))
+               hns3_update_linkstatus_and_event(hw, true);
+       else
                hns3_warn(hw, "Cancel the query when reset is pending");
-       }
 
        rte_eal_alarm_set(HNS3_SERVICE_INTERVAL, hns3_service_handler, eth_dev);
 }
 
+static void
+hns3_update_dev_lsc_cap(struct hns3_hw *hw,
+                       int fw_compact_cmd_result)
+{
+       struct rte_eth_dev *dev = &rte_eth_devices[hw->data->port_id];
+
+       if (hw->adapter_state != HNS3_NIC_UNINITIALIZED)
+               return;
+
+       if (fw_compact_cmd_result != 0) {
+               /*
+                * If fw_compact_cmd_result is not zero, it means firmware don't
+                * support link status change interrupt.
+                * Framework already set RTE_ETH_DEV_INTR_LSC bit because driver
+                * declared RTE_PCI_DRV_INTR_LSC in drv_flags. It need to clear
+                * the RTE_ETH_DEV_INTR_LSC capability when detect firmware
+                * don't support link status change interrupt.
+                */
+               dev->data->dev_flags &= ~RTE_ETH_DEV_INTR_LSC;
+       }
+}
+
 static int
 hns3_init_hardware(struct hns3_adapter *hns)
 {
@@ -4896,6 +4924,7 @@ hns3_init_hardware(struct hns3_adapter *hns)
        if (ret)
                PMD_INIT_LOG(WARNING, "firmware compatible features not "
                             "supported, ret = %d.", ret);
+       hns3_update_dev_lsc_cap(hw, ret);
 
        return 0;
 
@@ -4983,7 +5012,7 @@ hns3_init_pf(struct rte_eth_dev *eth_dev)
        ret = hns3_update_imissed_stats(hw, true);
        if (ret) {
                hns3_err(hw, "clear imissed stats failed, ret = %d", ret);
-               return ret;
+               goto err_cmd_init;
        }
 
        hns3_config_all_msix_error(hw, true);
@@ -5288,7 +5317,6 @@ hns3_dev_start(struct rte_eth_dev *dev)
        hns3_rx_scattered_calc(dev);
        hns3_set_rxtx_function(dev);
        hns3_mp_req_start_rxtx(dev);
-       rte_eal_alarm_set(HNS3_SERVICE_INTERVAL, hns3_service_handler, dev);
 
        hns3_restore_filter(dev);
 
@@ -5303,6 +5331,10 @@ hns3_dev_start(struct rte_eth_dev *dev)
 
        hns3_tm_dev_start_proc(hw);
 
+       if (dev->data->dev_conf.intr_conf.lsc != 0)
+               hns3_dev_link_update(dev, 0);
+       rte_eal_alarm_set(HNS3_SERVICE_INTERVAL, hns3_service_handler, dev);
+
        hns3_info(hw, "hns3 dev start successful!");
 
        return 0;
@@ -5416,6 +5448,7 @@ hns3_dev_stop(struct rte_eth_dev *dev)
        }
        hns3_rx_scattered_reset(dev);
        rte_eal_alarm_cancel(hns3_service_handler, dev);
+       hns3_stop_report_lse(dev);
        rte_spinlock_unlock(&hw->lock);
 
        return 0;
@@ -5463,8 +5496,11 @@ hns3_flow_ctrl_get(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
 
        fc_conf->pause_time = pf->pause_time;
 
-       /* return fc current mode */
-       switch (hw->current_mode) {
+       /*
+        * If fc auto-negotiation is not supported, the configured fc mode
+        * from user is the current fc mode.
+        */
+       switch (hw->requested_fc_mode) {
        case HNS3_FC_FULL:
                fc_conf->mode = RTE_FC_FULL;
                break;
@@ -5488,19 +5524,19 @@ hns3_get_fc_mode(struct hns3_hw *hw, enum rte_eth_fc_mode mode)
 {
        switch (mode) {
        case RTE_FC_NONE:
-               hw->requested_mode = HNS3_FC_NONE;
+               hw->requested_fc_mode = HNS3_FC_NONE;
                break;
        case RTE_FC_RX_PAUSE:
-               hw->requested_mode = HNS3_FC_RX_PAUSE;
+               hw->requested_fc_mode = HNS3_FC_RX_PAUSE;
                break;
        case RTE_FC_TX_PAUSE:
-               hw->requested_mode = HNS3_FC_TX_PAUSE;
+               hw->requested_fc_mode = HNS3_FC_TX_PAUSE;
                break;
        case RTE_FC_FULL:
-               hw->requested_mode = HNS3_FC_FULL;
+               hw->requested_fc_mode = HNS3_FC_FULL;
                break;
        default:
-               hw->requested_mode = HNS3_FC_NONE;
+               hw->requested_fc_mode = HNS3_FC_NONE;
                hns3_warn(hw, "fc_mode(%u) exceeds member scope and is "
                          "configured to RTE_FC_NONE", mode);
                break;
@@ -5511,7 +5547,6 @@ static int
 hns3_flow_ctrl_set(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
 {
        struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
-       struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(dev->data->dev_private);
        int ret;
 
        if (fc_conf->high_water || fc_conf->low_water ||
@@ -5540,10 +5575,12 @@ hns3_flow_ctrl_set(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
                return -EOPNOTSUPP;
        }
 
+       if (hw->num_tc > 1) {
+               hns3_err(hw, "in multi-TC scenarios, MAC pause is not supported.");
+               return -EOPNOTSUPP;
+       }
+
        hns3_get_fc_mode(hw, fc_conf->mode);
-       if (hw->requested_mode == hw->current_mode &&
-           pf->pause_time == fc_conf->pause_time)
-               return 0;
 
        rte_spinlock_lock(&hw->lock);
        ret = hns3_fc_enable(dev, fc_conf);
@@ -5557,8 +5594,6 @@ hns3_priority_flow_ctrl_set(struct rte_eth_dev *dev,
                            struct rte_eth_pfc_conf *pfc_conf)
 {
        struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
-       struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(dev->data->dev_private);
-       uint8_t priority;
        int ret;
 
        if (!hns3_dev_dcb_supported(hw)) {
@@ -5593,12 +5628,7 @@ hns3_priority_flow_ctrl_set(struct rte_eth_dev *dev,
                return -EOPNOTSUPP;
        }
 
-       priority = pfc_conf->priority;
        hns3_get_fc_mode(hw, pfc_conf->fc.mode);
-       if (hw->dcb_info.pfc_en & BIT(priority) &&
-           hw->requested_mode == hw->current_mode &&
-           pfc_conf->fc.pause_time == pf->pause_time)
-               return 0;
 
        rte_spinlock_lock(&hw->lock);
        ret = hns3_dcb_pfc_enable(dev, pfc_conf);
@@ -5914,11 +5944,11 @@ hns3_stop_service(struct hns3_adapter *hns)
        struct rte_eth_dev *eth_dev;
 
        eth_dev = &rte_eth_devices[hw->data->port_id];
+       hw->mac.link_status = ETH_LINK_DOWN;
        if (hw->adapter_state == HNS3_NIC_STARTED) {
                rte_eal_alarm_cancel(hns3_service_handler, eth_dev);
-               hns3_update_link_status_and_event(hw);
+               hns3_update_linkstatus_and_event(hw, false);
        }
-       hw->mac.link_status = ETH_LINK_DOWN;
 
        hns3_set_rxtx_function(eth_dev);
        rte_wmb();
@@ -6398,11 +6428,16 @@ hns3_fec_set(struct rte_eth_dev *dev, uint32_t mode)
                return -EINVAL;
        }
 
+       rte_spinlock_lock(&hw->lock);
        ret = hns3_set_fec_hw(hw, mode);
-       if (ret)
+       if (ret) {
+               rte_spinlock_unlock(&hw->lock);
                return ret;
+       }
 
        pf->fec_mode = mode;
+       rte_spinlock_unlock(&hw->lock);
+
        return 0;
 }
 
@@ -6920,7 +6955,7 @@ static const struct rte_pci_id pci_id_hns3_map[] = {
 
 static struct rte_pci_driver rte_hns3_pmd = {
        .id_table = pci_id_hns3_map,
-       .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
+       .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC,
        .probe = eth_hns3_pci_probe,
        .remove = eth_hns3_pci_remove,
 };