Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Copyright (c) 2004 Topspin Corporation. All rights reserved. | |
2a1d9b7f | 3 | * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved. |
1da177e4 LT |
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 | * $Id: ud_header.c 1349 2004-12-16 21:09:43Z roland $ | |
34 | */ | |
35 | ||
36 | #include <linux/errno.h> | |
37 | ||
a4d61e84 | 38 | #include <rdma/ib_pack.h> |
1da177e4 LT |
39 | |
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 | |
44 | ||
45 | static const struct ib_field lrh_table[] = { | |
46 | { STRUCT_FIELD(lrh, virtual_lane), | |
47 | .offset_words = 0, | |
48 | .offset_bits = 0, | |
49 | .size_bits = 4 }, | |
50 | { STRUCT_FIELD(lrh, link_version), | |
51 | .offset_words = 0, | |
52 | .offset_bits = 4, | |
53 | .size_bits = 4 }, | |
54 | { STRUCT_FIELD(lrh, service_level), | |
55 | .offset_words = 0, | |
56 | .offset_bits = 8, | |
57 | .size_bits = 4 }, | |
58 | { RESERVED, | |
59 | .offset_words = 0, | |
60 | .offset_bits = 12, | |
61 | .size_bits = 2 }, | |
62 | { STRUCT_FIELD(lrh, link_next_header), | |
63 | .offset_words = 0, | |
64 | .offset_bits = 14, | |
65 | .size_bits = 2 }, | |
66 | { STRUCT_FIELD(lrh, destination_lid), | |
67 | .offset_words = 0, | |
68 | .offset_bits = 16, | |
69 | .size_bits = 16 }, | |
70 | { RESERVED, | |
71 | .offset_words = 1, | |
72 | .offset_bits = 0, | |
73 | .size_bits = 5 }, | |
74 | { STRUCT_FIELD(lrh, packet_length), | |
75 | .offset_words = 1, | |
76 | .offset_bits = 5, | |
77 | .size_bits = 11 }, | |
78 | { STRUCT_FIELD(lrh, source_lid), | |
79 | .offset_words = 1, | |
80 | .offset_bits = 16, | |
81 | .size_bits = 16 } | |
82 | }; | |
83 | ||
84 | static const struct ib_field grh_table[] = { | |
85 | { STRUCT_FIELD(grh, ip_version), | |
86 | .offset_words = 0, | |
87 | .offset_bits = 0, | |
88 | .size_bits = 4 }, | |
89 | { STRUCT_FIELD(grh, traffic_class), | |
90 | .offset_words = 0, | |
91 | .offset_bits = 4, | |
92 | .size_bits = 8 }, | |
93 | { STRUCT_FIELD(grh, flow_label), | |
94 | .offset_words = 0, | |
95 | .offset_bits = 12, | |
96 | .size_bits = 20 }, | |
97 | { STRUCT_FIELD(grh, payload_length), | |
98 | .offset_words = 1, | |
99 | .offset_bits = 0, | |
100 | .size_bits = 16 }, | |
101 | { STRUCT_FIELD(grh, next_header), | |
102 | .offset_words = 1, | |
103 | .offset_bits = 16, | |
104 | .size_bits = 8 }, | |
105 | { STRUCT_FIELD(grh, hop_limit), | |
106 | .offset_words = 1, | |
107 | .offset_bits = 24, | |
108 | .size_bits = 8 }, | |
109 | { STRUCT_FIELD(grh, source_gid), | |
110 | .offset_words = 2, | |
111 | .offset_bits = 0, | |
112 | .size_bits = 128 }, | |
113 | { STRUCT_FIELD(grh, destination_gid), | |
114 | .offset_words = 6, | |
115 | .offset_bits = 0, | |
116 | .size_bits = 128 } | |
117 | }; | |
118 | ||
119 | static const struct ib_field bth_table[] = { | |
120 | { STRUCT_FIELD(bth, opcode), | |
121 | .offset_words = 0, | |
122 | .offset_bits = 0, | |
123 | .size_bits = 8 }, | |
124 | { STRUCT_FIELD(bth, solicited_event), | |
125 | .offset_words = 0, | |
126 | .offset_bits = 8, | |
127 | .size_bits = 1 }, | |
128 | { STRUCT_FIELD(bth, mig_req), | |
129 | .offset_words = 0, | |
130 | .offset_bits = 9, | |
131 | .size_bits = 1 }, | |
132 | { STRUCT_FIELD(bth, pad_count), | |
133 | .offset_words = 0, | |
134 | .offset_bits = 10, | |
135 | .size_bits = 2 }, | |
136 | { STRUCT_FIELD(bth, transport_header_version), | |
137 | .offset_words = 0, | |
138 | .offset_bits = 12, | |
139 | .size_bits = 4 }, | |
140 | { STRUCT_FIELD(bth, pkey), | |
141 | .offset_words = 0, | |
142 | .offset_bits = 16, | |
143 | .size_bits = 16 }, | |
144 | { RESERVED, | |
145 | .offset_words = 1, | |
146 | .offset_bits = 0, | |
147 | .size_bits = 8 }, | |
148 | { STRUCT_FIELD(bth, destination_qpn), | |
149 | .offset_words = 1, | |
150 | .offset_bits = 8, | |
151 | .size_bits = 24 }, | |
152 | { STRUCT_FIELD(bth, ack_req), | |
153 | .offset_words = 2, | |
154 | .offset_bits = 0, | |
155 | .size_bits = 1 }, | |
156 | { RESERVED, | |
157 | .offset_words = 2, | |
158 | .offset_bits = 1, | |
159 | .size_bits = 7 }, | |
160 | { STRUCT_FIELD(bth, psn), | |
161 | .offset_words = 2, | |
162 | .offset_bits = 8, | |
163 | .size_bits = 24 } | |
164 | }; | |
165 | ||
166 | static const struct ib_field deth_table[] = { | |
167 | { STRUCT_FIELD(deth, qkey), | |
168 | .offset_words = 0, | |
169 | .offset_bits = 0, | |
170 | .size_bits = 32 }, | |
171 | { RESERVED, | |
172 | .offset_words = 1, | |
173 | .offset_bits = 0, | |
174 | .size_bits = 8 }, | |
175 | { STRUCT_FIELD(deth, source_qpn), | |
176 | .offset_words = 1, | |
177 | .offset_bits = 8, | |
178 | .size_bits = 24 } | |
179 | }; | |
180 | ||
181 | /** | |
182 | * ib_ud_header_init - Initialize UD header structure | |
183 | * @payload_bytes:Length of packet payload | |
184 | * @grh_present:GRH flag (if non-zero, GRH will be included) | |
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, | |
195 | struct ib_ud_header *header) | |
196 | { | |
197 | int header_len; | |
97f52eb4 | 198 | u16 packet_length; |
1da177e4 LT |
199 | |
200 | memset(header, 0, sizeof *header); | |
201 | ||
202 | header_len = | |
203 | IB_LRH_BYTES + | |
204 | IB_BTH_BYTES + | |
205 | IB_DETH_BYTES; | |
206 | if (grh_present) { | |
207 | header_len += IB_GRH_BYTES; | |
208 | } | |
209 | ||
210 | header->lrh.link_version = 0; | |
211 | header->lrh.link_next_header = | |
212 | grh_present ? IB_LNH_IBA_GLOBAL : IB_LNH_IBA_LOCAL; | |
97f52eb4 | 213 | packet_length = (IB_LRH_BYTES + |
1da177e4 LT |
214 | IB_BTH_BYTES + |
215 | IB_DETH_BYTES + | |
216 | payload_bytes + | |
217 | 4 + /* ICRC */ | |
218 | 3) / 4; /* round up */ | |
219 | ||
220 | header->grh_present = grh_present; | |
221 | if (grh_present) { | |
97f52eb4 | 222 | packet_length += IB_GRH_BYTES / 4; |
1da177e4 LT |
223 | header->grh.ip_version = 6; |
224 | header->grh.payload_length = | |
225 | cpu_to_be16((IB_BTH_BYTES + | |
226 | IB_DETH_BYTES + | |
227 | payload_bytes + | |
228 | 4 + /* ICRC */ | |
229 | 3) & ~3); /* round up */ | |
230 | header->grh.next_header = 0x1b; | |
231 | } | |
232 | ||
97f52eb4 | 233 | header->lrh.packet_length = cpu_to_be16(packet_length); |
1da177e4 LT |
234 | |
235 | if (header->immediate_present) | |
236 | header->bth.opcode = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE; | |
237 | else | |
238 | header->bth.opcode = IB_OPCODE_UD_SEND_ONLY; | |
239 | header->bth.pad_count = (4 - payload_bytes) & 3; | |
240 | header->bth.transport_header_version = 0; | |
241 | } | |
242 | EXPORT_SYMBOL(ib_ud_header_init); | |
243 | ||
244 | /** | |
245 | * ib_ud_header_pack - Pack UD header struct into wire format | |
246 | * @header:UD header struct | |
247 | * @buf:Buffer to pack into | |
248 | * | |
249 | * ib_ud_header_pack() packs the UD header structure @header into wire | |
250 | * format in the buffer @buf. | |
251 | */ | |
252 | int ib_ud_header_pack(struct ib_ud_header *header, | |
253 | void *buf) | |
254 | { | |
255 | int len = 0; | |
256 | ||
257 | ib_pack(lrh_table, ARRAY_SIZE(lrh_table), | |
258 | &header->lrh, buf); | |
259 | len += IB_LRH_BYTES; | |
260 | ||
261 | if (header->grh_present) { | |
262 | ib_pack(grh_table, ARRAY_SIZE(grh_table), | |
263 | &header->grh, buf + len); | |
264 | len += IB_GRH_BYTES; | |
265 | } | |
266 | ||
267 | ib_pack(bth_table, ARRAY_SIZE(bth_table), | |
268 | &header->bth, buf + len); | |
269 | len += IB_BTH_BYTES; | |
270 | ||
271 | ib_pack(deth_table, ARRAY_SIZE(deth_table), | |
272 | &header->deth, buf + len); | |
273 | len += IB_DETH_BYTES; | |
274 | ||
275 | if (header->immediate_present) { | |
276 | memcpy(buf + len, &header->immediate_data, sizeof header->immediate_data); | |
277 | len += sizeof header->immediate_data; | |
278 | } | |
279 | ||
280 | return len; | |
281 | } | |
282 | EXPORT_SYMBOL(ib_ud_header_pack); | |
283 | ||
284 | /** | |
285 | * ib_ud_header_unpack - Unpack UD header struct from wire format | |
286 | * @header:UD header struct | |
287 | * @buf:Buffer to pack into | |
288 | * | |
289 | * ib_ud_header_pack() unpacks the UD header structure @header from wire | |
290 | * format in the buffer @buf. | |
291 | */ | |
292 | int ib_ud_header_unpack(void *buf, | |
293 | struct ib_ud_header *header) | |
294 | { | |
295 | ib_unpack(lrh_table, ARRAY_SIZE(lrh_table), | |
296 | buf, &header->lrh); | |
297 | buf += IB_LRH_BYTES; | |
298 | ||
299 | if (header->lrh.link_version != 0) { | |
300 | printk(KERN_WARNING "Invalid LRH.link_version %d\n", | |
301 | header->lrh.link_version); | |
302 | return -EINVAL; | |
303 | } | |
304 | ||
305 | switch (header->lrh.link_next_header) { | |
306 | case IB_LNH_IBA_LOCAL: | |
307 | header->grh_present = 0; | |
308 | break; | |
309 | ||
310 | case IB_LNH_IBA_GLOBAL: | |
311 | header->grh_present = 1; | |
312 | ib_unpack(grh_table, ARRAY_SIZE(grh_table), | |
313 | buf, &header->grh); | |
314 | buf += IB_GRH_BYTES; | |
315 | ||
316 | if (header->grh.ip_version != 6) { | |
317 | printk(KERN_WARNING "Invalid GRH.ip_version %d\n", | |
318 | header->grh.ip_version); | |
319 | return -EINVAL; | |
320 | } | |
321 | if (header->grh.next_header != 0x1b) { | |
322 | printk(KERN_WARNING "Invalid GRH.next_header 0x%02x\n", | |
323 | header->grh.next_header); | |
324 | return -EINVAL; | |
325 | } | |
326 | break; | |
327 | ||
328 | default: | |
329 | printk(KERN_WARNING "Invalid LRH.link_next_header %d\n", | |
330 | header->lrh.link_next_header); | |
331 | return -EINVAL; | |
332 | } | |
333 | ||
334 | ib_unpack(bth_table, ARRAY_SIZE(bth_table), | |
335 | buf, &header->bth); | |
336 | buf += IB_BTH_BYTES; | |
337 | ||
338 | switch (header->bth.opcode) { | |
339 | case IB_OPCODE_UD_SEND_ONLY: | |
340 | header->immediate_present = 0; | |
341 | break; | |
342 | case IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE: | |
343 | header->immediate_present = 1; | |
344 | break; | |
345 | default: | |
346 | printk(KERN_WARNING "Invalid BTH.opcode 0x%02x\n", | |
347 | header->bth.opcode); | |
348 | return -EINVAL; | |
349 | } | |
350 | ||
351 | if (header->bth.transport_header_version != 0) { | |
352 | printk(KERN_WARNING "Invalid BTH.transport_header_version %d\n", | |
353 | header->bth.transport_header_version); | |
354 | return -EINVAL; | |
355 | } | |
356 | ||
357 | ib_unpack(deth_table, ARRAY_SIZE(deth_table), | |
358 | buf, &header->deth); | |
359 | buf += IB_DETH_BYTES; | |
360 | ||
361 | if (header->immediate_present) | |
362 | memcpy(&header->immediate_data, buf, sizeof header->immediate_data); | |
363 | ||
364 | return 0; | |
365 | } | |
366 | EXPORT_SYMBOL(ib_ud_header_unpack); |