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