4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2014, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
34 #include <libcfs/libcfs.h>
35 #include <lnet/lnet_crypto.h>
36 #include <uapi/linux/lustre/lustre_idl.h>
38 int obd_t10_cksum_speed(const char *obd_name,
39 enum cksum_types cksum_type);
41 static inline unsigned char cksum_obd2cfs(enum cksum_types cksum_type)
45 return CFS_HASH_ALG_CRC32;
47 return CFS_HASH_ALG_ADLER32;
48 case OBD_CKSUM_CRC32C:
49 return CFS_HASH_ALG_CRC32C;
51 CERROR("Unknown checksum type (%x)!!!\n", cksum_type);
57 u32 obd_cksum_type_pack(const char *obd_name, enum cksum_types cksum_type);
59 static inline enum cksum_types obd_cksum_type_unpack(u32 o_flags)
61 switch (o_flags & OBD_FL_CKSUM_ALL) {
62 case OBD_FL_CKSUM_CRC32C:
63 return OBD_CKSUM_CRC32C;
64 case OBD_FL_CKSUM_CRC32:
65 return OBD_CKSUM_CRC32;
66 case OBD_FL_CKSUM_T10IP512:
67 return OBD_CKSUM_T10IP512;
68 case OBD_FL_CKSUM_T10IP4K:
69 return OBD_CKSUM_T10IP4K;
70 case OBD_FL_CKSUM_T10CRC512:
71 return OBD_CKSUM_T10CRC512;
72 case OBD_FL_CKSUM_T10CRC4K:
73 return OBD_CKSUM_T10CRC4K;
78 return OBD_CKSUM_ADLER;
81 /* Return a bitmask of the checksum types supported on this system.
82 * 1.8 supported ADLER it is base and not depend on hw
83 * Client uses all available local algos
85 static inline enum cksum_types obd_cksum_types_supported_client(void)
87 enum cksum_types ret = OBD_CKSUM_ADLER;
89 CDEBUG(D_INFO, "Crypto hash speed: crc %d, crc32c %d, adler %d\n",
90 cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32)),
91 cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32C)),
92 cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_ADLER)));
94 if (cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32C)) > 0)
95 ret |= OBD_CKSUM_CRC32C;
96 if (cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32)) > 0)
97 ret |= OBD_CKSUM_CRC32;
99 /* Client support all kinds of T10 checksum */
100 ret |= OBD_CKSUM_T10_ALL;
105 enum cksum_types obd_cksum_types_supported_server(const char *obd_name);
107 /* Select the best checksum algorithm among those supplied in the cksum_types
110 * Currently, calling cksum_type_pack() with a mask will return the fastest
111 * checksum type due to its benchmarking at libcfs module load.
112 * Caution is advised, however, since what is fastest on a single client may
113 * not be the fastest or most efficient algorithm on the server. */
115 enum cksum_types obd_cksum_type_select(const char *obd_name,
116 enum cksum_types cksum_types,
117 enum cksum_types preferred)
121 if (preferred & cksum_types)
125 * Server reporting a single T10 checksum type
126 * means the target actually supports T10-PI.
128 if (hweight32(cksum_types & OBD_CKSUM_T10_ALL) == 1)
129 return cksum_types & OBD_CKSUM_T10_ALL;
131 flag = obd_cksum_type_pack(obd_name, cksum_types);
133 return obd_cksum_type_unpack(flag);
136 /* Checksum algorithm names. Must be defined in the same order as the
137 * OBD_CKSUM_* flags. */
138 #define DECLARE_CKSUM_NAME const char *const cksum_name[] = {"crc32", "adler", \
139 "crc32c", "reserved", "t10ip512", "t10ip4K", "t10crc512", "t10crc4K"}
141 typedef __be16 (obd_dif_csum_fn) (void *, unsigned int);
143 __be16 obd_dif_crc_fn(void *data, unsigned int len);
144 __be16 obd_dif_ip_fn(void *data, unsigned int len);
145 int obd_page_dif_generate_buffer(const char *obd_name, struct page *page,
146 __u32 offset, __u32 length,
147 __be16 *guard_start, int guard_number,
148 int *used_number, int sector_size,
149 obd_dif_csum_fn *fn);
151 * If checksum type is one T10 checksum types, init the csum_fn and sector
152 * size. Otherwise, init them to NULL/zero.
154 static inline void obd_t10_cksum2dif(enum cksum_types cksum_type,
155 obd_dif_csum_fn **fn, int *sector_size)
160 #if IS_ENABLED(CONFIG_CRC_T10DIF)
161 switch (cksum_type) {
162 case OBD_CKSUM_T10IP512:
166 case OBD_CKSUM_T10IP4K:
170 case OBD_CKSUM_T10CRC512:
171 *fn = obd_dif_crc_fn;
174 case OBD_CKSUM_T10CRC4K:
175 *fn = obd_dif_crc_fn;
181 #endif /* CONFIG_CRC_T10DIF */
184 enum obd_t10_cksum_type {
185 OBD_T10_CKSUM_UNKNOWN = 0,
188 OBD_T10_CKSUM_CRC512,