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