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_mi.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. FILE_LICENCE ( GPL2_OR_LATER );
  20. #include <stdint.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <errno.h>
  24. #include <stdio.h>
  25. #include <unistd.h>
  26. #include <byteswap.h>
  27. #include <ipxe/infiniband.h>
  28. #include <ipxe/iobuf.h>
  29. #include <ipxe/ib_mi.h>
  30. /**
  31. * @file
  32. *
  33. * Infiniband management interfaces
  34. *
  35. */
  36. /** Management interface number of send WQEs
  37. *
  38. * This is a policy decision.
  39. */
  40. #define IB_MI_NUM_SEND_WQES 4
  41. /** Management interface number of receive WQEs
  42. *
  43. * This is a policy decision.
  44. */
  45. #define IB_MI_NUM_RECV_WQES 2
  46. /** Management interface number of completion queue entries
  47. *
  48. * This is a policy decision
  49. */
  50. #define IB_MI_NUM_CQES 8
  51. /** TID magic signature */
  52. #define IB_MI_TID_MAGIC ( ( 'i' << 24 ) | ( 'P' << 16 ) | ( 'X' << 8 ) | 'E' )
  53. /** TID to use for next MAD */
  54. static unsigned int next_tid;
  55. /**
  56. * Handle received MAD
  57. *
  58. * @v ibdev Infiniband device
  59. * @v mi Management interface
  60. * @v mad Received MAD
  61. * @v av Source address vector
  62. * @ret rc Return status code
  63. */
  64. static int ib_mi_handle ( struct ib_device *ibdev,
  65. struct ib_mad_interface *mi,
  66. union ib_mad *mad,
  67. struct ib_address_vector *av ) {
  68. struct ib_mad_hdr *hdr = &mad->hdr;
  69. struct ib_mad_transaction *madx;
  70. struct ib_mad_agent *agent;
  71. /* Look for a matching transaction by TID */
  72. list_for_each_entry ( madx, &mi->madx, list ) {
  73. if ( memcmp ( &hdr->tid, &madx->mad.hdr.tid,
  74. sizeof ( hdr->tid ) ) != 0 )
  75. continue;
  76. /* Found a matching transaction */
  77. madx->op->complete ( ibdev, mi, madx, 0, mad, av );
  78. return 0;
  79. }
  80. /* If there is no matching transaction, look for a listening agent */
  81. for_each_table_entry ( agent, IB_MAD_AGENTS ) {
  82. if ( ( ( agent->mgmt_class & IB_MGMT_CLASS_MASK ) !=
  83. ( hdr->mgmt_class & IB_MGMT_CLASS_MASK ) ) ||
  84. ( agent->class_version != hdr->class_version ) ||
  85. ( agent->attr_id != hdr->attr_id ) )
  86. continue;
  87. /* Found a matching agent */
  88. agent->handle ( ibdev, mi, mad, av );
  89. return 0;
  90. }
  91. /* Otherwise, ignore it */
  92. DBGC ( mi, "MI %p RX TID %08x%08x ignored\n",
  93. mi, ntohl ( hdr->tid[0] ), ntohl ( hdr->tid[1] ) );
  94. return -ENOTSUP;
  95. }
  96. /**
  97. * Complete receive via management interface
  98. *
  99. *
  100. * @v ibdev Infiniband device
  101. * @v qp Queue pair
  102. * @v dest Destination address vector
  103. * @v source Source address vector
  104. * @v iobuf I/O buffer
  105. * @v rc Completion status code
  106. */
  107. static void ib_mi_complete_recv ( struct ib_device *ibdev,
  108. struct ib_queue_pair *qp,
  109. struct ib_address_vector *dest __unused,
  110. struct ib_address_vector *source,
  111. struct io_buffer *iobuf, int rc ) {
  112. struct ib_mad_interface *mi = ib_qp_get_ownerdata ( qp );
  113. union ib_mad *mad;
  114. struct ib_mad_hdr *hdr;
  115. /* Ignore errors */
  116. if ( rc != 0 ) {
  117. DBGC ( mi, "MI %p RX error: %s\n", mi, strerror ( rc ) );
  118. goto out;
  119. }
  120. /* Sanity checks */
  121. if ( iob_len ( iobuf ) != sizeof ( *mad ) ) {
  122. DBGC ( mi, "MI %p RX bad size (%zd bytes)\n",
  123. mi, iob_len ( iobuf ) );
  124. DBGC_HDA ( mi, 0, iobuf->data, iob_len ( iobuf ) );
  125. goto out;
  126. }
  127. mad = iobuf->data;
  128. hdr = &mad->hdr;
  129. if ( hdr->base_version != IB_MGMT_BASE_VERSION ) {
  130. DBGC ( mi, "MI %p RX unsupported base version %x\n",
  131. mi, hdr->base_version );
  132. DBGC_HDA ( mi, 0, mad, sizeof ( *mad ) );
  133. goto out;
  134. }
  135. DBGC ( mi, "MI %p RX TID %08x%08x (%02x,%02x,%02x,%04x) status "
  136. "%04x\n", mi, ntohl ( hdr->tid[0] ), ntohl ( hdr->tid[1] ),
  137. hdr->mgmt_class, hdr->class_version, hdr->method,
  138. ntohs ( hdr->attr_id ), ntohs ( hdr->status ) );
  139. DBGC2_HDA ( mi, 0, mad, sizeof ( *mad ) );
  140. /* Handle MAD */
  141. if ( ( rc = ib_mi_handle ( ibdev, mi, mad, source ) ) != 0 )
  142. goto out;
  143. out:
  144. free_iob ( iobuf );
  145. }
  146. /** Management interface completion operations */
  147. static struct ib_completion_queue_operations ib_mi_completion_ops = {
  148. .complete_recv = ib_mi_complete_recv,
  149. };
  150. /** Management interface queue pair operations */
  151. static struct ib_queue_pair_operations ib_mi_queue_pair_ops = {
  152. .alloc_iob = alloc_iob,
  153. };
  154. /**
  155. * Transmit MAD
  156. *
  157. * @v ibdev Infiniband device
  158. * @v mi Management interface
  159. * @v mad MAD
  160. * @v av Destination address vector
  161. * @ret rc Return status code
  162. */
  163. int ib_mi_send ( struct ib_device *ibdev, struct ib_mad_interface *mi,
  164. union ib_mad *mad, struct ib_address_vector *av ) {
  165. struct ib_mad_hdr *hdr = &mad->hdr;
  166. struct io_buffer *iobuf;
  167. int rc;
  168. /* Set common fields */
  169. hdr->base_version = IB_MGMT_BASE_VERSION;
  170. if ( ( hdr->tid[0] == 0 ) && ( hdr->tid[1] == 0 ) ) {
  171. hdr->tid[0] = htonl ( IB_MI_TID_MAGIC );
  172. hdr->tid[1] = htonl ( ++next_tid );
  173. }
  174. DBGC ( mi, "MI %p TX TID %08x%08x (%02x,%02x,%02x,%04x) status "
  175. "%04x\n", mi, ntohl ( hdr->tid[0] ), ntohl ( hdr->tid[1] ),
  176. hdr->mgmt_class, hdr->class_version, hdr->method,
  177. ntohs ( hdr->attr_id ), ntohs ( hdr->status ) );
  178. DBGC2_HDA ( mi, 0, mad, sizeof ( *mad ) );
  179. /* Construct directed route portion of response, if necessary */
  180. if ( hdr->mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE ) {
  181. struct ib_mad_smp *smp = &mad->smp;
  182. unsigned int hop_pointer;
  183. unsigned int hop_count;
  184. smp->mad_hdr.status |= htons ( IB_SMP_STATUS_D_INBOUND );
  185. hop_pointer = smp->mad_hdr.class_specific.smp.hop_pointer;
  186. hop_count = smp->mad_hdr.class_specific.smp.hop_count;
  187. assert ( hop_count == hop_pointer );
  188. if ( hop_pointer < ( sizeof ( smp->return_path.hops ) /
  189. sizeof ( smp->return_path.hops[0] ) ) ) {
  190. smp->return_path.hops[hop_pointer] = ibdev->port;
  191. } else {
  192. DBGC ( mi, "MI %p TX TID %08x%08x invalid hop pointer "
  193. "%d\n", mi, ntohl ( hdr->tid[0] ),
  194. ntohl ( hdr->tid[1] ), hop_pointer );
  195. return -EINVAL;
  196. }
  197. }
  198. /* Construct I/O buffer */
  199. iobuf = alloc_iob ( sizeof ( *mad ) );
  200. if ( ! iobuf ) {
  201. DBGC ( mi, "MI %p could not allocate buffer for TID "
  202. "%08x%08x\n",
  203. mi, ntohl ( hdr->tid[0] ), ntohl ( hdr->tid[1] ) );
  204. return -ENOMEM;
  205. }
  206. memcpy ( iob_put ( iobuf, sizeof ( *mad ) ), mad, sizeof ( *mad ) );
  207. /* Send I/O buffer */
  208. if ( ( rc = ib_post_send ( ibdev, mi->qp, av, iobuf ) ) != 0 ) {
  209. DBGC ( mi, "MI %p TX TID %08x%08x failed: %s\n",
  210. mi, ntohl ( hdr->tid[0] ), ntohl ( hdr->tid[1] ),
  211. strerror ( rc ) );
  212. free_iob ( iobuf );
  213. return rc;
  214. }
  215. return 0;
  216. }
  217. /**
  218. * Handle management transaction timer expiry
  219. *
  220. * @v timer Retry timer
  221. * @v expired Failure indicator
  222. */
  223. static void ib_mi_timer_expired ( struct retry_timer *timer, int expired ) {
  224. struct ib_mad_transaction *madx =
  225. container_of ( timer, struct ib_mad_transaction, timer );
  226. struct ib_mad_interface *mi = madx->mi;
  227. struct ib_device *ibdev = mi->ibdev;
  228. struct ib_mad_hdr *hdr = &madx->mad.hdr;
  229. /* Abandon transaction if we have tried too many times */
  230. if ( expired ) {
  231. DBGC ( mi, "MI %p abandoning TID %08x%08x\n",
  232. mi, ntohl ( hdr->tid[0] ), ntohl ( hdr->tid[1] ) );
  233. madx->op->complete ( ibdev, mi, madx, -ETIMEDOUT, NULL, NULL );
  234. return;
  235. }
  236. /* Restart retransmission timer */
  237. start_timer ( timer );
  238. /* Resend MAD */
  239. ib_mi_send ( ibdev, mi, &madx->mad, &madx->av );
  240. }
  241. /**
  242. * Create management transaction
  243. *
  244. * @v ibdev Infiniband device
  245. * @v mi Management interface
  246. * @v mad MAD to send
  247. * @v av Destination address, or NULL to use SM's GSI
  248. * @v op Management transaction operations
  249. * @ret madx Management transaction, or NULL
  250. */
  251. struct ib_mad_transaction *
  252. ib_create_madx ( struct ib_device *ibdev, struct ib_mad_interface *mi,
  253. union ib_mad *mad, struct ib_address_vector *av,
  254. struct ib_mad_transaction_operations *op ) {
  255. struct ib_mad_transaction *madx;
  256. /* Allocate and initialise structure */
  257. madx = zalloc ( sizeof ( *madx ) );
  258. if ( ! madx )
  259. return NULL;
  260. timer_init ( &madx->timer, ib_mi_timer_expired, NULL );
  261. madx->mi = mi;
  262. madx->op = op;
  263. /* Determine address vector */
  264. if ( av ) {
  265. memcpy ( &madx->av, av, sizeof ( madx->av ) );
  266. } else {
  267. madx->av.lid = ibdev->sm_lid;
  268. madx->av.sl = ibdev->sm_sl;
  269. madx->av.qpn = IB_QPN_GSI;
  270. madx->av.qkey = IB_QKEY_GSI;
  271. }
  272. /* Copy MAD */
  273. memcpy ( &madx->mad, mad, sizeof ( madx->mad ) );
  274. /* Add to list and start timer to send initial MAD */
  275. list_add ( &madx->list, &mi->madx );
  276. start_timer_nodelay ( &madx->timer );
  277. return madx;
  278. }
  279. /**
  280. * Destroy management transaction
  281. *
  282. * @v ibdev Infiniband device
  283. * @v mi Management interface
  284. * @v madx Management transaction
  285. */
  286. void ib_destroy_madx ( struct ib_device *ibdev __unused,
  287. struct ib_mad_interface *mi __unused,
  288. struct ib_mad_transaction *madx ) {
  289. /* Stop timer and remove from list */
  290. stop_timer ( &madx->timer );
  291. list_del ( &madx->list );
  292. /* Free transaction */
  293. free ( madx );
  294. }
  295. /**
  296. * Create management interface
  297. *
  298. * @v ibdev Infiniband device
  299. * @v type Queue pair type
  300. * @ret mi Management agent, or NULL
  301. */
  302. struct ib_mad_interface * ib_create_mi ( struct ib_device *ibdev,
  303. enum ib_queue_pair_type type ) {
  304. struct ib_mad_interface *mi;
  305. int rc;
  306. /* Allocate and initialise fields */
  307. mi = zalloc ( sizeof ( *mi ) );
  308. if ( ! mi )
  309. goto err_alloc;
  310. mi->ibdev = ibdev;
  311. INIT_LIST_HEAD ( &mi->madx );
  312. /* Create completion queue */
  313. mi->cq = ib_create_cq ( ibdev, IB_MI_NUM_CQES, &ib_mi_completion_ops );
  314. if ( ! mi->cq ) {
  315. DBGC ( mi, "MI %p could not allocate completion queue\n", mi );
  316. goto err_create_cq;
  317. }
  318. /* Create queue pair */
  319. mi->qp = ib_create_qp ( ibdev, type, IB_MI_NUM_SEND_WQES, mi->cq,
  320. IB_MI_NUM_RECV_WQES, mi->cq,
  321. &ib_mi_queue_pair_ops );
  322. if ( ! mi->qp ) {
  323. DBGC ( mi, "MI %p could not allocate queue pair\n", mi );
  324. goto err_create_qp;
  325. }
  326. ib_qp_set_ownerdata ( mi->qp, mi );
  327. DBGC ( mi, "MI %p (%s) running on QPN %#lx\n",
  328. mi, ( ( type == IB_QPT_SMI ) ? "SMI" : "GSI" ), mi->qp->qpn );
  329. /* Set queue key */
  330. mi->qp->qkey = ( ( type == IB_QPT_SMI ) ? IB_QKEY_SMI : IB_QKEY_GSI );
  331. if ( ( rc = ib_modify_qp ( ibdev, mi->qp ) ) != 0 ) {
  332. DBGC ( mi, "MI %p could not set queue key: %s\n",
  333. mi, strerror ( rc ) );
  334. goto err_modify_qp;
  335. }
  336. /* Fill receive ring */
  337. ib_refill_recv ( ibdev, mi->qp );
  338. return mi;
  339. err_modify_qp:
  340. ib_destroy_qp ( ibdev, mi->qp );
  341. err_create_qp:
  342. ib_destroy_cq ( ibdev, mi->cq );
  343. err_create_cq:
  344. free ( mi );
  345. err_alloc:
  346. return NULL;
  347. }
  348. /**
  349. * Destroy management interface
  350. *
  351. * @v mi Management interface
  352. */
  353. void ib_destroy_mi ( struct ib_device *ibdev, struct ib_mad_interface *mi ) {
  354. struct ib_mad_transaction *madx;
  355. struct ib_mad_transaction *tmp;
  356. /* Flush any outstanding requests */
  357. list_for_each_entry_safe ( madx, tmp, &mi->madx, list ) {
  358. DBGC ( mi, "MI %p destroyed while TID %08x%08x in progress\n",
  359. mi, ntohl ( madx->mad.hdr.tid[0] ),
  360. ntohl ( madx->mad.hdr.tid[1] ) );
  361. madx->op->complete ( ibdev, mi, madx, -ECANCELED, NULL, NULL );
  362. }
  363. ib_destroy_qp ( ibdev, mi->qp );
  364. ib_destroy_cq ( ibdev, mi->cq );
  365. free ( mi );
  366. }