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.

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