blob: 99a2534c38a1ffa092ecc9f1334a8d12aff1a16f [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;
Jack Steiner6f2584f2009-04-02 16:59:10 -0700186 enum xp_retval ret;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700187
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700188 spin_lock_irqsave(&ch->lock, irq_flags);
189
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500190again:
Dean Nelsone54af722005-10-25 14:07:43 -0500191
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500192 if ((ch->flags & XPC_C_DISCONNECTED) &&
193 (ch->flags & XPC_C_WDISCONNECT)) {
Dean Nelsone54af722005-10-25 14:07:43 -0500194 /*
Dean Nelson7fb5e592008-07-29 22:34:10 -0700195 * Delay processing chctl flags until thread waiting disconnect
Dean Nelsone54af722005-10-25 14:07:43 -0500196 * has had a chance to see that the channel is disconnected.
197 */
Dean Nelson7fb5e592008-07-29 22:34:10 -0700198 ch->delayed_chctl_flags |= chctl_flags;
Dean Nelsone54af722005-10-25 14:07:43 -0500199 spin_unlock_irqrestore(&ch->lock, irq_flags);
200 return;
201 }
202
Dean Nelson7fb5e592008-07-29 22:34:10 -0700203 if (chctl_flags & XPC_CHCTL_CLOSEREQUEST) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700204
Dean Nelson7fb5e592008-07-29 22:34:10 -0700205 dev_dbg(xpc_chan, "XPC_CHCTL_CLOSEREQUEST (reason=%d) received "
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700206 "from partid=%d, channel=%d\n", args->reason,
207 ch->partid, ch->number);
208
209 /*
210 * If RCLOSEREQUEST is set, we're probably waiting for
211 * RCLOSEREPLY. We should find it and a ROPENREQUEST packed
Dean Nelson7fb5e592008-07-29 22:34:10 -0700212 * with this RCLOSEREQUEST in the chctl_flags.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700213 */
214
215 if (ch->flags & XPC_C_RCLOSEREQUEST) {
216 DBUG_ON(!(ch->flags & XPC_C_DISCONNECTING));
217 DBUG_ON(!(ch->flags & XPC_C_CLOSEREQUEST));
218 DBUG_ON(!(ch->flags & XPC_C_CLOSEREPLY));
219 DBUG_ON(ch->flags & XPC_C_RCLOSEREPLY);
220
Dean Nelson7fb5e592008-07-29 22:34:10 -0700221 DBUG_ON(!(chctl_flags & XPC_CHCTL_CLOSEREPLY));
222 chctl_flags &= ~XPC_CHCTL_CLOSEREPLY;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700223 ch->flags |= XPC_C_RCLOSEREPLY;
224
225 /* both sides have finished disconnecting */
226 xpc_process_disconnect(ch, &irq_flags);
Dean Nelsone54af722005-10-25 14:07:43 -0500227 DBUG_ON(!(ch->flags & XPC_C_DISCONNECTED));
228 goto again;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700229 }
230
231 if (ch->flags & XPC_C_DISCONNECTED) {
Dean Nelson7fb5e592008-07-29 22:34:10 -0700232 if (!(chctl_flags & XPC_CHCTL_OPENREQUEST)) {
233 if (part->chctl.flags[ch_number] &
234 XPC_CHCTL_OPENREQUEST) {
Dean Nelsone54af722005-10-25 14:07:43 -0500235
Dean Nelson7fb5e592008-07-29 22:34:10 -0700236 DBUG_ON(ch->delayed_chctl_flags != 0);
237 spin_lock(&part->chctl_lock);
238 part->chctl.flags[ch_number] |=
239 XPC_CHCTL_CLOSEREQUEST;
240 spin_unlock(&part->chctl_lock);
Dean Nelsone54af722005-10-25 14:07:43 -0500241 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700242 spin_unlock_irqrestore(&ch->lock, irq_flags);
243 return;
244 }
245
246 XPC_SET_REASON(ch, 0, 0);
247 ch->flags &= ~XPC_C_DISCONNECTED;
248
249 atomic_inc(&part->nchannels_active);
250 ch->flags |= (XPC_C_CONNECTING | XPC_C_ROPENREQUEST);
251 }
252
Dean Nelson7fb5e592008-07-29 22:34:10 -0700253 chctl_flags &= ~(XPC_CHCTL_OPENREQUEST | XPC_CHCTL_OPENREPLY);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700254
255 /*
256 * The meaningful CLOSEREQUEST connection state fields are:
257 * reason = reason connection is to be closed
258 */
259
260 ch->flags |= XPC_C_RCLOSEREQUEST;
261
262 if (!(ch->flags & XPC_C_DISCONNECTING)) {
263 reason = args->reason;
Dean Nelson65c17b82008-05-12 14:02:02 -0700264 if (reason <= xpSuccess || reason > xpUnknownReason)
265 reason = xpUnknownReason;
266 else if (reason == xpUnregistering)
267 reason = xpOtherUnregistering;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700268
269 XPC_DISCONNECT_CHANNEL(ch, reason, &irq_flags);
Dean Nelsone54af722005-10-25 14:07:43 -0500270
Dean Nelson7fb5e592008-07-29 22:34:10 -0700271 DBUG_ON(chctl_flags & XPC_CHCTL_CLOSEREPLY);
Dean Nelsone54af722005-10-25 14:07:43 -0500272 spin_unlock_irqrestore(&ch->lock, irq_flags);
273 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700274 }
Dean Nelsone54af722005-10-25 14:07:43 -0500275
276 xpc_process_disconnect(ch, &irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700277 }
278
Dean Nelson7fb5e592008-07-29 22:34:10 -0700279 if (chctl_flags & XPC_CHCTL_CLOSEREPLY) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700280
Dean Nelson7fb5e592008-07-29 22:34:10 -0700281 dev_dbg(xpc_chan, "XPC_CHCTL_CLOSEREPLY received from partid="
282 "%d, channel=%d\n", ch->partid, ch->number);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700283
284 if (ch->flags & XPC_C_DISCONNECTED) {
Dean Nelson83469b52008-07-29 22:34:18 -0700285 DBUG_ON(part->act_state != XPC_P_AS_DEACTIVATING);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700286 spin_unlock_irqrestore(&ch->lock, irq_flags);
287 return;
288 }
289
290 DBUG_ON(!(ch->flags & XPC_C_CLOSEREQUEST));
Dean Nelsone54af722005-10-25 14:07:43 -0500291
292 if (!(ch->flags & XPC_C_RCLOSEREQUEST)) {
Dean Nelson7fb5e592008-07-29 22:34:10 -0700293 if (part->chctl.flags[ch_number] &
294 XPC_CHCTL_CLOSEREQUEST) {
Dean Nelsone54af722005-10-25 14:07:43 -0500295
Dean Nelson7fb5e592008-07-29 22:34:10 -0700296 DBUG_ON(ch->delayed_chctl_flags != 0);
297 spin_lock(&part->chctl_lock);
298 part->chctl.flags[ch_number] |=
299 XPC_CHCTL_CLOSEREPLY;
300 spin_unlock(&part->chctl_lock);
Dean Nelsone54af722005-10-25 14:07:43 -0500301 }
302 spin_unlock_irqrestore(&ch->lock, irq_flags);
303 return;
304 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700305
306 ch->flags |= XPC_C_RCLOSEREPLY;
307
308 if (ch->flags & XPC_C_CLOSEREPLY) {
309 /* both sides have finished disconnecting */
310 xpc_process_disconnect(ch, &irq_flags);
311 }
312 }
313
Dean Nelson7fb5e592008-07-29 22:34:10 -0700314 if (chctl_flags & XPC_CHCTL_OPENREQUEST) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700315
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700316 dev_dbg(xpc_chan, "XPC_CHCTL_OPENREQUEST (entry_size=%d, "
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700317 "local_nentries=%d) received from partid=%d, "
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700318 "channel=%d\n", args->entry_size, args->local_nentries,
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700319 ch->partid, ch->number);
320
Dean Nelson83469b52008-07-29 22:34:18 -0700321 if (part->act_state == XPC_P_AS_DEACTIVATING ||
Dean Nelson35190502008-04-22 14:48:55 -0500322 (ch->flags & XPC_C_ROPENREQUEST)) {
Dean Nelsone54af722005-10-25 14:07:43 -0500323 spin_unlock_irqrestore(&ch->lock, irq_flags);
324 return;
325 }
326
327 if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_WDISCONNECT)) {
Dean Nelson7fb5e592008-07-29 22:34:10 -0700328 ch->delayed_chctl_flags |= XPC_CHCTL_OPENREQUEST;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700329 spin_unlock_irqrestore(&ch->lock, irq_flags);
330 return;
331 }
332 DBUG_ON(!(ch->flags & (XPC_C_DISCONNECTED |
Dean Nelson35190502008-04-22 14:48:55 -0500333 XPC_C_OPENREQUEST)));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700334 DBUG_ON(ch->flags & (XPC_C_ROPENREQUEST | XPC_C_ROPENREPLY |
Dean Nelson35190502008-04-22 14:48:55 -0500335 XPC_C_OPENREPLY | XPC_C_CONNECTED));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700336
337 /*
338 * The meaningful OPENREQUEST connection state fields are:
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700339 * entry_size = size of channel's messages in bytes
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700340 * local_nentries = remote partition's local_nentries
341 */
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700342 if (args->entry_size == 0 || args->local_nentries == 0) {
Dean Nelsone54af722005-10-25 14:07:43 -0500343 /* assume OPENREQUEST was delayed by mistake */
344 spin_unlock_irqrestore(&ch->lock, irq_flags);
345 return;
346 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700347
348 ch->flags |= (XPC_C_ROPENREQUEST | XPC_C_CONNECTING);
349 ch->remote_nentries = args->local_nentries;
350
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700351 if (ch->flags & XPC_C_OPENREQUEST) {
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700352 if (args->entry_size != ch->entry_size) {
Dean Nelson65c17b82008-05-12 14:02:02 -0700353 XPC_DISCONNECT_CHANNEL(ch, xpUnequalMsgSizes,
Dean Nelson35190502008-04-22 14:48:55 -0500354 &irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700355 spin_unlock_irqrestore(&ch->lock, irq_flags);
356 return;
357 }
358 } else {
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700359 ch->entry_size = args->entry_size;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700360
361 XPC_SET_REASON(ch, 0, 0);
362 ch->flags &= ~XPC_C_DISCONNECTED;
363
364 atomic_inc(&part->nchannels_active);
365 }
366
367 xpc_process_connect(ch, &irq_flags);
368 }
369
Dean Nelson7fb5e592008-07-29 22:34:10 -0700370 if (chctl_flags & XPC_CHCTL_OPENREPLY) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700371
Dean Nelson7fb5e592008-07-29 22:34:10 -0700372 dev_dbg(xpc_chan, "XPC_CHCTL_OPENREPLY (local_msgqueue_pa="
373 "0x%lx, local_nentries=%d, remote_nentries=%d) "
374 "received from partid=%d, channel=%d\n",
Dean Nelsona812dcc2008-07-29 22:34:16 -0700375 args->local_msgqueue_pa, args->local_nentries,
376 args->remote_nentries, ch->partid, ch->number);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700377
378 if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_DISCONNECTED)) {
379 spin_unlock_irqrestore(&ch->lock, irq_flags);
380 return;
381 }
Dean Nelsone54af722005-10-25 14:07:43 -0500382 if (!(ch->flags & XPC_C_OPENREQUEST)) {
Dean Nelson65c17b82008-05-12 14:02:02 -0700383 XPC_DISCONNECT_CHANNEL(ch, xpOpenCloseError,
Dean Nelson35190502008-04-22 14:48:55 -0500384 &irq_flags);
Dean Nelsone54af722005-10-25 14:07:43 -0500385 spin_unlock_irqrestore(&ch->lock, irq_flags);
386 return;
387 }
388
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700389 DBUG_ON(!(ch->flags & XPC_C_ROPENREQUEST));
390 DBUG_ON(ch->flags & XPC_C_CONNECTED);
391
392 /*
393 * The meaningful OPENREPLY connection state fields are:
394 * local_msgqueue_pa = physical address of remote
Dean Nelson35190502008-04-22 14:48:55 -0500395 * partition's local_msgqueue
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700396 * local_nentries = remote partition's local_nentries
397 * remote_nentries = remote partition's remote_nentries
398 */
399 DBUG_ON(args->local_msgqueue_pa == 0);
400 DBUG_ON(args->local_nentries == 0);
401 DBUG_ON(args->remote_nentries == 0);
402
Jack Steiner6f2584f2009-04-02 16:59:10 -0700403 ret = xpc_save_remote_msgqueue_pa(ch, args->local_msgqueue_pa);
404 if (ret != xpSuccess) {
405 XPC_DISCONNECT_CHANNEL(ch, ret, &irq_flags);
406 spin_unlock_irqrestore(&ch->lock, irq_flags);
407 return;
408 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700409 ch->flags |= XPC_C_ROPENREPLY;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700410
411 if (args->local_nentries < ch->remote_nentries) {
Dean Nelson7fb5e592008-07-29 22:34:10 -0700412 dev_dbg(xpc_chan, "XPC_CHCTL_OPENREPLY: new "
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700413 "remote_nentries=%d, old remote_nentries=%d, "
414 "partid=%d, channel=%d\n",
415 args->local_nentries, ch->remote_nentries,
416 ch->partid, ch->number);
417
418 ch->remote_nentries = args->local_nentries;
419 }
420 if (args->remote_nentries < ch->local_nentries) {
Dean Nelson7fb5e592008-07-29 22:34:10 -0700421 dev_dbg(xpc_chan, "XPC_CHCTL_OPENREPLY: new "
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700422 "local_nentries=%d, old local_nentries=%d, "
423 "partid=%d, channel=%d\n",
424 args->remote_nentries, ch->local_nentries,
425 ch->partid, ch->number);
426
427 ch->local_nentries = args->remote_nentries;
428 }
429
430 xpc_process_connect(ch, &irq_flags);
431 }
432
433 spin_unlock_irqrestore(&ch->lock, irq_flags);
434}
435
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700436/*
437 * Attempt to establish a channel connection to a remote partition.
438 */
Dean Nelson65c17b82008-05-12 14:02:02 -0700439static enum xp_retval
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700440xpc_connect_channel(struct xpc_channel *ch)
441{
442 unsigned long irq_flags;
443 struct xpc_registration *registration = &xpc_registrations[ch->number];
444
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500445 if (mutex_trylock(&registration->mutex) == 0)
Dean Nelson65c17b82008-05-12 14:02:02 -0700446 return xpRetry;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700447
448 if (!XPC_CHANNEL_REGISTERED(ch->number)) {
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500449 mutex_unlock(&registration->mutex);
Dean Nelson65c17b82008-05-12 14:02:02 -0700450 return xpUnregistered;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700451 }
452
453 spin_lock_irqsave(&ch->lock, irq_flags);
454
455 DBUG_ON(ch->flags & XPC_C_CONNECTED);
456 DBUG_ON(ch->flags & XPC_C_OPENREQUEST);
457
458 if (ch->flags & XPC_C_DISCONNECTING) {
459 spin_unlock_irqrestore(&ch->lock, irq_flags);
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500460 mutex_unlock(&registration->mutex);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700461 return ch->reason;
462 }
463
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700464 /* add info from the channel connect registration to the channel */
465
466 ch->kthreads_assigned_limit = registration->assigned_limit;
467 ch->kthreads_idle_limit = registration->idle_limit;
468 DBUG_ON(atomic_read(&ch->kthreads_assigned) != 0);
469 DBUG_ON(atomic_read(&ch->kthreads_idle) != 0);
470 DBUG_ON(atomic_read(&ch->kthreads_active) != 0);
471
472 ch->func = registration->func;
473 DBUG_ON(registration->func == NULL);
474 ch->key = registration->key;
475
476 ch->local_nentries = registration->nentries;
477
478 if (ch->flags & XPC_C_ROPENREQUEST) {
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700479 if (registration->entry_size != ch->entry_size) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700480 /* the local and remote sides aren't the same */
481
482 /*
483 * Because XPC_DISCONNECT_CHANNEL() can block we're
484 * forced to up the registration sema before we unlock
485 * the channel lock. But that's okay here because we're
486 * done with the part that required the registration
487 * sema. XPC_DISCONNECT_CHANNEL() requires that the
488 * channel lock be locked and will unlock and relock
489 * the channel lock as needed.
490 */
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500491 mutex_unlock(&registration->mutex);
Dean Nelson65c17b82008-05-12 14:02:02 -0700492 XPC_DISCONNECT_CHANNEL(ch, xpUnequalMsgSizes,
Dean Nelson35190502008-04-22 14:48:55 -0500493 &irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700494 spin_unlock_irqrestore(&ch->lock, irq_flags);
Dean Nelson65c17b82008-05-12 14:02:02 -0700495 return xpUnequalMsgSizes;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700496 }
497 } else {
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700498 ch->entry_size = registration->entry_size;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700499
500 XPC_SET_REASON(ch, 0, 0);
501 ch->flags &= ~XPC_C_DISCONNECTED;
502
503 atomic_inc(&xpc_partitions[ch->partid].nchannels_active);
504 }
505
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500506 mutex_unlock(&registration->mutex);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700507
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700508 /* initiate the connection */
509
510 ch->flags |= (XPC_C_OPENREQUEST | XPC_C_CONNECTING);
Dean Nelson7fb5e592008-07-29 22:34:10 -0700511 xpc_send_chctl_openrequest(ch, &irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700512
513 xpc_process_connect(ch, &irq_flags);
514
515 spin_unlock_irqrestore(&ch->lock, irq_flags);
516
Dean Nelson65c17b82008-05-12 14:02:02 -0700517 return xpSuccess;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700518}
519
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700520void
Dean Nelson7fb5e592008-07-29 22:34:10 -0700521xpc_process_sent_chctl_flags(struct xpc_partition *part)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700522{
523 unsigned long irq_flags;
Dean Nelson7fb5e592008-07-29 22:34:10 -0700524 union xpc_channel_ctl_flags chctl;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700525 struct xpc_channel *ch;
526 int ch_number;
Dean Nelsona607c382005-09-01 14:01:37 -0500527 u32 ch_flags;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700528
Dean Nelson7fb5e592008-07-29 22:34:10 -0700529 chctl.all_flags = xpc_get_chctl_all_flags(part);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700530
531 /*
532 * Initiate channel connections for registered channels.
533 *
534 * For each connected channel that has pending messages activate idle
535 * kthreads and/or create new kthreads as needed.
536 */
537
538 for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
539 ch = &part->channels[ch_number];
540
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700541 /*
Dean Nelson7fb5e592008-07-29 22:34:10 -0700542 * Process any open or close related chctl flags, and then deal
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700543 * with connecting or disconnecting the channel as required.
544 */
545
Dean Nelson7fb5e592008-07-29 22:34:10 -0700546 if (chctl.flags[ch_number] & XPC_OPENCLOSE_CHCTL_FLAGS) {
547 xpc_process_openclose_chctl_flags(part, ch_number,
548 chctl.flags[ch_number]);
549 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700550
Dean Nelsona607c382005-09-01 14:01:37 -0500551 ch_flags = ch->flags; /* need an atomic snapshot of flags */
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700552
Dean Nelsona607c382005-09-01 14:01:37 -0500553 if (ch_flags & XPC_C_DISCONNECTING) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700554 spin_lock_irqsave(&ch->lock, irq_flags);
555 xpc_process_disconnect(ch, &irq_flags);
556 spin_unlock_irqrestore(&ch->lock, irq_flags);
557 continue;
558 }
559
Dean Nelson83469b52008-07-29 22:34:18 -0700560 if (part->act_state == XPC_P_AS_DEACTIVATING)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700561 continue;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700562
Dean Nelsona607c382005-09-01 14:01:37 -0500563 if (!(ch_flags & XPC_C_CONNECTED)) {
564 if (!(ch_flags & XPC_C_OPENREQUEST)) {
565 DBUG_ON(ch_flags & XPC_C_SETUP);
Dean Nelson35190502008-04-22 14:48:55 -0500566 (void)xpc_connect_channel(ch);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700567 } else {
568 spin_lock_irqsave(&ch->lock, irq_flags);
569 xpc_process_connect(ch, &irq_flags);
570 spin_unlock_irqrestore(&ch->lock, irq_flags);
571 }
572 continue;
573 }
574
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700575 /*
Dean Nelson7fb5e592008-07-29 22:34:10 -0700576 * Process any message related chctl flags, this may involve
577 * the activation of kthreads to deliver any pending messages
578 * sent from the other partition.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700579 */
580
Dean Nelson7fb5e592008-07-29 22:34:10 -0700581 if (chctl.flags[ch_number] & XPC_MSG_CHCTL_FLAGS)
582 xpc_process_msg_chctl_flags(part, ch_number);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700583 }
584}
585
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700586/*
Dean Nelsona607c382005-09-01 14:01:37 -0500587 * XPC's heartbeat code calls this function to inform XPC that a partition is
588 * going down. XPC responds by tearing down the XPartition Communication
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700589 * infrastructure used for the just downed partition.
590 *
591 * XPC's heartbeat code will never call this function and xpc_partition_up()
592 * at the same time. Nor will it ever make multiple calls to either function
593 * at the same time.
594 */
595void
Dean Nelson65c17b82008-05-12 14:02:02 -0700596xpc_partition_going_down(struct xpc_partition *part, enum xp_retval reason)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700597{
598 unsigned long irq_flags;
599 int ch_number;
600 struct xpc_channel *ch;
601
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700602 dev_dbg(xpc_chan, "deactivating partition %d, reason=%d\n",
603 XPC_PARTID(part), reason);
604
605 if (!xpc_part_ref(part)) {
606 /* infrastructure for this partition isn't currently set up */
607 return;
608 }
609
Dean Nelsona607c382005-09-01 14:01:37 -0500610 /* disconnect channels associated with the partition going down */
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700611
612 for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
613 ch = &part->channels[ch_number];
614
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700615 xpc_msgqueue_ref(ch);
616 spin_lock_irqsave(&ch->lock, irq_flags);
617
618 XPC_DISCONNECT_CHANNEL(ch, reason, &irq_flags);
619
620 spin_unlock_irqrestore(&ch->lock, irq_flags);
621 xpc_msgqueue_deref(ch);
622 }
623
624 xpc_wakeup_channel_mgr(part);
625
626 xpc_part_deref(part);
627}
628
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700629/*
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700630 * Called by XP at the time of channel connection registration to cause
631 * XPC to establish connections to all currently active partitions.
632 */
633void
634xpc_initiate_connect(int ch_number)
635{
Dean Nelson64d032b2008-05-12 14:02:03 -0700636 short partid;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700637 struct xpc_partition *part;
638 struct xpc_channel *ch;
639
Dean Nelsonbc63d382008-07-29 22:34:04 -0700640 DBUG_ON(ch_number < 0 || ch_number >= XPC_MAX_NCHANNELS);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700641
Dean Nelsonbc63d382008-07-29 22:34:04 -0700642 for (partid = 0; partid < xp_max_npartitions; partid++) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700643 part = &xpc_partitions[partid];
644
645 if (xpc_part_ref(part)) {
646 ch = &part->channels[ch_number];
647
Dean Nelsone54af722005-10-25 14:07:43 -0500648 /*
649 * Initiate the establishment of a connection on the
650 * newly registered channel to the remote partition.
651 */
652 xpc_wakeup_channel_mgr(part);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700653 xpc_part_deref(part);
654 }
655 }
656}
657
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700658void
659xpc_connected_callout(struct xpc_channel *ch)
660{
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700661 /* let the registerer know that a connection has been established */
662
663 if (ch->func != NULL) {
Dean Nelson65c17b82008-05-12 14:02:02 -0700664 dev_dbg(xpc_chan, "ch->func() called, reason=xpConnected, "
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700665 "partid=%d, channel=%d\n", ch->partid, ch->number);
666
Dean Nelson65c17b82008-05-12 14:02:02 -0700667 ch->func(xpConnected, ch->partid, ch->number,
Dean Nelson35190502008-04-22 14:48:55 -0500668 (void *)(u64)ch->local_nentries, ch->key);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700669
Dean Nelson65c17b82008-05-12 14:02:02 -0700670 dev_dbg(xpc_chan, "ch->func() returned, reason=xpConnected, "
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700671 "partid=%d, channel=%d\n", ch->partid, ch->number);
672 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700673}
674
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700675/*
676 * Called by XP at the time of channel connection unregistration to cause
677 * XPC to teardown all current connections for the specified channel.
678 *
679 * Before returning xpc_initiate_disconnect() will wait until all connections
680 * on the specified channel have been closed/torndown. So the caller can be
681 * assured that they will not be receiving any more callouts from XPC to the
682 * function they registered via xpc_connect().
683 *
684 * Arguments:
685 *
686 * ch_number - channel # to unregister.
687 */
688void
689xpc_initiate_disconnect(int ch_number)
690{
691 unsigned long irq_flags;
Dean Nelson64d032b2008-05-12 14:02:03 -0700692 short partid;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700693 struct xpc_partition *part;
694 struct xpc_channel *ch;
695
Dean Nelsonbc63d382008-07-29 22:34:04 -0700696 DBUG_ON(ch_number < 0 || ch_number >= XPC_MAX_NCHANNELS);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700697
698 /* initiate the channel disconnect for every active partition */
Dean Nelsonbc63d382008-07-29 22:34:04 -0700699 for (partid = 0; partid < xp_max_npartitions; partid++) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700700 part = &xpc_partitions[partid];
701
702 if (xpc_part_ref(part)) {
703 ch = &part->channels[ch_number];
704 xpc_msgqueue_ref(ch);
705
706 spin_lock_irqsave(&ch->lock, irq_flags);
707
Dean Nelsona607c382005-09-01 14:01:37 -0500708 if (!(ch->flags & XPC_C_DISCONNECTED)) {
709 ch->flags |= XPC_C_WDISCONNECT;
710
Dean Nelson65c17b82008-05-12 14:02:02 -0700711 XPC_DISCONNECT_CHANNEL(ch, xpUnregistering,
Dean Nelson35190502008-04-22 14:48:55 -0500712 &irq_flags);
Dean Nelsona607c382005-09-01 14:01:37 -0500713 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700714
715 spin_unlock_irqrestore(&ch->lock, irq_flags);
716
717 xpc_msgqueue_deref(ch);
718 xpc_part_deref(part);
719 }
720 }
721
722 xpc_disconnect_wait(ch_number);
723}
724
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700725/*
726 * To disconnect a channel, and reflect it back to all who may be waiting.
727 *
Dean Nelsona607c382005-09-01 14:01:37 -0500728 * An OPEN is not allowed until XPC_C_DISCONNECTING is cleared by
729 * xpc_process_disconnect(), and if set, XPC_C_WDISCONNECT is cleared by
730 * xpc_disconnect_wait().
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700731 *
732 * THE CHANNEL IS TO BE LOCKED BY THE CALLER AND WILL REMAIN LOCKED UPON RETURN.
733 */
734void
735xpc_disconnect_channel(const int line, struct xpc_channel *ch,
Dean Nelson65c17b82008-05-12 14:02:02 -0700736 enum xp_retval reason, unsigned long *irq_flags)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700737{
Dean Nelsona607c382005-09-01 14:01:37 -0500738 u32 channel_was_connected = (ch->flags & XPC_C_CONNECTED);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700739
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700740 DBUG_ON(!spin_is_locked(&ch->lock));
741
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500742 if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_DISCONNECTED))
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700743 return;
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500744
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700745 DBUG_ON(!(ch->flags & (XPC_C_CONNECTING | XPC_C_CONNECTED)));
746
747 dev_dbg(xpc_chan, "reason=%d, line=%d, partid=%d, channel=%d\n",
748 reason, line, ch->partid, ch->number);
749
750 XPC_SET_REASON(ch, reason, line);
751
Dean Nelsona607c382005-09-01 14:01:37 -0500752 ch->flags |= (XPC_C_CLOSEREQUEST | XPC_C_DISCONNECTING);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700753 /* some of these may not have been set */
754 ch->flags &= ~(XPC_C_OPENREQUEST | XPC_C_OPENREPLY |
Dean Nelson35190502008-04-22 14:48:55 -0500755 XPC_C_ROPENREQUEST | XPC_C_ROPENREPLY |
756 XPC_C_CONNECTING | XPC_C_CONNECTED);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700757
Dean Nelson7fb5e592008-07-29 22:34:10 -0700758 xpc_send_chctl_closerequest(ch, irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700759
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500760 if (channel_was_connected)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700761 ch->flags |= XPC_C_WASCONNECTED;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700762
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700763 spin_unlock_irqrestore(&ch->lock, *irq_flags);
764
Dean Nelsona607c382005-09-01 14:01:37 -0500765 /* wake all idle kthreads so they can exit */
766 if (atomic_read(&ch->kthreads_idle) > 0) {
767 wake_up_all(&ch->idle_wq);
Dean Nelsona460ef82006-11-22 08:25:00 -0600768
769 } else if ((ch->flags & XPC_C_CONNECTEDCALLOUT_MADE) &&
Dean Nelson35190502008-04-22 14:48:55 -0500770 !(ch->flags & XPC_C_DISCONNECTINGCALLOUT)) {
Dean Nelson65c17b82008-05-12 14:02:02 -0700771 /* start a kthread that will do the xpDisconnecting callout */
Dean Nelsona460ef82006-11-22 08:25:00 -0600772 xpc_create_kthreads(ch, 1, 1);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700773 }
774
Dean Nelsona607c382005-09-01 14:01:37 -0500775 /* wake those waiting to allocate an entry from the local msg queue */
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500776 if (atomic_read(&ch->n_on_msg_allocate_wq) > 0)
Dean Nelsona607c382005-09-01 14:01:37 -0500777 wake_up(&ch->msg_allocate_wq);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700778
779 spin_lock_irqsave(&ch->lock, *irq_flags);
780}
781
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700782void
Dean Nelson65c17b82008-05-12 14:02:02 -0700783xpc_disconnect_callout(struct xpc_channel *ch, enum xp_retval reason)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700784{
785 /*
Dean Nelsona607c382005-09-01 14:01:37 -0500786 * Let the channel's registerer know that the channel is being
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700787 * disconnected. We don't want to do this if the registerer was never
Dean Nelsona607c382005-09-01 14:01:37 -0500788 * informed of a connection being made.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700789 */
790
791 if (ch->func != NULL) {
Dean Nelson246c7e32005-12-22 14:32:56 -0600792 dev_dbg(xpc_chan, "ch->func() called, reason=%d, partid=%d, "
793 "channel=%d\n", reason, ch->partid, ch->number);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700794
Dean Nelson246c7e32005-12-22 14:32:56 -0600795 ch->func(reason, ch->partid, ch->number, NULL, ch->key);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700796
Dean Nelson246c7e32005-12-22 14:32:56 -0600797 dev_dbg(xpc_chan, "ch->func() returned, reason=%d, partid=%d, "
798 "channel=%d\n", reason, ch->partid, ch->number);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700799 }
800}
801
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700802/*
803 * Wait for a message entry to become available for the specified channel,
804 * but don't wait any longer than 1 jiffy.
805 */
Dean Nelson33ba3c72008-07-29 22:34:07 -0700806enum xp_retval
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700807xpc_allocate_msg_wait(struct xpc_channel *ch)
808{
Dean Nelson65c17b82008-05-12 14:02:02 -0700809 enum xp_retval ret;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700810
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700811 if (ch->flags & XPC_C_DISCONNECTING) {
Dean Nelson65c17b82008-05-12 14:02:02 -0700812 DBUG_ON(ch->reason == xpInterrupted);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700813 return ch->reason;
814 }
815
816 atomic_inc(&ch->n_on_msg_allocate_wq);
817 ret = interruptible_sleep_on_timeout(&ch->msg_allocate_wq, 1);
818 atomic_dec(&ch->n_on_msg_allocate_wq);
819
820 if (ch->flags & XPC_C_DISCONNECTING) {
821 ret = ch->reason;
Dean Nelson65c17b82008-05-12 14:02:02 -0700822 DBUG_ON(ch->reason == xpInterrupted);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700823 } else if (ret == 0) {
Dean Nelson65c17b82008-05-12 14:02:02 -0700824 ret = xpTimeout;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700825 } else {
Dean Nelson65c17b82008-05-12 14:02:02 -0700826 ret = xpInterrupted;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700827 }
828
829 return ret;
830}
831
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700832/*
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700833 * Send a message that contains the user's payload on the specified channel
834 * connected to the specified partition.
835 *
836 * NOTE that this routine can sleep waiting for a message entry to become
837 * available. To not sleep, pass in the XPC_NOWAIT flag.
838 *
839 * Once sent, this routine will not wait for the message to be received, nor
840 * will notification be given when it does happen.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700841 *
842 * Arguments:
843 *
844 * partid - ID of partition to which the channel is connected.
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700845 * ch_number - channel # to send message on.
846 * flags - see xp.h for valid flags.
847 * payload - pointer to the payload which is to be sent.
848 * payload_size - size of the payload in bytes.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700849 */
Dean Nelson65c17b82008-05-12 14:02:02 -0700850enum xp_retval
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700851xpc_initiate_send(short partid, int ch_number, u32 flags, void *payload,
852 u16 payload_size)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700853{
854 struct xpc_partition *part = &xpc_partitions[partid];
Dean Nelson65c17b82008-05-12 14:02:02 -0700855 enum xp_retval ret = xpUnknownReason;
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700856
857 dev_dbg(xpc_chan, "payload=0x%p, partid=%d, channel=%d\n", payload,
858 partid, ch_number);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700859
Dean Nelsonbc63d382008-07-29 22:34:04 -0700860 DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700861 DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700862 DBUG_ON(payload == NULL);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700863
864 if (xpc_part_ref(part)) {
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700865 ret = xpc_send_payload(&part->channels[ch_number], flags,
866 payload, payload_size, 0, NULL, NULL);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700867 xpc_part_deref(part);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700868 }
869
870 return ret;
871}
872
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700873/*
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700874 * Send a message that contains the user's payload on the specified channel
875 * connected to the specified partition.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700876 *
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700877 * NOTE that this routine can sleep waiting for a message entry to become
878 * available. To not sleep, pass in the XPC_NOWAIT flag.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700879 *
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700880 * This routine will not wait for the message to be sent or received.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700881 *
882 * Once the remote end of the channel has received the message, the function
883 * passed as an argument to xpc_initiate_send_notify() will be called. This
884 * allows the sender to free up or re-use any buffers referenced by the
885 * message, but does NOT mean the message has been processed at the remote
886 * end by a receiver.
887 *
888 * If this routine returns an error, the caller's function will NOT be called.
889 *
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700890 * Arguments:
891 *
892 * partid - ID of partition to which the channel is connected.
893 * ch_number - channel # to send message on.
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700894 * flags - see xp.h for valid flags.
895 * payload - pointer to the payload which is to be sent.
896 * payload_size - size of the payload in bytes.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700897 * func - function to call with asynchronous notification of message
898 * receipt. THIS FUNCTION MUST BE NON-BLOCKING.
899 * key - user-defined key to be passed to the function when it's called.
900 */
Dean Nelson65c17b82008-05-12 14:02:02 -0700901enum xp_retval
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700902xpc_initiate_send_notify(short partid, int ch_number, u32 flags, void *payload,
903 u16 payload_size, xpc_notify_func func, void *key)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700904{
905 struct xpc_partition *part = &xpc_partitions[partid];
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700906 enum xp_retval ret = xpUnknownReason;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700907
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700908 dev_dbg(xpc_chan, "payload=0x%p, partid=%d, channel=%d\n", payload,
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700909 partid, ch_number);
910
Dean Nelsonbc63d382008-07-29 22:34:04 -0700911 DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700912 DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700913 DBUG_ON(payload == NULL);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700914 DBUG_ON(func == NULL);
915
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700916 if (xpc_part_ref(part)) {
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700917 ret = xpc_send_payload(&part->channels[ch_number], flags,
918 payload, payload_size, XPC_N_CALL, func,
919 key);
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700920 xpc_part_deref(part);
921 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700922 return ret;
923}
924
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700925/*
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700926 * Deliver a message's payload to its intended recipient.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700927 */
928void
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700929xpc_deliver_payload(struct xpc_channel *ch)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700930{
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700931 void *payload;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700932
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700933 payload = xpc_get_deliverable_payload(ch);
934 if (payload != NULL) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700935
936 /*
937 * This ref is taken to protect the payload itself from being
938 * freed before the user is finished with it, which the user
939 * indicates by calling xpc_initiate_received().
940 */
941 xpc_msgqueue_ref(ch);
942
943 atomic_inc(&ch->kthreads_active);
944
945 if (ch->func != NULL) {
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700946 dev_dbg(xpc_chan, "ch->func() called, payload=0x%p "
947 "partid=%d channel=%d\n", payload, ch->partid,
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700948 ch->number);
949
950 /* deliver the message to its intended recipient */
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700951 ch->func(xpMsgReceived, ch->partid, ch->number, payload,
952 ch->key);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700953
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700954 dev_dbg(xpc_chan, "ch->func() returned, payload=0x%p "
955 "partid=%d channel=%d\n", payload, ch->partid,
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700956 ch->number);
957 }
958
959 atomic_dec(&ch->kthreads_active);
960 }
961}
962
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700963/*
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700964 * Acknowledge receipt of a delivered message's payload.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700965 *
966 * This function, although called by users, does not call xpc_part_ref() to
967 * ensure that the partition infrastructure is in place. It relies on the
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700968 * fact that we called xpc_msgqueue_ref() in xpc_deliver_payload().
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700969 *
970 * Arguments:
971 *
972 * partid - ID of partition to which the channel is connected.
973 * ch_number - channel # message received on.
974 * payload - pointer to the payload area allocated via
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700975 * xpc_initiate_send() or xpc_initiate_send_notify().
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700976 */
977void
Dean Nelson64d032b2008-05-12 14:02:03 -0700978xpc_initiate_received(short partid, int ch_number, void *payload)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700979{
980 struct xpc_partition *part = &xpc_partitions[partid];
981 struct xpc_channel *ch;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700982
Dean Nelsonbc63d382008-07-29 22:34:04 -0700983 DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700984 DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
985
986 ch = &part->channels[ch_number];
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700987 xpc_received_payload(ch, payload);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700988
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700989 /* the call to xpc_msgqueue_ref() was done by xpc_deliver_payload() */
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700990 xpc_msgqueue_deref(ch);
991}