time.c 37 KB

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