crct10dif-ce-glue.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. unsigned int l;
  31. if (!may_use_simd()) {
  32. *crc = crc_t10dif_generic(*crc, data, length);
  33. } else {
  34. if (unlikely((u32)data % CRC_T10DIF_PMULL_CHUNK_SIZE)) {
  35. l = min_t(u32, length, CRC_T10DIF_PMULL_CHUNK_SIZE -
  36. ((u32)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. kernel_neon_begin();
  43. *crc = crc_t10dif_pmull(*crc, data, length);
  44. kernel_neon_end();
  45. }
  46. }
  47. return 0;
  48. }
  49. static int crct10dif_final(struct shash_desc *desc, u8 *out)
  50. {
  51. u16 *crc = shash_desc_ctx(desc);
  52. *(u16 *)out = *crc;
  53. return 0;
  54. }
  55. static struct shash_alg crc_t10dif_alg = {
  56. .digestsize = CRC_T10DIF_DIGEST_SIZE,
  57. .init = crct10dif_init,
  58. .update = crct10dif_update,
  59. .final = crct10dif_final,
  60. .descsize = CRC_T10DIF_DIGEST_SIZE,
  61. .base.cra_name = "crct10dif",
  62. .base.cra_driver_name = "crct10dif-arm-ce",
  63. .base.cra_priority = 200,
  64. .base.cra_blocksize = CRC_T10DIF_BLOCK_SIZE,
  65. .base.cra_module = THIS_MODULE,
  66. };
  67. static int __init crc_t10dif_mod_init(void)
  68. {
  69. if (!(elf_hwcap2 & HWCAP2_PMULL))
  70. return -ENODEV;
  71. return crypto_register_shash(&crc_t10dif_alg);
  72. }
  73. static void __exit crc_t10dif_mod_exit(void)
  74. {
  75. crypto_unregister_shash(&crc_t10dif_alg);
  76. }
  77. module_init(crc_t10dif_mod_init);
  78. module_exit(crc_t10dif_mod_exit);
  79. MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
  80. MODULE_LICENSE("GPL v2");
  81. MODULE_ALIAS_CRYPTO("crct10dif");