add prefix to cache line macros
[dpdk.git] / examples / dpdk_qat / crypto.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   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 Intel Corporation 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 <stdio.h>
35 #include <stdlib.h>
36 #include <strings.h>
37 #include <string.h>
38 #include <inttypes.h>
39 #include <errno.h>
40 #include <sys/queue.h>
41 #include <stdarg.h>
42
43 #include <rte_common.h>
44 #include <rte_log.h>
45 #include <rte_debug.h>
46 #include <rte_memory.h>
47 #include <rte_memzone.h>
48 #include <rte_tailq.h>
49 #include <rte_ether.h>
50 #include <rte_malloc.h>
51 #include <rte_launch.h>
52 #include <rte_eal.h>
53 #include <rte_per_lcore.h>
54 #include <rte_lcore.h>
55 #include <rte_atomic.h>
56 #include <rte_branch_prediction.h>
57 #include <rte_ring.h>
58 #include <rte_mempool.h>
59 #include <rte_mbuf.h>
60 #include <rte_string_fns.h>
61
62 #define CPA_CY_SYM_DP_TMP_WORKAROUND 1
63
64 #include "cpa.h"
65 #include "cpa_types.h"
66 #include "cpa_cy_sym_dp.h"
67 #include "cpa_cy_common.h"
68 #include "cpa_cy_im.h"
69 #include "icp_sal_user.h"
70 #include "icp_sal_poll.h"
71
72 #include "crypto.h"
73
74 /* CIPHER KEY LENGTHS */
75 #define KEY_SIZE_64_IN_BYTES    (64 / 8)
76 #define KEY_SIZE_56_IN_BYTES    (56 / 8)
77 #define KEY_SIZE_128_IN_BYTES   (128 / 8)
78 #define KEY_SIZE_168_IN_BYTES   (168 / 8)
79 #define KEY_SIZE_192_IN_BYTES   (192 / 8)
80 #define KEY_SIZE_256_IN_BYTES   (256 / 8)
81
82 /* HMAC AUTH KEY LENGTHS */
83 #define AES_XCBC_AUTH_KEY_LENGTH_IN_BYTES       (128 / 8)
84 #define SHA1_AUTH_KEY_LENGTH_IN_BYTES           (160 / 8)
85 #define SHA224_AUTH_KEY_LENGTH_IN_BYTES         (224 / 8)
86 #define SHA256_AUTH_KEY_LENGTH_IN_BYTES         (256 / 8)
87 #define SHA384_AUTH_KEY_LENGTH_IN_BYTES         (384 / 8)
88 #define SHA512_AUTH_KEY_LENGTH_IN_BYTES         (512 / 8)
89 #define MD5_AUTH_KEY_LENGTH_IN_BYTES            (128 / 8)
90 #define KASUMI_AUTH_KEY_LENGTH_IN_BYTES         (128 / 8)
91
92 /* HASH DIGEST LENGHTS */
93 #define AES_XCBC_DIGEST_LENGTH_IN_BYTES         (128 / 8)
94 #define AES_XCBC_96_DIGEST_LENGTH_IN_BYTES      (96 / 8)
95 #define MD5_DIGEST_LENGTH_IN_BYTES              (128 / 8)
96 #define SHA1_DIGEST_LENGTH_IN_BYTES             (160 / 8)
97 #define SHA1_96_DIGEST_LENGTH_IN_BYTES          (96 / 8)
98 #define SHA224_DIGEST_LENGTH_IN_BYTES           (224 / 8)
99 #define SHA256_DIGEST_LENGTH_IN_BYTES           (256 / 8)
100 #define SHA384_DIGEST_LENGTH_IN_BYTES           (384 / 8)
101 #define SHA512_DIGEST_LENGTH_IN_BYTES           (512 / 8)
102 #define KASUMI_DIGEST_LENGTH_IN_BYTES           (32 / 8)
103
104 #define IV_LENGTH_16_BYTES      (16)
105 #define IV_LENGTH_8_BYTES       (8)
106
107
108 /*
109  * rte_memzone is used to allocate physically contiguous virtual memory.
110  * In this application we allocate a single block and divide between variables
111  * which require a virtual to physical mapping for use by the QAT driver.
112  * Virt2phys is only performed during initialisation and not on the data-path.
113  */
114
115 #define LCORE_MEMZONE_SIZE      (1 << 22)
116
117 struct lcore_memzone
118 {
119         const struct rte_memzone *memzone;
120         void *next_free_address;
121 };
122
123 /*
124  * Size the qa software response queue.
125  * Note: Head and Tail are 8 bit, therefore, the queue is
126  * fixed to 256 entries.
127  */
128 #define CRYPTO_SOFTWARE_QUEUE_SIZE 256
129
130 struct qa_callbackQueue {
131         uint8_t head;
132         uint8_t tail;
133         uint16_t numEntries;
134         struct rte_mbuf *qaCallbackRing[CRYPTO_SOFTWARE_QUEUE_SIZE];
135 };
136
137 struct qa_core_conf {
138         CpaCySymDpSessionCtx *encryptSessionHandleTbl[NUM_CRYPTO][NUM_HMAC];
139         CpaCySymDpSessionCtx *decryptSessionHandleTbl[NUM_CRYPTO][NUM_HMAC];
140         CpaInstanceHandle instanceHandle;
141         struct qa_callbackQueue callbackQueue;
142         uint64_t qaOutstandingRequests;
143         uint64_t numResponseAttempts;
144         uint8_t kickFreq;
145         void *pPacketIV;
146         CpaPhysicalAddr packetIVPhy;
147         struct lcore_memzone lcoreMemzone;
148 } __rte_cache_aligned;
149
150 #define MAX_CORES   (RTE_MAX_LCORE)
151
152 static struct qa_core_conf qaCoreConf[MAX_CORES];
153
154 /*
155  *Create maximum possible key size,
156  *One for cipher and one for hash
157  */
158 struct glob_keys {
159         uint8_t cipher_key[32];
160         uint8_t hash_key[64];
161         uint8_t iv[16];
162 };
163
164 struct glob_keys g_crypto_hash_keys = {
165         .cipher_key = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
166                 0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10,
167                 0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,
168                 0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,0x20},
169         .hash_key = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
170                 0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10,
171                 0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,
172                 0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,0x20,
173                 0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,
174                 0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,0x30,
175                 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,
176                 0x39,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,0x50},
177         .iv = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
178                 0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10}
179 };
180
181 /*
182  * Offsets from the start of the packet.
183  *
184  */
185 #define PACKET_DATA_START_PHYS(p) \
186                 ((p)->buf_physaddr + (p)->data_off)
187
188 /*
189  * A fixed offset to where the crypto is to be performed, which is the first
190  * byte after the Ethernet(14 bytes) and IPv4 headers(20 bytes)
191  */
192 #define CRYPTO_START_OFFSET             (14+20)
193 #define HASH_START_OFFSET               (14+20)
194 #define CIPHER_BLOCK_DEFAULT_SIZE       (16)
195 #define HASH_BLOCK_DEFAULT_SIZE         (16)
196
197 /*
198  * Offset to the opdata from the start of the data portion of packet.
199  * Assumption: The buffer is physically contiguous.
200  * +18 takes this to the next cache line.
201  */
202
203 #define CRYPTO_OFFSET_TO_OPDATA         (ETHER_MAX_LEN+18)
204
205 /*
206  * Default number of requests to place on the hardware ring before kicking the
207  * ring pointers.
208  */
209 #define CRYPTO_BURST_TX (16)
210
211 /*
212  * Only call the qa poll function when the number responses in the software
213  * queue drops below this number.
214  */
215 #define CRYPTO_QUEUED_RESP_POLL_THRESHOLD       (32)
216
217 /*
218  * Limit the number of polls per call to get_next_response.
219  */
220 #define GET_NEXT_RESPONSE_FREQ  (32)
221
222 /*
223  * Max number of responses to pull from the qa in one poll.
224  */
225 #define CRYPTO_MAX_RESPONSE_QUOTA \
226                 (CRYPTO_SOFTWARE_QUEUE_SIZE-CRYPTO_QUEUED_RESP_POLL_THRESHOLD-1)
227
228 #if (CRYPTO_QUEUED_RESP_POLL_THRESHOLD + CRYPTO_MAX_RESPONSE_QUOTA >= \
229                 CRYPTO_SOFTWARE_QUEUE_SIZE)
230 #error Its possible to overflow the qa response Q with current poll and \
231                 response quota.
232 #endif
233
234 static void
235 crypto_callback(CpaCySymDpOpData *pOpData,
236                 __rte_unused CpaStatus status,
237                 __rte_unused CpaBoolean verifyResult)
238 {
239         uint32_t lcore_id;
240         lcore_id = rte_lcore_id();
241         struct qa_callbackQueue *callbackQ = &(qaCoreConf[lcore_id].callbackQueue);
242
243         /*
244          * Received a completion from the QA hardware.
245          * Place the response on the return queue.
246          */
247         callbackQ->qaCallbackRing[callbackQ->head] = pOpData->pCallbackTag;
248         callbackQ->head++;
249         callbackQ->numEntries++;
250         qaCoreConf[lcore_id].qaOutstandingRequests--;
251 }
252
253 static void
254 qa_crypto_callback(CpaCySymDpOpData *pOpData, CpaStatus status,
255                 CpaBoolean verifyResult)
256 {
257         crypto_callback(pOpData, status, verifyResult);
258 }
259
260 /*
261  * Each allocation from a particular memzone lasts for the life-time of
262  * the application. No freeing of previous allocations will occur.
263  */
264 static void *
265 alloc_memzone_region(uint32_t length, uint32_t lcore_id)
266 {
267         char *current_free_addr_ptr = NULL;
268         struct lcore_memzone *lcore_memzone = &(qaCoreConf[lcore_id].lcoreMemzone);
269
270         current_free_addr_ptr  = lcore_memzone->next_free_address;
271
272         if (current_free_addr_ptr + length >=
273                 (char *)lcore_memzone->memzone->addr + lcore_memzone->memzone->len) {
274                 printf("Crypto: No memory available in memzone\n");
275                 return NULL;
276         }
277         lcore_memzone->next_free_address = current_free_addr_ptr + length;
278
279         return (void *)current_free_addr_ptr;
280 }
281
282 /*
283  * Virtual to Physical Address translation is only executed during initialization
284  * and not on the data-path.
285  */
286 static CpaPhysicalAddr
287 qa_v2p(void *ptr)
288 {
289         const struct rte_memzone *memzone = NULL;
290         uint32_t lcore_id = 0;
291         RTE_LCORE_FOREACH(lcore_id) {
292                 memzone = qaCoreConf[lcore_id].lcoreMemzone.memzone;
293
294                 if ((char*) ptr >= (char *) memzone->addr &&
295                                 (char*) ptr < ((char*) memzone->addr + memzone->len)) {
296                         return (CpaPhysicalAddr)
297                                         (memzone->phys_addr + ((char *) ptr - (char*) memzone->addr));
298                 }
299         }
300         printf("Crypto: Corresponding physical address not found in memzone\n");
301         return (CpaPhysicalAddr) 0;
302 }
303
304 static CpaStatus
305 getCoreAffinity(Cpa32U *coreAffinity, const CpaInstanceHandle instanceHandle)
306 {
307         CpaInstanceInfo2 info;
308         Cpa16U i = 0;
309         CpaStatus status = CPA_STATUS_SUCCESS;
310
311         bzero(&info, sizeof(CpaInstanceInfo2));
312
313         status = cpaCyInstanceGetInfo2(instanceHandle, &info);
314         if (CPA_STATUS_SUCCESS != status) {
315                 printf("Crypto: Error getting instance info\n");
316                 return CPA_STATUS_FAIL;
317         }
318         for (i = 0; i < MAX_CORES; i++) {
319                 if (CPA_BITMAP_BIT_TEST(info.coreAffinity, i)) {
320                         *coreAffinity = i;
321                         return CPA_STATUS_SUCCESS;
322                 }
323         }
324         return CPA_STATUS_FAIL;
325 }
326
327 static CpaStatus
328 get_crypto_instance_on_core(CpaInstanceHandle *pInstanceHandle,
329                 uint32_t lcore_id)
330 {
331         Cpa16U numInstances = 0, i = 0;
332         CpaStatus status = CPA_STATUS_FAIL;
333         CpaInstanceHandle *pLocalInstanceHandles = NULL;
334         Cpa32U coreAffinity = 0;
335
336         status = cpaCyGetNumInstances(&numInstances);
337         if (CPA_STATUS_SUCCESS != status || numInstances == 0) {
338                 return CPA_STATUS_FAIL;
339         }
340
341         pLocalInstanceHandles = rte_malloc("pLocalInstanceHandles",
342                         sizeof(CpaInstanceHandle) * numInstances, RTE_CACHE_LINE_SIZE);
343
344         if (NULL == pLocalInstanceHandles) {
345                 return CPA_STATUS_FAIL;
346         }
347         status = cpaCyGetInstances(numInstances, pLocalInstanceHandles);
348         if (CPA_STATUS_SUCCESS != status) {
349                 printf("Crypto: cpaCyGetInstances failed with status: %"PRId32"\n", status);
350                 rte_free((void *) pLocalInstanceHandles);
351                 return CPA_STATUS_FAIL;
352         }
353
354         for (i = 0; i < numInstances; i++) {
355                 status = getCoreAffinity(&coreAffinity, pLocalInstanceHandles[i]);
356                 if (CPA_STATUS_SUCCESS != status) {
357                         rte_free((void *) pLocalInstanceHandles);
358                         return CPA_STATUS_FAIL;
359                 }
360                 if (coreAffinity == lcore_id) {
361                         printf("Crypto: instance found on core %d\n", i);
362                         *pInstanceHandle = pLocalInstanceHandles[i];
363                         return CPA_STATUS_SUCCESS;
364                 }
365         }
366         /* core affinity not found */
367         rte_free((void *) pLocalInstanceHandles);
368         return CPA_STATUS_FAIL;
369 }
370
371 static CpaStatus
372 initCySymSession(const int pkt_cipher_alg,
373                 const int pkt_hash_alg, const CpaCySymHashMode hashMode,
374                 const CpaCySymCipherDirection crypto_direction,
375                 CpaCySymSessionCtx **ppSessionCtx,
376                 const CpaInstanceHandle cyInstanceHandle,
377                 const uint32_t lcore_id)
378 {
379         Cpa32U sessionCtxSizeInBytes = 0;
380         CpaStatus status = CPA_STATUS_FAIL;
381         CpaBoolean isCrypto = CPA_TRUE, isHmac = CPA_TRUE;
382         CpaCySymSessionSetupData sessionSetupData;
383
384         bzero(&sessionSetupData, sizeof(CpaCySymSessionSetupData));
385
386         /* Assumption: key length is set to each algorithm's max length */
387         switch (pkt_cipher_alg) {
388         case NO_CIPHER:
389                 isCrypto = CPA_FALSE;
390                 break;
391         case CIPHER_DES:
392                 sessionSetupData.cipherSetupData.cipherAlgorithm =
393                                 CPA_CY_SYM_CIPHER_DES_ECB;
394                 sessionSetupData.cipherSetupData.cipherKeyLenInBytes =
395                                 KEY_SIZE_64_IN_BYTES;
396                 break;
397         case CIPHER_DES_CBC:
398                 sessionSetupData.cipherSetupData.cipherAlgorithm =
399                                 CPA_CY_SYM_CIPHER_DES_CBC;
400                 sessionSetupData.cipherSetupData.cipherKeyLenInBytes =
401                                 KEY_SIZE_64_IN_BYTES;
402                 break;
403         case CIPHER_DES3:
404                 sessionSetupData.cipherSetupData.cipherAlgorithm =
405                                 CPA_CY_SYM_CIPHER_3DES_ECB;
406                 sessionSetupData.cipherSetupData.cipherKeyLenInBytes =
407                                 KEY_SIZE_192_IN_BYTES;
408                 break;
409         case CIPHER_DES3_CBC:
410                 sessionSetupData.cipherSetupData.cipherAlgorithm =
411                                 CPA_CY_SYM_CIPHER_3DES_CBC;
412                 sessionSetupData.cipherSetupData.cipherKeyLenInBytes =
413                                 KEY_SIZE_192_IN_BYTES;
414                 break;
415         case CIPHER_AES:
416                 sessionSetupData.cipherSetupData.cipherAlgorithm =
417                                 CPA_CY_SYM_CIPHER_AES_ECB;
418                 sessionSetupData.cipherSetupData.cipherKeyLenInBytes =
419                                 KEY_SIZE_128_IN_BYTES;
420                 break;
421         case CIPHER_AES_CBC_128:
422                 sessionSetupData.cipherSetupData.cipherAlgorithm =
423                                 CPA_CY_SYM_CIPHER_AES_CBC;
424                 sessionSetupData.cipherSetupData.cipherKeyLenInBytes =
425                                 KEY_SIZE_128_IN_BYTES;
426                 break;
427         case CIPHER_KASUMI_F8:
428                 sessionSetupData.cipherSetupData.cipherAlgorithm =
429                                 CPA_CY_SYM_CIPHER_KASUMI_F8;
430                 sessionSetupData.cipherSetupData.cipherKeyLenInBytes =
431                                 KEY_SIZE_128_IN_BYTES;
432                 break;
433         default:
434                 printf("Crypto: Undefined Cipher specified\n");
435                 break;
436         }
437         /* Set the cipher direction */
438         if (isCrypto) {
439                 sessionSetupData.cipherSetupData.cipherDirection = crypto_direction;
440                 sessionSetupData.cipherSetupData.pCipherKey =
441                                 g_crypto_hash_keys.cipher_key;
442                 sessionSetupData.symOperation = CPA_CY_SYM_OP_CIPHER;
443         }
444
445         /* Setup Hash common fields */
446         switch (pkt_hash_alg) {
447         case NO_HASH:
448                 isHmac = CPA_FALSE;
449                 break;
450         case HASH_AES_XCBC:
451                 sessionSetupData.hashSetupData.hashAlgorithm = CPA_CY_SYM_HASH_AES_XCBC;
452                 sessionSetupData.hashSetupData.digestResultLenInBytes =
453                                 AES_XCBC_DIGEST_LENGTH_IN_BYTES;
454                 break;
455         case HASH_AES_XCBC_96:
456                 sessionSetupData.hashSetupData.hashAlgorithm = CPA_CY_SYM_HASH_AES_XCBC;
457                                 sessionSetupData.hashSetupData.digestResultLenInBytes =
458                                 AES_XCBC_96_DIGEST_LENGTH_IN_BYTES;
459                 break;
460         case HASH_MD5:
461                 sessionSetupData.hashSetupData.hashAlgorithm = CPA_CY_SYM_HASH_MD5;
462                 sessionSetupData.hashSetupData.digestResultLenInBytes =
463                                 MD5_DIGEST_LENGTH_IN_BYTES;
464                 break;
465         case HASH_SHA1:
466                 sessionSetupData.hashSetupData.hashAlgorithm = CPA_CY_SYM_HASH_SHA1;
467                 sessionSetupData.hashSetupData.digestResultLenInBytes =
468                                 SHA1_DIGEST_LENGTH_IN_BYTES;
469                 break;
470         case HASH_SHA1_96:
471                 sessionSetupData.hashSetupData.hashAlgorithm = CPA_CY_SYM_HASH_SHA1;
472                 sessionSetupData.hashSetupData.digestResultLenInBytes =
473                                 SHA1_96_DIGEST_LENGTH_IN_BYTES;
474             break;
475         case HASH_SHA224:
476                 sessionSetupData.hashSetupData.hashAlgorithm = CPA_CY_SYM_HASH_SHA224;
477                 sessionSetupData.hashSetupData.digestResultLenInBytes =
478                                 SHA224_DIGEST_LENGTH_IN_BYTES;
479                 break;
480         case HASH_SHA256:
481                 sessionSetupData.hashSetupData.hashAlgorithm = CPA_CY_SYM_HASH_SHA256;
482                 sessionSetupData.hashSetupData.digestResultLenInBytes =
483                                 SHA256_DIGEST_LENGTH_IN_BYTES;
484                 break;
485         case HASH_SHA384:
486                 sessionSetupData.hashSetupData.hashAlgorithm = CPA_CY_SYM_HASH_SHA384;
487                 sessionSetupData.hashSetupData.digestResultLenInBytes =
488                                 SHA384_DIGEST_LENGTH_IN_BYTES;
489                 break;
490         case HASH_SHA512:
491                 sessionSetupData.hashSetupData.hashAlgorithm = CPA_CY_SYM_HASH_SHA512;
492                 sessionSetupData.hashSetupData.digestResultLenInBytes =
493                                 SHA512_DIGEST_LENGTH_IN_BYTES;
494                 break;
495         case HASH_KASUMI_F9:
496                 sessionSetupData.hashSetupData.hashAlgorithm = CPA_CY_SYM_HASH_KASUMI_F9;
497                 sessionSetupData.hashSetupData.digestResultLenInBytes =
498                                 KASUMI_DIGEST_LENGTH_IN_BYTES;
499                 break;
500         default:
501                 printf("Crypto: Undefined Hash specified\n");
502                 break;
503         }
504         if (isHmac) {
505                 sessionSetupData.hashSetupData.hashMode = hashMode;
506                 sessionSetupData.symOperation = CPA_CY_SYM_OP_HASH;
507                 /* If using authenticated hash setup key lengths */
508                 if (CPA_CY_SYM_HASH_MODE_AUTH == hashMode) {
509                         /* Use a common max length key */
510                         sessionSetupData.hashSetupData.authModeSetupData.authKey =
511                                         g_crypto_hash_keys.hash_key;
512                         switch (pkt_hash_alg) {
513                         case HASH_AES_XCBC:
514                         case HASH_AES_XCBC_96:
515                                 sessionSetupData.hashSetupData.authModeSetupData.authKeyLenInBytes =
516                                                 AES_XCBC_AUTH_KEY_LENGTH_IN_BYTES;
517                                 break;
518                         case HASH_MD5:
519                                 sessionSetupData.hashSetupData.authModeSetupData.authKeyLenInBytes =
520                                                 SHA1_AUTH_KEY_LENGTH_IN_BYTES;
521                                 break;
522                         case HASH_SHA1:
523                         case HASH_SHA1_96:
524                                 sessionSetupData.hashSetupData.authModeSetupData.authKeyLenInBytes =
525                                                 SHA1_AUTH_KEY_LENGTH_IN_BYTES;
526                                 break;
527                         case HASH_SHA224:
528                                 sessionSetupData.hashSetupData.authModeSetupData.authKeyLenInBytes =
529                                                 SHA224_AUTH_KEY_LENGTH_IN_BYTES;
530                                 break;
531                         case HASH_SHA256:
532                                 sessionSetupData.hashSetupData.authModeSetupData.authKeyLenInBytes =
533                                                 SHA256_AUTH_KEY_LENGTH_IN_BYTES;
534                                 break;
535                         case HASH_SHA384:
536                                 sessionSetupData.hashSetupData.authModeSetupData.authKeyLenInBytes =
537                                                 SHA384_AUTH_KEY_LENGTH_IN_BYTES;
538                                 break;
539                         case HASH_SHA512:
540                                 sessionSetupData.hashSetupData.authModeSetupData.authKeyLenInBytes =
541                                                 SHA512_AUTH_KEY_LENGTH_IN_BYTES;
542                                 break;
543                         case HASH_KASUMI_F9:
544                                 sessionSetupData.hashSetupData.authModeSetupData.authKeyLenInBytes =
545                                                 KASUMI_AUTH_KEY_LENGTH_IN_BYTES;
546                                 break;
547                         default:
548                                 printf("Crypto: Undefined Hash specified\n");
549                                 return CPA_STATUS_FAIL;
550                         }
551                 }
552         }
553
554         /* Only high priority supported */
555         sessionSetupData.sessionPriority = CPA_CY_PRIORITY_HIGH;
556
557         /* If chaining algorithms */
558         if (isCrypto && isHmac) {
559                 sessionSetupData.symOperation = CPA_CY_SYM_OP_ALGORITHM_CHAINING;
560                 /* @assumption Alg Chain order is cipher then hash for encrypt
561                  * and hash then cipher then has for decrypt*/
562                 if (CPA_CY_SYM_CIPHER_DIRECTION_ENCRYPT == crypto_direction) {
563                         sessionSetupData.algChainOrder =
564                                         CPA_CY_SYM_ALG_CHAIN_ORDER_CIPHER_THEN_HASH;
565                 } else {
566                         sessionSetupData.algChainOrder =
567                                         CPA_CY_SYM_ALG_CHAIN_ORDER_HASH_THEN_CIPHER;
568                 }
569         }
570         if (!isCrypto && !isHmac) {
571                 *ppSessionCtx = NULL;
572                 return CPA_STATUS_SUCCESS;
573         }
574
575         /* Set flags for digest operations */
576         sessionSetupData.digestIsAppended = CPA_FALSE;
577         sessionSetupData.verifyDigest = CPA_TRUE;
578
579         /* Get the session context size based on the crypto and/or hash operations*/
580         status = cpaCySymDpSessionCtxGetSize(cyInstanceHandle, &sessionSetupData,
581                         &sessionCtxSizeInBytes);
582         if (CPA_STATUS_SUCCESS != status) {
583                 printf("Crypto: cpaCySymDpSessionCtxGetSize error, status: %"PRId32"\n",
584                                 status);
585                 return CPA_STATUS_FAIL;
586         }
587
588         *ppSessionCtx = alloc_memzone_region(sessionCtxSizeInBytes, lcore_id);
589         if (NULL == *ppSessionCtx) {
590                 printf("Crypto: Failed to allocate memory for Session Context\n");
591                 return CPA_STATUS_FAIL;
592         }
593
594         status = cpaCySymDpInitSession(cyInstanceHandle, &sessionSetupData,
595                         *ppSessionCtx);
596         if (CPA_STATUS_SUCCESS != status) {
597                 printf("Crypto: cpaCySymDpInitSession failed with status %"PRId32"\n", status);
598                 return CPA_STATUS_FAIL;
599         }
600         return CPA_STATUS_SUCCESS;
601 }
602
603 static CpaStatus
604 initSessionDataTables(struct qa_core_conf *qaCoreConf,uint32_t lcore_id)
605 {
606         Cpa32U i = 0, j = 0;
607         CpaStatus status = CPA_STATUS_FAIL;
608         for (i = 0; i < NUM_CRYPTO; i++) {
609                 for (j = 0; j < NUM_HMAC; j++) {
610                         if (((i == CIPHER_KASUMI_F8) && (j != NO_HASH) && (j != HASH_KASUMI_F9)) ||
611                                 ((i != NO_CIPHER) && (i != CIPHER_KASUMI_F8) && (j == HASH_KASUMI_F9)))
612                                 continue;
613                         status = initCySymSession(i, j, CPA_CY_SYM_HASH_MODE_AUTH,
614                                         CPA_CY_SYM_CIPHER_DIRECTION_ENCRYPT,
615                                         &qaCoreConf->encryptSessionHandleTbl[i][j],
616                                         qaCoreConf->instanceHandle,
617                                         lcore_id);
618                         if (CPA_STATUS_SUCCESS != status) {
619                                 printf("Crypto: Failed to initialize Encrypt sessions\n");
620                                 return CPA_STATUS_FAIL;
621                         }
622                         status = initCySymSession(i, j, CPA_CY_SYM_HASH_MODE_AUTH,
623                                         CPA_CY_SYM_CIPHER_DIRECTION_DECRYPT,
624                                         &qaCoreConf->decryptSessionHandleTbl[i][j],
625                                         qaCoreConf->instanceHandle,
626                                         lcore_id);
627                         if (CPA_STATUS_SUCCESS != status) {
628                                 printf("Crypto: Failed to initialize Decrypt sessions\n");
629                                 return CPA_STATUS_FAIL;
630                         }
631                 }
632         }
633         return CPA_STATUS_SUCCESS;
634 }
635
636 int
637 crypto_init(void)
638 {
639         if (CPA_STATUS_SUCCESS != icp_sal_userStartMultiProcess("SSL",CPA_FALSE)) {
640                 printf("Crypto: Could not start sal for user space\n");
641                 return CPA_STATUS_FAIL;
642         }
643         printf("Crypto: icp_sal_userStartMultiProcess(\"SSL\",CPA_FALSE)\n");
644         return 0;
645 }
646
647 /*
648  * Per core initialisation
649  */
650 int
651 per_core_crypto_init(uint32_t lcore_id)
652 {
653         CpaStatus status = CPA_STATUS_FAIL;
654         char memzone_name[RTE_MEMZONE_NAMESIZE];
655
656         int socketID = rte_lcore_to_socket_id(lcore_id);
657
658         /* Allocate software ring for response messages. */
659
660         qaCoreConf[lcore_id].callbackQueue.head = 0;
661         qaCoreConf[lcore_id].callbackQueue.tail = 0;
662         qaCoreConf[lcore_id].callbackQueue.numEntries = 0;
663         qaCoreConf[lcore_id].kickFreq = 0;
664         qaCoreConf[lcore_id].qaOutstandingRequests = 0;
665         qaCoreConf[lcore_id].numResponseAttempts = 0;
666
667         /* Initialise and reserve lcore memzone for virt2phys translation */
668         snprintf(memzone_name,
669                         RTE_MEMZONE_NAMESIZE,
670                         "lcore_%u",
671                         lcore_id);
672
673         qaCoreConf[lcore_id].lcoreMemzone.memzone = rte_memzone_reserve(
674                         memzone_name,
675                         LCORE_MEMZONE_SIZE,
676                         socketID,
677                         0);
678         if (NULL == qaCoreConf[lcore_id].lcoreMemzone.memzone) {
679                 printf("Crypto: Error allocating memzone on lcore %u\n",lcore_id);
680                 return -1;
681         }
682         qaCoreConf[lcore_id].lcoreMemzone.next_free_address =
683                                                         qaCoreConf[lcore_id].lcoreMemzone.memzone->addr;
684
685         qaCoreConf[lcore_id].pPacketIV = alloc_memzone_region(IV_LENGTH_16_BYTES,
686                                                         lcore_id);
687
688         if (NULL == qaCoreConf[lcore_id].pPacketIV ) {
689                 printf("Crypto: Failed to allocate memory for Initialization Vector\n");
690                 return -1;
691         }
692
693         memcpy(qaCoreConf[lcore_id].pPacketIV, &g_crypto_hash_keys.iv,
694                         IV_LENGTH_16_BYTES);
695
696         qaCoreConf[lcore_id].packetIVPhy = qa_v2p(qaCoreConf[lcore_id].pPacketIV);
697         if (0 == qaCoreConf[lcore_id].packetIVPhy) {
698                 printf("Crypto: Invalid physical address for Initialization Vector\n");
699                 return -1;
700         }
701
702         /*
703          * Obtain the instance handle that is mapped to the current lcore.
704          * This can fail if an instance is not mapped to a bank which has been
705          * affinitized to the current lcore.
706          */
707         status = get_crypto_instance_on_core(&(qaCoreConf[lcore_id].instanceHandle),
708                         lcore_id);
709         if (CPA_STATUS_SUCCESS != status) {
710                 printf("Crypto: get_crypto_instance_on_core failed with status: %"PRId32"\n",
711                                 status);
712                 return -1;
713         }
714
715         status = cpaCySymDpRegCbFunc(qaCoreConf[lcore_id].instanceHandle,
716                         (CpaCySymDpCbFunc) qa_crypto_callback);
717         if (CPA_STATUS_SUCCESS != status) {
718                 printf("Crypto: cpaCySymDpRegCbFunc failed with status: %"PRId32"\n", status);
719                 return -1;
720         }
721
722         /*
723          * Set the address translation callback for virtual to physcial address
724          * mapping. This will be called by the QAT driver during initialisation only.
725          */
726         status = cpaCySetAddressTranslation(qaCoreConf[lcore_id].instanceHandle,
727                         (CpaVirtualToPhysical) qa_v2p);
728         if (CPA_STATUS_SUCCESS != status) {
729                 printf("Crypto: cpaCySetAddressTranslation failed with status: %"PRId32"\n",
730                                 status);
731                 return -1;
732         }
733
734         status = initSessionDataTables(&qaCoreConf[lcore_id],lcore_id);
735         if (CPA_STATUS_SUCCESS != status) {
736                 printf("Crypto: Failed to allocate all session tables.");
737                 return -1;
738         }
739         return 0;
740 }
741
742 static CpaStatus
743 enqueueOp(CpaCySymDpOpData *opData, uint32_t lcore_id)
744 {
745
746         CpaStatus status;
747
748         /*
749          * Assumption is there is no requirement to do load balancing between
750          * acceleration units - that is one acceleration unit is tied to a core.
751          */
752         opData->instanceHandle = qaCoreConf[lcore_id].instanceHandle;
753
754         if ((++qaCoreConf[lcore_id].kickFreq) % CRYPTO_BURST_TX == 0) {
755                 status = cpaCySymDpEnqueueOp(opData, CPA_TRUE);
756         } else {
757                 status = cpaCySymDpEnqueueOp(opData, CPA_FALSE);
758         }
759
760         qaCoreConf[lcore_id].qaOutstandingRequests++;
761
762         return status;
763 }
764
765 void
766 crypto_flush_tx_queue(uint32_t lcore_id)
767 {
768
769         cpaCySymDpPerformOpNow(qaCoreConf[lcore_id].instanceHandle);
770 }
771
772 enum crypto_result
773 crypto_encrypt(struct rte_mbuf *rte_buff, enum cipher_alg c, enum hash_alg h)
774 {
775         CpaCySymDpOpData *opData =
776                         (CpaCySymDpOpData *) (rte_pktmbuf_mtod(rte_buff, char *)
777                                         + CRYPTO_OFFSET_TO_OPDATA);
778         uint32_t lcore_id;
779
780         if (unlikely(c >= NUM_CRYPTO || h >= NUM_HMAC))
781                 return CRYPTO_RESULT_FAIL;
782
783         lcore_id = rte_lcore_id();
784
785         bzero(opData, sizeof(CpaCySymDpOpData));
786
787         opData->srcBuffer = opData->dstBuffer = PACKET_DATA_START_PHYS(rte_buff);
788         opData->srcBufferLen = opData->dstBufferLen = rte_buff->data_len;
789         opData->sessionCtx = qaCoreConf[lcore_id].encryptSessionHandleTbl[c][h];
790         opData->thisPhys = PACKET_DATA_START_PHYS(rte_buff)
791                         + CRYPTO_OFFSET_TO_OPDATA;
792         opData->pCallbackTag = rte_buff;
793
794         /* if no crypto or hash operations are specified return fail */
795         if (NO_CIPHER == c && NO_HASH == h)
796                 return CRYPTO_RESULT_FAIL;
797
798         if (NO_CIPHER != c) {
799                 opData->pIv = qaCoreConf[lcore_id].pPacketIV;
800                 opData->iv = qaCoreConf[lcore_id].packetIVPhy;
801
802                 if (CIPHER_AES_CBC_128 == c)
803                         opData->ivLenInBytes = IV_LENGTH_16_BYTES;
804                 else
805                         opData->ivLenInBytes = IV_LENGTH_8_BYTES;
806
807                 opData->cryptoStartSrcOffsetInBytes = CRYPTO_START_OFFSET;
808                 opData->messageLenToCipherInBytes = rte_buff->data_len
809                                 - CRYPTO_START_OFFSET;
810                 /*
811                  * Work around for padding, message length has to be a multiple of
812                  * block size.
813                  */
814                 opData->messageLenToCipherInBytes -= opData->messageLenToCipherInBytes
815                                 % CIPHER_BLOCK_DEFAULT_SIZE;
816         }
817
818         if (NO_HASH != h) {
819
820                 opData->hashStartSrcOffsetInBytes = HASH_START_OFFSET;
821                 opData->messageLenToHashInBytes = rte_buff->data_len
822                                 - HASH_START_OFFSET;
823                 /*
824                  * Work around for padding, message length has to be a multiple of block
825                  * size.
826                  */
827                 opData->messageLenToHashInBytes -= opData->messageLenToHashInBytes
828                                 % HASH_BLOCK_DEFAULT_SIZE;
829
830                 /*
831                  * Assumption: Ok ignore the passed digest pointer and place HMAC at end
832                  * of packet.
833                  */
834                 opData->digestResult = rte_buff->buf_physaddr + rte_buff->data_len;
835         }
836
837         if (CPA_STATUS_SUCCESS != enqueueOp(opData, lcore_id)) {
838                 /*
839                  * Failed to place a packet on the hardware queue.
840                  * Most likely because the QA hardware is busy.
841                  */
842                 return CRYPTO_RESULT_FAIL;
843         }
844         return CRYPTO_RESULT_IN_PROGRESS;
845 }
846
847 enum crypto_result
848 crypto_decrypt(struct rte_mbuf *rte_buff, enum cipher_alg c, enum hash_alg h)
849 {
850
851         CpaCySymDpOpData *opData = (void*) (rte_pktmbuf_mtod(rte_buff, char *)
852                         + CRYPTO_OFFSET_TO_OPDATA);
853         uint32_t lcore_id;
854
855         if (unlikely(c >= NUM_CRYPTO || h >= NUM_HMAC))
856                 return CRYPTO_RESULT_FAIL;
857
858         lcore_id = rte_lcore_id();
859
860         bzero(opData, sizeof(CpaCySymDpOpData));
861
862         opData->dstBuffer = opData->srcBuffer = PACKET_DATA_START_PHYS(rte_buff);
863         opData->dstBufferLen = opData->srcBufferLen = rte_buff->data_len;
864         opData->thisPhys = PACKET_DATA_START_PHYS(rte_buff)
865                         + CRYPTO_OFFSET_TO_OPDATA;
866         opData->sessionCtx = qaCoreConf[lcore_id].decryptSessionHandleTbl[c][h];
867         opData->pCallbackTag = rte_buff;
868
869         /* if no crypto or hmac operations are specified return fail */
870         if (NO_CIPHER == c && NO_HASH == h)
871                 return CRYPTO_RESULT_FAIL;
872
873         if (NO_CIPHER != c) {
874                 opData->pIv = qaCoreConf[lcore_id].pPacketIV;
875                 opData->iv = qaCoreConf[lcore_id].packetIVPhy;
876
877                 if (CIPHER_AES_CBC_128 == c)
878                         opData->ivLenInBytes = IV_LENGTH_16_BYTES;
879                 else
880                         opData->ivLenInBytes = IV_LENGTH_8_BYTES;
881
882                 opData->cryptoStartSrcOffsetInBytes = CRYPTO_START_OFFSET;
883                 opData->messageLenToCipherInBytes = rte_buff->data_len
884                                 - CRYPTO_START_OFFSET;
885
886                 /*
887                  * Work around for padding, message length has to be a multiple of block
888                  * size.
889                  */
890                 opData->messageLenToCipherInBytes -= opData->messageLenToCipherInBytes
891                                 % CIPHER_BLOCK_DEFAULT_SIZE;
892         }
893         if (NO_HASH != h) {
894                 opData->hashStartSrcOffsetInBytes = HASH_START_OFFSET;
895                 opData->messageLenToHashInBytes = rte_buff->data_len
896                                 - HASH_START_OFFSET;
897                 /*
898                  * Work around for padding, message length has to be a multiple of block
899                  * size.
900                  */
901                 opData->messageLenToHashInBytes -= opData->messageLenToHashInBytes
902                                 % HASH_BLOCK_DEFAULT_SIZE;
903                 opData->digestResult = rte_buff->buf_physaddr + rte_buff->data_len;
904         }
905
906         if (CPA_STATUS_SUCCESS != enqueueOp(opData, lcore_id)) {
907                 /*
908                  * Failed to place a packet on the hardware queue.
909                  * Most likely because the QA hardware is busy.
910                  */
911                 return CRYPTO_RESULT_FAIL;
912         }
913         return CRYPTO_RESULT_IN_PROGRESS;
914 }
915
916 void *
917 crypto_get_next_response(void)
918 {
919         uint32_t lcore_id;
920         lcore_id = rte_lcore_id();
921         struct qa_callbackQueue *callbackQ = &(qaCoreConf[lcore_id].callbackQueue);
922         void *entry = NULL;
923
924         if (callbackQ->numEntries) {
925                 entry = callbackQ->qaCallbackRing[callbackQ->tail];
926                 callbackQ->tail++;
927                 callbackQ->numEntries--;
928         }
929
930         /* If there are no outstanding requests no need to poll, return entry */
931         if (qaCoreConf[lcore_id].qaOutstandingRequests == 0)
932                 return entry;
933
934         if (callbackQ->numEntries < CRYPTO_QUEUED_RESP_POLL_THRESHOLD
935                         && qaCoreConf[lcore_id].numResponseAttempts++
936                                         % GET_NEXT_RESPONSE_FREQ == 0) {
937                 /*
938                  * Only poll the hardware when there is less than
939                  * CRYPTO_QUEUED_RESP_POLL_THRESHOLD elements in the software queue
940                  */
941                 icp_sal_CyPollDpInstance(qaCoreConf[lcore_id].instanceHandle,
942                                 CRYPTO_MAX_RESPONSE_QUOTA);
943         }
944         return entry;
945 }