Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 
0003 #ifndef __NX_H__
0004 #define __NX_H__
0005 
0006 #include <crypto/ctr.h>
0007 
0008 #define NX_NAME     "nx-crypto"
0009 #define NX_STRING   "IBM Power7+ Nest Accelerator Crypto Driver"
0010 #define NX_VERSION  "1.0"
0011 
0012 /* a scatterlist in the format PHYP is expecting */
0013 struct nx_sg {
0014     u64 addr;
0015     u32 rsvd;
0016     u32 len;
0017 } __attribute((packed));
0018 
0019 #define NX_PAGE_SIZE        (4096)
0020 #define NX_MAX_SG_ENTRIES   (NX_PAGE_SIZE/(sizeof(struct nx_sg)))
0021 
0022 enum nx_status {
0023     NX_DISABLED,
0024     NX_WAITING,
0025     NX_OKAY
0026 };
0027 
0028 /* msc_triplet and max_sync_cop are used only to assist in parsing the
0029  * openFirmware property */
0030 struct msc_triplet {
0031     u32 keybitlen;
0032     u32 databytelen;
0033     u32 sglen;
0034 } __packed;
0035 
0036 struct max_sync_cop {
0037     u32 fc;
0038     u32 mode;
0039     u32 triplets;
0040     struct msc_triplet trip[];
0041 } __packed;
0042 
0043 struct alg_props {
0044     u32 databytelen;
0045     u32 sglen;
0046 };
0047 
0048 #define NX_OF_FLAG_MAXSGLEN_SET     (1)
0049 #define NX_OF_FLAG_STATUS_SET       (2)
0050 #define NX_OF_FLAG_MAXSYNCCOP_SET   (4)
0051 #define NX_OF_FLAG_MASK_READY       (NX_OF_FLAG_MAXSGLEN_SET | \
0052                      NX_OF_FLAG_STATUS_SET |   \
0053                      NX_OF_FLAG_MAXSYNCCOP_SET)
0054 struct nx_of {
0055     u32 flags;
0056     u32 max_sg_len;
0057     enum nx_status status;
0058     struct alg_props ap[NX_MAX_FC][NX_MAX_MODE][3];
0059 };
0060 
0061 struct nx_stats {
0062     atomic_t aes_ops;
0063     atomic64_t aes_bytes;
0064     atomic_t sha256_ops;
0065     atomic64_t sha256_bytes;
0066     atomic_t sha512_ops;
0067     atomic64_t sha512_bytes;
0068 
0069     atomic_t sync_ops;
0070 
0071     atomic_t errors;
0072     atomic_t last_error;
0073     atomic_t last_error_pid;
0074 };
0075 
0076 struct nx_crypto_driver {
0077     struct nx_stats    stats;
0078     struct nx_of       of;
0079     struct vio_dev    *viodev;
0080     struct vio_driver  viodriver;
0081     struct dentry     *dfs_root;
0082 };
0083 
0084 #define NX_GCM4106_NONCE_LEN        (4)
0085 #define NX_GCM_CTR_OFFSET       (12)
0086 struct nx_gcm_rctx {
0087     u8 iv[16];
0088 };
0089 
0090 struct nx_gcm_priv {
0091     u8 iauth_tag[16];
0092     u8 nonce[NX_GCM4106_NONCE_LEN];
0093 };
0094 
0095 #define NX_CCM_AES_KEY_LEN      (16)
0096 #define NX_CCM4309_AES_KEY_LEN      (19)
0097 #define NX_CCM4309_NONCE_LEN        (3)
0098 struct nx_ccm_rctx {
0099     u8 iv[16];
0100 };
0101 
0102 struct nx_ccm_priv {
0103     u8 b0[16];
0104     u8 iauth_tag[16];
0105     u8 oauth_tag[16];
0106     u8 nonce[NX_CCM4309_NONCE_LEN];
0107 };
0108 
0109 struct nx_xcbc_priv {
0110     u8 key[16];
0111 };
0112 
0113 struct nx_ctr_priv {
0114     u8 nonce[CTR_RFC3686_NONCE_SIZE];
0115 };
0116 
0117 struct nx_crypto_ctx {
0118     spinlock_t lock;      /* synchronize access to the context */
0119     void *kmem;       /* unaligned, kmalloc'd buffer */
0120     size_t kmem_len;      /* length of kmem */
0121     struct nx_csbcpb *csbcpb; /* aligned page given to phyp @ hcall time */
0122     struct vio_pfo_op op;     /* operation struct with hcall parameters */
0123     struct nx_csbcpb *csbcpb_aead; /* secondary csbcpb used by AEAD algs */
0124     struct vio_pfo_op op_aead;/* operation struct for csbcpb_aead */
0125 
0126     struct nx_sg *in_sg;      /* aligned pointer into kmem to an sg list */
0127     struct nx_sg *out_sg;     /* aligned pointer into kmem to an sg list */
0128 
0129     struct alg_props *ap;     /* pointer into props based on our key size */
0130     struct alg_props props[3];/* openFirmware properties for requests */
0131     struct nx_stats *stats;   /* pointer into an nx_crypto_driver for stats
0132                      reporting */
0133 
0134     union {
0135         struct nx_gcm_priv gcm;
0136         struct nx_ccm_priv ccm;
0137         struct nx_xcbc_priv xcbc;
0138         struct nx_ctr_priv ctr;
0139     } priv;
0140 };
0141 
0142 struct crypto_aead;
0143 
0144 /* prototypes */
0145 int nx_crypto_ctx_aes_ccm_init(struct crypto_aead *tfm);
0146 int nx_crypto_ctx_aes_gcm_init(struct crypto_aead *tfm);
0147 int nx_crypto_ctx_aes_xcbc_init(struct crypto_tfm *tfm);
0148 int nx_crypto_ctx_aes_ctr_init(struct crypto_skcipher *tfm);
0149 int nx_crypto_ctx_aes_cbc_init(struct crypto_skcipher *tfm);
0150 int nx_crypto_ctx_aes_ecb_init(struct crypto_skcipher *tfm);
0151 int nx_crypto_ctx_sha_init(struct crypto_tfm *tfm);
0152 void nx_crypto_ctx_exit(struct crypto_tfm *tfm);
0153 void nx_crypto_ctx_skcipher_exit(struct crypto_skcipher *tfm);
0154 void nx_crypto_ctx_aead_exit(struct crypto_aead *tfm);
0155 void nx_ctx_init(struct nx_crypto_ctx *nx_ctx, unsigned int function);
0156 int nx_hcall_sync(struct nx_crypto_ctx *ctx, struct vio_pfo_op *op,
0157           u32 may_sleep);
0158 struct nx_sg *nx_build_sg_list(struct nx_sg *, u8 *, unsigned int *, u32);
0159 int nx_build_sg_lists(struct nx_crypto_ctx *nx_ctx, const u8 *iv,
0160               struct scatterlist *dst, struct scatterlist *src,
0161               unsigned int *nbytes, unsigned int offset, u8 *oiv);
0162 struct nx_sg *nx_walk_and_build(struct nx_sg *, unsigned int,
0163                 struct scatterlist *, unsigned int,
0164                 unsigned int *);
0165 
0166 #ifdef CONFIG_DEBUG_FS
0167 #define NX_DEBUGFS_INIT(drv)    nx_debugfs_init(drv)
0168 #define NX_DEBUGFS_FINI(drv)    nx_debugfs_fini(drv)
0169 
0170 void nx_debugfs_init(struct nx_crypto_driver *);
0171 void nx_debugfs_fini(struct nx_crypto_driver *);
0172 #else
0173 #define NX_DEBUGFS_INIT(drv)    (0)
0174 #define NX_DEBUGFS_FINI(drv)    (0)
0175 #endif
0176 
0177 #define NX_PAGE_NUM(x)      ((u64)(x) & 0xfffffffffffff000ULL)
0178 
0179 extern struct skcipher_alg nx_cbc_aes_alg;
0180 extern struct skcipher_alg nx_ecb_aes_alg;
0181 extern struct aead_alg nx_gcm_aes_alg;
0182 extern struct aead_alg nx_gcm4106_aes_alg;
0183 extern struct skcipher_alg nx_ctr3686_aes_alg;
0184 extern struct aead_alg nx_ccm_aes_alg;
0185 extern struct aead_alg nx_ccm4309_aes_alg;
0186 extern struct shash_alg nx_shash_aes_xcbc_alg;
0187 extern struct shash_alg nx_shash_sha512_alg;
0188 extern struct shash_alg nx_shash_sha256_alg;
0189 
0190 extern struct nx_crypto_driver nx_driver;
0191 
0192 #define SCATTERWALK_TO_SG   1
0193 #define SCATTERWALK_FROM_SG 0
0194 
0195 #endif