Linux Audio

Check our new training course

Loading...
v5.9
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2 /* Copyright (C) 2004-2006, Advanced Micro Devices, Inc.
  3  */
 
 
 
 
 
  4
  5#include <linux/module.h>
  6#include <linux/kernel.h>
  7#include <linux/pci.h>
  8#include <linux/pci_ids.h>
  9#include <linux/crypto.h>
 10#include <linux/spinlock.h>
 11#include <crypto/algapi.h>
 12#include <crypto/aes.h>
 13#include <crypto/internal/skcipher.h>
 14
 15#include <linux/io.h>
 16#include <linux/delay.h>
 17
 18#include "geode-aes.h"
 19
 20/* Static structures */
 21
 22static void __iomem *_iobase;
 23static spinlock_t lock;
 24
 25/* Write a 128 bit field (either a writable key or IV) */
 26static inline void
 27_writefield(u32 offset, const void *value)
 28{
 29	int i;
 30
 31	for (i = 0; i < 4; i++)
 32		iowrite32(((const u32 *) value)[i], _iobase + offset + (i * 4));
 33}
 34
 35/* Read a 128 bit field (either a writable key or IV) */
 36static inline void
 37_readfield(u32 offset, void *value)
 38{
 39	int i;
 40
 41	for (i = 0; i < 4; i++)
 42		((u32 *) value)[i] = ioread32(_iobase + offset + (i * 4));
 43}
 44
 45static int
 46do_crypt(const void *src, void *dst, u32 len, u32 flags)
 47{
 48	u32 status;
 49	u32 counter = AES_OP_TIMEOUT;
 50
 51	iowrite32(virt_to_phys((void *)src), _iobase + AES_SOURCEA_REG);
 52	iowrite32(virt_to_phys(dst), _iobase + AES_DSTA_REG);
 53	iowrite32(len,  _iobase + AES_LENA_REG);
 54
 55	/* Start the operation */
 56	iowrite32(AES_CTRL_START | flags, _iobase + AES_CTRLA_REG);
 57
 58	do {
 59		status = ioread32(_iobase + AES_INTR_REG);
 60		cpu_relax();
 61	} while (!(status & AES_INTRA_PENDING) && --counter);
 62
 63	/* Clear the event */
 64	iowrite32((status & 0xFF) | AES_INTRA_PENDING, _iobase + AES_INTR_REG);
 65	return counter ? 0 : 1;
 66}
 67
 68static void
 69geode_aes_crypt(const struct geode_aes_tfm_ctx *tctx, const void *src,
 70		void *dst, u32 len, u8 *iv, int mode, int dir)
 71{
 72	u32 flags = 0;
 73	unsigned long iflags;
 74	int ret;
 75
 
 
 
 76	/* If the source and destination is the same, then
 77	 * we need to turn on the coherent flags, otherwise
 78	 * we don't need to worry
 79	 */
 80
 81	flags |= (AES_CTRL_DCA | AES_CTRL_SCA);
 82
 83	if (dir == AES_DIR_ENCRYPT)
 84		flags |= AES_CTRL_ENCRYPT;
 85
 86	/* Start the critical section */
 87
 88	spin_lock_irqsave(&lock, iflags);
 89
 90	if (mode == AES_MODE_CBC) {
 91		flags |= AES_CTRL_CBC;
 92		_writefield(AES_WRITEIV0_REG, iv);
 93	}
 94
 95	flags |= AES_CTRL_WRKEY;
 96	_writefield(AES_WRITEKEY0_REG, tctx->key);
 
 
 97
 98	ret = do_crypt(src, dst, len, flags);
 99	BUG_ON(ret);
100
101	if (mode == AES_MODE_CBC)
102		_readfield(AES_WRITEIV0_REG, iv);
103
104	spin_unlock_irqrestore(&lock, iflags);
 
 
105}
106
107/* CRYPTO-API Functions */
108
109static int geode_setkey_cip(struct crypto_tfm *tfm, const u8 *key,
110		unsigned int len)
111{
112	struct geode_aes_tfm_ctx *tctx = crypto_tfm_ctx(tfm);
 
113
114	tctx->keylen = len;
115
116	if (len == AES_KEYSIZE_128) {
117		memcpy(tctx->key, key, len);
118		return 0;
119	}
120
121	if (len != AES_KEYSIZE_192 && len != AES_KEYSIZE_256)
122		/* not supported at all */
 
123		return -EINVAL;
 
124
125	/*
126	 * The requested key size is not supported by HW, do a fallback
127	 */
128	tctx->fallback.cip->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK;
129	tctx->fallback.cip->base.crt_flags |=
130		(tfm->crt_flags & CRYPTO_TFM_REQ_MASK);
131
132	return crypto_cipher_setkey(tctx->fallback.cip, key, len);
 
 
 
 
 
133}
134
135static int geode_setkey_skcipher(struct crypto_skcipher *tfm, const u8 *key,
136				 unsigned int len)
137{
138	struct geode_aes_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
 
139
140	tctx->keylen = len;
141
142	if (len == AES_KEYSIZE_128) {
143		memcpy(tctx->key, key, len);
144		return 0;
145	}
146
147	if (len != AES_KEYSIZE_192 && len != AES_KEYSIZE_256)
148		/* not supported at all */
 
149		return -EINVAL;
 
150
151	/*
152	 * The requested key size is not supported by HW, do a fallback
153	 */
154	crypto_skcipher_clear_flags(tctx->fallback.skcipher,
155				    CRYPTO_TFM_REQ_MASK);
156	crypto_skcipher_set_flags(tctx->fallback.skcipher,
157				  crypto_skcipher_get_flags(tfm) &
158				  CRYPTO_TFM_REQ_MASK);
159	return crypto_skcipher_setkey(tctx->fallback.skcipher, key, len);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
160}
161
162static void
163geode_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
164{
165	const struct geode_aes_tfm_ctx *tctx = crypto_tfm_ctx(tfm);
166
167	if (unlikely(tctx->keylen != AES_KEYSIZE_128)) {
168		crypto_cipher_encrypt_one(tctx->fallback.cip, out, in);
169		return;
170	}
171
172	geode_aes_crypt(tctx, in, out, AES_BLOCK_SIZE, NULL,
173			AES_MODE_ECB, AES_DIR_ENCRYPT);
 
 
 
 
 
 
174}
175
176
177static void
178geode_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
179{
180	const struct geode_aes_tfm_ctx *tctx = crypto_tfm_ctx(tfm);
181
182	if (unlikely(tctx->keylen != AES_KEYSIZE_128)) {
183		crypto_cipher_decrypt_one(tctx->fallback.cip, out, in);
184		return;
185	}
186
187	geode_aes_crypt(tctx, in, out, AES_BLOCK_SIZE, NULL,
188			AES_MODE_ECB, AES_DIR_DECRYPT);
 
 
 
 
 
 
189}
190
191static int fallback_init_cip(struct crypto_tfm *tfm)
192{
193	const char *name = crypto_tfm_alg_name(tfm);
194	struct geode_aes_tfm_ctx *tctx = crypto_tfm_ctx(tfm);
195
196	tctx->fallback.cip = crypto_alloc_cipher(name, 0,
197						 CRYPTO_ALG_NEED_FALLBACK);
198
199	if (IS_ERR(tctx->fallback.cip)) {
200		printk(KERN_ERR "Error allocating fallback algo %s\n", name);
201		return PTR_ERR(tctx->fallback.cip);
202	}
203
204	return 0;
205}
206
207static void fallback_exit_cip(struct crypto_tfm *tfm)
208{
209	struct geode_aes_tfm_ctx *tctx = crypto_tfm_ctx(tfm);
210
211	crypto_free_cipher(tctx->fallback.cip);
 
212}
213
214static struct crypto_alg geode_alg = {
215	.cra_name			=	"aes",
216	.cra_driver_name	=	"geode-aes",
217	.cra_priority		=	300,
218	.cra_alignmask		=	15,
219	.cra_flags			=	CRYPTO_ALG_TYPE_CIPHER |
220							CRYPTO_ALG_NEED_FALLBACK,
221	.cra_init			=	fallback_init_cip,
222	.cra_exit			=	fallback_exit_cip,
223	.cra_blocksize		=	AES_BLOCK_SIZE,
224	.cra_ctxsize		=	sizeof(struct geode_aes_tfm_ctx),
225	.cra_module			=	THIS_MODULE,
 
226	.cra_u				=	{
227		.cipher	=	{
228			.cia_min_keysize	=	AES_MIN_KEY_SIZE,
229			.cia_max_keysize	=	AES_MAX_KEY_SIZE,
230			.cia_setkey			=	geode_setkey_cip,
231			.cia_encrypt		=	geode_encrypt,
232			.cia_decrypt		=	geode_decrypt
233		}
234	}
235};
236
237static int geode_init_skcipher(struct crypto_skcipher *tfm)
238{
239	const char *name = crypto_tfm_alg_name(&tfm->base);
240	struct geode_aes_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
241
242	tctx->fallback.skcipher =
243		crypto_alloc_skcipher(name, 0, CRYPTO_ALG_NEED_FALLBACK |
244				      CRYPTO_ALG_ASYNC);
245	if (IS_ERR(tctx->fallback.skcipher)) {
246		printk(KERN_ERR "Error allocating fallback algo %s\n", name);
247		return PTR_ERR(tctx->fallback.skcipher);
248	}
249
250	crypto_skcipher_set_reqsize(tfm, sizeof(struct skcipher_request) +
251				    crypto_skcipher_reqsize(tctx->fallback.skcipher));
252	return 0;
253}
254
255static void geode_exit_skcipher(struct crypto_skcipher *tfm)
256{
257	struct geode_aes_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
258
259	crypto_free_skcipher(tctx->fallback.skcipher);
260}
261
262static int geode_skcipher_crypt(struct skcipher_request *req, int mode, int dir)
263{
264	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
265	const struct geode_aes_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
266	struct skcipher_walk walk;
267	unsigned int nbytes;
268	int err;
269
270	if (unlikely(tctx->keylen != AES_KEYSIZE_128)) {
271		struct skcipher_request *subreq = skcipher_request_ctx(req);
272
273		*subreq = *req;
274		skcipher_request_set_tfm(subreq, tctx->fallback.skcipher);
275		if (dir == AES_DIR_DECRYPT)
276			return crypto_skcipher_decrypt(subreq);
277		else
278			return crypto_skcipher_encrypt(subreq);
279	}
280
281	err = skcipher_walk_virt(&walk, req, false);
 
 
 
 
 
282
283	while ((nbytes = walk.nbytes) != 0) {
284		geode_aes_crypt(tctx, walk.src.virt.addr, walk.dst.virt.addr,
285				round_down(nbytes, AES_BLOCK_SIZE),
286				walk.iv, mode, dir);
287		err = skcipher_walk_done(&walk, nbytes % AES_BLOCK_SIZE);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
288	}
289
290	return err;
291}
292
293static int geode_cbc_encrypt(struct skcipher_request *req)
294{
295	return geode_skcipher_crypt(req, AES_MODE_CBC, AES_DIR_ENCRYPT);
296}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
297
298static int geode_cbc_decrypt(struct skcipher_request *req)
299{
300	return geode_skcipher_crypt(req, AES_MODE_CBC, AES_DIR_DECRYPT);
301}
302
303static int geode_ecb_encrypt(struct skcipher_request *req)
304{
305	return geode_skcipher_crypt(req, AES_MODE_ECB, AES_DIR_ENCRYPT);
306}
307
308static int geode_ecb_decrypt(struct skcipher_request *req)
309{
310	return geode_skcipher_crypt(req, AES_MODE_ECB, AES_DIR_DECRYPT);
311}
312
313static struct skcipher_alg geode_skcipher_algs[] = {
314	{
315		.base.cra_name		= "cbc(aes)",
316		.base.cra_driver_name	= "cbc-aes-geode",
317		.base.cra_priority	= 400,
318		.base.cra_flags		= CRYPTO_ALG_KERN_DRIVER_ONLY |
319					  CRYPTO_ALG_NEED_FALLBACK,
320		.base.cra_blocksize	= AES_BLOCK_SIZE,
321		.base.cra_ctxsize	= sizeof(struct geode_aes_tfm_ctx),
322		.base.cra_alignmask	= 15,
323		.base.cra_module	= THIS_MODULE,
324		.init			= geode_init_skcipher,
325		.exit			= geode_exit_skcipher,
326		.setkey			= geode_setkey_skcipher,
327		.encrypt		= geode_cbc_encrypt,
328		.decrypt		= geode_cbc_decrypt,
329		.min_keysize		= AES_MIN_KEY_SIZE,
330		.max_keysize		= AES_MAX_KEY_SIZE,
331		.ivsize			= AES_BLOCK_SIZE,
332	}, {
333		.base.cra_name		= "ecb(aes)",
334		.base.cra_driver_name	= "ecb-aes-geode",
335		.base.cra_priority	= 400,
336		.base.cra_flags		= CRYPTO_ALG_KERN_DRIVER_ONLY |
337					  CRYPTO_ALG_NEED_FALLBACK,
338		.base.cra_blocksize	= AES_BLOCK_SIZE,
339		.base.cra_ctxsize	= sizeof(struct geode_aes_tfm_ctx),
340		.base.cra_alignmask	= 15,
341		.base.cra_module	= THIS_MODULE,
342		.init			= geode_init_skcipher,
343		.exit			= geode_exit_skcipher,
344		.setkey			= geode_setkey_skcipher,
345		.encrypt		= geode_ecb_encrypt,
346		.decrypt		= geode_ecb_decrypt,
347		.min_keysize		= AES_MIN_KEY_SIZE,
348		.max_keysize		= AES_MAX_KEY_SIZE,
349	},
350};
351
352static void geode_aes_remove(struct pci_dev *dev)
 
