Merge tag 'kgdb-5.2-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/danielt...
[linux-2.6-microblaze.git] / drivers / net / ethernet / mellanox / mlx5 / core / eswitch.c
index 8a67fd1..9ea0ccf 100644 (file)
@@ -72,25 +72,22 @@ static void esw_cleanup_vepa_rules(struct mlx5_eswitch *esw);
                            MC_ADDR_CHANGE | \
                            PROMISC_CHANGE)
 
-/* The vport getter/iterator are only valid after esw->total_vports
- * and vport->vport are initialized in mlx5_eswitch_init.
- */
-#define mlx5_esw_for_all_vports(esw, i, vport)                 \
-       for ((i) = MLX5_VPORT_PF;                               \
-            (vport) = &(esw)->vports[i],                       \
-            (i) < (esw)->total_vports; (i)++)
+struct mlx5_vport *__must_check
+mlx5_eswitch_get_vport(struct mlx5_eswitch *esw, u16 vport_num)
+{
+       u16 idx;
 
-#define mlx5_esw_for_each_vf_vport(esw, i, vport, nvfs)        \
-       for ((i) = MLX5_VPORT_FIRST_VF;                         \
-            (vport) = &(esw)->vports[i],                       \
-            (i) <= (nvfs); (i)++)
+       if (!esw || !MLX5_CAP_GEN(esw->dev, vport_group_manager))
+               return ERR_PTR(-EPERM);
 
-static struct mlx5_vport *mlx5_eswitch_get_vport(struct mlx5_eswitch *esw,
-                                                u16 vport_num)
-{
-       u16 idx = mlx5_eswitch_vport_num_to_index(esw, vport_num);
+       idx = mlx5_eswitch_vport_num_to_index(esw, vport_num);
+
+       if (idx > esw->total_vports - 1) {
+               esw_debug(esw->dev, "vport out of range: num(0x%x), idx(0x%x)\n",
+                         vport_num, idx);
+               return ERR_PTR(-EINVAL);
+       }
 
-       WARN_ON(idx > esw->total_vports - 1);
        return &esw->vports[idx];
 }
 
@@ -644,9 +641,8 @@ static int esw_del_mc_addr(struct mlx5_eswitch *esw, struct vport_addr *vaddr)
 
 /* Apply vport UC/MC list to HW l2 table and FDB table */
 static void esw_apply_vport_addr_list(struct mlx5_eswitch *esw,
-                                     u16 vport_num, int list_type)
+                                     struct mlx5_vport *vport, int list_type)
 {
-       struct mlx5_vport *vport = mlx5_eswitch_get_vport(esw, vport_num);
        bool is_uc = list_type == MLX5_NVPRT_LIST_TYPE_UC;
        vport_addr_action vport_addr_add;
        vport_addr_action vport_addr_del;
@@ -679,9 +675,8 @@ static void esw_apply_vport_addr_list(struct mlx5_eswitch *esw,
 
 /* Sync vport UC/MC list from vport context */
 static void esw_update_vport_addr_list(struct mlx5_eswitch *esw,
-                                      u16 vport_num, int list_type)
+                                      struct mlx5_vport *vport, int list_type)
 {
-       struct mlx5_vport *vport = mlx5_eswitch_get_vport(esw, vport_num);
        bool is_uc = list_type == MLX5_NVPRT_LIST_TYPE_UC;
        u8 (*mac_list)[ETH_ALEN];
        struct l2addr_node *node;
@@ -710,12 +705,12 @@ static void esw_update_vport_addr_list(struct mlx5_eswitch *esw,
        if (!vport->enabled)
                goto out;
 
-       err = mlx5_query_nic_vport_mac_list(esw->dev, vport_num, list_type,
+       err = mlx5_query_nic_vport_mac_list(esw->dev, vport->vport, list_type,
                                            mac_list, &size);
        if (err)
                goto out;
        esw_debug(esw->dev, "vport[%d] context update %s list size (%d)\n",
-                 vport_num, is_uc ? "UC" : "MC", size);
+                 vport->vport, is_uc ? "UC" : "MC", size);
 
        for (i = 0; i < size; i++) {
                if (is_uc && !is_valid_ether_addr(mac_list[i]))
@@ -753,10 +748,10 @@ static void esw_update_vport_addr_list(struct mlx5_eswitch *esw,
                if (!addr) {
                        esw_warn(esw->dev,
                                 "Failed to add MAC(%pM) to vport[%d] DB\n",
-                                mac_list[i], vport_num);
+                                mac_list[i], vport->vport);
                        continue;
                }
-               addr->vport = vport_num;
+               addr->vport = vport->vport;
                addr->action = MLX5_ACTION_ADD;
        }
 out:
@@ -766,9 +761,9 @@ out:
 /* Sync vport UC/MC list from vport context
  * Must be called after esw_update_vport_addr_list
  */
-static void esw_update_vport_mc_promisc(struct mlx5_eswitch *esw, u16 vport_num)
+static void esw_update_vport_mc_promisc(struct mlx5_eswitch *esw,
+                                       struct mlx5_vport *vport)
 {
-       struct mlx5_vport *vport = mlx5_eswitch_get_vport(esw, vport_num);
        struct l2addr_node *node;
        struct vport_addr *addr;
        struct hlist_head *hash;
@@ -791,20 +786,20 @@ static void esw_update_vport_mc_promisc(struct mlx5_eswitch *esw, u16 vport_num)
                if (!addr) {
                        esw_warn(esw->dev,
                                 "Failed to add allmulti MAC(%pM) to vport[%d] DB\n",
-                                mac, vport_num);
+                                mac, vport->vport);
                        continue;
                }
-               addr->vport = vport_num;
+               addr->vport = vport->vport;
                addr->action = MLX5_ACTION_ADD;
                addr->mc_promisc = true;
        }
 }
 
 /* Apply vport rx mode to HW FDB table */
-static void esw_apply_vport_rx_mode(struct mlx5_eswitch *esw, u16 vport_num,
+static void esw_apply_vport_rx_mode(struct mlx5_eswitch *esw,
+                                   struct mlx5_vport *vport,
                                    bool promisc, bool mc_promisc)
 {
-       struct mlx5_vport *vport = mlx5_eswitch_get_vport(esw, vport_num);
        struct esw_mc_addr *allmulti_addr = &esw->mc_promisc;
 
        if (IS_ERR_OR_NULL(vport->allmulti_rule) != mc_promisc)
@@ -812,7 +807,7 @@ static void esw_apply_vport_rx_mode(struct mlx5_eswitch *esw, u16 vport_num,
 
        if (mc_promisc) {
                vport->allmulti_rule =
-                               esw_fdb_set_vport_allmulti_rule(esw, vport_num);
+                       esw_fdb_set_vport_allmulti_rule(esw, vport->vport);
                if (!allmulti_addr->uplink_rule)
                        allmulti_addr->uplink_rule =
                                esw_fdb_set_vport_allmulti_rule(esw,
@@ -835,8 +830,8 @@ promisc:
                return;
 
        if (promisc) {
-               vport->promisc_rule = esw_fdb_set_vport_promisc_rule(esw,
-                                                                    vport_num);
+               vport->promisc_rule =
+                       esw_fdb_set_vport_promisc_rule(esw, vport->vport);
        } else if (vport->promisc_rule) {
                mlx5_del_flow_rules(vport->promisc_rule);
                vport->promisc_rule = NULL;
@@ -844,23 +839,23 @@ promisc:
 }
 
 /* Sync vport rx mode from vport context */
-static void esw_update_vport_rx_mode(struct mlx5_eswitch *esw, u16 vport_num)
+static void esw_update_vport_rx_mode(struct mlx5_eswitch *esw,
+                                    struct mlx5_vport *vport)
 {
-       struct mlx5_vport *vport = mlx5_eswitch_get_vport(esw, vport_num);
        int promisc_all = 0;
        int promisc_uc = 0;
        int promisc_mc = 0;
        int err;
 
        err = mlx5_query_nic_vport_promisc(esw->dev,
-                                          vport_num,
+                                          vport->vport,
                                           &promisc_uc,
                                           &promisc_mc,
                                           &promisc_all);
        if (err)
                return;
        esw_debug(esw->dev, "vport[%d] context update rx mode promisc_all=%d, all_multi=%d\n",
-                 vport_num, promisc_all, promisc_mc);
+                 vport->vport, promisc_all, promisc_mc);
 
        if (!vport->info.trusted || !vport->enabled) {
                promisc_uc = 0;
@@ -868,7 +863,7 @@ static void esw_update_vport_rx_mode(struct mlx5_eswitch *esw, u16 vport_num)
                promisc_all = 0;
        }
 
-       esw_apply_vport_rx_mode(esw, vport_num, promisc_all,
+       esw_apply_vport_rx_mode(esw, vport, promisc_all,
                                (promisc_all || promisc_mc));
 }
 
@@ -883,27 +878,21 @@ static void esw_vport_change_handle_locked(struct mlx5_vport *vport)
                  vport->vport, mac);
 
        if (vport->enabled_events & UC_ADDR_CHANGE) {
-               esw_update_vport_addr_list(esw, vport->vport,
-                                          MLX5_NVPRT_LIST_TYPE_UC);
-               esw_apply_vport_addr_list(esw, vport->vport,
-                                         MLX5_NVPRT_LIST_TYPE_UC);
+               esw_update_vport_addr_list(esw, vport, MLX5_NVPRT_LIST_TYPE_UC);
+               esw_apply_vport_addr_list(esw, vport, MLX5_NVPRT_LIST_TYPE_UC);
        }
 
-       if (vport->enabled_events & MC_ADDR_CHANGE) {
-               esw_update_vport_addr_list(esw, vport->vport,
-                                          MLX5_NVPRT_LIST_TYPE_MC);
-       }
+       if (vport->enabled_events & MC_ADDR_CHANGE)
+               esw_update_vport_addr_list(esw, vport, MLX5_NVPRT_LIST_TYPE_MC);
 
        if (vport->enabled_events & PROMISC_CHANGE) {
-               esw_update_vport_rx_mode(esw, vport->vport);
+               esw_update_vport_rx_mode(esw, vport);
                if (!IS_ERR_OR_NULL(vport->allmulti_rule))
-                       esw_update_vport_mc_promisc(esw, vport->vport);
+                       esw_update_vport_mc_promisc(esw, vport);
        }
 
-       if (vport->enabled_events & (PROMISC_CHANGE | MC_ADDR_CHANGE)) {
-               esw_apply_vport_addr_list(esw, vport->vport,
-                                         MLX5_NVPRT_LIST_TYPE_MC);
-       }
+       if (vport->enabled_events & (PROMISC_CHANGE | MC_ADDR_CHANGE))
+               esw_apply_vport_addr_list(esw, vport, MLX5_NVPRT_LIST_TYPE_MC);
 
        esw_debug(esw->dev, "vport[%d] Context Changed: Done\n", vport->vport);
        if (vport->enabled)
@@ -922,8 +911,8 @@ static void esw_vport_change_handler(struct work_struct *work)
        mutex_unlock(&esw->state_lock);
 }
 
-static int esw_vport_enable_egress_acl(struct mlx5_eswitch *esw,
-                                      struct mlx5_vport *vport)
+int esw_vport_enable_egress_acl(struct mlx5_eswitch *esw,
+                               struct mlx5_vport *vport)
 {
        int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
        struct mlx5_flow_group *vlan_grp = NULL;
@@ -1006,8 +995,8 @@ out:
        return err;
 }
 
-static void esw_vport_cleanup_egress_rules(struct mlx5_eswitch *esw,
-                                          struct mlx5_vport *vport)
+void esw_vport_cleanup_egress_rules(struct mlx5_eswitch *esw,
+                                   struct mlx5_vport *vport)
 {
        if (!IS_ERR_OR_NULL(vport->egress.allowed_vlan))
                mlx5_del_flow_rules(vport->egress.allowed_vlan);
@@ -1019,8 +1008,8 @@ static void esw_vport_cleanup_egress_rules(struct mlx5_eswitch *esw,
        vport->egress.drop_rule = NULL;
 }
 
-static void esw_vport_disable_egress_acl(struct mlx5_eswitch *esw,
-                                        struct mlx5_vport *vport)
+void esw_vport_disable_egress_acl(struct mlx5_eswitch *esw,
+                                 struct mlx5_vport *vport)
 {
        if (IS_ERR_OR_NULL(vport->egress.acl))
                return;
@@ -1036,8 +1025,8 @@ static void esw_vport_disable_egress_acl(struct mlx5_eswitch *esw,
        vport->egress.acl = NULL;
 }
 
-static int esw_vport_enable_ingress_acl(struct mlx5_eswitch *esw,
-                                       struct mlx5_vport *vport)
+int esw_vport_enable_ingress_acl(struct mlx5_eswitch *esw,
+                                struct mlx5_vport *vport)
 {
        int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
        struct mlx5_core_dev *dev = esw->dev;
@@ -1168,8 +1157,8 @@ out:
        return err;
 }
 
-static void esw_vport_cleanup_ingress_rules(struct mlx5_eswitch *esw,
-                                           struct mlx5_vport *vport)
+void esw_vport_cleanup_ingress_rules(struct mlx5_eswitch *esw,
+                                    struct mlx5_vport *vport)
 {
        if (!IS_ERR_OR_NULL(vport->ingress.drop_rule))
                mlx5_del_flow_rules(vport->ingress.drop_rule);
@@ -1181,8 +1170,8 @@ static void esw_vport_cleanup_ingress_rules(struct mlx5_eswitch *esw,
        vport->ingress.allow_rule = NULL;
 }
 
-static void esw_vport_disable_ingress_acl(struct mlx5_eswitch *esw,
-                                         struct mlx5_vport *vport)
+void esw_vport_disable_ingress_acl(struct mlx5_eswitch *esw,
+                                  struct mlx5_vport *vport)
 {
        if (IS_ERR_OR_NULL(vport->ingress.acl))
                return;
@@ -1420,10 +1409,10 @@ static void esw_destroy_tsar(struct mlx5_eswitch *esw)
        esw->qos.enabled = false;
 }
 
-static int esw_vport_enable_qos(struct mlx5_eswitch *esw, int vport_num,
+static int esw_vport_enable_qos(struct mlx5_eswitch *esw,
+                               struct mlx5_vport *vport,
                                u32 initial_max_rate, u32 initial_bw_share)
 {
-       struct mlx5_vport *vport = mlx5_eswitch_get_vport(esw, vport_num);
        u32 sched_ctx[MLX5_ST_SZ_DW(scheduling_context)] = {0};
        struct mlx5_core_dev *dev = esw->dev;
        void *vport_elem;
@@ -1440,7 +1429,7 @@ static int esw_vport_enable_qos(struct mlx5_eswitch *esw, int vport_num,
                 SCHEDULING_CONTEXT_ELEMENT_TYPE_VPORT);
        vport_elem = MLX5_ADDR_OF(scheduling_context, sched_ctx,
                                  element_attributes);
-       MLX5_SET(vport_element, vport_elem, vport_number, vport_num);
+       MLX5_SET(vport_element, vport_elem, vport_number, vport->vport);
        MLX5_SET(scheduling_context, sched_ctx, parent_element_id,
                 esw->qos.root_tsar_id);
        MLX5_SET(scheduling_context, sched_ctx, max_average_bw,
@@ -1453,7 +1442,7 @@ static int esw_vport_enable_qos(struct mlx5_eswitch *esw, int vport_num,
                                                 &vport->qos.esw_tsar_ix);
        if (err) {
                esw_warn(esw->dev, "E-Switch create TSAR vport element failed (vport=%d,err=%d)\n",
-                        vport_num, err);
+                        vport->vport, err);
                return err;
        }
 
@@ -1461,10 +1450,10 @@ static int esw_vport_enable_qos(struct mlx5_eswitch *esw, int vport_num,
        return 0;
 }
 
-static void esw_vport_disable_qos(struct mlx5_eswitch *esw, int vport_num)
+static void esw_vport_disable_qos(struct mlx5_eswitch *esw,
+                                 struct mlx5_vport *vport)
 {
-       struct mlx5_vport *vport = mlx5_eswitch_get_vport(esw, vport_num);
-       int err = 0;
+       int err;
 
        if (!vport->qos.enabled)
                return;
@@ -1474,15 +1463,15 @@ static void esw_vport_disable_qos(struct mlx5_eswitch *esw, int vport_num)
                                                  vport->qos.esw_tsar_ix);
        if (err)
                esw_warn(esw->dev, "E-Switch destroy TSAR vport element failed (vport=%d,err=%d)\n",
-                        vport_num, err);
+                        vport->vport, err);
 
        vport->qos.enabled = false;
 }
 
-static int esw_vport_qos_config(struct mlx5_eswitch *esw, int vport_num,
+static int esw_vport_qos_config(struct mlx5_eswitch *esw,
+                               struct mlx5_vport *vport,
                                u32 max_rate, u32 bw_share)
 {
-       struct mlx5_vport *vport = mlx5_eswitch_get_vport(esw, vport_num);
        u32 sched_ctx[MLX5_ST_SZ_DW(scheduling_context)] = {0};
        struct mlx5_core_dev *dev = esw->dev;
        void *vport_elem;
@@ -1499,7 +1488,7 @@ static int esw_vport_qos_config(struct mlx5_eswitch *esw, int vport_num,
                 SCHEDULING_CONTEXT_ELEMENT_TYPE_VPORT);
        vport_elem = MLX5_ADDR_OF(scheduling_context, sched_ctx,
                                  element_attributes);
-       MLX5_SET(vport_element, vport_elem, vport_number, vport_num);
+       MLX5_SET(vport_element, vport_elem, vport_number, vport->vport);
        MLX5_SET(scheduling_context, sched_ctx, parent_element_id,
                 esw->qos.root_tsar_id);
        MLX5_SET(scheduling_context, sched_ctx, max_average_bw,
@@ -1515,7 +1504,7 @@ static int esw_vport_qos_config(struct mlx5_eswitch *esw, int vport_num,
                                                 bitmask);
        if (err) {
                esw_warn(esw->dev, "E-Switch modify TSAR vport element failed (vport=%d,err=%d)\n",
-                        vport_num, err);
+                        vport->vport, err);
                return err;
        }
 
@@ -1618,7 +1607,7 @@ static void esw_enable_vport(struct mlx5_eswitch *esw, struct mlx5_vport *vport,
        esw_apply_vport_conf(esw, vport);
 
        /* Attach vport to the eswitch rate limiter */
-       if (esw_vport_enable_qos(esw, vport_num, vport->info.max_rate,
+       if (esw_vport_enable_qos(esw, vport, vport->info.max_rate,
                                 vport->qos.bw_share))
                esw_warn(esw->dev, "Failed to attach vport %d to eswitch rate limiter", vport_num);
 
@@ -1663,7 +1652,7 @@ static void esw_disable_vport(struct mlx5_eswitch *esw,
         */
        esw_vport_change_handle_locked(vport);
        vport->enabled_events = 0;
-       esw_vport_disable_qos(esw, vport_num);
+       esw_vport_disable_qos(esw, vport);
        if (esw->manager_vport != vport_num &&
            esw->mode == SRIOV_LEGACY) {
                mlx5_modify_vport_admin_state(esw->dev,
@@ -1688,6 +1677,9 @@ static int eswitch_vport_event(struct notifier_block *nb,
 
        vport_num = be16_to_cpu(eqe->data.vport_change.vport_num);
        vport = mlx5_eswitch_get_vport(esw, vport_num);
+       if (IS_ERR(vport))
+               return NOTIFY_OK;
+
        if (vport->enabled)
                queue_work(esw->work_queue, &vport->vport_change_handler);
 
@@ -1922,22 +1914,19 @@ void mlx5_eswitch_cleanup(struct mlx5_eswitch *esw)
 }
 
 /* Vport Administration */
-#define LEGAL_VPORT(esw, vport) (vport >= 0 && vport < esw->total_vports)
-
 int mlx5_eswitch_set_vport_mac(struct mlx5_eswitch *esw,
                               int vport, u8 mac[ETH_ALEN])
 {
-       struct mlx5_vport *evport;
+       struct mlx5_vport *evport = mlx5_eswitch_get_vport(esw, vport);
        u64 node_guid;
        int err = 0;
 
-       if (!esw || !MLX5_CAP_GEN(esw->dev, vport_group_manager))
-               return -EPERM;
-       if (!LEGAL_VPORT(esw, vport) || is_multicast_ether_addr(mac))
+       if (IS_ERR(evport))
+               return PTR_ERR(evport);
+       if (is_multicast_ether_addr(mac))
                return -EINVAL;
 
        mutex_lock(&esw->state_lock);
-       evport = &esw->vports[vport];
 
        if (evport->info.spoofchk && !is_valid_ether_addr(mac))
                mlx5_core_warn(esw->dev,
@@ -1972,16 +1961,15 @@ unlock:
 int mlx5_eswitch_set_vport_state(struct mlx5_eswitch *esw,
                                 int vport, int link_state)
 {
-       struct mlx5_vport *evport;
+       struct mlx5_vport *evport = mlx5_eswitch_get_vport(esw, vport);
        int err = 0;
 
        if (!ESW_ALLOWED(esw))
                return -EPERM;
-       if (!LEGAL_VPORT(esw, vport))
-               return -EINVAL;
+       if (IS_ERR(evport))
+               return PTR_ERR(evport);
 
        mutex_lock(&esw->state_lock);
-       evport = &esw->vports[vport];
 
        err = mlx5_modify_vport_admin_state(esw->dev,
                                            MLX5_VPORT_STATE_OP_MOD_ESW_VPORT,
@@ -2003,14 +1991,10 @@ unlock:
 int mlx5_eswitch_get_vport_config(struct mlx5_eswitch *esw,
                                  int vport, struct ifla_vf_info *ivi)
 {
-       struct mlx5_vport *evport;
-
-       if (!esw || !MLX5_CAP_GEN(esw->dev, vport_group_manager))
-               return -EPERM;
-       if (!LEGAL_VPORT(esw, vport))
-               return -EINVAL;
+       struct mlx5_vport *evport = mlx5_eswitch_get_vport(esw, vport);
 
-       evport = &esw->vports[vport];
+       if (IS_ERR(evport))
+               return PTR_ERR(evport);
 
        memset(ivi, 0, sizeof(*ivi));
        ivi->vf = vport - 1;
@@ -2032,16 +2016,17 @@ int mlx5_eswitch_get_vport_config(struct mlx5_eswitch *esw,
 int __mlx5_eswitch_set_vport_vlan(struct mlx5_eswitch *esw,
                                  int vport, u16 vlan, u8 qos, u8 set_flags)
 {
-       struct mlx5_vport *evport;
+       struct mlx5_vport *evport = mlx5_eswitch_get_vport(esw, vport);
        int err = 0;
 
        if (!ESW_ALLOWED(esw))
                return -EPERM;
-       if (!LEGAL_VPORT(esw, vport) || (vlan > 4095) || (qos > 7))
+       if (IS_ERR(evport))
+               return PTR_ERR(evport);
+       if (vlan > 4095 || qos > 7)
                return -EINVAL;
 
        mutex_lock(&esw->state_lock);
-       evport = &esw->vports[vport];
 
        err = modify_esw_vport_cvlan(esw->dev, vport, vlan, qos, set_flags);
        if (err)
@@ -2075,17 +2060,16 @@ int mlx5_eswitch_set_vport_vlan(struct mlx5_eswitch *esw,
 int mlx5_eswitch_set_vport_spoofchk(struct mlx5_eswitch *esw,
                                    int vport, bool spoofchk)
 {
-       struct mlx5_vport *evport;
+       struct mlx5_vport *evport = mlx5_eswitch_get_vport(esw, vport);
        bool pschk;
        int err = 0;
 
        if (!ESW_ALLOWED(esw))
                return -EPERM;
-       if (!LEGAL_VPORT(esw, vport))
-               return -EINVAL;
+       if (IS_ERR(evport))
+               return PTR_ERR(evport);
 
        mutex_lock(&esw->state_lock);
-       evport = &esw->vports[vport];
        pschk = evport->info.spoofchk;
        evport->info.spoofchk = spoofchk;
        if (pschk && !is_valid_ether_addr(evport->info.mac))
@@ -2226,15 +2210,14 @@ out:
 int mlx5_eswitch_set_vport_trust(struct mlx5_eswitch *esw,
                                 int vport, bool setting)
 {
-       struct mlx5_vport *evport;
+       struct mlx5_vport *evport = mlx5_eswitch_get_vport(esw, vport);
 
        if (!ESW_ALLOWED(esw))
                return -EPERM;
-       if (!LEGAL_VPORT(esw, vport))
-               return -EINVAL;
+       if (IS_ERR(evport))
+               return PTR_ERR(evport);
 
        mutex_lock(&esw->state_lock);
-       evport = &esw->vports[vport];
        evport->info.trusted = setting;
        if (evport->enabled)
                esw_vport_change_handle_locked(evport);
@@ -2284,7 +2267,7 @@ static int normalize_vports_min_rate(struct mlx5_eswitch *esw, u32 divider)
                if (bw_share == evport->qos.bw_share)
                        continue;
 
-               err = esw_vport_qos_config(esw, evport->vport, vport_max_rate,
+               err = esw_vport_qos_config(esw, evport, vport_max_rate,
                                           bw_share);
                if (!err)
                        evport->qos.bw_share = bw_share;
@@ -2298,7 +2281,7 @@ static int normalize_vports_min_rate(struct mlx5_eswitch *esw, u32 divider)
 int mlx5_eswitch_set_vport_rate(struct mlx5_eswitch *esw, int vport,
                                u32 max_rate, u32 min_rate)
 {
-       struct mlx5_vport *evport;
+       struct mlx5_vport *evport = mlx5_eswitch_get_vport(esw, vport);
        u32 fw_max_bw_share;
        u32 previous_min_rate;
        u32 divider;
@@ -2308,8 +2291,8 @@ int mlx5_eswitch_set_vport_rate(struct mlx5_eswitch *esw, int vport,
 
        if (!ESW_ALLOWED(esw))
                return -EPERM;
-       if (!LEGAL_VPORT(esw, vport))
-               return -EINVAL;
+       if (IS_ERR(evport))
+               return PTR_ERR(evport);
 
        fw_max_bw_share = MLX5_CAP_QOS(esw->dev, max_tsar_bw_share);
        min_rate_supported = MLX5_CAP_QOS(esw->dev, esw_bw_share) &&
@@ -2320,7 +2303,6 @@ int mlx5_eswitch_set_vport_rate(struct mlx5_eswitch *esw, int vport,
                return -EOPNOTSUPP;
 
        mutex_lock(&esw->state_lock);
-       evport = &esw->vports[vport];
 
        if (min_rate == evport->info.min_rate)
                goto set_max_rate;
@@ -2338,7 +2320,7 @@ set_max_rate:
        if (max_rate == evport->info.max_rate)
                goto unlock;
 
-       err = esw_vport_qos_config(esw, vport, max_rate, evport->qos.bw_share);
+       err = esw_vport_qos_config(esw, evport, max_rate, evport->qos.bw_share);
        if (!err)
                evport->info.max_rate = max_rate;
 
@@ -2348,11 +2330,10 @@ unlock:
 }
 
 static int mlx5_eswitch_query_vport_drop_stats(struct mlx5_core_dev *dev,
-                                              int vport_idx,
+                                              struct mlx5_vport *vport,
                                               struct mlx5_vport_drop_stats *stats)
 {
        struct mlx5_eswitch *esw = dev->priv.eswitch;
-       struct mlx5_vport *vport = &esw->vports[vport_idx];
        u64 rx_discard_vport_down, tx_discard_vport_down;
        u64 bytes = 0;
        int err = 0;
@@ -2372,7 +2353,7 @@ static int mlx5_eswitch_query_vport_drop_stats(struct mlx5_core_dev *dev,
            !MLX5_CAP_GEN(dev, transmit_discard_vport_down))
                return 0;
 
-       err = mlx5_query_vport_down_stats(dev, vport_idx, 1,
+       err = mlx5_query_vport_down_stats(dev, vport->vport, 1,
                                          &rx_discard_vport_down,
                                          &tx_discard_vport_down);
        if (err)
@@ -2387,19 +2368,18 @@ static int mlx5_eswitch_query_vport_drop_stats(struct mlx5_core_dev *dev,
 }
 
 int mlx5_eswitch_get_vport_stats(struct mlx5_eswitch *esw,
-                                int vport,
+                                int vport_num,
                                 struct ifla_vf_stats *vf_stats)
 {
+       struct mlx5_vport *vport = mlx5_eswitch_get_vport(esw, vport_num);
        int outlen = MLX5_ST_SZ_BYTES(query_vport_counter_out);
        u32 in[MLX5_ST_SZ_DW(query_vport_counter_in)] = {0};
        struct mlx5_vport_drop_stats stats = {0};
        int err = 0;
        u32 *out;
 
-       if (!ESW_ALLOWED(esw))
-               return -EPERM;
-       if (!LEGAL_VPORT(esw, vport))
-               return -EINVAL;
+       if (IS_ERR(vport))
+               return PTR_ERR(vport);
 
        out = kvzalloc(outlen, GFP_KERNEL);
        if (!out)
@@ -2408,7 +2388,7 @@ int mlx5_eswitch_get_vport_stats(struct mlx5_eswitch *esw,
        MLX5_SET(query_vport_counter_in, in, opcode,
                 MLX5_CMD_OP_QUERY_VPORT_COUNTER);
        MLX5_SET(query_vport_counter_in, in, op_mod, 0);
-       MLX5_SET(query_vport_counter_in, in, vport_number, vport);
+       MLX5_SET(query_vport_counter_in, in, vport_number, vport->vport);
        MLX5_SET(query_vport_counter_in, in, other_vport, 1);
 
        memset(out, 0, outlen);