2 * Copyright (c) 2004 Topspin Corporation. All rights reserved.
3 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
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:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
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.
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
34 #include <linux/errno.h>
35 #include <linux/string.h>
37 #include <rdma/ib_pack.h>
39 #define STRUCT_FIELD(header, field) \
40 .struct_offset_bytes = offsetof(struct ib_unpacked_ ## header, field), \
41 .struct_size_bytes = sizeof ((struct ib_unpacked_ ## header *) 0)->field, \
42 .field_name = #header ":" #field
44 static const struct ib_field lrh_table[] = {
45 { STRUCT_FIELD(lrh, virtual_lane),
49 { STRUCT_FIELD(lrh, link_version),
53 { STRUCT_FIELD(lrh, service_level),
61 { STRUCT_FIELD(lrh, link_next_header),
65 { STRUCT_FIELD(lrh, destination_lid),
73 { STRUCT_FIELD(lrh, packet_length),
77 { STRUCT_FIELD(lrh, source_lid),
83 static const struct ib_field grh_table[] = {
84 { STRUCT_FIELD(grh, ip_version),
88 { STRUCT_FIELD(grh, traffic_class),
92 { STRUCT_FIELD(grh, flow_label),
96 { STRUCT_FIELD(grh, payload_length),
100 { STRUCT_FIELD(grh, next_header),
104 { STRUCT_FIELD(grh, hop_limit),
108 { STRUCT_FIELD(grh, source_gid),
112 { STRUCT_FIELD(grh, destination_gid),
118 static const struct ib_field bth_table[] = {
119 { STRUCT_FIELD(bth, opcode),
123 { STRUCT_FIELD(bth, solicited_event),
127 { STRUCT_FIELD(bth, mig_req),
131 { STRUCT_FIELD(bth, pad_count),
135 { STRUCT_FIELD(bth, transport_header_version),
139 { STRUCT_FIELD(bth, pkey),
147 { STRUCT_FIELD(bth, destination_qpn),
151 { STRUCT_FIELD(bth, ack_req),
159 { STRUCT_FIELD(bth, psn),
165 static const struct ib_field deth_table[] = {
166 { STRUCT_FIELD(deth, qkey),
174 { STRUCT_FIELD(deth, source_qpn),
181 * ib_ud_header_init - Initialize UD header structure
182 * @payload_bytes:Length of packet payload
183 * @grh_present:GRH flag (if non-zero, GRH will be included)
184 * @header:Structure to initialize
186 * ib_ud_header_init() initializes the lrh.link_version, lrh.link_next_header,
187 * lrh.packet_length, grh.ip_version, grh.payload_length,
188 * grh.next_header, bth.opcode, bth.pad_count and
189 * bth.transport_header_version fields of a &struct ib_ud_header given
190 * the payload length and whether a GRH will be included.
192 void ib_ud_header_init(int payload_bytes,
194 struct ib_ud_header *header)
199 memset(header, 0, sizeof *header);
206 header_len += IB_GRH_BYTES;
209 header->lrh.link_version = 0;
210 header->lrh.link_next_header =
211 grh_present ? IB_LNH_IBA_GLOBAL : IB_LNH_IBA_LOCAL;
212 packet_length = (IB_LRH_BYTES +
217 3) / 4; /* round up */
219 header->grh_present = grh_present;
221 packet_length += IB_GRH_BYTES / 4;
222 header->grh.ip_version = 6;
223 header->grh.payload_length =
224 cpu_to_be16((IB_BTH_BYTES +
228 3) & ~3); /* round up */
229 header->grh.next_header = 0x1b;
232 header->lrh.packet_length = cpu_to_be16(packet_length);
234 if (header->immediate_present)
235 header->bth.opcode = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE;
237 header->bth.opcode = IB_OPCODE_UD_SEND_ONLY;
238 header->bth.pad_count = (4 - payload_bytes) & 3;
239 header->bth.transport_header_version = 0;
241 EXPORT_SYMBOL(ib_ud_header_init);
244 * ib_ud_header_pack - Pack UD header struct into wire format
245 * @header:UD header struct
246 * @buf:Buffer to pack into
248 * ib_ud_header_pack() packs the UD header structure @header into wire
249 * format in the buffer @buf.
251 int ib_ud_header_pack(struct ib_ud_header *header,
256 ib_pack(lrh_table, ARRAY_SIZE(lrh_table),
260 if (header->grh_present) {
261 ib_pack(grh_table, ARRAY_SIZE(grh_table),
262 &header->grh, buf + len);
266 ib_pack(bth_table, ARRAY_SIZE(bth_table),
267 &header->bth, buf + len);
270 ib_pack(deth_table, ARRAY_SIZE(deth_table),
271 &header->deth, buf + len);
272 len += IB_DETH_BYTES;
274 if (header->immediate_present) {
275 memcpy(buf + len, &header->immediate_data, sizeof header->immediate_data);
276 len += sizeof header->immediate_data;
281 EXPORT_SYMBOL(ib_ud_header_pack);
284 * ib_ud_header_unpack - Unpack UD header struct from wire format
285 * @header:UD header struct
286 * @buf:Buffer to pack into
288 * ib_ud_header_pack() unpacks the UD header structure @header from wire
289 * format in the buffer @buf.
291 int ib_ud_header_unpack(void *buf,
292 struct ib_ud_header *header)
294 ib_unpack(lrh_table, ARRAY_SIZE(lrh_table),
298 if (header->lrh.link_version != 0) {
299 printk(KERN_WARNING "Invalid LRH.link_version %d\n",
300 header->lrh.link_version);
304 switch (header->lrh.link_next_header) {
305 case IB_LNH_IBA_LOCAL:
306 header->grh_present = 0;
309 case IB_LNH_IBA_GLOBAL:
310 header->grh_present = 1;
311 ib_unpack(grh_table, ARRAY_SIZE(grh_table),
315 if (header->grh.ip_version != 6) {
316 printk(KERN_WARNING "Invalid GRH.ip_version %d\n",
317 header->grh.ip_version);
320 if (header->grh.next_header != 0x1b) {
321 printk(KERN_WARNING "Invalid GRH.next_header 0x%02x\n",
322 header->grh.next_header);
328 printk(KERN_WARNING "Invalid LRH.link_next_header %d\n",
329 header->lrh.link_next_header);
333 ib_unpack(bth_table, ARRAY_SIZE(bth_table),
337 switch (header->bth.opcode) {
338 case IB_OPCODE_UD_SEND_ONLY:
339 header->immediate_present = 0;
341 case IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE:
342 header->immediate_present = 1;
345 printk(KERN_WARNING "Invalid BTH.opcode 0x%02x\n",
350 if (header->bth.transport_header_version != 0) {
351 printk(KERN_WARNING "Invalid BTH.transport_header_version %d\n",
352 header->bth.transport_header_version);
356 ib_unpack(deth_table, ARRAY_SIZE(deth_table),
358 buf += IB_DETH_BYTES;
360 if (header->immediate_present)
361 memcpy(&header->immediate_data, buf, sizeof header->immediate_data);
365 EXPORT_SYMBOL(ib_ud_header_unpack);