18d59ecb2SHans Petter Selasky /*-
28d59ecb2SHans Petter Selasky * Copyright (c) 2010 Isilon Systems, Inc.
38d59ecb2SHans Petter Selasky * Copyright (c) 2010 iX Systems, Inc.
48d59ecb2SHans Petter Selasky * Copyright (c) 2010 Panasas, Inc.
57652bc32SHans Petter Selasky * Copyright (c) 2013-2016 Mellanox Technologies, Ltd.
68d59ecb2SHans Petter Selasky * All rights reserved.
78d59ecb2SHans Petter Selasky *
88d59ecb2SHans Petter Selasky * Redistribution and use in source and binary forms, with or without
98d59ecb2SHans Petter Selasky * modification, are permitted provided that the following conditions
108d59ecb2SHans Petter Selasky * are met:
118d59ecb2SHans Petter Selasky * 1. Redistributions of source code must retain the above copyright
128d59ecb2SHans Petter Selasky * notice unmodified, this list of conditions, and the following
138d59ecb2SHans Petter Selasky * disclaimer.
148d59ecb2SHans Petter Selasky * 2. Redistributions in binary form must reproduce the above copyright
158d59ecb2SHans Petter Selasky * notice, this list of conditions and the following disclaimer in the
168d59ecb2SHans Petter Selasky * documentation and/or other materials provided with the distribution.
178d59ecb2SHans Petter Selasky *
188d59ecb2SHans Petter Selasky * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
198d59ecb2SHans Petter Selasky * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
208d59ecb2SHans Petter Selasky * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
218d59ecb2SHans Petter Selasky * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
228d59ecb2SHans Petter Selasky * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
238d59ecb2SHans Petter Selasky * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
248d59ecb2SHans Petter Selasky * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
258d59ecb2SHans Petter Selasky * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
268d59ecb2SHans Petter Selasky * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
278d59ecb2SHans Petter Selasky * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
288d59ecb2SHans Petter Selasky */
29*307f78f3SVladimir Kondratyev #ifndef _LINUXKPI_ASM_BYTEORDER_H_
30*307f78f3SVladimir Kondratyev #define _LINUXKPI_ASM_BYTEORDER_H_
318d59ecb2SHans Petter Selasky
328d59ecb2SHans Petter Selasky #include <sys/types.h>
338d59ecb2SHans Petter Selasky #include <sys/endian.h>
348d59ecb2SHans Petter Selasky #include <asm/types.h>
358d59ecb2SHans Petter Selasky
368d59ecb2SHans Petter Selasky #if BYTE_ORDER == LITTLE_ENDIAN
378d59ecb2SHans Petter Selasky #define __LITTLE_ENDIAN
388d59ecb2SHans Petter Selasky #else
398d59ecb2SHans Petter Selasky #define __BIG_ENDIAN
408d59ecb2SHans Petter Selasky #endif
418d59ecb2SHans Petter Selasky
425aeeab54SBjoern A. Zeeb #define __cpu_to_le64(x) htole64(x)
435aeeab54SBjoern A. Zeeb #define cpu_to_le64(x) __cpu_to_le64(x)
445aeeab54SBjoern A. Zeeb #define __le64_to_cpu(x) le64toh(x)
455aeeab54SBjoern A. Zeeb #define le64_to_cpu(x) __le64_to_cpu(x)
465aeeab54SBjoern A. Zeeb #define __cpu_to_le32(x) htole32(x)
475aeeab54SBjoern A. Zeeb #define cpu_to_le32(x) __cpu_to_le32(x)
485aeeab54SBjoern A. Zeeb #define __le32_to_cpu(x) le32toh(x)
495aeeab54SBjoern A. Zeeb #define le32_to_cpu(x) __le32_to_cpu(x)
505aeeab54SBjoern A. Zeeb #define __cpu_to_le16(x) htole16(x)
515aeeab54SBjoern A. Zeeb #define cpu_to_le16(x) __cpu_to_le16(x)
525aeeab54SBjoern A. Zeeb #define __le16_to_cpu(x) le16toh(x)
535aeeab54SBjoern A. Zeeb #define le16_to_cpu(x) __le16_to_cpu(x)
545aeeab54SBjoern A. Zeeb #define __cpu_to_be64(x) htobe64(x)
555aeeab54SBjoern A. Zeeb #define cpu_to_be64(x) __cpu_to_be64(x)
565aeeab54SBjoern A. Zeeb #define __be64_to_cpu(x) be64toh(x)
575aeeab54SBjoern A. Zeeb #define be64_to_cpu(x) __be64_to_cpu(x)
585aeeab54SBjoern A. Zeeb #define __cpu_to_be32(x) htobe32(x)
595aeeab54SBjoern A. Zeeb #define cpu_to_be32(x) __cpu_to_be32(x)
605aeeab54SBjoern A. Zeeb #define __be32_to_cpu(x) be32toh(x)
615aeeab54SBjoern A. Zeeb #define be32_to_cpu(x) __be32_to_cpu(x)
625aeeab54SBjoern A. Zeeb #define __cpu_to_be16(x) htobe16(x)
635aeeab54SBjoern A. Zeeb #define cpu_to_be16(x) __cpu_to_be16(x)
647652bc32SHans Petter Selasky #define __be16_to_cpu(x) be16toh(x)
655aeeab54SBjoern A. Zeeb #define be16_to_cpu(x) __be16_to_cpu(x)
668d59ecb2SHans Petter Selasky
675aeeab54SBjoern A. Zeeb #define __cpu_to_le64p(x) htole64(*((const uint64_t *)(x)))
685aeeab54SBjoern A. Zeeb #define cpu_to_le64p(x) __cpu_to_le64p(x)
695aeeab54SBjoern A. Zeeb #define __le64_to_cpup(x) le64toh(*((const uint64_t *)(x)))
705aeeab54SBjoern A. Zeeb #define le64_to_cpup(x) __le64_to_cpup(x)
715aeeab54SBjoern A. Zeeb #define __cpu_to_le32p(x) htole32(*((const uint32_t *)(x)))
725aeeab54SBjoern A. Zeeb #define cpu_to_le32p(x) __cpu_to_le32p(x)
735aeeab54SBjoern A. Zeeb #define __le32_to_cpup(x) le32toh(*((const uint32_t *)(x)))
745aeeab54SBjoern A. Zeeb #define le32_to_cpup(x) __le32_to_cpup(x)
755aeeab54SBjoern A. Zeeb #define __cpu_to_le16p(x) htole16(*((const uint16_t *)(x)))
765aeeab54SBjoern A. Zeeb #define cpu_to_le16p(x) __cpu_to_le16p(x)
775aeeab54SBjoern A. Zeeb #define __le16_to_cpup(x) le16toh(*((const uint16_t *)(x)))
785aeeab54SBjoern A. Zeeb #define le16_to_cpup(x) __le16_to_cpup(x)
795aeeab54SBjoern A. Zeeb #define __cpu_to_be64p(x) htobe64(*((const uint64_t *)(x)))
805aeeab54SBjoern A. Zeeb #define cpu_to_be64p(x) __cpu_to_be64p(x)
815aeeab54SBjoern A. Zeeb #define __be64_to_cpup(x) be64toh(*((const uint64_t *)(x)))
825aeeab54SBjoern A. Zeeb #define be64_to_cpup(x) __be64_to_cpup(x)
835aeeab54SBjoern A. Zeeb #define __cpu_to_be32p(x) htobe32(*((const uint32_t *)(x)))
845aeeab54SBjoern A. Zeeb #define cpu_to_be32p(x) __cpu_to_be32p(x)
855aeeab54SBjoern A. Zeeb #define __be32_to_cpup(x) be32toh(*((const uint32_t *)(x)))
865aeeab54SBjoern A. Zeeb #define be32_to_cpup(x) __be32_to_cpup(x)
875aeeab54SBjoern A. Zeeb #define __cpu_to_be16p(x) htobe16(*((const uint16_t *)(x)))
885aeeab54SBjoern A. Zeeb #define cpu_to_be16p(x) __cpu_to_be16p(x)
895aeeab54SBjoern A. Zeeb #define __be16_to_cpup(x) be16toh(*((const uint16_t *)(x)))
905aeeab54SBjoern A. Zeeb #define be16_to_cpup(x) __be16_to_cpup(x)
918d59ecb2SHans Petter Selasky
925aeeab54SBjoern A. Zeeb
935aeeab54SBjoern A. Zeeb #define __cpu_to_le64s(x) do { *((uint64_t *)(x)) = cpu_to_le64p((x)); } while (0)
945aeeab54SBjoern A. Zeeb #define cpu_to_le64s(x) __cpu_to_le64s(x)
955aeeab54SBjoern A. Zeeb #define __le64_to_cpus(x) do { *((uint64_t *)(x)) = le64_to_cpup((x)); } while (0)
965aeeab54SBjoern A. Zeeb #define le64_to_cpus(x) __le64_to_cpus(x)
975aeeab54SBjoern A. Zeeb #define __cpu_to_le32s(x) do { *((uint32_t *)(x)) = cpu_to_le32p((x)); } while (0)
985aeeab54SBjoern A. Zeeb #define cpu_to_le32s(x) __cpu_to_le32s(x)
995aeeab54SBjoern A. Zeeb #define __le32_to_cpus(x) do { *((uint32_t *)(x)) = le32_to_cpup((x)); } while (0)
1005aeeab54SBjoern A. Zeeb #define le32_to_cpus(x) __le32_to_cpus(x)
1015aeeab54SBjoern A. Zeeb #define __cpu_to_le16s(x) do { *((uint16_t *)(x)) = cpu_to_le16p((x)); } while (0)
1025aeeab54SBjoern A. Zeeb #define cpu_to_le16s(x) __cpu_to_le16s(x)
1035aeeab54SBjoern A. Zeeb #define __le16_to_cpus(x) do { *((uint16_t *)(x)) = le16_to_cpup((x)); } while (0)
1045aeeab54SBjoern A. Zeeb #define le16_to_cpus(x) __le16_to_cpus(x)
1055aeeab54SBjoern A. Zeeb #define __cpu_to_be64s(x) do { *((uint64_t *)(x)) = cpu_to_be64p((x)); } while (0)
1065aeeab54SBjoern A. Zeeb #define cpu_to_be64s(x) __cpu_to_be64s(x)
1075aeeab54SBjoern A. Zeeb #define __be64_to_cpus(x) do { *((uint64_t *)(x)) = be64_to_cpup((x)); } while (0)
1085aeeab54SBjoern A. Zeeb #define be64_to_cpus(x) __be64_to_cpus(x)
1095aeeab54SBjoern A. Zeeb #define __cpu_to_be32s(x) do { *((uint32_t *)(x)) = cpu_to_be32p((x)); } while (0)
1105aeeab54SBjoern A. Zeeb #define cpu_to_be32s(x) __cpu_to_be32s(x)
1115aeeab54SBjoern A. Zeeb #define __be32_to_cpus(x) do { *((uint32_t *)(x)) = be32_to_cpup((x)); } while (0)
1125aeeab54SBjoern A. Zeeb #define be32_to_cpus(x) __be32_to_cpus(x)
1135aeeab54SBjoern A. Zeeb #define __cpu_to_be16s(x) do { *((uint16_t *)(x)) = cpu_to_be16p((x)); } while (0)
1145aeeab54SBjoern A. Zeeb #define cpu_to_be16s(x) __cpu_to_be16s(x)
1155aeeab54SBjoern A. Zeeb #define __be16_to_cpus(x) do { *((uint16_t *)(x)) = be16_to_cpup((x)); } while (0)
1165aeeab54SBjoern A. Zeeb #define be16_to_cpus(x) __be16_to_cpus(x)
1178d59ecb2SHans Petter Selasky
1187652bc32SHans Petter Selasky #define swab16(x) bswap16(x)
1197652bc32SHans Petter Selasky #define swab32(x) bswap32(x)
1207652bc32SHans Petter Selasky #define swab64(x) bswap64(x)
1218d59ecb2SHans Petter Selasky
1228d59ecb2SHans Petter Selasky static inline void
be64_add_cpu(uint64_t * var,uint64_t val)1235aeeab54SBjoern A. Zeeb be64_add_cpu(uint64_t *var, uint64_t val)
1245aeeab54SBjoern A. Zeeb {
1255aeeab54SBjoern A. Zeeb *var = cpu_to_be64(be64_to_cpu(*var) + val);
1265aeeab54SBjoern A. Zeeb }
1275aeeab54SBjoern A. Zeeb
1285aeeab54SBjoern A. Zeeb static inline void
be32_add_cpu(uint32_t * var,uint32_t val)1295aeeab54SBjoern A. Zeeb be32_add_cpu(uint32_t *var, uint32_t val)
1305aeeab54SBjoern A. Zeeb {
1315aeeab54SBjoern A. Zeeb *var = cpu_to_be32(be32_to_cpu(*var) + val);
1325aeeab54SBjoern A. Zeeb }
1335aeeab54SBjoern A. Zeeb
1345aeeab54SBjoern A. Zeeb static inline void
be16_add_cpu(uint16_t * var,uint16_t val)1358e7baabcSHans Petter Selasky be16_add_cpu(uint16_t *var, uint16_t val)
1368d59ecb2SHans Petter Selasky {
1378d59ecb2SHans Petter Selasky *var = cpu_to_be16(be16_to_cpu(*var) + val);
1388d59ecb2SHans Petter Selasky }
1398d59ecb2SHans Petter Selasky
1405aeeab54SBjoern A. Zeeb static __inline void
le64_add_cpu(uint64_t * var,uint64_t val)1415aeeab54SBjoern A. Zeeb le64_add_cpu(uint64_t *var, uint64_t val)
1425aeeab54SBjoern A. Zeeb {
1435aeeab54SBjoern A. Zeeb *var = cpu_to_le64(le64_to_cpu(*var) + val);
1445aeeab54SBjoern A. Zeeb }
1455aeeab54SBjoern A. Zeeb
1465aeeab54SBjoern A. Zeeb static __inline void
le32_add_cpu(uint32_t * var,uint32_t val)1475aeeab54SBjoern A. Zeeb le32_add_cpu(uint32_t *var, uint32_t val)
1485aeeab54SBjoern A. Zeeb {
1495aeeab54SBjoern A. Zeeb *var = cpu_to_le32(le32_to_cpu(*var) + val);
1505aeeab54SBjoern A. Zeeb }
1515aeeab54SBjoern A. Zeeb
1525aeeab54SBjoern A. Zeeb static inline void
le16_add_cpu(uint16_t * var,uint16_t val)1535aeeab54SBjoern A. Zeeb le16_add_cpu(uint16_t *var, uint16_t val)
1545aeeab54SBjoern A. Zeeb {
1555aeeab54SBjoern A. Zeeb *var = cpu_to_le16(le16_to_cpu(*var) + val);
1565aeeab54SBjoern A. Zeeb }
1575aeeab54SBjoern A. Zeeb
158*307f78f3SVladimir Kondratyev #endif /* _LINUXKPI_ASM_BYTEORDER_H_ */
159