blob: 1c73423665bdbf806ea30b964f38d0984bbf69b6 [file] [log] [blame]
Dean Nelson89eb8eb2005-03-23 19:50:00 -07001/*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
Dean Nelson45d9ca42008-04-22 14:46:56 -05006 * Copyright (c) 2004-2008 Silicon Graphics, Inc. All Rights Reserved.
Dean Nelson89eb8eb2005-03-23 19:50:00 -07007 */
8
Dean Nelson89eb8eb2005-03-23 19:50:00 -07009/*
10 * Cross Partition Communication (XPC) channel support.
11 *
12 * This is the part of XPC that manages the channels and
13 * sends/receives messages across them to/from other partitions.
14 *
15 */
16
Dean Nelson89eb8eb2005-03-23 19:50:00 -070017#include <linux/kernel.h>
18#include <linux/init.h>
19#include <linux/sched.h>
20#include <linux/cache.h>
21#include <linux/interrupt.h>
Jes Sorensenf9e505a2006-01-17 12:52:21 -050022#include <linux/mutex.h>
23#include <linux/completion.h>
Dean Nelson89eb8eb2005-03-23 19:50:00 -070024#include <asm/sn/sn_sal.h>
Dean Nelson45d9ca42008-04-22 14:46:56 -050025#include "xpc.h"
Dean Nelson89eb8eb2005-03-23 19:50:00 -070026
Dean Nelson89eb8eb2005-03-23 19:50:00 -070027/*
Dean Nelson89eb8eb2005-03-23 19:50:00 -070028 * Process a connect message from a remote partition.
29 *
30 * Note: xpc_process_connect() is expecting to be called with the
31 * spin_lock_irqsave held and will leave it locked upon return.
32 */
33static void
34xpc_process_connect(struct xpc_channel *ch, unsigned long *irq_flags)
35{
Dean Nelson65c17b82008-05-12 14:02:02 -070036 enum xp_retval ret;
Dean Nelson89eb8eb2005-03-23 19:50:00 -070037
Dean Nelson89eb8eb2005-03-23 19:50:00 -070038 DBUG_ON(!spin_is_locked(&ch->lock));
39
40 if (!(ch->flags & XPC_C_OPENREQUEST) ||
Dean Nelson35190502008-04-22 14:48:55 -050041 !(ch->flags & XPC_C_ROPENREQUEST)) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -070042 /* nothing more to do for now */
43 return;
44 }
45 DBUG_ON(!(ch->flags & XPC_C_CONNECTING));
46
47 if (!(ch->flags & XPC_C_SETUP)) {
48 spin_unlock_irqrestore(&ch->lock, *irq_flags);
49 ret = xpc_allocate_msgqueues(ch);
50 spin_lock_irqsave(&ch->lock, *irq_flags);
51
Dean Nelson65c17b82008-05-12 14:02:02 -070052 if (ret != xpSuccess)
Dean Nelson89eb8eb2005-03-23 19:50:00 -070053 XPC_DISCONNECT_CHANNEL(ch, ret, irq_flags);
Dean Nelson2c2b94f2008-04-22 14:50:17 -050054
Dean Nelson185c3a12008-07-29 22:34:11 -070055 ch->flags |= XPC_C_SETUP;
56
Dean Nelson2c2b94f2008-04-22 14:50:17 -050057 if (ch->flags & (XPC_C_CONNECTED | XPC_C_DISCONNECTING))
Dean Nelson89eb8eb2005-03-23 19:50:00 -070058 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -070059
Dean Nelson89eb8eb2005-03-23 19:50:00 -070060 DBUG_ON(ch->local_msgqueue == NULL);
61 DBUG_ON(ch->remote_msgqueue == NULL);
62 }
63
64 if (!(ch->flags & XPC_C_OPENREPLY)) {
65 ch->flags |= XPC_C_OPENREPLY;
Dean Nelson7fb5e592008-07-29 22:34:10 -070066 xpc_send_chctl_openreply(ch, irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -070067 }
68
Dean Nelson2c2b94f2008-04-22 14:50:17 -050069 if (!(ch->flags & XPC_C_ROPENREPLY))
Dean Nelson89eb8eb2005-03-23 19:50:00 -070070 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -070071
72 DBUG_ON(ch->remote_msgqueue_pa == 0);
73
74 ch->flags = (XPC_C_CONNECTED | XPC_C_SETUP); /* clear all else */
75
76 dev_info(xpc_chan, "channel %d to partition %d connected\n",
Dean Nelson35190502008-04-22 14:48:55 -050077 ch->number, ch->partid);
Dean Nelson89eb8eb2005-03-23 19:50:00 -070078
79 spin_unlock_irqrestore(&ch->lock, *irq_flags);
Dean Nelsona460ef82006-11-22 08:25:00 -060080 xpc_create_kthreads(ch, 1, 0);
Dean Nelson89eb8eb2005-03-23 19:50:00 -070081 spin_lock_irqsave(&ch->lock, *irq_flags);
82}
83
Dean Nelson89eb8eb2005-03-23 19:50:00 -070084/*
Dean Nelson89eb8eb2005-03-23 19:50:00 -070085 * spin_lock_irqsave() is expected to be held on entry.
86 */
87static void
88xpc_process_disconnect(struct xpc_channel *ch, unsigned long *irq_flags)
89{
90 struct xpc_partition *part = &xpc_partitions[ch->partid];
Dean Nelsona607c3892005-09-01 14:01:37 -050091 u32 channel_was_connected = (ch->flags & XPC_C_WASCONNECTED);
Dean Nelson89eb8eb2005-03-23 19:50:00 -070092
Dean Nelson89eb8eb2005-03-23 19:50:00 -070093 DBUG_ON(!spin_is_locked(&ch->lock));
94
Dean Nelson2c2b94f2008-04-22 14:50:17 -050095 if (!(ch->flags & XPC_C_DISCONNECTING))
Dean Nelson89eb8eb2005-03-23 19:50:00 -070096 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -070097
98 DBUG_ON(!(ch->flags & XPC_C_CLOSEREQUEST));
99
100 /* make sure all activity has settled down first */
101
Dean Nelsona460ef82006-11-22 08:25:00 -0600102 if (atomic_read(&ch->kthreads_assigned) > 0 ||
Dean Nelson35190502008-04-22 14:48:55 -0500103 atomic_read(&ch->references) > 0) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700104 return;
105 }
Dean Nelsona460ef82006-11-22 08:25:00 -0600106 DBUG_ON((ch->flags & XPC_C_CONNECTEDCALLOUT_MADE) &&
Dean Nelson35190502008-04-22 14:48:55 -0500107 !(ch->flags & XPC_C_DISCONNECTINGCALLOUT_MADE));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700108
Dean Nelsona607c3892005-09-01 14:01:37 -0500109 if (part->act_state == XPC_P_DEACTIVATING) {
110 /* can't proceed until the other side disengages from us */
Dean Nelsona47d5da2008-07-29 22:34:09 -0700111 if (xpc_partition_engaged(ch->partid))
Dean Nelsona607c3892005-09-01 14:01:37 -0500112 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700113
Dean Nelsona607c3892005-09-01 14:01:37 -0500114 } else {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700115
116 /* as long as the other side is up do the full protocol */
117
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500118 if (!(ch->flags & XPC_C_RCLOSEREQUEST))
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700119 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700120
121 if (!(ch->flags & XPC_C_CLOSEREPLY)) {
122 ch->flags |= XPC_C_CLOSEREPLY;
Dean Nelson7fb5e592008-07-29 22:34:10 -0700123 xpc_send_chctl_closereply(ch, irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700124 }
125
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500126 if (!(ch->flags & XPC_C_RCLOSEREPLY))
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700127 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700128 }
129
Dean Nelsona607c3892005-09-01 14:01:37 -0500130 /* wake those waiting for notify completion */
131 if (atomic_read(&ch->n_to_notify) > 0) {
132 /* >>> we do callout while holding ch->lock */
Dean Nelsona47d5da2008-07-29 22:34:09 -0700133 xpc_notify_senders_of_disconnect(ch);
Dean Nelsona607c3892005-09-01 14:01:37 -0500134 }
135
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700136 /* both sides are disconnected now */
137
Dean Nelson4c2cd962006-02-15 08:02:21 -0600138 if (ch->flags & XPC_C_DISCONNECTINGCALLOUT_MADE) {
Dean Nelson246c7e32005-12-22 14:32:56 -0600139 spin_unlock_irqrestore(&ch->lock, *irq_flags);
Dean Nelson65c17b82008-05-12 14:02:02 -0700140 xpc_disconnect_callout(ch, xpDisconnected);
Dean Nelson246c7e32005-12-22 14:32:56 -0600141 spin_lock_irqsave(&ch->lock, *irq_flags);
142 }
143
Dean Nelsona607c3892005-09-01 14:01:37 -0500144 /* it's now safe to free the channel's message queues */
145 xpc_free_msgqueues(ch);
146
Dean Nelson185c3a12008-07-29 22:34:11 -0700147 /*
148 * Mark the channel disconnected and clear all other flags, including
149 * XPC_C_SETUP (because of call to xpc_free_msgqueues()) but not
150 * including XPC_C_WDISCONNECT (if it was set).
151 */
Dean Nelsona607c3892005-09-01 14:01:37 -0500152 ch->flags = (XPC_C_DISCONNECTED | (ch->flags & XPC_C_WDISCONNECT));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700153
154 atomic_dec(&part->nchannels_active);
155
Dean Nelsona607c3892005-09-01 14:01:37 -0500156 if (channel_was_connected) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700157 dev_info(xpc_chan, "channel %d to partition %d disconnected, "
Dean Nelson35190502008-04-22 14:48:55 -0500158 "reason=%d\n", ch->number, ch->partid, ch->reason);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700159 }
Dean Nelsona607c3892005-09-01 14:01:37 -0500160
Dean Nelsona607c3892005-09-01 14:01:37 -0500161 if (ch->flags & XPC_C_WDISCONNECT) {
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500162 /* we won't lose the CPU since we're holding ch->lock */
163 complete(&ch->wdisconnect_wait);
Dean Nelson7fb5e592008-07-29 22:34:10 -0700164 } else if (ch->delayed_chctl_flags) {
Dean Nelsone54af722005-10-25 14:07:43 -0500165 if (part->act_state != XPC_P_DEACTIVATING) {
Dean Nelson7fb5e592008-07-29 22:34:10 -0700166 /* time to take action on any delayed chctl flags */
167 spin_lock(&part->chctl_lock);
168 part->chctl.flags[ch->number] |=
169 ch->delayed_chctl_flags;
170 spin_unlock(&part->chctl_lock);
Dean Nelsone54af722005-10-25 14:07:43 -0500171 }
Dean Nelson7fb5e592008-07-29 22:34:10 -0700172 ch->delayed_chctl_flags = 0;
Dean Nelsona607c3892005-09-01 14:01:37 -0500173 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700174}
175
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700176/*
177 * Process a change in the channel's remote connection state.
178 */
179static void
Dean Nelson7fb5e592008-07-29 22:34:10 -0700180xpc_process_openclose_chctl_flags(struct xpc_partition *part, int ch_number,
181 u8 chctl_flags)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700182{
183 unsigned long irq_flags;
184 struct xpc_openclose_args *args =
Dean Nelson35190502008-04-22 14:48:55 -0500185 &part->remote_openclose_args[ch_number];
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700186 struct xpc_channel *ch = &part->channels[ch_number];
Dean Nelson65c17b82008-05-12 14:02:02 -0700187 enum xp_retval reason;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700188
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700189 spin_lock_irqsave(&ch->lock, irq_flags);
190
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500191again:
Dean Nelsone54af722005-10-25 14:07:43 -0500192
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500193 if ((ch->flags & XPC_C_DISCONNECTED) &&
194 (ch->flags & XPC_C_WDISCONNECT)) {
Dean Nelsone54af722005-10-25 14:07:43 -0500195 /*
Dean Nelson7fb5e592008-07-29 22:34:10 -0700196 * Delay processing chctl flags until thread waiting disconnect
Dean Nelsone54af722005-10-25 14:07:43 -0500197 * has had a chance to see that the channel is disconnected.
198 */
Dean Nelson7fb5e592008-07-29 22:34:10 -0700199 ch->delayed_chctl_flags |= chctl_flags;
Dean Nelsone54af722005-10-25 14:07:43 -0500200 spin_unlock_irqrestore(&ch->lock, irq_flags);
201 return;
202 }
203
Dean Nelson7fb5e592008-07-29 22:34:10 -0700204 if (chctl_flags & XPC_CHCTL_CLOSEREQUEST) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700205
Dean Nelson7fb5e592008-07-29 22:34:10 -0700206 dev_dbg(xpc_chan, "XPC_CHCTL_CLOSEREQUEST (reason=%d) received "
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700207 "from partid=%d, channel=%d\n", args->reason,
208 ch->partid, ch->number);
209
210 /*
211 * If RCLOSEREQUEST is set, we're probably waiting for
212 * RCLOSEREPLY. We should find it and a ROPENREQUEST packed
Dean Nelson7fb5e592008-07-29 22:34:10 -0700213 * with this RCLOSEREQUEST in the chctl_flags.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700214 */
215
216 if (ch->flags & XPC_C_RCLOSEREQUEST) {
217 DBUG_ON(!(ch->flags & XPC_C_DISCONNECTING));
218 DBUG_ON(!(ch->flags & XPC_C_CLOSEREQUEST));
219 DBUG_ON(!(ch->flags & XPC_C_CLOSEREPLY));
220 DBUG_ON(ch->flags & XPC_C_RCLOSEREPLY);
221
Dean Nelson7fb5e592008-07-29 22:34:10 -0700222 DBUG_ON(!(chctl_flags & XPC_CHCTL_CLOSEREPLY));
223 chctl_flags &= ~XPC_CHCTL_CLOSEREPLY;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700224 ch->flags |= XPC_C_RCLOSEREPLY;
225
226 /* both sides have finished disconnecting */
227 xpc_process_disconnect(ch, &irq_flags);
Dean Nelsone54af722005-10-25 14:07:43 -0500228 DBUG_ON(!(ch->flags & XPC_C_DISCONNECTED));
229 goto again;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700230 }
231
232 if (ch->flags & XPC_C_DISCONNECTED) {
Dean Nelson7fb5e592008-07-29 22:34:10 -0700233 if (!(chctl_flags & XPC_CHCTL_OPENREQUEST)) {
234 if (part->chctl.flags[ch_number] &
235 XPC_CHCTL_OPENREQUEST) {
Dean Nelsone54af722005-10-25 14:07:43 -0500236
Dean Nelson7fb5e592008-07-29 22:34:10 -0700237 DBUG_ON(ch->delayed_chctl_flags != 0);
238 spin_lock(&part->chctl_lock);
239 part->chctl.flags[ch_number] |=
240 XPC_CHCTL_CLOSEREQUEST;
241 spin_unlock(&part->chctl_lock);
Dean Nelsone54af722005-10-25 14:07:43 -0500242 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700243 spin_unlock_irqrestore(&ch->lock, irq_flags);
244 return;
245 }
246
247 XPC_SET_REASON(ch, 0, 0);
248 ch->flags &= ~XPC_C_DISCONNECTED;
249
250 atomic_inc(&part->nchannels_active);
251 ch->flags |= (XPC_C_CONNECTING | XPC_C_ROPENREQUEST);
252 }
253
Dean Nelson7fb5e592008-07-29 22:34:10 -0700254 chctl_flags &= ~(XPC_CHCTL_OPENREQUEST | XPC_CHCTL_OPENREPLY);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700255
256 /*
257 * The meaningful CLOSEREQUEST connection state fields are:
258 * reason = reason connection is to be closed
259 */
260
261 ch->flags |= XPC_C_RCLOSEREQUEST;
262
263 if (!(ch->flags & XPC_C_DISCONNECTING)) {
264 reason = args->reason;
Dean Nelson65c17b82008-05-12 14:02:02 -0700265 if (reason <= xpSuccess || reason > xpUnknownReason)
266 reason = xpUnknownReason;
267 else if (reason == xpUnregistering)
268 reason = xpOtherUnregistering;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700269
270 XPC_DISCONNECT_CHANNEL(ch, reason, &irq_flags);
Dean Nelsone54af722005-10-25 14:07:43 -0500271
Dean Nelson7fb5e592008-07-29 22:34:10 -0700272 DBUG_ON(chctl_flags & XPC_CHCTL_CLOSEREPLY);
Dean Nelsone54af722005-10-25 14:07:43 -0500273 spin_unlock_irqrestore(&ch->lock, irq_flags);
274 return;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700275 }
Dean Nelsone54af722005-10-25 14:07:43 -0500276
277 xpc_process_disconnect(ch, &irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700278 }
279
Dean Nelson7fb5e592008-07-29 22:34:10 -0700280 if (chctl_flags & XPC_CHCTL_CLOSEREPLY) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700281
Dean Nelson7fb5e592008-07-29 22:34:10 -0700282 dev_dbg(xpc_chan, "XPC_CHCTL_CLOSEREPLY received from partid="
283 "%d, channel=%d\n", ch->partid, ch->number);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700284
285 if (ch->flags & XPC_C_DISCONNECTED) {
286 DBUG_ON(part->act_state != XPC_P_DEACTIVATING);
287 spin_unlock_irqrestore(&ch->lock, irq_flags);
288 return;
289 }
290
291 DBUG_ON(!(ch->flags & XPC_C_CLOSEREQUEST));
Dean Nelsone54af722005-10-25 14:07:43 -0500292
293 if (!(ch->flags & XPC_C_RCLOSEREQUEST)) {
Dean Nelson7fb5e592008-07-29 22:34:10 -0700294 if (part->chctl.flags[ch_number] &
295 XPC_CHCTL_CLOSEREQUEST) {
Dean Nelsone54af722005-10-25 14:07:43 -0500296
Dean Nelson7fb5e592008-07-29 22:34:10 -0700297 DBUG_ON(ch->delayed_chctl_flags != 0);
298 spin_lock(&part->chctl_lock);
299 part->chctl.flags[ch_number] |=
300 XPC_CHCTL_CLOSEREPLY;
301 spin_unlock(&part->chctl_lock);
Dean Nelsone54af722005-10-25 14:07:43 -0500302 }
303 spin_unlock_irqrestore(&ch->lock, irq_flags);
304 return;
305 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700306
307 ch->flags |= XPC_C_RCLOSEREPLY;
308
309 if (ch->flags & XPC_C_CLOSEREPLY) {
310 /* both sides have finished disconnecting */
311 xpc_process_disconnect(ch, &irq_flags);
312 }
313 }
314
Dean Nelson7fb5e592008-07-29 22:34:10 -0700315 if (chctl_flags & XPC_CHCTL_OPENREQUEST) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700316
Dean Nelson7fb5e592008-07-29 22:34:10 -0700317 dev_dbg(xpc_chan, "XPC_CHCTL_OPENREQUEST (msg_size=%d, "
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700318 "local_nentries=%d) received from partid=%d, "
319 "channel=%d\n", args->msg_size, args->local_nentries,
320 ch->partid, ch->number);
321
Dean Nelsone54af722005-10-25 14:07:43 -0500322 if (part->act_state == XPC_P_DEACTIVATING ||
Dean Nelson35190502008-04-22 14:48:55 -0500323 (ch->flags & XPC_C_ROPENREQUEST)) {
Dean Nelsone54af722005-10-25 14:07:43 -0500324 spin_unlock_irqrestore(&ch->lock, irq_flags);
325 return;
326 }
327
328 if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_WDISCONNECT)) {
Dean Nelson7fb5e592008-07-29 22:34:10 -0700329 ch->delayed_chctl_flags |= XPC_CHCTL_OPENREQUEST;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700330 spin_unlock_irqrestore(&ch->lock, irq_flags);
331 return;
332 }
333 DBUG_ON(!(ch->flags & (XPC_C_DISCONNECTED |
Dean Nelson35190502008-04-22 14:48:55 -0500334 XPC_C_OPENREQUEST)));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700335 DBUG_ON(ch->flags & (XPC_C_ROPENREQUEST | XPC_C_ROPENREPLY |
Dean Nelson35190502008-04-22 14:48:55 -0500336 XPC_C_OPENREPLY | XPC_C_CONNECTED));
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700337
338 /*
339 * The meaningful OPENREQUEST connection state fields are:
340 * msg_size = size of channel's messages in bytes
341 * local_nentries = remote partition's local_nentries
342 */
Dean Nelsone54af722005-10-25 14:07:43 -0500343 if (args->msg_size == 0 || args->local_nentries == 0) {
344 /* assume OPENREQUEST was delayed by mistake */
345 spin_unlock_irqrestore(&ch->lock, irq_flags);
346 return;
347 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700348
349 ch->flags |= (XPC_C_ROPENREQUEST | XPC_C_CONNECTING);
350 ch->remote_nentries = args->local_nentries;
351
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700352 if (ch->flags & XPC_C_OPENREQUEST) {
353 if (args->msg_size != ch->msg_size) {
Dean Nelson65c17b82008-05-12 14:02:02 -0700354 XPC_DISCONNECT_CHANNEL(ch, xpUnequalMsgSizes,
Dean Nelson35190502008-04-22 14:48:55 -0500355 &irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700356 spin_unlock_irqrestore(&ch->lock, irq_flags);
357 return;
358 }
359 } else {
360 ch->msg_size = args->msg_size;
361
362 XPC_SET_REASON(ch, 0, 0);
363 ch->flags &= ~XPC_C_DISCONNECTED;
364
365 atomic_inc(&part->nchannels_active);
366 }
367
368 xpc_process_connect(ch, &irq_flags);
369 }
370
Dean Nelson7fb5e592008-07-29 22:34:10 -0700371 if (chctl_flags & XPC_CHCTL_OPENREPLY) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700372
Dean Nelson7fb5e592008-07-29 22:34:10 -0700373 dev_dbg(xpc_chan, "XPC_CHCTL_OPENREPLY (local_msgqueue_pa="
374 "0x%lx, local_nentries=%d, remote_nentries=%d) "
375 "received from partid=%d, channel=%d\n",
376 args->local_msgqueue_pa, args->local_nentries,
377 args->remote_nentries, ch->partid, ch->number);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700378
379 if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_DISCONNECTED)) {
380 spin_unlock_irqrestore(&ch->lock, irq_flags);
381 return;
382 }
Dean Nelsone54af722005-10-25 14:07:43 -0500383 if (!(ch->flags & XPC_C_OPENREQUEST)) {
Dean Nelson65c17b82008-05-12 14:02:02 -0700384 XPC_DISCONNECT_CHANNEL(ch, xpOpenCloseError,
Dean Nelson35190502008-04-22 14:48:55 -0500385 &irq_flags);
Dean Nelsone54af722005-10-25 14:07:43 -0500386 spin_unlock_irqrestore(&ch->lock, irq_flags);
387 return;
388 }
389
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700390 DBUG_ON(!(ch->flags & XPC_C_ROPENREQUEST));
391 DBUG_ON(ch->flags & XPC_C_CONNECTED);
392
393 /*
394 * The meaningful OPENREPLY connection state fields are:
395 * local_msgqueue_pa = physical address of remote
Dean Nelson35190502008-04-22 14:48:55 -0500396 * partition's local_msgqueue
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700397 * local_nentries = remote partition's local_nentries
398 * remote_nentries = remote partition's remote_nentries
399 */
400 DBUG_ON(args->local_msgqueue_pa == 0);
401 DBUG_ON(args->local_nentries == 0);
402 DBUG_ON(args->remote_nentries == 0);
403
404 ch->flags |= XPC_C_ROPENREPLY;
405 ch->remote_msgqueue_pa = args->local_msgqueue_pa;
406
407 if (args->local_nentries < ch->remote_nentries) {
Dean Nelson7fb5e592008-07-29 22:34:10 -0700408 dev_dbg(xpc_chan, "XPC_CHCTL_OPENREPLY: new "
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700409 "remote_nentries=%d, old remote_nentries=%d, "
410 "partid=%d, channel=%d\n",
411 args->local_nentries, ch->remote_nentries,
412 ch->partid, ch->number);
413
414 ch->remote_nentries = args->local_nentries;
415 }
416 if (args->remote_nentries < ch->local_nentries) {
Dean Nelson7fb5e592008-07-29 22:34:10 -0700417 dev_dbg(xpc_chan, "XPC_CHCTL_OPENREPLY: new "
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700418 "local_nentries=%d, old local_nentries=%d, "
419 "partid=%d, channel=%d\n",
420 args->remote_nentries, ch->local_nentries,
421 ch->partid, ch->number);
422
423 ch->local_nentries = args->remote_nentries;
424 }
425
426 xpc_process_connect(ch, &irq_flags);
427 }
428
429 spin_unlock_irqrestore(&ch->lock, irq_flags);
430}
431
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700432/*
433 * Attempt to establish a channel connection to a remote partition.
434 */
Dean Nelson65c17b82008-05-12 14:02:02 -0700435static enum xp_retval
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700436xpc_connect_channel(struct xpc_channel *ch)
437{
438 unsigned long irq_flags;
439 struct xpc_registration *registration = &xpc_registrations[ch->number];
440
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500441 if (mutex_trylock(&registration->mutex) == 0)
Dean Nelson65c17b82008-05-12 14:02:02 -0700442 return xpRetry;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700443
444 if (!XPC_CHANNEL_REGISTERED(ch->number)) {
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500445 mutex_unlock(&registration->mutex);
Dean Nelson65c17b82008-05-12 14:02:02 -0700446 return xpUnregistered;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700447 }
448
449 spin_lock_irqsave(&ch->lock, irq_flags);
450
451 DBUG_ON(ch->flags & XPC_C_CONNECTED);
452 DBUG_ON(ch->flags & XPC_C_OPENREQUEST);
453
454 if (ch->flags & XPC_C_DISCONNECTING) {
455 spin_unlock_irqrestore(&ch->lock, irq_flags);
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500456 mutex_unlock(&registration->mutex);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700457 return ch->reason;
458 }
459
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700460 /* add info from the channel connect registration to the channel */
461
462 ch->kthreads_assigned_limit = registration->assigned_limit;
463 ch->kthreads_idle_limit = registration->idle_limit;
464 DBUG_ON(atomic_read(&ch->kthreads_assigned) != 0);
465 DBUG_ON(atomic_read(&ch->kthreads_idle) != 0);
466 DBUG_ON(atomic_read(&ch->kthreads_active) != 0);
467
468 ch->func = registration->func;
469 DBUG_ON(registration->func == NULL);
470 ch->key = registration->key;
471
472 ch->local_nentries = registration->nentries;
473
474 if (ch->flags & XPC_C_ROPENREQUEST) {
475 if (registration->msg_size != ch->msg_size) {
476 /* the local and remote sides aren't the same */
477
478 /*
479 * Because XPC_DISCONNECT_CHANNEL() can block we're
480 * forced to up the registration sema before we unlock
481 * the channel lock. But that's okay here because we're
482 * done with the part that required the registration
483 * sema. XPC_DISCONNECT_CHANNEL() requires that the
484 * channel lock be locked and will unlock and relock
485 * the channel lock as needed.
486 */
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500487 mutex_unlock(&registration->mutex);
Dean Nelson65c17b82008-05-12 14:02:02 -0700488 XPC_DISCONNECT_CHANNEL(ch, xpUnequalMsgSizes,
Dean Nelson35190502008-04-22 14:48:55 -0500489 &irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700490 spin_unlock_irqrestore(&ch->lock, irq_flags);
Dean Nelson65c17b82008-05-12 14:02:02 -0700491 return xpUnequalMsgSizes;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700492 }
493 } else {
494 ch->msg_size = registration->msg_size;
495
496 XPC_SET_REASON(ch, 0, 0);
497 ch->flags &= ~XPC_C_DISCONNECTED;
498
499 atomic_inc(&xpc_partitions[ch->partid].nchannels_active);
500 }
501
Jes Sorensenf9e505a2006-01-17 12:52:21 -0500502 mutex_unlock(&registration->mutex);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700503
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700504 /* initiate the connection */
505
506 ch->flags |= (XPC_C_OPENREQUEST | XPC_C_CONNECTING);
Dean Nelson7fb5e592008-07-29 22:34:10 -0700507 xpc_send_chctl_openrequest(ch, &irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700508
509 xpc_process_connect(ch, &irq_flags);
510
511 spin_unlock_irqrestore(&ch->lock, irq_flags);
512
Dean Nelson65c17b82008-05-12 14:02:02 -0700513 return xpSuccess;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700514}
515
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700516void
Dean Nelson7fb5e592008-07-29 22:34:10 -0700517xpc_process_sent_chctl_flags(struct xpc_partition *part)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700518{
519 unsigned long irq_flags;
Dean Nelson7fb5e592008-07-29 22:34:10 -0700520 union xpc_channel_ctl_flags chctl;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700521 struct xpc_channel *ch;
522 int ch_number;
Dean Nelsona607c3892005-09-01 14:01:37 -0500523 u32 ch_flags;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700524
Dean Nelson7fb5e592008-07-29 22:34:10 -0700525 chctl.all_flags = xpc_get_chctl_all_flags(part);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700526
527 /*
528 * Initiate channel connections for registered channels.
529 *
530 * For each connected channel that has pending messages activate idle
531 * kthreads and/or create new kthreads as needed.
532 */
533
534 for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
535 ch = &part->channels[ch_number];
536
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700537 /*
Dean Nelson7fb5e592008-07-29 22:34:10 -0700538 * Process any open or close related chctl flags, and then deal
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700539 * with connecting or disconnecting the channel as required.
540 */
541
Dean Nelson7fb5e592008-07-29 22:34:10 -0700542 if (chctl.flags[ch_number] & XPC_OPENCLOSE_CHCTL_FLAGS) {
543 xpc_process_openclose_chctl_flags(part, ch_number,
544 chctl.flags[ch_number]);
545 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700546
Dean Nelsona607c3892005-09-01 14:01:37 -0500547 ch_flags = ch->flags; /* need an atomic snapshot of flags */
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700548
Dean Nelsona607c3892005-09-01 14:01:37 -0500549 if (ch_flags & XPC_C_DISCONNECTING) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700550 spin_lock_irqsave(&ch->lock, irq_flags);
551 xpc_process_disconnect(ch, &irq_flags);
552 spin_unlock_irqrestore(&ch->lock, irq_flags);
553 continue;
554 }
555
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500556 if (part->act_state == XPC_P_DEACTIVATING)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700557 continue;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700558
Dean Nelsona607c3892005-09-01 14:01:37 -0500559 if (!(ch_flags & XPC_C_CONNECTED)) {
560 if (!(ch_flags & XPC_C_OPENREQUEST)) {
561 DBUG_ON(ch_flags & XPC_C_SETUP);
Dean Nelson35190502008-04-22 14:48:55 -0500562 (void)xpc_connect_channel(ch);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700563 } else {
564 spin_lock_irqsave(&ch->lock, irq_flags);
565 xpc_process_connect(ch, &irq_flags);
566 spin_unlock_irqrestore(&ch->lock, irq_flags);
567 }
568 continue;
569 }
570
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700571 /*
Dean Nelson7fb5e592008-07-29 22:34:10 -0700572 * Process any message related chctl flags, this may involve
573 * the activation of kthreads to deliver any pending messages
574 * sent from the other partition.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700575 */
576
Dean Nelson7fb5e592008-07-29 22:34:10 -0700577 if (chctl.flags[ch_number] & XPC_MSG_CHCTL_FLAGS)
578 xpc_process_msg_chctl_flags(part, ch_number);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700579 }
580}
581
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700582/*
Dean Nelsona607c3892005-09-01 14:01:37 -0500583 * XPC's heartbeat code calls this function to inform XPC that a partition is
584 * going down. XPC responds by tearing down the XPartition Communication
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700585 * infrastructure used for the just downed partition.
586 *
587 * XPC's heartbeat code will never call this function and xpc_partition_up()
588 * at the same time. Nor will it ever make multiple calls to either function
589 * at the same time.
590 */
591void
Dean Nelson65c17b82008-05-12 14:02:02 -0700592xpc_partition_going_down(struct xpc_partition *part, enum xp_retval reason)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700593{
594 unsigned long irq_flags;
595 int ch_number;
596 struct xpc_channel *ch;
597
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700598 dev_dbg(xpc_chan, "deactivating partition %d, reason=%d\n",
599 XPC_PARTID(part), reason);
600
601 if (!xpc_part_ref(part)) {
602 /* infrastructure for this partition isn't currently set up */
603 return;
604 }
605
Dean Nelsona607c3892005-09-01 14:01:37 -0500606 /* disconnect channels associated with the partition going down */
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700607
608 for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
609 ch = &part->channels[ch_number];
610
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700611 xpc_msgqueue_ref(ch);
612 spin_lock_irqsave(&ch->lock, irq_flags);
613
614 XPC_DISCONNECT_CHANNEL(ch, reason, &irq_flags);
615
616 spin_unlock_irqrestore(&ch->lock, irq_flags);
617 xpc_msgqueue_deref(ch);
618 }
619
620 xpc_wakeup_channel_mgr(part);
621
622 xpc_part_deref(part);
623}
624
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700625/*
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700626 * Called by XP at the time of channel connection registration to cause
627 * XPC to establish connections to all currently active partitions.
628 */
629void
630xpc_initiate_connect(int ch_number)
631{
Dean Nelson64d032b2008-05-12 14:02:03 -0700632 short partid;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700633 struct xpc_partition *part;
634 struct xpc_channel *ch;
635
Dean Nelsonbc63d382008-07-29 22:34:04 -0700636 DBUG_ON(ch_number < 0 || ch_number >= XPC_MAX_NCHANNELS);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700637
Dean Nelsonbc63d382008-07-29 22:34:04 -0700638 for (partid = 0; partid < xp_max_npartitions; partid++) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700639 part = &xpc_partitions[partid];
640
641 if (xpc_part_ref(part)) {
642 ch = &part->channels[ch_number];
643
Dean Nelsone54af722005-10-25 14:07:43 -0500644 /*
645 * Initiate the establishment of a connection on the
646 * newly registered channel to the remote partition.
647 */
648 xpc_wakeup_channel_mgr(part);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700649 xpc_part_deref(part);
650 }
651 }
652}
653
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700654void
655xpc_connected_callout(struct xpc_channel *ch)
656{
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700657 /* let the registerer know that a connection has been established */
658
659 if (ch->func != NULL) {
Dean Nelson65c17b82008-05-12 14:02:02 -0700660 dev_dbg(xpc_chan, "ch->func() called, reason=xpConnected, "
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700661 "partid=%d, channel=%d\n", ch->partid, ch->number);
662
Dean Nelson65c17b82008-05-12 14:02:02 -0700663 ch->func(xpConnected, ch->partid, ch->number,
Dean Nelson35190502008-04-22 14:48:55 -0500664 (void *)(u64)ch->local_nentries, ch->key);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700665
Dean Nelson65c17b82008-05-12 14:02:02 -0700666 dev_dbg(xpc_chan, "ch->func() returned, reason=xpConnected, "
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700667 "partid=%d, channel=%d\n", ch->partid, ch->number);
668 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700669}
670
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700671/*
672 * Called by XP at the time of channel connection unregistration to cause
673 * XPC to teardown all current connections for the specified channel.
674 *
675 * Before returning xpc_initiate_disconnect() will wait until all connections
676 * on the specified channel have been closed/torndown. So the caller can be
677 * assured that they will not be receiving any more callouts from XPC to the
678 * function they registered via xpc_connect().
679 *
680 * Arguments:
681 *
682 * ch_number - channel # to unregister.
683 */
684void
685xpc_initiate_disconnect(int ch_number)
686{
687 unsigned long irq_flags;
Dean Nelson64d032b2008-05-12 14:02:03 -0700688 short partid;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700689 struct xpc_partition *part;
690 struct xpc_channel *ch;
691
Dean Nelsonbc63d382008-07-29 22:34:04 -0700692 DBUG_ON(ch_number < 0 || ch_number >= XPC_MAX_NCHANNELS);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700693
694 /* initiate the channel disconnect for every active partition */
Dean Nelsonbc63d382008-07-29 22:34:04 -0700695 for (partid = 0; partid < xp_max_npartitions; partid++) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700696 part = &xpc_partitions[partid];
697
698 if (xpc_part_ref(part)) {
699 ch = &part->channels[ch_number];
700 xpc_msgqueue_ref(ch);
701
702 spin_lock_irqsave(&ch->lock, irq_flags);
703
Dean Nelsona607c3892005-09-01 14:01:37 -0500704 if (!(ch->flags & XPC_C_DISCONNECTED)) {
705 ch->flags |= XPC_C_WDISCONNECT;
706
Dean Nelson65c17b82008-05-12 14:02:02 -0700707 XPC_DISCONNECT_CHANNEL(ch, xpUnregistering,
Dean Nelson35190502008-04-22 14:48:55 -0500708 &irq_flags);
Dean Nelsona607c3892005-09-01 14:01:37 -0500709 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700710
711 spin_unlock_irqrestore(&ch->lock, irq_flags);
712
713 xpc_msgqueue_deref(ch);
714 xpc_part_deref(part);
715 }
716 }
717
718 xpc_disconnect_wait(ch_number);
719}
720
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700721/*
722 * To disconnect a channel, and reflect it back to all who may be waiting.
723 *
Dean Nelsona607c3892005-09-01 14:01:37 -0500724 * An OPEN is not allowed until XPC_C_DISCONNECTING is cleared by
725 * xpc_process_disconnect(), and if set, XPC_C_WDISCONNECT is cleared by
726 * xpc_disconnect_wait().
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700727 *
728 * THE CHANNEL IS TO BE LOCKED BY THE CALLER AND WILL REMAIN LOCKED UPON RETURN.
729 */
730void
731xpc_disconnect_channel(const int line, struct xpc_channel *ch,
Dean Nelson65c17b82008-05-12 14:02:02 -0700732 enum xp_retval reason, unsigned long *irq_flags)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700733{
Dean Nelsona607c3892005-09-01 14:01:37 -0500734 u32 channel_was_connected = (ch->flags & XPC_C_CONNECTED);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700735
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700736 DBUG_ON(!spin_is_locked(&ch->lock));
737
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500738 if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_DISCONNECTED))
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700739 return;
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500740
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700741 DBUG_ON(!(ch->flags & (XPC_C_CONNECTING | XPC_C_CONNECTED)));
742
743 dev_dbg(xpc_chan, "reason=%d, line=%d, partid=%d, channel=%d\n",
744 reason, line, ch->partid, ch->number);
745
746 XPC_SET_REASON(ch, reason, line);
747
Dean Nelsona607c3892005-09-01 14:01:37 -0500748 ch->flags |= (XPC_C_CLOSEREQUEST | XPC_C_DISCONNECTING);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700749 /* some of these may not have been set */
750 ch->flags &= ~(XPC_C_OPENREQUEST | XPC_C_OPENREPLY |
Dean Nelson35190502008-04-22 14:48:55 -0500751 XPC_C_ROPENREQUEST | XPC_C_ROPENREPLY |
752 XPC_C_CONNECTING | XPC_C_CONNECTED);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700753
Dean Nelson7fb5e592008-07-29 22:34:10 -0700754 xpc_send_chctl_closerequest(ch, irq_flags);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700755
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500756 if (channel_was_connected)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700757 ch->flags |= XPC_C_WASCONNECTED;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700758
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700759 spin_unlock_irqrestore(&ch->lock, *irq_flags);
760
Dean Nelsona607c3892005-09-01 14:01:37 -0500761 /* wake all idle kthreads so they can exit */
762 if (atomic_read(&ch->kthreads_idle) > 0) {
763 wake_up_all(&ch->idle_wq);
Dean Nelsona460ef82006-11-22 08:25:00 -0600764
765 } else if ((ch->flags & XPC_C_CONNECTEDCALLOUT_MADE) &&
Dean Nelson35190502008-04-22 14:48:55 -0500766 !(ch->flags & XPC_C_DISCONNECTINGCALLOUT)) {
Dean Nelson65c17b82008-05-12 14:02:02 -0700767 /* start a kthread that will do the xpDisconnecting callout */
Dean Nelsona460ef82006-11-22 08:25:00 -0600768 xpc_create_kthreads(ch, 1, 1);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700769 }
770
Dean Nelsona607c3892005-09-01 14:01:37 -0500771 /* wake those waiting to allocate an entry from the local msg queue */
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500772 if (atomic_read(&ch->n_on_msg_allocate_wq) > 0)
Dean Nelsona607c3892005-09-01 14:01:37 -0500773 wake_up(&ch->msg_allocate_wq);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700774
775 spin_lock_irqsave(&ch->lock, *irq_flags);
776}
777
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700778void
Dean Nelson65c17b82008-05-12 14:02:02 -0700779xpc_disconnect_callout(struct xpc_channel *ch, enum xp_retval reason)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700780{
781 /*
Dean Nelsona607c3892005-09-01 14:01:37 -0500782 * Let the channel's registerer know that the channel is being
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700783 * disconnected. We don't want to do this if the registerer was never
Dean Nelsona607c3892005-09-01 14:01:37 -0500784 * informed of a connection being made.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700785 */
786
787 if (ch->func != NULL) {
Dean Nelson246c7e32005-12-22 14:32:56 -0600788 dev_dbg(xpc_chan, "ch->func() called, reason=%d, partid=%d, "
789 "channel=%d\n", reason, ch->partid, ch->number);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700790
Dean Nelson246c7e32005-12-22 14:32:56 -0600791 ch->func(reason, ch->partid, ch->number, NULL, ch->key);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700792
Dean Nelson246c7e32005-12-22 14:32:56 -0600793 dev_dbg(xpc_chan, "ch->func() returned, reason=%d, partid=%d, "
794 "channel=%d\n", reason, ch->partid, ch->number);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700795 }
796}
797
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700798/*
799 * Wait for a message entry to become available for the specified channel,
800 * but don't wait any longer than 1 jiffy.
801 */
Dean Nelson33ba3c72008-07-29 22:34:07 -0700802enum xp_retval
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700803xpc_allocate_msg_wait(struct xpc_channel *ch)
804{
Dean Nelson65c17b82008-05-12 14:02:02 -0700805 enum xp_retval ret;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700806
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700807 if (ch->flags & XPC_C_DISCONNECTING) {
Dean Nelson65c17b82008-05-12 14:02:02 -0700808 DBUG_ON(ch->reason == xpInterrupted);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700809 return ch->reason;
810 }
811
812 atomic_inc(&ch->n_on_msg_allocate_wq);
813 ret = interruptible_sleep_on_timeout(&ch->msg_allocate_wq, 1);
814 atomic_dec(&ch->n_on_msg_allocate_wq);
815
816 if (ch->flags & XPC_C_DISCONNECTING) {
817 ret = ch->reason;
Dean Nelson65c17b82008-05-12 14:02:02 -0700818 DBUG_ON(ch->reason == xpInterrupted);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700819 } else if (ret == 0) {
Dean Nelson65c17b82008-05-12 14:02:02 -0700820 ret = xpTimeout;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700821 } else {
Dean Nelson65c17b82008-05-12 14:02:02 -0700822 ret = xpInterrupted;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700823 }
824
825 return ret;
826}
827
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700828/*
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700829 * Send a message that contains the user's payload on the specified channel
830 * connected to the specified partition.
831 *
832 * NOTE that this routine can sleep waiting for a message entry to become
833 * available. To not sleep, pass in the XPC_NOWAIT flag.
834 *
835 * Once sent, this routine will not wait for the message to be received, nor
836 * will notification be given when it does happen.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700837 *
838 * Arguments:
839 *
840 * partid - ID of partition to which the channel is connected.
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700841 * ch_number - channel # to send message on.
842 * flags - see xp.h for valid flags.
843 * payload - pointer to the payload which is to be sent.
844 * payload_size - size of the payload in bytes.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700845 */
Dean Nelson65c17b82008-05-12 14:02:02 -0700846enum xp_retval
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700847xpc_initiate_send(short partid, int ch_number, u32 flags, void *payload,
848 u16 payload_size)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700849{
850 struct xpc_partition *part = &xpc_partitions[partid];
Dean Nelson65c17b82008-05-12 14:02:02 -0700851 enum xp_retval ret = xpUnknownReason;
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700852
853 dev_dbg(xpc_chan, "payload=0x%p, partid=%d, channel=%d\n", payload,
854 partid, ch_number);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700855
Dean Nelsonbc63d382008-07-29 22:34:04 -0700856 DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700857 DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700858 DBUG_ON(payload == NULL);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700859
860 if (xpc_part_ref(part)) {
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700861 ret = xpc_send_msg(&part->channels[ch_number], flags, payload,
862 payload_size, 0, NULL, NULL);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700863 xpc_part_deref(part);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700864 }
865
866 return ret;
867}
868
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700869/*
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700870 * Send a message that contains the user's payload on the specified channel
871 * connected to the specified partition.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700872 *
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700873 * NOTE that this routine can sleep waiting for a message entry to become
874 * available. To not sleep, pass in the XPC_NOWAIT flag.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700875 *
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700876 * This routine will not wait for the message to be sent or received.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700877 *
878 * Once the remote end of the channel has received the message, the function
879 * passed as an argument to xpc_initiate_send_notify() will be called. This
880 * allows the sender to free up or re-use any buffers referenced by the
881 * message, but does NOT mean the message has been processed at the remote
882 * end by a receiver.
883 *
884 * If this routine returns an error, the caller's function will NOT be called.
885 *
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700886 * Arguments:
887 *
888 * partid - ID of partition to which the channel is connected.
889 * ch_number - channel # to send message on.
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700890 * flags - see xp.h for valid flags.
891 * payload - pointer to the payload which is to be sent.
892 * payload_size - size of the payload in bytes.
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700893 * func - function to call with asynchronous notification of message
894 * receipt. THIS FUNCTION MUST BE NON-BLOCKING.
895 * key - user-defined key to be passed to the function when it's called.
896 */
Dean Nelson65c17b82008-05-12 14:02:02 -0700897enum xp_retval
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700898xpc_initiate_send_notify(short partid, int ch_number, u32 flags, void *payload,
899 u16 payload_size, xpc_notify_func func, void *key)
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700900{
901 struct xpc_partition *part = &xpc_partitions[partid];
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700902 enum xp_retval ret = xpUnknownReason;
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700903
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700904 dev_dbg(xpc_chan, "payload=0x%p, partid=%d, channel=%d\n", payload,
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700905 partid, ch_number);
906
Dean Nelsonbc63d382008-07-29 22:34:04 -0700907 DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700908 DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700909 DBUG_ON(payload == NULL);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700910 DBUG_ON(func == NULL);
911
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700912 if (xpc_part_ref(part)) {
913 ret = xpc_send_msg(&part->channels[ch_number], flags, payload,
914 payload_size, XPC_N_CALL, func, key);
915 xpc_part_deref(part);
916 }
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700917 return ret;
918}
919
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700920/*
921 * Deliver a message to its intended recipient.
922 */
923void
924xpc_deliver_msg(struct xpc_channel *ch)
925{
926 struct xpc_msg *msg;
927
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500928 msg = xpc_get_deliverable_msg(ch);
929 if (msg != NULL) {
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700930
931 /*
932 * This ref is taken to protect the payload itself from being
933 * freed before the user is finished with it, which the user
934 * indicates by calling xpc_initiate_received().
935 */
936 xpc_msgqueue_ref(ch);
937
938 atomic_inc(&ch->kthreads_active);
939
940 if (ch->func != NULL) {
941 dev_dbg(xpc_chan, "ch->func() called, msg=0x%p, "
942 "msg_number=%ld, partid=%d, channel=%d\n",
Dean Nelson35190502008-04-22 14:48:55 -0500943 (void *)msg, msg->number, ch->partid,
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700944 ch->number);
945
946 /* deliver the message to its intended recipient */
Dean Nelson65c17b82008-05-12 14:02:02 -0700947 ch->func(xpMsgReceived, ch->partid, ch->number,
Dean Nelson35190502008-04-22 14:48:55 -0500948 &msg->payload, ch->key);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700949
950 dev_dbg(xpc_chan, "ch->func() returned, msg=0x%p, "
951 "msg_number=%ld, partid=%d, channel=%d\n",
Dean Nelson35190502008-04-22 14:48:55 -0500952 (void *)msg, msg->number, ch->partid,
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700953 ch->number);
954 }
955
956 atomic_dec(&ch->kthreads_active);
957 }
958}
959
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700960/*
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700961 * Acknowledge receipt of a delivered message.
962 *
963 * If a message has XPC_M_INTERRUPT set, send an interrupt to the partition
964 * that sent the message.
965 *
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
968 * fact that we called xpc_msgqueue_ref() in xpc_deliver_msg().
969 *
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;
982 struct xpc_msg *msg = XPC_MSG_ADDRESS(payload);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700983
Dean Nelsonbc63d382008-07-29 22:34:04 -0700984 DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700985 DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
986
987 ch = &part->channels[ch_number];
Dean Nelson33ba3c72008-07-29 22:34:07 -0700988 xpc_received_msg(ch, msg);
Dean Nelson89eb8eb2005-03-23 19:50:00 -0700989
990 /* the call to xpc_msgqueue_ref() was done by xpc_deliver_msg() */
991 xpc_msgqueue_deref(ch);
992}