xpc_main.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (c) 2004-2008 Silicon Graphics, Inc. All Rights Reserved.
  7. */
  8. /*
  9. * Cross Partition Communication (XPC) support - standard version.
  10. *
  11. * XPC provides a message passing capability that crosses partition
  12. * boundaries. This module is made up of two parts:
  13. *
  14. * partition This part detects the presence/absence of other
  15. * partitions. It provides a heartbeat and monitors
  16. * the heartbeats of other partitions.
  17. *
  18. * channel This part manages the channels and sends/receives
  19. * messages across them to/from other partitions.
  20. *
  21. * There are a couple of additional functions residing in XP, which
  22. * provide an interface to XPC for its users.
  23. *
  24. *
  25. * Caveats:
  26. *
  27. * . Currently on sn2, we have no way to determine which nasid an IRQ
  28. * came from. Thus, xpc_send_IRQ_sn2() does a remote amo write
  29. * followed by an IPI. The amo indicates where data is to be pulled
  30. * from, so after the IPI arrives, the remote partition checks the amo
  31. * word. The IPI can actually arrive before the amo however, so other
  32. * code must periodically check for this case. Also, remote amo
  33. * operations do not reliably time out. Thus we do a remote PIO read
  34. * solely to know whether the remote partition is down and whether we
  35. * should stop sending IPIs to it. This remote PIO read operation is
  36. * set up in a special nofault region so SAL knows to ignore (and
  37. * cleanup) any errors due to the remote amo write, PIO read, and/or
  38. * PIO write operations.
  39. *
  40. * If/when new hardware solves this IPI problem, we should abandon
  41. * the current approach.
  42. *
  43. */
  44. #include <linux/kernel.h>
  45. #include <linux/module.h>
  46. #include <linux/init.h>
  47. #include <linux/cache.h>
  48. #include <linux/interrupt.h>
  49. #include <linux/delay.h>
  50. #include <linux/reboot.h>
  51. #include <linux/completion.h>
  52. #include <linux/kdebug.h>
  53. #include <linux/kthread.h>
  54. #include <linux/uaccess.h>
  55. #include <asm/sn/intr.h>
  56. #include <asm/sn/sn_sal.h>
  57. #include "xpc.h"
  58. /* define two XPC debug device structures to be used with dev_dbg() et al */
  59. struct device_driver xpc_dbg_name = {
  60. .name = "xpc"
  61. };
  62. struct device xpc_part_dbg_subname = {
  63. .bus_id = {0}, /* set to "part" at xpc_init() time */
  64. .driver = &xpc_dbg_name
  65. };
  66. struct device xpc_chan_dbg_subname = {
  67. .bus_id = {0}, /* set to "chan" at xpc_init() time */
  68. .driver = &xpc_dbg_name
  69. };
  70. struct device *xpc_part = &xpc_part_dbg_subname;
  71. struct device *xpc_chan = &xpc_chan_dbg_subname;
  72. static int xpc_kdebug_ignore;
  73. /* systune related variables for /proc/sys directories */
  74. static int xpc_hb_interval = XPC_HB_DEFAULT_INTERVAL;
  75. static int xpc_hb_min_interval = 1;
  76. static int xpc_hb_max_interval = 10;
  77. static int xpc_hb_check_interval = XPC_HB_CHECK_DEFAULT_INTERVAL;
  78. static int xpc_hb_check_min_interval = 10;
  79. static int xpc_hb_check_max_interval = 120;
  80. int xpc_disengage_timelimit = XPC_DISENGAGE_DEFAULT_TIMELIMIT;
  81. static int xpc_disengage_min_timelimit; /* = 0 */
  82. static int xpc_disengage_max_timelimit = 120;
  83. static ctl_table xpc_sys_xpc_hb_dir[] = {
  84. {
  85. .ctl_name = CTL_UNNUMBERED,
  86. .procname = "hb_interval",
  87. .data = &xpc_hb_interval,
  88. .maxlen = sizeof(int),
  89. .mode = 0644,
  90. .proc_handler = &proc_dointvec_minmax,
  91. .strategy = &sysctl_intvec,
  92. .extra1 = &xpc_hb_min_interval,
  93. .extra2 = &xpc_hb_max_interval},
  94. {
  95. .ctl_name = CTL_UNNUMBERED,
  96. .procname = "hb_check_interval",
  97. .data = &xpc_hb_check_interval,
  98. .maxlen = sizeof(int),
  99. .mode = 0644,
  100. .proc_handler = &proc_dointvec_minmax,
  101. .strategy = &sysctl_intvec,
  102. .extra1 = &xpc_hb_check_min_interval,
  103. .extra2 = &xpc_hb_check_max_interval},
  104. {}
  105. };
  106. static ctl_table xpc_sys_xpc_dir[] = {
  107. {
  108. .ctl_name = CTL_UNNUMBERED,
  109. .procname = "hb",
  110. .mode = 0555,
  111. .child = xpc_sys_xpc_hb_dir},
  112. {
  113. .ctl_name = CTL_UNNUMBERED,
  114. .procname = "disengage_timelimit",
  115. .data = &xpc_disengage_timelimit,
  116. .maxlen = sizeof(int),
  117. .mode = 0644,
  118. .proc_handler = &proc_dointvec_minmax,
  119. .strategy = &sysctl_intvec,
  120. .extra1 = &xpc_disengage_min_timelimit,
  121. .extra2 = &xpc_disengage_max_timelimit},
  122. {}
  123. };
  124. static ctl_table xpc_sys_dir[] = {
  125. {
  126. .ctl_name = CTL_UNNUMBERED,
  127. .procname = "xpc",
  128. .mode = 0555,
  129. .child = xpc_sys_xpc_dir},
  130. {}
  131. };
  132. static struct ctl_table_header *xpc_sysctl;
  133. /* non-zero if any remote partition disengage was timed out */
  134. int xpc_disengage_timedout;
  135. /* #of activate IRQs received */
  136. atomic_t xpc_activate_IRQ_rcvd = ATOMIC_INIT(0);
  137. /* IRQ handler notifies this wait queue on receipt of an IRQ */
  138. DECLARE_WAIT_QUEUE_HEAD(xpc_activate_IRQ_wq);
  139. static unsigned long xpc_hb_check_timeout;
  140. static struct timer_list xpc_hb_timer;
  141. void *xpc_heartbeating_to_mask;
  142. /* notification that the xpc_hb_checker thread has exited */
  143. static DECLARE_COMPLETION(xpc_hb_checker_exited);
  144. /* notification that the xpc_discovery thread has exited */
  145. static DECLARE_COMPLETION(xpc_discovery_exited);
  146. static void xpc_kthread_waitmsgs(struct xpc_partition *, struct xpc_channel *);
  147. static int xpc_system_reboot(struct notifier_block *, unsigned long, void *);
  148. static struct notifier_block xpc_reboot_notifier = {
  149. .notifier_call = xpc_system_reboot,
  150. };
  151. static int xpc_system_die(struct notifier_block *, unsigned long, void *);
  152. static struct notifier_block xpc_die_notifier = {
  153. .notifier_call = xpc_system_die,
  154. };
  155. enum xp_retval (*xpc_rsvd_page_init) (struct xpc_rsvd_page *rp);
  156. void (*xpc_heartbeat_init) (void);
  157. void (*xpc_heartbeat_exit) (void);
  158. void (*xpc_increment_heartbeat) (void);
  159. void (*xpc_offline_heartbeat) (void);
  160. void (*xpc_online_heartbeat) (void);
  161. void (*xpc_check_remote_hb) (void);
  162. enum xp_retval (*xpc_make_first_contact) (struct xpc_partition *part);
  163. void (*xpc_notify_senders_of_disconnect) (struct xpc_channel *ch);
  164. u64 (*xpc_get_chctl_all_flags) (struct xpc_partition *part);
  165. enum xp_retval (*xpc_allocate_msgqueues) (struct xpc_channel *ch);
  166. void (*xpc_free_msgqueues) (struct xpc_channel *ch);
  167. void (*xpc_process_msg_chctl_flags) (struct xpc_partition *part, int ch_number);
  168. int (*xpc_n_of_deliverable_msgs) (struct xpc_channel *ch);
  169. struct xpc_msg *(*xpc_get_deliverable_msg) (struct xpc_channel *ch);
  170. void (*xpc_request_partition_activation) (struct xpc_rsvd_page *remote_rp,
  171. u64 remote_rp_pa, int nasid);
  172. void (*xpc_request_partition_reactivation) (struct xpc_partition *part);
  173. void (*xpc_request_partition_deactivation) (struct xpc_partition *part);
  174. void (*xpc_cancel_partition_deactivation_request) (struct xpc_partition *part);
  175. void (*xpc_process_activate_IRQ_rcvd) (int n_IRQs_expected);
  176. enum xp_retval (*xpc_setup_infrastructure) (struct xpc_partition *part);
  177. void (*xpc_teardown_infrastructure) (struct xpc_partition *part);
  178. void (*xpc_indicate_partition_engaged) (struct xpc_partition *part);
  179. int (*xpc_partition_engaged) (short partid);
  180. int (*xpc_any_partition_engaged) (void);
  181. void (*xpc_indicate_partition_disengaged) (struct xpc_partition *part);
  182. void (*xpc_assume_partition_disengaged) (short partid);
  183. void (*xpc_send_chctl_closerequest) (struct xpc_channel *ch,
  184. unsigned long *irq_flags);
  185. void (*xpc_send_chctl_closereply) (struct xpc_channel *ch,
  186. unsigned long *irq_flags);
  187. void (*xpc_send_chctl_openrequest) (struct xpc_channel *ch,
  188. unsigned long *irq_flags);
  189. void (*xpc_send_chctl_openreply) (struct xpc_channel *ch,
  190. unsigned long *irq_flags);
  191. enum xp_retval (*xpc_send_msg) (struct xpc_channel *ch, u32 flags,
  192. void *payload, u16 payload_size, u8 notify_type,
  193. xpc_notify_func func, void *key);
  194. void (*xpc_received_msg) (struct xpc_channel *ch, struct xpc_msg *msg);
  195. /*
  196. * Timer function to enforce the timelimit on the partition disengage.
  197. */
  198. static void
  199. xpc_timeout_partition_disengage(unsigned long data)
  200. {
  201. struct xpc_partition *part = (struct xpc_partition *)data;
  202. DBUG_ON(time_is_after_jiffies(part->disengage_timeout));
  203. (void)xpc_partition_disengaged(part);
  204. DBUG_ON(part->disengage_timeout != 0);
  205. DBUG_ON(xpc_partition_engaged(XPC_PARTID(part)));
  206. }
  207. /*
  208. * Timer to produce the heartbeat. The timer structures function is
  209. * already set when this is initially called. A tunable is used to
  210. * specify when the next timeout should occur.
  211. */
  212. static void
  213. xpc_hb_beater(unsigned long dummy)
  214. {
  215. xpc_increment_heartbeat();
  216. if (time_is_before_eq_jiffies(xpc_hb_check_timeout))
  217. wake_up_interruptible(&xpc_activate_IRQ_wq);
  218. xpc_hb_timer.expires = jiffies + (xpc_hb_interval * HZ);
  219. add_timer(&xpc_hb_timer);
  220. }
  221. static void
  222. xpc_start_hb_beater(void)
  223. {
  224. xpc_heartbeat_init();
  225. init_timer(&xpc_hb_timer);
  226. xpc_hb_timer.function = xpc_hb_beater;
  227. xpc_hb_beater(0);
  228. }
  229. static void
  230. xpc_stop_hb_beater(void)
  231. {
  232. del_timer_sync(&xpc_hb_timer);
  233. xpc_heartbeat_exit();
  234. }
  235. /*
  236. * This thread is responsible for nearly all of the partition
  237. * activation/deactivation.
  238. */
  239. static int
  240. xpc_hb_checker(void *ignore)
  241. {
  242. int last_IRQ_count = 0;
  243. int new_IRQ_count;
  244. int force_IRQ = 0;
  245. /* this thread was marked active by xpc_hb_init() */
  246. set_cpus_allowed_ptr(current, &cpumask_of_cpu(XPC_HB_CHECK_CPU));
  247. /* set our heartbeating to other partitions into motion */
  248. xpc_hb_check_timeout = jiffies + (xpc_hb_check_interval * HZ);
  249. xpc_start_hb_beater();
  250. while (!xpc_exiting) {
  251. dev_dbg(xpc_part, "woke up with %d ticks rem; %d IRQs have "
  252. "been received\n",
  253. (int)(xpc_hb_check_timeout - jiffies),
  254. atomic_read(&xpc_activate_IRQ_rcvd) - last_IRQ_count);
  255. /* checking of remote heartbeats is skewed by IRQ handling */
  256. if (time_is_before_eq_jiffies(xpc_hb_check_timeout)) {
  257. dev_dbg(xpc_part, "checking remote heartbeats\n");
  258. xpc_check_remote_hb();
  259. /*
  260. * We need to periodically recheck to ensure no
  261. * IRQ/amo pairs have been missed. That check
  262. * must always reset xpc_hb_check_timeout.
  263. */
  264. force_IRQ = 1;
  265. }
  266. /* check for outstanding IRQs */
  267. new_IRQ_count = atomic_read(&xpc_activate_IRQ_rcvd);
  268. if (last_IRQ_count < new_IRQ_count || force_IRQ != 0) {
  269. force_IRQ = 0;
  270. dev_dbg(xpc_part, "found an IRQ to process; will be "
  271. "resetting xpc_hb_check_timeout\n");
  272. xpc_process_activate_IRQ_rcvd(new_IRQ_count -
  273. last_IRQ_count);
  274. last_IRQ_count = new_IRQ_count;
  275. xpc_hb_check_timeout = jiffies +
  276. (xpc_hb_check_interval * HZ);
  277. }
  278. /* wait for IRQ or timeout */
  279. (void)wait_event_interruptible(xpc_activate_IRQ_wq,
  280. (last_IRQ_count < atomic_read(
  281. &xpc_activate_IRQ_rcvd)
  282. || time_is_before_eq_jiffies(
  283. xpc_hb_check_timeout) ||
  284. xpc_exiting));
  285. }
  286. xpc_stop_hb_beater();
  287. dev_dbg(xpc_part, "heartbeat checker is exiting\n");
  288. /* mark this thread as having exited */
  289. complete(&xpc_hb_checker_exited);
  290. return 0;
  291. }
  292. /*
  293. * This thread will attempt to discover other partitions to activate
  294. * based on info provided by SAL. This new thread is short lived and
  295. * will exit once discovery is complete.
  296. */
  297. static int
  298. xpc_initiate_discovery(void *ignore)
  299. {
  300. xpc_discovery();
  301. dev_dbg(xpc_part, "discovery thread is exiting\n");
  302. /* mark this thread as having exited */
  303. complete(&xpc_discovery_exited);
  304. return 0;
  305. }
  306. /*
  307. * The first kthread assigned to a newly activated partition is the one
  308. * created by XPC HB with which it calls xpc_activating(). XPC hangs on to
  309. * that kthread until the partition is brought down, at which time that kthread
  310. * returns back to XPC HB. (The return of that kthread will signify to XPC HB
  311. * that XPC has dismantled all communication infrastructure for the associated
  312. * partition.) This kthread becomes the channel manager for that partition.
  313. *
  314. * Each active partition has a channel manager, who, besides connecting and
  315. * disconnecting channels, will ensure that each of the partition's connected
  316. * channels has the required number of assigned kthreads to get the work done.
  317. */
  318. static void
  319. xpc_channel_mgr(struct xpc_partition *part)
  320. {
  321. while (part->act_state != XPC_P_DEACTIVATING ||
  322. atomic_read(&part->nchannels_active) > 0 ||
  323. !xpc_partition_disengaged(part)) {
  324. xpc_process_sent_chctl_flags(part);
  325. /*
  326. * Wait until we've been requested to activate kthreads or
  327. * all of the channel's message queues have been torn down or
  328. * a signal is pending.
  329. *
  330. * The channel_mgr_requests is set to 1 after being awakened,
  331. * This is done to prevent the channel mgr from making one pass
  332. * through the loop for each request, since he will
  333. * be servicing all the requests in one pass. The reason it's
  334. * set to 1 instead of 0 is so that other kthreads will know
  335. * that the channel mgr is running and won't bother trying to
  336. * wake him up.
  337. */
  338. atomic_dec(&part->channel_mgr_requests);
  339. (void)wait_event_interruptible(part->channel_mgr_wq,
  340. (atomic_read(&part->channel_mgr_requests) > 0 ||
  341. part->chctl.all_flags != 0 ||
  342. (part->act_state == XPC_P_DEACTIVATING &&
  343. atomic_read(&part->nchannels_active) == 0 &&
  344. xpc_partition_disengaged(part))));
  345. atomic_set(&part->channel_mgr_requests, 1);
  346. }
  347. }
  348. /*
  349. * When XPC HB determines that a partition has come up, it will create a new
  350. * kthread and that kthread will call this function to attempt to set up the
  351. * basic infrastructure used for Cross Partition Communication with the newly
  352. * upped partition.
  353. *
  354. * The kthread that was created by XPC HB and which setup the XPC
  355. * infrastructure will remain assigned to the partition becoming the channel
  356. * manager for that partition until the partition is deactivating, at which
  357. * time the kthread will teardown the XPC infrastructure and then exit.
  358. */
  359. static int
  360. xpc_activating(void *__partid)
  361. {
  362. short partid = (u64)__partid;
  363. struct xpc_partition *part = &xpc_partitions[partid];
  364. unsigned long irq_flags;
  365. DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
  366. spin_lock_irqsave(&part->act_lock, irq_flags);
  367. if (part->act_state == XPC_P_DEACTIVATING) {
  368. part->act_state = XPC_P_INACTIVE;
  369. spin_unlock_irqrestore(&part->act_lock, irq_flags);
  370. part->remote_rp_pa = 0;
  371. return 0;
  372. }
  373. /* indicate the thread is activating */
  374. DBUG_ON(part->act_state != XPC_P_ACTIVATION_REQ);
  375. part->act_state = XPC_P_ACTIVATING;
  376. XPC_SET_REASON(part, 0, 0);
  377. spin_unlock_irqrestore(&part->act_lock, irq_flags);
  378. dev_dbg(xpc_part, "activating partition %d\n", partid);
  379. xpc_allow_hb(partid);
  380. if (xpc_setup_infrastructure(part) == xpSuccess) {
  381. (void)xpc_part_ref(part); /* this will always succeed */
  382. if (xpc_make_first_contact(part) == xpSuccess) {
  383. xpc_mark_partition_active(part);
  384. xpc_channel_mgr(part);
  385. /* won't return until partition is deactivating */
  386. }
  387. xpc_part_deref(part);
  388. xpc_teardown_infrastructure(part);
  389. }
  390. xpc_disallow_hb(partid);
  391. xpc_mark_partition_inactive(part);
  392. if (part->reason == xpReactivating) {
  393. /* interrupting ourselves results in activating partition */
  394. xpc_request_partition_reactivation(part);
  395. }
  396. return 0;
  397. }
  398. void
  399. xpc_activate_partition(struct xpc_partition *part)
  400. {
  401. short partid = XPC_PARTID(part);
  402. unsigned long irq_flags;
  403. struct task_struct *kthread;
  404. spin_lock_irqsave(&part->act_lock, irq_flags);
  405. DBUG_ON(part->act_state != XPC_P_INACTIVE);
  406. part->act_state = XPC_P_ACTIVATION_REQ;
  407. XPC_SET_REASON(part, xpCloneKThread, __LINE__);
  408. spin_unlock_irqrestore(&part->act_lock, irq_flags);
  409. kthread = kthread_run(xpc_activating, (void *)((u64)partid), "xpc%02d",
  410. partid);
  411. if (IS_ERR(kthread)) {
  412. spin_lock_irqsave(&part->act_lock, irq_flags);
  413. part->act_state = XPC_P_INACTIVE;
  414. XPC_SET_REASON(part, xpCloneKThreadFailed, __LINE__);
  415. spin_unlock_irqrestore(&part->act_lock, irq_flags);
  416. }
  417. }
  418. void
  419. xpc_activate_kthreads(struct xpc_channel *ch, int needed)
  420. {
  421. int idle = atomic_read(&ch->kthreads_idle);
  422. int assigned = atomic_read(&ch->kthreads_assigned);
  423. int wakeup;
  424. DBUG_ON(needed <= 0);
  425. if (idle > 0) {
  426. wakeup = (needed > idle) ? idle : needed;
  427. needed -= wakeup;
  428. dev_dbg(xpc_chan, "wakeup %d idle kthreads, partid=%d, "
  429. "channel=%d\n", wakeup, ch->partid, ch->number);
  430. /* only wakeup the requested number of kthreads */
  431. wake_up_nr(&ch->idle_wq, wakeup);
  432. }
  433. if (needed <= 0)
  434. return;
  435. if (needed + assigned > ch->kthreads_assigned_limit) {
  436. needed = ch->kthreads_assigned_limit - assigned;
  437. if (needed <= 0)
  438. return;
  439. }
  440. dev_dbg(xpc_chan, "create %d new kthreads, partid=%d, channel=%d\n",
  441. needed, ch->partid, ch->number);
  442. xpc_create_kthreads(ch, needed, 0);
  443. }
  444. /*
  445. * This function is where XPC's kthreads wait for messages to deliver.
  446. */
  447. static void
  448. xpc_kthread_waitmsgs(struct xpc_partition *part, struct xpc_channel *ch)
  449. {
  450. do {
  451. /* deliver messages to their intended recipients */
  452. while (xpc_n_of_deliverable_msgs(ch) > 0 &&
  453. !(ch->flags & XPC_C_DISCONNECTING)) {
  454. xpc_deliver_msg(ch);
  455. }
  456. if (atomic_inc_return(&ch->kthreads_idle) >
  457. ch->kthreads_idle_limit) {
  458. /* too many idle kthreads on this channel */
  459. atomic_dec(&ch->kthreads_idle);
  460. break;
  461. }
  462. dev_dbg(xpc_chan, "idle kthread calling "
  463. "wait_event_interruptible_exclusive()\n");
  464. (void)wait_event_interruptible_exclusive(ch->idle_wq,
  465. (xpc_n_of_deliverable_msgs(ch) > 0 ||
  466. (ch->flags & XPC_C_DISCONNECTING)));
  467. atomic_dec(&ch->kthreads_idle);
  468. } while (!(ch->flags & XPC_C_DISCONNECTING));
  469. }
  470. static int
  471. xpc_kthread_start(void *args)
  472. {
  473. short partid = XPC_UNPACK_ARG1(args);
  474. u16 ch_number = XPC_UNPACK_ARG2(args);
  475. struct xpc_partition *part = &xpc_partitions[partid];
  476. struct xpc_channel *ch;
  477. int n_needed;
  478. unsigned long irq_flags;
  479. dev_dbg(xpc_chan, "kthread starting, partid=%d, channel=%d\n",
  480. partid, ch_number);
  481. ch = &part->channels[ch_number];
  482. if (!(ch->flags & XPC_C_DISCONNECTING)) {
  483. /* let registerer know that connection has been established */
  484. spin_lock_irqsave(&ch->lock, irq_flags);
  485. if (!(ch->flags & XPC_C_CONNECTEDCALLOUT)) {
  486. ch->flags |= XPC_C_CONNECTEDCALLOUT;
  487. spin_unlock_irqrestore(&ch->lock, irq_flags);
  488. xpc_connected_callout(ch);
  489. spin_lock_irqsave(&ch->lock, irq_flags);
  490. ch->flags |= XPC_C_CONNECTEDCALLOUT_MADE;
  491. spin_unlock_irqrestore(&ch->lock, irq_flags);
  492. /*
  493. * It is possible that while the callout was being
  494. * made that the remote partition sent some messages.
  495. * If that is the case, we may need to activate
  496. * additional kthreads to help deliver them. We only
  497. * need one less than total #of messages to deliver.
  498. */
  499. n_needed = xpc_n_of_deliverable_msgs(ch) - 1;
  500. if (n_needed > 0 && !(ch->flags & XPC_C_DISCONNECTING))
  501. xpc_activate_kthreads(ch, n_needed);
  502. } else {
  503. spin_unlock_irqrestore(&ch->lock, irq_flags);
  504. }
  505. xpc_kthread_waitmsgs(part, ch);
  506. }
  507. /* let registerer know that connection is disconnecting */
  508. spin_lock_irqsave(&ch->lock, irq_flags);
  509. if ((ch->flags & XPC_C_CONNECTEDCALLOUT_MADE) &&
  510. !(ch->flags & XPC_C_DISCONNECTINGCALLOUT)) {
  511. ch->flags |= XPC_C_DISCONNECTINGCALLOUT;
  512. spin_unlock_irqrestore(&ch->lock, irq_flags);
  513. xpc_disconnect_callout(ch, xpDisconnecting);
  514. spin_lock_irqsave(&ch->lock, irq_flags);
  515. ch->flags |= XPC_C_DISCONNECTINGCALLOUT_MADE;
  516. }
  517. spin_unlock_irqrestore(&ch->lock, irq_flags);
  518. if (atomic_dec_return(&ch->kthreads_assigned) == 0 &&
  519. atomic_dec_return(&part->nchannels_engaged) == 0) {
  520. xpc_indicate_partition_disengaged(part);
  521. }
  522. xpc_msgqueue_deref(ch);
  523. dev_dbg(xpc_chan, "kthread exiting, partid=%d, channel=%d\n",
  524. partid, ch_number);
  525. xpc_part_deref(part);
  526. return 0;
  527. }
  528. /*
  529. * For each partition that XPC has established communications with, there is
  530. * a minimum of one kernel thread assigned to perform any operation that
  531. * may potentially sleep or block (basically the callouts to the asynchronous
  532. * functions registered via xpc_connect()).
  533. *
  534. * Additional kthreads are created and destroyed by XPC as the workload
  535. * demands.
  536. *
  537. * A kthread is assigned to one of the active channels that exists for a given
  538. * partition.
  539. */
  540. void
  541. xpc_create_kthreads(struct xpc_channel *ch, int needed,
  542. int ignore_disconnecting)
  543. {
  544. unsigned long irq_flags;
  545. u64 args = XPC_PACK_ARGS(ch->partid, ch->number);
  546. struct xpc_partition *part = &xpc_partitions[ch->partid];
  547. struct task_struct *kthread;
  548. while (needed-- > 0) {
  549. /*
  550. * The following is done on behalf of the newly created
  551. * kthread. That kthread is responsible for doing the
  552. * counterpart to the following before it exits.
  553. */
  554. if (ignore_disconnecting) {
  555. if (!atomic_inc_not_zero(&ch->kthreads_assigned)) {
  556. /* kthreads assigned had gone to zero */
  557. BUG_ON(!(ch->flags &
  558. XPC_C_DISCONNECTINGCALLOUT_MADE));
  559. break;
  560. }
  561. } else if (ch->flags & XPC_C_DISCONNECTING) {
  562. break;
  563. } else if (atomic_inc_return(&ch->kthreads_assigned) == 1 &&
  564. atomic_inc_return(&part->nchannels_engaged) == 1) {
  565. xpc_indicate_partition_engaged(part);
  566. }
  567. (void)xpc_part_ref(part);
  568. xpc_msgqueue_ref(ch);
  569. kthread = kthread_run(xpc_kthread_start, (void *)args,
  570. "xpc%02dc%d", ch->partid, ch->number);
  571. if (IS_ERR(kthread)) {
  572. /* the fork failed */
  573. /*
  574. * NOTE: if (ignore_disconnecting &&
  575. * !(ch->flags & XPC_C_DISCONNECTINGCALLOUT)) is true,
  576. * then we'll deadlock if all other kthreads assigned
  577. * to this channel are blocked in the channel's
  578. * registerer, because the only thing that will unblock
  579. * them is the xpDisconnecting callout that this
  580. * failed kthread_run() would have made.
  581. */
  582. if (atomic_dec_return(&ch->kthreads_assigned) == 0 &&
  583. atomic_dec_return(&part->nchannels_engaged) == 0) {
  584. xpc_indicate_partition_disengaged(part);
  585. }
  586. xpc_msgqueue_deref(ch);
  587. xpc_part_deref(part);
  588. if (atomic_read(&ch->kthreads_assigned) <
  589. ch->kthreads_idle_limit) {
  590. /*
  591. * Flag this as an error only if we have an
  592. * insufficient #of kthreads for the channel
  593. * to function.
  594. */
  595. spin_lock_irqsave(&ch->lock, irq_flags);
  596. XPC_DISCONNECT_CHANNEL(ch, xpLackOfResources,
  597. &irq_flags);
  598. spin_unlock_irqrestore(&ch->lock, irq_flags);
  599. }
  600. break;
  601. }
  602. }
  603. }
  604. void
  605. xpc_disconnect_wait(int ch_number)
  606. {
  607. unsigned long irq_flags;
  608. short partid;
  609. struct xpc_partition *part;
  610. struct xpc_channel *ch;
  611. int wakeup_channel_mgr;
  612. /* now wait for all callouts to the caller's function to cease */
  613. for (partid = 0; partid < xp_max_npartitions; partid++) {
  614. part = &xpc_partitions[partid];
  615. if (!xpc_part_ref(part))
  616. continue;
  617. ch = &part->channels[ch_number];
  618. if (!(ch->flags & XPC_C_WDISCONNECT)) {
  619. xpc_part_deref(part);
  620. continue;
  621. }
  622. wait_for_completion(&ch->wdisconnect_wait);
  623. spin_lock_irqsave(&ch->lock, irq_flags);
  624. DBUG_ON(!(ch->flags & XPC_C_DISCONNECTED));
  625. wakeup_channel_mgr = 0;
  626. if (ch->delayed_chctl_flags) {
  627. if (part->act_state != XPC_P_DEACTIVATING) {
  628. spin_lock(&part->chctl_lock);
  629. part->chctl.flags[ch->number] |=
  630. ch->delayed_chctl_flags;
  631. spin_unlock(&part->chctl_lock);
  632. wakeup_channel_mgr = 1;
  633. }
  634. ch->delayed_chctl_flags = 0;
  635. }
  636. ch->flags &= ~XPC_C_WDISCONNECT;
  637. spin_unlock_irqrestore(&ch->lock, irq_flags);
  638. if (wakeup_channel_mgr)
  639. xpc_wakeup_channel_mgr(part);
  640. xpc_part_deref(part);
  641. }
  642. }
  643. static void
  644. xpc_do_exit(enum xp_retval reason)
  645. {
  646. short partid;
  647. int active_part_count, printed_waiting_msg = 0;
  648. struct xpc_partition *part;
  649. unsigned long printmsg_time, disengage_timeout = 0;
  650. /* a 'rmmod XPC' and a 'reboot' cannot both end up here together */
  651. DBUG_ON(xpc_exiting == 1);
  652. /*
  653. * Let the heartbeat checker thread and the discovery thread
  654. * (if one is running) know that they should exit. Also wake up
  655. * the heartbeat checker thread in case it's sleeping.
  656. */
  657. xpc_exiting = 1;
  658. wake_up_interruptible(&xpc_activate_IRQ_wq);
  659. /* wait for the discovery thread to exit */
  660. wait_for_completion(&xpc_discovery_exited);
  661. /* wait for the heartbeat checker thread to exit */
  662. wait_for_completion(&xpc_hb_checker_exited);
  663. /* sleep for a 1/3 of a second or so */
  664. (void)msleep_interruptible(300);
  665. /* wait for all partitions to become inactive */
  666. printmsg_time = jiffies + (XPC_DEACTIVATE_PRINTMSG_INTERVAL * HZ);
  667. xpc_disengage_timedout = 0;
  668. do {
  669. active_part_count = 0;
  670. for (partid = 0; partid < xp_max_npartitions; partid++) {
  671. part = &xpc_partitions[partid];
  672. if (xpc_partition_disengaged(part) &&
  673. part->act_state == XPC_P_INACTIVE) {
  674. continue;
  675. }
  676. active_part_count++;
  677. XPC_DEACTIVATE_PARTITION(part, reason);
  678. if (part->disengage_timeout > disengage_timeout)
  679. disengage_timeout = part->disengage_timeout;
  680. }
  681. if (xpc_any_partition_engaged()) {
  682. if (time_is_before_jiffies(printmsg_time)) {
  683. dev_info(xpc_part, "waiting for remote "
  684. "partitions to deactivate, timeout in "
  685. "%ld seconds\n", (disengage_timeout -
  686. jiffies) / HZ);
  687. printmsg_time = jiffies +
  688. (XPC_DEACTIVATE_PRINTMSG_INTERVAL * HZ);
  689. printed_waiting_msg = 1;
  690. }
  691. } else if (active_part_count > 0) {
  692. if (printed_waiting_msg) {
  693. dev_info(xpc_part, "waiting for local partition"
  694. " to deactivate\n");
  695. printed_waiting_msg = 0;
  696. }
  697. } else {
  698. if (!xpc_disengage_timedout) {
  699. dev_info(xpc_part, "all partitions have "
  700. "deactivated\n");
  701. }
  702. break;
  703. }
  704. /* sleep for a 1/3 of a second or so */
  705. (void)msleep_interruptible(300);
  706. } while (1);
  707. DBUG_ON(xpc_any_partition_engaged());
  708. DBUG_ON(xpc_any_hbs_allowed() != 0);
  709. /* a zero timestamp indicates our rsvd page is not initialized */
  710. xpc_rsvd_page->ts_jiffies = 0;
  711. if (reason == xpUnloading) {
  712. (void)unregister_die_notifier(&xpc_die_notifier);
  713. (void)unregister_reboot_notifier(&xpc_reboot_notifier);
  714. }
  715. /* clear the interface to XPC's functions */
  716. xpc_clear_interface();
  717. if (xpc_sysctl)
  718. unregister_sysctl_table(xpc_sysctl);
  719. kfree(xpc_partitions);
  720. if (is_shub())
  721. xpc_exit_sn2();
  722. else
  723. xpc_exit_uv();
  724. }
  725. /*
  726. * This function is called when the system is being rebooted.
  727. */
  728. static int
  729. xpc_system_reboot(struct notifier_block *nb, unsigned long event, void *unused)
  730. {
  731. enum xp_retval reason;
  732. switch (event) {
  733. case SYS_RESTART:
  734. reason = xpSystemReboot;
  735. break;
  736. case SYS_HALT:
  737. reason = xpSystemHalt;
  738. break;
  739. case SYS_POWER_OFF:
  740. reason = xpSystemPoweroff;
  741. break;
  742. default:
  743. reason = xpSystemGoingDown;
  744. }
  745. xpc_do_exit(reason);
  746. return NOTIFY_DONE;
  747. }
  748. /*
  749. * Notify other partitions to deactivate from us by first disengaging from all
  750. * references to our memory.
  751. */
  752. static void
  753. xpc_die_deactivate(void)
  754. {
  755. struct xpc_partition *part;
  756. short partid;
  757. int any_engaged;
  758. long time, printmsg_time, disengage_timeout;
  759. /* keep xpc_hb_checker thread from doing anything (just in case) */
  760. xpc_exiting = 1;
  761. xpc_disallow_all_hbs(); /*indicate we're deactivated */
  762. for (partid = 0; partid < xp_max_npartitions; partid++) {
  763. part = &xpc_partitions[partid];
  764. if (xpc_partition_engaged(partid) ||
  765. part->act_state != XPC_P_INACTIVE) {
  766. xpc_request_partition_deactivation(part);
  767. xpc_indicate_partition_disengaged(part);
  768. }
  769. }
  770. time = rtc_time();
  771. printmsg_time = time +
  772. (XPC_DEACTIVATE_PRINTMSG_INTERVAL * sn_rtc_cycles_per_second);
  773. disengage_timeout = time +
  774. (xpc_disengage_timelimit * sn_rtc_cycles_per_second);
  775. /*
  776. * Though we requested that all other partitions deactivate from us,
  777. * we only wait until they've all disengaged.
  778. */
  779. while (1) {
  780. any_engaged = xpc_any_partition_engaged();
  781. if (!any_engaged) {
  782. dev_info(xpc_part, "all partitions have deactivated\n");
  783. break;
  784. }
  785. time = rtc_time();
  786. if (time >= disengage_timeout) {
  787. for (partid = 0; partid < xp_max_npartitions;
  788. partid++) {
  789. if (xpc_partition_engaged(partid)) {
  790. dev_info(xpc_part, "deactivate from "
  791. "remote partition %d timed "
  792. "out\n", partid);
  793. }
  794. }
  795. break;
  796. }
  797. if (time >= printmsg_time) {
  798. dev_info(xpc_part, "waiting for remote partitions to "
  799. "deactivate, timeout in %ld seconds\n",
  800. (disengage_timeout - time) /
  801. sn_rtc_cycles_per_second);
  802. printmsg_time = time +
  803. (XPC_DEACTIVATE_PRINTMSG_INTERVAL *
  804. sn_rtc_cycles_per_second);
  805. }
  806. }
  807. }
  808. /*
  809. * This function is called when the system is being restarted or halted due
  810. * to some sort of system failure. If this is the case we need to notify the
  811. * other partitions to disengage from all references to our memory.
  812. * This function can also be called when our heartbeater could be offlined
  813. * for a time. In this case we need to notify other partitions to not worry
  814. * about the lack of a heartbeat.
  815. */
  816. static int
  817. xpc_system_die(struct notifier_block *nb, unsigned long event, void *unused)
  818. {
  819. switch (event) {
  820. case DIE_MACHINE_RESTART:
  821. case DIE_MACHINE_HALT:
  822. xpc_die_deactivate();
  823. break;
  824. case DIE_KDEBUG_ENTER:
  825. /* Should lack of heartbeat be ignored by other partitions? */
  826. if (!xpc_kdebug_ignore)
  827. break;
  828. /* fall through */
  829. case DIE_MCA_MONARCH_ENTER:
  830. case DIE_INIT_MONARCH_ENTER:
  831. xpc_offline_heartbeat();
  832. break;
  833. case DIE_KDEBUG_LEAVE:
  834. /* Is lack of heartbeat being ignored by other partitions? */
  835. if (!xpc_kdebug_ignore)
  836. break;
  837. /* fall through */
  838. case DIE_MCA_MONARCH_LEAVE:
  839. case DIE_INIT_MONARCH_LEAVE:
  840. xpc_online_heartbeat();
  841. break;
  842. }
  843. return NOTIFY_DONE;
  844. }
  845. int __init
  846. xpc_init(void)
  847. {
  848. int ret;
  849. short partid;
  850. struct xpc_partition *part;
  851. struct task_struct *kthread;
  852. snprintf(xpc_part->bus_id, BUS_ID_SIZE, "part");
  853. snprintf(xpc_chan->bus_id, BUS_ID_SIZE, "chan");
  854. if (is_shub()) {
  855. /*
  856. * The ia64-sn2 architecture supports at most 64 partitions.
  857. * And the inability to unregister remote amos restricts us
  858. * further to only support exactly 64 partitions on this
  859. * architecture, no less.
  860. */
  861. if (xp_max_npartitions != 64)
  862. return -EINVAL;
  863. ret = xpc_init_sn2();
  864. if (ret != 0)
  865. return ret;
  866. } else if (is_uv()) {
  867. xpc_init_uv();
  868. } else {
  869. return -ENODEV;
  870. }
  871. xpc_partitions = kzalloc(sizeof(struct xpc_partition) *
  872. xp_max_npartitions, GFP_KERNEL);
  873. if (xpc_partitions == NULL) {
  874. dev_err(xpc_part, "can't get memory for partition structure\n");
  875. ret = -ENOMEM;
  876. goto out_1;
  877. }
  878. /*
  879. * The first few fields of each entry of xpc_partitions[] need to
  880. * be initialized now so that calls to xpc_connect() and
  881. * xpc_disconnect() can be made prior to the activation of any remote
  882. * partition. NOTE THAT NONE OF THE OTHER FIELDS BELONGING TO THESE
  883. * ENTRIES ARE MEANINGFUL UNTIL AFTER AN ENTRY'S CORRESPONDING
  884. * PARTITION HAS BEEN ACTIVATED.
  885. */
  886. for (partid = 0; partid < xp_max_npartitions; partid++) {
  887. part = &xpc_partitions[partid];
  888. DBUG_ON((u64)part != L1_CACHE_ALIGN((u64)part));
  889. part->activate_IRQ_rcvd = 0;
  890. spin_lock_init(&part->act_lock);
  891. part->act_state = XPC_P_INACTIVE;
  892. XPC_SET_REASON(part, 0, 0);
  893. init_timer(&part->disengage_timer);
  894. part->disengage_timer.function =
  895. xpc_timeout_partition_disengage;
  896. part->disengage_timer.data = (unsigned long)part;
  897. part->setup_state = XPC_P_UNSET;
  898. init_waitqueue_head(&part->teardown_wq);
  899. atomic_set(&part->references, 0);
  900. }
  901. xpc_sysctl = register_sysctl_table(xpc_sys_dir);
  902. /*
  903. * Fill the partition reserved page with the information needed by
  904. * other partitions to discover we are alive and establish initial
  905. * communications.
  906. */
  907. xpc_rsvd_page = xpc_setup_rsvd_page();
  908. if (xpc_rsvd_page == NULL) {
  909. dev_err(xpc_part, "can't setup our reserved page\n");
  910. ret = -EBUSY;
  911. goto out_2;
  912. }
  913. /* add ourselves to the reboot_notifier_list */
  914. ret = register_reboot_notifier(&xpc_reboot_notifier);
  915. if (ret != 0)
  916. dev_warn(xpc_part, "can't register reboot notifier\n");
  917. /* add ourselves to the die_notifier list */
  918. ret = register_die_notifier(&xpc_die_notifier);
  919. if (ret != 0)
  920. dev_warn(xpc_part, "can't register die notifier\n");
  921. /*
  922. * The real work-horse behind xpc. This processes incoming
  923. * interrupts and monitors remote heartbeats.
  924. */
  925. kthread = kthread_run(xpc_hb_checker, NULL, XPC_HB_CHECK_THREAD_NAME);
  926. if (IS_ERR(kthread)) {
  927. dev_err(xpc_part, "failed while forking hb check thread\n");
  928. ret = -EBUSY;
  929. goto out_3;
  930. }
  931. /*
  932. * Startup a thread that will attempt to discover other partitions to
  933. * activate based on info provided by SAL. This new thread is short
  934. * lived and will exit once discovery is complete.
  935. */
  936. kthread = kthread_run(xpc_initiate_discovery, NULL,
  937. XPC_DISCOVERY_THREAD_NAME);
  938. if (IS_ERR(kthread)) {
  939. dev_err(xpc_part, "failed while forking discovery thread\n");
  940. /* mark this new thread as a non-starter */
  941. complete(&xpc_discovery_exited);
  942. xpc_do_exit(xpUnloading);
  943. return -EBUSY;
  944. }
  945. /* set the interface to point at XPC's functions */
  946. xpc_set_interface(xpc_initiate_connect, xpc_initiate_disconnect,
  947. xpc_initiate_send, xpc_initiate_send_notify,
  948. xpc_initiate_received, xpc_initiate_partid_to_nasids);
  949. return 0;
  950. /* initialization was not successful */
  951. out_3:
  952. /* a zero timestamp indicates our rsvd page is not initialized */
  953. xpc_rsvd_page->ts_jiffies = 0;
  954. (void)unregister_die_notifier(&xpc_die_notifier);
  955. (void)unregister_reboot_notifier(&xpc_reboot_notifier);
  956. out_2:
  957. if (xpc_sysctl)
  958. unregister_sysctl_table(xpc_sysctl);
  959. kfree(xpc_partitions);
  960. out_1:
  961. if (is_shub())
  962. xpc_exit_sn2();
  963. else
  964. xpc_exit_uv();
  965. return ret;
  966. }
  967. module_init(xpc_init);
  968. void __exit
  969. xpc_exit(void)
  970. {
  971. xpc_do_exit(xpUnloading);
  972. }
  973. module_exit(xpc_exit);
  974. MODULE_AUTHOR("Silicon Graphics, Inc.");
  975. MODULE_DESCRIPTION("Cross Partition Communication (XPC) support");
  976. MODULE_LICENSE("GPL");
  977. module_param(xpc_hb_interval, int, 0);
  978. MODULE_PARM_DESC(xpc_hb_interval, "Number of seconds between "
  979. "heartbeat increments.");
  980. module_param(xpc_hb_check_interval, int, 0);
  981. MODULE_PARM_DESC(xpc_hb_check_interval, "Number of seconds between "
  982. "heartbeat checks.");
  983. module_param(xpc_disengage_timelimit, int, 0);
  984. MODULE_PARM_DESC(xpc_disengage_timelimit, "Number of seconds to wait "
  985. "for disengage to complete.");
  986. module_param(xpc_kdebug_ignore, int, 0);
  987. MODULE_PARM_DESC(xpc_kdebug_ignore, "Should lack of heartbeat be ignored by "
  988. "other partitions when dropping into kdebug.");