4 * Copyright(c) 2010-2012 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.
33 * version: DPDK.L.1.2.3-3
42 #include <sys/queue.h>
45 #include <rte_common.h>
47 #include <rte_debug.h>
48 #include <rte_memory.h>
49 #include <rte_memzone.h>
50 #include <rte_tailq.h>
51 #include <rte_ether.h>
52 #include <rte_malloc.h>
53 #include <rte_launch.h>
55 #include <rte_per_lcore.h>
56 #include <rte_lcore.h>
57 #include <rte_atomic.h>
58 #include <rte_branch_prediction.h>
60 #include <rte_mempool.h>
62 #include <rte_string_fns.h>
64 #define CPA_CY_SYM_DP_TMP_WORKAROUND 1
67 #include "cpa_types.h"
68 #include "cpa_cy_sym_dp.h"
69 #include "cpa_cy_common.h"
70 #include "cpa_cy_im.h"
71 #include "icp_sal_user.h"
72 #include "icp_sal_poll.h"
77 #define NUM_CRYPTO (7)
80 /* CIPHER KEY LENGTHS */
81 #define KEY_SIZE_64_IN_BYTES (64 / 8)
82 #define KEY_SIZE_56_IN_BYTES (56 / 8)
83 #define KEY_SIZE_128_IN_BYTES (128 / 8)
84 #define KEY_SIZE_168_IN_BYTES (168 / 8)
85 #define KEY_SIZE_192_IN_BYTES (192 / 8)
86 #define KEY_SIZE_256_IN_BYTES (256 / 8)
88 /* HMAC AUTH KEY LENGTHS */
89 #define AES_XCBC_AUTH_KEY_LENGTH_IN_BYTES (128 / 8)
90 #define SHA1_AUTH_KEY_LENGTH_IN_BYTES (160 / 8)
91 #define SHA224_AUTH_KEY_LENGTH_IN_BYTES (224 / 8)
92 #define SHA256_AUTH_KEY_LENGTH_IN_BYTES (256 / 8)
93 #define SHA384_AUTH_KEY_LENGTH_IN_BYTES (384 / 8)
94 #define SHA512_AUTH_KEY_LENGTH_IN_BYTES (512 / 8)
95 #define MD5_AUTH_KEY_LENGTH_IN_BYTES (128 / 8)
97 /* HASH DIGEST LENGHTS */
98 #define AES_XCBC_DIGEST_LENGTH_IN_BYTES (128 / 8)
99 #define AES_XCBC_96_DIGEST_LENGTH_IN_BYTES (96 / 8)
100 #define MD5_DIGEST_LENGTH_IN_BYTES (128 / 8)
101 #define SHA1_DIGEST_LENGTH_IN_BYTES (160 / 8)
102 #define SHA1_96_DIGEST_LENGTH_IN_BYTES (96 / 8)
103 #define SHA224_DIGEST_LENGTH_IN_BYTES (224 / 8)
104 #define SHA256_DIGEST_LENGTH_IN_BYTES (256 / 8)
105 #define SHA384_DIGEST_LENGTH_IN_BYTES (384 / 8)
106 #define SHA512_DIGEST_LENGTH_IN_BYTES (512 / 8)
108 #define IV_LENGTH_16_BYTES (16)
109 #define IV_LENGTH_8_BYTES (8)
113 * rte_memzone is used to allocate physically contiguous virtual memory.
114 * In this application we allocate a single block and divide between variables
115 * which require a virtual to physical mapping for use by the QAT driver.
116 * Virt2phys is only performed during initialisation and not on the data-path.
119 #define LCORE_MEMZONE_SIZE (1 << 22)
123 const struct rte_memzone *memzone;
124 void *next_free_address;
128 * Size the qa software response queue.
129 * Note: Head and Tail are 8 bit, therefore, the queue is
130 * fixed to 256 entries.
132 #define CRYPTO_SOFTWARE_QUEUE_SIZE 256
134 struct qa_callbackQueue {
138 struct rte_mbuf *qaCallbackRing[CRYPTO_SOFTWARE_QUEUE_SIZE];
141 struct qa_core_conf {
142 CpaCySymDpSessionCtx *encryptSessionHandleTbl[NUM_CRYPTO][NUM_HMAC];
143 CpaCySymDpSessionCtx *decryptSessionHandleTbl[NUM_CRYPTO][NUM_HMAC];
144 CpaInstanceHandle instanceHandle;
145 struct qa_callbackQueue callbackQueue;
146 uint64_t qaOutstandingRequests;
147 uint64_t numResponseAttempts;
150 CpaPhysicalAddr packetIVPhy;
151 struct lcore_memzone lcoreMemzone;
152 } __rte_cache_aligned;
154 #define MAX_CORES (RTE_MAX_LCORE)
156 static struct qa_core_conf qaCoreConf[MAX_CORES];
159 *Create maximum possible key size,
160 *One for cipher and one for hash
163 uint8_t cipher_key[32];
164 uint8_t hash_key[64];
168 struct glob_keys g_crypto_hash_keys = {
169 .cipher_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 .hash_key = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
174 0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10,
175 0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,
176 0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,0x20,
177 0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,
178 0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,0x30,
179 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,
180 0x39,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,0x50},
181 .iv = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
182 0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10}
186 * Offsets from the start of the packet.
189 #define PACKET_DATA_START_PHYS(p) \
190 ((p)->buf_physaddr + ((char *)p->pkt.data - (char *)p->buf_addr))
193 * A fixed offset to where the crypto is to be performed, which is the first
194 * byte after the Ethernet(14 bytes) and IPv4 headers(20 bytes)
196 #define CRYPTO_START_OFFSET (14+20)
197 #define HASH_START_OFFSET (14+20)
198 #define CIPHER_BLOCK_DEFAULT_SIZE (16)
199 #define HASH_BLOCK_DEFAULT_SIZE (16)
202 * Offset to the opdata from the start of the data portion of packet.
203 * Assumption: The buffer is physically contiguous.
204 * +18 takes this to the next cache line.
207 #define CRYPTO_OFFSET_TO_OPDATA (ETHER_MAX_LEN+18)
210 * Default number of requests to place on the hardware ring before kicking the
213 #define CRYPTO_BURST_TX (16)
216 * Only call the qa poll function when the number responses in the software
217 * queue drops below this number.
219 #define CRYPTO_QUEUED_RESP_POLL_THRESHOLD (32)
222 * Limit the number of polls per call to get_next_response.
224 #define GET_NEXT_RESPONSE_FREQ (32)
227 * Max number of responses to pull from the qa in one poll.
229 #define CRYPTO_MAX_RESPONSE_QUOTA \
230 (CRYPTO_SOFTWARE_QUEUE_SIZE-CRYPTO_QUEUED_RESP_POLL_THRESHOLD-1)
232 #if (CRYPTO_QUEUED_RESP_POLL_THRESHOLD + CRYPTO_MAX_RESPONSE_QUOTA >= \
233 CRYPTO_SOFTWARE_QUEUE_SIZE)
234 #error Its possible to overflow the qa response Q with current poll and \
239 crypto_callback(CpaCySymDpOpData *pOpData,
240 __rte_unused CpaStatus status,
241 __rte_unused CpaBoolean verifyResult)
244 lcore_id = rte_lcore_id();
245 struct qa_callbackQueue *callbackQ = &(qaCoreConf[lcore_id].callbackQueue);
248 * Received a completion from the QA hardware.
249 * Place the response on the return queue.
251 callbackQ->qaCallbackRing[callbackQ->head] = pOpData->pCallbackTag;
253 callbackQ->numEntries++;
254 qaCoreConf[lcore_id].qaOutstandingRequests--;
258 qa_crypto_callback(CpaCySymDpOpData *pOpData, CpaStatus status,
259 CpaBoolean verifyResult)
261 crypto_callback(pOpData, status, verifyResult);
265 * Each allocation from a particular memzone lasts for the life-time of
266 * the application. No freeing of previous allocations will occur.
269 alloc_memzone_region(uint32_t length, uint32_t lcore_id)
271 char *current_free_addr_ptr = NULL;
272 struct lcore_memzone *lcore_memzone = &(qaCoreConf[lcore_id].lcoreMemzone);
274 current_free_addr_ptr = lcore_memzone->next_free_address;
276 if (current_free_addr_ptr + length >=
277 (char *)lcore_memzone->memzone->addr + lcore_memzone->memzone->len) {
278 printf("Crypto: No memory available in memzone\n");
281 lcore_memzone->next_free_address = current_free_addr_ptr + length;
283 return (void *)current_free_addr_ptr;
287 * Virtual to Physical Address translation is only executed during initialization
288 * and not on the data-path.
290 static CpaPhysicalAddr
293 const struct rte_memzone *memzone = NULL;
294 uint32_t lcore_id = 0;
295 RTE_LCORE_FOREACH(lcore_id) {
296 memzone = qaCoreConf[lcore_id].lcoreMemzone.memzone;
298 if ((char*) ptr >= (char *) memzone->addr &&
299 (char*) ptr < ((char*) memzone->addr + memzone->len)) {
300 return (CpaPhysicalAddr)
301 (memzone->phys_addr + ((char *) ptr - (char*) memzone->addr));
304 printf("Crypto: Corresponding physical address not found in memzone\n");
305 return (CpaPhysicalAddr) 0;
309 getCoreAffinity(Cpa32U *coreAffinity, const CpaInstanceHandle instanceHandle)
311 CpaInstanceInfo2 info;
313 CpaStatus status = CPA_STATUS_SUCCESS;
315 bzero(&info, sizeof(CpaInstanceInfo2));
317 status = cpaCyInstanceGetInfo2(instanceHandle, &info);
318 if (CPA_STATUS_SUCCESS != status) {
319 printf("Crypto: Error getting instance info\n");
320 return CPA_STATUS_FAIL;
322 for (i = 0; i < MAX_CORES; i++) {
323 if (CPA_BITMAP_BIT_TEST(info.coreAffinity, i)) {
325 return CPA_STATUS_SUCCESS;
328 return CPA_STATUS_FAIL;
332 get_crypto_instance_on_core(CpaInstanceHandle *pInstanceHandle,
335 Cpa16U numInstances = 0, i = 0;
336 CpaStatus status = CPA_STATUS_FAIL;
337 CpaInstanceHandle *pLocalInstanceHandles = NULL;
338 Cpa32U coreAffinity = 0;
340 status = cpaCyGetNumInstances(&numInstances);
341 if (CPA_STATUS_SUCCESS != status || numInstances == 0) {
342 return CPA_STATUS_FAIL;
345 pLocalInstanceHandles = rte_malloc("pLocalInstanceHandles",
346 sizeof(CpaInstanceHandle) * numInstances, CACHE_LINE_SIZE);
348 if (NULL == pLocalInstanceHandles) {
349 return CPA_STATUS_FAIL;
351 status = cpaCyGetInstances(numInstances, pLocalInstanceHandles);
352 if (CPA_STATUS_SUCCESS != status) {
353 printf("Crypto: cpaCyGetInstances failed with status: %"PRId32"\n", status);
354 rte_free((void *) pLocalInstanceHandles);
355 return CPA_STATUS_FAIL;
358 for (i = 0; i < numInstances; i++) {
359 status = getCoreAffinity(&coreAffinity, pLocalInstanceHandles[i]);
360 if (CPA_STATUS_SUCCESS != status) {
361 rte_free((void *) pLocalInstanceHandles);
362 return CPA_STATUS_FAIL;
364 if (coreAffinity == lcore_id) {
365 printf("Crypto: instance found on core %d\n", i);
366 *pInstanceHandle = pLocalInstanceHandles[i];
367 return CPA_STATUS_SUCCESS;
370 /* core affinity not found */
371 rte_free((void *) pLocalInstanceHandles);
372 return CPA_STATUS_FAIL;
376 initCySymSession(const int pkt_cipher_alg,
377 const int pkt_hash_alg, const CpaCySymHashMode hashMode,
378 const CpaCySymCipherDirection crypto_direction,
379 CpaCySymSessionCtx **ppSessionCtx,
380 const CpaInstanceHandle cyInstanceHandle,
381 const uint32_t lcore_id)
383 Cpa32U sessionCtxSizeInBytes = 0;
384 CpaStatus status = CPA_STATUS_FAIL;
385 CpaBoolean isCrypto = CPA_TRUE, isHmac = CPA_TRUE;
386 CpaCySymSessionSetupData sessionSetupData;
388 bzero(&sessionSetupData, sizeof(CpaCySymSessionSetupData));
390 /* Assumption: key length is set to each algorithm's max length */
391 switch (pkt_cipher_alg) {
393 isCrypto = CPA_FALSE;
396 sessionSetupData.cipherSetupData.cipherAlgorithm =
397 CPA_CY_SYM_CIPHER_DES_ECB;
398 sessionSetupData.cipherSetupData.cipherKeyLenInBytes =
399 KEY_SIZE_64_IN_BYTES;
402 sessionSetupData.cipherSetupData.cipherAlgorithm =
403 CPA_CY_SYM_CIPHER_DES_CBC;
404 sessionSetupData.cipherSetupData.cipherKeyLenInBytes =
405 KEY_SIZE_64_IN_BYTES;
408 sessionSetupData.cipherSetupData.cipherAlgorithm =
409 CPA_CY_SYM_CIPHER_3DES_ECB;
410 sessionSetupData.cipherSetupData.cipherKeyLenInBytes =
411 KEY_SIZE_192_IN_BYTES;
413 case CIPHER_DES3_CBC:
414 sessionSetupData.cipherSetupData.cipherAlgorithm =
415 CPA_CY_SYM_CIPHER_3DES_CBC;
416 sessionSetupData.cipherSetupData.cipherKeyLenInBytes =
417 KEY_SIZE_192_IN_BYTES;
420 sessionSetupData.cipherSetupData.cipherAlgorithm =
421 CPA_CY_SYM_CIPHER_AES_ECB;
422 sessionSetupData.cipherSetupData.cipherKeyLenInBytes =
423 KEY_SIZE_128_IN_BYTES;
425 case CIPHER_AES_CBC_128:
426 sessionSetupData.cipherSetupData.cipherAlgorithm =
427 CPA_CY_SYM_CIPHER_AES_CBC;
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 printf("Crypto: Undefined Hash specified\n");
498 sessionSetupData.hashSetupData.hashMode = hashMode;
499 sessionSetupData.symOperation = CPA_CY_SYM_OP_HASH;
500 /* If using authenticated hash setup key lengths */
501 if (CPA_CY_SYM_HASH_MODE_AUTH == hashMode) {
502 /* Use a common max length key */
503 sessionSetupData.hashSetupData.authModeSetupData.authKey =
504 g_crypto_hash_keys.hash_key;
505 switch (pkt_hash_alg) {
507 case HASH_AES_XCBC_96:
508 sessionSetupData.hashSetupData.authModeSetupData.authKeyLenInBytes =
509 AES_XCBC_AUTH_KEY_LENGTH_IN_BYTES;
512 sessionSetupData.hashSetupData.authModeSetupData.authKeyLenInBytes =
513 SHA1_AUTH_KEY_LENGTH_IN_BYTES;
517 sessionSetupData.hashSetupData.authModeSetupData.authKeyLenInBytes =
518 SHA1_AUTH_KEY_LENGTH_IN_BYTES;
521 sessionSetupData.hashSetupData.authModeSetupData.authKeyLenInBytes =
522 SHA224_AUTH_KEY_LENGTH_IN_BYTES;
525 sessionSetupData.hashSetupData.authModeSetupData.authKeyLenInBytes =
526 SHA256_AUTH_KEY_LENGTH_IN_BYTES;
529 sessionSetupData.hashSetupData.authModeSetupData.authKeyLenInBytes =
530 SHA384_AUTH_KEY_LENGTH_IN_BYTES;
533 sessionSetupData.hashSetupData.authModeSetupData.authKeyLenInBytes =
534 SHA512_AUTH_KEY_LENGTH_IN_BYTES;
537 printf("Crypto: Undefined Hash specified\n");
538 return CPA_STATUS_FAIL;
543 /* Only high priority supported */
544 sessionSetupData.sessionPriority = CPA_CY_PRIORITY_HIGH;
546 /* If chaining algorithms */
547 if (isCrypto && isHmac) {
548 sessionSetupData.symOperation = CPA_CY_SYM_OP_ALGORITHM_CHAINING;
549 /* @assumption Alg Chain order is cipher then hash for encrypt
550 * and hash then cipher then has for decrypt*/
551 if (CPA_CY_SYM_CIPHER_DIRECTION_ENCRYPT == crypto_direction) {
552 sessionSetupData.algChainOrder =
553 CPA_CY_SYM_ALG_CHAIN_ORDER_CIPHER_THEN_HASH;
555 sessionSetupData.algChainOrder =
556 CPA_CY_SYM_ALG_CHAIN_ORDER_HASH_THEN_CIPHER;
559 if (!isCrypto && !isHmac) {
560 *ppSessionCtx = NULL;
561 return CPA_STATUS_SUCCESS;
564 /* Get the session context size based on the crypto and/or hash operations*/
565 status = cpaCySymDpSessionCtxGetSize(cyInstanceHandle, &sessionSetupData,
566 &sessionCtxSizeInBytes);
567 if (CPA_STATUS_SUCCESS != status) {
568 printf("Crypto: cpaCySymDpSessionCtxGetSize error, status: %"PRId32"\n",
570 return CPA_STATUS_FAIL;
573 *ppSessionCtx = alloc_memzone_region(sessionCtxSizeInBytes, lcore_id);
574 if (NULL == *ppSessionCtx) {
575 printf("Crypto: Failed to allocate memory for Session Context\n");
576 return CPA_STATUS_FAIL;
579 status = cpaCySymDpInitSession(cyInstanceHandle, &sessionSetupData,
580 CPA_TRUE,CPA_FALSE, *ppSessionCtx);
581 if (CPA_STATUS_SUCCESS != status) {
582 printf("Crypto: cpaCySymDpInitSession failed with status %"PRId32"\n", status);
583 return CPA_STATUS_FAIL;
585 return CPA_STATUS_SUCCESS;
589 initSessionDataTables(struct qa_core_conf *qaCoreConf,uint32_t lcore_id)
592 CpaStatus status = CPA_STATUS_FAIL;
593 for (i = 0; i < NUM_CRYPTO; i++) {
594 for (j = 0; j < NUM_HMAC; j++) {
595 status = initCySymSession(i, j, CPA_CY_SYM_HASH_MODE_AUTH,
596 CPA_CY_SYM_CIPHER_DIRECTION_ENCRYPT,
597 &qaCoreConf->encryptSessionHandleTbl[i][j],
598 qaCoreConf->instanceHandle,
600 if (CPA_STATUS_SUCCESS != status) {
601 printf("Crypto: Failed to initialize Encrypt sessions\n");
602 return CPA_STATUS_FAIL;
604 status = initCySymSession(i, j, CPA_CY_SYM_HASH_MODE_AUTH,
605 CPA_CY_SYM_CIPHER_DIRECTION_DECRYPT,
606 &qaCoreConf->decryptSessionHandleTbl[i][j],
607 qaCoreConf->instanceHandle,
609 if (CPA_STATUS_SUCCESS != status) {
610 printf("Crypto: Failed to initialize Decrypt sessions\n");
611 return CPA_STATUS_FAIL;
615 return CPA_STATUS_SUCCESS;
621 if (CPA_STATUS_SUCCESS != icp_sal_userStart("SSL")) {
622 printf("Crypto: Could not start sal for user space\n");
623 return CPA_STATUS_FAIL;
625 printf("Crypto: icp_sal_userStart(\"SSL\")\n");
630 * Per core initialisation
633 per_core_crypto_init(uint32_t lcore_id)
635 CpaStatus status = CPA_STATUS_FAIL;
636 char memzone_name[RTE_MEMZONE_NAMESIZE];
638 int socketID = rte_lcore_to_socket_id(lcore_id);
640 /* Allocate software ring for response messages. */
642 qaCoreConf[lcore_id].callbackQueue.head = 0;
643 qaCoreConf[lcore_id].callbackQueue.tail = 0;
644 qaCoreConf[lcore_id].callbackQueue.numEntries = 0;
645 qaCoreConf[lcore_id].kickFreq = 0;
646 qaCoreConf[lcore_id].qaOutstandingRequests = 0;
647 qaCoreConf[lcore_id].numResponseAttempts = 0;
649 /* Initialise and reserve lcore memzone for virt2phys translation */
650 rte_snprintf(memzone_name,
651 RTE_MEMZONE_NAMESIZE,
655 qaCoreConf[lcore_id].lcoreMemzone.memzone = rte_memzone_reserve(
660 if (NULL == qaCoreConf[lcore_id].lcoreMemzone.memzone) {
661 printf("Crypto: Error allocating memzone on lcore %u\n",lcore_id);
664 qaCoreConf[lcore_id].lcoreMemzone.next_free_address =
665 qaCoreConf[lcore_id].lcoreMemzone.memzone->addr;
667 qaCoreConf[lcore_id].pPacketIV = alloc_memzone_region(IV_LENGTH_16_BYTES,
670 if (NULL == qaCoreConf[lcore_id].pPacketIV ) {
671 printf("Crypto: Failed to allocate memory for Initialization Vector\n");
675 memcpy(qaCoreConf[lcore_id].pPacketIV, &g_crypto_hash_keys.iv,
678 qaCoreConf[lcore_id].packetIVPhy = qa_v2p(qaCoreConf[lcore_id].pPacketIV);
679 if (0 == qaCoreConf[lcore_id].packetIVPhy) {
680 printf("Crypto: Invalid physical address for Initialization Vector\n");
685 * Obtain the instance handle that is mapped to the current lcore.
686 * This can fail if an instance is not mapped to a bank which has been
687 * affinitized to the current lcore.
689 status = get_crypto_instance_on_core(&(qaCoreConf[lcore_id].instanceHandle),
691 if (CPA_STATUS_SUCCESS != status) {
692 printf("Crypto: get_crypto_instance_on_core failed with status: %"PRId32"\n",
697 status = cpaCySymDpRegCbFunc(qaCoreConf[lcore_id].instanceHandle,
698 (CpaCySymDpCbFunc) qa_crypto_callback);
699 if (CPA_STATUS_SUCCESS != status) {
700 printf("Crypto: cpaCySymDpRegCbFunc failed with status: %"PRId32"\n", status);
705 * Set the address translation callback for virtual to physcial address
706 * mapping. This will be called by the QAT driver during initialisation only.
708 status = cpaCySetAddressTranslation(qaCoreConf[lcore_id].instanceHandle,
709 (CpaVirtualToPhysical) qa_v2p);
710 if (CPA_STATUS_SUCCESS != status) {
711 printf("Crypto: cpaCySetAddressTranslation failed with status: %"PRId32"\n",
716 status = initSessionDataTables(&qaCoreConf[lcore_id],lcore_id);
717 if (CPA_STATUS_SUCCESS != status) {
718 printf("Crypto: Failed to allocate all session tables.");
725 enqueueOp(CpaCySymDpOpData *opData, uint32_t lcore_id)
731 * Assumption is there is no requirement to do load balancing between
732 * acceleration units - that is one acceleration unit is tied to a core.
734 opData->instanceHandle = qaCoreConf[lcore_id].instanceHandle;
736 if ((++qaCoreConf[lcore_id].kickFreq) % CRYPTO_BURST_TX == 0) {
737 status = cpaCySymDpEnqueueOp(opData, CPA_TRUE);
739 status = cpaCySymDpEnqueueOp(opData, CPA_FALSE);
742 qaCoreConf[lcore_id].qaOutstandingRequests++;
748 crypto_flush_tx_queue(uint32_t lcore_id)
751 cpaCySymDpPerformOpNow(qaCoreConf[lcore_id].instanceHandle);
755 crypto_encrypt(struct rte_mbuf *rte_buff, enum cipher_alg c, enum hash_alg h)
757 CpaCySymDpOpData *opData =
758 (CpaCySymDpOpData *) ((char *) (rte_buff->pkt.data)
759 + CRYPTO_OFFSET_TO_OPDATA);
762 lcore_id = rte_lcore_id();
764 bzero(opData, sizeof(CpaCySymDpOpData));
766 opData->srcBuffer = opData->dstBuffer = PACKET_DATA_START_PHYS(rte_buff);
767 opData->srcBufferLen = opData->dstBufferLen = rte_buff->pkt.data_len;
768 opData->sessionCtx = qaCoreConf[lcore_id].encryptSessionHandleTbl[c][h];
769 opData->thisPhys = PACKET_DATA_START_PHYS(rte_buff)
770 + CRYPTO_OFFSET_TO_OPDATA;
771 opData->pCallbackTag = rte_buff;
773 /* if no crypto or hash operations are specified return fail */
774 if (NO_CIPHER == c && NO_HASH == h)
775 return CRYPTO_RESULT_FAIL;
777 if (NO_CIPHER != c) {
778 opData->pIv = qaCoreConf[lcore_id].pPacketIV;
779 opData->iv = qaCoreConf[lcore_id].packetIVPhy;
781 if (CIPHER_AES_CBC_128 == c)
782 opData->ivLenInBytes = IV_LENGTH_16_BYTES;
784 opData->ivLenInBytes = IV_LENGTH_8_BYTES;
786 opData->cryptoStartSrcOffsetInBytes = CRYPTO_START_OFFSET;
787 opData->messageLenToCipherInBytes = rte_buff->pkt.data_len
788 - CRYPTO_START_OFFSET;
790 * Work around for padding, message length has to be a multiple of
793 opData->messageLenToCipherInBytes -= opData->messageLenToCipherInBytes
794 % CIPHER_BLOCK_DEFAULT_SIZE;
799 opData->hashStartSrcOffsetInBytes = HASH_START_OFFSET;
800 opData->messageLenToHashInBytes = rte_buff->pkt.data_len
803 * Work around for padding, message length has to be a multiple of block
806 opData->messageLenToHashInBytes -= opData->messageLenToHashInBytes
807 % HASH_BLOCK_DEFAULT_SIZE;
810 * Assumption: Ok ignore the passed digest pointer and place HMAC at end
813 opData->digestResult = rte_buff->buf_physaddr + rte_buff->pkt.data_len;
816 if (CPA_STATUS_SUCCESS != enqueueOp(opData, lcore_id)) {
818 * Failed to place a packet on the hardware queue.
819 * Most likely because the QA hardware is busy.
821 return CRYPTO_RESULT_FAIL;
823 return CRYPTO_RESULT_IN_PROGRESS;
827 crypto_decrypt(struct rte_mbuf *rte_buff, enum cipher_alg c, enum hash_alg h)
830 CpaCySymDpOpData *opData = (void*) (((char *) rte_buff->pkt.data)
831 + CRYPTO_OFFSET_TO_OPDATA);
834 lcore_id = rte_lcore_id();
836 bzero(opData, sizeof(CpaCySymDpOpData));
838 opData->dstBuffer = opData->srcBuffer = PACKET_DATA_START_PHYS(rte_buff);
839 opData->dstBufferLen = opData->srcBufferLen = rte_buff->pkt.data_len;
840 opData->thisPhys = PACKET_DATA_START_PHYS(rte_buff)
841 + CRYPTO_OFFSET_TO_OPDATA;
842 opData->sessionCtx = qaCoreConf[lcore_id].decryptSessionHandleTbl[c][h];
843 opData->pCallbackTag = rte_buff;
845 /* if no crypto or hmac operations are specified return fail */
846 if (NO_CIPHER == c && NO_HASH == h)
847 return CRYPTO_RESULT_FAIL;
849 if (NO_CIPHER != c) {
850 opData->pIv = qaCoreConf[lcore_id].pPacketIV;
851 opData->iv = qaCoreConf[lcore_id].packetIVPhy;
853 if (CIPHER_AES_CBC_128 == c)
854 opData->ivLenInBytes = IV_LENGTH_16_BYTES;
856 opData->ivLenInBytes = IV_LENGTH_8_BYTES;
858 opData->cryptoStartSrcOffsetInBytes = CRYPTO_START_OFFSET;
859 opData->messageLenToCipherInBytes = rte_buff->pkt.data_len
860 - CRYPTO_START_OFFSET;
863 * Work around for padding, message length has to be a multiple of block
866 opData->messageLenToCipherInBytes -= opData->messageLenToCipherInBytes
867 % CIPHER_BLOCK_DEFAULT_SIZE;
870 opData->hashStartSrcOffsetInBytes = HASH_START_OFFSET;
871 opData->messageLenToHashInBytes = rte_buff->pkt.data_len
874 * Work around for padding, message length has to be a multiple of block
877 opData->messageLenToHashInBytes -= opData->messageLenToHashInBytes
878 % HASH_BLOCK_DEFAULT_SIZE;
879 opData->digestResult = rte_buff->buf_physaddr + rte_buff->pkt.data_len;
882 if (CPA_STATUS_SUCCESS != enqueueOp(opData, lcore_id)) {
884 * Failed to place a packet on the hardware queue.
885 * Most likely because the QA hardware is busy.
887 return CRYPTO_RESULT_FAIL;
889 return CRYPTO_RESULT_IN_PROGRESS;
893 crypto_get_next_response(void)
896 lcore_id = rte_lcore_id();
897 struct qa_callbackQueue *callbackQ = &(qaCoreConf[lcore_id].callbackQueue);
900 if (callbackQ->numEntries) {
901 entry = callbackQ->qaCallbackRing[callbackQ->tail];
903 callbackQ->numEntries--;
906 /* If there are no outstanding requests no need to poll, return entry */
907 if (qaCoreConf[lcore_id].qaOutstandingRequests == 0)
910 if (callbackQ->numEntries < CRYPTO_QUEUED_RESP_POLL_THRESHOLD
911 && qaCoreConf[lcore_id].numResponseAttempts++
912 % GET_NEXT_RESPONSE_FREQ == 0) {
914 * Only poll the hardware when there is less than
915 * CRYPTO_QUEUED_RESP_POLL_THRESHOLD elements in the software queue
917 icp_sal_CyPollDpInstance(qaCoreConf[lcore_id].instanceHandle,
918 CRYPTO_MAX_RESPONSE_QUOTA);