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

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