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_pathrec.c 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 <string.h>
  21. #include <byteswap.h>
  22. #include <errno.h>
  23. #include <gpxe/infiniband.h>
  24. #include <gpxe/ib_gma.h>
  25. #include <gpxe/ib_pathrec.h>
  26. /** @file
  27. *
  28. * Infiniband path lookups
  29. *
  30. */
  31. /** Number of path record cache entries
  32. *
  33. * Must be a power of two.
  34. */
  35. #define IB_NUM_CACHED_PATHS 4
  36. /** A path record cache entry */
  37. struct ib_cached_path_record {
  38. /** Infiniband device's port GID
  39. *
  40. * Used to disambiguate cache entries when we have multiple
  41. * Infiniband devices, without having to maintain a pointer to
  42. * the Infiniband device.
  43. */
  44. struct ib_gid sgid;
  45. /** Destination GID */
  46. struct ib_gid dgid;
  47. /** Destination LID */
  48. unsigned int dlid;
  49. /** Rate */
  50. unsigned int rate;
  51. /** Service level */
  52. unsigned int sl;
  53. };
  54. /** Path record cache */
  55. static struct ib_cached_path_record ib_path_cache[IB_NUM_CACHED_PATHS];
  56. /** Oldest path record cache entry index */
  57. static unsigned int ib_path_cache_idx;
  58. /**
  59. * Find path record cache entry
  60. *
  61. * @v ibdev Infiniband device
  62. * @v dgid Destination GID
  63. * @ret cached Path record cache entry, or NULL
  64. */
  65. static struct ib_cached_path_record *
  66. ib_find_path_cache_entry ( struct ib_device *ibdev, struct ib_gid *dgid ) {
  67. struct ib_cached_path_record *cached;
  68. unsigned int i;
  69. for ( i = 0 ; i < IB_NUM_CACHED_PATHS ; i++ ) {
  70. cached = &ib_path_cache[i];
  71. if ( memcmp ( &cached->sgid, &ibdev->gid,
  72. sizeof ( cached->sgid ) ) != 0 )
  73. continue;
  74. if ( memcmp ( &cached->dgid, dgid,
  75. sizeof ( cached->dgid ) ) != 0 )
  76. continue;
  77. return cached;
  78. }
  79. return NULL;
  80. }
  81. /**
  82. * Resolve path record
  83. *
  84. * @v ibdev Infiniband device
  85. * @v av Address vector to complete
  86. * @ret rc Return status code
  87. */
  88. int ib_resolve_path ( struct ib_device *ibdev,
  89. struct ib_address_vector *av ) {
  90. struct ib_gid *gid = &av->gid;
  91. struct ib_cached_path_record *cached;
  92. union ib_mad mad;
  93. struct ib_mad_sa *sa = &mad.sa;
  94. unsigned int cache_idx;
  95. int rc;
  96. /* Sanity check */
  97. if ( ! av->gid_present ) {
  98. DBGC ( ibdev, "IBDEV %p attempt to look up path record "
  99. "without GID\n", ibdev );
  100. return -EINVAL;
  101. }
  102. /* Look in cache for a matching entry */
  103. cached = ib_find_path_cache_entry ( ibdev, gid );
  104. if ( cached && cached->dlid ) {
  105. /* Populated entry found */
  106. av->lid = cached->dlid;
  107. av->rate = cached->rate;
  108. av->sl = cached->sl;
  109. DBGC2 ( ibdev, "IBDEV %p cache hit for %08x:%08x:%08x:%08x\n",
  110. ibdev, htonl ( gid->u.dwords[0] ),
  111. htonl ( gid->u.dwords[1] ), htonl ( gid->u.dwords[2] ),
  112. htonl ( gid->u.dwords[3] ) );
  113. return 0;
  114. }
  115. DBGC ( ibdev, "IBDEV %p cache miss for %08x:%08x:%08x:%08x%s\n", ibdev,
  116. htonl ( gid->u.dwords[0] ), htonl ( gid->u.dwords[1] ),
  117. htonl ( gid->u.dwords[2] ), htonl ( gid->u.dwords[3] ),
  118. ( cached ? " (in progress)" : "" ) );
  119. /* If no unresolved entry was found, then create a new one */
  120. if ( ! cached ) {
  121. cache_idx = ( (ib_path_cache_idx++) % IB_NUM_CACHED_PATHS );
  122. cached = &ib_path_cache[cache_idx];
  123. memset ( cached, 0, sizeof ( *cached ) );
  124. memcpy ( &cached->sgid, &ibdev->gid, sizeof ( cached->sgid ) );
  125. memcpy ( &cached->dgid, gid, sizeof ( cached->dgid ) );
  126. }
  127. /* Construct path record request */
  128. memset ( sa, 0, sizeof ( *sa ) );
  129. sa->mad_hdr.base_version = IB_MGMT_BASE_VERSION;
  130. sa->mad_hdr.mgmt_class = IB_MGMT_CLASS_SUBN_ADM;
  131. sa->mad_hdr.class_version = IB_SA_CLASS_VERSION;
  132. sa->mad_hdr.method = IB_MGMT_METHOD_GET;
  133. sa->mad_hdr.attr_id = htons ( IB_SA_ATTR_PATH_REC );
  134. sa->sa_hdr.comp_mask[1] =
  135. htonl ( IB_SA_PATH_REC_DGID | IB_SA_PATH_REC_SGID );
  136. memcpy ( &sa->sa_data.path_record.dgid, &cached->dgid,
  137. sizeof ( sa->sa_data.path_record.dgid ) );
  138. memcpy ( &sa->sa_data.path_record.sgid, &cached->sgid,
  139. sizeof ( sa->sa_data.path_record.sgid ) );
  140. /* Issue path record request */
  141. if ( ( rc = ib_gma_request ( &ibdev->gma, &mad, NULL ) ) != 0 ) {
  142. DBGC ( ibdev, "IBDEV %p could not get path record: %s\n",
  143. ibdev, strerror ( rc ) );
  144. return rc;
  145. }
  146. /* Not found yet */
  147. return -ENOENT;
  148. }
  149. /**
  150. * Handle path record response
  151. *
  152. * @v ibdev Infiniband device
  153. * @v mad MAD
  154. * @ret rc Return status code
  155. */
  156. static int ib_handle_path_record ( struct ib_device *ibdev,
  157. union ib_mad *mad ) {
  158. struct ib_path_record *path_record = &mad->sa.sa_data.path_record;
  159. struct ib_gid *dgid = &path_record->dgid;
  160. struct ib_cached_path_record *cached;
  161. unsigned int dlid;
  162. unsigned int sl;
  163. unsigned int rate;
  164. /* Ignore if not a success */
  165. if ( mad->hdr.status != htons ( IB_MGMT_STATUS_OK ) ) {
  166. DBGC ( ibdev, "IBDEV %p path record lookup failed with status "
  167. "%04x\n", ibdev, ntohs ( mad->hdr.status ) );
  168. return -EINVAL;
  169. }
  170. /* Extract values from MAD */
  171. dlid = ntohs ( path_record->dlid );
  172. sl = ( path_record->reserved__sl & 0x0f );
  173. rate = ( path_record->rate_selector__rate & 0x3f );
  174. DBGC ( ibdev, "IBDEV %p path to %08x:%08x:%08x:%08x is %04x sl %d "
  175. "rate %d\n", ibdev, htonl ( dgid->u.dwords[0] ),
  176. htonl ( dgid->u.dwords[1] ), htonl ( dgid->u.dwords[2] ),
  177. htonl ( dgid->u.dwords[3] ), dlid, sl, rate );
  178. /* Look for a matching cache entry to fill in */
  179. if ( ( cached = ib_find_path_cache_entry ( ibdev, dgid ) ) != NULL ) {
  180. DBGC ( ibdev, "IBDEV %p cache add for %08x:%08x:%08x:%08x\n",
  181. ibdev, htonl ( dgid->u.dwords[0] ),
  182. htonl ( dgid->u.dwords[1] ),
  183. htonl ( dgid->u.dwords[2] ),
  184. htonl ( dgid->u.dwords[3] ) );
  185. cached->dlid = dlid;
  186. cached->rate = rate;
  187. cached->sl = sl;
  188. }
  189. return 0;
  190. }
  191. /** Path record response handler */
  192. struct ib_mad_handler ib_path_record_handler __ib_mad_handler = {
  193. .mgmt_class = IB_MGMT_CLASS_SUBN_ADM,
  194. .class_version = IB_SA_CLASS_VERSION,
  195. .method = IB_MGMT_METHOD_GET_RESP,
  196. .attr_id = htons ( IB_SA_ATTR_PATH_REC ),
  197. .handle = ib_handle_path_record,
  198. };