1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Advanced Micro Devices, Inc. All rights reserved.
5 #include <rte_string_fns.h>
6 #include <rte_bus_pci.h>
7 #include <rte_bus_vdev.h>
8 #include <rte_common.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;
24 uint8_t cryptodev_cnt;
27 struct ccp_pmd_init_params {
28 struct rte_cryptodev_pmd_init_params def_p;
32 #define CCP_CRYPTODEV_PARAM_NAME ("name")
33 #define CCP_CRYPTODEV_PARAM_SOCKET_ID ("socket_id")
34 #define CCP_CRYPTODEV_PARAM_MAX_NB_QP ("max_nb_queue_pairs")
35 #define CCP_CRYPTODEV_PARAM_AUTH_OPT ("ccp_auth_opt")
37 const char *ccp_pmd_valid_params[] = {
38 CCP_CRYPTODEV_PARAM_NAME,
39 CCP_CRYPTODEV_PARAM_SOCKET_ID,
40 CCP_CRYPTODEV_PARAM_MAX_NB_QP,
41 CCP_CRYPTODEV_PARAM_AUTH_OPT,
44 /** ccp pmd auth option */
45 enum ccp_pmd_auth_opt {
46 CCP_PMD_AUTH_OPT_CCP = 0,
50 /** parse integer from integer argument */
52 parse_integer_arg(const char *key __rte_unused,
53 const char *value, void *extra_args)
55 int *i = (int *) extra_args;
59 CCP_LOG_ERR("Argument has to be positive.\n");
66 /** parse name argument */
68 parse_name_arg(const char *key __rte_unused,
69 const char *value, void *extra_args)
71 struct rte_cryptodev_pmd_init_params *params = extra_args;
73 if (strlen(value) >= RTE_CRYPTODEV_NAME_MAX_LEN - 1) {
74 CCP_LOG_ERR("Invalid name %s, should be less than "
76 RTE_CRYPTODEV_NAME_MAX_LEN - 1);
80 strncpy(params->name, value, RTE_CRYPTODEV_NAME_MAX_LEN);
85 /** parse authentication operation option */
87 parse_auth_opt_arg(const char *key __rte_unused,
88 const char *value, void *extra_args)
90 struct ccp_pmd_init_params *params = extra_args;
94 if (i < CCP_PMD_AUTH_OPT_CCP || i > CCP_PMD_AUTH_OPT_CPU) {
95 CCP_LOG_ERR("Invalid ccp pmd auth option. "
96 "0->auth on CCP(default), "
100 params->auth_opt = i;
105 ccp_pmd_parse_input_args(struct ccp_pmd_init_params *params,
106 const char *input_args)
108 struct rte_kvargs *kvlist = NULL;
115 kvlist = rte_kvargs_parse(input_args,
116 ccp_pmd_valid_params);
120 ret = rte_kvargs_process(kvlist,
121 CCP_CRYPTODEV_PARAM_MAX_NB_QP,
123 ¶ms->def_p.max_nb_queue_pairs);
127 ret = rte_kvargs_process(kvlist,
128 CCP_CRYPTODEV_PARAM_SOCKET_ID,
130 ¶ms->def_p.socket_id);
134 ret = rte_kvargs_process(kvlist,
135 CCP_CRYPTODEV_PARAM_NAME,
141 ret = rte_kvargs_process(kvlist,
142 CCP_CRYPTODEV_PARAM_AUTH_OPT,
151 rte_kvargs_free(kvlist);
155 static struct ccp_session *
156 get_ccp_session(struct ccp_qp *qp, struct rte_crypto_op *op)
158 struct ccp_session *sess = NULL;
160 if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) {
161 if (unlikely(op->sym->session == NULL))
164 sess = (struct ccp_session *)
165 get_sym_session_private_data(
167 ccp_cryptodev_driver_id);
168 } else if (op->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
170 void *_sess_private_data = NULL;
171 struct ccp_private *internals;
173 if (rte_mempool_get(qp->sess_mp, &_sess))
175 if (rte_mempool_get(qp->sess_mp, (void **)&_sess_private_data))
178 sess = (struct ccp_session *)_sess_private_data;
180 internals = (struct ccp_private *)qp->dev->data->dev_private;
181 if (unlikely(ccp_set_session_parameters(sess, op->sym->xform,
183 rte_mempool_put(qp->sess_mp, _sess);
184 rte_mempool_put(qp->sess_mp_priv, _sess_private_data);
187 op->sym->session = (struct rte_cryptodev_sym_session *)_sess;
188 set_sym_session_private_data(op->sym->session,
189 ccp_cryptodev_driver_id,
197 ccp_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
200 struct ccp_session *sess = NULL;
201 struct ccp_qp *qp = queue_pair;
202 struct ccp_queue *cmd_q;
203 struct rte_cryptodev *dev = qp->dev;
204 uint16_t i, enq_cnt = 0, slots_req = 0;
205 uint16_t tmp_ops = nb_ops, b_idx, cur_ops = 0;
210 if (unlikely(rte_ring_full(qp->processed_pkts) != 0))
212 if (tmp_ops >= cryptodev_cnt)
213 cur_ops = nb_ops / cryptodev_cnt + (nb_ops)%cryptodev_cnt;
217 b_idx = nb_ops - tmp_ops;
219 if (cur_ops <= tmp_ops) {
225 for (i = 0; i < cur_ops; i++) {
226 sess = get_ccp_session(qp, ops[i + b_idx]);
227 if (unlikely(sess == NULL) && (i == 0)) {
228 qp->qp_stats.enqueue_err_count++;
230 } else if (sess == NULL) {
234 slots_req += ccp_compute_slot_count(sess);
237 cmd_q = ccp_allot_queue(dev, slots_req);
238 if (unlikely(cmd_q == NULL))
240 enq_cnt += process_ops_to_enqueue(qp, ops, cmd_q, cur_ops,
241 nb_ops, slots_req, b_idx);
245 qp->qp_stats.enqueued_count += enq_cnt;
250 ccp_pmd_dequeue_burst(void *queue_pair, struct rte_crypto_op **ops,
253 struct ccp_qp *qp = queue_pair;
254 uint16_t nb_dequeued = 0, i, total_nb_ops;
256 nb_dequeued = process_ops_to_dequeue(qp, ops, nb_ops, &total_nb_ops);
259 while (nb_dequeued != total_nb_ops) {
260 nb_dequeued = process_ops_to_dequeue(qp,
261 ops, nb_ops, &total_nb_ops);
265 /* Free session if a session-less crypto op */
266 for (i = 0; i < nb_dequeued; i++)
267 if (unlikely(ops[i]->sess_type ==
268 RTE_CRYPTO_OP_SESSIONLESS)) {
269 struct ccp_session *sess = (struct ccp_session *)
270 get_sym_session_private_data(
271 ops[i]->sym->session,
272 ccp_cryptodev_driver_id);
274 rte_mempool_put(qp->sess_mp_priv,
276 rte_mempool_put(qp->sess_mp,
277 ops[i]->sym->session);
278 ops[i]->sym->session = NULL;
280 qp->qp_stats.dequeued_count += nb_dequeued;
286 * The set of PCI devices this driver supports
288 static struct rte_pci_id ccp_pci_id[] = {
290 RTE_PCI_DEVICE(0x1022, 0x1456), /* AMD CCP-5a */
293 RTE_PCI_DEVICE(0x1022, 0x1468), /* AMD CCP-5b */
296 RTE_PCI_DEVICE(0x1022, 0x15df), /* AMD CCP RV */
301 /** Remove ccp pmd */
303 cryptodev_ccp_remove(struct rte_vdev_device *dev)
307 ccp_pmd_init_done = 0;
308 name = rte_vdev_device_name(dev);
313 RTE_LOG(INFO, PMD, "Closing ccp device %s on numa socket %u\n",
314 name, rte_socket_id());
319 /** Create crypto device */
321 cryptodev_ccp_create(const char *name,
322 struct rte_vdev_device *vdev,
323 struct ccp_pmd_init_params *init_params)
325 struct rte_cryptodev *dev;
326 struct ccp_private *internals;
328 if (init_params->def_p.name[0] == '\0')
329 strlcpy(init_params->def_p.name, name,
330 sizeof(init_params->def_p.name));
332 dev = rte_cryptodev_pmd_create(init_params->def_p.name,
334 &init_params->def_p);
336 CCP_LOG_ERR("failed to create cryptodev vdev");
340 cryptodev_cnt = ccp_probe_devices(ccp_pci_id);
342 if (cryptodev_cnt == 0) {
343 CCP_LOG_ERR("failed to detect CCP crypto device");
347 printf("CCP : Crypto device count = %d\n", cryptodev_cnt);
348 dev->driver_id = ccp_cryptodev_driver_id;
350 /* register rx/tx burst functions for data path */
351 dev->dev_ops = ccp_pmd_ops;
352 dev->enqueue_burst = ccp_pmd_enqueue_burst;
353 dev->dequeue_burst = ccp_pmd_dequeue_burst;
355 dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
356 RTE_CRYPTODEV_FF_HW_ACCELERATED |
357 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
358 RTE_CRYPTODEV_FF_SYM_SESSIONLESS;
360 internals = dev->data->dev_private;
362 internals->max_nb_qpairs = init_params->def_p.max_nb_queue_pairs;
363 internals->auth_opt = init_params->auth_opt;
364 internals->crypto_num_dev = cryptodev_cnt;
369 CCP_LOG_ERR("driver %s: %s() failed",
370 init_params->def_p.name, __func__);
371 cryptodev_ccp_remove(vdev);
378 cryptodev_ccp_probe(struct rte_vdev_device *vdev)
382 struct ccp_pmd_init_params init_params = {
385 sizeof(struct ccp_private),
387 CCP_PMD_MAX_QUEUE_PAIRS
389 .auth_opt = CCP_PMD_AUTH_OPT_CCP,
391 const char *input_args;
393 sha_ctx = (void *)rte_malloc(NULL, SHA512_DIGEST_SIZE, 64);
394 if (ccp_pmd_init_done) {
395 RTE_LOG(INFO, PMD, "CCP PMD already initialized\n");
398 name = rte_vdev_device_name(vdev);
402 input_args = rte_vdev_device_args(vdev);
403 ccp_pmd_parse_input_args(&init_params, input_args);
404 init_params.def_p.max_nb_queue_pairs = CCP_PMD_MAX_QUEUE_PAIRS;
406 RTE_LOG(INFO, PMD, "Initialising %s on NUMA node %d\n", name,
407 init_params.def_p.socket_id);
408 RTE_LOG(INFO, PMD, "Max number of queue pairs = %d\n",
409 init_params.def_p.max_nb_queue_pairs);
410 RTE_LOG(INFO, PMD, "Authentication offload to %s\n",
411 ((init_params.auth_opt == 0) ? "CCP" : "CPU"));
413 rc = cryptodev_ccp_create(name, vdev, &init_params);
416 ccp_pmd_init_done = 1;
420 static struct rte_vdev_driver cryptodev_ccp_pmd_drv = {
421 .probe = cryptodev_ccp_probe,
422 .remove = cryptodev_ccp_remove
425 static struct cryptodev_driver ccp_crypto_drv;
427 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_CCP_PMD, cryptodev_ccp_pmd_drv);
428 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_CCP_PMD,
429 "max_nb_queue_pairs=<int> "
431 "ccp_auth_opt=<int>");
432 RTE_PMD_REGISTER_CRYPTO_DRIVER(ccp_crypto_drv, cryptodev_ccp_pmd_drv.driver,
433 ccp_cryptodev_driver_id);