Merge master.kernel.org:/pub/scm/linux/kernel/git/davej/agpgart
[deliverable/linux.git] / arch / ia64 / sn / kernel / xpc_main.c
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.
5 *
6 * Copyright (c) 2004-2006 Silicon Graphics, Inc. All Rights Reserved.
7 */
8
9
10 /*
11 * Cross Partition Communication (XPC) support - standard version.
12 *
13 * XPC provides a message passing capability that crosses partition
14 * boundaries. This module is made up of two parts:
15 *
16 * partition This part detects the presence/absence of other
17 * partitions. It provides a heartbeat and monitors
18 * the heartbeats of other partitions.
19 *
20 * channel This part manages the channels and sends/receives
21 * messages across them to/from other partitions.
22 *
23 * There are a couple of additional functions residing in XP, which
24 * provide an interface to XPC for its users.
25 *
26 *
27 * Caveats:
28 *
29 * . We currently have no way to determine which nasid an IPI came
30 * from. Thus, xpc_IPI_send() does a remote AMO write followed by
31 * an IPI. The AMO indicates where data is to be pulled from, so
32 * after the IPI arrives, the remote partition checks the AMO word.
33 * The IPI can actually arrive before the AMO however, so other code
34 * must periodically check for this case. Also, remote AMO operations
35 * do not reliably time out. Thus we do a remote PIO read solely to
36 * know whether the remote partition is down and whether we should
37 * stop sending IPIs to it. This remote PIO read operation is set up
38 * in a special nofault region so SAL knows to ignore (and cleanup)
39 * any errors due to the remote AMO write, PIO read, and/or PIO
40 * write operations.
41 *
42 * If/when new hardware solves this IPI problem, we should abandon
43 * the current approach.
44 *
45 */
46
47
48 #include <linux/kernel.h>
49 #include <linux/module.h>
50 #include <linux/init.h>
51 #include <linux/sched.h>
52 #include <linux/syscalls.h>
53 #include <linux/cache.h>
54 #include <linux/interrupt.h>
55 #include <linux/delay.h>
56 #include <linux/reboot.h>
57 #include <linux/completion.h>
58 #include <asm/sn/intr.h>
59 #include <asm/sn/sn_sal.h>
60 #include <asm/kdebug.h>
61 #include <asm/uaccess.h>
62 #include <asm/sn/xpc.h>
63
64
65 /* define two XPC debug device structures to be used with dev_dbg() et al */
66
67 struct device_driver xpc_dbg_name = {
68 .name = "xpc"
69 };
70
71 struct device xpc_part_dbg_subname = {
72 .bus_id = {0}, /* set to "part" at xpc_init() time */
73 .driver = &xpc_dbg_name
74 };
75
76 struct device xpc_chan_dbg_subname = {
77 .bus_id = {0}, /* set to "chan" at xpc_init() time */
78 .driver = &xpc_dbg_name
79 };
80
81 struct device *xpc_part = &xpc_part_dbg_subname;
82 struct device *xpc_chan = &xpc_chan_dbg_subname;
83
84
85 static int xpc_kdebug_ignore;
86
87
88 /* systune related variables for /proc/sys directories */
89
90 static int xpc_hb_interval = XPC_HB_DEFAULT_INTERVAL;
91 static int xpc_hb_min_interval = 1;
92 static int xpc_hb_max_interval = 10;
93
94 static int xpc_hb_check_interval = XPC_HB_CHECK_DEFAULT_INTERVAL;
95 static int xpc_hb_check_min_interval = 10;
96 static int xpc_hb_check_max_interval = 120;
97
98 int xpc_disengage_request_timelimit = XPC_DISENGAGE_REQUEST_DEFAULT_TIMELIMIT;
99 static int xpc_disengage_request_min_timelimit = 0;
100 static int xpc_disengage_request_max_timelimit = 120;
101
102 static ctl_table xpc_sys_xpc_hb_dir[] = {
103 {
104 1,
105 "hb_interval",
106 &xpc_hb_interval,
107 sizeof(int),
108 0644,
109 NULL,
110 &proc_dointvec_minmax,
111 &sysctl_intvec,
112 NULL,
113 &xpc_hb_min_interval,
114 &xpc_hb_max_interval
115 },
116 {
117 2,
118 "hb_check_interval",
119 &xpc_hb_check_interval,
120 sizeof(int),
121 0644,
122 NULL,
123 &proc_dointvec_minmax,
124 &sysctl_intvec,
125 NULL,
126 &xpc_hb_check_min_interval,
127 &xpc_hb_check_max_interval
128 },
129 {0}
130 };
131 static ctl_table xpc_sys_xpc_dir[] = {
132 {
133 1,
134 "hb",
135 NULL,
136 0,
137 0555,
138 xpc_sys_xpc_hb_dir
139 },
140 {
141 2,
142 "disengage_request_timelimit",
143 &xpc_disengage_request_timelimit,
144 sizeof(int),
145 0644,
146 NULL,
147 &proc_dointvec_minmax,
148 &sysctl_intvec,
149 NULL,
150 &xpc_disengage_request_min_timelimit,
151 &xpc_disengage_request_max_timelimit
152 },
153 {0}
154 };
155 static ctl_table xpc_sys_dir[] = {
156 {
157 1,
158 "xpc",
159 NULL,
160 0,
161 0555,
162 xpc_sys_xpc_dir
163 },
164 {0}
165 };
166 static struct ctl_table_header *xpc_sysctl;
167
168 /* non-zero if any remote partition disengage request was timed out */
169 int xpc_disengage_request_timedout;
170
171 /* #of IRQs received */
172 static atomic_t xpc_act_IRQ_rcvd;
173
174 /* IRQ handler notifies this wait queue on receipt of an IRQ */
175 static DECLARE_WAIT_QUEUE_HEAD(xpc_act_IRQ_wq);
176
177 static unsigned long xpc_hb_check_timeout;
178
179 /* notification that the xpc_hb_checker thread has exited */
180 static DECLARE_COMPLETION(xpc_hb_checker_exited);
181
182 /* notification that the xpc_discovery thread has exited */
183 static DECLARE_COMPLETION(xpc_discovery_exited);
184
185
186 static struct timer_list xpc_hb_timer;
187
188
189 static void xpc_kthread_waitmsgs(struct xpc_partition *, struct xpc_channel *);
190
191
192 static int xpc_system_reboot(struct notifier_block *, unsigned long, void *);
193 static struct notifier_block xpc_reboot_notifier = {
194 .notifier_call = xpc_system_reboot,
195 };
196
197 static int xpc_system_die(struct notifier_block *, unsigned long, void *);
198 static struct notifier_block xpc_die_notifier = {
199 .notifier_call = xpc_system_die,
200 };
201
202
203 /*
204 * Timer function to enforce the timelimit on the partition disengage request.
205 */
206 static void
207 xpc_timeout_partition_disengage_request(unsigned long data)
208 {
209 struct xpc_partition *part = (struct xpc_partition *) data;
210
211
212 DBUG_ON(jiffies < part->disengage_request_timeout);
213
214 (void) xpc_partition_disengaged(part);
215
216 DBUG_ON(part->disengage_request_timeout != 0);
217 DBUG_ON(xpc_partition_engaged(1UL << XPC_PARTID(part)) != 0);
218 }
219
220
221 /*
222 * Notify the heartbeat check thread that an IRQ has been received.
223 */
224 static irqreturn_t
225 xpc_act_IRQ_handler(int irq, void *dev_id)
226 {
227 atomic_inc(&xpc_act_IRQ_rcvd);
228 wake_up_interruptible(&xpc_act_IRQ_wq);
229 return IRQ_HANDLED;
230 }
231
232
233 /*
234 * Timer to produce the heartbeat. The timer structures function is
235 * already set when this is initially called. A tunable is used to
236 * specify when the next timeout should occur.
237 */
238 static void
239 xpc_hb_beater(unsigned long dummy)
240 {
241 xpc_vars->heartbeat++;
242
243 if (jiffies >= xpc_hb_check_timeout) {
244 wake_up_interruptible(&xpc_act_IRQ_wq);
245 }
246
247 xpc_hb_timer.expires = jiffies + (xpc_hb_interval * HZ);
248 add_timer(&xpc_hb_timer);
249 }
250
251
252 /*
253 * This thread is responsible for nearly all of the partition
254 * activation/deactivation.
255 */
256 static int
257 xpc_hb_checker(void *ignore)
258 {
259 int last_IRQ_count = 0;
260 int new_IRQ_count;
261 int force_IRQ=0;
262
263
264 /* this thread was marked active by xpc_hb_init() */
265
266 daemonize(XPC_HB_CHECK_THREAD_NAME);
267
268 set_cpus_allowed(current, cpumask_of_cpu(XPC_HB_CHECK_CPU));
269
270 xpc_hb_check_timeout = jiffies + (xpc_hb_check_interval * HZ);
271
272 while (!(volatile int) xpc_exiting) {
273
274 dev_dbg(xpc_part, "woke up with %d ticks rem; %d IRQs have "
275 "been received\n",
276 (int) (xpc_hb_check_timeout - jiffies),
277 atomic_read(&xpc_act_IRQ_rcvd) - last_IRQ_count);
278
279
280 /* checking of remote heartbeats is skewed by IRQ handling */
281 if (jiffies >= xpc_hb_check_timeout) {
282 dev_dbg(xpc_part, "checking remote heartbeats\n");
283 xpc_check_remote_hb();
284
285 /*
286 * We need to periodically recheck to ensure no
287 * IPI/AMO pairs have been missed. That check
288 * must always reset xpc_hb_check_timeout.
289 */
290 force_IRQ = 1;
291 }
292
293
294 /* check for outstanding IRQs */
295 new_IRQ_count = atomic_read(&xpc_act_IRQ_rcvd);
296 if (last_IRQ_count < new_IRQ_count || force_IRQ != 0) {
297 force_IRQ = 0;
298
299 dev_dbg(xpc_part, "found an IRQ to process; will be "
300 "resetting xpc_hb_check_timeout\n");
301
302 last_IRQ_count += xpc_identify_act_IRQ_sender();
303 if (last_IRQ_count < new_IRQ_count) {
304 /* retry once to help avoid missing AMO */
305 (void) xpc_identify_act_IRQ_sender();
306 }
307 last_IRQ_count = new_IRQ_count;
308
309 xpc_hb_check_timeout = jiffies +
310 (xpc_hb_check_interval * HZ);
311 }
312
313 /* wait for IRQ or timeout */
314 (void) wait_event_interruptible(xpc_act_IRQ_wq,
315 (last_IRQ_count < atomic_read(&xpc_act_IRQ_rcvd) ||
316 jiffies >= xpc_hb_check_timeout ||
317 (volatile int) xpc_exiting));
318 }
319
320 dev_dbg(xpc_part, "heartbeat checker is exiting\n");
321
322
323 /* mark this thread as having exited */
324 complete(&xpc_hb_checker_exited);
325 return 0;
326 }
327
328
329 /*
330 * This thread will attempt to discover other partitions to activate
331 * based on info provided by SAL. This new thread is short lived and
332 * will exit once discovery is complete.
333 */
334 static int
335 xpc_initiate_discovery(void *ignore)
336 {
337 daemonize(XPC_DISCOVERY_THREAD_NAME);
338
339 xpc_discovery();
340
341 dev_dbg(xpc_part, "discovery thread is exiting\n");
342
343 /* mark this thread as having exited */
344 complete(&xpc_discovery_exited);
345 return 0;
346 }
347
348
349 /*
350 * Establish first contact with the remote partititon. This involves pulling
351 * the XPC per partition variables from the remote partition and waiting for
352 * the remote partition to pull ours.
353 */
354 static enum xpc_retval
355 xpc_make_first_contact(struct xpc_partition *part)
356 {
357 enum xpc_retval ret;
358
359
360 while ((ret = xpc_pull_remote_vars_part(part)) != xpcSuccess) {
361 if (ret != xpcRetry) {
362 XPC_DEACTIVATE_PARTITION(part, ret);
363 return ret;
364 }
365
366 dev_dbg(xpc_chan, "waiting to make first contact with "
367 "partition %d\n", XPC_PARTID(part));
368
369 /* wait a 1/4 of a second or so */
370 (void) msleep_interruptible(250);
371
372 if (part->act_state == XPC_P_DEACTIVATING) {
373 return part->reason;
374 }
375 }
376
377 return xpc_mark_partition_active(part);
378 }
379
380
381 /*
382 * The first kthread assigned to a newly activated partition is the one
383 * created by XPC HB with which it calls xpc_partition_up(). XPC hangs on to
384 * that kthread until the partition is brought down, at which time that kthread
385 * returns back to XPC HB. (The return of that kthread will signify to XPC HB
386 * that XPC has dismantled all communication infrastructure for the associated
387 * partition.) This kthread becomes the channel manager for that partition.
388 *
389 * Each active partition has a channel manager, who, besides connecting and
390 * disconnecting channels, will ensure that each of the partition's connected
391 * channels has the required number of assigned kthreads to get the work done.
392 */
393 static void
394 xpc_channel_mgr(struct xpc_partition *part)
395 {
396 while (part->act_state != XPC_P_DEACTIVATING ||
397 atomic_read(&part->nchannels_active) > 0 ||
398 !xpc_partition_disengaged(part)) {
399
400 xpc_process_channel_activity(part);
401
402
403 /*
404 * Wait until we've been requested to activate kthreads or
405 * all of the channel's message queues have been torn down or
406 * a signal is pending.
407 *
408 * The channel_mgr_requests is set to 1 after being awakened,
409 * This is done to prevent the channel mgr from making one pass
410 * through the loop for each request, since he will
411 * be servicing all the requests in one pass. The reason it's
412 * set to 1 instead of 0 is so that other kthreads will know
413 * that the channel mgr is running and won't bother trying to
414 * wake him up.
415 */
416 atomic_dec(&part->channel_mgr_requests);
417 (void) wait_event_interruptible(part->channel_mgr_wq,
418 (atomic_read(&part->channel_mgr_requests) > 0 ||
419 (volatile u64) part->local_IPI_amo != 0 ||
420 ((volatile u8) part->act_state ==
421 XPC_P_DEACTIVATING &&
422 atomic_read(&part->nchannels_active) == 0 &&
423 xpc_partition_disengaged(part))));
424 atomic_set(&part->channel_mgr_requests, 1);
425
426 // >>> Does it need to wakeup periodically as well? In case we
427 // >>> miscalculated the #of kthreads to wakeup or create?
428 }
429 }
430
431
432 /*
433 * When XPC HB determines that a partition has come up, it will create a new
434 * kthread and that kthread will call this function to attempt to set up the
435 * basic infrastructure used for Cross Partition Communication with the newly
436 * upped partition.
437 *
438 * The kthread that was created by XPC HB and which setup the XPC
439 * infrastructure will remain assigned to the partition until the partition
440 * goes down. At which time the kthread will teardown the XPC infrastructure
441 * and then exit.
442 *
443 * XPC HB will put the remote partition's XPC per partition specific variables
444 * physical address into xpc_partitions[partid].remote_vars_part_pa prior to
445 * calling xpc_partition_up().
446 */
447 static void
448 xpc_partition_up(struct xpc_partition *part)
449 {
450 DBUG_ON(part->channels != NULL);
451
452 dev_dbg(xpc_chan, "activating partition %d\n", XPC_PARTID(part));
453
454 if (xpc_setup_infrastructure(part) != xpcSuccess) {
455 return;
456 }
457
458 /*
459 * The kthread that XPC HB called us with will become the
460 * channel manager for this partition. It will not return
461 * back to XPC HB until the partition's XPC infrastructure
462 * has been dismantled.
463 */
464
465 (void) xpc_part_ref(part); /* this will always succeed */
466
467 if (xpc_make_first_contact(part) == xpcSuccess) {
468 xpc_channel_mgr(part);
469 }
470
471 xpc_part_deref(part);
472
473 xpc_teardown_infrastructure(part);
474 }
475
476
477 static int
478 xpc_activating(void *__partid)
479 {
480 partid_t partid = (u64) __partid;
481 struct xpc_partition *part = &xpc_partitions[partid];
482 unsigned long irq_flags;
483 struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
484 int ret;
485
486
487 DBUG_ON(partid <= 0 || partid >= XP_MAX_PARTITIONS);
488
489 spin_lock_irqsave(&part->act_lock, irq_flags);
490
491 if (part->act_state == XPC_P_DEACTIVATING) {
492 part->act_state = XPC_P_INACTIVE;
493 spin_unlock_irqrestore(&part->act_lock, irq_flags);
494 part->remote_rp_pa = 0;
495 return 0;
496 }
497
498 /* indicate the thread is activating */
499 DBUG_ON(part->act_state != XPC_P_ACTIVATION_REQ);
500 part->act_state = XPC_P_ACTIVATING;
501
502 XPC_SET_REASON(part, 0, 0);
503 spin_unlock_irqrestore(&part->act_lock, irq_flags);
504
505 dev_dbg(xpc_part, "bringing partition %d up\n", partid);
506
507 daemonize("xpc%02d", partid);
508
509 /*
510 * This thread needs to run at a realtime priority to prevent a
511 * significant performance degradation.
512 */
513 ret = sched_setscheduler(current, SCHED_FIFO, &param);
514 if (ret != 0) {
515 dev_warn(xpc_part, "unable to set pid %d to a realtime "
516 "priority, ret=%d\n", current->pid, ret);
517 }
518
519 /* allow this thread and its children to run on any CPU */
520 set_cpus_allowed(current, CPU_MASK_ALL);
521
522 /*
523 * Register the remote partition's AMOs with SAL so it can handle
524 * and cleanup errors within that address range should the remote
525 * partition go down. We don't unregister this range because it is
526 * difficult to tell when outstanding writes to the remote partition
527 * are finished and thus when it is safe to unregister. This should
528 * not result in wasted space in the SAL xp_addr_region table because
529 * we should get the same page for remote_amos_page_pa after module
530 * reloads and system reboots.
531 */
532 if (sn_register_xp_addr_region(part->remote_amos_page_pa,
533 PAGE_SIZE, 1) < 0) {
534 dev_warn(xpc_part, "xpc_partition_up(%d) failed to register "
535 "xp_addr region\n", partid);
536
537 spin_lock_irqsave(&part->act_lock, irq_flags);
538 part->act_state = XPC_P_INACTIVE;
539 XPC_SET_REASON(part, xpcPhysAddrRegFailed, __LINE__);
540 spin_unlock_irqrestore(&part->act_lock, irq_flags);
541 part->remote_rp_pa = 0;
542 return 0;
543 }
544
545 xpc_allow_hb(partid, xpc_vars);
546 xpc_IPI_send_activated(part);
547
548
549 /*
550 * xpc_partition_up() holds this thread and marks this partition as
551 * XPC_P_ACTIVE by calling xpc_hb_mark_active().
552 */
553 (void) xpc_partition_up(part);
554
555 xpc_disallow_hb(partid, xpc_vars);
556 xpc_mark_partition_inactive(part);
557
558 if (part->reason == xpcReactivating) {
559 /* interrupting ourselves results in activating partition */
560 xpc_IPI_send_reactivate(part);
561 }
562
563 return 0;
564 }
565
566
567 void
568 xpc_activate_partition(struct xpc_partition *part)
569 {
570 partid_t partid = XPC_PARTID(part);
571 unsigned long irq_flags;
572 pid_t pid;
573
574
575 spin_lock_irqsave(&part->act_lock, irq_flags);
576
577 DBUG_ON(part->act_state != XPC_P_INACTIVE);
578
579 part->act_state = XPC_P_ACTIVATION_REQ;
580 XPC_SET_REASON(part, xpcCloneKThread, __LINE__);
581
582 spin_unlock_irqrestore(&part->act_lock, irq_flags);
583
584 pid = kernel_thread(xpc_activating, (void *) ((u64) partid), 0);
585
586 if (unlikely(pid <= 0)) {
587 spin_lock_irqsave(&part->act_lock, irq_flags);
588 part->act_state = XPC_P_INACTIVE;
589 XPC_SET_REASON(part, xpcCloneKThreadFailed, __LINE__);
590 spin_unlock_irqrestore(&part->act_lock, irq_flags);
591 }
592 }
593
594
595 /*
596 * Handle the receipt of a SGI_XPC_NOTIFY IRQ by seeing whether the specified
597 * partition actually sent it. Since SGI_XPC_NOTIFY IRQs may be shared by more
598 * than one partition, we use an AMO_t structure per partition to indicate
599 * whether a partition has sent an IPI or not. >>> If it has, then wake up the
600 * associated kthread to handle it.
601 *
602 * All SGI_XPC_NOTIFY IRQs received by XPC are the result of IPIs sent by XPC
603 * running on other partitions.
604 *
605 * Noteworthy Arguments:
606 *
607 * irq - Interrupt ReQuest number. NOT USED.
608 *
609 * dev_id - partid of IPI's potential sender.
610 */
611 irqreturn_t
612 xpc_notify_IRQ_handler(int irq, void *dev_id)
613 {
614 partid_t partid = (partid_t) (u64) dev_id;
615 struct xpc_partition *part = &xpc_partitions[partid];
616
617
618 DBUG_ON(partid <= 0 || partid >= XP_MAX_PARTITIONS);
619
620 if (xpc_part_ref(part)) {
621 xpc_check_for_channel_activity(part);
622
623 xpc_part_deref(part);
624 }
625 return IRQ_HANDLED;
626 }
627
628
629 /*
630 * Check to see if xpc_notify_IRQ_handler() dropped any IPIs on the floor
631 * because the write to their associated IPI amo completed after the IRQ/IPI
632 * was received.
633 */
634 void
635 xpc_dropped_IPI_check(struct xpc_partition *part)
636 {
637 if (xpc_part_ref(part)) {
638 xpc_check_for_channel_activity(part);
639
640 part->dropped_IPI_timer.expires = jiffies +
641 XPC_P_DROPPED_IPI_WAIT;
642 add_timer(&part->dropped_IPI_timer);
643 xpc_part_deref(part);
644 }
645 }
646
647
648 void
649 xpc_activate_kthreads(struct xpc_channel *ch, int needed)
650 {
651 int idle = atomic_read(&ch->kthreads_idle);
652 int assigned = atomic_read(&ch->kthreads_assigned);
653 int wakeup;
654
655
656 DBUG_ON(needed <= 0);
657
658 if (idle > 0) {
659 wakeup = (needed > idle) ? idle : needed;
660 needed -= wakeup;
661
662 dev_dbg(xpc_chan, "wakeup %d idle kthreads, partid=%d, "
663 "channel=%d\n", wakeup, ch->partid, ch->number);
664
665 /* only wakeup the requested number of kthreads */
666 wake_up_nr(&ch->idle_wq, wakeup);
667 }
668
669 if (needed <= 0) {
670 return;
671 }
672
673 if (needed + assigned > ch->kthreads_assigned_limit) {
674 needed = ch->kthreads_assigned_limit - assigned;
675 // >>>should never be less than 0
676 if (needed <= 0) {
677 return;
678 }
679 }
680
681 dev_dbg(xpc_chan, "create %d new kthreads, partid=%d, channel=%d\n",
682 needed, ch->partid, ch->number);
683
684 xpc_create_kthreads(ch, needed);
685 }
686
687
688 /*
689 * This function is where XPC's kthreads wait for messages to deliver.
690 */
691 static void
692 xpc_kthread_waitmsgs(struct xpc_partition *part, struct xpc_channel *ch)
693 {
694 do {
695 /* deliver messages to their intended recipients */
696
697 while ((volatile s64) ch->w_local_GP.get <
698 (volatile s64) ch->w_remote_GP.put &&
699 !((volatile u32) ch->flags &
700 XPC_C_DISCONNECTING)) {
701 xpc_deliver_msg(ch);
702 }
703
704 if (atomic_inc_return(&ch->kthreads_idle) >
705 ch->kthreads_idle_limit) {
706 /* too many idle kthreads on this channel */
707 atomic_dec(&ch->kthreads_idle);
708 break;
709 }
710
711 dev_dbg(xpc_chan, "idle kthread calling "
712 "wait_event_interruptible_exclusive()\n");
713
714 (void) wait_event_interruptible_exclusive(ch->idle_wq,
715 ((volatile s64) ch->w_local_GP.get <
716 (volatile s64) ch->w_remote_GP.put ||
717 ((volatile u32) ch->flags &
718 XPC_C_DISCONNECTING)));
719
720 atomic_dec(&ch->kthreads_idle);
721
722 } while (!((volatile u32) ch->flags & XPC_C_DISCONNECTING));
723 }
724
725
726 static int
727 xpc_daemonize_kthread(void *args)
728 {
729 partid_t partid = XPC_UNPACK_ARG1(args);
730 u16 ch_number = XPC_UNPACK_ARG2(args);
731 struct xpc_partition *part = &xpc_partitions[partid];
732 struct xpc_channel *ch;
733 int n_needed;
734 unsigned long irq_flags;
735
736
737 daemonize("xpc%02dc%d", partid, ch_number);
738
739 dev_dbg(xpc_chan, "kthread starting, partid=%d, channel=%d\n",
740 partid, ch_number);
741
742 ch = &part->channels[ch_number];
743
744 if (!(ch->flags & XPC_C_DISCONNECTING)) {
745
746 /* let registerer know that connection has been established */
747
748 spin_lock_irqsave(&ch->lock, irq_flags);
749 if (!(ch->flags & XPC_C_CONNECTEDCALLOUT)) {
750 ch->flags |= XPC_C_CONNECTEDCALLOUT;
751 spin_unlock_irqrestore(&ch->lock, irq_flags);
752
753 xpc_connected_callout(ch);
754
755 spin_lock_irqsave(&ch->lock, irq_flags);
756 ch->flags |= XPC_C_CONNECTEDCALLOUT_MADE;
757 spin_unlock_irqrestore(&ch->lock, irq_flags);
758
759 /*
760 * It is possible that while the callout was being
761 * made that the remote partition sent some messages.
762 * If that is the case, we may need to activate
763 * additional kthreads to help deliver them. We only
764 * need one less than total #of messages to deliver.
765 */
766 n_needed = ch->w_remote_GP.put - ch->w_local_GP.get - 1;
767 if (n_needed > 0 &&
768 !(ch->flags & XPC_C_DISCONNECTING)) {
769 xpc_activate_kthreads(ch, n_needed);
770 }
771 } else {
772 spin_unlock_irqrestore(&ch->lock, irq_flags);
773 }
774
775 xpc_kthread_waitmsgs(part, ch);
776 }
777
778 if (atomic_dec_return(&ch->kthreads_assigned) == 0) {
779 spin_lock_irqsave(&ch->lock, irq_flags);
780 if ((ch->flags & XPC_C_CONNECTEDCALLOUT_MADE) &&
781 !(ch->flags & XPC_C_DISCONNECTINGCALLOUT)) {
782 ch->flags |= XPC_C_DISCONNECTINGCALLOUT;
783 spin_unlock_irqrestore(&ch->lock, irq_flags);
784
785 xpc_disconnect_callout(ch, xpcDisconnecting);
786
787 spin_lock_irqsave(&ch->lock, irq_flags);
788 ch->flags |= XPC_C_DISCONNECTINGCALLOUT_MADE;
789 }
790 spin_unlock_irqrestore(&ch->lock, irq_flags);
791 if (atomic_dec_return(&part->nchannels_engaged) == 0) {
792 xpc_mark_partition_disengaged(part);
793 xpc_IPI_send_disengage(part);
794 }
795 }
796
797
798 xpc_msgqueue_deref(ch);
799
800 dev_dbg(xpc_chan, "kthread exiting, partid=%d, channel=%d\n",
801 partid, ch_number);
802
803 xpc_part_deref(part);
804 return 0;
805 }
806
807
808 /*
809 * For each partition that XPC has established communications with, there is
810 * a minimum of one kernel thread assigned to perform any operation that
811 * may potentially sleep or block (basically the callouts to the asynchronous
812 * functions registered via xpc_connect()).
813 *
814 * Additional kthreads are created and destroyed by XPC as the workload
815 * demands.
816 *
817 * A kthread is assigned to one of the active channels that exists for a given
818 * partition.
819 */
820 void
821 xpc_create_kthreads(struct xpc_channel *ch, int needed)
822 {
823 unsigned long irq_flags;
824 pid_t pid;
825 u64 args = XPC_PACK_ARGS(ch->partid, ch->number);
826 struct xpc_partition *part = &xpc_partitions[ch->partid];
827
828
829 while (needed-- > 0) {
830
831 /*
832 * The following is done on behalf of the newly created
833 * kthread. That kthread is responsible for doing the
834 * counterpart to the following before it exits.
835 */
836 (void) xpc_part_ref(part);
837 xpc_msgqueue_ref(ch);
838 if (atomic_inc_return(&ch->kthreads_assigned) == 1 &&
839 atomic_inc_return(&part->nchannels_engaged) == 1) {
840 xpc_mark_partition_engaged(part);
841 }
842
843 pid = kernel_thread(xpc_daemonize_kthread, (void *) args, 0);
844 if (pid < 0) {
845 /* the fork failed */
846 if (atomic_dec_return(&ch->kthreads_assigned) == 0 &&
847 atomic_dec_return(&part->nchannels_engaged) == 0) {
848 xpc_mark_partition_disengaged(part);
849 xpc_IPI_send_disengage(part);
850 }
851 xpc_msgqueue_deref(ch);
852 xpc_part_deref(part);
853
854 if (atomic_read(&ch->kthreads_assigned) <
855 ch->kthreads_idle_limit) {
856 /*
857 * Flag this as an error only if we have an
858 * insufficient #of kthreads for the channel
859 * to function.
860 *
861 * No xpc_msgqueue_ref() is needed here since
862 * the channel mgr is doing this.
863 */
864 spin_lock_irqsave(&ch->lock, irq_flags);
865 XPC_DISCONNECT_CHANNEL(ch, xpcLackOfResources,
866 &irq_flags);
867 spin_unlock_irqrestore(&ch->lock, irq_flags);
868 }
869 break;
870 }
871
872 ch->kthreads_created++; // >>> temporary debug only!!!
873 }
874 }
875
876
877 void
878 xpc_disconnect_wait(int ch_number)
879 {
880 unsigned long irq_flags;
881 partid_t partid;
882 struct xpc_partition *part;
883 struct xpc_channel *ch;
884 int wakeup_channel_mgr;
885
886
887 /* now wait for all callouts to the caller's function to cease */
888 for (partid = 1; partid < XP_MAX_PARTITIONS; partid++) {
889 part = &xpc_partitions[partid];
890
891 if (!xpc_part_ref(part)) {
892 continue;
893 }
894
895 ch = &part->channels[ch_number];
896
897 if (!(ch->flags & XPC_C_WDISCONNECT)) {
898 xpc_part_deref(part);
899 continue;
900 }
901
902 wait_for_completion(&ch->wdisconnect_wait);
903
904 spin_lock_irqsave(&ch->lock, irq_flags);
905 DBUG_ON(!(ch->flags & XPC_C_DISCONNECTED));
906 wakeup_channel_mgr = 0;
907
908 if (ch->delayed_IPI_flags) {
909 if (part->act_state != XPC_P_DEACTIVATING) {
910 spin_lock(&part->IPI_lock);
911 XPC_SET_IPI_FLAGS(part->local_IPI_amo,
912 ch->number, ch->delayed_IPI_flags);
913 spin_unlock(&part->IPI_lock);
914 wakeup_channel_mgr = 1;
915 }
916 ch->delayed_IPI_flags = 0;
917 }
918
919 ch->flags &= ~XPC_C_WDISCONNECT;
920 spin_unlock_irqrestore(&ch->lock, irq_flags);
921
922 if (wakeup_channel_mgr) {
923 xpc_wakeup_channel_mgr(part);
924 }
925
926 xpc_part_deref(part);
927 }
928 }
929
930
931 static void
932 xpc_do_exit(enum xpc_retval reason)
933 {
934 partid_t partid;
935 int active_part_count, printed_waiting_msg = 0;
936 struct xpc_partition *part;
937 unsigned long printmsg_time, disengage_request_timeout = 0;
938
939
940 /* a 'rmmod XPC' and a 'reboot' cannot both end up here together */
941 DBUG_ON(xpc_exiting == 1);
942
943 /*
944 * Let the heartbeat checker thread and the discovery thread
945 * (if one is running) know that they should exit. Also wake up
946 * the heartbeat checker thread in case it's sleeping.
947 */
948 xpc_exiting = 1;
949 wake_up_interruptible(&xpc_act_IRQ_wq);
950
951 /* ignore all incoming interrupts */
952 free_irq(SGI_XPC_ACTIVATE, NULL);
953
954 /* wait for the discovery thread to exit */
955 wait_for_completion(&xpc_discovery_exited);
956
957 /* wait for the heartbeat checker thread to exit */
958 wait_for_completion(&xpc_hb_checker_exited);
959
960
961 /* sleep for a 1/3 of a second or so */
962 (void) msleep_interruptible(300);
963
964
965 /* wait for all partitions to become inactive */
966
967 printmsg_time = jiffies + (XPC_DISENGAGE_PRINTMSG_INTERVAL * HZ);
968 xpc_disengage_request_timedout = 0;
969
970 do {
971 active_part_count = 0;
972
973 for (partid = 1; partid < XP_MAX_PARTITIONS; partid++) {
974 part = &xpc_partitions[partid];
975
976 if (xpc_partition_disengaged(part) &&
977 part->act_state == XPC_P_INACTIVE) {
978 continue;
979 }
980
981 active_part_count++;
982
983 XPC_DEACTIVATE_PARTITION(part, reason);
984
985 if (part->disengage_request_timeout >
986 disengage_request_timeout) {
987 disengage_request_timeout =
988 part->disengage_request_timeout;
989 }
990 }
991
992 if (xpc_partition_engaged(-1UL)) {
993 if (time_after(jiffies, printmsg_time)) {
994 dev_info(xpc_part, "waiting for remote "
995 "partitions to disengage, timeout in "
996 "%ld seconds\n",
997 (disengage_request_timeout - jiffies)
998 / HZ);
999 printmsg_time = jiffies +
1000 (XPC_DISENGAGE_PRINTMSG_INTERVAL * HZ);
1001 printed_waiting_msg = 1;
1002 }
1003
1004 } else if (active_part_count > 0) {
1005 if (printed_waiting_msg) {
1006 dev_info(xpc_part, "waiting for local partition"
1007 " to disengage\n");
1008 printed_waiting_msg = 0;
1009 }
1010
1011 } else {
1012 if (!xpc_disengage_request_timedout) {
1013 dev_info(xpc_part, "all partitions have "
1014 "disengaged\n");
1015 }
1016 break;
1017 }
1018
1019 /* sleep for a 1/3 of a second or so */
1020 (void) msleep_interruptible(300);
1021
1022 } while (1);
1023
1024 DBUG_ON(xpc_partition_engaged(-1UL));
1025
1026
1027 /* indicate to others that our reserved page is uninitialized */
1028 xpc_rsvd_page->vars_pa = 0;
1029
1030 /* now it's time to eliminate our heartbeat */
1031 del_timer_sync(&xpc_hb_timer);
1032 DBUG_ON(xpc_vars->heartbeating_to_mask != 0);
1033
1034 if (reason == xpcUnloading) {
1035 /* take ourselves off of the reboot_notifier_list */
1036 (void) unregister_reboot_notifier(&xpc_reboot_notifier);
1037
1038 /* take ourselves off of the die_notifier list */
1039 (void) unregister_die_notifier(&xpc_die_notifier);
1040 }
1041
1042 /* close down protections for IPI operations */
1043 xpc_restrict_IPI_ops();
1044
1045
1046 /* clear the interface to XPC's functions */
1047 xpc_clear_interface();
1048
1049 if (xpc_sysctl) {
1050 unregister_sysctl_table(xpc_sysctl);
1051 }
1052
1053 kfree(xpc_remote_copy_buffer_base);
1054 }
1055
1056
1057 /*
1058 * This function is called when the system is being rebooted.
1059 */
1060 static int
1061 xpc_system_reboot(struct notifier_block *nb, unsigned long event, void *unused)
1062 {
1063 enum xpc_retval reason;
1064
1065
1066 switch (event) {
1067 case SYS_RESTART:
1068 reason = xpcSystemReboot;
1069 break;
1070 case SYS_HALT:
1071 reason = xpcSystemHalt;
1072 break;
1073 case SYS_POWER_OFF:
1074 reason = xpcSystemPoweroff;
1075 break;
1076 default:
1077 reason = xpcSystemGoingDown;
1078 }
1079
1080 xpc_do_exit(reason);
1081 return NOTIFY_DONE;
1082 }
1083
1084
1085 /*
1086 * Notify other partitions to disengage from all references to our memory.
1087 */
1088 static void
1089 xpc_die_disengage(void)
1090 {
1091 struct xpc_partition *part;
1092 partid_t partid;
1093 unsigned long engaged;
1094 long time, printmsg_time, disengage_request_timeout;
1095
1096
1097 /* keep xpc_hb_checker thread from doing anything (just in case) */
1098 xpc_exiting = 1;
1099
1100 xpc_vars->heartbeating_to_mask = 0; /* indicate we're deactivated */
1101
1102 for (partid = 1; partid < XP_MAX_PARTITIONS; partid++) {
1103 part = &xpc_partitions[partid];
1104
1105 if (!XPC_SUPPORTS_DISENGAGE_REQUEST(part->
1106 remote_vars_version)) {
1107
1108 /* just in case it was left set by an earlier XPC */
1109 xpc_clear_partition_engaged(1UL << partid);
1110 continue;
1111 }
1112
1113 if (xpc_partition_engaged(1UL << partid) ||
1114 part->act_state != XPC_P_INACTIVE) {
1115 xpc_request_partition_disengage(part);
1116 xpc_mark_partition_disengaged(part);
1117 xpc_IPI_send_disengage(part);
1118 }
1119 }
1120
1121 time = rtc_time();
1122 printmsg_time = time +
1123 (XPC_DISENGAGE_PRINTMSG_INTERVAL * sn_rtc_cycles_per_second);
1124 disengage_request_timeout = time +
1125 (xpc_disengage_request_timelimit * sn_rtc_cycles_per_second);
1126
1127 /* wait for all other partitions to disengage from us */
1128
1129 while (1) {
1130 engaged = xpc_partition_engaged(-1UL);
1131 if (!engaged) {
1132 dev_info(xpc_part, "all partitions have disengaged\n");
1133 break;
1134 }
1135
1136 time = rtc_time();
1137 if (time >= disengage_request_timeout) {
1138 for (partid = 1; partid < XP_MAX_PARTITIONS; partid++) {
1139 if (engaged & (1UL << partid)) {
1140 dev_info(xpc_part, "disengage from "
1141 "remote partition %d timed "
1142 "out\n", partid);
1143 }
1144 }
1145 break;
1146 }
1147
1148 if (time >= printmsg_time) {
1149 dev_info(xpc_part, "waiting for remote partitions to "
1150 "disengage, timeout in %ld seconds\n",
1151 (disengage_request_timeout - time) /
1152 sn_rtc_cycles_per_second);
1153 printmsg_time = time +
1154 (XPC_DISENGAGE_PRINTMSG_INTERVAL *
1155 sn_rtc_cycles_per_second);
1156 }
1157 }
1158 }
1159
1160
1161 /*
1162 * This function is called when the system is being restarted or halted due
1163 * to some sort of system failure. If this is the case we need to notify the
1164 * other partitions to disengage from all references to our memory.
1165 * This function can also be called when our heartbeater could be offlined
1166 * for a time. In this case we need to notify other partitions to not worry
1167 * about the lack of a heartbeat.
1168 */
1169 static int
1170 xpc_system_die(struct notifier_block *nb, unsigned long event, void *unused)
1171 {
1172 switch (event) {
1173 case DIE_MACHINE_RESTART:
1174 case DIE_MACHINE_HALT:
1175 xpc_die_disengage();
1176 break;
1177
1178 case DIE_KDEBUG_ENTER:
1179 /* Should lack of heartbeat be ignored by other partitions? */
1180 if (!xpc_kdebug_ignore) {
1181 break;
1182 }
1183 /* fall through */
1184 case DIE_MCA_MONARCH_ENTER:
1185 case DIE_INIT_MONARCH_ENTER:
1186 xpc_vars->heartbeat++;
1187 xpc_vars->heartbeat_offline = 1;
1188 break;
1189
1190 case DIE_KDEBUG_LEAVE:
1191 /* Is lack of heartbeat being ignored by other partitions? */
1192 if (!xpc_kdebug_ignore) {
1193 break;
1194 }
1195 /* fall through */
1196 case DIE_MCA_MONARCH_LEAVE:
1197 case DIE_INIT_MONARCH_LEAVE:
1198 xpc_vars->heartbeat++;
1199 xpc_vars->heartbeat_offline = 0;
1200 break;
1201 }
1202
1203 return NOTIFY_DONE;
1204 }
1205
1206
1207 int __init
1208 xpc_init(void)
1209 {
1210 int ret;
1211 partid_t partid;
1212 struct xpc_partition *part;
1213 pid_t pid;
1214 size_t buf_size;
1215
1216
1217 if (!ia64_platform_is("sn2")) {
1218 return -ENODEV;
1219 }
1220
1221
1222 buf_size = max(XPC_RP_VARS_SIZE,
1223 XPC_RP_HEADER_SIZE + XP_NASID_MASK_BYTES);
1224 xpc_remote_copy_buffer = xpc_kmalloc_cacheline_aligned(buf_size,
1225 GFP_KERNEL, &xpc_remote_copy_buffer_base);
1226 if (xpc_remote_copy_buffer == NULL)
1227 return -ENOMEM;
1228
1229 snprintf(xpc_part->bus_id, BUS_ID_SIZE, "part");
1230 snprintf(xpc_chan->bus_id, BUS_ID_SIZE, "chan");
1231
1232 xpc_sysctl = register_sysctl_table(xpc_sys_dir, 1);
1233
1234 /*
1235 * The first few fields of each entry of xpc_partitions[] need to
1236 * be initialized now so that calls to xpc_connect() and
1237 * xpc_disconnect() can be made prior to the activation of any remote
1238 * partition. NOTE THAT NONE OF THE OTHER FIELDS BELONGING TO THESE
1239 * ENTRIES ARE MEANINGFUL UNTIL AFTER AN ENTRY'S CORRESPONDING
1240 * PARTITION HAS BEEN ACTIVATED.
1241 */
1242 for (partid = 1; partid < XP_MAX_PARTITIONS; partid++) {
1243 part = &xpc_partitions[partid];
1244
1245 DBUG_ON((u64) part != L1_CACHE_ALIGN((u64) part));
1246
1247 part->act_IRQ_rcvd = 0;
1248 spin_lock_init(&part->act_lock);
1249 part->act_state = XPC_P_INACTIVE;
1250 XPC_SET_REASON(part, 0, 0);
1251
1252 init_timer(&part->disengage_request_timer);
1253 part->disengage_request_timer.function =
1254 xpc_timeout_partition_disengage_request;
1255 part->disengage_request_timer.data = (unsigned long) part;
1256
1257 part->setup_state = XPC_P_UNSET;
1258 init_waitqueue_head(&part->teardown_wq);
1259 atomic_set(&part->references, 0);
1260 }
1261
1262 /*
1263 * Open up protections for IPI operations (and AMO operations on
1264 * Shub 1.1 systems).
1265 */
1266 xpc_allow_IPI_ops();
1267
1268 /*
1269 * Interrupts being processed will increment this atomic variable and
1270 * awaken the heartbeat thread which will process the interrupts.
1271 */
1272 atomic_set(&xpc_act_IRQ_rcvd, 0);
1273
1274 /*
1275 * This is safe to do before the xpc_hb_checker thread has started
1276 * because the handler releases a wait queue. If an interrupt is
1277 * received before the thread is waiting, it will not go to sleep,
1278 * but rather immediately process the interrupt.
1279 */
1280 ret = request_irq(SGI_XPC_ACTIVATE, xpc_act_IRQ_handler, 0,
1281 "xpc hb", NULL);
1282 if (ret != 0) {
1283 dev_err(xpc_part, "can't register ACTIVATE IRQ handler, "
1284 "errno=%d\n", -ret);
1285
1286 xpc_restrict_IPI_ops();
1287
1288 if (xpc_sysctl) {
1289 unregister_sysctl_table(xpc_sysctl);
1290 }
1291
1292 kfree(xpc_remote_copy_buffer_base);
1293 return -EBUSY;
1294 }
1295
1296 /*
1297 * Fill the partition reserved page with the information needed by
1298 * other partitions to discover we are alive and establish initial
1299 * communications.
1300 */
1301 xpc_rsvd_page = xpc_rsvd_page_init();
1302 if (xpc_rsvd_page == NULL) {
1303 dev_err(xpc_part, "could not setup our reserved page\n");
1304
1305 free_irq(SGI_XPC_ACTIVATE, NULL);
1306 xpc_restrict_IPI_ops();
1307
1308 if (xpc_sysctl) {
1309 unregister_sysctl_table(xpc_sysctl);
1310 }
1311
1312 kfree(xpc_remote_copy_buffer_base);
1313 return -EBUSY;
1314 }
1315
1316
1317 /* add ourselves to the reboot_notifier_list */
1318 ret = register_reboot_notifier(&xpc_reboot_notifier);
1319 if (ret != 0) {
1320 dev_warn(xpc_part, "can't register reboot notifier\n");
1321 }
1322
1323 /* add ourselves to the die_notifier list (i.e., ia64die_chain) */
1324 ret = register_die_notifier(&xpc_die_notifier);
1325 if (ret != 0) {
1326 dev_warn(xpc_part, "can't register die notifier\n");
1327 }
1328
1329
1330 /*
1331 * Set the beating to other partitions into motion. This is
1332 * the last requirement for other partitions' discovery to
1333 * initiate communications with us.
1334 */
1335 init_timer(&xpc_hb_timer);
1336 xpc_hb_timer.function = xpc_hb_beater;
1337 xpc_hb_beater(0);
1338
1339
1340 /*
1341 * The real work-horse behind xpc. This processes incoming
1342 * interrupts and monitors remote heartbeats.
1343 */
1344 pid = kernel_thread(xpc_hb_checker, NULL, 0);
1345 if (pid < 0) {
1346 dev_err(xpc_part, "failed while forking hb check thread\n");
1347
1348 /* indicate to others that our reserved page is uninitialized */
1349 xpc_rsvd_page->vars_pa = 0;
1350
1351 /* take ourselves off of the reboot_notifier_list */
1352 (void) unregister_reboot_notifier(&xpc_reboot_notifier);
1353
1354 /* take ourselves off of the die_notifier list */
1355 (void) unregister_die_notifier(&xpc_die_notifier);
1356
1357 del_timer_sync(&xpc_hb_timer);
1358 free_irq(SGI_XPC_ACTIVATE, NULL);
1359 xpc_restrict_IPI_ops();
1360
1361 if (xpc_sysctl) {
1362 unregister_sysctl_table(xpc_sysctl);
1363 }
1364
1365 kfree(xpc_remote_copy_buffer_base);
1366 return -EBUSY;
1367 }
1368
1369
1370 /*
1371 * Startup a thread that will attempt to discover other partitions to
1372 * activate based on info provided by SAL. This new thread is short
1373 * lived and will exit once discovery is complete.
1374 */
1375 pid = kernel_thread(xpc_initiate_discovery, NULL, 0);
1376 if (pid < 0) {
1377 dev_err(xpc_part, "failed while forking discovery thread\n");
1378
1379 /* mark this new thread as a non-starter */
1380 complete(&xpc_discovery_exited);
1381
1382 xpc_do_exit(xpcUnloading);
1383 return -EBUSY;
1384 }
1385
1386
1387 /* set the interface to point at XPC's functions */
1388 xpc_set_interface(xpc_initiate_connect, xpc_initiate_disconnect,
1389 xpc_initiate_allocate, xpc_initiate_send,
1390 xpc_initiate_send_notify, xpc_initiate_received,
1391 xpc_initiate_partid_to_nasids);
1392
1393 return 0;
1394 }
1395 module_init(xpc_init);
1396
1397
1398 void __exit
1399 xpc_exit(void)
1400 {
1401 xpc_do_exit(xpcUnloading);
1402 }
1403 module_exit(xpc_exit);
1404
1405
1406 MODULE_AUTHOR("Silicon Graphics, Inc.");
1407 MODULE_DESCRIPTION("Cross Partition Communication (XPC) support");
1408 MODULE_LICENSE("GPL");
1409
1410 module_param(xpc_hb_interval, int, 0);
1411 MODULE_PARM_DESC(xpc_hb_interval, "Number of seconds between "
1412 "heartbeat increments.");
1413
1414 module_param(xpc_hb_check_interval, int, 0);
1415 MODULE_PARM_DESC(xpc_hb_check_interval, "Number of seconds between "
1416 "heartbeat checks.");
1417
1418 module_param(xpc_disengage_request_timelimit, int, 0);
1419 MODULE_PARM_DESC(xpc_disengage_request_timelimit, "Number of seconds to wait "
1420 "for disengage request to complete.");
1421
1422 module_param(xpc_kdebug_ignore, int, 0);
1423 MODULE_PARM_DESC(xpc_kdebug_ignore, "Should lack of heartbeat be ignored by "
1424 "other partitions when dropping into kdebug.");
1425
This page took 0.07836 seconds and 6 git commands to generate.