xpc_main.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405
  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-2009 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/module.h>
  45. #include <linux/sysctl.h>
  46. #include <linux/device.h>
  47. #include <linux/delay.h>
  48. #include <linux/reboot.h>
  49. #include <linux/kdebug.h>
  50. #include <linux/kthread.h>
  51. #include "xpc.h"
  52. /* define two XPC debug device structures to be used with dev_dbg() et al */
  53. struct device_driver xpc_dbg_name = {
  54. .name = "xpc"
  55. };
  56. struct device xpc_part_dbg_subname = {
  57. .init_name = "", /* set to "part" at xpc_init() time */
  58. .driver = &xpc_dbg_name
  59. };
  60. struct device xpc_chan_dbg_subname = {
  61. .init_name = "", /* set to "chan" at xpc_init() time */
  62. .driver = &xpc_dbg_name
  63. };
  64. struct device *xpc_part = &xpc_part_dbg_subname;
  65. struct device *xpc_chan = &xpc_chan_dbg_subname;
  66. static int xpc_kdebug_ignore;
  67. /* systune related variables for /proc/sys directories */
  68. static int xpc_hb_interval = XPC_HB_DEFAULT_INTERVAL;
  69. static int xpc_hb_min_interval = 1;
  70. static int xpc_hb_max_interval = 10;
  71. static int xpc_hb_check_interval = XPC_HB_CHECK_DEFAULT_INTERVAL;
  72. static int xpc_hb_check_min_interval = 10;
  73. static int xpc_hb_check_max_interval = 120;
  74. int xpc_disengage_timelimit = XPC_DISENGAGE_DEFAULT_TIMELIMIT;
  75. static int xpc_disengage_min_timelimit; /* = 0 */
  76. static int xpc_disengage_max_timelimit = 120;
  77. static ctl_table xpc_sys_xpc_hb_dir[] = {
  78. {
  79. .ctl_name = CTL_UNNUMBERED,
  80. .procname = "hb_interval",
  81. .data = &xpc_hb_interval,
  82. .maxlen = sizeof(int),
  83. .mode = 0644,
  84. .proc_handler = &proc_dointvec_minmax,
  85. .strategy = &sysctl_intvec,
  86. .extra1 = &xpc_hb_min_interval,
  87. .extra2 = &xpc_hb_max_interval},
  88. {
  89. .ctl_name = CTL_UNNUMBERED,
  90. .procname = "hb_check_interval",
  91. .data = &xpc_hb_check_interval,
  92. .maxlen = sizeof(int),
  93. .mode = 0644,
  94. .proc_handler = &proc_dointvec_minmax,
  95. .strategy = &sysctl_intvec,
  96. .extra1 = &xpc_hb_check_min_interval,
  97. .extra2 = &xpc_hb_check_max_interval},
  98. {}
  99. };
  100. static ctl_table xpc_sys_xpc_dir[] = {
  101. {
  102. .ctl_name = CTL_UNNUMBERED,
  103. .procname = "hb",
  104. .mode = 0555,
  105. .child = xpc_sys_xpc_hb_dir},
  106. {
  107. .ctl_name = CTL_UNNUMBERED,
  108. .procname = "disengage_timelimit",
  109. .data = &xpc_disengage_timelimit,
  110. .maxlen = sizeof(int),
  111. .mode = 0644,
  112. .proc_handler = &proc_dointvec_minmax,
  113. .strategy = &sysctl_intvec,
  114. .extra1 = &xpc_disengage_min_timelimit,
  115. .extra2 = &xpc_disengage_max_timelimit},
  116. {}
  117. };
  118. static ctl_table xpc_sys_dir[] = {
  119. {
  120. .ctl_name = CTL_UNNUMBERED,
  121. .procname = "xpc",
  122. .mode = 0555,
  123. .child = xpc_sys_xpc_dir},
  124. {}
  125. };
  126. static struct ctl_table_header *xpc_sysctl;
  127. /* non-zero if any remote partition disengage was timed out */
  128. int xpc_disengage_timedout;
  129. /* #of activate IRQs received and not yet processed */
  130. int xpc_activate_IRQ_rcvd;
  131. DEFINE_SPINLOCK(xpc_activate_IRQ_rcvd_lock);
  132. /* IRQ handler notifies this wait queue on receipt of an IRQ */
  133. DECLARE_WAIT_QUEUE_HEAD(xpc_activate_IRQ_wq);
  134. static unsigned long xpc_hb_check_timeout;
  135. static struct timer_list xpc_hb_timer;
  136. /* notification that the xpc_hb_checker thread has exited */
  137. static DECLARE_COMPLETION(xpc_hb_checker_exited);
  138. /* notification that the xpc_discovery thread has exited */
  139. static DECLARE_COMPLETION(xpc_discovery_exited);
  140. static void xpc_kthread_waitmsgs(struct xpc_partition *, struct xpc_channel *);
  141. static int xpc_system_reboot(struct notifier_block *, unsigned long, void *);
  142. static struct notifier_block xpc_reboot_notifier = {
  143. .notifier_call = xpc_system_reboot,
  144. };
  145. static int xpc_system_die(struct notifier_block *, unsigned long, void *);
  146. static struct notifier_block xpc_die_notifier = {
  147. .notifier_call = xpc_system_die,
  148. };
  149. int (*xpc_setup_partitions_sn) (void);
  150. void (*xpc_teardown_partitions_sn) (void);
  151. enum xp_retval (*xpc_get_partition_rsvd_page_pa) (void *buf, u64 *cookie,
  152. unsigned long *rp_pa,
  153. size_t *len);
  154. int (*xpc_setup_rsvd_page_sn) (struct xpc_rsvd_page *rp);
  155. void (*xpc_allow_hb) (short partid);
  156. void (*xpc_disallow_hb) (short partid);
  157. void (*xpc_disallow_all_hbs) (void);
  158. void (*xpc_heartbeat_init) (void);
  159. void (*xpc_heartbeat_exit) (void);
  160. void (*xpc_increment_heartbeat) (void);
  161. void (*xpc_offline_heartbeat) (void);
  162. void (*xpc_online_heartbeat) (void);
  163. enum xp_retval (*xpc_get_remote_heartbeat) (struct xpc_partition *part);
  164. enum xp_retval (*xpc_make_first_contact) (struct xpc_partition *part);
  165. void (*xpc_notify_senders_of_disconnect) (struct xpc_channel *ch);
  166. u64 (*xpc_get_chctl_all_flags) (struct xpc_partition *part);
  167. enum xp_retval (*xpc_setup_msg_structures) (struct xpc_channel *ch);
  168. void (*xpc_teardown_msg_structures) (struct xpc_channel *ch);
  169. void (*xpc_process_msg_chctl_flags) (struct xpc_partition *part, int ch_number);
  170. int (*xpc_n_of_deliverable_payloads) (struct xpc_channel *ch);
  171. void *(*xpc_get_deliverable_payload) (struct xpc_channel *ch);
  172. void (*xpc_request_partition_activation) (struct xpc_rsvd_page *remote_rp,
  173. unsigned long remote_rp_pa,
  174. int nasid);
  175. void (*xpc_request_partition_reactivation) (struct xpc_partition *part);
  176. void (*xpc_request_partition_deactivation) (struct xpc_partition *part);
  177. void (*xpc_cancel_partition_deactivation_request) (struct xpc_partition *part);
  178. void (*xpc_process_activate_IRQ_rcvd) (void);
  179. enum xp_retval (*xpc_setup_ch_structures_sn) (struct xpc_partition *part);
  180. void (*xpc_teardown_ch_structures_sn) (struct xpc_partition *part);
  181. void (*xpc_indicate_partition_engaged) (struct xpc_partition *part);
  182. int (*xpc_partition_engaged) (short partid);
  183. int (*xpc_any_partition_engaged) (void);
  184. void (*xpc_indicate_partition_disengaged) (struct xpc_partition *part);
  185. void (*xpc_assume_partition_disengaged) (short partid);
  186. void (*xpc_send_chctl_closerequest) (struct xpc_channel *ch,
  187. unsigned long *irq_flags);
  188. void (*xpc_send_chctl_closereply) (struct xpc_channel *ch,
  189. unsigned long *irq_flags);
  190. void (*xpc_send_chctl_openrequest) (struct xpc_channel *ch,
  191. unsigned long *irq_flags);
  192. void (*xpc_send_chctl_openreply) (struct xpc_channel *ch,
  193. unsigned long *irq_flags);
  194. void (*xpc_send_chctl_opencomplete) (struct xpc_channel *ch,
  195. unsigned long *irq_flags);
  196. enum xp_retval (*xpc_save_remote_msgqueue_pa) (struct xpc_channel *ch,
  197. unsigned long msgqueue_pa);
  198. enum xp_retval (*xpc_send_payload) (struct xpc_channel *ch, u32 flags,
  199. void *payload, u16 payload_size,
  200. u8 notify_type, xpc_notify_func func,
  201. void *key);
  202. void (*xpc_received_payload) (struct xpc_channel *ch, void *payload);
  203. /*
  204. * Timer function to enforce the timelimit on the partition disengage.
  205. */
  206. static void
  207. xpc_timeout_partition_disengage(unsigned long data)
  208. {
  209. struct xpc_partition *part = (struct xpc_partition *)data;
  210. DBUG_ON(time_is_after_jiffies(part->disengage_timeout));
  211. (void)xpc_partition_disengaged(part);
  212. DBUG_ON(part->disengage_timeout != 0);
  213. DBUG_ON(xpc_partition_engaged(XPC_PARTID(part)));
  214. }
  215. /*
  216. * Timer to produce the heartbeat. The timer structures function is
  217. * already set when this is initially called. A tunable is used to
  218. * specify when the next timeout should occur.
  219. */
  220. static void
  221. xpc_hb_beater(unsigned long dummy)
  222. {
  223. xpc_increment_heartbeat();
  224. if (time_is_before_eq_jiffies(xpc_hb_check_timeout))
  225. wake_up_interruptible(&xpc_activate_IRQ_wq);
  226. xpc_hb_timer.expires = jiffies + (xpc_hb_interval * HZ);
  227. add_timer(&xpc_hb_timer);
  228. }
  229. static void
  230. xpc_start_hb_beater(void)
  231. {
  232. xpc_heartbeat_init();
  233. init_timer(&xpc_hb_timer);
  234. xpc_hb_timer.function = xpc_hb_beater;
  235. xpc_hb_beater(0);
  236. }
  237. static void
  238. xpc_stop_hb_beater(void)
  239. {
  240. del_timer_sync(&xpc_hb_timer);
  241. xpc_heartbeat_exit();
  242. }
  243. /*
  244. * At periodic intervals, scan through all active partitions and ensure
  245. * their heartbeat is still active. If not, the partition is deactivated.
  246. */
  247. static void
  248. xpc_check_remote_hb(void)
  249. {
  250. struct xpc_partition *part;
  251. short partid;
  252. enum xp_retval ret;
  253. for (partid = 0; partid < xp_max_npartitions; partid++) {
  254. if (xpc_exiting)
  255. break;
  256. if (partid == xp_partition_id)
  257. continue;
  258. part = &xpc_partitions[partid];
  259. if (part->act_state == XPC_P_AS_INACTIVE ||
  260. part->act_state == XPC_P_AS_DEACTIVATING) {
  261. continue;
  262. }
  263. ret = xpc_get_remote_heartbeat(part);
  264. if (ret != xpSuccess)
  265. XPC_DEACTIVATE_PARTITION(part, ret);
  266. }
  267. }
  268. /*
  269. * This thread is responsible for nearly all of the partition
  270. * activation/deactivation.
  271. */
  272. static int
  273. xpc_hb_checker(void *ignore)
  274. {
  275. int force_IRQ = 0;
  276. /* this thread was marked active by xpc_hb_init() */
  277. set_cpus_allowed_ptr(current, cpumask_of(XPC_HB_CHECK_CPU));
  278. /* set our heartbeating to other partitions into motion */
  279. xpc_hb_check_timeout = jiffies + (xpc_hb_check_interval * HZ);
  280. xpc_start_hb_beater();
  281. while (!xpc_exiting) {
  282. dev_dbg(xpc_part, "woke up with %d ticks rem; %d IRQs have "
  283. "been received\n",
  284. (int)(xpc_hb_check_timeout - jiffies),
  285. xpc_activate_IRQ_rcvd);
  286. /* checking of remote heartbeats is skewed by IRQ handling */
  287. if (time_is_before_eq_jiffies(xpc_hb_check_timeout)) {
  288. xpc_hb_check_timeout = jiffies +
  289. (xpc_hb_check_interval * HZ);
  290. dev_dbg(xpc_part, "checking remote heartbeats\n");
  291. xpc_check_remote_hb();
  292. /*
  293. * On sn2 we need to periodically recheck to ensure no
  294. * IRQ/amo pairs have been missed.
  295. */
  296. if (is_shub())
  297. force_IRQ = 1;
  298. }
  299. /* check for outstanding IRQs */
  300. if (xpc_activate_IRQ_rcvd > 0 || force_IRQ != 0) {
  301. force_IRQ = 0;
  302. dev_dbg(xpc_part, "processing activate IRQs "
  303. "received\n");
  304. xpc_process_activate_IRQ_rcvd();
  305. }
  306. /* wait for IRQ or timeout */
  307. (void)wait_event_interruptible(xpc_activate_IRQ_wq,
  308. (time_is_before_eq_jiffies(
  309. xpc_hb_check_timeout) ||
  310. xpc_activate_IRQ_rcvd > 0 ||
  311. xpc_exiting));
  312. }
  313. xpc_stop_hb_beater();
  314. dev_dbg(xpc_part, "heartbeat checker is exiting\n");
  315. /* mark this thread as having exited */
  316. complete(&xpc_hb_checker_exited);
  317. return 0;
  318. }
  319. /*
  320. * This thread will attempt to discover other partitions to activate
  321. * based on info provided by SAL. This new thread is short lived and
  322. * will exit once discovery is complete.
  323. */
  324. static int
  325. xpc_initiate_discovery(void *ignore)
  326. {
  327. xpc_discovery();
  328. dev_dbg(xpc_part, "discovery thread is exiting\n");
  329. /* mark this thread as having exited */
  330. complete(&xpc_discovery_exited);
  331. return 0;
  332. }
  333. /*
  334. * The first kthread assigned to a newly activated partition is the one
  335. * created by XPC HB with which it calls xpc_activating(). XPC hangs on to
  336. * that kthread until the partition is brought down, at which time that kthread
  337. * returns back to XPC HB. (The return of that kthread will signify to XPC HB
  338. * that XPC has dismantled all communication infrastructure for the associated
  339. * partition.) This kthread becomes the channel manager for that partition.
  340. *
  341. * Each active partition has a channel manager, who, besides connecting and
  342. * disconnecting channels, will ensure that each of the partition's connected
  343. * channels has the required number of assigned kthreads to get the work done.
  344. */
  345. static void
  346. xpc_channel_mgr(struct xpc_partition *part)
  347. {
  348. while (part->act_state != XPC_P_AS_DEACTIVATING ||
  349. atomic_read(&part->nchannels_active) > 0 ||
  350. !xpc_partition_disengaged(part)) {
  351. xpc_process_sent_chctl_flags(part);
  352. /*
  353. * Wait until we've been requested to activate kthreads or
  354. * all of the channel's message queues have been torn down or
  355. * a signal is pending.
  356. *
  357. * The channel_mgr_requests is set to 1 after being awakened,
  358. * This is done to prevent the channel mgr from making one pass
  359. * through the loop for each request, since he will
  360. * be servicing all the requests in one pass. The reason it's
  361. * set to 1 instead of 0 is so that other kthreads will know
  362. * that the channel mgr is running and won't bother trying to
  363. * wake him up.
  364. */
  365. atomic_dec(&part->channel_mgr_requests);
  366. (void)wait_event_interruptible(part->channel_mgr_wq,
  367. (atomic_read(&part->channel_mgr_requests) > 0 ||
  368. part->chctl.all_flags != 0 ||
  369. (part->act_state == XPC_P_AS_DEACTIVATING &&
  370. atomic_read(&part->nchannels_active) == 0 &&
  371. xpc_partition_disengaged(part))));
  372. atomic_set(&part->channel_mgr_requests, 1);
  373. }
  374. }
  375. /*
  376. * Guarantee that the kzalloc'd memory is cacheline aligned.
  377. */
  378. void *
  379. xpc_kzalloc_cacheline_aligned(size_t size, gfp_t flags, void **base)
  380. {
  381. /* see if kzalloc will give us cachline aligned memory by default */
  382. *base = kzalloc(size, flags);
  383. if (*base == NULL)
  384. return NULL;
  385. if ((u64)*base == L1_CACHE_ALIGN((u64)*base))
  386. return *base;
  387. kfree(*base);
  388. /* nope, we'll have to do it ourselves */
  389. *base = kzalloc(size + L1_CACHE_BYTES, flags);
  390. if (*base == NULL)
  391. return NULL;
  392. return (void *)L1_CACHE_ALIGN((u64)*base);
  393. }
  394. /*
  395. * Setup the channel structures necessary to support XPartition Communication
  396. * between the specified remote partition and the local one.
  397. */
  398. static enum xp_retval
  399. xpc_setup_ch_structures(struct xpc_partition *part)
  400. {
  401. enum xp_retval ret;
  402. int ch_number;
  403. struct xpc_channel *ch;
  404. short partid = XPC_PARTID(part);
  405. /*
  406. * Allocate all of the channel structures as a contiguous chunk of
  407. * memory.
  408. */
  409. DBUG_ON(part->channels != NULL);
  410. part->channels = kzalloc(sizeof(struct xpc_channel) * XPC_MAX_NCHANNELS,
  411. GFP_KERNEL);
  412. if (part->channels == NULL) {
  413. dev_err(xpc_chan, "can't get memory for channels\n");
  414. return xpNoMemory;
  415. }
  416. /* allocate the remote open and close args */
  417. part->remote_openclose_args =
  418. xpc_kzalloc_cacheline_aligned(XPC_OPENCLOSE_ARGS_SIZE,
  419. GFP_KERNEL, &part->
  420. remote_openclose_args_base);
  421. if (part->remote_openclose_args == NULL) {
  422. dev_err(xpc_chan, "can't get memory for remote connect args\n");
  423. ret = xpNoMemory;
  424. goto out_1;
  425. }
  426. part->chctl.all_flags = 0;
  427. spin_lock_init(&part->chctl_lock);
  428. atomic_set(&part->channel_mgr_requests, 1);
  429. init_waitqueue_head(&part->channel_mgr_wq);
  430. part->nchannels = XPC_MAX_NCHANNELS;
  431. atomic_set(&part->nchannels_active, 0);
  432. atomic_set(&part->nchannels_engaged, 0);
  433. for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
  434. ch = &part->channels[ch_number];
  435. ch->partid = partid;
  436. ch->number = ch_number;
  437. ch->flags = XPC_C_DISCONNECTED;
  438. atomic_set(&ch->kthreads_assigned, 0);
  439. atomic_set(&ch->kthreads_idle, 0);
  440. atomic_set(&ch->kthreads_active, 0);
  441. atomic_set(&ch->references, 0);
  442. atomic_set(&ch->n_to_notify, 0);
  443. spin_lock_init(&ch->lock);
  444. init_completion(&ch->wdisconnect_wait);
  445. atomic_set(&ch->n_on_msg_allocate_wq, 0);
  446. init_waitqueue_head(&ch->msg_allocate_wq);
  447. init_waitqueue_head(&ch->idle_wq);
  448. }
  449. ret = xpc_setup_ch_structures_sn(part);
  450. if (ret != xpSuccess)
  451. goto out_2;
  452. /*
  453. * With the setting of the partition setup_state to XPC_P_SS_SETUP,
  454. * we're declaring that this partition is ready to go.
  455. */
  456. part->setup_state = XPC_P_SS_SETUP;
  457. return xpSuccess;
  458. /* setup of ch structures failed */
  459. out_2:
  460. kfree(part->remote_openclose_args_base);
  461. part->remote_openclose_args = NULL;
  462. out_1:
  463. kfree(part->channels);
  464. part->channels = NULL;
  465. return ret;
  466. }
  467. /*
  468. * Teardown the channel structures necessary to support XPartition Communication
  469. * between the specified remote partition and the local one.
  470. */
  471. static void
  472. xpc_teardown_ch_structures(struct xpc_partition *part)
  473. {
  474. DBUG_ON(atomic_read(&part->nchannels_engaged) != 0);
  475. DBUG_ON(atomic_read(&part->nchannels_active) != 0);
  476. /*
  477. * Make this partition inaccessible to local processes by marking it
  478. * as no longer setup. Then wait before proceeding with the teardown
  479. * until all existing references cease.
  480. */
  481. DBUG_ON(part->setup_state != XPC_P_SS_SETUP);
  482. part->setup_state = XPC_P_SS_WTEARDOWN;
  483. wait_event(part->teardown_wq, (atomic_read(&part->references) == 0));
  484. /* now we can begin tearing down the infrastructure */
  485. xpc_teardown_ch_structures_sn(part);
  486. kfree(part->remote_openclose_args_base);
  487. part->remote_openclose_args = NULL;
  488. kfree(part->channels);
  489. part->channels = NULL;
  490. part->setup_state = XPC_P_SS_TORNDOWN;
  491. }
  492. /*
  493. * When XPC HB determines that a partition has come up, it will create a new
  494. * kthread and that kthread will call this function to attempt to set up the
  495. * basic infrastructure used for Cross Partition Communication with the newly
  496. * upped partition.
  497. *
  498. * The kthread that was created by XPC HB and which setup the XPC
  499. * infrastructure will remain assigned to the partition becoming the channel
  500. * manager for that partition until the partition is deactivating, at which
  501. * time the kthread will teardown the XPC infrastructure and then exit.
  502. */
  503. static int
  504. xpc_activating(void *__partid)
  505. {
  506. short partid = (u64)__partid;
  507. struct xpc_partition *part = &xpc_partitions[partid];
  508. unsigned long irq_flags;
  509. DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
  510. spin_lock_irqsave(&part->act_lock, irq_flags);
  511. if (part->act_state == XPC_P_AS_DEACTIVATING) {
  512. part->act_state = XPC_P_AS_INACTIVE;
  513. spin_unlock_irqrestore(&part->act_lock, irq_flags);
  514. part->remote_rp_pa = 0;
  515. return 0;
  516. }
  517. /* indicate the thread is activating */
  518. DBUG_ON(part->act_state != XPC_P_AS_ACTIVATION_REQ);
  519. part->act_state = XPC_P_AS_ACTIVATING;
  520. XPC_SET_REASON(part, 0, 0);
  521. spin_unlock_irqrestore(&part->act_lock, irq_flags);
  522. dev_dbg(xpc_part, "activating partition %d\n", partid);
  523. xpc_allow_hb(partid);
  524. if (xpc_setup_ch_structures(part) == xpSuccess) {
  525. (void)xpc_part_ref(part); /* this will always succeed */
  526. if (xpc_make_first_contact(part) == xpSuccess) {
  527. xpc_mark_partition_active(part);
  528. xpc_channel_mgr(part);
  529. /* won't return until partition is deactivating */
  530. }
  531. xpc_part_deref(part);
  532. xpc_teardown_ch_structures(part);
  533. }
  534. xpc_disallow_hb(partid);
  535. xpc_mark_partition_inactive(part);
  536. if (part->reason == xpReactivating) {
  537. /* interrupting ourselves results in activating partition */
  538. xpc_request_partition_reactivation(part);
  539. }
  540. return 0;
  541. }
  542. void
  543. xpc_activate_partition(struct xpc_partition *part)
  544. {
  545. short partid = XPC_PARTID(part);
  546. unsigned long irq_flags;
  547. struct task_struct *kthread;
  548. spin_lock_irqsave(&part->act_lock, irq_flags);
  549. DBUG_ON(part->act_state != XPC_P_AS_INACTIVE);
  550. part->act_state = XPC_P_AS_ACTIVATION_REQ;
  551. XPC_SET_REASON(part, xpCloneKThread, __LINE__);
  552. spin_unlock_irqrestore(&part->act_lock, irq_flags);
  553. kthread = kthread_run(xpc_activating, (void *)((u64)partid), "xpc%02d",
  554. partid);
  555. if (IS_ERR(kthread)) {
  556. spin_lock_irqsave(&part->act_lock, irq_flags);
  557. part->act_state = XPC_P_AS_INACTIVE;
  558. XPC_SET_REASON(part, xpCloneKThreadFailed, __LINE__);
  559. spin_unlock_irqrestore(&part->act_lock, irq_flags);
  560. }
  561. }
  562. void
  563. xpc_activate_kthreads(struct xpc_channel *ch, int needed)
  564. {
  565. int idle = atomic_read(&ch->kthreads_idle);
  566. int assigned = atomic_read(&ch->kthreads_assigned);
  567. int wakeup;
  568. DBUG_ON(needed <= 0);
  569. if (idle > 0) {
  570. wakeup = (needed > idle) ? idle : needed;
  571. needed -= wakeup;
  572. dev_dbg(xpc_chan, "wakeup %d idle kthreads, partid=%d, "
  573. "channel=%d\n", wakeup, ch->partid, ch->number);
  574. /* only wakeup the requested number of kthreads */
  575. wake_up_nr(&ch->idle_wq, wakeup);
  576. }
  577. if (needed <= 0)
  578. return;
  579. if (needed + assigned > ch->kthreads_assigned_limit) {
  580. needed = ch->kthreads_assigned_limit - assigned;
  581. if (needed <= 0)
  582. return;
  583. }
  584. dev_dbg(xpc_chan, "create %d new kthreads, partid=%d, channel=%d\n",
  585. needed, ch->partid, ch->number);
  586. xpc_create_kthreads(ch, needed, 0);
  587. }
  588. /*
  589. * This function is where XPC's kthreads wait for messages to deliver.
  590. */
  591. static void
  592. xpc_kthread_waitmsgs(struct xpc_partition *part, struct xpc_channel *ch)
  593. {
  594. do {
  595. /* deliver messages to their intended recipients */
  596. while (xpc_n_of_deliverable_payloads(ch) > 0 &&
  597. !(ch->flags & XPC_C_DISCONNECTING)) {
  598. xpc_deliver_payload(ch);
  599. }
  600. if (atomic_inc_return(&ch->kthreads_idle) >
  601. ch->kthreads_idle_limit) {
  602. /* too many idle kthreads on this channel */
  603. atomic_dec(&ch->kthreads_idle);
  604. break;
  605. }
  606. dev_dbg(xpc_chan, "idle kthread calling "
  607. "wait_event_interruptible_exclusive()\n");
  608. (void)wait_event_interruptible_exclusive(ch->idle_wq,
  609. (xpc_n_of_deliverable_payloads(ch) > 0 ||
  610. (ch->flags & XPC_C_DISCONNECTING)));
  611. atomic_dec(&ch->kthreads_idle);
  612. } while (!(ch->flags & XPC_C_DISCONNECTING));
  613. }
  614. static int
  615. xpc_kthread_start(void *args)
  616. {
  617. short partid = XPC_UNPACK_ARG1(args);
  618. u16 ch_number = XPC_UNPACK_ARG2(args);
  619. struct xpc_partition *part = &xpc_partitions[partid];
  620. struct xpc_channel *ch;
  621. int n_needed;
  622. unsigned long irq_flags;
  623. dev_dbg(xpc_chan, "kthread starting, partid=%d, channel=%d\n",
  624. partid, ch_number);
  625. ch = &part->channels[ch_number];
  626. if (!(ch->flags & XPC_C_DISCONNECTING)) {
  627. /* let registerer know that connection has been established */
  628. spin_lock_irqsave(&ch->lock, irq_flags);
  629. if (!(ch->flags & XPC_C_CONNECTEDCALLOUT)) {
  630. ch->flags |= XPC_C_CONNECTEDCALLOUT;
  631. spin_unlock_irqrestore(&ch->lock, irq_flags);
  632. xpc_connected_callout(ch);
  633. spin_lock_irqsave(&ch->lock, irq_flags);
  634. ch->flags |= XPC_C_CONNECTEDCALLOUT_MADE;
  635. spin_unlock_irqrestore(&ch->lock, irq_flags);
  636. /*
  637. * It is possible that while the callout was being
  638. * made that the remote partition sent some messages.
  639. * If that is the case, we may need to activate
  640. * additional kthreads to help deliver them. We only
  641. * need one less than total #of messages to deliver.
  642. */
  643. n_needed = xpc_n_of_deliverable_payloads(ch) - 1;
  644. if (n_needed > 0 && !(ch->flags & XPC_C_DISCONNECTING))
  645. xpc_activate_kthreads(ch, n_needed);
  646. } else {
  647. spin_unlock_irqrestore(&ch->lock, irq_flags);
  648. }
  649. xpc_kthread_waitmsgs(part, ch);
  650. }
  651. /* let registerer know that connection is disconnecting */
  652. spin_lock_irqsave(&ch->lock, irq_flags);
  653. if ((ch->flags & XPC_C_CONNECTEDCALLOUT_MADE) &&
  654. !(ch->flags & XPC_C_DISCONNECTINGCALLOUT)) {
  655. ch->flags |= XPC_C_DISCONNECTINGCALLOUT;
  656. spin_unlock_irqrestore(&ch->lock, irq_flags);
  657. xpc_disconnect_callout(ch, xpDisconnecting);
  658. spin_lock_irqsave(&ch->lock, irq_flags);
  659. ch->flags |= XPC_C_DISCONNECTINGCALLOUT_MADE;
  660. }
  661. spin_unlock_irqrestore(&ch->lock, irq_flags);
  662. if (atomic_dec_return(&ch->kthreads_assigned) == 0 &&
  663. atomic_dec_return(&part->nchannels_engaged) == 0) {
  664. xpc_indicate_partition_disengaged(part);
  665. }
  666. xpc_msgqueue_deref(ch);
  667. dev_dbg(xpc_chan, "kthread exiting, partid=%d, channel=%d\n",
  668. partid, ch_number);
  669. xpc_part_deref(part);
  670. return 0;
  671. }
  672. /*
  673. * For each partition that XPC has established communications with, there is
  674. * a minimum of one kernel thread assigned to perform any operation that
  675. * may potentially sleep or block (basically the callouts to the asynchronous
  676. * functions registered via xpc_connect()).
  677. *
  678. * Additional kthreads are created and destroyed by XPC as the workload
  679. * demands.
  680. *
  681. * A kthread is assigned to one of the active channels that exists for a given
  682. * partition.
  683. */
  684. void
  685. xpc_create_kthreads(struct xpc_channel *ch, int needed,
  686. int ignore_disconnecting)
  687. {
  688. unsigned long irq_flags;
  689. u64 args = XPC_PACK_ARGS(ch->partid, ch->number);
  690. struct xpc_partition *part = &xpc_partitions[ch->partid];
  691. struct task_struct *kthread;
  692. while (needed-- > 0) {
  693. /*
  694. * The following is done on behalf of the newly created
  695. * kthread. That kthread is responsible for doing the
  696. * counterpart to the following before it exits.
  697. */
  698. if (ignore_disconnecting) {
  699. if (!atomic_inc_not_zero(&ch->kthreads_assigned)) {
  700. /* kthreads assigned had gone to zero */
  701. BUG_ON(!(ch->flags &
  702. XPC_C_DISCONNECTINGCALLOUT_MADE));
  703. break;
  704. }
  705. } else if (ch->flags & XPC_C_DISCONNECTING) {
  706. break;
  707. } else if (atomic_inc_return(&ch->kthreads_assigned) == 1 &&
  708. atomic_inc_return(&part->nchannels_engaged) == 1) {
  709. xpc_indicate_partition_engaged(part);
  710. }
  711. (void)xpc_part_ref(part);
  712. xpc_msgqueue_ref(ch);
  713. kthread = kthread_run(xpc_kthread_start, (void *)args,
  714. "xpc%02dc%d", ch->partid, ch->number);
  715. if (IS_ERR(kthread)) {
  716. /* the fork failed */
  717. /*
  718. * NOTE: if (ignore_disconnecting &&
  719. * !(ch->flags & XPC_C_DISCONNECTINGCALLOUT)) is true,
  720. * then we'll deadlock if all other kthreads assigned
  721. * to this channel are blocked in the channel's
  722. * registerer, because the only thing that will unblock
  723. * them is the xpDisconnecting callout that this
  724. * failed kthread_run() would have made.
  725. */
  726. if (atomic_dec_return(&ch->kthreads_assigned) == 0 &&
  727. atomic_dec_return(&part->nchannels_engaged) == 0) {
  728. xpc_indicate_partition_disengaged(part);
  729. }
  730. xpc_msgqueue_deref(ch);
  731. xpc_part_deref(part);
  732. if (atomic_read(&ch->kthreads_assigned) <
  733. ch->kthreads_idle_limit) {
  734. /*
  735. * Flag this as an error only if we have an
  736. * insufficient #of kthreads for the channel
  737. * to function.
  738. */
  739. spin_lock_irqsave(&ch->lock, irq_flags);
  740. XPC_DISCONNECT_CHANNEL(ch, xpLackOfResources,
  741. &irq_flags);
  742. spin_unlock_irqrestore(&ch->lock, irq_flags);
  743. }
  744. break;
  745. }
  746. }
  747. }
  748. void
  749. xpc_disconnect_wait(int ch_number)
  750. {
  751. unsigned long irq_flags;
  752. short partid;
  753. struct xpc_partition *part;
  754. struct xpc_channel *ch;
  755. int wakeup_channel_mgr;
  756. /* now wait for all callouts to the caller's function to cease */
  757. for (partid = 0; partid < xp_max_npartitions; partid++) {
  758. part = &xpc_partitions[partid];
  759. if (!xpc_part_ref(part))
  760. continue;
  761. ch = &part->channels[ch_number];
  762. if (!(ch->flags & XPC_C_WDISCONNECT)) {
  763. xpc_part_deref(part);
  764. continue;
  765. }
  766. wait_for_completion(&ch->wdisconnect_wait);
  767. spin_lock_irqsave(&ch->lock, irq_flags);
  768. DBUG_ON(!(ch->flags & XPC_C_DISCONNECTED));
  769. wakeup_channel_mgr = 0;
  770. if (ch->delayed_chctl_flags) {
  771. if (part->act_state != XPC_P_AS_DEACTIVATING) {
  772. spin_lock(&part->chctl_lock);
  773. part->chctl.flags[ch->number] |=
  774. ch->delayed_chctl_flags;
  775. spin_unlock(&part->chctl_lock);
  776. wakeup_channel_mgr = 1;
  777. }
  778. ch->delayed_chctl_flags = 0;
  779. }
  780. ch->flags &= ~XPC_C_WDISCONNECT;
  781. spin_unlock_irqrestore(&ch->lock, irq_flags);
  782. if (wakeup_channel_mgr)
  783. xpc_wakeup_channel_mgr(part);
  784. xpc_part_deref(part);
  785. }
  786. }
  787. static int
  788. xpc_setup_partitions(void)
  789. {
  790. short partid;
  791. struct xpc_partition *part;
  792. xpc_partitions = kzalloc(sizeof(struct xpc_partition) *
  793. xp_max_npartitions, GFP_KERNEL);
  794. if (xpc_partitions == NULL) {
  795. dev_err(xpc_part, "can't get memory for partition structure\n");
  796. return -ENOMEM;
  797. }
  798. /*
  799. * The first few fields of each entry of xpc_partitions[] need to
  800. * be initialized now so that calls to xpc_connect() and
  801. * xpc_disconnect() can be made prior to the activation of any remote
  802. * partition. NOTE THAT NONE OF THE OTHER FIELDS BELONGING TO THESE
  803. * ENTRIES ARE MEANINGFUL UNTIL AFTER AN ENTRY'S CORRESPONDING
  804. * PARTITION HAS BEEN ACTIVATED.
  805. */
  806. for (partid = 0; partid < xp_max_npartitions; partid++) {
  807. part = &xpc_partitions[partid];
  808. DBUG_ON((u64)part != L1_CACHE_ALIGN((u64)part));
  809. part->activate_IRQ_rcvd = 0;
  810. spin_lock_init(&part->act_lock);
  811. part->act_state = XPC_P_AS_INACTIVE;
  812. XPC_SET_REASON(part, 0, 0);
  813. init_timer(&part->disengage_timer);
  814. part->disengage_timer.function =
  815. xpc_timeout_partition_disengage;
  816. part->disengage_timer.data = (unsigned long)part;
  817. part->setup_state = XPC_P_SS_UNSET;
  818. init_waitqueue_head(&part->teardown_wq);
  819. atomic_set(&part->references, 0);
  820. }
  821. return xpc_setup_partitions_sn();
  822. }
  823. static void
  824. xpc_teardown_partitions(void)
  825. {
  826. xpc_teardown_partitions_sn();
  827. kfree(xpc_partitions);
  828. }
  829. static void
  830. xpc_do_exit(enum xp_retval reason)
  831. {
  832. short partid;
  833. int active_part_count, printed_waiting_msg = 0;
  834. struct xpc_partition *part;
  835. unsigned long printmsg_time, disengage_timeout = 0;
  836. /* a 'rmmod XPC' and a 'reboot' cannot both end up here together */
  837. DBUG_ON(xpc_exiting == 1);
  838. /*
  839. * Let the heartbeat checker thread and the discovery thread
  840. * (if one is running) know that they should exit. Also wake up
  841. * the heartbeat checker thread in case it's sleeping.
  842. */
  843. xpc_exiting = 1;
  844. wake_up_interruptible(&xpc_activate_IRQ_wq);
  845. /* wait for the discovery thread to exit */
  846. wait_for_completion(&xpc_discovery_exited);
  847. /* wait for the heartbeat checker thread to exit */
  848. wait_for_completion(&xpc_hb_checker_exited);
  849. /* sleep for a 1/3 of a second or so */
  850. (void)msleep_interruptible(300);
  851. /* wait for all partitions to become inactive */
  852. printmsg_time = jiffies + (XPC_DEACTIVATE_PRINTMSG_INTERVAL * HZ);
  853. xpc_disengage_timedout = 0;
  854. do {
  855. active_part_count = 0;
  856. for (partid = 0; partid < xp_max_npartitions; partid++) {
  857. part = &xpc_partitions[partid];
  858. if (xpc_partition_disengaged(part) &&
  859. part->act_state == XPC_P_AS_INACTIVE) {
  860. continue;
  861. }
  862. active_part_count++;
  863. XPC_DEACTIVATE_PARTITION(part, reason);
  864. if (part->disengage_timeout > disengage_timeout)
  865. disengage_timeout = part->disengage_timeout;
  866. }
  867. if (xpc_any_partition_engaged()) {
  868. if (time_is_before_jiffies(printmsg_time)) {
  869. dev_info(xpc_part, "waiting for remote "
  870. "partitions to deactivate, timeout in "
  871. "%ld seconds\n", (disengage_timeout -
  872. jiffies) / HZ);
  873. printmsg_time = jiffies +
  874. (XPC_DEACTIVATE_PRINTMSG_INTERVAL * HZ);
  875. printed_waiting_msg = 1;
  876. }
  877. } else if (active_part_count > 0) {
  878. if (printed_waiting_msg) {
  879. dev_info(xpc_part, "waiting for local partition"
  880. " to deactivate\n");
  881. printed_waiting_msg = 0;
  882. }
  883. } else {
  884. if (!xpc_disengage_timedout) {
  885. dev_info(xpc_part, "all partitions have "
  886. "deactivated\n");
  887. }
  888. break;
  889. }
  890. /* sleep for a 1/3 of a second or so */
  891. (void)msleep_interruptible(300);
  892. } while (1);
  893. DBUG_ON(xpc_any_partition_engaged());
  894. xpc_teardown_rsvd_page();
  895. if (reason == xpUnloading) {
  896. (void)unregister_die_notifier(&xpc_die_notifier);
  897. (void)unregister_reboot_notifier(&xpc_reboot_notifier);
  898. }
  899. /* clear the interface to XPC's functions */
  900. xpc_clear_interface();
  901. if (xpc_sysctl)
  902. unregister_sysctl_table(xpc_sysctl);
  903. xpc_teardown_partitions();
  904. if (is_shub())
  905. xpc_exit_sn2();
  906. else if (is_uv())
  907. xpc_exit_uv();
  908. }
  909. /*
  910. * This function is called when the system is being rebooted.
  911. */
  912. static int
  913. xpc_system_reboot(struct notifier_block *nb, unsigned long event, void *unused)
  914. {
  915. enum xp_retval reason;
  916. switch (event) {
  917. case SYS_RESTART:
  918. reason = xpSystemReboot;
  919. break;
  920. case SYS_HALT:
  921. reason = xpSystemHalt;
  922. break;
  923. case SYS_POWER_OFF:
  924. reason = xpSystemPoweroff;
  925. break;
  926. default:
  927. reason = xpSystemGoingDown;
  928. }
  929. xpc_do_exit(reason);
  930. return NOTIFY_DONE;
  931. }
  932. /*
  933. * Notify other partitions to deactivate from us by first disengaging from all
  934. * references to our memory.
  935. */
  936. static void
  937. xpc_die_deactivate(void)
  938. {
  939. struct xpc_partition *part;
  940. short partid;
  941. int any_engaged;
  942. long keep_waiting;
  943. long wait_to_print;
  944. /* keep xpc_hb_checker thread from doing anything (just in case) */
  945. xpc_exiting = 1;
  946. xpc_disallow_all_hbs(); /*indicate we're deactivated */
  947. for (partid = 0; partid < xp_max_npartitions; partid++) {
  948. part = &xpc_partitions[partid];
  949. if (xpc_partition_engaged(partid) ||
  950. part->act_state != XPC_P_AS_INACTIVE) {
  951. xpc_request_partition_deactivation(part);
  952. xpc_indicate_partition_disengaged(part);
  953. }
  954. }
  955. /*
  956. * Though we requested that all other partitions deactivate from us,
  957. * we only wait until they've all disengaged or we've reached the
  958. * defined timelimit.
  959. *
  960. * Given that one iteration through the following while-loop takes
  961. * approximately 200 microseconds, calculate the #of loops to take
  962. * before bailing and the #of loops before printing a waiting message.
  963. */
  964. keep_waiting = xpc_disengage_timelimit * 1000 * 5;
  965. wait_to_print = XPC_DEACTIVATE_PRINTMSG_INTERVAL * 1000 * 5;
  966. while (1) {
  967. any_engaged = xpc_any_partition_engaged();
  968. if (!any_engaged) {
  969. dev_info(xpc_part, "all partitions have deactivated\n");
  970. break;
  971. }
  972. if (!keep_waiting--) {
  973. for (partid = 0; partid < xp_max_npartitions;
  974. partid++) {
  975. if (xpc_partition_engaged(partid)) {
  976. dev_info(xpc_part, "deactivate from "
  977. "remote partition %d timed "
  978. "out\n", partid);
  979. }
  980. }
  981. break;
  982. }
  983. if (!wait_to_print--) {
  984. dev_info(xpc_part, "waiting for remote partitions to "
  985. "deactivate, timeout in %ld seconds\n",
  986. keep_waiting / (1000 * 5));
  987. wait_to_print = XPC_DEACTIVATE_PRINTMSG_INTERVAL *
  988. 1000 * 5;
  989. }
  990. udelay(200);
  991. }
  992. }
  993. /*
  994. * This function is called when the system is being restarted or halted due
  995. * to some sort of system failure. If this is the case we need to notify the
  996. * other partitions to disengage from all references to our memory.
  997. * This function can also be called when our heartbeater could be offlined
  998. * for a time. In this case we need to notify other partitions to not worry
  999. * about the lack of a heartbeat.
  1000. */
  1001. static int
  1002. xpc_system_die(struct notifier_block *nb, unsigned long event, void *unused)
  1003. {
  1004. #ifdef CONFIG_IA64 /* !!! temporary kludge */
  1005. switch (event) {
  1006. case DIE_MACHINE_RESTART:
  1007. case DIE_MACHINE_HALT:
  1008. xpc_die_deactivate();
  1009. break;
  1010. case DIE_KDEBUG_ENTER:
  1011. /* Should lack of heartbeat be ignored by other partitions? */
  1012. if (!xpc_kdebug_ignore)
  1013. break;
  1014. /* fall through */
  1015. case DIE_MCA_MONARCH_ENTER:
  1016. case DIE_INIT_MONARCH_ENTER:
  1017. xpc_offline_heartbeat();
  1018. break;
  1019. case DIE_KDEBUG_LEAVE:
  1020. /* Is lack of heartbeat being ignored by other partitions? */
  1021. if (!xpc_kdebug_ignore)
  1022. break;
  1023. /* fall through */
  1024. case DIE_MCA_MONARCH_LEAVE:
  1025. case DIE_INIT_MONARCH_LEAVE:
  1026. xpc_online_heartbeat();
  1027. break;
  1028. }
  1029. #else
  1030. xpc_die_deactivate();
  1031. #endif
  1032. return NOTIFY_DONE;
  1033. }
  1034. int __init
  1035. xpc_init(void)
  1036. {
  1037. int ret;
  1038. struct task_struct *kthread;
  1039. dev_set_name(xpc_part, "part");
  1040. dev_set_name(xpc_chan, "chan");
  1041. if (is_shub()) {
  1042. /*
  1043. * The ia64-sn2 architecture supports at most 64 partitions.
  1044. * And the inability to unregister remote amos restricts us
  1045. * further to only support exactly 64 partitions on this
  1046. * architecture, no less.
  1047. */
  1048. if (xp_max_npartitions != 64) {
  1049. dev_err(xpc_part, "max #of partitions not set to 64\n");
  1050. ret = -EINVAL;
  1051. } else {
  1052. ret = xpc_init_sn2();
  1053. }
  1054. } else if (is_uv()) {
  1055. ret = xpc_init_uv();
  1056. } else {
  1057. ret = -ENODEV;
  1058. }
  1059. if (ret != 0)
  1060. return ret;
  1061. ret = xpc_setup_partitions();
  1062. if (ret != 0) {
  1063. dev_err(xpc_part, "can't get memory for partition structure\n");
  1064. goto out_1;
  1065. }
  1066. xpc_sysctl = register_sysctl_table(xpc_sys_dir);
  1067. /*
  1068. * Fill the partition reserved page with the information needed by
  1069. * other partitions to discover we are alive and establish initial
  1070. * communications.
  1071. */
  1072. ret = xpc_setup_rsvd_page();
  1073. if (ret != 0) {
  1074. dev_err(xpc_part, "can't setup our reserved page\n");
  1075. goto out_2;
  1076. }
  1077. /* add ourselves to the reboot_notifier_list */
  1078. ret = register_reboot_notifier(&xpc_reboot_notifier);
  1079. if (ret != 0)
  1080. dev_warn(xpc_part, "can't register reboot notifier\n");
  1081. /* add ourselves to the die_notifier list */
  1082. ret = register_die_notifier(&xpc_die_notifier);
  1083. if (ret != 0)
  1084. dev_warn(xpc_part, "can't register die notifier\n");
  1085. /*
  1086. * The real work-horse behind xpc. This processes incoming
  1087. * interrupts and monitors remote heartbeats.
  1088. */
  1089. kthread = kthread_run(xpc_hb_checker, NULL, XPC_HB_CHECK_THREAD_NAME);
  1090. if (IS_ERR(kthread)) {
  1091. dev_err(xpc_part, "failed while forking hb check thread\n");
  1092. ret = -EBUSY;
  1093. goto out_3;
  1094. }
  1095. /*
  1096. * Startup a thread that will attempt to discover other partitions to
  1097. * activate based on info provided by SAL. This new thread is short
  1098. * lived and will exit once discovery is complete.
  1099. */
  1100. kthread = kthread_run(xpc_initiate_discovery, NULL,
  1101. XPC_DISCOVERY_THREAD_NAME);
  1102. if (IS_ERR(kthread)) {
  1103. dev_err(xpc_part, "failed while forking discovery thread\n");
  1104. /* mark this new thread as a non-starter */
  1105. complete(&xpc_discovery_exited);
  1106. xpc_do_exit(xpUnloading);
  1107. return -EBUSY;
  1108. }
  1109. /* set the interface to point at XPC's functions */
  1110. xpc_set_interface(xpc_initiate_connect, xpc_initiate_disconnect,
  1111. xpc_initiate_send, xpc_initiate_send_notify,
  1112. xpc_initiate_received, xpc_initiate_partid_to_nasids);
  1113. return 0;
  1114. /* initialization was not successful */
  1115. out_3:
  1116. xpc_teardown_rsvd_page();
  1117. (void)unregister_die_notifier(&xpc_die_notifier);
  1118. (void)unregister_reboot_notifier(&xpc_reboot_notifier);
  1119. out_2:
  1120. if (xpc_sysctl)
  1121. unregister_sysctl_table(xpc_sysctl);
  1122. xpc_teardown_partitions();
  1123. out_1:
  1124. if (is_shub())
  1125. xpc_exit_sn2();
  1126. else if (is_uv())
  1127. xpc_exit_uv();
  1128. return ret;
  1129. }
  1130. module_init(xpc_init);
  1131. void __exit
  1132. xpc_exit(void)
  1133. {
  1134. xpc_do_exit(xpUnloading);
  1135. }
  1136. module_exit(xpc_exit);
  1137. MODULE_AUTHOR("Silicon Graphics, Inc.");
  1138. MODULE_DESCRIPTION("Cross Partition Communication (XPC) support");
  1139. MODULE_LICENSE("GPL");
  1140. module_param(xpc_hb_interval, int, 0);
  1141. MODULE_PARM_DESC(xpc_hb_interval, "Number of seconds between "
  1142. "heartbeat increments.");
  1143. module_param(xpc_hb_check_interval, int, 0);
  1144. MODULE_PARM_DESC(xpc_hb_check_interval, "Number of seconds between "
  1145. "heartbeat checks.");
  1146. module_param(xpc_disengage_timelimit, int, 0);
  1147. MODULE_PARM_DESC(xpc_disengage_timelimit, "Number of seconds to wait "
  1148. "for disengage to complete.");
  1149. module_param(xpc_kdebug_ignore, int, 0);
  1150. MODULE_PARM_DESC(xpc_kdebug_ignore, "Should lack of heartbeat be ignored by "
  1151. "other partitions when dropping into kdebug.");