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) support - standard version.
12 * XPC provides a message passing capability that crosses partition
13 * boundaries. This module is made up of two parts:
15 * partition This part detects the presence/absence of other
16 * partitions. It provides a heartbeat and monitors
17 * the heartbeats of other partitions.
19 * channel This part manages the channels and sends/receives
20 * messages across them to/from other partitions.
22 * There are a couple of additional functions residing in XP, which
23 * provide an interface to XPC for its users.
28 * . We currently have no way to determine which nasid an IPI came
29 * from. Thus, xpc_IPI_send() does a remote AMO write followed by
30 * an IPI. The AMO indicates where data is to be pulled from, so
31 * after the IPI arrives, the remote partition checks the AMO word.
32 * The IPI can actually arrive before the AMO however, so other code
33 * must periodically check for this case. Also, remote AMO operations
34 * do not reliably time out. Thus we do a remote PIO read solely to
35 * know whether the remote partition is down and whether we should
36 * stop sending IPIs to it. This remote PIO read operation is set up
37 * in a special nofault region so SAL knows to ignore (and cleanup)
38 * any errors due to the remote AMO write, PIO read, and/or PIO
41 * If/when new hardware solves this IPI problem, we should abandon
42 * the current approach.
46 #include <linux/kernel.h>
47 #include <linux/module.h>
48 #include <linux/init.h>
49 #include <linux/sched.h>
50 #include <linux/syscalls.h>
51 #include <linux/cache.h>
52 #include <linux/interrupt.h>
53 #include <linux/delay.h>
54 #include <linux/reboot.h>
55 #include <linux/completion.h>
56 #include <linux/kdebug.h>
57 #include <asm/sn/intr.h>
58 #include <asm/sn/sn_sal.h>
59 #include <asm/uaccess.h>
62 /* define two XPC debug device structures to be used with dev_dbg() et al */
64 struct device_driver xpc_dbg_name = {
68 struct device xpc_part_dbg_subname = {
69 .bus_id = {0}, /* set to "part" at xpc_init() time */
70 .driver = &xpc_dbg_name
73 struct device xpc_chan_dbg_subname = {
74 .bus_id = {0}, /* set to "chan" at xpc_init() time */
75 .driver = &xpc_dbg_name
78 struct device *xpc_part = &xpc_part_dbg_subname;
79 struct device *xpc_chan = &xpc_chan_dbg_subname;
81 static int xpc_kdebug_ignore;
83 /* systune related variables for /proc/sys directories */
85 static int xpc_hb_interval = XPC_HB_DEFAULT_INTERVAL;
86 static int xpc_hb_min_interval = 1;
87 static int xpc_hb_max_interval = 10;
89 static int xpc_hb_check_interval = XPC_HB_CHECK_DEFAULT_INTERVAL;
90 static int xpc_hb_check_min_interval = 10;
91 static int xpc_hb_check_max_interval = 120;
93 int xpc_disengage_request_timelimit = XPC_DISENGAGE_REQUEST_DEFAULT_TIMELIMIT;
94 static int xpc_disengage_request_min_timelimit = 0;
95 static int xpc_disengage_request_max_timelimit = 120;
97 static ctl_table xpc_sys_xpc_hb_dir[] = {
99 .ctl_name = CTL_UNNUMBERED,
100 .procname = "hb_interval",
101 .data = &xpc_hb_interval,
102 .maxlen = sizeof(int),
104 .proc_handler = &proc_dointvec_minmax,
105 .strategy = &sysctl_intvec,
106 .extra1 = &xpc_hb_min_interval,
107 .extra2 = &xpc_hb_max_interval},
109 .ctl_name = CTL_UNNUMBERED,
110 .procname = "hb_check_interval",
111 .data = &xpc_hb_check_interval,
112 .maxlen = sizeof(int),
114 .proc_handler = &proc_dointvec_minmax,
115 .strategy = &sysctl_intvec,
116 .extra1 = &xpc_hb_check_min_interval,
117 .extra2 = &xpc_hb_check_max_interval},
120 static ctl_table xpc_sys_xpc_dir[] = {
122 .ctl_name = CTL_UNNUMBERED,
125 .child = xpc_sys_xpc_hb_dir},
127 .ctl_name = CTL_UNNUMBERED,
128 .procname = "disengage_request_timelimit",
129 .data = &xpc_disengage_request_timelimit,
130 .maxlen = sizeof(int),
132 .proc_handler = &proc_dointvec_minmax,
133 .strategy = &sysctl_intvec,
134 .extra1 = &xpc_disengage_request_min_timelimit,
135 .extra2 = &xpc_disengage_request_max_timelimit},
138 static ctl_table xpc_sys_dir[] = {
140 .ctl_name = CTL_UNNUMBERED,
143 .child = xpc_sys_xpc_dir},
146 static struct ctl_table_header *xpc_sysctl;
148 /* non-zero if any remote partition disengage request was timed out */
149 int xpc_disengage_request_timedout;
151 /* #of IRQs received */
152 static atomic_t xpc_act_IRQ_rcvd;
154 /* IRQ handler notifies this wait queue on receipt of an IRQ */
155 static DECLARE_WAIT_QUEUE_HEAD(xpc_act_IRQ_wq);
157 static unsigned long xpc_hb_check_timeout;
159 /* notification that the xpc_hb_checker thread has exited */
160 static DECLARE_COMPLETION(xpc_hb_checker_exited);
162 /* notification that the xpc_discovery thread has exited */
163 static DECLARE_COMPLETION(xpc_discovery_exited);
165 static struct timer_list xpc_hb_timer;
167 static void xpc_kthread_waitmsgs(struct xpc_partition *, struct xpc_channel *);
169 static int xpc_system_reboot(struct notifier_block *, unsigned long, void *);
170 static struct notifier_block xpc_reboot_notifier = {
171 .notifier_call = xpc_system_reboot,
174 static int xpc_system_die(struct notifier_block *, unsigned long, void *);
175 static struct notifier_block xpc_die_notifier = {
176 .notifier_call = xpc_system_die,
180 * Timer function to enforce the timelimit on the partition disengage request.
183 xpc_timeout_partition_disengage_request(unsigned long data)
185 struct xpc_partition *part = (struct xpc_partition *)data;
187 DBUG_ON(time_before(jiffies, part->disengage_request_timeout));
189 (void)xpc_partition_disengaged(part);
191 DBUG_ON(part->disengage_request_timeout != 0);
192 DBUG_ON(xpc_partition_engaged(1UL << XPC_PARTID(part)) != 0);
196 * Notify the heartbeat check thread that an IRQ has been received.
199 xpc_act_IRQ_handler(int irq, void *dev_id)
201 atomic_inc(&xpc_act_IRQ_rcvd);
202 wake_up_interruptible(&xpc_act_IRQ_wq);
207 * Timer to produce the heartbeat. The timer structures function is
208 * already set when this is initially called. A tunable is used to
209 * specify when the next timeout should occur.
212 xpc_hb_beater(unsigned long dummy)
214 xpc_vars->heartbeat++;
216 if (time_after_eq(jiffies, xpc_hb_check_timeout)) {
217 wake_up_interruptible(&xpc_act_IRQ_wq);
220 xpc_hb_timer.expires = jiffies + (xpc_hb_interval * HZ);
221 add_timer(&xpc_hb_timer);
225 * This thread is responsible for nearly all of the partition
226 * activation/deactivation.
229 xpc_hb_checker(void *ignore)
231 int last_IRQ_count = 0;
235 /* this thread was marked active by xpc_hb_init() */
237 daemonize(XPC_HB_CHECK_THREAD_NAME);
239 set_cpus_allowed(current, cpumask_of_cpu(XPC_HB_CHECK_CPU));
241 /* set our heartbeating to other partitions into motion */
242 xpc_hb_check_timeout = jiffies + (xpc_hb_check_interval * HZ);
245 while (!(volatile int)xpc_exiting) {
247 dev_dbg(xpc_part, "woke up with %d ticks rem; %d IRQs have "
249 (int)(xpc_hb_check_timeout - jiffies),
250 atomic_read(&xpc_act_IRQ_rcvd) - last_IRQ_count);
252 /* checking of remote heartbeats is skewed by IRQ handling */
253 if (time_after_eq(jiffies, xpc_hb_check_timeout)) {
254 dev_dbg(xpc_part, "checking remote heartbeats\n");
255 xpc_check_remote_hb();
258 * We need to periodically recheck to ensure no
259 * IPI/AMO pairs have been missed. That check
260 * must always reset xpc_hb_check_timeout.
265 /* check for outstanding IRQs */
266 new_IRQ_count = atomic_read(&xpc_act_IRQ_rcvd);
267 if (last_IRQ_count < new_IRQ_count || force_IRQ != 0) {
270 dev_dbg(xpc_part, "found an IRQ to process; will be "
271 "resetting xpc_hb_check_timeout\n");
273 last_IRQ_count += xpc_identify_act_IRQ_sender();
274 if (last_IRQ_count < new_IRQ_count) {
275 /* retry once to help avoid missing AMO */
276 (void)xpc_identify_act_IRQ_sender();
278 last_IRQ_count = new_IRQ_count;
280 xpc_hb_check_timeout = jiffies +
281 (xpc_hb_check_interval * HZ);
284 /* wait for IRQ or timeout */
285 (void)wait_event_interruptible(xpc_act_IRQ_wq,
287 atomic_read(&xpc_act_IRQ_rcvd)
288 || time_after_eq(jiffies,
289 xpc_hb_check_timeout) ||
290 (volatile int)xpc_exiting));
293 dev_dbg(xpc_part, "heartbeat checker is exiting\n");
295 /* mark this thread as having exited */
296 complete(&xpc_hb_checker_exited);
301 * This thread will attempt to discover other partitions to activate
302 * based on info provided by SAL. This new thread is short lived and
303 * will exit once discovery is complete.
306 xpc_initiate_discovery(void *ignore)
308 daemonize(XPC_DISCOVERY_THREAD_NAME);
312 dev_dbg(xpc_part, "discovery thread is exiting\n");
314 /* mark this thread as having exited */
315 complete(&xpc_discovery_exited);
320 * Establish first contact with the remote partititon. This involves pulling
321 * the XPC per partition variables from the remote partition and waiting for
322 * the remote partition to pull ours.
324 static enum xpc_retval
325 xpc_make_first_contact(struct xpc_partition *part)
329 while ((ret = xpc_pull_remote_vars_part(part)) != xpcSuccess) {
330 if (ret != xpcRetry) {
331 XPC_DEACTIVATE_PARTITION(part, ret);
335 dev_dbg(xpc_chan, "waiting to make first contact with "
336 "partition %d\n", XPC_PARTID(part));
338 /* wait a 1/4 of a second or so */
339 (void)msleep_interruptible(250);
341 if (part->act_state == XPC_P_DEACTIVATING) {
346 return xpc_mark_partition_active(part);
350 * The first kthread assigned to a newly activated partition is the one
351 * created by XPC HB with which it calls xpc_partition_up(). XPC hangs on to
352 * that kthread until the partition is brought down, at which time that kthread
353 * returns back to XPC HB. (The return of that kthread will signify to XPC HB
354 * that XPC has dismantled all communication infrastructure for the associated
355 * partition.) This kthread becomes the channel manager for that partition.
357 * Each active partition has a channel manager, who, besides connecting and
358 * disconnecting channels, will ensure that each of the partition's connected
359 * channels has the required number of assigned kthreads to get the work done.
362 xpc_channel_mgr(struct xpc_partition *part)
364 while (part->act_state != XPC_P_DEACTIVATING ||
365 atomic_read(&part->nchannels_active) > 0 ||
366 !xpc_partition_disengaged(part)) {
368 xpc_process_channel_activity(part);
371 * Wait until we've been requested to activate kthreads or
372 * all of the channel's message queues have been torn down or
373 * a signal is pending.
375 * The channel_mgr_requests is set to 1 after being awakened,
376 * This is done to prevent the channel mgr from making one pass
377 * through the loop for each request, since he will
378 * be servicing all the requests in one pass. The reason it's
379 * set to 1 instead of 0 is so that other kthreads will know
380 * that the channel mgr is running and won't bother trying to
383 atomic_dec(&part->channel_mgr_requests);
384 (void)wait_event_interruptible(part->channel_mgr_wq,
386 (&part->channel_mgr_requests) >
389 local_IPI_amo != 0 ||
390 ((volatile u8)part->act_state ==
391 XPC_P_DEACTIVATING &&
395 xpc_partition_disengaged
397 atomic_set(&part->channel_mgr_requests, 1);
399 // >>> Does it need to wakeup periodically as well? In case we
400 // >>> miscalculated the #of kthreads to wakeup or create?
405 * When XPC HB determines that a partition has come up, it will create a new
406 * kthread and that kthread will call this function to attempt to set up the
407 * basic infrastructure used for Cross Partition Communication with the newly
410 * The kthread that was created by XPC HB and which setup the XPC
411 * infrastructure will remain assigned to the partition until the partition
412 * goes down. At which time the kthread will teardown the XPC infrastructure
415 * XPC HB will put the remote partition's XPC per partition specific variables
416 * physical address into xpc_partitions[partid].remote_vars_part_pa prior to
417 * calling xpc_partition_up().
420 xpc_partition_up(struct xpc_partition *part)
422 DBUG_ON(part->channels != NULL);
424 dev_dbg(xpc_chan, "activating partition %d\n", XPC_PARTID(part));
426 if (xpc_setup_infrastructure(part) != xpcSuccess) {
431 * The kthread that XPC HB called us with will become the
432 * channel manager for this partition. It will not return
433 * back to XPC HB until the partition's XPC infrastructure
434 * has been dismantled.
437 (void)xpc_part_ref(part); /* this will always succeed */
439 if (xpc_make_first_contact(part) == xpcSuccess) {
440 xpc_channel_mgr(part);
443 xpc_part_deref(part);
445 xpc_teardown_infrastructure(part);
449 xpc_activating(void *__partid)
451 partid_t partid = (u64)__partid;
452 struct xpc_partition *part = &xpc_partitions[partid];
453 unsigned long irq_flags;
454 struct sched_param param = {.sched_priority = MAX_RT_PRIO - 1 };
457 DBUG_ON(partid <= 0 || partid >= XP_MAX_PARTITIONS);
459 spin_lock_irqsave(&part->act_lock, irq_flags);
461 if (part->act_state == XPC_P_DEACTIVATING) {
462 part->act_state = XPC_P_INACTIVE;
463 spin_unlock_irqrestore(&part->act_lock, irq_flags);
464 part->remote_rp_pa = 0;
468 /* indicate the thread is activating */
469 DBUG_ON(part->act_state != XPC_P_ACTIVATION_REQ);
470 part->act_state = XPC_P_ACTIVATING;
472 XPC_SET_REASON(part, 0, 0);
473 spin_unlock_irqrestore(&part->act_lock, irq_flags);
475 dev_dbg(xpc_part, "bringing partition %d up\n", partid);
477 daemonize("xpc%02d", partid);
480 * This thread needs to run at a realtime priority to prevent a
481 * significant performance degradation.
483 ret = sched_setscheduler(current, SCHED_FIFO, ¶m);
485 dev_warn(xpc_part, "unable to set pid %d to a realtime "
486 "priority, ret=%d\n", current->pid, ret);
489 /* allow this thread and its children to run on any CPU */
490 set_cpus_allowed(current, CPU_MASK_ALL);
493 * Register the remote partition's AMOs with SAL so it can handle
494 * and cleanup errors within that address range should the remote
495 * partition go down. We don't unregister this range because it is
496 * difficult to tell when outstanding writes to the remote partition
497 * are finished and thus when it is safe to unregister. This should
498 * not result in wasted space in the SAL xp_addr_region table because
499 * we should get the same page for remote_amos_page_pa after module
500 * reloads and system reboots.
502 if (sn_register_xp_addr_region(part->remote_amos_page_pa,
504 dev_warn(xpc_part, "xpc_partition_up(%d) failed to register "
505 "xp_addr region\n", partid);
507 spin_lock_irqsave(&part->act_lock, irq_flags);
508 part->act_state = XPC_P_INACTIVE;
509 XPC_SET_REASON(part, xpcPhysAddrRegFailed, __LINE__);
510 spin_unlock_irqrestore(&part->act_lock, irq_flags);
511 part->remote_rp_pa = 0;
515 xpc_allow_hb(partid, xpc_vars);
516 xpc_IPI_send_activated(part);
519 * xpc_partition_up() holds this thread and marks this partition as
520 * XPC_P_ACTIVE by calling xpc_hb_mark_active().
522 (void)xpc_partition_up(part);
524 xpc_disallow_hb(partid, xpc_vars);
525 xpc_mark_partition_inactive(part);
527 if (part->reason == xpcReactivating) {
528 /* interrupting ourselves results in activating partition */
529 xpc_IPI_send_reactivate(part);
536 xpc_activate_partition(struct xpc_partition *part)
538 partid_t partid = XPC_PARTID(part);
539 unsigned long irq_flags;
542 spin_lock_irqsave(&part->act_lock, irq_flags);
544 DBUG_ON(part->act_state != XPC_P_INACTIVE);
546 part->act_state = XPC_P_ACTIVATION_REQ;
547 XPC_SET_REASON(part, xpcCloneKThread, __LINE__);
549 spin_unlock_irqrestore(&part->act_lock, irq_flags);
551 pid = kernel_thread(xpc_activating, (void *)((u64)partid), 0);
553 if (unlikely(pid <= 0)) {
554 spin_lock_irqsave(&part->act_lock, irq_flags);
555 part->act_state = XPC_P_INACTIVE;
556 XPC_SET_REASON(part, xpcCloneKThreadFailed, __LINE__);
557 spin_unlock_irqrestore(&part->act_lock, irq_flags);
562 * Handle the receipt of a SGI_XPC_NOTIFY IRQ by seeing whether the specified
563 * partition actually sent it. Since SGI_XPC_NOTIFY IRQs may be shared by more
564 * than one partition, we use an AMO_t structure per partition to indicate
565 * whether a partition has sent an IPI or not. >>> If it has, then wake up the
566 * associated kthread to handle it.
568 * All SGI_XPC_NOTIFY IRQs received by XPC are the result of IPIs sent by XPC
569 * running on other partitions.
571 * Noteworthy Arguments:
573 * irq - Interrupt ReQuest number. NOT USED.
575 * dev_id - partid of IPI's potential sender.
578 xpc_notify_IRQ_handler(int irq, void *dev_id)
580 partid_t partid = (partid_t) (u64)dev_id;
581 struct xpc_partition *part = &xpc_partitions[partid];
583 DBUG_ON(partid <= 0 || partid >= XP_MAX_PARTITIONS);
585 if (xpc_part_ref(part)) {
586 xpc_check_for_channel_activity(part);
588 xpc_part_deref(part);
594 * Check to see if xpc_notify_IRQ_handler() dropped any IPIs on the floor
595 * because the write to their associated IPI amo completed after the IRQ/IPI
599 xpc_dropped_IPI_check(struct xpc_partition *part)
601 if (xpc_part_ref(part)) {
602 xpc_check_for_channel_activity(part);
604 part->dropped_IPI_timer.expires = jiffies +
605 XPC_P_DROPPED_IPI_WAIT;
606 add_timer(&part->dropped_IPI_timer);
607 xpc_part_deref(part);
612 xpc_activate_kthreads(struct xpc_channel *ch, int needed)
614 int idle = atomic_read(&ch->kthreads_idle);
615 int assigned = atomic_read(&ch->kthreads_assigned);
618 DBUG_ON(needed <= 0);
621 wakeup = (needed > idle) ? idle : needed;
624 dev_dbg(xpc_chan, "wakeup %d idle kthreads, partid=%d, "
625 "channel=%d\n", wakeup, ch->partid, ch->number);
627 /* only wakeup the requested number of kthreads */
628 wake_up_nr(&ch->idle_wq, wakeup);
635 if (needed + assigned > ch->kthreads_assigned_limit) {
636 needed = ch->kthreads_assigned_limit - assigned;
637 // >>>should never be less than 0
643 dev_dbg(xpc_chan, "create %d new kthreads, partid=%d, channel=%d\n",
644 needed, ch->partid, ch->number);
646 xpc_create_kthreads(ch, needed, 0);
650 * This function is where XPC's kthreads wait for messages to deliver.
653 xpc_kthread_waitmsgs(struct xpc_partition *part, struct xpc_channel *ch)
656 /* deliver messages to their intended recipients */
658 while ((volatile s64)ch->w_local_GP.get <
659 (volatile s64)ch->w_remote_GP.put &&
660 !((volatile u32)ch->flags & XPC_C_DISCONNECTING)) {
664 if (atomic_inc_return(&ch->kthreads_idle) >
665 ch->kthreads_idle_limit) {
666 /* too many idle kthreads on this channel */
667 atomic_dec(&ch->kthreads_idle);
671 dev_dbg(xpc_chan, "idle kthread calling "
672 "wait_event_interruptible_exclusive()\n");
674 (void)wait_event_interruptible_exclusive(ch->idle_wq,
681 XPC_C_DISCONNECTING)));
683 atomic_dec(&ch->kthreads_idle);
685 } while (!((volatile u32)ch->flags & XPC_C_DISCONNECTING));
689 xpc_daemonize_kthread(void *args)
691 partid_t partid = XPC_UNPACK_ARG1(args);
692 u16 ch_number = XPC_UNPACK_ARG2(args);
693 struct xpc_partition *part = &xpc_partitions[partid];
694 struct xpc_channel *ch;
696 unsigned long irq_flags;
698 daemonize("xpc%02dc%d", partid, ch_number);
700 dev_dbg(xpc_chan, "kthread starting, partid=%d, channel=%d\n",
703 ch = &part->channels[ch_number];
705 if (!(ch->flags & XPC_C_DISCONNECTING)) {
707 /* let registerer know that connection has been established */
709 spin_lock_irqsave(&ch->lock, irq_flags);
710 if (!(ch->flags & XPC_C_CONNECTEDCALLOUT)) {
711 ch->flags |= XPC_C_CONNECTEDCALLOUT;
712 spin_unlock_irqrestore(&ch->lock, irq_flags);
714 xpc_connected_callout(ch);
716 spin_lock_irqsave(&ch->lock, irq_flags);
717 ch->flags |= XPC_C_CONNECTEDCALLOUT_MADE;
718 spin_unlock_irqrestore(&ch->lock, irq_flags);
721 * It is possible that while the callout was being
722 * made that the remote partition sent some messages.
723 * If that is the case, we may need to activate
724 * additional kthreads to help deliver them. We only
725 * need one less than total #of messages to deliver.
727 n_needed = ch->w_remote_GP.put - ch->w_local_GP.get - 1;
728 if (n_needed > 0 && !(ch->flags & XPC_C_DISCONNECTING)) {
729 xpc_activate_kthreads(ch, n_needed);
732 spin_unlock_irqrestore(&ch->lock, irq_flags);
735 xpc_kthread_waitmsgs(part, ch);
738 /* let registerer know that connection is disconnecting */
740 spin_lock_irqsave(&ch->lock, irq_flags);
741 if ((ch->flags & XPC_C_CONNECTEDCALLOUT_MADE) &&
742 !(ch->flags & XPC_C_DISCONNECTINGCALLOUT)) {
743 ch->flags |= XPC_C_DISCONNECTINGCALLOUT;
744 spin_unlock_irqrestore(&ch->lock, irq_flags);
746 xpc_disconnect_callout(ch, xpcDisconnecting);
748 spin_lock_irqsave(&ch->lock, irq_flags);
749 ch->flags |= XPC_C_DISCONNECTINGCALLOUT_MADE;
751 spin_unlock_irqrestore(&ch->lock, irq_flags);
753 if (atomic_dec_return(&ch->kthreads_assigned) == 0) {
754 if (atomic_dec_return(&part->nchannels_engaged) == 0) {
755 xpc_mark_partition_disengaged(part);
756 xpc_IPI_send_disengage(part);
760 xpc_msgqueue_deref(ch);
762 dev_dbg(xpc_chan, "kthread exiting, partid=%d, channel=%d\n",
765 xpc_part_deref(part);
770 * For each partition that XPC has established communications with, there is
771 * a minimum of one kernel thread assigned to perform any operation that
772 * may potentially sleep or block (basically the callouts to the asynchronous
773 * functions registered via xpc_connect()).
775 * Additional kthreads are created and destroyed by XPC as the workload
778 * A kthread is assigned to one of the active channels that exists for a given
782 xpc_create_kthreads(struct xpc_channel *ch, int needed,
783 int ignore_disconnecting)
785 unsigned long irq_flags;
787 u64 args = XPC_PACK_ARGS(ch->partid, ch->number);
788 struct xpc_partition *part = &xpc_partitions[ch->partid];
790 while (needed-- > 0) {
793 * The following is done on behalf of the newly created
794 * kthread. That kthread is responsible for doing the
795 * counterpart to the following before it exits.
797 if (ignore_disconnecting) {
798 if (!atomic_inc_not_zero(&ch->kthreads_assigned)) {
799 /* kthreads assigned had gone to zero */
801 XPC_C_DISCONNECTINGCALLOUT_MADE));
805 } else if (ch->flags & XPC_C_DISCONNECTING) {
808 } else if (atomic_inc_return(&ch->kthreads_assigned) == 1) {
809 if (atomic_inc_return(&part->nchannels_engaged) == 1)
810 xpc_mark_partition_engaged(part);
812 (void)xpc_part_ref(part);
813 xpc_msgqueue_ref(ch);
815 pid = kernel_thread(xpc_daemonize_kthread, (void *)args, 0);
817 /* the fork failed */
820 * NOTE: if (ignore_disconnecting &&
821 * !(ch->flags & XPC_C_DISCONNECTINGCALLOUT)) is true,
822 * then we'll deadlock if all other kthreads assigned
823 * to this channel are blocked in the channel's
824 * registerer, because the only thing that will unblock
825 * them is the xpcDisconnecting callout that this
826 * failed kernel_thread would have made.
829 if (atomic_dec_return(&ch->kthreads_assigned) == 0 &&
830 atomic_dec_return(&part->nchannels_engaged) == 0) {
831 xpc_mark_partition_disengaged(part);
832 xpc_IPI_send_disengage(part);
834 xpc_msgqueue_deref(ch);
835 xpc_part_deref(part);
837 if (atomic_read(&ch->kthreads_assigned) <
838 ch->kthreads_idle_limit) {
840 * Flag this as an error only if we have an
841 * insufficient #of kthreads for the channel
844 spin_lock_irqsave(&ch->lock, irq_flags);
845 XPC_DISCONNECT_CHANNEL(ch, xpcLackOfResources,
847 spin_unlock_irqrestore(&ch->lock, irq_flags);
852 ch->kthreads_created++; // >>> temporary debug only!!!
857 xpc_disconnect_wait(int ch_number)
859 unsigned long irq_flags;
861 struct xpc_partition *part;
862 struct xpc_channel *ch;
863 int wakeup_channel_mgr;
865 /* now wait for all callouts to the caller's function to cease */
866 for (partid = 1; partid < XP_MAX_PARTITIONS; partid++) {
867 part = &xpc_partitions[partid];
869 if (!xpc_part_ref(part)) {
873 ch = &part->channels[ch_number];
875 if (!(ch->flags & XPC_C_WDISCONNECT)) {
876 xpc_part_deref(part);
880 wait_for_completion(&ch->wdisconnect_wait);
882 spin_lock_irqsave(&ch->lock, irq_flags);
883 DBUG_ON(!(ch->flags & XPC_C_DISCONNECTED));
884 wakeup_channel_mgr = 0;
886 if (ch->delayed_IPI_flags) {
887 if (part->act_state != XPC_P_DEACTIVATING) {
888 spin_lock(&part->IPI_lock);
889 XPC_SET_IPI_FLAGS(part->local_IPI_amo,
891 ch->delayed_IPI_flags);
892 spin_unlock(&part->IPI_lock);
893 wakeup_channel_mgr = 1;
895 ch->delayed_IPI_flags = 0;
898 ch->flags &= ~XPC_C_WDISCONNECT;
899 spin_unlock_irqrestore(&ch->lock, irq_flags);
901 if (wakeup_channel_mgr) {
902 xpc_wakeup_channel_mgr(part);
905 xpc_part_deref(part);
910 xpc_do_exit(enum xpc_retval reason)
913 int active_part_count, printed_waiting_msg = 0;
914 struct xpc_partition *part;
915 unsigned long printmsg_time, disengage_request_timeout = 0;
917 /* a 'rmmod XPC' and a 'reboot' cannot both end up here together */
918 DBUG_ON(xpc_exiting == 1);
921 * Let the heartbeat checker thread and the discovery thread
922 * (if one is running) know that they should exit. Also wake up
923 * the heartbeat checker thread in case it's sleeping.
926 wake_up_interruptible(&xpc_act_IRQ_wq);
928 /* ignore all incoming interrupts */
929 free_irq(SGI_XPC_ACTIVATE, NULL);
931 /* wait for the discovery thread to exit */
932 wait_for_completion(&xpc_discovery_exited);
934 /* wait for the heartbeat checker thread to exit */
935 wait_for_completion(&xpc_hb_checker_exited);
937 /* sleep for a 1/3 of a second or so */
938 (void)msleep_interruptible(300);
940 /* wait for all partitions to become inactive */
942 printmsg_time = jiffies + (XPC_DISENGAGE_PRINTMSG_INTERVAL * HZ);
943 xpc_disengage_request_timedout = 0;
946 active_part_count = 0;
948 for (partid = 1; partid < XP_MAX_PARTITIONS; partid++) {
949 part = &xpc_partitions[partid];
951 if (xpc_partition_disengaged(part) &&
952 part->act_state == XPC_P_INACTIVE) {
958 XPC_DEACTIVATE_PARTITION(part, reason);
960 if (part->disengage_request_timeout >
961 disengage_request_timeout) {
962 disengage_request_timeout =
963 part->disengage_request_timeout;
967 if (xpc_partition_engaged(-1UL)) {
968 if (time_after(jiffies, printmsg_time)) {
969 dev_info(xpc_part, "waiting for remote "
970 "partitions to disengage, timeout in "
972 (disengage_request_timeout - jiffies)
974 printmsg_time = jiffies +
975 (XPC_DISENGAGE_PRINTMSG_INTERVAL * HZ);
976 printed_waiting_msg = 1;
979 } else if (active_part_count > 0) {
980 if (printed_waiting_msg) {
981 dev_info(xpc_part, "waiting for local partition"
983 printed_waiting_msg = 0;
987 if (!xpc_disengage_request_timedout) {
988 dev_info(xpc_part, "all partitions have "
994 /* sleep for a 1/3 of a second or so */
995 (void)msleep_interruptible(300);
999 DBUG_ON(xpc_partition_engaged(-1UL));
1001 /* indicate to others that our reserved page is uninitialized */
1002 xpc_rsvd_page->vars_pa = 0;
1004 /* now it's time to eliminate our heartbeat */
1005 del_timer_sync(&xpc_hb_timer);
1006 DBUG_ON(xpc_vars->heartbeating_to_mask != 0);
1008 if (reason == xpcUnloading) {
1009 /* take ourselves off of the reboot_notifier_list */
1010 (void)unregister_reboot_notifier(&xpc_reboot_notifier);
1012 /* take ourselves off of the die_notifier list */
1013 (void)unregister_die_notifier(&xpc_die_notifier);
1016 /* close down protections for IPI operations */
1017 xpc_restrict_IPI_ops();
1019 /* clear the interface to XPC's functions */
1020 xpc_clear_interface();
1023 unregister_sysctl_table(xpc_sysctl);
1026 kfree(xpc_remote_copy_buffer_base);
1030 * This function is called when the system is being rebooted.
1033 xpc_system_reboot(struct notifier_block *nb, unsigned long event, void *unused)
1035 enum xpc_retval reason;
1039 reason = xpcSystemReboot;
1042 reason = xpcSystemHalt;
1045 reason = xpcSystemPoweroff;
1048 reason = xpcSystemGoingDown;
1051 xpc_do_exit(reason);
1056 * Notify other partitions to disengage from all references to our memory.
1059 xpc_die_disengage(void)
1061 struct xpc_partition *part;
1063 unsigned long engaged;
1064 long time, printmsg_time, disengage_request_timeout;
1066 /* keep xpc_hb_checker thread from doing anything (just in case) */
1069 xpc_vars->heartbeating_to_mask = 0; /* indicate we're deactivated */
1071 for (partid = 1; partid < XP_MAX_PARTITIONS; partid++) {
1072 part = &xpc_partitions[partid];
1074 if (!XPC_SUPPORTS_DISENGAGE_REQUEST(part->remote_vars_version)) {
1076 /* just in case it was left set by an earlier XPC */
1077 xpc_clear_partition_engaged(1UL << partid);
1081 if (xpc_partition_engaged(1UL << partid) ||
1082 part->act_state != XPC_P_INACTIVE) {
1083 xpc_request_partition_disengage(part);
1084 xpc_mark_partition_disengaged(part);
1085 xpc_IPI_send_disengage(part);
1090 printmsg_time = time +
1091 (XPC_DISENGAGE_PRINTMSG_INTERVAL * sn_rtc_cycles_per_second);
1092 disengage_request_timeout = time +
1093 (xpc_disengage_request_timelimit * sn_rtc_cycles_per_second);
1095 /* wait for all other partitions to disengage from us */
1098 engaged = xpc_partition_engaged(-1UL);
1100 dev_info(xpc_part, "all partitions have disengaged\n");
1105 if (time >= disengage_request_timeout) {
1106 for (partid = 1; partid < XP_MAX_PARTITIONS; partid++) {
1107 if (engaged & (1UL << partid)) {
1108 dev_info(xpc_part, "disengage from "
1109 "remote partition %d timed "
1116 if (time >= printmsg_time) {
1117 dev_info(xpc_part, "waiting for remote partitions to "
1118 "disengage, timeout in %ld seconds\n",
1119 (disengage_request_timeout - time) /
1120 sn_rtc_cycles_per_second);
1121 printmsg_time = time +
1122 (XPC_DISENGAGE_PRINTMSG_INTERVAL *
1123 sn_rtc_cycles_per_second);
1129 * This function is called when the system is being restarted or halted due
1130 * to some sort of system failure. If this is the case we need to notify the
1131 * other partitions to disengage from all references to our memory.
1132 * This function can also be called when our heartbeater could be offlined
1133 * for a time. In this case we need to notify other partitions to not worry
1134 * about the lack of a heartbeat.
1137 xpc_system_die(struct notifier_block *nb, unsigned long event, void *unused)
1140 case DIE_MACHINE_RESTART:
1141 case DIE_MACHINE_HALT:
1142 xpc_die_disengage();
1145 case DIE_KDEBUG_ENTER:
1146 /* Should lack of heartbeat be ignored by other partitions? */
1147 if (!xpc_kdebug_ignore) {
1151 case DIE_MCA_MONARCH_ENTER:
1152 case DIE_INIT_MONARCH_ENTER:
1153 xpc_vars->heartbeat++;
1154 xpc_vars->heartbeat_offline = 1;
1157 case DIE_KDEBUG_LEAVE:
1158 /* Is lack of heartbeat being ignored by other partitions? */
1159 if (!xpc_kdebug_ignore) {
1163 case DIE_MCA_MONARCH_LEAVE:
1164 case DIE_INIT_MONARCH_LEAVE:
1165 xpc_vars->heartbeat++;
1166 xpc_vars->heartbeat_offline = 0;
1178 struct xpc_partition *part;
1182 if (!ia64_platform_is("sn2")) {
1186 buf_size = max(XPC_RP_VARS_SIZE,
1187 XPC_RP_HEADER_SIZE + XP_NASID_MASK_BYTES);
1188 xpc_remote_copy_buffer = xpc_kmalloc_cacheline_aligned(buf_size,
1190 &xpc_remote_copy_buffer_base);
1191 if (xpc_remote_copy_buffer == NULL)
1194 snprintf(xpc_part->bus_id, BUS_ID_SIZE, "part");
1195 snprintf(xpc_chan->bus_id, BUS_ID_SIZE, "chan");
1197 xpc_sysctl = register_sysctl_table(xpc_sys_dir);
1200 * The first few fields of each entry of xpc_partitions[] need to
1201 * be initialized now so that calls to xpc_connect() and
1202 * xpc_disconnect() can be made prior to the activation of any remote
1203 * partition. NOTE THAT NONE OF THE OTHER FIELDS BELONGING TO THESE
1204 * ENTRIES ARE MEANINGFUL UNTIL AFTER AN ENTRY'S CORRESPONDING
1205 * PARTITION HAS BEEN ACTIVATED.
1207 for (partid = 1; partid < XP_MAX_PARTITIONS; partid++) {
1208 part = &xpc_partitions[partid];
1210 DBUG_ON((u64)part != L1_CACHE_ALIGN((u64)part));
1212 part->act_IRQ_rcvd = 0;
1213 spin_lock_init(&part->act_lock);
1214 part->act_state = XPC_P_INACTIVE;
1215 XPC_SET_REASON(part, 0, 0);
1217 init_timer(&part->disengage_request_timer);
1218 part->disengage_request_timer.function =
1219 xpc_timeout_partition_disengage_request;
1220 part->disengage_request_timer.data = (unsigned long)part;
1222 part->setup_state = XPC_P_UNSET;
1223 init_waitqueue_head(&part->teardown_wq);
1224 atomic_set(&part->references, 0);
1228 * Open up protections for IPI operations (and AMO operations on
1229 * Shub 1.1 systems).
1231 xpc_allow_IPI_ops();
1234 * Interrupts being processed will increment this atomic variable and
1235 * awaken the heartbeat thread which will process the interrupts.
1237 atomic_set(&xpc_act_IRQ_rcvd, 0);
1240 * This is safe to do before the xpc_hb_checker thread has started
1241 * because the handler releases a wait queue. If an interrupt is
1242 * received before the thread is waiting, it will not go to sleep,
1243 * but rather immediately process the interrupt.
1245 ret = request_irq(SGI_XPC_ACTIVATE, xpc_act_IRQ_handler, 0,
1248 dev_err(xpc_part, "can't register ACTIVATE IRQ handler, "
1249 "errno=%d\n", -ret);
1251 xpc_restrict_IPI_ops();
1254 unregister_sysctl_table(xpc_sysctl);
1257 kfree(xpc_remote_copy_buffer_base);
1262 * Fill the partition reserved page with the information needed by
1263 * other partitions to discover we are alive and establish initial
1266 xpc_rsvd_page = xpc_rsvd_page_init();
1267 if (xpc_rsvd_page == NULL) {
1268 dev_err(xpc_part, "could not setup our reserved page\n");
1270 free_irq(SGI_XPC_ACTIVATE, NULL);
1271 xpc_restrict_IPI_ops();
1274 unregister_sysctl_table(xpc_sysctl);
1277 kfree(xpc_remote_copy_buffer_base);
1281 /* add ourselves to the reboot_notifier_list */
1282 ret = register_reboot_notifier(&xpc_reboot_notifier);
1284 dev_warn(xpc_part, "can't register reboot notifier\n");
1287 /* add ourselves to the die_notifier list */
1288 ret = register_die_notifier(&xpc_die_notifier);
1290 dev_warn(xpc_part, "can't register die notifier\n");
1293 init_timer(&xpc_hb_timer);
1294 xpc_hb_timer.function = xpc_hb_beater;
1297 * The real work-horse behind xpc. This processes incoming
1298 * interrupts and monitors remote heartbeats.
1300 pid = kernel_thread(xpc_hb_checker, NULL, 0);
1302 dev_err(xpc_part, "failed while forking hb check thread\n");
1304 /* indicate to others that our reserved page is uninitialized */
1305 xpc_rsvd_page->vars_pa = 0;
1307 /* take ourselves off of the reboot_notifier_list */
1308 (void)unregister_reboot_notifier(&xpc_reboot_notifier);
1310 /* take ourselves off of the die_notifier list */
1311 (void)unregister_die_notifier(&xpc_die_notifier);
1313 del_timer_sync(&xpc_hb_timer);
1314 free_irq(SGI_XPC_ACTIVATE, NULL);
1315 xpc_restrict_IPI_ops();
1318 unregister_sysctl_table(xpc_sysctl);
1321 kfree(xpc_remote_copy_buffer_base);
1326 * Startup a thread that will attempt to discover other partitions to
1327 * activate based on info provided by SAL. This new thread is short
1328 * lived and will exit once discovery is complete.
1330 pid = kernel_thread(xpc_initiate_discovery, NULL, 0);
1332 dev_err(xpc_part, "failed while forking discovery thread\n");
1334 /* mark this new thread as a non-starter */
1335 complete(&xpc_discovery_exited);
1337 xpc_do_exit(xpcUnloading);
1341 /* set the interface to point at XPC's functions */
1342 xpc_set_interface(xpc_initiate_connect, xpc_initiate_disconnect,
1343 xpc_initiate_allocate, xpc_initiate_send,
1344 xpc_initiate_send_notify, xpc_initiate_received,
1345 xpc_initiate_partid_to_nasids);
1350 module_init(xpc_init);
1355 xpc_do_exit(xpcUnloading);
1358 module_exit(xpc_exit);
1360 MODULE_AUTHOR("Silicon Graphics, Inc.");
1361 MODULE_DESCRIPTION("Cross Partition Communication (XPC) support");
1362 MODULE_LICENSE("GPL");
1364 module_param(xpc_hb_interval, int, 0);
1365 MODULE_PARM_DESC(xpc_hb_interval, "Number of seconds between "
1366 "heartbeat increments.");
1368 module_param(xpc_hb_check_interval, int, 0);
1369 MODULE_PARM_DESC(xpc_hb_check_interval, "Number of seconds between "
1370 "heartbeat checks.");
1372 module_param(xpc_disengage_request_timelimit, int, 0);
1373 MODULE_PARM_DESC(xpc_disengage_request_timelimit, "Number of seconds to wait "
1374 "for disengage request to complete.");
1376 module_param(xpc_kdebug_ignore, int, 0);
1377 MODULE_PARM_DESC(xpc_kdebug_ignore, "Should lack of heartbeat be ignored by "
1378 "other partitions when dropping into kdebug.");