blob: f86ad3af26b7e63fae36eebdae85e8c5a9fddca9 [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 Nelsona812dcc2008-07-29 22:34:16 -070044unsigned long (*xp_pa) (void *addr);
45EXPORT_SYMBOL_GPL(xp_pa);
46
47enum xp_retval (*xp_remote_memcpy) (unsigned long dst_gpa,
48 const unsigned long src_gpa, size_t len);
Dean Nelson908787d2008-07-29 22:34:05 -070049EXPORT_SYMBOL_GPL(xp_remote_memcpy);
50
Dean Nelson261f3b42008-07-29 22:34:16 -070051int (*xp_cpu_to_nasid) (int cpuid);
52EXPORT_SYMBOL_GPL(xp_cpu_to_nasid);
53
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070054/*
55 * xpc_registrations[] keeps track of xpc_connect()'s done by the kernel-level
56 * users of XPC.
57 */
Dean Nelsonbc63d382008-07-29 22:34:04 -070058struct xpc_registration xpc_registrations[XPC_MAX_NCHANNELS];
Dean Nelson2c2b94f2008-04-22 14:50:17 -050059EXPORT_SYMBOL_GPL(xpc_registrations);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070060
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070061/*
62 * Initialize the XPC interface to indicate that XPC isn't loaded.
63 */
Dean Nelson65c17b82008-05-12 14:02:02 -070064static enum xp_retval
Dean Nelson4a3ad2d2008-04-22 14:48:01 -050065xpc_notloaded(void)
66{
Dean Nelson65c17b82008-05-12 14:02:02 -070067 return xpNotLoaded;
Dean Nelson4a3ad2d2008-04-22 14:48:01 -050068}
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070069
70struct xpc_interface xpc_interface = {
Dean Nelson4a3ad2d2008-04-22 14:48:01 -050071 (void (*)(int))xpc_notloaded,
72 (void (*)(int))xpc_notloaded,
Dean Nelson97bf1aa2008-07-29 22:34:08 -070073 (enum xp_retval(*)(short, int, u32, void *, u16))xpc_notloaded,
74 (enum xp_retval(*)(short, int, u32, void *, u16, xpc_notify_func,
75 void *))xpc_notloaded,
Dean Nelson64d032b2008-05-12 14:02:03 -070076 (void (*)(short, int, void *))xpc_notloaded,
77 (enum xp_retval(*)(short, void *))xpc_notloaded
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070078};
Dean Nelson2c2b94f2008-04-22 14:50:17 -050079EXPORT_SYMBOL_GPL(xpc_interface);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070080
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070081/*
82 * XPC calls this when it (the XPC module) has been loaded.
83 */
84void
Dean Nelson4a3ad2d2008-04-22 14:48:01 -050085xpc_set_interface(void (*connect) (int),
86 void (*disconnect) (int),
Dean Nelson97bf1aa2008-07-29 22:34:08 -070087 enum xp_retval (*send) (short, int, u32, void *, u16),
88 enum xp_retval (*send_notify) (short, int, u32, void *, u16,
Dean Nelson4a3ad2d2008-04-22 14:48:01 -050089 xpc_notify_func, void *),
Dean Nelson64d032b2008-05-12 14:02:03 -070090 void (*received) (short, int, void *),
91 enum xp_retval (*partid_to_nasids) (short, void *))
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070092{
93 xpc_interface.connect = connect;
94 xpc_interface.disconnect = disconnect;
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070095 xpc_interface.send = send;
96 xpc_interface.send_notify = send_notify;
97 xpc_interface.received = received;
98 xpc_interface.partid_to_nasids = partid_to_nasids;
99}
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500100EXPORT_SYMBOL_GPL(xpc_set_interface);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700101
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700102/*
103 * XPC calls this when it (the XPC module) is being unloaded.
104 */
105void
106xpc_clear_interface(void)
107{
Dean Nelson4a3ad2d2008-04-22 14:48:01 -0500108 xpc_interface.connect = (void (*)(int))xpc_notloaded;
109 xpc_interface.disconnect = (void (*)(int))xpc_notloaded;
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700110 xpc_interface.send = (enum xp_retval(*)(short, int, u32, void *, u16))
Dean Nelson4a3ad2d2008-04-22 14:48:01 -0500111 xpc_notloaded;
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700112 xpc_interface.send_notify = (enum xp_retval(*)(short, int, u32, void *,
113 u16, xpc_notify_func,
114 void *))xpc_notloaded;
Dean Nelson64d032b2008-05-12 14:02:03 -0700115 xpc_interface.received = (void (*)(short, int, void *))
Dean Nelson4a3ad2d2008-04-22 14:48:01 -0500116 xpc_notloaded;
Dean Nelson64d032b2008-05-12 14:02:03 -0700117 xpc_interface.partid_to_nasids = (enum xp_retval(*)(short, void *))
Dean Nelson4a3ad2d2008-04-22 14:48:01 -0500118 xpc_notloaded;
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700119}
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500120EXPORT_SYMBOL_GPL(xpc_clear_interface);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700121
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700122/*
123 * Register for automatic establishment of a channel connection whenever
124 * a partition comes up.
125 *
126 * Arguments:
127 *
128 * ch_number - channel # to register for connection.
129 * func - function to call for asynchronous notification of channel
130 * state changes (i.e., connection, disconnection, error) and
131 * the arrival of incoming messages.
132 * key - pointer to optional user-defined value that gets passed back
133 * to the user on any callouts made to func.
134 * payload_size - size in bytes of the XPC message's payload area which
135 * contains a user-defined message. The user should make
136 * this large enough to hold their largest message.
137 * nentries - max #of XPC message entries a message queue can contain.
138 * The actual number, which is determined when a connection
139 * is established and may be less then requested, will be
Dean Nelson65c17b82008-05-12 14:02:02 -0700140 * passed to the user via the xpConnected callout.
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700141 * assigned_limit - max number of kthreads allowed to be processing
142 * messages (per connection) at any given instant.
143 * idle_limit - max number of kthreads allowed to be idle at any given
144 * instant.
145 */
Dean Nelson65c17b82008-05-12 14:02:02 -0700146enum xp_retval
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700147xpc_connect(int ch_number, xpc_channel_func func, void *key, u16 payload_size,
Dean Nelson4a3ad2d2008-04-22 14:48:01 -0500148 u16 nentries, u32 assigned_limit, u32 idle_limit)
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700149{
150 struct xpc_registration *registration;
151
Dean Nelsonbc63d382008-07-29 22:34:04 -0700152 DBUG_ON(ch_number < 0 || ch_number >= XPC_MAX_NCHANNELS);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700153 DBUG_ON(payload_size == 0 || nentries == 0);
154 DBUG_ON(func == NULL);
155 DBUG_ON(assigned_limit == 0 || idle_limit > assigned_limit);
156
157 registration = &xpc_registrations[ch_number];
158
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500159 if (mutex_lock_interruptible(&registration->mutex) != 0)
Dean Nelson65c17b82008-05-12 14:02:02 -0700160 return xpInterrupted;
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700161
162 /* if XPC_CHANNEL_REGISTERED(ch_number) */
163 if (registration->func != NULL) {
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500164 mutex_unlock(&registration->mutex);
Dean Nelson65c17b82008-05-12 14:02:02 -0700165 return xpAlreadyRegistered;
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700166 }
167
168 /* register the channel for connection */
169 registration->msg_size = XPC_MSG_SIZE(payload_size);
170 registration->nentries = nentries;
171 registration->assigned_limit = assigned_limit;
172 registration->idle_limit = idle_limit;
173 registration->key = key;
174 registration->func = func;
175
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500176 mutex_unlock(&registration->mutex);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700177
178 xpc_interface.connect(ch_number);
179
Dean Nelson65c17b82008-05-12 14:02:02 -0700180 return xpSuccess;
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700181}
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500182EXPORT_SYMBOL_GPL(xpc_connect);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700183
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700184/*
185 * Remove the registration for automatic connection of the specified channel
186 * when a partition comes up.
187 *
188 * Before returning this xpc_disconnect() will wait for all connections on the
189 * specified channel have been closed/torndown. So the caller can be assured
190 * that they will not be receiving any more callouts from XPC to their
191 * function registered via xpc_connect().
192 *
193 * Arguments:
194 *
195 * ch_number - channel # to unregister.
196 */
197void
198xpc_disconnect(int ch_number)
199{
200 struct xpc_registration *registration;
201
Dean Nelsonbc63d382008-07-29 22:34:04 -0700202 DBUG_ON(ch_number < 0 || ch_number >= XPC_MAX_NCHANNELS);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700203
204 registration = &xpc_registrations[ch_number];
205
206 /*
207 * We've decided not to make this a down_interruptible(), since we
208 * figured XPC's users will just turn around and call xpc_disconnect()
209 * again anyways, so we might as well wait, if need be.
210 */
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500211 mutex_lock(&registration->mutex);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700212
213 /* if !XPC_CHANNEL_REGISTERED(ch_number) */
214 if (registration->func == NULL) {
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500215 mutex_unlock(&registration->mutex);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700216 return;
217 }
218
219 /* remove the connection registration for the specified channel */
220 registration->func = NULL;
221 registration->key = NULL;
222 registration->nentries = 0;
223 registration->msg_size = 0;
224 registration->assigned_limit = 0;
225 registration->idle_limit = 0;
226
227 xpc_interface.disconnect(ch_number);
228
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500229 mutex_unlock(&registration->mutex);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700230
231 return;
232}
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500233EXPORT_SYMBOL_GPL(xpc_disconnect);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700234
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700235int __init
236xp_init(void)
237{
Dean Nelsonbc63d382008-07-29 22:34:04 -0700238 enum xp_retval ret;
239 int ch_number;
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700240
Dean Nelsonbc63d382008-07-29 22:34:04 -0700241 if (is_shub())
242 ret = xp_init_sn2();
243 else if (is_uv())
244 ret = xp_init_uv();
245 else
246 ret = xpUnsupported;
247
248 if (ret != xpSuccess)
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700249 return -ENODEV;
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700250
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500251 /* initialize the connection registration mutex */
Dean Nelsonbc63d382008-07-29 22:34:04 -0700252 for (ch_number = 0; ch_number < XPC_MAX_NCHANNELS; ch_number++)
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500253 mutex_init(&xpc_registrations[ch_number].mutex);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700254
255 return 0;
256}
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700257
Dean Nelson4a3ad2d2008-04-22 14:48:01 -0500258module_init(xp_init);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700259
260void __exit
261xp_exit(void)
262{
Dean Nelsonbc63d382008-07-29 22:34:04 -0700263 if (is_shub())
264 xp_exit_sn2();
265 else if (is_uv())
266 xp_exit_uv();
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700267}
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700268
Dean Nelson4a3ad2d2008-04-22 14:48:01 -0500269module_exit(xp_exit);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700270
271MODULE_AUTHOR("Silicon Graphics, Inc.");
272MODULE_DESCRIPTION("Cross Partition (XP) base");
273MODULE_LICENSE("GPL");