blob: c34b23fe498f72ff9c4c1a4b5909fc52b41245ab [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/module.h>
Dean Nelsonbc63d382008-07-29 22:34:04 -070018#include <linux/device.h>
Dean Nelson45d9ca42008-04-22 14:46:56 -050019#include "xp.h"
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070020
Dean Nelsonbc63d382008-07-29 22:34:04 -070021/* define the XP debug device structures to be used with dev_dbg() et al */
Dean Nelson2c2b94f2008-04-22 14:50:17 -050022
Dean Nelsonbc63d382008-07-29 22:34:04 -070023struct device_driver xp_dbg_name = {
24 .name = "xp"
25};
26
27struct device xp_dbg_subname = {
28 .bus_id = {0}, /* set to "" */
29 .driver = &xp_dbg_name
30};
31
32struct device *xp = &xp_dbg_subname;
33
34/* max #of partitions possible */
35short xp_max_npartitions;
36EXPORT_SYMBOL_GPL(xp_max_npartitions);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070037
Dean Nelson261f3b42008-07-29 22:34:16 -070038short xp_partition_id;
39EXPORT_SYMBOL_GPL(xp_partition_id);
40
41u8 xp_region_size;
42EXPORT_SYMBOL_GPL(xp_region_size);
43
Dean Nelson908787d2008-07-29 22:34:05 -070044enum xp_retval (*xp_remote_memcpy) (void *dst, const void *src, size_t len);
45EXPORT_SYMBOL_GPL(xp_remote_memcpy);
46
Dean Nelson261f3b42008-07-29 22:34:16 -070047int (*xp_cpu_to_nasid) (int cpuid);
48EXPORT_SYMBOL_GPL(xp_cpu_to_nasid);
49
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070050/*
51 * xpc_registrations[] keeps track of xpc_connect()'s done by the kernel-level
52 * users of XPC.
53 */
Dean Nelsonbc63d382008-07-29 22:34:04 -070054struct xpc_registration xpc_registrations[XPC_MAX_NCHANNELS];
Dean Nelson2c2b94f2008-04-22 14:50:17 -050055EXPORT_SYMBOL_GPL(xpc_registrations);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070056
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070057/*
58 * Initialize the XPC interface to indicate that XPC isn't loaded.
59 */
Dean Nelson65c17b82008-05-12 14:02:02 -070060static enum xp_retval
Dean Nelson4a3ad2d2008-04-22 14:48:01 -050061xpc_notloaded(void)
62{
Dean Nelson65c17b82008-05-12 14:02:02 -070063 return xpNotLoaded;
Dean Nelson4a3ad2d2008-04-22 14:48:01 -050064}
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070065
66struct xpc_interface xpc_interface = {
Dean Nelson4a3ad2d2008-04-22 14:48:01 -050067 (void (*)(int))xpc_notloaded,
68 (void (*)(int))xpc_notloaded,
Dean Nelson97bf1aa2008-07-29 22:34:08 -070069 (enum xp_retval(*)(short, int, u32, void *, u16))xpc_notloaded,
70 (enum xp_retval(*)(short, int, u32, void *, u16, xpc_notify_func,
71 void *))xpc_notloaded,
Dean Nelson64d032b2008-05-12 14:02:03 -070072 (void (*)(short, int, void *))xpc_notloaded,
73 (enum xp_retval(*)(short, void *))xpc_notloaded
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070074};
Dean Nelson2c2b94f2008-04-22 14:50:17 -050075EXPORT_SYMBOL_GPL(xpc_interface);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070076
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070077/*
78 * XPC calls this when it (the XPC module) has been loaded.
79 */
80void
Dean Nelson4a3ad2d2008-04-22 14:48:01 -050081xpc_set_interface(void (*connect) (int),
82 void (*disconnect) (int),
Dean Nelson97bf1aa2008-07-29 22:34:08 -070083 enum xp_retval (*send) (short, int, u32, void *, u16),
84 enum xp_retval (*send_notify) (short, int, u32, void *, u16,
Dean Nelson4a3ad2d2008-04-22 14:48:01 -050085 xpc_notify_func, void *),
Dean Nelson64d032b2008-05-12 14:02:03 -070086 void (*received) (short, int, void *),
87 enum xp_retval (*partid_to_nasids) (short, void *))
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070088{
89 xpc_interface.connect = connect;
90 xpc_interface.disconnect = disconnect;
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070091 xpc_interface.send = send;
92 xpc_interface.send_notify = send_notify;
93 xpc_interface.received = received;
94 xpc_interface.partid_to_nasids = partid_to_nasids;
95}
Dean Nelson2c2b94f2008-04-22 14:50:17 -050096EXPORT_SYMBOL_GPL(xpc_set_interface);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070097
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070098/*
99 * XPC calls this when it (the XPC module) is being unloaded.
100 */
101void
102xpc_clear_interface(void)
103{
Dean Nelson4a3ad2d2008-04-22 14:48:01 -0500104 xpc_interface.connect = (void (*)(int))xpc_notloaded;
105 xpc_interface.disconnect = (void (*)(int))xpc_notloaded;
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700106 xpc_interface.send = (enum xp_retval(*)(short, int, u32, void *, u16))
Dean Nelson4a3ad2d2008-04-22 14:48:01 -0500107 xpc_notloaded;
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700108 xpc_interface.send_notify = (enum xp_retval(*)(short, int, u32, void *,
109 u16, xpc_notify_func,
110 void *))xpc_notloaded;
Dean Nelson64d032b2008-05-12 14:02:03 -0700111 xpc_interface.received = (void (*)(short, int, void *))
Dean Nelson4a3ad2d2008-04-22 14:48:01 -0500112 xpc_notloaded;
Dean Nelson64d032b2008-05-12 14:02:03 -0700113 xpc_interface.partid_to_nasids = (enum xp_retval(*)(short, void *))
Dean Nelson4a3ad2d2008-04-22 14:48:01 -0500114 xpc_notloaded;
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700115}
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500116EXPORT_SYMBOL_GPL(xpc_clear_interface);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700117
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700118/*
119 * Register for automatic establishment of a channel connection whenever
120 * a partition comes up.
121 *
122 * Arguments:
123 *
124 * ch_number - channel # to register for connection.
125 * func - function to call for asynchronous notification of channel
126 * state changes (i.e., connection, disconnection, error) and
127 * the arrival of incoming messages.
128 * key - pointer to optional user-defined value that gets passed back
129 * to the user on any callouts made to func.
130 * payload_size - size in bytes of the XPC message's payload area which
131 * contains a user-defined message. The user should make
132 * this large enough to hold their largest message.
133 * nentries - max #of XPC message entries a message queue can contain.
134 * The actual number, which is determined when a connection
135 * is established and may be less then requested, will be
Dean Nelson65c17b82008-05-12 14:02:02 -0700136 * passed to the user via the xpConnected callout.
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700137 * assigned_limit - max number of kthreads allowed to be processing
138 * messages (per connection) at any given instant.
139 * idle_limit - max number of kthreads allowed to be idle at any given
140 * instant.
141 */
Dean Nelson65c17b82008-05-12 14:02:02 -0700142enum xp_retval
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700143xpc_connect(int ch_number, xpc_channel_func func, void *key, u16 payload_size,
Dean Nelson4a3ad2d2008-04-22 14:48:01 -0500144 u16 nentries, u32 assigned_limit, u32 idle_limit)
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700145{
146 struct xpc_registration *registration;
147
Dean Nelsonbc63d382008-07-29 22:34:04 -0700148 DBUG_ON(ch_number < 0 || ch_number >= XPC_MAX_NCHANNELS);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700149 DBUG_ON(payload_size == 0 || nentries == 0);
150 DBUG_ON(func == NULL);
151 DBUG_ON(assigned_limit == 0 || idle_limit > assigned_limit);
152
153 registration = &xpc_registrations[ch_number];
154
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500155 if (mutex_lock_interruptible(&registration->mutex) != 0)
Dean Nelson65c17b82008-05-12 14:02:02 -0700156 return xpInterrupted;
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700157
158 /* if XPC_CHANNEL_REGISTERED(ch_number) */
159 if (registration->func != NULL) {
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500160 mutex_unlock(&registration->mutex);
Dean Nelson65c17b82008-05-12 14:02:02 -0700161 return xpAlreadyRegistered;
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700162 }
163
164 /* register the channel for connection */
165 registration->msg_size = XPC_MSG_SIZE(payload_size);
166 registration->nentries = nentries;
167 registration->assigned_limit = assigned_limit;
168 registration->idle_limit = idle_limit;
169 registration->key = key;
170 registration->func = func;
171
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500172 mutex_unlock(&registration->mutex);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700173
174 xpc_interface.connect(ch_number);
175
Dean Nelson65c17b82008-05-12 14:02:02 -0700176 return xpSuccess;
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700177}
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500178EXPORT_SYMBOL_GPL(xpc_connect);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700179
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700180/*
181 * Remove the registration for automatic connection of the specified channel
182 * when a partition comes up.
183 *
184 * Before returning this xpc_disconnect() will wait for all connections on the
185 * specified channel have been closed/torndown. So the caller can be assured
186 * that they will not be receiving any more callouts from XPC to their
187 * function registered via xpc_connect().
188 *
189 * Arguments:
190 *
191 * ch_number - channel # to unregister.
192 */
193void
194xpc_disconnect(int ch_number)
195{
196 struct xpc_registration *registration;
197
Dean Nelsonbc63d382008-07-29 22:34:04 -0700198 DBUG_ON(ch_number < 0 || ch_number >= XPC_MAX_NCHANNELS);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700199
200 registration = &xpc_registrations[ch_number];
201
202 /*
203 * We've decided not to make this a down_interruptible(), since we
204 * figured XPC's users will just turn around and call xpc_disconnect()
205 * again anyways, so we might as well wait, if need be.
206 */
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500207 mutex_lock(&registration->mutex);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700208
209 /* if !XPC_CHANNEL_REGISTERED(ch_number) */
210 if (registration->func == NULL) {
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500211 mutex_unlock(&registration->mutex);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700212 return;
213 }
214
215 /* remove the connection registration for the specified channel */
216 registration->func = NULL;
217 registration->key = NULL;
218 registration->nentries = 0;
219 registration->msg_size = 0;
220 registration->assigned_limit = 0;
221 registration->idle_limit = 0;
222
223 xpc_interface.disconnect(ch_number);
224
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500225 mutex_unlock(&registration->mutex);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700226
227 return;
228}
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500229EXPORT_SYMBOL_GPL(xpc_disconnect);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700230
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700231int __init
232xp_init(void)
233{
Dean Nelsonbc63d382008-07-29 22:34:04 -0700234 enum xp_retval ret;
235 int ch_number;
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700236
Dean Nelsonbc63d382008-07-29 22:34:04 -0700237 if (is_shub())
238 ret = xp_init_sn2();
239 else if (is_uv())
240 ret = xp_init_uv();
241 else
242 ret = xpUnsupported;
243
244 if (ret != xpSuccess)
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700245 return -ENODEV;
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700246
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500247 /* initialize the connection registration mutex */
Dean Nelsonbc63d382008-07-29 22:34:04 -0700248 for (ch_number = 0; ch_number < XPC_MAX_NCHANNELS; ch_number++)
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500249 mutex_init(&xpc_registrations[ch_number].mutex);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700250
251 return 0;
252}
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700253
Dean Nelson4a3ad2d2008-04-22 14:48:01 -0500254module_init(xp_init);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700255
256void __exit
257xp_exit(void)
258{
Dean Nelsonbc63d382008-07-29 22:34:04 -0700259 if (is_shub())
260 xp_exit_sn2();
261 else if (is_uv())
262 xp_exit_uv();
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700263}
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700264
Dean Nelson4a3ad2d2008-04-22 14:48:01 -0500265module_exit(xp_exit);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700266
267MODULE_AUTHOR("Silicon Graphics, Inc.");
268MODULE_DESCRIPTION("Cross Partition (XP) base");
269MODULE_LICENSE("GPL");