blob: 48b16136305e64d9c9170c33c0c1e858e8369e5a [file] [log] [blame]
Dean Nelson89eb8eb2005-03-23 19:50: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 Nelson89eb8eb2005-03-23 19:50:00 -07007 */
8
Dean Nelson89eb8eb2005-03-23 19:50:00 -07009/*
10 * Cross Partition Communication (XPC) channel support.
11 *
12 * This is the part of XPC that manages the channels and
13 * sends/receives messages across them to/from other partitions.
14 *
15 */
16
Dean Nelson89eb8eb2005-03-23 19:50:00 -070017#include <linux/kernel.h>
18#include <linux/init.h>
19#include <linux/sched.h>
20#include <linux/cache.h>
21#include <linux/interrupt.h>
Jes Sorensenf9e505a2006-01-17 12:52:21 -050022#include <linux/mutex.h>
23#include <linux/completion.h>
Dean Nelson89eb8eb2005-03-23 19:50:00 -070024#include <asm/sn/sn_sal.h>
Dean Nelson45d9ca42008-04-22 14:46:56 -050025#include "xpc.h"
Dean Nelson89eb8eb2005-03-23 19:50:00 -070026
Dean Nelson89eb8eb2005-03-23 19:50:00 -070027/*
Jes Sorensen7aa6ba42006-02-17 05:18:43 -050028 * Guarantee that the kzalloc'd memory is cacheline aligned.
29 */
Dean Nelsone17d4162008-07-29 22:34:06 -070030void *
Jes Sorensen7aa6ba42006-02-17 05:18:43 -050031xpc_kzalloc_cacheline_aligned(size_t size, gfp_t flags, void **base)
32{
33 /* see if kzalloc will give us cachline aligned memory by default */
34 *base = kzalloc(size, flags);
Dean Nelson2c2b94f2008-04-22 14:50:17 -050035 if (*base == NULL)
Jes Sorensen7aa6ba42006-02-17 05:18:43 -050036 return NULL;
Dean Nelson2c2b94f2008-04-22 14:50:17 -050037
38 if ((u64)*base == L1_CACHE_ALIGN((u64)*base))
Jes Sorensen7aa6ba42006-02-17 05:18:43 -050039 return *base;
Dean Nelson2c2b94f2008-04-22 14:50:17 -050040
Jes Sorensen7aa6ba42006-02-17 05:18:43 -050041 kfree(*base);
42
43 /* nope, we'll have to do it ourselves */
44 *base = kzalloc(size + L1_CACHE_BYTES, flags);
Dean Nelson2c2b94f2008-04-22 14:50:17 -050045 if (*base == NULL)
Jes Sorensen7aa6ba42006-02-17 05:18:43 -050046 return NULL;
Dean Nelson2c2b94f2008-04-22 14:50:17 -050047
Dean Nelson35190502008-04-22 14:48:55 -050048 return (void *)L1_CACHE_ALIGN((u64)*base);
Jes Sorensen7aa6ba42006-02-17 05:18:43 -050049}
50
Jes Sorensen7aa6ba42006-02-17 05:18:43 -050051/*
Dean Nelson89eb8eb2005-03-23 19:50:00 -070052 * Allocate the local message queue and the notify queue.
53 */
Dean Nelson65c17b82008-05-12 14:02:02 -070054static enum xp_retval
Dean Nelson89eb8eb2005-03-23 19:50:00 -070055xpc_allocate_local_msgqueue(struct xpc_channel *ch)
56{
57 unsigned long irq_flags;
58 int nentries;
59 size_t nbytes;
60
Dean Nelson89eb8eb2005-03-23 19:50:00 -070061 for (nentries = ch->local_nentries; nentries > 0; nentries--) {
62
63 nbytes = nentries * ch->msg_size;
Jes Sorensen7aa6ba42006-02-17 05:18:43 -050064 ch->local_msgqueue = xpc_kzalloc_cacheline_aligned(nbytes,
Dean Nelson35190502008-04-22 14:48:55 -050065 GFP_KERNEL,
Dean Nelson2c2b94f2008-04-22 14:50:17 -050066 &ch->local_msgqueue_base);
67 if (ch->local_msgqueue == NULL)
Dean Nelson89eb8eb2005-03-23 19:50:00 -070068 continue;
Dean Nelson89eb8eb2005-03-23 19:50:00 -070069
70 nbytes = nentries * sizeof(struct xpc_notify);
Jes Sorensen7aa6ba42006-02-17 05:18:43 -050071 ch->notify_queue = kzalloc(nbytes, GFP_KERNEL);
Dean Nelson89eb8eb2005-03-23 19:50:00 -070072 if (ch->notify_queue == NULL) {
73 kfree(ch->local_msgqueue_base);
74 ch->local_msgqueue = NULL;
75 continue;
76 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -070077
78 spin_lock_irqsave(&ch->lock, irq_flags);
79 if (nentries < ch->local_nentries) {
80 dev_dbg(xpc_chan, "nentries=%d local_nentries=%d, "
81 "partid=%d, channel=%d\n", nentries,
82 ch->local_nentries, ch->partid, ch->number);
83
84 ch->local_nentries = nentries;
85 }
86 spin_unlock_irqrestore(&ch->lock, irq_flags);
Dean Nelson65c17b82008-05-12 14:02:02 -070087 return xpSuccess;
Dean Nelson89eb8eb2005-03-23 19:50:00 -070088 }
89
90 dev_dbg(xpc_chan, "can't get memory for local message queue and notify "
91 "queue, partid=%d, channel=%d\n", ch->partid, ch->number);
Dean Nelson65c17b82008-05-12 14:02:02 -070092 return xpNoMemory;
Dean Nelson89eb8eb2005-03-23 19:50:00 -070093}
94
Dean Nelson89eb8eb2005-03-23 19:50:00 -070095/*
96 * Allocate the cached remote message queue.
97 */
Dean Nelson65c17b82008-05-12 14:02:02 -070098static enum xp_retval
Dean Nelson89eb8eb2005-03-23 19:50:00 -070099xpc_allocate_remote_msgqueue(struct xpc_channel *ch)
100{
101 unsigned long irq_flags;
102 int nentries;
103 size_t nbytes;
104
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700105 DBUG_ON(ch->remote_nentries <= 0);
106
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700107 for (nentries = ch->remote_nentries; nentries > 0; nentries--) {
108
109 nbytes = nentries * ch->msg_size;
Jes Sorensen7aa6ba42006-02-17 05:18:43 -0500110 ch->remote_msgqueue = xpc_kzalloc_cacheline_aligned(nbytes,
Dean Nelson35190502008-04-22 14:48:55 -0500111 GFP_KERNEL,
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500112 &ch->remote_msgqueue_base);
113 if (ch->remote_msgqueue == NULL)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700114 continue;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700115
116 spin_lock_irqsave(&ch->lock, irq_flags);
117 if (nentries < ch->remote_nentries) {
118 dev_dbg(xpc_chan, "nentries=%d remote_nentries=%d, "
119 "partid=%d, channel=%d\n", nentries,
120 ch->remote_nentries, ch->partid, ch->number);
121
122 ch->remote_nentries = nentries;
123 }
124 spin_unlock_irqrestore(&ch->lock, irq_flags);
Dean Nelson65c17b82008-05-12 14:02:02 -0700125 return xpSuccess;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700126 }
127
128 dev_dbg(xpc_chan, "can't get memory for cached remote message queue, "
129 "partid=%d, channel=%d\n", ch->partid, ch->number);
Dean Nelson65c17b82008-05-12 14:02:02 -0700130 return xpNoMemory;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700131}
132
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700133/*
134 * Allocate message queues and other stuff associated with a channel.
135 *
136 * Note: Assumes all of the channel sizes are filled in.
137 */
Dean Nelson65c17b82008-05-12 14:02:02 -0700138static enum xp_retval
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700139xpc_allocate_msgqueues(struct xpc_channel *ch)
140{
141 unsigned long irq_flags;
Dean Nelson65c17b82008-05-12 14:02:02 -0700142 enum xp_retval ret;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700143
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700144 DBUG_ON(ch->flags & XPC_C_SETUP);
145
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500146 ret = xpc_allocate_local_msgqueue(ch);
Dean Nelson65c17b82008-05-12 14:02:02 -0700147 if (ret != xpSuccess)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700148 return ret;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700149
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500150 ret = xpc_allocate_remote_msgqueue(ch);
Dean Nelson65c17b82008-05-12 14:02:02 -0700151 if (ret != xpSuccess) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700152 kfree(ch->local_msgqueue_base);
153 ch->local_msgqueue = NULL;
154 kfree(ch->notify_queue);
155 ch->notify_queue = NULL;
156 return ret;
157 }
158
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700159 spin_lock_irqsave(&ch->lock, irq_flags);
160 ch->flags |= XPC_C_SETUP;
161 spin_unlock_irqrestore(&ch->lock, irq_flags);
162
Dean Nelson65c17b82008-05-12 14:02:02 -0700163 return xpSuccess;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700164}
165
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700166/*
167 * Process a connect message from a remote partition.
168 *
169 * Note: xpc_process_connect() is expecting to be called with the
170 * spin_lock_irqsave held and will leave it locked upon return.
171 */
172static void
173xpc_process_connect(struct xpc_channel *ch, unsigned long *irq_flags)
174{
Dean Nelson65c17b82008-05-12 14:02:02 -0700175 enum xp_retval ret;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700176
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700177 DBUG_ON(!spin_is_locked(&ch->lock));
178
179 if (!(ch->flags & XPC_C_OPENREQUEST) ||
Dean Nelson35190502008-04-22 14:48:55 -0500180 !(ch->flags & XPC_C_ROPENREQUEST)) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700181 /* nothing more to do for now */
182 return;
183 }
184 DBUG_ON(!(ch->flags & XPC_C_CONNECTING));
185
186 if (!(ch->flags & XPC_C_SETUP)) {
187 spin_unlock_irqrestore(&ch->lock, *irq_flags);
188 ret = xpc_allocate_msgqueues(ch);
189 spin_lock_irqsave(&ch->lock, *irq_flags);
190
Dean Nelson65c17b82008-05-12 14:02:02 -0700191 if (ret != xpSuccess)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700192 XPC_DISCONNECT_CHANNEL(ch, ret, irq_flags);
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500193
194 if (ch->flags & (XPC_C_CONNECTED | XPC_C_DISCONNECTING))
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700195 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700196
197 DBUG_ON(!(ch->flags & XPC_C_SETUP));
198 DBUG_ON(ch->local_msgqueue == NULL);
199 DBUG_ON(ch->remote_msgqueue == NULL);
200 }
201
202 if (!(ch->flags & XPC_C_OPENREPLY)) {
203 ch->flags |= XPC_C_OPENREPLY;
Dean Nelsona47d5da2008-07-29 22:34:09 -0700204 xpc_send_channel_openreply(ch, irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700205 }
206
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500207 if (!(ch->flags & XPC_C_ROPENREPLY))
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700208 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700209
210 DBUG_ON(ch->remote_msgqueue_pa == 0);
211
212 ch->flags = (XPC_C_CONNECTED | XPC_C_SETUP); /* clear all else */
213
214 dev_info(xpc_chan, "channel %d to partition %d connected\n",
Dean Nelson35190502008-04-22 14:48:55 -0500215 ch->number, ch->partid);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700216
217 spin_unlock_irqrestore(&ch->lock, *irq_flags);
Dean Nelsona460ef82006-11-22 08:25:00 -0600218 xpc_create_kthreads(ch, 1, 0);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700219 spin_lock_irqsave(&ch->lock, *irq_flags);
220}
221
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700222/*
223 * Free up message queues and other stuff that were allocated for the specified
224 * channel.
225 *
226 * Note: ch->reason and ch->reason_line are left set for debugging purposes,
227 * they're cleared when XPC_C_DISCONNECTED is cleared.
228 */
229static void
230xpc_free_msgqueues(struct xpc_channel *ch)
231{
Dean Nelsona47d5da2008-07-29 22:34:09 -0700232 struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
233
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700234 DBUG_ON(!spin_is_locked(&ch->lock));
235 DBUG_ON(atomic_read(&ch->n_to_notify) != 0);
236
237 ch->remote_msgqueue_pa = 0;
238 ch->func = NULL;
239 ch->key = NULL;
240 ch->msg_size = 0;
241 ch->local_nentries = 0;
242 ch->remote_nentries = 0;
243 ch->kthreads_assigned_limit = 0;
244 ch->kthreads_idle_limit = 0;
245
Dean Nelsona47d5da2008-07-29 22:34:09 -0700246 ch_sn2->local_GP->get = 0;
247 ch_sn2->local_GP->put = 0;
248 ch_sn2->remote_GP.get = 0;
249 ch_sn2->remote_GP.put = 0;
250 ch_sn2->w_local_GP.get = 0;
251 ch_sn2->w_local_GP.put = 0;
252 ch_sn2->w_remote_GP.get = 0;
253 ch_sn2->w_remote_GP.put = 0;
254 ch_sn2->next_msg_to_pull = 0;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700255
256 if (ch->flags & XPC_C_SETUP) {
257 ch->flags &= ~XPC_C_SETUP;
258
259 dev_dbg(xpc_chan, "ch->flags=0x%x, partid=%d, channel=%d\n",
260 ch->flags, ch->partid, ch->number);
261
262 kfree(ch->local_msgqueue_base);
263 ch->local_msgqueue = NULL;
264 kfree(ch->remote_msgqueue_base);
265 ch->remote_msgqueue = NULL;
266 kfree(ch->notify_queue);
267 ch->notify_queue = NULL;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700268 }
269}
270
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700271/*
272 * spin_lock_irqsave() is expected to be held on entry.
273 */
274static void
275xpc_process_disconnect(struct xpc_channel *ch, unsigned long *irq_flags)
276{
277 struct xpc_partition *part = &xpc_partitions[ch->partid];
Dean Nelsona607c3892005-09-01 14:01:37 -0500278 u32 channel_was_connected = (ch->flags & XPC_C_WASCONNECTED);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700279
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700280 DBUG_ON(!spin_is_locked(&ch->lock));
281
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500282 if (!(ch->flags & XPC_C_DISCONNECTING))
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700283 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700284
285 DBUG_ON(!(ch->flags & XPC_C_CLOSEREQUEST));
286
287 /* make sure all activity has settled down first */
288
Dean Nelsona460ef82006-11-22 08:25:00 -0600289 if (atomic_read(&ch->kthreads_assigned) > 0 ||
Dean Nelson35190502008-04-22 14:48:55 -0500290 atomic_read(&ch->references) > 0) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700291 return;
292 }
Dean Nelsona460ef82006-11-22 08:25:00 -0600293 DBUG_ON((ch->flags & XPC_C_CONNECTEDCALLOUT_MADE) &&
Dean Nelson35190502008-04-22 14:48:55 -0500294 !(ch->flags & XPC_C_DISCONNECTINGCALLOUT_MADE));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700295
Dean Nelsona607c3892005-09-01 14:01:37 -0500296 if (part->act_state == XPC_P_DEACTIVATING) {
297 /* can't proceed until the other side disengages from us */
Dean Nelsona47d5da2008-07-29 22:34:09 -0700298 if (xpc_partition_engaged(ch->partid))
Dean Nelsona607c3892005-09-01 14:01:37 -0500299 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700300
Dean Nelsona607c3892005-09-01 14:01:37 -0500301 } else {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700302
303 /* as long as the other side is up do the full protocol */
304
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500305 if (!(ch->flags & XPC_C_RCLOSEREQUEST))
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700306 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700307
308 if (!(ch->flags & XPC_C_CLOSEREPLY)) {
309 ch->flags |= XPC_C_CLOSEREPLY;
Dean Nelsona47d5da2008-07-29 22:34:09 -0700310 xpc_send_channel_closereply(ch, irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700311 }
312
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500313 if (!(ch->flags & XPC_C_RCLOSEREPLY))
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700314 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700315 }
316
Dean Nelsona607c3892005-09-01 14:01:37 -0500317 /* wake those waiting for notify completion */
318 if (atomic_read(&ch->n_to_notify) > 0) {
319 /* >>> we do callout while holding ch->lock */
Dean Nelsona47d5da2008-07-29 22:34:09 -0700320 xpc_notify_senders_of_disconnect(ch);
Dean Nelsona607c3892005-09-01 14:01:37 -0500321 }
322
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700323 /* both sides are disconnected now */
324
Dean Nelson4c2cd962006-02-15 08:02:21 -0600325 if (ch->flags & XPC_C_DISCONNECTINGCALLOUT_MADE) {
Dean Nelson246c7e32005-12-22 14:32:56 -0600326 spin_unlock_irqrestore(&ch->lock, *irq_flags);
Dean Nelson65c17b82008-05-12 14:02:02 -0700327 xpc_disconnect_callout(ch, xpDisconnected);
Dean Nelson246c7e32005-12-22 14:32:56 -0600328 spin_lock_irqsave(&ch->lock, *irq_flags);
329 }
330
Dean Nelsona607c3892005-09-01 14:01:37 -0500331 /* it's now safe to free the channel's message queues */
332 xpc_free_msgqueues(ch);
333
334 /* mark disconnected, clear all other flags except XPC_C_WDISCONNECT */
335 ch->flags = (XPC_C_DISCONNECTED | (ch->flags & XPC_C_WDISCONNECT));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700336
337 atomic_dec(&part->nchannels_active);
338
Dean Nelsona607c3892005-09-01 14:01:37 -0500339 if (channel_was_connected) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700340 dev_info(xpc_chan, "channel %d to partition %d disconnected, "
Dean Nelson35190502008-04-22 14:48:55 -0500341 "reason=%d\n", ch->number, ch->partid, ch->reason);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700342 }
Dean Nelsona607c3892005-09-01 14:01:37 -0500343
Dean Nelsona607c3892005-09-01 14:01:37 -0500344 if (ch->flags & XPC_C_WDISCONNECT) {
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500345 /* we won't lose the CPU since we're holding ch->lock */
346 complete(&ch->wdisconnect_wait);
Dean Nelsone54af722005-10-25 14:07:43 -0500347 } else if (ch->delayed_IPI_flags) {
348 if (part->act_state != XPC_P_DEACTIVATING) {
349 /* time to take action on any delayed IPI flags */
350 spin_lock(&part->IPI_lock);
351 XPC_SET_IPI_FLAGS(part->local_IPI_amo, ch->number,
Dean Nelson35190502008-04-22 14:48:55 -0500352 ch->delayed_IPI_flags);
Dean Nelsone54af722005-10-25 14:07:43 -0500353 spin_unlock(&part->IPI_lock);
354 }
355 ch->delayed_IPI_flags = 0;
Dean Nelsona607c3892005-09-01 14:01:37 -0500356 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700357}
358
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700359/*
360 * Process a change in the channel's remote connection state.
361 */
362static void
363xpc_process_openclose_IPI(struct xpc_partition *part, int ch_number,
Dean Nelson35190502008-04-22 14:48:55 -0500364 u8 IPI_flags)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700365{
366 unsigned long irq_flags;
367 struct xpc_openclose_args *args =
Dean Nelson35190502008-04-22 14:48:55 -0500368 &part->remote_openclose_args[ch_number];
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700369 struct xpc_channel *ch = &part->channels[ch_number];
Dean Nelson65c17b82008-05-12 14:02:02 -0700370 enum xp_retval reason;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700371
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700372 spin_lock_irqsave(&ch->lock, irq_flags);
373
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500374again:
Dean Nelsone54af722005-10-25 14:07:43 -0500375
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500376 if ((ch->flags & XPC_C_DISCONNECTED) &&
377 (ch->flags & XPC_C_WDISCONNECT)) {
Dean Nelsone54af722005-10-25 14:07:43 -0500378 /*
379 * Delay processing IPI flags until thread waiting disconnect
380 * has had a chance to see that the channel is disconnected.
381 */
382 ch->delayed_IPI_flags |= IPI_flags;
383 spin_unlock_irqrestore(&ch->lock, irq_flags);
384 return;
385 }
386
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700387 if (IPI_flags & XPC_IPI_CLOSEREQUEST) {
388
389 dev_dbg(xpc_chan, "XPC_IPI_CLOSEREQUEST (reason=%d) received "
390 "from partid=%d, channel=%d\n", args->reason,
391 ch->partid, ch->number);
392
393 /*
394 * If RCLOSEREQUEST is set, we're probably waiting for
395 * RCLOSEREPLY. We should find it and a ROPENREQUEST packed
Dean Nelsona607c3892005-09-01 14:01:37 -0500396 * with this RCLOSEREQUEST in the IPI_flags.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700397 */
398
399 if (ch->flags & XPC_C_RCLOSEREQUEST) {
400 DBUG_ON(!(ch->flags & XPC_C_DISCONNECTING));
401 DBUG_ON(!(ch->flags & XPC_C_CLOSEREQUEST));
402 DBUG_ON(!(ch->flags & XPC_C_CLOSEREPLY));
403 DBUG_ON(ch->flags & XPC_C_RCLOSEREPLY);
404
405 DBUG_ON(!(IPI_flags & XPC_IPI_CLOSEREPLY));
406 IPI_flags &= ~XPC_IPI_CLOSEREPLY;
407 ch->flags |= XPC_C_RCLOSEREPLY;
408
409 /* both sides have finished disconnecting */
410 xpc_process_disconnect(ch, &irq_flags);
Dean Nelsone54af722005-10-25 14:07:43 -0500411 DBUG_ON(!(ch->flags & XPC_C_DISCONNECTED));
412 goto again;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700413 }
414
415 if (ch->flags & XPC_C_DISCONNECTED) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700416 if (!(IPI_flags & XPC_IPI_OPENREQUEST)) {
Dean Nelsone54af722005-10-25 14:07:43 -0500417 if ((XPC_GET_IPI_FLAGS(part->local_IPI_amo,
Dean Nelson35190502008-04-22 14:48:55 -0500418 ch_number) &
419 XPC_IPI_OPENREQUEST)) {
Dean Nelsone54af722005-10-25 14:07:43 -0500420
421 DBUG_ON(ch->delayed_IPI_flags != 0);
422 spin_lock(&part->IPI_lock);
423 XPC_SET_IPI_FLAGS(part->local_IPI_amo,
Dean Nelson35190502008-04-22 14:48:55 -0500424 ch_number,
425 XPC_IPI_CLOSEREQUEST);
Dean Nelsone54af722005-10-25 14:07:43 -0500426 spin_unlock(&part->IPI_lock);
427 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700428 spin_unlock_irqrestore(&ch->lock, irq_flags);
429 return;
430 }
431
432 XPC_SET_REASON(ch, 0, 0);
433 ch->flags &= ~XPC_C_DISCONNECTED;
434
435 atomic_inc(&part->nchannels_active);
436 ch->flags |= (XPC_C_CONNECTING | XPC_C_ROPENREQUEST);
437 }
438
439 IPI_flags &= ~(XPC_IPI_OPENREQUEST | XPC_IPI_OPENREPLY);
440
441 /*
442 * The meaningful CLOSEREQUEST connection state fields are:
443 * reason = reason connection is to be closed
444 */
445
446 ch->flags |= XPC_C_RCLOSEREQUEST;
447
448 if (!(ch->flags & XPC_C_DISCONNECTING)) {
449 reason = args->reason;
Dean Nelson65c17b82008-05-12 14:02:02 -0700450 if (reason <= xpSuccess || reason > xpUnknownReason)
451 reason = xpUnknownReason;
452 else if (reason == xpUnregistering)
453 reason = xpOtherUnregistering;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700454
455 XPC_DISCONNECT_CHANNEL(ch, reason, &irq_flags);
Dean Nelsone54af722005-10-25 14:07:43 -0500456
457 DBUG_ON(IPI_flags & XPC_IPI_CLOSEREPLY);
458 spin_unlock_irqrestore(&ch->lock, irq_flags);
459 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700460 }
Dean Nelsone54af722005-10-25 14:07:43 -0500461
462 xpc_process_disconnect(ch, &irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700463 }
464
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700465 if (IPI_flags & XPC_IPI_CLOSEREPLY) {
466
467 dev_dbg(xpc_chan, "XPC_IPI_CLOSEREPLY received from partid=%d,"
468 " channel=%d\n", ch->partid, ch->number);
469
470 if (ch->flags & XPC_C_DISCONNECTED) {
471 DBUG_ON(part->act_state != XPC_P_DEACTIVATING);
472 spin_unlock_irqrestore(&ch->lock, irq_flags);
473 return;
474 }
475
476 DBUG_ON(!(ch->flags & XPC_C_CLOSEREQUEST));
Dean Nelsone54af722005-10-25 14:07:43 -0500477
478 if (!(ch->flags & XPC_C_RCLOSEREQUEST)) {
479 if ((XPC_GET_IPI_FLAGS(part->local_IPI_amo, ch_number)
Dean Nelson35190502008-04-22 14:48:55 -0500480 & XPC_IPI_CLOSEREQUEST)) {
Dean Nelsone54af722005-10-25 14:07:43 -0500481
482 DBUG_ON(ch->delayed_IPI_flags != 0);
483 spin_lock(&part->IPI_lock);
484 XPC_SET_IPI_FLAGS(part->local_IPI_amo,
Dean Nelson35190502008-04-22 14:48:55 -0500485 ch_number,
486 XPC_IPI_CLOSEREPLY);
Dean Nelsone54af722005-10-25 14:07:43 -0500487 spin_unlock(&part->IPI_lock);
488 }
489 spin_unlock_irqrestore(&ch->lock, irq_flags);
490 return;
491 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700492
493 ch->flags |= XPC_C_RCLOSEREPLY;
494
495 if (ch->flags & XPC_C_CLOSEREPLY) {
496 /* both sides have finished disconnecting */
497 xpc_process_disconnect(ch, &irq_flags);
498 }
499 }
500
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700501 if (IPI_flags & XPC_IPI_OPENREQUEST) {
502
503 dev_dbg(xpc_chan, "XPC_IPI_OPENREQUEST (msg_size=%d, "
504 "local_nentries=%d) received from partid=%d, "
505 "channel=%d\n", args->msg_size, args->local_nentries,
506 ch->partid, ch->number);
507
Dean Nelsone54af722005-10-25 14:07:43 -0500508 if (part->act_state == XPC_P_DEACTIVATING ||
Dean Nelson35190502008-04-22 14:48:55 -0500509 (ch->flags & XPC_C_ROPENREQUEST)) {
Dean Nelsone54af722005-10-25 14:07:43 -0500510 spin_unlock_irqrestore(&ch->lock, irq_flags);
511 return;
512 }
513
514 if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_WDISCONNECT)) {
515 ch->delayed_IPI_flags |= XPC_IPI_OPENREQUEST;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700516 spin_unlock_irqrestore(&ch->lock, irq_flags);
517 return;
518 }
519 DBUG_ON(!(ch->flags & (XPC_C_DISCONNECTED |
Dean Nelson35190502008-04-22 14:48:55 -0500520 XPC_C_OPENREQUEST)));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700521 DBUG_ON(ch->flags & (XPC_C_ROPENREQUEST | XPC_C_ROPENREPLY |
Dean Nelson35190502008-04-22 14:48:55 -0500522 XPC_C_OPENREPLY | XPC_C_CONNECTED));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700523
524 /*
525 * The meaningful OPENREQUEST connection state fields are:
526 * msg_size = size of channel's messages in bytes
527 * local_nentries = remote partition's local_nentries
528 */
Dean Nelsone54af722005-10-25 14:07:43 -0500529 if (args->msg_size == 0 || args->local_nentries == 0) {
530 /* assume OPENREQUEST was delayed by mistake */
531 spin_unlock_irqrestore(&ch->lock, irq_flags);
532 return;
533 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700534
535 ch->flags |= (XPC_C_ROPENREQUEST | XPC_C_CONNECTING);
536 ch->remote_nentries = args->local_nentries;
537
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700538 if (ch->flags & XPC_C_OPENREQUEST) {
539 if (args->msg_size != ch->msg_size) {
Dean Nelson65c17b82008-05-12 14:02:02 -0700540 XPC_DISCONNECT_CHANNEL(ch, xpUnequalMsgSizes,
Dean Nelson35190502008-04-22 14:48:55 -0500541 &irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700542 spin_unlock_irqrestore(&ch->lock, irq_flags);
543 return;
544 }
545 } else {
546 ch->msg_size = args->msg_size;
547
548 XPC_SET_REASON(ch, 0, 0);
549 ch->flags &= ~XPC_C_DISCONNECTED;
550
551 atomic_inc(&part->nchannels_active);
552 }
553
554 xpc_process_connect(ch, &irq_flags);
555 }
556
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700557 if (IPI_flags & XPC_IPI_OPENREPLY) {
558
559 dev_dbg(xpc_chan, "XPC_IPI_OPENREPLY (local_msgqueue_pa=0x%lx, "
560 "local_nentries=%d, remote_nentries=%d) received from "
561 "partid=%d, channel=%d\n", args->local_msgqueue_pa,
562 args->local_nentries, args->remote_nentries,
563 ch->partid, ch->number);
564
565 if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_DISCONNECTED)) {
566 spin_unlock_irqrestore(&ch->lock, irq_flags);
567 return;
568 }
Dean Nelsone54af722005-10-25 14:07:43 -0500569 if (!(ch->flags & XPC_C_OPENREQUEST)) {
Dean Nelson65c17b82008-05-12 14:02:02 -0700570 XPC_DISCONNECT_CHANNEL(ch, xpOpenCloseError,
Dean Nelson35190502008-04-22 14:48:55 -0500571 &irq_flags);
Dean Nelsone54af722005-10-25 14:07:43 -0500572 spin_unlock_irqrestore(&ch->lock, irq_flags);
573 return;
574 }
575
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700576 DBUG_ON(!(ch->flags & XPC_C_ROPENREQUEST));
577 DBUG_ON(ch->flags & XPC_C_CONNECTED);
578
579 /*
580 * The meaningful OPENREPLY connection state fields are:
581 * local_msgqueue_pa = physical address of remote
Dean Nelson35190502008-04-22 14:48:55 -0500582 * partition's local_msgqueue
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700583 * local_nentries = remote partition's local_nentries
584 * remote_nentries = remote partition's remote_nentries
585 */
586 DBUG_ON(args->local_msgqueue_pa == 0);
587 DBUG_ON(args->local_nentries == 0);
588 DBUG_ON(args->remote_nentries == 0);
589
590 ch->flags |= XPC_C_ROPENREPLY;
591 ch->remote_msgqueue_pa = args->local_msgqueue_pa;
592
593 if (args->local_nentries < ch->remote_nentries) {
594 dev_dbg(xpc_chan, "XPC_IPI_OPENREPLY: new "
595 "remote_nentries=%d, old remote_nentries=%d, "
596 "partid=%d, channel=%d\n",
597 args->local_nentries, ch->remote_nentries,
598 ch->partid, ch->number);
599
600 ch->remote_nentries = args->local_nentries;
601 }
602 if (args->remote_nentries < ch->local_nentries) {
603 dev_dbg(xpc_chan, "XPC_IPI_OPENREPLY: new "
604 "local_nentries=%d, old local_nentries=%d, "
605 "partid=%d, channel=%d\n",
606 args->remote_nentries, ch->local_nentries,
607 ch->partid, ch->number);
608
609 ch->local_nentries = args->remote_nentries;
610 }
611
612 xpc_process_connect(ch, &irq_flags);
613 }
614
615 spin_unlock_irqrestore(&ch->lock, irq_flags);
616}
617
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700618/*
619 * Attempt to establish a channel connection to a remote partition.
620 */
Dean Nelson65c17b82008-05-12 14:02:02 -0700621static enum xp_retval
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700622xpc_connect_channel(struct xpc_channel *ch)
623{
624 unsigned long irq_flags;
625 struct xpc_registration *registration = &xpc_registrations[ch->number];
626
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500627 if (mutex_trylock(&registration->mutex) == 0)
Dean Nelson65c17b82008-05-12 14:02:02 -0700628 return xpRetry;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700629
630 if (!XPC_CHANNEL_REGISTERED(ch->number)) {
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500631 mutex_unlock(&registration->mutex);
Dean Nelson65c17b82008-05-12 14:02:02 -0700632 return xpUnregistered;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700633 }
634
635 spin_lock_irqsave(&ch->lock, irq_flags);
636
637 DBUG_ON(ch->flags & XPC_C_CONNECTED);
638 DBUG_ON(ch->flags & XPC_C_OPENREQUEST);
639
640 if (ch->flags & XPC_C_DISCONNECTING) {
641 spin_unlock_irqrestore(&ch->lock, irq_flags);
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500642 mutex_unlock(&registration->mutex);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700643 return ch->reason;
644 }
645
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700646 /* add info from the channel connect registration to the channel */
647
648 ch->kthreads_assigned_limit = registration->assigned_limit;
649 ch->kthreads_idle_limit = registration->idle_limit;
650 DBUG_ON(atomic_read(&ch->kthreads_assigned) != 0);
651 DBUG_ON(atomic_read(&ch->kthreads_idle) != 0);
652 DBUG_ON(atomic_read(&ch->kthreads_active) != 0);
653
654 ch->func = registration->func;
655 DBUG_ON(registration->func == NULL);
656 ch->key = registration->key;
657
658 ch->local_nentries = registration->nentries;
659
660 if (ch->flags & XPC_C_ROPENREQUEST) {
661 if (registration->msg_size != ch->msg_size) {
662 /* the local and remote sides aren't the same */
663
664 /*
665 * Because XPC_DISCONNECT_CHANNEL() can block we're
666 * forced to up the registration sema before we unlock
667 * the channel lock. But that's okay here because we're
668 * done with the part that required the registration
669 * sema. XPC_DISCONNECT_CHANNEL() requires that the
670 * channel lock be locked and will unlock and relock
671 * the channel lock as needed.
672 */
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500673 mutex_unlock(&registration->mutex);
Dean Nelson65c17b82008-05-12 14:02:02 -0700674 XPC_DISCONNECT_CHANNEL(ch, xpUnequalMsgSizes,
Dean Nelson35190502008-04-22 14:48:55 -0500675 &irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700676 spin_unlock_irqrestore(&ch->lock, irq_flags);
Dean Nelson65c17b82008-05-12 14:02:02 -0700677 return xpUnequalMsgSizes;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700678 }
679 } else {
680 ch->msg_size = registration->msg_size;
681
682 XPC_SET_REASON(ch, 0, 0);
683 ch->flags &= ~XPC_C_DISCONNECTED;
684
685 atomic_inc(&xpc_partitions[ch->partid].nchannels_active);
686 }
687
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500688 mutex_unlock(&registration->mutex);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700689
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700690 /* initiate the connection */
691
692 ch->flags |= (XPC_C_OPENREQUEST | XPC_C_CONNECTING);
Dean Nelsona47d5da2008-07-29 22:34:09 -0700693 xpc_send_channel_openrequest(ch, &irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700694
695 xpc_process_connect(ch, &irq_flags);
696
697 spin_unlock_irqrestore(&ch->lock, irq_flags);
698
Dean Nelson65c17b82008-05-12 14:02:02 -0700699 return xpSuccess;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700700}
701
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700702void
703xpc_process_channel_activity(struct xpc_partition *part)
704{
705 unsigned long irq_flags;
706 u64 IPI_amo, IPI_flags;
707 struct xpc_channel *ch;
708 int ch_number;
Dean Nelsona607c3892005-09-01 14:01:37 -0500709 u32 ch_flags;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700710
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700711 IPI_amo = xpc_get_IPI_flags(part);
712
713 /*
714 * Initiate channel connections for registered channels.
715 *
716 * For each connected channel that has pending messages activate idle
717 * kthreads and/or create new kthreads as needed.
718 */
719
720 for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
721 ch = &part->channels[ch_number];
722
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700723 /*
724 * Process any open or close related IPI flags, and then deal
725 * with connecting or disconnecting the channel as required.
726 */
727
728 IPI_flags = XPC_GET_IPI_FLAGS(IPI_amo, ch_number);
729
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500730 if (XPC_ANY_OPENCLOSE_IPI_FLAGS_SET(IPI_flags))
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700731 xpc_process_openclose_IPI(part, ch_number, IPI_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700732
Dean Nelsona607c3892005-09-01 14:01:37 -0500733 ch_flags = ch->flags; /* need an atomic snapshot of flags */
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700734
Dean Nelsona607c3892005-09-01 14:01:37 -0500735 if (ch_flags & XPC_C_DISCONNECTING) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700736 spin_lock_irqsave(&ch->lock, irq_flags);
737 xpc_process_disconnect(ch, &irq_flags);
738 spin_unlock_irqrestore(&ch->lock, irq_flags);
739 continue;
740 }
741
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500742 if (part->act_state == XPC_P_DEACTIVATING)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700743 continue;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700744
Dean Nelsona607c3892005-09-01 14:01:37 -0500745 if (!(ch_flags & XPC_C_CONNECTED)) {
746 if (!(ch_flags & XPC_C_OPENREQUEST)) {
747 DBUG_ON(ch_flags & XPC_C_SETUP);
Dean Nelson35190502008-04-22 14:48:55 -0500748 (void)xpc_connect_channel(ch);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700749 } else {
750 spin_lock_irqsave(&ch->lock, irq_flags);
751 xpc_process_connect(ch, &irq_flags);
752 spin_unlock_irqrestore(&ch->lock, irq_flags);
753 }
754 continue;
755 }
756
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700757 /*
758 * Process any message related IPI flags, this may involve the
759 * activation of kthreads to deliver any pending messages sent
760 * from the other partition.
761 */
762
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500763 if (XPC_ANY_MSG_IPI_FLAGS_SET(IPI_flags))
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700764 xpc_process_msg_IPI(part, ch_number);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700765 }
766}
767
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700768/*
Dean Nelsona607c3892005-09-01 14:01:37 -0500769 * XPC's heartbeat code calls this function to inform XPC that a partition is
770 * going down. XPC responds by tearing down the XPartition Communication
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700771 * infrastructure used for the just downed partition.
772 *
773 * XPC's heartbeat code will never call this function and xpc_partition_up()
774 * at the same time. Nor will it ever make multiple calls to either function
775 * at the same time.
776 */
777void
Dean Nelson65c17b82008-05-12 14:02:02 -0700778xpc_partition_going_down(struct xpc_partition *part, enum xp_retval reason)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700779{
780 unsigned long irq_flags;
781 int ch_number;
782 struct xpc_channel *ch;
783
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700784 dev_dbg(xpc_chan, "deactivating partition %d, reason=%d\n",
785 XPC_PARTID(part), reason);
786
787 if (!xpc_part_ref(part)) {
788 /* infrastructure for this partition isn't currently set up */
789 return;
790 }
791
Dean Nelsona607c3892005-09-01 14:01:37 -0500792 /* disconnect channels associated with the partition going down */
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700793
794 for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
795 ch = &part->channels[ch_number];
796
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700797 xpc_msgqueue_ref(ch);
798 spin_lock_irqsave(&ch->lock, irq_flags);
799
800 XPC_DISCONNECT_CHANNEL(ch, reason, &irq_flags);
801
802 spin_unlock_irqrestore(&ch->lock, irq_flags);
803 xpc_msgqueue_deref(ch);
804 }
805
806 xpc_wakeup_channel_mgr(part);
807
808 xpc_part_deref(part);
809}
810
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700811/*
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700812 * Called by XP at the time of channel connection registration to cause
813 * XPC to establish connections to all currently active partitions.
814 */
815void
816xpc_initiate_connect(int ch_number)
817{
Dean Nelson64d032b2008-05-12 14:02:03 -0700818 short partid;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700819 struct xpc_partition *part;
820 struct xpc_channel *ch;
821
Dean Nelsonbc63d382008-07-29 22:34:04 -0700822 DBUG_ON(ch_number < 0 || ch_number >= XPC_MAX_NCHANNELS);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700823
Dean Nelsonbc63d382008-07-29 22:34:04 -0700824 for (partid = 0; partid < xp_max_npartitions; partid++) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700825 part = &xpc_partitions[partid];
826
827 if (xpc_part_ref(part)) {
828 ch = &part->channels[ch_number];
829
Dean Nelsone54af722005-10-25 14:07:43 -0500830 /*
831 * Initiate the establishment of a connection on the
832 * newly registered channel to the remote partition.
833 */
834 xpc_wakeup_channel_mgr(part);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700835 xpc_part_deref(part);
836 }
837 }
838}
839
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700840void
841xpc_connected_callout(struct xpc_channel *ch)
842{
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700843 /* let the registerer know that a connection has been established */
844
845 if (ch->func != NULL) {
Dean Nelson65c17b82008-05-12 14:02:02 -0700846 dev_dbg(xpc_chan, "ch->func() called, reason=xpConnected, "
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700847 "partid=%d, channel=%d\n", ch->partid, ch->number);
848
Dean Nelson65c17b82008-05-12 14:02:02 -0700849 ch->func(xpConnected, ch->partid, ch->number,
Dean Nelson35190502008-04-22 14:48:55 -0500850 (void *)(u64)ch->local_nentries, ch->key);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700851
Dean Nelson65c17b82008-05-12 14:02:02 -0700852 dev_dbg(xpc_chan, "ch->func() returned, reason=xpConnected, "
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700853 "partid=%d, channel=%d\n", ch->partid, ch->number);
854 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700855}
856
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700857/*
858 * Called by XP at the time of channel connection unregistration to cause
859 * XPC to teardown all current connections for the specified channel.
860 *
861 * Before returning xpc_initiate_disconnect() will wait until all connections
862 * on the specified channel have been closed/torndown. So the caller can be
863 * assured that they will not be receiving any more callouts from XPC to the
864 * function they registered via xpc_connect().
865 *
866 * Arguments:
867 *
868 * ch_number - channel # to unregister.
869 */
870void
871xpc_initiate_disconnect(int ch_number)
872{
873 unsigned long irq_flags;
Dean Nelson64d032b2008-05-12 14:02:03 -0700874 short partid;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700875 struct xpc_partition *part;
876 struct xpc_channel *ch;
877
Dean Nelsonbc63d382008-07-29 22:34:04 -0700878 DBUG_ON(ch_number < 0 || ch_number >= XPC_MAX_NCHANNELS);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700879
880 /* initiate the channel disconnect for every active partition */
Dean Nelsonbc63d382008-07-29 22:34:04 -0700881 for (partid = 0; partid < xp_max_npartitions; partid++) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700882 part = &xpc_partitions[partid];
883
884 if (xpc_part_ref(part)) {
885 ch = &part->channels[ch_number];
886 xpc_msgqueue_ref(ch);
887
888 spin_lock_irqsave(&ch->lock, irq_flags);
889
Dean Nelsona607c3892005-09-01 14:01:37 -0500890 if (!(ch->flags & XPC_C_DISCONNECTED)) {
891 ch->flags |= XPC_C_WDISCONNECT;
892
Dean Nelson65c17b82008-05-12 14:02:02 -0700893 XPC_DISCONNECT_CHANNEL(ch, xpUnregistering,
Dean Nelson35190502008-04-22 14:48:55 -0500894 &irq_flags);
Dean Nelsona607c3892005-09-01 14:01:37 -0500895 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700896
897 spin_unlock_irqrestore(&ch->lock, irq_flags);
898
899 xpc_msgqueue_deref(ch);
900 xpc_part_deref(part);
901 }
902 }
903
904 xpc_disconnect_wait(ch_number);
905}
906
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700907/*
908 * To disconnect a channel, and reflect it back to all who may be waiting.
909 *
Dean Nelsona607c3892005-09-01 14:01:37 -0500910 * An OPEN is not allowed until XPC_C_DISCONNECTING is cleared by
911 * xpc_process_disconnect(), and if set, XPC_C_WDISCONNECT is cleared by
912 * xpc_disconnect_wait().
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700913 *
914 * THE CHANNEL IS TO BE LOCKED BY THE CALLER AND WILL REMAIN LOCKED UPON RETURN.
915 */
916void
917xpc_disconnect_channel(const int line, struct xpc_channel *ch,
Dean Nelson65c17b82008-05-12 14:02:02 -0700918 enum xp_retval reason, unsigned long *irq_flags)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700919{
Dean Nelsona607c3892005-09-01 14:01:37 -0500920 u32 channel_was_connected = (ch->flags & XPC_C_CONNECTED);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700921
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700922 DBUG_ON(!spin_is_locked(&ch->lock));
923
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500924 if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_DISCONNECTED))
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700925 return;
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500926
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700927 DBUG_ON(!(ch->flags & (XPC_C_CONNECTING | XPC_C_CONNECTED)));
928
929 dev_dbg(xpc_chan, "reason=%d, line=%d, partid=%d, channel=%d\n",
930 reason, line, ch->partid, ch->number);
931
932 XPC_SET_REASON(ch, reason, line);
933
Dean Nelsona607c3892005-09-01 14:01:37 -0500934 ch->flags |= (XPC_C_CLOSEREQUEST | XPC_C_DISCONNECTING);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700935 /* some of these may not have been set */
936 ch->flags &= ~(XPC_C_OPENREQUEST | XPC_C_OPENREPLY |
Dean Nelson35190502008-04-22 14:48:55 -0500937 XPC_C_ROPENREQUEST | XPC_C_ROPENREPLY |
938 XPC_C_CONNECTING | XPC_C_CONNECTED);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700939
Dean Nelsona47d5da2008-07-29 22:34:09 -0700940 xpc_send_channel_closerequest(ch, irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700941
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500942 if (channel_was_connected)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700943 ch->flags |= XPC_C_WASCONNECTED;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700944
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700945 spin_unlock_irqrestore(&ch->lock, *irq_flags);
946
Dean Nelsona607c3892005-09-01 14:01:37 -0500947 /* wake all idle kthreads so they can exit */
948 if (atomic_read(&ch->kthreads_idle) > 0) {
949 wake_up_all(&ch->idle_wq);
Dean Nelsona460ef82006-11-22 08:25:00 -0600950
951 } else if ((ch->flags & XPC_C_CONNECTEDCALLOUT_MADE) &&
Dean Nelson35190502008-04-22 14:48:55 -0500952 !(ch->flags & XPC_C_DISCONNECTINGCALLOUT)) {
Dean Nelson65c17b82008-05-12 14:02:02 -0700953 /* start a kthread that will do the xpDisconnecting callout */
Dean Nelsona460ef82006-11-22 08:25:00 -0600954 xpc_create_kthreads(ch, 1, 1);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700955 }
956
Dean Nelsona607c3892005-09-01 14:01:37 -0500957 /* wake those waiting to allocate an entry from the local msg queue */
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500958 if (atomic_read(&ch->n_on_msg_allocate_wq) > 0)
Dean Nelsona607c3892005-09-01 14:01:37 -0500959 wake_up(&ch->msg_allocate_wq);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700960
961 spin_lock_irqsave(&ch->lock, *irq_flags);
962}
963
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700964void
Dean Nelson65c17b82008-05-12 14:02:02 -0700965xpc_disconnect_callout(struct xpc_channel *ch, enum xp_retval reason)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700966{
967 /*
Dean Nelsona607c3892005-09-01 14:01:37 -0500968 * Let the channel's registerer know that the channel is being
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700969 * disconnected. We don't want to do this if the registerer was never
Dean Nelsona607c3892005-09-01 14:01:37 -0500970 * informed of a connection being made.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700971 */
972
973 if (ch->func != NULL) {
Dean Nelson246c7e32005-12-22 14:32:56 -0600974 dev_dbg(xpc_chan, "ch->func() called, reason=%d, partid=%d, "
975 "channel=%d\n", reason, ch->partid, ch->number);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700976
Dean Nelson246c7e32005-12-22 14:32:56 -0600977 ch->func(reason, ch->partid, ch->number, NULL, ch->key);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700978
Dean Nelson246c7e32005-12-22 14:32:56 -0600979 dev_dbg(xpc_chan, "ch->func() returned, reason=%d, partid=%d, "
980 "channel=%d\n", reason, ch->partid, ch->number);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700981 }
982}
983
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700984/*
985 * Wait for a message entry to become available for the specified channel,
986 * but don't wait any longer than 1 jiffy.
987 */
Dean Nelson33ba3c72008-07-29 22:34:07 -0700988enum xp_retval
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700989xpc_allocate_msg_wait(struct xpc_channel *ch)
990{
Dean Nelson65c17b82008-05-12 14:02:02 -0700991 enum xp_retval ret;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700992
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700993 if (ch->flags & XPC_C_DISCONNECTING) {
Dean Nelson65c17b82008-05-12 14:02:02 -0700994 DBUG_ON(ch->reason == xpInterrupted);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700995 return ch->reason;
996 }
997
998 atomic_inc(&ch->n_on_msg_allocate_wq);
999 ret = interruptible_sleep_on_timeout(&ch->msg_allocate_wq, 1);
1000 atomic_dec(&ch->n_on_msg_allocate_wq);
1001
1002 if (ch->flags & XPC_C_DISCONNECTING) {
1003 ret = ch->reason;
Dean Nelson65c17b82008-05-12 14:02:02 -07001004 DBUG_ON(ch->reason == xpInterrupted);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001005 } else if (ret == 0) {
Dean Nelson65c17b82008-05-12 14:02:02 -07001006 ret = xpTimeout;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001007 } else {
Dean Nelson65c17b82008-05-12 14:02:02 -07001008 ret = xpInterrupted;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001009 }
1010
1011 return ret;
1012}
1013
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001014/*
Dean Nelson97bf1aa2008-07-29 22:34:08 -07001015 * Send a message that contains the user's payload on the specified channel
1016 * connected to the specified partition.
1017 *
1018 * NOTE that this routine can sleep waiting for a message entry to become
1019 * available. To not sleep, pass in the XPC_NOWAIT flag.
1020 *
1021 * Once sent, this routine will not wait for the message to be received, nor
1022 * will notification be given when it does happen.
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001023 *
1024 * Arguments:
1025 *
1026 * partid - ID of partition to which the channel is connected.
Dean Nelson97bf1aa2008-07-29 22:34:08 -07001027 * ch_number - channel # to send message on.
1028 * flags - see xp.h for valid flags.
1029 * payload - pointer to the payload which is to be sent.
1030 * payload_size - size of the payload in bytes.
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001031 */
Dean Nelson65c17b82008-05-12 14:02:02 -07001032enum xp_retval
Dean Nelson97bf1aa2008-07-29 22:34:08 -07001033xpc_initiate_send(short partid, int ch_number, u32 flags, void *payload,
1034 u16 payload_size)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001035{
1036 struct xpc_partition *part = &xpc_partitions[partid];
Dean Nelson65c17b82008-05-12 14:02:02 -07001037 enum xp_retval ret = xpUnknownReason;
Dean Nelson97bf1aa2008-07-29 22:34:08 -07001038
1039 dev_dbg(xpc_chan, "payload=0x%p, partid=%d, channel=%d\n", payload,
1040 partid, ch_number);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001041
Dean Nelsonbc63d382008-07-29 22:34:04 -07001042 DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001043 DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
Dean Nelson97bf1aa2008-07-29 22:34:08 -07001044 DBUG_ON(payload == NULL);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001045
1046 if (xpc_part_ref(part)) {
Dean Nelson97bf1aa2008-07-29 22:34:08 -07001047 ret = xpc_send_msg(&part->channels[ch_number], flags, payload,
1048 payload_size, 0, NULL, NULL);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001049 xpc_part_deref(part);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001050 }
1051
1052 return ret;
1053}
1054
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001055/*
Dean Nelson97bf1aa2008-07-29 22:34:08 -07001056 * Send a message that contains the user's payload on the specified channel
1057 * connected to the specified partition.
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001058 *
Dean Nelson97bf1aa2008-07-29 22:34:08 -07001059 * NOTE that this routine can sleep waiting for a message entry to become
1060 * available. To not sleep, pass in the XPC_NOWAIT flag.
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001061 *
Dean Nelson97bf1aa2008-07-29 22:34:08 -07001062 * This routine will not wait for the message to be sent or received.
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001063 *
1064 * Once the remote end of the channel has received the message, the function
1065 * passed as an argument to xpc_initiate_send_notify() will be called. This
1066 * allows the sender to free up or re-use any buffers referenced by the
1067 * message, but does NOT mean the message has been processed at the remote
1068 * end by a receiver.
1069 *
1070 * If this routine returns an error, the caller's function will NOT be called.
1071 *
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001072 * Arguments:
1073 *
1074 * partid - ID of partition to which the channel is connected.
1075 * ch_number - channel # to send message on.
Dean Nelson97bf1aa2008-07-29 22:34:08 -07001076 * flags - see xp.h for valid flags.
1077 * payload - pointer to the payload which is to be sent.
1078 * payload_size - size of the payload in bytes.
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001079 * func - function to call with asynchronous notification of message
1080 * receipt. THIS FUNCTION MUST BE NON-BLOCKING.
1081 * key - user-defined key to be passed to the function when it's called.
1082 */
Dean Nelson65c17b82008-05-12 14:02:02 -07001083enum xp_retval
Dean Nelson97bf1aa2008-07-29 22:34:08 -07001084xpc_initiate_send_notify(short partid, int ch_number, u32 flags, void *payload,
1085 u16 payload_size, xpc_notify_func func, void *key)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001086{
1087 struct xpc_partition *part = &xpc_partitions[partid];
Dean Nelson97bf1aa2008-07-29 22:34:08 -07001088 enum xp_retval ret = xpUnknownReason;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001089
Dean Nelson97bf1aa2008-07-29 22:34:08 -07001090 dev_dbg(xpc_chan, "payload=0x%p, partid=%d, channel=%d\n", payload,
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001091 partid, ch_number);
1092
Dean Nelsonbc63d382008-07-29 22:34:04 -07001093 DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001094 DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
Dean Nelson97bf1aa2008-07-29 22:34:08 -07001095 DBUG_ON(payload == NULL);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001096 DBUG_ON(func == NULL);
1097
Dean Nelson97bf1aa2008-07-29 22:34:08 -07001098 if (xpc_part_ref(part)) {
1099 ret = xpc_send_msg(&part->channels[ch_number], flags, payload,
1100 payload_size, XPC_N_CALL, func, key);
1101 xpc_part_deref(part);
1102 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001103 return ret;
1104}
1105
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001106/*
1107 * Deliver a message to its intended recipient.
1108 */
1109void
1110xpc_deliver_msg(struct xpc_channel *ch)
1111{
1112 struct xpc_msg *msg;
1113
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001114 msg = xpc_get_deliverable_msg(ch);
1115 if (msg != NULL) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001116
1117 /*
1118 * This ref is taken to protect the payload itself from being
1119 * freed before the user is finished with it, which the user
1120 * indicates by calling xpc_initiate_received().
1121 */
1122 xpc_msgqueue_ref(ch);
1123
1124 atomic_inc(&ch->kthreads_active);
1125
1126 if (ch->func != NULL) {
1127 dev_dbg(xpc_chan, "ch->func() called, msg=0x%p, "
1128 "msg_number=%ld, partid=%d, channel=%d\n",
Dean Nelson35190502008-04-22 14:48:55 -05001129 (void *)msg, msg->number, ch->partid,
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001130 ch->number);
1131
1132 /* deliver the message to its intended recipient */
Dean Nelson65c17b82008-05-12 14:02:02 -07001133 ch->func(xpMsgReceived, ch->partid, ch->number,
Dean Nelson35190502008-04-22 14:48:55 -05001134 &msg->payload, ch->key);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001135
1136 dev_dbg(xpc_chan, "ch->func() returned, msg=0x%p, "
1137 "msg_number=%ld, partid=%d, channel=%d\n",
Dean Nelson35190502008-04-22 14:48:55 -05001138 (void *)msg, msg->number, ch->partid,
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001139 ch->number);
1140 }
1141
1142 atomic_dec(&ch->kthreads_active);
1143 }
1144}
1145
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001146/*
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001147 * Acknowledge receipt of a delivered message.
1148 *
1149 * If a message has XPC_M_INTERRUPT set, send an interrupt to the partition
1150 * that sent the message.
1151 *
1152 * This function, although called by users, does not call xpc_part_ref() to
1153 * ensure that the partition infrastructure is in place. It relies on the
1154 * fact that we called xpc_msgqueue_ref() in xpc_deliver_msg().
1155 *
1156 * Arguments:
1157 *
1158 * partid - ID of partition to which the channel is connected.
1159 * ch_number - channel # message received on.
1160 * payload - pointer to the payload area allocated via
Dean Nelson97bf1aa2008-07-29 22:34:08 -07001161 * xpc_initiate_send() or xpc_initiate_send_notify().
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001162 */
1163void
Dean Nelson64d032b2008-05-12 14:02:03 -07001164xpc_initiate_received(short partid, int ch_number, void *payload)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001165{
1166 struct xpc_partition *part = &xpc_partitions[partid];
1167 struct xpc_channel *ch;
1168 struct xpc_msg *msg = XPC_MSG_ADDRESS(payload);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001169
Dean Nelsonbc63d382008-07-29 22:34:04 -07001170 DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001171 DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
1172
1173 ch = &part->channels[ch_number];
Dean Nelson33ba3c72008-07-29 22:34:07 -07001174 xpc_received_msg(ch, msg);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001175
1176 /* the call to xpc_msgqueue_ref() was done by xpc_deliver_msg() */
1177 xpc_msgqueue_deref(ch);
1178}