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
6 * Copyright (c) 2004-2008 Silicon Graphics, Inc. All Rights Reserved.
10 * Cross Partition Communication (XPC) channel support.
12 * This is the part of XPC that manages the channels and
13 * sends/receives messages across them to/from other partitions.
17 #include <linux/device.h>
21 * Process a connect message from a remote partition.
23 * Note: xpc_process_connect() is expecting to be called with the
24 * spin_lock_irqsave held and will leave it locked upon return.
27 xpc_process_connect(struct xpc_channel *ch, unsigned long *irq_flags)
31 DBUG_ON(!spin_is_locked(&ch->lock));
33 if (!(ch->flags & XPC_C_OPENREQUEST) ||
34 !(ch->flags & XPC_C_ROPENREQUEST)) {
35 /* nothing more to do for now */
38 DBUG_ON(!(ch->flags & XPC_C_CONNECTING));
40 if (!(ch->flags & XPC_C_SETUP)) {
41 spin_unlock_irqrestore(&ch->lock, *irq_flags);
42 ret = xpc_setup_msg_structures(ch);
43 spin_lock_irqsave(&ch->lock, *irq_flags);
46 XPC_DISCONNECT_CHANNEL(ch, ret, irq_flags);
48 ch->flags |= XPC_C_SETUP;
50 if (ch->flags & (XPC_C_CONNECTED | XPC_C_DISCONNECTING))
53 DBUG_ON(ch->local_msgqueue == NULL);
54 DBUG_ON(ch->remote_msgqueue == NULL);
57 if (!(ch->flags & XPC_C_OPENREPLY)) {
58 ch->flags |= XPC_C_OPENREPLY;
59 xpc_send_chctl_openreply(ch, irq_flags);
62 if (!(ch->flags & XPC_C_ROPENREPLY))
65 ch->flags = (XPC_C_CONNECTED | XPC_C_SETUP); /* clear all else */
67 dev_info(xpc_chan, "channel %d to partition %d connected\n",
68 ch->number, ch->partid);
70 spin_unlock_irqrestore(&ch->lock, *irq_flags);
71 xpc_create_kthreads(ch, 1, 0);
72 spin_lock_irqsave(&ch->lock, *irq_flags);
76 * spin_lock_irqsave() is expected to be held on entry.
79 xpc_process_disconnect(struct xpc_channel *ch, unsigned long *irq_flags)
81 struct xpc_partition *part = &xpc_partitions[ch->partid];
82 u32 channel_was_connected = (ch->flags & XPC_C_WASCONNECTED);
84 DBUG_ON(!spin_is_locked(&ch->lock));
86 if (!(ch->flags & XPC_C_DISCONNECTING))
89 DBUG_ON(!(ch->flags & XPC_C_CLOSEREQUEST));
91 /* make sure all activity has settled down first */
93 if (atomic_read(&ch->kthreads_assigned) > 0 ||
94 atomic_read(&ch->references) > 0) {
97 DBUG_ON((ch->flags & XPC_C_CONNECTEDCALLOUT_MADE) &&
98 !(ch->flags & XPC_C_DISCONNECTINGCALLOUT_MADE));
100 if (part->act_state == XPC_P_AS_DEACTIVATING) {
101 /* can't proceed until the other side disengages from us */
102 if (xpc_partition_engaged(ch->partid))
107 /* as long as the other side is up do the full protocol */
109 if (!(ch->flags & XPC_C_RCLOSEREQUEST))
112 if (!(ch->flags & XPC_C_CLOSEREPLY)) {
113 ch->flags |= XPC_C_CLOSEREPLY;
114 xpc_send_chctl_closereply(ch, irq_flags);
117 if (!(ch->flags & XPC_C_RCLOSEREPLY))
121 /* wake those waiting for notify completion */
122 if (atomic_read(&ch->n_to_notify) > 0) {
123 /* we do callout while holding ch->lock, callout can't block */
124 xpc_notify_senders_of_disconnect(ch);
127 /* both sides are disconnected now */
129 if (ch->flags & XPC_C_DISCONNECTINGCALLOUT_MADE) {
130 spin_unlock_irqrestore(&ch->lock, *irq_flags);
131 xpc_disconnect_callout(ch, xpDisconnected);
132 spin_lock_irqsave(&ch->lock, *irq_flags);
135 DBUG_ON(atomic_read(&ch->n_to_notify) != 0);
137 /* it's now safe to free the channel's message queues */
138 xpc_teardown_msg_structures(ch);
143 ch->local_nentries = 0;
144 ch->remote_nentries = 0;
145 ch->kthreads_assigned_limit = 0;
146 ch->kthreads_idle_limit = 0;
149 * Mark the channel disconnected and clear all other flags, including
150 * XPC_C_SETUP (because of call to xpc_teardown_msg_structures()) but
151 * not including XPC_C_WDISCONNECT (if it was set).
153 ch->flags = (XPC_C_DISCONNECTED | (ch->flags & XPC_C_WDISCONNECT));
155 atomic_dec(&part->nchannels_active);
157 if (channel_was_connected) {
158 dev_info(xpc_chan, "channel %d to partition %d disconnected, "
159 "reason=%d\n", ch->number, ch->partid, ch->reason);
162 if (ch->flags & XPC_C_WDISCONNECT) {
163 /* we won't lose the CPU since we're holding ch->lock */
164 complete(&ch->wdisconnect_wait);
165 } else if (ch->delayed_chctl_flags) {
166 if (part->act_state != XPC_P_AS_DEACTIVATING) {
167 /* time to take action on any delayed chctl flags */
168 spin_lock(&part->chctl_lock);
169 part->chctl.flags[ch->number] |=
170 ch->delayed_chctl_flags;
171 spin_unlock(&part->chctl_lock);
173 ch->delayed_chctl_flags = 0;
178 * Process a change in the channel's remote connection state.
181 xpc_process_openclose_chctl_flags(struct xpc_partition *part, int ch_number,
184 unsigned long irq_flags;
185 struct xpc_openclose_args *args =
186 &part->remote_openclose_args[ch_number];
187 struct xpc_channel *ch = &part->channels[ch_number];
188 enum xp_retval reason;
190 spin_lock_irqsave(&ch->lock, irq_flags);
194 if ((ch->flags & XPC_C_DISCONNECTED) &&
195 (ch->flags & XPC_C_WDISCONNECT)) {
197 * Delay processing chctl flags until thread waiting disconnect
198 * has had a chance to see that the channel is disconnected.
200 ch->delayed_chctl_flags |= chctl_flags;
201 spin_unlock_irqrestore(&ch->lock, irq_flags);
205 if (chctl_flags & XPC_CHCTL_CLOSEREQUEST) {
207 dev_dbg(xpc_chan, "XPC_CHCTL_CLOSEREQUEST (reason=%d) received "
208 "from partid=%d, channel=%d\n", args->reason,
209 ch->partid, ch->number);
212 * If RCLOSEREQUEST is set, we're probably waiting for
213 * RCLOSEREPLY. We should find it and a ROPENREQUEST packed
214 * with this RCLOSEREQUEST in the chctl_flags.
217 if (ch->flags & XPC_C_RCLOSEREQUEST) {
218 DBUG_ON(!(ch->flags & XPC_C_DISCONNECTING));
219 DBUG_ON(!(ch->flags & XPC_C_CLOSEREQUEST));
220 DBUG_ON(!(ch->flags & XPC_C_CLOSEREPLY));
221 DBUG_ON(ch->flags & XPC_C_RCLOSEREPLY);
223 DBUG_ON(!(chctl_flags & XPC_CHCTL_CLOSEREPLY));
224 chctl_flags &= ~XPC_CHCTL_CLOSEREPLY;
225 ch->flags |= XPC_C_RCLOSEREPLY;
227 /* both sides have finished disconnecting */
228 xpc_process_disconnect(ch, &irq_flags);
229 DBUG_ON(!(ch->flags & XPC_C_DISCONNECTED));
233 if (ch->flags & XPC_C_DISCONNECTED) {
234 if (!(chctl_flags & XPC_CHCTL_OPENREQUEST)) {
235 if (part->chctl.flags[ch_number] &
236 XPC_CHCTL_OPENREQUEST) {
238 DBUG_ON(ch->delayed_chctl_flags != 0);
239 spin_lock(&part->chctl_lock);
240 part->chctl.flags[ch_number] |=
241 XPC_CHCTL_CLOSEREQUEST;
242 spin_unlock(&part->chctl_lock);
244 spin_unlock_irqrestore(&ch->lock, irq_flags);
248 XPC_SET_REASON(ch, 0, 0);
249 ch->flags &= ~XPC_C_DISCONNECTED;
251 atomic_inc(&part->nchannels_active);
252 ch->flags |= (XPC_C_CONNECTING | XPC_C_ROPENREQUEST);
255 chctl_flags &= ~(XPC_CHCTL_OPENREQUEST | XPC_CHCTL_OPENREPLY);
258 * The meaningful CLOSEREQUEST connection state fields are:
259 * reason = reason connection is to be closed
262 ch->flags |= XPC_C_RCLOSEREQUEST;
264 if (!(ch->flags & XPC_C_DISCONNECTING)) {
265 reason = args->reason;
266 if (reason <= xpSuccess || reason > xpUnknownReason)
267 reason = xpUnknownReason;
268 else if (reason == xpUnregistering)
269 reason = xpOtherUnregistering;
271 XPC_DISCONNECT_CHANNEL(ch, reason, &irq_flags);
273 DBUG_ON(chctl_flags & XPC_CHCTL_CLOSEREPLY);
274 spin_unlock_irqrestore(&ch->lock, irq_flags);
278 xpc_process_disconnect(ch, &irq_flags);
281 if (chctl_flags & XPC_CHCTL_CLOSEREPLY) {
283 dev_dbg(xpc_chan, "XPC_CHCTL_CLOSEREPLY received from partid="
284 "%d, channel=%d\n", ch->partid, ch->number);
286 if (ch->flags & XPC_C_DISCONNECTED) {
287 DBUG_ON(part->act_state != XPC_P_AS_DEACTIVATING);
288 spin_unlock_irqrestore(&ch->lock, irq_flags);
292 DBUG_ON(!(ch->flags & XPC_C_CLOSEREQUEST));
294 if (!(ch->flags & XPC_C_RCLOSEREQUEST)) {
295 if (part->chctl.flags[ch_number] &
296 XPC_CHCTL_CLOSEREQUEST) {
298 DBUG_ON(ch->delayed_chctl_flags != 0);
299 spin_lock(&part->chctl_lock);
300 part->chctl.flags[ch_number] |=
301 XPC_CHCTL_CLOSEREPLY;
302 spin_unlock(&part->chctl_lock);
304 spin_unlock_irqrestore(&ch->lock, irq_flags);
308 ch->flags |= XPC_C_RCLOSEREPLY;
310 if (ch->flags & XPC_C_CLOSEREPLY) {
311 /* both sides have finished disconnecting */
312 xpc_process_disconnect(ch, &irq_flags);
316 if (chctl_flags & XPC_CHCTL_OPENREQUEST) {
318 dev_dbg(xpc_chan, "XPC_CHCTL_OPENREQUEST (entry_size=%d, "
319 "local_nentries=%d) received from partid=%d, "
320 "channel=%d\n", args->entry_size, args->local_nentries,
321 ch->partid, ch->number);
323 if (part->act_state == XPC_P_AS_DEACTIVATING ||
324 (ch->flags & XPC_C_ROPENREQUEST)) {
325 spin_unlock_irqrestore(&ch->lock, irq_flags);
329 if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_WDISCONNECT)) {
330 ch->delayed_chctl_flags |= XPC_CHCTL_OPENREQUEST;
331 spin_unlock_irqrestore(&ch->lock, irq_flags);
334 DBUG_ON(!(ch->flags & (XPC_C_DISCONNECTED |
335 XPC_C_OPENREQUEST)));
336 DBUG_ON(ch->flags & (XPC_C_ROPENREQUEST | XPC_C_ROPENREPLY |
337 XPC_C_OPENREPLY | XPC_C_CONNECTED));
340 * The meaningful OPENREQUEST connection state fields are:
341 * entry_size = size of channel's messages in bytes
342 * local_nentries = remote partition's local_nentries
344 if (args->entry_size == 0 || args->local_nentries == 0) {
345 /* assume OPENREQUEST was delayed by mistake */
346 spin_unlock_irqrestore(&ch->lock, irq_flags);
350 ch->flags |= (XPC_C_ROPENREQUEST | XPC_C_CONNECTING);
351 ch->remote_nentries = args->local_nentries;
353 if (ch->flags & XPC_C_OPENREQUEST) {
354 if (args->entry_size != ch->entry_size) {
355 XPC_DISCONNECT_CHANNEL(ch, xpUnequalMsgSizes,
357 spin_unlock_irqrestore(&ch->lock, irq_flags);
361 ch->entry_size = args->entry_size;
363 XPC_SET_REASON(ch, 0, 0);
364 ch->flags &= ~XPC_C_DISCONNECTED;
366 atomic_inc(&part->nchannels_active);
369 xpc_process_connect(ch, &irq_flags);
372 if (chctl_flags & XPC_CHCTL_OPENREPLY) {
374 dev_dbg(xpc_chan, "XPC_CHCTL_OPENREPLY (local_msgqueue_pa="
375 "0x%lx, local_nentries=%d, remote_nentries=%d) "
376 "received from partid=%d, channel=%d\n",
377 args->local_msgqueue_pa, args->local_nentries,
378 args->remote_nentries, ch->partid, ch->number);
380 if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_DISCONNECTED)) {
381 spin_unlock_irqrestore(&ch->lock, irq_flags);
384 if (!(ch->flags & XPC_C_OPENREQUEST)) {
385 XPC_DISCONNECT_CHANNEL(ch, xpOpenCloseError,
387 spin_unlock_irqrestore(&ch->lock, irq_flags);
391 DBUG_ON(!(ch->flags & XPC_C_ROPENREQUEST));
392 DBUG_ON(ch->flags & XPC_C_CONNECTED);
395 * The meaningful OPENREPLY connection state fields are:
396 * local_msgqueue_pa = physical address of remote
397 * partition's local_msgqueue
398 * local_nentries = remote partition's local_nentries
399 * remote_nentries = remote partition's remote_nentries
401 DBUG_ON(args->local_msgqueue_pa == 0);
402 DBUG_ON(args->local_nentries == 0);
403 DBUG_ON(args->remote_nentries == 0);
405 ch->flags |= XPC_C_ROPENREPLY;
406 xpc_save_remote_msgqueue_pa(ch, args->local_msgqueue_pa);
408 if (args->local_nentries < ch->remote_nentries) {
409 dev_dbg(xpc_chan, "XPC_CHCTL_OPENREPLY: new "
410 "remote_nentries=%d, old remote_nentries=%d, "
411 "partid=%d, channel=%d\n",
412 args->local_nentries, ch->remote_nentries,
413 ch->partid, ch->number);
415 ch->remote_nentries = args->local_nentries;
417 if (args->remote_nentries < ch->local_nentries) {
418 dev_dbg(xpc_chan, "XPC_CHCTL_OPENREPLY: new "
419 "local_nentries=%d, old local_nentries=%d, "
420 "partid=%d, channel=%d\n",
421 args->remote_nentries, ch->local_nentries,
422 ch->partid, ch->number);
424 ch->local_nentries = args->remote_nentries;
427 xpc_process_connect(ch, &irq_flags);
430 spin_unlock_irqrestore(&ch->lock, irq_flags);
434 * Attempt to establish a channel connection to a remote partition.
436 static enum xp_retval
437 xpc_connect_channel(struct xpc_channel *ch)
439 unsigned long irq_flags;
440 struct xpc_registration *registration = &xpc_registrations[ch->number];
442 if (mutex_trylock(®istration->mutex) == 0)
445 if (!XPC_CHANNEL_REGISTERED(ch->number)) {
446 mutex_unlock(®istration->mutex);
447 return xpUnregistered;
450 spin_lock_irqsave(&ch->lock, irq_flags);
452 DBUG_ON(ch->flags & XPC_C_CONNECTED);
453 DBUG_ON(ch->flags & XPC_C_OPENREQUEST);
455 if (ch->flags & XPC_C_DISCONNECTING) {
456 spin_unlock_irqrestore(&ch->lock, irq_flags);
457 mutex_unlock(®istration->mutex);
461 /* add info from the channel connect registration to the channel */
463 ch->kthreads_assigned_limit = registration->assigned_limit;
464 ch->kthreads_idle_limit = registration->idle_limit;
465 DBUG_ON(atomic_read(&ch->kthreads_assigned) != 0);
466 DBUG_ON(atomic_read(&ch->kthreads_idle) != 0);
467 DBUG_ON(atomic_read(&ch->kthreads_active) != 0);
469 ch->func = registration->func;
470 DBUG_ON(registration->func == NULL);
471 ch->key = registration->key;
473 ch->local_nentries = registration->nentries;
475 if (ch->flags & XPC_C_ROPENREQUEST) {
476 if (registration->entry_size != ch->entry_size) {
477 /* the local and remote sides aren't the same */
480 * Because XPC_DISCONNECT_CHANNEL() can block we're
481 * forced to up the registration sema before we unlock
482 * the channel lock. But that's okay here because we're
483 * done with the part that required the registration
484 * sema. XPC_DISCONNECT_CHANNEL() requires that the
485 * channel lock be locked and will unlock and relock
486 * the channel lock as needed.
488 mutex_unlock(®istration->mutex);
489 XPC_DISCONNECT_CHANNEL(ch, xpUnequalMsgSizes,
491 spin_unlock_irqrestore(&ch->lock, irq_flags);
492 return xpUnequalMsgSizes;
495 ch->entry_size = registration->entry_size;
497 XPC_SET_REASON(ch, 0, 0);
498 ch->flags &= ~XPC_C_DISCONNECTED;
500 atomic_inc(&xpc_partitions[ch->partid].nchannels_active);
503 mutex_unlock(®istration->mutex);
505 /* initiate the connection */
507 ch->flags |= (XPC_C_OPENREQUEST | XPC_C_CONNECTING);
508 xpc_send_chctl_openrequest(ch, &irq_flags);
510 xpc_process_connect(ch, &irq_flags);
512 spin_unlock_irqrestore(&ch->lock, irq_flags);
518 xpc_process_sent_chctl_flags(struct xpc_partition *part)
520 unsigned long irq_flags;
521 union xpc_channel_ctl_flags chctl;
522 struct xpc_channel *ch;
526 chctl.all_flags = xpc_get_chctl_all_flags(part);
529 * Initiate channel connections for registered channels.
531 * For each connected channel that has pending messages activate idle
532 * kthreads and/or create new kthreads as needed.
535 for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
536 ch = &part->channels[ch_number];
539 * Process any open or close related chctl flags, and then deal
540 * with connecting or disconnecting the channel as required.
543 if (chctl.flags[ch_number] & XPC_OPENCLOSE_CHCTL_FLAGS) {
544 xpc_process_openclose_chctl_flags(part, ch_number,
545 chctl.flags[ch_number]);
548 ch_flags = ch->flags; /* need an atomic snapshot of flags */
550 if (ch_flags & XPC_C_DISCONNECTING) {
551 spin_lock_irqsave(&ch->lock, irq_flags);
552 xpc_process_disconnect(ch, &irq_flags);
553 spin_unlock_irqrestore(&ch->lock, irq_flags);
557 if (part->act_state == XPC_P_AS_DEACTIVATING)
560 if (!(ch_flags & XPC_C_CONNECTED)) {
561 if (!(ch_flags & XPC_C_OPENREQUEST)) {
562 DBUG_ON(ch_flags & XPC_C_SETUP);
563 (void)xpc_connect_channel(ch);
565 spin_lock_irqsave(&ch->lock, irq_flags);
566 xpc_process_connect(ch, &irq_flags);
567 spin_unlock_irqrestore(&ch->lock, irq_flags);
573 * Process any message related chctl flags, this may involve
574 * the activation of kthreads to deliver any pending messages
575 * sent from the other partition.
578 if (chctl.flags[ch_number] & XPC_MSG_CHCTL_FLAGS)
579 xpc_process_msg_chctl_flags(part, ch_number);
584 * XPC's heartbeat code calls this function to inform XPC that a partition is
585 * going down. XPC responds by tearing down the XPartition Communication
586 * infrastructure used for the just downed partition.
588 * XPC's heartbeat code will never call this function and xpc_partition_up()
589 * at the same time. Nor will it ever make multiple calls to either function
593 xpc_partition_going_down(struct xpc_partition *part, enum xp_retval reason)
595 unsigned long irq_flags;
597 struct xpc_channel *ch;
599 dev_dbg(xpc_chan, "deactivating partition %d, reason=%d\n",
600 XPC_PARTID(part), reason);
602 if (!xpc_part_ref(part)) {
603 /* infrastructure for this partition isn't currently set up */
607 /* disconnect channels associated with the partition going down */
609 for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
610 ch = &part->channels[ch_number];
612 xpc_msgqueue_ref(ch);
613 spin_lock_irqsave(&ch->lock, irq_flags);
615 XPC_DISCONNECT_CHANNEL(ch, reason, &irq_flags);
617 spin_unlock_irqrestore(&ch->lock, irq_flags);
618 xpc_msgqueue_deref(ch);
621 xpc_wakeup_channel_mgr(part);
623 xpc_part_deref(part);
627 * Called by XP at the time of channel connection registration to cause
628 * XPC to establish connections to all currently active partitions.
631 xpc_initiate_connect(int ch_number)
634 struct xpc_partition *part;
635 struct xpc_channel *ch;
637 DBUG_ON(ch_number < 0 || ch_number >= XPC_MAX_NCHANNELS);
639 for (partid = 0; partid < xp_max_npartitions; partid++) {
640 part = &xpc_partitions[partid];
642 if (xpc_part_ref(part)) {
643 ch = &part->channels[ch_number];
646 * Initiate the establishment of a connection on the
647 * newly registered channel to the remote partition.
649 xpc_wakeup_channel_mgr(part);
650 xpc_part_deref(part);
656 xpc_connected_callout(struct xpc_channel *ch)
658 /* let the registerer know that a connection has been established */
660 if (ch->func != NULL) {
661 dev_dbg(xpc_chan, "ch->func() called, reason=xpConnected, "
662 "partid=%d, channel=%d\n", ch->partid, ch->number);
664 ch->func(xpConnected, ch->partid, ch->number,
665 (void *)(u64)ch->local_nentries, ch->key);
667 dev_dbg(xpc_chan, "ch->func() returned, reason=xpConnected, "
668 "partid=%d, channel=%d\n", ch->partid, ch->number);
673 * Called by XP at the time of channel connection unregistration to cause
674 * XPC to teardown all current connections for the specified channel.
676 * Before returning xpc_initiate_disconnect() will wait until all connections
677 * on the specified channel have been closed/torndown. So the caller can be
678 * assured that they will not be receiving any more callouts from XPC to the
679 * function they registered via xpc_connect().
683 * ch_number - channel # to unregister.
686 xpc_initiate_disconnect(int ch_number)
688 unsigned long irq_flags;
690 struct xpc_partition *part;
691 struct xpc_channel *ch;
693 DBUG_ON(ch_number < 0 || ch_number >= XPC_MAX_NCHANNELS);
695 /* initiate the channel disconnect for every active partition */
696 for (partid = 0; partid < xp_max_npartitions; partid++) {
697 part = &xpc_partitions[partid];
699 if (xpc_part_ref(part)) {
700 ch = &part->channels[ch_number];
701 xpc_msgqueue_ref(ch);
703 spin_lock_irqsave(&ch->lock, irq_flags);
705 if (!(ch->flags & XPC_C_DISCONNECTED)) {
706 ch->flags |= XPC_C_WDISCONNECT;
708 XPC_DISCONNECT_CHANNEL(ch, xpUnregistering,
712 spin_unlock_irqrestore(&ch->lock, irq_flags);
714 xpc_msgqueue_deref(ch);
715 xpc_part_deref(part);
719 xpc_disconnect_wait(ch_number);
723 * To disconnect a channel, and reflect it back to all who may be waiting.
725 * An OPEN is not allowed until XPC_C_DISCONNECTING is cleared by
726 * xpc_process_disconnect(), and if set, XPC_C_WDISCONNECT is cleared by
727 * xpc_disconnect_wait().
729 * THE CHANNEL IS TO BE LOCKED BY THE CALLER AND WILL REMAIN LOCKED UPON RETURN.
732 xpc_disconnect_channel(const int line, struct xpc_channel *ch,
733 enum xp_retval reason, unsigned long *irq_flags)
735 u32 channel_was_connected = (ch->flags & XPC_C_CONNECTED);
737 DBUG_ON(!spin_is_locked(&ch->lock));
739 if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_DISCONNECTED))
742 DBUG_ON(!(ch->flags & (XPC_C_CONNECTING | XPC_C_CONNECTED)));
744 dev_dbg(xpc_chan, "reason=%d, line=%d, partid=%d, channel=%d\n",
745 reason, line, ch->partid, ch->number);
747 XPC_SET_REASON(ch, reason, line);
749 ch->flags |= (XPC_C_CLOSEREQUEST | XPC_C_DISCONNECTING);
750 /* some of these may not have been set */
751 ch->flags &= ~(XPC_C_OPENREQUEST | XPC_C_OPENREPLY |
752 XPC_C_ROPENREQUEST | XPC_C_ROPENREPLY |
753 XPC_C_CONNECTING | XPC_C_CONNECTED);
755 xpc_send_chctl_closerequest(ch, irq_flags);
757 if (channel_was_connected)
758 ch->flags |= XPC_C_WASCONNECTED;
760 spin_unlock_irqrestore(&ch->lock, *irq_flags);
762 /* wake all idle kthreads so they can exit */
763 if (atomic_read(&ch->kthreads_idle) > 0) {
764 wake_up_all(&ch->idle_wq);
766 } else if ((ch->flags & XPC_C_CONNECTEDCALLOUT_MADE) &&
767 !(ch->flags & XPC_C_DISCONNECTINGCALLOUT)) {
768 /* start a kthread that will do the xpDisconnecting callout */
769 xpc_create_kthreads(ch, 1, 1);
772 /* wake those waiting to allocate an entry from the local msg queue */
773 if (atomic_read(&ch->n_on_msg_allocate_wq) > 0)
774 wake_up(&ch->msg_allocate_wq);
776 spin_lock_irqsave(&ch->lock, *irq_flags);
780 xpc_disconnect_callout(struct xpc_channel *ch, enum xp_retval reason)
783 * Let the channel's registerer know that the channel is being
784 * disconnected. We don't want to do this if the registerer was never
785 * informed of a connection being made.
788 if (ch->func != NULL) {
789 dev_dbg(xpc_chan, "ch->func() called, reason=%d, partid=%d, "
790 "channel=%d\n", reason, ch->partid, ch->number);
792 ch->func(reason, ch->partid, ch->number, NULL, ch->key);
794 dev_dbg(xpc_chan, "ch->func() returned, reason=%d, partid=%d, "
795 "channel=%d\n", reason, ch->partid, ch->number);
800 * Wait for a message entry to become available for the specified channel,
801 * but don't wait any longer than 1 jiffy.
804 xpc_allocate_msg_wait(struct xpc_channel *ch)
808 if (ch->flags & XPC_C_DISCONNECTING) {
809 DBUG_ON(ch->reason == xpInterrupted);
813 atomic_inc(&ch->n_on_msg_allocate_wq);
814 ret = interruptible_sleep_on_timeout(&ch->msg_allocate_wq, 1);
815 atomic_dec(&ch->n_on_msg_allocate_wq);
817 if (ch->flags & XPC_C_DISCONNECTING) {
819 DBUG_ON(ch->reason == xpInterrupted);
820 } else if (ret == 0) {
830 * Send a message that contains the user's payload on the specified channel
831 * connected to the specified partition.
833 * NOTE that this routine can sleep waiting for a message entry to become
834 * available. To not sleep, pass in the XPC_NOWAIT flag.
836 * Once sent, this routine will not wait for the message to be received, nor
837 * will notification be given when it does happen.
841 * partid - ID of partition to which the channel is connected.
842 * ch_number - channel # to send message on.
843 * flags - see xp.h for valid flags.
844 * payload - pointer to the payload which is to be sent.
845 * payload_size - size of the payload in bytes.
848 xpc_initiate_send(short partid, int ch_number, u32 flags, void *payload,
851 struct xpc_partition *part = &xpc_partitions[partid];
852 enum xp_retval ret = xpUnknownReason;
854 dev_dbg(xpc_chan, "payload=0x%p, partid=%d, channel=%d\n", payload,
857 DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
858 DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
859 DBUG_ON(payload == NULL);
861 if (xpc_part_ref(part)) {
862 ret = xpc_send_payload(&part->channels[ch_number], flags,
863 payload, payload_size, 0, NULL, NULL);
864 xpc_part_deref(part);
871 * Send a message that contains the user's payload on the specified channel
872 * connected to the specified partition.
874 * NOTE that this routine can sleep waiting for a message entry to become
875 * available. To not sleep, pass in the XPC_NOWAIT flag.
877 * This routine will not wait for the message to be sent or received.
879 * Once the remote end of the channel has received the message, the function
880 * passed as an argument to xpc_initiate_send_notify() will be called. This
881 * allows the sender to free up or re-use any buffers referenced by the
882 * message, but does NOT mean the message has been processed at the remote
885 * If this routine returns an error, the caller's function will NOT be called.
889 * partid - ID of partition to which the channel is connected.
890 * ch_number - channel # to send message on.
891 * flags - see xp.h for valid flags.
892 * payload - pointer to the payload which is to be sent.
893 * payload_size - size of the payload in bytes.
894 * func - function to call with asynchronous notification of message
895 * receipt. THIS FUNCTION MUST BE NON-BLOCKING.
896 * key - user-defined key to be passed to the function when it's called.
899 xpc_initiate_send_notify(short partid, int ch_number, u32 flags, void *payload,
900 u16 payload_size, xpc_notify_func func, void *key)
902 struct xpc_partition *part = &xpc_partitions[partid];
903 enum xp_retval ret = xpUnknownReason;
905 dev_dbg(xpc_chan, "payload=0x%p, partid=%d, channel=%d\n", payload,
908 DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
909 DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
910 DBUG_ON(payload == NULL);
911 DBUG_ON(func == NULL);
913 if (xpc_part_ref(part)) {
914 ret = xpc_send_payload(&part->channels[ch_number], flags,
915 payload, payload_size, XPC_N_CALL, func,
917 xpc_part_deref(part);
923 * Deliver a message's payload to its intended recipient.
926 xpc_deliver_payload(struct xpc_channel *ch)
930 payload = xpc_get_deliverable_payload(ch);
931 if (payload != NULL) {
934 * This ref is taken to protect the payload itself from being
935 * freed before the user is finished with it, which the user
936 * indicates by calling xpc_initiate_received().
938 xpc_msgqueue_ref(ch);
940 atomic_inc(&ch->kthreads_active);
942 if (ch->func != NULL) {
943 dev_dbg(xpc_chan, "ch->func() called, payload=0x%p "
944 "partid=%d channel=%d\n", payload, ch->partid,
947 /* deliver the message to its intended recipient */
948 ch->func(xpMsgReceived, ch->partid, ch->number, payload,
951 dev_dbg(xpc_chan, "ch->func() returned, payload=0x%p "
952 "partid=%d channel=%d\n", payload, ch->partid,
956 atomic_dec(&ch->kthreads_active);
961 * Acknowledge receipt of a delivered message's payload.
963 * This function, although called by users, does not call xpc_part_ref() to
964 * ensure that the partition infrastructure is in place. It relies on the
965 * fact that we called xpc_msgqueue_ref() in xpc_deliver_payload().
969 * partid - ID of partition to which the channel is connected.
970 * ch_number - channel # message received on.
971 * payload - pointer to the payload area allocated via
972 * xpc_initiate_send() or xpc_initiate_send_notify().
975 xpc_initiate_received(short partid, int ch_number, void *payload)
977 struct xpc_partition *part = &xpc_partitions[partid];
978 struct xpc_channel *ch;
980 DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
981 DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
983 ch = &part->channels[ch_number];
984 xpc_received_payload(ch, payload);
986 /* the call to xpc_msgqueue_ref() was done by xpc_deliver_payload() */
987 xpc_msgqueue_deref(ch);