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 34//#include <linux/string.h> 35#include <linux/if_ether.h> 36#include <linux/module.h> 37#include <linux/kernel.h> 38 39#include <asm/byteorder.h> 40 41#include <rdma/ib_pack.h> 42 43#define STRUCT_FIELD(header, field) \ 44 .struct_offset_bytes = offsetof(struct ib_unpacked_ ## header, field), \ 45 .struct_size_bytes = sizeof ((struct ib_unpacked_ ## header *) 0)->field, \ 46 .field_name = #header ":" #field 47 48static const struct ib_field lrh_table[] = { { STRUCT_FIELD(lrh, virtual_lane), 49 .offset_words = 0, .offset_bits = 0, .size_bits = 4 }, { 50STRUCT_FIELD(lrh, link_version), .offset_words = 0, .offset_bits = 4, 51 .size_bits = 4 }, { STRUCT_FIELD(lrh, service_level), .offset_words = 0, 52 .offset_bits = 8, .size_bits = 4 }, { RESERVED, .offset_words = 0, 53 .offset_bits = 12, .size_bits = 2 }, { 54STRUCT_FIELD(lrh, link_next_header), .offset_words = 0, .offset_bits = 14, 55 .size_bits = 2 }, { STRUCT_FIELD(lrh, destination_lid), .offset_words = 56 0, .offset_bits = 16, .size_bits = 16 }, { RESERVED, .offset_words = 1, 57 .offset_bits = 0, .size_bits = 5 }, { 58STRUCT_FIELD(lrh, packet_length), .offset_words = 1, .offset_bits = 5, 59 .size_bits = 11 }, { STRUCT_FIELD(lrh, source_lid), .offset_words = 1, 60 .offset_bits = 16, .size_bits = 16 } }; 61 62static const struct ib_field eth_table[] = { { STRUCT_FIELD(eth, dmac_h), 63 .offset_words = 0, .offset_bits = 0, .size_bits = 32 }, 64 { 65 STRUCT_FIELD(eth, dmac_l), .offset_words = 1, .offset_bits = 0, 66 .size_bits = 16 }, { STRUCT_FIELD(eth, smac_h), .offset_words = 67 1, .offset_bits = 16, .size_bits = 16 }, { 68 STRUCT_FIELD(eth, smac_l), .offset_words = 2, .offset_bits = 0, 69 .size_bits = 32 }, { 70 STRUCT_FIELD(eth, type), .offset_words = 3, .offset_bits = 0, 71 .size_bits = 16 } }; 72 73static const struct ib_field vlan_table[] = { { STRUCT_FIELD(vlan, tag), 74 .offset_words = 0, .offset_bits = 0, .size_bits = 16 }, 75 { 76 STRUCT_FIELD(vlan, type), .offset_words = 0, .offset_bits = 16, 77 .size_bits = 16 } }; 78 79static const struct ib_field grh_table[] = { { STRUCT_FIELD(grh, ip_version), 80 .offset_words = 0, .offset_bits = 0, .size_bits = 4 }, { 81STRUCT_FIELD(grh, traffic_class), .offset_words = 0, .offset_bits = 4, 82 .size_bits = 8 }, { STRUCT_FIELD(grh, flow_label), .offset_words = 0, 83 .offset_bits = 12, .size_bits = 20 }, { 84STRUCT_FIELD(grh, payload_length), .offset_words = 1, .offset_bits = 0, 85 .size_bits = 16 }, { STRUCT_FIELD(grh, next_header), .offset_words = 1, 86 .offset_bits = 16, .size_bits = 8 }, { STRUCT_FIELD(grh, hop_limit), 87 .offset_words = 1, .offset_bits = 24, .size_bits = 8 }, { 88STRUCT_FIELD(grh, source_gid), .offset_words = 2, .offset_bits = 0, .size_bits = 89 128 }, { STRUCT_FIELD(grh, destination_gid), .offset_words = 6, 90 .offset_bits = 0, .size_bits = 128 } }; 91 92static const struct ib_field bth_table[] = { { STRUCT_FIELD(bth, opcode), 93 .offset_words = 0, .offset_bits = 0, .size_bits = 8 }, { 94STRUCT_FIELD(bth, solicited_event), .offset_words = 0, .offset_bits = 8, 95 .size_bits = 1 }, { STRUCT_FIELD(bth, mig_req), .offset_words = 0, 96 .offset_bits = 9, .size_bits = 1 }, { STRUCT_FIELD(bth, pad_count), 97 .offset_words = 0, .offset_bits = 10, .size_bits = 2 }, { 98STRUCT_FIELD(bth, transport_header_version), .offset_words = 0, .offset_bits = 99 12, .size_bits = 4 }, { STRUCT_FIELD(bth, pkey), .offset_words = 0, 100 .offset_bits = 16, .size_bits = 16 }, { RESERVED, .offset_words = 1, 101 .offset_bits = 0, .size_bits = 8 }, { 102STRUCT_FIELD(bth, destination_qpn), .offset_words = 1, .offset_bits = 8, 103 .size_bits = 24 }, { STRUCT_FIELD(bth, ack_req), .offset_words = 2, 104 .offset_bits = 0, .size_bits = 1 }, { RESERVED, .offset_words = 2, 105 .offset_bits = 1, .size_bits = 7 }, { STRUCT_FIELD(bth, psn), 106 .offset_words = 2, .offset_bits = 8, .size_bits = 24 } }; 107 108static const struct ib_field deth_table[] = { { STRUCT_FIELD(deth, qkey), 109 .offset_words = 0, .offset_bits = 0, .size_bits = 32 }, { RESERVED, 110 .offset_words = 1, .offset_bits = 0, .size_bits = 8 }, { 111STRUCT_FIELD(deth, source_qpn), .offset_words = 1, .offset_bits = 8, 112 .size_bits = 24 } }; 113 114/** 115 * ib_ud_header_init - Initialize UD header structure 116 * @payload_bytes:Length of packet payload 117 * @lrh_present: specify if LRH is present 118 * @eth_present: specify if Eth header is present 119 * @vlan_present: packet is tagged vlan 120 * @grh_present:GRH flag (if non-zero, GRH will be included) 121 * @immediate_present: specify if immediate data is present 122 * @header:Structure to initialize 123 */ 124void ib_ud_header_init(int payload_bytes, int lrh_present, int eth_present, 125 int vlan_present, int grh_present, int immediate_present, 126 struct ib_ud_header *header) { 127 u16 packet_length = 0; 128 129 memset(header, 0, sizeof *header); 130 131 if (lrh_present) { 132 header->lrh.link_version = 0; 133 header->lrh.link_next_header = 134 grh_present ? IB_LNH_IBA_GLOBAL : IB_LNH_IBA_LOCAL; 135 packet_length = IB_LRH_BYTES; 136 } 137 138 if (eth_present) { 139 if (vlan_present) { 140 header->eth.type = cpu_to_be16(ETH_P_8021Q); 141 packet_length += IB_VLAN_BYTES; 142 143 } 144 packet_length += IB_ETH_BYTES; 145 } 146 147 packet_length += IB_BTH_BYTES + IB_DETH_BYTES + payload_bytes + 4 + /* ICRC */ 148 3; /* round up */ 149 packet_length /= 4; 150 if (grh_present) { 151 packet_length += IB_GRH_BYTES / 4; 152 header->grh.ip_version = 6; 153 header->grh.payload_length = cpu_to_be16( 154 (IB_BTH_BYTES + IB_DETH_BYTES + payload_bytes + 4 + /* ICRC */ 155 3) & ~3); /* round up */ 156 header->grh.next_header = 0x1b; 157 } 158 159 if (lrh_present) 160 header->lrh.packet_length = cpu_to_be16(packet_length); 161 162 if (immediate_present) 163 header->bth.opcode = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE; 164 else 165 header->bth.opcode = IB_OPCODE_UD_SEND_ONLY; 166 header->bth.pad_count = (4 - payload_bytes) & 3; 167 header->bth.transport_header_version = 0; 168 169 header->lrh_present = lrh_present; 170 header->eth_present = eth_present; 171 header->vlan_present = vlan_present; 172 header->grh_present = grh_present; 173 header->immediate_present = immediate_present; 174} 175EXPORT_SYMBOL(ib_ud_header_init); 176 177/** 178 * ib_lrh_header_pack - Pack LRH header struct into wire format 179 * @lrh:unpacked LRH header struct 180 * @buf:Buffer to pack into 181 * 182 * ib_lrh_header_pack() packs the LRH header structure @lrh into 183 * wire format in the buffer @buf. 184 */ 185int ib_lrh_header_pack(struct ib_unpacked_lrh *lrh, void *buf) { 186 ib_pack(lrh_table, ARRAY_SIZE(lrh_table), lrh, buf); 187 return 0; 188} 189EXPORT_SYMBOL(ib_lrh_header_pack); 190 191/** 192 * ib_lrh_header_unpack - Unpack LRH structure from wire format 193 * @lrh:unpacked LRH header struct 194 * @buf:Buffer to pack into 195 * 196 * ib_lrh_header_unpack() unpacks the LRH header structure from 197 * wire format (in buf) into @lrh. 198 */ 199int ib_lrh_header_unpack(void *buf, struct ib_unpacked_lrh *lrh) { 200 ib_unpack(lrh_table, ARRAY_SIZE(lrh_table), buf, lrh); 201 return 0; 202} 203EXPORT_SYMBOL(ib_lrh_header_unpack); 204 205/** 206 * ib_ud_header_pack - Pack UD header struct into wire format 207 * @header:UD header struct 208 * @buf:Buffer to pack into 209 * 210 * ib_ud_header_pack() packs the UD header structure @header into wire 211 * format in the buffer @buf. 212 */ 213int ib_ud_header_pack(struct ib_ud_header *header, void *buf) { 214 int len = 0; 215 216 if (header->lrh_present) { 217 ib_pack(lrh_table, ARRAY_SIZE(lrh_table), &header->lrh, buf + len); 218 len += IB_LRH_BYTES; 219 } 220 if (header->eth_present) { 221 ib_pack(eth_table, ARRAY_SIZE(eth_table), &header->eth, buf + len); 222 len += IB_ETH_BYTES; 223 } 224 225 if (header->vlan_present) { 226 ib_pack(vlan_table, ARRAY_SIZE(vlan_table), &header->vlan, buf + len); 227 len += IB_VLAN_BYTES; 228 } 229 230 if (header->grh_present) { 231 ib_pack(grh_table, ARRAY_SIZE(grh_table), &header->grh, buf + len); 232 len += IB_GRH_BYTES; 233 } 234 235 ib_pack(bth_table, ARRAY_SIZE(bth_table), &header->bth, buf + len); 236 len += IB_BTH_BYTES; 237 238 ib_pack(deth_table, ARRAY_SIZE(deth_table), &header->deth, buf + len); 239 len += IB_DETH_BYTES; 240 241 if (header->immediate_present) { 242 memcpy(buf + len, &header->immediate_data, 243 sizeof header->immediate_data); 244 len += sizeof header->immediate_data; 245 } 246 247 return len; 248} 249EXPORT_SYMBOL(ib_ud_header_pack); 250 251/** 252 * ib_ud_header_unpack - Unpack UD header struct from wire format 253 * @header:UD header struct 254 * @buf:Buffer to pack into 255 * 256 * ib_ud_header_pack() unpacks the UD header structure @header from wire 257 * format in the buffer @buf. 258 */ 259int ib_ud_header_unpack(void *buf, struct ib_ud_header *header) { 260 ib_unpack(lrh_table, ARRAY_SIZE(lrh_table), buf, &header->lrh); 261 buf += IB_LRH_BYTES; 262 263 if (header->lrh.link_version != 0) { 264 printf("Invalid LRH.link_version %d\n", header->lrh.link_version); 265 return -EINVAL; 266 } 267 268 switch (header->lrh.link_next_header) { 269 case IB_LNH_IBA_LOCAL: 270 header->grh_present = 0; 271 break; 272 273 case IB_LNH_IBA_GLOBAL: 274 header->grh_present = 1; 275 ib_unpack(grh_table, ARRAY_SIZE(grh_table), buf, &header->grh); 276 buf += IB_GRH_BYTES; 277 278 if (header->grh.ip_version != 6) { 279 printf("Invalid GRH.ip_version %d\n", header->grh.ip_version); 280 return -EINVAL; 281 } 282 if (header->grh.next_header != 0x1b) { 283 printf("Invalid GRH.next_header 0x%02x\n", header->grh.next_header); 284 return -EINVAL; 285 } 286 break; 287 288 default: 289 printf("Invalid LRH.link_next_header %d\n", 290 header->lrh.link_next_header); 291 return -EINVAL; 292 } 293 294 ib_unpack(bth_table, ARRAY_SIZE(bth_table), buf, &header->bth); 295 buf += IB_BTH_BYTES; 296 297 switch (header->bth.opcode) { 298 case IB_OPCODE_UD_SEND_ONLY: 299 header->immediate_present = 0; 300 break; 301 case IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE: 302 header->immediate_present = 1; 303 break; 304 default: 305 printf("Invalid BTH.opcode 0x%02x\n", header->bth.opcode); 306 return -EINVAL; 307 } 308 309 if (header->bth.transport_header_version != 0) { 310 printf("Invalid BTH.transport_header_version %d\n", 311 header->bth.transport_header_version); 312 return -EINVAL; 313 } 314 315 ib_unpack(deth_table, ARRAY_SIZE(deth_table), buf, &header->deth); 316 buf += IB_DETH_BYTES; 317 318 if (header->immediate_present) 319 memcpy(&header->immediate_data, buf, sizeof header->immediate_data); 320 321 return 0; 322} 323EXPORT_SYMBOL(ib_ud_header_unpack); 324