^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * CRC vpmsum tester
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright 2017 Daniel Axtens, IBM Corporation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/crc-t10dif.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/crc32.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <crypto/internal/hash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/random.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/cpufeature.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <asm/switch_to.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) static unsigned long iterations = 10000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #define MAX_CRC_LENGTH 65535
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) static int __init crc_test_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) u16 crc16 = 0, verify16 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) __le32 verify32le = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) unsigned char *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) u32 verify32 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) unsigned long i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) __le32 crc32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) struct crypto_shash *crct10dif_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) struct crypto_shash *crc32c_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) if (!cpu_has_feature(CPU_FTR_ARCH_207S))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) data = kmalloc(MAX_CRC_LENGTH, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) crct10dif_tfm = crypto_alloc_shash("crct10dif", 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) if (IS_ERR(crct10dif_tfm)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) pr_err("Error allocating crc-t10dif\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) goto free_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) crc32c_tfm = crypto_alloc_shash("crc32c", 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) if (IS_ERR(crc32c_tfm)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) pr_err("Error allocating crc32c\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) goto free_16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) SHASH_DESC_ON_STACK(crct10dif_shash, crct10dif_tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) SHASH_DESC_ON_STACK(crc32c_shash, crc32c_tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) crct10dif_shash->tfm = crct10dif_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) ret = crypto_shash_init(crct10dif_shash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) pr_err("Error initing crc-t10dif\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) goto free_32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) crc32c_shash->tfm = crc32c_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) ret = crypto_shash_init(crc32c_shash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) pr_err("Error initing crc32c\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) goto free_32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) pr_info("crc-vpmsum_test begins, %lu iterations\n", iterations);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) for (i=0; i<iterations; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) size_t offset = prandom_u32_max(16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) size_t len = prandom_u32_max(MAX_CRC_LENGTH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (len <= offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) prandom_bytes(data, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) len -= offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) crypto_shash_update(crct10dif_shash, data+offset, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) crypto_shash_final(crct10dif_shash, (u8 *)(&crc16));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) verify16 = crc_t10dif_generic(verify16, data+offset, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) if (crc16 != verify16) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) pr_err("FAILURE in CRC16: got 0x%04x expected 0x%04x (len %lu)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) crc16, verify16, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) crypto_shash_update(crc32c_shash, data+offset, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) crypto_shash_final(crc32c_shash, (u8 *)(&crc32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) verify32 = le32_to_cpu(verify32le);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) verify32le = ~cpu_to_le32(__crc32c_le(~verify32, data+offset, len));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) if (crc32 != verify32le) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) pr_err("FAILURE in CRC32: got 0x%08x expected 0x%08x (len %lu)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) crc32, verify32, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) pr_info("crc-vpmsum_test done, completed %lu iterations\n", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) } while (0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) free_32:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) crypto_free_shash(crc32c_tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) free_16:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) crypto_free_shash(crct10dif_tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) free_buf:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) kfree(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) static void __exit crc_test_exit(void) {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) module_init(crc_test_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) module_exit(crc_test_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) module_param(iterations, long, 0400);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) MODULE_AUTHOR("Daniel Axtens <dja@axtens.net>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) MODULE_DESCRIPTION("Vector polynomial multiply-sum CRC tester");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) MODULE_LICENSE("GPL");