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/mlx5/driver.h>
35 #include <linux/mlx5/mlx5_ifc.h>
36 #include <linux/mlx5/vport.h>
37 #include <linux/mlx5/fs.h>
38 #include <linux/mlx5/mpfs.h>
39 #include "esw/acl/lgcy.h"
40 #include "esw/legacy.h"
42 #include "mlx5_core.h"
48 #include "en/mod_hdr.h"
56 /* Vport UC/MC hash node */
58 struct l2addr_node node;
61 struct mlx5_flow_handle *flow_rule;
62 bool mpfs; /* UC MAC was added to MPFs */
63 /* A flag indicating that mac was added due to mc promiscuous vport */
67 static int mlx5_eswitch_check(const struct mlx5_core_dev *dev)
69 if (MLX5_CAP_GEN(dev, port_type) != MLX5_CAP_PORT_TYPE_ETH)
72 if (!MLX5_ESWITCH_MANAGER(dev))
78 struct mlx5_eswitch *mlx5_devlink_eswitch_get(struct devlink *devlink)
80 struct mlx5_core_dev *dev = devlink_priv(devlink);
83 err = mlx5_eswitch_check(dev);
87 return dev->priv.eswitch;
90 struct mlx5_vport *__must_check
91 mlx5_eswitch_get_vport(struct mlx5_eswitch *esw, u16 vport_num)
93 struct mlx5_vport *vport;
96 return ERR_PTR(-EPERM);
98 vport = xa_load(&esw->vports, vport_num);
100 esw_debug(esw->dev, "vport out of range: num(0x%x)\n", vport_num);
101 return ERR_PTR(-EINVAL);
106 static int arm_vport_context_events_cmd(struct mlx5_core_dev *dev, u16 vport,
109 u32 in[MLX5_ST_SZ_DW(modify_nic_vport_context_in)] = {};
112 MLX5_SET(modify_nic_vport_context_in, in,
113 opcode, MLX5_CMD_OP_MODIFY_NIC_VPORT_CONTEXT);
114 MLX5_SET(modify_nic_vport_context_in, in, field_select.change_event, 1);
115 MLX5_SET(modify_nic_vport_context_in, in, vport_number, vport);
116 if (vport || mlx5_core_is_ecpf(dev))
117 MLX5_SET(modify_nic_vport_context_in, in, other_vport, 1);
118 nic_vport_ctx = MLX5_ADDR_OF(modify_nic_vport_context_in,
119 in, nic_vport_context);
121 MLX5_SET(nic_vport_context, nic_vport_ctx, arm_change_event, 1);
123 if (events_mask & MLX5_VPORT_UC_ADDR_CHANGE)
124 MLX5_SET(nic_vport_context, nic_vport_ctx,
125 event_on_uc_address_change, 1);
126 if (events_mask & MLX5_VPORT_MC_ADDR_CHANGE)
127 MLX5_SET(nic_vport_context, nic_vport_ctx,
128 event_on_mc_address_change, 1);
129 if (events_mask & MLX5_VPORT_PROMISC_CHANGE)
130 MLX5_SET(nic_vport_context, nic_vport_ctx,
131 event_on_promisc_change, 1);
133 return mlx5_cmd_exec_in(dev, modify_nic_vport_context, in);
136 /* E-Switch vport context HW commands */
137 int mlx5_eswitch_modify_esw_vport_context(struct mlx5_core_dev *dev, u16 vport,
138 bool other_vport, void *in)
140 MLX5_SET(modify_esw_vport_context_in, in, opcode,
141 MLX5_CMD_OP_MODIFY_ESW_VPORT_CONTEXT);
142 MLX5_SET(modify_esw_vport_context_in, in, vport_number, vport);
143 MLX5_SET(modify_esw_vport_context_in, in, other_vport, other_vport);
144 return mlx5_cmd_exec_in(dev, modify_esw_vport_context, in);
147 static int modify_esw_vport_cvlan(struct mlx5_core_dev *dev, u16 vport,
148 u16 vlan, u8 qos, u8 set_flags)
150 u32 in[MLX5_ST_SZ_DW(modify_esw_vport_context_in)] = {};
152 if (!MLX5_CAP_ESW(dev, vport_cvlan_strip) ||
153 !MLX5_CAP_ESW(dev, vport_cvlan_insert_if_not_exist))
156 esw_debug(dev, "Set Vport[%d] VLAN %d qos %d set=%x\n",
157 vport, vlan, qos, set_flags);
159 if (set_flags & SET_VLAN_STRIP)
160 MLX5_SET(modify_esw_vport_context_in, in,
161 esw_vport_context.vport_cvlan_strip, 1);
163 if (set_flags & SET_VLAN_INSERT) {
164 if (MLX5_CAP_ESW(dev, vport_cvlan_insert_always)) {
165 /* insert either if vlan exist in packet or not */
166 MLX5_SET(modify_esw_vport_context_in, in,
167 esw_vport_context.vport_cvlan_insert,
168 MLX5_VPORT_CVLAN_INSERT_ALWAYS);
170 /* insert only if no vlan in packet */
171 MLX5_SET(modify_esw_vport_context_in, in,
172 esw_vport_context.vport_cvlan_insert,
173 MLX5_VPORT_CVLAN_INSERT_WHEN_NO_CVLAN);
175 MLX5_SET(modify_esw_vport_context_in, in,
176 esw_vport_context.cvlan_pcp, qos);
177 MLX5_SET(modify_esw_vport_context_in, in,
178 esw_vport_context.cvlan_id, vlan);
181 MLX5_SET(modify_esw_vport_context_in, in,
182 field_select.vport_cvlan_strip, 1);
183 MLX5_SET(modify_esw_vport_context_in, in,
184 field_select.vport_cvlan_insert, 1);
186 return mlx5_eswitch_modify_esw_vport_context(dev, vport, true, in);
190 static struct mlx5_flow_handle *
191 __esw_fdb_set_vport_rule(struct mlx5_eswitch *esw, u16 vport, bool rx_rule,
192 u8 mac_c[ETH_ALEN], u8 mac_v[ETH_ALEN])
194 int match_header = (is_zero_ether_addr(mac_c) ? 0 :
195 MLX5_MATCH_OUTER_HEADERS);
196 struct mlx5_flow_handle *flow_rule = NULL;
197 struct mlx5_flow_act flow_act = {0};
198 struct mlx5_flow_destination dest = {};
199 struct mlx5_flow_spec *spec;
200 void *mv_misc = NULL;
201 void *mc_misc = NULL;
206 match_header |= MLX5_MATCH_MISC_PARAMETERS;
208 spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
212 dmac_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
213 outer_headers.dmac_47_16);
214 dmac_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
215 outer_headers.dmac_47_16);
217 if (match_header & MLX5_MATCH_OUTER_HEADERS) {
218 ether_addr_copy(dmac_v, mac_v);
219 ether_addr_copy(dmac_c, mac_c);
222 if (match_header & MLX5_MATCH_MISC_PARAMETERS) {
223 mv_misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
225 mc_misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
227 MLX5_SET(fte_match_set_misc, mv_misc, source_port, MLX5_VPORT_UPLINK);
228 MLX5_SET_TO_ONES(fte_match_set_misc, mc_misc, source_port);
231 dest.type = MLX5_FLOW_DESTINATION_TYPE_VPORT;
232 dest.vport.num = vport;
235 "\tFDB add rule dmac_v(%pM) dmac_c(%pM) -> vport(%d)\n",
236 dmac_v, dmac_c, vport);
237 spec->match_criteria_enable = match_header;
238 flow_act.action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
240 mlx5_add_flow_rules(esw->fdb_table.legacy.fdb, spec,
241 &flow_act, &dest, 1);
242 if (IS_ERR(flow_rule)) {
244 "FDB: Failed to add flow rule: dmac_v(%pM) dmac_c(%pM) -> vport(%d), err(%ld)\n",
245 dmac_v, dmac_c, vport, PTR_ERR(flow_rule));
253 static struct mlx5_flow_handle *
254 esw_fdb_set_vport_rule(struct mlx5_eswitch *esw, u8 mac[ETH_ALEN], u16 vport)
258 eth_broadcast_addr(mac_c);
259 return __esw_fdb_set_vport_rule(esw, vport, false, mac_c, mac);
262 static struct mlx5_flow_handle *
263 esw_fdb_set_vport_allmulti_rule(struct mlx5_eswitch *esw, u16 vport)
268 eth_zero_addr(mac_c);
269 eth_zero_addr(mac_v);
272 return __esw_fdb_set_vport_rule(esw, vport, false, mac_c, mac_v);
275 static struct mlx5_flow_handle *
276 esw_fdb_set_vport_promisc_rule(struct mlx5_eswitch *esw, u16 vport)
281 eth_zero_addr(mac_c);
282 eth_zero_addr(mac_v);
283 return __esw_fdb_set_vport_rule(esw, vport, true, mac_c, mac_v);
286 /* E-Switch vport UC/MC lists management */
287 typedef int (*vport_addr_action)(struct mlx5_eswitch *esw,
288 struct vport_addr *vaddr);
290 static int esw_add_uc_addr(struct mlx5_eswitch *esw, struct vport_addr *vaddr)
292 u8 *mac = vaddr->node.addr;
293 u16 vport = vaddr->vport;
296 /* Skip mlx5_mpfs_add_mac for eswitch_managers,
297 * it is already done by its netdev in mlx5e_execute_l2_action
299 if (mlx5_esw_is_manager_vport(esw, vport))
302 err = mlx5_mpfs_add_mac(esw->dev, mac);
305 "Failed to add L2 table mac(%pM) for vport(0x%x), err(%d)\n",
312 /* SRIOV is enabled: Forward UC MAC to vport */
313 if (esw->fdb_table.legacy.fdb && esw->mode == MLX5_ESWITCH_LEGACY) {
314 vaddr->flow_rule = esw_fdb_set_vport_rule(esw, mac, vport);
316 esw_debug(esw->dev, "\tADDED UC MAC: vport[%d] %pM fr(%p)\n",
317 vport, mac, vaddr->flow_rule);
323 static int esw_del_uc_addr(struct mlx5_eswitch *esw, struct vport_addr *vaddr)
325 u8 *mac = vaddr->node.addr;
326 u16 vport = vaddr->vport;
329 /* Skip mlx5_mpfs_del_mac for eswitch managers,
330 * it is already done by its netdev in mlx5e_execute_l2_action
332 if (!vaddr->mpfs || mlx5_esw_is_manager_vport(esw, vport))
335 err = mlx5_mpfs_del_mac(esw->dev, mac);
338 "Failed to del L2 table mac(%pM) for vport(%d), err(%d)\n",
343 if (vaddr->flow_rule)
344 mlx5_del_flow_rules(vaddr->flow_rule);
345 vaddr->flow_rule = NULL;
350 static void update_allmulti_vports(struct mlx5_eswitch *esw,
351 struct vport_addr *vaddr,
352 struct esw_mc_addr *esw_mc)
354 u8 *mac = vaddr->node.addr;
355 struct mlx5_vport *vport;
359 mlx5_esw_for_each_vport(esw, i, vport) {
360 struct hlist_head *vport_hash = vport->mc_list;
361 struct vport_addr *iter_vaddr =
362 l2addr_hash_find(vport_hash,
365 vport_num = vport->vport;
366 if (IS_ERR_OR_NULL(vport->allmulti_rule) ||
367 vaddr->vport == vport_num)
369 switch (vaddr->action) {
370 case MLX5_ACTION_ADD:
373 iter_vaddr = l2addr_hash_add(vport_hash, mac,
378 "ALL-MULTI: Failed to add MAC(%pM) to vport[%d] DB\n",
382 iter_vaddr->vport = vport_num;
383 iter_vaddr->flow_rule =
384 esw_fdb_set_vport_rule(esw,
387 iter_vaddr->mc_promisc = true;
389 case MLX5_ACTION_DEL:
392 mlx5_del_flow_rules(iter_vaddr->flow_rule);
393 l2addr_hash_del(iter_vaddr);
399 static int esw_add_mc_addr(struct mlx5_eswitch *esw, struct vport_addr *vaddr)
401 struct hlist_head *hash = esw->mc_table;
402 struct esw_mc_addr *esw_mc;
403 u8 *mac = vaddr->node.addr;
404 u16 vport = vaddr->vport;
406 if (!esw->fdb_table.legacy.fdb)
409 esw_mc = l2addr_hash_find(hash, mac, struct esw_mc_addr);
413 esw_mc = l2addr_hash_add(hash, mac, struct esw_mc_addr, GFP_KERNEL);
417 esw_mc->uplink_rule = /* Forward MC MAC to Uplink */
418 esw_fdb_set_vport_rule(esw, mac, MLX5_VPORT_UPLINK);
420 /* Add this multicast mac to all the mc promiscuous vports */
421 update_allmulti_vports(esw, vaddr, esw_mc);
424 /* If the multicast mac is added as a result of mc promiscuous vport,
425 * don't increment the multicast ref count
427 if (!vaddr->mc_promisc)
430 /* Forward MC MAC to vport */
431 vaddr->flow_rule = esw_fdb_set_vport_rule(esw, mac, vport);
433 "\tADDED MC MAC: vport[%d] %pM fr(%p) refcnt(%d) uplinkfr(%p)\n",
434 vport, mac, vaddr->flow_rule,
435 esw_mc->refcnt, esw_mc->uplink_rule);
439 static int esw_del_mc_addr(struct mlx5_eswitch *esw, struct vport_addr *vaddr)
441 struct hlist_head *hash = esw->mc_table;
442 struct esw_mc_addr *esw_mc;
443 u8 *mac = vaddr->node.addr;
444 u16 vport = vaddr->vport;
446 if (!esw->fdb_table.legacy.fdb)
449 esw_mc = l2addr_hash_find(hash, mac, struct esw_mc_addr);
452 "Failed to find eswitch MC addr for MAC(%pM) vport(%d)",
457 "\tDELETE MC MAC: vport[%d] %pM fr(%p) refcnt(%d) uplinkfr(%p)\n",
458 vport, mac, vaddr->flow_rule, esw_mc->refcnt,
459 esw_mc->uplink_rule);
461 if (vaddr->flow_rule)
462 mlx5_del_flow_rules(vaddr->flow_rule);
463 vaddr->flow_rule = NULL;
465 /* If the multicast mac is added as a result of mc promiscuous vport,
466 * don't decrement the multicast ref count.
468 if (vaddr->mc_promisc || (--esw_mc->refcnt > 0))
471 /* Remove this multicast mac from all the mc promiscuous vports */
472 update_allmulti_vports(esw, vaddr, esw_mc);
474 if (esw_mc->uplink_rule)
475 mlx5_del_flow_rules(esw_mc->uplink_rule);
477 l2addr_hash_del(esw_mc);
481 /* Apply vport UC/MC list to HW l2 table and FDB table */
482 static void esw_apply_vport_addr_list(struct mlx5_eswitch *esw,
483 struct mlx5_vport *vport, int list_type)
485 bool is_uc = list_type == MLX5_NVPRT_LIST_TYPE_UC;
486 vport_addr_action vport_addr_add;
487 vport_addr_action vport_addr_del;
488 struct vport_addr *addr;
489 struct l2addr_node *node;
490 struct hlist_head *hash;
491 struct hlist_node *tmp;
494 vport_addr_add = is_uc ? esw_add_uc_addr :
496 vport_addr_del = is_uc ? esw_del_uc_addr :
499 hash = is_uc ? vport->uc_list : vport->mc_list;
500 for_each_l2hash_node(node, tmp, hash, hi) {
501 addr = container_of(node, struct vport_addr, node);
502 switch (addr->action) {
503 case MLX5_ACTION_ADD:
504 vport_addr_add(esw, addr);
505 addr->action = MLX5_ACTION_NONE;
507 case MLX5_ACTION_DEL:
508 vport_addr_del(esw, addr);
509 l2addr_hash_del(addr);
515 /* Sync vport UC/MC list from vport context */
516 static void esw_update_vport_addr_list(struct mlx5_eswitch *esw,
517 struct mlx5_vport *vport, int list_type)
519 bool is_uc = list_type == MLX5_NVPRT_LIST_TYPE_UC;
520 u8 (*mac_list)[ETH_ALEN];
521 struct l2addr_node *node;
522 struct vport_addr *addr;
523 struct hlist_head *hash;
524 struct hlist_node *tmp;
530 size = is_uc ? MLX5_MAX_UC_PER_VPORT(esw->dev) :
531 MLX5_MAX_MC_PER_VPORT(esw->dev);
533 mac_list = kcalloc(size, ETH_ALEN, GFP_KERNEL);
537 hash = is_uc ? vport->uc_list : vport->mc_list;
539 for_each_l2hash_node(node, tmp, hash, hi) {
540 addr = container_of(node, struct vport_addr, node);
541 addr->action = MLX5_ACTION_DEL;
547 err = mlx5_query_nic_vport_mac_list(esw->dev, vport->vport, list_type,
551 esw_debug(esw->dev, "vport[%d] context update %s list size (%d)\n",
552 vport->vport, is_uc ? "UC" : "MC", size);
554 for (i = 0; i < size; i++) {
555 if (is_uc && !is_valid_ether_addr(mac_list[i]))
558 if (!is_uc && !is_multicast_ether_addr(mac_list[i]))
561 addr = l2addr_hash_find(hash, mac_list[i], struct vport_addr);
563 addr->action = MLX5_ACTION_NONE;
564 /* If this mac was previously added because of allmulti
565 * promiscuous rx mode, its now converted to be original
568 if (addr->mc_promisc) {
569 struct esw_mc_addr *esw_mc =
570 l2addr_hash_find(esw->mc_table,
575 "Failed to MAC(%pM) in mcast DB\n",
580 addr->mc_promisc = false;
585 addr = l2addr_hash_add(hash, mac_list[i], struct vport_addr,
589 "Failed to add MAC(%pM) to vport[%d] DB\n",
590 mac_list[i], vport->vport);
593 addr->vport = vport->vport;
594 addr->action = MLX5_ACTION_ADD;
600 /* Sync vport UC/MC list from vport context
601 * Must be called after esw_update_vport_addr_list
603 static void esw_update_vport_mc_promisc(struct mlx5_eswitch *esw,
604 struct mlx5_vport *vport)
606 struct l2addr_node *node;
607 struct vport_addr *addr;
608 struct hlist_head *hash;
609 struct hlist_node *tmp;
612 hash = vport->mc_list;
614 for_each_l2hash_node(node, tmp, esw->mc_table, hi) {
615 u8 *mac = node->addr;
617 addr = l2addr_hash_find(hash, mac, struct vport_addr);
619 if (addr->action == MLX5_ACTION_DEL)
620 addr->action = MLX5_ACTION_NONE;
623 addr = l2addr_hash_add(hash, mac, struct vport_addr,
627 "Failed to add allmulti MAC(%pM) to vport[%d] DB\n",
631 addr->vport = vport->vport;
632 addr->action = MLX5_ACTION_ADD;
633 addr->mc_promisc = true;
637 /* Apply vport rx mode to HW FDB table */
638 static void esw_apply_vport_rx_mode(struct mlx5_eswitch *esw,
639 struct mlx5_vport *vport,
640 bool promisc, bool mc_promisc)
642 struct esw_mc_addr *allmulti_addr = &esw->mc_promisc;
644 if (IS_ERR_OR_NULL(vport->allmulti_rule) != mc_promisc)
648 vport->allmulti_rule =
649 esw_fdb_set_vport_allmulti_rule(esw, vport->vport);
650 if (!allmulti_addr->uplink_rule)
651 allmulti_addr->uplink_rule =
652 esw_fdb_set_vport_allmulti_rule(esw,
654 allmulti_addr->refcnt++;
655 } else if (vport->allmulti_rule) {
656 mlx5_del_flow_rules(vport->allmulti_rule);
657 vport->allmulti_rule = NULL;
659 if (--allmulti_addr->refcnt > 0)
662 if (allmulti_addr->uplink_rule)
663 mlx5_del_flow_rules(allmulti_addr->uplink_rule);
664 allmulti_addr->uplink_rule = NULL;
668 if (IS_ERR_OR_NULL(vport->promisc_rule) != promisc)
672 vport->promisc_rule =
673 esw_fdb_set_vport_promisc_rule(esw, vport->vport);
674 } else if (vport->promisc_rule) {
675 mlx5_del_flow_rules(vport->promisc_rule);
676 vport->promisc_rule = NULL;
680 /* Sync vport rx mode from vport context */
681 static void esw_update_vport_rx_mode(struct mlx5_eswitch *esw,
682 struct mlx5_vport *vport)
689 err = mlx5_query_nic_vport_promisc(esw->dev,
696 esw_debug(esw->dev, "vport[%d] context update rx mode promisc_all=%d, all_multi=%d\n",
697 vport->vport, promisc_all, promisc_mc);
699 if (!vport->info.trusted || !vport->enabled) {
705 esw_apply_vport_rx_mode(esw, vport, promisc_all,
706 (promisc_all || promisc_mc));
709 void esw_vport_change_handle_locked(struct mlx5_vport *vport)
711 struct mlx5_core_dev *dev = vport->dev;
712 struct mlx5_eswitch *esw = dev->priv.eswitch;
715 if (!MLX5_CAP_GEN(dev, log_max_l2_table))
718 mlx5_query_nic_vport_mac_address(dev, vport->vport, true, mac);
719 esw_debug(dev, "vport[%d] Context Changed: perm mac: %pM\n",
722 if (vport->enabled_events & MLX5_VPORT_UC_ADDR_CHANGE) {
723 esw_update_vport_addr_list(esw, vport, MLX5_NVPRT_LIST_TYPE_UC);
724 esw_apply_vport_addr_list(esw, vport, MLX5_NVPRT_LIST_TYPE_UC);
727 if (vport->enabled_events & MLX5_VPORT_MC_ADDR_CHANGE)
728 esw_update_vport_addr_list(esw, vport, MLX5_NVPRT_LIST_TYPE_MC);
730 if (vport->enabled_events & MLX5_VPORT_PROMISC_CHANGE) {
731 esw_update_vport_rx_mode(esw, vport);
732 if (!IS_ERR_OR_NULL(vport->allmulti_rule))
733 esw_update_vport_mc_promisc(esw, vport);
736 if (vport->enabled_events & (MLX5_VPORT_PROMISC_CHANGE | MLX5_VPORT_MC_ADDR_CHANGE))
737 esw_apply_vport_addr_list(esw, vport, MLX5_NVPRT_LIST_TYPE_MC);
739 esw_debug(esw->dev, "vport[%d] Context Changed: Done\n", vport->vport);
741 arm_vport_context_events_cmd(dev, vport->vport,
742 vport->enabled_events);
745 static void esw_vport_change_handler(struct work_struct *work)
747 struct mlx5_vport *vport =
748 container_of(work, struct mlx5_vport, vport_change_handler);
749 struct mlx5_eswitch *esw = vport->dev->priv.eswitch;
751 mutex_lock(&esw->state_lock);
752 esw_vport_change_handle_locked(vport);
753 mutex_unlock(&esw->state_lock);
756 static void node_guid_gen_from_mac(u64 *node_guid, const u8 *mac)
758 ((u8 *)node_guid)[7] = mac[0];
759 ((u8 *)node_guid)[6] = mac[1];
760 ((u8 *)node_guid)[5] = mac[2];
761 ((u8 *)node_guid)[4] = 0xff;
762 ((u8 *)node_guid)[3] = 0xfe;
763 ((u8 *)node_guid)[2] = mac[3];
764 ((u8 *)node_guid)[1] = mac[4];
765 ((u8 *)node_guid)[0] = mac[5];
768 static int esw_vport_setup_acl(struct mlx5_eswitch *esw,
769 struct mlx5_vport *vport)
771 if (esw->mode == MLX5_ESWITCH_LEGACY)
772 return esw_legacy_vport_acl_setup(esw, vport);
774 return esw_vport_create_offloads_acl_tables(esw, vport);
777 static void esw_vport_cleanup_acl(struct mlx5_eswitch *esw,
778 struct mlx5_vport *vport)
780 if (esw->mode == MLX5_ESWITCH_LEGACY)
781 esw_legacy_vport_acl_cleanup(esw, vport);
783 esw_vport_destroy_offloads_acl_tables(esw, vport);
786 static int mlx5_esw_vport_caps_get(struct mlx5_eswitch *esw, struct mlx5_vport *vport)
788 int query_out_sz = MLX5_ST_SZ_BYTES(query_hca_cap_out);
793 if (!MLX5_CAP_GEN(esw->dev, vhca_resource_manager))
796 query_ctx = kzalloc(query_out_sz, GFP_KERNEL);
800 err = mlx5_vport_get_other_func_cap(esw->dev, vport->vport, query_ctx,
805 hca_caps = MLX5_ADDR_OF(query_hca_cap_out, query_ctx, capability);
806 vport->info.roce_enabled = MLX5_GET(cmd_hca_cap, hca_caps, roce);
808 memset(query_ctx, 0, query_out_sz);
809 err = mlx5_vport_get_other_func_cap(esw->dev, vport->vport, query_ctx,
814 hca_caps = MLX5_ADDR_OF(query_hca_cap_out, query_ctx, capability);
815 vport->info.mig_enabled = MLX5_GET(cmd_hca_cap_2, hca_caps, migratable);
821 static int esw_vport_setup(struct mlx5_eswitch *esw, struct mlx5_vport *vport)
823 bool vst_mode_steering = esw_vst_mode_is_steering(esw);
824 u16 vport_num = vport->vport;
828 err = esw_vport_setup_acl(esw, vport);
832 if (mlx5_esw_is_manager_vport(esw, vport_num))
835 err = mlx5_esw_vport_caps_get(esw, vport);
839 mlx5_modify_vport_admin_state(esw->dev,
840 MLX5_VPORT_STATE_OP_MOD_ESW_VPORT,
842 vport->info.link_state);
844 /* Host PF has its own mac/guid. */
846 mlx5_modify_nic_vport_mac_address(esw->dev, vport_num,
848 mlx5_modify_nic_vport_node_guid(esw->dev, vport_num,
849 vport->info.node_guid);
852 flags = (vport->info.vlan || vport->info.qos) ?
853 SET_VLAN_STRIP | SET_VLAN_INSERT : 0;
854 if (esw->mode == MLX5_ESWITCH_OFFLOADS || !vst_mode_steering)
855 modify_esw_vport_cvlan(esw->dev, vport_num, vport->info.vlan,
856 vport->info.qos, flags);
861 esw_vport_cleanup_acl(esw, vport);
865 /* Don't cleanup vport->info, it's needed to restore vport configuration */
866 static void esw_vport_cleanup(struct mlx5_eswitch *esw, struct mlx5_vport *vport)
868 u16 vport_num = vport->vport;
870 if (!mlx5_esw_is_manager_vport(esw, vport_num))
871 mlx5_modify_vport_admin_state(esw->dev,
872 MLX5_VPORT_STATE_OP_MOD_ESW_VPORT,
874 MLX5_VPORT_ADMIN_STATE_DOWN);
876 mlx5_esw_qos_vport_disable(esw, vport);
877 esw_vport_cleanup_acl(esw, vport);
880 int mlx5_esw_vport_enable(struct mlx5_eswitch *esw, u16 vport_num,
881 enum mlx5_eswitch_vport_event enabled_events)
883 struct mlx5_vport *vport;
886 vport = mlx5_eswitch_get_vport(esw, vport_num);
888 return PTR_ERR(vport);
890 mutex_lock(&esw->state_lock);
891 WARN_ON(vport->enabled);
893 esw_debug(esw->dev, "Enabling VPORT(%d)\n", vport_num);
895 ret = esw_vport_setup(esw, vport);
899 /* Sync with current vport context */
900 vport->enabled_events = enabled_events;
901 vport->enabled = true;
903 /* Esw manager is trusted by default. Host PF (vport 0) is trusted as well
904 * in smartNIC as it's a vport group manager.
906 if (mlx5_esw_is_manager_vport(esw, vport_num) ||
907 (!vport_num && mlx5_core_is_ecpf(esw->dev)))
908 vport->info.trusted = true;
910 if (!mlx5_esw_is_manager_vport(esw, vport->vport) &&
911 MLX5_CAP_GEN(esw->dev, vhca_resource_manager)) {
912 ret = mlx5_esw_vport_vhca_id_set(esw, vport_num);
914 goto err_vhca_mapping;
917 /* External controller host PF has factory programmed MAC.
918 * Read it from the device.
920 if (mlx5_core_is_ecpf(esw->dev) && vport_num == MLX5_VPORT_PF)
921 mlx5_query_nic_vport_mac_address(esw->dev, vport_num, true, vport->info.mac);
923 esw_vport_change_handle_locked(vport);
925 esw->enabled_vports++;
926 esw_debug(esw->dev, "Enabled VPORT(%d)\n", vport_num);
928 mutex_unlock(&esw->state_lock);
932 esw_vport_cleanup(esw, vport);
933 mutex_unlock(&esw->state_lock);
937 void mlx5_esw_vport_disable(struct mlx5_eswitch *esw, u16 vport_num)
939 struct mlx5_vport *vport;
941 vport = mlx5_eswitch_get_vport(esw, vport_num);
945 mutex_lock(&esw->state_lock);
949 esw_debug(esw->dev, "Disabling vport(%d)\n", vport_num);
950 /* Mark this vport as disabled to discard new events */
951 vport->enabled = false;
953 /* Disable events from this vport */
954 if (MLX5_CAP_GEN(esw->dev, log_max_l2_table))
955 arm_vport_context_events_cmd(esw->dev, vport->vport, 0);
957 if (!mlx5_esw_is_manager_vport(esw, vport->vport) &&
958 MLX5_CAP_GEN(esw->dev, vhca_resource_manager))
959 mlx5_esw_vport_vhca_id_clear(esw, vport_num);
961 /* We don't assume VFs will cleanup after themselves.
962 * Calling vport change handler while vport is disabled will cleanup
963 * the vport resources.
965 esw_vport_change_handle_locked(vport);
966 vport->enabled_events = 0;
967 esw_apply_vport_rx_mode(esw, vport, false, false);
968 esw_vport_cleanup(esw, vport);
969 esw->enabled_vports--;
972 mutex_unlock(&esw->state_lock);
975 static int eswitch_vport_event(struct notifier_block *nb,
976 unsigned long type, void *data)
978 struct mlx5_eswitch *esw = mlx5_nb_cof(nb, struct mlx5_eswitch, nb);
979 struct mlx5_eqe *eqe = data;
980 struct mlx5_vport *vport;
983 vport_num = be16_to_cpu(eqe->data.vport_change.vport_num);
984 vport = mlx5_eswitch_get_vport(esw, vport_num);
986 queue_work(esw->work_queue, &vport->vport_change_handler);
991 * mlx5_esw_query_functions - Returns raw output about functions state
992 * @dev: Pointer to device to query
994 * mlx5_esw_query_functions() allocates and returns functions changed
995 * raw output memory pointer from device on success. Otherwise returns ERR_PTR.
996 * Caller must free the memory using kvfree() when valid pointer is returned.
998 const u32 *mlx5_esw_query_functions(struct mlx5_core_dev *dev)
1000 int outlen = MLX5_ST_SZ_BYTES(query_esw_functions_out);
1001 u32 in[MLX5_ST_SZ_DW(query_esw_functions_in)] = {};
1005 out = kvzalloc(outlen, GFP_KERNEL);
1007 return ERR_PTR(-ENOMEM);
1009 MLX5_SET(query_esw_functions_in, in, opcode,
1010 MLX5_CMD_OP_QUERY_ESW_FUNCTIONS);
1012 err = mlx5_cmd_exec(dev, in, sizeof(in), out, outlen);
1017 return ERR_PTR(err);
1020 static void mlx5_eswitch_event_handlers_register(struct mlx5_eswitch *esw)
1022 MLX5_NB_INIT(&esw->nb, eswitch_vport_event, NIC_VPORT_CHANGE);
1023 mlx5_eq_notifier_register(esw->dev, &esw->nb);
1025 if (esw->mode == MLX5_ESWITCH_OFFLOADS && mlx5_eswitch_is_funcs_handler(esw->dev)) {
1026 MLX5_NB_INIT(&esw->esw_funcs.nb, mlx5_esw_funcs_changed_handler,
1027 ESW_FUNCTIONS_CHANGED);
1028 mlx5_eq_notifier_register(esw->dev, &esw->esw_funcs.nb);
1032 static void mlx5_eswitch_event_handlers_unregister(struct mlx5_eswitch *esw)
1034 if (esw->mode == MLX5_ESWITCH_OFFLOADS && mlx5_eswitch_is_funcs_handler(esw->dev))
1035 mlx5_eq_notifier_unregister(esw->dev, &esw->esw_funcs.nb);
1037 mlx5_eq_notifier_unregister(esw->dev, &esw->nb);
1039 flush_workqueue(esw->work_queue);
1042 static void mlx5_eswitch_clear_vf_vports_info(struct mlx5_eswitch *esw)
1044 struct mlx5_vport *vport;
1047 mlx5_esw_for_each_vf_vport(esw, i, vport, esw->esw_funcs.num_vfs) {
1048 memset(&vport->qos, 0, sizeof(vport->qos));
1049 memset(&vport->info, 0, sizeof(vport->info));
1050 vport->info.link_state = MLX5_VPORT_ADMIN_STATE_AUTO;
1054 /* Public E-Switch API */
1055 int mlx5_eswitch_load_vport(struct mlx5_eswitch *esw, u16 vport_num,
1056 enum mlx5_eswitch_vport_event enabled_events)
1060 err = mlx5_esw_vport_enable(esw, vport_num, enabled_events);
1064 err = esw_offloads_load_rep(esw, vport_num);
1071 mlx5_esw_vport_disable(esw, vport_num);
1075 void mlx5_eswitch_unload_vport(struct mlx5_eswitch *esw, u16 vport_num)
1077 esw_offloads_unload_rep(esw, vport_num);
1078 mlx5_esw_vport_disable(esw, vport_num);
1081 void mlx5_eswitch_unload_vf_vports(struct mlx5_eswitch *esw, u16 num_vfs)
1083 struct mlx5_vport *vport;
1086 mlx5_esw_for_each_vf_vport(esw, i, vport, num_vfs) {
1087 if (!vport->enabled)
1089 mlx5_eswitch_unload_vport(esw, vport->vport);
1093 int mlx5_eswitch_load_vf_vports(struct mlx5_eswitch *esw, u16 num_vfs,
1094 enum mlx5_eswitch_vport_event enabled_events)
1096 struct mlx5_vport *vport;
1100 mlx5_esw_for_each_vf_vport(esw, i, vport, num_vfs) {
1101 err = mlx5_eswitch_load_vport(esw, vport->vport, enabled_events);
1109 mlx5_eswitch_unload_vf_vports(esw, num_vfs);
1113 static int host_pf_enable_hca(struct mlx5_core_dev *dev)
1115 if (!mlx5_core_is_ecpf(dev))
1118 /* Once vport and representor are ready, take out the external host PF
1119 * out of initializing state. Enabling HCA clears the iser->initializing
1120 * bit and host PF driver loading can progress.
1122 return mlx5_cmd_host_pf_enable_hca(dev);
1125 static void host_pf_disable_hca(struct mlx5_core_dev *dev)
1127 if (!mlx5_core_is_ecpf(dev))
1130 mlx5_cmd_host_pf_disable_hca(dev);
1133 /* mlx5_eswitch_enable_pf_vf_vports() enables vports of PF, ECPF and VFs
1134 * whichever are present on the eswitch.
1137 mlx5_eswitch_enable_pf_vf_vports(struct mlx5_eswitch *esw,
1138 enum mlx5_eswitch_vport_event enabled_events)
1142 /* Enable PF vport */
1143 ret = mlx5_eswitch_load_vport(esw, MLX5_VPORT_PF, enabled_events);
1147 /* Enable external host PF HCA */
1148 ret = host_pf_enable_hca(esw->dev);
1152 /* Enable ECPF vport */
1153 if (mlx5_ecpf_vport_exists(esw->dev)) {
1154 ret = mlx5_eswitch_load_vport(esw, MLX5_VPORT_ECPF, enabled_events);
1159 /* Enable VF vports */
1160 ret = mlx5_eswitch_load_vf_vports(esw, esw->esw_funcs.num_vfs,
1167 if (mlx5_ecpf_vport_exists(esw->dev))
1168 mlx5_eswitch_unload_vport(esw, MLX5_VPORT_ECPF);
1170 host_pf_disable_hca(esw->dev);
1172 mlx5_eswitch_unload_vport(esw, MLX5_VPORT_PF);
1176 /* mlx5_eswitch_disable_pf_vf_vports() disables vports of PF, ECPF and VFs
1177 * whichever are previously enabled on the eswitch.
1179 void mlx5_eswitch_disable_pf_vf_vports(struct mlx5_eswitch *esw)
1181 mlx5_eswitch_unload_vf_vports(esw, esw->esw_funcs.num_vfs);
1183 if (mlx5_ecpf_vport_exists(esw->dev))
1184 mlx5_eswitch_unload_vport(esw, MLX5_VPORT_ECPF);
1186 host_pf_disable_hca(esw->dev);
1187 mlx5_eswitch_unload_vport(esw, MLX5_VPORT_PF);
1190 static void mlx5_eswitch_get_devlink_param(struct mlx5_eswitch *esw)
1192 struct devlink *devlink = priv_to_devlink(esw->dev);
1193 union devlink_param_value val;
1196 err = devl_param_driverinit_value_get(devlink,
1197 MLX5_DEVLINK_PARAM_ID_ESW_LARGE_GROUP_NUM,
1200 esw->params.large_group_num = val.vu32;
1203 "Devlink can't get param fdb_large_groups, uses default (%d).\n",
1204 ESW_OFFLOADS_DEFAULT_NUM_GROUPS);
1205 esw->params.large_group_num = ESW_OFFLOADS_DEFAULT_NUM_GROUPS;
1210 mlx5_eswitch_update_num_of_vfs(struct mlx5_eswitch *esw, int num_vfs)
1217 if (!mlx5_core_is_ecpf_esw_manager(esw->dev)) {
1218 esw->esw_funcs.num_vfs = num_vfs;
1222 out = mlx5_esw_query_functions(esw->dev);
1226 esw->esw_funcs.num_vfs = MLX5_GET(query_esw_functions_out, out,
1227 host_params_context.host_num_of_vfs);
1231 static void mlx5_esw_mode_change_notify(struct mlx5_eswitch *esw, u16 mode)
1233 struct mlx5_esw_event_info info = {};
1235 info.new_mode = mode;
1237 blocking_notifier_call_chain(&esw->n_head, 0, &info);
1240 static int mlx5_esw_acls_ns_init(struct mlx5_eswitch *esw)
1242 struct mlx5_core_dev *dev = esw->dev;
1246 if (esw->flags & MLX5_ESWITCH_VPORT_ACL_NS_CREATED)
1249 total_vports = mlx5_eswitch_get_total_vports(dev);
1251 if (MLX5_CAP_ESW_EGRESS_ACL(dev, ft_support)) {
1252 err = mlx5_fs_egress_acls_init(dev, total_vports);
1256 esw_warn(dev, "egress ACL is not supported by FW\n");
1259 if (MLX5_CAP_ESW_INGRESS_ACL(dev, ft_support)) {
1260 err = mlx5_fs_ingress_acls_init(dev, total_vports);
1264 esw_warn(dev, "ingress ACL is not supported by FW\n");
1266 esw->flags |= MLX5_ESWITCH_VPORT_ACL_NS_CREATED;
1270 if (MLX5_CAP_ESW_EGRESS_ACL(dev, ft_support))
1271 mlx5_fs_egress_acls_cleanup(dev);
1275 static void mlx5_esw_acls_ns_cleanup(struct mlx5_eswitch *esw)
1277 struct mlx5_core_dev *dev = esw->dev;
1279 esw->flags &= ~MLX5_ESWITCH_VPORT_ACL_NS_CREATED;
1280 if (MLX5_CAP_ESW_INGRESS_ACL(dev, ft_support))
1281 mlx5_fs_ingress_acls_cleanup(dev);
1282 if (MLX5_CAP_ESW_EGRESS_ACL(dev, ft_support))
1283 mlx5_fs_egress_acls_cleanup(dev);
1287 * mlx5_eswitch_enable_locked - Enable eswitch
1288 * @esw: Pointer to eswitch
1289 * @num_vfs: Enable eswitch for given number of VFs. This is optional.
1290 * Valid value are 0, > 0 and MLX5_ESWITCH_IGNORE_NUM_VFS.
1291 * Caller should pass num_vfs > 0 when enabling eswitch for
1292 * vf vports. Caller should pass num_vfs = 0, when eswitch
1293 * is enabled without sriov VFs or when caller
1294 * is unaware of the sriov state of the host PF on ECPF based
1295 * eswitch. Caller should pass < 0 when num_vfs should be
1296 * completely ignored. This is typically the case when eswitch
1297 * is enabled without sriov regardless of PF/ECPF system.
1298 * mlx5_eswitch_enable_locked() Enables eswitch in either legacy or offloads
1299 * mode. If num_vfs >=0 is provided, it setup VF related eswitch vports.
1300 * It returns 0 on success or error code on failure.
1302 int mlx5_eswitch_enable_locked(struct mlx5_eswitch *esw, int num_vfs)
1306 lockdep_assert_held(&esw->mode_lock);
1308 if (!MLX5_CAP_ESW_FLOWTABLE_FDB(esw->dev, ft_support)) {
1309 esw_warn(esw->dev, "FDB is not supported, aborting ...\n");
1313 mlx5_eswitch_get_devlink_param(esw);
1315 err = mlx5_esw_acls_ns_init(esw);
1319 mlx5_eswitch_update_num_of_vfs(esw, num_vfs);
1321 if (esw->mode == MLX5_ESWITCH_LEGACY) {
1322 err = esw_legacy_enable(esw);
1324 mlx5_rescan_drivers(esw->dev);
1325 err = esw_offloads_enable(esw);
1331 esw->fdb_table.flags |= MLX5_ESW_FDB_CREATED;
1333 mlx5_eswitch_event_handlers_register(esw);
1335 esw_info(esw->dev, "Enable: mode(%s), nvfs(%d), active vports(%d)\n",
1336 esw->mode == MLX5_ESWITCH_LEGACY ? "LEGACY" : "OFFLOADS",
1337 esw->esw_funcs.num_vfs, esw->enabled_vports);
1339 mlx5_esw_mode_change_notify(esw, esw->mode);
1344 mlx5_esw_acls_ns_cleanup(esw);
1349 * mlx5_eswitch_enable - Enable eswitch
1350 * @esw: Pointer to eswitch
1351 * @num_vfs: Enable eswitch switch for given number of VFs.
1352 * Caller must pass num_vfs > 0 when enabling eswitch for
1354 * mlx5_eswitch_enable() returns 0 on success or error code on failure.
1356 int mlx5_eswitch_enable(struct mlx5_eswitch *esw, int num_vfs)
1361 if (!mlx5_esw_allowed(esw))
1364 devl_assert_locked(priv_to_devlink(esw->dev));
1366 toggle_lag = !mlx5_esw_is_fdb_created(esw);
1369 mlx5_lag_disable_change(esw->dev);
1371 down_write(&esw->mode_lock);
1372 if (!mlx5_esw_is_fdb_created(esw)) {
1373 ret = mlx5_eswitch_enable_locked(esw, num_vfs);
1375 enum mlx5_eswitch_vport_event vport_events;
1377 vport_events = (esw->mode == MLX5_ESWITCH_LEGACY) ?
1378 MLX5_LEGACY_SRIOV_VPORT_EVENTS : MLX5_VPORT_UC_ADDR_CHANGE;
1379 ret = mlx5_eswitch_load_vf_vports(esw, num_vfs, vport_events);
1381 esw->esw_funcs.num_vfs = num_vfs;
1383 up_write(&esw->mode_lock);
1386 mlx5_lag_enable_change(esw->dev);
1391 /* When disabling sriov, free driver level resources. */
1392 void mlx5_eswitch_disable_sriov(struct mlx5_eswitch *esw, bool clear_vf)
1394 if (!mlx5_esw_allowed(esw))
1397 devl_assert_locked(priv_to_devlink(esw->dev));
1398 down_write(&esw->mode_lock);
1399 /* If driver is unloaded, this function is called twice by remove_one()
1400 * and mlx5_unload(). Prevent the second call.
1402 if (!esw->esw_funcs.num_vfs && !clear_vf)
1405 esw_info(esw->dev, "Unload vfs: mode(%s), nvfs(%d), active vports(%d)\n",
1406 esw->mode == MLX5_ESWITCH_LEGACY ? "LEGACY" : "OFFLOADS",
1407 esw->esw_funcs.num_vfs, esw->enabled_vports);
1409 mlx5_eswitch_unload_vf_vports(esw, esw->esw_funcs.num_vfs);
1411 mlx5_eswitch_clear_vf_vports_info(esw);
1413 if (esw->mode == MLX5_ESWITCH_OFFLOADS) {
1414 struct devlink *devlink = priv_to_devlink(esw->dev);
1416 devl_rate_nodes_destroy(devlink);
1418 /* Destroy legacy fdb when disabling sriov in legacy mode. */
1419 if (esw->mode == MLX5_ESWITCH_LEGACY)
1420 mlx5_eswitch_disable_locked(esw);
1422 esw->esw_funcs.num_vfs = 0;
1425 up_write(&esw->mode_lock);
1428 /* Free resources for corresponding eswitch mode. It is called by devlink
1429 * when changing eswitch mode or modprobe when unloading driver.
1431 void mlx5_eswitch_disable_locked(struct mlx5_eswitch *esw)
1433 struct devlink *devlink = priv_to_devlink(esw->dev);
1435 /* Notify eswitch users that it is exiting from current mode.
1436 * So that it can do necessary cleanup before the eswitch is disabled.
1438 mlx5_esw_mode_change_notify(esw, MLX5_ESWITCH_LEGACY);
1440 mlx5_eswitch_event_handlers_unregister(esw);
1442 esw_info(esw->dev, "Disable: mode(%s), nvfs(%d), active vports(%d)\n",
1443 esw->mode == MLX5_ESWITCH_LEGACY ? "LEGACY" : "OFFLOADS",
1444 esw->esw_funcs.num_vfs, esw->enabled_vports);
1446 if (esw->fdb_table.flags & MLX5_ESW_FDB_CREATED) {
1447 esw->fdb_table.flags &= ~MLX5_ESW_FDB_CREATED;
1448 if (esw->mode == MLX5_ESWITCH_OFFLOADS)
1449 esw_offloads_disable(esw);
1450 else if (esw->mode == MLX5_ESWITCH_LEGACY)
1451 esw_legacy_disable(esw);
1452 mlx5_esw_acls_ns_cleanup(esw);
1455 if (esw->mode == MLX5_ESWITCH_OFFLOADS)
1456 devl_rate_nodes_destroy(devlink);
1459 void mlx5_eswitch_disable(struct mlx5_eswitch *esw)
1461 if (!mlx5_esw_allowed(esw))
1464 devl_assert_locked(priv_to_devlink(esw->dev));
1465 mlx5_lag_disable_change(esw->dev);
1466 down_write(&esw->mode_lock);
1467 mlx5_eswitch_disable_locked(esw);
1468 esw->mode = MLX5_ESWITCH_LEGACY;
1469 up_write(&esw->mode_lock);
1470 mlx5_lag_enable_change(esw->dev);
1473 static int mlx5_query_hca_cap_host_pf(struct mlx5_core_dev *dev, void *out)
1475 u16 opmod = (MLX5_CAP_GENERAL << 1) | (HCA_CAP_OPMOD_GET_MAX & 0x01);
1476 u8 in[MLX5_ST_SZ_BYTES(query_hca_cap_in)] = {};
1478 MLX5_SET(query_hca_cap_in, in, opcode, MLX5_CMD_OP_QUERY_HCA_CAP);
1479 MLX5_SET(query_hca_cap_in, in, op_mod, opmod);
1480 MLX5_SET(query_hca_cap_in, in, function_id, MLX5_VPORT_PF);
1481 MLX5_SET(query_hca_cap_in, in, other_function, true);
1482 return mlx5_cmd_exec_inout(dev, query_hca_cap, in, out);
1485 int mlx5_esw_sf_max_hpf_functions(struct mlx5_core_dev *dev, u16 *max_sfs, u16 *sf_base_id)
1488 int query_out_sz = MLX5_ST_SZ_BYTES(query_hca_cap_out);
1493 if (!mlx5_core_is_ecpf(dev)) {
1498 query_ctx = kzalloc(query_out_sz, GFP_KERNEL);
1502 err = mlx5_query_hca_cap_host_pf(dev, query_ctx);
1506 hca_caps = MLX5_ADDR_OF(query_hca_cap_out, query_ctx, capability);
1507 *max_sfs = MLX5_GET(cmd_hca_cap, hca_caps, max_num_sf);
1508 *sf_base_id = MLX5_GET(cmd_hca_cap, hca_caps, sf_base_id);
1515 static int mlx5_esw_vport_alloc(struct mlx5_eswitch *esw,
1516 int index, u16 vport_num)
1518 struct mlx5_vport *vport;
1521 vport = kzalloc(sizeof(*vport), GFP_KERNEL);
1525 vport->dev = esw->dev;
1526 vport->vport = vport_num;
1527 vport->index = index;
1528 vport->info.link_state = MLX5_VPORT_ADMIN_STATE_AUTO;
1529 INIT_WORK(&vport->vport_change_handler, esw_vport_change_handler);
1530 err = xa_insert(&esw->vports, vport_num, vport, GFP_KERNEL);
1534 esw->total_vports++;
1542 static void mlx5_esw_vport_free(struct mlx5_eswitch *esw, struct mlx5_vport *vport)
1544 xa_erase(&esw->vports, vport->vport);
1548 static void mlx5_esw_vports_cleanup(struct mlx5_eswitch *esw)
1550 struct mlx5_vport *vport;
1553 mlx5_esw_for_each_vport(esw, i, vport)
1554 mlx5_esw_vport_free(esw, vport);
1555 xa_destroy(&esw->vports);
1558 static int mlx5_esw_vports_init(struct mlx5_eswitch *esw)
1560 struct mlx5_core_dev *dev = esw->dev;
1561 u16 max_host_pf_sfs;
1567 xa_init(&esw->vports);
1569 err = mlx5_esw_vport_alloc(esw, idx, MLX5_VPORT_PF);
1572 if (esw->first_host_vport == MLX5_VPORT_PF)
1573 xa_set_mark(&esw->vports, idx, MLX5_ESW_VPT_HOST_FN);
1576 for (i = 0; i < mlx5_core_max_vfs(dev); i++) {
1577 err = mlx5_esw_vport_alloc(esw, idx, idx);
1580 xa_set_mark(&esw->vports, idx, MLX5_ESW_VPT_VF);
1581 xa_set_mark(&esw->vports, idx, MLX5_ESW_VPT_HOST_FN);
1584 base_sf_num = mlx5_sf_start_function_id(dev);
1585 for (i = 0; i < mlx5_sf_max_functions(dev); i++) {
1586 err = mlx5_esw_vport_alloc(esw, idx, base_sf_num + i);
1589 xa_set_mark(&esw->vports, base_sf_num + i, MLX5_ESW_VPT_SF);
1593 err = mlx5_esw_sf_max_hpf_functions(dev, &max_host_pf_sfs, &base_sf_num);
1596 for (i = 0; i < max_host_pf_sfs; i++) {
1597 err = mlx5_esw_vport_alloc(esw, idx, base_sf_num + i);
1600 xa_set_mark(&esw->vports, base_sf_num + i, MLX5_ESW_VPT_SF);
1604 if (mlx5_ecpf_vport_exists(dev)) {
1605 err = mlx5_esw_vport_alloc(esw, idx, MLX5_VPORT_ECPF);
1610 err = mlx5_esw_vport_alloc(esw, idx, MLX5_VPORT_UPLINK);
1616 mlx5_esw_vports_cleanup(esw);
1620 int mlx5_eswitch_init(struct mlx5_core_dev *dev)
1622 struct mlx5_eswitch *esw;
1625 if (!MLX5_VPORT_MANAGER(dev) && !MLX5_ESWITCH_MANAGER(dev))
1628 esw = kzalloc(sizeof(*esw), GFP_KERNEL);
1633 esw->manager_vport = mlx5_eswitch_manager_vport(dev);
1634 esw->first_host_vport = mlx5_eswitch_first_host_vport_num(dev);
1636 esw->work_queue = create_singlethread_workqueue("mlx5_esw_wq");
1637 if (!esw->work_queue) {
1642 err = mlx5_esw_vports_init(esw);
1646 err = esw_offloads_init(esw);
1650 mutex_init(&esw->offloads.encap_tbl_lock);
1651 hash_init(esw->offloads.encap_tbl);
1652 mutex_init(&esw->offloads.decap_tbl_lock);
1653 hash_init(esw->offloads.decap_tbl);
1654 mlx5e_mod_hdr_tbl_init(&esw->offloads.mod_hdr);
1655 atomic64_set(&esw->offloads.num_flows, 0);
1656 ida_init(&esw->offloads.vport_metadata_ida);
1657 xa_init_flags(&esw->offloads.vhca_map, XA_FLAGS_ALLOC);
1658 mutex_init(&esw->state_lock);
1659 init_rwsem(&esw->mode_lock);
1660 refcount_set(&esw->qos.refcnt, 0);
1662 esw->enabled_vports = 0;
1663 esw->mode = MLX5_ESWITCH_LEGACY;
1664 esw->offloads.inline_mode = MLX5_INLINE_MODE_NONE;
1665 if (MLX5_CAP_ESW_FLOWTABLE_FDB(dev, reformat) &&
1666 MLX5_CAP_ESW_FLOWTABLE_FDB(dev, decap))
1667 esw->offloads.encap = DEVLINK_ESWITCH_ENCAP_MODE_BASIC;
1669 esw->offloads.encap = DEVLINK_ESWITCH_ENCAP_MODE_NONE;
1670 if (MLX5_ESWITCH_MANAGER(dev) &&
1671 mlx5_esw_vport_match_metadata_supported(esw))
1672 esw->flags |= MLX5_ESWITCH_VPORT_MATCH_METADATA;
1674 dev->priv.eswitch = esw;
1675 BLOCKING_INIT_NOTIFIER_HEAD(&esw->n_head);
1678 "Total vports %d, per vport: max uc(%d) max mc(%d)\n",
1680 MLX5_MAX_UC_PER_VPORT(dev),
1681 MLX5_MAX_MC_PER_VPORT(dev));
1685 mlx5_esw_vports_cleanup(esw);
1687 if (esw->work_queue)
1688 destroy_workqueue(esw->work_queue);
1693 void mlx5_eswitch_cleanup(struct mlx5_eswitch *esw)
1698 esw_info(esw->dev, "cleanup\n");
1700 esw->dev->priv.eswitch = NULL;
1701 destroy_workqueue(esw->work_queue);
1702 WARN_ON(refcount_read(&esw->qos.refcnt));
1703 mutex_destroy(&esw->state_lock);
1704 WARN_ON(!xa_empty(&esw->offloads.vhca_map));
1705 xa_destroy(&esw->offloads.vhca_map);
1706 ida_destroy(&esw->offloads.vport_metadata_ida);
1707 mlx5e_mod_hdr_tbl_destroy(&esw->offloads.mod_hdr);
1708 mutex_destroy(&esw->offloads.encap_tbl_lock);
1709 mutex_destroy(&esw->offloads.decap_tbl_lock);
1710 esw_offloads_cleanup(esw);
1711 mlx5_esw_vports_cleanup(esw);
1715 /* Vport Administration */
1717 mlx5_esw_set_vport_mac_locked(struct mlx5_eswitch *esw,
1718 struct mlx5_vport *evport, const u8 *mac)
1720 u16 vport_num = evport->vport;
1724 if (is_multicast_ether_addr(mac))
1727 if (evport->info.spoofchk && !is_valid_ether_addr(mac))
1728 mlx5_core_warn(esw->dev,
1729 "Set invalid MAC while spoofchk is on, vport(%d)\n",
1732 err = mlx5_modify_nic_vport_mac_address(esw->dev, vport_num, mac);
1734 mlx5_core_warn(esw->dev,
1735 "Failed to mlx5_modify_nic_vport_mac vport(%d) err=(%d)\n",
1740 node_guid_gen_from_mac(&node_guid, mac);
1741 err = mlx5_modify_nic_vport_node_guid(esw->dev, vport_num, node_guid);
1743 mlx5_core_warn(esw->dev,
1744 "Failed to set vport %d node guid, err = %d. RDMA_CM will not function properly for this VF.\n",
1747 ether_addr_copy(evport->info.mac, mac);
1748 evport->info.node_guid = node_guid;
1749 if (evport->enabled && esw->mode == MLX5_ESWITCH_LEGACY)
1750 err = esw_acl_ingress_lgcy_setup(esw, evport);
1755 int mlx5_eswitch_set_vport_mac(struct mlx5_eswitch *esw,
1756 u16 vport, const u8 *mac)
1758 struct mlx5_vport *evport = mlx5_eswitch_get_vport(esw, vport);
1762 return PTR_ERR(evport);
1764 mutex_lock(&esw->state_lock);
1765 err = mlx5_esw_set_vport_mac_locked(esw, evport, mac);
1766 mutex_unlock(&esw->state_lock);
1770 static bool mlx5_esw_check_port_type(struct mlx5_eswitch *esw, u16 vport_num, xa_mark_t mark)
1772 struct mlx5_vport *vport;
1774 vport = mlx5_eswitch_get_vport(esw, vport_num);
1778 return xa_get_mark(&esw->vports, vport_num, mark);
1781 bool mlx5_eswitch_is_vf_vport(struct mlx5_eswitch *esw, u16 vport_num)
1783 return mlx5_esw_check_port_type(esw, vport_num, MLX5_ESW_VPT_VF);
1786 bool mlx5_esw_is_sf_vport(struct mlx5_eswitch *esw, u16 vport_num)
1788 return mlx5_esw_check_port_type(esw, vport_num, MLX5_ESW_VPT_SF);
1791 int mlx5_eswitch_set_vport_state(struct mlx5_eswitch *esw,
1792 u16 vport, int link_state)
1794 struct mlx5_vport *evport = mlx5_eswitch_get_vport(esw, vport);
1795 int opmod = MLX5_VPORT_STATE_OP_MOD_ESW_VPORT;
1796 int other_vport = 1;
1799 if (!mlx5_esw_allowed(esw))
1802 return PTR_ERR(evport);
1804 if (vport == MLX5_VPORT_UPLINK) {
1805 opmod = MLX5_VPORT_STATE_OP_MOD_UPLINK;
1809 mutex_lock(&esw->state_lock);
1810 if (esw->mode != MLX5_ESWITCH_LEGACY) {
1815 err = mlx5_modify_vport_admin_state(esw->dev, opmod, vport, other_vport, link_state);
1817 mlx5_core_warn(esw->dev, "Failed to set vport %d link state, opmod = %d, err = %d",
1822 evport->info.link_state = link_state;
1825 mutex_unlock(&esw->state_lock);
1829 int mlx5_eswitch_get_vport_config(struct mlx5_eswitch *esw,
1830 u16 vport, struct ifla_vf_info *ivi)
1832 struct mlx5_vport *evport = mlx5_eswitch_get_vport(esw, vport);
1835 return PTR_ERR(evport);
1837 memset(ivi, 0, sizeof(*ivi));
1838 ivi->vf = vport - 1;
1840 mutex_lock(&esw->state_lock);
1841 ether_addr_copy(ivi->mac, evport->info.mac);
1842 ivi->linkstate = evport->info.link_state;
1843 ivi->vlan = evport->info.vlan;
1844 ivi->qos = evport->info.qos;
1845 ivi->spoofchk = evport->info.spoofchk;
1846 ivi->trusted = evport->info.trusted;
1847 if (evport->qos.enabled) {
1848 ivi->min_tx_rate = evport->qos.min_rate;
1849 ivi->max_tx_rate = evport->qos.max_rate;
1851 mutex_unlock(&esw->state_lock);
1856 int __mlx5_eswitch_set_vport_vlan(struct mlx5_eswitch *esw,
1857 u16 vport, u16 vlan, u8 qos, u8 set_flags)
1859 struct mlx5_vport *evport = mlx5_eswitch_get_vport(esw, vport);
1860 bool vst_mode_steering = esw_vst_mode_is_steering(esw);
1864 return PTR_ERR(evport);
1865 if (vlan > 4095 || qos > 7)
1868 if (esw->mode == MLX5_ESWITCH_OFFLOADS || !vst_mode_steering) {
1869 err = modify_esw_vport_cvlan(esw->dev, vport, vlan, qos, set_flags);
1874 evport->info.vlan = vlan;
1875 evport->info.qos = qos;
1876 if (evport->enabled && esw->mode == MLX5_ESWITCH_LEGACY) {
1877 err = esw_acl_ingress_lgcy_setup(esw, evport);
1880 err = esw_acl_egress_lgcy_setup(esw, evport);
1886 int mlx5_eswitch_get_vport_stats(struct mlx5_eswitch *esw,
1888 struct ifla_vf_stats *vf_stats)
1890 struct mlx5_vport *vport = mlx5_eswitch_get_vport(esw, vport_num);
1891 int outlen = MLX5_ST_SZ_BYTES(query_vport_counter_out);
1892 u32 in[MLX5_ST_SZ_DW(query_vport_counter_in)] = {};
1893 struct mlx5_vport_drop_stats stats = {};
1898 return PTR_ERR(vport);
1900 out = kvzalloc(outlen, GFP_KERNEL);
1904 MLX5_SET(query_vport_counter_in, in, opcode,
1905 MLX5_CMD_OP_QUERY_VPORT_COUNTER);
1906 MLX5_SET(query_vport_counter_in, in, op_mod, 0);
1907 MLX5_SET(query_vport_counter_in, in, vport_number, vport->vport);
1908 MLX5_SET(query_vport_counter_in, in, other_vport, 1);
1910 err = mlx5_cmd_exec_inout(esw->dev, query_vport_counter, in, out);
1914 #define MLX5_GET_CTR(p, x) \
1915 MLX5_GET64(query_vport_counter_out, p, x)
1917 memset(vf_stats, 0, sizeof(*vf_stats));
1918 vf_stats->rx_packets =
1919 MLX5_GET_CTR(out, received_eth_unicast.packets) +
1920 MLX5_GET_CTR(out, received_ib_unicast.packets) +
1921 MLX5_GET_CTR(out, received_eth_multicast.packets) +
1922 MLX5_GET_CTR(out, received_ib_multicast.packets) +
1923 MLX5_GET_CTR(out, received_eth_broadcast.packets);
1925 vf_stats->rx_bytes =
1926 MLX5_GET_CTR(out, received_eth_unicast.octets) +
1927 MLX5_GET_CTR(out, received_ib_unicast.octets) +
1928 MLX5_GET_CTR(out, received_eth_multicast.octets) +
1929 MLX5_GET_CTR(out, received_ib_multicast.octets) +
1930 MLX5_GET_CTR(out, received_eth_broadcast.octets);
1932 vf_stats->tx_packets =
1933 MLX5_GET_CTR(out, transmitted_eth_unicast.packets) +
1934 MLX5_GET_CTR(out, transmitted_ib_unicast.packets) +
1935 MLX5_GET_CTR(out, transmitted_eth_multicast.packets) +
1936 MLX5_GET_CTR(out, transmitted_ib_multicast.packets) +
1937 MLX5_GET_CTR(out, transmitted_eth_broadcast.packets);
1939 vf_stats->tx_bytes =
1940 MLX5_GET_CTR(out, transmitted_eth_unicast.octets) +
1941 MLX5_GET_CTR(out, transmitted_ib_unicast.octets) +
1942 MLX5_GET_CTR(out, transmitted_eth_multicast.octets) +
1943 MLX5_GET_CTR(out, transmitted_ib_multicast.octets) +
1944 MLX5_GET_CTR(out, transmitted_eth_broadcast.octets);
1946 vf_stats->multicast =
1947 MLX5_GET_CTR(out, received_eth_multicast.packets) +
1948 MLX5_GET_CTR(out, received_ib_multicast.packets);
1950 vf_stats->broadcast =
1951 MLX5_GET_CTR(out, received_eth_broadcast.packets);
1953 err = mlx5_esw_query_vport_drop_stats(esw->dev, vport, &stats);
1956 vf_stats->rx_dropped = stats.rx_dropped;
1957 vf_stats->tx_dropped = stats.tx_dropped;
1964 u8 mlx5_eswitch_mode(const struct mlx5_core_dev *dev)
1966 struct mlx5_eswitch *esw = dev->priv.eswitch;
1968 return mlx5_esw_allowed(esw) ? esw->mode : MLX5_ESWITCH_LEGACY;
1970 EXPORT_SYMBOL_GPL(mlx5_eswitch_mode);
1972 enum devlink_eswitch_encap_mode
1973 mlx5_eswitch_get_encap_mode(const struct mlx5_core_dev *dev)
1975 struct mlx5_eswitch *esw;
1977 esw = dev->priv.eswitch;
1978 return (mlx5_eswitch_mode(dev) == MLX5_ESWITCH_OFFLOADS) ? esw->offloads.encap :
1979 DEVLINK_ESWITCH_ENCAP_MODE_NONE;
1981 EXPORT_SYMBOL(mlx5_eswitch_get_encap_mode);
1983 bool mlx5_esw_multipath_prereq(struct mlx5_core_dev *dev0,
1984 struct mlx5_core_dev *dev1)
1986 return (dev0->priv.eswitch->mode == MLX5_ESWITCH_OFFLOADS &&
1987 dev1->priv.eswitch->mode == MLX5_ESWITCH_OFFLOADS);
1990 int mlx5_esw_event_notifier_register(struct mlx5_eswitch *esw, struct notifier_block *nb)
1992 return blocking_notifier_chain_register(&esw->n_head, nb);
1995 void mlx5_esw_event_notifier_unregister(struct mlx5_eswitch *esw, struct notifier_block *nb)
1997 blocking_notifier_chain_unregister(&esw->n_head, nb);
2001 * mlx5_esw_hold() - Try to take a read lock on esw mode lock.
2002 * @mdev: mlx5 core device.
2004 * Should be called by esw resources callers.
2006 * Return: true on success or false.
2008 bool mlx5_esw_hold(struct mlx5_core_dev *mdev)
2010 struct mlx5_eswitch *esw = mdev->priv.eswitch;
2012 /* e.g. VF doesn't have eswitch so nothing to do */
2013 if (!mlx5_esw_allowed(esw))
2016 if (down_read_trylock(&esw->mode_lock) != 0)
2023 * mlx5_esw_release() - Release a read lock on esw mode lock.
2024 * @mdev: mlx5 core device.
2026 void mlx5_esw_release(struct mlx5_core_dev *mdev)
2028 struct mlx5_eswitch *esw = mdev->priv.eswitch;
2030 if (mlx5_esw_allowed(esw))
2031 up_read(&esw->mode_lock);
2035 * mlx5_esw_get() - Increase esw user count.
2036 * @mdev: mlx5 core device.
2038 void mlx5_esw_get(struct mlx5_core_dev *mdev)
2040 struct mlx5_eswitch *esw = mdev->priv.eswitch;
2042 if (mlx5_esw_allowed(esw))
2043 atomic64_inc(&esw->user_count);
2047 * mlx5_esw_put() - Decrease esw user count.
2048 * @mdev: mlx5 core device.
2050 void mlx5_esw_put(struct mlx5_core_dev *mdev)
2052 struct mlx5_eswitch *esw = mdev->priv.eswitch;
2054 if (mlx5_esw_allowed(esw))
2055 atomic64_dec_if_positive(&esw->user_count);
2059 * mlx5_esw_try_lock() - Take a write lock on esw mode lock.
2060 * @esw: eswitch device.
2062 * Should be called by esw mode change routine.
2065 * * 0 - esw mode if successfully locked and refcount is 0.
2066 * * -EBUSY - refcount is not 0.
2067 * * -EINVAL - In the middle of switching mode or lock is already held.
2069 int mlx5_esw_try_lock(struct mlx5_eswitch *esw)
2071 if (down_write_trylock(&esw->mode_lock) == 0)
2074 if (atomic64_read(&esw->user_count) > 0) {
2075 up_write(&esw->mode_lock);
2083 * mlx5_esw_unlock() - Release write lock on esw mode lock
2084 * @esw: eswitch device.
2086 void mlx5_esw_unlock(struct mlx5_eswitch *esw)
2088 up_write(&esw->mode_lock);
2092 * mlx5_eswitch_get_total_vports - Get total vports of the eswitch
2094 * @dev: Pointer to core device
2096 * mlx5_eswitch_get_total_vports returns total number of eswitch vports.
2098 u16 mlx5_eswitch_get_total_vports(const struct mlx5_core_dev *dev)
2100 struct mlx5_eswitch *esw;
2102 esw = dev->priv.eswitch;
2103 return mlx5_esw_allowed(esw) ? esw->total_vports : 0;
2105 EXPORT_SYMBOL_GPL(mlx5_eswitch_get_total_vports);
2108 * mlx5_eswitch_get_core_dev - Get the mdev device
2109 * @esw : eswitch device.
2111 * Return the mellanox core device which manages the eswitch.
2113 struct mlx5_core_dev *mlx5_eswitch_get_core_dev(struct mlx5_eswitch *esw)
2115 return mlx5_esw_allowed(esw) ? esw->dev : NULL;
2117 EXPORT_SYMBOL(mlx5_eswitch_get_core_dev);