123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
-
-
- FILE_LICENCE ( GPL2_OR_LATER );
-
- #include <stdint.h>
- #include <stdlib.h>
- #include <string.h>
- #include <byteswap.h>
- #include <errno.h>
- #include <ipxe/infiniband.h>
- #include <ipxe/ib_mi.h>
- #include <ipxe/ib_pathrec.h>
-
-
-
-
- static void ib_path_complete ( struct ib_device *ibdev,
- struct ib_mad_interface *mi,
- struct ib_mad_transaction *madx,
- int rc, union ib_mad *mad,
- struct ib_address_vector *av __unused ) {
- struct ib_path *path = ib_madx_get_ownerdata ( madx );
- union ib_gid *dgid = &path->av.gid;
- struct ib_path_record *pathrec = &mad->sa.sa_data.path_record;
-
-
- if ( ( rc == 0 ) && ( mad->hdr.status != htons ( IB_MGMT_STATUS_OK ) ))
- rc = -ENETUNREACH;
- if ( rc != 0 ) {
- DBGC ( ibdev, "IBDEV %p path lookup for " IB_GID_FMT
- " failed: %s\n",
- ibdev, IB_GID_ARGS ( dgid ), strerror ( rc ) );
- goto out;
- }
-
-
- path->av.lid = ntohs ( pathrec->dlid );
- path->av.sl = ( pathrec->reserved__sl & 0x0f );
- path->av.rate = ( pathrec->rate_selector__rate & 0x3f );
- DBGC ( ibdev, "IBDEV %p path to " IB_GID_FMT " is %04x sl %d rate "
- "%d\n", ibdev, IB_GID_ARGS ( dgid ), path->av.lid, path->av.sl,
- path->av.rate );
-
- out:
-
- ib_destroy_madx ( ibdev, mi, madx );
- path->madx = NULL;
-
-
- path->op->complete ( ibdev, path, rc, &path->av );
- }
-
-
- static struct ib_mad_transaction_operations ib_path_op = {
- .complete = ib_path_complete,
- };
-
-
- struct ib_path *
- ib_create_path ( struct ib_device *ibdev, struct ib_address_vector *av,
- struct ib_path_operations *op ) {
- struct ib_path *path;
- union ib_mad mad;
- struct ib_mad_sa *sa = &mad.sa;
-
-
- path = zalloc ( sizeof ( *path ) );
- if ( ! path )
- goto err_alloc_path;
- path->ibdev = ibdev;
- memcpy ( &path->av, av, sizeof ( path->av ) );
- path->op = op;
-
-
- memset ( sa, 0, sizeof ( *sa ) );
- sa->mad_hdr.mgmt_class = IB_MGMT_CLASS_SUBN_ADM;
- sa->mad_hdr.class_version = IB_SA_CLASS_VERSION;
- sa->mad_hdr.method = IB_MGMT_METHOD_GET;
- sa->mad_hdr.attr_id = htons ( IB_SA_ATTR_PATH_REC );
- sa->sa_hdr.comp_mask[1] =
- htonl ( IB_SA_PATH_REC_DGID | IB_SA_PATH_REC_SGID );
- memcpy ( &sa->sa_data.path_record.dgid, &path->av.gid,
- sizeof ( sa->sa_data.path_record.dgid ) );
- memcpy ( &sa->sa_data.path_record.sgid, &ibdev->gid,
- sizeof ( sa->sa_data.path_record.sgid ) );
-
-
- path->madx = ib_create_madx ( ibdev, ibdev->gsi, &mad, NULL,
- &ib_path_op );
- if ( ! path->madx )
- goto err_create_madx;
- ib_madx_set_ownerdata ( path->madx, path );
-
- return path;
-
- ib_destroy_madx ( ibdev, ibdev->gsi, path->madx );
- err_create_madx:
- free ( path );
- err_alloc_path:
- return NULL;
- }
-
-
- void ib_destroy_path ( struct ib_device *ibdev, struct ib_path *path ) {
-
- if ( path->madx )
- ib_destroy_madx ( ibdev, ibdev->gsi, path->madx );
- free ( path );
- }
-
-
- #define IB_NUM_CACHED_PATHS 4
-
-
- struct ib_cached_path {
-
- struct ib_path *path;
- };
-
-
- static struct ib_cached_path ib_path_cache[IB_NUM_CACHED_PATHS];
-
-
- static unsigned int ib_path_cache_idx;
-
-
- static struct ib_cached_path *
- ib_find_path_cache_entry ( struct ib_device *ibdev, union ib_gid *dgid ) {
- struct ib_cached_path *cached;
- unsigned int i;
-
- for ( i = 0 ; i < IB_NUM_CACHED_PATHS ; i++ ) {
- cached = &ib_path_cache[i];
- if ( ! cached->path )
- continue;
- if ( cached->path->ibdev != ibdev )
- continue;
- if ( memcmp ( &cached->path->av.gid, dgid,
- sizeof ( cached->path->av.gid ) ) != 0 )
- continue;
- return cached;
- }
-
- return NULL;
- }
-
-
- static void ib_cached_path_complete ( struct ib_device *ibdev,
- struct ib_path *path, int rc,
- struct ib_address_vector *av __unused ) {
- struct ib_cached_path *cached = ib_path_get_ownerdata ( path );
-
-
- if ( rc != 0 ) {
-
- ib_destroy_path ( ibdev, path );
- memset ( cached, 0, sizeof ( *cached ) );
- return;
- }
-
-
-
- }
-
-
- static struct ib_path_operations ib_cached_path_op = {
- .complete = ib_cached_path_complete,
- };
-
-
- int ib_resolve_path ( struct ib_device *ibdev, struct ib_address_vector *av ) {
- union ib_gid *gid = &av->gid;
- struct ib_cached_path *cached;
- unsigned int cache_idx;
-
-
- if ( ! av->gid_present ) {
- DBGC ( ibdev, "IBDEV %p attempt to look up path without GID\n",
- ibdev );
- return -EINVAL;
- }
-
-
- cached = ib_find_path_cache_entry ( ibdev, gid );
- if ( cached && cached->path->av.lid ) {
-
- av->lid = cached->path->av.lid;
- av->rate = cached->path->av.rate;
- av->sl = cached->path->av.sl;
- DBGC2 ( ibdev, "IBDEV %p cache hit for " IB_GID_FMT "\n",
- ibdev, IB_GID_ARGS ( gid ) );
- return 0;
- }
- DBGC ( ibdev, "IBDEV %p cache miss for " IB_GID_FMT "%s\n", ibdev,
- IB_GID_ARGS ( gid ), ( cached ? " (in progress)" : "" ) );
-
-
- if ( cached )
- return -ENOENT;
-
-
- cache_idx = ( (ib_path_cache_idx++) % IB_NUM_CACHED_PATHS );
- cached = &ib_path_cache[cache_idx];
-
-
- if ( cached->path )
- ib_destroy_path ( ibdev, cached->path );
- memset ( cached, 0, sizeof ( *cached ) );
-
-
- cached->path = ib_create_path ( ibdev, av, &ib_cached_path_op );
- if ( ! cached->path ) {
- DBGC ( ibdev, "IBDEV %p could not create path\n",
- ibdev );
- return -ENOMEM;
- }
- ib_path_set_ownerdata ( cached->path, cached );
-
-
- return -ENOENT;
- }
|