blob: c5cec606377d18eddd4ab3df4069548f011c015c [file] [log] [blame]
Dean Nelsonb0d82bd2005-03-23 19:46:00 -07001/*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
Dean Nelson45d9ca42008-04-22 14:46:56 -05006 * Copyright (c) 2004-2008 Silicon Graphics, Inc. All Rights Reserved.
Dean Nelsonb0d82bd2005-03-23 19:46:00 -07007 */
8
Dean Nelsonb0d82bd2005-03-23 19:46:00 -07009/*
10 * Cross Partition (XP) base.
11 *
12 * XP provides a base from which its users can interact
13 * with XPC, yet not be dependent on XPC.
14 *
15 */
16
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070017#include <linux/kernel.h>
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070018#include <linux/module.h>
Dean Nelsonbc63d382008-07-29 22:34:04 -070019#include <linux/device.h>
Dean Nelson45d9ca42008-04-22 14:46:56 -050020#include "xp.h"
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070021
Dean Nelsonbc63d382008-07-29 22:34:04 -070022/* define the XP debug device structures to be used with dev_dbg() et al */
Dean Nelson2c2b94f2008-04-22 14:50:17 -050023
Dean Nelsonbc63d382008-07-29 22:34:04 -070024struct device_driver xp_dbg_name = {
25 .name = "xp"
26};
27
28struct device xp_dbg_subname = {
29 .bus_id = {0}, /* set to "" */
30 .driver = &xp_dbg_name
31};
32
33struct device *xp = &xp_dbg_subname;
34
35/* max #of partitions possible */
36short xp_max_npartitions;
37EXPORT_SYMBOL_GPL(xp_max_npartitions);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070038
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070039/*
40 * xpc_registrations[] keeps track of xpc_connect()'s done by the kernel-level
41 * users of XPC.
42 */
Dean Nelsonbc63d382008-07-29 22:34:04 -070043struct xpc_registration xpc_registrations[XPC_MAX_NCHANNELS];
Dean Nelson2c2b94f2008-04-22 14:50:17 -050044EXPORT_SYMBOL_GPL(xpc_registrations);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070045
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070046/*
47 * Initialize the XPC interface to indicate that XPC isn't loaded.
48 */
Dean Nelson65c17b82008-05-12 14:02:02 -070049static enum xp_retval
Dean Nelson4a3ad2d2008-04-22 14:48:01 -050050xpc_notloaded(void)
51{
Dean Nelson65c17b82008-05-12 14:02:02 -070052 return xpNotLoaded;
Dean Nelson4a3ad2d2008-04-22 14:48:01 -050053}
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070054
55struct xpc_interface xpc_interface = {
Dean Nelson4a3ad2d2008-04-22 14:48:01 -050056 (void (*)(int))xpc_notloaded,
57 (void (*)(int))xpc_notloaded,
Dean Nelson64d032b2008-05-12 14:02:03 -070058 (enum xp_retval(*)(short, int, u32, void **))xpc_notloaded,
59 (enum xp_retval(*)(short, int, void *))xpc_notloaded,
60 (enum xp_retval(*)(short, int, void *, xpc_notify_func, void *))
Dean Nelson4a3ad2d2008-04-22 14:48:01 -050061 xpc_notloaded,
Dean Nelson64d032b2008-05-12 14:02:03 -070062 (void (*)(short, int, void *))xpc_notloaded,
63 (enum xp_retval(*)(short, void *))xpc_notloaded
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070064};
Dean Nelson2c2b94f2008-04-22 14:50:17 -050065EXPORT_SYMBOL_GPL(xpc_interface);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070066
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070067/*
68 * XPC calls this when it (the XPC module) has been loaded.
69 */
70void
Dean Nelson4a3ad2d2008-04-22 14:48:01 -050071xpc_set_interface(void (*connect) (int),
72 void (*disconnect) (int),
Dean Nelson64d032b2008-05-12 14:02:03 -070073 enum xp_retval (*allocate) (short, int, u32, void **),
74 enum xp_retval (*send) (short, int, void *),
75 enum xp_retval (*send_notify) (short, int, void *,
Dean Nelson4a3ad2d2008-04-22 14:48:01 -050076 xpc_notify_func, void *),
Dean Nelson64d032b2008-05-12 14:02:03 -070077 void (*received) (short, int, void *),
78 enum xp_retval (*partid_to_nasids) (short, void *))
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070079{
80 xpc_interface.connect = connect;
81 xpc_interface.disconnect = disconnect;
82 xpc_interface.allocate = allocate;
83 xpc_interface.send = send;
84 xpc_interface.send_notify = send_notify;
85 xpc_interface.received = received;
86 xpc_interface.partid_to_nasids = partid_to_nasids;
87}
Dean Nelson2c2b94f2008-04-22 14:50:17 -050088EXPORT_SYMBOL_GPL(xpc_set_interface);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070089
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070090/*
91 * XPC calls this when it (the XPC module) is being unloaded.
92 */
93void
94xpc_clear_interface(void)
95{
Dean Nelson4a3ad2d2008-04-22 14:48:01 -050096 xpc_interface.connect = (void (*)(int))xpc_notloaded;
97 xpc_interface.disconnect = (void (*)(int))xpc_notloaded;
Dean Nelson64d032b2008-05-12 14:02:03 -070098 xpc_interface.allocate = (enum xp_retval(*)(short, int, u32,
Dean Nelson4a3ad2d2008-04-22 14:48:01 -050099 void **))xpc_notloaded;
Dean Nelson64d032b2008-05-12 14:02:03 -0700100 xpc_interface.send = (enum xp_retval(*)(short, int, void *))
Dean Nelson4a3ad2d2008-04-22 14:48:01 -0500101 xpc_notloaded;
Dean Nelson64d032b2008-05-12 14:02:03 -0700102 xpc_interface.send_notify = (enum xp_retval(*)(short, int, void *,
Dean Nelson4a3ad2d2008-04-22 14:48:01 -0500103 xpc_notify_func,
104 void *))xpc_notloaded;
Dean Nelson64d032b2008-05-12 14:02:03 -0700105 xpc_interface.received = (void (*)(short, int, void *))
Dean Nelson4a3ad2d2008-04-22 14:48:01 -0500106 xpc_notloaded;
Dean Nelson64d032b2008-05-12 14:02:03 -0700107 xpc_interface.partid_to_nasids = (enum xp_retval(*)(short, void *))
Dean Nelson4a3ad2d2008-04-22 14:48:01 -0500108 xpc_notloaded;
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700109}
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500110EXPORT_SYMBOL_GPL(xpc_clear_interface);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700111
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700112/*
113 * Register for automatic establishment of a channel connection whenever
114 * a partition comes up.
115 *
116 * Arguments:
117 *
118 * ch_number - channel # to register for connection.
119 * func - function to call for asynchronous notification of channel
120 * state changes (i.e., connection, disconnection, error) and
121 * the arrival of incoming messages.
122 * key - pointer to optional user-defined value that gets passed back
123 * to the user on any callouts made to func.
124 * payload_size - size in bytes of the XPC message's payload area which
125 * contains a user-defined message. The user should make
126 * this large enough to hold their largest message.
127 * nentries - max #of XPC message entries a message queue can contain.
128 * The actual number, which is determined when a connection
129 * is established and may be less then requested, will be
Dean Nelson65c17b82008-05-12 14:02:02 -0700130 * passed to the user via the xpConnected callout.
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700131 * assigned_limit - max number of kthreads allowed to be processing
132 * messages (per connection) at any given instant.
133 * idle_limit - max number of kthreads allowed to be idle at any given
134 * instant.
135 */
Dean Nelson65c17b82008-05-12 14:02:02 -0700136enum xp_retval
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700137xpc_connect(int ch_number, xpc_channel_func func, void *key, u16 payload_size,
Dean Nelson4a3ad2d2008-04-22 14:48:01 -0500138 u16 nentries, u32 assigned_limit, u32 idle_limit)
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700139{
140 struct xpc_registration *registration;
141
Dean Nelsonbc63d382008-07-29 22:34:04 -0700142 DBUG_ON(ch_number < 0 || ch_number >= XPC_MAX_NCHANNELS);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700143 DBUG_ON(payload_size == 0 || nentries == 0);
144 DBUG_ON(func == NULL);
145 DBUG_ON(assigned_limit == 0 || idle_limit > assigned_limit);
146
147 registration = &xpc_registrations[ch_number];
148
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500149 if (mutex_lock_interruptible(&registration->mutex) != 0)
Dean Nelson65c17b82008-05-12 14:02:02 -0700150 return xpInterrupted;
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700151
152 /* if XPC_CHANNEL_REGISTERED(ch_number) */
153 if (registration->func != NULL) {
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500154 mutex_unlock(&registration->mutex);
Dean Nelson65c17b82008-05-12 14:02:02 -0700155 return xpAlreadyRegistered;
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700156 }
157
158 /* register the channel for connection */
159 registration->msg_size = XPC_MSG_SIZE(payload_size);
160 registration->nentries = nentries;
161 registration->assigned_limit = assigned_limit;
162 registration->idle_limit = idle_limit;
163 registration->key = key;
164 registration->func = func;
165
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500166 mutex_unlock(&registration->mutex);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700167
168 xpc_interface.connect(ch_number);
169
Dean Nelson65c17b82008-05-12 14:02:02 -0700170 return xpSuccess;
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700171}
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500172EXPORT_SYMBOL_GPL(xpc_connect);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700173
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700174/*
175 * Remove the registration for automatic connection of the specified channel
176 * when a partition comes up.
177 *
178 * Before returning this xpc_disconnect() will wait for all connections on the
179 * specified channel have been closed/torndown. So the caller can be assured
180 * that they will not be receiving any more callouts from XPC to their
181 * function registered via xpc_connect().
182 *
183 * Arguments:
184 *
185 * ch_number - channel # to unregister.
186 */
187void
188xpc_disconnect(int ch_number)
189{
190 struct xpc_registration *registration;
191
Dean Nelsonbc63d382008-07-29 22:34:04 -0700192 DBUG_ON(ch_number < 0 || ch_number >= XPC_MAX_NCHANNELS);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700193
194 registration = &xpc_registrations[ch_number];
195
196 /*
197 * We've decided not to make this a down_interruptible(), since we
198 * figured XPC's users will just turn around and call xpc_disconnect()
199 * again anyways, so we might as well wait, if need be.
200 */
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500201 mutex_lock(&registration->mutex);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700202
203 /* if !XPC_CHANNEL_REGISTERED(ch_number) */
204 if (registration->func == NULL) {
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500205 mutex_unlock(&registration->mutex);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700206 return;
207 }
208
209 /* remove the connection registration for the specified channel */
210 registration->func = NULL;
211 registration->key = NULL;
212 registration->nentries = 0;
213 registration->msg_size = 0;
214 registration->assigned_limit = 0;
215 registration->idle_limit = 0;
216
217 xpc_interface.disconnect(ch_number);
218
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500219 mutex_unlock(&registration->mutex);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700220
221 return;
222}
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500223EXPORT_SYMBOL_GPL(xpc_disconnect);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700224
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700225int __init
226xp_init(void)
227{
Dean Nelsonbc63d382008-07-29 22:34:04 -0700228 enum xp_retval ret;
229 int ch_number;
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700230
Dean Nelsonbc63d382008-07-29 22:34:04 -0700231 if (is_shub())
232 ret = xp_init_sn2();
233 else if (is_uv())
234 ret = xp_init_uv();
235 else
236 ret = xpUnsupported;
237
238 if (ret != xpSuccess)
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700239 return -ENODEV;
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700240
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500241 /* initialize the connection registration mutex */
Dean Nelsonbc63d382008-07-29 22:34:04 -0700242 for (ch_number = 0; ch_number < XPC_MAX_NCHANNELS; ch_number++)
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500243 mutex_init(&xpc_registrations[ch_number].mutex);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700244
245 return 0;
246}
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700247
Dean Nelson4a3ad2d2008-04-22 14:48:01 -0500248module_init(xp_init);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700249
250void __exit
251xp_exit(void)
252{
Dean Nelsonbc63d382008-07-29 22:34:04 -0700253 if (is_shub())
254 xp_exit_sn2();
255 else if (is_uv())
256 xp_exit_uv();
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700257}
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700258
Dean Nelson4a3ad2d2008-04-22 14:48:01 -0500259module_exit(xp_exit);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700260
261MODULE_AUTHOR("Silicon Graphics, Inc.");
262MODULE_DESCRIPTION("Cross Partition (XP) base");
263MODULE_LICENSE("GPL");