Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
0002 /*
0003  * Copyright (c) 2020, Mellanox Technologies inc.  All rights reserved.
0004  */
0005 #ifndef _IBA_DEFS_H_
0006 #define _IBA_DEFS_H_
0007 
0008 #include <linux/kernel.h>
0009 #include <linux/bitfield.h>
0010 #include <asm/unaligned.h>
0011 
0012 static inline u32 _iba_get8(const u8 *ptr)
0013 {
0014     return *ptr;
0015 }
0016 
0017 static inline void _iba_set8(u8 *ptr, u32 mask, u32 prep_value)
0018 {
0019     *ptr = (*ptr & ~mask) | prep_value;
0020 }
0021 
0022 static inline u16 _iba_get16(const __be16 *ptr)
0023 {
0024     return be16_to_cpu(*ptr);
0025 }
0026 
0027 static inline void _iba_set16(__be16 *ptr, u16 mask, u16 prep_value)
0028 {
0029     *ptr = cpu_to_be16((be16_to_cpu(*ptr) & ~mask) | prep_value);
0030 }
0031 
0032 static inline u32 _iba_get32(const __be32 *ptr)
0033 {
0034     return be32_to_cpu(*ptr);
0035 }
0036 
0037 static inline void _iba_set32(__be32 *ptr, u32 mask, u32 prep_value)
0038 {
0039     *ptr = cpu_to_be32((be32_to_cpu(*ptr) & ~mask) | prep_value);
0040 }
0041 
0042 static inline u64 _iba_get64(const __be64 *ptr)
0043 {
0044     /*
0045      * The mads are constructed so that 32 bit and smaller are naturally
0046      * aligned, everything larger has a max alignment of 4 bytes.
0047      */
0048     return be64_to_cpu(get_unaligned(ptr));
0049 }
0050 
0051 static inline void _iba_set64(__be64 *ptr, u64 mask, u64 prep_value)
0052 {
0053     put_unaligned(cpu_to_be64((_iba_get64(ptr) & ~mask) | prep_value), ptr);
0054 }
0055 
0056 #define _IBA_SET(field_struct, field_offset, field_mask, num_bits, ptr, value) \
0057     ({                                                                     \
0058         field_struct *_ptr = ptr;                                      \
0059         _iba_set##num_bits((void *)_ptr + (field_offset), field_mask,  \
0060                    FIELD_PREP(field_mask, value));             \
0061     })
0062 #define IBA_SET(field, ptr, value) _IBA_SET(field, ptr, value)
0063 
0064 #define _IBA_GET_MEM_PTR(field_struct, field_offset, type, num_bits, ptr)      \
0065     ({                                                                     \
0066         field_struct *_ptr = ptr;                                      \
0067         (type *)((void *)_ptr + (field_offset));                       \
0068     })
0069 #define IBA_GET_MEM_PTR(field, ptr) _IBA_GET_MEM_PTR(field, ptr)
0070 
0071 /* FIXME: A set should always set the entire field, meaning we should zero the trailing bytes */
0072 #define _IBA_SET_MEM(field_struct, field_offset, type, num_bits, ptr, in,      \
0073              bytes)                                                    \
0074     ({                                                                     \
0075         const type *_in_ptr = in;                                      \
0076         WARN_ON(bytes * 8 > num_bits);                                 \
0077         if (in && bytes)                                               \
0078             memcpy(_IBA_GET_MEM_PTR(field_struct, field_offset,    \
0079                         type, num_bits, ptr),          \
0080                    _in_ptr, bytes);                                \
0081     })
0082 #define IBA_SET_MEM(field, ptr, in, bytes) _IBA_SET_MEM(field, ptr, in, bytes)
0083 
0084 #define _IBA_GET(field_struct, field_offset, field_mask, num_bits, ptr)        \
0085     ({                                                                     \
0086         const field_struct *_ptr = ptr;                                \
0087         (u##num_bits) FIELD_GET(                                       \
0088             field_mask, _iba_get##num_bits((const void *)_ptr +    \
0089                                (field_offset)));       \
0090     })
0091 #define IBA_GET(field, ptr) _IBA_GET(field, ptr)
0092 
0093 #define _IBA_GET_MEM(field_struct, field_offset, type, num_bits, ptr, out,     \
0094              bytes)                                                    \
0095     ({                                                                     \
0096         type *_out_ptr = out;                                          \
0097         WARN_ON(bytes * 8 > num_bits);                                 \
0098         if (out && bytes)                                              \
0099             memcpy(_out_ptr,                                       \
0100                    _IBA_GET_MEM_PTR(field_struct, field_offset,    \
0101                         type, num_bits, ptr),          \
0102                    bytes);                                         \
0103     })
0104 #define IBA_GET_MEM(field, ptr, out, bytes) _IBA_GET_MEM(field, ptr, out, bytes)
0105 
0106 /*
0107  * The generated list becomes the parameters to the macros, the order is:
0108  *  - struct this applies to
0109  *  - starting offset of the max
0110  *  - GENMASK or GENMASK_ULL in CPU order
0111  *  - The width of data the mask operations should work on, in bits
0112  */
0113 
0114 /*
0115  * Extraction using a tabular description like table 106. bit_offset is from
0116  * the Byte[Bit] notation.
0117  */
0118 #define IBA_FIELD_BLOC(field_struct, byte_offset, bit_offset, num_bits)        \
0119     field_struct, byte_offset,                                             \
0120         GENMASK(7 - (bit_offset), 7 - (bit_offset) - (num_bits - 1)),  \
0121         8
0122 #define IBA_FIELD8_LOC(field_struct, byte_offset, num_bits)                    \
0123     IBA_FIELD_BLOC(field_struct, byte_offset, 0, num_bits)
0124 
0125 #define IBA_FIELD16_LOC(field_struct, byte_offset, num_bits)                   \
0126     field_struct, (byte_offset)&0xFFFE,                                    \
0127         GENMASK(15 - (((byte_offset) % 2) * 8),                        \
0128             15 - (((byte_offset) % 2) * 8) - (num_bits - 1)),      \
0129         16
0130 
0131 #define IBA_FIELD32_LOC(field_struct, byte_offset, num_bits)                   \
0132     field_struct, (byte_offset)&0xFFFC,                                    \
0133         GENMASK(31 - (((byte_offset) % 4) * 8),                        \
0134             31 - (((byte_offset) % 4) * 8) - (num_bits - 1)),      \
0135         32
0136 
0137 #define IBA_FIELD64_LOC(field_struct, byte_offset)                             \
0138     field_struct, byte_offset, GENMASK_ULL(63, 0), 64
0139 /*
0140  * In IBTA spec, everything that is more than 64bits is multiple
0141  * of bytes without leftover bits.
0142  */
0143 #define IBA_FIELD_MLOC(field_struct, byte_offset, num_bits, type)              \
0144     field_struct, byte_offset, type, num_bits
0145 
0146 #endif /* _IBA_DEFS_H_ */