sgi-xp: isolate xpc_vars_part structure to sn2 only
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / misc / sgi-xp / xpc_main.c
blobbe3a485393078f97bccd57f60d5a65065ce0ff4f
1 /*
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
4 * for more details.
6 * Copyright (c) 2004-2008 Silicon Graphics, Inc. All Rights Reserved.
7 */
9 /*
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.
26 * Caveats:
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
39 * write operations.
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/cache.h>
50 #include <linux/interrupt.h>
51 #include <linux/delay.h>
52 #include <linux/reboot.h>
53 #include <linux/completion.h>
54 #include <linux/kdebug.h>
55 #include <linux/kthread.h>
56 #include <linux/uaccess.h>
57 #include <asm/sn/intr.h>
58 #include <asm/sn/sn_sal.h>
59 #include "xpc.h"
61 /* define two XPC debug device structures to be used with dev_dbg() et al */
63 struct device_driver xpc_dbg_name = {
64 .name = "xpc"
67 struct device xpc_part_dbg_subname = {
68 .bus_id = {0}, /* set to "part" at xpc_init() time */
69 .driver = &xpc_dbg_name
72 struct device xpc_chan_dbg_subname = {
73 .bus_id = {0}, /* set to "chan" at xpc_init() time */
74 .driver = &xpc_dbg_name
77 struct device *xpc_part = &xpc_part_dbg_subname;
78 struct device *xpc_chan = &xpc_chan_dbg_subname;
80 static int xpc_kdebug_ignore;
82 /* systune related variables for /proc/sys directories */
84 static int xpc_hb_interval = XPC_HB_DEFAULT_INTERVAL;
85 static int xpc_hb_min_interval = 1;
86 static int xpc_hb_max_interval = 10;
88 static int xpc_hb_check_interval = XPC_HB_CHECK_DEFAULT_INTERVAL;
89 static int xpc_hb_check_min_interval = 10;
90 static int xpc_hb_check_max_interval = 120;
92 int xpc_disengage_request_timelimit = XPC_DISENGAGE_REQUEST_DEFAULT_TIMELIMIT;
93 static int xpc_disengage_request_min_timelimit; /* = 0 */
94 static int xpc_disengage_request_max_timelimit = 120;
96 static ctl_table xpc_sys_xpc_hb_dir[] = {
98 .ctl_name = CTL_UNNUMBERED,
99 .procname = "hb_interval",
100 .data = &xpc_hb_interval,
101 .maxlen = sizeof(int),
102 .mode = 0644,
103 .proc_handler = &proc_dointvec_minmax,
104 .strategy = &sysctl_intvec,
105 .extra1 = &xpc_hb_min_interval,
106 .extra2 = &xpc_hb_max_interval},
108 .ctl_name = CTL_UNNUMBERED,
109 .procname = "hb_check_interval",
110 .data = &xpc_hb_check_interval,
111 .maxlen = sizeof(int),
112 .mode = 0644,
113 .proc_handler = &proc_dointvec_minmax,
114 .strategy = &sysctl_intvec,
115 .extra1 = &xpc_hb_check_min_interval,
116 .extra2 = &xpc_hb_check_max_interval},
119 static ctl_table xpc_sys_xpc_dir[] = {
121 .ctl_name = CTL_UNNUMBERED,
122 .procname = "hb",
123 .mode = 0555,
124 .child = xpc_sys_xpc_hb_dir},
126 .ctl_name = CTL_UNNUMBERED,
127 .procname = "disengage_request_timelimit",
128 .data = &xpc_disengage_request_timelimit,
129 .maxlen = sizeof(int),
130 .mode = 0644,
131 .proc_handler = &proc_dointvec_minmax,
132 .strategy = &sysctl_intvec,
133 .extra1 = &xpc_disengage_request_min_timelimit,
134 .extra2 = &xpc_disengage_request_max_timelimit},
137 static ctl_table xpc_sys_dir[] = {
139 .ctl_name = CTL_UNNUMBERED,
140 .procname = "xpc",
141 .mode = 0555,
142 .child = xpc_sys_xpc_dir},
145 static struct ctl_table_header *xpc_sysctl;
147 /* non-zero if any remote partition disengage request was timed out */
148 int xpc_disengage_request_timedout;
150 /* #of IRQs received */
151 static atomic_t xpc_act_IRQ_rcvd;
153 /* IRQ handler notifies this wait queue on receipt of an IRQ */
154 static DECLARE_WAIT_QUEUE_HEAD(xpc_act_IRQ_wq);
156 static unsigned long xpc_hb_check_timeout;
158 /* notification that the xpc_hb_checker thread has exited */
159 static DECLARE_COMPLETION(xpc_hb_checker_exited);
161 /* notification that the xpc_discovery thread has exited */
162 static DECLARE_COMPLETION(xpc_discovery_exited);
164 static struct timer_list xpc_hb_timer;
166 static void xpc_kthread_waitmsgs(struct xpc_partition *, struct xpc_channel *);
168 static int xpc_system_reboot(struct notifier_block *, unsigned long, void *);
169 static struct notifier_block xpc_reboot_notifier = {
170 .notifier_call = xpc_system_reboot,
173 static int xpc_system_die(struct notifier_block *, unsigned long, void *);
174 static struct notifier_block xpc_die_notifier = {
175 .notifier_call = xpc_system_die,
178 enum xp_retval (*xpc_rsvd_page_init) (struct xpc_rsvd_page *rp);
179 enum xp_retval (*xpc_make_first_contact) (struct xpc_partition *part);
180 u64 (*xpc_get_IPI_flags) (struct xpc_partition *part);
181 struct xpc_msg *(*xpc_get_deliverable_msg) (struct xpc_channel *ch);
182 enum xp_retval (*xpc_setup_infrastructure) (struct xpc_partition *part);
183 void (*xpc_teardown_infrastructure) (struct xpc_partition *part);
187 * Timer function to enforce the timelimit on the partition disengage request.
189 static void
190 xpc_timeout_partition_disengage_request(unsigned long data)
192 struct xpc_partition *part = (struct xpc_partition *)data;
194 DBUG_ON(time_before(jiffies, part->disengage_request_timeout));
196 (void)xpc_partition_disengaged(part);
198 DBUG_ON(part->disengage_request_timeout != 0);
199 DBUG_ON(xpc_partition_engaged(1UL << XPC_PARTID(part)) != 0);
203 * Notify the heartbeat check thread that an IRQ has been received.
205 static irqreturn_t
206 xpc_act_IRQ_handler(int irq, void *dev_id)
208 atomic_inc(&xpc_act_IRQ_rcvd);
209 wake_up_interruptible(&xpc_act_IRQ_wq);
210 return IRQ_HANDLED;
214 * Timer to produce the heartbeat. The timer structures function is
215 * already set when this is initially called. A tunable is used to
216 * specify when the next timeout should occur.
218 static void
219 xpc_hb_beater(unsigned long dummy)
221 xpc_vars->heartbeat++;
223 if (time_after_eq(jiffies, xpc_hb_check_timeout))
224 wake_up_interruptible(&xpc_act_IRQ_wq);
226 xpc_hb_timer.expires = jiffies + (xpc_hb_interval * HZ);
227 add_timer(&xpc_hb_timer);
231 * This thread is responsible for nearly all of the partition
232 * activation/deactivation.
234 static int
235 xpc_hb_checker(void *ignore)
237 int last_IRQ_count = 0;
238 int new_IRQ_count;
239 int force_IRQ = 0;
241 /* this thread was marked active by xpc_hb_init() */
243 set_cpus_allowed_ptr(current, &cpumask_of_cpu(XPC_HB_CHECK_CPU));
245 /* set our heartbeating to other partitions into motion */
246 xpc_hb_check_timeout = jiffies + (xpc_hb_check_interval * HZ);
247 xpc_hb_beater(0);
249 while (!xpc_exiting) {
251 dev_dbg(xpc_part, "woke up with %d ticks rem; %d IRQs have "
252 "been received\n",
253 (int)(xpc_hb_check_timeout - jiffies),
254 atomic_read(&xpc_act_IRQ_rcvd) - last_IRQ_count);
256 /* checking of remote heartbeats is skewed by IRQ handling */
257 if (time_after_eq(jiffies, xpc_hb_check_timeout)) {
258 dev_dbg(xpc_part, "checking remote heartbeats\n");
259 xpc_check_remote_hb();
262 * We need to periodically recheck to ensure no
263 * IPI/AMO pairs have been missed. That check
264 * must always reset xpc_hb_check_timeout.
266 force_IRQ = 1;
269 /* check for outstanding IRQs */
270 new_IRQ_count = atomic_read(&xpc_act_IRQ_rcvd);
271 if (last_IRQ_count < new_IRQ_count || force_IRQ != 0) {
272 force_IRQ = 0;
274 dev_dbg(xpc_part, "found an IRQ to process; will be "
275 "resetting xpc_hb_check_timeout\n");
277 last_IRQ_count += xpc_identify_act_IRQ_sender();
278 if (last_IRQ_count < new_IRQ_count) {
279 /* retry once to help avoid missing AMO */
280 (void)xpc_identify_act_IRQ_sender();
282 last_IRQ_count = new_IRQ_count;
284 xpc_hb_check_timeout = jiffies +
285 (xpc_hb_check_interval * HZ);
288 /* wait for IRQ or timeout */
289 (void)wait_event_interruptible(xpc_act_IRQ_wq,
290 (last_IRQ_count <
291 atomic_read(&xpc_act_IRQ_rcvd)
292 || time_after_eq(jiffies,
293 xpc_hb_check_timeout) ||
294 xpc_exiting));
297 dev_dbg(xpc_part, "heartbeat checker is exiting\n");
299 /* mark this thread as having exited */
300 complete(&xpc_hb_checker_exited);
301 return 0;
305 * This thread will attempt to discover other partitions to activate
306 * based on info provided by SAL. This new thread is short lived and
307 * will exit once discovery is complete.
309 static int
310 xpc_initiate_discovery(void *ignore)
312 xpc_discovery();
314 dev_dbg(xpc_part, "discovery thread is exiting\n");
316 /* mark this thread as having exited */
317 complete(&xpc_discovery_exited);
318 return 0;
322 * The first kthread assigned to a newly activated partition is the one
323 * created by XPC HB with which it calls xpc_activating(). XPC hangs on to
324 * that kthread until the partition is brought down, at which time that kthread
325 * returns back to XPC HB. (The return of that kthread will signify to XPC HB
326 * that XPC has dismantled all communication infrastructure for the associated
327 * partition.) This kthread becomes the channel manager for that partition.
329 * Each active partition has a channel manager, who, besides connecting and
330 * disconnecting channels, will ensure that each of the partition's connected
331 * channels has the required number of assigned kthreads to get the work done.
333 static void
334 xpc_channel_mgr(struct xpc_partition *part)
336 while (part->act_state != XPC_P_DEACTIVATING ||
337 atomic_read(&part->nchannels_active) > 0 ||
338 !xpc_partition_disengaged(part)) {
340 xpc_process_channel_activity(part);
343 * Wait until we've been requested to activate kthreads or
344 * all of the channel's message queues have been torn down or
345 * a signal is pending.
347 * The channel_mgr_requests is set to 1 after being awakened,
348 * This is done to prevent the channel mgr from making one pass
349 * through the loop for each request, since he will
350 * be servicing all the requests in one pass. The reason it's
351 * set to 1 instead of 0 is so that other kthreads will know
352 * that the channel mgr is running and won't bother trying to
353 * wake him up.
355 atomic_dec(&part->channel_mgr_requests);
356 (void)wait_event_interruptible(part->channel_mgr_wq,
357 (atomic_read(&part->channel_mgr_requests) > 0 ||
358 part->local_IPI_amo != 0 ||
359 (part->act_state == XPC_P_DEACTIVATING &&
360 atomic_read(&part->nchannels_active) == 0 &&
361 xpc_partition_disengaged(part))));
362 atomic_set(&part->channel_mgr_requests, 1);
367 * When XPC HB determines that a partition has come up, it will create a new
368 * kthread and that kthread will call this function to attempt to set up the
369 * basic infrastructure used for Cross Partition Communication with the newly
370 * upped partition.
372 * The kthread that was created by XPC HB and which setup the XPC
373 * infrastructure will remain assigned to the partition becoming the channel
374 * manager for that partition until the partition is deactivating, at which
375 * time the kthread will teardown the XPC infrastructure and then exit.
377 static int
378 xpc_activating(void *__partid)
380 short partid = (u64)__partid;
381 struct xpc_partition *part = &xpc_partitions[partid];
382 unsigned long irq_flags;
384 DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
386 spin_lock_irqsave(&part->act_lock, irq_flags);
388 if (part->act_state == XPC_P_DEACTIVATING) {
389 part->act_state = XPC_P_INACTIVE;
390 spin_unlock_irqrestore(&part->act_lock, irq_flags);
391 part->remote_rp_pa = 0;
392 return 0;
395 /* indicate the thread is activating */
396 DBUG_ON(part->act_state != XPC_P_ACTIVATION_REQ);
397 part->act_state = XPC_P_ACTIVATING;
399 XPC_SET_REASON(part, 0, 0);
400 spin_unlock_irqrestore(&part->act_lock, irq_flags);
402 dev_dbg(xpc_part, "activating partition %d\n", partid);
405 * Register the remote partition's AMOs with SAL so it can handle
406 * and cleanup errors within that address range should the remote
407 * partition go down. We don't unregister this range because it is
408 * difficult to tell when outstanding writes to the remote partition
409 * are finished and thus when it is safe to unregister. This should
410 * not result in wasted space in the SAL xp_addr_region table because
411 * we should get the same page for remote_amos_page_pa after module
412 * reloads and system reboots.
414 if (sn_register_xp_addr_region(part->remote_amos_page_pa,
415 PAGE_SIZE, 1) < 0) {
416 dev_warn(xpc_part, "xpc_activating(%d) failed to register "
417 "xp_addr region\n", partid);
419 spin_lock_irqsave(&part->act_lock, irq_flags);
420 part->act_state = XPC_P_INACTIVE;
421 XPC_SET_REASON(part, xpPhysAddrRegFailed, __LINE__);
422 spin_unlock_irqrestore(&part->act_lock, irq_flags);
423 part->remote_rp_pa = 0;
424 return 0;
427 xpc_allow_hb(partid, xpc_vars);
428 xpc_IPI_send_activated(part);
430 if (xpc_setup_infrastructure(part) == xpSuccess) {
431 (void)xpc_part_ref(part); /* this will always succeed */
433 if (xpc_make_first_contact(part) == xpSuccess) {
434 xpc_mark_partition_active(part);
435 xpc_channel_mgr(part);
436 /* won't return until partition is deactivating */
439 xpc_part_deref(part);
440 xpc_teardown_infrastructure(part);
443 xpc_disallow_hb(partid, xpc_vars);
444 xpc_mark_partition_inactive(part);
446 if (part->reason == xpReactivating) {
447 /* interrupting ourselves results in activating partition */
448 xpc_IPI_send_reactivate(part);
451 return 0;
454 void
455 xpc_activate_partition(struct xpc_partition *part)
457 short partid = XPC_PARTID(part);
458 unsigned long irq_flags;
459 struct task_struct *kthread;
461 spin_lock_irqsave(&part->act_lock, irq_flags);
463 DBUG_ON(part->act_state != XPC_P_INACTIVE);
465 part->act_state = XPC_P_ACTIVATION_REQ;
466 XPC_SET_REASON(part, xpCloneKThread, __LINE__);
468 spin_unlock_irqrestore(&part->act_lock, irq_flags);
470 kthread = kthread_run(xpc_activating, (void *)((u64)partid), "xpc%02d",
471 partid);
472 if (IS_ERR(kthread)) {
473 spin_lock_irqsave(&part->act_lock, irq_flags);
474 part->act_state = XPC_P_INACTIVE;
475 XPC_SET_REASON(part, xpCloneKThreadFailed, __LINE__);
476 spin_unlock_irqrestore(&part->act_lock, irq_flags);
481 * Handle the receipt of a SGI_XPC_NOTIFY IRQ by seeing whether the specified
482 * partition actually sent it. Since SGI_XPC_NOTIFY IRQs may be shared by more
483 * than one partition, we use an AMO_t structure per partition to indicate
484 * whether a partition has sent an IPI or not. If it has, then wake up the
485 * associated kthread to handle it.
487 * All SGI_XPC_NOTIFY IRQs received by XPC are the result of IPIs sent by XPC
488 * running on other partitions.
490 * Noteworthy Arguments:
492 * irq - Interrupt ReQuest number. NOT USED.
494 * dev_id - partid of IPI's potential sender.
496 irqreturn_t
497 xpc_notify_IRQ_handler(int irq, void *dev_id)
499 short partid = (short)(u64)dev_id;
500 struct xpc_partition *part = &xpc_partitions[partid];
502 DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
504 if (xpc_part_ref(part)) {
505 xpc_check_for_channel_activity(part);
507 xpc_part_deref(part);
509 return IRQ_HANDLED;
513 * Check to see if xpc_notify_IRQ_handler() dropped any IPIs on the floor
514 * because the write to their associated IPI amo completed after the IRQ/IPI
515 * was received.
517 void
518 xpc_dropped_IPI_check(struct xpc_partition *part)
520 if (xpc_part_ref(part)) {
521 xpc_check_for_channel_activity(part);
523 part->dropped_IPI_timer.expires = jiffies +
524 XPC_P_DROPPED_IPI_WAIT_INTERVAL;
525 add_timer(&part->dropped_IPI_timer);
526 xpc_part_deref(part);
530 void
531 xpc_activate_kthreads(struct xpc_channel *ch, int needed)
533 int idle = atomic_read(&ch->kthreads_idle);
534 int assigned = atomic_read(&ch->kthreads_assigned);
535 int wakeup;
537 DBUG_ON(needed <= 0);
539 if (idle > 0) {
540 wakeup = (needed > idle) ? idle : needed;
541 needed -= wakeup;
543 dev_dbg(xpc_chan, "wakeup %d idle kthreads, partid=%d, "
544 "channel=%d\n", wakeup, ch->partid, ch->number);
546 /* only wakeup the requested number of kthreads */
547 wake_up_nr(&ch->idle_wq, wakeup);
550 if (needed <= 0)
551 return;
553 if (needed + assigned > ch->kthreads_assigned_limit) {
554 needed = ch->kthreads_assigned_limit - assigned;
555 if (needed <= 0)
556 return;
559 dev_dbg(xpc_chan, "create %d new kthreads, partid=%d, channel=%d\n",
560 needed, ch->partid, ch->number);
562 xpc_create_kthreads(ch, needed, 0);
566 * This function is where XPC's kthreads wait for messages to deliver.
568 static void
569 xpc_kthread_waitmsgs(struct xpc_partition *part, struct xpc_channel *ch)
571 do {
572 /* deliver messages to their intended recipients */
574 while (ch->w_local_GP.get < ch->w_remote_GP.put &&
575 !(ch->flags & XPC_C_DISCONNECTING)) {
576 xpc_deliver_msg(ch);
579 if (atomic_inc_return(&ch->kthreads_idle) >
580 ch->kthreads_idle_limit) {
581 /* too many idle kthreads on this channel */
582 atomic_dec(&ch->kthreads_idle);
583 break;
586 dev_dbg(xpc_chan, "idle kthread calling "
587 "wait_event_interruptible_exclusive()\n");
589 (void)wait_event_interruptible_exclusive(ch->idle_wq,
590 (ch->w_local_GP.get < ch->w_remote_GP.put ||
591 (ch->flags & XPC_C_DISCONNECTING)));
593 atomic_dec(&ch->kthreads_idle);
595 } while (!(ch->flags & XPC_C_DISCONNECTING));
598 static int
599 xpc_kthread_start(void *args)
601 short partid = XPC_UNPACK_ARG1(args);
602 u16 ch_number = XPC_UNPACK_ARG2(args);
603 struct xpc_partition *part = &xpc_partitions[partid];
604 struct xpc_channel *ch;
605 int n_needed;
606 unsigned long irq_flags;
608 dev_dbg(xpc_chan, "kthread starting, partid=%d, channel=%d\n",
609 partid, ch_number);
611 ch = &part->channels[ch_number];
613 if (!(ch->flags & XPC_C_DISCONNECTING)) {
615 /* let registerer know that connection has been established */
617 spin_lock_irqsave(&ch->lock, irq_flags);
618 if (!(ch->flags & XPC_C_CONNECTEDCALLOUT)) {
619 ch->flags |= XPC_C_CONNECTEDCALLOUT;
620 spin_unlock_irqrestore(&ch->lock, irq_flags);
622 xpc_connected_callout(ch);
624 spin_lock_irqsave(&ch->lock, irq_flags);
625 ch->flags |= XPC_C_CONNECTEDCALLOUT_MADE;
626 spin_unlock_irqrestore(&ch->lock, irq_flags);
629 * It is possible that while the callout was being
630 * made that the remote partition sent some messages.
631 * If that is the case, we may need to activate
632 * additional kthreads to help deliver them. We only
633 * need one less than total #of messages to deliver.
635 n_needed = ch->w_remote_GP.put - ch->w_local_GP.get - 1;
636 if (n_needed > 0 && !(ch->flags & XPC_C_DISCONNECTING))
637 xpc_activate_kthreads(ch, n_needed);
639 } else {
640 spin_unlock_irqrestore(&ch->lock, irq_flags);
643 xpc_kthread_waitmsgs(part, ch);
646 /* let registerer know that connection is disconnecting */
648 spin_lock_irqsave(&ch->lock, irq_flags);
649 if ((ch->flags & XPC_C_CONNECTEDCALLOUT_MADE) &&
650 !(ch->flags & XPC_C_DISCONNECTINGCALLOUT)) {
651 ch->flags |= XPC_C_DISCONNECTINGCALLOUT;
652 spin_unlock_irqrestore(&ch->lock, irq_flags);
654 xpc_disconnect_callout(ch, xpDisconnecting);
656 spin_lock_irqsave(&ch->lock, irq_flags);
657 ch->flags |= XPC_C_DISCONNECTINGCALLOUT_MADE;
659 spin_unlock_irqrestore(&ch->lock, irq_flags);
661 if (atomic_dec_return(&ch->kthreads_assigned) == 0) {
662 if (atomic_dec_return(&part->nchannels_engaged) == 0) {
663 xpc_mark_partition_disengaged(part);
664 xpc_IPI_send_disengage(part);
668 xpc_msgqueue_deref(ch);
670 dev_dbg(xpc_chan, "kthread exiting, partid=%d, channel=%d\n",
671 partid, ch_number);
673 xpc_part_deref(part);
674 return 0;
678 * For each partition that XPC has established communications with, there is
679 * a minimum of one kernel thread assigned to perform any operation that
680 * may potentially sleep or block (basically the callouts to the asynchronous
681 * functions registered via xpc_connect()).
683 * Additional kthreads are created and destroyed by XPC as the workload
684 * demands.
686 * A kthread is assigned to one of the active channels that exists for a given
687 * partition.
689 void
690 xpc_create_kthreads(struct xpc_channel *ch, int needed,
691 int ignore_disconnecting)
693 unsigned long irq_flags;
694 u64 args = XPC_PACK_ARGS(ch->partid, ch->number);
695 struct xpc_partition *part = &xpc_partitions[ch->partid];
696 struct task_struct *kthread;
698 while (needed-- > 0) {
701 * The following is done on behalf of the newly created
702 * kthread. That kthread is responsible for doing the
703 * counterpart to the following before it exits.
705 if (ignore_disconnecting) {
706 if (!atomic_inc_not_zero(&ch->kthreads_assigned)) {
707 /* kthreads assigned had gone to zero */
708 BUG_ON(!(ch->flags &
709 XPC_C_DISCONNECTINGCALLOUT_MADE));
710 break;
713 } else if (ch->flags & XPC_C_DISCONNECTING) {
714 break;
716 } else if (atomic_inc_return(&ch->kthreads_assigned) == 1) {
717 if (atomic_inc_return(&part->nchannels_engaged) == 1)
718 xpc_mark_partition_engaged(part);
720 (void)xpc_part_ref(part);
721 xpc_msgqueue_ref(ch);
723 kthread = kthread_run(xpc_kthread_start, (void *)args,
724 "xpc%02dc%d", ch->partid, ch->number);
725 if (IS_ERR(kthread)) {
726 /* the fork failed */
729 * NOTE: if (ignore_disconnecting &&
730 * !(ch->flags & XPC_C_DISCONNECTINGCALLOUT)) is true,
731 * then we'll deadlock if all other kthreads assigned
732 * to this channel are blocked in the channel's
733 * registerer, because the only thing that will unblock
734 * them is the xpDisconnecting callout that this
735 * failed kthread_run() would have made.
738 if (atomic_dec_return(&ch->kthreads_assigned) == 0 &&
739 atomic_dec_return(&part->nchannels_engaged) == 0) {
740 xpc_mark_partition_disengaged(part);
741 xpc_IPI_send_disengage(part);
743 xpc_msgqueue_deref(ch);
744 xpc_part_deref(part);
746 if (atomic_read(&ch->kthreads_assigned) <
747 ch->kthreads_idle_limit) {
749 * Flag this as an error only if we have an
750 * insufficient #of kthreads for the channel
751 * to function.
753 spin_lock_irqsave(&ch->lock, irq_flags);
754 XPC_DISCONNECT_CHANNEL(ch, xpLackOfResources,
755 &irq_flags);
756 spin_unlock_irqrestore(&ch->lock, irq_flags);
758 break;
763 void
764 xpc_disconnect_wait(int ch_number)
766 unsigned long irq_flags;
767 short partid;
768 struct xpc_partition *part;
769 struct xpc_channel *ch;
770 int wakeup_channel_mgr;
772 /* now wait for all callouts to the caller's function to cease */
773 for (partid = 0; partid < xp_max_npartitions; partid++) {
774 part = &xpc_partitions[partid];
776 if (!xpc_part_ref(part))
777 continue;
779 ch = &part->channels[ch_number];
781 if (!(ch->flags & XPC_C_WDISCONNECT)) {
782 xpc_part_deref(part);
783 continue;
786 wait_for_completion(&ch->wdisconnect_wait);
788 spin_lock_irqsave(&ch->lock, irq_flags);
789 DBUG_ON(!(ch->flags & XPC_C_DISCONNECTED));
790 wakeup_channel_mgr = 0;
792 if (ch->delayed_IPI_flags) {
793 if (part->act_state != XPC_P_DEACTIVATING) {
794 spin_lock(&part->IPI_lock);
795 XPC_SET_IPI_FLAGS(part->local_IPI_amo,
796 ch->number,
797 ch->delayed_IPI_flags);
798 spin_unlock(&part->IPI_lock);
799 wakeup_channel_mgr = 1;
801 ch->delayed_IPI_flags = 0;
804 ch->flags &= ~XPC_C_WDISCONNECT;
805 spin_unlock_irqrestore(&ch->lock, irq_flags);
807 if (wakeup_channel_mgr)
808 xpc_wakeup_channel_mgr(part);
810 xpc_part_deref(part);
814 static void
815 xpc_do_exit(enum xp_retval reason)
817 short partid;
818 int active_part_count, printed_waiting_msg = 0;
819 struct xpc_partition *part;
820 unsigned long printmsg_time, disengage_request_timeout = 0;
822 /* a 'rmmod XPC' and a 'reboot' cannot both end up here together */
823 DBUG_ON(xpc_exiting == 1);
826 * Let the heartbeat checker thread and the discovery thread
827 * (if one is running) know that they should exit. Also wake up
828 * the heartbeat checker thread in case it's sleeping.
830 xpc_exiting = 1;
831 wake_up_interruptible(&xpc_act_IRQ_wq);
833 /* ignore all incoming interrupts */
834 free_irq(SGI_XPC_ACTIVATE, NULL);
836 /* wait for the discovery thread to exit */
837 wait_for_completion(&xpc_discovery_exited);
839 /* wait for the heartbeat checker thread to exit */
840 wait_for_completion(&xpc_hb_checker_exited);
842 /* sleep for a 1/3 of a second or so */
843 (void)msleep_interruptible(300);
845 /* wait for all partitions to become inactive */
847 printmsg_time = jiffies + (XPC_DISENGAGE_PRINTMSG_INTERVAL * HZ);
848 xpc_disengage_request_timedout = 0;
850 do {
851 active_part_count = 0;
853 for (partid = 0; partid < xp_max_npartitions; partid++) {
854 part = &xpc_partitions[partid];
856 if (xpc_partition_disengaged(part) &&
857 part->act_state == XPC_P_INACTIVE) {
858 continue;
861 active_part_count++;
863 XPC_DEACTIVATE_PARTITION(part, reason);
865 if (part->disengage_request_timeout >
866 disengage_request_timeout) {
867 disengage_request_timeout =
868 part->disengage_request_timeout;
872 if (xpc_partition_engaged(-1UL)) {
873 if (time_after(jiffies, printmsg_time)) {
874 dev_info(xpc_part, "waiting for remote "
875 "partitions to disengage, timeout in "
876 "%ld seconds\n",
877 (disengage_request_timeout - jiffies)
878 / HZ);
879 printmsg_time = jiffies +
880 (XPC_DISENGAGE_PRINTMSG_INTERVAL * HZ);
881 printed_waiting_msg = 1;
884 } else if (active_part_count > 0) {
885 if (printed_waiting_msg) {
886 dev_info(xpc_part, "waiting for local partition"
887 " to disengage\n");
888 printed_waiting_msg = 0;
891 } else {
892 if (!xpc_disengage_request_timedout) {
893 dev_info(xpc_part, "all partitions have "
894 "disengaged\n");
896 break;
899 /* sleep for a 1/3 of a second or so */
900 (void)msleep_interruptible(300);
902 } while (1);
904 DBUG_ON(xpc_partition_engaged(-1UL));
906 /* indicate to others that our reserved page is uninitialized */
907 xpc_rsvd_page->stamp = ZERO_STAMP;
909 /* now it's time to eliminate our heartbeat */
910 del_timer_sync(&xpc_hb_timer);
911 DBUG_ON(xpc_vars->heartbeating_to_mask != 0);
913 if (reason == xpUnloading) {
914 (void)unregister_die_notifier(&xpc_die_notifier);
915 (void)unregister_reboot_notifier(&xpc_reboot_notifier);
918 /* close down protections for IPI operations */
919 xpc_restrict_IPI_ops();
921 /* clear the interface to XPC's functions */
922 xpc_clear_interface();
924 if (xpc_sysctl)
925 unregister_sysctl_table(xpc_sysctl);
927 kfree(xpc_partitions);
928 kfree(xpc_remote_copy_buffer_base);
932 * This function is called when the system is being rebooted.
934 static int
935 xpc_system_reboot(struct notifier_block *nb, unsigned long event, void *unused)
937 enum xp_retval reason;
939 switch (event) {
940 case SYS_RESTART:
941 reason = xpSystemReboot;
942 break;
943 case SYS_HALT:
944 reason = xpSystemHalt;
945 break;
946 case SYS_POWER_OFF:
947 reason = xpSystemPoweroff;
948 break;
949 default:
950 reason = xpSystemGoingDown;
953 xpc_do_exit(reason);
954 return NOTIFY_DONE;
958 * Notify other partitions to disengage from all references to our memory.
960 static void
961 xpc_die_disengage(void)
963 struct xpc_partition *part;
964 short partid;
965 unsigned long engaged;
966 long time, printmsg_time, disengage_request_timeout;
968 /* keep xpc_hb_checker thread from doing anything (just in case) */
969 xpc_exiting = 1;
971 xpc_vars->heartbeating_to_mask = 0; /* indicate we're deactivated */
973 for (partid = 0; partid < xp_max_npartitions; partid++) {
974 part = &xpc_partitions[partid];
976 if (!XPC_SUPPORTS_DISENGAGE_REQUEST(part->
977 remote_vars_version)) {
979 /* just in case it was left set by an earlier XPC */
980 xpc_clear_partition_engaged(1UL << partid);
981 continue;
984 if (xpc_partition_engaged(1UL << partid) ||
985 part->act_state != XPC_P_INACTIVE) {
986 xpc_request_partition_disengage(part);
987 xpc_mark_partition_disengaged(part);
988 xpc_IPI_send_disengage(part);
992 time = rtc_time();
993 printmsg_time = time +
994 (XPC_DISENGAGE_PRINTMSG_INTERVAL * sn_rtc_cycles_per_second);
995 disengage_request_timeout = time +
996 (xpc_disengage_request_timelimit * sn_rtc_cycles_per_second);
998 /* wait for all other partitions to disengage from us */
1000 while (1) {
1001 engaged = xpc_partition_engaged(-1UL);
1002 if (!engaged) {
1003 dev_info(xpc_part, "all partitions have disengaged\n");
1004 break;
1007 time = rtc_time();
1008 if (time >= disengage_request_timeout) {
1009 for (partid = 0; partid < xp_max_npartitions;
1010 partid++) {
1011 if (engaged & (1UL << partid)) {
1012 dev_info(xpc_part, "disengage from "
1013 "remote partition %d timed "
1014 "out\n", partid);
1017 break;
1020 if (time >= printmsg_time) {
1021 dev_info(xpc_part, "waiting for remote partitions to "
1022 "disengage, timeout in %ld seconds\n",
1023 (disengage_request_timeout - time) /
1024 sn_rtc_cycles_per_second);
1025 printmsg_time = time +
1026 (XPC_DISENGAGE_PRINTMSG_INTERVAL *
1027 sn_rtc_cycles_per_second);
1033 * This function is called when the system is being restarted or halted due
1034 * to some sort of system failure. If this is the case we need to notify the
1035 * other partitions to disengage from all references to our memory.
1036 * This function can also be called when our heartbeater could be offlined
1037 * for a time. In this case we need to notify other partitions to not worry
1038 * about the lack of a heartbeat.
1040 static int
1041 xpc_system_die(struct notifier_block *nb, unsigned long event, void *unused)
1043 switch (event) {
1044 case DIE_MACHINE_RESTART:
1045 case DIE_MACHINE_HALT:
1046 xpc_die_disengage();
1047 break;
1049 case DIE_KDEBUG_ENTER:
1050 /* Should lack of heartbeat be ignored by other partitions? */
1051 if (!xpc_kdebug_ignore)
1052 break;
1054 /* fall through */
1055 case DIE_MCA_MONARCH_ENTER:
1056 case DIE_INIT_MONARCH_ENTER:
1057 xpc_vars->heartbeat++;
1058 xpc_vars->heartbeat_offline = 1;
1059 break;
1061 case DIE_KDEBUG_LEAVE:
1062 /* Is lack of heartbeat being ignored by other partitions? */
1063 if (!xpc_kdebug_ignore)
1064 break;
1066 /* fall through */
1067 case DIE_MCA_MONARCH_LEAVE:
1068 case DIE_INIT_MONARCH_LEAVE:
1069 xpc_vars->heartbeat++;
1070 xpc_vars->heartbeat_offline = 0;
1071 break;
1074 return NOTIFY_DONE;
1077 int __init
1078 xpc_init(void)
1080 int ret;
1081 short partid;
1082 struct xpc_partition *part;
1083 struct task_struct *kthread;
1084 size_t buf_size;
1086 if (is_shub()) {
1088 * The ia64-sn2 architecture supports at most 64 partitions.
1089 * And the inability to unregister remote AMOs restricts us
1090 * further to only support exactly 64 partitions on this
1091 * architecture, no less.
1093 if (xp_max_npartitions != 64)
1094 return -EINVAL;
1096 xpc_init_sn2();
1098 } else if (is_uv()) {
1099 xpc_init_uv();
1101 } else {
1102 return -ENODEV;
1105 snprintf(xpc_part->bus_id, BUS_ID_SIZE, "part");
1106 snprintf(xpc_chan->bus_id, BUS_ID_SIZE, "chan");
1108 buf_size = max(XPC_RP_VARS_SIZE,
1109 XPC_RP_HEADER_SIZE + XP_NASID_MASK_BYTES);
1110 xpc_remote_copy_buffer = xpc_kmalloc_cacheline_aligned(buf_size,
1111 GFP_KERNEL,
1112 &xpc_remote_copy_buffer_base);
1113 if (xpc_remote_copy_buffer == NULL) {
1114 dev_err(xpc_part, "can't get memory for remote copy buffer\n");
1115 return -ENOMEM;
1118 xpc_partitions = kzalloc(sizeof(struct xpc_partition) *
1119 xp_max_npartitions, GFP_KERNEL);
1120 if (xpc_partitions == NULL) {
1121 dev_err(xpc_part, "can't get memory for partition structure\n");
1122 ret = -ENOMEM;
1123 goto out_1;
1127 * The first few fields of each entry of xpc_partitions[] need to
1128 * be initialized now so that calls to xpc_connect() and
1129 * xpc_disconnect() can be made prior to the activation of any remote
1130 * partition. NOTE THAT NONE OF THE OTHER FIELDS BELONGING TO THESE
1131 * ENTRIES ARE MEANINGFUL UNTIL AFTER AN ENTRY'S CORRESPONDING
1132 * PARTITION HAS BEEN ACTIVATED.
1134 for (partid = 0; partid < xp_max_npartitions; partid++) {
1135 part = &xpc_partitions[partid];
1137 DBUG_ON((u64)part != L1_CACHE_ALIGN((u64)part));
1139 part->act_IRQ_rcvd = 0;
1140 spin_lock_init(&part->act_lock);
1141 part->act_state = XPC_P_INACTIVE;
1142 XPC_SET_REASON(part, 0, 0);
1144 init_timer(&part->disengage_request_timer);
1145 part->disengage_request_timer.function =
1146 xpc_timeout_partition_disengage_request;
1147 part->disengage_request_timer.data = (unsigned long)part;
1149 part->setup_state = XPC_P_UNSET;
1150 init_waitqueue_head(&part->teardown_wq);
1151 atomic_set(&part->references, 0);
1154 xpc_sysctl = register_sysctl_table(xpc_sys_dir);
1157 * Open up protections for IPI operations (and AMO operations on
1158 * Shub 1.1 systems).
1160 xpc_allow_IPI_ops();
1163 * Interrupts being processed will increment this atomic variable and
1164 * awaken the heartbeat thread which will process the interrupts.
1166 atomic_set(&xpc_act_IRQ_rcvd, 0);
1169 * This is safe to do before the xpc_hb_checker thread has started
1170 * because the handler releases a wait queue. If an interrupt is
1171 * received before the thread is waiting, it will not go to sleep,
1172 * but rather immediately process the interrupt.
1174 ret = request_irq(SGI_XPC_ACTIVATE, xpc_act_IRQ_handler, 0,
1175 "xpc hb", NULL);
1176 if (ret != 0) {
1177 dev_err(xpc_part, "can't register ACTIVATE IRQ handler, "
1178 "errno=%d\n", -ret);
1179 ret = -EBUSY;
1180 goto out_2;
1184 * Fill the partition reserved page with the information needed by
1185 * other partitions to discover we are alive and establish initial
1186 * communications.
1188 xpc_rsvd_page = xpc_setup_rsvd_page();
1189 if (xpc_rsvd_page == NULL) {
1190 dev_err(xpc_part, "can't setup our reserved page\n");
1191 ret = -EBUSY;
1192 goto out_3;
1195 /* add ourselves to the reboot_notifier_list */
1196 ret = register_reboot_notifier(&xpc_reboot_notifier);
1197 if (ret != 0)
1198 dev_warn(xpc_part, "can't register reboot notifier\n");
1200 /* add ourselves to the die_notifier list */
1201 ret = register_die_notifier(&xpc_die_notifier);
1202 if (ret != 0)
1203 dev_warn(xpc_part, "can't register die notifier\n");
1205 init_timer(&xpc_hb_timer);
1206 xpc_hb_timer.function = xpc_hb_beater;
1209 * The real work-horse behind xpc. This processes incoming
1210 * interrupts and monitors remote heartbeats.
1212 kthread = kthread_run(xpc_hb_checker, NULL, XPC_HB_CHECK_THREAD_NAME);
1213 if (IS_ERR(kthread)) {
1214 dev_err(xpc_part, "failed while forking hb check thread\n");
1215 ret = -EBUSY;
1216 goto out_4;
1220 * Startup a thread that will attempt to discover other partitions to
1221 * activate based on info provided by SAL. This new thread is short
1222 * lived and will exit once discovery is complete.
1224 kthread = kthread_run(xpc_initiate_discovery, NULL,
1225 XPC_DISCOVERY_THREAD_NAME);
1226 if (IS_ERR(kthread)) {
1227 dev_err(xpc_part, "failed while forking discovery thread\n");
1229 /* mark this new thread as a non-starter */
1230 complete(&xpc_discovery_exited);
1232 xpc_do_exit(xpUnloading);
1233 return -EBUSY;
1236 /* set the interface to point at XPC's functions */
1237 xpc_set_interface(xpc_initiate_connect, xpc_initiate_disconnect,
1238 xpc_initiate_allocate, xpc_initiate_send,
1239 xpc_initiate_send_notify, xpc_initiate_received,
1240 xpc_initiate_partid_to_nasids);
1242 return 0;
1244 /* initialization was not successful */
1245 out_4:
1246 /* indicate to others that our reserved page is uninitialized */
1247 xpc_rsvd_page->stamp = ZERO_STAMP;
1249 del_timer_sync(&xpc_hb_timer);
1250 (void)unregister_die_notifier(&xpc_die_notifier);
1251 (void)unregister_reboot_notifier(&xpc_reboot_notifier);
1252 out_3:
1253 free_irq(SGI_XPC_ACTIVATE, NULL);
1254 out_2:
1255 xpc_restrict_IPI_ops();
1256 if (xpc_sysctl)
1257 unregister_sysctl_table(xpc_sysctl);
1258 kfree(xpc_partitions);
1259 out_1:
1260 kfree(xpc_remote_copy_buffer_base);
1261 return ret;
1264 module_init(xpc_init);
1266 void __exit
1267 xpc_exit(void)
1269 xpc_do_exit(xpUnloading);
1272 module_exit(xpc_exit);
1274 MODULE_AUTHOR("Silicon Graphics, Inc.");
1275 MODULE_DESCRIPTION("Cross Partition Communication (XPC) support");
1276 MODULE_LICENSE("GPL");
1278 module_param(xpc_hb_interval, int, 0);
1279 MODULE_PARM_DESC(xpc_hb_interval, "Number of seconds between "
1280 "heartbeat increments.");
1282 module_param(xpc_hb_check_interval, int, 0);
1283 MODULE_PARM_DESC(xpc_hb_check_interval, "Number of seconds between "
1284 "heartbeat checks.");
1286 module_param(xpc_disengage_request_timelimit, int, 0);
1287 MODULE_PARM_DESC(xpc_disengage_request_timelimit, "Number of seconds to wait "
1288 "for disengage request to complete.");
1290 module_param(xpc_kdebug_ignore, int, 0);
1291 MODULE_PARM_DESC(xpc_kdebug_ignore, "Should lack of heartbeat be ignored by "
1292 "other partitions when dropping into kdebug.");