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>
36 #include <linux/if_ether.h>
38 #include <rdma/ib_pack.h>
40 #define STRUCT_FIELD(header, field) \
41 .struct_offset_bytes = offsetof(struct ib_unpacked_ ## header, field), \
42 .struct_size_bytes = sizeof ((struct ib_unpacked_ ## header *) 0)->field, \
43 .field_name = #header ":" #field
45 static const struct ib_field lrh_table[] = {
46 { STRUCT_FIELD(lrh, virtual_lane),
50 { STRUCT_FIELD(lrh, link_version),
54 { STRUCT_FIELD(lrh, service_level),
62 { STRUCT_FIELD(lrh, link_next_header),
66 { STRUCT_FIELD(lrh, destination_lid),
74 { STRUCT_FIELD(lrh, packet_length),
78 { STRUCT_FIELD(lrh, source_lid),
84 static const struct ib_field eth_table[] = {
85 { STRUCT_FIELD(eth, dmac_h),
89 { STRUCT_FIELD(eth, dmac_l),
93 { STRUCT_FIELD(eth, smac_h),
97 { STRUCT_FIELD(eth, smac_l),
101 { STRUCT_FIELD(eth, type),
107 static const struct ib_field vlan_table[] = {
108 { STRUCT_FIELD(vlan, tag),
112 { STRUCT_FIELD(vlan, type),
118 static const struct ib_field grh_table[] = {
119 { STRUCT_FIELD(grh, ip_version),
123 { STRUCT_FIELD(grh, traffic_class),
127 { STRUCT_FIELD(grh, flow_label),
131 { STRUCT_FIELD(grh, payload_length),
135 { STRUCT_FIELD(grh, next_header),
139 { STRUCT_FIELD(grh, hop_limit),
143 { STRUCT_FIELD(grh, source_gid),
147 { STRUCT_FIELD(grh, destination_gid),
153 static const struct ib_field bth_table[] = {
154 { STRUCT_FIELD(bth, opcode),
158 { STRUCT_FIELD(bth, solicited_event),
162 { STRUCT_FIELD(bth, mig_req),
166 { STRUCT_FIELD(bth, pad_count),
170 { STRUCT_FIELD(bth, transport_header_version),
174 { STRUCT_FIELD(bth, pkey),
182 { STRUCT_FIELD(bth, destination_qpn),
186 { STRUCT_FIELD(bth, ack_req),
194 { STRUCT_FIELD(bth, psn),
200 static const struct ib_field deth_table[] = {
201 { STRUCT_FIELD(deth, qkey),
209 { STRUCT_FIELD(deth, source_qpn),
216 * ib_ud_header_init - Initialize UD header structure
217 * @payload_bytes:Length of packet payload
218 * @lrh_present: specify if LRH is present
219 * @eth_present: specify if Eth header is present
220 * @vlan_present: packet is tagged vlan
221 * @grh_present:GRH flag (if non-zero, GRH will be included)
222 * @immediate_present: specify if immediate data is present
223 * @header:Structure to initialize
225 void ib_ud_header_init(int payload_bytes,
230 int immediate_present,
231 struct ib_ud_header *header)
233 memset(header, 0, sizeof *header);
238 header->lrh.link_version = 0;
239 header->lrh.link_next_header =
240 grh_present ? IB_LNH_IBA_GLOBAL : IB_LNH_IBA_LOCAL;
241 packet_length = (IB_LRH_BYTES +
244 (grh_present ? IB_GRH_BYTES : 0) +
247 3) / 4; /* round up */
248 header->lrh.packet_length = cpu_to_be16(packet_length);
252 header->eth.type = cpu_to_be16(ETH_P_8021Q);
255 header->grh.ip_version = 6;
256 header->grh.payload_length =
257 cpu_to_be16((IB_BTH_BYTES +
261 3) & ~3); /* round up */
262 header->grh.next_header = 0x1b;
265 if (immediate_present)
266 header->bth.opcode = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE;
268 header->bth.opcode = IB_OPCODE_UD_SEND_ONLY;
269 header->bth.pad_count = (4 - payload_bytes) & 3;
270 header->bth.transport_header_version = 0;
272 header->lrh_present = lrh_present;
273 header->eth_present = eth_present;
274 header->vlan_present = vlan_present;
275 header->grh_present = grh_present;
276 header->immediate_present = immediate_present;
278 EXPORT_SYMBOL(ib_ud_header_init);
281 * ib_lrh_header_pack - Pack LRH header struct into wire format
282 * @lrh:unpacked LRH header struct
283 * @buf:Buffer to pack into
285 * ib_lrh_header_pack() packs the LRH header structure @lrh into
286 * wire format in the buffer @buf.
288 int ib_lrh_header_pack(struct ib_unpacked_lrh *lrh, void *buf)
290 ib_pack(lrh_table, ARRAY_SIZE(lrh_table), lrh, buf);
293 EXPORT_SYMBOL(ib_lrh_header_pack);
296 * ib_lrh_header_unpack - Unpack LRH structure from wire format
297 * @lrh:unpacked LRH header struct
298 * @buf:Buffer to pack into
300 * ib_lrh_header_unpack() unpacks the LRH header structure from
301 * wire format (in buf) into @lrh.
303 int ib_lrh_header_unpack(void *buf, struct ib_unpacked_lrh *lrh)
305 ib_unpack(lrh_table, ARRAY_SIZE(lrh_table), buf, lrh);
308 EXPORT_SYMBOL(ib_lrh_header_unpack);
311 * ib_ud_header_pack - Pack UD header struct into wire format
312 * @header:UD header struct
313 * @buf:Buffer to pack into
315 * ib_ud_header_pack() packs the UD header structure @header into wire
316 * format in the buffer @buf.
318 int ib_ud_header_pack(struct ib_ud_header *header,
323 if (header->lrh_present) {
324 ib_pack(lrh_table, ARRAY_SIZE(lrh_table),
325 &header->lrh, buf + len);
328 if (header->eth_present) {
329 ib_pack(eth_table, ARRAY_SIZE(eth_table),
330 &header->eth, buf + len);
333 if (header->vlan_present) {
334 ib_pack(vlan_table, ARRAY_SIZE(vlan_table),
335 &header->vlan, buf + len);
336 len += IB_VLAN_BYTES;
338 if (header->grh_present) {
339 ib_pack(grh_table, ARRAY_SIZE(grh_table),
340 &header->grh, buf + len);
344 ib_pack(bth_table, ARRAY_SIZE(bth_table),
345 &header->bth, buf + len);
348 ib_pack(deth_table, ARRAY_SIZE(deth_table),
349 &header->deth, buf + len);
350 len += IB_DETH_BYTES;
352 if (header->immediate_present) {
353 memcpy(buf + len, &header->immediate_data, sizeof header->immediate_data);
354 len += sizeof header->immediate_data;
359 EXPORT_SYMBOL(ib_ud_header_pack);
362 * ib_ud_header_unpack - Unpack UD header struct from wire format
363 * @header:UD header struct
364 * @buf:Buffer to pack into
366 * ib_ud_header_pack() unpacks the UD header structure @header from wire
367 * format in the buffer @buf.
369 int ib_ud_header_unpack(void *buf,
370 struct ib_ud_header *header)
372 ib_unpack(lrh_table, ARRAY_SIZE(lrh_table),
376 if (header->lrh.link_version != 0) {
377 printk(KERN_WARNING "Invalid LRH.link_version %d\n",
378 header->lrh.link_version);
382 switch (header->lrh.link_next_header) {
383 case IB_LNH_IBA_LOCAL:
384 header->grh_present = 0;
387 case IB_LNH_IBA_GLOBAL:
388 header->grh_present = 1;
389 ib_unpack(grh_table, ARRAY_SIZE(grh_table),
393 if (header->grh.ip_version != 6) {
394 printk(KERN_WARNING "Invalid GRH.ip_version %d\n",
395 header->grh.ip_version);
398 if (header->grh.next_header != 0x1b) {
399 printk(KERN_WARNING "Invalid GRH.next_header 0x%02x\n",
400 header->grh.next_header);
406 printk(KERN_WARNING "Invalid LRH.link_next_header %d\n",
407 header->lrh.link_next_header);
411 ib_unpack(bth_table, ARRAY_SIZE(bth_table),
415 switch (header->bth.opcode) {
416 case IB_OPCODE_UD_SEND_ONLY:
417 header->immediate_present = 0;
419 case IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE:
420 header->immediate_present = 1;
423 printk(KERN_WARNING "Invalid BTH.opcode 0x%02x\n",
428 if (header->bth.transport_header_version != 0) {
429 printk(KERN_WARNING "Invalid BTH.transport_header_version %d\n",
430 header->bth.transport_header_version);
434 ib_unpack(deth_table, ARRAY_SIZE(deth_table),
436 buf += IB_DETH_BYTES;
438 if (header->immediate_present)
439 memcpy(&header->immediate_data, buf, sizeof header->immediate_data);
443 EXPORT_SYMBOL(ib_ud_header_unpack);