iba.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
  2. /*
  3. * Copyright (c) 2020, Mellanox Technologies inc. All rights reserved.
  4. */
  5. #ifndef _IBA_DEFS_H_
  6. #define _IBA_DEFS_H_
  7. #include <linux/kernel.h>
  8. #include <linux/bitfield.h>
  9. #include <asm/unaligned.h>
  10. static inline u32 _iba_get8(const u8 *ptr)
  11. {
  12. return *ptr;
  13. }
  14. static inline void _iba_set8(u8 *ptr, u32 mask, u32 prep_value)
  15. {
  16. *ptr = (*ptr & ~mask) | prep_value;
  17. }
  18. static inline u16 _iba_get16(const __be16 *ptr)
  19. {
  20. return be16_to_cpu(*ptr);
  21. }
  22. static inline void _iba_set16(__be16 *ptr, u16 mask, u16 prep_value)
  23. {
  24. *ptr = cpu_to_be16((be16_to_cpu(*ptr) & ~mask) | prep_value);
  25. }
  26. static inline u32 _iba_get32(const __be32 *ptr)
  27. {
  28. return be32_to_cpu(*ptr);
  29. }
  30. static inline void _iba_set32(__be32 *ptr, u32 mask, u32 prep_value)
  31. {
  32. *ptr = cpu_to_be32((be32_to_cpu(*ptr) & ~mask) | prep_value);
  33. }
  34. static inline u64 _iba_get64(const __be64 *ptr)
  35. {
  36. /*
  37. * The mads are constructed so that 32 bit and smaller are naturally
  38. * aligned, everything larger has a max alignment of 4 bytes.
  39. */
  40. return be64_to_cpu(get_unaligned(ptr));
  41. }
  42. static inline void _iba_set64(__be64 *ptr, u64 mask, u64 prep_value)
  43. {
  44. put_unaligned(cpu_to_be64((_iba_get64(ptr) & ~mask) | prep_value), ptr);
  45. }
  46. #define _IBA_SET(field_struct, field_offset, field_mask, num_bits, ptr, value) \
  47. ({ \
  48. field_struct *_ptr = ptr; \
  49. _iba_set##num_bits((void *)_ptr + (field_offset), field_mask, \
  50. FIELD_PREP(field_mask, value)); \
  51. })
  52. #define IBA_SET(field, ptr, value) _IBA_SET(field, ptr, value)
  53. #define _IBA_GET_MEM_PTR(field_struct, field_offset, type, num_bits, ptr) \
  54. ({ \
  55. field_struct *_ptr = ptr; \
  56. (type *)((void *)_ptr + (field_offset)); \
  57. })
  58. #define IBA_GET_MEM_PTR(field, ptr) _IBA_GET_MEM_PTR(field, ptr)
  59. /* FIXME: A set should always set the entire field, meaning we should zero the trailing bytes */
  60. #define _IBA_SET_MEM(field_struct, field_offset, type, num_bits, ptr, in, \
  61. bytes) \
  62. ({ \
  63. const type *_in_ptr = in; \
  64. WARN_ON(bytes * 8 > num_bits); \
  65. if (in && bytes) \
  66. memcpy(_IBA_GET_MEM_PTR(field_struct, field_offset, \
  67. type, num_bits, ptr), \
  68. _in_ptr, bytes); \
  69. })
  70. #define IBA_SET_MEM(field, ptr, in, bytes) _IBA_SET_MEM(field, ptr, in, bytes)
  71. #define _IBA_GET(field_struct, field_offset, field_mask, num_bits, ptr) \
  72. ({ \
  73. const field_struct *_ptr = ptr; \
  74. (u##num_bits) FIELD_GET( \
  75. field_mask, _iba_get##num_bits((const void *)_ptr + \
  76. (field_offset))); \
  77. })
  78. #define IBA_GET(field, ptr) _IBA_GET(field, ptr)
  79. #define _IBA_GET_MEM(field_struct, field_offset, type, num_bits, ptr, out, \
  80. bytes) \
  81. ({ \
  82. type *_out_ptr = out; \
  83. WARN_ON(bytes * 8 > num_bits); \
  84. if (out && bytes) \
  85. memcpy(_out_ptr, \
  86. _IBA_GET_MEM_PTR(field_struct, field_offset, \
  87. type, num_bits, ptr), \
  88. bytes); \
  89. })
  90. #define IBA_GET_MEM(field, ptr, out, bytes) _IBA_GET_MEM(field, ptr, out, bytes)
  91. /*
  92. * The generated list becomes the parameters to the macros, the order is:
  93. * - struct this applies to
  94. * - starting offset of the max
  95. * - GENMASK or GENMASK_ULL in CPU order
  96. * - The width of data the mask operations should work on, in bits
  97. */
  98. /*
  99. * Extraction using a tabular description like table 106. bit_offset is from
  100. * the Byte[Bit] notation.
  101. */
  102. #define IBA_FIELD_BLOC(field_struct, byte_offset, bit_offset, num_bits) \
  103. field_struct, byte_offset, \
  104. GENMASK(7 - (bit_offset), 7 - (bit_offset) - (num_bits - 1)), \
  105. 8
  106. #define IBA_FIELD8_LOC(field_struct, byte_offset, num_bits) \
  107. IBA_FIELD_BLOC(field_struct, byte_offset, 0, num_bits)
  108. #define IBA_FIELD16_LOC(field_struct, byte_offset, num_bits) \
  109. field_struct, (byte_offset)&0xFFFE, \
  110. GENMASK(15 - (((byte_offset) % 2) * 8), \
  111. 15 - (((byte_offset) % 2) * 8) - (num_bits - 1)), \
  112. 16
  113. #define IBA_FIELD32_LOC(field_struct, byte_offset, num_bits) \
  114. field_struct, (byte_offset)&0xFFFC, \
  115. GENMASK(31 - (((byte_offset) % 4) * 8), \
  116. 31 - (((byte_offset) % 4) * 8) - (num_bits - 1)), \
  117. 32
  118. #define IBA_FIELD64_LOC(field_struct, byte_offset) \
  119. field_struct, byte_offset, GENMASK_ULL(63, 0), 64
  120. /*
  121. * In IBTA spec, everything that is more than 64bits is multiple
  122. * of bytes without leftover bits.
  123. */
  124. #define IBA_FIELD_MLOC(field_struct, byte_offset, num_bits, type) \
  125. field_struct, byte_offset, type, num_bits
  126. #endif /* _IBA_DEFS_H_ */