time.c 46.3 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4
/*
 *    Time of day based timer functions.
 *
 *  S390 version
M
Martin Schwidefsky 已提交
5
 *    Copyright IBM Corp. 1999, 2008
L
Linus Torvalds 已提交
6 7 8 9 10 11 12 13
 *    Author(s): Hartmut Penner (hp@de.ibm.com),
 *               Martin Schwidefsky (schwidefsky@de.ibm.com),
 *               Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
 *
 *  Derived from "arch/i386/kernel/time.c"
 *    Copyright (C) 1991, 1992, 1995  Linus Torvalds
 */

14 15 16
#define KMSG_COMPONENT "time"
#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt

17
#include <linux/kernel_stat.h>
L
Linus Torvalds 已提交
18 19 20 21 22 23 24 25
#include <linux/errno.h>
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/kernel.h>
#include <linux/param.h>
#include <linux/string.h>
#include <linux/mm.h>
#include <linux/interrupt.h>
26 27
#include <linux/cpu.h>
#include <linux/stop_machine.h>
L
Linus Torvalds 已提交
28
#include <linux/time.h>
29
#include <linux/device.h>
L
Linus Torvalds 已提交
30 31 32 33 34 35 36
#include <linux/delay.h>
#include <linux/init.h>
#include <linux/smp.h>
#include <linux/types.h>
#include <linux/profile.h>
#include <linux/timex.h>
#include <linux/notifier.h>
37
#include <linux/timekeeper_internal.h>
38
#include <linux/clockchips.h>
39
#include <linux/gfp.h>
40
#include <linux/kprobes.h>
L
Linus Torvalds 已提交
41 42 43
#include <asm/uaccess.h>
#include <asm/delay.h>
#include <asm/div64.h>
M
Martin Schwidefsky 已提交
44
#include <asm/vdso.h>
L
Linus Torvalds 已提交
45
#include <asm/irq.h>
H
Heiko Carstens 已提交
46
#include <asm/irq_regs.h>
47
#include <asm/vtimer.h>
M
Martin Schwidefsky 已提交
48
#include <asm/etr.h>
49
#include <asm/cio.h>
50
#include "entry.h"
L
Linus Torvalds 已提交
51 52 53 54 55

/* change this if you have some constant time drift */
#define USECS_PER_JIFFY     ((unsigned long) 1000000/HZ)
#define CLK_TICKS_PER_JIFFY ((unsigned long) USECS_PER_JIFFY << 12)

56
u64 sched_clock_base_cc = -1;	/* Force to data section. */
57
EXPORT_SYMBOL_GPL(sched_clock_base_cc);
58

59
static DEFINE_PER_CPU(struct clock_event_device, comparators);
L
Linus Torvalds 已提交
60 61 62 63

/*
 * Scheduler clock - returns current time in nanosec units.
 */
64
unsigned long long notrace __kprobes sched_clock(void)
L
Linus Torvalds 已提交
65
{
66
	return tod_to_ns(get_tod_clock_monotonic());
L
Linus Torvalds 已提交
67 68
}

69 70 71 72 73 74 75 76 77
/*
 * Monotonic_clock - returns # of nanoseconds passed since time_init()
 */
unsigned long long monotonic_clock(void)
{
	return sched_clock();
}
EXPORT_SYMBOL(monotonic_clock);

J
John Stultz 已提交
78
void tod_to_timeval(__u64 todval, struct timespec *xt)
L
Linus Torvalds 已提交
79 80 81 82 83
{
	unsigned long long sec;

	sec = todval >> 12;
	do_div(sec, 1000000);
J
John Stultz 已提交
84
	xt->tv_sec = sec;
L
Linus Torvalds 已提交
85
	todval -= (sec * 1000000) << 12;
J
John Stultz 已提交
86
	xt->tv_nsec = ((todval * 1000) >> 12);
L
Linus Torvalds 已提交
87
}
88
EXPORT_SYMBOL(tod_to_timeval);
L
Linus Torvalds 已提交
89

90
void clock_comparator_work(void)
L
Linus Torvalds 已提交
91
{
92
	struct clock_event_device *cd;
L
Linus Torvalds 已提交
93

94
	S390_lowcore.clock_comparator = -1ULL;
95
	cd = this_cpu_ptr(&comparators);
96
	cd->event_handler(cd);
L
Linus Torvalds 已提交
97 98 99
}

/*
100
 * Fixup the clock comparator.
L
Linus Torvalds 已提交
101
 */
102
static void fixup_clock_comparator(unsigned long long delta)
L
Linus Torvalds 已提交
103
{
104 105
	/* If nobody is waiting there's nothing to fix. */
	if (S390_lowcore.clock_comparator == -1ULL)
L
Linus Torvalds 已提交
106
		return;
107 108
	S390_lowcore.clock_comparator += delta;
	set_clock_comparator(S390_lowcore.clock_comparator);
L
Linus Torvalds 已提交
109 110
}

111
static int s390_next_event(unsigned long delta,
112
			   struct clock_event_device *evt)
L
Linus Torvalds 已提交
113
{
114
	S390_lowcore.clock_comparator = get_tod_clock() + delta;
115 116
	set_clock_comparator(S390_lowcore.clock_comparator);
	return 0;
L
Linus Torvalds 已提交
117 118
}

119 120
static void s390_set_mode(enum clock_event_mode mode,
			  struct clock_event_device *evt)
L
Linus Torvalds 已提交
121
{
M
Martin Schwidefsky 已提交
122 123 124 125 126
}

/*
 * Set up lowcore and control register of the current cpu to
 * enable TOD clock and clock comparator interrupts.
L
Linus Torvalds 已提交
127 128 129
 */
void init_cpu_timer(void)
{
130 131 132 133 134 135 136 137 138
	struct clock_event_device *cd;
	int cpu;

	S390_lowcore.clock_comparator = -1ULL;
	set_clock_comparator(S390_lowcore.clock_comparator);

	cpu = smp_processor_id();
	cd = &per_cpu(comparators, cpu);
	cd->name		= "comparator";
139
	cd->features		= CLOCK_EVT_FEAT_ONESHOT;
140 141 142 143 144
	cd->mult		= 16777;
	cd->shift		= 12;
	cd->min_delta_ns	= 1;
	cd->max_delta_ns	= LONG_MAX;
	cd->rating		= 400;
145
	cd->cpumask		= cpumask_of(cpu);
146
	cd->set_next_event	= s390_next_event;
147 148 149
	cd->set_mode		= s390_set_mode;

	clockevents_register_device(cd);
M
Martin Schwidefsky 已提交
150 151 152 153

	/* Enable clock comparator timer interrupt. */
	__ctl_set_bit(0,11);

M
Martin Schwidefsky 已提交
154
	/* Always allow the timing alert external interrupt. */
M
Martin Schwidefsky 已提交
155 156 157
	__ctl_set_bit(0, 4);
}

158
static void clock_comparator_interrupt(struct ext_code ext_code,
159 160
				       unsigned int param32,
				       unsigned long param64)
M
Martin Schwidefsky 已提交
161
{
162
	inc_irq_stat(IRQEXT_CLK);
H
Heiko Carstens 已提交
163 164
	if (S390_lowcore.clock_comparator == -1ULL)
		set_clock_comparator(S390_lowcore.clock_comparator);
M
Martin Schwidefsky 已提交
165 166
}

M
Martin Schwidefsky 已提交
167 168 169
static void etr_timing_alert(struct etr_irq_parm *);
static void stp_timing_alert(struct stp_irq_parm *);

170
static void timing_alert_interrupt(struct ext_code ext_code,
171
				   unsigned int param32, unsigned long param64)
M
Martin Schwidefsky 已提交
172
{
173
	inc_irq_stat(IRQEXT_TLA);
174 175 176 177
	if (param32 & 0x00c40000)
		etr_timing_alert((struct etr_irq_parm *) &param32);
	if (param32 & 0x00038000)
		stp_timing_alert((struct stp_irq_parm *) &param32);
M
Martin Schwidefsky 已提交
178 179
}

M
Martin Schwidefsky 已提交
180
static void etr_reset(void);
M
Martin Schwidefsky 已提交
181
static void stp_reset(void);
M
Martin Schwidefsky 已提交
182

183
void read_persistent_clock(struct timespec *ts)
M
Martin Schwidefsky 已提交
184
{
185
	tod_to_timeval(get_tod_clock() - TOD_UNIX_EPOCH, ts);
L
Linus Torvalds 已提交
186
}
M
Martin Schwidefsky 已提交
187

188 189 190
void read_boot_clock(struct timespec *ts)
{
	tod_to_timeval(sched_clock_base_cc - TOD_UNIX_EPOCH, ts);
L
Linus Torvalds 已提交
191 192
}

193
static cycle_t read_tod_clock(struct clocksource *cs)
194
{
195
	return get_tod_clock();
196 197 198 199
}

static struct clocksource clocksource_tod = {
	.name		= "tod",
200
	.rating		= 400,
201 202 203 204
	.read		= read_tod_clock,
	.mask		= -1ULL,
	.mult		= 1000,
	.shift		= 12,
205
	.flags		= CLOCK_SOURCE_IS_CONTINUOUS,
206 207
};

208 209 210 211
struct clocksource * __init clocksource_default_clock(void)
{
	return &clocksource_tod;
}
212

