xpc_sn2.c 68 KB

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