xpc_main.c 27 KB

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