213
void update_vsyscall(struct timekeeper *tk)
M
Martin Schwidefsky 已提交
214
{
215 216
	u64 nsecps;

217
	if (tk->tkr.clock != &clocksource_tod)
M
Martin Schwidefsky 已提交
218 219 220 221 222
		return;

	/* Make userspace gettimeofday spin until we're done. */
	++vdso_data->tb_update_count;
	smp_wmb();
223
	vdso_data->xtime_tod_stamp = tk->tkr.cycle_last;
224
	vdso_data->xtime_clock_sec = tk->xtime_sec;
225
	vdso_data->xtime_clock_nsec = tk->tkr.xtime_nsec;
226 227
	vdso_data->wtom_clock_sec =
		tk->xtime_sec + tk->wall_to_monotonic.tv_sec;
228 229 230
	vdso_data->wtom_clock_nsec = tk->tkr.xtime_nsec +
		+ ((u64) tk->wall_to_monotonic.tv_nsec << tk->tkr.shift);
	nsecps = (u64) NSEC_PER_SEC << tk->tkr.shift;
231 232 233 234
	while (vdso_data->wtom_clock_nsec >= nsecps) {
		vdso_data->wtom_clock_nsec -= nsecps;
		vdso_data->wtom_clock_sec++;
	}
235 236 237 238 239 240 241 242 243 244 245 246 247

	vdso_data->xtime_coarse_sec = tk->xtime_sec;
	vdso_data->xtime_coarse_nsec =
		(long)(tk->tkr.xtime_nsec >> tk->tkr.shift);
	vdso_data->wtom_coarse_sec =
		vdso_data->xtime_coarse_sec + tk->wall_to_monotonic.tv_sec;
	vdso_data->wtom_coarse_nsec =
		vdso_data->xtime_coarse_nsec + tk->wall_to_monotonic.tv_nsec;
	while (vdso_data->wtom_coarse_nsec >= NSEC_PER_SEC) {
		vdso_data->wtom_coarse_nsec -= NSEC_PER_SEC;
		vdso_data->wtom_coarse_sec++;
	}

248 249
	vdso_data->tk_mult = tk->tkr.mult;
	vdso_data->tk_shift = tk->tkr.shift;
M
Martin Schwidefsky 已提交
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
	smp_wmb();
	++vdso_data->tb_update_count;
}

extern struct timezone sys_tz;

void update_vsyscall_tz(void)
{
	/* Make userspace gettimeofday spin until we're done. */
	++vdso_data->tb_update_count;
	smp_wmb();
	vdso_data->tz_minuteswest = sys_tz.tz_minuteswest;
	vdso_data->tz_dsttime = sys_tz.tz_dsttime;
	smp_wmb();
	++vdso_data->tb_update_count;
}

L
Linus Torvalds 已提交
267 268 269 270 271 272
/*
 * Initialize the TOD clock and the CPU timer of
 * the boot cpu.
 */
void __init time_init(void)
{
273 274 275
	/* Reset time synchronization interfaces. */
	etr_reset();
	stp_reset();
L
Linus Torvalds 已提交
276 277

	/* request the clock comparator external interrupt */
278 279
	if (register_external_irq(EXT_IRQ_CLK_COMP, clock_comparator_interrupt))
		panic("Couldn't request external interrupt 0x1004");
L
Linus Torvalds 已提交
280

M
Martin Schwidefsky 已提交
281
	/* request the timing alert external interrupt */
282
	if (register_external_irq(EXT_IRQ_TIMING_ALERT, timing_alert_interrupt))
M
Martin Schwidefsky 已提交
283 284
		panic("Couldn't request external interrupt 0x1406");

285 286 287
	if (clocksource_register(&clocksource_tod) != 0)
		panic("Could not register TOD clock source");

M
Martin Schwidefsky 已提交
288 289
	/* Enable TOD clock interrupts on the boot cpu. */
	init_cpu_timer();
290

M
Martin Schwidefsky 已提交
291
	/* Enable cpu timer interrupts on the boot cpu. */
L
Linus Torvalds 已提交
292
	vtime_init();
M
Martin Schwidefsky 已提交
293 294
}

M
Martin Schwidefsky 已提交
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
/*
 * The time is "clock". old is what we think the time is.
 * Adjust the value by a multiple of jiffies and add the delta to ntp.
 * "delay" is an approximation how long the synchronization took. If
 * the time correction is positive, then "delay" is subtracted from
 * the time difference and only the remaining part is passed to ntp.
 */
static unsigned long long adjust_time(unsigned long long old,
				      unsigned long long clock,
				      unsigned long long delay)
{
	unsigned long long delta, ticks;
	struct timex adjust;

	if (clock > old) {
		/* It is later than we thought. */
		delta = ticks = clock - old;
		delta = ticks = (delta < delay) ? 0 : delta - delay;
		delta -= do_div(ticks, CLK_TICKS_PER_JIFFY);
		adjust.offset = ticks * (1000000 / HZ);
	} else {
		/* It is earlier than we thought. */
		delta = ticks = old - clock;
		delta -= do_div(ticks, CLK_TICKS_PER_JIFFY);
		delta = -delta;
		adjust.offset = -ticks * (1000000 / HZ);
	}
322
	sched_clock_base_cc += delta;
M
Martin Schwidefsky 已提交
323
	if (adjust.offset != 0) {
324 325
		pr_notice("The ETR interface has adjusted the clock "
			  "by %li microseconds\n", adjust.offset);
M
Martin Schwidefsky 已提交
326 327 328 329 330 331 332
		adjust.modes = ADJ_OFFSET_SINGLESHOT;
		do_adjtimex(&adjust);
	}
	return delta;
}

static DEFINE_PER_CPU(atomic_t, clock_sync_word);
333
static DEFINE_MUTEX(clock_sync_mutex);
M
Martin Schwidefsky 已提交
334 335 336 337 338 339 340 341 342 343 344
static unsigned long clock_sync_flags;

#define CLOCK_SYNC_HAS_ETR	0
#define CLOCK_SYNC_HAS_STP	1
#define CLOCK_SYNC_ETR		2
#define CLOCK_SYNC_STP		3

/*
 * The synchronous get_clock function. It will write the current clock
 * value to the clock pointer and return 0 if the clock is in sync with
 * the external time source. If the clock mode is local it will return
345
 * -EOPNOTSUPP and -EAGAIN if the clock is not in sync with the external
M
Martin Schwidefsky 已提交
346 347 348 349 350 351 352 353 354
 * reference.
 */
int get_sync_clock(unsigned long long *clock)
{
	atomic_t *sw_ptr;
	unsigned int sw0, sw1;

	sw_ptr = &get_cpu_var(clock_sync_word);
	sw0 = atomic_read(sw_ptr);
355
	*clock = get_tod_clock();
M
Martin Schwidefsky 已提交
356
	sw1 = atomic_read(sw_ptr);
357
	put_cpu_var(clock_sync_word);
M
Martin Schwidefsky 已提交
358 359 360 361 362
	if (sw0 == sw1 && (sw0 & 0x80000000U))
		/* Success: time is in sync. */
		return 0;
	if (!test_bit(CLOCK_SYNC_HAS_ETR, &clock_sync_flags) &&
	    !test_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags))
363
		return -EOPNOTSUPP;
M
Martin Schwidefsky 已提交
364 365 366 367 368 369 370 371 372 373 374 375
	if (!test_bit(CLOCK_SYNC_ETR, &clock_sync_flags) &&
	    !test_bit(CLOCK_SYNC_STP, &clock_sync_flags))
		return -EACCES;
	return -EAGAIN;
}
EXPORT_SYMBOL(get_sync_clock);

/*
 * Make get_sync_clock return -EAGAIN.
 */
static void disable_sync_clock(void *dummy)
{
376
	atomic_t *sw_ptr = this_cpu_ptr(&clock_sync_word);
M
Martin Schwidefsky 已提交
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
	/*
	 * Clear the in-sync bit 2^31. All get_sync_clock calls will
	 * fail until the sync bit is turned back on. In addition
	 * increase the "sequence" counter to avoid the race of an
	 * etr event and the complete recovery against get_sync_clock.
	 */
	atomic_clear_mask(0x80000000, sw_ptr);
	atomic_inc(sw_ptr);
}

/*
 * Make get_sync_clock return 0 again.
 * Needs to be called from a context disabled for preemption.
 */
static void enable_sync_clock(void)
{
393
	atomic_t *sw_ptr = this_cpu_ptr(&clock_sync_word);
M
Martin Schwidefsky 已提交
394 395 396
	atomic_set_mask(0x80000000, sw_ptr);
}

397 398 399 400 401 402 403 404 405 406
/*
 * Function to check if the clock is in sync.
 */
static inline int check_sync_clock(void)
{
	atomic_t *sw_ptr;
	int rc;

	sw_ptr = &get_cpu_var(clock_sync_word);
	rc = (atomic_read(sw_ptr) & 0x80000000U) != 0;
407
	put_cpu_var(clock_sync_word);
408 409 410
	return rc;
}

411 412 413 414 415
/* Single threaded workqueue used for etr and stp sync events */
static struct workqueue_struct *time_sync_wq;

static void __init time_init_wq(void)
{
416 417 418
	if (time_sync_wq)
		return;
	time_sync_wq = create_singlethread_workqueue("timesync");
419 420
}

M
Martin Schwidefsky 已提交
421 422 423 424 425
/*
 * External Time Reference (ETR) code.
 */
static int etr_port0_online;
static int etr_port1_online;
M
Martin Schwidefsky 已提交
426
static int etr_steai_available;
M
Martin Schwidefsky 已提交
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484

static int __init early_parse_etr(char *p)
{
	if (strncmp(p, "off", 3) == 0)
		etr_port0_online = etr_port1_online = 0;
	else if (strncmp(p, "port0", 5) == 0)
		etr_port0_online = 1;
	else if (strncmp(p, "port1", 5) == 0)
		etr_port1_online = 1;
	else if (strncmp(p, "on", 2) == 0)
		etr_port0_online = etr_port1_online = 1;
	return 0;
}
early_param("etr", early_parse_etr);

enum etr_event {
	ETR_EVENT_PORT0_CHANGE,
	ETR_EVENT_PORT1_CHANGE,
	ETR_EVENT_PORT_ALERT,
	ETR_EVENT_SYNC_CHECK,
	ETR_EVENT_SWITCH_LOCAL,
	ETR_EVENT_UPDATE,
};

