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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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., 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 <byteswap.h>
  28. #include <errno.h>
  29. #include <assert.h>
  30. #include <ipxe/infiniband.h>
  31. #include <ipxe/ib_mi.h>
  32. #include <ipxe/ib_pathrec.h>
  33. #include <ipxe/ib_cm.h>
  34. /**
  35. * @file
  36. *
  37. * Infiniband communication management
  38. *
  39. */
  40. /** List of connections */
  41. static LIST_HEAD ( ib_cm_conns );
  42. /**
  43. * Find connection by local communication ID
  44. *
  45. * @v local_id Local communication ID
  46. * @ret conn Connection, or NULL
  47. */
  48. static struct ib_connection * ib_cm_find ( uint32_t local_id ) {
  49. struct ib_connection *conn;
  50. list_for_each_entry ( conn, &ib_cm_conns, list ) {
  51. if ( conn->local_id == local_id )
  52. return conn;
  53. }
  54. return NULL;
  55. }
  56. /**
  57. * Send "ready to use" response
  58. *
  59. * @v ibdev Infiniband device
  60. * @v mi Management interface
  61. * @v av Address vector
  62. * @v local_id Local communication ID
  63. * @v remote_id Remote communication ID
  64. * @ret rc Return status code
  65. */
  66. static int ib_cm_send_rtu ( struct ib_device *ibdev,
  67. struct ib_mad_interface *mi,
  68. struct ib_address_vector *av,
  69. uint32_t local_id, uint32_t remote_id ) {
  70. union ib_mad mad;
  71. struct ib_cm_ready_to_use *rtu = &mad.cm.cm_data.ready_to_use;
  72. int rc;
  73. /* Construct "ready to use" response */
  74. memset ( &mad, 0, sizeof ( mad ) );
  75. mad.hdr.mgmt_class = IB_MGMT_CLASS_CM;
  76. mad.hdr.class_version = IB_CM_CLASS_VERSION;
  77. mad.hdr.method = IB_MGMT_METHOD_SEND;
  78. mad.hdr.attr_id = htons ( IB_CM_ATTR_READY_TO_USE );
  79. rtu->local_id = htonl ( local_id );
  80. rtu->remote_id = htonl ( remote_id );
  81. if ( ( rc = ib_mi_send ( ibdev, mi, &mad, av ) ) != 0 ){
  82. DBG ( "CM could not send RTU: %s\n", strerror ( rc ) );
  83. return rc;
  84. }
  85. return 0;
  86. }
  87. /**
  88. * Handle duplicate connection replies
  89. *
  90. * @v ibdev Infiniband device
  91. * @v mi Management interface
  92. * @v mad Received MAD
  93. * @v av Source address vector
  94. * @ret rc Return status code
  95. *
  96. * If a "ready to use" MAD is lost, the peer may resend the connection
  97. * reply. We have to respond to these with duplicate "ready to use"
  98. * MADs, otherwise the peer may time out and drop the connection.
  99. */
  100. static void ib_cm_recv_rep ( struct ib_device *ibdev,
  101. struct ib_mad_interface *mi,
  102. union ib_mad *mad,
  103. struct ib_address_vector *av ) {
  104. struct ib_cm_connect_reply *rep = &mad->cm.cm_data.connect_reply;
  105. struct ib_connection *conn;
  106. uint32_t local_id = ntohl ( rep->remote_id );
  107. int rc;
  108. /* Identify connection */
  109. conn = ib_cm_find ( local_id );
  110. if ( conn ) {
  111. /* Try to send "ready to use" reply */
  112. if ( ( rc = ib_cm_send_rtu ( ibdev, mi, av, conn->local_id,
  113. conn->remote_id ) ) != 0 ) {
  114. /* Ignore errors; the remote end will retry */
  115. }
  116. } else {
  117. DBG ( "CM unidentified connection %08x\n", local_id );
  118. }
  119. }
  120. /**
  121. * Send reply to disconnection request
  122. *
  123. * @v ibdev Infiniband device
  124. * @v mi Management interface
  125. * @v av Address vector
  126. * @v local_id Local communication ID
  127. * @v remote_id Remote communication ID
  128. * @ret rc Return status code
  129. */
  130. static int ib_cm_send_drep ( struct ib_device *ibdev,
  131. struct ib_mad_interface *mi,
  132. struct ib_address_vector *av,
  133. uint32_t local_id, uint32_t remote_id ) {
  134. union ib_mad mad;
  135. struct ib_cm_disconnect_reply *drep = &mad.cm.cm_data.disconnect_reply;
  136. int rc;
  137. /* Construct reply to disconnection request */
  138. memset ( &mad, 0, sizeof ( mad ) );
  139. mad.hdr.mgmt_class = IB_MGMT_CLASS_CM;
  140. mad.hdr.class_version = IB_CM_CLASS_VERSION;
  141. mad.hdr.method = IB_MGMT_METHOD_SEND;
  142. mad.hdr.attr_id = htons ( IB_CM_ATTR_DISCONNECT_REPLY );
  143. drep->local_id = htonl ( local_id );
  144. drep->remote_id = htonl ( remote_id );
  145. if ( ( rc = ib_mi_send ( ibdev, mi, &mad, av ) ) != 0 ){
  146. DBG ( "CM could not send DREP: %s\n", strerror ( rc ) );
  147. return rc;
  148. }
  149. return 0;
  150. }
  151. /**
  152. * Handle disconnection requests
  153. *
  154. * @v ibdev Infiniband device
  155. * @v mi Management interface
  156. * @v mad Received MAD
  157. * @v av Source address vector
  158. * @ret rc Return status code
  159. */
  160. static void ib_cm_recv_dreq ( struct ib_device *ibdev,
  161. struct ib_mad_interface *mi,
  162. union ib_mad *mad,
  163. struct ib_address_vector *av ) {
  164. struct ib_cm_disconnect_request *dreq =
  165. &mad->cm.cm_data.disconnect_request;
  166. struct ib_connection *conn;
  167. uint32_t local_id = ntohl ( dreq->remote_id );
  168. uint32_t remote_id = ntohl ( dreq->local_id );
  169. int rc;
  170. /* Identify connection */
  171. conn = ib_cm_find ( local_id );
  172. if ( conn ) {
  173. /* Notify upper layer */
  174. conn->op->changed ( ibdev, conn->qp, conn, -ENOTCONN,
  175. &dreq->private_data,
  176. sizeof ( dreq->private_data ) );
  177. } else {
  178. DBG ( "CM unidentified connection %08x\n", local_id );
  179. }
  180. /* Send reply */
  181. if ( ( rc = ib_cm_send_drep ( ibdev, mi, av, local_id,
  182. remote_id ) ) != 0 ) {
  183. /* Ignore errors; the remote end will retry */
  184. }
  185. };
  186. /** Communication management agents */
  187. struct ib_mad_agent ib_cm_agent[] __ib_mad_agent = {
  188. {
  189. .mgmt_class = IB_MGMT_CLASS_CM,
  190. .class_version = IB_CM_CLASS_VERSION,
  191. .attr_id = htons ( IB_CM_ATTR_CONNECT_REPLY ),
  192. .handle = ib_cm_recv_rep,
  193. },
  194. {
  195. .mgmt_class = IB_MGMT_CLASS_CM,
  196. .class_version = IB_CM_CLASS_VERSION,
  197. .attr_id = htons ( IB_CM_ATTR_DISCONNECT_REQUEST ),
  198. .handle = ib_cm_recv_dreq,
  199. },
  200. };
  201. /**
  202. * Convert connection rejection reason to return status code
  203. *
  204. * @v reason Rejection reason (in network byte order)
  205. * @ret rc Return status code
  206. */
  207. static int ib_cm_rejection_reason_to_rc ( uint16_t reason ) {
  208. switch ( reason ) {
  209. case htons ( IB_CM_REJECT_BAD_SERVICE_ID ) :
  210. return -ENODEV;
  211. case htons ( IB_CM_REJECT_STALE_CONN ) :
  212. return -EALREADY;
  213. case htons ( IB_CM_REJECT_CONSUMER ) :
  214. return -ENOTTY;
  215. default:
  216. return -EPERM;
  217. }
  218. }
  219. /**
  220. * Handle connection request transaction completion
  221. *
  222. * @v ibdev Infiniband device
  223. * @v mi Management interface
  224. * @v madx Management transaction
  225. * @v rc Status code
  226. * @v mad Received MAD (or NULL on error)
  227. * @v av Source address vector (or NULL on error)
  228. */
  229. static void ib_cm_req_complete ( struct ib_device *ibdev,
  230. struct ib_mad_interface *mi,
  231. struct ib_mad_transaction *madx,
  232. int rc, union ib_mad *mad,
  233. struct ib_address_vector *av ) {
  234. struct ib_connection *conn = ib_madx_get_ownerdata ( madx );
  235. struct ib_queue_pair *qp = conn->qp;
  236. struct ib_cm_common *common = &mad->cm.cm_data.common;
  237. struct ib_cm_connect_reply *rep = &mad->cm.cm_data.connect_reply;
  238. struct ib_cm_connect_reject *rej = &mad->cm.cm_data.connect_reject;
  239. void *private_data = NULL;
  240. size_t private_data_len = 0;
  241. /* Report failures */
  242. if ( ( rc == 0 ) && ( mad->hdr.status != htons ( IB_MGMT_STATUS_OK ) ))
  243. rc = -EIO;
  244. if ( rc != 0 ) {
  245. DBGC ( conn, "CM %p connection request failed: %s\n",
  246. conn, strerror ( rc ) );
  247. goto out;
  248. }
  249. /* Record remote communication ID */
  250. conn->remote_id = ntohl ( common->local_id );
  251. /* Handle response */
  252. switch ( mad->hdr.attr_id ) {
  253. case htons ( IB_CM_ATTR_CONNECT_REPLY ) :
  254. /* Extract fields */
  255. qp->av.qpn = ( ntohl ( rep->local_qpn ) >> 8 );
  256. qp->send.psn = ( ntohl ( rep->starting_psn ) >> 8 );
  257. private_data = &rep->private_data;
  258. private_data_len = sizeof ( rep->private_data );
  259. DBGC ( conn, "CM %p connected to QPN %lx PSN %x\n",
  260. conn, qp->av.qpn, qp->send.psn );
  261. /* Modify queue pair */
  262. if ( ( rc = ib_modify_qp ( ibdev, qp ) ) != 0 ) {
  263. DBGC ( conn, "CM %p could not modify queue pair: %s\n",
  264. conn, strerror ( rc ) );
  265. goto out;
  266. }
  267. /* Send "ready to use" reply */
  268. if ( ( rc = ib_cm_send_rtu ( ibdev, mi, av, conn->local_id,
  269. conn->remote_id ) ) != 0 ) {
  270. /* Treat as non-fatal */
  271. rc = 0;
  272. }
  273. break;
  274. case htons ( IB_CM_ATTR_CONNECT_REJECT ) :
  275. /* Extract fields */
  276. DBGC ( conn, "CM %p connection rejected (reason %d)\n",
  277. conn, ntohs ( rej->reason ) );
  278. /* Private data is valid only for a Consumer Reject */
  279. if ( rej->reason == htons ( IB_CM_REJECT_CONSUMER ) ) {
  280. private_data = &rej->private_data;
  281. private_data_len = sizeof ( rej->private_data );
  282. }
  283. rc = ib_cm_rejection_reason_to_rc ( rej->reason );
  284. break;
  285. default:
  286. DBGC ( conn, "CM %p unexpected response (attribute %04x)\n",
  287. conn, ntohs ( mad->hdr.attr_id ) );
  288. rc = -ENOTSUP;
  289. break;
  290. }
  291. out:
  292. /* Destroy the completed transaction */
  293. ib_destroy_madx ( ibdev, ibdev->gsi, madx );
  294. conn->madx = NULL;
  295. /* Hand off to the upper completion handler */
  296. conn->op->changed ( ibdev, qp, conn, rc, private_data,
  297. private_data_len );
  298. }
  299. /** Connection request operations */
  300. static struct ib_mad_transaction_operations ib_cm_req_op = {
  301. .complete = ib_cm_req_complete,
  302. };
  303. /**
  304. * Handle connection path transaction completion
  305. *
  306. * @v ibdev Infiniband device
  307. * @v path Path
  308. * @v rc Status code
  309. * @v av Address vector, or NULL on error
  310. */
  311. static void ib_cm_path_complete ( struct ib_device *ibdev,
  312. struct ib_path *path, int rc,
  313. struct ib_address_vector *av ) {
  314. struct ib_connection *conn = ib_path_get_ownerdata ( path );
  315. struct ib_queue_pair *qp = conn->qp;
  316. union ib_mad mad;
  317. struct ib_cm_connect_request *req = &mad.cm.cm_data.connect_request;
  318. size_t private_data_len;
  319. /* Report failures */
  320. if ( rc != 0 ) {
  321. DBGC ( conn, "CM %p path lookup failed: %s\n",
  322. conn, strerror ( rc ) );
  323. conn->op->changed ( ibdev, qp, conn, rc, NULL, 0 );
  324. goto out;
  325. }
  326. /* Update queue pair peer path */
  327. memcpy ( &qp->av, av, sizeof ( qp->av ) );
  328. /* Construct connection request */
  329. memset ( &mad, 0, sizeof ( mad ) );
  330. mad.hdr.mgmt_class = IB_MGMT_CLASS_CM;
  331. mad.hdr.class_version = IB_CM_CLASS_VERSION;
  332. mad.hdr.method = IB_MGMT_METHOD_SEND;
  333. mad.hdr.attr_id = htons ( IB_CM_ATTR_CONNECT_REQUEST );
  334. req->local_id = htonl ( conn->local_id );
  335. memcpy ( &req->service_id, &conn->service_id,
  336. sizeof ( req->service_id ) );
  337. memcpy ( &req->local_ca, &ibdev->node_guid, sizeof ( req->local_ca ) );
  338. req->local_qpn__responder_resources = htonl ( ( qp->qpn << 8 ) | 1 );
  339. req->local_eecn__initiator_depth = htonl ( ( 0 << 8 ) | 1 );
  340. req->remote_eecn__remote_timeout__service_type__ee_flow_ctrl =
  341. htonl ( ( 0x14 << 3 ) | ( IB_CM_TRANSPORT_RC << 1 ) |
  342. ( 0 << 0 ) );
  343. req->starting_psn__local_timeout__retry_count =
  344. htonl ( ( qp->recv.psn << 8 ) | ( 0x14 << 3 ) |
  345. ( 0x07 << 0 ) );
  346. req->pkey = htons ( ibdev->pkey );
  347. req->payload_mtu__rdc_exists__rnr_retry =
  348. ( ( IB_MTU_2048 << 4 ) | ( 1 << 3 ) | ( 0x07 << 0 ) );
  349. req->max_cm_retries__srq = ( ( 0x0f << 4 ) | ( 0 << 3 ) );
  350. req->primary.local_lid = htons ( ibdev->lid );
  351. req->primary.remote_lid = htons ( conn->qp->av.lid );
  352. memcpy ( &req->primary.local_gid, &ibdev->gid,
  353. sizeof ( req->primary.local_gid ) );
  354. memcpy ( &req->primary.remote_gid, &conn->qp->av.gid,
  355. sizeof ( req->primary.remote_gid ) );
  356. req->primary.flow_label__rate =
  357. htonl ( ( 0 << 12 ) | ( conn->qp->av.rate << 0 ) );
  358. req->primary.hop_limit = 0;
  359. req->primary.sl__subnet_local =
  360. ( ( conn->qp->av.sl << 4 ) | ( 1 << 3 ) );
  361. req->primary.local_ack_timeout = ( 0x13 << 3 );
  362. private_data_len = conn->private_data_len;
  363. if ( private_data_len > sizeof ( req->private_data ) )
  364. private_data_len = sizeof ( req->private_data );
  365. memcpy ( &req->private_data, &conn->private_data, private_data_len );
  366. /* Create connection request */
  367. av->qpn = IB_QPN_GSI;
  368. av->qkey = IB_QKEY_GSI;
  369. conn->madx = ib_create_madx ( ibdev, ibdev->gsi, &mad, av,
  370. &ib_cm_req_op );
  371. if ( ! conn->madx ) {
  372. DBGC ( conn, "CM %p could not create connection request\n",
  373. conn );
  374. conn->op->changed ( ibdev, qp, conn, rc, NULL, 0 );
  375. goto out;
  376. }
  377. ib_madx_set_ownerdata ( conn->madx, conn );
  378. out:
  379. /* Destroy the completed transaction */
  380. ib_destroy_path ( ibdev, path );
  381. conn->path = NULL;
  382. }
  383. /** Connection path operations */
  384. static struct ib_path_operations ib_cm_path_op = {
  385. .complete = ib_cm_path_complete,
  386. };
  387. /**
  388. * Create connection to remote QP
  389. *
  390. * @v ibdev Infiniband device
  391. * @v qp Queue pair
  392. * @v dgid Target GID
  393. * @v service_id Target service ID
  394. * @v private_data Connection request private data
  395. * @v private_data_len Length of connection request private data
  396. * @v op Connection operations
  397. * @ret conn Connection
  398. */
  399. struct ib_connection *
  400. ib_create_conn ( struct ib_device *ibdev, struct ib_queue_pair *qp,
  401. union ib_gid *dgid, union ib_guid *service_id,
  402. void *private_data, size_t private_data_len,
  403. struct ib_connection_operations *op ) {
  404. struct ib_connection *conn;
  405. /* Allocate and initialise request */
  406. conn = zalloc ( sizeof ( *conn ) + private_data_len );
  407. if ( ! conn )
  408. goto err_alloc_conn;
  409. conn->ibdev = ibdev;
  410. conn->qp = qp;
  411. memset ( &qp->av, 0, sizeof ( qp->av ) );
  412. qp->av.gid_present = 1;
  413. memcpy ( &qp->av.gid, dgid, sizeof ( qp->av.gid ) );
  414. conn->local_id = random();
  415. memcpy ( &conn->service_id, service_id, sizeof ( conn->service_id ) );
  416. conn->op = op;
  417. conn->private_data_len = private_data_len;
  418. memcpy ( &conn->private_data, private_data, private_data_len );
  419. /* Create path */
  420. conn->path = ib_create_path ( ibdev, &qp->av, &ib_cm_path_op );
  421. if ( ! conn->path )
  422. goto err_create_path;
  423. ib_path_set_ownerdata ( conn->path, conn );
  424. /* Add to list of connections */
  425. list_add ( &conn->list, &ib_cm_conns );
  426. DBGC ( conn, "CM %p created for IBDEV %p QPN %lx\n",
  427. conn, ibdev, qp->qpn );
  428. DBGC ( conn, "CM %p connecting to " IB_GID_FMT " " IB_GUID_FMT "\n",
  429. conn, IB_GID_ARGS ( dgid ), IB_GUID_ARGS ( service_id ) );
  430. return conn;
  431. ib_destroy_path ( ibdev, conn->path );
  432. err_create_path:
  433. free ( conn );
  434. err_alloc_conn:
  435. return NULL;
  436. }
  437. /**
  438. * Destroy connection to remote QP
  439. *
  440. * @v ibdev Infiniband device
  441. * @v qp Queue pair
  442. * @v conn Connection
  443. */
  444. void ib_destroy_conn ( struct ib_device *ibdev,
  445. struct ib_queue_pair *qp __unused,
  446. struct ib_connection *conn ) {
  447. list_del ( &conn->list );
  448. if ( conn->madx )
  449. ib_destroy_madx ( ibdev, ibdev->gsi, conn->madx );
  450. if ( conn->path )
  451. ib_destroy_path ( ibdev, conn->path );
  452. free ( conn );
  453. }