nx.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __NX_H__
  3. #define __NX_H__
  4. #include <crypto/ctr.h>
  5. #define NX_NAME "nx-crypto"
  6. #define NX_STRING "IBM Power7+ Nest Accelerator Crypto Driver"
  7. #define NX_VERSION "1.0"
  8. static const char nx_driver_string[] = NX_STRING;
  9. static const char nx_driver_version[] = NX_VERSION;
  10. /* a scatterlist in the format PHYP is expecting */
  11. struct nx_sg {
  12. u64 addr;
  13. u32 rsvd;
  14. u32 len;
  15. } __attribute((packed));
  16. #define NX_PAGE_SIZE (4096)
  17. #define NX_MAX_SG_ENTRIES (NX_PAGE_SIZE/(sizeof(struct nx_sg)))
  18. enum nx_status {
  19. NX_DISABLED,
  20. NX_WAITING,
  21. NX_OKAY
  22. };
  23. /* msc_triplet and max_sync_cop are used only to assist in parsing the
  24. * openFirmware property */
  25. struct msc_triplet {
  26. u32 keybitlen;
  27. u32 databytelen;
  28. u32 sglen;
  29. } __packed;
  30. struct max_sync_cop {
  31. u32 fc;
  32. u32 mode;
  33. u32 triplets;
  34. struct msc_triplet trip[0];
  35. } __packed;
  36. struct alg_props {
  37. u32 databytelen;
  38. u32 sglen;
  39. };
  40. #define NX_OF_FLAG_MAXSGLEN_SET (1)
  41. #define NX_OF_FLAG_STATUS_SET (2)
  42. #define NX_OF_FLAG_MAXSYNCCOP_SET (4)
  43. #define NX_OF_FLAG_MASK_READY (NX_OF_FLAG_MAXSGLEN_SET | \
  44. NX_OF_FLAG_STATUS_SET | \
  45. NX_OF_FLAG_MAXSYNCCOP_SET)
  46. struct nx_of {
  47. u32 flags;
  48. u32 max_sg_len;
  49. enum nx_status status;
  50. struct alg_props ap[NX_MAX_FC][NX_MAX_MODE][3];
  51. };
  52. struct nx_stats {
  53. atomic_t aes_ops;
  54. atomic64_t aes_bytes;
  55. atomic_t sha256_ops;
  56. atomic64_t sha256_bytes;
  57. atomic_t sha512_ops;
  58. atomic64_t sha512_bytes;
  59. atomic_t sync_ops;
  60. atomic_t errors;
  61. atomic_t last_error;
  62. atomic_t last_error_pid;
  63. };
  64. struct nx_debugfs {
  65. struct dentry *dfs_root;
  66. struct dentry *dfs_aes_ops, *dfs_aes_bytes;
  67. struct dentry *dfs_sha256_ops, *dfs_sha256_bytes;
  68. struct dentry *dfs_sha512_ops, *dfs_sha512_bytes;
  69. struct dentry *dfs_errors, *dfs_last_error, *dfs_last_error_pid;
  70. };
  71. struct nx_crypto_driver {
  72. struct nx_stats stats;
  73. struct nx_of of;
  74. struct vio_dev *viodev;
  75. struct vio_driver viodriver;
  76. struct nx_debugfs dfs;
  77. };
  78. #define NX_GCM4106_NONCE_LEN (4)
  79. #define NX_GCM_CTR_OFFSET (12)
  80. struct nx_gcm_rctx {
  81. u8 iv[16];
  82. };
  83. struct nx_gcm_priv {
  84. u8 iauth_tag[16];
  85. u8 nonce[NX_GCM4106_NONCE_LEN];
  86. };
  87. #define NX_CCM_AES_KEY_LEN (16)
  88. #define NX_CCM4309_AES_KEY_LEN (19)
  89. #define NX_CCM4309_NONCE_LEN (3)
  90. struct nx_ccm_rctx {
  91. u8 iv[16];
  92. };
  93. struct nx_ccm_priv {
  94. u8 b0[16];
  95. u8 iauth_tag[16];
  96. u8 oauth_tag[16];
  97. u8 nonce[NX_CCM4309_NONCE_LEN];
  98. };
  99. struct nx_xcbc_priv {
  100. u8 key[16];
  101. };
  102. struct nx_ctr_priv {
  103. u8 nonce[CTR_RFC3686_NONCE_SIZE];
  104. };
  105. struct nx_crypto_ctx {
  106. spinlock_t lock; /* synchronize access to the context */
  107. void *kmem; /* unaligned, kmalloc'd buffer */
  108. size_t kmem_len; /* length of kmem */
  109. struct nx_csbcpb *csbcpb; /* aligned page given to phyp @ hcall time */
  110. struct vio_pfo_op op; /* operation struct with hcall parameters */
  111. struct nx_csbcpb *csbcpb_aead; /* secondary csbcpb used by AEAD algs */
  112. struct vio_pfo_op op_aead;/* operation struct for csbcpb_aead */
  113. struct nx_sg *in_sg; /* aligned pointer into kmem to an sg list */
  114. struct nx_sg *out_sg; /* aligned pointer into kmem to an sg list */
  115. struct alg_props *ap; /* pointer into props based on our key size */
  116. struct alg_props props[3];/* openFirmware properties for requests */
  117. struct nx_stats *stats; /* pointer into an nx_crypto_driver for stats
  118. reporting */
  119. union {
  120. struct nx_gcm_priv gcm;
  121. struct nx_ccm_priv ccm;
  122. struct nx_xcbc_priv xcbc;
  123. struct nx_ctr_priv ctr;
  124. } priv;
  125. };
  126. struct crypto_aead;
  127. /* prototypes */
  128. int nx_crypto_ctx_aes_ccm_init(struct crypto_aead *tfm);
  129. int nx_crypto_ctx_aes_gcm_init(struct crypto_aead *tfm);
  130. int nx_crypto_ctx_aes_xcbc_init(struct crypto_tfm *tfm);
  131. int nx_crypto_ctx_aes_ctr_init(struct crypto_tfm *tfm);
  132. int nx_crypto_ctx_aes_cbc_init(struct crypto_tfm *tfm);
  133. int nx_crypto_ctx_aes_ecb_init(struct crypto_tfm *tfm);
  134. int nx_crypto_ctx_sha_init(struct crypto_tfm *tfm);
  135. void nx_crypto_ctx_exit(struct crypto_tfm *tfm);
  136. void nx_crypto_ctx_aead_exit(struct crypto_aead *tfm);
  137. void nx_ctx_init(struct nx_crypto_ctx *nx_ctx, unsigned int function);
  138. int nx_hcall_sync(struct nx_crypto_ctx *ctx, struct vio_pfo_op *op,
  139. u32 may_sleep);
  140. struct nx_sg *nx_build_sg_list(struct nx_sg *, u8 *, unsigned int *, u32);
  141. int nx_build_sg_lists(struct nx_crypto_ctx *, struct blkcipher_desc *,
  142. struct scatterlist *, struct scatterlist *, unsigned int *,
  143. unsigned int, u8 *);
  144. struct nx_sg *nx_walk_and_build(struct nx_sg *, unsigned int,
  145. struct scatterlist *, unsigned int,
  146. unsigned int *);
  147. #ifdef CONFIG_DEBUG_FS
  148. #define NX_DEBUGFS_INIT(drv) nx_debugfs_init(drv)
  149. #define NX_DEBUGFS_FINI(drv) nx_debugfs_fini(drv)
  150. int nx_debugfs_init(struct nx_crypto_driver *);
  151. void nx_debugfs_fini(struct nx_crypto_driver *);
  152. #else
  153. #define NX_DEBUGFS_INIT(drv) (0)
  154. #define NX_DEBUGFS_FINI(drv) (0)
  155. #endif
  156. #define NX_PAGE_NUM(x) ((u64)(x) & 0xfffffffffffff000ULL)
  157. extern struct crypto_alg nx_cbc_aes_alg;
  158. extern struct crypto_alg nx_ecb_aes_alg;
  159. extern struct aead_alg nx_gcm_aes_alg;
  160. extern struct aead_alg nx_gcm4106_aes_alg;
  161. extern struct crypto_alg nx_ctr3686_aes_alg;
  162. extern struct aead_alg nx_ccm_aes_alg;
  163. extern struct aead_alg nx_ccm4309_aes_alg;
  164. extern struct shash_alg nx_shash_aes_xcbc_alg;
  165. extern struct shash_alg nx_shash_sha512_alg;
  166. extern struct shash_alg nx_shash_sha256_alg;
  167. extern struct nx_crypto_driver nx_driver;
  168. #define SCATTERWALK_TO_SG 1
  169. #define SCATTERWALK_FROM_SG 0
  170. #endif