Path: blob/master/drivers/infiniband/core/ud_header.c
37212 views
/*1* Copyright (c) 2004 Topspin Corporation. All rights reserved.2* Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.3*4* This software is available to you under a choice of one of two5* licenses. You may choose to be licensed under the terms of the GNU6* General Public License (GPL) Version 2, available from the file7* COPYING in the main directory of this source tree, or the8* OpenIB.org BSD license below:9*10* Redistribution and use in source and binary forms, with or11* without modification, are permitted provided that the following12* conditions are met:13*14* - Redistributions of source code must retain the above15* copyright notice, this list of conditions and the following16* disclaimer.17*18* - Redistributions in binary form must reproduce the above19* copyright notice, this list of conditions and the following20* disclaimer in the documentation and/or other materials21* 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 OF25* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND26* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS27* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN28* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN29* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE30* SOFTWARE.31*/3233#include <linux/errno.h>34#include <linux/string.h>35#include <linux/if_ether.h>3637#include <rdma/ib_pack.h>3839#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 ":" #field4344static const struct ib_field lrh_table[] = {45{ STRUCT_FIELD(lrh, virtual_lane),46.offset_words = 0,47.offset_bits = 0,48.size_bits = 4 },49{ STRUCT_FIELD(lrh, link_version),50.offset_words = 0,51.offset_bits = 4,52.size_bits = 4 },53{ STRUCT_FIELD(lrh, service_level),54.offset_words = 0,55.offset_bits = 8,56.size_bits = 4 },57{ RESERVED,58.offset_words = 0,59.offset_bits = 12,60.size_bits = 2 },61{ STRUCT_FIELD(lrh, link_next_header),62.offset_words = 0,63.offset_bits = 14,64.size_bits = 2 },65{ STRUCT_FIELD(lrh, destination_lid),66.offset_words = 0,67.offset_bits = 16,68.size_bits = 16 },69{ RESERVED,70.offset_words = 1,71.offset_bits = 0,72.size_bits = 5 },73{ STRUCT_FIELD(lrh, packet_length),74.offset_words = 1,75.offset_bits = 5,76.size_bits = 11 },77{ STRUCT_FIELD(lrh, source_lid),78.offset_words = 1,79.offset_bits = 16,80.size_bits = 16 }81};8283static const struct ib_field eth_table[] = {84{ STRUCT_FIELD(eth, dmac_h),85.offset_words = 0,86.offset_bits = 0,87.size_bits = 32 },88{ STRUCT_FIELD(eth, dmac_l),89.offset_words = 1,90.offset_bits = 0,91.size_bits = 16 },92{ STRUCT_FIELD(eth, smac_h),93.offset_words = 1,94.offset_bits = 16,95.size_bits = 16 },96{ STRUCT_FIELD(eth, smac_l),97.offset_words = 2,98.offset_bits = 0,99.size_bits = 32 },100{ STRUCT_FIELD(eth, type),101.offset_words = 3,102.offset_bits = 0,103.size_bits = 16 }104};105106static const struct ib_field vlan_table[] = {107{ STRUCT_FIELD(vlan, tag),108.offset_words = 0,109.offset_bits = 0,110.size_bits = 16 },111{ STRUCT_FIELD(vlan, type),112.offset_words = 0,113.offset_bits = 16,114.size_bits = 16 }115};116117static const struct ib_field grh_table[] = {118{ STRUCT_FIELD(grh, ip_version),119.offset_words = 0,120.offset_bits = 0,121.size_bits = 4 },122{ STRUCT_FIELD(grh, traffic_class),123.offset_words = 0,124.offset_bits = 4,125.size_bits = 8 },126{ STRUCT_FIELD(grh, flow_label),127.offset_words = 0,128.offset_bits = 12,129.size_bits = 20 },130{ STRUCT_FIELD(grh, payload_length),131.offset_words = 1,132.offset_bits = 0,133.size_bits = 16 },134{ STRUCT_FIELD(grh, next_header),135.offset_words = 1,136.offset_bits = 16,137.size_bits = 8 },138{ STRUCT_FIELD(grh, hop_limit),139.offset_words = 1,140.offset_bits = 24,141.size_bits = 8 },142{ STRUCT_FIELD(grh, source_gid),143.offset_words = 2,144.offset_bits = 0,145.size_bits = 128 },146{ STRUCT_FIELD(grh, destination_gid),147.offset_words = 6,148.offset_bits = 0,149.size_bits = 128 }150};151152static const struct ib_field bth_table[] = {153{ STRUCT_FIELD(bth, opcode),154.offset_words = 0,155.offset_bits = 0,156.size_bits = 8 },157{ STRUCT_FIELD(bth, solicited_event),158.offset_words = 0,159.offset_bits = 8,160.size_bits = 1 },161{ STRUCT_FIELD(bth, mig_req),162.offset_words = 0,163.offset_bits = 9,164.size_bits = 1 },165{ STRUCT_FIELD(bth, pad_count),166.offset_words = 0,167.offset_bits = 10,168.size_bits = 2 },169{ STRUCT_FIELD(bth, transport_header_version),170.offset_words = 0,171.offset_bits = 12,172.size_bits = 4 },173{ STRUCT_FIELD(bth, pkey),174.offset_words = 0,175.offset_bits = 16,176.size_bits = 16 },177{ RESERVED,178.offset_words = 1,179.offset_bits = 0,180.size_bits = 8 },181{ STRUCT_FIELD(bth, destination_qpn),182.offset_words = 1,183.offset_bits = 8,184.size_bits = 24 },185{ STRUCT_FIELD(bth, ack_req),186.offset_words = 2,187.offset_bits = 0,188.size_bits = 1 },189{ RESERVED,190.offset_words = 2,191.offset_bits = 1,192.size_bits = 7 },193{ STRUCT_FIELD(bth, psn),194.offset_words = 2,195.offset_bits = 8,196.size_bits = 24 }197};198199static const struct ib_field deth_table[] = {200{ STRUCT_FIELD(deth, qkey),201.offset_words = 0,202.offset_bits = 0,203.size_bits = 32 },204{ RESERVED,205.offset_words = 1,206.offset_bits = 0,207.size_bits = 8 },208{ STRUCT_FIELD(deth, source_qpn),209.offset_words = 1,210.offset_bits = 8,211.size_bits = 24 }212};213214/**215* ib_ud_header_init - Initialize UD header structure216* @payload_bytes:Length of packet payload217* @lrh_present: specify if LRH is present218* @eth_present: specify if Eth header is present219* @vlan_present: packet is tagged vlan220* @grh_present:GRH flag (if non-zero, GRH will be included)221* @immediate_present: specify if immediate data is present222* @header:Structure to initialize223*/224void ib_ud_header_init(int payload_bytes,225int lrh_present,226int eth_present,227int vlan_present,228int grh_present,229int immediate_present,230struct ib_ud_header *header)231{232memset(header, 0, sizeof *header);233234if (lrh_present) {235u16 packet_length;236237header->lrh.link_version = 0;238header->lrh.link_next_header =239grh_present ? IB_LNH_IBA_GLOBAL : IB_LNH_IBA_LOCAL;240packet_length = (IB_LRH_BYTES +241IB_BTH_BYTES +242IB_DETH_BYTES +243(grh_present ? IB_GRH_BYTES : 0) +244payload_bytes +2454 + /* ICRC */2463) / 4; /* round up */247header->lrh.packet_length = cpu_to_be16(packet_length);248}249250if (vlan_present)251header->eth.type = cpu_to_be16(ETH_P_8021Q);252253if (grh_present) {254header->grh.ip_version = 6;255header->grh.payload_length =256cpu_to_be16((IB_BTH_BYTES +257IB_DETH_BYTES +258payload_bytes +2594 + /* ICRC */2603) & ~3); /* round up */261header->grh.next_header = 0x1b;262}263264if (immediate_present)265header->bth.opcode = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE;266else267header->bth.opcode = IB_OPCODE_UD_SEND_ONLY;268header->bth.pad_count = (4 - payload_bytes) & 3;269header->bth.transport_header_version = 0;270271header->lrh_present = lrh_present;272header->eth_present = eth_present;273header->vlan_present = vlan_present;274header->grh_present = grh_present;275header->immediate_present = immediate_present;276}277EXPORT_SYMBOL(ib_ud_header_init);278279/**280* ib_ud_header_pack - Pack UD header struct into wire format281* @header:UD header struct282* @buf:Buffer to pack into283*284* ib_ud_header_pack() packs the UD header structure @header into wire285* format in the buffer @buf.286*/287int ib_ud_header_pack(struct ib_ud_header *header,288void *buf)289{290int len = 0;291292if (header->lrh_present) {293ib_pack(lrh_table, ARRAY_SIZE(lrh_table),294&header->lrh, buf + len);295len += IB_LRH_BYTES;296}297if (header->eth_present) {298ib_pack(eth_table, ARRAY_SIZE(eth_table),299&header->eth, buf + len);300len += IB_ETH_BYTES;301}302if (header->vlan_present) {303ib_pack(vlan_table, ARRAY_SIZE(vlan_table),304&header->vlan, buf + len);305len += IB_VLAN_BYTES;306}307if (header->grh_present) {308ib_pack(grh_table, ARRAY_SIZE(grh_table),309&header->grh, buf + len);310len += IB_GRH_BYTES;311}312313ib_pack(bth_table, ARRAY_SIZE(bth_table),314&header->bth, buf + len);315len += IB_BTH_BYTES;316317ib_pack(deth_table, ARRAY_SIZE(deth_table),318&header->deth, buf + len);319len += IB_DETH_BYTES;320321if (header->immediate_present) {322memcpy(buf + len, &header->immediate_data, sizeof header->immediate_data);323len += sizeof header->immediate_data;324}325326return len;327}328EXPORT_SYMBOL(ib_ud_header_pack);329330/**331* ib_ud_header_unpack - Unpack UD header struct from wire format332* @header:UD header struct333* @buf:Buffer to pack into334*335* ib_ud_header_pack() unpacks the UD header structure @header from wire336* format in the buffer @buf.337*/338int ib_ud_header_unpack(void *buf,339struct ib_ud_header *header)340{341ib_unpack(lrh_table, ARRAY_SIZE(lrh_table),342buf, &header->lrh);343buf += IB_LRH_BYTES;344345if (header->lrh.link_version != 0) {346printk(KERN_WARNING "Invalid LRH.link_version %d\n",347header->lrh.link_version);348return -EINVAL;349}350351switch (header->lrh.link_next_header) {352case IB_LNH_IBA_LOCAL:353header->grh_present = 0;354break;355356case IB_LNH_IBA_GLOBAL:357header->grh_present = 1;358ib_unpack(grh_table, ARRAY_SIZE(grh_table),359buf, &header->grh);360buf += IB_GRH_BYTES;361362if (header->grh.ip_version != 6) {363printk(KERN_WARNING "Invalid GRH.ip_version %d\n",364header->grh.ip_version);365return -EINVAL;366}367if (header->grh.next_header != 0x1b) {368printk(KERN_WARNING "Invalid GRH.next_header 0x%02x\n",369header->grh.next_header);370return -EINVAL;371}372break;373374default:375printk(KERN_WARNING "Invalid LRH.link_next_header %d\n",376header->lrh.link_next_header);377return -EINVAL;378}379380ib_unpack(bth_table, ARRAY_SIZE(bth_table),381buf, &header->bth);382buf += IB_BTH_BYTES;383384switch (header->bth.opcode) {385case IB_OPCODE_UD_SEND_ONLY:386header->immediate_present = 0;387break;388case IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE:389header->immediate_present = 1;390break;391default:392printk(KERN_WARNING "Invalid BTH.opcode 0x%02x\n",393header->bth.opcode);394return -EINVAL;395}396397if (header->bth.transport_header_version != 0) {398printk(KERN_WARNING "Invalid BTH.transport_header_version %d\n",399header->bth.transport_header_version);400return -EINVAL;401}402403ib_unpack(deth_table, ARRAY_SIZE(deth_table),404buf, &header->deth);405buf += IB_DETH_BYTES;406407if (header->immediate_present)408memcpy(&header->immediate_data, buf, sizeof header->immediate_data);409410return 0;411}412EXPORT_SYMBOL(ib_ud_header_unpack);413414415