/*
 * Valid bit combinations of the eacr register are (x = don't care):
 * e0 e1 dp p0 p1 ea es sl
 *  0  0  x  0	0  0  0  0  initial, disabled state
 *  0  0  x  0	1  1  0  0  port 1 online
 *  0  0  x  1	0  1  0  0  port 0 online
 *  0  0  x  1	1  1  0  0  both ports online
 *  0  1  x  0	1  1  0  0  port 1 online and usable, ETR or PPS mode
 *  0  1  x  0	1  1  0  1  port 1 online, usable and ETR mode
 *  0  1  x  0	1  1  1  0  port 1 online, usable, PPS mode, in-sync
 *  0  1  x  0	1  1  1  1  port 1 online, usable, ETR mode, in-sync
 *  0  1  x  1	1  1  0  0  both ports online, port 1 usable
 *  0  1  x  1	1  1  1  0  both ports online, port 1 usable, PPS mode, in-sync
 *  0  1  x  1	1  1  1  1  both ports online, port 1 usable, ETR mode, in-sync
 *  1  0  x  1	0  1  0  0  port 0 online and usable, ETR or PPS mode
 *  1  0  x  1	0  1  0  1  port 0 online, usable and ETR mode
 *  1  0  x  1	0  1  1  0  port 0 online, usable, PPS mode, in-sync
 *  1  0  x  1	0  1  1  1  port 0 online, usable, ETR mode, in-sync
 *  1  0  x  1	1  1  0  0  both ports online, port 0 usable
 *  1  0  x  1	1  1  1  0  both ports online, port 0 usable, PPS mode, in-sync
 *  1  0  x  1	1  1  1  1  both ports online, port 0 usable, ETR mode, in-sync
 *  1  1  x  1	1  1  1  0  both ports online & usable, ETR, in-sync
 *  1  1  x  1	1  1  1  1  both ports online & usable, ETR, in-sync
 */
static struct etr_eacr etr_eacr;
static u64 etr_tolec;			/* time of last eacr update */
static struct etr_aib etr_port0;
static int etr_port0_uptodate;
static struct etr_aib etr_port1;
static int etr_port1_uptodate;
static unsigned long etr_events;
static struct timer_list etr_timer;

static void etr_timeout(unsigned long dummy);
485
static void etr_work_fn(struct work_struct *work);
M
Martin Schwidefsky 已提交
486
static DEFINE_MUTEX(etr_work_mutex);
487
static DECLARE_WORK(etr_work, etr_work_fn);
M
Martin Schwidefsky 已提交
488 489 490 491 492 493 494 495 496 497

/*
 * Reset ETR attachment.
 */
static void etr_reset(void)
{
	etr_eacr =  (struct etr_eacr) {
		.e0 = 0, .e1 = 0, ._pad0 = 4, .dp = 0,
		.p0 = 0, .p1 = 0, ._pad1 = 0, .ea = 0,
		.es = 0, .sl = 0 };
M
Martin Schwidefsky 已提交
498
	if (etr_setr(&etr_eacr) == 0) {
499
		etr_tolec = get_tod_clock();
M
Martin Schwidefsky 已提交
500
		set_bit(CLOCK_SYNC_HAS_ETR, &clock_sync_flags);
501 502
		if (etr_port0_online && etr_port1_online)
			set_bit(CLOCK_SYNC_ETR, &clock_sync_flags);
M
Martin Schwidefsky 已提交
503
	} else if (etr_port0_online || etr_port1_online) {
504 505
		pr_warning("The real or virtual hardware system does "
			   "not provide an ETR interface\n");
M
Martin Schwidefsky 已提交
506
		etr_port0_online = etr_port1_online = 0;
M
Martin Schwidefsky 已提交
507 508 509
	}
}

510
static int __init etr_init(void)
M
Martin Schwidefsky 已提交
511 512 513
{
	struct etr_aib aib;

M
Martin Schwidefsky 已提交
514
	if (!test_bit(CLOCK_SYNC_HAS_ETR, &clock_sync_flags))
515
		return 0;
516
	time_init_wq();
M
Martin Schwidefsky 已提交
517 518
	/* Check if this machine has the steai instruction. */
	if (etr_steai(&aib, ETR_STEAI_STEPPING_PORT) == 0)
M
Martin Schwidefsky 已提交
519
		etr_steai_available = 1;
M
Martin Schwidefsky 已提交
520 521 522
	setup_timer(&etr_timer, etr_timeout, 0UL);
	if (etr_port0_online) {
		set_bit(ETR_EVENT_PORT0_CHANGE, &etr_events);
523
		queue_work(time_sync_wq, &etr_work);
M
Martin Schwidefsky 已提交
524 525 526
	}
	if (etr_port1_online) {
		set_bit(ETR_EVENT_PORT1_CHANGE, &etr_events);
527
		queue_work(time_sync_wq, &etr_work);
M
Martin Schwidefsky 已提交
528
	}
529
	return 0;
M
Martin Schwidefsky 已提交
530 531
}

532 533
arch_initcall(etr_init);

M
Martin Schwidefsky 已提交
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
/*
 * Two sorts of ETR machine checks. The architecture reads:
 * "When a machine-check niterruption occurs and if a switch-to-local or
 *  ETR-sync-check interrupt request is pending but disabled, this pending
 *  disabled interruption request is indicated and is cleared".
 * Which means that we can get etr_switch_to_local events from the machine
 * check handler although the interruption condition is disabled. Lovely..
 */

/*
 * Switch to local machine check. This is called when the last usable
 * ETR port goes inactive. After switch to local the clock is not in sync.
 */
void etr_switch_to_local(void)
{
	if (!etr_eacr.sl)
		return;
551
	disable_sync_clock(NULL);
552 553 554 555 556
	if (!test_and_set_bit(ETR_EVENT_SWITCH_LOCAL, &etr_events)) {
		etr_eacr.es = etr_eacr.sl = 0;
		etr_setr(&etr_eacr);
		queue_work(time_sync_wq, &etr_work);
	}
M
Martin Schwidefsky 已提交
557 558 559 560 561 562 563 564 565 566 567 568
}

/*
 * ETR sync check machine check. This is called when the ETR OTE and the
 * local clock OTE are farther apart than the ETR sync check tolerance.
 * After a ETR sync check the clock is not in sync. The machine check
 * is broadcasted to all cpus at the same time.
 */
void etr_sync_check(void)
{
	if (!etr_eacr.es)
		return;
569
	disable_sync_clock(NULL);
570 571 572 573 574
	if (!test_and_set_bit(ETR_EVENT_SYNC_CHECK, &etr_events)) {
		etr_eacr.es = 0;
		etr_setr(&etr_eacr);
		queue_work(time_sync_wq, &etr_work);
	}
M
Martin Schwidefsky 已提交
575 576 577
}

/*
M
Martin Schwidefsky 已提交
578
 * ETR timing alert. There are two causes:
M
Martin Schwidefsky 已提交
579 580 581 582 583
 * 1) port state change, check the usability of the port
 * 2) port alert, one of the ETR-data-validity bits (v1-v2 bits of the
 *    sldr-status word) or ETR-data word 1 (edf1) or ETR-data word 3 (edf3)
 *    or ETR-data word 4 (edf4) has changed.
 */
M
Martin Schwidefsky 已提交
584
static void etr_timing_alert(struct etr_irq_parm *intparm)
M
Martin Schwidefsky 已提交
585 586 587 588 589 590 591 592 593 594 595 596 597
{
	if (intparm->pc0)
		/* ETR port 0 state change. */
		set_bit(ETR_EVENT_PORT0_CHANGE, &etr_events);
	if (intparm->pc1)
		/* ETR port 1 state change. */
		set_bit(ETR_EVENT_PORT1_CHANGE, &etr_events);
	if (intparm->eai)
		/*
		 * ETR port alert on either port 0, 1 or both.
		 * Both ports are not up-to-date now.
		 */
		set_bit(ETR_EVENT_PORT_ALERT, &etr_events);
598
	queue_work(time_sync_wq, &etr_work);
M
Martin Schwidefsky 已提交
599 600 601 602 603
}

static void etr_timeout(unsigned long dummy)
{
	set_bit(ETR_EVENT_UPDATE, &etr_events);
604
	queue_work(time_sync_wq, &etr_work);
M
Martin Schwidefsky 已提交
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
}

/*
 * Check if the etr mode is pss.
 */
static inline int etr_mode_is_pps(struct etr_eacr eacr)
{
	return eacr.es && !eacr.sl;
}

/*
 * Check if the etr mode is etr.
 */
static inline int etr_mode_is_etr(struct etr_eacr eacr)
{
	return eacr.es && eacr.sl;
}

/*
 * Check if the port can be used for TOD synchronization.
 * For PPS mode the port has to receive OTEs. For ETR mode
 * the port has to receive OTEs, the ETR stepping bit has to
 * be zero and the validity bits for data frame 1, 2, and 3
 * have to be 1.
 */
static int etr_port_valid(struct etr_aib *aib, int port)
{
	unsigned int psc;

	/* Check that this port is receiving OTEs. */
	if (aib->tsp == 0)
		return 0;

	psc = port ? aib->esw.psc1 : aib->esw.psc0;
	if (psc == etr_lpsc_pps_mode)
		return 1;
	if (psc == etr_lpsc_operational_step)
		return !aib->esw.y && aib->slsw.v1 &&
			aib->slsw.v2 && aib->slsw.v3;
	return 0;
}

/*
 * Check if two ports are on the same network.
 */
static int etr_compare_network(struct etr_aib *aib1, struct etr_aib *aib2)
{
	// FIXME: any other fields we have to compare?
	return aib1->edf1.net_id == aib2->edf1.net_id;
}

/*
 * Wrapper for etr_stei that converts physical port states
 * to logical port states to be consistent with the output
 * of stetr (see etr_psc vs. etr_lpsc).
 */
