crct10dif-ce-glue.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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(u16 init_crc, const u8 buf[], u64 len);
  21. static int crct10dif_init(struct shash_desc *desc)
  22. {
  23. u16 *crc = shash_desc_ctx(desc);
  24. *crc = 0;
  25. return 0;
  26. }
  27. static int crct10dif_update(struct shash_desc *desc, const u8 *data,
  28. unsigned int length)
  29. {
  30. u16 *crc = shash_desc_ctx(desc);
  31. if (length >= CRC_T10DIF_PMULL_CHUNK_SIZE && may_use_simd()) {
  32. kernel_neon_begin();
  33. *crc = crc_t10dif_pmull(*crc, data, length);
  34. kernel_neon_end();
  35. } else {
  36. *crc = crc_t10dif_generic(*crc, data, length);
  37. }
  38. return 0;
  39. }
  40. static int crct10dif_final(struct shash_desc *desc, u8 *out)
  41. {
  42. u16 *crc = shash_desc_ctx(desc);
  43. *(u16 *)out = *crc;
  44. return 0;
  45. }
  46. static struct shash_alg crc_t10dif_alg = {
  47. .digestsize = CRC_T10DIF_DIGEST_SIZE,
  48. .init = crct10dif_init,
  49. .update = crct10dif_update,
  50. .final = crct10dif_final,
  51. .descsize = CRC_T10DIF_DIGEST_SIZE,
  52. .base.cra_name = "crct10dif",
  53. .base.cra_driver_name = "crct10dif-arm64-ce",
  54. .base.cra_priority = 200,
  55. .base.cra_blocksize = CRC_T10DIF_BLOCK_SIZE,
  56. .base.cra_module = THIS_MODULE,
  57. };
  58. static int __init crc_t10dif_mod_init(void)
  59. {
  60. return crypto_register_shash(&crc_t10dif_alg);
  61. }
  62. static void __exit crc_t10dif_mod_exit(void)
  63. {
  64. crypto_unregister_shash(&crc_t10dif_alg);
  65. }
  66. module_cpu_feature_match(PMULL, crc_t10dif_mod_init);
  67. module_exit(crc_t10dif_mod_exit);
  68. MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
  69. MODULE_LICENSE("GPL v2");