xpc_main.c 34 KB

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