static void etr_steai_cv(struct etr_aib *aib, unsigned int func)
{
	BUG_ON(etr_steai(aib, func) != 0);
	/* Convert port state to logical port state. */
	if (aib->esw.psc0 == 1)
		aib->esw.psc0 = 2;
	else if (aib->esw.psc0 == 0 && aib->esw.p == 0)
		aib->esw.psc0 = 1;
	if (aib->esw.psc1 == 1)
		aib->esw.psc1 = 2;
	else if (aib->esw.psc1 == 0 && aib->esw.p == 1)
		aib->esw.psc1 = 1;
}

/*
 * Check if the aib a2 is still connected to the same attachment as
 * aib a1, the etv values differ by one and a2 is valid.
 */
static int etr_aib_follows(struct etr_aib *a1, struct etr_aib *a2, int p)
{
	int state_a1, state_a2;

	/* Paranoia check: e0/e1 should better be the same. */
	if (a1->esw.eacr.e0 != a2->esw.eacr.e0 ||
	    a1->esw.eacr.e1 != a2->esw.eacr.e1)
		return 0;

	/* Still connected to the same etr ? */
	state_a1 = p ? a1->esw.psc1 : a1->esw.psc0;
	state_a2 = p ? a2->esw.psc1 : a2->esw.psc0;
	if (state_a1 == etr_lpsc_operational_step) {
		if (state_a2 != etr_lpsc_operational_step ||
		    a1->edf1.net_id != a2->edf1.net_id ||
		    a1->edf1.etr_id != a2->edf1.etr_id ||
		    a1->edf1.etr_pn != a2->edf1.etr_pn)
			return 0;
	} else if (state_a2 != etr_lpsc_pps_mode)
		return 0;

	/* The ETV value of a2 needs to be ETV of a1 + 1. */
	if (a1->edf2.etv + 1 != a2->edf2.etv)
		return 0;

	if (!etr_port_valid(a2, p))
		return 0;

	return 1;
}

M
Martin Schwidefsky 已提交
710
struct clock_sync_data {
711
	atomic_t cpus;
712 713
	int in_sync;
	unsigned long long fixup_cc;
714 715
	int etr_port;
	struct etr_aib *etr_aib;
M
Martin Schwidefsky 已提交
716
};
717

718
static void clock_sync_cpu(struct clock_sync_data *sync)
M
Martin Schwidefsky 已提交
719
{
720
	atomic_dec(&sync->cpus);
M
Martin Schwidefsky 已提交
721
	enable_sync_clock();
M
Martin Schwidefsky 已提交
722 723 724 725 726 727
	/*
	 * This looks like a busy wait loop but it isn't. etr_sync_cpus
	 * is called on all other cpus while the TOD clocks is stopped.
	 * __udelay will stop the cpu on an enabled wait psw until the
	 * TOD is running again.
	 */
M
Martin Schwidefsky 已提交
728
	while (sync->in_sync == 0) {
M
Martin Schwidefsky 已提交
729
		__udelay(1);
730 731 732 733 734 735
		/*
		 * A different cpu changes *in_sync. Therefore use
		 * barrier() to force memory access.
		 */
		barrier();
	}
M
Martin Schwidefsky 已提交
736
	if (sync->in_sync != 1)
M
Martin Schwidefsky 已提交
737
		/* Didn't work. Clear per-cpu in sync bit again. */
M
Martin Schwidefsky 已提交
738
		disable_sync_clock(NULL);
M
Martin Schwidefsky 已提交
739 740 741 742
	/*
	 * This round of TOD syncing is done. Set the clock comparator
	 * to the next tick and let the processor continue.
	 */
M
Martin Schwidefsky 已提交
743
	fixup_clock_comparator(sync->fixup_cc);
M
Martin Schwidefsky 已提交
744 745 746
}

/*
L
Lucas De Marchi 已提交
747
 * Sync the TOD clock using the port referred to by aibp. This port
M
Martin Schwidefsky 已提交
748 749 750
 * has to be enabled and the other port has to be disabled. The
 * last eacr update has to be more than 1.6 seconds in the past.
 */
751
static int etr_sync_clock(void *data)
M
Martin Schwidefsky 已提交
752
{
753
	static int first;
754
	unsigned long long clock, old_clock, delay, delta;
755 756 757
	struct clock_sync_data *etr_sync;
	struct etr_aib *sync_port, *aib;
	int port;
M
Martin Schwidefsky 已提交
758 759
	int rc;

760
	etr_sync = data;
M
Martin Schwidefsky 已提交
761

762 763 764 765 766 767 768 769 770 771 772 773 774
	if (xchg(&first, 1) == 1) {
		/* Slave */
		clock_sync_cpu(etr_sync);
		return 0;
	}

	/* Wait until all other cpus entered the sync function. */
	while (atomic_read(&etr_sync->cpus) != 0)
		cpu_relax();

	port = etr_sync->etr_port;
	aib = etr_sync->etr_aib;
	sync_port = (port == 0) ? &etr_port0 : &etr_port1;
M
Martin Schwidefsky 已提交
775
	enable_sync_clock();
M
Martin Schwidefsky 已提交
776 777 778 779 780

	/* Set clock to next OTE. */
	__ctl_set_bit(14, 21);
	__ctl_set_bit(0, 29);
	clock = ((unsigned long long) (aib->edf2.etv + 1)) << 32;
781 782
	old_clock = get_tod_clock();
	if (set_tod_clock(clock) == 0) {
M
Martin Schwidefsky 已提交
783 784 785 786 787 788 789
		__udelay(1);	/* Wait for the clock to start. */
		__ctl_clear_bit(0, 29);
		__ctl_clear_bit(14, 21);
		etr_stetr(aib);
		/* Adjust Linux timing variables. */
		delay = (unsigned long long)
			(aib->edf2.etv - sync_port->edf2.etv) << 32;
M
Martin Schwidefsky 已提交
790
		delta = adjust_time(old_clock, clock, delay);
791
		etr_sync->fixup_cc = delta;
792
		fixup_clock_comparator(delta);
M
Martin Schwidefsky 已提交
793 794 795
		/* Verify that the clock is properly set. */
		if (!etr_aib_follows(sync_port, aib, port)) {
			/* Didn't work. */
M
Martin Schwidefsky 已提交
796
			disable_sync_clock(NULL);
797
			etr_sync->in_sync = -EAGAIN;
M
Martin Schwidefsky 已提交
798 799
			rc = -EAGAIN;
		} else {
800
			etr_sync->in_sync = 1;
M
Martin Schwidefsky 已提交
801 802 803 804 805 806
			rc = 0;
		}
	} else {
		/* Could not set the clock ?!? */
		__ctl_clear_bit(0, 29);
		__ctl_clear_bit(14, 21);
M
Martin Schwidefsky 已提交
807
		disable_sync_clock(NULL);
808
		etr_sync->in_sync = -EAGAIN;
M
Martin Schwidefsky 已提交
809 810
		rc = -EAGAIN;
	}
811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832
	xchg(&first, 0);
	return rc;
}

static int etr_sync_clock_stop(struct etr_aib *aib, int port)
{
	struct clock_sync_data etr_sync;
	struct etr_aib *sync_port;
	int follows;
	int rc;

	/* Check if the current aib is adjacent to the sync port aib. */
	sync_port = (port == 0) ? &etr_port0 : &etr_port1;
	follows = etr_aib_follows(sync_port, aib, port);
	memcpy(sync_port, aib, sizeof(*aib));
	if (!follows)
		return -EAGAIN;
	memset(&etr_sync, 0, sizeof(etr_sync));
	etr_sync.etr_aib = aib;
	etr_sync.etr_port = port;
	get_online_cpus();
	atomic_set(&etr_sync.cpus, num_online_cpus() - 1);
833
	rc = stop_machine(etr_sync_clock, &etr_sync, cpu_online_mask);
834
	put_online_cpus();
M
Martin Schwidefsky 已提交
835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857
	return rc;
}

/*
 * Handle the immediate effects of the different events.
 * The port change event is used for online/offline changes.
 */
static struct etr_eacr etr_handle_events(struct etr_eacr eacr)
{
	if (test_and_clear_bit(ETR_EVENT_SYNC_CHECK, &etr_events))
		eacr.es = 0;
	if (test_and_clear_bit(ETR_EVENT_SWITCH_LOCAL, &etr_events))
		eacr.es = eacr.sl = 0;
	if (test_and_clear_bit(ETR_EVENT_PORT_ALERT, &etr_events))
		etr_port0_uptodate = etr_port1_uptodate = 0;

	if (test_and_clear_bit(ETR_EVENT_PORT0_CHANGE, &etr_events)) {
		if (eacr.e0)
			/*
			 * Port change of an enabled port. We have to
			 * assume that this can have caused an stepping
			 * port switch.
			 */
858
			etr_tolec = get_tod_clock();
M
Martin Schwidefsky 已提交
859 860 861 862 863 864 865 866 867 868 869 870
		eacr.p0 = etr_port0_online;
		if (!eacr.p0)
			eacr.e0 = 0;
		etr_port0_uptodate = 0;
	}
	if (test_and_clear_bit(ETR_EVENT_PORT1_CHANGE, &etr_events)) {
		if (eacr.e1)
			/*
			 * Port change of an enabled port. We have to
			 * assume that this can have caused an stepping
			 * port switch.
			 */
871
			etr_tolec = get_tod_clock();
M
Martin Schwidefsky 已提交
872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914
		eacr.p1 = etr_port1_online;
		if (!eacr.p1)
			eacr.e1 = 0;
		etr_port1_uptodate = 0;
	}
	clear_bit(ETR_EVENT_UPDATE, &etr_events);
	return eacr;
}

/*
 * Set up a timer that expires after the etr_tolec + 1.6 seconds if
 * one of the ports needs an update.
 */