353{
354	crypto_unregister_alg(&geode_alg);
355	crypto_unregister_skciphers(geode_skcipher_algs,
356				    ARRAY_SIZE(geode_skcipher_algs));
357
358	pci_iounmap(dev, _iobase);
359	_iobase = NULL;
360
361	pci_release_regions(dev);
362	pci_disable_device(dev);
363}
364
365
366static int geode_aes_probe(struct pci_dev *dev, const struct pci_device_id *id)
 
367{
368	int ret;
369
370	ret = pci_enable_device(dev);
371	if (ret)
372		return ret;
373
374	ret = pci_request_regions(dev, "geode-aes");
375	if (ret)
376		goto eenable;
377
378	_iobase = pci_iomap(dev, 0, 0);
379
380	if (_iobase == NULL) {
381		ret = -ENOMEM;
382		goto erequest;
383	}
384
385	spin_lock_init(&lock);
386
387	/* Clear any pending activity */
388	iowrite32(AES_INTR_PENDING | AES_INTR_MASK, _iobase + AES_INTR_REG);
389
390	ret = crypto_register_alg(&geode_alg);
391	if (ret)
392		goto eiomap;
393
394	ret = crypto_register_skciphers(geode_skcipher_algs,
395					ARRAY_SIZE(geode_skcipher_algs));
396	if (ret)
397		goto ealg;
398
399	dev_notice(&dev->dev, "GEODE AES engine enabled.\n");
 
 
 
 
400	return 0;
401
 
 
 
402 ealg:
403	crypto_unregister_alg(&geode_alg);
404
405 eiomap:
406	pci_iounmap(dev, _iobase);
407
408 erequest:
409	pci_release_regions(dev);
410
411 eenable:
412	pci_disable_device(dev);
413
414	dev_err(&dev->dev, "GEODE AES initialization failed.\n");
415	return ret;
416}
417
418static struct pci_device_id geode_aes_tbl[] = {
419	{ PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_LX_AES), },
420	{ 0, }
421};
422
423MODULE_DEVICE_TABLE(pci, geode_aes_tbl);
424
425static struct pci_driver geode_aes_driver = {
426	.name = "Geode LX AES",
427	.id_table = geode_aes_tbl,
428	.probe = geode_aes_probe,
429	.remove = geode_aes_remove,
430};
431
432module_pci_driver(geode_aes_driver);
 
 
 
 
 
 
 
 
 
 
433
434MODULE_AUTHOR("Advanced Micro Devices, Inc.");
435MODULE_DESCRIPTION("Geode LX Hardware AES driver");
436MODULE_LICENSE("GPL");
v3.5.6
 
  1 /* Copyright (C) 2004-2006, Advanced Micro Devices, Inc.
  2 *
  3 * This program is free software; you can redistribute it and/or modify
  4 * it under the terms of the GNU General Public License as published by
  5 * the Free Software Foundation; either version 2 of the License, or
  6 * (at your option) any later version.
  7 */
  8
  9#include <linux/module.h>
 10#include <linux/kernel.h>
 11#include <linux/pci.h>
 12#include <linux/pci_ids.h>
 13#include <linux/crypto.h>
 14#include <linux/spinlock.h>
 15#include <crypto/algapi.h>
 16#include <crypto/aes.h>
 
 17
 18#include <linux/io.h>
 19#include <linux/delay.h>
 20
 21#include "geode-aes.h"
 22
 23/* Static structures */
 24
 25static void __iomem *_iobase;
 26static spinlock_t lock;
 27
 28/* Write a 128 bit field (either a writable key or IV) */
 29static inline void
 30_writefield(u32 offset, void *value)
 31{
 32	int i;
 
 33	for (i = 0; i < 4; i++)
 34		iowrite32(((u32 *) value)[i], _iobase + offset + (i * 4));
 35}
 36
 37/* Read a 128 bit field (either a writable key or IV) */
 38static inline void
 39_readfield(u32 offset, void *value)
 40{
 41	int i;
 
 42	for (i = 0; i < 4; i++)
 43		((u32 *) value)[i] = ioread32(_iobase + offset + (i * 4));
 44}
 45
 46static int
 47do_crypt(void *src, void *dst, int len, u32 flags)
 48{
 49	u32 status;
 50	u32 counter = AES_OP_TIMEOUT;
 51
 52	iowrite32(virt_to_phys(src), _iobase + AES_SOURCEA_REG);
 53	iowrite32(virt_to_phys(dst), _iobase + AES_DSTA_REG);
 54	iowrite32(len,  _iobase + AES_LENA_REG);
 55
 56	/* Start the operation */
 57	iowrite32(AES_CTRL_START | flags, _iobase + AES_CTRLA_REG);
 58
 59	do {
 60		status = ioread32(_iobase + AES_INTR_REG);
 61		cpu_relax();
 62	} while (!(status & AES_INTRA_PENDING) && --counter);
 63
 64	/* Clear the event */
 65	iowrite32((status & 0xFF) | AES_INTRA_PENDING, _iobase + AES_INTR_REG);
 66	return counter ? 0 : 1;
 67}
 68
 69static unsigned int
 70geode_aes_crypt(struct geode_aes_op *op)
 
 71{
 72	u32 flags = 0;
 73	unsigned long iflags;
 74	int ret;
 75
 76	if (op->len == 0)
 77		return 0;
 78
 79	/* If the source and destination is the same, then
 80	 * we need to turn on the coherent flags, otherwise
 81	 * we don't need to worry
 82	 */
 83
 84	flags |= (AES_CTRL_DCA | AES_CTRL_SCA);
 85
 86	if (op->dir == AES_DIR_ENCRYPT)
 87		flags |= AES_CTRL_ENCRYPT;
 88
 89	/* Start the critical section */
 90
 91	spin_lock_irqsave(&lock, iflags);
 92
 93	if (op->mode == AES_MODE_CBC) {
 94		flags |= AES_CTRL_CBC;
 95		_writefield(AES_WRITEIV0_REG, op->iv);
 96	}
 97
 98	if (!(op->flags & AES_FLAGS_HIDDENKEY)) {
 99		flags |= AES_CTRL_WRKEY;
100		_writefield(AES_WRITEKEY0_REG, op->key);
101	}
102
103	ret = do_crypt(op->src, op->dst, op->len, flags);
104	BUG_ON(ret);
105
106	if (op->mode == AES_MODE_CBC)
107		_readfield(AES_WRITEIV0_REG, op->iv);
108
109	spin_unlock_irqrestore(&lock, iflags);
110
111	return op->len;
112}
113
114/* CRYPTO-API Functions */
115
116static int geode_setkey_cip(struct crypto_tfm *tfm, const u8 *key,
117		unsigned int len)
118{
119	struct geode_aes_op *op = crypto_tfm_ctx(tfm);
120	unsigned int ret;
121
122	op->keylen = len;
123
124	if (len == AES_KEYSIZE_128) {
125		memcpy(op->key, key, len);
126		return 0;
127	}
128
129	if (len != AES_KEYSIZE_192 && len != AES_KEYSIZE_256) {
130		/* not supported at all */
131		tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
132		return -EINVAL;
133	}
134
135	/*
136	 * The requested key size is not supported by HW, do a fallback
137	 */
138	op->fallback.cip->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK;
139	op->fallback.cip->base.crt_flags |= (tfm->crt_flags & CRYPTO_TFM_REQ_MASK);
 
140
141	ret = crypto_cipher_setkey(op->fallback.cip, key, len);
142	if (ret) {
143		tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
144		tfm->crt_flags |= (op->fallback.cip->base.crt_flags & CRYPTO_TFM_RES_MASK);
145	}
146	return ret;
147}
148
149static int geode_setkey_blk(struct crypto_tfm *tfm, const u8 *key,
150		unsigned int len)
151{
152	struct geode_aes_op *op = crypto_tfm_ctx(tfm);
153	unsigned int ret;
154
155	op->keylen = len;
156
157	if (len == AES_KEYSIZE_128) {
158		memcpy(op->key, key, len);
159		return 0;
160	}
161
162	if (len != AES_KEYSIZE_192 && len != AES_KEYSIZE_256) {
163		/* not supported at all */
164		tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
165		return -EINVAL;
166	}
167
168	/*
169	 * The requested key size is not supported by HW, do a fallback
170	 */
171	op->fallback.blk->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK;
172	op->fallback.blk->base.crt_flags |= (tfm->crt_flags & CRYPTO_TFM_REQ_MASK);
173
174	ret = crypto_blkcipher_setkey(op->fallback.blk, key, len);
175	if (ret) {
176		tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
177		tfm->crt_flags |= (op->fallback.blk->base.crt_flags & CRYPTO_TFM_RES_MASK);
178	}
179	return ret;
180}
181
182static int fallback_blk_dec(struct blkcipher_desc *desc,
183		struct scatterlist *dst, struct scatterlist *src,
184		unsigned int nbytes)
185{
186	unsigned int ret;
187	struct crypto_blkcipher *tfm;
188	struct geode_aes_op *op = crypto_blkcipher_ctx(desc->tfm);
189
190	tfm = desc->tfm;
191	desc->tfm = op->fallback.blk;
192
193	ret = crypto_blkcipher_decrypt_iv(desc, dst, src, nbytes);
194
195	desc->tfm = tfm;
196	return ret;
197}
198static int fallback_blk_enc(struct blkcipher_desc *desc,
199		struct scatterlist *dst, struct scatterlist *src,
200		unsigned int nbytes)
201{
202	unsigned int ret;
203	struct crypto_blkcipher *tfm;
204	struct geode_aes_op *op = crypto_blkcipher_ctx(desc->tfm);
205
206	tfm = desc->tfm;
207	desc->tfm = op->fallback.blk;
208
209	ret = crypto_blkcipher_encrypt_iv(desc, dst, src, nbytes);
210
211	desc->tfm = tfm;
212	return ret;
213}
214
215static void
216geode_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
217{
218	struct geode_aes_op *op = crypto_tfm_ctx(tfm);
219
220	if (unlikely(op->keylen != AES_KEYSIZE_128)) {
221		crypto_cipher_encrypt_one(op->fallback.cip, out, in);
222		return;
223	}
224
225	op->src = (void *) in;
226	op->dst = (void *) out;
227	op->mode = AES_MODE_ECB;
228	op->flags = 0;
229	op->len = AES_MIN_BLOCK_SIZE;
230	op->dir = AES_DIR_ENCRYPT;
231
232	geode_aes_crypt(op);
233}
234
235
236static void
237geode_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
238{
239	struct geode_aes_op *op = crypto_tfm_ctx(tfm);
240
241	if (unlikely(op->keylen != AES_KEYSIZE_128)) {
242		crypto_cipher_decrypt_one(op->fallback.cip, out, in);
243		return;
244	}
245
246	op->src = (void *) in;
247	op->dst = (void *) out;
248	op->mode = AES_MODE_ECB;
249	op->flags = 0;
250	op->len = AES_MIN_BLOCK_SIZE;
251	op->dir = AES_DIR_DECRYPT;
252
253	geode_aes_crypt(op);
254}
255
256static int fallback_init_cip(struct crypto_tfm *tfm)
257{
258	const char *name = tfm->__crt_alg->cra_name;
259	struct geode_aes_op *op = crypto_tfm_ctx(tfm);
260
261	op->fallback.cip = crypto_alloc_cipher(name, 0,
262				CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK);
263
264	if (IS_ERR(op->fallback.cip)) {
265		printk(KERN_ERR "Error allocating fallback algo %s\n", name);
266		return PTR_ERR(op->fallback.cip);
267	}
268
269	return 0;
270}
271
272static void fallback_exit_cip(struct crypto_tfm *tfm)
273{
274	struct geode_aes_op *op = crypto_tfm_ctx(tfm);
275
276	crypto_free_cipher(op->fallback.cip);
277	op->fallback.cip = NULL;
278}
279
280static struct crypto_alg geode_alg = {
281	.cra_name			=	"aes",
282	.cra_driver_name	=	"geode-aes",
283	.cra_priority		=	300,
284	.cra_alignmask		=	15,
285	.cra_flags			=	CRYPTO_ALG_TYPE_CIPHER |
286							CRYPTO_ALG_NEED_FALLBACK,
287	.cra_init			=	fallback_init_cip,
288	.cra_exit			=	fallback_exit_cip,
289	.cra_blocksize		=	AES_MIN_BLOCK_SIZE,
290	.cra_ctxsize		=	sizeof(struct geode_aes_op),
291	.cra_module			=	THIS_MODULE,
292	.cra_list			=	LIST_HEAD_INIT(geode_alg.cra_list),
293	.cra_u				=	{
294		.cipher	=	{
295			.cia_min_keysize	=	AES_MIN_KEY_SIZE,
296			.cia_max_keysize	=	AES_MAX_KEY_SIZE,
297			.cia_setkey			=	geode_setkey_cip,
298			.cia_encrypt		=	geode_encrypt,
299			.cia_decrypt		=	geode_decrypt
300		}
301	}
302};
303
304static int
305geode_cbc_decrypt(struct blkcipher_desc *desc,
306		  struct scatterlist *dst, struct scatterlist *src,
307		  unsigned int nbytes)
308{
309	struct geode_aes_op *op = crypto_blkcipher_ctx(desc->tfm);
310	struct blkcipher_walk walk;
311	int err, ret;
312
313	if (unlikely(op->keylen != AES_KEYSIZE_128))
314		return fallback_blk_dec(desc, dst, src, nbytes);
315
316	blkcipher_walk_init(&walk, dst, src, nbytes);
317	err = blkcipher_walk_virt(desc, &walk);
318	op->iv = walk.iv;
319
320	while ((nbytes = walk.nbytes)) {
321		op->src = walk.src.virt.addr,
322		op->dst = walk.dst.virt.addr;
323		op->mode = AES_MODE_CBC;
324		op->len = nbytes - (nbytes % AES_MIN_BLOCK_SIZE);
325		op->dir = AES_DIR_DECRYPT;
326
327		ret = geode_aes_crypt(op);
328
329		nbytes -= ret;
330		err = blkcipher_walk_done(desc, &walk, nbytes);
 
 
331	}
332
333	return err;
 
 
334}
335
336static int
337geode_cbc_encrypt(struct blkcipher_desc *desc,
338		  struct scatterlist *dst, struct scatterlist *src,
339		  unsigned int nbytes)
340{
341	struct geode_aes_op *op = crypto_blkcipher_ctx(desc->tfm);
342	struct blkcipher_walk walk;
343	int err, ret;
344
345	if (unlikely(op->keylen != AES_KEYSIZE_128))
346		return fallback_blk_enc(desc, dst, src, nbytes);
347
348	blkcipher_walk_init(&walk, dst, src, nbytes);
349	err = blkcipher_walk_virt(desc, &walk);
350	op->iv = walk.iv;
351
352	while ((nbytes = walk.nbytes)) {
353		op->src = walk.src.virt.addr,
354		op->dst = walk.dst.virt.addr;
355		op->mode = AES_MODE_CBC;
356		op->len = nbytes - (nbytes % AES_MIN_BLOCK_SIZE);
357		op->dir = AES_DIR_ENCRYPT;
358
359		ret = geode_aes_crypt(op);
360		nbytes -= ret;
361		err = blkcipher_walk_done(desc, &walk, nbytes);
362	}
363
364	return err;
365}
366
367static int fallback_init_blk(struct crypto_tfm *tfm)
368{
369	const char *name = tfm->__crt_alg->cra_name;
370	struct geode_aes_op *op = crypto_tfm_ctx(tfm);
 
 
 
371
372	op->fallback.blk = crypto_alloc_blkcipher(name, 0,
373			CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK);
374
375	if (IS_ERR(op->fallback.blk)) {
376		printk(KERN_ERR "Error allocating fallback algo %s\n", name);
377		return PTR_ERR(op->fallback.blk);
 
 
 
378	}
379
380	return 0;
381}
382
383static void fallback_exit_blk(struct crypto_tfm *tfm)
384{
385	struct geode_aes_op *op = crypto_tfm_ctx(tfm);
386
387	crypto_free_blkcipher(op->fallback.blk);
388	op->fallback.blk = NULL;
389}
390
391static struct crypto_alg geode_cbc_alg = {
392	.cra_name		=	"cbc(aes)",
393	.cra_driver_name	=	"cbc-aes-geode",
394	.cra_priority		=	400,
395	.cra_flags			=	CRYPTO_ALG_TYPE_BLKCIPHER |
396						CRYPTO_ALG_KERN_DRIVER_ONLY |
397						CRYPTO_ALG_NEED_FALLBACK,
398	.cra_init			=	fallback_init_blk,
399	.cra_exit			=	fallback_exit_blk,
400	.cra_blocksize		=	AES_MIN_BLOCK_SIZE,
401	.cra_ctxsize		=	sizeof(struct geode_aes_op),
402	.cra_alignmask		=	15,
403	.cra_type			=	&crypto_blkcipher_type,
404	.cra_module			=	THIS_MODULE,
405	.cra_list			=	LIST_HEAD_INIT(geode_cbc_alg.cra_list),
406	.cra_u				=	{
407		.blkcipher	=	{
408			.min_keysize	=	AES_MIN_KEY_SIZE,
409			.max_keysize	=	AES_MAX_KEY_SIZE,
410			.setkey			=	geode_setkey_blk,
411			.encrypt		=	geode_cbc_encrypt,
412			.decrypt		=	geode_cbc_decrypt,
413			.ivsize			=	AES_IV_LENGTH,
414		}
415	}
416};
417
418static int
419geode_ecb_decrypt(struct blkcipher_desc *desc,
420		  struct scatterlist *dst, struct scatterlist *src,
421		  unsigned int nbytes)
422{
423	struct geode_aes_op *op = crypto_blkcipher_ctx(desc->tfm);
424	struct blkcipher_walk walk;
425	int err, ret;
426
427	if (unlikely(op->keylen != AES_KEYSIZE_128))
428		return fallback_blk_dec(desc, dst, src, nbytes);
429
430	blkcipher_walk_init(&walk, dst, src, nbytes);
431	err = blkcipher_walk_virt(desc, &walk);
432
433	while ((nbytes = walk.nbytes)) {
434		op->src = walk.src.virt.addr,
435		op->dst = walk.dst.virt.addr;
436		op->mode = AES_MODE_ECB;
437		op->len = nbytes - (nbytes % AES_MIN_BLOCK_SIZE);
438		op->dir = AES_DIR_DECRYPT;
439
440		ret = geode_aes_crypt(op);
441		nbytes -= ret;
442		err = blkcipher_walk_done(desc, &walk, nbytes);
443	}
444
445	return err;
446}
447
448static int
449geode_ecb_encrypt(struct blkcipher_desc *desc,
450		  struct scatterlist *dst, struct scatterlist *src,
451		  unsigned int nbytes)
452{
453	struct geode_aes_op *op = crypto_blkcipher_ctx(desc->tfm);
454	struct blkcipher_walk walk;
455	int err, ret;
456
457	if (unlikely(op->keylen != AES_KEYSIZE_128))
458		return fallback_blk_enc(desc, dst, src, nbytes);
459
460	blkcipher_walk_init(&walk, dst, src, nbytes);
461	err = blkcipher_walk_virt(desc, &walk);
462
463	while ((nbytes = walk.nbytes)) {
464		op->src = walk.src.virt.addr,
465		op->dst = walk.dst.virt.addr;
466		op->mode = AES_MODE_ECB;
467		op->len = nbytes - (nbytes % AES_MIN_BLOCK_SIZE);
468		op->dir = AES_DIR_ENCRYPT;
469
470		ret = geode_aes_crypt(op);
471		nbytes -= ret;
472		ret =  blkcipher_walk_done(desc, &walk, nbytes);
473	}
474
475	return err;
 
 
476}
477
478static struct crypto_alg geode_ecb_alg = {
479	.cra_name			=	"ecb(aes)",
480	.cra_driver_name	=	"ecb-aes-geode",
481	.cra_priority		=	400,
482	.cra_flags			=	CRYPTO_ALG_TYPE_BLKCIPHER |
483						CRYPTO_ALG_KERN_DRIVER_ONLY |
484						CRYPTO_ALG_NEED_FALLBACK,
485	.cra_init			=	fallback_init_blk,
486	.cra_exit			=	fallback_exit_blk,
487	.cra_blocksize		=	AES_MIN_BLOCK_SIZE,
488	.cra_ctxsize		=	sizeof(struct geode_aes_op),
489	.cra_alignmask		=	15,
490	.cra_type			=	&crypto_blkcipher_type,
491	.cra_module			=	THIS_MODULE,
492	.cra_list			=	LIST_HEAD_INIT(geode_ecb_alg.cra_list),
493	.cra_u				=	{
494		.blkcipher	=	{
495			.min_keysize	=	AES_MIN_KEY_SIZE,
496			.max_keysize	=	AES_MAX_KEY_SIZE,
497			.setkey			=	geode_setkey_blk,
498			.encrypt		=	geode_ecb_encrypt,
499			.decrypt		=	geode_ecb_decrypt,
500		}
501	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
502};
503
504static void __devexit
505geode_aes_remove(struct pci_dev *dev)
506{
507	crypto_unregister_alg(&geode_alg);
508	crypto_unregister_alg(&geode_ecb_alg);
509	crypto_unregister_alg(&geode_cbc_alg);
510
511	pci_iounmap(dev, _iobase);
512	_iobase = NULL;
513
514	pci_release_regions(dev);
515	pci_disable_device(dev);
516}
517
518
519static int __devinit
520geode_aes_probe(struct pci_dev *dev, const struct pci_device_id *id)
521{
522	int ret;
 
523	ret = pci_enable_device(dev);
524	if (ret)
525		return ret;
526
527	ret = pci_request_regions(dev, "geode-aes");
528	if (ret)
529		goto eenable;
530
531	_iobase = pci_iomap(dev, 0, 0);
532
533	if (_iobase == NULL) {
534		ret = -ENOMEM;
535		goto erequest;
536	}
537
538	spin_lock_init(&lock);
539
540	/* Clear any pending activity */
541	iowrite32(AES_INTR_PENDING | AES_INTR_MASK, _iobase + AES_INTR_REG);
542
543	ret = crypto_register_alg(&geode_alg);
544	if (ret)
545		goto eiomap;
546
547	ret = crypto_register_alg(&geode_ecb_alg);
 
548	if (ret)
549		goto ealg;
550
551	ret = crypto_register_alg(&geode_cbc_alg);
552	if (ret)
553		goto eecb;
554
555	printk(KERN_NOTICE "geode-aes: GEODE AES engine enabled.\n");
556	return 0;
557
558 eecb:
559	crypto_unregister_alg(&geode_ecb_alg);
560
561 ealg:
562	crypto_unregister_alg(&geode_alg);
563
564 eiomap:
565	pci_iounmap(dev, _iobase);
566
567 erequest:
568	pci_release_regions(dev);
569
570 eenable:
571	pci_disable_device(dev);
572
573	printk(KERN_ERR "geode-aes:  GEODE AES initialization failed.\n");
574	return ret;
575}
576
577static struct pci_device_id geode_aes_tbl[] = {
578	{ PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_LX_AES), } ,
579	{ 0, }
580};
581
582MODULE_DEVICE_TABLE(pci, geode_aes_tbl);
583
584static struct pci_driver geode_aes_driver = {
585	.name = "Geode LX AES",
586	.id_table = geode_aes_tbl,
587	.probe = geode_aes_probe,
588	.remove = __devexit_p(geode_aes_remove)
589};
590
591static int __init
592geode_aes_init(void)
593{
594	return pci_register_driver(&geode_aes_driver);
595}
596
597static void __exit
598geode_aes_exit(void)
599{
600	pci_unregister_driver(&geode_aes_driver);
601}
602
603MODULE_AUTHOR("Advanced Micro Devices, Inc.");
604MODULE_DESCRIPTION("Geode LX Hardware AES driver");
605MODULE_LICENSE("GPL");
606
607module_init(geode_aes_init);
608module_exit(geode_aes_exit);