1 /* SPDX-License-Identifier: BSD-3-Clause */ 2 /* Copyright(c) 2007-2022 Intel Corporation */ 3 /* $FreeBSD$ */ 4 /* System headers */ 5 #include <sys/param.h> 6 #include <sys/systm.h> 7 #include <sys/bus.h> 8 #include <sys/kernel.h> 9 #include <sys/mutex.h> 10 #include <sys/timespec.h> 11 12 /* QAT specific headers */ 13 #include "qat_ocf_utils.h" 14 #include "cpa.h" 15 #include "lac_common.h" 16 #include "lac_log.h" 17 #include "lac_mem.h" 18 #include "lac_mem_pools.h" 19 #include "lac_list.h" 20 #include "lac_sym.h" 21 #include "lac_sym_qat.h" 22 #include "lac_sal.h" 23 #include "lac_sal_ctrl.h" 24 #include "lac_session.h" 25 #include "lac_sym_cipher.h" 26 #include "lac_sym_hash.h" 27 #include "lac_sym_alg_chain.h" 28 #include "lac_sym_stats.h" 29 #include "lac_sym_partial.h" 30 #include "lac_sym_qat_hash_defs_lookup.h" 31 32 #define QAT_OCF_AAD_NOCHANGE (-1) 33 34 CpaStatus 35 qat_ocf_wait_for_session(CpaCySymSessionCtx sessionCtx, Cpa32U timeoutMS) 36 { 37 CpaBoolean sessionInUse = CPA_TRUE; 38 CpaStatus status; 39 struct timespec start_ts; 40 struct timespec current_ts; 41 struct timespec delta; 42 u64 delta_ms; 43 44 nanotime(&start_ts); 45 for (;;) { 46 status = cpaCySymSessionInUse(sessionCtx, &sessionInUse); 47 if (CPA_STATUS_SUCCESS != status) 48 return CPA_STATUS_FAIL; 49 if (CPA_FALSE == sessionInUse) 50 break; 51 nanotime(¤t_ts); 52 delta = timespec_sub(current_ts, start_ts); 53 delta_ms = (delta.tv_sec * 1000) + 54 (delta.tv_nsec / NSEC_PER_MSEC); 55 if (delta_ms > (timeoutMS)) 56 return CPA_STATUS_RESOURCE; 57 qatUtilsYield(); 58 } 59 60 return CPA_STATUS_SUCCESS; 61 } 62 63 static CpaStatus 64 qat_ocf_session_update(struct qat_ocf_session *ocf_session, 65 Cpa8U *newCipher, 66 Cpa8U *newAuth, 67 Cpa32U newAADLength) 68 { 69 lac_session_desc_t *pSessionDesc = NULL; 70 CpaStatus status = CPA_STATUS_SUCCESS; 71 CpaBoolean sessionInUse = CPA_TRUE; 72 73 if (!ocf_session->sessionCtx) 74 return CPA_STATUS_SUCCESS; 75 76 status = cpaCySymSessionInUse(ocf_session->sessionCtx, &sessionInUse); 77 if (CPA_TRUE == sessionInUse) 78 return CPA_STATUS_RESOURCE; 79 80 pSessionDesc = 81 LAC_SYM_SESSION_DESC_FROM_CTX_GET(ocf_session->sessionCtx); 82 83 if (newAADLength != QAT_OCF_AAD_NOCHANGE) { 84 ocf_session->aadLen = newAADLength; 85 status = 86 LacAlgChain_SessionAADUpdate(pSessionDesc, newAADLength); 87 if (CPA_STATUS_SUCCESS != status) 88 return status; 89 } 90 91 if (newCipher) { 92 status = 93 LacAlgChain_SessionCipherKeyUpdate(pSessionDesc, newCipher); 94 if (CPA_STATUS_SUCCESS != status) 95 return status; 96 } 97 98 if (newAuth) { 99 status = 100 LacAlgChain_SessionAuthKeyUpdate(pSessionDesc, newAuth); 101 if (CPA_STATUS_SUCCESS != status) 102 return status; 103 } 104 105 return status; 106 } 107 108 CpaStatus 109 qat_ocf_handle_session_update(struct qat_ocf_dsession *ocf_dsession, 110 struct cryptop *crp) 111 { 112 Cpa32U newAADLength = QAT_OCF_AAD_NOCHANGE; 113 Cpa8U *cipherKey; 114 Cpa8U *authKey; 115 crypto_session_t cses; 116 const struct crypto_session_params *csp; 117 CpaStatus status = CPA_STATUS_SUCCESS; 118 119 if (!ocf_dsession) 120 return CPA_STATUS_FAIL; 121 122 cses = crp->crp_session; 123 if (!cses) 124 return CPA_STATUS_FAIL; 125 csp = crypto_get_params(cses); 126 if (!csp) 127 return CPA_STATUS_FAIL; 128 129 cipherKey = crp->crp_cipher_key; 130 authKey = crp->crp_auth_key; 131 132 if (is_sep_aad_supported(csp)) { 133 /* Determine if AAD has change */ 134 if ((ocf_dsession->encSession.sessionCtx && 135 ocf_dsession->encSession.aadLen != crp->crp_aad_length) || 136 (ocf_dsession->decSession.sessionCtx && 137 ocf_dsession->decSession.aadLen != crp->crp_aad_length)) { 138 newAADLength = crp->crp_aad_length; 139 140 /* Get auth and cipher keys from session if not present 141 * in the request. Update keys is required to update 142 * AAD. 143 */ 144 if (!authKey) 145 authKey = csp->csp_auth_key; 146 if (!cipherKey) 147 cipherKey = csp->csp_cipher_key; 148 } 149 if (!authKey) 150 authKey = cipherKey; 151 } 152 153 if (crp->crp_cipher_key || crp->crp_auth_key || 154 newAADLength != QAT_OCF_AAD_NOCHANGE) { 155 /* Update encryption session */ 156 status = qat_ocf_session_update(&ocf_dsession->encSession, 157 cipherKey, 158 authKey, 159 newAADLength); 160 if (CPA_STATUS_SUCCESS != status) 161 return status; 162 /* Update decryption session */ 163 status = qat_ocf_session_update(&ocf_dsession->decSession, 164 cipherKey, 165 authKey, 166 newAADLength); 167 if (CPA_STATUS_SUCCESS != status) 168 return status; 169 } 170 171 return status; 172 } 173