time.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402
  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. .is_continuous = 1,
  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. static void etr_sync_cpu_start(void *dummy)
  666. {
  667. int *in_sync = dummy;
  668. etr_enable_sync_clock();
  669. /*
  670. * This looks like a busy wait loop but it isn't. etr_sync_cpus
  671. * is called on all other cpus while the TOD clocks is stopped.
  672. * __udelay will stop the cpu on an enabled wait psw until the
  673. * TOD is running again.
  674. */
  675. while (*in_sync == 0)
  676. __udelay(1);
  677. if (*in_sync != 1)
  678. /* Didn't work. Clear per-cpu in sync bit again. */
  679. etr_disable_sync_clock(NULL);
  680. /*
  681. * This round of TOD syncing is done. Set the clock comparator
  682. * to the next tick and let the processor continue.
  683. */
  684. setup_jiffy_timer();
  685. }
  686. static void etr_sync_cpu_end(void *dummy)
  687. {
  688. }
  689. /*
  690. * Sync the TOD clock using the port refered to by aibp. This port
  691. * has to be enabled and the other port has to be disabled. The
  692. * last eacr update has to be more than 1.6 seconds in the past.
  693. */
  694. static int etr_sync_clock(struct etr_aib *aib, int port)
  695. {
  696. struct etr_aib *sync_port;
  697. unsigned long long clock, delay;
  698. int in_sync, follows;
  699. int rc;
  700. /* Check if the current aib is adjacent to the sync port aib. */
  701. sync_port = (port == 0) ? &etr_port0 : &etr_port1;
  702. follows = etr_aib_follows(sync_port, aib, port);
  703. memcpy(sync_port, aib, sizeof(*aib));
  704. if (!follows)
  705. return -EAGAIN;
  706. /*
  707. * Catch all other cpus and make them wait until we have
  708. * successfully synced the clock. smp_call_function will
  709. * return after all other cpus are in etr_sync_cpu_start.
  710. */
  711. in_sync = 0;
  712. preempt_disable();
  713. smp_call_function(etr_sync_cpu_start,&in_sync,0,0);
  714. local_irq_disable();
  715. etr_enable_sync_clock();
  716. /* Set clock to next OTE. */
  717. __ctl_set_bit(14, 21);
  718. __ctl_set_bit(0, 29);
  719. clock = ((unsigned long long) (aib->edf2.etv + 1)) << 32;
  720. if (set_clock(clock) == 0) {
  721. __udelay(1); /* Wait for the clock to start. */
  722. __ctl_clear_bit(0, 29);
  723. __ctl_clear_bit(14, 21);
  724. etr_stetr(aib);
  725. /* Adjust Linux timing variables. */
  726. delay = (unsigned long long)
  727. (aib->edf2.etv - sync_port->edf2.etv) << 32;
  728. etr_adjust_time(clock, delay);
  729. setup_jiffy_timer();
  730. /* Verify that the clock is properly set. */
  731. if (!etr_aib_follows(sync_port, aib, port)) {
  732. /* Didn't work. */
  733. etr_disable_sync_clock(NULL);
  734. in_sync = -EAGAIN;
  735. rc = -EAGAIN;
  736. } else {
  737. in_sync = 1;
  738. rc = 0;
  739. }
  740. } else {
  741. /* Could not set the clock ?!? */
  742. __ctl_clear_bit(0, 29);
  743. __ctl_clear_bit(14, 21);
  744. etr_disable_sync_clock(NULL);
  745. in_sync = -EAGAIN;
  746. rc = -EAGAIN;
  747. }
  748. local_irq_enable();
  749. smp_call_function(etr_sync_cpu_end,NULL,0,0);
  750. preempt_enable();
  751. return rc;
  752. }
  753. /*
  754. * Handle the immediate effects of the different events.
  755. * The port change event is used for online/offline changes.
  756. */
  757. static struct etr_eacr etr_handle_events(struct etr_eacr eacr)
  758. {
  759. if (test_and_clear_bit(ETR_EVENT_SYNC_CHECK, &etr_events))
  760. eacr.es = 0;
  761. if (test_and_clear_bit(ETR_EVENT_SWITCH_LOCAL, &etr_events))
  762. eacr.es = eacr.sl = 0;
  763. if (test_and_clear_bit(ETR_EVENT_PORT_ALERT, &etr_events))
  764. etr_port0_uptodate = etr_port1_uptodate = 0;
  765. if (test_and_clear_bit(ETR_EVENT_PORT0_CHANGE, &etr_events)) {
  766. if (eacr.e0)
  767. /*
  768. * Port change of an enabled port. We have to
  769. * assume that this can have caused an stepping
  770. * port switch.
  771. */
  772. etr_tolec = get_clock();
  773. eacr.p0 = etr_port0_online;
  774. if (!eacr.p0)
  775. eacr.e0 = 0;
  776. etr_port0_uptodate = 0;
  777. }
  778. if (test_and_clear_bit(ETR_EVENT_PORT1_CHANGE, &etr_events)) {
  779. if (eacr.e1)
  780. /*
  781. * Port change of an enabled port. We have to
  782. * assume that this can have caused an stepping
  783. * port switch.
  784. */
  785. etr_tolec = get_clock();
  786. eacr.p1 = etr_port1_online;
  787. if (!eacr.p1)
  788. eacr.e1 = 0;
  789. etr_port1_uptodate = 0;
  790. }
  791. clear_bit(ETR_EVENT_UPDATE, &etr_events);
  792. return eacr;
  793. }
  794. /*
  795. * Set up a timer that expires after the etr_tolec + 1.6 seconds if
  796. * one of the ports needs an update.
  797. */
  798. static void etr_set_tolec_timeout(unsigned long long now)
  799. {
  800. unsigned long micros;
  801. if ((!etr_eacr.p0 || etr_port0_uptodate) &&
  802. (!etr_eacr.p1 || etr_port1_uptodate))
  803. return;
  804. micros = (now > etr_tolec) ? ((now - etr_tolec) >> 12) : 0;
  805. micros = (micros > 1600000) ? 0 : 1600000 - micros;
  806. mod_timer(&etr_timer, jiffies + (micros * HZ) / 1000000 + 1);
  807. }
  808. /*
  809. * Set up a time that expires after 1/2 second.
  810. */
  811. static void etr_set_sync_timeout(void)
  812. {
  813. mod_timer(&etr_timer, jiffies + HZ/2);
  814. }
  815. /*
  816. * Update the aib information for one or both ports.
  817. */
  818. static struct etr_eacr etr_handle_update(struct etr_aib *aib,
  819. struct etr_eacr eacr)
  820. {
  821. /* With both ports disabled the aib information is useless. */
  822. if (!eacr.e0 && !eacr.e1)
  823. return eacr;
  824. /* Update port0 or port1 with aib stored in etr_tasklet_fn. */
  825. if (aib->esw.q == 0) {
  826. /* Information for port 0 stored. */
  827. if (eacr.p0 && !etr_port0_uptodate) {
  828. etr_port0 = *aib;
  829. if (etr_port0_online)
  830. etr_port0_uptodate = 1;
  831. }
  832. } else {
  833. /* Information for port 1 stored. */
  834. if (eacr.p1 && !etr_port1_uptodate) {
  835. etr_port1 = *aib;
  836. if (etr_port0_online)
  837. etr_port1_uptodate = 1;
  838. }
  839. }
  840. /*
  841. * Do not try to get the alternate port aib if the clock
  842. * is not in sync yet.
  843. */
  844. if (!eacr.es)
  845. return eacr;
  846. /*
  847. * If steai is available we can get the information about
  848. * the other port immediately. If only stetr is available the
  849. * data-port bit toggle has to be used.
  850. */
  851. if (test_bit(ETR_FLAG_STEAI, &etr_flags)) {
  852. if (eacr.p0 && !etr_port0_uptodate) {
  853. etr_steai_cv(&etr_port0, ETR_STEAI_PORT_0);
  854. etr_port0_uptodate = 1;
  855. }
  856. if (eacr.p1 && !etr_port1_uptodate) {
  857. etr_steai_cv(&etr_port1, ETR_STEAI_PORT_1);
  858. etr_port1_uptodate = 1;
  859. }
  860. } else {
  861. /*
  862. * One port was updated above, if the other
  863. * port is not uptodate toggle dp bit.
  864. */
  865. if ((eacr.p0 && !etr_port0_uptodate) ||
  866. (eacr.p1 && !etr_port1_uptodate))
  867. eacr.dp ^= 1;
  868. else
  869. eacr.dp = 0;
  870. }
  871. return eacr;
  872. }
  873. /*
  874. * Write new etr control register if it differs from the current one.
  875. * Return 1 if etr_tolec has been updated as well.
  876. */
  877. static void etr_update_eacr(struct etr_eacr eacr)
  878. {
  879. int dp_changed;
  880. if (memcmp(&etr_eacr, &eacr, sizeof(eacr)) == 0)
  881. /* No change, return. */
  882. return;
  883. /*
  884. * The disable of an active port of the change of the data port
  885. * bit can/will cause a change in the data port.
  886. */
  887. dp_changed = etr_eacr.e0 > eacr.e0 || etr_eacr.e1 > eacr.e1 ||
  888. (etr_eacr.dp ^ eacr.dp) != 0;
  889. etr_eacr = eacr;
  890. etr_setr(&etr_eacr);
  891. if (dp_changed)
  892. etr_tolec = get_clock();
  893. }
  894. /*
  895. * ETR tasklet. In this function you'll find the main logic. In
  896. * particular this is the only function that calls etr_update_eacr(),
  897. * it "controls" the etr control register.
  898. */
  899. static void etr_tasklet_fn(unsigned long dummy)
  900. {
  901. unsigned long long now;
  902. struct etr_eacr eacr;
  903. struct etr_aib aib;
  904. int sync_port;
  905. /* Create working copy of etr_eacr. */
  906. eacr = etr_eacr;
  907. /* Check for the different events and their immediate effects. */
  908. eacr = etr_handle_events(eacr);
  909. /* Check if ETR is supposed to be active. */
  910. eacr.ea = eacr.p0 || eacr.p1;
  911. if (!eacr.ea) {
  912. /* Both ports offline. Reset everything. */
  913. eacr.dp = eacr.es = eacr.sl = 0;
  914. on_each_cpu(etr_disable_sync_clock, NULL, 0, 1);
  915. del_timer_sync(&etr_timer);
  916. etr_update_eacr(eacr);
  917. set_bit(ETR_FLAG_EACCES, &etr_flags);
  918. return;
  919. }
  920. /* Store aib to get the current ETR status word. */
  921. BUG_ON(etr_stetr(&aib) != 0);
  922. etr_port0.esw = etr_port1.esw = aib.esw; /* Copy status word. */
  923. now = get_clock();
  924. /*
  925. * Update the port information if the last stepping port change
  926. * or data port change is older than 1.6 seconds.
  927. */
  928. if (now >= etr_tolec + (1600000 << 12))
  929. eacr = etr_handle_update(&aib, eacr);
  930. /*
  931. * Select ports to enable. The prefered synchronization mode is PPS.
  932. * If a port can be enabled depends on a number of things:
  933. * 1) The port needs to be online and uptodate. A port is not
  934. * disabled just because it is not uptodate, but it is only
  935. * enabled if it is uptodate.
  936. * 2) The port needs to have the same mode (pps / etr).
  937. * 3) The port needs to be usable -> etr_port_valid() == 1
  938. * 4) To enable the second port the clock needs to be in sync.
  939. * 5) If both ports are useable and are ETR ports, the network id
  940. * has to be the same.
  941. * The eacr.sl bit is used to indicate etr mode vs. pps mode.
  942. */
  943. if (eacr.p0 && aib.esw.psc0 == etr_lpsc_pps_mode) {
  944. eacr.sl = 0;
  945. eacr.e0 = 1;
  946. if (!etr_mode_is_pps(etr_eacr))
  947. eacr.es = 0;
  948. if (!eacr.es || !eacr.p1 || aib.esw.psc1 != etr_lpsc_pps_mode)
  949. eacr.e1 = 0;
  950. // FIXME: uptodate checks ?
  951. else if (etr_port0_uptodate && etr_port1_uptodate)
  952. eacr.e1 = 1;
  953. sync_port = (etr_port0_uptodate &&
  954. etr_port_valid(&etr_port0, 0)) ? 0 : -1;
  955. clear_bit(ETR_FLAG_EACCES, &etr_flags);
  956. } else if (eacr.p1 && aib.esw.psc1 == etr_lpsc_pps_mode) {
  957. eacr.sl = 0;
  958. eacr.e0 = 0;
  959. eacr.e1 = 1;
  960. if (!etr_mode_is_pps(etr_eacr))
  961. eacr.es = 0;
  962. sync_port = (etr_port1_uptodate &&
  963. etr_port_valid(&etr_port1, 1)) ? 1 : -1;
  964. clear_bit(ETR_FLAG_EACCES, &etr_flags);
  965. } else if (eacr.p0 && aib.esw.psc0 == etr_lpsc_operational_step) {
  966. eacr.sl = 1;
  967. eacr.e0 = 1;
  968. if (!etr_mode_is_etr(etr_eacr))
  969. eacr.es = 0;
  970. if (!eacr.es || !eacr.p1 ||
  971. aib.esw.psc1 != etr_lpsc_operational_alt)
  972. eacr.e1 = 0;
  973. else if (etr_port0_uptodate && etr_port1_uptodate &&
  974. etr_compare_network(&etr_port0, &etr_port1))
  975. eacr.e1 = 1;
  976. sync_port = (etr_port0_uptodate &&
  977. etr_port_valid(&etr_port0, 0)) ? 0 : -1;
  978. clear_bit(ETR_FLAG_EACCES, &etr_flags);
  979. } else if (eacr.p1 && aib.esw.psc1 == etr_lpsc_operational_step) {
  980. eacr.sl = 1;
  981. eacr.e0 = 0;
  982. eacr.e1 = 1;
  983. if (!etr_mode_is_etr(etr_eacr))
  984. eacr.es = 0;
  985. sync_port = (etr_port1_uptodate &&
  986. etr_port_valid(&etr_port1, 1)) ? 1 : -1;
  987. clear_bit(ETR_FLAG_EACCES, &etr_flags);
  988. } else {
  989. /* Both ports not usable. */
  990. eacr.es = eacr.sl = 0;
  991. sync_port = -1;
  992. set_bit(ETR_FLAG_EACCES, &etr_flags);
  993. }
  994. /*
  995. * If the clock is in sync just update the eacr and return.
  996. * If there is no valid sync port wait for a port update.
  997. */
  998. if (eacr.es || sync_port < 0) {
  999. etr_update_eacr(eacr);
  1000. etr_set_tolec_timeout(now);
  1001. return;
  1002. }
  1003. /*
  1004. * Prepare control register for clock syncing
  1005. * (reset data port bit, set sync check control.
  1006. */
  1007. eacr.dp = 0;
  1008. eacr.es = 1;
  1009. /*
  1010. * Update eacr and try to synchronize the clock. If the update
  1011. * of eacr caused a stepping port switch (or if we have to
  1012. * assume that a stepping port switch has occured) or the
  1013. * clock syncing failed, reset the sync check control bit
  1014. * and set up a timer to try again after 0.5 seconds
  1015. */
  1016. etr_update_eacr(eacr);
  1017. if (now < etr_tolec + (1600000 << 12) ||
  1018. etr_sync_clock(&aib, sync_port) != 0) {
  1019. /* Sync failed. Try again in 1/2 second. */
  1020. eacr.es = 0;
  1021. etr_update_eacr(eacr);
  1022. etr_set_sync_timeout();
  1023. } else
  1024. etr_set_tolec_timeout(now);
  1025. }
  1026. /*
  1027. * Sysfs interface functions
  1028. */
  1029. static struct sysdev_class etr_sysclass = {
  1030. set_kset_name("etr")
  1031. };
  1032. static struct sys_device etr_port0_dev = {
  1033. .id = 0,
  1034. .cls = &etr_sysclass,
  1035. };
  1036. static struct sys_device etr_port1_dev = {
  1037. .id = 1,
  1038. .cls = &etr_sysclass,
  1039. };
  1040. /*
  1041. * ETR class attributes
  1042. */
  1043. static ssize_t etr_stepping_port_show(struct sysdev_class *class, char *buf)
  1044. {
  1045. return sprintf(buf, "%i\n", etr_port0.esw.p);
  1046. }
  1047. static SYSDEV_CLASS_ATTR(stepping_port, 0400, etr_stepping_port_show, NULL);
  1048. static ssize_t etr_stepping_mode_show(struct sysdev_class *class, char *buf)
  1049. {
  1050. char *mode_str;
  1051. if (etr_mode_is_pps(etr_eacr))
  1052. mode_str = "pps";
  1053. else if (etr_mode_is_etr(etr_eacr))
  1054. mode_str = "etr";
  1055. else
  1056. mode_str = "local";
  1057. return sprintf(buf, "%s\n", mode_str);
  1058. }
  1059. static SYSDEV_CLASS_ATTR(stepping_mode, 0400, etr_stepping_mode_show, NULL);
  1060. /*
  1061. * ETR port attributes
  1062. */
  1063. static inline struct etr_aib *etr_aib_from_dev(struct sys_device *dev)
  1064. {
  1065. if (dev == &etr_port0_dev)
  1066. return etr_port0_online ? &etr_port0 : NULL;
  1067. else
  1068. return etr_port1_online ? &etr_port1 : NULL;
  1069. }
  1070. static ssize_t etr_online_show(struct sys_device *dev, char *buf)
  1071. {
  1072. unsigned int online;
  1073. online = (dev == &etr_port0_dev) ? etr_port0_online : etr_port1_online;
  1074. return sprintf(buf, "%i\n", online);
  1075. }
  1076. static ssize_t etr_online_store(struct sys_device *dev,
  1077. const char *buf, size_t count)
  1078. {
  1079. unsigned int value;
  1080. value = simple_strtoul(buf, NULL, 0);
  1081. if (value != 0 && value != 1)
  1082. return -EINVAL;
  1083. if (test_bit(ETR_FLAG_ENOSYS, &etr_flags))
  1084. return -ENOSYS;
  1085. if (dev == &etr_port0_dev) {
  1086. if (etr_port0_online == value)
  1087. return count; /* Nothing to do. */
  1088. etr_port0_online = value;
  1089. set_bit(ETR_EVENT_PORT0_CHANGE, &etr_events);
  1090. tasklet_hi_schedule(&etr_tasklet);
  1091. } else {
  1092. if (etr_port1_online == value)
  1093. return count; /* Nothing to do. */
  1094. etr_port1_online = value;
  1095. set_bit(ETR_EVENT_PORT1_CHANGE, &etr_events);
  1096. tasklet_hi_schedule(&etr_tasklet);
  1097. }
  1098. return count;
  1099. }
  1100. static SYSDEV_ATTR(online, 0600, etr_online_show, etr_online_store);
  1101. static ssize_t etr_stepping_control_show(struct sys_device *dev, char *buf)
  1102. {
  1103. return sprintf(buf, "%i\n", (dev == &etr_port0_dev) ?
  1104. etr_eacr.e0 : etr_eacr.e1);
  1105. }
  1106. static SYSDEV_ATTR(stepping_control, 0400, etr_stepping_control_show, NULL);
  1107. static ssize_t etr_mode_code_show(struct sys_device *dev, char *buf)
  1108. {
  1109. if (!etr_port0_online && !etr_port1_online)
  1110. /* Status word is not uptodate if both ports are offline. */
  1111. return -ENODATA;
  1112. return sprintf(buf, "%i\n", (dev == &etr_port0_dev) ?
  1113. etr_port0.esw.psc0 : etr_port0.esw.psc1);
  1114. }
  1115. static SYSDEV_ATTR(state_code, 0400, etr_mode_code_show, NULL);
  1116. static ssize_t etr_untuned_show(struct sys_device *dev, char *buf)
  1117. {
  1118. struct etr_aib *aib = etr_aib_from_dev(dev);
  1119. if (!aib || !aib->slsw.v1)
  1120. return -ENODATA;
  1121. return sprintf(buf, "%i\n", aib->edf1.u);
  1122. }
  1123. static SYSDEV_ATTR(untuned, 0400, etr_untuned_show, NULL);
  1124. static ssize_t etr_network_id_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.net_id);
  1130. }
  1131. static SYSDEV_ATTR(network, 0400, etr_network_id_show, NULL);
  1132. static ssize_t etr_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.etr_id);
  1138. }
  1139. static SYSDEV_ATTR(id, 0400, etr_id_show, NULL);
  1140. static ssize_t etr_port_number_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_pn);
  1146. }
  1147. static SYSDEV_ATTR(port, 0400, etr_port_number_show, NULL);
  1148. static ssize_t etr_coupled_show(struct sys_device *dev, char *buf)
  1149. {
  1150. struct etr_aib *aib = etr_aib_from_dev(dev);
  1151. if (!aib || !aib->slsw.v3)
  1152. return -ENODATA;
  1153. return sprintf(buf, "%i\n", aib->edf3.c);
  1154. }
  1155. static SYSDEV_ATTR(coupled, 0400, etr_coupled_show, NULL);
  1156. static ssize_t etr_local_time_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.blto);
  1162. }
  1163. static SYSDEV_ATTR(local_time, 0400, etr_local_time_show, NULL);
  1164. static ssize_t etr_utc_offset_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.buo);
  1170. }
  1171. static SYSDEV_ATTR(utc_offset, 0400, etr_utc_offset_show, NULL);
  1172. static struct sysdev_attribute *etr_port_attributes[] = {
  1173. &attr_online,
  1174. &attr_stepping_control,
  1175. &attr_state_code,
  1176. &attr_untuned,
  1177. &attr_network,
  1178. &attr_id,
  1179. &attr_port,
  1180. &attr_coupled,
  1181. &attr_local_time,
  1182. &attr_utc_offset,
  1183. NULL
  1184. };
  1185. static int __init etr_register_port(struct sys_device *dev)
  1186. {
  1187. struct sysdev_attribute **attr;
  1188. int rc;
  1189. rc = sysdev_register(dev);
  1190. if (rc)
  1191. goto out;
  1192. for (attr = etr_port_attributes; *attr; attr++) {
  1193. rc = sysdev_create_file(dev, *attr);
  1194. if (rc)
  1195. goto out_unreg;
  1196. }
  1197. return 0;
  1198. out_unreg:
  1199. for (; attr >= etr_port_attributes; attr--)
  1200. sysdev_remove_file(dev, *attr);
  1201. sysdev_unregister(dev);
  1202. out:
  1203. return rc;
  1204. }
  1205. static void __init etr_unregister_port(struct sys_device *dev)
  1206. {
  1207. struct sysdev_attribute **attr;
  1208. for (attr = etr_port_attributes; *attr; attr++)
  1209. sysdev_remove_file(dev, *attr);
  1210. sysdev_unregister(dev);
  1211. }
  1212. static int __init etr_init_sysfs(void)
  1213. {
  1214. int rc;
  1215. rc = sysdev_class_register(&etr_sysclass);
  1216. if (rc)
  1217. goto out;
  1218. rc = sysdev_class_create_file(&etr_sysclass, &attr_stepping_port);
  1219. if (rc)
  1220. goto out_unreg_class;
  1221. rc = sysdev_class_create_file(&etr_sysclass, &attr_stepping_mode);
  1222. if (rc)
  1223. goto out_remove_stepping_port;
  1224. rc = etr_register_port(&etr_port0_dev);
  1225. if (rc)
  1226. goto out_remove_stepping_mode;
  1227. rc = etr_register_port(&etr_port1_dev);
  1228. if (rc)
  1229. goto out_remove_port0;
  1230. return 0;
  1231. out_remove_port0:
  1232. etr_unregister_port(&etr_port0_dev);
  1233. out_remove_stepping_mode:
  1234. sysdev_class_remove_file(&etr_sysclass, &attr_stepping_mode);
  1235. out_remove_stepping_port:
  1236. sysdev_class_remove_file(&etr_sysclass, &attr_stepping_port);
  1237. out_unreg_class:
  1238. sysdev_class_unregister(&etr_sysclass);
  1239. out:
  1240. return rc;
  1241. }
  1242. device_initcall(etr_init_sysfs);