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