Whamcloud - gitweb
LU-10467 ptlrpc: discard SVC_SIGNAL and related functions
[fs/lustre-release.git] / lustre / include / obd_cksum.h
index ef76b66..6e807d7 100644 (file)
@@ -1,6 +1,4 @@
-/* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
- * vim:expandtab:shiftwidth=8:tabstop=8:
- *
+/*
  * GPL HEADER START
  *
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * You should have received a copy of the GNU General Public License
  * version 2 along with this program; If not, see
- * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
- *
- * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
- * CA 95054 USA or visit www.sun.com if you need additional information or
- * have any questions.
+ * http://www.gnu.org/licenses/gpl-2.0.html
  *
  * GPL HEADER END
  */
 /*
  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
+ *
+ * Copyright (c) 2014, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
 
 #ifndef __OBD_CKSUM
 #define __OBD_CKSUM
+#include <libcfs/libcfs.h>
+#include <libcfs/libcfs_crypto.h>
+#include <uapi/linux/lustre/lustre_idl.h>
 
-#if defined(__linux__)
-#include <linux/obd_cksum.h>
-#elif defined(__APPLE__)
-#include <darwin/obd_chksum.h>
-#elif defined(__WINNT__)
-#include <winnt/obd_cksum.h>
-#else
-#error Unsupported operating system.
-#endif
-
-#include <lustre/lustre_idl.h>
-
-/*
- * Checksums
- */
-
-#ifndef HAVE_ARCH_CRC32
-/* crc32_le lifted from the Linux kernel, which had the following to say:
- *
- * This code is in the public domain; copyright abandoned.
- * Liability for non-performance of this code is limited to the amount
- * you paid for it.  Since it is distributed for free, your refund will
- * be very very small.  If it breaks, you get to keep both pieces.
- */
-#define CRCPOLY_LE 0xedb88320
-/**
- * crc32_le() - Calculate bitwise little-endian Ethernet AUTODIN II CRC32
- * \param crc  seed value for computation.  ~0 for Ethernet, sometimes 0 for
- *             other uses, or the previous crc32 value if computing incrementally.
- * \param p  - pointer to buffer over which CRC is run
- * \param len- length of buffer \a p
- */
-static inline __u32 crc32_le(__u32 crc, unsigned char const *p, size_t len)
-{
-        int i;
-        while (len--) {
-                crc ^= *p++;
-                for (i = 0; i < 8; i++)
-                        crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
-        }
-        return crc;
-}
-#endif
-#ifdef HAVE_ADLER
-/* Adler-32 is supported */
-#define CHECKSUM_ADLER OBD_CKSUM_ADLER
-#else
-#define CHECKSUM_ADLER 0
-#endif
+int obd_t10_cksum_speed(const char *obd_name,
+                       enum cksum_types cksum_type);
 
-#ifdef X86_FEATURE_XMM4_2
-/* Call Nehalem+ CRC32C harware acceleration instruction on individual bytes. */
-static inline __u32 crc32c_hw_byte(__u32 crc, unsigned char const *p,
-                                  size_t bytes)
+static inline unsigned char cksum_obd2cfs(enum cksum_types cksum_type)
 {
-        while (bytes--) {
-                __asm__ __volatile__ (
-                        ".byte 0xf2, 0xf, 0x38, 0xf0, 0xf1"
-                        : "=S"(crc)
-                        : "0"(crc), "c"(*p)
-                );
-                p++;
-        }
-
-        return crc;
+       switch (cksum_type) {
+       case OBD_CKSUM_CRC32:
+               return CFS_HASH_ALG_CRC32;
+       case OBD_CKSUM_ADLER:
+               return CFS_HASH_ALG_ADLER32;
+       case OBD_CKSUM_CRC32C:
+               return CFS_HASH_ALG_CRC32C;
+       default:
+               CERROR("Unknown checksum type (%x)!!!\n", cksum_type);
+               LBUG();
+       }
+       return 0;
 }
 
-#if BITS_PER_LONG > 32
-#define WORD_SHIFT 3
-#define WORD_MASK  7
-#define REX "0x48, "
-#else
-#define WORD_SHIFT 2
-#define WORD_MASK  3
-#define REX ""
-#endif
-
-/* Do we need to worry about unaligned input data here? */
-static inline __u32 crc32c_hw(__u32 crc, unsigned char const *p, size_t len)
-{
-        unsigned int words = len >> WORD_SHIFT;
-        unsigned int bytes = len &  WORD_MASK;
-        long *ptmp = (long *)p;
-
-        while (words--) {
-                __asm__ __volatile__(
-                        ".byte 0xf2, " REX "0xf, 0x38, 0xf1, 0xf1;"
-                        : "=S"(crc)
-                        : "0"(crc), "c"(*ptmp)
-                );
-                ptmp++;
-        }
-
-        if (bytes)
-                crc = crc32c_hw_byte(crc, (unsigned char *)ptmp, bytes);
-
-        return crc;
-}
-#else
-/* We should never call this unless the CPU has previously been detected to
- * support this instruction in the SSE4.2 feature set. b=23549  */
-static inline __u32 crc32c_hw(__u32 crc, unsigned char const *p,size_t len)
-{
-        LBUG();
-}
-#endif
+u32 obd_cksum_type_pack(const char *obd_name, enum cksum_types cksum_type);
 
