net/mlx5: E-Switch, Add SR-IOV (FDB) support
[sfrench/cifs-2.6.git] / drivers / net / ethernet / mellanox / mlx5 / core / eswitch.h
1 /*
2  * Copyright (c) 2015, Mellanox Technologies, Ltd.  All rights reserved.
3  *
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:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
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.
22  *
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
30  * SOFTWARE.
31  */
32
33 #ifndef __MLX5_ESWITCH_H__
34 #define __MLX5_ESWITCH_H__
35
36 #include <linux/mlx5/device.h>
37
38 #define MLX5_MAX_UC_PER_VPORT(dev) \
39         (1 << MLX5_CAP_GEN(dev, log_max_current_uc_list))
40
41 #define MLX5_MAX_MC_PER_VPORT(dev) \
42         (1 << MLX5_CAP_GEN(dev, log_max_current_mc_list))
43
44 #define MLX5_L2_ADDR_HASH_SIZE (BIT(BITS_PER_BYTE))
45 #define MLX5_L2_ADDR_HASH(addr) (addr[5])
46
47 /* L2 -mac address based- hash helpers */
48 struct l2addr_node {
49         struct hlist_node hlist;
50         u8                addr[ETH_ALEN];
51 };
52
53 #define for_each_l2hash_node(hn, tmp, hash, i) \
54         for (i = 0; i < MLX5_L2_ADDR_HASH_SIZE; i++) \
55                 hlist_for_each_entry_safe(hn, tmp, &hash[i], hlist)
56
57 #define l2addr_hash_find(hash, mac, type) ({                \
58         int ix = MLX5_L2_ADDR_HASH(mac);                    \
59         bool found = false;                                 \
60         type *ptr = NULL;                                   \
61                                                             \
62         hlist_for_each_entry(ptr, &hash[ix], node.hlist)    \
63                 if (ether_addr_equal(ptr->node.addr, mac)) {\
64                         found = true;                       \
65                         break;                              \
66                 }                                           \
67         if (!found)                                         \
68                 ptr = NULL;                                 \
69         ptr;                                                \
70 })
71
72 #define l2addr_hash_add(hash, mac, type, gfp) ({            \
73         int ix = MLX5_L2_ADDR_HASH(mac);                    \
74         type *ptr = NULL;                                   \
75                                                             \
76         ptr = kzalloc(sizeof(type), gfp);                   \
77         if (ptr) {                                          \
78                 ether_addr_copy(ptr->node.addr, mac);       \
79                 hlist_add_head(&ptr->node.hlist, &hash[ix]);\
80         }                                                   \
81         ptr;                                                \
82 })
83
84 #define l2addr_hash_del(ptr) ({                             \
85         hlist_del(&ptr->node.hlist);                        \
86         kfree(ptr);                                         \
87 })
88
89 struct mlx5_flow_rule {
90         void             *ft;
91         u32              fi;
92         u8               match_criteria_enable;
93         u32              *match_criteria;
94         u32              *match_value;
95         u32              action;
96         u32              flow_tag;
97         bool             valid;
98         atomic_t         refcount;
99         struct mutex     mutex; /* protect flow rule updates */
100         struct list_head dest_list;
101 };
102
103 struct mlx5_vport {
104         struct mlx5_core_dev    *dev;
105         int                     vport;
106         struct hlist_head       uc_list[MLX5_L2_ADDR_HASH_SIZE];
107         struct hlist_head       mc_list[MLX5_L2_ADDR_HASH_SIZE];
108         struct work_struct      vport_change_handler;
109
110         /* This spinlock protects access to vport data, between
111          * "esw_vport_disable" and ongoing interrupt "mlx5_eswitch_vport_event"
112          * once vport marked as disabled new interrupts are discarded.
113          */
114         spinlock_t              lock; /* vport events sync */
115         bool                    enabled;
116         u16                     enabled_events;
117 };
118
119 struct mlx5_l2_table {
120         struct hlist_head l2_hash[MLX5_L2_ADDR_HASH_SIZE];
121         u32                  size;
122         unsigned long        *bitmap;
123 };
124
125 struct mlx5_eswitch_fdb {
126         void *fdb;
127 };
128
129 struct mlx5_eswitch {
130         struct mlx5_core_dev    *dev;
131         struct mlx5_l2_table    l2_table;
132         struct mlx5_eswitch_fdb fdb_table;
133         struct hlist_head       mc_table[MLX5_L2_ADDR_HASH_SIZE];
134         struct workqueue_struct *work_queue;
135         struct mlx5_vport       *vports;
136         int                     total_vports;
137         int                     enabled_vports;
138 };
139
140 /* E-Switch API */
141 int mlx5_eswitch_init(struct mlx5_core_dev *dev);
142 void mlx5_eswitch_cleanup(struct mlx5_eswitch *esw);
143 void mlx5_eswitch_vport_event(struct mlx5_eswitch *esw, struct mlx5_eqe *eqe);
144 int mlx5_eswitch_enable_sriov(struct mlx5_eswitch *esw, int nvfs);
145 void mlx5_eswitch_disable_sriov(struct mlx5_eswitch *esw);
146
147 #endif /* __MLX5_ESWITCH_H__ */