blob: 9347878d4f30c13a06ee6e781e72f1c04ef1d7c2 [file] [log] [blame]
Kent Yoderae0222b2012-05-14 10:59:38 +00001
2#ifndef __NX_H__
3#define __NX_H__
4
Herbert Xu030f4e92015-07-07 17:30:25 +08005#include <crypto/ctr.h>
6
Kent Yoderae0222b2012-05-14 10:59:38 +00007#define NX_NAME "nx-crypto"
8#define NX_STRING "IBM Power7+ Nest Accelerator Crypto Driver"
9#define NX_VERSION "1.0"
10
11static const char nx_driver_string[] = NX_STRING;
12static const char nx_driver_version[] = NX_VERSION;
13
14/* a scatterlist in the format PHYP is expecting */
15struct nx_sg {
16 u64 addr;
17 u32 rsvd;
18 u32 len;
19} __attribute((packed));
20
21#define NX_PAGE_SIZE (4096)
22#define NX_MAX_SG_ENTRIES (NX_PAGE_SIZE/(sizeof(struct nx_sg)))
23
24enum nx_status {
25 NX_DISABLED,
26 NX_WAITING,
27 NX_OKAY
28};
29
30/* msc_triplet and max_sync_cop are used only to assist in parsing the
31 * openFirmware property */
32struct msc_triplet {
33 u32 keybitlen;
34 u32 databytelen;
35 u32 sglen;
36} __packed;
37
38struct max_sync_cop {
39 u32 fc;
40 u32 mode;
41 u32 triplets;
42 struct msc_triplet trip[0];
43} __packed;
44
45struct alg_props {
46 u32 databytelen;
47 u32 sglen;
48};
49
50#define NX_OF_FLAG_MAXSGLEN_SET (1)
51#define NX_OF_FLAG_STATUS_SET (2)
52#define NX_OF_FLAG_MAXSYNCCOP_SET (4)
53#define NX_OF_FLAG_MASK_READY (NX_OF_FLAG_MAXSGLEN_SET | \
54 NX_OF_FLAG_STATUS_SET | \
55 NX_OF_FLAG_MAXSYNCCOP_SET)
56struct nx_of {
57 u32 flags;
58 u32 max_sg_len;
59 enum nx_status status;
60 struct alg_props ap[NX_MAX_FC][NX_MAX_MODE][3];
61};
62
63struct nx_stats {
64 atomic_t aes_ops;
65 atomic64_t aes_bytes;
66 atomic_t sha256_ops;
67 atomic64_t sha256_bytes;
68 atomic_t sha512_ops;
69 atomic64_t sha512_bytes;
70
71 atomic_t sync_ops;
72
73 atomic_t errors;
74 atomic_t last_error;
75 atomic_t last_error_pid;
76};
77
78struct nx_debugfs {
79 struct dentry *dfs_root;
80 struct dentry *dfs_aes_ops, *dfs_aes_bytes;
81 struct dentry *dfs_sha256_ops, *dfs_sha256_bytes;
82 struct dentry *dfs_sha512_ops, *dfs_sha512_bytes;
83 struct dentry *dfs_errors, *dfs_last_error, *dfs_last_error_pid;
84};
85
86struct nx_crypto_driver {
87 struct nx_stats stats;
88 struct nx_of of;
89 struct vio_dev *viodev;
90 struct vio_driver viodriver;
91 struct nx_debugfs dfs;
92};
93
94#define NX_GCM4106_NONCE_LEN (4)
95#define NX_GCM_CTR_OFFSET (12)
Herbert Xu030f4e92015-07-07 17:30:25 +080096struct nx_gcm_rctx {
Kent Yoderae0222b2012-05-14 10:59:38 +000097 u8 iv[16];
Herbert Xu030f4e92015-07-07 17:30:25 +080098};
99
100struct nx_gcm_priv {
Kent Yoderae0222b2012-05-14 10:59:38 +0000101 u8 iauth_tag[16];
102 u8 nonce[NX_GCM4106_NONCE_LEN];
103};
104
105#define NX_CCM_AES_KEY_LEN (16)
106#define NX_CCM4309_AES_KEY_LEN (19)
107#define NX_CCM4309_NONCE_LEN (3)
Herbert Xu030f4e92015-07-07 17:30:25 +0800108struct nx_ccm_rctx {
Kent Yoderae0222b2012-05-14 10:59:38 +0000109 u8 iv[16];
Herbert Xu030f4e92015-07-07 17:30:25 +0800110};
111
112struct nx_ccm_priv {
Kent Yoderae0222b2012-05-14 10:59:38 +0000113 u8 b0[16];
114 u8 iauth_tag[16];
115 u8 oauth_tag[16];
116 u8 nonce[NX_CCM4309_NONCE_LEN];
117};
118
119struct nx_xcbc_priv {
120 u8 key[16];
121};
122
123struct nx_ctr_priv {
Herbert Xu030f4e92015-07-07 17:30:25 +0800124 u8 nonce[CTR_RFC3686_NONCE_SIZE];
Kent Yoderae0222b2012-05-14 10:59:38 +0000125};
126
127struct nx_crypto_ctx {
Marcelo Cerric8491632013-08-12 18:49:37 -0300128 spinlock_t lock; /* synchronize access to the context */
Kent Yoderae0222b2012-05-14 10:59:38 +0000129 void *kmem; /* unaligned, kmalloc'd buffer */
130 size_t kmem_len; /* length of kmem */
131 struct nx_csbcpb *csbcpb; /* aligned page given to phyp @ hcall time */
132 struct vio_pfo_op op; /* operation struct with hcall parameters */
133 struct nx_csbcpb *csbcpb_aead; /* secondary csbcpb used by AEAD algs */
134 struct vio_pfo_op op_aead;/* operation struct for csbcpb_aead */
135
136 struct nx_sg *in_sg; /* aligned pointer into kmem to an sg list */
137 struct nx_sg *out_sg; /* aligned pointer into kmem to an sg list */
138
139 struct alg_props *ap; /* pointer into props based on our key size */
140 struct alg_props props[3];/* openFirmware properties for requests */
141 struct nx_stats *stats; /* pointer into an nx_crypto_driver for stats
142 reporting */
143
144 union {
145 struct nx_gcm_priv gcm;
146 struct nx_ccm_priv ccm;
147 struct nx_xcbc_priv xcbc;
148 struct nx_ctr_priv ctr;
149 } priv;
150};
151
Herbert Xu9129c262015-08-17 18:04:17 +0800152struct crypto_aead;
153
Kent Yoderae0222b2012-05-14 10:59:38 +0000154/* prototypes */
Herbert Xucc815652015-07-14 16:53:21 +0800155int nx_crypto_ctx_aes_ccm_init(struct crypto_aead *tfm);
Herbert Xu201f28f2015-06-16 13:54:21 +0800156int nx_crypto_ctx_aes_gcm_init(struct crypto_aead *tfm);
Kent Yoderae0222b2012-05-14 10:59:38 +0000157int nx_crypto_ctx_aes_xcbc_init(struct crypto_tfm *tfm);
158int nx_crypto_ctx_aes_ctr_init(struct crypto_tfm *tfm);
159int nx_crypto_ctx_aes_cbc_init(struct crypto_tfm *tfm);
160int nx_crypto_ctx_aes_ecb_init(struct crypto_tfm *tfm);
161int nx_crypto_ctx_sha_init(struct crypto_tfm *tfm);
162void nx_crypto_ctx_exit(struct crypto_tfm *tfm);
Herbert Xu201f28f2015-06-16 13:54:21 +0800163void nx_crypto_ctx_aead_exit(struct crypto_aead *tfm);
Kent Yoderae0222b2012-05-14 10:59:38 +0000164void nx_ctx_init(struct nx_crypto_ctx *nx_ctx, unsigned int function);
165int nx_hcall_sync(struct nx_crypto_ctx *ctx, struct vio_pfo_op *op,
166 u32 may_sleep);
Leonidas S. Barbosaf1294302014-10-28 15:50:45 -0200167struct nx_sg *nx_build_sg_list(struct nx_sg *, u8 *, unsigned int *, u32);
Kent Yoderae0222b2012-05-14 10:59:38 +0000168int nx_build_sg_lists(struct nx_crypto_ctx *, struct blkcipher_desc *,
Leonidas S. Barbosaf1294302014-10-28 15:50:45 -0200169 struct scatterlist *, struct scatterlist *, unsigned int *,
Marcelo Cerria8fc3912013-08-29 11:36:31 -0300170 unsigned int, u8 *);
Kent Yoderae0222b2012-05-14 10:59:38 +0000171struct nx_sg *nx_walk_and_build(struct nx_sg *, unsigned int,
172 struct scatterlist *, unsigned int,
Leonidas S. Barbosaf1294302014-10-28 15:50:45 -0200173 unsigned int *);
Kent Yoderae0222b2012-05-14 10:59:38 +0000174
175#ifdef CONFIG_DEBUG_FS
176#define NX_DEBUGFS_INIT(drv) nx_debugfs_init(drv)
177#define NX_DEBUGFS_FINI(drv) nx_debugfs_fini(drv)
178
179int nx_debugfs_init(struct nx_crypto_driver *);
180void nx_debugfs_fini(struct nx_crypto_driver *);
181#else
182#define NX_DEBUGFS_INIT(drv) (0)
183#define NX_DEBUGFS_FINI(drv) (0)
184#endif
185
186#define NX_PAGE_NUM(x) ((u64)(x) & 0xfffffffffffff000ULL)
187
188extern struct crypto_alg nx_cbc_aes_alg;
189extern struct crypto_alg nx_ecb_aes_alg;
Herbert Xu201f28f2015-06-16 13:54:21 +0800190extern struct aead_alg nx_gcm_aes_alg;
191extern struct aead_alg nx_gcm4106_aes_alg;
Kent Yoderae0222b2012-05-14 10:59:38 +0000192extern struct crypto_alg nx_ctr3686_aes_alg;
Herbert Xucc815652015-07-14 16:53:21 +0800193extern struct aead_alg nx_ccm_aes_alg;
194extern struct aead_alg nx_ccm4309_aes_alg;
Kent Yoderae0222b2012-05-14 10:59:38 +0000195extern struct shash_alg nx_shash_aes_xcbc_alg;
196extern struct shash_alg nx_shash_sha512_alg;
197extern struct shash_alg nx_shash_sha256_alg;
198
199extern struct nx_crypto_driver nx_driver;
200
201#define SCATTERWALK_TO_SG 1
202#define SCATTERWALK_FROM_SG 0
203
204#endif