xref: /freebsd/sys/dev/mlx5/mlx5_core/fs_core.h (revision 35bbcf09)
1 /*-
2  * Copyright (c) 2013-2017, Mellanox Technologies, Ltd.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS `AS IS' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25 
26 #ifndef _MLX5_FS_CORE_
27 #define _MLX5_FS_CORE_
28 
29 #include <asm/atomic.h>
30 #include <linux/completion.h>
31 #include <linux/mutex.h>
32 #include <dev/mlx5/fs.h>
33 
34 enum fs_type {
35 	FS_TYPE_NAMESPACE,
36 	FS_TYPE_PRIO,
37 	FS_TYPE_FLOW_TABLE,
38 	FS_TYPE_FLOW_GROUP,
39 	FS_TYPE_FLOW_ENTRY,
40 	FS_TYPE_FLOW_DEST
41 };
42 
43 enum fs_ft_type {
44 	FS_FT_NIC_RX          = 0x0,
45 	FS_FT_ESW_EGRESS_ACL  = 0x2,
46 	FS_FT_ESW_INGRESS_ACL = 0x3,
47 	FS_FT_FDB             = 0X4,
48 	FS_FT_SNIFFER_RX      = 0x5,
49 	FS_FT_SNIFFER_TX      = 0x6
50 };
51 
52 enum fs_fte_status {
53 	FS_FTE_STATUS_EXISTING = 1UL << 0,
54 };
55 
56 /* Should always be the first variable in the struct */
57 struct fs_base {
58 	struct list_head			list;
59 	struct fs_base			*parent;
60 	enum fs_type			type;
61 	struct kref			refcount;
62 	/* lock the node for writing and traversing */
63 	struct mutex		lock;
64 	struct completion			complete;
65 	atomic_t			users_refcount;
66 	const char			*name;
67 };
68 
69 struct mlx5_flow_rule {
70 	struct fs_base				base;
71 	struct mlx5_flow_destination		dest_attr;
72 	struct list_head			clients_data;
73 	/*protect clients lits*/
74 	struct mutex			clients_lock;
75 };
76 
77 struct fs_fte {
78 	struct fs_base				base;
79 	u32					val[MLX5_ST_SZ_DW(fte_match_param)];
80 	uint32_t				dests_size;
81 	struct list_head			dests;
82 	uint32_t				index; /* index in ft */
83 	struct mlx5_flow_act			flow_act;
84 	u32					sw_action; /* enum mlx5_rule_fwd_action */
85 	enum fs_fte_status			status;
86 };
87 
88 struct fs_star_rule {
89 	struct	mlx5_flow_group	 *fg;
90 	struct	fs_fte		*fte;
91 };
92 
93 struct mlx5_flow_table {
94 	struct fs_base			base;
95 	/* sorted list by start_index */
96 	struct list_head		fgs;
97 	struct {
98 		bool			active;
99 		unsigned int		max_types;
100 		unsigned int		group_size;
101 		unsigned int		num_types;
102 		unsigned int		max_fte;
103 	} autogroup;
104 	unsigned int			max_fte;
105 	unsigned int			level;
106 	uint32_t			id;
107 	u16                             vport;
108 	enum fs_ft_type			type;
109 	struct fs_star_rule		star_rule;
110 	unsigned int			shared_refcount;
111 };
112 
113 enum fs_prio_flags {
114 	MLX5_CORE_FS_PRIO_SHARED = 1
115 };
116 
117 struct fs_prio {
118 	struct fs_base			base;
119 	struct list_head		objs; /* each object is a namespace or ft */
120 	unsigned int			max_ft;
121 	unsigned int			num_ft;
122 	unsigned int			max_ns;
123 	unsigned int			prio;
124 	/*When create shared flow table, this lock should be taken*/
125 	struct mutex		shared_lock;
126 	u8				flags;
127 };
128 
129 struct mlx5_flow_namespace {
130 	/* parent == NULL => root ns */
131 	struct	fs_base			base;
132 	/* sorted by priority number */
133 	struct	list_head		prios; /* list of fs_prios */
134 	struct  list_head		list_notifiers;
135 	struct	rw_semaphore		notifiers_rw_sem;
136 	struct  rw_semaphore		dests_rw_sem;
137 };
138 
139 struct mlx5_flow_root_namespace {
140 	struct mlx5_flow_namespace	ns;
141 	struct mlx5_flow_table		*ft_level_0;
142 	enum   fs_ft_type		table_type;
143 	struct mlx5_core_dev		*dev;
144 	struct mlx5_flow_table		*root_ft;
145 	/* When chaining flow-tables, this lock should be taken */
146 	struct mutex		fs_chain_lock;
147 };
148 
149 struct mlx5_flow_group {
150 	struct fs_base			base;
151 	struct list_head		ftes;
152 	struct mlx5_core_fs_mask	mask;
153 	uint32_t			start_index;
154 	uint32_t			max_ftes;
155 	uint32_t			num_ftes;
156 	uint32_t			id;
157 };
158 
159 struct mlx5_flow_handler {
160 	struct list_head list;
161 	rule_event_fn add_dst_cb;
162 	rule_event_fn del_dst_cb;
163 	void *client_context;
164 	struct mlx5_flow_namespace *ns;
165 };
166 
167 struct fs_client_priv_data {
168 	struct mlx5_flow_handler *fs_handler;
169 	struct list_head list;
170 	void   *client_dst_data;
171 };
172 
173 struct mlx5_modify_hdr {
174 	enum mlx5_flow_namespace_type ns_type;
175 	u32 id;
176 };
177 
178 struct mlx5_pkt_reformat {
179         enum mlx5_flow_namespace_type ns_type;
180         int reformat_type; /* from mlx5_ifc */
181 	u32 id;
182 };
183 
184 void _fs_remove_node(struct kref *kref);
185 #define fs_get_obj(v, _base)  {v = container_of((_base), typeof(*v), base); }
186 #define fs_get_parent(v, child)  {v = (child)->base.parent ?		     \
187 				  container_of((child)->base.parent,	     \
188 					       typeof(*v), base) : NULL; }
189 
190 #define fs_list_for_each_entry(pos, cond, root)		\
191 	list_for_each_entry(pos, root, base.list)	\
192 		if (!(cond)) {} else
193 
194 #define fs_list_for_each_entry_continue(pos, cond, root)	\
195 	list_for_each_entry_continue(pos, root, base.list)	\
196 		if (!(cond)) {} else
197 
198 #define fs_list_for_each_entry_reverse(pos, cond, root)		\
199 	list_for_each_entry_reverse(pos, root, base.list)	\
200 		if (!(cond)) {} else
201 
202 #define fs_list_for_each_entry_continue_reverse(pos, cond, root)	\
203 	list_for_each_entry_continue_reverse(pos, root, base.list)	\
204 		if (!(cond)) {} else
205 
206 #define fs_for_each_ft(pos, prio)			\
207 	fs_list_for_each_entry(pos, (pos)->base.type == FS_TYPE_FLOW_TABLE, \
208 			       &(prio)->objs)
209 
210 #define fs_for_each_ft_reverse(pos, prio)			\
211 	fs_list_for_each_entry_reverse(pos,			\
212 				       (pos)->base.type == FS_TYPE_FLOW_TABLE, \
213 				       &(prio)->objs)
214 
215 #define fs_for_each_ns(pos, prio)			\
216 	fs_list_for_each_entry(pos,			\
217 			       (pos)->base.type == FS_TYPE_NAMESPACE, \
218 			       &(prio)->objs)
219 
220 #define fs_for_each_ns_or_ft_reverse(pos, prio)			\
221 	list_for_each_entry_reverse(pos, &(prio)->objs, list)		\
222 		if (!((pos)->type == FS_TYPE_NAMESPACE ||		\
223 		      (pos)->type == FS_TYPE_FLOW_TABLE)) {} else
224 
225 #define fs_for_each_ns_or_ft(pos, prio)			\
226 	list_for_each_entry(pos, &(prio)->objs, list)		\
227 		if (!((pos)->type == FS_TYPE_NAMESPACE ||	\
228 		      (pos)->type == FS_TYPE_FLOW_TABLE)) {} else
229 
230 #define fs_for_each_ns_or_ft_continue_reverse(pos, prio)		\
231 	list_for_each_entry_continue_reverse(pos, &(prio)->objs, list)	\
232 		if (!((pos)->type == FS_TYPE_NAMESPACE ||		\
233 		      (pos)->type == FS_TYPE_FLOW_TABLE)) {} else
234 
235 #define fs_for_each_ns_or_ft_continue(pos, prio)			\
236 	list_for_each_entry_continue(pos, &(prio)->objs, list)		\
237 		if (!((pos)->type == FS_TYPE_NAMESPACE ||		\
238 		      (pos)->type == FS_TYPE_FLOW_TABLE)) {} else
239 
240 #define fs_for_each_prio(pos, ns)			\
241 	fs_list_for_each_entry(pos, (pos)->base.type == FS_TYPE_PRIO, \
242 			       &(ns)->prios)
243 
244 #define fs_for_each_prio_reverse(pos, ns)			\
245 	fs_list_for_each_entry_reverse(pos, (pos)->base.type == FS_TYPE_PRIO, \
246 				       &(ns)->prios)
247 
248 #define fs_for_each_prio_continue(pos, ns)			\
249 	fs_list_for_each_entry_continue(pos, (pos)->base.type == FS_TYPE_PRIO, \
250 				       &(ns)->prios)
251 
252 #define fs_for_each_prio_continue_reverse(pos, ns)			\
253 	fs_list_for_each_entry_continue_reverse(pos,			\
254 						(pos)->base.type == FS_TYPE_PRIO, \
255 						&(ns)->prios)
256 
257 #define fs_for_each_fg(pos, ft)			\
258 	fs_list_for_each_entry(pos, (pos)->base.type == FS_TYPE_FLOW_GROUP, \
259 			       &(ft)->fgs)
260 
261 #define fs_for_each_fte(pos, fg)			\
262 	fs_list_for_each_entry(pos, (pos)->base.type == FS_TYPE_FLOW_ENTRY, \
263 			       &(fg)->ftes)
264 #define fs_for_each_dst(pos, fte)			\
265 	fs_list_for_each_entry(pos, (pos)->base.type == FS_TYPE_FLOW_DEST, \
266 			       &(fte)->dests)
267 
268 int mlx5_cmd_fs_create_ft(struct mlx5_core_dev *dev,
269 			  u16 vport, enum fs_ft_type type, unsigned int level,
270 			  unsigned int log_size, const char *name, unsigned int *table_id);
271 
272 int mlx5_cmd_fs_destroy_ft(struct mlx5_core_dev *dev,
273 			   u16 vport,
274 			   enum fs_ft_type type, unsigned int table_id);
275 
276 int mlx5_cmd_fs_create_fg(struct mlx5_core_dev *dev,
277 			  u32 *in,
278 			  u16 vport,
279 			  enum fs_ft_type type, unsigned int table_id,
280 			  unsigned int *group_id);
281 
282 int mlx5_cmd_fs_destroy_fg(struct mlx5_core_dev *dev,
283 			   u16 vport,
284 			   enum fs_ft_type type, unsigned int table_id,
285 			   unsigned int group_id);
286 
287 
288 int mlx5_cmd_fs_set_fte(struct mlx5_core_dev *dev,
289 			u16 vport,
290 			enum fs_fte_status *fte_status,
291 			u32 *match_val,
292 			enum fs_ft_type type, unsigned int table_id,
293 			unsigned int index, unsigned int group_id,
294 			struct mlx5_flow_act *flow_act,
295 			u32 sw_action, int dest_size,
296 			struct list_head *dests);  /* mlx5_flow_desination */
297 
298 int mlx5_cmd_fs_delete_fte(struct mlx5_core_dev *dev,
299 			   u16 vport,
300 			   enum fs_fte_status *fte_status,
301 			   enum fs_ft_type type, unsigned int table_id,
302 			   unsigned int index);
303 
304 int mlx5_cmd_update_root_ft(struct mlx5_core_dev *dev,
305 			    enum fs_ft_type type,
306 			    unsigned int id);
307 
308 int mlx5_init_fs(struct mlx5_core_dev *dev);
309 void mlx5_cleanup_fs(struct mlx5_core_dev *dev);
310 void mlx5_fc_update_sampling_interval(struct mlx5_core_dev *dev,
311 				      unsigned long interval);
312 
313 int mlx5_cmd_modify_header_alloc(struct mlx5_core_dev *dev,
314 				 enum mlx5_flow_namespace_type namespace,
315 				 u8 num_actions,
316 				 void *modify_actions,
317 				 struct mlx5_modify_hdr *modify_hdr);
318 void mlx5_cmd_modify_header_dealloc(struct mlx5_core_dev *dev,
319 				    struct mlx5_modify_hdr *modify_hdr);
320 int mlx5_cmd_packet_reformat_alloc(struct mlx5_core_dev *dev,
321 				   struct mlx5_pkt_reformat_params *params,
322 				   enum mlx5_flow_namespace_type namespace,
323 				   struct mlx5_pkt_reformat *pkt_reformat);
324 void mlx5_cmd_packet_reformat_dealloc(struct mlx5_core_dev *dev,
325 				      struct mlx5_pkt_reformat *pkt_reformat);
326 int mlx5_init_fc_stats(struct mlx5_core_dev *dev);
327 void mlx5_cleanup_fc_stats(struct mlx5_core_dev *dev);
328 void mlx5_fc_queue_stats_work(struct mlx5_core_dev *dev,
329 			      struct delayed_work *dwork,
330 			      unsigned long delay);
331 #endif
332