1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Advanced Micro Devices, Inc. All rights reserved.
5 #include <rte_bus_pci.h>
6 #include <rte_bus_vdev.h>
7 #include <rte_common.h>
8 #include <rte_config.h>
9 #include <rte_cryptodev.h>
10 #include <rte_cryptodev_pmd.h>
13 #include <rte_malloc.h>
15 #include "ccp_crypto.h"
17 #include "ccp_pmd_private.h"
20 * Global static parameter used to find if CCP device is already initialized.
22 static unsigned int ccp_pmd_init_done;
23 uint8_t ccp_cryptodev_driver_id;
25 struct ccp_pmd_init_params {
26 struct rte_cryptodev_pmd_init_params def_p;
30 #define CCP_CRYPTODEV_PARAM_NAME ("name")
31 #define CCP_CRYPTODEV_PARAM_SOCKET_ID ("socket_id")
32 #define CCP_CRYPTODEV_PARAM_MAX_NB_QP ("max_nb_queue_pairs")
33 #define CCP_CRYPTODEV_PARAM_AUTH_OPT ("ccp_auth_opt")
35 const char *ccp_pmd_valid_params[] = {
36 CCP_CRYPTODEV_PARAM_NAME,
37 CCP_CRYPTODEV_PARAM_SOCKET_ID,
38 CCP_CRYPTODEV_PARAM_MAX_NB_QP,
39 CCP_CRYPTODEV_PARAM_AUTH_OPT,
42 /** ccp pmd auth option */
43 enum ccp_pmd_auth_opt {
44 CCP_PMD_AUTH_OPT_CCP = 0,
48 /** parse integer from integer argument */
50 parse_integer_arg(const char *key __rte_unused,
51 const char *value, void *extra_args)
53 int *i = (int *) extra_args;
57 CCP_LOG_ERR("Argument has to be positive.\n");
64 /** parse name argument */
66 parse_name_arg(const char *key __rte_unused,
67 const char *value, void *extra_args)
69 struct rte_cryptodev_pmd_init_params *params = extra_args;
71 if (strlen(value) >= RTE_CRYPTODEV_NAME_MAX_LEN - 1) {
72 CCP_LOG_ERR("Invalid name %s, should be less than "
74 RTE_CRYPTODEV_NAME_MAX_LEN - 1);
78 strncpy(params->name, value, RTE_CRYPTODEV_NAME_MAX_LEN);
83 /** parse authentication operation option */
85 parse_auth_opt_arg(const char *key __rte_unused,
86 const char *value, void *extra_args)
88 struct ccp_pmd_init_params *params = extra_args;
92 if (i < CCP_PMD_AUTH_OPT_CCP || i > CCP_PMD_AUTH_OPT_CPU) {
93 CCP_LOG_ERR("Invalid ccp pmd auth option. "
94 "0->auth on CCP(default), "
103 ccp_pmd_parse_input_args(struct ccp_pmd_init_params *params,
104 const char *input_args)
106 struct rte_kvargs *kvlist = NULL;
113 kvlist = rte_kvargs_parse(input_args,
114 ccp_pmd_valid_params);
118 ret = rte_kvargs_process(kvlist,
119 CCP_CRYPTODEV_PARAM_MAX_NB_QP,
121 ¶ms->def_p.max_nb_queue_pairs);
125 ret = rte_kvargs_process(kvlist,
126 CCP_CRYPTODEV_PARAM_SOCKET_ID,
128 ¶ms->def_p.socket_id);
132 ret = rte_kvargs_process(kvlist,
133 CCP_CRYPTODEV_PARAM_NAME,
139 ret = rte_kvargs_process(kvlist,
140 CCP_CRYPTODEV_PARAM_AUTH_OPT,
149 rte_kvargs_free(kvlist);
153 static struct ccp_session *
154 get_ccp_session(struct ccp_qp *qp, struct rte_crypto_op *op)
156 struct ccp_session *sess = NULL;
158 if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) {
159 if (unlikely(op->sym->session == NULL))
162 sess = (struct ccp_session *)
163 get_sym_session_private_data(
165 ccp_cryptodev_driver_id);
166 } else if (op->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
168 void *_sess_private_data = NULL;
169 struct ccp_private *internals;
171 if (rte_mempool_get(qp->sess_mp, &_sess))
173 if (rte_mempool_get(qp->sess_mp, (void **)&_sess_private_data))
176 sess = (struct ccp_session *)_sess_private_data;
178 internals = (struct ccp_private *)qp->dev->data->dev_private;
179 if (unlikely(ccp_set_session_parameters(sess, op->sym->xform,
181 rte_mempool_put(qp->sess_mp, _sess);
182 rte_mempool_put(qp->sess_mp, _sess_private_data);
185 op->sym->session = (struct rte_cryptodev_sym_session *)_sess;
186 set_sym_session_private_data(op->sym->session,
187 ccp_cryptodev_driver_id,
195 ccp_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
198 struct ccp_session *sess = NULL;
199 struct ccp_qp *qp = queue_pair;
200 struct ccp_queue *cmd_q;
201 struct rte_cryptodev *dev = qp->dev;
202 uint16_t i, enq_cnt = 0, slots_req = 0;
207 if (unlikely(rte_ring_full(qp->processed_pkts) != 0))
210 for (i = 0; i < nb_ops; i++) {
211 sess = get_ccp_session(qp, ops[i]);
212 if (unlikely(sess == NULL) && (i == 0)) {
213 qp->qp_stats.enqueue_err_count++;
215 } else if (sess == NULL) {
219 slots_req += ccp_compute_slot_count(sess);
222 cmd_q = ccp_allot_queue(dev, slots_req);
223 if (unlikely(cmd_q == NULL))
226 enq_cnt = process_ops_to_enqueue(qp, ops, cmd_q, nb_ops, slots_req);
227 qp->qp_stats.enqueued_count += enq_cnt;
232 ccp_pmd_dequeue_burst(void *queue_pair, struct rte_crypto_op **ops,
235 struct ccp_qp *qp = queue_pair;
236 uint16_t nb_dequeued = 0, i;
238 nb_dequeued = process_ops_to_dequeue(qp, ops, nb_ops);
240 /* Free session if a session-less crypto op */
241 for (i = 0; i < nb_dequeued; i++)
242 if (unlikely(ops[i]->sess_type ==
243 RTE_CRYPTO_OP_SESSIONLESS)) {
244 rte_mempool_put(qp->sess_mp,
245 ops[i]->sym->session);
246 ops[i]->sym->session = NULL;
248 qp->qp_stats.dequeued_count += nb_dequeued;
254 * The set of PCI devices this driver supports
256 static struct rte_pci_id ccp_pci_id[] = {
258 RTE_PCI_DEVICE(0x1022, 0x1456), /* AMD CCP-5a */
261 RTE_PCI_DEVICE(0x1022, 0x1468), /* AMD CCP-5b */
266 /** Remove ccp pmd */
268 cryptodev_ccp_remove(struct rte_vdev_device *dev)
272 ccp_pmd_init_done = 0;
273 name = rte_vdev_device_name(dev);
277 RTE_LOG(INFO, PMD, "Closing ccp device %s on numa socket %u\n",
278 name, rte_socket_id());
283 /** Create crypto device */
285 cryptodev_ccp_create(const char *name,
286 struct rte_vdev_device *vdev,
287 struct ccp_pmd_init_params *init_params)
289 struct rte_cryptodev *dev;
290 struct ccp_private *internals;
291 uint8_t cryptodev_cnt = 0;
293 if (init_params->def_p.name[0] == '\0')
294 snprintf(init_params->def_p.name,
295 sizeof(init_params->def_p.name),
298 dev = rte_cryptodev_pmd_create(init_params->def_p.name,
300 &init_params->def_p);
302 CCP_LOG_ERR("failed to create cryptodev vdev");
306 cryptodev_cnt = ccp_probe_devices(ccp_pci_id);
308 if (cryptodev_cnt == 0) {
309 CCP_LOG_ERR("failed to detect CCP crypto device");
313 printf("CCP : Crypto device count = %d\n", cryptodev_cnt);
314 dev->driver_id = ccp_cryptodev_driver_id;
316 /* register rx/tx burst functions for data path */
317 dev->dev_ops = ccp_pmd_ops;
318 dev->enqueue_burst = ccp_pmd_enqueue_burst;
319 dev->dequeue_burst = ccp_pmd_dequeue_burst;
321 dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
322 RTE_CRYPTODEV_FF_HW_ACCELERATED |
323 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING;
325 internals = dev->data->dev_private;
327 internals->max_nb_qpairs = init_params->def_p.max_nb_queue_pairs;
328 internals->auth_opt = init_params->auth_opt;
329 internals->crypto_num_dev = cryptodev_cnt;
334 CCP_LOG_ERR("driver %s: %s() failed",
335 init_params->def_p.name, __func__);
336 cryptodev_ccp_remove(vdev);
343 cryptodev_ccp_probe(struct rte_vdev_device *vdev)
347 struct ccp_pmd_init_params init_params = {
350 sizeof(struct ccp_private),
352 CCP_PMD_MAX_QUEUE_PAIRS
354 .auth_opt = CCP_PMD_AUTH_OPT_CCP,
356 const char *input_args;
358 if (ccp_pmd_init_done) {
359 RTE_LOG(INFO, PMD, "CCP PMD already initialized\n");
362 name = rte_vdev_device_name(vdev);
366 input_args = rte_vdev_device_args(vdev);
367 ccp_pmd_parse_input_args(&init_params, input_args);
368 init_params.def_p.max_nb_queue_pairs = CCP_PMD_MAX_QUEUE_PAIRS;
370 RTE_LOG(INFO, PMD, "Initialising %s on NUMA node %d\n", name,
371 init_params.def_p.socket_id);
372 RTE_LOG(INFO, PMD, "Max number of queue pairs = %d\n",
373 init_params.def_p.max_nb_queue_pairs);
374 RTE_LOG(INFO, PMD, "Authentication offload to %s\n",
375 ((init_params.auth_opt == 0) ? "CCP" : "CPU"));
377 rc = cryptodev_ccp_create(name, vdev, &init_params);
380 ccp_pmd_init_done = 1;
384 static struct rte_vdev_driver cryptodev_ccp_pmd_drv = {
385 .probe = cryptodev_ccp_probe,
386 .remove = cryptodev_ccp_remove
389 static struct cryptodev_driver ccp_crypto_drv;
391 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_CCP_PMD, cryptodev_ccp_pmd_drv);
392 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_CCP_PMD,
393 "max_nb_queue_pairs=<int> "
395 "ccp_auth_opt=<int>");
396 RTE_PMD_REGISTER_CRYPTO_DRIVER(ccp_crypto_drv, cryptodev_ccp_pmd_drv.driver,
397 ccp_cryptodev_driver_id);