18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * CRC vpmsum tester 48c2ecf20Sopenharmony_ci * Copyright 2017 Daniel Axtens, IBM Corporation. 58c2ecf20Sopenharmony_ci */ 68c2ecf20Sopenharmony_ci 78c2ecf20Sopenharmony_ci#include <linux/crc-t10dif.h> 88c2ecf20Sopenharmony_ci#include <linux/crc32.h> 98c2ecf20Sopenharmony_ci#include <crypto/internal/hash.h> 108c2ecf20Sopenharmony_ci#include <linux/init.h> 118c2ecf20Sopenharmony_ci#include <linux/module.h> 128c2ecf20Sopenharmony_ci#include <linux/random.h> 138c2ecf20Sopenharmony_ci#include <linux/string.h> 148c2ecf20Sopenharmony_ci#include <linux/kernel.h> 158c2ecf20Sopenharmony_ci#include <linux/cpufeature.h> 168c2ecf20Sopenharmony_ci#include <asm/switch_to.h> 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_cistatic unsigned long iterations = 10000; 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_ci#define MAX_CRC_LENGTH 65535 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_cistatic int __init crc_test_init(void) 248c2ecf20Sopenharmony_ci{ 258c2ecf20Sopenharmony_ci u16 crc16 = 0, verify16 = 0; 268c2ecf20Sopenharmony_ci __le32 verify32le = 0; 278c2ecf20Sopenharmony_ci unsigned char *data; 288c2ecf20Sopenharmony_ci u32 verify32 = 0; 298c2ecf20Sopenharmony_ci unsigned long i; 308c2ecf20Sopenharmony_ci __le32 crc32; 318c2ecf20Sopenharmony_ci int ret; 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci struct crypto_shash *crct10dif_tfm; 348c2ecf20Sopenharmony_ci struct crypto_shash *crc32c_tfm; 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_ci if (!cpu_has_feature(CPU_FTR_ARCH_207S)) 378c2ecf20Sopenharmony_ci return -ENODEV; 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ci data = kmalloc(MAX_CRC_LENGTH, GFP_KERNEL); 408c2ecf20Sopenharmony_ci if (!data) 418c2ecf20Sopenharmony_ci return -ENOMEM; 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_ci crct10dif_tfm = crypto_alloc_shash("crct10dif", 0, 0); 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_ci if (IS_ERR(crct10dif_tfm)) { 468c2ecf20Sopenharmony_ci pr_err("Error allocating crc-t10dif\n"); 478c2ecf20Sopenharmony_ci goto free_buf; 488c2ecf20Sopenharmony_ci } 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci crc32c_tfm = crypto_alloc_shash("crc32c", 0, 0); 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci if (IS_ERR(crc32c_tfm)) { 538c2ecf20Sopenharmony_ci pr_err("Error allocating crc32c\n"); 548c2ecf20Sopenharmony_ci goto free_16; 558c2ecf20Sopenharmony_ci } 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_ci do { 588c2ecf20Sopenharmony_ci SHASH_DESC_ON_STACK(crct10dif_shash, crct10dif_tfm); 598c2ecf20Sopenharmony_ci SHASH_DESC_ON_STACK(crc32c_shash, crc32c_tfm); 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_ci crct10dif_shash->tfm = crct10dif_tfm; 628c2ecf20Sopenharmony_ci ret = crypto_shash_init(crct10dif_shash); 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci if (ret) { 658c2ecf20Sopenharmony_ci pr_err("Error initing crc-t10dif\n"); 668c2ecf20Sopenharmony_ci goto free_32; 678c2ecf20Sopenharmony_ci } 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ci crc32c_shash->tfm = crc32c_tfm; 718c2ecf20Sopenharmony_ci ret = crypto_shash_init(crc32c_shash); 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ci if (ret) { 748c2ecf20Sopenharmony_ci pr_err("Error initing crc32c\n"); 758c2ecf20Sopenharmony_ci goto free_32; 768c2ecf20Sopenharmony_ci } 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_ci pr_info("crc-vpmsum_test begins, %lu iterations\n", iterations); 798c2ecf20Sopenharmony_ci for (i=0; i<iterations; i++) { 808c2ecf20Sopenharmony_ci size_t offset = prandom_u32_max(16); 818c2ecf20Sopenharmony_ci size_t len = prandom_u32_max(MAX_CRC_LENGTH); 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ci if (len <= offset) 848c2ecf20Sopenharmony_ci continue; 858c2ecf20Sopenharmony_ci prandom_bytes(data, len); 868c2ecf20Sopenharmony_ci len -= offset; 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci crypto_shash_update(crct10dif_shash, data+offset, len); 898c2ecf20Sopenharmony_ci crypto_shash_final(crct10dif_shash, (u8 *)(&crc16)); 908c2ecf20Sopenharmony_ci verify16 = crc_t10dif_generic(verify16, data+offset, len); 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_ci if (crc16 != verify16) { 948c2ecf20Sopenharmony_ci pr_err("FAILURE in CRC16: got 0x%04x expected 0x%04x (len %lu)\n", 958c2ecf20Sopenharmony_ci crc16, verify16, len); 968c2ecf20Sopenharmony_ci break; 978c2ecf20Sopenharmony_ci } 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_ci crypto_shash_update(crc32c_shash, data+offset, len); 1008c2ecf20Sopenharmony_ci crypto_shash_final(crc32c_shash, (u8 *)(&crc32)); 1018c2ecf20Sopenharmony_ci verify32 = le32_to_cpu(verify32le); 1028c2ecf20Sopenharmony_ci verify32le = ~cpu_to_le32(__crc32c_le(~verify32, data+offset, len)); 1038c2ecf20Sopenharmony_ci if (crc32 != verify32le) { 1048c2ecf20Sopenharmony_ci pr_err("FAILURE in CRC32: got 0x%08x expected 0x%08x (len %lu)\n", 1058c2ecf20Sopenharmony_ci crc32, verify32, len); 1068c2ecf20Sopenharmony_ci break; 1078c2ecf20Sopenharmony_ci } 1088c2ecf20Sopenharmony_ci cond_resched(); 1098c2ecf20Sopenharmony_ci } 1108c2ecf20Sopenharmony_ci pr_info("crc-vpmsum_test done, completed %lu iterations\n", i); 1118c2ecf20Sopenharmony_ci } while (0); 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_cifree_32: 1148c2ecf20Sopenharmony_ci crypto_free_shash(crc32c_tfm); 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_cifree_16: 1178c2ecf20Sopenharmony_ci crypto_free_shash(crct10dif_tfm); 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_cifree_buf: 1208c2ecf20Sopenharmony_ci kfree(data); 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_ci return 0; 1238c2ecf20Sopenharmony_ci} 1248c2ecf20Sopenharmony_ci 1258c2ecf20Sopenharmony_cistatic void __exit crc_test_exit(void) {} 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_cimodule_init(crc_test_init); 1288c2ecf20Sopenharmony_cimodule_exit(crc_test_exit); 1298c2ecf20Sopenharmony_cimodule_param(iterations, long, 0400); 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_ciMODULE_AUTHOR("Daniel Axtens <dja@axtens.net>"); 1328c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Vector polynomial multiply-sum CRC tester"); 1338c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 134