common.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2012-2014, The Linux Foundation. All rights reserved.
  4. */
  5. #include <crypto/internal/hash.h>
  6. #include <linux/err.h>
  7. #include <linux/interrupt.h>
  8. #include <linux/types.h>
  9. #include <crypto/scatterwalk.h>
  10. #include <crypto/sha1.h>
  11. #include <crypto/sha2.h>
  12. #include "cipher.h"
  13. #include "common.h"
  14. #include "core.h"
  15. #include "regs-v5.h"
  16. #include "sha.h"
  17. #include "aead.h"
  18. static inline u32 qce_read(struct qce_device *qce, u32 offset)
  19. {
  20. return readl(qce->base + offset);
  21. }
  22. static inline void qce_write(struct qce_device *qce, u32 offset, u32 val)
  23. {
  24. writel(val, qce->base + offset);
  25. }
  26. static inline void qce_write_array(struct qce_device *qce, u32 offset,
  27. const u32 *val, unsigned int len)
  28. {
  29. int i;
  30. for (i = 0; i < len; i++)
  31. qce_write(qce, offset + i * sizeof(u32), val[i]);
  32. }
  33. static inline void
  34. qce_clear_array(struct qce_device *qce, u32 offset, unsigned int len)
  35. {
  36. int i;
  37. for (i = 0; i < len; i++)
  38. qce_write(qce, offset + i * sizeof(u32), 0);
  39. }
  40. static u32 qce_config_reg(struct qce_device *qce, int little)
  41. {
  42. u32 beats = (qce->burst_size >> 3) - 1;
  43. u32 pipe_pair = qce->pipe_pair_id;
  44. u32 config;
  45. config = (beats << REQ_SIZE_SHIFT) & REQ_SIZE_MASK;
  46. config |= BIT(MASK_DOUT_INTR_SHIFT) | BIT(MASK_DIN_INTR_SHIFT) |
  47. BIT(MASK_OP_DONE_INTR_SHIFT) | BIT(MASK_ERR_INTR_SHIFT);
  48. config |= (pipe_pair << PIPE_SET_SELECT_SHIFT) & PIPE_SET_SELECT_MASK;
  49. config &= ~HIGH_SPD_EN_N_SHIFT;
  50. if (little)
  51. config |= BIT(LITTLE_ENDIAN_MODE_SHIFT);
  52. return config;
  53. }
  54. void qce_cpu_to_be32p_array(__be32 *dst, const u8 *src, unsigned int len)
  55. {
  56. __be32 *d = dst;
  57. const u8 *s = src;
  58. unsigned int n;
  59. n = len / sizeof(u32);
  60. for (; n > 0; n--) {
  61. *d = cpu_to_be32p((const __u32 *) s);
  62. s += sizeof(__u32);
  63. d++;
  64. }
  65. }
  66. static void qce_setup_config(struct qce_device *qce)
  67. {
  68. u32 config;
  69. /* get big endianness */
  70. config = qce_config_reg(qce, 0);
  71. /* clear status */
  72. qce_write(qce, REG_STATUS, 0);
  73. qce_write(qce, REG_CONFIG, config);
  74. }
  75. static inline void qce_crypto_go(struct qce_device *qce, bool result_dump)
  76. {
  77. if (result_dump)
  78. qce_write(qce, REG_GOPROC, BIT(GO_SHIFT) | BIT(RESULTS_DUMP_SHIFT));
  79. else
  80. qce_write(qce, REG_GOPROC, BIT(GO_SHIFT));
  81. }
  82. #if defined(CONFIG_CRYPTO_DEV_QCE_SHA) || defined(CONFIG_CRYPTO_DEV_QCE_AEAD)
  83. static u32 qce_auth_cfg(unsigned long flags, u32 key_size, u32 auth_size)
  84. {
  85. u32 cfg = 0;
  86. if (IS_CCM(flags) || IS_CMAC(flags))
  87. cfg |= AUTH_ALG_AES << AUTH_ALG_SHIFT;
  88. else
  89. cfg |= AUTH_ALG_SHA << AUTH_ALG_SHIFT;
  90. if (IS_CCM(flags) || IS_CMAC(flags)) {
  91. if (key_size == AES_KEYSIZE_128)
  92. cfg |= AUTH_KEY_SZ_AES128 << AUTH_KEY_SIZE_SHIFT;
  93. else if (key_size == AES_KEYSIZE_256)
  94. cfg |= AUTH_KEY_SZ_AES256 << AUTH_KEY_SIZE_SHIFT;
  95. }
  96. if (IS_SHA1(flags) || IS_SHA1_HMAC(flags))
  97. cfg |= AUTH_SIZE_SHA1 << AUTH_SIZE_SHIFT;
  98. else if (IS_SHA256(flags) || IS_SHA256_HMAC(flags))
  99. cfg |= AUTH_SIZE_SHA256 << AUTH_SIZE_SHIFT;
  100. else if (IS_CMAC(flags))
  101. cfg |= AUTH_SIZE_ENUM_16_BYTES << AUTH_SIZE_SHIFT;
  102. else if (IS_CCM(flags))
  103. cfg |= (auth_size - 1) << AUTH_SIZE_SHIFT;
  104. if (IS_SHA1(flags) || IS_SHA256(flags))
  105. cfg |= AUTH_MODE_HASH << AUTH_MODE_SHIFT;
  106. else if (IS_SHA1_HMAC(flags) || IS_SHA256_HMAC(flags))
  107. cfg |= AUTH_MODE_HMAC << AUTH_MODE_SHIFT;
  108. else if (IS_CCM(flags))
  109. cfg |= AUTH_MODE_CCM << AUTH_MODE_SHIFT;
  110. else if (IS_CMAC(flags))
  111. cfg |= AUTH_MODE_CMAC << AUTH_MODE_SHIFT;
  112. if (IS_SHA(flags) || IS_SHA_HMAC(flags))
  113. cfg |= AUTH_POS_BEFORE << AUTH_POS_SHIFT;
  114. if (IS_CCM(flags))
  115. cfg |= QCE_MAX_NONCE_WORDS << AUTH_NONCE_NUM_WORDS_SHIFT;
  116. return cfg;
  117. }
  118. #endif
  119. #ifdef CONFIG_CRYPTO_DEV_QCE_SHA
  120. static int qce_setup_regs_ahash(struct crypto_async_request *async_req)
  121. {
  122. struct ahash_request *req = ahash_request_cast(async_req);
  123. struct crypto_ahash *ahash = __crypto_ahash_cast(async_req->tfm);
  124. struct qce_sha_reqctx *rctx = ahash_request_ctx_dma(req);
  125. struct qce_alg_template *tmpl = to_ahash_tmpl(async_req->tfm);
  126. struct qce_device *qce = tmpl->qce;
  127. unsigned int digestsize = crypto_ahash_digestsize(ahash);
  128. unsigned int blocksize = crypto_tfm_alg_blocksize(async_req->tfm);
  129. __be32 auth[SHA256_DIGEST_SIZE / sizeof(__be32)] = {0};
  130. __be32 mackey[QCE_SHA_HMAC_KEY_SIZE / sizeof(__be32)] = {0};
  131. u32 auth_cfg = 0, config;
  132. unsigned int iv_words;
  133. /* if not the last, the size has to be on the block boundary */
  134. if (!rctx->last_blk && req->nbytes % blocksize)
  135. return -EINVAL;
  136. qce_setup_config(qce);
  137. if (IS_CMAC(rctx->flags)) {
  138. qce_write(qce, REG_AUTH_SEG_CFG, 0);
  139. qce_write(qce, REG_ENCR_SEG_CFG, 0);
  140. qce_write(qce, REG_ENCR_SEG_SIZE, 0);
  141. qce_clear_array(qce, REG_AUTH_IV0, 16);
  142. qce_clear_array(qce, REG_AUTH_KEY0, 16);
  143. qce_clear_array(qce, REG_AUTH_BYTECNT0, 4);
  144. auth_cfg = qce_auth_cfg(rctx->flags, rctx->authklen, digestsize);
  145. }
  146. if (IS_SHA_HMAC(rctx->flags) || IS_CMAC(rctx->flags)) {
  147. u32 authkey_words = rctx->authklen / sizeof(u32);
  148. qce_cpu_to_be32p_array(mackey, rctx->authkey, rctx->authklen);
  149. qce_write_array(qce, REG_AUTH_KEY0, (u32 *)mackey,
  150. authkey_words);
  151. }
  152. if (IS_CMAC(rctx->flags))
  153. goto go_proc;
  154. if (rctx->first_blk)
  155. memcpy(auth, rctx->digest, digestsize);
  156. else
  157. qce_cpu_to_be32p_array(auth, rctx->digest, digestsize);
  158. iv_words = (IS_SHA1(rctx->flags) || IS_SHA1_HMAC(rctx->flags)) ? 5 : 8;
  159. qce_write_array(qce, REG_AUTH_IV0, (u32 *)auth, iv_words);
  160. if (rctx->first_blk)
  161. qce_clear_array(qce, REG_AUTH_BYTECNT0, 4);
  162. else
  163. qce_write_array(qce, REG_AUTH_BYTECNT0,
  164. (u32 *)rctx->byte_count, 2);
  165. auth_cfg = qce_auth_cfg(rctx->flags, 0, digestsize);
  166. if (rctx->last_blk)
  167. auth_cfg |= BIT(AUTH_LAST_SHIFT);
  168. else
  169. auth_cfg &= ~BIT(AUTH_LAST_SHIFT);
  170. if (rctx->first_blk)
  171. auth_cfg |= BIT(AUTH_FIRST_SHIFT);
  172. else
  173. auth_cfg &= ~BIT(AUTH_FIRST_SHIFT);
  174. go_proc:
  175. qce_write(qce, REG_AUTH_SEG_CFG, auth_cfg);
  176. qce_write(qce, REG_AUTH_SEG_SIZE, req->nbytes);
  177. qce_write(qce, REG_AUTH_SEG_START, 0);
  178. qce_write(qce, REG_ENCR_SEG_CFG, 0);
  179. qce_write(qce, REG_SEG_SIZE, req->nbytes);
  180. /* get little endianness */
  181. config = qce_config_reg(qce, 1);
  182. qce_write(qce, REG_CONFIG, config);
  183. qce_crypto_go(qce, true);
  184. return 0;
  185. }
  186. #endif
  187. #if defined(CONFIG_CRYPTO_DEV_QCE_SKCIPHER) || defined(CONFIG_CRYPTO_DEV_QCE_AEAD)
  188. static u32 qce_encr_cfg(unsigned long flags, u32 aes_key_size)
  189. {
  190. u32 cfg = 0;
  191. if (IS_AES(flags)) {
  192. if (aes_key_size == AES_KEYSIZE_128)
  193. cfg |= ENCR_KEY_SZ_AES128 << ENCR_KEY_SZ_SHIFT;
  194. else if (aes_key_size == AES_KEYSIZE_256)
  195. cfg |= ENCR_KEY_SZ_AES256 << ENCR_KEY_SZ_SHIFT;
  196. }
  197. if (IS_AES(flags))
  198. cfg |= ENCR_ALG_AES << ENCR_ALG_SHIFT;
  199. else if (IS_DES(flags) || IS_3DES(flags))
  200. cfg |= ENCR_ALG_DES << ENCR_ALG_SHIFT;
  201. if (IS_DES(flags))
  202. cfg |= ENCR_KEY_SZ_DES << ENCR_KEY_SZ_SHIFT;
  203. if (IS_3DES(flags))
  204. cfg |= ENCR_KEY_SZ_3DES << ENCR_KEY_SZ_SHIFT;
  205. switch (flags & QCE_MODE_MASK) {
  206. case QCE_MODE_ECB:
  207. cfg |= ENCR_MODE_ECB << ENCR_MODE_SHIFT;
  208. break;
  209. case QCE_MODE_CBC:
  210. cfg |= ENCR_MODE_CBC << ENCR_MODE_SHIFT;
  211. break;
  212. case QCE_MODE_CTR:
  213. cfg |= ENCR_MODE_CTR << ENCR_MODE_SHIFT;
  214. break;
  215. case QCE_MODE_XTS:
  216. cfg |= ENCR_MODE_XTS << ENCR_MODE_SHIFT;
  217. break;
  218. case QCE_MODE_CCM:
  219. cfg |= ENCR_MODE_CCM << ENCR_MODE_SHIFT;
  220. cfg |= LAST_CCM_XFR << LAST_CCM_SHIFT;
  221. break;
  222. default:
  223. return ~0;
  224. }
  225. return cfg;
  226. }
  227. #endif
  228. #ifdef CONFIG_CRYPTO_DEV_QCE_SKCIPHER
  229. static void qce_xts_swapiv(__be32 *dst, const u8 *src, unsigned int ivsize)
  230. {
  231. u8 swap[QCE_AES_IV_LENGTH];
  232. u32 i, j;
  233. if (ivsize > QCE_AES_IV_LENGTH)
  234. return;
  235. memset(swap, 0, QCE_AES_IV_LENGTH);
  236. for (i = (QCE_AES_IV_LENGTH - ivsize), j = ivsize - 1;
  237. i < QCE_AES_IV_LENGTH; i++, j--)
  238. swap[i] = src[j];
  239. qce_cpu_to_be32p_array(dst, swap, QCE_AES_IV_LENGTH);
  240. }
  241. static void qce_xtskey(struct qce_device *qce, const u8 *enckey,
  242. unsigned int enckeylen, unsigned int cryptlen)
  243. {
  244. u32 xtskey[QCE_MAX_CIPHER_KEY_SIZE / sizeof(u32)] = {0};
  245. unsigned int xtsklen = enckeylen / (2 * sizeof(u32));
  246. qce_cpu_to_be32p_array((__be32 *)xtskey, enckey + enckeylen / 2,
  247. enckeylen / 2);
  248. qce_write_array(qce, REG_ENCR_XTS_KEY0, xtskey, xtsklen);
  249. /* Set data unit size to cryptlen. Anything else causes
  250. * crypto engine to return back incorrect results.
  251. */
  252. qce_write(qce, REG_ENCR_XTS_DU_SIZE, cryptlen);
  253. }
  254. static int qce_setup_regs_skcipher(struct crypto_async_request *async_req)
  255. {
  256. struct skcipher_request *req = skcipher_request_cast(async_req);
  257. struct qce_cipher_reqctx *rctx = skcipher_request_ctx(req);
  258. struct qce_cipher_ctx *ctx = crypto_tfm_ctx(async_req->tfm);
  259. struct qce_alg_template *tmpl = to_cipher_tmpl(crypto_skcipher_reqtfm(req));
  260. struct qce_device *qce = tmpl->qce;
  261. __be32 enckey[QCE_MAX_CIPHER_KEY_SIZE / sizeof(__be32)] = {0};
  262. __be32 enciv[QCE_MAX_IV_SIZE / sizeof(__be32)] = {0};
  263. unsigned int enckey_words, enciv_words;
  264. unsigned int keylen;
  265. u32 encr_cfg = 0, auth_cfg = 0, config;
  266. unsigned int ivsize = rctx->ivsize;
  267. unsigned long flags = rctx->flags;
  268. qce_setup_config(qce);
  269. if (IS_XTS(flags))
  270. keylen = ctx->enc_keylen / 2;
  271. else
  272. keylen = ctx->enc_keylen;
  273. qce_cpu_to_be32p_array(enckey, ctx->enc_key, keylen);
  274. enckey_words = keylen / sizeof(u32);
  275. qce_write(qce, REG_AUTH_SEG_CFG, auth_cfg);
  276. encr_cfg = qce_encr_cfg(flags, keylen);
  277. if (IS_DES(flags)) {
  278. enciv_words = 2;
  279. enckey_words = 2;
  280. } else if (IS_3DES(flags)) {
  281. enciv_words = 2;
  282. enckey_words = 6;
  283. } else if (IS_AES(flags)) {
  284. if (IS_XTS(flags))
  285. qce_xtskey(qce, ctx->enc_key, ctx->enc_keylen,
  286. rctx->cryptlen);
  287. enciv_words = 4;
  288. } else {
  289. return -EINVAL;
  290. }
  291. qce_write_array(qce, REG_ENCR_KEY0, (u32 *)enckey, enckey_words);
  292. if (!IS_ECB(flags)) {
  293. if (IS_XTS(flags))
  294. qce_xts_swapiv(enciv, rctx->iv, ivsize);
  295. else
  296. qce_cpu_to_be32p_array(enciv, rctx->iv, ivsize);
  297. qce_write_array(qce, REG_CNTR0_IV0, (u32 *)enciv, enciv_words);
  298. }
  299. if (IS_ENCRYPT(flags))
  300. encr_cfg |= BIT(ENCODE_SHIFT);
  301. qce_write(qce, REG_ENCR_SEG_CFG, encr_cfg);
  302. qce_write(qce, REG_ENCR_SEG_SIZE, rctx->cryptlen);
  303. qce_write(qce, REG_ENCR_SEG_START, 0);
  304. if (IS_CTR(flags)) {
  305. qce_write(qce, REG_CNTR_MASK, ~0);
  306. qce_write(qce, REG_CNTR_MASK0, ~0);
  307. qce_write(qce, REG_CNTR_MASK1, ~0);
  308. qce_write(qce, REG_CNTR_MASK2, ~0);
  309. }
  310. qce_write(qce, REG_SEG_SIZE, rctx->cryptlen);
  311. /* get little endianness */
  312. config = qce_config_reg(qce, 1);
  313. qce_write(qce, REG_CONFIG, config);
  314. qce_crypto_go(qce, true);
  315. return 0;
  316. }
  317. #endif
  318. #ifdef CONFIG_CRYPTO_DEV_QCE_AEAD
  319. static const u32 std_iv_sha1[SHA256_DIGEST_SIZE / sizeof(u32)] = {
  320. SHA1_H0, SHA1_H1, SHA1_H2, SHA1_H3, SHA1_H4, 0, 0, 0
  321. };
  322. static const u32 std_iv_sha256[SHA256_DIGEST_SIZE / sizeof(u32)] = {
  323. SHA256_H0, SHA256_H1, SHA256_H2, SHA256_H3,
  324. SHA256_H4, SHA256_H5, SHA256_H6, SHA256_H7
  325. };
  326. static unsigned int qce_be32_to_cpu_array(u32 *dst, const u8 *src, unsigned int len)
  327. {
  328. u32 *d = dst;
  329. const u8 *s = src;
  330. unsigned int n;
  331. n = len / sizeof(u32);
  332. for (; n > 0; n--) {
  333. *d = be32_to_cpup((const __be32 *)s);
  334. s += sizeof(u32);
  335. d++;
  336. }
  337. return DIV_ROUND_UP(len, sizeof(u32));
  338. }
  339. static int qce_setup_regs_aead(struct crypto_async_request *async_req)
  340. {
  341. struct aead_request *req = aead_request_cast(async_req);
  342. struct qce_aead_reqctx *rctx = aead_request_ctx_dma(req);
  343. struct qce_aead_ctx *ctx = crypto_tfm_ctx(async_req->tfm);
  344. struct qce_alg_template *tmpl = to_aead_tmpl(crypto_aead_reqtfm(req));
  345. struct qce_device *qce = tmpl->qce;
  346. u32 enckey[QCE_MAX_CIPHER_KEY_SIZE / sizeof(u32)] = {0};
  347. u32 enciv[QCE_MAX_IV_SIZE / sizeof(u32)] = {0};
  348. u32 authkey[QCE_SHA_HMAC_KEY_SIZE / sizeof(u32)] = {0};
  349. u32 authiv[SHA256_DIGEST_SIZE / sizeof(u32)] = {0};
  350. u32 authnonce[QCE_MAX_NONCE / sizeof(u32)] = {0};
  351. unsigned int enc_keylen = ctx->enc_keylen;
  352. unsigned int auth_keylen = ctx->auth_keylen;
  353. unsigned int enc_ivsize = rctx->ivsize;
  354. unsigned int auth_ivsize = 0;
  355. unsigned int enckey_words, enciv_words;
  356. unsigned int authkey_words, authiv_words, authnonce_words;
  357. unsigned long flags = rctx->flags;
  358. u32 encr_cfg, auth_cfg, config, totallen;
  359. u32 iv_last_word;
  360. qce_setup_config(qce);
  361. /* Write encryption key */
  362. enckey_words = qce_be32_to_cpu_array(enckey, ctx->enc_key, enc_keylen);
  363. qce_write_array(qce, REG_ENCR_KEY0, enckey, enckey_words);
  364. /* Write encryption iv */
  365. enciv_words = qce_be32_to_cpu_array(enciv, rctx->iv, enc_ivsize);
  366. qce_write_array(qce, REG_CNTR0_IV0, enciv, enciv_words);
  367. if (IS_CCM(rctx->flags)) {
  368. iv_last_word = enciv[enciv_words - 1];
  369. qce_write(qce, REG_CNTR3_IV3, iv_last_word + 1);
  370. qce_write_array(qce, REG_ENCR_CCM_INT_CNTR0, (u32 *)enciv, enciv_words);
  371. qce_write(qce, REG_CNTR_MASK, ~0);
  372. qce_write(qce, REG_CNTR_MASK0, ~0);
  373. qce_write(qce, REG_CNTR_MASK1, ~0);
  374. qce_write(qce, REG_CNTR_MASK2, ~0);
  375. }
  376. /* Clear authentication IV and KEY registers of previous values */
  377. qce_clear_array(qce, REG_AUTH_IV0, 16);
  378. qce_clear_array(qce, REG_AUTH_KEY0, 16);
  379. /* Clear byte count */
  380. qce_clear_array(qce, REG_AUTH_BYTECNT0, 4);
  381. /* Write authentication key */
  382. authkey_words = qce_be32_to_cpu_array(authkey, ctx->auth_key, auth_keylen);
  383. qce_write_array(qce, REG_AUTH_KEY0, (u32 *)authkey, authkey_words);
  384. /* Write initial authentication IV only for HMAC algorithms */
  385. if (IS_SHA_HMAC(rctx->flags)) {
  386. /* Write default authentication iv */
  387. if (IS_SHA1_HMAC(rctx->flags)) {
  388. auth_ivsize = SHA1_DIGEST_SIZE;
  389. memcpy(authiv, std_iv_sha1, auth_ivsize);
  390. } else if (IS_SHA256_HMAC(rctx->flags)) {
  391. auth_ivsize = SHA256_DIGEST_SIZE;
  392. memcpy(authiv, std_iv_sha256, auth_ivsize);
  393. }
  394. authiv_words = auth_ivsize / sizeof(u32);
  395. qce_write_array(qce, REG_AUTH_IV0, (u32 *)authiv, authiv_words);
  396. } else if (IS_CCM(rctx->flags)) {
  397. /* Write nonce for CCM algorithms */
  398. authnonce_words = qce_be32_to_cpu_array(authnonce, rctx->ccm_nonce, QCE_MAX_NONCE);
  399. qce_write_array(qce, REG_AUTH_INFO_NONCE0, authnonce, authnonce_words);
  400. }
  401. /* Set up ENCR_SEG_CFG */
  402. encr_cfg = qce_encr_cfg(flags, enc_keylen);
  403. if (IS_ENCRYPT(flags))
  404. encr_cfg |= BIT(ENCODE_SHIFT);
  405. qce_write(qce, REG_ENCR_SEG_CFG, encr_cfg);
  406. /* Set up AUTH_SEG_CFG */
  407. auth_cfg = qce_auth_cfg(rctx->flags, auth_keylen, ctx->authsize);
  408. auth_cfg |= BIT(AUTH_LAST_SHIFT);
  409. auth_cfg |= BIT(AUTH_FIRST_SHIFT);
  410. if (IS_ENCRYPT(flags)) {
  411. if (IS_CCM(rctx->flags))
  412. auth_cfg |= AUTH_POS_BEFORE << AUTH_POS_SHIFT;
  413. else
  414. auth_cfg |= AUTH_POS_AFTER << AUTH_POS_SHIFT;
  415. } else {
  416. if (IS_CCM(rctx->flags))
  417. auth_cfg |= AUTH_POS_AFTER << AUTH_POS_SHIFT;
  418. else
  419. auth_cfg |= AUTH_POS_BEFORE << AUTH_POS_SHIFT;
  420. }
  421. qce_write(qce, REG_AUTH_SEG_CFG, auth_cfg);
  422. totallen = rctx->cryptlen + rctx->assoclen;
  423. /* Set the encryption size and start offset */
  424. if (IS_CCM(rctx->flags) && IS_DECRYPT(rctx->flags))
  425. qce_write(qce, REG_ENCR_SEG_SIZE, rctx->cryptlen + ctx->authsize);
  426. else
  427. qce_write(qce, REG_ENCR_SEG_SIZE, rctx->cryptlen);
  428. qce_write(qce, REG_ENCR_SEG_START, rctx->assoclen & 0xffff);
  429. /* Set the authentication size and start offset */
  430. qce_write(qce, REG_AUTH_SEG_SIZE, totallen);
  431. qce_write(qce, REG_AUTH_SEG_START, 0);
  432. /* Write total length */
  433. if (IS_CCM(rctx->flags) && IS_DECRYPT(rctx->flags))
  434. qce_write(qce, REG_SEG_SIZE, totallen + ctx->authsize);
  435. else
  436. qce_write(qce, REG_SEG_SIZE, totallen);
  437. /* get little endianness */
  438. config = qce_config_reg(qce, 1);
  439. qce_write(qce, REG_CONFIG, config);
  440. /* Start the process */
  441. qce_crypto_go(qce, !IS_CCM(flags));
  442. return 0;
  443. }
  444. #endif
  445. int qce_start(struct crypto_async_request *async_req, u32 type)
  446. {
  447. switch (type) {
  448. #ifdef CONFIG_CRYPTO_DEV_QCE_SKCIPHER
  449. case CRYPTO_ALG_TYPE_SKCIPHER:
  450. return qce_setup_regs_skcipher(async_req);
  451. #endif
  452. #ifdef CONFIG_CRYPTO_DEV_QCE_SHA
  453. case CRYPTO_ALG_TYPE_AHASH:
  454. return qce_setup_regs_ahash(async_req);
  455. #endif
  456. #ifdef CONFIG_CRYPTO_DEV_QCE_AEAD
  457. case CRYPTO_ALG_TYPE_AEAD:
  458. return qce_setup_regs_aead(async_req);
  459. #endif
  460. default:
  461. return -EINVAL;
  462. }
  463. }
  464. #define STATUS_ERRORS \
  465. (BIT(SW_ERR_SHIFT) | BIT(AXI_ERR_SHIFT) | BIT(HSD_ERR_SHIFT))
  466. int qce_check_status(struct qce_device *qce, u32 *status)
  467. {
  468. int ret = 0;
  469. *status = qce_read(qce, REG_STATUS);
  470. /*
  471. * Don't use result dump status. The operation may not be complete.
  472. * Instead, use the status we just read from device. In case, we need to
  473. * use result_status from result dump the result_status needs to be byte
  474. * swapped, since we set the device to little endian.
  475. */
  476. if (*status & STATUS_ERRORS || !(*status & BIT(OPERATION_DONE_SHIFT)))
  477. ret = -ENXIO;
  478. else if (*status & BIT(MAC_FAILED_SHIFT))
  479. ret = -EBADMSG;
  480. return ret;
  481. }
  482. void qce_get_version(struct qce_device *qce, u32 *major, u32 *minor, u32 *step)
  483. {
  484. u32 val;
  485. val = qce_read(qce, REG_VERSION);
  486. *major = (val & CORE_MAJOR_REV_MASK) >> CORE_MAJOR_REV_SHIFT;
  487. *minor = (val & CORE_MINOR_REV_MASK) >> CORE_MINOR_REV_SHIFT;
  488. *step = (val & CORE_STEP_REV_MASK) >> CORE_STEP_REV_SHIFT;
  489. }