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

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