omap-aes.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Cryptographic API.
  3. *
  4. * Support for OMAP AES HW ACCELERATOR defines
  5. *
  6. * Copyright (c) 2015 Texas Instruments Incorporated
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as published
  10. * by the Free Software Foundation.
  11. *
  12. */
  13. #ifndef __OMAP_AES_H__
  14. #define __OMAP_AES_H__
  15. #include <crypto/engine.h>
  16. #define DST_MAXBURST 4
  17. #define DMA_MIN (DST_MAXBURST * sizeof(u32))
  18. #define _calc_walked(inout) (dd->inout##_walk.offset - dd->inout##_sg->offset)
  19. /*
  20. * OMAP TRM gives bitfields as start:end, where start is the higher bit
  21. * number. For example 7:0
  22. */
  23. #define FLD_MASK(start, end) (((1 << ((start) - (end) + 1)) - 1) << (end))
  24. #define FLD_VAL(val, start, end) (((val) << (end)) & FLD_MASK(start, end))
  25. #define AES_REG_KEY(dd, x) ((dd)->pdata->key_ofs - \
  26. (((x) ^ 0x01) * 0x04))
  27. #define AES_REG_IV(dd, x) ((dd)->pdata->iv_ofs + ((x) * 0x04))
  28. #define AES_REG_CTRL(dd) ((dd)->pdata->ctrl_ofs)
  29. #define AES_REG_CTRL_CONTEXT_READY BIT(31)
  30. #define AES_REG_CTRL_CTR_WIDTH_MASK GENMASK(8, 7)
  31. #define AES_REG_CTRL_CTR_WIDTH_32 0
  32. #define AES_REG_CTRL_CTR_WIDTH_64 BIT(7)
  33. #define AES_REG_CTRL_CTR_WIDTH_96 BIT(8)
  34. #define AES_REG_CTRL_CTR_WIDTH_128 GENMASK(8, 7)
  35. #define AES_REG_CTRL_GCM GENMASK(17, 16)
  36. #define AES_REG_CTRL_CTR BIT(6)
  37. #define AES_REG_CTRL_CBC BIT(5)
  38. #define AES_REG_CTRL_KEY_SIZE GENMASK(4, 3)
  39. #define AES_REG_CTRL_DIRECTION BIT(2)
  40. #define AES_REG_CTRL_INPUT_READY BIT(1)
  41. #define AES_REG_CTRL_OUTPUT_READY BIT(0)
  42. #define AES_REG_CTRL_MASK GENMASK(24, 2)
  43. #define AES_REG_C_LEN_0 0x54
  44. #define AES_REG_C_LEN_1 0x58
  45. #define AES_REG_A_LEN 0x5C
  46. #define AES_REG_DATA_N(dd, x) ((dd)->pdata->data_ofs + ((x) * 0x04))
  47. #define AES_REG_TAG_N(dd, x) (0x70 + ((x) * 0x04))
  48. #define AES_REG_REV(dd) ((dd)->pdata->rev_ofs)
  49. #define AES_REG_MASK(dd) ((dd)->pdata->mask_ofs)
  50. #define AES_REG_MASK_SIDLE BIT(6)
  51. #define AES_REG_MASK_START BIT(5)
  52. #define AES_REG_MASK_DMA_OUT_EN BIT(3)
  53. #define AES_REG_MASK_DMA_IN_EN BIT(2)
  54. #define AES_REG_MASK_SOFTRESET BIT(1)
  55. #define AES_REG_AUTOIDLE BIT(0)
  56. #define AES_REG_LENGTH_N(x) (0x54 + ((x) * 0x04))
  57. #define AES_REG_IRQ_STATUS(dd) ((dd)->pdata->irq_status_ofs)
  58. #define AES_REG_IRQ_ENABLE(dd) ((dd)->pdata->irq_enable_ofs)
  59. #define AES_REG_IRQ_DATA_IN BIT(1)
  60. #define AES_REG_IRQ_DATA_OUT BIT(2)
  61. #define DEFAULT_TIMEOUT (5 * HZ)
  62. #define DEFAULT_AUTOSUSPEND_DELAY 1000
  63. #define FLAGS_MODE_MASK 0x001f
  64. #define FLAGS_ENCRYPT BIT(0)
  65. #define FLAGS_CBC BIT(1)
  66. #define FLAGS_CTR BIT(2)
  67. #define FLAGS_GCM BIT(3)
  68. #define FLAGS_RFC4106_GCM BIT(4)
  69. #define FLAGS_INIT BIT(5)
  70. #define FLAGS_FAST BIT(6)
  71. #define FLAGS_BUSY BIT(7)
  72. #define FLAGS_IN_DATA_ST_SHIFT 8
  73. #define FLAGS_OUT_DATA_ST_SHIFT 10
  74. #define FLAGS_ASSOC_DATA_ST_SHIFT 12
  75. #define AES_BLOCK_WORDS (AES_BLOCK_SIZE >> 2)
  76. struct omap_aes_gcm_result {
  77. struct completion completion;
  78. int err;
  79. };
  80. struct omap_aes_ctx {
  81. struct crypto_engine_ctx enginectx;
  82. int keylen;
  83. u32 key[AES_KEYSIZE_256 / sizeof(u32)];
  84. u8 nonce[4];
  85. struct crypto_skcipher *fallback;
  86. struct crypto_skcipher *ctr;
  87. };
  88. struct omap_aes_reqctx {
  89. struct omap_aes_dev *dd;
  90. unsigned long mode;
  91. u8 iv[AES_BLOCK_SIZE];
  92. u32 auth_tag[AES_BLOCK_SIZE / sizeof(u32)];
  93. };
  94. #define OMAP_AES_QUEUE_LENGTH 1
  95. #define OMAP_AES_CACHE_SIZE 0
  96. struct omap_aes_algs_info {
  97. struct crypto_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 ablkcipher_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_4106gcm_encrypt(struct aead_request *req);
  174. int omap_aes_4106gcm_decrypt(struct aead_request *req);
  175. int omap_aes_write_ctrl(struct omap_aes_dev *dd);
  176. int omap_aes_crypt_dma_start(struct omap_aes_dev *dd);
  177. int omap_aes_crypt_dma_stop(struct omap_aes_dev *dd);
  178. void omap_aes_gcm_dma_out_callback(void *data);
  179. void omap_aes_clear_copy_flags(struct omap_aes_dev *dd);
  180. #endif