static void etr_set_tolec_timeout(unsigned long long now)
{
	unsigned long micros;

	if ((!etr_eacr.p0 || etr_port0_uptodate) &&
	    (!etr_eacr.p1 || etr_port1_uptodate))
		return;
	micros = (now > etr_tolec) ? ((now - etr_tolec) >> 12) : 0;
	micros = (micros > 1600000) ? 0 : 1600000 - micros;
	mod_timer(&etr_timer, jiffies + (micros * HZ) / 1000000 + 1);
}

/*
 * Set up a time that expires after 1/2 second.
 */
static void etr_set_sync_timeout(void)
{
	mod_timer(&etr_timer, jiffies + HZ/2);
}

/*
 * Update the aib information for one or both ports.
 */
static struct etr_eacr etr_handle_update(struct etr_aib *aib,
					 struct etr_eacr eacr)
{
	/* With both ports disabled the aib information is useless. */
	if (!eacr.e0 && !eacr.e1)
		return eacr;

915
	/* Update port0 or port1 with aib stored in etr_work_fn. */
M
Martin Schwidefsky 已提交
916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935
	if (aib->esw.q == 0) {
		/* Information for port 0 stored. */
		if (eacr.p0 && !etr_port0_uptodate) {
			etr_port0 = *aib;
			if (etr_port0_online)
				etr_port0_uptodate = 1;
		}
	} else {
		/* Information for port 1 stored. */
		if (eacr.p1 && !etr_port1_uptodate) {
			etr_port1 = *aib;
			if (etr_port0_online)
				etr_port1_uptodate = 1;
		}
	}

	/*
	 * Do not try to get the alternate port aib if the clock
	 * is not in sync yet.
	 */
936
	if (!eacr.es || !check_sync_clock())
M
Martin Schwidefsky 已提交
937 938 939 940 941 942 943
		return eacr;

	/*
	 * If steai is available we can get the information about
	 * the other port immediately. If only stetr is available the
	 * data-port bit toggle has to be used.
	 */
M
Martin Schwidefsky 已提交
944
	if (etr_steai_available) {
M
Martin Schwidefsky 已提交
945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986
		if (eacr.p0 && !etr_port0_uptodate) {
			etr_steai_cv(&etr_port0, ETR_STEAI_PORT_0);
			etr_port0_uptodate = 1;
		}
		if (eacr.p1 && !etr_port1_uptodate) {
			etr_steai_cv(&etr_port1, ETR_STEAI_PORT_1);
			etr_port1_uptodate = 1;
		}
	} else {
		/*
		 * One port was updated above, if the other
		 * port is not uptodate toggle dp bit.
		 */
		if ((eacr.p0 && !etr_port0_uptodate) ||
		    (eacr.p1 && !etr_port1_uptodate))
			eacr.dp ^= 1;
		else
			eacr.dp = 0;
	}
	return eacr;
}

/*
 * Write new etr control register if it differs from the current one.
 * Return 1 if etr_tolec has been updated as well.
 */
static void etr_update_eacr(struct etr_eacr eacr)
{
	int dp_changed;

	if (memcmp(&etr_eacr, &eacr, sizeof(eacr)) == 0)
		/* No change, return. */
		return;
	/*
	 * The disable of an active port of the change of the data port
	 * bit can/will cause a change in the data port.
	 */
	dp_changed = etr_eacr.e0 > eacr.e0 || etr_eacr.e1 > eacr.e1 ||
		(etr_eacr.dp ^ eacr.dp) != 0;
	etr_eacr = eacr;
	etr_setr(&etr_eacr);
	if (dp_changed)
987
		etr_tolec = get_tod_clock();
M
Martin Schwidefsky 已提交
988 989 990
}

/*
991
 * ETR work. In this function you'll find the main logic. In
M
Martin Schwidefsky 已提交
992 993 994
 * particular this is the only function that calls etr_update_eacr(),
 * it "controls" the etr control register.
 */
995
static void etr_work_fn(struct work_struct *work)
M
Martin Schwidefsky 已提交
996 997 998 999 1000 1001
{
	unsigned long long now;
	struct etr_eacr eacr;
	struct etr_aib aib;
	int sync_port;

M
Martin Schwidefsky 已提交
1002 1003 1004
	/* prevent multiple execution. */
	mutex_lock(&etr_work_mutex);

M
Martin Schwidefsky 已提交
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
	/* Create working copy of etr_eacr. */
	eacr = etr_eacr;

	/* Check for the different events and their immediate effects. */
	eacr = etr_handle_events(eacr);

	/* Check if ETR is supposed to be active. */
	eacr.ea = eacr.p0 || eacr.p1;
	if (!eacr.ea) {
		/* Both ports offline. Reset everything. */
		eacr.dp = eacr.es = eacr.sl = 0;
1016
		on_each_cpu(disable_sync_clock, NULL, 1);
M
Martin Schwidefsky 已提交
1017 1018
		del_timer_sync(&etr_timer);
		etr_update_eacr(eacr);
M
Martin Schwidefsky 已提交
1019
		goto out_unlock;
M
Martin Schwidefsky 已提交
1020 1021 1022 1023 1024
	}

	/* Store aib to get the current ETR status word. */
	BUG_ON(etr_stetr(&aib) != 0);
	etr_port0.esw = etr_port1.esw = aib.esw;	/* Copy status word. */
1025
	now = get_tod_clock();
M
Martin Schwidefsky 已提交
1026 1027 1028 1029 1030 1031 1032 1033 1034

	/*
	 * Update the port information if the last stepping port change
	 * or data port change is older than 1.6 seconds.
	 */
	if (now >= etr_tolec + (1600000 << 12))
		eacr = etr_handle_update(&aib, eacr);

	/*
L
Lucas De Marchi 已提交
1035
	 * Select ports to enable. The preferred synchronization mode is PPS.
M
Martin Schwidefsky 已提交
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
	 * If a port can be enabled depends on a number of things:
	 * 1) The port needs to be online and uptodate. A port is not
	 *    disabled just because it is not uptodate, but it is only
	 *    enabled if it is uptodate.
	 * 2) The port needs to have the same mode (pps / etr).
	 * 3) The port needs to be usable -> etr_port_valid() == 1
	 * 4) To enable the second port the clock needs to be in sync.
	 * 5) If both ports are useable and are ETR ports, the network id
	 *    has to be the same.
	 * The eacr.sl bit is used to indicate etr mode vs. pps mode.
	 */
	if (eacr.p0 && aib.esw.psc0 == etr_lpsc_pps_mode) {
		eacr.sl = 0;
		eacr.e0 = 1;
		if (!etr_mode_is_pps(etr_eacr))
			eacr.es = 0;
		if (!eacr.es || !eacr.p1 || aib.esw.psc1 != etr_lpsc_pps_mode)
			eacr.e1 = 0;
		// FIXME: uptodate checks ?
		else if (etr_port0_uptodate && etr_port1_uptodate)
			eacr.e1 = 1;
		sync_port = (etr_port0_uptodate &&
			     etr_port_valid(&etr_port0, 0)) ? 0 : -1;
	} else if (eacr.p1 && aib.esw.psc1 == etr_lpsc_pps_mode) {
		eacr.sl = 0;
		eacr.e0 = 0;
		eacr.e1 = 1;
		if (!etr_mode_is_pps(etr_eacr))
			eacr.es = 0;
		sync_port = (etr_port1_uptodate &&
			     etr_port_valid(&etr_port1, 1)) ? 1 : -1;
	} else if (eacr.p0 && aib.esw.psc0 == etr_lpsc_operational_step) {
		eacr.sl = 1;
		eacr.e0 = 1;
		if (!etr_mode_is_etr(etr_eacr))
			eacr.es = 0;
		if (!eacr.es || !eacr.p1 ||
		    aib.esw.psc1 != etr_lpsc_operational_alt)
			eacr.e1 = 0;
		else if (etr_port0_uptodate && etr_port1_uptodate &&
			 etr_compare_network(&etr_port0, &etr_port1))
			eacr.e1 = 1;
		sync_port = (etr_port0_uptodate &&
			     etr_port_valid(&etr_port0, 0)) ? 0 : -1;
	} else if (eacr.p1 && aib.esw.psc1 == etr_lpsc_operational_step) {
		eacr.sl = 1;
		eacr.e0 = 0;
		eacr.e1 = 1;
		if (!etr_mode_is_etr(etr_eacr))
			eacr.es = 0;
		sync_port = (etr_port1_uptodate &&
			     etr_port_valid(&etr_port1, 1)) ? 1 : -1;
	} else {
		/* Both ports not usable. */
		eacr.es = eacr.sl = 0;
		sync_port = -1;
	}

	/*
	 * If the clock is in sync just update the eacr and return.
	 * If there is no valid sync port wait for a port update.
	 */
1098
	if ((eacr.es && check_sync_clock()) || sync_port < 0) {
M
Martin Schwidefsky 已提交
1099 1100
		etr_update_eacr(eacr);
		etr_set_tolec_timeout(now);
M
Martin Schwidefsky 已提交
1101
		goto out_unlock;
M
Martin Schwidefsky 已提交
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
	}

	/*
	 * Prepare control register for clock syncing
	 * (reset data port bit, set sync check control.
	 */
	eacr.dp = 0;
	eacr.es = 1;

	/*
	 * Update eacr and try to synchronize the clock. If the update
	 * of eacr caused a stepping port switch (or if we have to
L
Lucas De Marchi 已提交
1114
	 * assume that a stepping port switch has occurred) or the
M
Martin Schwidefsky 已提交
1115 1116 1117 1118 1119
	 * clock syncing failed, reset the sync check control bit
	 * and set up a timer to try again after 0.5 seconds
	 */
	etr_update_eacr(eacr);
	if (now < etr_tolec + (1600000 << 12) ||
1120
	    etr_sync_clock_stop(&aib, sync_port) != 0) {
M
Martin Schwidefsky 已提交
1121 1122 1123 1124 1125 1126
		/* Sync failed. Try again in 1/2 second. */
		eacr.es = 0;
		etr_update_eacr(eacr);
		etr_set_sync_timeout();
	} else
		etr_set_tolec_timeout(now);
M
Martin Schwidefsky 已提交
1127 1128
out_unlock:
	mutex_unlock(&etr_work_mutex);
M
Martin Schwidefsky 已提交
1129 1130 1131 1132 1133
}

