amlogic-gxl.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * amlogic.h - hardware cryptographic offloader for Amlogic SoC
  4. *
  5. * Copyright (C) 2018-2019 Corentin LABBE <[email protected]>
  6. */
  7. #include <crypto/aes.h>
  8. #include <crypto/engine.h>
  9. #include <crypto/skcipher.h>
  10. #include <linux/debugfs.h>
  11. #include <linux/crypto.h>
  12. #include <linux/scatterlist.h>
  13. #define MODE_KEY 1
  14. #define MODE_AES_128 0x8
  15. #define MODE_AES_192 0x9
  16. #define MODE_AES_256 0xa
  17. #define MESON_DECRYPT 0
  18. #define MESON_ENCRYPT 1
  19. #define MESON_OPMODE_ECB 0
  20. #define MESON_OPMODE_CBC 1
  21. #define MAXFLOW 2
  22. #define MAXDESC 64
  23. #define DESC_LAST BIT(18)
  24. #define DESC_ENCRYPTION BIT(28)
  25. #define DESC_OWN BIT(31)
  26. /*
  27. * struct meson_desc - Descriptor for DMA operations
  28. * Note that without datasheet, some are unknown
  29. * @t_status: Descriptor of the cipher operation (see description below)
  30. * @t_src: Physical address of data to read
  31. * @t_dst: Physical address of data to write
  32. * t_status is segmented like this:
  33. * @len: 0-16 length of data to operate
  34. * @irq: 17 Ignored by hardware
  35. * @eoc: 18 End means the descriptor is the last
  36. * @loop: 19 Unknown
  37. * @mode: 20-23 Type of algorithm (AES, SHA)
  38. * @begin: 24 Unknown
  39. * @end: 25 Unknown
  40. * @op_mode: 26-27 Blockmode (CBC, ECB)
  41. * @enc: 28 0 means decryption, 1 is for encryption
  42. * @block: 29 Unknown
  43. * @error: 30 Unknown
  44. * @owner: 31 owner of the descriptor, 1 own by HW
  45. */
  46. struct meson_desc {
  47. __le32 t_status;
  48. __le32 t_src;
  49. __le32 t_dst;
  50. };
  51. /*
  52. * struct meson_flow - Information used by each flow
  53. * @engine: ptr to the crypto_engine for this flow
  54. * @keylen: keylen for this flow operation
  55. * @complete: completion for the current task on this flow
  56. * @status: set to 1 by interrupt if task is done
  57. * @t_phy: Physical address of task
  58. * @tl: pointer to the current ce_task for this flow
  59. * @stat_req: number of request done by this flow
  60. */
  61. struct meson_flow {
  62. struct crypto_engine *engine;
  63. struct completion complete;
  64. int status;
  65. unsigned int keylen;
  66. dma_addr_t t_phy;
  67. struct meson_desc *tl;
  68. #ifdef CONFIG_CRYPTO_DEV_AMLOGIC_GXL_DEBUG
  69. unsigned long stat_req;
  70. #endif
  71. };
  72. /*
  73. * struct meson_dev - main container for all this driver information
  74. * @base: base address of amlogic-crypto
  75. * @busclk: bus clock for amlogic-crypto
  76. * @dev: the platform device
  77. * @chanlist: array of all flow
  78. * @flow: flow to use in next request
  79. * @irqs: IRQ numbers for amlogic-crypto
  80. * @dbgfs_dir: Debugfs dentry for statistic directory
  81. * @dbgfs_stats: Debugfs dentry for statistic counters
  82. */
  83. struct meson_dev {
  84. void __iomem *base;
  85. struct clk *busclk;
  86. struct device *dev;
  87. struct meson_flow *chanlist;
  88. atomic_t flow;
  89. int irqs[MAXFLOW];
  90. #ifdef CONFIG_CRYPTO_DEV_AMLOGIC_GXL_DEBUG
  91. struct dentry *dbgfs_dir;
  92. #endif
  93. };
  94. /*
  95. * struct meson_cipher_req_ctx - context for a skcipher request
  96. * @op_dir: direction (encrypt vs decrypt) for this request
  97. * @flow: the flow to use for this request
  98. */
  99. struct meson_cipher_req_ctx {
  100. u32 op_dir;
  101. int flow;
  102. struct skcipher_request fallback_req; // keep at the end
  103. };
  104. /*
  105. * struct meson_cipher_tfm_ctx - context for a skcipher TFM
  106. * @enginectx: crypto_engine used by this TFM
  107. * @key: pointer to key data
  108. * @keylen: len of the key
  109. * @keymode: The keymode(type and size of key) associated with this TFM
  110. * @mc: pointer to the private data of driver handling this TFM
  111. * @fallback_tfm: pointer to the fallback TFM
  112. */
  113. struct meson_cipher_tfm_ctx {
  114. struct crypto_engine_ctx enginectx;
  115. u32 *key;
  116. u32 keylen;
  117. u32 keymode;
  118. struct meson_dev *mc;
  119. struct crypto_skcipher *fallback_tfm;
  120. };
  121. /*
  122. * struct meson_alg_template - crypto_alg template
  123. * @type: the CRYPTO_ALG_TYPE for this template
  124. * @blockmode: the type of block operation
  125. * @mc: pointer to the meson_dev structure associated with this template
  126. * @alg: one of sub struct must be used
  127. * @stat_req: number of request done on this template
  128. * @stat_fb: total of all data len done on this template
  129. */
  130. struct meson_alg_template {
  131. u32 type;
  132. u32 blockmode;
  133. union {
  134. struct skcipher_alg skcipher;
  135. } alg;
  136. struct meson_dev *mc;
  137. #ifdef CONFIG_CRYPTO_DEV_AMLOGIC_GXL_DEBUG
  138. unsigned long stat_req;
  139. unsigned long stat_fb;
  140. #endif
  141. };
  142. int meson_enqueue(struct crypto_async_request *areq, u32 type);
  143. int meson_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,
  144. unsigned int keylen);
  145. int meson_cipher_init(struct crypto_tfm *tfm);
  146. void meson_cipher_exit(struct crypto_tfm *tfm);
  147. int meson_skdecrypt(struct skcipher_request *areq);
  148. int meson_skencrypt(struct skcipher_request *areq);