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/kernel.h>
18 #include <linux/init.h>
19 #include <linux/sched.h>
20 #include <linux/cache.h>
21 #include <linux/interrupt.h>
22 #include <linux/mutex.h>
23 #include <linux/completion.h>
24 #include <asm/sn/bte.h>
25 #include <asm/sn/sn_sal.h>
29 * Guarantee that the kzalloc'd memory is cacheline aligned.
32 xpc_kzalloc_cacheline_aligned(size_t size, gfp_t flags, void **base)
34 /* see if kzalloc will give us cachline aligned memory by default */
35 *base = kzalloc(size, flags);
39 if ((u64)*base == L1_CACHE_ALIGN((u64)*base)) {
44 /* nope, we'll have to do it ourselves */
45 *base = kzalloc(size + L1_CACHE_BYTES, flags);
49 return (void *)L1_CACHE_ALIGN((u64)*base);
53 * Set up the initial values for the XPartition Communication channels.
56 xpc_initialize_channels(struct xpc_partition *part, partid_t partid)
59 struct xpc_channel *ch;
61 for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
62 ch = &part->channels[ch_number];
65 ch->number = ch_number;
66 ch->flags = XPC_C_DISCONNECTED;
68 ch->local_GP = &part->local_GPs[ch_number];
69 ch->local_openclose_args =
70 &part->local_openclose_args[ch_number];
72 atomic_set(&ch->kthreads_assigned, 0);
73 atomic_set(&ch->kthreads_idle, 0);
74 atomic_set(&ch->kthreads_active, 0);
76 atomic_set(&ch->references, 0);
77 atomic_set(&ch->n_to_notify, 0);
79 spin_lock_init(&ch->lock);
80 mutex_init(&ch->msg_to_pull_mutex);
81 init_completion(&ch->wdisconnect_wait);
83 atomic_set(&ch->n_on_msg_allocate_wq, 0);
84 init_waitqueue_head(&ch->msg_allocate_wq);
85 init_waitqueue_head(&ch->idle_wq);
90 * Setup the infrastructure necessary to support XPartition Communication
91 * between the specified remote partition and the local one.
94 xpc_setup_infrastructure(struct xpc_partition *part)
97 struct timer_list *timer;
98 partid_t partid = XPC_PARTID(part);
101 * Zero out MOST of the entry for this partition. Only the fields
102 * starting with `nchannels' will be zeroed. The preceding fields must
103 * remain `viable' across partition ups and downs, since they may be
104 * referenced during this memset() operation.
106 memset(&part->nchannels, 0, sizeof(struct xpc_partition) -
107 offsetof(struct xpc_partition, nchannels));
110 * Allocate all of the channel structures as a contiguous chunk of
113 part->channels = kzalloc(sizeof(struct xpc_channel) * XPC_NCHANNELS,
115 if (part->channels == NULL) {
116 dev_err(xpc_chan, "can't get memory for channels\n");
120 part->nchannels = XPC_NCHANNELS;
122 /* allocate all the required GET/PUT values */
124 part->local_GPs = xpc_kzalloc_cacheline_aligned(XPC_GP_SIZE,
126 &part->local_GPs_base);
127 if (part->local_GPs == NULL) {
128 kfree(part->channels);
129 part->channels = NULL;
130 dev_err(xpc_chan, "can't get memory for local get/put "
135 part->remote_GPs = xpc_kzalloc_cacheline_aligned(XPC_GP_SIZE,
139 if (part->remote_GPs == NULL) {
140 dev_err(xpc_chan, "can't get memory for remote get/put "
142 kfree(part->local_GPs_base);
143 part->local_GPs = NULL;
144 kfree(part->channels);
145 part->channels = NULL;
149 /* allocate all the required open and close args */
151 part->local_openclose_args =
152 xpc_kzalloc_cacheline_aligned(XPC_OPENCLOSE_ARGS_SIZE, GFP_KERNEL,
153 &part->local_openclose_args_base);
154 if (part->local_openclose_args == NULL) {
155 dev_err(xpc_chan, "can't get memory for local connect args\n");
156 kfree(part->remote_GPs_base);
157 part->remote_GPs = NULL;
158 kfree(part->local_GPs_base);
159 part->local_GPs = NULL;
160 kfree(part->channels);
161 part->channels = NULL;
165 part->remote_openclose_args =
166 xpc_kzalloc_cacheline_aligned(XPC_OPENCLOSE_ARGS_SIZE, GFP_KERNEL,
167 &part->remote_openclose_args_base);
168 if (part->remote_openclose_args == NULL) {
169 dev_err(xpc_chan, "can't get memory for remote connect args\n");
170 kfree(part->local_openclose_args_base);
171 part->local_openclose_args = NULL;
172 kfree(part->remote_GPs_base);
173 part->remote_GPs = NULL;
174 kfree(part->local_GPs_base);
175 part->local_GPs = NULL;
176 kfree(part->channels);
177 part->channels = NULL;
181 xpc_initialize_channels(part, partid);
183 atomic_set(&part->nchannels_active, 0);
184 atomic_set(&part->nchannels_engaged, 0);
186 /* local_IPI_amo were set to 0 by an earlier memset() */
188 /* Initialize this partitions AMO_t structure */
189 part->local_IPI_amo_va = xpc_IPI_init(partid);
191 spin_lock_init(&part->IPI_lock);
193 atomic_set(&part->channel_mgr_requests, 1);
194 init_waitqueue_head(&part->channel_mgr_wq);
196 sprintf(part->IPI_owner, "xpc%02d", partid);
197 ret = request_irq(SGI_XPC_NOTIFY, xpc_notify_IRQ_handler, IRQF_SHARED,
198 part->IPI_owner, (void *)(u64)partid);
200 dev_err(xpc_chan, "can't register NOTIFY IRQ handler, "
202 kfree(part->remote_openclose_args_base);
203 part->remote_openclose_args = NULL;
204 kfree(part->local_openclose_args_base);
205 part->local_openclose_args = NULL;
206 kfree(part->remote_GPs_base);
207 part->remote_GPs = NULL;
208 kfree(part->local_GPs_base);
209 part->local_GPs = NULL;
210 kfree(part->channels);
211 part->channels = NULL;
212 return xpcLackOfResources;
215 /* Setup a timer to check for dropped IPIs */
216 timer = &part->dropped_IPI_timer;
218 timer->function = (void (*)(unsigned long))xpc_dropped_IPI_check;
219 timer->data = (unsigned long)part;
220 timer->expires = jiffies + XPC_P_DROPPED_IPI_WAIT;
224 * With the setting of the partition setup_state to XPC_P_SETUP, we're
225 * declaring that this partition is ready to go.
227 part->setup_state = XPC_P_SETUP;
230 * Setup the per partition specific variables required by the
231 * remote partition to establish channel connections with us.
233 * The setting of the magic # indicates that these per partition
234 * specific variables are ready to be used.
236 xpc_vars_part[partid].GPs_pa = __pa(part->local_GPs);
237 xpc_vars_part[partid].openclose_args_pa =
238 __pa(part->local_openclose_args);
239 xpc_vars_part[partid].IPI_amo_pa = __pa(part->local_IPI_amo_va);
240 cpuid = raw_smp_processor_id(); /* any CPU in this partition will do */
241 xpc_vars_part[partid].IPI_nasid = cpuid_to_nasid(cpuid);
242 xpc_vars_part[partid].IPI_phys_cpuid = cpu_physical_id(cpuid);
243 xpc_vars_part[partid].nchannels = part->nchannels;
244 xpc_vars_part[partid].magic = XPC_VP_MAGIC1;
250 * Create a wrapper that hides the underlying mechanism for pulling a cacheline
251 * (or multiple cachelines) from a remote partition.
253 * src must be a cacheline aligned physical address on the remote partition.
254 * dst must be a cacheline aligned virtual address on this partition.
255 * cnt must be an cacheline sized
257 static enum xpc_retval
258 xpc_pull_remote_cachelines(struct xpc_partition *part, void *dst,
259 const void *src, size_t cnt)
261 bte_result_t bte_ret;
263 DBUG_ON((u64)src != L1_CACHE_ALIGN((u64)src));
264 DBUG_ON((u64)dst != L1_CACHE_ALIGN((u64)dst));
265 DBUG_ON(cnt != L1_CACHE_ALIGN(cnt));
267 if (part->act_state == XPC_P_DEACTIVATING) {
271 bte_ret = xp_bte_copy((u64)src, (u64)dst, (u64)cnt,
272 (BTE_NORMAL | BTE_WACQUIRE), NULL);
273 if (bte_ret == BTE_SUCCESS) {
277 dev_dbg(xpc_chan, "xp_bte_copy() from partition %d failed, ret=%d\n",
278 XPC_PARTID(part), bte_ret);
280 return xpc_map_bte_errors(bte_ret);
284 * Pull the remote per partition specific variables from the specified
288 xpc_pull_remote_vars_part(struct xpc_partition *part)
290 u8 buffer[L1_CACHE_BYTES * 2];
291 struct xpc_vars_part *pulled_entry_cacheline =
292 (struct xpc_vars_part *)L1_CACHE_ALIGN((u64)buffer);
293 struct xpc_vars_part *pulled_entry;
294 u64 remote_entry_cacheline_pa, remote_entry_pa;
295 partid_t partid = XPC_PARTID(part);
298 /* pull the cacheline that contains the variables we're interested in */
300 DBUG_ON(part->remote_vars_part_pa !=
301 L1_CACHE_ALIGN(part->remote_vars_part_pa));
302 DBUG_ON(sizeof(struct xpc_vars_part) != L1_CACHE_BYTES / 2);
304 remote_entry_pa = part->remote_vars_part_pa +
305 sn_partition_id * sizeof(struct xpc_vars_part);
307 remote_entry_cacheline_pa = (remote_entry_pa & ~(L1_CACHE_BYTES - 1));
309 pulled_entry = (struct xpc_vars_part *)((u64)pulled_entry_cacheline +
311 (L1_CACHE_BYTES - 1)));
313 ret = xpc_pull_remote_cachelines(part, pulled_entry_cacheline,
314 (void *)remote_entry_cacheline_pa,
316 if (ret != xpcSuccess) {
317 dev_dbg(xpc_chan, "failed to pull XPC vars_part from "
318 "partition %d, ret=%d\n", partid, ret);
322 /* see if they've been set up yet */
324 if (pulled_entry->magic != XPC_VP_MAGIC1 &&
325 pulled_entry->magic != XPC_VP_MAGIC2) {
327 if (pulled_entry->magic != 0) {
328 dev_dbg(xpc_chan, "partition %d's XPC vars_part for "
329 "partition %d has bad magic value (=0x%lx)\n",
330 partid, sn_partition_id, pulled_entry->magic);
334 /* they've not been initialized yet */
338 if (xpc_vars_part[partid].magic == XPC_VP_MAGIC1) {
340 /* validate the variables */
342 if (pulled_entry->GPs_pa == 0 ||
343 pulled_entry->openclose_args_pa == 0 ||
344 pulled_entry->IPI_amo_pa == 0) {
346 dev_err(xpc_chan, "partition %d's XPC vars_part for "
347 "partition %d are not valid\n", partid,
349 return xpcInvalidAddress;
352 /* the variables we imported look to be valid */
354 part->remote_GPs_pa = pulled_entry->GPs_pa;
355 part->remote_openclose_args_pa =
356 pulled_entry->openclose_args_pa;
357 part->remote_IPI_amo_va =
358 (AMO_t *)__va(pulled_entry->IPI_amo_pa);
359 part->remote_IPI_nasid = pulled_entry->IPI_nasid;
360 part->remote_IPI_phys_cpuid = pulled_entry->IPI_phys_cpuid;
362 if (part->nchannels > pulled_entry->nchannels) {
363 part->nchannels = pulled_entry->nchannels;
366 /* let the other side know that we've pulled their variables */
368 xpc_vars_part[partid].magic = XPC_VP_MAGIC2;
371 if (pulled_entry->magic == XPC_VP_MAGIC1) {
379 * Get the IPI flags and pull the openclose args and/or remote GPs as needed.
382 xpc_get_IPI_flags(struct xpc_partition *part)
384 unsigned long irq_flags;
389 * See if there are any IPI flags to be handled.
392 spin_lock_irqsave(&part->IPI_lock, irq_flags);
393 if ((IPI_amo = part->local_IPI_amo) != 0) {
394 part->local_IPI_amo = 0;
396 spin_unlock_irqrestore(&part->IPI_lock, irq_flags);
398 if (XPC_ANY_OPENCLOSE_IPI_FLAGS_SET(IPI_amo)) {
399 ret = xpc_pull_remote_cachelines(part,
400 part->remote_openclose_args,
402 remote_openclose_args_pa,
403 XPC_OPENCLOSE_ARGS_SIZE);
404 if (ret != xpcSuccess) {
405 XPC_DEACTIVATE_PARTITION(part, ret);
407 dev_dbg(xpc_chan, "failed to pull openclose args from "
408 "partition %d, ret=%d\n", XPC_PARTID(part),
411 /* don't bother processing IPIs anymore */
416 if (XPC_ANY_MSG_IPI_FLAGS_SET(IPI_amo)) {
417 ret = xpc_pull_remote_cachelines(part, part->remote_GPs,
418 (void *)part->remote_GPs_pa,
420 if (ret != xpcSuccess) {
421 XPC_DEACTIVATE_PARTITION(part, ret);
423 dev_dbg(xpc_chan, "failed to pull GPs from partition "
424 "%d, ret=%d\n", XPC_PARTID(part), ret);
426 /* don't bother processing IPIs anymore */
435 * Allocate the local message queue and the notify queue.
437 static enum xpc_retval
438 xpc_allocate_local_msgqueue(struct xpc_channel *ch)
440 unsigned long irq_flags;
444 // >>> may want to check for ch->flags & XPC_C_DISCONNECTING between
445 // >>> iterations of the for-loop, bail if set?
447 // >>> should we impose a minimum #of entries? like 4 or 8?
448 for (nentries = ch->local_nentries; nentries > 0; nentries--) {
450 nbytes = nentries * ch->msg_size;
451 ch->local_msgqueue = xpc_kzalloc_cacheline_aligned(nbytes,
454 local_msgqueue_base);
455 if (ch->local_msgqueue == NULL) {
459 nbytes = nentries * sizeof(struct xpc_notify);
460 ch->notify_queue = kzalloc(nbytes, GFP_KERNEL);
461 if (ch->notify_queue == NULL) {
462 kfree(ch->local_msgqueue_base);
463 ch->local_msgqueue = NULL;
467 spin_lock_irqsave(&ch->lock, irq_flags);
468 if (nentries < ch->local_nentries) {
469 dev_dbg(xpc_chan, "nentries=%d local_nentries=%d, "
470 "partid=%d, channel=%d\n", nentries,
471 ch->local_nentries, ch->partid, ch->number);
473 ch->local_nentries = nentries;
475 spin_unlock_irqrestore(&ch->lock, irq_flags);
479 dev_dbg(xpc_chan, "can't get memory for local message queue and notify "
480 "queue, partid=%d, channel=%d\n", ch->partid, ch->number);
485 * Allocate the cached remote message queue.
487 static enum xpc_retval
488 xpc_allocate_remote_msgqueue(struct xpc_channel *ch)
490 unsigned long irq_flags;
494 DBUG_ON(ch->remote_nentries <= 0);
496 // >>> may want to check for ch->flags & XPC_C_DISCONNECTING between
497 // >>> iterations of the for-loop, bail if set?
499 // >>> should we impose a minimum #of entries? like 4 or 8?
500 for (nentries = ch->remote_nentries; nentries > 0; nentries--) {
502 nbytes = nentries * ch->msg_size;
503 ch->remote_msgqueue = xpc_kzalloc_cacheline_aligned(nbytes,
506 remote_msgqueue_base);
507 if (ch->remote_msgqueue == NULL) {
511 spin_lock_irqsave(&ch->lock, irq_flags);
512 if (nentries < ch->remote_nentries) {
513 dev_dbg(xpc_chan, "nentries=%d remote_nentries=%d, "
514 "partid=%d, channel=%d\n", nentries,
515 ch->remote_nentries, ch->partid, ch->number);
517 ch->remote_nentries = nentries;
519 spin_unlock_irqrestore(&ch->lock, irq_flags);
523 dev_dbg(xpc_chan, "can't get memory for cached remote message queue, "
524 "partid=%d, channel=%d\n", ch->partid, ch->number);
529 * Allocate message queues and other stuff associated with a channel.
531 * Note: Assumes all of the channel sizes are filled in.
533 static enum xpc_retval
534 xpc_allocate_msgqueues(struct xpc_channel *ch)
536 unsigned long irq_flags;
539 DBUG_ON(ch->flags & XPC_C_SETUP);
541 if ((ret = xpc_allocate_local_msgqueue(ch)) != xpcSuccess) {
545 if ((ret = xpc_allocate_remote_msgqueue(ch)) != xpcSuccess) {
546 kfree(ch->local_msgqueue_base);
547 ch->local_msgqueue = NULL;
548 kfree(ch->notify_queue);
549 ch->notify_queue = NULL;
553 spin_lock_irqsave(&ch->lock, irq_flags);
554 ch->flags |= XPC_C_SETUP;
555 spin_unlock_irqrestore(&ch->lock, irq_flags);
561 * Process a connect message from a remote partition.
563 * Note: xpc_process_connect() is expecting to be called with the
564 * spin_lock_irqsave held and will leave it locked upon return.
567 xpc_process_connect(struct xpc_channel *ch, unsigned long *irq_flags)
571 DBUG_ON(!spin_is_locked(&ch->lock));
573 if (!(ch->flags & XPC_C_OPENREQUEST) ||
574 !(ch->flags & XPC_C_ROPENREQUEST)) {
575 /* nothing more to do for now */
578 DBUG_ON(!(ch->flags & XPC_C_CONNECTING));
580 if (!(ch->flags & XPC_C_SETUP)) {
581 spin_unlock_irqrestore(&ch->lock, *irq_flags);
582 ret = xpc_allocate_msgqueues(ch);
583 spin_lock_irqsave(&ch->lock, *irq_flags);
585 if (ret != xpcSuccess) {
586 XPC_DISCONNECT_CHANNEL(ch, ret, irq_flags);
588 if (ch->flags & (XPC_C_CONNECTED | XPC_C_DISCONNECTING)) {
592 DBUG_ON(!(ch->flags & XPC_C_SETUP));
593 DBUG_ON(ch->local_msgqueue == NULL);
594 DBUG_ON(ch->remote_msgqueue == NULL);
597 if (!(ch->flags & XPC_C_OPENREPLY)) {
598 ch->flags |= XPC_C_OPENREPLY;
599 xpc_IPI_send_openreply(ch, irq_flags);
602 if (!(ch->flags & XPC_C_ROPENREPLY)) {
606 DBUG_ON(ch->remote_msgqueue_pa == 0);
608 ch->flags = (XPC_C_CONNECTED | XPC_C_SETUP); /* clear all else */
610 dev_info(xpc_chan, "channel %d to partition %d connected\n",
611 ch->number, ch->partid);
613 spin_unlock_irqrestore(&ch->lock, *irq_flags);
614 xpc_create_kthreads(ch, 1, 0);
615 spin_lock_irqsave(&ch->lock, *irq_flags);
619 * Notify those who wanted to be notified upon delivery of their message.
622 xpc_notify_senders(struct xpc_channel *ch, enum xpc_retval reason, s64 put)
624 struct xpc_notify *notify;
626 s64 get = ch->w_remote_GP.get - 1;
628 while (++get < put && atomic_read(&ch->n_to_notify) > 0) {
630 notify = &ch->notify_queue[get % ch->local_nentries];
633 * See if the notify entry indicates it was associated with
634 * a message who's sender wants to be notified. It is possible
635 * that it is, but someone else is doing or has done the
638 notify_type = notify->type;
639 if (notify_type == 0 ||
640 cmpxchg(¬ify->type, notify_type, 0) != notify_type) {
644 DBUG_ON(notify_type != XPC_N_CALL);
646 atomic_dec(&ch->n_to_notify);
648 if (notify->func != NULL) {
649 dev_dbg(xpc_chan, "notify->func() called, notify=0x%p, "
650 "msg_number=%ld, partid=%d, channel=%d\n",
651 (void *)notify, get, ch->partid, ch->number);
653 notify->func(reason, ch->partid, ch->number,
656 dev_dbg(xpc_chan, "notify->func() returned, "
657 "notify=0x%p, msg_number=%ld, partid=%d, "
658 "channel=%d\n", (void *)notify, get,
659 ch->partid, ch->number);
665 * Free up message queues and other stuff that were allocated for the specified
668 * Note: ch->reason and ch->reason_line are left set for debugging purposes,
669 * they're cleared when XPC_C_DISCONNECTED is cleared.
672 xpc_free_msgqueues(struct xpc_channel *ch)
674 DBUG_ON(!spin_is_locked(&ch->lock));
675 DBUG_ON(atomic_read(&ch->n_to_notify) != 0);
677 ch->remote_msgqueue_pa = 0;
681 ch->local_nentries = 0;
682 ch->remote_nentries = 0;
683 ch->kthreads_assigned_limit = 0;
684 ch->kthreads_idle_limit = 0;
686 ch->local_GP->get = 0;
687 ch->local_GP->put = 0;
688 ch->remote_GP.get = 0;
689 ch->remote_GP.put = 0;
690 ch->w_local_GP.get = 0;
691 ch->w_local_GP.put = 0;
692 ch->w_remote_GP.get = 0;
693 ch->w_remote_GP.put = 0;
694 ch->next_msg_to_pull = 0;
696 if (ch->flags & XPC_C_SETUP) {
697 ch->flags &= ~XPC_C_SETUP;
699 dev_dbg(xpc_chan, "ch->flags=0x%x, partid=%d, channel=%d\n",
700 ch->flags, ch->partid, ch->number);
702 kfree(ch->local_msgqueue_base);
703 ch->local_msgqueue = NULL;
704 kfree(ch->remote_msgqueue_base);
705 ch->remote_msgqueue = NULL;
706 kfree(ch->notify_queue);
707 ch->notify_queue = NULL;
712 * spin_lock_irqsave() is expected to be held on entry.
715 xpc_process_disconnect(struct xpc_channel *ch, unsigned long *irq_flags)
717 struct xpc_partition *part = &xpc_partitions[ch->partid];
718 u32 channel_was_connected = (ch->flags & XPC_C_WASCONNECTED);
720 DBUG_ON(!spin_is_locked(&ch->lock));
722 if (!(ch->flags & XPC_C_DISCONNECTING)) {
726 DBUG_ON(!(ch->flags & XPC_C_CLOSEREQUEST));
728 /* make sure all activity has settled down first */
730 if (atomic_read(&ch->kthreads_assigned) > 0 ||
731 atomic_read(&ch->references) > 0) {
734 DBUG_ON((ch->flags & XPC_C_CONNECTEDCALLOUT_MADE) &&
735 !(ch->flags & XPC_C_DISCONNECTINGCALLOUT_MADE));
737 if (part->act_state == XPC_P_DEACTIVATING) {
738 /* can't proceed until the other side disengages from us */
739 if (xpc_partition_engaged(1UL << ch->partid)) {
745 /* as long as the other side is up do the full protocol */
747 if (!(ch->flags & XPC_C_RCLOSEREQUEST)) {
751 if (!(ch->flags & XPC_C_CLOSEREPLY)) {
752 ch->flags |= XPC_C_CLOSEREPLY;
753 xpc_IPI_send_closereply(ch, irq_flags);
756 if (!(ch->flags & XPC_C_RCLOSEREPLY)) {
761 /* wake those waiting for notify completion */
762 if (atomic_read(&ch->n_to_notify) > 0) {
763 /* >>> we do callout while holding ch->lock */
764 xpc_notify_senders(ch, ch->reason, ch->w_local_GP.put);
767 /* both sides are disconnected now */
769 if (ch->flags & XPC_C_DISCONNECTINGCALLOUT_MADE) {
770 spin_unlock_irqrestore(&ch->lock, *irq_flags);
771 xpc_disconnect_callout(ch, xpcDisconnected);
772 spin_lock_irqsave(&ch->lock, *irq_flags);
775 /* it's now safe to free the channel's message queues */
776 xpc_free_msgqueues(ch);
778 /* mark disconnected, clear all other flags except XPC_C_WDISCONNECT */
779 ch->flags = (XPC_C_DISCONNECTED | (ch->flags & XPC_C_WDISCONNECT));
781 atomic_dec(&part->nchannels_active);
783 if (channel_was_connected) {
784 dev_info(xpc_chan, "channel %d to partition %d disconnected, "
785 "reason=%d\n", ch->number, ch->partid, ch->reason);
788 if (ch->flags & XPC_C_WDISCONNECT) {
789 /* we won't lose the CPU since we're holding ch->lock */
790 complete(&ch->wdisconnect_wait);
791 } else if (ch->delayed_IPI_flags) {
792 if (part->act_state != XPC_P_DEACTIVATING) {
793 /* time to take action on any delayed IPI flags */
794 spin_lock(&part->IPI_lock);
795 XPC_SET_IPI_FLAGS(part->local_IPI_amo, ch->number,
796 ch->delayed_IPI_flags);
797 spin_unlock(&part->IPI_lock);
799 ch->delayed_IPI_flags = 0;
804 * Process a change in the channel's remote connection state.
807 xpc_process_openclose_IPI(struct xpc_partition *part, int ch_number,
810 unsigned long irq_flags;
811 struct xpc_openclose_args *args =
812 &part->remote_openclose_args[ch_number];
813 struct xpc_channel *ch = &part->channels[ch_number];
814 enum xpc_retval reason;
816 spin_lock_irqsave(&ch->lock, irq_flags);
820 if ((ch->flags & XPC_C_DISCONNECTED) && (ch->flags & XPC_C_WDISCONNECT)) {
822 * Delay processing IPI flags until thread waiting disconnect
823 * has had a chance to see that the channel is disconnected.
825 ch->delayed_IPI_flags |= IPI_flags;
826 spin_unlock_irqrestore(&ch->lock, irq_flags);
830 if (IPI_flags & XPC_IPI_CLOSEREQUEST) {
832 dev_dbg(xpc_chan, "XPC_IPI_CLOSEREQUEST (reason=%d) received "
833 "from partid=%d, channel=%d\n", args->reason,
834 ch->partid, ch->number);
837 * If RCLOSEREQUEST is set, we're probably waiting for
838 * RCLOSEREPLY. We should find it and a ROPENREQUEST packed
839 * with this RCLOSEREQUEST in the IPI_flags.
842 if (ch->flags & XPC_C_RCLOSEREQUEST) {
843 DBUG_ON(!(ch->flags & XPC_C_DISCONNECTING));
844 DBUG_ON(!(ch->flags & XPC_C_CLOSEREQUEST));
845 DBUG_ON(!(ch->flags & XPC_C_CLOSEREPLY));
846 DBUG_ON(ch->flags & XPC_C_RCLOSEREPLY);
848 DBUG_ON(!(IPI_flags & XPC_IPI_CLOSEREPLY));
849 IPI_flags &= ~XPC_IPI_CLOSEREPLY;
850 ch->flags |= XPC_C_RCLOSEREPLY;
852 /* both sides have finished disconnecting */
853 xpc_process_disconnect(ch, &irq_flags);
854 DBUG_ON(!(ch->flags & XPC_C_DISCONNECTED));
858 if (ch->flags & XPC_C_DISCONNECTED) {
859 if (!(IPI_flags & XPC_IPI_OPENREQUEST)) {
860 if ((XPC_GET_IPI_FLAGS(part->local_IPI_amo,
862 XPC_IPI_OPENREQUEST)) {
864 DBUG_ON(ch->delayed_IPI_flags != 0);
865 spin_lock(&part->IPI_lock);
866 XPC_SET_IPI_FLAGS(part->local_IPI_amo,
868 XPC_IPI_CLOSEREQUEST);
869 spin_unlock(&part->IPI_lock);
871 spin_unlock_irqrestore(&ch->lock, irq_flags);
875 XPC_SET_REASON(ch, 0, 0);
876 ch->flags &= ~XPC_C_DISCONNECTED;
878 atomic_inc(&part->nchannels_active);
879 ch->flags |= (XPC_C_CONNECTING | XPC_C_ROPENREQUEST);
882 IPI_flags &= ~(XPC_IPI_OPENREQUEST | XPC_IPI_OPENREPLY);
885 * The meaningful CLOSEREQUEST connection state fields are:
886 * reason = reason connection is to be closed
889 ch->flags |= XPC_C_RCLOSEREQUEST;
891 if (!(ch->flags & XPC_C_DISCONNECTING)) {
892 reason = args->reason;
893 if (reason <= xpcSuccess || reason > xpcUnknownReason) {
894 reason = xpcUnknownReason;
895 } else if (reason == xpcUnregistering) {
896 reason = xpcOtherUnregistering;
899 XPC_DISCONNECT_CHANNEL(ch, reason, &irq_flags);
901 DBUG_ON(IPI_flags & XPC_IPI_CLOSEREPLY);
902 spin_unlock_irqrestore(&ch->lock, irq_flags);
906 xpc_process_disconnect(ch, &irq_flags);
909 if (IPI_flags & XPC_IPI_CLOSEREPLY) {
911 dev_dbg(xpc_chan, "XPC_IPI_CLOSEREPLY received from partid=%d,"
912 " channel=%d\n", ch->partid, ch->number);
914 if (ch->flags & XPC_C_DISCONNECTED) {
915 DBUG_ON(part->act_state != XPC_P_DEACTIVATING);
916 spin_unlock_irqrestore(&ch->lock, irq_flags);
920 DBUG_ON(!(ch->flags & XPC_C_CLOSEREQUEST));
922 if (!(ch->flags & XPC_C_RCLOSEREQUEST)) {
923 if ((XPC_GET_IPI_FLAGS(part->local_IPI_amo, ch_number)
924 & XPC_IPI_CLOSEREQUEST)) {
926 DBUG_ON(ch->delayed_IPI_flags != 0);
927 spin_lock(&part->IPI_lock);
928 XPC_SET_IPI_FLAGS(part->local_IPI_amo,
931 spin_unlock(&part->IPI_lock);
933 spin_unlock_irqrestore(&ch->lock, irq_flags);
937 ch->flags |= XPC_C_RCLOSEREPLY;
939 if (ch->flags & XPC_C_CLOSEREPLY) {
940 /* both sides have finished disconnecting */
941 xpc_process_disconnect(ch, &irq_flags);
945 if (IPI_flags & XPC_IPI_OPENREQUEST) {
947 dev_dbg(xpc_chan, "XPC_IPI_OPENREQUEST (msg_size=%d, "
948 "local_nentries=%d) received from partid=%d, "
949 "channel=%d\n", args->msg_size, args->local_nentries,
950 ch->partid, ch->number);
952 if (part->act_state == XPC_P_DEACTIVATING ||
953 (ch->flags & XPC_C_ROPENREQUEST)) {
954 spin_unlock_irqrestore(&ch->lock, irq_flags);
958 if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_WDISCONNECT)) {
959 ch->delayed_IPI_flags |= XPC_IPI_OPENREQUEST;
960 spin_unlock_irqrestore(&ch->lock, irq_flags);
963 DBUG_ON(!(ch->flags & (XPC_C_DISCONNECTED |
964 XPC_C_OPENREQUEST)));
965 DBUG_ON(ch->flags & (XPC_C_ROPENREQUEST | XPC_C_ROPENREPLY |
966 XPC_C_OPENREPLY | XPC_C_CONNECTED));
969 * The meaningful OPENREQUEST connection state fields are:
970 * msg_size = size of channel's messages in bytes
971 * local_nentries = remote partition's local_nentries
973 if (args->msg_size == 0 || args->local_nentries == 0) {
974 /* assume OPENREQUEST was delayed by mistake */
975 spin_unlock_irqrestore(&ch->lock, irq_flags);
979 ch->flags |= (XPC_C_ROPENREQUEST | XPC_C_CONNECTING);
980 ch->remote_nentries = args->local_nentries;
982 if (ch->flags & XPC_C_OPENREQUEST) {
983 if (args->msg_size != ch->msg_size) {
984 XPC_DISCONNECT_CHANNEL(ch, xpcUnequalMsgSizes,
986 spin_unlock_irqrestore(&ch->lock, irq_flags);
990 ch->msg_size = args->msg_size;
992 XPC_SET_REASON(ch, 0, 0);
993 ch->flags &= ~XPC_C_DISCONNECTED;
995 atomic_inc(&part->nchannels_active);
998 xpc_process_connect(ch, &irq_flags);
1001 if (IPI_flags & XPC_IPI_OPENREPLY) {
1003 dev_dbg(xpc_chan, "XPC_IPI_OPENREPLY (local_msgqueue_pa=0x%lx, "
1004 "local_nentries=%d, remote_nentries=%d) received from "
1005 "partid=%d, channel=%d\n", args->local_msgqueue_pa,
1006 args->local_nentries, args->remote_nentries,
1007 ch->partid, ch->number);
1009 if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_DISCONNECTED)) {
1010 spin_unlock_irqrestore(&ch->lock, irq_flags);
1013 if (!(ch->flags & XPC_C_OPENREQUEST)) {
1014 XPC_DISCONNECT_CHANNEL(ch, xpcOpenCloseError,
1016 spin_unlock_irqrestore(&ch->lock, irq_flags);
1020 DBUG_ON(!(ch->flags & XPC_C_ROPENREQUEST));
1021 DBUG_ON(ch->flags & XPC_C_CONNECTED);
1024 * The meaningful OPENREPLY connection state fields are:
1025 * local_msgqueue_pa = physical address of remote
1026 * partition's local_msgqueue
1027 * local_nentries = remote partition's local_nentries
1028 * remote_nentries = remote partition's remote_nentries
1030 DBUG_ON(args->local_msgqueue_pa == 0);
1031 DBUG_ON(args->local_nentries == 0);
1032 DBUG_ON(args->remote_nentries == 0);
1034 ch->flags |= XPC_C_ROPENREPLY;
1035 ch->remote_msgqueue_pa = args->local_msgqueue_pa;
1037 if (args->local_nentries < ch->remote_nentries) {
1038 dev_dbg(xpc_chan, "XPC_IPI_OPENREPLY: new "
1039 "remote_nentries=%d, old remote_nentries=%d, "
1040 "partid=%d, channel=%d\n",
1041 args->local_nentries, ch->remote_nentries,
1042 ch->partid, ch->number);
1044 ch->remote_nentries = args->local_nentries;
1046 if (args->remote_nentries < ch->local_nentries) {
1047 dev_dbg(xpc_chan, "XPC_IPI_OPENREPLY: new "
1048 "local_nentries=%d, old local_nentries=%d, "
1049 "partid=%d, channel=%d\n",
1050 args->remote_nentries, ch->local_nentries,
1051 ch->partid, ch->number);
1053 ch->local_nentries = args->remote_nentries;
1056 xpc_process_connect(ch, &irq_flags);
1059 spin_unlock_irqrestore(&ch->lock, irq_flags);
1063 * Attempt to establish a channel connection to a remote partition.
1065 static enum xpc_retval
1066 xpc_connect_channel(struct xpc_channel *ch)
1068 unsigned long irq_flags;
1069 struct xpc_registration *registration = &xpc_registrations[ch->number];
1071 if (mutex_trylock(®istration->mutex) == 0) {
1075 if (!XPC_CHANNEL_REGISTERED(ch->number)) {
1076 mutex_unlock(®istration->mutex);
1077 return xpcUnregistered;
1080 spin_lock_irqsave(&ch->lock, irq_flags);
1082 DBUG_ON(ch->flags & XPC_C_CONNECTED);
1083 DBUG_ON(ch->flags & XPC_C_OPENREQUEST);
1085 if (ch->flags & XPC_C_DISCONNECTING) {
1086 spin_unlock_irqrestore(&ch->lock, irq_flags);
1087 mutex_unlock(®istration->mutex);
1091 /* add info from the channel connect registration to the channel */
1093 ch->kthreads_assigned_limit = registration->assigned_limit;
1094 ch->kthreads_idle_limit = registration->idle_limit;
1095 DBUG_ON(atomic_read(&ch->kthreads_assigned) != 0);
1096 DBUG_ON(atomic_read(&ch->kthreads_idle) != 0);
1097 DBUG_ON(atomic_read(&ch->kthreads_active) != 0);
1099 ch->func = registration->func;
1100 DBUG_ON(registration->func == NULL);
1101 ch->key = registration->key;
1103 ch->local_nentries = registration->nentries;
1105 if (ch->flags & XPC_C_ROPENREQUEST) {
1106 if (registration->msg_size != ch->msg_size) {
1107 /* the local and remote sides aren't the same */
1110 * Because XPC_DISCONNECT_CHANNEL() can block we're
1111 * forced to up the registration sema before we unlock
1112 * the channel lock. But that's okay here because we're
1113 * done with the part that required the registration
1114 * sema. XPC_DISCONNECT_CHANNEL() requires that the
1115 * channel lock be locked and will unlock and relock
1116 * the channel lock as needed.
1118 mutex_unlock(®istration->mutex);
1119 XPC_DISCONNECT_CHANNEL(ch, xpcUnequalMsgSizes,
1121 spin_unlock_irqrestore(&ch->lock, irq_flags);
1122 return xpcUnequalMsgSizes;
1125 ch->msg_size = registration->msg_size;
1127 XPC_SET_REASON(ch, 0, 0);
1128 ch->flags &= ~XPC_C_DISCONNECTED;
1130 atomic_inc(&xpc_partitions[ch->partid].nchannels_active);
1133 mutex_unlock(®istration->mutex);
1135 /* initiate the connection */
1137 ch->flags |= (XPC_C_OPENREQUEST | XPC_C_CONNECTING);
1138 xpc_IPI_send_openrequest(ch, &irq_flags);
1140 xpc_process_connect(ch, &irq_flags);
1142 spin_unlock_irqrestore(&ch->lock, irq_flags);
1148 * Clear some of the msg flags in the local message queue.
1151 xpc_clear_local_msgqueue_flags(struct xpc_channel *ch)
1153 struct xpc_msg *msg;
1156 get = ch->w_remote_GP.get;
1158 msg = (struct xpc_msg *)((u64)ch->local_msgqueue +
1159 (get % ch->local_nentries) *
1162 } while (++get < (volatile s64)ch->remote_GP.get);
1166 * Clear some of the msg flags in the remote message queue.
1169 xpc_clear_remote_msgqueue_flags(struct xpc_channel *ch)
1171 struct xpc_msg *msg;
1174 put = ch->w_remote_GP.put;
1176 msg = (struct xpc_msg *)((u64)ch->remote_msgqueue +
1177 (put % ch->remote_nentries) *
1180 } while (++put < (volatile s64)ch->remote_GP.put);
1184 xpc_process_msg_IPI(struct xpc_partition *part, int ch_number)
1186 struct xpc_channel *ch = &part->channels[ch_number];
1189 ch->remote_GP = part->remote_GPs[ch_number];
1191 /* See what, if anything, has changed for each connected channel */
1193 xpc_msgqueue_ref(ch);
1195 if (ch->w_remote_GP.get == ch->remote_GP.get &&
1196 ch->w_remote_GP.put == ch->remote_GP.put) {
1197 /* nothing changed since GPs were last pulled */
1198 xpc_msgqueue_deref(ch);
1202 if (!(ch->flags & XPC_C_CONNECTED)) {
1203 xpc_msgqueue_deref(ch);
1208 * First check to see if messages recently sent by us have been
1209 * received by the other side. (The remote GET value will have
1210 * changed since we last looked at it.)
1213 if (ch->w_remote_GP.get != ch->remote_GP.get) {
1216 * We need to notify any senders that want to be notified
1217 * that their sent messages have been received by their
1218 * intended recipients. We need to do this before updating
1219 * w_remote_GP.get so that we don't allocate the same message
1220 * queue entries prematurely (see xpc_allocate_msg()).
1222 if (atomic_read(&ch->n_to_notify) > 0) {
1224 * Notify senders that messages sent have been
1225 * received and delivered by the other side.
1227 xpc_notify_senders(ch, xpcMsgDelivered,
1232 * Clear msg->flags in previously sent messages, so that
1233 * they're ready for xpc_allocate_msg().
1235 xpc_clear_local_msgqueue_flags(ch);
1237 ch->w_remote_GP.get = ch->remote_GP.get;
1239 dev_dbg(xpc_chan, "w_remote_GP.get changed to %ld, partid=%d, "
1240 "channel=%d\n", ch->w_remote_GP.get, ch->partid,
1244 * If anyone was waiting for message queue entries to become
1245 * available, wake them up.
1247 if (atomic_read(&ch->n_on_msg_allocate_wq) > 0) {
1248 wake_up(&ch->msg_allocate_wq);
1253 * Now check for newly sent messages by the other side. (The remote
1254 * PUT value will have changed since we last looked at it.)
1257 if (ch->w_remote_GP.put != ch->remote_GP.put) {
1259 * Clear msg->flags in previously received messages, so that
1260 * they're ready for xpc_get_deliverable_msg().
1262 xpc_clear_remote_msgqueue_flags(ch);
1264 ch->w_remote_GP.put = ch->remote_GP.put;
1266 dev_dbg(xpc_chan, "w_remote_GP.put changed to %ld, partid=%d, "
1267 "channel=%d\n", ch->w_remote_GP.put, ch->partid,
1270 nmsgs_sent = ch->w_remote_GP.put - ch->w_local_GP.get;
1271 if (nmsgs_sent > 0) {
1272 dev_dbg(xpc_chan, "msgs waiting to be copied and "
1273 "delivered=%d, partid=%d, channel=%d\n",
1274 nmsgs_sent, ch->partid, ch->number);
1276 if (ch->flags & XPC_C_CONNECTEDCALLOUT_MADE) {
1277 xpc_activate_kthreads(ch, nmsgs_sent);
1282 xpc_msgqueue_deref(ch);
1286 xpc_process_channel_activity(struct xpc_partition *part)
1288 unsigned long irq_flags;
1289 u64 IPI_amo, IPI_flags;
1290 struct xpc_channel *ch;
1294 IPI_amo = xpc_get_IPI_flags(part);
1297 * Initiate channel connections for registered channels.
1299 * For each connected channel that has pending messages activate idle
1300 * kthreads and/or create new kthreads as needed.
1303 for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
1304 ch = &part->channels[ch_number];
1307 * Process any open or close related IPI flags, and then deal
1308 * with connecting or disconnecting the channel as required.
1311 IPI_flags = XPC_GET_IPI_FLAGS(IPI_amo, ch_number);
1313 if (XPC_ANY_OPENCLOSE_IPI_FLAGS_SET(IPI_flags)) {
1314 xpc_process_openclose_IPI(part, ch_number, IPI_flags);
1317 ch_flags = ch->flags; /* need an atomic snapshot of flags */
1319 if (ch_flags & XPC_C_DISCONNECTING) {
1320 spin_lock_irqsave(&ch->lock, irq_flags);
1321 xpc_process_disconnect(ch, &irq_flags);
1322 spin_unlock_irqrestore(&ch->lock, irq_flags);
1326 if (part->act_state == XPC_P_DEACTIVATING) {
1330 if (!(ch_flags & XPC_C_CONNECTED)) {
1331 if (!(ch_flags & XPC_C_OPENREQUEST)) {
1332 DBUG_ON(ch_flags & XPC_C_SETUP);
1333 (void)xpc_connect_channel(ch);
1335 spin_lock_irqsave(&ch->lock, irq_flags);
1336 xpc_process_connect(ch, &irq_flags);
1337 spin_unlock_irqrestore(&ch->lock, irq_flags);
1343 * Process any message related IPI flags, this may involve the
1344 * activation of kthreads to deliver any pending messages sent
1345 * from the other partition.
1348 if (XPC_ANY_MSG_IPI_FLAGS_SET(IPI_flags)) {
1349 xpc_process_msg_IPI(part, ch_number);
1355 * XPC's heartbeat code calls this function to inform XPC that a partition is
1356 * going down. XPC responds by tearing down the XPartition Communication
1357 * infrastructure used for the just downed partition.
1359 * XPC's heartbeat code will never call this function and xpc_partition_up()
1360 * at the same time. Nor will it ever make multiple calls to either function
1364 xpc_partition_going_down(struct xpc_partition *part, enum xpc_retval reason)
1366 unsigned long irq_flags;
1368 struct xpc_channel *ch;
1370 dev_dbg(xpc_chan, "deactivating partition %d, reason=%d\n",
1371 XPC_PARTID(part), reason);
1373 if (!xpc_part_ref(part)) {
1374 /* infrastructure for this partition isn't currently set up */
1378 /* disconnect channels associated with the partition going down */
1380 for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
1381 ch = &part->channels[ch_number];
1383 xpc_msgqueue_ref(ch);
1384 spin_lock_irqsave(&ch->lock, irq_flags);
1386 XPC_DISCONNECT_CHANNEL(ch, reason, &irq_flags);
1388 spin_unlock_irqrestore(&ch->lock, irq_flags);
1389 xpc_msgqueue_deref(ch);
1392 xpc_wakeup_channel_mgr(part);
1394 xpc_part_deref(part);
1398 * Teardown the infrastructure necessary to support XPartition Communication
1399 * between the specified remote partition and the local one.
1402 xpc_teardown_infrastructure(struct xpc_partition *part)
1404 partid_t partid = XPC_PARTID(part);
1407 * We start off by making this partition inaccessible to local
1408 * processes by marking it as no longer setup. Then we make it
1409 * inaccessible to remote processes by clearing the XPC per partition
1410 * specific variable's magic # (which indicates that these variables
1411 * are no longer valid) and by ignoring all XPC notify IPIs sent to
1415 DBUG_ON(atomic_read(&part->nchannels_engaged) != 0);
1416 DBUG_ON(atomic_read(&part->nchannels_active) != 0);
1417 DBUG_ON(part->setup_state != XPC_P_SETUP);
1418 part->setup_state = XPC_P_WTEARDOWN;
1420 xpc_vars_part[partid].magic = 0;
1422 free_irq(SGI_XPC_NOTIFY, (void *)(u64)partid);
1425 * Before proceeding with the teardown we have to wait until all
1426 * existing references cease.
1428 wait_event(part->teardown_wq, (atomic_read(&part->references) == 0));
1430 /* now we can begin tearing down the infrastructure */
1432 part->setup_state = XPC_P_TORNDOWN;
1434 /* in case we've still got outstanding timers registered... */
1435 del_timer_sync(&part->dropped_IPI_timer);
1437 kfree(part->remote_openclose_args_base);
1438 part->remote_openclose_args = NULL;
1439 kfree(part->local_openclose_args_base);
1440 part->local_openclose_args = NULL;
1441 kfree(part->remote_GPs_base);
1442 part->remote_GPs = NULL;
1443 kfree(part->local_GPs_base);
1444 part->local_GPs = NULL;
1445 kfree(part->channels);
1446 part->channels = NULL;
1447 part->local_IPI_amo_va = NULL;
1451 * Called by XP at the time of channel connection registration to cause
1452 * XPC to establish connections to all currently active partitions.
1455 xpc_initiate_connect(int ch_number)
1458 struct xpc_partition *part;
1459 struct xpc_channel *ch;
1461 DBUG_ON(ch_number < 0 || ch_number >= XPC_NCHANNELS);
1463 for (partid = 1; partid < XP_MAX_PARTITIONS; partid++) {
1464 part = &xpc_partitions[partid];
1466 if (xpc_part_ref(part)) {
1467 ch = &part->channels[ch_number];
1470 * Initiate the establishment of a connection on the
1471 * newly registered channel to the remote partition.
1473 xpc_wakeup_channel_mgr(part);
1474 xpc_part_deref(part);
1480 xpc_connected_callout(struct xpc_channel *ch)
1482 /* let the registerer know that a connection has been established */
1484 if (ch->func != NULL) {
1485 dev_dbg(xpc_chan, "ch->func() called, reason=xpcConnected, "
1486 "partid=%d, channel=%d\n", ch->partid, ch->number);
1488 ch->func(xpcConnected, ch->partid, ch->number,
1489 (void *)(u64)ch->local_nentries, ch->key);
1491 dev_dbg(xpc_chan, "ch->func() returned, reason=xpcConnected, "
1492 "partid=%d, channel=%d\n", ch->partid, ch->number);
1497 * Called by XP at the time of channel connection unregistration to cause
1498 * XPC to teardown all current connections for the specified channel.
1500 * Before returning xpc_initiate_disconnect() will wait until all connections
1501 * on the specified channel have been closed/torndown. So the caller can be
1502 * assured that they will not be receiving any more callouts from XPC to the
1503 * function they registered via xpc_connect().
1507 * ch_number - channel # to unregister.
1510 xpc_initiate_disconnect(int ch_number)
1512 unsigned long irq_flags;
1514 struct xpc_partition *part;
1515 struct xpc_channel *ch;
1517 DBUG_ON(ch_number < 0 || ch_number >= XPC_NCHANNELS);
1519 /* initiate the channel disconnect for every active partition */
1520 for (partid = 1; partid < XP_MAX_PARTITIONS; partid++) {
1521 part = &xpc_partitions[partid];
1523 if (xpc_part_ref(part)) {
1524 ch = &part->channels[ch_number];
1525 xpc_msgqueue_ref(ch);
1527 spin_lock_irqsave(&ch->lock, irq_flags);
1529 if (!(ch->flags & XPC_C_DISCONNECTED)) {
1530 ch->flags |= XPC_C_WDISCONNECT;
1532 XPC_DISCONNECT_CHANNEL(ch, xpcUnregistering,
1536 spin_unlock_irqrestore(&ch->lock, irq_flags);
1538 xpc_msgqueue_deref(ch);
1539 xpc_part_deref(part);
1543 xpc_disconnect_wait(ch_number);
1547 * To disconnect a channel, and reflect it back to all who may be waiting.
1549 * An OPEN is not allowed until XPC_C_DISCONNECTING is cleared by
1550 * xpc_process_disconnect(), and if set, XPC_C_WDISCONNECT is cleared by
1551 * xpc_disconnect_wait().
1553 * THE CHANNEL IS TO BE LOCKED BY THE CALLER AND WILL REMAIN LOCKED UPON RETURN.
1556 xpc_disconnect_channel(const int line, struct xpc_channel *ch,
1557 enum xpc_retval reason, unsigned long *irq_flags)
1559 u32 channel_was_connected = (ch->flags & XPC_C_CONNECTED);
1561 DBUG_ON(!spin_is_locked(&ch->lock));
1563 if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_DISCONNECTED)) {
1566 DBUG_ON(!(ch->flags & (XPC_C_CONNECTING | XPC_C_CONNECTED)));
1568 dev_dbg(xpc_chan, "reason=%d, line=%d, partid=%d, channel=%d\n",
1569 reason, line, ch->partid, ch->number);
1571 XPC_SET_REASON(ch, reason, line);
1573 ch->flags |= (XPC_C_CLOSEREQUEST | XPC_C_DISCONNECTING);
1574 /* some of these may not have been set */
1575 ch->flags &= ~(XPC_C_OPENREQUEST | XPC_C_OPENREPLY |
1576 XPC_C_ROPENREQUEST | XPC_C_ROPENREPLY |
1577 XPC_C_CONNECTING | XPC_C_CONNECTED);
1579 xpc_IPI_send_closerequest(ch, irq_flags);
1581 if (channel_was_connected) {
1582 ch->flags |= XPC_C_WASCONNECTED;
1585 spin_unlock_irqrestore(&ch->lock, *irq_flags);
1587 /* wake all idle kthreads so they can exit */
1588 if (atomic_read(&ch->kthreads_idle) > 0) {
1589 wake_up_all(&ch->idle_wq);
1591 } else if ((ch->flags & XPC_C_CONNECTEDCALLOUT_MADE) &&
1592 !(ch->flags & XPC_C_DISCONNECTINGCALLOUT)) {
1593 /* start a kthread that will do the xpcDisconnecting callout */
1594 xpc_create_kthreads(ch, 1, 1);
1597 /* wake those waiting to allocate an entry from the local msg queue */
1598 if (atomic_read(&ch->n_on_msg_allocate_wq) > 0) {
1599 wake_up(&ch->msg_allocate_wq);
1602 spin_lock_irqsave(&ch->lock, *irq_flags);
1606 xpc_disconnect_callout(struct xpc_channel *ch, enum xpc_retval reason)
1609 * Let the channel's registerer know that the channel is being
1610 * disconnected. We don't want to do this if the registerer was never
1611 * informed of a connection being made.
1614 if (ch->func != NULL) {
1615 dev_dbg(xpc_chan, "ch->func() called, reason=%d, partid=%d, "
1616 "channel=%d\n", reason, ch->partid, ch->number);
1618 ch->func(reason, ch->partid, ch->number, NULL, ch->key);
1620 dev_dbg(xpc_chan, "ch->func() returned, reason=%d, partid=%d, "
1621 "channel=%d\n", reason, ch->partid, ch->number);
1626 * Wait for a message entry to become available for the specified channel,
1627 * but don't wait any longer than 1 jiffy.
1629 static enum xpc_retval
1630 xpc_allocate_msg_wait(struct xpc_channel *ch)
1632 enum xpc_retval ret;
1634 if (ch->flags & XPC_C_DISCONNECTING) {
1635 DBUG_ON(ch->reason == xpcInterrupted); // >>> Is this true?
1639 atomic_inc(&ch->n_on_msg_allocate_wq);
1640 ret = interruptible_sleep_on_timeout(&ch->msg_allocate_wq, 1);
1641 atomic_dec(&ch->n_on_msg_allocate_wq);
1643 if (ch->flags & XPC_C_DISCONNECTING) {
1645 DBUG_ON(ch->reason == xpcInterrupted); // >>> Is this true?
1646 } else if (ret == 0) {
1649 ret = xpcInterrupted;
1656 * Allocate an entry for a message from the message queue associated with the
1657 * specified channel.
1659 static enum xpc_retval
1660 xpc_allocate_msg(struct xpc_channel *ch, u32 flags,
1661 struct xpc_msg **address_of_msg)
1663 struct xpc_msg *msg;
1664 enum xpc_retval ret;
1667 /* this reference will be dropped in xpc_send_msg() */
1668 xpc_msgqueue_ref(ch);
1670 if (ch->flags & XPC_C_DISCONNECTING) {
1671 xpc_msgqueue_deref(ch);
1674 if (!(ch->flags & XPC_C_CONNECTED)) {
1675 xpc_msgqueue_deref(ch);
1676 return xpcNotConnected;
1680 * Get the next available message entry from the local message queue.
1681 * If none are available, we'll make sure that we grab the latest
1688 put = (volatile s64)ch->w_local_GP.put;
1689 if (put - (volatile s64)ch->w_remote_GP.get <
1690 ch->local_nentries) {
1692 /* There are available message entries. We need to try
1693 * to secure one for ourselves. We'll do this by trying
1694 * to increment w_local_GP.put as long as someone else
1695 * doesn't beat us to it. If they do, we'll have to
1698 if (cmpxchg(&ch->w_local_GP.put, put, put + 1) == put) {
1699 /* we got the entry referenced by put */
1702 continue; /* try again */
1706 * There aren't any available msg entries at this time.
1708 * In waiting for a message entry to become available,
1709 * we set a timeout in case the other side is not
1710 * sending completion IPIs. This lets us fake an IPI
1711 * that will cause the IPI handler to fetch the latest
1712 * GP values as if an IPI was sent by the other side.
1714 if (ret == xpcTimeout) {
1715 xpc_IPI_send_local_msgrequest(ch);
1718 if (flags & XPC_NOWAIT) {
1719 xpc_msgqueue_deref(ch);
1723 ret = xpc_allocate_msg_wait(ch);
1724 if (ret != xpcInterrupted && ret != xpcTimeout) {
1725 xpc_msgqueue_deref(ch);
1730 /* get the message's address and initialize it */
1731 msg = (struct xpc_msg *)((u64)ch->local_msgqueue +
1732 (put % ch->local_nentries) * ch->msg_size);
1734 DBUG_ON(msg->flags != 0);
1737 dev_dbg(xpc_chan, "w_local_GP.put changed to %ld; msg=0x%p, "
1738 "msg_number=%ld, partid=%d, channel=%d\n", put + 1,
1739 (void *)msg, msg->number, ch->partid, ch->number);
1741 *address_of_msg = msg;
1747 * Allocate an entry for a message from the message queue associated with the
1748 * specified channel. NOTE that this routine can sleep waiting for a message
1749 * entry to become available. To not sleep, pass in the XPC_NOWAIT flag.
1753 * partid - ID of partition to which the channel is connected.
1754 * ch_number - channel #.
1755 * flags - see xpc.h for valid flags.
1756 * payload - address of the allocated payload area pointer (filled in on
1757 * return) in which the user-defined message is constructed.
1760 xpc_initiate_allocate(partid_t partid, int ch_number, u32 flags, void **payload)
1762 struct xpc_partition *part = &xpc_partitions[partid];
1763 enum xpc_retval ret = xpcUnknownReason;
1764 struct xpc_msg *msg = NULL;
1766 DBUG_ON(partid <= 0 || partid >= XP_MAX_PARTITIONS);
1767 DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
1771 if (xpc_part_ref(part)) {
1772 ret = xpc_allocate_msg(&part->channels[ch_number], flags, &msg);
1773 xpc_part_deref(part);
1776 *payload = &msg->payload;
1784 * Now we actually send the messages that are ready to be sent by advancing
1785 * the local message queue's Put value and then send an IPI to the recipient
1789 xpc_send_msgs(struct xpc_channel *ch, s64 initial_put)
1791 struct xpc_msg *msg;
1792 s64 put = initial_put + 1;
1798 if (put == (volatile s64)ch->w_local_GP.put) {
1802 msg = (struct xpc_msg *)((u64)ch->local_msgqueue +
1803 (put % ch->local_nentries) *
1806 if (!(msg->flags & XPC_M_READY)) {
1813 if (put == initial_put) {
1814 /* nothing's changed */
1818 if (cmpxchg_rel(&ch->local_GP->put, initial_put, put) !=
1820 /* someone else beat us to it */
1821 DBUG_ON((volatile s64)ch->local_GP->put < initial_put);
1825 /* we just set the new value of local_GP->put */
1827 dev_dbg(xpc_chan, "local_GP->put changed to %ld, partid=%d, "
1828 "channel=%d\n", put, ch->partid, ch->number);
1833 * We need to ensure that the message referenced by
1834 * local_GP->put is not XPC_M_READY or that local_GP->put
1835 * equals w_local_GP.put, so we'll go have a look.
1841 xpc_IPI_send_msgrequest(ch);
1846 * Common code that does the actual sending of the message by advancing the
1847 * local message queue's Put value and sends an IPI to the partition the
1848 * message is being sent to.
1850 static enum xpc_retval
1851 xpc_send_msg(struct xpc_channel *ch, struct xpc_msg *msg, u8 notify_type,
1852 xpc_notify_func func, void *key)
1854 enum xpc_retval ret = xpcSuccess;
1855 struct xpc_notify *notify = notify;
1856 s64 put, msg_number = msg->number;
1858 DBUG_ON(notify_type == XPC_N_CALL && func == NULL);
1859 DBUG_ON((((u64)msg - (u64)ch->local_msgqueue) / ch->msg_size) !=
1860 msg_number % ch->local_nentries);
1861 DBUG_ON(msg->flags & XPC_M_READY);
1863 if (ch->flags & XPC_C_DISCONNECTING) {
1864 /* drop the reference grabbed in xpc_allocate_msg() */
1865 xpc_msgqueue_deref(ch);
1869 if (notify_type != 0) {
1871 * Tell the remote side to send an ACK interrupt when the
1872 * message has been delivered.
1874 msg->flags |= XPC_M_INTERRUPT;
1876 atomic_inc(&ch->n_to_notify);
1878 notify = &ch->notify_queue[msg_number % ch->local_nentries];
1879 notify->func = func;
1881 notify->type = notify_type;
1883 // >>> is a mb() needed here?
1885 if (ch->flags & XPC_C_DISCONNECTING) {
1887 * An error occurred between our last error check and
1888 * this one. We will try to clear the type field from
1889 * the notify entry. If we succeed then
1890 * xpc_disconnect_channel() didn't already process
1893 if (cmpxchg(¬ify->type, notify_type, 0) ==
1895 atomic_dec(&ch->n_to_notify);
1899 /* drop the reference grabbed in xpc_allocate_msg() */
1900 xpc_msgqueue_deref(ch);
1905 msg->flags |= XPC_M_READY;
1908 * The preceding store of msg->flags must occur before the following
1909 * load of ch->local_GP->put.
1913 /* see if the message is next in line to be sent, if so send it */
1915 put = ch->local_GP->put;
1916 if (put == msg_number) {
1917 xpc_send_msgs(ch, put);
1920 /* drop the reference grabbed in xpc_allocate_msg() */
1921 xpc_msgqueue_deref(ch);
1926 * Send a message previously allocated using xpc_initiate_allocate() on the
1927 * specified channel connected to the specified partition.
1929 * This routine will not wait for the message to be received, nor will
1930 * notification be given when it does happen. Once this routine has returned
1931 * the message entry allocated via xpc_initiate_allocate() is no longer
1932 * accessable to the caller.
1934 * This routine, although called by users, does not call xpc_part_ref() to
1935 * ensure that the partition infrastructure is in place. It relies on the
1936 * fact that we called xpc_msgqueue_ref() in xpc_allocate_msg().
1940 * partid - ID of partition to which the channel is connected.
1941 * ch_number - channel # to send message on.
1942 * payload - pointer to the payload area allocated via
1943 * xpc_initiate_allocate().
1946 xpc_initiate_send(partid_t partid, int ch_number, void *payload)
1948 struct xpc_partition *part = &xpc_partitions[partid];
1949 struct xpc_msg *msg = XPC_MSG_ADDRESS(payload);
1950 enum xpc_retval ret;
1952 dev_dbg(xpc_chan, "msg=0x%p, partid=%d, channel=%d\n", (void *)msg,
1955 DBUG_ON(partid <= 0 || partid >= XP_MAX_PARTITIONS);
1956 DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
1957 DBUG_ON(msg == NULL);
1959 ret = xpc_send_msg(&part->channels[ch_number], msg, 0, NULL, NULL);
1965 * Send a message previously allocated using xpc_initiate_allocate on the
1966 * specified channel connected to the specified partition.
1968 * This routine will not wait for the message to be sent. Once this routine
1969 * has returned the message entry allocated via xpc_initiate_allocate() is no
1970 * longer accessable to the caller.
1972 * Once the remote end of the channel has received the message, the function
1973 * passed as an argument to xpc_initiate_send_notify() will be called. This
1974 * allows the sender to free up or re-use any buffers referenced by the
1975 * message, but does NOT mean the message has been processed at the remote
1976 * end by a receiver.
1978 * If this routine returns an error, the caller's function will NOT be called.
1980 * This routine, although called by users, does not call xpc_part_ref() to
1981 * ensure that the partition infrastructure is in place. It relies on the
1982 * fact that we called xpc_msgqueue_ref() in xpc_allocate_msg().
1986 * partid - ID of partition to which the channel is connected.
1987 * ch_number - channel # to send message on.
1988 * payload - pointer to the payload area allocated via
1989 * xpc_initiate_allocate().
1990 * func - function to call with asynchronous notification of message
1991 * receipt. THIS FUNCTION MUST BE NON-BLOCKING.
1992 * key - user-defined key to be passed to the function when it's called.
1995 xpc_initiate_send_notify(partid_t partid, int ch_number, void *payload,
1996 xpc_notify_func func, void *key)
1998 struct xpc_partition *part = &xpc_partitions[partid];
1999 struct xpc_msg *msg = XPC_MSG_ADDRESS(payload);
2000 enum xpc_retval ret;
2002 dev_dbg(xpc_chan, "msg=0x%p, partid=%d, channel=%d\n", (void *)msg,
2005 DBUG_ON(partid <= 0 || partid >= XP_MAX_PARTITIONS);
2006 DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
2007 DBUG_ON(msg == NULL);
2008 DBUG_ON(func == NULL);
2010 ret = xpc_send_msg(&part->channels[ch_number], msg, XPC_N_CALL,
2015 static struct xpc_msg *
2016 xpc_pull_remote_msg(struct xpc_channel *ch, s64 get)
2018 struct xpc_partition *part = &xpc_partitions[ch->partid];
2019 struct xpc_msg *remote_msg, *msg;
2020 u32 msg_index, nmsgs;
2022 enum xpc_retval ret;
2024 if (mutex_lock_interruptible(&ch->msg_to_pull_mutex) != 0) {
2025 /* we were interrupted by a signal */
2029 while (get >= ch->next_msg_to_pull) {
2031 /* pull as many messages as are ready and able to be pulled */
2033 msg_index = ch->next_msg_to_pull % ch->remote_nentries;
2035 DBUG_ON(ch->next_msg_to_pull >=
2036 (volatile s64)ch->w_remote_GP.put);
2037 nmsgs = (volatile s64)ch->w_remote_GP.put -
2038 ch->next_msg_to_pull;
2039 if (msg_index + nmsgs > ch->remote_nentries) {
2040 /* ignore the ones that wrap the msg queue for now */
2041 nmsgs = ch->remote_nentries - msg_index;
2044 msg_offset = msg_index * ch->msg_size;
2045 msg = (struct xpc_msg *)((u64)ch->remote_msgqueue + msg_offset);
2046 remote_msg = (struct xpc_msg *)(ch->remote_msgqueue_pa +
2049 if ((ret = xpc_pull_remote_cachelines(part, msg, remote_msg,
2050 nmsgs * ch->msg_size)) !=
2053 dev_dbg(xpc_chan, "failed to pull %d msgs starting with"
2054 " msg %ld from partition %d, channel=%d, "
2055 "ret=%d\n", nmsgs, ch->next_msg_to_pull,
2056 ch->partid, ch->number, ret);
2058 XPC_DEACTIVATE_PARTITION(part, ret);
2060 mutex_unlock(&ch->msg_to_pull_mutex);
2064 mb(); /* >>> this may not be needed, we're not sure */
2066 ch->next_msg_to_pull += nmsgs;
2069 mutex_unlock(&ch->msg_to_pull_mutex);
2071 /* return the message we were looking for */
2072 msg_offset = (get % ch->remote_nentries) * ch->msg_size;
2073 msg = (struct xpc_msg *)((u64)ch->remote_msgqueue + msg_offset);
2079 * Get a message to be delivered.
2081 static struct xpc_msg *
2082 xpc_get_deliverable_msg(struct xpc_channel *ch)
2084 struct xpc_msg *msg = NULL;
2088 if ((volatile u32)ch->flags & XPC_C_DISCONNECTING) {
2092 get = (volatile s64)ch->w_local_GP.get;
2093 if (get == (volatile s64)ch->w_remote_GP.put) {
2097 /* There are messages waiting to be pulled and delivered.
2098 * We need to try to secure one for ourselves. We'll do this
2099 * by trying to increment w_local_GP.get and hope that no one
2100 * else beats us to it. If they do, we'll we'll simply have
2101 * to try again for the next one.
2104 if (cmpxchg(&ch->w_local_GP.get, get, get + 1) == get) {
2105 /* we got the entry referenced by get */
2107 dev_dbg(xpc_chan, "w_local_GP.get changed to %ld, "
2108 "partid=%d, channel=%d\n", get + 1,
2109 ch->partid, ch->number);
2111 /* pull the message from the remote partition */
2113 msg = xpc_pull_remote_msg(ch, get);
2115 DBUG_ON(msg != NULL && msg->number != get);
2116 DBUG_ON(msg != NULL && (msg->flags & XPC_M_DONE));
2117 DBUG_ON(msg != NULL && !(msg->flags & XPC_M_READY));
2128 * Deliver a message to its intended recipient.
2131 xpc_deliver_msg(struct xpc_channel *ch)
2133 struct xpc_msg *msg;
2135 if ((msg = xpc_get_deliverable_msg(ch)) != NULL) {
2138 * This ref is taken to protect the payload itself from being
2139 * freed before the user is finished with it, which the user
2140 * indicates by calling xpc_initiate_received().
2142 xpc_msgqueue_ref(ch);
2144 atomic_inc(&ch->kthreads_active);
2146 if (ch->func != NULL) {
2147 dev_dbg(xpc_chan, "ch->func() called, msg=0x%p, "
2148 "msg_number=%ld, partid=%d, channel=%d\n",
2149 (void *)msg, msg->number, ch->partid,
2152 /* deliver the message to its intended recipient */
2153 ch->func(xpcMsgReceived, ch->partid, ch->number,
2154 &msg->payload, ch->key);
2156 dev_dbg(xpc_chan, "ch->func() returned, msg=0x%p, "
2157 "msg_number=%ld, partid=%d, channel=%d\n",
2158 (void *)msg, msg->number, ch->partid,
2162 atomic_dec(&ch->kthreads_active);
2167 * Now we actually acknowledge the messages that have been delivered and ack'd
2168 * by advancing the cached remote message queue's Get value and if requested
2169 * send an IPI to the message sender's partition.
2172 xpc_acknowledge_msgs(struct xpc_channel *ch, s64 initial_get, u8 msg_flags)
2174 struct xpc_msg *msg;
2175 s64 get = initial_get + 1;
2181 if (get == (volatile s64)ch->w_local_GP.get) {
2185 msg = (struct xpc_msg *)((u64)ch->remote_msgqueue +
2186 (get % ch->remote_nentries) *
2189 if (!(msg->flags & XPC_M_DONE)) {
2193 msg_flags |= msg->flags;
2197 if (get == initial_get) {
2198 /* nothing's changed */
2202 if (cmpxchg_rel(&ch->local_GP->get, initial_get, get) !=
2204 /* someone else beat us to it */
2205 DBUG_ON((volatile s64)ch->local_GP->get <= initial_get);
2209 /* we just set the new value of local_GP->get */
2211 dev_dbg(xpc_chan, "local_GP->get changed to %ld, partid=%d, "
2212 "channel=%d\n", get, ch->partid, ch->number);
2214 send_IPI = (msg_flags & XPC_M_INTERRUPT);
2217 * We need to ensure that the message referenced by
2218 * local_GP->get is not XPC_M_DONE or that local_GP->get
2219 * equals w_local_GP.get, so we'll go have a look.
2225 xpc_IPI_send_msgrequest(ch);
2230 * Acknowledge receipt of a delivered message.
2232 * If a message has XPC_M_INTERRUPT set, send an interrupt to the partition
2233 * that sent the message.
2235 * This function, although called by users, does not call xpc_part_ref() to
2236 * ensure that the partition infrastructure is in place. It relies on the
2237 * fact that we called xpc_msgqueue_ref() in xpc_deliver_msg().
2241 * partid - ID of partition to which the channel is connected.
2242 * ch_number - channel # message received on.
2243 * payload - pointer to the payload area allocated via
2244 * xpc_initiate_allocate().
2247 xpc_initiate_received(partid_t partid, int ch_number, void *payload)
2249 struct xpc_partition *part = &xpc_partitions[partid];
2250 struct xpc_channel *ch;
2251 struct xpc_msg *msg = XPC_MSG_ADDRESS(payload);
2252 s64 get, msg_number = msg->number;
2254 DBUG_ON(partid <= 0 || partid >= XP_MAX_PARTITIONS);
2255 DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
2257 ch = &part->channels[ch_number];
2259 dev_dbg(xpc_chan, "msg=0x%p, msg_number=%ld, partid=%d, channel=%d\n",
2260 (void *)msg, msg_number, ch->partid, ch->number);
2262 DBUG_ON((((u64)msg - (u64)ch->remote_msgqueue) / ch->msg_size) !=
2263 msg_number % ch->remote_nentries);
2264 DBUG_ON(msg->flags & XPC_M_DONE);
2266 msg->flags |= XPC_M_DONE;
2269 * The preceding store of msg->flags must occur before the following
2270 * load of ch->local_GP->get.
2275 * See if this message is next in line to be acknowledged as having
2278 get = ch->local_GP->get;
2279 if (get == msg_number) {
2280 xpc_acknowledge_msgs(ch, get, msg->flags);
2283 /* the call to xpc_msgqueue_ref() was done by xpc_deliver_msg() */
2284 xpc_msgqueue_deref(ch);