/*
 * Sysfs interface functions
 */
1134 1135 1136
static struct bus_type etr_subsys = {
	.name		= "etr",
	.dev_name	= "etr",
M
Martin Schwidefsky 已提交
1137 1138
};

1139
static struct device etr_port0_dev = {
M
Martin Schwidefsky 已提交
1140
	.id	= 0,
1141
	.bus	= &etr_subsys,
M
Martin Schwidefsky 已提交
1142 1143
};

1144
static struct device etr_port1_dev = {
M
Martin Schwidefsky 已提交
1145
	.id	= 1,
1146
	.bus	= &etr_subsys,
M
Martin Schwidefsky 已提交
1147 1148 1149
};

/*
1150
 * ETR subsys attributes
M
Martin Schwidefsky 已提交
1151
 */
1152 1153
static ssize_t etr_stepping_port_show(struct device *dev,
					struct device_attribute *attr,
1154
					char *buf)
M
Martin Schwidefsky 已提交
1155 1156 1157 1158
{
	return sprintf(buf, "%i\n", etr_port0.esw.p);
}

1159
static DEVICE_ATTR(stepping_port, 0400, etr_stepping_port_show, NULL);
M
Martin Schwidefsky 已提交
1160

1161 1162
static ssize_t etr_stepping_mode_show(struct device *dev,
					struct device_attribute *attr,
1163
					char *buf)
M
Martin Schwidefsky 已提交
1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175
{
	char *mode_str;

	if (etr_mode_is_pps(etr_eacr))
		mode_str = "pps";
	else if (etr_mode_is_etr(etr_eacr))
		mode_str = "etr";
	else
		mode_str = "local";
	return sprintf(buf, "%s\n", mode_str);
}

1176
static DEVICE_ATTR(stepping_mode, 0400, etr_stepping_mode_show, NULL);
M
Martin Schwidefsky 已提交
1177 1178 1179 1180

/*
 * ETR port attributes
 */
1181
static inline struct etr_aib *etr_aib_from_dev(struct device *dev)
M
Martin Schwidefsky 已提交
1182 1183 1184 1185 1186 1187 1188
{
	if (dev == &etr_port0_dev)
		return etr_port0_online ? &etr_port0 : NULL;
	else
		return etr_port1_online ? &etr_port1 : NULL;
}

1189 1190
static ssize_t etr_online_show(struct device *dev,
				struct device_attribute *attr,
1191
				char *buf)
M
Martin Schwidefsky 已提交
1192 1193 1194 1195 1196 1197 1198
{
	unsigned int online;

	online = (dev == &etr_port0_dev) ? etr_port0_online : etr_port1_online;
	return sprintf(buf, "%i\n", online);
}

1199 1200
static ssize_t etr_online_store(struct device *dev,
				struct device_attribute *attr,
1201
				const char *buf, size_t count)
M
Martin Schwidefsky 已提交
1202 1203 1204 1205 1206 1207
{
	unsigned int value;

	value = simple_strtoul(buf, NULL, 0);
	if (value != 0 && value != 1)
		return -EINVAL;
M
Martin Schwidefsky 已提交
1208 1209
	if (!test_bit(CLOCK_SYNC_HAS_ETR, &clock_sync_flags))
		return -EOPNOTSUPP;
1210
	mutex_lock(&clock_sync_mutex);
M
Martin Schwidefsky 已提交
1211 1212
	if (dev == &etr_port0_dev) {
		if (etr_port0_online == value)
1213
			goto out;	/* Nothing to do. */
M
Martin Schwidefsky 已提交
1214
		etr_port0_online = value;
1215 1216 1217 1218
		if (etr_port0_online && etr_port1_online)
			set_bit(CLOCK_SYNC_ETR, &clock_sync_flags);
		else
			clear_bit(CLOCK_SYNC_ETR, &clock_sync_flags);
M
Martin Schwidefsky 已提交
1219
		set_bit(ETR_EVENT_PORT0_CHANGE, &etr_events);
1220
		queue_work(time_sync_wq, &etr_work);
M
Martin Schwidefsky 已提交
1221 1222
	} else {
		if (etr_port1_online == value)
1223
			goto out;	/* Nothing to do. */
M
Martin Schwidefsky 已提交
1224
		etr_port1_online = value;
1225 1226 1227 1228
		if (etr_port0_online && etr_port1_online)
			set_bit(CLOCK_SYNC_ETR, &clock_sync_flags);
		else
			clear_bit(CLOCK_SYNC_ETR, &clock_sync_flags);
M
Martin Schwidefsky 已提交
1229
		set_bit(ETR_EVENT_PORT1_CHANGE, &etr_events);
1230
		queue_work(time_sync_wq, &etr_work);
M
Martin Schwidefsky 已提交
1231
	}
1232 1233
out:
	mutex_unlock(&clock_sync_mutex);
M
Martin Schwidefsky 已提交
1234 1235 1236
	return count;
}

1237
static DEVICE_ATTR(online, 0600, etr_online_show, etr_online_store);
M
Martin Schwidefsky 已提交
1238

1239 1240
static ssize_t etr_stepping_control_show(struct device *dev,
					struct device_attribute *attr,
1241
					char *buf)
M
Martin Schwidefsky 已提交
1242 1243 1244 1245 1246
{
	return sprintf(buf, "%i\n", (dev == &etr_port0_dev) ?
		       etr_eacr.e0 : etr_eacr.e1);
}

1247
static DEVICE_ATTR(stepping_control, 0400, etr_stepping_control_show, NULL);
M
Martin Schwidefsky 已提交
1248

1249 1250
static ssize_t etr_mode_code_show(struct device *dev,
				struct device_attribute *attr, char *buf)
M
Martin Schwidefsky 已提交
1251 1252 1253 1254 1255 1256 1257 1258
{
	if (!etr_port0_online && !etr_port1_online)
		/* Status word is not uptodate if both ports are offline. */
		return -ENODATA;
	return sprintf(buf, "%i\n", (dev == &etr_port0_dev) ?
		       etr_port0.esw.psc0 : etr_port0.esw.psc1);
}

1259
static DEVICE_ATTR(state_code, 0400, etr_mode_code_show, NULL);
M
Martin Schwidefsky 已提交
1260

1261 1262
static ssize_t etr_untuned_show(struct device *dev,
				struct device_attribute *attr, char *buf)
M
Martin Schwidefsky 已提交
1263 1264 1265 1266 1267 1268 1269 1270
{
	struct etr_aib *aib = etr_aib_from_dev(dev);

	if (!aib || !aib->slsw.v1)
		return -ENODATA;
	return sprintf(buf, "%i\n", aib->edf1.u);
}

1271
static DEVICE_ATTR(untuned, 0400, etr_untuned_show, NULL);
M
Martin Schwidefsky 已提交
1272

1273 1274
static ssize_t etr_network_id_show(struct device *dev,
				struct device_attribute *attr, char *buf)
M
Martin Schwidefsky 已提交
1275 1276 1277 1278 1279 1280 1281 1282
{
	struct etr_aib *aib = etr_aib_from_dev(dev);

	if (!aib || !aib->slsw.v1)
		return -ENODATA;
	return sprintf(buf, "%i\n", aib->edf1.net_id);
}

1283
static DEVICE_ATTR(network, 0400, etr_network_id_show, NULL);
M
Martin Schwidefsky 已提交
1284

1285 1286
static ssize_t etr_id_show(struct device *dev,
			struct device_attribute *attr, char *buf)
M
Martin Schwidefsky 已提交
1287 1288 1289 1290 1291 1292 1293 1294
{
	struct etr_aib *aib = etr_aib_from_dev(dev);

	if (!aib || !aib->slsw.v1)
		return -ENODATA;
	return sprintf(buf, "%i\n", aib->edf1.etr_id);
}

1295
static DEVICE_ATTR(id, 0400, etr_id_show, NULL);
M
Martin Schwidefsky 已提交
1296

1297 1298
static ssize_t etr_port_number_show(struct device *dev,
			struct device_attribute *attr, char *buf)
M
Martin Schwidefsky 已提交
1299 1300 1301 1302 1303 1304 1305 1306
{
	struct etr_aib *aib = etr_aib_from_dev(dev);

	if (!aib || !aib->slsw.v1)
		return -ENODATA;
	return sprintf(buf, "%i\n", aib->edf1.etr_pn);
}

1307
static DEVICE_ATTR(port, 0400, etr_port_number_show, NULL);
M
Martin Schwidefsky 已提交
1308

1309 1310
static ssize_t etr_coupled_show(struct device *dev,
			struct device_attribute *attr, char *buf)
M
Martin Schwidefsky 已提交
1311 1312 1313 1314 1315 1316 1317 1318
{
	struct etr_aib *aib = etr_aib_from_dev(dev);

	if (!aib || !aib->slsw.v3)
		return -ENODATA;
	return sprintf(buf, "%i\n", aib->edf3.c);
}

1319
static DEVICE_ATTR(coupled, 0400, etr_coupled_show, NULL);
M
Martin Schwidefsky 已提交
1320

1321 1322
static ssize_t etr_local_time_show(struct device *dev,
			struct device_attribute *attr, char *buf)
M
Martin Schwidefsky 已提交
1323 1324 1325 1326 1327 1328 1329 1330
{
	struct etr_aib *aib = etr_aib_from_dev(dev);

	if (!aib || !aib->slsw.v3)
		return -ENODATA;
	return sprintf(buf, "%i\n", aib->edf3.blto);
}

1331
static DEVICE_ATTR(local_time, 0400, etr_local_time_show, NULL);
M
Martin Schwidefsky 已提交
1332

1333 1334
static ssize_t etr_utc_offset_show(struct device *dev,
			struct device_attribute *attr, char *buf)
