ib_hdrs.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. * Copyright(c) 2016 Intel Corporation.
  3. *
  4. * This file is provided under a dual BSD/GPLv2 license. When using or
  5. * redistributing this file, you may do so under either license.
  6. *
  7. * GPL LICENSE SUMMARY
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * BSD LICENSE
  19. *
  20. * Redistribution and use in source and binary forms, with or without
  21. * modification, are permitted provided that the following conditions
  22. * are met:
  23. *
  24. * - Redistributions of source code must retain the above copyright
  25. * notice, this list of conditions and the following disclaimer.
  26. * - Redistributions in binary form must reproduce the above copyright
  27. * notice, this list of conditions and the following disclaimer in
  28. * the documentation and/or other materials provided with the
  29. * distribution.
  30. * - Neither the name of Intel Corporation nor the names of its
  31. * contributors may be used to endorse or promote products derived
  32. * from this software without specific prior written permission.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  35. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  36. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  37. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  38. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  39. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  40. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  41. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  42. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  43. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  44. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. *
  46. */
  47. #ifndef IB_HDRS_H
  48. #define IB_HDRS_H
  49. #include <linux/types.h>
  50. #include <asm/unaligned.h>
  51. #include <rdma/ib_verbs.h>
  52. #define IB_SEQ_NAK (3 << 29)
  53. /* AETH NAK opcode values */
  54. #define IB_RNR_NAK 0x20
  55. #define IB_NAK_PSN_ERROR 0x60
  56. #define IB_NAK_INVALID_REQUEST 0x61
  57. #define IB_NAK_REMOTE_ACCESS_ERROR 0x62
  58. #define IB_NAK_REMOTE_OPERATIONAL_ERROR 0x63
  59. #define IB_NAK_INVALID_RD_REQUEST 0x64
  60. #define IB_BTH_REQ_ACK BIT(31)
  61. #define IB_BTH_SOLICITED BIT(23)
  62. #define IB_BTH_MIG_REQ BIT(22)
  63. #define IB_GRH_VERSION 6
  64. #define IB_GRH_VERSION_MASK 0xF
  65. #define IB_GRH_VERSION_SHIFT 28
  66. #define IB_GRH_TCLASS_MASK 0xFF
  67. #define IB_GRH_TCLASS_SHIFT 20
  68. #define IB_GRH_FLOW_MASK 0xFFFFF
  69. #define IB_GRH_FLOW_SHIFT 0
  70. #define IB_GRH_NEXT_HDR 0x1B
  71. #define IB_FECN_SHIFT 31
  72. #define IB_FECN_MASK 1
  73. #define IB_FECN_SMASK BIT(IB_FECN_SHIFT)
  74. #define IB_BECN_SHIFT 30
  75. #define IB_BECN_MASK 1
  76. #define IB_BECN_SMASK BIT(IB_BECN_SHIFT)
  77. #define IB_AETH_CREDIT_SHIFT 24
  78. #define IB_AETH_CREDIT_MASK 0x1F
  79. #define IB_AETH_CREDIT_INVAL 0x1F
  80. #define IB_AETH_NAK_SHIFT 29
  81. #define IB_MSN_MASK 0xFFFFFF
  82. struct ib_reth {
  83. __be64 vaddr; /* potentially unaligned */
  84. __be32 rkey;
  85. __be32 length;
  86. } __packed;
  87. struct ib_atomic_eth {
  88. __be64 vaddr; /* potentially unaligned */
  89. __be32 rkey;
  90. __be64 swap_data; /* potentially unaligned */
  91. __be64 compare_data; /* potentially unaligned */
  92. } __packed;
  93. union ib_ehdrs {
  94. struct {
  95. __be32 deth[2];
  96. __be32 imm_data;
  97. } ud;
  98. struct {
  99. struct ib_reth reth;
  100. __be32 imm_data;
  101. } rc;
  102. struct {
  103. __be32 aeth;
  104. __be64 atomic_ack_eth; /* potentially unaligned */
  105. } __packed at;
  106. __be32 imm_data;
  107. __be32 aeth;
  108. __be32 ieth;
  109. struct ib_atomic_eth atomic_eth;
  110. } __packed;
  111. struct ib_other_headers {
  112. __be32 bth[3];
  113. union ib_ehdrs u;
  114. } __packed;
  115. struct ib_header {
  116. __be16 lrh[4];
  117. union {
  118. struct {
  119. struct ib_grh grh;
  120. struct ib_other_headers oth;
  121. } l;
  122. struct ib_other_headers oth;
  123. } u;
  124. } __packed;
  125. /* accessors for unaligned __be64 items */
  126. static inline u64 ib_u64_get(__be64 *p)
  127. {
  128. return get_unaligned_be64(p);
  129. }
  130. static inline void ib_u64_put(u64 val, __be64 *p)
  131. {
  132. put_unaligned_be64(val, p);
  133. }
  134. static inline u64 get_ib_reth_vaddr(struct ib_reth *reth)
  135. {
  136. return ib_u64_get(&reth->vaddr);
  137. }
  138. static inline void put_ib_reth_vaddr(u64 val, struct ib_reth *reth)
  139. {
  140. ib_u64_put(val, &reth->vaddr);
  141. }
  142. static inline u64 get_ib_ateth_vaddr(struct ib_atomic_eth *ateth)
  143. {
  144. return ib_u64_get(&ateth->vaddr);
  145. }
  146. static inline void put_ib_ateth_vaddr(u64 val, struct ib_atomic_eth *ateth)
  147. {
  148. ib_u64_put(val, &ateth->vaddr);
  149. }
  150. static inline u64 get_ib_ateth_swap(struct ib_atomic_eth *ateth)
  151. {
  152. return ib_u64_get(&ateth->swap_data);
  153. }
  154. static inline void put_ib_ateth_swap(u64 val, struct ib_atomic_eth *ateth)
  155. {
  156. ib_u64_put(val, &ateth->swap_data);
  157. }
  158. static inline u64 get_ib_ateth_compare(struct ib_atomic_eth *ateth)
  159. {
  160. return ib_u64_get(&ateth->compare_data);
  161. }
  162. static inline void put_ib_ateth_compare(u64 val, struct ib_atomic_eth *ateth)
  163. {
  164. ib_u64_put(val, &ateth->compare_data);
  165. }
  166. /*
  167. * 9B/IB Packet Format
  168. */
  169. #define IB_LNH_MASK 3
  170. #define IB_SC_MASK 0xf
  171. #define IB_SC_SHIFT 12
  172. #define IB_SC5_MASK 0x10
  173. #define IB_SL_MASK 0xf
  174. #define IB_SL_SHIFT 4
  175. #define IB_SL_SHIFT 4
  176. #define IB_LVER_MASK 0xf
  177. #define IB_LVER_SHIFT 8
  178. static inline u8 ib_get_lnh(struct ib_header *hdr)
  179. {
  180. return (be16_to_cpu(hdr->lrh[0]) & IB_LNH_MASK);
  181. }
  182. static inline u8 ib_get_sc(struct ib_header *hdr)
  183. {
  184. return ((be16_to_cpu(hdr->lrh[0]) >> IB_SC_SHIFT) & IB_SC_MASK);
  185. }
  186. static inline bool ib_is_sc5(u16 sc5)
  187. {
  188. return !!(sc5 & IB_SC5_MASK);
  189. }
  190. static inline u8 ib_get_sl(struct ib_header *hdr)
  191. {
  192. return ((be16_to_cpu(hdr->lrh[0]) >> IB_SL_SHIFT) & IB_SL_MASK);
  193. }
  194. static inline u16 ib_get_dlid(struct ib_header *hdr)
  195. {
  196. return (be16_to_cpu(hdr->lrh[1]));
  197. }
  198. static inline u16 ib_get_slid(struct ib_header *hdr)
  199. {
  200. return (be16_to_cpu(hdr->lrh[3]));
  201. }
  202. static inline u8 ib_get_lver(struct ib_header *hdr)
  203. {
  204. return (u8)((be16_to_cpu(hdr->lrh[0]) >> IB_LVER_SHIFT) &
  205. IB_LVER_MASK);
  206. }
  207. static inline u16 ib_get_len(struct ib_header *hdr)
  208. {
  209. return (u16)(be16_to_cpu(hdr->lrh[2]));
  210. }
  211. static inline u32 ib_get_qkey(struct ib_other_headers *ohdr)
  212. {
  213. return be32_to_cpu(ohdr->u.ud.deth[0]);
  214. }
  215. static inline u32 ib_get_sqpn(struct ib_other_headers *ohdr)
  216. {
  217. return ((be32_to_cpu(ohdr->u.ud.deth[1])) & IB_QPN_MASK);
  218. }
  219. /*
  220. * BTH
  221. */
  222. #define IB_BTH_OPCODE_MASK 0xff
  223. #define IB_BTH_OPCODE_SHIFT 24
  224. #define IB_BTH_PAD_MASK 3
  225. #define IB_BTH_PKEY_MASK 0xffff
  226. #define IB_BTH_PAD_SHIFT 20
  227. #define IB_BTH_A_MASK 1
  228. #define IB_BTH_A_SHIFT 31
  229. #define IB_BTH_M_MASK 1
  230. #define IB_BTH_M_SHIFT 22
  231. #define IB_BTH_SE_MASK 1
  232. #define IB_BTH_SE_SHIFT 23
  233. #define IB_BTH_TVER_MASK 0xf
  234. #define IB_BTH_TVER_SHIFT 16
  235. static inline u8 ib_bth_get_pad(struct ib_other_headers *ohdr)
  236. {
  237. return ((be32_to_cpu(ohdr->bth[0]) >> IB_BTH_PAD_SHIFT) &
  238. IB_BTH_PAD_MASK);
  239. }
  240. static inline u16 ib_bth_get_pkey(struct ib_other_headers *ohdr)
  241. {
  242. return (be32_to_cpu(ohdr->bth[0]) & IB_BTH_PKEY_MASK);
  243. }
  244. static inline u8 ib_bth_get_opcode(struct ib_other_headers *ohdr)
  245. {
  246. return ((be32_to_cpu(ohdr->bth[0]) >> IB_BTH_OPCODE_SHIFT) &
  247. IB_BTH_OPCODE_MASK);
  248. }
  249. static inline u8 ib_bth_get_ackreq(struct ib_other_headers *ohdr)
  250. {
  251. return (u8)((be32_to_cpu(ohdr->bth[2]) >> IB_BTH_A_SHIFT) &
  252. IB_BTH_A_MASK);
  253. }
  254. static inline u8 ib_bth_get_migreq(struct ib_other_headers *ohdr)
  255. {
  256. return (u8)((be32_to_cpu(ohdr->bth[0]) >> IB_BTH_M_SHIFT) &
  257. IB_BTH_M_MASK);
  258. }
  259. static inline u8 ib_bth_get_se(struct ib_other_headers *ohdr)
  260. {
  261. return (u8)((be32_to_cpu(ohdr->bth[0]) >> IB_BTH_SE_SHIFT) &
  262. IB_BTH_SE_MASK);
  263. }
  264. static inline u32 ib_bth_get_psn(struct ib_other_headers *ohdr)
  265. {
  266. return (u32)(be32_to_cpu(ohdr->bth[2]));
  267. }
  268. static inline u32 ib_bth_get_qpn(struct ib_other_headers *ohdr)
  269. {
  270. return (u32)((be32_to_cpu(ohdr->bth[1])) & IB_QPN_MASK);
  271. }
  272. static inline bool ib_bth_get_becn(struct ib_other_headers *ohdr)
  273. {
  274. return (ohdr->bth[1]) & cpu_to_be32(IB_BECN_SMASK);
  275. }
  276. static inline bool ib_bth_get_fecn(struct ib_other_headers *ohdr)
  277. {
  278. return (ohdr->bth[1]) & cpu_to_be32(IB_FECN_SMASK);
  279. }
  280. static inline u8 ib_bth_get_tver(struct ib_other_headers *ohdr)
  281. {
  282. return (u8)((be32_to_cpu(ohdr->bth[0]) >> IB_BTH_TVER_SHIFT) &
  283. IB_BTH_TVER_MASK);
  284. }
  285. static inline bool ib_bth_is_solicited(struct ib_other_headers *ohdr)
  286. {
  287. return ohdr->bth[0] & cpu_to_be32(IB_BTH_SOLICITED);
  288. }
  289. static inline bool ib_bth_is_migration(struct ib_other_headers *ohdr)
  290. {
  291. return ohdr->bth[0] & cpu_to_be32(IB_BTH_MIG_REQ);
  292. }
  293. #endif /* IB_HDRS_H */