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 "mlx5_core.h"
43 #define UPLINK_VPORT 0xFFFF
51 /* Vport UC/MC hash node */
53 struct l2addr_node node
;
56 struct mlx5_flow_handle
*flow_rule
;
57 bool mpfs
; /* UC MAC was added to MPFs */
58 /* A flag indicating that mac was added due to mc promiscuous vport */
63 UC_ADDR_CHANGE
= BIT(0),
64 MC_ADDR_CHANGE
= BIT(1),
65 PROMISC_CHANGE
= BIT(3),
68 /* Vport context events */
69 #define SRIOV_VPORT_EVENTS (UC_ADDR_CHANGE | \
73 static int arm_vport_context_events_cmd(struct mlx5_core_dev
*dev
, u16 vport
,
76 int in
[MLX5_ST_SZ_DW(modify_nic_vport_context_in
)] = {0};
77 int out
[MLX5_ST_SZ_DW(modify_nic_vport_context_out
)] = {0};
80 MLX5_SET(modify_nic_vport_context_in
, in
,
81 opcode
, MLX5_CMD_OP_MODIFY_NIC_VPORT_CONTEXT
);
82 MLX5_SET(modify_nic_vport_context_in
, in
, field_select
.change_event
, 1);
83 MLX5_SET(modify_nic_vport_context_in
, in
, vport_number
, vport
);
85 MLX5_SET(modify_nic_vport_context_in
, in
, other_vport
, 1);
86 nic_vport_ctx
= MLX5_ADDR_OF(modify_nic_vport_context_in
,
87 in
, nic_vport_context
);
89 MLX5_SET(nic_vport_context
, nic_vport_ctx
, arm_change_event
, 1);
91 if (events_mask
& UC_ADDR_CHANGE
)
92 MLX5_SET(nic_vport_context
, nic_vport_ctx
,
93 event_on_uc_address_change
, 1);
94 if (events_mask
& MC_ADDR_CHANGE
)
95 MLX5_SET(nic_vport_context
, nic_vport_ctx
,
96 event_on_mc_address_change
, 1);
97 if (events_mask
& PROMISC_CHANGE
)
98 MLX5_SET(nic_vport_context
, nic_vport_ctx
,
99 event_on_promisc_change
, 1);
101 return mlx5_cmd_exec(dev
, in
, sizeof(in
), out
, sizeof(out
));
104 /* E-Switch vport context HW commands */
105 static int modify_esw_vport_context_cmd(struct mlx5_core_dev
*dev
, u16 vport
,
108 u32 out
[MLX5_ST_SZ_DW(modify_esw_vport_context_out
)] = {0};
110 MLX5_SET(modify_esw_vport_context_in
, in
, opcode
,
111 MLX5_CMD_OP_MODIFY_ESW_VPORT_CONTEXT
);
112 MLX5_SET(modify_esw_vport_context_in
, in
, vport_number
, vport
);
114 MLX5_SET(modify_esw_vport_context_in
, in
, other_vport
, 1);
115 return mlx5_cmd_exec(dev
, in
, inlen
, out
, sizeof(out
));
118 static int modify_esw_vport_cvlan(struct mlx5_core_dev
*dev
, u32 vport
,
119 u16 vlan
, u8 qos
, u8 set_flags
)
121 u32 in
[MLX5_ST_SZ_DW(modify_esw_vport_context_in
)] = {0};
123 if (!MLX5_CAP_ESW(dev
, vport_cvlan_strip
) ||
124 !MLX5_CAP_ESW(dev
, vport_cvlan_insert_if_not_exist
))
127 esw_debug(dev
, "Set Vport[%d] VLAN %d qos %d set=%x\n",
128 vport
, vlan
, qos
, set_flags
);
130 if (set_flags
& SET_VLAN_STRIP
)
131 MLX5_SET(modify_esw_vport_context_in
, in
,
132 esw_vport_context
.vport_cvlan_strip
, 1);
134 if (set_flags
& SET_VLAN_INSERT
) {
135 /* insert only if no vlan in packet */
136 MLX5_SET(modify_esw_vport_context_in
, in
,
137 esw_vport_context
.vport_cvlan_insert
, 1);
139 MLX5_SET(modify_esw_vport_context_in
, in
,
140 esw_vport_context
.cvlan_pcp
, qos
);
141 MLX5_SET(modify_esw_vport_context_in
, in
,
142 esw_vport_context
.cvlan_id
, vlan
);
145 MLX5_SET(modify_esw_vport_context_in
, in
,
146 field_select
.vport_cvlan_strip
, 1);
147 MLX5_SET(modify_esw_vport_context_in
, in
,
148 field_select
.vport_cvlan_insert
, 1);
150 return modify_esw_vport_context_cmd(dev
, vport
, in
, sizeof(in
));
154 static struct mlx5_flow_handle
*
155 __esw_fdb_set_vport_rule(struct mlx5_eswitch
*esw
, u32 vport
, bool rx_rule
,
156 u8 mac_c
[ETH_ALEN
], u8 mac_v
[ETH_ALEN
])
158 int match_header
= (is_zero_ether_addr(mac_c
) ? 0 :
159 MLX5_MATCH_OUTER_HEADERS
);
160 struct mlx5_flow_handle
*flow_rule
= NULL
;
161 struct mlx5_flow_act flow_act
= {0};
162 struct mlx5_flow_destination dest
= {};
163 struct mlx5_flow_spec
*spec
;
164 void *mv_misc
= NULL
;
165 void *mc_misc
= NULL
;
170 match_header
|= MLX5_MATCH_MISC_PARAMETERS
;
172 spec
= kvzalloc(sizeof(*spec
), GFP_KERNEL
);
176 dmac_v
= MLX5_ADDR_OF(fte_match_param
, spec
->match_value
,
177 outer_headers
.dmac_47_16
);
178 dmac_c
= MLX5_ADDR_OF(fte_match_param
, spec
->match_criteria
,
179 outer_headers
.dmac_47_16
);
181 if (match_header
& MLX5_MATCH_OUTER_HEADERS
) {
182 ether_addr_copy(dmac_v
, mac_v
);
183 ether_addr_copy(dmac_c
, mac_c
);
186 if (match_header
& MLX5_MATCH_MISC_PARAMETERS
) {
187 mv_misc
= MLX5_ADDR_OF(fte_match_param
, spec
->match_value
,
189 mc_misc
= MLX5_ADDR_OF(fte_match_param
, spec
->match_criteria
,
191 MLX5_SET(fte_match_set_misc
, mv_misc
, source_port
, UPLINK_VPORT
);
192 MLX5_SET_TO_ONES(fte_match_set_misc
, mc_misc
, source_port
);
195 dest
.type
= MLX5_FLOW_DESTINATION_TYPE_VPORT
;
196 dest
.vport
.num
= vport
;
199 "\tFDB add rule dmac_v(%pM) dmac_c(%pM) -> vport(%d)\n",
200 dmac_v
, dmac_c
, vport
);
201 spec
->match_criteria_enable
= match_header
;
202 flow_act
.action
= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST
;
204 mlx5_add_flow_rules(esw
->fdb_table
.legacy
.fdb
, spec
,
205 &flow_act
, &dest
, 1);
206 if (IS_ERR(flow_rule
)) {
208 "FDB: Failed to add flow rule: dmac_v(%pM) dmac_c(%pM) -> vport(%d), err(%ld)\n",
209 dmac_v
, dmac_c
, vport
, PTR_ERR(flow_rule
));
217 static struct mlx5_flow_handle
*
218 esw_fdb_set_vport_rule(struct mlx5_eswitch
*esw
, u8 mac
[ETH_ALEN
], u32 vport
)
222 eth_broadcast_addr(mac_c
);
223 return __esw_fdb_set_vport_rule(esw
, vport
, false, mac_c
, mac
);
226 static struct mlx5_flow_handle
*
227 esw_fdb_set_vport_allmulti_rule(struct mlx5_eswitch
*esw
, u32 vport
)
232 eth_zero_addr(mac_c
);
233 eth_zero_addr(mac_v
);
236 return __esw_fdb_set_vport_rule(esw
, vport
, false, mac_c
, mac_v
);
239 static struct mlx5_flow_handle
*
240 esw_fdb_set_vport_promisc_rule(struct mlx5_eswitch
*esw
, u32 vport
)
245 eth_zero_addr(mac_c
);
246 eth_zero_addr(mac_v
);
247 return __esw_fdb_set_vport_rule(esw
, vport
, true, mac_c
, mac_v
);
250 static int esw_create_legacy_fdb_table(struct mlx5_eswitch
*esw
)
252 int inlen
= MLX5_ST_SZ_BYTES(create_flow_group_in
);
253 struct mlx5_flow_table_attr ft_attr
= {};
254 struct mlx5_core_dev
*dev
= esw
->dev
;
255 struct mlx5_flow_namespace
*root_ns
;
256 struct mlx5_flow_table
*fdb
;
257 struct mlx5_flow_group
*g
;
258 void *match_criteria
;
264 esw_debug(dev
, "Create FDB log_max_size(%d)\n",
265 MLX5_CAP_ESW_FLOWTABLE_FDB(dev
, log_max_ft_size
));
267 root_ns
= mlx5_get_fdb_sub_ns(dev
, 0);
269 esw_warn(dev
, "Failed to get FDB flow namespace\n");
273 flow_group_in
= kvzalloc(inlen
, GFP_KERNEL
);
277 table_size
= BIT(MLX5_CAP_ESW_FLOWTABLE_FDB(dev
, log_max_ft_size
));
279 ft_attr
.max_fte
= table_size
;
280 fdb
= mlx5_create_flow_table(root_ns
, &ft_attr
);
283 esw_warn(dev
, "Failed to create FDB Table err %d\n", err
);
286 esw
->fdb_table
.legacy
.fdb
= fdb
;
288 /* Addresses group : Full match unicast/multicast addresses */
289 MLX5_SET(create_flow_group_in
, flow_group_in
, match_criteria_enable
,
290 MLX5_MATCH_OUTER_HEADERS
);
291 match_criteria
= MLX5_ADDR_OF(create_flow_group_in
, flow_group_in
, match_criteria
);
292 dmac
= MLX5_ADDR_OF(fte_match_param
, match_criteria
, outer_headers
.dmac_47_16
);
293 MLX5_SET(create_flow_group_in
, flow_group_in
, start_flow_index
, 0);
294 /* Preserve 2 entries for allmulti and promisc rules*/
295 MLX5_SET(create_flow_group_in
, flow_group_in
, end_flow_index
, table_size
- 3);
296 eth_broadcast_addr(dmac
);
297 g
= mlx5_create_flow_group(fdb
, flow_group_in
);
300 esw_warn(dev
, "Failed to create flow group err(%d)\n", err
);
303 esw
->fdb_table
.legacy
.addr_grp
= g
;
305 /* Allmulti group : One rule that forwards any mcast traffic */
306 MLX5_SET(create_flow_group_in
, flow_group_in
, match_criteria_enable
,
307 MLX5_MATCH_OUTER_HEADERS
);
308 MLX5_SET(create_flow_group_in
, flow_group_in
, start_flow_index
, table_size
- 2);
309 MLX5_SET(create_flow_group_in
, flow_group_in
, end_flow_index
, table_size
- 2);
312 g
= mlx5_create_flow_group(fdb
, flow_group_in
);
315 esw_warn(dev
, "Failed to create allmulti flow group err(%d)\n", err
);
318 esw
->fdb_table
.legacy
.allmulti_grp
= g
;
320 /* Promiscuous group :
321 * One rule that forward all unmatched traffic from previous groups
324 MLX5_SET(create_flow_group_in
, flow_group_in
, match_criteria_enable
,
325 MLX5_MATCH_MISC_PARAMETERS
);
326 MLX5_SET_TO_ONES(fte_match_param
, match_criteria
, misc_parameters
.source_port
);
327 MLX5_SET(create_flow_group_in
, flow_group_in
, start_flow_index
, table_size
- 1);
328 MLX5_SET(create_flow_group_in
, flow_group_in
, end_flow_index
, table_size
- 1);
329 g
= mlx5_create_flow_group(fdb
, flow_group_in
);
332 esw_warn(dev
, "Failed to create promisc flow group err(%d)\n", err
);
335 esw
->fdb_table
.legacy
.promisc_grp
= g
;
339 if (!IS_ERR_OR_NULL(esw
->fdb_table
.legacy
.allmulti_grp
)) {
340 mlx5_destroy_flow_group(esw
->fdb_table
.legacy
.allmulti_grp
);
341 esw
->fdb_table
.legacy
.allmulti_grp
= NULL
;
343 if (!IS_ERR_OR_NULL(esw
->fdb_table
.legacy
.addr_grp
)) {
344 mlx5_destroy_flow_group(esw
->fdb_table
.legacy
.addr_grp
);
345 esw
->fdb_table
.legacy
.addr_grp
= NULL
;
347 if (!IS_ERR_OR_NULL(esw
->fdb_table
.legacy
.fdb
)) {
348 mlx5_destroy_flow_table(esw
->fdb_table
.legacy
.fdb
);
349 esw
->fdb_table
.legacy
.fdb
= NULL
;
353 kvfree(flow_group_in
);
357 static void esw_destroy_legacy_fdb_table(struct mlx5_eswitch
*esw
)
359 if (!esw
->fdb_table
.legacy
.fdb
)
362 esw_debug(esw
->dev
, "Destroy FDB Table\n");
363 mlx5_destroy_flow_group(esw
->fdb_table
.legacy
.promisc_grp
);
364 mlx5_destroy_flow_group(esw
->fdb_table
.legacy
.allmulti_grp
);
365 mlx5_destroy_flow_group(esw
->fdb_table
.legacy
.addr_grp
);
366 mlx5_destroy_flow_table(esw
->fdb_table
.legacy
.fdb
);
367 esw
->fdb_table
.legacy
.fdb
= NULL
;
368 esw
->fdb_table
.legacy
.addr_grp
= NULL
;
369 esw
->fdb_table
.legacy
.allmulti_grp
= NULL
;
370 esw
->fdb_table
.legacy
.promisc_grp
= NULL
;
373 /* E-Switch vport UC/MC lists management */
374 typedef int (*vport_addr_action
)(struct mlx5_eswitch
*esw
,
375 struct vport_addr
*vaddr
);
377 static int esw_add_uc_addr(struct mlx5_eswitch
*esw
, struct vport_addr
*vaddr
)
379 u8
*mac
= vaddr
->node
.addr
;
380 u32 vport
= vaddr
->vport
;
383 /* Skip mlx5_mpfs_add_mac for PFs,
384 * it is already done by the PF netdev in mlx5e_execute_l2_action
389 err
= mlx5_mpfs_add_mac(esw
->dev
, mac
);
392 "Failed to add L2 table mac(%pM) for vport(%d), err(%d)\n",
399 /* SRIOV is enabled: Forward UC MAC to vport */
400 if (esw
->fdb_table
.legacy
.fdb
&& esw
->mode
== SRIOV_LEGACY
)
401 vaddr
->flow_rule
= esw_fdb_set_vport_rule(esw
, mac
, vport
);
403 esw_debug(esw
->dev
, "\tADDED UC MAC: vport[%d] %pM fr(%p)\n",
404 vport
, mac
, vaddr
->flow_rule
);
409 static int esw_del_uc_addr(struct mlx5_eswitch
*esw
, struct vport_addr
*vaddr
)
411 u8
*mac
= vaddr
->node
.addr
;
412 u32 vport
= vaddr
->vport
;
415 /* Skip mlx5_mpfs_del_mac for PFs,
416 * it is already done by the PF netdev in mlx5e_execute_l2_action
418 if (!vport
|| !vaddr
->mpfs
)
421 err
= mlx5_mpfs_del_mac(esw
->dev
, mac
);
424 "Failed to del L2 table mac(%pM) for vport(%d), err(%d)\n",
429 if (vaddr
->flow_rule
)
430 mlx5_del_flow_rules(vaddr
->flow_rule
);
431 vaddr
->flow_rule
= NULL
;
436 static void update_allmulti_vports(struct mlx5_eswitch
*esw
,
437 struct vport_addr
*vaddr
,
438 struct esw_mc_addr
*esw_mc
)
440 u8
*mac
= vaddr
->node
.addr
;
443 for (vport_idx
= 0; vport_idx
< esw
->total_vports
; vport_idx
++) {
444 struct mlx5_vport
*vport
= &esw
->vports
[vport_idx
];
445 struct hlist_head
*vport_hash
= vport
->mc_list
;
446 struct vport_addr
*iter_vaddr
=
447 l2addr_hash_find(vport_hash
,
450 if (IS_ERR_OR_NULL(vport
->allmulti_rule
) ||
451 vaddr
->vport
== vport_idx
)
453 switch (vaddr
->action
) {
454 case MLX5_ACTION_ADD
:
457 iter_vaddr
= l2addr_hash_add(vport_hash
, mac
,
462 "ALL-MULTI: Failed to add MAC(%pM) to vport[%d] DB\n",
466 iter_vaddr
->vport
= vport_idx
;
467 iter_vaddr
->flow_rule
=
468 esw_fdb_set_vport_rule(esw
,
471 iter_vaddr
->mc_promisc
= true;
473 case MLX5_ACTION_DEL
:
476 mlx5_del_flow_rules(iter_vaddr
->flow_rule
);
477 l2addr_hash_del(iter_vaddr
);
483 static int esw_add_mc_addr(struct mlx5_eswitch
*esw
, struct vport_addr
*vaddr
)
485 struct hlist_head
*hash
= esw
->mc_table
;
486 struct esw_mc_addr
*esw_mc
;
487 u8
*mac
= vaddr
->node
.addr
;
488 u32 vport
= vaddr
->vport
;
490 if (!esw
->fdb_table
.legacy
.fdb
)
493 esw_mc
= l2addr_hash_find(hash
, mac
, struct esw_mc_addr
);
497 esw_mc
= l2addr_hash_add(hash
, mac
, struct esw_mc_addr
, GFP_KERNEL
);
501 esw_mc
->uplink_rule
= /* Forward MC MAC to Uplink */
502 esw_fdb_set_vport_rule(esw
, mac
, UPLINK_VPORT
);
504 /* Add this multicast mac to all the mc promiscuous vports */
505 update_allmulti_vports(esw
, vaddr
, esw_mc
);
508 /* If the multicast mac is added as a result of mc promiscuous vport,
509 * don't increment the multicast ref count
511 if (!vaddr
->mc_promisc
)
514 /* Forward MC MAC to vport */
515 vaddr
->flow_rule
= esw_fdb_set_vport_rule(esw
, mac
, vport
);
517 "\tADDED MC MAC: vport[%d] %pM fr(%p) refcnt(%d) uplinkfr(%p)\n",
518 vport
, mac
, vaddr
->flow_rule
,
519 esw_mc
->refcnt
, esw_mc
->uplink_rule
);
523 static int esw_del_mc_addr(struct mlx5_eswitch
*esw
, struct vport_addr
*vaddr
)
525 struct hlist_head
*hash
= esw
->mc_table
;
526 struct esw_mc_addr
*esw_mc
;
527 u8
*mac
= vaddr
->node
.addr
;
528 u32 vport
= vaddr
->vport
;
530 if (!esw
->fdb_table
.legacy
.fdb
)
533 esw_mc
= l2addr_hash_find(hash
, mac
, struct esw_mc_addr
);
536 "Failed to find eswitch MC addr for MAC(%pM) vport(%d)",
541 "\tDELETE MC MAC: vport[%d] %pM fr(%p) refcnt(%d) uplinkfr(%p)\n",
542 vport
, mac
, vaddr
->flow_rule
, esw_mc
->refcnt
,
543 esw_mc
->uplink_rule
);
545 if (vaddr
->flow_rule
)
546 mlx5_del_flow_rules(vaddr
->flow_rule
);
547 vaddr
->flow_rule
= NULL
;
549 /* If the multicast mac is added as a result of mc promiscuous vport,
550 * don't decrement the multicast ref count.
552 if (vaddr
->mc_promisc
|| (--esw_mc
->refcnt
> 0))
555 /* Remove this multicast mac from all the mc promiscuous vports */
556 update_allmulti_vports(esw
, vaddr
, esw_mc
);
558 if (esw_mc
->uplink_rule
)
559 mlx5_del_flow_rules(esw_mc
->uplink_rule
);
561 l2addr_hash_del(esw_mc
);
565 /* Apply vport UC/MC list to HW l2 table and FDB table */
566 static void esw_apply_vport_addr_list(struct mlx5_eswitch
*esw
,
567 u32 vport_num
, int list_type
)
569 struct mlx5_vport
*vport
= &esw
->vports
[vport_num
];
570 bool is_uc
= list_type
== MLX5_NVPRT_LIST_TYPE_UC
;
571 vport_addr_action vport_addr_add
;
572 vport_addr_action vport_addr_del
;
573 struct vport_addr
*addr
;
574 struct l2addr_node
*node
;
575 struct hlist_head
*hash
;
576 struct hlist_node
*tmp
;
579 vport_addr_add
= is_uc
? esw_add_uc_addr
:
581 vport_addr_del
= is_uc
? esw_del_uc_addr
:
584 hash
= is_uc
? vport
->uc_list
: vport
->mc_list
;
585 for_each_l2hash_node(node
, tmp
, hash
, hi
) {
586 addr
= container_of(node
, struct vport_addr
, node
);
587 switch (addr
->action
) {
588 case MLX5_ACTION_ADD
:
589 vport_addr_add(esw
, addr
);
590 addr
->action
= MLX5_ACTION_NONE
;
592 case MLX5_ACTION_DEL
:
593 vport_addr_del(esw
, addr
);
594 l2addr_hash_del(addr
);
600 /* Sync vport UC/MC list from vport context */
601 static void esw_update_vport_addr_list(struct mlx5_eswitch
*esw
,
602 u32 vport_num
, int list_type
)
604 struct mlx5_vport
*vport
= &esw
->vports
[vport_num
];
605 bool is_uc
= list_type
== MLX5_NVPRT_LIST_TYPE_UC
;
606 u8 (*mac_list
)[ETH_ALEN
];
607 struct l2addr_node
*node
;
608 struct vport_addr
*addr
;
609 struct hlist_head
*hash
;
610 struct hlist_node
*tmp
;
616 size
= is_uc
? MLX5_MAX_UC_PER_VPORT(esw
->dev
) :
617 MLX5_MAX_MC_PER_VPORT(esw
->dev
);
619 mac_list
= kcalloc(size
, ETH_ALEN
, GFP_KERNEL
);
623 hash
= is_uc
? vport
->uc_list
: vport
->mc_list
;
625 for_each_l2hash_node(node
, tmp
, hash
, hi
) {
626 addr
= container_of(node
, struct vport_addr
, node
);
627 addr
->action
= MLX5_ACTION_DEL
;
633 err
= mlx5_query_nic_vport_mac_list(esw
->dev
, vport_num
, list_type
,
637 esw_debug(esw
->dev
, "vport[%d] context update %s list size (%d)\n",
638 vport_num
, is_uc
? "UC" : "MC", size
);
640 for (i
= 0; i
< size
; i
++) {
641 if (is_uc
&& !is_valid_ether_addr(mac_list
[i
]))
644 if (!is_uc
&& !is_multicast_ether_addr(mac_list
[i
]))
647 addr
= l2addr_hash_find(hash
, mac_list
[i
], struct vport_addr
);
649 addr
->action
= MLX5_ACTION_NONE
;
650 /* If this mac was previously added because of allmulti
651 * promiscuous rx mode, its now converted to be original
654 if (addr
->mc_promisc
) {
655 struct esw_mc_addr
*esw_mc
=
656 l2addr_hash_find(esw
->mc_table
,
661 "Failed to MAC(%pM) in mcast DB\n",
666 addr
->mc_promisc
= false;
671 addr
= l2addr_hash_add(hash
, mac_list
[i
], struct vport_addr
,
675 "Failed to add MAC(%pM) to vport[%d] DB\n",
676 mac_list
[i
], vport_num
);
679 addr
->vport
= vport_num
;
680 addr
->action
= MLX5_ACTION_ADD
;
686 /* Sync vport UC/MC list from vport context
687 * Must be called after esw_update_vport_addr_list
689 static void esw_update_vport_mc_promisc(struct mlx5_eswitch
*esw
, u32 vport_num
)
691 struct mlx5_vport
*vport
= &esw
->vports
[vport_num
];
692 struct l2addr_node
*node
;
693 struct vport_addr
*addr
;
694 struct hlist_head
*hash
;
695 struct hlist_node
*tmp
;
698 hash
= vport
->mc_list
;
700 for_each_l2hash_node(node
, tmp
, esw
->mc_table
, hi
) {
701 u8
*mac
= node
->addr
;
703 addr
= l2addr_hash_find(hash
, mac
, struct vport_addr
);
705 if (addr
->action
== MLX5_ACTION_DEL
)
706 addr
->action
= MLX5_ACTION_NONE
;
709 addr
= l2addr_hash_add(hash
, mac
, struct vport_addr
,
713 "Failed to add allmulti MAC(%pM) to vport[%d] DB\n",
717 addr
->vport
= vport_num
;
718 addr
->action
= MLX5_ACTION_ADD
;
719 addr
->mc_promisc
= true;
723 /* Apply vport rx mode to HW FDB table */
724 static void esw_apply_vport_rx_mode(struct mlx5_eswitch
*esw
, u32 vport_num
,
725 bool promisc
, bool mc_promisc
)
727 struct esw_mc_addr
*allmulti_addr
= &esw
->mc_promisc
;
728 struct mlx5_vport
*vport
= &esw
->vports
[vport_num
];
730 if (IS_ERR_OR_NULL(vport
->allmulti_rule
) != mc_promisc
)
734 vport
->allmulti_rule
=
735 esw_fdb_set_vport_allmulti_rule(esw
, vport_num
);
736 if (!allmulti_addr
->uplink_rule
)
737 allmulti_addr
->uplink_rule
=
738 esw_fdb_set_vport_allmulti_rule(esw
,
740 allmulti_addr
->refcnt
++;
741 } else if (vport
->allmulti_rule
) {
742 mlx5_del_flow_rules(vport
->allmulti_rule
);
743 vport
->allmulti_rule
= NULL
;
745 if (--allmulti_addr
->refcnt
> 0)
748 if (allmulti_addr
->uplink_rule
)
749 mlx5_del_flow_rules(allmulti_addr
->uplink_rule
);
750 allmulti_addr
->uplink_rule
= NULL
;
754 if (IS_ERR_OR_NULL(vport
->promisc_rule
) != promisc
)
758 vport
->promisc_rule
= esw_fdb_set_vport_promisc_rule(esw
,
760 } else if (vport
->promisc_rule
) {
761 mlx5_del_flow_rules(vport
->promisc_rule
);
762 vport
->promisc_rule
= NULL
;
766 /* Sync vport rx mode from vport context */
767 static void esw_update_vport_rx_mode(struct mlx5_eswitch
*esw
, u32 vport_num
)
769 struct mlx5_vport
*vport
= &esw
->vports
[vport_num
];
775 err
= mlx5_query_nic_vport_promisc(esw
->dev
,
782 esw_debug(esw
->dev
, "vport[%d] context update rx mode promisc_all=%d, all_multi=%d\n",
783 vport_num
, promisc_all
, promisc_mc
);
785 if (!vport
->info
.trusted
|| !vport
->enabled
) {
791 esw_apply_vport_rx_mode(esw
, vport_num
, promisc_all
,
792 (promisc_all
|| promisc_mc
));
795 static void esw_vport_change_handle_locked(struct mlx5_vport
*vport
)
797 struct mlx5_core_dev
*dev
= vport
->dev
;
798 struct mlx5_eswitch
*esw
= dev
->priv
.eswitch
;
801 mlx5_query_nic_vport_mac_address(dev
, vport
->vport
, mac
);
802 esw_debug(dev
, "vport[%d] Context Changed: perm mac: %pM\n",
805 if (vport
->enabled_events
& UC_ADDR_CHANGE
) {
806 esw_update_vport_addr_list(esw
, vport
->vport
,
807 MLX5_NVPRT_LIST_TYPE_UC
);
808 esw_apply_vport_addr_list(esw
, vport
->vport
,
809 MLX5_NVPRT_LIST_TYPE_UC
);
812 if (vport
->enabled_events
& MC_ADDR_CHANGE
) {
813 esw_update_vport_addr_list(esw
, vport
->vport
,
814 MLX5_NVPRT_LIST_TYPE_MC
);
817 if (vport
->enabled_events
& PROMISC_CHANGE
) {
818 esw_update_vport_rx_mode(esw
, vport
->vport
);
819 if (!IS_ERR_OR_NULL(vport
->allmulti_rule
))
820 esw_update_vport_mc_promisc(esw
, vport
->vport
);
823 if (vport
->enabled_events
& (PROMISC_CHANGE
| MC_ADDR_CHANGE
)) {
824 esw_apply_vport_addr_list(esw
, vport
->vport
,
825 MLX5_NVPRT_LIST_TYPE_MC
);
828 esw_debug(esw
->dev
, "vport[%d] Context Changed: Done\n", vport
->vport
);
830 arm_vport_context_events_cmd(dev
, vport
->vport
,
831 vport
->enabled_events
);
834 static void esw_vport_change_handler(struct work_struct
*work
)
836 struct mlx5_vport
*vport
=
837 container_of(work
, struct mlx5_vport
, vport_change_handler
);
838 struct mlx5_eswitch
*esw
= vport
->dev
->priv
.eswitch
;
840 mutex_lock(&esw
->state_lock
);
841 esw_vport_change_handle_locked(vport
);
842 mutex_unlock(&esw
->state_lock
);
845 static int esw_vport_enable_egress_acl(struct mlx5_eswitch
*esw
,
846 struct mlx5_vport
*vport
)
848 int inlen
= MLX5_ST_SZ_BYTES(create_flow_group_in
);
849 struct mlx5_flow_group
*vlan_grp
= NULL
;
850 struct mlx5_flow_group
*drop_grp
= NULL
;
851 struct mlx5_core_dev
*dev
= esw
->dev
;
852 struct mlx5_flow_namespace
*root_ns
;
853 struct mlx5_flow_table
*acl
;
854 void *match_criteria
;
856 /* The egress acl table contains 2 rules:
857 * 1)Allow traffic with vlan_tag=vst_vlan_id
858 * 2)Drop all other traffic.
863 if (!MLX5_CAP_ESW_EGRESS_ACL(dev
, ft_support
))
866 if (!IS_ERR_OR_NULL(vport
->egress
.acl
))
869 esw_debug(dev
, "Create vport[%d] egress ACL log_max_size(%d)\n",
870 vport
->vport
, MLX5_CAP_ESW_EGRESS_ACL(dev
, log_max_ft_size
));
872 root_ns
= mlx5_get_flow_vport_acl_namespace(dev
, MLX5_FLOW_NAMESPACE_ESW_EGRESS
,
875 esw_warn(dev
, "Failed to get E-Switch egress flow namespace for vport (%d)\n", vport
->vport
);
879 flow_group_in
= kvzalloc(inlen
, GFP_KERNEL
);
883 acl
= mlx5_create_vport_flow_table(root_ns
, 0, table_size
, 0, vport
->vport
);
886 esw_warn(dev
, "Failed to create E-Switch vport[%d] egress flow Table, err(%d)\n",
891 MLX5_SET(create_flow_group_in
, flow_group_in
, match_criteria_enable
, MLX5_MATCH_OUTER_HEADERS
);
892 match_criteria
= MLX5_ADDR_OF(create_flow_group_in
, flow_group_in
, match_criteria
);
893 MLX5_SET_TO_ONES(fte_match_param
, match_criteria
, outer_headers
.cvlan_tag
);
894 MLX5_SET_TO_ONES(fte_match_param
, match_criteria
, outer_headers
.first_vid
);
895 MLX5_SET(create_flow_group_in
, flow_group_in
, start_flow_index
, 0);
896 MLX5_SET(create_flow_group_in
, flow_group_in
, end_flow_index
, 0);
898 vlan_grp
= mlx5_create_flow_group(acl
, flow_group_in
);
899 if (IS_ERR(vlan_grp
)) {
900 err
= PTR_ERR(vlan_grp
);
901 esw_warn(dev
, "Failed to create E-Switch vport[%d] egress allowed vlans flow group, err(%d)\n",
906 memset(flow_group_in
, 0, inlen
);
907 MLX5_SET(create_flow_group_in
, flow_group_in
, start_flow_index
, 1);
908 MLX5_SET(create_flow_group_in
, flow_group_in
, end_flow_index
, 1);
909 drop_grp
= mlx5_create_flow_group(acl
, flow_group_in
);
910 if (IS_ERR(drop_grp
)) {
911 err
= PTR_ERR(drop_grp
);
912 esw_warn(dev
, "Failed to create E-Switch vport[%d] egress drop flow group, err(%d)\n",
917 vport
->egress
.acl
= acl
;
918 vport
->egress
.drop_grp
= drop_grp
;
919 vport
->egress
.allowed_vlans_grp
= vlan_grp
;
921 kvfree(flow_group_in
);
922 if (err
&& !IS_ERR_OR_NULL(vlan_grp
))
923 mlx5_destroy_flow_group(vlan_grp
);
924 if (err
&& !IS_ERR_OR_NULL(acl
))
925 mlx5_destroy_flow_table(acl
);
929 static void esw_vport_cleanup_egress_rules(struct mlx5_eswitch
*esw
,
930 struct mlx5_vport
*vport
)
932 if (!IS_ERR_OR_NULL(vport
->egress
.allowed_vlan
))
933 mlx5_del_flow_rules(vport
->egress
.allowed_vlan
);
935 if (!IS_ERR_OR_NULL(vport
->egress
.drop_rule
))
936 mlx5_del_flow_rules(vport
->egress
.drop_rule
);
938 vport
->egress
.allowed_vlan
= NULL
;
939 vport
->egress
.drop_rule
= NULL
;
942 static void esw_vport_disable_egress_acl(struct mlx5_eswitch
*esw
,
943 struct mlx5_vport
*vport
)
945 if (IS_ERR_OR_NULL(vport
->egress
.acl
))
948 esw_debug(esw
->dev
, "Destroy vport[%d] E-Switch egress ACL\n", vport
->vport
);
950 esw_vport_cleanup_egress_rules(esw
, vport
);
951 mlx5_destroy_flow_group(vport
->egress
.allowed_vlans_grp
);
952 mlx5_destroy_flow_group(vport
->egress
.drop_grp
);
953 mlx5_destroy_flow_table(vport
->egress
.acl
);
954 vport
->egress
.allowed_vlans_grp
= NULL
;
955 vport
->egress
.drop_grp
= NULL
;
956 vport
->egress
.acl
= NULL
;
959 static int esw_vport_enable_ingress_acl(struct mlx5_eswitch
*esw
,
960 struct mlx5_vport
*vport
)
962 int inlen
= MLX5_ST_SZ_BYTES(create_flow_group_in
);
963 struct mlx5_core_dev
*dev
= esw
->dev
;
964 struct mlx5_flow_namespace
*root_ns
;
965 struct mlx5_flow_table
*acl
;
966 struct mlx5_flow_group
*g
;
967 void *match_criteria
;
969 /* The ingress acl table contains 4 groups
970 * (2 active rules at the same time -
971 * 1 allow rule from one of the first 3 groups.
972 * 1 drop rule from the last group):
973 * 1)Allow untagged traffic with smac=original mac.
974 * 2)Allow untagged traffic.
975 * 3)Allow traffic with smac=original mac.
976 * 4)Drop all other traffic.
981 if (!MLX5_CAP_ESW_INGRESS_ACL(dev
, ft_support
))
984 if (!IS_ERR_OR_NULL(vport
->ingress
.acl
))
987 esw_debug(dev
, "Create vport[%d] ingress ACL log_max_size(%d)\n",
988 vport
->vport
, MLX5_CAP_ESW_INGRESS_ACL(dev
, log_max_ft_size
));
990 root_ns
= mlx5_get_flow_vport_acl_namespace(dev
, MLX5_FLOW_NAMESPACE_ESW_INGRESS
,
993 esw_warn(dev
, "Failed to get E-Switch ingress flow namespace for vport (%d)\n", vport
->vport
);
997 flow_group_in
= kvzalloc(inlen
, GFP_KERNEL
);
1001 acl
= mlx5_create_vport_flow_table(root_ns
, 0, table_size
, 0, vport
->vport
);
1004 esw_warn(dev
, "Failed to create E-Switch vport[%d] ingress flow Table, err(%d)\n",
1008 vport
->ingress
.acl
= acl
;
1010 match_criteria
= MLX5_ADDR_OF(create_flow_group_in
, flow_group_in
, match_criteria
);
1012 MLX5_SET(create_flow_group_in
, flow_group_in
, match_criteria_enable
, MLX5_MATCH_OUTER_HEADERS
);
1013 MLX5_SET_TO_ONES(fte_match_param
, match_criteria
, outer_headers
.cvlan_tag
);
1014 MLX5_SET_TO_ONES(fte_match_param
, match_criteria
, outer_headers
.smac_47_16
);
1015 MLX5_SET_TO_ONES(fte_match_param
, match_criteria
, outer_headers
.smac_15_0
);
1016 MLX5_SET(create_flow_group_in
, flow_group_in
, start_flow_index
, 0);
1017 MLX5_SET(create_flow_group_in
, flow_group_in
, end_flow_index
, 0);
1019 g
= mlx5_create_flow_group(acl
, flow_group_in
);
1022 esw_warn(dev
, "Failed to create E-Switch vport[%d] ingress untagged spoofchk flow group, err(%d)\n",
1026 vport
->ingress
.allow_untagged_spoofchk_grp
= g
;
1028 memset(flow_group_in
, 0, inlen
);
1029 MLX5_SET(create_flow_group_in
, flow_group_in
, match_criteria_enable
, MLX5_MATCH_OUTER_HEADERS
);
1030 MLX5_SET_TO_ONES(fte_match_param
, match_criteria
, outer_headers
.cvlan_tag
);
1031 MLX5_SET(create_flow_group_in
, flow_group_in
, start_flow_index
, 1);
1032 MLX5_SET(create_flow_group_in
, flow_group_in
, end_flow_index
, 1);
1034 g
= mlx5_create_flow_group(acl
, flow_group_in
);
1037 esw_warn(dev
, "Failed to create E-Switch vport[%d] ingress untagged flow group, err(%d)\n",
1041 vport
->ingress
.allow_untagged_only_grp
= g
;
1043 memset(flow_group_in
, 0, inlen
);
1044 MLX5_SET(create_flow_group_in
, flow_group_in
, match_criteria_enable
, MLX5_MATCH_OUTER_HEADERS
);
1045 MLX5_SET_TO_ONES(fte_match_param
, match_criteria
, outer_headers
.smac_47_16
);
1046 MLX5_SET_TO_ONES(fte_match_param
, match_criteria
, outer_headers
.smac_15_0
);
1047 MLX5_SET(create_flow_group_in
, flow_group_in
, start_flow_index
, 2);
1048 MLX5_SET(create_flow_group_in
, flow_group_in
, end_flow_index
, 2);
1050 g
= mlx5_create_flow_group(acl
, flow_group_in
);
1053 esw_warn(dev
, "Failed to create E-Switch vport[%d] ingress spoofchk flow group, err(%d)\n",
1057 vport
->ingress
.allow_spoofchk_only_grp
= g
;
1059 memset(flow_group_in
, 0, inlen
);
1060 MLX5_SET(create_flow_group_in
, flow_group_in
, start_flow_index
, 3);
1061 MLX5_SET(create_flow_group_in
, flow_group_in
, end_flow_index
, 3);
1063 g
= mlx5_create_flow_group(acl
, flow_group_in
);
1066 esw_warn(dev
, "Failed to create E-Switch vport[%d] ingress drop flow group, err(%d)\n",
1070 vport
->ingress
.drop_grp
= g
;
1074 if (!IS_ERR_OR_NULL(vport
->ingress
.allow_spoofchk_only_grp
))
1075 mlx5_destroy_flow_group(
1076 vport
->ingress
.allow_spoofchk_only_grp
);
1077 if (!IS_ERR_OR_NULL(vport
->ingress
.allow_untagged_only_grp
))
1078 mlx5_destroy_flow_group(
1079 vport
->ingress
.allow_untagged_only_grp
);
1080 if (!IS_ERR_OR_NULL(vport
->ingress
.allow_untagged_spoofchk_grp
))
1081 mlx5_destroy_flow_group(
1082 vport
->ingress
.allow_untagged_spoofchk_grp
);
1083 if (!IS_ERR_OR_NULL(vport
->ingress
.acl
))
1084 mlx5_destroy_flow_table(vport
->ingress
.acl
);
1087 kvfree(flow_group_in
);
1091 static void esw_vport_cleanup_ingress_rules(struct mlx5_eswitch
*esw
,
1092 struct mlx5_vport
*vport
)
1094 if (!IS_ERR_OR_NULL(vport
->ingress
.drop_rule
))
1095 mlx5_del_flow_rules(vport
->ingress
.drop_rule
);
1097 if (!IS_ERR_OR_NULL(vport
->ingress
.allow_rule
))
1098 mlx5_del_flow_rules(vport
->ingress
.allow_rule
);
1100 vport
->ingress
.drop_rule
= NULL
;
1101 vport
->ingress
.allow_rule
= NULL
;
1104 static void esw_vport_disable_ingress_acl(struct mlx5_eswitch
*esw
,
1105 struct mlx5_vport
*vport
)
1107 if (IS_ERR_OR_NULL(vport
->ingress
.acl
))
1110 esw_debug(esw
->dev
, "Destroy vport[%d] E-Switch ingress ACL\n", vport
->vport
);
1112 esw_vport_cleanup_ingress_rules(esw
, vport
);
1113 mlx5_destroy_flow_group(vport
->ingress
.allow_spoofchk_only_grp
);
1114 mlx5_destroy_flow_group(vport
->ingress
.allow_untagged_only_grp
);
1115 mlx5_destroy_flow_group(vport
->ingress
.allow_untagged_spoofchk_grp
);
1116 mlx5_destroy_flow_group(vport
->ingress
.drop_grp
);
1117 mlx5_destroy_flow_table(vport
->ingress
.acl
);
1118 vport
->ingress
.acl
= NULL
;
1119 vport
->ingress
.drop_grp
= NULL
;
1120 vport
->ingress
.allow_spoofchk_only_grp
= NULL
;
1121 vport
->ingress
.allow_untagged_only_grp
= NULL
;
1122 vport
->ingress
.allow_untagged_spoofchk_grp
= NULL
;
1125 static int esw_vport_ingress_config(struct mlx5_eswitch
*esw
,
1126 struct mlx5_vport
*vport
)
1128 struct mlx5_fc
*counter
= vport
->ingress
.drop_counter
;
1129 struct mlx5_flow_destination drop_ctr_dst
= {0};
1130 struct mlx5_flow_destination
*dst
= NULL
;
1131 struct mlx5_flow_act flow_act
= {0};
1132 struct mlx5_flow_spec
*spec
;
1137 esw_vport_cleanup_ingress_rules(esw
, vport
);
1139 if (!vport
->info
.vlan
&& !vport
->info
.qos
&& !vport
->info
.spoofchk
) {
1140 esw_vport_disable_ingress_acl(esw
, vport
);
1144 err
= esw_vport_enable_ingress_acl(esw
, vport
);
1146 mlx5_core_warn(esw
->dev
,
1147 "failed to enable ingress acl (%d) on vport[%d]\n",
1153 "vport[%d] configure ingress rules, vlan(%d) qos(%d)\n",
1154 vport
->vport
, vport
->info
.vlan
, vport
->info
.qos
);
1156 spec
= kvzalloc(sizeof(*spec
), GFP_KERNEL
);
1162 if (vport
->info
.vlan
|| vport
->info
.qos
)
1163 MLX5_SET_TO_ONES(fte_match_param
, spec
->match_criteria
, outer_headers
.cvlan_tag
);
1165 if (vport
->info
.spoofchk
) {
1166 MLX5_SET_TO_ONES(fte_match_param
, spec
->match_criteria
, outer_headers
.smac_47_16
);
1167 MLX5_SET_TO_ONES(fte_match_param
, spec
->match_criteria
, outer_headers
.smac_15_0
);
1168 smac_v
= MLX5_ADDR_OF(fte_match_param
,
1170 outer_headers
.smac_47_16
);
1171 ether_addr_copy(smac_v
, vport
->info
.mac
);
1174 spec
->match_criteria_enable
= MLX5_MATCH_OUTER_HEADERS
;
1175 flow_act
.action
= MLX5_FLOW_CONTEXT_ACTION_ALLOW
;
1176 vport
->ingress
.allow_rule
=
1177 mlx5_add_flow_rules(vport
->ingress
.acl
, spec
,
1178 &flow_act
, NULL
, 0);
1179 if (IS_ERR(vport
->ingress
.allow_rule
)) {
1180 err
= PTR_ERR(vport
->ingress
.allow_rule
);
1182 "vport[%d] configure ingress allow rule, err(%d)\n",
1184 vport
->ingress
.allow_rule
= NULL
;
1188 memset(spec
, 0, sizeof(*spec
));
1189 flow_act
.action
= MLX5_FLOW_CONTEXT_ACTION_DROP
;
1191 /* Attach drop flow counter */
1193 flow_act
.action
|= MLX5_FLOW_CONTEXT_ACTION_COUNT
;
1194 drop_ctr_dst
.type
= MLX5_FLOW_DESTINATION_TYPE_COUNTER
;
1195 drop_ctr_dst
.counter_id
= mlx5_fc_id(counter
);
1196 dst
= &drop_ctr_dst
;
1199 vport
->ingress
.drop_rule
=
1200 mlx5_add_flow_rules(vport
->ingress
.acl
, spec
,
1201 &flow_act
, dst
, dest_num
);
1202 if (IS_ERR(vport
->ingress
.drop_rule
)) {
1203 err
= PTR_ERR(vport
->ingress
.drop_rule
);
1205 "vport[%d] configure ingress drop rule, err(%d)\n",
1207 vport
->ingress
.drop_rule
= NULL
;
1213 esw_vport_cleanup_ingress_rules(esw
, vport
);
1218 static int esw_vport_egress_config(struct mlx5_eswitch
*esw
,
1219 struct mlx5_vport
*vport
)
1221 struct mlx5_fc
*counter
= vport
->egress
.drop_counter
;
1222 struct mlx5_flow_destination drop_ctr_dst
= {0};
1223 struct mlx5_flow_destination
*dst
= NULL
;
1224 struct mlx5_flow_act flow_act
= {0};
1225 struct mlx5_flow_spec
*spec
;
1229 esw_vport_cleanup_egress_rules(esw
, vport
);
1231 if (!vport
->info
.vlan
&& !vport
->info
.qos
) {
1232 esw_vport_disable_egress_acl(esw
, vport
);
1236 err
= esw_vport_enable_egress_acl(esw
, vport
);
1238 mlx5_core_warn(esw
->dev
,
1239 "failed to enable egress acl (%d) on vport[%d]\n",
1245 "vport[%d] configure egress rules, vlan(%d) qos(%d)\n",
1246 vport
->vport
, vport
->info
.vlan
, vport
->info
.qos
);
1248 spec
= kvzalloc(sizeof(*spec
), GFP_KERNEL
);
1254 /* Allowed vlan rule */
1255 MLX5_SET_TO_ONES(fte_match_param
, spec
->match_criteria
, outer_headers
.cvlan_tag
);
1256 MLX5_SET_TO_ONES(fte_match_param
, spec
->match_value
, outer_headers
.cvlan_tag
);
1257 MLX5_SET_TO_ONES(fte_match_param
, spec
->match_criteria
, outer_headers
.first_vid
);
1258 MLX5_SET(fte_match_param
, spec
->match_value
, outer_headers
.first_vid
, vport
->info
.vlan
);
1260 spec
->match_criteria_enable
= MLX5_MATCH_OUTER_HEADERS
;
1261 flow_act
.action
= MLX5_FLOW_CONTEXT_ACTION_ALLOW
;
1262 vport
->egress
.allowed_vlan
=
1263 mlx5_add_flow_rules(vport
->egress
.acl
, spec
,
1264 &flow_act
, NULL
, 0);
1265 if (IS_ERR(vport
->egress
.allowed_vlan
)) {
1266 err
= PTR_ERR(vport
->egress
.allowed_vlan
);
1268 "vport[%d] configure egress allowed vlan rule failed, err(%d)\n",
1270 vport
->egress
.allowed_vlan
= NULL
;
1274 /* Drop others rule (star rule) */
1275 memset(spec
, 0, sizeof(*spec
));
1276 flow_act
.action
= MLX5_FLOW_CONTEXT_ACTION_DROP
;
1278 /* Attach egress drop flow counter */
1280 flow_act
.action
|= MLX5_FLOW_CONTEXT_ACTION_COUNT
;
1281 drop_ctr_dst
.type
= MLX5_FLOW_DESTINATION_TYPE_COUNTER
;
1282 drop_ctr_dst
.counter_id
= mlx5_fc_id(counter
);
1283 dst
= &drop_ctr_dst
;
1286 vport
->egress
.drop_rule
=
1287 mlx5_add_flow_rules(vport
->egress
.acl
, spec
,
1288 &flow_act
, dst
, dest_num
);
1289 if (IS_ERR(vport
->egress
.drop_rule
)) {
1290 err
= PTR_ERR(vport
->egress
.drop_rule
);
1292 "vport[%d] configure egress drop rule failed, err(%d)\n",
1294 vport
->egress
.drop_rule
= NULL
;
1301 /* Vport QoS management */
1302 static int esw_create_tsar(struct mlx5_eswitch
*esw
)
1304 u32 tsar_ctx
[MLX5_ST_SZ_DW(scheduling_context
)] = {0};
1305 struct mlx5_core_dev
*dev
= esw
->dev
;
1308 if (!MLX5_CAP_GEN(dev
, qos
) || !MLX5_CAP_QOS(dev
, esw_scheduling
))
1311 if (esw
->qos
.enabled
)
1314 err
= mlx5_create_scheduling_element_cmd(dev
,
1315 SCHEDULING_HIERARCHY_E_SWITCH
,
1317 &esw
->qos
.root_tsar_id
);
1319 esw_warn(esw
->dev
, "E-Switch create TSAR failed (%d)\n", err
);
1323 esw
->qos
.enabled
= true;
1327 static void esw_destroy_tsar(struct mlx5_eswitch
*esw
)
1331 if (!esw
->qos
.enabled
)
1334 err
= mlx5_destroy_scheduling_element_cmd(esw
->dev
,
1335 SCHEDULING_HIERARCHY_E_SWITCH
,
1336 esw
->qos
.root_tsar_id
);
1338 esw_warn(esw
->dev
, "E-Switch destroy TSAR failed (%d)\n", err
);
1340 esw
->qos
.enabled
= false;
1343 static int esw_vport_enable_qos(struct mlx5_eswitch
*esw
, int vport_num
,
1344 u32 initial_max_rate
, u32 initial_bw_share
)
1346 u32 sched_ctx
[MLX5_ST_SZ_DW(scheduling_context
)] = {0};
1347 struct mlx5_vport
*vport
= &esw
->vports
[vport_num
];
1348 struct mlx5_core_dev
*dev
= esw
->dev
;
1352 if (!esw
->qos
.enabled
|| !MLX5_CAP_GEN(dev
, qos
) ||
1353 !MLX5_CAP_QOS(dev
, esw_scheduling
))
1356 if (vport
->qos
.enabled
)
1359 MLX5_SET(scheduling_context
, sched_ctx
, element_type
,
1360 SCHEDULING_CONTEXT_ELEMENT_TYPE_VPORT
);
1361 vport_elem
= MLX5_ADDR_OF(scheduling_context
, sched_ctx
,
1362 element_attributes
);
1363 MLX5_SET(vport_element
, vport_elem
, vport_number
, vport_num
);
1364 MLX5_SET(scheduling_context
, sched_ctx
, parent_element_id
,
1365 esw
->qos
.root_tsar_id
);
1366 MLX5_SET(scheduling_context
, sched_ctx
, max_average_bw
,
1368 MLX5_SET(scheduling_context
, sched_ctx
, bw_share
, initial_bw_share
);
1370 err
= mlx5_create_scheduling_element_cmd(dev
,
1371 SCHEDULING_HIERARCHY_E_SWITCH
,
1373 &vport
->qos
.esw_tsar_ix
);
1375 esw_warn(esw
->dev
, "E-Switch create TSAR vport element failed (vport=%d,err=%d)\n",
1380 vport
->qos
.enabled
= true;
1384 static void esw_vport_disable_qos(struct mlx5_eswitch
*esw
, int vport_num
)
1386 struct mlx5_vport
*vport
= &esw
->vports
[vport_num
];
1389 if (!vport
->qos
.enabled
)
1392 err
= mlx5_destroy_scheduling_element_cmd(esw
->dev
,
1393 SCHEDULING_HIERARCHY_E_SWITCH
,
1394 vport
->qos
.esw_tsar_ix
);
1396 esw_warn(esw
->dev
, "E-Switch destroy TSAR vport element failed (vport=%d,err=%d)\n",
1399 vport
->qos
.enabled
= false;
1402 static int esw_vport_qos_config(struct mlx5_eswitch
*esw
, int vport_num
,
1403 u32 max_rate
, u32 bw_share
)
1405 u32 sched_ctx
[MLX5_ST_SZ_DW(scheduling_context
)] = {0};
1406 struct mlx5_vport
*vport
= &esw
->vports
[vport_num
];
1407 struct mlx5_core_dev
*dev
= esw
->dev
;
1412 if (!MLX5_CAP_GEN(dev
, qos
) || !MLX5_CAP_QOS(dev
, esw_scheduling
))
1415 if (!vport
->qos
.enabled
)
1418 MLX5_SET(scheduling_context
, sched_ctx
, element_type
,
1419 SCHEDULING_CONTEXT_ELEMENT_TYPE_VPORT
);
1420 vport_elem
= MLX5_ADDR_OF(scheduling_context
, sched_ctx
,
1421 element_attributes
);
1422 MLX5_SET(vport_element
, vport_elem
, vport_number
, vport_num
);
1423 MLX5_SET(scheduling_context
, sched_ctx
, parent_element_id
,
1424 esw
->qos
.root_tsar_id
);
1425 MLX5_SET(scheduling_context
, sched_ctx
, max_average_bw
,
1427 MLX5_SET(scheduling_context
, sched_ctx
, bw_share
, bw_share
);
1428 bitmask
|= MODIFY_SCHEDULING_ELEMENT_IN_MODIFY_BITMASK_MAX_AVERAGE_BW
;
1429 bitmask
|= MODIFY_SCHEDULING_ELEMENT_IN_MODIFY_BITMASK_BW_SHARE
;
1431 err
= mlx5_modify_scheduling_element_cmd(dev
,
1432 SCHEDULING_HIERARCHY_E_SWITCH
,
1434 vport
->qos
.esw_tsar_ix
,
1437 esw_warn(esw
->dev
, "E-Switch modify TSAR vport element failed (vport=%d,err=%d)\n",
1445 static void node_guid_gen_from_mac(u64
*node_guid
, u8 mac
[ETH_ALEN
])
1447 ((u8
*)node_guid
)[7] = mac
[0];
1448 ((u8
*)node_guid
)[6] = mac
[1];
1449 ((u8
*)node_guid
)[5] = mac
[2];
1450 ((u8
*)node_guid
)[4] = 0xff;
1451 ((u8
*)node_guid
)[3] = 0xfe;
1452 ((u8
*)node_guid
)[2] = mac
[3];
1453 ((u8
*)node_guid
)[1] = mac
[4];
1454 ((u8
*)node_guid
)[0] = mac
[5];
1457 static void esw_apply_vport_conf(struct mlx5_eswitch
*esw
,
1458 struct mlx5_vport
*vport
)
1460 int vport_num
= vport
->vport
;
1465 mlx5_modify_vport_admin_state(esw
->dev
,
1466 MLX5_VPORT_STATE_OP_MOD_ESW_VPORT
,
1468 vport
->info
.link_state
);
1469 mlx5_modify_nic_vport_mac_address(esw
->dev
, vport_num
, vport
->info
.mac
);
1470 mlx5_modify_nic_vport_node_guid(esw
->dev
, vport_num
, vport
->info
.node_guid
);
1471 modify_esw_vport_cvlan(esw
->dev
, vport_num
, vport
->info
.vlan
, vport
->info
.qos
,
1472 (vport
->info
.vlan
|| vport
->info
.qos
));
1474 /* Only legacy mode needs ACLs */
1475 if (esw
->mode
== SRIOV_LEGACY
) {
1476 esw_vport_ingress_config(esw
, vport
);
1477 esw_vport_egress_config(esw
, vport
);
1481 static void esw_vport_create_drop_counters(struct mlx5_vport
*vport
)
1483 struct mlx5_core_dev
*dev
= vport
->dev
;
1485 if (MLX5_CAP_ESW_INGRESS_ACL(dev
, flow_counter
)) {
1486 vport
->ingress
.drop_counter
= mlx5_fc_create(dev
, false);
1487 if (IS_ERR(vport
->ingress
.drop_counter
)) {
1489 "vport[%d] configure ingress drop rule counter failed\n",
1491 vport
->ingress
.drop_counter
= NULL
;
1495 if (MLX5_CAP_ESW_EGRESS_ACL(dev
, flow_counter
)) {
1496 vport
->egress
.drop_counter
= mlx5_fc_create(dev
, false);
1497 if (IS_ERR(vport
->egress
.drop_counter
)) {
1499 "vport[%d] configure egress drop rule counter failed\n",
1501 vport
->egress
.drop_counter
= NULL
;
1506 static void esw_vport_destroy_drop_counters(struct mlx5_vport
*vport
)
1508 struct mlx5_core_dev
*dev
= vport
->dev
;
1510 if (vport
->ingress
.drop_counter
)
1511 mlx5_fc_destroy(dev
, vport
->ingress
.drop_counter
);
1512 if (vport
->egress
.drop_counter
)
1513 mlx5_fc_destroy(dev
, vport
->egress
.drop_counter
);
1516 static void esw_enable_vport(struct mlx5_eswitch
*esw
, int vport_num
,
1519 struct mlx5_vport
*vport
= &esw
->vports
[vport_num
];
1521 mutex_lock(&esw
->state_lock
);
1522 WARN_ON(vport
->enabled
);
1524 esw_debug(esw
->dev
, "Enabling VPORT(%d)\n", vport_num
);
1526 /* Create steering drop counters for ingress and egress ACLs */
1527 if (vport_num
&& esw
->mode
== SRIOV_LEGACY
)
1528 esw_vport_create_drop_counters(vport
);
1530 /* Restore old vport configuration */
1531 esw_apply_vport_conf(esw
, vport
);
1533 /* Attach vport to the eswitch rate limiter */
1534 if (esw_vport_enable_qos(esw
, vport_num
, vport
->info
.max_rate
,
1535 vport
->qos
.bw_share
))
1536 esw_warn(esw
->dev
, "Failed to attach vport %d to eswitch rate limiter", vport_num
);
1538 /* Sync with current vport context */
1539 vport
->enabled_events
= enable_events
;
1540 vport
->enabled
= true;
1542 /* only PF is trusted by default */
1544 vport
->info
.trusted
= true;
1546 esw_vport_change_handle_locked(vport
);
1548 esw
->enabled_vports
++;
1549 esw_debug(esw
->dev
, "Enabled VPORT(%d)\n", vport_num
);
1550 mutex_unlock(&esw
->state_lock
);
1553 static void esw_disable_vport(struct mlx5_eswitch
*esw
, int vport_num
)
1555 struct mlx5_vport
*vport
= &esw
->vports
[vport_num
];
1557 if (!vport
->enabled
)
1560 esw_debug(esw
->dev
, "Disabling vport(%d)\n", vport_num
);
1561 /* Mark this vport as disabled to discard new events */
1562 vport
->enabled
= false;
1564 /* Wait for current already scheduled events to complete */
1565 flush_workqueue(esw
->work_queue
);
1566 /* Disable events from this vport */
1567 arm_vport_context_events_cmd(esw
->dev
, vport
->vport
, 0);
1568 mutex_lock(&esw
->state_lock
);
1569 /* We don't assume VFs will cleanup after themselves.
1570 * Calling vport change handler while vport is disabled will cleanup
1571 * the vport resources.
1573 esw_vport_change_handle_locked(vport
);
1574 vport
->enabled_events
= 0;
1575 esw_vport_disable_qos(esw
, vport_num
);
1576 if (vport_num
&& esw
->mode
== SRIOV_LEGACY
) {
1577 mlx5_modify_vport_admin_state(esw
->dev
,
1578 MLX5_VPORT_STATE_OP_MOD_ESW_VPORT
,
1580 MLX5_VPORT_ADMIN_STATE_DOWN
);
1581 esw_vport_disable_egress_acl(esw
, vport
);
1582 esw_vport_disable_ingress_acl(esw
, vport
);
1583 esw_vport_destroy_drop_counters(vport
);
1585 esw
->enabled_vports
--;
1586 mutex_unlock(&esw
->state_lock
);
1589 static int eswitch_vport_event(struct notifier_block
*nb
,
1590 unsigned long type
, void *data
)
1592 struct mlx5_eswitch
*esw
= mlx5_nb_cof(nb
, struct mlx5_eswitch
, nb
);
1593 struct mlx5_eqe
*eqe
= data
;
1594 struct mlx5_vport
*vport
;
1597 vport_num
= be16_to_cpu(eqe
->data
.vport_change
.vport_num
);
1598 vport
= &esw
->vports
[vport_num
];
1600 queue_work(esw
->work_queue
, &vport
->vport_change_handler
);
1605 /* Public E-Switch API */
1606 #define ESW_ALLOWED(esw) ((esw) && MLX5_ESWITCH_MANAGER((esw)->dev))
1608 int mlx5_eswitch_enable_sriov(struct mlx5_eswitch
*esw
, int nvfs
, int mode
)
1611 int i
, enabled_events
;
1613 if (!ESW_ALLOWED(esw
) ||
1614 !MLX5_CAP_ESW_FLOWTABLE_FDB(esw
->dev
, ft_support
)) {
1615 esw_warn(esw
->dev
, "E-Switch FDB is not supported, aborting ...\n");
1619 if (!MLX5_CAP_ESW_INGRESS_ACL(esw
->dev
, ft_support
))
1620 esw_warn(esw
->dev
, "E-Switch ingress ACL is not supported by FW\n");
1622 if (!MLX5_CAP_ESW_EGRESS_ACL(esw
->dev
, ft_support
))
1623 esw_warn(esw
->dev
, "E-Switch engress ACL is not supported by FW\n");
1625 esw_info(esw
->dev
, "E-Switch enable SRIOV: nvfs(%d) mode (%d)\n", nvfs
, mode
);
1629 mlx5_lag_update(esw
->dev
);
1631 if (mode
== SRIOV_LEGACY
) {
1632 err
= esw_create_legacy_fdb_table(esw
);
1634 mlx5_reload_interface(esw
->dev
, MLX5_INTERFACE_PROTOCOL_ETH
);
1635 mlx5_reload_interface(esw
->dev
, MLX5_INTERFACE_PROTOCOL_IB
);
1636 err
= esw_offloads_init(esw
, nvfs
+ 1);
1642 err
= esw_create_tsar(esw
);
1644 esw_warn(esw
->dev
, "Failed to create eswitch TSAR");
1646 /* Don't enable vport events when in SRIOV_OFFLOADS mode, since:
1647 * 1. L2 table (MPFS) is programmed by PF/VF representors netdevs set_rx_mode
1648 * 2. FDB/Eswitch is programmed by user space tools
1650 enabled_events
= (mode
== SRIOV_LEGACY
) ? SRIOV_VPORT_EVENTS
: 0;
1651 for (i
= 0; i
<= nvfs
; i
++)
1652 esw_enable_vport(esw
, i
, enabled_events
);
1654 if (mode
== SRIOV_LEGACY
) {
1655 MLX5_NB_INIT(&esw
->nb
, eswitch_vport_event
, NIC_VPORT_CHANGE
);
1656 mlx5_eq_notifier_register(esw
->dev
, &esw
->nb
);
1659 esw_info(esw
->dev
, "SRIOV enabled: active vports(%d)\n",
1660 esw
->enabled_vports
);
1664 esw
->mode
= SRIOV_NONE
;
1666 if (mode
== SRIOV_OFFLOADS
) {
1667 mlx5_reload_interface(esw
->dev
, MLX5_INTERFACE_PROTOCOL_IB
);
1668 mlx5_reload_interface(esw
->dev
, MLX5_INTERFACE_PROTOCOL_ETH
);
1674 void mlx5_eswitch_disable_sriov(struct mlx5_eswitch
*esw
)
1676 struct esw_mc_addr
*mc_promisc
;
1681 if (!ESW_ALLOWED(esw
) || esw
->mode
== SRIOV_NONE
)
1684 esw_info(esw
->dev
, "disable SRIOV: active vports(%d) mode(%d)\n",
1685 esw
->enabled_vports
, esw
->mode
);
1687 mc_promisc
= &esw
->mc_promisc
;
1688 nvports
= esw
->enabled_vports
;
1690 if (esw
->mode
== SRIOV_LEGACY
)
1691 mlx5_eq_notifier_unregister(esw
->dev
, &esw
->nb
);
1693 for (i
= 0; i
< esw
->total_vports
; i
++)
1694 esw_disable_vport(esw
, i
);
1696 if (mc_promisc
&& mc_promisc
->uplink_rule
)
1697 mlx5_del_flow_rules(mc_promisc
->uplink_rule
);
1699 esw_destroy_tsar(esw
);
1701 if (esw
->mode
== SRIOV_LEGACY
)
1702 esw_destroy_legacy_fdb_table(esw
);
1703 else if (esw
->mode
== SRIOV_OFFLOADS
)
1704 esw_offloads_cleanup(esw
, nvports
);
1706 old_mode
= esw
->mode
;
1707 esw
->mode
= SRIOV_NONE
;
1709 mlx5_lag_update(esw
->dev
);
1711 if (old_mode
== SRIOV_OFFLOADS
) {
1712 mlx5_reload_interface(esw
->dev
, MLX5_INTERFACE_PROTOCOL_IB
);
1713 mlx5_reload_interface(esw
->dev
, MLX5_INTERFACE_PROTOCOL_ETH
);
1717 int mlx5_eswitch_init(struct mlx5_core_dev
*dev
)
1719 int total_vports
= MLX5_TOTAL_VPORTS(dev
);
1720 struct mlx5_eswitch
*esw
;
1724 if (!MLX5_VPORT_MANAGER(dev
))
1728 "Total vports %d, per vport: max uc(%d) max mc(%d)\n",
1730 MLX5_MAX_UC_PER_VPORT(dev
),
1731 MLX5_MAX_MC_PER_VPORT(dev
));
1733 esw
= kzalloc(sizeof(*esw
), GFP_KERNEL
);
1739 esw
->work_queue
= create_singlethread_workqueue("mlx5_esw_wq");
1740 if (!esw
->work_queue
) {
1745 esw
->vports
= kcalloc(total_vports
, sizeof(struct mlx5_vport
),
1752 err
= esw_offloads_init_reps(esw
);
1756 hash_init(esw
->offloads
.encap_tbl
);
1757 hash_init(esw
->offloads
.mod_hdr_tbl
);
1758 mutex_init(&esw
->state_lock
);
1760 for (vport_num
= 0; vport_num
< total_vports
; vport_num
++) {
1761 struct mlx5_vport
*vport
= &esw
->vports
[vport_num
];
1763 vport
->vport
= vport_num
;
1764 vport
->info
.link_state
= MLX5_VPORT_ADMIN_STATE_AUTO
;
1766 INIT_WORK(&vport
->vport_change_handler
,
1767 esw_vport_change_handler
);
1770 esw
->total_vports
= total_vports
;
1771 esw
->enabled_vports
= 0;
1772 esw
->mode
= SRIOV_NONE
;
1773 esw
->offloads
.inline_mode
= MLX5_INLINE_MODE_NONE
;
1774 if (MLX5_CAP_ESW_FLOWTABLE_FDB(dev
, reformat
) &&
1775 MLX5_CAP_ESW_FLOWTABLE_FDB(dev
, decap
))
1776 esw
->offloads
.encap
= DEVLINK_ESWITCH_ENCAP_MODE_BASIC
;
1778 esw
->offloads
.encap
= DEVLINK_ESWITCH_ENCAP_MODE_NONE
;
1780 dev
->priv
.eswitch
= esw
;
1783 if (esw
->work_queue
)
1784 destroy_workqueue(esw
->work_queue
);
1785 esw_offloads_cleanup_reps(esw
);
1791 void mlx5_eswitch_cleanup(struct mlx5_eswitch
*esw
)
1793 if (!esw
|| !MLX5_VPORT_MANAGER(esw
->dev
))
1796 esw_info(esw
->dev
, "cleanup\n");
1798 esw
->dev
->priv
.eswitch
= NULL
;
1799 destroy_workqueue(esw
->work_queue
);
1800 esw_offloads_cleanup_reps(esw
);
1805 /* Vport Administration */
1806 #define LEGAL_VPORT(esw, vport) (vport >= 0 && vport < esw->total_vports)
1808 int mlx5_eswitch_set_vport_mac(struct mlx5_eswitch
*esw
,
1809 int vport
, u8 mac
[ETH_ALEN
])
1811 struct mlx5_vport
*evport
;
1815 if (!MLX5_CAP_GEN(esw
->dev
, vport_group_manager
))
1817 if (!LEGAL_VPORT(esw
, vport
) || is_multicast_ether_addr(mac
))
1820 mutex_lock(&esw
->state_lock
);
1821 evport
= &esw
->vports
[vport
];
1823 if (evport
->info
.spoofchk
&& !is_valid_ether_addr(mac
))
1824 mlx5_core_warn(esw
->dev
,
1825 "Set invalid MAC while spoofchk is on, vport(%d)\n",
1828 err
= mlx5_modify_nic_vport_mac_address(esw
->dev
, vport
, mac
);
1830 mlx5_core_warn(esw
->dev
,
1831 "Failed to mlx5_modify_nic_vport_mac vport(%d) err=(%d)\n",
1836 node_guid_gen_from_mac(&node_guid
, mac
);
1837 err
= mlx5_modify_nic_vport_node_guid(esw
->dev
, vport
, node_guid
);
1839 mlx5_core_warn(esw
->dev
,
1840 "Failed to set vport %d node guid, err = %d. RDMA_CM will not function properly for this VF.\n",
1843 ether_addr_copy(evport
->info
.mac
, mac
);
1844 evport
->info
.node_guid
= node_guid
;
1845 if (evport
->enabled
&& esw
->mode
== SRIOV_LEGACY
)
1846 err
= esw_vport_ingress_config(esw
, evport
);
1849 mutex_unlock(&esw
->state_lock
);
1853 int mlx5_eswitch_set_vport_state(struct mlx5_eswitch
*esw
,
1854 int vport
, int link_state
)
1856 struct mlx5_vport
*evport
;
1859 if (!ESW_ALLOWED(esw
))
1861 if (!LEGAL_VPORT(esw
, vport
))
1864 mutex_lock(&esw
->state_lock
);
1865 evport
= &esw
->vports
[vport
];
1867 err
= mlx5_modify_vport_admin_state(esw
->dev
,
1868 MLX5_VPORT_STATE_OP_MOD_ESW_VPORT
,
1871 mlx5_core_warn(esw
->dev
,
1872 "Failed to set vport %d link state, err = %d",
1877 evport
->info
.link_state
= link_state
;
1880 mutex_unlock(&esw
->state_lock
);
1884 int mlx5_eswitch_get_vport_config(struct mlx5_eswitch
*esw
,
1885 int vport
, struct ifla_vf_info
*ivi
)
1887 struct mlx5_vport
*evport
;
1889 if (!MLX5_CAP_GEN(esw
->dev
, vport_group_manager
))
1891 if (!LEGAL_VPORT(esw
, vport
))
1894 evport
= &esw
->vports
[vport
];
1896 memset(ivi
, 0, sizeof(*ivi
));
1897 ivi
->vf
= vport
- 1;
1899 mutex_lock(&esw
->state_lock
);
1900 ether_addr_copy(ivi
->mac
, evport
->info
.mac
);
1901 ivi
->linkstate
= evport
->info
.link_state
;
1902 ivi
->vlan
= evport
->info
.vlan
;
1903 ivi
->qos
= evport
->info
.qos
;
1904 ivi
->spoofchk
= evport
->info
.spoofchk
;
1905 ivi
->trusted
= evport
->info
.trusted
;
1906 ivi
->min_tx_rate
= evport
->info
.min_rate
;
1907 ivi
->max_tx_rate
= evport
->info
.max_rate
;
1908 mutex_unlock(&esw
->state_lock
);
1913 int __mlx5_eswitch_set_vport_vlan(struct mlx5_eswitch
*esw
,
1914 int vport
, u16 vlan
, u8 qos
, u8 set_flags
)
1916 struct mlx5_vport
*evport
;
1919 if (!ESW_ALLOWED(esw
))
1921 if (!LEGAL_VPORT(esw
, vport
) || (vlan
> 4095) || (qos
> 7))
1924 mutex_lock(&esw
->state_lock
);
1925 evport
= &esw
->vports
[vport
];
1927 err
= modify_esw_vport_cvlan(esw
->dev
, vport
, vlan
, qos
, set_flags
);
1931 evport
->info
.vlan
= vlan
;
1932 evport
->info
.qos
= qos
;
1933 if (evport
->enabled
&& esw
->mode
== SRIOV_LEGACY
) {
1934 err
= esw_vport_ingress_config(esw
, evport
);
1937 err
= esw_vport_egress_config(esw
, evport
);
1941 mutex_unlock(&esw
->state_lock
);
1945 int mlx5_eswitch_set_vport_vlan(struct mlx5_eswitch
*esw
,
1946 int vport
, u16 vlan
, u8 qos
)
1951 set_flags
= SET_VLAN_STRIP
| SET_VLAN_INSERT
;
1953 return __mlx5_eswitch_set_vport_vlan(esw
, vport
, vlan
, qos
, set_flags
);
1956 int mlx5_eswitch_set_vport_spoofchk(struct mlx5_eswitch
*esw
,
1957 int vport
, bool spoofchk
)
1959 struct mlx5_vport
*evport
;
1963 if (!ESW_ALLOWED(esw
))
1965 if (!LEGAL_VPORT(esw
, vport
))
1968 mutex_lock(&esw
->state_lock
);
1969 evport
= &esw
->vports
[vport
];
1970 pschk
= evport
->info
.spoofchk
;
1971 evport
->info
.spoofchk
= spoofchk
;
1972 if (pschk
&& !is_valid_ether_addr(evport
->info
.mac
))
1973 mlx5_core_warn(esw
->dev
,
1974 "Spoofchk in set while MAC is invalid, vport(%d)\n",
1976 if (evport
->enabled
&& esw
->mode
== SRIOV_LEGACY
)
1977 err
= esw_vport_ingress_config(esw
, evport
);
1979 evport
->info
.spoofchk
= pschk
;
1980 mutex_unlock(&esw
->state_lock
);
1985 int mlx5_eswitch_set_vport_trust(struct mlx5_eswitch
*esw
,
1986 int vport
, bool setting
)
1988 struct mlx5_vport
*evport
;
1990 if (!ESW_ALLOWED(esw
))
1992 if (!LEGAL_VPORT(esw
, vport
))
1995 mutex_lock(&esw
->state_lock
);
1996 evport
= &esw
->vports
[vport
];
1997 evport
->info
.trusted
= setting
;
1998 if (evport
->enabled
)
1999 esw_vport_change_handle_locked(evport
);
2000 mutex_unlock(&esw
->state_lock
);
2005 static u32
calculate_vports_min_rate_divider(struct mlx5_eswitch
*esw
)
2007 u32 fw_max_bw_share
= MLX5_CAP_QOS(esw
->dev
, max_tsar_bw_share
);
2008 struct mlx5_vport
*evport
;
2009 u32 max_guarantee
= 0;
2012 for (i
= 0; i
< esw
->total_vports
; i
++) {
2013 evport
= &esw
->vports
[i
];
2014 if (!evport
->enabled
|| evport
->info
.min_rate
< max_guarantee
)
2016 max_guarantee
= evport
->info
.min_rate
;
2019 return max_t(u32
, max_guarantee
/ fw_max_bw_share
, 1);
2022 static int normalize_vports_min_rate(struct mlx5_eswitch
*esw
, u32 divider
)
2024 u32 fw_max_bw_share
= MLX5_CAP_QOS(esw
->dev
, max_tsar_bw_share
);
2025 struct mlx5_vport
*evport
;
2032 for (i
= 0; i
< esw
->total_vports
; i
++) {
2033 evport
= &esw
->vports
[i
];
2034 if (!evport
->enabled
)
2036 vport_min_rate
= evport
->info
.min_rate
;
2037 vport_max_rate
= evport
->info
.max_rate
;
2038 bw_share
= MLX5_MIN_BW_SHARE
;
2041 bw_share
= MLX5_RATE_TO_BW_SHARE(vport_min_rate
,
2045 if (bw_share
== evport
->qos
.bw_share
)
2048 err
= esw_vport_qos_config(esw
, i
, vport_max_rate
,
2051 evport
->qos
.bw_share
= bw_share
;
2059 int mlx5_eswitch_set_vport_rate(struct mlx5_eswitch
*esw
, int vport
,
2060 u32 max_rate
, u32 min_rate
)
2062 u32 fw_max_bw_share
= MLX5_CAP_QOS(esw
->dev
, max_tsar_bw_share
);
2063 bool min_rate_supported
= MLX5_CAP_QOS(esw
->dev
, esw_bw_share
) &&
2064 fw_max_bw_share
>= MLX5_MIN_BW_SHARE
;
2065 bool max_rate_supported
= MLX5_CAP_QOS(esw
->dev
, esw_rate_limit
);
2066 struct mlx5_vport
*evport
;
2067 u32 previous_min_rate
;
2071 if (!ESW_ALLOWED(esw
))
2073 if (!LEGAL_VPORT(esw
, vport
))
2075 if ((min_rate
&& !min_rate_supported
) || (max_rate
&& !max_rate_supported
))
2078 mutex_lock(&esw
->state_lock
);
2079 evport
= &esw
->vports
[vport
];
2081 if (min_rate
== evport
->info
.min_rate
)
2084 previous_min_rate
= evport
->info
.min_rate
;
2085 evport
->info
.min_rate
= min_rate
;
2086 divider
= calculate_vports_min_rate_divider(esw
);
2087 err
= normalize_vports_min_rate(esw
, divider
);
2089 evport
->info
.min_rate
= previous_min_rate
;
2094 if (max_rate
== evport
->info
.max_rate
)
2097 err
= esw_vport_qos_config(esw
, vport
, max_rate
, evport
->qos
.bw_share
);
2099 evport
->info
.max_rate
= max_rate
;
2102 mutex_unlock(&esw
->state_lock
);
2106 static int mlx5_eswitch_query_vport_drop_stats(struct mlx5_core_dev
*dev
,
2108 struct mlx5_vport_drop_stats
*stats
)
2110 struct mlx5_eswitch
*esw
= dev
->priv
.eswitch
;
2111 struct mlx5_vport
*vport
= &esw
->vports
[vport_idx
];
2112 u64 rx_discard_vport_down
, tx_discard_vport_down
;
2116 if (!vport
->enabled
|| esw
->mode
!= SRIOV_LEGACY
)
2119 if (vport
->egress
.drop_counter
)
2120 mlx5_fc_query(dev
, vport
->egress
.drop_counter
,
2121 &stats
->rx_dropped
, &bytes
);
2123 if (vport
->ingress
.drop_counter
)
2124 mlx5_fc_query(dev
, vport
->ingress
.drop_counter
,
2125 &stats
->tx_dropped
, &bytes
);
2127 if (!MLX5_CAP_GEN(dev
, receive_discard_vport_down
) &&
2128 !MLX5_CAP_GEN(dev
, transmit_discard_vport_down
))
2131 err
= mlx5_query_vport_down_stats(dev
, vport_idx
,
2132 &rx_discard_vport_down
,
2133 &tx_discard_vport_down
);
2137 if (MLX5_CAP_GEN(dev
, receive_discard_vport_down
))
2138 stats
->rx_dropped
+= rx_discard_vport_down
;
2139 if (MLX5_CAP_GEN(dev
, transmit_discard_vport_down
))
2140 stats
->tx_dropped
+= tx_discard_vport_down
;
2145 int mlx5_eswitch_get_vport_stats(struct mlx5_eswitch
*esw
,
2147 struct ifla_vf_stats
*vf_stats
)
2149 int outlen
= MLX5_ST_SZ_BYTES(query_vport_counter_out
);
2150 u32 in
[MLX5_ST_SZ_DW(query_vport_counter_in
)] = {0};
2151 struct mlx5_vport_drop_stats stats
= {0};
2155 if (!ESW_ALLOWED(esw
))
2157 if (!LEGAL_VPORT(esw
, vport
))
2160 out
= kvzalloc(outlen
, GFP_KERNEL
);
2164 MLX5_SET(query_vport_counter_in
, in
, opcode
,
2165 MLX5_CMD_OP_QUERY_VPORT_COUNTER
);
2166 MLX5_SET(query_vport_counter_in
, in
, op_mod
, 0);
2167 MLX5_SET(query_vport_counter_in
, in
, vport_number
, vport
);
2169 MLX5_SET(query_vport_counter_in
, in
, other_vport
, 1);
2171 memset(out
, 0, outlen
);
2172 err
= mlx5_cmd_exec(esw
->dev
, in
, sizeof(in
), out
, outlen
);
2176 #define MLX5_GET_CTR(p, x) \
2177 MLX5_GET64(query_vport_counter_out, p, x)
2179 memset(vf_stats
, 0, sizeof(*vf_stats
));
2180 vf_stats
->rx_packets
=
2181 MLX5_GET_CTR(out
, received_eth_unicast
.packets
) +
2182 MLX5_GET_CTR(out
, received_ib_unicast
.packets
) +
2183 MLX5_GET_CTR(out
, received_eth_multicast
.packets
) +
2184 MLX5_GET_CTR(out
, received_ib_multicast
.packets
) +
2185 MLX5_GET_CTR(out
, received_eth_broadcast
.packets
);
2187 vf_stats
->rx_bytes
=
2188 MLX5_GET_CTR(out
, received_eth_unicast
.octets
) +
2189 MLX5_GET_CTR(out
, received_ib_unicast
.octets
) +
2190 MLX5_GET_CTR(out
, received_eth_multicast
.octets
) +
2191 MLX5_GET_CTR(out
, received_ib_multicast
.octets
) +
2192 MLX5_GET_CTR(out
, received_eth_broadcast
.octets
);
2194 vf_stats
->tx_packets
=
2195 MLX5_GET_CTR(out
, transmitted_eth_unicast
.packets
) +
2196 MLX5_GET_CTR(out
, transmitted_ib_unicast
.packets
) +
2197 MLX5_GET_CTR(out
, transmitted_eth_multicast
.packets
) +
2198 MLX5_GET_CTR(out
, transmitted_ib_multicast
.packets
) +
2199 MLX5_GET_CTR(out
, transmitted_eth_broadcast
.packets
);
2201 vf_stats
->tx_bytes
=
2202 MLX5_GET_CTR(out
, transmitted_eth_unicast
.octets
) +
2203 MLX5_GET_CTR(out
, transmitted_ib_unicast
.octets
) +
2204 MLX5_GET_CTR(out
, transmitted_eth_multicast
.octets
) +
2205 MLX5_GET_CTR(out
, transmitted_ib_multicast
.octets
) +
2206 MLX5_GET_CTR(out
, transmitted_eth_broadcast
.octets
);
2208 vf_stats
->multicast
=
2209 MLX5_GET_CTR(out
, received_eth_multicast
.packets
) +
2210 MLX5_GET_CTR(out
, received_ib_multicast
.packets
);
2212 vf_stats
->broadcast
=
2213 MLX5_GET_CTR(out
, received_eth_broadcast
.packets
);
2215 err
= mlx5_eswitch_query_vport_drop_stats(esw
->dev
, vport
, &stats
);
2218 vf_stats
->rx_dropped
= stats
.rx_dropped
;
2219 vf_stats
->tx_dropped
= stats
.tx_dropped
;
2226 u8
mlx5_eswitch_mode(struct mlx5_eswitch
*esw
)
2228 return ESW_ALLOWED(esw
) ? esw
->mode
: SRIOV_NONE
;
2230 EXPORT_SYMBOL_GPL(mlx5_eswitch_mode
);
2232 bool mlx5_esw_lag_prereq(struct mlx5_core_dev
*dev0
, struct mlx5_core_dev
*dev1
)
2234 if ((dev0
->priv
.eswitch
->mode
== SRIOV_NONE
&&
2235 dev1
->priv
.eswitch
->mode
== SRIOV_NONE
) ||
2236 (dev0
->priv
.eswitch
->mode
== SRIOV_OFFLOADS
&&
2237 dev1
->priv
.eswitch
->mode
== SRIOV_OFFLOADS
))