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-2009 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_DISCONNECTING)
54 if (!(ch->flags & XPC_C_OPENREPLY)) {
55 ch->flags |= XPC_C_OPENREPLY;
56 xpc_send_chctl_openreply(ch, irq_flags);
59 if (!(ch->flags & XPC_C_ROPENREPLY))
62 if (!(ch->flags & XPC_C_OPENCOMPLETE)) {
63 ch->flags |= (XPC_C_OPENCOMPLETE | XPC_C_CONNECTED);
64 xpc_send_chctl_opencomplete(ch, irq_flags);
67 if (!(ch->flags & XPC_C_ROPENCOMPLETE))
70 dev_info(xpc_chan, "channel %d to partition %d connected\n",
71 ch->number, ch->partid);
73 ch->flags = (XPC_C_CONNECTED | XPC_C_SETUP); /* clear all else */
77 * spin_lock_irqsave() is expected to be held on entry.
80 xpc_process_disconnect(struct xpc_channel *ch, unsigned long *irq_flags)
82 struct xpc_partition *part = &xpc_partitions[ch->partid];
83 u32 channel_was_connected = (ch->flags & XPC_C_WASCONNECTED);
85 DBUG_ON(!spin_is_locked(&ch->lock));
87 if (!(ch->flags & XPC_C_DISCONNECTING))
90 DBUG_ON(!(ch->flags & XPC_C_CLOSEREQUEST));
92 /* make sure all activity has settled down first */
94 if (atomic_read(&ch->kthreads_assigned) > 0 ||
95 atomic_read(&ch->references) > 0) {
98 DBUG_ON((ch->flags & XPC_C_CONNECTEDCALLOUT_MADE) &&
99 !(ch->flags & XPC_C_DISCONNECTINGCALLOUT_MADE));
101 if (part->act_state == XPC_P_AS_DEACTIVATING) {
102 /* can't proceed until the other side disengages from us */
103 if (xpc_partition_engaged(ch->partid))
108 /* as long as the other side is up do the full protocol */
110 if (!(ch->flags & XPC_C_RCLOSEREQUEST))
113 if (!(ch->flags & XPC_C_CLOSEREPLY)) {
114 ch->flags |= XPC_C_CLOSEREPLY;
115 xpc_send_chctl_closereply(ch, irq_flags);
118 if (!(ch->flags & XPC_C_RCLOSEREPLY))
122 /* wake those waiting for notify completion */
123 if (atomic_read(&ch->n_to_notify) > 0) {
124 /* we do callout while holding ch->lock, callout can't block */
125 xpc_notify_senders_of_disconnect(ch);
128 /* both sides are disconnected now */
130 if (ch->flags & XPC_C_DISCONNECTINGCALLOUT_MADE) {
131 spin_unlock_irqrestore(&ch->lock, *irq_flags);
132 xpc_disconnect_callout(ch, xpDisconnected);
133 spin_lock_irqsave(&ch->lock, *irq_flags);
136 DBUG_ON(atomic_read(&ch->n_to_notify) != 0);
138 /* it's now safe to free the channel's message queues */
139 xpc_teardown_msg_structures(ch);
144 ch->local_nentries = 0;
145 ch->remote_nentries = 0;
146 ch->kthreads_assigned_limit = 0;
147 ch->kthreads_idle_limit = 0;
150 * Mark the channel disconnected and clear all other flags, including
151 * XPC_C_SETUP (because of call to xpc_teardown_msg_structures()) but
152 * not including XPC_C_WDISCONNECT (if it was set).
154 ch->flags = (XPC_C_DISCONNECTED | (ch->flags & XPC_C_WDISCONNECT));
156 atomic_dec(&part->nchannels_active);
158 if (channel_was_connected) {
159 dev_info(xpc_chan, "channel %d to partition %d disconnected, "
160 "reason=%d\n", ch->number, ch->partid, ch->reason);
163 if (ch->flags & XPC_C_WDISCONNECT) {
164 /* we won't lose the CPU since we're holding ch->lock */
165 complete(&ch->wdisconnect_wait);
166 } else if (ch->delayed_chctl_flags) {
167 if (part->act_state != XPC_P_AS_DEACTIVATING) {
168 /* time to take action on any delayed chctl flags */
169 spin_lock(&part->chctl_lock);
170 part->chctl.flags[ch->number] |=
171 ch->delayed_chctl_flags;
172 spin_unlock(&part->chctl_lock);
174 ch->delayed_chctl_flags = 0;
179 * Process a change in the channel's remote connection state.
182 xpc_process_openclose_chctl_flags(struct xpc_partition *part, int ch_number,
185 unsigned long irq_flags;
186 struct xpc_openclose_args *args =
187 &part->remote_openclose_args[ch_number];
188 struct xpc_channel *ch = &part->channels[ch_number];
189 enum xp_retval reason;
191 int create_kthread = 0;
193 spin_lock_irqsave(&ch->lock, irq_flags);
197 if ((ch->flags & XPC_C_DISCONNECTED) &&
198 (ch->flags & XPC_C_WDISCONNECT)) {
200 * Delay processing chctl flags until thread waiting disconnect
201 * has had a chance to see that the channel is disconnected.
203 ch->delayed_chctl_flags |= chctl_flags;
207 if (chctl_flags & XPC_CHCTL_CLOSEREQUEST) {
209 dev_dbg(xpc_chan, "XPC_CHCTL_CLOSEREQUEST (reason=%d) received "
210 "from partid=%d, channel=%d\n", args->reason,
211 ch->partid, ch->number);
214 * If RCLOSEREQUEST is set, we're probably waiting for
215 * RCLOSEREPLY. We should find it and a ROPENREQUEST packed
216 * with this RCLOSEREQUEST in the chctl_flags.
219 if (ch->flags & XPC_C_RCLOSEREQUEST) {
220 DBUG_ON(!(ch->flags & XPC_C_DISCONNECTING));
221 DBUG_ON(!(ch->flags & XPC_C_CLOSEREQUEST));
222 DBUG_ON(!(ch->flags & XPC_C_CLOSEREPLY));
223 DBUG_ON(ch->flags & XPC_C_RCLOSEREPLY);
225 DBUG_ON(!(chctl_flags & XPC_CHCTL_CLOSEREPLY));
226 chctl_flags &= ~XPC_CHCTL_CLOSEREPLY;
227 ch->flags |= XPC_C_RCLOSEREPLY;
229 /* both sides have finished disconnecting */
230 xpc_process_disconnect(ch, &irq_flags);
231 DBUG_ON(!(ch->flags & XPC_C_DISCONNECTED));
235 if (ch->flags & XPC_C_DISCONNECTED) {
236 if (!(chctl_flags & XPC_CHCTL_OPENREQUEST)) {
237 if (part->chctl.flags[ch_number] &
238 XPC_CHCTL_OPENREQUEST) {
240 DBUG_ON(ch->delayed_chctl_flags != 0);
241 spin_lock(&part->chctl_lock);
242 part->chctl.flags[ch_number] |=
243 XPC_CHCTL_CLOSEREQUEST;
244 spin_unlock(&part->chctl_lock);
249 XPC_SET_REASON(ch, 0, 0);
250 ch->flags &= ~XPC_C_DISCONNECTED;
252 atomic_inc(&part->nchannels_active);
253 ch->flags |= (XPC_C_CONNECTING | XPC_C_ROPENREQUEST);
256 chctl_flags &= ~(XPC_CHCTL_OPENREQUEST | XPC_CHCTL_OPENREPLY |
257 XPC_CHCTL_OPENCOMPLETE);
260 * The meaningful CLOSEREQUEST connection state fields are:
261 * reason = reason connection is to be closed
264 ch->flags |= XPC_C_RCLOSEREQUEST;
266 if (!(ch->flags & XPC_C_DISCONNECTING)) {
267 reason = args->reason;
268 if (reason <= xpSuccess || reason > xpUnknownReason)
269 reason = xpUnknownReason;
270 else if (reason == xpUnregistering)
271 reason = xpOtherUnregistering;
273 XPC_DISCONNECT_CHANNEL(ch, reason, &irq_flags);
275 DBUG_ON(chctl_flags & XPC_CHCTL_CLOSEREPLY);
279 xpc_process_disconnect(ch, &irq_flags);
282 if (chctl_flags & XPC_CHCTL_CLOSEREPLY) {
284 dev_dbg(xpc_chan, "XPC_CHCTL_CLOSEREPLY received from partid="
285 "%d, channel=%d\n", ch->partid, ch->number);
287 if (ch->flags & XPC_C_DISCONNECTED) {
288 DBUG_ON(part->act_state != XPC_P_AS_DEACTIVATING);
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);
307 ch->flags |= XPC_C_RCLOSEREPLY;
309 if (ch->flags & XPC_C_CLOSEREPLY) {
310 /* both sides have finished disconnecting */
311 xpc_process_disconnect(ch, &irq_flags);
315 if (chctl_flags & XPC_CHCTL_OPENREQUEST) {
317 dev_dbg(xpc_chan, "XPC_CHCTL_OPENREQUEST (entry_size=%d, "
318 "local_nentries=%d) received from partid=%d, "
319 "channel=%d\n", args->entry_size, args->local_nentries,
320 ch->partid, ch->number);
322 if (part->act_state == XPC_P_AS_DEACTIVATING ||
323 (ch->flags & XPC_C_ROPENREQUEST)) {
327 if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_WDISCONNECT)) {
328 ch->delayed_chctl_flags |= XPC_CHCTL_OPENREQUEST;
331 DBUG_ON(!(ch->flags & (XPC_C_DISCONNECTED |
332 XPC_C_OPENREQUEST)));
333 DBUG_ON(ch->flags & (XPC_C_ROPENREQUEST | XPC_C_ROPENREPLY |
334 XPC_C_OPENREPLY | XPC_C_CONNECTED));
337 * The meaningful OPENREQUEST connection state fields are:
338 * entry_size = size of channel's messages in bytes
339 * local_nentries = remote partition's local_nentries
341 if (args->entry_size == 0 || args->local_nentries == 0) {
342 /* assume OPENREQUEST was delayed by mistake */
346 ch->flags |= (XPC_C_ROPENREQUEST | XPC_C_CONNECTING);
347 ch->remote_nentries = args->local_nentries;
349 if (ch->flags & XPC_C_OPENREQUEST) {
350 if (args->entry_size != ch->entry_size) {
351 XPC_DISCONNECT_CHANNEL(ch, xpUnequalMsgSizes,
356 ch->entry_size = args->entry_size;
358 XPC_SET_REASON(ch, 0, 0);
359 ch->flags &= ~XPC_C_DISCONNECTED;
361 atomic_inc(&part->nchannels_active);
364 xpc_process_connect(ch, &irq_flags);
367 if (chctl_flags & XPC_CHCTL_OPENREPLY) {
369 dev_dbg(xpc_chan, "XPC_CHCTL_OPENREPLY (local_msgqueue_pa="
370 "0x%lx, local_nentries=%d, remote_nentries=%d) "
371 "received from partid=%d, channel=%d\n",
372 args->local_msgqueue_pa, args->local_nentries,
373 args->remote_nentries, ch->partid, ch->number);
375 if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_DISCONNECTED))
378 if (!(ch->flags & XPC_C_OPENREQUEST)) {
379 XPC_DISCONNECT_CHANNEL(ch, xpOpenCloseError,
384 DBUG_ON(!(ch->flags & XPC_C_ROPENREQUEST));
385 DBUG_ON(ch->flags & XPC_C_CONNECTED);
388 * The meaningful OPENREPLY connection state fields are:
389 * local_msgqueue_pa = physical address of remote
390 * partition's local_msgqueue
391 * local_nentries = remote partition's local_nentries
392 * remote_nentries = remote partition's remote_nentries
394 DBUG_ON(args->local_msgqueue_pa == 0);
395 DBUG_ON(args->local_nentries == 0);
396 DBUG_ON(args->remote_nentries == 0);
398 ret = xpc_save_remote_msgqueue_pa(ch, args->local_msgqueue_pa);
399 if (ret != xpSuccess) {
400 XPC_DISCONNECT_CHANNEL(ch, ret, &irq_flags);
403 ch->flags |= XPC_C_ROPENREPLY;
405 if (args->local_nentries < ch->remote_nentries) {
406 dev_dbg(xpc_chan, "XPC_CHCTL_OPENREPLY: new "
407 "remote_nentries=%d, old remote_nentries=%d, "
408 "partid=%d, channel=%d\n",
409 args->local_nentries, ch->remote_nentries,
410 ch->partid, ch->number);
412 ch->remote_nentries = args->local_nentries;
414 if (args->remote_nentries < ch->local_nentries) {
415 dev_dbg(xpc_chan, "XPC_CHCTL_OPENREPLY: new "
416 "local_nentries=%d, old local_nentries=%d, "
417 "partid=%d, channel=%d\n",
418 args->remote_nentries, ch->local_nentries,
419 ch->partid, ch->number);
421 ch->local_nentries = args->remote_nentries;
424 xpc_process_connect(ch, &irq_flags);
427 if (chctl_flags & XPC_CHCTL_OPENCOMPLETE) {
429 dev_dbg(xpc_chan, "XPC_CHCTL_OPENCOMPLETE received from "
430 "partid=%d, channel=%d\n", ch->partid, ch->number);
432 if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_DISCONNECTED))
435 if (!(ch->flags & XPC_C_OPENREQUEST) ||
436 !(ch->flags & XPC_C_OPENREPLY)) {
437 XPC_DISCONNECT_CHANNEL(ch, xpOpenCloseError,
442 DBUG_ON(!(ch->flags & XPC_C_ROPENREQUEST));
443 DBUG_ON(!(ch->flags & XPC_C_ROPENREPLY));
444 DBUG_ON(!(ch->flags & XPC_C_CONNECTED));
446 ch->flags |= XPC_C_ROPENCOMPLETE;
448 xpc_process_connect(ch, &irq_flags);
453 spin_unlock_irqrestore(&ch->lock, irq_flags);
456 xpc_create_kthreads(ch, 1, 0);
460 * Attempt to establish a channel connection to a remote partition.
462 static enum xp_retval
463 xpc_connect_channel(struct xpc_channel *ch)
465 unsigned long irq_flags;
466 struct xpc_registration *registration = &xpc_registrations[ch->number];
468 if (mutex_trylock(®istration->mutex) == 0)
471 if (!XPC_CHANNEL_REGISTERED(ch->number)) {
472 mutex_unlock(®istration->mutex);
473 return xpUnregistered;
476 spin_lock_irqsave(&ch->lock, irq_flags);
478 DBUG_ON(ch->flags & XPC_C_CONNECTED);
479 DBUG_ON(ch->flags & XPC_C_OPENREQUEST);
481 if (ch->flags & XPC_C_DISCONNECTING) {
482 spin_unlock_irqrestore(&ch->lock, irq_flags);
483 mutex_unlock(®istration->mutex);
487 /* add info from the channel connect registration to the channel */
489 ch->kthreads_assigned_limit = registration->assigned_limit;
490 ch->kthreads_idle_limit = registration->idle_limit;
491 DBUG_ON(atomic_read(&ch->kthreads_assigned) != 0);
492 DBUG_ON(atomic_read(&ch->kthreads_idle) != 0);
493 DBUG_ON(atomic_read(&ch->kthreads_active) != 0);
495 ch->func = registration->func;
496 DBUG_ON(registration->func == NULL);
497 ch->key = registration->key;
499 ch->local_nentries = registration->nentries;
501 if (ch->flags & XPC_C_ROPENREQUEST) {
502 if (registration->entry_size != ch->entry_size) {
503 /* the local and remote sides aren't the same */
506 * Because XPC_DISCONNECT_CHANNEL() can block we're
507 * forced to up the registration sema before we unlock
508 * the channel lock. But that's okay here because we're
509 * done with the part that required the registration
510 * sema. XPC_DISCONNECT_CHANNEL() requires that the
511 * channel lock be locked and will unlock and relock
512 * the channel lock as needed.
514 mutex_unlock(®istration->mutex);
515 XPC_DISCONNECT_CHANNEL(ch, xpUnequalMsgSizes,
517 spin_unlock_irqrestore(&ch->lock, irq_flags);
518 return xpUnequalMsgSizes;
521 ch->entry_size = registration->entry_size;
523 XPC_SET_REASON(ch, 0, 0);
524 ch->flags &= ~XPC_C_DISCONNECTED;
526 atomic_inc(&xpc_partitions[ch->partid].nchannels_active);
529 mutex_unlock(®istration->mutex);
531 /* initiate the connection */
533 ch->flags |= (XPC_C_OPENREQUEST | XPC_C_CONNECTING);
534 xpc_send_chctl_openrequest(ch, &irq_flags);
536 xpc_process_connect(ch, &irq_flags);
538 spin_unlock_irqrestore(&ch->lock, irq_flags);
544 xpc_process_sent_chctl_flags(struct xpc_partition *part)
546 unsigned long irq_flags;
547 union xpc_channel_ctl_flags chctl;
548 struct xpc_channel *ch;
552 chctl.all_flags = xpc_get_chctl_all_flags(part);
555 * Initiate channel connections for registered channels.
557 * For each connected channel that has pending messages activate idle
558 * kthreads and/or create new kthreads as needed.
561 for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
562 ch = &part->channels[ch_number];
565 * Process any open or close related chctl flags, and then deal
566 * with connecting or disconnecting the channel as required.
569 if (chctl.flags[ch_number] & XPC_OPENCLOSE_CHCTL_FLAGS) {
570 xpc_process_openclose_chctl_flags(part, ch_number,
571 chctl.flags[ch_number]);
574 ch_flags = ch->flags; /* need an atomic snapshot of flags */
576 if (ch_flags & XPC_C_DISCONNECTING) {
577 spin_lock_irqsave(&ch->lock, irq_flags);
578 xpc_process_disconnect(ch, &irq_flags);
579 spin_unlock_irqrestore(&ch->lock, irq_flags);
583 if (part->act_state == XPC_P_AS_DEACTIVATING)
586 if (!(ch_flags & XPC_C_CONNECTED)) {
587 if (!(ch_flags & XPC_C_OPENREQUEST)) {
588 DBUG_ON(ch_flags & XPC_C_SETUP);
589 (void)xpc_connect_channel(ch);
595 * Process any message related chctl flags, this may involve
596 * the activation of kthreads to deliver any pending messages
597 * sent from the other partition.
600 if (chctl.flags[ch_number] & XPC_MSG_CHCTL_FLAGS)
601 xpc_process_msg_chctl_flags(part, ch_number);
606 * XPC's heartbeat code calls this function to inform XPC that a partition is
607 * going down. XPC responds by tearing down the XPartition Communication
608 * infrastructure used for the just downed partition.
610 * XPC's heartbeat code will never call this function and xpc_partition_up()
611 * at the same time. Nor will it ever make multiple calls to either function
615 xpc_partition_going_down(struct xpc_partition *part, enum xp_retval reason)
617 unsigned long irq_flags;
619 struct xpc_channel *ch;
621 dev_dbg(xpc_chan, "deactivating partition %d, reason=%d\n",
622 XPC_PARTID(part), reason);
624 if (!xpc_part_ref(part)) {
625 /* infrastructure for this partition isn't currently set up */
629 /* disconnect channels associated with the partition going down */
631 for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
632 ch = &part->channels[ch_number];
634 xpc_msgqueue_ref(ch);
635 spin_lock_irqsave(&ch->lock, irq_flags);
637 XPC_DISCONNECT_CHANNEL(ch, reason, &irq_flags);
639 spin_unlock_irqrestore(&ch->lock, irq_flags);
640 xpc_msgqueue_deref(ch);
643 xpc_wakeup_channel_mgr(part);
645 xpc_part_deref(part);
649 * Called by XP at the time of channel connection registration to cause
650 * XPC to establish connections to all currently active partitions.
653 xpc_initiate_connect(int ch_number)
656 struct xpc_partition *part;
657 struct xpc_channel *ch;
659 DBUG_ON(ch_number < 0 || ch_number >= XPC_MAX_NCHANNELS);
661 for (partid = 0; partid < xp_max_npartitions; partid++) {
662 part = &xpc_partitions[partid];
664 if (xpc_part_ref(part)) {
665 ch = &part->channels[ch_number];
668 * Initiate the establishment of a connection on the
669 * newly registered channel to the remote partition.
671 xpc_wakeup_channel_mgr(part);
672 xpc_part_deref(part);
678 xpc_connected_callout(struct xpc_channel *ch)
680 /* let the registerer know that a connection has been established */
682 if (ch->func != NULL) {
683 dev_dbg(xpc_chan, "ch->func() called, reason=xpConnected, "
684 "partid=%d, channel=%d\n", ch->partid, ch->number);
686 ch->func(xpConnected, ch->partid, ch->number,
687 (void *)(u64)ch->local_nentries, ch->key);
689 dev_dbg(xpc_chan, "ch->func() returned, reason=xpConnected, "
690 "partid=%d, channel=%d\n", ch->partid, ch->number);
695 * Called by XP at the time of channel connection unregistration to cause
696 * XPC to teardown all current connections for the specified channel.
698 * Before returning xpc_initiate_disconnect() will wait until all connections
699 * on the specified channel have been closed/torndown. So the caller can be
700 * assured that they will not be receiving any more callouts from XPC to the
701 * function they registered via xpc_connect().
705 * ch_number - channel # to unregister.
708 xpc_initiate_disconnect(int ch_number)
710 unsigned long irq_flags;
712 struct xpc_partition *part;
713 struct xpc_channel *ch;
715 DBUG_ON(ch_number < 0 || ch_number >= XPC_MAX_NCHANNELS);
717 /* initiate the channel disconnect for every active partition */
718 for (partid = 0; partid < xp_max_npartitions; partid++) {
719 part = &xpc_partitions[partid];
721 if (xpc_part_ref(part)) {
722 ch = &part->channels[ch_number];
723 xpc_msgqueue_ref(ch);
725 spin_lock_irqsave(&ch->lock, irq_flags);
727 if (!(ch->flags & XPC_C_DISCONNECTED)) {
728 ch->flags |= XPC_C_WDISCONNECT;
730 XPC_DISCONNECT_CHANNEL(ch, xpUnregistering,
734 spin_unlock_irqrestore(&ch->lock, irq_flags);
736 xpc_msgqueue_deref(ch);
737 xpc_part_deref(part);
741 xpc_disconnect_wait(ch_number);
745 * To disconnect a channel, and reflect it back to all who may be waiting.
747 * An OPEN is not allowed until XPC_C_DISCONNECTING is cleared by
748 * xpc_process_disconnect(), and if set, XPC_C_WDISCONNECT is cleared by
749 * xpc_disconnect_wait().
751 * THE CHANNEL IS TO BE LOCKED BY THE CALLER AND WILL REMAIN LOCKED UPON RETURN.
754 xpc_disconnect_channel(const int line, struct xpc_channel *ch,
755 enum xp_retval reason, unsigned long *irq_flags)
757 u32 channel_was_connected = (ch->flags & XPC_C_CONNECTED);
759 DBUG_ON(!spin_is_locked(&ch->lock));
761 if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_DISCONNECTED))
764 DBUG_ON(!(ch->flags & (XPC_C_CONNECTING | XPC_C_CONNECTED)));
766 dev_dbg(xpc_chan, "reason=%d, line=%d, partid=%d, channel=%d\n",
767 reason, line, ch->partid, ch->number);
769 XPC_SET_REASON(ch, reason, line);
771 ch->flags |= (XPC_C_CLOSEREQUEST | XPC_C_DISCONNECTING);
772 /* some of these may not have been set */
773 ch->flags &= ~(XPC_C_OPENREQUEST | XPC_C_OPENREPLY |
774 XPC_C_ROPENREQUEST | XPC_C_ROPENREPLY |
775 XPC_C_CONNECTING | XPC_C_CONNECTED);
777 xpc_send_chctl_closerequest(ch, irq_flags);
779 if (channel_was_connected)
780 ch->flags |= XPC_C_WASCONNECTED;
782 spin_unlock_irqrestore(&ch->lock, *irq_flags);
784 /* wake all idle kthreads so they can exit */
785 if (atomic_read(&ch->kthreads_idle) > 0) {
786 wake_up_all(&ch->idle_wq);
788 } else if ((ch->flags & XPC_C_CONNECTEDCALLOUT_MADE) &&
789 !(ch->flags & XPC_C_DISCONNECTINGCALLOUT)) {
790 /* start a kthread that will do the xpDisconnecting callout */
791 xpc_create_kthreads(ch, 1, 1);
794 /* wake those waiting to allocate an entry from the local msg queue */
795 if (atomic_read(&ch->n_on_msg_allocate_wq) > 0)
796 wake_up(&ch->msg_allocate_wq);
798 spin_lock_irqsave(&ch->lock, *irq_flags);
802 xpc_disconnect_callout(struct xpc_channel *ch, enum xp_retval reason)
805 * Let the channel's registerer know that the channel is being
806 * disconnected. We don't want to do this if the registerer was never
807 * informed of a connection being made.
810 if (ch->func != NULL) {
811 dev_dbg(xpc_chan, "ch->func() called, reason=%d, partid=%d, "
812 "channel=%d\n", reason, ch->partid, ch->number);
814 ch->func(reason, ch->partid, ch->number, NULL, ch->key);
816 dev_dbg(xpc_chan, "ch->func() returned, reason=%d, partid=%d, "
817 "channel=%d\n", reason, ch->partid, ch->number);
822 * Wait for a message entry to become available for the specified channel,
823 * but don't wait any longer than 1 jiffy.
826 xpc_allocate_msg_wait(struct xpc_channel *ch)
830 if (ch->flags & XPC_C_DISCONNECTING) {
831 DBUG_ON(ch->reason == xpInterrupted);
835 atomic_inc(&ch->n_on_msg_allocate_wq);
836 ret = interruptible_sleep_on_timeout(&ch->msg_allocate_wq, 1);
837 atomic_dec(&ch->n_on_msg_allocate_wq);
839 if (ch->flags & XPC_C_DISCONNECTING) {
841 DBUG_ON(ch->reason == xpInterrupted);
842 } else if (ret == 0) {
852 * Send a message that contains the user's payload on the specified channel
853 * connected to the specified partition.
855 * NOTE that this routine can sleep waiting for a message entry to become
856 * available. To not sleep, pass in the XPC_NOWAIT flag.
858 * Once sent, this routine will not wait for the message to be received, nor
859 * will notification be given when it does happen.
863 * partid - ID of partition to which the channel is connected.
864 * ch_number - channel # to send message on.
865 * flags - see xp.h for valid flags.
866 * payload - pointer to the payload which is to be sent.
867 * payload_size - size of the payload in bytes.
870 xpc_initiate_send(short partid, int ch_number, u32 flags, void *payload,
873 struct xpc_partition *part = &xpc_partitions[partid];
874 enum xp_retval ret = xpUnknownReason;
876 dev_dbg(xpc_chan, "payload=0x%p, partid=%d, channel=%d\n", payload,
879 DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
880 DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
881 DBUG_ON(payload == NULL);
883 if (xpc_part_ref(part)) {
884 ret = xpc_send_payload(&part->channels[ch_number], flags,
885 payload, payload_size, 0, NULL, NULL);
886 xpc_part_deref(part);
893 * Send a message that contains the user's payload on the specified channel
894 * connected to the specified partition.
896 * NOTE that this routine can sleep waiting for a message entry to become
897 * available. To not sleep, pass in the XPC_NOWAIT flag.
899 * This routine will not wait for the message to be sent or received.
901 * Once the remote end of the channel has received the message, the function
902 * passed as an argument to xpc_initiate_send_notify() will be called. This
903 * allows the sender to free up or re-use any buffers referenced by the
904 * message, but does NOT mean the message has been processed at the remote
907 * If this routine returns an error, the caller's function will NOT be called.
911 * partid - ID of partition to which the channel is connected.
912 * ch_number - channel # to send message on.
913 * flags - see xp.h for valid flags.
914 * payload - pointer to the payload which is to be sent.
915 * payload_size - size of the payload in bytes.
916 * func - function to call with asynchronous notification of message
917 * receipt. THIS FUNCTION MUST BE NON-BLOCKING.
918 * key - user-defined key to be passed to the function when it's called.
921 xpc_initiate_send_notify(short partid, int ch_number, u32 flags, void *payload,
922 u16 payload_size, xpc_notify_func func, void *key)
924 struct xpc_partition *part = &xpc_partitions[partid];
925 enum xp_retval ret = xpUnknownReason;
927 dev_dbg(xpc_chan, "payload=0x%p, partid=%d, channel=%d\n", payload,
930 DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
931 DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
932 DBUG_ON(payload == NULL);
933 DBUG_ON(func == NULL);
935 if (xpc_part_ref(part)) {
936 ret = xpc_send_payload(&part->channels[ch_number], flags,
937 payload, payload_size, XPC_N_CALL, func,
939 xpc_part_deref(part);
945 * Deliver a message's payload to its intended recipient.
948 xpc_deliver_payload(struct xpc_channel *ch)
952 payload = xpc_get_deliverable_payload(ch);
953 if (payload != NULL) {
956 * This ref is taken to protect the payload itself from being
957 * freed before the user is finished with it, which the user
958 * indicates by calling xpc_initiate_received().
960 xpc_msgqueue_ref(ch);
962 atomic_inc(&ch->kthreads_active);
964 if (ch->func != NULL) {
965 dev_dbg(xpc_chan, "ch->func() called, payload=0x%p "
966 "partid=%d channel=%d\n", payload, ch->partid,
969 /* deliver the message to its intended recipient */
970 ch->func(xpMsgReceived, ch->partid, ch->number, payload,
973 dev_dbg(xpc_chan, "ch->func() returned, payload=0x%p "
974 "partid=%d channel=%d\n", payload, ch->partid,
978 atomic_dec(&ch->kthreads_active);
983 * Acknowledge receipt of a delivered message's payload.
985 * This function, although called by users, does not call xpc_part_ref() to
986 * ensure that the partition infrastructure is in place. It relies on the
987 * fact that we called xpc_msgqueue_ref() in xpc_deliver_payload().
991 * partid - ID of partition to which the channel is connected.
992 * ch_number - channel # message received on.
993 * payload - pointer to the payload area allocated via
994 * xpc_initiate_send() or xpc_initiate_send_notify().
997 xpc_initiate_received(short partid, int ch_number, void *payload)
999 struct xpc_partition *part = &xpc_partitions[partid];
1000 struct xpc_channel *ch;
1002 DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
1003 DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
1005 ch = &part->channels[ch_number];
1006 xpc_received_payload(ch, payload);
1008 /* the call to xpc_msgqueue_ref() was done by xpc_deliver_payload() */
1009 xpc_msgqueue_deref(ch);