xref: /linux/drivers/misc/sgi-xp/xp_main.c (revision cf206bffbb7542df54043fad9898113172af99d8)
1 /*
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  *
6  * Copyright (c) 2004-2008 Silicon Graphics, Inc.  All Rights Reserved.
7  */
8 
9 /*
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 
17 #include <linux/module.h>
18 #include <linux/device.h>
19 #include "xp.h"
20 
21 /* define the XP debug device structures to be used with dev_dbg() et al */
22 
23 struct device_driver xp_dbg_name = {
24 	.name = "xp"
25 };
26 
27 struct device xp_dbg_subname = {
28 	.bus_id = {0},		/* set to "" */
29 	.driver = &xp_dbg_name
30 };
31 
32 struct device *xp = &xp_dbg_subname;
33 
34 /* max #of partitions possible */
35 short xp_max_npartitions;
36 EXPORT_SYMBOL_GPL(xp_max_npartitions);
37 
38 short xp_partition_id;
39 EXPORT_SYMBOL_GPL(xp_partition_id);
40 
41 u8 xp_region_size;
42 EXPORT_SYMBOL_GPL(xp_region_size);
43 
44 unsigned long (*xp_pa) (void *addr);
45 EXPORT_SYMBOL_GPL(xp_pa);
46 
47 enum xp_retval (*xp_remote_memcpy) (unsigned long dst_gpa,
48 				    const unsigned long src_gpa, size_t len);
49 EXPORT_SYMBOL_GPL(xp_remote_memcpy);
50 
51 int (*xp_cpu_to_nasid) (int cpuid);
52 EXPORT_SYMBOL_GPL(xp_cpu_to_nasid);
53 
54 /*
55  * xpc_registrations[] keeps track of xpc_connect()'s done by the kernel-level
56  * users of XPC.
57  */
58 struct xpc_registration xpc_registrations[XPC_MAX_NCHANNELS];
59 EXPORT_SYMBOL_GPL(xpc_registrations);
60 
61 /*
62  * Initialize the XPC interface to indicate that XPC isn't loaded.
63  */
64 static enum xp_retval
65 xpc_notloaded(void)
66 {
67 	return xpNotLoaded;
68 }
69 
70 struct xpc_interface xpc_interface = {
71 	(void (*)(int))xpc_notloaded,
72 	(void (*)(int))xpc_notloaded,
73 	(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,
76 	(void (*)(short, int, void *))xpc_notloaded,
77 	(enum xp_retval(*)(short, void *))xpc_notloaded
78 };
79 EXPORT_SYMBOL_GPL(xpc_interface);
80 
81 /*
82  * XPC calls this when it (the XPC module) has been loaded.
83  */
84 void
85 xpc_set_interface(void (*connect) (int),
86 		  void (*disconnect) (int),
87 		  enum xp_retval (*send) (short, int, u32, void *, u16),
88 		  enum xp_retval (*send_notify) (short, int, u32, void *, u16,
89 						  xpc_notify_func, void *),
90 		  void (*received) (short, int, void *),
91 		  enum xp_retval (*partid_to_nasids) (short, void *))
92 {
93 	xpc_interface.connect = connect;
94 	xpc_interface.disconnect = disconnect;
95 	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 }
100 EXPORT_SYMBOL_GPL(xpc_set_interface);
101 
102 /*
103  * XPC calls this when it (the XPC module) is being unloaded.
104  */
105 void
106 xpc_clear_interface(void)
107 {
108 	xpc_interface.connect = (void (*)(int))xpc_notloaded;
109 	xpc_interface.disconnect = (void (*)(int))xpc_notloaded;
110 	xpc_interface.send = (enum xp_retval(*)(short, int, u32, void *, u16))
111 	    xpc_notloaded;
112 	xpc_interface.send_notify = (enum xp_retval(*)(short, int, u32, void *,
113 						       u16, xpc_notify_func,
114 						       void *))xpc_notloaded;
115 	xpc_interface.received = (void (*)(short, int, void *))
116 	    xpc_notloaded;
117 	xpc_interface.partid_to_nasids = (enum xp_retval(*)(short, void *))
118 	    xpc_notloaded;
119 }
120 EXPORT_SYMBOL_GPL(xpc_clear_interface);
121 
122 /*
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
140  *		   passed to the user via the xpConnected callout.
141  *	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  */
146 enum xp_retval
147 xpc_connect(int ch_number, xpc_channel_func func, void *key, u16 payload_size,
148 	    u16 nentries, u32 assigned_limit, u32 idle_limit)
149 {
150 	struct xpc_registration *registration;
151 
152 	DBUG_ON(ch_number < 0 || ch_number >= XPC_MAX_NCHANNELS);
153 	DBUG_ON(payload_size == 0 || nentries == 0);
154 	DBUG_ON(func == NULL);
155 	DBUG_ON(assigned_limit == 0 || idle_limit > assigned_limit);
156 
157 	if (XPC_MSG_SIZE(payload_size) > XPC_MSG_MAX_SIZE)
158 		return xpPayloadTooBig;
159 
160 	registration = &xpc_registrations[ch_number];
161 
162 	if (mutex_lock_interruptible(&registration->mutex) != 0)
163 		return xpInterrupted;
164 
165 	/* if XPC_CHANNEL_REGISTERED(ch_number) */
166 	if (registration->func != NULL) {
167 		mutex_unlock(&registration->mutex);
168 		return xpAlreadyRegistered;
169 	}
170 
171 	/* register the channel for connection */
172 	registration->entry_size = XPC_MSG_SIZE(payload_size);
173 	registration->nentries = nentries;
174 	registration->assigned_limit = assigned_limit;
175 	registration->idle_limit = idle_limit;
176 	registration->key = key;
177 	registration->func = func;
178 
179 	mutex_unlock(&registration->mutex);
180 
181 	xpc_interface.connect(ch_number);
182 
183 	return xpSuccess;
184 }
185 EXPORT_SYMBOL_GPL(xpc_connect);
186 
187 /*
188  * Remove the registration for automatic connection of the specified channel
189  * when a partition comes up.
190  *
191  * Before returning this xpc_disconnect() will wait for all connections on the
192  * specified channel have been closed/torndown. So the caller can be assured
193  * that they will not be receiving any more callouts from XPC to their
194  * function registered via xpc_connect().
195  *
196  * Arguments:
197  *
198  *	ch_number - channel # to unregister.
199  */
200 void
201 xpc_disconnect(int ch_number)
202 {
203 	struct xpc_registration *registration;
204 
205 	DBUG_ON(ch_number < 0 || ch_number >= XPC_MAX_NCHANNELS);
206 
207 	registration = &xpc_registrations[ch_number];
208 
209 	/*
210 	 * We've decided not to make this a down_interruptible(), since we
211 	 * figured XPC's users will just turn around and call xpc_disconnect()
212 	 * again anyways, so we might as well wait, if need be.
213 	 */
214 	mutex_lock(&registration->mutex);
215 
216 	/* if !XPC_CHANNEL_REGISTERED(ch_number) */
217 	if (registration->func == NULL) {
218 		mutex_unlock(&registration->mutex);
219 		return;
220 	}
221 
222 	/* remove the connection registration for the specified channel */
223 	registration->func = NULL;
224 	registration->key = NULL;
225 	registration->nentries = 0;
226 	registration->entry_size = 0;
227 	registration->assigned_limit = 0;
228 	registration->idle_limit = 0;
229 
230 	xpc_interface.disconnect(ch_number);
231 
232 	mutex_unlock(&registration->mutex);
233 
234 	return;
235 }
236 EXPORT_SYMBOL_GPL(xpc_disconnect);
237 
238 int __init
239 xp_init(void)
240 {
241 	enum xp_retval ret;
242 	int ch_number;
243 
244 	if (is_shub())
245 		ret = xp_init_sn2();
246 	else if (is_uv())
247 		ret = xp_init_uv();
248 	else
249 		ret = xpUnsupported;
250 
251 	if (ret != xpSuccess)
252 		return -ENODEV;
253 
254 	/* initialize the connection registration mutex */
255 	for (ch_number = 0; ch_number < XPC_MAX_NCHANNELS; ch_number++)
256 		mutex_init(&xpc_registrations[ch_number].mutex);
257 
258 	return 0;
259 }
260 
261 module_init(xp_init);
262 
263 void __exit
264 xp_exit(void)
265 {
266 	if (is_shub())
267 		xp_exit_sn2();
268 	else if (is_uv())
269 		xp_exit_uv();
270 }
271 
272 module_exit(xp_exit);
273 
274 MODULE_AUTHOR("Silicon Graphics, Inc.");
275 MODULE_DESCRIPTION("Cross Partition (XP) base");
276 MODULE_LICENSE("GPL");
277