Skip to contentSkip to conversation

HIFN: preliminary HIFN 795x driver for new async cryptoapi.

PATCH In review Evgeniy Polyakov 3 participants 14 messages Jun 4, 2007 – Jun 22, 2007 Crypto API

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 HIFN: preliminary HIFN 795x driver for new async cryptoapi.

Hi.

This is preliminary driver for HIFN 795x crypto accelerator chip.

It is slightly restructured acrypto driver, which worked correctly, but this one was not tested with real hardware (yet, I will do it as soon as get back my soekris adapter).

Changelog:

take2:
* added a lot of cryptoapi interfaces (i.e. support for all crypto modes and three ciphers: aes, des and 3des via cryptoapi).

My cat has pissed on my domestic slippers when he saw that. And believe me, he does know, how to distinguish between good and other code. Well, I do not have a cat, but if I would, he would definitely piss on my slippers. The most interesting stuff starts from 1600+ lines. Herbert, are you sure it is ok?

drivers/crypto/Kconfig +12 -0
index ff8c4be..9d2fffd 100644
@@ -66,4 +66,16 @@ config CRYPTO_DEV_GEODE
 	  To compile this driver as a module, choose M here: the module
 	  will be called geode-aes.
 
+config CRYPTO_DEV_HIFN_795x
+	tristate "Support for HIFN 795x crypto processors"
+	depends on CRYPTO && PCI
+	select CRYPTO_ALGAPI
+	select CRYPTO_BLKCIPHER
+	help
+	  Say 'Y' here to enable support for HIFN 795x crypto processor for
+	  various crypto algorithms this processor supports.
+	  
+	  To compile this driver as a module, choose M here: the module
+	  will be called hifn795x.
+
 endmenu
drivers/crypto/Makefile +1 -0
index 6059cf8..f8c1af8 100644
@@ -2,3 +2,4 @@ obj-$(CONFIG_CRYPTO_DEV_PADLOCK) += padlock.o
 obj-$(CONFIG_CRYPTO_DEV_PADLOCK_AES) += padlock-aes.o
 obj-$(CONFIG_CRYPTO_DEV_PADLOCK_SHA) += padlock-sha.o
 obj-$(CONFIG_CRYPTO_DEV_GEODE) += geode-aes.o
+obj-$(CRYPTO_DEV_HIFN_795x) += hifn_795x.o
drivers/crypto/hifn_795x.c new file +2289 -0

2,289 lines not on this page · raw message

Signature · 1 line
	Evgeniy Polyakov
#1 lore raw

drivers/crypto/hifn_795x.c line 439

Quoted 8 lines · * Evgeniy Polyakov | 2007-06-04 17:42:48 [+0400]:
drivers/crypto/hifn_795x.c new file +8 -0
+struct hifn_device
+{
+	struct pci_dev		*pdev;
+ 	spinlock_t              lock;
+	u8			current_key[HIFN_MAX_CRYPT_KEY_LENGTH];
+	int			current_key_len;
+	struct list_head	alg_list;
+};

...
This struct looks to me like one per real hardware. What are you doing if you get two+ crypto users? Or is this struct one per crypto user? This is what gets allocated by crypto api for every user.

Quoted 4 lines
drivers/crypto/hifn_795x.c new file +4 -0
+static void hifn_work(struct work_struct *);
+
+static int hifn_start_device(struct hifn_device *dev)
+{

...

Quoted 5 lines
drivers/crypto/hifn_795x.c new file +5 -0
+	INIT_DELAYED_WORK(&dev->work, hifn_work);
+	schedule_delayed_work(&dev->work, HZ);
+	
+	return 0;
+}

...

Quoted 1 line

+static irqreturn_t hifn_interrupt(int irq, void *data)

...
This looks to me like you have to reset the hardware once in a while. The worker func and the interrupt handler are the only two functions (exept hifn_remove()) that know about your hardware at run time.

