xpc_main.c 38 KB

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