xpc_sn2.c 66 KB

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