2 * Copyright (c) 2013-2015, Mellanox Technologies, Ltd. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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
28 #include <dev/mlx5/driver.h>
29 #include <dev/mlx5/flow_table.h>
30 #include "mlx5_core.h"
33 struct mlx5_flow_table_group g;
38 struct mlx5_flow_table {
39 struct mlx5_core_dev *dev;
44 struct mutex mutex; /* sync bitmap alloc */
46 struct mlx5_ftg *group;
47 unsigned long *bitmap;
51 static int mlx5_set_flow_entry_cmd(struct mlx5_flow_table *ft, u32 group_ix,
52 u32 flow_index, void *flow_context)
54 u32 out[MLX5_ST_SZ_DW(set_fte_out)];
56 void *in_flow_context;
58 MLX5_GET(flow_context, flow_context, destination_list_size) *
59 MLX5_ST_SZ_BYTES(dest_format_struct);
60 int inlen = MLX5_ST_SZ_BYTES(set_fte_in) + fcdls;
63 in = mlx5_vzalloc(inlen);
65 mlx5_core_warn(ft->dev, "failed to allocate inbox\n");
69 MLX5_SET(set_fte_in, in, vport_number, ft->vport);
70 MLX5_SET(set_fte_in, in, other_vport, !!ft->vport);
71 MLX5_SET(set_fte_in, in, table_type, ft->type);
72 MLX5_SET(set_fte_in, in, table_id, ft->id);
73 MLX5_SET(set_fte_in, in, flow_index, flow_index);
74 MLX5_SET(set_fte_in, in, opcode, MLX5_CMD_OP_SET_FLOW_TABLE_ENTRY);
76 in_flow_context = MLX5_ADDR_OF(set_fte_in, in, flow_context);
77 memcpy(in_flow_context, flow_context,
78 MLX5_ST_SZ_BYTES(flow_context) + fcdls);
80 MLX5_SET(flow_context, in_flow_context, group_id, ft->group[group_ix].id);
82 memset(out, 0, sizeof(out));
83 err = mlx5_cmd_exec_check_status(ft->dev, in, inlen, out,
90 static void mlx5_del_flow_entry_cmd(struct mlx5_flow_table *ft, u32 flow_index)
92 u32 in[MLX5_ST_SZ_DW(delete_fte_in)];
93 u32 out[MLX5_ST_SZ_DW(delete_fte_out)];
95 memset(in, 0, sizeof(in));
96 memset(out, 0, sizeof(out));
98 #define MLX5_SET_DFTEI(p, x, v) MLX5_SET(delete_fte_in, p, x, v)
99 MLX5_SET_DFTEI(in, vport_number, ft->vport);
100 MLX5_SET_DFTEI(in, other_vport, !!ft->vport);
101 MLX5_SET_DFTEI(in, table_type, ft->type);
102 MLX5_SET_DFTEI(in, table_id, ft->id);
103 MLX5_SET_DFTEI(in, flow_index, flow_index);
104 MLX5_SET_DFTEI(in, opcode, MLX5_CMD_OP_DELETE_FLOW_TABLE_ENTRY);
106 mlx5_cmd_exec_check_status(ft->dev, in, sizeof(in), out, sizeof(out));
109 static void mlx5_destroy_flow_group_cmd(struct mlx5_flow_table *ft, int i)
111 u32 in[MLX5_ST_SZ_DW(destroy_flow_group_in)];
112 u32 out[MLX5_ST_SZ_DW(destroy_flow_group_out)];
114 memset(in, 0, sizeof(in));
115 memset(out, 0, sizeof(out));
117 #define MLX5_SET_DFGI(p, x, v) MLX5_SET(destroy_flow_group_in, p, x, v)
118 MLX5_SET_DFGI(in, vport_number, ft->vport);
119 MLX5_SET_DFGI(in, other_vport, !!ft->vport);
120 MLX5_SET_DFGI(in, table_type, ft->type);
121 MLX5_SET_DFGI(in, table_id, ft->id);
122 MLX5_SET_DFGI(in, opcode, MLX5_CMD_OP_DESTROY_FLOW_GROUP);
123 MLX5_SET_DFGI(in, group_id, ft->group[i].id);
124 mlx5_cmd_exec_check_status(ft->dev, in, sizeof(in), out, sizeof(out));
127 static int mlx5_create_flow_group_cmd(struct mlx5_flow_table *ft, int i)
129 u32 out[MLX5_ST_SZ_DW(create_flow_group_out)];
131 void *in_match_criteria;
132 int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
133 struct mlx5_flow_table_group *g = &ft->group[i].g;
134 u32 start_ix = ft->group[i].start_ix;
135 u32 end_ix = start_ix + (1 << g->log_sz) - 1;
138 in = mlx5_vzalloc(inlen);
140 mlx5_core_warn(ft->dev, "failed to allocate inbox\n");
143 in_match_criteria = MLX5_ADDR_OF(create_flow_group_in, in,
146 memset(out, 0, sizeof(out));
148 #define MLX5_SET_CFGI(p, x, v) MLX5_SET(create_flow_group_in, p, x, v)
149 MLX5_SET_CFGI(in, vport_number, ft->vport);
150 MLX5_SET_CFGI(in, other_vport, !!ft->vport);
151 MLX5_SET_CFGI(in, table_type, ft->type);
152 MLX5_SET_CFGI(in, table_id, ft->id);
153 MLX5_SET_CFGI(in, opcode, MLX5_CMD_OP_CREATE_FLOW_GROUP);
154 MLX5_SET_CFGI(in, start_flow_index, start_ix);
155 MLX5_SET_CFGI(in, end_flow_index, end_ix);
156 MLX5_SET_CFGI(in, match_criteria_enable, g->match_criteria_enable);
158 memcpy(in_match_criteria, g->match_criteria,
159 MLX5_ST_SZ_BYTES(fte_match_param));
161 err = mlx5_cmd_exec_check_status(ft->dev, in, inlen, out,
164 ft->group[i].id = MLX5_GET(create_flow_group_out, out,
172 static void mlx5_destroy_flow_table_groups(struct mlx5_flow_table *ft)
176 for (i = 0; i < ft->num_groups; i++)
177 mlx5_destroy_flow_group_cmd(ft, i);
180 static int mlx5_create_flow_table_groups(struct mlx5_flow_table *ft)
185 for (i = 0; i < ft->num_groups; i++) {
186 err = mlx5_create_flow_group_cmd(ft, i);
188 goto err_destroy_flow_table_groups;
193 err_destroy_flow_table_groups:
194 for (i--; i >= 0; i--)
195 mlx5_destroy_flow_group_cmd(ft, i);
200 static int mlx5_create_flow_table_cmd(struct mlx5_flow_table *ft)
202 u32 in[MLX5_ST_SZ_DW(create_flow_table_in)];
203 u32 out[MLX5_ST_SZ_DW(create_flow_table_out)];
206 memset(in, 0, sizeof(in));
208 MLX5_SET(create_flow_table_in, in, vport_number, ft->vport);
209 MLX5_SET(create_flow_table_in, in, other_vport, !!ft->vport);
210 MLX5_SET(create_flow_table_in, in, table_type, ft->type);
211 MLX5_SET(create_flow_table_in, in, level, ft->level);
212 MLX5_SET(create_flow_table_in, in, log_size, order_base_2(ft->size));
214 MLX5_SET(create_flow_table_in, in, opcode,
215 MLX5_CMD_OP_CREATE_FLOW_TABLE);
217 memset(out, 0, sizeof(out));
218 err = mlx5_cmd_exec_check_status(ft->dev, in, sizeof(in), out,
223 ft->id = MLX5_GET(create_flow_table_out, out, table_id);
228 static void mlx5_destroy_flow_table_cmd(struct mlx5_flow_table *ft)
230 u32 in[MLX5_ST_SZ_DW(destroy_flow_table_in)];
231 u32 out[MLX5_ST_SZ_DW(destroy_flow_table_out)];
233 memset(in, 0, sizeof(in));
234 memset(out, 0, sizeof(out));
236 #define MLX5_SET_DFTI(p, x, v) MLX5_SET(destroy_flow_table_in, p, x, v)
237 MLX5_SET_DFTI(in, vport_number, ft->vport);
238 MLX5_SET_DFTI(in, other_vport, !!ft->vport);
239 MLX5_SET_DFTI(in, table_type, ft->type);
240 MLX5_SET_DFTI(in, table_id, ft->id);
241 MLX5_SET_DFTI(in, opcode, MLX5_CMD_OP_DESTROY_FLOW_TABLE);
243 mlx5_cmd_exec_check_status(ft->dev, in, sizeof(in), out, sizeof(out));
246 static int mlx5_find_group(struct mlx5_flow_table *ft, u8 match_criteria_enable,
247 u32 *match_criteria, int *group_ix)
249 void *mc_outer = MLX5_ADDR_OF(fte_match_param, match_criteria,
251 void *mc_misc = MLX5_ADDR_OF(fte_match_param, match_criteria,
253 void *mc_inner = MLX5_ADDR_OF(fte_match_param, match_criteria,
255 int mc_outer_sz = MLX5_ST_SZ_BYTES(fte_match_set_lyr_2_4);
256 int mc_misc_sz = MLX5_ST_SZ_BYTES(fte_match_set_misc);
257 int mc_inner_sz = MLX5_ST_SZ_BYTES(fte_match_set_lyr_2_4);
260 for (i = 0; i < ft->num_groups; i++) {
261 struct mlx5_flow_table_group *g = &ft->group[i].g;
262 void *gmc_outer = MLX5_ADDR_OF(fte_match_param,
265 void *gmc_misc = MLX5_ADDR_OF(fte_match_param,
268 void *gmc_inner = MLX5_ADDR_OF(fte_match_param,
272 if (g->match_criteria_enable != match_criteria_enable)
275 if (match_criteria_enable & MLX5_MATCH_OUTER_HEADERS)
276 if (memcmp(mc_outer, gmc_outer, mc_outer_sz))
279 if (match_criteria_enable & MLX5_MATCH_MISC_PARAMETERS)
280 if (memcmp(mc_misc, gmc_misc, mc_misc_sz))
283 if (match_criteria_enable & MLX5_MATCH_INNER_HEADERS)
284 if (memcmp(mc_inner, gmc_inner, mc_inner_sz))
294 static int alloc_flow_index(struct mlx5_flow_table *ft, int group_ix, u32 *ix)
296 struct mlx5_ftg *g = &ft->group[group_ix];
299 mutex_lock(&ft->mutex);
301 *ix = find_next_zero_bit(ft->bitmap, ft->size, g->start_ix);
302 if (*ix >= (g->start_ix + (1 << g->g.log_sz)))
305 __set_bit(*ix, ft->bitmap);
307 mutex_unlock(&ft->mutex);
312 static void mlx5_free_flow_index(struct mlx5_flow_table *ft, u32 ix)
314 __clear_bit(ix, ft->bitmap);
317 int mlx5_add_flow_table_entry(void *flow_table, u8 match_criteria_enable,
318 void *match_criteria, void *flow_context,
321 struct mlx5_flow_table *ft = flow_table;
325 err = mlx5_find_group(ft, match_criteria_enable, match_criteria,
328 mlx5_core_warn(ft->dev, "mlx5_find_group failed\n");
332 err = alloc_flow_index(ft, group_ix, flow_index);
334 mlx5_core_warn(ft->dev, "alloc_flow_index failed\n");
338 err = mlx5_set_flow_entry_cmd(ft, group_ix, *flow_index, flow_context);
340 mlx5_free_flow_index(ft, *flow_index);
344 EXPORT_SYMBOL(mlx5_add_flow_table_entry);
346 void mlx5_del_flow_table_entry(void *flow_table, u32 flow_index)
348 struct mlx5_flow_table *ft = flow_table;
350 mlx5_del_flow_entry_cmd(ft, flow_index);
351 mlx5_free_flow_index(ft, flow_index);
353 EXPORT_SYMBOL(mlx5_del_flow_table_entry);
355 void *mlx5_create_flow_table(struct mlx5_core_dev *dev, u8 level, u8 table_type,
358 struct mlx5_flow_table_group *group)
360 struct mlx5_flow_table *ft;
368 for (i = 0; i < num_groups; i++)
369 ft_size += (1 << group[i].log_sz);
371 ft = kzalloc(sizeof(*ft), GFP_KERNEL);
372 gr = kcalloc(num_groups, sizeof(struct mlx5_ftg), GFP_KERNEL);
373 bm = kcalloc(BITS_TO_LONGS(ft_size), sizeof(uintptr_t), GFP_KERNEL);
377 ft->num_groups = num_groups;
380 ft->type = table_type;
383 mutex_init(&ft->mutex);
385 for (i = 0; i < ft->num_groups; i++) {
386 memcpy(&ft->group[i].g, &group[i], sizeof(*group));
387 ft->group[i].start_ix = start_ix;
388 start_ix += 1 << group[i].log_sz;
391 err = mlx5_create_flow_table_cmd(ft);
395 err = mlx5_create_flow_table_groups(ft);
397 goto err_destroy_flow_table_cmd;
401 err_destroy_flow_table_cmd:
402 mlx5_destroy_flow_table_cmd(ft);
405 mlx5_core_warn(dev, "failed to alloc flow table\n");
412 EXPORT_SYMBOL(mlx5_create_flow_table);
414 void mlx5_destroy_flow_table(void *flow_table)
416 struct mlx5_flow_table *ft = flow_table;
418 mlx5_destroy_flow_table_groups(ft);
419 mlx5_destroy_flow_table_cmd(ft);
424 EXPORT_SYMBOL(mlx5_destroy_flow_table);
426 u32 mlx5_get_flow_table_id(void *flow_table)
428 struct mlx5_flow_table *ft = flow_table;
432 EXPORT_SYMBOL(mlx5_get_flow_table_id);