2 * Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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
20 FILE_LICENCE ( GPL2_OR_LATER );
27 #include <ipxe/iobuf.h>
28 #include <ipxe/infiniband.h>
29 #include <ipxe/ib_packet.h>
34 * Infiniband Packet Formats
41 * @v ibdev Infiniband device
42 * @v iobuf I/O buffer to contain headers
44 * @v payload_len Payload length
45 * @v dest Destination address vector
46 * @ret rc Return status code
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 );
62 DBGC2 ( ibdev, "IBDEV %p TX %04x:%08lx => %04x:%08lx (key %08lx)\n",
63 ibdev, ibdev->lid, qp->ext_qpn, dest->lid, dest->qpn,
66 /* Calculate packet length */
67 pad_len = ( (-payload_len) & 0x3 );
68 payload_len += pad_len;
69 payload_len += 4; /* ICRC */
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 );
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 );
90 /* Construct GRH, if required */
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;
97 memcpy ( &grh->sgid, &ibdev->gid, sizeof ( grh->sgid ) );
98 memcpy ( &grh->dgid, &dest->gid, sizeof ( grh->dgid ) );
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 );
109 deth->qkey = htonl ( dest->qkey );
110 deth->src_qp = htonl ( qp->ext_qpn );
112 DBGCP_HDA ( ibdev, 0, iobuf->data,
113 ( iob_len ( iobuf ) - orig_iob_len ) );
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
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 );
141 /* Clear return values */
146 memset ( dest, 0, sizeof ( *dest ) );
147 memset ( source, 0, sizeof ( *source ) );
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 ) );
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 );
163 /* Reject unsupported packets */
164 if ( ! ( ( lnh == IB_LNH_BTH ) || ( lnh == IB_LNH_GRH ) ) ) {
165 DBGC ( ibdev, "IBDEV %p RX unsupported LNH %x\n",
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 ) );
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 ) );
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 ) );
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 );
200 dest->qpn = ntohl ( bth->dest_qp );
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 ) );
209 iob_pull ( iobuf, sizeof ( *deth ) );
210 source->qpn = ntohl ( deth->src_qp );
211 source->qkey = ntohl ( deth->qkey );
213 /* Calculate payload length, if applicable */
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 */ );
221 /* Determine destination QP, if applicable */
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 "
227 ibdev, IB_GID_ARGS ( &grh->dgid ) );
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 );
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 ) ) );