crypto4xx_core.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * AMCC SoC PPC4xx Crypto Driver
  4. *
  5. * Copyright (c) 2008 Applied Micro Circuits Corporation.
  6. * All rights reserved. James Hsiao <jhsiao@amcc.com>
  7. *
  8. * This is the header file for AMCC Crypto offload Linux device driver for
  9. * use with Linux CryptoAPI.
  10. */
  11. #ifndef __CRYPTO4XX_CORE_H__
  12. #define __CRYPTO4XX_CORE_H__
  13. #include <linux/ratelimit.h>
  14. #include <linux/mutex.h>
  15. #include <linux/scatterlist.h>
  16. #include <crypto/internal/aead.h>
  17. #include <crypto/internal/rng.h>
  18. #include <crypto/internal/skcipher.h>
  19. #include "crypto4xx_reg_def.h"
  20. #include "crypto4xx_sa.h"
  21. #define PPC460SX_SDR0_SRST 0x201
  22. #define PPC405EX_SDR0_SRST 0x200
  23. #define PPC460EX_SDR0_SRST 0x201
  24. #define PPC460EX_CE_RESET 0x08000000
  25. #define PPC460SX_CE_RESET 0x20000000
  26. #define PPC405EX_CE_RESET 0x00000008
  27. #define CRYPTO4XX_CRYPTO_PRIORITY 300
  28. #define PPC4XX_NUM_PD 256
  29. #define PPC4XX_LAST_PD (PPC4XX_NUM_PD - 1)
  30. #define PPC4XX_NUM_GD 1024
  31. #define PPC4XX_LAST_GD (PPC4XX_NUM_GD - 1)
  32. #define PPC4XX_NUM_SD 256
  33. #define PPC4XX_LAST_SD (PPC4XX_NUM_SD - 1)
  34. #define PPC4XX_SD_BUFFER_SIZE 2048
  35. #define PD_ENTRY_BUSY BIT(1)
  36. #define PD_ENTRY_INUSE BIT(0)
  37. #define PD_ENTRY_FREE 0
  38. #define ERING_WAS_FULL 0xffffffff
  39. struct crypto4xx_device;
  40. union shadow_sa_buf {
  41. struct dynamic_sa_ctl sa;
  42. /* alloc 256 bytes which is enough for any kind of dynamic sa */
  43. u8 buf[256];
  44. } __packed;
  45. struct pd_uinfo {
  46. struct crypto4xx_device *dev;
  47. u32 state;
  48. u32 first_gd; /* first gather discriptor
  49. used by this packet */
  50. u32 num_gd; /* number of gather discriptor
  51. used by this packet */
  52. u32 first_sd; /* first scatter discriptor
  53. used by this packet */
  54. u32 num_sd; /* number of scatter discriptors
  55. used by this packet */
  56. struct dynamic_sa_ctl *sa_va; /* shadow sa */
  57. struct sa_state_record *sr_va; /* state record for shadow sa */
  58. u32 sr_pa;
  59. struct scatterlist *dest_va;
  60. struct crypto_async_request *async_req; /* base crypto request
  61. for this packet */
  62. };
  63. struct crypto4xx_device {
  64. struct crypto4xx_core_device *core_dev;
  65. void __iomem *ce_base;
  66. void __iomem *trng_base;
  67. struct ce_pd *pdr; /* base address of packet descriptor ring */
  68. dma_addr_t pdr_pa; /* physical address of pdr_base_register */
  69. struct ce_gd *gdr; /* gather descriptor ring */
  70. dma_addr_t gdr_pa; /* physical address of gdr_base_register */
  71. struct ce_sd *sdr; /* scatter descriptor ring */
  72. dma_addr_t sdr_pa; /* physical address of sdr_base_register */
  73. void *scatter_buffer_va;
  74. dma_addr_t scatter_buffer_pa;
  75. union shadow_sa_buf *shadow_sa_pool;
  76. dma_addr_t shadow_sa_pool_pa;
  77. struct sa_state_record *shadow_sr_pool;
  78. dma_addr_t shadow_sr_pool_pa;
  79. u32 pdr_tail;
  80. u32 pdr_head;
  81. u32 gdr_tail;
  82. u32 gdr_head;
  83. u32 sdr_tail;
  84. u32 sdr_head;
  85. struct pd_uinfo *pdr_uinfo;
  86. struct list_head alg_list; /* List of algorithm supported
  87. by this device */
  88. struct ratelimit_state aead_ratelimit;
  89. bool is_revb;
  90. };
  91. struct crypto4xx_core_device {
  92. struct device *device;
  93. struct platform_device *ofdev;
  94. struct crypto4xx_device *dev;
  95. struct hwrng *trng;
  96. u32 int_status;
  97. u32 irq;
  98. struct tasklet_struct tasklet;
  99. spinlock_t lock;
  100. struct mutex rng_lock;
  101. };
  102. struct crypto4xx_ctx {
  103. struct crypto4xx_device *dev;
  104. struct dynamic_sa_ctl *sa_in;
  105. struct dynamic_sa_ctl *sa_out;
  106. __le32 iv_nonce;
  107. u32 sa_len;
  108. union {
  109. struct crypto_sync_skcipher *cipher;
  110. struct crypto_aead *aead;
  111. } sw_cipher;
  112. };
  113. struct crypto4xx_aead_reqctx {
  114. struct scatterlist dst[2];
  115. };
  116. struct crypto4xx_alg_common {
  117. u32 type;
  118. union {
  119. struct skcipher_alg cipher;
  120. struct aead_alg aead;
  121. struct rng_alg rng;
  122. } u;
  123. };
  124. struct crypto4xx_alg {
  125. struct list_head entry;
  126. struct crypto4xx_alg_common alg;
  127. struct crypto4xx_device *dev;
  128. };
  129. #if IS_ENABLED(CONFIG_CC_IS_GCC) && CONFIG_GCC_VERSION >= 120000
  130. #define BUILD_PD_ACCESS __attribute__((access(read_only, 6, 7)))
  131. #else
  132. #define BUILD_PD_ACCESS
  133. #endif
  134. int crypto4xx_alloc_sa(struct crypto4xx_ctx *ctx, u32 size);
  135. void crypto4xx_free_sa(struct crypto4xx_ctx *ctx);
  136. int crypto4xx_build_pd(struct crypto_async_request *req,
  137. struct crypto4xx_ctx *ctx,
  138. struct scatterlist *src,
  139. struct scatterlist *dst,
  140. const unsigned int datalen,
  141. const void *iv, const u32 iv_len,
  142. const struct dynamic_sa_ctl *sa,
  143. const unsigned int sa_len,
  144. const unsigned int assoclen,
  145. struct scatterlist *dst_tmp) BUILD_PD_ACCESS;
  146. int crypto4xx_setkey_aes_cbc(struct crypto_skcipher *cipher,
  147. const u8 *key, unsigned int keylen);
  148. int crypto4xx_setkey_aes_ctr(struct crypto_skcipher *cipher,
  149. const u8 *key, unsigned int keylen);
  150. int crypto4xx_setkey_aes_ecb(struct crypto_skcipher *cipher,
  151. const u8 *key, unsigned int keylen);
  152. int crypto4xx_setkey_rfc3686(struct crypto_skcipher *cipher,
  153. const u8 *key, unsigned int keylen);
  154. int crypto4xx_encrypt_ctr(struct skcipher_request *req);
  155. int crypto4xx_decrypt_ctr(struct skcipher_request *req);
  156. int crypto4xx_encrypt_iv_stream(struct skcipher_request *req);
  157. int crypto4xx_decrypt_iv_stream(struct skcipher_request *req);
  158. int crypto4xx_encrypt_iv_block(struct skcipher_request *req);
  159. int crypto4xx_decrypt_iv_block(struct skcipher_request *req);
  160. int crypto4xx_encrypt_noiv_block(struct skcipher_request *req);
  161. int crypto4xx_decrypt_noiv_block(struct skcipher_request *req);
  162. int crypto4xx_rfc3686_encrypt(struct skcipher_request *req);
  163. int crypto4xx_rfc3686_decrypt(struct skcipher_request *req);
  164. /*
  165. * Note: Only use this function to copy items that is word aligned.
  166. */
  167. static inline void crypto4xx_memcpy_swab32(u32 *dst, const void *buf,
  168. size_t len)
  169. {
  170. for (; len >= 4; buf += 4, len -= 4)
  171. *dst++ = __swab32p((u32 *) buf);
  172. if (len) {
  173. const u8 *tmp = (u8 *)buf;
  174. switch (len) {
  175. case 3:
  176. *dst = (tmp[2] << 16) |
  177. (tmp[1] << 8) |
  178. tmp[0];
  179. break;
  180. case 2:
  181. *dst = (tmp[1] << 8) |
  182. tmp[0];
  183. break;
  184. case 1:
  185. *dst = tmp[0];
  186. break;
  187. default:
  188. break;
  189. }
  190. }
  191. }
  192. static inline void crypto4xx_memcpy_from_le32(u32 *dst, const void *buf,
  193. size_t len)
  194. {
  195. crypto4xx_memcpy_swab32(dst, buf, len);
  196. }
  197. static inline void crypto4xx_memcpy_to_le32(__le32 *dst, const void *buf,
  198. size_t len)
  199. {
  200. crypto4xx_memcpy_swab32((u32 *)dst, buf, len);
  201. }
  202. int crypto4xx_setauthsize_aead(struct crypto_aead *ciper,
  203. unsigned int authsize);
  204. int crypto4xx_setkey_aes_ccm(struct crypto_aead *cipher,
  205. const u8 *key, unsigned int keylen);
  206. int crypto4xx_encrypt_aes_ccm(struct aead_request *req);
  207. int crypto4xx_decrypt_aes_ccm(struct aead_request *req);
  208. int crypto4xx_setkey_aes_gcm(struct crypto_aead *cipher,
  209. const u8 *key, unsigned int keylen);
  210. int crypto4xx_encrypt_aes_gcm(struct aead_request *req);
  211. int crypto4xx_decrypt_aes_gcm(struct aead_request *req);
  212. #endif