xpc_main.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244
  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-2005 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/sched.h>
  48. #include <linux/syscalls.h>
  49. #include <linux/cache.h>
  50. #include <linux/interrupt.h>
  51. #include <linux/slab.h>
  52. #include <linux/delay.h>
  53. #include <linux/reboot.h>
  54. #include <asm/sn/intr.h>
  55. #include <asm/sn/sn_sal.h>
  56. #include <asm/uaccess.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. /* systune related variables for /proc/sys directories */
  73. static int xpc_hb_interval = XPC_HB_DEFAULT_INTERVAL;
  74. static int xpc_hb_min_interval = 1;
  75. static int xpc_hb_max_interval = 10;
  76. static int xpc_hb_check_interval = XPC_HB_CHECK_DEFAULT_INTERVAL;
  77. static int xpc_hb_check_min_interval = 10;
  78. static int xpc_hb_check_max_interval = 120;
  79. int xpc_disengage_request_timelimit = XPC_DISENGAGE_REQUEST_DEFAULT_TIMELIMIT;
  80. static int xpc_disengage_request_min_timelimit = 0;
  81. static int xpc_disengage_request_max_timelimit = 120;
  82. static ctl_table xpc_sys_xpc_hb_dir[] = {
  83. {
  84. 1,
  85. "hb_interval",
  86. &xpc_hb_interval,
  87. sizeof(int),
  88. 0644,
  89. NULL,
  90. &proc_dointvec_minmax,
  91. &sysctl_intvec,
  92. NULL,
  93. &xpc_hb_min_interval,
  94. &xpc_hb_max_interval
  95. },
  96. {
  97. 2,
  98. "hb_check_interval",
  99. &xpc_hb_check_interval,
  100. sizeof(int),
  101. 0644,
  102. NULL,
  103. &proc_dointvec_minmax,
  104. &sysctl_intvec,
  105. NULL,
  106. &xpc_hb_check_min_interval,
  107. &xpc_hb_check_max_interval
  108. },
  109. {0}
  110. };
  111. static ctl_table xpc_sys_xpc_dir[] = {
  112. {
  113. 1,
  114. "hb",
  115. NULL,
  116. 0,
  117. 0555,
  118. xpc_sys_xpc_hb_dir
  119. },
  120. {
  121. 2,
  122. "disengage_request_timelimit",
  123. &xpc_disengage_request_timelimit,
  124. sizeof(int),
  125. 0644,
  126. NULL,
  127. &proc_dointvec_minmax,
  128. &sysctl_intvec,
  129. NULL,
  130. &xpc_disengage_request_min_timelimit,
  131. &xpc_disengage_request_max_timelimit
  132. },
  133. {0}
  134. };
  135. static ctl_table xpc_sys_dir[] = {
  136. {
  137. 1,
  138. "xpc",
  139. NULL,
  140. 0,
  141. 0555,
  142. xpc_sys_xpc_dir
  143. },
  144. {0}
  145. };
  146. static struct ctl_table_header *xpc_sysctl;
  147. /* #of IRQs received */
  148. static atomic_t xpc_act_IRQ_rcvd;
  149. /* IRQ handler notifies this wait queue on receipt of an IRQ */
  150. static DECLARE_WAIT_QUEUE_HEAD(xpc_act_IRQ_wq);
  151. static unsigned long xpc_hb_check_timeout;
  152. /* notification that the xpc_hb_checker thread has exited */
  153. static DECLARE_MUTEX_LOCKED(xpc_hb_checker_exited);
  154. /* notification that the xpc_discovery thread has exited */
  155. static DECLARE_MUTEX_LOCKED(xpc_discovery_exited);
  156. static struct timer_list xpc_hb_timer;
  157. static void xpc_kthread_waitmsgs(struct xpc_partition *, struct xpc_channel *);
  158. static int xpc_system_reboot(struct notifier_block *, unsigned long, void *);
  159. static struct notifier_block xpc_reboot_notifier = {
  160. .notifier_call = xpc_system_reboot,
  161. };
  162. /*
  163. * Timer function to enforce the timelimit on the partition disengage request.
  164. */
  165. static void
  166. xpc_timeout_partition_disengage_request(unsigned long data)
  167. {
  168. struct xpc_partition *part = (struct xpc_partition *) data;
  169. DBUG_ON(jiffies < part->disengage_request_timeout);
  170. (void) xpc_partition_disengaged(part);
  171. DBUG_ON(part->disengage_request_timeout != 0);
  172. DBUG_ON(xpc_partition_engaged(1UL << XPC_PARTID(part)) != 0);
  173. }
  174. /*
  175. * Notify the heartbeat check thread that an IRQ has been received.
  176. */
  177. static irqreturn_t
  178. xpc_act_IRQ_handler(int irq, void *dev_id, struct pt_regs *regs)
  179. {
  180. atomic_inc(&xpc_act_IRQ_rcvd);
  181. wake_up_interruptible(&xpc_act_IRQ_wq);
  182. return IRQ_HANDLED;
  183. }
  184. /*
  185. * Timer to produce the heartbeat. The timer structures function is
  186. * already set when this is initially called. A tunable is used to
  187. * specify when the next timeout should occur.
  188. */
  189. static void
  190. xpc_hb_beater(unsigned long dummy)
  191. {
  192. xpc_vars->heartbeat++;
  193. if (jiffies >= xpc_hb_check_timeout) {
  194. wake_up_interruptible(&xpc_act_IRQ_wq);
  195. }
  196. xpc_hb_timer.expires = jiffies + (xpc_hb_interval * HZ);
  197. add_timer(&xpc_hb_timer);
  198. }
  199. /*
  200. * This thread is responsible for nearly all of the partition
  201. * activation/deactivation.
  202. */
  203. static int
  204. xpc_hb_checker(void *ignore)
  205. {
  206. int last_IRQ_count = 0;
  207. int new_IRQ_count;
  208. int force_IRQ=0;
  209. /* this thread was marked active by xpc_hb_init() */
  210. daemonize(XPC_HB_CHECK_THREAD_NAME);
  211. set_cpus_allowed(current, cpumask_of_cpu(XPC_HB_CHECK_CPU));
  212. xpc_hb_check_timeout = jiffies + (xpc_hb_check_interval * HZ);
  213. while (!(volatile int) xpc_exiting) {
  214. dev_dbg(xpc_part, "woke up with %d ticks rem; %d IRQs have "
  215. "been received\n",
  216. (int) (xpc_hb_check_timeout - jiffies),
  217. atomic_read(&xpc_act_IRQ_rcvd) - last_IRQ_count);
  218. /* checking of remote heartbeats is skewed by IRQ handling */
  219. if (jiffies >= xpc_hb_check_timeout) {
  220. dev_dbg(xpc_part, "checking remote heartbeats\n");
  221. xpc_check_remote_hb();
  222. /*
  223. * We need to periodically recheck to ensure no
  224. * IPI/AMO pairs have been missed. That check
  225. * must always reset xpc_hb_check_timeout.
  226. */
  227. force_IRQ = 1;
  228. }
  229. /* check for outstanding IRQs */
  230. new_IRQ_count = atomic_read(&xpc_act_IRQ_rcvd);
  231. if (last_IRQ_count < new_IRQ_count || force_IRQ != 0) {
  232. force_IRQ = 0;
  233. dev_dbg(xpc_part, "found an IRQ to process; will be "
  234. "resetting xpc_hb_check_timeout\n");
  235. last_IRQ_count += xpc_identify_act_IRQ_sender();
  236. if (last_IRQ_count < new_IRQ_count) {
  237. /* retry once to help avoid missing AMO */
  238. (void) xpc_identify_act_IRQ_sender();
  239. }
  240. last_IRQ_count = new_IRQ_count;
  241. xpc_hb_check_timeout = jiffies +
  242. (xpc_hb_check_interval * HZ);
  243. }
  244. /* wait for IRQ or timeout */
  245. (void) wait_event_interruptible(xpc_act_IRQ_wq,
  246. (last_IRQ_count < atomic_read(&xpc_act_IRQ_rcvd) ||
  247. jiffies >= xpc_hb_check_timeout ||
  248. (volatile int) xpc_exiting));
  249. }
  250. dev_dbg(xpc_part, "heartbeat checker is exiting\n");
  251. /* mark this thread as having exited */
  252. up(&xpc_hb_checker_exited);
  253. return 0;
  254. }
  255. /*
  256. * This thread will attempt to discover other partitions to activate
  257. * based on info provided by SAL. This new thread is short lived and
  258. * will exit once discovery is complete.
  259. */
  260. static int
  261. xpc_initiate_discovery(void *ignore)
  262. {
  263. daemonize(XPC_DISCOVERY_THREAD_NAME);
  264. xpc_discovery();
  265. dev_dbg(xpc_part, "discovery thread is exiting\n");
  266. /* mark this thread as having exited */
  267. up(&xpc_discovery_exited);
  268. return 0;
  269. }
  270. /*
  271. * Establish first contact with the remote partititon. This involves pulling
  272. * the XPC per partition variables from the remote partition and waiting for
  273. * the remote partition to pull ours.
  274. */
  275. static enum xpc_retval
  276. xpc_make_first_contact(struct xpc_partition *part)
  277. {
  278. enum xpc_retval ret;
  279. while ((ret = xpc_pull_remote_vars_part(part)) != xpcSuccess) {
  280. if (ret != xpcRetry) {
  281. XPC_DEACTIVATE_PARTITION(part, ret);
  282. return ret;
  283. }
  284. dev_dbg(xpc_chan, "waiting to make first contact with "
  285. "partition %d\n", XPC_PARTID(part));
  286. /* wait a 1/4 of a second or so */
  287. (void) msleep_interruptible(250);
  288. if (part->act_state == XPC_P_DEACTIVATING) {
  289. return part->reason;
  290. }
  291. }
  292. return xpc_mark_partition_active(part);
  293. }
  294. /*
  295. * The first kthread assigned to a newly activated partition is the one
  296. * created by XPC HB with which it calls xpc_partition_up(). XPC hangs on to
  297. * that kthread until the partition is brought down, at which time that kthread
  298. * returns back to XPC HB. (The return of that kthread will signify to XPC HB
  299. * that XPC has dismantled all communication infrastructure for the associated
  300. * partition.) This kthread becomes the channel manager for that partition.
  301. *
  302. * Each active partition has a channel manager, who, besides connecting and
  303. * disconnecting channels, will ensure that each of the partition's connected
  304. * channels has the required number of assigned kthreads to get the work done.
  305. */
  306. static void
  307. xpc_channel_mgr(struct xpc_partition *part)
  308. {
  309. while (part->act_state != XPC_P_DEACTIVATING ||
  310. atomic_read(&part->nchannels_active) > 0 ||
  311. !xpc_partition_disengaged(part)) {
  312. xpc_process_channel_activity(part);
  313. /*
  314. * Wait until we've been requested to activate kthreads or
  315. * all of the channel's message queues have been torn down or
  316. * a signal is pending.
  317. *
  318. * The channel_mgr_requests is set to 1 after being awakened,
  319. * This is done to prevent the channel mgr from making one pass
  320. * through the loop for each request, since he will
  321. * be servicing all the requests in one pass. The reason it's
  322. * set to 1 instead of 0 is so that other kthreads will know
  323. * that the channel mgr is running and won't bother trying to
  324. * wake him up.
  325. */
  326. atomic_dec(&part->channel_mgr_requests);
  327. (void) wait_event_interruptible(part->channel_mgr_wq,
  328. (atomic_read(&part->channel_mgr_requests) > 0 ||
  329. (volatile u64) part->local_IPI_amo != 0 ||
  330. ((volatile u8) part->act_state ==
  331. XPC_P_DEACTIVATING &&
  332. atomic_read(&part->nchannels_active) == 0 &&
  333. xpc_partition_disengaged(part))));
  334. atomic_set(&part->channel_mgr_requests, 1);
  335. // >>> Does it need to wakeup periodically as well? In case we
  336. // >>> miscalculated the #of kthreads to wakeup or create?
  337. }
  338. }
  339. /*
  340. * When XPC HB determines that a partition has come up, it will create a new
  341. * kthread and that kthread will call this function to attempt to set up the
  342. * basic infrastructure used for Cross Partition Communication with the newly
  343. * upped partition.
  344. *
  345. * The kthread that was created by XPC HB and which setup the XPC
  346. * infrastructure will remain assigned to the partition until the partition
  347. * goes down. At which time the kthread will teardown the XPC infrastructure
  348. * and then exit.
  349. *
  350. * XPC HB will put the remote partition's XPC per partition specific variables
  351. * physical address into xpc_partitions[partid].remote_vars_part_pa prior to
  352. * calling xpc_partition_up().
  353. */
  354. static void
  355. xpc_partition_up(struct xpc_partition *part)
  356. {
  357. DBUG_ON(part->channels != NULL);
  358. dev_dbg(xpc_chan, "activating partition %d\n", XPC_PARTID(part));
  359. if (xpc_setup_infrastructure(part) != xpcSuccess) {
  360. return;
  361. }
  362. /*
  363. * The kthread that XPC HB called us with will become the
  364. * channel manager for this partition. It will not return
  365. * back to XPC HB until the partition's XPC infrastructure
  366. * has been dismantled.
  367. */
  368. (void) xpc_part_ref(part); /* this will always succeed */
  369. if (xpc_make_first_contact(part) == xpcSuccess) {
  370. xpc_channel_mgr(part);
  371. }
  372. xpc_part_deref(part);
  373. xpc_teardown_infrastructure(part);
  374. }
  375. static int
  376. xpc_activating(void *__partid)
  377. {
  378. partid_t partid = (u64) __partid;
  379. struct xpc_partition *part = &xpc_partitions[partid];
  380. unsigned long irq_flags;
  381. struct sched_param param = { sched_priority: MAX_RT_PRIO - 1 };
  382. int ret;
  383. DBUG_ON(partid <= 0 || partid >= XP_MAX_PARTITIONS);
  384. spin_lock_irqsave(&part->act_lock, irq_flags);
  385. if (part->act_state == XPC_P_DEACTIVATING) {
  386. part->act_state = XPC_P_INACTIVE;
  387. spin_unlock_irqrestore(&part->act_lock, irq_flags);
  388. part->remote_rp_pa = 0;
  389. return 0;
  390. }
  391. /* indicate the thread is activating */
  392. DBUG_ON(part->act_state != XPC_P_ACTIVATION_REQ);
  393. part->act_state = XPC_P_ACTIVATING;
  394. XPC_SET_REASON(part, 0, 0);
  395. spin_unlock_irqrestore(&part->act_lock, irq_flags);
  396. dev_dbg(xpc_part, "bringing partition %d up\n", partid);
  397. daemonize("xpc%02d", partid);
  398. /*
  399. * This thread needs to run at a realtime priority to prevent a
  400. * significant performance degradation.
  401. */
  402. ret = sched_setscheduler(current, SCHED_FIFO, &param);
  403. if (ret != 0) {
  404. dev_warn(xpc_part, "unable to set pid %d to a realtime "
  405. "priority, ret=%d\n", current->pid, ret);
  406. }
  407. /* allow this thread and its children to run on any CPU */
  408. set_cpus_allowed(current, CPU_MASK_ALL);
  409. /*
  410. * Register the remote partition's AMOs with SAL so it can handle
  411. * and cleanup errors within that address range should the remote
  412. * partition go down. We don't unregister this range because it is
  413. * difficult to tell when outstanding writes to the remote partition
  414. * are finished and thus when it is safe to unregister. This should
  415. * not result in wasted space in the SAL xp_addr_region table because
  416. * we should get the same page for remote_amos_page_pa after module
  417. * reloads and system reboots.
  418. */
  419. if (sn_register_xp_addr_region(part->remote_amos_page_pa,
  420. PAGE_SIZE, 1) < 0) {
  421. dev_warn(xpc_part, "xpc_partition_up(%d) failed to register "
  422. "xp_addr region\n", partid);
  423. spin_lock_irqsave(&part->act_lock, irq_flags);
  424. part->act_state = XPC_P_INACTIVE;
  425. XPC_SET_REASON(part, xpcPhysAddrRegFailed, __LINE__);
  426. spin_unlock_irqrestore(&part->act_lock, irq_flags);
  427. part->remote_rp_pa = 0;
  428. return 0;
  429. }
  430. xpc_allow_hb(partid, xpc_vars);
  431. xpc_IPI_send_activated(part);
  432. /*
  433. * xpc_partition_up() holds this thread and marks this partition as
  434. * XPC_P_ACTIVE by calling xpc_hb_mark_active().
  435. */
  436. (void) xpc_partition_up(part);
  437. xpc_disallow_hb(partid, xpc_vars);
  438. xpc_mark_partition_inactive(part);
  439. if (part->reason == xpcReactivating) {
  440. /* interrupting ourselves results in activating partition */
  441. xpc_IPI_send_reactivate(part);
  442. }
  443. return 0;
  444. }
  445. void
  446. xpc_activate_partition(struct xpc_partition *part)
  447. {
  448. partid_t partid = XPC_PARTID(part);
  449. unsigned long irq_flags;
  450. pid_t pid;
  451. spin_lock_irqsave(&part->act_lock, irq_flags);
  452. pid = kernel_thread(xpc_activating, (void *) ((u64) partid), 0);
  453. DBUG_ON(part->act_state != XPC_P_INACTIVE);
  454. if (pid > 0) {
  455. part->act_state = XPC_P_ACTIVATION_REQ;
  456. XPC_SET_REASON(part, xpcCloneKThread, __LINE__);
  457. } else {
  458. XPC_SET_REASON(part, xpcCloneKThreadFailed, __LINE__);
  459. }
  460. spin_unlock_irqrestore(&part->act_lock, irq_flags);
  461. }
  462. /*
  463. * Handle the receipt of a SGI_XPC_NOTIFY IRQ by seeing whether the specified
  464. * partition actually sent it. Since SGI_XPC_NOTIFY IRQs may be shared by more
  465. * than one partition, we use an AMO_t structure per partition to indicate
  466. * whether a partition has sent an IPI or not. >>> If it has, then wake up the
  467. * associated kthread to handle it.
  468. *
  469. * All SGI_XPC_NOTIFY IRQs received by XPC are the result of IPIs sent by XPC
  470. * running on other partitions.
  471. *
  472. * Noteworthy Arguments:
  473. *
  474. * irq - Interrupt ReQuest number. NOT USED.
  475. *
  476. * dev_id - partid of IPI's potential sender.
  477. *
  478. * regs - processor's context before the processor entered
  479. * interrupt code. NOT USED.
  480. */
  481. irqreturn_t
  482. xpc_notify_IRQ_handler(int irq, void *dev_id, struct pt_regs *regs)
  483. {
  484. partid_t partid = (partid_t) (u64) dev_id;
  485. struct xpc_partition *part = &xpc_partitions[partid];
  486. DBUG_ON(partid <= 0 || partid >= XP_MAX_PARTITIONS);
  487. if (xpc_part_ref(part)) {
  488. xpc_check_for_channel_activity(part);
  489. xpc_part_deref(part);
  490. }
  491. return IRQ_HANDLED;
  492. }
  493. /*
  494. * Check to see if xpc_notify_IRQ_handler() dropped any IPIs on the floor
  495. * because the write to their associated IPI amo completed after the IRQ/IPI
  496. * was received.
  497. */
  498. void
  499. xpc_dropped_IPI_check(struct xpc_partition *part)
  500. {
  501. if (xpc_part_ref(part)) {
  502. xpc_check_for_channel_activity(part);
  503. part->dropped_IPI_timer.expires = jiffies +
  504. XPC_P_DROPPED_IPI_WAIT;
  505. add_timer(&part->dropped_IPI_timer);
  506. xpc_part_deref(part);
  507. }
  508. }
  509. void
  510. xpc_activate_kthreads(struct xpc_channel *ch, int needed)
  511. {
  512. int idle = atomic_read(&ch->kthreads_idle);
  513. int assigned = atomic_read(&ch->kthreads_assigned);
  514. int wakeup;
  515. DBUG_ON(needed <= 0);
  516. if (idle > 0) {
  517. wakeup = (needed > idle) ? idle : needed;
  518. needed -= wakeup;
  519. dev_dbg(xpc_chan, "wakeup %d idle kthreads, partid=%d, "
  520. "channel=%d\n", wakeup, ch->partid, ch->number);
  521. /* only wakeup the requested number of kthreads */
  522. wake_up_nr(&ch->idle_wq, wakeup);
  523. }
  524. if (needed <= 0) {
  525. return;
  526. }
  527. if (needed + assigned > ch->kthreads_assigned_limit) {
  528. needed = ch->kthreads_assigned_limit - assigned;
  529. // >>>should never be less than 0
  530. if (needed <= 0) {
  531. return;
  532. }
  533. }
  534. dev_dbg(xpc_chan, "create %d new kthreads, partid=%d, channel=%d\n",
  535. needed, ch->partid, ch->number);
  536. xpc_create_kthreads(ch, needed);
  537. }
  538. /*
  539. * This function is where XPC's kthreads wait for messages to deliver.
  540. */
  541. static void
  542. xpc_kthread_waitmsgs(struct xpc_partition *part, struct xpc_channel *ch)
  543. {
  544. do {
  545. /* deliver messages to their intended recipients */
  546. while ((volatile s64) ch->w_local_GP.get <
  547. (volatile s64) ch->w_remote_GP.put &&
  548. !((volatile u32) ch->flags &
  549. XPC_C_DISCONNECTING)) {
  550. xpc_deliver_msg(ch);
  551. }
  552. if (atomic_inc_return(&ch->kthreads_idle) >
  553. ch->kthreads_idle_limit) {
  554. /* too many idle kthreads on this channel */
  555. atomic_dec(&ch->kthreads_idle);
  556. break;
  557. }
  558. dev_dbg(xpc_chan, "idle kthread calling "
  559. "wait_event_interruptible_exclusive()\n");
  560. (void) wait_event_interruptible_exclusive(ch->idle_wq,
  561. ((volatile s64) ch->w_local_GP.get <
  562. (volatile s64) ch->w_remote_GP.put ||
  563. ((volatile u32) ch->flags &
  564. XPC_C_DISCONNECTING)));
  565. atomic_dec(&ch->kthreads_idle);
  566. } while (!((volatile u32) ch->flags & XPC_C_DISCONNECTING));
  567. }
  568. static int
  569. xpc_daemonize_kthread(void *args)
  570. {
  571. partid_t partid = XPC_UNPACK_ARG1(args);
  572. u16 ch_number = XPC_UNPACK_ARG2(args);
  573. struct xpc_partition *part = &xpc_partitions[partid];
  574. struct xpc_channel *ch;
  575. int n_needed;
  576. unsigned long irq_flags;
  577. daemonize("xpc%02dc%d", partid, ch_number);
  578. dev_dbg(xpc_chan, "kthread starting, partid=%d, channel=%d\n",
  579. partid, ch_number);
  580. ch = &part->channels[ch_number];
  581. if (!(ch->flags & XPC_C_DISCONNECTING)) {
  582. /* let registerer know that connection has been established */
  583. spin_lock_irqsave(&ch->lock, irq_flags);
  584. if (!(ch->flags & XPC_C_CONNECTCALLOUT)) {
  585. ch->flags |= XPC_C_CONNECTCALLOUT;
  586. spin_unlock_irqrestore(&ch->lock, irq_flags);
  587. xpc_connected_callout(ch);
  588. /*
  589. * It is possible that while the callout was being
  590. * made that the remote partition sent some messages.
  591. * If that is the case, we may need to activate
  592. * additional kthreads to help deliver them. We only
  593. * need one less than total #of messages to deliver.
  594. */
  595. n_needed = ch->w_remote_GP.put - ch->w_local_GP.get - 1;
  596. if (n_needed > 0 &&
  597. !(ch->flags & XPC_C_DISCONNECTING)) {
  598. xpc_activate_kthreads(ch, n_needed);
  599. }
  600. } else {
  601. spin_unlock_irqrestore(&ch->lock, irq_flags);
  602. }
  603. xpc_kthread_waitmsgs(part, ch);
  604. }
  605. if (atomic_dec_return(&ch->kthreads_assigned) == 0) {
  606. spin_lock_irqsave(&ch->lock, irq_flags);
  607. if ((ch->flags & XPC_C_CONNECTCALLOUT) &&
  608. !(ch->flags & XPC_C_DISCONNECTCALLOUT)) {
  609. ch->flags |= XPC_C_DISCONNECTCALLOUT;
  610. spin_unlock_irqrestore(&ch->lock, irq_flags);
  611. xpc_disconnecting_callout(ch);
  612. } else {
  613. spin_unlock_irqrestore(&ch->lock, irq_flags);
  614. }
  615. if (atomic_dec_return(&part->nchannels_engaged) == 0) {
  616. xpc_mark_partition_disengaged(part);
  617. xpc_IPI_send_disengage(part);
  618. }
  619. }
  620. xpc_msgqueue_deref(ch);
  621. dev_dbg(xpc_chan, "kthread exiting, partid=%d, channel=%d\n",
  622. partid, ch_number);
  623. xpc_part_deref(part);
  624. return 0;
  625. }
  626. /*
  627. * For each partition that XPC has established communications with, there is
  628. * a minimum of one kernel thread assigned to perform any operation that
  629. * may potentially sleep or block (basically the callouts to the asynchronous
  630. * functions registered via xpc_connect()).
  631. *
  632. * Additional kthreads are created and destroyed by XPC as the workload
  633. * demands.
  634. *
  635. * A kthread is assigned to one of the active channels that exists for a given
  636. * partition.
  637. */
  638. void
  639. xpc_create_kthreads(struct xpc_channel *ch, int needed)
  640. {
  641. unsigned long irq_flags;
  642. pid_t pid;
  643. u64 args = XPC_PACK_ARGS(ch->partid, ch->number);
  644. struct xpc_partition *part = &xpc_partitions[ch->partid];
  645. while (needed-- > 0) {
  646. /*
  647. * The following is done on behalf of the newly created
  648. * kthread. That kthread is responsible for doing the
  649. * counterpart to the following before it exits.
  650. */
  651. (void) xpc_part_ref(part);
  652. xpc_msgqueue_ref(ch);
  653. if (atomic_inc_return(&ch->kthreads_assigned) == 1 &&
  654. atomic_inc_return(&part->nchannels_engaged) == 1) {
  655. xpc_mark_partition_engaged(part);
  656. }
  657. pid = kernel_thread(xpc_daemonize_kthread, (void *) args, 0);
  658. if (pid < 0) {
  659. /* the fork failed */
  660. if (atomic_dec_return(&ch->kthreads_assigned) == 0 &&
  661. atomic_dec_return(&part->nchannels_engaged) == 0) {
  662. xpc_mark_partition_disengaged(part);
  663. xpc_IPI_send_disengage(part);
  664. }
  665. xpc_msgqueue_deref(ch);
  666. xpc_part_deref(part);
  667. if (atomic_read(&ch->kthreads_assigned) <
  668. ch->kthreads_idle_limit) {
  669. /*
  670. * Flag this as an error only if we have an
  671. * insufficient #of kthreads for the channel
  672. * to function.
  673. *
  674. * No xpc_msgqueue_ref() is needed here since
  675. * the channel mgr is doing this.
  676. */
  677. spin_lock_irqsave(&ch->lock, irq_flags);
  678. XPC_DISCONNECT_CHANNEL(ch, xpcLackOfResources,
  679. &irq_flags);
  680. spin_unlock_irqrestore(&ch->lock, irq_flags);
  681. }
  682. break;
  683. }
  684. ch->kthreads_created++; // >>> temporary debug only!!!
  685. }
  686. }
  687. void
  688. xpc_disconnect_wait(int ch_number)
  689. {
  690. unsigned long irq_flags;
  691. partid_t partid;
  692. struct xpc_partition *part;
  693. struct xpc_channel *ch;
  694. int wakeup_channel_mgr;
  695. /* now wait for all callouts to the caller's function to cease */
  696. for (partid = 1; partid < XP_MAX_PARTITIONS; partid++) {
  697. part = &xpc_partitions[partid];
  698. if (!xpc_part_ref(part)) {
  699. continue;
  700. }
  701. ch = &part->channels[ch_number];
  702. if (!(ch->flags & XPC_C_WDISCONNECT)) {
  703. xpc_part_deref(part);
  704. continue;
  705. }
  706. (void) down(&ch->wdisconnect_sema);
  707. spin_lock_irqsave(&ch->lock, irq_flags);
  708. DBUG_ON(!(ch->flags & XPC_C_DISCONNECTED));
  709. wakeup_channel_mgr = 0;
  710. if (ch->delayed_IPI_flags) {
  711. if (part->act_state != XPC_P_DEACTIVATING) {
  712. spin_lock(&part->IPI_lock);
  713. XPC_SET_IPI_FLAGS(part->local_IPI_amo,
  714. ch->number, ch->delayed_IPI_flags);
  715. spin_unlock(&part->IPI_lock);
  716. wakeup_channel_mgr = 1;
  717. }
  718. ch->delayed_IPI_flags = 0;
  719. }
  720. ch->flags &= ~XPC_C_WDISCONNECT;
  721. spin_unlock_irqrestore(&ch->lock, irq_flags);
  722. if (wakeup_channel_mgr) {
  723. xpc_wakeup_channel_mgr(part);
  724. }
  725. xpc_part_deref(part);
  726. }
  727. }
  728. static void
  729. xpc_do_exit(enum xpc_retval reason)
  730. {
  731. partid_t partid;
  732. int active_part_count;
  733. struct xpc_partition *part;
  734. unsigned long printmsg_time;
  735. /* a 'rmmod XPC' and a 'reboot' cannot both end up here together */
  736. DBUG_ON(xpc_exiting == 1);
  737. /*
  738. * Let the heartbeat checker thread and the discovery thread
  739. * (if one is running) know that they should exit. Also wake up
  740. * the heartbeat checker thread in case it's sleeping.
  741. */
  742. xpc_exiting = 1;
  743. wake_up_interruptible(&xpc_act_IRQ_wq);
  744. /* ignore all incoming interrupts */
  745. free_irq(SGI_XPC_ACTIVATE, NULL);
  746. /* wait for the discovery thread to exit */
  747. down(&xpc_discovery_exited);
  748. /* wait for the heartbeat checker thread to exit */
  749. down(&xpc_hb_checker_exited);
  750. /* sleep for a 1/3 of a second or so */
  751. (void) msleep_interruptible(300);
  752. /* wait for all partitions to become inactive */
  753. printmsg_time = jiffies;
  754. do {
  755. active_part_count = 0;
  756. for (partid = 1; partid < XP_MAX_PARTITIONS; partid++) {
  757. part = &xpc_partitions[partid];
  758. if (xpc_partition_disengaged(part) &&
  759. part->act_state == XPC_P_INACTIVE) {
  760. continue;
  761. }
  762. active_part_count++;
  763. XPC_DEACTIVATE_PARTITION(part, reason);
  764. }
  765. if (active_part_count == 0) {
  766. break;
  767. }
  768. if (jiffies >= printmsg_time) {
  769. dev_info(xpc_part, "waiting for partitions to "
  770. "deactivate/disengage, active count=%d, remote "
  771. "engaged=0x%lx\n", active_part_count,
  772. xpc_partition_engaged(1UL << partid));
  773. printmsg_time = jiffies +
  774. (XPC_DISENGAGE_PRINTMSG_INTERVAL * HZ);
  775. }
  776. /* sleep for a 1/3 of a second or so */
  777. (void) msleep_interruptible(300);
  778. } while (1);
  779. DBUG_ON(xpc_partition_engaged(-1UL));
  780. /* indicate to others that our reserved page is uninitialized */
  781. xpc_rsvd_page->vars_pa = 0;
  782. /* now it's time to eliminate our heartbeat */
  783. del_timer_sync(&xpc_hb_timer);
  784. DBUG_ON(xpc_vars->heartbeating_to_mask != 0);
  785. /* take ourselves off of the reboot_notifier_list */
  786. (void) unregister_reboot_notifier(&xpc_reboot_notifier);
  787. /* close down protections for IPI operations */
  788. xpc_restrict_IPI_ops();
  789. /* clear the interface to XPC's functions */
  790. xpc_clear_interface();
  791. if (xpc_sysctl) {
  792. unregister_sysctl_table(xpc_sysctl);
  793. }
  794. }
  795. /*
  796. * This function is called when the system is being rebooted.
  797. */
  798. static int
  799. xpc_system_reboot(struct notifier_block *nb, unsigned long event, void *unused)
  800. {
  801. enum xpc_retval reason;
  802. switch (event) {
  803. case SYS_RESTART:
  804. reason = xpcSystemReboot;
  805. break;
  806. case SYS_HALT:
  807. reason = xpcSystemHalt;
  808. break;
  809. case SYS_POWER_OFF:
  810. reason = xpcSystemPoweroff;
  811. break;
  812. default:
  813. reason = xpcSystemGoingDown;
  814. }
  815. xpc_do_exit(reason);
  816. return NOTIFY_DONE;
  817. }
  818. int __init
  819. xpc_init(void)
  820. {
  821. int ret;
  822. partid_t partid;
  823. struct xpc_partition *part;
  824. pid_t pid;
  825. if (!ia64_platform_is("sn2")) {
  826. return -ENODEV;
  827. }
  828. /*
  829. * xpc_remote_copy_buffer is used as a temporary buffer for bte_copy'ng
  830. * various portions of a partition's reserved page. Its size is based
  831. * on the size of the reserved page header and part_nasids mask. So we
  832. * need to ensure that the other items will fit as well.
  833. */
  834. if (XPC_RP_VARS_SIZE > XPC_RP_HEADER_SIZE + XP_NASID_MASK_BYTES) {
  835. dev_err(xpc_part, "xpc_remote_copy_buffer is not big enough\n");
  836. return -EPERM;
  837. }
  838. DBUG_ON((u64) xpc_remote_copy_buffer !=
  839. L1_CACHE_ALIGN((u64) xpc_remote_copy_buffer));
  840. snprintf(xpc_part->bus_id, BUS_ID_SIZE, "part");
  841. snprintf(xpc_chan->bus_id, BUS_ID_SIZE, "chan");
  842. xpc_sysctl = register_sysctl_table(xpc_sys_dir, 1);
  843. /*
  844. * The first few fields of each entry of xpc_partitions[] need to
  845. * be initialized now so that calls to xpc_connect() and
  846. * xpc_disconnect() can be made prior to the activation of any remote
  847. * partition. NOTE THAT NONE OF THE OTHER FIELDS BELONGING TO THESE
  848. * ENTRIES ARE MEANINGFUL UNTIL AFTER AN ENTRY'S CORRESPONDING
  849. * PARTITION HAS BEEN ACTIVATED.
  850. */
  851. for (partid = 1; partid < XP_MAX_PARTITIONS; partid++) {
  852. part = &xpc_partitions[partid];
  853. DBUG_ON((u64) part != L1_CACHE_ALIGN((u64) part));
  854. part->act_IRQ_rcvd = 0;
  855. spin_lock_init(&part->act_lock);
  856. part->act_state = XPC_P_INACTIVE;
  857. XPC_SET_REASON(part, 0, 0);
  858. init_timer(&part->disengage_request_timer);
  859. part->disengage_request_timer.function =
  860. xpc_timeout_partition_disengage_request;
  861. part->disengage_request_timer.data = (unsigned long) part;
  862. part->setup_state = XPC_P_UNSET;
  863. init_waitqueue_head(&part->teardown_wq);
  864. atomic_set(&part->references, 0);
  865. }
  866. /*
  867. * Open up protections for IPI operations (and AMO operations on
  868. * Shub 1.1 systems).
  869. */
  870. xpc_allow_IPI_ops();
  871. /*
  872. * Interrupts being processed will increment this atomic variable and
  873. * awaken the heartbeat thread which will process the interrupts.
  874. */
  875. atomic_set(&xpc_act_IRQ_rcvd, 0);
  876. /*
  877. * This is safe to do before the xpc_hb_checker thread has started
  878. * because the handler releases a wait queue. If an interrupt is
  879. * received before the thread is waiting, it will not go to sleep,
  880. * but rather immediately process the interrupt.
  881. */
  882. ret = request_irq(SGI_XPC_ACTIVATE, xpc_act_IRQ_handler, 0,
  883. "xpc hb", NULL);
  884. if (ret != 0) {
  885. dev_err(xpc_part, "can't register ACTIVATE IRQ handler, "
  886. "errno=%d\n", -ret);
  887. xpc_restrict_IPI_ops();
  888. if (xpc_sysctl) {
  889. unregister_sysctl_table(xpc_sysctl);
  890. }
  891. return -EBUSY;
  892. }
  893. /*
  894. * Fill the partition reserved page with the information needed by
  895. * other partitions to discover we are alive and establish initial
  896. * communications.
  897. */
  898. xpc_rsvd_page = xpc_rsvd_page_init();
  899. if (xpc_rsvd_page == NULL) {
  900. dev_err(xpc_part, "could not setup our reserved page\n");
  901. free_irq(SGI_XPC_ACTIVATE, NULL);
  902. xpc_restrict_IPI_ops();
  903. if (xpc_sysctl) {
  904. unregister_sysctl_table(xpc_sysctl);
  905. }
  906. return -EBUSY;
  907. }
  908. /* add ourselves to the reboot_notifier_list */
  909. ret = register_reboot_notifier(&xpc_reboot_notifier);
  910. if (ret != 0) {
  911. dev_warn(xpc_part, "can't register reboot notifier\n");
  912. }
  913. /*
  914. * Set the beating to other partitions into motion. This is
  915. * the last requirement for other partitions' discovery to
  916. * initiate communications with us.
  917. */
  918. init_timer(&xpc_hb_timer);
  919. xpc_hb_timer.function = xpc_hb_beater;
  920. xpc_hb_beater(0);
  921. /*
  922. * The real work-horse behind xpc. This processes incoming
  923. * interrupts and monitors remote heartbeats.
  924. */
  925. pid = kernel_thread(xpc_hb_checker, NULL, 0);
  926. if (pid < 0) {
  927. dev_err(xpc_part, "failed while forking hb check thread\n");
  928. /* indicate to others that our reserved page is uninitialized */
  929. xpc_rsvd_page->vars_pa = 0;
  930. /* take ourselves off of the reboot_notifier_list */
  931. (void) unregister_reboot_notifier(&xpc_reboot_notifier);
  932. del_timer_sync(&xpc_hb_timer);
  933. free_irq(SGI_XPC_ACTIVATE, NULL);
  934. xpc_restrict_IPI_ops();
  935. if (xpc_sysctl) {
  936. unregister_sysctl_table(xpc_sysctl);
  937. }
  938. return -EBUSY;
  939. }
  940. /*
  941. * Startup a thread that will attempt to discover other partitions to
  942. * activate based on info provided by SAL. This new thread is short
  943. * lived and will exit once discovery is complete.
  944. */
  945. pid = kernel_thread(xpc_initiate_discovery, NULL, 0);
  946. if (pid < 0) {
  947. dev_err(xpc_part, "failed while forking discovery thread\n");
  948. /* mark this new thread as a non-starter */
  949. up(&xpc_discovery_exited);
  950. xpc_do_exit(xpcUnloading);
  951. return -EBUSY;
  952. }
  953. /* set the interface to point at XPC's functions */
  954. xpc_set_interface(xpc_initiate_connect, xpc_initiate_disconnect,
  955. xpc_initiate_allocate, xpc_initiate_send,
  956. xpc_initiate_send_notify, xpc_initiate_received,
  957. xpc_initiate_partid_to_nasids);
  958. return 0;
  959. }
  960. module_init(xpc_init);
  961. void __exit
  962. xpc_exit(void)
  963. {
  964. xpc_do_exit(xpcUnloading);
  965. }
  966. module_exit(xpc_exit);
  967. MODULE_AUTHOR("Silicon Graphics, Inc.");
  968. MODULE_DESCRIPTION("Cross Partition Communication (XPC) support");
  969. MODULE_LICENSE("GPL");
  970. module_param(xpc_hb_interval, int, 0);
  971. MODULE_PARM_DESC(xpc_hb_interval, "Number of seconds between "
  972. "heartbeat increments.");
  973. module_param(xpc_hb_check_interval, int, 0);
  974. MODULE_PARM_DESC(xpc_hb_check_interval, "Number of seconds between "
  975. "heartbeat checks.");
  976. module_param(xpc_disengage_request_timelimit, int, 0);
  977. MODULE_PARM_DESC(xpc_disengage_request_timelimit, "Number of seconds to wait "
  978. "for disengage request to complete.");