M
Martin Schwidefsky 已提交
1335 1336 1337 1338 1339 1340 1341 1342
{
	struct etr_aib *aib = etr_aib_from_dev(dev);

	if (!aib || !aib->slsw.v3)
		return -ENODATA;
	return sprintf(buf, "%i\n", aib->edf3.buo);
}

1343
static DEVICE_ATTR(utc_offset, 0400, etr_utc_offset_show, NULL);
M
Martin Schwidefsky 已提交
1344

1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355
static struct device_attribute *etr_port_attributes[] = {
	&dev_attr_online,
	&dev_attr_stepping_control,
	&dev_attr_state_code,
	&dev_attr_untuned,
	&dev_attr_network,
	&dev_attr_id,
	&dev_attr_port,
	&dev_attr_coupled,
	&dev_attr_local_time,
	&dev_attr_utc_offset,
M
Martin Schwidefsky 已提交
1356 1357 1358
	NULL
};

1359
static int __init etr_register_port(struct device *dev)
M
Martin Schwidefsky 已提交
1360
{
1361
	struct device_attribute **attr;
M
Martin Schwidefsky 已提交
1362 1363
	int rc;

1364
	rc = device_register(dev);
M
Martin Schwidefsky 已提交
1365 1366 1367
	if (rc)
		goto out;
	for (attr = etr_port_attributes; *attr; attr++) {
1368
		rc = device_create_file(dev, *attr);
M
Martin Schwidefsky 已提交
1369 1370 1371 1372 1373 1374
		if (rc)
			goto out_unreg;
	}
	return 0;
out_unreg:
	for (; attr >= etr_port_attributes; attr--)
1375 1376
		device_remove_file(dev, *attr);
	device_unregister(dev);
M
Martin Schwidefsky 已提交
1377 1378 1379 1380
out:
	return rc;
}

1381
static void __init etr_unregister_port(struct device *dev)
M
Martin Schwidefsky 已提交
1382
{
1383
	struct device_attribute **attr;
M
Martin Schwidefsky 已提交
1384 1385

	for (attr = etr_port_attributes; *attr; attr++)
1386 1387
		device_remove_file(dev, *attr);
	device_unregister(dev);
M
Martin Schwidefsky 已提交
1388 1389 1390 1391 1392 1393
}

static int __init etr_init_sysfs(void)
{
	int rc;

1394
	rc = subsys_system_register(&etr_subsys, NULL);
M
Martin Schwidefsky 已提交
1395 1396
	if (rc)
		goto out;
1397
	rc = device_create_file(etr_subsys.dev_root, &dev_attr_stepping_port);
M
Martin Schwidefsky 已提交
1398
	if (rc)
1399 1400
		goto out_unreg_subsys;
	rc = device_create_file(etr_subsys.dev_root, &dev_attr_stepping_mode);
M
Martin Schwidefsky 已提交
1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413
	if (rc)
		goto out_remove_stepping_port;
	rc = etr_register_port(&etr_port0_dev);
	if (rc)
		goto out_remove_stepping_mode;
	rc = etr_register_port(&etr_port1_dev);
	if (rc)
		goto out_remove_port0;
	return 0;

out_remove_port0:
	etr_unregister_port(&etr_port0_dev);
out_remove_stepping_mode:
1414
	device_remove_file(etr_subsys.dev_root, &dev_attr_stepping_mode);
M
Martin Schwidefsky 已提交
1415
out_remove_stepping_port:
1416 1417 1418
	device_remove_file(etr_subsys.dev_root, &dev_attr_stepping_port);
out_unreg_subsys:
	bus_unregister(&etr_subsys);
M
Martin Schwidefsky 已提交
1419 1420
out:
	return rc;
L
Linus Torvalds 已提交
1421 1422
}

M
Martin Schwidefsky 已提交
1423
device_initcall(etr_init_sysfs);
M
Martin Schwidefsky 已提交
1424 1425 1426 1427 1428 1429 1430 1431 1432

/*
 * Server Time Protocol (STP) code.
 */
static int stp_online;
static struct stp_sstpi stp_info;
static void *stp_page;

static void stp_work_fn(struct work_struct *work);
M
Martin Schwidefsky 已提交
1433
static DEFINE_MUTEX(stp_work_mutex);
M
Martin Schwidefsky 已提交
1434
static DECLARE_WORK(stp_work, stp_work_fn);
1435
static struct timer_list stp_timer;
M
Martin Schwidefsky 已提交
1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449

static int __init early_parse_stp(char *p)
{
	if (strncmp(p, "off", 3) == 0)
		stp_online = 0;
	else if (strncmp(p, "on", 2) == 0)
		stp_online = 1;
	return 0;
}
early_param("stp", early_parse_stp);

/*
 * Reset STP attachment.
 */
1450
static void __init stp_reset(void)
M
Martin Schwidefsky 已提交
1451 1452 1453
{
	int rc;

1454
	stp_page = (void *) get_zeroed_page(GFP_ATOMIC);
M
Martin Schwidefsky 已提交
1455
	rc = chsc_sstpc(stp_page, STP_OP_CTRL, 0x0000);
1456
	if (rc == 0)
M
Martin Schwidefsky 已提交
1457 1458
		set_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags);
	else if (stp_online) {
1459 1460
		pr_warning("The real or virtual hardware system does "
			   "not provide an STP interface\n");
1461
		free_page((unsigned long) stp_page);
M
Martin Schwidefsky 已提交
1462 1463 1464 1465 1466
		stp_page = NULL;
		stp_online = 0;
	}
}

1467 1468 1469 1470 1471
static void stp_timeout(unsigned long dummy)
{
	queue_work(time_sync_wq, &stp_work);
}

M
Martin Schwidefsky 已提交
1472 1473
static int __init stp_init(void)
{
1474 1475
	if (!test_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags))
		return 0;
1476
	setup_timer(&stp_timer, stp_timeout, 0UL);
1477 1478 1479 1480
	time_init_wq();
	if (!stp_online)
		return 0;
	queue_work(time_sync_wq, &stp_work);
M
Martin Schwidefsky 已提交
1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496
	return 0;
}

arch_initcall(stp_init);

/*
 * STP timing alert. There are three causes:
 * 1) timing status change
 * 2) link availability change
 * 3) time control parameter change
 * In all three cases we are only interested in the clock source state.
 * If a STP clock source is now available use it.
 */
static void stp_timing_alert(struct stp_irq_parm *intparm)
{
	if (intparm->tsc || intparm->lac || intparm->tcpc)
1497
		queue_work(time_sync_wq, &stp_work);
M
Martin Schwidefsky 已提交
1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508
}

/*
 * STP sync check machine check. This is called when the timing state
 * changes from the synchronized state to the unsynchronized state.
 * After a STP sync check the clock is not in sync. The machine check
 * is broadcasted to all cpus at the same time.
 */
void stp_sync_check(void)
{
	disable_sync_clock(NULL);
1509
	queue_work(time_sync_wq, &stp_work);
M
Martin Schwidefsky 已提交
1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520
}

/*
 * STP island condition machine check. This is called when an attached
 * server  attempts to communicate over an STP link and the servers
 * have matching CTN ids and have a valid stratum-1 configuration
 * but the configurations do not match.
 */
void stp_island_check(void)
{
	disable_sync_clock(NULL);
1521
	queue_work(time_sync_wq, &stp_work);
M
Martin Schwidefsky 已提交
1522 1523
}

1524 1525

static int stp_sync_clock(void *data)
M
Martin Schwidefsky 已提交
1526
{
1527
	static int first;
M
Martin Schwidefsky 已提交
1528
	unsigned long long old_clock, delta;
1529
	struct clock_sync_data *stp_sync;
M
Martin Schwidefsky 已提交
1530 1531
	int rc;

1532
	stp_sync = data;
M
Martin Schwidefsky 已提交
1533

1534 1535 1536 1537 1538
	if (xchg(&first, 1) == 1) {
		/* Slave */
		clock_sync_cpu(stp_sync);
		return 0;
	}
M
Martin Schwidefsky 已提交
1539

1540 1541 1542
	/* Wait until all other cpus entered the sync function. */
	while (atomic_read(&stp_sync->cpus) != 0)
		cpu_relax();
M
Martin Schwidefsky 已提交
1543 1544 1545 1546 1547 1548 1549

	enable_sync_clock();

	rc = 0;
	if (stp_info.todoff[0] || stp_info.todoff[1] ||
	    stp_info.todoff[2] || stp_info.todoff[3] ||
	    stp_info.tmd != 2) {
1550
		old_clock = get_tod_clock();
M
Martin Schwidefsky 已提交
1551 1552
		rc = chsc_sstpc(stp_page, STP_OP_SYNC, 0);
		if (rc == 0) {
1553
			delta = adjust_time(old_clock, get_tod_clock(), 0);
M
Martin Schwidefsky 已提交
1554 1555 1556 1557 1558 1559 1560 1561 1562
			fixup_clock_comparator(delta);
			rc = chsc_sstpi(stp_page, &stp_info,
					sizeof(struct stp_sstpi));
			if (rc == 0 && stp_info.tmd != 2)
				rc = -EAGAIN;
		}
	}
	if (rc) {
		disable_sync_clock(NULL);
1563
		stp_sync->in_sync = -EAGAIN;
M
Martin Schwidefsky 已提交
1564
	} else
1565 1566 1567 1568
		stp_sync->in_sync = 1;
	xchg(&first, 0);
	return 0;
}
M
Martin Schwidefsky 已提交
1569

1570 1571 1572 1573 1574 1575 1576 1577 1578
/*
 * STP work. Check for the STP state and take over the clock
 * synchronization if the STP clock source is usable.
 */
