2 * Copyright (c) 2015, Mellanox Technologies. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 #include <linux/etherdevice.h>
34 #include <linux/debugfs.h>
35 #include <linux/mlx5/driver.h>
36 #include <linux/mlx5/mlx5_ifc.h>
37 #include <linux/mlx5/vport.h>
38 #include <linux/mlx5/fs.h>
39 #include <linux/mlx5/mpfs.h>
40 #include "esw/acl/lgcy.h"
41 #include "esw/legacy.h"
43 #include "mlx5_core.h"
50 #include "en/mod_hdr.h"
58 /* Vport UC/MC hash node */
60 struct l2addr_node node;
63 struct mlx5_flow_handle *flow_rule;
64 bool mpfs; /* UC MAC was added to MPFs */
65 /* A flag indicating that mac was added due to mc promiscuous vport */
69 static int mlx5_eswitch_check(const struct mlx5_core_dev *dev)
71 if (MLX5_CAP_GEN(dev, port_type) != MLX5_CAP_PORT_TYPE_ETH)
74 if (!MLX5_ESWITCH_MANAGER(dev))
80 struct mlx5_eswitch *mlx5_devlink_eswitch_get(struct devlink *devlink)
82 struct mlx5_core_dev *dev = devlink_priv(devlink);
85 err = mlx5_eswitch_check(dev);
89 return dev->priv.eswitch;
92 struct mlx5_vport *__must_check
93 mlx5_eswitch_get_vport(struct mlx5_eswitch *esw, u16 vport_num)
95 struct mlx5_vport *vport;
98 return ERR_PTR(-EPERM);
100 vport = xa_load(&esw->vports, vport_num);
102 esw_debug(esw->dev, "vport out of range: num(0x%x)\n", vport_num);
103 return ERR_PTR(-EINVAL);
108 static int arm_vport_context_events_cmd(struct mlx5_core_dev *dev, u16 vport,
111 u32 in[MLX5_ST_SZ_DW(modify_nic_vport_context_in)] = {};
114 MLX5_SET(modify_nic_vport_context_in, in,
115 opcode, MLX5_CMD_OP_MODIFY_NIC_VPORT_CONTEXT);
116 MLX5_SET(modify_nic_vport_context_in, in, field_select.change_event, 1);
117 MLX5_SET(modify_nic_vport_context_in, in, vport_number, vport);
118 if (vport || mlx5_core_is_ecpf(dev))
119 MLX5_SET(modify_nic_vport_context_in, in, other_vport, 1);
120 nic_vport_ctx = MLX5_ADDR_OF(modify_nic_vport_context_in,
121 in, nic_vport_context);
123 MLX5_SET(nic_vport_context, nic_vport_ctx, arm_change_event, 1);
125 if (events_mask & MLX5_VPORT_UC_ADDR_CHANGE)
126 MLX5_SET(nic_vport_context, nic_vport_ctx,
127 event_on_uc_address_change, 1);
128 if (events_mask & MLX5_VPORT_MC_ADDR_CHANGE)
129 MLX5_SET(nic_vport_context, nic_vport_ctx,
130 event_on_mc_address_change, 1);
131 if (events_mask & MLX5_VPORT_PROMISC_CHANGE)
132 MLX5_SET(nic_vport_context, nic_vport_ctx,
133 event_on_promisc_change, 1);
135 return mlx5_cmd_exec_in(dev, modify_nic_vport_context, in);
138 /* E-Switch vport context HW commands */
139 int mlx5_eswitch_modify_esw_vport_context(struct mlx5_core_dev *dev, u16 vport,
140 bool other_vport, void *in)
142 MLX5_SET(modify_esw_vport_context_in, in, opcode,
143 MLX5_CMD_OP_MODIFY_ESW_VPORT_CONTEXT);
144 MLX5_SET(modify_esw_vport_context_in, in, vport_number, vport);
145 MLX5_SET(modify_esw_vport_context_in, in, other_vport, other_vport);
146 return mlx5_cmd_exec_in(dev, modify_esw_vport_context, in);
149 static int modify_esw_vport_cvlan(struct mlx5_core_dev *dev, u16 vport,
150 u16 vlan, u8 qos, u8 set_flags)
152 u32 in[MLX5_ST_SZ_DW(modify_esw_vport_context_in)] = {};
154 if (!MLX5_CAP_ESW(dev, vport_cvlan_strip) ||
155 !MLX5_CAP_ESW(dev, vport_cvlan_insert_if_not_exist))
158 esw_debug(dev, "Set Vport[%d] VLAN %d qos %d set=%x\n",
159 vport, vlan, qos, set_flags);
161 if (set_flags & SET_VLAN_STRIP)
162 MLX5_SET(modify_esw_vport_context_in, in,
163 esw_vport_context.vport_cvlan_strip, 1);
165 if (set_flags & SET_VLAN_INSERT) {
166 if (MLX5_CAP_ESW(dev, vport_cvlan_insert_always)) {
167 /* insert either if vlan exist in packet or not */
168 MLX5_SET(modify_esw_vport_context_in, in,
169 esw_vport_context.vport_cvlan_insert,
170 MLX5_VPORT_CVLAN_INSERT_ALWAYS);
172 /* insert only if no vlan in packet */
173 MLX5_SET(modify_esw_vport_context_in, in,
174 esw_vport_context.vport_cvlan_insert,
175 MLX5_VPORT_CVLAN_INSERT_WHEN_NO_CVLAN);
177 MLX5_SET(modify_esw_vport_context_in, in,
178 esw_vport_context.cvlan_pcp, qos);
179 MLX5_SET(modify_esw_vport_context_in, in,
180 esw_vport_context.cvlan_id, vlan);
183 MLX5_SET(modify_esw_vport_context_in, in,
184 field_select.vport_cvlan_strip, 1);
185 MLX5_SET(modify_esw_vport_context_in, in,
186 field_select.vport_cvlan_insert, 1);
188 return mlx5_eswitch_modify_esw_vport_context(dev, vport, true, in);
192 static struct mlx5_flow_handle *
193 __esw_fdb_set_vport_rule(struct mlx5_eswitch *esw, u16 vport, bool rx_rule,
194 u8 mac_c[ETH_ALEN], u8 mac_v[ETH_ALEN])
196 int match_header = (is_zero_ether_addr(mac_c) ? 0 :
197 MLX5_MATCH_OUTER_HEADERS);
198 struct mlx5_flow_handle *flow_rule = NULL;
199 struct mlx5_flow_act flow_act = {0};
200 struct mlx5_flow_destination dest = {};
201 struct mlx5_flow_spec *spec;
202 void *mv_misc = NULL;
203 void *mc_misc = NULL;
208 match_header |= MLX5_MATCH_MISC_PARAMETERS;
210 spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
214 dmac_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
215 outer_headers.dmac_47_16);
216 dmac_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
217 outer_headers.dmac_47_16);
219 if (match_header & MLX5_MATCH_OUTER_HEADERS) {
220 ether_addr_copy(dmac_v, mac_v);
221 ether_addr_copy(dmac_c, mac_c);
224 if (match_header & MLX5_MATCH_MISC_PARAMETERS) {
225 mv_misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
227 mc_misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
229 MLX5_SET(fte_match_set_misc, mv_misc, source_port, MLX5_VPORT_UPLINK);
230 MLX5_SET_TO_ONES(fte_match_set_misc, mc_misc, source_port);
233 dest.type = MLX5_FLOW_DESTINATION_TYPE_VPORT;
234 dest.vport.num = vport;
237 "\tFDB add rule dmac_v(%pM) dmac_c(%pM) -> vport(%d)\n",
238 dmac_v, dmac_c, vport);
239 spec->match_criteria_enable = match_header;
240 flow_act.action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
242 mlx5_add_flow_rules(esw->fdb_table.legacy.fdb, spec,
243 &flow_act, &dest, 1);
244 if (IS_ERR(flow_rule)) {
246 "FDB: Failed to add flow rule: dmac_v(%pM) dmac_c(%pM) -> vport(%d), err(%ld)\n",
247 dmac_v, dmac_c, vport, PTR_ERR(flow_rule));
255 static struct mlx5_flow_handle *
256 esw_fdb_set_vport_rule(struct mlx5_eswitch *esw, u8 mac[ETH_ALEN], u16 vport)
260 eth_broadcast_addr(mac_c);
261 return __esw_fdb_set_vport_rule(esw, vport, false, mac_c, mac);
264 static struct mlx5_flow_handle *
265 esw_fdb_set_vport_allmulti_rule(struct mlx5_eswitch *esw, u16 vport)
270 eth_zero_addr(mac_c);
271 eth_zero_addr(mac_v);
274 return __esw_fdb_set_vport_rule(esw, vport, false, mac_c, mac_v);
277 static struct mlx5_flow_handle *
278 esw_fdb_set_vport_promisc_rule(struct mlx5_eswitch *esw, u16 vport)
283 eth_zero_addr(mac_c);
284 eth_zero_addr(mac_v);
285 return __esw_fdb_set_vport_rule(esw, vport, true, mac_c, mac_v);
288 /* E-Switch vport UC/MC lists management */
289 typedef int (*vport_addr_action)(struct mlx5_eswitch *esw,
290 struct vport_addr *vaddr);
292 static int esw_add_uc_addr(struct mlx5_eswitch *esw, struct vport_addr *vaddr)
294 u8 *mac = vaddr->node.addr;
295 u16 vport = vaddr->vport;
298 /* Skip mlx5_mpfs_add_mac for eswitch_managers,
299 * it is already done by its netdev in mlx5e_execute_l2_action
301 if (mlx5_esw_is_manager_vport(esw, vport))
304 err = mlx5_mpfs_add_mac(esw->dev, mac);
307 "Failed to add L2 table mac(%pM) for vport(0x%x), err(%d)\n",
314 /* SRIOV is enabled: Forward UC MAC to vport */
315 if (esw->fdb_table.legacy.fdb && esw->mode == MLX5_ESWITCH_LEGACY) {
316 vaddr->flow_rule = esw_fdb_set_vport_rule(esw, mac, vport);
318 esw_debug(esw->dev, "\tADDED UC MAC: vport[%d] %pM fr(%p)\n",
319 vport, mac, vaddr->flow_rule);
325 static int esw_del_uc_addr(struct mlx5_eswitch *esw, struct vport_addr *vaddr)
327 u8 *mac = vaddr->node.addr;
328 u16 vport = vaddr->vport;
331 /* Skip mlx5_mpfs_del_mac for eswitch managers,
332 * it is already done by its netdev in mlx5e_execute_l2_action
334 if (!vaddr->mpfs || mlx5_esw_is_manager_vport(esw, vport))
337 err = mlx5_mpfs_del_mac(esw->dev, mac);
340 "Failed to del L2 table mac(%pM) for vport(%d), err(%d)\n",
345 if (vaddr->flow_rule)
346 mlx5_del_flow_rules(vaddr->flow_rule);
347 vaddr->flow_rule = NULL;
352 static void update_allmulti_vports(struct mlx5_eswitch *esw,
353 struct vport_addr *vaddr,
354 struct esw_mc_addr *esw_mc)
356 u8 *mac = vaddr->node.addr;
357 struct mlx5_vport *vport;
361 mlx5_esw_for_each_vport(esw, i, vport) {
362 struct hlist_head *vport_hash = vport->mc_list;
363 struct vport_addr *iter_vaddr =
364 l2addr_hash_find(vport_hash,
367 vport_num = vport->vport;
368 if (IS_ERR_OR_NULL(vport->allmulti_rule) ||
369 vaddr->vport == vport_num)
371 switch (vaddr->action) {
372 case MLX5_ACTION_ADD:
375 iter_vaddr = l2addr_hash_add(vport_hash, mac,
380 "ALL-MULTI: Failed to add MAC(%pM) to vport[%d] DB\n",
384 iter_vaddr->vport = vport_num;
385 iter_vaddr->flow_rule =
386 esw_fdb_set_vport_rule(esw,
389 iter_vaddr->mc_promisc = true;
391 case MLX5_ACTION_DEL:
394 mlx5_del_flow_rules(iter_vaddr->flow_rule);
395 l2addr_hash_del(iter_vaddr);
401 static int esw_add_mc_addr(struct mlx5_eswitch *esw, struct vport_addr *vaddr)
403 struct hlist_head *hash = esw->mc_table;
404 struct esw_mc_addr *esw_mc;
405 u8 *mac = vaddr->node.addr;
406 u16 vport = vaddr->vport;
408 if (!esw->fdb_table.legacy.fdb)
411 esw_mc = l2addr_hash_find(hash, mac, struct esw_mc_addr);
415 esw_mc = l2addr_hash_add(hash, mac, struct esw_mc_addr, GFP_KERNEL);
419 esw_mc->uplink_rule = /* Forward MC MAC to Uplink */
420 esw_fdb_set_vport_rule(esw, mac, MLX5_VPORT_UPLINK);
422 /* Add this multicast mac to all the mc promiscuous vports */
423 update_allmulti_vports(esw, vaddr, esw_mc);
426 /* If the multicast mac is added as a result of mc promiscuous vport,
427 * don't increment the multicast ref count
429 if (!vaddr->mc_promisc)
432 /* Forward MC MAC to vport */
433 vaddr->flow_rule = esw_fdb_set_vport_rule(esw, mac, vport);
435 "\tADDED MC MAC: vport[%d] %pM fr(%p) refcnt(%d) uplinkfr(%p)\n",
436 vport, mac, vaddr->flow_rule,
437 esw_mc->refcnt, esw_mc->uplink_rule);
441 static int esw_del_mc_addr(struct mlx5_eswitch *esw, struct vport_addr *vaddr)
443 struct hlist_head *hash = esw->mc_table;
444 struct esw_mc_addr *esw_mc;
445 u8 *mac = vaddr->node.addr;
446 u16 vport = vaddr->vport;
448 if (!esw->fdb_table.legacy.fdb)
451 esw_mc = l2addr_hash_find(hash, mac, struct esw_mc_addr);
454 "Failed to find eswitch MC addr for MAC(%pM) vport(%d)",
459 "\tDELETE MC MAC: vport[%d] %pM fr(%p) refcnt(%d) uplinkfr(%p)\n",
460 vport, mac, vaddr->flow_rule, esw_mc->refcnt,
461 esw_mc->uplink_rule);
463 if (vaddr->flow_rule)
464 mlx5_del_flow_rules(vaddr->flow_rule);
465 vaddr->flow_rule = NULL;
467 /* If the multicast mac is added as a result of mc promiscuous vport,
468 * don't decrement the multicast ref count.
470 if (vaddr->mc_promisc || (--esw_mc->refcnt > 0))
473 /* Remove this multicast mac from all the mc promiscuous vports */
474 update_allmulti_vports(esw, vaddr, esw_mc);
476 if (esw_mc->uplink_rule)
477 mlx5_del_flow_rules(esw_mc->uplink_rule);
479 l2addr_hash_del(esw_mc);
483 /* Apply vport UC/MC list to HW l2 table and FDB table */
484 static void esw_apply_vport_addr_list(struct mlx5_eswitch *esw,
485 struct mlx5_vport *vport, int list_type)
487 bool is_uc = list_type == MLX5_NVPRT_LIST_TYPE_UC;
488 vport_addr_action vport_addr_add;
489 vport_addr_action vport_addr_del;
490 struct vport_addr *addr;
491 struct l2addr_node *node;
492 struct hlist_head *hash;
493 struct hlist_node *tmp;
496 vport_addr_add = is_uc ? esw_add_uc_addr :
498 vport_addr_del = is_uc ? esw_del_uc_addr :
501 hash = is_uc ? vport->uc_list : vport->mc_list;
502 for_each_l2hash_node(node, tmp, hash, hi) {
503 addr = container_of(node, struct vport_addr, node);
504 switch (addr->action) {
505 case MLX5_ACTION_ADD:
506 vport_addr_add(esw, addr);
507 addr->action = MLX5_ACTION_NONE;
509 case MLX5_ACTION_DEL:
510 vport_addr_del(esw, addr);
511 l2addr_hash_del(addr);
517 /* Sync vport UC/MC list from vport context */
518 static void esw_update_vport_addr_list(struct mlx5_eswitch *esw,
519 struct mlx5_vport *vport, int list_type)
521 bool is_uc = list_type == MLX5_NVPRT_LIST_TYPE_UC;
522 u8 (*mac_list)[ETH_ALEN];
523 struct l2addr_node *node;
524 struct vport_addr *addr;
525 struct hlist_head *hash;
526 struct hlist_node *tmp;
532 size = is_uc ? MLX5_MAX_UC_PER_VPORT(esw->dev) :
533 MLX5_MAX_MC_PER_VPORT(esw->dev);
535 mac_list = kcalloc(size, ETH_ALEN, GFP_KERNEL);
539 hash = is_uc ? vport->uc_list : vport->mc_list;
541 for_each_l2hash_node(node, tmp, hash, hi) {
542 addr = container_of(node, struct vport_addr, node);
543 addr->action = MLX5_ACTION_DEL;
549 err = mlx5_query_nic_vport_mac_list(esw->dev, vport->vport, list_type,
553 esw_debug(esw->dev, "vport[%d] context update %s list size (%d)\n",
554 vport->vport, is_uc ? "UC" : "MC", size);
556 for (i = 0; i < size; i++) {
557 if (is_uc && !is_valid_ether_addr(mac_list[i]))
560 if (!is_uc && !is_multicast_ether_addr(mac_list[i]))
563 addr = l2addr_hash_find(hash, mac_list[i], struct vport_addr);
565 addr->action = MLX5_ACTION_NONE;
566 /* If this mac was previously added because of allmulti
567 * promiscuous rx mode, its now converted to be original
570 if (addr->mc_promisc) {
571 struct esw_mc_addr *esw_mc =
572 l2addr_hash_find(esw->mc_table,
577 "Failed to MAC(%pM) in mcast DB\n",
582 addr->mc_promisc = false;
587 addr = l2addr_hash_add(hash, mac_list[i], struct vport_addr,
591 "Failed to add MAC(%pM) to vport[%d] DB\n",
592 mac_list[i], vport->vport);
595 addr->vport = vport->vport;
596 addr->action = MLX5_ACTION_ADD;
602 /* Sync vport UC/MC list from vport context
603 * Must be called after esw_update_vport_addr_list
605 static void esw_update_vport_mc_promisc(struct mlx5_eswitch *esw,
606 struct mlx5_vport *vport)
608 struct l2addr_node *node;
609 struct vport_addr *addr;
610 struct hlist_head *hash;
611 struct hlist_node *tmp;
614 hash = vport->mc_list;
616 for_each_l2hash_node(node, tmp, esw->mc_table, hi) {
617 u8 *mac = node->addr;
619 addr = l2addr_hash_find(hash, mac, struct vport_addr);
621 if (addr->action == MLX5_ACTION_DEL)
622 addr->action = MLX5_ACTION_NONE;
625 addr = l2addr_hash_add(hash, mac, struct vport_addr,
629 "Failed to add allmulti MAC(%pM) to vport[%d] DB\n",
633 addr->vport = vport->vport;
634 addr->action = MLX5_ACTION_ADD;
635 addr->mc_promisc = true;
639 /* Apply vport rx mode to HW FDB table */
640 static void esw_apply_vport_rx_mode(struct mlx5_eswitch *esw,
641 struct mlx5_vport *vport,
642 bool promisc, bool mc_promisc)
644 struct esw_mc_addr *allmulti_addr = &esw->mc_promisc;
646 if (IS_ERR_OR_NULL(vport->allmulti_rule) != mc_promisc)
650 vport->allmulti_rule =
651 esw_fdb_set_vport_allmulti_rule(esw, vport->vport);
652 if (!allmulti_addr->uplink_rule)
653 allmulti_addr->uplink_rule =
654 esw_fdb_set_vport_allmulti_rule(esw,
656 allmulti_addr->refcnt++;
657 } else if (vport->allmulti_rule) {
658 mlx5_del_flow_rules(vport->allmulti_rule);
659 vport->allmulti_rule = NULL;
661 if (--allmulti_addr->refcnt > 0)
664 if (allmulti_addr->uplink_rule)
665 mlx5_del_flow_rules(allmulti_addr->uplink_rule);
666 allmulti_addr->uplink_rule = NULL;
670 if (IS_ERR_OR_NULL(vport->promisc_rule) != promisc)
674 vport->promisc_rule =
675 esw_fdb_set_vport_promisc_rule(esw, vport->vport);
676 } else if (vport->promisc_rule) {
677 mlx5_del_flow_rules(vport->promisc_rule);
678 vport->promisc_rule = NULL;
682 /* Sync vport rx mode from vport context */
683 static void esw_update_vport_rx_mode(struct mlx5_eswitch *esw,
684 struct mlx5_vport *vport)
691 err = mlx5_query_nic_vport_promisc(esw->dev,
698 esw_debug(esw->dev, "vport[%d] context update rx mode promisc_all=%d, all_multi=%d\n",
699 vport->vport, promisc_all, promisc_mc);
701 if (!vport->info.trusted || !vport->enabled) {
707 esw_apply_vport_rx_mode(esw, vport, promisc_all,
708 (promisc_all || promisc_mc));
711 void esw_vport_change_handle_locked(struct mlx5_vport *vport)
713 struct mlx5_core_dev *dev = vport->dev;
714 struct mlx5_eswitch *esw = dev->priv.eswitch;
717 if (!MLX5_CAP_GEN(dev, log_max_l2_table))
720 mlx5_query_nic_vport_mac_address(dev, vport->vport, true, mac);
721 esw_debug(dev, "vport[%d] Context Changed: perm mac: %pM\n",
724 if (vport->enabled_events & MLX5_VPORT_UC_ADDR_CHANGE) {
725 esw_update_vport_addr_list(esw, vport, MLX5_NVPRT_LIST_TYPE_UC);
726 esw_apply_vport_addr_list(esw, vport, MLX5_NVPRT_LIST_TYPE_UC);
729 if (vport->enabled_events & MLX5_VPORT_MC_ADDR_CHANGE)
730 esw_update_vport_addr_list(esw, vport, MLX5_NVPRT_LIST_TYPE_MC);
732 if (vport->enabled_events & MLX5_VPORT_PROMISC_CHANGE) {
733 esw_update_vport_rx_mode(esw, vport);
734 if (!IS_ERR_OR_NULL(vport->allmulti_rule))
735 esw_update_vport_mc_promisc(esw, vport);
738 if (vport->enabled_events & (MLX5_VPORT_PROMISC_CHANGE | MLX5_VPORT_MC_ADDR_CHANGE))
739 esw_apply_vport_addr_list(esw, vport, MLX5_NVPRT_LIST_TYPE_MC);
741 esw_debug(esw->dev, "vport[%d] Context Changed: Done\n", vport->vport);
743 arm_vport_context_events_cmd(dev, vport->vport,
744 vport->enabled_events);
747 static void esw_vport_change_handler(struct work_struct *work)
749 struct mlx5_vport *vport =
750 container_of(work, struct mlx5_vport, vport_change_handler);
751 struct mlx5_eswitch *esw = vport->dev->priv.eswitch;
753 mutex_lock(&esw->state_lock);
754 esw_vport_change_handle_locked(vport);
755 mutex_unlock(&esw->state_lock);
758 static void node_guid_gen_from_mac(u64 *node_guid, const u8 *mac)
760 ((u8 *)node_guid)[7] = mac[0];
761 ((u8 *)node_guid)[6] = mac[1];
762 ((u8 *)node_guid)[5] = mac[2];
763 ((u8 *)node_guid)[4] = 0xff;
764 ((u8 *)node_guid)[3] = 0xfe;
765 ((u8 *)node_guid)[2] = mac[3];
766 ((u8 *)node_guid)[1] = mac[4];
767 ((u8 *)node_guid)[0] = mac[5];
770 static int esw_vport_setup_acl(struct mlx5_eswitch *esw,
771 struct mlx5_vport *vport)
773 if (esw->mode == MLX5_ESWITCH_LEGACY)
774 return esw_legacy_vport_acl_setup(esw, vport);
776 return esw_vport_create_offloads_acl_tables(esw, vport);
779 static void esw_vport_cleanup_acl(struct mlx5_eswitch *esw,
780 struct mlx5_vport *vport)
782 if (esw->mode == MLX5_ESWITCH_LEGACY)
783 esw_legacy_vport_acl_cleanup(esw, vport);
785 esw_vport_destroy_offloads_acl_tables(esw, vport);
788 static int mlx5_esw_vport_caps_get(struct mlx5_eswitch *esw, struct mlx5_vport *vport)
790 int query_out_sz = MLX5_ST_SZ_BYTES(query_hca_cap_out);
795 if (!MLX5_CAP_GEN(esw->dev, vhca_resource_manager))
798 query_ctx = kzalloc(query_out_sz, GFP_KERNEL);
802 err = mlx5_vport_get_other_func_cap(esw->dev, vport->vport, query_ctx,
807 hca_caps = MLX5_ADDR_OF(query_hca_cap_out, query_ctx, capability);
808 vport->info.roce_enabled = MLX5_GET(cmd_hca_cap, hca_caps, roce);
810 if (!MLX5_CAP_GEN_MAX(esw->dev, hca_cap_2))
813 memset(query_ctx, 0, query_out_sz);
814 err = mlx5_vport_get_other_func_cap(esw->dev, vport->vport, query_ctx,
819 hca_caps = MLX5_ADDR_OF(query_hca_cap_out, query_ctx, capability);
820 vport->info.mig_enabled = MLX5_GET(cmd_hca_cap_2, hca_caps, migratable);
826 static int esw_vport_setup(struct mlx5_eswitch *esw, struct mlx5_vport *vport)
828 bool vst_mode_steering = esw_vst_mode_is_steering(esw);
829 u16 vport_num = vport->vport;
833 err = esw_vport_setup_acl(esw, vport);
837 if (mlx5_esw_is_manager_vport(esw, vport_num))
840 err = mlx5_esw_vport_caps_get(esw, vport);
844 mlx5_modify_vport_admin_state(esw->dev,
845 MLX5_VPORT_STATE_OP_MOD_ESW_VPORT,
847 vport->info.link_state);
849 /* Host PF has its own mac/guid. */
851 mlx5_modify_nic_vport_mac_address(esw->dev, vport_num,
853 mlx5_modify_nic_vport_node_guid(esw->dev, vport_num,
854 vport->info.node_guid);
857 flags = (vport->info.vlan || vport->info.qos) ?
858 SET_VLAN_STRIP | SET_VLAN_INSERT : 0;
859 if (esw->mode == MLX5_ESWITCH_OFFLOADS || !vst_mode_steering)
860 modify_esw_vport_cvlan(esw->dev, vport_num, vport->info.vlan,
861 vport->info.qos, flags);
866 esw_vport_cleanup_acl(esw, vport);
870 /* Don't cleanup vport->info, it's needed to restore vport configuration */
871 static void esw_vport_cleanup(struct mlx5_eswitch *esw, struct mlx5_vport *vport)
873 u16 vport_num = vport->vport;
875 if (!mlx5_esw_is_manager_vport(esw, vport_num))
876 mlx5_modify_vport_admin_state(esw->dev,
877 MLX5_VPORT_STATE_OP_MOD_ESW_VPORT,
879 MLX5_VPORT_ADMIN_STATE_DOWN);
881 mlx5_esw_qos_vport_disable(esw, vport);
882 esw_vport_cleanup_acl(esw, vport);
885 int mlx5_esw_vport_enable(struct mlx5_eswitch *esw, u16 vport_num,
886 enum mlx5_eswitch_vport_event enabled_events)
888 struct mlx5_vport *vport;
891 vport = mlx5_eswitch_get_vport(esw, vport_num);
893 return PTR_ERR(vport);
895 mutex_lock(&esw->state_lock);
896 WARN_ON(vport->enabled);
898 esw_debug(esw->dev, "Enabling VPORT(%d)\n", vport_num);
900 ret = esw_vport_setup(esw, vport);
904 /* Sync with current vport context */
905 vport->enabled_events = enabled_events;
906 vport->enabled = true;
908 /* Esw manager is trusted by default. Host PF (vport 0) is trusted as well
909 * in smartNIC as it's a vport group manager.
911 if (mlx5_esw_is_manager_vport(esw, vport_num) ||
912 (!vport_num && mlx5_core_is_ecpf(esw->dev)))
913 vport->info.trusted = true;
915 if (!mlx5_esw_is_manager_vport(esw, vport->vport) &&
916 MLX5_CAP_GEN(esw->dev, vhca_resource_manager)) {
917 ret = mlx5_esw_vport_vhca_id_set(esw, vport_num);
919 goto err_vhca_mapping;
922 /* External controller host PF has factory programmed MAC.
923 * Read it from the device.
925 if (mlx5_core_is_ecpf(esw->dev) && vport_num == MLX5_VPORT_PF)
926 mlx5_query_nic_vport_mac_address(esw->dev, vport_num, true, vport->info.mac);
928 esw_vport_change_handle_locked(vport);
930 esw->enabled_vports++;
931 esw_debug(esw->dev, "Enabled VPORT(%d)\n", vport_num);
933 mutex_unlock(&esw->state_lock);
937 esw_vport_cleanup(esw, vport);
938 mutex_unlock(&esw->state_lock);
942 void mlx5_esw_vport_disable(struct mlx5_eswitch *esw, u16 vport_num)
944 struct mlx5_vport *vport;
946 vport = mlx5_eswitch_get_vport(esw, vport_num);
950 mutex_lock(&esw->state_lock);
954 esw_debug(esw->dev, "Disabling vport(%d)\n", vport_num);
955 /* Mark this vport as disabled to discard new events */
956 vport->enabled = false;
958 /* Disable events from this vport */
959 if (MLX5_CAP_GEN(esw->dev, log_max_l2_table))
960 arm_vport_context_events_cmd(esw->dev, vport->vport, 0);
962 if (!mlx5_esw_is_manager_vport(esw, vport->vport) &&
963 MLX5_CAP_GEN(esw->dev, vhca_resource_manager))
964 mlx5_esw_vport_vhca_id_clear(esw, vport_num);
966 /* We don't assume VFs will cleanup after themselves.
967 * Calling vport change handler while vport is disabled will cleanup
968 * the vport resources.
970 esw_vport_change_handle_locked(vport);
971 vport->enabled_events = 0;
972 esw_apply_vport_rx_mode(esw, vport, false, false);
973 esw_vport_cleanup(esw, vport);
974 esw->enabled_vports--;
977 mutex_unlock(&esw->state_lock);
980 static int eswitch_vport_event(struct notifier_block *nb,
981 unsigned long type, void *data)
983 struct mlx5_eswitch *esw = mlx5_nb_cof(nb, struct mlx5_eswitch, nb);
984 struct mlx5_eqe *eqe = data;
985 struct mlx5_vport *vport;
988 vport_num = be16_to_cpu(eqe->data.vport_change.vport_num);
989 vport = mlx5_eswitch_get_vport(esw, vport_num);
991 queue_work(esw->work_queue, &vport->vport_change_handler);
996 * mlx5_esw_query_functions - Returns raw output about functions state
997 * @dev: Pointer to device to query
999 * mlx5_esw_query_functions() allocates and returns functions changed
1000 * raw output memory pointer from device on success. Otherwise returns ERR_PTR.
1001 * Caller must free the memory using kvfree() when valid pointer is returned.
1003 const u32 *mlx5_esw_query_functions(struct mlx5_core_dev *dev)
1005 int outlen = MLX5_ST_SZ_BYTES(query_esw_functions_out);
1006 u32 in[MLX5_ST_SZ_DW(query_esw_functions_in)] = {};
1010 out = kvzalloc(outlen, GFP_KERNEL);
1012 return ERR_PTR(-ENOMEM);
1014 MLX5_SET(query_esw_functions_in, in, opcode,
1015 MLX5_CMD_OP_QUERY_ESW_FUNCTIONS);
1017 err = mlx5_cmd_exec(dev, in, sizeof(in), out, outlen);
1022 return ERR_PTR(err);
1025 static void mlx5_eswitch_event_handlers_register(struct mlx5_eswitch *esw)
1027 MLX5_NB_INIT(&esw->nb, eswitch_vport_event, NIC_VPORT_CHANGE);
1028 mlx5_eq_notifier_register(esw->dev, &esw->nb);
1030 if (esw->mode == MLX5_ESWITCH_OFFLOADS && mlx5_eswitch_is_funcs_handler(esw->dev)) {
1031 MLX5_NB_INIT(&esw->esw_funcs.nb, mlx5_esw_funcs_changed_handler,
1032 ESW_FUNCTIONS_CHANGED);
1033 mlx5_eq_notifier_register(esw->dev, &esw->esw_funcs.nb);
1037 static void mlx5_eswitch_event_handlers_unregister(struct mlx5_eswitch *esw)
1039 if (esw->mode == MLX5_ESWITCH_OFFLOADS && mlx5_eswitch_is_funcs_handler(esw->dev))
1040 mlx5_eq_notifier_unregister(esw->dev, &esw->esw_funcs.nb);
1042 mlx5_eq_notifier_unregister(esw->dev, &esw->nb);
1044 flush_workqueue(esw->work_queue);
1047 static void mlx5_eswitch_clear_vf_vports_info(struct mlx5_eswitch *esw)
1049 struct mlx5_vport *vport;
1052 mlx5_esw_for_each_vf_vport(esw, i, vport, esw->esw_funcs.num_vfs) {
1053 memset(&vport->qos, 0, sizeof(vport->qos));
1054 memset(&vport->info, 0, sizeof(vport->info));
1055 vport->info.link_state = MLX5_VPORT_ADMIN_STATE_AUTO;
1059 static void mlx5_eswitch_clear_ec_vf_vports_info(struct mlx5_eswitch *esw)
1061 struct mlx5_vport *vport;
1064 mlx5_esw_for_each_ec_vf_vport(esw, i, vport, esw->esw_funcs.num_ec_vfs) {
1065 memset(&vport->qos, 0, sizeof(vport->qos));
1066 memset(&vport->info, 0, sizeof(vport->info));
1067 vport->info.link_state = MLX5_VPORT_ADMIN_STATE_AUTO;
1071 /* Public E-Switch API */
1072 int mlx5_eswitch_load_vport(struct mlx5_eswitch *esw, u16 vport_num,
1073 enum mlx5_eswitch_vport_event enabled_events)
1077 err = mlx5_esw_vport_enable(esw, vport_num, enabled_events);
1081 err = esw_offloads_load_rep(esw, vport_num);
1088 mlx5_esw_vport_disable(esw, vport_num);
1092 void mlx5_eswitch_unload_vport(struct mlx5_eswitch *esw, u16 vport_num)
1094 esw_offloads_unload_rep(esw, vport_num);
1095 mlx5_esw_vport_disable(esw, vport_num);
1098 void mlx5_eswitch_unload_vf_vports(struct mlx5_eswitch *esw, u16 num_vfs)
1100 struct mlx5_vport *vport;
1103 mlx5_esw_for_each_vf_vport(esw, i, vport, num_vfs) {
1104 if (!vport->enabled)
1106 mlx5_eswitch_unload_vport(esw, vport->vport);
1110 static void mlx5_eswitch_unload_ec_vf_vports(struct mlx5_eswitch *esw,
1113 struct mlx5_vport *vport;
1116 mlx5_esw_for_each_ec_vf_vport(esw, i, vport, num_ec_vfs) {
1117 if (!vport->enabled)
1119 mlx5_eswitch_unload_vport(esw, vport->vport);
1123 int mlx5_eswitch_load_vf_vports(struct mlx5_eswitch *esw, u16 num_vfs,
1124 enum mlx5_eswitch_vport_event enabled_events)
1126 struct mlx5_vport *vport;
1130 mlx5_esw_for_each_vf_vport(esw, i, vport, num_vfs) {
1131 err = mlx5_eswitch_load_vport(esw, vport->vport, enabled_events);
1139 mlx5_eswitch_unload_vf_vports(esw, num_vfs);
1143 static int mlx5_eswitch_load_ec_vf_vports(struct mlx5_eswitch *esw, u16 num_ec_vfs,
1144 enum mlx5_eswitch_vport_event enabled_events)
1146 struct mlx5_vport *vport;
1150 mlx5_esw_for_each_ec_vf_vport(esw, i, vport, num_ec_vfs) {
1151 err = mlx5_eswitch_load_vport(esw, vport->vport, enabled_events);
1159 mlx5_eswitch_unload_ec_vf_vports(esw, num_ec_vfs);
1163 static int host_pf_enable_hca(struct mlx5_core_dev *dev)
1165 if (!mlx5_core_is_ecpf(dev))
1168 /* Once vport and representor are ready, take out the external host PF
1169 * out of initializing state. Enabling HCA clears the iser->initializing
1170 * bit and host PF driver loading can progress.
1172 return mlx5_cmd_host_pf_enable_hca(dev);
1175 static void host_pf_disable_hca(struct mlx5_core_dev *dev)
1177 if (!mlx5_core_is_ecpf(dev))
1180 mlx5_cmd_host_pf_disable_hca(dev);
1183 /* mlx5_eswitch_enable_pf_vf_vports() enables vports of PF, ECPF and VFs
1184 * whichever are present on the eswitch.
1187 mlx5_eswitch_enable_pf_vf_vports(struct mlx5_eswitch *esw,
1188 enum mlx5_eswitch_vport_event enabled_events)
1192 /* Enable PF vport */
1193 ret = mlx5_eswitch_load_vport(esw, MLX5_VPORT_PF, enabled_events);
1197 /* Enable external host PF HCA */
1198 ret = host_pf_enable_hca(esw->dev);
1202 /* Enable ECPF vport */
1203 if (mlx5_ecpf_vport_exists(esw->dev)) {
1204 ret = mlx5_eswitch_load_vport(esw, MLX5_VPORT_ECPF, enabled_events);
1207 if (mlx5_core_ec_sriov_enabled(esw->dev)) {
1208 ret = mlx5_eswitch_load_ec_vf_vports(esw, esw->esw_funcs.num_ec_vfs,
1215 /* Enable VF vports */
1216 ret = mlx5_eswitch_load_vf_vports(esw, esw->esw_funcs.num_vfs,
1223 if (mlx5_core_ec_sriov_enabled(esw->dev))
1224 mlx5_eswitch_unload_ec_vf_vports(esw, esw->esw_funcs.num_ec_vfs);
1226 if (mlx5_ecpf_vport_exists(esw->dev))
1227 mlx5_eswitch_unload_vport(esw, MLX5_VPORT_ECPF);
1229 host_pf_disable_hca(esw->dev);
1231 mlx5_eswitch_unload_vport(esw, MLX5_VPORT_PF);
1235 /* mlx5_eswitch_disable_pf_vf_vports() disables vports of PF, ECPF and VFs
1236 * whichever are previously enabled on the eswitch.
1238 void mlx5_eswitch_disable_pf_vf_vports(struct mlx5_eswitch *esw)
1240 mlx5_eswitch_unload_vf_vports(esw, esw->esw_funcs.num_vfs);
1242 if (mlx5_ecpf_vport_exists(esw->dev)) {
1243 if (mlx5_core_ec_sriov_enabled(esw->dev))
1244 mlx5_eswitch_unload_ec_vf_vports(esw, esw->esw_funcs.num_vfs);
1245 mlx5_eswitch_unload_vport(esw, MLX5_VPORT_ECPF);
1248 host_pf_disable_hca(esw->dev);
1249 mlx5_eswitch_unload_vport(esw, MLX5_VPORT_PF);
1252 static void mlx5_eswitch_get_devlink_param(struct mlx5_eswitch *esw)
1254 struct devlink *devlink = priv_to_devlink(esw->dev);
1255 union devlink_param_value val;
1258 err = devl_param_driverinit_value_get(devlink,
1259 MLX5_DEVLINK_PARAM_ID_ESW_LARGE_GROUP_NUM,
1262 esw->params.large_group_num = val.vu32;
1265 "Devlink can't get param fdb_large_groups, uses default (%d).\n",
1266 ESW_OFFLOADS_DEFAULT_NUM_GROUPS);
1267 esw->params.large_group_num = ESW_OFFLOADS_DEFAULT_NUM_GROUPS;
1272 mlx5_eswitch_update_num_of_vfs(struct mlx5_eswitch *esw, int num_vfs)
1279 if (!mlx5_core_is_ecpf_esw_manager(esw->dev)) {
1280 esw->esw_funcs.num_vfs = num_vfs;
1284 out = mlx5_esw_query_functions(esw->dev);
1288 esw->esw_funcs.num_vfs = MLX5_GET(query_esw_functions_out, out,
1289 host_params_context.host_num_of_vfs);
1290 if (mlx5_core_ec_sriov_enabled(esw->dev))
1291 esw->esw_funcs.num_ec_vfs = num_vfs;
1296 static void mlx5_esw_mode_change_notify(struct mlx5_eswitch *esw, u16 mode)
1298 struct mlx5_esw_event_info info = {};
1300 info.new_mode = mode;
1302 blocking_notifier_call_chain(&esw->n_head, 0, &info);
1305 static int mlx5_esw_acls_ns_init(struct mlx5_eswitch *esw)
1307 struct mlx5_core_dev *dev = esw->dev;
1311 if (esw->flags & MLX5_ESWITCH_VPORT_ACL_NS_CREATED)
1314 total_vports = mlx5_eswitch_get_total_vports(dev);
1316 if (MLX5_CAP_ESW_EGRESS_ACL(dev, ft_support)) {
1317 err = mlx5_fs_egress_acls_init(dev, total_vports);
1321 esw_warn(dev, "egress ACL is not supported by FW\n");
1324 if (MLX5_CAP_ESW_INGRESS_ACL(dev, ft_support)) {
1325 err = mlx5_fs_ingress_acls_init(dev, total_vports);
1329 esw_warn(dev, "ingress ACL is not supported by FW\n");
1331 esw->flags |= MLX5_ESWITCH_VPORT_ACL_NS_CREATED;
1335 if (MLX5_CAP_ESW_EGRESS_ACL(dev, ft_support))
1336 mlx5_fs_egress_acls_cleanup(dev);
1340 static void mlx5_esw_acls_ns_cleanup(struct mlx5_eswitch *esw)
1342 struct mlx5_core_dev *dev = esw->dev;
1344 esw->flags &= ~MLX5_ESWITCH_VPORT_ACL_NS_CREATED;
1345 if (MLX5_CAP_ESW_INGRESS_ACL(dev, ft_support))
1346 mlx5_fs_ingress_acls_cleanup(dev);
1347 if (MLX5_CAP_ESW_EGRESS_ACL(dev, ft_support))
1348 mlx5_fs_egress_acls_cleanup(dev);
1352 * mlx5_eswitch_enable_locked - Enable eswitch
1353 * @esw: Pointer to eswitch
1354 * @num_vfs: Enable eswitch for given number of VFs. This is optional.
1355 * Valid value are 0, > 0 and MLX5_ESWITCH_IGNORE_NUM_VFS.
1356 * Caller should pass num_vfs > 0 when enabling eswitch for
1357 * vf vports. Caller should pass num_vfs = 0, when eswitch
1358 * is enabled without sriov VFs or when caller
1359 * is unaware of the sriov state of the host PF on ECPF based
1360 * eswitch. Caller should pass < 0 when num_vfs should be
1361 * completely ignored. This is typically the case when eswitch
1362 * is enabled without sriov regardless of PF/ECPF system.
1363 * mlx5_eswitch_enable_locked() Enables eswitch in either legacy or offloads
1364 * mode. If num_vfs >=0 is provided, it setup VF related eswitch vports.
1365 * It returns 0 on success or error code on failure.
1367 int mlx5_eswitch_enable_locked(struct mlx5_eswitch *esw, int num_vfs)
1371 lockdep_assert_held(&esw->mode_lock);
1373 if (!MLX5_CAP_ESW_FLOWTABLE_FDB(esw->dev, ft_support)) {
1374 esw_warn(esw->dev, "FDB is not supported, aborting ...\n");
1378 mlx5_eswitch_get_devlink_param(esw);
1380 err = mlx5_esw_acls_ns_init(esw);
1384 mlx5_eswitch_update_num_of_vfs(esw, num_vfs);
1386 if (esw->mode == MLX5_ESWITCH_LEGACY) {
1387 err = esw_legacy_enable(esw);
1389 mlx5_rescan_drivers(esw->dev);
1390 err = esw_offloads_enable(esw);
1396 esw->fdb_table.flags |= MLX5_ESW_FDB_CREATED;
1398 mlx5_eswitch_event_handlers_register(esw);
1400 esw_info(esw->dev, "Enable: mode(%s), nvfs(%d), necvfs(%d), active vports(%d)\n",
1401 esw->mode == MLX5_ESWITCH_LEGACY ? "LEGACY" : "OFFLOADS",
1402 esw->esw_funcs.num_vfs, esw->esw_funcs.num_ec_vfs, esw->enabled_vports);
1404 mlx5_esw_mode_change_notify(esw, esw->mode);
1409 mlx5_esw_acls_ns_cleanup(esw);
1414 * mlx5_eswitch_enable - Enable eswitch
1415 * @esw: Pointer to eswitch
1416 * @num_vfs: Enable eswitch switch for given number of VFs.
1417 * Caller must pass num_vfs > 0 when enabling eswitch for
1419 * mlx5_eswitch_enable() returns 0 on success or error code on failure.
1421 int mlx5_eswitch_enable(struct mlx5_eswitch *esw, int num_vfs)
1426 if (!mlx5_esw_allowed(esw))
1429 devl_assert_locked(priv_to_devlink(esw->dev));
1431 toggle_lag = !mlx5_esw_is_fdb_created(esw);
1434 mlx5_lag_disable_change(esw->dev);
1436 down_write(&esw->mode_lock);
1437 if (!mlx5_esw_is_fdb_created(esw)) {
1438 ret = mlx5_eswitch_enable_locked(esw, num_vfs);
1440 enum mlx5_eswitch_vport_event vport_events;
1442 vport_events = (esw->mode == MLX5_ESWITCH_LEGACY) ?
1443 MLX5_LEGACY_SRIOV_VPORT_EVENTS : MLX5_VPORT_UC_ADDR_CHANGE;
1444 /* If this is the ECPF the number of host VFs is managed via the
1445 * eswitch function change event handler, and any num_vfs provided
1446 * here are intended to be EC VFs.
1448 if (!mlx5_core_is_ecpf(esw->dev)) {
1449 ret = mlx5_eswitch_load_vf_vports(esw, num_vfs, vport_events);
1451 esw->esw_funcs.num_vfs = num_vfs;
1452 } else if (mlx5_core_ec_sriov_enabled(esw->dev)) {
1453 ret = mlx5_eswitch_load_ec_vf_vports(esw, num_vfs, vport_events);
1455 esw->esw_funcs.num_ec_vfs = num_vfs;
1459 up_write(&esw->mode_lock);
1462 mlx5_lag_enable_change(esw->dev);
1467 /* When disabling sriov, free driver level resources. */
1468 void mlx5_eswitch_disable_sriov(struct mlx5_eswitch *esw, bool clear_vf)
1470 if (!mlx5_esw_allowed(esw))
1473 devl_assert_locked(priv_to_devlink(esw->dev));
1474 down_write(&esw->mode_lock);
1475 /* If driver is unloaded, this function is called twice by remove_one()
1476 * and mlx5_unload(). Prevent the second call.
1478 if (!esw->esw_funcs.num_vfs && !esw->esw_funcs.num_ec_vfs && !clear_vf)
1481 esw_info(esw->dev, "Unload vfs: mode(%s), nvfs(%d), necvfs(%d), active vports(%d)\n",
1482 esw->mode == MLX5_ESWITCH_LEGACY ? "LEGACY" : "OFFLOADS",
1483 esw->esw_funcs.num_vfs, esw->esw_funcs.num_ec_vfs, esw->enabled_vports);
1485 if (!mlx5_core_is_ecpf(esw->dev)) {
1486 mlx5_eswitch_unload_vf_vports(esw, esw->esw_funcs.num_vfs);
1488 mlx5_eswitch_clear_vf_vports_info(esw);
1489 } else if (mlx5_core_ec_sriov_enabled(esw->dev)) {
1490 mlx5_eswitch_unload_ec_vf_vports(esw, esw->esw_funcs.num_ec_vfs);
1492 mlx5_eswitch_clear_ec_vf_vports_info(esw);
1495 if (esw->mode == MLX5_ESWITCH_OFFLOADS) {
1496 struct devlink *devlink = priv_to_devlink(esw->dev);
1498 devl_rate_nodes_destroy(devlink);
1500 /* Destroy legacy fdb when disabling sriov in legacy mode. */
1501 if (esw->mode == MLX5_ESWITCH_LEGACY)
1502 mlx5_eswitch_disable_locked(esw);
1504 if (!mlx5_core_is_ecpf(esw->dev))
1505 esw->esw_funcs.num_vfs = 0;
1507 esw->esw_funcs.num_ec_vfs = 0;
1510 up_write(&esw->mode_lock);
1513 /* Free resources for corresponding eswitch mode. It is called by devlink
1514 * when changing eswitch mode or modprobe when unloading driver.
1516 void mlx5_eswitch_disable_locked(struct mlx5_eswitch *esw)
1518 struct devlink *devlink = priv_to_devlink(esw->dev);
1520 /* Notify eswitch users that it is exiting from current mode.
1521 * So that it can do necessary cleanup before the eswitch is disabled.
1523 mlx5_esw_mode_change_notify(esw, MLX5_ESWITCH_LEGACY);
1525 mlx5_eswitch_event_handlers_unregister(esw);
1527 esw_info(esw->dev, "Disable: mode(%s), nvfs(%d), necvfs(%d), active vports(%d)\n",
1528 esw->mode == MLX5_ESWITCH_LEGACY ? "LEGACY" : "OFFLOADS",
1529 esw->esw_funcs.num_vfs, esw->esw_funcs.num_ec_vfs, esw->enabled_vports);
1531 if (esw->fdb_table.flags & MLX5_ESW_FDB_CREATED) {
1532 esw->fdb_table.flags &= ~MLX5_ESW_FDB_CREATED;
1533 if (esw->mode == MLX5_ESWITCH_OFFLOADS)
1534 esw_offloads_disable(esw);
1535 else if (esw->mode == MLX5_ESWITCH_LEGACY)
1536 esw_legacy_disable(esw);
1537 mlx5_esw_acls_ns_cleanup(esw);
1540 if (esw->mode == MLX5_ESWITCH_OFFLOADS)
1541 devl_rate_nodes_destroy(devlink);
1544 void mlx5_eswitch_disable(struct mlx5_eswitch *esw)
1546 if (!mlx5_esw_allowed(esw))
1549 devl_assert_locked(priv_to_devlink(esw->dev));
1550 mlx5_lag_disable_change(esw->dev);
1551 down_write(&esw->mode_lock);
1552 mlx5_eswitch_disable_locked(esw);
1553 esw->mode = MLX5_ESWITCH_LEGACY;
1554 up_write(&esw->mode_lock);
1555 mlx5_lag_enable_change(esw->dev);
1558 static int mlx5_query_hca_cap_host_pf(struct mlx5_core_dev *dev, void *out)
1560 u16 opmod = (MLX5_CAP_GENERAL << 1) | (HCA_CAP_OPMOD_GET_MAX & 0x01);
1561 u8 in[MLX5_ST_SZ_BYTES(query_hca_cap_in)] = {};
1563 MLX5_SET(query_hca_cap_in, in, opcode, MLX5_CMD_OP_QUERY_HCA_CAP);
1564 MLX5_SET(query_hca_cap_in, in, op_mod, opmod);
1565 MLX5_SET(query_hca_cap_in, in, function_id, MLX5_VPORT_PF);
1566 MLX5_SET(query_hca_cap_in, in, other_function, true);
1567 return mlx5_cmd_exec_inout(dev, query_hca_cap, in, out);
1570 int mlx5_esw_sf_max_hpf_functions(struct mlx5_core_dev *dev, u16 *max_sfs, u16 *sf_base_id)
1573 int query_out_sz = MLX5_ST_SZ_BYTES(query_hca_cap_out);
1578 if (!mlx5_core_is_ecpf(dev)) {
1583 query_ctx = kzalloc(query_out_sz, GFP_KERNEL);
1587 err = mlx5_query_hca_cap_host_pf(dev, query_ctx);
1591 hca_caps = MLX5_ADDR_OF(query_hca_cap_out, query_ctx, capability);
1592 *max_sfs = MLX5_GET(cmd_hca_cap, hca_caps, max_num_sf);
1593 *sf_base_id = MLX5_GET(cmd_hca_cap, hca_caps, sf_base_id);
1600 static int mlx5_esw_vport_alloc(struct mlx5_eswitch *esw,
1601 int index, u16 vport_num)
1603 struct mlx5_vport *vport;
1606 vport = kzalloc(sizeof(*vport), GFP_KERNEL);
1610 vport->dev = esw->dev;
1611 vport->vport = vport_num;
1612 vport->index = index;
1613 vport->info.link_state = MLX5_VPORT_ADMIN_STATE_AUTO;
1614 INIT_WORK(&vport->vport_change_handler, esw_vport_change_handler);
1615 err = xa_insert(&esw->vports, vport_num, vport, GFP_KERNEL);
1619 esw->total_vports++;
1627 static void mlx5_esw_vport_free(struct mlx5_eswitch *esw, struct mlx5_vport *vport)
1629 xa_erase(&esw->vports, vport->vport);
1633 static void mlx5_esw_vports_cleanup(struct mlx5_eswitch *esw)
1635 struct mlx5_vport *vport;
1638 mlx5_esw_for_each_vport(esw, i, vport)
1639 mlx5_esw_vport_free(esw, vport);
1640 xa_destroy(&esw->vports);
1643 static int mlx5_esw_vports_init(struct mlx5_eswitch *esw)
1645 struct mlx5_core_dev *dev = esw->dev;
1646 u16 max_host_pf_sfs;
1652 xa_init(&esw->vports);
1654 err = mlx5_esw_vport_alloc(esw, idx, MLX5_VPORT_PF);
1657 if (esw->first_host_vport == MLX5_VPORT_PF)
1658 xa_set_mark(&esw->vports, idx, MLX5_ESW_VPT_HOST_FN);
1661 for (i = 0; i < mlx5_core_max_vfs(dev); i++) {
1662 err = mlx5_esw_vport_alloc(esw, idx, idx);
1665 xa_set_mark(&esw->vports, idx, MLX5_ESW_VPT_VF);
1666 xa_set_mark(&esw->vports, idx, MLX5_ESW_VPT_HOST_FN);
1669 base_sf_num = mlx5_sf_start_function_id(dev);
1670 for (i = 0; i < mlx5_sf_max_functions(dev); i++) {
1671 err = mlx5_esw_vport_alloc(esw, idx, base_sf_num + i);
1674 xa_set_mark(&esw->vports, base_sf_num + i, MLX5_ESW_VPT_SF);
1678 err = mlx5_esw_sf_max_hpf_functions(dev, &max_host_pf_sfs, &base_sf_num);
1681 for (i = 0; i < max_host_pf_sfs; i++) {
1682 err = mlx5_esw_vport_alloc(esw, idx, base_sf_num + i);
1685 xa_set_mark(&esw->vports, base_sf_num + i, MLX5_ESW_VPT_SF);
1689 if (mlx5_core_ec_sriov_enabled(esw->dev)) {
1690 int ec_vf_base_num = mlx5_core_ec_vf_vport_base(dev);
1692 for (i = 0; i < mlx5_core_max_ec_vfs(esw->dev); i++) {
1693 err = mlx5_esw_vport_alloc(esw, idx, ec_vf_base_num + i);
1700 if (mlx5_ecpf_vport_exists(dev) ||
1701 mlx5_core_is_ecpf_esw_manager(dev)) {
1702 err = mlx5_esw_vport_alloc(esw, idx, MLX5_VPORT_ECPF);
1707 err = mlx5_esw_vport_alloc(esw, idx, MLX5_VPORT_UPLINK);
1713 mlx5_esw_vports_cleanup(esw);
1717 static int mlx5_devlink_esw_multiport_set(struct devlink *devlink, u32 id,
1718 struct devlink_param_gset_ctx *ctx)
1720 struct mlx5_core_dev *dev = devlink_priv(devlink);
1722 if (!MLX5_ESWITCH_MANAGER(dev))
1726 return mlx5_lag_mpesw_enable(dev);
1728 mlx5_lag_mpesw_disable(dev);
1732 static int mlx5_devlink_esw_multiport_get(struct devlink *devlink, u32 id,
1733 struct devlink_param_gset_ctx *ctx)
1735 struct mlx5_core_dev *dev = devlink_priv(devlink);
1737 ctx->val.vbool = mlx5_lag_is_mpesw(dev);
1741 static const struct devlink_param mlx5_eswitch_params[] = {
1742 DEVLINK_PARAM_DRIVER(MLX5_DEVLINK_PARAM_ID_ESW_MULTIPORT,
1743 "esw_multiport", DEVLINK_PARAM_TYPE_BOOL,
1744 BIT(DEVLINK_PARAM_CMODE_RUNTIME),
1745 mlx5_devlink_esw_multiport_get,
1746 mlx5_devlink_esw_multiport_set, NULL),
1749 int mlx5_eswitch_init(struct mlx5_core_dev *dev)
1751 struct mlx5_eswitch *esw;
1754 if (!MLX5_VPORT_MANAGER(dev) && !MLX5_ESWITCH_MANAGER(dev))
1757 esw = kzalloc(sizeof(*esw), GFP_KERNEL);
1761 err = devl_params_register(priv_to_devlink(dev), mlx5_eswitch_params,
1762 ARRAY_SIZE(mlx5_eswitch_params));
1767 esw->manager_vport = mlx5_eswitch_manager_vport(dev);
1768 esw->first_host_vport = mlx5_eswitch_first_host_vport_num(dev);
1770 esw->debugfs_root = debugfs_create_dir("esw", mlx5_debugfs_get_dev_root(dev));
1771 esw->work_queue = create_singlethread_workqueue("mlx5_esw_wq");
1772 if (!esw->work_queue) {
1777 err = mlx5_esw_vports_init(esw);
1781 err = esw_offloads_init(esw);
1785 mutex_init(&esw->offloads.encap_tbl_lock);
1786 hash_init(esw->offloads.encap_tbl);
1787 mutex_init(&esw->offloads.decap_tbl_lock);
1788 hash_init(esw->offloads.decap_tbl);
1789 mlx5e_mod_hdr_tbl_init(&esw->offloads.mod_hdr);
1790 atomic64_set(&esw->offloads.num_flows, 0);
1791 ida_init(&esw->offloads.vport_metadata_ida);
1792 xa_init_flags(&esw->offloads.vhca_map, XA_FLAGS_ALLOC);
1793 mutex_init(&esw->state_lock);
1794 init_rwsem(&esw->mode_lock);
1795 refcount_set(&esw->qos.refcnt, 0);
1797 esw->enabled_vports = 0;
1798 esw->mode = MLX5_ESWITCH_LEGACY;
1799 esw->offloads.inline_mode = MLX5_INLINE_MODE_NONE;
1800 if (MLX5_CAP_ESW_FLOWTABLE_FDB(dev, reformat) &&
1801 MLX5_CAP_ESW_FLOWTABLE_FDB(dev, decap))
1802 esw->offloads.encap = DEVLINK_ESWITCH_ENCAP_MODE_BASIC;
1804 esw->offloads.encap = DEVLINK_ESWITCH_ENCAP_MODE_NONE;
1805 if (MLX5_ESWITCH_MANAGER(dev) &&
1806 mlx5_esw_vport_match_metadata_supported(esw))
1807 esw->flags |= MLX5_ESWITCH_VPORT_MATCH_METADATA;
1809 dev->priv.eswitch = esw;
1810 BLOCKING_INIT_NOTIFIER_HEAD(&esw->n_head);
1813 "Total vports %d, per vport: max uc(%d) max mc(%d)\n",
1815 MLX5_MAX_UC_PER_VPORT(dev),
1816 MLX5_MAX_MC_PER_VPORT(dev));
1820 mlx5_esw_vports_cleanup(esw);
1822 if (esw->work_queue)
1823 destroy_workqueue(esw->work_queue);
1824 debugfs_remove_recursive(esw->debugfs_root);
1825 devl_params_unregister(priv_to_devlink(dev), mlx5_eswitch_params,
1826 ARRAY_SIZE(mlx5_eswitch_params));
1832 void mlx5_eswitch_cleanup(struct mlx5_eswitch *esw)
1837 esw_info(esw->dev, "cleanup\n");
1839 esw->dev->priv.eswitch = NULL;
1840 destroy_workqueue(esw->work_queue);
1841 WARN_ON(refcount_read(&esw->qos.refcnt));
1842 mutex_destroy(&esw->state_lock);
1843 WARN_ON(!xa_empty(&esw->offloads.vhca_map));
1844 xa_destroy(&esw->offloads.vhca_map);
1845 ida_destroy(&esw->offloads.vport_metadata_ida);
1846 mlx5e_mod_hdr_tbl_destroy(&esw->offloads.mod_hdr);
1847 mutex_destroy(&esw->offloads.encap_tbl_lock);
1848 mutex_destroy(&esw->offloads.decap_tbl_lock);
1849 esw_offloads_cleanup(esw);
1850 mlx5_esw_vports_cleanup(esw);
1851 debugfs_remove_recursive(esw->debugfs_root);
1852 devl_params_unregister(priv_to_devlink(esw->dev), mlx5_eswitch_params,
1853 ARRAY_SIZE(mlx5_eswitch_params));
1857 /* Vport Administration */
1859 mlx5_esw_set_vport_mac_locked(struct mlx5_eswitch *esw,
1860 struct mlx5_vport *evport, const u8 *mac)
1862 u16 vport_num = evport->vport;
1866 if (is_multicast_ether_addr(mac))
1869 if (evport->info.spoofchk && !is_valid_ether_addr(mac))
1870 mlx5_core_warn(esw->dev,
1871 "Set invalid MAC while spoofchk is on, vport(%d)\n",
1874 err = mlx5_modify_nic_vport_mac_address(esw->dev, vport_num, mac);
1876 mlx5_core_warn(esw->dev,
1877 "Failed to mlx5_modify_nic_vport_mac vport(%d) err=(%d)\n",
1882 node_guid_gen_from_mac(&node_guid, mac);
1883 err = mlx5_modify_nic_vport_node_guid(esw->dev, vport_num, node_guid);
1885 mlx5_core_warn(esw->dev,
1886 "Failed to set vport %d node guid, err = %d. RDMA_CM will not function properly for this VF.\n",
1889 ether_addr_copy(evport->info.mac, mac);
1890 evport->info.node_guid = node_guid;
1891 if (evport->enabled && esw->mode == MLX5_ESWITCH_LEGACY)
1892 err = esw_acl_ingress_lgcy_setup(esw, evport);
1897 int mlx5_eswitch_set_vport_mac(struct mlx5_eswitch *esw,
1898 u16 vport, const u8 *mac)
1900 struct mlx5_vport *evport = mlx5_eswitch_get_vport(esw, vport);
1904 return PTR_ERR(evport);
1906 mutex_lock(&esw->state_lock);
1907 err = mlx5_esw_set_vport_mac_locked(esw, evport, mac);
1908 mutex_unlock(&esw->state_lock);
1912 static bool mlx5_esw_check_port_type(struct mlx5_eswitch *esw, u16 vport_num, xa_mark_t mark)
1914 return xa_get_mark(&esw->vports, vport_num, mark);
1917 bool mlx5_eswitch_is_vf_vport(struct mlx5_eswitch *esw, u16 vport_num)
1919 return mlx5_esw_check_port_type(esw, vport_num, MLX5_ESW_VPT_VF);
1922 bool mlx5_esw_is_sf_vport(struct mlx5_eswitch *esw, u16 vport_num)
1924 return mlx5_esw_check_port_type(esw, vport_num, MLX5_ESW_VPT_SF);
1927 int mlx5_eswitch_set_vport_state(struct mlx5_eswitch *esw,
1928 u16 vport, int link_state)
1930 struct mlx5_vport *evport = mlx5_eswitch_get_vport(esw, vport);
1931 int opmod = MLX5_VPORT_STATE_OP_MOD_ESW_VPORT;
1932 int other_vport = 1;
1935 if (!mlx5_esw_allowed(esw))
1938 return PTR_ERR(evport);
1940 if (vport == MLX5_VPORT_UPLINK) {
1941 opmod = MLX5_VPORT_STATE_OP_MOD_UPLINK;
1945 mutex_lock(&esw->state_lock);
1946 if (esw->mode != MLX5_ESWITCH_LEGACY) {
1951 err = mlx5_modify_vport_admin_state(esw->dev, opmod, vport, other_vport, link_state);
1953 mlx5_core_warn(esw->dev, "Failed to set vport %d link state, opmod = %d, err = %d",
1958 evport->info.link_state = link_state;
1961 mutex_unlock(&esw->state_lock);
1965 int mlx5_eswitch_get_vport_config(struct mlx5_eswitch *esw,
1966 u16 vport, struct ifla_vf_info *ivi)
1968 struct mlx5_vport *evport = mlx5_eswitch_get_vport(esw, vport);
1971 return PTR_ERR(evport);
1973 memset(ivi, 0, sizeof(*ivi));
1974 ivi->vf = vport - 1;
1976 mutex_lock(&esw->state_lock);
1977 ether_addr_copy(ivi->mac, evport->info.mac);
1978 ivi->linkstate = evport->info.link_state;
1979 ivi->vlan = evport->info.vlan;
1980 ivi->qos = evport->info.qos;
1981 ivi->spoofchk = evport->info.spoofchk;
1982 ivi->trusted = evport->info.trusted;
1983 if (evport->qos.enabled) {
1984 ivi->min_tx_rate = evport->qos.min_rate;
1985 ivi->max_tx_rate = evport->qos.max_rate;
1987 mutex_unlock(&esw->state_lock);
1992 int __mlx5_eswitch_set_vport_vlan(struct mlx5_eswitch *esw,
1993 u16 vport, u16 vlan, u8 qos, u8 set_flags)
1995 struct mlx5_vport *evport = mlx5_eswitch_get_vport(esw, vport);
1996 bool vst_mode_steering = esw_vst_mode_is_steering(esw);
2000 return PTR_ERR(evport);
2001 if (vlan > 4095 || qos > 7)
2004 if (esw->mode == MLX5_ESWITCH_OFFLOADS || !vst_mode_steering) {
2005 err = modify_esw_vport_cvlan(esw->dev, vport, vlan, qos, set_flags);
2010 evport->info.vlan = vlan;
2011 evport->info.qos = qos;
2012 if (evport->enabled && esw->mode == MLX5_ESWITCH_LEGACY) {
2013 err = esw_acl_ingress_lgcy_setup(esw, evport);
2016 err = esw_acl_egress_lgcy_setup(esw, evport);
2022 int mlx5_eswitch_get_vport_stats(struct mlx5_eswitch *esw,
2024 struct ifla_vf_stats *vf_stats)
2026 struct mlx5_vport *vport = mlx5_eswitch_get_vport(esw, vport_num);
2027 int outlen = MLX5_ST_SZ_BYTES(query_vport_counter_out);
2028 u32 in[MLX5_ST_SZ_DW(query_vport_counter_in)] = {};
2029 struct mlx5_vport_drop_stats stats = {};
2034 return PTR_ERR(vport);
2036 out = kvzalloc(outlen, GFP_KERNEL);
2040 MLX5_SET(query_vport_counter_in, in, opcode,
2041 MLX5_CMD_OP_QUERY_VPORT_COUNTER);
2042 MLX5_SET(query_vport_counter_in, in, op_mod, 0);
2043 MLX5_SET(query_vport_counter_in, in, vport_number, vport->vport);
2044 MLX5_SET(query_vport_counter_in, in, other_vport, 1);
2046 err = mlx5_cmd_exec_inout(esw->dev, query_vport_counter, in, out);
2050 #define MLX5_GET_CTR(p, x) \
2051 MLX5_GET64(query_vport_counter_out, p, x)
2053 memset(vf_stats, 0, sizeof(*vf_stats));
2054 vf_stats->rx_packets =
2055 MLX5_GET_CTR(out, received_eth_unicast.packets) +
2056 MLX5_GET_CTR(out, received_ib_unicast.packets) +
2057 MLX5_GET_CTR(out, received_eth_multicast.packets) +
2058 MLX5_GET_CTR(out, received_ib_multicast.packets) +
2059 MLX5_GET_CTR(out, received_eth_broadcast.packets);
2061 vf_stats->rx_bytes =
2062 MLX5_GET_CTR(out, received_eth_unicast.octets) +
2063 MLX5_GET_CTR(out, received_ib_unicast.octets) +
2064 MLX5_GET_CTR(out, received_eth_multicast.octets) +
2065 MLX5_GET_CTR(out, received_ib_multicast.octets) +
2066 MLX5_GET_CTR(out, received_eth_broadcast.octets);
2068 vf_stats->tx_packets =
2069 MLX5_GET_CTR(out, transmitted_eth_unicast.packets) +
2070 MLX5_GET_CTR(out, transmitted_ib_unicast.packets) +
2071 MLX5_GET_CTR(out, transmitted_eth_multicast.packets) +
2072 MLX5_GET_CTR(out, transmitted_ib_multicast.packets) +
2073 MLX5_GET_CTR(out, transmitted_eth_broadcast.packets);
2075 vf_stats->tx_bytes =
2076 MLX5_GET_CTR(out, transmitted_eth_unicast.octets) +
2077 MLX5_GET_CTR(out, transmitted_ib_unicast.octets) +
2078 MLX5_GET_CTR(out, transmitted_eth_multicast.octets) +
2079 MLX5_GET_CTR(out, transmitted_ib_multicast.octets) +
2080 MLX5_GET_CTR(out, transmitted_eth_broadcast.octets);
2082 vf_stats->multicast =
2083 MLX5_GET_CTR(out, received_eth_multicast.packets) +
2084 MLX5_GET_CTR(out, received_ib_multicast.packets);
2086 vf_stats->broadcast =
2087 MLX5_GET_CTR(out, received_eth_broadcast.packets);
2089 err = mlx5_esw_query_vport_drop_stats(esw->dev, vport, &stats);
2092 vf_stats->rx_dropped = stats.rx_dropped;
2093 vf_stats->tx_dropped = stats.tx_dropped;
2100 u8 mlx5_eswitch_mode(const struct mlx5_core_dev *dev)
2102 struct mlx5_eswitch *esw = dev->priv.eswitch;
2104 return mlx5_esw_allowed(esw) ? esw->mode : MLX5_ESWITCH_LEGACY;
2106 EXPORT_SYMBOL_GPL(mlx5_eswitch_mode);
2108 enum devlink_eswitch_encap_mode
2109 mlx5_eswitch_get_encap_mode(const struct mlx5_core_dev *dev)
2111 struct mlx5_eswitch *esw;
2113 esw = dev->priv.eswitch;
2114 return (mlx5_eswitch_mode(dev) == MLX5_ESWITCH_OFFLOADS) ? esw->offloads.encap :
2115 DEVLINK_ESWITCH_ENCAP_MODE_NONE;
2117 EXPORT_SYMBOL(mlx5_eswitch_get_encap_mode);
2119 bool mlx5_esw_multipath_prereq(struct mlx5_core_dev *dev0,
2120 struct mlx5_core_dev *dev1)
2122 return (dev0->priv.eswitch->mode == MLX5_ESWITCH_OFFLOADS &&
2123 dev1->priv.eswitch->mode == MLX5_ESWITCH_OFFLOADS);
2126 int mlx5_esw_event_notifier_register(struct mlx5_eswitch *esw, struct notifier_block *nb)
2128 return blocking_notifier_chain_register(&esw->n_head, nb);
2131 void mlx5_esw_event_notifier_unregister(struct mlx5_eswitch *esw, struct notifier_block *nb)
2133 blocking_notifier_chain_unregister(&esw->n_head, nb);
2137 * mlx5_esw_hold() - Try to take a read lock on esw mode lock.
2138 * @mdev: mlx5 core device.
2140 * Should be called by esw resources callers.
2142 * Return: true on success or false.
2144 bool mlx5_esw_hold(struct mlx5_core_dev *mdev)
2146 struct mlx5_eswitch *esw = mdev->priv.eswitch;
2148 /* e.g. VF doesn't have eswitch so nothing to do */
2149 if (!mlx5_esw_allowed(esw))
2152 if (down_read_trylock(&esw->mode_lock) != 0)
2159 * mlx5_esw_release() - Release a read lock on esw mode lock.
2160 * @mdev: mlx5 core device.
2162 void mlx5_esw_release(struct mlx5_core_dev *mdev)
2164 struct mlx5_eswitch *esw = mdev->priv.eswitch;
2166 if (mlx5_esw_allowed(esw))
2167 up_read(&esw->mode_lock);
2171 * mlx5_esw_get() - Increase esw user count.
2172 * @mdev: mlx5 core device.
2174 void mlx5_esw_get(struct mlx5_core_dev *mdev)
2176 struct mlx5_eswitch *esw = mdev->priv.eswitch;
2178 if (mlx5_esw_allowed(esw))
2179 atomic64_inc(&esw->user_count);
2183 * mlx5_esw_put() - Decrease esw user count.
2184 * @mdev: mlx5 core device.
2186 void mlx5_esw_put(struct mlx5_core_dev *mdev)
2188 struct mlx5_eswitch *esw = mdev->priv.eswitch;
2190 if (mlx5_esw_allowed(esw))
2191 atomic64_dec_if_positive(&esw->user_count);
2195 * mlx5_esw_try_lock() - Take a write lock on esw mode lock.
2196 * @esw: eswitch device.
2198 * Should be called by esw mode change routine.
2201 * * 0 - esw mode if successfully locked and refcount is 0.
2202 * * -EBUSY - refcount is not 0.
2203 * * -EINVAL - In the middle of switching mode or lock is already held.
2205 int mlx5_esw_try_lock(struct mlx5_eswitch *esw)
2207 if (down_write_trylock(&esw->mode_lock) == 0)
2210 if (atomic64_read(&esw->user_count) > 0) {
2211 up_write(&esw->mode_lock);
2219 * mlx5_esw_unlock() - Release write lock on esw mode lock
2220 * @esw: eswitch device.
2222 void mlx5_esw_unlock(struct mlx5_eswitch *esw)
2224 up_write(&esw->mode_lock);
2228 * mlx5_eswitch_get_total_vports - Get total vports of the eswitch
2230 * @dev: Pointer to core device
2232 * mlx5_eswitch_get_total_vports returns total number of eswitch vports.
2234 u16 mlx5_eswitch_get_total_vports(const struct mlx5_core_dev *dev)
2236 struct mlx5_eswitch *esw;
2238 esw = dev->priv.eswitch;
2239 return mlx5_esw_allowed(esw) ? esw->total_vports : 0;
2241 EXPORT_SYMBOL_GPL(mlx5_eswitch_get_total_vports);
2244 * mlx5_eswitch_get_core_dev - Get the mdev device
2245 * @esw : eswitch device.
2247 * Return the mellanox core device which manages the eswitch.
2249 struct mlx5_core_dev *mlx5_eswitch_get_core_dev(struct mlx5_eswitch *esw)
2251 return mlx5_esw_allowed(esw) ? esw->dev : NULL;
2253 EXPORT_SYMBOL(mlx5_eswitch_get_core_dev);