crct10dif-ce-glue.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Accelerated CRC-T10DIF using ARM NEON and Crypto Extensions instructions
  4. *
  5. * Copyright (C) 2016 Linaro Ltd <[email protected]>
  6. */
  7. #include <linux/crc-t10dif.h>
  8. #include <linux/init.h>
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/string.h>
  12. #include <crypto/internal/hash.h>
  13. #include <crypto/internal/simd.h>
  14. #include <asm/neon.h>
  15. #include <asm/simd.h>
  16. #define CRC_T10DIF_PMULL_CHUNK_SIZE 16U
  17. asmlinkage u16 crc_t10dif_pmull(u16 init_crc, const u8 *buf, size_t len);
  18. static int crct10dif_init(struct shash_desc *desc)
  19. {
  20. u16 *crc = shash_desc_ctx(desc);
  21. *crc = 0;
  22. return 0;
  23. }
  24. static int crct10dif_update(struct shash_desc *desc, const u8 *data,
  25. unsigned int length)
  26. {
  27. u16 *crc = shash_desc_ctx(desc);
  28. if (length >= CRC_T10DIF_PMULL_CHUNK_SIZE && crypto_simd_usable()) {
  29. kernel_neon_begin();
  30. *crc = crc_t10dif_pmull(*crc, data, length);
  31. kernel_neon_end();
  32. } else {
  33. *crc = crc_t10dif_generic(*crc, data, length);
  34. }
  35. return 0;
  36. }
  37. static int crct10dif_final(struct shash_desc *desc, u8 *out)
  38. {
  39. u16 *crc = shash_desc_ctx(desc);
  40. *(u16 *)out = *crc;
  41. return 0;
  42. }
  43. static struct shash_alg crc_t10dif_alg = {
  44. .digestsize = CRC_T10DIF_DIGEST_SIZE,
  45. .init = crct10dif_init,
  46. .update = crct10dif_update,
  47. .final = crct10dif_final,
  48. .descsize = CRC_T10DIF_DIGEST_SIZE,
  49. .base.cra_name = "crct10dif",
  50. .base.cra_driver_name = "crct10dif-arm-ce",
  51. .base.cra_priority = 200,
  52. .base.cra_blocksize = CRC_T10DIF_BLOCK_SIZE,
  53. .base.cra_module = THIS_MODULE,
  54. };
  55. static int __init crc_t10dif_mod_init(void)
  56. {
  57. if (!(elf_hwcap2 & HWCAP2_PMULL))
  58. return -ENODEV;
  59. return crypto_register_shash(&crc_t10dif_alg);
  60. }
  61. static void __exit crc_t10dif_mod_exit(void)
  62. {
  63. crypto_unregister_shash(&crc_t10dif_alg);
  64. }
  65. module_init(crc_t10dif_mod_init);
  66. module_exit(crc_t10dif_mod_exit);
  67. MODULE_AUTHOR("Ard Biesheuvel <[email protected]>");
  68. MODULE_LICENSE("GPL v2");
  69. MODULE_ALIAS_CRYPTO("crct10dif");