xpc_sn2.c 68 KB

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