Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / net / infiniband / ib_packet.c
1 /*
2  * Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  */
19
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 #include <stdint.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <byteswap.h>
27 #include <ipxe/iobuf.h>
28 #include <ipxe/infiniband.h>
29 #include <ipxe/ib_packet.h>
30
31 /**
32  * @file
33  *
34  * Infiniband Packet Formats
35  *
36  */
37
38 /**
39  * Add IB headers
40  *
41  * @v ibdev             Infiniband device
42  * @v iobuf             I/O buffer to contain headers
43  * @v qp                Queue pair
44  * @v payload_len       Payload length
45  * @v dest              Destination address vector
46  * @ret rc              Return status code
47  */
48 int ib_push ( struct ib_device *ibdev, struct io_buffer *iobuf,
49               struct ib_queue_pair *qp, size_t payload_len,
50               const struct ib_address_vector *dest ) {
51         struct ib_local_route_header *lrh;
52         struct ib_global_route_header *grh;
53         struct ib_base_transport_header *bth;
54         struct ib_datagram_extended_transport_header *deth;
55         size_t orig_iob_len = iob_len ( iobuf );
56         size_t pad_len;
57         size_t lrh_len;
58         size_t grh_len;
59         unsigned int vl;
60         unsigned int lnh;
61
62         DBGC2 ( ibdev, "IBDEV %p TX %04x:%08lx => %04x:%08lx (key %08lx)\n",
63                 ibdev, ibdev->lid, qp->ext_qpn, dest->lid, dest->qpn,
64                 dest->qkey );
65
66         /* Calculate packet length */
67         pad_len = ( (-payload_len) & 0x3 );
68         payload_len += pad_len;
69         payload_len += 4; /* ICRC */
70
71         /* Reserve space for headers */
72         orig_iob_len = iob_len ( iobuf );
73         deth = iob_push ( iobuf, sizeof ( *deth ) );
74         bth = iob_push ( iobuf, sizeof ( *bth ) );
75         grh_len = ( payload_len + iob_len ( iobuf ) - orig_iob_len );
76         grh = ( dest->gid_present ?
77                 iob_push ( iobuf, sizeof ( *grh ) ) : NULL );
78         lrh = iob_push ( iobuf, sizeof ( *lrh ) );
79         lrh_len = ( payload_len + iob_len ( iobuf ) - orig_iob_len );
80
81         /* Construct LRH */
82         vl = ( ( qp->ext_qpn == IB_QPN_SMI ) ? IB_VL_SMP : IB_VL_DEFAULT );
83         lrh->vl__lver = ( vl << 4 );
84         lnh = ( grh ? IB_LNH_GRH : IB_LNH_BTH );
85         lrh->sl__lnh = ( ( dest->sl << 4 ) | lnh );
86         lrh->dlid = htons ( dest->lid );
87         lrh->length = htons ( lrh_len >> 2 );
88         lrh->slid = htons ( ibdev->lid );
89
90         /* Construct GRH, if required */
91         if ( grh ) {
92                 grh->ipver__tclass__flowlabel =
93                         htonl ( IB_GRH_IPVER_IPv6 << 28 );
94                 grh->paylen = htons ( grh_len );
95                 grh->nxthdr = IB_GRH_NXTHDR_IBA;
96                 grh->hoplmt = 0;
97                 memcpy ( &grh->sgid, &ibdev->gid, sizeof ( grh->sgid ) );
98                 memcpy ( &grh->dgid, &dest->gid, sizeof ( grh->dgid ) );
99         }
100
101         /* Construct BTH */
102         bth->opcode = BTH_OPCODE_UD_SEND;
103         bth->se__m__padcnt__tver = ( pad_len << 4 );
104         bth->pkey = htons ( ibdev->pkey );
105         bth->dest_qp = htonl ( dest->qpn );
106         bth->ack__psn = htonl ( ( qp->send.psn++ ) & 0xffffffUL );
107
108         /* Construct DETH */
109         deth->qkey = htonl ( dest->qkey );
110         deth->src_qp = htonl ( qp->ext_qpn );
111
112         DBGCP_HDA ( ibdev, 0, iobuf->data,
113                     ( iob_len ( iobuf ) - orig_iob_len ) );
114
115         return 0;
116 }
117
118 /**
119  * Remove IB headers
120  *
121  * @v ibdev             Infiniband device
122  * @v iobuf             I/O buffer containing headers
123  * @v qp                Queue pair to fill in, or NULL
124  * @v payload_len       Payload length to fill in, or NULL
125  * @v dest              Destination address vector to fill in
126  * @v source            Source address vector to fill in
127  * @ret rc              Return status code
128  */
129 int ib_pull ( struct ib_device *ibdev, struct io_buffer *iobuf,
130               struct ib_queue_pair **qp, size_t *payload_len,
131               struct ib_address_vector *dest,
132               struct ib_address_vector *source ) {
133         struct ib_local_route_header *lrh;
134         struct ib_global_route_header *grh;
135         struct ib_base_transport_header *bth;
136         struct ib_datagram_extended_transport_header *deth;
137         size_t orig_iob_len = iob_len ( iobuf );
138         unsigned int lnh;
139         size_t pad_len;
140
141         /* Clear return values */
142         if ( qp )
143                 *qp = NULL;
144         if ( payload_len )
145                 *payload_len = 0;
146         memset ( dest, 0, sizeof ( *dest ) );
147         memset ( source, 0, sizeof ( *source ) );
148
149         /* Extract LRH */
150         if ( iob_len ( iobuf ) < sizeof ( *lrh ) ) {
151                 DBGC ( ibdev, "IBDEV %p RX too short (%zd bytes) for LRH\n",
152                        ibdev, iob_len ( iobuf ) );
153                 return -EINVAL;
154         }
155         lrh = iobuf->data;
156         iob_pull ( iobuf, sizeof ( *lrh ) );
157         dest->lid = ntohs ( lrh->dlid );
158         dest->sl = ( lrh->sl__lnh >> 4 );
159         source->lid = ntohs ( lrh->slid );
160         source->sl = ( lrh->sl__lnh >> 4 );
161         lnh = ( lrh->sl__lnh & 0x3 );
162
163         /* Reject unsupported packets */
164         if ( ! ( ( lnh == IB_LNH_BTH ) || ( lnh == IB_LNH_GRH ) ) ) {
165                 DBGC ( ibdev, "IBDEV %p RX unsupported LNH %x\n",
166                        ibdev, lnh );
167                 return -ENOTSUP;
168         }
169
170         /* Extract GRH, if present */
171         if ( lnh == IB_LNH_GRH ) {
172                 if ( iob_len ( iobuf ) < sizeof ( *grh ) ) {
173                         DBGC ( ibdev, "IBDEV %p RX too short (%zd bytes) "
174                                "for GRH\n", ibdev, iob_len ( iobuf ) );
175                         return -EINVAL;
176                 }
177                 grh = iobuf->data;
178                 iob_pull ( iobuf, sizeof ( *grh ) );
179                 dest->gid_present = 1;
180                 memcpy ( &dest->gid, &grh->dgid, sizeof ( dest->gid ) );
181                 source->gid_present = 1;
182                 memcpy ( &source->gid, &grh->sgid, sizeof ( source->gid ) );
183         } else {
184                 grh = NULL;
185         }
186
187         /* Extract BTH */
188         if ( iob_len ( iobuf ) < sizeof ( *bth ) ) {
189                 DBGC ( ibdev, "IBDEV %p RX too short (%zd bytes) for BTH\n",
190                        ibdev, iob_len ( iobuf ) );
191                 return -EINVAL;
192         }
193         bth = iobuf->data;
194         iob_pull ( iobuf, sizeof ( *bth ) );
195         if ( bth->opcode != BTH_OPCODE_UD_SEND ) {
196                 DBGC ( ibdev, "IBDEV %p unsupported BTH opcode %x\n",
197                        ibdev, bth->opcode );
198                 return -ENOTSUP;
199         }
200         dest->qpn = ntohl ( bth->dest_qp );
201
202         /* Extract DETH */
203         if ( iob_len ( iobuf ) < sizeof ( *deth ) ) {
204                 DBGC ( ibdev, "IBDEV %p RX too short (%zd bytes) for DETH\n",
205                        ibdev, iob_len ( iobuf ) );
206                 return -EINVAL;
207         }
208         deth = iobuf->data;
209         iob_pull ( iobuf, sizeof ( *deth ) );
210         source->qpn = ntohl ( deth->src_qp );
211         source->qkey = ntohl ( deth->qkey );
212
213         /* Calculate payload length, if applicable */
214         if ( payload_len ) {
215                 pad_len = ( ( bth->se__m__padcnt__tver >> 4 ) & 0x3 );
216                 *payload_len = ( ( ntohs ( lrh->length ) << 2 )
217                                  - ( orig_iob_len - iob_len ( iobuf ) )
218                                  - pad_len - 4 /* ICRC */ );
219         }
220
221         /* Determine destination QP, if applicable */
222         if ( qp ) {
223                 if ( IB_LID_MULTICAST ( dest->lid ) && grh ) {
224                         if ( ! ( *qp = ib_find_qp_mgid ( ibdev, &grh->dgid ))){
225                                 DBGC ( ibdev, "IBDEV %p RX for unknown MGID "
226                                        IB_GID_FMT "\n",
227                                        ibdev, IB_GID_ARGS ( &grh->dgid ) );
228                                 return -ENODEV;
229                         }
230                 } else {
231                         if ( ! ( *qp = ib_find_qp_qpn ( ibdev, dest->qpn ) ) ) {
232                                 DBGC ( ibdev, "IBDEV %p RX for nonexistent "
233                                        "QPN %lx\n", ibdev, dest->qpn );
234                                 return -ENODEV;
235                         }
236                 }
237                 assert ( *qp );
238         }
239
240         DBGC2 ( ibdev, "IBDEV %p RX %04x:%08lx <= %04x:%08lx (key %08x)\n",
241                 ibdev, dest->lid, ( IB_LID_MULTICAST ( dest->lid ) ?
242                               ( qp ? (*qp)->ext_qpn : -1UL ) : dest->qpn ),
243                 source->lid, source->qpn, ntohl ( deth->qkey ) );
244         DBGCP_HDA ( ibdev, 0,
245                     ( iobuf->data - ( orig_iob_len - iob_len ( iobuf ) ) ),
246                     ( orig_iob_len - iob_len ( iobuf ) ) );
247
248         return 0;
249 }