1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (c) 2001-2003 Cluster File Systems, Inc.
6 * This file is part of Lustre, http://www.sf.net/projects/lustre/
8 * Lustre is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * Lustre is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Lustre; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #define DEBUG_SUBSYSTEM S_PORTALS
23 #include <portals/api-support.h>
27 /* Put some magic in the NI handle so uninitialised/zeroed handles are easy
29 #define NI_HANDLE_MAGIC 0xebc0de00
30 #define NI_HANDLE_MASK 0x000000ff
32 static struct nal_t *ptl_nal_table[NAL_MAX_NR + 1];
35 struct semaphore ptl_mutex;
37 static void ptl_mutex_enter (void)
39 mutex_down (&ptl_mutex);
42 static void ptl_mutex_exit (void)
44 mutex_up (&ptl_mutex);
47 static void ptl_mutex_enter (void)
51 static void ptl_mutex_exit (void)
56 nal_t *ptl_hndl2nal(ptl_handle_any_t *handle)
58 unsigned int idx = handle->nal_idx;
60 /* XXX we really rely on the caller NOT racing with interface
61 * setup/teardown. That ensures her NI handle can't get
62 * invalidated out from under her (or worse, swapped for a
63 * completely different interface!) */
67 if (((idx ^ NI_HANDLE_MAGIC) & ~NI_HANDLE_MASK) != 0)
70 idx &= NI_HANDLE_MASK;
72 if (idx > NAL_MAX_NR ||
73 ptl_nal_table[idx] == NULL ||
74 ptl_nal_table[idx]->nal_refct == 0)
77 return ptl_nal_table[idx];
80 int ptl_register_nal (ptl_interface_t interface, nal_t *nal)
86 if (interface < 0 || interface > NAL_MAX_NR)
87 rc = PTL_IFACE_INVALID;
88 else if (ptl_nal_table[interface] != NULL)
92 ptl_nal_table[interface] = nal;
93 LASSERT(nal->nal_refct == 0);
100 void ptl_unregister_nal (ptl_interface_t interface)
102 LASSERT(interface >= 0 && interface <= NAL_MAX_NR);
103 LASSERT(ptl_nal_table[interface] != NULL);
104 LASSERT(ptl_nal_table[interface]->nal_refct == 0);
108 ptl_nal_table[interface] = NULL;
113 int PtlInit(int *max_interfaces)
115 LASSERT(!strcmp(ptl_err_str[PTL_MAX_ERRNO], "PTL_MAX_ERRNO"));
117 /* If this assertion fails, we need more bits in NI_HANDLE_MASK and
118 * to shift NI_HANDLE_MAGIC left appropriately */
119 LASSERT (NAL_MAX_NR < (NI_HANDLE_MASK + 1));
121 if (max_interfaces != NULL)
122 *max_interfaces = NAL_MAX_NR + 1;
127 /* NULL pointers, clear flags */
128 memset(ptl_nal_table, 0, sizeof(ptl_nal_table));
130 /* Kernel NALs register themselves when their module loads,
131 * and unregister themselves when their module is unloaded.
132 * Userspace NALs, are plugged in explicitly here... */
134 extern nal_t procapi_nal;
136 /* XXX pretend it's socknal to keep liblustre happy... */
137 ptl_nal_table[SOCKNAL] = &procapi_nal;
138 LASSERT (procapi_nal.nal_refct == 0);
157 for (i = 0; i <= NAL_MAX_NR; i++) {
159 nal = ptl_nal_table[i];
163 if (nal->nal_refct != 0) {
164 CWARN("NAL %x has outstanding refcount %d\n",
166 nal->nal_ni_fini(nal);
169 ptl_nal_table[i] = NULL;
178 int PtlNIInit(ptl_interface_t interface, ptl_pid_t requested_pid,
179 ptl_ni_limits_t *desired_limits, ptl_ni_limits_t *actual_limits,
180 ptl_handle_ni_t *handle)
191 if (interface == PTL_IFACE_DEFAULT) {
192 for (i = 0; i <= NAL_MAX_NR; i++)
193 if (ptl_nal_table[i] != NULL) {
197 /* NB if no interfaces are registered, 'interface' will
198 * fail the valid test below */
202 interface > NAL_MAX_NR ||
203 ptl_nal_table[interface] == NULL) {
204 GOTO(out, rc = PTL_IFACE_INVALID);
207 nal = ptl_nal_table[interface];
208 nal->nal_handle.nal_idx = (NI_HANDLE_MAGIC & ~NI_HANDLE_MASK) | interface;
209 nal->nal_handle.cookie = 0;
211 CDEBUG(D_OTHER, "Starting up NAL (%x) refs %d\n", interface, nal->nal_refct);
212 rc = nal->nal_ni_init(nal, requested_pid, desired_limits, actual_limits);
215 CERROR("Error %d starting up NAL %x, refs %d\n", rc,
216 interface, nal->nal_refct);
220 if (nal->nal_refct != 0) {
221 /* Caller gets to know if this was the first ref or not */
226 *handle = nal->nal_handle;
234 int PtlNIFini(ptl_handle_ni_t ni)
244 nal = ptl_hndl2nal (&ni);
247 return PTL_HANDLE_INVALID;
250 idx = ni.nal_idx & NI_HANDLE_MASK;
252 LASSERT(nal->nal_refct > 0);
256 /* nal_refct == 0 tells nal->shutdown to really shut down */
257 nal->nal_ni_fini(nal);