1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0 3 * 4 * Copyright (c) 2004 Topspin Corporation. All rights reserved. 5 * 6 * This software is available to you under a choice of one of two 7 * licenses. You may choose to be licensed under the terms of the GNU 8 * General Public License (GPL) Version 2, available from the file 9 * COPYING in the main directory of this source tree, or the 10 * OpenIB.org BSD license below: 11 * 12 * Redistribution and use in source and binary forms, with or 13 * without modification, are permitted provided that the following 14 * conditions are met: 15 * 16 * - Redistributions of source code must retain the above 17 * copyright notice, this list of conditions and the following 18 * disclaimer. 19 * 20 * - Redistributions in binary form must reproduce the above 21 * copyright notice, this list of conditions and the following 22 * disclaimer in the documentation and/or other materials 23 * provided with the distribution. 24 * 25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 32 * SOFTWARE. 33 */ 34 35 #ifndef IB_PACK_H 36 #define IB_PACK_H 37 38 #include <rdma/ib_verbs.h> 39 40 enum { 41 IB_LRH_BYTES = 8, 42 IB_ETH_BYTES = 14, 43 IB_VLAN_BYTES = 4, 44 IB_GRH_BYTES = 40, 45 IB_IP4_BYTES = 20, 46 IB_UDP_BYTES = 8, 47 IB_BTH_BYTES = 12, 48 IB_DETH_BYTES = 8 49 }; 50 51 struct ib_field { 52 size_t struct_offset_bytes; 53 size_t struct_size_bytes; 54 int offset_words; 55 int offset_bits; 56 int size_bits; 57 char *field_name; 58 }; 59 60 #define RESERVED \ 61 .field_name = "reserved" 62 63 /* 64 * This macro cleans up the definitions of constants for BTH opcodes. 65 * It is used to define constants such as IB_OPCODE_UD_SEND_ONLY, 66 * which becomes IB_OPCODE_UD + IB_OPCODE_SEND_ONLY, and this gives 67 * the correct value. 68 * 69 * In short, user code should use the constants defined using the 70 * macro rather than worrying about adding together other constants. 71 */ 72 #define IB_OPCODE(transport, op) \ 73 IB_OPCODE_ ## transport ## _ ## op = \ 74 IB_OPCODE_ ## transport + IB_OPCODE_ ## op 75 76 enum { 77 /* transport types -- just used to define real constants */ 78 IB_OPCODE_RC = 0x00, 79 IB_OPCODE_UC = 0x20, 80 IB_OPCODE_RD = 0x40, 81 IB_OPCODE_UD = 0x60, 82 /* per IBTA 1.3 vol 1 Table 38, A10.3.2 */ 83 IB_OPCODE_CNP = 0x80, 84 85 /* operations -- just used to define real constants */ 86 IB_OPCODE_SEND_FIRST = 0x00, 87 IB_OPCODE_SEND_MIDDLE = 0x01, 88 IB_OPCODE_SEND_LAST = 0x02, 89 IB_OPCODE_SEND_LAST_WITH_IMMEDIATE = 0x03, 90 IB_OPCODE_SEND_ONLY = 0x04, 91 IB_OPCODE_SEND_ONLY_WITH_IMMEDIATE = 0x05, 92 IB_OPCODE_RDMA_WRITE_FIRST = 0x06, 93 IB_OPCODE_RDMA_WRITE_MIDDLE = 0x07, 94 IB_OPCODE_RDMA_WRITE_LAST = 0x08, 95 IB_OPCODE_RDMA_WRITE_LAST_WITH_IMMEDIATE = 0x09, 96 IB_OPCODE_RDMA_WRITE_ONLY = 0x0a, 97 IB_OPCODE_RDMA_WRITE_ONLY_WITH_IMMEDIATE = 0x0b, 98 IB_OPCODE_RDMA_READ_REQUEST = 0x0c, 99 IB_OPCODE_RDMA_READ_RESPONSE_FIRST = 0x0d, 100 IB_OPCODE_RDMA_READ_RESPONSE_MIDDLE = 0x0e, 101 IB_OPCODE_RDMA_READ_RESPONSE_LAST = 0x0f, 102 IB_OPCODE_RDMA_READ_RESPONSE_ONLY = 0x10, 103 IB_OPCODE_ACKNOWLEDGE = 0x11, 104 IB_OPCODE_ATOMIC_ACKNOWLEDGE = 0x12, 105 IB_OPCODE_COMPARE_SWAP = 0x13, 106 IB_OPCODE_FETCH_ADD = 0x14, 107 /* opcode 0x15 is reserved */ 108 IB_OPCODE_SEND_LAST_WITH_INVALIDATE = 0x16, 109 IB_OPCODE_SEND_ONLY_WITH_INVALIDATE = 0x17, 110 111 /* real constants follow -- see comment about above IB_OPCODE() 112 macro for more details */ 113 114 /* RC */ 115 IB_OPCODE(RC, SEND_FIRST), 116 IB_OPCODE(RC, SEND_MIDDLE), 117 IB_OPCODE(RC, SEND_LAST), 118 IB_OPCODE(RC, SEND_LAST_WITH_IMMEDIATE), 119 IB_OPCODE(RC, SEND_ONLY), 120 IB_OPCODE(RC, SEND_ONLY_WITH_IMMEDIATE), 121 IB_OPCODE(RC, RDMA_WRITE_FIRST), 122 IB_OPCODE(RC, RDMA_WRITE_MIDDLE), 123 IB_OPCODE(RC, RDMA_WRITE_LAST), 124 IB_OPCODE(RC, RDMA_WRITE_LAST_WITH_IMMEDIATE), 125 IB_OPCODE(RC, RDMA_WRITE_ONLY), 126 IB_OPCODE(RC, RDMA_WRITE_ONLY_WITH_IMMEDIATE), 127 IB_OPCODE(RC, RDMA_READ_REQUEST), 128 IB_OPCODE(RC, RDMA_READ_RESPONSE_FIRST), 129 IB_OPCODE(RC, RDMA_READ_RESPONSE_MIDDLE), 130 IB_OPCODE(RC, RDMA_READ_RESPONSE_LAST), 131 IB_OPCODE(RC, RDMA_READ_RESPONSE_ONLY), 132 IB_OPCODE(RC, ACKNOWLEDGE), 133 IB_OPCODE(RC, ATOMIC_ACKNOWLEDGE), 134 IB_OPCODE(RC, COMPARE_SWAP), 135 IB_OPCODE(RC, FETCH_ADD), 136 IB_OPCODE(RC, SEND_LAST_WITH_INVALIDATE), 137 IB_OPCODE(RC, SEND_ONLY_WITH_INVALIDATE), 138 139 /* UC */ 140 IB_OPCODE(UC, SEND_FIRST), 141 IB_OPCODE(UC, SEND_MIDDLE), 142 IB_OPCODE(UC, SEND_LAST), 143 IB_OPCODE(UC, SEND_LAST_WITH_IMMEDIATE), 144 IB_OPCODE(UC, SEND_ONLY), 145 IB_OPCODE(UC, SEND_ONLY_WITH_IMMEDIATE), 146 IB_OPCODE(UC, RDMA_WRITE_FIRST), 147 IB_OPCODE(UC, RDMA_WRITE_MIDDLE), 148 IB_OPCODE(UC, RDMA_WRITE_LAST), 149 IB_OPCODE(UC, RDMA_WRITE_LAST_WITH_IMMEDIATE), 150 IB_OPCODE(UC, RDMA_WRITE_ONLY), 151 IB_OPCODE(UC, RDMA_WRITE_ONLY_WITH_IMMEDIATE), 152 153 /* RD */ 154 IB_OPCODE(RD, SEND_FIRST), 155 IB_OPCODE(RD, SEND_MIDDLE), 156 IB_OPCODE(RD, SEND_LAST), 157 IB_OPCODE(RD, SEND_LAST_WITH_IMMEDIATE), 158 IB_OPCODE(RD, SEND_ONLY), 159 IB_OPCODE(RD, SEND_ONLY_WITH_IMMEDIATE), 160 IB_OPCODE(RD, RDMA_WRITE_FIRST), 161 IB_OPCODE(RD, RDMA_WRITE_MIDDLE), 162 IB_OPCODE(RD, RDMA_WRITE_LAST), 163 IB_OPCODE(RD, RDMA_WRITE_LAST_WITH_IMMEDIATE), 164 IB_OPCODE(RD, RDMA_WRITE_ONLY), 165 IB_OPCODE(RD, RDMA_WRITE_ONLY_WITH_IMMEDIATE), 166 IB_OPCODE(RD, RDMA_READ_REQUEST), 167 IB_OPCODE(RD, RDMA_READ_RESPONSE_FIRST), 168 IB_OPCODE(RD, RDMA_READ_RESPONSE_MIDDLE), 169 IB_OPCODE(RD, RDMA_READ_RESPONSE_LAST), 170 IB_OPCODE(RD, RDMA_READ_RESPONSE_ONLY), 171 IB_OPCODE(RD, ACKNOWLEDGE), 172 IB_OPCODE(RD, ATOMIC_ACKNOWLEDGE), 173 IB_OPCODE(RD, COMPARE_SWAP), 174 IB_OPCODE(RD, FETCH_ADD), 175 176 /* UD */ 177 IB_OPCODE(UD, SEND_ONLY), 178 IB_OPCODE(UD, SEND_ONLY_WITH_IMMEDIATE) 179 }; 180 181 enum { 182 IB_LNH_RAW = 0, 183 IB_LNH_IP = 1, 184 IB_LNH_IBA_LOCAL = 2, 185 IB_LNH_IBA_GLOBAL = 3 186 }; 187 188 struct ib_unpacked_lrh { 189 u8 virtual_lane; 190 u8 link_version; 191 u8 service_level; 192 u8 link_next_header; 193 __be16 destination_lid; 194 __be16 packet_length; 195 __be16 source_lid; 196 }; 197 198 struct ib_unpacked_grh { 199 u8 ip_version; 200 u8 traffic_class; 201 __be32 flow_label; 202 __be16 payload_length; 203 u8 next_header; 204 u8 hop_limit; 205 union ib_gid source_gid; 206 union ib_gid destination_gid; 207 }; 208 209 struct ib_unpacked_bth { 210 u8 opcode; 211 u8 solicited_event; 212 u8 mig_req; 213 u8 pad_count; 214 u8 transport_header_version; 215 __be16 pkey; 216 __be32 destination_qpn; 217 u8 ack_req; 218 __be32 psn; 219 }; 220 221 struct ib_unpacked_deth { 222 __be32 qkey; 223 __be32 source_qpn; 224 }; 225 226 struct ib_unpacked_eth { 227 u8 dmac_h[4]; 228 u8 dmac_l[2]; 229 u8 smac_h[2]; 230 u8 smac_l[4]; 231 __be16 type; 232 }; 233 234 struct ib_unpacked_ip4 { 235 u8 ver; 236 u8 hdr_len; 237 u8 tos; 238 __be16 tot_len; 239 __be16 id; 240 __be16 frag_off; 241 u8 ttl; 242 u8 protocol; 243 __sum16 check; 244 __be32 saddr; 245 __be32 daddr; 246 }; 247 248 struct ib_unpacked_udp { 249 __be16 sport; 250 __be16 dport; 251 __be16 length; 252 __be16 csum; 253 }; 254 255 struct ib_unpacked_vlan { 256 __be16 tag; 257 __be16 type; 258 }; 259 260 struct ib_ud_header { 261 int lrh_present; 262 struct ib_unpacked_lrh lrh; 263 int eth_present; 264 struct ib_unpacked_eth eth; 265 int vlan_present; 266 struct ib_unpacked_vlan vlan; 267 int grh_present; 268 struct ib_unpacked_grh grh; 269 int ipv4_present; 270 struct ib_unpacked_ip4 ip4; 271 int udp_present; 272 struct ib_unpacked_udp udp; 273 struct ib_unpacked_bth bth; 274 struct ib_unpacked_deth deth; 275 int immediate_present; 276 __be32 immediate_data; 277 }; 278 279 void ib_pack(const struct ib_field *desc, 280 int desc_len, 281 void *structure, 282 void *buf); 283 284 void ib_unpack(const struct ib_field *desc, 285 int desc_len, 286 void *buf, 287 void *structure); 288 289 __sum16 ib_ud_ip4_csum(struct ib_ud_header *header); 290 291 int ib_ud_header_init(int payload_bytes, 292 int lrh_present, 293 int eth_present, 294 int vlan_present, 295 int grh_present, 296 int ip_version, 297 int udp_present, 298 int immediate_present, 299 struct ib_ud_header *header); 300 301 int ib_ud_header_pack(struct ib_ud_header *header, 302 void *buf); 303 304 int ib_ud_header_unpack(void *buf, 305 struct ib_ud_header *header); 306 307 #endif /* IB_PACK_H */ 308