xpc_sn2.c 69 KB

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