xpc_main.c 38 KB

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