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_sma.c 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /*
  2. * Copyright (C) 2008 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_sma.h>
  29. /**
  30. * @file
  31. *
  32. * Infiniband Subnet Management Agent
  33. *
  34. */
  35. /**
  36. * Get node information
  37. *
  38. * @v sma Subnet management agent
  39. * @v get Attribute to get
  40. */
  41. static void ib_sma_get_node_info ( struct ib_sma *sma,
  42. union ib_smp_data *get ) {
  43. struct ib_device *ibdev = sma->ibdev;
  44. struct ib_node_info *node_info = &get->node_info;
  45. memset ( node_info, 0, sizeof ( *node_info ) );
  46. node_info->base_version = IB_MGMT_BASE_VERSION;
  47. node_info->class_version = IB_SMP_CLASS_VERSION;
  48. node_info->node_type = IB_NODE_TYPE_HCA;
  49. node_info->num_ports = ib_get_hca_info ( ibdev, &node_info->sys_guid );
  50. memcpy ( &node_info->node_guid, &node_info->sys_guid,
  51. sizeof ( node_info->node_guid ) );
  52. memcpy ( &node_info->port_guid, &ibdev->gid.u.half[1],
  53. sizeof ( node_info->port_guid ) );
  54. node_info->partition_cap = htons ( 1 );
  55. node_info->local_port_num = ibdev->port;
  56. }
  57. /**
  58. * Get node description
  59. *
  60. * @v sma Subnet management agent
  61. * @v get Attribute to get
  62. */
  63. static void ib_sma_get_node_desc ( struct ib_sma *sma,
  64. union ib_smp_data *get ) {
  65. struct ib_device *ibdev = sma->ibdev;
  66. struct ib_node_desc *node_desc = &get->node_desc;
  67. struct ib_gid_half *guid = &ibdev->gid.u.half[1];
  68. memset ( node_desc, 0, sizeof ( *node_desc ) );
  69. snprintf ( node_desc->node_string, sizeof ( node_desc->node_string ),
  70. "gPXE %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x (%s)",
  71. guid->bytes[0], guid->bytes[1], guid->bytes[2],
  72. guid->bytes[3], guid->bytes[4], guid->bytes[5],
  73. guid->bytes[6], guid->bytes[7], ibdev->dev->name );
  74. }
  75. /**
  76. * Get GUID information
  77. *
  78. * @v sma Subnet management agent
  79. * @v get Attribute to get
  80. */
  81. static void ib_sma_get_guid_info ( struct ib_sma *sma,
  82. union ib_smp_data *get ) {
  83. struct ib_device *ibdev = sma->ibdev;
  84. struct ib_guid_info *guid_info = &get->guid_info;
  85. memset ( guid_info, 0, sizeof ( *guid_info ) );
  86. memcpy ( guid_info->guid[0], &ibdev->gid.u.half[1],
  87. sizeof ( guid_info->guid[0] ) );
  88. }
  89. /**
  90. * Get port information
  91. *
  92. * @v sma Subnet management agent
  93. * @v get Attribute to get
  94. */
  95. static void ib_sma_get_port_info ( struct ib_sma *sma,
  96. union ib_smp_data *get ) {
  97. struct ib_device *ibdev = sma->ibdev;
  98. struct ib_port_info *port_info = &get->port_info;
  99. memset ( port_info, 0, sizeof ( *port_info ) );
  100. memcpy ( port_info->gid_prefix, &ibdev->gid.u.half[0],
  101. sizeof ( port_info->gid_prefix ) );
  102. port_info->lid = ntohs ( ibdev->lid );
  103. port_info->mastersm_lid = ntohs ( ibdev->sm_lid );
  104. port_info->local_port_num = ibdev->port;
  105. port_info->link_width_enabled = ibdev->link_width;
  106. port_info->link_width_supported = ibdev->link_width;
  107. port_info->link_width_active = ibdev->link_width;
  108. port_info->link_speed_supported__port_state =
  109. ( ( ibdev->link_speed << 4 ) | ibdev->port_state );
  110. port_info->port_phys_state__link_down_def_state =
  111. ( ( IB_PORT_PHYS_STATE_POLLING << 4 ) |
  112. IB_PORT_PHYS_STATE_POLLING );
  113. port_info->link_speed_active__link_speed_enabled =
  114. ( ( ibdev->link_speed << 4 ) | ibdev->link_speed );
  115. port_info->neighbour_mtu__mastersm_sl =
  116. ( ( IB_MTU_2048 << 4 ) | ibdev->sm_sl );
  117. port_info->vl_cap__init_type = ( IB_VL_0 << 4 );
  118. port_info->init_type_reply__mtu_cap = IB_MTU_2048;
  119. port_info->operational_vls__enforcement = ( IB_VL_0 << 4 );
  120. port_info->guid_cap = 1;
  121. }
  122. /**
  123. * Set port information
  124. *
  125. * @v sma Subnet management agent
  126. * @v set Attribute to set
  127. * @ret rc Return status code
  128. */
  129. static int ib_sma_set_port_info ( struct ib_sma *sma,
  130. const union ib_smp_data *set ) {
  131. struct ib_device *ibdev = sma->ibdev;
  132. const struct ib_port_info *port_info = &set->port_info;
  133. memcpy ( &ibdev->gid.u.half[0], port_info->gid_prefix,
  134. sizeof ( ibdev->gid.u.half[0] ) );
  135. ibdev->lid = ntohs ( port_info->lid );
  136. ibdev->sm_lid = ntohs ( port_info->mastersm_lid );
  137. ibdev->sm_sl = ( port_info->neighbour_mtu__mastersm_sl & 0xf );
  138. if ( ! sma->op->set_port_info ) {
  139. /* Not an error; we just ignore all other settings */
  140. return 0;
  141. }
  142. return sma->op->set_port_info ( ibdev, port_info );
  143. }
  144. /**
  145. * Get partition key table
  146. *
  147. * @v sma Subnet management agent
  148. * @v get Attribute to get
  149. */
  150. static void ib_sma_get_pkey_table ( struct ib_sma *sma,
  151. union ib_smp_data *get ) {
  152. struct ib_device *ibdev = sma->ibdev;
  153. struct ib_pkey_table *pkey_table = &get->pkey_table;
  154. memset ( pkey_table, 0, sizeof ( *pkey_table ) );
  155. pkey_table->pkey[0] = htons ( ibdev->pkey );
  156. }
  157. /**
  158. * Set partition key table
  159. *
  160. * @v sma Subnet management agent
  161. * @v set Attribute to set
  162. */
  163. static int ib_sma_set_pkey_table ( struct ib_sma *sma,
  164. const union ib_smp_data *get ) {
  165. struct ib_device *ibdev = sma->ibdev;
  166. const struct ib_pkey_table *pkey_table = &get->pkey_table;
  167. ibdev->pkey = ntohs ( pkey_table->pkey[0] );
  168. return 0;
  169. }
  170. /** An attribute handler */
  171. struct ib_sma_handler {
  172. /** Attribute (in network byte order) */
  173. uint16_t attr_id;
  174. /** Get attribute
  175. *
  176. * @v sma Subnet management agent
  177. * @v get Attribute to get
  178. * @ret rc Return status code
  179. */
  180. void ( * get ) ( struct ib_sma *sma, union ib_smp_data *get );
  181. /** Set attribute
  182. *
  183. * @v sma Subnet management agent
  184. * @v set Attribute to set
  185. * @ret rc Return status code
  186. */
  187. int ( * set ) ( struct ib_sma *sma, const union ib_smp_data *set );
  188. };
  189. /** List of attribute handlers */
  190. static struct ib_sma_handler ib_sma_handlers[] = {
  191. { htons ( IB_SMP_ATTR_NODE_DESC ),
  192. ib_sma_get_node_desc, NULL },
  193. { htons ( IB_SMP_ATTR_NODE_INFO ),
  194. ib_sma_get_node_info, NULL },
  195. { htons ( IB_SMP_ATTR_GUID_INFO ),
  196. ib_sma_get_guid_info, NULL },
  197. { htons ( IB_SMP_ATTR_PORT_INFO ),
  198. ib_sma_get_port_info, ib_sma_set_port_info },
  199. { htons ( IB_SMP_ATTR_PKEY_TABLE ),
  200. ib_sma_get_pkey_table, ib_sma_set_pkey_table },
  201. };
  202. /**
  203. * Identify attribute handler
  204. *
  205. * @v attr_id Attribute ID (in network byte order)
  206. * @ret handler Attribute handler (or NULL)
  207. */
  208. static struct ib_sma_handler * ib_sma_handler ( uint16_t attr_id ) {
  209. struct ib_sma_handler *handler;
  210. unsigned int i;
  211. for ( i = 0 ; i < ( sizeof ( ib_sma_handlers ) /
  212. sizeof ( ib_sma_handlers[0] ) ) ; i++ ) {
  213. handler = &ib_sma_handlers[i];
  214. if ( handler->attr_id == attr_id )
  215. return handler;
  216. }
  217. return NULL;
  218. }
  219. /**
  220. * Respond to management datagram
  221. *
  222. * @v sma Subnet management agent
  223. * @v mad Management datagram
  224. * @ret rc Return status code
  225. */
  226. static int ib_sma_mad ( struct ib_sma *sma, union ib_mad *mad ) {
  227. struct ib_device *ibdev = sma->ibdev;
  228. struct ib_mad_hdr *hdr = &mad->hdr;
  229. struct ib_mad_smp *smp = &mad->smp;
  230. struct ib_sma_handler *handler = NULL;
  231. unsigned int hop_pointer;
  232. unsigned int hop_count;
  233. int rc;
  234. DBGC ( sma, "SMA %p received SMP with bv=%02x mc=%02x cv=%02x "
  235. "meth=%02x attr=%04x mod=%08x\n", sma, hdr->base_version,
  236. hdr->mgmt_class, hdr->class_version, hdr->method,
  237. ntohs ( hdr->attr_id ), ntohl ( hdr->attr_mod ) );
  238. DBGC2_HDA ( sma, 0, mad, sizeof ( *mad ) );
  239. /* Sanity checks */
  240. if ( hdr->base_version != IB_MGMT_BASE_VERSION ) {
  241. DBGC ( sma, "SMA %p unsupported base version %x\n",
  242. sma, hdr->base_version );
  243. return -ENOTSUP;
  244. }
  245. if ( ( hdr->mgmt_class != IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE ) &&
  246. ( hdr->mgmt_class != IB_MGMT_CLASS_SUBN_LID_ROUTED ) ) {
  247. DBGC ( sma, "SMA %p unsupported management class %x\n",
  248. sma, hdr->mgmt_class );
  249. return -ENOTSUP;
  250. }
  251. if ( hdr->class_version != IB_SMP_CLASS_VERSION ) {
  252. DBGC ( sma, "SMA %p unsupported class version %x\n",
  253. sma, hdr->class_version );
  254. return -ENOTSUP;
  255. }
  256. if ( ( hdr->method != IB_MGMT_METHOD_GET ) &&
  257. ( hdr->method != IB_MGMT_METHOD_SET ) ) {
  258. DBGC ( sma, "SMA %p unsupported method %x\n",
  259. sma, hdr->method );
  260. return -ENOTSUP;
  261. }
  262. /* Identify handler */
  263. if ( ! ( handler = ib_sma_handler ( hdr->attr_id ) ) ) {
  264. DBGC ( sma, "SMA %p unsupported attribute %x\n",
  265. sma, ntohs ( hdr->attr_id ) );
  266. hdr->status = htons ( IB_MGMT_STATUS_UNSUPPORTED_METHOD_ATTR );
  267. goto respond_without_data;
  268. }
  269. /* Set attribute (if applicable) */
  270. if ( hdr->method != IB_MGMT_METHOD_SET ) {
  271. hdr->status = htons ( IB_MGMT_STATUS_OK );
  272. goto respond;
  273. }
  274. if ( ! handler->set ) {
  275. DBGC ( sma, "SMA %p attribute %x is unsettable\n",
  276. sma, ntohs ( hdr->attr_id ) );
  277. hdr->status = htons ( IB_MGMT_STATUS_UNSUPPORTED_METHOD_ATTR );
  278. goto respond;
  279. }
  280. if ( ( rc = handler->set ( sma, &smp->smp_data ) ) != 0 ) {
  281. DBGC ( sma, "SMA %p could not set attribute %x: %s\n",
  282. sma, ntohs ( hdr->attr_id ), strerror ( rc ) );
  283. hdr->status = htons ( IB_MGMT_STATUS_UNSUPPORTED_METHOD_ATTR );
  284. goto respond;
  285. }
  286. hdr->status = htons ( IB_MGMT_STATUS_OK );
  287. respond:
  288. /* Get attribute */
  289. handler->get ( sma, &smp->smp_data );
  290. respond_without_data:
  291. /* Set method to "Get Response" */
  292. hdr->method = IB_MGMT_METHOD_GET_RESP;
  293. /* Set response fields for directed route SMPs */
  294. if ( hdr->mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE ) {
  295. hdr->status |= htons ( IB_SMP_STATUS_D_INBOUND );
  296. hop_pointer = smp->mad_hdr.class_specific.smp.hop_pointer;
  297. hop_count = smp->mad_hdr.class_specific.smp.hop_count;
  298. assert ( hop_count == hop_pointer );
  299. if ( hop_pointer < ( sizeof ( smp->return_path.hops ) /
  300. sizeof ( smp->return_path.hops[0] ) ) ) {
  301. smp->return_path.hops[hop_pointer] = ibdev->port;
  302. } else {
  303. DBGC ( sma, "SMA %p invalid hop pointer %d\n",
  304. sma, hop_pointer );
  305. return -EINVAL;
  306. }
  307. }
  308. DBGC ( sma, "SMA %p responding with status=%04x\n",
  309. sma, ntohs ( hdr->status ) );
  310. DBGC2_HDA ( sma, 0, mad, sizeof ( *mad ) );
  311. return 0;
  312. }
  313. /**
  314. * Complete SMA send
  315. *
  316. *
  317. * @v ibdev Infiniband device
  318. * @v qp Queue pair
  319. * @v iobuf I/O buffer
  320. * @v rc Completion status code
  321. */
  322. static void ib_sma_complete_send ( struct ib_device *ibdev __unused,
  323. struct ib_queue_pair *qp,
  324. struct io_buffer *iobuf, int rc ) {
  325. struct ib_sma *sma = ib_qp_get_ownerdata ( qp );
  326. if ( rc != 0 ) {
  327. DBGC ( sma, "SMA %p send completion error: %s\n",
  328. sma, strerror ( rc ) );
  329. }
  330. free_iob ( iobuf );
  331. }
  332. /**
  333. * Complete SMA receive
  334. *
  335. *
  336. * @v ibdev Infiniband device
  337. * @v qp Queue pair
  338. * @v av Address vector
  339. * @v iobuf I/O buffer
  340. * @v rc Completion status code
  341. */
  342. static void ib_sma_complete_recv ( struct ib_device *ibdev,
  343. struct ib_queue_pair *qp,
  344. struct ib_address_vector *av,
  345. struct io_buffer *iobuf, int rc ) {
  346. struct ib_sma *sma = ib_qp_get_ownerdata ( qp );
  347. union ib_mad *mad;
  348. /* Ignore errors */
  349. if ( rc != 0 ) {
  350. DBGC ( sma, "SMA %p RX error: %s\n", sma, strerror ( rc ) );
  351. goto err;
  352. }
  353. /* Sanity check */
  354. if ( iob_len ( iobuf ) != sizeof ( *mad ) ) {
  355. DBGC ( sma, "SMA %p RX bad size (%zd bytes)\n",
  356. sma, iob_len ( iobuf ) );
  357. goto err;
  358. }
  359. mad = iobuf->data;
  360. /* Construct MAD response */
  361. if ( ( rc = ib_sma_mad ( sma, mad ) ) != 0 ) {
  362. DBGC ( sma, "SMA %p could not construct MAD response: %s\n",
  363. sma, strerror ( rc ) );
  364. goto err;
  365. }
  366. /* Send MAD response */
  367. if ( ( rc = ib_post_send ( ibdev, qp, av, iobuf ) ) != 0 ) {
  368. DBGC ( sma, "SMA %p could not send MAD response: %s\n",
  369. sma, strerror ( rc ) );
  370. goto err;
  371. }
  372. return;
  373. err:
  374. free_iob ( iobuf );
  375. }
  376. /** SMA completion operations */
  377. static struct ib_completion_queue_operations ib_sma_completion_ops = {
  378. .complete_send = ib_sma_complete_send,
  379. .complete_recv = ib_sma_complete_recv,
  380. };
  381. /**
  382. * Create SMA
  383. *
  384. * @v sma Subnet management agent
  385. * @v ibdev Infiniband device
  386. * @v op Subnet management operations
  387. * @ret rc Return status code
  388. */
  389. int ib_create_sma ( struct ib_sma *sma, struct ib_device *ibdev,
  390. struct ib_sma_operations *op ) {
  391. int rc;
  392. /* Initialise fields */
  393. memset ( sma, 0, sizeof ( *sma ) );
  394. sma->ibdev = ibdev;
  395. sma->op = op;
  396. /* Create completion queue */
  397. sma->cq = ib_create_cq ( ibdev, IB_SMA_NUM_CQES,
  398. &ib_sma_completion_ops );
  399. if ( ! sma->cq ) {
  400. rc = -ENOMEM;
  401. goto err_create_cq;
  402. }
  403. /* Create queue pair */
  404. sma->qp = ib_create_qp ( ibdev, IB_SMA_NUM_SEND_WQES, sma->cq,
  405. IB_SMA_NUM_RECV_WQES, sma->cq, 0 );
  406. if ( ! sma->qp ) {
  407. rc = -ENOMEM;
  408. goto err_create_qp;
  409. }
  410. ib_qp_set_ownerdata ( sma->qp, sma );
  411. /* If we don't get QP0, we can't function */
  412. if ( sma->qp->qpn != IB_QPN_SMP ) {
  413. DBGC ( sma, "SMA %p on QPN %lx, needs to be on QPN 0\n",
  414. sma, sma->qp->qpn );
  415. rc = -ENOTSUP;
  416. goto err_not_qp0;
  417. }
  418. /* Fill receive ring */
  419. ib_refill_recv ( ibdev, sma->qp );
  420. return 0;
  421. err_not_qp0:
  422. ib_destroy_qp ( ibdev, sma->qp );
  423. err_create_qp:
  424. ib_destroy_cq ( ibdev, sma->cq );
  425. err_create_cq:
  426. return rc;
  427. }
  428. /**
  429. * Destroy SMA
  430. *
  431. * @v sma Subnet management agent
  432. */
  433. void ib_destroy_sma ( struct ib_sma *sma ) {
  434. struct ib_device *ibdev = sma->ibdev;
  435. ib_destroy_qp ( ibdev, sma->qp );
  436. ib_destroy_cq ( ibdev, sma->cq );
  437. }