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