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