1219820Sjeff/*
2219820Sjeff * Copyright (c) 2004 Topspin Corporation.  All rights reserved.
3219820Sjeff *
4219820Sjeff * This software is available to you under a choice of one of two
5219820Sjeff * licenses.  You may choose to be licensed under the terms of the GNU
6219820Sjeff * General Public License (GPL) Version 2, available from the file
7219820Sjeff * COPYING in the main directory of this source tree, or the
8219820Sjeff * OpenIB.org BSD license below:
9219820Sjeff *
10219820Sjeff *     Redistribution and use in source and binary forms, with or
11219820Sjeff *     without modification, are permitted provided that the following
12219820Sjeff *     conditions are met:
13219820Sjeff *
14219820Sjeff *      - Redistributions of source code must retain the above
15219820Sjeff *        copyright notice, this list of conditions and the following
16219820Sjeff *        disclaimer.
17219820Sjeff *
18219820Sjeff *      - Redistributions in binary form must reproduce the above
19219820Sjeff *        copyright notice, this list of conditions and the following
20219820Sjeff *        disclaimer in the documentation and/or other materials
21219820Sjeff *        provided with the distribution.
22219820Sjeff *
23219820Sjeff * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24219820Sjeff * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25219820Sjeff * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26219820Sjeff * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27219820Sjeff * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28219820Sjeff * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29219820Sjeff * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30219820Sjeff * SOFTWARE.
31331772Shselasky *
32331772Shselasky * $FreeBSD: stable/11/sys/ofed/include/rdma/ib_pack.h 331772 2018-03-30 18:17:33Z hselasky $
33219820Sjeff */
34219820Sjeff
35219820Sjeff#ifndef IB_PACK_H
36219820Sjeff#define IB_PACK_H
37219820Sjeff
38219820Sjeff#include <rdma/ib_verbs.h>
39219820Sjeff
40219820Sjeffenum {
41219820Sjeff	IB_LRH_BYTES  = 8,
42219820Sjeff	IB_ETH_BYTES  = 14,
43219820Sjeff	IB_VLAN_BYTES = 4,
44219820Sjeff	IB_GRH_BYTES  = 40,
45331769Shselasky	IB_IP4_BYTES  = 20,
46331769Shselasky	IB_UDP_BYTES  = 8,
47219820Sjeff	IB_BTH_BYTES  = 12,
48219820Sjeff	IB_DETH_BYTES = 8
49219820Sjeff};
50219820Sjeff
51219820Sjeffstruct ib_field {
52219820Sjeff	size_t struct_offset_bytes;
53219820Sjeff	size_t struct_size_bytes;
54219820Sjeff	int    offset_words;
55219820Sjeff	int    offset_bits;
56219820Sjeff	int    size_bits;
57219820Sjeff	char  *field_name;
58219820Sjeff};
59219820Sjeff
60219820Sjeff#define RESERVED \
61219820Sjeff	.field_name          = "reserved"
62219820Sjeff
63219820Sjeff/*
64219820Sjeff * This macro cleans up the definitions of constants for BTH opcodes.
65219820Sjeff * It is used to define constants such as IB_OPCODE_UD_SEND_ONLY,
66219820Sjeff * which becomes IB_OPCODE_UD + IB_OPCODE_SEND_ONLY, and this gives
67219820Sjeff * the correct value.
68219820Sjeff *
69219820Sjeff * In short, user code should use the constants defined using the
70219820Sjeff * macro rather than worrying about adding together other constants.
71219820Sjeff*/
72219820Sjeff#define IB_OPCODE(transport, op) \
73219820Sjeff	IB_OPCODE_ ## transport ## _ ## op = \
74219820Sjeff		IB_OPCODE_ ## transport + IB_OPCODE_ ## op
75219820Sjeff
76219820Sjeffenum {
77219820Sjeff	/* transport types -- just used to define real constants */
78219820Sjeff	IB_OPCODE_RC                                = 0x00,
79219820Sjeff	IB_OPCODE_UC                                = 0x20,
80219820Sjeff	IB_OPCODE_RD                                = 0x40,
81219820Sjeff	IB_OPCODE_UD                                = 0x60,
82331769Shselasky	/* per IBTA 1.3 vol 1 Table 38, A10.3.2 */
83331769Shselasky	IB_OPCODE_CNP                               = 0x80,
84219820Sjeff
85219820Sjeff	/* operations -- just used to define real constants */
86219820Sjeff	IB_OPCODE_SEND_FIRST                        = 0x00,
87219820Sjeff	IB_OPCODE_SEND_MIDDLE                       = 0x01,
88219820Sjeff	IB_OPCODE_SEND_LAST                         = 0x02,
89219820Sjeff	IB_OPCODE_SEND_LAST_WITH_IMMEDIATE          = 0x03,
90219820Sjeff	IB_OPCODE_SEND_ONLY                         = 0x04,
91219820Sjeff	IB_OPCODE_SEND_ONLY_WITH_IMMEDIATE          = 0x05,
92219820Sjeff	IB_OPCODE_RDMA_WRITE_FIRST                  = 0x06,
93219820Sjeff	IB_OPCODE_RDMA_WRITE_MIDDLE                 = 0x07,
94219820Sjeff	IB_OPCODE_RDMA_WRITE_LAST                   = 0x08,
95219820Sjeff	IB_OPCODE_RDMA_WRITE_LAST_WITH_IMMEDIATE    = 0x09,
96219820Sjeff	IB_OPCODE_RDMA_WRITE_ONLY                   = 0x0a,
97219820Sjeff	IB_OPCODE_RDMA_WRITE_ONLY_WITH_IMMEDIATE    = 0x0b,
98219820Sjeff	IB_OPCODE_RDMA_READ_REQUEST                 = 0x0c,
99219820Sjeff	IB_OPCODE_RDMA_READ_RESPONSE_FIRST          = 0x0d,
100219820Sjeff	IB_OPCODE_RDMA_READ_RESPONSE_MIDDLE         = 0x0e,
101219820Sjeff	IB_OPCODE_RDMA_READ_RESPONSE_LAST           = 0x0f,
102219820Sjeff	IB_OPCODE_RDMA_READ_RESPONSE_ONLY           = 0x10,
103219820Sjeff	IB_OPCODE_ACKNOWLEDGE                       = 0x11,
104219820Sjeff	IB_OPCODE_ATOMIC_ACKNOWLEDGE                = 0x12,
105219820Sjeff	IB_OPCODE_COMPARE_SWAP                      = 0x13,
106219820Sjeff	IB_OPCODE_FETCH_ADD                         = 0x14,
107331769Shselasky	/* opcode 0x15 is reserved */
108331769Shselasky	IB_OPCODE_SEND_LAST_WITH_INVALIDATE         = 0x16,
109331769Shselasky	IB_OPCODE_SEND_ONLY_WITH_INVALIDATE         = 0x17,
110219820Sjeff
111219820Sjeff	/* real constants follow -- see comment about above IB_OPCODE()
112219820Sjeff	   macro for more details */
113219820Sjeff
114219820Sjeff	/* RC */
115219820Sjeff	IB_OPCODE(RC, SEND_FIRST),
116219820Sjeff	IB_OPCODE(RC, SEND_MIDDLE),
117219820Sjeff	IB_OPCODE(RC, SEND_LAST),
118219820Sjeff	IB_OPCODE(RC, SEND_LAST_WITH_IMMEDIATE),
119219820Sjeff	IB_OPCODE(RC, SEND_ONLY),
120219820Sjeff	IB_OPCODE(RC, SEND_ONLY_WITH_IMMEDIATE),
121219820Sjeff	IB_OPCODE(RC, RDMA_WRITE_FIRST),
122219820Sjeff	IB_OPCODE(RC, RDMA_WRITE_MIDDLE),
123219820Sjeff	IB_OPCODE(RC, RDMA_WRITE_LAST),
124219820Sjeff	IB_OPCODE(RC, RDMA_WRITE_LAST_WITH_IMMEDIATE),
125219820Sjeff	IB_OPCODE(RC, RDMA_WRITE_ONLY),
126219820Sjeff	IB_OPCODE(RC, RDMA_WRITE_ONLY_WITH_IMMEDIATE),
127219820Sjeff	IB_OPCODE(RC, RDMA_READ_REQUEST),
128219820Sjeff	IB_OPCODE(RC, RDMA_READ_RESPONSE_FIRST),
129219820Sjeff	IB_OPCODE(RC, RDMA_READ_RESPONSE_MIDDLE),
130219820Sjeff	IB_OPCODE(RC, RDMA_READ_RESPONSE_LAST),
131219820Sjeff	IB_OPCODE(RC, RDMA_READ_RESPONSE_ONLY),
132219820Sjeff	IB_OPCODE(RC, ACKNOWLEDGE),
133219820Sjeff	IB_OPCODE(RC, ATOMIC_ACKNOWLEDGE),
134219820Sjeff	IB_OPCODE(RC, COMPARE_SWAP),
135219820Sjeff	IB_OPCODE(RC, FETCH_ADD),
136331769Shselasky	IB_OPCODE(RC, SEND_LAST_WITH_INVALIDATE),
137331769Shselasky	IB_OPCODE(RC, SEND_ONLY_WITH_INVALIDATE),
138219820Sjeff
139219820Sjeff	/* UC */
140219820Sjeff	IB_OPCODE(UC, SEND_FIRST),
141219820Sjeff	IB_OPCODE(UC, SEND_MIDDLE),
142219820Sjeff	IB_OPCODE(UC, SEND_LAST),
143219820Sjeff	IB_OPCODE(UC, SEND_LAST_WITH_IMMEDIATE),
144219820Sjeff	IB_OPCODE(UC, SEND_ONLY),
145219820Sjeff	IB_OPCODE(UC, SEND_ONLY_WITH_IMMEDIATE),
146219820Sjeff	IB_OPCODE(UC, RDMA_WRITE_FIRST),
147219820Sjeff	IB_OPCODE(UC, RDMA_WRITE_MIDDLE),
148219820Sjeff	IB_OPCODE(UC, RDMA_WRITE_LAST),
149219820Sjeff	IB_OPCODE(UC, RDMA_WRITE_LAST_WITH_IMMEDIATE),
150219820Sjeff	IB_OPCODE(UC, RDMA_WRITE_ONLY),
151219820Sjeff	IB_OPCODE(UC, RDMA_WRITE_ONLY_WITH_IMMEDIATE),
152219820Sjeff
153219820Sjeff	/* RD */
154219820Sjeff	IB_OPCODE(RD, SEND_FIRST),
155219820Sjeff	IB_OPCODE(RD, SEND_MIDDLE),
156219820Sjeff	IB_OPCODE(RD, SEND_LAST),
157219820Sjeff	IB_OPCODE(RD, SEND_LAST_WITH_IMMEDIATE),
158219820Sjeff	IB_OPCODE(RD, SEND_ONLY),
159219820Sjeff	IB_OPCODE(RD, SEND_ONLY_WITH_IMMEDIATE),
160219820Sjeff	IB_OPCODE(RD, RDMA_WRITE_FIRST),
161219820Sjeff	IB_OPCODE(RD, RDMA_WRITE_MIDDLE),
162219820Sjeff	IB_OPCODE(RD, RDMA_WRITE_LAST),
163219820Sjeff	IB_OPCODE(RD, RDMA_WRITE_LAST_WITH_IMMEDIATE),
164219820Sjeff	IB_OPCODE(RD, RDMA_WRITE_ONLY),
165219820Sjeff	IB_OPCODE(RD, RDMA_WRITE_ONLY_WITH_IMMEDIATE),
166219820Sjeff	IB_OPCODE(RD, RDMA_READ_REQUEST),
167219820Sjeff	IB_OPCODE(RD, RDMA_READ_RESPONSE_FIRST),
168219820Sjeff	IB_OPCODE(RD, RDMA_READ_RESPONSE_MIDDLE),
169219820Sjeff	IB_OPCODE(RD, RDMA_READ_RESPONSE_LAST),
170219820Sjeff	IB_OPCODE(RD, RDMA_READ_RESPONSE_ONLY),
171219820Sjeff	IB_OPCODE(RD, ACKNOWLEDGE),
172219820Sjeff	IB_OPCODE(RD, ATOMIC_ACKNOWLEDGE),
173219820Sjeff	IB_OPCODE(RD, COMPARE_SWAP),
174219820Sjeff	IB_OPCODE(RD, FETCH_ADD),
175219820Sjeff
176219820Sjeff	/* UD */
177219820Sjeff	IB_OPCODE(UD, SEND_ONLY),
178219820Sjeff	IB_OPCODE(UD, SEND_ONLY_WITH_IMMEDIATE)
179219820Sjeff};
180219820Sjeff
181219820Sjeffenum {
182219820Sjeff	IB_LNH_RAW        = 0,
183219820Sjeff	IB_LNH_IP         = 1,
184219820Sjeff	IB_LNH_IBA_LOCAL  = 2,
185219820Sjeff	IB_LNH_IBA_GLOBAL = 3
186219820Sjeff};
187219820Sjeff
188219820Sjeffstruct ib_unpacked_lrh {
189219820Sjeff	u8        virtual_lane;
190219820Sjeff	u8        link_version;
191219820Sjeff	u8        service_level;
192219820Sjeff	u8        link_next_header;
193219820Sjeff	__be16    destination_lid;
194219820Sjeff	__be16    packet_length;
195219820Sjeff	__be16    source_lid;
196219820Sjeff};
197219820Sjeff
198219820Sjeffstruct ib_unpacked_grh {
199219820Sjeff	u8    	     ip_version;
200219820Sjeff	u8    	     traffic_class;
201219820Sjeff	__be32 	     flow_label;
202219820Sjeff	__be16       payload_length;
203219820Sjeff	u8    	     next_header;
204219820Sjeff	u8    	     hop_limit;
205219820Sjeff	union ib_gid source_gid;
206219820Sjeff	union ib_gid destination_gid;
207219820Sjeff};
208219820Sjeff
209219820Sjeffstruct ib_unpacked_bth {
210219820Sjeff	u8           opcode;
211219820Sjeff	u8           solicited_event;
212219820Sjeff	u8           mig_req;
213219820Sjeff	u8           pad_count;
214219820Sjeff	u8           transport_header_version;
215219820Sjeff	__be16       pkey;
216219820Sjeff	__be32       destination_qpn;
217219820Sjeff	u8           ack_req;
218219820Sjeff	__be32       psn;
219219820Sjeff};
220219820Sjeff
221219820Sjeffstruct ib_unpacked_deth {
222219820Sjeff	__be32       qkey;
223219820Sjeff	__be32       source_qpn;
224219820Sjeff};
225219820Sjeff
226219820Sjeffstruct ib_unpacked_eth {
227219820Sjeff	u8	dmac_h[4];
228219820Sjeff	u8	dmac_l[2];
229219820Sjeff	u8	smac_h[2];
230219820Sjeff	u8	smac_l[4];
231219820Sjeff	__be16	type;
232219820Sjeff};
233219820Sjeff
234331769Shselaskystruct ib_unpacked_ip4 {
235331769Shselasky	u8	ver;
236331769Shselasky	u8	hdr_len;
237331769Shselasky	u8	tos;
238331769Shselasky	__be16	tot_len;
239331769Shselasky	__be16	id;
240331769Shselasky	__be16	frag_off;
241331769Shselasky	u8	ttl;
242331769Shselasky	u8	protocol;
243331769Shselasky	__sum16	check;
244331769Shselasky	__be32	saddr;
245331769Shselasky	__be32	daddr;
246331769Shselasky};
247331769Shselasky
248331769Shselaskystruct ib_unpacked_udp {
249331769Shselasky	__be16	sport;
250331769Shselasky	__be16	dport;
251331769Shselasky	__be16	length;
252331769Shselasky	__be16	csum;
253331769Shselasky};
254331769Shselasky
255219820Sjeffstruct ib_unpacked_vlan {
256219820Sjeff	__be16  tag;
257219820Sjeff	__be16  type;
258219820Sjeff};
259219820Sjeff
260219820Sjeffstruct ib_ud_header {
261219820Sjeff	int                     lrh_present;
262219820Sjeff	struct ib_unpacked_lrh  lrh;
263331769Shselasky	int			eth_present;
264331769Shselasky	struct ib_unpacked_eth	eth;
265219820Sjeff	int                     vlan_present;
266219820Sjeff	struct ib_unpacked_vlan vlan;
267331769Shselasky	int			grh_present;
268331769Shselasky	struct ib_unpacked_grh	grh;
269331769Shselasky	int			ipv4_present;
270331769Shselasky	struct ib_unpacked_ip4	ip4;
271331769Shselasky	int			udp_present;
272331769Shselasky	struct ib_unpacked_udp	udp;
273331769Shselasky	struct ib_unpacked_bth	bth;
274219820Sjeff	struct ib_unpacked_deth deth;
275331769Shselasky	int			immediate_present;
276331769Shselasky	__be32			immediate_data;
277219820Sjeff};
278219820Sjeff
279219820Sjeffvoid ib_pack(const struct ib_field        *desc,
280219820Sjeff	     int                           desc_len,
281219820Sjeff	     void                         *structure,
282219820Sjeff	     void                         *buf);
283219820Sjeff
284219820Sjeffvoid ib_unpack(const struct ib_field        *desc,
285219820Sjeff	       int                           desc_len,
286219820Sjeff	       void                         *buf,
287219820Sjeff	       void                         *structure);
288219820Sjeff
289331769Shselasky__sum16 ib_ud_ip4_csum(struct ib_ud_header *header);
290219820Sjeff
291331769Shselaskyint ib_ud_header_init(int		    payload_bytes,
292331769Shselasky		      int		    lrh_present,
293331769Shselasky		      int		    eth_present,
294331769Shselasky		      int		    vlan_present,
295331769Shselasky		      int		    grh_present,
296331769Shselasky		      int		    ip_version,
297331769Shselasky		      int		    udp_present,
298331769Shselasky		      int		    immediate_present,
299331769Shselasky		      struct ib_ud_header *header);
300331769Shselasky
301219820Sjeffint ib_ud_header_pack(struct ib_ud_header *header,
302219820Sjeff		      void                *buf);
303219820Sjeff
304219820Sjeffint ib_ud_header_unpack(void                *buf,
305219820Sjeff			struct ib_ud_header *header);
306219820Sjeff
307219820Sjeff#endif /* IB_PACK_H */
308