Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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 av Address vector
  103. * @v iobuf I/O buffer
  104. * @v rc Completion status code
  105. */
  106. static void ib_mi_complete_recv ( struct ib_device *ibdev,
  107. struct ib_queue_pair *qp,
  108. struct ib_address_vector *av,
  109. struct io_buffer *iobuf, int rc ) {
  110. struct ib_mad_interface *mi = ib_qp_get_ownerdata ( qp );
  111. union ib_mad *mad;
  112. struct ib_mad_hdr *hdr;
  113. /* Ignore errors */
  114. if ( rc != 0 ) {
  115. DBGC ( mi, "MI %p RX error: %s\n", mi, strerror ( rc ) );
  116. goto out;
  117. }
  118. /* Sanity checks */
  119. if ( iob_len ( iobuf ) != sizeof ( *mad ) ) {
  120. DBGC ( mi, "MI %p RX bad size (%zd bytes)\n",
  121. mi, iob_len ( iobuf ) );
  122. DBGC_HDA ( mi, 0, iobuf->data, iob_len ( iobuf ) );
  123. goto out;
  124. }
  125. mad = iobuf->data;
  126. hdr = &mad->hdr;
  127. if ( hdr->base_version != IB_MGMT_BASE_VERSION ) {
  128. DBGC ( mi, "MI %p RX unsupported base version %x\n",
  129. mi, hdr->base_version );
  130. DBGC_HDA ( mi, 0, mad, sizeof ( *mad ) );
  131. goto out;
  132. }
  133. DBGC ( mi, "MI %p RX TID %08x%08x (%02x,%02x,%02x,%04x) status "
  134. "%04x\n", mi, ntohl ( hdr->tid[0] ), ntohl ( hdr->tid[1] ),
  135. hdr->mgmt_class, hdr->class_version, hdr->method,
  136. ntohs ( hdr->attr_id ), ntohs ( hdr->status ) );
  137. DBGC2_HDA ( mi, 0, mad, sizeof ( *mad ) );
  138. /* Handle MAD */
  139. if ( ( rc = ib_mi_handle ( ibdev, mi, mad, av ) ) != 0 )
  140. goto out;
  141. out:
  142. free_iob ( iobuf );
  143. }
  144. /** Management interface completion operations */
  145. static struct ib_completion_queue_operations ib_mi_completion_ops = {
  146. .complete_recv = ib_mi_complete_recv,
  147. };
  148. /** Management interface queue pair operations */
  149. static struct ib_queue_pair_operations ib_mi_queue_pair_ops = {
  150. .alloc_iob = alloc_iob,
  151. };
  152. /**
  153. * Transmit MAD
  154. *
  155. * @v ibdev Infiniband device
  156. * @v mi Management interface
  157. * @v mad MAD
  158. * @v av Destination address vector
  159. * @ret rc Return status code
  160. */
  161. int ib_mi_send ( struct ib_device *ibdev, struct ib_mad_interface *mi,
  162. union ib_mad *mad, struct ib_address_vector *av ) {
  163. struct ib_mad_hdr *hdr = &mad->hdr;
  164. struct io_buffer *iobuf;
  165. int rc;
  166. /* Set common fields */
  167. hdr->base_version = IB_MGMT_BASE_VERSION;
  168. if ( ( hdr->tid[0] == 0 ) && ( hdr->tid[1] == 0 ) ) {
  169. hdr->tid[0] = htonl ( IB_MI_TID_MAGIC );
  170. hdr->tid[1] = htonl ( ++next_tid );
  171. }
  172. DBGC ( mi, "MI %p TX TID %08x%08x (%02x,%02x,%02x,%04x) status "
  173. "%04x\n", mi, ntohl ( hdr->tid[0] ), ntohl ( hdr->tid[1] ),
  174. hdr->mgmt_class, hdr->class_version, hdr->method,
  175. ntohs ( hdr->attr_id ), ntohs ( hdr->status ) );
  176. DBGC2_HDA ( mi, 0, mad, sizeof ( *mad ) );
  177. /* Construct directed route portion of response, if necessary */
  178. if ( hdr->mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE ) {
  179. struct ib_mad_smp *smp = &mad->smp;
  180. unsigned int hop_pointer;
  181. unsigned int hop_count;
  182. smp->mad_hdr.status |= htons ( IB_SMP_STATUS_D_INBOUND );
  183. hop_pointer = smp->mad_hdr.class_specific.smp.hop_pointer;
  184. hop_count = smp->mad_hdr.class_specific.smp.hop_count;
  185. assert ( hop_count == hop_pointer );
  186. if ( hop_pointer < ( sizeof ( smp->return_path.hops ) /
  187. sizeof ( smp->return_path.hops[0] ) ) ) {
  188. smp->return_path.hops[hop_pointer] = ibdev->port;
  189. } else {
  190. DBGC ( mi, "MI %p TX TID %08x%08x invalid hop pointer "
  191. "%d\n", mi, ntohl ( hdr->tid[0] ),
  192. ntohl ( hdr->tid[1] ), hop_pointer );
  193. return -EINVAL;
  194. }
  195. }
  196. /* Construct I/O buffer */
  197. iobuf = alloc_iob ( sizeof ( *mad ) );
  198. if ( ! iobuf ) {
  199. DBGC ( mi, "MI %p could not allocate buffer for TID "
  200. "%08x%08x\n",
  201. mi, ntohl ( hdr->tid[0] ), ntohl ( hdr->tid[1] ) );
  202. return -ENOMEM;
  203. }
  204. memcpy ( iob_put ( iobuf, sizeof ( *mad ) ), mad, sizeof ( *mad ) );
  205. /* Send I/O buffer */
  206. if ( ( rc = ib_post_send ( ibdev, mi->qp, av, iobuf ) ) != 0 ) {
  207. DBGC ( mi, "MI %p TX TID %08x%08x failed: %s\n",
  208. mi, ntohl ( hdr->tid[0] ), ntohl ( hdr->tid[1] ),
  209. strerror ( rc ) );
  210. free_iob ( iobuf );
  211. return rc;
  212. }
  213. return 0;
  214. }
  215. /**
  216. * Handle management transaction timer expiry
  217. *
  218. * @v timer Retry timer
  219. * @v expired Failure indicator
  220. */
  221. static void ib_mi_timer_expired ( struct retry_timer *timer, int expired ) {
  222. struct ib_mad_transaction *madx =
  223. container_of ( timer, struct ib_mad_transaction, timer );
  224. struct ib_mad_interface *mi = madx->mi;
  225. struct ib_device *ibdev = mi->ibdev;
  226. struct ib_mad_hdr *hdr = &madx->mad.hdr;
  227. /* Abandon transaction if we have tried too many times */
  228. if ( expired ) {
  229. DBGC ( mi, "MI %p abandoning TID %08x%08x\n",
  230. mi, ntohl ( hdr->tid[0] ), ntohl ( hdr->tid[1] ) );
  231. madx->op->complete ( ibdev, mi, madx, -ETIMEDOUT, NULL, NULL );
  232. return;
  233. }
  234. /* Restart retransmission timer */
  235. start_timer ( timer );
  236. /* Resend MAD */
  237. ib_mi_send ( ibdev, mi, &madx->mad, &madx->av );
  238. }
  239. /**
  240. * Create management transaction
  241. *
  242. * @v ibdev Infiniband device
  243. * @v mi Management interface
  244. * @v mad MAD to send
  245. * @v av Destination address, or NULL to use SM's GSI
  246. * @v op Management transaction operations
  247. * @ret madx Management transaction, or NULL
  248. */
  249. struct ib_mad_transaction *
  250. ib_create_madx ( struct ib_device *ibdev, struct ib_mad_interface *mi,
  251. union ib_mad *mad, struct ib_address_vector *av,
  252. struct ib_mad_transaction_operations *op ) {
  253. struct ib_mad_transaction *madx;
  254. /* Allocate and initialise structure */
  255. madx = zalloc ( sizeof ( *madx ) );
  256. if ( ! madx )
  257. return NULL;
  258. timer_init ( &madx->timer, ib_mi_timer_expired, NULL );
  259. madx->mi = mi;
  260. madx->op = op;
  261. /* Determine address vector */
  262. if ( av ) {
  263. memcpy ( &madx->av, av, sizeof ( madx->av ) );
  264. } else {
  265. madx->av.lid = ibdev->sm_lid;
  266. madx->av.sl = ibdev->sm_sl;
  267. madx->av.qpn = IB_QPN_GSI;
  268. madx->av.qkey = IB_QKEY_GSI;
  269. }
  270. /* Copy MAD */
  271. memcpy ( &madx->mad, mad, sizeof ( madx->mad ) );
  272. /* Add to list and start timer to send initial MAD */
  273. list_add ( &madx->list, &mi->madx );
  274. start_timer_nodelay ( &madx->timer );
  275. return madx;
  276. }
  277. /**
  278. * Destroy management transaction
  279. *
  280. * @v ibdev Infiniband device
  281. * @v mi Management interface
  282. * @v madx Management transaction
  283. */
  284. void ib_destroy_madx ( struct ib_device *ibdev __unused,
  285. struct ib_mad_interface *mi __unused,
  286. struct ib_mad_transaction *madx ) {
  287. /* Stop timer and remove from list */
  288. stop_timer ( &madx->timer );
  289. list_del ( &madx->list );
  290. /* Free transaction */
  291. free ( madx );
  292. }
  293. /**
  294. * Create management interface
  295. *
  296. * @v ibdev Infiniband device
  297. * @v type Queue pair type
  298. * @ret mi Management agent, or NULL
  299. */
  300. struct ib_mad_interface * ib_create_mi ( struct ib_device *ibdev,
  301. enum ib_queue_pair_type type ) {
  302. struct ib_mad_interface *mi;
  303. int rc;
  304. /* Allocate and initialise fields */
  305. mi = zalloc ( sizeof ( *mi ) );
  306. if ( ! mi )
  307. goto err_alloc;
  308. mi->ibdev = ibdev;
  309. INIT_LIST_HEAD ( &mi->madx );
  310. /* Create completion queue */
  311. mi->cq = ib_create_cq ( ibdev, IB_MI_NUM_CQES, &ib_mi_completion_ops );
  312. if ( ! mi->cq ) {
  313. DBGC ( mi, "MI %p could not allocate completion queue\n", mi );
  314. goto err_create_cq;
  315. }
  316. /* Create queue pair */
  317. mi->qp = ib_create_qp ( ibdev, type, IB_MI_NUM_SEND_WQES, mi->cq,
  318. IB_MI_NUM_RECV_WQES, mi->cq,
  319. &ib_mi_queue_pair_ops );
  320. if ( ! mi->qp ) {
  321. DBGC ( mi, "MI %p could not allocate queue pair\n", mi );
  322. goto err_create_qp;
  323. }
  324. ib_qp_set_ownerdata ( mi->qp, mi );
  325. DBGC ( mi, "MI %p (%s) running on QPN %#lx\n",
  326. mi, ( ( type == IB_QPT_SMI ) ? "SMI" : "GSI" ), mi->qp->qpn );
  327. /* Set queue key */
  328. mi->qp->qkey = ( ( type == IB_QPT_SMI ) ? IB_QKEY_SMI : IB_QKEY_GSI );
  329. if ( ( rc = ib_modify_qp ( ibdev, mi->qp ) ) != 0 ) {
  330. DBGC ( mi, "MI %p could not set queue key: %s\n",
  331. mi, strerror ( rc ) );
  332. goto err_modify_qp;
  333. }
  334. /* Fill receive ring */
  335. ib_refill_recv ( ibdev, mi->qp );
  336. return mi;
  337. err_modify_qp:
  338. ib_destroy_qp ( ibdev, mi->qp );
  339. err_create_qp:
  340. ib_destroy_cq ( ibdev, mi->cq );
  341. err_create_cq:
  342. free ( mi );
  343. err_alloc:
  344. return NULL;
  345. }
  346. /**
  347. * Destroy management interface
  348. *
  349. * @v mi Management interface
  350. */
  351. void ib_destroy_mi ( struct ib_device *ibdev, struct ib_mad_interface *mi ) {
  352. struct ib_mad_transaction *madx;
  353. struct ib_mad_transaction *tmp;
  354. /* Flush any outstanding requests */
  355. list_for_each_entry_safe ( madx, tmp, &mi->madx, list ) {
  356. DBGC ( mi, "MI %p destroyed while TID %08x%08x in progress\n",
  357. mi, ntohl ( madx->mad.hdr.tid[0] ),
  358. ntohl ( madx->mad.hdr.tid[1] ) );
  359. madx->op->complete ( ibdev, mi, madx, -ECANCELED, NULL, NULL );
  360. }
  361. ib_destroy_qp ( ibdev, mi->qp );
  362. ib_destroy_cq ( ibdev, mi->cq );
  363. free ( mi );
  364. }