199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause 299a2dd95SBruce Richardson * Copyright(C) 2019 Marvell International Ltd. 399a2dd95SBruce Richardson * Copyright 2020 Mellanox Technologies, Ltd 499a2dd95SBruce Richardson */ 599a2dd95SBruce Richardson 699a2dd95SBruce Richardson #include <string.h> 799a2dd95SBruce Richardson 899a2dd95SBruce Richardson #include <rte_memzone.h> 999a2dd95SBruce Richardson #include <rte_string_fns.h> 1099a2dd95SBruce Richardson 1199a2dd95SBruce Richardson #include "rte_regexdev.h" 1299a2dd95SBruce Richardson #include "rte_regexdev_core.h" 1399a2dd95SBruce Richardson #include "rte_regexdev_driver.h" 1499a2dd95SBruce Richardson 1599a2dd95SBruce Richardson static const char *MZ_RTE_REGEXDEV_DATA = "rte_regexdev_data"; 1699a2dd95SBruce Richardson struct rte_regexdev rte_regex_devices[RTE_MAX_REGEXDEV_DEVS]; 1799a2dd95SBruce Richardson /* Shared memory between primary and secondary processes. */ 1899a2dd95SBruce Richardson static struct { 1999a2dd95SBruce Richardson struct rte_regexdev_data data[RTE_MAX_REGEXDEV_DEVS]; 2099a2dd95SBruce Richardson } *rte_regexdev_shared_data; 2199a2dd95SBruce Richardson 22*6351bdebSDavid Marchand RTE_LOG_REGISTER_DEFAULT(rte_regexdev_logtype, INFO); 2399a2dd95SBruce Richardson 2499a2dd95SBruce Richardson static uint16_t 2599a2dd95SBruce Richardson regexdev_find_free_dev(void) 2699a2dd95SBruce Richardson { 2799a2dd95SBruce Richardson uint16_t i; 2899a2dd95SBruce Richardson 2999a2dd95SBruce Richardson for (i = 0; i < RTE_MAX_REGEXDEV_DEVS; i++) { 3099a2dd95SBruce Richardson if (rte_regex_devices[i].state == RTE_REGEXDEV_UNUSED) 3199a2dd95SBruce Richardson return i; 3299a2dd95SBruce Richardson } 3399a2dd95SBruce Richardson return RTE_MAX_REGEXDEV_DEVS; 3499a2dd95SBruce Richardson } 3599a2dd95SBruce Richardson 3699a2dd95SBruce Richardson static struct rte_regexdev* 3799a2dd95SBruce Richardson regexdev_allocated(const char *name) 3899a2dd95SBruce Richardson { 3999a2dd95SBruce Richardson uint16_t i; 4099a2dd95SBruce Richardson 4199a2dd95SBruce Richardson for (i = 0; i < RTE_MAX_REGEXDEV_DEVS; i++) { 4299a2dd95SBruce Richardson if (rte_regex_devices[i].state != RTE_REGEXDEV_UNUSED) 4399a2dd95SBruce Richardson if (!strcmp(name, rte_regex_devices[i].data->dev_name)) 4499a2dd95SBruce Richardson return &rte_regex_devices[i]; 4599a2dd95SBruce Richardson } 4699a2dd95SBruce Richardson return NULL; 4799a2dd95SBruce Richardson } 4899a2dd95SBruce Richardson 4999a2dd95SBruce Richardson static int 5099a2dd95SBruce Richardson regexdev_shared_data_prepare(void) 5199a2dd95SBruce Richardson { 5299a2dd95SBruce Richardson const unsigned int flags = 0; 5399a2dd95SBruce Richardson const struct rte_memzone *mz; 5499a2dd95SBruce Richardson 5599a2dd95SBruce Richardson if (rte_regexdev_shared_data == NULL) { 5699a2dd95SBruce Richardson /* Allocate port data and ownership shared memory. */ 5799a2dd95SBruce Richardson mz = rte_memzone_reserve(MZ_RTE_REGEXDEV_DATA, 5899a2dd95SBruce Richardson sizeof(*rte_regexdev_shared_data), 5999a2dd95SBruce Richardson rte_socket_id(), flags); 6099a2dd95SBruce Richardson if (mz == NULL) 6199a2dd95SBruce Richardson return -ENOMEM; 6299a2dd95SBruce Richardson 6399a2dd95SBruce Richardson rte_regexdev_shared_data = mz->addr; 6499a2dd95SBruce Richardson memset(rte_regexdev_shared_data->data, 0, 6599a2dd95SBruce Richardson sizeof(rte_regexdev_shared_data->data)); 6699a2dd95SBruce Richardson } 6799a2dd95SBruce Richardson return 0; 6899a2dd95SBruce Richardson } 6999a2dd95SBruce Richardson 7099a2dd95SBruce Richardson static int 7199a2dd95SBruce Richardson regexdev_check_name(const char *name) 7299a2dd95SBruce Richardson { 7399a2dd95SBruce Richardson size_t name_len; 7499a2dd95SBruce Richardson 7599a2dd95SBruce Richardson if (name == NULL) { 7699a2dd95SBruce Richardson RTE_REGEXDEV_LOG(ERR, "Name can't be NULL\n"); 7799a2dd95SBruce Richardson return -EINVAL; 7899a2dd95SBruce Richardson } 7999a2dd95SBruce Richardson name_len = strnlen(name, RTE_REGEXDEV_NAME_MAX_LEN); 8099a2dd95SBruce Richardson if (name_len == 0) { 8199a2dd95SBruce Richardson RTE_REGEXDEV_LOG(ERR, "Zero length RegEx device name\n"); 8299a2dd95SBruce Richardson return -EINVAL; 8399a2dd95SBruce Richardson } 8499a2dd95SBruce Richardson if (name_len >= RTE_REGEXDEV_NAME_MAX_LEN) { 8599a2dd95SBruce Richardson RTE_REGEXDEV_LOG(ERR, "RegEx device name is too long\n"); 8699a2dd95SBruce Richardson return -EINVAL; 8799a2dd95SBruce Richardson } 8899a2dd95SBruce Richardson return (int)name_len; 8999a2dd95SBruce Richardson 9099a2dd95SBruce Richardson } 9199a2dd95SBruce Richardson 9299a2dd95SBruce Richardson struct rte_regexdev * 9399a2dd95SBruce Richardson rte_regexdev_register(const char *name) 9499a2dd95SBruce Richardson { 9599a2dd95SBruce Richardson uint16_t dev_id; 9699a2dd95SBruce Richardson int name_len; 9799a2dd95SBruce Richardson struct rte_regexdev *dev; 9899a2dd95SBruce Richardson 9999a2dd95SBruce Richardson name_len = regexdev_check_name(name); 10099a2dd95SBruce Richardson if (name_len < 0) 10199a2dd95SBruce Richardson return NULL; 10299a2dd95SBruce Richardson dev = regexdev_allocated(name); 10399a2dd95SBruce Richardson if (dev != NULL) { 10499a2dd95SBruce Richardson RTE_REGEXDEV_LOG(ERR, "RegEx device already allocated\n"); 10599a2dd95SBruce Richardson return NULL; 10699a2dd95SBruce Richardson } 10799a2dd95SBruce Richardson dev_id = regexdev_find_free_dev(); 10899a2dd95SBruce Richardson if (dev_id == RTE_MAX_REGEXDEV_DEVS) { 10999a2dd95SBruce Richardson RTE_REGEXDEV_LOG 11099a2dd95SBruce Richardson (ERR, "Reached maximum number of RegEx devices\n"); 11199a2dd95SBruce Richardson return NULL; 11299a2dd95SBruce Richardson } 11399a2dd95SBruce Richardson if (regexdev_shared_data_prepare() < 0) { 11499a2dd95SBruce Richardson RTE_REGEXDEV_LOG(ERR, "Cannot allocate RegEx shared data\n"); 11599a2dd95SBruce Richardson return NULL; 11699a2dd95SBruce Richardson } 11799a2dd95SBruce Richardson 11899a2dd95SBruce Richardson dev = &rte_regex_devices[dev_id]; 11999a2dd95SBruce Richardson dev->state = RTE_REGEXDEV_REGISTERED; 12099a2dd95SBruce Richardson if (dev->data == NULL) 12199a2dd95SBruce Richardson dev->data = &rte_regexdev_shared_data->data[dev_id]; 12299a2dd95SBruce Richardson else 12399a2dd95SBruce Richardson memset(dev->data, 1, sizeof(*dev->data)); 12499a2dd95SBruce Richardson dev->data->dev_id = dev_id; 12599a2dd95SBruce Richardson strlcpy(dev->data->dev_name, name, sizeof(dev->data->dev_name)); 12699a2dd95SBruce Richardson return dev; 12799a2dd95SBruce Richardson } 12899a2dd95SBruce Richardson 12999a2dd95SBruce Richardson void 13099a2dd95SBruce Richardson rte_regexdev_unregister(struct rte_regexdev *dev) 13199a2dd95SBruce Richardson { 13299a2dd95SBruce Richardson dev->state = RTE_REGEXDEV_UNUSED; 13399a2dd95SBruce Richardson } 13499a2dd95SBruce Richardson 13599a2dd95SBruce Richardson struct rte_regexdev * 13699a2dd95SBruce Richardson rte_regexdev_get_device_by_name(const char *name) 13799a2dd95SBruce Richardson { 13899a2dd95SBruce Richardson if (regexdev_check_name(name) < 0) 13999a2dd95SBruce Richardson return NULL; 14099a2dd95SBruce Richardson return regexdev_allocated(name); 14199a2dd95SBruce Richardson } 14299a2dd95SBruce Richardson 14399a2dd95SBruce Richardson uint8_t 14499a2dd95SBruce Richardson rte_regexdev_count(void) 14599a2dd95SBruce Richardson { 14699a2dd95SBruce Richardson int i; 14799a2dd95SBruce Richardson int count = 0; 14899a2dd95SBruce Richardson 14999a2dd95SBruce Richardson for (i = 0; i < RTE_MAX_REGEXDEV_DEVS; i++) { 15099a2dd95SBruce Richardson if (rte_regex_devices[i].state != RTE_REGEXDEV_UNUSED) 15199a2dd95SBruce Richardson count++; 15299a2dd95SBruce Richardson } 15399a2dd95SBruce Richardson return count; 15499a2dd95SBruce Richardson } 15599a2dd95SBruce Richardson 15699a2dd95SBruce Richardson int 15799a2dd95SBruce Richardson rte_regexdev_get_dev_id(const char *name) 15899a2dd95SBruce Richardson { 15999a2dd95SBruce Richardson int i; 16099a2dd95SBruce Richardson int id = -EINVAL; 16199a2dd95SBruce Richardson 16299a2dd95SBruce Richardson if (name == NULL) 16399a2dd95SBruce Richardson return -EINVAL; 16499a2dd95SBruce Richardson for (i = 0; i < RTE_MAX_REGEXDEV_DEVS; i++) { 16599a2dd95SBruce Richardson if (rte_regex_devices[i].state != RTE_REGEXDEV_UNUSED) 16699a2dd95SBruce Richardson if (strcmp(name, rte_regex_devices[i].data->dev_name)) { 16799a2dd95SBruce Richardson id = rte_regex_devices[i].data->dev_id; 16899a2dd95SBruce Richardson break; 16999a2dd95SBruce Richardson } 17099a2dd95SBruce Richardson } 17199a2dd95SBruce Richardson return id; 17299a2dd95SBruce Richardson } 17399a2dd95SBruce Richardson 17499a2dd95SBruce Richardson int 17599a2dd95SBruce Richardson rte_regexdev_is_valid_dev(uint16_t dev_id) 17699a2dd95SBruce Richardson { 17799a2dd95SBruce Richardson if (dev_id >= RTE_MAX_REGEXDEV_DEVS || 17899a2dd95SBruce Richardson rte_regex_devices[dev_id].state != RTE_REGEXDEV_READY) 17999a2dd95SBruce Richardson return 0; 18099a2dd95SBruce Richardson return 1; 18199a2dd95SBruce Richardson } 18299a2dd95SBruce Richardson 18399a2dd95SBruce Richardson static int 18499a2dd95SBruce Richardson regexdev_info_get(uint8_t dev_id, struct rte_regexdev_info *dev_info) 18599a2dd95SBruce Richardson { 18699a2dd95SBruce Richardson struct rte_regexdev *dev; 18799a2dd95SBruce Richardson 18899a2dd95SBruce Richardson RTE_REGEXDEV_VALID_DEV_ID_OR_ERR_RET(dev_id, -EINVAL); 18999a2dd95SBruce Richardson if (dev_info == NULL) 19099a2dd95SBruce Richardson return -EINVAL; 19199a2dd95SBruce Richardson dev = &rte_regex_devices[dev_id]; 1928f1d23ecSDavid Marchand if (*dev->dev_ops->dev_info_get == NULL) 1938f1d23ecSDavid Marchand return -ENOTSUP; 19499a2dd95SBruce Richardson return (*dev->dev_ops->dev_info_get)(dev, dev_info); 19599a2dd95SBruce Richardson 19699a2dd95SBruce Richardson } 19799a2dd95SBruce Richardson 19899a2dd95SBruce Richardson int 19999a2dd95SBruce Richardson rte_regexdev_info_get(uint8_t dev_id, struct rte_regexdev_info *dev_info) 20099a2dd95SBruce Richardson { 20199a2dd95SBruce Richardson return regexdev_info_get(dev_id, dev_info); 20299a2dd95SBruce Richardson } 20399a2dd95SBruce Richardson 20499a2dd95SBruce Richardson int 20599a2dd95SBruce Richardson rte_regexdev_configure(uint8_t dev_id, const struct rte_regexdev_config *cfg) 20699a2dd95SBruce Richardson { 20799a2dd95SBruce Richardson struct rte_regexdev *dev; 20899a2dd95SBruce Richardson struct rte_regexdev_info dev_info; 20999a2dd95SBruce Richardson int ret; 21099a2dd95SBruce Richardson 21199a2dd95SBruce Richardson RTE_REGEXDEV_VALID_DEV_ID_OR_ERR_RET(dev_id, -EINVAL); 21299a2dd95SBruce Richardson if (cfg == NULL) 21399a2dd95SBruce Richardson return -EINVAL; 21499a2dd95SBruce Richardson dev = &rte_regex_devices[dev_id]; 2158f1d23ecSDavid Marchand if (*dev->dev_ops->dev_configure == NULL) 2168f1d23ecSDavid Marchand return -ENOTSUP; 21799a2dd95SBruce Richardson if (dev->data->dev_started) { 21899a2dd95SBruce Richardson RTE_REGEXDEV_LOG 21999a2dd95SBruce Richardson (ERR, "Dev %u must be stopped to allow configuration\n", 22099a2dd95SBruce Richardson dev_id); 22199a2dd95SBruce Richardson return -EBUSY; 22299a2dd95SBruce Richardson } 22399a2dd95SBruce Richardson ret = regexdev_info_get(dev_id, &dev_info); 22499a2dd95SBruce Richardson if (ret < 0) 22599a2dd95SBruce Richardson return ret; 22699a2dd95SBruce Richardson if ((cfg->dev_cfg_flags & RTE_REGEXDEV_CFG_CROSS_BUFFER_SCAN_F) && 22799a2dd95SBruce Richardson !(dev_info.regexdev_capa & RTE_REGEXDEV_SUPP_CROSS_BUFFER_F)) { 22899a2dd95SBruce Richardson RTE_REGEXDEV_LOG(ERR, 22999a2dd95SBruce Richardson "Dev %u doesn't support cross buffer scan\n", 23099a2dd95SBruce Richardson dev_id); 23199a2dd95SBruce Richardson return -EINVAL; 23299a2dd95SBruce Richardson } 23399a2dd95SBruce Richardson if ((cfg->dev_cfg_flags & RTE_REGEXDEV_CFG_MATCH_AS_END_F) && 23499a2dd95SBruce Richardson !(dev_info.regexdev_capa & RTE_REGEXDEV_SUPP_MATCH_AS_END_F)) { 23599a2dd95SBruce Richardson RTE_REGEXDEV_LOG(ERR, 23699a2dd95SBruce Richardson "Dev %u doesn't support match as end\n", 23799a2dd95SBruce Richardson dev_id); 23899a2dd95SBruce Richardson return -EINVAL; 23999a2dd95SBruce Richardson } 24099a2dd95SBruce Richardson if ((cfg->dev_cfg_flags & RTE_REGEXDEV_CFG_MATCH_ALL_F) && 24199a2dd95SBruce Richardson !(dev_info.regexdev_capa & RTE_REGEXDEV_SUPP_MATCH_ALL_F)) { 24299a2dd95SBruce Richardson RTE_REGEXDEV_LOG(ERR, 24399a2dd95SBruce Richardson "Dev %u doesn't support match all\n", 24499a2dd95SBruce Richardson dev_id); 24599a2dd95SBruce Richardson return -EINVAL; 24699a2dd95SBruce Richardson } 24799a2dd95SBruce Richardson if (cfg->nb_groups == 0) { 24899a2dd95SBruce Richardson RTE_REGEXDEV_LOG(ERR, "Dev %u num of groups must be > 0\n", 24999a2dd95SBruce Richardson dev_id); 25099a2dd95SBruce Richardson return -EINVAL; 25199a2dd95SBruce Richardson } 25299a2dd95SBruce Richardson if (cfg->nb_groups > dev_info.max_groups) { 25399a2dd95SBruce Richardson RTE_REGEXDEV_LOG(ERR, "Dev %u num of groups %d > %d\n", 25499a2dd95SBruce Richardson dev_id, cfg->nb_groups, dev_info.max_groups); 25599a2dd95SBruce Richardson return -EINVAL; 25699a2dd95SBruce Richardson } 25799a2dd95SBruce Richardson if (cfg->nb_max_matches == 0) { 25899a2dd95SBruce Richardson RTE_REGEXDEV_LOG(ERR, "Dev %u num of matches must be > 0\n", 25999a2dd95SBruce Richardson dev_id); 26099a2dd95SBruce Richardson return -EINVAL; 26199a2dd95SBruce Richardson } 26299a2dd95SBruce Richardson if (cfg->nb_max_matches > dev_info.max_matches) { 26399a2dd95SBruce Richardson RTE_REGEXDEV_LOG(ERR, "Dev %u num of matches %d > %d\n", 26499a2dd95SBruce Richardson dev_id, cfg->nb_max_matches, 26599a2dd95SBruce Richardson dev_info.max_matches); 26699a2dd95SBruce Richardson return -EINVAL; 26799a2dd95SBruce Richardson } 26899a2dd95SBruce Richardson if (cfg->nb_queue_pairs == 0) { 26999a2dd95SBruce Richardson RTE_REGEXDEV_LOG(ERR, "Dev %u num of queues must be > 0\n", 27099a2dd95SBruce Richardson dev_id); 27199a2dd95SBruce Richardson return -EINVAL; 27299a2dd95SBruce Richardson } 27399a2dd95SBruce Richardson if (cfg->nb_queue_pairs > dev_info.max_queue_pairs) { 27499a2dd95SBruce Richardson RTE_REGEXDEV_LOG(ERR, "Dev %u num of queues %d > %d\n", 27599a2dd95SBruce Richardson dev_id, cfg->nb_queue_pairs, 27699a2dd95SBruce Richardson dev_info.max_queue_pairs); 27799a2dd95SBruce Richardson return -EINVAL; 27899a2dd95SBruce Richardson } 27999a2dd95SBruce Richardson if (cfg->nb_rules_per_group == 0) { 28099a2dd95SBruce Richardson RTE_REGEXDEV_LOG(ERR, 28199a2dd95SBruce Richardson "Dev %u num of rules per group must be > 0\n", 28299a2dd95SBruce Richardson dev_id); 28399a2dd95SBruce Richardson return -EINVAL; 28499a2dd95SBruce Richardson } 28599a2dd95SBruce Richardson if (cfg->nb_rules_per_group > dev_info.max_rules_per_group) { 28699a2dd95SBruce Richardson RTE_REGEXDEV_LOG(ERR, 28799a2dd95SBruce Richardson "Dev %u num of rules per group %d > %d\n", 28899a2dd95SBruce Richardson dev_id, cfg->nb_rules_per_group, 28999a2dd95SBruce Richardson dev_info.max_rules_per_group); 29099a2dd95SBruce Richardson return -EINVAL; 29199a2dd95SBruce Richardson } 29299a2dd95SBruce Richardson ret = (*dev->dev_ops->dev_configure)(dev, cfg); 29399a2dd95SBruce Richardson if (ret == 0) 29499a2dd95SBruce Richardson dev->data->dev_conf = *cfg; 29599a2dd95SBruce Richardson return ret; 29699a2dd95SBruce Richardson } 29799a2dd95SBruce Richardson 29899a2dd95SBruce Richardson int 29999a2dd95SBruce Richardson rte_regexdev_queue_pair_setup(uint8_t dev_id, uint16_t queue_pair_id, 30099a2dd95SBruce Richardson const struct rte_regexdev_qp_conf *qp_conf) 30199a2dd95SBruce Richardson { 30299a2dd95SBruce Richardson struct rte_regexdev *dev; 30399a2dd95SBruce Richardson 30499a2dd95SBruce Richardson RTE_REGEXDEV_VALID_DEV_ID_OR_ERR_RET(dev_id, -EINVAL); 30599a2dd95SBruce Richardson dev = &rte_regex_devices[dev_id]; 3068f1d23ecSDavid Marchand if (*dev->dev_ops->dev_qp_setup == NULL) 3078f1d23ecSDavid Marchand return -ENOTSUP; 30899a2dd95SBruce Richardson if (dev->data->dev_started) { 30999a2dd95SBruce Richardson RTE_REGEXDEV_LOG 31099a2dd95SBruce Richardson (ERR, "Dev %u must be stopped to allow configuration\n", 31199a2dd95SBruce Richardson dev_id); 31299a2dd95SBruce Richardson return -EBUSY; 31399a2dd95SBruce Richardson } 31499a2dd95SBruce Richardson if (queue_pair_id >= dev->data->dev_conf.nb_queue_pairs) { 31599a2dd95SBruce Richardson RTE_REGEXDEV_LOG(ERR, 31699a2dd95SBruce Richardson "Dev %u invalid queue %d > %d\n", 31799a2dd95SBruce Richardson dev_id, queue_pair_id, 31899a2dd95SBruce Richardson dev->data->dev_conf.nb_queue_pairs); 31999a2dd95SBruce Richardson return -EINVAL; 32099a2dd95SBruce Richardson } 32199a2dd95SBruce Richardson if (dev->data->dev_started) { 32299a2dd95SBruce Richardson RTE_REGEXDEV_LOG 32399a2dd95SBruce Richardson (ERR, "Dev %u must be stopped to allow configuration\n", 32499a2dd95SBruce Richardson dev_id); 32599a2dd95SBruce Richardson return -EBUSY; 32699a2dd95SBruce Richardson } 32799a2dd95SBruce Richardson return (*dev->dev_ops->dev_qp_setup)(dev, queue_pair_id, qp_conf); 32899a2dd95SBruce Richardson } 32999a2dd95SBruce Richardson 33099a2dd95SBruce Richardson int 33199a2dd95SBruce Richardson rte_regexdev_start(uint8_t dev_id) 33299a2dd95SBruce Richardson { 33399a2dd95SBruce Richardson struct rte_regexdev *dev; 33499a2dd95SBruce Richardson int ret; 33599a2dd95SBruce Richardson 33699a2dd95SBruce Richardson RTE_REGEXDEV_VALID_DEV_ID_OR_ERR_RET(dev_id, -EINVAL); 33799a2dd95SBruce Richardson dev = &rte_regex_devices[dev_id]; 3388f1d23ecSDavid Marchand if (*dev->dev_ops->dev_start == NULL) 3398f1d23ecSDavid Marchand return -ENOTSUP; 34099a2dd95SBruce Richardson ret = (*dev->dev_ops->dev_start)(dev); 34199a2dd95SBruce Richardson if (ret == 0) 34299a2dd95SBruce Richardson dev->data->dev_started = 1; 34399a2dd95SBruce Richardson return ret; 34499a2dd95SBruce Richardson } 34599a2dd95SBruce Richardson 34699a2dd95SBruce Richardson int 34799a2dd95SBruce Richardson rte_regexdev_stop(uint8_t dev_id) 34899a2dd95SBruce Richardson { 34999a2dd95SBruce Richardson struct rte_regexdev *dev; 35099a2dd95SBruce Richardson 35199a2dd95SBruce Richardson RTE_REGEXDEV_VALID_DEV_ID_OR_ERR_RET(dev_id, -EINVAL); 35299a2dd95SBruce Richardson dev = &rte_regex_devices[dev_id]; 3538f1d23ecSDavid Marchand if (*dev->dev_ops->dev_stop == NULL) 3548f1d23ecSDavid Marchand return -ENOTSUP; 35599a2dd95SBruce Richardson (*dev->dev_ops->dev_stop)(dev); 35699a2dd95SBruce Richardson dev->data->dev_started = 0; 35799a2dd95SBruce Richardson return 0; 35899a2dd95SBruce Richardson } 35999a2dd95SBruce Richardson 36099a2dd95SBruce Richardson int 36199a2dd95SBruce Richardson rte_regexdev_close(uint8_t dev_id) 36299a2dd95SBruce Richardson { 36399a2dd95SBruce Richardson struct rte_regexdev *dev; 36499a2dd95SBruce Richardson 36599a2dd95SBruce Richardson RTE_REGEXDEV_VALID_DEV_ID_OR_ERR_RET(dev_id, -EINVAL); 36699a2dd95SBruce Richardson dev = &rte_regex_devices[dev_id]; 3678f1d23ecSDavid Marchand if (*dev->dev_ops->dev_close == NULL) 3688f1d23ecSDavid Marchand return -ENOTSUP; 36999a2dd95SBruce Richardson (*dev->dev_ops->dev_close)(dev); 37099a2dd95SBruce Richardson dev->data->dev_started = 0; 37199a2dd95SBruce Richardson dev->state = RTE_REGEXDEV_UNUSED; 37299a2dd95SBruce Richardson return 0; 37399a2dd95SBruce Richardson } 37499a2dd95SBruce Richardson 37599a2dd95SBruce Richardson int 37699a2dd95SBruce Richardson rte_regexdev_attr_get(uint8_t dev_id, enum rte_regexdev_attr_id attr_id, 37799a2dd95SBruce Richardson void *attr_value) 37899a2dd95SBruce Richardson { 37999a2dd95SBruce Richardson struct rte_regexdev *dev; 38099a2dd95SBruce Richardson 38199a2dd95SBruce Richardson RTE_REGEXDEV_VALID_DEV_ID_OR_ERR_RET(dev_id, -EINVAL); 38299a2dd95SBruce Richardson dev = &rte_regex_devices[dev_id]; 3838f1d23ecSDavid Marchand if (*dev->dev_ops->dev_attr_get == NULL) 3848f1d23ecSDavid Marchand return -ENOTSUP; 38599a2dd95SBruce Richardson if (attr_value == NULL) { 38699a2dd95SBruce Richardson RTE_REGEXDEV_LOG(ERR, "Dev %d attribute value can't be NULL\n", 38799a2dd95SBruce Richardson dev_id); 38899a2dd95SBruce Richardson return -EINVAL; 38999a2dd95SBruce Richardson } 39099a2dd95SBruce Richardson return (*dev->dev_ops->dev_attr_get)(dev, attr_id, attr_value); 39199a2dd95SBruce Richardson } 39299a2dd95SBruce Richardson 39399a2dd95SBruce Richardson int 39499a2dd95SBruce Richardson rte_regexdev_attr_set(uint8_t dev_id, enum rte_regexdev_attr_id attr_id, 39599a2dd95SBruce Richardson const void *attr_value) 39699a2dd95SBruce Richardson { 39799a2dd95SBruce Richardson struct rte_regexdev *dev; 39899a2dd95SBruce Richardson 39999a2dd95SBruce Richardson RTE_REGEXDEV_VALID_DEV_ID_OR_ERR_RET(dev_id, -EINVAL); 40099a2dd95SBruce Richardson dev = &rte_regex_devices[dev_id]; 4018f1d23ecSDavid Marchand if (*dev->dev_ops->dev_attr_set == NULL) 4028f1d23ecSDavid Marchand return -ENOTSUP; 40399a2dd95SBruce Richardson if (attr_value == NULL) { 40499a2dd95SBruce Richardson RTE_REGEXDEV_LOG(ERR, "Dev %d attribute value can't be NULL\n", 40599a2dd95SBruce Richardson dev_id); 40699a2dd95SBruce Richardson return -EINVAL; 40799a2dd95SBruce Richardson } 40899a2dd95SBruce Richardson return (*dev->dev_ops->dev_attr_set)(dev, attr_id, attr_value); 40999a2dd95SBruce Richardson } 41099a2dd95SBruce Richardson 41199a2dd95SBruce Richardson int 41299a2dd95SBruce Richardson rte_regexdev_rule_db_update(uint8_t dev_id, 41399a2dd95SBruce Richardson const struct rte_regexdev_rule *rules, 41499a2dd95SBruce Richardson uint32_t nb_rules) 41599a2dd95SBruce Richardson { 41699a2dd95SBruce Richardson struct rte_regexdev *dev; 41799a2dd95SBruce Richardson 41899a2dd95SBruce Richardson RTE_REGEXDEV_VALID_DEV_ID_OR_ERR_RET(dev_id, -EINVAL); 41999a2dd95SBruce Richardson dev = &rte_regex_devices[dev_id]; 4208f1d23ecSDavid Marchand if (*dev->dev_ops->dev_rule_db_update == NULL) 4218f1d23ecSDavid Marchand return -ENOTSUP; 42299a2dd95SBruce Richardson if (rules == NULL) { 42399a2dd95SBruce Richardson RTE_REGEXDEV_LOG(ERR, "Dev %d rules can't be NULL\n", 42499a2dd95SBruce Richardson dev_id); 42599a2dd95SBruce Richardson return -EINVAL; 42699a2dd95SBruce Richardson } 42799a2dd95SBruce Richardson return (*dev->dev_ops->dev_rule_db_update)(dev, rules, nb_rules); 42899a2dd95SBruce Richardson } 42999a2dd95SBruce Richardson 43099a2dd95SBruce Richardson int 43199a2dd95SBruce Richardson rte_regexdev_rule_db_compile_activate(uint8_t dev_id) 43299a2dd95SBruce Richardson { 43399a2dd95SBruce Richardson struct rte_regexdev *dev; 43499a2dd95SBruce Richardson 43599a2dd95SBruce Richardson RTE_REGEXDEV_VALID_DEV_ID_OR_ERR_RET(dev_id, -EINVAL); 43699a2dd95SBruce Richardson dev = &rte_regex_devices[dev_id]; 4378f1d23ecSDavid Marchand if (*dev->dev_ops->dev_rule_db_compile_activate == NULL) 4388f1d23ecSDavid Marchand return -ENOTSUP; 43999a2dd95SBruce Richardson return (*dev->dev_ops->dev_rule_db_compile_activate)(dev); 44099a2dd95SBruce Richardson } 44199a2dd95SBruce Richardson 44299a2dd95SBruce Richardson int 44399a2dd95SBruce Richardson rte_regexdev_rule_db_import(uint8_t dev_id, const char *rule_db, 44499a2dd95SBruce Richardson uint32_t rule_db_len) 44599a2dd95SBruce Richardson { 44699a2dd95SBruce Richardson struct rte_regexdev *dev; 44799a2dd95SBruce Richardson 44899a2dd95SBruce Richardson RTE_REGEXDEV_VALID_DEV_ID_OR_ERR_RET(dev_id, -EINVAL); 44999a2dd95SBruce Richardson dev = &rte_regex_devices[dev_id]; 4508f1d23ecSDavid Marchand if (*dev->dev_ops->dev_db_import == NULL) 4518f1d23ecSDavid Marchand return -ENOTSUP; 45299a2dd95SBruce Richardson if (rule_db == NULL) { 45399a2dd95SBruce Richardson RTE_REGEXDEV_LOG(ERR, "Dev %d rules can't be NULL\n", 45499a2dd95SBruce Richardson dev_id); 45599a2dd95SBruce Richardson return -EINVAL; 45699a2dd95SBruce Richardson } 45799a2dd95SBruce Richardson return (*dev->dev_ops->dev_db_import)(dev, rule_db, rule_db_len); 45899a2dd95SBruce Richardson } 45999a2dd95SBruce Richardson 46099a2dd95SBruce Richardson int 46199a2dd95SBruce Richardson rte_regexdev_rule_db_export(uint8_t dev_id, char *rule_db) 46299a2dd95SBruce Richardson { 46399a2dd95SBruce Richardson struct rte_regexdev *dev; 46499a2dd95SBruce Richardson 46599a2dd95SBruce Richardson RTE_REGEXDEV_VALID_DEV_ID_OR_ERR_RET(dev_id, -EINVAL); 46699a2dd95SBruce Richardson dev = &rte_regex_devices[dev_id]; 4678f1d23ecSDavid Marchand if (*dev->dev_ops->dev_db_export == NULL) 4688f1d23ecSDavid Marchand return -ENOTSUP; 46999a2dd95SBruce Richardson return (*dev->dev_ops->dev_db_export)(dev, rule_db); 47099a2dd95SBruce Richardson } 47199a2dd95SBruce Richardson 47299a2dd95SBruce Richardson int 47399a2dd95SBruce Richardson rte_regexdev_xstats_names_get(uint8_t dev_id, 47499a2dd95SBruce Richardson struct rte_regexdev_xstats_map *xstats_map) 47599a2dd95SBruce Richardson { 47699a2dd95SBruce Richardson struct rte_regexdev *dev; 47799a2dd95SBruce Richardson 47899a2dd95SBruce Richardson RTE_REGEXDEV_VALID_DEV_ID_OR_ERR_RET(dev_id, -EINVAL); 47999a2dd95SBruce Richardson dev = &rte_regex_devices[dev_id]; 4808f1d23ecSDavid Marchand if (*dev->dev_ops->dev_xstats_names_get == NULL) 4818f1d23ecSDavid Marchand return -ENOTSUP; 48299a2dd95SBruce Richardson if (xstats_map == NULL) { 48399a2dd95SBruce Richardson RTE_REGEXDEV_LOG(ERR, "Dev %d xstats map can't be NULL\n", 48499a2dd95SBruce Richardson dev_id); 48599a2dd95SBruce Richardson return -EINVAL; 48699a2dd95SBruce Richardson } 48799a2dd95SBruce Richardson return (*dev->dev_ops->dev_xstats_names_get)(dev, xstats_map); 48899a2dd95SBruce Richardson } 48999a2dd95SBruce Richardson 49099a2dd95SBruce Richardson int 49199a2dd95SBruce Richardson rte_regexdev_xstats_get(uint8_t dev_id, const uint16_t *ids, 49299a2dd95SBruce Richardson uint64_t *values, uint16_t n) 49399a2dd95SBruce Richardson { 49499a2dd95SBruce Richardson struct rte_regexdev *dev; 49599a2dd95SBruce Richardson 49699a2dd95SBruce Richardson RTE_REGEXDEV_VALID_DEV_ID_OR_ERR_RET(dev_id, -EINVAL); 49799a2dd95SBruce Richardson dev = &rte_regex_devices[dev_id]; 4988f1d23ecSDavid Marchand if (*dev->dev_ops->dev_xstats_get == NULL) 4998f1d23ecSDavid Marchand return -ENOTSUP; 50099a2dd95SBruce Richardson if (ids == NULL) { 50199a2dd95SBruce Richardson RTE_REGEXDEV_LOG(ERR, "Dev %d ids can't be NULL\n", dev_id); 50299a2dd95SBruce Richardson return -EINVAL; 50399a2dd95SBruce Richardson } 50499a2dd95SBruce Richardson if (values == NULL) { 50599a2dd95SBruce Richardson RTE_REGEXDEV_LOG(ERR, "Dev %d values can't be NULL\n", dev_id); 50699a2dd95SBruce Richardson return -EINVAL; 50799a2dd95SBruce Richardson } 50899a2dd95SBruce Richardson return (*dev->dev_ops->dev_xstats_get)(dev, ids, values, n); 50999a2dd95SBruce Richardson } 51099a2dd95SBruce Richardson 51199a2dd95SBruce Richardson int 51299a2dd95SBruce Richardson rte_regexdev_xstats_by_name_get(uint8_t dev_id, const char *name, 51399a2dd95SBruce Richardson uint16_t *id, uint64_t *value) 51499a2dd95SBruce Richardson { 51599a2dd95SBruce Richardson struct rte_regexdev *dev; 51699a2dd95SBruce Richardson 51799a2dd95SBruce Richardson RTE_REGEXDEV_VALID_DEV_ID_OR_ERR_RET(dev_id, -EINVAL); 51899a2dd95SBruce Richardson dev = &rte_regex_devices[dev_id]; 5198f1d23ecSDavid Marchand if (*dev->dev_ops->dev_xstats_by_name_get == NULL) 5208f1d23ecSDavid Marchand return -ENOTSUP; 52199a2dd95SBruce Richardson if (name == NULL) { 52299a2dd95SBruce Richardson RTE_REGEXDEV_LOG(ERR, "Dev %d name can't be NULL\n", dev_id); 52399a2dd95SBruce Richardson return -EINVAL; 52499a2dd95SBruce Richardson } 52599a2dd95SBruce Richardson if (id == NULL) { 52699a2dd95SBruce Richardson RTE_REGEXDEV_LOG(ERR, "Dev %d id can't be NULL\n", dev_id); 52799a2dd95SBruce Richardson return -EINVAL; 52899a2dd95SBruce Richardson } 52999a2dd95SBruce Richardson if (value == NULL) { 53099a2dd95SBruce Richardson RTE_REGEXDEV_LOG(ERR, "Dev %d value can't be NULL\n", dev_id); 53199a2dd95SBruce Richardson return -EINVAL; 53299a2dd95SBruce Richardson } 53399a2dd95SBruce Richardson return (*dev->dev_ops->dev_xstats_by_name_get)(dev, name, id, value); 53499a2dd95SBruce Richardson } 53599a2dd95SBruce Richardson 53699a2dd95SBruce Richardson int 53799a2dd95SBruce Richardson rte_regexdev_xstats_reset(uint8_t dev_id, const uint16_t *ids, 53899a2dd95SBruce Richardson uint16_t nb_ids) 53999a2dd95SBruce Richardson { 54099a2dd95SBruce Richardson struct rte_regexdev *dev; 54199a2dd95SBruce Richardson 54299a2dd95SBruce Richardson RTE_REGEXDEV_VALID_DEV_ID_OR_ERR_RET(dev_id, -EINVAL); 54399a2dd95SBruce Richardson dev = &rte_regex_devices[dev_id]; 5448f1d23ecSDavid Marchand if (*dev->dev_ops->dev_xstats_reset == NULL) 5458f1d23ecSDavid Marchand return -ENOTSUP; 54699a2dd95SBruce Richardson if (ids == NULL) { 54799a2dd95SBruce Richardson RTE_REGEXDEV_LOG(ERR, "Dev %d ids can't be NULL\n", dev_id); 54899a2dd95SBruce Richardson return -EINVAL; 54999a2dd95SBruce Richardson } 55099a2dd95SBruce Richardson return (*dev->dev_ops->dev_xstats_reset)(dev, ids, nb_ids); 55199a2dd95SBruce Richardson } 55299a2dd95SBruce Richardson 55399a2dd95SBruce Richardson int 55499a2dd95SBruce Richardson rte_regexdev_selftest(uint8_t dev_id) 55599a2dd95SBruce Richardson { 55699a2dd95SBruce Richardson struct rte_regexdev *dev; 55799a2dd95SBruce Richardson 55899a2dd95SBruce Richardson RTE_REGEXDEV_VALID_DEV_ID_OR_ERR_RET(dev_id, -EINVAL); 55999a2dd95SBruce Richardson dev = &rte_regex_devices[dev_id]; 5608f1d23ecSDavid Marchand if (*dev->dev_ops->dev_selftest == NULL) 5618f1d23ecSDavid Marchand return -ENOTSUP; 56299a2dd95SBruce Richardson return (*dev->dev_ops->dev_selftest)(dev); 56399a2dd95SBruce Richardson } 56499a2dd95SBruce Richardson 56599a2dd95SBruce Richardson int 56699a2dd95SBruce Richardson rte_regexdev_dump(uint8_t dev_id, FILE *f) 56799a2dd95SBruce Richardson { 56899a2dd95SBruce Richardson struct rte_regexdev *dev; 56999a2dd95SBruce Richardson 57099a2dd95SBruce Richardson RTE_REGEXDEV_VALID_DEV_ID_OR_ERR_RET(dev_id, -EINVAL); 57199a2dd95SBruce Richardson dev = &rte_regex_devices[dev_id]; 5728f1d23ecSDavid Marchand if (*dev->dev_ops->dev_dump == NULL) 5738f1d23ecSDavid Marchand return -ENOTSUP; 57499a2dd95SBruce Richardson if (f == NULL) { 57599a2dd95SBruce Richardson RTE_REGEXDEV_LOG(ERR, "Dev %d file can't be NULL\n", dev_id); 57699a2dd95SBruce Richardson return -EINVAL; 57799a2dd95SBruce Richardson } 57899a2dd95SBruce Richardson return (*dev->dev_ops->dev_dump)(dev, f); 57999a2dd95SBruce Richardson } 580