您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ib_gma.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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_gma.h>
  29. /**
  30. * @file
  31. *
  32. * Infiniband General Management Agent
  33. *
  34. */
  35. /** A MAD request */
  36. struct ib_mad_request {
  37. /** Associated GMA */
  38. struct ib_gma *gma;
  39. /** List of outstanding MAD requests */
  40. struct list_head list;
  41. /** Retry timer */
  42. struct retry_timer timer;
  43. /** Destination address */
  44. struct ib_address_vector av;
  45. /** MAD request */
  46. union ib_mad mad;
  47. };
  48. /** GMA number of send WQEs
  49. *
  50. * This is a policy decision.
  51. */
  52. #define IB_GMA_NUM_SEND_WQES 4
  53. /** GMA number of receive WQEs
  54. *
  55. * This is a policy decision.
  56. */
  57. #define IB_GMA_NUM_RECV_WQES 2
  58. /** GMA number of completion queue entries
  59. *
  60. * This is a policy decision
  61. */
  62. #define IB_GMA_NUM_CQES 8
  63. /** GMA TID magic signature */
  64. #define IB_GMA_TID_MAGIC ( ( 'g' << 24 ) | ( 'P' << 16 ) | ( 'X' << 8 ) | 'E' )
  65. /** TID to use for next MAD request */
  66. static unsigned int next_request_tid;
  67. /**
  68. * Call attribute handler
  69. *
  70. * @v gma General management agent
  71. * @v mad MAD
  72. * @ret rc Return status code
  73. */
  74. static int ib_handle_mad ( struct ib_gma *gma, union ib_mad *mad ) {
  75. struct ib_mad_hdr *hdr = &mad->hdr;
  76. struct ib_gma_handler *handler;
  77. for_each_table_entry ( handler, IB_GMA_HANDLERS ) {
  78. if ( ( ( handler->mgmt_class & ~handler->mgmt_class_ignore ) ==
  79. ( hdr->mgmt_class & ~handler->mgmt_class_ignore ) ) &&
  80. ( handler->class_version == hdr->class_version ) &&
  81. ( handler->method == hdr->method ) &&
  82. ( handler->attr_id == hdr->attr_id ) ) {
  83. hdr->method = handler->resp_method;
  84. return handler->handle ( gma, mad );
  85. }
  86. }
  87. hdr->method = IB_MGMT_METHOD_TRAP;
  88. hdr->status = htons ( IB_MGMT_STATUS_UNSUPPORTED_METHOD_ATTR );
  89. return -ENOTSUP;
  90. }
  91. /**
  92. * Complete GMA receive
  93. *
  94. *
  95. * @v ibdev Infiniband device
  96. * @v qp Queue pair
  97. * @v av Address vector
  98. * @v iobuf I/O buffer
  99. * @v rc Completion status code
  100. */
  101. static void ib_gma_complete_recv ( struct ib_device *ibdev,
  102. struct ib_queue_pair *qp,
  103. struct ib_address_vector *av,
  104. struct io_buffer *iobuf, int rc ) {
  105. struct ib_gma *gma = ib_qp_get_ownerdata ( qp );
  106. struct ib_mad_request *request;
  107. union ib_mad *mad;
  108. struct ib_mad_hdr *hdr;
  109. unsigned int hop_pointer;
  110. unsigned int hop_count;
  111. /* Ignore errors */
  112. if ( rc != 0 ) {
  113. DBGC ( gma, "GMA %p RX error: %s\n", gma, strerror ( rc ) );
  114. goto out;
  115. }
  116. /* Sanity checks */
  117. if ( iob_len ( iobuf ) != sizeof ( *mad ) ) {
  118. DBGC ( gma, "GMA %p RX bad size (%zd bytes)\n",
  119. gma, iob_len ( iobuf ) );
  120. DBGC_HDA ( gma, 0, iobuf->data, iob_len ( iobuf ) );
  121. goto out;
  122. }
  123. mad = iobuf->data;
  124. hdr = &mad->hdr;
  125. if ( hdr->base_version != IB_MGMT_BASE_VERSION ) {
  126. DBGC ( gma, "GMA %p unsupported base version %x\n",
  127. gma, hdr->base_version );
  128. DBGC_HDA ( gma, 0, mad, sizeof ( *mad ) );
  129. goto out;
  130. }
  131. DBGC ( gma, "GMA %p RX TID %08x%08x (%02x,%02x,%02x,%04x) status "
  132. "%04x\n", gma, ntohl ( hdr->tid[0] ), ntohl ( hdr->tid[1] ),
  133. hdr->mgmt_class, hdr->class_version, hdr->method,
  134. ntohs ( hdr->attr_id ), ntohs ( hdr->status ) );
  135. DBGC2_HDA ( gma, 0, mad, sizeof ( *mad ) );
  136. /* Dequeue request if applicable */
  137. list_for_each_entry ( request, &gma->requests, list ) {
  138. if ( memcmp ( &request->mad.hdr.tid, &hdr->tid,
  139. sizeof ( request->mad.hdr.tid ) ) == 0 ) {
  140. stop_timer ( &request->timer );
  141. list_del ( &request->list );
  142. free ( request );
  143. break;
  144. }
  145. }
  146. /* Handle MAD, if possible */
  147. if ( ( rc = ib_handle_mad ( gma, mad ) ) != 0 ) {
  148. DBGC ( gma, "GMA %p could not handle TID %08x%08x: %s\n",
  149. gma, ntohl ( hdr->tid[0] ), ntohl ( hdr->tid[1] ),
  150. strerror ( rc ) );
  151. /* Do not abort; we may want to send an error response */
  152. }
  153. /* Finish processing if we have no response to send */
  154. if ( ! hdr->method )
  155. goto out;
  156. DBGC ( gma, "GMA %p TX TID %08x%08x (%02x,%02x,%02x,%04x)\n", gma,
  157. ntohl ( hdr->tid[0] ), ntohl ( hdr->tid[1] ), hdr->mgmt_class,
  158. hdr->class_version, hdr->method, ntohs ( hdr->attr_id ) );
  159. DBGC2_HDA ( gma, 0, mad, sizeof ( *mad ) );
  160. /* Set response fields for directed route SMPs */
  161. if ( hdr->mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE ) {
  162. struct ib_mad_smp *smp = &mad->smp;
  163. hdr->status |= htons ( IB_SMP_STATUS_D_INBOUND );
  164. hop_pointer = smp->mad_hdr.class_specific.smp.hop_pointer;
  165. hop_count = smp->mad_hdr.class_specific.smp.hop_count;
  166. assert ( hop_count == hop_pointer );
  167. if ( hop_pointer < ( sizeof ( smp->return_path.hops ) /
  168. sizeof ( smp->return_path.hops[0] ) ) ) {
  169. smp->return_path.hops[hop_pointer] = ibdev->port;
  170. } else {
  171. DBGC ( gma, "GMA %p invalid hop pointer %d\n",
  172. gma, hop_pointer );
  173. goto out;
  174. }
  175. }
  176. /* Send MAD response, if applicable */
  177. if ( ( rc = ib_post_send ( ibdev, qp, av,
  178. iob_disown ( iobuf ) ) ) != 0 ) {
  179. DBGC ( gma, "GMA %p could not send MAD response: %s\n",
  180. gma, strerror ( rc ) );
  181. goto out;
  182. }
  183. out:
  184. free_iob ( iobuf );
  185. }
  186. /**
  187. * Complete GMA send
  188. *
  189. *
  190. * @v ibdev Infiniband device
  191. * @v qp Queue pair
  192. * @v iobuf I/O buffer
  193. * @v rc Completion status code
  194. */
  195. static void ib_gma_complete_send ( struct ib_device *ibdev __unused,
  196. struct ib_queue_pair *qp,
  197. struct io_buffer *iobuf, int rc ) {
  198. struct ib_gma *gma = ib_qp_get_ownerdata ( qp );
  199. if ( rc != 0 ) {
  200. DBGC ( gma, "GMA %p send completion error: %s\n",
  201. gma, strerror ( rc ) );
  202. }
  203. free_iob ( iobuf );
  204. }
  205. /** GMA completion operations */
  206. static struct ib_completion_queue_operations ib_gma_completion_ops = {
  207. .complete_send = ib_gma_complete_send,
  208. .complete_recv = ib_gma_complete_recv,
  209. };
  210. /**
  211. * Transmit MAD request
  212. *
  213. * @v gma General management agent
  214. * @v request MAD request
  215. * @ret rc Return status code
  216. */
  217. static int ib_gma_send ( struct ib_gma *gma, struct ib_mad_request *request ) {
  218. struct io_buffer *iobuf;
  219. int rc;
  220. DBGC ( gma, "GMA %p TX TID %08x%08x (%02x,%02x,%02x,%04x)\n",
  221. gma, ntohl ( request->mad.hdr.tid[0] ),
  222. ntohl ( request->mad.hdr.tid[1] ), request->mad.hdr.mgmt_class,
  223. request->mad.hdr.class_version, request->mad.hdr.method,
  224. ntohs ( request->mad.hdr.attr_id ) );
  225. DBGC2_HDA ( gma, 0, &request->mad, sizeof ( request->mad ) );
  226. /* Construct I/O buffer */
  227. iobuf = alloc_iob ( sizeof ( request->mad ) );
  228. if ( ! iobuf ) {
  229. DBGC ( gma, "GMA %p could not allocate buffer for TID "
  230. "%08x%08x\n", gma, ntohl ( request->mad.hdr.tid[0] ),
  231. ntohl ( request->mad.hdr.tid[1] ) );
  232. return -ENOMEM;
  233. }
  234. memcpy ( iob_put ( iobuf, sizeof ( request->mad ) ), &request->mad,
  235. sizeof ( request->mad ) );
  236. /* Send I/O buffer */
  237. if ( ( rc = ib_post_send ( gma->ibdev, gma->qp, &request->av,
  238. iobuf ) ) != 0 ) {
  239. DBGC ( gma, "GMA %p could not send TID %08x%08x: %s\n",
  240. gma, ntohl ( request->mad.hdr.tid[0] ),
  241. ntohl ( request->mad.hdr.tid[1] ), strerror ( rc ) );
  242. free_iob ( iobuf );
  243. return rc;
  244. }
  245. return 0;
  246. }
  247. /**
  248. * Handle MAD request timer expiry
  249. *
  250. * @v timer Retry timer
  251. * @v expired Failure indicator
  252. */
  253. static void ib_gma_timer_expired ( struct retry_timer *timer, int expired ) {
  254. struct ib_mad_request *request =
  255. container_of ( timer, struct ib_mad_request, timer );
  256. struct ib_gma *gma = request->gma;
  257. /* Abandon TID if we have tried too many times */
  258. if ( expired ) {
  259. DBGC ( gma, "GMA %p abandoning TID %08x%08x\n",
  260. gma, ntohl ( request->mad.hdr.tid[0] ),
  261. ntohl ( request->mad.hdr.tid[1] ) );
  262. list_del ( &request->list );
  263. free ( request );
  264. return;
  265. }
  266. /* Restart retransmission timer */
  267. start_timer ( timer );
  268. /* Resend request */
  269. ib_gma_send ( gma, request );
  270. }
  271. /**
  272. * Issue MAD request
  273. *
  274. * @v gma General management agent
  275. * @v mad MAD request
  276. * @v av Destination address, or NULL for SM
  277. * @v retry Request should be retried until a response arrives
  278. * @ret rc Return status code
  279. */
  280. int ib_gma_request ( struct ib_gma *gma, union ib_mad *mad,
  281. struct ib_address_vector *av, int retry ) {
  282. struct ib_device *ibdev = gma->ibdev;
  283. struct ib_mad_request *request;
  284. /* Allocate and initialise structure */
  285. request = zalloc ( sizeof ( *request ) );
  286. if ( ! request ) {
  287. DBGC ( gma, "GMA %p could not allocate MAD request\n", gma );
  288. return -ENOMEM;
  289. }
  290. request->gma = gma;
  291. request->timer.expired = ib_gma_timer_expired;
  292. /* Determine address vector */
  293. if ( av ) {
  294. memcpy ( &request->av, av, sizeof ( request->av ) );
  295. } else {
  296. request->av.lid = ibdev->sm_lid;
  297. request->av.sl = ibdev->sm_sl;
  298. request->av.qpn = IB_QPN_GMA;
  299. request->av.qkey = IB_QKEY_GMA;
  300. }
  301. /* Copy MAD body */
  302. memcpy ( &request->mad, mad, sizeof ( request->mad ) );
  303. /* Allocate TID */
  304. request->mad.hdr.tid[0] = htonl ( IB_GMA_TID_MAGIC );
  305. request->mad.hdr.tid[1] = htonl ( ++next_request_tid );
  306. /* Send initial request. Ignore errors; the retry timer will
  307. * take care of those we care about.
  308. */
  309. ib_gma_send ( gma, request );
  310. /* Add to list and start timer if applicable */
  311. if ( retry ) {
  312. list_add ( &request->list, &gma->requests );
  313. start_timer ( &request->timer );
  314. } else {
  315. free ( request );
  316. }
  317. return 0;
  318. }
  319. /**
  320. * Create GMA
  321. *
  322. * @v gma General management agent
  323. * @v ibdev Infiniband device
  324. * @v qkey Queue key
  325. * @ret rc Return status code
  326. */
  327. int ib_create_gma ( struct ib_gma *gma, struct ib_device *ibdev,
  328. unsigned long qkey ) {
  329. int rc;
  330. /* Initialise fields */
  331. memset ( gma, 0, sizeof ( *gma ) );
  332. gma->ibdev = ibdev;
  333. INIT_LIST_HEAD ( &gma->requests );
  334. /* Create completion queue */
  335. gma->cq = ib_create_cq ( ibdev, IB_GMA_NUM_CQES,
  336. &ib_gma_completion_ops );
  337. if ( ! gma->cq ) {
  338. DBGC ( gma, "GMA %p could not allocate completion queue\n",
  339. gma );
  340. rc = -ENOMEM;
  341. goto err_create_cq;
  342. }
  343. /* Create queue pair */
  344. gma->qp = ib_create_qp ( ibdev, IB_GMA_NUM_SEND_WQES, gma->cq,
  345. IB_GMA_NUM_RECV_WQES, gma->cq, qkey );
  346. if ( ! gma->qp ) {
  347. DBGC ( gma, "GMA %p could not allocate queue pair\n", gma );
  348. rc = -ENOMEM;
  349. goto err_create_qp;
  350. }
  351. ib_qp_set_ownerdata ( gma->qp, gma );
  352. DBGC ( gma, "GMA %p running on QPN %#lx\n", gma, gma->qp->qpn );
  353. /* Fill receive ring */
  354. ib_refill_recv ( ibdev, gma->qp );
  355. return 0;
  356. ib_destroy_qp ( ibdev, gma->qp );
  357. err_create_qp:
  358. ib_destroy_cq ( ibdev, gma->cq );
  359. err_create_cq:
  360. return rc;
  361. }
  362. /**
  363. * Destroy GMA
  364. *
  365. * @v gma General management agent
  366. */
  367. void ib_destroy_gma ( struct ib_gma *gma ) {
  368. struct ib_device *ibdev = gma->ibdev;
  369. struct ib_mad_request *request;
  370. struct ib_mad_request *tmp;
  371. /* Flush any outstanding requests */
  372. list_for_each_entry_safe ( request, tmp, &gma->requests, list ) {
  373. stop_timer ( &request->timer );
  374. list_del ( &request->list );
  375. free ( request );
  376. }
  377. ib_destroy_qp ( ibdev, gma->qp );
  378. ib_destroy_cq ( ibdev, gma->cq );
  379. }