include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[safe/jmp/linux-2.6] / drivers / crypto / padlock-aes.c
index 87f92c3..2e992bc 100644 (file)
 #include <linux/kernel.h>
 #include <linux/percpu.h>
 #include <linux/smp.h>
+#include <linux/slab.h>
 #include <asm/byteorder.h>
+#include <asm/processor.h>
 #include <asm/i387.h>
 #include "padlock.h"
 
+/*
+ * Number of data blocks actually fetched for each xcrypt insn.
+ * Processors with prefetch errata will fetch extra blocks.
+ */
+static unsigned int ecb_fetch_blocks = 2;
+#define MAX_ECB_FETCH_BLOCKS (8)
+#define ecb_fetch_bytes (ecb_fetch_blocks * AES_BLOCK_SIZE)
+
+static unsigned int cbc_fetch_blocks = 1;
+#define MAX_CBC_FETCH_BLOCKS (4)
+#define cbc_fetch_bytes (cbc_fetch_blocks * AES_BLOCK_SIZE)
+
 /* Control word. */
 struct cword {
        unsigned int __attribute__ ((__packed__))
@@ -51,7 +65,7 @@ struct aes_ctx {
        u32 *D;
 };
 
-static DEFINE_PER_CPU(struct cword *, last_cword);
+static DEFINE_PER_CPU(struct cword *, paes_last_cword);
 
 /* Tells whether the ACE is capable to generate
    the extended key for a given key_len. */
@@ -139,9 +153,9 @@ static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
 
 ok:
        for_each_online_cpu(cpu)
-               if (&ctx->cword.encrypt == per_cpu(last_cword, cpu) ||
-                   &ctx->cword.decrypt == per_cpu(last_cword, cpu))
-                       per_cpu(last_cword, cpu) = NULL;
+               if (&ctx->cword.encrypt == per_cpu(paes_last_cword, cpu) ||
+                   &ctx->cword.decrypt == per_cpu(paes_last_cword, cpu))
+                       per_cpu(paes_last_cword, cpu) = NULL;
 
        return 0;
 }
@@ -153,7 +167,7 @@ static inline void padlock_reset_key(struct cword *cword)
 {
        int cpu = raw_smp_processor_id();
 
-       if (cword != per_cpu(last_cword, cpu))
+       if (cword != per_cpu(paes_last_cword, cpu))
 #ifndef CONFIG_X86_64
                asm volatile ("pushfl; popfl");
 #else
@@ -163,7 +177,7 @@ static inline void padlock_reset_key(struct cword *cword)
 
 static inline void padlock_store_cword(struct cword *cword)
 {
-       per_cpu(last_cword, raw_smp_processor_id()) = cword;
+       per_cpu(paes_last_cword, raw_smp_processor_id()) = cword;
 }
 
 /*
@@ -172,73 +186,111 @@ static inline void padlock_store_cword(struct cword *cword)
  * should be used only inside the irq_ts_save/restore() context
  */
 
-static inline void padlock_xcrypt(const u8 *input, u8 *output, void *key,
-                                 struct cword *control_word)
+static inline void rep_xcrypt_ecb(const u8 *input, u8 *output, void *key,
+                                 struct cword *control_word, int count)
 {
        asm volatile (".byte 0xf3,0x0f,0xa7,0xc8"       /* rep xcryptecb */
                      : "+S"(input), "+D"(output)
-                     : "d"(control_word), "b"(key), "c"(1));
+                     : "d"(control_word), "b"(key), "c"(count));
+}
+
+static inline u8 *rep_xcrypt_cbc(const u8 *input, u8 *output, void *key,
+                                u8 *iv, struct cword *control_word, int count)
+{
+       asm volatile (".byte 0xf3,0x0f,0xa7,0xd0"       /* rep xcryptcbc */
+                     : "+S" (input), "+D" (output), "+a" (iv)
+                     : "d" (control_word), "b" (key), "c" (count));
+       return iv;
 }
 
