crypto4xx_core.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 "crypto4xx_reg_def.h"
  26. #include "crypto4xx_sa.h"
  27. #define PPC460SX_SDR0_SRST 0x201
  28. #define PPC405EX_SDR0_SRST 0x200
  29. #define PPC460EX_SDR0_SRST 0x201
  30. #define PPC460EX_CE_RESET 0x08000000
  31. #define PPC460SX_CE_RESET 0x20000000
  32. #define PPC405EX_CE_RESET 0x00000008
  33. #define CRYPTO4XX_CRYPTO_PRIORITY 300
  34. #define PPC4XX_NUM_PD 256
  35. #define PPC4XX_LAST_PD (PPC4XX_NUM_PD - 1)
  36. #define PPC4XX_NUM_GD 1024
  37. #define PPC4XX_LAST_GD (PPC4XX_NUM_GD - 1)
  38. #define PPC4XX_NUM_SD 256
  39. #define PPC4XX_LAST_SD (PPC4XX_NUM_SD - 1)
  40. #define PPC4XX_SD_BUFFER_SIZE 2048
  41. #define PD_ENTRY_BUSY BIT(1)
  42. #define PD_ENTRY_INUSE BIT(0)
  43. #define PD_ENTRY_FREE 0
  44. #define ERING_WAS_FULL 0xffffffff
  45. struct crypto4xx_device;
  46. union shadow_sa_buf {
  47. struct dynamic_sa_ctl sa;
  48. /* alloc 256 bytes which is enough for any kind of dynamic sa */
  49. u8 buf[256];
  50. } __packed;
  51. struct pd_uinfo {
  52. struct crypto4xx_device *dev;
  53. u32 state;
  54. u32 using_sd;
  55. u32 first_gd; /* first gather discriptor
  56. used by this packet */
  57. u32 num_gd; /* number of gather discriptor
  58. used by this packet */
  59. u32 first_sd; /* first scatter discriptor
  60. used by this packet */
  61. u32 num_sd; /* number of scatter discriptors
  62. used by this packet */
  63. struct dynamic_sa_ctl *sa_va; /* shadow sa */
  64. struct sa_state_record *sr_va; /* state record for shadow sa */
  65. u32 sr_pa;
  66. struct scatterlist *dest_va;
  67. struct crypto_async_request *async_req; /* base crypto request
  68. for this packet */
  69. };
  70. struct crypto4xx_device {
  71. struct crypto4xx_core_device *core_dev;
  72. void __iomem *ce_base;
  73. void __iomem *trng_base;
  74. struct ce_pd *pdr; /* base address of packet descriptor ring */
  75. dma_addr_t pdr_pa; /* physical address of pdr_base_register */
  76. struct ce_gd *gdr; /* gather descriptor ring */
  77. dma_addr_t gdr_pa; /* physical address of gdr_base_register */
  78. struct ce_sd *sdr; /* scatter descriptor ring */
  79. dma_addr_t sdr_pa; /* physical address of sdr_base_register */
  80. void *scatter_buffer_va;
  81. dma_addr_t scatter_buffer_pa;
  82. union shadow_sa_buf *shadow_sa_pool;
  83. dma_addr_t shadow_sa_pool_pa;
  84. struct sa_state_record *shadow_sr_pool;
  85. dma_addr_t shadow_sr_pool_pa;
  86. u32 pdr_tail;
  87. u32 pdr_head;
  88. u32 gdr_tail;
  89. u32 gdr_head;
  90. u32 sdr_tail;
  91. u32 sdr_head;
  92. struct pd_uinfo *pdr_uinfo;
  93. struct list_head alg_list; /* List of algorithm supported
  94. by this device */
  95. struct ratelimit_state aead_ratelimit;
  96. bool is_revb;
  97. };
  98. struct crypto4xx_core_device {
  99. struct device *device;
  100. struct platform_device *ofdev;
  101. struct crypto4xx_device *dev;
  102. struct hwrng *trng;
  103. u32 int_status;
  104. u32 irq;
  105. struct tasklet_struct tasklet;
  106. spinlock_t lock;
  107. };
  108. struct crypto4xx_ctx {
  109. struct crypto4xx_device *dev;
  110. struct dynamic_sa_ctl *sa_in;
  111. struct dynamic_sa_ctl *sa_out;
  112. __le32 iv_nonce;
  113. u32 sa_len;
  114. union {
  115. struct crypto_aead *aead;
  116. } sw_cipher;
  117. };
  118. struct crypto4xx_alg_common {
  119. u32 type;
  120. union {
  121. struct crypto_alg cipher;
  122. struct ahash_alg hash;
  123. struct aead_alg aead;
  124. } u;
  125. };
  126. struct crypto4xx_alg {
  127. struct list_head entry;
  128. struct crypto4xx_alg_common alg;
  129. struct crypto4xx_device *dev;
  130. };
  131. int crypto4xx_alloc_sa(struct crypto4xx_ctx *ctx, u32 size);
  132. void crypto4xx_free_sa(struct crypto4xx_ctx *ctx);
  133. void crypto4xx_free_ctx(struct crypto4xx_ctx *ctx);
  134. int crypto4xx_build_pd(struct crypto_async_request *req,
  135. struct crypto4xx_ctx *ctx,
  136. struct scatterlist *src,
  137. struct scatterlist *dst,
  138. const unsigned int datalen,
  139. const __le32 *iv, const u32 iv_len,
  140. const struct dynamic_sa_ctl *sa,
  141. const unsigned int sa_len,
  142. const unsigned int assoclen);
  143. int crypto4xx_setkey_aes_cbc(struct crypto_ablkcipher *cipher,
  144. const u8 *key, unsigned int keylen);
  145. int crypto4xx_setkey_aes_cfb(struct crypto_ablkcipher *cipher,
  146. const u8 *key, unsigned int keylen);
  147. int crypto4xx_setkey_aes_ecb(struct crypto_ablkcipher *cipher,
  148. const u8 *key, unsigned int keylen);
  149. int crypto4xx_setkey_aes_ofb(struct crypto_ablkcipher *cipher,
  150. const u8 *key, unsigned int keylen);
  151. int crypto4xx_setkey_rfc3686(struct crypto_ablkcipher *cipher,
  152. const u8 *key, unsigned int keylen);
  153. int crypto4xx_encrypt(struct ablkcipher_request *req);
  154. int crypto4xx_decrypt(struct ablkcipher_request *req);
  155. int crypto4xx_rfc3686_encrypt(struct ablkcipher_request *req);
  156. int crypto4xx_rfc3686_decrypt(struct ablkcipher_request *req);
  157. int crypto4xx_sha1_alg_init(struct crypto_tfm *tfm);
  158. int crypto4xx_hash_digest(struct ahash_request *req);
  159. int crypto4xx_hash_final(struct ahash_request *req);
  160. int crypto4xx_hash_update(struct ahash_request *req);
  161. int crypto4xx_hash_init(struct ahash_request *req);
  162. /**
  163. * Note: Only use this function to copy items that is word aligned.
  164. */
  165. static inline void crypto4xx_memcpy_swab32(u32 *dst, const void *buf,
  166. size_t len)
  167. {
  168. for (; len >= 4; buf += 4, len -= 4)
  169. *dst++ = __swab32p((u32 *) buf);
  170. if (len) {
  171. const u8 *tmp = (u8 *)buf;
  172. switch (len) {
  173. case 3:
  174. *dst = (tmp[2] << 16) |
  175. (tmp[1] << 8) |
  176. tmp[0];
  177. break;
  178. case 2:
  179. *dst = (tmp[1] << 8) |
  180. tmp[0];
  181. break;
  182. case 1:
  183. *dst = tmp[0];
  184. break;
  185. default:
  186. break;
  187. }
  188. }
  189. }
  190. static inline void crypto4xx_memcpy_from_le32(u32 *dst, const void *buf,
  191. size_t len)
  192. {
  193. crypto4xx_memcpy_swab32(dst, buf, len);
  194. }
  195. static inline void crypto4xx_memcpy_to_le32(__le32 *dst, const void *buf,
  196. size_t len)
  197. {
  198. crypto4xx_memcpy_swab32((u32 *)dst, buf, len);
  199. }
  200. int crypto4xx_setauthsize_aead(struct crypto_aead *ciper,
  201. unsigned int authsize);
  202. int crypto4xx_setkey_aes_ccm(struct crypto_aead *cipher,
  203. const u8 *key, unsigned int keylen);
  204. int crypto4xx_encrypt_aes_ccm(struct aead_request *req);
  205. int crypto4xx_decrypt_aes_ccm(struct aead_request *req);
  206. int crypto4xx_setkey_aes_gcm(struct crypto_aead *cipher,
  207. const u8 *key, unsigned int keylen);
  208. int crypto4xx_encrypt_aes_gcm(struct aead_request *req);
  209. int crypto4xx_decrypt_aes_gcm(struct aead_request *req);
  210. #endif