Quoted 10 lines
drivers/crypto/hifn_795x.c new file +10 -0
+static int hifn_setkey(struct crypto_ablkcipher *cipher, const u8 *key, unsigned int len)
+{
+	struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
+	struct hifn_device *dev = crypto_tfm_ctx(tfm);
+
+	if (len > HIFN_MAX_CRYPT_KEY_LENGTH)
+		return -1;
+
+	if (!memcmp(dev->current_key, key, len)) {
+		dev->flags |= HIFN_FLAG_OLD_KEY;

drivers/crypto/hifn_795x.c line 1607

Quoted 10 lines
drivers/crypto/hifn_795x.c new file +10 -0
+		return 0;
+	}
+
+	dev->flags &= ~HIFN_FLAG_OLD_KEY;
+
+	memcpy(dev->current_key, key, len);
+	dev->current_key_len = len;
+
+	return 0;
+}

...
dev is allocated by the crypto API but isn't initialized right? Nothing points to your real hw right?
I (in my case) assign directly key ctx to hw ctx in set_key. This is crap because more than just one hw could exist. The API chooses the algo with the highest prio so you can't use more than one device. A load balancer could be the person in charge for assigning hw ctx to crypto user ctx.

drivers/crypto/hifn_795x.c line 1641

Quoted 4 lines
drivers/crypto/hifn_795x.c new file +4 -0
+static inline int hifn_encrypt_aes_ecb_16(struct ablkcipher_request *req)
+{
+	return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT, ACRYPTO_TYPE_AES_128, ACRYPTO_MODE_ECB);
+}

...
This is what I had in mind, as I said look on my skeleton, than you will see how you can distinguish which algo is requested. Since you have 12 algos I understand now what you meant with "it doesn't" and your cat :)

Quoted 1 line

Evgeniy Polyakov

Sebastian

#2 lore raw

Hi Sebastian.

Quoted 14 lines · Sebastian Siewior

drivers/crypto/hifn_795x.c line 439

Quoted 8 lines · * Evgeniy Polyakov | 2007-06-04 17:42:48 [+0400]:
drivers/crypto/hifn_795x.c new file +8 -0
+struct hifn_device
+{
+	struct pci_dev		*pdev;
+ 	spinlock_t              lock;
+	u8			current_key[HIFN_MAX_CRYPT_KEY_LENGTH];
+	int			current_key_len;
+	struct list_head	alg_list;
+};

...
This struct looks to me like one per real hardware. What are you doing if you get two+ crypto users? Or is this struct one per crypto user? This is what gets allocated by crypto api for every user.

This is tricky, hifn requires to have a key per crypto setup, so this one must be in a crypto context, but context is not used right now, so yes, it supports only one user. I will update it to use context, since it is smaller that hifn_device.

Quoted 11 lines
Quoted 4 lines
drivers/crypto/hifn_795x.c new file +4 -0
+static void hifn_work(struct work_struct *);
+
+static int hifn_start_device(struct hifn_device *dev)
+{

...

Quoted 5 lines
drivers/crypto/hifn_795x.c new file +5 -0
+	INIT_DELAYED_WORK(&dev->work, hifn_work);
+	schedule_delayed_work(&dev->work, HZ);
+	
+	return 0;
+}

...

Quoted 5 lines
Quoted 1 line

+static irqreturn_t hifn_interrupt(int irq, void *data)

...
This looks to me like you have to reset the hardware once in a while. The worker func and the interrupt handler are the only two functions (exept hifn_remove()) that know about your hardware at run time.

It is a watchdog worker, it checks if there were sessions setup, and if they were not ready when watchdog fired, it resets hardware and completes appropriate requests with error.

Quoted 20 lines
Quoted 10 lines
drivers/crypto/hifn_795x.c new file +10 -0
+static int hifn_setkey(struct crypto_ablkcipher *cipher, const u8 *key, unsigned int len)
+{
+	struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
+	struct hifn_device *dev = crypto_tfm_ctx(tfm);
+
+	if (len > HIFN_MAX_CRYPT_KEY_LENGTH)
+		return -1;
+
+	if (!memcmp(dev->current_key, key, len)) {
+		dev->flags |= HIFN_FLAG_OLD_KEY;

drivers/crypto/hifn_795x.c line 1607

Quoted 10 lines
drivers/crypto/hifn_795x.c new file +10 -0
+		return 0;
+	}
+
+	dev->flags &= ~HIFN_FLAG_OLD_KEY;
+
+	memcpy(dev->current_key, key, len);
+	dev->current_key_len = len;
+
+	return 0;
+}
Quoted 8 lines

