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))
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 ch->flags = (XPC_C_CONNECTED | XPC_C_SETUP); /* clear all else */
64 dev_info(xpc_chan, "channel %d to partition %d connected\n",
65 ch->number, ch->partid);
67 spin_unlock_irqrestore(&ch->lock, *irq_flags);
68 xpc_create_kthreads(ch, 1, 0);
69 spin_lock_irqsave(&ch->lock, *irq_flags);
73 * spin_lock_irqsave() is expected to be held on entry.
76 xpc_process_disconnect(struct xpc_channel *ch, unsigned long *irq_flags)
78 struct xpc_partition *part = &xpc_partitions[ch->partid];
79 u32 channel_was_connected = (ch->flags & XPC_C_WASCONNECTED);
81 DBUG_ON(!spin_is_locked(&ch->lock));
83 if (!(ch->flags & XPC_C_DISCONNECTING))
86 DBUG_ON(!(ch->flags & XPC_C_CLOSEREQUEST));
88 /* make sure all activity has settled down first */
90 if (atomic_read(&ch->kthreads_assigned) > 0 ||
91 atomic_read(&ch->references) > 0) {
94 DBUG_ON((ch->flags & XPC_C_CONNECTEDCALLOUT_MADE) &&
95 !(ch->flags & XPC_C_DISCONNECTINGCALLOUT_MADE));
97 if (part->act_state == XPC_P_AS_DEACTIVATING) {
98 /* can't proceed until the other side disengages from us */
99 if (xpc_partition_engaged(ch->partid))
104 /* as long as the other side is up do the full protocol */
106 if (!(ch->flags & XPC_C_RCLOSEREQUEST))
109 if (!(ch->flags & XPC_C_CLOSEREPLY)) {
110 ch->flags |= XPC_C_CLOSEREPLY;
111 xpc_send_chctl_closereply(ch, irq_flags);
114 if (!(ch->flags & XPC_C_RCLOSEREPLY))
118 /* wake those waiting for notify completion */
119 if (atomic_read(&ch->n_to_notify) > 0) {
120 /* we do callout while holding ch->lock, callout can't block */
121 xpc_notify_senders_of_disconnect(ch);
124 /* both sides are disconnected now */
126 if (ch->flags & XPC_C_DISCONNECTINGCALLOUT_MADE) {
127 spin_unlock_irqrestore(&ch->lock, *irq_flags);
128 xpc_disconnect_callout(ch, xpDisconnected);
129 spin_lock_irqsave(&ch->lock, *irq_flags);
132 DBUG_ON(atomic_read(&ch->n_to_notify) != 0);
134 /* it's now safe to free the channel's message queues */
135 xpc_teardown_msg_structures(ch);
140 ch->local_nentries = 0;
141 ch->remote_nentries = 0;
142 ch->kthreads_assigned_limit = 0;
143 ch->kthreads_idle_limit = 0;
146 * Mark the channel disconnected and clear all other flags, including
147 * XPC_C_SETUP (because of call to xpc_teardown_msg_structures()) but
148 * not including XPC_C_WDISCONNECT (if it was set).
150 ch->flags = (XPC_C_DISCONNECTED | (ch->flags & XPC_C_WDISCONNECT));
152 atomic_dec(&part->nchannels_active);
154 if (channel_was_connected) {
155 dev_info(xpc_chan, "channel %d to partition %d disconnected, "
156 "reason=%d\n", ch->number, ch->partid, ch->reason);
159 if (ch->flags & XPC_C_WDISCONNECT) {
160 /* we won't lose the CPU since we're holding ch->lock */
161 complete(&ch->wdisconnect_wait);
162 } else if (ch->delayed_chctl_flags) {
163 if (part->act_state != XPC_P_AS_DEACTIVATING) {
164 /* time to take action on any delayed chctl flags */
165 spin_lock(&part->chctl_lock);
166 part->chctl.flags[ch->number] |=
167 ch->delayed_chctl_flags;
168 spin_unlock(&part->chctl_lock);
170 ch->delayed_chctl_flags = 0;
175 * Process a change in the channel's remote connection state.
178 xpc_process_openclose_chctl_flags(struct xpc_partition *part, int ch_number,
181 unsigned long irq_flags;
182 struct xpc_openclose_args *args =
183 &part->remote_openclose_args[ch_number];
184 struct xpc_channel *ch = &part->channels[ch_number];
185 enum xp_retval reason;
187 spin_lock_irqsave(&ch->lock, irq_flags);
191 if ((ch->flags & XPC_C_DISCONNECTED) &&
192 (ch->flags & XPC_C_WDISCONNECT)) {
194 * Delay processing chctl flags until thread waiting disconnect
195 * has had a chance to see that the channel is disconnected.
197 ch->delayed_chctl_flags |= chctl_flags;
198 spin_unlock_irqrestore(&ch->lock, irq_flags);
202 if (chctl_flags & XPC_CHCTL_CLOSEREQUEST) {
204 dev_dbg(xpc_chan, "XPC_CHCTL_CLOSEREQUEST (reason=%d) received "
205 "from partid=%d, channel=%d\n", args->reason,
206 ch->partid, ch->number);
209 * If RCLOSEREQUEST is set, we're probably waiting for
210 * RCLOSEREPLY. We should find it and a ROPENREQUEST packed
211 * with this RCLOSEREQUEST in the chctl_flags.
214 if (ch->flags & XPC_C_RCLOSEREQUEST) {
215 DBUG_ON(!(ch->flags & XPC_C_DISCONNECTING));
216 DBUG_ON(!(ch->flags & XPC_C_CLOSEREQUEST));
217 DBUG_ON(!(ch->flags & XPC_C_CLOSEREPLY));
218 DBUG_ON(ch->flags & XPC_C_RCLOSEREPLY);
220 DBUG_ON(!(chctl_flags & XPC_CHCTL_CLOSEREPLY));
221 chctl_flags &= ~XPC_CHCTL_CLOSEREPLY;
222 ch->flags |= XPC_C_RCLOSEREPLY;
224 /* both sides have finished disconnecting */
225 xpc_process_disconnect(ch, &irq_flags);
226 DBUG_ON(!(ch->flags & XPC_C_DISCONNECTED));
230 if (ch->flags & XPC_C_DISCONNECTED) {
231 if (!(chctl_flags & XPC_CHCTL_OPENREQUEST)) {
232 if (part->chctl.flags[ch_number] &
233 XPC_CHCTL_OPENREQUEST) {
235 DBUG_ON(ch->delayed_chctl_flags != 0);
236 spin_lock(&part->chctl_lock);
237 part->chctl.flags[ch_number] |=
238 XPC_CHCTL_CLOSEREQUEST;
239 spin_unlock(&part->chctl_lock);
241 spin_unlock_irqrestore(&ch->lock, irq_flags);
245 XPC_SET_REASON(ch, 0, 0);
246 ch->flags &= ~XPC_C_DISCONNECTED;
248 atomic_inc(&part->nchannels_active);
249 ch->flags |= (XPC_C_CONNECTING | XPC_C_ROPENREQUEST);
252 chctl_flags &= ~(XPC_CHCTL_OPENREQUEST | XPC_CHCTL_OPENREPLY);
255 * The meaningful CLOSEREQUEST connection state fields are:
256 * reason = reason connection is to be closed
259 ch->flags |= XPC_C_RCLOSEREQUEST;
261 if (!(ch->flags & XPC_C_DISCONNECTING)) {
262 reason = args->reason;
263 if (reason <= xpSuccess || reason > xpUnknownReason)
264 reason = xpUnknownReason;
265 else if (reason == xpUnregistering)
266 reason = xpOtherUnregistering;
268 XPC_DISCONNECT_CHANNEL(ch, reason, &irq_flags);
270 DBUG_ON(chctl_flags & XPC_CHCTL_CLOSEREPLY);
271 spin_unlock_irqrestore(&ch->lock, irq_flags);
275 xpc_process_disconnect(ch, &irq_flags);
278 if (chctl_flags & XPC_CHCTL_CLOSEREPLY) {
280 dev_dbg(xpc_chan, "XPC_CHCTL_CLOSEREPLY received from partid="
281 "%d, channel=%d\n", ch->partid, ch->number);
283 if (ch->flags & XPC_C_DISCONNECTED) {
284 DBUG_ON(part->act_state != XPC_P_AS_DEACTIVATING);
285 spin_unlock_irqrestore(&ch->lock, irq_flags);
289 DBUG_ON(!(ch->flags & XPC_C_CLOSEREQUEST));
291 if (!(ch->flags & XPC_C_RCLOSEREQUEST)) {
292 if (part->chctl.flags[ch_number] &
293 XPC_CHCTL_CLOSEREQUEST) {
295 DBUG_ON(ch->delayed_chctl_flags != 0);
296 spin_lock(&part->chctl_lock);
297 part->chctl.flags[ch_number] |=
298 XPC_CHCTL_CLOSEREPLY;
299 spin_unlock(&part->chctl_lock);
301 spin_unlock_irqrestore(&ch->lock, irq_flags);
305 ch->flags |= XPC_C_RCLOSEREPLY;
307 if (ch->flags & XPC_C_CLOSEREPLY) {
308 /* both sides have finished disconnecting */
309 xpc_process_disconnect(ch, &irq_flags);
313 if (chctl_flags & XPC_CHCTL_OPENREQUEST) {
315 dev_dbg(xpc_chan, "XPC_CHCTL_OPENREQUEST (entry_size=%d, "
316 "local_nentries=%d) received from partid=%d, "
317 "channel=%d\n", args->entry_size, args->local_nentries,
318 ch->partid, ch->number);
320 if (part->act_state == XPC_P_AS_DEACTIVATING ||
321 (ch->flags & XPC_C_ROPENREQUEST)) {
322 spin_unlock_irqrestore(&ch->lock, irq_flags);
326 if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_WDISCONNECT)) {
327 ch->delayed_chctl_flags |= XPC_CHCTL_OPENREQUEST;
328 spin_unlock_irqrestore(&ch->lock, irq_flags);
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 */
343 spin_unlock_irqrestore(&ch->lock, irq_flags);
347 ch->flags |= (XPC_C_ROPENREQUEST | XPC_C_CONNECTING);
348 ch->remote_nentries = args->local_nentries;
350 if (ch->flags & XPC_C_OPENREQUEST) {
351 if (args->entry_size != ch->entry_size) {
352 XPC_DISCONNECT_CHANNEL(ch, xpUnequalMsgSizes,
354 spin_unlock_irqrestore(&ch->lock, irq_flags);
358 ch->entry_size = args->entry_size;
360 XPC_SET_REASON(ch, 0, 0);
361 ch->flags &= ~XPC_C_DISCONNECTED;
363 atomic_inc(&part->nchannels_active);
366 xpc_process_connect(ch, &irq_flags);
369 if (chctl_flags & XPC_CHCTL_OPENREPLY) {
371 dev_dbg(xpc_chan, "XPC_CHCTL_OPENREPLY (local_msgqueue_pa="
372 "0x%lx, local_nentries=%d, remote_nentries=%d) "
373 "received from partid=%d, channel=%d\n",
374 args->local_msgqueue_pa, args->local_nentries,
375 args->remote_nentries, ch->partid, ch->number);
377 if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_DISCONNECTED)) {
378 spin_unlock_irqrestore(&ch->lock, irq_flags);
381 if (!(ch->flags & XPC_C_OPENREQUEST)) {
382 XPC_DISCONNECT_CHANNEL(ch, xpOpenCloseError,
384 spin_unlock_irqrestore(&ch->lock, irq_flags);
388 DBUG_ON(!(ch->flags & XPC_C_ROPENREQUEST));
389 DBUG_ON(ch->flags & XPC_C_CONNECTED);
392 * The meaningful OPENREPLY connection state fields are:
393 * local_msgqueue_pa = physical address of remote
394 * partition's local_msgqueue
395 * local_nentries = remote partition's local_nentries
396 * remote_nentries = remote partition's remote_nentries
398 DBUG_ON(args->local_msgqueue_pa == 0);
399 DBUG_ON(args->local_nentries == 0);
400 DBUG_ON(args->remote_nentries == 0);
402 ch->flags |= XPC_C_ROPENREPLY;
403 xpc_save_remote_msgqueue_pa(ch, args->local_msgqueue_pa);
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 spin_unlock_irqrestore(&ch->lock, irq_flags);
431 * Attempt to establish a channel connection to a remote partition.
433 static enum xp_retval
434 xpc_connect_channel(struct xpc_channel *ch)
436 unsigned long irq_flags;
437 struct xpc_registration *registration = &xpc_registrations[ch->number];
439 if (mutex_trylock(®istration->mutex) == 0)
442 if (!XPC_CHANNEL_REGISTERED(ch->number)) {
443 mutex_unlock(®istration->mutex);
444 return xpUnregistered;
447 spin_lock_irqsave(&ch->lock, irq_flags);
449 DBUG_ON(ch->flags & XPC_C_CONNECTED);
450 DBUG_ON(ch->flags & XPC_C_OPENREQUEST);
452 if (ch->flags & XPC_C_DISCONNECTING) {
453 spin_unlock_irqrestore(&ch->lock, irq_flags);
454 mutex_unlock(®istration->mutex);
458 /* add info from the channel connect registration to the channel */
460 ch->kthreads_assigned_limit = registration->assigned_limit;
461 ch->kthreads_idle_limit = registration->idle_limit;
462 DBUG_ON(atomic_read(&ch->kthreads_assigned) != 0);
463 DBUG_ON(atomic_read(&ch->kthreads_idle) != 0);
464 DBUG_ON(atomic_read(&ch->kthreads_active) != 0);
466 ch->func = registration->func;
467 DBUG_ON(registration->func == NULL);
468 ch->key = registration->key;
470 ch->local_nentries = registration->nentries;
472 if (ch->flags & XPC_C_ROPENREQUEST) {
473 if (registration->entry_size != ch->entry_size) {
474 /* the local and remote sides aren't the same */
477 * Because XPC_DISCONNECT_CHANNEL() can block we're
478 * forced to up the registration sema before we unlock
479 * the channel lock. But that's okay here because we're
480 * done with the part that required the registration
481 * sema. XPC_DISCONNECT_CHANNEL() requires that the
482 * channel lock be locked and will unlock and relock
483 * the channel lock as needed.
485 mutex_unlock(®istration->mutex);
486 XPC_DISCONNECT_CHANNEL(ch, xpUnequalMsgSizes,
488 spin_unlock_irqrestore(&ch->lock, irq_flags);
489 return xpUnequalMsgSizes;
492 ch->entry_size = registration->entry_size;
494 XPC_SET_REASON(ch, 0, 0);
495 ch->flags &= ~XPC_C_DISCONNECTED;
497 atomic_inc(&xpc_partitions[ch->partid].nchannels_active);
500 mutex_unlock(®istration->mutex);
502 /* initiate the connection */
504 ch->flags |= (XPC_C_OPENREQUEST | XPC_C_CONNECTING);
505 xpc_send_chctl_openrequest(ch, &irq_flags);
507 xpc_process_connect(ch, &irq_flags);
509 spin_unlock_irqrestore(&ch->lock, irq_flags);
515 xpc_process_sent_chctl_flags(struct xpc_partition *part)
517 unsigned long irq_flags;
518 union xpc_channel_ctl_flags chctl;
519 struct xpc_channel *ch;
523 chctl.all_flags = xpc_get_chctl_all_flags(part);
526 * Initiate channel connections for registered channels.
528 * For each connected channel that has pending messages activate idle
529 * kthreads and/or create new kthreads as needed.
532 for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
533 ch = &part->channels[ch_number];
536 * Process any open or close related chctl flags, and then deal
537 * with connecting or disconnecting the channel as required.
540 if (chctl.flags[ch_number] & XPC_OPENCLOSE_CHCTL_FLAGS) {
541 xpc_process_openclose_chctl_flags(part, ch_number,
542 chctl.flags[ch_number]);
545 ch_flags = ch->flags; /* need an atomic snapshot of flags */
547 if (ch_flags & XPC_C_DISCONNECTING) {
548 spin_lock_irqsave(&ch->lock, irq_flags);
549 xpc_process_disconnect(ch, &irq_flags);
550 spin_unlock_irqrestore(&ch->lock, irq_flags);
554 if (part->act_state == XPC_P_AS_DEACTIVATING)
557 if (!(ch_flags & XPC_C_CONNECTED)) {
558 if (!(ch_flags & XPC_C_OPENREQUEST)) {
559 DBUG_ON(ch_flags & XPC_C_SETUP);
560 (void)xpc_connect_channel(ch);
562 spin_lock_irqsave(&ch->lock, irq_flags);
563 xpc_process_connect(ch, &irq_flags);
564 spin_unlock_irqrestore(&ch->lock, irq_flags);
570 * Process any message related chctl flags, this may involve
571 * the activation of kthreads to deliver any pending messages
572 * sent from the other partition.
575 if (chctl.flags[ch_number] & XPC_MSG_CHCTL_FLAGS)
576 xpc_process_msg_chctl_flags(part, ch_number);
581 * XPC's heartbeat code calls this function to inform XPC that a partition is
582 * going down. XPC responds by tearing down the XPartition Communication
583 * infrastructure used for the just downed partition.
585 * XPC's heartbeat code will never call this function and xpc_partition_up()
586 * at the same time. Nor will it ever make multiple calls to either function
590 xpc_partition_going_down(struct xpc_partition *part, enum xp_retval reason)
592 unsigned long irq_flags;
594 struct xpc_channel *ch;
596 dev_dbg(xpc_chan, "deactivating partition %d, reason=%d\n",
597 XPC_PARTID(part), reason);
599 if (!xpc_part_ref(part)) {
600 /* infrastructure for this partition isn't currently set up */
604 /* disconnect channels associated with the partition going down */
606 for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
607 ch = &part->channels[ch_number];
609 xpc_msgqueue_ref(ch);
610 spin_lock_irqsave(&ch->lock, irq_flags);
612 XPC_DISCONNECT_CHANNEL(ch, reason, &irq_flags);
614 spin_unlock_irqrestore(&ch->lock, irq_flags);
615 xpc_msgqueue_deref(ch);
618 xpc_wakeup_channel_mgr(part);
620 xpc_part_deref(part);
624 * Called by XP at the time of channel connection registration to cause
625 * XPC to establish connections to all currently active partitions.
628 xpc_initiate_connect(int ch_number)
631 struct xpc_partition *part;
632 struct xpc_channel *ch;
634 DBUG_ON(ch_number < 0 || ch_number >= XPC_MAX_NCHANNELS);
636 for (partid = 0; partid < xp_max_npartitions; partid++) {
637 part = &xpc_partitions[partid];
639 if (xpc_part_ref(part)) {
640 ch = &part->channels[ch_number];
643 * Initiate the establishment of a connection on the
644 * newly registered channel to the remote partition.
646 xpc_wakeup_channel_mgr(part);
647 xpc_part_deref(part);
653 xpc_connected_callout(struct xpc_channel *ch)
655 /* let the registerer know that a connection has been established */
657 if (ch->func != NULL) {
658 dev_dbg(xpc_chan, "ch->func() called, reason=xpConnected, "
659 "partid=%d, channel=%d\n", ch->partid, ch->number);
661 ch->func(xpConnected, ch->partid, ch->number,
662 (void *)(u64)ch->local_nentries, ch->key);
664 dev_dbg(xpc_chan, "ch->func() returned, reason=xpConnected, "
665 "partid=%d, channel=%d\n", ch->partid, ch->number);
670 * Called by XP at the time of channel connection unregistration to cause
671 * XPC to teardown all current connections for the specified channel.
673 * Before returning xpc_initiate_disconnect() will wait until all connections
674 * on the specified channel have been closed/torndown. So the caller can be
675 * assured that they will not be receiving any more callouts from XPC to the
676 * function they registered via xpc_connect().
680 * ch_number - channel # to unregister.
683 xpc_initiate_disconnect(int ch_number)
685 unsigned long irq_flags;
687 struct xpc_partition *part;
688 struct xpc_channel *ch;
690 DBUG_ON(ch_number < 0 || ch_number >= XPC_MAX_NCHANNELS);
692 /* initiate the channel disconnect for every active partition */
693 for (partid = 0; partid < xp_max_npartitions; partid++) {
694 part = &xpc_partitions[partid];
696 if (xpc_part_ref(part)) {
697 ch = &part->channels[ch_number];
698 xpc_msgqueue_ref(ch);
700 spin_lock_irqsave(&ch->lock, irq_flags);
702 if (!(ch->flags & XPC_C_DISCONNECTED)) {
703 ch->flags |= XPC_C_WDISCONNECT;
705 XPC_DISCONNECT_CHANNEL(ch, xpUnregistering,
709 spin_unlock_irqrestore(&ch->lock, irq_flags);
711 xpc_msgqueue_deref(ch);
712 xpc_part_deref(part);
716 xpc_disconnect_wait(ch_number);
720 * To disconnect a channel, and reflect it back to all who may be waiting.
722 * An OPEN is not allowed until XPC_C_DISCONNECTING is cleared by
723 * xpc_process_disconnect(), and if set, XPC_C_WDISCONNECT is cleared by
724 * xpc_disconnect_wait().
726 * THE CHANNEL IS TO BE LOCKED BY THE CALLER AND WILL REMAIN LOCKED UPON RETURN.
729 xpc_disconnect_channel(const int line, struct xpc_channel *ch,
730 enum xp_retval reason, unsigned long *irq_flags)
732 u32 channel_was_connected = (ch->flags & XPC_C_CONNECTED);
734 DBUG_ON(!spin_is_locked(&ch->lock));
736 if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_DISCONNECTED))
739 DBUG_ON(!(ch->flags & (XPC_C_CONNECTING | XPC_C_CONNECTED)));
741 dev_dbg(xpc_chan, "reason=%d, line=%d, partid=%d, channel=%d\n",
742 reason, line, ch->partid, ch->number);
744 XPC_SET_REASON(ch, reason, line);
746 ch->flags |= (XPC_C_CLOSEREQUEST | XPC_C_DISCONNECTING);
747 /* some of these may not have been set */
748 ch->flags &= ~(XPC_C_OPENREQUEST | XPC_C_OPENREPLY |
749 XPC_C_ROPENREQUEST | XPC_C_ROPENREPLY |
750 XPC_C_CONNECTING | XPC_C_CONNECTED);
752 xpc_send_chctl_closerequest(ch, irq_flags);
754 if (channel_was_connected)
755 ch->flags |= XPC_C_WASCONNECTED;
757 spin_unlock_irqrestore(&ch->lock, *irq_flags);
759 /* wake all idle kthreads so they can exit */
760 if (atomic_read(&ch->kthreads_idle) > 0) {
761 wake_up_all(&ch->idle_wq);
763 } else if ((ch->flags & XPC_C_CONNECTEDCALLOUT_MADE) &&
764 !(ch->flags & XPC_C_DISCONNECTINGCALLOUT)) {
765 /* start a kthread that will do the xpDisconnecting callout */
766 xpc_create_kthreads(ch, 1, 1);
769 /* wake those waiting to allocate an entry from the local msg queue */
770 if (atomic_read(&ch->n_on_msg_allocate_wq) > 0)
771 wake_up(&ch->msg_allocate_wq);
773 spin_lock_irqsave(&ch->lock, *irq_flags);
777 xpc_disconnect_callout(struct xpc_channel *ch, enum xp_retval reason)
780 * Let the channel's registerer know that the channel is being
781 * disconnected. We don't want to do this if the registerer was never
782 * informed of a connection being made.
785 if (ch->func != NULL) {
786 dev_dbg(xpc_chan, "ch->func() called, reason=%d, partid=%d, "
787 "channel=%d\n", reason, ch->partid, ch->number);
789 ch->func(reason, ch->partid, ch->number, NULL, ch->key);
791 dev_dbg(xpc_chan, "ch->func() returned, reason=%d, partid=%d, "
792 "channel=%d\n", reason, ch->partid, ch->number);
797 * Wait for a message entry to become available for the specified channel,
798 * but don't wait any longer than 1 jiffy.
801 xpc_allocate_msg_wait(struct xpc_channel *ch)
805 if (ch->flags & XPC_C_DISCONNECTING) {
806 DBUG_ON(ch->reason == xpInterrupted);
810 atomic_inc(&ch->n_on_msg_allocate_wq);
811 ret = interruptible_sleep_on_timeout(&ch->msg_allocate_wq, 1);
812 atomic_dec(&ch->n_on_msg_allocate_wq);
814 if (ch->flags & XPC_C_DISCONNECTING) {
816 DBUG_ON(ch->reason == xpInterrupted);
817 } else if (ret == 0) {
827 * Send a message that contains the user's payload on the specified channel
828 * connected to the specified partition.
830 * NOTE that this routine can sleep waiting for a message entry to become
831 * available. To not sleep, pass in the XPC_NOWAIT flag.
833 * Once sent, this routine will not wait for the message to be received, nor
834 * will notification be given when it does happen.
838 * partid - ID of partition to which the channel is connected.
839 * ch_number - channel # to send message on.
840 * flags - see xp.h for valid flags.
841 * payload - pointer to the payload which is to be sent.
842 * payload_size - size of the payload in bytes.
845 xpc_initiate_send(short partid, int ch_number, u32 flags, void *payload,
848 struct xpc_partition *part = &xpc_partitions[partid];
849 enum xp_retval ret = xpUnknownReason;
851 dev_dbg(xpc_chan, "payload=0x%p, partid=%d, channel=%d\n", payload,
854 DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
855 DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
856 DBUG_ON(payload == NULL);
858 if (xpc_part_ref(part)) {
859 ret = xpc_send_payload(&part->channels[ch_number], flags,
860 payload, payload_size, 0, NULL, NULL);
861 xpc_part_deref(part);
868 * Send a message that contains the user's payload on the specified channel
869 * connected to the specified partition.
871 * NOTE that this routine can sleep waiting for a message entry to become
872 * available. To not sleep, pass in the XPC_NOWAIT flag.
874 * This routine will not wait for the message to be sent or received.
876 * Once the remote end of the channel has received the message, the function
877 * passed as an argument to xpc_initiate_send_notify() will be called. This
878 * allows the sender to free up or re-use any buffers referenced by the
879 * message, but does NOT mean the message has been processed at the remote
882 * If this routine returns an error, the caller's function will NOT be called.
886 * partid - ID of partition to which the channel is connected.
887 * ch_number - channel # to send message on.
888 * flags - see xp.h for valid flags.
889 * payload - pointer to the payload which is to be sent.
890 * payload_size - size of the payload in bytes.
891 * func - function to call with asynchronous notification of message
892 * receipt. THIS FUNCTION MUST BE NON-BLOCKING.
893 * key - user-defined key to be passed to the function when it's called.
896 xpc_initiate_send_notify(short partid, int ch_number, u32 flags, void *payload,
897 u16 payload_size, xpc_notify_func func, void *key)
899 struct xpc_partition *part = &xpc_partitions[partid];
900 enum xp_retval ret = xpUnknownReason;
902 dev_dbg(xpc_chan, "payload=0x%p, partid=%d, channel=%d\n", payload,
905 DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
906 DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
907 DBUG_ON(payload == NULL);
908 DBUG_ON(func == NULL);
910 if (xpc_part_ref(part)) {
911 ret = xpc_send_payload(&part->channels[ch_number], flags,
912 payload, payload_size, XPC_N_CALL, func,
914 xpc_part_deref(part);
920 * Deliver a message's payload to its intended recipient.
923 xpc_deliver_payload(struct xpc_channel *ch)
927 payload = xpc_get_deliverable_payload(ch);
928 if (payload != NULL) {
931 * This ref is taken to protect the payload itself from being
932 * freed before the user is finished with it, which the user
933 * indicates by calling xpc_initiate_received().
935 xpc_msgqueue_ref(ch);
937 atomic_inc(&ch->kthreads_active);
939 if (ch->func != NULL) {
940 dev_dbg(xpc_chan, "ch->func() called, payload=0x%p "
941 "partid=%d channel=%d\n", payload, ch->partid,
944 /* deliver the message to its intended recipient */
945 ch->func(xpMsgReceived, ch->partid, ch->number, payload,
948 dev_dbg(xpc_chan, "ch->func() returned, payload=0x%p "
949 "partid=%d channel=%d\n", payload, ch->partid,
953 atomic_dec(&ch->kthreads_active);
958 * Acknowledge receipt of a delivered message's payload.
960 * This function, although called by users, does not call xpc_part_ref() to
961 * ensure that the partition infrastructure is in place. It relies on the
962 * fact that we called xpc_msgqueue_ref() in xpc_deliver_payload().
966 * partid - ID of partition to which the channel is connected.
967 * ch_number - channel # message received on.
968 * payload - pointer to the payload area allocated via
969 * xpc_initiate_send() or xpc_initiate_send_notify().
972 xpc_initiate_received(short partid, int ch_number, void *payload)
974 struct xpc_partition *part = &xpc_partitions[partid];
975 struct xpc_channel *ch;
977 DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
978 DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
980 ch = &part->channels[ch_number];
981 xpc_received_payload(ch, payload);
983 /* the call to xpc_msgqueue_ref() was done by xpc_deliver_payload() */
984 xpc_msgqueue_deref(ch);