crct10dif-ce-glue.c 2.1 KB

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