-static inline __u32 init_checksum(cksum_type_t cksum_type)
+static inline enum cksum_types obd_cksum_type_unpack(u32 o_flags)
 {
-        switch(cksum_type) {
-        case OBD_CKSUM_CRC32C:
-                return ~0U;
-#ifdef HAVE_ADLER
-        case OBD_CKSUM_ADLER:
-                return 1U;
-#endif
-        case OBD_CKSUM_CRC32:
-                return ~0U;
-        default:
-                CERROR("Unknown checksum type (%x)!!!\n", cksum_type);
-                LBUG();
-        }
-        return 0;
+       switch (o_flags & OBD_FL_CKSUM_ALL) {
+       case OBD_FL_CKSUM_CRC32C:
+               return OBD_CKSUM_CRC32C;
+       case OBD_FL_CKSUM_CRC32:
+               return OBD_CKSUM_CRC32;
+       case OBD_FL_CKSUM_T10IP512:
+               return OBD_CKSUM_T10IP512;
+       case OBD_FL_CKSUM_T10IP4K:
+               return OBD_CKSUM_T10IP4K;
+       case OBD_FL_CKSUM_T10CRC512:
+               return OBD_CKSUM_T10CRC512;
+       case OBD_FL_CKSUM_T10CRC4K:
+               return OBD_CKSUM_T10CRC4K;
+       default:
+               break;
+       }
+
+       return OBD_CKSUM_ADLER;
 }
 
-static inline __u32 compute_checksum(__u32 cksum, unsigned char const *p,
-                                     size_t len, cksum_type_t cksum_type)
+/* Return a bitmask of the checksum types supported on this system.
+ * 1.8 supported ADLER it is base and not depend on hw
+ * Client uses all available local algos
+ */
+static inline enum cksum_types obd_cksum_types_supported_client(void)
 {
-        switch(cksum_type) {
-        case OBD_CKSUM_CRC32C:
-                return crc32c_hw(cksum, p, len);
-#ifdef HAVE_ADLER
-        case OBD_CKSUM_ADLER:
-                return adler32(cksum, p, len);
-#endif
-        case OBD_CKSUM_CRC32:
-                return crc32_le(cksum, p, len);
-        default:
-                CERROR("Unknown checksum type (%x)!!!\n", cksum_type);
-                LBUG();
-        }
-        return 0;
-}
+       enum cksum_types ret = OBD_CKSUM_ADLER;
 
-/* The OBD_FL_CKSUM_* flags is packed into 5 bits of o_flags, since there can
- * only be a single checksum type per RPC.
- *
- * The OBD_CHECKSUM_* type bits passed in ocd_cksum_types are a 32-bit bitmask
- * since they need to represent the full range of checksum algorithms that
- * both the client and server can understand.
- *
- * In case of an unsupported types/flags we fall back to CRC32 (even though
- * it isn't very fast) because that is supported by all clients
- * checksums, since 1.6.5 (or earlier via patches).
- *
- * These flags should be listed in order of descending performance, so that
- * in case multiple algorithms are supported the best one is used. */
-static inline obd_flag cksum_type_pack(cksum_type_t cksum_type)
-{
-        if (cksum_type & OBD_CKSUM_CRC32C)
-                return OBD_FL_CKSUM_CRC32C;
-#ifdef HAVE_ADLER
-        if (cksum_type & OBD_CKSUM_ADLER)
-                return OBD_FL_CKSUM_ADLER;
-#endif
-        if (unlikely(cksum_type && !(cksum_type & OBD_CKSUM_CRC32)))
-                CWARN("unknown cksum type %x\n", cksum_type);
+       CDEBUG(D_INFO, "Crypto hash speed: crc %d, crc32c %d, adler %d\n",
+              cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32)),
+              cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32C)),
+              cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_ADLER)));
 
-        return OBD_FL_CKSUM_CRC32;
-}
+       if (cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32C)) > 0)
+               ret |= OBD_CKSUM_CRC32C;
+       if (cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32)) > 0)
+               ret |= OBD_CKSUM_CRC32;
 
