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