1*99a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
2*99a2dd95SBruce Richardson * Copyright(C) 2019 Marvell International Ltd.
3*99a2dd95SBruce Richardson * Copyright 2020 Mellanox Technologies, Ltd
4*99a2dd95SBruce Richardson */
5*99a2dd95SBruce Richardson
6*99a2dd95SBruce Richardson #include <string.h>
7*99a2dd95SBruce Richardson
8*99a2dd95SBruce Richardson #include <rte_memzone.h>
9*99a2dd95SBruce Richardson #include <rte_string_fns.h>
10*99a2dd95SBruce Richardson
11*99a2dd95SBruce Richardson #include "rte_regexdev.h"
12*99a2dd95SBruce Richardson #include "rte_regexdev_core.h"
13*99a2dd95SBruce Richardson #include "rte_regexdev_driver.h"
14*99a2dd95SBruce Richardson
15*99a2dd95SBruce Richardson static const char *MZ_RTE_REGEXDEV_DATA = "rte_regexdev_data";
16*99a2dd95SBruce Richardson struct rte_regexdev rte_regex_devices[RTE_MAX_REGEXDEV_DEVS];
17*99a2dd95SBruce Richardson /* Shared memory between primary and secondary processes. */
18*99a2dd95SBruce Richardson static struct {
19*99a2dd95SBruce Richardson struct rte_regexdev_data data[RTE_MAX_REGEXDEV_DEVS];
20*99a2dd95SBruce Richardson } *rte_regexdev_shared_data;
21*99a2dd95SBruce Richardson
22*99a2dd95SBruce Richardson int rte_regexdev_logtype;
23*99a2dd95SBruce Richardson
24*99a2dd95SBruce Richardson static uint16_t
regexdev_find_free_dev(void)25*99a2dd95SBruce Richardson regexdev_find_free_dev(void)
26*99a2dd95SBruce Richardson {
27*99a2dd95SBruce Richardson uint16_t i;
28*99a2dd95SBruce Richardson
29*99a2dd95SBruce Richardson for (i = 0; i < RTE_MAX_REGEXDEV_DEVS; i++) {
30*99a2dd95SBruce Richardson if (rte_regex_devices[i].state == RTE_REGEXDEV_UNUSED)
31*99a2dd95SBruce Richardson return i;
32*99a2dd95SBruce Richardson }
33*99a2dd95SBruce Richardson return RTE_MAX_REGEXDEV_DEVS;
34*99a2dd95SBruce Richardson }
35*99a2dd95SBruce Richardson
36*99a2dd95SBruce Richardson static struct rte_regexdev*
regexdev_allocated(const char * name)37*99a2dd95SBruce Richardson regexdev_allocated(const char *name)
38*99a2dd95SBruce Richardson {
39*99a2dd95SBruce Richardson uint16_t i;
40*99a2dd95SBruce Richardson
41*99a2dd95SBruce Richardson for (i = 0; i < RTE_MAX_REGEXDEV_DEVS; i++) {
42*99a2dd95SBruce Richardson if (rte_regex_devices[i].state != RTE_REGEXDEV_UNUSED)
43*99a2dd95SBruce Richardson if (!strcmp(name, rte_regex_devices[i].data->dev_name))
44*99a2dd95SBruce Richardson return &rte_regex_devices[i];
45*99a2dd95SBruce Richardson }
46*99a2dd95SBruce Richardson return NULL;
47*99a2dd95SBruce Richardson }
48*99a2dd95SBruce Richardson
49*99a2dd95SBruce Richardson static int
regexdev_shared_data_prepare(void)50*99a2dd95SBruce Richardson regexdev_shared_data_prepare(void)
51*99a2dd95SBruce Richardson {
52*99a2dd95SBruce Richardson const unsigned int flags = 0;
53*99a2dd95SBruce Richardson const struct rte_memzone *mz;
54*99a2dd95SBruce Richardson
55*99a2dd95SBruce Richardson if (rte_regexdev_shared_data == NULL) {
56*99a2dd95SBruce Richardson /* Allocate port data and ownership shared memory. */
57*99a2dd95SBruce Richardson mz = rte_memzone_reserve(MZ_RTE_REGEXDEV_DATA,
58*99a2dd95SBruce Richardson sizeof(*rte_regexdev_shared_data),
59*99a2dd95SBruce Richardson rte_socket_id(), flags);
60*99a2dd95SBruce Richardson if (mz == NULL)
61*99a2dd95SBruce Richardson return -ENOMEM;
62*99a2dd95SBruce Richardson
63*99a2dd95SBruce Richardson rte_regexdev_shared_data = mz->addr;
64*99a2dd95SBruce Richardson memset(rte_regexdev_shared_data->data, 0,
65*99a2dd95SBruce Richardson sizeof(rte_regexdev_shared_data->data));
66*99a2dd95SBruce Richardson }
67*99a2dd95SBruce Richardson return 0;
68*99a2dd95SBruce Richardson }
69*99a2dd95SBruce Richardson
70*99a2dd95SBruce Richardson static int
regexdev_check_name(const char * name)71*99a2dd95SBruce Richardson regexdev_check_name(const char *name)
72*99a2dd95SBruce Richardson {
73*99a2dd95SBruce Richardson size_t name_len;
74*99a2dd95SBruce Richardson
75*99a2dd95SBruce Richardson if (name == NULL) {
76*99a2dd95SBruce Richardson RTE_REGEXDEV_LOG(ERR, "Name can't be NULL\n");
77*99a2dd95SBruce Richardson return -EINVAL;
78*99a2dd95SBruce Richardson }
79*99a2dd95SBruce Richardson name_len = strnlen(name, RTE_REGEXDEV_NAME_MAX_LEN);
80*99a2dd95SBruce Richardson if (name_len == 0) {
81*99a2dd95SBruce Richardson RTE_REGEXDEV_LOG(ERR, "Zero length RegEx device name\n");
82*99a2dd95SBruce Richardson return -EINVAL;
83*99a2dd95SBruce Richardson }
84*99a2dd95SBruce Richardson if (name_len >= RTE_REGEXDEV_NAME_MAX_LEN) {
85*99a2dd95SBruce Richardson RTE_REGEXDEV_LOG(ERR, "RegEx device name is too long\n");
86*99a2dd95SBruce Richardson return -EINVAL;
87*99a2dd95SBruce Richardson }
88*99a2dd95SBruce Richardson return (int)name_len;
89*99a2dd95SBruce Richardson
90*99a2dd95SBruce Richardson }
91*99a2dd95SBruce Richardson
92*99a2dd95SBruce Richardson struct rte_regexdev *
rte_regexdev_register(const char * name)93*99a2dd95SBruce Richardson rte_regexdev_register(const char *name)
94*99a2dd95SBruce Richardson {
95*99a2dd95SBruce Richardson uint16_t dev_id;
96*99a2dd95SBruce Richardson int name_len;
97*99a2dd95SBruce Richardson struct rte_regexdev *dev;
98*99a2dd95SBruce Richardson
99*99a2dd95SBruce Richardson name_len = regexdev_check_name(name);
100*99a2dd95SBruce Richardson if (name_len < 0)
101*99a2dd95SBruce Richardson return NULL;
102*99a2dd95SBruce Richardson dev = regexdev_allocated(name);
103*99a2dd95SBruce Richardson if (dev != NULL) {
104*99a2dd95SBruce Richardson RTE_REGEXDEV_LOG(ERR, "RegEx device already allocated\n");
105*99a2dd95SBruce Richardson return NULL;
106*99a2dd95SBruce Richardson }
107*99a2dd95SBruce Richardson dev_id = regexdev_find_free_dev();
108*99a2dd95SBruce Richardson if (dev_id == RTE_MAX_REGEXDEV_DEVS) {
109*99a2dd95SBruce Richardson RTE_REGEXDEV_LOG
110*99a2dd95SBruce Richardson (ERR, "Reached maximum number of RegEx devices\n");
111*99a2dd95SBruce Richardson return NULL;
112*99a2dd95SBruce Richardson }
113*99a2dd95SBruce Richardson if (regexdev_shared_data_prepare() < 0) {
114*99a2dd95SBruce Richardson RTE_REGEXDEV_LOG(ERR, "Cannot allocate RegEx shared data\n");
115*99a2dd95SBruce Richardson return NULL;
116*99a2dd95SBruce Richardson }
117*99a2dd95SBruce Richardson
118*99a2dd95SBruce Richardson dev = &rte_regex_devices[dev_id];
119*99a2dd95SBruce Richardson dev->state = RTE_REGEXDEV_REGISTERED;
120*99a2dd95SBruce Richardson if (dev->data == NULL)
121*99a2dd95SBruce Richardson dev->data = &rte_regexdev_shared_data->data[dev_id];
122*99a2dd95SBruce Richardson else
123*99a2dd95SBruce Richardson memset(dev->data, 1, sizeof(*dev->data));
124*99a2dd95SBruce Richardson dev->data->dev_id = dev_id;
125*99a2dd95SBruce Richardson strlcpy(dev->data->dev_name, name, sizeof(dev->data->dev_name));
126*99a2dd95SBruce Richardson return dev;
127*99a2dd95SBruce Richardson }
128*99a2dd95SBruce Richardson
129*99a2dd95SBruce Richardson void
rte_regexdev_unregister(struct rte_regexdev * dev)130*99a2dd95SBruce Richardson rte_regexdev_unregister(struct rte_regexdev *dev)
131*99a2dd95SBruce Richardson {
132*99a2dd95SBruce Richardson dev->state = RTE_REGEXDEV_UNUSED;
133*99a2dd95SBruce Richardson }
134*99a2dd95SBruce Richardson
135*99a2dd95SBruce Richardson struct rte_regexdev *
rte_regexdev_get_device_by_name(const char * name)136*99a2dd95SBruce Richardson rte_regexdev_get_device_by_name(const char *name)
137*99a2dd95SBruce Richardson {
138*99a2dd95SBruce Richardson if (regexdev_check_name(name) < 0)
139*99a2dd95SBruce Richardson return NULL;
140*99a2dd95SBruce Richardson return regexdev_allocated(name);
141*99a2dd95SBruce Richardson }
142*99a2dd95SBruce Richardson
143*99a2dd95SBruce Richardson uint8_t
rte_regexdev_count(void)144*99a2dd95SBruce Richardson rte_regexdev_count(void)
145*99a2dd95SBruce Richardson {
146*99a2dd95SBruce Richardson int i;
147*99a2dd95SBruce Richardson int count = 0;
148*99a2dd95SBruce Richardson
149*99a2dd95SBruce Richardson for (i = 0; i < RTE_MAX_REGEXDEV_DEVS; i++) {
150*99a2dd95SBruce Richardson if (rte_regex_devices[i].state != RTE_REGEXDEV_UNUSED)
151*99a2dd95SBruce Richardson count++;
152*99a2dd95SBruce Richardson }
153*99a2dd95SBruce Richardson return count;
154*99a2dd95SBruce Richardson }
155*99a2dd95SBruce Richardson
156*99a2dd95SBruce Richardson int
rte_regexdev_get_dev_id(const char * name)157*99a2dd95SBruce Richardson rte_regexdev_get_dev_id(const char *name)
158*99a2dd95SBruce Richardson {
159*99a2dd95SBruce Richardson int i;
160*99a2dd95SBruce Richardson int id = -EINVAL;
161*99a2dd95SBruce Richardson
162*99a2dd95SBruce Richardson if (name == NULL)
163*99a2dd95SBruce Richardson return -EINVAL;
164*99a2dd95SBruce Richardson for (i = 0; i < RTE_MAX_REGEXDEV_DEVS; i++) {
165*99a2dd95SBruce Richardson if (rte_regex_devices[i].state != RTE_REGEXDEV_UNUSED)
166*99a2dd95SBruce Richardson if (strcmp(name, rte_regex_devices[i].data->dev_name)) {
167*99a2dd95SBruce Richardson id = rte_regex_devices[i].data->dev_id;
168*99a2dd95SBruce Richardson break;
169*99a2dd95SBruce Richardson }
170*99a2dd95SBruce Richardson }
171*99a2dd95SBruce Richardson return id;
172*99a2dd95SBruce Richardson }
173*99a2dd95SBruce Richardson
174*99a2dd95SBruce Richardson int
rte_regexdev_is_valid_dev(uint16_t dev_id)175*99a2dd95SBruce Richardson rte_regexdev_is_valid_dev(uint16_t dev_id)
176*99a2dd95SBruce Richardson {
177*99a2dd95SBruce Richardson if (dev_id >= RTE_MAX_REGEXDEV_DEVS ||
178*99a2dd95SBruce Richardson rte_regex_devices[dev_id].state != RTE_REGEXDEV_READY)
179*99a2dd95SBruce Richardson return 0;
180*99a2dd95SBruce Richardson return 1;
181*99a2dd95SBruce Richardson }
182*99a2dd95SBruce Richardson
183*99a2dd95SBruce Richardson static int
regexdev_info_get(uint8_t dev_id,struct rte_regexdev_info * dev_info)184*99a2dd95SBruce Richardson regexdev_info_get(uint8_t dev_id, struct rte_regexdev_info *dev_info)
185*99a2dd95SBruce Richardson {
186*99a2dd95SBruce Richardson struct rte_regexdev *dev;
187*99a2dd95SBruce Richardson
188*99a2dd95SBruce Richardson RTE_REGEXDEV_VALID_DEV_ID_OR_ERR_RET(dev_id, -EINVAL);
189*99a2dd95SBruce Richardson if (dev_info == NULL)
190*99a2dd95SBruce Richardson return -EINVAL;
191*99a2dd95SBruce Richardson dev = &rte_regex_devices[dev_id];
192*99a2dd95SBruce Richardson RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_info_get, -ENOTSUP);
193*99a2dd95SBruce Richardson return (*dev->dev_ops->dev_info_get)(dev, dev_info);
194*99a2dd95SBruce Richardson
195*99a2dd95SBruce Richardson }
196*99a2dd95SBruce Richardson
197*99a2dd95SBruce Richardson int
rte_regexdev_info_get(uint8_t dev_id,struct rte_regexdev_info * dev_info)198*99a2dd95SBruce Richardson rte_regexdev_info_get(uint8_t dev_id, struct rte_regexdev_info *dev_info)
199*99a2dd95SBruce Richardson {
200*99a2dd95SBruce Richardson return regexdev_info_get(dev_id, dev_info);
201*99a2dd95SBruce Richardson }
202*99a2dd95SBruce Richardson
203*99a2dd95SBruce Richardson int
rte_regexdev_configure(uint8_t dev_id,const struct rte_regexdev_config * cfg)204*99a2dd95SBruce Richardson rte_regexdev_configure(uint8_t dev_id, const struct rte_regexdev_config *cfg)
205*99a2dd95SBruce Richardson {
206*99a2dd95SBruce Richardson struct rte_regexdev *dev;
207*99a2dd95SBruce Richardson struct rte_regexdev_info dev_info;
208*99a2dd95SBruce Richardson int ret;
209*99a2dd95SBruce Richardson
210*99a2dd95SBruce Richardson RTE_REGEXDEV_VALID_DEV_ID_OR_ERR_RET(dev_id, -EINVAL);
211*99a2dd95SBruce Richardson if (cfg == NULL)
212*99a2dd95SBruce Richardson return -EINVAL;
213*99a2dd95SBruce Richardson dev = &rte_regex_devices[dev_id];
214*99a2dd95SBruce Richardson RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_configure, -ENOTSUP);
215*99a2dd95SBruce Richardson if (dev->data->dev_started) {
216*99a2dd95SBruce Richardson RTE_REGEXDEV_LOG
217*99a2dd95SBruce Richardson (ERR, "Dev %u must be stopped to allow configuration\n",
218*99a2dd95SBruce Richardson dev_id);
219*99a2dd95SBruce Richardson return -EBUSY;
220*99a2dd95SBruce Richardson }
221*99a2dd95SBruce Richardson ret = regexdev_info_get(dev_id, &dev_info);
222*99a2dd95SBruce Richardson if (ret < 0)
223*99a2dd95SBruce Richardson return ret;
224*99a2dd95SBruce Richardson if ((cfg->dev_cfg_flags & RTE_REGEXDEV_CFG_CROSS_BUFFER_SCAN_F) &&
225*99a2dd95SBruce Richardson !(dev_info.regexdev_capa & RTE_REGEXDEV_SUPP_CROSS_BUFFER_F)) {
226*99a2dd95SBruce Richardson RTE_REGEXDEV_LOG(ERR,
227*99a2dd95SBruce Richardson "Dev %u doesn't support cross buffer scan\n",
228*99a2dd95SBruce Richardson dev_id);
229*99a2dd95SBruce Richardson return -EINVAL;
230*99a2dd95SBruce Richardson }
231*99a2dd95SBruce Richardson if ((cfg->dev_cfg_flags & RTE_REGEXDEV_CFG_MATCH_AS_END_F) &&
232*99a2dd95SBruce Richardson !(dev_info.regexdev_capa & RTE_REGEXDEV_SUPP_MATCH_AS_END_F)) {
233*99a2dd95SBruce Richardson RTE_REGEXDEV_LOG(ERR,
234*99a2dd95SBruce Richardson "Dev %u doesn't support match as end\n",
235*99a2dd95SBruce Richardson dev_id);
236*99a2dd95SBruce Richardson return -EINVAL;
237*99a2dd95SBruce Richardson }
238*99a2dd95SBruce Richardson if ((cfg->dev_cfg_flags & RTE_REGEXDEV_CFG_MATCH_ALL_F) &&
239*99a2dd95SBruce Richardson !(dev_info.regexdev_capa & RTE_REGEXDEV_SUPP_MATCH_ALL_F)) {
240*99a2dd95SBruce Richardson RTE_REGEXDEV_LOG(ERR,
241*99a2dd95SBruce Richardson "Dev %u doesn't support match all\n",
242*99a2dd95SBruce Richardson dev_id);
243*99a2dd95SBruce Richardson return -EINVAL;
244*99a2dd95SBruce Richardson }
245*99a2dd95SBruce Richardson if (cfg->nb_groups == 0) {
246*99a2dd95SBruce Richardson RTE_REGEXDEV_LOG(ERR, "Dev %u num of groups must be > 0\n",
247*99a2dd95SBruce Richardson dev_id);
248*99a2dd95SBruce Richardson return -EINVAL;
249*99a2dd95SBruce Richardson }
250*99a2dd95SBruce Richardson if (cfg->nb_groups > dev_info.max_groups) {
251*99a2dd95SBruce Richardson RTE_REGEXDEV_LOG(ERR, "Dev %u num of groups %d > %d\n",
252*99a2dd95SBruce Richardson dev_id, cfg->nb_groups, dev_info.max_groups);
253*99a2dd95SBruce Richardson return -EINVAL;
254*99a2dd95SBruce Richardson }
255*99a2dd95SBruce Richardson if (cfg->nb_max_matches == 0) {
256*99a2dd95SBruce Richardson RTE_REGEXDEV_LOG(ERR, "Dev %u num of matches must be > 0\n",
257*99a2dd95SBruce Richardson dev_id);
258*99a2dd95SBruce Richardson return -EINVAL;
259*99a2dd95SBruce Richardson }
260*99a2dd95SBruce Richardson if (cfg->nb_max_matches > dev_info.max_matches) {
261*99a2dd95SBruce Richardson RTE_REGEXDEV_LOG(ERR, "Dev %u num of matches %d > %d\n",
262*99a2dd95SBruce Richardson dev_id, cfg->nb_max_matches,
263*99a2dd95SBruce Richardson dev_info.max_matches);
264*99a2dd95SBruce Richardson return -EINVAL;
265*99a2dd95SBruce Richardson }
266*99a2dd95SBruce Richardson if (cfg->nb_queue_pairs == 0) {
267*99a2dd95SBruce Richardson RTE_REGEXDEV_LOG(ERR, "Dev %u num of queues must be > 0\n",
268*99a2dd95SBruce Richardson dev_id);
269*99a2dd95SBruce Richardson return -EINVAL;
270*99a2dd95SBruce Richardson }
271*99a2dd95SBruce Richardson if (cfg->nb_queue_pairs > dev_info.max_queue_pairs) {
272*99a2dd95SBruce Richardson RTE_REGEXDEV_LOG(ERR, "Dev %u num of queues %d > %d\n",
273*99a2dd95SBruce Richardson dev_id, cfg->nb_queue_pairs,
274*99a2dd95SBruce Richardson dev_info.max_queue_pairs);
275*99a2dd95SBruce Richardson return -EINVAL;
276*99a2dd95SBruce Richardson }
277*99a2dd95SBruce Richardson if (cfg->nb_rules_per_group == 0) {
278*99a2dd95SBruce Richardson RTE_REGEXDEV_LOG(ERR,
279*99a2dd95SBruce Richardson "Dev %u num of rules per group must be > 0\n",
280*99a2dd95SBruce Richardson dev_id);
281*99a2dd95SBruce Richardson return -EINVAL;
282*99a2dd95SBruce Richardson }
283*99a2dd95SBruce Richardson if (cfg->nb_rules_per_group > dev_info.max_rules_per_group) {
284*99a2dd95SBruce Richardson RTE_REGEXDEV_LOG(ERR,
285*99a2dd95SBruce Richardson "Dev %u num of rules per group %d > %d\n",
286*99a2dd95SBruce Richardson dev_id, cfg->nb_rules_per_group,
287*99a2dd95SBruce Richardson dev_info.max_rules_per_group);
288*99a2dd95SBruce Richardson return -EINVAL;
289*99a2dd95SBruce Richardson }
290*99a2dd95SBruce Richardson ret = (*dev->dev_ops->dev_configure)(dev, cfg);
291*99a2dd95SBruce Richardson if (ret == 0)
292*99a2dd95SBruce Richardson dev->data->dev_conf = *cfg;
293*99a2dd95SBruce Richardson return ret;
294*99a2dd95SBruce Richardson }
295*99a2dd95SBruce Richardson
296*99a2dd95SBruce Richardson int
rte_regexdev_queue_pair_setup(uint8_t dev_id,uint16_t queue_pair_id,const struct rte_regexdev_qp_conf * qp_conf)297*99a2dd95SBruce Richardson rte_regexdev_queue_pair_setup(uint8_t dev_id, uint16_t queue_pair_id,
298*99a2dd95SBruce Richardson const struct rte_regexdev_qp_conf *qp_conf)
299*99a2dd95SBruce Richardson {
300*99a2dd95SBruce Richardson struct rte_regexdev *dev;
301*99a2dd95SBruce Richardson
302*99a2dd95SBruce Richardson RTE_REGEXDEV_VALID_DEV_ID_OR_ERR_RET(dev_id, -EINVAL);
303*99a2dd95SBruce Richardson dev = &rte_regex_devices[dev_id];
304*99a2dd95SBruce Richardson RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_qp_setup, -ENOTSUP);
305*99a2dd95SBruce Richardson if (dev->data->dev_started) {
306*99a2dd95SBruce Richardson RTE_REGEXDEV_LOG
307*99a2dd95SBruce Richardson (ERR, "Dev %u must be stopped to allow configuration\n",
308*99a2dd95SBruce Richardson dev_id);
309*99a2dd95SBruce Richardson return -EBUSY;
310*99a2dd95SBruce Richardson }
311*99a2dd95SBruce Richardson if (queue_pair_id >= dev->data->dev_conf.nb_queue_pairs) {
312*99a2dd95SBruce Richardson RTE_REGEXDEV_LOG(ERR,
313*99a2dd95SBruce Richardson "Dev %u invalid queue %d > %d\n",
314*99a2dd95SBruce Richardson dev_id, queue_pair_id,
315*99a2dd95SBruce Richardson dev->data->dev_conf.nb_queue_pairs);
316*99a2dd95SBruce Richardson return -EINVAL;
317*99a2dd95SBruce Richardson }
318*99a2dd95SBruce Richardson if (dev->data->dev_started) {
319*99a2dd95SBruce Richardson RTE_REGEXDEV_LOG
320*99a2dd95SBruce Richardson (ERR, "Dev %u must be stopped to allow configuration\n",
321*99a2dd95SBruce Richardson dev_id);
322*99a2dd95SBruce Richardson return -EBUSY;
323*99a2dd95SBruce Richardson }
324*99a2dd95SBruce Richardson return (*dev->dev_ops->dev_qp_setup)(dev, queue_pair_id, qp_conf);
325*99a2dd95SBruce Richardson }
326*99a2dd95SBruce Richardson
327*99a2dd95SBruce Richardson int
rte_regexdev_start(uint8_t dev_id)328*99a2dd95SBruce Richardson rte_regexdev_start(uint8_t dev_id)
329*99a2dd95SBruce Richardson {
330*99a2dd95SBruce Richardson struct rte_regexdev *dev;
331*99a2dd95SBruce Richardson int ret;
332*99a2dd95SBruce Richardson
333*99a2dd95SBruce Richardson RTE_REGEXDEV_VALID_DEV_ID_OR_ERR_RET(dev_id, -EINVAL);
334*99a2dd95SBruce Richardson dev = &rte_regex_devices[dev_id];
335*99a2dd95SBruce Richardson RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_start, -ENOTSUP);
336*99a2dd95SBruce Richardson ret = (*dev->dev_ops->dev_start)(dev);
337*99a2dd95SBruce Richardson if (ret == 0)
338*99a2dd95SBruce Richardson dev->data->dev_started = 1;
339*99a2dd95SBruce Richardson return ret;
340*99a2dd95SBruce Richardson }
341*99a2dd95SBruce Richardson
342*99a2dd95SBruce Richardson int
rte_regexdev_stop(uint8_t dev_id)343*99a2dd95SBruce Richardson rte_regexdev_stop(uint8_t dev_id)
344*99a2dd95SBruce Richardson {
345*99a2dd95SBruce Richardson struct rte_regexdev *dev;
346*99a2dd95SBruce Richardson
347*99a2dd95SBruce Richardson RTE_REGEXDEV_VALID_DEV_ID_OR_ERR_RET(dev_id, -EINVAL);
348*99a2dd95SBruce Richardson dev = &rte_regex_devices[dev_id];
349*99a2dd95SBruce Richardson RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_stop, -ENOTSUP);
350*99a2dd95SBruce Richardson (*dev->dev_ops->dev_stop)(dev);
351*99a2dd95SBruce Richardson dev->data->dev_started = 0;
352*99a2dd95SBruce Richardson return 0;
353*99a2dd95SBruce Richardson }
354*99a2dd95SBruce Richardson
355*99a2dd95SBruce Richardson int
rte_regexdev_close(uint8_t dev_id)356*99a2dd95SBruce Richardson rte_regexdev_close(uint8_t dev_id)
357*99a2dd95SBruce Richardson {
358*99a2dd95SBruce Richardson struct rte_regexdev *dev;
359*99a2dd95SBruce Richardson
360*99a2dd95SBruce Richardson RTE_REGEXDEV_VALID_DEV_ID_OR_ERR_RET(dev_id, -EINVAL);
361*99a2dd95SBruce Richardson dev = &rte_regex_devices[dev_id];
362*99a2dd95SBruce Richardson RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_close, -ENOTSUP);
363*99a2dd95SBruce Richardson (*dev->dev_ops->dev_close)(dev);
364*99a2dd95SBruce Richardson dev->data->dev_started = 0;
365*99a2dd95SBruce Richardson dev->state = RTE_REGEXDEV_UNUSED;
366*99a2dd95SBruce Richardson return 0;
367*99a2dd95SBruce Richardson }
368*99a2dd95SBruce Richardson
369*99a2dd95SBruce Richardson int
rte_regexdev_attr_get(uint8_t dev_id,enum rte_regexdev_attr_id attr_id,void * attr_value)370*99a2dd95SBruce Richardson rte_regexdev_attr_get(uint8_t dev_id, enum rte_regexdev_attr_id attr_id,
371*99a2dd95SBruce Richardson void *attr_value)
372*99a2dd95SBruce Richardson {
373*99a2dd95SBruce Richardson struct rte_regexdev *dev;
374*99a2dd95SBruce Richardson
375*99a2dd95SBruce Richardson RTE_REGEXDEV_VALID_DEV_ID_OR_ERR_RET(dev_id, -EINVAL);
376*99a2dd95SBruce Richardson dev = &rte_regex_devices[dev_id];
377*99a2dd95SBruce Richardson RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_attr_get, -ENOTSUP);
378*99a2dd95SBruce Richardson if (attr_value == NULL) {
379*99a2dd95SBruce Richardson RTE_REGEXDEV_LOG(ERR, "Dev %d attribute value can't be NULL\n",
380*99a2dd95SBruce Richardson dev_id);
381*99a2dd95SBruce Richardson return -EINVAL;
382*99a2dd95SBruce Richardson }
383*99a2dd95SBruce Richardson return (*dev->dev_ops->dev_attr_get)(dev, attr_id, attr_value);
384*99a2dd95SBruce Richardson }
385*99a2dd95SBruce Richardson
386*99a2dd95SBruce Richardson int
rte_regexdev_attr_set(uint8_t dev_id,enum rte_regexdev_attr_id attr_id,const void * attr_value)387*99a2dd95SBruce Richardson rte_regexdev_attr_set(uint8_t dev_id, enum rte_regexdev_attr_id attr_id,
388*99a2dd95SBruce Richardson const void *attr_value)
389*99a2dd95SBruce Richardson {
390*99a2dd95SBruce Richardson struct rte_regexdev *dev;
391*99a2dd95SBruce Richardson
392*99a2dd95SBruce Richardson RTE_REGEXDEV_VALID_DEV_ID_OR_ERR_RET(dev_id, -EINVAL);
393*99a2dd95SBruce Richardson dev = &rte_regex_devices[dev_id];
394*99a2dd95SBruce Richardson RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_attr_set, -ENOTSUP);
395*99a2dd95SBruce Richardson if (attr_value == NULL) {
396*99a2dd95SBruce Richardson RTE_REGEXDEV_LOG(ERR, "Dev %d attribute value can't be NULL\n",
397*99a2dd95SBruce Richardson dev_id);
398*99a2dd95SBruce Richardson return -EINVAL;
399*99a2dd95SBruce Richardson }
400*99a2dd95SBruce Richardson return (*dev->dev_ops->dev_attr_set)(dev, attr_id, attr_value);
401*99a2dd95SBruce Richardson }
402*99a2dd95SBruce Richardson
403*99a2dd95SBruce Richardson int
rte_regexdev_rule_db_update(uint8_t dev_id,const struct rte_regexdev_rule * rules,uint32_t nb_rules)404*99a2dd95SBruce Richardson rte_regexdev_rule_db_update(uint8_t dev_id,
405*99a2dd95SBruce Richardson const struct rte_regexdev_rule *rules,
406*99a2dd95SBruce Richardson uint32_t nb_rules)
407*99a2dd95SBruce Richardson {
408*99a2dd95SBruce Richardson struct rte_regexdev *dev;
409*99a2dd95SBruce Richardson
410*99a2dd95SBruce Richardson RTE_REGEXDEV_VALID_DEV_ID_OR_ERR_RET(dev_id, -EINVAL);
411*99a2dd95SBruce Richardson dev = &rte_regex_devices[dev_id];
412*99a2dd95SBruce Richardson RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_rule_db_update, -ENOTSUP);
413*99a2dd95SBruce Richardson if (rules == NULL) {
414*99a2dd95SBruce Richardson RTE_REGEXDEV_LOG(ERR, "Dev %d rules can't be NULL\n",
415*99a2dd95SBruce Richardson dev_id);
416*99a2dd95SBruce Richardson return -EINVAL;
417*99a2dd95SBruce Richardson }
418*99a2dd95SBruce Richardson return (*dev->dev_ops->dev_rule_db_update)(dev, rules, nb_rules);
419*99a2dd95SBruce Richardson }
420*99a2dd95SBruce Richardson
421*99a2dd95SBruce Richardson int
rte_regexdev_rule_db_compile_activate(uint8_t dev_id)422*99a2dd95SBruce Richardson rte_regexdev_rule_db_compile_activate(uint8_t dev_id)
423*99a2dd95SBruce Richardson {
424*99a2dd95SBruce Richardson struct rte_regexdev *dev;
425*99a2dd95SBruce Richardson
426*99a2dd95SBruce Richardson RTE_REGEXDEV_VALID_DEV_ID_OR_ERR_RET(dev_id, -EINVAL);
427*99a2dd95SBruce Richardson dev = &rte_regex_devices[dev_id];
428*99a2dd95SBruce Richardson RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_rule_db_compile_activate,
429*99a2dd95SBruce Richardson -ENOTSUP);
430*99a2dd95SBruce Richardson return (*dev->dev_ops->dev_rule_db_compile_activate)(dev);
431*99a2dd95SBruce Richardson }
432*99a2dd95SBruce Richardson
433*99a2dd95SBruce Richardson int
rte_regexdev_rule_db_import(uint8_t dev_id,const char * rule_db,uint32_t rule_db_len)434*99a2dd95SBruce Richardson rte_regexdev_rule_db_import(uint8_t dev_id, const char *rule_db,
435*99a2dd95SBruce Richardson uint32_t rule_db_len)
436*99a2dd95SBruce Richardson {
437*99a2dd95SBruce Richardson struct rte_regexdev *dev;
438*99a2dd95SBruce Richardson
439*99a2dd95SBruce Richardson RTE_REGEXDEV_VALID_DEV_ID_OR_ERR_RET(dev_id, -EINVAL);
440*99a2dd95SBruce Richardson dev = &rte_regex_devices[dev_id];
441*99a2dd95SBruce Richardson RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_db_import,
442*99a2dd95SBruce Richardson -ENOTSUP);
443*99a2dd95SBruce Richardson if (rule_db == NULL) {
444*99a2dd95SBruce Richardson RTE_REGEXDEV_LOG(ERR, "Dev %d rules can't be NULL\n",
445*99a2dd95SBruce Richardson dev_id);
446*99a2dd95SBruce Richardson return -EINVAL;
447*99a2dd95SBruce Richardson }
448*99a2dd95SBruce Richardson return (*dev->dev_ops->dev_db_import)(dev, rule_db, rule_db_len);
449*99a2dd95SBruce Richardson }
450*99a2dd95SBruce Richardson
451*99a2dd95SBruce Richardson int
rte_regexdev_rule_db_export(uint8_t dev_id,char * rule_db)452*99a2dd95SBruce Richardson rte_regexdev_rule_db_export(uint8_t dev_id, char *rule_db)
453*99a2dd95SBruce Richardson {
454*99a2dd95SBruce Richardson struct rte_regexdev *dev;
455*99a2dd95SBruce Richardson
456*99a2dd95SBruce Richardson RTE_REGEXDEV_VALID_DEV_ID_OR_ERR_RET(dev_id, -EINVAL);
457*99a2dd95SBruce Richardson dev = &rte_regex_devices[dev_id];
458*99a2dd95SBruce Richardson RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_db_export,
459*99a2dd95SBruce Richardson -ENOTSUP);
460*99a2dd95SBruce Richardson return (*dev->dev_ops->dev_db_export)(dev, rule_db);
461*99a2dd95SBruce Richardson }
462*99a2dd95SBruce Richardson
463*99a2dd95SBruce Richardson int
rte_regexdev_xstats_names_get(uint8_t dev_id,struct rte_regexdev_xstats_map * xstats_map)464*99a2dd95SBruce Richardson rte_regexdev_xstats_names_get(uint8_t dev_id,
465*99a2dd95SBruce Richardson struct rte_regexdev_xstats_map *xstats_map)
466*99a2dd95SBruce Richardson {
467*99a2dd95SBruce Richardson struct rte_regexdev *dev;
468*99a2dd95SBruce Richardson
469*99a2dd95SBruce Richardson RTE_REGEXDEV_VALID_DEV_ID_OR_ERR_RET(dev_id, -EINVAL);
470*99a2dd95SBruce Richardson dev = &rte_regex_devices[dev_id];
471*99a2dd95SBruce Richardson RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_xstats_names_get,
472*99a2dd95SBruce Richardson -ENOTSUP);
473*99a2dd95SBruce Richardson if (xstats_map == NULL) {
474*99a2dd95SBruce Richardson RTE_REGEXDEV_LOG(ERR, "Dev %d xstats map can't be NULL\n",
475*99a2dd95SBruce Richardson dev_id);
476*99a2dd95SBruce Richardson return -EINVAL;
477*99a2dd95SBruce Richardson }
478*99a2dd95SBruce Richardson return (*dev->dev_ops->dev_xstats_names_get)(dev, xstats_map);
479*99a2dd95SBruce Richardson }
480*99a2dd95SBruce Richardson
481*99a2dd95SBruce Richardson int
rte_regexdev_xstats_get(uint8_t dev_id,const uint16_t * ids,uint64_t * values,uint16_t n)482*99a2dd95SBruce Richardson rte_regexdev_xstats_get(uint8_t dev_id, const uint16_t *ids,
483*99a2dd95SBruce Richardson uint64_t *values, uint16_t n)
484*99a2dd95SBruce Richardson {
485*99a2dd95SBruce Richardson struct rte_regexdev *dev;
486*99a2dd95SBruce Richardson
487*99a2dd95SBruce Richardson RTE_REGEXDEV_VALID_DEV_ID_OR_ERR_RET(dev_id, -EINVAL);
488*99a2dd95SBruce Richardson dev = &rte_regex_devices[dev_id];
489*99a2dd95SBruce Richardson RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_xstats_get, -ENOTSUP);
490*99a2dd95SBruce Richardson if (ids == NULL) {
491*99a2dd95SBruce Richardson RTE_REGEXDEV_LOG(ERR, "Dev %d ids can't be NULL\n", dev_id);
492*99a2dd95SBruce Richardson return -EINVAL;
493*99a2dd95SBruce Richardson }
494*99a2dd95SBruce Richardson if (values == NULL) {
495*99a2dd95SBruce Richardson RTE_REGEXDEV_LOG(ERR, "Dev %d values can't be NULL\n", dev_id);
496*99a2dd95SBruce Richardson return -EINVAL;
497*99a2dd95SBruce Richardson }
498*99a2dd95SBruce Richardson return (*dev->dev_ops->dev_xstats_get)(dev, ids, values, n);
499*99a2dd95SBruce Richardson }
500*99a2dd95SBruce Richardson
501*99a2dd95SBruce Richardson int
rte_regexdev_xstats_by_name_get(uint8_t dev_id,const char * name,uint16_t * id,uint64_t * value)502*99a2dd95SBruce Richardson rte_regexdev_xstats_by_name_get(uint8_t dev_id, const char *name,
503*99a2dd95SBruce Richardson uint16_t *id, uint64_t *value)
504*99a2dd95SBruce Richardson {
505*99a2dd95SBruce Richardson struct rte_regexdev *dev;
506*99a2dd95SBruce Richardson
507*99a2dd95SBruce Richardson RTE_REGEXDEV_VALID_DEV_ID_OR_ERR_RET(dev_id, -EINVAL);
508*99a2dd95SBruce Richardson dev = &rte_regex_devices[dev_id];
509*99a2dd95SBruce Richardson RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_xstats_by_name_get,
510*99a2dd95SBruce Richardson -ENOTSUP);
511*99a2dd95SBruce Richardson if (name == NULL) {
512*99a2dd95SBruce Richardson RTE_REGEXDEV_LOG(ERR, "Dev %d name can't be NULL\n", dev_id);
513*99a2dd95SBruce Richardson return -EINVAL;
514*99a2dd95SBruce Richardson }
515*99a2dd95SBruce Richardson if (id == NULL) {
516*99a2dd95SBruce Richardson RTE_REGEXDEV_LOG(ERR, "Dev %d id can't be NULL\n", dev_id);
517*99a2dd95SBruce Richardson return -EINVAL;
518*99a2dd95SBruce Richardson }
519*99a2dd95SBruce Richardson if (value == NULL) {
520*99a2dd95SBruce Richardson RTE_REGEXDEV_LOG(ERR, "Dev %d value can't be NULL\n", dev_id);
521*99a2dd95SBruce Richardson return -EINVAL;
522*99a2dd95SBruce Richardson }
523*99a2dd95SBruce Richardson return (*dev->dev_ops->dev_xstats_by_name_get)(dev, name, id, value);
524*99a2dd95SBruce Richardson }
525*99a2dd95SBruce Richardson
526*99a2dd95SBruce Richardson int
rte_regexdev_xstats_reset(uint8_t dev_id,const uint16_t * ids,uint16_t nb_ids)527*99a2dd95SBruce Richardson rte_regexdev_xstats_reset(uint8_t dev_id, const uint16_t *ids,
528*99a2dd95SBruce Richardson uint16_t nb_ids)
529*99a2dd95SBruce Richardson {
530*99a2dd95SBruce Richardson struct rte_regexdev *dev;
531*99a2dd95SBruce Richardson
532*99a2dd95SBruce Richardson RTE_REGEXDEV_VALID_DEV_ID_OR_ERR_RET(dev_id, -EINVAL);
533*99a2dd95SBruce Richardson dev = &rte_regex_devices[dev_id];
534*99a2dd95SBruce Richardson RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_xstats_reset, -ENOTSUP);
535*99a2dd95SBruce Richardson if (ids == NULL) {
536*99a2dd95SBruce Richardson RTE_REGEXDEV_LOG(ERR, "Dev %d ids can't be NULL\n", dev_id);
537*99a2dd95SBruce Richardson return -EINVAL;
538*99a2dd95SBruce Richardson }
539*99a2dd95SBruce Richardson return (*dev->dev_ops->dev_xstats_reset)(dev, ids, nb_ids);
540*99a2dd95SBruce Richardson }
541*99a2dd95SBruce Richardson
542*99a2dd95SBruce Richardson int
rte_regexdev_selftest(uint8_t dev_id)543*99a2dd95SBruce Richardson rte_regexdev_selftest(uint8_t dev_id)
544*99a2dd95SBruce Richardson {
545*99a2dd95SBruce Richardson struct rte_regexdev *dev;
546*99a2dd95SBruce Richardson
547*99a2dd95SBruce Richardson RTE_REGEXDEV_VALID_DEV_ID_OR_ERR_RET(dev_id, -EINVAL);
548*99a2dd95SBruce Richardson dev = &rte_regex_devices[dev_id];
549*99a2dd95SBruce Richardson RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_selftest, -ENOTSUP);
550*99a2dd95SBruce Richardson return (*dev->dev_ops->dev_selftest)(dev);
551*99a2dd95SBruce Richardson }
552*99a2dd95SBruce Richardson
553*99a2dd95SBruce Richardson int
rte_regexdev_dump(uint8_t dev_id,FILE * f)554*99a2dd95SBruce Richardson rte_regexdev_dump(uint8_t dev_id, FILE *f)
555*99a2dd95SBruce Richardson {
556*99a2dd95SBruce Richardson struct rte_regexdev *dev;
557*99a2dd95SBruce Richardson
558*99a2dd95SBruce Richardson RTE_REGEXDEV_VALID_DEV_ID_OR_ERR_RET(dev_id, -EINVAL);
559*99a2dd95SBruce Richardson dev = &rte_regex_devices[dev_id];
560*99a2dd95SBruce Richardson RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->dev_dump, -ENOTSUP);
561*99a2dd95SBruce Richardson if (f == NULL) {
562*99a2dd95SBruce Richardson RTE_REGEXDEV_LOG(ERR, "Dev %d file can't be NULL\n", dev_id);
563*99a2dd95SBruce Richardson return -EINVAL;
564*99a2dd95SBruce Richardson }
565*99a2dd95SBruce Richardson return (*dev->dev_ops->dev_dump)(dev, f);
566*99a2dd95SBruce Richardson }
567