blob: 45fd653dbe311b5d5c775e8ae8ee753a04b769c7 [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 }
53
54 if (!(ch->flags & XPC_C_OPENREPLY)) {
55 ch->flags |= XPC_C_OPENREPLY;
Dean Nelson7fb5e592008-07-29 22:34:10 -070056 xpc_send_chctl_openreply(ch, irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -070057 }
58
Dean Nelson2c2b94f2008-04-22 14:50:17 -050059 if (!(ch->flags & XPC_C_ROPENREPLY))
Dean Nelson89eb8eb2005-03-23 19:50:00 -070060 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -070061
Dean Nelson89eb8eb2005-03-23 19:50:00 -070062 ch->flags = (XPC_C_CONNECTED | XPC_C_SETUP); /* clear all else */
63
64 dev_info(xpc_chan, "channel %d to partition %d connected\n",
Dean Nelson35190502008-04-22 14:48:55 -050065 ch->number, ch->partid);
Dean Nelson89eb8eb2005-03-23 19:50:00 -070066
67 spin_unlock_irqrestore(&ch->lock, *irq_flags);
Dean Nelsona460ef82006-11-22 08:25:00 -060068 xpc_create_kthreads(ch, 1, 0);
Dean Nelson89eb8eb2005-03-23 19:50:00 -070069 spin_lock_irqsave(&ch->lock, *irq_flags);
70}
71
Dean Nelson89eb8eb2005-03-23 19:50:00 -070072/*
Dean Nelson89eb8eb2005-03-23 19:50:00 -070073 * spin_lock_irqsave() is expected to be held on entry.
74 */
75static void
76xpc_process_disconnect(struct xpc_channel *ch, unsigned long *irq_flags)
77{
78 struct xpc_partition *part = &xpc_partitions[ch->partid];
Dean Nelsona607c382005-09-01 14:01:37 -050079 u32 channel_was_connected = (ch->flags & XPC_C_WASCONNECTED);
Dean Nelson89eb8eb2005-03-23 19:50:00 -070080
Dean Nelson89eb8eb2005-03-23 19:50:00 -070081 DBUG_ON(!spin_is_locked(&ch->lock));
82
Dean Nelson2c2b94f2008-04-22 14:50:17 -050083 if (!(ch->flags & XPC_C_DISCONNECTING))
Dean Nelson89eb8eb2005-03-23 19:50:00 -070084 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -070085
86 DBUG_ON(!(ch->flags & XPC_C_CLOSEREQUEST));
87
88 /* make sure all activity has settled down first */
89
Dean Nelsona460ef82006-11-22 08:25:00 -060090 if (atomic_read(&ch->kthreads_assigned) > 0 ||
Dean Nelson35190502008-04-22 14:48:55 -050091 atomic_read(&ch->references) > 0) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -070092 return;
93 }
Dean Nelsona460ef82006-11-22 08:25:00 -060094 DBUG_ON((ch->flags & XPC_C_CONNECTEDCALLOUT_MADE) &&
Dean Nelson35190502008-04-22 14:48:55 -050095 !(ch->flags & XPC_C_DISCONNECTINGCALLOUT_MADE));
Dean Nelson89eb8eb2005-03-23 19:50:00 -070096
Dean Nelson83469b52008-07-29 22:34:18 -070097 if (part->act_state == XPC_P_AS_DEACTIVATING) {
Dean Nelsona607c382005-09-01 14:01:37 -050098 /* can't proceed until the other side disengages from us */
Dean Nelsona47d5da2008-07-29 22:34:09 -070099 if (xpc_partition_engaged(ch->partid))
Dean Nelsona607c382005-09-01 14:01:37 -0500100 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700101
Dean Nelsona607c382005-09-01 14:01:37 -0500102 } else {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700103
104 /* as long as the other side is up do the full protocol */
105
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500106 if (!(ch->flags & XPC_C_RCLOSEREQUEST))
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700107 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700108
109 if (!(ch->flags & XPC_C_CLOSEREPLY)) {
110 ch->flags |= XPC_C_CLOSEREPLY;
Dean Nelson7fb5e592008-07-29 22:34:10 -0700111 xpc_send_chctl_closereply(ch, irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700112 }
113
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500114 if (!(ch->flags & XPC_C_RCLOSEREPLY))
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700115 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700116 }
117
Dean Nelsona607c382005-09-01 14:01:37 -0500118 /* wake those waiting for notify completion */
119 if (atomic_read(&ch->n_to_notify) > 0) {
Dean Nelsonea57f802008-07-29 22:34:14 -0700120 /* we do callout while holding ch->lock, callout can't block */
Dean Nelsona47d5da2008-07-29 22:34:09 -0700121 xpc_notify_senders_of_disconnect(ch);
Dean Nelsona607c382005-09-01 14:01:37 -0500122 }
123
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700124 /* both sides are disconnected now */
125
Dean Nelson4c2cd962006-02-15 08:02:21 -0600126 if (ch->flags & XPC_C_DISCONNECTINGCALLOUT_MADE) {
Dean Nelson246c7e32005-12-22 14:32:56 -0600127 spin_unlock_irqrestore(&ch->lock, *irq_flags);
Dean Nelson65c17b82008-05-12 14:02:02 -0700128 xpc_disconnect_callout(ch, xpDisconnected);
Dean Nelson246c7e32005-12-22 14:32:56 -0600129 spin_lock_irqsave(&ch->lock, *irq_flags);
130 }
131
Dean Nelson5b8669d2008-07-29 22:34:18 -0700132 DBUG_ON(atomic_read(&ch->n_to_notify) != 0);
133
Dean Nelsona607c382005-09-01 14:01:37 -0500134 /* it's now safe to free the channel's message queues */
Dean Nelson5b8669d2008-07-29 22:34:18 -0700135 xpc_teardown_msg_structures(ch);
136
137 ch->func = NULL;
138 ch->key = NULL;
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700139 ch->entry_size = 0;
Dean Nelson5b8669d2008-07-29 22:34:18 -0700140 ch->local_nentries = 0;
141 ch->remote_nentries = 0;
142 ch->kthreads_assigned_limit = 0;
143 ch->kthreads_idle_limit = 0;
Dean Nelsona607c382005-09-01 14:01:37 -0500144
Dean Nelson185c3a12008-07-29 22:34:11 -0700145 /*
146 * Mark the channel disconnected and clear all other flags, including
Dean Nelson5b8669d2008-07-29 22:34:18 -0700147 * XPC_C_SETUP (because of call to xpc_teardown_msg_structures()) but
148 * not including XPC_C_WDISCONNECT (if it was set).
Dean Nelson185c3a12008-07-29 22:34:11 -0700149 */
Dean Nelsona607c382005-09-01 14:01:37 -0500150 ch->flags = (XPC_C_DISCONNECTED | (ch->flags & XPC_C_WDISCONNECT));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700151
152 atomic_dec(&part->nchannels_active);
153
Dean Nelsona607c382005-09-01 14:01:37 -0500154 if (channel_was_connected) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700155 dev_info(xpc_chan, "channel %d to partition %d disconnected, "
Dean Nelson35190502008-04-22 14:48:55 -0500156 "reason=%d\n", ch->number, ch->partid, ch->reason);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700157 }
Dean Nelsona607c382005-09-01 14:01:37 -0500158
Dean Nelsona607c382005-09-01 14:01:37 -0500159 if (ch->flags & XPC_C_WDISCONNECT) {
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500160 /* we won't lose the CPU since we're holding ch->lock */
161 complete(&ch->wdisconnect_wait);
Dean Nelson7fb5e592008-07-29 22:34:10 -0700162 } else if (ch->delayed_chctl_flags) {
Dean Nelson83469b52008-07-29 22:34:18 -0700163 if (part->act_state != XPC_P_AS_DEACTIVATING) {
Dean Nelson7fb5e592008-07-29 22:34:10 -0700164 /* time to take action on any delayed chctl flags */
165 spin_lock(&part->chctl_lock);
166 part->chctl.flags[ch->number] |=
167 ch->delayed_chctl_flags;
168 spin_unlock(&part->chctl_lock);
Dean Nelsone54af722005-10-25 14:07:43 -0500169 }
Dean Nelson7fb5e592008-07-29 22:34:10 -0700170 ch->delayed_chctl_flags = 0;
Dean Nelsona607c382005-09-01 14:01:37 -0500171 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700172}
173
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700174/*
175 * Process a change in the channel's remote connection state.
176 */
177static void
Dean Nelson7fb5e592008-07-29 22:34:10 -0700178xpc_process_openclose_chctl_flags(struct xpc_partition *part, int ch_number,
179 u8 chctl_flags)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700180{
181 unsigned long irq_flags;
182 struct xpc_openclose_args *args =
Dean Nelson35190502008-04-22 14:48:55 -0500183 &part->remote_openclose_args[ch_number];
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700184 struct xpc_channel *ch = &part->channels[ch_number];
Dean Nelson65c17b82008-05-12 14:02:02 -0700185 enum xp_retval reason;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700186
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700187 spin_lock_irqsave(&ch->lock, irq_flags);
188
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500189again:
Dean Nelsone54af722005-10-25 14:07:43 -0500190
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500191 if ((ch->flags & XPC_C_DISCONNECTED) &&
192 (ch->flags & XPC_C_WDISCONNECT)) {
Dean Nelsone54af722005-10-25 14:07:43 -0500193 /*
Dean Nelson7fb5e592008-07-29 22:34:10 -0700194 * Delay processing chctl flags until thread waiting disconnect
Dean Nelsone54af722005-10-25 14:07:43 -0500195 * has had a chance to see that the channel is disconnected.
196 */
Dean Nelson7fb5e592008-07-29 22:34:10 -0700197 ch->delayed_chctl_flags |= chctl_flags;
Dean Nelsone54af722005-10-25 14:07:43 -0500198 spin_unlock_irqrestore(&ch->lock, irq_flags);
199 return;
200 }
201
Dean Nelson7fb5e592008-07-29 22:34:10 -0700202 if (chctl_flags & XPC_CHCTL_CLOSEREQUEST) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700203
Dean Nelson7fb5e592008-07-29 22:34:10 -0700204 dev_dbg(xpc_chan, "XPC_CHCTL_CLOSEREQUEST (reason=%d) received "
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700205 "from partid=%d, channel=%d\n", args->reason,
206 ch->partid, ch->number);
207
208 /*
209 * If RCLOSEREQUEST is set, we're probably waiting for
210 * RCLOSEREPLY. We should find it and a ROPENREQUEST packed
Dean Nelson7fb5e592008-07-29 22:34:10 -0700211 * with this RCLOSEREQUEST in the chctl_flags.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700212 */
213
214 if (ch->flags & XPC_C_RCLOSEREQUEST) {
215 DBUG_ON(!(ch->flags & XPC_C_DISCONNECTING));
216 DBUG_ON(!(ch->flags & XPC_C_CLOSEREQUEST));
217 DBUG_ON(!(ch->flags & XPC_C_CLOSEREPLY));
218 DBUG_ON(ch->flags & XPC_C_RCLOSEREPLY);
219
Dean Nelson7fb5e592008-07-29 22:34:10 -0700220 DBUG_ON(!(chctl_flags & XPC_CHCTL_CLOSEREPLY));
221 chctl_flags &= ~XPC_CHCTL_CLOSEREPLY;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700222 ch->flags |= XPC_C_RCLOSEREPLY;
223
224 /* both sides have finished disconnecting */
225 xpc_process_disconnect(ch, &irq_flags);
Dean Nelsone54af722005-10-25 14:07:43 -0500226 DBUG_ON(!(ch->flags & XPC_C_DISCONNECTED));
227 goto again;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700228 }
229
230 if (ch->flags & XPC_C_DISCONNECTED) {
Dean Nelson7fb5e592008-07-29 22:34:10 -0700231 if (!(chctl_flags & XPC_CHCTL_OPENREQUEST)) {
232 if (part->chctl.flags[ch_number] &
233 XPC_CHCTL_OPENREQUEST) {
Dean Nelsone54af722005-10-25 14:07:43 -0500234
Dean Nelson7fb5e592008-07-29 22:34:10 -0700235 DBUG_ON(ch->delayed_chctl_flags != 0);
236 spin_lock(&part->chctl_lock);
237 part->chctl.flags[ch_number] |=
238 XPC_CHCTL_CLOSEREQUEST;
239 spin_unlock(&part->chctl_lock);
Dean Nelsone54af722005-10-25 14:07:43 -0500240 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700241 spin_unlock_irqrestore(&ch->lock, irq_flags);
242 return;
243 }
244
245 XPC_SET_REASON(ch, 0, 0);
246 ch->flags &= ~XPC_C_DISCONNECTED;
247
248 atomic_inc(&part->nchannels_active);
249 ch->flags |= (XPC_C_CONNECTING | XPC_C_ROPENREQUEST);
250 }
251
Dean Nelson7fb5e592008-07-29 22:34:10 -0700252 chctl_flags &= ~(XPC_CHCTL_OPENREQUEST | XPC_CHCTL_OPENREPLY);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700253
254 /*
255 * The meaningful CLOSEREQUEST connection state fields are:
256 * reason = reason connection is to be closed
257 */
258
259 ch->flags |= XPC_C_RCLOSEREQUEST;
260
261 if (!(ch->flags & XPC_C_DISCONNECTING)) {
262 reason = args->reason;
Dean Nelson65c17b82008-05-12 14:02:02 -0700263 if (reason <= xpSuccess || reason > xpUnknownReason)
264 reason = xpUnknownReason;
265 else if (reason == xpUnregistering)
266 reason = xpOtherUnregistering;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700267
268 XPC_DISCONNECT_CHANNEL(ch, reason, &irq_flags);
Dean Nelsone54af722005-10-25 14:07:43 -0500269
Dean Nelson7fb5e592008-07-29 22:34:10 -0700270 DBUG_ON(chctl_flags & XPC_CHCTL_CLOSEREPLY);
Dean Nelsone54af722005-10-25 14:07:43 -0500271 spin_unlock_irqrestore(&ch->lock, irq_flags);
272 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700273 }
Dean Nelsone54af722005-10-25 14:07:43 -0500274
275 xpc_process_disconnect(ch, &irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700276 }
277
Dean Nelson7fb5e592008-07-29 22:34:10 -0700278 if (chctl_flags & XPC_CHCTL_CLOSEREPLY) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700279
Dean Nelson7fb5e592008-07-29 22:34:10 -0700280 dev_dbg(xpc_chan, "XPC_CHCTL_CLOSEREPLY received from partid="
281 "%d, channel=%d\n", ch->partid, ch->number);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700282
283 if (ch->flags & XPC_C_DISCONNECTED) {
Dean Nelson83469b52008-07-29 22:34:18 -0700284 DBUG_ON(part->act_state != XPC_P_AS_DEACTIVATING);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700285 spin_unlock_irqrestore(&ch->lock, irq_flags);
286 return;
287 }
288
289 DBUG_ON(!(ch->flags & XPC_C_CLOSEREQUEST));
Dean Nelsone54af722005-10-25 14:07:43 -0500290
291 if (!(ch->flags & XPC_C_RCLOSEREQUEST)) {
Dean Nelson7fb5e592008-07-29 22:34:10 -0700292 if (part->chctl.flags[ch_number] &
293 XPC_CHCTL_CLOSEREQUEST) {
Dean Nelsone54af722005-10-25 14:07:43 -0500294
Dean Nelson7fb5e592008-07-29 22:34:10 -0700295 DBUG_ON(ch->delayed_chctl_flags != 0);
296 spin_lock(&part->chctl_lock);
297 part->chctl.flags[ch_number] |=
298 XPC_CHCTL_CLOSEREPLY;
299 spin_unlock(&part->chctl_lock);
Dean Nelsone54af722005-10-25 14:07:43 -0500300 }
301 spin_unlock_irqrestore(&ch->lock, irq_flags);
302 return;
303 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700304
305 ch->flags |= XPC_C_RCLOSEREPLY;
306
307 if (ch->flags & XPC_C_CLOSEREPLY) {
308 /* both sides have finished disconnecting */
309 xpc_process_disconnect(ch, &irq_flags);
310 }
311 }
312
Dean Nelson7fb5e592008-07-29 22:34:10 -0700313 if (chctl_flags & XPC_CHCTL_OPENREQUEST) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700314
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700315 dev_dbg(xpc_chan, "XPC_CHCTL_OPENREQUEST (entry_size=%d, "
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700316 "local_nentries=%d) received from partid=%d, "
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700317 "channel=%d\n", args->entry_size, args->local_nentries,
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700318 ch->partid, ch->number);
319
Dean Nelson83469b52008-07-29 22:34:18 -0700320 if (part->act_state == XPC_P_AS_DEACTIVATING ||
Dean Nelson35190502008-04-22 14:48:55 -0500321 (ch->flags & XPC_C_ROPENREQUEST)) {
Dean Nelsone54af722005-10-25 14:07:43 -0500322 spin_unlock_irqrestore(&ch->lock, irq_flags);
323 return;
324 }
325
326 if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_WDISCONNECT)) {
Dean Nelson7fb5e592008-07-29 22:34:10 -0700327 ch->delayed_chctl_flags |= XPC_CHCTL_OPENREQUEST;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700328 spin_unlock_irqrestore(&ch->lock, irq_flags);
329 return;
330 }
331 DBUG_ON(!(ch->flags & (XPC_C_DISCONNECTED |
Dean Nelson35190502008-04-22 14:48:55 -0500332 XPC_C_OPENREQUEST)));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700333 DBUG_ON(ch->flags & (XPC_C_ROPENREQUEST | XPC_C_ROPENREPLY |
Dean Nelson35190502008-04-22 14:48:55 -0500334 XPC_C_OPENREPLY | XPC_C_CONNECTED));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700335
336 /*
337 * The meaningful OPENREQUEST connection state fields are:
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700338 * entry_size = size of channel's messages in bytes
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700339 * local_nentries = remote partition's local_nentries
340 */
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700341 if (args->entry_size == 0 || args->local_nentries == 0) {
Dean Nelsone54af722005-10-25 14:07:43 -0500342 /* assume OPENREQUEST was delayed by mistake */
343 spin_unlock_irqrestore(&ch->lock, irq_flags);
344 return;
345 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700346
347 ch->flags |= (XPC_C_ROPENREQUEST | XPC_C_CONNECTING);
348 ch->remote_nentries = args->local_nentries;
349
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700350 if (ch->flags & XPC_C_OPENREQUEST) {
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700351 if (args->entry_size != ch->entry_size) {
Dean Nelson65c17b82008-05-12 14:02:02 -0700352 XPC_DISCONNECT_CHANNEL(ch, xpUnequalMsgSizes,
Dean Nelson35190502008-04-22 14:48:55 -0500353 &irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700354 spin_unlock_irqrestore(&ch->lock, irq_flags);
355 return;
356 }
357 } else {
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700358 ch->entry_size = args->entry_size;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700359
360 XPC_SET_REASON(ch, 0, 0);
361 ch->flags &= ~XPC_C_DISCONNECTED;
362
363 atomic_inc(&part->nchannels_active);
364 }
365
366 xpc_process_connect(ch, &irq_flags);
367 }
368
Dean Nelson7fb5e592008-07-29 22:34:10 -0700369 if (chctl_flags & XPC_CHCTL_OPENREPLY) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700370
Dean Nelson7fb5e592008-07-29 22:34:10 -0700371 dev_dbg(xpc_chan, "XPC_CHCTL_OPENREPLY (local_msgqueue_pa="
372 "0x%lx, local_nentries=%d, remote_nentries=%d) "
373 "received from partid=%d, channel=%d\n",
Dean Nelsona812dcc2008-07-29 22:34:16 -0700374 args->local_msgqueue_pa, args->local_nentries,
375 args->remote_nentries, ch->partid, ch->number);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700376
377 if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_DISCONNECTED)) {
378 spin_unlock_irqrestore(&ch->lock, irq_flags);
379 return;
380 }
Dean Nelsone54af722005-10-25 14:07:43 -0500381 if (!(ch->flags & XPC_C_OPENREQUEST)) {
Dean Nelson65c17b82008-05-12 14:02:02 -0700382 XPC_DISCONNECT_CHANNEL(ch, xpOpenCloseError,
Dean Nelson35190502008-04-22 14:48:55 -0500383 &irq_flags);
Dean Nelsone54af722005-10-25 14:07:43 -0500384 spin_unlock_irqrestore(&ch->lock, irq_flags);
385 return;
386 }
387
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700388 DBUG_ON(!(ch->flags & XPC_C_ROPENREQUEST));
389 DBUG_ON(ch->flags & XPC_C_CONNECTED);
390
391 /*
392 * The meaningful OPENREPLY connection state fields are:
393 * local_msgqueue_pa = physical address of remote
Dean Nelson35190502008-04-22 14:48:55 -0500394 * partition's local_msgqueue
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700395 * local_nentries = remote partition's local_nentries
396 * remote_nentries = remote partition's remote_nentries
397 */
398 DBUG_ON(args->local_msgqueue_pa == 0);
399 DBUG_ON(args->local_nentries == 0);
400 DBUG_ON(args->remote_nentries == 0);
401
402 ch->flags |= XPC_C_ROPENREPLY;
Dean Nelson5b8669d2008-07-29 22:34:18 -0700403 xpc_save_remote_msgqueue_pa(ch, args->local_msgqueue_pa);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700404
405 if (args->local_nentries < ch->remote_nentries) {
Dean Nelson7fb5e592008-07-29 22:34:10 -0700406 dev_dbg(xpc_chan, "XPC_CHCTL_OPENREPLY: new "
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700407 "remote_nentries=%d, old remote_nentries=%d, "
408 "partid=%d, channel=%d\n",
409 args->local_nentries, ch->remote_nentries,
410 ch->partid, ch->number);
411
412 ch->remote_nentries = args->local_nentries;
413 }
414 if (args->remote_nentries < ch->local_nentries) {
Dean Nelson7fb5e592008-07-29 22:34:10 -0700415 dev_dbg(xpc_chan, "XPC_CHCTL_OPENREPLY: new "
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700416 "local_nentries=%d, old local_nentries=%d, "
417 "partid=%d, channel=%d\n",
418 args->remote_nentries, ch->local_nentries,
419 ch->partid, ch->number);
420
421 ch->local_nentries = args->remote_nentries;
422 }
423
424 xpc_process_connect(ch, &irq_flags);
425 }
426
427 spin_unlock_irqrestore(&ch->lock, irq_flags);
428}
429
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700430/*
431 * Attempt to establish a channel connection to a remote partition.
432 */
Dean Nelson65c17b82008-05-12 14:02:02 -0700433static enum xp_retval
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700434xpc_connect_channel(struct xpc_channel *ch)
435{
436 unsigned long irq_flags;
437 struct xpc_registration *registration = &xpc_registrations[ch->number];
438
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500439 if (mutex_trylock(&registration->mutex) == 0)
Dean Nelson65c17b82008-05-12 14:02:02 -0700440 return xpRetry;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700441
442 if (!XPC_CHANNEL_REGISTERED(ch->number)) {
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500443 mutex_unlock(&registration->mutex);
Dean Nelson65c17b82008-05-12 14:02:02 -0700444 return xpUnregistered;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700445 }
446
447 spin_lock_irqsave(&ch->lock, irq_flags);
448
449 DBUG_ON(ch->flags & XPC_C_CONNECTED);
450 DBUG_ON(ch->flags & XPC_C_OPENREQUEST);
451
452 if (ch->flags & XPC_C_DISCONNECTING) {
453 spin_unlock_irqrestore(&ch->lock, irq_flags);
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500454 mutex_unlock(&registration->mutex);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700455 return ch->reason;
456 }
457
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700458 /* add info from the channel connect registration to the channel */
459
460 ch->kthreads_assigned_limit = registration->assigned_limit;
461 ch->kthreads_idle_limit = registration->idle_limit;
462 DBUG_ON(atomic_read(&ch->kthreads_assigned) != 0);
463 DBUG_ON(atomic_read(&ch->kthreads_idle) != 0);
464 DBUG_ON(atomic_read(&ch->kthreads_active) != 0);
465
466 ch->func = registration->func;
467 DBUG_ON(registration->func == NULL);
468 ch->key = registration->key;
469
470 ch->local_nentries = registration->nentries;
471
472 if (ch->flags & XPC_C_ROPENREQUEST) {
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700473 if (registration->entry_size != ch->entry_size) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700474 /* the local and remote sides aren't the same */
475
476 /*
477 * Because XPC_DISCONNECT_CHANNEL() can block we're
478 * forced to up the registration sema before we unlock
479 * the channel lock. But that's okay here because we're
480 * done with the part that required the registration
481 * sema. XPC_DISCONNECT_CHANNEL() requires that the
482 * channel lock be locked and will unlock and relock
483 * the channel lock as needed.
484 */
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500485 mutex_unlock(&registration->mutex);
Dean Nelson65c17b82008-05-12 14:02:02 -0700486 XPC_DISCONNECT_CHANNEL(ch, xpUnequalMsgSizes,
Dean Nelson35190502008-04-22 14:48:55 -0500487 &irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700488 spin_unlock_irqrestore(&ch->lock, irq_flags);
Dean Nelson65c17b82008-05-12 14:02:02 -0700489 return xpUnequalMsgSizes;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700490 }
491 } else {
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700492 ch->entry_size = registration->entry_size;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700493
494 XPC_SET_REASON(ch, 0, 0);
495 ch->flags &= ~XPC_C_DISCONNECTED;
496
497 atomic_inc(&xpc_partitions[ch->partid].nchannels_active);
498 }
499
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500500 mutex_unlock(&registration->mutex);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700501
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700502 /* initiate the connection */
503
504 ch->flags |= (XPC_C_OPENREQUEST | XPC_C_CONNECTING);
Dean Nelson7fb5e592008-07-29 22:34:10 -0700505 xpc_send_chctl_openrequest(ch, &irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700506
507 xpc_process_connect(ch, &irq_flags);
508
509 spin_unlock_irqrestore(&ch->lock, irq_flags);
510
Dean Nelson65c17b82008-05-12 14:02:02 -0700511 return xpSuccess;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700512}
513
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700514void
Dean Nelson7fb5e592008-07-29 22:34:10 -0700515xpc_process_sent_chctl_flags(struct xpc_partition *part)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700516{
517 unsigned long irq_flags;
Dean Nelson7fb5e592008-07-29 22:34:10 -0700518 union xpc_channel_ctl_flags chctl;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700519 struct xpc_channel *ch;
520 int ch_number;
Dean Nelsona607c382005-09-01 14:01:37 -0500521 u32 ch_flags;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700522
Dean Nelson7fb5e592008-07-29 22:34:10 -0700523 chctl.all_flags = xpc_get_chctl_all_flags(part);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700524
525 /*
526 * Initiate channel connections for registered channels.
527 *
528 * For each connected channel that has pending messages activate idle
529 * kthreads and/or create new kthreads as needed.
530 */
531
532 for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
533 ch = &part->channels[ch_number];
534
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700535 /*
Dean Nelson7fb5e592008-07-29 22:34:10 -0700536 * Process any open or close related chctl flags, and then deal
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700537 * with connecting or disconnecting the channel as required.
538 */
539
Dean Nelson7fb5e592008-07-29 22:34:10 -0700540 if (chctl.flags[ch_number] & XPC_OPENCLOSE_CHCTL_FLAGS) {
541 xpc_process_openclose_chctl_flags(part, ch_number,
542 chctl.flags[ch_number]);
543 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700544
Dean Nelsona607c382005-09-01 14:01:37 -0500545 ch_flags = ch->flags; /* need an atomic snapshot of flags */
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700546
Dean Nelsona607c382005-09-01 14:01:37 -0500547 if (ch_flags & XPC_C_DISCONNECTING) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700548 spin_lock_irqsave(&ch->lock, irq_flags);
549 xpc_process_disconnect(ch, &irq_flags);
550 spin_unlock_irqrestore(&ch->lock, irq_flags);
551 continue;
552 }
553
Dean Nelson83469b52008-07-29 22:34:18 -0700554 if (part->act_state == XPC_P_AS_DEACTIVATING)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700555 continue;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700556
Dean Nelsona607c382005-09-01 14:01:37 -0500557 if (!(ch_flags & XPC_C_CONNECTED)) {
558 if (!(ch_flags & XPC_C_OPENREQUEST)) {
559 DBUG_ON(ch_flags & XPC_C_SETUP);
Dean Nelson35190502008-04-22 14:48:55 -0500560 (void)xpc_connect_channel(ch);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700561 } else {
562 spin_lock_irqsave(&ch->lock, irq_flags);
563 xpc_process_connect(ch, &irq_flags);
564 spin_unlock_irqrestore(&ch->lock, irq_flags);
565 }
566 continue;
567 }
568
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700569 /*
Dean Nelson7fb5e592008-07-29 22:34:10 -0700570 * Process any message related chctl flags, this may involve
571 * the activation of kthreads to deliver any pending messages
572 * sent from the other partition.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700573 */
574
Dean Nelson7fb5e592008-07-29 22:34:10 -0700575 if (chctl.flags[ch_number] & XPC_MSG_CHCTL_FLAGS)
576 xpc_process_msg_chctl_flags(part, ch_number);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700577 }
578}
579
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700580/*
Dean Nelsona607c382005-09-01 14:01:37 -0500581 * XPC's heartbeat code calls this function to inform XPC that a partition is
582 * going down. XPC responds by tearing down the XPartition Communication
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700583 * infrastructure used for the just downed partition.
584 *
585 * XPC's heartbeat code will never call this function and xpc_partition_up()
586 * at the same time. Nor will it ever make multiple calls to either function
587 * at the same time.
588 */
589void
Dean Nelson65c17b82008-05-12 14:02:02 -0700590xpc_partition_going_down(struct xpc_partition *part, enum xp_retval reason)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700591{
592 unsigned long irq_flags;
593 int ch_number;
594 struct xpc_channel *ch;
595
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700596 dev_dbg(xpc_chan, "deactivating partition %d, reason=%d\n",
597 XPC_PARTID(part), reason);
598
599 if (!xpc_part_ref(part)) {
600 /* infrastructure for this partition isn't currently set up */
601 return;
602 }
603
Dean Nelsona607c382005-09-01 14:01:37 -0500604 /* disconnect channels associated with the partition going down */
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700605
606 for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
607 ch = &part->channels[ch_number];
608
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700609 xpc_msgqueue_ref(ch);
610 spin_lock_irqsave(&ch->lock, irq_flags);
611
612 XPC_DISCONNECT_CHANNEL(ch, reason, &irq_flags);
613
614 spin_unlock_irqrestore(&ch->lock, irq_flags);
615 xpc_msgqueue_deref(ch);
616 }
617
618 xpc_wakeup_channel_mgr(part);
619
620 xpc_part_deref(part);
621}
622
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700623/*
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700624 * Called by XP at the time of channel connection registration to cause
625 * XPC to establish connections to all currently active partitions.
626 */
627void
628xpc_initiate_connect(int ch_number)
629{
Dean Nelson64d032b2008-05-12 14:02:03 -0700630 short partid;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700631 struct xpc_partition *part;
632 struct xpc_channel *ch;
633
Dean Nelsonbc63d382008-07-29 22:34:04 -0700634 DBUG_ON(ch_number < 0 || ch_number >= XPC_MAX_NCHANNELS);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700635
Dean Nelsonbc63d382008-07-29 22:34:04 -0700636 for (partid = 0; partid < xp_max_npartitions; partid++) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700637 part = &xpc_partitions[partid];
638
639 if (xpc_part_ref(part)) {
640 ch = &part->channels[ch_number];
641
Dean Nelsone54af722005-10-25 14:07:43 -0500642 /*
643 * Initiate the establishment of a connection on the
644 * newly registered channel to the remote partition.
645 */
646 xpc_wakeup_channel_mgr(part);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700647 xpc_part_deref(part);
648 }
649 }
650}
651
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700652void
653xpc_connected_callout(struct xpc_channel *ch)
654{
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700655 /* let the registerer know that a connection has been established */
656
657 if (ch->func != NULL) {
Dean Nelson65c17b82008-05-12 14:02:02 -0700658 dev_dbg(xpc_chan, "ch->func() called, reason=xpConnected, "
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700659 "partid=%d, channel=%d\n", ch->partid, ch->number);
660
Dean Nelson65c17b82008-05-12 14:02:02 -0700661 ch->func(xpConnected, ch->partid, ch->number,
Dean Nelson35190502008-04-22 14:48:55 -0500662 (void *)(u64)ch->local_nentries, ch->key);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700663
Dean Nelson65c17b82008-05-12 14:02:02 -0700664 dev_dbg(xpc_chan, "ch->func() returned, reason=xpConnected, "
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700665 "partid=%d, channel=%d\n", ch->partid, ch->number);
666 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700667}
668
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700669/*
670 * Called by XP at the time of channel connection unregistration to cause
671 * XPC to teardown all current connections for the specified channel.
672 *
673 * Before returning xpc_initiate_disconnect() will wait until all connections
674 * on the specified channel have been closed/torndown. So the caller can be
675 * assured that they will not be receiving any more callouts from XPC to the
676 * function they registered via xpc_connect().
677 *
678 * Arguments:
679 *
680 * ch_number - channel # to unregister.
681 */
682void
683xpc_initiate_disconnect(int ch_number)
684{
685 unsigned long irq_flags;
Dean Nelson64d032b2008-05-12 14:02:03 -0700686 short partid;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700687 struct xpc_partition *part;
688 struct xpc_channel *ch;
689
Dean Nelsonbc63d382008-07-29 22:34:04 -0700690 DBUG_ON(ch_number < 0 || ch_number >= XPC_MAX_NCHANNELS);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700691
692 /* initiate the channel disconnect for every active partition */
Dean Nelsonbc63d382008-07-29 22:34:04 -0700693 for (partid = 0; partid < xp_max_npartitions; partid++) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700694 part = &xpc_partitions[partid];
695
696 if (xpc_part_ref(part)) {
697 ch = &part->channels[ch_number];
698 xpc_msgqueue_ref(ch);
699
700 spin_lock_irqsave(&ch->lock, irq_flags);
701
Dean Nelsona607c382005-09-01 14:01:37 -0500702 if (!(ch->flags & XPC_C_DISCONNECTED)) {
703 ch->flags |= XPC_C_WDISCONNECT;
704
Dean Nelson65c17b82008-05-12 14:02:02 -0700705 XPC_DISCONNECT_CHANNEL(ch, xpUnregistering,
Dean Nelson35190502008-04-22 14:48:55 -0500706 &irq_flags);
Dean Nelsona607c382005-09-01 14:01:37 -0500707 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700708
709 spin_unlock_irqrestore(&ch->lock, irq_flags);
710
711 xpc_msgqueue_deref(ch);
712 xpc_part_deref(part);
713 }
714 }
715
716 xpc_disconnect_wait(ch_number);
717}
718
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700719/*
720 * To disconnect a channel, and reflect it back to all who may be waiting.
721 *
Dean Nelsona607c382005-09-01 14:01:37 -0500722 * An OPEN is not allowed until XPC_C_DISCONNECTING is cleared by
723 * xpc_process_disconnect(), and if set, XPC_C_WDISCONNECT is cleared by
724 * xpc_disconnect_wait().
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700725 *
726 * THE CHANNEL IS TO BE LOCKED BY THE CALLER AND WILL REMAIN LOCKED UPON RETURN.
727 */
728void
729xpc_disconnect_channel(const int line, struct xpc_channel *ch,
Dean Nelson65c17b82008-05-12 14:02:02 -0700730 enum xp_retval reason, unsigned long *irq_flags)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700731{
Dean Nelsona607c382005-09-01 14:01:37 -0500732 u32 channel_was_connected = (ch->flags & XPC_C_CONNECTED);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700733
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700734 DBUG_ON(!spin_is_locked(&ch->lock));
735
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500736 if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_DISCONNECTED))
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700737 return;
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500738
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700739 DBUG_ON(!(ch->flags & (XPC_C_CONNECTING | XPC_C_CONNECTED)));
740
741 dev_dbg(xpc_chan, "reason=%d, line=%d, partid=%d, channel=%d\n",
742 reason, line, ch->partid, ch->number);
743
744 XPC_SET_REASON(ch, reason, line);
745
Dean Nelsona607c382005-09-01 14:01:37 -0500746 ch->flags |= (XPC_C_CLOSEREQUEST | XPC_C_DISCONNECTING);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700747 /* some of these may not have been set */
748 ch->flags &= ~(XPC_C_OPENREQUEST | XPC_C_OPENREPLY |
Dean Nelson35190502008-04-22 14:48:55 -0500749 XPC_C_ROPENREQUEST | XPC_C_ROPENREPLY |
750 XPC_C_CONNECTING | XPC_C_CONNECTED);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700751
Dean Nelson7fb5e592008-07-29 22:34:10 -0700752 xpc_send_chctl_closerequest(ch, irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700753
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500754 if (channel_was_connected)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700755 ch->flags |= XPC_C_WASCONNECTED;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700756
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700757 spin_unlock_irqrestore(&ch->lock, *irq_flags);
758
Dean Nelsona607c382005-09-01 14:01:37 -0500759 /* wake all idle kthreads so they can exit */
760 if (atomic_read(&ch->kthreads_idle) > 0) {
761 wake_up_all(&ch->idle_wq);
Dean Nelsona460ef82006-11-22 08:25:00 -0600762
763 } else if ((ch->flags & XPC_C_CONNECTEDCALLOUT_MADE) &&
Dean Nelson35190502008-04-22 14:48:55 -0500764 !(ch->flags & XPC_C_DISCONNECTINGCALLOUT)) {
Dean Nelson65c17b82008-05-12 14:02:02 -0700765 /* start a kthread that will do the xpDisconnecting callout */
Dean Nelsona460ef82006-11-22 08:25:00 -0600766 xpc_create_kthreads(ch, 1, 1);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700767 }
768
Dean Nelsona607c382005-09-01 14:01:37 -0500769 /* wake those waiting to allocate an entry from the local msg queue */
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500770 if (atomic_read(&ch->n_on_msg_allocate_wq) > 0)
Dean Nelsona607c382005-09-01 14:01:37 -0500771 wake_up(&ch->msg_allocate_wq);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700772
773 spin_lock_irqsave(&ch->lock, *irq_flags);
774}
775
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700776void
Dean Nelson65c17b82008-05-12 14:02:02 -0700777xpc_disconnect_callout(struct xpc_channel *ch, enum xp_retval reason)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700778{
779 /*
Dean Nelsona607c382005-09-01 14:01:37 -0500780 * Let the channel's registerer know that the channel is being
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700781 * disconnected. We don't want to do this if the registerer was never
Dean Nelsona607c382005-09-01 14:01:37 -0500782 * informed of a connection being made.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700783 */
784
785 if (ch->func != NULL) {
Dean Nelson246c7e32005-12-22 14:32:56 -0600786 dev_dbg(xpc_chan, "ch->func() called, reason=%d, partid=%d, "
787 "channel=%d\n", reason, ch->partid, ch->number);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700788
Dean Nelson246c7e32005-12-22 14:32:56 -0600789 ch->func(reason, ch->partid, ch->number, NULL, ch->key);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700790
Dean Nelson246c7e32005-12-22 14:32:56 -0600791 dev_dbg(xpc_chan, "ch->func() returned, reason=%d, partid=%d, "
792 "channel=%d\n", reason, ch->partid, ch->number);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700793 }
794}
795
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700796/*
797 * Wait for a message entry to become available for the specified channel,
798 * but don't wait any longer than 1 jiffy.
799 */
Dean Nelson33ba3c72008-07-29 22:34:07 -0700800enum xp_retval
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700801xpc_allocate_msg_wait(struct xpc_channel *ch)
802{
Dean Nelson65c17b82008-05-12 14:02:02 -0700803 enum xp_retval ret;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700804
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700805 if (ch->flags & XPC_C_DISCONNECTING) {
Dean Nelson65c17b82008-05-12 14:02:02 -0700806 DBUG_ON(ch->reason == xpInterrupted);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700807 return ch->reason;
808 }
809
810 atomic_inc(&ch->n_on_msg_allocate_wq);
811 ret = interruptible_sleep_on_timeout(&ch->msg_allocate_wq, 1);
812 atomic_dec(&ch->n_on_msg_allocate_wq);
813
814 if (ch->flags & XPC_C_DISCONNECTING) {
815 ret = ch->reason;
Dean Nelson65c17b82008-05-12 14:02:02 -0700816 DBUG_ON(ch->reason == xpInterrupted);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700817 } else if (ret == 0) {
Dean Nelson65c17b82008-05-12 14:02:02 -0700818 ret = xpTimeout;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700819 } else {
Dean Nelson65c17b82008-05-12 14:02:02 -0700820 ret = xpInterrupted;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700821 }
822
823 return ret;
824}
825
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700826/*
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700827 * Send a message that contains the user's payload on the specified channel
828 * connected to the specified partition.
829 *
830 * NOTE that this routine can sleep waiting for a message entry to become
831 * available. To not sleep, pass in the XPC_NOWAIT flag.
832 *
833 * Once sent, this routine will not wait for the message to be received, nor
834 * will notification be given when it does happen.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700835 *
836 * Arguments:
837 *
838 * partid - ID of partition to which the channel is connected.
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700839 * ch_number - channel # to send message on.
840 * flags - see xp.h for valid flags.
841 * payload - pointer to the payload which is to be sent.
842 * payload_size - size of the payload in bytes.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700843 */
Dean Nelson65c17b82008-05-12 14:02:02 -0700844enum xp_retval
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700845xpc_initiate_send(short partid, int ch_number, u32 flags, void *payload,
846 u16 payload_size)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700847{
848 struct xpc_partition *part = &xpc_partitions[partid];
Dean Nelson65c17b82008-05-12 14:02:02 -0700849 enum xp_retval ret = xpUnknownReason;
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700850
851 dev_dbg(xpc_chan, "payload=0x%p, partid=%d, channel=%d\n", payload,
852 partid, ch_number);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700853
Dean Nelsonbc63d382008-07-29 22:34:04 -0700854 DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700855 DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700856 DBUG_ON(payload == NULL);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700857
858 if (xpc_part_ref(part)) {
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700859 ret = xpc_send_payload(&part->channels[ch_number], flags,
860 payload, payload_size, 0, NULL, NULL);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700861 xpc_part_deref(part);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700862 }
863
864 return ret;
865}
866
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700867/*
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700868 * Send a message that contains the user's payload on the specified channel
869 * connected to the specified partition.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700870 *
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700871 * NOTE that this routine can sleep waiting for a message entry to become
872 * available. To not sleep, pass in the XPC_NOWAIT flag.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700873 *
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700874 * This routine will not wait for the message to be sent or received.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700875 *
876 * Once the remote end of the channel has received the message, the function
877 * passed as an argument to xpc_initiate_send_notify() will be called. This
878 * allows the sender to free up or re-use any buffers referenced by the
879 * message, but does NOT mean the message has been processed at the remote
880 * end by a receiver.
881 *
882 * If this routine returns an error, the caller's function will NOT be called.
883 *
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700884 * Arguments:
885 *
886 * partid - ID of partition to which the channel is connected.
887 * ch_number - channel # to send message on.
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700888 * flags - see xp.h for valid flags.
889 * payload - pointer to the payload which is to be sent.
890 * payload_size - size of the payload in bytes.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700891 * func - function to call with asynchronous notification of message
892 * receipt. THIS FUNCTION MUST BE NON-BLOCKING.
893 * key - user-defined key to be passed to the function when it's called.
894 */
Dean Nelson65c17b82008-05-12 14:02:02 -0700895enum xp_retval
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700896xpc_initiate_send_notify(short partid, int ch_number, u32 flags, void *payload,
897 u16 payload_size, xpc_notify_func func, void *key)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700898{
899 struct xpc_partition *part = &xpc_partitions[partid];
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700900 enum xp_retval ret = xpUnknownReason;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700901
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700902 dev_dbg(xpc_chan, "payload=0x%p, partid=%d, channel=%d\n", payload,
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700903 partid, ch_number);
904
Dean Nelsonbc63d382008-07-29 22:34:04 -0700905 DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700906 DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700907 DBUG_ON(payload == NULL);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700908 DBUG_ON(func == NULL);
909
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700910 if (xpc_part_ref(part)) {
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700911 ret = xpc_send_payload(&part->channels[ch_number], flags,
912 payload, payload_size, XPC_N_CALL, func,
913 key);
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700914 xpc_part_deref(part);
915 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700916 return ret;
917}
918
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700919/*
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700920 * Deliver a message's payload to its intended recipient.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700921 */
922void
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700923xpc_deliver_payload(struct xpc_channel *ch)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700924{
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700925 void *payload;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700926
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700927 payload = xpc_get_deliverable_payload(ch);
928 if (payload != NULL) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700929
930 /*
931 * This ref is taken to protect the payload itself from being
932 * freed before the user is finished with it, which the user
933 * indicates by calling xpc_initiate_received().
934 */
935 xpc_msgqueue_ref(ch);
936
937 atomic_inc(&ch->kthreads_active);
938
939 if (ch->func != NULL) {
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700940 dev_dbg(xpc_chan, "ch->func() called, payload=0x%p "
941 "partid=%d channel=%d\n", payload, ch->partid,
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700942 ch->number);
943
944 /* deliver the message to its intended recipient */
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700945 ch->func(xpMsgReceived, ch->partid, ch->number, payload,
946 ch->key);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700947
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700948 dev_dbg(xpc_chan, "ch->func() returned, payload=0x%p "
949 "partid=%d channel=%d\n", payload, ch->partid,
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700950 ch->number);
951 }
952
953 atomic_dec(&ch->kthreads_active);
954 }
955}
956
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700957/*
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700958 * Acknowledge receipt of a delivered message's payload.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700959 *
960 * This function, although called by users, does not call xpc_part_ref() to
961 * ensure that the partition infrastructure is in place. It relies on the
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700962 * fact that we called xpc_msgqueue_ref() in xpc_deliver_payload().
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700963 *
964 * Arguments:
965 *
966 * partid - ID of partition to which the channel is connected.
967 * ch_number - channel # message received on.
968 * payload - pointer to the payload area allocated via
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700969 * xpc_initiate_send() or xpc_initiate_send_notify().
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700970 */
971void
Dean Nelson64d032b2008-05-12 14:02:03 -0700972xpc_initiate_received(short partid, int ch_number, void *payload)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700973{
974 struct xpc_partition *part = &xpc_partitions[partid];
975 struct xpc_channel *ch;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700976
Dean Nelsonbc63d382008-07-29 22:34:04 -0700977 DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700978 DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
979
980 ch = &part->channels[ch_number];
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700981 xpc_received_payload(ch, payload);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700982
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700983 /* the call to xpc_msgqueue_ref() was done by xpc_deliver_payload() */
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700984 xpc_msgqueue_deref(ch);
985}