Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright (c) 2021 NVIDIA Corporation & Affiliates
3 : : */
4 : : #include <rte_malloc.h>
5 : : #include <mlx5_devx_cmds.h>
6 : : #include <mlx5_malloc.h>
7 : : #include "mlx5.h"
8 : : #include "mlx5_flow.h"
9 : :
10 : : static_assert(sizeof(uint32_t) * CHAR_BIT >= MLX5_PORT_FLEX_ITEM_NUM,
11 : : "Flex item maximal number exceeds uint32_t bit width");
12 : :
13 : : /**
14 : : * Routine called once on port initialization to init flex item
15 : : * related infrastructure initialization
16 : : *
17 : : * @param dev
18 : : * Ethernet device to perform flex item initialization
19 : : *
20 : : * @return
21 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
22 : : */
23 : : int
24 : 0 : mlx5_flex_item_port_init(struct rte_eth_dev *dev)
25 : : {
26 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
27 : :
28 : : rte_spinlock_init(&priv->flex_item_sl);
29 : : MLX5_ASSERT(!priv->flex_item_map);
30 : 0 : return 0;
31 : : }
32 : :
33 : : /**
34 : : * Routine called once on port close to perform flex item
35 : : * related infrastructure cleanup.
36 : : *
37 : : * @param dev
38 : : * Ethernet device to perform cleanup
39 : : */
40 : : void
41 : 0 : mlx5_flex_item_port_cleanup(struct rte_eth_dev *dev)
42 : : {
43 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
44 : : uint32_t i;
45 : :
46 [ # # # # ]: 0 : for (i = 0; i < MLX5_PORT_FLEX_ITEM_NUM && priv->flex_item_map ; i++) {
47 [ # # ]: 0 : if (priv->flex_item_map & (1 << i)) {
48 : : struct mlx5_flex_item *flex = &priv->flex_item[i];
49 : :
50 : 0 : claim_zero(mlx5_list_unregister
51 : : (priv->sh->flex_parsers_dv,
52 : : &flex->devx_fp->entry));
53 : 0 : flex->devx_fp = NULL;
54 : 0 : flex->refcnt = 0;
55 : 0 : priv->flex_item_map &= ~(1 << i);
56 : : }
57 : : }
58 : 0 : }
59 : :
60 : : static int
61 : : mlx5_flex_index(struct mlx5_priv *priv, struct mlx5_flex_item *item)
62 : : {
63 : 0 : uintptr_t start = (uintptr_t)&priv->flex_item[0];
64 : 0 : uintptr_t entry = (uintptr_t)item;
65 : 0 : uintptr_t idx = (entry - start) / sizeof(struct mlx5_flex_item);
66 : :
67 : 0 : if (entry < start ||
68 [ # # ]: 0 : idx >= MLX5_PORT_FLEX_ITEM_NUM ||
69 [ # # # # : 0 : (entry - start) % sizeof(struct mlx5_flex_item) ||
# # ]
70 [ # # # # : 0 : !(priv->flex_item_map & (1u << idx)))
# # ]
71 : : return -1;
72 : 0 : return (int)idx;
73 : : }
74 : :
75 : : static struct mlx5_flex_item *
76 : 0 : mlx5_flex_alloc(struct mlx5_priv *priv)
77 : : {
78 : : struct mlx5_flex_item *item = NULL;
79 : :
80 : 0 : rte_spinlock_lock(&priv->flex_item_sl);
81 [ # # ]: 0 : if (~priv->flex_item_map) {
82 [ # # ]: 0 : uint32_t idx = rte_bsf32(~priv->flex_item_map);
83 : :
84 [ # # ]: 0 : if (idx < MLX5_PORT_FLEX_ITEM_NUM) {
85 : 0 : item = &priv->flex_item[idx];
86 : : MLX5_ASSERT(!item->refcnt);
87 : : MLX5_ASSERT(!item->devx_fp);
88 : 0 : item->devx_fp = NULL;
89 : 0 : rte_atomic_store_explicit(&item->refcnt, 0, rte_memory_order_release);
90 : 0 : priv->flex_item_map |= 1u << idx;
91 : : }
92 : : }
93 : : rte_spinlock_unlock(&priv->flex_item_sl);
94 : 0 : return item;
95 : : }
96 : :
97 : : static void
98 [ # # ]: 0 : mlx5_flex_free(struct mlx5_priv *priv, struct mlx5_flex_item *item)
99 : : {
100 : : int idx = mlx5_flex_index(priv, item);
101 : :
102 : : MLX5_ASSERT(idx >= 0 &&
103 : : idx < MLX5_PORT_FLEX_ITEM_NUM &&
104 : : (priv->flex_item_map & (1u << idx)));
105 [ # # ]: 0 : if (idx >= 0) {
106 : 0 : rte_spinlock_lock(&priv->flex_item_sl);
107 : : MLX5_ASSERT(!item->refcnt);
108 : : MLX5_ASSERT(!item->devx_fp);
109 : 0 : item->devx_fp = NULL;
110 : 0 : rte_atomic_store_explicit(&item->refcnt, 0, rte_memory_order_release);
111 : 0 : priv->flex_item_map &= ~(1u << idx);
112 : : rte_spinlock_unlock(&priv->flex_item_sl);
113 : : }
114 : 0 : }
115 : :
116 : : static uint32_t
117 : 0 : mlx5_flex_get_bitfield(const struct rte_flow_item_flex *item,
118 : : uint32_t pos, uint32_t width, uint32_t shift)
119 : : {
120 : 0 : const uint8_t *ptr = item->pattern + pos / CHAR_BIT;
121 : 0 : uint32_t val, vbits, skip = pos % CHAR_BIT;
122 : :
123 : : /* Proceed the bitfield start byte. */
124 : : MLX5_ASSERT(width <= sizeof(uint32_t) * CHAR_BIT && width);
125 : : MLX5_ASSERT(width + shift <= sizeof(uint32_t) * CHAR_BIT);
126 [ # # ]: 0 : if (item->length <= pos / CHAR_BIT)
127 : : return 0;
128 : : /* Bits are enumerated in byte in network order: 01234567 */
129 : 0 : val = *ptr++;
130 : 0 : vbits = CHAR_BIT - pos % CHAR_BIT;
131 : 0 : pos = RTE_ALIGN_CEIL(pos, CHAR_BIT) / CHAR_BIT;
132 : 0 : vbits = RTE_MIN(vbits, width);
133 : : /* Load bytes to cover the field width, checking pattern boundary */
134 [ # # # # ]: 0 : while (vbits < width && pos < item->length) {
135 : 0 : uint32_t part = RTE_MIN(width - vbits, (uint32_t)CHAR_BIT);
136 : 0 : uint32_t tmp = *ptr++;
137 : :
138 : 0 : val |= tmp << RTE_ALIGN_CEIL(vbits, CHAR_BIT);
139 : 0 : vbits += part;
140 : 0 : pos++;
141 : : }
142 [ # # ]: 0 : val = rte_cpu_to_be_32(val);
143 : 0 : val <<= skip;
144 : 0 : val >>= shift;
145 : 0 : val &= (RTE_BIT64(width) - 1) << (sizeof(uint32_t) * CHAR_BIT - shift - width);
146 : 0 : return val;
147 : : }
148 : :
149 : : #define SET_FP_MATCH_SAMPLE_ID(x, def, msk, val, sid) \
150 : : do { \
151 : : uint32_t tmp, out = (def); \
152 : : tmp = MLX5_GET(fte_match_set_misc4, misc4_v, \
153 : : prog_sample_field_value_##x); \
154 : : tmp = (tmp & ~out) | (val); \
155 : : MLX5_SET(fte_match_set_misc4, misc4_v, \
156 : : prog_sample_field_value_##x, tmp); \
157 : : tmp = MLX5_GET(fte_match_set_misc4, misc4_m, \
158 : : prog_sample_field_value_##x); \
159 : : tmp = (tmp & ~out) | (msk); \
160 : : MLX5_SET(fte_match_set_misc4, misc4_m, \
161 : : prog_sample_field_value_##x, tmp); \
162 : : tmp = tmp ? (sid) : 0; \
163 : : MLX5_SET(fte_match_set_misc4, misc4_v, \
164 : : prog_sample_field_id_##x, tmp);\
165 : : MLX5_SET(fte_match_set_misc4, misc4_m, \
166 : : prog_sample_field_id_##x, tmp); \
167 : : } while (0)
168 : :
169 : : __rte_always_inline static void
170 : : mlx5_flex_set_match_sample(void *misc4_m, void *misc4_v,
171 : : uint32_t def, uint32_t mask, uint32_t value,
172 : : uint32_t sample_id, uint32_t id)
173 : : {
174 : 0 : switch (id) {
175 : 0 : case 0:
176 [ # # # # : 0 : SET_FP_MATCH_SAMPLE_ID(0, def, mask, value, sample_id);
# # # # #
# # # #
# ]
177 : 0 : break;
178 : 0 : case 1:
179 [ # # # # : 0 : SET_FP_MATCH_SAMPLE_ID(1, def, mask, value, sample_id);
# # # # #
# # # #
# ]
180 : 0 : break;
181 : 0 : case 2:
182 [ # # # # : 0 : SET_FP_MATCH_SAMPLE_ID(2, def, mask, value, sample_id);
# # # # #
# # # #
# ]
183 : 0 : break;
184 : 0 : case 3:
185 [ # # # # : 0 : SET_FP_MATCH_SAMPLE_ID(3, def, mask, value, sample_id);
# # # # #
# # # #
# ]
186 : 0 : break;
187 : 0 : case 4:
188 [ # # # # : 0 : SET_FP_MATCH_SAMPLE_ID(4, def, mask, value, sample_id);
# # # # #
# # # #
# ]
189 : 0 : break;
190 : 0 : case 5:
191 [ # # # # : 0 : SET_FP_MATCH_SAMPLE_ID(5, def, mask, value, sample_id);
# # # # #
# # # #
# ]
192 : 0 : break;
193 : 0 : case 6:
194 [ # # # # : 0 : SET_FP_MATCH_SAMPLE_ID(6, def, mask, value, sample_id);
# # # # #
# # # #
# ]
195 : 0 : break;
196 : 0 : case 7:
197 [ # # # # : 0 : SET_FP_MATCH_SAMPLE_ID(7, def, mask, value, sample_id);
# # # # #
# # # #
# ]
198 : 0 : break;
199 : : default:
200 : : MLX5_ASSERT(false);
201 : : break;
202 : : }
203 : : #undef SET_FP_MATCH_SAMPLE_ID
204 : : }
205 : :
206 : : /**
207 : : * Get the flex parser sample id and corresponding mask
208 : : * per shift and width information.
209 : : *
210 : : * @param[in] tp
211 : : * Mlx5 flex item sample mapping handle.
212 : : * @param[in] idx
213 : : * Mapping index.
214 : : * @param[in, out] pos
215 : : * Where to search the value and mask.
216 : : * @param[in] is_inner
217 : : * For inner matching or not.
218 : : *
219 : : * @return
220 : : * 0 on success, -1 to ignore.
221 : : */
222 : : int
223 : 0 : mlx5_flex_get_sample_id(const struct mlx5_flex_item *tp,
224 : : uint32_t idx, uint32_t *pos, bool is_inner)
225 : : {
226 : 0 : const struct mlx5_flex_pattern_field *map = tp->map + idx;
227 : 0 : uint32_t id = map->reg_id;
228 : :
229 : : /* Skip placeholders for DUMMY fields. */
230 [ # # ]: 0 : if (id == MLX5_INVALID_SAMPLE_REG_ID) {
231 : 0 : *pos += map->width;
232 : 0 : return -1;
233 : : }
234 : : MLX5_ASSERT(map->width);
235 : : MLX5_ASSERT(id < tp->devx_fp->num_samples);
236 [ # # # # ]: 0 : if (tp->tunnel_mode == FLEX_TUNNEL_MODE_MULTI && is_inner) {
237 : 0 : uint32_t num_samples = tp->devx_fp->num_samples / 2;
238 : :
239 : : MLX5_ASSERT(tp->devx_fp->num_samples % 2 == 0);
240 : : MLX5_ASSERT(id < num_samples);
241 : 0 : id += num_samples;
242 : : }
243 : 0 : return id;
244 : : }
245 : :
246 : : /**
247 : : * Get the flex parser mapping value per definer format_select_dw.
248 : : *
249 : : * @param[in] item
250 : : * Rte flex item pointer.
251 : : * @param[in] flex
252 : : * Mlx5 flex item sample mapping handle.
253 : : * @param[in] byte_off
254 : : * Mlx5 flex item format_select_dw.
255 : : * @param[in] tunnel
256 : : * Tunnel mode or not.
257 : : * @param[in, def] value
258 : : * Value calculated for this flex parser, either spec or mask.
259 : : *
260 : : * @return
261 : : * 0 on success, -1 for error.
262 : : */
263 : : int
264 : 0 : mlx5_flex_get_parser_value_per_byte_off(const struct rte_flow_item_flex *item,
265 : : void *flex, uint32_t byte_off,
266 : : bool tunnel, uint32_t *value)
267 : : {
268 : : struct mlx5_flex_pattern_field *map;
269 : : struct mlx5_flex_item *tp = flex;
270 : : uint32_t i, pos, val;
271 : : int id;
272 : :
273 : 0 : *value = 0;
274 [ # # # # ]: 0 : for (i = 0, pos = 0; i < tp->mapnum && pos < item->length * CHAR_BIT; i++) {
275 : 0 : map = tp->map + i;
276 : 0 : id = mlx5_flex_get_sample_id(tp, i, &pos, tunnel);
277 [ # # ]: 0 : if (id == -1)
278 : 0 : continue;
279 [ # # # # ]: 0 : if (id >= (int)tp->devx_fp->num_samples || id >= MLX5_GRAPH_NODE_SAMPLE_NUM)
280 : : return -1;
281 [ # # ]: 0 : if (byte_off == tp->devx_fp->sample_info[id].sample_dw_data * sizeof(uint32_t)) {
282 : 0 : val = mlx5_flex_get_bitfield(item, pos, map->width, map->shift);
283 : 0 : *value |= val;
284 : : }
285 : 0 : pos += map->width;
286 : : }
287 : : return 0;
288 : : }
289 : :
290 : : /**
291 : : * Get the flex parser tunnel mode.
292 : : *
293 : : * @param[in] item
294 : : * RTE Flex item.
295 : : * @param[in, out] tunnel_mode
296 : : * Pointer to return tunnel mode.
297 : : *
298 : : * @return
299 : : * 0 on success, otherwise negative error code.
300 : : */
301 : : int
302 : 0 : mlx5_flex_get_tunnel_mode(const struct rte_flow_item *item,
303 : : enum rte_flow_item_flex_tunnel_mode *tunnel_mode)
304 : : {
305 [ # # # # : 0 : if (item && item->spec && tunnel_mode) {
# # ]
306 : : const struct rte_flow_item_flex *spec = item->spec;
307 : 0 : struct mlx5_flex_item *flex = (struct mlx5_flex_item *)spec->handle;
308 : :
309 [ # # ]: 0 : if (flex) {
310 : 0 : *tunnel_mode = flex->tunnel_mode;
311 : 0 : return 0;
312 : : }
313 : : }
314 : : return -EINVAL;
315 : : }
316 : :
317 : : /**
318 : : * Translate item pattern into matcher fields according to translation
319 : : * array.
320 : : *
321 : : * @param dev
322 : : * Ethernet device to translate flex item on.
323 : : * @param[in, out] matcher
324 : : * Flow matcher to configure
325 : : * @param[in, out] key
326 : : * Flow matcher value.
327 : : * @param[in] item
328 : : * Flow pattern to translate.
329 : : * @param[in] is_inner
330 : : * Inner Flex Item (follows after tunnel header).
331 : : *
332 : : * @return
333 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
334 : : */
335 : : void
336 : 0 : mlx5_flex_flow_translate_item(struct rte_eth_dev *dev,
337 : : void *matcher, void *key,
338 : : const struct rte_flow_item *item,
339 : : bool is_inner)
340 : : {
341 : : const struct rte_flow_item_flex *spec, *mask;
342 : : void *misc4_m = MLX5_ADDR_OF(fte_match_param, matcher,
343 : : misc_parameters_4);
344 : : void *misc4_v = MLX5_ADDR_OF(fte_match_param, key, misc_parameters_4);
345 : : struct mlx5_flex_item *tp;
346 : 0 : uint32_t i, pos = 0;
347 : : uint32_t sample_id;
348 : :
349 : : RTE_SET_USED(dev);
350 : : MLX5_ASSERT(item->spec && item->mask);
351 : 0 : spec = item->spec;
352 : 0 : mask = item->mask;
353 : 0 : tp = (struct mlx5_flex_item *)spec->handle;
354 [ # # # # ]: 0 : for (i = 0; i < tp->mapnum && pos < (spec->length * CHAR_BIT); i++) {
355 : 0 : struct mlx5_flex_pattern_field *map = tp->map + i;
356 : : uint32_t val, msk, def;
357 : 0 : int id = mlx5_flex_get_sample_id(tp, i, &pos, is_inner);
358 : :
359 [ # # ]: 0 : if (id == -1)
360 : 0 : continue;
361 : : MLX5_ASSERT(id < (int)tp->devx_fp->num_samples);
362 [ # # # # ]: 0 : if (id >= (int)tp->devx_fp->num_samples ||
363 : : id >= MLX5_GRAPH_NODE_SAMPLE_NUM)
364 : 0 : return;
365 : 0 : def = (uint32_t)(RTE_BIT64(map->width) - 1);
366 : 0 : def <<= (sizeof(uint32_t) * CHAR_BIT - map->shift - map->width);
367 : 0 : val = mlx5_flex_get_bitfield(spec, pos, map->width, map->shift);
368 : 0 : msk = pos < (mask->length * CHAR_BIT) ?
369 [ # # ]: 0 : mlx5_flex_get_bitfield(mask, pos, map->width, map->shift) : def;
370 : 0 : sample_id = tp->devx_fp->sample_ids[id];
371 [ # # # # : 0 : mlx5_flex_set_match_sample(misc4_m, misc4_v,
# # # #
# ]
372 : : def, msk, val & msk,
373 : : sample_id, id);
374 : 0 : pos += map->width;
375 : : }
376 : : }
377 : :
378 : : /**
379 : : * Convert flex item handle (from the RTE flow) to flex item index on port.
380 : : * Optionally can increment flex item object reference count.
381 : : *
382 : : * @param dev
383 : : * Ethernet device to acquire flex item on.
384 : : * @param[in] handle
385 : : * Flow item handle from item spec.
386 : : * @param[in] acquire
387 : : * If set - increment reference counter.
388 : : *
389 : : * @return
390 : : * >=0 - index on success, a negative errno value otherwise
391 : : * and rte_errno is set.
392 : : */
393 : : int
394 : 0 : mlx5_flex_acquire_index(struct rte_eth_dev *dev,
395 : : struct rte_flow_item_flex_handle *handle,
396 : : bool acquire)
397 : : {
398 [ # # ]: 0 : struct mlx5_priv *priv = dev->data->dev_private;
399 : : struct mlx5_flex_item *flex = (struct mlx5_flex_item *)handle;
400 : : int ret = mlx5_flex_index(priv, flex);
401 : :
402 [ # # ]: 0 : if (ret < 0) {
403 : 0 : errno = -EINVAL;
404 : 0 : rte_errno = EINVAL;
405 : 0 : return ret;
406 : : }
407 [ # # ]: 0 : if (acquire)
408 : 0 : rte_atomic_fetch_add_explicit(&flex->refcnt, 1, rte_memory_order_release);
409 : : return ret;
410 : : }
411 : :
412 : : /**
413 : : * Release flex item index on port - decrements reference counter by index.
414 : : *
415 : : * @param dev
416 : : * Ethernet device to acquire flex item on.
417 : : * @param[in] index
418 : : * Flow item index.
419 : : *
420 : : * @return
421 : : * 0 - on success, a negative errno value otherwise and rte_errno is set.
422 : : */
423 : : int
424 : 0 : mlx5_flex_release_index(struct rte_eth_dev *dev,
425 : : int index)
426 : : {
427 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
428 : : struct mlx5_flex_item *flex;
429 : :
430 [ # # ]: 0 : if (index >= MLX5_PORT_FLEX_ITEM_NUM ||
431 [ # # ]: 0 : !(priv->flex_item_map & (1u << index))) {
432 : 0 : errno = EINVAL;
433 : 0 : rte_errno = -EINVAL;
434 : 0 : return -EINVAL;
435 : : }
436 : 0 : flex = priv->flex_item + index;
437 [ # # ]: 0 : if (flex->refcnt <= 1) {
438 : : MLX5_ASSERT(false);
439 : 0 : errno = EINVAL;
440 : 0 : rte_errno = -EINVAL;
441 : 0 : return -EINVAL;
442 : : }
443 : 0 : rte_atomic_fetch_sub_explicit(&flex->refcnt, 1, rte_memory_order_release);
444 : 0 : return 0;
445 : : }
446 : :
447 : : /*
448 : : * Calculate largest mask value for a given shift.
449 : : *
450 : : * shift mask
451 : : * ------- ---------------
452 : : * 0 b11111100 0x3C
453 : : * 1 b01111110 0x3E
454 : : * 2 b00111111 0x3F
455 : : * 3 b00011111 0x1F
456 : : * 4 b00001111 0x0F
457 : : * 5 b00000111 0x07
458 : : * 6 b00000011 0x03
459 : : * 7 b00000001 0x01
460 : : */
461 : : static uint8_t
462 : : mlx5_flex_hdr_len_mask(uint8_t shift,
463 : : const struct mlx5_hca_flex_attr *attr)
464 : : {
465 : : uint32_t base_mask;
466 [ # # ]: 0 : int diff = shift - MLX5_PARSE_GRAPH_NODE_HDR_LEN_SHIFT_DWORD;
467 : :
468 : : base_mask = mlx5_hca_parse_graph_node_base_hdr_len_mask(attr);
469 [ # # ]: 0 : return diff < 0 ? base_mask << -diff : base_mask >> diff;
470 : : }
471 : :
472 : : static int
473 : 0 : mlx5_flex_translate_length(struct mlx5_hca_flex_attr *attr,
474 : : const struct rte_flow_item_flex_conf *conf,
475 : : struct mlx5_flex_parser_devx *devx,
476 : : struct rte_flow_error *error)
477 : : {
478 : : const struct rte_flow_item_flex_field *field = &conf->next_header;
479 : : struct mlx5_devx_graph_node_attr *node = &devx->devx_conf;
480 : :
481 [ # # ]: 0 : if (field->field_base % CHAR_BIT)
482 : 0 : return rte_flow_error_set
483 : : (error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, NULL,
484 : : "not byte aligned header length field");
485 [ # # # # : 0 : switch (field->field_mode) {
# ]
486 : 0 : case FIELD_MODE_DUMMY:
487 : 0 : return rte_flow_error_set
488 : : (error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, NULL,
489 : : "invalid header length field mode (DUMMY)");
490 : 0 : case FIELD_MODE_FIXED:
491 [ # # ]: 0 : if (!(attr->header_length_mode &
492 : : RTE_BIT32(MLX5_GRAPH_NODE_LEN_FIXED)))
493 : 0 : return rte_flow_error_set
494 : : (error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, NULL,
495 : : "unsupported header length field mode (FIXED)");
496 [ # # ]: 0 : if (field->field_size ||
497 [ # # # # ]: 0 : field->offset_mask || field->offset_shift)
498 : 0 : return rte_flow_error_set
499 : : (error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, NULL,
500 : : "invalid fields for fixed mode");
501 [ # # ]: 0 : if (field->field_base < 0)
502 : 0 : return rte_flow_error_set
503 : : (error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, NULL,
504 : : "negative header length field base (FIXED)");
505 : 0 : node->header_length_mode = MLX5_GRAPH_NODE_LEN_FIXED;
506 : 0 : break;
507 : 0 : case FIELD_MODE_OFFSET: {
508 : : uint32_t msb, lsb;
509 : 0 : int32_t shift = field->offset_shift;
510 : 0 : uint32_t offset = field->offset_base;
511 : 0 : uint32_t mask = field->offset_mask;
512 : 0 : uint32_t wmax = attr->header_length_mask_width +
513 : : MLX5_PARSE_GRAPH_NODE_HDR_LEN_SHIFT_DWORD;
514 : :
515 [ # # ]: 0 : if (!(attr->header_length_mode &
516 : : RTE_BIT32(MLX5_GRAPH_NODE_LEN_FIELD)))
517 : 0 : return rte_flow_error_set
518 : : (error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, NULL,
519 : : "unsupported header length field mode (OFFSET)");
520 [ # # ]: 0 : if (!field->field_size)
521 : 0 : return rte_flow_error_set
522 : : (error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, NULL,
523 : : "field size is a must for offset mode");
524 [ # # ]: 0 : if ((offset ^ (field->field_size + offset)) >> 5)
525 : 0 : return rte_flow_error_set
526 : : (error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, NULL,
527 : : "field crosses the 32-bit word boundary");
528 : : /* Hardware counts in dwords, all shifts done by offset within mask */
529 [ # # ]: 0 : if (shift < 0 || (uint32_t)shift >= wmax)
530 : 0 : return rte_flow_error_set
531 : : (error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, NULL,
532 : : "header length field shift exceeds limits (OFFSET)");
533 [ # # ]: 0 : if (!mask)
534 : 0 : return rte_flow_error_set
535 : : (error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, NULL,
536 : : "zero length field offset mask (OFFSET)");
537 [ # # ]: 0 : msb = rte_fls_u32(mask) - 1;
538 : : lsb = rte_bsf32(mask);
539 [ # # ]: 0 : if (!rte_is_power_of_2((mask >> lsb) + 1))
540 : 0 : return rte_flow_error_set
541 : : (error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, NULL,
542 : : "length field offset mask not contiguous (OFFSET)");
543 [ # # ]: 0 : if (msb >= field->field_size)
544 : 0 : return rte_flow_error_set
545 : : (error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, NULL,
546 : : "length field offset mask exceeds field size (OFFSET)");
547 [ # # ]: 0 : if (msb >= wmax)
548 : 0 : return rte_flow_error_set
549 : : (error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, NULL,
550 : : "length field offset mask exceeds supported width (OFFSET)");
551 [ # # ]: 0 : if (mask & ~mlx5_flex_hdr_len_mask(shift, attr))
552 : 0 : return rte_flow_error_set
553 : : (error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, NULL,
554 : : "mask and shift combination not supported (OFFSET)");
555 : : msb++;
556 : 0 : offset += field->field_size - msb;
557 [ # # # # ]: 0 : if (attr->header_length_field_mode_wa && msb < attr->header_length_mask_width) {
558 [ # # ]: 0 : if (attr->header_length_mask_width - msb > offset)
559 : 0 : return rte_flow_error_set
560 : : (error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, NULL,
561 : : "field size plus offset_base is too small");
562 : 0 : offset += msb;
563 : : /*
564 : : * Here we can move to preceding dword. Hardware does
565 : : * cyclic left shift so we should avoid this and stay
566 : : * at current dword offset.
567 : : */
568 : 0 : offset = (offset & ~0x1Fu) |
569 : 0 : ((offset - attr->header_length_mask_width) & 0x1F);
570 : : }
571 : 0 : node->header_length_mode = MLX5_GRAPH_NODE_LEN_FIELD;
572 : 0 : node->header_length_field_mask = mask;
573 : 0 : node->header_length_field_shift = shift;
574 : 0 : node->header_length_field_offset = offset;
575 : 0 : node->header_length_field_offset_mode = !attr->header_length_field_mode_wa;
576 : 0 : break;
577 : : }
578 : 0 : case FIELD_MODE_BITMASK:
579 [ # # ]: 0 : if (!(attr->header_length_mode &
580 : : RTE_BIT32(MLX5_GRAPH_NODE_LEN_BITMASK)))
581 : 0 : return rte_flow_error_set
582 : : (error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, NULL,
583 : : "unsupported header length field mode (BITMASK)");
584 [ # # ]: 0 : if (field->offset_shift > 15 || field->offset_shift < 0)
585 : 0 : return rte_flow_error_set
586 : : (error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, NULL,
587 : : "header length field shift exceeds limit (BITMASK)");
588 : 0 : node->header_length_mode = MLX5_GRAPH_NODE_LEN_BITMASK;
589 : 0 : node->header_length_field_mask = field->offset_mask;
590 : 0 : node->header_length_field_shift = field->offset_shift;
591 : 0 : node->header_length_field_offset = field->offset_base;
592 : 0 : break;
593 : 0 : default:
594 : 0 : return rte_flow_error_set
595 : : (error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, NULL,
596 : : "unknown header length field mode");
597 : : }
598 [ # # ]: 0 : if (field->field_base / CHAR_BIT >= 0 &&
599 [ # # ]: 0 : field->field_base / CHAR_BIT > attr->max_base_header_length)
600 : 0 : return rte_flow_error_set
601 : : (error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, NULL,
602 : : "header length field base exceeds limit");
603 : 0 : node->header_length_base_value = field->field_base / CHAR_BIT;
604 : 0 : return 0;
605 : : }
606 : :
607 : : static int
608 : 0 : mlx5_flex_translate_next(struct mlx5_hca_flex_attr *attr,
609 : : const struct rte_flow_item_flex_conf *conf,
610 : : struct mlx5_flex_parser_devx *devx,
611 : : struct rte_flow_error *error)
612 : : {
613 : : const struct rte_flow_item_flex_field *field = &conf->next_protocol;
614 : : struct mlx5_devx_graph_node_attr *node = &devx->devx_conf;
615 : :
616 [ # # # # : 0 : switch (field->field_mode) {
# ]
617 : 0 : case FIELD_MODE_DUMMY:
618 [ # # ]: 0 : if (conf->nb_outputs)
619 : 0 : return rte_flow_error_set
620 : : (error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, NULL,
621 : : "next protocol field is required (DUMMY)");
622 : : return 0;
623 : : case FIELD_MODE_FIXED:
624 : : break;
625 : 0 : case FIELD_MODE_OFFSET:
626 : 0 : return rte_flow_error_set
627 : : (error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, NULL,
628 : : "unsupported next protocol field mode (OFFSET)");
629 : : break;
630 : 0 : case FIELD_MODE_BITMASK:
631 : 0 : return rte_flow_error_set
632 : : (error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, NULL,
633 : : "unsupported next protocol field mode (BITMASK)");
634 : 0 : default:
635 : 0 : return rte_flow_error_set
636 : : (error, EINVAL,
637 : : RTE_FLOW_ERROR_TYPE_ITEM, NULL,
638 : : "unknown next protocol field mode");
639 : : }
640 : : MLX5_ASSERT(field->field_mode == FIELD_MODE_FIXED);
641 [ # # ]: 0 : if (!conf->nb_outputs)
642 : 0 : return rte_flow_error_set
643 : : (error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, NULL,
644 : : "out link(s) is required if next field present");
645 [ # # ]: 0 : if (attr->max_next_header_offset < field->field_base)
646 : 0 : return rte_flow_error_set
647 : : (error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, NULL,
648 : : "next protocol field base exceeds limit");
649 [ # # ]: 0 : if (field->offset_shift)
650 : 0 : return rte_flow_error_set
651 : : (error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, NULL,
652 : : "unsupported next protocol field shift");
653 : 0 : node->next_header_field_offset = field->field_base;
654 : 0 : node->next_header_field_size = field->field_size;
655 : 0 : return 0;
656 : : }
657 : :
658 : : /* Helper structure to handle field bit intervals. */
659 : : struct mlx5_flex_field_cover {
660 : : uint16_t num;
661 : : int32_t start[MLX5_FLEX_ITEM_MAPPING_NUM];
662 : : int32_t end[MLX5_FLEX_ITEM_MAPPING_NUM];
663 : : uint8_t mapped[MLX5_FLEX_ITEM_MAPPING_NUM / CHAR_BIT + 1];
664 : : };
665 : :
666 : : static void
667 : 0 : mlx5_flex_insert_field(struct mlx5_flex_field_cover *cover,
668 : : uint16_t num, int32_t start, int32_t end)
669 : : {
670 : : MLX5_ASSERT(num < MLX5_FLEX_ITEM_MAPPING_NUM);
671 : : MLX5_ASSERT(num <= cover->num);
672 [ # # ]: 0 : if (num < cover->num) {
673 : 0 : memmove(&cover->start[num + 1], &cover->start[num],
674 : 0 : (cover->num - num) * sizeof(int32_t));
675 : 0 : memmove(&cover->end[num + 1], &cover->end[num],
676 : 0 : (cover->num - num) * sizeof(int32_t));
677 : : }
678 : 0 : cover->start[num] = start;
679 : 0 : cover->end[num] = end;
680 : 0 : cover->num++;
681 : 0 : }
682 : :
683 : : static void
684 : 0 : mlx5_flex_merge_field(struct mlx5_flex_field_cover *cover, uint16_t num)
685 : : {
686 : : uint32_t i, del = 0;
687 : : int32_t end;
688 : :
689 : : MLX5_ASSERT(num < MLX5_FLEX_ITEM_MAPPING_NUM);
690 : : MLX5_ASSERT(num < (cover->num - 1));
691 : 0 : end = cover->end[num];
692 [ # # ]: 0 : for (i = num + 1; i < cover->num; i++) {
693 [ # # ]: 0 : if (end < cover->start[i])
694 : : break;
695 : 0 : del++;
696 [ # # ]: 0 : if (end <= cover->end[i]) {
697 : 0 : cover->end[num] = cover->end[i];
698 : 0 : break;
699 : : }
700 : : }
701 [ # # ]: 0 : if (del) {
702 : : MLX5_ASSERT(del < (cover->num - 1u - num));
703 : 0 : cover->num -= del;
704 : : MLX5_ASSERT(cover->num > num);
705 [ # # ]: 0 : if ((cover->num - num) > 1) {
706 : 0 : memmove(&cover->start[num + 1],
707 : 0 : &cover->start[num + 1 + del],
708 : 0 : (cover->num - num - 1) * sizeof(int32_t));
709 : 0 : memmove(&cover->end[num + 1],
710 : 0 : &cover->end[num + 1 + del],
711 : 0 : (cover->num - num - 1) * sizeof(int32_t));
712 : : }
713 : : }
714 : 0 : }
715 : :
716 : : /*
717 : : * Validate the sample field and update interval array
718 : : * if parameters match with the 'match" field.
719 : : * Returns:
720 : : * < 0 - error
721 : : * == 0 - no match, interval array not updated
722 : : * > 0 - match, interval array updated
723 : : */
724 : : static int
725 : 0 : mlx5_flex_cover_sample(struct mlx5_flex_field_cover *cover,
726 : : struct rte_flow_item_flex_field *field,
727 : : struct rte_flow_item_flex_field *match,
728 : : struct mlx5_hca_flex_attr *attr,
729 : : struct rte_flow_error *error)
730 : : {
731 : : int32_t start, end;
732 : : uint32_t i;
733 : :
734 [ # # # # : 0 : switch (field->field_mode) {
# ]
735 : : case FIELD_MODE_DUMMY:
736 : : return 0;
737 : 0 : case FIELD_MODE_FIXED:
738 [ # # ]: 0 : if (!(attr->sample_offset_mode &
739 : : RTE_BIT32(MLX5_GRAPH_SAMPLE_OFFSET_FIXED)))
740 : 0 : return rte_flow_error_set
741 : : (error, EINVAL,
742 : : RTE_FLOW_ERROR_TYPE_ITEM, NULL,
743 : : "unsupported sample field mode (FIXED)");
744 [ # # ]: 0 : if (field->offset_shift)
745 : 0 : return rte_flow_error_set
746 : : (error, EINVAL,
747 : : RTE_FLOW_ERROR_TYPE_ITEM, NULL,
748 : : "invalid sample field shift (FIXED");
749 [ # # ]: 0 : if (field->field_base < 0)
750 : 0 : return rte_flow_error_set
751 : : (error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, NULL,
752 : : "invalid sample field base (FIXED)");
753 [ # # ]: 0 : if (field->field_base / CHAR_BIT > attr->max_sample_base_offset)
754 : 0 : return rte_flow_error_set
755 : : (error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, NULL,
756 : : "sample field base exceeds limit (FIXED)");
757 : : break;
758 : 0 : case FIELD_MODE_OFFSET:
759 [ # # ]: 0 : if (!(attr->sample_offset_mode &
760 : : RTE_BIT32(MLX5_GRAPH_SAMPLE_OFFSET_FIELD)))
761 : 0 : return rte_flow_error_set
762 : : (error, EINVAL,
763 : : RTE_FLOW_ERROR_TYPE_ITEM, NULL,
764 : : "unsupported sample field mode (OFFSET)");
765 [ # # ]: 0 : if (field->field_base / CHAR_BIT >= 0 &&
766 [ # # ]: 0 : field->field_base / CHAR_BIT > attr->max_sample_base_offset)
767 : 0 : return rte_flow_error_set
768 : : (error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, NULL,
769 : : "sample field base exceeds limit");
770 : : break;
771 : 0 : case FIELD_MODE_BITMASK:
772 [ # # ]: 0 : if (!(attr->sample_offset_mode &
773 : : RTE_BIT32(MLX5_GRAPH_SAMPLE_OFFSET_BITMASK)))
774 : 0 : return rte_flow_error_set
775 : : (error, EINVAL,
776 : : RTE_FLOW_ERROR_TYPE_ITEM, NULL,
777 : : "unsupported sample field mode (BITMASK)");
778 [ # # ]: 0 : if (field->field_base / CHAR_BIT >= 0 &&
779 [ # # ]: 0 : field->field_base / CHAR_BIT > attr->max_sample_base_offset)
780 : 0 : return rte_flow_error_set
781 : : (error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, NULL,
782 : : "sample field base exceeds limit");
783 : : break;
784 : 0 : default:
785 : 0 : return rte_flow_error_set
786 : : (error, EINVAL,
787 : : RTE_FLOW_ERROR_TYPE_ITEM, NULL,
788 : : "unknown data sample field mode");
789 : : }
790 [ # # ]: 0 : if (!match) {
791 [ # # ]: 0 : if (!field->field_size)
792 : 0 : return rte_flow_error_set
793 : : (error, EINVAL,
794 : : RTE_FLOW_ERROR_TYPE_ITEM, NULL,
795 : : "zero sample field width");
796 [ # # ]: 0 : if (field->field_id)
797 : 0 : DRV_LOG(DEBUG, "sample field id hint ignored");
798 : : } else {
799 [ # # ]: 0 : if (field->field_mode != match->field_mode ||
800 [ # # ]: 0 : field->offset_base | match->offset_base ||
801 [ # # ]: 0 : field->offset_mask | match->offset_mask ||
802 [ # # ]: 0 : field->offset_shift | match->offset_shift)
803 : : return 0;
804 : : }
805 : 0 : start = field->field_base;
806 : 0 : end = start + field->field_size;
807 : : /* Add the new or similar field to interval array. */
808 [ # # ]: 0 : if (!cover->num) {
809 : 0 : cover->start[cover->num] = start;
810 : 0 : cover->end[cover->num] = end;
811 : 0 : cover->num = 1;
812 : 0 : return 1;
813 : : }
814 [ # # ]: 0 : for (i = 0; i < cover->num; i++) {
815 [ # # ]: 0 : if (start > cover->end[i]) {
816 [ # # ]: 0 : if (i >= (cover->num - 1u)) {
817 : 0 : mlx5_flex_insert_field(cover, cover->num,
818 : : start, end);
819 : 0 : break;
820 : : }
821 : : continue;
822 : : }
823 [ # # ]: 0 : if (end < cover->start[i]) {
824 : 0 : mlx5_flex_insert_field(cover, i, start, end);
825 : 0 : break;
826 : : }
827 [ # # ]: 0 : if (start < cover->start[i])
828 : 0 : cover->start[i] = start;
829 [ # # ]: 0 : if (end > cover->end[i]) {
830 : 0 : cover->end[i] = end;
831 [ # # ]: 0 : if (i < (cover->num - 1u))
832 : 0 : mlx5_flex_merge_field(cover, i);
833 : : }
834 : : break;
835 : : }
836 : : return 1;
837 : : }
838 : :
839 : : static void
840 [ # # # ]: 0 : mlx5_flex_config_sample(struct mlx5_devx_match_sample_attr *na,
841 : : struct rte_flow_item_flex_field *field,
842 : : enum rte_flow_item_flex_tunnel_mode tunnel_mode)
843 : : {
844 : : memset(na, 0, sizeof(struct mlx5_devx_match_sample_attr));
845 : 0 : na->flow_match_sample_en = 1;
846 [ # # # ]: 0 : switch (field->field_mode) {
847 : : case FIELD_MODE_FIXED:
848 : : na->flow_match_sample_offset_mode =
849 : : MLX5_GRAPH_SAMPLE_OFFSET_FIXED;
850 : : break;
851 : 0 : case FIELD_MODE_OFFSET:
852 : 0 : na->flow_match_sample_offset_mode =
853 : : MLX5_GRAPH_SAMPLE_OFFSET_FIELD;
854 : 0 : na->flow_match_sample_field_offset = field->offset_base;
855 : 0 : na->flow_match_sample_field_offset_mask = field->offset_mask;
856 : 0 : na->flow_match_sample_field_offset_shift = field->offset_shift;
857 : 0 : break;
858 : 0 : case FIELD_MODE_BITMASK:
859 : 0 : na->flow_match_sample_offset_mode =
860 : : MLX5_GRAPH_SAMPLE_OFFSET_BITMASK;
861 : 0 : na->flow_match_sample_field_offset = field->offset_base;
862 : 0 : na->flow_match_sample_field_offset_mask = field->offset_mask;
863 : 0 : na->flow_match_sample_field_offset_shift = field->offset_shift;
864 : 0 : break;
865 : : default:
866 : : MLX5_ASSERT(false);
867 : : break;
868 : : }
869 [ # # # ]: 0 : switch (tunnel_mode) {
870 : 0 : case FLEX_TUNNEL_MODE_SINGLE:
871 : : /* Fallthrough */
872 : : case FLEX_TUNNEL_MODE_TUNNEL:
873 : 0 : na->flow_match_sample_tunnel_mode =
874 : : MLX5_GRAPH_SAMPLE_TUNNEL_FIRST;
875 : 0 : break;
876 : : case FLEX_TUNNEL_MODE_MULTI:
877 : : /* Fallthrough */
878 : : case FLEX_TUNNEL_MODE_OUTER:
879 : : na->flow_match_sample_tunnel_mode =
880 : : MLX5_GRAPH_SAMPLE_TUNNEL_OUTER;
881 : : break;
882 : 0 : case FLEX_TUNNEL_MODE_INNER:
883 : 0 : na->flow_match_sample_tunnel_mode =
884 : : MLX5_GRAPH_SAMPLE_TUNNEL_INNER;
885 : 0 : break;
886 : : default:
887 : : MLX5_ASSERT(false);
888 : : break;
889 : : }
890 : 0 : }
891 : :
892 : : /* Map specified field to set/subset of allocated sample registers. */
893 : : static int
894 : 0 : mlx5_flex_map_sample(struct rte_flow_item_flex_field *field,
895 : : struct mlx5_flex_parser_devx *parser,
896 : : struct mlx5_flex_item *item,
897 : : struct rte_flow_error *error)
898 : : {
899 : : struct mlx5_devx_match_sample_attr node;
900 : 0 : int32_t start = field->field_base;
901 : 0 : int32_t end = start + field->field_size;
902 : : struct mlx5_flex_pattern_field *trans;
903 : : uint32_t i, done_bits = 0;
904 : :
905 [ # # ]: 0 : if (field->field_mode == FIELD_MODE_DUMMY) {
906 : : done_bits = field->field_size;
907 [ # # ]: 0 : while (done_bits) {
908 : 0 : uint32_t part = RTE_MIN(done_bits,
909 : : sizeof(uint32_t) * CHAR_BIT);
910 [ # # ]: 0 : if (item->mapnum >= MLX5_FLEX_ITEM_MAPPING_NUM)
911 : 0 : return rte_flow_error_set
912 : : (error,
913 : : EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, NULL,
914 : : "too many flex item pattern translations");
915 : : trans = &item->map[item->mapnum];
916 : 0 : trans->reg_id = MLX5_INVALID_SAMPLE_REG_ID;
917 : 0 : trans->shift = 0;
918 : 0 : trans->width = part;
919 : 0 : item->mapnum++;
920 : 0 : done_bits -= part;
921 : : }
922 : : return 0;
923 : : }
924 : 0 : mlx5_flex_config_sample(&node, field, item->tunnel_mode);
925 [ # # ]: 0 : for (i = 0; i < parser->num_samples; i++) {
926 : 0 : struct mlx5_devx_match_sample_attr *sample =
927 : : &parser->devx_conf.sample[i];
928 : : int32_t reg_start, reg_end;
929 : : int32_t cov_start, cov_end;
930 : :
931 : : MLX5_ASSERT(sample->flow_match_sample_en);
932 [ # # ]: 0 : if (!sample->flow_match_sample_en)
933 : : break;
934 : 0 : node.flow_match_sample_field_base_offset =
935 : 0 : sample->flow_match_sample_field_base_offset;
936 [ # # ]: 0 : if (memcmp(&node, sample, sizeof(node)))
937 : 0 : continue;
938 : 0 : reg_start = (int8_t)sample->flow_match_sample_field_base_offset;
939 : 0 : reg_start *= CHAR_BIT;
940 : 0 : reg_end = reg_start + 32;
941 [ # # ]: 0 : if (end <= reg_start || start >= reg_end)
942 : 0 : continue;
943 : 0 : cov_start = RTE_MAX(reg_start, start);
944 : 0 : cov_end = RTE_MIN(reg_end, end);
945 : : MLX5_ASSERT(cov_end > cov_start);
946 : 0 : done_bits += cov_end - cov_start;
947 [ # # ]: 0 : if (item->mapnum >= MLX5_FLEX_ITEM_MAPPING_NUM)
948 : 0 : return rte_flow_error_set
949 : : (error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, NULL,
950 : : "too many flex item pattern translations");
951 : : trans = &item->map[item->mapnum];
952 : 0 : item->mapnum++;
953 : 0 : trans->reg_id = i;
954 : 0 : trans->shift = cov_start - reg_start;
955 : 0 : trans->width = cov_end - cov_start;
956 : : }
957 [ # # ]: 0 : if (done_bits != field->field_size) {
958 : : MLX5_ASSERT(false);
959 : 0 : return rte_flow_error_set
960 : : (error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, NULL,
961 : : "failed to map field to sample register");
962 : : }
963 : : return 0;
964 : : }
965 : :
966 : : /* Allocate sample registers for the specified field type and interval array. */
967 : : static int
968 : 0 : mlx5_flex_alloc_sample(struct mlx5_flex_field_cover *cover,
969 : : struct mlx5_flex_parser_devx *parser,
970 : : struct mlx5_flex_item *item,
971 : : struct rte_flow_item_flex_field *field,
972 : : struct mlx5_hca_flex_attr *attr,
973 : : struct rte_flow_error *error)
974 : : {
975 : : struct mlx5_devx_match_sample_attr node;
976 : : uint32_t idx = 0;
977 : :
978 : 0 : mlx5_flex_config_sample(&node, field, item->tunnel_mode);
979 [ # # ]: 0 : while (idx < cover->num) {
980 : : int32_t start, end;
981 : :
982 : : /*
983 : : * Sample base offsets are in bytes, should be aligned
984 : : * to 32-bit as required by firmware for samples.
985 : : */
986 : 0 : start = RTE_ALIGN_FLOOR(cover->start[idx],
987 : : sizeof(uint32_t) * CHAR_BIT);
988 : 0 : node.flow_match_sample_field_base_offset =
989 : 0 : (start / CHAR_BIT) & 0xFF;
990 : : /* Allocate sample register. */
991 [ # # ]: 0 : if (parser->num_samples >= MLX5_GRAPH_NODE_SAMPLE_NUM ||
992 [ # # ]: 0 : parser->num_samples >= attr->max_num_sample ||
993 [ # # ]: 0 : parser->num_samples >= attr->max_num_prog_sample)
994 : 0 : return rte_flow_error_set
995 : : (error, EINVAL,
996 : : RTE_FLOW_ERROR_TYPE_ITEM, NULL,
997 : : "no sample registers to handle all flex item fields");
998 : 0 : parser->devx_conf.sample[parser->num_samples] = node;
999 : 0 : parser->num_samples++;
1000 : : /* Remove or update covered intervals. */
1001 : 0 : end = start + 32;
1002 [ # # ]: 0 : while (idx < cover->num) {
1003 [ # # ]: 0 : if (end >= cover->end[idx]) {
1004 : 0 : idx++;
1005 : 0 : continue;
1006 : : }
1007 [ # # ]: 0 : if (end > cover->start[idx])
1008 : 0 : cover->start[idx] = end;
1009 : : break;
1010 : : }
1011 : : }
1012 : : return 0;
1013 : : }
1014 : :
1015 : : static int
1016 : 0 : mlx5_flex_translate_sample(struct mlx5_hca_flex_attr *attr,
1017 : : const struct rte_flow_item_flex_conf *conf,
1018 : : struct mlx5_flex_parser_devx *parser,
1019 : : struct mlx5_flex_item *item,
1020 : : struct rte_flow_error *error)
1021 : : {
1022 : : struct mlx5_flex_field_cover cover;
1023 : : uint32_t i, j;
1024 : : int ret;
1025 : :
1026 [ # # ]: 0 : switch (conf->tunnel) {
1027 : : case FLEX_TUNNEL_MODE_SINGLE:
1028 : : /* Fallthrough */
1029 : : case FLEX_TUNNEL_MODE_OUTER:
1030 : : /* Fallthrough */
1031 : : case FLEX_TUNNEL_MODE_INNER:
1032 : : /* Fallthrough */
1033 : : case FLEX_TUNNEL_MODE_MULTI:
1034 : : /* Fallthrough */
1035 : : case FLEX_TUNNEL_MODE_TUNNEL:
1036 : : break;
1037 : 0 : default:
1038 : 0 : return rte_flow_error_set
1039 : : (error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, NULL,
1040 : : "unrecognized tunnel mode");
1041 : : }
1042 : 0 : item->tunnel_mode = conf->tunnel;
1043 [ # # ]: 0 : if (conf->nb_samples > MLX5_FLEX_ITEM_MAPPING_NUM)
1044 : 0 : return rte_flow_error_set
1045 : : (error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, NULL,
1046 : : "sample field number exceeds limit");
1047 : : /*
1048 : : * The application can specify fields smaller or bigger than 32 bits
1049 : : * covered with single sample register and it can specify field
1050 : : * offsets in any order.
1051 : : *
1052 : : * Gather all similar fields together, build array of bit intervals
1053 : : * in ascending order and try to cover with the smallest set of sample
1054 : : * registers.
1055 : : */
1056 : : memset(&cover, 0, sizeof(cover));
1057 [ # # ]: 0 : for (i = 0; i < conf->nb_samples; i++) {
1058 : 0 : struct rte_flow_item_flex_field *fl = conf->sample_data + i;
1059 : :
1060 : : /* Check whether field was covered in the previous iteration. */
1061 [ # # ]: 0 : if (cover.mapped[i / CHAR_BIT] & (1u << (i % CHAR_BIT)))
1062 : 0 : continue;
1063 [ # # ]: 0 : if (fl->field_mode == FIELD_MODE_DUMMY)
1064 : 0 : continue;
1065 : : /* Build an interval array for the field and similar ones */
1066 : 0 : cover.num = 0;
1067 : : /* Add the first field to array unconditionally. */
1068 : 0 : ret = mlx5_flex_cover_sample(&cover, fl, NULL, attr, error);
1069 [ # # ]: 0 : if (ret < 0)
1070 : 0 : return ret;
1071 : : MLX5_ASSERT(ret > 0);
1072 : 0 : cover.mapped[i / CHAR_BIT] |= 1u << (i % CHAR_BIT);
1073 [ # # ]: 0 : for (j = i + 1; j < conf->nb_samples; j++) {
1074 : : struct rte_flow_item_flex_field *ft;
1075 : :
1076 : : /* Add field to array if its type matches. */
1077 : 0 : ft = conf->sample_data + j;
1078 : 0 : ret = mlx5_flex_cover_sample(&cover, ft, fl,
1079 : : attr, error);
1080 [ # # ]: 0 : if (ret < 0)
1081 : 0 : return ret;
1082 [ # # ]: 0 : if (!ret)
1083 : 0 : continue;
1084 : 0 : cover.mapped[j / CHAR_BIT] |= 1u << (j % CHAR_BIT);
1085 : : }
1086 : : /* Allocate sample registers to cover array of intervals. */
1087 : 0 : ret = mlx5_flex_alloc_sample(&cover, parser, item,
1088 : : fl, attr, error);
1089 [ # # ]: 0 : if (ret)
1090 : 0 : return ret;
1091 : : }
1092 : : /* Build the item pattern translating data on flow creation. */
1093 : 0 : item->mapnum = 0;
1094 : 0 : memset(&item->map, 0, sizeof(item->map));
1095 [ # # ]: 0 : for (i = 0; i < conf->nb_samples; i++) {
1096 : 0 : struct rte_flow_item_flex_field *fl = conf->sample_data + i;
1097 : :
1098 : 0 : ret = mlx5_flex_map_sample(fl, parser, item, error);
1099 [ # # ]: 0 : if (ret) {
1100 : : MLX5_ASSERT(false);
1101 : 0 : return ret;
1102 : : }
1103 : : }
1104 [ # # ]: 0 : if (conf->tunnel == FLEX_TUNNEL_MODE_MULTI) {
1105 : : /*
1106 : : * In FLEX_TUNNEL_MODE_MULTI tunnel mode PMD creates 2 sets
1107 : : * of samples. The first set is for outer and the second set
1108 : : * for inner flex flow item. Outer and inner samples differ
1109 : : * only in tunnel_mode.
1110 : : */
1111 [ # # ]: 0 : if (parser->num_samples > MLX5_GRAPH_NODE_SAMPLE_NUM / 2)
1112 : 0 : return rte_flow_error_set
1113 : : (error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, NULL,
1114 : : "no sample registers for inner");
1115 : 0 : rte_memcpy(parser->devx_conf.sample + parser->num_samples,
1116 : 0 : parser->devx_conf.sample,
1117 [ # # ]: 0 : parser->num_samples *
1118 : : sizeof(parser->devx_conf.sample[0]));
1119 [ # # ]: 0 : for (i = 0; i < parser->num_samples; i++) {
1120 : 0 : struct mlx5_devx_match_sample_attr *sm = i +
1121 : 0 : parser->devx_conf.sample + parser->num_samples;
1122 : :
1123 : 0 : sm->flow_match_sample_tunnel_mode =
1124 : : MLX5_GRAPH_SAMPLE_TUNNEL_INNER;
1125 : : }
1126 : 0 : parser->num_samples *= 2;
1127 : : }
1128 : : return 0;
1129 : : }
1130 : :
1131 : : static int
1132 : 0 : mlx5_flex_arc_type(enum rte_flow_item_type type, int in)
1133 : : {
1134 [ # # # # : 0 : switch (type) {
# # # # #
# # ]
1135 : : case RTE_FLOW_ITEM_TYPE_ETH:
1136 : : return MLX5_GRAPH_ARC_NODE_MAC;
1137 : 0 : case RTE_FLOW_ITEM_TYPE_IPV4:
1138 [ # # ]: 0 : return in ? MLX5_GRAPH_ARC_NODE_IP : MLX5_GRAPH_ARC_NODE_IPV4;
1139 : 0 : case RTE_FLOW_ITEM_TYPE_IPV6:
1140 [ # # ]: 0 : return in ? MLX5_GRAPH_ARC_NODE_IP : MLX5_GRAPH_ARC_NODE_IPV6;
1141 : 0 : case RTE_FLOW_ITEM_TYPE_UDP:
1142 : 0 : return MLX5_GRAPH_ARC_NODE_UDP;
1143 : 0 : case RTE_FLOW_ITEM_TYPE_TCP:
1144 : 0 : return MLX5_GRAPH_ARC_NODE_TCP;
1145 : 0 : case RTE_FLOW_ITEM_TYPE_MPLS:
1146 : 0 : return MLX5_GRAPH_ARC_NODE_MPLS;
1147 : 0 : case RTE_FLOW_ITEM_TYPE_GRE:
1148 : 0 : return MLX5_GRAPH_ARC_NODE_GRE;
1149 : 0 : case RTE_FLOW_ITEM_TYPE_GENEVE:
1150 : 0 : return MLX5_GRAPH_ARC_NODE_GENEVE;
1151 : 0 : case RTE_FLOW_ITEM_TYPE_VXLAN_GPE:
1152 : 0 : return MLX5_GRAPH_ARC_NODE_VXLAN_GPE;
1153 : 0 : case RTE_FLOW_ITEM_TYPE_ESP:
1154 : 0 : return MLX5_GRAPH_ARC_NODE_IPSEC_ESP;
1155 : 0 : default:
1156 : 0 : return -EINVAL;
1157 : : }
1158 : : }
1159 : :
1160 : : static int
1161 : 0 : mlx5_flex_arc_in_eth(const struct rte_flow_item *item,
1162 : : struct rte_flow_error *error)
1163 : : {
1164 : 0 : const struct rte_flow_item_eth *spec = item->spec;
1165 : 0 : const struct rte_flow_item_eth *mask = item->mask;
1166 : 0 : struct rte_flow_item_eth eth = { .hdr.ether_type = RTE_BE16(0xFFFF) };
1167 : :
1168 [ # # ]: 0 : if (memcmp(mask, ð, sizeof(struct rte_flow_item_eth))) {
1169 : 0 : return rte_flow_error_set
1170 : : (error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, item,
1171 : : "invalid eth item mask");
1172 : : }
1173 [ # # ]: 0 : return rte_be_to_cpu_16(spec->hdr.ether_type);
1174 : : }
1175 : :
1176 : : static int
1177 : 0 : mlx5_flex_arc_in_udp(const struct rte_flow_item *item,
1178 : : struct rte_flow_error *error)
1179 : : {
1180 : 0 : const struct rte_flow_item_udp *spec = item->spec;
1181 : 0 : const struct rte_flow_item_udp *mask = item->mask;
1182 : 0 : struct rte_flow_item_udp udp = { .hdr.dst_port = RTE_BE16(0xFFFF) };
1183 : :
1184 [ # # ]: 0 : if (memcmp(mask, &udp, sizeof(struct rte_flow_item_udp))) {
1185 : 0 : return rte_flow_error_set
1186 : : (error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, item,
1187 : : "invalid eth item mask");
1188 : : }
1189 [ # # ]: 0 : return rte_be_to_cpu_16(spec->hdr.dst_port);
1190 : : }
1191 : :
1192 : : static int
1193 : 0 : mlx5_flex_arc_in_ipv4(const struct rte_flow_item *item,
1194 : : struct rte_flow_error *error)
1195 : : {
1196 : 0 : const struct rte_flow_item_ipv4 *spec = item->spec;
1197 : 0 : const struct rte_flow_item_ipv4 *mask = item->mask;
1198 : 0 : struct rte_flow_item_ipv4 ip = { .hdr.next_proto_id = 0xff };
1199 : :
1200 [ # # ]: 0 : if (memcmp(mask, &ip, sizeof(struct rte_flow_item_ipv4))) {
1201 : 0 : return rte_flow_error_set
1202 : : (error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, item,
1203 : : "invalid ipv4 item mask, full mask is desired");
1204 : : }
1205 : 0 : return spec->hdr.next_proto_id;
1206 : : }
1207 : :
1208 : : static int
1209 : 0 : mlx5_flex_arc_in_ipv6(const struct rte_flow_item *item,
1210 : : struct rte_flow_error *error)
1211 : : {
1212 : 0 : const struct rte_flow_item_ipv6 *spec = item->spec;
1213 : 0 : const struct rte_flow_item_ipv6 *mask = item->mask;
1214 : 0 : struct rte_flow_item_ipv6 ip = { .hdr.proto = 0xff };
1215 : :
1216 [ # # ]: 0 : if (memcmp(mask, &ip, sizeof(struct rte_flow_item_ipv6))) {
1217 : 0 : return rte_flow_error_set
1218 : : (error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, item,
1219 : : "invalid ipv6 item mask, full mask is desired");
1220 : : }
1221 : 0 : return spec->hdr.proto;
1222 : : }
1223 : :
1224 : : static int
1225 : 0 : mlx5_flex_translate_arc_in(struct mlx5_hca_flex_attr *attr,
1226 : : const struct rte_flow_item_flex_conf *conf,
1227 : : struct mlx5_flex_parser_devx *devx,
1228 : : struct mlx5_flex_item *item,
1229 : : struct rte_flow_error *error)
1230 : : {
1231 : : struct mlx5_devx_graph_node_attr *node = &devx->devx_conf;
1232 : : uint32_t i;
1233 : :
1234 : : RTE_SET_USED(item);
1235 [ # # ]: 0 : if (conf->nb_inputs > attr->max_num_arc_in)
1236 : 0 : return rte_flow_error_set
1237 : : (error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, NULL,
1238 : : "too many input links");
1239 [ # # ]: 0 : for (i = 0; i < conf->nb_inputs; i++) {
1240 : 0 : struct mlx5_devx_graph_arc_attr *arc = node->in + i;
1241 : 0 : struct rte_flow_item_flex_link *link = conf->input_link + i;
1242 : 0 : const struct rte_flow_item *rte_item = &link->item;
1243 : : int arc_type;
1244 : : int ret;
1245 : :
1246 [ # # # # : 0 : if (!rte_item->spec || !rte_item->mask || rte_item->last)
# # ]
1247 : 0 : return rte_flow_error_set
1248 : : (error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, NULL,
1249 : : "invalid flex item IN arc format");
1250 : 0 : arc_type = mlx5_flex_arc_type(rte_item->type, true);
1251 [ # # # # ]: 0 : if (arc_type < 0 || !(attr->node_in & RTE_BIT32(arc_type)))
1252 : 0 : return rte_flow_error_set
1253 : : (error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, NULL,
1254 : : "unsupported flex item IN arc type");
1255 : 0 : arc->arc_parse_graph_node = arc_type;
1256 : 0 : arc->start_inner_tunnel = 0;
1257 : : /*
1258 : : * Configure arc IN condition value. The value location depends
1259 : : * on protocol. Current FW version supports IP & UDP for IN
1260 : : * arcs only, and locations for these protocols are defined.
1261 : : * Add more protocols when available.
1262 : : */
1263 [ # # # # : 0 : switch (rte_item->type) {
# ]
1264 : 0 : case RTE_FLOW_ITEM_TYPE_ETH:
1265 : 0 : ret = mlx5_flex_arc_in_eth(rte_item, error);
1266 : 0 : break;
1267 : 0 : case RTE_FLOW_ITEM_TYPE_UDP:
1268 : 0 : ret = mlx5_flex_arc_in_udp(rte_item, error);
1269 : 0 : break;
1270 : 0 : case RTE_FLOW_ITEM_TYPE_IPV4:
1271 : 0 : ret = mlx5_flex_arc_in_ipv4(rte_item, error);
1272 : 0 : break;
1273 : 0 : case RTE_FLOW_ITEM_TYPE_IPV6:
1274 : 0 : ret = mlx5_flex_arc_in_ipv6(rte_item, error);
1275 : 0 : break;
1276 : 0 : default:
1277 : : MLX5_ASSERT(false);
1278 : 0 : return rte_flow_error_set
1279 : : (error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, NULL,
1280 : : "unsupported flex item IN arc type");
1281 : : }
1282 [ # # ]: 0 : if (ret < 0)
1283 : 0 : return ret;
1284 : 0 : arc->compare_condition_value = (uint16_t)ret;
1285 : : }
1286 : : return 0;
1287 : : }
1288 : :
1289 : : static int
1290 : 0 : mlx5_flex_translate_arc_out(struct mlx5_hca_flex_attr *attr,
1291 : : const struct rte_flow_item_flex_conf *conf,
1292 : : struct mlx5_flex_parser_devx *devx,
1293 : : struct mlx5_flex_item *item,
1294 : : struct rte_flow_error *error)
1295 : : {
1296 : : struct mlx5_devx_graph_node_attr *node = &devx->devx_conf;
1297 : 0 : bool is_tunnel = conf->tunnel == FLEX_TUNNEL_MODE_TUNNEL;
1298 : : uint32_t i;
1299 : :
1300 : : RTE_SET_USED(item);
1301 [ # # ]: 0 : if (conf->nb_outputs > attr->max_num_arc_out)
1302 : 0 : return rte_flow_error_set
1303 : : (error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, NULL,
1304 : : "too many output links");
1305 [ # # ]: 0 : for (i = 0; i < conf->nb_outputs; i++) {
1306 : 0 : struct mlx5_devx_graph_arc_attr *arc = node->out + i;
1307 : 0 : struct rte_flow_item_flex_link *link = conf->output_link + i;
1308 : : const struct rte_flow_item *rte_item = &link->item;
1309 : : int arc_type;
1310 : :
1311 [ # # # # : 0 : if (rte_item->spec || rte_item->mask || rte_item->last)
# # ]
1312 : 0 : return rte_flow_error_set
1313 : : (error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, NULL,
1314 : : "flex node: invalid OUT arc format");
1315 : 0 : arc_type = mlx5_flex_arc_type(rte_item->type, false);
1316 [ # # # # ]: 0 : if (arc_type < 0 || !(attr->node_out & RTE_BIT32(arc_type)))
1317 : 0 : return rte_flow_error_set
1318 : : (error, EINVAL, RTE_FLOW_ERROR_TYPE_ITEM, NULL,
1319 : : "unsupported flex item OUT arc type");
1320 : 0 : arc->arc_parse_graph_node = arc_type;
1321 : 0 : arc->start_inner_tunnel = !!is_tunnel;
1322 : 0 : arc->compare_condition_value = link->next;
1323 : : }
1324 : : return 0;
1325 : : }
1326 : :
1327 : : /* Translate RTE flex item API configuration into flaex parser settings. */
1328 : : static int
1329 : 0 : mlx5_flex_translate_conf(struct rte_eth_dev *dev,
1330 : : const struct rte_flow_item_flex_conf *conf,
1331 : : struct mlx5_flex_parser_devx *devx,
1332 : : struct mlx5_flex_item *item,
1333 : : struct rte_flow_error *error)
1334 : : {
1335 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
1336 : 0 : struct mlx5_hca_flex_attr *attr = &priv->sh->cdev->config.hca_attr.flex;
1337 : : int ret;
1338 : :
1339 : 0 : ret = mlx5_flex_translate_length(attr, conf, devx, error);
1340 [ # # ]: 0 : if (ret)
1341 : : return ret;
1342 : 0 : ret = mlx5_flex_translate_next(attr, conf, devx, error);
1343 [ # # ]: 0 : if (ret)
1344 : : return ret;
1345 : 0 : ret = mlx5_flex_translate_sample(attr, conf, devx, item, error);
1346 [ # # ]: 0 : if (ret)
1347 : : return ret;
1348 : 0 : ret = mlx5_flex_translate_arc_in(attr, conf, devx, item, error);
1349 [ # # ]: 0 : if (ret)
1350 : : return ret;
1351 : 0 : ret = mlx5_flex_translate_arc_out(attr, conf, devx, item, error);
1352 [ # # ]: 0 : if (ret)
1353 : 0 : return ret;
1354 : : return 0;
1355 : : }
1356 : :
1357 : : /**
1358 : : * Create the flex item with specified configuration over the Ethernet device.
1359 : : *
1360 : : * @param dev
1361 : : * Ethernet device to create flex item on.
1362 : : * @param[in] conf
1363 : : * Flex item configuration.
1364 : : * @param[out] error
1365 : : * Perform verbose error reporting if not NULL. PMDs initialize this
1366 : : * structure in case of error only.
1367 : : *
1368 : : * @return
1369 : : * Non-NULL opaque pointer on success, NULL otherwise and rte_errno is set.
1370 : : */
1371 : : struct rte_flow_item_flex_handle *
1372 : 0 : flow_dv_item_create(struct rte_eth_dev *dev,
1373 : : const struct rte_flow_item_flex_conf *conf,
1374 : : struct rte_flow_error *error)
1375 : : {
1376 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
1377 : 0 : struct mlx5_flex_parser_devx devx_config = { .devx_obj = NULL };
1378 : : struct mlx5_flex_item *flex;
1379 : : struct mlx5_list_entry *ent;
1380 : :
1381 : : MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
1382 : 0 : flex = mlx5_flex_alloc(priv);
1383 [ # # ]: 0 : if (!flex) {
1384 : 0 : rte_flow_error_set(error, ENOMEM,
1385 : : RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1386 : : "too many flex items created on the port");
1387 : 0 : return NULL;
1388 : : }
1389 [ # # ]: 0 : if (mlx5_flex_translate_conf(dev, conf, &devx_config, flex, error))
1390 : 0 : goto error;
1391 : 0 : ent = mlx5_list_register(priv->sh->flex_parsers_dv, &devx_config);
1392 [ # # ]: 0 : if (!ent) {
1393 : 0 : rte_flow_error_set(error, ENOMEM,
1394 : : RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1395 : : "flex item creation failure");
1396 : 0 : goto error;
1397 : : }
1398 : 0 : flex->devx_fp = container_of(ent, struct mlx5_flex_parser_devx, entry);
1399 : : /* Mark initialized flex item valid. */
1400 : 0 : rte_atomic_fetch_add_explicit(&flex->refcnt, 1, rte_memory_order_release);
1401 : 0 : return (struct rte_flow_item_flex_handle *)flex;
1402 : :
1403 : 0 : error:
1404 : 0 : mlx5_flex_free(priv, flex);
1405 : 0 : return NULL;
1406 : : }
1407 : :
1408 : : /**
1409 : : * Release the flex item on the specified Ethernet device.
1410 : : *
1411 : : * @param dev
1412 : : * Ethernet device to destroy flex item on.
1413 : : * @param[in] handle
1414 : : * Handle of the item existing on the specified device.
1415 : : * @param[out] error
1416 : : * Perform verbose error reporting if not NULL. PMDs initialize this
1417 : : * structure in case of error only.
1418 : : *
1419 : : * @return
1420 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
1421 : : */
1422 : : int
1423 : 0 : flow_dv_item_release(struct rte_eth_dev *dev,
1424 : : const struct rte_flow_item_flex_handle *handle,
1425 : : struct rte_flow_error *error)
1426 : : {
1427 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
1428 : : struct mlx5_flex_item *flex =
1429 : : (struct mlx5_flex_item *)(uintptr_t)handle;
1430 : : uint32_t old_refcnt = 1;
1431 : : int rc;
1432 : :
1433 : : MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
1434 : 0 : rte_spinlock_lock(&priv->flex_item_sl);
1435 [ # # ]: 0 : if (mlx5_flex_index(priv, flex) < 0) {
1436 : : rte_spinlock_unlock(&priv->flex_item_sl);
1437 : 0 : return rte_flow_error_set(error, EINVAL,
1438 : : RTE_FLOW_ERROR_TYPE_ITEM, NULL,
1439 : : "invalid flex item handle value");
1440 : : }
1441 [ # # ]: 0 : if (!rte_atomic_compare_exchange_strong_explicit(&flex->refcnt, &old_refcnt, 0,
1442 : : rte_memory_order_acquire, rte_memory_order_relaxed)) {
1443 : : rte_spinlock_unlock(&priv->flex_item_sl);
1444 : 0 : return rte_flow_error_set(error, EBUSY,
1445 : : RTE_FLOW_ERROR_TYPE_ITEM, NULL,
1446 : : "flex item has flow references");
1447 : : }
1448 : : /* Flex item is marked as invalid, we can leave locked section. */
1449 : : rte_spinlock_unlock(&priv->flex_item_sl);
1450 : : MLX5_ASSERT(flex->devx_fp);
1451 : 0 : rc = mlx5_list_unregister(priv->sh->flex_parsers_dv,
1452 : 0 : &flex->devx_fp->entry);
1453 : 0 : flex->devx_fp = NULL;
1454 : 0 : mlx5_flex_free(priv, flex);
1455 [ # # ]: 0 : if (rc < 0)
1456 : 0 : return rte_flow_error_set(error, EBUSY,
1457 : : RTE_FLOW_ERROR_TYPE_ITEM, NULL,
1458 : : "flex item release failure");
1459 : : return 0;
1460 : : }
1461 : :
1462 : : /* DevX flex parser list callbacks. */
1463 : : struct mlx5_list_entry *
1464 : 0 : mlx5_flex_parser_create_cb(void *list_ctx, void *ctx)
1465 : : {
1466 : : struct mlx5_dev_ctx_shared *sh = list_ctx;
1467 : : struct mlx5_flex_parser_devx *fp, *conf = ctx;
1468 : : uint32_t i;
1469 : 0 : uint8_t sample_info = sh->cdev->config.hca_attr.flex.query_match_sample_info;
1470 : : int ret;
1471 : :
1472 : 0 : fp = mlx5_malloc(MLX5_MEM_ZERO, sizeof(struct mlx5_flex_parser_devx),
1473 : : 0, SOCKET_ID_ANY);
1474 [ # # ]: 0 : if (!fp)
1475 : : return NULL;
1476 : : /* Copy the requested configurations. */
1477 : 0 : fp->num_samples = conf->num_samples;
1478 : 0 : memcpy(&fp->devx_conf, &conf->devx_conf, sizeof(fp->devx_conf));
1479 : : /* Create DevX flex parser. */
1480 : 0 : fp->devx_obj = mlx5_devx_cmd_create_flex_parser(sh->cdev->ctx,
1481 : : &fp->devx_conf);
1482 [ # # ]: 0 : if (!fp->devx_obj)
1483 : 0 : goto error;
1484 : : /* Query the firmware assigned sample ids. */
1485 : 0 : ret = mlx5_devx_cmd_query_parse_samples(fp->devx_obj,
1486 : 0 : fp->sample_ids,
1487 : : fp->num_samples,
1488 : : &fp->anchor_id);
1489 [ # # ]: 0 : if (ret)
1490 : 0 : goto error;
1491 : : /* Query sample information per ID. */
1492 [ # # # # ]: 0 : for (i = 0; i < fp->num_samples && sample_info; i++) {
1493 : 0 : ret = mlx5_devx_cmd_match_sample_info_query(sh->cdev->ctx, fp->sample_ids[i],
1494 : : &fp->sample_info[i]);
1495 [ # # ]: 0 : if (ret)
1496 : 0 : goto error;
1497 : : }
1498 : 0 : DRV_LOG(DEBUG, "DEVx flex parser %p created, samples num: %u",
1499 : : (const void *)fp, fp->num_samples);
1500 : 0 : return &fp->entry;
1501 : 0 : error:
1502 [ # # ]: 0 : if (fp->devx_obj)
1503 : 0 : mlx5_devx_cmd_destroy((void *)(uintptr_t)fp->devx_obj);
1504 : : if (fp)
1505 : 0 : mlx5_free(fp);
1506 : 0 : return NULL;
1507 : : }
1508 : :
1509 : : int
1510 : 0 : mlx5_flex_parser_match_cb(void *list_ctx,
1511 : : struct mlx5_list_entry *iter, void *ctx)
1512 : : {
1513 : : struct mlx5_flex_parser_devx *fp =
1514 : : container_of(iter, struct mlx5_flex_parser_devx, entry);
1515 : : struct mlx5_flex_parser_devx *org =
1516 : : container_of(ctx, struct mlx5_flex_parser_devx, entry);
1517 : :
1518 : : RTE_SET_USED(list_ctx);
1519 [ # # ]: 0 : return !iter || !ctx || memcmp(&fp->devx_conf,
1520 [ # # ]: 0 : &org->devx_conf,
1521 : : sizeof(fp->devx_conf));
1522 : : }
1523 : :
1524 : : void
1525 : 0 : mlx5_flex_parser_remove_cb(void *list_ctx, struct mlx5_list_entry *entry)
1526 : : {
1527 : : struct mlx5_flex_parser_devx *fp =
1528 : : container_of(entry, struct mlx5_flex_parser_devx, entry);
1529 : :
1530 : : RTE_SET_USED(list_ctx);
1531 : : MLX5_ASSERT(fp->devx_obj);
1532 : 0 : claim_zero(mlx5_devx_cmd_destroy(fp->devx_obj));
1533 : 0 : DRV_LOG(DEBUG, "DEVx flex parser %p destroyed", (const void *)fp);
1534 : 0 : mlx5_free(entry);
1535 : 0 : }
1536 : :
1537 : : struct mlx5_list_entry *
1538 : 0 : mlx5_flex_parser_clone_cb(void *list_ctx,
1539 : : struct mlx5_list_entry *entry, void *ctx)
1540 : : {
1541 : : struct mlx5_flex_parser_devx *fp;
1542 : :
1543 : : RTE_SET_USED(list_ctx);
1544 : : RTE_SET_USED(entry);
1545 : 0 : fp = mlx5_malloc(0, sizeof(struct mlx5_flex_parser_devx),
1546 : : 0, SOCKET_ID_ANY);
1547 [ # # ]: 0 : if (!fp)
1548 : : return NULL;
1549 : : memcpy(fp, ctx, sizeof(struct mlx5_flex_parser_devx));
1550 : 0 : return &fp->entry;
1551 : : }
1552 : :
1553 : : void
1554 : 0 : mlx5_flex_parser_clone_free_cb(void *list_ctx, struct mlx5_list_entry *entry)
1555 : : {
1556 : : struct mlx5_flex_parser_devx *fp =
1557 : : container_of(entry, struct mlx5_flex_parser_devx, entry);
1558 : : RTE_SET_USED(list_ctx);
1559 : 0 : mlx5_free(fp);
1560 : 0 : }
1561 : :
1562 : : /*
1563 : : * Destroy the flex parser node, including the parser itself, input / output
1564 : : * arcs and DW samples. Resources could be reused then.
1565 : : *
1566 : : * @param dev
1567 : : * Pointer to Ethernet device structure.
1568 : : */
1569 : : void
1570 : 0 : mlx5_flex_parser_ecpri_release(struct rte_eth_dev *dev)
1571 : : {
1572 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
1573 : 0 : struct mlx5_ecpri_parser_profile *prf = &priv->sh->ecpri_parser;
1574 : :
1575 [ # # ]: 0 : if (prf->obj)
1576 : 0 : mlx5_devx_cmd_destroy(prf->obj);
1577 : 0 : prf->obj = NULL;
1578 : 0 : }
|