xpc_main.c 33 KB

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