2 * Copyright (c) 2015, Mellanox Technologies, Ltd. 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 #ifndef __MLX5_ESWITCH_H__
34 #define __MLX5_ESWITCH_H__
36 #include <linux/if_ether.h>
37 #include <linux/if_link.h>
38 #include <net/devlink.h>
39 #include <net/ip_tunnels.h>
40 #include <linux/mlx5/device.h>
42 #define MLX5_MAX_UC_PER_VPORT(dev) \
43 (1 << MLX5_CAP_GEN(dev, log_max_current_uc_list))
45 #define MLX5_MAX_MC_PER_VPORT(dev) \
46 (1 << MLX5_CAP_GEN(dev, log_max_current_mc_list))
48 #define MLX5_L2_ADDR_HASH_SIZE (BIT(BITS_PER_BYTE))
49 #define MLX5_L2_ADDR_HASH(addr) (addr[5])
51 #define FDB_UPLINK_VPORT 0xffff
53 /* L2 -mac address based- hash helpers */
55 struct hlist_node hlist;
59 #define for_each_l2hash_node(hn, tmp, hash, i) \
60 for (i = 0; i < MLX5_L2_ADDR_HASH_SIZE; i++) \
61 hlist_for_each_entry_safe(hn, tmp, &hash[i], hlist)
63 #define l2addr_hash_find(hash, mac, type) ({ \
64 int ix = MLX5_L2_ADDR_HASH(mac); \
68 hlist_for_each_entry(ptr, &hash[ix], node.hlist) \
69 if (ether_addr_equal(ptr->node.addr, mac)) {\
78 #define l2addr_hash_add(hash, mac, type, gfp) ({ \
79 int ix = MLX5_L2_ADDR_HASH(mac); \
82 ptr = kzalloc(sizeof(type), gfp); \
84 ether_addr_copy(ptr->node.addr, mac); \
85 hlist_add_head(&ptr->node.hlist, &hash[ix]);\
90 #define l2addr_hash_del(ptr) ({ \
91 hlist_del(&ptr->node.hlist); \
95 struct vport_ingress {
96 struct mlx5_flow_table *acl;
97 struct mlx5_flow_group *allow_untagged_spoofchk_grp;
98 struct mlx5_flow_group *allow_spoofchk_only_grp;
99 struct mlx5_flow_group *allow_untagged_only_grp;
100 struct mlx5_flow_group *drop_grp;
101 struct mlx5_flow_handle *allow_rule;
102 struct mlx5_flow_handle *drop_rule;
105 struct vport_egress {
106 struct mlx5_flow_table *acl;
107 struct mlx5_flow_group *allowed_vlans_grp;
108 struct mlx5_flow_group *drop_grp;
109 struct mlx5_flow_handle *allowed_vlan;
110 struct mlx5_flow_handle *drop_rule;
113 struct mlx5_vport_info {
125 struct mlx5_core_dev *dev;
127 struct hlist_head uc_list[MLX5_L2_ADDR_HASH_SIZE];
128 struct hlist_head mc_list[MLX5_L2_ADDR_HASH_SIZE];
129 struct mlx5_flow_handle *promisc_rule;
130 struct mlx5_flow_handle *allmulti_rule;
131 struct work_struct vport_change_handler;
133 struct vport_ingress ingress;
134 struct vport_egress egress;
136 struct mlx5_vport_info info;
147 struct mlx5_l2_table {
148 struct hlist_head l2_hash[MLX5_L2_ADDR_HASH_SIZE];
150 unsigned long *bitmap;
153 struct mlx5_eswitch_fdb {
157 struct mlx5_flow_group *addr_grp;
158 struct mlx5_flow_group *allmulti_grp;
159 struct mlx5_flow_group *promisc_grp;
162 struct offloads_fdb {
163 struct mlx5_flow_table *fdb;
164 struct mlx5_flow_group *send_to_vport_grp;
165 struct mlx5_flow_group *miss_grp;
166 struct mlx5_flow_handle *miss_rule;
167 int vlan_push_pop_refcount;
179 struct mlx5_flow_handle *send_to_vport_rule;
180 struct list_head list;
183 struct mlx5_eswitch_rep {
184 int (*load)(struct mlx5_eswitch *esw,
185 struct mlx5_eswitch_rep *rep);
186 void (*unload)(struct mlx5_eswitch *esw,
187 struct mlx5_eswitch_rep *rep);
190 struct net_device *netdev;
192 struct mlx5_flow_handle *vport_rx_rule;
193 struct list_head vport_sqs_list;
199 struct mlx5_esw_offload {
200 struct mlx5_flow_table *ft_offloads;
201 struct mlx5_flow_group *vport_rx_group;
202 struct mlx5_eswitch_rep *vport_reps;
203 DECLARE_HASHTABLE(encap_tbl, 8);
207 struct mlx5_eswitch {
208 struct mlx5_core_dev *dev;
209 struct mlx5_l2_table l2_table;
210 struct mlx5_eswitch_fdb fdb_table;
211 struct hlist_head mc_table[MLX5_L2_ADDR_HASH_SIZE];
212 struct workqueue_struct *work_queue;
213 struct mlx5_vport *vports;
216 /* Synchronize between vport change events
217 * and async SRIOV admin state changes
219 struct mutex state_lock;
220 struct esw_mc_addr *mc_promisc;
227 struct mlx5_esw_offload offloads;
231 void esw_offloads_cleanup(struct mlx5_eswitch *esw, int nvports);
232 int esw_offloads_init(struct mlx5_eswitch *esw, int nvports);
235 int mlx5_eswitch_init(struct mlx5_core_dev *dev);
236 void mlx5_eswitch_cleanup(struct mlx5_eswitch *esw);
237 void mlx5_eswitch_attach(struct mlx5_eswitch *esw);
238 void mlx5_eswitch_detach(struct mlx5_eswitch *esw);
239 void mlx5_eswitch_vport_event(struct mlx5_eswitch *esw, struct mlx5_eqe *eqe);
240 int mlx5_eswitch_enable_sriov(struct mlx5_eswitch *esw, int nvfs, int mode);
241 void mlx5_eswitch_disable_sriov(struct mlx5_eswitch *esw);
242 int mlx5_eswitch_set_vport_mac(struct mlx5_eswitch *esw,
243 int vport, u8 mac[ETH_ALEN]);
244 int mlx5_eswitch_set_vport_state(struct mlx5_eswitch *esw,
245 int vport, int link_state);
246 int mlx5_eswitch_set_vport_vlan(struct mlx5_eswitch *esw,
247 int vport, u16 vlan, u8 qos);
248 int mlx5_eswitch_set_vport_spoofchk(struct mlx5_eswitch *esw,
249 int vport, bool spoofchk);
250 int mlx5_eswitch_set_vport_trust(struct mlx5_eswitch *esw,
251 int vport_num, bool setting);
252 int mlx5_eswitch_set_vport_rate(struct mlx5_eswitch *esw,
253 int vport, u32 max_rate);
254 int mlx5_eswitch_get_vport_config(struct mlx5_eswitch *esw,
255 int vport, struct ifla_vf_info *ivi);
256 int mlx5_eswitch_get_vport_stats(struct mlx5_eswitch *esw,
258 struct ifla_vf_stats *vf_stats);
260 struct mlx5_flow_spec;
261 struct mlx5_esw_flow_attr;
263 struct mlx5_flow_handle *
264 mlx5_eswitch_add_offloaded_rule(struct mlx5_eswitch *esw,
265 struct mlx5_flow_spec *spec,
266 struct mlx5_esw_flow_attr *attr);
267 struct mlx5_flow_handle *
268 mlx5_eswitch_create_vport_rx_rule(struct mlx5_eswitch *esw, int vport, u32 tirn);
271 SET_VLAN_STRIP = BIT(0),
272 SET_VLAN_INSERT = BIT(1)
275 #define MLX5_FLOW_CONTEXT_ACTION_VLAN_POP 0x40
276 #define MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH 0x80
278 struct mlx5_encap_entry {
279 struct hlist_node encap_hlist;
280 struct list_head flows;
283 struct ip_tunnel_info tun_info;
284 unsigned char h_dest[ETH_ALEN]; /* destination eth addr */
286 struct net_device *out_dev;
290 struct mlx5_esw_flow_attr {
291 struct mlx5_eswitch_rep *in_rep;
292 struct mlx5_eswitch_rep *out_rep;
297 struct mlx5_encap_entry *encap;
300 int mlx5_eswitch_sqs2vport_start(struct mlx5_eswitch *esw,
301 struct mlx5_eswitch_rep *rep,
302 u16 *sqns_array, int sqns_num);
303 void mlx5_eswitch_sqs2vport_stop(struct mlx5_eswitch *esw,
304 struct mlx5_eswitch_rep *rep);
306 int mlx5_devlink_eswitch_mode_set(struct devlink *devlink, u16 mode);
307 int mlx5_devlink_eswitch_mode_get(struct devlink *devlink, u16 *mode);
308 int mlx5_devlink_eswitch_inline_mode_set(struct devlink *devlink, u8 mode);
309 int mlx5_devlink_eswitch_inline_mode_get(struct devlink *devlink, u8 *mode);
310 int mlx5_eswitch_inline_mode_get(struct mlx5_eswitch *esw, int nvfs, u8 *mode);
311 void mlx5_eswitch_register_vport_rep(struct mlx5_eswitch *esw,
313 struct mlx5_eswitch_rep *rep);
314 void mlx5_eswitch_unregister_vport_rep(struct mlx5_eswitch *esw,
316 struct net_device *mlx5_eswitch_get_uplink_netdev(struct mlx5_eswitch *esw);
318 int mlx5_eswitch_add_vlan_action(struct mlx5_eswitch *esw,
319 struct mlx5_esw_flow_attr *attr);
320 int mlx5_eswitch_del_vlan_action(struct mlx5_eswitch *esw,
321 struct mlx5_esw_flow_attr *attr);
322 int __mlx5_eswitch_set_vport_vlan(struct mlx5_eswitch *esw,
323 int vport, u16 vlan, u8 qos, u8 set_flags);
325 #define MLX5_DEBUG_ESWITCH_MASK BIT(3)
327 #define esw_info(dev, format, ...) \
328 pr_info("(%s): E-Switch: " format, (dev)->priv.name, ##__VA_ARGS__)
330 #define esw_warn(dev, format, ...) \
331 pr_warn("(%s): E-Switch: " format, (dev)->priv.name, ##__VA_ARGS__)
333 #define esw_debug(dev, format, ...) \
334 mlx5_core_dbg_mask(dev, MLX5_DEBUG_ESWITCH_MASK, format, ##__VA_ARGS__)
335 #endif /* __MLX5_ESWITCH_H__ */