[IA64] run some drivers/misc/sgi-xp through scripts/Lindent
[linux-2.6] / drivers / misc / sgi-xp / xp_main.c
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/kernel.h>
18 #include <linux/interrupt.h>
19 #include <linux/module.h>
20 #include <linux/mutex.h>
21 #include <asm/sn/intr.h>
22 #include <asm/sn/sn_sal.h>
23 #include "xp.h"
24
25 /*
26  * Target of nofault PIO read.
27  */
28 u64 xp_nofault_PIOR_target;
29
30 /*
31  * xpc_registrations[] keeps track of xpc_connect()'s done by the kernel-level
32  * users of XPC.
33  */
34 struct xpc_registration xpc_registrations[XPC_NCHANNELS];
35
36 /*
37  * Initialize the XPC interface to indicate that XPC isn't loaded.
38  */
39 static enum xpc_retval
40 xpc_notloaded(void)
41 {
42         return xpcNotLoaded;
43 }
44
45 struct xpc_interface xpc_interface = {
46         (void (*)(int))xpc_notloaded,
47         (void (*)(int))xpc_notloaded,
48         (enum xpc_retval(*)(partid_t, int, u32, void **))xpc_notloaded,
49         (enum xpc_retval(*)(partid_t, int, void *))xpc_notloaded,
50         (enum xpc_retval(*)(partid_t, int, void *, xpc_notify_func, void *))
51             xpc_notloaded,
52         (void (*)(partid_t, int, void *))xpc_notloaded,
53         (enum xpc_retval(*)(partid_t, void *))xpc_notloaded
54 };
55
56 /*
57  * XPC calls this when it (the XPC module) has been loaded.
58  */
59 void
60 xpc_set_interface(void (*connect) (int),
61                   void (*disconnect) (int),
62                   enum xpc_retval (*allocate) (partid_t, int, u32, void **),
63                   enum xpc_retval (*send) (partid_t, int, void *),
64                   enum xpc_retval (*send_notify) (partid_t, int, void *,
65                                                   xpc_notify_func, void *),
66                   void (*received) (partid_t, int, void *),
67                   enum xpc_retval (*partid_to_nasids) (partid_t, void *))
68 {
69         xpc_interface.connect = connect;
70         xpc_interface.disconnect = disconnect;
71         xpc_interface.allocate = allocate;
72         xpc_interface.send = send;
73         xpc_interface.send_notify = send_notify;
74         xpc_interface.received = received;
75         xpc_interface.partid_to_nasids = partid_to_nasids;
76 }
77
78 /*
79  * XPC calls this when it (the XPC module) is being unloaded.
80  */
81 void
82 xpc_clear_interface(void)
83 {
84         xpc_interface.connect = (void (*)(int))xpc_notloaded;
85         xpc_interface.disconnect = (void (*)(int))xpc_notloaded;
86         xpc_interface.allocate = (enum xpc_retval(*)(partid_t, int, u32,
87                                                      void **))xpc_notloaded;
88         xpc_interface.send = (enum xpc_retval(*)(partid_t, int, void *))
89             xpc_notloaded;
90         xpc_interface.send_notify = (enum xpc_retval(*)(partid_t, int, void *,
91                                                         xpc_notify_func,
92                                                         void *))xpc_notloaded;
93         xpc_interface.received = (void (*)(partid_t, int, void *))
94             xpc_notloaded;
95         xpc_interface.partid_to_nasids = (enum xpc_retval(*)(partid_t, void *))
96             xpc_notloaded;
97 }
98
99 /*
100  * Register for automatic establishment of a channel connection whenever
101  * a partition comes up.
102  *
103  * Arguments:
104  *
105  *      ch_number - channel # to register for connection.
106  *      func - function to call for asynchronous notification of channel
107  *             state changes (i.e., connection, disconnection, error) and
108  *             the arrival of incoming messages.
109  *      key - pointer to optional user-defined value that gets passed back
110  *            to the user on any callouts made to func.
111  *      payload_size - size in bytes of the XPC message's payload area which
112  *                     contains a user-defined message. The user should make
113  *                     this large enough to hold their largest message.
114  *      nentries - max #of XPC message entries a message queue can contain.
115  *                 The actual number, which is determined when a connection
116  *                 is established and may be less then requested, will be
117  *                 passed to the user via the xpcConnected callout.
118  *      assigned_limit - max number of kthreads allowed to be processing
119  *                       messages (per connection) at any given instant.
120  *      idle_limit - max number of kthreads allowed to be idle at any given
121  *                   instant.
122  */
123 enum xpc_retval
124 xpc_connect(int ch_number, xpc_channel_func func, void *key, u16 payload_size,
125             u16 nentries, u32 assigned_limit, u32 idle_limit)
126 {
127         struct xpc_registration *registration;
128
129         DBUG_ON(ch_number < 0 || ch_number >= XPC_NCHANNELS);
130         DBUG_ON(payload_size == 0 || nentries == 0);
131         DBUG_ON(func == NULL);
132         DBUG_ON(assigned_limit == 0 || idle_limit > assigned_limit);
133
134         registration = &xpc_registrations[ch_number];
135
136         if (mutex_lock_interruptible(&registration->mutex) != 0) {
137                 return xpcInterrupted;
138         }
139
140         /* if XPC_CHANNEL_REGISTERED(ch_number) */
141         if (registration->func != NULL) {
142                 mutex_unlock(&registration->mutex);
143                 return xpcAlreadyRegistered;
144         }
145
146         /* register the channel for connection */
147         registration->msg_size = XPC_MSG_SIZE(payload_size);
148         registration->nentries = nentries;
149         registration->assigned_limit = assigned_limit;
150         registration->idle_limit = idle_limit;
151         registration->key = key;
152         registration->func = func;
153
154         mutex_unlock(&registration->mutex);
155
156         xpc_interface.connect(ch_number);
157
158         return xpcSuccess;
159 }
160
161 /*
162  * Remove the registration for automatic connection of the specified channel
163  * when a partition comes up.
164  *
165  * Before returning this xpc_disconnect() will wait for all connections on the
166  * specified channel have been closed/torndown. So the caller can be assured
167  * that they will not be receiving any more callouts from XPC to their
168  * function registered via xpc_connect().
169  *
170  * Arguments:
171  *
172  *      ch_number - channel # to unregister.
173  */
174 void
175 xpc_disconnect(int ch_number)
176 {
177         struct xpc_registration *registration;
178
179         DBUG_ON(ch_number < 0 || ch_number >= XPC_NCHANNELS);
180
181         registration = &xpc_registrations[ch_number];
182
183         /*
184          * We've decided not to make this a down_interruptible(), since we
185          * figured XPC's users will just turn around and call xpc_disconnect()
186          * again anyways, so we might as well wait, if need be.
187          */
188         mutex_lock(&registration->mutex);
189
190         /* if !XPC_CHANNEL_REGISTERED(ch_number) */
191         if (registration->func == NULL) {
192                 mutex_unlock(&registration->mutex);
193                 return;
194         }
195
196         /* remove the connection registration for the specified channel */
197         registration->func = NULL;
198         registration->key = NULL;
199         registration->nentries = 0;
200         registration->msg_size = 0;
201         registration->assigned_limit = 0;
202         registration->idle_limit = 0;
203
204         xpc_interface.disconnect(ch_number);
205
206         mutex_unlock(&registration->mutex);
207
208         return;
209 }
210
211 int __init
212 xp_init(void)
213 {
214         int ret, ch_number;
215         u64 func_addr = *(u64 *)xp_nofault_PIOR;
216         u64 err_func_addr = *(u64 *)xp_error_PIOR;
217
218         if (!ia64_platform_is("sn2")) {
219                 return -ENODEV;
220         }
221
222         /*
223          * Register a nofault code region which performs a cross-partition
224          * PIO read. If the PIO read times out, the MCA handler will consume
225          * the error and return to a kernel-provided instruction to indicate
226          * an error. This PIO read exists because it is guaranteed to timeout
227          * if the destination is down (AMO operations do not timeout on at
228          * least some CPUs on Shubs <= v1.2, which unfortunately we have to
229          * work around).
230          */
231         if ((ret = sn_register_nofault_code(func_addr, err_func_addr,
232                                             err_func_addr, 1, 1)) != 0) {
233                 printk(KERN_ERR "XP: can't register nofault code, error=%d\n",
234                        ret);
235         }
236         /*
237          * Setup the nofault PIO read target. (There is no special reason why
238          * SH_IPI_ACCESS was selected.)
239          */
240         if (is_shub2()) {
241                 xp_nofault_PIOR_target = SH2_IPI_ACCESS0;
242         } else {
243                 xp_nofault_PIOR_target = SH1_IPI_ACCESS;
244         }
245
246         /* initialize the connection registration mutex */
247         for (ch_number = 0; ch_number < XPC_NCHANNELS; ch_number++) {
248                 mutex_init(&xpc_registrations[ch_number].mutex);
249         }
250
251         return 0;
252 }
253
254 module_init(xp_init);
255
256 void __exit
257 xp_exit(void)
258 {
259         u64 func_addr = *(u64 *)xp_nofault_PIOR;
260         u64 err_func_addr = *(u64 *)xp_error_PIOR;
261
262         /* unregister the PIO read nofault code region */
263         (void)sn_register_nofault_code(func_addr, err_func_addr,
264                                        err_func_addr, 1, 0);
265 }
266
267 module_exit(xp_exit);
268
269 MODULE_AUTHOR("Silicon Graphics, Inc.");
270 MODULE_DESCRIPTION("Cross Partition (XP) base");
271 MODULE_LICENSE("GPL");
272
273 EXPORT_SYMBOL(xp_nofault_PIOR);
274 EXPORT_SYMBOL(xp_nofault_PIOR_target);
275 EXPORT_SYMBOL(xpc_registrations);
276 EXPORT_SYMBOL(xpc_interface);
277 EXPORT_SYMBOL(xpc_clear_interface);
278 EXPORT_SYMBOL(xpc_set_interface);
279 EXPORT_SYMBOL(xpc_connect);
280 EXPORT_SYMBOL(xpc_disconnect);