crct10dif-ce-glue.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Accelerated CRC-T10DIF using arm64 NEON and Crypto Extensions instructions
  3. *
  4. * Copyright (C) 2016 - 2017 Linaro Ltd <ard.biesheuvel@linaro.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/cpufeature.h>
  11. #include <linux/crc-t10dif.h>
  12. #include <linux/init.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/string.h>
  16. #include <crypto/internal/hash.h>
  17. #include <asm/neon.h>
  18. #include <asm/simd.h>
  19. #define CRC_T10DIF_PMULL_CHUNK_SIZE 16U
  20. asmlinkage u16 crc_t10dif_pmull_p64(u16 init_crc, const u8 buf[], u64 len);
  21. asmlinkage u16 crc_t10dif_pmull_p8(u16 init_crc, const u8 buf[], u64 len);
  22. static u16 (*crc_t10dif_pmull)(u16 init_crc, const u8 buf[], u64 len);
  23. static int crct10dif_init(struct shash_desc *desc)
  24. {
  25. u16 *crc = shash_desc_ctx(desc);
  26. *crc = 0;
  27. return 0;
  28. }
  29. static int crct10dif_update(struct shash_desc *desc, const u8 *data,
  30. unsigned int length)
  31. {
  32. u16 *crc = shash_desc_ctx(desc);
  33. unsigned int l;
  34. if (unlikely((u64)data % CRC_T10DIF_PMULL_CHUNK_SIZE)) {
  35. l = min_t(u32, length, CRC_T10DIF_PMULL_CHUNK_SIZE -
  36. ((u64)data % CRC_T10DIF_PMULL_CHUNK_SIZE));
  37. *crc = crc_t10dif_generic(*crc, data, l);
  38. length -= l;
  39. data += l;
  40. }
  41. if (length > 0) {
  42. if (may_use_simd()) {
  43. kernel_neon_begin();
  44. *crc = crc_t10dif_pmull(*crc, data, length);
  45. kernel_neon_end();
  46. } else {
  47. *crc = crc_t10dif_generic(*crc, data, length);
  48. }
  49. }
  50. return 0;
  51. }
  52. static int crct10dif_final(struct shash_desc *desc, u8 *out)
  53. {
  54. u16 *crc = shash_desc_ctx(desc);
  55. *(u16 *)out = *crc;
  56. return 0;
  57. }
  58. static struct shash_alg crc_t10dif_alg = {
  59. .digestsize = CRC_T10DIF_DIGEST_SIZE,
  60. .init = crct10dif_init,
  61. .update = crct10dif_update,
  62. .final = crct10dif_final,
  63. .descsize = CRC_T10DIF_DIGEST_SIZE,
  64. .base.cra_name = "crct10dif",
  65. .base.cra_driver_name = "crct10dif-arm64-ce",
  66. .base.cra_priority = 200,
  67. .base.cra_blocksize = CRC_T10DIF_BLOCK_SIZE,
  68. .base.cra_module = THIS_MODULE,
  69. };
  70. static int __init crc_t10dif_mod_init(void)
  71. {
  72. if (elf_hwcap & HWCAP_PMULL)
  73. crc_t10dif_pmull = crc_t10dif_pmull_p64;
  74. else
  75. crc_t10dif_pmull = crc_t10dif_pmull_p8;
  76. return crypto_register_shash(&crc_t10dif_alg);
  77. }
  78. static void __exit crc_t10dif_mod_exit(void)
  79. {
  80. crypto_unregister_shash(&crc_t10dif_alg);
  81. }
  82. module_cpu_feature_match(ASIMD, crc_t10dif_mod_init);
  83. module_exit(crc_t10dif_mod_exit);
  84. MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
  85. MODULE_LICENSE("GPL v2");
  86. MODULE_ALIAS_CRYPTO("crct10dif");
  87. MODULE_ALIAS_CRYPTO("crct10dif-arm64-ce");