xpc_sn2.c 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (c) 2008 Silicon Graphics, Inc. All Rights Reserved.
  7. */
  8. /*
  9. * Cross Partition Communication (XPC) sn2-based functions.
  10. *
  11. * Architecture specific implementation of common functions.
  12. *
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/delay.h>
  16. #include <asm/uncached.h>
  17. #include <asm/sn/sn_sal.h>
  18. #include "xpc.h"
  19. /*
  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(u64 amos_page_pa, int from_nasid, int to_nasid,
  185. 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);
  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_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 = __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_rsvd_page_init_sn2(struct xpc_rsvd_page *rp)
  498. {
  499. struct amo *amos_page;
  500. int i;
  501. int ret;
  502. xpc_vars_sn2 = XPC_RP_VARS(rp);
  503. rp->sn.vars_pa = __pa(xpc_vars_sn2);
  504. /* vars_part array follows immediately after vars */
  505. xpc_vars_part_sn2 = (struct xpc_vars_part_sn2 *)((u8 *)XPC_RP_VARS(rp) +
  506. XPC_RP_VARS_SIZE);
  507. /*
  508. * Before clearing xpc_vars_sn2, see if a page of amos had been
  509. * previously allocated. If not we'll need to allocate one and set
  510. * permissions so that cross-partition amos are allowed.
  511. *
  512. * The allocated amo page needs MCA reporting to remain disabled after
  513. * XPC has unloaded. To make this work, we keep a copy of the pointer
  514. * to this page (i.e., amos_page) in the struct xpc_vars_sn2 structure,
  515. * which is pointed to by the reserved page, and re-use that saved copy
  516. * on subsequent loads of XPC. This amo page is never freed, and its
  517. * memory protections are never restricted.
  518. */
  519. amos_page = xpc_vars_sn2->amos_page;
  520. if (amos_page == NULL) {
  521. amos_page = (struct amo *)TO_AMO(uncached_alloc_page(0, 1));
  522. if (amos_page == NULL) {
  523. dev_err(xpc_part, "can't allocate page of amos\n");
  524. return xpNoMemory;
  525. }
  526. /*
  527. * Open up amo-R/W to cpu. This is done on Shub 1.1 systems
  528. * when xpc_allow_amo_ops_shub_wars_1_1_sn2() is called.
  529. */
  530. ret = xpc_allow_amo_ops_sn2(amos_page);
  531. if (ret != xpSuccess) {
  532. dev_err(xpc_part, "can't allow amo operations\n");
  533. uncached_free_page(__IA64_UNCACHED_OFFSET |
  534. TO_PHYS((u64)amos_page), 1);
  535. return ret;
  536. }
  537. }
  538. /* clear xpc_vars_sn2 */
  539. memset(xpc_vars_sn2, 0, sizeof(struct xpc_vars_sn2));
  540. xpc_vars_sn2->version = XPC_V_VERSION;
  541. xpc_vars_sn2->activate_IRQ_nasid = cpuid_to_nasid(0);
  542. xpc_vars_sn2->activate_IRQ_phys_cpuid = cpu_physical_id(0);
  543. xpc_vars_sn2->vars_part_pa = __pa(xpc_vars_part_sn2);
  544. xpc_vars_sn2->amos_page_pa = ia64_tpa((u64)amos_page);
  545. xpc_vars_sn2->amos_page = amos_page; /* save for next load of XPC */
  546. /* clear xpc_vars_part_sn2 */
  547. memset((u64 *)xpc_vars_part_sn2, 0, sizeof(struct xpc_vars_part_sn2) *
  548. xp_max_npartitions);
  549. /* initialize the activate IRQ related amo variables */
  550. for (i = 0; i < xpc_nasid_mask_nlongs; i++)
  551. (void)xpc_init_IRQ_amo_sn2(XPC_ACTIVATE_IRQ_AMOS_SN2 + i);
  552. /* initialize the engaged remote partitions related amo variables */
  553. (void)xpc_init_IRQ_amo_sn2(XPC_ENGAGED_PARTITIONS_AMO_SN2);
  554. (void)xpc_init_IRQ_amo_sn2(XPC_DEACTIVATE_REQUEST_AMO_SN2);
  555. return xpSuccess;
  556. }
  557. static void
  558. xpc_increment_heartbeat_sn2(void)
  559. {
  560. xpc_vars_sn2->heartbeat++;
  561. }
  562. static void
  563. xpc_offline_heartbeat_sn2(void)
  564. {
  565. xpc_increment_heartbeat_sn2();
  566. xpc_vars_sn2->heartbeat_offline = 1;
  567. }
  568. static void
  569. xpc_online_heartbeat_sn2(void)
  570. {
  571. xpc_increment_heartbeat_sn2();
  572. xpc_vars_sn2->heartbeat_offline = 0;
  573. }
  574. static void
  575. xpc_heartbeat_init_sn2(void)
  576. {
  577. DBUG_ON(xpc_vars_sn2 == NULL);
  578. bitmap_zero(xpc_vars_sn2->heartbeating_to_mask, XP_MAX_NPARTITIONS_SN2);
  579. xpc_heartbeating_to_mask = &xpc_vars_sn2->heartbeating_to_mask[0];
  580. xpc_online_heartbeat_sn2();
  581. }
  582. static void
  583. xpc_heartbeat_exit_sn2(void)
  584. {
  585. xpc_offline_heartbeat_sn2();
  586. }
  587. /*
  588. * At periodic intervals, scan through all active partitions and ensure
  589. * their heartbeat is still active. If not, the partition is deactivated.
  590. */
  591. static void
  592. xpc_check_remote_hb_sn2(void)
  593. {
  594. struct xpc_vars_sn2 *remote_vars;
  595. struct xpc_partition *part;
  596. short partid;
  597. enum xp_retval ret;
  598. remote_vars = (struct xpc_vars_sn2 *)xpc_remote_copy_buffer_sn2;
  599. for (partid = 0; partid < xp_max_npartitions; partid++) {
  600. if (xpc_exiting)
  601. break;
  602. if (partid == sn_partition_id)
  603. continue;
  604. part = &xpc_partitions[partid];
  605. if (part->act_state == XPC_P_INACTIVE ||
  606. part->act_state == XPC_P_DEACTIVATING) {
  607. continue;
  608. }
  609. /* pull the remote_hb cache line */
  610. ret = xp_remote_memcpy(remote_vars,
  611. (void *)part->sn.sn2.remote_vars_pa,
  612. XPC_RP_VARS_SIZE);
  613. if (ret != xpSuccess) {
  614. XPC_DEACTIVATE_PARTITION(part, ret);
  615. continue;
  616. }
  617. dev_dbg(xpc_part, "partid = %d, heartbeat = %ld, last_heartbeat"
  618. " = %ld, heartbeat_offline = %ld, HB_mask[0] = 0x%lx\n",
  619. partid, remote_vars->heartbeat, part->last_heartbeat,
  620. remote_vars->heartbeat_offline,
  621. remote_vars->heartbeating_to_mask[0]);
  622. if (((remote_vars->heartbeat == part->last_heartbeat) &&
  623. (remote_vars->heartbeat_offline == 0)) ||
  624. !xpc_hb_allowed(sn_partition_id,
  625. &remote_vars->heartbeating_to_mask)) {
  626. XPC_DEACTIVATE_PARTITION(part, xpNoHeartbeat);
  627. continue;
  628. }
  629. part->last_heartbeat = remote_vars->heartbeat;
  630. }
  631. }
  632. /*
  633. * Get a copy of the remote partition's XPC variables from the reserved page.
  634. *
  635. * remote_vars points to a buffer that is cacheline aligned for BTE copies and
  636. * assumed to be of size XPC_RP_VARS_SIZE.
  637. */
  638. static enum xp_retval
  639. xpc_get_remote_vars_sn2(u64 remote_vars_pa, struct xpc_vars_sn2 *remote_vars)
  640. {
  641. enum xp_retval ret;
  642. if (remote_vars_pa == 0)
  643. return xpVarsNotSet;
  644. /* pull over the cross partition variables */
  645. ret = xp_remote_memcpy(remote_vars, (void *)remote_vars_pa,
  646. XPC_RP_VARS_SIZE);
  647. if (ret != xpSuccess)
  648. return ret;
  649. if (XPC_VERSION_MAJOR(remote_vars->version) !=
  650. XPC_VERSION_MAJOR(XPC_V_VERSION)) {
  651. return xpBadVersion;
  652. }
  653. return xpSuccess;
  654. }
  655. static void
  656. xpc_request_partition_activation_sn2(struct xpc_rsvd_page *remote_rp,
  657. u64 remote_rp_pa, int nasid)
  658. {
  659. xpc_send_local_activate_IRQ_sn2(nasid);
  660. }
  661. static void
  662. xpc_request_partition_reactivation_sn2(struct xpc_partition *part)
  663. {
  664. xpc_send_local_activate_IRQ_sn2(part->sn.sn2.activate_IRQ_nasid);
  665. }
  666. static void
  667. xpc_request_partition_deactivation_sn2(struct xpc_partition *part)
  668. {
  669. struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
  670. unsigned long irq_flags;
  671. struct amo *amo = (struct amo *)__va(part_sn2->remote_amos_page_pa +
  672. (XPC_DEACTIVATE_REQUEST_AMO_SN2 *
  673. sizeof(struct amo)));
  674. local_irq_save(irq_flags);
  675. /* set bit corresponding to our partid in remote partition's amo */
  676. FETCHOP_STORE_OP(TO_AMO((u64)&amo->variable), FETCHOP_OR,
  677. BIT(sn_partition_id));
  678. /*
  679. * We must always use the nofault function regardless of whether we
  680. * are on a Shub 1.1 system or a Shub 1.2 slice 0xc processor. If we
  681. * didn't, we'd never know that the other partition is down and would
  682. * keep sending IRQs and amos to it until the heartbeat times out.
  683. */
  684. (void)xp_nofault_PIOR((u64 *)GLOBAL_MMR_ADDR(NASID_GET(&amo->
  685. variable),
  686. xp_nofault_PIOR_target));
  687. local_irq_restore(irq_flags);
  688. /*
  689. * Send activate IRQ to get other side to see that we've set our
  690. * bit in their deactivate request amo.
  691. */
  692. xpc_send_activate_IRQ_sn2(part_sn2->remote_amos_page_pa,
  693. cnodeid_to_nasid(0),
  694. part_sn2->activate_IRQ_nasid,
  695. part_sn2->activate_IRQ_phys_cpuid);
  696. }
  697. static void
  698. xpc_cancel_partition_deactivation_request_sn2(struct xpc_partition *part)
  699. {
  700. unsigned long irq_flags;
  701. struct amo *amo = (struct amo *)__va(part->sn.sn2.remote_amos_page_pa +
  702. (XPC_DEACTIVATE_REQUEST_AMO_SN2 *
  703. sizeof(struct amo)));
  704. local_irq_save(irq_flags);
  705. /* clear bit corresponding to our partid in remote partition's amo */
  706. FETCHOP_STORE_OP(TO_AMO((u64)&amo->variable), FETCHOP_AND,
  707. ~BIT(sn_partition_id));
  708. /*
  709. * We must always use the nofault function regardless of whether we
  710. * are on a Shub 1.1 system or a Shub 1.2 slice 0xc processor. If we
  711. * didn't, we'd never know that the other partition is down and would
  712. * keep sending IRQs and amos to it until the heartbeat times out.
  713. */
  714. (void)xp_nofault_PIOR((u64 *)GLOBAL_MMR_ADDR(NASID_GET(&amo->
  715. variable),
  716. xp_nofault_PIOR_target));
  717. local_irq_restore(irq_flags);
  718. }
  719. static int
  720. xpc_partition_deactivation_requested_sn2(short partid)
  721. {
  722. struct amo *amo = xpc_vars_sn2->amos_page +
  723. XPC_DEACTIVATE_REQUEST_AMO_SN2;
  724. /* our partition's amo variable ANDed with partid mask */
  725. return (FETCHOP_LOAD_OP(TO_AMO((u64)&amo->variable), FETCHOP_LOAD) &
  726. BIT(partid)) != 0;
  727. }
  728. /*
  729. * Update the remote partition's info.
  730. */
  731. static void
  732. xpc_update_partition_info_sn2(struct xpc_partition *part, u8 remote_rp_version,
  733. unsigned long *remote_rp_stamp, u64 remote_rp_pa,
  734. u64 remote_vars_pa,
  735. struct xpc_vars_sn2 *remote_vars)
  736. {
  737. struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
  738. part->remote_rp_version = remote_rp_version;
  739. dev_dbg(xpc_part, " remote_rp_version = 0x%016x\n",
  740. part->remote_rp_version);
  741. part->remote_rp_stamp = *remote_rp_stamp;
  742. dev_dbg(xpc_part, " remote_rp_stamp = 0x%016lx\n",
  743. part->remote_rp_stamp);
  744. part->remote_rp_pa = remote_rp_pa;
  745. dev_dbg(xpc_part, " remote_rp_pa = 0x%016lx\n", part->remote_rp_pa);
  746. part_sn2->remote_vars_pa = remote_vars_pa;
  747. dev_dbg(xpc_part, " remote_vars_pa = 0x%016lx\n",
  748. part_sn2->remote_vars_pa);
  749. part->last_heartbeat = remote_vars->heartbeat;
  750. dev_dbg(xpc_part, " last_heartbeat = 0x%016lx\n",
  751. part->last_heartbeat);
  752. part_sn2->remote_vars_part_pa = remote_vars->vars_part_pa;
  753. dev_dbg(xpc_part, " remote_vars_part_pa = 0x%016lx\n",
  754. part_sn2->remote_vars_part_pa);
  755. part_sn2->activate_IRQ_nasid = remote_vars->activate_IRQ_nasid;
  756. dev_dbg(xpc_part, " activate_IRQ_nasid = 0x%x\n",
  757. part_sn2->activate_IRQ_nasid);
  758. part_sn2->activate_IRQ_phys_cpuid =
  759. remote_vars->activate_IRQ_phys_cpuid;
  760. dev_dbg(xpc_part, " activate_IRQ_phys_cpuid = 0x%x\n",
  761. part_sn2->activate_IRQ_phys_cpuid);
  762. part_sn2->remote_amos_page_pa = remote_vars->amos_page_pa;
  763. dev_dbg(xpc_part, " remote_amos_page_pa = 0x%lx\n",
  764. part_sn2->remote_amos_page_pa);
  765. part_sn2->remote_vars_version = remote_vars->version;
  766. dev_dbg(xpc_part, " remote_vars_version = 0x%x\n",
  767. part_sn2->remote_vars_version);
  768. }
  769. /*
  770. * Prior code has determined the nasid which generated a activate IRQ.
  771. * Inspect that nasid to determine if its partition needs to be activated
  772. * or deactivated.
  773. *
  774. * A partition is considered "awaiting activation" if our partition
  775. * flags indicate it is not active and it has a heartbeat. A
  776. * partition is considered "awaiting deactivation" if our partition
  777. * flags indicate it is active but it has no heartbeat or it is not
  778. * sending its heartbeat to us.
  779. *
  780. * To determine the heartbeat, the remote nasid must have a properly
  781. * initialized reserved page.
  782. */
  783. static void
  784. xpc_identify_activate_IRQ_req_sn2(int nasid)
  785. {
  786. struct xpc_rsvd_page *remote_rp;
  787. struct xpc_vars_sn2 *remote_vars;
  788. u64 remote_rp_pa;
  789. u64 remote_vars_pa;
  790. int remote_rp_version;
  791. int reactivate = 0;
  792. unsigned long remote_rp_stamp = 0;
  793. short partid;
  794. struct xpc_partition *part;
  795. struct xpc_partition_sn2 *part_sn2;
  796. enum xp_retval ret;
  797. /* pull over the reserved page structure */
  798. remote_rp = (struct xpc_rsvd_page *)xpc_remote_copy_buffer_sn2;
  799. ret = xpc_get_remote_rp(nasid, NULL, remote_rp, &remote_rp_pa);
  800. if (ret != xpSuccess) {
  801. dev_warn(xpc_part, "unable to get reserved page from nasid %d, "
  802. "which sent interrupt, reason=%d\n", nasid, ret);
  803. return;
  804. }
  805. remote_vars_pa = remote_rp->sn.vars_pa;
  806. remote_rp_version = remote_rp->version;
  807. remote_rp_stamp = remote_rp->stamp;
  808. partid = remote_rp->SAL_partid;
  809. part = &xpc_partitions[partid];
  810. part_sn2 = &part->sn.sn2;
  811. /* pull over the cross partition variables */
  812. remote_vars = (struct xpc_vars_sn2 *)xpc_remote_copy_buffer_sn2;
  813. ret = xpc_get_remote_vars_sn2(remote_vars_pa, remote_vars);
  814. if (ret != xpSuccess) {
  815. dev_warn(xpc_part, "unable to get XPC variables from nasid %d, "
  816. "which sent interrupt, reason=%d\n", nasid, ret);
  817. XPC_DEACTIVATE_PARTITION(part, ret);
  818. return;
  819. }
  820. part->activate_IRQ_rcvd++;
  821. dev_dbg(xpc_part, "partid for nasid %d is %d; IRQs = %d; HB = "
  822. "%ld:0x%lx\n", (int)nasid, (int)partid, part->activate_IRQ_rcvd,
  823. remote_vars->heartbeat, remote_vars->heartbeating_to_mask[0]);
  824. if (xpc_partition_disengaged(part) &&
  825. part->act_state == XPC_P_INACTIVE) {
  826. xpc_update_partition_info_sn2(part, remote_rp_version,
  827. &remote_rp_stamp, remote_rp_pa,
  828. remote_vars_pa, 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_stamp != part->remote_rp_stamp) {
  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_stamp, remote_rp_pa,
  847. remote_vars_pa, remote_vars);
  848. reactivate = 1;
  849. }
  850. if (part->disengage_timeout > 0 && !xpc_partition_disengaged(part)) {
  851. /* still waiting on other side to disengage from us */
  852. return;
  853. }
  854. if (reactivate)
  855. XPC_DEACTIVATE_PARTITION(part, xpReactivating);
  856. else if (xpc_partition_deactivation_requested_sn2(partid))
  857. XPC_DEACTIVATE_PARTITION(part, xpOtherGoingDown);
  858. }
  859. /*
  860. * Loop through the activation amo variables and process any bits
  861. * which are set. Each bit indicates a nasid sending a partition
  862. * activation or deactivation request.
  863. *
  864. * Return #of IRQs detected.
  865. */
  866. int
  867. xpc_identify_activate_IRQ_sender_sn2(void)
  868. {
  869. int l;
  870. int b;
  871. unsigned long nasid_mask_long;
  872. u64 nasid; /* remote nasid */
  873. int n_IRQs_detected = 0;
  874. struct amo *act_amos;
  875. act_amos = xpc_vars_sn2->amos_page + XPC_ACTIVATE_IRQ_AMOS_SN2;
  876. /* scan through activate amo variables looking for non-zero entries */
  877. for (l = 0; l < xpc_nasid_mask_nlongs; l++) {
  878. if (xpc_exiting)
  879. break;
  880. nasid_mask_long = xpc_receive_IRQ_amo_sn2(&act_amos[l]);
  881. b = find_first_bit(&nasid_mask_long, BITS_PER_LONG);
  882. if (b >= BITS_PER_LONG) {
  883. /* no IRQs from nasids in this amo variable */
  884. continue;
  885. }
  886. dev_dbg(xpc_part, "amo[%d] gave back 0x%lx\n", l,
  887. nasid_mask_long);
  888. /*
  889. * If this nasid has been added to the machine since
  890. * our partition was reset, this will retain the
  891. * remote nasid in our reserved pages machine mask.
  892. * This is used in the event of module reload.
  893. */
  894. xpc_mach_nasids[l] |= nasid_mask_long;
  895. /* locate the nasid(s) which sent interrupts */
  896. do {
  897. n_IRQs_detected++;
  898. nasid = (l * BITS_PER_LONG + b) * 2;
  899. dev_dbg(xpc_part, "interrupt from nasid %ld\n", nasid);
  900. xpc_identify_activate_IRQ_req_sn2(nasid);
  901. b = find_next_bit(&nasid_mask_long, BITS_PER_LONG,
  902. b + 1);
  903. } while (b < BITS_PER_LONG);
  904. }
  905. return n_IRQs_detected;
  906. }
  907. static void
  908. xpc_process_activate_IRQ_rcvd_sn2(int n_IRQs_expected)
  909. {
  910. int n_IRQs_detected;
  911. n_IRQs_detected = xpc_identify_activate_IRQ_sender_sn2();
  912. if (n_IRQs_detected < n_IRQs_expected) {
  913. /* retry once to help avoid missing amo */
  914. (void)xpc_identify_activate_IRQ_sender_sn2();
  915. }
  916. }
  917. /*
  918. * Guarantee that the kzalloc'd memory is cacheline aligned.
  919. */
  920. static void *
  921. xpc_kzalloc_cacheline_aligned_sn2(size_t size, gfp_t flags, void **base)
  922. {
  923. /* see if kzalloc will give us cachline aligned memory by default */
  924. *base = kzalloc(size, flags);
  925. if (*base == NULL)
  926. return NULL;
  927. if ((u64)*base == L1_CACHE_ALIGN((u64)*base))
  928. return *base;
  929. kfree(*base);
  930. /* nope, we'll have to do it ourselves */
  931. *base = kzalloc(size + L1_CACHE_BYTES, flags);
  932. if (*base == NULL)
  933. return NULL;
  934. return (void *)L1_CACHE_ALIGN((u64)*base);
  935. }
  936. /*
  937. * Setup the infrastructure necessary to support XPartition Communication
  938. * between the specified remote partition and the local one.
  939. */
  940. static enum xp_retval
  941. xpc_setup_infrastructure_sn2(struct xpc_partition *part)
  942. {
  943. struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
  944. enum xp_retval retval;
  945. int ret;
  946. int cpuid;
  947. int ch_number;
  948. struct xpc_channel *ch;
  949. struct timer_list *timer;
  950. short partid = XPC_PARTID(part);
  951. /*
  952. * Allocate all of the channel structures as a contiguous chunk of
  953. * memory.
  954. */
  955. DBUG_ON(part->channels != NULL);
  956. part->channels = kzalloc(sizeof(struct xpc_channel) * XPC_MAX_NCHANNELS,
  957. GFP_KERNEL);
  958. if (part->channels == NULL) {
  959. dev_err(xpc_chan, "can't get memory for channels\n");
  960. return xpNoMemory;
  961. }
  962. /* allocate all the required GET/PUT values */
  963. part_sn2->local_GPs =
  964. xpc_kzalloc_cacheline_aligned_sn2(XPC_GP_SIZE, GFP_KERNEL,
  965. &part_sn2->local_GPs_base);
  966. if (part_sn2->local_GPs == NULL) {
  967. dev_err(xpc_chan, "can't get memory for local get/put "
  968. "values\n");
  969. retval = xpNoMemory;
  970. goto out_1;
  971. }
  972. part_sn2->remote_GPs =
  973. xpc_kzalloc_cacheline_aligned_sn2(XPC_GP_SIZE, GFP_KERNEL,
  974. &part_sn2->remote_GPs_base);
  975. if (part_sn2->remote_GPs == NULL) {
  976. dev_err(xpc_chan, "can't get memory for remote get/put "
  977. "values\n");
  978. retval = xpNoMemory;
  979. goto out_2;
  980. }
  981. part_sn2->remote_GPs_pa = 0;
  982. /* allocate all the required open and close args */
  983. part->local_openclose_args =
  984. xpc_kzalloc_cacheline_aligned_sn2(XPC_OPENCLOSE_ARGS_SIZE,
  985. GFP_KERNEL,
  986. &part->local_openclose_args_base);
  987. if (part->local_openclose_args == NULL) {
  988. dev_err(xpc_chan, "can't get memory for local connect args\n");
  989. retval = xpNoMemory;
  990. goto out_3;
  991. }
  992. part->remote_openclose_args =
  993. xpc_kzalloc_cacheline_aligned_sn2(XPC_OPENCLOSE_ARGS_SIZE,
  994. GFP_KERNEL,
  995. &part->remote_openclose_args_base);
  996. if (part->remote_openclose_args == NULL) {
  997. dev_err(xpc_chan, "can't get memory for remote connect args\n");
  998. retval = xpNoMemory;
  999. goto out_4;
  1000. }
  1001. part_sn2->remote_openclose_args_pa = 0;
  1002. part_sn2->local_chctl_amo_va = xpc_init_IRQ_amo_sn2(partid);
  1003. part->chctl.all_flags = 0;
  1004. spin_lock_init(&part->chctl_lock);
  1005. part_sn2->notify_IRQ_nasid = 0;
  1006. part_sn2->notify_IRQ_phys_cpuid = 0;
  1007. part_sn2->remote_chctl_amo_va = NULL;
  1008. atomic_set(&part->channel_mgr_requests, 1);
  1009. init_waitqueue_head(&part->channel_mgr_wq);
  1010. sprintf(part_sn2->notify_IRQ_owner, "xpc%02d", partid);
  1011. ret = request_irq(SGI_XPC_NOTIFY, xpc_handle_notify_IRQ_sn2,
  1012. IRQF_SHARED, part_sn2->notify_IRQ_owner,
  1013. (void *)(u64)partid);
  1014. if (ret != 0) {
  1015. dev_err(xpc_chan, "can't register NOTIFY IRQ handler, "
  1016. "errno=%d\n", -ret);
  1017. retval = xpLackOfResources;
  1018. goto out_5;
  1019. }
  1020. /* Setup a timer to check for dropped notify IRQs */
  1021. timer = &part_sn2->dropped_notify_IRQ_timer;
  1022. init_timer(timer);
  1023. timer->function =
  1024. (void (*)(unsigned long))xpc_check_for_dropped_notify_IRQ_sn2;
  1025. timer->data = (unsigned long)part;
  1026. timer->expires = jiffies + XPC_DROPPED_NOTIFY_IRQ_WAIT_INTERVAL;
  1027. add_timer(timer);
  1028. part->nchannels = XPC_MAX_NCHANNELS;
  1029. atomic_set(&part->nchannels_active, 0);
  1030. atomic_set(&part->nchannels_engaged, 0);
  1031. for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
  1032. ch = &part->channels[ch_number];
  1033. ch->partid = partid;
  1034. ch->number = ch_number;
  1035. ch->flags = XPC_C_DISCONNECTED;
  1036. ch->sn.sn2.local_GP = &part_sn2->local_GPs[ch_number];
  1037. ch->local_openclose_args =
  1038. &part->local_openclose_args[ch_number];
  1039. atomic_set(&ch->kthreads_assigned, 0);
  1040. atomic_set(&ch->kthreads_idle, 0);
  1041. atomic_set(&ch->kthreads_active, 0);
  1042. atomic_set(&ch->references, 0);
  1043. atomic_set(&ch->n_to_notify, 0);
  1044. spin_lock_init(&ch->lock);
  1045. mutex_init(&ch->sn.sn2.msg_to_pull_mutex);
  1046. init_completion(&ch->wdisconnect_wait);
  1047. atomic_set(&ch->n_on_msg_allocate_wq, 0);
  1048. init_waitqueue_head(&ch->msg_allocate_wq);
  1049. init_waitqueue_head(&ch->idle_wq);
  1050. }
  1051. /*
  1052. * With the setting of the partition setup_state to XPC_P_SETUP, we're
  1053. * declaring that this partition is ready to go.
  1054. */
  1055. part->setup_state = XPC_P_SETUP;
  1056. /*
  1057. * Setup the per partition specific variables required by the
  1058. * remote partition to establish channel connections with us.
  1059. *
  1060. * The setting of the magic # indicates that these per partition
  1061. * specific variables are ready to be used.
  1062. */
  1063. xpc_vars_part_sn2[partid].GPs_pa = __pa(part_sn2->local_GPs);
  1064. xpc_vars_part_sn2[partid].openclose_args_pa =
  1065. __pa(part->local_openclose_args);
  1066. xpc_vars_part_sn2[partid].chctl_amo_pa =
  1067. __pa(part_sn2->local_chctl_amo_va);
  1068. cpuid = raw_smp_processor_id(); /* any CPU in this partition will do */
  1069. xpc_vars_part_sn2[partid].notify_IRQ_nasid = cpuid_to_nasid(cpuid);
  1070. xpc_vars_part_sn2[partid].notify_IRQ_phys_cpuid =
  1071. cpu_physical_id(cpuid);
  1072. xpc_vars_part_sn2[partid].nchannels = part->nchannels;
  1073. xpc_vars_part_sn2[partid].magic = XPC_VP_MAGIC1;
  1074. return xpSuccess;
  1075. /* setup of infrastructure failed */
  1076. out_5:
  1077. kfree(part->remote_openclose_args_base);
  1078. part->remote_openclose_args = NULL;
  1079. out_4:
  1080. kfree(part->local_openclose_args_base);
  1081. part->local_openclose_args = NULL;
  1082. out_3:
  1083. kfree(part_sn2->remote_GPs_base);
  1084. part_sn2->remote_GPs = NULL;
  1085. out_2:
  1086. kfree(part_sn2->local_GPs_base);
  1087. part_sn2->local_GPs = NULL;
  1088. out_1:
  1089. kfree(part->channels);
  1090. part->channels = NULL;
  1091. return retval;
  1092. }
  1093. /*
  1094. * Teardown the infrastructure necessary to support XPartition Communication
  1095. * between the specified remote partition and the local one.
  1096. */
  1097. static void
  1098. xpc_teardown_infrastructure_sn2(struct xpc_partition *part)
  1099. {
  1100. struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
  1101. short partid = XPC_PARTID(part);
  1102. /*
  1103. * We start off by making this partition inaccessible to local
  1104. * processes by marking it as no longer setup. Then we make it
  1105. * inaccessible to remote processes by clearing the XPC per partition
  1106. * specific variable's magic # (which indicates that these variables
  1107. * are no longer valid) and by ignoring all XPC notify IRQs sent to
  1108. * this partition.
  1109. */
  1110. DBUG_ON(atomic_read(&part->nchannels_engaged) != 0);
  1111. DBUG_ON(atomic_read(&part->nchannels_active) != 0);
  1112. DBUG_ON(part->setup_state != XPC_P_SETUP);
  1113. part->setup_state = XPC_P_WTEARDOWN;
  1114. xpc_vars_part_sn2[partid].magic = 0;
  1115. free_irq(SGI_XPC_NOTIFY, (void *)(u64)partid);
  1116. /*
  1117. * Before proceeding with the teardown we have to wait until all
  1118. * existing references cease.
  1119. */
  1120. wait_event(part->teardown_wq, (atomic_read(&part->references) == 0));
  1121. /* now we can begin tearing down the infrastructure */
  1122. part->setup_state = XPC_P_TORNDOWN;
  1123. /* in case we've still got outstanding timers registered... */
  1124. del_timer_sync(&part_sn2->dropped_notify_IRQ_timer);
  1125. kfree(part->remote_openclose_args_base);
  1126. part->remote_openclose_args = NULL;
  1127. kfree(part->local_openclose_args_base);
  1128. part->local_openclose_args = NULL;
  1129. kfree(part_sn2->remote_GPs_base);
  1130. part_sn2->remote_GPs = NULL;
  1131. kfree(part_sn2->local_GPs_base);
  1132. part_sn2->local_GPs = NULL;
  1133. kfree(part->channels);
  1134. part->channels = NULL;
  1135. part_sn2->local_chctl_amo_va = NULL;
  1136. }
  1137. /*
  1138. * Create a wrapper that hides the underlying mechanism for pulling a cacheline
  1139. * (or multiple cachelines) from a remote partition.
  1140. *
  1141. * src must be a cacheline aligned physical address on the remote partition.
  1142. * dst must be a cacheline aligned virtual address on this partition.
  1143. * cnt must be cacheline sized
  1144. */
  1145. /* ??? Replace this function by call to xp_remote_memcpy() or bte_copy()? */
  1146. static enum xp_retval
  1147. xpc_pull_remote_cachelines_sn2(struct xpc_partition *part, void *dst,
  1148. const void *src, size_t cnt)
  1149. {
  1150. enum xp_retval ret;
  1151. DBUG_ON((u64)src != L1_CACHE_ALIGN((u64)src));
  1152. DBUG_ON((u64)dst != L1_CACHE_ALIGN((u64)dst));
  1153. DBUG_ON(cnt != L1_CACHE_ALIGN(cnt));
  1154. if (part->act_state == XPC_P_DEACTIVATING)
  1155. return part->reason;
  1156. ret = xp_remote_memcpy(dst, src, cnt);
  1157. if (ret != xpSuccess) {
  1158. dev_dbg(xpc_chan, "xp_remote_memcpy() from partition %d failed,"
  1159. " ret=%d\n", XPC_PARTID(part), ret);
  1160. }
  1161. return ret;
  1162. }
  1163. /*
  1164. * Pull the remote per partition specific variables from the specified
  1165. * partition.
  1166. */
  1167. static enum xp_retval
  1168. xpc_pull_remote_vars_part_sn2(struct xpc_partition *part)
  1169. {
  1170. struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
  1171. u8 buffer[L1_CACHE_BYTES * 2];
  1172. struct xpc_vars_part_sn2 *pulled_entry_cacheline =
  1173. (struct xpc_vars_part_sn2 *)L1_CACHE_ALIGN((u64)buffer);
  1174. struct xpc_vars_part_sn2 *pulled_entry;
  1175. u64 remote_entry_cacheline_pa, remote_entry_pa;
  1176. short partid = XPC_PARTID(part);
  1177. enum xp_retval ret;
  1178. /* pull the cacheline that contains the variables we're interested in */
  1179. DBUG_ON(part_sn2->remote_vars_part_pa !=
  1180. L1_CACHE_ALIGN(part_sn2->remote_vars_part_pa));
  1181. DBUG_ON(sizeof(struct xpc_vars_part_sn2) != L1_CACHE_BYTES / 2);
  1182. remote_entry_pa = part_sn2->remote_vars_part_pa +
  1183. sn_partition_id * sizeof(struct xpc_vars_part_sn2);
  1184. remote_entry_cacheline_pa = (remote_entry_pa & ~(L1_CACHE_BYTES - 1));
  1185. pulled_entry = (struct xpc_vars_part_sn2 *)((u64)pulled_entry_cacheline
  1186. + (remote_entry_pa &
  1187. (L1_CACHE_BYTES - 1)));
  1188. ret = xpc_pull_remote_cachelines_sn2(part, pulled_entry_cacheline,
  1189. (void *)remote_entry_cacheline_pa,
  1190. L1_CACHE_BYTES);
  1191. if (ret != xpSuccess) {
  1192. dev_dbg(xpc_chan, "failed to pull XPC vars_part from "
  1193. "partition %d, ret=%d\n", partid, ret);
  1194. return ret;
  1195. }
  1196. /* see if they've been set up yet */
  1197. if (pulled_entry->magic != XPC_VP_MAGIC1 &&
  1198. pulled_entry->magic != XPC_VP_MAGIC2) {
  1199. if (pulled_entry->magic != 0) {
  1200. dev_dbg(xpc_chan, "partition %d's XPC vars_part for "
  1201. "partition %d has bad magic value (=0x%lx)\n",
  1202. partid, sn_partition_id, pulled_entry->magic);
  1203. return xpBadMagic;
  1204. }
  1205. /* they've not been initialized yet */
  1206. return xpRetry;
  1207. }
  1208. if (xpc_vars_part_sn2[partid].magic == XPC_VP_MAGIC1) {
  1209. /* validate the variables */
  1210. if (pulled_entry->GPs_pa == 0 ||
  1211. pulled_entry->openclose_args_pa == 0 ||
  1212. pulled_entry->chctl_amo_pa == 0) {
  1213. dev_err(xpc_chan, "partition %d's XPC vars_part for "
  1214. "partition %d are not valid\n", partid,
  1215. sn_partition_id);
  1216. return xpInvalidAddress;
  1217. }
  1218. /* the variables we imported look to be valid */
  1219. part_sn2->remote_GPs_pa = pulled_entry->GPs_pa;
  1220. part_sn2->remote_openclose_args_pa =
  1221. pulled_entry->openclose_args_pa;
  1222. part_sn2->remote_chctl_amo_va =
  1223. (struct amo *)__va(pulled_entry->chctl_amo_pa);
  1224. part_sn2->notify_IRQ_nasid = pulled_entry->notify_IRQ_nasid;
  1225. part_sn2->notify_IRQ_phys_cpuid =
  1226. pulled_entry->notify_IRQ_phys_cpuid;
  1227. if (part->nchannels > pulled_entry->nchannels)
  1228. part->nchannels = pulled_entry->nchannels;
  1229. /* let the other side know that we've pulled their variables */
  1230. xpc_vars_part_sn2[partid].magic = XPC_VP_MAGIC2;
  1231. }
  1232. if (pulled_entry->magic == XPC_VP_MAGIC1)
  1233. return xpRetry;
  1234. return xpSuccess;
  1235. }
  1236. /*
  1237. * Establish first contact with the remote partititon. This involves pulling
  1238. * the XPC per partition variables from the remote partition and waiting for
  1239. * the remote partition to pull ours.
  1240. */
  1241. static enum xp_retval
  1242. xpc_make_first_contact_sn2(struct xpc_partition *part)
  1243. {
  1244. struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
  1245. enum xp_retval ret;
  1246. /*
  1247. * Register the remote partition's amos with SAL so it can handle
  1248. * and cleanup errors within that address range should the remote
  1249. * partition go down. We don't unregister this range because it is
  1250. * difficult to tell when outstanding writes to the remote partition
  1251. * are finished and thus when it is safe to unregister. This should
  1252. * not result in wasted space in the SAL xp_addr_region table because
  1253. * we should get the same page for remote_amos_page_pa after module
  1254. * reloads and system reboots.
  1255. */
  1256. if (sn_register_xp_addr_region(part_sn2->remote_amos_page_pa,
  1257. PAGE_SIZE, 1) < 0) {
  1258. dev_warn(xpc_part, "xpc_activating(%d) failed to register "
  1259. "xp_addr region\n", XPC_PARTID(part));
  1260. ret = xpPhysAddrRegFailed;
  1261. XPC_DEACTIVATE_PARTITION(part, ret);
  1262. return ret;
  1263. }
  1264. /*
  1265. * Send activate IRQ to get other side to activate if they've not
  1266. * already begun to do so.
  1267. */
  1268. xpc_send_activate_IRQ_sn2(part_sn2->remote_amos_page_pa,
  1269. cnodeid_to_nasid(0),
  1270. part_sn2->activate_IRQ_nasid,
  1271. part_sn2->activate_IRQ_phys_cpuid);
  1272. while ((ret = xpc_pull_remote_vars_part_sn2(part)) != xpSuccess) {
  1273. if (ret != xpRetry) {
  1274. XPC_DEACTIVATE_PARTITION(part, ret);
  1275. return ret;
  1276. }
  1277. dev_dbg(xpc_part, "waiting to make first contact with "
  1278. "partition %d\n", XPC_PARTID(part));
  1279. /* wait a 1/4 of a second or so */
  1280. (void)msleep_interruptible(250);
  1281. if (part->act_state == XPC_P_DEACTIVATING)
  1282. return part->reason;
  1283. }
  1284. return xpSuccess;
  1285. }
  1286. /*
  1287. * Get the chctl flags and pull the openclose args and/or remote GPs as needed.
  1288. */
  1289. static u64
  1290. xpc_get_chctl_all_flags_sn2(struct xpc_partition *part)
  1291. {
  1292. struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
  1293. unsigned long irq_flags;
  1294. union xpc_channel_ctl_flags chctl;
  1295. enum xp_retval ret;
  1296. /*
  1297. * See if there are any chctl flags to be handled.
  1298. */
  1299. spin_lock_irqsave(&part->chctl_lock, irq_flags);
  1300. chctl = part->chctl;
  1301. if (chctl.all_flags != 0)
  1302. part->chctl.all_flags = 0;
  1303. spin_unlock_irqrestore(&part->chctl_lock, irq_flags);
  1304. if (xpc_any_openclose_chctl_flags_set(&chctl)) {
  1305. ret = xpc_pull_remote_cachelines_sn2(part, part->
  1306. remote_openclose_args,
  1307. (void *)part_sn2->
  1308. remote_openclose_args_pa,
  1309. XPC_OPENCLOSE_ARGS_SIZE);
  1310. if (ret != xpSuccess) {
  1311. XPC_DEACTIVATE_PARTITION(part, ret);
  1312. dev_dbg(xpc_chan, "failed to pull openclose args from "
  1313. "partition %d, ret=%d\n", XPC_PARTID(part),
  1314. ret);
  1315. /* don't bother processing chctl flags anymore */
  1316. chctl.all_flags = 0;
  1317. }
  1318. }
  1319. if (xpc_any_msg_chctl_flags_set(&chctl)) {
  1320. ret = xpc_pull_remote_cachelines_sn2(part, part_sn2->remote_GPs,
  1321. (void *)part_sn2->remote_GPs_pa,
  1322. XPC_GP_SIZE);
  1323. if (ret != xpSuccess) {
  1324. XPC_DEACTIVATE_PARTITION(part, ret);
  1325. dev_dbg(xpc_chan, "failed to pull GPs from partition "
  1326. "%d, ret=%d\n", XPC_PARTID(part), ret);
  1327. /* don't bother processing chctl flags anymore */
  1328. chctl.all_flags = 0;
  1329. }
  1330. }
  1331. return chctl.all_flags;
  1332. }
  1333. /*
  1334. * Allocate the local message queue and the notify queue.
  1335. */
  1336. static enum xp_retval
  1337. xpc_allocate_local_msgqueue_sn2(struct xpc_channel *ch)
  1338. {
  1339. unsigned long irq_flags;
  1340. int nentries;
  1341. size_t nbytes;
  1342. for (nentries = ch->local_nentries; nentries > 0; nentries--) {
  1343. nbytes = nentries * ch->msg_size;
  1344. ch->local_msgqueue =
  1345. xpc_kzalloc_cacheline_aligned_sn2(nbytes, GFP_KERNEL,
  1346. &ch->local_msgqueue_base);
  1347. if (ch->local_msgqueue == NULL)
  1348. continue;
  1349. nbytes = nentries * sizeof(struct xpc_notify);
  1350. ch->notify_queue = kzalloc(nbytes, GFP_KERNEL);
  1351. if (ch->notify_queue == NULL) {
  1352. kfree(ch->local_msgqueue_base);
  1353. ch->local_msgqueue = NULL;
  1354. continue;
  1355. }
  1356. spin_lock_irqsave(&ch->lock, irq_flags);
  1357. if (nentries < ch->local_nentries) {
  1358. dev_dbg(xpc_chan, "nentries=%d local_nentries=%d, "
  1359. "partid=%d, channel=%d\n", nentries,
  1360. ch->local_nentries, ch->partid, ch->number);
  1361. ch->local_nentries = nentries;
  1362. }
  1363. spin_unlock_irqrestore(&ch->lock, irq_flags);
  1364. return xpSuccess;
  1365. }
  1366. dev_dbg(xpc_chan, "can't get memory for local message queue and notify "
  1367. "queue, partid=%d, channel=%d\n", ch->partid, ch->number);
  1368. return xpNoMemory;
  1369. }
  1370. /*
  1371. * Allocate the cached remote message queue.
  1372. */
  1373. static enum xp_retval
  1374. xpc_allocate_remote_msgqueue_sn2(struct xpc_channel *ch)
  1375. {
  1376. unsigned long irq_flags;
  1377. int nentries;
  1378. size_t nbytes;
  1379. DBUG_ON(ch->remote_nentries <= 0);
  1380. for (nentries = ch->remote_nentries; nentries > 0; nentries--) {
  1381. nbytes = nentries * ch->msg_size;
  1382. ch->remote_msgqueue =
  1383. xpc_kzalloc_cacheline_aligned_sn2(nbytes, GFP_KERNEL,
  1384. &ch->remote_msgqueue_base);
  1385. if (ch->remote_msgqueue == NULL)
  1386. continue;
  1387. spin_lock_irqsave(&ch->lock, irq_flags);
  1388. if (nentries < ch->remote_nentries) {
  1389. dev_dbg(xpc_chan, "nentries=%d remote_nentries=%d, "
  1390. "partid=%d, channel=%d\n", nentries,
  1391. ch->remote_nentries, ch->partid, ch->number);
  1392. ch->remote_nentries = nentries;
  1393. }
  1394. spin_unlock_irqrestore(&ch->lock, irq_flags);
  1395. return xpSuccess;
  1396. }
  1397. dev_dbg(xpc_chan, "can't get memory for cached remote message queue, "
  1398. "partid=%d, channel=%d\n", ch->partid, ch->number);
  1399. return xpNoMemory;
  1400. }
  1401. /*
  1402. * Allocate message queues and other stuff associated with a channel.
  1403. *
  1404. * Note: Assumes all of the channel sizes are filled in.
  1405. */
  1406. static enum xp_retval
  1407. xpc_allocate_msgqueues_sn2(struct xpc_channel *ch)
  1408. {
  1409. enum xp_retval ret;
  1410. DBUG_ON(ch->flags & XPC_C_SETUP);
  1411. ret = xpc_allocate_local_msgqueue_sn2(ch);
  1412. if (ret == xpSuccess) {
  1413. ret = xpc_allocate_remote_msgqueue_sn2(ch);
  1414. if (ret != xpSuccess) {
  1415. kfree(ch->local_msgqueue_base);
  1416. ch->local_msgqueue = NULL;
  1417. kfree(ch->notify_queue);
  1418. ch->notify_queue = NULL;
  1419. }
  1420. }
  1421. return ret;
  1422. }
  1423. /*
  1424. * Free up message queues and other stuff that were allocated for the specified
  1425. * channel.
  1426. *
  1427. * Note: ch->reason and ch->reason_line are left set for debugging purposes,
  1428. * they're cleared when XPC_C_DISCONNECTED is cleared.
  1429. */
  1430. static void
  1431. xpc_free_msgqueues_sn2(struct xpc_channel *ch)
  1432. {
  1433. struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
  1434. DBUG_ON(!spin_is_locked(&ch->lock));
  1435. DBUG_ON(atomic_read(&ch->n_to_notify) != 0);
  1436. ch->remote_msgqueue_pa = 0;
  1437. ch->func = NULL;
  1438. ch->key = NULL;
  1439. ch->msg_size = 0;
  1440. ch->local_nentries = 0;
  1441. ch->remote_nentries = 0;
  1442. ch->kthreads_assigned_limit = 0;
  1443. ch->kthreads_idle_limit = 0;
  1444. ch_sn2->local_GP->get = 0;
  1445. ch_sn2->local_GP->put = 0;
  1446. ch_sn2->remote_GP.get = 0;
  1447. ch_sn2->remote_GP.put = 0;
  1448. ch_sn2->w_local_GP.get = 0;
  1449. ch_sn2->w_local_GP.put = 0;
  1450. ch_sn2->w_remote_GP.get = 0;
  1451. ch_sn2->w_remote_GP.put = 0;
  1452. ch_sn2->next_msg_to_pull = 0;
  1453. if (ch->flags & XPC_C_SETUP) {
  1454. dev_dbg(xpc_chan, "ch->flags=0x%x, partid=%d, channel=%d\n",
  1455. ch->flags, ch->partid, ch->number);
  1456. kfree(ch->local_msgqueue_base);
  1457. ch->local_msgqueue = NULL;
  1458. kfree(ch->remote_msgqueue_base);
  1459. ch->remote_msgqueue = NULL;
  1460. kfree(ch->notify_queue);
  1461. ch->notify_queue = NULL;
  1462. }
  1463. }
  1464. /*
  1465. * Notify those who wanted to be notified upon delivery of their message.
  1466. */
  1467. static void
  1468. xpc_notify_senders_sn2(struct xpc_channel *ch, enum xp_retval reason, s64 put)
  1469. {
  1470. struct xpc_notify *notify;
  1471. u8 notify_type;
  1472. s64 get = ch->sn.sn2.w_remote_GP.get - 1;
  1473. while (++get < put && atomic_read(&ch->n_to_notify) > 0) {
  1474. notify = &ch->notify_queue[get % ch->local_nentries];
  1475. /*
  1476. * See if the notify entry indicates it was associated with
  1477. * a message who's sender wants to be notified. It is possible
  1478. * that it is, but someone else is doing or has done the
  1479. * notification.
  1480. */
  1481. notify_type = notify->type;
  1482. if (notify_type == 0 ||
  1483. cmpxchg(&notify->type, notify_type, 0) != notify_type) {
  1484. continue;
  1485. }
  1486. DBUG_ON(notify_type != XPC_N_CALL);
  1487. atomic_dec(&ch->n_to_notify);
  1488. if (notify->func != NULL) {
  1489. dev_dbg(xpc_chan, "notify->func() called, notify=0x%p, "
  1490. "msg_number=%ld, partid=%d, channel=%d\n",
  1491. (void *)notify, get, ch->partid, ch->number);
  1492. notify->func(reason, ch->partid, ch->number,
  1493. notify->key);
  1494. dev_dbg(xpc_chan, "notify->func() returned, "
  1495. "notify=0x%p, msg_number=%ld, partid=%d, "
  1496. "channel=%d\n", (void *)notify, get,
  1497. ch->partid, ch->number);
  1498. }
  1499. }
  1500. }
  1501. static void
  1502. xpc_notify_senders_of_disconnect_sn2(struct xpc_channel *ch)
  1503. {
  1504. xpc_notify_senders_sn2(ch, ch->reason, ch->sn.sn2.w_local_GP.put);
  1505. }
  1506. /*
  1507. * Clear some of the msg flags in the local message queue.
  1508. */
  1509. static inline void
  1510. xpc_clear_local_msgqueue_flags_sn2(struct xpc_channel *ch)
  1511. {
  1512. struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
  1513. struct xpc_msg *msg;
  1514. s64 get;
  1515. get = ch_sn2->w_remote_GP.get;
  1516. do {
  1517. msg = (struct xpc_msg *)((u64)ch->local_msgqueue +
  1518. (get % ch->local_nentries) *
  1519. ch->msg_size);
  1520. msg->flags = 0;
  1521. } while (++get < ch_sn2->remote_GP.get);
  1522. }
  1523. /*
  1524. * Clear some of the msg flags in the remote message queue.
  1525. */
  1526. static inline void
  1527. xpc_clear_remote_msgqueue_flags_sn2(struct xpc_channel *ch)
  1528. {
  1529. struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
  1530. struct xpc_msg *msg;
  1531. s64 put;
  1532. put = ch_sn2->w_remote_GP.put;
  1533. do {
  1534. msg = (struct xpc_msg *)((u64)ch->remote_msgqueue +
  1535. (put % ch->remote_nentries) *
  1536. ch->msg_size);
  1537. msg->flags = 0;
  1538. } while (++put < ch_sn2->remote_GP.put);
  1539. }
  1540. static void
  1541. xpc_process_msg_chctl_flags_sn2(struct xpc_partition *part, int ch_number)
  1542. {
  1543. struct xpc_channel *ch = &part->channels[ch_number];
  1544. struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
  1545. int nmsgs_sent;
  1546. ch_sn2->remote_GP = part->sn.sn2.remote_GPs[ch_number];
  1547. /* See what, if anything, has changed for each connected channel */
  1548. xpc_msgqueue_ref(ch);
  1549. if (ch_sn2->w_remote_GP.get == ch_sn2->remote_GP.get &&
  1550. ch_sn2->w_remote_GP.put == ch_sn2->remote_GP.put) {
  1551. /* nothing changed since GPs were last pulled */
  1552. xpc_msgqueue_deref(ch);
  1553. return;
  1554. }
  1555. if (!(ch->flags & XPC_C_CONNECTED)) {
  1556. xpc_msgqueue_deref(ch);
  1557. return;
  1558. }
  1559. /*
  1560. * First check to see if messages recently sent by us have been
  1561. * received by the other side. (The remote GET value will have
  1562. * changed since we last looked at it.)
  1563. */
  1564. if (ch_sn2->w_remote_GP.get != ch_sn2->remote_GP.get) {
  1565. /*
  1566. * We need to notify any senders that want to be notified
  1567. * that their sent messages have been received by their
  1568. * intended recipients. We need to do this before updating
  1569. * w_remote_GP.get so that we don't allocate the same message
  1570. * queue entries prematurely (see xpc_allocate_msg()).
  1571. */
  1572. if (atomic_read(&ch->n_to_notify) > 0) {
  1573. /*
  1574. * Notify senders that messages sent have been
  1575. * received and delivered by the other side.
  1576. */
  1577. xpc_notify_senders_sn2(ch, xpMsgDelivered,
  1578. ch_sn2->remote_GP.get);
  1579. }
  1580. /*
  1581. * Clear msg->flags in previously sent messages, so that
  1582. * they're ready for xpc_allocate_msg().
  1583. */
  1584. xpc_clear_local_msgqueue_flags_sn2(ch);
  1585. ch_sn2->w_remote_GP.get = ch_sn2->remote_GP.get;
  1586. dev_dbg(xpc_chan, "w_remote_GP.get changed to %ld, partid=%d, "
  1587. "channel=%d\n", ch_sn2->w_remote_GP.get, ch->partid,
  1588. ch->number);
  1589. /*
  1590. * If anyone was waiting for message queue entries to become
  1591. * available, wake them up.
  1592. */
  1593. if (atomic_read(&ch->n_on_msg_allocate_wq) > 0)
  1594. wake_up(&ch->msg_allocate_wq);
  1595. }
  1596. /*
  1597. * Now check for newly sent messages by the other side. (The remote
  1598. * PUT value will have changed since we last looked at it.)
  1599. */
  1600. if (ch_sn2->w_remote_GP.put != ch_sn2->remote_GP.put) {
  1601. /*
  1602. * Clear msg->flags in previously received messages, so that
  1603. * they're ready for xpc_get_deliverable_msg().
  1604. */
  1605. xpc_clear_remote_msgqueue_flags_sn2(ch);
  1606. ch_sn2->w_remote_GP.put = ch_sn2->remote_GP.put;
  1607. dev_dbg(xpc_chan, "w_remote_GP.put changed to %ld, partid=%d, "
  1608. "channel=%d\n", ch_sn2->w_remote_GP.put, ch->partid,
  1609. ch->number);
  1610. nmsgs_sent = ch_sn2->w_remote_GP.put - ch_sn2->w_local_GP.get;
  1611. if (nmsgs_sent > 0) {
  1612. dev_dbg(xpc_chan, "msgs waiting to be copied and "
  1613. "delivered=%d, partid=%d, channel=%d\n",
  1614. nmsgs_sent, ch->partid, ch->number);
  1615. if (ch->flags & XPC_C_CONNECTEDCALLOUT_MADE)
  1616. xpc_activate_kthreads(ch, nmsgs_sent);
  1617. }
  1618. }
  1619. xpc_msgqueue_deref(ch);
  1620. }
  1621. static struct xpc_msg *
  1622. xpc_pull_remote_msg_sn2(struct xpc_channel *ch, s64 get)
  1623. {
  1624. struct xpc_partition *part = &xpc_partitions[ch->partid];
  1625. struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
  1626. struct xpc_msg *remote_msg, *msg;
  1627. u32 msg_index, nmsgs;
  1628. u64 msg_offset;
  1629. enum xp_retval ret;
  1630. if (mutex_lock_interruptible(&ch_sn2->msg_to_pull_mutex) != 0) {
  1631. /* we were interrupted by a signal */
  1632. return NULL;
  1633. }
  1634. while (get >= ch_sn2->next_msg_to_pull) {
  1635. /* pull as many messages as are ready and able to be pulled */
  1636. msg_index = ch_sn2->next_msg_to_pull % ch->remote_nentries;
  1637. DBUG_ON(ch_sn2->next_msg_to_pull >= ch_sn2->w_remote_GP.put);
  1638. nmsgs = ch_sn2->w_remote_GP.put - ch_sn2->next_msg_to_pull;
  1639. if (msg_index + nmsgs > ch->remote_nentries) {
  1640. /* ignore the ones that wrap the msg queue for now */
  1641. nmsgs = ch->remote_nentries - msg_index;
  1642. }
  1643. msg_offset = msg_index * ch->msg_size;
  1644. msg = (struct xpc_msg *)((u64)ch->remote_msgqueue + msg_offset);
  1645. remote_msg = (struct xpc_msg *)(ch->remote_msgqueue_pa +
  1646. msg_offset);
  1647. ret = xpc_pull_remote_cachelines_sn2(part, msg, remote_msg,
  1648. nmsgs * ch->msg_size);
  1649. if (ret != xpSuccess) {
  1650. dev_dbg(xpc_chan, "failed to pull %d msgs starting with"
  1651. " msg %ld from partition %d, channel=%d, "
  1652. "ret=%d\n", nmsgs, ch_sn2->next_msg_to_pull,
  1653. ch->partid, ch->number, ret);
  1654. XPC_DEACTIVATE_PARTITION(part, ret);
  1655. mutex_unlock(&ch_sn2->msg_to_pull_mutex);
  1656. return NULL;
  1657. }
  1658. ch_sn2->next_msg_to_pull += nmsgs;
  1659. }
  1660. mutex_unlock(&ch_sn2->msg_to_pull_mutex);
  1661. /* return the message we were looking for */
  1662. msg_offset = (get % ch->remote_nentries) * ch->msg_size;
  1663. msg = (struct xpc_msg *)((u64)ch->remote_msgqueue + msg_offset);
  1664. return msg;
  1665. }
  1666. static int
  1667. xpc_n_of_deliverable_msgs_sn2(struct xpc_channel *ch)
  1668. {
  1669. return ch->sn.sn2.w_remote_GP.put - ch->sn.sn2.w_local_GP.get;
  1670. }
  1671. /*
  1672. * Get a message to be delivered.
  1673. */
  1674. static struct xpc_msg *
  1675. xpc_get_deliverable_msg_sn2(struct xpc_channel *ch)
  1676. {
  1677. struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
  1678. struct xpc_msg *msg = NULL;
  1679. s64 get;
  1680. do {
  1681. if (ch->flags & XPC_C_DISCONNECTING)
  1682. break;
  1683. get = ch_sn2->w_local_GP.get;
  1684. rmb(); /* guarantee that .get loads before .put */
  1685. if (get == ch_sn2->w_remote_GP.put)
  1686. break;
  1687. /* There are messages waiting to be pulled and delivered.
  1688. * We need to try to secure one for ourselves. We'll do this
  1689. * by trying to increment w_local_GP.get and hope that no one
  1690. * else beats us to it. If they do, we'll we'll simply have
  1691. * to try again for the next one.
  1692. */
  1693. if (cmpxchg(&ch_sn2->w_local_GP.get, get, get + 1) == get) {
  1694. /* we got the entry referenced by get */
  1695. dev_dbg(xpc_chan, "w_local_GP.get changed to %ld, "
  1696. "partid=%d, channel=%d\n", get + 1,
  1697. ch->partid, ch->number);
  1698. /* pull the message from the remote partition */
  1699. msg = xpc_pull_remote_msg_sn2(ch, get);
  1700. DBUG_ON(msg != NULL && msg->number != get);
  1701. DBUG_ON(msg != NULL && (msg->flags & XPC_M_DONE));
  1702. DBUG_ON(msg != NULL && !(msg->flags & XPC_M_READY));
  1703. break;
  1704. }
  1705. } while (1);
  1706. return msg;
  1707. }
  1708. /*
  1709. * Now we actually send the messages that are ready to be sent by advancing
  1710. * the local message queue's Put value and then send a chctl msgrequest to the
  1711. * recipient partition.
  1712. */
  1713. static void
  1714. xpc_send_msgs_sn2(struct xpc_channel *ch, s64 initial_put)
  1715. {
  1716. struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
  1717. struct xpc_msg *msg;
  1718. s64 put = initial_put + 1;
  1719. int send_msgrequest = 0;
  1720. while (1) {
  1721. while (1) {
  1722. if (put == ch_sn2->w_local_GP.put)
  1723. break;
  1724. msg = (struct xpc_msg *)((u64)ch->local_msgqueue +
  1725. (put % ch->local_nentries) *
  1726. ch->msg_size);
  1727. if (!(msg->flags & XPC_M_READY))
  1728. break;
  1729. put++;
  1730. }
  1731. if (put == initial_put) {
  1732. /* nothing's changed */
  1733. break;
  1734. }
  1735. if (cmpxchg_rel(&ch_sn2->local_GP->put, initial_put, put) !=
  1736. initial_put) {
  1737. /* someone else beat us to it */
  1738. DBUG_ON(ch_sn2->local_GP->put < initial_put);
  1739. break;
  1740. }
  1741. /* we just set the new value of local_GP->put */
  1742. dev_dbg(xpc_chan, "local_GP->put changed to %ld, partid=%d, "
  1743. "channel=%d\n", put, ch->partid, ch->number);
  1744. send_msgrequest = 1;
  1745. /*
  1746. * We need to ensure that the message referenced by
  1747. * local_GP->put is not XPC_M_READY or that local_GP->put
  1748. * equals w_local_GP.put, so we'll go have a look.
  1749. */
  1750. initial_put = put;
  1751. }
  1752. if (send_msgrequest)
  1753. xpc_send_chctl_msgrequest_sn2(ch);
  1754. }
  1755. /*
  1756. * Allocate an entry for a message from the message queue associated with the
  1757. * specified channel.
  1758. */
  1759. static enum xp_retval
  1760. xpc_allocate_msg_sn2(struct xpc_channel *ch, u32 flags,
  1761. struct xpc_msg **address_of_msg)
  1762. {
  1763. struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
  1764. struct xpc_msg *msg;
  1765. enum xp_retval ret;
  1766. s64 put;
  1767. /*
  1768. * Get the next available message entry from the local message queue.
  1769. * If none are available, we'll make sure that we grab the latest
  1770. * GP values.
  1771. */
  1772. ret = xpTimeout;
  1773. while (1) {
  1774. put = ch_sn2->w_local_GP.put;
  1775. rmb(); /* guarantee that .put loads before .get */
  1776. if (put - ch_sn2->w_remote_GP.get < ch->local_nentries) {
  1777. /* There are available message entries. We need to try
  1778. * to secure one for ourselves. We'll do this by trying
  1779. * to increment w_local_GP.put as long as someone else
  1780. * doesn't beat us to it. If they do, we'll have to
  1781. * try again.
  1782. */
  1783. if (cmpxchg(&ch_sn2->w_local_GP.put, put, put + 1) ==
  1784. put) {
  1785. /* we got the entry referenced by put */
  1786. break;
  1787. }
  1788. continue; /* try again */
  1789. }
  1790. /*
  1791. * There aren't any available msg entries at this time.
  1792. *
  1793. * In waiting for a message entry to become available,
  1794. * we set a timeout in case the other side is not sending
  1795. * completion interrupts. This lets us fake a notify IRQ
  1796. * that will cause the notify IRQ handler to fetch the latest
  1797. * GP values as if an interrupt was sent by the other side.
  1798. */
  1799. if (ret == xpTimeout)
  1800. xpc_send_chctl_local_msgrequest_sn2(ch);
  1801. if (flags & XPC_NOWAIT)
  1802. return xpNoWait;
  1803. ret = xpc_allocate_msg_wait(ch);
  1804. if (ret != xpInterrupted && ret != xpTimeout)
  1805. return ret;
  1806. }
  1807. /* get the message's address and initialize it */
  1808. msg = (struct xpc_msg *)((u64)ch->local_msgqueue +
  1809. (put % ch->local_nentries) * ch->msg_size);
  1810. DBUG_ON(msg->flags != 0);
  1811. msg->number = put;
  1812. dev_dbg(xpc_chan, "w_local_GP.put changed to %ld; msg=0x%p, "
  1813. "msg_number=%ld, partid=%d, channel=%d\n", put + 1,
  1814. (void *)msg, msg->number, ch->partid, ch->number);
  1815. *address_of_msg = msg;
  1816. return xpSuccess;
  1817. }
  1818. /*
  1819. * Common code that does the actual sending of the message by advancing the
  1820. * local message queue's Put value and sends a chctl msgrequest to the
  1821. * partition the message is being sent to.
  1822. */
  1823. static enum xp_retval
  1824. xpc_send_msg_sn2(struct xpc_channel *ch, u32 flags, void *payload,
  1825. u16 payload_size, u8 notify_type, xpc_notify_func func,
  1826. void *key)
  1827. {
  1828. enum xp_retval ret = xpSuccess;
  1829. struct xpc_msg *msg = msg;
  1830. struct xpc_notify *notify = notify;
  1831. s64 msg_number;
  1832. s64 put;
  1833. DBUG_ON(notify_type == XPC_N_CALL && func == NULL);
  1834. if (XPC_MSG_SIZE(payload_size) > ch->msg_size)
  1835. return xpPayloadTooBig;
  1836. xpc_msgqueue_ref(ch);
  1837. if (ch->flags & XPC_C_DISCONNECTING) {
  1838. ret = ch->reason;
  1839. goto out_1;
  1840. }
  1841. if (!(ch->flags & XPC_C_CONNECTED)) {
  1842. ret = xpNotConnected;
  1843. goto out_1;
  1844. }
  1845. ret = xpc_allocate_msg_sn2(ch, flags, &msg);
  1846. if (ret != xpSuccess)
  1847. goto out_1;
  1848. msg_number = msg->number;
  1849. if (notify_type != 0) {
  1850. /*
  1851. * Tell the remote side to send an ACK interrupt when the
  1852. * message has been delivered.
  1853. */
  1854. msg->flags |= XPC_M_INTERRUPT;
  1855. atomic_inc(&ch->n_to_notify);
  1856. notify = &ch->notify_queue[msg_number % ch->local_nentries];
  1857. notify->func = func;
  1858. notify->key = key;
  1859. notify->type = notify_type;
  1860. /* ??? Is a mb() needed here? */
  1861. if (ch->flags & XPC_C_DISCONNECTING) {
  1862. /*
  1863. * An error occurred between our last error check and
  1864. * this one. We will try to clear the type field from
  1865. * the notify entry. If we succeed then
  1866. * xpc_disconnect_channel() didn't already process
  1867. * the notify entry.
  1868. */
  1869. if (cmpxchg(&notify->type, notify_type, 0) ==
  1870. notify_type) {
  1871. atomic_dec(&ch->n_to_notify);
  1872. ret = ch->reason;
  1873. }
  1874. goto out_1;
  1875. }
  1876. }
  1877. memcpy(&msg->payload, payload, payload_size);
  1878. msg->flags |= XPC_M_READY;
  1879. /*
  1880. * The preceding store of msg->flags must occur before the following
  1881. * load of local_GP->put.
  1882. */
  1883. mb();
  1884. /* see if the message is next in line to be sent, if so send it */
  1885. put = ch->sn.sn2.local_GP->put;
  1886. if (put == msg_number)
  1887. xpc_send_msgs_sn2(ch, put);
  1888. out_1:
  1889. xpc_msgqueue_deref(ch);
  1890. return ret;
  1891. }
  1892. /*
  1893. * Now we actually acknowledge the messages that have been delivered and ack'd
  1894. * by advancing the cached remote message queue's Get value and if requested
  1895. * send a chctl msgrequest to the message sender's partition.
  1896. */
  1897. static void
  1898. xpc_acknowledge_msgs_sn2(struct xpc_channel *ch, s64 initial_get, u8 msg_flags)
  1899. {
  1900. struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
  1901. struct xpc_msg *msg;
  1902. s64 get = initial_get + 1;
  1903. int send_msgrequest = 0;
  1904. while (1) {
  1905. while (1) {
  1906. if (get == ch_sn2->w_local_GP.get)
  1907. break;
  1908. msg = (struct xpc_msg *)((u64)ch->remote_msgqueue +
  1909. (get % ch->remote_nentries) *
  1910. ch->msg_size);
  1911. if (!(msg->flags & XPC_M_DONE))
  1912. break;
  1913. msg_flags |= msg->flags;
  1914. get++;
  1915. }
  1916. if (get == initial_get) {
  1917. /* nothing's changed */
  1918. break;
  1919. }
  1920. if (cmpxchg_rel(&ch_sn2->local_GP->get, initial_get, get) !=
  1921. initial_get) {
  1922. /* someone else beat us to it */
  1923. DBUG_ON(ch_sn2->local_GP->get <= initial_get);
  1924. break;
  1925. }
  1926. /* we just set the new value of local_GP->get */
  1927. dev_dbg(xpc_chan, "local_GP->get changed to %ld, partid=%d, "
  1928. "channel=%d\n", get, ch->partid, ch->number);
  1929. send_msgrequest = (msg_flags & XPC_M_INTERRUPT);
  1930. /*
  1931. * We need to ensure that the message referenced by
  1932. * local_GP->get is not XPC_M_DONE or that local_GP->get
  1933. * equals w_local_GP.get, so we'll go have a look.
  1934. */
  1935. initial_get = get;
  1936. }
  1937. if (send_msgrequest)
  1938. xpc_send_chctl_msgrequest_sn2(ch);
  1939. }
  1940. static void
  1941. xpc_received_msg_sn2(struct xpc_channel *ch, struct xpc_msg *msg)
  1942. {
  1943. s64 get;
  1944. s64 msg_number = msg->number;
  1945. dev_dbg(xpc_chan, "msg=0x%p, msg_number=%ld, partid=%d, channel=%d\n",
  1946. (void *)msg, msg_number, ch->partid, ch->number);
  1947. DBUG_ON((((u64)msg - (u64)ch->remote_msgqueue) / ch->msg_size) !=
  1948. msg_number % ch->remote_nentries);
  1949. DBUG_ON(msg->flags & XPC_M_DONE);
  1950. msg->flags |= XPC_M_DONE;
  1951. /*
  1952. * The preceding store of msg->flags must occur before the following
  1953. * load of local_GP->get.
  1954. */
  1955. mb();
  1956. /*
  1957. * See if this message is next in line to be acknowledged as having
  1958. * been delivered.
  1959. */
  1960. get = ch->sn.sn2.local_GP->get;
  1961. if (get == msg_number)
  1962. xpc_acknowledge_msgs_sn2(ch, get, msg->flags);
  1963. }
  1964. int
  1965. xpc_init_sn2(void)
  1966. {
  1967. int ret;
  1968. size_t buf_size;
  1969. xpc_rsvd_page_init = xpc_rsvd_page_init_sn2;
  1970. xpc_increment_heartbeat = xpc_increment_heartbeat_sn2;
  1971. xpc_offline_heartbeat = xpc_offline_heartbeat_sn2;
  1972. xpc_online_heartbeat = xpc_online_heartbeat_sn2;
  1973. xpc_heartbeat_init = xpc_heartbeat_init_sn2;
  1974. xpc_heartbeat_exit = xpc_heartbeat_exit_sn2;
  1975. xpc_check_remote_hb = xpc_check_remote_hb_sn2;
  1976. xpc_request_partition_activation = xpc_request_partition_activation_sn2;
  1977. xpc_request_partition_reactivation =
  1978. xpc_request_partition_reactivation_sn2;
  1979. xpc_request_partition_deactivation =
  1980. xpc_request_partition_deactivation_sn2;
  1981. xpc_cancel_partition_deactivation_request =
  1982. xpc_cancel_partition_deactivation_request_sn2;
  1983. xpc_process_activate_IRQ_rcvd = xpc_process_activate_IRQ_rcvd_sn2;
  1984. xpc_setup_infrastructure = xpc_setup_infrastructure_sn2;
  1985. xpc_teardown_infrastructure = xpc_teardown_infrastructure_sn2;
  1986. xpc_make_first_contact = xpc_make_first_contact_sn2;
  1987. xpc_get_chctl_all_flags = xpc_get_chctl_all_flags_sn2;
  1988. xpc_allocate_msgqueues = xpc_allocate_msgqueues_sn2;
  1989. xpc_free_msgqueues = xpc_free_msgqueues_sn2;
  1990. xpc_notify_senders_of_disconnect = xpc_notify_senders_of_disconnect_sn2;
  1991. xpc_process_msg_chctl_flags = xpc_process_msg_chctl_flags_sn2;
  1992. xpc_n_of_deliverable_msgs = xpc_n_of_deliverable_msgs_sn2;
  1993. xpc_get_deliverable_msg = xpc_get_deliverable_msg_sn2;
  1994. xpc_indicate_partition_engaged = xpc_indicate_partition_engaged_sn2;
  1995. xpc_partition_engaged = xpc_partition_engaged_sn2;
  1996. xpc_any_partition_engaged = xpc_any_partition_engaged_sn2;
  1997. xpc_indicate_partition_disengaged =
  1998. xpc_indicate_partition_disengaged_sn2;
  1999. xpc_assume_partition_disengaged = xpc_assume_partition_disengaged_sn2;
  2000. xpc_send_chctl_closerequest = xpc_send_chctl_closerequest_sn2;
  2001. xpc_send_chctl_closereply = xpc_send_chctl_closereply_sn2;
  2002. xpc_send_chctl_openrequest = xpc_send_chctl_openrequest_sn2;
  2003. xpc_send_chctl_openreply = xpc_send_chctl_openreply_sn2;
  2004. xpc_send_msg = xpc_send_msg_sn2;
  2005. xpc_received_msg = xpc_received_msg_sn2;
  2006. buf_size = max(XPC_RP_VARS_SIZE,
  2007. XPC_RP_HEADER_SIZE + XP_NASID_MASK_BYTES_SN2);
  2008. xpc_remote_copy_buffer_sn2 = xpc_kmalloc_cacheline_aligned(buf_size,
  2009. GFP_KERNEL,
  2010. &xpc_remote_copy_buffer_base_sn2);
  2011. if (xpc_remote_copy_buffer_sn2 == NULL) {
  2012. dev_err(xpc_part, "can't get memory for remote copy buffer\n");
  2013. return -ENOMEM;
  2014. }
  2015. /* open up protections for IPI and [potentially] amo operations */
  2016. xpc_allow_IPI_ops_sn2();
  2017. xpc_allow_amo_ops_shub_wars_1_1_sn2();
  2018. /*
  2019. * This is safe to do before the xpc_hb_checker thread has started
  2020. * because the handler releases a wait queue. If an interrupt is
  2021. * received before the thread is waiting, it will not go to sleep,
  2022. * but rather immediately process the interrupt.
  2023. */
  2024. ret = request_irq(SGI_XPC_ACTIVATE, xpc_handle_activate_IRQ_sn2, 0,
  2025. "xpc hb", NULL);
  2026. if (ret != 0) {
  2027. dev_err(xpc_part, "can't register ACTIVATE IRQ handler, "
  2028. "errno=%d\n", -ret);
  2029. xpc_disallow_IPI_ops_sn2();
  2030. kfree(xpc_remote_copy_buffer_base_sn2);
  2031. }
  2032. return ret;
  2033. }
  2034. void
  2035. xpc_exit_sn2(void)
  2036. {
  2037. free_irq(SGI_XPC_ACTIVATE, NULL);
  2038. xpc_disallow_IPI_ops_sn2();
  2039. kfree(xpc_remote_copy_buffer_base_sn2);
  2040. }