Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ib_sma.c 14KB

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