Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2004 Topspin Corporation. All rights reserved. |
Roland Dreier | 2a1d9b7 | 2005-08-10 23:03:10 -0700 | [diff] [blame] | 3 | * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | */ |
| 33 | |
| 34 | #include <linux/errno.h> |
Tim Schmielau | 8c65b4a | 2005-11-07 00:59:43 -0800 | [diff] [blame] | 35 | #include <linux/string.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
Roland Dreier | a4d61e8 | 2005-08-25 13:40:04 -0700 | [diff] [blame] | 37 | #include <rdma/ib_pack.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
| 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 |
| 43 | |
| 44 | static 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 | }; |
| 82 | |
| 83 | static const struct ib_field grh_table[] = { |
| 84 | { STRUCT_FIELD(grh, ip_version), |
| 85 | .offset_words = 0, |
| 86 | .offset_bits = 0, |
| 87 | .size_bits = 4 }, |
| 88 | { STRUCT_FIELD(grh, traffic_class), |
| 89 | .offset_words = 0, |
| 90 | .offset_bits = 4, |
| 91 | .size_bits = 8 }, |
| 92 | { STRUCT_FIELD(grh, flow_label), |
| 93 | .offset_words = 0, |
| 94 | .offset_bits = 12, |
| 95 | .size_bits = 20 }, |
| 96 | { STRUCT_FIELD(grh, payload_length), |
| 97 | .offset_words = 1, |
| 98 | .offset_bits = 0, |
| 99 | .size_bits = 16 }, |
| 100 | { STRUCT_FIELD(grh, next_header), |
| 101 | .offset_words = 1, |
| 102 | .offset_bits = 16, |
| 103 | .size_bits = 8 }, |
| 104 | { STRUCT_FIELD(grh, hop_limit), |
| 105 | .offset_words = 1, |
| 106 | .offset_bits = 24, |
| 107 | .size_bits = 8 }, |
| 108 | { STRUCT_FIELD(grh, source_gid), |
| 109 | .offset_words = 2, |
| 110 | .offset_bits = 0, |
| 111 | .size_bits = 128 }, |
| 112 | { STRUCT_FIELD(grh, destination_gid), |
| 113 | .offset_words = 6, |
| 114 | .offset_bits = 0, |
| 115 | .size_bits = 128 } |
| 116 | }; |
| 117 | |
| 118 | static const struct ib_field bth_table[] = { |
| 119 | { STRUCT_FIELD(bth, opcode), |
| 120 | .offset_words = 0, |
| 121 | .offset_bits = 0, |
| 122 | .size_bits = 8 }, |
| 123 | { STRUCT_FIELD(bth, solicited_event), |
| 124 | .offset_words = 0, |
| 125 | .offset_bits = 8, |
| 126 | .size_bits = 1 }, |
| 127 | { STRUCT_FIELD(bth, mig_req), |
| 128 | .offset_words = 0, |
| 129 | .offset_bits = 9, |
| 130 | .size_bits = 1 }, |
| 131 | { STRUCT_FIELD(bth, pad_count), |
| 132 | .offset_words = 0, |
| 133 | .offset_bits = 10, |
| 134 | .size_bits = 2 }, |
| 135 | { STRUCT_FIELD(bth, transport_header_version), |
| 136 | .offset_words = 0, |
| 137 | .offset_bits = 12, |
| 138 | .size_bits = 4 }, |
| 139 | { STRUCT_FIELD(bth, pkey), |
| 140 | .offset_words = 0, |
| 141 | .offset_bits = 16, |
| 142 | .size_bits = 16 }, |
| 143 | { RESERVED, |
| 144 | .offset_words = 1, |
| 145 | .offset_bits = 0, |
| 146 | .size_bits = 8 }, |
| 147 | { STRUCT_FIELD(bth, destination_qpn), |
| 148 | .offset_words = 1, |
| 149 | .offset_bits = 8, |
| 150 | .size_bits = 24 }, |
| 151 | { STRUCT_FIELD(bth, ack_req), |
| 152 | .offset_words = 2, |
| 153 | .offset_bits = 0, |
| 154 | .size_bits = 1 }, |
| 155 | { RESERVED, |
| 156 | .offset_words = 2, |
| 157 | .offset_bits = 1, |
| 158 | .size_bits = 7 }, |
| 159 | { STRUCT_FIELD(bth, psn), |
| 160 | .offset_words = 2, |
| 161 | .offset_bits = 8, |
| 162 | .size_bits = 24 } |
| 163 | }; |
| 164 | |
| 165 | static const struct ib_field deth_table[] = { |
| 166 | { STRUCT_FIELD(deth, qkey), |
| 167 | .offset_words = 0, |
| 168 | .offset_bits = 0, |
| 169 | .size_bits = 32 }, |
| 170 | { RESERVED, |
| 171 | .offset_words = 1, |
| 172 | .offset_bits = 0, |
| 173 | .size_bits = 8 }, |
| 174 | { STRUCT_FIELD(deth, source_qpn), |
| 175 | .offset_words = 1, |
| 176 | .offset_bits = 8, |
| 177 | .size_bits = 24 } |
| 178 | }; |
| 179 | |
| 180 | /** |
| 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) |
Eli Cohen | 920d706 | 2010-02-08 11:40:37 +0000 | [diff] [blame] | 184 | * @immediate_present: specify if immediate data should be used |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | * @header:Structure to initialize |
| 186 | * |
| 187 | * ib_ud_header_init() initializes the lrh.link_version, lrh.link_next_header, |
| 188 | * lrh.packet_length, grh.ip_version, grh.payload_length, |
| 189 | * grh.next_header, bth.opcode, bth.pad_count and |
| 190 | * bth.transport_header_version fields of a &struct ib_ud_header given |
| 191 | * the payload length and whether a GRH will be included. |
| 192 | */ |
| 193 | void ib_ud_header_init(int payload_bytes, |
| 194 | int grh_present, |
Eli Cohen | 920d706 | 2010-02-08 11:40:37 +0000 | [diff] [blame] | 195 | int immediate_present, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | struct ib_ud_header *header) |
| 197 | { |
Sean Hefty | 97f52eb | 2005-08-13 21:05:57 -0700 | [diff] [blame] | 198 | u16 packet_length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | |
| 200 | memset(header, 0, sizeof *header); |
| 201 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | header->lrh.link_version = 0; |
| 203 | header->lrh.link_next_header = |
| 204 | grh_present ? IB_LNH_IBA_GLOBAL : IB_LNH_IBA_LOCAL; |
Sean Hefty | 97f52eb | 2005-08-13 21:05:57 -0700 | [diff] [blame] | 205 | packet_length = (IB_LRH_BYTES + |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | IB_BTH_BYTES + |
| 207 | IB_DETH_BYTES + |
| 208 | payload_bytes + |
| 209 | 4 + /* ICRC */ |
| 210 | 3) / 4; /* round up */ |
| 211 | |
| 212 | header->grh_present = grh_present; |
| 213 | if (grh_present) { |
Sean Hefty | 97f52eb | 2005-08-13 21:05:57 -0700 | [diff] [blame] | 214 | packet_length += IB_GRH_BYTES / 4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | header->grh.ip_version = 6; |
| 216 | header->grh.payload_length = |
| 217 | cpu_to_be16((IB_BTH_BYTES + |
| 218 | IB_DETH_BYTES + |
| 219 | payload_bytes + |
| 220 | 4 + /* ICRC */ |
| 221 | 3) & ~3); /* round up */ |
| 222 | header->grh.next_header = 0x1b; |
| 223 | } |
| 224 | |
Sean Hefty | 97f52eb | 2005-08-13 21:05:57 -0700 | [diff] [blame] | 225 | header->lrh.packet_length = cpu_to_be16(packet_length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | |
Eli Cohen | 920d706 | 2010-02-08 11:40:37 +0000 | [diff] [blame] | 227 | header->immediate_present = immediate_present; |
| 228 | if (immediate_present) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | header->bth.opcode = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE; |
| 230 | else |
| 231 | header->bth.opcode = IB_OPCODE_UD_SEND_ONLY; |
| 232 | header->bth.pad_count = (4 - payload_bytes) & 3; |
| 233 | header->bth.transport_header_version = 0; |
| 234 | } |
| 235 | EXPORT_SYMBOL(ib_ud_header_init); |
| 236 | |
| 237 | /** |
| 238 | * ib_ud_header_pack - Pack UD header struct into wire format |
| 239 | * @header:UD header struct |
| 240 | * @buf:Buffer to pack into |
| 241 | * |
| 242 | * ib_ud_header_pack() packs the UD header structure @header into wire |
| 243 | * format in the buffer @buf. |
| 244 | */ |
| 245 | int ib_ud_header_pack(struct ib_ud_header *header, |
| 246 | void *buf) |
| 247 | { |
| 248 | int len = 0; |
| 249 | |
| 250 | ib_pack(lrh_table, ARRAY_SIZE(lrh_table), |
| 251 | &header->lrh, buf); |
| 252 | len += IB_LRH_BYTES; |
| 253 | |
| 254 | if (header->grh_present) { |
| 255 | ib_pack(grh_table, ARRAY_SIZE(grh_table), |
| 256 | &header->grh, buf + len); |
| 257 | len += IB_GRH_BYTES; |
| 258 | } |
| 259 | |
| 260 | ib_pack(bth_table, ARRAY_SIZE(bth_table), |
| 261 | &header->bth, buf + len); |
| 262 | len += IB_BTH_BYTES; |
| 263 | |
| 264 | ib_pack(deth_table, ARRAY_SIZE(deth_table), |
| 265 | &header->deth, buf + len); |
| 266 | len += IB_DETH_BYTES; |
| 267 | |
| 268 | if (header->immediate_present) { |
| 269 | memcpy(buf + len, &header->immediate_data, sizeof header->immediate_data); |
| 270 | len += sizeof header->immediate_data; |
| 271 | } |
| 272 | |
| 273 | return len; |
| 274 | } |
| 275 | EXPORT_SYMBOL(ib_ud_header_pack); |
| 276 | |
| 277 | /** |
| 278 | * ib_ud_header_unpack - Unpack UD header struct from wire format |
| 279 | * @header:UD header struct |
| 280 | * @buf:Buffer to pack into |
| 281 | * |
| 282 | * ib_ud_header_pack() unpacks the UD header structure @header from wire |
| 283 | * format in the buffer @buf. |
| 284 | */ |
| 285 | int ib_ud_header_unpack(void *buf, |
| 286 | struct ib_ud_header *header) |
| 287 | { |
| 288 | ib_unpack(lrh_table, ARRAY_SIZE(lrh_table), |
| 289 | buf, &header->lrh); |
| 290 | buf += IB_LRH_BYTES; |
| 291 | |
| 292 | if (header->lrh.link_version != 0) { |
| 293 | printk(KERN_WARNING "Invalid LRH.link_version %d\n", |
| 294 | header->lrh.link_version); |
| 295 | return -EINVAL; |
| 296 | } |
| 297 | |
| 298 | switch (header->lrh.link_next_header) { |
| 299 | case IB_LNH_IBA_LOCAL: |
| 300 | header->grh_present = 0; |
| 301 | break; |
| 302 | |
| 303 | case IB_LNH_IBA_GLOBAL: |
| 304 | header->grh_present = 1; |
| 305 | ib_unpack(grh_table, ARRAY_SIZE(grh_table), |
| 306 | buf, &header->grh); |
| 307 | buf += IB_GRH_BYTES; |
| 308 | |
| 309 | if (header->grh.ip_version != 6) { |
| 310 | printk(KERN_WARNING "Invalid GRH.ip_version %d\n", |
| 311 | header->grh.ip_version); |
| 312 | return -EINVAL; |
| 313 | } |
| 314 | if (header->grh.next_header != 0x1b) { |
| 315 | printk(KERN_WARNING "Invalid GRH.next_header 0x%02x\n", |
| 316 | header->grh.next_header); |
| 317 | return -EINVAL; |
| 318 | } |
| 319 | break; |
| 320 | |
| 321 | default: |
| 322 | printk(KERN_WARNING "Invalid LRH.link_next_header %d\n", |
| 323 | header->lrh.link_next_header); |
| 324 | return -EINVAL; |
| 325 | } |
| 326 | |
| 327 | ib_unpack(bth_table, ARRAY_SIZE(bth_table), |
| 328 | buf, &header->bth); |
| 329 | buf += IB_BTH_BYTES; |
| 330 | |
| 331 | switch (header->bth.opcode) { |
| 332 | case IB_OPCODE_UD_SEND_ONLY: |
| 333 | header->immediate_present = 0; |
| 334 | break; |
| 335 | case IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE: |
| 336 | header->immediate_present = 1; |
| 337 | break; |
| 338 | default: |
| 339 | printk(KERN_WARNING "Invalid BTH.opcode 0x%02x\n", |
| 340 | header->bth.opcode); |
| 341 | return -EINVAL; |
| 342 | } |
| 343 | |
| 344 | if (header->bth.transport_header_version != 0) { |
| 345 | printk(KERN_WARNING "Invalid BTH.transport_header_version %d\n", |
| 346 | header->bth.transport_header_version); |
| 347 | return -EINVAL; |
| 348 | } |
| 349 | |
| 350 | ib_unpack(deth_table, ARRAY_SIZE(deth_table), |
| 351 | buf, &header->deth); |
| 352 | buf += IB_DETH_BYTES; |
| 353 | |
| 354 | if (header->immediate_present) |
| 355 | memcpy(&header->immediate_data, buf, sizeof header->immediate_data); |
| 356 | |
| 357 | return 0; |
| 358 | } |
| 359 | EXPORT_SYMBOL(ib_ud_header_unpack); |