-static void aes_crypt_copy(const u8 *in, u8 *out, u32 *key, struct cword *cword)
+static void ecb_crypt_copy(const u8 *in, u8 *out, u32 *key,
+                          struct cword *cword, int count)
 {
-       u8 buf[AES_BLOCK_SIZE * 2 + PADLOCK_ALIGNMENT - 1];
+       /*
+        * Padlock prefetches extra data so we must provide mapped input buffers.
+        * Assume there are at least 16 bytes of stack already in use.
+        */
+       u8 buf[AES_BLOCK_SIZE * (MAX_ECB_FETCH_BLOCKS - 1) + PADLOCK_ALIGNMENT - 1];
+       u8 *tmp = PTR_ALIGN(&buf[0], PADLOCK_ALIGNMENT);
+
+       memcpy(tmp, in, count * AES_BLOCK_SIZE);
+       rep_xcrypt_ecb(tmp, out, key, cword, count);
+}
+
+static u8 *cbc_crypt_copy(const u8 *in, u8 *out, u32 *key,
+                          u8 *iv, struct cword *cword, int count)
+{
+       /*
+        * Padlock prefetches extra data so we must provide mapped input buffers.
+        * Assume there are at least 16 bytes of stack already in use.
+        */
+       u8 buf[AES_BLOCK_SIZE * (MAX_CBC_FETCH_BLOCKS - 1) + PADLOCK_ALIGNMENT - 1];
        u8 *tmp = PTR_ALIGN(&buf[0], PADLOCK_ALIGNMENT);
 
-       memcpy(tmp, in, AES_BLOCK_SIZE);
-       padlock_xcrypt(tmp, out, key, cword);
+       memcpy(tmp, in, count * AES_BLOCK_SIZE);
+       return rep_xcrypt_cbc(tmp, out, key, iv, cword, count);
 }
 
