crypto4xx_core.h 7.5 KB

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