You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ib_cm.c 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. * Copyright (C) 2009 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., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. FILE_LICENCE ( GPL2_OR_LATER );
  19. #include <stdint.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <byteswap.h>
  23. #include <errno.h>
  24. #include <assert.h>
  25. #include <gpxe/infiniband.h>
  26. #include <gpxe/ib_mi.h>
  27. #include <gpxe/ib_pathrec.h>
  28. #include <gpxe/ib_cm.h>
  29. /**
  30. * @file
  31. *
  32. * Infiniband communication management
  33. *
  34. */
  35. /**
  36. * Send "ready to use" response
  37. *
  38. * @v ibdev Infiniband device
  39. * @v mi Management interface
  40. * @v conn Connection
  41. * @v av Address vector
  42. * @ret rc Return status code
  43. */
  44. static int ib_cm_send_rtu ( struct ib_device *ibdev,
  45. struct ib_mad_interface *mi,
  46. struct ib_connection *conn,
  47. struct ib_address_vector *av ) {
  48. union ib_mad mad;
  49. struct ib_cm_ready_to_use *ready =
  50. &mad.cm.cm_data.ready_to_use;
  51. int rc;
  52. /* Construct "ready to use" response */
  53. memset ( &mad, 0, sizeof ( mad ) );
  54. mad.hdr.mgmt_class = IB_MGMT_CLASS_CM;
  55. mad.hdr.class_version = IB_CM_CLASS_VERSION;
  56. mad.hdr.method = IB_MGMT_METHOD_SEND;
  57. mad.hdr.attr_id = htons ( IB_CM_ATTR_READY_TO_USE );
  58. ready->local_id = htonl ( conn->local_id );
  59. ready->remote_id = htonl ( conn->remote_id );
  60. if ( ( rc = ib_mi_send ( ibdev, mi, &mad, av ) ) != 0 ){
  61. DBGC ( conn, "CM %p could not send RTU: %s\n",
  62. conn, strerror ( rc ) );
  63. return rc;
  64. }
  65. return 0;
  66. }
  67. /**
  68. * Handle connection request transaction completion
  69. *
  70. * @v ibdev Infiniband device
  71. * @v mi Management interface
  72. * @v madx Management transaction
  73. * @v rc Status code
  74. * @v mad Received MAD (or NULL on error)
  75. * @v av Source address vector (or NULL on error)
  76. */
  77. static void ib_cm_req_complete ( struct ib_device *ibdev,
  78. struct ib_mad_interface *mi,
  79. struct ib_mad_transaction *madx,
  80. int rc, union ib_mad *mad,
  81. struct ib_address_vector *av ) {
  82. struct ib_connection *conn = ib_madx_get_ownerdata ( madx );
  83. struct ib_queue_pair *qp = conn->qp;
  84. struct ib_cm_common *common = &mad->cm.cm_data.common;
  85. struct ib_cm_connect_reply *connect_rep =
  86. &mad->cm.cm_data.connect_reply;
  87. struct ib_cm_connect_reject *connect_rej =
  88. &mad->cm.cm_data.connect_reject;
  89. void *private_data = NULL;
  90. size_t private_data_len = 0;
  91. /* Report failures */
  92. if ( rc != 0 ) {
  93. DBGC ( conn, "CM %p connection request failed: %s\n",
  94. conn, strerror ( rc ) );
  95. goto out;
  96. }
  97. /* Record remote communication ID */
  98. conn->remote_id = ntohl ( common->local_id );
  99. /* Handle response */
  100. switch ( mad->hdr.attr_id ) {
  101. case htons ( IB_CM_ATTR_CONNECT_REPLY ) :
  102. /* Extract fields */
  103. qp->av.qpn = ( ntohl ( connect_rep->local_qpn ) >> 8 );
  104. qp->send.psn = ( ntohl ( connect_rep->starting_psn ) >> 8 );
  105. private_data = &connect_rep->private_data;
  106. private_data_len = sizeof ( connect_rep->private_data );
  107. DBGC ( conn, "CM %p connected to QPN %lx PSN %x\n",
  108. conn, qp->av.qpn, qp->send.psn );
  109. /* Modify queue pair */
  110. if ( ( rc = ib_modify_qp ( ibdev, qp ) ) != 0 ) {
  111. DBGC ( conn, "CM %p could not modify queue pair: %s\n",
  112. conn, strerror ( rc ) );
  113. goto out;
  114. }
  115. /* Send "ready to use" reply */
  116. if ( ( rc = ib_cm_send_rtu ( ibdev, mi, conn, av ) ) != 0 ) {
  117. /* Treat as non-fatal */
  118. rc = 0;
  119. }
  120. break;
  121. case htons ( IB_CM_ATTR_CONNECT_REJECT ) :
  122. /* Extract fields */
  123. DBGC ( conn, "CM %p connection rejected (reason %d)\n",
  124. conn, ntohs ( connect_rej->reason ) );
  125. private_data = &connect_rej->private_data;
  126. private_data_len = sizeof ( connect_rej->private_data );
  127. rc = -ENOTCONN;
  128. break;
  129. default:
  130. DBGC ( conn, "CM %p unexpected response (attribute %04x)\n",
  131. conn, ntohs ( mad->hdr.attr_id ) );
  132. rc = -EIO;
  133. break;
  134. }
  135. out:
  136. /* Destroy the completed transaction */
  137. ib_destroy_madx ( ibdev, ibdev->gsi, madx );
  138. conn->madx = NULL;
  139. /* Hand off to the upper completion handler */
  140. conn->op->changed ( ibdev, qp, conn, rc, private_data,
  141. private_data_len );
  142. }
  143. /** Connection request operations */
  144. static struct ib_mad_transaction_operations ib_cm_req_op = {
  145. .complete = ib_cm_req_complete,
  146. };
  147. /**
  148. * Handle connection path transaction completion
  149. *
  150. * @v ibdev Infiniband device
  151. * @v path Path
  152. * @v rc Status code
  153. * @v av Address vector, or NULL on error
  154. */
  155. static void ib_cm_path_complete ( struct ib_device *ibdev,
  156. struct ib_path *path, int rc,
  157. struct ib_address_vector *av ) {
  158. struct ib_connection *conn = ib_path_get_ownerdata ( path );
  159. struct ib_queue_pair *qp = conn->qp;
  160. union ib_mad mad;
  161. struct ib_cm_connect_request *connect_req =
  162. &mad.cm.cm_data.connect_request;
  163. size_t private_data_len;
  164. /* Report failures */
  165. if ( rc != 0 ) {
  166. DBGC ( conn, "CM %p path lookup failed: %s\n",
  167. conn, strerror ( rc ) );
  168. conn->op->changed ( ibdev, qp, conn, rc, NULL, 0 );
  169. goto out;
  170. }
  171. /* Update queue pair peer path */
  172. memcpy ( &qp->av, av, sizeof ( qp->av ) );
  173. /* Construct connection request */
  174. memset ( &mad, 0, sizeof ( mad ) );
  175. mad.hdr.mgmt_class = IB_MGMT_CLASS_CM;
  176. mad.hdr.class_version = IB_CM_CLASS_VERSION;
  177. mad.hdr.method = IB_MGMT_METHOD_SEND;
  178. mad.hdr.attr_id = htons ( IB_CM_ATTR_CONNECT_REQUEST );
  179. connect_req->local_id = htonl ( conn->local_id );
  180. memcpy ( &connect_req->service_id, &conn->service_id,
  181. sizeof ( connect_req->service_id ) );
  182. ib_get_hca_info ( ibdev, &connect_req->local_ca );
  183. connect_req->local_qpn__responder_resources =
  184. htonl ( ( qp->qpn << 8 ) | 1 );
  185. connect_req->local_eecn__initiator_depth = htonl ( ( 0 << 8 ) | 1 );
  186. connect_req->remote_eecn__remote_timeout__service_type__ee_flow_ctrl =
  187. htonl ( ( 0x14 << 3 ) | ( IB_CM_TRANSPORT_RC << 1 ) |
  188. ( 0 << 0 ) );
  189. connect_req->starting_psn__local_timeout__retry_count =
  190. htonl ( ( qp->recv.psn << 8 ) | ( 0x14 << 3 ) |
  191. ( 0x07 << 0 ) );
  192. connect_req->pkey = htons ( ibdev->pkey );
  193. connect_req->payload_mtu__rdc_exists__rnr_retry =
  194. ( ( IB_MTU_2048 << 4 ) | ( 1 << 3 ) | ( 0x07 << 0 ) );
  195. connect_req->max_cm_retries__srq =
  196. ( ( 0x0f << 4 ) | ( 0 << 3 ) );
  197. connect_req->primary.local_lid = htons ( ibdev->lid );
  198. connect_req->primary.remote_lid = htons ( conn->qp->av.lid );
  199. memcpy ( &connect_req->primary.local_gid, &ibdev->gid,
  200. sizeof ( connect_req->primary.local_gid ) );
  201. memcpy ( &connect_req->primary.remote_gid, &conn->qp->av.gid,
  202. sizeof ( connect_req->primary.remote_gid ) );
  203. connect_req->primary.flow_label__rate =
  204. htonl ( ( 0 << 12 ) | ( conn->qp->av.rate << 0 ) );
  205. connect_req->primary.hop_limit = 0;
  206. connect_req->primary.sl__subnet_local =
  207. ( ( conn->qp->av.sl << 4 ) | ( 1 << 3 ) );
  208. connect_req->primary.local_ack_timeout = ( 0x13 << 3 );
  209. private_data_len = conn->private_data_len;
  210. if ( private_data_len > sizeof ( connect_req->private_data ) )
  211. private_data_len = sizeof ( connect_req->private_data );
  212. memcpy ( &connect_req->private_data, &conn->private_data,
  213. private_data_len );
  214. /* Create connection request */
  215. conn->madx = ib_create_madx ( ibdev, ibdev->gsi, &mad, NULL,
  216. &ib_cm_req_op );
  217. if ( ! conn->madx ) {
  218. DBGC ( conn, "CM %p could not create connection request\n",
  219. conn );
  220. conn->op->changed ( ibdev, qp, conn, rc, NULL, 0 );
  221. goto out;
  222. }
  223. ib_madx_set_ownerdata ( conn->madx, conn );
  224. out:
  225. /* Destroy the completed transaction */
  226. ib_destroy_path ( ibdev, path );
  227. conn->path = NULL;
  228. }
  229. /** Connection path operations */
  230. static struct ib_path_operations ib_cm_path_op = {
  231. .complete = ib_cm_path_complete,
  232. };
  233. /**
  234. * Create connection to remote QP
  235. *
  236. * @v ibdev Infiniband device
  237. * @v qp Queue pair
  238. * @v dgid Target GID
  239. * @v service_id Target service ID
  240. * @v private_data Connection request private data
  241. * @v private_data_len Length of connection request private data
  242. * @v op Connection operations
  243. * @ret conn Connection
  244. */
  245. struct ib_connection *
  246. ib_create_conn ( struct ib_device *ibdev, struct ib_queue_pair *qp,
  247. struct ib_gid *dgid, struct ib_gid_half *service_id,
  248. void *private_data, size_t private_data_len,
  249. struct ib_connection_operations *op ) {
  250. struct ib_connection *conn;
  251. /* Allocate and initialise request */
  252. conn = zalloc ( sizeof ( *conn ) + private_data_len );
  253. if ( ! conn )
  254. goto err_alloc_conn;
  255. conn->ibdev = ibdev;
  256. conn->qp = qp;
  257. memset ( &qp->av, 0, sizeof ( qp->av ) );
  258. qp->av.gid_present = 1;
  259. memcpy ( &qp->av.gid, dgid, sizeof ( qp->av.gid ) );
  260. conn->local_id = random();
  261. memcpy ( &conn->service_id, service_id, sizeof ( conn->service_id ) );
  262. conn->op = op;
  263. conn->private_data_len = private_data_len;
  264. memcpy ( &conn->private_data, private_data, private_data_len );
  265. /* Create path */
  266. conn->path = ib_create_path ( ibdev, &qp->av, &ib_cm_path_op );
  267. if ( ! conn->path )
  268. goto err_create_path;
  269. ib_path_set_ownerdata ( conn->path, conn );
  270. DBGC ( conn, "CM %p created for IBDEV %p QPN %lx\n",
  271. conn, ibdev, qp->qpn );
  272. DBGC ( conn, "CM %p connecting to %08x:%08x:%08x:%08x %08x:%08x\n",
  273. conn, ntohl ( dgid->u.dwords[0] ), ntohl ( dgid->u.dwords[1] ),
  274. ntohl ( dgid->u.dwords[2] ), ntohl ( dgid->u.dwords[3] ),
  275. ntohl ( service_id->u.dwords[0] ),
  276. ntohl ( service_id->u.dwords[1] ) );
  277. return conn;
  278. ib_destroy_path ( ibdev, conn->path );
  279. err_create_path:
  280. free ( conn );
  281. err_alloc_conn:
  282. return NULL;
  283. }
  284. /**
  285. * Destroy connection to remote QP
  286. *
  287. * @v ibdev Infiniband device
  288. * @v qp Queue pair
  289. * @v conn Connection
  290. */
  291. void ib_destroy_conn ( struct ib_device *ibdev,
  292. struct ib_queue_pair *qp __unused,
  293. struct ib_connection *conn ) {
  294. if ( conn->madx )
  295. ib_destroy_madx ( ibdev, ibdev->gsi, conn->madx );
  296. if ( conn->path )
  297. ib_destroy_path ( ibdev, conn->path );
  298. free ( conn );
  299. }