cryptodev: move IV parameters to session
[dpdk.git] / drivers / crypto / dpaa2_sec / dpaa2_sec_dpseci.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) 2016 Freescale Semiconductor, Inc. All rights reserved.
5  *   Copyright (c) 2016 NXP. All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of  Freescale Semiconductor, Inc nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <time.h>
35 #include <net/if.h>
36
37 #include <rte_mbuf.h>
38 #include <rte_cryptodev.h>
39 #include <rte_malloc.h>
40 #include <rte_memcpy.h>
41 #include <rte_string_fns.h>
42 #include <rte_cycles.h>
43 #include <rte_kvargs.h>
44 #include <rte_dev.h>
45 #include <rte_cryptodev_pmd.h>
46 #include <rte_common.h>
47 #include <rte_fslmc.h>
48 #include <fslmc_vfio.h>
49 #include <dpaa2_hw_pvt.h>
50 #include <dpaa2_hw_dpio.h>
51 #include <dpaa2_hw_mempool.h>
52 #include <fsl_dpseci.h>
53 #include <fsl_mc_sys.h>
54
55 #include "dpaa2_sec_priv.h"
56 #include "dpaa2_sec_logs.h"
57
58 /* RTA header files */
59 #include <hw/desc/ipsec.h>
60 #include <hw/desc/algo.h>
61
62 /* Minimum job descriptor consists of a oneword job descriptor HEADER and
63  * a pointer to the shared descriptor
64  */
65 #define MIN_JOB_DESC_SIZE       (CAAM_CMD_SZ + CAAM_PTR_SZ)
66 #define FSL_VENDOR_ID           0x1957
67 #define FSL_DEVICE_ID           0x410
68 #define FSL_SUBSYSTEM_SEC       1
69 #define FSL_MC_DPSECI_DEVID     3
70
71 #define NO_PREFETCH 0
72 #define TDES_CBC_IV_LEN 8
73 #define AES_CBC_IV_LEN 16
74 enum rta_sec_era rta_sec_era = RTA_SEC_ERA_8;
75
76 static inline int
77 build_authenc_fd(dpaa2_sec_session *sess,
78                  struct rte_crypto_op *op,
79                  struct qbman_fd *fd, uint16_t bpid)
80 {
81         struct rte_crypto_sym_op *sym_op = op->sym;
82         struct ctxt_priv *priv = sess->ctxt;
83         struct qbman_fle *fle, *sge;
84         struct sec_flow_context *flc;
85         uint32_t auth_only_len = sym_op->auth.data.length -
86                                 sym_op->cipher.data.length;
87         int icv_len = sym_op->auth.digest.length;
88         uint8_t *old_icv;
89         uint32_t mem_len = (7 * sizeof(struct qbman_fle)) + icv_len;
90         uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
91                         sess->iv.offset);
92
93         PMD_INIT_FUNC_TRACE();
94
95         /* we are using the first FLE entry to store Mbuf.
96          * Currently we donot know which FLE has the mbuf stored.
97          * So while retreiving we can go back 1 FLE from the FD -ADDR
98          * to get the MBUF Addr from the previous FLE.
99          * We can have a better approach to use the inline Mbuf
100          */
101         fle = rte_zmalloc(NULL, mem_len, RTE_CACHE_LINE_SIZE);
102         if (!fle) {
103                 RTE_LOG(ERR, PMD, "Memory alloc failed for SGE\n");
104                 return -1;
105         }
106         DPAA2_SET_FLE_ADDR(fle, DPAA2_OP_VADDR_TO_IOVA(op));
107         fle = fle + 1;
108         sge = fle + 2;
109         if (likely(bpid < MAX_BPID)) {
110                 DPAA2_SET_FD_BPID(fd, bpid);
111                 DPAA2_SET_FLE_BPID(fle, bpid);
112                 DPAA2_SET_FLE_BPID(fle + 1, bpid);
113                 DPAA2_SET_FLE_BPID(sge, bpid);
114                 DPAA2_SET_FLE_BPID(sge + 1, bpid);
115                 DPAA2_SET_FLE_BPID(sge + 2, bpid);
116                 DPAA2_SET_FLE_BPID(sge + 3, bpid);
117         } else {
118                 DPAA2_SET_FD_IVP(fd);
119                 DPAA2_SET_FLE_IVP(fle);
120                 DPAA2_SET_FLE_IVP((fle + 1));
121                 DPAA2_SET_FLE_IVP(sge);
122                 DPAA2_SET_FLE_IVP((sge + 1));
123                 DPAA2_SET_FLE_IVP((sge + 2));
124                 DPAA2_SET_FLE_IVP((sge + 3));
125         }
126
127         /* Save the shared descriptor */
128         flc = &priv->flc_desc[0].flc;
129         /* Configure FD as a FRAME LIST */
130         DPAA2_SET_FD_ADDR(fd, DPAA2_VADDR_TO_IOVA(fle));
131         DPAA2_SET_FD_COMPOUND_FMT(fd);
132         DPAA2_SET_FD_FLC(fd, DPAA2_VADDR_TO_IOVA(flc));
133
134         PMD_TX_LOG(DEBUG, "auth_off: 0x%x/length %d, digest-len=%d\n"
135                    "cipher_off: 0x%x/length %d, iv-len=%d data_off: 0x%x\n",
136                    sym_op->auth.data.offset,
137                    sym_op->auth.data.length,
138                    sym_op->auth.digest.length,
139                    sym_op->cipher.data.offset,
140                    sym_op->cipher.data.length,
141                    sess->iv.length,
142                    sym_op->m_src->data_off);
143
144         /* Configure Output FLE with Scatter/Gather Entry */
145         DPAA2_SET_FLE_ADDR(fle, DPAA2_VADDR_TO_IOVA(sge));
146         if (auth_only_len)
147                 DPAA2_SET_FLE_INTERNAL_JD(fle, auth_only_len);
148         fle->length = (sess->dir == DIR_ENC) ?
149                         (sym_op->cipher.data.length + icv_len) :
150                         sym_op->cipher.data.length;
151
152         DPAA2_SET_FLE_SG_EXT(fle);
153
154         /* Configure Output SGE for Encap/Decap */
155         DPAA2_SET_FLE_ADDR(sge, DPAA2_MBUF_VADDR_TO_IOVA(sym_op->m_src));
156         DPAA2_SET_FLE_OFFSET(sge, sym_op->cipher.data.offset +
157                                 sym_op->m_src->data_off);
158         sge->length = sym_op->cipher.data.length;
159
160         if (sess->dir == DIR_ENC) {
161                 sge++;
162                 DPAA2_SET_FLE_ADDR(sge,
163                                 DPAA2_VADDR_TO_IOVA(sym_op->auth.digest.data));
164                 sge->length = sym_op->auth.digest.length;
165                 DPAA2_SET_FD_LEN(fd, (sym_op->auth.data.length +
166                                         sess->iv.length));
167         }
168         DPAA2_SET_FLE_FIN(sge);
169
170         sge++;
171         fle++;
172
173         /* Configure Input FLE with Scatter/Gather Entry */
174         DPAA2_SET_FLE_ADDR(fle, DPAA2_VADDR_TO_IOVA(sge));
175         DPAA2_SET_FLE_SG_EXT(fle);
176         DPAA2_SET_FLE_FIN(fle);
177         fle->length = (sess->dir == DIR_ENC) ?
178                         (sym_op->auth.data.length + sess->iv.length) :
179                         (sym_op->auth.data.length + sess->iv.length +
180                          sym_op->auth.digest.length);
181
182         /* Configure Input SGE for Encap/Decap */
183         DPAA2_SET_FLE_ADDR(sge, DPAA2_VADDR_TO_IOVA(iv_ptr));
184         sge->length = sess->iv.length;
185         sge++;
186
187         DPAA2_SET_FLE_ADDR(sge, DPAA2_MBUF_VADDR_TO_IOVA(sym_op->m_src));
188         DPAA2_SET_FLE_OFFSET(sge, sym_op->auth.data.offset +
189                                 sym_op->m_src->data_off);
190         sge->length = sym_op->auth.data.length;
191         if (sess->dir == DIR_DEC) {
192                 sge++;
193                 old_icv = (uint8_t *)(sge + 1);
194                 memcpy(old_icv, sym_op->auth.digest.data,
195                        sym_op->auth.digest.length);
196                 memset(sym_op->auth.digest.data, 0, sym_op->auth.digest.length);
197                 DPAA2_SET_FLE_ADDR(sge, DPAA2_VADDR_TO_IOVA(old_icv));
198                 sge->length = sym_op->auth.digest.length;
199                 DPAA2_SET_FD_LEN(fd, (sym_op->auth.data.length +
200                                  sym_op->auth.digest.length +
201                                  sess->iv.length));
202         }
203         DPAA2_SET_FLE_FIN(sge);
204         if (auth_only_len) {
205                 DPAA2_SET_FLE_INTERNAL_JD(fle, auth_only_len);
206                 DPAA2_SET_FD_INTERNAL_JD(fd, auth_only_len);
207         }
208         return 0;
209 }
210
211 static inline int
212 build_auth_fd(dpaa2_sec_session *sess, struct rte_crypto_op *op,
213               struct qbman_fd *fd, uint16_t bpid)
214 {
215         struct rte_crypto_sym_op *sym_op = op->sym;
216         struct qbman_fle *fle, *sge;
217         uint32_t mem_len = (sess->dir == DIR_ENC) ?
218                            (3 * sizeof(struct qbman_fle)) :
219                            (5 * sizeof(struct qbman_fle) +
220                             sym_op->auth.digest.length);
221         struct sec_flow_context *flc;
222         struct ctxt_priv *priv = sess->ctxt;
223         uint8_t *old_digest;
224
225         PMD_INIT_FUNC_TRACE();
226
227         fle = rte_zmalloc(NULL, mem_len, RTE_CACHE_LINE_SIZE);
228         if (!fle) {
229                 RTE_LOG(ERR, PMD, "Memory alloc failed for FLE\n");
230                 return -1;
231         }
232         /* TODO we are using the first FLE entry to store Mbuf.
233          * Currently we donot know which FLE has the mbuf stored.
234          * So while retreiving we can go back 1 FLE from the FD -ADDR
235          * to get the MBUF Addr from the previous FLE.
236          * We can have a better approach to use the inline Mbuf
237          */
238         DPAA2_SET_FLE_ADDR(fle, DPAA2_OP_VADDR_TO_IOVA(op));
239         fle = fle + 1;
240
241         if (likely(bpid < MAX_BPID)) {
242                 DPAA2_SET_FD_BPID(fd, bpid);
243                 DPAA2_SET_FLE_BPID(fle, bpid);
244                 DPAA2_SET_FLE_BPID(fle + 1, bpid);
245         } else {
246                 DPAA2_SET_FD_IVP(fd);
247                 DPAA2_SET_FLE_IVP(fle);
248                 DPAA2_SET_FLE_IVP((fle + 1));
249         }
250         flc = &priv->flc_desc[DESC_INITFINAL].flc;
251         DPAA2_SET_FD_FLC(fd, DPAA2_VADDR_TO_IOVA(flc));
252
253         DPAA2_SET_FLE_ADDR(fle, DPAA2_VADDR_TO_IOVA(sym_op->auth.digest.data));
254         fle->length = sym_op->auth.digest.length;
255
256         DPAA2_SET_FD_ADDR(fd, DPAA2_VADDR_TO_IOVA(fle));
257         DPAA2_SET_FD_COMPOUND_FMT(fd);
258         fle++;
259
260         if (sess->dir == DIR_ENC) {
261                 DPAA2_SET_FLE_ADDR(fle,
262                                    DPAA2_MBUF_VADDR_TO_IOVA(sym_op->m_src));
263                 DPAA2_SET_FLE_OFFSET(fle, sym_op->auth.data.offset +
264                                      sym_op->m_src->data_off);
265                 DPAA2_SET_FD_LEN(fd, sym_op->auth.data.length);
266                 fle->length = sym_op->auth.data.length;
267         } else {
268                 sge = fle + 2;
269                 DPAA2_SET_FLE_SG_EXT(fle);
270                 DPAA2_SET_FLE_ADDR(fle, DPAA2_VADDR_TO_IOVA(sge));
271
272                 if (likely(bpid < MAX_BPID)) {
273                         DPAA2_SET_FLE_BPID(sge, bpid);
274                         DPAA2_SET_FLE_BPID(sge + 1, bpid);
275                 } else {
276                         DPAA2_SET_FLE_IVP(sge);
277                         DPAA2_SET_FLE_IVP((sge + 1));
278                 }
279                 DPAA2_SET_FLE_ADDR(sge,
280                                    DPAA2_MBUF_VADDR_TO_IOVA(sym_op->m_src));
281                 DPAA2_SET_FLE_OFFSET(sge, sym_op->auth.data.offset +
282                                      sym_op->m_src->data_off);
283
284                 DPAA2_SET_FD_LEN(fd, sym_op->auth.data.length +
285                                  sym_op->auth.digest.length);
286                 sge->length = sym_op->auth.data.length;
287                 sge++;
288                 old_digest = (uint8_t *)(sge + 1);
289                 rte_memcpy(old_digest, sym_op->auth.digest.data,
290                            sym_op->auth.digest.length);
291                 memset(sym_op->auth.digest.data, 0, sym_op->auth.digest.length);
292                 DPAA2_SET_FLE_ADDR(sge, DPAA2_VADDR_TO_IOVA(old_digest));
293                 sge->length = sym_op->auth.digest.length;
294                 fle->length = sym_op->auth.data.length +
295                                 sym_op->auth.digest.length;
296                 DPAA2_SET_FLE_FIN(sge);
297         }
298         DPAA2_SET_FLE_FIN(fle);
299
300         return 0;
301 }
302
303 static int
304 build_cipher_fd(dpaa2_sec_session *sess, struct rte_crypto_op *op,
305                 struct qbman_fd *fd, uint16_t bpid)
306 {
307         struct rte_crypto_sym_op *sym_op = op->sym;
308         struct qbman_fle *fle, *sge;
309         uint32_t mem_len = (5 * sizeof(struct qbman_fle));
310         struct sec_flow_context *flc;
311         struct ctxt_priv *priv = sess->ctxt;
312         uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
313                         sess->iv.offset);
314
315         PMD_INIT_FUNC_TRACE();
316
317         /* todo - we can use some mempool to avoid malloc here */
318         fle = rte_zmalloc(NULL, mem_len, RTE_CACHE_LINE_SIZE);
319         if (!fle) {
320                 RTE_LOG(ERR, PMD, "Memory alloc failed for SGE\n");
321                 return -1;
322         }
323         /* TODO we are using the first FLE entry to store Mbuf.
324          * Currently we donot know which FLE has the mbuf stored.
325          * So while retreiving we can go back 1 FLE from the FD -ADDR
326          * to get the MBUF Addr from the previous FLE.
327          * We can have a better approach to use the inline Mbuf
328          */
329         DPAA2_SET_FLE_ADDR(fle, DPAA2_OP_VADDR_TO_IOVA(op));
330         fle = fle + 1;
331         sge = fle + 2;
332
333         if (likely(bpid < MAX_BPID)) {
334                 DPAA2_SET_FD_BPID(fd, bpid);
335                 DPAA2_SET_FLE_BPID(fle, bpid);
336                 DPAA2_SET_FLE_BPID(fle + 1, bpid);
337                 DPAA2_SET_FLE_BPID(sge, bpid);
338                 DPAA2_SET_FLE_BPID(sge + 1, bpid);
339         } else {
340                 DPAA2_SET_FD_IVP(fd);
341                 DPAA2_SET_FLE_IVP(fle);
342                 DPAA2_SET_FLE_IVP((fle + 1));
343                 DPAA2_SET_FLE_IVP(sge);
344                 DPAA2_SET_FLE_IVP((sge + 1));
345         }
346
347         flc = &priv->flc_desc[0].flc;
348         DPAA2_SET_FD_ADDR(fd, DPAA2_VADDR_TO_IOVA(fle));
349         DPAA2_SET_FD_LEN(fd, sym_op->cipher.data.length +
350                          sess->iv.length);
351         DPAA2_SET_FD_COMPOUND_FMT(fd);
352         DPAA2_SET_FD_FLC(fd, DPAA2_VADDR_TO_IOVA(flc));
353
354         PMD_TX_LOG(DEBUG, "cipher_off: 0x%x/length %d,ivlen=%d data_off: 0x%x",
355                    sym_op->cipher.data.offset,
356                    sym_op->cipher.data.length,
357                    sess->iv.length,
358                    sym_op->m_src->data_off);
359
360         DPAA2_SET_FLE_ADDR(fle, DPAA2_MBUF_VADDR_TO_IOVA(sym_op->m_src));
361         DPAA2_SET_FLE_OFFSET(fle, sym_op->cipher.data.offset +
362                              sym_op->m_src->data_off);
363
364         fle->length = sym_op->cipher.data.length + sess->iv.length;
365
366         PMD_TX_LOG(DEBUG, "1 - flc = %p, fle = %p FLEaddr = %x-%x, length %d",
367                    flc, fle, fle->addr_hi, fle->addr_lo, fle->length);
368
369         fle++;
370
371         DPAA2_SET_FLE_ADDR(fle, DPAA2_VADDR_TO_IOVA(sge));
372         fle->length = sym_op->cipher.data.length + sess->iv.length;
373
374         DPAA2_SET_FLE_SG_EXT(fle);
375
376         DPAA2_SET_FLE_ADDR(sge, DPAA2_VADDR_TO_IOVA(iv_ptr));
377         sge->length = sess->iv.length;
378
379         sge++;
380         DPAA2_SET_FLE_ADDR(sge, DPAA2_MBUF_VADDR_TO_IOVA(sym_op->m_src));
381         DPAA2_SET_FLE_OFFSET(sge, sym_op->cipher.data.offset +
382                              sym_op->m_src->data_off);
383
384         sge->length = sym_op->cipher.data.length;
385         DPAA2_SET_FLE_FIN(sge);
386         DPAA2_SET_FLE_FIN(fle);
387
388         PMD_TX_LOG(DEBUG, "fdaddr =%p bpid =%d meta =%d off =%d, len =%d",
389                    (void *)DPAA2_GET_FD_ADDR(fd),
390                    DPAA2_GET_FD_BPID(fd),
391                    rte_dpaa2_bpid_info[bpid].meta_data_size,
392                    DPAA2_GET_FD_OFFSET(fd),
393                    DPAA2_GET_FD_LEN(fd));
394
395         return 0;
396 }
397
398 static inline int
399 build_sec_fd(dpaa2_sec_session *sess, struct rte_crypto_op *op,
400              struct qbman_fd *fd, uint16_t bpid)
401 {
402         int ret = -1;
403
404         PMD_INIT_FUNC_TRACE();
405
406         switch (sess->ctxt_type) {
407         case DPAA2_SEC_CIPHER:
408                 ret = build_cipher_fd(sess, op, fd, bpid);
409                 break;
410         case DPAA2_SEC_AUTH:
411                 ret = build_auth_fd(sess, op, fd, bpid);
412                 break;
413         case DPAA2_SEC_CIPHER_HASH:
414                 ret = build_authenc_fd(sess, op, fd, bpid);
415                 break;
416         case DPAA2_SEC_HASH_CIPHER:
417         default:
418                 RTE_LOG(ERR, PMD, "error: Unsupported session\n");
419         }
420         return ret;
421 }
422
423 static uint16_t
424 dpaa2_sec_enqueue_burst(void *qp, struct rte_crypto_op **ops,
425                         uint16_t nb_ops)
426 {
427         /* Function to transmit the frames to given device and VQ*/
428         uint32_t loop;
429         int32_t ret;
430         struct qbman_fd fd_arr[MAX_TX_RING_SLOTS];
431         uint32_t frames_to_send;
432         struct qbman_eq_desc eqdesc;
433         struct dpaa2_sec_qp *dpaa2_qp = (struct dpaa2_sec_qp *)qp;
434         struct qbman_swp *swp;
435         uint16_t num_tx = 0;
436         /*todo - need to support multiple buffer pools */
437         uint16_t bpid;
438         struct rte_mempool *mb_pool;
439         dpaa2_sec_session *sess;
440
441         if (unlikely(nb_ops == 0))
442                 return 0;
443
444         if (ops[0]->sess_type != RTE_CRYPTO_OP_WITH_SESSION) {
445                 RTE_LOG(ERR, PMD, "sessionless crypto op not supported\n");
446                 return 0;
447         }
448         /*Prepare enqueue descriptor*/
449         qbman_eq_desc_clear(&eqdesc);
450         qbman_eq_desc_set_no_orp(&eqdesc, DPAA2_EQ_RESP_ERR_FQ);
451         qbman_eq_desc_set_response(&eqdesc, 0, 0);
452         qbman_eq_desc_set_fq(&eqdesc, dpaa2_qp->tx_vq.fqid);
453
454         if (!DPAA2_PER_LCORE_SEC_DPIO) {
455                 ret = dpaa2_affine_qbman_swp_sec();
456                 if (ret) {
457                         RTE_LOG(ERR, PMD, "Failure in affining portal\n");
458                         return 0;
459                 }
460         }
461         swp = DPAA2_PER_LCORE_SEC_PORTAL;
462
463         while (nb_ops) {
464                 frames_to_send = (nb_ops >> 3) ? MAX_TX_RING_SLOTS : nb_ops;
465
466                 for (loop = 0; loop < frames_to_send; loop++) {
467                         /*Clear the unused FD fields before sending*/
468                         memset(&fd_arr[loop], 0, sizeof(struct qbman_fd));
469                         sess = (dpaa2_sec_session *)
470                                 (*ops)->sym->session->_private;
471                         mb_pool = (*ops)->sym->m_src->pool;
472                         bpid = mempool_to_bpid(mb_pool);
473                         ret = build_sec_fd(sess, *ops, &fd_arr[loop], bpid);
474                         if (ret) {
475                                 PMD_DRV_LOG(ERR, "error: Improper packet"
476                                             " contents for crypto operation\n");
477                                 goto skip_tx;
478                         }
479                         ops++;
480                 }
481                 loop = 0;
482                 while (loop < frames_to_send) {
483                         loop += qbman_swp_send_multiple(swp, &eqdesc,
484                                                         &fd_arr[loop],
485                                                         frames_to_send - loop);
486                 }
487
488                 num_tx += frames_to_send;
489                 nb_ops -= frames_to_send;
490         }
491 skip_tx:
492         dpaa2_qp->tx_vq.tx_pkts += num_tx;
493         dpaa2_qp->tx_vq.err_pkts += nb_ops;
494         return num_tx;
495 }
496
497 static inline struct rte_crypto_op *
498 sec_fd_to_mbuf(const struct qbman_fd *fd)
499 {
500         struct qbman_fle *fle;
501         struct rte_crypto_op *op;
502
503         fle = (struct qbman_fle *)DPAA2_IOVA_TO_VADDR(DPAA2_GET_FD_ADDR(fd));
504
505         PMD_RX_LOG(DEBUG, "FLE addr = %x - %x, offset = %x",
506                    fle->addr_hi, fle->addr_lo, fle->fin_bpid_offset);
507
508         /* we are using the first FLE entry to store Mbuf.
509          * Currently we donot know which FLE has the mbuf stored.
510          * So while retreiving we can go back 1 FLE from the FD -ADDR
511          * to get the MBUF Addr from the previous FLE.
512          * We can have a better approach to use the inline Mbuf
513          */
514
515         if (unlikely(DPAA2_GET_FD_IVP(fd))) {
516                 /* TODO complete it. */
517                 RTE_LOG(ERR, PMD, "error: Non inline buffer - WHAT to DO?");
518                 return NULL;
519         }
520         op = (struct rte_crypto_op *)DPAA2_IOVA_TO_VADDR(
521                         DPAA2_GET_FLE_ADDR((fle - 1)));
522
523         /* Prefeth op */
524         rte_prefetch0(op->sym->m_src);
525
526         PMD_RX_LOG(DEBUG, "mbuf %p BMAN buf addr %p",
527                    (void *)op->sym->m_src, op->sym->m_src->buf_addr);
528
529         PMD_RX_LOG(DEBUG, "fdaddr =%p bpid =%d meta =%d off =%d, len =%d",
530                    (void *)DPAA2_GET_FD_ADDR(fd),
531                    DPAA2_GET_FD_BPID(fd),
532                    rte_dpaa2_bpid_info[DPAA2_GET_FD_BPID(fd)].meta_data_size,
533                    DPAA2_GET_FD_OFFSET(fd),
534                    DPAA2_GET_FD_LEN(fd));
535
536         /* free the fle memory */
537         rte_free(fle - 1);
538
539         return op;
540 }
541
542 static uint16_t
543 dpaa2_sec_dequeue_burst(void *qp, struct rte_crypto_op **ops,
544                         uint16_t nb_ops)
545 {
546         /* Function is responsible to receive frames for a given device and VQ*/
547         struct dpaa2_sec_qp *dpaa2_qp = (struct dpaa2_sec_qp *)qp;
548         struct qbman_result *dq_storage;
549         uint32_t fqid = dpaa2_qp->rx_vq.fqid;
550         int ret, num_rx = 0;
551         uint8_t is_last = 0, status;
552         struct qbman_swp *swp;
553         const struct qbman_fd *fd;
554         struct qbman_pull_desc pulldesc;
555
556         if (!DPAA2_PER_LCORE_SEC_DPIO) {
557                 ret = dpaa2_affine_qbman_swp_sec();
558                 if (ret) {
559                         RTE_LOG(ERR, PMD, "Failure in affining portal\n");
560                         return 0;
561                 }
562         }
563         swp = DPAA2_PER_LCORE_SEC_PORTAL;
564         dq_storage = dpaa2_qp->rx_vq.q_storage->dq_storage[0];
565
566         qbman_pull_desc_clear(&pulldesc);
567         qbman_pull_desc_set_numframes(&pulldesc,
568                                       (nb_ops > DPAA2_DQRR_RING_SIZE) ?
569                                       DPAA2_DQRR_RING_SIZE : nb_ops);
570         qbman_pull_desc_set_fq(&pulldesc, fqid);
571         qbman_pull_desc_set_storage(&pulldesc, dq_storage,
572                                     (dma_addr_t)DPAA2_VADDR_TO_IOVA(dq_storage),
573                                     1);
574
575         /*Issue a volatile dequeue command. */
576         while (1) {
577                 if (qbman_swp_pull(swp, &pulldesc)) {
578                         RTE_LOG(WARNING, PMD, "SEC VDQ command is not issued."
579                                 "QBMAN is busy\n");
580                         /* Portal was busy, try again */
581                         continue;
582                 }
583                 break;
584         };
585
586         /* Receive the packets till Last Dequeue entry is found with
587          * respect to the above issues PULL command.
588          */
589         while (!is_last) {
590                 /* Check if the previous issued command is completed.
591                  * Also seems like the SWP is shared between the Ethernet Driver
592                  * and the SEC driver.
593                  */
594                 while (!qbman_check_command_complete(swp, dq_storage))
595                         ;
596
597                 /* Loop until the dq_storage is updated with
598                  * new token by QBMAN
599                  */
600                 while (!qbman_result_has_new_result(swp, dq_storage))
601                         ;
602                 /* Check whether Last Pull command is Expired and
603                  * setting Condition for Loop termination
604                  */
605                 if (qbman_result_DQ_is_pull_complete(dq_storage)) {
606                         is_last = 1;
607                         /* Check for valid frame. */
608                         status = (uint8_t)qbman_result_DQ_flags(dq_storage);
609                         if (unlikely(
610                                 (status & QBMAN_DQ_STAT_VALIDFRAME) == 0)) {
611                                 PMD_RX_LOG(DEBUG, "No frame is delivered");
612                                 continue;
613                         }
614                 }
615
616                 fd = qbman_result_DQ_fd(dq_storage);
617                 ops[num_rx] = sec_fd_to_mbuf(fd);
618
619                 if (unlikely(fd->simple.frc)) {
620                         /* TODO Parse SEC errors */
621                         RTE_LOG(ERR, PMD, "SEC returned Error - %x\n",
622                                 fd->simple.frc);
623                         ops[num_rx]->status = RTE_CRYPTO_OP_STATUS_ERROR;
624                 } else {
625                         ops[num_rx]->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
626                 }
627
628                 num_rx++;
629                 dq_storage++;
630         } /* End of Packet Rx loop */
631
632         dpaa2_qp->rx_vq.rx_pkts += num_rx;
633
634         PMD_RX_LOG(DEBUG, "SEC Received %d Packets", num_rx);
635         /*Return the total number of packets received to DPAA2 app*/
636         return num_rx;
637 }
638
639 /** Release queue pair */
640 static int
641 dpaa2_sec_queue_pair_release(struct rte_cryptodev *dev, uint16_t queue_pair_id)
642 {
643         struct dpaa2_sec_qp *qp =
644                 (struct dpaa2_sec_qp *)dev->data->queue_pairs[queue_pair_id];
645
646         PMD_INIT_FUNC_TRACE();
647
648         if (qp->rx_vq.q_storage) {
649                 dpaa2_free_dq_storage(qp->rx_vq.q_storage);
650                 rte_free(qp->rx_vq.q_storage);
651         }
652         rte_free(qp);
653
654         dev->data->queue_pairs[queue_pair_id] = NULL;
655
656         return 0;
657 }
658
659 /** Setup a queue pair */
660 static int
661 dpaa2_sec_queue_pair_setup(struct rte_cryptodev *dev, uint16_t qp_id,
662                 __rte_unused const struct rte_cryptodev_qp_conf *qp_conf,
663                 __rte_unused int socket_id)
664 {
665         struct dpaa2_sec_dev_private *priv = dev->data->dev_private;
666         struct dpaa2_sec_qp *qp;
667         struct fsl_mc_io *dpseci = (struct fsl_mc_io *)priv->hw;
668         struct dpseci_rx_queue_cfg cfg;
669         int32_t retcode;
670
671         PMD_INIT_FUNC_TRACE();
672
673         /* If qp is already in use free ring memory and qp metadata. */
674         if (dev->data->queue_pairs[qp_id] != NULL) {
675                 PMD_DRV_LOG(INFO, "QP already setup");
676                 return 0;
677         }
678
679         PMD_DRV_LOG(DEBUG, "dev =%p, queue =%d, conf =%p",
680                     dev, qp_id, qp_conf);
681
682         memset(&cfg, 0, sizeof(struct dpseci_rx_queue_cfg));
683
684         qp = rte_malloc(NULL, sizeof(struct dpaa2_sec_qp),
685                         RTE_CACHE_LINE_SIZE);
686         if (!qp) {
687                 RTE_LOG(ERR, PMD, "malloc failed for rx/tx queues\n");
688                 return -1;
689         }
690
691         qp->rx_vq.dev = dev;
692         qp->tx_vq.dev = dev;
693         qp->rx_vq.q_storage = rte_malloc("sec dq storage",
694                 sizeof(struct queue_storage_info_t),
695                 RTE_CACHE_LINE_SIZE);
696         if (!qp->rx_vq.q_storage) {
697                 RTE_LOG(ERR, PMD, "malloc failed for q_storage\n");
698                 return -1;
699         }
700         memset(qp->rx_vq.q_storage, 0, sizeof(struct queue_storage_info_t));
701
702         if (dpaa2_alloc_dq_storage(qp->rx_vq.q_storage)) {
703                 RTE_LOG(ERR, PMD, "dpaa2_alloc_dq_storage failed\n");
704                 return -1;
705         }
706
707         dev->data->queue_pairs[qp_id] = qp;
708
709         cfg.options = cfg.options | DPSECI_QUEUE_OPT_USER_CTX;
710         cfg.user_ctx = (uint64_t)(&qp->rx_vq);
711         retcode = dpseci_set_rx_queue(dpseci, CMD_PRI_LOW, priv->token,
712                                       qp_id, &cfg);
713         return retcode;
714 }
715
716 /** Start queue pair */
717 static int
718 dpaa2_sec_queue_pair_start(__rte_unused struct rte_cryptodev *dev,
719                            __rte_unused uint16_t queue_pair_id)
720 {
721         PMD_INIT_FUNC_TRACE();
722
723         return 0;
724 }
725
726 /** Stop queue pair */
727 static int
728 dpaa2_sec_queue_pair_stop(__rte_unused struct rte_cryptodev *dev,
729                           __rte_unused uint16_t queue_pair_id)
730 {
731         PMD_INIT_FUNC_TRACE();
732
733         return 0;
734 }
735
736 /** Return the number of allocated queue pairs */
737 static uint32_t
738 dpaa2_sec_queue_pair_count(struct rte_cryptodev *dev)
739 {
740         PMD_INIT_FUNC_TRACE();
741
742         return dev->data->nb_queue_pairs;
743 }
744
745 /** Returns the size of the aesni gcm session structure */
746 static unsigned int
747 dpaa2_sec_session_get_size(struct rte_cryptodev *dev __rte_unused)
748 {
749         PMD_INIT_FUNC_TRACE();
750
751         return sizeof(dpaa2_sec_session);
752 }
753
754 static void
755 dpaa2_sec_session_initialize(struct rte_mempool *mp __rte_unused,
756                              void *sess __rte_unused)
757 {
758         PMD_INIT_FUNC_TRACE();
759 }
760
761 static int
762 dpaa2_sec_cipher_init(struct rte_cryptodev *dev,
763                       struct rte_crypto_sym_xform *xform,
764                       dpaa2_sec_session *session)
765 {
766         struct dpaa2_sec_cipher_ctxt *ctxt = &session->ext_params.cipher_ctxt;
767         struct alginfo cipherdata;
768         int bufsize, i;
769         struct ctxt_priv *priv;
770         struct sec_flow_context *flc;
771
772         PMD_INIT_FUNC_TRACE();
773
774         /* For SEC CIPHER only one descriptor is required. */
775         priv = (struct ctxt_priv *)rte_zmalloc(NULL,
776                         sizeof(struct ctxt_priv) + sizeof(struct sec_flc_desc),
777                         RTE_CACHE_LINE_SIZE);
778         if (priv == NULL) {
779                 RTE_LOG(ERR, PMD, "No Memory for priv CTXT");
780                 return -1;
781         }
782
783         flc = &priv->flc_desc[0].flc;
784
785         session->cipher_key.data = rte_zmalloc(NULL, xform->cipher.key.length,
786                         RTE_CACHE_LINE_SIZE);
787         if (session->cipher_key.data == NULL) {
788                 RTE_LOG(ERR, PMD, "No Memory for cipher key");
789                 rte_free(priv);
790                 return -1;
791         }
792         session->cipher_key.length = xform->cipher.key.length;
793
794         memcpy(session->cipher_key.data, xform->cipher.key.data,
795                xform->cipher.key.length);
796         cipherdata.key = (uint64_t)session->cipher_key.data;
797         cipherdata.keylen = session->cipher_key.length;
798         cipherdata.key_enc_flags = 0;
799         cipherdata.key_type = RTA_DATA_IMM;
800
801         /* Set IV parameters */
802         session->iv.offset = xform->cipher.iv.offset;
803         session->iv.length = xform->cipher.iv.length;
804
805         switch (xform->cipher.algo) {
806         case RTE_CRYPTO_CIPHER_AES_CBC:
807                 cipherdata.algtype = OP_ALG_ALGSEL_AES;
808                 cipherdata.algmode = OP_ALG_AAI_CBC;
809                 session->cipher_alg = RTE_CRYPTO_CIPHER_AES_CBC;
810                 ctxt->iv.length = AES_CBC_IV_LEN;
811                 break;
812         case RTE_CRYPTO_CIPHER_3DES_CBC:
813                 cipherdata.algtype = OP_ALG_ALGSEL_3DES;
814                 cipherdata.algmode = OP_ALG_AAI_CBC;
815                 session->cipher_alg = RTE_CRYPTO_CIPHER_3DES_CBC;
816                 ctxt->iv.length = TDES_CBC_IV_LEN;
817                 break;
818         case RTE_CRYPTO_CIPHER_AES_CTR:
819         case RTE_CRYPTO_CIPHER_3DES_CTR:
820         case RTE_CRYPTO_CIPHER_AES_GCM:
821         case RTE_CRYPTO_CIPHER_AES_CCM:
822         case RTE_CRYPTO_CIPHER_AES_ECB:
823         case RTE_CRYPTO_CIPHER_3DES_ECB:
824         case RTE_CRYPTO_CIPHER_AES_XTS:
825         case RTE_CRYPTO_CIPHER_AES_F8:
826         case RTE_CRYPTO_CIPHER_ARC4:
827         case RTE_CRYPTO_CIPHER_KASUMI_F8:
828         case RTE_CRYPTO_CIPHER_SNOW3G_UEA2:
829         case RTE_CRYPTO_CIPHER_ZUC_EEA3:
830         case RTE_CRYPTO_CIPHER_NULL:
831                 RTE_LOG(ERR, PMD, "Crypto: Unsupported Cipher alg %u",
832                         xform->cipher.algo);
833                 goto error_out;
834         default:
835                 RTE_LOG(ERR, PMD, "Crypto: Undefined Cipher specified %u\n",
836                         xform->cipher.algo);
837                 goto error_out;
838         }
839         session->dir = (xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
840                                 DIR_ENC : DIR_DEC;
841
842         bufsize = cnstr_shdsc_blkcipher(priv->flc_desc[0].desc, 1, 0,
843                                         &cipherdata, NULL, ctxt->iv.length,
844                         session->dir);
845         if (bufsize < 0) {
846                 RTE_LOG(ERR, PMD, "Crypto: Descriptor build failed\n");
847                 goto error_out;
848         }
849         flc->dhr = 0;
850         flc->bpv0 = 0x1;
851         flc->mode_bits = 0x8000;
852
853         flc->word1_sdl = (uint8_t)bufsize;
854         flc->word2_rflc_31_0 = lower_32_bits(
855                         (uint64_t)&(((struct dpaa2_sec_qp *)
856                         dev->data->queue_pairs[0])->rx_vq));
857         flc->word3_rflc_63_32 = upper_32_bits(
858                         (uint64_t)&(((struct dpaa2_sec_qp *)
859                         dev->data->queue_pairs[0])->rx_vq));
860         session->ctxt = priv;
861
862         for (i = 0; i < bufsize; i++)
863                 PMD_DRV_LOG(DEBUG, "DESC[%d]:0x%x\n",
864                             i, priv->flc_desc[0].desc[i]);
865
866         return 0;
867
868 error_out:
869         rte_free(session->cipher_key.data);
870         rte_free(priv);
871         return -1;
872 }
873
874 static int
875 dpaa2_sec_auth_init(struct rte_cryptodev *dev,
876                     struct rte_crypto_sym_xform *xform,
877                     dpaa2_sec_session *session)
878 {
879         struct dpaa2_sec_auth_ctxt *ctxt = &session->ext_params.auth_ctxt;
880         struct alginfo authdata;
881         unsigned int bufsize;
882         struct ctxt_priv *priv;
883         struct sec_flow_context *flc;
884
885         PMD_INIT_FUNC_TRACE();
886
887         /* For SEC AUTH three descriptors are required for various stages */
888         priv = (struct ctxt_priv *)rte_zmalloc(NULL,
889                         sizeof(struct ctxt_priv) + 3 *
890                         sizeof(struct sec_flc_desc),
891                         RTE_CACHE_LINE_SIZE);
892         if (priv == NULL) {
893                 RTE_LOG(ERR, PMD, "No Memory for priv CTXT");
894                 return -1;
895         }
896
897         flc = &priv->flc_desc[DESC_INITFINAL].flc;
898
899         session->auth_key.data = rte_zmalloc(NULL, xform->auth.key.length,
900                         RTE_CACHE_LINE_SIZE);
901         if (session->auth_key.data == NULL) {
902                 RTE_LOG(ERR, PMD, "No Memory for auth key");
903                 rte_free(priv);
904                 return -1;
905         }
906         session->auth_key.length = xform->auth.key.length;
907
908         memcpy(session->auth_key.data, xform->auth.key.data,
909                xform->auth.key.length);
910         authdata.key = (uint64_t)session->auth_key.data;
911         authdata.keylen = session->auth_key.length;
912         authdata.key_enc_flags = 0;
913         authdata.key_type = RTA_DATA_IMM;
914
915         switch (xform->auth.algo) {
916         case RTE_CRYPTO_AUTH_SHA1_HMAC:
917                 authdata.algtype = OP_ALG_ALGSEL_SHA1;
918                 authdata.algmode = OP_ALG_AAI_HMAC;
919                 session->auth_alg = RTE_CRYPTO_AUTH_SHA1_HMAC;
920                 break;
921         case RTE_CRYPTO_AUTH_MD5_HMAC:
922                 authdata.algtype = OP_ALG_ALGSEL_MD5;
923                 authdata.algmode = OP_ALG_AAI_HMAC;
924                 session->auth_alg = RTE_CRYPTO_AUTH_MD5_HMAC;
925                 break;
926         case RTE_CRYPTO_AUTH_SHA256_HMAC:
927                 authdata.algtype = OP_ALG_ALGSEL_SHA256;
928                 authdata.algmode = OP_ALG_AAI_HMAC;
929                 session->auth_alg = RTE_CRYPTO_AUTH_SHA256_HMAC;
930                 break;
931         case RTE_CRYPTO_AUTH_SHA384_HMAC:
932                 authdata.algtype = OP_ALG_ALGSEL_SHA384;
933                 authdata.algmode = OP_ALG_AAI_HMAC;
934                 session->auth_alg = RTE_CRYPTO_AUTH_SHA384_HMAC;
935                 break;
936         case RTE_CRYPTO_AUTH_SHA512_HMAC:
937                 authdata.algtype = OP_ALG_ALGSEL_SHA512;
938                 authdata.algmode = OP_ALG_AAI_HMAC;
939                 session->auth_alg = RTE_CRYPTO_AUTH_SHA512_HMAC;
940                 break;
941         case RTE_CRYPTO_AUTH_SHA224_HMAC:
942                 authdata.algtype = OP_ALG_ALGSEL_SHA224;
943                 authdata.algmode = OP_ALG_AAI_HMAC;
944                 session->auth_alg = RTE_CRYPTO_AUTH_SHA224_HMAC;
945                 break;
946         case RTE_CRYPTO_AUTH_AES_XCBC_MAC:
947         case RTE_CRYPTO_AUTH_AES_GCM:
948         case RTE_CRYPTO_AUTH_SNOW3G_UIA2:
949         case RTE_CRYPTO_AUTH_NULL:
950         case RTE_CRYPTO_AUTH_SHA1:
951         case RTE_CRYPTO_AUTH_SHA256:
952         case RTE_CRYPTO_AUTH_SHA512:
953         case RTE_CRYPTO_AUTH_SHA224:
954         case RTE_CRYPTO_AUTH_SHA384:
955         case RTE_CRYPTO_AUTH_MD5:
956         case RTE_CRYPTO_AUTH_AES_CCM:
957         case RTE_CRYPTO_AUTH_AES_GMAC:
958         case RTE_CRYPTO_AUTH_KASUMI_F9:
959         case RTE_CRYPTO_AUTH_AES_CMAC:
960         case RTE_CRYPTO_AUTH_AES_CBC_MAC:
961         case RTE_CRYPTO_AUTH_ZUC_EIA3:
962                 RTE_LOG(ERR, PMD, "Crypto: Unsupported auth alg %u",
963                         xform->auth.algo);
964                 goto error_out;
965         default:
966                 RTE_LOG(ERR, PMD, "Crypto: Undefined Auth specified %u\n",
967                         xform->auth.algo);
968                 goto error_out;
969         }
970         session->dir = (xform->auth.op == RTE_CRYPTO_AUTH_OP_GENERATE) ?
971                                 DIR_ENC : DIR_DEC;
972
973         bufsize = cnstr_shdsc_hmac(priv->flc_desc[DESC_INITFINAL].desc,
974                                    1, 0, &authdata, !session->dir,
975                                    ctxt->trunc_len);
976
977         flc->word1_sdl = (uint8_t)bufsize;
978         flc->word2_rflc_31_0 = lower_32_bits(
979                         (uint64_t)&(((struct dpaa2_sec_qp *)
980                         dev->data->queue_pairs[0])->rx_vq));
981         flc->word3_rflc_63_32 = upper_32_bits(
982                         (uint64_t)&(((struct dpaa2_sec_qp *)
983                         dev->data->queue_pairs[0])->rx_vq));
984         session->ctxt = priv;
985
986         return 0;
987
988 error_out:
989         rte_free(session->auth_key.data);
990         rte_free(priv);
991         return -1;
992 }
993
994 static int
995 dpaa2_sec_aead_init(struct rte_cryptodev *dev,
996                     struct rte_crypto_sym_xform *xform,
997                     dpaa2_sec_session *session)
998 {
999         struct dpaa2_sec_aead_ctxt *ctxt = &session->ext_params.aead_ctxt;
1000         struct alginfo authdata, cipherdata;
1001         unsigned int bufsize;
1002         struct ctxt_priv *priv;
1003         struct sec_flow_context *flc;
1004         struct rte_crypto_cipher_xform *cipher_xform;
1005         struct rte_crypto_auth_xform *auth_xform;
1006         int err;
1007
1008         PMD_INIT_FUNC_TRACE();
1009
1010         if (session->ext_params.aead_ctxt.auth_cipher_text) {
1011                 cipher_xform = &xform->cipher;
1012                 auth_xform = &xform->next->auth;
1013                 session->ctxt_type =
1014                         (cipher_xform->op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
1015                         DPAA2_SEC_CIPHER_HASH : DPAA2_SEC_HASH_CIPHER;
1016         } else {
1017                 cipher_xform = &xform->next->cipher;
1018                 auth_xform = &xform->auth;
1019                 session->ctxt_type =
1020                         (cipher_xform->op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
1021                         DPAA2_SEC_HASH_CIPHER : DPAA2_SEC_CIPHER_HASH;
1022         }
1023
1024         /* Set IV parameters */
1025         session->iv.offset = cipher_xform->iv.offset;
1026         session->iv.length = cipher_xform->iv.length;
1027
1028         /* For SEC AEAD only one descriptor is required */
1029         priv = (struct ctxt_priv *)rte_zmalloc(NULL,
1030                         sizeof(struct ctxt_priv) + sizeof(struct sec_flc_desc),
1031                         RTE_CACHE_LINE_SIZE);
1032         if (priv == NULL) {
1033                 RTE_LOG(ERR, PMD, "No Memory for priv CTXT");
1034                 return -1;
1035         }
1036
1037         flc = &priv->flc_desc[0].flc;
1038
1039         session->cipher_key.data = rte_zmalloc(NULL, cipher_xform->key.length,
1040                                                RTE_CACHE_LINE_SIZE);
1041         if (session->cipher_key.data == NULL && cipher_xform->key.length > 0) {
1042                 RTE_LOG(ERR, PMD, "No Memory for cipher key");
1043                 rte_free(priv);
1044                 return -1;
1045         }
1046         session->cipher_key.length = cipher_xform->key.length;
1047         session->auth_key.data = rte_zmalloc(NULL, auth_xform->key.length,
1048                                              RTE_CACHE_LINE_SIZE);
1049         if (session->auth_key.data == NULL && auth_xform->key.length > 0) {
1050                 RTE_LOG(ERR, PMD, "No Memory for auth key");
1051                 rte_free(session->cipher_key.data);
1052                 rte_free(priv);
1053                 return -1;
1054         }
1055         session->auth_key.length = auth_xform->key.length;
1056         memcpy(session->cipher_key.data, cipher_xform->key.data,
1057                cipher_xform->key.length);
1058         memcpy(session->auth_key.data, auth_xform->key.data,
1059                auth_xform->key.length);
1060
1061         ctxt->trunc_len = auth_xform->digest_length;
1062         authdata.key = (uint64_t)session->auth_key.data;
1063         authdata.keylen = session->auth_key.length;
1064         authdata.key_enc_flags = 0;
1065         authdata.key_type = RTA_DATA_IMM;
1066
1067         switch (auth_xform->algo) {
1068         case RTE_CRYPTO_AUTH_SHA1_HMAC:
1069                 authdata.algtype = OP_ALG_ALGSEL_SHA1;
1070                 authdata.algmode = OP_ALG_AAI_HMAC;
1071                 session->auth_alg = RTE_CRYPTO_AUTH_SHA1_HMAC;
1072                 break;
1073         case RTE_CRYPTO_AUTH_MD5_HMAC:
1074                 authdata.algtype = OP_ALG_ALGSEL_MD5;
1075                 authdata.algmode = OP_ALG_AAI_HMAC;
1076                 session->auth_alg = RTE_CRYPTO_AUTH_MD5_HMAC;
1077                 break;
1078         case RTE_CRYPTO_AUTH_SHA224_HMAC:
1079                 authdata.algtype = OP_ALG_ALGSEL_SHA224;
1080                 authdata.algmode = OP_ALG_AAI_HMAC;
1081                 session->auth_alg = RTE_CRYPTO_AUTH_SHA224_HMAC;
1082                 break;
1083         case RTE_CRYPTO_AUTH_SHA256_HMAC:
1084                 authdata.algtype = OP_ALG_ALGSEL_SHA256;
1085                 authdata.algmode = OP_ALG_AAI_HMAC;
1086                 session->auth_alg = RTE_CRYPTO_AUTH_SHA256_HMAC;
1087                 break;
1088         case RTE_CRYPTO_AUTH_SHA384_HMAC:
1089                 authdata.algtype = OP_ALG_ALGSEL_SHA384;
1090                 authdata.algmode = OP_ALG_AAI_HMAC;
1091                 session->auth_alg = RTE_CRYPTO_AUTH_SHA384_HMAC;
1092                 break;
1093         case RTE_CRYPTO_AUTH_SHA512_HMAC:
1094                 authdata.algtype = OP_ALG_ALGSEL_SHA512;
1095                 authdata.algmode = OP_ALG_AAI_HMAC;
1096                 session->auth_alg = RTE_CRYPTO_AUTH_SHA512_HMAC;
1097                 break;
1098         case RTE_CRYPTO_AUTH_AES_XCBC_MAC:
1099         case RTE_CRYPTO_AUTH_AES_GCM:
1100         case RTE_CRYPTO_AUTH_SNOW3G_UIA2:
1101         case RTE_CRYPTO_AUTH_NULL:
1102         case RTE_CRYPTO_AUTH_SHA1:
1103         case RTE_CRYPTO_AUTH_SHA256:
1104         case RTE_CRYPTO_AUTH_SHA512:
1105         case RTE_CRYPTO_AUTH_SHA224:
1106         case RTE_CRYPTO_AUTH_SHA384:
1107         case RTE_CRYPTO_AUTH_MD5:
1108         case RTE_CRYPTO_AUTH_AES_CCM:
1109         case RTE_CRYPTO_AUTH_AES_GMAC:
1110         case RTE_CRYPTO_AUTH_KASUMI_F9:
1111         case RTE_CRYPTO_AUTH_AES_CMAC:
1112         case RTE_CRYPTO_AUTH_AES_CBC_MAC:
1113         case RTE_CRYPTO_AUTH_ZUC_EIA3:
1114                 RTE_LOG(ERR, PMD, "Crypto: Unsupported auth alg %u",
1115                         auth_xform->algo);
1116                 goto error_out;
1117         default:
1118                 RTE_LOG(ERR, PMD, "Crypto: Undefined Auth specified %u\n",
1119                         auth_xform->algo);
1120                 goto error_out;
1121         }
1122         cipherdata.key = (uint64_t)session->cipher_key.data;
1123         cipherdata.keylen = session->cipher_key.length;
1124         cipherdata.key_enc_flags = 0;
1125         cipherdata.key_type = RTA_DATA_IMM;
1126
1127         switch (cipher_xform->algo) {
1128         case RTE_CRYPTO_CIPHER_AES_CBC:
1129                 cipherdata.algtype = OP_ALG_ALGSEL_AES;
1130                 cipherdata.algmode = OP_ALG_AAI_CBC;
1131                 session->cipher_alg = RTE_CRYPTO_CIPHER_AES_CBC;
1132                 ctxt->iv.length = AES_CBC_IV_LEN;
1133                 break;
1134         case RTE_CRYPTO_CIPHER_3DES_CBC:
1135                 cipherdata.algtype = OP_ALG_ALGSEL_3DES;
1136                 cipherdata.algmode = OP_ALG_AAI_CBC;
1137                 session->cipher_alg = RTE_CRYPTO_CIPHER_3DES_CBC;
1138                 ctxt->iv.length = TDES_CBC_IV_LEN;
1139                 break;
1140         case RTE_CRYPTO_CIPHER_AES_GCM:
1141         case RTE_CRYPTO_CIPHER_SNOW3G_UEA2:
1142         case RTE_CRYPTO_CIPHER_NULL:
1143         case RTE_CRYPTO_CIPHER_3DES_ECB:
1144         case RTE_CRYPTO_CIPHER_AES_ECB:
1145         case RTE_CRYPTO_CIPHER_AES_CTR:
1146         case RTE_CRYPTO_CIPHER_AES_CCM:
1147         case RTE_CRYPTO_CIPHER_KASUMI_F8:
1148                 RTE_LOG(ERR, PMD, "Crypto: Unsupported Cipher alg %u",
1149                         cipher_xform->algo);
1150                 goto error_out;
1151         default:
1152                 RTE_LOG(ERR, PMD, "Crypto: Undefined Cipher specified %u\n",
1153                         cipher_xform->algo);
1154                 goto error_out;
1155         }
1156         session->dir = (cipher_xform->op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
1157                                 DIR_ENC : DIR_DEC;
1158
1159         priv->flc_desc[0].desc[0] = cipherdata.keylen;
1160         priv->flc_desc[0].desc[1] = authdata.keylen;
1161         err = rta_inline_query(IPSEC_AUTH_VAR_AES_DEC_BASE_DESC_LEN,
1162                                MIN_JOB_DESC_SIZE,
1163                                (unsigned int *)priv->flc_desc[0].desc,
1164                                &priv->flc_desc[0].desc[2], 2);
1165
1166         if (err < 0) {
1167                 PMD_DRV_LOG(ERR, "Crypto: Incorrect key lengths");
1168                 goto error_out;
1169         }
1170         if (priv->flc_desc[0].desc[2] & 1) {
1171                 cipherdata.key_type = RTA_DATA_IMM;
1172         } else {
1173                 cipherdata.key = DPAA2_VADDR_TO_IOVA(cipherdata.key);
1174                 cipherdata.key_type = RTA_DATA_PTR;
1175         }
1176         if (priv->flc_desc[0].desc[2] & (1 << 1)) {
1177                 authdata.key_type = RTA_DATA_IMM;
1178         } else {
1179                 authdata.key = DPAA2_VADDR_TO_IOVA(authdata.key);
1180                 authdata.key_type = RTA_DATA_PTR;
1181         }
1182         priv->flc_desc[0].desc[0] = 0;
1183         priv->flc_desc[0].desc[1] = 0;
1184         priv->flc_desc[0].desc[2] = 0;
1185
1186         if (session->ctxt_type == DPAA2_SEC_CIPHER_HASH) {
1187                 bufsize = cnstr_shdsc_authenc(priv->flc_desc[0].desc, 1,
1188                                               0, &cipherdata, &authdata,
1189                                               ctxt->iv.length,
1190                                               ctxt->auth_only_len,
1191                                               ctxt->trunc_len,
1192                                               session->dir);
1193         } else {
1194                 RTE_LOG(ERR, PMD, "Hash before cipher not supported");
1195                 goto error_out;
1196         }
1197
1198         flc->word1_sdl = (uint8_t)bufsize;
1199         flc->word2_rflc_31_0 = lower_32_bits(
1200                         (uint64_t)&(((struct dpaa2_sec_qp *)
1201                         dev->data->queue_pairs[0])->rx_vq));
1202         flc->word3_rflc_63_32 = upper_32_bits(
1203                         (uint64_t)&(((struct dpaa2_sec_qp *)
1204                         dev->data->queue_pairs[0])->rx_vq));
1205         session->ctxt = priv;
1206
1207         return 0;
1208
1209 error_out:
1210         rte_free(session->cipher_key.data);
1211         rte_free(session->auth_key.data);
1212         rte_free(priv);
1213         return -1;
1214 }
1215
1216 static void *
1217 dpaa2_sec_session_configure(struct rte_cryptodev *dev,
1218                             struct rte_crypto_sym_xform *xform, void *sess)
1219 {
1220         dpaa2_sec_session *session = sess;
1221
1222         PMD_INIT_FUNC_TRACE();
1223
1224         if (unlikely(sess == NULL)) {
1225                 RTE_LOG(ERR, PMD, "invalid session struct");
1226                 return NULL;
1227         }
1228
1229         /* Default IV length = 0 */
1230         session->iv.length = 0;
1231
1232         /* Cipher Only */
1233         if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER && xform->next == NULL) {
1234                 session->ctxt_type = DPAA2_SEC_CIPHER;
1235                 dpaa2_sec_cipher_init(dev, xform, session);
1236
1237         /* Authentication Only */
1238         } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
1239                    xform->next == NULL) {
1240                 session->ctxt_type = DPAA2_SEC_AUTH;
1241                 dpaa2_sec_auth_init(dev, xform, session);
1242
1243         /* Cipher then Authenticate */
1244         } else if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
1245                    xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
1246                 session->ext_params.aead_ctxt.auth_cipher_text = true;
1247                 dpaa2_sec_aead_init(dev, xform, session);
1248
1249         /* Authenticate then Cipher */
1250         } else if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
1251                    xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
1252                 session->ext_params.aead_ctxt.auth_cipher_text = false;
1253                 dpaa2_sec_aead_init(dev, xform, session);
1254         } else {
1255                 RTE_LOG(ERR, PMD, "Invalid crypto type");
1256                 return NULL;
1257         }
1258
1259         return session;
1260 }
1261
1262 /** Clear the memory of session so it doesn't leave key material behind */
1263 static void
1264 dpaa2_sec_session_clear(struct rte_cryptodev *dev __rte_unused, void *sess)
1265 {
1266         PMD_INIT_FUNC_TRACE();
1267         dpaa2_sec_session *s = (dpaa2_sec_session *)sess;
1268
1269         if (s) {
1270                 rte_free(s->ctxt);
1271                 rte_free(s->cipher_key.data);
1272                 rte_free(s->auth_key.data);
1273                 memset(sess, 0, sizeof(dpaa2_sec_session));
1274         }
1275 }
1276
1277 static int
1278 dpaa2_sec_dev_configure(struct rte_cryptodev *dev __rte_unused,
1279                         struct rte_cryptodev_config *config __rte_unused)
1280 {
1281         PMD_INIT_FUNC_TRACE();
1282
1283         return -ENOTSUP;
1284 }
1285
1286 static int
1287 dpaa2_sec_dev_start(struct rte_cryptodev *dev)
1288 {
1289         struct dpaa2_sec_dev_private *priv = dev->data->dev_private;
1290         struct fsl_mc_io *dpseci = (struct fsl_mc_io *)priv->hw;
1291         struct dpseci_attr attr;
1292         struct dpaa2_queue *dpaa2_q;
1293         struct dpaa2_sec_qp **qp = (struct dpaa2_sec_qp **)
1294                                         dev->data->queue_pairs;
1295         struct dpseci_rx_queue_attr rx_attr;
1296         struct dpseci_tx_queue_attr tx_attr;
1297         int ret, i;
1298
1299         PMD_INIT_FUNC_TRACE();
1300
1301         memset(&attr, 0, sizeof(struct dpseci_attr));
1302
1303         ret = dpseci_enable(dpseci, CMD_PRI_LOW, priv->token);
1304         if (ret) {
1305                 PMD_INIT_LOG(ERR, "DPSECI with HW_ID = %d ENABLE FAILED\n",
1306                              priv->hw_id);
1307                 goto get_attr_failure;
1308         }
1309         ret = dpseci_get_attributes(dpseci, CMD_PRI_LOW, priv->token, &attr);
1310         if (ret) {
1311                 PMD_INIT_LOG(ERR,
1312                              "DPSEC ATTRIBUTE READ FAILED, disabling DPSEC\n");
1313                 goto get_attr_failure;
1314         }
1315         for (i = 0; i < attr.num_rx_queues && qp[i]; i++) {
1316                 dpaa2_q = &qp[i]->rx_vq;
1317                 dpseci_get_rx_queue(dpseci, CMD_PRI_LOW, priv->token, i,
1318                                     &rx_attr);
1319                 dpaa2_q->fqid = rx_attr.fqid;
1320                 PMD_INIT_LOG(DEBUG, "rx_fqid: %d", dpaa2_q->fqid);
1321         }
1322         for (i = 0; i < attr.num_tx_queues && qp[i]; i++) {
1323                 dpaa2_q = &qp[i]->tx_vq;
1324                 dpseci_get_tx_queue(dpseci, CMD_PRI_LOW, priv->token, i,
1325                                     &tx_attr);
1326                 dpaa2_q->fqid = tx_attr.fqid;
1327                 PMD_INIT_LOG(DEBUG, "tx_fqid: %d", dpaa2_q->fqid);
1328         }
1329
1330         return 0;
1331 get_attr_failure:
1332         dpseci_disable(dpseci, CMD_PRI_LOW, priv->token);
1333         return -1;
1334 }
1335
1336 static void
1337 dpaa2_sec_dev_stop(struct rte_cryptodev *dev)
1338 {
1339         struct dpaa2_sec_dev_private *priv = dev->data->dev_private;
1340         struct fsl_mc_io *dpseci = (struct fsl_mc_io *)priv->hw;
1341         int ret;
1342
1343         PMD_INIT_FUNC_TRACE();
1344
1345         ret = dpseci_disable(dpseci, CMD_PRI_LOW, priv->token);
1346         if (ret) {
1347                 PMD_INIT_LOG(ERR, "Failure in disabling dpseci %d device",
1348                              priv->hw_id);
1349                 return;
1350         }
1351
1352         ret = dpseci_reset(dpseci, CMD_PRI_LOW, priv->token);
1353         if (ret < 0) {
1354                 PMD_INIT_LOG(ERR, "SEC Device cannot be reset:Error = %0x\n",
1355                              ret);
1356                 return;
1357         }
1358 }
1359
1360 static int
1361 dpaa2_sec_dev_close(struct rte_cryptodev *dev)
1362 {
1363         struct dpaa2_sec_dev_private *priv = dev->data->dev_private;
1364         struct fsl_mc_io *dpseci = (struct fsl_mc_io *)priv->hw;
1365         int ret;
1366
1367         PMD_INIT_FUNC_TRACE();
1368
1369         /* Function is reverse of dpaa2_sec_dev_init.
1370          * It does the following:
1371          * 1. Detach a DPSECI from attached resources i.e. buffer pools, dpbp_id
1372          * 2. Close the DPSECI device
1373          * 3. Free the allocated resources.
1374          */
1375
1376         /*Close the device at underlying layer*/
1377         ret = dpseci_close(dpseci, CMD_PRI_LOW, priv->token);
1378         if (ret) {
1379                 PMD_INIT_LOG(ERR, "Failure closing dpseci device with"
1380                              " error code %d\n", ret);
1381                 return -1;
1382         }
1383
1384         /*Free the allocated memory for ethernet private data and dpseci*/
1385         priv->hw = NULL;
1386         free(dpseci);
1387
1388         return 0;
1389 }
1390
1391 static void
1392 dpaa2_sec_dev_infos_get(struct rte_cryptodev *dev,
1393                         struct rte_cryptodev_info *info)
1394 {
1395         struct dpaa2_sec_dev_private *internals = dev->data->dev_private;
1396
1397         PMD_INIT_FUNC_TRACE();
1398         if (info != NULL) {
1399                 info->max_nb_queue_pairs = internals->max_nb_queue_pairs;
1400                 info->feature_flags = dev->feature_flags;
1401                 info->capabilities = dpaa2_sec_capabilities;
1402                 info->sym.max_nb_sessions = internals->max_nb_sessions;
1403                 info->dev_type = RTE_CRYPTODEV_DPAA2_SEC_PMD;
1404         }
1405 }
1406
1407 static
1408 void dpaa2_sec_stats_get(struct rte_cryptodev *dev,
1409                          struct rte_cryptodev_stats *stats)
1410 {
1411         struct dpaa2_sec_dev_private *priv = dev->data->dev_private;
1412         struct fsl_mc_io *dpseci = (struct fsl_mc_io *)priv->hw;
1413         struct dpseci_sec_counters counters = {0};
1414         struct dpaa2_sec_qp **qp = (struct dpaa2_sec_qp **)
1415                                         dev->data->queue_pairs;
1416         int ret, i;
1417
1418         PMD_INIT_FUNC_TRACE();
1419         if (stats == NULL) {
1420                 PMD_DRV_LOG(ERR, "invalid stats ptr NULL");
1421                 return;
1422         }
1423         for (i = 0; i < dev->data->nb_queue_pairs; i++) {
1424                 if (qp[i] == NULL) {
1425                         PMD_DRV_LOG(DEBUG, "Uninitialised queue pair");
1426                         continue;
1427                 }
1428
1429                 stats->enqueued_count += qp[i]->tx_vq.tx_pkts;
1430                 stats->dequeued_count += qp[i]->rx_vq.rx_pkts;
1431                 stats->enqueue_err_count += qp[i]->tx_vq.err_pkts;
1432                 stats->dequeue_err_count += qp[i]->rx_vq.err_pkts;
1433         }
1434
1435         ret = dpseci_get_sec_counters(dpseci, CMD_PRI_LOW, priv->token,
1436                                       &counters);
1437         if (ret) {
1438                 PMD_DRV_LOG(ERR, "dpseci_get_sec_counters failed\n");
1439         } else {
1440                 PMD_DRV_LOG(INFO, "dpseci hw stats:"
1441                             "\n\tNumber of Requests Dequeued = %lu"
1442                             "\n\tNumber of Outbound Encrypt Requests = %lu"
1443                             "\n\tNumber of Inbound Decrypt Requests = %lu"
1444                             "\n\tNumber of Outbound Bytes Encrypted = %lu"
1445                             "\n\tNumber of Outbound Bytes Protected = %lu"
1446                             "\n\tNumber of Inbound Bytes Decrypted = %lu"
1447                             "\n\tNumber of Inbound Bytes Validated = %lu",
1448                             counters.dequeued_requests,
1449                             counters.ob_enc_requests,
1450                             counters.ib_dec_requests,
1451                             counters.ob_enc_bytes,
1452                             counters.ob_prot_bytes,
1453                             counters.ib_dec_bytes,
1454                             counters.ib_valid_bytes);
1455         }
1456 }
1457
1458 static
1459 void dpaa2_sec_stats_reset(struct rte_cryptodev *dev)
1460 {
1461         int i;
1462         struct dpaa2_sec_qp **qp = (struct dpaa2_sec_qp **)
1463                                    (dev->data->queue_pairs);
1464
1465         PMD_INIT_FUNC_TRACE();
1466
1467         for (i = 0; i < dev->data->nb_queue_pairs; i++) {
1468                 if (qp[i] == NULL) {
1469                         PMD_DRV_LOG(DEBUG, "Uninitialised queue pair");
1470                         continue;
1471                 }
1472                 qp[i]->tx_vq.rx_pkts = 0;
1473                 qp[i]->tx_vq.tx_pkts = 0;
1474                 qp[i]->tx_vq.err_pkts = 0;
1475                 qp[i]->rx_vq.rx_pkts = 0;
1476                 qp[i]->rx_vq.tx_pkts = 0;
1477                 qp[i]->rx_vq.err_pkts = 0;
1478         }
1479 }
1480
1481 static struct rte_cryptodev_ops crypto_ops = {
1482         .dev_configure        = dpaa2_sec_dev_configure,
1483         .dev_start            = dpaa2_sec_dev_start,
1484         .dev_stop             = dpaa2_sec_dev_stop,
1485         .dev_close            = dpaa2_sec_dev_close,
1486         .dev_infos_get        = dpaa2_sec_dev_infos_get,
1487         .stats_get            = dpaa2_sec_stats_get,
1488         .stats_reset          = dpaa2_sec_stats_reset,
1489         .queue_pair_setup     = dpaa2_sec_queue_pair_setup,
1490         .queue_pair_release   = dpaa2_sec_queue_pair_release,
1491         .queue_pair_start     = dpaa2_sec_queue_pair_start,
1492         .queue_pair_stop      = dpaa2_sec_queue_pair_stop,
1493         .queue_pair_count     = dpaa2_sec_queue_pair_count,
1494         .session_get_size     = dpaa2_sec_session_get_size,
1495         .session_initialize   = dpaa2_sec_session_initialize,
1496         .session_configure    = dpaa2_sec_session_configure,
1497         .session_clear        = dpaa2_sec_session_clear,
1498 };
1499
1500 static int
1501 dpaa2_sec_uninit(const struct rte_cryptodev *dev)
1502 {
1503         PMD_INIT_LOG(INFO, "Closing DPAA2_SEC device %s on numa socket %u\n",
1504                      dev->data->name, rte_socket_id());
1505
1506         return 0;
1507 }
1508
1509 static int
1510 dpaa2_sec_dev_init(struct rte_cryptodev *cryptodev)
1511 {
1512         struct dpaa2_sec_dev_private *internals;
1513         struct rte_device *dev = cryptodev->device;
1514         struct rte_dpaa2_device *dpaa2_dev;
1515         struct fsl_mc_io *dpseci;
1516         uint16_t token;
1517         struct dpseci_attr attr;
1518         int retcode, hw_id;
1519
1520         PMD_INIT_FUNC_TRACE();
1521         dpaa2_dev = container_of(dev, struct rte_dpaa2_device, device);
1522         if (dpaa2_dev == NULL) {
1523                 PMD_INIT_LOG(ERR, "dpaa2_device not found\n");
1524                 return -1;
1525         }
1526         hw_id = dpaa2_dev->object_id;
1527
1528         cryptodev->dev_type = RTE_CRYPTODEV_DPAA2_SEC_PMD;
1529         cryptodev->dev_ops = &crypto_ops;
1530
1531         cryptodev->enqueue_burst = dpaa2_sec_enqueue_burst;
1532         cryptodev->dequeue_burst = dpaa2_sec_dequeue_burst;
1533         cryptodev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
1534                         RTE_CRYPTODEV_FF_HW_ACCELERATED |
1535                         RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING;
1536
1537         internals = cryptodev->data->dev_private;
1538         internals->max_nb_sessions = RTE_DPAA2_SEC_PMD_MAX_NB_SESSIONS;
1539
1540         /*
1541          * For secondary processes, we don't initialise any further as primary
1542          * has already done this work. Only check we don't need a different
1543          * RX function
1544          */
1545         if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
1546                 PMD_INIT_LOG(DEBUG, "Device already init by primary process");
1547                 return 0;
1548         }
1549         /*Open the rte device via MC and save the handle for further use*/
1550         dpseci = (struct fsl_mc_io *)rte_calloc(NULL, 1,
1551                                 sizeof(struct fsl_mc_io), 0);
1552         if (!dpseci) {
1553                 PMD_INIT_LOG(ERR,
1554                              "Error in allocating the memory for dpsec object");
1555                 return -1;
1556         }
1557         dpseci->regs = rte_mcp_ptr_list[0];
1558
1559         retcode = dpseci_open(dpseci, CMD_PRI_LOW, hw_id, &token);
1560         if (retcode != 0) {
1561                 PMD_INIT_LOG(ERR, "Cannot open the dpsec device: Error = %x",
1562                              retcode);
1563                 goto init_error;
1564         }
1565         retcode = dpseci_get_attributes(dpseci, CMD_PRI_LOW, token, &attr);
1566         if (retcode != 0) {
1567                 PMD_INIT_LOG(ERR,
1568                              "Cannot get dpsec device attributed: Error = %x",
1569                              retcode);
1570                 goto init_error;
1571         }
1572         sprintf(cryptodev->data->name, "dpsec-%u", hw_id);
1573
1574         internals->max_nb_queue_pairs = attr.num_tx_queues;
1575         cryptodev->data->nb_queue_pairs = internals->max_nb_queue_pairs;
1576         internals->hw = dpseci;
1577         internals->token = token;
1578
1579         PMD_INIT_LOG(DEBUG, "driver %s: created\n", cryptodev->data->name);
1580         return 0;
1581
1582 init_error:
1583         PMD_INIT_LOG(ERR, "driver %s: create failed\n", cryptodev->data->name);
1584
1585         /* dpaa2_sec_uninit(crypto_dev_name); */
1586         return -EFAULT;
1587 }
1588
1589 static int
1590 cryptodev_dpaa2_sec_probe(struct rte_dpaa2_driver *dpaa2_drv __rte_unused,
1591                           struct rte_dpaa2_device *dpaa2_dev)
1592 {
1593         struct rte_cryptodev *cryptodev;
1594         char cryptodev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
1595
1596         int retval;
1597
1598         sprintf(cryptodev_name, "dpsec-%d", dpaa2_dev->object_id);
1599
1600         cryptodev = rte_cryptodev_pmd_allocate(cryptodev_name, rte_socket_id());
1601         if (cryptodev == NULL)
1602                 return -ENOMEM;
1603
1604         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
1605                 cryptodev->data->dev_private = rte_zmalloc_socket(
1606                                         "cryptodev private structure",
1607                                         sizeof(struct dpaa2_sec_dev_private),
1608                                         RTE_CACHE_LINE_SIZE,
1609                                         rte_socket_id());
1610
1611                 if (cryptodev->data->dev_private == NULL)
1612                         rte_panic("Cannot allocate memzone for private "
1613                                         "device data");
1614         }
1615
1616         dpaa2_dev->cryptodev = cryptodev;
1617         cryptodev->device = &dpaa2_dev->device;
1618
1619         /* init user callbacks */
1620         TAILQ_INIT(&(cryptodev->link_intr_cbs));
1621
1622         /* Invoke PMD device initialization function */
1623         retval = dpaa2_sec_dev_init(cryptodev);
1624         if (retval == 0)
1625                 return 0;
1626
1627         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
1628                 rte_free(cryptodev->data->dev_private);
1629
1630         cryptodev->attached = RTE_CRYPTODEV_DETACHED;
1631
1632         return -ENXIO;
1633 }
1634
1635 static int
1636 cryptodev_dpaa2_sec_remove(struct rte_dpaa2_device *dpaa2_dev)
1637 {
1638         struct rte_cryptodev *cryptodev;
1639         int ret;
1640
1641         cryptodev = dpaa2_dev->cryptodev;
1642         if (cryptodev == NULL)
1643                 return -ENODEV;
1644
1645         ret = dpaa2_sec_uninit(cryptodev);
1646         if (ret)
1647                 return ret;
1648
1649         /* free crypto device */
1650         rte_cryptodev_pmd_release_device(cryptodev);
1651
1652         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
1653                 rte_free(cryptodev->data->dev_private);
1654
1655         cryptodev->device = NULL;
1656         cryptodev->data = NULL;
1657
1658         return 0;
1659 }
1660
1661 static struct rte_dpaa2_driver rte_dpaa2_sec_driver = {
1662         .drv_type = DPAA2_MC_DPSECI_DEVID,
1663         .driver = {
1664                 .name = "DPAA2 SEC PMD"
1665         },
1666         .probe = cryptodev_dpaa2_sec_probe,
1667         .remove = cryptodev_dpaa2_sec_remove,
1668 };
1669
1670 RTE_PMD_REGISTER_DPAA2(dpaa2_sec_pmd, rte_dpaa2_sec_driver);