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 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 <byteswap.h>
  24. #include <errno.h>
  25. #include <ipxe/infiniband.h>
  26. #include <ipxe/ib_mi.h>
  27. #include <ipxe/ib_pathrec.h>
  28. /** @file
  29. *
  30. * Infiniband path lookups
  31. *
  32. */
  33. /**
  34. * Handle path transaction completion
  35. *
  36. * @v ibdev Infiniband device
  37. * @v mi Management interface
  38. * @v madx Management transaction
  39. * @v rc Status code
  40. * @v mad Received MAD (or NULL on error)
  41. * @v av Source address vector (or NULL on error)
  42. */
  43. static void ib_path_complete ( struct ib_device *ibdev,
  44. struct ib_mad_interface *mi,
  45. struct ib_mad_transaction *madx,
  46. int rc, union ib_mad *mad,
  47. struct ib_address_vector *av __unused ) {
  48. struct ib_path *path = ib_madx_get_ownerdata ( madx );
  49. union ib_gid *dgid = &path->av.gid;
  50. struct ib_path_record *pathrec = &mad->sa.sa_data.path_record;
  51. /* Report failures */
  52. if ( ( rc == 0 ) && ( mad->hdr.status != htons ( IB_MGMT_STATUS_OK ) ))
  53. rc = -ENETUNREACH;
  54. if ( rc != 0 ) {
  55. DBGC ( ibdev, "IBDEV %p path lookup for " IB_GID_FMT
  56. " failed: %s\n",
  57. ibdev, IB_GID_ARGS ( dgid ), strerror ( rc ) );
  58. goto out;
  59. }
  60. /* Extract values from MAD */
  61. path->av.lid = ntohs ( pathrec->dlid );
  62. path->av.sl = ( pathrec->reserved__sl & 0x0f );
  63. path->av.rate = ( pathrec->rate_selector__rate & 0x3f );
  64. DBGC ( ibdev, "IBDEV %p path to " IB_GID_FMT " is %04x sl %d rate "
  65. "%d\n", ibdev, IB_GID_ARGS ( dgid ), path->av.lid, path->av.sl,
  66. path->av.rate );
  67. out:
  68. /* Destroy the completed transaction */
  69. ib_destroy_madx ( ibdev, mi, madx );
  70. path->madx = NULL;
  71. /* Hand off to upper completion handler */
  72. path->op->complete ( ibdev, path, rc, &path->av );
  73. }
  74. /** Path transaction completion operations */
  75. static struct ib_mad_transaction_operations ib_path_op = {
  76. .complete = ib_path_complete,
  77. };
  78. /**
  79. * Create path
  80. *
  81. * @v ibdev Infiniband device
  82. * @v av Address vector to complete
  83. * @v op Path operations
  84. * @ret path Path
  85. */
  86. struct ib_path *
  87. ib_create_path ( struct ib_device *ibdev, struct ib_address_vector *av,
  88. struct ib_path_operations *op ) {
  89. struct ib_path *path;
  90. union ib_mad mad;
  91. struct ib_mad_sa *sa = &mad.sa;
  92. /* Allocate and initialise structure */
  93. path = zalloc ( sizeof ( *path ) );
  94. if ( ! path )
  95. goto err_alloc_path;
  96. path->ibdev = ibdev;
  97. memcpy ( &path->av, av, sizeof ( path->av ) );
  98. path->op = op;
  99. /* Construct path request */
  100. memset ( sa, 0, sizeof ( *sa ) );
  101. sa->mad_hdr.mgmt_class = IB_MGMT_CLASS_SUBN_ADM;
  102. sa->mad_hdr.class_version = IB_SA_CLASS_VERSION;
  103. sa->mad_hdr.method = IB_MGMT_METHOD_GET;
  104. sa->mad_hdr.attr_id = htons ( IB_SA_ATTR_PATH_REC );
  105. sa->sa_hdr.comp_mask[1] =
  106. htonl ( IB_SA_PATH_REC_DGID | IB_SA_PATH_REC_SGID );
  107. memcpy ( &sa->sa_data.path_record.dgid, &path->av.gid,
  108. sizeof ( sa->sa_data.path_record.dgid ) );
  109. memcpy ( &sa->sa_data.path_record.sgid, &ibdev->gid,
  110. sizeof ( sa->sa_data.path_record.sgid ) );
  111. /* Create management transaction */
  112. path->madx = ib_create_madx ( ibdev, ibdev->gsi, &mad, NULL,
  113. &ib_path_op );
  114. if ( ! path->madx )
  115. goto err_create_madx;
  116. ib_madx_set_ownerdata ( path->madx, path );
  117. return path;
  118. ib_destroy_madx ( ibdev, ibdev->gsi, path->madx );
  119. err_create_madx:
  120. free ( path );
  121. err_alloc_path:
  122. return NULL;
  123. }
  124. /**
  125. * Destroy path
  126. *
  127. * @v ibdev Infiniband device
  128. * @v path Path
  129. */
  130. void ib_destroy_path ( struct ib_device *ibdev, struct ib_path *path ) {
  131. if ( path->madx )
  132. ib_destroy_madx ( ibdev, ibdev->gsi, path->madx );
  133. free ( path );
  134. }
  135. /** Number of path cache entries
  136. *
  137. * Must be a power of two.
  138. */
  139. #define IB_NUM_CACHED_PATHS 4
  140. /** A cached path */
  141. struct ib_cached_path {
  142. /** Path */
  143. struct ib_path *path;
  144. };
  145. /** Path cache */
  146. static struct ib_cached_path ib_path_cache[IB_NUM_CACHED_PATHS];
  147. /** Oldest path cache entry index */
  148. static unsigned int ib_path_cache_idx;
  149. /**
  150. * Find path cache entry
  151. *
  152. * @v ibdev Infiniband device
  153. * @v dgid Destination GID
  154. * @ret path Path cache entry, or NULL
  155. */
  156. static struct ib_cached_path *
  157. ib_find_path_cache_entry ( struct ib_device *ibdev, union ib_gid *dgid ) {
  158. struct ib_cached_path *cached;
  159. unsigned int i;
  160. for ( i = 0 ; i < IB_NUM_CACHED_PATHS ; i++ ) {
  161. cached = &ib_path_cache[i];
  162. if ( ! cached->path )
  163. continue;
  164. if ( cached->path->ibdev != ibdev )
  165. continue;
  166. if ( memcmp ( &cached->path->av.gid, dgid,
  167. sizeof ( cached->path->av.gid ) ) != 0 )
  168. continue;
  169. return cached;
  170. }
  171. return NULL;
  172. }
  173. /**
  174. * Handle cached path transaction completion
  175. *
  176. * @v ibdev Infiniband device
  177. * @v path Path
  178. * @v rc Status code
  179. * @v av Address vector, or NULL on error
  180. */
  181. static void ib_cached_path_complete ( struct ib_device *ibdev,
  182. struct ib_path *path, int rc,
  183. struct ib_address_vector *av __unused ) {
  184. struct ib_cached_path *cached = ib_path_get_ownerdata ( path );
  185. /* If the transaction failed, erase the cache entry */
  186. if ( rc != 0 ) {
  187. /* Destroy the old cache entry */
  188. ib_destroy_path ( ibdev, path );
  189. memset ( cached, 0, sizeof ( *cached ) );
  190. return;
  191. }
  192. /* Do not destroy the completed transaction; we still need to
  193. * refer to the resolved path.
  194. */
  195. }
  196. /** Cached path transaction completion operations */
  197. static struct ib_path_operations ib_cached_path_op = {
  198. .complete = ib_cached_path_complete,
  199. };
  200. /**
  201. * Resolve path
  202. *
  203. * @v ibdev Infiniband device
  204. * @v av Address vector to complete
  205. * @ret rc Return status code
  206. *
  207. * This provides a non-transactional way to resolve a path, via a
  208. * cache similar to ARP.
  209. */
  210. int ib_resolve_path ( struct ib_device *ibdev, struct ib_address_vector *av ) {
  211. union ib_gid *gid = &av->gid;
  212. struct ib_cached_path *cached;
  213. unsigned int cache_idx;
  214. /* Sanity check */
  215. if ( ! av->gid_present ) {
  216. DBGC ( ibdev, "IBDEV %p attempt to look up path without GID\n",
  217. ibdev );
  218. return -EINVAL;
  219. }
  220. /* Look in cache for a matching entry */
  221. cached = ib_find_path_cache_entry ( ibdev, gid );
  222. if ( cached && cached->path->av.lid ) {
  223. /* Populated entry found */
  224. av->lid = cached->path->av.lid;
  225. av->rate = cached->path->av.rate;
  226. av->sl = cached->path->av.sl;
  227. DBGC2 ( ibdev, "IBDEV %p cache hit for " IB_GID_FMT "\n",
  228. ibdev, IB_GID_ARGS ( gid ) );
  229. return 0;
  230. }
  231. DBGC ( ibdev, "IBDEV %p cache miss for " IB_GID_FMT "%s\n", ibdev,
  232. IB_GID_ARGS ( gid ), ( cached ? " (in progress)" : "" ) );
  233. /* If lookup is already in progress, do nothing */
  234. if ( cached )
  235. return -ENOENT;
  236. /* Locate a new cache entry to use */
  237. cache_idx = ( (ib_path_cache_idx++) % IB_NUM_CACHED_PATHS );
  238. cached = &ib_path_cache[cache_idx];
  239. /* Destroy the old cache entry */
  240. if ( cached->path )
  241. ib_destroy_path ( ibdev, cached->path );
  242. memset ( cached, 0, sizeof ( *cached ) );
  243. /* Create new path */
  244. cached->path = ib_create_path ( ibdev, av, &ib_cached_path_op );
  245. if ( ! cached->path ) {
  246. DBGC ( ibdev, "IBDEV %p could not create path\n",
  247. ibdev );
  248. return -ENOMEM;
  249. }
  250. ib_path_set_ownerdata ( cached->path, cached );
  251. /* Not found yet */
  252. return -ENOENT;
  253. }