-static inline cksum_type_t cksum_type_unpack(obd_flag o_flags)
-{
-        switch (o_flags & OBD_FL_CKSUM_ALL) {
-        case OBD_FL_CKSUM_CRC32C:
-                return OBD_CKSUM_CRC32C;
-        case OBD_FL_CKSUM_ADLER:
-#ifdef HAVE_ADLER
-                return OBD_CKSUM_ADLER;
-#else
-                CWARN("checksum type is set to adler32, but adler32 is not "
-                      "supported (%x)\n", o_flags);
-                break;
-#endif
-        default:
-                break;
-        }
+       /* Client support all kinds of T10 checksum */
+       ret |= OBD_CKSUM_T10_ALL;
 
-        /* 1.6.4- only supported CRC32 and didn't set o_flags */
-        return OBD_CKSUM_CRC32;
+       return ret;
 }
 
-/* Return a bitmask of the checksum types supported on this system.
- *
- * CRC32 is a required for compatibility (starting with 1.6.5),
- * after which we could move to Adler as the base checksum type.
- *
- * If hardware crc32c support is not available, it is slower than Adler,
- * so don't include it, even if it could be emulated in software. b=23549 */
-static inline cksum_type_t cksum_types_supported(void)
-{
-        cksum_type_t ret = OBD_CKSUM_CRC32;
-
-#ifdef X86_FEATURE_XMM4_2
-        if (cpu_has_xmm4_2)
-                ret |= OBD_CKSUM_CRC32C;
-#endif
-#ifdef HAVE_ADLER
-        ret |= OBD_CKSUM_ADLER;
-#endif
-        return ret;
-}
+enum cksum_types obd_cksum_types_supported_server(const char *obd_name);
 
 /* Select the best checksum algorithm among those supplied in the cksum_types
  * input.
  *
  * Currently, calling cksum_type_pack() with a mask will return the fastest
- * checksum type due to its ordering, but in the future we might want to
- * determine this based on benchmarking the different algorithms quickly.
+ * checksum type due to its benchmarking at libcfs module load.
  * Caution is advised, however, since what is fastest on a single client may
  * not be the fastest or most efficient algorithm on the server.  */
-static inline cksum_type_t cksum_type_select(cksum_type_t cksum_types)
+static inline enum cksum_types
+obd_cksum_type_select(const char *obd_name, enum cksum_types cksum_types)
 {
-        return cksum_type_unpack(cksum_type_pack(cksum_types));
+       u32 flag = obd_cksum_type_pack(obd_name, cksum_types);
+
+       return obd_cksum_type_unpack(flag);
 }
 
 /* Checksum algorithm names. Must be defined in the same order as the
  * OBD_CKSUM_* flags. */
-#define DECLARE_CKSUM_NAME char *cksum_name[] = {"crc32", "adler", "crc32c"}
+#define DECLARE_CKSUM_NAME const char *cksum_name[] = {"crc32", "adler", \
+       "crc32c", "reserved", "t10ip512", "t10ip4K", "t10crc512", "t10crc4K"}
+
+typedef __u16 (obd_dif_csum_fn) (void *, unsigned int);
+
+__u16 obd_dif_crc_fn(void *data, unsigned int len);
+__u16 obd_dif_ip_fn(void *data, unsigned int len);
+int obd_page_dif_generate_buffer(const char *obd_name, struct page *page,
+                                __u32 offset, __u32 length,
+                                __u16 *guard_start, int guard_number,
+                                int *used_number, int sector_size,
+                                obd_dif_csum_fn *fn);
+/*
+ * If checksum type is one T10 checksum types, init the csum_fn and sector
+ * size. Otherwise, init them to NULL/zero.
+ */
+static inline void obd_t10_cksum2dif(enum cksum_types cksum_type,
+                                    obd_dif_csum_fn **fn, int *sector_size)
+{
+       *fn = NULL;
+       *sector_size = 0;
+
+#if IS_ENABLED(CONFIG_CRC_T10DIF)
+       switch (cksum_type) {
+       case OBD_CKSUM_T10IP512:
+               *fn = obd_dif_ip_fn;
+               *sector_size = 512;
+               break;
+       case OBD_CKSUM_T10IP4K:
+               *fn = obd_dif_ip_fn;
+               *sector_size = 4096;
+               break;
+       case OBD_CKSUM_T10CRC512:
+               *fn = obd_dif_crc_fn;
+               *sector_size = 512;
+               break;
+       case OBD_CKSUM_T10CRC4K:
+               *fn = obd_dif_crc_fn;
+               *sector_size = 4096;
+               break;
+       default:
+               break;
+       }
+#endif /* CONFIG_CRC_T10DIF */
+}
+
+enum obd_t10_cksum_type {
+       OBD_T10_CKSUM_UNKNOWN = 0,
+       OBD_T10_CKSUM_IP512,
+       OBD_T10_CKSUM_IP4K,
+       OBD_T10_CKSUM_CRC512,
+       OBD_T10_CKSUM_CRC4K,
+       OBD_T10_CKSUM_MAX
+};
 
 #endif /* __OBD_H */