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 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. /** List of connections */
  36. static LIST_HEAD ( ib_cm_conns );
  37. /**
  38. * Send "ready to use" response
  39. *
  40. * @v ibdev Infiniband device
  41. * @v mi Management interface
  42. * @v conn Connection
  43. * @v av Address vector
  44. * @ret rc Return status code
  45. */
  46. static int ib_cm_send_rtu ( struct ib_device *ibdev,
  47. struct ib_mad_interface *mi,
  48. struct ib_connection *conn,
  49. struct ib_address_vector *av ) {
  50. union ib_mad mad;
  51. struct ib_cm_ready_to_use *ready =
  52. &mad.cm.cm_data.ready_to_use;
  53. int rc;
  54. /* Construct "ready to use" response */
  55. memset ( &mad, 0, sizeof ( mad ) );
  56. mad.hdr.mgmt_class = IB_MGMT_CLASS_CM;
  57. mad.hdr.class_version = IB_CM_CLASS_VERSION;
  58. mad.hdr.method = IB_MGMT_METHOD_SEND;
  59. mad.hdr.attr_id = htons ( IB_CM_ATTR_READY_TO_USE );
  60. ready->local_id = htonl ( conn->local_id );
  61. ready->remote_id = htonl ( conn->remote_id );
  62. if ( ( rc = ib_mi_send ( ibdev, mi, &mad, av ) ) != 0 ){
  63. DBGC ( conn, "CM %p could not send RTU: %s\n",
  64. conn, strerror ( rc ) );
  65. return rc;
  66. }
  67. return 0;
  68. }
  69. /**
  70. * Handle duplicate connection replies
  71. *
  72. * @v ibdev Infiniband device
  73. * @v mi Management interface
  74. * @v mad Received MAD
  75. * @v av Source address vector
  76. * @ret rc Return status code
  77. *
  78. * If a "ready to use" MAD is lost, the peer may resend the connection
  79. * reply. We have to respond to these with duplicate "ready to use"
  80. * MADs, otherwise the peer may time out and drop the connection.
  81. */
  82. static void ib_cm_connect_rep ( struct ib_device *ibdev,
  83. struct ib_mad_interface *mi,
  84. union ib_mad *mad,
  85. struct ib_address_vector *av ) {
  86. struct ib_cm_connect_reply *connect_rep =
  87. &mad->cm.cm_data.connect_reply;
  88. struct ib_connection *conn;
  89. int rc;
  90. /* Identify connection */
  91. list_for_each_entry ( conn, &ib_cm_conns, list ) {
  92. if ( ntohl ( connect_rep->remote_id ) != conn->local_id )
  93. continue;
  94. /* Try to send "ready to use" reply */
  95. if ( ( rc = ib_cm_send_rtu ( ibdev, mi, conn, av ) ) != 0 ) {
  96. /* Ignore errors */
  97. return;
  98. }
  99. return;
  100. }
  101. DBG ( "CM unidentified connection %08x\n",
  102. ntohl ( connect_rep->remote_id ) );
  103. }
  104. /** Communication management agents */
  105. struct ib_mad_agent ib_cm_agent[] __ib_mad_agent = {
  106. {
  107. .mgmt_class = IB_MGMT_CLASS_CM,
  108. .class_version = IB_CM_CLASS_VERSION,
  109. .attr_id = htons ( IB_CM_ATTR_CONNECT_REPLY ),
  110. .handle = ib_cm_connect_rep,
  111. },
  112. };
  113. /**
  114. * Handle connection request transaction completion
  115. *
  116. * @v ibdev Infiniband device
  117. * @v mi Management interface
  118. * @v madx Management transaction
  119. * @v rc Status code
  120. * @v mad Received MAD (or NULL on error)
  121. * @v av Source address vector (or NULL on error)
  122. */
  123. static void ib_cm_req_complete ( struct ib_device *ibdev,
  124. struct ib_mad_interface *mi,
  125. struct ib_mad_transaction *madx,
  126. int rc, union ib_mad *mad,
  127. struct ib_address_vector *av ) {
  128. struct ib_connection *conn = ib_madx_get_ownerdata ( madx );
  129. struct ib_queue_pair *qp = conn->qp;
  130. struct ib_cm_common *common = &mad->cm.cm_data.common;
  131. struct ib_cm_connect_reply *connect_rep =
  132. &mad->cm.cm_data.connect_reply;
  133. struct ib_cm_connect_reject *connect_rej =
  134. &mad->cm.cm_data.connect_reject;
  135. void *private_data = NULL;
  136. size_t private_data_len = 0;
  137. /* Report failures */
  138. if ( ( rc == 0 ) && ( mad->hdr.status != htons ( IB_MGMT_STATUS_OK ) ))
  139. rc = -EIO;
  140. if ( rc != 0 ) {
  141. DBGC ( conn, "CM %p connection request failed: %s\n",
  142. conn, strerror ( rc ) );
  143. goto out;
  144. }
  145. /* Record remote communication ID */
  146. conn->remote_id = ntohl ( common->local_id );
  147. /* Handle response */
  148. switch ( mad->hdr.attr_id ) {
  149. case htons ( IB_CM_ATTR_CONNECT_REPLY ) :
  150. /* Extract fields */
  151. qp->av.qpn = ( ntohl ( connect_rep->local_qpn ) >> 8 );
  152. qp->send.psn = ( ntohl ( connect_rep->starting_psn ) >> 8 );
  153. private_data = &connect_rep->private_data;
  154. private_data_len = sizeof ( connect_rep->private_data );
  155. DBGC ( conn, "CM %p connected to QPN %lx PSN %x\n",
  156. conn, qp->av.qpn, qp->send.psn );
  157. /* Modify queue pair */
  158. if ( ( rc = ib_modify_qp ( ibdev, qp ) ) != 0 ) {
  159. DBGC ( conn, "CM %p could not modify queue pair: %s\n",
  160. conn, strerror ( rc ) );
  161. goto out;
  162. }
  163. /* Send "ready to use" reply */
  164. if ( ( rc = ib_cm_send_rtu ( ibdev, mi, conn, av ) ) != 0 ) {
  165. /* Treat as non-fatal */
  166. rc = 0;
  167. }
  168. break;
  169. case htons ( IB_CM_ATTR_CONNECT_REJECT ) :
  170. /* Extract fields */
  171. DBGC ( conn, "CM %p connection rejected (reason %d)\n",
  172. conn, ntohs ( connect_rej->reason ) );
  173. private_data = &connect_rej->private_data;
  174. private_data_len = sizeof ( connect_rej->private_data );
  175. rc = -ENOTCONN;
  176. break;
  177. default:
  178. DBGC ( conn, "CM %p unexpected response (attribute %04x)\n",
  179. conn, ntohs ( mad->hdr.attr_id ) );
  180. rc = -ENOTSUP;
  181. break;
  182. }
  183. out:
  184. /* Destroy the completed transaction */
  185. ib_destroy_madx ( ibdev, ibdev->gsi, madx );
  186. conn->madx = NULL;
  187. /* Hand off to the upper completion handler */
  188. conn->op->changed ( ibdev, qp, conn, rc, private_data,
  189. private_data_len );
  190. }
  191. /** Connection request operations */
  192. static struct ib_mad_transaction_operations ib_cm_req_op = {
  193. .complete = ib_cm_req_complete,
  194. };
  195. /**
  196. * Handle connection path transaction completion
  197. *
  198. * @v ibdev Infiniband device
  199. * @v path Path
  200. * @v rc Status code
  201. * @v av Address vector, or NULL on error
  202. */
  203. static void ib_cm_path_complete ( struct ib_device *ibdev,
  204. struct ib_path *path, int rc,
  205. struct ib_address_vector *av ) {
  206. struct ib_connection *conn = ib_path_get_ownerdata ( path );
  207. struct ib_queue_pair *qp = conn->qp;
  208. union ib_mad mad;
  209. struct ib_cm_connect_request *connect_req =
  210. &mad.cm.cm_data.connect_request;
  211. size_t private_data_len;
  212. /* Report failures */
  213. if ( rc != 0 ) {
  214. DBGC ( conn, "CM %p path lookup failed: %s\n",
  215. conn, strerror ( rc ) );
  216. conn->op->changed ( ibdev, qp, conn, rc, NULL, 0 );
  217. goto out;
  218. }
  219. /* Update queue pair peer path */
  220. memcpy ( &qp->av, av, sizeof ( qp->av ) );
  221. /* Construct connection request */
  222. memset ( &mad, 0, sizeof ( mad ) );
  223. mad.hdr.mgmt_class = IB_MGMT_CLASS_CM;
  224. mad.hdr.class_version = IB_CM_CLASS_VERSION;
  225. mad.hdr.method = IB_MGMT_METHOD_SEND;
  226. mad.hdr.attr_id = htons ( IB_CM_ATTR_CONNECT_REQUEST );
  227. connect_req->local_id = htonl ( conn->local_id );
  228. memcpy ( &connect_req->service_id, &conn->service_id,
  229. sizeof ( connect_req->service_id ) );
  230. ib_get_hca_info ( ibdev, &connect_req->local_ca );
  231. connect_req->local_qpn__responder_resources =
  232. htonl ( ( qp->qpn << 8 ) | 1 );
  233. connect_req->local_eecn__initiator_depth = htonl ( ( 0 << 8 ) | 1 );
  234. connect_req->remote_eecn__remote_timeout__service_type__ee_flow_ctrl =
  235. htonl ( ( 0x14 << 3 ) | ( IB_CM_TRANSPORT_RC << 1 ) |
  236. ( 0 << 0 ) );
  237. connect_req->starting_psn__local_timeout__retry_count =
  238. htonl ( ( qp->recv.psn << 8 ) | ( 0x14 << 3 ) |
  239. ( 0x07 << 0 ) );
  240. connect_req->pkey = htons ( ibdev->pkey );
  241. connect_req->payload_mtu__rdc_exists__rnr_retry =
  242. ( ( IB_MTU_2048 << 4 ) | ( 1 << 3 ) | ( 0x07 << 0 ) );
  243. connect_req->max_cm_retries__srq =
  244. ( ( 0x0f << 4 ) | ( 0 << 3 ) );
  245. connect_req->primary.local_lid = htons ( ibdev->lid );
  246. connect_req->primary.remote_lid = htons ( conn->qp->av.lid );
  247. memcpy ( &connect_req->primary.local_gid, &ibdev->gid,
  248. sizeof ( connect_req->primary.local_gid ) );
  249. memcpy ( &connect_req->primary.remote_gid, &conn->qp->av.gid,
  250. sizeof ( connect_req->primary.remote_gid ) );
  251. connect_req->primary.flow_label__rate =
  252. htonl ( ( 0 << 12 ) | ( conn->qp->av.rate << 0 ) );
  253. connect_req->primary.hop_limit = 0;
  254. connect_req->primary.sl__subnet_local =
  255. ( ( conn->qp->av.sl << 4 ) | ( 1 << 3 ) );
  256. connect_req->primary.local_ack_timeout = ( 0x13 << 3 );
  257. private_data_len = conn->private_data_len;
  258. if ( private_data_len > sizeof ( connect_req->private_data ) )
  259. private_data_len = sizeof ( connect_req->private_data );
  260. memcpy ( &connect_req->private_data, &conn->private_data,
  261. private_data_len );
  262. /* Create connection request */
  263. conn->madx = ib_create_madx ( ibdev, ibdev->gsi, &mad, NULL,
  264. &ib_cm_req_op );
  265. if ( ! conn->madx ) {
  266. DBGC ( conn, "CM %p could not create connection request\n",
  267. conn );
  268. conn->op->changed ( ibdev, qp, conn, rc, NULL, 0 );
  269. goto out;
  270. }
  271. ib_madx_set_ownerdata ( conn->madx, conn );
  272. out:
  273. /* Destroy the completed transaction */
  274. ib_destroy_path ( ibdev, path );
  275. conn->path = NULL;
  276. }
  277. /** Connection path operations */
  278. static struct ib_path_operations ib_cm_path_op = {
  279. .complete = ib_cm_path_complete,
  280. };
  281. /**
  282. * Create connection to remote QP
  283. *
  284. * @v ibdev Infiniband device
  285. * @v qp Queue pair
  286. * @v dgid Target GID
  287. * @v service_id Target service ID
  288. * @v private_data Connection request private data
  289. * @v private_data_len Length of connection request private data
  290. * @v op Connection operations
  291. * @ret conn Connection
  292. */
  293. struct ib_connection *
  294. ib_create_conn ( struct ib_device *ibdev, struct ib_queue_pair *qp,
  295. struct ib_gid *dgid, struct ib_gid_half *service_id,
  296. void *private_data, size_t private_data_len,
  297. struct ib_connection_operations *op ) {
  298. struct ib_connection *conn;
  299. /* Allocate and initialise request */
  300. conn = zalloc ( sizeof ( *conn ) + private_data_len );
  301. if ( ! conn )
  302. goto err_alloc_conn;
  303. conn->ibdev = ibdev;
  304. conn->qp = qp;
  305. memset ( &qp->av, 0, sizeof ( qp->av ) );
  306. qp->av.gid_present = 1;
  307. memcpy ( &qp->av.gid, dgid, sizeof ( qp->av.gid ) );
  308. conn->local_id = random();
  309. memcpy ( &conn->service_id, service_id, sizeof ( conn->service_id ) );
  310. conn->op = op;
  311. conn->private_data_len = private_data_len;
  312. memcpy ( &conn->private_data, private_data, private_data_len );
  313. /* Create path */
  314. conn->path = ib_create_path ( ibdev, &qp->av, &ib_cm_path_op );
  315. if ( ! conn->path )
  316. goto err_create_path;
  317. ib_path_set_ownerdata ( conn->path, conn );
  318. /* Add to list of connections */
  319. list_add ( &conn->list, &ib_cm_conns );
  320. DBGC ( conn, "CM %p created for IBDEV %p QPN %lx\n",
  321. conn, ibdev, qp->qpn );
  322. DBGC ( conn, "CM %p connecting to %08x:%08x:%08x:%08x %08x:%08x\n",
  323. conn, ntohl ( dgid->u.dwords[0] ), ntohl ( dgid->u.dwords[1] ),
  324. ntohl ( dgid->u.dwords[2] ), ntohl ( dgid->u.dwords[3] ),
  325. ntohl ( service_id->u.dwords[0] ),
  326. ntohl ( service_id->u.dwords[1] ) );
  327. return conn;
  328. ib_destroy_path ( ibdev, conn->path );
  329. err_create_path:
  330. free ( conn );
  331. err_alloc_conn:
  332. return NULL;
  333. }
  334. /**
  335. * Destroy connection to remote QP
  336. *
  337. * @v ibdev Infiniband device
  338. * @v qp Queue pair
  339. * @v conn Connection
  340. */
  341. void ib_destroy_conn ( struct ib_device *ibdev,
  342. struct ib_queue_pair *qp __unused,
  343. struct ib_connection *conn ) {
  344. list_del ( &conn->list );
  345. if ( conn->madx )
  346. ib_destroy_madx ( ibdev, ibdev->gsi, conn->madx );
  347. if ( conn->path )
  348. ib_destroy_path ( ibdev, conn->path );
  349. free ( conn );
  350. }