1 /*
2  * Copyright (c) 2004 Topspin Corporation.  All rights reserved.
3  * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
4  *
5  * This software is available to you under a choice of one of two
6  * licenses.  You may choose to be licensed under the terms of the GNU
7  * General Public License (GPL) Version 2, available from the file
8  * COPYING in the main directory of this source tree, or the
9  * OpenIB.org BSD license below:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *      - Redistributions of source code must retain the above
16  *        copyright notice, this list of conditions and the following
17  *        disclaimer.
18  *
19  *      - Redistributions in binary form must reproduce the above
20  *        copyright notice, this list of conditions and the following
21  *        disclaimer in the documentation and/or other materials
22  *        provided with the distribution.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31  * SOFTWARE.
32  *
33  * $Id: ud_header.c 1349 2004-12-16 21:09:43Z roland $
34  */
35 
36 #include <linux/errno.h>
37 #include <linux/string.h>
38 
39 #include <rdma/ib_pack.h>
40 
41 #define STRUCT_FIELD(header, field) \
42 	.struct_offset_bytes = offsetof(struct ib_unpacked_ ## header, field),      \
43 	.struct_size_bytes   = sizeof ((struct ib_unpacked_ ## header *) 0)->field, \
44 	.field_name          = #header ":" #field
45 
46 static const struct ib_field lrh_table[]  = {
47 	{ STRUCT_FIELD(lrh, virtual_lane),
48 	  .offset_words = 0,
49 	  .offset_bits  = 0,
50 	  .size_bits    = 4 },
51 	{ STRUCT_FIELD(lrh, link_version),
52 	  .offset_words = 0,
53 	  .offset_bits  = 4,
54 	  .size_bits    = 4 },
55 	{ STRUCT_FIELD(lrh, service_level),
56 	  .offset_words = 0,
57 	  .offset_bits  = 8,
58 	  .size_bits    = 4 },
59 	{ RESERVED,
60 	  .offset_words = 0,
61 	  .offset_bits  = 12,
62 	  .size_bits    = 2 },
63 	{ STRUCT_FIELD(lrh, link_next_header),
64 	  .offset_words = 0,
65 	  .offset_bits  = 14,
66 	  .size_bits    = 2 },
67 	{ STRUCT_FIELD(lrh, destination_lid),
68 	  .offset_words = 0,
69 	  .offset_bits  = 16,
70 	  .size_bits    = 16 },
71 	{ RESERVED,
72 	  .offset_words = 1,
73 	  .offset_bits  = 0,
74 	  .size_bits    = 5 },
75 	{ STRUCT_FIELD(lrh, packet_length),
76 	  .offset_words = 1,
77 	  .offset_bits  = 5,
78 	  .size_bits    = 11 },
79 	{ STRUCT_FIELD(lrh, source_lid),
80 	  .offset_words = 1,
81 	  .offset_bits  = 16,
82 	  .size_bits    = 16 }
83 };
84 
85 static const struct ib_field grh_table[]  = {
86 	{ STRUCT_FIELD(grh, ip_version),
87 	  .offset_words = 0,
88 	  .offset_bits  = 0,
89 	  .size_bits    = 4 },
90 	{ STRUCT_FIELD(grh, traffic_class),
91 	  .offset_words = 0,
92 	  .offset_bits  = 4,
93 	  .size_bits    = 8 },
94 	{ STRUCT_FIELD(grh, flow_label),
95 	  .offset_words = 0,
96 	  .offset_bits  = 12,
97 	  .size_bits    = 20 },
98 	{ STRUCT_FIELD(grh, payload_length),
99 	  .offset_words = 1,
100 	  .offset_bits  = 0,
101 	  .size_bits    = 16 },
102 	{ STRUCT_FIELD(grh, next_header),
103 	  .offset_words = 1,
104 	  .offset_bits  = 16,
105 	  .size_bits    = 8 },
106 	{ STRUCT_FIELD(grh, hop_limit),
107 	  .offset_words = 1,
108 	  .offset_bits  = 24,
109 	  .size_bits    = 8 },
110 	{ STRUCT_FIELD(grh, source_gid),
111 	  .offset_words = 2,
112 	  .offset_bits  = 0,
113 	  .size_bits    = 128 },
114 	{ STRUCT_FIELD(grh, destination_gid),
115 	  .offset_words = 6,
116 	  .offset_bits  = 0,
117 	  .size_bits    = 128 }
118 };
119 
120 static const struct ib_field bth_table[]  = {
121 	{ STRUCT_FIELD(bth, opcode),
122 	  .offset_words = 0,
123 	  .offset_bits  = 0,
124 	  .size_bits    = 8 },
125 	{ STRUCT_FIELD(bth, solicited_event),
126 	  .offset_words = 0,
127 	  .offset_bits  = 8,
128 	  .size_bits    = 1 },
129 	{ STRUCT_FIELD(bth, mig_req),
130 	  .offset_words = 0,
131 	  .offset_bits  = 9,
132 	  .size_bits    = 1 },
133 	{ STRUCT_FIELD(bth, pad_count),
134 	  .offset_words = 0,
135 	  .offset_bits  = 10,
136 	  .size_bits    = 2 },
137 	{ STRUCT_FIELD(bth, transport_header_version),
138 	  .offset_words = 0,
139 	  .offset_bits  = 12,
140 	  .size_bits    = 4 },
141 	{ STRUCT_FIELD(bth, pkey),
142 	  .offset_words = 0,
143 	  .offset_bits  = 16,
144 	  .size_bits    = 16 },
145 	{ RESERVED,
146 	  .offset_words = 1,
147 	  .offset_bits  = 0,
148 	  .size_bits    = 8 },
149 	{ STRUCT_FIELD(bth, destination_qpn),
150 	  .offset_words = 1,
151 	  .offset_bits  = 8,
152 	  .size_bits    = 24 },
153 	{ STRUCT_FIELD(bth, ack_req),
154 	  .offset_words = 2,
155 	  .offset_bits  = 0,
156 	  .size_bits    = 1 },
157 	{ RESERVED,
158 	  .offset_words = 2,
159 	  .offset_bits  = 1,
160 	  .size_bits    = 7 },
161 	{ STRUCT_FIELD(bth, psn),
162 	  .offset_words = 2,
163 	  .offset_bits  = 8,
164 	  .size_bits    = 24 }
165 };
166 
167 static const struct ib_field deth_table[] = {
168 	{ STRUCT_FIELD(deth, qkey),
169 	  .offset_words = 0,
170 	  .offset_bits  = 0,
171 	  .size_bits    = 32 },
172 	{ RESERVED,
173 	  .offset_words = 1,
174 	  .offset_bits  = 0,
175 	  .size_bits    = 8 },
176 	{ STRUCT_FIELD(deth, source_qpn),
177 	  .offset_words = 1,
178 	  .offset_bits  = 8,
179 	  .size_bits    = 24 }
180 };
181 
182 /**
183  * ib_ud_header_init - Initialize UD header structure
184  * @payload_bytes:Length of packet payload
185  * @grh_present:GRH flag (if non-zero, GRH will be included)
186  * @header:Structure to initialize
187  *
188  * ib_ud_header_init() initializes the lrh.link_version, lrh.link_next_header,
189  * lrh.packet_length, grh.ip_version, grh.payload_length,
190  * grh.next_header, bth.opcode, bth.pad_count and
191  * bth.transport_header_version fields of a &struct ib_ud_header given
192  * the payload length and whether a GRH will be included.
193  */
194 void ib_ud_header_init(int     		    payload_bytes,
195 		       int    		    grh_present,
196 		       struct ib_ud_header *header)
197 {
198 	int header_len;
199 	u16 packet_length;
200 
201 	memset(header, 0, sizeof *header);
202 
203 	header_len =
204 		IB_LRH_BYTES  +
205 		IB_BTH_BYTES  +
206 		IB_DETH_BYTES;
207 	if (grh_present) {
208 		header_len += IB_GRH_BYTES;
209 	}
210 
211 	header->lrh.link_version     = 0;
212 	header->lrh.link_next_header =
213 		grh_present ? IB_LNH_IBA_GLOBAL : IB_LNH_IBA_LOCAL;
214 	packet_length		     = (IB_LRH_BYTES     +
215 					IB_BTH_BYTES     +
216 					IB_DETH_BYTES    +
217 					payload_bytes    +
218 					4                + /* ICRC     */
219 					3) / 4;            /* round up */
220 
221 	header->grh_present          = grh_present;
222 	if (grh_present) {
223 		packet_length		   += IB_GRH_BYTES / 4;
224 		header->grh.ip_version      = 6;
225 		header->grh.payload_length  =
226 			cpu_to_be16((IB_BTH_BYTES     +
227 				     IB_DETH_BYTES    +
228 				     payload_bytes    +
229 				     4                + /* ICRC     */
230 				     3) & ~3);          /* round up */
231 		header->grh.next_header     = 0x1b;
232 	}
233 
234 	header->lrh.packet_length = cpu_to_be16(packet_length);
235 
236 	if (header->immediate_present)
237 		header->bth.opcode           = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE;
238 	else
239 		header->bth.opcode           = IB_OPCODE_UD_SEND_ONLY;
240 	header->bth.pad_count                = (4 - payload_bytes) & 3;
241 	header->bth.transport_header_version = 0;
242 }
243 EXPORT_SYMBOL(ib_ud_header_init);
244 
245 /**
246  * ib_ud_header_pack - Pack UD header struct into wire format
247  * @header:UD header struct
248  * @buf:Buffer to pack into
249  *
250  * ib_ud_header_pack() packs the UD header structure @header into wire
251  * format in the buffer @buf.
252  */
253 int ib_ud_header_pack(struct ib_ud_header *header,
254 		      void                *buf)
255 {
256 	int len = 0;
257 
258 	ib_pack(lrh_table, ARRAY_SIZE(lrh_table),
259 		&header->lrh, buf);
260 	len += IB_LRH_BYTES;
261 
262 	if (header->grh_present) {
263 		ib_pack(grh_table, ARRAY_SIZE(grh_table),
264 			&header->grh, buf + len);
265 		len += IB_GRH_BYTES;
266 	}
267 
268 	ib_pack(bth_table, ARRAY_SIZE(bth_table),
269 		&header->bth, buf + len);
270 	len += IB_BTH_BYTES;
271 
272 	ib_pack(deth_table, ARRAY_SIZE(deth_table),
273 		&header->deth, buf + len);
274 	len += IB_DETH_BYTES;
275 
276 	if (header->immediate_present) {
277 		memcpy(buf + len, &header->immediate_data, sizeof header->immediate_data);
278 		len += sizeof header->immediate_data;
279 	}
280 
281 	return len;
282 }
283 EXPORT_SYMBOL(ib_ud_header_pack);
284 
285 /**
286  * ib_ud_header_unpack - Unpack UD header struct from wire format
287  * @header:UD header struct
288  * @buf:Buffer to pack into
289  *
290  * ib_ud_header_pack() unpacks the UD header structure @header from wire
291  * format in the buffer @buf.
292  */
293 int ib_ud_header_unpack(void                *buf,
294 			struct ib_ud_header *header)
295 {
296 	ib_unpack(lrh_table, ARRAY_SIZE(lrh_table),
297 		  buf, &header->lrh);
298 	buf += IB_LRH_BYTES;
299 
300 	if (header->lrh.link_version != 0) {
301 		printk(KERN_WARNING "Invalid LRH.link_version %d\n",
302 		       header->lrh.link_version);
303 		return -EINVAL;
304 	}
305 
306 	switch (header->lrh.link_next_header) {
307 	case IB_LNH_IBA_LOCAL:
308 		header->grh_present = 0;
309 		break;
310 
311 	case IB_LNH_IBA_GLOBAL:
312 		header->grh_present = 1;
313 		ib_unpack(grh_table, ARRAY_SIZE(grh_table),
314 			  buf, &header->grh);
315 		buf += IB_GRH_BYTES;
316 
317 		if (header->grh.ip_version != 6) {
318 			printk(KERN_WARNING "Invalid GRH.ip_version %d\n",
319 			       header->grh.ip_version);
320 			return -EINVAL;
321 		}
322 		if (header->grh.next_header != 0x1b) {
323 			printk(KERN_WARNING "Invalid GRH.next_header 0x%02x\n",
324 			       header->grh.next_header);
325 			return -EINVAL;
326 		}
327 		break;
328 
329 	default:
330 		printk(KERN_WARNING "Invalid LRH.link_next_header %d\n",
331 		       header->lrh.link_next_header);
332 		return -EINVAL;
333 	}
334 
335 	ib_unpack(bth_table, ARRAY_SIZE(bth_table),
336 		  buf, &header->bth);
337 	buf += IB_BTH_BYTES;
338 
339 	switch (header->bth.opcode) {
340 	case IB_OPCODE_UD_SEND_ONLY:
341 		header->immediate_present = 0;
342 		break;
343 	case IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE:
344 		header->immediate_present = 1;
345 		break;
346 	default:
347 		printk(KERN_WARNING "Invalid BTH.opcode 0x%02x\n",
348 		       header->bth.opcode);
349 		return -EINVAL;
350 	}
351 
352 	if (header->bth.transport_header_version != 0) {
353 		printk(KERN_WARNING "Invalid BTH.transport_header_version %d\n",
354 		       header->bth.transport_header_version);
355 		return -EINVAL;
356 	}
357 
358 	ib_unpack(deth_table, ARRAY_SIZE(deth_table),
359 		  buf, &header->deth);
360 	buf += IB_DETH_BYTES;
361 
362 	if (header->immediate_present)
363 		memcpy(&header->immediate_data, buf, sizeof header->immediate_data);
364 
365 	return 0;
366 }
367 EXPORT_SYMBOL(ib_ud_header_unpack);
368