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>
30 /* Put some magic in the NI handle so uninitialised/zeroed handles are easy
32 #define NI_HANDLE_MAGIC 0xebc0de00
33 #define NI_HANDLE_MASK 0x000000ff
35 static struct nal_t *ptl_nal_table[NAL_MAX_NR];
38 DECLARE_MUTEX(ptl_mutex);
40 static void ptl_mutex_enter (void)
45 static void ptl_mutex_exit (void)
50 static void ptl_mutex_enter (void)
54 static void ptl_mutex_exit (void)
59 nal_t *ptl_hndl2nal(ptl_handle_any_t *handle)
61 unsigned int idx = handle->nal_idx;
63 /* XXX we really rely on the caller NOT racing with interface
64 * setup/teardown. That ensures her NI handle can't get
65 * invalidated out from under her (or worse, swapped for a
66 * completely different interface!) */
68 if (((idx ^ NI_HANDLE_MAGIC) & ~NI_HANDLE_MASK) != 0)
71 idx &= NI_HANDLE_MASK;
73 if (idx >= NAL_MAX_NR ||
74 ptl_nal_table[idx] == NULL ||
75 ptl_nal_table[idx]->nal_refct == 0)
78 return ptl_nal_table[idx];
81 int ptl_register_nal (ptl_interface_t interface, nal_t *nal)
87 if (interface < 0 || interface >= NAL_MAX_NR)
88 rc = PTL_IFACE_INVALID;
89 else if (ptl_nal_table[interface] != NULL)
93 ptl_nal_table[interface] = nal;
94 LASSERT(nal->nal_refct == 0);
101 void ptl_unregister_nal (ptl_interface_t interface)
103 LASSERT(interface >= 0 && interface < NAL_MAX_NR);
104 LASSERT(ptl_nal_table[interface] != NULL);
105 LASSERT(ptl_nal_table[interface]->nal_refct == 0);
109 ptl_nal_table[interface] = NULL;
114 int ptl_ni_init(void)
116 /* If this assertion fails, we need more bits in NI_HANDLE_MASK and
117 * to shift NI_HANDLE_MAGIC left appropriately */
118 LASSERT (NAL_MAX_NR <= (NI_HANDLE_MASK + 1));
123 /* NULL pointers, clear flags */
124 memset(ptl_nal_table, 0, sizeof(ptl_nal_table));
126 /* Kernel NALs register themselves when their module loads,
127 * and unregister themselves when their module is unloaded.
128 * Userspace NALs, are plugged in explicitly here... */
130 extern nal_t procapi_nal;
132 /* XXX pretend it's socknal to keep liblustre happy... */
133 ptl_nal_table[SOCKNAL] = &procapi_nal;
134 LASSERT (procapi_nal.nal_refct == 0);
145 void ptl_ni_fini(void)
153 for (i = 0; i < NAL_MAX_NR; i++) {
155 nal = ptl_nal_table[i];
159 if (nal->nal_refct != 0) {
160 CWARN("NAL %d has outstanding refcount %d\n",
165 ptl_nal_table[i] = NULL;
174 int PtlNIInit(ptl_interface_t interface, ptl_pid_t requested_pid,
175 ptl_ni_limits_t *desired_limits, ptl_ni_limits_t *actual_limits,
176 ptl_handle_ni_t *handle)
187 if (interface == PTL_IFACE_DEFAULT) {
188 for (i = 0; i < NAL_MAX_NR; i++)
189 if (ptl_nal_table[i] != NULL) {
193 /* NB if no interfaces are registered, 'interface' will
194 * fail the valid test below */
198 interface >= NAL_MAX_NR ||
199 ptl_nal_table[interface] == NULL) {
200 GOTO(out, rc = PTL_IFACE_INVALID);
203 nal = ptl_nal_table[interface];
205 CDEBUG(D_OTHER, "Starting up NAL (%d) refs %d\n", interface, nal->nal_refct);
206 rc = nal->startup(nal, requested_pid, desired_limits, actual_limits);
209 CERROR("Error %d starting up NAL %d, refs %d\n", rc,
210 interface, nal->nal_refct);
214 if (nal->nal_refct != 0) {
215 /* Caller gets to know if this was the first ref or not */
220 handle->nal_idx = (NI_HANDLE_MAGIC & ~NI_HANDLE_MASK) | interface;
227 int PtlNIFini(ptl_handle_ni_t ni)
237 nal = ptl_hndl2nal (&ni);
240 return PTL_HANDLE_INVALID;
243 idx = ni.nal_idx & NI_HANDLE_MASK;
245 LASSERT(nal->nal_refct > 0);
249 /* nal_refct == 0 tells nal->shutdown to really shut down */
256 int PtlNIHandle(ptl_handle_any_t handle_in, ptl_handle_ni_t * ni_out)