xref: /linux/arch/s390/kernel/time.c (revision a33f32244d8550da8b4a26e277ce07d5c6d158b5)
1 /*
2  *  arch/s390/kernel/time.c
3  *    Time of day based timer functions.
4  *
5  *  S390 version
6  *    Copyright IBM Corp. 1999, 2008
7  *    Author(s): Hartmut Penner (hp@de.ibm.com),
8  *               Martin Schwidefsky (schwidefsky@de.ibm.com),
9  *               Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
10  *
11  *  Derived from "arch/i386/kernel/time.c"
12  *    Copyright (C) 1991, 1992, 1995  Linus Torvalds
13  */
14 
15 #define KMSG_COMPONENT "time"
16 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
17 
18 #include <linux/errno.h>
19 #include <linux/module.h>
20 #include <linux/sched.h>
21 #include <linux/kernel.h>
22 #include <linux/param.h>
23 #include <linux/string.h>
24 #include <linux/mm.h>
25 #include <linux/interrupt.h>
26 #include <linux/cpu.h>
27 #include <linux/stop_machine.h>
28 #include <linux/time.h>
29 #include <linux/sysdev.h>
30 #include <linux/delay.h>
31 #include <linux/init.h>
32 #include <linux/smp.h>
33 #include <linux/types.h>
34 #include <linux/profile.h>
35 #include <linux/timex.h>
36 #include <linux/notifier.h>
37 #include <linux/clocksource.h>
38 #include <linux/clockchips.h>
39 #include <linux/gfp.h>
40 #include <asm/uaccess.h>
41 #include <asm/delay.h>
42 #include <asm/s390_ext.h>
43 #include <asm/div64.h>
44 #include <asm/vdso.h>
45 #include <asm/irq.h>
46 #include <asm/irq_regs.h>
47 #include <asm/timer.h>
48 #include <asm/etr.h>
49 #include <asm/cio.h>
50 
51 /* change this if you have some constant time drift */
52 #define USECS_PER_JIFFY     ((unsigned long) 1000000/HZ)
53 #define CLK_TICKS_PER_JIFFY ((unsigned long) USECS_PER_JIFFY << 12)
54 
55 u64 sched_clock_base_cc = -1;	/* Force to data section. */
56 EXPORT_SYMBOL_GPL(sched_clock_base_cc);
57 
58 static DEFINE_PER_CPU(struct clock_event_device, comparators);
59 
60 /*
61  * Scheduler clock - returns current time in nanosec units.
62  */
63 unsigned long long notrace sched_clock(void)
64 {
65 	return (get_clock_monotonic() * 125) >> 9;
66 }
67 
68 /*
69  * Monotonic_clock - returns # of nanoseconds passed since time_init()
70  */
71 unsigned long long monotonic_clock(void)
72 {
73 	return sched_clock();
74 }
75 EXPORT_SYMBOL(monotonic_clock);
76 
77 void tod_to_timeval(__u64 todval, struct timespec *xt)
78 {
79 	unsigned long long sec;
80 
81 	sec = todval >> 12;
82 	do_div(sec, 1000000);
83 	xt->tv_sec = sec;
84 	todval -= (sec * 1000000) << 12;
85 	xt->tv_nsec = ((todval * 1000) >> 12);
86 }
87 EXPORT_SYMBOL(tod_to_timeval);
88 
89 void clock_comparator_work(void)
90 {
91 	struct clock_event_device *cd;
92 
93 	S390_lowcore.clock_comparator = -1ULL;
94 	set_clock_comparator(S390_lowcore.clock_comparator);
95 	cd = &__get_cpu_var(comparators);
96 	cd->event_handler(cd);
97 }
98 
99 /*
100  * Fixup the clock comparator.
101  */
102 static void fixup_clock_comparator(unsigned long long delta)
103 {
104 	/* If nobody is waiting there's nothing to fix. */
105 	if (S390_lowcore.clock_comparator == -1ULL)
106 		return;
107 	S390_lowcore.clock_comparator += delta;
108 	set_clock_comparator(S390_lowcore.clock_comparator);
109 }
110 
111 static int s390_next_event(unsigned long delta,
112 			   struct clock_event_device *evt)
113 {
114 	S390_lowcore.clock_comparator = get_clock() + delta;
115 	set_clock_comparator(S390_lowcore.clock_comparator);
116 	return 0;
117 }
118 
119 static void s390_set_mode(enum clock_event_mode mode,
120 			  struct clock_event_device *evt)
121 {
122 }
123 
124 /*
125  * Set up lowcore and control register of the current cpu to
126  * enable TOD clock and clock comparator interrupts.
127  */
128 void init_cpu_timer(void)
129 {
130 	struct clock_event_device *cd;
131 	int cpu;
132 
133 	S390_lowcore.clock_comparator = -1ULL;
134 	set_clock_comparator(S390_lowcore.clock_comparator);
135 
136 	cpu = smp_processor_id();
137 	cd = &per_cpu(comparators, cpu);
138 	cd->name		= "comparator";
139 	cd->features		= CLOCK_EVT_FEAT_ONESHOT;
140 	cd->mult		= 16777;
141 	cd->shift		= 12;
142 	cd->min_delta_ns	= 1;
143 	cd->max_delta_ns	= LONG_MAX;
144 	cd->rating		= 400;
145 	cd->cpumask		= cpumask_of(cpu);
146 	cd->set_next_event	= s390_next_event;
147 	cd->set_mode		= s390_set_mode;
148 
149 	clockevents_register_device(cd);
150 
151 	/* Enable clock comparator timer interrupt. */
152 	__ctl_set_bit(0,11);
153 
154 	/* Always allow the timing alert external interrupt. */
155 	__ctl_set_bit(0, 4);
156 }
157 
158 static void clock_comparator_interrupt(__u16 code)
159 {
160 	if (S390_lowcore.clock_comparator == -1ULL)
161 		set_clock_comparator(S390_lowcore.clock_comparator);
162 }
163 
164 static void etr_timing_alert(struct etr_irq_parm *);
165 static void stp_timing_alert(struct stp_irq_parm *);
166 
167 static void timing_alert_interrupt(__u16 code)
168 {
169 	if (S390_lowcore.ext_params & 0x00c40000)
170 		etr_timing_alert((struct etr_irq_parm *)
171 				 &S390_lowcore.ext_params);
172 	if (S390_lowcore.ext_params & 0x00038000)
173 		stp_timing_alert((struct stp_irq_parm *)
174 				 &S390_lowcore.ext_params);
175 }
176 
177 static void etr_reset(void);
178 static void stp_reset(void);
179 
180 void read_persistent_clock(struct timespec *ts)
181 {
182 	tod_to_timeval(get_clock() - TOD_UNIX_EPOCH, ts);
183 }
184 
185 void read_boot_clock(struct timespec *ts)
186 {
187 	tod_to_timeval(sched_clock_base_cc - TOD_UNIX_EPOCH, ts);
188 }
189 
190 static cycle_t read_tod_clock(struct clocksource *cs)
191 {
192 	return get_clock();
193 }
194 
195 static struct clocksource clocksource_tod = {
196 	.name		= "tod",
197 	.rating		= 400,
198 	.read		= read_tod_clock,
199 	.mask		= -1ULL,
200 	.mult		= 1000,
201 	.shift		= 12,
202 	.flags		= CLOCK_SOURCE_IS_CONTINUOUS,
203 };
204 
205 struct clocksource * __init clocksource_default_clock(void)
206 {
207 	return &clocksource_tod;
208 }
209 
210 void update_vsyscall(struct timespec *wall_time, struct clocksource *clock,
211 		     u32 mult)
212 {
213 	if (clock != &clocksource_tod)
214 		return;
215 
216 	/* Make userspace gettimeofday spin until we're done. */
217 	++vdso_data->tb_update_count;
218 	smp_wmb();
219 	vdso_data->xtime_tod_stamp = clock->cycle_last;
220 	vdso_data->xtime_clock_sec = wall_time->tv_sec;
221 	vdso_data->xtime_clock_nsec = wall_time->tv_nsec;
222 	vdso_data->wtom_clock_sec = wall_to_monotonic.tv_sec;
223 	vdso_data->wtom_clock_nsec = wall_to_monotonic.tv_nsec;
224 	vdso_data->ntp_mult = mult;
225 	smp_wmb();
226 	++vdso_data->tb_update_count;
227 }
228 
229 extern struct timezone sys_tz;
230 
231 void update_vsyscall_tz(void)
232 {
233 	/* Make userspace gettimeofday spin until we're done. */
234 	++vdso_data->tb_update_count;
235 	smp_wmb();
236 	vdso_data->tz_minuteswest = sys_tz.tz_minuteswest;
237 	vdso_data->tz_dsttime = sys_tz.tz_dsttime;
238 	smp_wmb();
239 	++vdso_data->tb_update_count;
240 }
241 
242 /*
243  * Initialize the TOD clock and the CPU timer of
244  * the boot cpu.
245  */
246 void __init time_init(void)
247 {
248 	/* Reset time synchronization interfaces. */
249 	etr_reset();
250 	stp_reset();
251 
252 	/* request the clock comparator external interrupt */
253 	if (register_external_interrupt(0x1004, clock_comparator_interrupt))
254                 panic("Couldn't request external interrupt 0x1004");
255 
256 	/* request the timing alert external interrupt */
257 	if (register_external_interrupt(0x1406, timing_alert_interrupt))
258 		panic("Couldn't request external interrupt 0x1406");
259 
260 	if (clocksource_register(&clocksource_tod) != 0)
261 		panic("Could not register TOD clock source");
262 
263 	/* Enable TOD clock interrupts on the boot cpu. */
264 	init_cpu_timer();
265 
266 	/* Enable cpu timer interrupts on the boot cpu. */
267 	vtime_init();
268 }
269 
270 /*
271  * The time is "clock". old is what we think the time is.
272  * Adjust the value by a multiple of jiffies and add the delta to ntp.
273  * "delay" is an approximation how long the synchronization took. If
274  * the time correction is positive, then "delay" is subtracted from
275  * the time difference and only the remaining part is passed to ntp.
276  */
277 static unsigned long long adjust_time(unsigned long long old,
278 				      unsigned long long clock,
279 				      unsigned long long delay)
280 {
281 	unsigned long long delta, ticks;
282 	struct timex adjust;
283 
284 	if (clock > old) {
285 		/* It is later than we thought. */
286 		delta = ticks = clock - old;
287 		delta = ticks = (delta < delay) ? 0 : delta - delay;
288 		delta -= do_div(ticks, CLK_TICKS_PER_JIFFY);
289 		adjust.offset = ticks * (1000000 / HZ);
290 	} else {
291 		/* It is earlier than we thought. */
292 		delta = ticks = old - clock;
293 		delta -= do_div(ticks, CLK_TICKS_PER_JIFFY);
294 		delta = -delta;
295 		adjust.offset = -ticks * (1000000 / HZ);
296 	}
297 	sched_clock_base_cc += delta;
298 	if (adjust.offset != 0) {
299 		pr_notice("The ETR interface has adjusted the clock "
300 			  "by %li microseconds\n", adjust.offset);
301 		adjust.modes = ADJ_OFFSET_SINGLESHOT;
302 		do_adjtimex(&adjust);
303 	}
304 	return delta;
305 }
306 
307 static DEFINE_PER_CPU(atomic_t, clock_sync_word);
308 static DEFINE_MUTEX(clock_sync_mutex);
309 static unsigned long clock_sync_flags;
310 
311 #define CLOCK_SYNC_HAS_ETR	0
312 #define CLOCK_SYNC_HAS_STP	1
313 #define CLOCK_SYNC_ETR		2
314 #define CLOCK_SYNC_STP		3
315 
316 /*
317  * The synchronous get_clock function. It will write the current clock
318  * value to the clock pointer and return 0 if the clock is in sync with
319  * the external time source. If the clock mode is local it will return
320  * -ENOSYS and -EAGAIN if the clock is not in sync with the external
321  * reference.
322  */
323 int get_sync_clock(unsigned long long *clock)
324 {
325 	atomic_t *sw_ptr;
326 	unsigned int sw0, sw1;
327 
328 	sw_ptr = &get_cpu_var(clock_sync_word);
329 	sw0 = atomic_read(sw_ptr);
330 	*clock = get_clock();
331 	sw1 = atomic_read(sw_ptr);
332 	put_cpu_var(clock_sync_word);
333 	if (sw0 == sw1 && (sw0 & 0x80000000U))
334 		/* Success: time is in sync. */
335 		return 0;
336 	if (!test_bit(CLOCK_SYNC_HAS_ETR, &clock_sync_flags) &&
337 	    !test_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags))
338 		return -ENOSYS;
339 	if (!test_bit(CLOCK_SYNC_ETR, &clock_sync_flags) &&
340 	    !test_bit(CLOCK_SYNC_STP, &clock_sync_flags))
341 		return -EACCES;
342 	return -EAGAIN;
343 }
344 EXPORT_SYMBOL(get_sync_clock);
345 
346 /*
347  * Make get_sync_clock return -EAGAIN.
348  */
349 static void disable_sync_clock(void *dummy)
350 {
351 	atomic_t *sw_ptr = &__get_cpu_var(clock_sync_word);
352 	/*
353 	 * Clear the in-sync bit 2^31. All get_sync_clock calls will
354 	 * fail until the sync bit is turned back on. In addition
355 	 * increase the "sequence" counter to avoid the race of an
356 	 * etr event and the complete recovery against get_sync_clock.
357 	 */
358 	atomic_clear_mask(0x80000000, sw_ptr);
359 	atomic_inc(sw_ptr);
360 }
361 
362 /*
363  * Make get_sync_clock return 0 again.
364  * Needs to be called from a context disabled for preemption.
365  */
366 static void enable_sync_clock(void)
367 {
368 	atomic_t *sw_ptr = &__get_cpu_var(clock_sync_word);
369 	atomic_set_mask(0x80000000, sw_ptr);
370 }
371 
372 /*
373  * Function to check if the clock is in sync.
374  */
375 static inline int check_sync_clock(void)
376 {
377 	atomic_t *sw_ptr;
378 	int rc;
379 
380 	sw_ptr = &get_cpu_var(clock_sync_word);
381 	rc = (atomic_read(sw_ptr) & 0x80000000U) != 0;
382 	put_cpu_var(clock_sync_word);
383 	return rc;
384 }
385 
386 /* Single threaded workqueue used for etr and stp sync events */
387 static struct workqueue_struct *time_sync_wq;
388 
389 static void __init time_init_wq(void)
390 {
391 	if (time_sync_wq)
392 		return;
393 	time_sync_wq = create_singlethread_workqueue("timesync");
394 	stop_machine_create();
395 }
396 
397 /*
398  * External Time Reference (ETR) code.
399  */
400 static int etr_port0_online;
401 static int etr_port1_online;
402 static int etr_steai_available;
403 
404 static int __init early_parse_etr(char *p)
405 {
406 	if (strncmp(p, "off", 3) == 0)
407 		etr_port0_online = etr_port1_online = 0;
408 	else if (strncmp(p, "port0", 5) == 0)
409 		etr_port0_online = 1;
410 	else if (strncmp(p, "port1", 5) == 0)
411 		etr_port1_online = 1;
412 	else if (strncmp(p, "on", 2) == 0)
413 		etr_port0_online = etr_port1_online = 1;
414 	return 0;
415 }
416 early_param("etr", early_parse_etr);
417 
418 enum etr_event {
419 	ETR_EVENT_PORT0_CHANGE,
420 	ETR_EVENT_PORT1_CHANGE,
421 	ETR_EVENT_PORT_ALERT,
422 	ETR_EVENT_SYNC_CHECK,
423 	ETR_EVENT_SWITCH_LOCAL,
424 	ETR_EVENT_UPDATE,
425 };
426 
427 /*
428  * Valid bit combinations of the eacr register are (x = don't care):
429  * e0 e1 dp p0 p1 ea es sl
430  *  0  0  x  0	0  0  0  0  initial, disabled state
431  *  0  0  x  0	1  1  0  0  port 1 online
432  *  0  0  x  1	0  1  0  0  port 0 online
433  *  0  0  x  1	1  1  0  0  both ports online
434  *  0  1  x  0	1  1  0  0  port 1 online and usable, ETR or PPS mode
435  *  0  1  x  0	1  1  0  1  port 1 online, usable and ETR mode
436  *  0  1  x  0	1  1  1  0  port 1 online, usable, PPS mode, in-sync
437  *  0  1  x  0	1  1  1  1  port 1 online, usable, ETR mode, in-sync
438  *  0  1  x  1	1  1  0  0  both ports online, port 1 usable
439  *  0  1  x  1	1  1  1  0  both ports online, port 1 usable, PPS mode, in-sync
440  *  0  1  x  1	1  1  1  1  both ports online, port 1 usable, ETR mode, in-sync
441  *  1  0  x  1	0  1  0  0  port 0 online and usable, ETR or PPS mode
442  *  1  0  x  1	0  1  0  1  port 0 online, usable and ETR mode
443  *  1  0  x  1	0  1  1  0  port 0 online, usable, PPS mode, in-sync
444  *  1  0  x  1	0  1  1  1  port 0 online, usable, ETR mode, in-sync
445  *  1  0  x  1	1  1  0  0  both ports online, port 0 usable
446  *  1  0  x  1	1  1  1  0  both ports online, port 0 usable, PPS mode, in-sync
447  *  1  0  x  1	1  1  1  1  both ports online, port 0 usable, ETR mode, in-sync
448  *  1  1  x  1	1  1  1  0  both ports online & usable, ETR, in-sync
449  *  1  1  x  1	1  1  1  1  both ports online & usable, ETR, in-sync
450  */
451 static struct etr_eacr etr_eacr;
452 static u64 etr_tolec;			/* time of last eacr update */
453 static struct etr_aib etr_port0;
454 static int etr_port0_uptodate;
455 static struct etr_aib etr_port1;
456 static int etr_port1_uptodate;
457 static unsigned long etr_events;
458 static struct timer_list etr_timer;
459 
460 static void etr_timeout(unsigned long dummy);
461 static void etr_work_fn(struct work_struct *work);
462 static DEFINE_MUTEX(etr_work_mutex);
463 static DECLARE_WORK(etr_work, etr_work_fn);
464 
465 /*
466  * Reset ETR attachment.
467  */
468 static void etr_reset(void)
469 {
470 	etr_eacr =  (struct etr_eacr) {
471 		.e0 = 0, .e1 = 0, ._pad0 = 4, .dp = 0,
472 		.p0 = 0, .p1 = 0, ._pad1 = 0, .ea = 0,
473 		.es = 0, .sl = 0 };
474 	if (etr_setr(&etr_eacr) == 0) {
475 		etr_tolec = get_clock();
476 		set_bit(CLOCK_SYNC_HAS_ETR, &clock_sync_flags);
477 		if (etr_port0_online && etr_port1_online)
478 			set_bit(CLOCK_SYNC_ETR, &clock_sync_flags);
479 	} else if (etr_port0_online || etr_port1_online) {
480 		pr_warning("The real or virtual hardware system does "
481 			   "not provide an ETR interface\n");
482 		etr_port0_online = etr_port1_online = 0;
483 	}
484 }
485 
486 static int __init etr_init(void)
487 {
488 	struct etr_aib aib;
489 
490 	if (!test_bit(CLOCK_SYNC_HAS_ETR, &clock_sync_flags))
491 		return 0;
492 	time_init_wq();
493 	/* Check if this machine has the steai instruction. */
494 	if (etr_steai(&aib, ETR_STEAI_STEPPING_PORT) == 0)
495 		etr_steai_available = 1;
496 	setup_timer(&etr_timer, etr_timeout, 0UL);
497 	if (etr_port0_online) {
498 		set_bit(ETR_EVENT_PORT0_CHANGE, &etr_events);
499 		queue_work(time_sync_wq, &etr_work);
500 	}
501 	if (etr_port1_online) {
502 		set_bit(ETR_EVENT_PORT1_CHANGE, &etr_events);
503 		queue_work(time_sync_wq, &etr_work);
504 	}
505 	return 0;
506 }
507 
508 arch_initcall(etr_init);
509 
510 /*
511  * Two sorts of ETR machine checks. The architecture reads:
512  * "When a machine-check niterruption occurs and if a switch-to-local or
513  *  ETR-sync-check interrupt request is pending but disabled, this pending
514  *  disabled interruption request is indicated and is cleared".
515  * Which means that we can get etr_switch_to_local events from the machine
516  * check handler although the interruption condition is disabled. Lovely..
517  */
518 
519 /*
520  * Switch to local machine check. This is called when the last usable
521  * ETR port goes inactive. After switch to local the clock is not in sync.
522  */
523 void etr_switch_to_local(void)
524 {
525 	if (!etr_eacr.sl)
526 		return;
527 	disable_sync_clock(NULL);
528 	set_bit(ETR_EVENT_SWITCH_LOCAL, &etr_events);
529 	queue_work(time_sync_wq, &etr_work);
530 }
531 
532 /*
533  * ETR sync check machine check. This is called when the ETR OTE and the
534  * local clock OTE are farther apart than the ETR sync check tolerance.
535  * After a ETR sync check the clock is not in sync. The machine check
536  * is broadcasted to all cpus at the same time.
537  */
538 void etr_sync_check(void)
539 {
540 	if (!etr_eacr.es)
541 		return;
542 	disable_sync_clock(NULL);
543 	set_bit(ETR_EVENT_SYNC_CHECK, &etr_events);
544 	queue_work(time_sync_wq, &etr_work);
545 }
546 
547 /*
548  * ETR timing alert. There are two causes:
549  * 1) port state change, check the usability of the port
550  * 2) port alert, one of the ETR-data-validity bits (v1-v2 bits of the
551  *    sldr-status word) or ETR-data word 1 (edf1) or ETR-data word 3 (edf3)
552  *    or ETR-data word 4 (edf4) has changed.
553  */
554 static void etr_timing_alert(struct etr_irq_parm *intparm)
555 {
556 	if (intparm->pc0)
557 		/* ETR port 0 state change. */
558 		set_bit(ETR_EVENT_PORT0_CHANGE, &etr_events);
559 	if (intparm->pc1)
560 		/* ETR port 1 state change. */
561 		set_bit(ETR_EVENT_PORT1_CHANGE, &etr_events);
562 	if (intparm->eai)
563 		/*
564 		 * ETR port alert on either port 0, 1 or both.
565 		 * Both ports are not up-to-date now.
566 		 */
567 		set_bit(ETR_EVENT_PORT_ALERT, &etr_events);
568 	queue_work(time_sync_wq, &etr_work);
569 }
570 
571 static void etr_timeout(unsigned long dummy)
572 {
573 	set_bit(ETR_EVENT_UPDATE, &etr_events);
574 	queue_work(time_sync_wq, &etr_work);
575 }
576 
577 /*
578  * Check if the etr mode is pss.
579  */
580 static inline int etr_mode_is_pps(struct etr_eacr eacr)
581 {
582 	return eacr.es && !eacr.sl;
583 }
584 
585 /*
586  * Check if the etr mode is etr.
587  */
588 static inline int etr_mode_is_etr(struct etr_eacr eacr)
589 {
590 	return eacr.es && eacr.sl;
591 }
592 
593 /*
594  * Check if the port can be used for TOD synchronization.
595  * For PPS mode the port has to receive OTEs. For ETR mode
596  * the port has to receive OTEs, the ETR stepping bit has to
597  * be zero and the validity bits for data frame 1, 2, and 3
598  * have to be 1.
599  */
600 static int etr_port_valid(struct etr_aib *aib, int port)
601 {
602 	unsigned int psc;
603 
604 	/* Check that this port is receiving OTEs. */
605 	if (aib->tsp == 0)
606 		return 0;
607 
608 	psc = port ? aib->esw.psc1 : aib->esw.psc0;
609 	if (psc == etr_lpsc_pps_mode)
610 		return 1;
611 	if (psc == etr_lpsc_operational_step)
612 		return !aib->esw.y && aib->slsw.v1 &&
613 			aib->slsw.v2 && aib->slsw.v3;
614 	return 0;
615 }
616 
617 /*
618  * Check if two ports are on the same network.
619  */
620 static int etr_compare_network(struct etr_aib *aib1, struct etr_aib *aib2)
621 {
622 	// FIXME: any other fields we have to compare?
623 	return aib1->edf1.net_id == aib2->edf1.net_id;
624 }
625 
626 /*
627  * Wrapper for etr_stei that converts physical port states
628  * to logical port states to be consistent with the output
629  * of stetr (see etr_psc vs. etr_lpsc).
630  */
631 static void etr_steai_cv(struct etr_aib *aib, unsigned int func)
632 {
633 	BUG_ON(etr_steai(aib, func) != 0);
634 	/* Convert port state to logical port state. */
635 	if (aib->esw.psc0 == 1)
636 		aib->esw.psc0 = 2;
637 	else if (aib->esw.psc0 == 0 && aib->esw.p == 0)
638 		aib->esw.psc0 = 1;
639 	if (aib->esw.psc1 == 1)
640 		aib->esw.psc1 = 2;
641 	else if (aib->esw.psc1 == 0 && aib->esw.p == 1)
642 		aib->esw.psc1 = 1;
643 }
644 
645 /*
646  * Check if the aib a2 is still connected to the same attachment as
647  * aib a1, the etv values differ by one and a2 is valid.
648  */
649 static int etr_aib_follows(struct etr_aib *a1, struct etr_aib *a2, int p)
650 {
651 	int state_a1, state_a2;
652 
653 	/* Paranoia check: e0/e1 should better be the same. */
654 	if (a1->esw.eacr.e0 != a2->esw.eacr.e0 ||
655 	    a1->esw.eacr.e1 != a2->esw.eacr.e1)
656 		return 0;
657 
658 	/* Still connected to the same etr ? */
659 	state_a1 = p ? a1->esw.psc1 : a1->esw.psc0;
660 	state_a2 = p ? a2->esw.psc1 : a2->esw.psc0;
661 	if (state_a1 == etr_lpsc_operational_step) {
662 		if (state_a2 != etr_lpsc_operational_step ||
663 		    a1->edf1.net_id != a2->edf1.net_id ||
664 		    a1->edf1.etr_id != a2->edf1.etr_id ||
665 		    a1->edf1.etr_pn != a2->edf1.etr_pn)
666 			return 0;
667 	} else if (state_a2 != etr_lpsc_pps_mode)
668 		return 0;
669 
670 	/* The ETV value of a2 needs to be ETV of a1 + 1. */
671 	if (a1->edf2.etv + 1 != a2->edf2.etv)
672 		return 0;
673 
674 	if (!etr_port_valid(a2, p))
675 		return 0;
676 
677 	return 1;
678 }
679 
680 struct clock_sync_data {
681 	atomic_t cpus;
682 	int in_sync;
683 	unsigned long long fixup_cc;
684 	int etr_port;
685 	struct etr_aib *etr_aib;
686 };
687 
688 static void clock_sync_cpu(struct clock_sync_data *sync)
689 {
690 	atomic_dec(&sync->cpus);
691 	enable_sync_clock();
692 	/*
693 	 * This looks like a busy wait loop but it isn't. etr_sync_cpus
694 	 * is called on all other cpus while the TOD clocks is stopped.
695 	 * __udelay will stop the cpu on an enabled wait psw until the
696 	 * TOD is running again.
697 	 */
698 	while (sync->in_sync == 0) {
699 		__udelay(1);
700 		/*
701 		 * A different cpu changes *in_sync. Therefore use
702 		 * barrier() to force memory access.
703 		 */
704 		barrier();
705 	}
706 	if (sync->in_sync != 1)
707 		/* Didn't work. Clear per-cpu in sync bit again. */
708 		disable_sync_clock(NULL);
709 	/*
710 	 * This round of TOD syncing is done. Set the clock comparator
711 	 * to the next tick and let the processor continue.
712 	 */
713 	fixup_clock_comparator(sync->fixup_cc);
714 }
715 
716 /*
717  * Sync the TOD clock using the port refered to by aibp. This port
718  * has to be enabled and the other port has to be disabled. The
719  * last eacr update has to be more than 1.6 seconds in the past.
720  */
721 static int etr_sync_clock(void *data)
722 {
723 	static int first;
724 	unsigned long long clock, old_clock, delay, delta;
725 	struct clock_sync_data *etr_sync;
726 	struct etr_aib *sync_port, *aib;
727 	int port;
728 	int rc;
729 
730 	etr_sync = data;
731 
732 	if (xchg(&first, 1) == 1) {
733 		/* Slave */
734 		clock_sync_cpu(etr_sync);
735 		return 0;
736 	}
737 
738 	/* Wait until all other cpus entered the sync function. */
739 	while (atomic_read(&etr_sync->cpus) != 0)
740 		cpu_relax();
741 
742 	port = etr_sync->etr_port;
743 	aib = etr_sync->etr_aib;
744 	sync_port = (port == 0) ? &etr_port0 : &etr_port1;
745 	enable_sync_clock();
746 
747 	/* Set clock to next OTE. */
748 	__ctl_set_bit(14, 21);
749 	__ctl_set_bit(0, 29);
750 	clock = ((unsigned long long) (aib->edf2.etv + 1)) << 32;
751 	old_clock = get_clock();
752 	if (set_clock(clock) == 0) {
753 		__udelay(1);	/* Wait for the clock to start. */
754 		__ctl_clear_bit(0, 29);
755 		__ctl_clear_bit(14, 21);
756 		etr_stetr(aib);
757 		/* Adjust Linux timing variables. */
758 		delay = (unsigned long long)
759 			(aib->edf2.etv - sync_port->edf2.etv) << 32;
760 		delta = adjust_time(old_clock, clock, delay);
761 		etr_sync->fixup_cc = delta;
762 		fixup_clock_comparator(delta);
763 		/* Verify that the clock is properly set. */
764 		if (!etr_aib_follows(sync_port, aib, port)) {
765 			/* Didn't work. */
766 			disable_sync_clock(NULL);
767 			etr_sync->in_sync = -EAGAIN;
768 			rc = -EAGAIN;
769 		} else {
770 			etr_sync->in_sync = 1;
771 			rc = 0;
772 		}
773 	} else {
774 		/* Could not set the clock ?!? */
775 		__ctl_clear_bit(0, 29);
776 		__ctl_clear_bit(14, 21);
777 		disable_sync_clock(NULL);
778 		etr_sync->in_sync = -EAGAIN;
779 		rc = -EAGAIN;
780 	}
781 	xchg(&first, 0);
782 	return rc;
783 }
784 
785 static int etr_sync_clock_stop(struct etr_aib *aib, int port)
786 {
787 	struct clock_sync_data etr_sync;
788 	struct etr_aib *sync_port;
789 	int follows;
790 	int rc;
791 
792 	/* Check if the current aib is adjacent to the sync port aib. */
793 	sync_port = (port == 0) ? &etr_port0 : &etr_port1;
794 	follows = etr_aib_follows(sync_port, aib, port);
795 	memcpy(sync_port, aib, sizeof(*aib));
796 	if (!follows)
797 		return -EAGAIN;
798 	memset(&etr_sync, 0, sizeof(etr_sync));
799 	etr_sync.etr_aib = aib;
800 	etr_sync.etr_port = port;
801 	get_online_cpus();
802 	atomic_set(&etr_sync.cpus, num_online_cpus() - 1);
803 	rc = stop_machine(etr_sync_clock, &etr_sync, &cpu_online_map);
804 	put_online_cpus();
805 	return rc;
806 }
807 
808 /*
809  * Handle the immediate effects of the different events.
810  * The port change event is used for online/offline changes.
811  */
812 static struct etr_eacr etr_handle_events(struct etr_eacr eacr)
813 {
814 	if (test_and_clear_bit(ETR_EVENT_SYNC_CHECK, &etr_events))
815 		eacr.es = 0;
816 	if (test_and_clear_bit(ETR_EVENT_SWITCH_LOCAL, &etr_events))
817 		eacr.es = eacr.sl = 0;
818 	if (test_and_clear_bit(ETR_EVENT_PORT_ALERT, &etr_events))
819 		etr_port0_uptodate = etr_port1_uptodate = 0;
820 
821 	if (test_and_clear_bit(ETR_EVENT_PORT0_CHANGE, &etr_events)) {
822 		if (eacr.e0)
823 			/*
824 			 * Port change of an enabled port. We have to
825 			 * assume that this can have caused an stepping
826 			 * port switch.
827 			 */
828 			etr_tolec = get_clock();
829 		eacr.p0 = etr_port0_online;
830 		if (!eacr.p0)
831 			eacr.e0 = 0;
832 		etr_port0_uptodate = 0;
833 	}
834 	if (test_and_clear_bit(ETR_EVENT_PORT1_CHANGE, &etr_events)) {
835 		if (eacr.e1)
836 			/*
837 			 * Port change of an enabled port. We have to
838 			 * assume that this can have caused an stepping
839 			 * port switch.
840 			 */
841 			etr_tolec = get_clock();
842 		eacr.p1 = etr_port1_online;
843 		if (!eacr.p1)
844 			eacr.e1 = 0;
845 		etr_port1_uptodate = 0;
846 	}
847 	clear_bit(ETR_EVENT_UPDATE, &etr_events);
848 	return eacr;
849 }
850 
851 /*
852  * Set up a timer that expires after the etr_tolec + 1.6 seconds if
853  * one of the ports needs an update.
854  */
855 static void etr_set_tolec_timeout(unsigned long long now)
856 {
857 	unsigned long micros;
858 
859 	if ((!etr_eacr.p0 || etr_port0_uptodate) &&
860 	    (!etr_eacr.p1 || etr_port1_uptodate))
861 		return;
862 	micros = (now > etr_tolec) ? ((now - etr_tolec) >> 12) : 0;
863 	micros = (micros > 1600000) ? 0 : 1600000 - micros;
864 	mod_timer(&etr_timer, jiffies + (micros * HZ) / 1000000 + 1);
865 }
866 
867 /*
868  * Set up a time that expires after 1/2 second.
869  */
870 static void etr_set_sync_timeout(void)
871 {
872 	mod_timer(&etr_timer, jiffies + HZ/2);
873 }
874 
875 /*
876  * Update the aib information for one or both ports.
877  */
878 static struct etr_eacr etr_handle_update(struct etr_aib *aib,
879 					 struct etr_eacr eacr)
880 {
881 	/* With both ports disabled the aib information is useless. */
882 	if (!eacr.e0 && !eacr.e1)
883 		return eacr;
884 
885 	/* Update port0 or port1 with aib stored in etr_work_fn. */
886 	if (aib->esw.q == 0) {
887 		/* Information for port 0 stored. */
888 		if (eacr.p0 && !etr_port0_uptodate) {
889 			etr_port0 = *aib;
890 			if (etr_port0_online)
891 				etr_port0_uptodate = 1;
892 		}
893 	} else {
894 		/* Information for port 1 stored. */
895 		if (eacr.p1 && !etr_port1_uptodate) {
896 			etr_port1 = *aib;
897 			if (etr_port0_online)
898 				etr_port1_uptodate = 1;
899 		}
900 	}
901 
902 	/*
903 	 * Do not try to get the alternate port aib if the clock
904 	 * is not in sync yet.
905 	 */
906 	if (!check_sync_clock())
907 		return eacr;
908 
909 	/*
910 	 * If steai is available we can get the information about
911 	 * the other port immediately. If only stetr is available the
912 	 * data-port bit toggle has to be used.
913 	 */
914 	if (etr_steai_available) {
915 		if (eacr.p0 && !etr_port0_uptodate) {
916 			etr_steai_cv(&etr_port0, ETR_STEAI_PORT_0);
917 			etr_port0_uptodate = 1;
918 		}
919 		if (eacr.p1 && !etr_port1_uptodate) {
920 			etr_steai_cv(&etr_port1, ETR_STEAI_PORT_1);
921 			etr_port1_uptodate = 1;
922 		}
923 	} else {
924 		/*
925 		 * One port was updated above, if the other
926 		 * port is not uptodate toggle dp bit.
927 		 */
928 		if ((eacr.p0 && !etr_port0_uptodate) ||
929 		    (eacr.p1 && !etr_port1_uptodate))
930 			eacr.dp ^= 1;
931 		else
932 			eacr.dp = 0;
933 	}
934 	return eacr;
935 }
936 
937 /*
938  * Write new etr control register if it differs from the current one.
939  * Return 1 if etr_tolec has been updated as well.
940  */
941 static void etr_update_eacr(struct etr_eacr eacr)
942 {
943 	int dp_changed;
944 
945 	if (memcmp(&etr_eacr, &eacr, sizeof(eacr)) == 0)
946 		/* No change, return. */
947 		return;
948 	/*
949 	 * The disable of an active port of the change of the data port
950 	 * bit can/will cause a change in the data port.
951 	 */
952 	dp_changed = etr_eacr.e0 > eacr.e0 || etr_eacr.e1 > eacr.e1 ||
953 		(etr_eacr.dp ^ eacr.dp) != 0;
954 	etr_eacr = eacr;
955 	etr_setr(&etr_eacr);
956 	if (dp_changed)
957 		etr_tolec = get_clock();
958 }
959 
960 /*
961  * ETR work. In this function you'll find the main logic. In
962  * particular this is the only function that calls etr_update_eacr(),
963  * it "controls" the etr control register.
964  */
965 static void etr_work_fn(struct work_struct *work)
966 {
967 	unsigned long long now;
968 	struct etr_eacr eacr;
969 	struct etr_aib aib;
970 	int sync_port;
971 
972 	/* prevent multiple execution. */
973 	mutex_lock(&etr_work_mutex);
974 
975 	/* Create working copy of etr_eacr. */
976 	eacr = etr_eacr;
977 
978 	/* Check for the different events and their immediate effects. */
979 	eacr = etr_handle_events(eacr);
980 
981 	/* Check if ETR is supposed to be active. */
982 	eacr.ea = eacr.p0 || eacr.p1;
983 	if (!eacr.ea) {
984 		/* Both ports offline. Reset everything. */
985 		eacr.dp = eacr.es = eacr.sl = 0;
986 		on_each_cpu(disable_sync_clock, NULL, 1);
987 		del_timer_sync(&etr_timer);
988 		etr_update_eacr(eacr);
989 		goto out_unlock;
990 	}
991 
992 	/* Store aib to get the current ETR status word. */
993 	BUG_ON(etr_stetr(&aib) != 0);
994 	etr_port0.esw = etr_port1.esw = aib.esw;	/* Copy status word. */
995 	now = get_clock();
996 
997 	/*
998 	 * Update the port information if the last stepping port change
999 	 * or data port change is older than 1.6 seconds.
1000 	 */
1001 	if (now >= etr_tolec + (1600000 << 12))
1002 		eacr = etr_handle_update(&aib, eacr);
1003 
1004 	/*
1005 	 * Select ports to enable. The prefered synchronization mode is PPS.
1006 	 * If a port can be enabled depends on a number of things:
1007 	 * 1) The port needs to be online and uptodate. A port is not
1008 	 *    disabled just because it is not uptodate, but it is only
1009 	 *    enabled if it is uptodate.
1010 	 * 2) The port needs to have the same mode (pps / etr).
1011 	 * 3) The port needs to be usable -> etr_port_valid() == 1
1012 	 * 4) To enable the second port the clock needs to be in sync.
1013 	 * 5) If both ports are useable and are ETR ports, the network id
1014 	 *    has to be the same.
1015 	 * The eacr.sl bit is used to indicate etr mode vs. pps mode.
1016 	 */
1017 	if (eacr.p0 && aib.esw.psc0 == etr_lpsc_pps_mode) {
1018 		eacr.sl = 0;
1019 		eacr.e0 = 1;
1020 		if (!etr_mode_is_pps(etr_eacr))
1021 			eacr.es = 0;
1022 		if (!eacr.es || !eacr.p1 || aib.esw.psc1 != etr_lpsc_pps_mode)
1023 			eacr.e1 = 0;
1024 		// FIXME: uptodate checks ?
1025 		else if (etr_port0_uptodate && etr_port1_uptodate)
1026 			eacr.e1 = 1;
1027 		sync_port = (etr_port0_uptodate &&
1028 			     etr_port_valid(&etr_port0, 0)) ? 0 : -1;
1029 	} else if (eacr.p1 && aib.esw.psc1 == etr_lpsc_pps_mode) {
1030 		eacr.sl = 0;
1031 		eacr.e0 = 0;
1032 		eacr.e1 = 1;
1033 		if (!etr_mode_is_pps(etr_eacr))
1034 			eacr.es = 0;
1035 		sync_port = (etr_port1_uptodate &&
1036 			     etr_port_valid(&etr_port1, 1)) ? 1 : -1;
1037 	} else if (eacr.p0 && aib.esw.psc0 == etr_lpsc_operational_step) {
1038 		eacr.sl = 1;
1039 		eacr.e0 = 1;
1040 		if (!etr_mode_is_etr(etr_eacr))
1041 			eacr.es = 0;
1042 		if (!eacr.es || !eacr.p1 ||
1043 		    aib.esw.psc1 != etr_lpsc_operational_alt)
1044 			eacr.e1 = 0;
1045 		else if (etr_port0_uptodate && etr_port1_uptodate &&
1046 			 etr_compare_network(&etr_port0, &etr_port1))
1047 			eacr.e1 = 1;
1048 		sync_port = (etr_port0_uptodate &&
1049 			     etr_port_valid(&etr_port0, 0)) ? 0 : -1;
1050 	} else if (eacr.p1 && aib.esw.psc1 == etr_lpsc_operational_step) {
1051 		eacr.sl = 1;
1052 		eacr.e0 = 0;
1053 		eacr.e1 = 1;
1054 		if (!etr_mode_is_etr(etr_eacr))
1055 			eacr.es = 0;
1056 		sync_port = (etr_port1_uptodate &&
1057 			     etr_port_valid(&etr_port1, 1)) ? 1 : -1;
1058 	} else {
1059 		/* Both ports not usable. */
1060 		eacr.es = eacr.sl = 0;
1061 		sync_port = -1;
1062 	}
1063 
1064 	/*
1065 	 * If the clock is in sync just update the eacr and return.
1066 	 * If there is no valid sync port wait for a port update.
1067 	 */
1068 	if (check_sync_clock() || sync_port < 0) {
1069 		etr_update_eacr(eacr);
1070 		etr_set_tolec_timeout(now);
1071 		goto out_unlock;
1072 	}
1073 
1074 	/*
1075 	 * Prepare control register for clock syncing
1076 	 * (reset data port bit, set sync check control.
1077 	 */
1078 	eacr.dp = 0;
1079 	eacr.es = 1;
1080 
1081 	/*
1082 	 * Update eacr and try to synchronize the clock. If the update
1083 	 * of eacr caused a stepping port switch (or if we have to
1084 	 * assume that a stepping port switch has occured) or the
1085 	 * clock syncing failed, reset the sync check control bit
1086 	 * and set up a timer to try again after 0.5 seconds
1087 	 */
1088 	etr_update_eacr(eacr);
1089 	if (now < etr_tolec + (1600000 << 12) ||
1090 	    etr_sync_clock_stop(&aib, sync_port) != 0) {
1091 		/* Sync failed. Try again in 1/2 second. */
1092 		eacr.es = 0;
1093 		etr_update_eacr(eacr);
1094 		etr_set_sync_timeout();
1095 	} else
1096 		etr_set_tolec_timeout(now);
1097 out_unlock:
1098 	mutex_unlock(&etr_work_mutex);
1099 }
1100 
1101 /*
1102  * Sysfs interface functions
1103  */
1104 static struct sysdev_class etr_sysclass = {
1105 	.name	= "etr",
1106 };
1107 
1108 static struct sys_device etr_port0_dev = {
1109 	.id	= 0,
1110 	.cls	= &etr_sysclass,
1111 };
1112 
1113 static struct sys_device etr_port1_dev = {
1114 	.id	= 1,
1115 	.cls	= &etr_sysclass,
1116 };
1117 
1118 /*
1119  * ETR class attributes
1120  */
1121 static ssize_t etr_stepping_port_show(struct sysdev_class *class,
1122 					struct sysdev_class_attribute *attr,
1123 					char *buf)
1124 {
1125 	return sprintf(buf, "%i\n", etr_port0.esw.p);
1126 }
1127 
1128 static SYSDEV_CLASS_ATTR(stepping_port, 0400, etr_stepping_port_show, NULL);
1129 
1130 static ssize_t etr_stepping_mode_show(struct sysdev_class *class,
1131 				      	struct sysdev_class_attribute *attr,
1132 					char *buf)
1133 {
1134 	char *mode_str;
1135 
1136 	if (etr_mode_is_pps(etr_eacr))
1137 		mode_str = "pps";
1138 	else if (etr_mode_is_etr(etr_eacr))
1139 		mode_str = "etr";
1140 	else
1141 		mode_str = "local";
1142 	return sprintf(buf, "%s\n", mode_str);
1143 }
1144 
1145 static SYSDEV_CLASS_ATTR(stepping_mode, 0400, etr_stepping_mode_show, NULL);
1146 
1147 /*
1148  * ETR port attributes
1149  */
1150 static inline struct etr_aib *etr_aib_from_dev(struct sys_device *dev)
1151 {
1152 	if (dev == &etr_port0_dev)
1153 		return etr_port0_online ? &etr_port0 : NULL;
1154 	else
1155 		return etr_port1_online ? &etr_port1 : NULL;
1156 }
1157 
1158 static ssize_t etr_online_show(struct sys_device *dev,
1159 				struct sysdev_attribute *attr,
1160 				char *buf)
1161 {
1162 	unsigned int online;
1163 
1164 	online = (dev == &etr_port0_dev) ? etr_port0_online : etr_port1_online;
1165 	return sprintf(buf, "%i\n", online);
1166 }
1167 
1168 static ssize_t etr_online_store(struct sys_device *dev,
1169 				struct sysdev_attribute *attr,
1170 				const char *buf, size_t count)
1171 {
1172 	unsigned int value;
1173 
1174 	value = simple_strtoul(buf, NULL, 0);
1175 	if (value != 0 && value != 1)
1176 		return -EINVAL;
1177 	if (!test_bit(CLOCK_SYNC_HAS_ETR, &clock_sync_flags))
1178 		return -EOPNOTSUPP;
1179 	mutex_lock(&clock_sync_mutex);
1180 	if (dev == &etr_port0_dev) {
1181 		if (etr_port0_online == value)
1182 			goto out;	/* Nothing to do. */
1183 		etr_port0_online = value;
1184 		if (etr_port0_online && etr_port1_online)
1185 			set_bit(CLOCK_SYNC_ETR, &clock_sync_flags);
1186 		else
1187 			clear_bit(CLOCK_SYNC_ETR, &clock_sync_flags);
1188 		set_bit(ETR_EVENT_PORT0_CHANGE, &etr_events);
1189 		queue_work(time_sync_wq, &etr_work);
1190 	} else {
1191 		if (etr_port1_online == value)
1192 			goto out;	/* Nothing to do. */
1193 		etr_port1_online = value;
1194 		if (etr_port0_online && etr_port1_online)
1195 			set_bit(CLOCK_SYNC_ETR, &clock_sync_flags);
1196 		else
1197 			clear_bit(CLOCK_SYNC_ETR, &clock_sync_flags);
1198 		set_bit(ETR_EVENT_PORT1_CHANGE, &etr_events);
1199 		queue_work(time_sync_wq, &etr_work);
1200 	}
1201 out:
1202 	mutex_unlock(&clock_sync_mutex);
1203 	return count;
1204 }
1205 
1206 static SYSDEV_ATTR(online, 0600, etr_online_show, etr_online_store);
1207 
1208 static ssize_t etr_stepping_control_show(struct sys_device *dev,
1209 					struct sysdev_attribute *attr,
1210 					char *buf)
1211 {
1212 	return sprintf(buf, "%i\n", (dev == &etr_port0_dev) ?
1213 		       etr_eacr.e0 : etr_eacr.e1);
1214 }
1215 
1216 static SYSDEV_ATTR(stepping_control, 0400, etr_stepping_control_show, NULL);
1217 
1218 static ssize_t etr_mode_code_show(struct sys_device *dev,
1219 				struct sysdev_attribute *attr, char *buf)
1220 {
1221 	if (!etr_port0_online && !etr_port1_online)
1222 		/* Status word is not uptodate if both ports are offline. */
1223 		return -ENODATA;
1224 	return sprintf(buf, "%i\n", (dev == &etr_port0_dev) ?
1225 		       etr_port0.esw.psc0 : etr_port0.esw.psc1);
1226 }
1227 
1228 static SYSDEV_ATTR(state_code, 0400, etr_mode_code_show, NULL);
1229 
1230 static ssize_t etr_untuned_show(struct sys_device *dev,
1231 				struct sysdev_attribute *attr, char *buf)
1232 {
1233 	struct etr_aib *aib = etr_aib_from_dev(dev);
1234 
1235 	if (!aib || !aib->slsw.v1)
1236 		return -ENODATA;
1237 	return sprintf(buf, "%i\n", aib->edf1.u);
1238 }
1239 
1240 static SYSDEV_ATTR(untuned, 0400, etr_untuned_show, NULL);
1241 
1242 static ssize_t etr_network_id_show(struct sys_device *dev,
1243 				struct sysdev_attribute *attr, char *buf)
1244 {
1245 	struct etr_aib *aib = etr_aib_from_dev(dev);
1246 
1247 	if (!aib || !aib->slsw.v1)
1248 		return -ENODATA;
1249 	return sprintf(buf, "%i\n", aib->edf1.net_id);
1250 }
1251 
1252 static SYSDEV_ATTR(network, 0400, etr_network_id_show, NULL);
1253 
1254 static ssize_t etr_id_show(struct sys_device *dev,
1255 			struct sysdev_attribute *attr, char *buf)
1256 {
1257 	struct etr_aib *aib = etr_aib_from_dev(dev);
1258 
1259 	if (!aib || !aib->slsw.v1)
1260 		return -ENODATA;
1261 	return sprintf(buf, "%i\n", aib->edf1.etr_id);
1262 }
1263 
1264 static SYSDEV_ATTR(id, 0400, etr_id_show, NULL);
1265 
1266 static ssize_t etr_port_number_show(struct sys_device *dev,
1267 			struct sysdev_attribute *attr, char *buf)
1268 {
1269 	struct etr_aib *aib = etr_aib_from_dev(dev);
1270 
1271 	if (!aib || !aib->slsw.v1)
1272 		return -ENODATA;
1273 	return sprintf(buf, "%i\n", aib->edf1.etr_pn);
1274 }
1275 
1276 static SYSDEV_ATTR(port, 0400, etr_port_number_show, NULL);
1277 
1278 static ssize_t etr_coupled_show(struct sys_device *dev,
1279 			struct sysdev_attribute *attr, char *buf)
1280 {
1281 	struct etr_aib *aib = etr_aib_from_dev(dev);
1282 
1283 	if (!aib || !aib->slsw.v3)
1284 		return -ENODATA;
1285 	return sprintf(buf, "%i\n", aib->edf3.c);
1286 }
1287 
1288 static SYSDEV_ATTR(coupled, 0400, etr_coupled_show, NULL);
1289 
1290 static ssize_t etr_local_time_show(struct sys_device *dev,
1291 			struct sysdev_attribute *attr, char *buf)
1292 {
1293 	struct etr_aib *aib = etr_aib_from_dev(dev);
1294 
1295 	if (!aib || !aib->slsw.v3)
1296 		return -ENODATA;
1297 	return sprintf(buf, "%i\n", aib->edf3.blto);
1298 }
1299 
1300 static SYSDEV_ATTR(local_time, 0400, etr_local_time_show, NULL);
1301 
1302 static ssize_t etr_utc_offset_show(struct sys_device *dev,
1303 			struct sysdev_attribute *attr, char *buf)
1304 {
1305 	struct etr_aib *aib = etr_aib_from_dev(dev);
1306 
1307 	if (!aib || !aib->slsw.v3)
1308 		return -ENODATA;
1309 	return sprintf(buf, "%i\n", aib->edf3.buo);
1310 }
1311 
1312 static SYSDEV_ATTR(utc_offset, 0400, etr_utc_offset_show, NULL);
1313 
1314 static struct sysdev_attribute *etr_port_attributes[] = {
1315 	&attr_online,
1316 	&attr_stepping_control,
1317 	&attr_state_code,
1318 	&attr_untuned,
1319 	&attr_network,
1320 	&attr_id,
1321 	&attr_port,
1322 	&attr_coupled,
1323 	&attr_local_time,
1324 	&attr_utc_offset,
1325 	NULL
1326 };
1327 
1328 static int __init etr_register_port(struct sys_device *dev)
1329 {
1330 	struct sysdev_attribute **attr;
1331 	int rc;
1332 
1333 	rc = sysdev_register(dev);
1334 	if (rc)
1335 		goto out;
1336 	for (attr = etr_port_attributes; *attr; attr++) {
1337 		rc = sysdev_create_file(dev, *attr);
1338 		if (rc)
1339 			goto out_unreg;
1340 	}
1341 	return 0;
1342 out_unreg:
1343 	for (; attr >= etr_port_attributes; attr--)
1344 		sysdev_remove_file(dev, *attr);
1345 	sysdev_unregister(dev);
1346 out:
1347 	return rc;
1348 }
1349 
1350 static void __init etr_unregister_port(struct sys_device *dev)
1351 {
1352 	struct sysdev_attribute **attr;
1353 
1354 	for (attr = etr_port_attributes; *attr; attr++)
1355 		sysdev_remove_file(dev, *attr);
1356 	sysdev_unregister(dev);
1357 }
1358 
1359 static int __init etr_init_sysfs(void)
1360 {
1361 	int rc;
1362 
1363 	rc = sysdev_class_register(&etr_sysclass);
1364 	if (rc)
1365 		goto out;
1366 	rc = sysdev_class_create_file(&etr_sysclass, &attr_stepping_port);
1367 	if (rc)
1368 		goto out_unreg_class;
1369 	rc = sysdev_class_create_file(&etr_sysclass, &attr_stepping_mode);
1370 	if (rc)
1371 		goto out_remove_stepping_port;
1372 	rc = etr_register_port(&etr_port0_dev);
1373 	if (rc)
1374 		goto out_remove_stepping_mode;
1375 	rc = etr_register_port(&etr_port1_dev);
1376 	if (rc)
1377 		goto out_remove_port0;
1378 	return 0;
1379 
1380 out_remove_port0:
1381 	etr_unregister_port(&etr_port0_dev);
1382 out_remove_stepping_mode:
1383 	sysdev_class_remove_file(&etr_sysclass, &attr_stepping_mode);
1384 out_remove_stepping_port:
1385 	sysdev_class_remove_file(&etr_sysclass, &attr_stepping_port);
1386 out_unreg_class:
1387 	sysdev_class_unregister(&etr_sysclass);
1388 out:
1389 	return rc;
1390 }
1391 
1392 device_initcall(etr_init_sysfs);
1393 
1394 /*
1395  * Server Time Protocol (STP) code.
1396  */
1397 static int stp_online;
1398 static struct stp_sstpi stp_info;
1399 static void *stp_page;
1400 
1401 static void stp_work_fn(struct work_struct *work);
1402 static DEFINE_MUTEX(stp_work_mutex);
1403 static DECLARE_WORK(stp_work, stp_work_fn);
1404 static struct timer_list stp_timer;
1405 
1406 static int __init early_parse_stp(char *p)
1407 {
1408 	if (strncmp(p, "off", 3) == 0)
1409 		stp_online = 0;
1410 	else if (strncmp(p, "on", 2) == 0)
1411 		stp_online = 1;
1412 	return 0;
1413 }
1414 early_param("stp", early_parse_stp);
1415 
1416 /*
1417  * Reset STP attachment.
1418  */
1419 static void __init stp_reset(void)
1420 {
1421 	int rc;
1422 
1423 	stp_page = (void *) get_zeroed_page(GFP_ATOMIC);
1424 	rc = chsc_sstpc(stp_page, STP_OP_CTRL, 0x0000);
1425 	if (rc == 0)
1426 		set_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags);
1427 	else if (stp_online) {
1428 		pr_warning("The real or virtual hardware system does "
1429 			   "not provide an STP interface\n");
1430 		free_page((unsigned long) stp_page);
1431 		stp_page = NULL;
1432 		stp_online = 0;
1433 	}
1434 }
1435 
1436 static void stp_timeout(unsigned long dummy)
1437 {
1438 	queue_work(time_sync_wq, &stp_work);
1439 }
1440 
1441 static int __init stp_init(void)
1442 {
1443 	if (!test_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags))
1444 		return 0;
1445 	setup_timer(&stp_timer, stp_timeout, 0UL);
1446 	time_init_wq();
1447 	if (!stp_online)
1448 		return 0;
1449 	queue_work(time_sync_wq, &stp_work);
1450 	return 0;
1451 }
1452 
1453 arch_initcall(stp_init);
1454 
1455 /*
1456  * STP timing alert. There are three causes:
1457  * 1) timing status change
1458  * 2) link availability change
1459  * 3) time control parameter change
1460  * In all three cases we are only interested in the clock source state.
1461  * If a STP clock source is now available use it.
1462  */
1463 static void stp_timing_alert(struct stp_irq_parm *intparm)
1464 {
1465 	if (intparm->tsc || intparm->lac || intparm->tcpc)
1466 		queue_work(time_sync_wq, &stp_work);
1467 }
1468 
1469 /*
1470  * STP sync check machine check. This is called when the timing state
1471  * changes from the synchronized state to the unsynchronized state.
1472  * After a STP sync check the clock is not in sync. The machine check
1473  * is broadcasted to all cpus at the same time.
1474  */
1475 void stp_sync_check(void)
1476 {
1477 	disable_sync_clock(NULL);
1478 	queue_work(time_sync_wq, &stp_work);
1479 }
1480 
1481 /*
1482  * STP island condition machine check. This is called when an attached
1483  * server  attempts to communicate over an STP link and the servers
1484  * have matching CTN ids and have a valid stratum-1 configuration
1485  * but the configurations do not match.
1486  */
1487 void stp_island_check(void)
1488 {
1489 	disable_sync_clock(NULL);
1490 	queue_work(time_sync_wq, &stp_work);
1491 }
1492 
1493 
1494 static int stp_sync_clock(void *data)
1495 {
1496 	static int first;
1497 	unsigned long long old_clock, delta;
1498 	struct clock_sync_data *stp_sync;
1499 	int rc;
1500 
1501 	stp_sync = data;
1502 
1503 	if (xchg(&first, 1) == 1) {
1504 		/* Slave */
1505 		clock_sync_cpu(stp_sync);
1506 		return 0;
1507 	}
1508 
1509 	/* Wait until all other cpus entered the sync function. */
1510 	while (atomic_read(&stp_sync->cpus) != 0)
1511 		cpu_relax();
1512 
1513 	enable_sync_clock();
1514 
1515 	rc = 0;
1516 	if (stp_info.todoff[0] || stp_info.todoff[1] ||
1517 	    stp_info.todoff[2] || stp_info.todoff[3] ||
1518 	    stp_info.tmd != 2) {
1519 		old_clock = get_clock();
1520 		rc = chsc_sstpc(stp_page, STP_OP_SYNC, 0);
1521 		if (rc == 0) {
1522 			delta = adjust_time(old_clock, get_clock(), 0);
1523 			fixup_clock_comparator(delta);
1524 			rc = chsc_sstpi(stp_page, &stp_info,
1525 					sizeof(struct stp_sstpi));
1526 			if (rc == 0 && stp_info.tmd != 2)
1527 				rc = -EAGAIN;
1528 		}
1529 	}
1530 	if (rc) {
1531 		disable_sync_clock(NULL);
1532 		stp_sync->in_sync = -EAGAIN;
1533 	} else
1534 		stp_sync->in_sync = 1;
1535 	xchg(&first, 0);
1536 	return 0;
1537 }
1538 
1539 /*
1540  * STP work. Check for the STP state and take over the clock
1541  * synchronization if the STP clock source is usable.
1542  */
1543 static void stp_work_fn(struct work_struct *work)
1544 {
1545 	struct clock_sync_data stp_sync;
1546 	int rc;
1547 
1548 	/* prevent multiple execution. */
1549 	mutex_lock(&stp_work_mutex);
1550 
1551 	if (!stp_online) {
1552 		chsc_sstpc(stp_page, STP_OP_CTRL, 0x0000);
1553 		del_timer_sync(&stp_timer);
1554 		goto out_unlock;
1555 	}
1556 
1557 	rc = chsc_sstpc(stp_page, STP_OP_CTRL, 0xb0e0);
1558 	if (rc)
1559 		goto out_unlock;
1560 
1561 	rc = chsc_sstpi(stp_page, &stp_info, sizeof(struct stp_sstpi));
1562 	if (rc || stp_info.c == 0)
1563 		goto out_unlock;
1564 
1565 	/* Skip synchronization if the clock is already in sync. */
1566 	if (check_sync_clock())
1567 		goto out_unlock;
1568 
1569 	memset(&stp_sync, 0, sizeof(stp_sync));
1570 	get_online_cpus();
1571 	atomic_set(&stp_sync.cpus, num_online_cpus() - 1);
1572 	stop_machine(stp_sync_clock, &stp_sync, &cpu_online_map);
1573 	put_online_cpus();
1574 
1575 	if (!check_sync_clock())
1576 		/*
1577 		 * There is a usable clock but the synchonization failed.
1578 		 * Retry after a second.
1579 		 */
1580 		mod_timer(&stp_timer, jiffies + HZ);
1581 
1582 out_unlock:
1583 	mutex_unlock(&stp_work_mutex);
1584 }
1585 
1586 /*
1587  * STP class sysfs interface functions
1588  */
1589 static struct sysdev_class stp_sysclass = {
1590 	.name	= "stp",
1591 };
1592 
1593 static ssize_t stp_ctn_id_show(struct sysdev_class *class,
1594 				struct sysdev_class_attribute *attr,
1595 				char *buf)
1596 {
1597 	if (!stp_online)
1598 		return -ENODATA;
1599 	return sprintf(buf, "%016llx\n",
1600 		       *(unsigned long long *) stp_info.ctnid);
1601 }
1602 
1603 static SYSDEV_CLASS_ATTR(ctn_id, 0400, stp_ctn_id_show, NULL);
1604 
1605 static ssize_t stp_ctn_type_show(struct sysdev_class *class,
1606 				struct sysdev_class_attribute *attr,
1607 				char *buf)
1608 {
1609 	if (!stp_online)
1610 		return -ENODATA;
1611 	return sprintf(buf, "%i\n", stp_info.ctn);
1612 }
1613 
1614 static SYSDEV_CLASS_ATTR(ctn_type, 0400, stp_ctn_type_show, NULL);
1615 
1616 static ssize_t stp_dst_offset_show(struct sysdev_class *class,
1617 				   struct sysdev_class_attribute *attr,
1618 				   char *buf)
1619 {
1620 	if (!stp_online || !(stp_info.vbits & 0x2000))
1621 		return -ENODATA;
1622 	return sprintf(buf, "%i\n", (int)(s16) stp_info.dsto);
1623 }
1624 
1625 static SYSDEV_CLASS_ATTR(dst_offset, 0400, stp_dst_offset_show, NULL);
1626 
1627 static ssize_t stp_leap_seconds_show(struct sysdev_class *class,
1628 					struct sysdev_class_attribute *attr,
1629 					char *buf)
1630 {
1631 	if (!stp_online || !(stp_info.vbits & 0x8000))
1632 		return -ENODATA;
1633 	return sprintf(buf, "%i\n", (int)(s16) stp_info.leaps);
1634 }
1635 
1636 static SYSDEV_CLASS_ATTR(leap_seconds, 0400, stp_leap_seconds_show, NULL);
1637 
1638 static ssize_t stp_stratum_show(struct sysdev_class *class,
1639 				struct sysdev_class_attribute *attr,
1640 				char *buf)
1641 {
1642 	if (!stp_online)
1643 		return -ENODATA;
1644 	return sprintf(buf, "%i\n", (int)(s16) stp_info.stratum);
1645 }
1646 
1647 static SYSDEV_CLASS_ATTR(stratum, 0400, stp_stratum_show, NULL);
1648 
1649 static ssize_t stp_time_offset_show(struct sysdev_class *class,
1650 				struct sysdev_class_attribute *attr,
1651 				char *buf)
1652 {
1653 	if (!stp_online || !(stp_info.vbits & 0x0800))
1654 		return -ENODATA;
1655 	return sprintf(buf, "%i\n", (int) stp_info.tto);
1656 }
1657 
1658 static SYSDEV_CLASS_ATTR(time_offset, 0400, stp_time_offset_show, NULL);
1659 
1660 static ssize_t stp_time_zone_offset_show(struct sysdev_class *class,
1661 				struct sysdev_class_attribute *attr,
1662 				char *buf)
1663 {
1664 	if (!stp_online || !(stp_info.vbits & 0x4000))
1665 		return -ENODATA;
1666 	return sprintf(buf, "%i\n", (int)(s16) stp_info.tzo);
1667 }
1668 
1669 static SYSDEV_CLASS_ATTR(time_zone_offset, 0400,
1670 			 stp_time_zone_offset_show, NULL);
1671 
1672 static ssize_t stp_timing_mode_show(struct sysdev_class *class,
1673 				struct sysdev_class_attribute *attr,
1674 				char *buf)
1675 {
1676 	if (!stp_online)
1677 		return -ENODATA;
1678 	return sprintf(buf, "%i\n", stp_info.tmd);
1679 }
1680 
1681 static SYSDEV_CLASS_ATTR(timing_mode, 0400, stp_timing_mode_show, NULL);
1682 
1683 static ssize_t stp_timing_state_show(struct sysdev_class *class,
1684 				struct sysdev_class_attribute *attr,
1685 				char *buf)
1686 {
1687 	if (!stp_online)
1688 		return -ENODATA;
1689 	return sprintf(buf, "%i\n", stp_info.tst);
1690 }
1691 
1692 static SYSDEV_CLASS_ATTR(timing_state, 0400, stp_timing_state_show, NULL);
1693 
1694 static ssize_t stp_online_show(struct sysdev_class *class,
1695 				struct sysdev_class_attribute *attr,
1696 				char *buf)
1697 {
1698 	return sprintf(buf, "%i\n", stp_online);
1699 }
1700 
1701 static ssize_t stp_online_store(struct sysdev_class *class,
1702 				struct sysdev_class_attribute *attr,
1703 				const char *buf, size_t count)
1704 {
1705 	unsigned int value;
1706 
1707 	value = simple_strtoul(buf, NULL, 0);
1708 	if (value != 0 && value != 1)
1709 		return -EINVAL;
1710 	if (!test_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags))
1711 		return -EOPNOTSUPP;
1712 	mutex_lock(&clock_sync_mutex);
1713 	stp_online = value;
1714 	if (stp_online)
1715 		set_bit(CLOCK_SYNC_STP, &clock_sync_flags);
1716 	else
1717 		clear_bit(CLOCK_SYNC_STP, &clock_sync_flags);
1718 	queue_work(time_sync_wq, &stp_work);
1719 	mutex_unlock(&clock_sync_mutex);
1720 	return count;
1721 }
1722 
1723 /*
1724  * Can't use SYSDEV_CLASS_ATTR because the attribute should be named
1725  * stp/online but attr_online already exists in this file ..
1726  */
1727 static struct sysdev_class_attribute attr_stp_online = {
1728 	.attr = { .name = "online", .mode = 0600 },
1729 	.show	= stp_online_show,
1730 	.store	= stp_online_store,
1731 };
1732 
1733 static struct sysdev_class_attribute *stp_attributes[] = {
1734 	&attr_ctn_id,
1735 	&attr_ctn_type,
1736 	&attr_dst_offset,
1737 	&attr_leap_seconds,
1738 	&attr_stp_online,
1739 	&attr_stratum,
1740 	&attr_time_offset,
1741 	&attr_time_zone_offset,
1742 	&attr_timing_mode,
1743 	&attr_timing_state,
1744 	NULL
1745 };
1746 
1747 static int __init stp_init_sysfs(void)
1748 {
1749 	struct sysdev_class_attribute **attr;
1750 	int rc;
1751 
1752 	rc = sysdev_class_register(&stp_sysclass);
1753 	if (rc)
1754 		goto out;
1755 	for (attr = stp_attributes; *attr; attr++) {
1756 		rc = sysdev_class_create_file(&stp_sysclass, *attr);
1757 		if (rc)
1758 			goto out_unreg;
1759 	}
1760 	return 0;
1761 out_unreg:
1762 	for (; attr >= stp_attributes; attr--)
1763 		sysdev_class_remove_file(&stp_sysclass, *attr);
1764 	sysdev_class_unregister(&stp_sysclass);
1765 out:
1766 	return rc;
1767 }
1768 
1769 device_initcall(stp_init_sysfs);
1770