xref: /dpdk/drivers/regex/mlx5/mlx5_rxp.c (revision 97de3671de4d067ce33ee5eeee9a87b450fb522c)
1c126512bSOri Kam /* SPDX-License-Identifier: BSD-3-Clause
2c126512bSOri Kam  * Copyright 2020 Mellanox Technologies, Ltd
3c126512bSOri Kam  */
4c126512bSOri Kam 
5c126512bSOri Kam #include <rte_log.h>
6c126512bSOri Kam #include <rte_errno.h>
7e3dbbf71SOri Kam #include <rte_malloc.h>
8c126512bSOri Kam #include <rte_regexdev.h>
9c126512bSOri Kam #include <rte_regexdev_core.h>
10c126512bSOri Kam #include <rte_regexdev_driver.h>
11c126512bSOri Kam 
12e3dbbf71SOri Kam #include <mlx5_glue.h>
13e3dbbf71SOri Kam #include <mlx5_devx_cmds.h>
14e3dbbf71SOri Kam #include <mlx5_prm.h>
15b34d8163SFrancis Kelly #include <mlx5_common_os.h>
16e3dbbf71SOri Kam 
17c126512bSOri Kam #include "mlx5_regex.h"
18e3dbbf71SOri Kam #include "mlx5_regex_utils.h"
19e3dbbf71SOri Kam #include "mlx5_rxp_csrs.h"
20b34d8163SFrancis Kelly #include "mlx5_rxp.h"
21c126512bSOri Kam 
22b34d8163SFrancis Kelly #define MLX5_REGEX_MAX_MATCHES MLX5_RXP_MAX_MATCHES
23b34d8163SFrancis Kelly #define MLX5_REGEX_MAX_PAYLOAD_SIZE MLX5_RXP_MAX_JOB_LENGTH
24b34d8163SFrancis Kelly #define MLX5_REGEX_MAX_RULES_PER_GROUP UINT32_MAX
25b34d8163SFrancis Kelly #define MLX5_REGEX_MAX_GROUPS MLX5_RXP_MAX_SUBSETS
26b34d8163SFrancis Kelly 
27b34d8163SFrancis Kelly /* Private Declarations */
28b34d8163SFrancis Kelly static int
29b34d8163SFrancis Kelly rxp_poll_csr_for_value(struct ibv_context *ctx, uint32_t *value,
30b34d8163SFrancis Kelly 		       uint32_t address, uint32_t expected_value,
31b34d8163SFrancis Kelly 		       uint32_t expected_mask, uint32_t timeout_ms, uint8_t id);
32b34d8163SFrancis Kelly static int
33b34d8163SFrancis Kelly mlnx_set_database(struct mlx5_regex_priv *priv, uint8_t id, uint8_t db_to_use);
34b34d8163SFrancis Kelly static int
35b34d8163SFrancis Kelly mlnx_resume_database(struct mlx5_regex_priv *priv, uint8_t id);
36b34d8163SFrancis Kelly static int
37b34d8163SFrancis Kelly mlnx_update_database(struct mlx5_regex_priv *priv, uint8_t id);
38b34d8163SFrancis Kelly static int
39b34d8163SFrancis Kelly program_rxp_rules(struct mlx5_regex_priv *priv,
40b34d8163SFrancis Kelly 		  struct mlx5_rxp_ctl_rules_pgm *rules, uint8_t id);
41b34d8163SFrancis Kelly static int
42b34d8163SFrancis Kelly rxp_init_eng(struct mlx5_regex_priv *priv, uint8_t id);
43b34d8163SFrancis Kelly static int
44b34d8163SFrancis Kelly write_private_rules(struct mlx5_regex_priv *priv,
45b34d8163SFrancis Kelly 		    struct mlx5_rxp_ctl_rules_pgm *rules,
46b34d8163SFrancis Kelly 		    uint8_t id);
47b34d8163SFrancis Kelly static int
48b34d8163SFrancis Kelly write_shared_rules(struct mlx5_regex_priv *priv,
49b34d8163SFrancis Kelly 		   struct mlx5_rxp_ctl_rules_pgm *rules, uint32_t count,
50b34d8163SFrancis Kelly 		   uint8_t db_to_program);
51b34d8163SFrancis Kelly static int
52b34d8163SFrancis Kelly rxp_db_setup(struct mlx5_regex_priv *priv);
53b34d8163SFrancis Kelly static void
54b34d8163SFrancis Kelly rxp_dump_csrs(struct ibv_context *ctx, uint8_t id);
55b34d8163SFrancis Kelly static int
56b34d8163SFrancis Kelly rxp_write_rules_via_cp(struct ibv_context *ctx,
57b34d8163SFrancis Kelly 		       struct mlx5_rxp_rof_entry *rules,
58b34d8163SFrancis Kelly 		       int count, uint8_t id);
59b34d8163SFrancis Kelly static int
60b34d8163SFrancis Kelly rxp_flush_rules(struct ibv_context *ctx, struct mlx5_rxp_rof_entry *rules,
61b34d8163SFrancis Kelly 		int count, uint8_t id);
62b34d8163SFrancis Kelly static int
63b34d8163SFrancis Kelly rxp_start_engine(struct ibv_context *ctx, uint8_t id);
64b34d8163SFrancis Kelly static int
65b34d8163SFrancis Kelly rxp_stop_engine(struct ibv_context *ctx, uint8_t id);
66b34d8163SFrancis Kelly 
67b34d8163SFrancis Kelly static void __rte_unused
68b34d8163SFrancis Kelly rxp_dump_csrs(struct ibv_context *ctx __rte_unused, uint8_t id __rte_unused)
69b34d8163SFrancis Kelly {
70b34d8163SFrancis Kelly 	uint32_t reg, i;
71b34d8163SFrancis Kelly 
72b34d8163SFrancis Kelly 	/* Main CSRs*/
73b34d8163SFrancis Kelly 	for (i = 0; i < MLX5_RXP_CSR_NUM_ENTRIES; i++) {
74b34d8163SFrancis Kelly 		if (mlx5_devx_regex_register_read(ctx, id,
75b34d8163SFrancis Kelly 						  (MLX5_RXP_CSR_WIDTH * i) +
76b34d8163SFrancis Kelly 						  MLX5_RXP_CSR_BASE_ADDRESS,
77b34d8163SFrancis Kelly 						  &reg)) {
78b34d8163SFrancis Kelly 			DRV_LOG(ERR, "Failed to read Main CSRs Engine %d!", id);
79b34d8163SFrancis Kelly 			return;
80b34d8163SFrancis Kelly 		}
81b34d8163SFrancis Kelly 		DRV_LOG(DEBUG, "RXP Main CSRs (Eng%d) register (%d): %08x",
82b34d8163SFrancis Kelly 			id, i, reg);
83b34d8163SFrancis Kelly 	}
84b34d8163SFrancis Kelly 	/* RTRU CSRs*/
85b34d8163SFrancis Kelly 	for (i = 0; i < MLX5_RXP_CSR_NUM_ENTRIES; i++) {
86b34d8163SFrancis Kelly 		if (mlx5_devx_regex_register_read(ctx, id,
87b34d8163SFrancis Kelly 						  (MLX5_RXP_CSR_WIDTH * i) +
88b34d8163SFrancis Kelly 						 MLX5_RXP_RTRU_CSR_BASE_ADDRESS,
89b34d8163SFrancis Kelly 						  &reg)) {
90b34d8163SFrancis Kelly 			DRV_LOG(ERR, "Failed to read RTRU CSRs Engine %d!", id);
91b34d8163SFrancis Kelly 			return;
92b34d8163SFrancis Kelly 		}
93b34d8163SFrancis Kelly 		DRV_LOG(DEBUG, "RXP RTRU CSRs (Eng%d) register (%d): %08x",
94b34d8163SFrancis Kelly 			id, i, reg);
95b34d8163SFrancis Kelly 	}
96b34d8163SFrancis Kelly 	/* STAT CSRs */
97b34d8163SFrancis Kelly 	for (i = 0; i < MLX5_RXP_CSR_NUM_ENTRIES; i++) {
98b34d8163SFrancis Kelly 		if (mlx5_devx_regex_register_read(ctx, id,
99b34d8163SFrancis Kelly 						  (MLX5_RXP_CSR_WIDTH * i) +
100b34d8163SFrancis Kelly 						MLX5_RXP_STATS_CSR_BASE_ADDRESS,
101b34d8163SFrancis Kelly 						  &reg)) {
102b34d8163SFrancis Kelly 			DRV_LOG(ERR, "Failed to read STAT CSRs Engine %d!", id);
103b34d8163SFrancis Kelly 			return;
104b34d8163SFrancis Kelly 		}
105b34d8163SFrancis Kelly 		DRV_LOG(DEBUG, "RXP STAT CSRs (Eng%d) register (%d): %08x",
106b34d8163SFrancis Kelly 			id, i, reg);
107b34d8163SFrancis Kelly 	}
108b34d8163SFrancis Kelly }
109c126512bSOri Kam 
110c126512bSOri Kam int
111c126512bSOri Kam mlx5_regex_info_get(struct rte_regexdev *dev __rte_unused,
112c126512bSOri Kam 		    struct rte_regexdev_info *info)
113c126512bSOri Kam {
114c126512bSOri Kam 	info->max_matches = MLX5_REGEX_MAX_MATCHES;
115c126512bSOri Kam 	info->max_payload_size = MLX5_REGEX_MAX_PAYLOAD_SIZE;
116c126512bSOri Kam 	info->max_rules_per_group = MLX5_REGEX_MAX_RULES_PER_GROUP;
117c126512bSOri Kam 	info->max_groups = MLX5_REGEX_MAX_GROUPS;
118b34d8163SFrancis Kelly 	info->max_queue_pairs = 1;
119c8452005SOphir Munk 	info->regexdev_capa = RTE_REGEXDEV_SUPP_PCRE_GREEDY_F |
120c8452005SOphir Munk 			      RTE_REGEXDEV_CAPA_QUEUE_PAIR_OOS_F;
121c126512bSOri Kam 	info->rule_flags = 0;
122fbc8c700SOri Kam 	info->max_queue_pairs = 10;
123c126512bSOri Kam 	return 0;
124c126512bSOri Kam }
125e3dbbf71SOri Kam 
126b34d8163SFrancis Kelly /**
127b34d8163SFrancis Kelly  * Actual writing of RXP instructions to RXP via CSRs.
128b34d8163SFrancis Kelly  */
129b34d8163SFrancis Kelly static int
130b34d8163SFrancis Kelly rxp_write_rules_via_cp(struct ibv_context *ctx,
131b34d8163SFrancis Kelly 		       struct mlx5_rxp_rof_entry *rules,
132b34d8163SFrancis Kelly 		       int count, uint8_t id)
133b34d8163SFrancis Kelly {
134b34d8163SFrancis Kelly 	int i, ret = 0;
135b34d8163SFrancis Kelly 	uint32_t tmp;
136b34d8163SFrancis Kelly 
137b34d8163SFrancis Kelly 	for (i = 0; i < count; i++) {
138b34d8163SFrancis Kelly 		tmp = (uint32_t)rules[i].value;
139b34d8163SFrancis Kelly 		ret |= mlx5_devx_regex_register_write(ctx, id,
140b34d8163SFrancis Kelly 						      MLX5_RXP_RTRU_CSR_DATA_0,
141b34d8163SFrancis Kelly 						      tmp);
142b34d8163SFrancis Kelly 		tmp = (uint32_t)(rules[i].value >> 32);
143b34d8163SFrancis Kelly 		ret |= mlx5_devx_regex_register_write(ctx, id,
144b34d8163SFrancis Kelly 						      MLX5_RXP_RTRU_CSR_DATA_0 +
145b34d8163SFrancis Kelly 						      MLX5_RXP_CSR_WIDTH, tmp);
146b34d8163SFrancis Kelly 		tmp = rules[i].addr;
147b34d8163SFrancis Kelly 		ret |= mlx5_devx_regex_register_write(ctx, id,
148b34d8163SFrancis Kelly 						      MLX5_RXP_RTRU_CSR_ADDR,
149b34d8163SFrancis Kelly 						      tmp);
150b34d8163SFrancis Kelly 		if (ret) {
151b34d8163SFrancis Kelly 			DRV_LOG(ERR, "Failed to copy instructions to RXP.");
152b34d8163SFrancis Kelly 			return -1;
153b34d8163SFrancis Kelly 		}
154b34d8163SFrancis Kelly 	}
155b34d8163SFrancis Kelly 	DRV_LOG(DEBUG, "Written %d instructions", count);
156b34d8163SFrancis Kelly 	return 0;
157b34d8163SFrancis Kelly }
158b34d8163SFrancis Kelly 
159b34d8163SFrancis Kelly static int
160b34d8163SFrancis Kelly rxp_flush_rules(struct ibv_context *ctx, struct mlx5_rxp_rof_entry *rules,
161b34d8163SFrancis Kelly 		int count, uint8_t id)
162b34d8163SFrancis Kelly {
163b34d8163SFrancis Kelly 	uint32_t val, fifo_depth;
164b34d8163SFrancis Kelly 	int ret;
165b34d8163SFrancis Kelly 
166b34d8163SFrancis Kelly 	ret = rxp_write_rules_via_cp(ctx, rules, count, id);
167b34d8163SFrancis Kelly 	if (ret < 0) {
168b34d8163SFrancis Kelly 		DRV_LOG(ERR, "Failed to write rules via CSRs.");
169b34d8163SFrancis Kelly 		return -1;
170b34d8163SFrancis Kelly 	}
171b34d8163SFrancis Kelly 	ret = mlx5_devx_regex_register_read(ctx, id,
172b34d8163SFrancis Kelly 					    MLX5_RXP_RTRU_CSR_CAPABILITY,
173b34d8163SFrancis Kelly 					    &fifo_depth);
174b34d8163SFrancis Kelly 	if (ret) {
175b34d8163SFrancis Kelly 		DRV_LOG(ERR, "CSR read failed!");
176b34d8163SFrancis Kelly 		return -1;
177b34d8163SFrancis Kelly 	}
178b34d8163SFrancis Kelly 	ret = rxp_poll_csr_for_value(ctx, &val, MLX5_RXP_RTRU_CSR_FIFO_STAT,
179b34d8163SFrancis Kelly 				     count, ~0,
180b34d8163SFrancis Kelly 				     MLX5_RXP_POLL_CSR_FOR_VALUE_TIMEOUT, id);
181b34d8163SFrancis Kelly 	if (ret < 0) {
182b34d8163SFrancis Kelly 		DRV_LOG(ERR, "Rules not rx by RXP: credit: %d, depth: %d", val,
183b34d8163SFrancis Kelly 			fifo_depth);
184b34d8163SFrancis Kelly 		return ret;
185b34d8163SFrancis Kelly 	}
186b34d8163SFrancis Kelly 	DRV_LOG(DEBUG, "RTRU FIFO depth: 0x%x", fifo_depth);
187b34d8163SFrancis Kelly 	DRV_LOG(DEBUG, "Rules flush took %d cycles.", ret);
188b34d8163SFrancis Kelly 	ret = mlx5_devx_regex_register_read(ctx, id, MLX5_RXP_RTRU_CSR_CTRL,
189b34d8163SFrancis Kelly 					    &val);
190b34d8163SFrancis Kelly 	if (ret) {
191b34d8163SFrancis Kelly 		DRV_LOG(ERR, "CSR read failed!");
192b34d8163SFrancis Kelly 		return -1;
193b34d8163SFrancis Kelly 	}
194b34d8163SFrancis Kelly 	val |= MLX5_RXP_RTRU_CSR_CTRL_GO;
195b34d8163SFrancis Kelly 	ret = mlx5_devx_regex_register_write(ctx, id, MLX5_RXP_RTRU_CSR_CTRL,
196b34d8163SFrancis Kelly 					     val);
197b34d8163SFrancis Kelly 	ret = rxp_poll_csr_for_value(ctx, &val, MLX5_RXP_RTRU_CSR_STATUS,
198b34d8163SFrancis Kelly 				     MLX5_RXP_RTRU_CSR_STATUS_UPDATE_DONE,
199b34d8163SFrancis Kelly 				     MLX5_RXP_RTRU_CSR_STATUS_UPDATE_DONE,
200b34d8163SFrancis Kelly 				     MLX5_RXP_POLL_CSR_FOR_VALUE_TIMEOUT, id);
201b34d8163SFrancis Kelly 	if (ret < 0) {
202b34d8163SFrancis Kelly 		DRV_LOG(ERR, "Rules update timeout: 0x%08X", val);
203b34d8163SFrancis Kelly 		return ret;
204b34d8163SFrancis Kelly 	}
205b34d8163SFrancis Kelly 	DRV_LOG(DEBUG, "Rules update took %d cycles", ret);
206b34d8163SFrancis Kelly 	if (mlx5_devx_regex_register_read(ctx, id, MLX5_RXP_RTRU_CSR_CTRL,
207b34d8163SFrancis Kelly 					  &val)) {
208b34d8163SFrancis Kelly 		DRV_LOG(ERR, "CSR read failed!");
209b34d8163SFrancis Kelly 		return -1;
210b34d8163SFrancis Kelly 	}
211b34d8163SFrancis Kelly 	val &= ~(MLX5_RXP_RTRU_CSR_CTRL_GO);
212b34d8163SFrancis Kelly 	if (mlx5_devx_regex_register_write(ctx, id, MLX5_RXP_RTRU_CSR_CTRL,
213b34d8163SFrancis Kelly 					   val)) {
214b34d8163SFrancis Kelly 		DRV_LOG(ERR, "CSR write write failed!");
215b34d8163SFrancis Kelly 		return -1;
216b34d8163SFrancis Kelly 	}
217b34d8163SFrancis Kelly 
218b34d8163SFrancis Kelly 	DRV_LOG(DEBUG, "RXP Flush rules finished.");
219b34d8163SFrancis Kelly 	return 0;
220b34d8163SFrancis Kelly }
221b34d8163SFrancis Kelly 
222e3dbbf71SOri Kam static int
223e3dbbf71SOri Kam rxp_poll_csr_for_value(struct ibv_context *ctx, uint32_t *value,
224e3dbbf71SOri Kam 		       uint32_t address, uint32_t expected_value,
225e3dbbf71SOri Kam 		       uint32_t expected_mask, uint32_t timeout_ms, uint8_t id)
226e3dbbf71SOri Kam {
227e3dbbf71SOri Kam 	unsigned int i;
228*97de3671SMichael Baum 	int ret;
229e3dbbf71SOri Kam 
230e3dbbf71SOri Kam 	ret = -EBUSY;
231e3dbbf71SOri Kam 	for (i = 0; i < timeout_ms; i++) {
232e3dbbf71SOri Kam 		if (mlx5_devx_regex_register_read(ctx, id, address, value))
233e3dbbf71SOri Kam 			return -1;
234e3dbbf71SOri Kam 		if ((*value & expected_mask) == expected_value) {
235e3dbbf71SOri Kam 			ret = 0;
236e3dbbf71SOri Kam 			break;
237e3dbbf71SOri Kam 		}
238e3dbbf71SOri Kam 		rte_delay_us(1000);
239e3dbbf71SOri Kam 	}
240e3dbbf71SOri Kam 	return ret;
241e3dbbf71SOri Kam }
242e3dbbf71SOri Kam 
243e3dbbf71SOri Kam static int
244e3dbbf71SOri Kam rxp_start_engine(struct ibv_context *ctx, uint8_t id)
245e3dbbf71SOri Kam {
246e3dbbf71SOri Kam 	uint32_t ctrl;
247e3dbbf71SOri Kam 	int ret;
248e3dbbf71SOri Kam 
249e3dbbf71SOri Kam 	ret = mlx5_devx_regex_register_read(ctx, id, MLX5_RXP_CSR_CTRL, &ctrl);
250e3dbbf71SOri Kam 	if (ret)
251e3dbbf71SOri Kam 		return ret;
252e3dbbf71SOri Kam 	ctrl |= MLX5_RXP_CSR_CTRL_GO;
253b34d8163SFrancis Kelly 	ctrl |= MLX5_RXP_CSR_CTRL_DISABLE_L2C;
254e3dbbf71SOri Kam 	ret = mlx5_devx_regex_register_write(ctx, id, MLX5_RXP_CSR_CTRL, ctrl);
255e3dbbf71SOri Kam 	return ret;
256e3dbbf71SOri Kam }
257e3dbbf71SOri Kam 
258e3dbbf71SOri Kam static int
259e3dbbf71SOri Kam rxp_stop_engine(struct ibv_context *ctx, uint8_t id)
260e3dbbf71SOri Kam {
261e3dbbf71SOri Kam 	uint32_t ctrl;
262e3dbbf71SOri Kam 	int ret;
263e3dbbf71SOri Kam 
264e3dbbf71SOri Kam 	ret = mlx5_devx_regex_register_read(ctx, id, MLX5_RXP_CSR_CTRL, &ctrl);
265e3dbbf71SOri Kam 	if (ret)
266e3dbbf71SOri Kam 		return ret;
267e3dbbf71SOri Kam 	ctrl &= ~MLX5_RXP_CSR_CTRL_GO;
268e3dbbf71SOri Kam 	ret = mlx5_devx_regex_register_write(ctx, id, MLX5_RXP_CSR_CTRL, ctrl);
269e3dbbf71SOri Kam 	return ret;
270e3dbbf71SOri Kam }
271e3dbbf71SOri Kam 
272e3dbbf71SOri Kam static int
273e3dbbf71SOri Kam rxp_init_rtru(struct ibv_context *ctx, uint8_t id, uint32_t init_bits)
274e3dbbf71SOri Kam {
275e3dbbf71SOri Kam 	uint32_t ctrl_value;
276e3dbbf71SOri Kam 	uint32_t poll_value;
277e3dbbf71SOri Kam 	uint32_t expected_value;
278e3dbbf71SOri Kam 	uint32_t expected_mask;
279*97de3671SMichael Baum 	int ret;
280e3dbbf71SOri Kam 
281e3dbbf71SOri Kam 	/* Read the rtru ctrl CSR. */
282e3dbbf71SOri Kam 	ret = mlx5_devx_regex_register_read(ctx, id, MLX5_RXP_RTRU_CSR_CTRL,
283e3dbbf71SOri Kam 					    &ctrl_value);
284e3dbbf71SOri Kam 	if (ret)
285e3dbbf71SOri Kam 		return -1;
286e3dbbf71SOri Kam 	/* Clear any previous init modes. */
287e3dbbf71SOri Kam 	ctrl_value &= ~(MLX5_RXP_RTRU_CSR_CTRL_INIT_MODE_MASK);
288e3dbbf71SOri Kam 	if (ctrl_value & MLX5_RXP_RTRU_CSR_CTRL_INIT) {
289e3dbbf71SOri Kam 		ctrl_value &= ~(MLX5_RXP_RTRU_CSR_CTRL_INIT);
290e3dbbf71SOri Kam 		mlx5_devx_regex_register_write(ctx, id, MLX5_RXP_RTRU_CSR_CTRL,
291e3dbbf71SOri Kam 					       ctrl_value);
292e3dbbf71SOri Kam 	}
293e3dbbf71SOri Kam 	/* Set the init_mode bits in the rtru ctrl CSR. */
294e3dbbf71SOri Kam 	ctrl_value |= init_bits;
295e3dbbf71SOri Kam 	mlx5_devx_regex_register_write(ctx, id, MLX5_RXP_RTRU_CSR_CTRL,
296e3dbbf71SOri Kam 				       ctrl_value);
297e3dbbf71SOri Kam 	/* Need to sleep for a short period after pulsing the rtru init bit. */
298e3dbbf71SOri Kam 	rte_delay_us(20000);
299e3dbbf71SOri Kam 	/* Poll the rtru status CSR until all the init done bits are set. */
300e3dbbf71SOri Kam 	DRV_LOG(DEBUG, "waiting for RXP rule memory to complete init");
301e3dbbf71SOri Kam 	/* Set the init bit in the rtru ctrl CSR. */
302e3dbbf71SOri Kam 	ctrl_value |= MLX5_RXP_RTRU_CSR_CTRL_INIT;
303e3dbbf71SOri Kam 	mlx5_devx_regex_register_write(ctx, id, MLX5_RXP_RTRU_CSR_CTRL,
304e3dbbf71SOri Kam 				       ctrl_value);
305e3dbbf71SOri Kam 	/* Clear the init bit in the rtru ctrl CSR */
306e3dbbf71SOri Kam 	ctrl_value &= ~MLX5_RXP_RTRU_CSR_CTRL_INIT;
307e3dbbf71SOri Kam 	mlx5_devx_regex_register_write(ctx, id, MLX5_RXP_RTRU_CSR_CTRL,
308e3dbbf71SOri Kam 				       ctrl_value);
309e3dbbf71SOri Kam 	/* Check that the following bits are set in the RTRU_CSR. */
310e3dbbf71SOri Kam 	if (init_bits == MLX5_RXP_RTRU_CSR_CTRL_INIT_MODE_L1_L2) {
311e3dbbf71SOri Kam 		/* Must be incremental mode */
312e3dbbf71SOri Kam 		expected_value = MLX5_RXP_RTRU_CSR_STATUS_L1C_INIT_DONE |
313e3dbbf71SOri Kam 			MLX5_RXP_RTRU_CSR_STATUS_L2C_INIT_DONE;
314e3dbbf71SOri Kam 	} else {
315e3dbbf71SOri Kam 		expected_value = MLX5_RXP_RTRU_CSR_STATUS_IM_INIT_DONE |
316e3dbbf71SOri Kam 			MLX5_RXP_RTRU_CSR_STATUS_L1C_INIT_DONE |
317e3dbbf71SOri Kam 			MLX5_RXP_RTRU_CSR_STATUS_L2C_INIT_DONE;
318e3dbbf71SOri Kam 	}
319e3dbbf71SOri Kam 	expected_mask = expected_value;
320e3dbbf71SOri Kam 	ret = rxp_poll_csr_for_value(ctx, &poll_value,
321e3dbbf71SOri Kam 				     MLX5_RXP_RTRU_CSR_STATUS,
322e3dbbf71SOri Kam 				     expected_value, expected_mask,
323e3dbbf71SOri Kam 				     MLX5_RXP_CSR_STATUS_TRIAL_TIMEOUT, id);
324e3dbbf71SOri Kam 	if (ret)
325e3dbbf71SOri Kam 		return ret;
326e3dbbf71SOri Kam 	DRV_LOG(DEBUG, "rule memory initialise: 0x%08X", poll_value);
327e3dbbf71SOri Kam 	/* Clear the init bit in the rtru ctrl CSR */
328e3dbbf71SOri Kam 	ctrl_value &= ~(MLX5_RXP_RTRU_CSR_CTRL_INIT);
329e3dbbf71SOri Kam 	mlx5_devx_regex_register_write(ctx, id, MLX5_RXP_RTRU_CSR_CTRL,
330e3dbbf71SOri Kam 				       ctrl_value);
331e3dbbf71SOri Kam 	return 0;
332e3dbbf71SOri Kam }
333e3dbbf71SOri Kam 
334e3dbbf71SOri Kam static int
335b34d8163SFrancis Kelly rxp_parse_rof(const char *buf, uint32_t len,
336b34d8163SFrancis Kelly 	      struct mlx5_rxp_ctl_rules_pgm **rules)
337b34d8163SFrancis Kelly {
338b34d8163SFrancis Kelly 	static const char del[] = "\n\r";
339b34d8163SFrancis Kelly 	char *line;
340b34d8163SFrancis Kelly 	char *tmp;
341b34d8163SFrancis Kelly 	char *cur_pos;
342b34d8163SFrancis Kelly 	uint32_t lines = 0;
343b34d8163SFrancis Kelly 	uint32_t entries;
344b34d8163SFrancis Kelly 	struct mlx5_rxp_rof_entry *curentry;
345b34d8163SFrancis Kelly 
346b34d8163SFrancis Kelly 	tmp = rte_malloc("", len, 0);
347b34d8163SFrancis Kelly 	if (!tmp)
348b34d8163SFrancis Kelly 		return -ENOMEM;
349b34d8163SFrancis Kelly 	memcpy(tmp, buf, len);
350b34d8163SFrancis Kelly 	line = strtok(tmp, del);
351b34d8163SFrancis Kelly 	while (line) {
352b34d8163SFrancis Kelly 		if (line[0] != '#' && line[0] != '\0')
353b34d8163SFrancis Kelly 			lines++;
354b34d8163SFrancis Kelly 		line = strtok(NULL, del);
355b34d8163SFrancis Kelly 	}
356b34d8163SFrancis Kelly 	*rules = rte_malloc("", lines * sizeof(*curentry) + sizeof(**rules), 0);
357b34d8163SFrancis Kelly 	if (!(*rules)) {
358b34d8163SFrancis Kelly 		rte_free(tmp);
359b34d8163SFrancis Kelly 		return -ENOMEM;
360b34d8163SFrancis Kelly 	}
361b34d8163SFrancis Kelly 	memset(*rules, 0, lines * sizeof(curentry) + sizeof(**rules));
362b34d8163SFrancis Kelly 	curentry = (*rules)->rules;
363b34d8163SFrancis Kelly 	(*rules)->hdr.cmd = MLX5_RXP_CTL_RULES_PGM;
364b34d8163SFrancis Kelly 	entries = 0;
365b34d8163SFrancis Kelly 	memcpy(tmp, buf, len);
366b34d8163SFrancis Kelly 	line = strtok(tmp, del);
367b34d8163SFrancis Kelly 	while (line) {
368b34d8163SFrancis Kelly 		if (line[0] == '#' || line[0] == '\0') {
369b34d8163SFrancis Kelly 			line = strtok(NULL, del);
370b34d8163SFrancis Kelly 			continue;
371b34d8163SFrancis Kelly 		}
372b34d8163SFrancis Kelly 		curentry->type = strtoul(line, &cur_pos, 10);
373b34d8163SFrancis Kelly 		if (cur_pos == line || cur_pos[0] != ',')
374b34d8163SFrancis Kelly 			goto parse_error;
375b34d8163SFrancis Kelly 		cur_pos++;
376b34d8163SFrancis Kelly 		curentry->addr = strtoul(cur_pos, &cur_pos, 16);
377b34d8163SFrancis Kelly 		if (cur_pos[0] != ',')
378b34d8163SFrancis Kelly 			goto parse_error;
379b34d8163SFrancis Kelly 		cur_pos++;
380b34d8163SFrancis Kelly 		curentry->value = strtoull(cur_pos, &cur_pos, 16);
381b34d8163SFrancis Kelly 		if (cur_pos[0] != '\0' && cur_pos[0] != '\n')
382b34d8163SFrancis Kelly 			goto parse_error;
383b34d8163SFrancis Kelly 		curentry++;
384b34d8163SFrancis Kelly 		entries++;
385b34d8163SFrancis Kelly 		if (entries > lines)
386b34d8163SFrancis Kelly 			goto parse_error;
387b34d8163SFrancis Kelly 		line = strtok(NULL, del);
388b34d8163SFrancis Kelly 	}
389b34d8163SFrancis Kelly 	(*rules)->count = entries;
390b34d8163SFrancis Kelly 	(*rules)->hdr.len = entries * sizeof(*curentry) + sizeof(**rules);
391b34d8163SFrancis Kelly 	rte_free(tmp);
392b34d8163SFrancis Kelly 	return 0;
393b34d8163SFrancis Kelly parse_error:
394b34d8163SFrancis Kelly 	rte_free(tmp);
395b34d8163SFrancis Kelly 	if (*rules)
396b34d8163SFrancis Kelly 		rte_free(*rules);
397b34d8163SFrancis Kelly 	return -EINVAL;
398b34d8163SFrancis Kelly }
399b34d8163SFrancis Kelly 
400b34d8163SFrancis Kelly static int
401b34d8163SFrancis Kelly mlnx_set_database(struct mlx5_regex_priv *priv, uint8_t id, uint8_t db_to_use)
402b34d8163SFrancis Kelly {
403b34d8163SFrancis Kelly 	int ret;
404b34d8163SFrancis Kelly 	uint32_t umem_id;
405b34d8163SFrancis Kelly 
406b34d8163SFrancis Kelly 	ret = mlx5_devx_regex_database_stop(priv->ctx, id);
407b34d8163SFrancis Kelly 	if (ret < 0) {
408b34d8163SFrancis Kelly 		DRV_LOG(ERR, "stop engine failed!");
409b34d8163SFrancis Kelly 		return ret;
410b34d8163SFrancis Kelly 	}
411b34d8163SFrancis Kelly 	umem_id = mlx5_os_get_umem_id(priv->db[db_to_use].umem.umem);
412b34d8163SFrancis Kelly 	ret = mlx5_devx_regex_database_program(priv->ctx, id, umem_id, 0);
413b34d8163SFrancis Kelly 	if (ret < 0) {
414b34d8163SFrancis Kelly 		DRV_LOG(ERR, "program db failed!");
415b34d8163SFrancis Kelly 		return ret;
416b34d8163SFrancis Kelly 	}
417b34d8163SFrancis Kelly 	return 0;
418b34d8163SFrancis Kelly }
419b34d8163SFrancis Kelly 
420b34d8163SFrancis Kelly static int
421b34d8163SFrancis Kelly mlnx_resume_database(struct mlx5_regex_priv *priv, uint8_t id)
422b34d8163SFrancis Kelly {
423b34d8163SFrancis Kelly 	mlx5_devx_regex_database_resume(priv->ctx, id);
424b34d8163SFrancis Kelly 	return 0;
425b34d8163SFrancis Kelly }
426b34d8163SFrancis Kelly 
427b34d8163SFrancis Kelly /*
428b34d8163SFrancis Kelly  * Assign db memory for RXP programming.
429b34d8163SFrancis Kelly  */
430b34d8163SFrancis Kelly static int
431b34d8163SFrancis Kelly mlnx_update_database(struct mlx5_regex_priv *priv, uint8_t id)
432b34d8163SFrancis Kelly {
433b34d8163SFrancis Kelly 	unsigned int i;
434b34d8163SFrancis Kelly 	uint8_t db_free = MLX5_RXP_DB_NOT_ASSIGNED;
435b34d8163SFrancis Kelly 	uint8_t eng_assigned = MLX5_RXP_DB_NOT_ASSIGNED;
436b34d8163SFrancis Kelly 
437b34d8163SFrancis Kelly 	/* Check which database rxp_eng is currently located if any? */
438b34d8163SFrancis Kelly 	for (i = 0; i < (priv->nb_engines + MLX5_RXP_EM_COUNT);
439b34d8163SFrancis Kelly 	     i++) {
440b34d8163SFrancis Kelly 		if (priv->db[i].db_assigned_to_eng_num == id) {
441b34d8163SFrancis Kelly 			eng_assigned = i;
442b34d8163SFrancis Kelly 			break;
443b34d8163SFrancis Kelly 		}
444b34d8163SFrancis Kelly 	}
445b34d8163SFrancis Kelly 	/*
446b34d8163SFrancis Kelly 	 * If private mode then, we can keep the same db ptr as RXP will be
447b34d8163SFrancis Kelly 	 * programming EM itself if necessary, however need to see if
448b34d8163SFrancis Kelly 	 * programmed yet.
449b34d8163SFrancis Kelly 	 */
450b34d8163SFrancis Kelly 	if ((priv->prog_mode == MLX5_RXP_PRIVATE_PROG_MODE) &&
451b34d8163SFrancis Kelly 	    (eng_assigned != MLX5_RXP_DB_NOT_ASSIGNED))
452b34d8163SFrancis Kelly 		return eng_assigned;
453b34d8163SFrancis Kelly 	/* Check for inactive db memory to use. */
454b34d8163SFrancis Kelly 	for (i = 0; i < (priv->nb_engines + MLX5_RXP_EM_COUNT);
455b34d8163SFrancis Kelly 	     i++) {
456b34d8163SFrancis Kelly 		if (priv->db[i].active == true)
457b34d8163SFrancis Kelly 			continue; /* Already in use, so skip db. */
458b34d8163SFrancis Kelly 		/* Set this db to active now as free to use. */
459b34d8163SFrancis Kelly 		priv->db[i].active = true;
460b34d8163SFrancis Kelly 		/* Now unassign last db index in use by RXP Eng. */
461b34d8163SFrancis Kelly 		if (eng_assigned != MLX5_RXP_DB_NOT_ASSIGNED) {
462b34d8163SFrancis Kelly 			priv->db[eng_assigned].active = false;
463b34d8163SFrancis Kelly 			priv->db[eng_assigned].db_assigned_to_eng_num =
464b34d8163SFrancis Kelly 				MLX5_RXP_DB_NOT_ASSIGNED;
465b34d8163SFrancis Kelly 
466b34d8163SFrancis Kelly 			/* Set all DB memory to 0's before setting up DB. */
467b34d8163SFrancis Kelly 			memset(priv->db[i].ptr, 0x00, MLX5_MAX_DB_SIZE);
468b34d8163SFrancis Kelly 		}
469b34d8163SFrancis Kelly 		/* Now reassign new db index with RXP Engine. */
470b34d8163SFrancis Kelly 		priv->db[i].db_assigned_to_eng_num = id;
471b34d8163SFrancis Kelly 		db_free = i;
472b34d8163SFrancis Kelly 		break;
473b34d8163SFrancis Kelly 	}
474b34d8163SFrancis Kelly 	if (db_free == MLX5_RXP_DB_NOT_ASSIGNED)
475b34d8163SFrancis Kelly 		return -1;
476b34d8163SFrancis Kelly 	return db_free;
477b34d8163SFrancis Kelly }
478b34d8163SFrancis Kelly 
479b34d8163SFrancis Kelly /*
480b34d8163SFrancis Kelly  * Program RXP instruction db to RXP engine/s.
481b34d8163SFrancis Kelly  */
482b34d8163SFrancis Kelly static int
483b34d8163SFrancis Kelly program_rxp_rules(struct mlx5_regex_priv *priv,
484b34d8163SFrancis Kelly 		  struct mlx5_rxp_ctl_rules_pgm *rules, uint8_t id)
485b34d8163SFrancis Kelly {
486b34d8163SFrancis Kelly 	int ret, db_free;
487b34d8163SFrancis Kelly 	uint32_t rule_cnt;
488b34d8163SFrancis Kelly 
489b34d8163SFrancis Kelly 	rule_cnt = rules->count;
490b34d8163SFrancis Kelly 	db_free = mlnx_update_database(priv, id);
491b34d8163SFrancis Kelly 	if (db_free < 0) {
492b34d8163SFrancis Kelly 		DRV_LOG(ERR, "Failed to setup db memory!");
493b34d8163SFrancis Kelly 		return db_free;
494b34d8163SFrancis Kelly 	}
495b34d8163SFrancis Kelly 	if (priv->prog_mode == MLX5_RXP_PRIVATE_PROG_MODE) {
496b34d8163SFrancis Kelly 		/* Register early to ensure RXP writes to EM use valid addr. */
497b34d8163SFrancis Kelly 		ret = mlnx_set_database(priv, id, db_free);
498b34d8163SFrancis Kelly 		if (ret < 0) {
499b34d8163SFrancis Kelly 			DRV_LOG(ERR, "Failed to register db memory!");
500b34d8163SFrancis Kelly 			return ret;
501b34d8163SFrancis Kelly 		}
502b34d8163SFrancis Kelly 	}
503b34d8163SFrancis Kelly 	ret = write_private_rules(priv, rules, id);
504b34d8163SFrancis Kelly 	if (ret < 0) {
505b34d8163SFrancis Kelly 		DRV_LOG(ERR, "Failed to write rules!");
506b34d8163SFrancis Kelly 		return ret;
507b34d8163SFrancis Kelly 	}
508b34d8163SFrancis Kelly 	if (priv->prog_mode == MLX5_RXP_SHARED_PROG_MODE) {
509b34d8163SFrancis Kelly 		/* Write external rules directly to EM. */
510b34d8163SFrancis Kelly 		rules->count = rule_cnt;
511b34d8163SFrancis Kelly 	       /* Now write external instructions to EM. */
512b34d8163SFrancis Kelly 		ret = write_shared_rules(priv, rules, rules->hdr.len, db_free);
513b34d8163SFrancis Kelly 		if (ret < 0) {
514b34d8163SFrancis Kelly 			DRV_LOG(ERR, "Failed to write EM rules!");
515b34d8163SFrancis Kelly 			return ret;
516b34d8163SFrancis Kelly 		}
517b34d8163SFrancis Kelly 		ret = mlnx_set_database(priv, id, db_free);
518b34d8163SFrancis Kelly 		if (ret < 0) {
519b34d8163SFrancis Kelly 			DRV_LOG(ERR, "Failed to register db memory!");
520b34d8163SFrancis Kelly 			return ret;
521b34d8163SFrancis Kelly 		}
522b34d8163SFrancis Kelly 	}
523b34d8163SFrancis Kelly 	ret = mlnx_resume_database(priv, id);
524b34d8163SFrancis Kelly 	if (ret < 0) {
525b34d8163SFrancis Kelly 		DRV_LOG(ERR, "Failed to resume engine!");
526b34d8163SFrancis Kelly 		return ret;
527b34d8163SFrancis Kelly 	}
528b34d8163SFrancis Kelly 	DRV_LOG(DEBUG, "Programmed RXP Engine %d\n", id);
529b34d8163SFrancis Kelly 	rules->count = rule_cnt;
530b34d8163SFrancis Kelly 	return 0;
531b34d8163SFrancis Kelly }
532b34d8163SFrancis Kelly 
533b34d8163SFrancis Kelly static int
534b34d8163SFrancis Kelly rxp_init_eng(struct mlx5_regex_priv *priv, uint8_t id)
535e3dbbf71SOri Kam {
536e3dbbf71SOri Kam 	uint32_t ctrl;
537e3dbbf71SOri Kam 	uint32_t reg;
538e3dbbf71SOri Kam 	struct ibv_context *ctx = priv->ctx;
539e3dbbf71SOri Kam 	int ret;
540e3dbbf71SOri Kam 
541e3dbbf71SOri Kam 	ret = mlx5_devx_regex_register_read(ctx, id, MLX5_RXP_CSR_CTRL, &ctrl);
542e3dbbf71SOri Kam 	if (ret)
543e3dbbf71SOri Kam 		return ret;
544e3dbbf71SOri Kam 	if (ctrl & MLX5_RXP_CSR_CTRL_INIT) {
545e3dbbf71SOri Kam 		ctrl &= ~MLX5_RXP_CSR_CTRL_INIT;
546e3dbbf71SOri Kam 		ret = mlx5_devx_regex_register_write(ctx, id, MLX5_RXP_CSR_CTRL,
547e3dbbf71SOri Kam 						     ctrl);
548e3dbbf71SOri Kam 		if (ret)
549e3dbbf71SOri Kam 			return ret;
550e3dbbf71SOri Kam 	}
551e3dbbf71SOri Kam 	ctrl |= MLX5_RXP_CSR_CTRL_INIT;
552e3dbbf71SOri Kam 	ret = mlx5_devx_regex_register_write(ctx, id, MLX5_RXP_CSR_CTRL, ctrl);
553e3dbbf71SOri Kam 	if (ret)
554e3dbbf71SOri Kam 		return ret;
555e3dbbf71SOri Kam 	ctrl &= ~MLX5_RXP_CSR_CTRL_INIT;
556e3dbbf71SOri Kam 	ret = mlx5_devx_regex_register_write(ctx, id, MLX5_RXP_CSR_CTRL, ctrl);
557e3dbbf71SOri Kam 	rte_delay_us(20000);
558e3dbbf71SOri Kam 	ret = rxp_poll_csr_for_value(ctx, &ctrl, MLX5_RXP_CSR_STATUS,
559e3dbbf71SOri Kam 				     MLX5_RXP_CSR_STATUS_INIT_DONE,
560e3dbbf71SOri Kam 				     MLX5_RXP_CSR_STATUS_INIT_DONE,
561e3dbbf71SOri Kam 				     MLX5_RXP_CSR_STATUS_TRIAL_TIMEOUT, id);
562e3dbbf71SOri Kam 	if (ret)
563e3dbbf71SOri Kam 		return ret;
564e3dbbf71SOri Kam 	ret = mlx5_devx_regex_register_read(ctx, id, MLX5_RXP_CSR_CTRL, &ctrl);
565e3dbbf71SOri Kam 	if (ret)
566e3dbbf71SOri Kam 		return ret;
567e3dbbf71SOri Kam 	ctrl &= ~MLX5_RXP_CSR_CTRL_INIT;
568e3dbbf71SOri Kam 	ret = mlx5_devx_regex_register_write(ctx, id, MLX5_RXP_CSR_CTRL,
569e3dbbf71SOri Kam 					     ctrl);
570e3dbbf71SOri Kam 	if (ret)
571e3dbbf71SOri Kam 		return ret;
572e3dbbf71SOri Kam 	ret = rxp_init_rtru(ctx, id, MLX5_RXP_RTRU_CSR_CTRL_INIT_MODE_IM_L1_L2);
573e3dbbf71SOri Kam 	if (ret)
574e3dbbf71SOri Kam 		return ret;
575e3dbbf71SOri Kam 	ret = mlx5_devx_regex_register_read(ctx, id, MLX5_RXP_CSR_CAPABILITY_5,
576e3dbbf71SOri Kam 					    &reg);
577e3dbbf71SOri Kam 	if (ret)
578e3dbbf71SOri Kam 		return ret;
579e3dbbf71SOri Kam 	DRV_LOG(DEBUG, "max matches: %d, DDOS threshold: %d", reg >> 16,
580e3dbbf71SOri Kam 		reg & 0xffff);
581b34d8163SFrancis Kelly 	if ((reg >> 16) >= priv->nb_max_matches)
582b34d8163SFrancis Kelly 		ret = mlx5_devx_regex_register_write(ctx, id,
583b34d8163SFrancis Kelly 						     MLX5_RXP_CSR_MAX_MATCH,
584e3dbbf71SOri Kam 						     priv->nb_max_matches);
585b34d8163SFrancis Kelly 	else
586b34d8163SFrancis Kelly 		ret = mlx5_devx_regex_register_write(ctx, id,
587b34d8163SFrancis Kelly 						     MLX5_RXP_CSR_MAX_MATCH,
588b34d8163SFrancis Kelly 						     (reg >> 16));
589b34d8163SFrancis Kelly 	ret |= mlx5_devx_regex_register_write(ctx, id, MLX5_RXP_CSR_MAX_PREFIX,
590b34d8163SFrancis Kelly 					 (reg & 0xFFFF));
591e3dbbf71SOri Kam 	ret |= mlx5_devx_regex_register_write(ctx, id,
592e3dbbf71SOri Kam 					      MLX5_RXP_CSR_MAX_LATENCY, 0);
593e3dbbf71SOri Kam 	ret |= mlx5_devx_regex_register_write(ctx, id,
594e3dbbf71SOri Kam 					      MLX5_RXP_CSR_MAX_PRI_THREAD, 0);
595e3dbbf71SOri Kam 	return ret;
596e3dbbf71SOri Kam }
597e3dbbf71SOri Kam 
598b34d8163SFrancis Kelly static int
599b34d8163SFrancis Kelly write_private_rules(struct mlx5_regex_priv *priv,
600b34d8163SFrancis Kelly 		    struct mlx5_rxp_ctl_rules_pgm *rules,
601b34d8163SFrancis Kelly 		    uint8_t id)
602b34d8163SFrancis Kelly {
603b34d8163SFrancis Kelly 	unsigned int pending;
604b34d8163SFrancis Kelly 	uint32_t block, reg, val, rule_cnt, rule_offset, rtru_max_num_entries;
605b34d8163SFrancis Kelly 	int ret = 1;
606b34d8163SFrancis Kelly 
607b34d8163SFrancis Kelly 	if (priv->prog_mode == MLX5_RXP_MODE_NOT_DEFINED)
608b34d8163SFrancis Kelly 		return -EINVAL;
609b34d8163SFrancis Kelly 	if (rules->hdr.len == 0 || rules->hdr.cmd < MLX5_RXP_CTL_RULES_PGM ||
610b34d8163SFrancis Kelly 				   rules->hdr.cmd > MLX5_RXP_CTL_RULES_PGM_INCR)
611b34d8163SFrancis Kelly 		return -EINVAL;
612b34d8163SFrancis Kelly 	/* For a non-incremental rules program, re-init the RXP. */
613b34d8163SFrancis Kelly 	if (rules->hdr.cmd == MLX5_RXP_CTL_RULES_PGM) {
614b34d8163SFrancis Kelly 		ret = rxp_init_eng(priv, id);
615b34d8163SFrancis Kelly 		if (ret < 0)
616b34d8163SFrancis Kelly 			return ret;
617b34d8163SFrancis Kelly 	} else if (rules->hdr.cmd == MLX5_RXP_CTL_RULES_PGM_INCR) {
618b34d8163SFrancis Kelly 		/* Flush RXP L1 and L2 cache by using MODE_L1_L2. */
619b34d8163SFrancis Kelly 		ret = rxp_init_rtru(priv->ctx, id,
620b34d8163SFrancis Kelly 				    MLX5_RXP_RTRU_CSR_CTRL_INIT_MODE_L1_L2);
621b34d8163SFrancis Kelly 		if (ret < 0)
622b34d8163SFrancis Kelly 			return ret;
623b34d8163SFrancis Kelly 	}
624b34d8163SFrancis Kelly 	if (rules->count == 0)
625b34d8163SFrancis Kelly 		return -EINVAL;
626b34d8163SFrancis Kelly 	/* Confirm the RXP is initialised. */
627b34d8163SFrancis Kelly 	if (mlx5_devx_regex_register_read(priv->ctx, id,
628b34d8163SFrancis Kelly 					    MLX5_RXP_CSR_STATUS, &val)) {
629b34d8163SFrancis Kelly 		DRV_LOG(ERR, "Failed to read from RXP!");
630b34d8163SFrancis Kelly 		return -ENODEV;
631b34d8163SFrancis Kelly 	}
632b34d8163SFrancis Kelly 	if (!(val & MLX5_RXP_CSR_STATUS_INIT_DONE)) {
633b34d8163SFrancis Kelly 		DRV_LOG(ERR, "RXP not initialised...");
634b34d8163SFrancis Kelly 		return -EBUSY;
635b34d8163SFrancis Kelly 	}
636b34d8163SFrancis Kelly 	/* Get the RTRU maximum number of entries allowed. */
637b34d8163SFrancis Kelly 	if (mlx5_devx_regex_register_read(priv->ctx, id,
638b34d8163SFrancis Kelly 			MLX5_RXP_RTRU_CSR_CAPABILITY, &rtru_max_num_entries)) {
639b34d8163SFrancis Kelly 		DRV_LOG(ERR, "Failed to read RTRU capability!");
640b34d8163SFrancis Kelly 		return -ENODEV;
641b34d8163SFrancis Kelly 	}
642b34d8163SFrancis Kelly 	rtru_max_num_entries = (rtru_max_num_entries & 0x00FF);
643b34d8163SFrancis Kelly 	rule_cnt = 0;
644b34d8163SFrancis Kelly 	pending = 0;
645b34d8163SFrancis Kelly 	while (rules->count > 0) {
646b34d8163SFrancis Kelly 		if ((rules->rules[rule_cnt].type == MLX5_RXP_ROF_ENTRY_INST) ||
647b34d8163SFrancis Kelly 		    (rules->rules[rule_cnt].type == MLX5_RXP_ROF_ENTRY_IM) ||
648b34d8163SFrancis Kelly 		    (rules->rules[rule_cnt].type == MLX5_RXP_ROF_ENTRY_EM)) {
649b34d8163SFrancis Kelly 			if ((rules->rules[rule_cnt].type ==
650b34d8163SFrancis Kelly 			     MLX5_RXP_ROF_ENTRY_EM) &&
651b34d8163SFrancis Kelly 			    (priv->prog_mode == MLX5_RXP_SHARED_PROG_MODE)) {
652b34d8163SFrancis Kelly 				/* Skip EM rules programming. */
653b34d8163SFrancis Kelly 				if (pending > 0) {
654b34d8163SFrancis Kelly 					/* Flush any rules that are pending. */
655b34d8163SFrancis Kelly 					rule_offset = (rule_cnt - pending);
656b34d8163SFrancis Kelly 					ret = rxp_flush_rules(priv->ctx,
657b34d8163SFrancis Kelly 						&rules->rules[rule_offset],
658b34d8163SFrancis Kelly 						pending, id);
659b34d8163SFrancis Kelly 					if (ret < 0) {
660b34d8163SFrancis Kelly 						DRV_LOG(ERR, "Flushing rules.");
661b34d8163SFrancis Kelly 						return -ENODEV;
662b34d8163SFrancis Kelly 					}
663b34d8163SFrancis Kelly 					pending = 0;
664b34d8163SFrancis Kelly 				}
665b34d8163SFrancis Kelly 				rule_cnt++;
666b34d8163SFrancis Kelly 			} else {
667b34d8163SFrancis Kelly 				pending++;
668b34d8163SFrancis Kelly 				rule_cnt++;
669b34d8163SFrancis Kelly 				/*
670b34d8163SFrancis Kelly 				 * If parsing the last rule, or if reached the
671b34d8163SFrancis Kelly 				 * maximum number of rules for this batch, then
672b34d8163SFrancis Kelly 				 * flush the rules batch to the RXP.
673b34d8163SFrancis Kelly 				 */
674b34d8163SFrancis Kelly 				if ((rules->count == 1) ||
675b34d8163SFrancis Kelly 				    (pending == rtru_max_num_entries)) {
676b34d8163SFrancis Kelly 					rule_offset = (rule_cnt - pending);
677b34d8163SFrancis Kelly 					ret = rxp_flush_rules(priv->ctx,
678b34d8163SFrancis Kelly 						&rules->rules[rule_offset],
679b34d8163SFrancis Kelly 						pending, id);
680b34d8163SFrancis Kelly 					if (ret < 0) {
681b34d8163SFrancis Kelly 						DRV_LOG(ERR, "Flushing rules.");
682b34d8163SFrancis Kelly 						return -ENODEV;
683b34d8163SFrancis Kelly 					}
684b34d8163SFrancis Kelly 					pending = 0;
685b34d8163SFrancis Kelly 				}
686b34d8163SFrancis Kelly 			}
687b34d8163SFrancis Kelly 		} else if ((rules->rules[rule_cnt].type ==
688b34d8163SFrancis Kelly 				MLX5_RXP_ROF_ENTRY_EQ) ||
689b34d8163SFrancis Kelly 			 (rules->rules[rule_cnt].type ==
690b34d8163SFrancis Kelly 				MLX5_RXP_ROF_ENTRY_GTE) ||
691b34d8163SFrancis Kelly 			 (rules->rules[rule_cnt].type ==
692b34d8163SFrancis Kelly 				MLX5_RXP_ROF_ENTRY_LTE) ||
693b34d8163SFrancis Kelly 			 (rules->rules[rule_cnt].type ==
694b34d8163SFrancis Kelly 				MLX5_RXP_ROF_ENTRY_CHECKSUM) ||
695b34d8163SFrancis Kelly 			 (rules->rules[rule_cnt].type ==
696b34d8163SFrancis Kelly 				MLX5_RXP_ROF_ENTRY_CHECKSUM_EX_EM)) {
697b34d8163SFrancis Kelly 			if (pending) {
698b34d8163SFrancis Kelly 				/* Flush rules before checking reg values. */
699b34d8163SFrancis Kelly 				rule_offset = (rule_cnt - pending);
700b34d8163SFrancis Kelly 				ret = rxp_flush_rules(priv->ctx,
701b34d8163SFrancis Kelly 					&rules->rules[rule_offset],
702b34d8163SFrancis Kelly 					pending, id);
703b34d8163SFrancis Kelly 				if (ret < 0) {
704b34d8163SFrancis Kelly 					DRV_LOG(ERR, "Failed to flush rules.");
705b34d8163SFrancis Kelly 					return -ENODEV;
706b34d8163SFrancis Kelly 				}
707b34d8163SFrancis Kelly 			}
708b34d8163SFrancis Kelly 			block = (rules->rules[rule_cnt].addr >> 16) & 0xFFFF;
709b34d8163SFrancis Kelly 			if (block == 0)
710b34d8163SFrancis Kelly 				reg = MLX5_RXP_CSR_BASE_ADDRESS;
711b34d8163SFrancis Kelly 			else if (block == 1)
712b34d8163SFrancis Kelly 				reg = MLX5_RXP_RTRU_CSR_BASE_ADDRESS;
713b34d8163SFrancis Kelly 			else {
714b34d8163SFrancis Kelly 				DRV_LOG(ERR, "Invalid ROF register 0x%08X!",
715b34d8163SFrancis Kelly 					rules->rules[rule_cnt].addr);
716b34d8163SFrancis Kelly 				return -EINVAL;
717b34d8163SFrancis Kelly 			}
718b34d8163SFrancis Kelly 			reg += (rules->rules[rule_cnt].addr & 0xFFFF) *
719b34d8163SFrancis Kelly 				MLX5_RXP_CSR_WIDTH;
720b34d8163SFrancis Kelly 			ret = mlx5_devx_regex_register_read(priv->ctx, id,
721b34d8163SFrancis Kelly 							    reg, &val);
722b34d8163SFrancis Kelly 			if (ret) {
723b34d8163SFrancis Kelly 				DRV_LOG(ERR, "RXP CSR read failed!");
724b34d8163SFrancis Kelly 				return ret;
725b34d8163SFrancis Kelly 			}
726b34d8163SFrancis Kelly 			if ((priv->prog_mode == MLX5_RXP_SHARED_PROG_MODE) &&
727b34d8163SFrancis Kelly 			    ((rules->rules[rule_cnt].type ==
728b34d8163SFrancis Kelly 			    MLX5_RXP_ROF_ENTRY_CHECKSUM_EX_EM) &&
729b34d8163SFrancis Kelly 			    (val != rules->rules[rule_cnt].value))) {
730b34d8163SFrancis Kelly 				DRV_LOG(ERR, "Unexpected value for register:");
731b34d8163SFrancis Kelly 				DRV_LOG(ERR, "reg %x" PRIu32 " got %x" PRIu32,
732b34d8163SFrancis Kelly 					rules->rules[rule_cnt].addr, val);
733b34d8163SFrancis Kelly 				DRV_LOG(ERR, "expected %" PRIx64 ".",
734b34d8163SFrancis Kelly 					rules->rules[rule_cnt].value);
735b34d8163SFrancis Kelly 					return -EINVAL;
736b34d8163SFrancis Kelly 			} else if ((priv->prog_mode ==
737b34d8163SFrancis Kelly 				 MLX5_RXP_PRIVATE_PROG_MODE) &&
738b34d8163SFrancis Kelly 				 (rules->rules[rule_cnt].type ==
739b34d8163SFrancis Kelly 				 MLX5_RXP_ROF_ENTRY_CHECKSUM) &&
740b34d8163SFrancis Kelly 				 (val != rules->rules[rule_cnt].value)) {
741b34d8163SFrancis Kelly 				DRV_LOG(ERR, "Unexpected value for register:");
742b34d8163SFrancis Kelly 				DRV_LOG(ERR, "reg %x" PRIu32 " got %x" PRIu32,
743b34d8163SFrancis Kelly 					rules->rules[rule_cnt].addr, val);
744b34d8163SFrancis Kelly 				DRV_LOG(ERR, "expected %" PRIx64 ".",
745b34d8163SFrancis Kelly 					rules->rules[rule_cnt].value);
746b34d8163SFrancis Kelly 				return -EINVAL;
747b34d8163SFrancis Kelly 			} else if ((rules->rules[rule_cnt].type ==
748b34d8163SFrancis Kelly 					MLX5_RXP_ROF_ENTRY_EQ) &&
749b34d8163SFrancis Kelly 				  (val != rules->rules[rule_cnt].value)) {
750b34d8163SFrancis Kelly 				DRV_LOG(ERR, "Unexpected value for register:");
751b34d8163SFrancis Kelly 				DRV_LOG(ERR, "reg %x" PRIu32 " got %x" PRIu32,
752b34d8163SFrancis Kelly 					rules->rules[rule_cnt].addr, val);
753b34d8163SFrancis Kelly 				DRV_LOG(ERR, "expected %" PRIx64 ".",
754b34d8163SFrancis Kelly 					rules->rules[rule_cnt].value);
755b34d8163SFrancis Kelly 					return -EINVAL;
756b34d8163SFrancis Kelly 			} else if ((rules->rules[rule_cnt].type ==
757b34d8163SFrancis Kelly 					MLX5_RXP_ROF_ENTRY_GTE) &&
758b34d8163SFrancis Kelly 				 (val < rules->rules[rule_cnt].value)) {
759b34d8163SFrancis Kelly 				DRV_LOG(ERR, "Unexpected value reg 0x%08X,",
760b34d8163SFrancis Kelly 					rules->rules[rule_cnt].addr);
761b34d8163SFrancis Kelly 				DRV_LOG(ERR, "got %X, expected >= %" PRIx64 ".",
762b34d8163SFrancis Kelly 					val, rules->rules[rule_cnt].value);
763b34d8163SFrancis Kelly 				return -EINVAL;
764b34d8163SFrancis Kelly 			} else if ((rules->rules[rule_cnt].type ==
765b34d8163SFrancis Kelly 					MLX5_RXP_ROF_ENTRY_LTE) &&
766b34d8163SFrancis Kelly 				 (val > rules->rules[rule_cnt].value)) {
767b34d8163SFrancis Kelly 				DRV_LOG(ERR, "Unexpected value reg 0x%08X,",
768b34d8163SFrancis Kelly 					rules->rules[rule_cnt].addr);
769b34d8163SFrancis Kelly 				DRV_LOG(ERR, "got %08X expected <= %" PRIx64,
770b34d8163SFrancis Kelly 					val, rules->rules[rule_cnt].value);
771b34d8163SFrancis Kelly 				return -EINVAL;
772b34d8163SFrancis Kelly 			}
773b34d8163SFrancis Kelly 			rule_cnt++;
774b34d8163SFrancis Kelly 			pending = 0;
775b34d8163SFrancis Kelly 		} else {
776b34d8163SFrancis Kelly 			DRV_LOG(ERR, "Error: Invalid rule type %d!",
777b34d8163SFrancis Kelly 				rules->rules[rule_cnt].type);
778b34d8163SFrancis Kelly 			return -EINVAL;
779b34d8163SFrancis Kelly 		}
780b34d8163SFrancis Kelly 		rules->count--;
781b34d8163SFrancis Kelly 	}
782b34d8163SFrancis Kelly 	return ret;
783b34d8163SFrancis Kelly }
784b34d8163SFrancis Kelly 
785b34d8163SFrancis Kelly /*
786b34d8163SFrancis Kelly  * Shared memory programming mode, here all external db instructions are written
787b34d8163SFrancis Kelly  * to EM via the host.
788b34d8163SFrancis Kelly  */
789b34d8163SFrancis Kelly static int
790b34d8163SFrancis Kelly write_shared_rules(struct mlx5_regex_priv *priv,
791b34d8163SFrancis Kelly 		   struct mlx5_rxp_ctl_rules_pgm *rules, uint32_t count,
792b34d8163SFrancis Kelly 		   uint8_t db_to_program)
793b34d8163SFrancis Kelly {
794b34d8163SFrancis Kelly 	uint32_t rule_cnt, rof_rule_addr;
795b34d8163SFrancis Kelly 	uint64_t tmp_write_swap[4];
796b34d8163SFrancis Kelly 
797b34d8163SFrancis Kelly 	if (priv->prog_mode == MLX5_RXP_MODE_NOT_DEFINED)
798b34d8163SFrancis Kelly 		return -EINVAL;
799b34d8163SFrancis Kelly 	if ((rules->count == 0) || (count == 0))
800b34d8163SFrancis Kelly 		return -EINVAL;
801b34d8163SFrancis Kelly 	rule_cnt = 0;
802b34d8163SFrancis Kelly 	/*
803b34d8163SFrancis Kelly 	 * Note the following section of code carries out a 32byte swap of
804b34d8163SFrancis Kelly 	 * instruction to coincide with HW 32byte swap. This may need removed
805b34d8163SFrancis Kelly 	 * in new variants of this programming function!
806b34d8163SFrancis Kelly 	 */
807b34d8163SFrancis Kelly 	while (rule_cnt < rules->count) {
808b34d8163SFrancis Kelly 		if ((rules->rules[rule_cnt].type == MLX5_RXP_ROF_ENTRY_EM) &&
809b34d8163SFrancis Kelly 		    (priv->prog_mode == MLX5_RXP_SHARED_PROG_MODE)) {
810b34d8163SFrancis Kelly 			/*
811b34d8163SFrancis Kelly 			 * Note there are always blocks of 8 instructions for
812b34d8163SFrancis Kelly 			 * 7's written sequentially. However there is no
813b34d8163SFrancis Kelly 			 * guarantee that all blocks are sequential!
814b34d8163SFrancis Kelly 			 */
815b34d8163SFrancis Kelly 			if (count >= (rule_cnt + MLX5_RXP_INST_BLOCK_SIZE)) {
816b34d8163SFrancis Kelly 				/*
817b34d8163SFrancis Kelly 				 * Ensure memory write not exceeding boundary
818b34d8163SFrancis Kelly 				 * Check essential to ensure 0x10000 offset
819b34d8163SFrancis Kelly 				 * accounted for!
820b34d8163SFrancis Kelly 				 */
821b34d8163SFrancis Kelly 				if ((uint8_t *)((uint8_t *)
822b34d8163SFrancis Kelly 				    priv->db[db_to_program].ptr +
823b34d8163SFrancis Kelly 				    ((rules->rules[rule_cnt + 7].addr <<
824b34d8163SFrancis Kelly 				    MLX5_RXP_INST_OFFSET))) >=
825b34d8163SFrancis Kelly 				    ((uint8_t *)((uint8_t *)
826b34d8163SFrancis Kelly 				    priv->db[db_to_program].ptr +
827b34d8163SFrancis Kelly 				    MLX5_MAX_DB_SIZE))) {
828b34d8163SFrancis Kelly 					DRV_LOG(ERR, "DB exceeded memory!");
829b34d8163SFrancis Kelly 					return -ENODEV;
830b34d8163SFrancis Kelly 				}
831b34d8163SFrancis Kelly 				/*
832b34d8163SFrancis Kelly 				 * Rule address Offset to align with RXP
833b34d8163SFrancis Kelly 				 * external instruction offset.
834b34d8163SFrancis Kelly 				 */
835b34d8163SFrancis Kelly 				rof_rule_addr = (rules->rules[rule_cnt].addr <<
836b34d8163SFrancis Kelly 						 MLX5_RXP_INST_OFFSET);
837b34d8163SFrancis Kelly 				/* 32 byte instruction swap (sw work around)! */
838b34d8163SFrancis Kelly 				tmp_write_swap[0] = le64toh(
839b34d8163SFrancis Kelly 					rules->rules[(rule_cnt + 4)].value);
840b34d8163SFrancis Kelly 				tmp_write_swap[1] = le64toh(
841b34d8163SFrancis Kelly 					rules->rules[(rule_cnt + 5)].value);
842b34d8163SFrancis Kelly 				tmp_write_swap[2] = le64toh(
843b34d8163SFrancis Kelly 					rules->rules[(rule_cnt + 6)].value);
844b34d8163SFrancis Kelly 				tmp_write_swap[3] = le64toh(
845b34d8163SFrancis Kelly 					rules->rules[(rule_cnt + 7)].value);
846b34d8163SFrancis Kelly 				/* Write only 4 of the 8 instructions. */
847b34d8163SFrancis Kelly 				memcpy((uint8_t *)((uint8_t *)
848b34d8163SFrancis Kelly 				       priv->db[db_to_program].ptr +
849b34d8163SFrancis Kelly 				       rof_rule_addr), &tmp_write_swap,
850b34d8163SFrancis Kelly 				       (sizeof(uint64_t) * 4));
851b34d8163SFrancis Kelly 				/* Write 1st 4 rules of block after last 4. */
852b34d8163SFrancis Kelly 				rof_rule_addr = (rules->rules[
853b34d8163SFrancis Kelly 						 (rule_cnt + 4)].addr <<
854b34d8163SFrancis Kelly 						 MLX5_RXP_INST_OFFSET);
855b34d8163SFrancis Kelly 				tmp_write_swap[0] = le64toh(
856b34d8163SFrancis Kelly 					rules->rules[(rule_cnt + 0)].value);
857b34d8163SFrancis Kelly 				tmp_write_swap[1] = le64toh(
858b34d8163SFrancis Kelly 					rules->rules[(rule_cnt + 1)].value);
859b34d8163SFrancis Kelly 				tmp_write_swap[2] = le64toh(
860b34d8163SFrancis Kelly 					rules->rules[(rule_cnt + 2)].value);
861b34d8163SFrancis Kelly 				tmp_write_swap[3] = le64toh(
862b34d8163SFrancis Kelly 					rules->rules[(rule_cnt + 3)].value);
863b34d8163SFrancis Kelly 				memcpy((uint8_t *)((uint8_t *)
864b34d8163SFrancis Kelly 				       priv->db[db_to_program].ptr +
865b34d8163SFrancis Kelly 				       rof_rule_addr), &tmp_write_swap,
866b34d8163SFrancis Kelly 				       (sizeof(uint64_t) * 4));
867b34d8163SFrancis Kelly 			} else
868b34d8163SFrancis Kelly 				return -1;
869b34d8163SFrancis Kelly 			/* Fast forward as already handled block of 8. */
870b34d8163SFrancis Kelly 			rule_cnt += MLX5_RXP_INST_BLOCK_SIZE;
871b34d8163SFrancis Kelly 		} else
872b34d8163SFrancis Kelly 			rule_cnt++; /* Must be something other than EM rule. */
873b34d8163SFrancis Kelly 	}
874b34d8163SFrancis Kelly 	return 0;
875b34d8163SFrancis Kelly }
876b34d8163SFrancis Kelly 
877b34d8163SFrancis Kelly static int
878b34d8163SFrancis Kelly rxp_db_setup(struct mlx5_regex_priv *priv)
879b34d8163SFrancis Kelly {
880b34d8163SFrancis Kelly 	int ret;
881b34d8163SFrancis Kelly 	uint8_t i;
882b34d8163SFrancis Kelly 
883b34d8163SFrancis Kelly 	/* Setup database memories for both RXP engines + reprogram memory. */
884b34d8163SFrancis Kelly 	for (i = 0; i < (priv->nb_engines + MLX5_RXP_EM_COUNT); i++) {
885b34d8163SFrancis Kelly 		priv->db[i].ptr = rte_malloc("", MLX5_MAX_DB_SIZE, 0);
886b34d8163SFrancis Kelly 		if (!priv->db[i].ptr) {
887b34d8163SFrancis Kelly 			DRV_LOG(ERR, "Failed to alloc db memory!");
888b34d8163SFrancis Kelly 			ret = ENODEV;
889b34d8163SFrancis Kelly 			goto tidyup_error;
890b34d8163SFrancis Kelly 		}
891b34d8163SFrancis Kelly 		/* Register the memory. */
892b34d8163SFrancis Kelly 		priv->db[i].umem.umem = mlx5_glue->devx_umem_reg(priv->ctx,
893b34d8163SFrancis Kelly 							priv->db[i].ptr,
894b34d8163SFrancis Kelly 							MLX5_MAX_DB_SIZE, 7);
895b34d8163SFrancis Kelly 		if (!priv->db[i].umem.umem) {
896b34d8163SFrancis Kelly 			DRV_LOG(ERR, "Failed to register memory!");
897b34d8163SFrancis Kelly 			ret = ENODEV;
898b34d8163SFrancis Kelly 			goto tidyup_error;
899b34d8163SFrancis Kelly 		}
900b34d8163SFrancis Kelly 		/* Ensure set all DB memory to 0's before setting up DB. */
901b34d8163SFrancis Kelly 		memset(priv->db[i].ptr, 0x00, MLX5_MAX_DB_SIZE);
902b34d8163SFrancis Kelly 		/* No data currently in database. */
903b34d8163SFrancis Kelly 		priv->db[i].len = 0;
904b34d8163SFrancis Kelly 		priv->db[i].active = false;
905b34d8163SFrancis Kelly 		priv->db[i].db_assigned_to_eng_num = MLX5_RXP_DB_NOT_ASSIGNED;
906b34d8163SFrancis Kelly 	}
907b34d8163SFrancis Kelly 	return 0;
908b34d8163SFrancis Kelly tidyup_error:
909b34d8163SFrancis Kelly 	for (i = 0; i < (priv->nb_engines + MLX5_RXP_EM_COUNT); i++) {
910b34d8163SFrancis Kelly 		if (priv->db[i].ptr)
911b34d8163SFrancis Kelly 			rte_free(priv->db[i].ptr);
912b34d8163SFrancis Kelly 		if (priv->db[i].umem.umem)
913b34d8163SFrancis Kelly 			mlx5_glue->devx_umem_dereg(priv->db[i].umem.umem);
914b34d8163SFrancis Kelly 	}
915b34d8163SFrancis Kelly 	return -ret;
916b34d8163SFrancis Kelly }
917b34d8163SFrancis Kelly 
918b34d8163SFrancis Kelly int
919b34d8163SFrancis Kelly mlx5_regex_rules_db_import(struct rte_regexdev *dev,
920b34d8163SFrancis Kelly 		     const char *rule_db, uint32_t rule_db_len)
921b34d8163SFrancis Kelly {
922b34d8163SFrancis Kelly 	struct mlx5_regex_priv *priv = dev->data->dev_private;
923b34d8163SFrancis Kelly 	struct mlx5_rxp_ctl_rules_pgm *rules = NULL;
924a07079b1SMichael Baum 	uint32_t id;
925b34d8163SFrancis Kelly 	int ret;
926b34d8163SFrancis Kelly 
927b34d8163SFrancis Kelly 	if (priv->prog_mode == MLX5_RXP_MODE_NOT_DEFINED) {
928b34d8163SFrancis Kelly 		DRV_LOG(ERR, "RXP programming mode not set!");
929b34d8163SFrancis Kelly 		return -1;
930b34d8163SFrancis Kelly 	}
931b34d8163SFrancis Kelly 	if (rule_db == NULL) {
932b34d8163SFrancis Kelly 		DRV_LOG(ERR, "Database empty!");
933b34d8163SFrancis Kelly 		return -ENODEV;
934b34d8163SFrancis Kelly 	}
935b34d8163SFrancis Kelly 	if (rule_db_len == 0)
936b34d8163SFrancis Kelly 		return -EINVAL;
937b34d8163SFrancis Kelly 	ret = rxp_parse_rof(rule_db, rule_db_len, &rules);
938b34d8163SFrancis Kelly 	if (ret) {
939b34d8163SFrancis Kelly 		DRV_LOG(ERR, "Can't parse ROF file.");
940b34d8163SFrancis Kelly 		return ret;
941b34d8163SFrancis Kelly 	}
942b34d8163SFrancis Kelly 	/* Need to ensure RXP not busy before stop! */
943b34d8163SFrancis Kelly 	for (id = 0; id < priv->nb_engines; id++) {
944b34d8163SFrancis Kelly 		ret = rxp_stop_engine(priv->ctx, id);
945b34d8163SFrancis Kelly 		if (ret) {
946b34d8163SFrancis Kelly 			DRV_LOG(ERR, "Can't stop engine.");
947b34d8163SFrancis Kelly 			ret = -ENODEV;
948b34d8163SFrancis Kelly 			goto tidyup_error;
949b34d8163SFrancis Kelly 		}
950b34d8163SFrancis Kelly 		ret = program_rxp_rules(priv, rules, id);
951b34d8163SFrancis Kelly 		if (ret < 0) {
952b34d8163SFrancis Kelly 			DRV_LOG(ERR, "Failed to program rxp rules.");
953b34d8163SFrancis Kelly 			ret = -ENODEV;
954b34d8163SFrancis Kelly 			goto tidyup_error;
955b34d8163SFrancis Kelly 		}
956b34d8163SFrancis Kelly 		ret = rxp_start_engine(priv->ctx, id);
957b34d8163SFrancis Kelly 		if (ret) {
958b34d8163SFrancis Kelly 			DRV_LOG(ERR, "Can't start engine.");
959b34d8163SFrancis Kelly 			ret = -ENODEV;
960b34d8163SFrancis Kelly 			goto tidyup_error;
961b34d8163SFrancis Kelly 		}
962b34d8163SFrancis Kelly 	}
963b34d8163SFrancis Kelly 	rte_free(rules);
964b34d8163SFrancis Kelly 	return 0;
965b34d8163SFrancis Kelly tidyup_error:
966b34d8163SFrancis Kelly 	rte_free(rules);
967b34d8163SFrancis Kelly 	return ret;
968b34d8163SFrancis Kelly }
969b34d8163SFrancis Kelly 
970e3dbbf71SOri Kam int
971e3dbbf71SOri Kam mlx5_regex_configure(struct rte_regexdev *dev,
972e3dbbf71SOri Kam 		     const struct rte_regexdev_config *cfg)
973e3dbbf71SOri Kam {
974e3dbbf71SOri Kam 	struct mlx5_regex_priv *priv = dev->data->dev_private;
975e3dbbf71SOri Kam 	int ret;
976e3dbbf71SOri Kam 
977b34d8163SFrancis Kelly 	if (priv->prog_mode == MLX5_RXP_MODE_NOT_DEFINED)
978b34d8163SFrancis Kelly 		return -1;
979e3dbbf71SOri Kam 	priv->nb_queues = cfg->nb_queue_pairs;
980b34d8163SFrancis Kelly 	dev->data->dev_conf.nb_queue_pairs = priv->nb_queues;
981e3dbbf71SOri Kam 	priv->qps = rte_zmalloc(NULL, sizeof(struct mlx5_regex_qp) *
982e3dbbf71SOri Kam 				priv->nb_queues, 0);
983e3dbbf71SOri Kam 	if (!priv->nb_queues) {
984e3dbbf71SOri Kam 		DRV_LOG(ERR, "can't allocate qps memory");
985e3dbbf71SOri Kam 		rte_errno = ENOMEM;
986e3dbbf71SOri Kam 		return -rte_errno;
987e3dbbf71SOri Kam 	}
988e3dbbf71SOri Kam 	priv->nb_max_matches = cfg->nb_max_matches;
989b34d8163SFrancis Kelly 	/* Setup rxp db memories. */
990b34d8163SFrancis Kelly 	if (rxp_db_setup(priv)) {
991b34d8163SFrancis Kelly 		DRV_LOG(ERR, "Failed to setup RXP db memory");
992b34d8163SFrancis Kelly 		rte_errno = ENOMEM;
993e3dbbf71SOri Kam 		return -rte_errno;
994e3dbbf71SOri Kam 	}
995b34d8163SFrancis Kelly 	if (cfg->rule_db != NULL) {
996b34d8163SFrancis Kelly 		ret = mlx5_regex_rules_db_import(dev, cfg->rule_db,
997b34d8163SFrancis Kelly 						 cfg->rule_db_len);
998b34d8163SFrancis Kelly 		if (ret < 0) {
999b34d8163SFrancis Kelly 			DRV_LOG(ERR, "Failed to program rxp rules.");
1000e3dbbf71SOri Kam 			rte_errno = ENODEV;
1001e3dbbf71SOri Kam 			goto configure_error;
1002e3dbbf71SOri Kam 		}
1003b34d8163SFrancis Kelly 	} else
1004b34d8163SFrancis Kelly 		DRV_LOG(DEBUG, "Regex config without rules programming!");
1005e3dbbf71SOri Kam 	return 0;
1006e3dbbf71SOri Kam configure_error:
1007e3dbbf71SOri Kam 	if (priv->qps)
1008e3dbbf71SOri Kam 		rte_free(priv->qps);
1009e3dbbf71SOri Kam 	return -rte_errno;
1010e3dbbf71SOri Kam }
1011