blob: d7a15f1a78a5c131c382d5fe6f44351aa6818336 [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);
42 ret = xpc_allocate_msgqueues(ch);
43 spin_lock_irqsave(&ch->lock, *irq_flags);
44
Dean Nelson65c17b82008-05-12 14:02:02 -070045 if (ret != xpSuccess)
Dean Nelson89eb8eb2005-03-23 19:50:00 -070046 XPC_DISCONNECT_CHANNEL(ch, ret, irq_flags);
Dean Nelson2c2b94f2008-04-22 14:50:17 -050047
Dean Nelson185c3a12008-07-29 22:34:11 -070048 ch->flags |= XPC_C_SETUP;
49
Dean Nelson2c2b94f2008-04-22 14:50:17 -050050 if (ch->flags & (XPC_C_CONNECTED | XPC_C_DISCONNECTING))
Dean Nelson89eb8eb2005-03-23 19:50:00 -070051 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -070052
Dean Nelson89eb8eb2005-03-23 19:50:00 -070053 DBUG_ON(ch->local_msgqueue == NULL);
54 DBUG_ON(ch->remote_msgqueue == NULL);
55 }
56
57 if (!(ch->flags & XPC_C_OPENREPLY)) {
58 ch->flags |= XPC_C_OPENREPLY;
Dean Nelson7fb5e592008-07-29 22:34:10 -070059 xpc_send_chctl_openreply(ch, irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -070060 }
61
Dean Nelson2c2b94f2008-04-22 14:50:17 -050062 if (!(ch->flags & XPC_C_ROPENREPLY))
Dean Nelson89eb8eb2005-03-23 19:50:00 -070063 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -070064
65 DBUG_ON(ch->remote_msgqueue_pa == 0);
66
67 ch->flags = (XPC_C_CONNECTED | XPC_C_SETUP); /* clear all else */
68
69 dev_info(xpc_chan, "channel %d to partition %d connected\n",
Dean Nelson35190502008-04-22 14:48:55 -050070 ch->number, ch->partid);
Dean Nelson89eb8eb2005-03-23 19:50:00 -070071
72 spin_unlock_irqrestore(&ch->lock, *irq_flags);
Dean Nelsona460ef82006-11-22 08:25:00 -060073 xpc_create_kthreads(ch, 1, 0);
Dean Nelson89eb8eb2005-03-23 19:50:00 -070074 spin_lock_irqsave(&ch->lock, *irq_flags);
75}
76
Dean Nelson89eb8eb2005-03-23 19:50:00 -070077/*
Dean Nelson89eb8eb2005-03-23 19:50:00 -070078 * spin_lock_irqsave() is expected to be held on entry.
79 */
80static void
81xpc_process_disconnect(struct xpc_channel *ch, unsigned long *irq_flags)
82{
83 struct xpc_partition *part = &xpc_partitions[ch->partid];
Dean Nelsona607c3892005-09-01 14:01:37 -050084 u32 channel_was_connected = (ch->flags & XPC_C_WASCONNECTED);
Dean Nelson89eb8eb2005-03-23 19:50:00 -070085
Dean Nelson89eb8eb2005-03-23 19:50:00 -070086 DBUG_ON(!spin_is_locked(&ch->lock));
87
Dean Nelson2c2b94f2008-04-22 14:50:17 -050088 if (!(ch->flags & XPC_C_DISCONNECTING))
Dean Nelson89eb8eb2005-03-23 19:50:00 -070089 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -070090
91 DBUG_ON(!(ch->flags & XPC_C_CLOSEREQUEST));
92
93 /* make sure all activity has settled down first */
94
Dean Nelsona460ef82006-11-22 08:25:00 -060095 if (atomic_read(&ch->kthreads_assigned) > 0 ||
Dean Nelson35190502008-04-22 14:48:55 -050096 atomic_read(&ch->references) > 0) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -070097 return;
98 }
Dean Nelsona460ef82006-11-22 08:25:00 -060099 DBUG_ON((ch->flags & XPC_C_CONNECTEDCALLOUT_MADE) &&
Dean Nelson35190502008-04-22 14:48:55 -0500100 !(ch->flags & XPC_C_DISCONNECTINGCALLOUT_MADE));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700101
Dean Nelsona607c3892005-09-01 14:01:37 -0500102 if (part->act_state == XPC_P_DEACTIVATING) {
103 /* can't proceed until the other side disengages from us */
Dean Nelsona47d5da2008-07-29 22:34:09 -0700104 if (xpc_partition_engaged(ch->partid))
Dean Nelsona607c3892005-09-01 14:01:37 -0500105 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700106
Dean Nelsona607c3892005-09-01 14:01:37 -0500107 } else {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700108
109 /* as long as the other side is up do the full protocol */
110
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500111 if (!(ch->flags & XPC_C_RCLOSEREQUEST))
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700112 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700113
114 if (!(ch->flags & XPC_C_CLOSEREPLY)) {
115 ch->flags |= XPC_C_CLOSEREPLY;
Dean Nelson7fb5e592008-07-29 22:34:10 -0700116 xpc_send_chctl_closereply(ch, irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700117 }
118
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500119 if (!(ch->flags & XPC_C_RCLOSEREPLY))
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700120 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700121 }
122
Dean Nelsona607c3892005-09-01 14:01:37 -0500123 /* wake those waiting for notify completion */
124 if (atomic_read(&ch->n_to_notify) > 0) {
Dean Nelsonea57f802008-07-29 22:34:14 -0700125 /* we do callout while holding ch->lock, callout can't block */
Dean Nelsona47d5da2008-07-29 22:34:09 -0700126 xpc_notify_senders_of_disconnect(ch);
Dean Nelsona607c3892005-09-01 14:01:37 -0500127 }
128
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700129 /* both sides are disconnected now */
130
Dean Nelson4c2cd962006-02-15 08:02:21 -0600131 if (ch->flags & XPC_C_DISCONNECTINGCALLOUT_MADE) {
Dean Nelson246c7e32005-12-22 14:32:56 -0600132 spin_unlock_irqrestore(&ch->lock, *irq_flags);
Dean Nelson65c17b82008-05-12 14:02:02 -0700133 xpc_disconnect_callout(ch, xpDisconnected);
Dean Nelson246c7e32005-12-22 14:32:56 -0600134 spin_lock_irqsave(&ch->lock, *irq_flags);
135 }
136
Dean Nelsona607c3892005-09-01 14:01:37 -0500137 /* it's now safe to free the channel's message queues */
138 xpc_free_msgqueues(ch);
139
Dean Nelson185c3a12008-07-29 22:34:11 -0700140 /*
141 * Mark the channel disconnected and clear all other flags, including
142 * XPC_C_SETUP (because of call to xpc_free_msgqueues()) but not
143 * including XPC_C_WDISCONNECT (if it was set).
144 */
Dean Nelsona607c3892005-09-01 14:01:37 -0500145 ch->flags = (XPC_C_DISCONNECTED | (ch->flags & XPC_C_WDISCONNECT));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700146
147 atomic_dec(&part->nchannels_active);
148
Dean Nelsona607c3892005-09-01 14:01:37 -0500149 if (channel_was_connected) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700150 dev_info(xpc_chan, "channel %d to partition %d disconnected, "
Dean Nelson35190502008-04-22 14:48:55 -0500151 "reason=%d\n", ch->number, ch->partid, ch->reason);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700152 }
Dean Nelsona607c3892005-09-01 14:01:37 -0500153
Dean Nelsona607c3892005-09-01 14:01:37 -0500154 if (ch->flags & XPC_C_WDISCONNECT) {
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500155 /* we won't lose the CPU since we're holding ch->lock */
156 complete(&ch->wdisconnect_wait);
Dean Nelson7fb5e592008-07-29 22:34:10 -0700157 } else if (ch->delayed_chctl_flags) {
Dean Nelsone54af722005-10-25 14:07:43 -0500158 if (part->act_state != XPC_P_DEACTIVATING) {
Dean Nelson7fb5e592008-07-29 22:34:10 -0700159 /* time to take action on any delayed chctl flags */
160 spin_lock(&part->chctl_lock);
161 part->chctl.flags[ch->number] |=
162 ch->delayed_chctl_flags;
163 spin_unlock(&part->chctl_lock);
Dean Nelsone54af722005-10-25 14:07:43 -0500164 }
Dean Nelson7fb5e592008-07-29 22:34:10 -0700165 ch->delayed_chctl_flags = 0;
Dean Nelsona607c3892005-09-01 14:01:37 -0500166 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700167}
168
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700169/*
170 * Process a change in the channel's remote connection state.
171 */
172static void
Dean Nelson7fb5e592008-07-29 22:34:10 -0700173xpc_process_openclose_chctl_flags(struct xpc_partition *part, int ch_number,
174 u8 chctl_flags)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700175{
176 unsigned long irq_flags;
177 struct xpc_openclose_args *args =
Dean Nelson35190502008-04-22 14:48:55 -0500178 &part->remote_openclose_args[ch_number];
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700179 struct xpc_channel *ch = &part->channels[ch_number];
Dean Nelson65c17b82008-05-12 14:02:02 -0700180 enum xp_retval reason;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700181
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700182 spin_lock_irqsave(&ch->lock, irq_flags);
183
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500184again:
Dean Nelsone54af722005-10-25 14:07:43 -0500185
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500186 if ((ch->flags & XPC_C_DISCONNECTED) &&
187 (ch->flags & XPC_C_WDISCONNECT)) {
Dean Nelsone54af722005-10-25 14:07:43 -0500188 /*
Dean Nelson7fb5e592008-07-29 22:34:10 -0700189 * Delay processing chctl flags until thread waiting disconnect
Dean Nelsone54af722005-10-25 14:07:43 -0500190 * has had a chance to see that the channel is disconnected.
191 */
Dean Nelson7fb5e592008-07-29 22:34:10 -0700192 ch->delayed_chctl_flags |= chctl_flags;
Dean Nelsone54af722005-10-25 14:07:43 -0500193 spin_unlock_irqrestore(&ch->lock, irq_flags);
194 return;
195 }
196
Dean Nelson7fb5e592008-07-29 22:34:10 -0700197 if (chctl_flags & XPC_CHCTL_CLOSEREQUEST) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700198
Dean Nelson7fb5e592008-07-29 22:34:10 -0700199 dev_dbg(xpc_chan, "XPC_CHCTL_CLOSEREQUEST (reason=%d) received "
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700200 "from partid=%d, channel=%d\n", args->reason,
201 ch->partid, ch->number);
202
203 /*
204 * If RCLOSEREQUEST is set, we're probably waiting for
205 * RCLOSEREPLY. We should find it and a ROPENREQUEST packed
Dean Nelson7fb5e592008-07-29 22:34:10 -0700206 * with this RCLOSEREQUEST in the chctl_flags.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700207 */
208
209 if (ch->flags & XPC_C_RCLOSEREQUEST) {
210 DBUG_ON(!(ch->flags & XPC_C_DISCONNECTING));
211 DBUG_ON(!(ch->flags & XPC_C_CLOSEREQUEST));
212 DBUG_ON(!(ch->flags & XPC_C_CLOSEREPLY));
213 DBUG_ON(ch->flags & XPC_C_RCLOSEREPLY);
214
Dean Nelson7fb5e592008-07-29 22:34:10 -0700215 DBUG_ON(!(chctl_flags & XPC_CHCTL_CLOSEREPLY));
216 chctl_flags &= ~XPC_CHCTL_CLOSEREPLY;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700217 ch->flags |= XPC_C_RCLOSEREPLY;
218
219 /* both sides have finished disconnecting */
220 xpc_process_disconnect(ch, &irq_flags);
Dean Nelsone54af722005-10-25 14:07:43 -0500221 DBUG_ON(!(ch->flags & XPC_C_DISCONNECTED));
222 goto again;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700223 }
224
225 if (ch->flags & XPC_C_DISCONNECTED) {
Dean Nelson7fb5e592008-07-29 22:34:10 -0700226 if (!(chctl_flags & XPC_CHCTL_OPENREQUEST)) {
227 if (part->chctl.flags[ch_number] &
228 XPC_CHCTL_OPENREQUEST) {
Dean Nelsone54af722005-10-25 14:07:43 -0500229
Dean Nelson7fb5e592008-07-29 22:34:10 -0700230 DBUG_ON(ch->delayed_chctl_flags != 0);
231 spin_lock(&part->chctl_lock);
232 part->chctl.flags[ch_number] |=
233 XPC_CHCTL_CLOSEREQUEST;
234 spin_unlock(&part->chctl_lock);
Dean Nelsone54af722005-10-25 14:07:43 -0500235 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700236 spin_unlock_irqrestore(&ch->lock, irq_flags);
237 return;
238 }
239
240 XPC_SET_REASON(ch, 0, 0);
241 ch->flags &= ~XPC_C_DISCONNECTED;
242
243 atomic_inc(&part->nchannels_active);
244 ch->flags |= (XPC_C_CONNECTING | XPC_C_ROPENREQUEST);
245 }
246
Dean Nelson7fb5e592008-07-29 22:34:10 -0700247 chctl_flags &= ~(XPC_CHCTL_OPENREQUEST | XPC_CHCTL_OPENREPLY);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700248
249 /*
250 * The meaningful CLOSEREQUEST connection state fields are:
251 * reason = reason connection is to be closed
252 */
253
254 ch->flags |= XPC_C_RCLOSEREQUEST;
255
256 if (!(ch->flags & XPC_C_DISCONNECTING)) {
257 reason = args->reason;
Dean Nelson65c17b82008-05-12 14:02:02 -0700258 if (reason <= xpSuccess || reason > xpUnknownReason)
259 reason = xpUnknownReason;
260 else if (reason == xpUnregistering)
261 reason = xpOtherUnregistering;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700262
263 XPC_DISCONNECT_CHANNEL(ch, reason, &irq_flags);
Dean Nelsone54af722005-10-25 14:07:43 -0500264
Dean Nelson7fb5e592008-07-29 22:34:10 -0700265 DBUG_ON(chctl_flags & XPC_CHCTL_CLOSEREPLY);
Dean Nelsone54af722005-10-25 14:07:43 -0500266 spin_unlock_irqrestore(&ch->lock, irq_flags);
267 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700268 }
Dean Nelsone54af722005-10-25 14:07:43 -0500269
270 xpc_process_disconnect(ch, &irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700271 }
272
Dean Nelson7fb5e592008-07-29 22:34:10 -0700273 if (chctl_flags & XPC_CHCTL_CLOSEREPLY) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700274
Dean Nelson7fb5e592008-07-29 22:34:10 -0700275 dev_dbg(xpc_chan, "XPC_CHCTL_CLOSEREPLY received from partid="
276 "%d, channel=%d\n", ch->partid, ch->number);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700277
278 if (ch->flags & XPC_C_DISCONNECTED) {
279 DBUG_ON(part->act_state != XPC_P_DEACTIVATING);
280 spin_unlock_irqrestore(&ch->lock, irq_flags);
281 return;
282 }
283
284 DBUG_ON(!(ch->flags & XPC_C_CLOSEREQUEST));
Dean Nelsone54af722005-10-25 14:07:43 -0500285
286 if (!(ch->flags & XPC_C_RCLOSEREQUEST)) {
Dean Nelson7fb5e592008-07-29 22:34:10 -0700287 if (part->chctl.flags[ch_number] &
288 XPC_CHCTL_CLOSEREQUEST) {
Dean Nelsone54af722005-10-25 14:07:43 -0500289
Dean Nelson7fb5e592008-07-29 22:34:10 -0700290 DBUG_ON(ch->delayed_chctl_flags != 0);
291 spin_lock(&part->chctl_lock);
292 part->chctl.flags[ch_number] |=
293 XPC_CHCTL_CLOSEREPLY;
294 spin_unlock(&part->chctl_lock);
Dean Nelsone54af722005-10-25 14:07:43 -0500295 }
296 spin_unlock_irqrestore(&ch->lock, irq_flags);
297 return;
298 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700299
300 ch->flags |= XPC_C_RCLOSEREPLY;
301
302 if (ch->flags & XPC_C_CLOSEREPLY) {
303 /* both sides have finished disconnecting */
304 xpc_process_disconnect(ch, &irq_flags);
305 }
306 }
307
Dean Nelson7fb5e592008-07-29 22:34:10 -0700308 if (chctl_flags & XPC_CHCTL_OPENREQUEST) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700309
Dean Nelson7fb5e592008-07-29 22:34:10 -0700310 dev_dbg(xpc_chan, "XPC_CHCTL_OPENREQUEST (msg_size=%d, "
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700311 "local_nentries=%d) received from partid=%d, "
312 "channel=%d\n", args->msg_size, args->local_nentries,
313 ch->partid, ch->number);
314
Dean Nelsone54af722005-10-25 14:07:43 -0500315 if (part->act_state == XPC_P_DEACTIVATING ||
Dean Nelson35190502008-04-22 14:48:55 -0500316 (ch->flags & XPC_C_ROPENREQUEST)) {
Dean Nelsone54af722005-10-25 14:07:43 -0500317 spin_unlock_irqrestore(&ch->lock, irq_flags);
318 return;
319 }
320
321 if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_WDISCONNECT)) {
Dean Nelson7fb5e592008-07-29 22:34:10 -0700322 ch->delayed_chctl_flags |= XPC_CHCTL_OPENREQUEST;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700323 spin_unlock_irqrestore(&ch->lock, irq_flags);
324 return;
325 }
326 DBUG_ON(!(ch->flags & (XPC_C_DISCONNECTED |
Dean Nelson35190502008-04-22 14:48:55 -0500327 XPC_C_OPENREQUEST)));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700328 DBUG_ON(ch->flags & (XPC_C_ROPENREQUEST | XPC_C_ROPENREPLY |
Dean Nelson35190502008-04-22 14:48:55 -0500329 XPC_C_OPENREPLY | XPC_C_CONNECTED));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700330
331 /*
332 * The meaningful OPENREQUEST connection state fields are:
333 * msg_size = size of channel's messages in bytes
334 * local_nentries = remote partition's local_nentries
335 */
Dean Nelsone54af722005-10-25 14:07:43 -0500336 if (args->msg_size == 0 || args->local_nentries == 0) {
337 /* assume OPENREQUEST was delayed by mistake */
338 spin_unlock_irqrestore(&ch->lock, irq_flags);
339 return;
340 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700341
342 ch->flags |= (XPC_C_ROPENREQUEST | XPC_C_CONNECTING);
343 ch->remote_nentries = args->local_nentries;
344
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700345 if (ch->flags & XPC_C_OPENREQUEST) {
346 if (args->msg_size != ch->msg_size) {
Dean Nelson65c17b82008-05-12 14:02:02 -0700347 XPC_DISCONNECT_CHANNEL(ch, xpUnequalMsgSizes,
Dean Nelson35190502008-04-22 14:48:55 -0500348 &irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700349 spin_unlock_irqrestore(&ch->lock, irq_flags);
350 return;
351 }
352 } else {
353 ch->msg_size = args->msg_size;
354
355 XPC_SET_REASON(ch, 0, 0);
356 ch->flags &= ~XPC_C_DISCONNECTED;
357
358 atomic_inc(&part->nchannels_active);
359 }
360
361 xpc_process_connect(ch, &irq_flags);
362 }
363
Dean Nelson7fb5e592008-07-29 22:34:10 -0700364 if (chctl_flags & XPC_CHCTL_OPENREPLY) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700365
Dean Nelson7fb5e592008-07-29 22:34:10 -0700366 dev_dbg(xpc_chan, "XPC_CHCTL_OPENREPLY (local_msgqueue_pa="
367 "0x%lx, local_nentries=%d, remote_nentries=%d) "
368 "received from partid=%d, channel=%d\n",
Dean Nelsona812dcc2008-07-29 22:34:16 -0700369 args->local_msgqueue_pa, args->local_nentries,
370 args->remote_nentries, ch->partid, ch->number);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700371
372 if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_DISCONNECTED)) {
373 spin_unlock_irqrestore(&ch->lock, irq_flags);
374 return;
375 }
Dean Nelsone54af722005-10-25 14:07:43 -0500376 if (!(ch->flags & XPC_C_OPENREQUEST)) {
Dean Nelson65c17b82008-05-12 14:02:02 -0700377 XPC_DISCONNECT_CHANNEL(ch, xpOpenCloseError,
Dean Nelson35190502008-04-22 14:48:55 -0500378 &irq_flags);
Dean Nelsone54af722005-10-25 14:07:43 -0500379 spin_unlock_irqrestore(&ch->lock, irq_flags);
380 return;
381 }
382
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700383 DBUG_ON(!(ch->flags & XPC_C_ROPENREQUEST));
384 DBUG_ON(ch->flags & XPC_C_CONNECTED);
385
386 /*
387 * The meaningful OPENREPLY connection state fields are:
388 * local_msgqueue_pa = physical address of remote
Dean Nelson35190502008-04-22 14:48:55 -0500389 * partition's local_msgqueue
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700390 * local_nentries = remote partition's local_nentries
391 * remote_nentries = remote partition's remote_nentries
392 */
393 DBUG_ON(args->local_msgqueue_pa == 0);
394 DBUG_ON(args->local_nentries == 0);
395 DBUG_ON(args->remote_nentries == 0);
396
397 ch->flags |= XPC_C_ROPENREPLY;
398 ch->remote_msgqueue_pa = args->local_msgqueue_pa;
399
400 if (args->local_nentries < ch->remote_nentries) {
Dean Nelson7fb5e592008-07-29 22:34:10 -0700401 dev_dbg(xpc_chan, "XPC_CHCTL_OPENREPLY: new "
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700402 "remote_nentries=%d, old remote_nentries=%d, "
403 "partid=%d, channel=%d\n",
404 args->local_nentries, ch->remote_nentries,
405 ch->partid, ch->number);
406
407 ch->remote_nentries = args->local_nentries;
408 }
409 if (args->remote_nentries < ch->local_nentries) {
Dean Nelson7fb5e592008-07-29 22:34:10 -0700410 dev_dbg(xpc_chan, "XPC_CHCTL_OPENREPLY: new "
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700411 "local_nentries=%d, old local_nentries=%d, "
412 "partid=%d, channel=%d\n",
413 args->remote_nentries, ch->local_nentries,
414 ch->partid, ch->number);
415
416 ch->local_nentries = args->remote_nentries;
417 }
418
419 xpc_process_connect(ch, &irq_flags);
420 }
421
422 spin_unlock_irqrestore(&ch->lock, irq_flags);
423}
424
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700425/*
426 * Attempt to establish a channel connection to a remote partition.
427 */
Dean Nelson65c17b82008-05-12 14:02:02 -0700428static enum xp_retval
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700429xpc_connect_channel(struct xpc_channel *ch)
430{
431 unsigned long irq_flags;
432 struct xpc_registration *registration = &xpc_registrations[ch->number];
433
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500434 if (mutex_trylock(&registration->mutex) == 0)
Dean Nelson65c17b82008-05-12 14:02:02 -0700435 return xpRetry;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700436
437 if (!XPC_CHANNEL_REGISTERED(ch->number)) {
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500438 mutex_unlock(&registration->mutex);
Dean Nelson65c17b82008-05-12 14:02:02 -0700439 return xpUnregistered;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700440 }
441
442 spin_lock_irqsave(&ch->lock, irq_flags);
443
444 DBUG_ON(ch->flags & XPC_C_CONNECTED);
445 DBUG_ON(ch->flags & XPC_C_OPENREQUEST);
446
447 if (ch->flags & XPC_C_DISCONNECTING) {
448 spin_unlock_irqrestore(&ch->lock, irq_flags);
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500449 mutex_unlock(&registration->mutex);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700450 return ch->reason;
451 }
452
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700453 /* add info from the channel connect registration to the channel */
454
455 ch->kthreads_assigned_limit = registration->assigned_limit;
456 ch->kthreads_idle_limit = registration->idle_limit;
457 DBUG_ON(atomic_read(&ch->kthreads_assigned) != 0);
458 DBUG_ON(atomic_read(&ch->kthreads_idle) != 0);
459 DBUG_ON(atomic_read(&ch->kthreads_active) != 0);
460
461 ch->func = registration->func;
462 DBUG_ON(registration->func == NULL);
463 ch->key = registration->key;
464
465 ch->local_nentries = registration->nentries;
466
467 if (ch->flags & XPC_C_ROPENREQUEST) {
468 if (registration->msg_size != ch->msg_size) {
469 /* the local and remote sides aren't the same */
470
471 /*
472 * Because XPC_DISCONNECT_CHANNEL() can block we're
473 * forced to up the registration sema before we unlock
474 * the channel lock. But that's okay here because we're
475 * done with the part that required the registration
476 * sema. XPC_DISCONNECT_CHANNEL() requires that the
477 * channel lock be locked and will unlock and relock
478 * the channel lock as needed.
479 */
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500480 mutex_unlock(&registration->mutex);
Dean Nelson65c17b82008-05-12 14:02:02 -0700481 XPC_DISCONNECT_CHANNEL(ch, xpUnequalMsgSizes,
Dean Nelson35190502008-04-22 14:48:55 -0500482 &irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700483 spin_unlock_irqrestore(&ch->lock, irq_flags);
Dean Nelson65c17b82008-05-12 14:02:02 -0700484 return xpUnequalMsgSizes;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700485 }
486 } else {
487 ch->msg_size = registration->msg_size;
488
489 XPC_SET_REASON(ch, 0, 0);
490 ch->flags &= ~XPC_C_DISCONNECTED;
491
492 atomic_inc(&xpc_partitions[ch->partid].nchannels_active);
493 }
494
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500495 mutex_unlock(&registration->mutex);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700496
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700497 /* initiate the connection */
498
499 ch->flags |= (XPC_C_OPENREQUEST | XPC_C_CONNECTING);
Dean Nelson7fb5e592008-07-29 22:34:10 -0700500 xpc_send_chctl_openrequest(ch, &irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700501
502 xpc_process_connect(ch, &irq_flags);
503
504 spin_unlock_irqrestore(&ch->lock, irq_flags);
505
Dean Nelson65c17b82008-05-12 14:02:02 -0700506 return xpSuccess;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700507}
508
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700509void
Dean Nelson7fb5e592008-07-29 22:34:10 -0700510xpc_process_sent_chctl_flags(struct xpc_partition *part)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700511{
512 unsigned long irq_flags;
Dean Nelson7fb5e592008-07-29 22:34:10 -0700513 union xpc_channel_ctl_flags chctl;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700514 struct xpc_channel *ch;
515 int ch_number;
Dean Nelsona607c3892005-09-01 14:01:37 -0500516 u32 ch_flags;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700517
Dean Nelson7fb5e592008-07-29 22:34:10 -0700518 chctl.all_flags = xpc_get_chctl_all_flags(part);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700519
520 /*
521 * Initiate channel connections for registered channels.
522 *
523 * For each connected channel that has pending messages activate idle
524 * kthreads and/or create new kthreads as needed.
525 */
526
527 for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
528 ch = &part->channels[ch_number];
529
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700530 /*
Dean Nelson7fb5e592008-07-29 22:34:10 -0700531 * Process any open or close related chctl flags, and then deal
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700532 * with connecting or disconnecting the channel as required.
533 */
534
Dean Nelson7fb5e592008-07-29 22:34:10 -0700535 if (chctl.flags[ch_number] & XPC_OPENCLOSE_CHCTL_FLAGS) {
536 xpc_process_openclose_chctl_flags(part, ch_number,
537 chctl.flags[ch_number]);
538 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700539
Dean Nelsona607c3892005-09-01 14:01:37 -0500540 ch_flags = ch->flags; /* need an atomic snapshot of flags */
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700541
Dean Nelsona607c3892005-09-01 14:01:37 -0500542 if (ch_flags & XPC_C_DISCONNECTING) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700543 spin_lock_irqsave(&ch->lock, irq_flags);
544 xpc_process_disconnect(ch, &irq_flags);
545 spin_unlock_irqrestore(&ch->lock, irq_flags);
546 continue;
547 }
548
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500549 if (part->act_state == XPC_P_DEACTIVATING)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700550 continue;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700551
Dean Nelsona607c3892005-09-01 14:01:37 -0500552 if (!(ch_flags & XPC_C_CONNECTED)) {
553 if (!(ch_flags & XPC_C_OPENREQUEST)) {
554 DBUG_ON(ch_flags & XPC_C_SETUP);
Dean Nelson35190502008-04-22 14:48:55 -0500555 (void)xpc_connect_channel(ch);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700556 } else {
557 spin_lock_irqsave(&ch->lock, irq_flags);
558 xpc_process_connect(ch, &irq_flags);
559 spin_unlock_irqrestore(&ch->lock, irq_flags);
560 }
561 continue;
562 }
563
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700564 /*
Dean Nelson7fb5e592008-07-29 22:34:10 -0700565 * Process any message related chctl flags, this may involve
566 * the activation of kthreads to deliver any pending messages
567 * sent from the other partition.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700568 */
569
Dean Nelson7fb5e592008-07-29 22:34:10 -0700570 if (chctl.flags[ch_number] & XPC_MSG_CHCTL_FLAGS)
571 xpc_process_msg_chctl_flags(part, ch_number);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700572 }
573}
574
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700575/*
Dean Nelsona607c3892005-09-01 14:01:37 -0500576 * XPC's heartbeat code calls this function to inform XPC that a partition is
577 * going down. XPC responds by tearing down the XPartition Communication
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700578 * infrastructure used for the just downed partition.
579 *
580 * XPC's heartbeat code will never call this function and xpc_partition_up()
581 * at the same time. Nor will it ever make multiple calls to either function
582 * at the same time.
583 */
584void
Dean Nelson65c17b82008-05-12 14:02:02 -0700585xpc_partition_going_down(struct xpc_partition *part, enum xp_retval reason)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700586{
587 unsigned long irq_flags;
588 int ch_number;
589 struct xpc_channel *ch;
590
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700591 dev_dbg(xpc_chan, "deactivating partition %d, reason=%d\n",
592 XPC_PARTID(part), reason);
593
594 if (!xpc_part_ref(part)) {
595 /* infrastructure for this partition isn't currently set up */
596 return;
597 }
598
Dean Nelsona607c3892005-09-01 14:01:37 -0500599 /* disconnect channels associated with the partition going down */
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700600
601 for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
602 ch = &part->channels[ch_number];
603
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700604 xpc_msgqueue_ref(ch);
605 spin_lock_irqsave(&ch->lock, irq_flags);
606
607 XPC_DISCONNECT_CHANNEL(ch, reason, &irq_flags);
608
609 spin_unlock_irqrestore(&ch->lock, irq_flags);
610 xpc_msgqueue_deref(ch);
611 }
612
613 xpc_wakeup_channel_mgr(part);
614
615 xpc_part_deref(part);
616}
617
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700618/*
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700619 * Called by XP at the time of channel connection registration to cause
620 * XPC to establish connections to all currently active partitions.
621 */
622void
623xpc_initiate_connect(int ch_number)
624{
Dean Nelson64d032b2008-05-12 14:02:03 -0700625 short partid;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700626 struct xpc_partition *part;
627 struct xpc_channel *ch;
628
Dean Nelsonbc63d382008-07-29 22:34:04 -0700629 DBUG_ON(ch_number < 0 || ch_number >= XPC_MAX_NCHANNELS);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700630
Dean Nelsonbc63d382008-07-29 22:34:04 -0700631 for (partid = 0; partid < xp_max_npartitions; partid++) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700632 part = &xpc_partitions[partid];
633
634 if (xpc_part_ref(part)) {
635 ch = &part->channels[ch_number];
636
Dean Nelsone54af722005-10-25 14:07:43 -0500637 /*
638 * Initiate the establishment of a connection on the
639 * newly registered channel to the remote partition.
640 */
641 xpc_wakeup_channel_mgr(part);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700642 xpc_part_deref(part);
643 }
644 }
645}
646
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700647void
648xpc_connected_callout(struct xpc_channel *ch)
649{
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700650 /* let the registerer know that a connection has been established */
651
652 if (ch->func != NULL) {
Dean Nelson65c17b82008-05-12 14:02:02 -0700653 dev_dbg(xpc_chan, "ch->func() called, reason=xpConnected, "
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700654 "partid=%d, channel=%d\n", ch->partid, ch->number);
655
Dean Nelson65c17b82008-05-12 14:02:02 -0700656 ch->func(xpConnected, ch->partid, ch->number,
Dean Nelson35190502008-04-22 14:48:55 -0500657 (void *)(u64)ch->local_nentries, ch->key);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700658
Dean Nelson65c17b82008-05-12 14:02:02 -0700659 dev_dbg(xpc_chan, "ch->func() returned, reason=xpConnected, "
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700660 "partid=%d, channel=%d\n", ch->partid, ch->number);
661 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700662}
663
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700664/*
665 * Called by XP at the time of channel connection unregistration to cause
666 * XPC to teardown all current connections for the specified channel.
667 *
668 * Before returning xpc_initiate_disconnect() will wait until all connections
669 * on the specified channel have been closed/torndown. So the caller can be
670 * assured that they will not be receiving any more callouts from XPC to the
671 * function they registered via xpc_connect().
672 *
673 * Arguments:
674 *
675 * ch_number - channel # to unregister.
676 */
677void
678xpc_initiate_disconnect(int ch_number)
679{
680 unsigned long irq_flags;
Dean Nelson64d032b2008-05-12 14:02:03 -0700681 short partid;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700682 struct xpc_partition *part;
683 struct xpc_channel *ch;
684
Dean Nelsonbc63d382008-07-29 22:34:04 -0700685 DBUG_ON(ch_number < 0 || ch_number >= XPC_MAX_NCHANNELS);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700686
687 /* initiate the channel disconnect for every active partition */
Dean Nelsonbc63d382008-07-29 22:34:04 -0700688 for (partid = 0; partid < xp_max_npartitions; partid++) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700689 part = &xpc_partitions[partid];
690
691 if (xpc_part_ref(part)) {
692 ch = &part->channels[ch_number];
693 xpc_msgqueue_ref(ch);
694
695 spin_lock_irqsave(&ch->lock, irq_flags);
696
Dean Nelsona607c3892005-09-01 14:01:37 -0500697 if (!(ch->flags & XPC_C_DISCONNECTED)) {
698 ch->flags |= XPC_C_WDISCONNECT;
699
Dean Nelson65c17b82008-05-12 14:02:02 -0700700 XPC_DISCONNECT_CHANNEL(ch, xpUnregistering,
Dean Nelson35190502008-04-22 14:48:55 -0500701 &irq_flags);
Dean Nelsona607c3892005-09-01 14:01:37 -0500702 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700703
704 spin_unlock_irqrestore(&ch->lock, irq_flags);
705
706 xpc_msgqueue_deref(ch);
707 xpc_part_deref(part);
708 }
709 }
710
711 xpc_disconnect_wait(ch_number);
712}
713
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700714/*
715 * To disconnect a channel, and reflect it back to all who may be waiting.
716 *
Dean Nelsona607c3892005-09-01 14:01:37 -0500717 * An OPEN is not allowed until XPC_C_DISCONNECTING is cleared by
718 * xpc_process_disconnect(), and if set, XPC_C_WDISCONNECT is cleared by
719 * xpc_disconnect_wait().
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700720 *
721 * THE CHANNEL IS TO BE LOCKED BY THE CALLER AND WILL REMAIN LOCKED UPON RETURN.
722 */
723void
724xpc_disconnect_channel(const int line, struct xpc_channel *ch,
Dean Nelson65c17b82008-05-12 14:02:02 -0700725 enum xp_retval reason, unsigned long *irq_flags)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700726{
Dean Nelsona607c3892005-09-01 14:01:37 -0500727 u32 channel_was_connected = (ch->flags & XPC_C_CONNECTED);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700728
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700729 DBUG_ON(!spin_is_locked(&ch->lock));
730
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500731 if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_DISCONNECTED))
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700732 return;
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500733
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700734 DBUG_ON(!(ch->flags & (XPC_C_CONNECTING | XPC_C_CONNECTED)));
735
736 dev_dbg(xpc_chan, "reason=%d, line=%d, partid=%d, channel=%d\n",
737 reason, line, ch->partid, ch->number);
738
739 XPC_SET_REASON(ch, reason, line);
740
Dean Nelsona607c3892005-09-01 14:01:37 -0500741 ch->flags |= (XPC_C_CLOSEREQUEST | XPC_C_DISCONNECTING);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700742 /* some of these may not have been set */
743 ch->flags &= ~(XPC_C_OPENREQUEST | XPC_C_OPENREPLY |
Dean Nelson35190502008-04-22 14:48:55 -0500744 XPC_C_ROPENREQUEST | XPC_C_ROPENREPLY |
745 XPC_C_CONNECTING | XPC_C_CONNECTED);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700746
Dean Nelson7fb5e592008-07-29 22:34:10 -0700747 xpc_send_chctl_closerequest(ch, irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700748
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500749 if (channel_was_connected)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700750 ch->flags |= XPC_C_WASCONNECTED;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700751
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700752 spin_unlock_irqrestore(&ch->lock, *irq_flags);
753
Dean Nelsona607c3892005-09-01 14:01:37 -0500754 /* wake all idle kthreads so they can exit */
755 if (atomic_read(&ch->kthreads_idle) > 0) {
756 wake_up_all(&ch->idle_wq);
Dean Nelsona460ef82006-11-22 08:25:00 -0600757
758 } else if ((ch->flags & XPC_C_CONNECTEDCALLOUT_MADE) &&
Dean Nelson35190502008-04-22 14:48:55 -0500759 !(ch->flags & XPC_C_DISCONNECTINGCALLOUT)) {
Dean Nelson65c17b82008-05-12 14:02:02 -0700760 /* start a kthread that will do the xpDisconnecting callout */
Dean Nelsona460ef82006-11-22 08:25:00 -0600761 xpc_create_kthreads(ch, 1, 1);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700762 }
763
Dean Nelsona607c3892005-09-01 14:01:37 -0500764 /* wake those waiting to allocate an entry from the local msg queue */
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500765 if (atomic_read(&ch->n_on_msg_allocate_wq) > 0)
Dean Nelsona607c3892005-09-01 14:01:37 -0500766 wake_up(&ch->msg_allocate_wq);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700767
768 spin_lock_irqsave(&ch->lock, *irq_flags);
769}
770
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700771void
Dean Nelson65c17b82008-05-12 14:02:02 -0700772xpc_disconnect_callout(struct xpc_channel *ch, enum xp_retval reason)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700773{
774 /*
Dean Nelsona607c3892005-09-01 14:01:37 -0500775 * Let the channel's registerer know that the channel is being
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700776 * disconnected. We don't want to do this if the registerer was never
Dean Nelsona607c3892005-09-01 14:01:37 -0500777 * informed of a connection being made.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700778 */
779
780 if (ch->func != NULL) {
Dean Nelson246c7e32005-12-22 14:32:56 -0600781 dev_dbg(xpc_chan, "ch->func() called, reason=%d, partid=%d, "
782 "channel=%d\n", reason, ch->partid, ch->number);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700783
Dean Nelson246c7e32005-12-22 14:32:56 -0600784 ch->func(reason, ch->partid, ch->number, NULL, ch->key);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700785
Dean Nelson246c7e32005-12-22 14:32:56 -0600786 dev_dbg(xpc_chan, "ch->func() returned, reason=%d, partid=%d, "
787 "channel=%d\n", reason, ch->partid, ch->number);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700788 }
789}
790
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700791/*
792 * Wait for a message entry to become available for the specified channel,
793 * but don't wait any longer than 1 jiffy.
794 */
Dean Nelson33ba3c72008-07-29 22:34:07 -0700795enum xp_retval
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700796xpc_allocate_msg_wait(struct xpc_channel *ch)
797{
Dean Nelson65c17b82008-05-12 14:02:02 -0700798 enum xp_retval ret;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700799
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700800 if (ch->flags & XPC_C_DISCONNECTING) {
Dean Nelson65c17b82008-05-12 14:02:02 -0700801 DBUG_ON(ch->reason == xpInterrupted);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700802 return ch->reason;
803 }
804
805 atomic_inc(&ch->n_on_msg_allocate_wq);
806 ret = interruptible_sleep_on_timeout(&ch->msg_allocate_wq, 1);
807 atomic_dec(&ch->n_on_msg_allocate_wq);
808
809 if (ch->flags & XPC_C_DISCONNECTING) {
810 ret = ch->reason;
Dean Nelson65c17b82008-05-12 14:02:02 -0700811 DBUG_ON(ch->reason == xpInterrupted);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700812 } else if (ret == 0) {
Dean Nelson65c17b82008-05-12 14:02:02 -0700813 ret = xpTimeout;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700814 } else {
Dean Nelson65c17b82008-05-12 14:02:02 -0700815 ret = xpInterrupted;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700816 }
817
818 return ret;
819}
820
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700821/*
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700822 * Send a message that contains the user's payload on the specified channel
823 * connected to the specified partition.
824 *
825 * NOTE that this routine can sleep waiting for a message entry to become
826 * available. To not sleep, pass in the XPC_NOWAIT flag.
827 *
828 * Once sent, this routine will not wait for the message to be received, nor
829 * will notification be given when it does happen.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700830 *
831 * Arguments:
832 *
833 * partid - ID of partition to which the channel is connected.
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700834 * ch_number - channel # to send message on.
835 * flags - see xp.h for valid flags.
836 * payload - pointer to the payload which is to be sent.
837 * payload_size - size of the payload in bytes.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700838 */
Dean Nelson65c17b82008-05-12 14:02:02 -0700839enum xp_retval
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700840xpc_initiate_send(short partid, int ch_number, u32 flags, void *payload,
841 u16 payload_size)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700842{
843 struct xpc_partition *part = &xpc_partitions[partid];
Dean Nelson65c17b82008-05-12 14:02:02 -0700844 enum xp_retval ret = xpUnknownReason;
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700845
846 dev_dbg(xpc_chan, "payload=0x%p, partid=%d, channel=%d\n", payload,
847 partid, ch_number);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700848
Dean Nelsonbc63d382008-07-29 22:34:04 -0700849 DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700850 DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700851 DBUG_ON(payload == NULL);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700852
853 if (xpc_part_ref(part)) {
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700854 ret = xpc_send_msg(&part->channels[ch_number], flags, payload,
855 payload_size, 0, NULL, NULL);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700856 xpc_part_deref(part);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700857 }
858
859 return ret;
860}
861
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700862/*
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700863 * Send a message that contains the user's payload on the specified channel
864 * connected to the specified partition.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700865 *
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700866 * NOTE that this routine can sleep waiting for a message entry to become
867 * available. To not sleep, pass in the XPC_NOWAIT flag.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700868 *
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700869 * This routine will not wait for the message to be sent or received.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700870 *
871 * Once the remote end of the channel has received the message, the function
872 * passed as an argument to xpc_initiate_send_notify() will be called. This
873 * allows the sender to free up or re-use any buffers referenced by the
874 * message, but does NOT mean the message has been processed at the remote
875 * end by a receiver.
876 *
877 * If this routine returns an error, the caller's function will NOT be called.
878 *
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700879 * Arguments:
880 *
881 * partid - ID of partition to which the channel is connected.
882 * ch_number - channel # to send message on.
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700883 * flags - see xp.h for valid flags.
884 * payload - pointer to the payload which is to be sent.
885 * payload_size - size of the payload in bytes.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700886 * func - function to call with asynchronous notification of message
887 * receipt. THIS FUNCTION MUST BE NON-BLOCKING.
888 * key - user-defined key to be passed to the function when it's called.
889 */
Dean Nelson65c17b82008-05-12 14:02:02 -0700890enum xp_retval
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700891xpc_initiate_send_notify(short partid, int ch_number, u32 flags, void *payload,
892 u16 payload_size, xpc_notify_func func, void *key)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700893{
894 struct xpc_partition *part = &xpc_partitions[partid];
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700895 enum xp_retval ret = xpUnknownReason;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700896
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700897 dev_dbg(xpc_chan, "payload=0x%p, partid=%d, channel=%d\n", payload,
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700898 partid, ch_number);
899
Dean Nelsonbc63d382008-07-29 22:34:04 -0700900 DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700901 DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700902 DBUG_ON(payload == NULL);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700903 DBUG_ON(func == NULL);
904
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700905 if (xpc_part_ref(part)) {
906 ret = xpc_send_msg(&part->channels[ch_number], flags, payload,
907 payload_size, XPC_N_CALL, func, key);
908 xpc_part_deref(part);
909 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700910 return ret;
911}
912
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700913/*
914 * Deliver a message to its intended recipient.
915 */
916void
917xpc_deliver_msg(struct xpc_channel *ch)
918{
919 struct xpc_msg *msg;
920
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500921 msg = xpc_get_deliverable_msg(ch);
922 if (msg != NULL) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700923
924 /*
925 * This ref is taken to protect the payload itself from being
926 * freed before the user is finished with it, which the user
927 * indicates by calling xpc_initiate_received().
928 */
929 xpc_msgqueue_ref(ch);
930
931 atomic_inc(&ch->kthreads_active);
932
933 if (ch->func != NULL) {
934 dev_dbg(xpc_chan, "ch->func() called, msg=0x%p, "
935 "msg_number=%ld, partid=%d, channel=%d\n",
Dean Nelson261f3b42008-07-29 22:34:16 -0700936 msg, (signed long)msg->number, ch->partid,
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700937 ch->number);
938
939 /* deliver the message to its intended recipient */
Dean Nelson65c17b82008-05-12 14:02:02 -0700940 ch->func(xpMsgReceived, ch->partid, ch->number,
Dean Nelson35190502008-04-22 14:48:55 -0500941 &msg->payload, ch->key);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700942
943 dev_dbg(xpc_chan, "ch->func() returned, msg=0x%p, "
944 "msg_number=%ld, partid=%d, channel=%d\n",
Dean Nelson261f3b42008-07-29 22:34:16 -0700945 msg, (signed long)msg->number, ch->partid,
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700946 ch->number);
947 }
948
949 atomic_dec(&ch->kthreads_active);
950 }
951}
952
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700953/*
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700954 * Acknowledge receipt of a delivered message.
955 *
956 * If a message has XPC_M_INTERRUPT set, send an interrupt to the partition
957 * that sent the message.
958 *
959 * This function, although called by users, does not call xpc_part_ref() to
960 * ensure that the partition infrastructure is in place. It relies on the
961 * fact that we called xpc_msgqueue_ref() in xpc_deliver_msg().
962 *
963 * Arguments:
964 *
965 * partid - ID of partition to which the channel is connected.
966 * ch_number - channel # message received on.
967 * payload - pointer to the payload area allocated via
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700968 * xpc_initiate_send() or xpc_initiate_send_notify().
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700969 */
970void
Dean Nelson64d032b2008-05-12 14:02:03 -0700971xpc_initiate_received(short partid, int ch_number, void *payload)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700972{
973 struct xpc_partition *part = &xpc_partitions[partid];
974 struct xpc_channel *ch;
975 struct xpc_msg *msg = XPC_MSG_ADDRESS(payload);
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 Nelson33ba3c72008-07-29 22:34:07 -0700981 xpc_received_msg(ch, msg);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700982
983 /* the call to xpc_msgqueue_ref() was done by xpc_deliver_msg() */
984 xpc_msgqueue_deref(ch);
985}