blob: 9c0ce2f15ff65771d67993cec007a6fc3de1acd2 [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 Nelson908787d2008-07-29 22:34:05 -070039enum xp_retval (*xp_remote_memcpy) (void *dst, const void *src, size_t len);
40EXPORT_SYMBOL_GPL(xp_remote_memcpy);
41
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070042/*
43 * xpc_registrations[] keeps track of xpc_connect()'s done by the kernel-level
44 * users of XPC.
45 */
Dean Nelsonbc63d382008-07-29 22:34:04 -070046struct xpc_registration xpc_registrations[XPC_MAX_NCHANNELS];
Dean Nelson2c2b94f2008-04-22 14:50:17 -050047EXPORT_SYMBOL_GPL(xpc_registrations);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070048
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070049/*
50 * Initialize the XPC interface to indicate that XPC isn't loaded.
51 */
Dean Nelson65c17b82008-05-12 14:02:02 -070052static enum xp_retval
Dean Nelson4a3ad2d2008-04-22 14:48:01 -050053xpc_notloaded(void)
54{
Dean Nelson65c17b82008-05-12 14:02:02 -070055 return xpNotLoaded;
Dean Nelson4a3ad2d2008-04-22 14:48:01 -050056}
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070057
58struct xpc_interface xpc_interface = {
Dean Nelson4a3ad2d2008-04-22 14:48:01 -050059 (void (*)(int))xpc_notloaded,
60 (void (*)(int))xpc_notloaded,
Dean Nelson97bf1aa2008-07-29 22:34:08 -070061 (enum xp_retval(*)(short, int, u32, void *, u16))xpc_notloaded,
62 (enum xp_retval(*)(short, int, u32, void *, u16, xpc_notify_func,
63 void *))xpc_notloaded,
Dean Nelson64d032b2008-05-12 14:02:03 -070064 (void (*)(short, int, void *))xpc_notloaded,
65 (enum xp_retval(*)(short, void *))xpc_notloaded
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070066};
Dean Nelson2c2b94f2008-04-22 14:50:17 -050067EXPORT_SYMBOL_GPL(xpc_interface);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070068
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070069/*
70 * XPC calls this when it (the XPC module) has been loaded.
71 */
72void
Dean Nelson4a3ad2d2008-04-22 14:48:01 -050073xpc_set_interface(void (*connect) (int),
74 void (*disconnect) (int),
Dean Nelson97bf1aa2008-07-29 22:34:08 -070075 enum xp_retval (*send) (short, int, u32, void *, u16),
76 enum xp_retval (*send_notify) (short, int, u32, void *, u16,
Dean Nelson4a3ad2d2008-04-22 14:48:01 -050077 xpc_notify_func, void *),
Dean Nelson64d032b2008-05-12 14:02:03 -070078 void (*received) (short, int, void *),
79 enum xp_retval (*partid_to_nasids) (short, void *))
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070080{
81 xpc_interface.connect = connect;
82 xpc_interface.disconnect = disconnect;
Dean Nelsonb0d82bd2005-03-23 19:46:00 -070083 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 Nelson97bf1aa2008-07-29 22:34:08 -070098 xpc_interface.send = (enum xp_retval(*)(short, int, u32, void *, u16))
Dean Nelson4a3ad2d2008-04-22 14:48:01 -050099 xpc_notloaded;
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700100 xpc_interface.send_notify = (enum xp_retval(*)(short, int, u32, void *,
101 u16, xpc_notify_func,
102 void *))xpc_notloaded;
Dean Nelson64d032b2008-05-12 14:02:03 -0700103 xpc_interface.received = (void (*)(short, int, void *))
Dean Nelson4a3ad2d2008-04-22 14:48:01 -0500104 xpc_notloaded;
Dean Nelson64d032b2008-05-12 14:02:03 -0700105 xpc_interface.partid_to_nasids = (enum xp_retval(*)(short, void *))
Dean Nelson4a3ad2d2008-04-22 14:48:01 -0500106 xpc_notloaded;
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700107}
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500108EXPORT_SYMBOL_GPL(xpc_clear_interface);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700109
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700110/*
111 * Register for automatic establishment of a channel connection whenever
112 * a partition comes up.
113 *
114 * Arguments:
115 *
116 * ch_number - channel # to register for connection.
117 * func - function to call for asynchronous notification of channel
118 * state changes (i.e., connection, disconnection, error) and
119 * the arrival of incoming messages.
120 * key - pointer to optional user-defined value that gets passed back
121 * to the user on any callouts made to func.
122 * payload_size - size in bytes of the XPC message's payload area which
123 * contains a user-defined message. The user should make
124 * this large enough to hold their largest message.
125 * nentries - max #of XPC message entries a message queue can contain.
126 * The actual number, which is determined when a connection
127 * is established and may be less then requested, will be
Dean Nelson65c17b82008-05-12 14:02:02 -0700128 * passed to the user via the xpConnected callout.
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700129 * assigned_limit - max number of kthreads allowed to be processing
130 * messages (per connection) at any given instant.
131 * idle_limit - max number of kthreads allowed to be idle at any given
132 * instant.
133 */
Dean Nelson65c17b82008-05-12 14:02:02 -0700134enum xp_retval
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700135xpc_connect(int ch_number, xpc_channel_func func, void *key, u16 payload_size,
Dean Nelson4a3ad2d2008-04-22 14:48:01 -0500136 u16 nentries, u32 assigned_limit, u32 idle_limit)
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700137{
138 struct xpc_registration *registration;
139
Dean Nelsonbc63d382008-07-29 22:34:04 -0700140 DBUG_ON(ch_number < 0 || ch_number >= XPC_MAX_NCHANNELS);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700141 DBUG_ON(payload_size == 0 || nentries == 0);
142 DBUG_ON(func == NULL);
143 DBUG_ON(assigned_limit == 0 || idle_limit > assigned_limit);
144
145 registration = &xpc_registrations[ch_number];
146
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500147 if (mutex_lock_interruptible(&registration->mutex) != 0)
Dean Nelson65c17b82008-05-12 14:02:02 -0700148 return xpInterrupted;
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700149
150 /* if XPC_CHANNEL_REGISTERED(ch_number) */
151 if (registration->func != NULL) {
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500152 mutex_unlock(&registration->mutex);
Dean Nelson65c17b82008-05-12 14:02:02 -0700153 return xpAlreadyRegistered;
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700154 }
155
156 /* register the channel for connection */
157 registration->msg_size = XPC_MSG_SIZE(payload_size);
158 registration->nentries = nentries;
159 registration->assigned_limit = assigned_limit;
160 registration->idle_limit = idle_limit;
161 registration->key = key;
162 registration->func = func;
163
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500164 mutex_unlock(&registration->mutex);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700165
166 xpc_interface.connect(ch_number);
167
Dean Nelson65c17b82008-05-12 14:02:02 -0700168 return xpSuccess;
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700169}
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500170EXPORT_SYMBOL_GPL(xpc_connect);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700171
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700172/*
173 * Remove the registration for automatic connection of the specified channel
174 * when a partition comes up.
175 *
176 * Before returning this xpc_disconnect() will wait for all connections on the
177 * specified channel have been closed/torndown. So the caller can be assured
178 * that they will not be receiving any more callouts from XPC to their
179 * function registered via xpc_connect().
180 *
181 * Arguments:
182 *
183 * ch_number - channel # to unregister.
184 */
185void
186xpc_disconnect(int ch_number)
187{
188 struct xpc_registration *registration;
189
Dean Nelsonbc63d382008-07-29 22:34:04 -0700190 DBUG_ON(ch_number < 0 || ch_number >= XPC_MAX_NCHANNELS);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700191
192 registration = &xpc_registrations[ch_number];
193
194 /*
195 * We've decided not to make this a down_interruptible(), since we
196 * figured XPC's users will just turn around and call xpc_disconnect()
197 * again anyways, so we might as well wait, if need be.
198 */
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500199 mutex_lock(&registration->mutex);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700200
201 /* if !XPC_CHANNEL_REGISTERED(ch_number) */
202 if (registration->func == NULL) {
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500203 mutex_unlock(&registration->mutex);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700204 return;
205 }
206
207 /* remove the connection registration for the specified channel */
208 registration->func = NULL;
209 registration->key = NULL;
210 registration->nentries = 0;
211 registration->msg_size = 0;
212 registration->assigned_limit = 0;
213 registration->idle_limit = 0;
214
215 xpc_interface.disconnect(ch_number);
216
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500217 mutex_unlock(&registration->mutex);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700218
219 return;
220}
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500221EXPORT_SYMBOL_GPL(xpc_disconnect);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700222
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700223int __init
224xp_init(void)
225{
Dean Nelsonbc63d382008-07-29 22:34:04 -0700226 enum xp_retval ret;
227 int ch_number;
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700228
Dean Nelsonbc63d382008-07-29 22:34:04 -0700229 if (is_shub())
230 ret = xp_init_sn2();
231 else if (is_uv())
232 ret = xp_init_uv();
233 else
234 ret = xpUnsupported;
235
236 if (ret != xpSuccess)
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700237 return -ENODEV;
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700238
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500239 /* initialize the connection registration mutex */
Dean Nelsonbc63d382008-07-29 22:34:04 -0700240 for (ch_number = 0; ch_number < XPC_MAX_NCHANNELS; ch_number++)
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500241 mutex_init(&xpc_registrations[ch_number].mutex);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700242
243 return 0;
244}
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700245
Dean Nelson4a3ad2d2008-04-22 14:48:01 -0500246module_init(xp_init);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700247
248void __exit
249xp_exit(void)
250{
Dean Nelsonbc63d382008-07-29 22:34:04 -0700251 if (is_shub())
252 xp_exit_sn2();
253 else if (is_uv())
254 xp_exit_uv();
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700255}
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700256
Dean Nelson4a3ad2d2008-04-22 14:48:01 -0500257module_exit(xp_exit);
Dean Nelsonb0d82bd2005-03-23 19:46:00 -0700258
259MODULE_AUTHOR("Silicon Graphics, Inc.");
260MODULE_DESCRIPTION("Cross Partition (XP) base");
261MODULE_LICENSE("GPL");