11bc7fdbfSTom Zanussi // SPDX-License-Identifier: GPL-2.0-only
21bc7fdbfSTom Zanussi /*
31bc7fdbfSTom Zanussi  * Intel IXP4xx NPE-C crypto driver
41bc7fdbfSTom Zanussi  *
51bc7fdbfSTom Zanussi  * Copyright (C) 2008 Christian Hohnstaedt <chohnstaedt@innominate.com>
61bc7fdbfSTom Zanussi  */
71bc7fdbfSTom Zanussi 
81bc7fdbfSTom Zanussi #include <linux/platform_device.h>
91bc7fdbfSTom Zanussi #include <linux/dma-mapping.h>
101bc7fdbfSTom Zanussi #include <linux/dmapool.h>
111bc7fdbfSTom Zanussi #include <linux/crypto.h>
121bc7fdbfSTom Zanussi #include <linux/kernel.h>
131bc7fdbfSTom Zanussi #include <linux/rtnetlink.h>
141bc7fdbfSTom Zanussi #include <linux/interrupt.h>
151bc7fdbfSTom Zanussi #include <linux/spinlock.h>
161bc7fdbfSTom Zanussi #include <linux/gfp.h>
171bc7fdbfSTom Zanussi #include <linux/module.h>
181bc7fdbfSTom Zanussi #include <linux/of.h>
191bc7fdbfSTom Zanussi 
201bc7fdbfSTom Zanussi #include <crypto/ctr.h>
211bc7fdbfSTom Zanussi #include <crypto/internal/des.h>
221bc7fdbfSTom Zanussi #include <crypto/aes.h>
231bc7fdbfSTom Zanussi #include <crypto/hmac.h>
241bc7fdbfSTom Zanussi #include <crypto/sha1.h>
251bc7fdbfSTom Zanussi #include <crypto/algapi.h>
261bc7fdbfSTom Zanussi #include <crypto/internal/aead.h>
271bc7fdbfSTom Zanussi #include <crypto/internal/skcipher.h>
281bc7fdbfSTom Zanussi #include <crypto/authenc.h>
291bc7fdbfSTom Zanussi #include <crypto/scatterwalk.h>
301bc7fdbfSTom Zanussi 
311bc7fdbfSTom Zanussi #include <linux/soc/ixp4xx/npe.h>
321bc7fdbfSTom Zanussi #include <linux/soc/ixp4xx/qmgr.h>
331bc7fdbfSTom Zanussi 
341bc7fdbfSTom Zanussi /* Intermittent includes, delete this after v5.14-rc1 */
351bc7fdbfSTom Zanussi #include <linux/soc/ixp4xx/cpu.h>
361bc7fdbfSTom Zanussi 
371bc7fdbfSTom Zanussi #define MAX_KEYLEN 32
381bc7fdbfSTom Zanussi 
391bc7fdbfSTom Zanussi /* hash: cfgword + 2 * digestlen; crypt: keylen + cfgword */
401bc7fdbfSTom Zanussi #define NPE_CTX_LEN 80
411bc7fdbfSTom Zanussi #define AES_BLOCK128 16
421bc7fdbfSTom Zanussi 
431bc7fdbfSTom Zanussi #define NPE_OP_HASH_VERIFY   0x01
441bc7fdbfSTom Zanussi #define NPE_OP_CCM_ENABLE    0x04
451bc7fdbfSTom Zanussi #define NPE_OP_CRYPT_ENABLE  0x08
461bc7fdbfSTom Zanussi #define NPE_OP_HASH_ENABLE   0x10
471bc7fdbfSTom Zanussi #define NPE_OP_NOT_IN_PLACE  0x20
481bc7fdbfSTom Zanussi #define NPE_OP_HMAC_DISABLE  0x40
491bc7fdbfSTom Zanussi #define NPE_OP_CRYPT_ENCRYPT 0x80
501bc7fdbfSTom Zanussi 
511bc7fdbfSTom Zanussi #define NPE_OP_CCM_GEN_MIC   0xcc
521bc7fdbfSTom Zanussi #define NPE_OP_HASH_GEN_ICV  0x50
531bc7fdbfSTom Zanussi #define NPE_OP_ENC_GEN_KEY   0xc9
541bc7fdbfSTom Zanussi 
551bc7fdbfSTom Zanussi #define MOD_ECB     0x0000
561bc7fdbfSTom Zanussi #define MOD_CTR     0x1000
571bc7fdbfSTom Zanussi #define MOD_CBC_ENC 0x2000
581bc7fdbfSTom Zanussi #define MOD_CBC_DEC 0x3000
591bc7fdbfSTom Zanussi #define MOD_CCM_ENC 0x4000
601bc7fdbfSTom Zanussi #define MOD_CCM_DEC 0x5000
611bc7fdbfSTom Zanussi 
621bc7fdbfSTom Zanussi #define KEYLEN_128  4
631bc7fdbfSTom Zanussi #define KEYLEN_192  6
641bc7fdbfSTom Zanussi #define KEYLEN_256  8
651bc7fdbfSTom Zanussi 
661bc7fdbfSTom Zanussi #define CIPH_DECR   0x0000
671bc7fdbfSTom Zanussi #define CIPH_ENCR   0x0400
681bc7fdbfSTom Zanussi 
691bc7fdbfSTom Zanussi #define MOD_DES     0x0000
701bc7fdbfSTom Zanussi #define MOD_TDEA2   0x0100
711bc7fdbfSTom Zanussi #define MOD_3DES   0x0200
721bc7fdbfSTom Zanussi #define MOD_AES     0x0800
731bc7fdbfSTom Zanussi #define MOD_AES128  (0x0800 | KEYLEN_128)
741bc7fdbfSTom Zanussi #define MOD_AES192  (0x0900 | KEYLEN_192)
751bc7fdbfSTom Zanussi #define MOD_AES256  (0x0a00 | KEYLEN_256)
761bc7fdbfSTom Zanussi 
771bc7fdbfSTom Zanussi #define MAX_IVLEN   16
781bc7fdbfSTom Zanussi #define NPE_QLEN    16
791bc7fdbfSTom Zanussi /* Space for registering when the first
801bc7fdbfSTom Zanussi  * NPE_QLEN crypt_ctl are busy */
811bc7fdbfSTom Zanussi #define NPE_QLEN_TOTAL 64
821bc7fdbfSTom Zanussi 
831bc7fdbfSTom Zanussi #define CTL_FLAG_UNUSED		0x0000
841bc7fdbfSTom Zanussi #define CTL_FLAG_USED		0x1000
851bc7fdbfSTom Zanussi #define CTL_FLAG_PERFORM_ABLK	0x0001
861bc7fdbfSTom Zanussi #define CTL_FLAG_GEN_ICV	0x0002
871bc7fdbfSTom Zanussi #define CTL_FLAG_GEN_REVAES	0x0004
881bc7fdbfSTom Zanussi #define CTL_FLAG_PERFORM_AEAD	0x0008
891bc7fdbfSTom Zanussi #define CTL_FLAG_MASK		0x000f
901bc7fdbfSTom Zanussi 
911bc7fdbfSTom Zanussi #define HMAC_PAD_BLOCKLEN SHA1_BLOCK_SIZE
921bc7fdbfSTom Zanussi 
931bc7fdbfSTom Zanussi #define MD5_DIGEST_SIZE   16
941bc7fdbfSTom Zanussi 
951bc7fdbfSTom Zanussi struct buffer_desc {
961bc7fdbfSTom Zanussi 	u32 phys_next;
971bc7fdbfSTom Zanussi #ifdef __ARMEB__
981bc7fdbfSTom Zanussi 	u16 buf_len;
991bc7fdbfSTom Zanussi 	u16 pkt_len;
1001bc7fdbfSTom Zanussi #else
1011bc7fdbfSTom Zanussi 	u16 pkt_len;
1021bc7fdbfSTom Zanussi 	u16 buf_len;
1031bc7fdbfSTom Zanussi #endif
1041bc7fdbfSTom Zanussi 	dma_addr_t phys_addr;
1051bc7fdbfSTom Zanussi 	u32 __reserved[4];
1061bc7fdbfSTom Zanussi 	struct buffer_desc *next;
1071bc7fdbfSTom Zanussi 	enum dma_data_direction dir;
1081bc7fdbfSTom Zanussi };
1091bc7fdbfSTom Zanussi 
1101bc7fdbfSTom Zanussi struct crypt_ctl {
1111bc7fdbfSTom Zanussi #ifdef __ARMEB__
1121bc7fdbfSTom Zanussi 	u8 mode;		/* NPE_OP_*  operation mode */
1131bc7fdbfSTom Zanussi 	u8 init_len;
1141bc7fdbfSTom Zanussi 	u16 reserved;
1151bc7fdbfSTom Zanussi #else
1161bc7fdbfSTom Zanussi 	u16 reserved;
1171bc7fdbfSTom Zanussi 	u8 init_len;
1181bc7fdbfSTom Zanussi 	u8 mode;		/* NPE_OP_*  operation mode */
1191bc7fdbfSTom Zanussi #endif
1201bc7fdbfSTom Zanussi 	u8 iv[MAX_IVLEN];	/* IV for CBC mode or CTR IV for CTR mode */
121586d492fSArnd Bergmann 	u32 icv_rev_aes;	/* icv or rev aes */
122586d492fSArnd Bergmann 	u32 src_buf;
123586d492fSArnd Bergmann 	u32 dst_buf;
1241bc7fdbfSTom Zanussi #ifdef __ARMEB__
1251bc7fdbfSTom Zanussi 	u16 auth_offs;		/* Authentication start offset */
1261bc7fdbfSTom Zanussi 	u16 auth_len;		/* Authentication data length */
1271bc7fdbfSTom Zanussi 	u16 crypt_offs;		/* Cryption start offset */
1281bc7fdbfSTom Zanussi 	u16 crypt_len;		/* Cryption data length */
1291bc7fdbfSTom Zanussi #else
1301bc7fdbfSTom Zanussi 	u16 auth_len;		/* Authentication data length */
1311bc7fdbfSTom Zanussi 	u16 auth_offs;		/* Authentication start offset */
1321bc7fdbfSTom Zanussi 	u16 crypt_len;		/* Cryption data length */
1331bc7fdbfSTom Zanussi 	u16 crypt_offs;		/* Cryption start offset */
1341bc7fdbfSTom Zanussi #endif
1351bc7fdbfSTom Zanussi 	u32 aadAddr;		/* Additional Auth Data Addr for CCM mode */
1361bc7fdbfSTom Zanussi 	u32 crypto_ctx;		/* NPE Crypto Param structure address */
1371bc7fdbfSTom Zanussi 
1381bc7fdbfSTom Zanussi 	/* Used by Host: 4*4 bytes*/
1391bc7fdbfSTom Zanussi 	unsigned int ctl_flags;
1401bc7fdbfSTom Zanussi 	union {
1411bc7fdbfSTom Zanussi 		struct skcipher_request *ablk_req;
1421bc7fdbfSTom Zanussi 		struct aead_request *aead_req;
1431bc7fdbfSTom Zanussi 		struct crypto_tfm *tfm;
1441bc7fdbfSTom Zanussi 	} data;
1451bc7fdbfSTom Zanussi 	struct buffer_desc *regist_buf;
1461bc7fdbfSTom Zanussi 	u8 *regist_ptr;
1471bc7fdbfSTom Zanussi };
1481bc7fdbfSTom Zanussi 
1491bc7fdbfSTom Zanussi struct ablk_ctx {
1501bc7fdbfSTom Zanussi 	struct buffer_desc *src;
1511bc7fdbfSTom Zanussi 	struct buffer_desc *dst;
1521bc7fdbfSTom Zanussi 	u8 iv[MAX_IVLEN];
1531bc7fdbfSTom Zanussi 	bool encrypt;
1541bc7fdbfSTom Zanussi 	struct skcipher_request fallback_req;   // keep at the end
1551bc7fdbfSTom Zanussi };
1561bc7fdbfSTom Zanussi 
1571bc7fdbfSTom Zanussi struct aead_ctx {
1581bc7fdbfSTom Zanussi 	struct buffer_desc *src;
1591bc7fdbfSTom Zanussi 	struct buffer_desc *dst;
1601bc7fdbfSTom Zanussi 	struct scatterlist ivlist;
1611bc7fdbfSTom Zanussi 	/* used when the hmac is not on one sg entry */
1621bc7fdbfSTom Zanussi 	u8 *hmac_virt;
1631bc7fdbfSTom Zanussi 	int encrypt;
1641bc7fdbfSTom Zanussi };
1651bc7fdbfSTom Zanussi 
1661bc7fdbfSTom Zanussi struct ix_hash_algo {
1671bc7fdbfSTom Zanussi 	u32 cfgword;
1681bc7fdbfSTom Zanussi 	unsigned char *icv;
1691bc7fdbfSTom Zanussi };
1701bc7fdbfSTom Zanussi 
1711bc7fdbfSTom Zanussi struct ix_sa_dir {
1721bc7fdbfSTom Zanussi 	unsigned char *npe_ctx;
1731bc7fdbfSTom Zanussi 	dma_addr_t npe_ctx_phys;
1741bc7fdbfSTom Zanussi 	int npe_ctx_idx;
1751bc7fdbfSTom Zanussi 	u8 npe_mode;
1761bc7fdbfSTom Zanussi };
1771bc7fdbfSTom Zanussi 
1781bc7fdbfSTom Zanussi struct ixp_ctx {
1791bc7fdbfSTom Zanussi 	struct ix_sa_dir encrypt;
1801bc7fdbfSTom Zanussi 	struct ix_sa_dir decrypt;
1811bc7fdbfSTom Zanussi 	int authkey_len;
1821bc7fdbfSTom Zanussi 	u8 authkey[MAX_KEYLEN];
1831bc7fdbfSTom Zanussi 	int enckey_len;
1841bc7fdbfSTom Zanussi 	u8 enckey[MAX_KEYLEN];
1851bc7fdbfSTom Zanussi 	u8 salt[MAX_IVLEN];
1861bc7fdbfSTom Zanussi 	u8 nonce[CTR_RFC3686_NONCE_SIZE];
1871bc7fdbfSTom Zanussi 	unsigned int salted;
1881bc7fdbfSTom Zanussi 	atomic_t configuring;
1891bc7fdbfSTom Zanussi 	struct completion completion;
1901bc7fdbfSTom Zanussi 	struct crypto_skcipher *fallback_tfm;
1911bc7fdbfSTom Zanussi };
1921bc7fdbfSTom Zanussi 
1931bc7fdbfSTom Zanussi struct ixp_alg {
1941bc7fdbfSTom Zanussi 	struct skcipher_alg crypto;
1951bc7fdbfSTom Zanussi 	const struct ix_hash_algo *hash;
1961bc7fdbfSTom Zanussi 	u32 cfg_enc;
1971bc7fdbfSTom Zanussi 	u32 cfg_dec;
1981bc7fdbfSTom Zanussi 
1991bc7fdbfSTom Zanussi 	int registered;
2001bc7fdbfSTom Zanussi };
2011bc7fdbfSTom Zanussi 
2021bc7fdbfSTom Zanussi struct ixp_aead_alg {
2031bc7fdbfSTom Zanussi 	struct aead_alg crypto;
2041bc7fdbfSTom Zanussi 	const struct ix_hash_algo *hash;
2051bc7fdbfSTom Zanussi 	u32 cfg_enc;
2061bc7fdbfSTom Zanussi 	u32 cfg_dec;
2071bc7fdbfSTom Zanussi 
2081bc7fdbfSTom Zanussi 	int registered;
2091bc7fdbfSTom Zanussi };
2101bc7fdbfSTom Zanussi 
2111bc7fdbfSTom Zanussi static const struct ix_hash_algo hash_alg_md5 = {
2121bc7fdbfSTom Zanussi 	.cfgword	= 0xAA010004,
2131bc7fdbfSTom Zanussi 	.icv		= "\x01\x23\x45\x67\x89\xAB\xCD\xEF"
2141bc7fdbfSTom Zanussi 			  "\xFE\xDC\xBA\x98\x76\x54\x32\x10",
2151bc7fdbfSTom Zanussi };
2161bc7fdbfSTom Zanussi 
2171bc7fdbfSTom Zanussi static const struct ix_hash_algo hash_alg_sha1 = {
2181bc7fdbfSTom Zanussi 	.cfgword	= 0x00000005,
2191bc7fdbfSTom Zanussi 	.icv		= "\x67\x45\x23\x01\xEF\xCD\xAB\x89\x98\xBA"
2201bc7fdbfSTom Zanussi 			  "\xDC\xFE\x10\x32\x54\x76\xC3\xD2\xE1\xF0",
2211bc7fdbfSTom Zanussi };
2221bc7fdbfSTom Zanussi 
2231bc7fdbfSTom Zanussi static struct npe *npe_c;
2241bc7fdbfSTom Zanussi 
2251bc7fdbfSTom Zanussi static unsigned int send_qid;
2261bc7fdbfSTom Zanussi static unsigned int recv_qid;
2271bc7fdbfSTom Zanussi static struct dma_pool *buffer_pool;
2281bc7fdbfSTom Zanussi static struct dma_pool *ctx_pool;
2291bc7fdbfSTom Zanussi 
2301bc7fdbfSTom Zanussi static struct crypt_ctl *crypt_virt;
2311bc7fdbfSTom Zanussi static dma_addr_t crypt_phys;
2321bc7fdbfSTom Zanussi 
2331bc7fdbfSTom Zanussi static int support_aes = 1;
2341bc7fdbfSTom Zanussi 
2351bc7fdbfSTom Zanussi static struct platform_device *pdev;
2361bc7fdbfSTom Zanussi 
crypt_virt2phys(struct crypt_ctl * virt)2371bc7fdbfSTom Zanussi static inline dma_addr_t crypt_virt2phys(struct crypt_ctl *virt)
2381bc7fdbfSTom Zanussi {
2391bc7fdbfSTom Zanussi 	return crypt_phys + (virt - crypt_virt) * sizeof(struct crypt_ctl);
2401bc7fdbfSTom Zanussi }
2411bc7fdbfSTom Zanussi 
crypt_phys2virt(dma_addr_t phys)2421bc7fdbfSTom Zanussi static inline struct crypt_ctl *crypt_phys2virt(dma_addr_t phys)
2431bc7fdbfSTom Zanussi {
2441bc7fdbfSTom Zanussi 	return crypt_virt + (phys - crypt_phys) / sizeof(struct crypt_ctl);
2451bc7fdbfSTom Zanussi }
2461bc7fdbfSTom Zanussi 
cipher_cfg_enc(struct crypto_tfm * tfm)2471bc7fdbfSTom Zanussi static inline u32 cipher_cfg_enc(struct crypto_tfm *tfm)
2481bc7fdbfSTom Zanussi {
2491bc7fdbfSTom Zanussi 	return container_of(tfm->__crt_alg, struct ixp_alg, crypto.base)->cfg_enc;
2501bc7fdbfSTom Zanussi }
2511bc7fdbfSTom Zanussi 
cipher_cfg_dec(struct crypto_tfm * tfm)2521bc7fdbfSTom Zanussi static inline u32 cipher_cfg_dec(struct crypto_tfm *tfm)
2531bc7fdbfSTom Zanussi {
2541bc7fdbfSTom Zanussi 	return container_of(tfm->__crt_alg, struct ixp_alg, crypto.base)->cfg_dec;
2551bc7fdbfSTom Zanussi }
2561bc7fdbfSTom Zanussi 
ix_hash(struct crypto_tfm * tfm)2571bc7fdbfSTom Zanussi static inline const struct ix_hash_algo *ix_hash(struct crypto_tfm *tfm)
2581bc7fdbfSTom Zanussi {
2591bc7fdbfSTom Zanussi 	return container_of(tfm->__crt_alg, struct ixp_alg, crypto.base)->hash;
2601bc7fdbfSTom Zanussi }
2611bc7fdbfSTom Zanussi 
setup_crypt_desc(void)2621bc7fdbfSTom Zanussi static int setup_crypt_desc(void)
2631bc7fdbfSTom Zanussi {
2641bc7fdbfSTom Zanussi 	struct device *dev = &pdev->dev;
2651bc7fdbfSTom Zanussi 
266586d492fSArnd Bergmann 	BUILD_BUG_ON(!(IS_ENABLED(CONFIG_COMPILE_TEST) &&
267586d492fSArnd Bergmann 		       IS_ENABLED(CONFIG_64BIT)) &&
2688ae8a494SHerbert Xu 		     sizeof(struct crypt_ctl) != 64);
2691bc7fdbfSTom Zanussi 	crypt_virt = dma_alloc_coherent(dev,
2701bc7fdbfSTom Zanussi 					NPE_QLEN * sizeof(struct crypt_ctl),
2711bc7fdbfSTom Zanussi 					&crypt_phys, GFP_ATOMIC);
2721bc7fdbfSTom Zanussi 	if (!crypt_virt)
2731bc7fdbfSTom Zanussi 		return -ENOMEM;
2741bc7fdbfSTom Zanussi 	return 0;
2751bc7fdbfSTom Zanussi }
2761bc7fdbfSTom Zanussi 
2771bc7fdbfSTom Zanussi static DEFINE_SPINLOCK(desc_lock);
get_crypt_desc(void)2781bc7fdbfSTom Zanussi static struct crypt_ctl *get_crypt_desc(void)
2791bc7fdbfSTom Zanussi {
2801bc7fdbfSTom Zanussi 	int i;
2811bc7fdbfSTom Zanussi 	static int idx;
2821bc7fdbfSTom Zanussi 	unsigned long flags;
2831bc7fdbfSTom Zanussi 
2841bc7fdbfSTom Zanussi 	spin_lock_irqsave(&desc_lock, flags);
2851bc7fdbfSTom Zanussi 
2861bc7fdbfSTom Zanussi 	if (unlikely(!crypt_virt))
2871bc7fdbfSTom Zanussi 		setup_crypt_desc();
2881bc7fdbfSTom Zanussi 	if (unlikely(!crypt_virt)) {
2891bc7fdbfSTom Zanussi 		spin_unlock_irqrestore(&desc_lock, flags);
2901bc7fdbfSTom Zanussi 		return NULL;
2911bc7fdbfSTom Zanussi 	}
2921bc7fdbfSTom Zanussi 	i = idx;
2931bc7fdbfSTom Zanussi 	if (crypt_virt[i].ctl_flags == CTL_FLAG_UNUSED) {
2941bc7fdbfSTom Zanussi 		if (++idx >= NPE_QLEN)
2951bc7fdbfSTom Zanussi 			idx = 0;
2961bc7fdbfSTom Zanussi 		crypt_virt[i].ctl_flags = CTL_FLAG_USED;
2971bc7fdbfSTom Zanussi 		spin_unlock_irqrestore(&desc_lock, flags);
2981bc7fdbfSTom Zanussi 		return crypt_virt + i;
2991bc7fdbfSTom Zanussi 	} else {
3001bc7fdbfSTom Zanussi 		spin_unlock_irqrestore(&desc_lock, flags);
3011bc7fdbfSTom Zanussi 		return NULL;
3021bc7fdbfSTom Zanussi 	}
3031bc7fdbfSTom Zanussi }
3041bc7fdbfSTom Zanussi 
3051bc7fdbfSTom Zanussi static DEFINE_SPINLOCK(emerg_lock);
get_crypt_desc_emerg(void)3061bc7fdbfSTom Zanussi static struct crypt_ctl *get_crypt_desc_emerg(void)
3071bc7fdbfSTom Zanussi {
3081bc7fdbfSTom Zanussi 	int i;
3091bc7fdbfSTom Zanussi 	static int idx = NPE_QLEN;
3101bc7fdbfSTom Zanussi 	struct crypt_ctl *desc;
3111bc7fdbfSTom Zanussi 	unsigned long flags;
3121bc7fdbfSTom Zanussi 
3131bc7fdbfSTom Zanussi 	desc = get_crypt_desc();
3141bc7fdbfSTom Zanussi 	if (desc)
3151bc7fdbfSTom Zanussi 		return desc;
3161bc7fdbfSTom Zanussi 	if (unlikely(!crypt_virt))
3171bc7fdbfSTom Zanussi 		return NULL;
3181bc7fdbfSTom Zanussi 
3191bc7fdbfSTom Zanussi 	spin_lock_irqsave(&emerg_lock, flags);
3201bc7fdbfSTom Zanussi 	i = idx;
3211bc7fdbfSTom Zanussi 	if (crypt_virt[i].ctl_flags == CTL_FLAG_UNUSED) {
3221bc7fdbfSTom Zanussi 		if (++idx >= NPE_QLEN_TOTAL)
3231bc7fdbfSTom Zanussi 			idx = NPE_QLEN;
3241bc7fdbfSTom Zanussi 		crypt_virt[i].ctl_flags = CTL_FLAG_USED;
3251bc7fdbfSTom Zanussi 		spin_unlock_irqrestore(&emerg_lock, flags);
3261bc7fdbfSTom Zanussi 		return crypt_virt + i;
3271bc7fdbfSTom Zanussi 	} else {
3281bc7fdbfSTom Zanussi 		spin_unlock_irqrestore(&emerg_lock, flags);
3291bc7fdbfSTom Zanussi 		return NULL;
3301bc7fdbfSTom Zanussi 	}
3311bc7fdbfSTom Zanussi }
3321bc7fdbfSTom Zanussi 
free_buf_chain(struct device * dev,struct buffer_desc * buf,dma_addr_t phys)3331bc7fdbfSTom Zanussi static void free_buf_chain(struct device *dev, struct buffer_desc *buf,
3341bc7fdbfSTom Zanussi 			   dma_addr_t phys)
3351bc7fdbfSTom Zanussi {
3361bc7fdbfSTom Zanussi 	while (buf) {
3371bc7fdbfSTom Zanussi 		struct buffer_desc *buf1;
3381bc7fdbfSTom Zanussi 		u32 phys1;
3391bc7fdbfSTom Zanussi 
3401bc7fdbfSTom Zanussi 		buf1 = buf->next;
3411bc7fdbfSTom Zanussi 		phys1 = buf->phys_next;
3421bc7fdbfSTom Zanussi 		dma_unmap_single(dev, buf->phys_addr, buf->buf_len, buf->dir);
3431bc7fdbfSTom Zanussi 		dma_pool_free(buffer_pool, buf, phys);
3441bc7fdbfSTom Zanussi 		buf = buf1;
3451bc7fdbfSTom Zanussi 		phys = phys1;
3461bc7fdbfSTom Zanussi 	}
3471bc7fdbfSTom Zanussi }
3481bc7fdbfSTom Zanussi 
3491bc7fdbfSTom Zanussi static struct tasklet_struct crypto_done_tasklet;
3501bc7fdbfSTom Zanussi 
finish_scattered_hmac(struct crypt_ctl * crypt)3511bc7fdbfSTom Zanussi static void finish_scattered_hmac(struct crypt_ctl *crypt)
3521bc7fdbfSTom Zanussi {
3531bc7fdbfSTom Zanussi 	struct aead_request *req = crypt->data.aead_req;
3541bc7fdbfSTom Zanussi 	struct aead_ctx *req_ctx = aead_request_ctx(req);
3551bc7fdbfSTom Zanussi 	struct crypto_aead *tfm = crypto_aead_reqtfm(req);
3561bc7fdbfSTom Zanussi 	int authsize = crypto_aead_authsize(tfm);
3571bc7fdbfSTom Zanussi 	int decryptlen = req->assoclen + req->cryptlen - authsize;
3581bc7fdbfSTom Zanussi 
3591bc7fdbfSTom Zanussi 	if (req_ctx->encrypt) {
3601bc7fdbfSTom Zanussi 		scatterwalk_map_and_copy(req_ctx->hmac_virt, req->dst,
3611bc7fdbfSTom Zanussi 					 decryptlen, authsize, 1);
3621bc7fdbfSTom Zanussi 	}
3631bc7fdbfSTom Zanussi 	dma_pool_free(buffer_pool, req_ctx->hmac_virt, crypt->icv_rev_aes);
3641bc7fdbfSTom Zanussi }
3651bc7fdbfSTom Zanussi 
one_packet(dma_addr_t phys)3661bc7fdbfSTom Zanussi static void one_packet(dma_addr_t phys)
3671bc7fdbfSTom Zanussi {
3681bc7fdbfSTom Zanussi 	struct device *dev = &pdev->dev;
3691bc7fdbfSTom Zanussi 	struct crypt_ctl *crypt;
3701bc7fdbfSTom Zanussi 	struct ixp_ctx *ctx;
3711bc7fdbfSTom Zanussi 	int failed;
3721bc7fdbfSTom Zanussi 
3731bc7fdbfSTom Zanussi 	failed = phys & 0x1 ? -EBADMSG : 0;
3741bc7fdbfSTom Zanussi 	phys &= ~0x3;
3751bc7fdbfSTom Zanussi 	crypt = crypt_phys2virt(phys);
3761bc7fdbfSTom Zanussi 
3771bc7fdbfSTom Zanussi 	switch (crypt->ctl_flags & CTL_FLAG_MASK) {
3781bc7fdbfSTom Zanussi 	case CTL_FLAG_PERFORM_AEAD: {
3791bc7fdbfSTom Zanussi 		struct aead_request *req = crypt->data.aead_req;
3801bc7fdbfSTom Zanussi 		struct aead_ctx *req_ctx = aead_request_ctx(req);
3811bc7fdbfSTom Zanussi 
3821bc7fdbfSTom Zanussi 		free_buf_chain(dev, req_ctx->src, crypt->src_buf);
3831bc7fdbfSTom Zanussi 		free_buf_chain(dev, req_ctx->dst, crypt->dst_buf);
3841bc7fdbfSTom Zanussi 		if (req_ctx->hmac_virt)
3851bc7fdbfSTom Zanussi 			finish_scattered_hmac(crypt);
3861bc7fdbfSTom Zanussi 
3871bc7fdbfSTom Zanussi 		aead_request_complete(req, failed);
3881bc7fdbfSTom Zanussi 		break;
3891bc7fdbfSTom Zanussi 	}
3901bc7fdbfSTom Zanussi 	case CTL_FLAG_PERFORM_ABLK: {
3911bc7fdbfSTom Zanussi 		struct skcipher_request *req = crypt->data.ablk_req;
3921bc7fdbfSTom Zanussi 		struct ablk_ctx *req_ctx = skcipher_request_ctx(req);
3931bc7fdbfSTom Zanussi 		struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
3941bc7fdbfSTom Zanussi 		unsigned int ivsize = crypto_skcipher_ivsize(tfm);
3951bc7fdbfSTom Zanussi 		unsigned int offset;
3961bc7fdbfSTom Zanussi 
3971bc7fdbfSTom Zanussi 		if (ivsize > 0) {
3981bc7fdbfSTom Zanussi 			offset = req->cryptlen - ivsize;
3991bc7fdbfSTom Zanussi 			if (req_ctx->encrypt) {
4001bc7fdbfSTom Zanussi 				scatterwalk_map_and_copy(req->iv, req->dst,
4011bc7fdbfSTom Zanussi 							 offset, ivsize, 0);
4021bc7fdbfSTom Zanussi 			} else {
4031bc7fdbfSTom Zanussi 				memcpy(req->iv, req_ctx->iv, ivsize);
4041bc7fdbfSTom Zanussi 				memzero_explicit(req_ctx->iv, ivsize);
4051bc7fdbfSTom Zanussi 			}
4061bc7fdbfSTom Zanussi 		}
4071bc7fdbfSTom Zanussi 
4081bc7fdbfSTom Zanussi 		if (req_ctx->dst)
4091bc7fdbfSTom Zanussi 			free_buf_chain(dev, req_ctx->dst, crypt->dst_buf);
4101bc7fdbfSTom Zanussi 
4111bc7fdbfSTom Zanussi 		free_buf_chain(dev, req_ctx->src, crypt->src_buf);
4121bc7fdbfSTom Zanussi 		skcipher_request_complete(req, failed);
4131bc7fdbfSTom Zanussi 		break;
4141bc7fdbfSTom Zanussi 	}
4151bc7fdbfSTom Zanussi 	case CTL_FLAG_GEN_ICV:
4161bc7fdbfSTom Zanussi 		ctx = crypto_tfm_ctx(crypt->data.tfm);
4171bc7fdbfSTom Zanussi 		dma_pool_free(ctx_pool, crypt->regist_ptr,
4181bc7fdbfSTom Zanussi 			      crypt->regist_buf->phys_addr);
4191bc7fdbfSTom Zanussi 		dma_pool_free(buffer_pool, crypt->regist_buf, crypt->src_buf);
4201bc7fdbfSTom Zanussi 		if (atomic_dec_and_test(&ctx->configuring))
4211bc7fdbfSTom Zanussi 			complete(&ctx->completion);
4221bc7fdbfSTom Zanussi 		break;
4231bc7fdbfSTom Zanussi 	case CTL_FLAG_GEN_REVAES:
4241bc7fdbfSTom Zanussi 		ctx = crypto_tfm_ctx(crypt->data.tfm);
4251bc7fdbfSTom Zanussi 		*(__be32 *)ctx->decrypt.npe_ctx &= cpu_to_be32(~CIPH_ENCR);
4261bc7fdbfSTom Zanussi 		if (atomic_dec_and_test(&ctx->configuring))
4271bc7fdbfSTom Zanussi 			complete(&ctx->completion);
4281bc7fdbfSTom Zanussi 		break;
4291bc7fdbfSTom Zanussi 	default:
4301bc7fdbfSTom Zanussi 		BUG();
4311bc7fdbfSTom Zanussi 	}
4321bc7fdbfSTom Zanussi 	crypt->ctl_flags = CTL_FLAG_UNUSED;
4331bc7fdbfSTom Zanussi }
4341bc7fdbfSTom Zanussi 
irqhandler(void * _unused)4351bc7fdbfSTom Zanussi static void irqhandler(void *_unused)
4361bc7fdbfSTom Zanussi {
4371bc7fdbfSTom Zanussi 	tasklet_schedule(&crypto_done_tasklet);
4381bc7fdbfSTom Zanussi }
4391bc7fdbfSTom Zanussi 
crypto_done_action(unsigned long arg)4401bc7fdbfSTom Zanussi static void crypto_done_action(unsigned long arg)
4411bc7fdbfSTom Zanussi {
4421bc7fdbfSTom Zanussi 	int i;
4431bc7fdbfSTom Zanussi 
4441bc7fdbfSTom Zanussi 	for (i = 0; i < 4; i++) {
4451bc7fdbfSTom Zanussi 		dma_addr_t phys = qmgr_get_entry(recv_qid);
4461bc7fdbfSTom Zanussi 		if (!phys)
4471bc7fdbfSTom Zanussi 			return;
4481bc7fdbfSTom Zanussi 		one_packet(phys);
4491bc7fdbfSTom Zanussi 	}
4501bc7fdbfSTom Zanussi 	tasklet_schedule(&crypto_done_tasklet);
4511bc7fdbfSTom Zanussi }
4521bc7fdbfSTom Zanussi 
init_ixp_crypto(struct device * dev)4531bc7fdbfSTom Zanussi static int init_ixp_crypto(struct device *dev)
4541bc7fdbfSTom Zanussi {
4551bc7fdbfSTom Zanussi 	struct device_node *np = dev->of_node;
4561bc7fdbfSTom Zanussi 	u32 msg[2] = { 0, 0 };
4571bc7fdbfSTom Zanussi 	int ret = -ENODEV;
4581bc7fdbfSTom Zanussi 	u32 npe_id;
4591bc7fdbfSTom Zanussi 
4601bc7fdbfSTom Zanussi 	dev_info(dev, "probing...\n");
4611bc7fdbfSTom Zanussi 
4621bc7fdbfSTom Zanussi 	/* Locate the NPE and queue manager to use from device tree */
4631bc7fdbfSTom Zanussi 	if (IS_ENABLED(CONFIG_OF) && np) {
4641bc7fdbfSTom Zanussi 		struct of_phandle_args queue_spec;
4651bc7fdbfSTom Zanussi 		struct of_phandle_args npe_spec;
4661bc7fdbfSTom Zanussi 
4671bc7fdbfSTom Zanussi 		ret = of_parse_phandle_with_fixed_args(np, "intel,npe-handle",
4681bc7fdbfSTom Zanussi 						       1, 0, &npe_spec);
4691bc7fdbfSTom Zanussi 		if (ret) {
4701bc7fdbfSTom Zanussi 			dev_err(dev, "no NPE engine specified\n");
4711bc7fdbfSTom Zanussi 			return -ENODEV;
4721bc7fdbfSTom Zanussi 		}
4731bc7fdbfSTom Zanussi 		npe_id = npe_spec.args[0];
4741bc7fdbfSTom Zanussi 
4751bc7fdbfSTom Zanussi 		ret = of_parse_phandle_with_fixed_args(np, "queue-rx", 1, 0,
4761bc7fdbfSTom Zanussi 						       &queue_spec);
4771bc7fdbfSTom Zanussi 		if (ret) {
4781bc7fdbfSTom Zanussi 			dev_err(dev, "no rx queue phandle\n");
4791bc7fdbfSTom Zanussi 			return -ENODEV;
4801bc7fdbfSTom Zanussi 		}
4811bc7fdbfSTom Zanussi 		recv_qid = queue_spec.args[0];
4821bc7fdbfSTom Zanussi 
4831bc7fdbfSTom Zanussi 		ret = of_parse_phandle_with_fixed_args(np, "queue-txready", 1, 0,
4841bc7fdbfSTom Zanussi 						       &queue_spec);
4851bc7fdbfSTom Zanussi 		if (ret) {
4861bc7fdbfSTom Zanussi 			dev_err(dev, "no txready queue phandle\n");
4871bc7fdbfSTom Zanussi 			return -ENODEV;
4881bc7fdbfSTom Zanussi 		}
4891bc7fdbfSTom Zanussi 		send_qid = queue_spec.args[0];
4901bc7fdbfSTom Zanussi 	} else {
4911bc7fdbfSTom Zanussi 		/*
4921bc7fdbfSTom Zanussi 		 * Hardcoded engine when using platform data, this goes away
4931bc7fdbfSTom Zanussi 		 * when we switch to using DT only.
4941bc7fdbfSTom Zanussi 		 */
4951bc7fdbfSTom Zanussi 		npe_id = 2;
4961bc7fdbfSTom Zanussi 		send_qid = 29;
4971bc7fdbfSTom Zanussi 		recv_qid = 30;
4981bc7fdbfSTom Zanussi 	}
4991bc7fdbfSTom Zanussi 
5001bc7fdbfSTom Zanussi 	npe_c = npe_request(npe_id);
5011bc7fdbfSTom Zanussi 	if (!npe_c)
5021bc7fdbfSTom Zanussi 		return ret;
5031bc7fdbfSTom Zanussi 
5041bc7fdbfSTom Zanussi 	if (!npe_running(npe_c)) {
5051bc7fdbfSTom Zanussi 		ret = npe_load_firmware(npe_c, npe_name(npe_c), dev);
5061bc7fdbfSTom Zanussi 		if (ret)
5071bc7fdbfSTom Zanussi 			goto npe_release;
5081bc7fdbfSTom Zanussi 		if (npe_recv_message(npe_c, msg, "STATUS_MSG"))
5091bc7fdbfSTom Zanussi 			goto npe_error;
5101bc7fdbfSTom Zanussi 	} else {
5111bc7fdbfSTom Zanussi 		if (npe_send_message(npe_c, msg, "STATUS_MSG"))
5121bc7fdbfSTom Zanussi 			goto npe_error;
5131bc7fdbfSTom Zanussi 
5141bc7fdbfSTom Zanussi 		if (npe_recv_message(npe_c, msg, "STATUS_MSG"))
5151bc7fdbfSTom Zanussi 			goto npe_error;
5161bc7fdbfSTom Zanussi 	}
5171bc7fdbfSTom Zanussi 
5181bc7fdbfSTom Zanussi 	switch ((msg[1] >> 16) & 0xff) {
5191bc7fdbfSTom Zanussi 	case 3:
5201bc7fdbfSTom Zanussi 		dev_warn(dev, "Firmware of %s lacks AES support\n", npe_name(npe_c));
5211bc7fdbfSTom Zanussi 		support_aes = 0;
5221bc7fdbfSTom Zanussi 		break;
5231bc7fdbfSTom Zanussi 	case 4:
5241bc7fdbfSTom Zanussi 	case 5:
5251bc7fdbfSTom Zanussi 		support_aes = 1;
5261bc7fdbfSTom Zanussi 		break;
5271bc7fdbfSTom Zanussi 	default:
5281bc7fdbfSTom Zanussi 		dev_err(dev, "Firmware of %s lacks crypto support\n", npe_name(npe_c));
5291bc7fdbfSTom Zanussi 		ret = -ENODEV;
5301bc7fdbfSTom Zanussi 		goto npe_release;
5311bc7fdbfSTom Zanussi 	}
5321bc7fdbfSTom Zanussi 	/* buffer_pool will also be used to sometimes store the hmac,
5331bc7fdbfSTom Zanussi 	 * so assure it is large enough
5341bc7fdbfSTom Zanussi 	 */
5351bc7fdbfSTom Zanussi 	BUILD_BUG_ON(SHA1_DIGEST_SIZE > sizeof(struct buffer_desc));
5361bc7fdbfSTom Zanussi 	buffer_pool = dma_pool_create("buffer", dev, sizeof(struct buffer_desc),
5371bc7fdbfSTom Zanussi 				      32, 0);
5381bc7fdbfSTom Zanussi 	ret = -ENOMEM;
5391bc7fdbfSTom Zanussi 	if (!buffer_pool)
5401bc7fdbfSTom Zanussi 		goto err;
5411bc7fdbfSTom Zanussi 
5421bc7fdbfSTom Zanussi 	ctx_pool = dma_pool_create("context", dev, NPE_CTX_LEN, 16, 0);
5431bc7fdbfSTom Zanussi 	if (!ctx_pool)
5441bc7fdbfSTom Zanussi 		goto err;
5451bc7fdbfSTom Zanussi 
5461bc7fdbfSTom Zanussi 	ret = qmgr_request_queue(send_qid, NPE_QLEN_TOTAL, 0, 0,
5471bc7fdbfSTom Zanussi 				 "ixp_crypto:out", NULL);
5481bc7fdbfSTom Zanussi 	if (ret)
5491bc7fdbfSTom Zanussi 		goto err;
5501bc7fdbfSTom Zanussi 	ret = qmgr_request_queue(recv_qid, NPE_QLEN, 0, 0,
5511bc7fdbfSTom Zanussi 				 "ixp_crypto:in", NULL);
5521bc7fdbfSTom Zanussi 	if (ret) {
5531bc7fdbfSTom Zanussi 		qmgr_release_queue(send_qid);
5541bc7fdbfSTom Zanussi 		goto err;
5551bc7fdbfSTom Zanussi 	}
5561bc7fdbfSTom Zanussi 	qmgr_set_irq(recv_qid, QUEUE_IRQ_SRC_NOT_EMPTY, irqhandler, NULL);
5571bc7fdbfSTom Zanussi 	tasklet_init(&crypto_done_tasklet, crypto_done_action, 0);
5581bc7fdbfSTom Zanussi 
5591bc7fdbfSTom Zanussi 	qmgr_enable_irq(recv_qid);
5601bc7fdbfSTom Zanussi 	return 0;
5611bc7fdbfSTom Zanussi 
5621bc7fdbfSTom Zanussi npe_error:
5631bc7fdbfSTom Zanussi 	dev_err(dev, "%s not responding\n", npe_name(npe_c));
5641bc7fdbfSTom Zanussi 	ret = -EIO;
5651bc7fdbfSTom Zanussi err:
5661bc7fdbfSTom Zanussi 	dma_pool_destroy(ctx_pool);
5671bc7fdbfSTom Zanussi 	dma_pool_destroy(buffer_pool);
5681bc7fdbfSTom Zanussi npe_release:
5691bc7fdbfSTom Zanussi 	npe_release(npe_c);
5701bc7fdbfSTom Zanussi 	return ret;
5711bc7fdbfSTom Zanussi }
5721bc7fdbfSTom Zanussi 
release_ixp_crypto(struct device * dev)5731bc7fdbfSTom Zanussi static void release_ixp_crypto(struct device *dev)
5741bc7fdbfSTom Zanussi {
5751bc7fdbfSTom Zanussi 	qmgr_disable_irq(recv_qid);
5761bc7fdbfSTom Zanussi 	tasklet_kill(&crypto_done_tasklet);
5771bc7fdbfSTom Zanussi 
5781bc7fdbfSTom Zanussi 	qmgr_release_queue(send_qid);
5791bc7fdbfSTom Zanussi 	qmgr_release_queue(recv_qid);
5801bc7fdbfSTom Zanussi 
5811bc7fdbfSTom Zanussi 	dma_pool_destroy(ctx_pool);
5821bc7fdbfSTom Zanussi 	dma_pool_destroy(buffer_pool);
5831bc7fdbfSTom Zanussi 
5841bc7fdbfSTom Zanussi 	npe_release(npe_c);
5851bc7fdbfSTom Zanussi 
5861bc7fdbfSTom Zanussi 	if (crypt_virt)
5871bc7fdbfSTom Zanussi 		dma_free_coherent(dev, NPE_QLEN * sizeof(struct crypt_ctl),
5881bc7fdbfSTom Zanussi 				  crypt_virt, crypt_phys);
5891bc7fdbfSTom Zanussi }
5901bc7fdbfSTom Zanussi 
reset_sa_dir(struct ix_sa_dir * dir)5911bc7fdbfSTom Zanussi static void reset_sa_dir(struct ix_sa_dir *dir)
5921bc7fdbfSTom Zanussi {
5931bc7fdbfSTom Zanussi 	memset(dir->npe_ctx, 0, NPE_CTX_LEN);
5941bc7fdbfSTom Zanussi 	dir->npe_ctx_idx = 0;
5951bc7fdbfSTom Zanussi 	dir->npe_mode = 0;
5961bc7fdbfSTom Zanussi }
5971bc7fdbfSTom Zanussi 
init_sa_dir(struct ix_sa_dir * dir)5981bc7fdbfSTom Zanussi static int init_sa_dir(struct ix_sa_dir *dir)
5991bc7fdbfSTom Zanussi {
6001bc7fdbfSTom Zanussi 	dir->npe_ctx = dma_pool_alloc(ctx_pool, GFP_KERNEL, &dir->npe_ctx_phys);
6011bc7fdbfSTom Zanussi 	if (!dir->npe_ctx)
6021bc7fdbfSTom Zanussi 		return -ENOMEM;
6031bc7fdbfSTom Zanussi 
6041bc7fdbfSTom Zanussi 	reset_sa_dir(dir);
6051bc7fdbfSTom Zanussi 	return 0;
6061bc7fdbfSTom Zanussi }
6071bc7fdbfSTom Zanussi 
free_sa_dir(struct ix_sa_dir * dir)6081bc7fdbfSTom Zanussi static void free_sa_dir(struct ix_sa_dir *dir)
6091bc7fdbfSTom Zanussi {
6101bc7fdbfSTom Zanussi 	memset(dir->npe_ctx, 0, NPE_CTX_LEN);
6111bc7fdbfSTom Zanussi 	dma_pool_free(ctx_pool, dir->npe_ctx, dir->npe_ctx_phys);
6121bc7fdbfSTom Zanussi }
6131bc7fdbfSTom Zanussi 
init_tfm(struct crypto_tfm * tfm)6141bc7fdbfSTom Zanussi static int init_tfm(struct crypto_tfm *tfm)
6151bc7fdbfSTom Zanussi {
6161bc7fdbfSTom Zanussi 	struct ixp_ctx *ctx = crypto_tfm_ctx(tfm);
6171bc7fdbfSTom Zanussi 	int ret;
6181bc7fdbfSTom Zanussi 
6191bc7fdbfSTom Zanussi 	atomic_set(&ctx->configuring, 0);
6201bc7fdbfSTom Zanussi 	ret = init_sa_dir(&ctx->encrypt);
6211bc7fdbfSTom Zanussi 	if (ret)
6221bc7fdbfSTom Zanussi 		return ret;
6231bc7fdbfSTom Zanussi 	ret = init_sa_dir(&ctx->decrypt);
6241bc7fdbfSTom Zanussi 	if (ret)
6251bc7fdbfSTom Zanussi 		free_sa_dir(&ctx->encrypt);
6261bc7fdbfSTom Zanussi 
6271bc7fdbfSTom Zanussi 	return ret;
6281bc7fdbfSTom Zanussi }
6291bc7fdbfSTom Zanussi 
init_tfm_ablk(struct crypto_skcipher * tfm)6301bc7fdbfSTom Zanussi static int init_tfm_ablk(struct crypto_skcipher *tfm)
6311bc7fdbfSTom Zanussi {
6321bc7fdbfSTom Zanussi 	struct crypto_tfm *ctfm = crypto_skcipher_tfm(tfm);
6331bc7fdbfSTom Zanussi 	struct ixp_ctx *ctx = crypto_tfm_ctx(ctfm);
6341bc7fdbfSTom Zanussi 	const char *name = crypto_tfm_alg_name(ctfm);
6351bc7fdbfSTom Zanussi 
6361bc7fdbfSTom Zanussi 	ctx->fallback_tfm = crypto_alloc_skcipher(name, 0, CRYPTO_ALG_NEED_FALLBACK);
6371bc7fdbfSTom Zanussi 	if (IS_ERR(ctx->fallback_tfm)) {
6381bc7fdbfSTom Zanussi 		pr_err("ERROR: Cannot allocate fallback for %s %ld\n",
6391bc7fdbfSTom Zanussi 			name, PTR_ERR(ctx->fallback_tfm));
6401bc7fdbfSTom Zanussi 		return PTR_ERR(ctx->fallback_tfm);
6411bc7fdbfSTom Zanussi 	}
6421bc7fdbfSTom Zanussi 
6431bc7fdbfSTom Zanussi 	pr_info("Fallback for %s is %s\n",
6441bc7fdbfSTom Zanussi 		 crypto_tfm_alg_driver_name(&tfm->base),
6451bc7fdbfSTom Zanussi 		 crypto_tfm_alg_driver_name(crypto_skcipher_tfm(ctx->fallback_tfm))
6461bc7fdbfSTom Zanussi 		 );
6471bc7fdbfSTom Zanussi 
6481bc7fdbfSTom Zanussi 	crypto_skcipher_set_reqsize(tfm, sizeof(struct ablk_ctx) + crypto_skcipher_reqsize(ctx->fallback_tfm));
6491bc7fdbfSTom Zanussi 	return init_tfm(crypto_skcipher_tfm(tfm));
6501bc7fdbfSTom Zanussi }
6511bc7fdbfSTom Zanussi 
init_tfm_aead(struct crypto_aead * tfm)6521bc7fdbfSTom Zanussi static int init_tfm_aead(struct crypto_aead *tfm)
6531bc7fdbfSTom Zanussi {
6541bc7fdbfSTom Zanussi 	crypto_aead_set_reqsize(tfm, sizeof(struct aead_ctx));
6551bc7fdbfSTom Zanussi 	return init_tfm(crypto_aead_tfm(tfm));
6561bc7fdbfSTom Zanussi }
6571bc7fdbfSTom Zanussi 
exit_tfm(struct crypto_tfm * tfm)6581bc7fdbfSTom Zanussi static void exit_tfm(struct crypto_tfm *tfm)
6591bc7fdbfSTom Zanussi {
6601bc7fdbfSTom Zanussi 	struct ixp_ctx *ctx = crypto_tfm_ctx(tfm);
6611bc7fdbfSTom Zanussi 
6621bc7fdbfSTom Zanussi 	free_sa_dir(&ctx->encrypt);
6631bc7fdbfSTom Zanussi 	free_sa_dir(&ctx->decrypt);
6641bc7fdbfSTom Zanussi }
6651bc7fdbfSTom Zanussi 
exit_tfm_ablk(struct crypto_skcipher * tfm)6661bc7fdbfSTom Zanussi static void exit_tfm_ablk(struct crypto_skcipher *tfm)
6671bc7fdbfSTom Zanussi {
6681bc7fdbfSTom Zanussi 	struct crypto_tfm *ctfm = crypto_skcipher_tfm(tfm);
6691bc7fdbfSTom Zanussi 	struct ixp_ctx *ctx = crypto_tfm_ctx(ctfm);
6701bc7fdbfSTom Zanussi 
6711bc7fdbfSTom Zanussi 	crypto_free_skcipher(ctx->fallback_tfm);
6721bc7fdbfSTom Zanussi 	exit_tfm(crypto_skcipher_tfm(tfm));
6731bc7fdbfSTom Zanussi }
6741bc7fdbfSTom Zanussi 
exit_tfm_aead(struct crypto_aead * tfm)6751bc7fdbfSTom Zanussi static void exit_tfm_aead(struct crypto_aead *tfm)
6761bc7fdbfSTom Zanussi {
6771bc7fdbfSTom Zanussi 	exit_tfm(crypto_aead_tfm(tfm));
6781bc7fdbfSTom Zanussi }
6791bc7fdbfSTom Zanussi 
register_chain_var(struct crypto_tfm * tfm,u8 xpad,u32 target,int init_len,u32 ctx_addr,const u8 * key,int key_len)6801bc7fdbfSTom Zanussi static int register_chain_var(struct crypto_tfm *tfm, u8 xpad, u32 target,
6811bc7fdbfSTom Zanussi 			      int init_len, u32 ctx_addr, const u8 *key,
6821bc7fdbfSTom Zanussi 			      int key_len)
6831bc7fdbfSTom Zanussi {
6841bc7fdbfSTom Zanussi 	struct ixp_ctx *ctx = crypto_tfm_ctx(tfm);
6851bc7fdbfSTom Zanussi 	struct crypt_ctl *crypt;
6861bc7fdbfSTom Zanussi 	struct buffer_desc *buf;
6871bc7fdbfSTom Zanussi 	int i;
6881bc7fdbfSTom Zanussi 	u8 *pad;
6891bc7fdbfSTom Zanussi 	dma_addr_t pad_phys, buf_phys;
6901bc7fdbfSTom Zanussi 
6911bc7fdbfSTom Zanussi 	BUILD_BUG_ON(NPE_CTX_LEN < HMAC_PAD_BLOCKLEN);
6921bc7fdbfSTom Zanussi 	pad = dma_pool_alloc(ctx_pool, GFP_KERNEL, &pad_phys);
6931bc7fdbfSTom Zanussi 	if (!pad)
6941bc7fdbfSTom Zanussi 		return -ENOMEM;
6951bc7fdbfSTom Zanussi 	buf = dma_pool_alloc(buffer_pool, GFP_KERNEL, &buf_phys);
6961bc7fdbfSTom Zanussi 	if (!buf) {
6971bc7fdbfSTom Zanussi 		dma_pool_free(ctx_pool, pad, pad_phys);
6981bc7fdbfSTom Zanussi 		return -ENOMEM;
6991bc7fdbfSTom Zanussi 	}
7001bc7fdbfSTom Zanussi 	crypt = get_crypt_desc_emerg();
7011bc7fdbfSTom Zanussi 	if (!crypt) {
7021bc7fdbfSTom Zanussi 		dma_pool_free(ctx_pool, pad, pad_phys);
7031bc7fdbfSTom Zanussi 		dma_pool_free(buffer_pool, buf, buf_phys);
7041bc7fdbfSTom Zanussi 		return -EAGAIN;
7051bc7fdbfSTom Zanussi 	}
7061bc7fdbfSTom Zanussi 
7071bc7fdbfSTom Zanussi 	memcpy(pad, key, key_len);
7081bc7fdbfSTom Zanussi 	memset(pad + key_len, 0, HMAC_PAD_BLOCKLEN - key_len);
7091bc7fdbfSTom Zanussi 	for (i = 0; i < HMAC_PAD_BLOCKLEN; i++)
7101bc7fdbfSTom Zanussi 		pad[i] ^= xpad;
7111bc7fdbfSTom Zanussi 
7121bc7fdbfSTom Zanussi 	crypt->data.tfm = tfm;
7131bc7fdbfSTom Zanussi 	crypt->regist_ptr = pad;
7141bc7fdbfSTom Zanussi 	crypt->regist_buf = buf;
7151bc7fdbfSTom Zanussi 
7161bc7fdbfSTom Zanussi 	crypt->auth_offs = 0;
7171bc7fdbfSTom Zanussi 	crypt->auth_len = HMAC_PAD_BLOCKLEN;
7181bc7fdbfSTom Zanussi 	crypt->crypto_ctx = ctx_addr;
7191bc7fdbfSTom Zanussi 	crypt->src_buf = buf_phys;
7201bc7fdbfSTom Zanussi 	crypt->icv_rev_aes = target;
7211bc7fdbfSTom Zanussi 	crypt->mode = NPE_OP_HASH_GEN_ICV;
7221bc7fdbfSTom Zanussi 	crypt->init_len = init_len;
7231bc7fdbfSTom Zanussi 	crypt->ctl_flags |= CTL_FLAG_GEN_ICV;
7241bc7fdbfSTom Zanussi 
7251bc7fdbfSTom Zanussi 	buf->next = NULL;
7261bc7fdbfSTom Zanussi 	buf->buf_len = HMAC_PAD_BLOCKLEN;
7271bc7fdbfSTom Zanussi 	buf->pkt_len = 0;
7281bc7fdbfSTom Zanussi 	buf->phys_addr = pad_phys;
7291bc7fdbfSTom Zanussi 
7301bc7fdbfSTom Zanussi 	atomic_inc(&ctx->configuring);
7311bc7fdbfSTom Zanussi 	qmgr_put_entry(send_qid, crypt_virt2phys(crypt));
7321bc7fdbfSTom Zanussi 	BUG_ON(qmgr_stat_overflow(send_qid));
7331bc7fdbfSTom Zanussi 	return 0;
7341bc7fdbfSTom Zanussi }
7351bc7fdbfSTom Zanussi 
setup_auth(struct crypto_tfm * tfm,int encrypt,unsigned int authsize,const u8 * key,int key_len,unsigned int digest_len)7361bc7fdbfSTom Zanussi static int setup_auth(struct crypto_tfm *tfm, int encrypt, unsigned int authsize,
7371bc7fdbfSTom Zanussi 		      const u8 *key, int key_len, unsigned int digest_len)
7381bc7fdbfSTom Zanussi {
7391bc7fdbfSTom Zanussi 	u32 itarget, otarget, npe_ctx_addr;
7401bc7fdbfSTom Zanussi 	unsigned char *cinfo;
7411bc7fdbfSTom Zanussi 	int init_len, ret = 0;
7421bc7fdbfSTom Zanussi 	u32 cfgword;
7431bc7fdbfSTom Zanussi 	struct ix_sa_dir *dir;
7441bc7fdbfSTom Zanussi 	struct ixp_ctx *ctx = crypto_tfm_ctx(tfm);
7451bc7fdbfSTom Zanussi 	const struct ix_hash_algo *algo;
7461bc7fdbfSTom Zanussi 
7471bc7fdbfSTom Zanussi 	dir = encrypt ? &ctx->encrypt : &ctx->decrypt;
7481bc7fdbfSTom Zanussi 	cinfo = dir->npe_ctx + dir->npe_ctx_idx;
7491bc7fdbfSTom Zanussi 	algo = ix_hash(tfm);
7501bc7fdbfSTom Zanussi 
7511bc7fdbfSTom Zanussi 	/* write cfg word to cryptinfo */
7521bc7fdbfSTom Zanussi 	cfgword = algo->cfgword | (authsize << 6); /* (authsize/4) << 8 */
7531bc7fdbfSTom Zanussi #ifndef __ARMEB__
7541bc7fdbfSTom Zanussi 	cfgword ^= 0xAA000000; /* change the "byte swap" flags */
7551bc7fdbfSTom Zanussi #endif
7561bc7fdbfSTom Zanussi 	*(__be32 *)cinfo = cpu_to_be32(cfgword);
7571bc7fdbfSTom Zanussi 	cinfo += sizeof(cfgword);
7581bc7fdbfSTom Zanussi 
7591bc7fdbfSTom Zanussi 	/* write ICV to cryptinfo */
7601bc7fdbfSTom Zanussi 	memcpy(cinfo, algo->icv, digest_len);
7611bc7fdbfSTom Zanussi 	cinfo += digest_len;
7621bc7fdbfSTom Zanussi 
7631bc7fdbfSTom Zanussi 	itarget = dir->npe_ctx_phys + dir->npe_ctx_idx
7641bc7fdbfSTom Zanussi 				+ sizeof(algo->cfgword);
7651bc7fdbfSTom Zanussi 	otarget = itarget + digest_len;
7661bc7fdbfSTom Zanussi 	init_len = cinfo - (dir->npe_ctx + dir->npe_ctx_idx);
7671bc7fdbfSTom Zanussi 	npe_ctx_addr = dir->npe_ctx_phys + dir->npe_ctx_idx;
7681bc7fdbfSTom Zanussi 
7691bc7fdbfSTom Zanussi 	dir->npe_ctx_idx += init_len;
7701bc7fdbfSTom Zanussi 	dir->npe_mode |= NPE_OP_HASH_ENABLE;
7711bc7fdbfSTom Zanussi 
7721bc7fdbfSTom Zanussi 	if (!encrypt)
7731bc7fdbfSTom Zanussi 		dir->npe_mode |= NPE_OP_HASH_VERIFY;
7741bc7fdbfSTom Zanussi 
7751bc7fdbfSTom Zanussi 	ret = register_chain_var(tfm, HMAC_OPAD_VALUE, otarget,
7761bc7fdbfSTom Zanussi 				 init_len, npe_ctx_addr, key, key_len);
7771bc7fdbfSTom Zanussi 	if (ret)
7781bc7fdbfSTom Zanussi 		return ret;
7791bc7fdbfSTom Zanussi 	return register_chain_var(tfm, HMAC_IPAD_VALUE, itarget,
7801bc7fdbfSTom Zanussi 				  init_len, npe_ctx_addr, key, key_len);
7811bc7fdbfSTom Zanussi }
7821bc7fdbfSTom Zanussi 
gen_rev_aes_key(struct crypto_tfm * tfm)7831bc7fdbfSTom Zanussi static int gen_rev_aes_key(struct crypto_tfm *tfm)
7841bc7fdbfSTom Zanussi {
7851bc7fdbfSTom Zanussi 	struct crypt_ctl *crypt;
7861bc7fdbfSTom Zanussi 	struct ixp_ctx *ctx = crypto_tfm_ctx(tfm);
7871bc7fdbfSTom Zanussi 	struct ix_sa_dir *dir = &ctx->decrypt;
7881bc7fdbfSTom Zanussi 
7891bc7fdbfSTom Zanussi 	crypt = get_crypt_desc_emerg();
7901bc7fdbfSTom Zanussi 	if (!crypt)
7911bc7fdbfSTom Zanussi 		return -EAGAIN;
7921bc7fdbfSTom Zanussi 
7931bc7fdbfSTom Zanussi 	*(__be32 *)dir->npe_ctx |= cpu_to_be32(CIPH_ENCR);
7941bc7fdbfSTom Zanussi 
7951bc7fdbfSTom Zanussi 	crypt->data.tfm = tfm;
7961bc7fdbfSTom Zanussi 	crypt->crypt_offs = 0;
7971bc7fdbfSTom Zanussi 	crypt->crypt_len = AES_BLOCK128;
7981bc7fdbfSTom Zanussi 	crypt->src_buf = 0;
7991bc7fdbfSTom Zanussi 	crypt->crypto_ctx = dir->npe_ctx_phys;
8001bc7fdbfSTom Zanussi 	crypt->icv_rev_aes = dir->npe_ctx_phys + sizeof(u32);
8011bc7fdbfSTom Zanussi 	crypt->mode = NPE_OP_ENC_GEN_KEY;
8021bc7fdbfSTom Zanussi 	crypt->init_len = dir->npe_ctx_idx;
8031bc7fdbfSTom Zanussi 	crypt->ctl_flags |= CTL_FLAG_GEN_REVAES;
8041bc7fdbfSTom Zanussi 
8051bc7fdbfSTom Zanussi 	atomic_inc(&ctx->configuring);
8061bc7fdbfSTom Zanussi 	qmgr_put_entry(send_qid, crypt_virt2phys(crypt));
8071bc7fdbfSTom Zanussi 	BUG_ON(qmgr_stat_overflow(send_qid));
8081bc7fdbfSTom Zanussi 	return 0;
8091bc7fdbfSTom Zanussi }
8101bc7fdbfSTom Zanussi 
setup_cipher(struct crypto_tfm * tfm,int encrypt,const u8 * key,int key_len)8111bc7fdbfSTom Zanussi static int setup_cipher(struct crypto_tfm *tfm, int encrypt, const u8 *key,
8121bc7fdbfSTom Zanussi 			int key_len)
8131bc7fdbfSTom Zanussi {
8141bc7fdbfSTom Zanussi 	u8 *cinfo;
8151bc7fdbfSTom Zanussi 	u32 cipher_cfg;
8161bc7fdbfSTom Zanussi 	u32 keylen_cfg = 0;
8171bc7fdbfSTom Zanussi 	struct ix_sa_dir *dir;
8181bc7fdbfSTom Zanussi 	struct ixp_ctx *ctx = crypto_tfm_ctx(tfm);
8191bc7fdbfSTom Zanussi 	int err;
8201bc7fdbfSTom Zanussi 
8211bc7fdbfSTom Zanussi 	dir = encrypt ? &ctx->encrypt : &ctx->decrypt;
8221bc7fdbfSTom Zanussi 	cinfo = dir->npe_ctx;
8231bc7fdbfSTom Zanussi 
8241bc7fdbfSTom Zanussi 	if (encrypt) {
8251bc7fdbfSTom Zanussi 		cipher_cfg = cipher_cfg_enc(tfm);
8261bc7fdbfSTom Zanussi 		dir->npe_mode |= NPE_OP_CRYPT_ENCRYPT;
8271bc7fdbfSTom Zanussi 	} else {
8281bc7fdbfSTom Zanussi 		cipher_cfg = cipher_cfg_dec(tfm);
8291bc7fdbfSTom Zanussi 	}
8301bc7fdbfSTom Zanussi 	if (cipher_cfg & MOD_AES) {
8311bc7fdbfSTom Zanussi 		switch (key_len) {
8321bc7fdbfSTom Zanussi 		case 16:
8331bc7fdbfSTom Zanussi 			keylen_cfg = MOD_AES128;
8341bc7fdbfSTom Zanussi 			break;
8351bc7fdbfSTom Zanussi 		case 24:
8361bc7fdbfSTom Zanussi 			keylen_cfg = MOD_AES192;
8371bc7fdbfSTom Zanussi 			break;
8381bc7fdbfSTom Zanussi 		case 32:
8391bc7fdbfSTom Zanussi 			keylen_cfg = MOD_AES256;
8401bc7fdbfSTom Zanussi 			break;
8411bc7fdbfSTom Zanussi 		default:
8421bc7fdbfSTom Zanussi 			return -EINVAL;
8431bc7fdbfSTom Zanussi 		}
8441bc7fdbfSTom Zanussi 		cipher_cfg |= keylen_cfg;
8451bc7fdbfSTom Zanussi 	} else {
8461bc7fdbfSTom Zanussi 		err = crypto_des_verify_key(tfm, key);
8471bc7fdbfSTom Zanussi 		if (err)
8481bc7fdbfSTom Zanussi 			return err;
8491bc7fdbfSTom Zanussi 	}
8501bc7fdbfSTom Zanussi 	/* write cfg word to cryptinfo */
8511bc7fdbfSTom Zanussi 	*(__be32 *)cinfo = cpu_to_be32(cipher_cfg);
8521bc7fdbfSTom Zanussi 	cinfo += sizeof(cipher_cfg);
8531bc7fdbfSTom Zanussi 
8541bc7fdbfSTom Zanussi 	/* write cipher key to cryptinfo */
8551bc7fdbfSTom Zanussi 	memcpy(cinfo, key, key_len);
8561bc7fdbfSTom Zanussi 	/* NPE wants keylen set to DES3_EDE_KEY_SIZE even for single DES */
8571bc7fdbfSTom Zanussi 	if (key_len < DES3_EDE_KEY_SIZE && !(cipher_cfg & MOD_AES)) {
8581bc7fdbfSTom Zanussi 		memset(cinfo + key_len, 0, DES3_EDE_KEY_SIZE - key_len);
8591bc7fdbfSTom Zanussi 		key_len = DES3_EDE_KEY_SIZE;
8601bc7fdbfSTom Zanussi 	}
8611bc7fdbfSTom Zanussi 	dir->npe_ctx_idx = sizeof(cipher_cfg) + key_len;
8621bc7fdbfSTom Zanussi 	dir->npe_mode |= NPE_OP_CRYPT_ENABLE;
8631bc7fdbfSTom Zanussi 	if ((cipher_cfg & MOD_AES) && !encrypt)
8641bc7fdbfSTom Zanussi 		return gen_rev_aes_key(tfm);
8651bc7fdbfSTom Zanussi 
8661bc7fdbfSTom Zanussi 	return 0;
8671bc7fdbfSTom Zanussi }
8681bc7fdbfSTom Zanussi 
chainup_buffers(struct device * dev,struct scatterlist * sg,unsigned int nbytes,struct buffer_desc * buf,gfp_t flags,enum dma_data_direction dir)8691bc7fdbfSTom Zanussi static struct buffer_desc *chainup_buffers(struct device *dev,
8701bc7fdbfSTom Zanussi 		struct scatterlist *sg,	unsigned int nbytes,
8711bc7fdbfSTom Zanussi 		struct buffer_desc *buf, gfp_t flags,
8721bc7fdbfSTom Zanussi 		enum dma_data_direction dir)
8731bc7fdbfSTom Zanussi {
8741bc7fdbfSTom Zanussi 	for (; nbytes > 0; sg = sg_next(sg)) {
8751bc7fdbfSTom Zanussi 		unsigned int len = min(nbytes, sg->length);
8761bc7fdbfSTom Zanussi 		struct buffer_desc *next_buf;
8771bc7fdbfSTom Zanussi 		dma_addr_t next_buf_phys;
8781bc7fdbfSTom Zanussi 		void *ptr;
8791bc7fdbfSTom Zanussi 
8801bc7fdbfSTom Zanussi 		nbytes -= len;
8811bc7fdbfSTom Zanussi 		ptr = sg_virt(sg);
8821bc7fdbfSTom Zanussi 		next_buf = dma_pool_alloc(buffer_pool, flags, &next_buf_phys);
8831bc7fdbfSTom Zanussi 		if (!next_buf) {
8841bc7fdbfSTom Zanussi 			buf = NULL;
8851bc7fdbfSTom Zanussi 			break;
8861bc7fdbfSTom Zanussi 		}
8871bc7fdbfSTom Zanussi 		sg_dma_address(sg) = dma_map_single(dev, ptr, len, dir);
8881bc7fdbfSTom Zanussi 		buf->next = next_buf;
8891bc7fdbfSTom Zanussi 		buf->phys_next = next_buf_phys;
8901bc7fdbfSTom Zanussi 		buf = next_buf;
8911bc7fdbfSTom Zanussi 
8921bc7fdbfSTom Zanussi 		buf->phys_addr = sg_dma_address(sg);
8931bc7fdbfSTom Zanussi 		buf->buf_len = len;
8941bc7fdbfSTom Zanussi 		buf->dir = dir;
8951bc7fdbfSTom Zanussi 	}
8961bc7fdbfSTom Zanussi 	buf->next = NULL;
8971bc7fdbfSTom Zanussi 	buf->phys_next = 0;
8981bc7fdbfSTom Zanussi 	return buf;
8991bc7fdbfSTom Zanussi }
9001bc7fdbfSTom Zanussi 
ablk_setkey(struct crypto_skcipher * tfm,const u8 * key,unsigned int key_len)9011bc7fdbfSTom Zanussi static int ablk_setkey(struct crypto_skcipher *tfm, const u8 *key,
9021bc7fdbfSTom Zanussi 		       unsigned int key_len)
9031bc7fdbfSTom Zanussi {
9041bc7fdbfSTom Zanussi 	struct ixp_ctx *ctx = crypto_skcipher_ctx(tfm);
9051bc7fdbfSTom Zanussi 	int ret;
9061bc7fdbfSTom Zanussi 
9071bc7fdbfSTom Zanussi 	init_completion(&ctx->completion);
9081bc7fdbfSTom Zanussi 	atomic_inc(&ctx->configuring);
9091bc7fdbfSTom Zanussi 
9101bc7fdbfSTom Zanussi 	reset_sa_dir(&ctx->encrypt);
9111bc7fdbfSTom Zanussi 	reset_sa_dir(&ctx->decrypt);
9121bc7fdbfSTom Zanussi 
9131bc7fdbfSTom Zanussi 	ctx->encrypt.npe_mode = NPE_OP_HMAC_DISABLE;
9141bc7fdbfSTom Zanussi 	ctx->decrypt.npe_mode = NPE_OP_HMAC_DISABLE;
9151bc7fdbfSTom Zanussi 
9161bc7fdbfSTom Zanussi 	ret = setup_cipher(&tfm->base, 0, key, key_len);
9171bc7fdbfSTom Zanussi 	if (ret)
9181bc7fdbfSTom Zanussi 		goto out;
9191bc7fdbfSTom Zanussi 	ret = setup_cipher(&tfm->base, 1, key, key_len);
9201bc7fdbfSTom Zanussi out:
9211bc7fdbfSTom Zanussi 	if (!atomic_dec_and_test(&ctx->configuring))
9221bc7fdbfSTom Zanussi 		wait_for_completion(&ctx->completion);
9231bc7fdbfSTom Zanussi 	if (ret)
9241bc7fdbfSTom Zanussi 		return ret;
9251bc7fdbfSTom Zanussi 	crypto_skcipher_clear_flags(ctx->fallback_tfm, CRYPTO_TFM_REQ_MASK);
9261bc7fdbfSTom Zanussi 	crypto_skcipher_set_flags(ctx->fallback_tfm, tfm->base.crt_flags & CRYPTO_TFM_REQ_MASK);
9271bc7fdbfSTom Zanussi 
9281bc7fdbfSTom Zanussi 	return crypto_skcipher_setkey(ctx->fallback_tfm, key, key_len);
9291bc7fdbfSTom Zanussi }
9301bc7fdbfSTom Zanussi 
ablk_des3_setkey(struct crypto_skcipher * tfm,const u8 * key,unsigned int key_len)9311bc7fdbfSTom Zanussi static int ablk_des3_setkey(struct crypto_skcipher *tfm, const u8 *key,
9321bc7fdbfSTom Zanussi 			    unsigned int key_len)
9331bc7fdbfSTom Zanussi {
9341bc7fdbfSTom Zanussi 	return verify_skcipher_des3_key(tfm, key) ?:
9351bc7fdbfSTom Zanussi 	       ablk_setkey(tfm, key, key_len);
9361bc7fdbfSTom Zanussi }
9371bc7fdbfSTom Zanussi 
ablk_rfc3686_setkey(struct crypto_skcipher * tfm,const u8 * key,unsigned int key_len)9381bc7fdbfSTom Zanussi static int ablk_rfc3686_setkey(struct crypto_skcipher *tfm, const u8 *key,
9391bc7fdbfSTom Zanussi 			       unsigned int key_len)
9401bc7fdbfSTom Zanussi {
9411bc7fdbfSTom Zanussi 	struct ixp_ctx *ctx = crypto_skcipher_ctx(tfm);
9421bc7fdbfSTom Zanussi 
9431bc7fdbfSTom Zanussi 	/* the nonce is stored in bytes at end of key */
9441bc7fdbfSTom Zanussi 	if (key_len < CTR_RFC3686_NONCE_SIZE)
9451bc7fdbfSTom Zanussi 		return -EINVAL;
9461bc7fdbfSTom Zanussi 
9471bc7fdbfSTom Zanussi 	memcpy(ctx->nonce, key + (key_len - CTR_RFC3686_NONCE_SIZE),
9481bc7fdbfSTom Zanussi 	       CTR_RFC3686_NONCE_SIZE);
9491bc7fdbfSTom Zanussi 
9501bc7fdbfSTom Zanussi 	key_len -= CTR_RFC3686_NONCE_SIZE;
9511bc7fdbfSTom Zanussi 	return ablk_setkey(tfm, key, key_len);
9521bc7fdbfSTom Zanussi }
9531bc7fdbfSTom Zanussi 
ixp4xx_cipher_fallback(struct skcipher_request * areq,int encrypt)9541bc7fdbfSTom Zanussi static int ixp4xx_cipher_fallback(struct skcipher_request *areq, int encrypt)
9551bc7fdbfSTom Zanussi {
9561bc7fdbfSTom Zanussi 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(areq);
9571bc7fdbfSTom Zanussi 	struct ixp_ctx *op = crypto_skcipher_ctx(tfm);
9581bc7fdbfSTom Zanussi 	struct ablk_ctx *rctx = skcipher_request_ctx(areq);
9591bc7fdbfSTom Zanussi 	int err;
9601bc7fdbfSTom Zanussi 
9611bc7fdbfSTom Zanussi 	skcipher_request_set_tfm(&rctx->fallback_req, op->fallback_tfm);
9621bc7fdbfSTom Zanussi 	skcipher_request_set_callback(&rctx->fallback_req, areq->base.flags,
9631bc7fdbfSTom Zanussi 				      areq->base.complete, areq->base.data);
9641bc7fdbfSTom Zanussi 	skcipher_request_set_crypt(&rctx->fallback_req, areq->src, areq->dst,
9651bc7fdbfSTom Zanussi 				   areq->cryptlen, areq->iv);
9661bc7fdbfSTom Zanussi 	if (encrypt)
9671bc7fdbfSTom Zanussi 		err = crypto_skcipher_encrypt(&rctx->fallback_req);
9681bc7fdbfSTom Zanussi 	else
9691bc7fdbfSTom Zanussi 		err = crypto_skcipher_decrypt(&rctx->fallback_req);
9701bc7fdbfSTom Zanussi 	return err;
9711bc7fdbfSTom Zanussi }
9721bc7fdbfSTom Zanussi 
ablk_perform(struct skcipher_request * req,int encrypt)9731bc7fdbfSTom Zanussi static int ablk_perform(struct skcipher_request *req, int encrypt)
9741bc7fdbfSTom Zanussi {
9751bc7fdbfSTom Zanussi 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
9761bc7fdbfSTom Zanussi 	struct ixp_ctx *ctx = crypto_skcipher_ctx(tfm);
9771bc7fdbfSTom Zanussi 	unsigned int ivsize = crypto_skcipher_ivsize(tfm);
9781bc7fdbfSTom Zanussi 	struct ix_sa_dir *dir;
9791bc7fdbfSTom Zanussi 	struct crypt_ctl *crypt;
9801bc7fdbfSTom Zanussi 	unsigned int nbytes = req->cryptlen;
9811bc7fdbfSTom Zanussi 	enum dma_data_direction src_direction = DMA_BIDIRECTIONAL;
9821bc7fdbfSTom Zanussi 	struct ablk_ctx *req_ctx = skcipher_request_ctx(req);
9831bc7fdbfSTom Zanussi 	struct buffer_desc src_hook;
9841bc7fdbfSTom Zanussi 	struct device *dev = &pdev->dev;
9851bc7fdbfSTom Zanussi 	unsigned int offset;
9861bc7fdbfSTom Zanussi 	gfp_t flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ?
9871bc7fdbfSTom Zanussi 				GFP_KERNEL : GFP_ATOMIC;
9881bc7fdbfSTom Zanussi 
9891bc7fdbfSTom Zanussi 	if (sg_nents(req->src) > 1 || sg_nents(req->dst) > 1)
9901bc7fdbfSTom Zanussi 		return ixp4xx_cipher_fallback(req, encrypt);
9911bc7fdbfSTom Zanussi 
9921bc7fdbfSTom Zanussi 	if (qmgr_stat_full(send_qid))
9931bc7fdbfSTom Zanussi 		return -EAGAIN;
9941bc7fdbfSTom Zanussi 	if (atomic_read(&ctx->configuring))
9951bc7fdbfSTom Zanussi 		return -EAGAIN;
9961bc7fdbfSTom Zanussi 
9971bc7fdbfSTom Zanussi 	dir = encrypt ? &ctx->encrypt : &ctx->decrypt;
9981bc7fdbfSTom Zanussi 	req_ctx->encrypt = encrypt;
9991bc7fdbfSTom Zanussi 
10001bc7fdbfSTom Zanussi 	crypt = get_crypt_desc();
10011bc7fdbfSTom Zanussi 	if (!crypt)
10021bc7fdbfSTom Zanussi 		return -ENOMEM;
10031bc7fdbfSTom Zanussi 
10041bc7fdbfSTom Zanussi 	crypt->data.ablk_req = req;
10051bc7fdbfSTom Zanussi 	crypt->crypto_ctx = dir->npe_ctx_phys;
10061bc7fdbfSTom Zanussi 	crypt->mode = dir->npe_mode;
10071bc7fdbfSTom Zanussi 	crypt->init_len = dir->npe_ctx_idx;
10081bc7fdbfSTom Zanussi 
10091bc7fdbfSTom Zanussi 	crypt->crypt_offs = 0;
10101bc7fdbfSTom Zanussi 	crypt->crypt_len = nbytes;
10111bc7fdbfSTom Zanussi 
10121bc7fdbfSTom Zanussi 	BUG_ON(ivsize && !req->iv);
10131bc7fdbfSTom Zanussi 	memcpy(crypt->iv, req->iv, ivsize);
10141bc7fdbfSTom Zanussi 	if (ivsize > 0 && !encrypt) {
10151bc7fdbfSTom Zanussi 		offset = req->cryptlen - ivsize;
10161bc7fdbfSTom Zanussi 		scatterwalk_map_and_copy(req_ctx->iv, req->src, offset, ivsize, 0);
10171bc7fdbfSTom Zanussi 	}
10181bc7fdbfSTom Zanussi 	if (req->src != req->dst) {
10191bc7fdbfSTom Zanussi 		struct buffer_desc dst_hook;
10201bc7fdbfSTom Zanussi 
10211bc7fdbfSTom Zanussi 		crypt->mode |= NPE_OP_NOT_IN_PLACE;
10221bc7fdbfSTom Zanussi 		/* This was never tested by Intel
10231bc7fdbfSTom Zanussi 		 * for more than one dst buffer, I think. */
10241bc7fdbfSTom Zanussi 		req_ctx->dst = NULL;
10251bc7fdbfSTom Zanussi 		if (!chainup_buffers(dev, req->dst, nbytes, &dst_hook,
10261bc7fdbfSTom Zanussi 				     flags, DMA_FROM_DEVICE))
10271bc7fdbfSTom Zanussi 			goto free_buf_dest;
10281bc7fdbfSTom Zanussi 		src_direction = DMA_TO_DEVICE;
10291bc7fdbfSTom Zanussi 		req_ctx->dst = dst_hook.next;
10301bc7fdbfSTom Zanussi 		crypt->dst_buf = dst_hook.phys_next;
10311bc7fdbfSTom Zanussi 	} else {
10321bc7fdbfSTom Zanussi 		req_ctx->dst = NULL;
10331bc7fdbfSTom Zanussi 	}
10341bc7fdbfSTom Zanussi 	req_ctx->src = NULL;
10351bc7fdbfSTom Zanussi 	if (!chainup_buffers(dev, req->src, nbytes, &src_hook, flags,
10361bc7fdbfSTom Zanussi 			     src_direction))
10371bc7fdbfSTom Zanussi 		goto free_buf_src;
10381bc7fdbfSTom Zanussi 
10391bc7fdbfSTom Zanussi 	req_ctx->src = src_hook.next;
10401bc7fdbfSTom Zanussi 	crypt->src_buf = src_hook.phys_next;
10411bc7fdbfSTom Zanussi 	crypt->ctl_flags |= CTL_FLAG_PERFORM_ABLK;
10421bc7fdbfSTom Zanussi 	qmgr_put_entry(send_qid, crypt_virt2phys(crypt));
10431bc7fdbfSTom Zanussi 	BUG_ON(qmgr_stat_overflow(send_qid));
10441bc7fdbfSTom Zanussi 	return -EINPROGRESS;
10451bc7fdbfSTom Zanussi 
10461bc7fdbfSTom Zanussi free_buf_src:
10471bc7fdbfSTom Zanussi 	free_buf_chain(dev, req_ctx->src, crypt->src_buf);
10481bc7fdbfSTom Zanussi free_buf_dest:
10491bc7fdbfSTom Zanussi 	if (req->src != req->dst)
10501bc7fdbfSTom Zanussi 		free_buf_chain(dev, req_ctx->dst, crypt->dst_buf);
10511bc7fdbfSTom Zanussi 
10521bc7fdbfSTom Zanussi 	crypt->ctl_flags = CTL_FLAG_UNUSED;
10531bc7fdbfSTom Zanussi 	return -ENOMEM;
10541bc7fdbfSTom Zanussi }
10551bc7fdbfSTom Zanussi 
ablk_encrypt(struct skcipher_request * req)10561bc7fdbfSTom Zanussi static int ablk_encrypt(struct skcipher_request *req)
10571bc7fdbfSTom Zanussi {
10581bc7fdbfSTom Zanussi 	return ablk_perform(req, 1);
10591bc7fdbfSTom Zanussi }
10601bc7fdbfSTom Zanussi 
ablk_decrypt(struct skcipher_request * req)10611bc7fdbfSTom Zanussi static int ablk_decrypt(struct skcipher_request *req)
10621bc7fdbfSTom Zanussi {
10631bc7fdbfSTom Zanussi 	return ablk_perform(req, 0);
10641bc7fdbfSTom Zanussi }
10651bc7fdbfSTom Zanussi 
ablk_rfc3686_crypt(struct skcipher_request * req)10661bc7fdbfSTom Zanussi static int ablk_rfc3686_crypt(struct skcipher_request *req)
10671bc7fdbfSTom Zanussi {
10681bc7fdbfSTom Zanussi 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
10691bc7fdbfSTom Zanussi 	struct ixp_ctx *ctx = crypto_skcipher_ctx(tfm);
10701bc7fdbfSTom Zanussi 	u8 iv[CTR_RFC3686_BLOCK_SIZE];
10711bc7fdbfSTom Zanussi 	u8 *info = req->iv;
10721bc7fdbfSTom Zanussi 	int ret;
10731bc7fdbfSTom Zanussi 
10741bc7fdbfSTom Zanussi 	/* set up counter block */
10751bc7fdbfSTom Zanussi 	memcpy(iv, ctx->nonce, CTR_RFC3686_NONCE_SIZE);
10761bc7fdbfSTom Zanussi 	memcpy(iv + CTR_RFC3686_NONCE_SIZE, info, CTR_RFC3686_IV_SIZE);
10771bc7fdbfSTom Zanussi 
10781bc7fdbfSTom Zanussi 	/* initialize counter portion of counter block */
10791bc7fdbfSTom Zanussi 	*(__be32 *)(iv + CTR_RFC3686_NONCE_SIZE + CTR_RFC3686_IV_SIZE) =
10801bc7fdbfSTom Zanussi 		cpu_to_be32(1);
10811bc7fdbfSTom Zanussi 
10821bc7fdbfSTom Zanussi 	req->iv = iv;
10831bc7fdbfSTom Zanussi 	ret = ablk_perform(req, 1);
10841bc7fdbfSTom Zanussi 	req->iv = info;
10851bc7fdbfSTom Zanussi 	return ret;
10861bc7fdbfSTom Zanussi }
10871bc7fdbfSTom Zanussi 
aead_perform(struct aead_request * req,int encrypt,int cryptoffset,int eff_cryptlen,u8 * iv)10881bc7fdbfSTom Zanussi static int aead_perform(struct aead_request *req, int encrypt,
10891bc7fdbfSTom Zanussi 			int cryptoffset, int eff_cryptlen, u8 *iv)
10901bc7fdbfSTom Zanussi {
10911bc7fdbfSTom Zanussi 	struct crypto_aead *tfm = crypto_aead_reqtfm(req);
10921bc7fdbfSTom Zanussi 	struct ixp_ctx *ctx = crypto_aead_ctx(tfm);
10931bc7fdbfSTom Zanussi 	unsigned int ivsize = crypto_aead_ivsize(tfm);
10941bc7fdbfSTom Zanussi 	unsigned int authsize = crypto_aead_authsize(tfm);
10951bc7fdbfSTom Zanussi 	struct ix_sa_dir *dir;
10961bc7fdbfSTom Zanussi 	struct crypt_ctl *crypt;
10971bc7fdbfSTom Zanussi 	unsigned int cryptlen;
10981bc7fdbfSTom Zanussi 	struct buffer_desc *buf, src_hook;
10991bc7fdbfSTom Zanussi 	struct aead_ctx *req_ctx = aead_request_ctx(req);
11001bc7fdbfSTom Zanussi 	struct device *dev = &pdev->dev;
11011bc7fdbfSTom Zanussi 	gfp_t flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ?
11021bc7fdbfSTom Zanussi 				GFP_KERNEL : GFP_ATOMIC;
11031bc7fdbfSTom Zanussi 	enum dma_data_direction src_direction = DMA_BIDIRECTIONAL;
11041bc7fdbfSTom Zanussi 	unsigned int lastlen;
11051bc7fdbfSTom Zanussi 
11061bc7fdbfSTom Zanussi 	if (qmgr_stat_full(send_qid))
11071bc7fdbfSTom Zanussi 		return -EAGAIN;
11081bc7fdbfSTom Zanussi 	if (atomic_read(&ctx->configuring))
11091bc7fdbfSTom Zanussi 		return -EAGAIN;
11101bc7fdbfSTom Zanussi 
11111bc7fdbfSTom Zanussi 	if (encrypt) {
11121bc7fdbfSTom Zanussi 		dir = &ctx->encrypt;
11131bc7fdbfSTom Zanussi 		cryptlen = req->cryptlen;
11141bc7fdbfSTom Zanussi 	} else {
11151bc7fdbfSTom Zanussi 		dir = &ctx->decrypt;
11161bc7fdbfSTom Zanussi 		/* req->cryptlen includes the authsize when decrypting */
11171bc7fdbfSTom Zanussi 		cryptlen = req->cryptlen - authsize;
11181bc7fdbfSTom Zanussi 		eff_cryptlen -= authsize;
11191bc7fdbfSTom Zanussi 	}
11201bc7fdbfSTom Zanussi 	crypt = get_crypt_desc();
11211bc7fdbfSTom Zanussi 	if (!crypt)
11221bc7fdbfSTom Zanussi 		return -ENOMEM;
11231bc7fdbfSTom Zanussi 
11241bc7fdbfSTom Zanussi 	crypt->data.aead_req = req;
11251bc7fdbfSTom Zanussi 	crypt->crypto_ctx = dir->npe_ctx_phys;
11261bc7fdbfSTom Zanussi 	crypt->mode = dir->npe_mode;
11271bc7fdbfSTom Zanussi 	crypt->init_len = dir->npe_ctx_idx;
11281bc7fdbfSTom Zanussi 
11291bc7fdbfSTom Zanussi 	crypt->crypt_offs = cryptoffset;
11301bc7fdbfSTom Zanussi 	crypt->crypt_len = eff_cryptlen;
11311bc7fdbfSTom Zanussi 
11321bc7fdbfSTom Zanussi 	crypt->auth_offs = 0;
11331bc7fdbfSTom Zanussi 	crypt->auth_len = req->assoclen + cryptlen;
11341bc7fdbfSTom Zanussi 	BUG_ON(ivsize && !req->iv);
11351bc7fdbfSTom Zanussi 	memcpy(crypt->iv, req->iv, ivsize);
11361bc7fdbfSTom Zanussi 
11371bc7fdbfSTom Zanussi 	buf = chainup_buffers(dev, req->src, crypt->auth_len,
11381bc7fdbfSTom Zanussi 			      &src_hook, flags, src_direction);
11391bc7fdbfSTom Zanussi 	req_ctx->src = src_hook.next;
11401bc7fdbfSTom Zanussi 	crypt->src_buf = src_hook.phys_next;
11411bc7fdbfSTom Zanussi 	if (!buf)
11421bc7fdbfSTom Zanussi 		goto free_buf_src;
11431bc7fdbfSTom Zanussi 
11441bc7fdbfSTom Zanussi 	lastlen = buf->buf_len;
11451bc7fdbfSTom Zanussi 	if (lastlen >= authsize)
11461bc7fdbfSTom Zanussi 		crypt->icv_rev_aes = buf->phys_addr +
11471bc7fdbfSTom Zanussi 				     buf->buf_len - authsize;
11481bc7fdbfSTom Zanussi 
11491bc7fdbfSTom Zanussi 	req_ctx->dst = NULL;
11501bc7fdbfSTom Zanussi 
11511bc7fdbfSTom Zanussi 	if (req->src != req->dst) {
11521bc7fdbfSTom Zanussi 		struct buffer_desc dst_hook;
11531bc7fdbfSTom Zanussi 
11541bc7fdbfSTom Zanussi 		crypt->mode |= NPE_OP_NOT_IN_PLACE;
11551bc7fdbfSTom Zanussi 		src_direction = DMA_TO_DEVICE;
11561bc7fdbfSTom Zanussi 
11571bc7fdbfSTom Zanussi 		buf = chainup_buffers(dev, req->dst, crypt->auth_len,
11581bc7fdbfSTom Zanussi 				      &dst_hook, flags, DMA_FROM_DEVICE);
11591bc7fdbfSTom Zanussi 		req_ctx->dst = dst_hook.next;
11601bc7fdbfSTom Zanussi 		crypt->dst_buf = dst_hook.phys_next;
11611bc7fdbfSTom Zanussi 
11621bc7fdbfSTom Zanussi 		if (!buf)
11631bc7fdbfSTom Zanussi 			goto free_buf_dst;
11641bc7fdbfSTom Zanussi 
11651bc7fdbfSTom Zanussi 		if (encrypt) {
11661bc7fdbfSTom Zanussi 			lastlen = buf->buf_len;
11671bc7fdbfSTom Zanussi 			if (lastlen >= authsize)
11681bc7fdbfSTom Zanussi 				crypt->icv_rev_aes = buf->phys_addr +
11691bc7fdbfSTom Zanussi 						     buf->buf_len - authsize;
11701bc7fdbfSTom Zanussi 		}
11711bc7fdbfSTom Zanussi 	}
11721bc7fdbfSTom Zanussi 
11731bc7fdbfSTom Zanussi 	if (unlikely(lastlen < authsize)) {
1174586d492fSArnd Bergmann 		dma_addr_t dma;
11751bc7fdbfSTom Zanussi 		/* The 12 hmac bytes are scattered,
11761bc7fdbfSTom Zanussi 		 * we need to copy them into a safe buffer */
1177586d492fSArnd Bergmann 		req_ctx->hmac_virt = dma_pool_alloc(buffer_pool, flags, &dma);
11781bc7fdbfSTom Zanussi 		if (unlikely(!req_ctx->hmac_virt))
11791bc7fdbfSTom Zanussi 			goto free_buf_dst;
1180a4ca033dSDan Carpenter 		crypt->icv_rev_aes = dma;
11811bc7fdbfSTom Zanussi 		if (!encrypt) {
11821bc7fdbfSTom Zanussi 			scatterwalk_map_and_copy(req_ctx->hmac_virt,
11831bc7fdbfSTom Zanussi 						 req->src, cryptlen, authsize, 0);
11841bc7fdbfSTom Zanussi 		}
11851bc7fdbfSTom Zanussi 		req_ctx->encrypt = encrypt;
11861bc7fdbfSTom Zanussi 	} else {
11871bc7fdbfSTom Zanussi 		req_ctx->hmac_virt = NULL;
11881bc7fdbfSTom Zanussi 	}
11891bc7fdbfSTom Zanussi 
11901bc7fdbfSTom Zanussi 	crypt->ctl_flags |= CTL_FLAG_PERFORM_AEAD;
11911bc7fdbfSTom Zanussi 	qmgr_put_entry(send_qid, crypt_virt2phys(crypt));
11921bc7fdbfSTom Zanussi 	BUG_ON(qmgr_stat_overflow(send_qid));
11931bc7fdbfSTom Zanussi 	return -EINPROGRESS;
11941bc7fdbfSTom Zanussi 
11951bc7fdbfSTom Zanussi free_buf_dst:
11961bc7fdbfSTom Zanussi 	free_buf_chain(dev, req_ctx->dst, crypt->dst_buf);
11971bc7fdbfSTom Zanussi free_buf_src:
11981bc7fdbfSTom Zanussi 	free_buf_chain(dev, req_ctx->src, crypt->src_buf);
11991bc7fdbfSTom Zanussi 	crypt->ctl_flags = CTL_FLAG_UNUSED;
12001bc7fdbfSTom Zanussi 	return -ENOMEM;
12011bc7fdbfSTom Zanussi }
12021bc7fdbfSTom Zanussi 
aead_setup(struct crypto_aead * tfm,unsigned int authsize)12031bc7fdbfSTom Zanussi static int aead_setup(struct crypto_aead *tfm, unsigned int authsize)
12041bc7fdbfSTom Zanussi {
12051bc7fdbfSTom Zanussi 	struct ixp_ctx *ctx = crypto_aead_ctx(tfm);
12061bc7fdbfSTom Zanussi 	unsigned int digest_len = crypto_aead_maxauthsize(tfm);
12071bc7fdbfSTom Zanussi 	int ret;
12081bc7fdbfSTom Zanussi 
12091bc7fdbfSTom Zanussi 	if (!ctx->enckey_len && !ctx->authkey_len)
12101bc7fdbfSTom Zanussi 		return 0;
12111bc7fdbfSTom Zanussi 	init_completion(&ctx->completion);
12121bc7fdbfSTom Zanussi 	atomic_inc(&ctx->configuring);
12131bc7fdbfSTom Zanussi 
12141bc7fdbfSTom Zanussi 	reset_sa_dir(&ctx->encrypt);
12151bc7fdbfSTom Zanussi 	reset_sa_dir(&ctx->decrypt);
12161bc7fdbfSTom Zanussi 
12171bc7fdbfSTom Zanussi 	ret = setup_cipher(&tfm->base, 0, ctx->enckey, ctx->enckey_len);
12181bc7fdbfSTom Zanussi 	if (ret)
12191bc7fdbfSTom Zanussi 		goto out;
12201bc7fdbfSTom Zanussi 	ret = setup_cipher(&tfm->base, 1, ctx->enckey, ctx->enckey_len);
12211bc7fdbfSTom Zanussi 	if (ret)
12221bc7fdbfSTom Zanussi 		goto out;
12231bc7fdbfSTom Zanussi 	ret = setup_auth(&tfm->base, 0, authsize, ctx->authkey,
12241bc7fdbfSTom Zanussi 			 ctx->authkey_len, digest_len);
12251bc7fdbfSTom Zanussi 	if (ret)
12261bc7fdbfSTom Zanussi 		goto out;
12271bc7fdbfSTom Zanussi 	ret = setup_auth(&tfm->base, 1, authsize,  ctx->authkey,
12281bc7fdbfSTom Zanussi 			 ctx->authkey_len, digest_len);
12291bc7fdbfSTom Zanussi out:
12301bc7fdbfSTom Zanussi 	if (!atomic_dec_and_test(&ctx->configuring))
12311bc7fdbfSTom Zanussi 		wait_for_completion(&ctx->completion);
12321bc7fdbfSTom Zanussi 	return ret;
12331bc7fdbfSTom Zanussi }
12341bc7fdbfSTom Zanussi 
aead_setauthsize(struct crypto_aead * tfm,unsigned int authsize)12351bc7fdbfSTom Zanussi static int aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize)
12361bc7fdbfSTom Zanussi {
12371bc7fdbfSTom Zanussi 	int max = crypto_aead_maxauthsize(tfm) >> 2;
12381bc7fdbfSTom Zanussi 
12391bc7fdbfSTom Zanussi 	if ((authsize >> 2) < 1 || (authsize >> 2) > max || (authsize & 3))
12401bc7fdbfSTom Zanussi 		return -EINVAL;
12411bc7fdbfSTom Zanussi 	return aead_setup(tfm, authsize);
12421bc7fdbfSTom Zanussi }
12431bc7fdbfSTom Zanussi 
aead_setkey(struct crypto_aead * tfm,const u8 * key,unsigned int keylen)12441bc7fdbfSTom Zanussi static int aead_setkey(struct crypto_aead *tfm, const u8 *key,
12451bc7fdbfSTom Zanussi 		       unsigned int keylen)
12461bc7fdbfSTom Zanussi {
12471bc7fdbfSTom Zanussi 	struct ixp_ctx *ctx = crypto_aead_ctx(tfm);
12481bc7fdbfSTom Zanussi 	struct crypto_authenc_keys keys;
12491bc7fdbfSTom Zanussi 
12501bc7fdbfSTom Zanussi 	if (crypto_authenc_extractkeys(&keys, key, keylen) != 0)
12511bc7fdbfSTom Zanussi 		goto badkey;
12521bc7fdbfSTom Zanussi 
12531bc7fdbfSTom Zanussi 	if (keys.authkeylen > sizeof(ctx->authkey))
12541bc7fdbfSTom Zanussi 		goto badkey;
12551bc7fdbfSTom Zanussi 
12561bc7fdbfSTom Zanussi 	if (keys.enckeylen > sizeof(ctx->enckey))
12571bc7fdbfSTom Zanussi 		goto badkey;
12581bc7fdbfSTom Zanussi 
12591bc7fdbfSTom Zanussi 	memcpy(ctx->authkey, keys.authkey, keys.authkeylen);
12601bc7fdbfSTom Zanussi 	memcpy(ctx->enckey, keys.enckey, keys.enckeylen);
12611bc7fdbfSTom Zanussi 	ctx->authkey_len = keys.authkeylen;
12621bc7fdbfSTom Zanussi 	ctx->enckey_len = keys.enckeylen;
12631bc7fdbfSTom Zanussi 
12641bc7fdbfSTom Zanussi 	memzero_explicit(&keys, sizeof(keys));
12651bc7fdbfSTom Zanussi 	return aead_setup(tfm, crypto_aead_authsize(tfm));
12661bc7fdbfSTom Zanussi badkey:
12671bc7fdbfSTom Zanussi 	memzero_explicit(&keys, sizeof(keys));
12681bc7fdbfSTom Zanussi 	return -EINVAL;
12691bc7fdbfSTom Zanussi }
12701bc7fdbfSTom Zanussi 
des3_aead_setkey(struct crypto_aead * tfm,const u8 * key,unsigned int keylen)12711bc7fdbfSTom Zanussi static int des3_aead_setkey(struct crypto_aead *tfm, const u8 *key,
12721bc7fdbfSTom Zanussi 			    unsigned int keylen)
12731bc7fdbfSTom Zanussi {
12741bc7fdbfSTom Zanussi 	struct ixp_ctx *ctx = crypto_aead_ctx(tfm);
12751bc7fdbfSTom Zanussi 	struct crypto_authenc_keys keys;
12761bc7fdbfSTom Zanussi 	int err;
12771bc7fdbfSTom Zanussi 
12781bc7fdbfSTom Zanussi 	err = crypto_authenc_extractkeys(&keys, key, keylen);
12791bc7fdbfSTom Zanussi 	if (unlikely(err))
12801bc7fdbfSTom Zanussi 		goto badkey;
12811bc7fdbfSTom Zanussi 
12821bc7fdbfSTom Zanussi 	err = -EINVAL;
12831bc7fdbfSTom Zanussi 	if (keys.authkeylen > sizeof(ctx->authkey))
12841bc7fdbfSTom Zanussi 		goto badkey;
12851bc7fdbfSTom Zanussi 
12861bc7fdbfSTom Zanussi 	err = verify_aead_des3_key(tfm, keys.enckey, keys.enckeylen);
12871bc7fdbfSTom Zanussi 	if (err)
12881bc7fdbfSTom Zanussi 		goto badkey;
12891bc7fdbfSTom Zanussi 
12901bc7fdbfSTom Zanussi 	memcpy(ctx->authkey, keys.authkey, keys.authkeylen);
12911bc7fdbfSTom Zanussi 	memcpy(ctx->enckey, keys.enckey, keys.enckeylen);
12921bc7fdbfSTom Zanussi 	ctx->authkey_len = keys.authkeylen;
12931bc7fdbfSTom Zanussi 	ctx->enckey_len = keys.enckeylen;
12941bc7fdbfSTom Zanussi 
12951bc7fdbfSTom Zanussi 	memzero_explicit(&keys, sizeof(keys));
12961bc7fdbfSTom Zanussi 	return aead_setup(tfm, crypto_aead_authsize(tfm));
12971bc7fdbfSTom Zanussi badkey:
12981bc7fdbfSTom Zanussi 	memzero_explicit(&keys, sizeof(keys));
12991bc7fdbfSTom Zanussi 	return err;
13001bc7fdbfSTom Zanussi }
13011bc7fdbfSTom Zanussi 
aead_encrypt(struct aead_request * req)13021bc7fdbfSTom Zanussi static int aead_encrypt(struct aead_request *req)
13031bc7fdbfSTom Zanussi {
13041bc7fdbfSTom Zanussi 	return aead_perform(req, 1, req->assoclen, req->cryptlen, req->iv);
13051bc7fdbfSTom Zanussi }
13061bc7fdbfSTom Zanussi 
aead_decrypt(struct aead_request * req)13071bc7fdbfSTom Zanussi static int aead_decrypt(struct aead_request *req)
13081bc7fdbfSTom Zanussi {
13091bc7fdbfSTom Zanussi 	return aead_perform(req, 0, req->assoclen, req->cryptlen, req->iv);
13101bc7fdbfSTom Zanussi }
13111bc7fdbfSTom Zanussi 
13121bc7fdbfSTom Zanussi static struct ixp_alg ixp4xx_algos[] = {
13131bc7fdbfSTom Zanussi {
13141bc7fdbfSTom Zanussi 	.crypto	= {
13151bc7fdbfSTom Zanussi 		.base.cra_name		= "cbc(des)",
13161bc7fdbfSTom Zanussi 		.base.cra_blocksize	= DES_BLOCK_SIZE,
13171bc7fdbfSTom Zanussi 
13181bc7fdbfSTom Zanussi 		.min_keysize		= DES_KEY_SIZE,
13191bc7fdbfSTom Zanussi 		.max_keysize		= DES_KEY_SIZE,
13201bc7fdbfSTom Zanussi 		.ivsize			= DES_BLOCK_SIZE,
13211bc7fdbfSTom Zanussi 	},
13221bc7fdbfSTom Zanussi 	.cfg_enc = CIPH_ENCR | MOD_DES | MOD_CBC_ENC | KEYLEN_192,
13231bc7fdbfSTom Zanussi 	.cfg_dec = CIPH_DECR | MOD_DES | MOD_CBC_DEC | KEYLEN_192,
13241bc7fdbfSTom Zanussi 
13251bc7fdbfSTom Zanussi }, {
13261bc7fdbfSTom Zanussi 	.crypto	= {
13271bc7fdbfSTom Zanussi 		.base.cra_name		= "ecb(des)",
13281bc7fdbfSTom Zanussi 		.base.cra_blocksize	= DES_BLOCK_SIZE,
13291bc7fdbfSTom Zanussi 		.min_keysize		= DES_KEY_SIZE,
13301bc7fdbfSTom Zanussi 		.max_keysize		= DES_KEY_SIZE,
13311bc7fdbfSTom Zanussi 	},
13321bc7fdbfSTom Zanussi 	.cfg_enc = CIPH_ENCR | MOD_DES | MOD_ECB | KEYLEN_192,
13331bc7fdbfSTom Zanussi 	.cfg_dec = CIPH_DECR | MOD_DES | MOD_ECB | KEYLEN_192,
13341bc7fdbfSTom Zanussi }, {
13351bc7fdbfSTom Zanussi 	.crypto	= {
13361bc7fdbfSTom Zanussi 		.base.cra_name		= "cbc(des3_ede)",
13371bc7fdbfSTom Zanussi 		.base.cra_blocksize	= DES3_EDE_BLOCK_SIZE,
13381bc7fdbfSTom Zanussi 
13391bc7fdbfSTom Zanussi 		.min_keysize		= DES3_EDE_KEY_SIZE,
13401bc7fdbfSTom Zanussi 		.max_keysize		= DES3_EDE_KEY_SIZE,
13411bc7fdbfSTom Zanussi 		.ivsize			= DES3_EDE_BLOCK_SIZE,
13421bc7fdbfSTom Zanussi 		.setkey			= ablk_des3_setkey,
13431bc7fdbfSTom Zanussi 	},
13441bc7fdbfSTom Zanussi 	.cfg_enc = CIPH_ENCR | MOD_3DES | MOD_CBC_ENC | KEYLEN_192,
13451bc7fdbfSTom Zanussi 	.cfg_dec = CIPH_DECR | MOD_3DES | MOD_CBC_DEC | KEYLEN_192,
13461bc7fdbfSTom Zanussi }, {
13471bc7fdbfSTom Zanussi 	.crypto	= {
13481bc7fdbfSTom Zanussi 		.base.cra_name		= "ecb(des3_ede)",
13491bc7fdbfSTom Zanussi 		.base.cra_blocksize	= DES3_EDE_BLOCK_SIZE,
13501bc7fdbfSTom Zanussi 
13511bc7fdbfSTom Zanussi 		.min_keysize		= DES3_EDE_KEY_SIZE,
13521bc7fdbfSTom Zanussi 		.max_keysize		= DES3_EDE_KEY_SIZE,
13531bc7fdbfSTom Zanussi 		.setkey			= ablk_des3_setkey,
13541bc7fdbfSTom Zanussi 	},
13551bc7fdbfSTom Zanussi 	.cfg_enc = CIPH_ENCR | MOD_3DES | MOD_ECB | KEYLEN_192,
13561bc7fdbfSTom Zanussi 	.cfg_dec = CIPH_DECR | MOD_3DES | MOD_ECB | KEYLEN_192,
13571bc7fdbfSTom Zanussi }, {
13581bc7fdbfSTom Zanussi 	.crypto	= {
13591bc7fdbfSTom Zanussi 		.base.cra_name		= "cbc(aes)",
13601bc7fdbfSTom Zanussi 		.base.cra_blocksize	= AES_BLOCK_SIZE,
13611bc7fdbfSTom Zanussi 
13621bc7fdbfSTom Zanussi 		.min_keysize		= AES_MIN_KEY_SIZE,
13631bc7fdbfSTom Zanussi 		.max_keysize		= AES_MAX_KEY_SIZE,
13641bc7fdbfSTom Zanussi 		.ivsize			= AES_BLOCK_SIZE,
13651bc7fdbfSTom Zanussi 	},
13661bc7fdbfSTom Zanussi 	.cfg_enc = CIPH_ENCR | MOD_AES | MOD_CBC_ENC,
13671bc7fdbfSTom Zanussi 	.cfg_dec = CIPH_DECR | MOD_AES | MOD_CBC_DEC,
13681bc7fdbfSTom Zanussi }, {
13691bc7fdbfSTom Zanussi 	.crypto	= {
13701bc7fdbfSTom Zanussi 		.base.cra_name		= "ecb(aes)",
13711bc7fdbfSTom Zanussi 		.base.cra_blocksize	= AES_BLOCK_SIZE,
13721bc7fdbfSTom Zanussi 
13731bc7fdbfSTom Zanussi 		.min_keysize		= AES_MIN_KEY_SIZE,
13741bc7fdbfSTom Zanussi 		.max_keysize		= AES_MAX_KEY_SIZE,
13751bc7fdbfSTom Zanussi 	},
13761bc7fdbfSTom Zanussi 	.cfg_enc = CIPH_ENCR | MOD_AES | MOD_ECB,
13771bc7fdbfSTom Zanussi 	.cfg_dec = CIPH_DECR | MOD_AES | MOD_ECB,
13781bc7fdbfSTom Zanussi }, {
13791bc7fdbfSTom Zanussi 	.crypto	= {
13801bc7fdbfSTom Zanussi 		.base.cra_name		= "ctr(aes)",
13811bc7fdbfSTom Zanussi 		.base.cra_blocksize	= 1,
13821bc7fdbfSTom Zanussi 
13831bc7fdbfSTom Zanussi 		.min_keysize		= AES_MIN_KEY_SIZE,
13841bc7fdbfSTom Zanussi 		.max_keysize		= AES_MAX_KEY_SIZE,
13851bc7fdbfSTom Zanussi 		.ivsize			= AES_BLOCK_SIZE,
13861bc7fdbfSTom Zanussi 	},
13871bc7fdbfSTom Zanussi 	.cfg_enc = CIPH_ENCR | MOD_AES | MOD_CTR,
13881bc7fdbfSTom Zanussi 	.cfg_dec = CIPH_ENCR | MOD_AES | MOD_CTR,
13891bc7fdbfSTom Zanussi }, {
13901bc7fdbfSTom Zanussi 	.crypto	= {
13911bc7fdbfSTom Zanussi 		.base.cra_name		= "rfc3686(ctr(aes))",
13921bc7fdbfSTom Zanussi 		.base.cra_blocksize	= 1,
13931bc7fdbfSTom Zanussi 
13941bc7fdbfSTom Zanussi 		.min_keysize		= AES_MIN_KEY_SIZE,
13951bc7fdbfSTom Zanussi 		.max_keysize		= AES_MAX_KEY_SIZE,
13961bc7fdbfSTom Zanussi 		.ivsize			= AES_BLOCK_SIZE,
13971bc7fdbfSTom Zanussi 		.setkey			= ablk_rfc3686_setkey,
13981bc7fdbfSTom Zanussi 		.encrypt		= ablk_rfc3686_crypt,
13991bc7fdbfSTom Zanussi 		.decrypt		= ablk_rfc3686_crypt,
14001bc7fdbfSTom Zanussi 	},
14011bc7fdbfSTom Zanussi 	.cfg_enc = CIPH_ENCR | MOD_AES | MOD_CTR,
14021bc7fdbfSTom Zanussi 	.cfg_dec = CIPH_ENCR | MOD_AES | MOD_CTR,
14031bc7fdbfSTom Zanussi } };
14041bc7fdbfSTom Zanussi 
14051bc7fdbfSTom Zanussi static struct ixp_aead_alg ixp4xx_aeads[] = {
14061bc7fdbfSTom Zanussi {
14071bc7fdbfSTom Zanussi 	.crypto	= {
14081bc7fdbfSTom Zanussi 		.base = {
14091bc7fdbfSTom Zanussi 			.cra_name	= "authenc(hmac(md5),cbc(des))",
14101bc7fdbfSTom Zanussi 			.cra_blocksize	= DES_BLOCK_SIZE,
14111bc7fdbfSTom Zanussi 		},
14121bc7fdbfSTom Zanussi 		.ivsize		= DES_BLOCK_SIZE,
14131bc7fdbfSTom Zanussi 		.maxauthsize	= MD5_DIGEST_SIZE,
14141bc7fdbfSTom Zanussi 	},
14151bc7fdbfSTom Zanussi 	.hash = &hash_alg_md5,
14161bc7fdbfSTom Zanussi 	.cfg_enc = CIPH_ENCR | MOD_DES | MOD_CBC_ENC | KEYLEN_192,
14171bc7fdbfSTom Zanussi 	.cfg_dec = CIPH_DECR | MOD_DES | MOD_CBC_DEC | KEYLEN_192,
14181bc7fdbfSTom Zanussi }, {
14191bc7fdbfSTom Zanussi 	.crypto	= {
14201bc7fdbfSTom Zanussi 		.base = {
14211bc7fdbfSTom Zanussi 			.cra_name	= "authenc(hmac(md5),cbc(des3_ede))",
14221bc7fdbfSTom Zanussi 			.cra_blocksize	= DES3_EDE_BLOCK_SIZE,
14231bc7fdbfSTom Zanussi 		},
14241bc7fdbfSTom Zanussi 		.ivsize		= DES3_EDE_BLOCK_SIZE,
14251bc7fdbfSTom Zanussi 		.maxauthsize	= MD5_DIGEST_SIZE,
14261bc7fdbfSTom Zanussi 		.setkey		= des3_aead_setkey,
14271bc7fdbfSTom Zanussi 	},
14281bc7fdbfSTom Zanussi 	.hash = &hash_alg_md5,
14291bc7fdbfSTom Zanussi 	.cfg_enc = CIPH_ENCR | MOD_3DES | MOD_CBC_ENC | KEYLEN_192,
14301bc7fdbfSTom Zanussi 	.cfg_dec = CIPH_DECR | MOD_3DES | MOD_CBC_DEC | KEYLEN_192,
14311bc7fdbfSTom Zanussi }, {
14321bc7fdbfSTom Zanussi 	.crypto	= {
14331bc7fdbfSTom Zanussi 		.base = {
14341bc7fdbfSTom Zanussi 			.cra_name	= "authenc(hmac(sha1),cbc(des))",
14351bc7fdbfSTom Zanussi 			.cra_blocksize	= DES_BLOCK_SIZE,
14361bc7fdbfSTom Zanussi 		},
14371bc7fdbfSTom Zanussi 			.ivsize		= DES_BLOCK_SIZE,
14381bc7fdbfSTom Zanussi 			.maxauthsize	= SHA1_DIGEST_SIZE,
14391bc7fdbfSTom Zanussi 	},
14401bc7fdbfSTom Zanussi 	.hash = &hash_alg_sha1,
14411bc7fdbfSTom Zanussi 	.cfg_enc = CIPH_ENCR | MOD_DES | MOD_CBC_ENC | KEYLEN_192,
14421bc7fdbfSTom Zanussi 	.cfg_dec = CIPH_DECR | MOD_DES | MOD_CBC_DEC | KEYLEN_192,
14431bc7fdbfSTom Zanussi }, {
14441bc7fdbfSTom Zanussi 	.crypto	= {
14451bc7fdbfSTom Zanussi 		.base = {
14461bc7fdbfSTom Zanussi 			.cra_name	= "authenc(hmac(sha1),cbc(des3_ede))",
14471bc7fdbfSTom Zanussi 			.cra_blocksize	= DES3_EDE_BLOCK_SIZE,
14481bc7fdbfSTom Zanussi 		},
14491bc7fdbfSTom Zanussi 		.ivsize		= DES3_EDE_BLOCK_SIZE,
14501bc7fdbfSTom Zanussi 		.maxauthsize	= SHA1_DIGEST_SIZE,
14511bc7fdbfSTom Zanussi 		.setkey		= des3_aead_setkey,
14521bc7fdbfSTom Zanussi 	},
14531bc7fdbfSTom Zanussi 	.hash = &hash_alg_sha1,
14541bc7fdbfSTom Zanussi 	.cfg_enc = CIPH_ENCR | MOD_3DES | MOD_CBC_ENC | KEYLEN_192,
14551bc7fdbfSTom Zanussi 	.cfg_dec = CIPH_DECR | MOD_3DES | MOD_CBC_DEC | KEYLEN_192,
14561bc7fdbfSTom Zanussi }, {
14571bc7fdbfSTom Zanussi 	.crypto	= {
14581bc7fdbfSTom Zanussi 		.base = {
14591bc7fdbfSTom Zanussi 			.cra_name	= "authenc(hmac(md5),cbc(aes))",
14601bc7fdbfSTom Zanussi 			.cra_blocksize	= AES_BLOCK_SIZE,
14611bc7fdbfSTom Zanussi 		},
14621bc7fdbfSTom Zanussi 		.ivsize		= AES_BLOCK_SIZE,
14631bc7fdbfSTom Zanussi 		.maxauthsize	= MD5_DIGEST_SIZE,
14641bc7fdbfSTom Zanussi 	},
14651bc7fdbfSTom Zanussi 	.hash = &hash_alg_md5,
14661bc7fdbfSTom Zanussi 	.cfg_enc = CIPH_ENCR | MOD_AES | MOD_CBC_ENC,
14671bc7fdbfSTom Zanussi 	.cfg_dec = CIPH_DECR | MOD_AES | MOD_CBC_DEC,
14681bc7fdbfSTom Zanussi }, {
14691bc7fdbfSTom Zanussi 	.crypto	= {
14701bc7fdbfSTom Zanussi 		.base = {
14711bc7fdbfSTom Zanussi 			.cra_name	= "authenc(hmac(sha1),cbc(aes))",
14721bc7fdbfSTom Zanussi 			.cra_blocksize	= AES_BLOCK_SIZE,
14731bc7fdbfSTom Zanussi 		},
14741bc7fdbfSTom Zanussi 		.ivsize		= AES_BLOCK_SIZE,
14751bc7fdbfSTom Zanussi 		.maxauthsize	= SHA1_DIGEST_SIZE,
14761bc7fdbfSTom Zanussi 	},
14771bc7fdbfSTom Zanussi 	.hash = &hash_alg_sha1,
14781bc7fdbfSTom Zanussi 	.cfg_enc = CIPH_ENCR | MOD_AES | MOD_CBC_ENC,
14791bc7fdbfSTom Zanussi 	.cfg_dec = CIPH_DECR | MOD_AES | MOD_CBC_DEC,
14801bc7fdbfSTom Zanussi } };
14811bc7fdbfSTom Zanussi 
14821bc7fdbfSTom Zanussi #define IXP_POSTFIX "-ixp4xx"
14831bc7fdbfSTom Zanussi 
ixp_crypto_probe(struct platform_device * _pdev)14841bc7fdbfSTom Zanussi static int ixp_crypto_probe(struct platform_device *_pdev)
14851bc7fdbfSTom Zanussi {
14861bc7fdbfSTom Zanussi 	struct device *dev = &_pdev->dev;
14871bc7fdbfSTom Zanussi 	int num = ARRAY_SIZE(ixp4xx_algos);
14881bc7fdbfSTom Zanussi 	int i, err;
14891bc7fdbfSTom Zanussi 
14901bc7fdbfSTom Zanussi 	pdev = _pdev;
14911bc7fdbfSTom Zanussi 
14921bc7fdbfSTom Zanussi 	err = init_ixp_crypto(dev);
14931bc7fdbfSTom Zanussi 	if (err)
14941bc7fdbfSTom Zanussi 		return err;
14951bc7fdbfSTom Zanussi 
14961bc7fdbfSTom Zanussi 	for (i = 0; i < num; i++) {
14971bc7fdbfSTom Zanussi 		struct skcipher_alg *cra = &ixp4xx_algos[i].crypto;
14981bc7fdbfSTom Zanussi 
14991bc7fdbfSTom Zanussi 		if (snprintf(cra->base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
15001bc7fdbfSTom Zanussi 			     "%s"IXP_POSTFIX, cra->base.cra_name) >=
15011bc7fdbfSTom Zanussi 			     CRYPTO_MAX_ALG_NAME)
15021bc7fdbfSTom Zanussi 			continue;
15031bc7fdbfSTom Zanussi 		if (!support_aes && (ixp4xx_algos[i].cfg_enc & MOD_AES))
15041bc7fdbfSTom Zanussi 			continue;
15051bc7fdbfSTom Zanussi 
15061bc7fdbfSTom Zanussi 		/* block ciphers */
15071bc7fdbfSTom Zanussi 		cra->base.cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY |
15081bc7fdbfSTom Zanussi 				      CRYPTO_ALG_ASYNC |
15091bc7fdbfSTom Zanussi 				      CRYPTO_ALG_ALLOCATES_MEMORY |
15101bc7fdbfSTom Zanussi 				      CRYPTO_ALG_NEED_FALLBACK;
15111bc7fdbfSTom Zanussi 		if (!cra->setkey)
15121bc7fdbfSTom Zanussi 			cra->setkey = ablk_setkey;
15131bc7fdbfSTom Zanussi 		if (!cra->encrypt)
15141bc7fdbfSTom Zanussi 			cra->encrypt = ablk_encrypt;
15151bc7fdbfSTom Zanussi 		if (!cra->decrypt)
15161bc7fdbfSTom Zanussi 			cra->decrypt = ablk_decrypt;
15171bc7fdbfSTom Zanussi 		cra->init = init_tfm_ablk;
15181bc7fdbfSTom Zanussi 		cra->exit = exit_tfm_ablk;
15191bc7fdbfSTom Zanussi 
15201bc7fdbfSTom Zanussi 		cra->base.cra_ctxsize = sizeof(struct ixp_ctx);
15211bc7fdbfSTom Zanussi 		cra->base.cra_module = THIS_MODULE;
15221bc7fdbfSTom Zanussi 		cra->base.cra_alignmask = 3;
15231bc7fdbfSTom Zanussi 		cra->base.cra_priority = 300;
15241bc7fdbfSTom Zanussi 		if (crypto_register_skcipher(cra))
15251bc7fdbfSTom Zanussi 			dev_err(&pdev->dev, "Failed to register '%s'\n",
15261bc7fdbfSTom Zanussi 				cra->base.cra_name);
15271bc7fdbfSTom Zanussi 		else
15281bc7fdbfSTom Zanussi 			ixp4xx_algos[i].registered = 1;
15291bc7fdbfSTom Zanussi 	}
15301bc7fdbfSTom Zanussi 
15311bc7fdbfSTom Zanussi 	for (i = 0; i < ARRAY_SIZE(ixp4xx_aeads); i++) {
15321bc7fdbfSTom Zanussi 		struct aead_alg *cra = &ixp4xx_aeads[i].crypto;
15331bc7fdbfSTom Zanussi 
15341bc7fdbfSTom Zanussi 		if (snprintf(cra->base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
15351bc7fdbfSTom Zanussi 			     "%s"IXP_POSTFIX, cra->base.cra_name) >=
15361bc7fdbfSTom Zanussi 		    CRYPTO_MAX_ALG_NAME)
15371bc7fdbfSTom Zanussi 			continue;
15381bc7fdbfSTom Zanussi 		if (!support_aes && (ixp4xx_algos[i].cfg_enc & MOD_AES))
15391bc7fdbfSTom Zanussi 			continue;
15401bc7fdbfSTom Zanussi 
15411bc7fdbfSTom Zanussi 		/* authenc */
15421bc7fdbfSTom Zanussi 		cra->base.cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY |
15431bc7fdbfSTom Zanussi 				      CRYPTO_ALG_ASYNC |
15441bc7fdbfSTom Zanussi 				      CRYPTO_ALG_ALLOCATES_MEMORY;
15451bc7fdbfSTom Zanussi 		cra->setkey = cra->setkey ?: aead_setkey;
15461bc7fdbfSTom Zanussi 		cra->setauthsize = aead_setauthsize;
15471bc7fdbfSTom Zanussi 		cra->encrypt = aead_encrypt;
15481bc7fdbfSTom Zanussi 		cra->decrypt = aead_decrypt;
15491bc7fdbfSTom Zanussi 		cra->init = init_tfm_aead;
15501bc7fdbfSTom Zanussi 		cra->exit = exit_tfm_aead;
15511bc7fdbfSTom Zanussi 
15521bc7fdbfSTom Zanussi 		cra->base.cra_ctxsize = sizeof(struct ixp_ctx);
15531bc7fdbfSTom Zanussi 		cra->base.cra_module = THIS_MODULE;
15541bc7fdbfSTom Zanussi 		cra->base.cra_alignmask = 3;
15551bc7fdbfSTom Zanussi 		cra->base.cra_priority = 300;
15561bc7fdbfSTom Zanussi 
15571bc7fdbfSTom Zanussi 		if (crypto_register_aead(cra))
15581bc7fdbfSTom Zanussi 			dev_err(&pdev->dev, "Failed to register '%s'\n",
15591bc7fdbfSTom Zanussi 				cra->base.cra_driver_name);
15601bc7fdbfSTom Zanussi 		else
15611bc7fdbfSTom Zanussi 			ixp4xx_aeads[i].registered = 1;
15621bc7fdbfSTom Zanussi 	}
15631bc7fdbfSTom Zanussi 	return 0;
15641bc7fdbfSTom Zanussi }
15651bc7fdbfSTom Zanussi 
ixp_crypto_remove(struct platform_device * pdev)1566*2fd7c206SUwe Kleine-König static void ixp_crypto_remove(struct platform_device *pdev)
15671bc7fdbfSTom Zanussi {
15681bc7fdbfSTom Zanussi 	int num = ARRAY_SIZE(ixp4xx_algos);
15691bc7fdbfSTom Zanussi 	int i;
15701bc7fdbfSTom Zanussi 
15711bc7fdbfSTom Zanussi 	for (i = 0; i < ARRAY_SIZE(ixp4xx_aeads); i++) {
15721bc7fdbfSTom Zanussi 		if (ixp4xx_aeads[i].registered)
15731bc7fdbfSTom Zanussi 			crypto_unregister_aead(&ixp4xx_aeads[i].crypto);
15741bc7fdbfSTom Zanussi 	}
15751bc7fdbfSTom Zanussi 
15761bc7fdbfSTom Zanussi 	for (i = 0; i < num; i++) {
15771bc7fdbfSTom Zanussi 		if (ixp4xx_algos[i].registered)
15781bc7fdbfSTom Zanussi 			crypto_unregister_skcipher(&ixp4xx_algos[i].crypto);
15791bc7fdbfSTom Zanussi 	}
15801bc7fdbfSTom Zanussi 	release_ixp_crypto(&pdev->dev);
15811bc7fdbfSTom Zanussi }
15821bc7fdbfSTom Zanussi static const struct of_device_id ixp4xx_crypto_of_match[] = {
15831bc7fdbfSTom Zanussi 	{
15841bc7fdbfSTom Zanussi 		.compatible = "intel,ixp4xx-crypto",
15851bc7fdbfSTom Zanussi 	},
15861bc7fdbfSTom Zanussi 	{},
15871bc7fdbfSTom Zanussi };
15881bc7fdbfSTom Zanussi 
15891bc7fdbfSTom Zanussi static struct platform_driver ixp_crypto_driver = {
15901bc7fdbfSTom Zanussi 	.probe = ixp_crypto_probe,
1591*2fd7c206SUwe Kleine-König 	.remove_new = ixp_crypto_remove,
15921bc7fdbfSTom Zanussi 	.driver = {
15931bc7fdbfSTom Zanussi 		.name = "ixp4xx_crypto",
15941bc7fdbfSTom Zanussi 		.of_match_table = ixp4xx_crypto_of_match,
15951bc7fdbfSTom Zanussi 	},
15961bc7fdbfSTom Zanussi };
15971bc7fdbfSTom Zanussi module_platform_driver(ixp_crypto_driver);
15981bc7fdbfSTom Zanussi 
15991bc7fdbfSTom Zanussi MODULE_LICENSE("GPL");
16001bc7fdbfSTom Zanussi MODULE_AUTHOR("Christian Hohnstaedt <chohnstaedt@innominate.com>");
16011bc7fdbfSTom Zanussi MODULE_DESCRIPTION("IXP4xx hardware crypto");
16021bc7fdbfSTom Zanussi 
1603