xpc_sn2.c 67 KB

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