...
dev is allocated by the crypto API but isn't initialized right? Nothing points to your real hw right?
I (in my case) assign directly key ctx to hw ctx in set_key. This is crap because more than just one hw could exist. The API chooses the algo with the highest prio so you can't use more than one device. A load balancer could be the person in charge for assigning hw ctx to crypto user ctx.

I should use crypto context here, it will hosts a pointer to hifn device and key, so far key is stored in hifn_device, which is one per HIFN cpu. Yes, I know, it is not the right way to do it.
Context should contain pointer to hardware structure and keys.

Quoted 8 lines

drivers/crypto/hifn_795x.c line 1641

Quoted 4 lines
drivers/crypto/hifn_795x.c new file +4 -0
+static inline int hifn_encrypt_aes_ecb_16(struct ablkcipher_request *req)
+{
+	return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT, ACRYPTO_TYPE_AES_128, ACRYPTO_MODE_ECB);
+}

...
This is what I had in mind, as I said look on my skeleton, than you will see how you can distinguish which algo is requested. Since you have 12 algos I understand now what you meant with "it doesn't" and your cat :)

HIFN also supports hashes, compressions and multiplication operations... This will look so horrible, that people will commit suicide, when are trying to fix something in such driver.

Quoted 1 line

Sebastian

Signature · 1 line
	Evgeniy Polyakov
#3 lore raw
Quoted 4 lines · Evgeniy Polyakov

This is tricky, hifn requires to have a key per crypto setup, so this one must be in a crypto context, but context is not used right now, so yes, it supports only one user. I will update it to use context, since it is smaller that hifn_device.

Hmm, I was wrong, it is not possible (from first view).

Here is a sequence of events to setup a crypto session:
1. user calls setkey, which results in tfm. It is possible to get a context from tfm. Context can be initialized via cra_init() callback in registered algorithm. But there is no way to provide a private pointer into that callback. So, there is no way to provide a pointer to hifn_device and thus tfm context can not find a hardware device to setup this session.

Herbert, could you please elaborate how to provide a private pointer into cra_init without searching via static lists of all devices?

Signature · 1 line
	Evgeniy Polyakov
maintainer
#8 lore raw
In reply to Evgeniy Polyakov
Quoted 10 lines · Evgeniy Polyakov

take2:
* added a lot of cryptoapi interfaces (i.e. support for all crypto modes and three ciphers: aes, des and 3des via cryptoapi).

My cat has pissed on my domestic slippers when he saw that. And believe me, he does know, how to distinguish between good and other code. Well, I do not have a cat, but if I would, he would definitely piss on my slippers. The most interesting stuff starts from 1600+ lines. Herbert, are you sure it is ok?

Well I congratulate your imaginary cat because he's obviously seen something fishy :)

drivers/crypto/hifn_795x.c line 1827

