^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* Copyright (C) 2004-2006, Advanced Micro Devices, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/pci.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/pci_ids.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/crypto.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <crypto/algapi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <crypto/aes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <crypto/internal/cipher.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <crypto/internal/skcipher.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include "geode-aes.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) /* Static structures */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) static void __iomem *_iobase;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) static spinlock_t lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) /* Write a 128 bit field (either a writable key or IV) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) static inline void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) _writefield(u32 offset, const void *value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) for (i = 0; i < 4; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) iowrite32(((const u32 *) value)[i], _iobase + offset + (i * 4));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) /* Read a 128 bit field (either a writable key or IV) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) static inline void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) _readfield(u32 offset, void *value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) for (i = 0; i < 4; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) ((u32 *) value)[i] = ioread32(_iobase + offset + (i * 4));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) do_crypt(const void *src, void *dst, u32 len, u32 flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) u32 status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) u32 counter = AES_OP_TIMEOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) iowrite32(virt_to_phys((void *)src), _iobase + AES_SOURCEA_REG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) iowrite32(virt_to_phys(dst), _iobase + AES_DSTA_REG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) iowrite32(len, _iobase + AES_LENA_REG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) /* Start the operation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) iowrite32(AES_CTRL_START | flags, _iobase + AES_CTRLA_REG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) status = ioread32(_iobase + AES_INTR_REG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) cpu_relax();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) } while (!(status & AES_INTRA_PENDING) && --counter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) /* Clear the event */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) iowrite32((status & 0xFF) | AES_INTRA_PENDING, _iobase + AES_INTR_REG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) return counter ? 0 : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) geode_aes_crypt(const struct geode_aes_tfm_ctx *tctx, const void *src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) void *dst, u32 len, u8 *iv, int mode, int dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) u32 flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) unsigned long iflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) /* If the source and destination is the same, then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) * we need to turn on the coherent flags, otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * we don't need to worry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) flags |= (AES_CTRL_DCA | AES_CTRL_SCA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) if (dir == AES_DIR_ENCRYPT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) flags |= AES_CTRL_ENCRYPT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) /* Start the critical section */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) spin_lock_irqsave(&lock, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) if (mode == AES_MODE_CBC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) flags |= AES_CTRL_CBC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) _writefield(AES_WRITEIV0_REG, iv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) flags |= AES_CTRL_WRKEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) _writefield(AES_WRITEKEY0_REG, tctx->key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) ret = do_crypt(src, dst, len, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) BUG_ON(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) if (mode == AES_MODE_CBC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) _readfield(AES_WRITEIV0_REG, iv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) spin_unlock_irqrestore(&lock, iflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) /* CRYPTO-API Functions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) static int geode_setkey_cip(struct crypto_tfm *tfm, const u8 *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) unsigned int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) struct geode_aes_tfm_ctx *tctx = crypto_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) tctx->keylen = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (len == AES_KEYSIZE_128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) memcpy(tctx->key, key, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) if (len != AES_KEYSIZE_192 && len != AES_KEYSIZE_256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) /* not supported at all */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) * The requested key size is not supported by HW, do a fallback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) tctx->fallback.cip->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) tctx->fallback.cip->base.crt_flags |=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) (tfm->crt_flags & CRYPTO_TFM_REQ_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) return crypto_cipher_setkey(tctx->fallback.cip, key, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) static int geode_setkey_skcipher(struct crypto_skcipher *tfm, const u8 *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) unsigned int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) struct geode_aes_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) tctx->keylen = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) if (len == AES_KEYSIZE_128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) memcpy(tctx->key, key, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) if (len != AES_KEYSIZE_192 && len != AES_KEYSIZE_256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) /* not supported at all */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) * The requested key size is not supported by HW, do a fallback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) crypto_skcipher_clear_flags(tctx->fallback.skcipher,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) CRYPTO_TFM_REQ_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) crypto_skcipher_set_flags(tctx->fallback.skcipher,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) crypto_skcipher_get_flags(tfm) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) CRYPTO_TFM_REQ_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) return crypto_skcipher_setkey(tctx->fallback.skcipher, key, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) geode_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) const struct geode_aes_tfm_ctx *tctx = crypto_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) if (unlikely(tctx->keylen != AES_KEYSIZE_128)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) crypto_cipher_encrypt_one(tctx->fallback.cip, out, in);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) geode_aes_crypt(tctx, in, out, AES_BLOCK_SIZE, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) AES_MODE_ECB, AES_DIR_ENCRYPT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) geode_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) const struct geode_aes_tfm_ctx *tctx = crypto_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) if (unlikely(tctx->keylen != AES_KEYSIZE_128)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) crypto_cipher_decrypt_one(tctx->fallback.cip, out, in);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) geode_aes_crypt(tctx, in, out, AES_BLOCK_SIZE, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) AES_MODE_ECB, AES_DIR_DECRYPT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) static int fallback_init_cip(struct crypto_tfm *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) const char *name = crypto_tfm_alg_name(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) struct geode_aes_tfm_ctx *tctx = crypto_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) tctx->fallback.cip = crypto_alloc_cipher(name, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) CRYPTO_ALG_NEED_FALLBACK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) if (IS_ERR(tctx->fallback.cip)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) printk(KERN_ERR "Error allocating fallback algo %s\n", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) return PTR_ERR(tctx->fallback.cip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) static void fallback_exit_cip(struct crypto_tfm *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) struct geode_aes_tfm_ctx *tctx = crypto_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) crypto_free_cipher(tctx->fallback.cip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) static struct crypto_alg geode_alg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) .cra_name = "aes",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) .cra_driver_name = "geode-aes",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) .cra_priority = 300,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) .cra_alignmask = 15,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) .cra_flags = CRYPTO_ALG_TYPE_CIPHER |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) CRYPTO_ALG_NEED_FALLBACK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) .cra_init = fallback_init_cip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) .cra_exit = fallback_exit_cip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) .cra_blocksize = AES_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) .cra_ctxsize = sizeof(struct geode_aes_tfm_ctx),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) .cra_module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) .cra_u = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) .cipher = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) .cia_min_keysize = AES_MIN_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) .cia_max_keysize = AES_MAX_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) .cia_setkey = geode_setkey_cip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) .cia_encrypt = geode_encrypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) .cia_decrypt = geode_decrypt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) static int geode_init_skcipher(struct crypto_skcipher *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) const char *name = crypto_tfm_alg_name(&tfm->base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) struct geode_aes_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) tctx->fallback.skcipher =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) crypto_alloc_skcipher(name, 0, CRYPTO_ALG_NEED_FALLBACK |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) CRYPTO_ALG_ASYNC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) if (IS_ERR(tctx->fallback.skcipher)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) printk(KERN_ERR "Error allocating fallback algo %s\n", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) return PTR_ERR(tctx->fallback.skcipher);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) crypto_skcipher_set_reqsize(tfm, sizeof(struct skcipher_request) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) crypto_skcipher_reqsize(tctx->fallback.skcipher));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) static void geode_exit_skcipher(struct crypto_skcipher *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) struct geode_aes_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) crypto_free_skcipher(tctx->fallback.skcipher);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) static int geode_skcipher_crypt(struct skcipher_request *req, int mode, int dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) const struct geode_aes_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) struct skcipher_walk walk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) unsigned int nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) if (unlikely(tctx->keylen != AES_KEYSIZE_128)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) struct skcipher_request *subreq = skcipher_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) *subreq = *req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) skcipher_request_set_tfm(subreq, tctx->fallback.skcipher);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) if (dir == AES_DIR_DECRYPT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) return crypto_skcipher_decrypt(subreq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) return crypto_skcipher_encrypt(subreq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) err = skcipher_walk_virt(&walk, req, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) while ((nbytes = walk.nbytes) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) geode_aes_crypt(tctx, walk.src.virt.addr, walk.dst.virt.addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) round_down(nbytes, AES_BLOCK_SIZE),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) walk.iv, mode, dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) err = skcipher_walk_done(&walk, nbytes % AES_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) static int geode_cbc_encrypt(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) return geode_skcipher_crypt(req, AES_MODE_CBC, AES_DIR_ENCRYPT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) static int geode_cbc_decrypt(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) return geode_skcipher_crypt(req, AES_MODE_CBC, AES_DIR_DECRYPT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) static int geode_ecb_encrypt(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) return geode_skcipher_crypt(req, AES_MODE_ECB, AES_DIR_ENCRYPT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) static int geode_ecb_decrypt(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) return geode_skcipher_crypt(req, AES_MODE_ECB, AES_DIR_DECRYPT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) static struct skcipher_alg geode_skcipher_algs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) .base.cra_name = "cbc(aes)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) .base.cra_driver_name = "cbc-aes-geode",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) .base.cra_priority = 400,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) .base.cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) CRYPTO_ALG_NEED_FALLBACK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) .base.cra_blocksize = AES_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) .base.cra_ctxsize = sizeof(struct geode_aes_tfm_ctx),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) .base.cra_alignmask = 15,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) .base.cra_module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) .init = geode_init_skcipher,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) .exit = geode_exit_skcipher,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) .setkey = geode_setkey_skcipher,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) .encrypt = geode_cbc_encrypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) .decrypt = geode_cbc_decrypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) .min_keysize = AES_MIN_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) .max_keysize = AES_MAX_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) .ivsize = AES_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) }, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) .base.cra_name = "ecb(aes)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) .base.cra_driver_name = "ecb-aes-geode",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) .base.cra_priority = 400,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) .base.cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) CRYPTO_ALG_NEED_FALLBACK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) .base.cra_blocksize = AES_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) .base.cra_ctxsize = sizeof(struct geode_aes_tfm_ctx),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) .base.cra_alignmask = 15,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) .base.cra_module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) .init = geode_init_skcipher,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) .exit = geode_exit_skcipher,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) .setkey = geode_setkey_skcipher,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) .encrypt = geode_ecb_encrypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) .decrypt = geode_ecb_decrypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) .min_keysize = AES_MIN_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) .max_keysize = AES_MAX_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) static void geode_aes_remove(struct pci_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) crypto_unregister_alg(&geode_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) crypto_unregister_skciphers(geode_skcipher_algs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) ARRAY_SIZE(geode_skcipher_algs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) pci_iounmap(dev, _iobase);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) _iobase = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) pci_release_regions(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) pci_disable_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) static int geode_aes_probe(struct pci_dev *dev, const struct pci_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) ret = pci_enable_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) ret = pci_request_regions(dev, "geode-aes");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) goto eenable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) _iobase = pci_iomap(dev, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) if (_iobase == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) goto erequest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) spin_lock_init(&lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) /* Clear any pending activity */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) iowrite32(AES_INTR_PENDING | AES_INTR_MASK, _iobase + AES_INTR_REG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) ret = crypto_register_alg(&geode_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) goto eiomap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) ret = crypto_register_skciphers(geode_skcipher_algs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) ARRAY_SIZE(geode_skcipher_algs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) goto ealg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) dev_notice(&dev->dev, "GEODE AES engine enabled.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) ealg:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) crypto_unregister_alg(&geode_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) eiomap:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) pci_iounmap(dev, _iobase);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) erequest:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) pci_release_regions(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) eenable:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) pci_disable_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) dev_err(&dev->dev, "GEODE AES initialization failed.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) static struct pci_device_id geode_aes_tbl[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_LX_AES), },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) { 0, }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) MODULE_DEVICE_TABLE(pci, geode_aes_tbl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) static struct pci_driver geode_aes_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) .name = "Geode LX AES",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) .id_table = geode_aes_tbl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) .probe = geode_aes_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) .remove = geode_aes_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) module_pci_driver(geode_aes_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) MODULE_AUTHOR("Advanced Micro Devices, Inc.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) MODULE_DESCRIPTION("Geode LX Hardware AES driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) MODULE_IMPORT_NS(CRYPTO_INTERNAL);