time.c 37 KB

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