blob: 9cd2ebe2a3b68be08e61c3afdff17702fe1442e4 [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 Nelson261f3b42008-07-29 22:34:16 -070017#include <linux/device.h>
Dean Nelson45d9ca42008-04-22 14:46:56 -050018#include "xpc.h"
Dean Nelson89eb8eb2005-03-23 19:50:00 -070019
Dean Nelson89eb8eb2005-03-23 19:50:00 -070020/*
Dean Nelson89eb8eb2005-03-23 19:50:00 -070021 * Process a connect message from a remote partition.
22 *
23 * Note: xpc_process_connect() is expecting to be called with the
24 * spin_lock_irqsave held and will leave it locked upon return.
25 */
26static void
27xpc_process_connect(struct xpc_channel *ch, unsigned long *irq_flags)
28{
Dean Nelson65c17b82008-05-12 14:02:02 -070029 enum xp_retval ret;
Dean Nelson89eb8eb2005-03-23 19:50:00 -070030
Dean Nelson89eb8eb2005-03-23 19:50:00 -070031 DBUG_ON(!spin_is_locked(&ch->lock));
32
33 if (!(ch->flags & XPC_C_OPENREQUEST) ||
Dean Nelson35190502008-04-22 14:48:55 -050034 !(ch->flags & XPC_C_ROPENREQUEST)) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -070035 /* nothing more to do for now */
36 return;
37 }
38 DBUG_ON(!(ch->flags & XPC_C_CONNECTING));
39
40 if (!(ch->flags & XPC_C_SETUP)) {
41 spin_unlock_irqrestore(&ch->lock, *irq_flags);
Dean Nelson5b8669d2008-07-29 22:34:18 -070042 ret = xpc_setup_msg_structures(ch);
Dean Nelson89eb8eb2005-03-23 19:50:00 -070043 spin_lock_irqsave(&ch->lock, *irq_flags);
44
Dean Nelson65c17b82008-05-12 14:02:02 -070045 if (ret != xpSuccess)
Dean Nelson89eb8eb2005-03-23 19:50:00 -070046 XPC_DISCONNECT_CHANNEL(ch, ret, irq_flags);
Dean Nelson2c2b94f2008-04-22 14:50:17 -050047
Dean Nelson185c3a12008-07-29 22:34:11 -070048 ch->flags |= XPC_C_SETUP;
49
Dean Nelson2c2b94f2008-04-22 14:50:17 -050050 if (ch->flags & (XPC_C_CONNECTED | XPC_C_DISCONNECTING))
Dean Nelson89eb8eb2005-03-23 19:50:00 -070051 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -070052
Dean Nelson89eb8eb2005-03-23 19:50:00 -070053 DBUG_ON(ch->local_msgqueue == NULL);
54 DBUG_ON(ch->remote_msgqueue == NULL);
55 }
56
57 if (!(ch->flags & XPC_C_OPENREPLY)) {
58 ch->flags |= XPC_C_OPENREPLY;
Dean Nelson7fb5e592008-07-29 22:34:10 -070059 xpc_send_chctl_openreply(ch, irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -070060 }
61
Dean Nelson2c2b94f2008-04-22 14:50:17 -050062 if (!(ch->flags & XPC_C_ROPENREPLY))
Dean Nelson89eb8eb2005-03-23 19:50:00 -070063 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -070064
Dean Nelson89eb8eb2005-03-23 19:50:00 -070065 ch->flags = (XPC_C_CONNECTED | XPC_C_SETUP); /* clear all else */
66
67 dev_info(xpc_chan, "channel %d to partition %d connected\n",
Dean Nelson35190502008-04-22 14:48:55 -050068 ch->number, ch->partid);
Dean Nelson89eb8eb2005-03-23 19:50:00 -070069
70 spin_unlock_irqrestore(&ch->lock, *irq_flags);
Dean Nelsona460ef82006-11-22 08:25:00 -060071 xpc_create_kthreads(ch, 1, 0);
Dean Nelson89eb8eb2005-03-23 19:50:00 -070072 spin_lock_irqsave(&ch->lock, *irq_flags);
73}
74
Dean Nelson89eb8eb2005-03-23 19:50:00 -070075/*
Dean Nelson89eb8eb2005-03-23 19:50:00 -070076 * spin_lock_irqsave() is expected to be held on entry.
77 */
78static void
79xpc_process_disconnect(struct xpc_channel *ch, unsigned long *irq_flags)
80{
81 struct xpc_partition *part = &xpc_partitions[ch->partid];
Dean Nelsona607c3892005-09-01 14:01:37 -050082 u32 channel_was_connected = (ch->flags & XPC_C_WASCONNECTED);
Dean Nelson89eb8eb2005-03-23 19:50:00 -070083
Dean Nelson89eb8eb2005-03-23 19:50:00 -070084 DBUG_ON(!spin_is_locked(&ch->lock));
85
Dean Nelson2c2b94f2008-04-22 14:50:17 -050086 if (!(ch->flags & XPC_C_DISCONNECTING))
Dean Nelson89eb8eb2005-03-23 19:50:00 -070087 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -070088
89 DBUG_ON(!(ch->flags & XPC_C_CLOSEREQUEST));
90
91 /* make sure all activity has settled down first */
92
Dean Nelsona460ef82006-11-22 08:25:00 -060093 if (atomic_read(&ch->kthreads_assigned) > 0 ||
Dean Nelson35190502008-04-22 14:48:55 -050094 atomic_read(&ch->references) > 0) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -070095 return;
96 }
Dean Nelsona460ef82006-11-22 08:25:00 -060097 DBUG_ON((ch->flags & XPC_C_CONNECTEDCALLOUT_MADE) &&
Dean Nelson35190502008-04-22 14:48:55 -050098 !(ch->flags & XPC_C_DISCONNECTINGCALLOUT_MADE));
Dean Nelson89eb8eb2005-03-23 19:50:00 -070099
Dean Nelson83469b52008-07-29 22:34:18 -0700100 if (part->act_state == XPC_P_AS_DEACTIVATING) {
Dean Nelsona607c3892005-09-01 14:01:37 -0500101 /* can't proceed until the other side disengages from us */
Dean Nelsona47d5da2008-07-29 22:34:09 -0700102 if (xpc_partition_engaged(ch->partid))
Dean Nelsona607c3892005-09-01 14:01:37 -0500103 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700104
Dean Nelsona607c3892005-09-01 14:01:37 -0500105 } else {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700106
107 /* as long as the other side is up do the full protocol */
108
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500109 if (!(ch->flags & XPC_C_RCLOSEREQUEST))
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700110 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700111
112 if (!(ch->flags & XPC_C_CLOSEREPLY)) {
113 ch->flags |= XPC_C_CLOSEREPLY;
Dean Nelson7fb5e592008-07-29 22:34:10 -0700114 xpc_send_chctl_closereply(ch, irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700115 }
116
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500117 if (!(ch->flags & XPC_C_RCLOSEREPLY))
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700118 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700119 }
120
Dean Nelsona607c3892005-09-01 14:01:37 -0500121 /* wake those waiting for notify completion */
122 if (atomic_read(&ch->n_to_notify) > 0) {
Dean Nelsonea57f802008-07-29 22:34:14 -0700123 /* we do callout while holding ch->lock, callout can't block */
Dean Nelsona47d5da2008-07-29 22:34:09 -0700124 xpc_notify_senders_of_disconnect(ch);
Dean Nelsona607c3892005-09-01 14:01:37 -0500125 }
126
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700127 /* both sides are disconnected now */
128
Dean Nelson4c2cd962006-02-15 08:02:21 -0600129 if (ch->flags & XPC_C_DISCONNECTINGCALLOUT_MADE) {
Dean Nelson246c7e32005-12-22 14:32:56 -0600130 spin_unlock_irqrestore(&ch->lock, *irq_flags);
Dean Nelson65c17b82008-05-12 14:02:02 -0700131 xpc_disconnect_callout(ch, xpDisconnected);
Dean Nelson246c7e32005-12-22 14:32:56 -0600132 spin_lock_irqsave(&ch->lock, *irq_flags);
133 }
134
Dean Nelson5b8669d2008-07-29 22:34:18 -0700135 DBUG_ON(atomic_read(&ch->n_to_notify) != 0);
136
Dean Nelsona607c3892005-09-01 14:01:37 -0500137 /* it's now safe to free the channel's message queues */
Dean Nelson5b8669d2008-07-29 22:34:18 -0700138 xpc_teardown_msg_structures(ch);
139
140 ch->func = NULL;
141 ch->key = NULL;
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700142 ch->entry_size = 0;
Dean Nelson5b8669d2008-07-29 22:34:18 -0700143 ch->local_nentries = 0;
144 ch->remote_nentries = 0;
145 ch->kthreads_assigned_limit = 0;
146 ch->kthreads_idle_limit = 0;
Dean Nelsona607c3892005-09-01 14:01:37 -0500147
Dean Nelson185c3a12008-07-29 22:34:11 -0700148 /*
149 * Mark the channel disconnected and clear all other flags, including
Dean Nelson5b8669d2008-07-29 22:34:18 -0700150 * XPC_C_SETUP (because of call to xpc_teardown_msg_structures()) but
151 * not including XPC_C_WDISCONNECT (if it was set).
Dean Nelson185c3a12008-07-29 22:34:11 -0700152 */
Dean Nelsona607c3892005-09-01 14:01:37 -0500153 ch->flags = (XPC_C_DISCONNECTED | (ch->flags & XPC_C_WDISCONNECT));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700154
155 atomic_dec(&part->nchannels_active);
156
Dean Nelsona607c3892005-09-01 14:01:37 -0500157 if (channel_was_connected) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700158 dev_info(xpc_chan, "channel %d to partition %d disconnected, "
Dean Nelson35190502008-04-22 14:48:55 -0500159 "reason=%d\n", ch->number, ch->partid, ch->reason);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700160 }
Dean Nelsona607c3892005-09-01 14:01:37 -0500161
Dean Nelsona607c3892005-09-01 14:01:37 -0500162 if (ch->flags & XPC_C_WDISCONNECT) {
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500163 /* we won't lose the CPU since we're holding ch->lock */
164 complete(&ch->wdisconnect_wait);
Dean Nelson7fb5e592008-07-29 22:34:10 -0700165 } else if (ch->delayed_chctl_flags) {
Dean Nelson83469b52008-07-29 22:34:18 -0700166 if (part->act_state != XPC_P_AS_DEACTIVATING) {
Dean Nelson7fb5e592008-07-29 22:34:10 -0700167 /* time to take action on any delayed chctl flags */
168 spin_lock(&part->chctl_lock);
169 part->chctl.flags[ch->number] |=
170 ch->delayed_chctl_flags;
171 spin_unlock(&part->chctl_lock);
Dean Nelsone54af722005-10-25 14:07:43 -0500172 }
Dean Nelson7fb5e592008-07-29 22:34:10 -0700173 ch->delayed_chctl_flags = 0;
Dean Nelsona607c3892005-09-01 14:01:37 -0500174 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700175}
176
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700177/*
178 * Process a change in the channel's remote connection state.
179 */
180static void
Dean Nelson7fb5e592008-07-29 22:34:10 -0700181xpc_process_openclose_chctl_flags(struct xpc_partition *part, int ch_number,
182 u8 chctl_flags)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700183{
184 unsigned long irq_flags;
185 struct xpc_openclose_args *args =
Dean Nelson35190502008-04-22 14:48:55 -0500186 &part->remote_openclose_args[ch_number];
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700187 struct xpc_channel *ch = &part->channels[ch_number];
Dean Nelson65c17b82008-05-12 14:02:02 -0700188 enum xp_retval reason;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700189
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700190 spin_lock_irqsave(&ch->lock, irq_flags);
191
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500192again:
Dean Nelsone54af722005-10-25 14:07:43 -0500193
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500194 if ((ch->flags & XPC_C_DISCONNECTED) &&
195 (ch->flags & XPC_C_WDISCONNECT)) {
Dean Nelsone54af722005-10-25 14:07:43 -0500196 /*
Dean Nelson7fb5e592008-07-29 22:34:10 -0700197 * Delay processing chctl flags until thread waiting disconnect
Dean Nelsone54af722005-10-25 14:07:43 -0500198 * has had a chance to see that the channel is disconnected.
199 */
Dean Nelson7fb5e592008-07-29 22:34:10 -0700200 ch->delayed_chctl_flags |= chctl_flags;
Dean Nelsone54af722005-10-25 14:07:43 -0500201 spin_unlock_irqrestore(&ch->lock, irq_flags);
202 return;
203 }
204
Dean Nelson7fb5e592008-07-29 22:34:10 -0700205 if (chctl_flags & XPC_CHCTL_CLOSEREQUEST) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700206
Dean Nelson7fb5e592008-07-29 22:34:10 -0700207 dev_dbg(xpc_chan, "XPC_CHCTL_CLOSEREQUEST (reason=%d) received "
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700208 "from partid=%d, channel=%d\n", args->reason,
209 ch->partid, ch->number);
210
211 /*
212 * If RCLOSEREQUEST is set, we're probably waiting for
213 * RCLOSEREPLY. We should find it and a ROPENREQUEST packed
Dean Nelson7fb5e592008-07-29 22:34:10 -0700214 * with this RCLOSEREQUEST in the chctl_flags.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700215 */
216
217 if (ch->flags & XPC_C_RCLOSEREQUEST) {
218 DBUG_ON(!(ch->flags & XPC_C_DISCONNECTING));
219 DBUG_ON(!(ch->flags & XPC_C_CLOSEREQUEST));
220 DBUG_ON(!(ch->flags & XPC_C_CLOSEREPLY));
221 DBUG_ON(ch->flags & XPC_C_RCLOSEREPLY);
222
Dean Nelson7fb5e592008-07-29 22:34:10 -0700223 DBUG_ON(!(chctl_flags & XPC_CHCTL_CLOSEREPLY));
224 chctl_flags &= ~XPC_CHCTL_CLOSEREPLY;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700225 ch->flags |= XPC_C_RCLOSEREPLY;
226
227 /* both sides have finished disconnecting */
228 xpc_process_disconnect(ch, &irq_flags);
Dean Nelsone54af722005-10-25 14:07:43 -0500229 DBUG_ON(!(ch->flags & XPC_C_DISCONNECTED));
230 goto again;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700231 }
232
233 if (ch->flags & XPC_C_DISCONNECTED) {
Dean Nelson7fb5e592008-07-29 22:34:10 -0700234 if (!(chctl_flags & XPC_CHCTL_OPENREQUEST)) {
235 if (part->chctl.flags[ch_number] &
236 XPC_CHCTL_OPENREQUEST) {
Dean Nelsone54af722005-10-25 14:07:43 -0500237
Dean Nelson7fb5e592008-07-29 22:34:10 -0700238 DBUG_ON(ch->delayed_chctl_flags != 0);
239 spin_lock(&part->chctl_lock);
240 part->chctl.flags[ch_number] |=
241 XPC_CHCTL_CLOSEREQUEST;
242 spin_unlock(&part->chctl_lock);
Dean Nelsone54af722005-10-25 14:07:43 -0500243 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700244 spin_unlock_irqrestore(&ch->lock, irq_flags);
245 return;
246 }
247
248 XPC_SET_REASON(ch, 0, 0);
249 ch->flags &= ~XPC_C_DISCONNECTED;
250
251 atomic_inc(&part->nchannels_active);
252 ch->flags |= (XPC_C_CONNECTING | XPC_C_ROPENREQUEST);
253 }
254
Dean Nelson7fb5e592008-07-29 22:34:10 -0700255 chctl_flags &= ~(XPC_CHCTL_OPENREQUEST | XPC_CHCTL_OPENREPLY);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700256
257 /*
258 * The meaningful CLOSEREQUEST connection state fields are:
259 * reason = reason connection is to be closed
260 */
261
262 ch->flags |= XPC_C_RCLOSEREQUEST;
263
264 if (!(ch->flags & XPC_C_DISCONNECTING)) {
265 reason = args->reason;
Dean Nelson65c17b82008-05-12 14:02:02 -0700266 if (reason <= xpSuccess || reason > xpUnknownReason)
267 reason = xpUnknownReason;
268 else if (reason == xpUnregistering)
269 reason = xpOtherUnregistering;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700270
271 XPC_DISCONNECT_CHANNEL(ch, reason, &irq_flags);
Dean Nelsone54af722005-10-25 14:07:43 -0500272
Dean Nelson7fb5e592008-07-29 22:34:10 -0700273 DBUG_ON(chctl_flags & XPC_CHCTL_CLOSEREPLY);
Dean Nelsone54af722005-10-25 14:07:43 -0500274 spin_unlock_irqrestore(&ch->lock, irq_flags);
275 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700276 }
Dean Nelsone54af722005-10-25 14:07:43 -0500277
278 xpc_process_disconnect(ch, &irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700279 }
280
Dean Nelson7fb5e592008-07-29 22:34:10 -0700281 if (chctl_flags & XPC_CHCTL_CLOSEREPLY) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700282
Dean Nelson7fb5e592008-07-29 22:34:10 -0700283 dev_dbg(xpc_chan, "XPC_CHCTL_CLOSEREPLY received from partid="
284 "%d, channel=%d\n", ch->partid, ch->number);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700285
286 if (ch->flags & XPC_C_DISCONNECTED) {
Dean Nelson83469b52008-07-29 22:34:18 -0700287 DBUG_ON(part->act_state != XPC_P_AS_DEACTIVATING);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700288 spin_unlock_irqrestore(&ch->lock, irq_flags);
289 return;
290 }
291
292 DBUG_ON(!(ch->flags & XPC_C_CLOSEREQUEST));
Dean Nelsone54af722005-10-25 14:07:43 -0500293
294 if (!(ch->flags & XPC_C_RCLOSEREQUEST)) {
Dean Nelson7fb5e592008-07-29 22:34:10 -0700295 if (part->chctl.flags[ch_number] &
296 XPC_CHCTL_CLOSEREQUEST) {
Dean Nelsone54af722005-10-25 14:07:43 -0500297
Dean Nelson7fb5e592008-07-29 22:34:10 -0700298 DBUG_ON(ch->delayed_chctl_flags != 0);
299 spin_lock(&part->chctl_lock);
300 part->chctl.flags[ch_number] |=
301 XPC_CHCTL_CLOSEREPLY;
302 spin_unlock(&part->chctl_lock);
Dean Nelsone54af722005-10-25 14:07:43 -0500303 }
304 spin_unlock_irqrestore(&ch->lock, irq_flags);
305 return;
306 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700307
308 ch->flags |= XPC_C_RCLOSEREPLY;
309
310 if (ch->flags & XPC_C_CLOSEREPLY) {
311 /* both sides have finished disconnecting */
312 xpc_process_disconnect(ch, &irq_flags);
313 }
314 }
315
Dean Nelson7fb5e592008-07-29 22:34:10 -0700316 if (chctl_flags & XPC_CHCTL_OPENREQUEST) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700317
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700318 dev_dbg(xpc_chan, "XPC_CHCTL_OPENREQUEST (entry_size=%d, "
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700319 "local_nentries=%d) received from partid=%d, "
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700320 "channel=%d\n", args->entry_size, args->local_nentries,
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700321 ch->partid, ch->number);
322
Dean Nelson83469b52008-07-29 22:34:18 -0700323 if (part->act_state == XPC_P_AS_DEACTIVATING ||
Dean Nelson35190502008-04-22 14:48:55 -0500324 (ch->flags & XPC_C_ROPENREQUEST)) {
Dean Nelsone54af722005-10-25 14:07:43 -0500325 spin_unlock_irqrestore(&ch->lock, irq_flags);
326 return;
327 }
328
329 if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_WDISCONNECT)) {
Dean Nelson7fb5e592008-07-29 22:34:10 -0700330 ch->delayed_chctl_flags |= XPC_CHCTL_OPENREQUEST;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700331 spin_unlock_irqrestore(&ch->lock, irq_flags);
332 return;
333 }
334 DBUG_ON(!(ch->flags & (XPC_C_DISCONNECTED |
Dean Nelson35190502008-04-22 14:48:55 -0500335 XPC_C_OPENREQUEST)));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700336 DBUG_ON(ch->flags & (XPC_C_ROPENREQUEST | XPC_C_ROPENREPLY |
Dean Nelson35190502008-04-22 14:48:55 -0500337 XPC_C_OPENREPLY | XPC_C_CONNECTED));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700338
339 /*
340 * The meaningful OPENREQUEST connection state fields are:
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700341 * entry_size = size of channel's messages in bytes
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700342 * local_nentries = remote partition's local_nentries
343 */
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700344 if (args->entry_size == 0 || args->local_nentries == 0) {
Dean Nelsone54af722005-10-25 14:07:43 -0500345 /* assume OPENREQUEST was delayed by mistake */
346 spin_unlock_irqrestore(&ch->lock, irq_flags);
347 return;
348 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700349
350 ch->flags |= (XPC_C_ROPENREQUEST | XPC_C_CONNECTING);
351 ch->remote_nentries = args->local_nentries;
352
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700353 if (ch->flags & XPC_C_OPENREQUEST) {
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700354 if (args->entry_size != ch->entry_size) {
Dean Nelson65c17b82008-05-12 14:02:02 -0700355 XPC_DISCONNECT_CHANNEL(ch, xpUnequalMsgSizes,
Dean Nelson35190502008-04-22 14:48:55 -0500356 &irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700357 spin_unlock_irqrestore(&ch->lock, irq_flags);
358 return;
359 }
360 } else {
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700361 ch->entry_size = args->entry_size;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700362
363 XPC_SET_REASON(ch, 0, 0);
364 ch->flags &= ~XPC_C_DISCONNECTED;
365
366 atomic_inc(&part->nchannels_active);
367 }
368
369 xpc_process_connect(ch, &irq_flags);
370 }
371
Dean Nelson7fb5e592008-07-29 22:34:10 -0700372 if (chctl_flags & XPC_CHCTL_OPENREPLY) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700373
Dean Nelson7fb5e592008-07-29 22:34:10 -0700374 dev_dbg(xpc_chan, "XPC_CHCTL_OPENREPLY (local_msgqueue_pa="
375 "0x%lx, local_nentries=%d, remote_nentries=%d) "
376 "received from partid=%d, channel=%d\n",
Dean Nelsona812dcc2008-07-29 22:34:16 -0700377 args->local_msgqueue_pa, args->local_nentries,
378 args->remote_nentries, ch->partid, ch->number);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700379
380 if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_DISCONNECTED)) {
381 spin_unlock_irqrestore(&ch->lock, irq_flags);
382 return;
383 }
Dean Nelsone54af722005-10-25 14:07:43 -0500384 if (!(ch->flags & XPC_C_OPENREQUEST)) {
Dean Nelson65c17b82008-05-12 14:02:02 -0700385 XPC_DISCONNECT_CHANNEL(ch, xpOpenCloseError,
Dean Nelson35190502008-04-22 14:48:55 -0500386 &irq_flags);
Dean Nelsone54af722005-10-25 14:07:43 -0500387 spin_unlock_irqrestore(&ch->lock, irq_flags);
388 return;
389 }
390
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700391 DBUG_ON(!(ch->flags & XPC_C_ROPENREQUEST));
392 DBUG_ON(ch->flags & XPC_C_CONNECTED);
393
394 /*
395 * The meaningful OPENREPLY connection state fields are:
396 * local_msgqueue_pa = physical address of remote
Dean Nelson35190502008-04-22 14:48:55 -0500397 * partition's local_msgqueue
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700398 * local_nentries = remote partition's local_nentries
399 * remote_nentries = remote partition's remote_nentries
400 */
401 DBUG_ON(args->local_msgqueue_pa == 0);
402 DBUG_ON(args->local_nentries == 0);
403 DBUG_ON(args->remote_nentries == 0);
404
405 ch->flags |= XPC_C_ROPENREPLY;
Dean Nelson5b8669d2008-07-29 22:34:18 -0700406 xpc_save_remote_msgqueue_pa(ch, args->local_msgqueue_pa);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700407
408 if (args->local_nentries < ch->remote_nentries) {
Dean Nelson7fb5e592008-07-29 22:34:10 -0700409 dev_dbg(xpc_chan, "XPC_CHCTL_OPENREPLY: new "
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700410 "remote_nentries=%d, old remote_nentries=%d, "
411 "partid=%d, channel=%d\n",
412 args->local_nentries, ch->remote_nentries,
413 ch->partid, ch->number);
414
415 ch->remote_nentries = args->local_nentries;
416 }
417 if (args->remote_nentries < ch->local_nentries) {
Dean Nelson7fb5e592008-07-29 22:34:10 -0700418 dev_dbg(xpc_chan, "XPC_CHCTL_OPENREPLY: new "
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700419 "local_nentries=%d, old local_nentries=%d, "
420 "partid=%d, channel=%d\n",
421 args->remote_nentries, ch->local_nentries,
422 ch->partid, ch->number);
423
424 ch->local_nentries = args->remote_nentries;
425 }
426
427 xpc_process_connect(ch, &irq_flags);
428 }
429
430 spin_unlock_irqrestore(&ch->lock, irq_flags);
431}
432
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700433/*
434 * Attempt to establish a channel connection to a remote partition.
435 */
Dean Nelson65c17b82008-05-12 14:02:02 -0700436static enum xp_retval
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700437xpc_connect_channel(struct xpc_channel *ch)
438{
439 unsigned long irq_flags;
440 struct xpc_registration *registration = &xpc_registrations[ch->number];
441
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500442 if (mutex_trylock(&registration->mutex) == 0)
Dean Nelson65c17b82008-05-12 14:02:02 -0700443 return xpRetry;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700444
445 if (!XPC_CHANNEL_REGISTERED(ch->number)) {
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500446 mutex_unlock(&registration->mutex);
Dean Nelson65c17b82008-05-12 14:02:02 -0700447 return xpUnregistered;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700448 }
449
450 spin_lock_irqsave(&ch->lock, irq_flags);
451
452 DBUG_ON(ch->flags & XPC_C_CONNECTED);
453 DBUG_ON(ch->flags & XPC_C_OPENREQUEST);
454
455 if (ch->flags & XPC_C_DISCONNECTING) {
456 spin_unlock_irqrestore(&ch->lock, irq_flags);
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500457 mutex_unlock(&registration->mutex);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700458 return ch->reason;
459 }
460
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700461 /* add info from the channel connect registration to the channel */
462
463 ch->kthreads_assigned_limit = registration->assigned_limit;
464 ch->kthreads_idle_limit = registration->idle_limit;
465 DBUG_ON(atomic_read(&ch->kthreads_assigned) != 0);
466 DBUG_ON(atomic_read(&ch->kthreads_idle) != 0);
467 DBUG_ON(atomic_read(&ch->kthreads_active) != 0);
468
469 ch->func = registration->func;
470 DBUG_ON(registration->func == NULL);
471 ch->key = registration->key;
472
473 ch->local_nentries = registration->nentries;
474
475 if (ch->flags & XPC_C_ROPENREQUEST) {
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700476 if (registration->entry_size != ch->entry_size) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700477 /* the local and remote sides aren't the same */
478
479 /*
480 * Because XPC_DISCONNECT_CHANNEL() can block we're
481 * forced to up the registration sema before we unlock
482 * the channel lock. But that's okay here because we're
483 * done with the part that required the registration
484 * sema. XPC_DISCONNECT_CHANNEL() requires that the
485 * channel lock be locked and will unlock and relock
486 * the channel lock as needed.
487 */
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500488 mutex_unlock(&registration->mutex);
Dean Nelson65c17b82008-05-12 14:02:02 -0700489 XPC_DISCONNECT_CHANNEL(ch, xpUnequalMsgSizes,
Dean Nelson35190502008-04-22 14:48:55 -0500490 &irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700491 spin_unlock_irqrestore(&ch->lock, irq_flags);
Dean Nelson65c17b82008-05-12 14:02:02 -0700492 return xpUnequalMsgSizes;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700493 }
494 } else {
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700495 ch->entry_size = registration->entry_size;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700496
497 XPC_SET_REASON(ch, 0, 0);
498 ch->flags &= ~XPC_C_DISCONNECTED;
499
500 atomic_inc(&xpc_partitions[ch->partid].nchannels_active);
501 }
502
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500503 mutex_unlock(&registration->mutex);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700504
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700505 /* initiate the connection */
506
507 ch->flags |= (XPC_C_OPENREQUEST | XPC_C_CONNECTING);
Dean Nelson7fb5e592008-07-29 22:34:10 -0700508 xpc_send_chctl_openrequest(ch, &irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700509
510 xpc_process_connect(ch, &irq_flags);
511
512 spin_unlock_irqrestore(&ch->lock, irq_flags);
513
Dean Nelson65c17b82008-05-12 14:02:02 -0700514 return xpSuccess;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700515}
516
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700517void
Dean Nelson7fb5e592008-07-29 22:34:10 -0700518xpc_process_sent_chctl_flags(struct xpc_partition *part)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700519{
520 unsigned long irq_flags;
Dean Nelson7fb5e592008-07-29 22:34:10 -0700521 union xpc_channel_ctl_flags chctl;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700522 struct xpc_channel *ch;
523 int ch_number;
Dean Nelsona607c3892005-09-01 14:01:37 -0500524 u32 ch_flags;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700525
Dean Nelson7fb5e592008-07-29 22:34:10 -0700526 chctl.all_flags = xpc_get_chctl_all_flags(part);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700527
528 /*
529 * Initiate channel connections for registered channels.
530 *
531 * For each connected channel that has pending messages activate idle
532 * kthreads and/or create new kthreads as needed.
533 */
534
535 for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
536 ch = &part->channels[ch_number];
537
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700538 /*
Dean Nelson7fb5e592008-07-29 22:34:10 -0700539 * Process any open or close related chctl flags, and then deal
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700540 * with connecting or disconnecting the channel as required.
541 */
542
Dean Nelson7fb5e592008-07-29 22:34:10 -0700543 if (chctl.flags[ch_number] & XPC_OPENCLOSE_CHCTL_FLAGS) {
544 xpc_process_openclose_chctl_flags(part, ch_number,
545 chctl.flags[ch_number]);
546 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700547
Dean Nelsona607c3892005-09-01 14:01:37 -0500548 ch_flags = ch->flags; /* need an atomic snapshot of flags */
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700549
Dean Nelsona607c3892005-09-01 14:01:37 -0500550 if (ch_flags & XPC_C_DISCONNECTING) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700551 spin_lock_irqsave(&ch->lock, irq_flags);
552 xpc_process_disconnect(ch, &irq_flags);
553 spin_unlock_irqrestore(&ch->lock, irq_flags);
554 continue;
555 }
556
Dean Nelson83469b52008-07-29 22:34:18 -0700557 if (part->act_state == XPC_P_AS_DEACTIVATING)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700558 continue;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700559
Dean Nelsona607c3892005-09-01 14:01:37 -0500560 if (!(ch_flags & XPC_C_CONNECTED)) {
561 if (!(ch_flags & XPC_C_OPENREQUEST)) {
562 DBUG_ON(ch_flags & XPC_C_SETUP);
Dean Nelson35190502008-04-22 14:48:55 -0500563 (void)xpc_connect_channel(ch);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700564 } else {
565 spin_lock_irqsave(&ch->lock, irq_flags);
566 xpc_process_connect(ch, &irq_flags);
567 spin_unlock_irqrestore(&ch->lock, irq_flags);
568 }
569 continue;
570 }
571
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700572 /*
Dean Nelson7fb5e592008-07-29 22:34:10 -0700573 * Process any message related chctl flags, this may involve
574 * the activation of kthreads to deliver any pending messages
575 * sent from the other partition.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700576 */
577
Dean Nelson7fb5e592008-07-29 22:34:10 -0700578 if (chctl.flags[ch_number] & XPC_MSG_CHCTL_FLAGS)
579 xpc_process_msg_chctl_flags(part, ch_number);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700580 }
581}
582
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700583/*
Dean Nelsona607c3892005-09-01 14:01:37 -0500584 * XPC's heartbeat code calls this function to inform XPC that a partition is
585 * going down. XPC responds by tearing down the XPartition Communication
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700586 * infrastructure used for the just downed partition.
587 *
588 * XPC's heartbeat code will never call this function and xpc_partition_up()
589 * at the same time. Nor will it ever make multiple calls to either function
590 * at the same time.
591 */
592void
Dean Nelson65c17b82008-05-12 14:02:02 -0700593xpc_partition_going_down(struct xpc_partition *part, enum xp_retval reason)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700594{
595 unsigned long irq_flags;
596 int ch_number;
597 struct xpc_channel *ch;
598
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700599 dev_dbg(xpc_chan, "deactivating partition %d, reason=%d\n",
600 XPC_PARTID(part), reason);
601
602 if (!xpc_part_ref(part)) {
603 /* infrastructure for this partition isn't currently set up */
604 return;
605 }
606
Dean Nelsona607c3892005-09-01 14:01:37 -0500607 /* disconnect channels associated with the partition going down */
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700608
609 for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
610 ch = &part->channels[ch_number];
611
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700612 xpc_msgqueue_ref(ch);
613 spin_lock_irqsave(&ch->lock, irq_flags);
614
615 XPC_DISCONNECT_CHANNEL(ch, reason, &irq_flags);
616
617 spin_unlock_irqrestore(&ch->lock, irq_flags);
618 xpc_msgqueue_deref(ch);
619 }
620
621 xpc_wakeup_channel_mgr(part);
622
623 xpc_part_deref(part);
624}
625
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700626/*
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700627 * Called by XP at the time of channel connection registration to cause
628 * XPC to establish connections to all currently active partitions.
629 */
630void
631xpc_initiate_connect(int ch_number)
632{
Dean Nelson64d032b2008-05-12 14:02:03 -0700633 short partid;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700634 struct xpc_partition *part;
635 struct xpc_channel *ch;
636
Dean Nelsonbc63d382008-07-29 22:34:04 -0700637 DBUG_ON(ch_number < 0 || ch_number >= XPC_MAX_NCHANNELS);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700638
Dean Nelsonbc63d382008-07-29 22:34:04 -0700639 for (partid = 0; partid < xp_max_npartitions; partid++) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700640 part = &xpc_partitions[partid];
641
642 if (xpc_part_ref(part)) {
643 ch = &part->channels[ch_number];
644
Dean Nelsone54af722005-10-25 14:07:43 -0500645 /*
646 * Initiate the establishment of a connection on the
647 * newly registered channel to the remote partition.
648 */
649 xpc_wakeup_channel_mgr(part);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700650 xpc_part_deref(part);
651 }
652 }
653}
654
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700655void
656xpc_connected_callout(struct xpc_channel *ch)
657{
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700658 /* let the registerer know that a connection has been established */
659
660 if (ch->func != NULL) {
Dean Nelson65c17b82008-05-12 14:02:02 -0700661 dev_dbg(xpc_chan, "ch->func() called, reason=xpConnected, "
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700662 "partid=%d, channel=%d\n", ch->partid, ch->number);
663
Dean Nelson65c17b82008-05-12 14:02:02 -0700664 ch->func(xpConnected, ch->partid, ch->number,
Dean Nelson35190502008-04-22 14:48:55 -0500665 (void *)(u64)ch->local_nentries, ch->key);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700666
Dean Nelson65c17b82008-05-12 14:02:02 -0700667 dev_dbg(xpc_chan, "ch->func() returned, reason=xpConnected, "
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700668 "partid=%d, channel=%d\n", ch->partid, ch->number);
669 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700670}
671
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700672/*
673 * Called by XP at the time of channel connection unregistration to cause
674 * XPC to teardown all current connections for the specified channel.
675 *
676 * Before returning xpc_initiate_disconnect() will wait until all connections
677 * on the specified channel have been closed/torndown. So the caller can be
678 * assured that they will not be receiving any more callouts from XPC to the
679 * function they registered via xpc_connect().
680 *
681 * Arguments:
682 *
683 * ch_number - channel # to unregister.
684 */
685void
686xpc_initiate_disconnect(int ch_number)
687{
688 unsigned long irq_flags;
Dean Nelson64d032b2008-05-12 14:02:03 -0700689 short partid;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700690 struct xpc_partition *part;
691 struct xpc_channel *ch;
692
Dean Nelsonbc63d382008-07-29 22:34:04 -0700693 DBUG_ON(ch_number < 0 || ch_number >= XPC_MAX_NCHANNELS);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700694
695 /* initiate the channel disconnect for every active partition */
Dean Nelsonbc63d382008-07-29 22:34:04 -0700696 for (partid = 0; partid < xp_max_npartitions; partid++) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700697 part = &xpc_partitions[partid];
698
699 if (xpc_part_ref(part)) {
700 ch = &part->channels[ch_number];
701 xpc_msgqueue_ref(ch);
702
703 spin_lock_irqsave(&ch->lock, irq_flags);
704
Dean Nelsona607c3892005-09-01 14:01:37 -0500705 if (!(ch->flags & XPC_C_DISCONNECTED)) {
706 ch->flags |= XPC_C_WDISCONNECT;
707
Dean Nelson65c17b82008-05-12 14:02:02 -0700708 XPC_DISCONNECT_CHANNEL(ch, xpUnregistering,
Dean Nelson35190502008-04-22 14:48:55 -0500709 &irq_flags);
Dean Nelsona607c3892005-09-01 14:01:37 -0500710 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700711
712 spin_unlock_irqrestore(&ch->lock, irq_flags);
713
714 xpc_msgqueue_deref(ch);
715 xpc_part_deref(part);
716 }
717 }
718
719 xpc_disconnect_wait(ch_number);
720}
721
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700722/*
723 * To disconnect a channel, and reflect it back to all who may be waiting.
724 *
Dean Nelsona607c3892005-09-01 14:01:37 -0500725 * An OPEN is not allowed until XPC_C_DISCONNECTING is cleared by
726 * xpc_process_disconnect(), and if set, XPC_C_WDISCONNECT is cleared by
727 * xpc_disconnect_wait().
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700728 *
729 * THE CHANNEL IS TO BE LOCKED BY THE CALLER AND WILL REMAIN LOCKED UPON RETURN.
730 */
731void
732xpc_disconnect_channel(const int line, struct xpc_channel *ch,
Dean Nelson65c17b82008-05-12 14:02:02 -0700733 enum xp_retval reason, unsigned long *irq_flags)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700734{
Dean Nelsona607c3892005-09-01 14:01:37 -0500735 u32 channel_was_connected = (ch->flags & XPC_C_CONNECTED);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700736
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700737 DBUG_ON(!spin_is_locked(&ch->lock));
738
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500739 if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_DISCONNECTED))
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700740 return;
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500741
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700742 DBUG_ON(!(ch->flags & (XPC_C_CONNECTING | XPC_C_CONNECTED)));
743
744 dev_dbg(xpc_chan, "reason=%d, line=%d, partid=%d, channel=%d\n",
745 reason, line, ch->partid, ch->number);
746
747 XPC_SET_REASON(ch, reason, line);
748
Dean Nelsona607c3892005-09-01 14:01:37 -0500749 ch->flags |= (XPC_C_CLOSEREQUEST | XPC_C_DISCONNECTING);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700750 /* some of these may not have been set */
751 ch->flags &= ~(XPC_C_OPENREQUEST | XPC_C_OPENREPLY |
Dean Nelson35190502008-04-22 14:48:55 -0500752 XPC_C_ROPENREQUEST | XPC_C_ROPENREPLY |
753 XPC_C_CONNECTING | XPC_C_CONNECTED);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700754
Dean Nelson7fb5e592008-07-29 22:34:10 -0700755 xpc_send_chctl_closerequest(ch, irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700756
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500757 if (channel_was_connected)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700758 ch->flags |= XPC_C_WASCONNECTED;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700759
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700760 spin_unlock_irqrestore(&ch->lock, *irq_flags);
761
Dean Nelsona607c3892005-09-01 14:01:37 -0500762 /* wake all idle kthreads so they can exit */
763 if (atomic_read(&ch->kthreads_idle) > 0) {
764 wake_up_all(&ch->idle_wq);
Dean Nelsona460ef82006-11-22 08:25:00 -0600765
766 } else if ((ch->flags & XPC_C_CONNECTEDCALLOUT_MADE) &&
Dean Nelson35190502008-04-22 14:48:55 -0500767 !(ch->flags & XPC_C_DISCONNECTINGCALLOUT)) {
Dean Nelson65c17b82008-05-12 14:02:02 -0700768 /* start a kthread that will do the xpDisconnecting callout */
Dean Nelsona460ef82006-11-22 08:25:00 -0600769 xpc_create_kthreads(ch, 1, 1);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700770 }
771
Dean Nelsona607c3892005-09-01 14:01:37 -0500772 /* wake those waiting to allocate an entry from the local msg queue */
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500773 if (atomic_read(&ch->n_on_msg_allocate_wq) > 0)
Dean Nelsona607c3892005-09-01 14:01:37 -0500774 wake_up(&ch->msg_allocate_wq);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700775
776 spin_lock_irqsave(&ch->lock, *irq_flags);
777}
778
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700779void
Dean Nelson65c17b82008-05-12 14:02:02 -0700780xpc_disconnect_callout(struct xpc_channel *ch, enum xp_retval reason)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700781{
782 /*
Dean Nelsona607c3892005-09-01 14:01:37 -0500783 * Let the channel's registerer know that the channel is being
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700784 * disconnected. We don't want to do this if the registerer was never
Dean Nelsona607c3892005-09-01 14:01:37 -0500785 * informed of a connection being made.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700786 */
787
788 if (ch->func != NULL) {
Dean Nelson246c7e32005-12-22 14:32:56 -0600789 dev_dbg(xpc_chan, "ch->func() called, reason=%d, partid=%d, "
790 "channel=%d\n", reason, ch->partid, ch->number);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700791
Dean Nelson246c7e32005-12-22 14:32:56 -0600792 ch->func(reason, ch->partid, ch->number, NULL, ch->key);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700793
Dean Nelson246c7e32005-12-22 14:32:56 -0600794 dev_dbg(xpc_chan, "ch->func() returned, reason=%d, partid=%d, "
795 "channel=%d\n", reason, ch->partid, ch->number);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700796 }
797}
798
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700799/*
800 * Wait for a message entry to become available for the specified channel,
801 * but don't wait any longer than 1 jiffy.
802 */
Dean Nelson33ba3c72008-07-29 22:34:07 -0700803enum xp_retval
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700804xpc_allocate_msg_wait(struct xpc_channel *ch)
805{
Dean Nelson65c17b82008-05-12 14:02:02 -0700806 enum xp_retval ret;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700807
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700808 if (ch->flags & XPC_C_DISCONNECTING) {
Dean Nelson65c17b82008-05-12 14:02:02 -0700809 DBUG_ON(ch->reason == xpInterrupted);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700810 return ch->reason;
811 }
812
813 atomic_inc(&ch->n_on_msg_allocate_wq);
814 ret = interruptible_sleep_on_timeout(&ch->msg_allocate_wq, 1);
815 atomic_dec(&ch->n_on_msg_allocate_wq);
816
817 if (ch->flags & XPC_C_DISCONNECTING) {
818 ret = ch->reason;
Dean Nelson65c17b82008-05-12 14:02:02 -0700819 DBUG_ON(ch->reason == xpInterrupted);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700820 } else if (ret == 0) {
Dean Nelson65c17b82008-05-12 14:02:02 -0700821 ret = xpTimeout;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700822 } else {
Dean Nelson65c17b82008-05-12 14:02:02 -0700823 ret = xpInterrupted;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700824 }
825
826 return ret;
827}
828
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700829/*
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700830 * Send a message that contains the user's payload on the specified channel
831 * connected to the specified partition.
832 *
833 * NOTE that this routine can sleep waiting for a message entry to become
834 * available. To not sleep, pass in the XPC_NOWAIT flag.
835 *
836 * Once sent, this routine will not wait for the message to be received, nor
837 * will notification be given when it does happen.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700838 *
839 * Arguments:
840 *
841 * partid - ID of partition to which the channel is connected.
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700842 * ch_number - channel # to send message on.
843 * flags - see xp.h for valid flags.
844 * payload - pointer to the payload which is to be sent.
845 * payload_size - size of the payload in bytes.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700846 */
Dean Nelson65c17b82008-05-12 14:02:02 -0700847enum xp_retval
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700848xpc_initiate_send(short partid, int ch_number, u32 flags, void *payload,
849 u16 payload_size)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700850{
851 struct xpc_partition *part = &xpc_partitions[partid];
Dean Nelson65c17b82008-05-12 14:02:02 -0700852 enum xp_retval ret = xpUnknownReason;
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700853
854 dev_dbg(xpc_chan, "payload=0x%p, partid=%d, channel=%d\n", payload,
855 partid, ch_number);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700856
Dean Nelsonbc63d382008-07-29 22:34:04 -0700857 DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700858 DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700859 DBUG_ON(payload == NULL);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700860
861 if (xpc_part_ref(part)) {
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700862 ret = xpc_send_payload(&part->channels[ch_number], flags,
863 payload, payload_size, 0, NULL, NULL);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700864 xpc_part_deref(part);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700865 }
866
867 return ret;
868}
869
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700870/*
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700871 * Send a message that contains the user's payload on the specified channel
872 * connected to the specified partition.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700873 *
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700874 * NOTE that this routine can sleep waiting for a message entry to become
875 * available. To not sleep, pass in the XPC_NOWAIT flag.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700876 *
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700877 * This routine will not wait for the message to be sent or received.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700878 *
879 * Once the remote end of the channel has received the message, the function
880 * passed as an argument to xpc_initiate_send_notify() will be called. This
881 * allows the sender to free up or re-use any buffers referenced by the
882 * message, but does NOT mean the message has been processed at the remote
883 * end by a receiver.
884 *
885 * If this routine returns an error, the caller's function will NOT be called.
886 *
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700887 * Arguments:
888 *
889 * partid - ID of partition to which the channel is connected.
890 * ch_number - channel # to send message on.
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700891 * flags - see xp.h for valid flags.
892 * payload - pointer to the payload which is to be sent.
893 * payload_size - size of the payload in bytes.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700894 * func - function to call with asynchronous notification of message
895 * receipt. THIS FUNCTION MUST BE NON-BLOCKING.
896 * key - user-defined key to be passed to the function when it's called.
897 */
Dean Nelson65c17b82008-05-12 14:02:02 -0700898enum xp_retval
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700899xpc_initiate_send_notify(short partid, int ch_number, u32 flags, void *payload,
900 u16 payload_size, xpc_notify_func func, void *key)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700901{
902 struct xpc_partition *part = &xpc_partitions[partid];
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700903 enum xp_retval ret = xpUnknownReason;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700904
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700905 dev_dbg(xpc_chan, "payload=0x%p, partid=%d, channel=%d\n", payload,
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700906 partid, ch_number);
907
Dean Nelsonbc63d382008-07-29 22:34:04 -0700908 DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700909 DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700910 DBUG_ON(payload == NULL);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700911 DBUG_ON(func == NULL);
912
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700913 if (xpc_part_ref(part)) {
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700914 ret = xpc_send_payload(&part->channels[ch_number], flags,
915 payload, payload_size, XPC_N_CALL, func,
916 key);
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700917 xpc_part_deref(part);
918 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700919 return ret;
920}
921
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700922/*
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700923 * Deliver a message's payload to its intended recipient.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700924 */
925void
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700926xpc_deliver_payload(struct xpc_channel *ch)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700927{
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700928 void *payload;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700929
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700930 payload = xpc_get_deliverable_payload(ch);
931 if (payload != NULL) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700932
933 /*
934 * This ref is taken to protect the payload itself from being
935 * freed before the user is finished with it, which the user
936 * indicates by calling xpc_initiate_received().
937 */
938 xpc_msgqueue_ref(ch);
939
940 atomic_inc(&ch->kthreads_active);
941
942 if (ch->func != NULL) {
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700943 dev_dbg(xpc_chan, "ch->func() called, payload=0x%p "
944 "partid=%d channel=%d\n", payload, ch->partid,
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700945 ch->number);
946
947 /* deliver the message to its intended recipient */
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700948 ch->func(xpMsgReceived, ch->partid, ch->number, payload,
949 ch->key);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700950
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700951 dev_dbg(xpc_chan, "ch->func() returned, payload=0x%p "
952 "partid=%d channel=%d\n", payload, ch->partid,
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700953 ch->number);
954 }
955
956 atomic_dec(&ch->kthreads_active);
957 }
958}
959
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700960/*
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700961 * Acknowledge receipt of a delivered message's payload.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700962 *
963 * This function, although called by users, does not call xpc_part_ref() to
964 * ensure that the partition infrastructure is in place. It relies on the
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700965 * fact that we called xpc_msgqueue_ref() in xpc_deliver_payload().
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700966 *
967 * Arguments:
968 *
969 * partid - ID of partition to which the channel is connected.
970 * ch_number - channel # message received on.
971 * payload - pointer to the payload area allocated via
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700972 * xpc_initiate_send() or xpc_initiate_send_notify().
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700973 */
974void
Dean Nelson64d032b2008-05-12 14:02:03 -0700975xpc_initiate_received(short partid, int ch_number, void *payload)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700976{
977 struct xpc_partition *part = &xpc_partitions[partid];
978 struct xpc_channel *ch;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700979
Dean Nelsonbc63d382008-07-29 22:34:04 -0700980 DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700981 DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
982
983 ch = &part->channels[ch_number];
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700984 xpc_received_payload(ch, payload);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700985
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700986 /* the call to xpc_msgqueue_ref() was done by xpc_deliver_payload() */
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700987 xpc_msgqueue_deref(ch);
988}