xref: /dpdk/lib/eventdev/rte_event_crypto_adapter.c (revision a256a743cf1cb40c9c98ebba87e6b7478fb9d2fc)
199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
299a2dd95SBruce Richardson  * Copyright(c) 2018 Intel Corporation.
399a2dd95SBruce Richardson  * All rights reserved.
499a2dd95SBruce Richardson  */
599a2dd95SBruce Richardson 
699a2dd95SBruce Richardson #include <string.h>
799a2dd95SBruce Richardson #include <stdbool.h>
899a2dd95SBruce Richardson #include <rte_common.h>
999a2dd95SBruce Richardson #include <rte_dev.h>
1099a2dd95SBruce Richardson #include <rte_errno.h>
1199a2dd95SBruce Richardson #include <rte_cryptodev.h>
12af668035SAkhil Goyal #include <cryptodev_pmd.h>
1399a2dd95SBruce Richardson #include <rte_log.h>
1499a2dd95SBruce Richardson #include <rte_malloc.h>
1599a2dd95SBruce Richardson #include <rte_service_component.h>
1699a2dd95SBruce Richardson 
1799a2dd95SBruce Richardson #include "rte_eventdev.h"
1899a2dd95SBruce Richardson #include "eventdev_pmd.h"
1999a2dd95SBruce Richardson #include "rte_eventdev_trace.h"
2099a2dd95SBruce Richardson #include "rte_event_crypto_adapter.h"
2199a2dd95SBruce Richardson 
2299a2dd95SBruce Richardson #define BATCH_SIZE 32
2399a2dd95SBruce Richardson #define DEFAULT_MAX_NB 128
2499a2dd95SBruce Richardson #define CRYPTO_ADAPTER_NAME_LEN 32
2599a2dd95SBruce Richardson #define CRYPTO_ADAPTER_MEM_NAME_LEN 32
2699a2dd95SBruce Richardson #define CRYPTO_ADAPTER_MAX_EV_ENQ_RETRIES 100
2799a2dd95SBruce Richardson 
2899a2dd95SBruce Richardson /* Flush an instance's enqueue buffers every CRYPTO_ENQ_FLUSH_THRESHOLD
2999a2dd95SBruce Richardson  * iterations of eca_crypto_adapter_enq_run()
3099a2dd95SBruce Richardson  */
3199a2dd95SBruce Richardson #define CRYPTO_ENQ_FLUSH_THRESHOLD 1024
3299a2dd95SBruce Richardson 
33*a256a743SPavan Nikhilesh struct event_crypto_adapter {
3499a2dd95SBruce Richardson 	/* Event device identifier */
3599a2dd95SBruce Richardson 	uint8_t eventdev_id;
3699a2dd95SBruce Richardson 	/* Event port identifier */
3799a2dd95SBruce Richardson 	uint8_t event_port_id;
3899a2dd95SBruce Richardson 	/* Store event device's implicit release capability */
3999a2dd95SBruce Richardson 	uint8_t implicit_release_disabled;
4099a2dd95SBruce Richardson 	/* Max crypto ops processed in any service function invocation */
4199a2dd95SBruce Richardson 	uint32_t max_nb;
4299a2dd95SBruce Richardson 	/* Lock to serialize config updates with service function */
4399a2dd95SBruce Richardson 	rte_spinlock_t lock;
4499a2dd95SBruce Richardson 	/* Next crypto device to be processed */
4599a2dd95SBruce Richardson 	uint16_t next_cdev_id;
4699a2dd95SBruce Richardson 	/* Per crypto device structure */
4799a2dd95SBruce Richardson 	struct crypto_device_info *cdevs;
4899a2dd95SBruce Richardson 	/* Loop counter to flush crypto ops */
4999a2dd95SBruce Richardson 	uint16_t transmit_loop_count;
5099a2dd95SBruce Richardson 	/* Per instance stats structure */
5199a2dd95SBruce Richardson 	struct rte_event_crypto_adapter_stats crypto_stats;
5299a2dd95SBruce Richardson 	/* Configuration callback for rte_service configuration */
5399a2dd95SBruce Richardson 	rte_event_crypto_adapter_conf_cb conf_cb;
5499a2dd95SBruce Richardson 	/* Configuration callback argument */
5599a2dd95SBruce Richardson 	void *conf_arg;
5699a2dd95SBruce Richardson 	/* Set if  default_cb is being used */
5799a2dd95SBruce Richardson 	int default_cb_arg;
5899a2dd95SBruce Richardson 	/* Service initialization state */
5999a2dd95SBruce Richardson 	uint8_t service_inited;
6099a2dd95SBruce Richardson 	/* Memory allocation name */
6199a2dd95SBruce Richardson 	char mem_name[CRYPTO_ADAPTER_MEM_NAME_LEN];
6299a2dd95SBruce Richardson 	/* Socket identifier cached from eventdev */
6399a2dd95SBruce Richardson 	int socket_id;
6499a2dd95SBruce Richardson 	/* Per adapter EAL service */
6599a2dd95SBruce Richardson 	uint32_t service_id;
6699a2dd95SBruce Richardson 	/* No. of queue pairs configured */
6799a2dd95SBruce Richardson 	uint16_t nb_qps;
6899a2dd95SBruce Richardson 	/* Adapter mode */
6999a2dd95SBruce Richardson 	enum rte_event_crypto_adapter_mode mode;
7099a2dd95SBruce Richardson } __rte_cache_aligned;
7199a2dd95SBruce Richardson 
7299a2dd95SBruce Richardson /* Per crypto device information */
7399a2dd95SBruce Richardson struct crypto_device_info {
7499a2dd95SBruce Richardson 	/* Pointer to cryptodev */
7599a2dd95SBruce Richardson 	struct rte_cryptodev *dev;
7699a2dd95SBruce Richardson 	/* Pointer to queue pair info */
7799a2dd95SBruce Richardson 	struct crypto_queue_pair_info *qpairs;
7899a2dd95SBruce Richardson 	/* Next queue pair to be processed */
7999a2dd95SBruce Richardson 	uint16_t next_queue_pair_id;
8099a2dd95SBruce Richardson 	/* Set to indicate cryptodev->eventdev packet
8199a2dd95SBruce Richardson 	 * transfer uses a hardware mechanism
8299a2dd95SBruce Richardson 	 */
8399a2dd95SBruce Richardson 	uint8_t internal_event_port;
8499a2dd95SBruce Richardson 	/* Set to indicate processing has been started */
8599a2dd95SBruce Richardson 	uint8_t dev_started;
8699a2dd95SBruce Richardson 	/* If num_qpairs > 0, the start callback will
8799a2dd95SBruce Richardson 	 * be invoked if not already invoked
8899a2dd95SBruce Richardson 	 */
8999a2dd95SBruce Richardson 	uint16_t num_qpairs;
9099a2dd95SBruce Richardson } __rte_cache_aligned;
9199a2dd95SBruce Richardson 
9299a2dd95SBruce Richardson /* Per queue pair information */
9399a2dd95SBruce Richardson struct crypto_queue_pair_info {
9499a2dd95SBruce Richardson 	/* Set to indicate queue pair is enabled */
9599a2dd95SBruce Richardson 	bool qp_enabled;
9699a2dd95SBruce Richardson 	/* Pointer to hold rte_crypto_ops for batching */
9799a2dd95SBruce Richardson 	struct rte_crypto_op **op_buffer;
9899a2dd95SBruce Richardson 	/* No of crypto ops accumulated */
9999a2dd95SBruce Richardson 	uint8_t len;
10099a2dd95SBruce Richardson } __rte_cache_aligned;
10199a2dd95SBruce Richardson 
102*a256a743SPavan Nikhilesh static struct event_crypto_adapter **event_crypto_adapter;
10399a2dd95SBruce Richardson 
10499a2dd95SBruce Richardson /* Macros to check for valid adapter */
10599a2dd95SBruce Richardson #define EVENT_CRYPTO_ADAPTER_ID_VALID_OR_ERR_RET(id, retval) do { \
10699a2dd95SBruce Richardson 	if (!eca_valid_id(id)) { \
10799a2dd95SBruce Richardson 		RTE_EDEV_LOG_ERR("Invalid crypto adapter id = %d\n", id); \
10899a2dd95SBruce Richardson 		return retval; \
10999a2dd95SBruce Richardson 	} \
11099a2dd95SBruce Richardson } while (0)
11199a2dd95SBruce Richardson 
11299a2dd95SBruce Richardson static inline int
11399a2dd95SBruce Richardson eca_valid_id(uint8_t id)
11499a2dd95SBruce Richardson {
11599a2dd95SBruce Richardson 	return id < RTE_EVENT_CRYPTO_ADAPTER_MAX_INSTANCE;
11699a2dd95SBruce Richardson }
11799a2dd95SBruce Richardson 
11899a2dd95SBruce Richardson static int
11999a2dd95SBruce Richardson eca_init(void)
12099a2dd95SBruce Richardson {
12199a2dd95SBruce Richardson 	const char *name = "crypto_adapter_array";
12299a2dd95SBruce Richardson 	const struct rte_memzone *mz;
12399a2dd95SBruce Richardson 	unsigned int sz;
12499a2dd95SBruce Richardson 
12599a2dd95SBruce Richardson 	sz = sizeof(*event_crypto_adapter) *
12699a2dd95SBruce Richardson 	    RTE_EVENT_CRYPTO_ADAPTER_MAX_INSTANCE;
12799a2dd95SBruce Richardson 	sz = RTE_ALIGN(sz, RTE_CACHE_LINE_SIZE);
12899a2dd95SBruce Richardson 
12999a2dd95SBruce Richardson 	mz = rte_memzone_lookup(name);
13099a2dd95SBruce Richardson 	if (mz == NULL) {
13199a2dd95SBruce Richardson 		mz = rte_memzone_reserve_aligned(name, sz, rte_socket_id(), 0,
13299a2dd95SBruce Richardson 						 RTE_CACHE_LINE_SIZE);
13399a2dd95SBruce Richardson 		if (mz == NULL) {
13499a2dd95SBruce Richardson 			RTE_EDEV_LOG_ERR("failed to reserve memzone err = %"
13599a2dd95SBruce Richardson 					PRId32, rte_errno);
13699a2dd95SBruce Richardson 			return -rte_errno;
13799a2dd95SBruce Richardson 		}
13899a2dd95SBruce Richardson 	}
13999a2dd95SBruce Richardson 
14099a2dd95SBruce Richardson 	event_crypto_adapter = mz->addr;
14199a2dd95SBruce Richardson 	return 0;
14299a2dd95SBruce Richardson }
14399a2dd95SBruce Richardson 
144*a256a743SPavan Nikhilesh static inline struct event_crypto_adapter *
14599a2dd95SBruce Richardson eca_id_to_adapter(uint8_t id)
14699a2dd95SBruce Richardson {
14799a2dd95SBruce Richardson 	return event_crypto_adapter ?
14899a2dd95SBruce Richardson 		event_crypto_adapter[id] : NULL;
14999a2dd95SBruce Richardson }
15099a2dd95SBruce Richardson 
15199a2dd95SBruce Richardson static int
15299a2dd95SBruce Richardson eca_default_config_cb(uint8_t id, uint8_t dev_id,
15399a2dd95SBruce Richardson 			struct rte_event_crypto_adapter_conf *conf, void *arg)
15499a2dd95SBruce Richardson {
15599a2dd95SBruce Richardson 	struct rte_event_dev_config dev_conf;
15699a2dd95SBruce Richardson 	struct rte_eventdev *dev;
15799a2dd95SBruce Richardson 	uint8_t port_id;
15899a2dd95SBruce Richardson 	int started;
15999a2dd95SBruce Richardson 	int ret;
16099a2dd95SBruce Richardson 	struct rte_event_port_conf *port_conf = arg;
161*a256a743SPavan Nikhilesh 	struct event_crypto_adapter *adapter = eca_id_to_adapter(id);
16299a2dd95SBruce Richardson 
16399a2dd95SBruce Richardson 	if (adapter == NULL)
16499a2dd95SBruce Richardson 		return -EINVAL;
16599a2dd95SBruce Richardson 
16699a2dd95SBruce Richardson 	dev = &rte_eventdevs[adapter->eventdev_id];
16799a2dd95SBruce Richardson 	dev_conf = dev->data->dev_conf;
16899a2dd95SBruce Richardson 
16999a2dd95SBruce Richardson 	started = dev->data->dev_started;
17099a2dd95SBruce Richardson 	if (started)
17199a2dd95SBruce Richardson 		rte_event_dev_stop(dev_id);
17299a2dd95SBruce Richardson 	port_id = dev_conf.nb_event_ports;
17399a2dd95SBruce Richardson 	dev_conf.nb_event_ports += 1;
17499a2dd95SBruce Richardson 	ret = rte_event_dev_configure(dev_id, &dev_conf);
17599a2dd95SBruce Richardson 	if (ret) {
17699a2dd95SBruce Richardson 		RTE_EDEV_LOG_ERR("failed to configure event dev %u\n", dev_id);
17799a2dd95SBruce Richardson 		if (started) {
17899a2dd95SBruce Richardson 			if (rte_event_dev_start(dev_id))
17999a2dd95SBruce Richardson 				return -EIO;
18099a2dd95SBruce Richardson 		}
18199a2dd95SBruce Richardson 		return ret;
18299a2dd95SBruce Richardson 	}
18399a2dd95SBruce Richardson 
18499a2dd95SBruce Richardson 	ret = rte_event_port_setup(dev_id, port_id, port_conf);
18599a2dd95SBruce Richardson 	if (ret) {
18699a2dd95SBruce Richardson 		RTE_EDEV_LOG_ERR("failed to setup event port %u\n", port_id);
18799a2dd95SBruce Richardson 		return ret;
18899a2dd95SBruce Richardson 	}
18999a2dd95SBruce Richardson 
19099a2dd95SBruce Richardson 	conf->event_port_id = port_id;
19199a2dd95SBruce Richardson 	conf->max_nb = DEFAULT_MAX_NB;
19299a2dd95SBruce Richardson 	if (started)
19399a2dd95SBruce Richardson 		ret = rte_event_dev_start(dev_id);
19499a2dd95SBruce Richardson 
19599a2dd95SBruce Richardson 	adapter->default_cb_arg = 1;
19699a2dd95SBruce Richardson 	return ret;
19799a2dd95SBruce Richardson }
19899a2dd95SBruce Richardson 
19999a2dd95SBruce Richardson int
20099a2dd95SBruce Richardson rte_event_crypto_adapter_create_ext(uint8_t id, uint8_t dev_id,
20199a2dd95SBruce Richardson 				rte_event_crypto_adapter_conf_cb conf_cb,
20299a2dd95SBruce Richardson 				enum rte_event_crypto_adapter_mode mode,
20399a2dd95SBruce Richardson 				void *conf_arg)
20499a2dd95SBruce Richardson {
205*a256a743SPavan Nikhilesh 	struct event_crypto_adapter *adapter;
20699a2dd95SBruce Richardson 	char mem_name[CRYPTO_ADAPTER_NAME_LEN];
20799a2dd95SBruce Richardson 	struct rte_event_dev_info dev_info;
20899a2dd95SBruce Richardson 	int socket_id;
20999a2dd95SBruce Richardson 	uint8_t i;
21099a2dd95SBruce Richardson 	int ret;
21199a2dd95SBruce Richardson 
21299a2dd95SBruce Richardson 	EVENT_CRYPTO_ADAPTER_ID_VALID_OR_ERR_RET(id, -EINVAL);
21399a2dd95SBruce Richardson 	RTE_EVENTDEV_VALID_DEVID_OR_ERR_RET(dev_id, -EINVAL);
21499a2dd95SBruce Richardson 	if (conf_cb == NULL)
21599a2dd95SBruce Richardson 		return -EINVAL;
21699a2dd95SBruce Richardson 
21799a2dd95SBruce Richardson 	if (event_crypto_adapter == NULL) {
21899a2dd95SBruce Richardson 		ret = eca_init();
21999a2dd95SBruce Richardson 		if (ret)
22099a2dd95SBruce Richardson 			return ret;
22199a2dd95SBruce Richardson 	}
22299a2dd95SBruce Richardson 
22399a2dd95SBruce Richardson 	adapter = eca_id_to_adapter(id);
22499a2dd95SBruce Richardson 	if (adapter != NULL) {
22599a2dd95SBruce Richardson 		RTE_EDEV_LOG_ERR("Crypto adapter id %u already exists!", id);
22699a2dd95SBruce Richardson 		return -EEXIST;
22799a2dd95SBruce Richardson 	}
22899a2dd95SBruce Richardson 
22999a2dd95SBruce Richardson 	socket_id = rte_event_dev_socket_id(dev_id);
23099a2dd95SBruce Richardson 	snprintf(mem_name, CRYPTO_ADAPTER_MEM_NAME_LEN,
23199a2dd95SBruce Richardson 		 "rte_event_crypto_adapter_%d", id);
23299a2dd95SBruce Richardson 
23399a2dd95SBruce Richardson 	adapter = rte_zmalloc_socket(mem_name, sizeof(*adapter),
23499a2dd95SBruce Richardson 			RTE_CACHE_LINE_SIZE, socket_id);
23599a2dd95SBruce Richardson 	if (adapter == NULL) {
23699a2dd95SBruce Richardson 		RTE_EDEV_LOG_ERR("Failed to get mem for event crypto adapter!");
23799a2dd95SBruce Richardson 		return -ENOMEM;
23899a2dd95SBruce Richardson 	}
23999a2dd95SBruce Richardson 
24099a2dd95SBruce Richardson 	ret = rte_event_dev_info_get(dev_id, &dev_info);
24199a2dd95SBruce Richardson 	if (ret < 0) {
24299a2dd95SBruce Richardson 		RTE_EDEV_LOG_ERR("Failed to get info for eventdev %d: %s!",
24399a2dd95SBruce Richardson 				 dev_id, dev_info.driver_name);
24499a2dd95SBruce Richardson 		rte_free(adapter);
24599a2dd95SBruce Richardson 		return ret;
24699a2dd95SBruce Richardson 	}
24799a2dd95SBruce Richardson 
24899a2dd95SBruce Richardson 	adapter->implicit_release_disabled = (dev_info.event_dev_cap &
24999a2dd95SBruce Richardson 			RTE_EVENT_DEV_CAP_IMPLICIT_RELEASE_DISABLE);
25099a2dd95SBruce Richardson 	adapter->eventdev_id = dev_id;
25199a2dd95SBruce Richardson 	adapter->socket_id = socket_id;
25299a2dd95SBruce Richardson 	adapter->conf_cb = conf_cb;
25399a2dd95SBruce Richardson 	adapter->conf_arg = conf_arg;
25499a2dd95SBruce Richardson 	adapter->mode = mode;
25599a2dd95SBruce Richardson 	strcpy(adapter->mem_name, mem_name);
25699a2dd95SBruce Richardson 	adapter->cdevs = rte_zmalloc_socket(adapter->mem_name,
25799a2dd95SBruce Richardson 					rte_cryptodev_count() *
25899a2dd95SBruce Richardson 					sizeof(struct crypto_device_info), 0,
25999a2dd95SBruce Richardson 					socket_id);
26099a2dd95SBruce Richardson 	if (adapter->cdevs == NULL) {
26199a2dd95SBruce Richardson 		RTE_EDEV_LOG_ERR("Failed to get mem for crypto devices\n");
26299a2dd95SBruce Richardson 		rte_free(adapter);
26399a2dd95SBruce Richardson 		return -ENOMEM;
26499a2dd95SBruce Richardson 	}
26599a2dd95SBruce Richardson 
26699a2dd95SBruce Richardson 	rte_spinlock_init(&adapter->lock);
26799a2dd95SBruce Richardson 	for (i = 0; i < rte_cryptodev_count(); i++)
26899a2dd95SBruce Richardson 		adapter->cdevs[i].dev = rte_cryptodev_pmd_get_dev(i);
26999a2dd95SBruce Richardson 
27099a2dd95SBruce Richardson 	event_crypto_adapter[id] = adapter;
27199a2dd95SBruce Richardson 
27299a2dd95SBruce Richardson 	rte_eventdev_trace_crypto_adapter_create(id, dev_id, adapter, conf_arg,
27399a2dd95SBruce Richardson 		mode);
27499a2dd95SBruce Richardson 	return 0;
27599a2dd95SBruce Richardson }
27699a2dd95SBruce Richardson 
27799a2dd95SBruce Richardson 
27899a2dd95SBruce Richardson int
27999a2dd95SBruce Richardson rte_event_crypto_adapter_create(uint8_t id, uint8_t dev_id,
28099a2dd95SBruce Richardson 				struct rte_event_port_conf *port_config,
28199a2dd95SBruce Richardson 				enum rte_event_crypto_adapter_mode mode)
28299a2dd95SBruce Richardson {
28399a2dd95SBruce Richardson 	struct rte_event_port_conf *pc;
28499a2dd95SBruce Richardson 	int ret;
28599a2dd95SBruce Richardson 
28699a2dd95SBruce Richardson 	if (port_config == NULL)
28799a2dd95SBruce Richardson 		return -EINVAL;
28899a2dd95SBruce Richardson 	EVENT_CRYPTO_ADAPTER_ID_VALID_OR_ERR_RET(id, -EINVAL);
28999a2dd95SBruce Richardson 
29099a2dd95SBruce Richardson 	pc = rte_malloc(NULL, sizeof(*pc), 0);
29199a2dd95SBruce Richardson 	if (pc == NULL)
29299a2dd95SBruce Richardson 		return -ENOMEM;
29399a2dd95SBruce Richardson 	*pc = *port_config;
29499a2dd95SBruce Richardson 	ret = rte_event_crypto_adapter_create_ext(id, dev_id,
29599a2dd95SBruce Richardson 						  eca_default_config_cb,
29699a2dd95SBruce Richardson 						  mode,
29799a2dd95SBruce Richardson 						  pc);
29899a2dd95SBruce Richardson 	if (ret)
29999a2dd95SBruce Richardson 		rte_free(pc);
30099a2dd95SBruce Richardson 
30199a2dd95SBruce Richardson 	return ret;
30299a2dd95SBruce Richardson }
30399a2dd95SBruce Richardson 
30499a2dd95SBruce Richardson int
30599a2dd95SBruce Richardson rte_event_crypto_adapter_free(uint8_t id)
30699a2dd95SBruce Richardson {
307*a256a743SPavan Nikhilesh 	struct event_crypto_adapter *adapter;
30899a2dd95SBruce Richardson 
30999a2dd95SBruce Richardson 	EVENT_CRYPTO_ADAPTER_ID_VALID_OR_ERR_RET(id, -EINVAL);
31099a2dd95SBruce Richardson 
31199a2dd95SBruce Richardson 	adapter = eca_id_to_adapter(id);
31299a2dd95SBruce Richardson 	if (adapter == NULL)
31399a2dd95SBruce Richardson 		return -EINVAL;
31499a2dd95SBruce Richardson 
31599a2dd95SBruce Richardson 	if (adapter->nb_qps) {
31699a2dd95SBruce Richardson 		RTE_EDEV_LOG_ERR("%" PRIu16 "Queue pairs not deleted",
31799a2dd95SBruce Richardson 				adapter->nb_qps);
31899a2dd95SBruce Richardson 		return -EBUSY;
31999a2dd95SBruce Richardson 	}
32099a2dd95SBruce Richardson 
32199a2dd95SBruce Richardson 	rte_eventdev_trace_crypto_adapter_free(id, adapter);
32299a2dd95SBruce Richardson 	if (adapter->default_cb_arg)
32399a2dd95SBruce Richardson 		rte_free(adapter->conf_arg);
32499a2dd95SBruce Richardson 	rte_free(adapter->cdevs);
32599a2dd95SBruce Richardson 	rte_free(adapter);
32699a2dd95SBruce Richardson 	event_crypto_adapter[id] = NULL;
32799a2dd95SBruce Richardson 
32899a2dd95SBruce Richardson 	return 0;
32999a2dd95SBruce Richardson }
33099a2dd95SBruce Richardson 
33199a2dd95SBruce Richardson static inline unsigned int
332*a256a743SPavan Nikhilesh eca_enq_to_cryptodev(struct event_crypto_adapter *adapter, struct rte_event *ev,
333*a256a743SPavan Nikhilesh 		     unsigned int cnt)
33499a2dd95SBruce Richardson {
33599a2dd95SBruce Richardson 	struct rte_event_crypto_adapter_stats *stats = &adapter->crypto_stats;
33699a2dd95SBruce Richardson 	union rte_event_crypto_metadata *m_data = NULL;
33799a2dd95SBruce Richardson 	struct crypto_queue_pair_info *qp_info = NULL;
33899a2dd95SBruce Richardson 	struct rte_crypto_op *crypto_op;
33999a2dd95SBruce Richardson 	unsigned int i, n;
34099a2dd95SBruce Richardson 	uint16_t qp_id, len, ret;
34199a2dd95SBruce Richardson 	uint8_t cdev_id;
34299a2dd95SBruce Richardson 
34399a2dd95SBruce Richardson 	len = 0;
34499a2dd95SBruce Richardson 	ret = 0;
34599a2dd95SBruce Richardson 	n = 0;
34699a2dd95SBruce Richardson 	stats->event_deq_count += cnt;
34799a2dd95SBruce Richardson 
34899a2dd95SBruce Richardson 	for (i = 0; i < cnt; i++) {
34999a2dd95SBruce Richardson 		crypto_op = ev[i].event_ptr;
35099a2dd95SBruce Richardson 		if (crypto_op == NULL)
35199a2dd95SBruce Richardson 			continue;
35299a2dd95SBruce Richardson 		if (crypto_op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) {
35399a2dd95SBruce Richardson 			m_data = rte_cryptodev_sym_session_get_user_data(
35499a2dd95SBruce Richardson 					crypto_op->sym->session);
35599a2dd95SBruce Richardson 			if (m_data == NULL) {
35699a2dd95SBruce Richardson 				rte_pktmbuf_free(crypto_op->sym->m_src);
35799a2dd95SBruce Richardson 				rte_crypto_op_free(crypto_op);
35899a2dd95SBruce Richardson 				continue;
35999a2dd95SBruce Richardson 			}
36099a2dd95SBruce Richardson 
36199a2dd95SBruce Richardson 			cdev_id = m_data->request_info.cdev_id;
36299a2dd95SBruce Richardson 			qp_id = m_data->request_info.queue_pair_id;
36399a2dd95SBruce Richardson 			qp_info = &adapter->cdevs[cdev_id].qpairs[qp_id];
36499a2dd95SBruce Richardson 			if (!qp_info->qp_enabled) {
36599a2dd95SBruce Richardson 				rte_pktmbuf_free(crypto_op->sym->m_src);
36699a2dd95SBruce Richardson 				rte_crypto_op_free(crypto_op);
36799a2dd95SBruce Richardson 				continue;
36899a2dd95SBruce Richardson 			}
36999a2dd95SBruce Richardson 			len = qp_info->len;
37099a2dd95SBruce Richardson 			qp_info->op_buffer[len] = crypto_op;
37199a2dd95SBruce Richardson 			len++;
37299a2dd95SBruce Richardson 		} else if (crypto_op->sess_type == RTE_CRYPTO_OP_SESSIONLESS &&
37399a2dd95SBruce Richardson 				crypto_op->private_data_offset) {
37499a2dd95SBruce Richardson 			m_data = (union rte_event_crypto_metadata *)
37599a2dd95SBruce Richardson 				 ((uint8_t *)crypto_op +
37699a2dd95SBruce Richardson 					crypto_op->private_data_offset);
37799a2dd95SBruce Richardson 			cdev_id = m_data->request_info.cdev_id;
37899a2dd95SBruce Richardson 			qp_id = m_data->request_info.queue_pair_id;
37999a2dd95SBruce Richardson 			qp_info = &adapter->cdevs[cdev_id].qpairs[qp_id];
38099a2dd95SBruce Richardson 			if (!qp_info->qp_enabled) {
38199a2dd95SBruce Richardson 				rte_pktmbuf_free(crypto_op->sym->m_src);
38299a2dd95SBruce Richardson 				rte_crypto_op_free(crypto_op);
38399a2dd95SBruce Richardson 				continue;
38499a2dd95SBruce Richardson 			}
38599a2dd95SBruce Richardson 			len = qp_info->len;
38699a2dd95SBruce Richardson 			qp_info->op_buffer[len] = crypto_op;
38799a2dd95SBruce Richardson 			len++;
38899a2dd95SBruce Richardson 		} else {
38999a2dd95SBruce Richardson 			rte_pktmbuf_free(crypto_op->sym->m_src);
39099a2dd95SBruce Richardson 			rte_crypto_op_free(crypto_op);
39199a2dd95SBruce Richardson 			continue;
39299a2dd95SBruce Richardson 		}
39399a2dd95SBruce Richardson 
39499a2dd95SBruce Richardson 		if (len == BATCH_SIZE) {
39599a2dd95SBruce Richardson 			struct rte_crypto_op **op_buffer = qp_info->op_buffer;
39699a2dd95SBruce Richardson 			ret = rte_cryptodev_enqueue_burst(cdev_id,
39799a2dd95SBruce Richardson 							  qp_id,
39899a2dd95SBruce Richardson 							  op_buffer,
39999a2dd95SBruce Richardson 							  BATCH_SIZE);
40099a2dd95SBruce Richardson 
40199a2dd95SBruce Richardson 			stats->crypto_enq_count += ret;
40299a2dd95SBruce Richardson 
40399a2dd95SBruce Richardson 			while (ret < len) {
40499a2dd95SBruce Richardson 				struct rte_crypto_op *op;
40599a2dd95SBruce Richardson 				op = op_buffer[ret++];
40699a2dd95SBruce Richardson 				stats->crypto_enq_fail++;
40799a2dd95SBruce Richardson 				rte_pktmbuf_free(op->sym->m_src);
40899a2dd95SBruce Richardson 				rte_crypto_op_free(op);
40999a2dd95SBruce Richardson 			}
41099a2dd95SBruce Richardson 
41199a2dd95SBruce Richardson 			len = 0;
41299a2dd95SBruce Richardson 		}
41399a2dd95SBruce Richardson 
41499a2dd95SBruce Richardson 		if (qp_info)
41599a2dd95SBruce Richardson 			qp_info->len = len;
41699a2dd95SBruce Richardson 		n += ret;
41799a2dd95SBruce Richardson 	}
41899a2dd95SBruce Richardson 
41999a2dd95SBruce Richardson 	return n;
42099a2dd95SBruce Richardson }
42199a2dd95SBruce Richardson 
42299a2dd95SBruce Richardson static unsigned int
423*a256a743SPavan Nikhilesh eca_crypto_enq_flush(struct event_crypto_adapter *adapter)
42499a2dd95SBruce Richardson {
42599a2dd95SBruce Richardson 	struct rte_event_crypto_adapter_stats *stats = &adapter->crypto_stats;
42699a2dd95SBruce Richardson 	struct crypto_device_info *curr_dev;
42799a2dd95SBruce Richardson 	struct crypto_queue_pair_info *curr_queue;
42899a2dd95SBruce Richardson 	struct rte_crypto_op **op_buffer;
42999a2dd95SBruce Richardson 	struct rte_cryptodev *dev;
43099a2dd95SBruce Richardson 	uint8_t cdev_id;
43199a2dd95SBruce Richardson 	uint16_t qp;
43299a2dd95SBruce Richardson 	uint16_t ret;
43399a2dd95SBruce Richardson 	uint16_t num_cdev = rte_cryptodev_count();
43499a2dd95SBruce Richardson 
43599a2dd95SBruce Richardson 	ret = 0;
43699a2dd95SBruce Richardson 	for (cdev_id = 0; cdev_id < num_cdev; cdev_id++) {
43799a2dd95SBruce Richardson 		curr_dev = &adapter->cdevs[cdev_id];
43899a2dd95SBruce Richardson 		dev = curr_dev->dev;
43999a2dd95SBruce Richardson 		if (dev == NULL)
44099a2dd95SBruce Richardson 			continue;
44199a2dd95SBruce Richardson 		for (qp = 0; qp < dev->data->nb_queue_pairs; qp++) {
44299a2dd95SBruce Richardson 
44399a2dd95SBruce Richardson 			curr_queue = &curr_dev->qpairs[qp];
44499a2dd95SBruce Richardson 			if (!curr_queue->qp_enabled)
44599a2dd95SBruce Richardson 				continue;
44699a2dd95SBruce Richardson 
44799a2dd95SBruce Richardson 			op_buffer = curr_queue->op_buffer;
44899a2dd95SBruce Richardson 			ret = rte_cryptodev_enqueue_burst(cdev_id,
44999a2dd95SBruce Richardson 							  qp,
45099a2dd95SBruce Richardson 							  op_buffer,
45199a2dd95SBruce Richardson 							  curr_queue->len);
45299a2dd95SBruce Richardson 			stats->crypto_enq_count += ret;
45399a2dd95SBruce Richardson 
45499a2dd95SBruce Richardson 			while (ret < curr_queue->len) {
45599a2dd95SBruce Richardson 				struct rte_crypto_op *op;
45699a2dd95SBruce Richardson 				op = op_buffer[ret++];
45799a2dd95SBruce Richardson 				stats->crypto_enq_fail++;
45899a2dd95SBruce Richardson 				rte_pktmbuf_free(op->sym->m_src);
45999a2dd95SBruce Richardson 				rte_crypto_op_free(op);
46099a2dd95SBruce Richardson 			}
46199a2dd95SBruce Richardson 			curr_queue->len = 0;
46299a2dd95SBruce Richardson 		}
46399a2dd95SBruce Richardson 	}
46499a2dd95SBruce Richardson 
46599a2dd95SBruce Richardson 	return ret;
46699a2dd95SBruce Richardson }
46799a2dd95SBruce Richardson 
46899a2dd95SBruce Richardson static int
469*a256a743SPavan Nikhilesh eca_crypto_adapter_enq_run(struct event_crypto_adapter *adapter,
47099a2dd95SBruce Richardson 			   unsigned int max_enq)
47199a2dd95SBruce Richardson {
47299a2dd95SBruce Richardson 	struct rte_event_crypto_adapter_stats *stats = &adapter->crypto_stats;
47399a2dd95SBruce Richardson 	struct rte_event ev[BATCH_SIZE];
47499a2dd95SBruce Richardson 	unsigned int nb_enq, nb_enqueued;
47599a2dd95SBruce Richardson 	uint16_t n;
47699a2dd95SBruce Richardson 	uint8_t event_dev_id = adapter->eventdev_id;
47799a2dd95SBruce Richardson 	uint8_t event_port_id = adapter->event_port_id;
47899a2dd95SBruce Richardson 
47999a2dd95SBruce Richardson 	nb_enqueued = 0;
48099a2dd95SBruce Richardson 	if (adapter->mode == RTE_EVENT_CRYPTO_ADAPTER_OP_NEW)
48199a2dd95SBruce Richardson 		return 0;
48299a2dd95SBruce Richardson 
48399a2dd95SBruce Richardson 	for (nb_enq = 0; nb_enq < max_enq; nb_enq += n) {
48499a2dd95SBruce Richardson 		stats->event_poll_count++;
48599a2dd95SBruce Richardson 		n = rte_event_dequeue_burst(event_dev_id,
48699a2dd95SBruce Richardson 					    event_port_id, ev, BATCH_SIZE, 0);
48799a2dd95SBruce Richardson 
48899a2dd95SBruce Richardson 		if (!n)
48999a2dd95SBruce Richardson 			break;
49099a2dd95SBruce Richardson 
49199a2dd95SBruce Richardson 		nb_enqueued += eca_enq_to_cryptodev(adapter, ev, n);
49299a2dd95SBruce Richardson 	}
49399a2dd95SBruce Richardson 
49499a2dd95SBruce Richardson 	if ((++adapter->transmit_loop_count &
49599a2dd95SBruce Richardson 		(CRYPTO_ENQ_FLUSH_THRESHOLD - 1)) == 0) {
49699a2dd95SBruce Richardson 		nb_enqueued += eca_crypto_enq_flush(adapter);
49799a2dd95SBruce Richardson 	}
49899a2dd95SBruce Richardson 
49999a2dd95SBruce Richardson 	return nb_enqueued;
50099a2dd95SBruce Richardson }
50199a2dd95SBruce Richardson 
50299a2dd95SBruce Richardson static inline void
503*a256a743SPavan Nikhilesh eca_ops_enqueue_burst(struct event_crypto_adapter *adapter,
50499a2dd95SBruce Richardson 		      struct rte_crypto_op **ops, uint16_t num)
50599a2dd95SBruce Richardson {
50699a2dd95SBruce Richardson 	struct rte_event_crypto_adapter_stats *stats = &adapter->crypto_stats;
50799a2dd95SBruce Richardson 	union rte_event_crypto_metadata *m_data = NULL;
50899a2dd95SBruce Richardson 	uint8_t event_dev_id = adapter->eventdev_id;
50999a2dd95SBruce Richardson 	uint8_t event_port_id = adapter->event_port_id;
51099a2dd95SBruce Richardson 	struct rte_event events[BATCH_SIZE];
51199a2dd95SBruce Richardson 	uint16_t nb_enqueued, nb_ev;
51299a2dd95SBruce Richardson 	uint8_t retry;
51399a2dd95SBruce Richardson 	uint8_t i;
51499a2dd95SBruce Richardson 
51599a2dd95SBruce Richardson 	nb_ev = 0;
51699a2dd95SBruce Richardson 	retry = 0;
51799a2dd95SBruce Richardson 	nb_enqueued = 0;
51899a2dd95SBruce Richardson 	num = RTE_MIN(num, BATCH_SIZE);
51999a2dd95SBruce Richardson 	for (i = 0; i < num; i++) {
52099a2dd95SBruce Richardson 		struct rte_event *ev = &events[nb_ev++];
52199a2dd95SBruce Richardson 		if (ops[i]->sess_type == RTE_CRYPTO_OP_WITH_SESSION) {
52299a2dd95SBruce Richardson 			m_data = rte_cryptodev_sym_session_get_user_data(
52399a2dd95SBruce Richardson 					ops[i]->sym->session);
52499a2dd95SBruce Richardson 		} else if (ops[i]->sess_type == RTE_CRYPTO_OP_SESSIONLESS &&
52599a2dd95SBruce Richardson 				ops[i]->private_data_offset) {
52699a2dd95SBruce Richardson 			m_data = (union rte_event_crypto_metadata *)
52799a2dd95SBruce Richardson 				 ((uint8_t *)ops[i] +
52899a2dd95SBruce Richardson 				  ops[i]->private_data_offset);
52999a2dd95SBruce Richardson 		}
53099a2dd95SBruce Richardson 
53199a2dd95SBruce Richardson 		if (unlikely(m_data == NULL)) {
53299a2dd95SBruce Richardson 			rte_pktmbuf_free(ops[i]->sym->m_src);
53399a2dd95SBruce Richardson 			rte_crypto_op_free(ops[i]);
53499a2dd95SBruce Richardson 			continue;
53599a2dd95SBruce Richardson 		}
53699a2dd95SBruce Richardson 
53799a2dd95SBruce Richardson 		rte_memcpy(ev, &m_data->response_info, sizeof(*ev));
53899a2dd95SBruce Richardson 		ev->event_ptr = ops[i];
53999a2dd95SBruce Richardson 		ev->event_type = RTE_EVENT_TYPE_CRYPTODEV;
54099a2dd95SBruce Richardson 		if (adapter->implicit_release_disabled)
54199a2dd95SBruce Richardson 			ev->op = RTE_EVENT_OP_FORWARD;
54299a2dd95SBruce Richardson 		else
54399a2dd95SBruce Richardson 			ev->op = RTE_EVENT_OP_NEW;
54499a2dd95SBruce Richardson 	}
54599a2dd95SBruce Richardson 
54699a2dd95SBruce Richardson 	do {
54799a2dd95SBruce Richardson 		nb_enqueued += rte_event_enqueue_burst(event_dev_id,
54899a2dd95SBruce Richardson 						  event_port_id,
54999a2dd95SBruce Richardson 						  &events[nb_enqueued],
55099a2dd95SBruce Richardson 						  nb_ev - nb_enqueued);
55199a2dd95SBruce Richardson 	} while (retry++ < CRYPTO_ADAPTER_MAX_EV_ENQ_RETRIES &&
55299a2dd95SBruce Richardson 		 nb_enqueued < nb_ev);
55399a2dd95SBruce Richardson 
55499a2dd95SBruce Richardson 	/* Free mbufs and rte_crypto_ops for failed events */
55599a2dd95SBruce Richardson 	for (i = nb_enqueued; i < nb_ev; i++) {
55699a2dd95SBruce Richardson 		struct rte_crypto_op *op = events[i].event_ptr;
55799a2dd95SBruce Richardson 		rte_pktmbuf_free(op->sym->m_src);
55899a2dd95SBruce Richardson 		rte_crypto_op_free(op);
55999a2dd95SBruce Richardson 	}
56099a2dd95SBruce Richardson 
56199a2dd95SBruce Richardson 	stats->event_enq_fail_count += nb_ev - nb_enqueued;
56299a2dd95SBruce Richardson 	stats->event_enq_count += nb_enqueued;
56399a2dd95SBruce Richardson 	stats->event_enq_retry_count += retry - 1;
56499a2dd95SBruce Richardson }
56599a2dd95SBruce Richardson 
56699a2dd95SBruce Richardson static inline unsigned int
567*a256a743SPavan Nikhilesh eca_crypto_adapter_deq_run(struct event_crypto_adapter *adapter,
56899a2dd95SBruce Richardson 			   unsigned int max_deq)
56999a2dd95SBruce Richardson {
57099a2dd95SBruce Richardson 	struct rte_event_crypto_adapter_stats *stats = &adapter->crypto_stats;
57199a2dd95SBruce Richardson 	struct crypto_device_info *curr_dev;
57299a2dd95SBruce Richardson 	struct crypto_queue_pair_info *curr_queue;
57399a2dd95SBruce Richardson 	struct rte_crypto_op *ops[BATCH_SIZE];
57499a2dd95SBruce Richardson 	uint16_t n, nb_deq;
57599a2dd95SBruce Richardson 	struct rte_cryptodev *dev;
57699a2dd95SBruce Richardson 	uint8_t cdev_id;
57799a2dd95SBruce Richardson 	uint16_t qp, dev_qps;
57899a2dd95SBruce Richardson 	bool done;
57999a2dd95SBruce Richardson 	uint16_t num_cdev = rte_cryptodev_count();
58099a2dd95SBruce Richardson 
58199a2dd95SBruce Richardson 	nb_deq = 0;
58299a2dd95SBruce Richardson 	do {
58399a2dd95SBruce Richardson 		uint16_t queues = 0;
58499a2dd95SBruce Richardson 		done = true;
58599a2dd95SBruce Richardson 
58699a2dd95SBruce Richardson 		for (cdev_id = adapter->next_cdev_id;
58799a2dd95SBruce Richardson 			cdev_id < num_cdev; cdev_id++) {
58899a2dd95SBruce Richardson 			curr_dev = &adapter->cdevs[cdev_id];
58999a2dd95SBruce Richardson 			dev = curr_dev->dev;
59099a2dd95SBruce Richardson 			if (dev == NULL)
59199a2dd95SBruce Richardson 				continue;
59299a2dd95SBruce Richardson 			dev_qps = dev->data->nb_queue_pairs;
59399a2dd95SBruce Richardson 
59499a2dd95SBruce Richardson 			for (qp = curr_dev->next_queue_pair_id;
59599a2dd95SBruce Richardson 				queues < dev_qps; qp = (qp + 1) % dev_qps,
59699a2dd95SBruce Richardson 				queues++) {
59799a2dd95SBruce Richardson 
59899a2dd95SBruce Richardson 				curr_queue = &curr_dev->qpairs[qp];
59999a2dd95SBruce Richardson 				if (!curr_queue->qp_enabled)
60099a2dd95SBruce Richardson 					continue;
60199a2dd95SBruce Richardson 
60299a2dd95SBruce Richardson 				n = rte_cryptodev_dequeue_burst(cdev_id, qp,
60399a2dd95SBruce Richardson 					ops, BATCH_SIZE);
60499a2dd95SBruce Richardson 				if (!n)
60599a2dd95SBruce Richardson 					continue;
60699a2dd95SBruce Richardson 
60799a2dd95SBruce Richardson 				done = false;
60899a2dd95SBruce Richardson 				stats->crypto_deq_count += n;
60999a2dd95SBruce Richardson 				eca_ops_enqueue_burst(adapter, ops, n);
61099a2dd95SBruce Richardson 				nb_deq += n;
61199a2dd95SBruce Richardson 
61299a2dd95SBruce Richardson 				if (nb_deq > max_deq) {
61399a2dd95SBruce Richardson 					if ((qp + 1) == dev_qps) {
61499a2dd95SBruce Richardson 						adapter->next_cdev_id =
61599a2dd95SBruce Richardson 							(cdev_id + 1)
61699a2dd95SBruce Richardson 							% num_cdev;
61799a2dd95SBruce Richardson 					}
61899a2dd95SBruce Richardson 					curr_dev->next_queue_pair_id = (qp + 1)
61999a2dd95SBruce Richardson 						% dev->data->nb_queue_pairs;
62099a2dd95SBruce Richardson 
62199a2dd95SBruce Richardson 					return nb_deq;
62299a2dd95SBruce Richardson 				}
62399a2dd95SBruce Richardson 			}
62499a2dd95SBruce Richardson 		}
62599a2dd95SBruce Richardson 	} while (done == false);
62699a2dd95SBruce Richardson 	return nb_deq;
62799a2dd95SBruce Richardson }
62899a2dd95SBruce Richardson 
62999a2dd95SBruce Richardson static void
630*a256a743SPavan Nikhilesh eca_crypto_adapter_run(struct event_crypto_adapter *adapter,
63199a2dd95SBruce Richardson 		       unsigned int max_ops)
63299a2dd95SBruce Richardson {
63399a2dd95SBruce Richardson 	while (max_ops) {
63499a2dd95SBruce Richardson 		unsigned int e_cnt, d_cnt;
63599a2dd95SBruce Richardson 
63699a2dd95SBruce Richardson 		e_cnt = eca_crypto_adapter_deq_run(adapter, max_ops);
63799a2dd95SBruce Richardson 		max_ops -= RTE_MIN(max_ops, e_cnt);
63899a2dd95SBruce Richardson 
63999a2dd95SBruce Richardson 		d_cnt = eca_crypto_adapter_enq_run(adapter, max_ops);
64099a2dd95SBruce Richardson 		max_ops -= RTE_MIN(max_ops, d_cnt);
64199a2dd95SBruce Richardson 
64299a2dd95SBruce Richardson 		if (e_cnt == 0 && d_cnt == 0)
64399a2dd95SBruce Richardson 			break;
64499a2dd95SBruce Richardson 
64599a2dd95SBruce Richardson 	}
64699a2dd95SBruce Richardson }
64799a2dd95SBruce Richardson 
64899a2dd95SBruce Richardson static int
64999a2dd95SBruce Richardson eca_service_func(void *args)
65099a2dd95SBruce Richardson {
651*a256a743SPavan Nikhilesh 	struct event_crypto_adapter *adapter = args;
65299a2dd95SBruce Richardson 
65399a2dd95SBruce Richardson 	if (rte_spinlock_trylock(&adapter->lock) == 0)
65499a2dd95SBruce Richardson 		return 0;
65599a2dd95SBruce Richardson 	eca_crypto_adapter_run(adapter, adapter->max_nb);
65699a2dd95SBruce Richardson 	rte_spinlock_unlock(&adapter->lock);
65799a2dd95SBruce Richardson 
65899a2dd95SBruce Richardson 	return 0;
65999a2dd95SBruce Richardson }
66099a2dd95SBruce Richardson 
66199a2dd95SBruce Richardson static int
662*a256a743SPavan Nikhilesh eca_init_service(struct event_crypto_adapter *adapter, uint8_t id)
66399a2dd95SBruce Richardson {
66499a2dd95SBruce Richardson 	struct rte_event_crypto_adapter_conf adapter_conf;
66599a2dd95SBruce Richardson 	struct rte_service_spec service;
66699a2dd95SBruce Richardson 	int ret;
66799a2dd95SBruce Richardson 
66899a2dd95SBruce Richardson 	if (adapter->service_inited)
66999a2dd95SBruce Richardson 		return 0;
67099a2dd95SBruce Richardson 
67199a2dd95SBruce Richardson 	memset(&service, 0, sizeof(service));
67299a2dd95SBruce Richardson 	snprintf(service.name, CRYPTO_ADAPTER_NAME_LEN,
67399a2dd95SBruce Richardson 		"rte_event_crypto_adapter_%d", id);
67499a2dd95SBruce Richardson 	service.socket_id = adapter->socket_id;
67599a2dd95SBruce Richardson 	service.callback = eca_service_func;
67699a2dd95SBruce Richardson 	service.callback_userdata = adapter;
67799a2dd95SBruce Richardson 	/* Service function handles locking for queue add/del updates */
67899a2dd95SBruce Richardson 	service.capabilities = RTE_SERVICE_CAP_MT_SAFE;
67999a2dd95SBruce Richardson 	ret = rte_service_component_register(&service, &adapter->service_id);
68099a2dd95SBruce Richardson 	if (ret) {
68199a2dd95SBruce Richardson 		RTE_EDEV_LOG_ERR("failed to register service %s err = %" PRId32,
68299a2dd95SBruce Richardson 			service.name, ret);
68399a2dd95SBruce Richardson 		return ret;
68499a2dd95SBruce Richardson 	}
68599a2dd95SBruce Richardson 
68699a2dd95SBruce Richardson 	ret = adapter->conf_cb(id, adapter->eventdev_id,
68799a2dd95SBruce Richardson 		&adapter_conf, adapter->conf_arg);
68899a2dd95SBruce Richardson 	if (ret) {
68999a2dd95SBruce Richardson 		RTE_EDEV_LOG_ERR("configuration callback failed err = %" PRId32,
69099a2dd95SBruce Richardson 			ret);
69199a2dd95SBruce Richardson 		return ret;
69299a2dd95SBruce Richardson 	}
69399a2dd95SBruce Richardson 
69499a2dd95SBruce Richardson 	adapter->max_nb = adapter_conf.max_nb;
69599a2dd95SBruce Richardson 	adapter->event_port_id = adapter_conf.event_port_id;
69699a2dd95SBruce Richardson 	adapter->service_inited = 1;
69799a2dd95SBruce Richardson 
69899a2dd95SBruce Richardson 	return ret;
69999a2dd95SBruce Richardson }
70099a2dd95SBruce Richardson 
70199a2dd95SBruce Richardson static void
702*a256a743SPavan Nikhilesh eca_update_qp_info(struct event_crypto_adapter *adapter,
703*a256a743SPavan Nikhilesh 		   struct crypto_device_info *dev_info, int32_t queue_pair_id,
70499a2dd95SBruce Richardson 		   uint8_t add)
70599a2dd95SBruce Richardson {
70699a2dd95SBruce Richardson 	struct crypto_queue_pair_info *qp_info;
70799a2dd95SBruce Richardson 	int enabled;
70899a2dd95SBruce Richardson 	uint16_t i;
70999a2dd95SBruce Richardson 
71099a2dd95SBruce Richardson 	if (dev_info->qpairs == NULL)
71199a2dd95SBruce Richardson 		return;
71299a2dd95SBruce Richardson 
71399a2dd95SBruce Richardson 	if (queue_pair_id == -1) {
71499a2dd95SBruce Richardson 		for (i = 0; i < dev_info->dev->data->nb_queue_pairs; i++)
71599a2dd95SBruce Richardson 			eca_update_qp_info(adapter, dev_info, i, add);
71699a2dd95SBruce Richardson 	} else {
71799a2dd95SBruce Richardson 		qp_info = &dev_info->qpairs[queue_pair_id];
71899a2dd95SBruce Richardson 		enabled = qp_info->qp_enabled;
71999a2dd95SBruce Richardson 		if (add) {
72099a2dd95SBruce Richardson 			adapter->nb_qps += !enabled;
72199a2dd95SBruce Richardson 			dev_info->num_qpairs += !enabled;
72299a2dd95SBruce Richardson 		} else {
72399a2dd95SBruce Richardson 			adapter->nb_qps -= enabled;
72499a2dd95SBruce Richardson 			dev_info->num_qpairs -= enabled;
72599a2dd95SBruce Richardson 		}
72699a2dd95SBruce Richardson 		qp_info->qp_enabled = !!add;
72799a2dd95SBruce Richardson 	}
72899a2dd95SBruce Richardson }
72999a2dd95SBruce Richardson 
73099a2dd95SBruce Richardson static int
731*a256a743SPavan Nikhilesh eca_add_queue_pair(struct event_crypto_adapter *adapter, uint8_t cdev_id,
73299a2dd95SBruce Richardson 		   int queue_pair_id)
73399a2dd95SBruce Richardson {
73499a2dd95SBruce Richardson 	struct crypto_device_info *dev_info = &adapter->cdevs[cdev_id];
73599a2dd95SBruce Richardson 	struct crypto_queue_pair_info *qpairs;
73699a2dd95SBruce Richardson 	uint32_t i;
73799a2dd95SBruce Richardson 
73899a2dd95SBruce Richardson 	if (dev_info->qpairs == NULL) {
73999a2dd95SBruce Richardson 		dev_info->qpairs =
74099a2dd95SBruce Richardson 		    rte_zmalloc_socket(adapter->mem_name,
74199a2dd95SBruce Richardson 					dev_info->dev->data->nb_queue_pairs *
74299a2dd95SBruce Richardson 					sizeof(struct crypto_queue_pair_info),
74399a2dd95SBruce Richardson 					0, adapter->socket_id);
74499a2dd95SBruce Richardson 		if (dev_info->qpairs == NULL)
74599a2dd95SBruce Richardson 			return -ENOMEM;
74699a2dd95SBruce Richardson 
74799a2dd95SBruce Richardson 		qpairs = dev_info->qpairs;
74899a2dd95SBruce Richardson 		qpairs->op_buffer = rte_zmalloc_socket(adapter->mem_name,
74999a2dd95SBruce Richardson 					BATCH_SIZE *
75099a2dd95SBruce Richardson 					sizeof(struct rte_crypto_op *),
75199a2dd95SBruce Richardson 					0, adapter->socket_id);
75299a2dd95SBruce Richardson 		if (!qpairs->op_buffer) {
75399a2dd95SBruce Richardson 			rte_free(qpairs);
75499a2dd95SBruce Richardson 			return -ENOMEM;
75599a2dd95SBruce Richardson 		}
75699a2dd95SBruce Richardson 	}
75799a2dd95SBruce Richardson 
75899a2dd95SBruce Richardson 	if (queue_pair_id == -1) {
75999a2dd95SBruce Richardson 		for (i = 0; i < dev_info->dev->data->nb_queue_pairs; i++)
76099a2dd95SBruce Richardson 			eca_update_qp_info(adapter, dev_info, i, 1);
76199a2dd95SBruce Richardson 	} else
76299a2dd95SBruce Richardson 		eca_update_qp_info(adapter, dev_info,
76399a2dd95SBruce Richardson 					(uint16_t)queue_pair_id, 1);
76499a2dd95SBruce Richardson 
76599a2dd95SBruce Richardson 	return 0;
76699a2dd95SBruce Richardson }
76799a2dd95SBruce Richardson 
76899a2dd95SBruce Richardson int
76999a2dd95SBruce Richardson rte_event_crypto_adapter_queue_pair_add(uint8_t id,
77099a2dd95SBruce Richardson 			uint8_t cdev_id,
77199a2dd95SBruce Richardson 			int32_t queue_pair_id,
77299a2dd95SBruce Richardson 			const struct rte_event *event)
77399a2dd95SBruce Richardson {
774*a256a743SPavan Nikhilesh 	struct event_crypto_adapter *adapter;
77599a2dd95SBruce Richardson 	struct rte_eventdev *dev;
77699a2dd95SBruce Richardson 	struct crypto_device_info *dev_info;
77799a2dd95SBruce Richardson 	uint32_t cap;
77899a2dd95SBruce Richardson 	int ret;
77999a2dd95SBruce Richardson 
78099a2dd95SBruce Richardson 	EVENT_CRYPTO_ADAPTER_ID_VALID_OR_ERR_RET(id, -EINVAL);
78199a2dd95SBruce Richardson 
782e74abd48SAkhil Goyal 	if (!rte_cryptodev_is_valid_dev(cdev_id)) {
78399a2dd95SBruce Richardson 		RTE_EDEV_LOG_ERR("Invalid dev_id=%" PRIu8, cdev_id);
78499a2dd95SBruce Richardson 		return -EINVAL;
78599a2dd95SBruce Richardson 	}
78699a2dd95SBruce Richardson 
78799a2dd95SBruce Richardson 	adapter = eca_id_to_adapter(id);
78899a2dd95SBruce Richardson 	if (adapter == NULL)
78999a2dd95SBruce Richardson 		return -EINVAL;
79099a2dd95SBruce Richardson 
79199a2dd95SBruce Richardson 	dev = &rte_eventdevs[adapter->eventdev_id];
79299a2dd95SBruce Richardson 	ret = rte_event_crypto_adapter_caps_get(adapter->eventdev_id,
79399a2dd95SBruce Richardson 						cdev_id,
79499a2dd95SBruce Richardson 						&cap);
79599a2dd95SBruce Richardson 	if (ret) {
79699a2dd95SBruce Richardson 		RTE_EDEV_LOG_ERR("Failed to get adapter caps dev %" PRIu8
79799a2dd95SBruce Richardson 			" cdev %" PRIu8, id, cdev_id);
79899a2dd95SBruce Richardson 		return ret;
79999a2dd95SBruce Richardson 	}
80099a2dd95SBruce Richardson 
80199a2dd95SBruce Richardson 	if ((cap & RTE_EVENT_CRYPTO_ADAPTER_CAP_INTERNAL_PORT_QP_EV_BIND) &&
80299a2dd95SBruce Richardson 	    (event == NULL)) {
80399a2dd95SBruce Richardson 		RTE_EDEV_LOG_ERR("Conf value can not be NULL for dev_id=%u",
80499a2dd95SBruce Richardson 				  cdev_id);
80599a2dd95SBruce Richardson 		return -EINVAL;
80699a2dd95SBruce Richardson 	}
80799a2dd95SBruce Richardson 
80899a2dd95SBruce Richardson 	dev_info = &adapter->cdevs[cdev_id];
80999a2dd95SBruce Richardson 
81099a2dd95SBruce Richardson 	if (queue_pair_id != -1 &&
81199a2dd95SBruce Richardson 	    (uint16_t)queue_pair_id >= dev_info->dev->data->nb_queue_pairs) {
81299a2dd95SBruce Richardson 		RTE_EDEV_LOG_ERR("Invalid queue_pair_id %" PRIu16,
81399a2dd95SBruce Richardson 				 (uint16_t)queue_pair_id);
81499a2dd95SBruce Richardson 		return -EINVAL;
81599a2dd95SBruce Richardson 	}
81699a2dd95SBruce Richardson 
81799a2dd95SBruce Richardson 	/* In case HW cap is RTE_EVENT_CRYPTO_ADAPTER_CAP_INTERNAL_PORT_OP_FWD,
81899a2dd95SBruce Richardson 	 * no need of service core as HW supports event forward capability.
81999a2dd95SBruce Richardson 	 */
82099a2dd95SBruce Richardson 	if ((cap & RTE_EVENT_CRYPTO_ADAPTER_CAP_INTERNAL_PORT_OP_FWD) ||
82199a2dd95SBruce Richardson 	    (cap & RTE_EVENT_CRYPTO_ADAPTER_CAP_INTERNAL_PORT_QP_EV_BIND &&
82299a2dd95SBruce Richardson 	     adapter->mode == RTE_EVENT_CRYPTO_ADAPTER_OP_NEW) ||
82399a2dd95SBruce Richardson 	    (cap & RTE_EVENT_CRYPTO_ADAPTER_CAP_INTERNAL_PORT_OP_NEW &&
82499a2dd95SBruce Richardson 	     adapter->mode == RTE_EVENT_CRYPTO_ADAPTER_OP_NEW)) {
82599a2dd95SBruce Richardson 		RTE_FUNC_PTR_OR_ERR_RET(
82699a2dd95SBruce Richardson 			*dev->dev_ops->crypto_adapter_queue_pair_add,
82799a2dd95SBruce Richardson 			-ENOTSUP);
82899a2dd95SBruce Richardson 		if (dev_info->qpairs == NULL) {
82999a2dd95SBruce Richardson 			dev_info->qpairs =
83099a2dd95SBruce Richardson 			    rte_zmalloc_socket(adapter->mem_name,
83199a2dd95SBruce Richardson 					dev_info->dev->data->nb_queue_pairs *
83299a2dd95SBruce Richardson 					sizeof(struct crypto_queue_pair_info),
83399a2dd95SBruce Richardson 					0, adapter->socket_id);
83499a2dd95SBruce Richardson 			if (dev_info->qpairs == NULL)
83599a2dd95SBruce Richardson 				return -ENOMEM;
83699a2dd95SBruce Richardson 		}
83799a2dd95SBruce Richardson 
83899a2dd95SBruce Richardson 		ret = (*dev->dev_ops->crypto_adapter_queue_pair_add)(dev,
83999a2dd95SBruce Richardson 				dev_info->dev,
84099a2dd95SBruce Richardson 				queue_pair_id,
84199a2dd95SBruce Richardson 				event);
84299a2dd95SBruce Richardson 		if (ret)
84399a2dd95SBruce Richardson 			return ret;
84499a2dd95SBruce Richardson 
84599a2dd95SBruce Richardson 		else
84699a2dd95SBruce Richardson 			eca_update_qp_info(adapter, &adapter->cdevs[cdev_id],
84799a2dd95SBruce Richardson 					   queue_pair_id, 1);
84899a2dd95SBruce Richardson 	}
84999a2dd95SBruce Richardson 
85099a2dd95SBruce Richardson 	/* In case HW cap is RTE_EVENT_CRYPTO_ADAPTER_CAP_INTERNAL_PORT_OP_NEW,
85199a2dd95SBruce Richardson 	 * or SW adapter, initiate services so the application can choose
85299a2dd95SBruce Richardson 	 * which ever way it wants to use the adapter.
85399a2dd95SBruce Richardson 	 * Case 1: RTE_EVENT_CRYPTO_ADAPTER_CAP_INTERNAL_PORT_OP_NEW
85499a2dd95SBruce Richardson 	 *         Application may wants to use one of below two mode
85599a2dd95SBruce Richardson 	 *          a. OP_FORWARD mode -> HW Dequeue + SW enqueue
85699a2dd95SBruce Richardson 	 *          b. OP_NEW mode -> HW Dequeue
85799a2dd95SBruce Richardson 	 * Case 2: No HW caps, use SW adapter
85899a2dd95SBruce Richardson 	 *          a. OP_FORWARD mode -> SW enqueue & dequeue
85999a2dd95SBruce Richardson 	 *          b. OP_NEW mode -> SW Dequeue
86099a2dd95SBruce Richardson 	 */
86199a2dd95SBruce Richardson 	if ((cap & RTE_EVENT_CRYPTO_ADAPTER_CAP_INTERNAL_PORT_OP_NEW &&
86299a2dd95SBruce Richardson 	     !(cap & RTE_EVENT_CRYPTO_ADAPTER_CAP_INTERNAL_PORT_OP_FWD) &&
86399a2dd95SBruce Richardson 	     adapter->mode == RTE_EVENT_CRYPTO_ADAPTER_OP_FORWARD) ||
86499a2dd95SBruce Richardson 	     (!(cap & RTE_EVENT_CRYPTO_ADAPTER_CAP_INTERNAL_PORT_OP_NEW) &&
86599a2dd95SBruce Richardson 	      !(cap & RTE_EVENT_CRYPTO_ADAPTER_CAP_INTERNAL_PORT_OP_FWD) &&
86699a2dd95SBruce Richardson 	      !(cap & RTE_EVENT_CRYPTO_ADAPTER_CAP_INTERNAL_PORT_QP_EV_BIND) &&
86799a2dd95SBruce Richardson 	       (cap & RTE_EVENT_CRYPTO_ADAPTER_CAP_SESSION_PRIVATE_DATA))) {
86899a2dd95SBruce Richardson 		rte_spinlock_lock(&adapter->lock);
86999a2dd95SBruce Richardson 		ret = eca_init_service(adapter, id);
87099a2dd95SBruce Richardson 		if (ret == 0)
87199a2dd95SBruce Richardson 			ret = eca_add_queue_pair(adapter, cdev_id,
87299a2dd95SBruce Richardson 						 queue_pair_id);
87399a2dd95SBruce Richardson 		rte_spinlock_unlock(&adapter->lock);
87499a2dd95SBruce Richardson 
87599a2dd95SBruce Richardson 		if (ret)
87699a2dd95SBruce Richardson 			return ret;
87799a2dd95SBruce Richardson 
87899a2dd95SBruce Richardson 		rte_service_component_runstate_set(adapter->service_id, 1);
87999a2dd95SBruce Richardson 	}
88099a2dd95SBruce Richardson 
88199a2dd95SBruce Richardson 	rte_eventdev_trace_crypto_adapter_queue_pair_add(id, cdev_id, event,
88299a2dd95SBruce Richardson 		queue_pair_id);
88399a2dd95SBruce Richardson 	return 0;
88499a2dd95SBruce Richardson }
88599a2dd95SBruce Richardson 
88699a2dd95SBruce Richardson int
88799a2dd95SBruce Richardson rte_event_crypto_adapter_queue_pair_del(uint8_t id, uint8_t cdev_id,
88899a2dd95SBruce Richardson 					int32_t queue_pair_id)
88999a2dd95SBruce Richardson {
890*a256a743SPavan Nikhilesh 	struct event_crypto_adapter *adapter;
89199a2dd95SBruce Richardson 	struct crypto_device_info *dev_info;
89299a2dd95SBruce Richardson 	struct rte_eventdev *dev;
89399a2dd95SBruce Richardson 	int ret;
89499a2dd95SBruce Richardson 	uint32_t cap;
89599a2dd95SBruce Richardson 	uint16_t i;
89699a2dd95SBruce Richardson 
89799a2dd95SBruce Richardson 	EVENT_CRYPTO_ADAPTER_ID_VALID_OR_ERR_RET(id, -EINVAL);
89899a2dd95SBruce Richardson 
899e74abd48SAkhil Goyal 	if (!rte_cryptodev_is_valid_dev(cdev_id)) {
90099a2dd95SBruce Richardson 		RTE_EDEV_LOG_ERR("Invalid dev_id=%" PRIu8, cdev_id);
90199a2dd95SBruce Richardson 		return -EINVAL;
90299a2dd95SBruce Richardson 	}
90399a2dd95SBruce Richardson 
90499a2dd95SBruce Richardson 	adapter = eca_id_to_adapter(id);
90599a2dd95SBruce Richardson 	if (adapter == NULL)
90699a2dd95SBruce Richardson 		return -EINVAL;
90799a2dd95SBruce Richardson 
90899a2dd95SBruce Richardson 	dev = &rte_eventdevs[adapter->eventdev_id];
90999a2dd95SBruce Richardson 	ret = rte_event_crypto_adapter_caps_get(adapter->eventdev_id,
91099a2dd95SBruce Richardson 						cdev_id,
91199a2dd95SBruce Richardson 						&cap);
91299a2dd95SBruce Richardson 	if (ret)
91399a2dd95SBruce Richardson 		return ret;
91499a2dd95SBruce Richardson 
91599a2dd95SBruce Richardson 	dev_info = &adapter->cdevs[cdev_id];
91699a2dd95SBruce Richardson 
91799a2dd95SBruce Richardson 	if (queue_pair_id != -1 &&
91899a2dd95SBruce Richardson 	    (uint16_t)queue_pair_id >= dev_info->dev->data->nb_queue_pairs) {
91999a2dd95SBruce Richardson 		RTE_EDEV_LOG_ERR("Invalid queue_pair_id %" PRIu16,
92099a2dd95SBruce Richardson 				 (uint16_t)queue_pair_id);
92199a2dd95SBruce Richardson 		return -EINVAL;
92299a2dd95SBruce Richardson 	}
92399a2dd95SBruce Richardson 
92499a2dd95SBruce Richardson 	if ((cap & RTE_EVENT_CRYPTO_ADAPTER_CAP_INTERNAL_PORT_OP_FWD) ||
92599a2dd95SBruce Richardson 	    (cap & RTE_EVENT_CRYPTO_ADAPTER_CAP_INTERNAL_PORT_OP_NEW &&
92699a2dd95SBruce Richardson 	     adapter->mode == RTE_EVENT_CRYPTO_ADAPTER_OP_NEW)) {
92799a2dd95SBruce Richardson 		RTE_FUNC_PTR_OR_ERR_RET(
92899a2dd95SBruce Richardson 			*dev->dev_ops->crypto_adapter_queue_pair_del,
92999a2dd95SBruce Richardson 			-ENOTSUP);
93099a2dd95SBruce Richardson 		ret = (*dev->dev_ops->crypto_adapter_queue_pair_del)(dev,
93199a2dd95SBruce Richardson 						dev_info->dev,
93299a2dd95SBruce Richardson 						queue_pair_id);
93399a2dd95SBruce Richardson 		if (ret == 0) {
93499a2dd95SBruce Richardson 			eca_update_qp_info(adapter,
93599a2dd95SBruce Richardson 					&adapter->cdevs[cdev_id],
93699a2dd95SBruce Richardson 					queue_pair_id,
93799a2dd95SBruce Richardson 					0);
93899a2dd95SBruce Richardson 			if (dev_info->num_qpairs == 0) {
93999a2dd95SBruce Richardson 				rte_free(dev_info->qpairs);
94099a2dd95SBruce Richardson 				dev_info->qpairs = NULL;
94199a2dd95SBruce Richardson 			}
94299a2dd95SBruce Richardson 		}
94399a2dd95SBruce Richardson 	} else {
94499a2dd95SBruce Richardson 		if (adapter->nb_qps == 0)
94599a2dd95SBruce Richardson 			return 0;
94699a2dd95SBruce Richardson 
94799a2dd95SBruce Richardson 		rte_spinlock_lock(&adapter->lock);
94899a2dd95SBruce Richardson 		if (queue_pair_id == -1) {
94999a2dd95SBruce Richardson 			for (i = 0; i < dev_info->dev->data->nb_queue_pairs;
95099a2dd95SBruce Richardson 				i++)
95199a2dd95SBruce Richardson 				eca_update_qp_info(adapter, dev_info,
95299a2dd95SBruce Richardson 							queue_pair_id, 0);
95399a2dd95SBruce Richardson 		} else {
95499a2dd95SBruce Richardson 			eca_update_qp_info(adapter, dev_info,
95599a2dd95SBruce Richardson 						(uint16_t)queue_pair_id, 0);
95699a2dd95SBruce Richardson 		}
95799a2dd95SBruce Richardson 
95899a2dd95SBruce Richardson 		if (dev_info->num_qpairs == 0) {
95999a2dd95SBruce Richardson 			rte_free(dev_info->qpairs);
96099a2dd95SBruce Richardson 			dev_info->qpairs = NULL;
96199a2dd95SBruce Richardson 		}
96299a2dd95SBruce Richardson 
96399a2dd95SBruce Richardson 		rte_spinlock_unlock(&adapter->lock);
96499a2dd95SBruce Richardson 		rte_service_component_runstate_set(adapter->service_id,
96599a2dd95SBruce Richardson 				adapter->nb_qps);
96699a2dd95SBruce Richardson 	}
96799a2dd95SBruce Richardson 
96899a2dd95SBruce Richardson 	rte_eventdev_trace_crypto_adapter_queue_pair_del(id, cdev_id,
96999a2dd95SBruce Richardson 		queue_pair_id, ret);
97099a2dd95SBruce Richardson 	return ret;
97199a2dd95SBruce Richardson }
97299a2dd95SBruce Richardson 
97399a2dd95SBruce Richardson static int
97499a2dd95SBruce Richardson eca_adapter_ctrl(uint8_t id, int start)
97599a2dd95SBruce Richardson {
976*a256a743SPavan Nikhilesh 	struct event_crypto_adapter *adapter;
97799a2dd95SBruce Richardson 	struct crypto_device_info *dev_info;
97899a2dd95SBruce Richardson 	struct rte_eventdev *dev;
97999a2dd95SBruce Richardson 	uint32_t i;
98099a2dd95SBruce Richardson 	int use_service;
98199a2dd95SBruce Richardson 	int stop = !start;
98299a2dd95SBruce Richardson 
98399a2dd95SBruce Richardson 	use_service = 0;
98499a2dd95SBruce Richardson 	EVENT_CRYPTO_ADAPTER_ID_VALID_OR_ERR_RET(id, -EINVAL);
98599a2dd95SBruce Richardson 	adapter = eca_id_to_adapter(id);
98699a2dd95SBruce Richardson 	if (adapter == NULL)
98799a2dd95SBruce Richardson 		return -EINVAL;
98899a2dd95SBruce Richardson 
98999a2dd95SBruce Richardson 	dev = &rte_eventdevs[adapter->eventdev_id];
99099a2dd95SBruce Richardson 
99199a2dd95SBruce Richardson 	for (i = 0; i < rte_cryptodev_count(); i++) {
99299a2dd95SBruce Richardson 		dev_info = &adapter->cdevs[i];
99399a2dd95SBruce Richardson 		/* if start  check for num queue pairs */
99499a2dd95SBruce Richardson 		if (start && !dev_info->num_qpairs)
99599a2dd95SBruce Richardson 			continue;
99699a2dd95SBruce Richardson 		/* if stop check if dev has been started */
99799a2dd95SBruce Richardson 		if (stop && !dev_info->dev_started)
99899a2dd95SBruce Richardson 			continue;
99999a2dd95SBruce Richardson 		use_service |= !dev_info->internal_event_port;
100099a2dd95SBruce Richardson 		dev_info->dev_started = start;
100199a2dd95SBruce Richardson 		if (dev_info->internal_event_port == 0)
100299a2dd95SBruce Richardson 			continue;
100399a2dd95SBruce Richardson 		start ? (*dev->dev_ops->crypto_adapter_start)(dev,
100499a2dd95SBruce Richardson 						&dev_info->dev[i]) :
100599a2dd95SBruce Richardson 			(*dev->dev_ops->crypto_adapter_stop)(dev,
100699a2dd95SBruce Richardson 						&dev_info->dev[i]);
100799a2dd95SBruce Richardson 	}
100899a2dd95SBruce Richardson 
100999a2dd95SBruce Richardson 	if (use_service)
101099a2dd95SBruce Richardson 		rte_service_runstate_set(adapter->service_id, start);
101199a2dd95SBruce Richardson 
101299a2dd95SBruce Richardson 	return 0;
101399a2dd95SBruce Richardson }
101499a2dd95SBruce Richardson 
101599a2dd95SBruce Richardson int
101699a2dd95SBruce Richardson rte_event_crypto_adapter_start(uint8_t id)
101799a2dd95SBruce Richardson {
1018*a256a743SPavan Nikhilesh 	struct event_crypto_adapter *adapter;
101999a2dd95SBruce Richardson 
102099a2dd95SBruce Richardson 	EVENT_CRYPTO_ADAPTER_ID_VALID_OR_ERR_RET(id, -EINVAL);
102199a2dd95SBruce Richardson 	adapter = eca_id_to_adapter(id);
102299a2dd95SBruce Richardson 	if (adapter == NULL)
102399a2dd95SBruce Richardson 		return -EINVAL;
102499a2dd95SBruce Richardson 
102599a2dd95SBruce Richardson 	rte_eventdev_trace_crypto_adapter_start(id, adapter);
102699a2dd95SBruce Richardson 	return eca_adapter_ctrl(id, 1);
102799a2dd95SBruce Richardson }
102899a2dd95SBruce Richardson 
102999a2dd95SBruce Richardson int
103099a2dd95SBruce Richardson rte_event_crypto_adapter_stop(uint8_t id)
103199a2dd95SBruce Richardson {
103299a2dd95SBruce Richardson 	rte_eventdev_trace_crypto_adapter_stop(id);
103399a2dd95SBruce Richardson 	return eca_adapter_ctrl(id, 0);
103499a2dd95SBruce Richardson }
103599a2dd95SBruce Richardson 
103699a2dd95SBruce Richardson int
103799a2dd95SBruce Richardson rte_event_crypto_adapter_stats_get(uint8_t id,
103899a2dd95SBruce Richardson 				struct rte_event_crypto_adapter_stats *stats)
103999a2dd95SBruce Richardson {
1040*a256a743SPavan Nikhilesh 	struct event_crypto_adapter *adapter;
104199a2dd95SBruce Richardson 	struct rte_event_crypto_adapter_stats dev_stats_sum = { 0 };
104299a2dd95SBruce Richardson 	struct rte_event_crypto_adapter_stats dev_stats;
104399a2dd95SBruce Richardson 	struct rte_eventdev *dev;
104499a2dd95SBruce Richardson 	struct crypto_device_info *dev_info;
104599a2dd95SBruce Richardson 	uint32_t i;
104699a2dd95SBruce Richardson 	int ret;
104799a2dd95SBruce Richardson 
104899a2dd95SBruce Richardson 	EVENT_CRYPTO_ADAPTER_ID_VALID_OR_ERR_RET(id, -EINVAL);
104999a2dd95SBruce Richardson 
105099a2dd95SBruce Richardson 	adapter = eca_id_to_adapter(id);
105199a2dd95SBruce Richardson 	if (adapter == NULL || stats == NULL)
105299a2dd95SBruce Richardson 		return -EINVAL;
105399a2dd95SBruce Richardson 
105499a2dd95SBruce Richardson 	dev = &rte_eventdevs[adapter->eventdev_id];
105599a2dd95SBruce Richardson 	memset(stats, 0, sizeof(*stats));
105699a2dd95SBruce Richardson 	for (i = 0; i < rte_cryptodev_count(); i++) {
105799a2dd95SBruce Richardson 		dev_info = &adapter->cdevs[i];
105899a2dd95SBruce Richardson 		if (dev_info->internal_event_port == 0 ||
105999a2dd95SBruce Richardson 			dev->dev_ops->crypto_adapter_stats_get == NULL)
106099a2dd95SBruce Richardson 			continue;
106199a2dd95SBruce Richardson 		ret = (*dev->dev_ops->crypto_adapter_stats_get)(dev,
106299a2dd95SBruce Richardson 						dev_info->dev,
106399a2dd95SBruce Richardson 						&dev_stats);
106499a2dd95SBruce Richardson 		if (ret)
106599a2dd95SBruce Richardson 			continue;
106699a2dd95SBruce Richardson 
106799a2dd95SBruce Richardson 		dev_stats_sum.crypto_deq_count += dev_stats.crypto_deq_count;
106899a2dd95SBruce Richardson 		dev_stats_sum.event_enq_count +=
106999a2dd95SBruce Richardson 			dev_stats.event_enq_count;
107099a2dd95SBruce Richardson 	}
107199a2dd95SBruce Richardson 
107299a2dd95SBruce Richardson 	if (adapter->service_inited)
107399a2dd95SBruce Richardson 		*stats = adapter->crypto_stats;
107499a2dd95SBruce Richardson 
107599a2dd95SBruce Richardson 	stats->crypto_deq_count += dev_stats_sum.crypto_deq_count;
107699a2dd95SBruce Richardson 	stats->event_enq_count += dev_stats_sum.event_enq_count;
107799a2dd95SBruce Richardson 
107899a2dd95SBruce Richardson 	return 0;
107999a2dd95SBruce Richardson }
108099a2dd95SBruce Richardson 
108199a2dd95SBruce Richardson int
108299a2dd95SBruce Richardson rte_event_crypto_adapter_stats_reset(uint8_t id)
108399a2dd95SBruce Richardson {
1084*a256a743SPavan Nikhilesh 	struct event_crypto_adapter *adapter;
108599a2dd95SBruce Richardson 	struct crypto_device_info *dev_info;
108699a2dd95SBruce Richardson 	struct rte_eventdev *dev;
108799a2dd95SBruce Richardson 	uint32_t i;
108899a2dd95SBruce Richardson 
108999a2dd95SBruce Richardson 	EVENT_CRYPTO_ADAPTER_ID_VALID_OR_ERR_RET(id, -EINVAL);
109099a2dd95SBruce Richardson 
109199a2dd95SBruce Richardson 	adapter = eca_id_to_adapter(id);
109299a2dd95SBruce Richardson 	if (adapter == NULL)
109399a2dd95SBruce Richardson 		return -EINVAL;
109499a2dd95SBruce Richardson 
109599a2dd95SBruce Richardson 	dev = &rte_eventdevs[adapter->eventdev_id];
109699a2dd95SBruce Richardson 	for (i = 0; i < rte_cryptodev_count(); i++) {
109799a2dd95SBruce Richardson 		dev_info = &adapter->cdevs[i];
109899a2dd95SBruce Richardson 		if (dev_info->internal_event_port == 0 ||
109999a2dd95SBruce Richardson 			dev->dev_ops->crypto_adapter_stats_reset == NULL)
110099a2dd95SBruce Richardson 			continue;
110199a2dd95SBruce Richardson 		(*dev->dev_ops->crypto_adapter_stats_reset)(dev,
110299a2dd95SBruce Richardson 						dev_info->dev);
110399a2dd95SBruce Richardson 	}
110499a2dd95SBruce Richardson 
110599a2dd95SBruce Richardson 	memset(&adapter->crypto_stats, 0, sizeof(adapter->crypto_stats));
110699a2dd95SBruce Richardson 	return 0;
110799a2dd95SBruce Richardson }
110899a2dd95SBruce Richardson 
110999a2dd95SBruce Richardson int
111099a2dd95SBruce Richardson rte_event_crypto_adapter_service_id_get(uint8_t id, uint32_t *service_id)
111199a2dd95SBruce Richardson {
1112*a256a743SPavan Nikhilesh 	struct event_crypto_adapter *adapter;
111399a2dd95SBruce Richardson 
111499a2dd95SBruce Richardson 	EVENT_CRYPTO_ADAPTER_ID_VALID_OR_ERR_RET(id, -EINVAL);
111599a2dd95SBruce Richardson 
111699a2dd95SBruce Richardson 	adapter = eca_id_to_adapter(id);
111799a2dd95SBruce Richardson 	if (adapter == NULL || service_id == NULL)
111899a2dd95SBruce Richardson 		return -EINVAL;
111999a2dd95SBruce Richardson 
112099a2dd95SBruce Richardson 	if (adapter->service_inited)
112199a2dd95SBruce Richardson 		*service_id = adapter->service_id;
112299a2dd95SBruce Richardson 
112399a2dd95SBruce Richardson 	return adapter->service_inited ? 0 : -ESRCH;
112499a2dd95SBruce Richardson }
112599a2dd95SBruce Richardson 
112699a2dd95SBruce Richardson int
112799a2dd95SBruce Richardson rte_event_crypto_adapter_event_port_get(uint8_t id, uint8_t *event_port_id)
112899a2dd95SBruce Richardson {
1129*a256a743SPavan Nikhilesh 	struct event_crypto_adapter *adapter;
113099a2dd95SBruce Richardson 
113199a2dd95SBruce Richardson 	EVENT_CRYPTO_ADAPTER_ID_VALID_OR_ERR_RET(id, -EINVAL);
113299a2dd95SBruce Richardson 
113399a2dd95SBruce Richardson 	adapter = eca_id_to_adapter(id);
113499a2dd95SBruce Richardson 	if (adapter == NULL || event_port_id == NULL)
113599a2dd95SBruce Richardson 		return -EINVAL;
113699a2dd95SBruce Richardson 
113799a2dd95SBruce Richardson 	*event_port_id = adapter->event_port_id;
113899a2dd95SBruce Richardson 
113999a2dd95SBruce Richardson 	return 0;
114099a2dd95SBruce Richardson }
1141