xpc_sn2.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225
  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) 2008 Silicon Graphics, Inc. All Rights Reserved.
  7. */
  8. /*
  9. * Cross Partition Communication (XPC) sn2-based functions.
  10. *
  11. * Architecture specific implementation of common functions.
  12. *
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/delay.h>
  16. #include <asm/uncached.h>
  17. #include <asm/sn/sn_sal.h>
  18. #include "xpc.h"
  19. static struct xpc_vars_sn2 *xpc_vars; /* >>> Add _sn2 suffix? */
  20. static struct xpc_vars_part_sn2 *xpc_vars_part; /* >>> Add _sn2 suffix? */
  21. /* SH_IPI_ACCESS shub register value on startup */
  22. static u64 xpc_sh1_IPI_access;
  23. static u64 xpc_sh2_IPI_access0;
  24. static u64 xpc_sh2_IPI_access1;
  25. static u64 xpc_sh2_IPI_access2;
  26. static u64 xpc_sh2_IPI_access3;
  27. /*
  28. * Change protections to allow IPI operations.
  29. */
  30. static void
  31. xpc_allow_IPI_ops_sn2(void)
  32. {
  33. int node;
  34. int nasid;
  35. /* >>> The following should get moved into SAL. */
  36. if (is_shub2()) {
  37. xpc_sh2_IPI_access0 =
  38. (u64)HUB_L((u64 *)LOCAL_MMR_ADDR(SH2_IPI_ACCESS0));
  39. xpc_sh2_IPI_access1 =
  40. (u64)HUB_L((u64 *)LOCAL_MMR_ADDR(SH2_IPI_ACCESS1));
  41. xpc_sh2_IPI_access2 =
  42. (u64)HUB_L((u64 *)LOCAL_MMR_ADDR(SH2_IPI_ACCESS2));
  43. xpc_sh2_IPI_access3 =
  44. (u64)HUB_L((u64 *)LOCAL_MMR_ADDR(SH2_IPI_ACCESS3));
  45. for_each_online_node(node) {
  46. nasid = cnodeid_to_nasid(node);
  47. HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS0),
  48. -1UL);
  49. HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS1),
  50. -1UL);
  51. HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS2),
  52. -1UL);
  53. HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS3),
  54. -1UL);
  55. }
  56. } else {
  57. xpc_sh1_IPI_access =
  58. (u64)HUB_L((u64 *)LOCAL_MMR_ADDR(SH1_IPI_ACCESS));
  59. for_each_online_node(node) {
  60. nasid = cnodeid_to_nasid(node);
  61. HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH1_IPI_ACCESS),
  62. -1UL);
  63. }
  64. }
  65. }
  66. /*
  67. * Restrict protections to disallow IPI operations.
  68. */
  69. static void
  70. xpc_disallow_IPI_ops_sn2(void)
  71. {
  72. int node;
  73. int nasid;
  74. /* >>> The following should get moved into SAL. */
  75. if (is_shub2()) {
  76. for_each_online_node(node) {
  77. nasid = cnodeid_to_nasid(node);
  78. HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS0),
  79. xpc_sh2_IPI_access0);
  80. HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS1),
  81. xpc_sh2_IPI_access1);
  82. HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS2),
  83. xpc_sh2_IPI_access2);
  84. HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS3),
  85. xpc_sh2_IPI_access3);
  86. }
  87. } else {
  88. for_each_online_node(node) {
  89. nasid = cnodeid_to_nasid(node);
  90. HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH1_IPI_ACCESS),
  91. xpc_sh1_IPI_access);
  92. }
  93. }
  94. }
  95. /*
  96. * The following set of functions are used for the sending and receiving of
  97. * IRQs (also known as IPIs). There are two flavors of IRQs, one that is
  98. * associated with partition activity (SGI_XPC_ACTIVATE) and the other that
  99. * is associated with channel activity (SGI_XPC_NOTIFY).
  100. */
  101. static u64
  102. xpc_receive_IRQ_amo_sn2(struct amo *amo)
  103. {
  104. return FETCHOP_LOAD_OP(TO_AMO((u64)&amo->variable), FETCHOP_CLEAR);
  105. }
  106. static enum xp_retval
  107. xpc_send_IRQ_sn2(struct amo *amo, u64 flag, int nasid, int phys_cpuid,
  108. int vector)
  109. {
  110. int ret = 0;
  111. unsigned long irq_flags;
  112. local_irq_save(irq_flags);
  113. FETCHOP_STORE_OP(TO_AMO((u64)&amo->variable), FETCHOP_OR, flag);
  114. sn_send_IPI_phys(nasid, phys_cpuid, vector, 0);
  115. /*
  116. * We must always use the nofault function regardless of whether we
  117. * are on a Shub 1.1 system or a Shub 1.2 slice 0xc processor. If we
  118. * didn't, we'd never know that the other partition is down and would
  119. * keep sending IRQs and amos to it until the heartbeat times out.
  120. */
  121. ret = xp_nofault_PIOR((u64 *)GLOBAL_MMR_ADDR(NASID_GET(&amo->variable),
  122. xp_nofault_PIOR_target));
  123. local_irq_restore(irq_flags);
  124. return ((ret == 0) ? xpSuccess : xpPioReadError);
  125. }
  126. static struct amo *
  127. xpc_init_IRQ_amo_sn2(int index)
  128. {
  129. struct amo *amo = xpc_vars->amos_page + index;
  130. (void)xpc_receive_IRQ_amo_sn2(amo); /* clear amo variable */
  131. return amo;
  132. }
  133. /*
  134. * Functions associated with SGI_XPC_ACTIVATE IRQ.
  135. */
  136. /*
  137. * Notify the heartbeat check thread that an activate IRQ has been received.
  138. */
  139. static irqreturn_t
  140. xpc_handle_activate_IRQ_sn2(int irq, void *dev_id)
  141. {
  142. atomic_inc(&xpc_activate_IRQ_rcvd);
  143. wake_up_interruptible(&xpc_activate_IRQ_wq);
  144. return IRQ_HANDLED;
  145. }
  146. /*
  147. * Flag the appropriate amo variable and send an IRQ to the specified node.
  148. */
  149. static void
  150. xpc_send_activate_IRQ_sn2(u64 amos_page_pa, int from_nasid, int to_nasid,
  151. int to_phys_cpuid)
  152. {
  153. int w_index = XPC_NASID_W_INDEX(from_nasid);
  154. int b_index = XPC_NASID_B_INDEX(from_nasid);
  155. struct amo *amos = (struct amo *)__va(amos_page_pa +
  156. (XPC_ACTIVATE_IRQ_AMOS *
  157. sizeof(struct amo)));
  158. (void)xpc_send_IRQ_sn2(&amos[w_index], (1UL << b_index), to_nasid,
  159. to_phys_cpuid, SGI_XPC_ACTIVATE);
  160. }
  161. static void
  162. xpc_send_local_activate_IRQ_sn2(int from_nasid)
  163. {
  164. int w_index = XPC_NASID_W_INDEX(from_nasid);
  165. int b_index = XPC_NASID_B_INDEX(from_nasid);
  166. struct amo *amos = (struct amo *)__va(xpc_vars->amos_page_pa +
  167. (XPC_ACTIVATE_IRQ_AMOS *
  168. sizeof(struct amo)));
  169. /* fake the sending and receipt of an activate IRQ from remote nasid */
  170. FETCHOP_STORE_OP(TO_AMO((u64)&amos[w_index].variable), FETCHOP_OR,
  171. (1UL << b_index));
  172. atomic_inc(&xpc_activate_IRQ_rcvd);
  173. wake_up_interruptible(&xpc_activate_IRQ_wq);
  174. }
  175. /*
  176. * Functions associated with SGI_XPC_NOTIFY IRQ.
  177. */
  178. /*
  179. * Check to see if any chctl flags were sent from the specified partition.
  180. */
  181. static void
  182. xpc_check_for_sent_chctl_flags_sn2(struct xpc_partition *part)
  183. {
  184. union xpc_channel_ctl_flags chctl;
  185. unsigned long irq_flags;
  186. chctl.all_flags = xpc_receive_IRQ_amo_sn2(part->sn.sn2.
  187. local_chctl_amo_va);
  188. if (chctl.all_flags == 0)
  189. return;
  190. spin_lock_irqsave(&part->chctl_lock, irq_flags);
  191. part->chctl.all_flags |= chctl.all_flags;
  192. spin_unlock_irqrestore(&part->chctl_lock, irq_flags);
  193. dev_dbg(xpc_chan, "received notify IRQ from partid=%d, chctl.all_flags="
  194. "0x%lx\n", XPC_PARTID(part), chctl.all_flags);
  195. xpc_wakeup_channel_mgr(part);
  196. }
  197. /*
  198. * Handle the receipt of a SGI_XPC_NOTIFY IRQ by seeing whether the specified
  199. * partition actually sent it. Since SGI_XPC_NOTIFY IRQs may be shared by more
  200. * than one partition, we use an amo structure per partition to indicate
  201. * whether a partition has sent an IRQ or not. If it has, then wake up the
  202. * associated kthread to handle it.
  203. *
  204. * All SGI_XPC_NOTIFY IRQs received by XPC are the result of IRQs sent by XPC
  205. * running on other partitions.
  206. *
  207. * Noteworthy Arguments:
  208. *
  209. * irq - Interrupt ReQuest number. NOT USED.
  210. *
  211. * dev_id - partid of IRQ's potential sender.
  212. */
  213. static irqreturn_t
  214. xpc_handle_notify_IRQ_sn2(int irq, void *dev_id)
  215. {
  216. short partid = (short)(u64)dev_id;
  217. struct xpc_partition *part = &xpc_partitions[partid];
  218. DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
  219. if (xpc_part_ref(part)) {
  220. xpc_check_for_sent_chctl_flags_sn2(part);
  221. xpc_part_deref(part);
  222. }
  223. return IRQ_HANDLED;
  224. }
  225. /*
  226. * Check to see if xpc_handle_notify_IRQ_sn2() dropped any IRQs on the floor
  227. * because the write to their associated amo variable completed after the IRQ
  228. * was received.
  229. */
  230. static void
  231. xpc_check_for_dropped_notify_IRQ_sn2(struct xpc_partition *part)
  232. {
  233. struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
  234. if (xpc_part_ref(part)) {
  235. xpc_check_for_sent_chctl_flags_sn2(part);
  236. part_sn2->dropped_notify_IRQ_timer.expires = jiffies +
  237. XPC_DROPPED_NOTIFY_IRQ_WAIT_INTERVAL;
  238. add_timer(&part_sn2->dropped_notify_IRQ_timer);
  239. xpc_part_deref(part);
  240. }
  241. }
  242. /*
  243. * Send a notify IRQ to the remote partition that is associated with the
  244. * specified channel.
  245. */
  246. static void
  247. xpc_send_notify_IRQ_sn2(struct xpc_channel *ch, u8 chctl_flag,
  248. char *chctl_flag_string, unsigned long *irq_flags)
  249. {
  250. struct xpc_partition *part = &xpc_partitions[ch->partid];
  251. struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
  252. union xpc_channel_ctl_flags chctl = { 0 };
  253. enum xp_retval ret;
  254. if (likely(part->act_state != XPC_P_DEACTIVATING)) {
  255. chctl.flags[ch->number] = chctl_flag;
  256. ret = xpc_send_IRQ_sn2(part_sn2->remote_chctl_amo_va,
  257. chctl.all_flags,
  258. part_sn2->notify_IRQ_nasid,
  259. part_sn2->notify_IRQ_phys_cpuid,
  260. SGI_XPC_NOTIFY);
  261. dev_dbg(xpc_chan, "%s sent to partid=%d, channel=%d, ret=%d\n",
  262. chctl_flag_string, ch->partid, ch->number, ret);
  263. if (unlikely(ret != xpSuccess)) {
  264. if (irq_flags != NULL)
  265. spin_unlock_irqrestore(&ch->lock, *irq_flags);
  266. XPC_DEACTIVATE_PARTITION(part, ret);
  267. if (irq_flags != NULL)
  268. spin_lock_irqsave(&ch->lock, *irq_flags);
  269. }
  270. }
  271. }
  272. #define XPC_SEND_NOTIFY_IRQ_SN2(_ch, _ipi_f, _irq_f) \
  273. xpc_send_notify_IRQ_sn2(_ch, _ipi_f, #_ipi_f, _irq_f)
  274. /*
  275. * Make it look like the remote partition, which is associated with the
  276. * specified channel, sent us a notify IRQ. This faked IRQ will be handled
  277. * by xpc_check_for_dropped_notify_IRQ_sn2().
  278. */
  279. static void
  280. xpc_send_local_notify_IRQ_sn2(struct xpc_channel *ch, u8 chctl_flag,
  281. char *chctl_flag_string)
  282. {
  283. struct xpc_partition *part = &xpc_partitions[ch->partid];
  284. union xpc_channel_ctl_flags chctl = { 0 };
  285. chctl.flags[ch->number] = chctl_flag;
  286. FETCHOP_STORE_OP(TO_AMO((u64)&part->sn.sn2.local_chctl_amo_va->
  287. variable), FETCHOP_OR, chctl.all_flags);
  288. dev_dbg(xpc_chan, "%s sent local from partid=%d, channel=%d\n",
  289. chctl_flag_string, ch->partid, ch->number);
  290. }
  291. #define XPC_SEND_LOCAL_NOTIFY_IRQ_SN2(_ch, _ipi_f) \
  292. xpc_send_local_notify_IRQ_sn2(_ch, _ipi_f, #_ipi_f)
  293. static void
  294. xpc_send_chctl_closerequest_sn2(struct xpc_channel *ch,
  295. unsigned long *irq_flags)
  296. {
  297. struct xpc_openclose_args *args = ch->local_openclose_args;
  298. args->reason = ch->reason;
  299. XPC_SEND_NOTIFY_IRQ_SN2(ch, XPC_CHCTL_CLOSEREQUEST, irq_flags);
  300. }
  301. static void
  302. xpc_send_chctl_closereply_sn2(struct xpc_channel *ch, unsigned long *irq_flags)
  303. {
  304. XPC_SEND_NOTIFY_IRQ_SN2(ch, XPC_CHCTL_CLOSEREPLY, irq_flags);
  305. }
  306. static void
  307. xpc_send_chctl_openrequest_sn2(struct xpc_channel *ch, unsigned long *irq_flags)
  308. {
  309. struct xpc_openclose_args *args = ch->local_openclose_args;
  310. args->msg_size = ch->msg_size;
  311. args->local_nentries = ch->local_nentries;
  312. XPC_SEND_NOTIFY_IRQ_SN2(ch, XPC_CHCTL_OPENREQUEST, irq_flags);
  313. }
  314. static void
  315. xpc_send_chctl_openreply_sn2(struct xpc_channel *ch, unsigned long *irq_flags)
  316. {
  317. struct xpc_openclose_args *args = ch->local_openclose_args;
  318. args->remote_nentries = ch->remote_nentries;
  319. args->local_nentries = ch->local_nentries;
  320. args->local_msgqueue_pa = __pa(ch->local_msgqueue);
  321. XPC_SEND_NOTIFY_IRQ_SN2(ch, XPC_CHCTL_OPENREPLY, irq_flags);
  322. }
  323. static void
  324. xpc_send_chctl_msgrequest_sn2(struct xpc_channel *ch)
  325. {
  326. XPC_SEND_NOTIFY_IRQ_SN2(ch, XPC_CHCTL_MSGREQUEST, NULL);
  327. }
  328. static void
  329. xpc_send_chctl_local_msgrequest_sn2(struct xpc_channel *ch)
  330. {
  331. XPC_SEND_LOCAL_NOTIFY_IRQ_SN2(ch, XPC_CHCTL_MSGREQUEST);
  332. }
  333. /*
  334. * This next set of functions are used to keep track of when a partition is
  335. * potentially engaged in accessing memory belonging to another partition.
  336. */
  337. static void
  338. xpc_indicate_partition_engaged_sn2(struct xpc_partition *part)
  339. {
  340. unsigned long irq_flags;
  341. struct amo *amo = (struct amo *)__va(part->sn.sn2.remote_amos_page_pa +
  342. (XPC_ENGAGED_PARTITIONS_AMO *
  343. sizeof(struct amo)));
  344. local_irq_save(irq_flags);
  345. /* set bit corresponding to our partid in remote partition's amo */
  346. FETCHOP_STORE_OP(TO_AMO((u64)&amo->variable), FETCHOP_OR,
  347. (1UL << sn_partition_id));
  348. /*
  349. * We must always use the nofault function regardless of whether we
  350. * are on a Shub 1.1 system or a Shub 1.2 slice 0xc processor. If we
  351. * didn't, we'd never know that the other partition is down and would
  352. * keep sending IRQs and amos to it until the heartbeat times out.
  353. */
  354. (void)xp_nofault_PIOR((u64 *)GLOBAL_MMR_ADDR(NASID_GET(&amo->
  355. variable),
  356. xp_nofault_PIOR_target));
  357. local_irq_restore(irq_flags);
  358. }
  359. static void
  360. xpc_indicate_partition_disengaged_sn2(struct xpc_partition *part)
  361. {
  362. struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
  363. unsigned long irq_flags;
  364. struct amo *amo = (struct amo *)__va(part_sn2->remote_amos_page_pa +
  365. (XPC_ENGAGED_PARTITIONS_AMO *
  366. sizeof(struct amo)));
  367. local_irq_save(irq_flags);
  368. /* clear bit corresponding to our partid in remote partition's amo */
  369. FETCHOP_STORE_OP(TO_AMO((u64)&amo->variable), FETCHOP_AND,
  370. ~(1UL << sn_partition_id));
  371. /*
  372. * We must always use the nofault function regardless of whether we
  373. * are on a Shub 1.1 system or a Shub 1.2 slice 0xc processor. If we
  374. * didn't, we'd never know that the other partition is down and would
  375. * keep sending IRQs and amos to it until the heartbeat times out.
  376. */
  377. (void)xp_nofault_PIOR((u64 *)GLOBAL_MMR_ADDR(NASID_GET(&amo->
  378. variable),
  379. xp_nofault_PIOR_target));
  380. local_irq_restore(irq_flags);
  381. /*
  382. * Send activate IRQ to get other side to see that we've cleared our
  383. * bit in their engaged partitions amo.
  384. */
  385. xpc_send_activate_IRQ_sn2(part_sn2->remote_amos_page_pa,
  386. cnodeid_to_nasid(0),
  387. part_sn2->activate_IRQ_nasid,
  388. part_sn2->activate_IRQ_phys_cpuid);
  389. }
  390. static int
  391. xpc_partition_engaged_sn2(short partid)
  392. {
  393. struct amo *amo = xpc_vars->amos_page + XPC_ENGAGED_PARTITIONS_AMO;
  394. /* our partition's amo variable ANDed with partid mask */
  395. return (FETCHOP_LOAD_OP(TO_AMO((u64)&amo->variable), FETCHOP_LOAD) &
  396. (1UL << partid)) != 0;
  397. }
  398. static int
  399. xpc_any_partition_engaged_sn2(void)
  400. {
  401. struct amo *amo = xpc_vars->amos_page + XPC_ENGAGED_PARTITIONS_AMO;
  402. /* our partition's amo variable */
  403. return FETCHOP_LOAD_OP(TO_AMO((u64)&amo->variable), FETCHOP_LOAD) != 0;
  404. }
  405. static void
  406. xpc_assume_partition_disengaged_sn2(short partid)
  407. {
  408. struct amo *amo = xpc_vars->amos_page + XPC_ENGAGED_PARTITIONS_AMO;
  409. /* clear bit(s) based on partid mask in our partition's amo */
  410. FETCHOP_STORE_OP(TO_AMO((u64)&amo->variable), FETCHOP_AND,
  411. ~(1UL << partid));
  412. }
  413. /* original protection values for each node */
  414. static u64 xpc_prot_vec_sn2[MAX_NUMNODES];
  415. /*
  416. * Change protections to allow amo operations on non-Shub 1.1 systems.
  417. */
  418. static enum xp_retval
  419. xpc_allow_amo_ops_sn2(struct amo *amos_page)
  420. {
  421. u64 nasid_array = 0;
  422. int ret;
  423. /*
  424. * On SHUB 1.1, we cannot call sn_change_memprotect() since the BIST
  425. * collides with memory operations. On those systems we call
  426. * xpc_allow_amo_ops_shub_wars_1_1_sn2() instead.
  427. */
  428. if (!enable_shub_wars_1_1()) {
  429. ret = sn_change_memprotect(ia64_tpa((u64)amos_page), PAGE_SIZE,
  430. SN_MEMPROT_ACCESS_CLASS_1,
  431. &nasid_array);
  432. if (ret != 0)
  433. return xpSalError;
  434. }
  435. return xpSuccess;
  436. }
  437. /*
  438. * Change protections to allow amo operations on Shub 1.1 systems.
  439. */
  440. static void
  441. xpc_allow_amo_ops_shub_wars_1_1_sn2(void)
  442. {
  443. int node;
  444. int nasid;
  445. if (!enable_shub_wars_1_1())
  446. return;
  447. for_each_online_node(node) {
  448. nasid = cnodeid_to_nasid(node);
  449. /* save current protection values */
  450. xpc_prot_vec_sn2[node] =
  451. (u64)HUB_L((u64 *)GLOBAL_MMR_ADDR(nasid,
  452. SH1_MD_DQLP_MMR_DIR_PRIVEC0));
  453. /* open up everything */
  454. HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid,
  455. SH1_MD_DQLP_MMR_DIR_PRIVEC0),
  456. -1UL);
  457. HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid,
  458. SH1_MD_DQRP_MMR_DIR_PRIVEC0),
  459. -1UL);
  460. }
  461. }
  462. static enum xp_retval
  463. xpc_rsvd_page_init_sn2(struct xpc_rsvd_page *rp)
  464. {
  465. struct amo *amos_page;
  466. int i;
  467. int ret;
  468. xpc_vars = XPC_RP_VARS(rp);
  469. rp->sn.vars_pa = __pa(xpc_vars);
  470. /* vars_part array follows immediately after vars */
  471. xpc_vars_part = (struct xpc_vars_part_sn2 *)((u8 *)XPC_RP_VARS(rp) +
  472. XPC_RP_VARS_SIZE);
  473. /*
  474. * Before clearing xpc_vars, see if a page of amos had been previously
  475. * allocated. If not we'll need to allocate one and set permissions
  476. * so that cross-partition amos are allowed.
  477. *
  478. * The allocated amo page needs MCA reporting to remain disabled after
  479. * XPC has unloaded. To make this work, we keep a copy of the pointer
  480. * to this page (i.e., amos_page) in the struct xpc_vars structure,
  481. * which is pointed to by the reserved page, and re-use that saved copy
  482. * on subsequent loads of XPC. This amo page is never freed, and its
  483. * memory protections are never restricted.
  484. */
  485. amos_page = xpc_vars->amos_page;
  486. if (amos_page == NULL) {
  487. amos_page = (struct amo *)TO_AMO(uncached_alloc_page(0, 1));
  488. if (amos_page == NULL) {
  489. dev_err(xpc_part, "can't allocate page of amos\n");
  490. return xpNoMemory;
  491. }
  492. /*
  493. * Open up amo-R/W to cpu. This is done on Shub 1.1 systems
  494. * when xpc_allow_amo_ops_shub_wars_1_1_sn2() is called.
  495. */
  496. ret = xpc_allow_amo_ops_sn2(amos_page);
  497. if (ret != xpSuccess) {
  498. dev_err(xpc_part, "can't allow amo operations\n");
  499. uncached_free_page(__IA64_UNCACHED_OFFSET |
  500. TO_PHYS((u64)amos_page), 1);
  501. return ret;
  502. }
  503. }
  504. /* clear xpc_vars */
  505. memset(xpc_vars, 0, sizeof(struct xpc_vars_sn2));
  506. xpc_vars->version = XPC_V_VERSION;
  507. xpc_vars->activate_IRQ_nasid = cpuid_to_nasid(0);
  508. xpc_vars->activate_IRQ_phys_cpuid = cpu_physical_id(0);
  509. xpc_vars->vars_part_pa = __pa(xpc_vars_part);
  510. xpc_vars->amos_page_pa = ia64_tpa((u64)amos_page);
  511. xpc_vars->amos_page = amos_page; /* save for next load of XPC */
  512. /* clear xpc_vars_part */
  513. memset((u64 *)xpc_vars_part, 0, sizeof(struct xpc_vars_part_sn2) *
  514. xp_max_npartitions);
  515. /* initialize the activate IRQ related amo variables */
  516. for (i = 0; i < xp_nasid_mask_words; i++)
  517. (void)xpc_init_IRQ_amo_sn2(XPC_ACTIVATE_IRQ_AMOS + i);
  518. /* initialize the engaged remote partitions related amo variables */
  519. (void)xpc_init_IRQ_amo_sn2(XPC_ENGAGED_PARTITIONS_AMO);
  520. (void)xpc_init_IRQ_amo_sn2(XPC_DEACTIVATE_REQUEST_AMO);
  521. return xpSuccess;
  522. }
  523. static void
  524. xpc_increment_heartbeat_sn2(void)
  525. {
  526. xpc_vars->heartbeat++;
  527. }
  528. static void
  529. xpc_offline_heartbeat_sn2(void)
  530. {
  531. xpc_increment_heartbeat_sn2();
  532. xpc_vars->heartbeat_offline = 1;
  533. }
  534. static void
  535. xpc_online_heartbeat_sn2(void)
  536. {
  537. xpc_increment_heartbeat_sn2();
  538. xpc_vars->heartbeat_offline = 0;
  539. }
  540. static void
  541. xpc_heartbeat_init_sn2(void)
  542. {
  543. DBUG_ON(xpc_vars == NULL);
  544. bitmap_zero(xpc_vars->heartbeating_to_mask, XP_MAX_NPARTITIONS_SN2);
  545. xpc_heartbeating_to_mask = &xpc_vars->heartbeating_to_mask[0];
  546. xpc_online_heartbeat_sn2();
  547. }
  548. static void
  549. xpc_heartbeat_exit_sn2(void)
  550. {
  551. xpc_offline_heartbeat_sn2();
  552. }
  553. /*
  554. * At periodic intervals, scan through all active partitions and ensure
  555. * their heartbeat is still active. If not, the partition is deactivated.
  556. */
  557. static void
  558. xpc_check_remote_hb_sn2(void)
  559. {
  560. struct xpc_vars_sn2 *remote_vars;
  561. struct xpc_partition *part;
  562. short partid;
  563. enum xp_retval ret;
  564. remote_vars = (struct xpc_vars_sn2 *)xpc_remote_copy_buffer;
  565. for (partid = 0; partid < xp_max_npartitions; partid++) {
  566. if (xpc_exiting)
  567. break;
  568. if (partid == sn_partition_id)
  569. continue;
  570. part = &xpc_partitions[partid];
  571. if (part->act_state == XPC_P_INACTIVE ||
  572. part->act_state == XPC_P_DEACTIVATING) {
  573. continue;
  574. }
  575. /* pull the remote_hb cache line */
  576. ret = xp_remote_memcpy(remote_vars,
  577. (void *)part->sn.sn2.remote_vars_pa,
  578. XPC_RP_VARS_SIZE);
  579. if (ret != xpSuccess) {
  580. XPC_DEACTIVATE_PARTITION(part, ret);
  581. continue;
  582. }
  583. dev_dbg(xpc_part, "partid = %d, heartbeat = %ld, last_heartbeat"
  584. " = %ld, heartbeat_offline = %ld, HB_mask[0] = 0x%lx\n",
  585. partid, remote_vars->heartbeat, part->last_heartbeat,
  586. remote_vars->heartbeat_offline,
  587. remote_vars->heartbeating_to_mask[0]);
  588. if (((remote_vars->heartbeat == part->last_heartbeat) &&
  589. (remote_vars->heartbeat_offline == 0)) ||
  590. !xpc_hb_allowed(sn_partition_id,
  591. &remote_vars->heartbeating_to_mask)) {
  592. XPC_DEACTIVATE_PARTITION(part, xpNoHeartbeat);
  593. continue;
  594. }
  595. part->last_heartbeat = remote_vars->heartbeat;
  596. }
  597. }
  598. /*
  599. * Get a copy of the remote partition's XPC variables from the reserved page.
  600. *
  601. * remote_vars points to a buffer that is cacheline aligned for BTE copies and
  602. * assumed to be of size XPC_RP_VARS_SIZE.
  603. */
  604. static enum xp_retval
  605. xpc_get_remote_vars_sn2(u64 remote_vars_pa, struct xpc_vars_sn2 *remote_vars)
  606. {
  607. enum xp_retval ret;
  608. if (remote_vars_pa == 0)
  609. return xpVarsNotSet;
  610. /* pull over the cross partition variables */
  611. ret = xp_remote_memcpy(remote_vars, (void *)remote_vars_pa,
  612. XPC_RP_VARS_SIZE);
  613. if (ret != xpSuccess)
  614. return ret;
  615. if (XPC_VERSION_MAJOR(remote_vars->version) !=
  616. XPC_VERSION_MAJOR(XPC_V_VERSION)) {
  617. return xpBadVersion;
  618. }
  619. return xpSuccess;
  620. }
  621. static void
  622. xpc_request_partition_activation_sn2(struct xpc_rsvd_page *remote_rp,
  623. u64 remote_rp_pa, int nasid)
  624. {
  625. xpc_send_local_activate_IRQ_sn2(nasid);
  626. }
  627. static void
  628. xpc_request_partition_reactivation_sn2(struct xpc_partition *part)
  629. {
  630. xpc_send_local_activate_IRQ_sn2(part->sn.sn2.activate_IRQ_nasid);
  631. }
  632. static void
  633. xpc_request_partition_deactivation_sn2(struct xpc_partition *part)
  634. {
  635. struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
  636. unsigned long irq_flags;
  637. struct amo *amo = (struct amo *)__va(part_sn2->remote_amos_page_pa +
  638. (XPC_DEACTIVATE_REQUEST_AMO *
  639. sizeof(struct amo)));
  640. local_irq_save(irq_flags);
  641. /* set bit corresponding to our partid in remote partition's amo */
  642. FETCHOP_STORE_OP(TO_AMO((u64)&amo->variable), FETCHOP_OR,
  643. (1UL << sn_partition_id));
  644. /*
  645. * We must always use the nofault function regardless of whether we
  646. * are on a Shub 1.1 system or a Shub 1.2 slice 0xc processor. If we
  647. * didn't, we'd never know that the other partition is down and would
  648. * keep sending IRQs and amos to it until the heartbeat times out.
  649. */
  650. (void)xp_nofault_PIOR((u64 *)GLOBAL_MMR_ADDR(NASID_GET(&amo->
  651. variable),
  652. xp_nofault_PIOR_target));
  653. local_irq_restore(irq_flags);
  654. /*
  655. * Send activate IRQ to get other side to see that we've set our
  656. * bit in their deactivate request amo.
  657. */
  658. xpc_send_activate_IRQ_sn2(part_sn2->remote_amos_page_pa,
  659. cnodeid_to_nasid(0),
  660. part_sn2->activate_IRQ_nasid,
  661. part_sn2->activate_IRQ_phys_cpuid);
  662. }
  663. static void
  664. xpc_cancel_partition_deactivation_request_sn2(struct xpc_partition *part)
  665. {
  666. unsigned long irq_flags;
  667. struct amo *amo = (struct amo *)__va(part->sn.sn2.remote_amos_page_pa +
  668. (XPC_DEACTIVATE_REQUEST_AMO *
  669. sizeof(struct amo)));
  670. local_irq_save(irq_flags);
  671. /* clear bit corresponding to our partid in remote partition's amo */
  672. FETCHOP_STORE_OP(TO_AMO((u64)&amo->variable), FETCHOP_AND,
  673. ~(1UL << sn_partition_id));
  674. /*
  675. * We must always use the nofault function regardless of whether we
  676. * are on a Shub 1.1 system or a Shub 1.2 slice 0xc processor. If we
  677. * didn't, we'd never know that the other partition is down and would
  678. * keep sending IRQs and amos to it until the heartbeat times out.
  679. */
  680. (void)xp_nofault_PIOR((u64 *)GLOBAL_MMR_ADDR(NASID_GET(&amo->
  681. variable),
  682. xp_nofault_PIOR_target));
  683. local_irq_restore(irq_flags);
  684. }
  685. static int
  686. xpc_partition_deactivation_requested_sn2(short partid)
  687. {
  688. struct amo *amo = xpc_vars->amos_page + XPC_DEACTIVATE_REQUEST_AMO;
  689. /* our partition's amo variable ANDed with partid mask */
  690. return (FETCHOP_LOAD_OP(TO_AMO((u64)&amo->variable), FETCHOP_LOAD) &
  691. (1UL << partid)) != 0;
  692. }
  693. /*
  694. * Update the remote partition's info.
  695. */
  696. static void
  697. xpc_update_partition_info_sn2(struct xpc_partition *part, u8 remote_rp_version,
  698. unsigned long *remote_rp_stamp, u64 remote_rp_pa,
  699. u64 remote_vars_pa,
  700. struct xpc_vars_sn2 *remote_vars)
  701. {
  702. struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
  703. part->remote_rp_version = remote_rp_version;
  704. dev_dbg(xpc_part, " remote_rp_version = 0x%016x\n",
  705. part->remote_rp_version);
  706. part->remote_rp_stamp = *remote_rp_stamp;
  707. dev_dbg(xpc_part, " remote_rp_stamp = 0x%016lx\n",
  708. part->remote_rp_stamp);
  709. part->remote_rp_pa = remote_rp_pa;
  710. dev_dbg(xpc_part, " remote_rp_pa = 0x%016lx\n", part->remote_rp_pa);
  711. part_sn2->remote_vars_pa = remote_vars_pa;
  712. dev_dbg(xpc_part, " remote_vars_pa = 0x%016lx\n",
  713. part_sn2->remote_vars_pa);
  714. part->last_heartbeat = remote_vars->heartbeat;
  715. dev_dbg(xpc_part, " last_heartbeat = 0x%016lx\n",
  716. part->last_heartbeat);
  717. part_sn2->remote_vars_part_pa = remote_vars->vars_part_pa;
  718. dev_dbg(xpc_part, " remote_vars_part_pa = 0x%016lx\n",
  719. part_sn2->remote_vars_part_pa);
  720. part_sn2->activate_IRQ_nasid = remote_vars->activate_IRQ_nasid;
  721. dev_dbg(xpc_part, " activate_IRQ_nasid = 0x%x\n",
  722. part_sn2->activate_IRQ_nasid);
  723. part_sn2->activate_IRQ_phys_cpuid =
  724. remote_vars->activate_IRQ_phys_cpuid;
  725. dev_dbg(xpc_part, " activate_IRQ_phys_cpuid = 0x%x\n",
  726. part_sn2->activate_IRQ_phys_cpuid);
  727. part_sn2->remote_amos_page_pa = remote_vars->amos_page_pa;
  728. dev_dbg(xpc_part, " remote_amos_page_pa = 0x%lx\n",
  729. part_sn2->remote_amos_page_pa);
  730. part_sn2->remote_vars_version = remote_vars->version;
  731. dev_dbg(xpc_part, " remote_vars_version = 0x%x\n",
  732. part_sn2->remote_vars_version);
  733. }
  734. /*
  735. * Prior code has determined the nasid which generated a activate IRQ.
  736. * Inspect that nasid to determine if its partition needs to be activated
  737. * or deactivated.
  738. *
  739. * A partition is considered "awaiting activation" if our partition
  740. * flags indicate it is not active and it has a heartbeat. A
  741. * partition is considered "awaiting deactivation" if our partition
  742. * flags indicate it is active but it has no heartbeat or it is not
  743. * sending its heartbeat to us.
  744. *
  745. * To determine the heartbeat, the remote nasid must have a properly
  746. * initialized reserved page.
  747. */
  748. static void
  749. xpc_identify_activate_IRQ_req_sn2(int nasid)
  750. {
  751. struct xpc_rsvd_page *remote_rp;
  752. struct xpc_vars_sn2 *remote_vars;
  753. u64 remote_rp_pa;
  754. u64 remote_vars_pa;
  755. int remote_rp_version;
  756. int reactivate = 0;
  757. unsigned long remote_rp_stamp = 0;
  758. short partid;
  759. struct xpc_partition *part;
  760. struct xpc_partition_sn2 *part_sn2;
  761. enum xp_retval ret;
  762. /* pull over the reserved page structure */
  763. remote_rp = (struct xpc_rsvd_page *)xpc_remote_copy_buffer;
  764. ret = xpc_get_remote_rp(nasid, NULL, remote_rp, &remote_rp_pa);
  765. if (ret != xpSuccess) {
  766. dev_warn(xpc_part, "unable to get reserved page from nasid %d, "
  767. "which sent interrupt, reason=%d\n", nasid, ret);
  768. return;
  769. }
  770. remote_vars_pa = remote_rp->sn.vars_pa;
  771. remote_rp_version = remote_rp->version;
  772. remote_rp_stamp = remote_rp->stamp;
  773. partid = remote_rp->SAL_partid;
  774. part = &xpc_partitions[partid];
  775. part_sn2 = &part->sn.sn2;
  776. /* pull over the cross partition variables */
  777. remote_vars = (struct xpc_vars_sn2 *)xpc_remote_copy_buffer;
  778. ret = xpc_get_remote_vars_sn2(remote_vars_pa, remote_vars);
  779. if (ret != xpSuccess) {
  780. dev_warn(xpc_part, "unable to get XPC variables from nasid %d, "
  781. "which sent interrupt, reason=%d\n", nasid, ret);
  782. XPC_DEACTIVATE_PARTITION(part, ret);
  783. return;
  784. }
  785. part->activate_IRQ_rcvd++;
  786. dev_dbg(xpc_part, "partid for nasid %d is %d; IRQs = %d; HB = "
  787. "%ld:0x%lx\n", (int)nasid, (int)partid, part->activate_IRQ_rcvd,
  788. remote_vars->heartbeat, remote_vars->heartbeating_to_mask[0]);
  789. if (xpc_partition_disengaged(part) &&
  790. part->act_state == XPC_P_INACTIVE) {
  791. xpc_update_partition_info_sn2(part, remote_rp_version,
  792. &remote_rp_stamp, remote_rp_pa,
  793. remote_vars_pa, remote_vars);
  794. if (xpc_partition_deactivation_requested_sn2(partid)) {
  795. /*
  796. * Other side is waiting on us to deactivate even though
  797. * we already have.
  798. */
  799. return;
  800. }
  801. xpc_activate_partition(part);
  802. return;
  803. }
  804. DBUG_ON(part->remote_rp_version == 0);
  805. DBUG_ON(part_sn2->remote_vars_version == 0);
  806. if (remote_rp_stamp != part->remote_rp_stamp) {
  807. /* the other side rebooted */
  808. DBUG_ON(xpc_partition_engaged_sn2(partid));
  809. DBUG_ON(xpc_partition_deactivation_requested_sn2(partid));
  810. xpc_update_partition_info_sn2(part, remote_rp_version,
  811. &remote_rp_stamp, remote_rp_pa,
  812. remote_vars_pa, remote_vars);
  813. reactivate = 1;
  814. }
  815. if (part->disengage_timeout > 0 && !xpc_partition_disengaged(part)) {
  816. /* still waiting on other side to disengage from us */
  817. return;
  818. }
  819. if (reactivate)
  820. XPC_DEACTIVATE_PARTITION(part, xpReactivating);
  821. else if (xpc_partition_deactivation_requested_sn2(partid))
  822. XPC_DEACTIVATE_PARTITION(part, xpOtherGoingDown);
  823. }
  824. /*
  825. * Loop through the activation amo variables and process any bits
  826. * which are set. Each bit indicates a nasid sending a partition
  827. * activation or deactivation request.
  828. *
  829. * Return #of IRQs detected.
  830. */
  831. int
  832. xpc_identify_activate_IRQ_sender_sn2(void)
  833. {
  834. int word, bit;
  835. u64 nasid_mask;
  836. u64 nasid; /* remote nasid */
  837. int n_IRQs_detected = 0;
  838. struct amo *act_amos;
  839. act_amos = xpc_vars->amos_page + XPC_ACTIVATE_IRQ_AMOS;
  840. /* scan through act amo variable looking for non-zero entries */
  841. for (word = 0; word < xp_nasid_mask_words; word++) {
  842. if (xpc_exiting)
  843. break;
  844. nasid_mask = xpc_receive_IRQ_amo_sn2(&act_amos[word]);
  845. if (nasid_mask == 0) {
  846. /* no IRQs from nasids in this variable */
  847. continue;
  848. }
  849. dev_dbg(xpc_part, "amo[%d] gave back 0x%lx\n", word,
  850. nasid_mask);
  851. /*
  852. * If this nasid has been added to the machine since
  853. * our partition was reset, this will retain the
  854. * remote nasid in our reserved pages machine mask.
  855. * This is used in the event of module reload.
  856. */
  857. xpc_mach_nasids[word] |= nasid_mask;
  858. /* locate the nasid(s) which sent interrupts */
  859. for (bit = 0; bit < (8 * sizeof(u64)); bit++) {
  860. if (nasid_mask & (1UL << bit)) {
  861. n_IRQs_detected++;
  862. nasid = XPC_NASID_FROM_W_B(word, bit);
  863. dev_dbg(xpc_part, "interrupt from nasid %ld\n",
  864. nasid);
  865. xpc_identify_activate_IRQ_req_sn2(nasid);
  866. }
  867. }
  868. }
  869. return n_IRQs_detected;
  870. }
  871. static void
  872. xpc_process_activate_IRQ_rcvd_sn2(int n_IRQs_expected)
  873. {
  874. int n_IRQs_detected;
  875. n_IRQs_detected = xpc_identify_activate_IRQ_sender_sn2();
  876. if (n_IRQs_detected < n_IRQs_expected) {
  877. /* retry once to help avoid missing amo */
  878. (void)xpc_identify_activate_IRQ_sender_sn2();
  879. }
  880. }
  881. /*
  882. * Setup the infrastructure necessary to support XPartition Communication
  883. * between the specified remote partition and the local one.
  884. */
  885. static enum xp_retval
  886. xpc_setup_infrastructure_sn2(struct xpc_partition *part)
  887. {
  888. struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
  889. enum xp_retval retval;
  890. int ret;
  891. int cpuid;
  892. int ch_number;
  893. struct xpc_channel *ch;
  894. struct timer_list *timer;
  895. short partid = XPC_PARTID(part);
  896. /*
  897. * Allocate all of the channel structures as a contiguous chunk of
  898. * memory.
  899. */
  900. DBUG_ON(part->channels != NULL);
  901. part->channels = kzalloc(sizeof(struct xpc_channel) * XPC_MAX_NCHANNELS,
  902. GFP_KERNEL);
  903. if (part->channels == NULL) {
  904. dev_err(xpc_chan, "can't get memory for channels\n");
  905. return xpNoMemory;
  906. }
  907. /* allocate all the required GET/PUT values */
  908. part_sn2->local_GPs = xpc_kzalloc_cacheline_aligned(XPC_GP_SIZE,
  909. GFP_KERNEL,
  910. &part_sn2->
  911. local_GPs_base);
  912. if (part_sn2->local_GPs == NULL) {
  913. dev_err(xpc_chan, "can't get memory for local get/put "
  914. "values\n");
  915. retval = xpNoMemory;
  916. goto out_1;
  917. }
  918. part_sn2->remote_GPs = xpc_kzalloc_cacheline_aligned(XPC_GP_SIZE,
  919. GFP_KERNEL,
  920. &part_sn2->
  921. remote_GPs_base);
  922. if (part_sn2->remote_GPs == NULL) {
  923. dev_err(xpc_chan, "can't get memory for remote get/put "
  924. "values\n");
  925. retval = xpNoMemory;
  926. goto out_2;
  927. }
  928. part_sn2->remote_GPs_pa = 0;
  929. /* allocate all the required open and close args */
  930. part->local_openclose_args =
  931. xpc_kzalloc_cacheline_aligned(XPC_OPENCLOSE_ARGS_SIZE, GFP_KERNEL,
  932. &part->local_openclose_args_base);
  933. if (part->local_openclose_args == NULL) {
  934. dev_err(xpc_chan, "can't get memory for local connect args\n");
  935. retval = xpNoMemory;
  936. goto out_3;
  937. }
  938. part->remote_openclose_args =
  939. xpc_kzalloc_cacheline_aligned(XPC_OPENCLOSE_ARGS_SIZE, GFP_KERNEL,
  940. &part->remote_openclose_args_base);
  941. if (part->remote_openclose_args == NULL) {
  942. dev_err(xpc_chan, "can't get memory for remote connect args\n");
  943. retval = xpNoMemory;
  944. goto out_4;
  945. }
  946. part_sn2->remote_openclose_args_pa = 0;
  947. part_sn2->local_chctl_amo_va = xpc_init_IRQ_amo_sn2(partid);
  948. part->chctl.all_flags = 0;
  949. spin_lock_init(&part->chctl_lock);
  950. part_sn2->notify_IRQ_nasid = 0;
  951. part_sn2->notify_IRQ_phys_cpuid = 0;
  952. part_sn2->remote_chctl_amo_va = NULL;
  953. atomic_set(&part->channel_mgr_requests, 1);
  954. init_waitqueue_head(&part->channel_mgr_wq);
  955. sprintf(part_sn2->notify_IRQ_owner, "xpc%02d", partid);
  956. ret = request_irq(SGI_XPC_NOTIFY, xpc_handle_notify_IRQ_sn2,
  957. IRQF_SHARED, part_sn2->notify_IRQ_owner,
  958. (void *)(u64)partid);
  959. if (ret != 0) {
  960. dev_err(xpc_chan, "can't register NOTIFY IRQ handler, "
  961. "errno=%d\n", -ret);
  962. retval = xpLackOfResources;
  963. goto out_5;
  964. }
  965. /* Setup a timer to check for dropped notify IRQs */
  966. timer = &part_sn2->dropped_notify_IRQ_timer;
  967. init_timer(timer);
  968. timer->function =
  969. (void (*)(unsigned long))xpc_check_for_dropped_notify_IRQ_sn2;
  970. timer->data = (unsigned long)part;
  971. timer->expires = jiffies + XPC_DROPPED_NOTIFY_IRQ_WAIT_INTERVAL;
  972. add_timer(timer);
  973. part->nchannels = XPC_MAX_NCHANNELS;
  974. atomic_set(&part->nchannels_active, 0);
  975. atomic_set(&part->nchannels_engaged, 0);
  976. for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
  977. ch = &part->channels[ch_number];
  978. ch->partid = partid;
  979. ch->number = ch_number;
  980. ch->flags = XPC_C_DISCONNECTED;
  981. ch->sn.sn2.local_GP = &part_sn2->local_GPs[ch_number];
  982. ch->local_openclose_args =
  983. &part->local_openclose_args[ch_number];
  984. atomic_set(&ch->kthreads_assigned, 0);
  985. atomic_set(&ch->kthreads_idle, 0);
  986. atomic_set(&ch->kthreads_active, 0);
  987. atomic_set(&ch->references, 0);
  988. atomic_set(&ch->n_to_notify, 0);
  989. spin_lock_init(&ch->lock);
  990. mutex_init(&ch->sn.sn2.msg_to_pull_mutex);
  991. init_completion(&ch->wdisconnect_wait);
  992. atomic_set(&ch->n_on_msg_allocate_wq, 0);
  993. init_waitqueue_head(&ch->msg_allocate_wq);
  994. init_waitqueue_head(&ch->idle_wq);
  995. }
  996. /*
  997. * With the setting of the partition setup_state to XPC_P_SETUP, we're
  998. * declaring that this partition is ready to go.
  999. */
  1000. part->setup_state = XPC_P_SETUP;
  1001. /*
  1002. * Setup the per partition specific variables required by the
  1003. * remote partition to establish channel connections with us.
  1004. *
  1005. * The setting of the magic # indicates that these per partition
  1006. * specific variables are ready to be used.
  1007. */
  1008. xpc_vars_part[partid].GPs_pa = __pa(part_sn2->local_GPs);
  1009. xpc_vars_part[partid].openclose_args_pa =
  1010. __pa(part->local_openclose_args);
  1011. xpc_vars_part[partid].chctl_amo_pa = __pa(part_sn2->local_chctl_amo_va);
  1012. cpuid = raw_smp_processor_id(); /* any CPU in this partition will do */
  1013. xpc_vars_part[partid].notify_IRQ_nasid = cpuid_to_nasid(cpuid);
  1014. xpc_vars_part[partid].notify_IRQ_phys_cpuid = cpu_physical_id(cpuid);
  1015. xpc_vars_part[partid].nchannels = part->nchannels;
  1016. xpc_vars_part[partid].magic = XPC_VP_MAGIC1;
  1017. return xpSuccess;
  1018. /* setup of infrastructure failed */
  1019. out_5:
  1020. kfree(part->remote_openclose_args_base);
  1021. part->remote_openclose_args = NULL;
  1022. out_4:
  1023. kfree(part->local_openclose_args_base);
  1024. part->local_openclose_args = NULL;
  1025. out_3:
  1026. kfree(part_sn2->remote_GPs_base);
  1027. part_sn2->remote_GPs = NULL;
  1028. out_2:
  1029. kfree(part_sn2->local_GPs_base);
  1030. part_sn2->local_GPs = NULL;
  1031. out_1:
  1032. kfree(part->channels);
  1033. part->channels = NULL;
  1034. return retval;
  1035. }
  1036. /*
  1037. * Teardown the infrastructure necessary to support XPartition Communication
  1038. * between the specified remote partition and the local one.
  1039. */
  1040. static void
  1041. xpc_teardown_infrastructure_sn2(struct xpc_partition *part)
  1042. {
  1043. struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
  1044. short partid = XPC_PARTID(part);
  1045. /*
  1046. * We start off by making this partition inaccessible to local
  1047. * processes by marking it as no longer setup. Then we make it
  1048. * inaccessible to remote processes by clearing the XPC per partition
  1049. * specific variable's magic # (which indicates that these variables
  1050. * are no longer valid) and by ignoring all XPC notify IRQs sent to
  1051. * this partition.
  1052. */
  1053. DBUG_ON(atomic_read(&part->nchannels_engaged) != 0);
  1054. DBUG_ON(atomic_read(&part->nchannels_active) != 0);
  1055. DBUG_ON(part->setup_state != XPC_P_SETUP);
  1056. part->setup_state = XPC_P_WTEARDOWN;
  1057. xpc_vars_part[partid].magic = 0;
  1058. free_irq(SGI_XPC_NOTIFY, (void *)(u64)partid);
  1059. /*
  1060. * Before proceeding with the teardown we have to wait until all
  1061. * existing references cease.
  1062. */
  1063. wait_event(part->teardown_wq, (atomic_read(&part->references) == 0));
  1064. /* now we can begin tearing down the infrastructure */
  1065. part->setup_state = XPC_P_TORNDOWN;
  1066. /* in case we've still got outstanding timers registered... */
  1067. del_timer_sync(&part_sn2->dropped_notify_IRQ_timer);
  1068. kfree(part->remote_openclose_args_base);
  1069. part->remote_openclose_args = NULL;
  1070. kfree(part->local_openclose_args_base);
  1071. part->local_openclose_args = NULL;
  1072. kfree(part_sn2->remote_GPs_base);
  1073. part_sn2->remote_GPs = NULL;
  1074. kfree(part_sn2->local_GPs_base);
  1075. part_sn2->local_GPs = NULL;
  1076. kfree(part->channels);
  1077. part->channels = NULL;
  1078. part_sn2->local_chctl_amo_va = NULL;
  1079. }
  1080. /*
  1081. * Create a wrapper that hides the underlying mechanism for pulling a cacheline
  1082. * (or multiple cachelines) from a remote partition.
  1083. *
  1084. * src must be a cacheline aligned physical address on the remote partition.
  1085. * dst must be a cacheline aligned virtual address on this partition.
  1086. * cnt must be cacheline sized
  1087. */
  1088. /* >>> Replace this function by call to xp_remote_memcpy() or bte_copy()? */
  1089. static enum xp_retval
  1090. xpc_pull_remote_cachelines_sn2(struct xpc_partition *part, void *dst,
  1091. const void *src, size_t cnt)
  1092. {
  1093. enum xp_retval ret;
  1094. DBUG_ON((u64)src != L1_CACHE_ALIGN((u64)src));
  1095. DBUG_ON((u64)dst != L1_CACHE_ALIGN((u64)dst));
  1096. DBUG_ON(cnt != L1_CACHE_ALIGN(cnt));
  1097. if (part->act_state == XPC_P_DEACTIVATING)
  1098. return part->reason;
  1099. ret = xp_remote_memcpy(dst, src, cnt);
  1100. if (ret != xpSuccess) {
  1101. dev_dbg(xpc_chan, "xp_remote_memcpy() from partition %d failed,"
  1102. " ret=%d\n", XPC_PARTID(part), ret);
  1103. }
  1104. return ret;
  1105. }
  1106. /*
  1107. * Pull the remote per partition specific variables from the specified
  1108. * partition.
  1109. */
  1110. static enum xp_retval
  1111. xpc_pull_remote_vars_part_sn2(struct xpc_partition *part)
  1112. {
  1113. struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
  1114. u8 buffer[L1_CACHE_BYTES * 2];
  1115. struct xpc_vars_part_sn2 *pulled_entry_cacheline =
  1116. (struct xpc_vars_part_sn2 *)L1_CACHE_ALIGN((u64)buffer);
  1117. struct xpc_vars_part_sn2 *pulled_entry;
  1118. u64 remote_entry_cacheline_pa, remote_entry_pa;
  1119. short partid = XPC_PARTID(part);
  1120. enum xp_retval ret;
  1121. /* pull the cacheline that contains the variables we're interested in */
  1122. DBUG_ON(part_sn2->remote_vars_part_pa !=
  1123. L1_CACHE_ALIGN(part_sn2->remote_vars_part_pa));
  1124. DBUG_ON(sizeof(struct xpc_vars_part_sn2) != L1_CACHE_BYTES / 2);
  1125. remote_entry_pa = part_sn2->remote_vars_part_pa +
  1126. sn_partition_id * sizeof(struct xpc_vars_part_sn2);
  1127. remote_entry_cacheline_pa = (remote_entry_pa & ~(L1_CACHE_BYTES - 1));
  1128. pulled_entry = (struct xpc_vars_part_sn2 *)((u64)pulled_entry_cacheline
  1129. + (remote_entry_pa &
  1130. (L1_CACHE_BYTES - 1)));
  1131. ret = xpc_pull_remote_cachelines_sn2(part, pulled_entry_cacheline,
  1132. (void *)remote_entry_cacheline_pa,
  1133. L1_CACHE_BYTES);
  1134. if (ret != xpSuccess) {
  1135. dev_dbg(xpc_chan, "failed to pull XPC vars_part from "
  1136. "partition %d, ret=%d\n", partid, ret);
  1137. return ret;
  1138. }
  1139. /* see if they've been set up yet */
  1140. if (pulled_entry->magic != XPC_VP_MAGIC1 &&
  1141. pulled_entry->magic != XPC_VP_MAGIC2) {
  1142. if (pulled_entry->magic != 0) {
  1143. dev_dbg(xpc_chan, "partition %d's XPC vars_part for "
  1144. "partition %d has bad magic value (=0x%lx)\n",
  1145. partid, sn_partition_id, pulled_entry->magic);
  1146. return xpBadMagic;
  1147. }
  1148. /* they've not been initialized yet */
  1149. return xpRetry;
  1150. }
  1151. if (xpc_vars_part[partid].magic == XPC_VP_MAGIC1) {
  1152. /* validate the variables */
  1153. if (pulled_entry->GPs_pa == 0 ||
  1154. pulled_entry->openclose_args_pa == 0 ||
  1155. pulled_entry->chctl_amo_pa == 0) {
  1156. dev_err(xpc_chan, "partition %d's XPC vars_part for "
  1157. "partition %d are not valid\n", partid,
  1158. sn_partition_id);
  1159. return xpInvalidAddress;
  1160. }
  1161. /* the variables we imported look to be valid */
  1162. part_sn2->remote_GPs_pa = pulled_entry->GPs_pa;
  1163. part_sn2->remote_openclose_args_pa =
  1164. pulled_entry->openclose_args_pa;
  1165. part_sn2->remote_chctl_amo_va =
  1166. (struct amo *)__va(pulled_entry->chctl_amo_pa);
  1167. part_sn2->notify_IRQ_nasid = pulled_entry->notify_IRQ_nasid;
  1168. part_sn2->notify_IRQ_phys_cpuid =
  1169. pulled_entry->notify_IRQ_phys_cpuid;
  1170. if (part->nchannels > pulled_entry->nchannels)
  1171. part->nchannels = pulled_entry->nchannels;
  1172. /* let the other side know that we've pulled their variables */
  1173. xpc_vars_part[partid].magic = XPC_VP_MAGIC2;
  1174. }
  1175. if (pulled_entry->magic == XPC_VP_MAGIC1)
  1176. return xpRetry;
  1177. return xpSuccess;
  1178. }
  1179. /*
  1180. * Establish first contact with the remote partititon. This involves pulling
  1181. * the XPC per partition variables from the remote partition and waiting for
  1182. * the remote partition to pull ours.
  1183. */
  1184. static enum xp_retval
  1185. xpc_make_first_contact_sn2(struct xpc_partition *part)
  1186. {
  1187. struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
  1188. enum xp_retval ret;
  1189. /*
  1190. * Register the remote partition's amos with SAL so it can handle
  1191. * and cleanup errors within that address range should the remote
  1192. * partition go down. We don't unregister this range because it is
  1193. * difficult to tell when outstanding writes to the remote partition
  1194. * are finished and thus when it is safe to unregister. This should
  1195. * not result in wasted space in the SAL xp_addr_region table because
  1196. * we should get the same page for remote_amos_page_pa after module
  1197. * reloads and system reboots.
  1198. */
  1199. if (sn_register_xp_addr_region(part_sn2->remote_amos_page_pa,
  1200. PAGE_SIZE, 1) < 0) {
  1201. dev_warn(xpc_part, "xpc_activating(%d) failed to register "
  1202. "xp_addr region\n", XPC_PARTID(part));
  1203. ret = xpPhysAddrRegFailed;
  1204. XPC_DEACTIVATE_PARTITION(part, ret);
  1205. return ret;
  1206. }
  1207. /*
  1208. * Send activate IRQ to get other side to activate if they've not
  1209. * already begun to do so.
  1210. */
  1211. xpc_send_activate_IRQ_sn2(part_sn2->remote_amos_page_pa,
  1212. cnodeid_to_nasid(0),
  1213. part_sn2->activate_IRQ_nasid,
  1214. part_sn2->activate_IRQ_phys_cpuid);
  1215. while ((ret = xpc_pull_remote_vars_part_sn2(part)) != xpSuccess) {
  1216. if (ret != xpRetry) {
  1217. XPC_DEACTIVATE_PARTITION(part, ret);
  1218. return ret;
  1219. }
  1220. dev_dbg(xpc_part, "waiting to make first contact with "
  1221. "partition %d\n", XPC_PARTID(part));
  1222. /* wait a 1/4 of a second or so */
  1223. (void)msleep_interruptible(250);
  1224. if (part->act_state == XPC_P_DEACTIVATING)
  1225. return part->reason;
  1226. }
  1227. return xpSuccess;
  1228. }
  1229. /*
  1230. * Get the chctl flags and pull the openclose args and/or remote GPs as needed.
  1231. */
  1232. static u64
  1233. xpc_get_chctl_all_flags_sn2(struct xpc_partition *part)
  1234. {
  1235. struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
  1236. unsigned long irq_flags;
  1237. union xpc_channel_ctl_flags chctl;
  1238. enum xp_retval ret;
  1239. /*
  1240. * See if there are any chctl flags to be handled.
  1241. */
  1242. spin_lock_irqsave(&part->chctl_lock, irq_flags);
  1243. chctl = part->chctl;
  1244. if (chctl.all_flags != 0)
  1245. part->chctl.all_flags = 0;
  1246. spin_unlock_irqrestore(&part->chctl_lock, irq_flags);
  1247. if (xpc_any_openclose_chctl_flags_set(&chctl)) {
  1248. ret = xpc_pull_remote_cachelines_sn2(part, part->
  1249. remote_openclose_args,
  1250. (void *)part_sn2->
  1251. remote_openclose_args_pa,
  1252. XPC_OPENCLOSE_ARGS_SIZE);
  1253. if (ret != xpSuccess) {
  1254. XPC_DEACTIVATE_PARTITION(part, ret);
  1255. dev_dbg(xpc_chan, "failed to pull openclose args from "
  1256. "partition %d, ret=%d\n", XPC_PARTID(part),
  1257. ret);
  1258. /* don't bother processing chctl flags anymore */
  1259. chctl.all_flags = 0;
  1260. }
  1261. }
  1262. if (xpc_any_msg_chctl_flags_set(&chctl)) {
  1263. ret = xpc_pull_remote_cachelines_sn2(part, part_sn2->remote_GPs,
  1264. (void *)part_sn2->remote_GPs_pa,
  1265. XPC_GP_SIZE);
  1266. if (ret != xpSuccess) {
  1267. XPC_DEACTIVATE_PARTITION(part, ret);
  1268. dev_dbg(xpc_chan, "failed to pull GPs from partition "
  1269. "%d, ret=%d\n", XPC_PARTID(part), ret);
  1270. /* don't bother processing chctl flags anymore */
  1271. chctl.all_flags = 0;
  1272. }
  1273. }
  1274. return chctl.all_flags;
  1275. }
  1276. /*
  1277. * Notify those who wanted to be notified upon delivery of their message.
  1278. */
  1279. static void
  1280. xpc_notify_senders_sn2(struct xpc_channel *ch, enum xp_retval reason, s64 put)
  1281. {
  1282. struct xpc_notify *notify;
  1283. u8 notify_type;
  1284. s64 get = ch->sn.sn2.w_remote_GP.get - 1;
  1285. while (++get < put && atomic_read(&ch->n_to_notify) > 0) {
  1286. notify = &ch->notify_queue[get % ch->local_nentries];
  1287. /*
  1288. * See if the notify entry indicates it was associated with
  1289. * a message who's sender wants to be notified. It is possible
  1290. * that it is, but someone else is doing or has done the
  1291. * notification.
  1292. */
  1293. notify_type = notify->type;
  1294. if (notify_type == 0 ||
  1295. cmpxchg(&notify->type, notify_type, 0) != notify_type) {
  1296. continue;
  1297. }
  1298. DBUG_ON(notify_type != XPC_N_CALL);
  1299. atomic_dec(&ch->n_to_notify);
  1300. if (notify->func != NULL) {
  1301. dev_dbg(xpc_chan, "notify->func() called, notify=0x%p, "
  1302. "msg_number=%ld, partid=%d, channel=%d\n",
  1303. (void *)notify, get, ch->partid, ch->number);
  1304. notify->func(reason, ch->partid, ch->number,
  1305. notify->key);
  1306. dev_dbg(xpc_chan, "notify->func() returned, "
  1307. "notify=0x%p, msg_number=%ld, partid=%d, "
  1308. "channel=%d\n", (void *)notify, get,
  1309. ch->partid, ch->number);
  1310. }
  1311. }
  1312. }
  1313. static void
  1314. xpc_notify_senders_of_disconnect_sn2(struct xpc_channel *ch)
  1315. {
  1316. xpc_notify_senders_sn2(ch, ch->reason, ch->sn.sn2.w_local_GP.put);
  1317. }
  1318. /*
  1319. * Clear some of the msg flags in the local message queue.
  1320. */
  1321. static inline void
  1322. xpc_clear_local_msgqueue_flags_sn2(struct xpc_channel *ch)
  1323. {
  1324. struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
  1325. struct xpc_msg *msg;
  1326. s64 get;
  1327. get = ch_sn2->w_remote_GP.get;
  1328. do {
  1329. msg = (struct xpc_msg *)((u64)ch->local_msgqueue +
  1330. (get % ch->local_nentries) *
  1331. ch->msg_size);
  1332. msg->flags = 0;
  1333. } while (++get < ch_sn2->remote_GP.get);
  1334. }
  1335. /*
  1336. * Clear some of the msg flags in the remote message queue.
  1337. */
  1338. static inline void
  1339. xpc_clear_remote_msgqueue_flags_sn2(struct xpc_channel *ch)
  1340. {
  1341. struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
  1342. struct xpc_msg *msg;
  1343. s64 put;
  1344. put = ch_sn2->w_remote_GP.put;
  1345. do {
  1346. msg = (struct xpc_msg *)((u64)ch->remote_msgqueue +
  1347. (put % ch->remote_nentries) *
  1348. ch->msg_size);
  1349. msg->flags = 0;
  1350. } while (++put < ch_sn2->remote_GP.put);
  1351. }
  1352. static void
  1353. xpc_process_msg_chctl_flags_sn2(struct xpc_partition *part, int ch_number)
  1354. {
  1355. struct xpc_channel *ch = &part->channels[ch_number];
  1356. struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
  1357. int nmsgs_sent;
  1358. ch_sn2->remote_GP = part->sn.sn2.remote_GPs[ch_number];
  1359. /* See what, if anything, has changed for each connected channel */
  1360. xpc_msgqueue_ref(ch);
  1361. if (ch_sn2->w_remote_GP.get == ch_sn2->remote_GP.get &&
  1362. ch_sn2->w_remote_GP.put == ch_sn2->remote_GP.put) {
  1363. /* nothing changed since GPs were last pulled */
  1364. xpc_msgqueue_deref(ch);
  1365. return;
  1366. }
  1367. if (!(ch->flags & XPC_C_CONNECTED)) {
  1368. xpc_msgqueue_deref(ch);
  1369. return;
  1370. }
  1371. /*
  1372. * First check to see if messages recently sent by us have been
  1373. * received by the other side. (The remote GET value will have
  1374. * changed since we last looked at it.)
  1375. */
  1376. if (ch_sn2->w_remote_GP.get != ch_sn2->remote_GP.get) {
  1377. /*
  1378. * We need to notify any senders that want to be notified
  1379. * that their sent messages have been received by their
  1380. * intended recipients. We need to do this before updating
  1381. * w_remote_GP.get so that we don't allocate the same message
  1382. * queue entries prematurely (see xpc_allocate_msg()).
  1383. */
  1384. if (atomic_read(&ch->n_to_notify) > 0) {
  1385. /*
  1386. * Notify senders that messages sent have been
  1387. * received and delivered by the other side.
  1388. */
  1389. xpc_notify_senders_sn2(ch, xpMsgDelivered,
  1390. ch_sn2->remote_GP.get);
  1391. }
  1392. /*
  1393. * Clear msg->flags in previously sent messages, so that
  1394. * they're ready for xpc_allocate_msg().
  1395. */
  1396. xpc_clear_local_msgqueue_flags_sn2(ch);
  1397. ch_sn2->w_remote_GP.get = ch_sn2->remote_GP.get;
  1398. dev_dbg(xpc_chan, "w_remote_GP.get changed to %ld, partid=%d, "
  1399. "channel=%d\n", ch_sn2->w_remote_GP.get, ch->partid,
  1400. ch->number);
  1401. /*
  1402. * If anyone was waiting for message queue entries to become
  1403. * available, wake them up.
  1404. */
  1405. if (atomic_read(&ch->n_on_msg_allocate_wq) > 0)
  1406. wake_up(&ch->msg_allocate_wq);
  1407. }
  1408. /*
  1409. * Now check for newly sent messages by the other side. (The remote
  1410. * PUT value will have changed since we last looked at it.)
  1411. */
  1412. if (ch_sn2->w_remote_GP.put != ch_sn2->remote_GP.put) {
  1413. /*
  1414. * Clear msg->flags in previously received messages, so that
  1415. * they're ready for xpc_get_deliverable_msg().
  1416. */
  1417. xpc_clear_remote_msgqueue_flags_sn2(ch);
  1418. ch_sn2->w_remote_GP.put = ch_sn2->remote_GP.put;
  1419. dev_dbg(xpc_chan, "w_remote_GP.put changed to %ld, partid=%d, "
  1420. "channel=%d\n", ch_sn2->w_remote_GP.put, ch->partid,
  1421. ch->number);
  1422. nmsgs_sent = ch_sn2->w_remote_GP.put - ch_sn2->w_local_GP.get;
  1423. if (nmsgs_sent > 0) {
  1424. dev_dbg(xpc_chan, "msgs waiting to be copied and "
  1425. "delivered=%d, partid=%d, channel=%d\n",
  1426. nmsgs_sent, ch->partid, ch->number);
  1427. if (ch->flags & XPC_C_CONNECTEDCALLOUT_MADE)
  1428. xpc_activate_kthreads(ch, nmsgs_sent);
  1429. }
  1430. }
  1431. xpc_msgqueue_deref(ch);
  1432. }
  1433. static struct xpc_msg *
  1434. xpc_pull_remote_msg_sn2(struct xpc_channel *ch, s64 get)
  1435. {
  1436. struct xpc_partition *part = &xpc_partitions[ch->partid];
  1437. struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
  1438. struct xpc_msg *remote_msg, *msg;
  1439. u32 msg_index, nmsgs;
  1440. u64 msg_offset;
  1441. enum xp_retval ret;
  1442. if (mutex_lock_interruptible(&ch_sn2->msg_to_pull_mutex) != 0) {
  1443. /* we were interrupted by a signal */
  1444. return NULL;
  1445. }
  1446. while (get >= ch_sn2->next_msg_to_pull) {
  1447. /* pull as many messages as are ready and able to be pulled */
  1448. msg_index = ch_sn2->next_msg_to_pull % ch->remote_nentries;
  1449. DBUG_ON(ch_sn2->next_msg_to_pull >= ch_sn2->w_remote_GP.put);
  1450. nmsgs = ch_sn2->w_remote_GP.put - ch_sn2->next_msg_to_pull;
  1451. if (msg_index + nmsgs > ch->remote_nentries) {
  1452. /* ignore the ones that wrap the msg queue for now */
  1453. nmsgs = ch->remote_nentries - msg_index;
  1454. }
  1455. msg_offset = msg_index * ch->msg_size;
  1456. msg = (struct xpc_msg *)((u64)ch->remote_msgqueue + msg_offset);
  1457. remote_msg = (struct xpc_msg *)(ch->remote_msgqueue_pa +
  1458. msg_offset);
  1459. ret = xpc_pull_remote_cachelines_sn2(part, msg, remote_msg,
  1460. nmsgs * ch->msg_size);
  1461. if (ret != xpSuccess) {
  1462. dev_dbg(xpc_chan, "failed to pull %d msgs starting with"
  1463. " msg %ld from partition %d, channel=%d, "
  1464. "ret=%d\n", nmsgs, ch_sn2->next_msg_to_pull,
  1465. ch->partid, ch->number, ret);
  1466. XPC_DEACTIVATE_PARTITION(part, ret);
  1467. mutex_unlock(&ch_sn2->msg_to_pull_mutex);
  1468. return NULL;
  1469. }
  1470. ch_sn2->next_msg_to_pull += nmsgs;
  1471. }
  1472. mutex_unlock(&ch_sn2->msg_to_pull_mutex);
  1473. /* return the message we were looking for */
  1474. msg_offset = (get % ch->remote_nentries) * ch->msg_size;
  1475. msg = (struct xpc_msg *)((u64)ch->remote_msgqueue + msg_offset);
  1476. return msg;
  1477. }
  1478. static int
  1479. xpc_n_of_deliverable_msgs_sn2(struct xpc_channel *ch)
  1480. {
  1481. return ch->sn.sn2.w_remote_GP.put - ch->sn.sn2.w_local_GP.get;
  1482. }
  1483. /*
  1484. * Get a message to be delivered.
  1485. */
  1486. static struct xpc_msg *
  1487. xpc_get_deliverable_msg_sn2(struct xpc_channel *ch)
  1488. {
  1489. struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
  1490. struct xpc_msg *msg = NULL;
  1491. s64 get;
  1492. do {
  1493. if (ch->flags & XPC_C_DISCONNECTING)
  1494. break;
  1495. get = ch_sn2->w_local_GP.get;
  1496. rmb(); /* guarantee that .get loads before .put */
  1497. if (get == ch_sn2->w_remote_GP.put)
  1498. break;
  1499. /* There are messages waiting to be pulled and delivered.
  1500. * We need to try to secure one for ourselves. We'll do this
  1501. * by trying to increment w_local_GP.get and hope that no one
  1502. * else beats us to it. If they do, we'll we'll simply have
  1503. * to try again for the next one.
  1504. */
  1505. if (cmpxchg(&ch_sn2->w_local_GP.get, get, get + 1) == get) {
  1506. /* we got the entry referenced by get */
  1507. dev_dbg(xpc_chan, "w_local_GP.get changed to %ld, "
  1508. "partid=%d, channel=%d\n", get + 1,
  1509. ch->partid, ch->number);
  1510. /* pull the message from the remote partition */
  1511. msg = xpc_pull_remote_msg_sn2(ch, get);
  1512. DBUG_ON(msg != NULL && msg->number != get);
  1513. DBUG_ON(msg != NULL && (msg->flags & XPC_M_DONE));
  1514. DBUG_ON(msg != NULL && !(msg->flags & XPC_M_READY));
  1515. break;
  1516. }
  1517. } while (1);
  1518. return msg;
  1519. }
  1520. /*
  1521. * Now we actually send the messages that are ready to be sent by advancing
  1522. * the local message queue's Put value and then send a chctl msgrequest to the
  1523. * recipient partition.
  1524. */
  1525. static void
  1526. xpc_send_msgs_sn2(struct xpc_channel *ch, s64 initial_put)
  1527. {
  1528. struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
  1529. struct xpc_msg *msg;
  1530. s64 put = initial_put + 1;
  1531. int send_msgrequest = 0;
  1532. while (1) {
  1533. while (1) {
  1534. if (put == ch_sn2->w_local_GP.put)
  1535. break;
  1536. msg = (struct xpc_msg *)((u64)ch->local_msgqueue +
  1537. (put % ch->local_nentries) *
  1538. ch->msg_size);
  1539. if (!(msg->flags & XPC_M_READY))
  1540. break;
  1541. put++;
  1542. }
  1543. if (put == initial_put) {
  1544. /* nothing's changed */
  1545. break;
  1546. }
  1547. if (cmpxchg_rel(&ch_sn2->local_GP->put, initial_put, put) !=
  1548. initial_put) {
  1549. /* someone else beat us to it */
  1550. DBUG_ON(ch_sn2->local_GP->put < initial_put);
  1551. break;
  1552. }
  1553. /* we just set the new value of local_GP->put */
  1554. dev_dbg(xpc_chan, "local_GP->put changed to %ld, partid=%d, "
  1555. "channel=%d\n", put, ch->partid, ch->number);
  1556. send_msgrequest = 1;
  1557. /*
  1558. * We need to ensure that the message referenced by
  1559. * local_GP->put is not XPC_M_READY or that local_GP->put
  1560. * equals w_local_GP.put, so we'll go have a look.
  1561. */
  1562. initial_put = put;
  1563. }
  1564. if (send_msgrequest)
  1565. xpc_send_chctl_msgrequest_sn2(ch);
  1566. }
  1567. /*
  1568. * Allocate an entry for a message from the message queue associated with the
  1569. * specified channel.
  1570. */
  1571. static enum xp_retval
  1572. xpc_allocate_msg_sn2(struct xpc_channel *ch, u32 flags,
  1573. struct xpc_msg **address_of_msg)
  1574. {
  1575. struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
  1576. struct xpc_msg *msg;
  1577. enum xp_retval ret;
  1578. s64 put;
  1579. /*
  1580. * Get the next available message entry from the local message queue.
  1581. * If none are available, we'll make sure that we grab the latest
  1582. * GP values.
  1583. */
  1584. ret = xpTimeout;
  1585. while (1) {
  1586. put = ch_sn2->w_local_GP.put;
  1587. rmb(); /* guarantee that .put loads before .get */
  1588. if (put - ch_sn2->w_remote_GP.get < ch->local_nentries) {
  1589. /* There are available message entries. We need to try
  1590. * to secure one for ourselves. We'll do this by trying
  1591. * to increment w_local_GP.put as long as someone else
  1592. * doesn't beat us to it. If they do, we'll have to
  1593. * try again.
  1594. */
  1595. if (cmpxchg(&ch_sn2->w_local_GP.put, put, put + 1) ==
  1596. put) {
  1597. /* we got the entry referenced by put */
  1598. break;
  1599. }
  1600. continue; /* try again */
  1601. }
  1602. /*
  1603. * There aren't any available msg entries at this time.
  1604. *
  1605. * In waiting for a message entry to become available,
  1606. * we set a timeout in case the other side is not sending
  1607. * completion interrupts. This lets us fake a notify IRQ
  1608. * that will cause the notify IRQ handler to fetch the latest
  1609. * GP values as if an interrupt was sent by the other side.
  1610. */
  1611. if (ret == xpTimeout)
  1612. xpc_send_chctl_local_msgrequest_sn2(ch);
  1613. if (flags & XPC_NOWAIT)
  1614. return xpNoWait;
  1615. ret = xpc_allocate_msg_wait(ch);
  1616. if (ret != xpInterrupted && ret != xpTimeout)
  1617. return ret;
  1618. }
  1619. /* get the message's address and initialize it */
  1620. msg = (struct xpc_msg *)((u64)ch->local_msgqueue +
  1621. (put % ch->local_nentries) * ch->msg_size);
  1622. DBUG_ON(msg->flags != 0);
  1623. msg->number = put;
  1624. dev_dbg(xpc_chan, "w_local_GP.put changed to %ld; msg=0x%p, "
  1625. "msg_number=%ld, partid=%d, channel=%d\n", put + 1,
  1626. (void *)msg, msg->number, ch->partid, ch->number);
  1627. *address_of_msg = msg;
  1628. return xpSuccess;
  1629. }
  1630. /*
  1631. * Common code that does the actual sending of the message by advancing the
  1632. * local message queue's Put value and sends a chctl msgrequest to the
  1633. * partition the message is being sent to.
  1634. */
  1635. static enum xp_retval
  1636. xpc_send_msg_sn2(struct xpc_channel *ch, u32 flags, void *payload,
  1637. u16 payload_size, u8 notify_type, xpc_notify_func func,
  1638. void *key)
  1639. {
  1640. enum xp_retval ret = xpSuccess;
  1641. struct xpc_msg *msg = msg;
  1642. struct xpc_notify *notify = notify;
  1643. s64 msg_number;
  1644. s64 put;
  1645. DBUG_ON(notify_type == XPC_N_CALL && func == NULL);
  1646. if (XPC_MSG_SIZE(payload_size) > ch->msg_size)
  1647. return xpPayloadTooBig;
  1648. xpc_msgqueue_ref(ch);
  1649. if (ch->flags & XPC_C_DISCONNECTING) {
  1650. ret = ch->reason;
  1651. goto out_1;
  1652. }
  1653. if (!(ch->flags & XPC_C_CONNECTED)) {
  1654. ret = xpNotConnected;
  1655. goto out_1;
  1656. }
  1657. ret = xpc_allocate_msg_sn2(ch, flags, &msg);
  1658. if (ret != xpSuccess)
  1659. goto out_1;
  1660. msg_number = msg->number;
  1661. if (notify_type != 0) {
  1662. /*
  1663. * Tell the remote side to send an ACK interrupt when the
  1664. * message has been delivered.
  1665. */
  1666. msg->flags |= XPC_M_INTERRUPT;
  1667. atomic_inc(&ch->n_to_notify);
  1668. notify = &ch->notify_queue[msg_number % ch->local_nentries];
  1669. notify->func = func;
  1670. notify->key = key;
  1671. notify->type = notify_type;
  1672. /* >>> is a mb() needed here? */
  1673. if (ch->flags & XPC_C_DISCONNECTING) {
  1674. /*
  1675. * An error occurred between our last error check and
  1676. * this one. We will try to clear the type field from
  1677. * the notify entry. If we succeed then
  1678. * xpc_disconnect_channel() didn't already process
  1679. * the notify entry.
  1680. */
  1681. if (cmpxchg(&notify->type, notify_type, 0) ==
  1682. notify_type) {
  1683. atomic_dec(&ch->n_to_notify);
  1684. ret = ch->reason;
  1685. }
  1686. goto out_1;
  1687. }
  1688. }
  1689. memcpy(&msg->payload, payload, payload_size);
  1690. msg->flags |= XPC_M_READY;
  1691. /*
  1692. * The preceding store of msg->flags must occur before the following
  1693. * load of local_GP->put.
  1694. */
  1695. mb();
  1696. /* see if the message is next in line to be sent, if so send it */
  1697. put = ch->sn.sn2.local_GP->put;
  1698. if (put == msg_number)
  1699. xpc_send_msgs_sn2(ch, put);
  1700. out_1:
  1701. xpc_msgqueue_deref(ch);
  1702. return ret;
  1703. }
  1704. /*
  1705. * Now we actually acknowledge the messages that have been delivered and ack'd
  1706. * by advancing the cached remote message queue's Get value and if requested
  1707. * send a chctl msgrequest to the message sender's partition.
  1708. */
  1709. static void
  1710. xpc_acknowledge_msgs_sn2(struct xpc_channel *ch, s64 initial_get, u8 msg_flags)
  1711. {
  1712. struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
  1713. struct xpc_msg *msg;
  1714. s64 get = initial_get + 1;
  1715. int send_msgrequest = 0;
  1716. while (1) {
  1717. while (1) {
  1718. if (get == ch_sn2->w_local_GP.get)
  1719. break;
  1720. msg = (struct xpc_msg *)((u64)ch->remote_msgqueue +
  1721. (get % ch->remote_nentries) *
  1722. ch->msg_size);
  1723. if (!(msg->flags & XPC_M_DONE))
  1724. break;
  1725. msg_flags |= msg->flags;
  1726. get++;
  1727. }
  1728. if (get == initial_get) {
  1729. /* nothing's changed */
  1730. break;
  1731. }
  1732. if (cmpxchg_rel(&ch_sn2->local_GP->get, initial_get, get) !=
  1733. initial_get) {
  1734. /* someone else beat us to it */
  1735. DBUG_ON(ch_sn2->local_GP->get <= initial_get);
  1736. break;
  1737. }
  1738. /* we just set the new value of local_GP->get */
  1739. dev_dbg(xpc_chan, "local_GP->get changed to %ld, partid=%d, "
  1740. "channel=%d\n", get, ch->partid, ch->number);
  1741. send_msgrequest = (msg_flags & XPC_M_INTERRUPT);
  1742. /*
  1743. * We need to ensure that the message referenced by
  1744. * local_GP->get is not XPC_M_DONE or that local_GP->get
  1745. * equals w_local_GP.get, so we'll go have a look.
  1746. */
  1747. initial_get = get;
  1748. }
  1749. if (send_msgrequest)
  1750. xpc_send_chctl_msgrequest_sn2(ch);
  1751. }
  1752. static void
  1753. xpc_received_msg_sn2(struct xpc_channel *ch, struct xpc_msg *msg)
  1754. {
  1755. s64 get;
  1756. s64 msg_number = msg->number;
  1757. dev_dbg(xpc_chan, "msg=0x%p, msg_number=%ld, partid=%d, channel=%d\n",
  1758. (void *)msg, msg_number, ch->partid, ch->number);
  1759. DBUG_ON((((u64)msg - (u64)ch->remote_msgqueue) / ch->msg_size) !=
  1760. msg_number % ch->remote_nentries);
  1761. DBUG_ON(msg->flags & XPC_M_DONE);
  1762. msg->flags |= XPC_M_DONE;
  1763. /*
  1764. * The preceding store of msg->flags must occur before the following
  1765. * load of local_GP->get.
  1766. */
  1767. mb();
  1768. /*
  1769. * See if this message is next in line to be acknowledged as having
  1770. * been delivered.
  1771. */
  1772. get = ch->sn.sn2.local_GP->get;
  1773. if (get == msg_number)
  1774. xpc_acknowledge_msgs_sn2(ch, get, msg->flags);
  1775. }
  1776. int
  1777. xpc_init_sn2(void)
  1778. {
  1779. int ret;
  1780. xpc_rsvd_page_init = xpc_rsvd_page_init_sn2;
  1781. xpc_increment_heartbeat = xpc_increment_heartbeat_sn2;
  1782. xpc_offline_heartbeat = xpc_offline_heartbeat_sn2;
  1783. xpc_online_heartbeat = xpc_online_heartbeat_sn2;
  1784. xpc_heartbeat_init = xpc_heartbeat_init_sn2;
  1785. xpc_heartbeat_exit = xpc_heartbeat_exit_sn2;
  1786. xpc_check_remote_hb = xpc_check_remote_hb_sn2;
  1787. xpc_request_partition_activation = xpc_request_partition_activation_sn2;
  1788. xpc_request_partition_reactivation =
  1789. xpc_request_partition_reactivation_sn2;
  1790. xpc_request_partition_deactivation =
  1791. xpc_request_partition_deactivation_sn2;
  1792. xpc_cancel_partition_deactivation_request =
  1793. xpc_cancel_partition_deactivation_request_sn2;
  1794. xpc_process_activate_IRQ_rcvd = xpc_process_activate_IRQ_rcvd_sn2;
  1795. xpc_setup_infrastructure = xpc_setup_infrastructure_sn2;
  1796. xpc_teardown_infrastructure = xpc_teardown_infrastructure_sn2;
  1797. xpc_make_first_contact = xpc_make_first_contact_sn2;
  1798. xpc_get_chctl_all_flags = xpc_get_chctl_all_flags_sn2;
  1799. xpc_notify_senders_of_disconnect = xpc_notify_senders_of_disconnect_sn2;
  1800. xpc_process_msg_chctl_flags = xpc_process_msg_chctl_flags_sn2;
  1801. xpc_n_of_deliverable_msgs = xpc_n_of_deliverable_msgs_sn2;
  1802. xpc_get_deliverable_msg = xpc_get_deliverable_msg_sn2;
  1803. xpc_indicate_partition_engaged = xpc_indicate_partition_engaged_sn2;
  1804. xpc_partition_engaged = xpc_partition_engaged_sn2;
  1805. xpc_any_partition_engaged = xpc_any_partition_engaged_sn2;
  1806. xpc_indicate_partition_disengaged =
  1807. xpc_indicate_partition_disengaged_sn2;
  1808. xpc_assume_partition_disengaged = xpc_assume_partition_disengaged_sn2;
  1809. xpc_send_chctl_closerequest = xpc_send_chctl_closerequest_sn2;
  1810. xpc_send_chctl_closereply = xpc_send_chctl_closereply_sn2;
  1811. xpc_send_chctl_openrequest = xpc_send_chctl_openrequest_sn2;
  1812. xpc_send_chctl_openreply = xpc_send_chctl_openreply_sn2;
  1813. xpc_send_msg = xpc_send_msg_sn2;
  1814. xpc_received_msg = xpc_received_msg_sn2;
  1815. /* open up protections for IPI and [potentially] amo operations */
  1816. xpc_allow_IPI_ops_sn2();
  1817. xpc_allow_amo_ops_shub_wars_1_1_sn2();
  1818. /*
  1819. * This is safe to do before the xpc_hb_checker thread has started
  1820. * because the handler releases a wait queue. If an interrupt is
  1821. * received before the thread is waiting, it will not go to sleep,
  1822. * but rather immediately process the interrupt.
  1823. */
  1824. ret = request_irq(SGI_XPC_ACTIVATE, xpc_handle_activate_IRQ_sn2, 0,
  1825. "xpc hb", NULL);
  1826. if (ret != 0) {
  1827. dev_err(xpc_part, "can't register ACTIVATE IRQ handler, "
  1828. "errno=%d\n", -ret);
  1829. xpc_disallow_IPI_ops_sn2();
  1830. }
  1831. return ret;
  1832. }
  1833. void
  1834. xpc_exit_sn2(void)
  1835. {
  1836. free_irq(SGI_XPC_ACTIVATE, NULL);
  1837. xpc_disallow_IPI_ops_sn2();
  1838. }