Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <stdint.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <errno.h>
  28. #include <byteswap.h>
  29. #include <ipxe/iobuf.h>
  30. #include <ipxe/infiniband.h>
  31. #include <ipxe/ib_packet.h>
  32. /**
  33. * @file
  34. *
  35. * Infiniband Packet Formats
  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. DBGC2 ( ibdev, "IBDEV %s TX %04x:%08lx => %04x:%08lx (key %08lx)\n",
  62. ibdev->name, ibdev->lid, qp->ext_qpn, dest->lid, dest->qpn,
  63. dest->qkey );
  64. /* Calculate packet length */
  65. pad_len = ( (-payload_len) & 0x3 );
  66. payload_len += pad_len;
  67. payload_len += 4; /* ICRC */
  68. /* Reserve space for headers */
  69. orig_iob_len = iob_len ( iobuf );
  70. deth = iob_push ( iobuf, sizeof ( *deth ) );
  71. bth = iob_push ( iobuf, sizeof ( *bth ) );
  72. grh_len = ( payload_len + iob_len ( iobuf ) - orig_iob_len );
  73. grh = ( dest->gid_present ?
  74. iob_push ( iobuf, sizeof ( *grh ) ) : NULL );
  75. lrh = iob_push ( iobuf, sizeof ( *lrh ) );
  76. lrh_len = ( payload_len + iob_len ( iobuf ) - orig_iob_len );
  77. /* Construct LRH */
  78. vl = ( ( qp->ext_qpn == IB_QPN_SMI ) ? IB_VL_SMP : IB_VL_DEFAULT );
  79. lrh->vl__lver = ( vl << 4 );
  80. lnh = ( grh ? IB_LNH_GRH : IB_LNH_BTH );
  81. lrh->sl__lnh = ( ( dest->sl << 4 ) | lnh );
  82. lrh->dlid = htons ( dest->lid );
  83. lrh->length = htons ( lrh_len >> 2 );
  84. lrh->slid = htons ( ibdev->lid );
  85. /* Construct GRH, if required */
  86. if ( grh ) {
  87. grh->ipver__tclass__flowlabel =
  88. htonl ( IB_GRH_IPVER_IPv6 << 28 );
  89. grh->paylen = htons ( grh_len );
  90. grh->nxthdr = IB_GRH_NXTHDR_IBA;
  91. grh->hoplmt = 0;
  92. memcpy ( &grh->sgid, &ibdev->gid, sizeof ( grh->sgid ) );
  93. memcpy ( &grh->dgid, &dest->gid, sizeof ( grh->dgid ) );
  94. }
  95. /* Construct BTH */
  96. bth->opcode = BTH_OPCODE_UD_SEND;
  97. bth->se__m__padcnt__tver = ( pad_len << 4 );
  98. bth->pkey = htons ( ibdev->pkey );
  99. bth->dest_qp = htonl ( dest->qpn );
  100. bth->ack__psn = htonl ( ( qp->send.psn++ ) & 0xffffffUL );
  101. /* Construct DETH */
  102. deth->qkey = htonl ( dest->qkey );
  103. deth->src_qp = htonl ( qp->ext_qpn );
  104. DBGCP_HDA ( ibdev, 0, iobuf->data,
  105. ( iob_len ( iobuf ) - orig_iob_len ) );
  106. return 0;
  107. }
  108. /**
  109. * Remove IB headers
  110. *
  111. * @v ibdev Infiniband device
  112. * @v iobuf I/O buffer containing headers
  113. * @v qp Queue pair to fill in, or NULL
  114. * @v payload_len Payload length to fill in, or NULL
  115. * @v dest Destination address vector to fill in
  116. * @v source Source address vector to fill in
  117. * @ret rc Return status code
  118. */
  119. int ib_pull ( struct ib_device *ibdev, struct io_buffer *iobuf,
  120. struct ib_queue_pair **qp, size_t *payload_len,
  121. struct ib_address_vector *dest,
  122. struct ib_address_vector *source ) {
  123. struct ib_local_route_header *lrh;
  124. struct ib_global_route_header *grh;
  125. struct ib_base_transport_header *bth;
  126. struct ib_datagram_extended_transport_header *deth;
  127. size_t orig_iob_len = iob_len ( iobuf );
  128. unsigned int lnh;
  129. size_t pad_len;
  130. /* Clear return values */
  131. if ( qp )
  132. *qp = NULL;
  133. if ( payload_len )
  134. *payload_len = 0;
  135. memset ( dest, 0, sizeof ( *dest ) );
  136. memset ( source, 0, sizeof ( *source ) );
  137. /* Extract LRH */
  138. if ( iob_len ( iobuf ) < sizeof ( *lrh ) ) {
  139. DBGC ( ibdev, "IBDEV %s RX too short (%zd bytes) for LRH\n",
  140. ibdev->name, iob_len ( iobuf ) );
  141. return -EINVAL;
  142. }
  143. lrh = iobuf->data;
  144. iob_pull ( iobuf, sizeof ( *lrh ) );
  145. dest->lid = ntohs ( lrh->dlid );
  146. dest->sl = ( lrh->sl__lnh >> 4 );
  147. source->lid = ntohs ( lrh->slid );
  148. source->sl = ( lrh->sl__lnh >> 4 );
  149. lnh = ( lrh->sl__lnh & 0x3 );
  150. /* Reject unsupported packets */
  151. if ( ! ( ( lnh == IB_LNH_BTH ) || ( lnh == IB_LNH_GRH ) ) ) {
  152. DBGC ( ibdev, "IBDEV %s RX unsupported LNH %x\n",
  153. ibdev->name, lnh );
  154. return -ENOTSUP;
  155. }
  156. /* Extract GRH, if present */
  157. if ( lnh == IB_LNH_GRH ) {
  158. if ( iob_len ( iobuf ) < sizeof ( *grh ) ) {
  159. DBGC ( ibdev, "IBDEV %s RX too short (%zd bytes) "
  160. "for GRH\n", ibdev->name, iob_len ( iobuf ) );
  161. return -EINVAL;
  162. }
  163. grh = iobuf->data;
  164. iob_pull ( iobuf, sizeof ( *grh ) );
  165. dest->gid_present = 1;
  166. memcpy ( &dest->gid, &grh->dgid, sizeof ( dest->gid ) );
  167. source->gid_present = 1;
  168. memcpy ( &source->gid, &grh->sgid, sizeof ( source->gid ) );
  169. } else {
  170. grh = NULL;
  171. }
  172. /* Extract BTH */
  173. if ( iob_len ( iobuf ) < sizeof ( *bth ) ) {
  174. DBGC ( ibdev, "IBDEV %s RX too short (%zd bytes) for BTH\n",
  175. ibdev->name, iob_len ( iobuf ) );
  176. return -EINVAL;
  177. }
  178. bth = iobuf->data;
  179. iob_pull ( iobuf, sizeof ( *bth ) );
  180. if ( bth->opcode != BTH_OPCODE_UD_SEND ) {
  181. DBGC ( ibdev, "IBDEV %s unsupported BTH opcode %x\n",
  182. ibdev->name, bth->opcode );
  183. return -ENOTSUP;
  184. }
  185. dest->qpn = ntohl ( bth->dest_qp );
  186. /* Extract DETH */
  187. if ( iob_len ( iobuf ) < sizeof ( *deth ) ) {
  188. DBGC ( ibdev, "IBDEV %s RX too short (%zd bytes) for DETH\n",
  189. ibdev->name, iob_len ( iobuf ) );
  190. return -EINVAL;
  191. }
  192. deth = iobuf->data;
  193. iob_pull ( iobuf, sizeof ( *deth ) );
  194. source->qpn = ntohl ( deth->src_qp );
  195. source->qkey = ntohl ( deth->qkey );
  196. /* Calculate payload length, if applicable */
  197. if ( payload_len ) {
  198. pad_len = ( ( bth->se__m__padcnt__tver >> 4 ) & 0x3 );
  199. *payload_len = ( ( ntohs ( lrh->length ) << 2 )
  200. - ( orig_iob_len - iob_len ( iobuf ) )
  201. - pad_len - 4 /* ICRC */ );
  202. }
  203. /* Determine destination QP, if applicable */
  204. if ( qp ) {
  205. if ( IB_LID_MULTICAST ( dest->lid ) && grh ) {
  206. if ( ! ( *qp = ib_find_qp_mgid ( ibdev, &grh->dgid ))){
  207. DBGC ( ibdev, "IBDEV %s RX for unknown MGID "
  208. IB_GID_FMT "\n", ibdev->name,
  209. IB_GID_ARGS ( &grh->dgid ) );
  210. return -ENODEV;
  211. }
  212. } else {
  213. if ( ! ( *qp = ib_find_qp_qpn ( ibdev, dest->qpn ) ) ) {
  214. DBGC ( ibdev, "IBDEV %s RX for nonexistent "
  215. "QPN %#lx\n", ibdev->name, dest->qpn );
  216. return -ENODEV;
  217. }
  218. }
  219. assert ( *qp );
  220. }
  221. DBGC2 ( ibdev, "IBDEV %s RX %04x:%08lx <= %04x:%08lx (key %08x)\n",
  222. ibdev->name, dest->lid,
  223. ( IB_LID_MULTICAST ( dest->lid ) ?
  224. ( qp ? (*qp)->ext_qpn : -1UL ) : dest->qpn ),
  225. source->lid, source->qpn, ntohl ( deth->qkey ) );
  226. DBGCP_HDA ( ibdev, 0,
  227. ( iobuf->data - ( orig_iob_len - iob_len ( iobuf ) ) ),
  228. ( orig_iob_len - iob_len ( iobuf ) ) );
  229. return 0;
  230. }