Daniel Axtens | b01df1c | 2017-03-15 23:37:36 +1100 | [diff] [blame] | 1 | /* |
| 2 | * Calculate a CRC T10-DIF with vpmsum acceleration |
| 3 | * |
| 4 | * Copyright 2017, Daniel Axtens, IBM Corporation. |
| 5 | * [based on crc32c-vpmsum_glue.c] |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; either version 2 |
| 10 | * of the License, or (at your option) any later version. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/crc-t10dif.h> |
| 14 | #include <crypto/internal/hash.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/string.h> |
| 18 | #include <linux/kernel.h> |
| 19 | #include <linux/cpufeature.h> |
| 20 | #include <asm/switch_to.h> |
| 21 | |
| 22 | #define VMX_ALIGN 16 |
| 23 | #define VMX_ALIGN_MASK (VMX_ALIGN-1) |
| 24 | |
| 25 | #define VECTOR_BREAKPOINT 64 |
| 26 | |
| 27 | u32 __crct10dif_vpmsum(u32 crc, unsigned char const *p, size_t len); |
| 28 | |
| 29 | static u16 crct10dif_vpmsum(u16 crci, unsigned char const *p, size_t len) |
| 30 | { |
| 31 | unsigned int prealign; |
| 32 | unsigned int tail; |
| 33 | u32 crc = crci; |
| 34 | |
| 35 | if (len < (VECTOR_BREAKPOINT + VMX_ALIGN) || in_interrupt()) |
| 36 | return crc_t10dif_generic(crc, p, len); |
| 37 | |
| 38 | if ((unsigned long)p & VMX_ALIGN_MASK) { |
| 39 | prealign = VMX_ALIGN - ((unsigned long)p & VMX_ALIGN_MASK); |
| 40 | crc = crc_t10dif_generic(crc, p, prealign); |
| 41 | len -= prealign; |
| 42 | p += prealign; |
| 43 | } |
| 44 | |
| 45 | if (len & ~VMX_ALIGN_MASK) { |
| 46 | crc <<= 16; |
Michael Ellerman | 0f89f6e | 2017-04-20 15:35:09 +1000 | [diff] [blame] | 47 | preempt_disable(); |
Daniel Axtens | b01df1c | 2017-03-15 23:37:36 +1100 | [diff] [blame] | 48 | pagefault_disable(); |
| 49 | enable_kernel_altivec(); |
| 50 | crc = __crct10dif_vpmsum(crc, p, len & ~VMX_ALIGN_MASK); |
Michael Ellerman | 0f89f6e | 2017-04-20 15:35:09 +1000 | [diff] [blame] | 51 | disable_kernel_altivec(); |
Daniel Axtens | b01df1c | 2017-03-15 23:37:36 +1100 | [diff] [blame] | 52 | pagefault_enable(); |
Michael Ellerman | 0f89f6e | 2017-04-20 15:35:09 +1000 | [diff] [blame] | 53 | preempt_enable(); |
Daniel Axtens | b01df1c | 2017-03-15 23:37:36 +1100 | [diff] [blame] | 54 | crc >>= 16; |
| 55 | } |
| 56 | |
| 57 | tail = len & VMX_ALIGN_MASK; |
| 58 | if (tail) { |
| 59 | p += len & ~VMX_ALIGN_MASK; |
| 60 | crc = crc_t10dif_generic(crc, p, tail); |
| 61 | } |
| 62 | |
| 63 | return crc & 0xffff; |
| 64 | } |
| 65 | |
| 66 | static int crct10dif_vpmsum_init(struct shash_desc *desc) |
| 67 | { |
| 68 | u16 *crc = shash_desc_ctx(desc); |
| 69 | |
| 70 | *crc = 0; |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | static int crct10dif_vpmsum_update(struct shash_desc *desc, const u8 *data, |
| 75 | unsigned int length) |
| 76 | { |
| 77 | u16 *crc = shash_desc_ctx(desc); |
| 78 | |
| 79 | *crc = crct10dif_vpmsum(*crc, data, length); |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | |
| 85 | static int crct10dif_vpmsum_final(struct shash_desc *desc, u8 *out) |
| 86 | { |
| 87 | u16 *crcp = shash_desc_ctx(desc); |
| 88 | |
| 89 | *(u16 *)out = *crcp; |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | static struct shash_alg alg = { |
| 94 | .init = crct10dif_vpmsum_init, |
| 95 | .update = crct10dif_vpmsum_update, |
| 96 | .final = crct10dif_vpmsum_final, |
| 97 | .descsize = CRC_T10DIF_DIGEST_SIZE, |
| 98 | .digestsize = CRC_T10DIF_DIGEST_SIZE, |
| 99 | .base = { |
| 100 | .cra_name = "crct10dif", |
| 101 | .cra_driver_name = "crct10dif-vpmsum", |
| 102 | .cra_priority = 200, |
| 103 | .cra_blocksize = CRC_T10DIF_BLOCK_SIZE, |
| 104 | .cra_module = THIS_MODULE, |
| 105 | } |
| 106 | }; |
| 107 | |
| 108 | static int __init crct10dif_vpmsum_mod_init(void) |
| 109 | { |
| 110 | if (!cpu_has_feature(CPU_FTR_ARCH_207S)) |
| 111 | return -ENODEV; |
| 112 | |
| 113 | return crypto_register_shash(&alg); |
| 114 | } |
| 115 | |
| 116 | static void __exit crct10dif_vpmsum_mod_fini(void) |
| 117 | { |
| 118 | crypto_unregister_shash(&alg); |
| 119 | } |
| 120 | |
| 121 | module_cpu_feature_match(PPC_MODULE_FEATURE_VEC_CRYPTO, crct10dif_vpmsum_mod_init); |
| 122 | module_exit(crct10dif_vpmsum_mod_fini); |
| 123 | |
| 124 | MODULE_AUTHOR("Daniel Axtens <dja@axtens.net>"); |
| 125 | MODULE_DESCRIPTION("CRCT10DIF using vector polynomial multiply-sum instructions"); |
| 126 | MODULE_LICENSE("GPL"); |
| 127 | MODULE_ALIAS_CRYPTO("crct10dif"); |
| 128 | MODULE_ALIAS_CRYPTO("crct10dif-vpmsum"); |