1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
5 * Network Interface code
7 * Copyright (c) 2001-2003 Cluster File Systems, Inc.
8 * Copyright (c) 2001-2002 Sandia National Laboratories
10 * This file is part of Lustre, http://www.sf.net/projects/lustre/
12 * Lustre is free software; you can redistribute it and/or
13 * modify it under the terms of version 2 of the GNU General Public
14 * License as published by the Free Software Foundation.
16 * Lustre is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with Lustre; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include <portals/api-support.h>
28 /* Put some magic in the NI handle so uninitialised/zeroed handles are easy
30 #define NI_HANDLE_MAGIC 0xebc0de00
31 #define NI_HANDLE_MASK 0x000000ff
33 static nal_t *ptl_interfaces[MAX_NIS];
34 int ptl_num_interfaces = 0;
36 nal_t *ptl_hndl2nal(ptl_handle_any_t *handle)
38 unsigned int idx = handle->nal_idx;
40 /* XXX we really rely on the caller NOT racing with interface
41 * setup/teardown. That ensures her NI handle can't get
42 * invalidated out from under her (or worse, swapped for a
43 * completely different interface!) */
45 if (((idx ^ NI_HANDLE_MAGIC) & ~NI_HANDLE_MASK) != 0)
48 idx &= NI_HANDLE_MASK;
50 return ptl_interfaces[idx];
59 LASSERT (MAX_NIS <= (NI_HANDLE_MASK + 1));
61 for (i = 0; i < MAX_NIS; i++)
62 ptl_interfaces[i] = NULL;
67 void ptl_ni_fini(void)
71 for (i = 0; i < MAX_NIS; i++) {
72 nal_t *nal = ptl_interfaces[i];
77 nal->shutdown(nal, i);
82 DECLARE_MUTEX(ptl_ni_init_mutex);
84 static void ptl_ni_init_mutex_enter (void)
86 down (&ptl_ni_init_mutex);
89 static void ptl_ni_init_mutex_exit (void)
91 up (&ptl_ni_init_mutex);
95 static void ptl_ni_init_mutex_enter (void)
99 static void ptl_ni_init_mutex_exit (void)
105 int PtlNIInit(ptl_interface_t interface, ptl_pt_index_t ptl_size,
106 ptl_ac_index_t acl_size, ptl_pid_t requested_pid,
107 ptl_handle_ni_t * handle)
115 ptl_ni_init_mutex_enter ();
117 nal = interface(ptl_num_interfaces, ptl_size, acl_size, requested_pid);
120 ptl_ni_init_mutex_exit ();
121 return PTL_NAL_FAILED;
124 for (i = 0; i < ptl_num_interfaces; i++) {
125 if (ptl_interfaces[i] == nal) {
127 handle->nal_idx = (NI_HANDLE_MAGIC & ~NI_HANDLE_MASK) | i;
128 fprintf(stderr, "Returning existing NAL (%d)\n", i);
129 ptl_ni_init_mutex_exit ();
135 if (ptl_num_interfaces >= MAX_NIS) {
137 nal->shutdown (nal, ptl_num_interfaces);
138 ptl_ni_init_mutex_exit ();
142 handle->nal_idx = (NI_HANDLE_MAGIC & ~NI_HANDLE_MASK) | ptl_num_interfaces;
143 ptl_interfaces[ptl_num_interfaces++] = nal;
148 ptl_ni_init_mutex_exit ();
153 int PtlNIFini(ptl_handle_ni_t ni)
162 ptl_ni_init_mutex_enter ();
164 nal = ptl_hndl2nal (&ni);
166 ptl_ni_init_mutex_exit ();
167 return PTL_INV_HANDLE;
170 idx = ni.nal_idx & NI_HANDLE_MASK;
173 if (nal->refct > 0) {
174 ptl_ni_init_mutex_exit ();
183 rc = nal->shutdown(nal, idx);
185 ptl_interfaces[idx] = NULL;
186 ptl_num_interfaces--;
188 ptl_ni_init_mutex_exit ();
192 int PtlNIHandle(ptl_handle_any_t handle_in, ptl_handle_ni_t * ni_out)