omap-aes.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Cryptographic API.
  4. *
  5. * Support for OMAP AES HW ACCELERATOR defines
  6. *
  7. * Copyright (c) 2015 Texas Instruments Incorporated
  8. */
  9. #ifndef __OMAP_AES_H__
  10. #define __OMAP_AES_H__
  11. #include <crypto/aes.h>
  12. #include <crypto/engine.h>
  13. #define DST_MAXBURST 4
  14. #define DMA_MIN (DST_MAXBURST * sizeof(u32))
  15. #define _calc_walked(inout) (dd->inout##_walk.offset - dd->inout##_sg->offset)
  16. /*
  17. * OMAP TRM gives bitfields as start:end, where start is the higher bit
  18. * number. For example 7:0
  19. */
  20. #define FLD_MASK(start, end) (((1 << ((start) - (end) + 1)) - 1) << (end))
  21. #define FLD_VAL(val, start, end) (((val) << (end)) & FLD_MASK(start, end))
  22. #define AES_REG_KEY(dd, x) ((dd)->pdata->key_ofs - \
  23. (((x) ^ 0x01) * 0x04))
  24. #define AES_REG_IV(dd, x) ((dd)->pdata->iv_ofs + ((x) * 0x04))
  25. #define AES_REG_CTRL(dd) ((dd)->pdata->ctrl_ofs)
  26. #define AES_REG_CTRL_CONTEXT_READY BIT(31)
  27. #define AES_REG_CTRL_CTR_WIDTH_MASK GENMASK(8, 7)
  28. #define AES_REG_CTRL_CTR_WIDTH_32 0
  29. #define AES_REG_CTRL_CTR_WIDTH_64 BIT(7)
  30. #define AES_REG_CTRL_CTR_WIDTH_96 BIT(8)
  31. #define AES_REG_CTRL_CTR_WIDTH_128 GENMASK(8, 7)
  32. #define AES_REG_CTRL_GCM GENMASK(17, 16)
  33. #define AES_REG_CTRL_CTR BIT(6)
  34. #define AES_REG_CTRL_CBC BIT(5)
  35. #define AES_REG_CTRL_KEY_SIZE GENMASK(4, 3)
  36. #define AES_REG_CTRL_DIRECTION BIT(2)
  37. #define AES_REG_CTRL_INPUT_READY BIT(1)
  38. #define AES_REG_CTRL_OUTPUT_READY BIT(0)
  39. #define AES_REG_CTRL_MASK GENMASK(24, 2)
  40. #define AES_REG_C_LEN_0 0x54
  41. #define AES_REG_C_LEN_1 0x58
  42. #define AES_REG_A_LEN 0x5C
  43. #define AES_REG_DATA_N(dd, x) ((dd)->pdata->data_ofs + ((x) * 0x04))
  44. #define AES_REG_TAG_N(dd, x) (0x70 + ((x) * 0x04))
  45. #define AES_REG_REV(dd) ((dd)->pdata->rev_ofs)
  46. #define AES_REG_MASK(dd) ((dd)->pdata->mask_ofs)
  47. #define AES_REG_MASK_SIDLE BIT(6)
  48. #define AES_REG_MASK_START BIT(5)
  49. #define AES_REG_MASK_DMA_OUT_EN BIT(3)
  50. #define AES_REG_MASK_DMA_IN_EN BIT(2)
  51. #define AES_REG_MASK_SOFTRESET BIT(1)
  52. #define AES_REG_AUTOIDLE BIT(0)
  53. #define AES_REG_LENGTH_N(x) (0x54 + ((x) * 0x04))
  54. #define AES_REG_IRQ_STATUS(dd) ((dd)->pdata->irq_status_ofs)
  55. #define AES_REG_IRQ_ENABLE(dd) ((dd)->pdata->irq_enable_ofs)
  56. #define AES_REG_IRQ_DATA_IN BIT(1)
  57. #define AES_REG_IRQ_DATA_OUT BIT(2)
  58. #define DEFAULT_TIMEOUT (5 * HZ)
  59. #define DEFAULT_AUTOSUSPEND_DELAY 1000
  60. #define FLAGS_MODE_MASK 0x001f
  61. #define FLAGS_ENCRYPT BIT(0)
  62. #define FLAGS_CBC BIT(1)
  63. #define FLAGS_CTR BIT(2)
  64. #define FLAGS_GCM BIT(3)
  65. #define FLAGS_RFC4106_GCM BIT(4)
  66. #define FLAGS_INIT BIT(5)
  67. #define FLAGS_FAST BIT(6)
  68. #define FLAGS_IN_DATA_ST_SHIFT 8
  69. #define FLAGS_OUT_DATA_ST_SHIFT 10
  70. #define FLAGS_ASSOC_DATA_ST_SHIFT 12
  71. #define AES_BLOCK_WORDS (AES_BLOCK_SIZE >> 2)
  72. struct omap_aes_gcm_result {
  73. struct completion completion;
  74. int err;
  75. };
  76. struct omap_aes_ctx {
  77. struct crypto_engine_ctx enginectx;
  78. int keylen;
  79. u32 key[AES_KEYSIZE_256 / sizeof(u32)];
  80. u8 nonce[4];
  81. struct crypto_skcipher *fallback;
  82. };
  83. struct omap_aes_gcm_ctx {
  84. struct omap_aes_ctx octx;
  85. struct crypto_aes_ctx actx;
  86. };
  87. struct omap_aes_reqctx {
  88. struct omap_aes_dev *dd;
  89. unsigned long mode;
  90. u8 iv[AES_BLOCK_SIZE];
  91. u32 auth_tag[AES_BLOCK_SIZE / sizeof(u32)];
  92. struct skcipher_request fallback_req; // keep at the end
  93. };
  94. #define OMAP_AES_QUEUE_LENGTH 1
  95. #define OMAP_AES_CACHE_SIZE 0
  96. struct omap_aes_algs_info {
  97. struct skcipher_alg *algs_list;
  98. unsigned int size;
  99. unsigned int registered;
  100. };
  101. struct omap_aes_aead_algs {
  102. struct aead_alg *algs_list;
  103. unsigned int size;
  104. unsigned int registered;
  105. };
  106. struct omap_aes_pdata {
  107. struct omap_aes_algs_info *algs_info;
  108. unsigned int algs_info_size;
  109. struct omap_aes_aead_algs *aead_algs_info;
  110. void (*trigger)(struct omap_aes_dev *dd, int length);
  111. u32 key_ofs;
  112. u32 iv_ofs;
  113. u32 ctrl_ofs;
  114. u32 data_ofs;
  115. u32 rev_ofs;
  116. u32 mask_ofs;
  117. u32 irq_enable_ofs;
  118. u32 irq_status_ofs;
  119. u32 dma_enable_in;
  120. u32 dma_enable_out;
  121. u32 dma_start;
  122. u32 major_mask;
  123. u32 major_shift;
  124. u32 minor_mask;
  125. u32 minor_shift;
  126. };
  127. struct omap_aes_dev {
  128. struct list_head list;
  129. unsigned long phys_base;
  130. void __iomem *io_base;
  131. struct omap_aes_ctx *ctx;
  132. struct device *dev;
  133. unsigned long flags;
  134. int err;
  135. struct tasklet_struct done_task;
  136. struct aead_queue aead_queue;
  137. spinlock_t lock;
  138. struct skcipher_request *req;
  139. struct aead_request *aead_req;
  140. struct crypto_engine *engine;
  141. /*
  142. * total is used by PIO mode for book keeping so introduce
  143. * variable total_save as need it to calc page_order
  144. */
  145. size_t total;
  146. size_t total_save;
  147. size_t assoc_len;
  148. size_t authsize;
  149. struct scatterlist *in_sg;
  150. struct scatterlist *out_sg;
  151. /* Buffers for copying for unaligned cases */
  152. struct scatterlist in_sgl[2];
  153. struct scatterlist out_sgl;
  154. struct scatterlist *orig_out;
  155. struct scatter_walk in_walk;
  156. struct scatter_walk out_walk;
  157. struct dma_chan *dma_lch_in;
  158. struct dma_chan *dma_lch_out;
  159. int in_sg_len;
  160. int out_sg_len;
  161. int pio_only;
  162. const struct omap_aes_pdata *pdata;
  163. };
  164. u32 omap_aes_read(struct omap_aes_dev *dd, u32 offset);
  165. void omap_aes_write(struct omap_aes_dev *dd, u32 offset, u32 value);
  166. struct omap_aes_dev *omap_aes_find_dev(struct omap_aes_reqctx *rctx);
  167. int omap_aes_gcm_setkey(struct crypto_aead *tfm, const u8 *key,
  168. unsigned int keylen);
  169. int omap_aes_4106gcm_setkey(struct crypto_aead *tfm, const u8 *key,
  170. unsigned int keylen);
  171. int omap_aes_gcm_encrypt(struct aead_request *req);
  172. int omap_aes_gcm_decrypt(struct aead_request *req);
  173. int omap_aes_gcm_setauthsize(struct crypto_aead *tfm, unsigned int authsize);
  174. int omap_aes_4106gcm_encrypt(struct aead_request *req);
  175. int omap_aes_4106gcm_decrypt(struct aead_request *req);
  176. int omap_aes_4106gcm_setauthsize(struct crypto_aead *parent,
  177. unsigned int authsize);
  178. int omap_aes_gcm_cra_init(struct crypto_aead *tfm);
  179. int omap_aes_write_ctrl(struct omap_aes_dev *dd);
  180. int omap_aes_crypt_dma_start(struct omap_aes_dev *dd);
  181. int omap_aes_crypt_dma_stop(struct omap_aes_dev *dd);
  182. void omap_aes_gcm_dma_out_callback(void *data);
  183. void omap_aes_clear_copy_flags(struct omap_aes_dev *dd);
  184. #endif