static void stp_work_fn(struct work_struct *work)
{
	struct clock_sync_data stp_sync;
	int rc;

M
Martin Schwidefsky 已提交
1579 1580 1581
	/* prevent multiple execution. */
	mutex_lock(&stp_work_mutex);

1582 1583
	if (!stp_online) {
		chsc_sstpc(stp_page, STP_OP_CTRL, 0x0000);
1584
		del_timer_sync(&stp_timer);
M
Martin Schwidefsky 已提交
1585
		goto out_unlock;
1586 1587 1588 1589
	}

	rc = chsc_sstpc(stp_page, STP_OP_CTRL, 0xb0e0);
	if (rc)
M
Martin Schwidefsky 已提交
1590
		goto out_unlock;
1591 1592 1593

	rc = chsc_sstpi(stp_page, &stp_info, sizeof(struct stp_sstpi));
	if (rc || stp_info.c == 0)
M
Martin Schwidefsky 已提交
1594
		goto out_unlock;
1595

1596 1597 1598 1599
	/* Skip synchronization if the clock is already in sync. */
	if (check_sync_clock())
		goto out_unlock;

1600 1601 1602
	memset(&stp_sync, 0, sizeof(stp_sync));
	get_online_cpus();
	atomic_set(&stp_sync.cpus, num_online_cpus() - 1);
1603
	stop_machine(stp_sync_clock, &stp_sync, cpu_online_mask);
1604
	put_online_cpus();
M
Martin Schwidefsky 已提交
1605

1606 1607 1608 1609 1610 1611 1612
	if (!check_sync_clock())
		/*
		 * There is a usable clock but the synchonization failed.
		 * Retry after a second.
		 */
		mod_timer(&stp_timer, jiffies + HZ);

M
Martin Schwidefsky 已提交
1613 1614
out_unlock:
	mutex_unlock(&stp_work_mutex);
M
Martin Schwidefsky 已提交
1615 1616 1617
}

/*
1618
 * STP subsys sysfs interface functions
M
Martin Schwidefsky 已提交
1619
 */
1620 1621 1622
static struct bus_type stp_subsys = {
	.name		= "stp",
	.dev_name	= "stp",
M
Martin Schwidefsky 已提交
1623 1624
};

1625 1626
static ssize_t stp_ctn_id_show(struct device *dev,
				struct device_attribute *attr,
1627
				char *buf)
M
Martin Schwidefsky 已提交
1628 1629 1630 1631 1632 1633 1634
{
	if (!stp_online)
		return -ENODATA;
	return sprintf(buf, "%016llx\n",
		       *(unsigned long long *) stp_info.ctnid);
}

1635
static DEVICE_ATTR(ctn_id, 0400, stp_ctn_id_show, NULL);
M
Martin Schwidefsky 已提交
1636

1637 1638
static ssize_t stp_ctn_type_show(struct device *dev,
				struct device_attribute *attr,
1639
				char *buf)
M
Martin Schwidefsky 已提交
1640 1641 1642 1643 1644 1645
{
	if (!stp_online)
		return -ENODATA;
	return sprintf(buf, "%i\n", stp_info.ctn);
}

1646
static DEVICE_ATTR(ctn_type, 0400, stp_ctn_type_show, NULL);
M
Martin Schwidefsky 已提交
1647

1648 1649
static ssize_t stp_dst_offset_show(struct device *dev,
				   struct device_attribute *attr,
1650
				   char *buf)
M
Martin Schwidefsky 已提交
1651 1652 1653 1654 1655 1656
{
	if (!stp_online || !(stp_info.vbits & 0x2000))
		return -ENODATA;
	return sprintf(buf, "%i\n", (int)(s16) stp_info.dsto);
}

1657
static DEVICE_ATTR(dst_offset, 0400, stp_dst_offset_show, NULL);
M
Martin Schwidefsky 已提交
1658

1659 1660
static ssize_t stp_leap_seconds_show(struct device *dev,
					struct device_attribute *attr,
1661
					char *buf)
M
Martin Schwidefsky 已提交
1662 1663 1664 1665 1666 1667
{
	if (!stp_online || !(stp_info.vbits & 0x8000))
		return -ENODATA;
	return sprintf(buf, "%i\n", (int)(s16) stp_info.leaps);
}

1668
static DEVICE_ATTR(leap_seconds, 0400, stp_leap_seconds_show, NULL);
M
Martin Schwidefsky 已提交
1669

1670 1671
static ssize_t stp_stratum_show(struct device *dev,
				struct device_attribute *attr,
1672
				char *buf)
M
Martin Schwidefsky 已提交
1673 1674 1675 1676 1677 1678
{
	if (!stp_online)
		return -ENODATA;
	return sprintf(buf, "%i\n", (int)(s16) stp_info.stratum);
}

1679
static DEVICE_ATTR(stratum, 0400, stp_stratum_show, NULL);
M
Martin Schwidefsky 已提交
1680

1681 1682
static ssize_t stp_time_offset_show(struct device *dev,
				struct device_attribute *attr,
1683
				char *buf)
M
Martin Schwidefsky 已提交
1684 1685 1686 1687 1688 1689
{
	if (!stp_online || !(stp_info.vbits & 0x0800))
		return -ENODATA;
	return sprintf(buf, "%i\n", (int) stp_info.tto);
}

1690
static DEVICE_ATTR(time_offset, 0400, stp_time_offset_show, NULL);
M
Martin Schwidefsky 已提交
1691

1692 1693
static ssize_t stp_time_zone_offset_show(struct device *dev,
				struct device_attribute *attr,
1694
				char *buf)
M
Martin Schwidefsky 已提交
1695 1696 1697 1698 1699 1700
{
	if (!stp_online || !(stp_info.vbits & 0x4000))
		return -ENODATA;
	return sprintf(buf, "%i\n", (int)(s16) stp_info.tzo);
}

1701
static DEVICE_ATTR(time_zone_offset, 0400,
M
Martin Schwidefsky 已提交
1702 1703
			 stp_time_zone_offset_show, NULL);

1704 1705
static ssize_t stp_timing_mode_show(struct device *dev,
				struct device_attribute *attr,
1706
				char *buf)
M
Martin Schwidefsky 已提交
1707 1708 1709 1710 1711 1712
{
	if (!stp_online)
		return -ENODATA;
	return sprintf(buf, "%i\n", stp_info.tmd);
}

1713
static DEVICE_ATTR(timing_mode, 0400, stp_timing_mode_show, NULL);
M
Martin Schwidefsky 已提交
1714

1715 1716
static ssize_t stp_timing_state_show(struct device *dev,
				struct device_attribute *attr,
1717
				char *buf)
M
Martin Schwidefsky 已提交
1718 1719 1720 1721 1722 1723
{
	if (!stp_online)
		return -ENODATA;
	return sprintf(buf, "%i\n", stp_info.tst);
}

1724
static DEVICE_ATTR(timing_state, 0400, stp_timing_state_show, NULL);
M
Martin Schwidefsky 已提交
1725

1726 1727
static ssize_t stp_online_show(struct device *dev,
				struct device_attribute *attr,
1728
				char *buf)
M
Martin Schwidefsky 已提交
1729 1730 1731 1732
{
	return sprintf(buf, "%i\n", stp_online);
}

1733 1734
static ssize_t stp_online_store(struct device *dev,
				struct device_attribute *attr,
M
Martin Schwidefsky 已提交
1735 1736 1737 1738 1739 1740 1741 1742 1743
				const char *buf, size_t count)
{
	unsigned int value;

	value = simple_strtoul(buf, NULL, 0);
	if (value != 0 && value != 1)
		return -EINVAL;
	if (!test_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags))
		return -EOPNOTSUPP;
1744
	mutex_lock(&clock_sync_mutex);
M
Martin Schwidefsky 已提交
1745
	stp_online = value;
1746 1747 1748 1749
	if (stp_online)
		set_bit(CLOCK_SYNC_STP, &clock_sync_flags);
	else
		clear_bit(CLOCK_SYNC_STP, &clock_sync_flags);
1750
	queue_work(time_sync_wq, &stp_work);
1751
	mutex_unlock(&clock_sync_mutex);
M
Martin Schwidefsky 已提交
1752 1753 1754 1755
	return count;
}

/*
1756 1757
 * Can't use DEVICE_ATTR because the attribute should be named
 * stp/online but dev_attr_online already exists in this file ..
M
Martin Schwidefsky 已提交
1758
 */
1759
static struct device_attribute dev_attr_stp_online = {
M
Martin Schwidefsky 已提交
1760 1761 1762 1763 1764
	.attr = { .name = "online", .mode = 0600 },
	.show	= stp_online_show,
	.store	= stp_online_store,
};

1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775
static struct device_attribute *stp_attributes[] = {
	&dev_attr_ctn_id,
	&dev_attr_ctn_type,
	&dev_attr_dst_offset,
	&dev_attr_leap_seconds,
	&dev_attr_stp_online,
	&dev_attr_stratum,
	&dev_attr_time_offset,
	&dev_attr_time_zone_offset,
	&dev_attr_timing_mode,
	&dev_attr_timing_state,
M
Martin Schwidefsky 已提交
1776 1777 1778 1779 1780
	NULL
};

static int __init stp_init_sysfs(void)
{
1781
	struct device_attribute **attr;
M
Martin Schwidefsky 已提交
1782 1783
	int rc;

1784
	rc = subsys_system_register(&stp_subsys, NULL);
M
Martin Schwidefsky 已提交
1785 1786 1787
	if (rc)
		goto out;
	for (attr = stp_attributes; *attr; attr++) {
1788
		rc = device_create_file(stp_subsys.dev_root, *attr);
M
Martin Schwidefsky 已提交
1789 1790 1791 1792 1793 1794
		if (rc)
			goto out_unreg;
	}
	return 0;
out_unreg:
	for (; attr >= stp_attributes; attr--)
1795 1796
		device_remove_file(stp_subsys.dev_root, *attr);
	bus_unregister(&stp_subsys);
M
Martin Schwidefsky 已提交
1797 1798 1799 1800 1801
out:
	return rc;
}

device_initcall(stp_init_sysfs);