blob: 55182c8dd32a232258577347ff2792491c0e3be8 [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;
204 xpc_IPI_send_openreply(ch, irq_flags);
205 }
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/*
Dean Nelsona607c3892005-09-01 14:01:37 -0500223 * Notify those who wanted to be notified upon delivery of their message.
224 */
225static void
Dean Nelson65c17b82008-05-12 14:02:02 -0700226xpc_notify_senders(struct xpc_channel *ch, enum xp_retval reason, s64 put)
Dean Nelsona607c3892005-09-01 14:01:37 -0500227{
228 struct xpc_notify *notify;
229 u8 notify_type;
230 s64 get = ch->w_remote_GP.get - 1;
231
Dean Nelsona607c3892005-09-01 14:01:37 -0500232 while (++get < put && atomic_read(&ch->n_to_notify) > 0) {
233
234 notify = &ch->notify_queue[get % ch->local_nentries];
235
236 /*
237 * See if the notify entry indicates it was associated with
238 * a message who's sender wants to be notified. It is possible
239 * that it is, but someone else is doing or has done the
240 * notification.
241 */
242 notify_type = notify->type;
243 if (notify_type == 0 ||
Dean Nelson35190502008-04-22 14:48:55 -0500244 cmpxchg(&notify->type, notify_type, 0) != notify_type) {
Dean Nelsona607c3892005-09-01 14:01:37 -0500245 continue;
246 }
247
248 DBUG_ON(notify_type != XPC_N_CALL);
249
250 atomic_dec(&ch->n_to_notify);
251
252 if (notify->func != NULL) {
253 dev_dbg(xpc_chan, "notify->func() called, notify=0x%p, "
254 "msg_number=%ld, partid=%d, channel=%d\n",
Dean Nelson35190502008-04-22 14:48:55 -0500255 (void *)notify, get, ch->partid, ch->number);
Dean Nelsona607c3892005-09-01 14:01:37 -0500256
257 notify->func(reason, ch->partid, ch->number,
Dean Nelson35190502008-04-22 14:48:55 -0500258 notify->key);
Dean Nelsona607c3892005-09-01 14:01:37 -0500259
260 dev_dbg(xpc_chan, "notify->func() returned, "
261 "notify=0x%p, msg_number=%ld, partid=%d, "
Dean Nelson35190502008-04-22 14:48:55 -0500262 "channel=%d\n", (void *)notify, get,
Dean Nelsona607c3892005-09-01 14:01:37 -0500263 ch->partid, ch->number);
264 }
265 }
266}
267
Dean Nelsona607c3892005-09-01 14:01:37 -0500268/*
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700269 * Free up message queues and other stuff that were allocated for the specified
270 * channel.
271 *
272 * Note: ch->reason and ch->reason_line are left set for debugging purposes,
273 * they're cleared when XPC_C_DISCONNECTED is cleared.
274 */
275static void
276xpc_free_msgqueues(struct xpc_channel *ch)
277{
278 DBUG_ON(!spin_is_locked(&ch->lock));
279 DBUG_ON(atomic_read(&ch->n_to_notify) != 0);
280
281 ch->remote_msgqueue_pa = 0;
282 ch->func = NULL;
283 ch->key = NULL;
284 ch->msg_size = 0;
285 ch->local_nentries = 0;
286 ch->remote_nentries = 0;
287 ch->kthreads_assigned_limit = 0;
288 ch->kthreads_idle_limit = 0;
289
290 ch->local_GP->get = 0;
291 ch->local_GP->put = 0;
292 ch->remote_GP.get = 0;
293 ch->remote_GP.put = 0;
294 ch->w_local_GP.get = 0;
295 ch->w_local_GP.put = 0;
296 ch->w_remote_GP.get = 0;
297 ch->w_remote_GP.put = 0;
298 ch->next_msg_to_pull = 0;
299
300 if (ch->flags & XPC_C_SETUP) {
301 ch->flags &= ~XPC_C_SETUP;
302
303 dev_dbg(xpc_chan, "ch->flags=0x%x, partid=%d, channel=%d\n",
304 ch->flags, ch->partid, ch->number);
305
306 kfree(ch->local_msgqueue_base);
307 ch->local_msgqueue = NULL;
308 kfree(ch->remote_msgqueue_base);
309 ch->remote_msgqueue = NULL;
310 kfree(ch->notify_queue);
311 ch->notify_queue = NULL;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700312 }
313}
314
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700315/*
316 * spin_lock_irqsave() is expected to be held on entry.
317 */
318static void
319xpc_process_disconnect(struct xpc_channel *ch, unsigned long *irq_flags)
320{
321 struct xpc_partition *part = &xpc_partitions[ch->partid];
Dean Nelsona607c3892005-09-01 14:01:37 -0500322 u32 channel_was_connected = (ch->flags & XPC_C_WASCONNECTED);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700323
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700324 DBUG_ON(!spin_is_locked(&ch->lock));
325
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500326 if (!(ch->flags & XPC_C_DISCONNECTING))
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700327 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700328
329 DBUG_ON(!(ch->flags & XPC_C_CLOSEREQUEST));
330
331 /* make sure all activity has settled down first */
332
Dean Nelsona460ef82006-11-22 08:25:00 -0600333 if (atomic_read(&ch->kthreads_assigned) > 0 ||
Dean Nelson35190502008-04-22 14:48:55 -0500334 atomic_read(&ch->references) > 0) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700335 return;
336 }
Dean Nelsona460ef82006-11-22 08:25:00 -0600337 DBUG_ON((ch->flags & XPC_C_CONNECTEDCALLOUT_MADE) &&
Dean Nelson35190502008-04-22 14:48:55 -0500338 !(ch->flags & XPC_C_DISCONNECTINGCALLOUT_MADE));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700339
Dean Nelsona607c3892005-09-01 14:01:37 -0500340 if (part->act_state == XPC_P_DEACTIVATING) {
341 /* can't proceed until the other side disengages from us */
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500342 if (xpc_partition_engaged(1UL << ch->partid))
Dean Nelsona607c3892005-09-01 14:01:37 -0500343 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700344
Dean Nelsona607c3892005-09-01 14:01:37 -0500345 } else {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700346
347 /* as long as the other side is up do the full protocol */
348
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500349 if (!(ch->flags & XPC_C_RCLOSEREQUEST))
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700350 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700351
352 if (!(ch->flags & XPC_C_CLOSEREPLY)) {
353 ch->flags |= XPC_C_CLOSEREPLY;
354 xpc_IPI_send_closereply(ch, irq_flags);
355 }
356
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500357 if (!(ch->flags & XPC_C_RCLOSEREPLY))
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700358 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700359 }
360
Dean Nelsona607c3892005-09-01 14:01:37 -0500361 /* wake those waiting for notify completion */
362 if (atomic_read(&ch->n_to_notify) > 0) {
363 /* >>> we do callout while holding ch->lock */
364 xpc_notify_senders(ch, ch->reason, ch->w_local_GP.put);
365 }
366
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700367 /* both sides are disconnected now */
368
Dean Nelson4c2cd962006-02-15 08:02:21 -0600369 if (ch->flags & XPC_C_DISCONNECTINGCALLOUT_MADE) {
Dean Nelson246c7e32005-12-22 14:32:56 -0600370 spin_unlock_irqrestore(&ch->lock, *irq_flags);
Dean Nelson65c17b82008-05-12 14:02:02 -0700371 xpc_disconnect_callout(ch, xpDisconnected);
Dean Nelson246c7e32005-12-22 14:32:56 -0600372 spin_lock_irqsave(&ch->lock, *irq_flags);
373 }
374
Dean Nelsona607c3892005-09-01 14:01:37 -0500375 /* it's now safe to free the channel's message queues */
376 xpc_free_msgqueues(ch);
377
378 /* mark disconnected, clear all other flags except XPC_C_WDISCONNECT */
379 ch->flags = (XPC_C_DISCONNECTED | (ch->flags & XPC_C_WDISCONNECT));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700380
381 atomic_dec(&part->nchannels_active);
382
Dean Nelsona607c3892005-09-01 14:01:37 -0500383 if (channel_was_connected) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700384 dev_info(xpc_chan, "channel %d to partition %d disconnected, "
Dean Nelson35190502008-04-22 14:48:55 -0500385 "reason=%d\n", ch->number, ch->partid, ch->reason);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700386 }
Dean Nelsona607c3892005-09-01 14:01:37 -0500387
Dean Nelsona607c3892005-09-01 14:01:37 -0500388 if (ch->flags & XPC_C_WDISCONNECT) {
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500389 /* we won't lose the CPU since we're holding ch->lock */
390 complete(&ch->wdisconnect_wait);
Dean Nelsone54af722005-10-25 14:07:43 -0500391 } else if (ch->delayed_IPI_flags) {
392 if (part->act_state != XPC_P_DEACTIVATING) {
393 /* time to take action on any delayed IPI flags */
394 spin_lock(&part->IPI_lock);
395 XPC_SET_IPI_FLAGS(part->local_IPI_amo, ch->number,
Dean Nelson35190502008-04-22 14:48:55 -0500396 ch->delayed_IPI_flags);
Dean Nelsone54af722005-10-25 14:07:43 -0500397 spin_unlock(&part->IPI_lock);
398 }
399 ch->delayed_IPI_flags = 0;
Dean Nelsona607c3892005-09-01 14:01:37 -0500400 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700401}
402
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700403/*
404 * Process a change in the channel's remote connection state.
405 */
406static void
407xpc_process_openclose_IPI(struct xpc_partition *part, int ch_number,
Dean Nelson35190502008-04-22 14:48:55 -0500408 u8 IPI_flags)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700409{
410 unsigned long irq_flags;
411 struct xpc_openclose_args *args =
Dean Nelson35190502008-04-22 14:48:55 -0500412 &part->remote_openclose_args[ch_number];
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700413 struct xpc_channel *ch = &part->channels[ch_number];
Dean Nelson65c17b82008-05-12 14:02:02 -0700414 enum xp_retval reason;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700415
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700416 spin_lock_irqsave(&ch->lock, irq_flags);
417
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500418again:
Dean Nelsone54af722005-10-25 14:07:43 -0500419
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500420 if ((ch->flags & XPC_C_DISCONNECTED) &&
421 (ch->flags & XPC_C_WDISCONNECT)) {
Dean Nelsone54af722005-10-25 14:07:43 -0500422 /*
423 * Delay processing IPI flags until thread waiting disconnect
424 * has had a chance to see that the channel is disconnected.
425 */
426 ch->delayed_IPI_flags |= IPI_flags;
427 spin_unlock_irqrestore(&ch->lock, irq_flags);
428 return;
429 }
430
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700431 if (IPI_flags & XPC_IPI_CLOSEREQUEST) {
432
433 dev_dbg(xpc_chan, "XPC_IPI_CLOSEREQUEST (reason=%d) received "
434 "from partid=%d, channel=%d\n", args->reason,
435 ch->partid, ch->number);
436
437 /*
438 * If RCLOSEREQUEST is set, we're probably waiting for
439 * RCLOSEREPLY. We should find it and a ROPENREQUEST packed
Dean Nelsona607c3892005-09-01 14:01:37 -0500440 * with this RCLOSEREQUEST in the IPI_flags.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700441 */
442
443 if (ch->flags & XPC_C_RCLOSEREQUEST) {
444 DBUG_ON(!(ch->flags & XPC_C_DISCONNECTING));
445 DBUG_ON(!(ch->flags & XPC_C_CLOSEREQUEST));
446 DBUG_ON(!(ch->flags & XPC_C_CLOSEREPLY));
447 DBUG_ON(ch->flags & XPC_C_RCLOSEREPLY);
448
449 DBUG_ON(!(IPI_flags & XPC_IPI_CLOSEREPLY));
450 IPI_flags &= ~XPC_IPI_CLOSEREPLY;
451 ch->flags |= XPC_C_RCLOSEREPLY;
452
453 /* both sides have finished disconnecting */
454 xpc_process_disconnect(ch, &irq_flags);
Dean Nelsone54af722005-10-25 14:07:43 -0500455 DBUG_ON(!(ch->flags & XPC_C_DISCONNECTED));
456 goto again;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700457 }
458
459 if (ch->flags & XPC_C_DISCONNECTED) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700460 if (!(IPI_flags & XPC_IPI_OPENREQUEST)) {
Dean Nelsone54af722005-10-25 14:07:43 -0500461 if ((XPC_GET_IPI_FLAGS(part->local_IPI_amo,
Dean Nelson35190502008-04-22 14:48:55 -0500462 ch_number) &
463 XPC_IPI_OPENREQUEST)) {
Dean Nelsone54af722005-10-25 14:07:43 -0500464
465 DBUG_ON(ch->delayed_IPI_flags != 0);
466 spin_lock(&part->IPI_lock);
467 XPC_SET_IPI_FLAGS(part->local_IPI_amo,
Dean Nelson35190502008-04-22 14:48:55 -0500468 ch_number,
469 XPC_IPI_CLOSEREQUEST);
Dean Nelsone54af722005-10-25 14:07:43 -0500470 spin_unlock(&part->IPI_lock);
471 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700472 spin_unlock_irqrestore(&ch->lock, irq_flags);
473 return;
474 }
475
476 XPC_SET_REASON(ch, 0, 0);
477 ch->flags &= ~XPC_C_DISCONNECTED;
478
479 atomic_inc(&part->nchannels_active);
480 ch->flags |= (XPC_C_CONNECTING | XPC_C_ROPENREQUEST);
481 }
482
483 IPI_flags &= ~(XPC_IPI_OPENREQUEST | XPC_IPI_OPENREPLY);
484
485 /*
486 * The meaningful CLOSEREQUEST connection state fields are:
487 * reason = reason connection is to be closed
488 */
489
490 ch->flags |= XPC_C_RCLOSEREQUEST;
491
492 if (!(ch->flags & XPC_C_DISCONNECTING)) {
493 reason = args->reason;
Dean Nelson65c17b82008-05-12 14:02:02 -0700494 if (reason <= xpSuccess || reason > xpUnknownReason)
495 reason = xpUnknownReason;
496 else if (reason == xpUnregistering)
497 reason = xpOtherUnregistering;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700498
499 XPC_DISCONNECT_CHANNEL(ch, reason, &irq_flags);
Dean Nelsone54af722005-10-25 14:07:43 -0500500
501 DBUG_ON(IPI_flags & XPC_IPI_CLOSEREPLY);
502 spin_unlock_irqrestore(&ch->lock, irq_flags);
503 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700504 }
Dean Nelsone54af722005-10-25 14:07:43 -0500505
506 xpc_process_disconnect(ch, &irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700507 }
508
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700509 if (IPI_flags & XPC_IPI_CLOSEREPLY) {
510
511 dev_dbg(xpc_chan, "XPC_IPI_CLOSEREPLY received from partid=%d,"
512 " channel=%d\n", ch->partid, ch->number);
513
514 if (ch->flags & XPC_C_DISCONNECTED) {
515 DBUG_ON(part->act_state != XPC_P_DEACTIVATING);
516 spin_unlock_irqrestore(&ch->lock, irq_flags);
517 return;
518 }
519
520 DBUG_ON(!(ch->flags & XPC_C_CLOSEREQUEST));
Dean Nelsone54af722005-10-25 14:07:43 -0500521
522 if (!(ch->flags & XPC_C_RCLOSEREQUEST)) {
523 if ((XPC_GET_IPI_FLAGS(part->local_IPI_amo, ch_number)
Dean Nelson35190502008-04-22 14:48:55 -0500524 & XPC_IPI_CLOSEREQUEST)) {
Dean Nelsone54af722005-10-25 14:07:43 -0500525
526 DBUG_ON(ch->delayed_IPI_flags != 0);
527 spin_lock(&part->IPI_lock);
528 XPC_SET_IPI_FLAGS(part->local_IPI_amo,
Dean Nelson35190502008-04-22 14:48:55 -0500529 ch_number,
530 XPC_IPI_CLOSEREPLY);
Dean Nelsone54af722005-10-25 14:07:43 -0500531 spin_unlock(&part->IPI_lock);
532 }
533 spin_unlock_irqrestore(&ch->lock, irq_flags);
534 return;
535 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700536
537 ch->flags |= XPC_C_RCLOSEREPLY;
538
539 if (ch->flags & XPC_C_CLOSEREPLY) {
540 /* both sides have finished disconnecting */
541 xpc_process_disconnect(ch, &irq_flags);
542 }
543 }
544
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700545 if (IPI_flags & XPC_IPI_OPENREQUEST) {
546
547 dev_dbg(xpc_chan, "XPC_IPI_OPENREQUEST (msg_size=%d, "
548 "local_nentries=%d) received from partid=%d, "
549 "channel=%d\n", args->msg_size, args->local_nentries,
550 ch->partid, ch->number);
551
Dean Nelsone54af722005-10-25 14:07:43 -0500552 if (part->act_state == XPC_P_DEACTIVATING ||
Dean Nelson35190502008-04-22 14:48:55 -0500553 (ch->flags & XPC_C_ROPENREQUEST)) {
Dean Nelsone54af722005-10-25 14:07:43 -0500554 spin_unlock_irqrestore(&ch->lock, irq_flags);
555 return;
556 }
557
558 if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_WDISCONNECT)) {
559 ch->delayed_IPI_flags |= XPC_IPI_OPENREQUEST;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700560 spin_unlock_irqrestore(&ch->lock, irq_flags);
561 return;
562 }
563 DBUG_ON(!(ch->flags & (XPC_C_DISCONNECTED |
Dean Nelson35190502008-04-22 14:48:55 -0500564 XPC_C_OPENREQUEST)));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700565 DBUG_ON(ch->flags & (XPC_C_ROPENREQUEST | XPC_C_ROPENREPLY |
Dean Nelson35190502008-04-22 14:48:55 -0500566 XPC_C_OPENREPLY | XPC_C_CONNECTED));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700567
568 /*
569 * The meaningful OPENREQUEST connection state fields are:
570 * msg_size = size of channel's messages in bytes
571 * local_nentries = remote partition's local_nentries
572 */
Dean Nelsone54af722005-10-25 14:07:43 -0500573 if (args->msg_size == 0 || args->local_nentries == 0) {
574 /* assume OPENREQUEST was delayed by mistake */
575 spin_unlock_irqrestore(&ch->lock, irq_flags);
576 return;
577 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700578
579 ch->flags |= (XPC_C_ROPENREQUEST | XPC_C_CONNECTING);
580 ch->remote_nentries = args->local_nentries;
581
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700582 if (ch->flags & XPC_C_OPENREQUEST) {
583 if (args->msg_size != ch->msg_size) {
Dean Nelson65c17b82008-05-12 14:02:02 -0700584 XPC_DISCONNECT_CHANNEL(ch, xpUnequalMsgSizes,
Dean Nelson35190502008-04-22 14:48:55 -0500585 &irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700586 spin_unlock_irqrestore(&ch->lock, irq_flags);
587 return;
588 }
589 } else {
590 ch->msg_size = args->msg_size;
591
592 XPC_SET_REASON(ch, 0, 0);
593 ch->flags &= ~XPC_C_DISCONNECTED;
594
595 atomic_inc(&part->nchannels_active);
596 }
597
598 xpc_process_connect(ch, &irq_flags);
599 }
600
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700601 if (IPI_flags & XPC_IPI_OPENREPLY) {
602
603 dev_dbg(xpc_chan, "XPC_IPI_OPENREPLY (local_msgqueue_pa=0x%lx, "
604 "local_nentries=%d, remote_nentries=%d) received from "
605 "partid=%d, channel=%d\n", args->local_msgqueue_pa,
606 args->local_nentries, args->remote_nentries,
607 ch->partid, ch->number);
608
609 if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_DISCONNECTED)) {
610 spin_unlock_irqrestore(&ch->lock, irq_flags);
611 return;
612 }
Dean Nelsone54af722005-10-25 14:07:43 -0500613 if (!(ch->flags & XPC_C_OPENREQUEST)) {
Dean Nelson65c17b82008-05-12 14:02:02 -0700614 XPC_DISCONNECT_CHANNEL(ch, xpOpenCloseError,
Dean Nelson35190502008-04-22 14:48:55 -0500615 &irq_flags);
Dean Nelsone54af722005-10-25 14:07:43 -0500616 spin_unlock_irqrestore(&ch->lock, irq_flags);
617 return;
618 }
619
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700620 DBUG_ON(!(ch->flags & XPC_C_ROPENREQUEST));
621 DBUG_ON(ch->flags & XPC_C_CONNECTED);
622
623 /*
624 * The meaningful OPENREPLY connection state fields are:
625 * local_msgqueue_pa = physical address of remote
Dean Nelson35190502008-04-22 14:48:55 -0500626 * partition's local_msgqueue
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700627 * local_nentries = remote partition's local_nentries
628 * remote_nentries = remote partition's remote_nentries
629 */
630 DBUG_ON(args->local_msgqueue_pa == 0);
631 DBUG_ON(args->local_nentries == 0);
632 DBUG_ON(args->remote_nentries == 0);
633
634 ch->flags |= XPC_C_ROPENREPLY;
635 ch->remote_msgqueue_pa = args->local_msgqueue_pa;
636
637 if (args->local_nentries < ch->remote_nentries) {
638 dev_dbg(xpc_chan, "XPC_IPI_OPENREPLY: new "
639 "remote_nentries=%d, old remote_nentries=%d, "
640 "partid=%d, channel=%d\n",
641 args->local_nentries, ch->remote_nentries,
642 ch->partid, ch->number);
643
644 ch->remote_nentries = args->local_nentries;
645 }
646 if (args->remote_nentries < ch->local_nentries) {
647 dev_dbg(xpc_chan, "XPC_IPI_OPENREPLY: new "
648 "local_nentries=%d, old local_nentries=%d, "
649 "partid=%d, channel=%d\n",
650 args->remote_nentries, ch->local_nentries,
651 ch->partid, ch->number);
652
653 ch->local_nentries = args->remote_nentries;
654 }
655
656 xpc_process_connect(ch, &irq_flags);
657 }
658
659 spin_unlock_irqrestore(&ch->lock, irq_flags);
660}
661
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700662/*
663 * Attempt to establish a channel connection to a remote partition.
664 */
Dean Nelson65c17b82008-05-12 14:02:02 -0700665static enum xp_retval
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700666xpc_connect_channel(struct xpc_channel *ch)
667{
668 unsigned long irq_flags;
669 struct xpc_registration *registration = &xpc_registrations[ch->number];
670
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500671 if (mutex_trylock(&registration->mutex) == 0)
Dean Nelson65c17b82008-05-12 14:02:02 -0700672 return xpRetry;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700673
674 if (!XPC_CHANNEL_REGISTERED(ch->number)) {
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500675 mutex_unlock(&registration->mutex);
Dean Nelson65c17b82008-05-12 14:02:02 -0700676 return xpUnregistered;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700677 }
678
679 spin_lock_irqsave(&ch->lock, irq_flags);
680
681 DBUG_ON(ch->flags & XPC_C_CONNECTED);
682 DBUG_ON(ch->flags & XPC_C_OPENREQUEST);
683
684 if (ch->flags & XPC_C_DISCONNECTING) {
685 spin_unlock_irqrestore(&ch->lock, irq_flags);
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500686 mutex_unlock(&registration->mutex);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700687 return ch->reason;
688 }
689
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700690 /* add info from the channel connect registration to the channel */
691
692 ch->kthreads_assigned_limit = registration->assigned_limit;
693 ch->kthreads_idle_limit = registration->idle_limit;
694 DBUG_ON(atomic_read(&ch->kthreads_assigned) != 0);
695 DBUG_ON(atomic_read(&ch->kthreads_idle) != 0);
696 DBUG_ON(atomic_read(&ch->kthreads_active) != 0);
697
698 ch->func = registration->func;
699 DBUG_ON(registration->func == NULL);
700 ch->key = registration->key;
701
702 ch->local_nentries = registration->nentries;
703
704 if (ch->flags & XPC_C_ROPENREQUEST) {
705 if (registration->msg_size != ch->msg_size) {
706 /* the local and remote sides aren't the same */
707
708 /*
709 * Because XPC_DISCONNECT_CHANNEL() can block we're
710 * forced to up the registration sema before we unlock
711 * the channel lock. But that's okay here because we're
712 * done with the part that required the registration
713 * sema. XPC_DISCONNECT_CHANNEL() requires that the
714 * channel lock be locked and will unlock and relock
715 * the channel lock as needed.
716 */
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500717 mutex_unlock(&registration->mutex);
Dean Nelson65c17b82008-05-12 14:02:02 -0700718 XPC_DISCONNECT_CHANNEL(ch, xpUnequalMsgSizes,
Dean Nelson35190502008-04-22 14:48:55 -0500719 &irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700720 spin_unlock_irqrestore(&ch->lock, irq_flags);
Dean Nelson65c17b82008-05-12 14:02:02 -0700721 return xpUnequalMsgSizes;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700722 }
723 } else {
724 ch->msg_size = registration->msg_size;
725
726 XPC_SET_REASON(ch, 0, 0);
727 ch->flags &= ~XPC_C_DISCONNECTED;
728
729 atomic_inc(&xpc_partitions[ch->partid].nchannels_active);
730 }
731
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500732 mutex_unlock(&registration->mutex);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700733
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700734 /* initiate the connection */
735
736 ch->flags |= (XPC_C_OPENREQUEST | XPC_C_CONNECTING);
737 xpc_IPI_send_openrequest(ch, &irq_flags);
738
739 xpc_process_connect(ch, &irq_flags);
740
741 spin_unlock_irqrestore(&ch->lock, irq_flags);
742
Dean Nelson65c17b82008-05-12 14:02:02 -0700743 return xpSuccess;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700744}
745
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700746/*
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700747 * Clear some of the msg flags in the local message queue.
748 */
749static inline void
750xpc_clear_local_msgqueue_flags(struct xpc_channel *ch)
751{
752 struct xpc_msg *msg;
753 s64 get;
754
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700755 get = ch->w_remote_GP.get;
756 do {
Dean Nelson35190502008-04-22 14:48:55 -0500757 msg = (struct xpc_msg *)((u64)ch->local_msgqueue +
758 (get % ch->local_nentries) *
759 ch->msg_size);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700760 msg->flags = 0;
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500761 } while (++get < ch->remote_GP.get);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700762}
763
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700764/*
765 * Clear some of the msg flags in the remote message queue.
766 */
767static inline void
768xpc_clear_remote_msgqueue_flags(struct xpc_channel *ch)
769{
770 struct xpc_msg *msg;
771 s64 put;
772
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700773 put = ch->w_remote_GP.put;
774 do {
Dean Nelson35190502008-04-22 14:48:55 -0500775 msg = (struct xpc_msg *)((u64)ch->remote_msgqueue +
776 (put % ch->remote_nentries) *
777 ch->msg_size);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700778 msg->flags = 0;
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500779 } while (++put < ch->remote_GP.put);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700780}
781
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700782static void
783xpc_process_msg_IPI(struct xpc_partition *part, int ch_number)
784{
785 struct xpc_channel *ch = &part->channels[ch_number];
786 int nmsgs_sent;
787
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700788 ch->remote_GP = part->remote_GPs[ch_number];
789
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700790 /* See what, if anything, has changed for each connected channel */
791
792 xpc_msgqueue_ref(ch);
793
794 if (ch->w_remote_GP.get == ch->remote_GP.get &&
Dean Nelson35190502008-04-22 14:48:55 -0500795 ch->w_remote_GP.put == ch->remote_GP.put) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700796 /* nothing changed since GPs were last pulled */
797 xpc_msgqueue_deref(ch);
798 return;
799 }
800
Dean Nelson35190502008-04-22 14:48:55 -0500801 if (!(ch->flags & XPC_C_CONNECTED)) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700802 xpc_msgqueue_deref(ch);
803 return;
804 }
805
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700806 /*
807 * First check to see if messages recently sent by us have been
808 * received by the other side. (The remote GET value will have
809 * changed since we last looked at it.)
810 */
811
812 if (ch->w_remote_GP.get != ch->remote_GP.get) {
813
814 /*
815 * We need to notify any senders that want to be notified
816 * that their sent messages have been received by their
817 * intended recipients. We need to do this before updating
818 * w_remote_GP.get so that we don't allocate the same message
819 * queue entries prematurely (see xpc_allocate_msg()).
820 */
821 if (atomic_read(&ch->n_to_notify) > 0) {
822 /*
823 * Notify senders that messages sent have been
824 * received and delivered by the other side.
825 */
Dean Nelson65c17b82008-05-12 14:02:02 -0700826 xpc_notify_senders(ch, xpMsgDelivered,
Dean Nelson35190502008-04-22 14:48:55 -0500827 ch->remote_GP.get);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700828 }
829
830 /*
831 * Clear msg->flags in previously sent messages, so that
832 * they're ready for xpc_allocate_msg().
833 */
834 xpc_clear_local_msgqueue_flags(ch);
835
Dave Jones821fe942005-06-25 14:54:29 -0700836 ch->w_remote_GP.get = ch->remote_GP.get;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700837
838 dev_dbg(xpc_chan, "w_remote_GP.get changed to %ld, partid=%d, "
839 "channel=%d\n", ch->w_remote_GP.get, ch->partid,
840 ch->number);
841
842 /*
843 * If anyone was waiting for message queue entries to become
844 * available, wake them up.
845 */
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500846 if (atomic_read(&ch->n_on_msg_allocate_wq) > 0)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700847 wake_up(&ch->msg_allocate_wq);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700848 }
849
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700850 /*
851 * Now check for newly sent messages by the other side. (The remote
852 * PUT value will have changed since we last looked at it.)
853 */
854
855 if (ch->w_remote_GP.put != ch->remote_GP.put) {
856 /*
857 * Clear msg->flags in previously received messages, so that
858 * they're ready for xpc_get_deliverable_msg().
859 */
860 xpc_clear_remote_msgqueue_flags(ch);
861
Dave Jones821fe942005-06-25 14:54:29 -0700862 ch->w_remote_GP.put = ch->remote_GP.put;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700863
864 dev_dbg(xpc_chan, "w_remote_GP.put changed to %ld, partid=%d, "
865 "channel=%d\n", ch->w_remote_GP.put, ch->partid,
866 ch->number);
867
868 nmsgs_sent = ch->w_remote_GP.put - ch->w_local_GP.get;
869 if (nmsgs_sent > 0) {
870 dev_dbg(xpc_chan, "msgs waiting to be copied and "
871 "delivered=%d, partid=%d, channel=%d\n",
872 nmsgs_sent, ch->partid, ch->number);
873
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500874 if (ch->flags & XPC_C_CONNECTEDCALLOUT_MADE)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700875 xpc_activate_kthreads(ch, nmsgs_sent);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700876 }
877 }
878
879 xpc_msgqueue_deref(ch);
880}
881
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700882void
883xpc_process_channel_activity(struct xpc_partition *part)
884{
885 unsigned long irq_flags;
886 u64 IPI_amo, IPI_flags;
887 struct xpc_channel *ch;
888 int ch_number;
Dean Nelsona607c3892005-09-01 14:01:37 -0500889 u32 ch_flags;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700890
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700891 IPI_amo = xpc_get_IPI_flags(part);
892
893 /*
894 * Initiate channel connections for registered channels.
895 *
896 * For each connected channel that has pending messages activate idle
897 * kthreads and/or create new kthreads as needed.
898 */
899
900 for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
901 ch = &part->channels[ch_number];
902
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700903 /*
904 * Process any open or close related IPI flags, and then deal
905 * with connecting or disconnecting the channel as required.
906 */
907
908 IPI_flags = XPC_GET_IPI_FLAGS(IPI_amo, ch_number);
909
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500910 if (XPC_ANY_OPENCLOSE_IPI_FLAGS_SET(IPI_flags))
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700911 xpc_process_openclose_IPI(part, ch_number, IPI_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700912
Dean Nelsona607c3892005-09-01 14:01:37 -0500913 ch_flags = ch->flags; /* need an atomic snapshot of flags */
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700914
Dean Nelsona607c3892005-09-01 14:01:37 -0500915 if (ch_flags & XPC_C_DISCONNECTING) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700916 spin_lock_irqsave(&ch->lock, irq_flags);
917 xpc_process_disconnect(ch, &irq_flags);
918 spin_unlock_irqrestore(&ch->lock, irq_flags);
919 continue;
920 }
921
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500922 if (part->act_state == XPC_P_DEACTIVATING)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700923 continue;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700924
Dean Nelsona607c3892005-09-01 14:01:37 -0500925 if (!(ch_flags & XPC_C_CONNECTED)) {
926 if (!(ch_flags & XPC_C_OPENREQUEST)) {
927 DBUG_ON(ch_flags & XPC_C_SETUP);
Dean Nelson35190502008-04-22 14:48:55 -0500928 (void)xpc_connect_channel(ch);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700929 } else {
930 spin_lock_irqsave(&ch->lock, irq_flags);
931 xpc_process_connect(ch, &irq_flags);
932 spin_unlock_irqrestore(&ch->lock, irq_flags);
933 }
934 continue;
935 }
936
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700937 /*
938 * Process any message related IPI flags, this may involve the
939 * activation of kthreads to deliver any pending messages sent
940 * from the other partition.
941 */
942
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500943 if (XPC_ANY_MSG_IPI_FLAGS_SET(IPI_flags))
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700944 xpc_process_msg_IPI(part, ch_number);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700945 }
946}
947
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700948/*
Dean Nelsona607c3892005-09-01 14:01:37 -0500949 * XPC's heartbeat code calls this function to inform XPC that a partition is
950 * going down. XPC responds by tearing down the XPartition Communication
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700951 * infrastructure used for the just downed partition.
952 *
953 * XPC's heartbeat code will never call this function and xpc_partition_up()
954 * at the same time. Nor will it ever make multiple calls to either function
955 * at the same time.
956 */
957void
Dean Nelson65c17b82008-05-12 14:02:02 -0700958xpc_partition_going_down(struct xpc_partition *part, enum xp_retval reason)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700959{
960 unsigned long irq_flags;
961 int ch_number;
962 struct xpc_channel *ch;
963
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700964 dev_dbg(xpc_chan, "deactivating partition %d, reason=%d\n",
965 XPC_PARTID(part), reason);
966
967 if (!xpc_part_ref(part)) {
968 /* infrastructure for this partition isn't currently set up */
969 return;
970 }
971
Dean Nelsona607c3892005-09-01 14:01:37 -0500972 /* disconnect channels associated with the partition going down */
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700973
974 for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
975 ch = &part->channels[ch_number];
976
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700977 xpc_msgqueue_ref(ch);
978 spin_lock_irqsave(&ch->lock, irq_flags);
979
980 XPC_DISCONNECT_CHANNEL(ch, reason, &irq_flags);
981
982 spin_unlock_irqrestore(&ch->lock, irq_flags);
983 xpc_msgqueue_deref(ch);
984 }
985
986 xpc_wakeup_channel_mgr(part);
987
988 xpc_part_deref(part);
989}
990
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700991/*
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700992 * Called by XP at the time of channel connection registration to cause
993 * XPC to establish connections to all currently active partitions.
994 */
995void
996xpc_initiate_connect(int ch_number)
997{
Dean Nelson64d032b2008-05-12 14:02:03 -0700998 short partid;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700999 struct xpc_partition *part;
1000 struct xpc_channel *ch;
1001
Dean Nelsonbc63d382008-07-29 22:34:04 -07001002 DBUG_ON(ch_number < 0 || ch_number >= XPC_MAX_NCHANNELS);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001003
Dean Nelsonbc63d382008-07-29 22:34:04 -07001004 for (partid = 0; partid < xp_max_npartitions; partid++) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001005 part = &xpc_partitions[partid];
1006
1007 if (xpc_part_ref(part)) {
1008 ch = &part->channels[ch_number];
1009
Dean Nelsone54af722005-10-25 14:07:43 -05001010 /*
1011 * Initiate the establishment of a connection on the
1012 * newly registered channel to the remote partition.
1013 */
1014 xpc_wakeup_channel_mgr(part);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001015 xpc_part_deref(part);
1016 }
1017 }
1018}
1019
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001020void
1021xpc_connected_callout(struct xpc_channel *ch)
1022{
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001023 /* let the registerer know that a connection has been established */
1024
1025 if (ch->func != NULL) {
Dean Nelson65c17b82008-05-12 14:02:02 -07001026 dev_dbg(xpc_chan, "ch->func() called, reason=xpConnected, "
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001027 "partid=%d, channel=%d\n", ch->partid, ch->number);
1028
Dean Nelson65c17b82008-05-12 14:02:02 -07001029 ch->func(xpConnected, ch->partid, ch->number,
Dean Nelson35190502008-04-22 14:48:55 -05001030 (void *)(u64)ch->local_nentries, ch->key);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001031
Dean Nelson65c17b82008-05-12 14:02:02 -07001032 dev_dbg(xpc_chan, "ch->func() returned, reason=xpConnected, "
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001033 "partid=%d, channel=%d\n", ch->partid, ch->number);
1034 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001035}
1036
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001037/*
1038 * Called by XP at the time of channel connection unregistration to cause
1039 * XPC to teardown all current connections for the specified channel.
1040 *
1041 * Before returning xpc_initiate_disconnect() will wait until all connections
1042 * on the specified channel have been closed/torndown. So the caller can be
1043 * assured that they will not be receiving any more callouts from XPC to the
1044 * function they registered via xpc_connect().
1045 *
1046 * Arguments:
1047 *
1048 * ch_number - channel # to unregister.
1049 */
1050void
1051xpc_initiate_disconnect(int ch_number)
1052{
1053 unsigned long irq_flags;
Dean Nelson64d032b2008-05-12 14:02:03 -07001054 short partid;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001055 struct xpc_partition *part;
1056 struct xpc_channel *ch;
1057
Dean Nelsonbc63d382008-07-29 22:34:04 -07001058 DBUG_ON(ch_number < 0 || ch_number >= XPC_MAX_NCHANNELS);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001059
1060 /* initiate the channel disconnect for every active partition */
Dean Nelsonbc63d382008-07-29 22:34:04 -07001061 for (partid = 0; partid < xp_max_npartitions; partid++) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001062 part = &xpc_partitions[partid];
1063
1064 if (xpc_part_ref(part)) {
1065 ch = &part->channels[ch_number];
1066 xpc_msgqueue_ref(ch);
1067
1068 spin_lock_irqsave(&ch->lock, irq_flags);
1069
Dean Nelsona607c3892005-09-01 14:01:37 -05001070 if (!(ch->flags & XPC_C_DISCONNECTED)) {
1071 ch->flags |= XPC_C_WDISCONNECT;
1072
Dean Nelson65c17b82008-05-12 14:02:02 -07001073 XPC_DISCONNECT_CHANNEL(ch, xpUnregistering,
Dean Nelson35190502008-04-22 14:48:55 -05001074 &irq_flags);
Dean Nelsona607c3892005-09-01 14:01:37 -05001075 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001076
1077 spin_unlock_irqrestore(&ch->lock, irq_flags);
1078
1079 xpc_msgqueue_deref(ch);
1080 xpc_part_deref(part);
1081 }
1082 }
1083
1084 xpc_disconnect_wait(ch_number);
1085}
1086
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001087/*
1088 * To disconnect a channel, and reflect it back to all who may be waiting.
1089 *
Dean Nelsona607c3892005-09-01 14:01:37 -05001090 * An OPEN is not allowed until XPC_C_DISCONNECTING is cleared by
1091 * xpc_process_disconnect(), and if set, XPC_C_WDISCONNECT is cleared by
1092 * xpc_disconnect_wait().
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001093 *
1094 * THE CHANNEL IS TO BE LOCKED BY THE CALLER AND WILL REMAIN LOCKED UPON RETURN.
1095 */
1096void
1097xpc_disconnect_channel(const int line, struct xpc_channel *ch,
Dean Nelson65c17b82008-05-12 14:02:02 -07001098 enum xp_retval reason, unsigned long *irq_flags)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001099{
Dean Nelsona607c3892005-09-01 14:01:37 -05001100 u32 channel_was_connected = (ch->flags & XPC_C_CONNECTED);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001101
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001102 DBUG_ON(!spin_is_locked(&ch->lock));
1103
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001104 if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_DISCONNECTED))
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001105 return;
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001106
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001107 DBUG_ON(!(ch->flags & (XPC_C_CONNECTING | XPC_C_CONNECTED)));
1108
1109 dev_dbg(xpc_chan, "reason=%d, line=%d, partid=%d, channel=%d\n",
1110 reason, line, ch->partid, ch->number);
1111
1112 XPC_SET_REASON(ch, reason, line);
1113
Dean Nelsona607c3892005-09-01 14:01:37 -05001114 ch->flags |= (XPC_C_CLOSEREQUEST | XPC_C_DISCONNECTING);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001115 /* some of these may not have been set */
1116 ch->flags &= ~(XPC_C_OPENREQUEST | XPC_C_OPENREPLY |
Dean Nelson35190502008-04-22 14:48:55 -05001117 XPC_C_ROPENREQUEST | XPC_C_ROPENREPLY |
1118 XPC_C_CONNECTING | XPC_C_CONNECTED);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001119
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001120 xpc_IPI_send_closerequest(ch, irq_flags);
1121
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001122 if (channel_was_connected)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001123 ch->flags |= XPC_C_WASCONNECTED;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001124
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001125 spin_unlock_irqrestore(&ch->lock, *irq_flags);
1126
Dean Nelsona607c3892005-09-01 14:01:37 -05001127 /* wake all idle kthreads so they can exit */
1128 if (atomic_read(&ch->kthreads_idle) > 0) {
1129 wake_up_all(&ch->idle_wq);
Dean Nelsona460ef82006-11-22 08:25:00 -06001130
1131 } else if ((ch->flags & XPC_C_CONNECTEDCALLOUT_MADE) &&
Dean Nelson35190502008-04-22 14:48:55 -05001132 !(ch->flags & XPC_C_DISCONNECTINGCALLOUT)) {
Dean Nelson65c17b82008-05-12 14:02:02 -07001133 /* start a kthread that will do the xpDisconnecting callout */
Dean Nelsona460ef82006-11-22 08:25:00 -06001134 xpc_create_kthreads(ch, 1, 1);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001135 }
1136
Dean Nelsona607c3892005-09-01 14:01:37 -05001137 /* wake those waiting to allocate an entry from the local msg queue */
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001138 if (atomic_read(&ch->n_on_msg_allocate_wq) > 0)
Dean Nelsona607c3892005-09-01 14:01:37 -05001139 wake_up(&ch->msg_allocate_wq);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001140
1141 spin_lock_irqsave(&ch->lock, *irq_flags);
1142}
1143
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001144void
Dean Nelson65c17b82008-05-12 14:02:02 -07001145xpc_disconnect_callout(struct xpc_channel *ch, enum xp_retval reason)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001146{
1147 /*
Dean Nelsona607c3892005-09-01 14:01:37 -05001148 * Let the channel's registerer know that the channel is being
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001149 * disconnected. We don't want to do this if the registerer was never
Dean Nelsona607c3892005-09-01 14:01:37 -05001150 * informed of a connection being made.
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001151 */
1152
1153 if (ch->func != NULL) {
Dean Nelson246c7e32005-12-22 14:32:56 -06001154 dev_dbg(xpc_chan, "ch->func() called, reason=%d, partid=%d, "
1155 "channel=%d\n", reason, ch->partid, ch->number);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001156
Dean Nelson246c7e32005-12-22 14:32:56 -06001157 ch->func(reason, ch->partid, ch->number, NULL, ch->key);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001158
Dean Nelson246c7e32005-12-22 14:32:56 -06001159 dev_dbg(xpc_chan, "ch->func() returned, reason=%d, partid=%d, "
1160 "channel=%d\n", reason, ch->partid, ch->number);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001161 }
1162}
1163
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001164/*
1165 * Wait for a message entry to become available for the specified channel,
1166 * but don't wait any longer than 1 jiffy.
1167 */
Dean Nelson33ba3c72008-07-29 22:34:07 -07001168enum xp_retval
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001169xpc_allocate_msg_wait(struct xpc_channel *ch)
1170{
Dean Nelson65c17b82008-05-12 14:02:02 -07001171 enum xp_retval ret;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001172
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001173 if (ch->flags & XPC_C_DISCONNECTING) {
Dean Nelson65c17b82008-05-12 14:02:02 -07001174 DBUG_ON(ch->reason == xpInterrupted);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001175 return ch->reason;
1176 }
1177
1178 atomic_inc(&ch->n_on_msg_allocate_wq);
1179 ret = interruptible_sleep_on_timeout(&ch->msg_allocate_wq, 1);
1180 atomic_dec(&ch->n_on_msg_allocate_wq);
1181
1182 if (ch->flags & XPC_C_DISCONNECTING) {
1183 ret = ch->reason;
Dean Nelson65c17b82008-05-12 14:02:02 -07001184 DBUG_ON(ch->reason == xpInterrupted);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001185 } else if (ret == 0) {
Dean Nelson65c17b82008-05-12 14:02:02 -07001186 ret = xpTimeout;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001187 } else {
Dean Nelson65c17b82008-05-12 14:02:02 -07001188 ret = xpInterrupted;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001189 }
1190
1191 return ret;
1192}
1193
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001194/*
Dean Nelson97bf1aa2008-07-29 22:34:08 -07001195 * Send a message that contains the user's payload on the specified channel
1196 * connected to the specified partition.
1197 *
1198 * NOTE that this routine can sleep waiting for a message entry to become
1199 * available. To not sleep, pass in the XPC_NOWAIT flag.
1200 *
1201 * Once sent, this routine will not wait for the message to be received, nor
1202 * will notification be given when it does happen.
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001203 *
1204 * Arguments:
1205 *
1206 * partid - ID of partition to which the channel is connected.
Dean Nelson97bf1aa2008-07-29 22:34:08 -07001207 * ch_number - channel # to send message on.
1208 * flags - see xp.h for valid flags.
1209 * payload - pointer to the payload which is to be sent.
1210 * payload_size - size of the payload in bytes.
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001211 */
Dean Nelson65c17b82008-05-12 14:02:02 -07001212enum xp_retval
Dean Nelson97bf1aa2008-07-29 22:34:08 -07001213xpc_initiate_send(short partid, int ch_number, u32 flags, void *payload,
1214 u16 payload_size)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001215{
1216 struct xpc_partition *part = &xpc_partitions[partid];
Dean Nelson65c17b82008-05-12 14:02:02 -07001217 enum xp_retval ret = xpUnknownReason;
Dean Nelson97bf1aa2008-07-29 22:34:08 -07001218
1219 dev_dbg(xpc_chan, "payload=0x%p, partid=%d, channel=%d\n", payload,
1220 partid, ch_number);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001221
Dean Nelsonbc63d382008-07-29 22:34:04 -07001222 DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001223 DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
Dean Nelson97bf1aa2008-07-29 22:34:08 -07001224 DBUG_ON(payload == NULL);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001225
1226 if (xpc_part_ref(part)) {
Dean Nelson97bf1aa2008-07-29 22:34:08 -07001227 ret = xpc_send_msg(&part->channels[ch_number], flags, payload,
1228 payload_size, 0, NULL, NULL);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001229 xpc_part_deref(part);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001230 }
1231
1232 return ret;
1233}
1234
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001235/*
Dean Nelson97bf1aa2008-07-29 22:34:08 -07001236 * Send a message that contains the user's payload on the specified channel
1237 * connected to the specified partition.
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001238 *
Dean Nelson97bf1aa2008-07-29 22:34:08 -07001239 * NOTE that this routine can sleep waiting for a message entry to become
1240 * available. To not sleep, pass in the XPC_NOWAIT flag.
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001241 *
Dean Nelson97bf1aa2008-07-29 22:34:08 -07001242 * This routine will not wait for the message to be sent or received.
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001243 *
1244 * Once the remote end of the channel has received the message, the function
1245 * passed as an argument to xpc_initiate_send_notify() will be called. This
1246 * allows the sender to free up or re-use any buffers referenced by the
1247 * message, but does NOT mean the message has been processed at the remote
1248 * end by a receiver.
1249 *
1250 * If this routine returns an error, the caller's function will NOT be called.
1251 *
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001252 * Arguments:
1253 *
1254 * partid - ID of partition to which the channel is connected.
1255 * ch_number - channel # to send message on.
Dean Nelson97bf1aa2008-07-29 22:34:08 -07001256 * flags - see xp.h for valid flags.
1257 * payload - pointer to the payload which is to be sent.
1258 * payload_size - size of the payload in bytes.
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001259 * func - function to call with asynchronous notification of message
1260 * receipt. THIS FUNCTION MUST BE NON-BLOCKING.
1261 * key - user-defined key to be passed to the function when it's called.
1262 */
Dean Nelson65c17b82008-05-12 14:02:02 -07001263enum xp_retval
Dean Nelson97bf1aa2008-07-29 22:34:08 -07001264xpc_initiate_send_notify(short partid, int ch_number, u32 flags, void *payload,
1265 u16 payload_size, xpc_notify_func func, void *key)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001266{
1267 struct xpc_partition *part = &xpc_partitions[partid];
Dean Nelson97bf1aa2008-07-29 22:34:08 -07001268 enum xp_retval ret = xpUnknownReason;
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001269
Dean Nelson97bf1aa2008-07-29 22:34:08 -07001270 dev_dbg(xpc_chan, "payload=0x%p, partid=%d, channel=%d\n", payload,
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001271 partid, ch_number);
1272
Dean Nelsonbc63d382008-07-29 22:34:04 -07001273 DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001274 DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
Dean Nelson97bf1aa2008-07-29 22:34:08 -07001275 DBUG_ON(payload == NULL);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001276 DBUG_ON(func == NULL);
1277
Dean Nelson97bf1aa2008-07-29 22:34:08 -07001278 if (xpc_part_ref(part)) {
1279 ret = xpc_send_msg(&part->channels[ch_number], flags, payload,
1280 payload_size, XPC_N_CALL, func, key);
1281 xpc_part_deref(part);
1282 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001283 return ret;
1284}
1285
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001286/*
1287 * Deliver a message to its intended recipient.
1288 */
1289void
1290xpc_deliver_msg(struct xpc_channel *ch)
1291{
1292 struct xpc_msg *msg;
1293
Dean Nelson2c2b94f2008-04-22 14:50:17 -05001294 msg = xpc_get_deliverable_msg(ch);
1295 if (msg != NULL) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001296
1297 /*
1298 * This ref is taken to protect the payload itself from being
1299 * freed before the user is finished with it, which the user
1300 * indicates by calling xpc_initiate_received().
1301 */
1302 xpc_msgqueue_ref(ch);
1303
1304 atomic_inc(&ch->kthreads_active);
1305
1306 if (ch->func != NULL) {
1307 dev_dbg(xpc_chan, "ch->func() called, msg=0x%p, "
1308 "msg_number=%ld, partid=%d, channel=%d\n",
Dean Nelson35190502008-04-22 14:48:55 -05001309 (void *)msg, msg->number, ch->partid,
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001310 ch->number);
1311
1312 /* deliver the message to its intended recipient */
Dean Nelson65c17b82008-05-12 14:02:02 -07001313 ch->func(xpMsgReceived, ch->partid, ch->number,
Dean Nelson35190502008-04-22 14:48:55 -05001314 &msg->payload, ch->key);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001315
1316 dev_dbg(xpc_chan, "ch->func() returned, msg=0x%p, "
1317 "msg_number=%ld, partid=%d, channel=%d\n",
Dean Nelson35190502008-04-22 14:48:55 -05001318 (void *)msg, msg->number, ch->partid,
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001319 ch->number);
1320 }
1321
1322 atomic_dec(&ch->kthreads_active);
1323 }
1324}
1325
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001326/*
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001327 * Acknowledge receipt of a delivered message.
1328 *
1329 * If a message has XPC_M_INTERRUPT set, send an interrupt to the partition
1330 * that sent the message.
1331 *
1332 * This function, although called by users, does not call xpc_part_ref() to
1333 * ensure that the partition infrastructure is in place. It relies on the
1334 * fact that we called xpc_msgqueue_ref() in xpc_deliver_msg().
1335 *
1336 * Arguments:
1337 *
1338 * partid - ID of partition to which the channel is connected.
1339 * ch_number - channel # message received on.
1340 * payload - pointer to the payload area allocated via
Dean Nelson97bf1aa2008-07-29 22:34:08 -07001341 * xpc_initiate_send() or xpc_initiate_send_notify().
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001342 */
1343void
Dean Nelson64d032b2008-05-12 14:02:03 -07001344xpc_initiate_received(short partid, int ch_number, void *payload)
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001345{
1346 struct xpc_partition *part = &xpc_partitions[partid];
1347 struct xpc_channel *ch;
1348 struct xpc_msg *msg = XPC_MSG_ADDRESS(payload);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001349
Dean Nelsonbc63d382008-07-29 22:34:04 -07001350 DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001351 DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
1352
1353 ch = &part->channels[ch_number];
Dean Nelson33ba3c72008-07-29 22:34:07 -07001354 xpc_received_msg(ch, msg);
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001355
1356 /* the call to xpc_msgqueue_ref() was done by xpc_deliver_msg() */
1357 xpc_msgqueue_deref(ch);
1358}