d3553db23e2b81c7d8d9ea0dcd3d20a1d1e4ca56
[dpdk.git] / drivers / crypto / ccp / rte_ccp_pmd.c
1 /*   SPDX-License-Identifier: BSD-3-Clause
2  *   Copyright(c) 2018 Advanced Micro Devices, Inc. All rights reserved.
3  */
4
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>
11 #include <rte_pci.h>
12 #include <rte_dev.h>
13 #include <rte_malloc.h>
14
15 #include "ccp_crypto.h"
16 #include "ccp_dev.h"
17 #include "ccp_pmd_private.h"
18
19 /**
20  * Global static parameter used to find if CCP device is already initialized.
21  */
22 static unsigned int ccp_pmd_init_done;
23 uint8_t ccp_cryptodev_driver_id;
24
25 static struct ccp_session *
26 get_ccp_session(struct ccp_qp *qp, struct rte_crypto_op *op)
27 {
28         struct ccp_session *sess = NULL;
29
30         if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) {
31                 if (unlikely(op->sym->session == NULL))
32                         return NULL;
33
34                 sess = (struct ccp_session *)
35                         get_session_private_data(
36                                 op->sym->session,
37                                 ccp_cryptodev_driver_id);
38         } else if (op->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
39                 void *_sess;
40                 void *_sess_private_data = NULL;
41
42                 if (rte_mempool_get(qp->sess_mp, &_sess))
43                         return NULL;
44                 if (rte_mempool_get(qp->sess_mp, (void **)&_sess_private_data))
45                         return NULL;
46
47                 sess = (struct ccp_session *)_sess_private_data;
48
49                 if (unlikely(ccp_set_session_parameters(sess,
50                                                         op->sym->xform) != 0)) {
51                         rte_mempool_put(qp->sess_mp, _sess);
52                         rte_mempool_put(qp->sess_mp, _sess_private_data);
53                         sess = NULL;
54                 }
55                 op->sym->session = (struct rte_cryptodev_sym_session *)_sess;
56                 set_session_private_data(op->sym->session,
57                                          ccp_cryptodev_driver_id,
58                                          _sess_private_data);
59         }
60
61         return sess;
62 }
63
64 static uint16_t
65 ccp_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
66                       uint16_t nb_ops)
67 {
68         struct ccp_session *sess = NULL;
69         struct ccp_qp *qp = queue_pair;
70         struct ccp_queue *cmd_q;
71         struct rte_cryptodev *dev = qp->dev;
72         uint16_t i, enq_cnt = 0, slots_req = 0;
73
74         if (nb_ops == 0)
75                 return 0;
76
77         if (unlikely(rte_ring_full(qp->processed_pkts) != 0))
78                 return 0;
79
80         for (i = 0; i < nb_ops; i++) {
81                 sess = get_ccp_session(qp, ops[i]);
82                 if (unlikely(sess == NULL) && (i == 0)) {
83                         qp->qp_stats.enqueue_err_count++;
84                         return 0;
85                 } else if (sess == NULL) {
86                         nb_ops = i;
87                         break;
88                 }
89                 slots_req += ccp_compute_slot_count(sess);
90         }
91
92         cmd_q = ccp_allot_queue(dev, slots_req);
93         if (unlikely(cmd_q == NULL))
94                 return 0;
95
96         enq_cnt = process_ops_to_enqueue(qp, ops, cmd_q, nb_ops, slots_req);
97         qp->qp_stats.enqueued_count += enq_cnt;
98         return enq_cnt;
99 }
100
101 static uint16_t
102 ccp_pmd_dequeue_burst(void *queue_pair, struct rte_crypto_op **ops,
103                 uint16_t nb_ops)
104 {
105         struct ccp_qp *qp = queue_pair;
106         uint16_t nb_dequeued = 0, i;
107
108         nb_dequeued = process_ops_to_dequeue(qp, ops, nb_ops);
109
110         /* Free session if a session-less crypto op */
111         for (i = 0; i < nb_dequeued; i++)
112                 if (unlikely(ops[i]->sess_type ==
113                              RTE_CRYPTO_OP_SESSIONLESS)) {
114                         rte_mempool_put(qp->sess_mp,
115                                         ops[i]->sym->session);
116                         ops[i]->sym->session = NULL;
117                 }
118         qp->qp_stats.dequeued_count += nb_dequeued;
119
120         return nb_dequeued;
121 }
122
123 /*
124  * The set of PCI devices this driver supports
125  */
126 static struct rte_pci_id ccp_pci_id[] = {
127         {
128                 RTE_PCI_DEVICE(0x1022, 0x1456), /* AMD CCP-5a */
129         },
130         {
131                 RTE_PCI_DEVICE(0x1022, 0x1468), /* AMD CCP-5b */
132         },
133         {.device_id = 0},
134 };
135
136 /** Remove ccp pmd */
137 static int
138 cryptodev_ccp_remove(struct rte_vdev_device *dev)
139 {
140         const char *name;
141
142         ccp_pmd_init_done = 0;
143         name = rte_vdev_device_name(dev);
144         if (name == NULL)
145                 return -EINVAL;
146
147         RTE_LOG(INFO, PMD, "Closing ccp device %s on numa socket %u\n",
148                         name, rte_socket_id());
149
150         return 0;
151 }
152
153 /** Create crypto device */
154 static int
155 cryptodev_ccp_create(const char *name,
156                      struct rte_vdev_device *vdev,
157                      struct rte_cryptodev_pmd_init_params *init_params)
158 {
159         struct rte_cryptodev *dev;
160         struct ccp_private *internals;
161         uint8_t cryptodev_cnt = 0;
162
163         if (init_params->name[0] == '\0')
164                 snprintf(init_params->name, sizeof(init_params->name),
165                                 "%s", name);
166
167         dev = rte_cryptodev_pmd_create(init_params->name,
168                                        &vdev->device,
169                                        init_params);
170         if (dev == NULL) {
171                 CCP_LOG_ERR("failed to create cryptodev vdev");
172                 goto init_error;
173         }
174
175         cryptodev_cnt = ccp_probe_devices(ccp_pci_id);
176
177         if (cryptodev_cnt == 0) {
178                 CCP_LOG_ERR("failed to detect CCP crypto device");
179                 goto init_error;
180         }
181
182         printf("CCP : Crypto device count = %d\n", cryptodev_cnt);
183         dev->driver_id = ccp_cryptodev_driver_id;
184
185         /* register rx/tx burst functions for data path */
186         dev->dev_ops = ccp_pmd_ops;
187         dev->enqueue_burst = ccp_pmd_enqueue_burst;
188         dev->dequeue_burst = ccp_pmd_dequeue_burst;
189
190         dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
191                         RTE_CRYPTODEV_FF_HW_ACCELERATED |
192                         RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING;
193
194         internals = dev->data->dev_private;
195
196         internals->max_nb_qpairs = init_params->max_nb_queue_pairs;
197         internals->max_nb_sessions = init_params->max_nb_sessions;
198         internals->crypto_num_dev = cryptodev_cnt;
199
200         return 0;
201
202 init_error:
203         CCP_LOG_ERR("driver %s: %s() failed",
204                     init_params->name, __func__);
205         cryptodev_ccp_remove(vdev);
206
207         return -EFAULT;
208 }
209
210 /** Probe ccp pmd */
211 static int
212 cryptodev_ccp_probe(struct rte_vdev_device *vdev)
213 {
214         int rc = 0;
215         const char *name;
216         struct rte_cryptodev_pmd_init_params init_params = {
217                 "",
218                 sizeof(struct ccp_private),
219                 rte_socket_id(),
220                 CCP_PMD_MAX_QUEUE_PAIRS,
221                 RTE_CRYPTODEV_PMD_DEFAULT_MAX_NB_SESSIONS
222         };
223         const char *input_args;
224
225         if (ccp_pmd_init_done) {
226                 RTE_LOG(INFO, PMD, "CCP PMD already initialized\n");
227                 return -EFAULT;
228         }
229         name = rte_vdev_device_name(vdev);
230         if (name == NULL)
231                 return -EINVAL;
232
233         input_args = rte_vdev_device_args(vdev);
234         rte_cryptodev_pmd_parse_input_args(&init_params, input_args);
235         init_params.max_nb_queue_pairs = CCP_PMD_MAX_QUEUE_PAIRS;
236
237         RTE_LOG(INFO, PMD, "Initialising %s on NUMA node %d\n", name,
238                         init_params.socket_id);
239         RTE_LOG(INFO, PMD, "Max number of queue pairs = %d\n",
240                         init_params.max_nb_queue_pairs);
241         RTE_LOG(INFO, PMD, "Max number of sessions = %d\n",
242                         init_params.max_nb_sessions);
243
244         rc = cryptodev_ccp_create(name, vdev, &init_params);
245         if (rc)
246                 return rc;
247         ccp_pmd_init_done = 1;
248         return 0;
249 }
250
251 static struct rte_vdev_driver cryptodev_ccp_pmd_drv = {
252         .probe = cryptodev_ccp_probe,
253         .remove = cryptodev_ccp_remove
254 };
255
256 static struct cryptodev_driver ccp_crypto_drv;
257
258 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_CCP_PMD, cryptodev_ccp_pmd_drv);
259 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_CCP_PMD,
260         "max_nb_queue_pairs=<int> max_nb_sessions=<int> socket_id=<int>");
261 RTE_PMD_REGISTER_CRYPTO_DRIVER(ccp_crypto_drv, cryptodev_ccp_pmd_drv.driver,
262                                ccp_cryptodev_driver_id);