xpc_sn2.c 67 KB

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