nx.h 5.3 KB

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