time.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410
  1. /*
  2. * arch/s390/kernel/time.c
  3. * Time of day based timer functions.
  4. *
  5. * S390 version
  6. * Copyright (C) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
  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. #include <linux/errno.h>
  15. #include <linux/module.h>
  16. #include <linux/sched.h>
  17. #include <linux/kernel.h>
  18. #include <linux/param.h>
  19. #include <linux/string.h>
  20. #include <linux/mm.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/time.h>
  23. #include <linux/delay.h>
  24. #include <linux/init.h>
  25. #include <linux/smp.h>
  26. #include <linux/types.h>
  27. #include <linux/profile.h>
  28. #include <linux/timex.h>
  29. #include <linux/notifier.h>
  30. #include <linux/clocksource.h>
  31. #include <asm/uaccess.h>
  32. #include <asm/delay.h>
  33. #include <asm/s390_ext.h>
  34. #include <asm/div64.h>
  35. #include <asm/irq.h>
  36. #include <asm/irq_regs.h>
  37. #include <asm/timer.h>
  38. #include <asm/etr.h>
  39. /* change this if you have some constant time drift */
  40. #define USECS_PER_JIFFY ((unsigned long) 1000000/HZ)
  41. #define CLK_TICKS_PER_JIFFY ((unsigned long) USECS_PER_JIFFY << 12)
  42. /* The value of the TOD clock for 1.1.1970. */
  43. #define TOD_UNIX_EPOCH 0x7d91048bca000000ULL
  44. /*
  45. * Create a small time difference between the timer interrupts
  46. * on the different cpus to avoid lock contention.
  47. */
  48. #define CPU_DEVIATION (smp_processor_id() << 12)
  49. #define TICK_SIZE tick
  50. static ext_int_info_t ext_int_info_cc;
  51. static ext_int_info_t ext_int_etr_cc;
  52. static u64 init_timer_cc;
  53. static u64 jiffies_timer_cc;
  54. static u64 xtime_cc;
  55. /*
  56. * Scheduler clock - returns current time in nanosec units.
  57. */
  58. unsigned long long sched_clock(void)
  59. {
  60. return ((get_clock() - jiffies_timer_cc) * 125) >> 9;
  61. }
  62. /*
  63. * Monotonic_clock - returns # of nanoseconds passed since time_init()
  64. */
  65. unsigned long long monotonic_clock(void)
  66. {
  67. return sched_clock();
  68. }
  69. EXPORT_SYMBOL(monotonic_clock);
  70. void tod_to_timeval(__u64 todval, struct timespec *xtime)
  71. {
  72. unsigned long long sec;
  73. sec = todval >> 12;
  74. do_div(sec, 1000000);
  75. xtime->tv_sec = sec;
  76. todval -= (sec * 1000000) << 12;
  77. xtime->tv_nsec = ((todval * 1000) >> 12);
  78. }
  79. #ifdef CONFIG_PROFILING
  80. #define s390_do_profile() profile_tick(CPU_PROFILING)
  81. #else
  82. #define s390_do_profile() do { ; } while(0)
  83. #endif /* CONFIG_PROFILING */
  84. /*
  85. * Advance the per cpu tick counter up to the time given with the
  86. * "time" argument. The per cpu update consists of accounting
  87. * the virtual cpu time, calling update_process_times and calling
  88. * the profiling hook. If xtime is before time it is advanced as well.
  89. */
  90. void account_ticks(u64 time)
  91. {
  92. __u32 ticks;
  93. __u64 tmp;
  94. /* Calculate how many ticks have passed. */
  95. if (time < S390_lowcore.jiffy_timer)
  96. return;
  97. tmp = time - S390_lowcore.jiffy_timer;
  98. if (tmp >= 2*CLK_TICKS_PER_JIFFY) { /* more than two ticks ? */
  99. ticks = __div(tmp, CLK_TICKS_PER_JIFFY) + 1;
  100. S390_lowcore.jiffy_timer +=
  101. CLK_TICKS_PER_JIFFY * (__u64) ticks;
  102. } else if (tmp >= CLK_TICKS_PER_JIFFY) {
  103. ticks = 2;
  104. S390_lowcore.jiffy_timer += 2*CLK_TICKS_PER_JIFFY;
  105. } else {
  106. ticks = 1;
  107. S390_lowcore.jiffy_timer += CLK_TICKS_PER_JIFFY;
  108. }
  109. #ifdef CONFIG_SMP
  110. /*
  111. * Do not rely on the boot cpu to do the calls to do_timer.
  112. * Spread it over all cpus instead.
  113. */
  114. write_seqlock(&xtime_lock);
  115. if (S390_lowcore.jiffy_timer > xtime_cc) {
  116. __u32 xticks;
  117. tmp = S390_lowcore.jiffy_timer - xtime_cc;
  118. if (tmp >= 2*CLK_TICKS_PER_JIFFY) {
  119. xticks = __div(tmp, CLK_TICKS_PER_JIFFY);
  120. xtime_cc += (__u64) xticks * CLK_TICKS_PER_JIFFY;
  121. } else {
  122. xticks = 1;
  123. xtime_cc += CLK_TICKS_PER_JIFFY;
  124. }
  125. do_timer(xticks);
  126. }
  127. write_sequnlock(&xtime_lock);
  128. #else
  129. do_timer(ticks);
  130. #endif
  131. #ifdef CONFIG_VIRT_CPU_ACCOUNTING
  132. account_tick_vtime(current);
  133. #else
  134. while (ticks--)
  135. update_process_times(user_mode(get_irq_regs()));
  136. #endif
  137. s390_do_profile();
  138. }
  139. #ifdef CONFIG_NO_IDLE_HZ
  140. #ifdef CONFIG_NO_IDLE_HZ_INIT
  141. int sysctl_hz_timer = 0;
  142. #else
  143. int sysctl_hz_timer = 1;
  144. #endif
  145. /*
  146. * Stop the HZ tick on the current CPU.
  147. * Only cpu_idle may call this function.
  148. */
  149. static void stop_hz_timer(void)
  150. {
  151. unsigned long flags;
  152. unsigned long seq, next;
  153. __u64 timer, todval;
  154. int cpu = smp_processor_id();
  155. if (sysctl_hz_timer != 0)
  156. return;
  157. cpu_set(cpu, nohz_cpu_mask);
  158. /*
  159. * Leave the clock comparator set up for the next timer
  160. * tick if either rcu or a softirq is pending.
  161. */
  162. if (rcu_needs_cpu(cpu) || local_softirq_pending()) {
  163. cpu_clear(cpu, nohz_cpu_mask);
  164. return;
  165. }
  166. /*
  167. * This cpu is going really idle. Set up the clock comparator
  168. * for the next event.
  169. */
  170. next = next_timer_interrupt();
  171. do {
  172. seq = read_seqbegin_irqsave(&xtime_lock, flags);
  173. timer = ((__u64) next) - ((__u64) jiffies) + jiffies_64;
  174. } while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
  175. todval = -1ULL;
  176. /* Be careful about overflows. */
  177. if (timer < (-1ULL / CLK_TICKS_PER_JIFFY)) {
  178. timer = jiffies_timer_cc + timer * CLK_TICKS_PER_JIFFY;
  179. if (timer >= jiffies_timer_cc)
  180. todval = timer;
  181. }
  182. set_clock_comparator(todval);
  183. }
  184. /*
  185. * Start the HZ tick on the current CPU.
  186. * Only cpu_idle may call this function.
  187. */
  188. static void start_hz_timer(void)
  189. {
  190. BUG_ON(!in_interrupt());
  191. if (!cpu_isset(smp_processor_id(), nohz_cpu_mask))
  192. return;
  193. account_ticks(get_clock());
  194. set_clock_comparator(S390_lowcore.jiffy_timer + CPU_DEVIATION);
  195. cpu_clear(smp_processor_id(), nohz_cpu_mask);
  196. }
  197. static int nohz_idle_notify(struct notifier_block *self,
  198. unsigned long action, void *hcpu)
  199. {
  200. switch (action) {
  201. case CPU_IDLE:
  202. stop_hz_timer();
  203. break;
  204. case CPU_NOT_IDLE:
  205. start_hz_timer();
  206. break;
  207. }
  208. return NOTIFY_OK;
  209. }
  210. static struct notifier_block nohz_idle_nb = {
  211. .notifier_call = nohz_idle_notify,
  212. };
  213. static void __init nohz_init(void)
  214. {
  215. if (register_idle_notifier(&nohz_idle_nb))
  216. panic("Couldn't register idle notifier");
  217. }
  218. #endif
  219. /*
  220. * Set up per cpu jiffy timer and set the clock comparator.
  221. */
  222. static void setup_jiffy_timer(void)
  223. {
  224. /* Set up clock comparator to next jiffy. */
  225. S390_lowcore.jiffy_timer =
  226. jiffies_timer_cc + (jiffies_64 + 1) * CLK_TICKS_PER_JIFFY;
  227. set_clock_comparator(S390_lowcore.jiffy_timer + CPU_DEVIATION);
  228. }
  229. /*
  230. * Set up lowcore and control register of the current cpu to
  231. * enable TOD clock and clock comparator interrupts.
  232. */
  233. void init_cpu_timer(void)
  234. {
  235. setup_jiffy_timer();
  236. /* Enable clock comparator timer interrupt. */
  237. __ctl_set_bit(0,11);
  238. /* Always allow ETR external interrupts, even without an ETR. */
  239. __ctl_set_bit(0, 4);
  240. }
  241. static void clock_comparator_interrupt(__u16 code)
  242. {
  243. /* set clock comparator for next tick */
  244. set_clock_comparator(S390_lowcore.jiffy_timer + CPU_DEVIATION);
  245. }
  246. static void etr_reset(void);
  247. static void etr_init(void);
  248. static void etr_ext_handler(__u16);
  249. /*
  250. * Get the TOD clock running.
  251. */
  252. static u64 __init reset_tod_clock(void)
  253. {
  254. u64 time;
  255. etr_reset();
  256. if (store_clock(&time) == 0)
  257. return time;
  258. /* TOD clock not running. Set the clock to Unix Epoch. */
  259. if (set_clock(TOD_UNIX_EPOCH) != 0 || store_clock(&time) != 0)
  260. panic("TOD clock not operational.");
  261. return TOD_UNIX_EPOCH;
  262. }
  263. static cycle_t read_tod_clock(void)
  264. {
  265. return get_clock();
  266. }
  267. static struct clocksource clocksource_tod = {
  268. .name = "tod",
  269. .rating = 100,
  270. .read = read_tod_clock,
  271. .mask = -1ULL,
  272. .mult = 1000,
  273. .shift = 12,
  274. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  275. };
  276. /*
  277. * Initialize the TOD clock and the CPU timer of
  278. * the boot cpu.
  279. */
  280. void __init time_init(void)
  281. {
  282. init_timer_cc = reset_tod_clock();
  283. xtime_cc = init_timer_cc + CLK_TICKS_PER_JIFFY;
  284. jiffies_timer_cc = init_timer_cc - jiffies_64 * CLK_TICKS_PER_JIFFY;
  285. /* set xtime */
  286. tod_to_timeval(init_timer_cc - TOD_UNIX_EPOCH, &xtime);
  287. set_normalized_timespec(&wall_to_monotonic,
  288. -xtime.tv_sec, -xtime.tv_nsec);
  289. /* request the clock comparator external interrupt */
  290. if (register_early_external_interrupt(0x1004,
  291. clock_comparator_interrupt,
  292. &ext_int_info_cc) != 0)
  293. panic("Couldn't request external interrupt 0x1004");
  294. if (clocksource_register(&clocksource_tod) != 0)
  295. panic("Could not register TOD clock source");
  296. /* request the etr external interrupt */
  297. if (register_early_external_interrupt(0x1406, etr_ext_handler,
  298. &ext_int_etr_cc) != 0)
  299. panic("Couldn't request external interrupt 0x1406");
  300. /* Enable TOD clock interrupts on the boot cpu. */
  301. init_cpu_timer();
  302. #ifdef CONFIG_NO_IDLE_HZ
  303. nohz_init();
  304. #endif
  305. #ifdef CONFIG_VIRT_TIMER
  306. vtime_init();
  307. #endif
  308. etr_init();
  309. }
  310. /*
  311. * External Time Reference (ETR) code.
  312. */
  313. static int etr_port0_online;
  314. static int etr_port1_online;
  315. static int __init early_parse_etr(char *p)
  316. {
  317. if (strncmp(p, "off", 3) == 0)
  318. etr_port0_online = etr_port1_online = 0;
  319. else if (strncmp(p, "port0", 5) == 0)
  320. etr_port0_online = 1;
  321. else if (strncmp(p, "port1", 5) == 0)
  322. etr_port1_online = 1;
  323. else if (strncmp(p, "on", 2) == 0)
  324. etr_port0_online = etr_port1_online = 1;
  325. return 0;
  326. }
  327. early_param("etr", early_parse_etr);
  328. enum etr_event {
  329. ETR_EVENT_PORT0_CHANGE,
  330. ETR_EVENT_PORT1_CHANGE,
  331. ETR_EVENT_PORT_ALERT,
  332. ETR_EVENT_SYNC_CHECK,
  333. ETR_EVENT_SWITCH_LOCAL,
  334. ETR_EVENT_UPDATE,
  335. };
  336. enum etr_flags {
  337. ETR_FLAG_ENOSYS,
  338. ETR_FLAG_EACCES,
  339. ETR_FLAG_STEAI,
  340. };
  341. /*
  342. * Valid bit combinations of the eacr register are (x = don't care):
  343. * e0 e1 dp p0 p1 ea es sl
  344. * 0 0 x 0 0 0 0 0 initial, disabled state
  345. * 0 0 x 0 1 1 0 0 port 1 online
  346. * 0 0 x 1 0 1 0 0 port 0 online
  347. * 0 0 x 1 1 1 0 0 both ports online
  348. * 0 1 x 0 1 1 0 0 port 1 online and usable, ETR or PPS mode
  349. * 0 1 x 0 1 1 0 1 port 1 online, usable and ETR mode
  350. * 0 1 x 0 1 1 1 0 port 1 online, usable, PPS mode, in-sync
  351. * 0 1 x 0 1 1 1 1 port 1 online, usable, ETR mode, in-sync
  352. * 0 1 x 1 1 1 0 0 both ports online, port 1 usable
  353. * 0 1 x 1 1 1 1 0 both ports online, port 1 usable, PPS mode, in-sync
  354. * 0 1 x 1 1 1 1 1 both ports online, port 1 usable, ETR mode, in-sync
  355. * 1 0 x 1 0 1 0 0 port 0 online and usable, ETR or PPS mode
  356. * 1 0 x 1 0 1 0 1 port 0 online, usable and ETR mode
  357. * 1 0 x 1 0 1 1 0 port 0 online, usable, PPS mode, in-sync
  358. * 1 0 x 1 0 1 1 1 port 0 online, usable, ETR mode, in-sync
  359. * 1 0 x 1 1 1 0 0 both ports online, port 0 usable
  360. * 1 0 x 1 1 1 1 0 both ports online, port 0 usable, PPS mode, in-sync
  361. * 1 0 x 1 1 1 1 1 both ports online, port 0 usable, ETR mode, in-sync
  362. * 1 1 x 1 1 1 1 0 both ports online & usable, ETR, in-sync
  363. * 1 1 x 1 1 1 1 1 both ports online & usable, ETR, in-sync
  364. */
  365. static struct etr_eacr etr_eacr;
  366. static u64 etr_tolec; /* time of last eacr update */
  367. static unsigned long etr_flags;
  368. static struct etr_aib etr_port0;
  369. static int etr_port0_uptodate;
  370. static struct etr_aib etr_port1;
  371. static int etr_port1_uptodate;
  372. static unsigned long etr_events;
  373. static struct timer_list etr_timer;
  374. static struct tasklet_struct etr_tasklet;
  375. static DEFINE_PER_CPU(atomic_t, etr_sync_word);
  376. static void etr_timeout(unsigned long dummy);
  377. static void etr_tasklet_fn(unsigned long dummy);
  378. /*
  379. * The etr get_clock function. It will write the current clock value
  380. * to the clock pointer and return 0 if the clock is in sync with the
  381. * external time source. If the clock mode is local it will return
  382. * -ENOSYS and -EAGAIN if the clock is not in sync with the external
  383. * reference. This function is what ETR is all about..
  384. */
  385. int get_sync_clock(unsigned long long *clock)
  386. {
  387. atomic_t *sw_ptr;
  388. unsigned int sw0, sw1;
  389. sw_ptr = &get_cpu_var(etr_sync_word);
  390. sw0 = atomic_read(sw_ptr);
  391. *clock = get_clock();
  392. sw1 = atomic_read(sw_ptr);
  393. put_cpu_var(etr_sync_sync);
  394. if (sw0 == sw1 && (sw0 & 0x80000000U))
  395. /* Success: time is in sync. */
  396. return 0;
  397. if (test_bit(ETR_FLAG_ENOSYS, &etr_flags))
  398. return -ENOSYS;
  399. if (test_bit(ETR_FLAG_EACCES, &etr_flags))
  400. return -EACCES;
  401. return -EAGAIN;
  402. }
  403. EXPORT_SYMBOL(get_sync_clock);
  404. /*
  405. * Make get_sync_clock return -EAGAIN.
  406. */
  407. static void etr_disable_sync_clock(void *dummy)
  408. {
  409. atomic_t *sw_ptr = &__get_cpu_var(etr_sync_word);
  410. /*
  411. * Clear the in-sync bit 2^31. All get_sync_clock calls will
  412. * fail until the sync bit is turned back on. In addition
  413. * increase the "sequence" counter to avoid the race of an
  414. * etr event and the complete recovery against get_sync_clock.
  415. */
  416. atomic_clear_mask(0x80000000, sw_ptr);
  417. atomic_inc(sw_ptr);
  418. }
  419. /*
  420. * Make get_sync_clock return 0 again.
  421. * Needs to be called from a context disabled for preemption.
  422. */
  423. static void etr_enable_sync_clock(void)
  424. {
  425. atomic_t *sw_ptr = &__get_cpu_var(etr_sync_word);
  426. atomic_set_mask(0x80000000, sw_ptr);
  427. }
  428. /*
  429. * Reset ETR attachment.
  430. */
  431. static void etr_reset(void)
  432. {
  433. etr_eacr = (struct etr_eacr) {
  434. .e0 = 0, .e1 = 0, ._pad0 = 4, .dp = 0,
  435. .p0 = 0, .p1 = 0, ._pad1 = 0, .ea = 0,
  436. .es = 0, .sl = 0 };
  437. if (etr_setr(&etr_eacr) == 0)
  438. etr_tolec = get_clock();
  439. else {
  440. set_bit(ETR_FLAG_ENOSYS, &etr_flags);
  441. if (etr_port0_online || etr_port1_online) {
  442. printk(KERN_WARNING "Running on non ETR capable "
  443. "machine, only local mode available.\n");
  444. etr_port0_online = etr_port1_online = 0;
  445. }
  446. }
  447. }
  448. static void etr_init(void)
  449. {
  450. struct etr_aib aib;
  451. if (test_bit(ETR_FLAG_ENOSYS, &etr_flags))
  452. return;
  453. /* Check if this machine has the steai instruction. */
  454. if (etr_steai(&aib, ETR_STEAI_STEPPING_PORT) == 0)
  455. set_bit(ETR_FLAG_STEAI, &etr_flags);
  456. setup_timer(&etr_timer, etr_timeout, 0UL);
  457. tasklet_init(&etr_tasklet, etr_tasklet_fn, 0);
  458. if (!etr_port0_online && !etr_port1_online)
  459. set_bit(ETR_FLAG_EACCES, &etr_flags);
  460. if (etr_port0_online) {
  461. set_bit(ETR_EVENT_PORT0_CHANGE, &etr_events);
  462. tasklet_hi_schedule(&etr_tasklet);
  463. }
  464. if (etr_port1_online) {
  465. set_bit(ETR_EVENT_PORT1_CHANGE, &etr_events);
  466. tasklet_hi_schedule(&etr_tasklet);
  467. }
  468. }
  469. /*
  470. * Two sorts of ETR machine checks. The architecture reads:
  471. * "When a machine-check niterruption occurs and if a switch-to-local or
  472. * ETR-sync-check interrupt request is pending but disabled, this pending
  473. * disabled interruption request is indicated and is cleared".
  474. * Which means that we can get etr_switch_to_local events from the machine
  475. * check handler although the interruption condition is disabled. Lovely..
  476. */
  477. /*
  478. * Switch to local machine check. This is called when the last usable
  479. * ETR port goes inactive. After switch to local the clock is not in sync.
  480. */
  481. void etr_switch_to_local(void)
  482. {
  483. if (!etr_eacr.sl)
  484. return;
  485. etr_disable_sync_clock(NULL);
  486. set_bit(ETR_EVENT_SWITCH_LOCAL, &etr_events);
  487. tasklet_hi_schedule(&etr_tasklet);
  488. }
  489. /*
  490. * ETR sync check machine check. This is called when the ETR OTE and the
  491. * local clock OTE are farther apart than the ETR sync check tolerance.
  492. * After a ETR sync check the clock is not in sync. The machine check
  493. * is broadcasted to all cpus at the same time.
  494. */
  495. void etr_sync_check(void)
  496. {
  497. if (!etr_eacr.es)
  498. return;
  499. etr_disable_sync_clock(NULL);
  500. set_bit(ETR_EVENT_SYNC_CHECK, &etr_events);
  501. tasklet_hi_schedule(&etr_tasklet);
  502. }
  503. /*
  504. * ETR external interrupt. There are two causes:
  505. * 1) port state change, check the usability of the port
  506. * 2) port alert, one of the ETR-data-validity bits (v1-v2 bits of the
  507. * sldr-status word) or ETR-data word 1 (edf1) or ETR-data word 3 (edf3)
  508. * or ETR-data word 4 (edf4) has changed.
  509. */
  510. static void etr_ext_handler(__u16 code)
  511. {
  512. struct etr_interruption_parameter *intparm =
  513. (struct etr_interruption_parameter *) &S390_lowcore.ext_params;
  514. if (intparm->pc0)
  515. /* ETR port 0 state change. */
  516. set_bit(ETR_EVENT_PORT0_CHANGE, &etr_events);
  517. if (intparm->pc1)
  518. /* ETR port 1 state change. */
  519. set_bit(ETR_EVENT_PORT1_CHANGE, &etr_events);
  520. if (intparm->eai)
  521. /*
  522. * ETR port alert on either port 0, 1 or both.
  523. * Both ports are not up-to-date now.
  524. */
  525. set_bit(ETR_EVENT_PORT_ALERT, &etr_events);
  526. tasklet_hi_schedule(&etr_tasklet);
  527. }
  528. static void etr_timeout(unsigned long dummy)
  529. {
  530. set_bit(ETR_EVENT_UPDATE, &etr_events);
  531. tasklet_hi_schedule(&etr_tasklet);
  532. }
  533. /*
  534. * Check if the etr mode is pss.
  535. */
  536. static inline int etr_mode_is_pps(struct etr_eacr eacr)
  537. {
  538. return eacr.es && !eacr.sl;
  539. }
  540. /*
  541. * Check if the etr mode is etr.
  542. */
  543. static inline int etr_mode_is_etr(struct etr_eacr eacr)
  544. {
  545. return eacr.es && eacr.sl;
  546. }
  547. /*
  548. * Check if the port can be used for TOD synchronization.
  549. * For PPS mode the port has to receive OTEs. For ETR mode
  550. * the port has to receive OTEs, the ETR stepping bit has to
  551. * be zero and the validity bits for data frame 1, 2, and 3
  552. * have to be 1.
  553. */
  554. static int etr_port_valid(struct etr_aib *aib, int port)
  555. {
  556. unsigned int psc;
  557. /* Check that this port is receiving OTEs. */
  558. if (aib->tsp == 0)
  559. return 0;
  560. psc = port ? aib->esw.psc1 : aib->esw.psc0;
  561. if (psc == etr_lpsc_pps_mode)
  562. return 1;
  563. if (psc == etr_lpsc_operational_step)
  564. return !aib->esw.y && aib->slsw.v1 &&
  565. aib->slsw.v2 && aib->slsw.v3;
  566. return 0;
  567. }
  568. /*
  569. * Check if two ports are on the same network.
  570. */
  571. static int etr_compare_network(struct etr_aib *aib1, struct etr_aib *aib2)
  572. {
  573. // FIXME: any other fields we have to compare?
  574. return aib1->edf1.net_id == aib2->edf1.net_id;
  575. }
  576. /*
  577. * Wrapper for etr_stei that converts physical port states
  578. * to logical port states to be consistent with the output
  579. * of stetr (see etr_psc vs. etr_lpsc).
  580. */
  581. static void etr_steai_cv(struct etr_aib *aib, unsigned int func)
  582. {
  583. BUG_ON(etr_steai(aib, func) != 0);
  584. /* Convert port state to logical port state. */
  585. if (aib->esw.psc0 == 1)
  586. aib->esw.psc0 = 2;
  587. else if (aib->esw.psc0 == 0 && aib->esw.p == 0)
  588. aib->esw.psc0 = 1;
  589. if (aib->esw.psc1 == 1)
  590. aib->esw.psc1 = 2;
  591. else if (aib->esw.psc1 == 0 && aib->esw.p == 1)
  592. aib->esw.psc1 = 1;
  593. }
  594. /*
  595. * Check if the aib a2 is still connected to the same attachment as
  596. * aib a1, the etv values differ by one and a2 is valid.
  597. */
  598. static int etr_aib_follows(struct etr_aib *a1, struct etr_aib *a2, int p)
  599. {
  600. int state_a1, state_a2;
  601. /* Paranoia check: e0/e1 should better be the same. */
  602. if (a1->esw.eacr.e0 != a2->esw.eacr.e0 ||
  603. a1->esw.eacr.e1 != a2->esw.eacr.e1)
  604. return 0;
  605. /* Still connected to the same etr ? */
  606. state_a1 = p ? a1->esw.psc1 : a1->esw.psc0;
  607. state_a2 = p ? a2->esw.psc1 : a2->esw.psc0;
  608. if (state_a1 == etr_lpsc_operational_step) {
  609. if (state_a2 != etr_lpsc_operational_step ||
  610. a1->edf1.net_id != a2->edf1.net_id ||
  611. a1->edf1.etr_id != a2->edf1.etr_id ||
  612. a1->edf1.etr_pn != a2->edf1.etr_pn)
  613. return 0;
  614. } else if (state_a2 != etr_lpsc_pps_mode)
  615. return 0;
  616. /* The ETV value of a2 needs to be ETV of a1 + 1. */
  617. if (a1->edf2.etv + 1 != a2->edf2.etv)
  618. return 0;
  619. if (!etr_port_valid(a2, p))
  620. return 0;
  621. return 1;
  622. }
  623. /*
  624. * The time is "clock". xtime is what we think the time is.
  625. * Adjust the value by a multiple of jiffies and add the delta to ntp.
  626. * "delay" is an approximation how long the synchronization took. If
  627. * the time correction is positive, then "delay" is subtracted from
  628. * the time difference and only the remaining part is passed to ntp.
  629. */
  630. static void etr_adjust_time(unsigned long long clock, unsigned long long delay)
  631. {
  632. unsigned long long delta, ticks;
  633. struct timex adjust;
  634. /*
  635. * We don't have to take the xtime lock because the cpu
  636. * executing etr_adjust_time is running disabled in
  637. * tasklet context and all other cpus are looping in
  638. * etr_sync_cpu_start.
  639. */
  640. if (clock > xtime_cc) {
  641. /* It is later than we thought. */
  642. delta = ticks = clock - xtime_cc;
  643. delta = ticks = (delta < delay) ? 0 : delta - delay;
  644. delta -= do_div(ticks, CLK_TICKS_PER_JIFFY);
  645. init_timer_cc = init_timer_cc + delta;
  646. jiffies_timer_cc = jiffies_timer_cc + delta;
  647. xtime_cc = xtime_cc + delta;
  648. adjust.offset = ticks * (1000000 / HZ);
  649. } else {
  650. /* It is earlier than we thought. */
  651. delta = ticks = xtime_cc - clock;
  652. delta -= do_div(ticks, CLK_TICKS_PER_JIFFY);
  653. init_timer_cc = init_timer_cc - delta;
  654. jiffies_timer_cc = jiffies_timer_cc - delta;
  655. xtime_cc = xtime_cc - delta;
  656. adjust.offset = -ticks * (1000000 / HZ);
  657. }
  658. if (adjust.offset != 0) {
  659. printk(KERN_NOTICE "etr: time adjusted by %li micro-seconds\n",
  660. adjust.offset);
  661. adjust.modes = ADJ_OFFSET_SINGLESHOT;
  662. do_adjtimex(&adjust);
  663. }
  664. }
  665. #ifdef CONFIG_SMP
  666. static void etr_sync_cpu_start(void *dummy)
  667. {
  668. int *in_sync = dummy;
  669. etr_enable_sync_clock();
  670. /*
  671. * This looks like a busy wait loop but it isn't. etr_sync_cpus
  672. * is called on all other cpus while the TOD clocks is stopped.
  673. * __udelay will stop the cpu on an enabled wait psw until the
  674. * TOD is running again.
  675. */
  676. while (*in_sync == 0) {
  677. __udelay(1);
  678. /*
  679. * A different cpu changes *in_sync. Therefore use
  680. * barrier() to force memory access.
  681. */
  682. barrier();
  683. }
  684. if (*in_sync != 1)
  685. /* Didn't work. Clear per-cpu in sync bit again. */
  686. etr_disable_sync_clock(NULL);
  687. /*
  688. * This round of TOD syncing is done. Set the clock comparator
  689. * to the next tick and let the processor continue.
  690. */
  691. setup_jiffy_timer();
  692. }
  693. static void etr_sync_cpu_end(void *dummy)
  694. {
  695. }
  696. #endif /* CONFIG_SMP */
  697. /*
  698. * Sync the TOD clock using the port refered to by aibp. This port
  699. * has to be enabled and the other port has to be disabled. The
  700. * last eacr update has to be more than 1.6 seconds in the past.
  701. */
  702. static int etr_sync_clock(struct etr_aib *aib, int port)
  703. {
  704. struct etr_aib *sync_port;
  705. unsigned long long clock, delay;
  706. int in_sync, follows;
  707. int rc;
  708. /* Check if the current aib is adjacent to the sync port aib. */
  709. sync_port = (port == 0) ? &etr_port0 : &etr_port1;
  710. follows = etr_aib_follows(sync_port, aib, port);
  711. memcpy(sync_port, aib, sizeof(*aib));
  712. if (!follows)
  713. return -EAGAIN;
  714. /*
  715. * Catch all other cpus and make them wait until we have
  716. * successfully synced the clock. smp_call_function will
  717. * return after all other cpus are in etr_sync_cpu_start.
  718. */
  719. in_sync = 0;
  720. preempt_disable();
  721. smp_call_function(etr_sync_cpu_start,&in_sync,0,0);
  722. local_irq_disable();
  723. etr_enable_sync_clock();
  724. /* Set clock to next OTE. */
  725. __ctl_set_bit(14, 21);
  726. __ctl_set_bit(0, 29);
  727. clock = ((unsigned long long) (aib->edf2.etv + 1)) << 32;
  728. if (set_clock(clock) == 0) {
  729. __udelay(1); /* Wait for the clock to start. */
  730. __ctl_clear_bit(0, 29);
  731. __ctl_clear_bit(14, 21);
  732. etr_stetr(aib);
  733. /* Adjust Linux timing variables. */
  734. delay = (unsigned long long)
  735. (aib->edf2.etv - sync_port->edf2.etv) << 32;
  736. etr_adjust_time(clock, delay);
  737. setup_jiffy_timer();
  738. /* Verify that the clock is properly set. */
  739. if (!etr_aib_follows(sync_port, aib, port)) {
  740. /* Didn't work. */
  741. etr_disable_sync_clock(NULL);
  742. in_sync = -EAGAIN;
  743. rc = -EAGAIN;
  744. } else {
  745. in_sync = 1;
  746. rc = 0;
  747. }
  748. } else {
  749. /* Could not set the clock ?!? */
  750. __ctl_clear_bit(0, 29);
  751. __ctl_clear_bit(14, 21);
  752. etr_disable_sync_clock(NULL);
  753. in_sync = -EAGAIN;
  754. rc = -EAGAIN;
  755. }
  756. local_irq_enable();
  757. smp_call_function(etr_sync_cpu_end,NULL,0,0);
  758. preempt_enable();
  759. return rc;
  760. }
  761. /*
  762. * Handle the immediate effects of the different events.
  763. * The port change event is used for online/offline changes.
  764. */
  765. static struct etr_eacr etr_handle_events(struct etr_eacr eacr)
  766. {
  767. if (test_and_clear_bit(ETR_EVENT_SYNC_CHECK, &etr_events))
  768. eacr.es = 0;
  769. if (test_and_clear_bit(ETR_EVENT_SWITCH_LOCAL, &etr_events))
  770. eacr.es = eacr.sl = 0;
  771. if (test_and_clear_bit(ETR_EVENT_PORT_ALERT, &etr_events))
  772. etr_port0_uptodate = etr_port1_uptodate = 0;
  773. if (test_and_clear_bit(ETR_EVENT_PORT0_CHANGE, &etr_events)) {
  774. if (eacr.e0)
  775. /*
  776. * Port change of an enabled port. We have to
  777. * assume that this can have caused an stepping
  778. * port switch.
  779. */
  780. etr_tolec = get_clock();
  781. eacr.p0 = etr_port0_online;
  782. if (!eacr.p0)
  783. eacr.e0 = 0;
  784. etr_port0_uptodate = 0;
  785. }
  786. if (test_and_clear_bit(ETR_EVENT_PORT1_CHANGE, &etr_events)) {
  787. if (eacr.e1)
  788. /*
  789. * Port change of an enabled port. We have to
  790. * assume that this can have caused an stepping
  791. * port switch.
  792. */
  793. etr_tolec = get_clock();
  794. eacr.p1 = etr_port1_online;
  795. if (!eacr.p1)
  796. eacr.e1 = 0;
  797. etr_port1_uptodate = 0;
  798. }
  799. clear_bit(ETR_EVENT_UPDATE, &etr_events);
  800. return eacr;
  801. }
  802. /*
  803. * Set up a timer that expires after the etr_tolec + 1.6 seconds if
  804. * one of the ports needs an update.
  805. */
  806. static void etr_set_tolec_timeout(unsigned long long now)
  807. {
  808. unsigned long micros;
  809. if ((!etr_eacr.p0 || etr_port0_uptodate) &&
  810. (!etr_eacr.p1 || etr_port1_uptodate))
  811. return;
  812. micros = (now > etr_tolec) ? ((now - etr_tolec) >> 12) : 0;
  813. micros = (micros > 1600000) ? 0 : 1600000 - micros;
  814. mod_timer(&etr_timer, jiffies + (micros * HZ) / 1000000 + 1);
  815. }
  816. /*
  817. * Set up a time that expires after 1/2 second.
  818. */
  819. static void etr_set_sync_timeout(void)
  820. {
  821. mod_timer(&etr_timer, jiffies + HZ/2);
  822. }
  823. /*
  824. * Update the aib information for one or both ports.
  825. */
  826. static struct etr_eacr etr_handle_update(struct etr_aib *aib,
  827. struct etr_eacr eacr)
  828. {
  829. /* With both ports disabled the aib information is useless. */
  830. if (!eacr.e0 && !eacr.e1)
  831. return eacr;
  832. /* Update port0 or port1 with aib stored in etr_tasklet_fn. */
  833. if (aib->esw.q == 0) {
  834. /* Information for port 0 stored. */
  835. if (eacr.p0 && !etr_port0_uptodate) {
  836. etr_port0 = *aib;
  837. if (etr_port0_online)
  838. etr_port0_uptodate = 1;
  839. }
  840. } else {
  841. /* Information for port 1 stored. */
  842. if (eacr.p1 && !etr_port1_uptodate) {
  843. etr_port1 = *aib;
  844. if (etr_port0_online)
  845. etr_port1_uptodate = 1;
  846. }
  847. }
  848. /*
  849. * Do not try to get the alternate port aib if the clock
  850. * is not in sync yet.
  851. */
  852. if (!eacr.es)
  853. return eacr;
  854. /*
  855. * If steai is available we can get the information about
  856. * the other port immediately. If only stetr is available the
  857. * data-port bit toggle has to be used.
  858. */
  859. if (test_bit(ETR_FLAG_STEAI, &etr_flags)) {
  860. if (eacr.p0 && !etr_port0_uptodate) {
  861. etr_steai_cv(&etr_port0, ETR_STEAI_PORT_0);
  862. etr_port0_uptodate = 1;
  863. }
  864. if (eacr.p1 && !etr_port1_uptodate) {
  865. etr_steai_cv(&etr_port1, ETR_STEAI_PORT_1);
  866. etr_port1_uptodate = 1;
  867. }
  868. } else {
  869. /*
  870. * One port was updated above, if the other
  871. * port is not uptodate toggle dp bit.
  872. */
  873. if ((eacr.p0 && !etr_port0_uptodate) ||
  874. (eacr.p1 && !etr_port1_uptodate))
  875. eacr.dp ^= 1;
  876. else
  877. eacr.dp = 0;
  878. }
  879. return eacr;
  880. }
  881. /*
  882. * Write new etr control register if it differs from the current one.
  883. * Return 1 if etr_tolec has been updated as well.
  884. */
  885. static void etr_update_eacr(struct etr_eacr eacr)
  886. {
  887. int dp_changed;
  888. if (memcmp(&etr_eacr, &eacr, sizeof(eacr)) == 0)
  889. /* No change, return. */
  890. return;
  891. /*
  892. * The disable of an active port of the change of the data port
  893. * bit can/will cause a change in the data port.
  894. */
  895. dp_changed = etr_eacr.e0 > eacr.e0 || etr_eacr.e1 > eacr.e1 ||
  896. (etr_eacr.dp ^ eacr.dp) != 0;
  897. etr_eacr = eacr;
  898. etr_setr(&etr_eacr);
  899. if (dp_changed)
  900. etr_tolec = get_clock();
  901. }
  902. /*
  903. * ETR tasklet. In this function you'll find the main logic. In
  904. * particular this is the only function that calls etr_update_eacr(),
  905. * it "controls" the etr control register.
  906. */
  907. static void etr_tasklet_fn(unsigned long dummy)
  908. {
  909. unsigned long long now;
  910. struct etr_eacr eacr;
  911. struct etr_aib aib;
  912. int sync_port;
  913. /* Create working copy of etr_eacr. */
  914. eacr = etr_eacr;
  915. /* Check for the different events and their immediate effects. */
  916. eacr = etr_handle_events(eacr);
  917. /* Check if ETR is supposed to be active. */
  918. eacr.ea = eacr.p0 || eacr.p1;
  919. if (!eacr.ea) {
  920. /* Both ports offline. Reset everything. */
  921. eacr.dp = eacr.es = eacr.sl = 0;
  922. on_each_cpu(etr_disable_sync_clock, NULL, 0, 1);
  923. del_timer_sync(&etr_timer);
  924. etr_update_eacr(eacr);
  925. set_bit(ETR_FLAG_EACCES, &etr_flags);
  926. return;
  927. }
  928. /* Store aib to get the current ETR status word. */
  929. BUG_ON(etr_stetr(&aib) != 0);
  930. etr_port0.esw = etr_port1.esw = aib.esw; /* Copy status word. */
  931. now = get_clock();
  932. /*
  933. * Update the port information if the last stepping port change
  934. * or data port change is older than 1.6 seconds.
  935. */
  936. if (now >= etr_tolec + (1600000 << 12))
  937. eacr = etr_handle_update(&aib, eacr);
  938. /*
  939. * Select ports to enable. The prefered synchronization mode is PPS.
  940. * If a port can be enabled depends on a number of things:
  941. * 1) The port needs to be online and uptodate. A port is not
  942. * disabled just because it is not uptodate, but it is only
  943. * enabled if it is uptodate.
  944. * 2) The port needs to have the same mode (pps / etr).
  945. * 3) The port needs to be usable -> etr_port_valid() == 1
  946. * 4) To enable the second port the clock needs to be in sync.
  947. * 5) If both ports are useable and are ETR ports, the network id
  948. * has to be the same.
  949. * The eacr.sl bit is used to indicate etr mode vs. pps mode.
  950. */
  951. if (eacr.p0 && aib.esw.psc0 == etr_lpsc_pps_mode) {
  952. eacr.sl = 0;
  953. eacr.e0 = 1;
  954. if (!etr_mode_is_pps(etr_eacr))
  955. eacr.es = 0;
  956. if (!eacr.es || !eacr.p1 || aib.esw.psc1 != etr_lpsc_pps_mode)
  957. eacr.e1 = 0;
  958. // FIXME: uptodate checks ?
  959. else if (etr_port0_uptodate && etr_port1_uptodate)
  960. eacr.e1 = 1;
  961. sync_port = (etr_port0_uptodate &&
  962. etr_port_valid(&etr_port0, 0)) ? 0 : -1;
  963. clear_bit(ETR_FLAG_EACCES, &etr_flags);
  964. } else if (eacr.p1 && aib.esw.psc1 == etr_lpsc_pps_mode) {
  965. eacr.sl = 0;
  966. eacr.e0 = 0;
  967. eacr.e1 = 1;
  968. if (!etr_mode_is_pps(etr_eacr))
  969. eacr.es = 0;
  970. sync_port = (etr_port1_uptodate &&
  971. etr_port_valid(&etr_port1, 1)) ? 1 : -1;
  972. clear_bit(ETR_FLAG_EACCES, &etr_flags);
  973. } else if (eacr.p0 && aib.esw.psc0 == etr_lpsc_operational_step) {
  974. eacr.sl = 1;
  975. eacr.e0 = 1;
  976. if (!etr_mode_is_etr(etr_eacr))
  977. eacr.es = 0;
  978. if (!eacr.es || !eacr.p1 ||
  979. aib.esw.psc1 != etr_lpsc_operational_alt)
  980. eacr.e1 = 0;
  981. else if (etr_port0_uptodate && etr_port1_uptodate &&
  982. etr_compare_network(&etr_port0, &etr_port1))
  983. eacr.e1 = 1;
  984. sync_port = (etr_port0_uptodate &&
  985. etr_port_valid(&etr_port0, 0)) ? 0 : -1;
  986. clear_bit(ETR_FLAG_EACCES, &etr_flags);
  987. } else if (eacr.p1 && aib.esw.psc1 == etr_lpsc_operational_step) {
  988. eacr.sl = 1;
  989. eacr.e0 = 0;
  990. eacr.e1 = 1;
  991. if (!etr_mode_is_etr(etr_eacr))
  992. eacr.es = 0;
  993. sync_port = (etr_port1_uptodate &&
  994. etr_port_valid(&etr_port1, 1)) ? 1 : -1;
  995. clear_bit(ETR_FLAG_EACCES, &etr_flags);
  996. } else {
  997. /* Both ports not usable. */
  998. eacr.es = eacr.sl = 0;
  999. sync_port = -1;
  1000. set_bit(ETR_FLAG_EACCES, &etr_flags);
  1001. }
  1002. /*
  1003. * If the clock is in sync just update the eacr and return.
  1004. * If there is no valid sync port wait for a port update.
  1005. */
  1006. if (eacr.es || sync_port < 0) {
  1007. etr_update_eacr(eacr);
  1008. etr_set_tolec_timeout(now);
  1009. return;
  1010. }
  1011. /*
  1012. * Prepare control register for clock syncing
  1013. * (reset data port bit, set sync check control.
  1014. */
  1015. eacr.dp = 0;
  1016. eacr.es = 1;
  1017. /*
  1018. * Update eacr and try to synchronize the clock. If the update
  1019. * of eacr caused a stepping port switch (or if we have to
  1020. * assume that a stepping port switch has occured) or the
  1021. * clock syncing failed, reset the sync check control bit
  1022. * and set up a timer to try again after 0.5 seconds
  1023. */
  1024. etr_update_eacr(eacr);
  1025. if (now < etr_tolec + (1600000 << 12) ||
  1026. etr_sync_clock(&aib, sync_port) != 0) {
  1027. /* Sync failed. Try again in 1/2 second. */
  1028. eacr.es = 0;
  1029. etr_update_eacr(eacr);
  1030. etr_set_sync_timeout();
  1031. } else
  1032. etr_set_tolec_timeout(now);
  1033. }
  1034. /*
  1035. * Sysfs interface functions
  1036. */
  1037. static struct sysdev_class etr_sysclass = {
  1038. set_kset_name("etr")
  1039. };
  1040. static struct sys_device etr_port0_dev = {
  1041. .id = 0,
  1042. .cls = &etr_sysclass,
  1043. };
  1044. static struct sys_device etr_port1_dev = {
  1045. .id = 1,
  1046. .cls = &etr_sysclass,
  1047. };
  1048. /*
  1049. * ETR class attributes
  1050. */
  1051. static ssize_t etr_stepping_port_show(struct sysdev_class *class, char *buf)
  1052. {
  1053. return sprintf(buf, "%i\n", etr_port0.esw.p);
  1054. }
  1055. static SYSDEV_CLASS_ATTR(stepping_port, 0400, etr_stepping_port_show, NULL);
  1056. static ssize_t etr_stepping_mode_show(struct sysdev_class *class, char *buf)
  1057. {
  1058. char *mode_str;
  1059. if (etr_mode_is_pps(etr_eacr))
  1060. mode_str = "pps";
  1061. else if (etr_mode_is_etr(etr_eacr))
  1062. mode_str = "etr";
  1063. else
  1064. mode_str = "local";
  1065. return sprintf(buf, "%s\n", mode_str);
  1066. }
  1067. static SYSDEV_CLASS_ATTR(stepping_mode, 0400, etr_stepping_mode_show, NULL);
  1068. /*
  1069. * ETR port attributes
  1070. */
  1071. static inline struct etr_aib *etr_aib_from_dev(struct sys_device *dev)
  1072. {
  1073. if (dev == &etr_port0_dev)
  1074. return etr_port0_online ? &etr_port0 : NULL;
  1075. else
  1076. return etr_port1_online ? &etr_port1 : NULL;
  1077. }
  1078. static ssize_t etr_online_show(struct sys_device *dev, char *buf)
  1079. {
  1080. unsigned int online;
  1081. online = (dev == &etr_port0_dev) ? etr_port0_online : etr_port1_online;
  1082. return sprintf(buf, "%i\n", online);
  1083. }
  1084. static ssize_t etr_online_store(struct sys_device *dev,
  1085. const char *buf, size_t count)
  1086. {
  1087. unsigned int value;
  1088. value = simple_strtoul(buf, NULL, 0);
  1089. if (value != 0 && value != 1)
  1090. return -EINVAL;
  1091. if (test_bit(ETR_FLAG_ENOSYS, &etr_flags))
  1092. return -ENOSYS;
  1093. if (dev == &etr_port0_dev) {
  1094. if (etr_port0_online == value)
  1095. return count; /* Nothing to do. */
  1096. etr_port0_online = value;
  1097. set_bit(ETR_EVENT_PORT0_CHANGE, &etr_events);
  1098. tasklet_hi_schedule(&etr_tasklet);
  1099. } else {
  1100. if (etr_port1_online == value)
  1101. return count; /* Nothing to do. */
  1102. etr_port1_online = value;
  1103. set_bit(ETR_EVENT_PORT1_CHANGE, &etr_events);
  1104. tasklet_hi_schedule(&etr_tasklet);
  1105. }
  1106. return count;
  1107. }
  1108. static SYSDEV_ATTR(online, 0600, etr_online_show, etr_online_store);
  1109. static ssize_t etr_stepping_control_show(struct sys_device *dev, char *buf)
  1110. {
  1111. return sprintf(buf, "%i\n", (dev == &etr_port0_dev) ?
  1112. etr_eacr.e0 : etr_eacr.e1);
  1113. }
  1114. static SYSDEV_ATTR(stepping_control, 0400, etr_stepping_control_show, NULL);
  1115. static ssize_t etr_mode_code_show(struct sys_device *dev, char *buf)
  1116. {
  1117. if (!etr_port0_online && !etr_port1_online)
  1118. /* Status word is not uptodate if both ports are offline. */
  1119. return -ENODATA;
  1120. return sprintf(buf, "%i\n", (dev == &etr_port0_dev) ?
  1121. etr_port0.esw.psc0 : etr_port0.esw.psc1);
  1122. }
  1123. static SYSDEV_ATTR(state_code, 0400, etr_mode_code_show, NULL);
  1124. static ssize_t etr_untuned_show(struct sys_device *dev, char *buf)
  1125. {
  1126. struct etr_aib *aib = etr_aib_from_dev(dev);
  1127. if (!aib || !aib->slsw.v1)
  1128. return -ENODATA;
  1129. return sprintf(buf, "%i\n", aib->edf1.u);
  1130. }
  1131. static SYSDEV_ATTR(untuned, 0400, etr_untuned_show, NULL);
  1132. static ssize_t etr_network_id_show(struct sys_device *dev, char *buf)
  1133. {
  1134. struct etr_aib *aib = etr_aib_from_dev(dev);
  1135. if (!aib || !aib->slsw.v1)
  1136. return -ENODATA;
  1137. return sprintf(buf, "%i\n", aib->edf1.net_id);
  1138. }
  1139. static SYSDEV_ATTR(network, 0400, etr_network_id_show, NULL);
  1140. static ssize_t etr_id_show(struct sys_device *dev, char *buf)
  1141. {
  1142. struct etr_aib *aib = etr_aib_from_dev(dev);
  1143. if (!aib || !aib->slsw.v1)
  1144. return -ENODATA;
  1145. return sprintf(buf, "%i\n", aib->edf1.etr_id);
  1146. }
  1147. static SYSDEV_ATTR(id, 0400, etr_id_show, NULL);
  1148. static ssize_t etr_port_number_show(struct sys_device *dev, char *buf)
  1149. {
  1150. struct etr_aib *aib = etr_aib_from_dev(dev);
  1151. if (!aib || !aib->slsw.v1)
  1152. return -ENODATA;
  1153. return sprintf(buf, "%i\n", aib->edf1.etr_pn);
  1154. }
  1155. static SYSDEV_ATTR(port, 0400, etr_port_number_show, NULL);
  1156. static ssize_t etr_coupled_show(struct sys_device *dev, char *buf)
  1157. {
  1158. struct etr_aib *aib = etr_aib_from_dev(dev);
  1159. if (!aib || !aib->slsw.v3)
  1160. return -ENODATA;
  1161. return sprintf(buf, "%i\n", aib->edf3.c);
  1162. }
  1163. static SYSDEV_ATTR(coupled, 0400, etr_coupled_show, NULL);
  1164. static ssize_t etr_local_time_show(struct sys_device *dev, char *buf)
  1165. {
  1166. struct etr_aib *aib = etr_aib_from_dev(dev);
  1167. if (!aib || !aib->slsw.v3)
  1168. return -ENODATA;
  1169. return sprintf(buf, "%i\n", aib->edf3.blto);
  1170. }
  1171. static SYSDEV_ATTR(local_time, 0400, etr_local_time_show, NULL);
  1172. static ssize_t etr_utc_offset_show(struct sys_device *dev, char *buf)
  1173. {
  1174. struct etr_aib *aib = etr_aib_from_dev(dev);
  1175. if (!aib || !aib->slsw.v3)
  1176. return -ENODATA;
  1177. return sprintf(buf, "%i\n", aib->edf3.buo);
  1178. }
  1179. static SYSDEV_ATTR(utc_offset, 0400, etr_utc_offset_show, NULL);
  1180. static struct sysdev_attribute *etr_port_attributes[] = {
  1181. &attr_online,
  1182. &attr_stepping_control,
  1183. &attr_state_code,
  1184. &attr_untuned,
  1185. &attr_network,
  1186. &attr_id,
  1187. &attr_port,
  1188. &attr_coupled,
  1189. &attr_local_time,
  1190. &attr_utc_offset,
  1191. NULL
  1192. };
  1193. static int __init etr_register_port(struct sys_device *dev)
  1194. {
  1195. struct sysdev_attribute **attr;
  1196. int rc;
  1197. rc = sysdev_register(dev);
  1198. if (rc)
  1199. goto out;
  1200. for (attr = etr_port_attributes; *attr; attr++) {
  1201. rc = sysdev_create_file(dev, *attr);
  1202. if (rc)
  1203. goto out_unreg;
  1204. }
  1205. return 0;
  1206. out_unreg:
  1207. for (; attr >= etr_port_attributes; attr--)
  1208. sysdev_remove_file(dev, *attr);
  1209. sysdev_unregister(dev);
  1210. out:
  1211. return rc;
  1212. }
  1213. static void __init etr_unregister_port(struct sys_device *dev)
  1214. {
  1215. struct sysdev_attribute **attr;
  1216. for (attr = etr_port_attributes; *attr; attr++)
  1217. sysdev_remove_file(dev, *attr);
  1218. sysdev_unregister(dev);
  1219. }
  1220. static int __init etr_init_sysfs(void)
  1221. {
  1222. int rc;
  1223. rc = sysdev_class_register(&etr_sysclass);
  1224. if (rc)
  1225. goto out;
  1226. rc = sysdev_class_create_file(&etr_sysclass, &attr_stepping_port);
  1227. if (rc)
  1228. goto out_unreg_class;
  1229. rc = sysdev_class_create_file(&etr_sysclass, &attr_stepping_mode);
  1230. if (rc)
  1231. goto out_remove_stepping_port;
  1232. rc = etr_register_port(&etr_port0_dev);
  1233. if (rc)
  1234. goto out_remove_stepping_mode;
  1235. rc = etr_register_port(&etr_port1_dev);
  1236. if (rc)
  1237. goto out_remove_port0;
  1238. return 0;
  1239. out_remove_port0:
  1240. etr_unregister_port(&etr_port0_dev);
  1241. out_remove_stepping_mode:
  1242. sysdev_class_remove_file(&etr_sysclass, &attr_stepping_mode);
  1243. out_remove_stepping_port:
  1244. sysdev_class_remove_file(&etr_sysclass, &attr_stepping_port);
  1245. out_unreg_class:
  1246. sysdev_class_unregister(&etr_sysclass);
  1247. out:
  1248. return rc;
  1249. }
  1250. device_initcall(etr_init_sysfs);