-static inline void aes_crypt(const u8 *in, u8 *out, u32 *key,
-                            struct cword *cword)
+static inline void ecb_crypt(const u8 *in, u8 *out, u32 *key,
+                            struct cword *cword, int count)
 {
-       /* padlock_xcrypt requires at least two blocks of data. */
-       if (unlikely(!(((unsigned long)in ^ (PAGE_SIZE - AES_BLOCK_SIZE)) &
-                      (PAGE_SIZE - 1)))) {
-               aes_crypt_copy(in, out, key, cword);
+       /* Padlock in ECB mode fetches at least ecb_fetch_bytes of data.
+        * We could avoid some copying here but it's probably not worth it.
+        */
+       if (unlikely(((unsigned long)in & ~PAGE_MASK) + ecb_fetch_bytes > PAGE_SIZE)) {
+               ecb_crypt_copy(in, out, key, cword, count);
                return;
        }
 
-       padlock_xcrypt(in, out, key, cword);
+       rep_xcrypt_ecb(in, out, key, cword, count);
+}
+
+static inline u8 *cbc_crypt(const u8 *in, u8 *out, u32 *key,
+                           u8 *iv, struct cword *cword, int count)
+{
+       /* Padlock in CBC mode fetches at least cbc_fetch_bytes of data. */
+       if (unlikely(((unsigned long)in & ~PAGE_MASK) + cbc_fetch_bytes > PAGE_SIZE))
+               return cbc_crypt_copy(in, out, key, iv, cword, count);
+
+       return rep_xcrypt_cbc(in, out, key, iv, cword, count);
 }
 
 static inline void padlock_xcrypt_ecb(const u8 *input, u8 *output, void *key,
                                      void *control_word, u32 count)
 {
-       if (count == 1) {
-               aes_crypt(input, output, key, control_word);
+       u32 initial = count & (ecb_fetch_blocks - 1);
+
+       if (count < ecb_fetch_blocks) {
+               ecb_crypt(input, output, key, control_word, count);
                return;
        }
 
-       asm volatile ("test $1, %%cl;"
-                     "je 1f;"
-#ifndef CONFIG_X86_64
-                     "lea -1(%%ecx), %%eax;"
-                     "mov $1, %%ecx;"
-#else
-                     "lea -1(%%rcx), %%rax;"
-                     "mov $1, %%rcx;"
-#endif
-                     ".byte 0xf3,0x0f,0xa7,0xc8;"      /* rep xcryptecb */
-#ifndef CONFIG_X86_64
-                     "mov %%eax, %%ecx;"
-#else
-                     "mov %%rax, %%rcx;"
-#endif
-                     "1:"
-                     ".byte 0xf3,0x0f,0xa7,0xc8"       /* rep xcryptecb */
+       if (initial)
+               asm volatile (".byte 0xf3,0x0f,0xa7,0xc8"       /* rep xcryptecb */
+                             : "+S"(input), "+D"(output)
+                             : "d"(control_word), "b"(key), "c"(initial));
+
+       asm volatile (".byte 0xf3,0x0f,0xa7,0xc8"       /* rep xcryptecb */
                      : "+S"(input), "+D"(output)
-                     : "d"(control_word), "b"(key), "c"(count)
-                     : "ax");
+                     : "d"(control_word), "b"(key), "c"(count - initial));
 }
 
 static inline u8 *padlock_xcrypt_cbc(const u8 *input, u8 *output, void *key,
                                     u8 *iv, void *control_word, u32 count)
 {
-       /* rep xcryptcbc */
-       asm volatile (".byte 0xf3,0x0f,0xa7,0xd0"
+       u32 initial = count & (cbc_fetch_blocks - 1);
+
+       if (count < cbc_fetch_blocks)
+               return cbc_crypt(input, output, key, iv, control_word, count);
+
+       if (initial)
+               asm volatile (".byte 0xf3,0x0f,0xa7,0xd0"       /* rep xcryptcbc */
+                             : "+S" (input), "+D" (output), "+a" (iv)
+                             : "d" (control_word), "b" (key), "c" (count));
+
+       asm volatile (".byte 0xf3,0x0f,0xa7,0xd0"       /* rep xcryptcbc */
                      : "+S" (input), "+D" (output), "+a" (iv)
-                     : "d" (control_word), "b" (key), "c" (count));
+                     : "d" (control_word), "b" (key), "c" (count-initial));
        return iv;
 }
 
@@ -249,7 +301,7 @@ static void aes_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
 
        padlock_reset_key(&ctx->cword.encrypt);
        ts_state = irq_ts_save();
-       aes_crypt(in, out, ctx->E, &ctx->cword.encrypt);
+       ecb_crypt(in, out, ctx->E, &ctx->cword.encrypt, 1);
        irq_ts_restore(ts_state);
        padlock_store_cword(&ctx->cword.encrypt);
 }
@@ -261,7 +313,7 @@ static void aes_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
 
        padlock_reset_key(&ctx->cword.encrypt);
        ts_state = irq_ts_save();
-       aes_crypt(in, out, ctx->D, &ctx->cword.decrypt);
+       ecb_crypt(in, out, ctx->D, &ctx->cword.decrypt, 1);
        irq_ts_restore(ts_state);
        padlock_store_cword(&ctx->cword.encrypt);
 }
@@ -454,6 +506,7 @@ static struct crypto_alg cbc_aes_alg = {
 static int __init padlock_init(void)
 {
        int ret;
+       struct cpuinfo_x86 *c = &cpu_data(0);
 
        if (!cpu_has_xcrypt) {
                printk(KERN_NOTICE PFX "VIA PadLock not detected.\n");
@@ -476,6 +529,12 @@ static int __init padlock_init(void)
 
        printk(KERN_NOTICE PFX "Using VIA PadLock ACE for AES algorithm.\n");
 
+       if (c->x86 == 6 && c->x86_model == 15 && c->x86_mask == 2) {
+               ecb_fetch_blocks = MAX_ECB_FETCH_BLOCKS;
+               cbc_fetch_blocks = MAX_CBC_FETCH_BLOCKS;
+               printk(KERN_NOTICE PFX "VIA Nano stepping 2 detected: enabling workaround.\n");
+       }
+
 out:
        return ret;