xref: /freebsd/sys/dev/mlx5/mlx5_core/eswitch.h (revision 95ee2897)
191ad1bd9SKonstantin Belousov /*-
291ad1bd9SKonstantin Belousov  * Copyright (c) 2013-2017, Mellanox Technologies, Ltd.  All rights reserved.
391ad1bd9SKonstantin Belousov  *
491ad1bd9SKonstantin Belousov  * Redistribution and use in source and binary forms, with or without
591ad1bd9SKonstantin Belousov  * modification, are permitted provided that the following conditions
691ad1bd9SKonstantin Belousov  * are met:
791ad1bd9SKonstantin Belousov  * 1. Redistributions of source code must retain the above copyright
891ad1bd9SKonstantin Belousov  *    notice, this list of conditions and the following disclaimer.
991ad1bd9SKonstantin Belousov  * 2. Redistributions in binary form must reproduce the above copyright
1091ad1bd9SKonstantin Belousov  *    notice, this list of conditions and the following disclaimer in the
1191ad1bd9SKonstantin Belousov  *    documentation and/or other materials provided with the distribution.
1291ad1bd9SKonstantin Belousov  *
1391ad1bd9SKonstantin Belousov  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS `AS IS' AND
1491ad1bd9SKonstantin Belousov  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1591ad1bd9SKonstantin Belousov  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1691ad1bd9SKonstantin Belousov  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
1791ad1bd9SKonstantin Belousov  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1891ad1bd9SKonstantin Belousov  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1991ad1bd9SKonstantin Belousov  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2091ad1bd9SKonstantin Belousov  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2191ad1bd9SKonstantin Belousov  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2291ad1bd9SKonstantin Belousov  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2391ad1bd9SKonstantin Belousov  * SUCH DAMAGE.
2491ad1bd9SKonstantin Belousov  */
2591ad1bd9SKonstantin Belousov 
2691ad1bd9SKonstantin Belousov #ifndef __MLX5_ESWITCH_H__
2791ad1bd9SKonstantin Belousov #define __MLX5_ESWITCH_H__
2891ad1bd9SKonstantin Belousov 
2991ad1bd9SKonstantin Belousov #include <linux/if_ether.h>
3091ad1bd9SKonstantin Belousov #include <dev/mlx5/device.h>
3191ad1bd9SKonstantin Belousov 
3291ad1bd9SKonstantin Belousov #define MLX5_MAX_UC_PER_VPORT(dev) \
3391ad1bd9SKonstantin Belousov 	(1 << MLX5_CAP_GEN(dev, log_max_current_uc_list))
3491ad1bd9SKonstantin Belousov 
3591ad1bd9SKonstantin Belousov #define MLX5_MAX_MC_PER_VPORT(dev) \
3691ad1bd9SKonstantin Belousov 	(1 << MLX5_CAP_GEN(dev, log_max_current_mc_list))
3791ad1bd9SKonstantin Belousov 
3891ad1bd9SKonstantin Belousov #define MLX5_L2_ADDR_HASH_SIZE (BIT(BITS_PER_BYTE))
3991ad1bd9SKonstantin Belousov #define MLX5_L2_ADDR_HASH(addr) (addr[5])
4091ad1bd9SKonstantin Belousov 
4191ad1bd9SKonstantin Belousov /* L2 -mac address based- hash helpers */
4291ad1bd9SKonstantin Belousov struct l2addr_node {
4391ad1bd9SKonstantin Belousov 	struct hlist_node hlist;
4491ad1bd9SKonstantin Belousov 	u8                addr[ETH_ALEN];
4591ad1bd9SKonstantin Belousov };
4691ad1bd9SKonstantin Belousov 
4791ad1bd9SKonstantin Belousov #define for_each_l2hash_node(hn, tmp, hash, i) \
4891ad1bd9SKonstantin Belousov 	for (i = 0; i < MLX5_L2_ADDR_HASH_SIZE; i++) \
4991ad1bd9SKonstantin Belousov 		hlist_for_each_entry_safe(hn, tmp, &hash[i], hlist)
5091ad1bd9SKonstantin Belousov 
5191ad1bd9SKonstantin Belousov #define l2addr_hash_find(hash, mac, type) ({                \
5291ad1bd9SKonstantin Belousov 	int ix = MLX5_L2_ADDR_HASH(mac);                    \
5391ad1bd9SKonstantin Belousov 	bool found = false;                                 \
5491ad1bd9SKonstantin Belousov 	type *ptr = NULL;                                   \
5591ad1bd9SKonstantin Belousov 							    \
5691ad1bd9SKonstantin Belousov 	hlist_for_each_entry(ptr, &hash[ix], node.hlist)    \
5791ad1bd9SKonstantin Belousov 		if (ether_addr_equal(ptr->node.addr, mac)) {\
5891ad1bd9SKonstantin Belousov 			found = true;                       \
5991ad1bd9SKonstantin Belousov 			break;                              \
6091ad1bd9SKonstantin Belousov 		}                                           \
6191ad1bd9SKonstantin Belousov 	if (!found)                                         \
6291ad1bd9SKonstantin Belousov 		ptr = NULL;                                 \
6391ad1bd9SKonstantin Belousov 	ptr;                                                \
6491ad1bd9SKonstantin Belousov })
6591ad1bd9SKonstantin Belousov 
6691ad1bd9SKonstantin Belousov #define l2addr_hash_add(hash, mac, type, gfp) ({            \
6791ad1bd9SKonstantin Belousov 	int ix = MLX5_L2_ADDR_HASH(mac);                    \
6891ad1bd9SKonstantin Belousov 	type *ptr = NULL;                                   \
6991ad1bd9SKonstantin Belousov 							    \
7091ad1bd9SKonstantin Belousov 	ptr = kzalloc(sizeof(type), gfp);                   \
7191ad1bd9SKonstantin Belousov 	if (ptr) {                                          \
7291ad1bd9SKonstantin Belousov 		ether_addr_copy(ptr->node.addr, mac);       \
7391ad1bd9SKonstantin Belousov 		hlist_add_head(&ptr->node.hlist, &hash[ix]);\
7491ad1bd9SKonstantin Belousov 	}                                                   \
7591ad1bd9SKonstantin Belousov 	ptr;                                                \
7691ad1bd9SKonstantin Belousov })
7791ad1bd9SKonstantin Belousov 
7891ad1bd9SKonstantin Belousov #define l2addr_hash_del(ptr) ({                             \
7991ad1bd9SKonstantin Belousov 	hlist_del(&ptr->node.hlist);                        \
8091ad1bd9SKonstantin Belousov 	kfree(ptr);                                         \
8191ad1bd9SKonstantin Belousov })
8291ad1bd9SKonstantin Belousov 
8391ad1bd9SKonstantin Belousov struct vport_ingress {
8491ad1bd9SKonstantin Belousov 	struct mlx5_flow_table *acl;
8591ad1bd9SKonstantin Belousov 	struct mlx5_flow_group *drop_grp;
8691ad1bd9SKonstantin Belousov 	struct mlx5_flow_rule  *drop_rule;
8791ad1bd9SKonstantin Belousov };
8891ad1bd9SKonstantin Belousov 
8991ad1bd9SKonstantin Belousov struct vport_egress {
9091ad1bd9SKonstantin Belousov 	struct mlx5_flow_table *acl;
9191ad1bd9SKonstantin Belousov 	struct mlx5_flow_group *allowed_vlans_grp;
9291ad1bd9SKonstantin Belousov 	struct mlx5_flow_group *drop_grp;
9391ad1bd9SKonstantin Belousov 	struct mlx5_flow_rule  *allowed_vlan;
9491ad1bd9SKonstantin Belousov 	struct mlx5_flow_rule  *drop_rule;
9591ad1bd9SKonstantin Belousov };
9691ad1bd9SKonstantin Belousov 
9791ad1bd9SKonstantin Belousov struct mlx5_vport {
9891ad1bd9SKonstantin Belousov 	struct mlx5_core_dev    *dev;
9991ad1bd9SKonstantin Belousov 	int                     vport;
10091ad1bd9SKonstantin Belousov 	struct hlist_head       uc_list[MLX5_L2_ADDR_HASH_SIZE];
10191ad1bd9SKonstantin Belousov 	struct hlist_head       mc_list[MLX5_L2_ADDR_HASH_SIZE];
10291ad1bd9SKonstantin Belousov 	struct work_struct      vport_change_handler;
10391ad1bd9SKonstantin Belousov 
10491ad1bd9SKonstantin Belousov 	struct vport_ingress    ingress;
10591ad1bd9SKonstantin Belousov 	struct vport_egress     egress;
10691ad1bd9SKonstantin Belousov 
10791ad1bd9SKonstantin Belousov 	u16                     vlan;
10891ad1bd9SKonstantin Belousov 	u8                      qos;
10991ad1bd9SKonstantin Belousov 	struct mutex	state_lock; /* protect dynamic state changes */
11091ad1bd9SKonstantin Belousov 	/* This spinlock protects access to vport data, between
11191ad1bd9SKonstantin Belousov 	 * "esw_vport_disable" and ongoing interrupt "mlx5_eswitch_vport_event"
11291ad1bd9SKonstantin Belousov 	 * once vport marked as disabled new interrupts are discarded.
11391ad1bd9SKonstantin Belousov 	 */
11491ad1bd9SKonstantin Belousov 	spinlock_t              lock; /* vport events sync */
11591ad1bd9SKonstantin Belousov 	bool                    enabled;
11691ad1bd9SKonstantin Belousov 	u16                     enabled_events;
11791ad1bd9SKonstantin Belousov };
11891ad1bd9SKonstantin Belousov 
11991ad1bd9SKonstantin Belousov struct mlx5_l2_table {
12091ad1bd9SKonstantin Belousov 	struct hlist_head l2_hash[MLX5_L2_ADDR_HASH_SIZE];
12191ad1bd9SKonstantin Belousov 	u32                  size;
12291ad1bd9SKonstantin Belousov 	unsigned long        *bitmap;
12391ad1bd9SKonstantin Belousov };
12491ad1bd9SKonstantin Belousov 
12591ad1bd9SKonstantin Belousov struct mlx5_eswitch_fdb {
12691ad1bd9SKonstantin Belousov 	void *fdb;
12791ad1bd9SKonstantin Belousov 	struct mlx5_flow_group *addr_grp;
12891ad1bd9SKonstantin Belousov };
12991ad1bd9SKonstantin Belousov 
13091ad1bd9SKonstantin Belousov struct mlx5_eswitch {
13191ad1bd9SKonstantin Belousov 	struct mlx5_core_dev    *dev;
13291ad1bd9SKonstantin Belousov 	struct mlx5_l2_table    l2_table;
13391ad1bd9SKonstantin Belousov 	struct mlx5_eswitch_fdb fdb_table;
13491ad1bd9SKonstantin Belousov 	struct hlist_head       mc_table[MLX5_L2_ADDR_HASH_SIZE];
13591ad1bd9SKonstantin Belousov 	struct workqueue_struct *work_queue;
13691ad1bd9SKonstantin Belousov 	struct mlx5_vport       *vports;
13791ad1bd9SKonstantin Belousov 	int                     total_vports;
13891ad1bd9SKonstantin Belousov 	int                     enabled_vports;
13991ad1bd9SKonstantin Belousov };
14091ad1bd9SKonstantin Belousov 
14191ad1bd9SKonstantin Belousov struct mlx5_esw_vport_info {
14291ad1bd9SKonstantin Belousov 	__u32 vf;
14391ad1bd9SKonstantin Belousov 	__u8 mac[32];
14491ad1bd9SKonstantin Belousov 	__u32 vlan;
14591ad1bd9SKonstantin Belousov 	__u32 qos;
14691ad1bd9SKonstantin Belousov 	__u32 spoofchk;
14791ad1bd9SKonstantin Belousov 	__u32 linkstate;
14891ad1bd9SKonstantin Belousov 	__u32 min_tx_rate;
14991ad1bd9SKonstantin Belousov 	__u32 max_tx_rate;
15091ad1bd9SKonstantin Belousov };
15191ad1bd9SKonstantin Belousov 
15291ad1bd9SKonstantin Belousov /* E-Switch API */
15390959e7eSKonstantin Belousov int mlx5_eswitch_init(struct mlx5_core_dev *dev, int total_vports);
15491ad1bd9SKonstantin Belousov void mlx5_eswitch_cleanup(struct mlx5_eswitch *esw);
15591ad1bd9SKonstantin Belousov void mlx5_eswitch_vport_event(struct mlx5_eswitch *esw, struct mlx5_eqe *eqe);
15691ad1bd9SKonstantin Belousov int mlx5_eswitch_enable_sriov(struct mlx5_eswitch *esw, int nvfs);
15791ad1bd9SKonstantin Belousov void mlx5_eswitch_disable_sriov(struct mlx5_eswitch *esw);
15891ad1bd9SKonstantin Belousov int mlx5_eswitch_set_vport_mac(struct mlx5_eswitch *esw,
15991ad1bd9SKonstantin Belousov 			       int vport, u8 mac[ETH_ALEN]);
16091ad1bd9SKonstantin Belousov int mlx5_eswitch_set_vport_state(struct mlx5_eswitch *esw,
16191ad1bd9SKonstantin Belousov 				 int vport, int link_state);
16291ad1bd9SKonstantin Belousov int mlx5_eswitch_set_vport_vlan(struct mlx5_eswitch *esw,
16391ad1bd9SKonstantin Belousov 				int vport, u16 vlan, u8 qos);
16491ad1bd9SKonstantin Belousov int mlx5_eswitch_get_vport_config(struct mlx5_eswitch *esw,
16591ad1bd9SKonstantin Belousov 				  int vport, struct mlx5_esw_vport_info *evi);
16691ad1bd9SKonstantin Belousov 
16791ad1bd9SKonstantin Belousov #endif /* __MLX5_ESWITCH_H__ */
168