net/bnxt: fix resource cleanup
[dpdk.git] / drivers / net / bnxt / bnxt_ethdev.c
index 57a409c..dc3b041 100644 (file)
@@ -219,16 +219,19 @@ uint16_t bnxt_rss_hash_tbl_size(const struct bnxt *bp)
 static void bnxt_free_parent_info(struct bnxt *bp)
 {
        rte_free(bp->parent);
+       bp->parent = NULL;
 }
 
 static void bnxt_free_pf_info(struct bnxt *bp)
 {
        rte_free(bp->pf);
+       bp->pf = NULL;
 }
 
 static void bnxt_free_link_info(struct bnxt *bp)
 {
        rte_free(bp->link_info);
+       bp->link_info = NULL;
 }
 
 static void bnxt_free_leds_info(struct bnxt *bp)
@@ -249,7 +252,9 @@ static void bnxt_free_flow_stats_info(struct bnxt *bp)
 static void bnxt_free_cos_queues(struct bnxt *bp)
 {
        rte_free(bp->rx_cos_queue);
+       bp->rx_cos_queue = NULL;
        rte_free(bp->tx_cos_queue);
+       bp->tx_cos_queue = NULL;
 }
 
 static void bnxt_free_mem(struct bnxt *bp, bool reconfig)
@@ -479,10 +484,11 @@ static int bnxt_setup_one_vnic(struct bnxt *bp, uint16_t vnic_id)
 
        bnxt_hwrm_vnic_plcmode_cfg(bp, vnic);
 
-       if (rx_offloads & DEV_RX_OFFLOAD_TCP_LRO)
-               bnxt_hwrm_vnic_tpa_cfg(bp, vnic, 1);
-       else
-               bnxt_hwrm_vnic_tpa_cfg(bp, vnic, 0);
+       rc = bnxt_hwrm_vnic_tpa_cfg(bp, vnic,
+                                   (rx_offloads & DEV_RX_OFFLOAD_TCP_LRO) ?
+                                   true : false);
+       if (rc)
+               goto err_out;
 
        return 0;
 err_out:
@@ -793,7 +799,7 @@ skip_cosq_cfg:
                        PMD_DRV_LOG(ERR, "Failed to allocate %d rx_queues"
                                " intr_vec", bp->eth_dev->data->nb_rx_queues);
                        rc = -ENOMEM;
