4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
40 #include <sys/queue.h>
43 #include <rte_common.h>
45 #include <rte_debug.h>
46 #include <rte_memory.h>
47 #include <rte_memzone.h>
48 #include <rte_ether.h>
49 #include <rte_malloc.h>
50 #include <rte_launch.h>
52 #include <rte_per_lcore.h>
53 #include <rte_lcore.h>
54 #include <rte_atomic.h>
55 #include <rte_branch_prediction.h>
56 #include <rte_mempool.h>
58 #include <rte_string_fns.h>
60 #define CPA_CY_SYM_DP_TMP_WORKAROUND 1
63 #include "cpa_types.h"
64 #include "cpa_cy_sym_dp.h"
65 #include "cpa_cy_common.h"
66 #include "cpa_cy_im.h"
67 #include "icp_sal_user.h"
68 #include "icp_sal_poll.h"
72 /* CIPHER KEY LENGTHS */
73 #define KEY_SIZE_64_IN_BYTES (64 / 8)
74 #define KEY_SIZE_56_IN_BYTES (56 / 8)
75 #define KEY_SIZE_128_IN_BYTES (128 / 8)
76 #define KEY_SIZE_168_IN_BYTES (168 / 8)
77 #define KEY_SIZE_192_IN_BYTES (192 / 8)
78 #define KEY_SIZE_256_IN_BYTES (256 / 8)
80 /* HMAC AUTH KEY LENGTHS */
81 #define AES_XCBC_AUTH_KEY_LENGTH_IN_BYTES (128 / 8)
82 #define SHA1_AUTH_KEY_LENGTH_IN_BYTES (160 / 8)
83 #define SHA224_AUTH_KEY_LENGTH_IN_BYTES (224 / 8)
84 #define SHA256_AUTH_KEY_LENGTH_IN_BYTES (256 / 8)
85 #define SHA384_AUTH_KEY_LENGTH_IN_BYTES (384 / 8)
86 #define SHA512_AUTH_KEY_LENGTH_IN_BYTES (512 / 8)
87 #define MD5_AUTH_KEY_LENGTH_IN_BYTES (128 / 8)
88 #define KASUMI_AUTH_KEY_LENGTH_IN_BYTES (128 / 8)
90 /* HASH DIGEST LENGHTS */
91 #define AES_XCBC_DIGEST_LENGTH_IN_BYTES (128 / 8)
92 #define AES_XCBC_96_DIGEST_LENGTH_IN_BYTES (96 / 8)
93 #define MD5_DIGEST_LENGTH_IN_BYTES (128 / 8)
94 #define SHA1_DIGEST_LENGTH_IN_BYTES (160 / 8)
95 #define SHA1_96_DIGEST_LENGTH_IN_BYTES (96 / 8)
96 #define SHA224_DIGEST_LENGTH_IN_BYTES (224 / 8)
97 #define SHA256_DIGEST_LENGTH_IN_BYTES (256 / 8)
98 #define SHA384_DIGEST_LENGTH_IN_BYTES (384 / 8)
99 #define SHA512_DIGEST_LENGTH_IN_BYTES (512 / 8)
100 #define KASUMI_DIGEST_LENGTH_IN_BYTES (32 / 8)
102 #define IV_LENGTH_16_BYTES (16)
103 #define IV_LENGTH_8_BYTES (8)
107 * rte_memzone is used to allocate physically contiguous virtual memory.
108 * In this application we allocate a single block and divide between variables
109 * which require a virtual to physical mapping for use by the QAT driver.
110 * Virt2phys is only performed during initialisation and not on the data-path.
113 #define LCORE_MEMZONE_SIZE (1 << 22)
117 const struct rte_memzone *memzone;
118 void *next_free_address;
122 * Size the qa software response queue.
123 * Note: Head and Tail are 8 bit, therefore, the queue is
124 * fixed to 256 entries.
126 #define CRYPTO_SOFTWARE_QUEUE_SIZE 256
128 struct qa_callbackQueue {
132 struct rte_mbuf *qaCallbackRing[CRYPTO_SOFTWARE_QUEUE_SIZE];
135 struct qa_core_conf {
136 CpaCySymDpSessionCtx *encryptSessionHandleTbl[NUM_CRYPTO][NUM_HMAC];
137 CpaCySymDpSessionCtx *decryptSessionHandleTbl[NUM_CRYPTO][NUM_HMAC];
138 CpaInstanceHandle instanceHandle;
139 struct qa_callbackQueue callbackQueue;
140 uint64_t qaOutstandingRequests;
141 uint64_t numResponseAttempts;
144 CpaPhysicalAddr packetIVPhy;
145 struct lcore_memzone lcoreMemzone;
146 } __rte_cache_aligned;
148 #define MAX_CORES (RTE_MAX_LCORE)
150 static struct qa_core_conf qaCoreConf[MAX_CORES];
153 *Create maximum possible key size,
154 *One for cipher and one for hash
157 uint8_t cipher_key[32];
158 uint8_t hash_key[64];
162 struct glob_keys g_crypto_hash_keys = {
163 .cipher_key = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
164 0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10,
165 0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,
166 0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,0x20},
167 .hash_key = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
168 0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10,
169 0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,
170 0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,0x20,
171 0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,
172 0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,0x30,
173 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,
174 0x39,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,0x50},
175 .iv = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
176 0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10}
180 * Offsets from the start of the packet.
183 #define PACKET_DATA_START_PHYS(p) \
184 ((p)->buf_physaddr + (p)->data_off)
187 * A fixed offset to where the crypto is to be performed, which is the first
188 * byte after the Ethernet(14 bytes) and IPv4 headers(20 bytes)
190 #define CRYPTO_START_OFFSET (14+20)
191 #define HASH_START_OFFSET (14+20)
192 #define CIPHER_BLOCK_DEFAULT_SIZE (16)
193 #define HASH_BLOCK_DEFAULT_SIZE (16)
196 * Offset to the opdata from the start of the data portion of packet.
197 * Assumption: The buffer is physically contiguous.
198 * +18 takes this to the next cache line.
201 #define CRYPTO_OFFSET_TO_OPDATA (ETHER_MAX_LEN+18)
204 * Default number of requests to place on the hardware ring before kicking the
207 #define CRYPTO_BURST_TX (16)
210 * Only call the qa poll function when the number responses in the software
211 * queue drops below this number.
213 #define CRYPTO_QUEUED_RESP_POLL_THRESHOLD (32)
216 * Limit the number of polls per call to get_next_response.
218 #define GET_NEXT_RESPONSE_FREQ (32)
221 * Max number of responses to pull from the qa in one poll.
223 #define CRYPTO_MAX_RESPONSE_QUOTA \
224 (CRYPTO_SOFTWARE_QUEUE_SIZE-CRYPTO_QUEUED_RESP_POLL_THRESHOLD-1)
226 #if (CRYPTO_QUEUED_RESP_POLL_THRESHOLD + CRYPTO_MAX_RESPONSE_QUOTA >= \
227 CRYPTO_SOFTWARE_QUEUE_SIZE)
228 #error Its possible to overflow the qa response Q with current poll and \
233 crypto_callback(CpaCySymDpOpData *pOpData,
234 __rte_unused CpaStatus status,
235 __rte_unused CpaBoolean verifyResult)
238 lcore_id = rte_lcore_id();
239 struct qa_callbackQueue *callbackQ = &(qaCoreConf[lcore_id].callbackQueue);
242 * Received a completion from the QA hardware.
243 * Place the response on the return queue.
245 callbackQ->qaCallbackRing[callbackQ->head] = pOpData->pCallbackTag;
247 callbackQ->numEntries++;
248 qaCoreConf[lcore_id].qaOutstandingRequests--;
252 qa_crypto_callback(CpaCySymDpOpData *pOpData, CpaStatus status,
253 CpaBoolean verifyResult)
255 crypto_callback(pOpData, status, verifyResult);
259 * Each allocation from a particular memzone lasts for the life-time of
260 * the application. No freeing of previous allocations will occur.
263 alloc_memzone_region(uint32_t length, uint32_t lcore_id)
265 char *current_free_addr_ptr = NULL;
266 struct lcore_memzone *lcore_memzone = &(qaCoreConf[lcore_id].lcoreMemzone);
268 current_free_addr_ptr = lcore_memzone->next_free_address;
270 if (current_free_addr_ptr + length >=
271 (char *)lcore_memzone->memzone->addr + lcore_memzone->memzone->len) {
272 printf("Crypto: No memory available in memzone\n");
275 lcore_memzone->next_free_address = current_free_addr_ptr + length;
277 return (void *)current_free_addr_ptr;
281 * Virtual to Physical Address translation is only executed during initialization
282 * and not on the data-path.
284 static CpaPhysicalAddr
287 const struct rte_memzone *memzone = NULL;
288 uint32_t lcore_id = 0;
289 RTE_LCORE_FOREACH(lcore_id) {
290 memzone = qaCoreConf[lcore_id].lcoreMemzone.memzone;
292 if ((char*) ptr >= (char *) memzone->addr &&
293 (char*) ptr < ((char*) memzone->addr + memzone->len)) {
294 return (CpaPhysicalAddr)
295 (memzone->phys_addr + ((char *) ptr - (char*) memzone->addr));
298 printf("Crypto: Corresponding physical address not found in memzone\n");
299 return (CpaPhysicalAddr) 0;
303 getCoreAffinity(Cpa32U *coreAffinity, const CpaInstanceHandle instanceHandle)
305 CpaInstanceInfo2 info;
307 CpaStatus status = CPA_STATUS_SUCCESS;
309 memset(&info, 0, sizeof(CpaInstanceInfo2));
311 status = cpaCyInstanceGetInfo2(instanceHandle, &info);
312 if (CPA_STATUS_SUCCESS != status) {
313 printf("Crypto: Error getting instance info\n");
314 return CPA_STATUS_FAIL;
316 for (i = 0; i < MAX_CORES; i++) {
317 if (CPA_BITMAP_BIT_TEST(info.coreAffinity, i)) {
319 return CPA_STATUS_SUCCESS;
322 return CPA_STATUS_FAIL;
326 get_crypto_instance_on_core(CpaInstanceHandle *pInstanceHandle,
329 Cpa16U numInstances = 0, i = 0;
330 CpaStatus status = CPA_STATUS_FAIL;
331 CpaInstanceHandle *pLocalInstanceHandles = NULL;
332 Cpa32U coreAffinity = 0;
334 status = cpaCyGetNumInstances(&numInstances);
335 if (CPA_STATUS_SUCCESS != status || numInstances == 0) {
336 return CPA_STATUS_FAIL;
339 pLocalInstanceHandles = rte_malloc("pLocalInstanceHandles",
340 sizeof(CpaInstanceHandle) * numInstances, RTE_CACHE_LINE_SIZE);
342 if (NULL == pLocalInstanceHandles) {
343 return CPA_STATUS_FAIL;
345 status = cpaCyGetInstances(numInstances, pLocalInstanceHandles);
346 if (CPA_STATUS_SUCCESS != status) {
347 printf("Crypto: cpaCyGetInstances failed with status: %"PRId32"\n", status);
348 rte_free((void *) pLocalInstanceHandles);
349 return CPA_STATUS_FAIL;
352 for (i = 0; i < numInstances; i++) {
353 status = getCoreAffinity(&coreAffinity, pLocalInstanceHandles[i]);
354 if (CPA_STATUS_SUCCESS != status) {
355 rte_free((void *) pLocalInstanceHandles);
356 return CPA_STATUS_FAIL;
358 if (coreAffinity == lcore_id) {
359 printf("Crypto: instance found on core %d\n", i);
360 *pInstanceHandle = pLocalInstanceHandles[i];
361 return CPA_STATUS_SUCCESS;
364 /* core affinity not found */
365 rte_free((void *) pLocalInstanceHandles);
366 return CPA_STATUS_FAIL;
370 initCySymSession(const int pkt_cipher_alg,
371 const int pkt_hash_alg, const CpaCySymHashMode hashMode,
372 const CpaCySymCipherDirection crypto_direction,
373 CpaCySymSessionCtx **ppSessionCtx,
374 const CpaInstanceHandle cyInstanceHandle,
375 const uint32_t lcore_id)
377 Cpa32U sessionCtxSizeInBytes = 0;
378 CpaStatus status = CPA_STATUS_FAIL;
379 CpaBoolean isCrypto = CPA_TRUE, isHmac = CPA_TRUE;
380 CpaCySymSessionSetupData sessionSetupData;
382 memset(&sessionSetupData, 0, sizeof(CpaCySymSessionSetupData));
384 /* Assumption: key length is set to each algorithm's max length */
385 switch (pkt_cipher_alg) {
387 isCrypto = CPA_FALSE;
390 sessionSetupData.cipherSetupData.cipherAlgorithm =
391 CPA_CY_SYM_CIPHER_DES_ECB;
392 sessionSetupData.cipherSetupData.cipherKeyLenInBytes =
393 KEY_SIZE_64_IN_BYTES;
396 sessionSetupData.cipherSetupData.cipherAlgorithm =
397 CPA_CY_SYM_CIPHER_DES_CBC;
398 sessionSetupData.cipherSetupData.cipherKeyLenInBytes =
399 KEY_SIZE_64_IN_BYTES;
402 sessionSetupData.cipherSetupData.cipherAlgorithm =
403 CPA_CY_SYM_CIPHER_3DES_ECB;
404 sessionSetupData.cipherSetupData.cipherKeyLenInBytes =
405 KEY_SIZE_192_IN_BYTES;
407 case CIPHER_DES3_CBC:
408 sessionSetupData.cipherSetupData.cipherAlgorithm =
409 CPA_CY_SYM_CIPHER_3DES_CBC;
410 sessionSetupData.cipherSetupData.cipherKeyLenInBytes =
411 KEY_SIZE_192_IN_BYTES;
414 sessionSetupData.cipherSetupData.cipherAlgorithm =
415 CPA_CY_SYM_CIPHER_AES_ECB;
416 sessionSetupData.cipherSetupData.cipherKeyLenInBytes =
417 KEY_SIZE_128_IN_BYTES;
419 case CIPHER_AES_CBC_128:
420 sessionSetupData.cipherSetupData.cipherAlgorithm =
421 CPA_CY_SYM_CIPHER_AES_CBC;
422 sessionSetupData.cipherSetupData.cipherKeyLenInBytes =
423 KEY_SIZE_128_IN_BYTES;
425 case CIPHER_KASUMI_F8:
426 sessionSetupData.cipherSetupData.cipherAlgorithm =
427 CPA_CY_SYM_CIPHER_KASUMI_F8;
428 sessionSetupData.cipherSetupData.cipherKeyLenInBytes =
429 KEY_SIZE_128_IN_BYTES;
432 printf("Crypto: Undefined Cipher specified\n");
435 /* Set the cipher direction */
437 sessionSetupData.cipherSetupData.cipherDirection = crypto_direction;
438 sessionSetupData.cipherSetupData.pCipherKey =
439 g_crypto_hash_keys.cipher_key;
440 sessionSetupData.symOperation = CPA_CY_SYM_OP_CIPHER;
443 /* Setup Hash common fields */
444 switch (pkt_hash_alg) {
449 sessionSetupData.hashSetupData.hashAlgorithm = CPA_CY_SYM_HASH_AES_XCBC;
450 sessionSetupData.hashSetupData.digestResultLenInBytes =
451 AES_XCBC_DIGEST_LENGTH_IN_BYTES;
453 case HASH_AES_XCBC_96:
454 sessionSetupData.hashSetupData.hashAlgorithm = CPA_CY_SYM_HASH_AES_XCBC;
455 sessionSetupData.hashSetupData.digestResultLenInBytes =
456 AES_XCBC_96_DIGEST_LENGTH_IN_BYTES;
459 sessionSetupData.hashSetupData.hashAlgorithm = CPA_CY_SYM_HASH_MD5;
460 sessionSetupData.hashSetupData.digestResultLenInBytes =
461 MD5_DIGEST_LENGTH_IN_BYTES;
464 sessionSetupData.hashSetupData.hashAlgorithm = CPA_CY_SYM_HASH_SHA1;
465 sessionSetupData.hashSetupData.digestResultLenInBytes =
466 SHA1_DIGEST_LENGTH_IN_BYTES;
469 sessionSetupData.hashSetupData.hashAlgorithm = CPA_CY_SYM_HASH_SHA1;
470 sessionSetupData.hashSetupData.digestResultLenInBytes =
471 SHA1_96_DIGEST_LENGTH_IN_BYTES;
474 sessionSetupData.hashSetupData.hashAlgorithm = CPA_CY_SYM_HASH_SHA224;
475 sessionSetupData.hashSetupData.digestResultLenInBytes =
476 SHA224_DIGEST_LENGTH_IN_BYTES;
479 sessionSetupData.hashSetupData.hashAlgorithm = CPA_CY_SYM_HASH_SHA256;
480 sessionSetupData.hashSetupData.digestResultLenInBytes =
481 SHA256_DIGEST_LENGTH_IN_BYTES;
484 sessionSetupData.hashSetupData.hashAlgorithm = CPA_CY_SYM_HASH_SHA384;
485 sessionSetupData.hashSetupData.digestResultLenInBytes =
486 SHA384_DIGEST_LENGTH_IN_BYTES;
489 sessionSetupData.hashSetupData.hashAlgorithm = CPA_CY_SYM_HASH_SHA512;
490 sessionSetupData.hashSetupData.digestResultLenInBytes =
491 SHA512_DIGEST_LENGTH_IN_BYTES;
494 sessionSetupData.hashSetupData.hashAlgorithm = CPA_CY_SYM_HASH_KASUMI_F9;
495 sessionSetupData.hashSetupData.digestResultLenInBytes =
496 KASUMI_DIGEST_LENGTH_IN_BYTES;
499 printf("Crypto: Undefined Hash specified\n");
503 sessionSetupData.hashSetupData.hashMode = hashMode;
504 sessionSetupData.symOperation = CPA_CY_SYM_OP_HASH;
505 /* If using authenticated hash setup key lengths */
506 if (CPA_CY_SYM_HASH_MODE_AUTH == hashMode) {
507 /* Use a common max length key */
508 sessionSetupData.hashSetupData.authModeSetupData.authKey =
509 g_crypto_hash_keys.hash_key;
510 switch (pkt_hash_alg) {
512 case HASH_AES_XCBC_96:
513 sessionSetupData.hashSetupData.authModeSetupData.authKeyLenInBytes =
514 AES_XCBC_AUTH_KEY_LENGTH_IN_BYTES;
517 sessionSetupData.hashSetupData.authModeSetupData.authKeyLenInBytes =
518 SHA1_AUTH_KEY_LENGTH_IN_BYTES;
522 sessionSetupData.hashSetupData.authModeSetupData.authKeyLenInBytes =
523 SHA1_AUTH_KEY_LENGTH_IN_BYTES;
526 sessionSetupData.hashSetupData.authModeSetupData.authKeyLenInBytes =
527 SHA224_AUTH_KEY_LENGTH_IN_BYTES;
530 sessionSetupData.hashSetupData.authModeSetupData.authKeyLenInBytes =
531 SHA256_AUTH_KEY_LENGTH_IN_BYTES;
534 sessionSetupData.hashSetupData.authModeSetupData.authKeyLenInBytes =
535 SHA384_AUTH_KEY_LENGTH_IN_BYTES;
538 sessionSetupData.hashSetupData.authModeSetupData.authKeyLenInBytes =
539 SHA512_AUTH_KEY_LENGTH_IN_BYTES;
542 sessionSetupData.hashSetupData.authModeSetupData.authKeyLenInBytes =
543 KASUMI_AUTH_KEY_LENGTH_IN_BYTES;
546 printf("Crypto: Undefined Hash specified\n");
547 return CPA_STATUS_FAIL;
552 /* Only high priority supported */
553 sessionSetupData.sessionPriority = CPA_CY_PRIORITY_HIGH;
555 /* If chaining algorithms */
556 if (isCrypto && isHmac) {
557 sessionSetupData.symOperation = CPA_CY_SYM_OP_ALGORITHM_CHAINING;
558 /* @assumption Alg Chain order is cipher then hash for encrypt
559 * and hash then cipher then has for decrypt*/
560 if (CPA_CY_SYM_CIPHER_DIRECTION_ENCRYPT == crypto_direction) {
561 sessionSetupData.algChainOrder =
562 CPA_CY_SYM_ALG_CHAIN_ORDER_CIPHER_THEN_HASH;
564 sessionSetupData.algChainOrder =
565 CPA_CY_SYM_ALG_CHAIN_ORDER_HASH_THEN_CIPHER;
568 if (!isCrypto && !isHmac) {
569 *ppSessionCtx = NULL;
570 return CPA_STATUS_SUCCESS;
573 /* Set flags for digest operations */
574 sessionSetupData.digestIsAppended = CPA_FALSE;
575 sessionSetupData.verifyDigest = CPA_TRUE;
577 /* Get the session context size based on the crypto and/or hash operations*/
578 status = cpaCySymDpSessionCtxGetSize(cyInstanceHandle, &sessionSetupData,
579 &sessionCtxSizeInBytes);
580 if (CPA_STATUS_SUCCESS != status) {
581 printf("Crypto: cpaCySymDpSessionCtxGetSize error, status: %"PRId32"\n",
583 return CPA_STATUS_FAIL;
586 *ppSessionCtx = alloc_memzone_region(sessionCtxSizeInBytes, lcore_id);
587 if (NULL == *ppSessionCtx) {
588 printf("Crypto: Failed to allocate memory for Session Context\n");
589 return CPA_STATUS_FAIL;
592 status = cpaCySymDpInitSession(cyInstanceHandle, &sessionSetupData,
594 if (CPA_STATUS_SUCCESS != status) {
595 printf("Crypto: cpaCySymDpInitSession failed with status %"PRId32"\n", status);
596 return CPA_STATUS_FAIL;
598 return CPA_STATUS_SUCCESS;
602 initSessionDataTables(struct qa_core_conf *qaCoreConf,uint32_t lcore_id)
605 CpaStatus status = CPA_STATUS_FAIL;
606 for (i = 0; i < NUM_CRYPTO; i++) {
607 for (j = 0; j < NUM_HMAC; j++) {
608 if (((i == CIPHER_KASUMI_F8) && (j != NO_HASH) && (j != HASH_KASUMI_F9)) ||
609 ((i != NO_CIPHER) && (i != CIPHER_KASUMI_F8) && (j == HASH_KASUMI_F9)))
611 status = initCySymSession(i, j, CPA_CY_SYM_HASH_MODE_AUTH,
612 CPA_CY_SYM_CIPHER_DIRECTION_ENCRYPT,
613 &qaCoreConf->encryptSessionHandleTbl[i][j],
614 qaCoreConf->instanceHandle,
616 if (CPA_STATUS_SUCCESS != status) {
617 printf("Crypto: Failed to initialize Encrypt sessions\n");
618 return CPA_STATUS_FAIL;
620 status = initCySymSession(i, j, CPA_CY_SYM_HASH_MODE_AUTH,
621 CPA_CY_SYM_CIPHER_DIRECTION_DECRYPT,
622 &qaCoreConf->decryptSessionHandleTbl[i][j],
623 qaCoreConf->instanceHandle,
625 if (CPA_STATUS_SUCCESS != status) {
626 printf("Crypto: Failed to initialize Decrypt sessions\n");
627 return CPA_STATUS_FAIL;
631 return CPA_STATUS_SUCCESS;
637 if (CPA_STATUS_SUCCESS != icp_sal_userStartMultiProcess("SSL",CPA_FALSE)) {
638 printf("Crypto: Could not start sal for user space\n");
639 return CPA_STATUS_FAIL;
641 printf("Crypto: icp_sal_userStartMultiProcess(\"SSL\",CPA_FALSE)\n");
646 * Per core initialisation
649 per_core_crypto_init(uint32_t lcore_id)
651 CpaStatus status = CPA_STATUS_FAIL;
652 char memzone_name[RTE_MEMZONE_NAMESIZE];
654 int socketID = rte_lcore_to_socket_id(lcore_id);
656 /* Allocate software ring for response messages. */
658 qaCoreConf[lcore_id].callbackQueue.head = 0;
659 qaCoreConf[lcore_id].callbackQueue.tail = 0;
660 qaCoreConf[lcore_id].callbackQueue.numEntries = 0;
661 qaCoreConf[lcore_id].kickFreq = 0;
662 qaCoreConf[lcore_id].qaOutstandingRequests = 0;
663 qaCoreConf[lcore_id].numResponseAttempts = 0;
665 /* Initialise and reserve lcore memzone for virt2phys translation */
666 snprintf(memzone_name,
667 RTE_MEMZONE_NAMESIZE,
671 qaCoreConf[lcore_id].lcoreMemzone.memzone = rte_memzone_reserve(
676 if (NULL == qaCoreConf[lcore_id].lcoreMemzone.memzone) {
677 printf("Crypto: Error allocating memzone on lcore %u\n",lcore_id);
680 qaCoreConf[lcore_id].lcoreMemzone.next_free_address =
681 qaCoreConf[lcore_id].lcoreMemzone.memzone->addr;
683 qaCoreConf[lcore_id].pPacketIV = alloc_memzone_region(IV_LENGTH_16_BYTES,
686 if (NULL == qaCoreConf[lcore_id].pPacketIV ) {
687 printf("Crypto: Failed to allocate memory for Initialization Vector\n");
691 memcpy(qaCoreConf[lcore_id].pPacketIV, &g_crypto_hash_keys.iv,
694 qaCoreConf[lcore_id].packetIVPhy = qa_v2p(qaCoreConf[lcore_id].pPacketIV);
695 if (0 == qaCoreConf[lcore_id].packetIVPhy) {
696 printf("Crypto: Invalid physical address for Initialization Vector\n");
701 * Obtain the instance handle that is mapped to the current lcore.
702 * This can fail if an instance is not mapped to a bank which has been
703 * affinitized to the current lcore.
705 status = get_crypto_instance_on_core(&(qaCoreConf[lcore_id].instanceHandle),
707 if (CPA_STATUS_SUCCESS != status) {
708 printf("Crypto: get_crypto_instance_on_core failed with status: %"PRId32"\n",
713 status = cpaCySymDpRegCbFunc(qaCoreConf[lcore_id].instanceHandle,
714 (CpaCySymDpCbFunc) qa_crypto_callback);
715 if (CPA_STATUS_SUCCESS != status) {
716 printf("Crypto: cpaCySymDpRegCbFunc failed with status: %"PRId32"\n", status);
721 * Set the address translation callback for virtual to physcial address
722 * mapping. This will be called by the QAT driver during initialisation only.
724 status = cpaCySetAddressTranslation(qaCoreConf[lcore_id].instanceHandle,
725 (CpaVirtualToPhysical) qa_v2p);
726 if (CPA_STATUS_SUCCESS != status) {
727 printf("Crypto: cpaCySetAddressTranslation failed with status: %"PRId32"\n",
732 status = initSessionDataTables(&qaCoreConf[lcore_id],lcore_id);
733 if (CPA_STATUS_SUCCESS != status) {
734 printf("Crypto: Failed to allocate all session tables.");
741 enqueueOp(CpaCySymDpOpData *opData, uint32_t lcore_id)
747 * Assumption is there is no requirement to do load balancing between
748 * acceleration units - that is one acceleration unit is tied to a core.
750 opData->instanceHandle = qaCoreConf[lcore_id].instanceHandle;
752 if ((++qaCoreConf[lcore_id].kickFreq) % CRYPTO_BURST_TX == 0) {
753 status = cpaCySymDpEnqueueOp(opData, CPA_TRUE);
755 status = cpaCySymDpEnqueueOp(opData, CPA_FALSE);
758 qaCoreConf[lcore_id].qaOutstandingRequests++;
764 crypto_flush_tx_queue(uint32_t lcore_id)
767 cpaCySymDpPerformOpNow(qaCoreConf[lcore_id].instanceHandle);
771 crypto_encrypt(struct rte_mbuf *rte_buff, enum cipher_alg c, enum hash_alg h)
773 CpaCySymDpOpData *opData =
774 rte_pktmbuf_mtod_offset(rte_buff, CpaCySymDpOpData *,
775 CRYPTO_OFFSET_TO_OPDATA);
778 if (unlikely(c >= NUM_CRYPTO || h >= NUM_HMAC))
779 return CRYPTO_RESULT_FAIL;
781 lcore_id = rte_lcore_id();
783 memset(opData, 0, sizeof(CpaCySymDpOpData));
785 opData->srcBuffer = opData->dstBuffer = PACKET_DATA_START_PHYS(rte_buff);
786 opData->srcBufferLen = opData->dstBufferLen = rte_buff->data_len;
787 opData->sessionCtx = qaCoreConf[lcore_id].encryptSessionHandleTbl[c][h];
788 opData->thisPhys = PACKET_DATA_START_PHYS(rte_buff)
789 + CRYPTO_OFFSET_TO_OPDATA;
790 opData->pCallbackTag = rte_buff;
792 /* if no crypto or hash operations are specified return fail */
793 if (NO_CIPHER == c && NO_HASH == h)
794 return CRYPTO_RESULT_FAIL;
796 if (NO_CIPHER != c) {
797 opData->pIv = qaCoreConf[lcore_id].pPacketIV;
798 opData->iv = qaCoreConf[lcore_id].packetIVPhy;
800 if (CIPHER_AES_CBC_128 == c)
801 opData->ivLenInBytes = IV_LENGTH_16_BYTES;
803 opData->ivLenInBytes = IV_LENGTH_8_BYTES;
805 opData->cryptoStartSrcOffsetInBytes = CRYPTO_START_OFFSET;
806 opData->messageLenToCipherInBytes = rte_buff->data_len
807 - CRYPTO_START_OFFSET;
809 * Work around for padding, message length has to be a multiple of
812 opData->messageLenToCipherInBytes -= opData->messageLenToCipherInBytes
813 % CIPHER_BLOCK_DEFAULT_SIZE;
818 opData->hashStartSrcOffsetInBytes = HASH_START_OFFSET;
819 opData->messageLenToHashInBytes = rte_buff->data_len
822 * Work around for padding, message length has to be a multiple of block
825 opData->messageLenToHashInBytes -= opData->messageLenToHashInBytes
826 % HASH_BLOCK_DEFAULT_SIZE;
829 * Assumption: Ok ignore the passed digest pointer and place HMAC at end
832 opData->digestResult = rte_buff->buf_physaddr + rte_buff->data_len;
835 if (CPA_STATUS_SUCCESS != enqueueOp(opData, lcore_id)) {
837 * Failed to place a packet on the hardware queue.
838 * Most likely because the QA hardware is busy.
840 return CRYPTO_RESULT_FAIL;
842 return CRYPTO_RESULT_IN_PROGRESS;
846 crypto_decrypt(struct rte_mbuf *rte_buff, enum cipher_alg c, enum hash_alg h)
849 CpaCySymDpOpData *opData = rte_pktmbuf_mtod_offset(rte_buff, void *,
850 CRYPTO_OFFSET_TO_OPDATA);
853 if (unlikely(c >= NUM_CRYPTO || h >= NUM_HMAC))
854 return CRYPTO_RESULT_FAIL;
856 lcore_id = rte_lcore_id();
858 memset(opData, 0, sizeof(CpaCySymDpOpData));
860 opData->dstBuffer = opData->srcBuffer = PACKET_DATA_START_PHYS(rte_buff);
861 opData->dstBufferLen = opData->srcBufferLen = rte_buff->data_len;
862 opData->thisPhys = PACKET_DATA_START_PHYS(rte_buff)
863 + CRYPTO_OFFSET_TO_OPDATA;
864 opData->sessionCtx = qaCoreConf[lcore_id].decryptSessionHandleTbl[c][h];
865 opData->pCallbackTag = rte_buff;
867 /* if no crypto or hmac operations are specified return fail */
868 if (NO_CIPHER == c && NO_HASH == h)
869 return CRYPTO_RESULT_FAIL;
871 if (NO_CIPHER != c) {
872 opData->pIv = qaCoreConf[lcore_id].pPacketIV;
873 opData->iv = qaCoreConf[lcore_id].packetIVPhy;
875 if (CIPHER_AES_CBC_128 == c)
876 opData->ivLenInBytes = IV_LENGTH_16_BYTES;
878 opData->ivLenInBytes = IV_LENGTH_8_BYTES;
880 opData->cryptoStartSrcOffsetInBytes = CRYPTO_START_OFFSET;
881 opData->messageLenToCipherInBytes = rte_buff->data_len
882 - CRYPTO_START_OFFSET;
885 * Work around for padding, message length has to be a multiple of block
888 opData->messageLenToCipherInBytes -= opData->messageLenToCipherInBytes
889 % CIPHER_BLOCK_DEFAULT_SIZE;
892 opData->hashStartSrcOffsetInBytes = HASH_START_OFFSET;
893 opData->messageLenToHashInBytes = rte_buff->data_len
896 * Work around for padding, message length has to be a multiple of block
899 opData->messageLenToHashInBytes -= opData->messageLenToHashInBytes
900 % HASH_BLOCK_DEFAULT_SIZE;
901 opData->digestResult = rte_buff->buf_physaddr + rte_buff->data_len;
904 if (CPA_STATUS_SUCCESS != enqueueOp(opData, lcore_id)) {
906 * Failed to place a packet on the hardware queue.
907 * Most likely because the QA hardware is busy.
909 return CRYPTO_RESULT_FAIL;
911 return CRYPTO_RESULT_IN_PROGRESS;
915 crypto_get_next_response(void)
918 lcore_id = rte_lcore_id();
919 struct qa_callbackQueue *callbackQ = &(qaCoreConf[lcore_id].callbackQueue);
922 if (callbackQ->numEntries) {
923 entry = callbackQ->qaCallbackRing[callbackQ->tail];
925 callbackQ->numEntries--;
928 /* If there are no outstanding requests no need to poll, return entry */
929 if (qaCoreConf[lcore_id].qaOutstandingRequests == 0)
932 if (callbackQ->numEntries < CRYPTO_QUEUED_RESP_POLL_THRESHOLD
933 && qaCoreConf[lcore_id].numResponseAttempts++
934 % GET_NEXT_RESPONSE_FREQ == 0) {
936 * Only poll the hardware when there is less than
937 * CRYPTO_QUEUED_RESP_POLL_THRESHOLD elements in the software queue
939 icp_sal_CyPollDpInstance(qaCoreConf[lcore_id].instanceHandle,
940 CRYPTO_MAX_RESPONSE_QUOTA);