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
;
188 spin_lock_irqsave(&ch
->lock
, irq_flags
);
192 if ((ch
->flags
& XPC_C_DISCONNECTED
) &&
193 (ch
->flags
& XPC_C_WDISCONNECT
)) {
195 * Delay processing chctl flags until thread waiting disconnect
196 * has had a chance to see that the channel is disconnected.
198 ch
->delayed_chctl_flags
|= chctl_flags
;
199 spin_unlock_irqrestore(&ch
->lock
, irq_flags
);
203 if (chctl_flags
& XPC_CHCTL_CLOSEREQUEST
) {
205 dev_dbg(xpc_chan
, "XPC_CHCTL_CLOSEREQUEST (reason=%d) received "
206 "from partid=%d, channel=%d\n", args
->reason
,
207 ch
->partid
, ch
->number
);
210 * If RCLOSEREQUEST is set, we're probably waiting for
211 * RCLOSEREPLY. We should find it and a ROPENREQUEST packed
212 * with this RCLOSEREQUEST in the chctl_flags.
215 if (ch
->flags
& XPC_C_RCLOSEREQUEST
) {
216 DBUG_ON(!(ch
->flags
& XPC_C_DISCONNECTING
));
217 DBUG_ON(!(ch
->flags
& XPC_C_CLOSEREQUEST
));
218 DBUG_ON(!(ch
->flags
& XPC_C_CLOSEREPLY
));
219 DBUG_ON(ch
->flags
& XPC_C_RCLOSEREPLY
);
221 DBUG_ON(!(chctl_flags
& XPC_CHCTL_CLOSEREPLY
));
222 chctl_flags
&= ~XPC_CHCTL_CLOSEREPLY
;
223 ch
->flags
|= XPC_C_RCLOSEREPLY
;
225 /* both sides have finished disconnecting */
226 xpc_process_disconnect(ch
, &irq_flags
);
227 DBUG_ON(!(ch
->flags
& XPC_C_DISCONNECTED
));
231 if (ch
->flags
& XPC_C_DISCONNECTED
) {
232 if (!(chctl_flags
& XPC_CHCTL_OPENREQUEST
)) {
233 if (part
->chctl
.flags
[ch_number
] &
234 XPC_CHCTL_OPENREQUEST
) {
236 DBUG_ON(ch
->delayed_chctl_flags
!= 0);
237 spin_lock(&part
->chctl_lock
);
238 part
->chctl
.flags
[ch_number
] |=
239 XPC_CHCTL_CLOSEREQUEST
;
240 spin_unlock(&part
->chctl_lock
);
242 spin_unlock_irqrestore(&ch
->lock
, irq_flags
);
246 XPC_SET_REASON(ch
, 0, 0);
247 ch
->flags
&= ~XPC_C_DISCONNECTED
;
249 atomic_inc(&part
->nchannels_active
);
250 ch
->flags
|= (XPC_C_CONNECTING
| XPC_C_ROPENREQUEST
);
253 chctl_flags
&= ~(XPC_CHCTL_OPENREQUEST
| XPC_CHCTL_OPENREPLY
);
256 * The meaningful CLOSEREQUEST connection state fields are:
257 * reason = reason connection is to be closed
260 ch
->flags
|= XPC_C_RCLOSEREQUEST
;
262 if (!(ch
->flags
& XPC_C_DISCONNECTING
)) {
263 reason
= args
->reason
;
264 if (reason
<= xpSuccess
|| reason
> xpUnknownReason
)
265 reason
= xpUnknownReason
;
266 else if (reason
== xpUnregistering
)
267 reason
= xpOtherUnregistering
;
269 XPC_DISCONNECT_CHANNEL(ch
, reason
, &irq_flags
);
271 DBUG_ON(chctl_flags
& XPC_CHCTL_CLOSEREPLY
);
272 spin_unlock_irqrestore(&ch
->lock
, irq_flags
);
276 xpc_process_disconnect(ch
, &irq_flags
);
279 if (chctl_flags
& XPC_CHCTL_CLOSEREPLY
) {
281 dev_dbg(xpc_chan
, "XPC_CHCTL_CLOSEREPLY received from partid="
282 "%d, channel=%d\n", ch
->partid
, ch
->number
);
284 if (ch
->flags
& XPC_C_DISCONNECTED
) {
285 DBUG_ON(part
->act_state
!= XPC_P_AS_DEACTIVATING
);
286 spin_unlock_irqrestore(&ch
->lock
, irq_flags
);
290 DBUG_ON(!(ch
->flags
& XPC_C_CLOSEREQUEST
));
292 if (!(ch
->flags
& XPC_C_RCLOSEREQUEST
)) {
293 if (part
->chctl
.flags
[ch_number
] &
294 XPC_CHCTL_CLOSEREQUEST
) {
296 DBUG_ON(ch
->delayed_chctl_flags
!= 0);
297 spin_lock(&part
->chctl_lock
);
298 part
->chctl
.flags
[ch_number
] |=
299 XPC_CHCTL_CLOSEREPLY
;
300 spin_unlock(&part
->chctl_lock
);
302 spin_unlock_irqrestore(&ch
->lock
, irq_flags
);
306 ch
->flags
|= XPC_C_RCLOSEREPLY
;
308 if (ch
->flags
& XPC_C_CLOSEREPLY
) {
309 /* both sides have finished disconnecting */
310 xpc_process_disconnect(ch
, &irq_flags
);
314 if (chctl_flags
& XPC_CHCTL_OPENREQUEST
) {
316 dev_dbg(xpc_chan
, "XPC_CHCTL_OPENREQUEST (entry_size=%d, "
317 "local_nentries=%d) received from partid=%d, "
318 "channel=%d\n", args
->entry_size
, args
->local_nentries
,
319 ch
->partid
, ch
->number
);
321 if (part
->act_state
== XPC_P_AS_DEACTIVATING
||
322 (ch
->flags
& XPC_C_ROPENREQUEST
)) {
323 spin_unlock_irqrestore(&ch
->lock
, irq_flags
);
327 if (ch
->flags
& (XPC_C_DISCONNECTING
| XPC_C_WDISCONNECT
)) {
328 ch
->delayed_chctl_flags
|= XPC_CHCTL_OPENREQUEST
;
329 spin_unlock_irqrestore(&ch
->lock
, irq_flags
);
332 DBUG_ON(!(ch
->flags
& (XPC_C_DISCONNECTED
|
333 XPC_C_OPENREQUEST
)));
334 DBUG_ON(ch
->flags
& (XPC_C_ROPENREQUEST
| XPC_C_ROPENREPLY
|
335 XPC_C_OPENREPLY
| XPC_C_CONNECTED
));
338 * The meaningful OPENREQUEST connection state fields are:
339 * entry_size = size of channel's messages in bytes
340 * local_nentries = remote partition's local_nentries
342 if (args
->entry_size
== 0 || args
->local_nentries
== 0) {
343 /* assume OPENREQUEST was delayed by mistake */
344 spin_unlock_irqrestore(&ch
->lock
, irq_flags
);
348 ch
->flags
|= (XPC_C_ROPENREQUEST
| XPC_C_CONNECTING
);
349 ch
->remote_nentries
= args
->local_nentries
;
351 if (ch
->flags
& XPC_C_OPENREQUEST
) {
352 if (args
->entry_size
!= ch
->entry_size
) {
353 XPC_DISCONNECT_CHANNEL(ch
, xpUnequalMsgSizes
,
355 spin_unlock_irqrestore(&ch
->lock
, irq_flags
);
359 ch
->entry_size
= args
->entry_size
;
361 XPC_SET_REASON(ch
, 0, 0);
362 ch
->flags
&= ~XPC_C_DISCONNECTED
;
364 atomic_inc(&part
->nchannels_active
);
367 xpc_process_connect(ch
, &irq_flags
);
370 if (chctl_flags
& XPC_CHCTL_OPENREPLY
) {
372 dev_dbg(xpc_chan
, "XPC_CHCTL_OPENREPLY (local_msgqueue_pa="
373 "0x%lx, local_nentries=%d, remote_nentries=%d) "
374 "received from partid=%d, channel=%d\n",
375 args
->local_msgqueue_pa
, args
->local_nentries
,
376 args
->remote_nentries
, ch
->partid
, ch
->number
);
378 if (ch
->flags
& (XPC_C_DISCONNECTING
| XPC_C_DISCONNECTED
)) {
379 spin_unlock_irqrestore(&ch
->lock
, irq_flags
);
382 if (!(ch
->flags
& XPC_C_OPENREQUEST
)) {
383 XPC_DISCONNECT_CHANNEL(ch
, xpOpenCloseError
,
385 spin_unlock_irqrestore(&ch
->lock
, irq_flags
);
389 DBUG_ON(!(ch
->flags
& XPC_C_ROPENREQUEST
));
390 DBUG_ON(ch
->flags
& XPC_C_CONNECTED
);
393 * The meaningful OPENREPLY connection state fields are:
394 * local_msgqueue_pa = physical address of remote
395 * partition's local_msgqueue
396 * local_nentries = remote partition's local_nentries
397 * remote_nentries = remote partition's remote_nentries
399 DBUG_ON(args
->local_msgqueue_pa
== 0);
400 DBUG_ON(args
->local_nentries
== 0);
401 DBUG_ON(args
->remote_nentries
== 0);
403 ret
= xpc_save_remote_msgqueue_pa(ch
, args
->local_msgqueue_pa
);
404 if (ret
!= xpSuccess
) {
405 XPC_DISCONNECT_CHANNEL(ch
, ret
, &irq_flags
);
406 spin_unlock_irqrestore(&ch
->lock
, irq_flags
);
409 ch
->flags
|= XPC_C_ROPENREPLY
;
411 if (args
->local_nentries
< ch
->remote_nentries
) {
412 dev_dbg(xpc_chan
, "XPC_CHCTL_OPENREPLY: new "
413 "remote_nentries=%d, old remote_nentries=%d, "
414 "partid=%d, channel=%d\n",
415 args
->local_nentries
, ch
->remote_nentries
,
416 ch
->partid
, ch
->number
);
418 ch
->remote_nentries
= args
->local_nentries
;
420 if (args
->remote_nentries
< ch
->local_nentries
) {
421 dev_dbg(xpc_chan
, "XPC_CHCTL_OPENREPLY: new "
422 "local_nentries=%d, old local_nentries=%d, "
423 "partid=%d, channel=%d\n",
424 args
->remote_nentries
, ch
->local_nentries
,
425 ch
->partid
, ch
->number
);
427 ch
->local_nentries
= args
->remote_nentries
;
430 xpc_process_connect(ch
, &irq_flags
);
433 spin_unlock_irqrestore(&ch
->lock
, irq_flags
);
437 * Attempt to establish a channel connection to a remote partition.
439 static enum xp_retval
440 xpc_connect_channel(struct xpc_channel
*ch
)
442 unsigned long irq_flags
;
443 struct xpc_registration
*registration
= &xpc_registrations
[ch
->number
];
445 if (mutex_trylock(®istration
->mutex
) == 0)
448 if (!XPC_CHANNEL_REGISTERED(ch
->number
)) {
449 mutex_unlock(®istration
->mutex
);
450 return xpUnregistered
;
453 spin_lock_irqsave(&ch
->lock
, irq_flags
);
455 DBUG_ON(ch
->flags
& XPC_C_CONNECTED
);
456 DBUG_ON(ch
->flags
& XPC_C_OPENREQUEST
);
458 if (ch
->flags
& XPC_C_DISCONNECTING
) {
459 spin_unlock_irqrestore(&ch
->lock
, irq_flags
);
460 mutex_unlock(®istration
->mutex
);
464 /* add info from the channel connect registration to the channel */
466 ch
->kthreads_assigned_limit
= registration
->assigned_limit
;
467 ch
->kthreads_idle_limit
= registration
->idle_limit
;
468 DBUG_ON(atomic_read(&ch
->kthreads_assigned
) != 0);
469 DBUG_ON(atomic_read(&ch
->kthreads_idle
) != 0);
470 DBUG_ON(atomic_read(&ch
->kthreads_active
) != 0);
472 ch
->func
= registration
->func
;
473 DBUG_ON(registration
->func
== NULL
);
474 ch
->key
= registration
->key
;
476 ch
->local_nentries
= registration
->nentries
;
478 if (ch
->flags
& XPC_C_ROPENREQUEST
) {
479 if (registration
->entry_size
!= ch
->entry_size
) {
480 /* the local and remote sides aren't the same */
483 * Because XPC_DISCONNECT_CHANNEL() can block we're
484 * forced to up the registration sema before we unlock
485 * the channel lock. But that's okay here because we're
486 * done with the part that required the registration
487 * sema. XPC_DISCONNECT_CHANNEL() requires that the
488 * channel lock be locked and will unlock and relock
489 * the channel lock as needed.
491 mutex_unlock(®istration
->mutex
);
492 XPC_DISCONNECT_CHANNEL(ch
, xpUnequalMsgSizes
,
494 spin_unlock_irqrestore(&ch
->lock
, irq_flags
);
495 return xpUnequalMsgSizes
;
498 ch
->entry_size
= registration
->entry_size
;
500 XPC_SET_REASON(ch
, 0, 0);
501 ch
->flags
&= ~XPC_C_DISCONNECTED
;
503 atomic_inc(&xpc_partitions
[ch
->partid
].nchannels_active
);
506 mutex_unlock(®istration
->mutex
);
508 /* initiate the connection */
510 ch
->flags
|= (XPC_C_OPENREQUEST
| XPC_C_CONNECTING
);
511 xpc_send_chctl_openrequest(ch
, &irq_flags
);
513 xpc_process_connect(ch
, &irq_flags
);
515 spin_unlock_irqrestore(&ch
->lock
, irq_flags
);
521 xpc_process_sent_chctl_flags(struct xpc_partition
*part
)
523 unsigned long irq_flags
;
524 union xpc_channel_ctl_flags chctl
;
525 struct xpc_channel
*ch
;
529 chctl
.all_flags
= xpc_get_chctl_all_flags(part
);
532 * Initiate channel connections for registered channels.
534 * For each connected channel that has pending messages activate idle
535 * kthreads and/or create new kthreads as needed.
538 for (ch_number
= 0; ch_number
< part
->nchannels
; ch_number
++) {
539 ch
= &part
->channels
[ch_number
];
542 * Process any open or close related chctl flags, and then deal
543 * with connecting or disconnecting the channel as required.
546 if (chctl
.flags
[ch_number
] & XPC_OPENCLOSE_CHCTL_FLAGS
) {
547 xpc_process_openclose_chctl_flags(part
, ch_number
,
548 chctl
.flags
[ch_number
]);
551 ch_flags
= ch
->flags
; /* need an atomic snapshot of flags */
553 if (ch_flags
& XPC_C_DISCONNECTING
) {
554 spin_lock_irqsave(&ch
->lock
, irq_flags
);
555 xpc_process_disconnect(ch
, &irq_flags
);
556 spin_unlock_irqrestore(&ch
->lock
, irq_flags
);
560 if (part
->act_state
== XPC_P_AS_DEACTIVATING
)
563 if (!(ch_flags
& XPC_C_CONNECTED
)) {
564 if (!(ch_flags
& XPC_C_OPENREQUEST
)) {
565 DBUG_ON(ch_flags
& XPC_C_SETUP
);
566 (void)xpc_connect_channel(ch
);
568 spin_lock_irqsave(&ch
->lock
, irq_flags
);
569 xpc_process_connect(ch
, &irq_flags
);
570 spin_unlock_irqrestore(&ch
->lock
, irq_flags
);
576 * Process any message related chctl flags, this may involve
577 * the activation of kthreads to deliver any pending messages
578 * sent from the other partition.
581 if (chctl
.flags
[ch_number
] & XPC_MSG_CHCTL_FLAGS
)
582 xpc_process_msg_chctl_flags(part
, ch_number
);
587 * XPC's heartbeat code calls this function to inform XPC that a partition is
588 * going down. XPC responds by tearing down the XPartition Communication
589 * infrastructure used for the just downed partition.
591 * XPC's heartbeat code will never call this function and xpc_partition_up()
592 * at the same time. Nor will it ever make multiple calls to either function
596 xpc_partition_going_down(struct xpc_partition
*part
, enum xp_retval reason
)
598 unsigned long irq_flags
;
600 struct xpc_channel
*ch
;
602 dev_dbg(xpc_chan
, "deactivating partition %d, reason=%d\n",
603 XPC_PARTID(part
), reason
);
605 if (!xpc_part_ref(part
)) {
606 /* infrastructure for this partition isn't currently set up */
610 /* disconnect channels associated with the partition going down */
612 for (ch_number
= 0; ch_number
< part
->nchannels
; ch_number
++) {
613 ch
= &part
->channels
[ch_number
];
615 xpc_msgqueue_ref(ch
);
616 spin_lock_irqsave(&ch
->lock
, irq_flags
);
618 XPC_DISCONNECT_CHANNEL(ch
, reason
, &irq_flags
);
620 spin_unlock_irqrestore(&ch
->lock
, irq_flags
);
621 xpc_msgqueue_deref(ch
);
624 xpc_wakeup_channel_mgr(part
);
626 xpc_part_deref(part
);
630 * Called by XP at the time of channel connection registration to cause
631 * XPC to establish connections to all currently active partitions.
634 xpc_initiate_connect(int ch_number
)
637 struct xpc_partition
*part
;
638 struct xpc_channel
*ch
;
640 DBUG_ON(ch_number
< 0 || ch_number
>= XPC_MAX_NCHANNELS
);
642 for (partid
= 0; partid
< xp_max_npartitions
; partid
++) {
643 part
= &xpc_partitions
[partid
];
645 if (xpc_part_ref(part
)) {
646 ch
= &part
->channels
[ch_number
];
649 * Initiate the establishment of a connection on the
650 * newly registered channel to the remote partition.
652 xpc_wakeup_channel_mgr(part
);
653 xpc_part_deref(part
);
659 xpc_connected_callout(struct xpc_channel
*ch
)
661 /* let the registerer know that a connection has been established */
663 if (ch
->func
!= NULL
) {
664 dev_dbg(xpc_chan
, "ch->func() called, reason=xpConnected, "
665 "partid=%d, channel=%d\n", ch
->partid
, ch
->number
);
667 ch
->func(xpConnected
, ch
->partid
, ch
->number
,
668 (void *)(u64
)ch
->local_nentries
, ch
->key
);
670 dev_dbg(xpc_chan
, "ch->func() returned, reason=xpConnected, "
671 "partid=%d, channel=%d\n", ch
->partid
, ch
->number
);
676 * Called by XP at the time of channel connection unregistration to cause
677 * XPC to teardown all current connections for the specified channel.
679 * Before returning xpc_initiate_disconnect() will wait until all connections
680 * on the specified channel have been closed/torndown. So the caller can be
681 * assured that they will not be receiving any more callouts from XPC to the
682 * function they registered via xpc_connect().
686 * ch_number - channel # to unregister.
689 xpc_initiate_disconnect(int ch_number
)
691 unsigned long irq_flags
;
693 struct xpc_partition
*part
;
694 struct xpc_channel
*ch
;
696 DBUG_ON(ch_number
< 0 || ch_number
>= XPC_MAX_NCHANNELS
);
698 /* initiate the channel disconnect for every active partition */
699 for (partid
= 0; partid
< xp_max_npartitions
; partid
++) {
700 part
= &xpc_partitions
[partid
];
702 if (xpc_part_ref(part
)) {
703 ch
= &part
->channels
[ch_number
];
704 xpc_msgqueue_ref(ch
);
706 spin_lock_irqsave(&ch
->lock
, irq_flags
);
708 if (!(ch
->flags
& XPC_C_DISCONNECTED
)) {
709 ch
->flags
|= XPC_C_WDISCONNECT
;
711 XPC_DISCONNECT_CHANNEL(ch
, xpUnregistering
,
715 spin_unlock_irqrestore(&ch
->lock
, irq_flags
);
717 xpc_msgqueue_deref(ch
);
718 xpc_part_deref(part
);
722 xpc_disconnect_wait(ch_number
);
726 * To disconnect a channel, and reflect it back to all who may be waiting.
728 * An OPEN is not allowed until XPC_C_DISCONNECTING is cleared by
729 * xpc_process_disconnect(), and if set, XPC_C_WDISCONNECT is cleared by
730 * xpc_disconnect_wait().
732 * THE CHANNEL IS TO BE LOCKED BY THE CALLER AND WILL REMAIN LOCKED UPON RETURN.
735 xpc_disconnect_channel(const int line
, struct xpc_channel
*ch
,
736 enum xp_retval reason
, unsigned long *irq_flags
)
738 u32 channel_was_connected
= (ch
->flags
& XPC_C_CONNECTED
);
740 DBUG_ON(!spin_is_locked(&ch
->lock
));
742 if (ch
->flags
& (XPC_C_DISCONNECTING
| XPC_C_DISCONNECTED
))
745 DBUG_ON(!(ch
->flags
& (XPC_C_CONNECTING
| XPC_C_CONNECTED
)));
747 dev_dbg(xpc_chan
, "reason=%d, line=%d, partid=%d, channel=%d\n",
748 reason
, line
, ch
->partid
, ch
->number
);
750 XPC_SET_REASON(ch
, reason
, line
);
752 ch
->flags
|= (XPC_C_CLOSEREQUEST
| XPC_C_DISCONNECTING
);
753 /* some of these may not have been set */
754 ch
->flags
&= ~(XPC_C_OPENREQUEST
| XPC_C_OPENREPLY
|
755 XPC_C_ROPENREQUEST
| XPC_C_ROPENREPLY
|
756 XPC_C_CONNECTING
| XPC_C_CONNECTED
);
758 xpc_send_chctl_closerequest(ch
, irq_flags
);
760 if (channel_was_connected
)
761 ch
->flags
|= XPC_C_WASCONNECTED
;
763 spin_unlock_irqrestore(&ch
->lock
, *irq_flags
);
765 /* wake all idle kthreads so they can exit */
766 if (atomic_read(&ch
->kthreads_idle
) > 0) {
767 wake_up_all(&ch
->idle_wq
);
769 } else if ((ch
->flags
& XPC_C_CONNECTEDCALLOUT_MADE
) &&
770 !(ch
->flags
& XPC_C_DISCONNECTINGCALLOUT
)) {
771 /* start a kthread that will do the xpDisconnecting callout */
772 xpc_create_kthreads(ch
, 1, 1);
775 /* wake those waiting to allocate an entry from the local msg queue */
776 if (atomic_read(&ch
->n_on_msg_allocate_wq
) > 0)
777 wake_up(&ch
->msg_allocate_wq
);
779 spin_lock_irqsave(&ch
->lock
, *irq_flags
);
783 xpc_disconnect_callout(struct xpc_channel
*ch
, enum xp_retval reason
)
786 * Let the channel's registerer know that the channel is being
787 * disconnected. We don't want to do this if the registerer was never
788 * informed of a connection being made.
791 if (ch
->func
!= NULL
) {
792 dev_dbg(xpc_chan
, "ch->func() called, reason=%d, partid=%d, "
793 "channel=%d\n", reason
, ch
->partid
, ch
->number
);
795 ch
->func(reason
, ch
->partid
, ch
->number
, NULL
, ch
->key
);
797 dev_dbg(xpc_chan
, "ch->func() returned, reason=%d, partid=%d, "
798 "channel=%d\n", reason
, ch
->partid
, ch
->number
);
803 * Wait for a message entry to become available for the specified channel,
804 * but don't wait any longer than 1 jiffy.
807 xpc_allocate_msg_wait(struct xpc_channel
*ch
)
811 if (ch
->flags
& XPC_C_DISCONNECTING
) {
812 DBUG_ON(ch
->reason
== xpInterrupted
);
816 atomic_inc(&ch
->n_on_msg_allocate_wq
);
817 ret
= interruptible_sleep_on_timeout(&ch
->msg_allocate_wq
, 1);
818 atomic_dec(&ch
->n_on_msg_allocate_wq
);
820 if (ch
->flags
& XPC_C_DISCONNECTING
) {
822 DBUG_ON(ch
->reason
== xpInterrupted
);
823 } else if (ret
== 0) {
833 * Send a message that contains the user's payload on the specified channel
834 * connected to the specified partition.
836 * NOTE that this routine can sleep waiting for a message entry to become
837 * available. To not sleep, pass in the XPC_NOWAIT flag.
839 * Once sent, this routine will not wait for the message to be received, nor
840 * will notification be given when it does happen.
844 * partid - ID of partition to which the channel is connected.
845 * ch_number - channel # to send message on.
846 * flags - see xp.h for valid flags.
847 * payload - pointer to the payload which is to be sent.
848 * payload_size - size of the payload in bytes.
851 xpc_initiate_send(short partid
, int ch_number
, u32 flags
, void *payload
,
854 struct xpc_partition
*part
= &xpc_partitions
[partid
];
855 enum xp_retval ret
= xpUnknownReason
;
857 dev_dbg(xpc_chan
, "payload=0x%p, partid=%d, channel=%d\n", payload
,
860 DBUG_ON(partid
< 0 || partid
>= xp_max_npartitions
);
861 DBUG_ON(ch_number
< 0 || ch_number
>= part
->nchannels
);
862 DBUG_ON(payload
== NULL
);
864 if (xpc_part_ref(part
)) {
865 ret
= xpc_send_payload(&part
->channels
[ch_number
], flags
,
866 payload
, payload_size
, 0, NULL
, NULL
);
867 xpc_part_deref(part
);
874 * Send a message that contains the user's payload on the specified channel
875 * connected to the specified partition.
877 * NOTE that this routine can sleep waiting for a message entry to become
878 * available. To not sleep, pass in the XPC_NOWAIT flag.
880 * This routine will not wait for the message to be sent or received.
882 * Once the remote end of the channel has received the message, the function
883 * passed as an argument to xpc_initiate_send_notify() will be called. This
884 * allows the sender to free up or re-use any buffers referenced by the
885 * message, but does NOT mean the message has been processed at the remote
888 * If this routine returns an error, the caller's function will NOT be called.
892 * partid - ID of partition to which the channel is connected.
893 * ch_number - channel # to send message on.
894 * flags - see xp.h for valid flags.
895 * payload - pointer to the payload which is to be sent.
896 * payload_size - size of the payload in bytes.
897 * func - function to call with asynchronous notification of message
898 * receipt. THIS FUNCTION MUST BE NON-BLOCKING.
899 * key - user-defined key to be passed to the function when it's called.
902 xpc_initiate_send_notify(short partid
, int ch_number
, u32 flags
, void *payload
,
903 u16 payload_size
, xpc_notify_func func
, void *key
)
905 struct xpc_partition
*part
= &xpc_partitions
[partid
];
906 enum xp_retval ret
= xpUnknownReason
;
908 dev_dbg(xpc_chan
, "payload=0x%p, partid=%d, channel=%d\n", payload
,
911 DBUG_ON(partid
< 0 || partid
>= xp_max_npartitions
);
912 DBUG_ON(ch_number
< 0 || ch_number
>= part
->nchannels
);
913 DBUG_ON(payload
== NULL
);
914 DBUG_ON(func
== NULL
);
916 if (xpc_part_ref(part
)) {
917 ret
= xpc_send_payload(&part
->channels
[ch_number
], flags
,
918 payload
, payload_size
, XPC_N_CALL
, func
,
920 xpc_part_deref(part
);
926 * Deliver a message's payload to its intended recipient.
929 xpc_deliver_payload(struct xpc_channel
*ch
)
933 payload
= xpc_get_deliverable_payload(ch
);
934 if (payload
!= NULL
) {
937 * This ref is taken to protect the payload itself from being
938 * freed before the user is finished with it, which the user
939 * indicates by calling xpc_initiate_received().
941 xpc_msgqueue_ref(ch
);
943 atomic_inc(&ch
->kthreads_active
);
945 if (ch
->func
!= NULL
) {
946 dev_dbg(xpc_chan
, "ch->func() called, payload=0x%p "
947 "partid=%d channel=%d\n", payload
, ch
->partid
,
950 /* deliver the message to its intended recipient */
951 ch
->func(xpMsgReceived
, ch
->partid
, ch
->number
, payload
,
954 dev_dbg(xpc_chan
, "ch->func() returned, payload=0x%p "
955 "partid=%d channel=%d\n", payload
, ch
->partid
,
959 atomic_dec(&ch
->kthreads_active
);
964 * Acknowledge receipt of a delivered message's payload.
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_payload().
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
975 * xpc_initiate_send() or xpc_initiate_send_notify().
978 xpc_initiate_received(short partid
, int ch_number
, void *payload
)
980 struct xpc_partition
*part
= &xpc_partitions
[partid
];
981 struct xpc_channel
*ch
;
983 DBUG_ON(partid
< 0 || partid
>= xp_max_npartitions
);
984 DBUG_ON(ch_number
< 0 || ch_number
>= part
->nchannels
);
986 ch
= &part
->channels
[ch_number
];
987 xpc_received_payload(ch
, payload
);
989 /* the call to xpc_msgqueue_ref() was done by xpc_deliver_payload() */
990 xpc_msgqueue_deref(ch
);