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-2006 Silicon Graphics, Inc. All Rights Reserved.
11 * Cross Partition Communication (XPC) channel support.
13 * This is the part of XPC that manages the channels and
14 * sends/receives messages across them to/from other partitions.
19 #include <linux/kernel.h>
20 #include <linux/init.h>
21 #include <linux/sched.h>
22 #include <linux/cache.h>
23 #include <linux/interrupt.h>
24 #include <linux/mutex.h>
25 #include <linux/completion.h>
26 #include <asm/sn/bte.h>
27 #include <asm/sn/sn_sal.h>
28 #include <asm/sn/xpc.h>
32 * Guarantee that the kzalloc'd memory is cacheline aligned.
35 xpc_kzalloc_cacheline_aligned(size_t size, gfp_t flags, void **base)
37 /* see if kzalloc will give us cachline aligned memory by default */
38 *base = kzalloc(size, flags);
42 if ((u64) *base == L1_CACHE_ALIGN((u64) *base)) {
47 /* nope, we'll have to do it ourselves */
48 *base = kzalloc(size + L1_CACHE_BYTES, flags);
52 return (void *) L1_CACHE_ALIGN((u64) *base);
57 * Set up the initial values for the XPartition Communication channels.
60 xpc_initialize_channels(struct xpc_partition *part, partid_t partid)
63 struct xpc_channel *ch;
66 for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
67 ch = &part->channels[ch_number];
70 ch->number = ch_number;
71 ch->flags = XPC_C_DISCONNECTED;
73 ch->local_GP = &part->local_GPs[ch_number];
74 ch->local_openclose_args =
75 &part->local_openclose_args[ch_number];
77 atomic_set(&ch->kthreads_assigned, 0);
78 atomic_set(&ch->kthreads_idle, 0);
79 atomic_set(&ch->kthreads_active, 0);
81 atomic_set(&ch->references, 0);
82 atomic_set(&ch->n_to_notify, 0);
84 spin_lock_init(&ch->lock);
85 mutex_init(&ch->msg_to_pull_mutex);
86 init_completion(&ch->wdisconnect_wait);
88 atomic_set(&ch->n_on_msg_allocate_wq, 0);
89 init_waitqueue_head(&ch->msg_allocate_wq);
90 init_waitqueue_head(&ch->idle_wq);
96 * Setup the infrastructure necessary to support XPartition Communication
97 * between the specified remote partition and the local one.
100 xpc_setup_infrastructure(struct xpc_partition *part)
103 struct timer_list *timer;
104 partid_t partid = XPC_PARTID(part);
108 * Zero out MOST of the entry for this partition. Only the fields
109 * starting with `nchannels' will be zeroed. The preceding fields must
110 * remain `viable' across partition ups and downs, since they may be
111 * referenced during this memset() operation.
113 memset(&part->nchannels, 0, sizeof(struct xpc_partition) -
114 offsetof(struct xpc_partition, nchannels));
117 * Allocate all of the channel structures as a contiguous chunk of
120 part->channels = kzalloc(sizeof(struct xpc_channel) * XPC_NCHANNELS,
122 if (part->channels == NULL) {
123 dev_err(xpc_chan, "can't get memory for channels\n");
127 part->nchannels = XPC_NCHANNELS;
130 /* allocate all the required GET/PUT values */
132 part->local_GPs = xpc_kzalloc_cacheline_aligned(XPC_GP_SIZE,
133 GFP_KERNEL, &part->local_GPs_base);
134 if (part->local_GPs == NULL) {
135 kfree(part->channels);
136 part->channels = NULL;
137 dev_err(xpc_chan, "can't get memory for local get/put "
142 part->remote_GPs = xpc_kzalloc_cacheline_aligned(XPC_GP_SIZE,
143 GFP_KERNEL, &part->remote_GPs_base);
144 if (part->remote_GPs == NULL) {
145 dev_err(xpc_chan, "can't get memory for remote get/put "
147 kfree(part->local_GPs_base);
148 part->local_GPs = NULL;
149 kfree(part->channels);
150 part->channels = NULL;
155 /* allocate all the required open and close args */
157 part->local_openclose_args = xpc_kzalloc_cacheline_aligned(
158 XPC_OPENCLOSE_ARGS_SIZE, GFP_KERNEL,
159 &part->local_openclose_args_base);
160 if (part->local_openclose_args == NULL) {
161 dev_err(xpc_chan, "can't get memory for local connect args\n");
162 kfree(part->remote_GPs_base);
163 part->remote_GPs = NULL;
164 kfree(part->local_GPs_base);
165 part->local_GPs = NULL;
166 kfree(part->channels);
167 part->channels = NULL;
171 part->remote_openclose_args = xpc_kzalloc_cacheline_aligned(
172 XPC_OPENCLOSE_ARGS_SIZE, GFP_KERNEL,
173 &part->remote_openclose_args_base);
174 if (part->remote_openclose_args == NULL) {
175 dev_err(xpc_chan, "can't get memory for remote connect args\n");
176 kfree(part->local_openclose_args_base);
177 part->local_openclose_args = NULL;
178 kfree(part->remote_GPs_base);
179 part->remote_GPs = NULL;
180 kfree(part->local_GPs_base);
181 part->local_GPs = NULL;
182 kfree(part->channels);
183 part->channels = NULL;
188 xpc_initialize_channels(part, partid);
190 atomic_set(&part->nchannels_active, 0);
191 atomic_set(&part->nchannels_engaged, 0);
194 /* local_IPI_amo were set to 0 by an earlier memset() */
196 /* Initialize this partitions AMO_t structure */
197 part->local_IPI_amo_va = xpc_IPI_init(partid);
199 spin_lock_init(&part->IPI_lock);
201 atomic_set(&part->channel_mgr_requests, 1);
202 init_waitqueue_head(&part->channel_mgr_wq);
204 sprintf(part->IPI_owner, "xpc%02d", partid);
205 ret = request_irq(SGI_XPC_NOTIFY, xpc_notify_IRQ_handler, IRQF_SHARED,
206 part->IPI_owner, (void *) (u64) partid);
208 dev_err(xpc_chan, "can't register NOTIFY IRQ handler, "
210 kfree(part->remote_openclose_args_base);
211 part->remote_openclose_args = NULL;
212 kfree(part->local_openclose_args_base);
213 part->local_openclose_args = NULL;
214 kfree(part->remote_GPs_base);
215 part->remote_GPs = NULL;
216 kfree(part->local_GPs_base);
217 part->local_GPs = NULL;
218 kfree(part->channels);
219 part->channels = NULL;
220 return xpcLackOfResources;
223 /* Setup a timer to check for dropped IPIs */
224 timer = &part->dropped_IPI_timer;
226 timer->function = (void (*)(unsigned long)) xpc_dropped_IPI_check;
227 timer->data = (unsigned long) part;
228 timer->expires = jiffies + XPC_P_DROPPED_IPI_WAIT;
232 * With the setting of the partition setup_state to XPC_P_SETUP, we're
233 * declaring that this partition is ready to go.
235 part->setup_state = XPC_P_SETUP;
239 * Setup the per partition specific variables required by the
240 * remote partition to establish channel connections with us.
242 * The setting of the magic # indicates that these per partition
243 * specific variables are ready to be used.
245 xpc_vars_part[partid].GPs_pa = __pa(part->local_GPs);
246 xpc_vars_part[partid].openclose_args_pa =
247 __pa(part->local_openclose_args);
248 xpc_vars_part[partid].IPI_amo_pa = __pa(part->local_IPI_amo_va);
249 cpuid = raw_smp_processor_id(); /* any CPU in this partition will do */
250 xpc_vars_part[partid].IPI_nasid = cpuid_to_nasid(cpuid);
251 xpc_vars_part[partid].IPI_phys_cpuid = cpu_physical_id(cpuid);
252 xpc_vars_part[partid].nchannels = part->nchannels;
253 xpc_vars_part[partid].magic = XPC_VP_MAGIC1;
260 * Create a wrapper that hides the underlying mechanism for pulling a cacheline
261 * (or multiple cachelines) from a remote partition.
263 * src must be a cacheline aligned physical address on the remote partition.
264 * dst must be a cacheline aligned virtual address on this partition.
265 * cnt must be an cacheline sized
267 static enum xpc_retval
268 xpc_pull_remote_cachelines(struct xpc_partition *part, void *dst,
269 const void *src, size_t cnt)
271 bte_result_t bte_ret;
274 DBUG_ON((u64) src != L1_CACHE_ALIGN((u64) src));
275 DBUG_ON((u64) dst != L1_CACHE_ALIGN((u64) dst));
276 DBUG_ON(cnt != L1_CACHE_ALIGN(cnt));
278 if (part->act_state == XPC_P_DEACTIVATING) {
282 bte_ret = xp_bte_copy((u64) src, (u64) dst, (u64) cnt,
283 (BTE_NORMAL | BTE_WACQUIRE), NULL);
284 if (bte_ret == BTE_SUCCESS) {
288 dev_dbg(xpc_chan, "xp_bte_copy() from partition %d failed, ret=%d\n",
289 XPC_PARTID(part), bte_ret);
291 return xpc_map_bte_errors(bte_ret);
296 * Pull the remote per partititon specific variables from the specified
300 xpc_pull_remote_vars_part(struct xpc_partition *part)
302 u8 buffer[L1_CACHE_BYTES * 2];
303 struct xpc_vars_part *pulled_entry_cacheline =
304 (struct xpc_vars_part *) L1_CACHE_ALIGN((u64) buffer);
305 struct xpc_vars_part *pulled_entry;
306 u64 remote_entry_cacheline_pa, remote_entry_pa;
307 partid_t partid = XPC_PARTID(part);
311 /* pull the cacheline that contains the variables we're interested in */
313 DBUG_ON(part->remote_vars_part_pa !=
314 L1_CACHE_ALIGN(part->remote_vars_part_pa));
315 DBUG_ON(sizeof(struct xpc_vars_part) != L1_CACHE_BYTES / 2);
317 remote_entry_pa = part->remote_vars_part_pa +
318 sn_partition_id * sizeof(struct xpc_vars_part);
320 remote_entry_cacheline_pa = (remote_entry_pa & ~(L1_CACHE_BYTES - 1));
322 pulled_entry = (struct xpc_vars_part *) ((u64) pulled_entry_cacheline +
323 (remote_entry_pa & (L1_CACHE_BYTES - 1)));
325 ret = xpc_pull_remote_cachelines(part, pulled_entry_cacheline,
326 (void *) remote_entry_cacheline_pa,
328 if (ret != xpcSuccess) {
329 dev_dbg(xpc_chan, "failed to pull XPC vars_part from "
330 "partition %d, ret=%d\n", partid, ret);
335 /* see if they've been set up yet */
337 if (pulled_entry->magic != XPC_VP_MAGIC1 &&
338 pulled_entry->magic != XPC_VP_MAGIC2) {
340 if (pulled_entry->magic != 0) {
341 dev_dbg(xpc_chan, "partition %d's XPC vars_part for "
342 "partition %d has bad magic value (=0x%lx)\n",
343 partid, sn_partition_id, pulled_entry->magic);
347 /* they've not been initialized yet */
351 if (xpc_vars_part[partid].magic == XPC_VP_MAGIC1) {
353 /* validate the variables */
355 if (pulled_entry->GPs_pa == 0 ||
356 pulled_entry->openclose_args_pa == 0 ||
357 pulled_entry->IPI_amo_pa == 0) {
359 dev_err(xpc_chan, "partition %d's XPC vars_part for "
360 "partition %d are not valid\n", partid,
362 return xpcInvalidAddress;
365 /* the variables we imported look to be valid */
367 part->remote_GPs_pa = pulled_entry->GPs_pa;
368 part->remote_openclose_args_pa =
369 pulled_entry->openclose_args_pa;
370 part->remote_IPI_amo_va =
371 (AMO_t *) __va(pulled_entry->IPI_amo_pa);
372 part->remote_IPI_nasid = pulled_entry->IPI_nasid;
373 part->remote_IPI_phys_cpuid = pulled_entry->IPI_phys_cpuid;
375 if (part->nchannels > pulled_entry->nchannels) {
376 part->nchannels = pulled_entry->nchannels;
379 /* let the other side know that we've pulled their variables */
381 xpc_vars_part[partid].magic = XPC_VP_MAGIC2;
384 if (pulled_entry->magic == XPC_VP_MAGIC1) {
393 * Get the IPI flags and pull the openclose args and/or remote GPs as needed.
396 xpc_get_IPI_flags(struct xpc_partition *part)
398 unsigned long irq_flags;
404 * See if there are any IPI flags to be handled.
407 spin_lock_irqsave(&part->IPI_lock, irq_flags);
408 if ((IPI_amo = part->local_IPI_amo) != 0) {
409 part->local_IPI_amo = 0;
411 spin_unlock_irqrestore(&part->IPI_lock, irq_flags);
414 if (XPC_ANY_OPENCLOSE_IPI_FLAGS_SET(IPI_amo)) {
415 ret = xpc_pull_remote_cachelines(part,
416 part->remote_openclose_args,
417 (void *) part->remote_openclose_args_pa,
418 XPC_OPENCLOSE_ARGS_SIZE);
419 if (ret != xpcSuccess) {
420 XPC_DEACTIVATE_PARTITION(part, ret);
422 dev_dbg(xpc_chan, "failed to pull openclose args from "
423 "partition %d, ret=%d\n", XPC_PARTID(part),
426 /* don't bother processing IPIs anymore */
431 if (XPC_ANY_MSG_IPI_FLAGS_SET(IPI_amo)) {
432 ret = xpc_pull_remote_cachelines(part, part->remote_GPs,
433 (void *) part->remote_GPs_pa,
435 if (ret != xpcSuccess) {
436 XPC_DEACTIVATE_PARTITION(part, ret);
438 dev_dbg(xpc_chan, "failed to pull GPs from partition "
439 "%d, ret=%d\n", XPC_PARTID(part), ret);
441 /* don't bother processing IPIs anymore */
451 * Allocate the local message queue and the notify queue.
453 static enum xpc_retval
454 xpc_allocate_local_msgqueue(struct xpc_channel *ch)
456 unsigned long irq_flags;
461 // >>> may want to check for ch->flags & XPC_C_DISCONNECTING between
462 // >>> iterations of the for-loop, bail if set?
464 // >>> should we impose a minumum #of entries? like 4 or 8?
465 for (nentries = ch->local_nentries; nentries > 0; nentries--) {
467 nbytes = nentries * ch->msg_size;
468 ch->local_msgqueue = xpc_kzalloc_cacheline_aligned(nbytes,
470 &ch->local_msgqueue_base);
471 if (ch->local_msgqueue == NULL) {
475 nbytes = nentries * sizeof(struct xpc_notify);
476 ch->notify_queue = kzalloc(nbytes, GFP_KERNEL);
477 if (ch->notify_queue == NULL) {
478 kfree(ch->local_msgqueue_base);
479 ch->local_msgqueue = NULL;
483 spin_lock_irqsave(&ch->lock, irq_flags);
484 if (nentries < ch->local_nentries) {
485 dev_dbg(xpc_chan, "nentries=%d local_nentries=%d, "
486 "partid=%d, channel=%d\n", nentries,
487 ch->local_nentries, ch->partid, ch->number);
489 ch->local_nentries = nentries;
491 spin_unlock_irqrestore(&ch->lock, irq_flags);
495 dev_dbg(xpc_chan, "can't get memory for local message queue and notify "
496 "queue, partid=%d, channel=%d\n", ch->partid, ch->number);
502 * Allocate the cached remote message queue.
504 static enum xpc_retval
505 xpc_allocate_remote_msgqueue(struct xpc_channel *ch)
507 unsigned long irq_flags;
512 DBUG_ON(ch->remote_nentries <= 0);
514 // >>> may want to check for ch->flags & XPC_C_DISCONNECTING between
515 // >>> iterations of the for-loop, bail if set?
517 // >>> should we impose a minumum #of entries? like 4 or 8?
518 for (nentries = ch->remote_nentries; nentries > 0; nentries--) {
520 nbytes = nentries * ch->msg_size;
521 ch->remote_msgqueue = xpc_kzalloc_cacheline_aligned(nbytes,
523 &ch->remote_msgqueue_base);
524 if (ch->remote_msgqueue == NULL) {
528 spin_lock_irqsave(&ch->lock, irq_flags);
529 if (nentries < ch->remote_nentries) {
530 dev_dbg(xpc_chan, "nentries=%d remote_nentries=%d, "
531 "partid=%d, channel=%d\n", nentries,
532 ch->remote_nentries, ch->partid, ch->number);
534 ch->remote_nentries = nentries;
536 spin_unlock_irqrestore(&ch->lock, irq_flags);
540 dev_dbg(xpc_chan, "can't get memory for cached remote message queue, "
541 "partid=%d, channel=%d\n", ch->partid, ch->number);
547 * Allocate message queues and other stuff associated with a channel.
549 * Note: Assumes all of the channel sizes are filled in.
551 static enum xpc_retval
552 xpc_allocate_msgqueues(struct xpc_channel *ch)
554 unsigned long irq_flags;
558 DBUG_ON(ch->flags & XPC_C_SETUP);
560 if ((ret = xpc_allocate_local_msgqueue(ch)) != xpcSuccess) {
564 if ((ret = xpc_allocate_remote_msgqueue(ch)) != xpcSuccess) {
565 kfree(ch->local_msgqueue_base);
566 ch->local_msgqueue = NULL;
567 kfree(ch->notify_queue);
568 ch->notify_queue = NULL;
572 spin_lock_irqsave(&ch->lock, irq_flags);
573 ch->flags |= XPC_C_SETUP;
574 spin_unlock_irqrestore(&ch->lock, irq_flags);
581 * Process a connect message from a remote partition.
583 * Note: xpc_process_connect() is expecting to be called with the
584 * spin_lock_irqsave held and will leave it locked upon return.
587 xpc_process_connect(struct xpc_channel *ch, unsigned long *irq_flags)
592 DBUG_ON(!spin_is_locked(&ch->lock));
594 if (!(ch->flags & XPC_C_OPENREQUEST) ||
595 !(ch->flags & XPC_C_ROPENREQUEST)) {
596 /* nothing more to do for now */
599 DBUG_ON(!(ch->flags & XPC_C_CONNECTING));
601 if (!(ch->flags & XPC_C_SETUP)) {
602 spin_unlock_irqrestore(&ch->lock, *irq_flags);
603 ret = xpc_allocate_msgqueues(ch);
604 spin_lock_irqsave(&ch->lock, *irq_flags);
606 if (ret != xpcSuccess) {
607 XPC_DISCONNECT_CHANNEL(ch, ret, irq_flags);
609 if (ch->flags & (XPC_C_CONNECTED | XPC_C_DISCONNECTING)) {
613 DBUG_ON(!(ch->flags & XPC_C_SETUP));
614 DBUG_ON(ch->local_msgqueue == NULL);
615 DBUG_ON(ch->remote_msgqueue == NULL);
618 if (!(ch->flags & XPC_C_OPENREPLY)) {
619 ch->flags |= XPC_C_OPENREPLY;
620 xpc_IPI_send_openreply(ch, irq_flags);
623 if (!(ch->flags & XPC_C_ROPENREPLY)) {
627 DBUG_ON(ch->remote_msgqueue_pa == 0);
629 ch->flags = (XPC_C_CONNECTED | XPC_C_SETUP); /* clear all else */
631 dev_info(xpc_chan, "channel %d to partition %d connected\n",
632 ch->number, ch->partid);
634 spin_unlock_irqrestore(&ch->lock, *irq_flags);
635 xpc_create_kthreads(ch, 1);
636 spin_lock_irqsave(&ch->lock, *irq_flags);
641 * Notify those who wanted to be notified upon delivery of their message.
644 xpc_notify_senders(struct xpc_channel *ch, enum xpc_retval reason, s64 put)
646 struct xpc_notify *notify;
648 s64 get = ch->w_remote_GP.get - 1;
651 while (++get < put && atomic_read(&ch->n_to_notify) > 0) {
653 notify = &ch->notify_queue[get % ch->local_nentries];
656 * See if the notify entry indicates it was associated with
657 * a message who's sender wants to be notified. It is possible
658 * that it is, but someone else is doing or has done the
661 notify_type = notify->type;
662 if (notify_type == 0 ||
663 cmpxchg(¬ify->type, notify_type, 0) !=
668 DBUG_ON(notify_type != XPC_N_CALL);
670 atomic_dec(&ch->n_to_notify);
672 if (notify->func != NULL) {
673 dev_dbg(xpc_chan, "notify->func() called, notify=0x%p, "
674 "msg_number=%ld, partid=%d, channel=%d\n",
675 (void *) notify, get, ch->partid, ch->number);
677 notify->func(reason, ch->partid, ch->number,
680 dev_dbg(xpc_chan, "notify->func() returned, "
681 "notify=0x%p, msg_number=%ld, partid=%d, "
682 "channel=%d\n", (void *) notify, get,
683 ch->partid, ch->number);
690 * Free up message queues and other stuff that were allocated for the specified
693 * Note: ch->reason and ch->reason_line are left set for debugging purposes,
694 * they're cleared when XPC_C_DISCONNECTED is cleared.
697 xpc_free_msgqueues(struct xpc_channel *ch)
699 DBUG_ON(!spin_is_locked(&ch->lock));
700 DBUG_ON(atomic_read(&ch->n_to_notify) != 0);
702 ch->remote_msgqueue_pa = 0;
706 ch->local_nentries = 0;
707 ch->remote_nentries = 0;
708 ch->kthreads_assigned_limit = 0;
709 ch->kthreads_idle_limit = 0;
711 ch->local_GP->get = 0;
712 ch->local_GP->put = 0;
713 ch->remote_GP.get = 0;
714 ch->remote_GP.put = 0;
715 ch->w_local_GP.get = 0;
716 ch->w_local_GP.put = 0;
717 ch->w_remote_GP.get = 0;
718 ch->w_remote_GP.put = 0;
719 ch->next_msg_to_pull = 0;
721 if (ch->flags & XPC_C_SETUP) {
722 ch->flags &= ~XPC_C_SETUP;
724 dev_dbg(xpc_chan, "ch->flags=0x%x, partid=%d, channel=%d\n",
725 ch->flags, ch->partid, ch->number);
727 kfree(ch->local_msgqueue_base);
728 ch->local_msgqueue = NULL;
729 kfree(ch->remote_msgqueue_base);
730 ch->remote_msgqueue = NULL;
731 kfree(ch->notify_queue);
732 ch->notify_queue = NULL;
738 * spin_lock_irqsave() is expected to be held on entry.
741 xpc_process_disconnect(struct xpc_channel *ch, unsigned long *irq_flags)
743 struct xpc_partition *part = &xpc_partitions[ch->partid];
744 u32 channel_was_connected = (ch->flags & XPC_C_WASCONNECTED);
747 DBUG_ON(!spin_is_locked(&ch->lock));
749 if (!(ch->flags & XPC_C_DISCONNECTING)) {
753 DBUG_ON(!(ch->flags & XPC_C_CLOSEREQUEST));
755 /* make sure all activity has settled down first */
757 if (atomic_read(&ch->references) > 0 ||
758 ((ch->flags & XPC_C_CONNECTEDCALLOUT_MADE) &&
759 !(ch->flags & XPC_C_DISCONNECTINGCALLOUT_MADE))) {
762 DBUG_ON(atomic_read(&ch->kthreads_assigned) != 0);
764 if (part->act_state == XPC_P_DEACTIVATING) {
765 /* can't proceed until the other side disengages from us */
766 if (xpc_partition_engaged(1UL << ch->partid)) {
772 /* as long as the other side is up do the full protocol */
774 if (!(ch->flags & XPC_C_RCLOSEREQUEST)) {
778 if (!(ch->flags & XPC_C_CLOSEREPLY)) {
779 ch->flags |= XPC_C_CLOSEREPLY;
780 xpc_IPI_send_closereply(ch, irq_flags);
783 if (!(ch->flags & XPC_C_RCLOSEREPLY)) {
788 /* wake those waiting for notify completion */
789 if (atomic_read(&ch->n_to_notify) > 0) {
790 /* >>> we do callout while holding ch->lock */
791 xpc_notify_senders(ch, ch->reason, ch->w_local_GP.put);
794 /* both sides are disconnected now */
796 if (ch->flags & XPC_C_DISCONNECTINGCALLOUT_MADE) {
797 spin_unlock_irqrestore(&ch->lock, *irq_flags);
798 xpc_disconnect_callout(ch, xpcDisconnected);
799 spin_lock_irqsave(&ch->lock, *irq_flags);
802 /* it's now safe to free the channel's message queues */
803 xpc_free_msgqueues(ch);
805 /* mark disconnected, clear all other flags except XPC_C_WDISCONNECT */
806 ch->flags = (XPC_C_DISCONNECTED | (ch->flags & XPC_C_WDISCONNECT));
808 atomic_dec(&part->nchannels_active);
810 if (channel_was_connected) {
811 dev_info(xpc_chan, "channel %d to partition %d disconnected, "
812 "reason=%d\n", ch->number, ch->partid, ch->reason);
815 if (ch->flags & XPC_C_WDISCONNECT) {
816 /* we won't lose the CPU since we're holding ch->lock */
817 complete(&ch->wdisconnect_wait);
818 } else if (ch->delayed_IPI_flags) {
819 if (part->act_state != XPC_P_DEACTIVATING) {
820 /* time to take action on any delayed IPI flags */
821 spin_lock(&part->IPI_lock);
822 XPC_SET_IPI_FLAGS(part->local_IPI_amo, ch->number,
823 ch->delayed_IPI_flags);
824 spin_unlock(&part->IPI_lock);
826 ch->delayed_IPI_flags = 0;
832 * Process a change in the channel's remote connection state.
835 xpc_process_openclose_IPI(struct xpc_partition *part, int ch_number,
838 unsigned long irq_flags;
839 struct xpc_openclose_args *args =
840 &part->remote_openclose_args[ch_number];
841 struct xpc_channel *ch = &part->channels[ch_number];
842 enum xpc_retval reason;
846 spin_lock_irqsave(&ch->lock, irq_flags);
850 if ((ch->flags & XPC_C_DISCONNECTED) &&
851 (ch->flags & XPC_C_WDISCONNECT)) {
853 * Delay processing IPI flags until thread waiting disconnect
854 * has had a chance to see that the channel is disconnected.
856 ch->delayed_IPI_flags |= IPI_flags;
857 spin_unlock_irqrestore(&ch->lock, irq_flags);
862 if (IPI_flags & XPC_IPI_CLOSEREQUEST) {
864 dev_dbg(xpc_chan, "XPC_IPI_CLOSEREQUEST (reason=%d) received "
865 "from partid=%d, channel=%d\n", args->reason,
866 ch->partid, ch->number);
869 * If RCLOSEREQUEST is set, we're probably waiting for
870 * RCLOSEREPLY. We should find it and a ROPENREQUEST packed
871 * with this RCLOSEREQUEST in the IPI_flags.
874 if (ch->flags & XPC_C_RCLOSEREQUEST) {
875 DBUG_ON(!(ch->flags & XPC_C_DISCONNECTING));
876 DBUG_ON(!(ch->flags & XPC_C_CLOSEREQUEST));
877 DBUG_ON(!(ch->flags & XPC_C_CLOSEREPLY));
878 DBUG_ON(ch->flags & XPC_C_RCLOSEREPLY);
880 DBUG_ON(!(IPI_flags & XPC_IPI_CLOSEREPLY));
881 IPI_flags &= ~XPC_IPI_CLOSEREPLY;
882 ch->flags |= XPC_C_RCLOSEREPLY;
884 /* both sides have finished disconnecting */
885 xpc_process_disconnect(ch, &irq_flags);
886 DBUG_ON(!(ch->flags & XPC_C_DISCONNECTED));
890 if (ch->flags & XPC_C_DISCONNECTED) {
891 if (!(IPI_flags & XPC_IPI_OPENREQUEST)) {
892 if ((XPC_GET_IPI_FLAGS(part->local_IPI_amo,
893 ch_number) & XPC_IPI_OPENREQUEST)) {
895 DBUG_ON(ch->delayed_IPI_flags != 0);
896 spin_lock(&part->IPI_lock);
897 XPC_SET_IPI_FLAGS(part->local_IPI_amo,
899 XPC_IPI_CLOSEREQUEST);
900 spin_unlock(&part->IPI_lock);
902 spin_unlock_irqrestore(&ch->lock, irq_flags);
906 XPC_SET_REASON(ch, 0, 0);
907 ch->flags &= ~XPC_C_DISCONNECTED;
909 atomic_inc(&part->nchannels_active);
910 ch->flags |= (XPC_C_CONNECTING | XPC_C_ROPENREQUEST);
913 IPI_flags &= ~(XPC_IPI_OPENREQUEST | XPC_IPI_OPENREPLY);
916 * The meaningful CLOSEREQUEST connection state fields are:
917 * reason = reason connection is to be closed
920 ch->flags |= XPC_C_RCLOSEREQUEST;
922 if (!(ch->flags & XPC_C_DISCONNECTING)) {
923 reason = args->reason;
924 if (reason <= xpcSuccess || reason > xpcUnknownReason) {
925 reason = xpcUnknownReason;
926 } else if (reason == xpcUnregistering) {
927 reason = xpcOtherUnregistering;
930 XPC_DISCONNECT_CHANNEL(ch, reason, &irq_flags);
932 DBUG_ON(IPI_flags & XPC_IPI_CLOSEREPLY);
933 spin_unlock_irqrestore(&ch->lock, irq_flags);
937 xpc_process_disconnect(ch, &irq_flags);
941 if (IPI_flags & XPC_IPI_CLOSEREPLY) {
943 dev_dbg(xpc_chan, "XPC_IPI_CLOSEREPLY received from partid=%d,"
944 " channel=%d\n", ch->partid, ch->number);
946 if (ch->flags & XPC_C_DISCONNECTED) {
947 DBUG_ON(part->act_state != XPC_P_DEACTIVATING);
948 spin_unlock_irqrestore(&ch->lock, irq_flags);
952 DBUG_ON(!(ch->flags & XPC_C_CLOSEREQUEST));
954 if (!(ch->flags & XPC_C_RCLOSEREQUEST)) {
955 if ((XPC_GET_IPI_FLAGS(part->local_IPI_amo, ch_number)
956 & XPC_IPI_CLOSEREQUEST)) {
958 DBUG_ON(ch->delayed_IPI_flags != 0);
959 spin_lock(&part->IPI_lock);
960 XPC_SET_IPI_FLAGS(part->local_IPI_amo,
961 ch_number, XPC_IPI_CLOSEREPLY);
962 spin_unlock(&part->IPI_lock);
964 spin_unlock_irqrestore(&ch->lock, irq_flags);
968 ch->flags |= XPC_C_RCLOSEREPLY;
970 if (ch->flags & XPC_C_CLOSEREPLY) {
971 /* both sides have finished disconnecting */
972 xpc_process_disconnect(ch, &irq_flags);
977 if (IPI_flags & XPC_IPI_OPENREQUEST) {
979 dev_dbg(xpc_chan, "XPC_IPI_OPENREQUEST (msg_size=%d, "
980 "local_nentries=%d) received from partid=%d, "
981 "channel=%d\n", args->msg_size, args->local_nentries,
982 ch->partid, ch->number);
984 if (part->act_state == XPC_P_DEACTIVATING ||
985 (ch->flags & XPC_C_ROPENREQUEST)) {
986 spin_unlock_irqrestore(&ch->lock, irq_flags);
990 if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_WDISCONNECT)) {
991 ch->delayed_IPI_flags |= XPC_IPI_OPENREQUEST;
992 spin_unlock_irqrestore(&ch->lock, irq_flags);
995 DBUG_ON(!(ch->flags & (XPC_C_DISCONNECTED |
996 XPC_C_OPENREQUEST)));
997 DBUG_ON(ch->flags & (XPC_C_ROPENREQUEST | XPC_C_ROPENREPLY |
998 XPC_C_OPENREPLY | XPC_C_CONNECTED));
1001 * The meaningful OPENREQUEST connection state fields are:
1002 * msg_size = size of channel's messages in bytes
1003 * local_nentries = remote partition's local_nentries
1005 if (args->msg_size == 0 || args->local_nentries == 0) {
1006 /* assume OPENREQUEST was delayed by mistake */
1007 spin_unlock_irqrestore(&ch->lock, irq_flags);
1011 ch->flags |= (XPC_C_ROPENREQUEST | XPC_C_CONNECTING);
1012 ch->remote_nentries = args->local_nentries;
1015 if (ch->flags & XPC_C_OPENREQUEST) {
1016 if (args->msg_size != ch->msg_size) {
1017 XPC_DISCONNECT_CHANNEL(ch, xpcUnequalMsgSizes,
1019 spin_unlock_irqrestore(&ch->lock, irq_flags);
1023 ch->msg_size = args->msg_size;
1025 XPC_SET_REASON(ch, 0, 0);
1026 ch->flags &= ~XPC_C_DISCONNECTED;
1028 atomic_inc(&part->nchannels_active);
1031 xpc_process_connect(ch, &irq_flags);
1035 if (IPI_flags & XPC_IPI_OPENREPLY) {
1037 dev_dbg(xpc_chan, "XPC_IPI_OPENREPLY (local_msgqueue_pa=0x%lx, "
1038 "local_nentries=%d, remote_nentries=%d) received from "
1039 "partid=%d, channel=%d\n", args->local_msgqueue_pa,
1040 args->local_nentries, args->remote_nentries,
1041 ch->partid, ch->number);
1043 if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_DISCONNECTED)) {
1044 spin_unlock_irqrestore(&ch->lock, irq_flags);
1047 if (!(ch->flags & XPC_C_OPENREQUEST)) {
1048 XPC_DISCONNECT_CHANNEL(ch, xpcOpenCloseError,
1050 spin_unlock_irqrestore(&ch->lock, irq_flags);
1054 DBUG_ON(!(ch->flags & XPC_C_ROPENREQUEST));
1055 DBUG_ON(ch->flags & XPC_C_CONNECTED);
1058 * The meaningful OPENREPLY connection state fields are:
1059 * local_msgqueue_pa = physical address of remote
1060 * partition's local_msgqueue
1061 * local_nentries = remote partition's local_nentries
1062 * remote_nentries = remote partition's remote_nentries
1064 DBUG_ON(args->local_msgqueue_pa == 0);
1065 DBUG_ON(args->local_nentries == 0);
1066 DBUG_ON(args->remote_nentries == 0);
1068 ch->flags |= XPC_C_ROPENREPLY;
1069 ch->remote_msgqueue_pa = args->local_msgqueue_pa;
1071 if (args->local_nentries < ch->remote_nentries) {
1072 dev_dbg(xpc_chan, "XPC_IPI_OPENREPLY: new "
1073 "remote_nentries=%d, old remote_nentries=%d, "
1074 "partid=%d, channel=%d\n",
1075 args->local_nentries, ch->remote_nentries,
1076 ch->partid, ch->number);
1078 ch->remote_nentries = args->local_nentries;
1080 if (args->remote_nentries < ch->local_nentries) {
1081 dev_dbg(xpc_chan, "XPC_IPI_OPENREPLY: new "
1082 "local_nentries=%d, old local_nentries=%d, "
1083 "partid=%d, channel=%d\n",
1084 args->remote_nentries, ch->local_nentries,
1085 ch->partid, ch->number);
1087 ch->local_nentries = args->remote_nentries;
1090 xpc_process_connect(ch, &irq_flags);
1093 spin_unlock_irqrestore(&ch->lock, irq_flags);
1098 * Attempt to establish a channel connection to a remote partition.
1100 static enum xpc_retval
1101 xpc_connect_channel(struct xpc_channel *ch)
1103 unsigned long irq_flags;
1104 struct xpc_registration *registration = &xpc_registrations[ch->number];
1107 if (mutex_trylock(®istration->mutex) == 0) {
1111 if (!XPC_CHANNEL_REGISTERED(ch->number)) {
1112 mutex_unlock(®istration->mutex);
1113 return xpcUnregistered;
1116 spin_lock_irqsave(&ch->lock, irq_flags);
1118 DBUG_ON(ch->flags & XPC_C_CONNECTED);
1119 DBUG_ON(ch->flags & XPC_C_OPENREQUEST);
1121 if (ch->flags & XPC_C_DISCONNECTING) {
1122 spin_unlock_irqrestore(&ch->lock, irq_flags);
1123 mutex_unlock(®istration->mutex);
1128 /* add info from the channel connect registration to the channel */
1130 ch->kthreads_assigned_limit = registration->assigned_limit;
1131 ch->kthreads_idle_limit = registration->idle_limit;
1132 DBUG_ON(atomic_read(&ch->kthreads_assigned) != 0);
1133 DBUG_ON(atomic_read(&ch->kthreads_idle) != 0);
1134 DBUG_ON(atomic_read(&ch->kthreads_active) != 0);
1136 ch->func = registration->func;
1137 DBUG_ON(registration->func == NULL);
1138 ch->key = registration->key;
1140 ch->local_nentries = registration->nentries;
1142 if (ch->flags & XPC_C_ROPENREQUEST) {
1143 if (registration->msg_size != ch->msg_size) {
1144 /* the local and remote sides aren't the same */
1147 * Because XPC_DISCONNECT_CHANNEL() can block we're
1148 * forced to up the registration sema before we unlock
1149 * the channel lock. But that's okay here because we're
1150 * done with the part that required the registration
1151 * sema. XPC_DISCONNECT_CHANNEL() requires that the
1152 * channel lock be locked and will unlock and relock
1153 * the channel lock as needed.
1155 mutex_unlock(®istration->mutex);
1156 XPC_DISCONNECT_CHANNEL(ch, xpcUnequalMsgSizes,
1158 spin_unlock_irqrestore(&ch->lock, irq_flags);
1159 return xpcUnequalMsgSizes;
1162 ch->msg_size = registration->msg_size;
1164 XPC_SET_REASON(ch, 0, 0);
1165 ch->flags &= ~XPC_C_DISCONNECTED;
1167 atomic_inc(&xpc_partitions[ch->partid].nchannels_active);
1170 mutex_unlock(®istration->mutex);
1173 /* initiate the connection */
1175 ch->flags |= (XPC_C_OPENREQUEST | XPC_C_CONNECTING);
1176 xpc_IPI_send_openrequest(ch, &irq_flags);
1178 xpc_process_connect(ch, &irq_flags);
1180 spin_unlock_irqrestore(&ch->lock, irq_flags);
1187 * Clear some of the msg flags in the local message queue.
1190 xpc_clear_local_msgqueue_flags(struct xpc_channel *ch)
1192 struct xpc_msg *msg;
1196 get = ch->w_remote_GP.get;
1198 msg = (struct xpc_msg *) ((u64) ch->local_msgqueue +
1199 (get % ch->local_nentries) * ch->msg_size);
1201 } while (++get < (volatile s64) ch->remote_GP.get);
1206 * Clear some of the msg flags in the remote message queue.
1209 xpc_clear_remote_msgqueue_flags(struct xpc_channel *ch)
1211 struct xpc_msg *msg;
1215 put = ch->w_remote_GP.put;
1217 msg = (struct xpc_msg *) ((u64) ch->remote_msgqueue +
1218 (put % ch->remote_nentries) * ch->msg_size);
1220 } while (++put < (volatile s64) ch->remote_GP.put);
1225 xpc_process_msg_IPI(struct xpc_partition *part, int ch_number)
1227 struct xpc_channel *ch = &part->channels[ch_number];
1231 ch->remote_GP = part->remote_GPs[ch_number];
1234 /* See what, if anything, has changed for each connected channel */
1236 xpc_msgqueue_ref(ch);
1238 if (ch->w_remote_GP.get == ch->remote_GP.get &&
1239 ch->w_remote_GP.put == ch->remote_GP.put) {
1240 /* nothing changed since GPs were last pulled */
1241 xpc_msgqueue_deref(ch);
1245 if (!(ch->flags & XPC_C_CONNECTED)){
1246 xpc_msgqueue_deref(ch);
1252 * First check to see if messages recently sent by us have been
1253 * received by the other side. (The remote GET value will have
1254 * changed since we last looked at it.)
1257 if (ch->w_remote_GP.get != ch->remote_GP.get) {
1260 * We need to notify any senders that want to be notified
1261 * that their sent messages have been received by their
1262 * intended recipients. We need to do this before updating
1263 * w_remote_GP.get so that we don't allocate the same message
1264 * queue entries prematurely (see xpc_allocate_msg()).
1266 if (atomic_read(&ch->n_to_notify) > 0) {
1268 * Notify senders that messages sent have been
1269 * received and delivered by the other side.
1271 xpc_notify_senders(ch, xpcMsgDelivered,
1276 * Clear msg->flags in previously sent messages, so that
1277 * they're ready for xpc_allocate_msg().
1279 xpc_clear_local_msgqueue_flags(ch);
1281 ch->w_remote_GP.get = ch->remote_GP.get;
1283 dev_dbg(xpc_chan, "w_remote_GP.get changed to %ld, partid=%d, "
1284 "channel=%d\n", ch->w_remote_GP.get, ch->partid,
1288 * If anyone was waiting for message queue entries to become
1289 * available, wake them up.
1291 if (atomic_read(&ch->n_on_msg_allocate_wq) > 0) {
1292 wake_up(&ch->msg_allocate_wq);
1298 * Now check for newly sent messages by the other side. (The remote
1299 * PUT value will have changed since we last looked at it.)
1302 if (ch->w_remote_GP.put != ch->remote_GP.put) {
1304 * Clear msg->flags in previously received messages, so that
1305 * they're ready for xpc_get_deliverable_msg().
1307 xpc_clear_remote_msgqueue_flags(ch);
1309 ch->w_remote_GP.put = ch->remote_GP.put;
1311 dev_dbg(xpc_chan, "w_remote_GP.put changed to %ld, partid=%d, "
1312 "channel=%d\n", ch->w_remote_GP.put, ch->partid,
1315 nmsgs_sent = ch->w_remote_GP.put - ch->w_local_GP.get;
1316 if (nmsgs_sent > 0) {
1317 dev_dbg(xpc_chan, "msgs waiting to be copied and "
1318 "delivered=%d, partid=%d, channel=%d\n",
1319 nmsgs_sent, ch->partid, ch->number);
1321 if (ch->flags & XPC_C_CONNECTEDCALLOUT_MADE) {
1322 xpc_activate_kthreads(ch, nmsgs_sent);
1327 xpc_msgqueue_deref(ch);
1332 xpc_process_channel_activity(struct xpc_partition *part)
1334 unsigned long irq_flags;
1335 u64 IPI_amo, IPI_flags;
1336 struct xpc_channel *ch;
1341 IPI_amo = xpc_get_IPI_flags(part);
1344 * Initiate channel connections for registered channels.
1346 * For each connected channel that has pending messages activate idle
1347 * kthreads and/or create new kthreads as needed.
1350 for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
1351 ch = &part->channels[ch_number];
1355 * Process any open or close related IPI flags, and then deal
1356 * with connecting or disconnecting the channel as required.
1359 IPI_flags = XPC_GET_IPI_FLAGS(IPI_amo, ch_number);
1361 if (XPC_ANY_OPENCLOSE_IPI_FLAGS_SET(IPI_flags)) {
1362 xpc_process_openclose_IPI(part, ch_number, IPI_flags);
1365 ch_flags = ch->flags; /* need an atomic snapshot of flags */
1367 if (ch_flags & XPC_C_DISCONNECTING) {
1368 spin_lock_irqsave(&ch->lock, irq_flags);
1369 xpc_process_disconnect(ch, &irq_flags);
1370 spin_unlock_irqrestore(&ch->lock, irq_flags);
1374 if (part->act_state == XPC_P_DEACTIVATING) {
1378 if (!(ch_flags & XPC_C_CONNECTED)) {
1379 if (!(ch_flags & XPC_C_OPENREQUEST)) {
1380 DBUG_ON(ch_flags & XPC_C_SETUP);
1381 (void) xpc_connect_channel(ch);
1383 spin_lock_irqsave(&ch->lock, irq_flags);
1384 xpc_process_connect(ch, &irq_flags);
1385 spin_unlock_irqrestore(&ch->lock, irq_flags);
1392 * Process any message related IPI flags, this may involve the
1393 * activation of kthreads to deliver any pending messages sent
1394 * from the other partition.
1397 if (XPC_ANY_MSG_IPI_FLAGS_SET(IPI_flags)) {
1398 xpc_process_msg_IPI(part, ch_number);
1405 * XPC's heartbeat code calls this function to inform XPC that a partition is
1406 * going down. XPC responds by tearing down the XPartition Communication
1407 * infrastructure used for the just downed partition.
1409 * XPC's heartbeat code will never call this function and xpc_partition_up()
1410 * at the same time. Nor will it ever make multiple calls to either function
1414 xpc_partition_going_down(struct xpc_partition *part, enum xpc_retval reason)
1416 unsigned long irq_flags;
1418 struct xpc_channel *ch;
1421 dev_dbg(xpc_chan, "deactivating partition %d, reason=%d\n",
1422 XPC_PARTID(part), reason);
1424 if (!xpc_part_ref(part)) {
1425 /* infrastructure for this partition isn't currently set up */
1430 /* disconnect channels associated with the partition going down */
1432 for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
1433 ch = &part->channels[ch_number];
1435 xpc_msgqueue_ref(ch);
1436 spin_lock_irqsave(&ch->lock, irq_flags);
1438 XPC_DISCONNECT_CHANNEL(ch, reason, &irq_flags);
1440 spin_unlock_irqrestore(&ch->lock, irq_flags);
1441 xpc_msgqueue_deref(ch);
1444 xpc_wakeup_channel_mgr(part);
1446 xpc_part_deref(part);
1451 * Teardown the infrastructure necessary to support XPartition Communication
1452 * between the specified remote partition and the local one.
1455 xpc_teardown_infrastructure(struct xpc_partition *part)
1457 partid_t partid = XPC_PARTID(part);
1461 * We start off by making this partition inaccessible to local
1462 * processes by marking it as no longer setup. Then we make it
1463 * inaccessible to remote processes by clearing the XPC per partition
1464 * specific variable's magic # (which indicates that these variables
1465 * are no longer valid) and by ignoring all XPC notify IPIs sent to
1469 DBUG_ON(atomic_read(&part->nchannels_engaged) != 0);
1470 DBUG_ON(atomic_read(&part->nchannels_active) != 0);
1471 DBUG_ON(part->setup_state != XPC_P_SETUP);
1472 part->setup_state = XPC_P_WTEARDOWN;
1474 xpc_vars_part[partid].magic = 0;
1477 free_irq(SGI_XPC_NOTIFY, (void *) (u64) partid);
1481 * Before proceding with the teardown we have to wait until all
1482 * existing references cease.
1484 wait_event(part->teardown_wq, (atomic_read(&part->references) == 0));
1487 /* now we can begin tearing down the infrastructure */
1489 part->setup_state = XPC_P_TORNDOWN;
1491 /* in case we've still got outstanding timers registered... */
1492 del_timer_sync(&part->dropped_IPI_timer);
1494 kfree(part->remote_openclose_args_base);
1495 part->remote_openclose_args = NULL;
1496 kfree(part->local_openclose_args_base);
1497 part->local_openclose_args = NULL;
1498 kfree(part->remote_GPs_base);
1499 part->remote_GPs = NULL;
1500 kfree(part->local_GPs_base);
1501 part->local_GPs = NULL;
1502 kfree(part->channels);
1503 part->channels = NULL;
1504 part->local_IPI_amo_va = NULL;
1509 * Called by XP at the time of channel connection registration to cause
1510 * XPC to establish connections to all currently active partitions.
1513 xpc_initiate_connect(int ch_number)
1516 struct xpc_partition *part;
1517 struct xpc_channel *ch;
1520 DBUG_ON(ch_number < 0 || ch_number >= XPC_NCHANNELS);
1522 for (partid = 1; partid < XP_MAX_PARTITIONS; partid++) {
1523 part = &xpc_partitions[partid];
1525 if (xpc_part_ref(part)) {
1526 ch = &part->channels[ch_number];
1529 * Initiate the establishment of a connection on the
1530 * newly registered channel to the remote partition.
1532 xpc_wakeup_channel_mgr(part);
1533 xpc_part_deref(part);
1540 xpc_connected_callout(struct xpc_channel *ch)
1542 /* let the registerer know that a connection has been established */
1544 if (ch->func != NULL) {
1545 dev_dbg(xpc_chan, "ch->func() called, reason=xpcConnected, "
1546 "partid=%d, channel=%d\n", ch->partid, ch->number);
1548 ch->func(xpcConnected, ch->partid, ch->number,
1549 (void *) (u64) ch->local_nentries, ch->key);
1551 dev_dbg(xpc_chan, "ch->func() returned, reason=xpcConnected, "
1552 "partid=%d, channel=%d\n", ch->partid, ch->number);
1558 * Called by XP at the time of channel connection unregistration to cause
1559 * XPC to teardown all current connections for the specified channel.
1561 * Before returning xpc_initiate_disconnect() will wait until all connections
1562 * on the specified channel have been closed/torndown. So the caller can be
1563 * assured that they will not be receiving any more callouts from XPC to the
1564 * function they registered via xpc_connect().
1568 * ch_number - channel # to unregister.
1571 xpc_initiate_disconnect(int ch_number)
1573 unsigned long irq_flags;
1575 struct xpc_partition *part;
1576 struct xpc_channel *ch;
1579 DBUG_ON(ch_number < 0 || ch_number >= XPC_NCHANNELS);
1581 /* initiate the channel disconnect for every active partition */
1582 for (partid = 1; partid < XP_MAX_PARTITIONS; partid++) {
1583 part = &xpc_partitions[partid];
1585 if (xpc_part_ref(part)) {
1586 ch = &part->channels[ch_number];
1587 xpc_msgqueue_ref(ch);
1589 spin_lock_irqsave(&ch->lock, irq_flags);
1591 if (!(ch->flags & XPC_C_DISCONNECTED)) {
1592 ch->flags |= XPC_C_WDISCONNECT;
1594 XPC_DISCONNECT_CHANNEL(ch, xpcUnregistering,
1598 spin_unlock_irqrestore(&ch->lock, irq_flags);
1600 xpc_msgqueue_deref(ch);
1601 xpc_part_deref(part);
1605 xpc_disconnect_wait(ch_number);
1610 * To disconnect a channel, and reflect it back to all who may be waiting.
1612 * An OPEN is not allowed until XPC_C_DISCONNECTING is cleared by
1613 * xpc_process_disconnect(), and if set, XPC_C_WDISCONNECT is cleared by
1614 * xpc_disconnect_wait().
1616 * THE CHANNEL IS TO BE LOCKED BY THE CALLER AND WILL REMAIN LOCKED UPON RETURN.
1619 xpc_disconnect_channel(const int line, struct xpc_channel *ch,
1620 enum xpc_retval reason, unsigned long *irq_flags)
1622 u32 channel_was_connected = (ch->flags & XPC_C_CONNECTED);
1625 DBUG_ON(!spin_is_locked(&ch->lock));
1627 if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_DISCONNECTED)) {
1630 DBUG_ON(!(ch->flags & (XPC_C_CONNECTING | XPC_C_CONNECTED)));
1632 dev_dbg(xpc_chan, "reason=%d, line=%d, partid=%d, channel=%d\n",
1633 reason, line, ch->partid, ch->number);
1635 XPC_SET_REASON(ch, reason, line);
1637 ch->flags |= (XPC_C_CLOSEREQUEST | XPC_C_DISCONNECTING);
1638 /* some of these may not have been set */
1639 ch->flags &= ~(XPC_C_OPENREQUEST | XPC_C_OPENREPLY |
1640 XPC_C_ROPENREQUEST | XPC_C_ROPENREPLY |
1641 XPC_C_CONNECTING | XPC_C_CONNECTED);
1643 xpc_IPI_send_closerequest(ch, irq_flags);
1645 if (channel_was_connected) {
1646 ch->flags |= XPC_C_WASCONNECTED;
1649 spin_unlock_irqrestore(&ch->lock, *irq_flags);
1651 /* wake all idle kthreads so they can exit */
1652 if (atomic_read(&ch->kthreads_idle) > 0) {
1653 wake_up_all(&ch->idle_wq);
1656 /* wake those waiting to allocate an entry from the local msg queue */
1657 if (atomic_read(&ch->n_on_msg_allocate_wq) > 0) {
1658 wake_up(&ch->msg_allocate_wq);
1661 spin_lock_irqsave(&ch->lock, *irq_flags);
1666 xpc_disconnect_callout(struct xpc_channel *ch, enum xpc_retval reason)
1669 * Let the channel's registerer know that the channel is being
1670 * disconnected. We don't want to do this if the registerer was never
1671 * informed of a connection being made.
1674 if (ch->func != NULL) {
1675 dev_dbg(xpc_chan, "ch->func() called, reason=%d, partid=%d, "
1676 "channel=%d\n", reason, ch->partid, ch->number);
1678 ch->func(reason, ch->partid, ch->number, NULL, ch->key);
1680 dev_dbg(xpc_chan, "ch->func() returned, reason=%d, partid=%d, "
1681 "channel=%d\n", reason, ch->partid, ch->number);
1687 * Wait for a message entry to become available for the specified channel,
1688 * but don't wait any longer than 1 jiffy.
1690 static enum xpc_retval
1691 xpc_allocate_msg_wait(struct xpc_channel *ch)
1693 enum xpc_retval ret;
1696 if (ch->flags & XPC_C_DISCONNECTING) {
1697 DBUG_ON(ch->reason == xpcInterrupted); // >>> Is this true?
1701 atomic_inc(&ch->n_on_msg_allocate_wq);
1702 ret = interruptible_sleep_on_timeout(&ch->msg_allocate_wq, 1);
1703 atomic_dec(&ch->n_on_msg_allocate_wq);
1705 if (ch->flags & XPC_C_DISCONNECTING) {
1707 DBUG_ON(ch->reason == xpcInterrupted); // >>> Is this true?
1708 } else if (ret == 0) {
1711 ret = xpcInterrupted;
1719 * Allocate an entry for a message from the message queue associated with the
1720 * specified channel.
1722 static enum xpc_retval
1723 xpc_allocate_msg(struct xpc_channel *ch, u32 flags,
1724 struct xpc_msg **address_of_msg)
1726 struct xpc_msg *msg;
1727 enum xpc_retval ret;
1731 /* this reference will be dropped in xpc_send_msg() */
1732 xpc_msgqueue_ref(ch);
1734 if (ch->flags & XPC_C_DISCONNECTING) {
1735 xpc_msgqueue_deref(ch);
1738 if (!(ch->flags & XPC_C_CONNECTED)) {
1739 xpc_msgqueue_deref(ch);
1740 return xpcNotConnected;
1745 * Get the next available message entry from the local message queue.
1746 * If none are available, we'll make sure that we grab the latest
1753 put = (volatile s64) ch->w_local_GP.put;
1754 if (put - (volatile s64) ch->w_remote_GP.get <
1755 ch->local_nentries) {
1757 /* There are available message entries. We need to try
1758 * to secure one for ourselves. We'll do this by trying
1759 * to increment w_local_GP.put as long as someone else
1760 * doesn't beat us to it. If they do, we'll have to
1763 if (cmpxchg(&ch->w_local_GP.put, put, put + 1) ==
1765 /* we got the entry referenced by put */
1768 continue; /* try again */
1773 * There aren't any available msg entries at this time.
1775 * In waiting for a message entry to become available,
1776 * we set a timeout in case the other side is not
1777 * sending completion IPIs. This lets us fake an IPI
1778 * that will cause the IPI handler to fetch the latest
1779 * GP values as if an IPI was sent by the other side.
1781 if (ret == xpcTimeout) {
1782 xpc_IPI_send_local_msgrequest(ch);
1785 if (flags & XPC_NOWAIT) {
1786 xpc_msgqueue_deref(ch);
1790 ret = xpc_allocate_msg_wait(ch);
1791 if (ret != xpcInterrupted && ret != xpcTimeout) {
1792 xpc_msgqueue_deref(ch);
1798 /* get the message's address and initialize it */
1799 msg = (struct xpc_msg *) ((u64) ch->local_msgqueue +
1800 (put % ch->local_nentries) * ch->msg_size);
1803 DBUG_ON(msg->flags != 0);
1806 dev_dbg(xpc_chan, "w_local_GP.put changed to %ld; msg=0x%p, "
1807 "msg_number=%ld, partid=%d, channel=%d\n", put + 1,
1808 (void *) msg, msg->number, ch->partid, ch->number);
1810 *address_of_msg = msg;
1817 * Allocate an entry for a message from the message queue associated with the
1818 * specified channel. NOTE that this routine can sleep waiting for a message
1819 * entry to become available. To not sleep, pass in the XPC_NOWAIT flag.
1823 * partid - ID of partition to which the channel is connected.
1824 * ch_number - channel #.
1825 * flags - see xpc.h for valid flags.
1826 * payload - address of the allocated payload area pointer (filled in on
1827 * return) in which the user-defined message is constructed.
1830 xpc_initiate_allocate(partid_t partid, int ch_number, u32 flags, void **payload)
1832 struct xpc_partition *part = &xpc_partitions[partid];
1833 enum xpc_retval ret = xpcUnknownReason;
1834 struct xpc_msg *msg = NULL;
1837 DBUG_ON(partid <= 0 || partid >= XP_MAX_PARTITIONS);
1838 DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
1842 if (xpc_part_ref(part)) {
1843 ret = xpc_allocate_msg(&part->channels[ch_number], flags, &msg);
1844 xpc_part_deref(part);
1847 *payload = &msg->payload;
1856 * Now we actually send the messages that are ready to be sent by advancing
1857 * the local message queue's Put value and then send an IPI to the recipient
1861 xpc_send_msgs(struct xpc_channel *ch, s64 initial_put)
1863 struct xpc_msg *msg;
1864 s64 put = initial_put + 1;
1871 if (put == (volatile s64) ch->w_local_GP.put) {
1875 msg = (struct xpc_msg *) ((u64) ch->local_msgqueue +
1876 (put % ch->local_nentries) * ch->msg_size);
1878 if (!(msg->flags & XPC_M_READY)) {
1885 if (put == initial_put) {
1886 /* nothing's changed */
1890 if (cmpxchg_rel(&ch->local_GP->put, initial_put, put) !=
1892 /* someone else beat us to it */
1893 DBUG_ON((volatile s64) ch->local_GP->put < initial_put);
1897 /* we just set the new value of local_GP->put */
1899 dev_dbg(xpc_chan, "local_GP->put changed to %ld, partid=%d, "
1900 "channel=%d\n", put, ch->partid, ch->number);
1905 * We need to ensure that the message referenced by
1906 * local_GP->put is not XPC_M_READY or that local_GP->put
1907 * equals w_local_GP.put, so we'll go have a look.
1913 xpc_IPI_send_msgrequest(ch);
1919 * Common code that does the actual sending of the message by advancing the
1920 * local message queue's Put value and sends an IPI to the partition the
1921 * message is being sent to.
1923 static enum xpc_retval
1924 xpc_send_msg(struct xpc_channel *ch, struct xpc_msg *msg, u8 notify_type,
1925 xpc_notify_func func, void *key)
1927 enum xpc_retval ret = xpcSuccess;
1928 struct xpc_notify *notify = notify;
1929 s64 put, msg_number = msg->number;
1932 DBUG_ON(notify_type == XPC_N_CALL && func == NULL);
1933 DBUG_ON((((u64) msg - (u64) ch->local_msgqueue) / ch->msg_size) !=
1934 msg_number % ch->local_nentries);
1935 DBUG_ON(msg->flags & XPC_M_READY);
1937 if (ch->flags & XPC_C_DISCONNECTING) {
1938 /* drop the reference grabbed in xpc_allocate_msg() */
1939 xpc_msgqueue_deref(ch);
1943 if (notify_type != 0) {
1945 * Tell the remote side to send an ACK interrupt when the
1946 * message has been delivered.
1948 msg->flags |= XPC_M_INTERRUPT;
1950 atomic_inc(&ch->n_to_notify);
1952 notify = &ch->notify_queue[msg_number % ch->local_nentries];
1953 notify->func = func;
1955 notify->type = notify_type;
1957 // >>> is a mb() needed here?
1959 if (ch->flags & XPC_C_DISCONNECTING) {
1961 * An error occurred between our last error check and
1962 * this one. We will try to clear the type field from
1963 * the notify entry. If we succeed then
1964 * xpc_disconnect_channel() didn't already process
1967 if (cmpxchg(¬ify->type, notify_type, 0) ==
1969 atomic_dec(&ch->n_to_notify);
1973 /* drop the reference grabbed in xpc_allocate_msg() */
1974 xpc_msgqueue_deref(ch);
1979 msg->flags |= XPC_M_READY;
1982 * The preceding store of msg->flags must occur before the following
1983 * load of ch->local_GP->put.
1987 /* see if the message is next in line to be sent, if so send it */
1989 put = ch->local_GP->put;
1990 if (put == msg_number) {
1991 xpc_send_msgs(ch, put);
1994 /* drop the reference grabbed in xpc_allocate_msg() */
1995 xpc_msgqueue_deref(ch);
2001 * Send a message previously allocated using xpc_initiate_allocate() on the
2002 * specified channel connected to the specified partition.
2004 * This routine will not wait for the message to be received, nor will
2005 * notification be given when it does happen. Once this routine has returned
2006 * the message entry allocated via xpc_initiate_allocate() is no longer
2007 * accessable to the caller.
2009 * This routine, although called by users, does not call xpc_part_ref() to
2010 * ensure that the partition infrastructure is in place. It relies on the
2011 * fact that we called xpc_msgqueue_ref() in xpc_allocate_msg().
2015 * partid - ID of partition to which the channel is connected.
2016 * ch_number - channel # to send message on.
2017 * payload - pointer to the payload area allocated via
2018 * xpc_initiate_allocate().
2021 xpc_initiate_send(partid_t partid, int ch_number, void *payload)
2023 struct xpc_partition *part = &xpc_partitions[partid];
2024 struct xpc_msg *msg = XPC_MSG_ADDRESS(payload);
2025 enum xpc_retval ret;
2028 dev_dbg(xpc_chan, "msg=0x%p, partid=%d, channel=%d\n", (void *) msg,
2031 DBUG_ON(partid <= 0 || partid >= XP_MAX_PARTITIONS);
2032 DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
2033 DBUG_ON(msg == NULL);
2035 ret = xpc_send_msg(&part->channels[ch_number], msg, 0, NULL, NULL);
2042 * Send a message previously allocated using xpc_initiate_allocate on the
2043 * specified channel connected to the specified partition.
2045 * This routine will not wait for the message to be sent. Once this routine
2046 * has returned the message entry allocated via xpc_initiate_allocate() is no
2047 * longer accessable to the caller.
2049 * Once the remote end of the channel has received the message, the function
2050 * passed as an argument to xpc_initiate_send_notify() will be called. This
2051 * allows the sender to free up or re-use any buffers referenced by the
2052 * message, but does NOT mean the message has been processed at the remote
2053 * end by a receiver.
2055 * If this routine returns an error, the caller's function will NOT be called.
2057 * This routine, although called by users, does not call xpc_part_ref() to
2058 * ensure that the partition infrastructure is in place. It relies on the
2059 * fact that we called xpc_msgqueue_ref() in xpc_allocate_msg().
2063 * partid - ID of partition to which the channel is connected.
2064 * ch_number - channel # to send message on.
2065 * payload - pointer to the payload area allocated via
2066 * xpc_initiate_allocate().
2067 * func - function to call with asynchronous notification of message
2068 * receipt. THIS FUNCTION MUST BE NON-BLOCKING.
2069 * key - user-defined key to be passed to the function when it's called.
2072 xpc_initiate_send_notify(partid_t partid, int ch_number, void *payload,
2073 xpc_notify_func func, void *key)
2075 struct xpc_partition *part = &xpc_partitions[partid];
2076 struct xpc_msg *msg = XPC_MSG_ADDRESS(payload);
2077 enum xpc_retval ret;
2080 dev_dbg(xpc_chan, "msg=0x%p, partid=%d, channel=%d\n", (void *) msg,
2083 DBUG_ON(partid <= 0 || partid >= XP_MAX_PARTITIONS);
2084 DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
2085 DBUG_ON(msg == NULL);
2086 DBUG_ON(func == NULL);
2088 ret = xpc_send_msg(&part->channels[ch_number], msg, XPC_N_CALL,
2094 static struct xpc_msg *
2095 xpc_pull_remote_msg(struct xpc_channel *ch, s64 get)
2097 struct xpc_partition *part = &xpc_partitions[ch->partid];
2098 struct xpc_msg *remote_msg, *msg;
2099 u32 msg_index, nmsgs;
2101 enum xpc_retval ret;
2104 if (mutex_lock_interruptible(&ch->msg_to_pull_mutex) != 0) {
2105 /* we were interrupted by a signal */
2109 while (get >= ch->next_msg_to_pull) {
2111 /* pull as many messages as are ready and able to be pulled */
2113 msg_index = ch->next_msg_to_pull % ch->remote_nentries;
2115 DBUG_ON(ch->next_msg_to_pull >=
2116 (volatile s64) ch->w_remote_GP.put);
2117 nmsgs = (volatile s64) ch->w_remote_GP.put -
2118 ch->next_msg_to_pull;
2119 if (msg_index + nmsgs > ch->remote_nentries) {
2120 /* ignore the ones that wrap the msg queue for now */
2121 nmsgs = ch->remote_nentries - msg_index;
2124 msg_offset = msg_index * ch->msg_size;
2125 msg = (struct xpc_msg *) ((u64) ch->remote_msgqueue +
2127 remote_msg = (struct xpc_msg *) (ch->remote_msgqueue_pa +
2130 if ((ret = xpc_pull_remote_cachelines(part, msg, remote_msg,
2131 nmsgs * ch->msg_size)) != xpcSuccess) {
2133 dev_dbg(xpc_chan, "failed to pull %d msgs starting with"
2134 " msg %ld from partition %d, channel=%d, "
2135 "ret=%d\n", nmsgs, ch->next_msg_to_pull,
2136 ch->partid, ch->number, ret);
2138 XPC_DEACTIVATE_PARTITION(part, ret);
2140 mutex_unlock(&ch->msg_to_pull_mutex);
2144 mb(); /* >>> this may not be needed, we're not sure */
2146 ch->next_msg_to_pull += nmsgs;
2149 mutex_unlock(&ch->msg_to_pull_mutex);
2151 /* return the message we were looking for */
2152 msg_offset = (get % ch->remote_nentries) * ch->msg_size;
2153 msg = (struct xpc_msg *) ((u64) ch->remote_msgqueue + msg_offset);
2160 * Get a message to be delivered.
2162 static struct xpc_msg *
2163 xpc_get_deliverable_msg(struct xpc_channel *ch)
2165 struct xpc_msg *msg = NULL;
2170 if ((volatile u32) ch->flags & XPC_C_DISCONNECTING) {
2174 get = (volatile s64) ch->w_local_GP.get;
2175 if (get == (volatile s64) ch->w_remote_GP.put) {
2179 /* There are messages waiting to be pulled and delivered.
2180 * We need to try to secure one for ourselves. We'll do this
2181 * by trying to increment w_local_GP.get and hope that no one
2182 * else beats us to it. If they do, we'll we'll simply have
2183 * to try again for the next one.
2186 if (cmpxchg(&ch->w_local_GP.get, get, get + 1) == get) {
2187 /* we got the entry referenced by get */
2189 dev_dbg(xpc_chan, "w_local_GP.get changed to %ld, "
2190 "partid=%d, channel=%d\n", get + 1,
2191 ch->partid, ch->number);
2193 /* pull the message from the remote partition */
2195 msg = xpc_pull_remote_msg(ch, get);
2197 DBUG_ON(msg != NULL && msg->number != get);
2198 DBUG_ON(msg != NULL && (msg->flags & XPC_M_DONE));
2199 DBUG_ON(msg != NULL && !(msg->flags & XPC_M_READY));
2211 * Deliver a message to its intended recipient.
2214 xpc_deliver_msg(struct xpc_channel *ch)
2216 struct xpc_msg *msg;
2219 if ((msg = xpc_get_deliverable_msg(ch)) != NULL) {
2222 * This ref is taken to protect the payload itself from being
2223 * freed before the user is finished with it, which the user
2224 * indicates by calling xpc_initiate_received().
2226 xpc_msgqueue_ref(ch);
2228 atomic_inc(&ch->kthreads_active);
2230 if (ch->func != NULL) {
2231 dev_dbg(xpc_chan, "ch->func() called, msg=0x%p, "
2232 "msg_number=%ld, partid=%d, channel=%d\n",
2233 (void *) msg, msg->number, ch->partid,
2236 /* deliver the message to its intended recipient */
2237 ch->func(xpcMsgReceived, ch->partid, ch->number,
2238 &msg->payload, ch->key);
2240 dev_dbg(xpc_chan, "ch->func() returned, msg=0x%p, "
2241 "msg_number=%ld, partid=%d, channel=%d\n",
2242 (void *) msg, msg->number, ch->partid,
2246 atomic_dec(&ch->kthreads_active);
2252 * Now we actually acknowledge the messages that have been delivered and ack'd
2253 * by advancing the cached remote message queue's Get value and if requested
2254 * send an IPI to the message sender's partition.
2257 xpc_acknowledge_msgs(struct xpc_channel *ch, s64 initial_get, u8 msg_flags)
2259 struct xpc_msg *msg;
2260 s64 get = initial_get + 1;
2267 if (get == (volatile s64) ch->w_local_GP.get) {
2271 msg = (struct xpc_msg *) ((u64) ch->remote_msgqueue +
2272 (get % ch->remote_nentries) * ch->msg_size);
2274 if (!(msg->flags & XPC_M_DONE)) {
2278 msg_flags |= msg->flags;
2282 if (get == initial_get) {
2283 /* nothing's changed */
2287 if (cmpxchg_rel(&ch->local_GP->get, initial_get, get) !=
2289 /* someone else beat us to it */
2290 DBUG_ON((volatile s64) ch->local_GP->get <=
2295 /* we just set the new value of local_GP->get */
2297 dev_dbg(xpc_chan, "local_GP->get changed to %ld, partid=%d, "
2298 "channel=%d\n", get, ch->partid, ch->number);
2300 send_IPI = (msg_flags & XPC_M_INTERRUPT);
2303 * We need to ensure that the message referenced by
2304 * local_GP->get is not XPC_M_DONE or that local_GP->get
2305 * equals w_local_GP.get, so we'll go have a look.
2311 xpc_IPI_send_msgrequest(ch);
2317 * Acknowledge receipt of a delivered message.
2319 * If a message has XPC_M_INTERRUPT set, send an interrupt to the partition
2320 * that sent the message.
2322 * This function, although called by users, does not call xpc_part_ref() to
2323 * ensure that the partition infrastructure is in place. It relies on the
2324 * fact that we called xpc_msgqueue_ref() in xpc_deliver_msg().
2328 * partid - ID of partition to which the channel is connected.
2329 * ch_number - channel # message received on.
2330 * payload - pointer to the payload area allocated via
2331 * xpc_initiate_allocate().
2334 xpc_initiate_received(partid_t partid, int ch_number, void *payload)
2336 struct xpc_partition *part = &xpc_partitions[partid];
2337 struct xpc_channel *ch;
2338 struct xpc_msg *msg = XPC_MSG_ADDRESS(payload);
2339 s64 get, msg_number = msg->number;
2342 DBUG_ON(partid <= 0 || partid >= XP_MAX_PARTITIONS);
2343 DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
2345 ch = &part->channels[ch_number];
2347 dev_dbg(xpc_chan, "msg=0x%p, msg_number=%ld, partid=%d, channel=%d\n",
2348 (void *) msg, msg_number, ch->partid, ch->number);
2350 DBUG_ON((((u64) msg - (u64) ch->remote_msgqueue) / ch->msg_size) !=
2351 msg_number % ch->remote_nentries);
2352 DBUG_ON(msg->flags & XPC_M_DONE);
2354 msg->flags |= XPC_M_DONE;
2357 * The preceding store of msg->flags must occur before the following
2358 * load of ch->local_GP->get.
2363 * See if this message is next in line to be acknowledged as having
2366 get = ch->local_GP->get;
2367 if (get == msg_number) {
2368 xpc_acknowledge_msgs(ch, get, msg->flags);
2371 /* the call to xpc_msgqueue_ref() was done by xpc_deliver_msg() */
2372 xpc_msgqueue_deref(ch);