ib_pack.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
  2. /*
  3. * Copyright (c) 2004 Topspin Corporation. All rights reserved.
  4. */
  5. #ifndef IB_PACK_H
  6. #define IB_PACK_H
  7. #include <rdma/ib_verbs.h>
  8. #include <uapi/linux/if_ether.h>
  9. enum {
  10. IB_LRH_BYTES = 8,
  11. IB_ETH_BYTES = 14,
  12. IB_VLAN_BYTES = 4,
  13. IB_GRH_BYTES = 40,
  14. IB_IP4_BYTES = 20,
  15. IB_UDP_BYTES = 8,
  16. IB_BTH_BYTES = 12,
  17. IB_DETH_BYTES = 8,
  18. IB_EXT_ATOMICETH_BYTES = 28,
  19. IB_EXT_XRC_BYTES = 4,
  20. IB_ICRC_BYTES = 4
  21. };
  22. struct ib_field {
  23. size_t struct_offset_bytes;
  24. size_t struct_size_bytes;
  25. int offset_words;
  26. int offset_bits;
  27. int size_bits;
  28. char *field_name;
  29. };
  30. #define RESERVED \
  31. .field_name = "reserved"
  32. /*
  33. * This macro cleans up the definitions of constants for BTH opcodes.
  34. * It is used to define constants such as IB_OPCODE_UD_SEND_ONLY,
  35. * which becomes IB_OPCODE_UD + IB_OPCODE_SEND_ONLY, and this gives
  36. * the correct value.
  37. *
  38. * In short, user code should use the constants defined using the
  39. * macro rather than worrying about adding together other constants.
  40. */
  41. #define IB_OPCODE(transport, op) \
  42. IB_OPCODE_ ## transport ## _ ## op = \
  43. IB_OPCODE_ ## transport + IB_OPCODE_ ## op
  44. enum {
  45. /* transport types -- just used to define real constants */
  46. IB_OPCODE_RC = 0x00,
  47. IB_OPCODE_UC = 0x20,
  48. IB_OPCODE_RD = 0x40,
  49. IB_OPCODE_UD = 0x60,
  50. /* per IBTA 1.3 vol 1 Table 38, A10.3.2 */
  51. IB_OPCODE_CNP = 0x80,
  52. /* Manufacturer specific */
  53. IB_OPCODE_MSP = 0xe0,
  54. /* operations -- just used to define real constants */
  55. IB_OPCODE_SEND_FIRST = 0x00,
  56. IB_OPCODE_SEND_MIDDLE = 0x01,
  57. IB_OPCODE_SEND_LAST = 0x02,
  58. IB_OPCODE_SEND_LAST_WITH_IMMEDIATE = 0x03,
  59. IB_OPCODE_SEND_ONLY = 0x04,
  60. IB_OPCODE_SEND_ONLY_WITH_IMMEDIATE = 0x05,
  61. IB_OPCODE_RDMA_WRITE_FIRST = 0x06,
  62. IB_OPCODE_RDMA_WRITE_MIDDLE = 0x07,
  63. IB_OPCODE_RDMA_WRITE_LAST = 0x08,
  64. IB_OPCODE_RDMA_WRITE_LAST_WITH_IMMEDIATE = 0x09,
  65. IB_OPCODE_RDMA_WRITE_ONLY = 0x0a,
  66. IB_OPCODE_RDMA_WRITE_ONLY_WITH_IMMEDIATE = 0x0b,
  67. IB_OPCODE_RDMA_READ_REQUEST = 0x0c,
  68. IB_OPCODE_RDMA_READ_RESPONSE_FIRST = 0x0d,
  69. IB_OPCODE_RDMA_READ_RESPONSE_MIDDLE = 0x0e,
  70. IB_OPCODE_RDMA_READ_RESPONSE_LAST = 0x0f,
  71. IB_OPCODE_RDMA_READ_RESPONSE_ONLY = 0x10,
  72. IB_OPCODE_ACKNOWLEDGE = 0x11,
  73. IB_OPCODE_ATOMIC_ACKNOWLEDGE = 0x12,
  74. IB_OPCODE_COMPARE_SWAP = 0x13,
  75. IB_OPCODE_FETCH_ADD = 0x14,
  76. /* opcode 0x15 is reserved */
  77. IB_OPCODE_SEND_LAST_WITH_INVALIDATE = 0x16,
  78. IB_OPCODE_SEND_ONLY_WITH_INVALIDATE = 0x17,
  79. IB_OPCODE_FLUSH = 0x1C,
  80. IB_OPCODE_ATOMIC_WRITE = 0x1D,
  81. /* real constants follow -- see comment about above IB_OPCODE()
  82. macro for more details */
  83. /* RC */
  84. IB_OPCODE(RC, SEND_FIRST),
  85. IB_OPCODE(RC, SEND_MIDDLE),
  86. IB_OPCODE(RC, SEND_LAST),
  87. IB_OPCODE(RC, SEND_LAST_WITH_IMMEDIATE),
  88. IB_OPCODE(RC, SEND_ONLY),
  89. IB_OPCODE(RC, SEND_ONLY_WITH_IMMEDIATE),
  90. IB_OPCODE(RC, RDMA_WRITE_FIRST),
  91. IB_OPCODE(RC, RDMA_WRITE_MIDDLE),
  92. IB_OPCODE(RC, RDMA_WRITE_LAST),
  93. IB_OPCODE(RC, RDMA_WRITE_LAST_WITH_IMMEDIATE),
  94. IB_OPCODE(RC, RDMA_WRITE_ONLY),
  95. IB_OPCODE(RC, RDMA_WRITE_ONLY_WITH_IMMEDIATE),
  96. IB_OPCODE(RC, RDMA_READ_REQUEST),
  97. IB_OPCODE(RC, RDMA_READ_RESPONSE_FIRST),
  98. IB_OPCODE(RC, RDMA_READ_RESPONSE_MIDDLE),
  99. IB_OPCODE(RC, RDMA_READ_RESPONSE_LAST),
  100. IB_OPCODE(RC, RDMA_READ_RESPONSE_ONLY),
  101. IB_OPCODE(RC, ACKNOWLEDGE),
  102. IB_OPCODE(RC, ATOMIC_ACKNOWLEDGE),
  103. IB_OPCODE(RC, COMPARE_SWAP),
  104. IB_OPCODE(RC, FETCH_ADD),
  105. IB_OPCODE(RC, SEND_LAST_WITH_INVALIDATE),
  106. IB_OPCODE(RC, SEND_ONLY_WITH_INVALIDATE),
  107. IB_OPCODE(RC, FLUSH),
  108. IB_OPCODE(RC, ATOMIC_WRITE),
  109. /* UC */
  110. IB_OPCODE(UC, SEND_FIRST),
  111. IB_OPCODE(UC, SEND_MIDDLE),
  112. IB_OPCODE(UC, SEND_LAST),
  113. IB_OPCODE(UC, SEND_LAST_WITH_IMMEDIATE),
  114. IB_OPCODE(UC, SEND_ONLY),
  115. IB_OPCODE(UC, SEND_ONLY_WITH_IMMEDIATE),
  116. IB_OPCODE(UC, RDMA_WRITE_FIRST),
  117. IB_OPCODE(UC, RDMA_WRITE_MIDDLE),
  118. IB_OPCODE(UC, RDMA_WRITE_LAST),
  119. IB_OPCODE(UC, RDMA_WRITE_LAST_WITH_IMMEDIATE),
  120. IB_OPCODE(UC, RDMA_WRITE_ONLY),
  121. IB_OPCODE(UC, RDMA_WRITE_ONLY_WITH_IMMEDIATE),
  122. /* RD */
  123. IB_OPCODE(RD, SEND_FIRST),
  124. IB_OPCODE(RD, SEND_MIDDLE),
  125. IB_OPCODE(RD, SEND_LAST),
  126. IB_OPCODE(RD, SEND_LAST_WITH_IMMEDIATE),
  127. IB_OPCODE(RD, SEND_ONLY),
  128. IB_OPCODE(RD, SEND_ONLY_WITH_IMMEDIATE),
  129. IB_OPCODE(RD, RDMA_WRITE_FIRST),
  130. IB_OPCODE(RD, RDMA_WRITE_MIDDLE),
  131. IB_OPCODE(RD, RDMA_WRITE_LAST),
  132. IB_OPCODE(RD, RDMA_WRITE_LAST_WITH_IMMEDIATE),
  133. IB_OPCODE(RD, RDMA_WRITE_ONLY),
  134. IB_OPCODE(RD, RDMA_WRITE_ONLY_WITH_IMMEDIATE),
  135. IB_OPCODE(RD, RDMA_READ_REQUEST),
  136. IB_OPCODE(RD, RDMA_READ_RESPONSE_FIRST),
  137. IB_OPCODE(RD, RDMA_READ_RESPONSE_MIDDLE),
  138. IB_OPCODE(RD, RDMA_READ_RESPONSE_LAST),
  139. IB_OPCODE(RD, RDMA_READ_RESPONSE_ONLY),
  140. IB_OPCODE(RD, ACKNOWLEDGE),
  141. IB_OPCODE(RD, ATOMIC_ACKNOWLEDGE),
  142. IB_OPCODE(RD, COMPARE_SWAP),
  143. IB_OPCODE(RD, FETCH_ADD),
  144. IB_OPCODE(RD, FLUSH),
  145. /* UD */
  146. IB_OPCODE(UD, SEND_ONLY),
  147. IB_OPCODE(UD, SEND_ONLY_WITH_IMMEDIATE)
  148. };
  149. enum {
  150. IB_LNH_RAW = 0,
  151. IB_LNH_IP = 1,
  152. IB_LNH_IBA_LOCAL = 2,
  153. IB_LNH_IBA_GLOBAL = 3
  154. };
  155. struct ib_unpacked_lrh {
  156. u8 virtual_lane;
  157. u8 link_version;
  158. u8 service_level;
  159. u8 link_next_header;
  160. __be16 destination_lid;
  161. __be16 packet_length;
  162. __be16 source_lid;
  163. };
  164. struct ib_unpacked_grh {
  165. u8 ip_version;
  166. u8 traffic_class;
  167. __be32 flow_label;
  168. __be16 payload_length;
  169. u8 next_header;
  170. u8 hop_limit;
  171. union ib_gid source_gid;
  172. union ib_gid destination_gid;
  173. };
  174. struct ib_unpacked_bth {
  175. u8 opcode;
  176. u8 solicited_event;
  177. u8 mig_req;
  178. u8 pad_count;
  179. u8 transport_header_version;
  180. __be16 pkey;
  181. __be32 destination_qpn;
  182. u8 ack_req;
  183. __be32 psn;
  184. };
  185. struct ib_unpacked_deth {
  186. __be32 qkey;
  187. __be32 source_qpn;
  188. };
  189. struct ib_unpacked_eth {
  190. u8 dmac_h[4];
  191. u8 dmac_l[2];
  192. u8 smac_h[2];
  193. u8 smac_l[4];
  194. __be16 type;
  195. };
  196. struct ib_unpacked_ip4 {
  197. u8 ver;
  198. u8 hdr_len;
  199. u8 tos;
  200. __be16 tot_len;
  201. __be16 id;
  202. __be16 frag_off;
  203. u8 ttl;
  204. u8 protocol;
  205. __sum16 check;
  206. __be32 saddr;
  207. __be32 daddr;
  208. };
  209. struct ib_unpacked_udp {
  210. __be16 sport;
  211. __be16 dport;
  212. __be16 length;
  213. __be16 csum;
  214. };
  215. struct ib_unpacked_vlan {
  216. __be16 tag;
  217. __be16 type;
  218. };
  219. struct ib_ud_header {
  220. int lrh_present;
  221. struct ib_unpacked_lrh lrh;
  222. int eth_present;
  223. struct ib_unpacked_eth eth;
  224. int vlan_present;
  225. struct ib_unpacked_vlan vlan;
  226. int grh_present;
  227. struct ib_unpacked_grh grh;
  228. int ipv4_present;
  229. struct ib_unpacked_ip4 ip4;
  230. int udp_present;
  231. struct ib_unpacked_udp udp;
  232. struct ib_unpacked_bth bth;
  233. struct ib_unpacked_deth deth;
  234. int immediate_present;
  235. __be32 immediate_data;
  236. };
  237. void ib_pack(const struct ib_field *desc,
  238. int desc_len,
  239. void *structure,
  240. void *buf);
  241. void ib_unpack(const struct ib_field *desc,
  242. int desc_len,
  243. void *buf,
  244. void *structure);
  245. __sum16 ib_ud_ip4_csum(struct ib_ud_header *header);
  246. int ib_ud_header_init(int payload_bytes,
  247. int lrh_present,
  248. int eth_present,
  249. int vlan_present,
  250. int grh_present,
  251. int ip_version,
  252. int udp_present,
  253. int immediate_present,
  254. struct ib_ud_header *header);
  255. int ib_ud_header_pack(struct ib_ud_header *header,
  256. void *buf);
  257. #endif /* IB_PACK_H */