xref: /dpdk/drivers/regex/mlx5/mlx5_rxp.c (revision 383049f1797b1c01dfa0225ec669f976c7890ca8)
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>
11f324162eSOri Kam #include <sys/mman.h>
12c126512bSOri Kam 
13e3dbbf71SOri Kam #include <mlx5_glue.h>
14e3dbbf71SOri Kam #include <mlx5_devx_cmds.h>
15e3dbbf71SOri Kam #include <mlx5_prm.h>
16b34d8163SFrancis Kelly #include <mlx5_common_os.h>
17e3dbbf71SOri Kam 
18c126512bSOri Kam #include "mlx5_regex.h"
19e3dbbf71SOri Kam #include "mlx5_regex_utils.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 
27f324162eSOri Kam #define MLX5_REGEX_RXP_ROF2_LINE_LEN 34
28f324162eSOri Kam 
29ab746801SGerry Gribbon const uint64_t combined_rof_tag = 0xff52544424a52475;
30ab746801SGerry Gribbon 
31b34d8163SFrancis Kelly /* Private Declarations */
329fa82d28SAdy Agbarih static int
339fa82d28SAdy Agbarih rxp_create_mkey(struct mlx5_regex_priv *priv, void *ptr, size_t size,
349fa82d28SAdy Agbarih 	uint32_t access, struct mlx5_regex_mkey *mkey);
359fa82d28SAdy Agbarih static inline void
369fa82d28SAdy Agbarih rxp_destroy_mkey(struct mlx5_regex_mkey *mkey);
379fa82d28SAdy Agbarih 
38c126512bSOri Kam int
mlx5_regex_info_get(struct rte_regexdev * dev __rte_unused,struct rte_regexdev_info * info)39c126512bSOri Kam mlx5_regex_info_get(struct rte_regexdev *dev __rte_unused,
40c126512bSOri Kam 		    struct rte_regexdev_info *info)
41c126512bSOri Kam {
42c126512bSOri Kam 	info->max_matches = MLX5_REGEX_MAX_MATCHES;
43c126512bSOri Kam 	info->max_payload_size = MLX5_REGEX_MAX_PAYLOAD_SIZE;
44c126512bSOri Kam 	info->max_rules_per_group = MLX5_REGEX_MAX_RULES_PER_GROUP;
45c126512bSOri Kam 	info->max_groups = MLX5_REGEX_MAX_GROUPS;
46c8452005SOphir Munk 	info->regexdev_capa = RTE_REGEXDEV_SUPP_PCRE_GREEDY_F |
47c8452005SOphir Munk 			      RTE_REGEXDEV_CAPA_QUEUE_PAIR_OOS_F;
48c126512bSOri Kam 	info->rule_flags = 0;
497a7a9907SOri Kam 	info->max_queue_pairs = UINT16_MAX;
5070f1ea71SGerry Gribbon 	info->max_segs = mlx5_regexdev_max_segs_get();
51c126512bSOri Kam 	return 0;
52c126512bSOri Kam }
53e3dbbf71SOri Kam 
54e3dbbf71SOri Kam static int
rxp_create_mkey(struct mlx5_regex_priv * priv,void * ptr,size_t size,uint32_t access,struct mlx5_regex_mkey * mkey)559fa82d28SAdy Agbarih rxp_create_mkey(struct mlx5_regex_priv *priv, void *ptr, size_t size,
569fa82d28SAdy Agbarih 	uint32_t access, struct mlx5_regex_mkey *mkey)
57b34d8163SFrancis Kelly {
589fa82d28SAdy Agbarih 	struct mlx5_devx_mkey_attr mkey_attr;
59b34d8163SFrancis Kelly 
60b34d8163SFrancis Kelly 	/* Register the memory. */
619fa82d28SAdy Agbarih 	mkey->umem = mlx5_glue->devx_umem_reg(priv->cdev->ctx, ptr, size, access);
629fa82d28SAdy Agbarih 	if (!mkey->umem) {
63b34d8163SFrancis Kelly 		DRV_LOG(ERR, "Failed to register memory!");
649fa82d28SAdy Agbarih 		return -ENODEV;
65b34d8163SFrancis Kelly 	}
669fa82d28SAdy Agbarih 	/* Create mkey */
679fa82d28SAdy Agbarih 	mkey_attr = (struct mlx5_devx_mkey_attr) {
689fa82d28SAdy Agbarih 		.addr = (uintptr_t)ptr,
699fa82d28SAdy Agbarih 		.size = (uint32_t)size,
709fa82d28SAdy Agbarih 		.umem_id = mlx5_os_get_umem_id(mkey->umem),
719fa82d28SAdy Agbarih 		.pg_access = 1,
729fa82d28SAdy Agbarih 		.umr_en = 0,
739fa82d28SAdy Agbarih 	};
749fa82d28SAdy Agbarih #ifdef HAVE_IBV_FLOW_DV_SUPPORT
759fa82d28SAdy Agbarih 	mkey_attr.pd = priv->cdev->pdn;
769fa82d28SAdy Agbarih #endif
779fa82d28SAdy Agbarih 	mkey->mkey = mlx5_devx_cmd_mkey_create(priv->cdev->ctx, &mkey_attr);
789fa82d28SAdy Agbarih 	if (!mkey->mkey) {
799fa82d28SAdy Agbarih 		DRV_LOG(ERR, "Failed to create direct mkey!");
809fa82d28SAdy Agbarih 		return -ENODEV;
81b34d8163SFrancis Kelly 	}
82b34d8163SFrancis Kelly 	return 0;
83b34d8163SFrancis Kelly }
849fa82d28SAdy Agbarih 
859fa82d28SAdy Agbarih static inline void
rxp_destroy_mkey(struct mlx5_regex_mkey * mkey)869fa82d28SAdy Agbarih rxp_destroy_mkey(struct mlx5_regex_mkey *mkey)
879fa82d28SAdy Agbarih {
889fa82d28SAdy Agbarih 	if (mkey->mkey)
899fa82d28SAdy Agbarih 		claim_zero(mlx5_devx_cmd_destroy(mkey->mkey));
909fa82d28SAdy Agbarih 	if (mkey->umem)
919fa82d28SAdy Agbarih 		claim_zero(mlx5_glue->devx_umem_dereg(mkey->umem));
92b34d8163SFrancis Kelly }
93b34d8163SFrancis Kelly 
94b34d8163SFrancis Kelly int
mlx5_regex_get_rxp_vers(uint32_t regexp_version,uint32_t * target_rxp_vers)95ab746801SGerry Gribbon mlx5_regex_get_rxp_vers(uint32_t regexp_version, uint32_t *target_rxp_vers)
96ab746801SGerry Gribbon {
97ab746801SGerry Gribbon 	int ret = 0;
98ab746801SGerry Gribbon 	switch (regexp_version) {
99ab746801SGerry Gribbon 	case MLX5_RXP_BF2_IDENTIFIER:
100ab746801SGerry Gribbon 		*target_rxp_vers = MLX5_RXP_BF2_ROF_VERSION_STRING;
101ab746801SGerry Gribbon 		break;
102ab746801SGerry Gribbon 	case MLX5_RXP_BF3_IDENTIFIER:
103ab746801SGerry Gribbon 		*target_rxp_vers = MLX5_RXP_BF3_ROF_VERSION_STRING;
104ab746801SGerry Gribbon 		break;
105ab746801SGerry Gribbon 	default:
106ab746801SGerry Gribbon 		DRV_LOG(ERR, "Unsupported rxp version: %u", regexp_version);
107ab746801SGerry Gribbon 		ret = -EINVAL;
108ab746801SGerry Gribbon 		break;
109ab746801SGerry Gribbon 	}
110ab746801SGerry Gribbon 	return ret;
111ab746801SGerry Gribbon }
112ab746801SGerry Gribbon 
113ab746801SGerry Gribbon int
mlx5_regex_check_rof_version(uint32_t combined_rof_vers)114ab746801SGerry Gribbon mlx5_regex_check_rof_version(uint32_t combined_rof_vers)
115ab746801SGerry Gribbon {
116ab746801SGerry Gribbon 	int ret = 0;
117ab746801SGerry Gribbon 	/* Check if combined rof version is supported */
118ab746801SGerry Gribbon 	switch (combined_rof_vers) {
119ab746801SGerry Gribbon 	case 1:
120ab746801SGerry Gribbon 		break;
121ab746801SGerry Gribbon 	default:
122ab746801SGerry Gribbon 		DRV_LOG(ERR, "Unsupported combined rof version: %u",
123ab746801SGerry Gribbon 		combined_rof_vers);
124ab746801SGerry Gribbon 		ret = -EINVAL;
125ab746801SGerry Gribbon 		break;
126ab746801SGerry Gribbon 	}
127ab746801SGerry Gribbon 	return ret;
128ab746801SGerry Gribbon }
129ab746801SGerry Gribbon 
130ab746801SGerry Gribbon int
mlx5_regex_parse_rules_db(struct mlx5_regex_priv * priv,const char ** rules_db,uint32_t * rules_db_len)131ab746801SGerry Gribbon mlx5_regex_parse_rules_db(struct mlx5_regex_priv *priv,
132ab746801SGerry Gribbon 			const char **rules_db, uint32_t *rules_db_len)
133ab746801SGerry Gribbon {
134ab746801SGerry Gribbon 	int i = 0;
135ab746801SGerry Gribbon 	uint32_t j = 0;
136ab746801SGerry Gribbon 	int ret = 0;
137ab746801SGerry Gribbon 	bool combined_rof = true;
138ab746801SGerry Gribbon 	const char *rof_ptr = *rules_db;
139ab746801SGerry Gribbon 	uint32_t combined_rof_vers = 0;
140ab746801SGerry Gribbon 	uint32_t num_rof_blocks = 0;
141ab746801SGerry Gribbon 	uint32_t rxpc_vers = 0;
142ab746801SGerry Gribbon 	uint32_t target_rxp_vers = 0;
143ab746801SGerry Gribbon 	uint32_t byte_count = 0;
144ab746801SGerry Gribbon 	uint32_t rof_bytes_read = 0;
145ab746801SGerry Gribbon 	bool rof_binary_found = false;
146ab746801SGerry Gribbon 	struct mlx5_hca_attr *attr = &priv->cdev->config.hca_attr;
147ab746801SGerry Gribbon 
148ab746801SGerry Gribbon 	/* Need minimum of 8 bytes to process single or combined rof */
149ab746801SGerry Gribbon 	if (*rules_db_len < 8)
150ab746801SGerry Gribbon 		return -EINVAL;
151ab746801SGerry Gribbon 
152ab746801SGerry Gribbon 	for (i = 0; i < 8; i++) {
153ab746801SGerry Gribbon 		if ((char) *rof_ptr !=
154ab746801SGerry Gribbon 			(char)((combined_rof_tag >> (i * 8)) & 0xFF)) {
155ab746801SGerry Gribbon 			combined_rof = false;
156ab746801SGerry Gribbon 			break;
157ab746801SGerry Gribbon 		}
158ab746801SGerry Gribbon 		rof_ptr++;
159ab746801SGerry Gribbon 	}
160ab746801SGerry Gribbon 	rof_bytes_read += 8;
161ab746801SGerry Gribbon 
162ab746801SGerry Gribbon 	if (combined_rof == true) {
163ab746801SGerry Gribbon 		/* Need at least 24 bytes of header info: 16 byte combined */
164ab746801SGerry Gribbon 		/* rof header and 8 byte binary rof blob header.           */
165ab746801SGerry Gribbon 		if (*rules_db_len < 24)
166ab746801SGerry Gribbon 			return -EINVAL;
167ab746801SGerry Gribbon 
168ab746801SGerry Gribbon 		/* Read the combined rof version and number of rof blocks */
169ab746801SGerry Gribbon 		for (i = 0; i < 4; i++) {
170ab746801SGerry Gribbon 			combined_rof_vers |= *rof_ptr << (i * 8);
171ab746801SGerry Gribbon 			rof_ptr++;
172ab746801SGerry Gribbon 		}
173ab746801SGerry Gribbon 
174ab746801SGerry Gribbon 		rof_bytes_read += 4;
175ab746801SGerry Gribbon 		ret = mlx5_regex_check_rof_version(combined_rof_vers);
176ab746801SGerry Gribbon 		if (ret < 0)
177ab746801SGerry Gribbon 			return ret;
178ab746801SGerry Gribbon 
179ab746801SGerry Gribbon 		for (i = 0; i < 4; i++) {
180ab746801SGerry Gribbon 			num_rof_blocks |= *rof_ptr << (i * 8);
181ab746801SGerry Gribbon 			rof_ptr++;
182ab746801SGerry Gribbon 		}
183ab746801SGerry Gribbon 		rof_bytes_read += 4;
184ab746801SGerry Gribbon 
185ab746801SGerry Gribbon 		if (num_rof_blocks == 0)
186ab746801SGerry Gribbon 			return -EINVAL;
187ab746801SGerry Gribbon 
188ab746801SGerry Gribbon 		/* Get the version of rxp we need the rof for */
189ab746801SGerry Gribbon 		ret = mlx5_regex_get_rxp_vers(attr->regexp_version, &target_rxp_vers);
190ab746801SGerry Gribbon 		if (ret < 0)
191ab746801SGerry Gribbon 			return ret;
192ab746801SGerry Gribbon 
193ab746801SGerry Gribbon 		/* Try to find the rof binary blob for this version of rxp */
194ab746801SGerry Gribbon 		for (j = 0; j < num_rof_blocks; j++) {
195ab746801SGerry Gribbon 			rxpc_vers = 0;
196ab746801SGerry Gribbon 			byte_count = 0;
197ab746801SGerry Gribbon 			for (i = 0; i < 4; i++) {
198ab746801SGerry Gribbon 				rxpc_vers |= (*rof_ptr & 0xFF) << (i * 8);
199ab746801SGerry Gribbon 				rof_ptr++;
200ab746801SGerry Gribbon 			}
201ab746801SGerry Gribbon 			for (i = 0; i < 4; i++) {
202ab746801SGerry Gribbon 				byte_count |= (*rof_ptr & 0xFF) << (i * 8);
203ab746801SGerry Gribbon 				rof_ptr++;
204ab746801SGerry Gribbon 			}
205ab746801SGerry Gribbon 			rof_bytes_read += 8;
206ab746801SGerry Gribbon 
207ab746801SGerry Gribbon 			if (rxpc_vers == target_rxp_vers) {
208ab746801SGerry Gribbon 				/* Found corresponding binary rof entry */
209ab746801SGerry Gribbon 				if (rof_bytes_read + byte_count <= (*rules_db_len))
210ab746801SGerry Gribbon 					rof_binary_found = true;
211ab746801SGerry Gribbon 				else
212ab746801SGerry Gribbon 					DRV_LOG(ERR, "Compatible rof file found - invalid length!");
213ab746801SGerry Gribbon 				break;
214ab746801SGerry Gribbon 			}
215ab746801SGerry Gribbon 				/* Move on to next rof blob */
216ab746801SGerry Gribbon 			if (rof_bytes_read + byte_count + 8 < (*rules_db_len)) {
217ab746801SGerry Gribbon 				rof_ptr += byte_count;
218ab746801SGerry Gribbon 				rof_bytes_read += byte_count;
219ab746801SGerry Gribbon 			} else {
220ab746801SGerry Gribbon 				/* Cannot parse any more of combined rof file */
221ab746801SGerry Gribbon 				break;
222ab746801SGerry Gribbon 			}
223ab746801SGerry Gribbon 		}
224ab746801SGerry Gribbon 		if (rof_binary_found == true) {
225ab746801SGerry Gribbon 			*rules_db = rof_ptr;
226ab746801SGerry Gribbon 			*rules_db_len = byte_count;
227ab746801SGerry Gribbon 		} else {
228ab746801SGerry Gribbon 			DRV_LOG(ERR, "Compatible rof file not found!");
229ab746801SGerry Gribbon 			return -EINVAL;
230ab746801SGerry Gribbon 		}
231ab746801SGerry Gribbon 	}
232ab746801SGerry Gribbon 	return 0;
233ab746801SGerry Gribbon }
234ab746801SGerry Gribbon 
235ab746801SGerry Gribbon int
mlx5_regex_rules_db_import(struct rte_regexdev * dev,const char * rule_db,uint32_t rule_db_len)236b34d8163SFrancis Kelly mlx5_regex_rules_db_import(struct rte_regexdev *dev,
237b34d8163SFrancis Kelly 		     const char *rule_db, uint32_t rule_db_len)
238b34d8163SFrancis Kelly {
239b34d8163SFrancis Kelly 	struct mlx5_regex_priv *priv = dev->data->dev_private;
2409fa82d28SAdy Agbarih 	struct mlx5_regex_mkey mkey;
2419fa82d28SAdy Agbarih 	uint32_t id;
2429fa82d28SAdy Agbarih 	int ret;
2439fa82d28SAdy Agbarih 	void *ptr;
244b34d8163SFrancis Kelly 
245b34d8163SFrancis Kelly 	if (priv->prog_mode == MLX5_RXP_MODE_NOT_DEFINED) {
246b34d8163SFrancis Kelly 		DRV_LOG(ERR, "RXP programming mode not set!");
247b34d8163SFrancis Kelly 		return -1;
248b34d8163SFrancis Kelly 	}
249b34d8163SFrancis Kelly 	if (rule_db == NULL) {
250b34d8163SFrancis Kelly 		DRV_LOG(ERR, "Database empty!");
251b34d8163SFrancis Kelly 		return -ENODEV;
252b34d8163SFrancis Kelly 	}
253b34d8163SFrancis Kelly 	if (rule_db_len == 0)
254b34d8163SFrancis Kelly 		return -EINVAL;
255ab746801SGerry Gribbon 
256ab746801SGerry Gribbon 	ret = mlx5_regex_parse_rules_db(priv, &rule_db, &rule_db_len);
257ab746801SGerry Gribbon 	if (ret < 0)
258ab746801SGerry Gribbon 		return ret;
259ab746801SGerry Gribbon 
2609fa82d28SAdy Agbarih 	/* copy rules - rules have to be 4KB aligned. */
2619fa82d28SAdy Agbarih 	ptr = rte_malloc("", rule_db_len, 1 << 12);
2629fa82d28SAdy Agbarih 	if (!ptr) {
2639fa82d28SAdy Agbarih 		DRV_LOG(ERR, "Failed to allocate rules file memory.");
2649fa82d28SAdy Agbarih 		return -ENOMEM;
2659fa82d28SAdy Agbarih 	}
2669fa82d28SAdy Agbarih 	rte_memcpy(ptr, rule_db, rule_db_len);
2679fa82d28SAdy Agbarih 	/* Register umem and create rof mkey. */
2689fa82d28SAdy Agbarih 	ret = rxp_create_mkey(priv, ptr, rule_db_len, /*access=*/7, &mkey);
2699fa82d28SAdy Agbarih 	if (ret < 0)
2709fa82d28SAdy Agbarih 		return ret;
271ab2e0b0dSAdy Agbarih 
2729fa82d28SAdy Agbarih 	for (id = 0; id < priv->nb_engines; id++) {
2739fa82d28SAdy Agbarih 		ret = mlx5_devx_regex_rules_program(priv->cdev->ctx, id,
2749fa82d28SAdy Agbarih 			mkey.mkey->id, rule_db_len, (uintptr_t)ptr);
2759fa82d28SAdy Agbarih 		if (ret < 0) {
2769fa82d28SAdy Agbarih 			DRV_LOG(ERR, "Failed to program rxp rules.");
2779fa82d28SAdy Agbarih 			ret = -ENODEV;
2789fa82d28SAdy Agbarih 			break;
2799fa82d28SAdy Agbarih 		}
2809fa82d28SAdy Agbarih 		ret = 0;
2819fa82d28SAdy Agbarih 	}
2829fa82d28SAdy Agbarih 	rxp_destroy_mkey(&mkey);
2839fa82d28SAdy Agbarih 	rte_free(ptr);
2849fa82d28SAdy Agbarih 	return ret;
285b34d8163SFrancis Kelly }
286b34d8163SFrancis Kelly 
287e3dbbf71SOri Kam int
mlx5_regex_configure(struct rte_regexdev * dev,const struct rte_regexdev_config * cfg)288e3dbbf71SOri Kam mlx5_regex_configure(struct rte_regexdev *dev,
289e3dbbf71SOri Kam 		     const struct rte_regexdev_config *cfg)
290e3dbbf71SOri Kam {
291e3dbbf71SOri Kam 	struct mlx5_regex_priv *priv = dev->data->dev_private;
292e3dbbf71SOri Kam 	int ret;
293e3dbbf71SOri Kam 
294b34d8163SFrancis Kelly 	if (priv->prog_mode == MLX5_RXP_MODE_NOT_DEFINED)
295b34d8163SFrancis Kelly 		return -1;
296*383049f1SGerry Gribbon 	if (cfg->nb_max_matches != MLX5_REGEX_MAX_MATCHES) {
297*383049f1SGerry Gribbon 		DRV_LOG(ERR, "nb_max_matches is not configurable.");
298*383049f1SGerry Gribbon 		rte_errno = EINVAL;
299*383049f1SGerry Gribbon 		return -rte_errno;
300*383049f1SGerry Gribbon 	}
301e3dbbf71SOri Kam 	priv->nb_queues = cfg->nb_queue_pairs;
302b34d8163SFrancis Kelly 	dev->data->dev_conf.nb_queue_pairs = priv->nb_queues;
303e3dbbf71SOri Kam 	priv->qps = rte_zmalloc(NULL, sizeof(struct mlx5_regex_qp) *
304e3dbbf71SOri Kam 				priv->nb_queues, 0);
30560e9028aSWeiguo Li 	if (!priv->qps) {
306e3dbbf71SOri Kam 		DRV_LOG(ERR, "can't allocate qps memory");
307e3dbbf71SOri Kam 		rte_errno = ENOMEM;
308e3dbbf71SOri Kam 		return -rte_errno;
309e3dbbf71SOri Kam 	}
310e3dbbf71SOri Kam 	priv->nb_max_matches = cfg->nb_max_matches;
311b34d8163SFrancis Kelly 	if (cfg->rule_db != NULL) {
312b34d8163SFrancis Kelly 		ret = mlx5_regex_rules_db_import(dev, cfg->rule_db,
313b34d8163SFrancis Kelly 						 cfg->rule_db_len);
314b34d8163SFrancis Kelly 		if (ret < 0) {
315b34d8163SFrancis Kelly 			DRV_LOG(ERR, "Failed to program rxp rules.");
316e3dbbf71SOri Kam 			rte_errno = ENODEV;
317e3dbbf71SOri Kam 			goto configure_error;
318e3dbbf71SOri Kam 		}
319b34d8163SFrancis Kelly 	} else
320b34d8163SFrancis Kelly 		DRV_LOG(DEBUG, "Regex config without rules programming!");
321e3dbbf71SOri Kam 	return 0;
322e3dbbf71SOri Kam configure_error:
323e3dbbf71SOri Kam 	rte_free(priv->qps);
324e3dbbf71SOri Kam 	return -rte_errno;
325e3dbbf71SOri Kam }
326