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