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"
51 #include "en_accel/ipsec.h"
59 /* Vport UC/MC hash node */
61 struct l2addr_node node;
64 struct mlx5_flow_handle *flow_rule;
65 bool mpfs; /* UC MAC was added to MPFs */
66 /* A flag indicating that mac was added due to mc promiscuous vport */
70 static int mlx5_eswitch_check(const struct mlx5_core_dev *dev)
72 if (MLX5_CAP_GEN(dev, port_type) != MLX5_CAP_PORT_TYPE_ETH)
75 if (!MLX5_ESWITCH_MANAGER(dev))
81 static struct mlx5_eswitch *__mlx5_devlink_eswitch_get(struct devlink *devlink, bool check)
83 struct mlx5_core_dev *dev = devlink_priv(devlink);
87 err = mlx5_eswitch_check(dev);
92 return dev->priv.eswitch;
95 struct mlx5_eswitch *__must_check
96 mlx5_devlink_eswitch_get(struct devlink *devlink)
98 return __mlx5_devlink_eswitch_get(devlink, true);
101 struct mlx5_eswitch *mlx5_devlink_eswitch_nocheck_get(struct devlink *devlink)
103 return __mlx5_devlink_eswitch_get(devlink, false);
106 struct mlx5_vport *__must_check
107 mlx5_eswitch_get_vport(struct mlx5_eswitch *esw, u16 vport_num)
109 struct mlx5_vport *vport;
112 return ERR_PTR(-EPERM);
114 vport = xa_load(&esw->vports, vport_num);
116 esw_debug(esw->dev, "vport out of range: num(0x%x)\n", vport_num);
117 return ERR_PTR(-EINVAL);
122 static int arm_vport_context_events_cmd(struct mlx5_core_dev *dev, u16 vport,
125 u32 in[MLX5_ST_SZ_DW(modify_nic_vport_context_in)] = {};
128 MLX5_SET(modify_nic_vport_context_in, in,
129 opcode, MLX5_CMD_OP_MODIFY_NIC_VPORT_CONTEXT);
130 MLX5_SET(modify_nic_vport_context_in, in, field_select.change_event, 1);
131 MLX5_SET(modify_nic_vport_context_in, in, vport_number, vport);
132 if (vport || mlx5_core_is_ecpf(dev))
133 MLX5_SET(modify_nic_vport_context_in, in, other_vport, 1);
134 nic_vport_ctx = MLX5_ADDR_OF(modify_nic_vport_context_in,
135 in, nic_vport_context);
137 MLX5_SET(nic_vport_context, nic_vport_ctx, arm_change_event, 1);
139 if (events_mask & MLX5_VPORT_UC_ADDR_CHANGE)
140 MLX5_SET(nic_vport_context, nic_vport_ctx,
141 event_on_uc_address_change, 1);
142 if (events_mask & MLX5_VPORT_MC_ADDR_CHANGE)
143 MLX5_SET(nic_vport_context, nic_vport_ctx,
144 event_on_mc_address_change, 1);
145 if (events_mask & MLX5_VPORT_PROMISC_CHANGE)
146 MLX5_SET(nic_vport_context, nic_vport_ctx,
147 event_on_promisc_change, 1);
149 return mlx5_cmd_exec_in(dev, modify_nic_vport_context, in);
152 /* E-Switch vport context HW commands */
153 int mlx5_eswitch_modify_esw_vport_context(struct mlx5_core_dev *dev, u16 vport,
154 bool other_vport, void *in)
156 MLX5_SET(modify_esw_vport_context_in, in, opcode,
157 MLX5_CMD_OP_MODIFY_ESW_VPORT_CONTEXT);
158 MLX5_SET(modify_esw_vport_context_in, in, vport_number, vport);
159 MLX5_SET(modify_esw_vport_context_in, in, other_vport, other_vport);
160 return mlx5_cmd_exec_in(dev, modify_esw_vport_context, in);
163 static int modify_esw_vport_cvlan(struct mlx5_core_dev *dev, u16 vport,
164 u16 vlan, u8 qos, u8 set_flags)
166 u32 in[MLX5_ST_SZ_DW(modify_esw_vport_context_in)] = {};
168 if (!MLX5_CAP_ESW(dev, vport_cvlan_strip) ||
169 !MLX5_CAP_ESW(dev, vport_cvlan_insert_if_not_exist))
172 esw_debug(dev, "Set Vport[%d] VLAN %d qos %d set=%x\n",
173 vport, vlan, qos, set_flags);
175 if (set_flags & SET_VLAN_STRIP)
176 MLX5_SET(modify_esw_vport_context_in, in,
177 esw_vport_context.vport_cvlan_strip, 1);
179 if (set_flags & SET_VLAN_INSERT) {
180 if (MLX5_CAP_ESW(dev, vport_cvlan_insert_always)) {
181 /* insert either if vlan exist in packet or not */
182 MLX5_SET(modify_esw_vport_context_in, in,
183 esw_vport_context.vport_cvlan_insert,
184 MLX5_VPORT_CVLAN_INSERT_ALWAYS);
186 /* insert only if no vlan in packet */
187 MLX5_SET(modify_esw_vport_context_in, in,
188 esw_vport_context.vport_cvlan_insert,
189 MLX5_VPORT_CVLAN_INSERT_WHEN_NO_CVLAN);
191 MLX5_SET(modify_esw_vport_context_in, in,
192 esw_vport_context.cvlan_pcp, qos);
193 MLX5_SET(modify_esw_vport_context_in, in,
194 esw_vport_context.cvlan_id, vlan);
197 MLX5_SET(modify_esw_vport_context_in, in,
198 field_select.vport_cvlan_strip, 1);
199 MLX5_SET(modify_esw_vport_context_in, in,
200 field_select.vport_cvlan_insert, 1);
202 return mlx5_eswitch_modify_esw_vport_context(dev, vport, true, in);
206 static struct mlx5_flow_handle *
207 __esw_fdb_set_vport_rule(struct mlx5_eswitch *esw, u16 vport, bool rx_rule,
208 u8 mac_c[ETH_ALEN], u8 mac_v[ETH_ALEN])
210 int match_header = (is_zero_ether_addr(mac_c) ? 0 :
211 MLX5_MATCH_OUTER_HEADERS);
212 struct mlx5_flow_handle *flow_rule = NULL;
213 struct mlx5_flow_act flow_act = {0};
214 struct mlx5_flow_destination dest = {};
215 struct mlx5_flow_spec *spec;
216 void *mv_misc = NULL;
217 void *mc_misc = NULL;
222 match_header |= MLX5_MATCH_MISC_PARAMETERS;
224 spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
228 dmac_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
229 outer_headers.dmac_47_16);
230 dmac_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
231 outer_headers.dmac_47_16);
233 if (match_header & MLX5_MATCH_OUTER_HEADERS) {
234 ether_addr_copy(dmac_v, mac_v);
235 ether_addr_copy(dmac_c, mac_c);
238 if (match_header & MLX5_MATCH_MISC_PARAMETERS) {
239 mv_misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
241 mc_misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
243 MLX5_SET(fte_match_set_misc, mv_misc, source_port, MLX5_VPORT_UPLINK);
244 MLX5_SET_TO_ONES(fte_match_set_misc, mc_misc, source_port);
247 dest.type = MLX5_FLOW_DESTINATION_TYPE_VPORT;
248 dest.vport.num = vport;
251 "\tFDB add rule dmac_v(%pM) dmac_c(%pM) -> vport(%d)\n",
252 dmac_v, dmac_c, vport);
253 spec->match_criteria_enable = match_header;
254 flow_act.action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
256 mlx5_add_flow_rules(esw->fdb_table.legacy.fdb, spec,
257 &flow_act, &dest, 1);
258 if (IS_ERR(flow_rule)) {
260 "FDB: Failed to add flow rule: dmac_v(%pM) dmac_c(%pM) -> vport(%d), err(%ld)\n",
261 dmac_v, dmac_c, vport, PTR_ERR(flow_rule));
269 static struct mlx5_flow_handle *
270 esw_fdb_set_vport_rule(struct mlx5_eswitch *esw, u8 mac[ETH_ALEN], u16 vport)
274 eth_broadcast_addr(mac_c);
275 return __esw_fdb_set_vport_rule(esw, vport, false, mac_c, mac);
278 static struct mlx5_flow_handle *
279 esw_fdb_set_vport_allmulti_rule(struct mlx5_eswitch *esw, u16 vport)
284 eth_zero_addr(mac_c);
285 eth_zero_addr(mac_v);
288 return __esw_fdb_set_vport_rule(esw, vport, false, mac_c, mac_v);
291 static struct mlx5_flow_handle *
292 esw_fdb_set_vport_promisc_rule(struct mlx5_eswitch *esw, u16 vport)
297 eth_zero_addr(mac_c);
298 eth_zero_addr(mac_v);
299 return __esw_fdb_set_vport_rule(esw, vport, true, mac_c, mac_v);
302 /* E-Switch vport UC/MC lists management */
303 typedef int (*vport_addr_action)(struct mlx5_eswitch *esw,
304 struct vport_addr *vaddr);
306 static int esw_add_uc_addr(struct mlx5_eswitch *esw, struct vport_addr *vaddr)
308 u8 *mac = vaddr->node.addr;
309 u16 vport = vaddr->vport;
312 /* Skip mlx5_mpfs_add_mac for eswitch_managers,
313 * it is already done by its netdev in mlx5e_execute_l2_action
315 if (mlx5_esw_is_manager_vport(esw, vport))
318 err = mlx5_mpfs_add_mac(esw->dev, mac);
321 "Failed to add L2 table mac(%pM) for vport(0x%x), err(%d)\n",
328 /* SRIOV is enabled: Forward UC MAC to vport */
329 if (esw->fdb_table.legacy.fdb && esw->mode == MLX5_ESWITCH_LEGACY) {
330 vaddr->flow_rule = esw_fdb_set_vport_rule(esw, mac, vport);
332 esw_debug(esw->dev, "\tADDED UC MAC: vport[%d] %pM fr(%p)\n",
333 vport, mac, vaddr->flow_rule);
339 static int esw_del_uc_addr(struct mlx5_eswitch *esw, struct vport_addr *vaddr)
341 u8 *mac = vaddr->node.addr;
342 u16 vport = vaddr->vport;
345 /* Skip mlx5_mpfs_del_mac for eswitch managers,
346 * it is already done by its netdev in mlx5e_execute_l2_action
348 if (!vaddr->mpfs || mlx5_esw_is_manager_vport(esw, vport))
351 err = mlx5_mpfs_del_mac(esw->dev, mac);
354 "Failed to del L2 table mac(%pM) for vport(%d), err(%d)\n",
359 if (vaddr->flow_rule)
360 mlx5_del_flow_rules(vaddr->flow_rule);
361 vaddr->flow_rule = NULL;
366 static void update_allmulti_vports(struct mlx5_eswitch *esw,
367 struct vport_addr *vaddr,
368 struct esw_mc_addr *esw_mc)
370 u8 *mac = vaddr->node.addr;
371 struct mlx5_vport *vport;
375 mlx5_esw_for_each_vport(esw, i, vport) {
376 struct hlist_head *vport_hash = vport->mc_list;
377 struct vport_addr *iter_vaddr =
378 l2addr_hash_find(vport_hash,
381 vport_num = vport->vport;
382 if (IS_ERR_OR_NULL(vport->allmulti_rule) ||
383 vaddr->vport == vport_num)
385 switch (vaddr->action) {
386 case MLX5_ACTION_ADD:
389 iter_vaddr = l2addr_hash_add(vport_hash, mac,
394 "ALL-MULTI: Failed to add MAC(%pM) to vport[%d] DB\n",
398 iter_vaddr->vport = vport_num;
399 iter_vaddr->flow_rule =
400 esw_fdb_set_vport_rule(esw,
403 iter_vaddr->mc_promisc = true;
405 case MLX5_ACTION_DEL:
408 mlx5_del_flow_rules(iter_vaddr->flow_rule);
409 l2addr_hash_del(iter_vaddr);
415 static int esw_add_mc_addr(struct mlx5_eswitch *esw, struct vport_addr *vaddr)
417 struct hlist_head *hash = esw->mc_table;
418 struct esw_mc_addr *esw_mc;
419 u8 *mac = vaddr->node.addr;
420 u16 vport = vaddr->vport;
422 if (!esw->fdb_table.legacy.fdb)
425 esw_mc = l2addr_hash_find(hash, mac, struct esw_mc_addr);
429 esw_mc = l2addr_hash_add(hash, mac, struct esw_mc_addr, GFP_KERNEL);
433 esw_mc->uplink_rule = /* Forward MC MAC to Uplink */
434 esw_fdb_set_vport_rule(esw, mac, MLX5_VPORT_UPLINK);
436 /* Add this multicast mac to all the mc promiscuous vports */
437 update_allmulti_vports(esw, vaddr, esw_mc);
440 /* If the multicast mac is added as a result of mc promiscuous vport,
441 * don't increment the multicast ref count
443 if (!vaddr->mc_promisc)
446 /* Forward MC MAC to vport */
447 vaddr->flow_rule = esw_fdb_set_vport_rule(esw, mac, vport);
449 "\tADDED MC MAC: vport[%d] %pM fr(%p) refcnt(%d) uplinkfr(%p)\n",
450 vport, mac, vaddr->flow_rule,
451 esw_mc->refcnt, esw_mc->uplink_rule);
455 static int esw_del_mc_addr(struct mlx5_eswitch *esw, struct vport_addr *vaddr)
457 struct hlist_head *hash = esw->mc_table;
458 struct esw_mc_addr *esw_mc;
459 u8 *mac = vaddr->node.addr;
460 u16 vport = vaddr->vport;
462 if (!esw->fdb_table.legacy.fdb)
465 esw_mc = l2addr_hash_find(hash, mac, struct esw_mc_addr);
468 "Failed to find eswitch MC addr for MAC(%pM) vport(%d)",
473 "\tDELETE MC MAC: vport[%d] %pM fr(%p) refcnt(%d) uplinkfr(%p)\n",
474 vport, mac, vaddr->flow_rule, esw_mc->refcnt,
475 esw_mc->uplink_rule);
477 if (vaddr->flow_rule)
478 mlx5_del_flow_rules(vaddr->flow_rule);
479 vaddr->flow_rule = NULL;
481 /* If the multicast mac is added as a result of mc promiscuous vport,
482 * don't decrement the multicast ref count.
484 if (vaddr->mc_promisc || (--esw_mc->refcnt > 0))
487 /* Remove this multicast mac from all the mc promiscuous vports */
488 update_allmulti_vports(esw, vaddr, esw_mc);
490 if (esw_mc->uplink_rule)
491 mlx5_del_flow_rules(esw_mc->uplink_rule);
493 l2addr_hash_del(esw_mc);
497 /* Apply vport UC/MC list to HW l2 table and FDB table */
498 static void esw_apply_vport_addr_list(struct mlx5_eswitch *esw,
499 struct mlx5_vport *vport, int list_type)
501 bool is_uc = list_type == MLX5_NVPRT_LIST_TYPE_UC;
502 vport_addr_action vport_addr_add;
503 vport_addr_action vport_addr_del;
504 struct vport_addr *addr;
505 struct l2addr_node *node;
506 struct hlist_head *hash;
507 struct hlist_node *tmp;
510 vport_addr_add = is_uc ? esw_add_uc_addr :
512 vport_addr_del = is_uc ? esw_del_uc_addr :
515 hash = is_uc ? vport->uc_list : vport->mc_list;
516 for_each_l2hash_node(node, tmp, hash, hi) {
517 addr = container_of(node, struct vport_addr, node);
518 switch (addr->action) {
519 case MLX5_ACTION_ADD:
520 vport_addr_add(esw, addr);
521 addr->action = MLX5_ACTION_NONE;
523 case MLX5_ACTION_DEL:
524 vport_addr_del(esw, addr);
525 l2addr_hash_del(addr);
531 /* Sync vport UC/MC list from vport context */
532 static void esw_update_vport_addr_list(struct mlx5_eswitch *esw,
533 struct mlx5_vport *vport, int list_type)
535 bool is_uc = list_type == MLX5_NVPRT_LIST_TYPE_UC;
536 u8 (*mac_list)[ETH_ALEN];
537 struct l2addr_node *node;
538 struct vport_addr *addr;
539 struct hlist_head *hash;
540 struct hlist_node *tmp;
546 size = is_uc ? MLX5_MAX_UC_PER_VPORT(esw->dev) :
547 MLX5_MAX_MC_PER_VPORT(esw->dev);
549 mac_list = kcalloc(size, ETH_ALEN, GFP_KERNEL);
553 hash = is_uc ? vport->uc_list : vport->mc_list;
555 for_each_l2hash_node(node, tmp, hash, hi) {
556 addr = container_of(node, struct vport_addr, node);
557 addr->action = MLX5_ACTION_DEL;
563 err = mlx5_query_nic_vport_mac_list(esw->dev, vport->vport, list_type,
567 esw_debug(esw->dev, "vport[%d] context update %s list size (%d)\n",
568 vport->vport, is_uc ? "UC" : "MC", size);
570 for (i = 0; i < size; i++) {
571 if (is_uc && !is_valid_ether_addr(mac_list[i]))
574 if (!is_uc && !is_multicast_ether_addr(mac_list[i]))
577 addr = l2addr_hash_find(hash, mac_list[i], struct vport_addr);
579 addr->action = MLX5_ACTION_NONE;
580 /* If this mac was previously added because of allmulti
581 * promiscuous rx mode, its now converted to be original
584 if (addr->mc_promisc) {
585 struct esw_mc_addr *esw_mc =
586 l2addr_hash_find(esw->mc_table,
591 "Failed to MAC(%pM) in mcast DB\n",
596 addr->mc_promisc = false;
601 addr = l2addr_hash_add(hash, mac_list[i], struct vport_addr,
605 "Failed to add MAC(%pM) to vport[%d] DB\n",
606 mac_list[i], vport->vport);
609 addr->vport = vport->vport;
610 addr->action = MLX5_ACTION_ADD;
616 /* Sync vport UC/MC list from vport context
617 * Must be called after esw_update_vport_addr_list
619 static void esw_update_vport_mc_promisc(struct mlx5_eswitch *esw,
620 struct mlx5_vport *vport)
622 struct l2addr_node *node;
623 struct vport_addr *addr;
624 struct hlist_head *hash;
625 struct hlist_node *tmp;
628 hash = vport->mc_list;
630 for_each_l2hash_node(node, tmp, esw->mc_table, hi) {
631 u8 *mac = node->addr;
633 addr = l2addr_hash_find(hash, mac, struct vport_addr);
635 if (addr->action == MLX5_ACTION_DEL)
636 addr->action = MLX5_ACTION_NONE;
639 addr = l2addr_hash_add(hash, mac, struct vport_addr,
643 "Failed to add allmulti MAC(%pM) to vport[%d] DB\n",
647 addr->vport = vport->vport;
648 addr->action = MLX5_ACTION_ADD;
649 addr->mc_promisc = true;
653 /* Apply vport rx mode to HW FDB table */
654 static void esw_apply_vport_rx_mode(struct mlx5_eswitch *esw,
655 struct mlx5_vport *vport,
656 bool promisc, bool mc_promisc)
658 struct esw_mc_addr *allmulti_addr = &esw->mc_promisc;
660 if (IS_ERR_OR_NULL(vport->allmulti_rule) != mc_promisc)
664 vport->allmulti_rule =
665 esw_fdb_set_vport_allmulti_rule(esw, vport->vport);
666 if (!allmulti_addr->uplink_rule)
667 allmulti_addr->uplink_rule =
668 esw_fdb_set_vport_allmulti_rule(esw,
670 allmulti_addr->refcnt++;
671 } else if (vport->allmulti_rule) {
672 mlx5_del_flow_rules(vport->allmulti_rule);
673 vport->allmulti_rule = NULL;
675 if (--allmulti_addr->refcnt > 0)
678 if (allmulti_addr->uplink_rule)
679 mlx5_del_flow_rules(allmulti_addr->uplink_rule);
680 allmulti_addr->uplink_rule = NULL;
684 if (IS_ERR_OR_NULL(vport->promisc_rule) != promisc)
688 vport->promisc_rule =
689 esw_fdb_set_vport_promisc_rule(esw, vport->vport);
690 } else if (vport->promisc_rule) {
691 mlx5_del_flow_rules(vport->promisc_rule);
692 vport->promisc_rule = NULL;
696 /* Sync vport rx mode from vport context */
697 static void esw_update_vport_rx_mode(struct mlx5_eswitch *esw,
698 struct mlx5_vport *vport)
705 err = mlx5_query_nic_vport_promisc(esw->dev,
712 esw_debug(esw->dev, "vport[%d] context update rx mode promisc_all=%d, all_multi=%d\n",
713 vport->vport, promisc_all, promisc_mc);
715 if (!vport->info.trusted || !vport->enabled) {
721 esw_apply_vport_rx_mode(esw, vport, promisc_all,
722 (promisc_all || promisc_mc));
725 void esw_vport_change_handle_locked(struct mlx5_vport *vport)
727 struct mlx5_core_dev *dev = vport->dev;
728 struct mlx5_eswitch *esw = dev->priv.eswitch;
731 if (!MLX5_CAP_GEN(dev, log_max_l2_table))
734 mlx5_query_nic_vport_mac_address(dev, vport->vport, true, mac);
735 esw_debug(dev, "vport[%d] Context Changed: perm mac: %pM\n",
738 if (vport->enabled_events & MLX5_VPORT_UC_ADDR_CHANGE) {
739 esw_update_vport_addr_list(esw, vport, MLX5_NVPRT_LIST_TYPE_UC);
740 esw_apply_vport_addr_list(esw, vport, MLX5_NVPRT_LIST_TYPE_UC);
743 if (vport->enabled_events & MLX5_VPORT_MC_ADDR_CHANGE)
744 esw_update_vport_addr_list(esw, vport, MLX5_NVPRT_LIST_TYPE_MC);
746 if (vport->enabled_events & MLX5_VPORT_PROMISC_CHANGE) {
747 esw_update_vport_rx_mode(esw, vport);
748 if (!IS_ERR_OR_NULL(vport->allmulti_rule))
749 esw_update_vport_mc_promisc(esw, vport);
752 if (vport->enabled_events & (MLX5_VPORT_PROMISC_CHANGE | MLX5_VPORT_MC_ADDR_CHANGE))
753 esw_apply_vport_addr_list(esw, vport, MLX5_NVPRT_LIST_TYPE_MC);
755 esw_debug(esw->dev, "vport[%d] Context Changed: Done\n", vport->vport);
757 arm_vport_context_events_cmd(dev, vport->vport,
758 vport->enabled_events);
761 static void esw_vport_change_handler(struct work_struct *work)
763 struct mlx5_vport *vport =
764 container_of(work, struct mlx5_vport, vport_change_handler);
765 struct mlx5_eswitch *esw = vport->dev->priv.eswitch;
767 mutex_lock(&esw->state_lock);
768 esw_vport_change_handle_locked(vport);
769 mutex_unlock(&esw->state_lock);
772 static void node_guid_gen_from_mac(u64 *node_guid, const u8 *mac)
774 ((u8 *)node_guid)[7] = mac[0];
775 ((u8 *)node_guid)[6] = mac[1];
776 ((u8 *)node_guid)[5] = mac[2];
777 ((u8 *)node_guid)[4] = 0xff;
778 ((u8 *)node_guid)[3] = 0xfe;
779 ((u8 *)node_guid)[2] = mac[3];
780 ((u8 *)node_guid)[1] = mac[4];
781 ((u8 *)node_guid)[0] = mac[5];
784 static int esw_vport_setup_acl(struct mlx5_eswitch *esw,
785 struct mlx5_vport *vport)
787 if (esw->mode == MLX5_ESWITCH_LEGACY)
788 return esw_legacy_vport_acl_setup(esw, vport);
790 return esw_vport_create_offloads_acl_tables(esw, vport);
793 static void esw_vport_cleanup_acl(struct mlx5_eswitch *esw,
794 struct mlx5_vport *vport)
796 if (esw->mode == MLX5_ESWITCH_LEGACY)
797 esw_legacy_vport_acl_cleanup(esw, vport);
799 esw_vport_destroy_offloads_acl_tables(esw, vport);
802 static int mlx5_esw_vport_caps_get(struct mlx5_eswitch *esw, struct mlx5_vport *vport)
804 int query_out_sz = MLX5_ST_SZ_BYTES(query_hca_cap_out);
809 if (!MLX5_CAP_GEN(esw->dev, vhca_resource_manager))
812 query_ctx = kzalloc(query_out_sz, GFP_KERNEL);
816 err = mlx5_vport_get_other_func_cap(esw->dev, vport->vport, query_ctx,
821 hca_caps = MLX5_ADDR_OF(query_hca_cap_out, query_ctx, capability);
822 vport->info.roce_enabled = MLX5_GET(cmd_hca_cap, hca_caps, roce);
824 if (!MLX5_CAP_GEN_MAX(esw->dev, hca_cap_2))
827 memset(query_ctx, 0, query_out_sz);
828 err = mlx5_vport_get_other_func_cap(esw->dev, vport->vport, query_ctx,
833 hca_caps = MLX5_ADDR_OF(query_hca_cap_out, query_ctx, capability);
834 vport->info.mig_enabled = MLX5_GET(cmd_hca_cap_2, hca_caps, migratable);
836 err = mlx5_esw_ipsec_vf_offload_get(esw->dev, vport);
842 static int esw_vport_setup(struct mlx5_eswitch *esw, struct mlx5_vport *vport)
844 bool vst_mode_steering = esw_vst_mode_is_steering(esw);
845 u16 vport_num = vport->vport;
849 err = esw_vport_setup_acl(esw, vport);
853 if (mlx5_esw_is_manager_vport(esw, vport_num))
856 err = mlx5_esw_vport_caps_get(esw, vport);
860 mlx5_modify_vport_admin_state(esw->dev,
861 MLX5_VPORT_STATE_OP_MOD_ESW_VPORT,
863 vport->info.link_state);
865 /* Host PF has its own mac/guid. */
867 mlx5_modify_nic_vport_mac_address(esw->dev, vport_num,
869 mlx5_modify_nic_vport_node_guid(esw->dev, vport_num,
870 vport->info.node_guid);
873 flags = (vport->info.vlan || vport->info.qos) ?
874 SET_VLAN_STRIP | SET_VLAN_INSERT : 0;
875 if (esw->mode == MLX5_ESWITCH_OFFLOADS || !vst_mode_steering)
876 modify_esw_vport_cvlan(esw->dev, vport_num, vport->info.vlan,
877 vport->info.qos, flags);
882 esw_vport_cleanup_acl(esw, vport);
886 /* Don't cleanup vport->info, it's needed to restore vport configuration */
887 static void esw_vport_cleanup(struct mlx5_eswitch *esw, struct mlx5_vport *vport)
889 u16 vport_num = vport->vport;
891 if (!mlx5_esw_is_manager_vport(esw, vport_num))
892 mlx5_modify_vport_admin_state(esw->dev,
893 MLX5_VPORT_STATE_OP_MOD_ESW_VPORT,
895 MLX5_VPORT_ADMIN_STATE_DOWN);
897 mlx5_esw_qos_vport_disable(esw, vport);
898 esw_vport_cleanup_acl(esw, vport);
901 int mlx5_esw_vport_enable(struct mlx5_eswitch *esw, struct mlx5_vport *vport,
902 enum mlx5_eswitch_vport_event enabled_events)
904 u16 vport_num = vport->vport;
907 mutex_lock(&esw->state_lock);
908 WARN_ON(vport->enabled);
910 esw_debug(esw->dev, "Enabling VPORT(%d)\n", vport_num);
912 ret = esw_vport_setup(esw, vport);
916 /* Sync with current vport context */
917 vport->enabled_events = enabled_events;
918 vport->enabled = true;
919 if (vport->vport != MLX5_VPORT_PF &&
920 (vport->info.ipsec_crypto_enabled || vport->info.ipsec_packet_enabled))
921 esw->enabled_ipsec_vf_count++;
923 /* Esw manager is trusted by default. Host PF (vport 0) is trusted as well
924 * in smartNIC as it's a vport group manager.
926 if (mlx5_esw_is_manager_vport(esw, vport_num) ||
927 (!vport_num && mlx5_core_is_ecpf(esw->dev)))
928 vport->info.trusted = true;
930 if (!mlx5_esw_is_manager_vport(esw, vport_num) &&
931 MLX5_CAP_GEN(esw->dev, vhca_resource_manager)) {
932 ret = mlx5_esw_vport_vhca_id_set(esw, vport_num);
934 goto err_vhca_mapping;
937 /* External controller host PF has factory programmed MAC.
938 * Read it from the device.
940 if (mlx5_core_is_ecpf(esw->dev) && vport_num == MLX5_VPORT_PF)
941 mlx5_query_nic_vport_mac_address(esw->dev, vport_num, true, vport->info.mac);
943 esw_vport_change_handle_locked(vport);
945 esw->enabled_vports++;
946 esw_debug(esw->dev, "Enabled VPORT(%d)\n", vport_num);
948 mutex_unlock(&esw->state_lock);
952 esw_vport_cleanup(esw, vport);
953 mutex_unlock(&esw->state_lock);
957 void mlx5_esw_vport_disable(struct mlx5_eswitch *esw, struct mlx5_vport *vport)
959 u16 vport_num = vport->vport;
961 mutex_lock(&esw->state_lock);
966 esw_debug(esw->dev, "Disabling vport(%d)\n", vport_num);
967 /* Mark this vport as disabled to discard new events */
968 vport->enabled = false;
970 /* Disable events from this vport */
971 if (MLX5_CAP_GEN(esw->dev, log_max_l2_table))
972 arm_vport_context_events_cmd(esw->dev, vport_num, 0);
974 if (!mlx5_esw_is_manager_vport(esw, vport_num) &&
975 MLX5_CAP_GEN(esw->dev, vhca_resource_manager))
976 mlx5_esw_vport_vhca_id_clear(esw, vport_num);
978 if (vport->vport != MLX5_VPORT_PF &&
979 (vport->info.ipsec_crypto_enabled || vport->info.ipsec_packet_enabled))
980 esw->enabled_ipsec_vf_count--;
982 /* We don't assume VFs will cleanup after themselves.
983 * Calling vport change handler while vport is disabled will cleanup
984 * the vport resources.
986 esw_vport_change_handle_locked(vport);
987 vport->enabled_events = 0;
988 esw_apply_vport_rx_mode(esw, vport, false, false);
989 esw_vport_cleanup(esw, vport);
990 esw->enabled_vports--;
993 mutex_unlock(&esw->state_lock);
996 static int eswitch_vport_event(struct notifier_block *nb,
997 unsigned long type, void *data)
999 struct mlx5_eswitch *esw = mlx5_nb_cof(nb, struct mlx5_eswitch, nb);
1000 struct mlx5_eqe *eqe = data;
1001 struct mlx5_vport *vport;
1004 vport_num = be16_to_cpu(eqe->data.vport_change.vport_num);
1005 vport = mlx5_eswitch_get_vport(esw, vport_num);
1007 queue_work(esw->work_queue, &vport->vport_change_handler);
1012 * mlx5_esw_query_functions - Returns raw output about functions state
1013 * @dev: Pointer to device to query
1015 * mlx5_esw_query_functions() allocates and returns functions changed
1016 * raw output memory pointer from device on success. Otherwise returns ERR_PTR.
1017 * Caller must free the memory using kvfree() when valid pointer is returned.
1019 const u32 *mlx5_esw_query_functions(struct mlx5_core_dev *dev)
1021 int outlen = MLX5_ST_SZ_BYTES(query_esw_functions_out);
1022 u32 in[MLX5_ST_SZ_DW(query_esw_functions_in)] = {};
1026 out = kvzalloc(outlen, GFP_KERNEL);
1028 return ERR_PTR(-ENOMEM);
1030 MLX5_SET(query_esw_functions_in, in, opcode,
1031 MLX5_CMD_OP_QUERY_ESW_FUNCTIONS);
1033 err = mlx5_cmd_exec(dev, in, sizeof(in), out, outlen);
1038 return ERR_PTR(err);
1041 static void mlx5_eswitch_event_handlers_register(struct mlx5_eswitch *esw)
1043 MLX5_NB_INIT(&esw->nb, eswitch_vport_event, NIC_VPORT_CHANGE);
1044 mlx5_eq_notifier_register(esw->dev, &esw->nb);
1046 if (esw->mode == MLX5_ESWITCH_OFFLOADS && mlx5_eswitch_is_funcs_handler(esw->dev)) {
1047 MLX5_NB_INIT(&esw->esw_funcs.nb, mlx5_esw_funcs_changed_handler,
1048 ESW_FUNCTIONS_CHANGED);
1049 mlx5_eq_notifier_register(esw->dev, &esw->esw_funcs.nb);
1053 static void mlx5_eswitch_event_handlers_unregister(struct mlx5_eswitch *esw)
1055 if (esw->mode == MLX5_ESWITCH_OFFLOADS && mlx5_eswitch_is_funcs_handler(esw->dev))
1056 mlx5_eq_notifier_unregister(esw->dev, &esw->esw_funcs.nb);
1058 mlx5_eq_notifier_unregister(esw->dev, &esw->nb);
1060 flush_workqueue(esw->work_queue);
1063 static void mlx5_eswitch_clear_vf_vports_info(struct mlx5_eswitch *esw)
1065 struct mlx5_vport *vport;
1068 mlx5_esw_for_each_vf_vport(esw, i, vport, esw->esw_funcs.num_vfs) {
1069 memset(&vport->qos, 0, sizeof(vport->qos));
1070 memset(&vport->info, 0, sizeof(vport->info));
1071 vport->info.link_state = MLX5_VPORT_ADMIN_STATE_AUTO;
1075 static void mlx5_eswitch_clear_ec_vf_vports_info(struct mlx5_eswitch *esw)
1077 struct mlx5_vport *vport;
1080 mlx5_esw_for_each_ec_vf_vport(esw, i, vport, esw->esw_funcs.num_ec_vfs) {
1081 memset(&vport->qos, 0, sizeof(vport->qos));
1082 memset(&vport->info, 0, sizeof(vport->info));
1083 vport->info.link_state = MLX5_VPORT_ADMIN_STATE_AUTO;
1087 static int mlx5_eswitch_load_vport(struct mlx5_eswitch *esw, struct mlx5_vport *vport,
1088 enum mlx5_eswitch_vport_event enabled_events)
1092 err = mlx5_esw_vport_enable(esw, vport, enabled_events);
1096 err = mlx5_esw_offloads_load_rep(esw, vport);
1103 mlx5_esw_vport_disable(esw, vport);
1107 static void mlx5_eswitch_unload_vport(struct mlx5_eswitch *esw, struct mlx5_vport *vport)
1109 mlx5_esw_offloads_unload_rep(esw, vport);
1110 mlx5_esw_vport_disable(esw, vport);
1113 static int mlx5_eswitch_load_pf_vf_vport(struct mlx5_eswitch *esw, u16 vport_num,
1114 enum mlx5_eswitch_vport_event enabled_events)
1116 struct mlx5_vport *vport;
1119 vport = mlx5_eswitch_get_vport(esw, vport_num);
1121 return PTR_ERR(vport);
1123 err = mlx5_esw_offloads_init_pf_vf_rep(esw, vport);
1127 err = mlx5_eswitch_load_vport(esw, vport, enabled_events);
1133 mlx5_esw_offloads_cleanup_pf_vf_rep(esw, vport);
1137 static void mlx5_eswitch_unload_pf_vf_vport(struct mlx5_eswitch *esw, u16 vport_num)
1139 struct mlx5_vport *vport;
1141 vport = mlx5_eswitch_get_vport(esw, vport_num);
1145 mlx5_eswitch_unload_vport(esw, vport);
1146 mlx5_esw_offloads_cleanup_pf_vf_rep(esw, vport);
1149 int mlx5_eswitch_load_sf_vport(struct mlx5_eswitch *esw, u16 vport_num,
1150 enum mlx5_eswitch_vport_event enabled_events,
1151 struct mlx5_devlink_port *dl_port, u32 controller, u32 sfnum)
1153 struct mlx5_vport *vport;
1156 vport = mlx5_eswitch_get_vport(esw, vport_num);
1158 return PTR_ERR(vport);
1160 err = mlx5_esw_offloads_init_sf_rep(esw, vport, dl_port, controller, sfnum);
1164 err = mlx5_eswitch_load_vport(esw, vport, enabled_events);
1171 mlx5_esw_offloads_cleanup_sf_rep(esw, vport);
1175 void mlx5_eswitch_unload_sf_vport(struct mlx5_eswitch *esw, u16 vport_num)
1177 struct mlx5_vport *vport;
1179 vport = mlx5_eswitch_get_vport(esw, vport_num);
1183 mlx5_eswitch_unload_vport(esw, vport);
1184 mlx5_esw_offloads_cleanup_sf_rep(esw, vport);
1187 void mlx5_eswitch_unload_vf_vports(struct mlx5_eswitch *esw, u16 num_vfs)
1189 struct mlx5_vport *vport;
1192 mlx5_esw_for_each_vf_vport(esw, i, vport, num_vfs) {
1193 if (!vport->enabled)
1195 mlx5_eswitch_unload_pf_vf_vport(esw, vport->vport);
1199 static void mlx5_eswitch_unload_ec_vf_vports(struct mlx5_eswitch *esw,
1202 struct mlx5_vport *vport;
1205 mlx5_esw_for_each_ec_vf_vport(esw, i, vport, num_ec_vfs) {
1206 if (!vport->enabled)
1208 mlx5_eswitch_unload_pf_vf_vport(esw, vport->vport);
1212 int mlx5_eswitch_load_vf_vports(struct mlx5_eswitch *esw, u16 num_vfs,
1213 enum mlx5_eswitch_vport_event enabled_events)
1215 struct mlx5_vport *vport;
1219 mlx5_esw_for_each_vf_vport(esw, i, vport, num_vfs) {
1220 err = mlx5_eswitch_load_pf_vf_vport(esw, vport->vport, enabled_events);
1228 mlx5_eswitch_unload_vf_vports(esw, num_vfs);
1232 static int mlx5_eswitch_load_ec_vf_vports(struct mlx5_eswitch *esw, u16 num_ec_vfs,
1233 enum mlx5_eswitch_vport_event enabled_events)
1235 struct mlx5_vport *vport;
1239 mlx5_esw_for_each_ec_vf_vport(esw, i, vport, num_ec_vfs) {
1240 err = mlx5_eswitch_load_pf_vf_vport(esw, vport->vport, enabled_events);
1248 mlx5_eswitch_unload_ec_vf_vports(esw, num_ec_vfs);
1252 static int host_pf_enable_hca(struct mlx5_core_dev *dev)
1254 if (!mlx5_core_is_ecpf(dev))
1257 /* Once vport and representor are ready, take out the external host PF
1258 * out of initializing state. Enabling HCA clears the iser->initializing
1259 * bit and host PF driver loading can progress.
1261 return mlx5_cmd_host_pf_enable_hca(dev);
1264 static void host_pf_disable_hca(struct mlx5_core_dev *dev)
1266 if (!mlx5_core_is_ecpf(dev))
1269 mlx5_cmd_host_pf_disable_hca(dev);
1272 /* mlx5_eswitch_enable_pf_vf_vports() enables vports of PF, ECPF and VFs
1273 * whichever are present on the eswitch.
1276 mlx5_eswitch_enable_pf_vf_vports(struct mlx5_eswitch *esw,
1277 enum mlx5_eswitch_vport_event enabled_events)
1282 pf_needed = mlx5_core_is_ecpf_esw_manager(esw->dev) ||
1283 esw->mode == MLX5_ESWITCH_LEGACY;
1285 /* Enable PF vport */
1287 ret = mlx5_eswitch_load_pf_vf_vport(esw, MLX5_VPORT_PF,
1293 /* Enable external host PF HCA */
1294 ret = host_pf_enable_hca(esw->dev);
1298 /* Enable ECPF vport */
1299 if (mlx5_ecpf_vport_exists(esw->dev)) {
1300 ret = mlx5_eswitch_load_pf_vf_vport(esw, MLX5_VPORT_ECPF, enabled_events);
1303 if (mlx5_core_ec_sriov_enabled(esw->dev)) {
1304 ret = mlx5_eswitch_load_ec_vf_vports(esw, esw->esw_funcs.num_ec_vfs,
1311 /* Enable VF vports */
1312 ret = mlx5_eswitch_load_vf_vports(esw, esw->esw_funcs.num_vfs,
1319 if (mlx5_core_ec_sriov_enabled(esw->dev))
1320 mlx5_eswitch_unload_ec_vf_vports(esw, esw->esw_funcs.num_ec_vfs);
1322 if (mlx5_ecpf_vport_exists(esw->dev))
1323 mlx5_eswitch_unload_pf_vf_vport(esw, MLX5_VPORT_ECPF);
1325 host_pf_disable_hca(esw->dev);
1328 mlx5_eswitch_unload_pf_vf_vport(esw, MLX5_VPORT_PF);
1332 /* mlx5_eswitch_disable_pf_vf_vports() disables vports of PF, ECPF and VFs
1333 * whichever are previously enabled on the eswitch.
1335 void mlx5_eswitch_disable_pf_vf_vports(struct mlx5_eswitch *esw)
1337 mlx5_eswitch_unload_vf_vports(esw, esw->esw_funcs.num_vfs);
1339 if (mlx5_ecpf_vport_exists(esw->dev)) {
1340 if (mlx5_core_ec_sriov_enabled(esw->dev))
1341 mlx5_eswitch_unload_ec_vf_vports(esw, esw->esw_funcs.num_vfs);
1342 mlx5_eswitch_unload_pf_vf_vport(esw, MLX5_VPORT_ECPF);
1345 host_pf_disable_hca(esw->dev);
1347 if (mlx5_core_is_ecpf_esw_manager(esw->dev) ||
1348 esw->mode == MLX5_ESWITCH_LEGACY)
1349 mlx5_eswitch_unload_pf_vf_vport(esw, MLX5_VPORT_PF);
1352 static void mlx5_eswitch_get_devlink_param(struct mlx5_eswitch *esw)
1354 struct devlink *devlink = priv_to_devlink(esw->dev);
1355 union devlink_param_value val;
1358 err = devl_param_driverinit_value_get(devlink,
1359 MLX5_DEVLINK_PARAM_ID_ESW_LARGE_GROUP_NUM,
1362 esw->params.large_group_num = val.vu32;
1365 "Devlink can't get param fdb_large_groups, uses default (%d).\n",
1366 ESW_OFFLOADS_DEFAULT_NUM_GROUPS);
1367 esw->params.large_group_num = ESW_OFFLOADS_DEFAULT_NUM_GROUPS;
1372 mlx5_eswitch_update_num_of_vfs(struct mlx5_eswitch *esw, int num_vfs)
1379 if (!mlx5_core_is_ecpf_esw_manager(esw->dev)) {
1380 esw->esw_funcs.num_vfs = num_vfs;
1384 out = mlx5_esw_query_functions(esw->dev);
1388 esw->esw_funcs.num_vfs = MLX5_GET(query_esw_functions_out, out,
1389 host_params_context.host_num_of_vfs);
1390 if (mlx5_core_ec_sriov_enabled(esw->dev))
1391 esw->esw_funcs.num_ec_vfs = num_vfs;
1396 static void mlx5_esw_mode_change_notify(struct mlx5_eswitch *esw, u16 mode)
1398 struct mlx5_esw_event_info info = {};
1400 info.new_mode = mode;
1402 blocking_notifier_call_chain(&esw->n_head, 0, &info);
1405 static int mlx5_esw_acls_ns_init(struct mlx5_eswitch *esw)
1407 struct mlx5_core_dev *dev = esw->dev;
1411 if (esw->flags & MLX5_ESWITCH_VPORT_ACL_NS_CREATED)
1414 total_vports = mlx5_eswitch_get_total_vports(dev);
1416 if (MLX5_CAP_ESW_EGRESS_ACL(dev, ft_support)) {
1417 err = mlx5_fs_egress_acls_init(dev, total_vports);
1421 esw_warn(dev, "egress ACL is not supported by FW\n");
1424 if (MLX5_CAP_ESW_INGRESS_ACL(dev, ft_support)) {
1425 err = mlx5_fs_ingress_acls_init(dev, total_vports);
1429 esw_warn(dev, "ingress ACL is not supported by FW\n");
1431 esw->flags |= MLX5_ESWITCH_VPORT_ACL_NS_CREATED;
1435 if (MLX5_CAP_ESW_EGRESS_ACL(dev, ft_support))
1436 mlx5_fs_egress_acls_cleanup(dev);
1440 static void mlx5_esw_acls_ns_cleanup(struct mlx5_eswitch *esw)
1442 struct mlx5_core_dev *dev = esw->dev;
1444 esw->flags &= ~MLX5_ESWITCH_VPORT_ACL_NS_CREATED;
1445 if (MLX5_CAP_ESW_INGRESS_ACL(dev, ft_support))
1446 mlx5_fs_ingress_acls_cleanup(dev);
1447 if (MLX5_CAP_ESW_EGRESS_ACL(dev, ft_support))
1448 mlx5_fs_egress_acls_cleanup(dev);
1452 * mlx5_eswitch_enable_locked - Enable eswitch
1453 * @esw: Pointer to eswitch
1454 * @num_vfs: Enable eswitch for given number of VFs. This is optional.
1455 * Valid value are 0, > 0 and MLX5_ESWITCH_IGNORE_NUM_VFS.
1456 * Caller should pass num_vfs > 0 when enabling eswitch for
1457 * vf vports. Caller should pass num_vfs = 0, when eswitch
1458 * is enabled without sriov VFs or when caller
1459 * is unaware of the sriov state of the host PF on ECPF based
1460 * eswitch. Caller should pass < 0 when num_vfs should be
1461 * completely ignored. This is typically the case when eswitch
1462 * is enabled without sriov regardless of PF/ECPF system.
1463 * mlx5_eswitch_enable_locked() Enables eswitch in either legacy or offloads
1464 * mode. If num_vfs >=0 is provided, it setup VF related eswitch vports.
1465 * It returns 0 on success or error code on failure.
1467 int mlx5_eswitch_enable_locked(struct mlx5_eswitch *esw, int num_vfs)
1471 lockdep_assert_held(&esw->mode_lock);
1473 if (!MLX5_CAP_ESW_FLOWTABLE_FDB(esw->dev, ft_support)) {
1474 esw_warn(esw->dev, "FDB is not supported, aborting ...\n");
1478 mlx5_eswitch_get_devlink_param(esw);
1480 err = mlx5_esw_acls_ns_init(esw);
1484 mlx5_eswitch_update_num_of_vfs(esw, num_vfs);
1486 if (esw->mode == MLX5_ESWITCH_LEGACY) {
1487 err = esw_legacy_enable(esw);
1489 mlx5_rescan_drivers(esw->dev);
1490 err = esw_offloads_enable(esw);
1496 esw->fdb_table.flags |= MLX5_ESW_FDB_CREATED;
1498 mlx5_eswitch_event_handlers_register(esw);
1500 esw_info(esw->dev, "Enable: mode(%s), nvfs(%d), necvfs(%d), active vports(%d)\n",
1501 esw->mode == MLX5_ESWITCH_LEGACY ? "LEGACY" : "OFFLOADS",
1502 esw->esw_funcs.num_vfs, esw->esw_funcs.num_ec_vfs, esw->enabled_vports);
1504 mlx5_esw_mode_change_notify(esw, esw->mode);
1509 mlx5_esw_acls_ns_cleanup(esw);
1514 * mlx5_eswitch_enable - Enable eswitch
1515 * @esw: Pointer to eswitch
1516 * @num_vfs: Enable eswitch switch for given number of VFs.
1517 * Caller must pass num_vfs > 0 when enabling eswitch for
1519 * mlx5_eswitch_enable() returns 0 on success or error code on failure.
1521 int mlx5_eswitch_enable(struct mlx5_eswitch *esw, int num_vfs)
1526 if (!mlx5_esw_allowed(esw))
1529 devl_assert_locked(priv_to_devlink(esw->dev));
1531 toggle_lag = !mlx5_esw_is_fdb_created(esw);
1534 mlx5_lag_disable_change(esw->dev);
1536 down_write(&esw->mode_lock);
1537 if (!mlx5_esw_is_fdb_created(esw)) {
1538 ret = mlx5_eswitch_enable_locked(esw, num_vfs);
1540 enum mlx5_eswitch_vport_event vport_events;
1542 vport_events = (esw->mode == MLX5_ESWITCH_LEGACY) ?
1543 MLX5_LEGACY_SRIOV_VPORT_EVENTS : MLX5_VPORT_UC_ADDR_CHANGE;
1544 /* If this is the ECPF the number of host VFs is managed via the
1545 * eswitch function change event handler, and any num_vfs provided
1546 * here are intended to be EC VFs.
1548 if (!mlx5_core_is_ecpf(esw->dev)) {
1549 ret = mlx5_eswitch_load_vf_vports(esw, num_vfs, vport_events);
1551 esw->esw_funcs.num_vfs = num_vfs;
1552 } else if (mlx5_core_ec_sriov_enabled(esw->dev)) {
1553 ret = mlx5_eswitch_load_ec_vf_vports(esw, num_vfs, vport_events);
1555 esw->esw_funcs.num_ec_vfs = num_vfs;
1559 up_write(&esw->mode_lock);
1562 mlx5_lag_enable_change(esw->dev);
1567 /* When disabling sriov, free driver level resources. */
1568 void mlx5_eswitch_disable_sriov(struct mlx5_eswitch *esw, bool clear_vf)
1570 if (!mlx5_esw_allowed(esw))
1573 devl_assert_locked(priv_to_devlink(esw->dev));
1574 down_write(&esw->mode_lock);
1575 /* If driver is unloaded, this function is called twice by remove_one()
1576 * and mlx5_unload(). Prevent the second call.
1578 if (!esw->esw_funcs.num_vfs && !esw->esw_funcs.num_ec_vfs && !clear_vf)
1581 esw_info(esw->dev, "Unload vfs: mode(%s), nvfs(%d), necvfs(%d), active vports(%d)\n",
1582 esw->mode == MLX5_ESWITCH_LEGACY ? "LEGACY" : "OFFLOADS",
1583 esw->esw_funcs.num_vfs, esw->esw_funcs.num_ec_vfs, esw->enabled_vports);
1585 if (!mlx5_core_is_ecpf(esw->dev)) {
1586 mlx5_eswitch_unload_vf_vports(esw, esw->esw_funcs.num_vfs);
1588 mlx5_eswitch_clear_vf_vports_info(esw);
1589 } else if (mlx5_core_ec_sriov_enabled(esw->dev)) {
1590 mlx5_eswitch_unload_ec_vf_vports(esw, esw->esw_funcs.num_ec_vfs);
1592 mlx5_eswitch_clear_ec_vf_vports_info(esw);
1595 if (esw->mode == MLX5_ESWITCH_OFFLOADS) {
1596 struct devlink *devlink = priv_to_devlink(esw->dev);
1598 devl_rate_nodes_destroy(devlink);
1600 /* Destroy legacy fdb when disabling sriov in legacy mode. */
1601 if (esw->mode == MLX5_ESWITCH_LEGACY)
1602 mlx5_eswitch_disable_locked(esw);
1604 if (!mlx5_core_is_ecpf(esw->dev))
1605 esw->esw_funcs.num_vfs = 0;
1607 esw->esw_funcs.num_ec_vfs = 0;
1610 up_write(&esw->mode_lock);
1613 /* Free resources for corresponding eswitch mode. It is called by devlink
1614 * when changing eswitch mode or modprobe when unloading driver.
1616 void mlx5_eswitch_disable_locked(struct mlx5_eswitch *esw)
1618 struct devlink *devlink = priv_to_devlink(esw->dev);
1620 /* Notify eswitch users that it is exiting from current mode.
1621 * So that it can do necessary cleanup before the eswitch is disabled.
1623 mlx5_esw_mode_change_notify(esw, MLX5_ESWITCH_LEGACY);
1625 mlx5_eswitch_event_handlers_unregister(esw);
1627 esw_info(esw->dev, "Disable: mode(%s), nvfs(%d), necvfs(%d), active vports(%d)\n",
1628 esw->mode == MLX5_ESWITCH_LEGACY ? "LEGACY" : "OFFLOADS",
1629 esw->esw_funcs.num_vfs, esw->esw_funcs.num_ec_vfs, esw->enabled_vports);
1631 if (esw->fdb_table.flags & MLX5_ESW_FDB_CREATED) {
1632 esw->fdb_table.flags &= ~MLX5_ESW_FDB_CREATED;
1633 if (esw->mode == MLX5_ESWITCH_OFFLOADS)
1634 esw_offloads_disable(esw);
1635 else if (esw->mode == MLX5_ESWITCH_LEGACY)
1636 esw_legacy_disable(esw);
1637 mlx5_esw_acls_ns_cleanup(esw);
1640 if (esw->mode == MLX5_ESWITCH_OFFLOADS)
1641 devl_rate_nodes_destroy(devlink);
1644 void mlx5_eswitch_disable(struct mlx5_eswitch *esw)
1646 if (!mlx5_esw_allowed(esw))
1649 devl_assert_locked(priv_to_devlink(esw->dev));
1650 mlx5_lag_disable_change(esw->dev);
1651 down_write(&esw->mode_lock);
1652 mlx5_eswitch_disable_locked(esw);
1653 esw->mode = MLX5_ESWITCH_LEGACY;
1654 up_write(&esw->mode_lock);
1655 mlx5_lag_enable_change(esw->dev);
1658 static int mlx5_query_hca_cap_host_pf(struct mlx5_core_dev *dev, void *out)
1660 u16 opmod = (MLX5_CAP_GENERAL << 1) | (HCA_CAP_OPMOD_GET_MAX & 0x01);
1661 u8 in[MLX5_ST_SZ_BYTES(query_hca_cap_in)] = {};
1663 MLX5_SET(query_hca_cap_in, in, opcode, MLX5_CMD_OP_QUERY_HCA_CAP);
1664 MLX5_SET(query_hca_cap_in, in, op_mod, opmod);
1665 MLX5_SET(query_hca_cap_in, in, function_id, MLX5_VPORT_PF);
1666 MLX5_SET(query_hca_cap_in, in, other_function, true);
1667 return mlx5_cmd_exec_inout(dev, query_hca_cap, in, out);
1670 int mlx5_esw_sf_max_hpf_functions(struct mlx5_core_dev *dev, u16 *max_sfs, u16 *sf_base_id)
1673 int query_out_sz = MLX5_ST_SZ_BYTES(query_hca_cap_out);
1678 if (!mlx5_core_is_ecpf(dev)) {
1683 query_ctx = kzalloc(query_out_sz, GFP_KERNEL);
1687 err = mlx5_query_hca_cap_host_pf(dev, query_ctx);
1691 hca_caps = MLX5_ADDR_OF(query_hca_cap_out, query_ctx, capability);
1692 *max_sfs = MLX5_GET(cmd_hca_cap, hca_caps, max_num_sf);
1693 *sf_base_id = MLX5_GET(cmd_hca_cap, hca_caps, sf_base_id);
1700 static int mlx5_esw_vport_alloc(struct mlx5_eswitch *esw,
1701 int index, u16 vport_num)
1703 struct mlx5_vport *vport;
1706 vport = kzalloc(sizeof(*vport), GFP_KERNEL);
1710 vport->dev = esw->dev;
1711 vport->vport = vport_num;
1712 vport->index = index;
1713 vport->info.link_state = MLX5_VPORT_ADMIN_STATE_AUTO;
1714 INIT_WORK(&vport->vport_change_handler, esw_vport_change_handler);
1715 err = xa_insert(&esw->vports, vport_num, vport, GFP_KERNEL);
1719 esw->total_vports++;
1727 static void mlx5_esw_vport_free(struct mlx5_eswitch *esw, struct mlx5_vport *vport)
1729 xa_erase(&esw->vports, vport->vport);
1733 static void mlx5_esw_vports_cleanup(struct mlx5_eswitch *esw)
1735 struct mlx5_vport *vport;
1738 mlx5_esw_for_each_vport(esw, i, vport)
1739 mlx5_esw_vport_free(esw, vport);
1740 xa_destroy(&esw->vports);
1743 static int mlx5_esw_vports_init(struct mlx5_eswitch *esw)
1745 struct mlx5_core_dev *dev = esw->dev;
1746 u16 max_host_pf_sfs;
1752 xa_init(&esw->vports);
1754 err = mlx5_esw_vport_alloc(esw, idx, MLX5_VPORT_PF);
1757 if (esw->first_host_vport == MLX5_VPORT_PF)
1758 xa_set_mark(&esw->vports, idx, MLX5_ESW_VPT_HOST_FN);
1761 for (i = 0; i < mlx5_core_max_vfs(dev); i++) {
1762 err = mlx5_esw_vport_alloc(esw, idx, idx);
1765 xa_set_mark(&esw->vports, idx, MLX5_ESW_VPT_VF);
1766 xa_set_mark(&esw->vports, idx, MLX5_ESW_VPT_HOST_FN);
1769 base_sf_num = mlx5_sf_start_function_id(dev);
1770 for (i = 0; i < mlx5_sf_max_functions(dev); i++) {
1771 err = mlx5_esw_vport_alloc(esw, idx, base_sf_num + i);
1774 xa_set_mark(&esw->vports, base_sf_num + i, MLX5_ESW_VPT_SF);
1778 err = mlx5_esw_sf_max_hpf_functions(dev, &max_host_pf_sfs, &base_sf_num);
1781 for (i = 0; i < max_host_pf_sfs; i++) {
1782 err = mlx5_esw_vport_alloc(esw, idx, base_sf_num + i);
1785 xa_set_mark(&esw->vports, base_sf_num + i, MLX5_ESW_VPT_SF);
1789 if (mlx5_core_ec_sriov_enabled(esw->dev)) {
1790 int ec_vf_base_num = mlx5_core_ec_vf_vport_base(dev);
1792 for (i = 0; i < mlx5_core_max_ec_vfs(esw->dev); i++) {
1793 err = mlx5_esw_vport_alloc(esw, idx, ec_vf_base_num + i);
1800 if (mlx5_ecpf_vport_exists(dev) ||
1801 mlx5_core_is_ecpf_esw_manager(dev)) {
1802 err = mlx5_esw_vport_alloc(esw, idx, MLX5_VPORT_ECPF);
1807 err = mlx5_esw_vport_alloc(esw, idx, MLX5_VPORT_UPLINK);
1813 mlx5_esw_vports_cleanup(esw);
1817 static int mlx5_devlink_esw_multiport_set(struct devlink *devlink, u32 id,
1818 struct devlink_param_gset_ctx *ctx)
1820 struct mlx5_core_dev *dev = devlink_priv(devlink);
1822 if (!MLX5_ESWITCH_MANAGER(dev))
1826 return mlx5_lag_mpesw_enable(dev);
1828 mlx5_lag_mpesw_disable(dev);
1832 static int mlx5_devlink_esw_multiport_get(struct devlink *devlink, u32 id,
1833 struct devlink_param_gset_ctx *ctx)
1835 struct mlx5_core_dev *dev = devlink_priv(devlink);
1837 ctx->val.vbool = mlx5_lag_is_mpesw(dev);
1841 static const struct devlink_param mlx5_eswitch_params[] = {
1842 DEVLINK_PARAM_DRIVER(MLX5_DEVLINK_PARAM_ID_ESW_MULTIPORT,
1843 "esw_multiport", DEVLINK_PARAM_TYPE_BOOL,
1844 BIT(DEVLINK_PARAM_CMODE_RUNTIME),
1845 mlx5_devlink_esw_multiport_get,
1846 mlx5_devlink_esw_multiport_set, NULL),
1849 int mlx5_eswitch_init(struct mlx5_core_dev *dev)
1851 struct mlx5_eswitch *esw;
1854 if (!MLX5_VPORT_MANAGER(dev) && !MLX5_ESWITCH_MANAGER(dev))
1857 esw = kzalloc(sizeof(*esw), GFP_KERNEL);
1861 err = devl_params_register(priv_to_devlink(dev), mlx5_eswitch_params,
1862 ARRAY_SIZE(mlx5_eswitch_params));
1867 esw->manager_vport = mlx5_eswitch_manager_vport(dev);
1868 esw->first_host_vport = mlx5_eswitch_first_host_vport_num(dev);
1870 esw->debugfs_root = debugfs_create_dir("esw", mlx5_debugfs_get_dev_root(dev));
1871 esw->work_queue = create_singlethread_workqueue("mlx5_esw_wq");
1872 if (!esw->work_queue) {
1877 err = mlx5_esw_vports_init(esw);
1881 err = esw_offloads_init(esw);
1885 mutex_init(&esw->offloads.encap_tbl_lock);
1886 hash_init(esw->offloads.encap_tbl);
1887 mutex_init(&esw->offloads.decap_tbl_lock);
1888 hash_init(esw->offloads.decap_tbl);
1889 mlx5e_mod_hdr_tbl_init(&esw->offloads.mod_hdr);
1890 atomic64_set(&esw->offloads.num_flows, 0);
1891 ida_init(&esw->offloads.vport_metadata_ida);
1892 xa_init_flags(&esw->offloads.vhca_map, XA_FLAGS_ALLOC);
1893 mutex_init(&esw->state_lock);
1894 init_rwsem(&esw->mode_lock);
1895 refcount_set(&esw->qos.refcnt, 0);
1897 esw->enabled_vports = 0;
1898 esw->mode = MLX5_ESWITCH_LEGACY;
1899 esw->offloads.inline_mode = MLX5_INLINE_MODE_NONE;
1900 if (MLX5_CAP_ESW_FLOWTABLE_FDB(dev, reformat) &&
1901 MLX5_CAP_ESW_FLOWTABLE_FDB(dev, decap))
1902 esw->offloads.encap = DEVLINK_ESWITCH_ENCAP_MODE_BASIC;
1904 esw->offloads.encap = DEVLINK_ESWITCH_ENCAP_MODE_NONE;
1905 if (MLX5_ESWITCH_MANAGER(dev) &&
1906 mlx5_esw_vport_match_metadata_supported(esw))
1907 esw->flags |= MLX5_ESWITCH_VPORT_MATCH_METADATA;
1909 dev->priv.eswitch = esw;
1910 BLOCKING_INIT_NOTIFIER_HEAD(&esw->n_head);
1913 "Total vports %d, per vport: max uc(%d) max mc(%d)\n",
1915 MLX5_MAX_UC_PER_VPORT(dev),
1916 MLX5_MAX_MC_PER_VPORT(dev));
1920 mlx5_esw_vports_cleanup(esw);
1922 if (esw->work_queue)
1923 destroy_workqueue(esw->work_queue);
1924 debugfs_remove_recursive(esw->debugfs_root);
1925 devl_params_unregister(priv_to_devlink(dev), mlx5_eswitch_params,
1926 ARRAY_SIZE(mlx5_eswitch_params));
1932 void mlx5_eswitch_cleanup(struct mlx5_eswitch *esw)
1937 esw_info(esw->dev, "cleanup\n");
1939 esw->dev->priv.eswitch = NULL;
1940 destroy_workqueue(esw->work_queue);
1941 WARN_ON(refcount_read(&esw->qos.refcnt));
1942 mutex_destroy(&esw->state_lock);
1943 WARN_ON(!xa_empty(&esw->offloads.vhca_map));
1944 xa_destroy(&esw->offloads.vhca_map);
1945 ida_destroy(&esw->offloads.vport_metadata_ida);
1946 mlx5e_mod_hdr_tbl_destroy(&esw->offloads.mod_hdr);
1947 mutex_destroy(&esw->offloads.encap_tbl_lock);
1948 mutex_destroy(&esw->offloads.decap_tbl_lock);
1949 esw_offloads_cleanup(esw);
1950 mlx5_esw_vports_cleanup(esw);
1951 debugfs_remove_recursive(esw->debugfs_root);
1952 devl_params_unregister(priv_to_devlink(esw->dev), mlx5_eswitch_params,
1953 ARRAY_SIZE(mlx5_eswitch_params));
1957 /* Vport Administration */
1959 mlx5_esw_set_vport_mac_locked(struct mlx5_eswitch *esw,
1960 struct mlx5_vport *evport, const u8 *mac)
1962 u16 vport_num = evport->vport;
1966 if (is_multicast_ether_addr(mac))
1969 if (evport->info.spoofchk && !is_valid_ether_addr(mac))
1970 mlx5_core_warn(esw->dev,
1971 "Set invalid MAC while spoofchk is on, vport(%d)\n",
1974 err = mlx5_modify_nic_vport_mac_address(esw->dev, vport_num, mac);
1976 mlx5_core_warn(esw->dev,
1977 "Failed to mlx5_modify_nic_vport_mac vport(%d) err=(%d)\n",
1982 node_guid_gen_from_mac(&node_guid, mac);
1983 err = mlx5_modify_nic_vport_node_guid(esw->dev, vport_num, node_guid);
1985 mlx5_core_warn(esw->dev,
1986 "Failed to set vport %d node guid, err = %d. RDMA_CM will not function properly for this VF.\n",
1989 ether_addr_copy(evport->info.mac, mac);
1990 evport->info.node_guid = node_guid;
1991 if (evport->enabled && esw->mode == MLX5_ESWITCH_LEGACY)
1992 err = esw_acl_ingress_lgcy_setup(esw, evport);
1997 int mlx5_eswitch_set_vport_mac(struct mlx5_eswitch *esw,
1998 u16 vport, const u8 *mac)
2000 struct mlx5_vport *evport = mlx5_eswitch_get_vport(esw, vport);
2004 return PTR_ERR(evport);
2006 mutex_lock(&esw->state_lock);
2007 err = mlx5_esw_set_vport_mac_locked(esw, evport, mac);
2008 mutex_unlock(&esw->state_lock);
2012 static bool mlx5_esw_check_port_type(struct mlx5_eswitch *esw, u16 vport_num, xa_mark_t mark)
2014 return xa_get_mark(&esw->vports, vport_num, mark);
2017 bool mlx5_eswitch_is_vf_vport(struct mlx5_eswitch *esw, u16 vport_num)
2019 return mlx5_esw_check_port_type(esw, vport_num, MLX5_ESW_VPT_VF);
2022 bool mlx5_eswitch_is_pf_vf_vport(struct mlx5_eswitch *esw, u16 vport_num)
2024 return vport_num == MLX5_VPORT_PF ||
2025 mlx5_eswitch_is_vf_vport(esw, vport_num);
2028 bool mlx5_esw_is_sf_vport(struct mlx5_eswitch *esw, u16 vport_num)
2030 return mlx5_esw_check_port_type(esw, vport_num, MLX5_ESW_VPT_SF);
2033 int mlx5_eswitch_set_vport_state(struct mlx5_eswitch *esw,
2034 u16 vport, int link_state)
2036 struct mlx5_vport *evport = mlx5_eswitch_get_vport(esw, vport);
2037 int opmod = MLX5_VPORT_STATE_OP_MOD_ESW_VPORT;
2038 int other_vport = 1;
2041 if (!mlx5_esw_allowed(esw))
2044 return PTR_ERR(evport);
2046 if (vport == MLX5_VPORT_UPLINK) {
2047 opmod = MLX5_VPORT_STATE_OP_MOD_UPLINK;
2051 mutex_lock(&esw->state_lock);
2052 if (esw->mode != MLX5_ESWITCH_LEGACY) {
2057 err = mlx5_modify_vport_admin_state(esw->dev, opmod, vport, other_vport, link_state);
2059 mlx5_core_warn(esw->dev, "Failed to set vport %d link state, opmod = %d, err = %d",
2064 evport->info.link_state = link_state;
2067 mutex_unlock(&esw->state_lock);
2071 int mlx5_eswitch_get_vport_config(struct mlx5_eswitch *esw,
2072 u16 vport, struct ifla_vf_info *ivi)
2074 struct mlx5_vport *evport = mlx5_eswitch_get_vport(esw, vport);
2077 return PTR_ERR(evport);
2079 memset(ivi, 0, sizeof(*ivi));
2080 ivi->vf = vport - 1;
2082 mutex_lock(&esw->state_lock);
2083 ether_addr_copy(ivi->mac, evport->info.mac);
2084 ivi->linkstate = evport->info.link_state;
2085 ivi->vlan = evport->info.vlan;
2086 ivi->qos = evport->info.qos;
2087 ivi->spoofchk = evport->info.spoofchk;
2088 ivi->trusted = evport->info.trusted;
2089 if (evport->qos.enabled) {
2090 ivi->min_tx_rate = evport->qos.min_rate;
2091 ivi->max_tx_rate = evport->qos.max_rate;
2093 mutex_unlock(&esw->state_lock);
2098 int __mlx5_eswitch_set_vport_vlan(struct mlx5_eswitch *esw,
2099 u16 vport, u16 vlan, u8 qos, u8 set_flags)
2101 struct mlx5_vport *evport = mlx5_eswitch_get_vport(esw, vport);
2102 bool vst_mode_steering = esw_vst_mode_is_steering(esw);
2106 return PTR_ERR(evport);
2107 if (vlan > 4095 || qos > 7)
2110 if (esw->mode == MLX5_ESWITCH_OFFLOADS || !vst_mode_steering) {
2111 err = modify_esw_vport_cvlan(esw->dev, vport, vlan, qos, set_flags);
2116 evport->info.vlan = vlan;
2117 evport->info.qos = qos;
2118 if (evport->enabled && esw->mode == MLX5_ESWITCH_LEGACY) {
2119 err = esw_acl_ingress_lgcy_setup(esw, evport);
2122 err = esw_acl_egress_lgcy_setup(esw, evport);
2128 int mlx5_eswitch_get_vport_stats(struct mlx5_eswitch *esw,
2130 struct ifla_vf_stats *vf_stats)
2132 struct mlx5_vport *vport = mlx5_eswitch_get_vport(esw, vport_num);
2133 int outlen = MLX5_ST_SZ_BYTES(query_vport_counter_out);
2134 u32 in[MLX5_ST_SZ_DW(query_vport_counter_in)] = {};
2135 struct mlx5_vport_drop_stats stats = {};
2140 return PTR_ERR(vport);
2142 out = kvzalloc(outlen, GFP_KERNEL);
2146 MLX5_SET(query_vport_counter_in, in, opcode,
2147 MLX5_CMD_OP_QUERY_VPORT_COUNTER);
2148 MLX5_SET(query_vport_counter_in, in, op_mod, 0);
2149 MLX5_SET(query_vport_counter_in, in, vport_number, vport->vport);
2150 MLX5_SET(query_vport_counter_in, in, other_vport, 1);
2152 err = mlx5_cmd_exec_inout(esw->dev, query_vport_counter, in, out);
2156 #define MLX5_GET_CTR(p, x) \
2157 MLX5_GET64(query_vport_counter_out, p, x)
2159 memset(vf_stats, 0, sizeof(*vf_stats));
2160 vf_stats->rx_packets =
2161 MLX5_GET_CTR(out, received_eth_unicast.packets) +
2162 MLX5_GET_CTR(out, received_ib_unicast.packets) +
2163 MLX5_GET_CTR(out, received_eth_multicast.packets) +
2164 MLX5_GET_CTR(out, received_ib_multicast.packets) +
2165 MLX5_GET_CTR(out, received_eth_broadcast.packets);
2167 vf_stats->rx_bytes =
2168 MLX5_GET_CTR(out, received_eth_unicast.octets) +
2169 MLX5_GET_CTR(out, received_ib_unicast.octets) +
2170 MLX5_GET_CTR(out, received_eth_multicast.octets) +
2171 MLX5_GET_CTR(out, received_ib_multicast.octets) +
2172 MLX5_GET_CTR(out, received_eth_broadcast.octets);
2174 vf_stats->tx_packets =
2175 MLX5_GET_CTR(out, transmitted_eth_unicast.packets) +
2176 MLX5_GET_CTR(out, transmitted_ib_unicast.packets) +
2177 MLX5_GET_CTR(out, transmitted_eth_multicast.packets) +
2178 MLX5_GET_CTR(out, transmitted_ib_multicast.packets) +
2179 MLX5_GET_CTR(out, transmitted_eth_broadcast.packets);
2181 vf_stats->tx_bytes =
2182 MLX5_GET_CTR(out, transmitted_eth_unicast.octets) +
2183 MLX5_GET_CTR(out, transmitted_ib_unicast.octets) +
2184 MLX5_GET_CTR(out, transmitted_eth_multicast.octets) +
2185 MLX5_GET_CTR(out, transmitted_ib_multicast.octets) +
2186 MLX5_GET_CTR(out, transmitted_eth_broadcast.octets);
2188 vf_stats->multicast =
2189 MLX5_GET_CTR(out, received_eth_multicast.packets) +
2190 MLX5_GET_CTR(out, received_ib_multicast.packets);
2192 vf_stats->broadcast =
2193 MLX5_GET_CTR(out, received_eth_broadcast.packets);
2195 err = mlx5_esw_query_vport_drop_stats(esw->dev, vport, &stats);
2198 vf_stats->rx_dropped = stats.rx_dropped;
2199 vf_stats->tx_dropped = stats.tx_dropped;
2206 u8 mlx5_eswitch_mode(const struct mlx5_core_dev *dev)
2208 struct mlx5_eswitch *esw = dev->priv.eswitch;
2210 return mlx5_esw_allowed(esw) ? esw->mode : MLX5_ESWITCH_LEGACY;
2212 EXPORT_SYMBOL_GPL(mlx5_eswitch_mode);
2214 enum devlink_eswitch_encap_mode
2215 mlx5_eswitch_get_encap_mode(const struct mlx5_core_dev *dev)
2217 struct mlx5_eswitch *esw;
2219 esw = dev->priv.eswitch;
2220 return (mlx5_eswitch_mode(dev) == MLX5_ESWITCH_OFFLOADS) ? esw->offloads.encap :
2221 DEVLINK_ESWITCH_ENCAP_MODE_NONE;
2223 EXPORT_SYMBOL(mlx5_eswitch_get_encap_mode);
2225 bool mlx5_esw_multipath_prereq(struct mlx5_core_dev *dev0,
2226 struct mlx5_core_dev *dev1)
2228 return (dev0->priv.eswitch->mode == MLX5_ESWITCH_OFFLOADS &&
2229 dev1->priv.eswitch->mode == MLX5_ESWITCH_OFFLOADS);
2232 int mlx5_esw_event_notifier_register(struct mlx5_eswitch *esw, struct notifier_block *nb)
2234 return blocking_notifier_chain_register(&esw->n_head, nb);
2237 void mlx5_esw_event_notifier_unregister(struct mlx5_eswitch *esw, struct notifier_block *nb)
2239 blocking_notifier_chain_unregister(&esw->n_head, nb);
2243 * mlx5_esw_hold() - Try to take a read lock on esw mode lock.
2244 * @mdev: mlx5 core device.
2246 * Should be called by esw resources callers.
2248 * Return: true on success or false.
2250 bool mlx5_esw_hold(struct mlx5_core_dev *mdev)
2252 struct mlx5_eswitch *esw = mdev->priv.eswitch;
2254 /* e.g. VF doesn't have eswitch so nothing to do */
2255 if (!mlx5_esw_allowed(esw))
2258 if (down_read_trylock(&esw->mode_lock) != 0)
2265 * mlx5_esw_release() - Release a read lock on esw mode lock.
2266 * @mdev: mlx5 core device.
2268 void mlx5_esw_release(struct mlx5_core_dev *mdev)
2270 struct mlx5_eswitch *esw = mdev->priv.eswitch;
2272 if (mlx5_esw_allowed(esw))
2273 up_read(&esw->mode_lock);
2277 * mlx5_esw_get() - Increase esw user count.
2278 * @mdev: mlx5 core device.
2280 void mlx5_esw_get(struct mlx5_core_dev *mdev)
2282 struct mlx5_eswitch *esw = mdev->priv.eswitch;
2284 if (mlx5_esw_allowed(esw))
2285 atomic64_inc(&esw->user_count);
2289 * mlx5_esw_put() - Decrease esw user count.
2290 * @mdev: mlx5 core device.
2292 void mlx5_esw_put(struct mlx5_core_dev *mdev)
2294 struct mlx5_eswitch *esw = mdev->priv.eswitch;
2296 if (mlx5_esw_allowed(esw))
2297 atomic64_dec_if_positive(&esw->user_count);
2301 * mlx5_esw_try_lock() - Take a write lock on esw mode lock.
2302 * @esw: eswitch device.
2304 * Should be called by esw mode change routine.
2307 * * 0 - esw mode if successfully locked and refcount is 0.
2308 * * -EBUSY - refcount is not 0.
2309 * * -EINVAL - In the middle of switching mode or lock is already held.
2311 int mlx5_esw_try_lock(struct mlx5_eswitch *esw)
2313 if (down_write_trylock(&esw->mode_lock) == 0)
2316 if (atomic64_read(&esw->user_count) > 0) {
2317 up_write(&esw->mode_lock);
2325 * mlx5_esw_unlock() - Release write lock on esw mode lock
2326 * @esw: eswitch device.
2328 void mlx5_esw_unlock(struct mlx5_eswitch *esw)
2330 up_write(&esw->mode_lock);
2334 * mlx5_eswitch_get_total_vports - Get total vports of the eswitch
2336 * @dev: Pointer to core device
2338 * mlx5_eswitch_get_total_vports returns total number of eswitch vports.
2340 u16 mlx5_eswitch_get_total_vports(const struct mlx5_core_dev *dev)
2342 struct mlx5_eswitch *esw;
2344 esw = dev->priv.eswitch;
2345 return mlx5_esw_allowed(esw) ? esw->total_vports : 0;
2347 EXPORT_SYMBOL_GPL(mlx5_eswitch_get_total_vports);
2350 * mlx5_eswitch_get_core_dev - Get the mdev device
2351 * @esw : eswitch device.
2353 * Return the mellanox core device which manages the eswitch.
2355 struct mlx5_core_dev *mlx5_eswitch_get_core_dev(struct mlx5_eswitch *esw)
2357 return mlx5_esw_allowed(esw) ? esw->dev : NULL;
2359 EXPORT_SYMBOL(mlx5_eswitch_get_core_dev);
2361 bool mlx5_eswitch_block_ipsec(struct mlx5_core_dev *dev)
2363 struct mlx5_eswitch *esw = dev->priv.eswitch;
2365 if (!mlx5_esw_allowed(esw))
2368 mutex_lock(&esw->state_lock);
2369 if (esw->enabled_ipsec_vf_count) {
2370 mutex_unlock(&esw->state_lock);
2374 dev->num_ipsec_offloads++;
2375 mutex_unlock(&esw->state_lock);
2379 void mlx5_eswitch_unblock_ipsec(struct mlx5_core_dev *dev)
2381 struct mlx5_eswitch *esw = dev->priv.eswitch;
2383 if (!mlx5_esw_allowed(esw))
2384 /* Failure means no eswitch => core dev is not a PF */
2387 mutex_lock(&esw->state_lock);
2388 dev->num_ipsec_offloads--;
2389 mutex_unlock(&esw->state_lock);