-                       goto err_disable;
+                       goto err_out;
                }
                PMD_DRV_LOG(DEBUG, "intr_handle->intr_vec = %p "
                        "intr_handle->nb_efd = %d intr_handle->max_intr = %d\n",
@@ -813,12 +819,12 @@ skip_cosq_cfg:
 #ifndef RTE_EXEC_ENV_FREEBSD
        /* In FreeBSD OS, nic_uio driver does not support interrupts */
        if (rc)
-               goto err_free;
+               goto err_out;
 #endif
 
        rc = bnxt_update_phy_setting(bp);
        if (rc)
-               goto err_free;
+               goto err_out;
 
        bp->mark_table = rte_zmalloc("bnxt_mark_table", BNXT_MARK_TABLE_SZ, 0);
        if (!bp->mark_table)
@@ -826,10 +832,6 @@ skip_cosq_cfg:
 
        return 0;
 
-err_free:
-       rte_free(intr_handle->intr_vec);
-err_disable:
-       rte_intr_efd_disable(intr_handle);
 err_out:
        /* Some of the error status returned by FW may not be from errno.h */
        if (rc > 0)
@@ -852,9 +854,14 @@ static int bnxt_shutdown_nic(struct bnxt *bp)
 
 uint32_t bnxt_get_speed_capabilities(struct bnxt *bp)
 {
-       uint32_t link_speed = bp->link_info->support_speeds;
+       uint32_t link_speed = 0;
        uint32_t speed_capa = 0;
 
+       if (bp->link_info == NULL)
+               return 0;
+
+       link_speed = bp->link_info->support_speeds;
+
        /* If PAM4 is configured, use PAM4 supported speed */
        if (link_speed == 0 && bp->link_info->support_pam4_speeds > 0)
                link_speed = bp->link_info->support_pam4_speeds;
@@ -889,8 +896,6 @@ uint32_t bnxt_get_speed_capabilities(struct bnxt *bp)
        if (bp->link_info->auto_mode ==
            HWRM_PORT_PHY_QCFG_OUTPUT_AUTO_MODE_NONE)
                speed_capa |= ETH_LINK_SPEED_FIXED;
-       else
-               speed_capa |= ETH_LINK_SPEED_AUTONEG;
 
        return speed_capa;
 }
@@ -1268,6 +1273,9 @@ static int bnxt_dev_set_link_up_op(struct rte_eth_dev *eth_dev)
        struct bnxt *bp = eth_dev->data->dev_private;
        int rc = 0;
 
+       if (!BNXT_SINGLE_PF(bp))
+               return -ENOTSUP;
+
        if (!bp->link_info->link_up)
                rc = bnxt_set_hwrm_link_config(bp, true);
        if (!rc)
@@ -1281,6 +1289,9 @@ static int bnxt_dev_set_link_down_op(struct rte_eth_dev *eth_dev)
 {
        struct bnxt *bp = eth_dev->data->dev_private;
 
+       if (!BNXT_SINGLE_PF(bp))
+               return -ENOTSUP;
+
        eth_dev->data->dev_link.link_status = 0;
        bnxt_set_hwrm_link_config(bp, false);
        bp->link_info->link_up = 0;
@@ -1292,12 +1303,13 @@ static void bnxt_free_switch_domain(struct bnxt *bp)
 {
        int rc = 0;
 
-       if (bp->switch_domain_id) {
-               rc = rte_eth_switch_domain_free(bp->switch_domain_id);
-               if (rc)
-                       PMD_DRV_LOG(ERR, "free switch domain:%d fail: %d\n",
-                                   bp->switch_domain_id, rc);
-       }
+       if (!(BNXT_PF(bp) || BNXT_VF_IS_TRUSTED(bp)))
+               return;
+
+       rc = rte_eth_switch_domain_free(bp->switch_domain_id);
+       if (rc)
+               PMD_DRV_LOG(ERR, "free switch domain:%d fail: %d\n",
+                           bp->switch_domain_id, rc);
 }
 
 static void bnxt_ptp_get_current_time(void *arg)
@@ -1544,11 +1556,9 @@ bnxt_uninit_locks(struct bnxt *bp)
 
 static void bnxt_drv_uninit(struct bnxt *bp)
 {
-       bnxt_free_switch_domain(bp);
        bnxt_free_leds_info(bp);
        bnxt_free_cos_queues(bp);
        bnxt_free_link_info(bp);
-       bnxt_free_pf_info(bp);
        bnxt_free_parent_info(bp);
        bnxt_uninit_locks(bp);
 
@@ -1558,6 +1568,7 @@ static void bnxt_drv_uninit(struct bnxt *bp)
        bp->rx_mem_zone = NULL;
 
        bnxt_free_vf_info(bp);
+       bnxt_free_pf_info(bp);
 
        rte_free(bp->grp_info);
        bp->grp_info = NULL;
@@ -1719,6 +1730,10 @@ int bnxt_link_update_op(struct rte_eth_dev *eth_dev, int wait_to_complete)
                return rc;
 
        memset(&new, 0, sizeof(new));
+
+       if (bp->link_info == NULL)
+               goto out;
+
        do {
                /* Retrieve link info from hardware */
                rc = bnxt_get_hwrm_link_config(bp, &new);
@@ -2531,6 +2546,10 @@ static int bnxt_free_one_vnic(struct bnxt *bp, uint16_t vnic_id)
        }
        bnxt_del_dflt_mac_filter(bp, vnic);
 
+       rc = bnxt_hwrm_vnic_ctx_free(bp, vnic);
+       if (rc)
+               return rc;
+
        rc = bnxt_hwrm_vnic_free(bp, vnic);
        if (rc)
                return rc;
@@ -3226,9 +3245,8 @@ bnxt_tx_descriptor_status_op(void *tx_queue, uint16_t offset)
 }
 
 int
-bnxt_filter_ctrl_op(struct rte_eth_dev *dev,
-                   enum rte_filter_type filter_type,
-                   enum rte_filter_op filter_op, void *arg)
+bnxt_flow_ops_get_op(struct rte_eth_dev *dev,
+                    const struct rte_flow_ops **ops)
 {
        struct bnxt *bp = dev->data->dev_private;
        int ret = 0;
@@ -3241,10 +3259,8 @@ bnxt_filter_ctrl_op(struct rte_eth_dev *dev,
                bp = vfr->parent_dev->data->dev_private;
                /* parent is deleted while children are still valid */
                if (!bp) {
-                       PMD_DRV_LOG(DEBUG, "BNXT Port:%d VFR Error %d:%d\n",
-                                   dev->data->port_id,
-                                   filter_type,
-                                   filter_op);
+                       PMD_DRV_LOG(DEBUG, "BNXT Port:%d VFR Error\n",
+                                   dev->data->port_id);
                        return -EIO;
                }
        }
@@ -3253,27 +3269,16 @@ bnxt_filter_ctrl_op(struct rte_eth_dev *dev,
        if (ret)
                return ret;
 
-       switch (filter_type) {
-       case RTE_ETH_FILTER_GENERIC:
-               if (filter_op != RTE_ETH_FILTER_GET)
-                       return -EINVAL;
+       /* PMD supports thread-safe flow operations.  rte_flow API
+        * functions can avoid mutex for multi-thread safety.
+        */
+       dev->data->dev_flags |= RTE_ETH_DEV_FLOW_OPS_THREAD_SAFE;
 
-               /* PMD supports thread-safe flow operations.  rte_flow API
-                * functions can avoid mutex for multi-thread safety.
-                */
-               dev->data->dev_flags |= RTE_ETH_DEV_FLOW_OPS_THREAD_SAFE;
+       if (BNXT_TRUFLOW_EN(bp))
+               *ops = &bnxt_ulp_rte_flow_ops;
+       else
+               *ops = &bnxt_flow_ops;
 
-               if (BNXT_TRUFLOW_EN(bp))
-                       *(const void **)arg = &bnxt_ulp_rte_flow_ops;
-               else
-                       *(const void **)arg = &bnxt_flow_ops;
-               break;
-       default:
-               PMD_DRV_LOG(ERR,
-                       "Filter type (%d) not supported", filter_type);
-               ret = -EINVAL;
-               break;
-       }
        return ret;
 }
 
@@ -3389,9 +3394,6 @@ static int bnxt_get_rx_ts(struct bnxt *bp, uint64_t *ts)
        uint16_t port_id;
        uint32_t fifo;
 
-       if (!ptp)
-               return -ENODEV;
-
        fifo = rte_le_to_cpu_32(rte_read32((uint8_t *)bp->bar0 +
                                ptp->rx_mapped_regs[BNXT_PTP_RX_FIFO]));
        if (!(fifo & BNXT_PTP_RX_FIFO_PENDING))
@@ -3424,7 +3426,7 @@ bnxt_timesync_write_time(struct rte_eth_dev *dev, const struct timespec *ts)
        struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
 
        if (!ptp)
-               return 0;
+               return -ENOTSUP;
 
        ns = rte_timespec_to_ns(ts);
        /* Set the timecounters to a new value. */
@@ -3444,7 +3446,7 @@ bnxt_timesync_read_time(struct rte_eth_dev *dev, struct timespec *ts)
        int rc = 0;
 
        if (!ptp)
-               return 0;
+               return -ENOTSUP;
 
        if (BNXT_CHIP_P5(bp))
                rc = bnxt_hwrm_port_ts_query(bp, BNXT_PTP_FLAGS_CURRENT_TIME,
@@ -3466,7 +3468,7 @@ bnxt_timesync_enable(struct rte_eth_dev *dev)
        int rc;
 
        if (!ptp)
-               return 0;
+               return -ENOTSUP;
 
        ptp->rx_filter = 1;
        ptp->tx_tstamp_en = 1;
@@ -3507,7 +3509,7 @@ bnxt_timesync_disable(struct rte_eth_dev *dev)
        struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
 
        if (!ptp)
-               return 0;
+               return -ENOTSUP;
 
        ptp->rx_filter = 0;
        ptp->tx_tstamp_en = 0;
@@ -3534,7 +3536,7 @@ bnxt_timesync_read_rx_timestamp(struct rte_eth_dev *dev,
        uint64_t ns;
 
        if (!ptp)
-               return 0;
+               return -ENOTSUP;
 
        if (BNXT_CHIP_P5(bp))
                rx_tstamp_cycles = ptp->rx_timestamp;
@@ -3557,7 +3559,7 @@ bnxt_timesync_read_tx_timestamp(struct rte_eth_dev *dev,
        int rc = 0;
 
        if (!ptp)
-               return 0;
+               return -ENOTSUP;
 
        if (BNXT_CHIP_P5(bp))
                rc = bnxt_hwrm_port_ts_query(bp, BNXT_PTP_FLAGS_PATH_TX,
@@ -3578,7 +3580,7 @@ bnxt_timesync_adjust_time(struct rte_eth_dev *dev, int64_t delta)
        struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
 
        if (!ptp)
-               return 0;
+               return -ENOTSUP;
 
        ptp->tc.nsec += delta;
        ptp->tx_tstamp_tc.nsec += delta;
@@ -3793,7 +3795,7 @@ static const struct eth_dev_ops bnxt_dev_ops = {
        .rx_queue_stop = bnxt_rx_queue_stop,
        .tx_queue_start = bnxt_tx_queue_start,
        .tx_queue_stop = bnxt_tx_queue_stop,
-       .filter_ctrl = bnxt_filter_ctrl_op,
+       .flow_ops_get = bnxt_flow_ops_get_op,
        .dev_supported_ptypes_get = bnxt_dev_supported_ptypes_get_op,
        .get_eeprom_length    = bnxt_get_eeprom_length_op,
        .get_eeprom           = bnxt_get_eeprom_op,
@@ -3861,13 +3863,19 @@ static void bnxt_write_fw_reset_reg(struct bnxt *bp, uint32_t index)
        uint32_t val = info->reset_reg_val[index];
        uint32_t reg = info->reset_reg[index];
        uint32_t type, offset;
+       int ret;
 
        type = BNXT_FW_STATUS_REG_TYPE(reg);
        offset = BNXT_FW_STATUS_REG_OFF(reg);
 
        switch (type) {
        case BNXT_FW_STATUS_REG_TYPE_CFG:
-               rte_pci_write_config(bp->pdev, &val, sizeof(val), offset);
+               ret = rte_pci_write_config(bp->pdev, &val, sizeof(val), offset);
+               if (ret < 0) {
+                       PMD_DRV_LOG(ERR, "Failed to write %#x at PCI offset %#x",
+                                   val, offset);
+                       return;
+               }
                break;
        case BNXT_FW_STATUS_REG_TYPE_GRC:
                offset = bnxt_map_reset_regs(bp, offset);
@@ -5471,40 +5479,49 @@ bnxt_parse_devarg_rep_fc_f2r(__rte_unused const char *key,
        return 0;
 }
 
-static void
+static int
 bnxt_parse_dev_args(struct bnxt *bp, struct rte_devargs *devargs)
 {
        struct rte_kvargs *kvlist;
+       int ret;
 
        if (devargs == NULL)
-               return;
+               return 0;
 
        kvlist = rte_kvargs_parse(devargs->args, bnxt_dev_args);
        if (kvlist == NULL)
-               return;
+               return -EINVAL;
 
        /*
         * Handler for "truflow" devarg.
         * Invoked as for ex: "-a 0000:00:0d.0,host-based-truflow=1"
         */
-       rte_kvargs_process(kvlist, BNXT_DEVARG_TRUFLOW,
-                          bnxt_parse_devarg_truflow, bp);
+       ret = rte_kvargs_process(kvlist, BNXT_DEVARG_TRUFLOW,
+                                bnxt_parse_devarg_truflow, bp);
+       if (ret)
+               goto err;
 
        /*
         * Handler for "flow_xstat" devarg.
         * Invoked as for ex: "-a 0000:00:0d.0,flow_xstat=1"
         */
-       rte_kvargs_process(kvlist, BNXT_DEVARG_FLOW_XSTAT,
-                          bnxt_parse_devarg_flow_xstat, bp);
+       ret = rte_kvargs_process(kvlist, BNXT_DEVARG_FLOW_XSTAT,
+                                bnxt_parse_devarg_flow_xstat, bp);
+       if (ret)
+               goto err;
 
        /*
         * Handler for "max_num_kflows" devarg.
         * Invoked as for ex: "-a 000:00:0d.0,max_num_kflows=32"
         */
-       rte_kvargs_process(kvlist, BNXT_DEVARG_MAX_NUM_KFLOWS,
-                          bnxt_parse_devarg_max_num_kflows, bp);
+       ret = rte_kvargs_process(kvlist, BNXT_DEVARG_MAX_NUM_KFLOWS,
+                                bnxt_parse_devarg_max_num_kflows, bp);
+       if (ret)
+               goto err;
 
+err:
        rte_kvargs_free(kvlist);
+       return ret;
 }
 
 static int bnxt_alloc_switch_domain(struct bnxt *bp)
@@ -5586,7 +5603,7 @@ static int bnxt_drv_init(struct rte_eth_dev *eth_dev)
        rc = bnxt_alloc_hwrm_resources(bp);
        if (rc) {
                PMD_DRV_LOG(ERR,
-                           "Failed to allocate hwrm resource rc: %x\n", rc);
+                           "Failed to allocate response buffer rc: %x\n", rc);
                return rc;
        }
        rc = bnxt_alloc_leds_info(bp);
@@ -5639,7 +5656,9 @@ bnxt_dev_init(struct rte_eth_dev *eth_dev, void *params __rte_unused)
        bp = eth_dev->data->dev_private;
 
        /* Parse dev arguments passed on when starting the DPDK application. */
-       bnxt_parse_dev_args(bp, pci_dev->device.devargs);
+       rc = bnxt_parse_dev_args(bp, pci_dev->device.devargs);
+       if (rc)
+               goto error_free;
 
        rc = bnxt_drv_init(eth_dev);
        if (rc)
@@ -5743,7 +5762,10 @@ bnxt_uninit_resources(struct bnxt *bp, bool reconfig_dev)
        bnxt_free_mem(bp, reconfig_dev);
 
        bnxt_hwrm_func_buf_unrgtr(bp);
-       rte_free(bp->pf->vf_req_buf);
+       if (bp->pf != NULL) {
+               rte_free(bp->pf->vf_req_buf);
+               bp->pf->vf_req_buf = NULL;
+       }
 
        rc = bnxt_hwrm_func_driver_unregister(bp, 0);
        bp->flags &= ~BNXT_FLAG_REGISTERED;
@@ -5756,6 +5778,8 @@ bnxt_uninit_resources(struct bnxt *bp, bool reconfig_dev)
        bnxt_uninit_ctx_mem(bp);
 
        bnxt_free_flow_stats_info(bp);
+       if (bp->rep_info != NULL)
+               bnxt_free_switch_domain(bp);
        bnxt_free_rep_info(bp);
        rte_free(bp->ptp_cfg);
        bp->ptp_cfg = NULL;