time.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817
  1. /*
  2. * Time of day based timer functions.
  3. *
  4. * S390 version
  5. * Copyright IBM Corp. 1999, 2008
  6. * Author(s): Hartmut Penner (hp@de.ibm.com),
  7. * Martin Schwidefsky (schwidefsky@de.ibm.com),
  8. * Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
  9. *
  10. * Derived from "arch/i386/kernel/time.c"
  11. * Copyright (C) 1991, 1992, 1995 Linus Torvalds
  12. */
  13. #define KMSG_COMPONENT "time"
  14. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  15. #include <linux/kernel_stat.h>
  16. #include <linux/errno.h>
  17. #include <linux/module.h>
  18. #include <linux/sched.h>
  19. #include <linux/kernel.h>
  20. #include <linux/param.h>
  21. #include <linux/string.h>
  22. #include <linux/mm.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/cpu.h>
  25. #include <linux/stop_machine.h>
  26. #include <linux/time.h>
  27. #include <linux/device.h>
  28. #include <linux/delay.h>
  29. #include <linux/init.h>
  30. #include <linux/smp.h>
  31. #include <linux/types.h>
  32. #include <linux/profile.h>
  33. #include <linux/timex.h>
  34. #include <linux/notifier.h>
  35. #include <linux/timekeeper_internal.h>
  36. #include <linux/clockchips.h>
  37. #include <linux/gfp.h>
  38. #include <linux/kprobes.h>
  39. #include <asm/uaccess.h>
  40. #include <asm/delay.h>
  41. #include <asm/div64.h>
  42. #include <asm/vdso.h>
  43. #include <asm/irq.h>
  44. #include <asm/irq_regs.h>
  45. #include <asm/vtimer.h>
  46. #include <asm/etr.h>
  47. #include <asm/cio.h>
  48. #include "entry.h"
  49. /* change this if you have some constant time drift */
  50. #define USECS_PER_JIFFY ((unsigned long) 1000000/HZ)
  51. #define CLK_TICKS_PER_JIFFY ((unsigned long) USECS_PER_JIFFY << 12)
  52. u64 sched_clock_base_cc = -1; /* Force to data section. */
  53. EXPORT_SYMBOL_GPL(sched_clock_base_cc);
  54. static DEFINE_PER_CPU(struct clock_event_device, comparators);
  55. ATOMIC_NOTIFIER_HEAD(s390_epoch_delta_notifier);
  56. EXPORT_SYMBOL(s390_epoch_delta_notifier);
  57. /*
  58. * Scheduler clock - returns current time in nanosec units.
  59. */
  60. unsigned long long notrace sched_clock(void)
  61. {
  62. return tod_to_ns(get_tod_clock_monotonic());
  63. }
  64. NOKPROBE_SYMBOL(sched_clock);
  65. /*
  66. * Monotonic_clock - returns # of nanoseconds passed since time_init()
  67. */
  68. unsigned long long monotonic_clock(void)
  69. {
  70. return sched_clock();
  71. }
  72. EXPORT_SYMBOL(monotonic_clock);
  73. void tod_to_timeval(__u64 todval, struct timespec64 *xt)
  74. {
  75. unsigned long long sec;
  76. sec = todval >> 12;
  77. do_div(sec, 1000000);
  78. xt->tv_sec = sec;
  79. todval -= (sec * 1000000) << 12;
  80. xt->tv_nsec = ((todval * 1000) >> 12);
  81. }
  82. EXPORT_SYMBOL(tod_to_timeval);
  83. void clock_comparator_work(void)
  84. {
  85. struct clock_event_device *cd;
  86. S390_lowcore.clock_comparator = -1ULL;
  87. cd = this_cpu_ptr(&comparators);
  88. cd->event_handler(cd);
  89. }
  90. /*
  91. * Fixup the clock comparator.
  92. */
  93. static void fixup_clock_comparator(unsigned long long delta)
  94. {
  95. /* If nobody is waiting there's nothing to fix. */
  96. if (S390_lowcore.clock_comparator == -1ULL)
  97. return;
  98. S390_lowcore.clock_comparator += delta;
  99. set_clock_comparator(S390_lowcore.clock_comparator);
  100. }
  101. static int s390_next_event(unsigned long delta,
  102. struct clock_event_device *evt)
  103. {
  104. S390_lowcore.clock_comparator = get_tod_clock() + delta;
  105. set_clock_comparator(S390_lowcore.clock_comparator);
  106. return 0;
  107. }
  108. /*
  109. * Set up lowcore and control register of the current cpu to
  110. * enable TOD clock and clock comparator interrupts.
  111. */
  112. void init_cpu_timer(void)
  113. {
  114. struct clock_event_device *cd;
  115. int cpu;
  116. S390_lowcore.clock_comparator = -1ULL;
  117. set_clock_comparator(S390_lowcore.clock_comparator);
  118. cpu = smp_processor_id();
  119. cd = &per_cpu(comparators, cpu);
  120. cd->name = "comparator";
  121. cd->features = CLOCK_EVT_FEAT_ONESHOT;
  122. cd->mult = 16777;
  123. cd->shift = 12;
  124. cd->min_delta_ns = 1;
  125. cd->max_delta_ns = LONG_MAX;
  126. cd->rating = 400;
  127. cd->cpumask = cpumask_of(cpu);
  128. cd->set_next_event = s390_next_event;
  129. clockevents_register_device(cd);
  130. /* Enable clock comparator timer interrupt. */
  131. __ctl_set_bit(0,11);
  132. /* Always allow the timing alert external interrupt. */
  133. __ctl_set_bit(0, 4);
  134. }
  135. static void clock_comparator_interrupt(struct ext_code ext_code,
  136. unsigned int param32,
  137. unsigned long param64)
  138. {
  139. inc_irq_stat(IRQEXT_CLK);
  140. if (S390_lowcore.clock_comparator == -1ULL)
  141. set_clock_comparator(S390_lowcore.clock_comparator);
  142. }
  143. static void etr_timing_alert(struct etr_irq_parm *);
  144. static void stp_timing_alert(struct stp_irq_parm *);
  145. static void timing_alert_interrupt(struct ext_code ext_code,
  146. unsigned int param32, unsigned long param64)
  147. {
  148. inc_irq_stat(IRQEXT_TLA);
  149. if (param32 & 0x00c40000)
  150. etr_timing_alert((struct etr_irq_parm *) &param32);
  151. if (param32 & 0x00038000)
  152. stp_timing_alert((struct stp_irq_parm *) &param32);
  153. }
  154. static void etr_reset(void);
  155. static void stp_reset(void);
  156. void read_persistent_clock64(struct timespec64 *ts)
  157. {
  158. tod_to_timeval(get_tod_clock() - TOD_UNIX_EPOCH, ts);
  159. }
  160. void read_boot_clock64(struct timespec64 *ts)
  161. {
  162. tod_to_timeval(sched_clock_base_cc - TOD_UNIX_EPOCH, ts);
  163. }
  164. static cycle_t read_tod_clock(struct clocksource *cs)
  165. {
  166. return get_tod_clock();
  167. }
  168. static struct clocksource clocksource_tod = {
  169. .name = "tod",
  170. .rating = 400,
  171. .read = read_tod_clock,
  172. .mask = -1ULL,
  173. .mult = 1000,
  174. .shift = 12,
  175. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  176. };
  177. struct clocksource * __init clocksource_default_clock(void)
  178. {
  179. return &clocksource_tod;
  180. }
  181. void update_vsyscall(struct timekeeper *tk)
  182. {
  183. u64 nsecps;
  184. if (tk->tkr_mono.clock != &clocksource_tod)
  185. return;
  186. /* Make userspace gettimeofday spin until we're done. */
  187. ++vdso_data->tb_update_count;
  188. smp_wmb();
  189. vdso_data->xtime_tod_stamp = tk->tkr_mono.cycle_last;
  190. vdso_data->xtime_clock_sec = tk->xtime_sec;
  191. vdso_data->xtime_clock_nsec = tk->tkr_mono.xtime_nsec;
  192. vdso_data->wtom_clock_sec =
  193. tk->xtime_sec + tk->wall_to_monotonic.tv_sec;
  194. vdso_data->wtom_clock_nsec = tk->tkr_mono.xtime_nsec +
  195. + ((u64) tk->wall_to_monotonic.tv_nsec << tk->tkr_mono.shift);
  196. nsecps = (u64) NSEC_PER_SEC << tk->tkr_mono.shift;
  197. while (vdso_data->wtom_clock_nsec >= nsecps) {
  198. vdso_data->wtom_clock_nsec -= nsecps;
  199. vdso_data->wtom_clock_sec++;
  200. }
  201. vdso_data->xtime_coarse_sec = tk->xtime_sec;
  202. vdso_data->xtime_coarse_nsec =
  203. (long)(tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift);
  204. vdso_data->wtom_coarse_sec =
  205. vdso_data->xtime_coarse_sec + tk->wall_to_monotonic.tv_sec;
  206. vdso_data->wtom_coarse_nsec =
  207. vdso_data->xtime_coarse_nsec + tk->wall_to_monotonic.tv_nsec;
  208. while (vdso_data->wtom_coarse_nsec >= NSEC_PER_SEC) {
  209. vdso_data->wtom_coarse_nsec -= NSEC_PER_SEC;
  210. vdso_data->wtom_coarse_sec++;
  211. }
  212. vdso_data->tk_mult = tk->tkr_mono.mult;
  213. vdso_data->tk_shift = tk->tkr_mono.shift;
  214. smp_wmb();
  215. ++vdso_data->tb_update_count;
  216. }
  217. extern struct timezone sys_tz;
  218. void update_vsyscall_tz(void)
  219. {
  220. /* Make userspace gettimeofday spin until we're done. */
  221. ++vdso_data->tb_update_count;
  222. smp_wmb();
  223. vdso_data->tz_minuteswest = sys_tz.tz_minuteswest;
  224. vdso_data->tz_dsttime = sys_tz.tz_dsttime;
  225. smp_wmb();
  226. ++vdso_data->tb_update_count;
  227. }
  228. /*
  229. * Initialize the TOD clock and the CPU timer of
  230. * the boot cpu.
  231. */
  232. void __init time_init(void)
  233. {
  234. /* Reset time synchronization interfaces. */
  235. etr_reset();
  236. stp_reset();
  237. /* request the clock comparator external interrupt */
  238. if (register_external_irq(EXT_IRQ_CLK_COMP, clock_comparator_interrupt))
  239. panic("Couldn't request external interrupt 0x1004");
  240. /* request the timing alert external interrupt */
  241. if (register_external_irq(EXT_IRQ_TIMING_ALERT, timing_alert_interrupt))
  242. panic("Couldn't request external interrupt 0x1406");
  243. if (__clocksource_register(&clocksource_tod) != 0)
  244. panic("Could not register TOD clock source");
  245. /* Enable TOD clock interrupts on the boot cpu. */
  246. init_cpu_timer();
  247. /* Enable cpu timer interrupts on the boot cpu. */
  248. vtime_init();
  249. }
  250. /*
  251. * The time is "clock". old is what we think the time is.
  252. * Adjust the value by a multiple of jiffies and add the delta to ntp.
  253. * "delay" is an approximation how long the synchronization took. If
  254. * the time correction is positive, then "delay" is subtracted from
  255. * the time difference and only the remaining part is passed to ntp.
  256. */
  257. static unsigned long long adjust_time(unsigned long long old,
  258. unsigned long long clock,
  259. unsigned long long delay)
  260. {
  261. unsigned long long delta, ticks;
  262. struct timex adjust;
  263. if (clock > old) {
  264. /* It is later than we thought. */
  265. delta = ticks = clock - old;
  266. delta = ticks = (delta < delay) ? 0 : delta - delay;
  267. delta -= do_div(ticks, CLK_TICKS_PER_JIFFY);
  268. adjust.offset = ticks * (1000000 / HZ);
  269. } else {
  270. /* It is earlier than we thought. */
  271. delta = ticks = old - clock;
  272. delta -= do_div(ticks, CLK_TICKS_PER_JIFFY);
  273. delta = -delta;
  274. adjust.offset = -ticks * (1000000 / HZ);
  275. }
  276. sched_clock_base_cc += delta;
  277. if (adjust.offset != 0) {
  278. pr_notice("The ETR interface has adjusted the clock "
  279. "by %li microseconds\n", adjust.offset);
  280. adjust.modes = ADJ_OFFSET_SINGLESHOT;
  281. do_adjtimex(&adjust);
  282. }
  283. return delta;
  284. }
  285. static DEFINE_PER_CPU(atomic_t, clock_sync_word);
  286. static DEFINE_MUTEX(clock_sync_mutex);
  287. static unsigned long clock_sync_flags;
  288. #define CLOCK_SYNC_HAS_ETR 0
  289. #define CLOCK_SYNC_HAS_STP 1
  290. #define CLOCK_SYNC_ETR 2
  291. #define CLOCK_SYNC_STP 3
  292. /*
  293. * The synchronous get_clock function. It will write the current clock
  294. * value to the clock pointer and return 0 if the clock is in sync with
  295. * the external time source. If the clock mode is local it will return
  296. * -EOPNOTSUPP and -EAGAIN if the clock is not in sync with the external
  297. * reference.
  298. */
  299. int get_sync_clock(unsigned long long *clock)
  300. {
  301. atomic_t *sw_ptr;
  302. unsigned int sw0, sw1;
  303. sw_ptr = &get_cpu_var(clock_sync_word);
  304. sw0 = atomic_read(sw_ptr);
  305. *clock = get_tod_clock();
  306. sw1 = atomic_read(sw_ptr);
  307. put_cpu_var(clock_sync_word);
  308. if (sw0 == sw1 && (sw0 & 0x80000000U))
  309. /* Success: time is in sync. */
  310. return 0;
  311. if (!test_bit(CLOCK_SYNC_HAS_ETR, &clock_sync_flags) &&
  312. !test_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags))
  313. return -EOPNOTSUPP;
  314. if (!test_bit(CLOCK_SYNC_ETR, &clock_sync_flags) &&
  315. !test_bit(CLOCK_SYNC_STP, &clock_sync_flags))
  316. return -EACCES;
  317. return -EAGAIN;
  318. }
  319. EXPORT_SYMBOL(get_sync_clock);
  320. /*
  321. * Make get_sync_clock return -EAGAIN.
  322. */
  323. static void disable_sync_clock(void *dummy)
  324. {
  325. atomic_t *sw_ptr = this_cpu_ptr(&clock_sync_word);
  326. /*
  327. * Clear the in-sync bit 2^31. All get_sync_clock calls will
  328. * fail until the sync bit is turned back on. In addition
  329. * increase the "sequence" counter to avoid the race of an
  330. * etr event and the complete recovery against get_sync_clock.
  331. */
  332. atomic_andnot(0x80000000, sw_ptr);
  333. atomic_inc(sw_ptr);
  334. }
  335. /*
  336. * Make get_sync_clock return 0 again.
  337. * Needs to be called from a context disabled for preemption.
  338. */
  339. static void enable_sync_clock(void)
  340. {
  341. atomic_t *sw_ptr = this_cpu_ptr(&clock_sync_word);
  342. atomic_or(0x80000000, sw_ptr);
  343. }
  344. /*
  345. * Function to check if the clock is in sync.
  346. */
  347. static inline int check_sync_clock(void)
  348. {
  349. atomic_t *sw_ptr;
  350. int rc;
  351. sw_ptr = &get_cpu_var(clock_sync_word);
  352. rc = (atomic_read(sw_ptr) & 0x80000000U) != 0;
  353. put_cpu_var(clock_sync_word);
  354. return rc;
  355. }
  356. /* Single threaded workqueue used for etr and stp sync events */
  357. static struct workqueue_struct *time_sync_wq;
  358. static void __init time_init_wq(void)
  359. {
  360. if (time_sync_wq)
  361. return;
  362. time_sync_wq = create_singlethread_workqueue("timesync");
  363. }
  364. /*
  365. * External Time Reference (ETR) code.
  366. */
  367. static int etr_port0_online;
  368. static int etr_port1_online;
  369. static int etr_steai_available;
  370. static int __init early_parse_etr(char *p)
  371. {
  372. if (strncmp(p, "off", 3) == 0)
  373. etr_port0_online = etr_port1_online = 0;
  374. else if (strncmp(p, "port0", 5) == 0)
  375. etr_port0_online = 1;
  376. else if (strncmp(p, "port1", 5) == 0)
  377. etr_port1_online = 1;
  378. else if (strncmp(p, "on", 2) == 0)
  379. etr_port0_online = etr_port1_online = 1;
  380. return 0;
  381. }
  382. early_param("etr", early_parse_etr);
  383. enum etr_event {
  384. ETR_EVENT_PORT0_CHANGE,
  385. ETR_EVENT_PORT1_CHANGE,
  386. ETR_EVENT_PORT_ALERT,
  387. ETR_EVENT_SYNC_CHECK,
  388. ETR_EVENT_SWITCH_LOCAL,
  389. ETR_EVENT_UPDATE,
  390. };
  391. /*
  392. * Valid bit combinations of the eacr register are (x = don't care):
  393. * e0 e1 dp p0 p1 ea es sl
  394. * 0 0 x 0 0 0 0 0 initial, disabled state
  395. * 0 0 x 0 1 1 0 0 port 1 online
  396. * 0 0 x 1 0 1 0 0 port 0 online
  397. * 0 0 x 1 1 1 0 0 both ports online
  398. * 0 1 x 0 1 1 0 0 port 1 online and usable, ETR or PPS mode
  399. * 0 1 x 0 1 1 0 1 port 1 online, usable and ETR mode
  400. * 0 1 x 0 1 1 1 0 port 1 online, usable, PPS mode, in-sync
  401. * 0 1 x 0 1 1 1 1 port 1 online, usable, ETR mode, in-sync
  402. * 0 1 x 1 1 1 0 0 both ports online, port 1 usable
  403. * 0 1 x 1 1 1 1 0 both ports online, port 1 usable, PPS mode, in-sync
  404. * 0 1 x 1 1 1 1 1 both ports online, port 1 usable, ETR mode, in-sync
  405. * 1 0 x 1 0 1 0 0 port 0 online and usable, ETR or PPS mode
  406. * 1 0 x 1 0 1 0 1 port 0 online, usable and ETR mode
  407. * 1 0 x 1 0 1 1 0 port 0 online, usable, PPS mode, in-sync
  408. * 1 0 x 1 0 1 1 1 port 0 online, usable, ETR mode, in-sync
  409. * 1 0 x 1 1 1 0 0 both ports online, port 0 usable
  410. * 1 0 x 1 1 1 1 0 both ports online, port 0 usable, PPS mode, in-sync
  411. * 1 0 x 1 1 1 1 1 both ports online, port 0 usable, ETR mode, in-sync
  412. * 1 1 x 1 1 1 1 0 both ports online & usable, ETR, in-sync
  413. * 1 1 x 1 1 1 1 1 both ports online & usable, ETR, in-sync
  414. */
  415. static struct etr_eacr etr_eacr;
  416. static u64 etr_tolec; /* time of last eacr update */
  417. static struct etr_aib etr_port0;
  418. static int etr_port0_uptodate;
  419. static struct etr_aib etr_port1;
  420. static int etr_port1_uptodate;
  421. static unsigned long etr_events;
  422. static struct timer_list etr_timer;
  423. static void etr_timeout(unsigned long dummy);
  424. static void etr_work_fn(struct work_struct *work);
  425. static DEFINE_MUTEX(etr_work_mutex);
  426. static DECLARE_WORK(etr_work, etr_work_fn);
  427. /*
  428. * Reset ETR attachment.
  429. */
  430. static void etr_reset(void)
  431. {
  432. etr_eacr = (struct etr_eacr) {
  433. .e0 = 0, .e1 = 0, ._pad0 = 4, .dp = 0,
  434. .p0 = 0, .p1 = 0, ._pad1 = 0, .ea = 0,
  435. .es = 0, .sl = 0 };
  436. if (etr_setr(&etr_eacr) == 0) {
  437. etr_tolec = get_tod_clock();
  438. set_bit(CLOCK_SYNC_HAS_ETR, &clock_sync_flags);
  439. if (etr_port0_online && etr_port1_online)
  440. set_bit(CLOCK_SYNC_ETR, &clock_sync_flags);
  441. } else if (etr_port0_online || etr_port1_online) {
  442. pr_warning("The real or virtual hardware system does "
  443. "not provide an ETR interface\n");
  444. etr_port0_online = etr_port1_online = 0;
  445. }
  446. }
  447. static int __init etr_init(void)
  448. {
  449. struct etr_aib aib;
  450. if (!test_bit(CLOCK_SYNC_HAS_ETR, &clock_sync_flags))
  451. return 0;
  452. time_init_wq();
  453. /* Check if this machine has the steai instruction. */
  454. if (etr_steai(&aib, ETR_STEAI_STEPPING_PORT) == 0)
  455. etr_steai_available = 1;
  456. setup_timer(&etr_timer, etr_timeout, 0UL);
  457. if (etr_port0_online) {
  458. set_bit(ETR_EVENT_PORT0_CHANGE, &etr_events);
  459. queue_work(time_sync_wq, &etr_work);
  460. }
  461. if (etr_port1_online) {
  462. set_bit(ETR_EVENT_PORT1_CHANGE, &etr_events);
  463. queue_work(time_sync_wq, &etr_work);
  464. }
  465. return 0;
  466. }
  467. arch_initcall(etr_init);
  468. /*
  469. * Two sorts of ETR machine checks. The architecture reads:
  470. * "When a machine-check niterruption occurs and if a switch-to-local or
  471. * ETR-sync-check interrupt request is pending but disabled, this pending
  472. * disabled interruption request is indicated and is cleared".
  473. * Which means that we can get etr_switch_to_local events from the machine
  474. * check handler although the interruption condition is disabled. Lovely..
  475. */
  476. /*
  477. * Switch to local machine check. This is called when the last usable
  478. * ETR port goes inactive. After switch to local the clock is not in sync.
  479. */
  480. int etr_switch_to_local(void)
  481. {
  482. if (!etr_eacr.sl)
  483. return 0;
  484. disable_sync_clock(NULL);
  485. if (!test_and_set_bit(ETR_EVENT_SWITCH_LOCAL, &etr_events)) {
  486. etr_eacr.es = etr_eacr.sl = 0;
  487. etr_setr(&etr_eacr);
  488. return 1;
  489. }
  490. return 0;
  491. }
  492. /*
  493. * ETR sync check machine check. This is called when the ETR OTE and the
  494. * local clock OTE are farther apart than the ETR sync check tolerance.
  495. * After a ETR sync check the clock is not in sync. The machine check
  496. * is broadcasted to all cpus at the same time.
  497. */
  498. int etr_sync_check(void)
  499. {
  500. if (!etr_eacr.es)
  501. return 0;
  502. disable_sync_clock(NULL);
  503. if (!test_and_set_bit(ETR_EVENT_SYNC_CHECK, &etr_events)) {
  504. etr_eacr.es = 0;
  505. etr_setr(&etr_eacr);
  506. return 1;
  507. }
  508. return 0;
  509. }
  510. void etr_queue_work(void)
  511. {
  512. queue_work(time_sync_wq, &etr_work);
  513. }
  514. /*
  515. * ETR timing alert. There are two causes:
  516. * 1) port state change, check the usability of the port
  517. * 2) port alert, one of the ETR-data-validity bits (v1-v2 bits of the
  518. * sldr-status word) or ETR-data word 1 (edf1) or ETR-data word 3 (edf3)
  519. * or ETR-data word 4 (edf4) has changed.
  520. */
  521. static void etr_timing_alert(struct etr_irq_parm *intparm)
  522. {
  523. if (intparm->pc0)
  524. /* ETR port 0 state change. */
  525. set_bit(ETR_EVENT_PORT0_CHANGE, &etr_events);
  526. if (intparm->pc1)
  527. /* ETR port 1 state change. */
  528. set_bit(ETR_EVENT_PORT1_CHANGE, &etr_events);
  529. if (intparm->eai)
  530. /*
  531. * ETR port alert on either port 0, 1 or both.
  532. * Both ports are not up-to-date now.
  533. */
  534. set_bit(ETR_EVENT_PORT_ALERT, &etr_events);
  535. queue_work(time_sync_wq, &etr_work);
  536. }
  537. static void etr_timeout(unsigned long dummy)
  538. {
  539. set_bit(ETR_EVENT_UPDATE, &etr_events);
  540. queue_work(time_sync_wq, &etr_work);
  541. }
  542. /*
  543. * Check if the etr mode is pss.
  544. */
  545. static inline int etr_mode_is_pps(struct etr_eacr eacr)
  546. {
  547. return eacr.es && !eacr.sl;
  548. }
  549. /*
  550. * Check if the etr mode is etr.
  551. */
  552. static inline int etr_mode_is_etr(struct etr_eacr eacr)
  553. {
  554. return eacr.es && eacr.sl;
  555. }
  556. /*
  557. * Check if the port can be used for TOD synchronization.
  558. * For PPS mode the port has to receive OTEs. For ETR mode
  559. * the port has to receive OTEs, the ETR stepping bit has to
  560. * be zero and the validity bits for data frame 1, 2, and 3
  561. * have to be 1.
  562. */
  563. static int etr_port_valid(struct etr_aib *aib, int port)
  564. {
  565. unsigned int psc;
  566. /* Check that this port is receiving OTEs. */
  567. if (aib->tsp == 0)
  568. return 0;
  569. psc = port ? aib->esw.psc1 : aib->esw.psc0;
  570. if (psc == etr_lpsc_pps_mode)
  571. return 1;
  572. if (psc == etr_lpsc_operational_step)
  573. return !aib->esw.y && aib->slsw.v1 &&
  574. aib->slsw.v2 && aib->slsw.v3;
  575. return 0;
  576. }
  577. /*
  578. * Check if two ports are on the same network.
  579. */
  580. static int etr_compare_network(struct etr_aib *aib1, struct etr_aib *aib2)
  581. {
  582. // FIXME: any other fields we have to compare?
  583. return aib1->edf1.net_id == aib2->edf1.net_id;
  584. }
  585. /*
  586. * Wrapper for etr_stei that converts physical port states
  587. * to logical port states to be consistent with the output
  588. * of stetr (see etr_psc vs. etr_lpsc).
  589. */
  590. static void etr_steai_cv(struct etr_aib *aib, unsigned int func)
  591. {
  592. BUG_ON(etr_steai(aib, func) != 0);
  593. /* Convert port state to logical port state. */
  594. if (aib->esw.psc0 == 1)
  595. aib->esw.psc0 = 2;
  596. else if (aib->esw.psc0 == 0 && aib->esw.p == 0)
  597. aib->esw.psc0 = 1;
  598. if (aib->esw.psc1 == 1)
  599. aib->esw.psc1 = 2;
  600. else if (aib->esw.psc1 == 0 && aib->esw.p == 1)
  601. aib->esw.psc1 = 1;
  602. }
  603. /*
  604. * Check if the aib a2 is still connected to the same attachment as
  605. * aib a1, the etv values differ by one and a2 is valid.
  606. */
  607. static int etr_aib_follows(struct etr_aib *a1, struct etr_aib *a2, int p)
  608. {
  609. int state_a1, state_a2;
  610. /* Paranoia check: e0/e1 should better be the same. */
  611. if (a1->esw.eacr.e0 != a2->esw.eacr.e0 ||
  612. a1->esw.eacr.e1 != a2->esw.eacr.e1)
  613. return 0;
  614. /* Still connected to the same etr ? */
  615. state_a1 = p ? a1->esw.psc1 : a1->esw.psc0;
  616. state_a2 = p ? a2->esw.psc1 : a2->esw.psc0;
  617. if (state_a1 == etr_lpsc_operational_step) {
  618. if (state_a2 != etr_lpsc_operational_step ||
  619. a1->edf1.net_id != a2->edf1.net_id ||
  620. a1->edf1.etr_id != a2->edf1.etr_id ||
  621. a1->edf1.etr_pn != a2->edf1.etr_pn)
  622. return 0;
  623. } else if (state_a2 != etr_lpsc_pps_mode)
  624. return 0;
  625. /* The ETV value of a2 needs to be ETV of a1 + 1. */
  626. if (a1->edf2.etv + 1 != a2->edf2.etv)
  627. return 0;
  628. if (!etr_port_valid(a2, p))
  629. return 0;
  630. return 1;
  631. }
  632. struct clock_sync_data {
  633. atomic_t cpus;
  634. int in_sync;
  635. unsigned long long fixup_cc;
  636. int etr_port;
  637. struct etr_aib *etr_aib;
  638. };
  639. static void clock_sync_cpu(struct clock_sync_data *sync)
  640. {
  641. atomic_dec(&sync->cpus);
  642. enable_sync_clock();
  643. /*
  644. * This looks like a busy wait loop but it isn't. etr_sync_cpus
  645. * is called on all other cpus while the TOD clocks is stopped.
  646. * __udelay will stop the cpu on an enabled wait psw until the
  647. * TOD is running again.
  648. */
  649. while (sync->in_sync == 0) {
  650. __udelay(1);
  651. /*
  652. * A different cpu changes *in_sync. Therefore use
  653. * barrier() to force memory access.
  654. */
  655. barrier();
  656. }
  657. if (sync->in_sync != 1)
  658. /* Didn't work. Clear per-cpu in sync bit again. */
  659. disable_sync_clock(NULL);
  660. /*
  661. * This round of TOD syncing is done. Set the clock comparator
  662. * to the next tick and let the processor continue.
  663. */
  664. fixup_clock_comparator(sync->fixup_cc);
  665. }
  666. /*
  667. * Sync the TOD clock using the port referred to by aibp. This port
  668. * has to be enabled and the other port has to be disabled. The
  669. * last eacr update has to be more than 1.6 seconds in the past.
  670. */
  671. static int etr_sync_clock(void *data)
  672. {
  673. static int first;
  674. unsigned long long clock, old_clock, clock_delta, delay, delta;
  675. struct clock_sync_data *etr_sync;
  676. struct etr_aib *sync_port, *aib;
  677. int port;
  678. int rc;
  679. etr_sync = data;
  680. if (xchg(&first, 1) == 1) {
  681. /* Slave */
  682. clock_sync_cpu(etr_sync);
  683. return 0;
  684. }
  685. /* Wait until all other cpus entered the sync function. */
  686. while (atomic_read(&etr_sync->cpus) != 0)
  687. cpu_relax();
  688. port = etr_sync->etr_port;
  689. aib = etr_sync->etr_aib;
  690. sync_port = (port == 0) ? &etr_port0 : &etr_port1;
  691. enable_sync_clock();
  692. /* Set clock to next OTE. */
  693. __ctl_set_bit(14, 21);
  694. __ctl_set_bit(0, 29);
  695. clock = ((unsigned long long) (aib->edf2.etv + 1)) << 32;
  696. old_clock = get_tod_clock();
  697. if (set_tod_clock(clock) == 0) {
  698. __udelay(1); /* Wait for the clock to start. */
  699. __ctl_clear_bit(0, 29);
  700. __ctl_clear_bit(14, 21);
  701. etr_stetr(aib);
  702. /* Adjust Linux timing variables. */
  703. delay = (unsigned long long)
  704. (aib->edf2.etv - sync_port->edf2.etv) << 32;
  705. delta = adjust_time(old_clock, clock, delay);
  706. clock_delta = clock - old_clock;
  707. atomic_notifier_call_chain(&s390_epoch_delta_notifier, 0,
  708. &clock_delta);
  709. etr_sync->fixup_cc = delta;
  710. fixup_clock_comparator(delta);
  711. /* Verify that the clock is properly set. */
  712. if (!etr_aib_follows(sync_port, aib, port)) {
  713. /* Didn't work. */
  714. disable_sync_clock(NULL);
  715. etr_sync->in_sync = -EAGAIN;
  716. rc = -EAGAIN;
  717. } else {
  718. etr_sync->in_sync = 1;
  719. rc = 0;
  720. }
  721. } else {
  722. /* Could not set the clock ?!? */
  723. __ctl_clear_bit(0, 29);
  724. __ctl_clear_bit(14, 21);
  725. disable_sync_clock(NULL);
  726. etr_sync->in_sync = -EAGAIN;
  727. rc = -EAGAIN;
  728. }
  729. xchg(&first, 0);
  730. return rc;
  731. }
  732. static int etr_sync_clock_stop(struct etr_aib *aib, int port)
  733. {
  734. struct clock_sync_data etr_sync;
  735. struct etr_aib *sync_port;
  736. int follows;
  737. int rc;
  738. /* Check if the current aib is adjacent to the sync port aib. */
  739. sync_port = (port == 0) ? &etr_port0 : &etr_port1;
  740. follows = etr_aib_follows(sync_port, aib, port);
  741. memcpy(sync_port, aib, sizeof(*aib));
  742. if (!follows)
  743. return -EAGAIN;
  744. memset(&etr_sync, 0, sizeof(etr_sync));
  745. etr_sync.etr_aib = aib;
  746. etr_sync.etr_port = port;
  747. get_online_cpus();
  748. atomic_set(&etr_sync.cpus, num_online_cpus() - 1);
  749. rc = stop_machine(etr_sync_clock, &etr_sync, cpu_online_mask);
  750. put_online_cpus();
  751. return rc;
  752. }
  753. /*
  754. * Handle the immediate effects of the different events.
  755. * The port change event is used for online/offline changes.
  756. */
  757. static struct etr_eacr etr_handle_events(struct etr_eacr eacr)
  758. {
  759. if (test_and_clear_bit(ETR_EVENT_SYNC_CHECK, &etr_events))
  760. eacr.es = 0;
  761. if (test_and_clear_bit(ETR_EVENT_SWITCH_LOCAL, &etr_events))
  762. eacr.es = eacr.sl = 0;
  763. if (test_and_clear_bit(ETR_EVENT_PORT_ALERT, &etr_events))
  764. etr_port0_uptodate = etr_port1_uptodate = 0;
  765. if (test_and_clear_bit(ETR_EVENT_PORT0_CHANGE, &etr_events)) {
  766. if (eacr.e0)
  767. /*
  768. * Port change of an enabled port. We have to
  769. * assume that this can have caused an stepping
  770. * port switch.
  771. */
  772. etr_tolec = get_tod_clock();
  773. eacr.p0 = etr_port0_online;
  774. if (!eacr.p0)
  775. eacr.e0 = 0;
  776. etr_port0_uptodate = 0;
  777. }
  778. if (test_and_clear_bit(ETR_EVENT_PORT1_CHANGE, &etr_events)) {
  779. if (eacr.e1)
  780. /*
  781. * Port change of an enabled port. We have to
  782. * assume that this can have caused an stepping
  783. * port switch.
  784. */
  785. etr_tolec = get_tod_clock();
  786. eacr.p1 = etr_port1_online;
  787. if (!eacr.p1)
  788. eacr.e1 = 0;
  789. etr_port1_uptodate = 0;
  790. }
  791. clear_bit(ETR_EVENT_UPDATE, &etr_events);
  792. return eacr;
  793. }
  794. /*
  795. * Set up a timer that expires after the etr_tolec + 1.6 seconds if
  796. * one of the ports needs an update.
  797. */
  798. static void etr_set_tolec_timeout(unsigned long long now)
  799. {
  800. unsigned long micros;
  801. if ((!etr_eacr.p0 || etr_port0_uptodate) &&
  802. (!etr_eacr.p1 || etr_port1_uptodate))
  803. return;
  804. micros = (now > etr_tolec) ? ((now - etr_tolec) >> 12) : 0;
  805. micros = (micros > 1600000) ? 0 : 1600000 - micros;
  806. mod_timer(&etr_timer, jiffies + (micros * HZ) / 1000000 + 1);
  807. }
  808. /*
  809. * Set up a time that expires after 1/2 second.
  810. */
  811. static void etr_set_sync_timeout(void)
  812. {
  813. mod_timer(&etr_timer, jiffies + HZ/2);
  814. }
  815. /*
  816. * Update the aib information for one or both ports.
  817. */
  818. static struct etr_eacr etr_handle_update(struct etr_aib *aib,
  819. struct etr_eacr eacr)
  820. {
  821. /* With both ports disabled the aib information is useless. */
  822. if (!eacr.e0 && !eacr.e1)
  823. return eacr;
  824. /* Update port0 or port1 with aib stored in etr_work_fn. */
  825. if (aib->esw.q == 0) {
  826. /* Information for port 0 stored. */
  827. if (eacr.p0 && !etr_port0_uptodate) {
  828. etr_port0 = *aib;
  829. if (etr_port0_online)
  830. etr_port0_uptodate = 1;
  831. }
  832. } else {
  833. /* Information for port 1 stored. */
  834. if (eacr.p1 && !etr_port1_uptodate) {
  835. etr_port1 = *aib;
  836. if (etr_port0_online)
  837. etr_port1_uptodate = 1;
  838. }
  839. }
  840. /*
  841. * Do not try to get the alternate port aib if the clock
  842. * is not in sync yet.
  843. */
  844. if (!eacr.es || !check_sync_clock())
  845. return eacr;
  846. /*
  847. * If steai is available we can get the information about
  848. * the other port immediately. If only stetr is available the
  849. * data-port bit toggle has to be used.
  850. */
  851. if (etr_steai_available) {
  852. if (eacr.p0 && !etr_port0_uptodate) {
  853. etr_steai_cv(&etr_port0, ETR_STEAI_PORT_0);
  854. etr_port0_uptodate = 1;
  855. }
  856. if (eacr.p1 && !etr_port1_uptodate) {
  857. etr_steai_cv(&etr_port1, ETR_STEAI_PORT_1);
  858. etr_port1_uptodate = 1;
  859. }
  860. } else {
  861. /*
  862. * One port was updated above, if the other
  863. * port is not uptodate toggle dp bit.
  864. */
  865. if ((eacr.p0 && !etr_port0_uptodate) ||
  866. (eacr.p1 && !etr_port1_uptodate))
  867. eacr.dp ^= 1;
  868. else
  869. eacr.dp = 0;
  870. }
  871. return eacr;
  872. }
  873. /*
  874. * Write new etr control register if it differs from the current one.
  875. * Return 1 if etr_tolec has been updated as well.
  876. */
  877. static void etr_update_eacr(struct etr_eacr eacr)
  878. {
  879. int dp_changed;
  880. if (memcmp(&etr_eacr, &eacr, sizeof(eacr)) == 0)
  881. /* No change, return. */
  882. return;
  883. /*
  884. * The disable of an active port of the change of the data port
  885. * bit can/will cause a change in the data port.
  886. */
  887. dp_changed = etr_eacr.e0 > eacr.e0 || etr_eacr.e1 > eacr.e1 ||
  888. (etr_eacr.dp ^ eacr.dp) != 0;
  889. etr_eacr = eacr;
  890. etr_setr(&etr_eacr);
  891. if (dp_changed)
  892. etr_tolec = get_tod_clock();
  893. }
  894. /*
  895. * ETR work. In this function you'll find the main logic. In
  896. * particular this is the only function that calls etr_update_eacr(),
  897. * it "controls" the etr control register.
  898. */
  899. static void etr_work_fn(struct work_struct *work)
  900. {
  901. unsigned long long now;
  902. struct etr_eacr eacr;
  903. struct etr_aib aib;
  904. int sync_port;
  905. /* prevent multiple execution. */
  906. mutex_lock(&etr_work_mutex);
  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(disable_sync_clock, NULL, 1);
  917. del_timer_sync(&etr_timer);
  918. etr_update_eacr(eacr);
  919. goto out_unlock;
  920. }
  921. /* Store aib to get the current ETR status word. */
  922. BUG_ON(etr_stetr(&aib) != 0);
  923. etr_port0.esw = etr_port1.esw = aib.esw; /* Copy status word. */
  924. now = get_tod_clock();
  925. /*
  926. * Update the port information if the last stepping port change
  927. * or data port change is older than 1.6 seconds.
  928. */
  929. if (now >= etr_tolec + (1600000 << 12))
  930. eacr = etr_handle_update(&aib, eacr);
  931. /*
  932. * Select ports to enable. The preferred synchronization mode is PPS.
  933. * If a port can be enabled depends on a number of things:
  934. * 1) The port needs to be online and uptodate. A port is not
  935. * disabled just because it is not uptodate, but it is only
  936. * enabled if it is uptodate.
  937. * 2) The port needs to have the same mode (pps / etr).
  938. * 3) The port needs to be usable -> etr_port_valid() == 1
  939. * 4) To enable the second port the clock needs to be in sync.
  940. * 5) If both ports are useable and are ETR ports, the network id
  941. * has to be the same.
  942. * The eacr.sl bit is used to indicate etr mode vs. pps mode.
  943. */
  944. if (eacr.p0 && aib.esw.psc0 == etr_lpsc_pps_mode) {
  945. eacr.sl = 0;
  946. eacr.e0 = 1;
  947. if (!etr_mode_is_pps(etr_eacr))
  948. eacr.es = 0;
  949. if (!eacr.es || !eacr.p1 || aib.esw.psc1 != etr_lpsc_pps_mode)
  950. eacr.e1 = 0;
  951. // FIXME: uptodate checks ?
  952. else if (etr_port0_uptodate && etr_port1_uptodate)
  953. eacr.e1 = 1;
  954. sync_port = (etr_port0_uptodate &&
  955. etr_port_valid(&etr_port0, 0)) ? 0 : -1;
  956. } else if (eacr.p1 && aib.esw.psc1 == etr_lpsc_pps_mode) {
  957. eacr.sl = 0;
  958. eacr.e0 = 0;
  959. eacr.e1 = 1;
  960. if (!etr_mode_is_pps(etr_eacr))
  961. eacr.es = 0;
  962. sync_port = (etr_port1_uptodate &&
  963. etr_port_valid(&etr_port1, 1)) ? 1 : -1;
  964. } else if (eacr.p0 && aib.esw.psc0 == etr_lpsc_operational_step) {
  965. eacr.sl = 1;
  966. eacr.e0 = 1;
  967. if (!etr_mode_is_etr(etr_eacr))
  968. eacr.es = 0;
  969. if (!eacr.es || !eacr.p1 ||
  970. aib.esw.psc1 != etr_lpsc_operational_alt)
  971. eacr.e1 = 0;
  972. else if (etr_port0_uptodate && etr_port1_uptodate &&
  973. etr_compare_network(&etr_port0, &etr_port1))
  974. eacr.e1 = 1;
  975. sync_port = (etr_port0_uptodate &&
  976. etr_port_valid(&etr_port0, 0)) ? 0 : -1;
  977. } else if (eacr.p1 && aib.esw.psc1 == etr_lpsc_operational_step) {
  978. eacr.sl = 1;
  979. eacr.e0 = 0;
  980. eacr.e1 = 1;
  981. if (!etr_mode_is_etr(etr_eacr))
  982. eacr.es = 0;
  983. sync_port = (etr_port1_uptodate &&
  984. etr_port_valid(&etr_port1, 1)) ? 1 : -1;
  985. } else {
  986. /* Both ports not usable. */
  987. eacr.es = eacr.sl = 0;
  988. sync_port = -1;
  989. }
  990. /*
  991. * If the clock is in sync just update the eacr and return.
  992. * If there is no valid sync port wait for a port update.
  993. */
  994. if ((eacr.es && check_sync_clock()) || sync_port < 0) {
  995. etr_update_eacr(eacr);
  996. etr_set_tolec_timeout(now);
  997. goto out_unlock;
  998. }
  999. /*
  1000. * Prepare control register for clock syncing
  1001. * (reset data port bit, set sync check control.
  1002. */
  1003. eacr.dp = 0;
  1004. eacr.es = 1;
  1005. /*
  1006. * Update eacr and try to synchronize the clock. If the update
  1007. * of eacr caused a stepping port switch (or if we have to
  1008. * assume that a stepping port switch has occurred) or the
  1009. * clock syncing failed, reset the sync check control bit
  1010. * and set up a timer to try again after 0.5 seconds
  1011. */
  1012. etr_update_eacr(eacr);
  1013. if (now < etr_tolec + (1600000 << 12) ||
  1014. etr_sync_clock_stop(&aib, sync_port) != 0) {
  1015. /* Sync failed. Try again in 1/2 second. */
  1016. eacr.es = 0;
  1017. etr_update_eacr(eacr);
  1018. etr_set_sync_timeout();
  1019. } else
  1020. etr_set_tolec_timeout(now);
  1021. out_unlock:
  1022. mutex_unlock(&etr_work_mutex);
  1023. }
  1024. /*
  1025. * Sysfs interface functions
  1026. */
  1027. static struct bus_type etr_subsys = {
  1028. .name = "etr",
  1029. .dev_name = "etr",
  1030. };
  1031. static struct device etr_port0_dev = {
  1032. .id = 0,
  1033. .bus = &etr_subsys,
  1034. };
  1035. static struct device etr_port1_dev = {
  1036. .id = 1,
  1037. .bus = &etr_subsys,
  1038. };
  1039. /*
  1040. * ETR subsys attributes
  1041. */
  1042. static ssize_t etr_stepping_port_show(struct device *dev,
  1043. struct device_attribute *attr,
  1044. char *buf)
  1045. {
  1046. return sprintf(buf, "%i\n", etr_port0.esw.p);
  1047. }
  1048. static DEVICE_ATTR(stepping_port, 0400, etr_stepping_port_show, NULL);
  1049. static ssize_t etr_stepping_mode_show(struct device *dev,
  1050. struct device_attribute *attr,
  1051. char *buf)
  1052. {
  1053. char *mode_str;
  1054. if (etr_mode_is_pps(etr_eacr))
  1055. mode_str = "pps";
  1056. else if (etr_mode_is_etr(etr_eacr))
  1057. mode_str = "etr";
  1058. else
  1059. mode_str = "local";
  1060. return sprintf(buf, "%s\n", mode_str);
  1061. }
  1062. static DEVICE_ATTR(stepping_mode, 0400, etr_stepping_mode_show, NULL);
  1063. /*
  1064. * ETR port attributes
  1065. */
  1066. static inline struct etr_aib *etr_aib_from_dev(struct device *dev)
  1067. {
  1068. if (dev == &etr_port0_dev)
  1069. return etr_port0_online ? &etr_port0 : NULL;
  1070. else
  1071. return etr_port1_online ? &etr_port1 : NULL;
  1072. }
  1073. static ssize_t etr_online_show(struct device *dev,
  1074. struct device_attribute *attr,
  1075. char *buf)
  1076. {
  1077. unsigned int online;
  1078. online = (dev == &etr_port0_dev) ? etr_port0_online : etr_port1_online;
  1079. return sprintf(buf, "%i\n", online);
  1080. }
  1081. static ssize_t etr_online_store(struct device *dev,
  1082. struct device_attribute *attr,
  1083. const char *buf, size_t count)
  1084. {
  1085. unsigned int value;
  1086. value = simple_strtoul(buf, NULL, 0);
  1087. if (value != 0 && value != 1)
  1088. return -EINVAL;
  1089. if (!test_bit(CLOCK_SYNC_HAS_ETR, &clock_sync_flags))
  1090. return -EOPNOTSUPP;
  1091. mutex_lock(&clock_sync_mutex);
  1092. if (dev == &etr_port0_dev) {
  1093. if (etr_port0_online == value)
  1094. goto out; /* Nothing to do. */
  1095. etr_port0_online = value;
  1096. if (etr_port0_online && etr_port1_online)
  1097. set_bit(CLOCK_SYNC_ETR, &clock_sync_flags);
  1098. else
  1099. clear_bit(CLOCK_SYNC_ETR, &clock_sync_flags);
  1100. set_bit(ETR_EVENT_PORT0_CHANGE, &etr_events);
  1101. queue_work(time_sync_wq, &etr_work);
  1102. } else {
  1103. if (etr_port1_online == value)
  1104. goto out; /* Nothing to do. */
  1105. etr_port1_online = value;
  1106. if (etr_port0_online && etr_port1_online)
  1107. set_bit(CLOCK_SYNC_ETR, &clock_sync_flags);
  1108. else
  1109. clear_bit(CLOCK_SYNC_ETR, &clock_sync_flags);
  1110. set_bit(ETR_EVENT_PORT1_CHANGE, &etr_events);
  1111. queue_work(time_sync_wq, &etr_work);
  1112. }
  1113. out:
  1114. mutex_unlock(&clock_sync_mutex);
  1115. return count;
  1116. }
  1117. static DEVICE_ATTR(online, 0600, etr_online_show, etr_online_store);
  1118. static ssize_t etr_stepping_control_show(struct device *dev,
  1119. struct device_attribute *attr,
  1120. char *buf)
  1121. {
  1122. return sprintf(buf, "%i\n", (dev == &etr_port0_dev) ?
  1123. etr_eacr.e0 : etr_eacr.e1);
  1124. }
  1125. static DEVICE_ATTR(stepping_control, 0400, etr_stepping_control_show, NULL);
  1126. static ssize_t etr_mode_code_show(struct device *dev,
  1127. struct device_attribute *attr, char *buf)
  1128. {
  1129. if (!etr_port0_online && !etr_port1_online)
  1130. /* Status word is not uptodate if both ports are offline. */
  1131. return -ENODATA;
  1132. return sprintf(buf, "%i\n", (dev == &etr_port0_dev) ?
  1133. etr_port0.esw.psc0 : etr_port0.esw.psc1);
  1134. }
  1135. static DEVICE_ATTR(state_code, 0400, etr_mode_code_show, NULL);
  1136. static ssize_t etr_untuned_show(struct device *dev,
  1137. struct device_attribute *attr, char *buf)
  1138. {
  1139. struct etr_aib *aib = etr_aib_from_dev(dev);
  1140. if (!aib || !aib->slsw.v1)
  1141. return -ENODATA;
  1142. return sprintf(buf, "%i\n", aib->edf1.u);
  1143. }
  1144. static DEVICE_ATTR(untuned, 0400, etr_untuned_show, NULL);
  1145. static ssize_t etr_network_id_show(struct device *dev,
  1146. struct device_attribute *attr, char *buf)
  1147. {
  1148. struct etr_aib *aib = etr_aib_from_dev(dev);
  1149. if (!aib || !aib->slsw.v1)
  1150. return -ENODATA;
  1151. return sprintf(buf, "%i\n", aib->edf1.net_id);
  1152. }
  1153. static DEVICE_ATTR(network, 0400, etr_network_id_show, NULL);
  1154. static ssize_t etr_id_show(struct device *dev,
  1155. struct device_attribute *attr, char *buf)
  1156. {
  1157. struct etr_aib *aib = etr_aib_from_dev(dev);
  1158. if (!aib || !aib->slsw.v1)
  1159. return -ENODATA;
  1160. return sprintf(buf, "%i\n", aib->edf1.etr_id);
  1161. }
  1162. static DEVICE_ATTR(id, 0400, etr_id_show, NULL);
  1163. static ssize_t etr_port_number_show(struct device *dev,
  1164. struct device_attribute *attr, char *buf)
  1165. {
  1166. struct etr_aib *aib = etr_aib_from_dev(dev);
  1167. if (!aib || !aib->slsw.v1)
  1168. return -ENODATA;
  1169. return sprintf(buf, "%i\n", aib->edf1.etr_pn);
  1170. }
  1171. static DEVICE_ATTR(port, 0400, etr_port_number_show, NULL);
  1172. static ssize_t etr_coupled_show(struct device *dev,
  1173. struct device_attribute *attr, char *buf)
  1174. {
  1175. struct etr_aib *aib = etr_aib_from_dev(dev);
  1176. if (!aib || !aib->slsw.v3)
  1177. return -ENODATA;
  1178. return sprintf(buf, "%i\n", aib->edf3.c);
  1179. }
  1180. static DEVICE_ATTR(coupled, 0400, etr_coupled_show, NULL);
  1181. static ssize_t etr_local_time_show(struct device *dev,
  1182. struct device_attribute *attr, char *buf)
  1183. {
  1184. struct etr_aib *aib = etr_aib_from_dev(dev);
  1185. if (!aib || !aib->slsw.v3)
  1186. return -ENODATA;
  1187. return sprintf(buf, "%i\n", aib->edf3.blto);
  1188. }
  1189. static DEVICE_ATTR(local_time, 0400, etr_local_time_show, NULL);
  1190. static ssize_t etr_utc_offset_show(struct device *dev,
  1191. struct device_attribute *attr, char *buf)
  1192. {
  1193. struct etr_aib *aib = etr_aib_from_dev(dev);
  1194. if (!aib || !aib->slsw.v3)
  1195. return -ENODATA;
  1196. return sprintf(buf, "%i\n", aib->edf3.buo);
  1197. }
  1198. static DEVICE_ATTR(utc_offset, 0400, etr_utc_offset_show, NULL);
  1199. static struct device_attribute *etr_port_attributes[] = {
  1200. &dev_attr_online,
  1201. &dev_attr_stepping_control,
  1202. &dev_attr_state_code,
  1203. &dev_attr_untuned,
  1204. &dev_attr_network,
  1205. &dev_attr_id,
  1206. &dev_attr_port,
  1207. &dev_attr_coupled,
  1208. &dev_attr_local_time,
  1209. &dev_attr_utc_offset,
  1210. NULL
  1211. };
  1212. static int __init etr_register_port(struct device *dev)
  1213. {
  1214. struct device_attribute **attr;
  1215. int rc;
  1216. rc = device_register(dev);
  1217. if (rc)
  1218. goto out;
  1219. for (attr = etr_port_attributes; *attr; attr++) {
  1220. rc = device_create_file(dev, *attr);
  1221. if (rc)
  1222. goto out_unreg;
  1223. }
  1224. return 0;
  1225. out_unreg:
  1226. for (; attr >= etr_port_attributes; attr--)
  1227. device_remove_file(dev, *attr);
  1228. device_unregister(dev);
  1229. out:
  1230. return rc;
  1231. }
  1232. static void __init etr_unregister_port(struct device *dev)
  1233. {
  1234. struct device_attribute **attr;
  1235. for (attr = etr_port_attributes; *attr; attr++)
  1236. device_remove_file(dev, *attr);
  1237. device_unregister(dev);
  1238. }
  1239. static int __init etr_init_sysfs(void)
  1240. {
  1241. int rc;
  1242. rc = subsys_system_register(&etr_subsys, NULL);
  1243. if (rc)
  1244. goto out;
  1245. rc = device_create_file(etr_subsys.dev_root, &dev_attr_stepping_port);
  1246. if (rc)
  1247. goto out_unreg_subsys;
  1248. rc = device_create_file(etr_subsys.dev_root, &dev_attr_stepping_mode);
  1249. if (rc)
  1250. goto out_remove_stepping_port;
  1251. rc = etr_register_port(&etr_port0_dev);
  1252. if (rc)
  1253. goto out_remove_stepping_mode;
  1254. rc = etr_register_port(&etr_port1_dev);
  1255. if (rc)
  1256. goto out_remove_port0;
  1257. return 0;
  1258. out_remove_port0:
  1259. etr_unregister_port(&etr_port0_dev);
  1260. out_remove_stepping_mode:
  1261. device_remove_file(etr_subsys.dev_root, &dev_attr_stepping_mode);
  1262. out_remove_stepping_port:
  1263. device_remove_file(etr_subsys.dev_root, &dev_attr_stepping_port);
  1264. out_unreg_subsys:
  1265. bus_unregister(&etr_subsys);
  1266. out:
  1267. return rc;
  1268. }
  1269. device_initcall(etr_init_sysfs);
  1270. /*
  1271. * Server Time Protocol (STP) code.
  1272. */
  1273. static int stp_online;
  1274. static struct stp_sstpi stp_info;
  1275. static void *stp_page;
  1276. static void stp_work_fn(struct work_struct *work);
  1277. static DEFINE_MUTEX(stp_work_mutex);
  1278. static DECLARE_WORK(stp_work, stp_work_fn);
  1279. static struct timer_list stp_timer;
  1280. static int __init early_parse_stp(char *p)
  1281. {
  1282. if (strncmp(p, "off", 3) == 0)
  1283. stp_online = 0;
  1284. else if (strncmp(p, "on", 2) == 0)
  1285. stp_online = 1;
  1286. return 0;
  1287. }
  1288. early_param("stp", early_parse_stp);
  1289. /*
  1290. * Reset STP attachment.
  1291. */
  1292. static void __init stp_reset(void)
  1293. {
  1294. int rc;
  1295. stp_page = (void *) get_zeroed_page(GFP_ATOMIC);
  1296. rc = chsc_sstpc(stp_page, STP_OP_CTRL, 0x0000);
  1297. if (rc == 0)
  1298. set_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags);
  1299. else if (stp_online) {
  1300. pr_warning("The real or virtual hardware system does "
  1301. "not provide an STP interface\n");
  1302. free_page((unsigned long) stp_page);
  1303. stp_page = NULL;
  1304. stp_online = 0;
  1305. }
  1306. }
  1307. static void stp_timeout(unsigned long dummy)
  1308. {
  1309. queue_work(time_sync_wq, &stp_work);
  1310. }
  1311. static int __init stp_init(void)
  1312. {
  1313. if (!test_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags))
  1314. return 0;
  1315. setup_timer(&stp_timer, stp_timeout, 0UL);
  1316. time_init_wq();
  1317. if (!stp_online)
  1318. return 0;
  1319. queue_work(time_sync_wq, &stp_work);
  1320. return 0;
  1321. }
  1322. arch_initcall(stp_init);
  1323. /*
  1324. * STP timing alert. There are three causes:
  1325. * 1) timing status change
  1326. * 2) link availability change
  1327. * 3) time control parameter change
  1328. * In all three cases we are only interested in the clock source state.
  1329. * If a STP clock source is now available use it.
  1330. */
  1331. static void stp_timing_alert(struct stp_irq_parm *intparm)
  1332. {
  1333. if (intparm->tsc || intparm->lac || intparm->tcpc)
  1334. queue_work(time_sync_wq, &stp_work);
  1335. }
  1336. /*
  1337. * STP sync check machine check. This is called when the timing state
  1338. * changes from the synchronized state to the unsynchronized state.
  1339. * After a STP sync check the clock is not in sync. The machine check
  1340. * is broadcasted to all cpus at the same time.
  1341. */
  1342. int stp_sync_check(void)
  1343. {
  1344. disable_sync_clock(NULL);
  1345. return 1;
  1346. }
  1347. /*
  1348. * STP island condition machine check. This is called when an attached
  1349. * server attempts to communicate over an STP link and the servers
  1350. * have matching CTN ids and have a valid stratum-1 configuration
  1351. * but the configurations do not match.
  1352. */
  1353. int stp_island_check(void)
  1354. {
  1355. disable_sync_clock(NULL);
  1356. return 1;
  1357. }
  1358. void stp_queue_work(void)
  1359. {
  1360. queue_work(time_sync_wq, &stp_work);
  1361. }
  1362. static int stp_sync_clock(void *data)
  1363. {
  1364. static int first;
  1365. unsigned long long old_clock, delta, new_clock, clock_delta;
  1366. struct clock_sync_data *stp_sync;
  1367. int rc;
  1368. stp_sync = data;
  1369. if (xchg(&first, 1) == 1) {
  1370. /* Slave */
  1371. clock_sync_cpu(stp_sync);
  1372. return 0;
  1373. }
  1374. /* Wait until all other cpus entered the sync function. */
  1375. while (atomic_read(&stp_sync->cpus) != 0)
  1376. cpu_relax();
  1377. enable_sync_clock();
  1378. rc = 0;
  1379. if (stp_info.todoff[0] || stp_info.todoff[1] ||
  1380. stp_info.todoff[2] || stp_info.todoff[3] ||
  1381. stp_info.tmd != 2) {
  1382. old_clock = get_tod_clock();
  1383. rc = chsc_sstpc(stp_page, STP_OP_SYNC, 0);
  1384. if (rc == 0) {
  1385. new_clock = get_tod_clock();
  1386. delta = adjust_time(old_clock, new_clock, 0);
  1387. clock_delta = new_clock - old_clock;
  1388. atomic_notifier_call_chain(&s390_epoch_delta_notifier,
  1389. 0, &clock_delta);
  1390. fixup_clock_comparator(delta);
  1391. rc = chsc_sstpi(stp_page, &stp_info,
  1392. sizeof(struct stp_sstpi));
  1393. if (rc == 0 && stp_info.tmd != 2)
  1394. rc = -EAGAIN;
  1395. }
  1396. }
  1397. if (rc) {
  1398. disable_sync_clock(NULL);
  1399. stp_sync->in_sync = -EAGAIN;
  1400. } else
  1401. stp_sync->in_sync = 1;
  1402. xchg(&first, 0);
  1403. return 0;
  1404. }
  1405. /*
  1406. * STP work. Check for the STP state and take over the clock
  1407. * synchronization if the STP clock source is usable.
  1408. */
  1409. static void stp_work_fn(struct work_struct *work)
  1410. {
  1411. struct clock_sync_data stp_sync;
  1412. int rc;
  1413. /* prevent multiple execution. */
  1414. mutex_lock(&stp_work_mutex);
  1415. if (!stp_online) {
  1416. chsc_sstpc(stp_page, STP_OP_CTRL, 0x0000);
  1417. del_timer_sync(&stp_timer);
  1418. goto out_unlock;
  1419. }
  1420. rc = chsc_sstpc(stp_page, STP_OP_CTRL, 0xb0e0);
  1421. if (rc)
  1422. goto out_unlock;
  1423. rc = chsc_sstpi(stp_page, &stp_info, sizeof(struct stp_sstpi));
  1424. if (rc || stp_info.c == 0)
  1425. goto out_unlock;
  1426. /* Skip synchronization if the clock is already in sync. */
  1427. if (check_sync_clock())
  1428. goto out_unlock;
  1429. memset(&stp_sync, 0, sizeof(stp_sync));
  1430. get_online_cpus();
  1431. atomic_set(&stp_sync.cpus, num_online_cpus() - 1);
  1432. stop_machine(stp_sync_clock, &stp_sync, cpu_online_mask);
  1433. put_online_cpus();
  1434. if (!check_sync_clock())
  1435. /*
  1436. * There is a usable clock but the synchonization failed.
  1437. * Retry after a second.
  1438. */
  1439. mod_timer(&stp_timer, jiffies + HZ);
  1440. out_unlock:
  1441. mutex_unlock(&stp_work_mutex);
  1442. }
  1443. /*
  1444. * STP subsys sysfs interface functions
  1445. */
  1446. static struct bus_type stp_subsys = {
  1447. .name = "stp",
  1448. .dev_name = "stp",
  1449. };
  1450. static ssize_t stp_ctn_id_show(struct device *dev,
  1451. struct device_attribute *attr,
  1452. char *buf)
  1453. {
  1454. if (!stp_online)
  1455. return -ENODATA;
  1456. return sprintf(buf, "%016llx\n",
  1457. *(unsigned long long *) stp_info.ctnid);
  1458. }
  1459. static DEVICE_ATTR(ctn_id, 0400, stp_ctn_id_show, NULL);
  1460. static ssize_t stp_ctn_type_show(struct device *dev,
  1461. struct device_attribute *attr,
  1462. char *buf)
  1463. {
  1464. if (!stp_online)
  1465. return -ENODATA;
  1466. return sprintf(buf, "%i\n", stp_info.ctn);
  1467. }
  1468. static DEVICE_ATTR(ctn_type, 0400, stp_ctn_type_show, NULL);
  1469. static ssize_t stp_dst_offset_show(struct device *dev,
  1470. struct device_attribute *attr,
  1471. char *buf)
  1472. {
  1473. if (!stp_online || !(stp_info.vbits & 0x2000))
  1474. return -ENODATA;
  1475. return sprintf(buf, "%i\n", (int)(s16) stp_info.dsto);
  1476. }
  1477. static DEVICE_ATTR(dst_offset, 0400, stp_dst_offset_show, NULL);
  1478. static ssize_t stp_leap_seconds_show(struct device *dev,
  1479. struct device_attribute *attr,
  1480. char *buf)
  1481. {
  1482. if (!stp_online || !(stp_info.vbits & 0x8000))
  1483. return -ENODATA;
  1484. return sprintf(buf, "%i\n", (int)(s16) stp_info.leaps);
  1485. }
  1486. static DEVICE_ATTR(leap_seconds, 0400, stp_leap_seconds_show, NULL);
  1487. static ssize_t stp_stratum_show(struct device *dev,
  1488. struct device_attribute *attr,
  1489. char *buf)
  1490. {
  1491. if (!stp_online)
  1492. return -ENODATA;
  1493. return sprintf(buf, "%i\n", (int)(s16) stp_info.stratum);
  1494. }
  1495. static DEVICE_ATTR(stratum, 0400, stp_stratum_show, NULL);
  1496. static ssize_t stp_time_offset_show(struct device *dev,
  1497. struct device_attribute *attr,
  1498. char *buf)
  1499. {
  1500. if (!stp_online || !(stp_info.vbits & 0x0800))
  1501. return -ENODATA;
  1502. return sprintf(buf, "%i\n", (int) stp_info.tto);
  1503. }
  1504. static DEVICE_ATTR(time_offset, 0400, stp_time_offset_show, NULL);
  1505. static ssize_t stp_time_zone_offset_show(struct device *dev,
  1506. struct device_attribute *attr,
  1507. char *buf)
  1508. {
  1509. if (!stp_online || !(stp_info.vbits & 0x4000))
  1510. return -ENODATA;
  1511. return sprintf(buf, "%i\n", (int)(s16) stp_info.tzo);
  1512. }
  1513. static DEVICE_ATTR(time_zone_offset, 0400,
  1514. stp_time_zone_offset_show, NULL);
  1515. static ssize_t stp_timing_mode_show(struct device *dev,
  1516. struct device_attribute *attr,
  1517. char *buf)
  1518. {
  1519. if (!stp_online)
  1520. return -ENODATA;
  1521. return sprintf(buf, "%i\n", stp_info.tmd);
  1522. }
  1523. static DEVICE_ATTR(timing_mode, 0400, stp_timing_mode_show, NULL);
  1524. static ssize_t stp_timing_state_show(struct device *dev,
  1525. struct device_attribute *attr,
  1526. char *buf)
  1527. {
  1528. if (!stp_online)
  1529. return -ENODATA;
  1530. return sprintf(buf, "%i\n", stp_info.tst);
  1531. }
  1532. static DEVICE_ATTR(timing_state, 0400, stp_timing_state_show, NULL);
  1533. static ssize_t stp_online_show(struct device *dev,
  1534. struct device_attribute *attr,
  1535. char *buf)
  1536. {
  1537. return sprintf(buf, "%i\n", stp_online);
  1538. }
  1539. static ssize_t stp_online_store(struct device *dev,
  1540. struct device_attribute *attr,
  1541. const char *buf, size_t count)
  1542. {
  1543. unsigned int value;
  1544. value = simple_strtoul(buf, NULL, 0);
  1545. if (value != 0 && value != 1)
  1546. return -EINVAL;
  1547. if (!test_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags))
  1548. return -EOPNOTSUPP;
  1549. mutex_lock(&clock_sync_mutex);
  1550. stp_online = value;
  1551. if (stp_online)
  1552. set_bit(CLOCK_SYNC_STP, &clock_sync_flags);
  1553. else
  1554. clear_bit(CLOCK_SYNC_STP, &clock_sync_flags);
  1555. queue_work(time_sync_wq, &stp_work);
  1556. mutex_unlock(&clock_sync_mutex);
  1557. return count;
  1558. }
  1559. /*
  1560. * Can't use DEVICE_ATTR because the attribute should be named
  1561. * stp/online but dev_attr_online already exists in this file ..
  1562. */
  1563. static struct device_attribute dev_attr_stp_online = {
  1564. .attr = { .name = "online", .mode = 0600 },
  1565. .show = stp_online_show,
  1566. .store = stp_online_store,
  1567. };
  1568. static struct device_attribute *stp_attributes[] = {
  1569. &dev_attr_ctn_id,
  1570. &dev_attr_ctn_type,
  1571. &dev_attr_dst_offset,
  1572. &dev_attr_leap_seconds,
  1573. &dev_attr_stp_online,
  1574. &dev_attr_stratum,
  1575. &dev_attr_time_offset,
  1576. &dev_attr_time_zone_offset,
  1577. &dev_attr_timing_mode,
  1578. &dev_attr_timing_state,
  1579. NULL
  1580. };
  1581. static int __init stp_init_sysfs(void)
  1582. {
  1583. struct device_attribute **attr;
  1584. int rc;
  1585. rc = subsys_system_register(&stp_subsys, NULL);
  1586. if (rc)
  1587. goto out;
  1588. for (attr = stp_attributes; *attr; attr++) {
  1589. rc = device_create_file(stp_subsys.dev_root, *attr);
  1590. if (rc)
  1591. goto out_unreg;
  1592. }
  1593. return 0;
  1594. out_unreg:
  1595. for (; attr >= stp_attributes; attr--)
  1596. device_remove_file(stp_subsys.dev_root, *attr);
  1597. bus_unregister(&stp_subsys);
  1598. out:
  1599. return rc;
  1600. }
  1601. device_initcall(stp_init_sysfs);