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