Quoted 11 lines
drivers/crypto/hifn_795x.c new file +11 -0
+static struct hifn_alg_template hifn_alg_templates[] = {
+	{
+		.name = "ecb(aes)", .drv_name = "hifn-aes", .bsize = 16, 
+		.ablkcipher = {
+			.min_keysize	=	AES_MIN_KEY_SIZE,
+			.max_keysize	=	AES_MAX_KEY_SIZE,
+			.setkey		=	hifn_setkey,
+			.encrypt	=	hifn_encrypt_aes_ecb_16,
+			.decrypt	=	hifn_decrypt_aes_ecb_16,
+		},
+	},

You don't have to repeat this for every key size, well not just yet. If we get enough weird hardware in future that doesn't support all of 16/24/32, then it might become necessary.

So for now you should reduce the 16/24/32 variants down to just one.

drivers/crypto/hifn_795x.c line 1859

Quoted 2 lines
drivers/crypto/hifn_795x.c new file +2 -0
+	{
+		.name = "ecb(aes)", .drv_name = "hifn-aes", .bsize = 24, 

Even if we were doing different key sizes individually, it's not bsize that needs changing but keysize. The block size for AES is always 16.

I'll look over the rest of it next.

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
maintainer
#9 lore raw
In reply to Evgeniy Polyakov

Hi Evgeniey:

Sorry for taking so long to get back to you. I've been busy lately.

Quoted 16 lines · Evgeniy Polyakov
drivers/crypto/hifn_795x.c new file +15 -0
+static int hifn_setup_crypto(struct ablkcipher_request *req, u8 op, u8 type, u8 mode)
+{
+	struct hifn_device *dev = ablkcipher_request_ctx(req);
+	int err = -EBUSY;
+	unsigned long flags;
+
+	spin_lock_irqsave(&dev->lock, flags);
+	if (dev->started < HIFN_QUEUE_LENGTH) {
+		err = hifn_setup_session(dev, req->src, 1, req->dst, 1, 
+				(dev->flags & HIFN_FLAG_OLD_KEY)?NULL:dev->current_key, dev->current_key_len,
+				NULL, 0, 
+				op, type, mode, dev->snum, req);
+		dev->snum++;
+		dev->started++;
+	}

drivers/crypto/hifn_795x.c line 1633

Quoted 10 lines
drivers/crypto/hifn_795x.c new file +10 -0
+	spin_unlock_irqrestore(&dev->lock, flags);
+
+	/*
+	 * HEAVY TODO: needs to kick Herbert XU to write documentation.
+	 * HEAVY TODO: needs to kick Herbert XU to write documentation.
+	 * HEAVY TODO: needs to kick Herbert XU to write documentation.
+	 *
+	 * Actually I need to think about how to handle the case, when queue is full.
+	 * So far error (-EINVAL) is returned.
+	 */

OK you need to provide a software queue here. Since you already have a hardware queue, you may choose to have a queue with a (advisory) maximum length of zero. However, a queue is still necessary since requests with the MAY_BACKLOG flag must never be discarded.

This is (or will be) used by users such as dm-crypt that must be able to add at least one request but can throttle themselves afterwards .

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
maintainer
#4 lore raw
In reply to Evgeniy Polyakov
Quoted 3 lines · Evgeniy Polyakov

Herbert, could you please elaborate how to provide a private pointer into cra_init without searching via static lists of all devices?

The crypto_alg object should contain a back pointer to the device. In setkey, you have access to a tfm object which has a pointer to the crypto_alg object.

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#10 lore raw
In reply to Herbert Xu

Hi Herbert.

Quoted 10 lines · Herbert Xu

drivers/crypto/hifn_795x.c line 1633

Quoted 3 lines
drivers/crypto/hifn_795x.c new file +3 -0
+	 * Actually I need to think about how to handle the case, when queue is full.
+	 * So far error (-EINVAL) is returned.
+	 */

OK you need to provide a software queue here. Since you already have a hardware queue, you may choose to have a queue with a (advisory) maximum length of zero. However, a queue is still necessary since requests with the MAY_BACKLOG flag must never be discarded.

Quoted 3 lines

This is (or will be) used by users such as dm-crypt that must be able to add at least one request but can throttle themselves afterwards .

What will prevent crypto user from filling that queue with crypto requests more and more when hardware is not capable to work with such rate?

Signature · 1 line
	Evgeniy Polyakov
#5 lore raw
In reply to Herbert Xu
Quoted 8 lines · Herbert Xu
Quoted 3 lines · Evgeniy Polyakov

Herbert, could you please elaborate how to provide a private pointer into cra_init without searching via static lists of all devices?

The crypto_alg object should contain a back pointer to the device. In setkey, you have access to a tfm object which has a pointer to the crypto_alg object.

crypto_alg does not contain it, or do I miss something?

Signature · 1 line
	Evgeniy Polyakov
#11 lore raw
In reply to Evgeniy Polyakov
Quoted 11 lines · * Evgeniy Polyakov | 2007-06-22 15:57:26 [+0400]:
Quoted 10 lines · Herbert Xu

drivers/crypto/hifn_795x.c line 1633

Quoted 3 lines
drivers/crypto/hifn_795x.c new file +3 -0
+	 * Actually I need to think about how to handle the case, when queue is full.
+	 * So far error (-EINVAL) is returned.
+	 */

OK you need to provide a software queue here. Since you already have a hardware queue, you may choose to have a queue with a (advisory) maximum length of zero. However, a queue is still necessary since requests with the MAY_BACKLOG flag must never be discarded.

Quoted 7 lines
Quoted 3 lines

This is (or will be) used by users such as dm-crypt that must be able to add at least one request but can throttle themselves afterwards .

What will prevent crypto user from filling that queue with crypto requests more and more when hardware is not capable to work with such rate?

Nothing, the hw signalizes with -EBUSY such a state and the crypto user has to slow down.
If the crypto user continues such a behavior, the system will go OOM (this is atleast my understanding).

Quoted 2 lines
Signature · 1 line
	Evgeniy Polyakov

Sebastian

maintainer
#12 lore raw
Quoted 9 lines · Sebastian Siewior
Quoted 4 lines

What will prevent crypto user from filling that queue with crypto requests more and more when hardware is not capable to work with such rate?

Nothing, the hw signalizes with -EBUSY such a state and the crypto user has to slow down.
If the crypto user continues such a behavior, the system will go OOM (this is atleast my understanding).

Yes, we're assuming that each tfm will only use MAY_BACKLOG once so we don't actually need to allocate memory for it at request time.

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
maintainer
#6 lore raw
In reply to Evgeniy Polyakov
Quoted 6 lines · Evgeniy Polyakov
Quoted 4 lines

The crypto_alg object should contain a back pointer to the device. In setkey, you have access to a tfm object which has a pointer to the crypto_alg object.

crypto_alg does not contain it, or do I miss something?

You're right that it's not there as such. However, there's nothing stopping you from allocating struct crypto_alg + ctx and putting your pointer there.

To make this easier, I'll add a pointer to the API explicitly.

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#13 lore raw
In reply to Sebastian Siewior
Quoted 17 lines · Sebastian Siewior
Quoted 15 lines · * Evgeniy Polyakov | 2007-06-22 15:57:26 [+0400]:
Quoted 13 lines · Herbert Xu

drivers/crypto/hifn_795x.c line 1633

Quoted 3 lines
drivers/crypto/hifn_795x.c new file +3 -0
+	 * Actually I need to think about how to handle the case, when queue is full.
+	 * So far error (-EINVAL) is returned.
+	 */

OK you need to provide a software queue here. Since you already have a hardware queue, you may choose to have a queue with a (advisory) maximum length of zero. However, a queue is still necessary since requests with the MAY_BACKLOG flag must never be discarded.

This is (or will be) used by users such as dm-crypt that must be able to add at least one request but can throttle themselves afterwards .

Quoted 8 lines
Quoted 3 lines

What will prevent crypto user from filling that queue with crypto requests more and more when hardware is not capable to work with such rate?

Nothing, the hw signalizes with -EBUSY such a state and the crypto user has to slow down.
If the crypto user continues such a behavior, the system will go OOM (this is atleast my understanding).

That is what is returned right now - -EBUSY if queue is full and -EINVAL if there are some more serious errors, but there is no way dm-crypt will recover from error.

Signature · 1 line
	Evgeniy Polyakov
#7 lore raw
In reply to Herbert Xu
Quoted 11 lines · Herbert Xu
Quoted 6 lines · Evgeniy Polyakov
Quoted 4 lines

The crypto_alg object should contain a back pointer to the device. In setkey, you have access to a tfm object which has a pointer to the crypto_alg object.

crypto_alg does not contain it, or do I miss something?

You're right that it's not there as such. However, there's nothing stopping you from allocating struct crypto_alg + ctx and putting your pointer there.

Argh, I understand now. I will create an access wrapper in HIFN driver for such access for now, if there will be some API changes it will be possible to rewrite.

Signature · 1 line
	Evgeniy Polyakov