Merge tag 'gcc-plugins-v4.9-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git...
[cascardo/linux.git] / drivers / staging / lustre / lustre / include / obd_cksum.h
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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 only,
8  * as published by the Free Software Foundation.
9  *
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).
15  *
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
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  */
26 /*
27  * This file is part of Lustre, http://www.lustre.org/
28  * Lustre is a trademark of Sun Microsystems, Inc.
29  */
30
31 #ifndef __OBD_CKSUM
32 #define __OBD_CKSUM
33 #include "../../include/linux/libcfs/libcfs.h"
34 #include "../../include/linux/libcfs/libcfs_crypto.h"
35 #include "lustre/lustre_idl.h"
36
37 static inline unsigned char cksum_obd2cfs(enum cksum_type cksum_type)
38 {
39         switch (cksum_type) {
40         case OBD_CKSUM_CRC32:
41                 return CFS_HASH_ALG_CRC32;
42         case OBD_CKSUM_ADLER:
43                 return CFS_HASH_ALG_ADLER32;
44         case OBD_CKSUM_CRC32C:
45                 return CFS_HASH_ALG_CRC32C;
46         default:
47                 CERROR("Unknown checksum type (%x)!!!\n", cksum_type);
48                 LBUG();
49         }
50         return 0;
51 }
52
53 /* The OBD_FL_CKSUM_* flags is packed into 5 bits of o_flags, since there can
54  * only be a single checksum type per RPC.
55  *
56  * The OBD_CHECKSUM_* type bits passed in ocd_cksum_types are a 32-bit bitmask
57  * since they need to represent the full range of checksum algorithms that
58  * both the client and server can understand.
59  *
60  * In case of an unsupported types/flags we fall back to ADLER
61  * because that is supported by all clients since 1.8
62  *
63  * In case multiple algorithms are supported the best one is used.
64  */
65 static inline u32 cksum_type_pack(enum cksum_type cksum_type)
66 {
67         unsigned int    performance = 0, tmp;
68         u32             flag = OBD_FL_CKSUM_ADLER;
69
70         if (cksum_type & OBD_CKSUM_CRC32) {
71                 tmp = cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32));
72                 if (tmp > performance) {
73                         performance = tmp;
74                         flag = OBD_FL_CKSUM_CRC32;
75                 }
76         }
77         if (cksum_type & OBD_CKSUM_CRC32C) {
78                 tmp = cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32C));
79                 if (tmp > performance) {
80                         performance = tmp;
81                         flag = OBD_FL_CKSUM_CRC32C;
82                 }
83         }
84         if (cksum_type & OBD_CKSUM_ADLER) {
85                 tmp = cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_ADLER));
86                 if (tmp > performance) {
87                         performance = tmp;
88                         flag = OBD_FL_CKSUM_ADLER;
89                 }
90         }
91         if (unlikely(cksum_type && !(cksum_type & (OBD_CKSUM_CRC32C |
92                                                    OBD_CKSUM_CRC32 |
93                                                    OBD_CKSUM_ADLER))))
94                 CWARN("unknown cksum type %x\n", cksum_type);
95
96         return flag;
97 }
98
99 static inline enum cksum_type cksum_type_unpack(u32 o_flags)
100 {
101         switch (o_flags & OBD_FL_CKSUM_ALL) {
102         case OBD_FL_CKSUM_CRC32C:
103                 return OBD_CKSUM_CRC32C;
104         case OBD_FL_CKSUM_CRC32:
105                 return OBD_CKSUM_CRC32;
106         default:
107                 break;
108         }
109
110         return OBD_CKSUM_ADLER;
111 }
112
113 /* Return a bitmask of the checksum types supported on this system.
114  * 1.8 supported ADLER it is base and not depend on hw
115  * Client uses all available local algos
116  */
117 static inline enum cksum_type cksum_types_supported_client(void)
118 {
119         enum cksum_type ret = OBD_CKSUM_ADLER;
120
121         CDEBUG(D_INFO, "Crypto hash speed: crc %d, crc32c %d, adler %d\n",
122                cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32)),
123                cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32C)),
124                cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_ADLER)));
125
126         if (cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32C)) > 0)
127                 ret |= OBD_CKSUM_CRC32C;
128         if (cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32)) > 0)
129                 ret |= OBD_CKSUM_CRC32;
130
131         return ret;
132 }
133
134 /* Select the best checksum algorithm among those supplied in the cksum_types
135  * input.
136  *
137  * Currently, calling cksum_type_pack() with a mask will return the fastest
138  * checksum type due to its benchmarking at libcfs module load.
139  * Caution is advised, however, since what is fastest on a single client may
140  * not be the fastest or most efficient algorithm on the server.
141  */
142 static inline enum cksum_type cksum_type_select(enum cksum_type cksum_types)
143 {
144         return cksum_type_unpack(cksum_type_pack(cksum_types));
145 }
146
147 /* Checksum algorithm names. Must be defined in the same order as the
148  * OBD_CKSUM_* flags.
149  */
150 #define DECLARE_CKSUM_NAME char *cksum_name[] = {"crc32", "adler", "crc32c"}
151
152 #endif /* __OBD_H */