xpc_channel.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311
  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) 2004-2005 Silicon Graphics, Inc. All Rights Reserved.
  7. */
  8. /*
  9. * Cross Partition Communication (XPC) channel support.
  10. *
  11. * This is the part of XPC that manages the channels and
  12. * sends/receives messages across them to/from other partitions.
  13. *
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/init.h>
  17. #include <linux/sched.h>
  18. #include <linux/cache.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/slab.h>
  21. #include <asm/sn/bte.h>
  22. #include <asm/sn/sn_sal.h>
  23. #include "xpc.h"
  24. /*
  25. * Set up the initial values for the XPartition Communication channels.
  26. */
  27. static void
  28. xpc_initialize_channels(struct xpc_partition *part, partid_t partid)
  29. {
  30. int ch_number;
  31. struct xpc_channel *ch;
  32. for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
  33. ch = &part->channels[ch_number];
  34. ch->partid = partid;
  35. ch->number = ch_number;
  36. ch->flags = XPC_C_DISCONNECTED;
  37. ch->local_GP = &part->local_GPs[ch_number];
  38. ch->local_openclose_args =
  39. &part->local_openclose_args[ch_number];
  40. atomic_set(&ch->kthreads_assigned, 0);
  41. atomic_set(&ch->kthreads_idle, 0);
  42. atomic_set(&ch->kthreads_active, 0);
  43. atomic_set(&ch->references, 0);
  44. atomic_set(&ch->n_to_notify, 0);
  45. spin_lock_init(&ch->lock);
  46. sema_init(&ch->msg_to_pull_sema, 1); /* mutex */
  47. sema_init(&ch->wdisconnect_sema, 0); /* event wait */
  48. atomic_set(&ch->n_on_msg_allocate_wq, 0);
  49. init_waitqueue_head(&ch->msg_allocate_wq);
  50. init_waitqueue_head(&ch->idle_wq);
  51. }
  52. }
  53. /*
  54. * Setup the infrastructure necessary to support XPartition Communication
  55. * between the specified remote partition and the local one.
  56. */
  57. enum xpc_retval
  58. xpc_setup_infrastructure(struct xpc_partition *part)
  59. {
  60. int ret, cpuid;
  61. struct timer_list *timer;
  62. partid_t partid = XPC_PARTID(part);
  63. /*
  64. * Zero out MOST of the entry for this partition. Only the fields
  65. * starting with `nchannels' will be zeroed. The preceding fields must
  66. * remain `viable' across partition ups and downs, since they may be
  67. * referenced during this memset() operation.
  68. */
  69. memset(&part->nchannels, 0, sizeof(struct xpc_partition) -
  70. offsetof(struct xpc_partition, nchannels));
  71. /*
  72. * Allocate all of the channel structures as a contiguous chunk of
  73. * memory.
  74. */
  75. part->channels = kmalloc(sizeof(struct xpc_channel) * XPC_NCHANNELS,
  76. GFP_KERNEL);
  77. if (part->channels == NULL) {
  78. dev_err(xpc_chan, "can't get memory for channels\n");
  79. return xpcNoMemory;
  80. }
  81. memset(part->channels, 0, sizeof(struct xpc_channel) * XPC_NCHANNELS);
  82. part->nchannels = XPC_NCHANNELS;
  83. /* allocate all the required GET/PUT values */
  84. part->local_GPs = xpc_kmalloc_cacheline_aligned(XPC_GP_SIZE,
  85. GFP_KERNEL, &part->local_GPs_base);
  86. if (part->local_GPs == NULL) {
  87. kfree(part->channels);
  88. part->channels = NULL;
  89. dev_err(xpc_chan, "can't get memory for local get/put "
  90. "values\n");
  91. return xpcNoMemory;
  92. }
  93. memset(part->local_GPs, 0, XPC_GP_SIZE);
  94. part->remote_GPs = xpc_kmalloc_cacheline_aligned(XPC_GP_SIZE,
  95. GFP_KERNEL, &part->remote_GPs_base);
  96. if (part->remote_GPs == NULL) {
  97. kfree(part->channels);
  98. part->channels = NULL;
  99. kfree(part->local_GPs_base);
  100. part->local_GPs = NULL;
  101. dev_err(xpc_chan, "can't get memory for remote get/put "
  102. "values\n");
  103. return xpcNoMemory;
  104. }
  105. memset(part->remote_GPs, 0, XPC_GP_SIZE);
  106. /* allocate all the required open and close args */
  107. part->local_openclose_args = xpc_kmalloc_cacheline_aligned(
  108. XPC_OPENCLOSE_ARGS_SIZE, GFP_KERNEL,
  109. &part->local_openclose_args_base);
  110. if (part->local_openclose_args == NULL) {
  111. kfree(part->channels);
  112. part->channels = NULL;
  113. kfree(part->local_GPs_base);
  114. part->local_GPs = NULL;
  115. kfree(part->remote_GPs_base);
  116. part->remote_GPs = NULL;
  117. dev_err(xpc_chan, "can't get memory for local connect args\n");
  118. return xpcNoMemory;
  119. }
  120. memset(part->local_openclose_args, 0, XPC_OPENCLOSE_ARGS_SIZE);
  121. part->remote_openclose_args = xpc_kmalloc_cacheline_aligned(
  122. XPC_OPENCLOSE_ARGS_SIZE, GFP_KERNEL,
  123. &part->remote_openclose_args_base);
  124. if (part->remote_openclose_args == NULL) {
  125. kfree(part->channels);
  126. part->channels = NULL;
  127. kfree(part->local_GPs_base);
  128. part->local_GPs = NULL;
  129. kfree(part->remote_GPs_base);
  130. part->remote_GPs = NULL;
  131. kfree(part->local_openclose_args_base);
  132. part->local_openclose_args = NULL;
  133. dev_err(xpc_chan, "can't get memory for remote connect args\n");
  134. return xpcNoMemory;
  135. }
  136. memset(part->remote_openclose_args, 0, XPC_OPENCLOSE_ARGS_SIZE);
  137. xpc_initialize_channels(part, partid);
  138. atomic_set(&part->nchannels_active, 0);
  139. atomic_set(&part->nchannels_engaged, 0);
  140. /* local_IPI_amo were set to 0 by an earlier memset() */
  141. /* Initialize this partitions AMO_t structure */
  142. part->local_IPI_amo_va = xpc_IPI_init(partid);
  143. spin_lock_init(&part->IPI_lock);
  144. atomic_set(&part->channel_mgr_requests, 1);
  145. init_waitqueue_head(&part->channel_mgr_wq);
  146. sprintf(part->IPI_owner, "xpc%02d", partid);
  147. ret = request_irq(SGI_XPC_NOTIFY, xpc_notify_IRQ_handler, SA_SHIRQ,
  148. part->IPI_owner, (void *) (u64) partid);
  149. if (ret != 0) {
  150. kfree(part->channels);
  151. part->channels = NULL;
  152. kfree(part->local_GPs_base);
  153. part->local_GPs = NULL;
  154. kfree(part->remote_GPs_base);
  155. part->remote_GPs = NULL;
  156. kfree(part->local_openclose_args_base);
  157. part->local_openclose_args = NULL;
  158. kfree(part->remote_openclose_args_base);
  159. part->remote_openclose_args = NULL;
  160. dev_err(xpc_chan, "can't register NOTIFY IRQ handler, "
  161. "errno=%d\n", -ret);
  162. return xpcLackOfResources;
  163. }
  164. /* Setup a timer to check for dropped IPIs */
  165. timer = &part->dropped_IPI_timer;
  166. init_timer(timer);
  167. timer->function = (void (*)(unsigned long)) xpc_dropped_IPI_check;
  168. timer->data = (unsigned long) part;
  169. timer->expires = jiffies + XPC_P_DROPPED_IPI_WAIT;
  170. add_timer(timer);
  171. /*
  172. * With the setting of the partition setup_state to XPC_P_SETUP, we're
  173. * declaring that this partition is ready to go.
  174. */
  175. part->setup_state = XPC_P_SETUP;
  176. /*
  177. * Setup the per partition specific variables required by the
  178. * remote partition to establish channel connections with us.
  179. *
  180. * The setting of the magic # indicates that these per partition
  181. * specific variables are ready to be used.
  182. */
  183. xpc_vars_part[partid].GPs_pa = __pa(part->local_GPs);
  184. xpc_vars_part[partid].openclose_args_pa =
  185. __pa(part->local_openclose_args);
  186. xpc_vars_part[partid].IPI_amo_pa = __pa(part->local_IPI_amo_va);
  187. cpuid = raw_smp_processor_id(); /* any CPU in this partition will do */
  188. xpc_vars_part[partid].IPI_nasid = cpuid_to_nasid(cpuid);
  189. xpc_vars_part[partid].IPI_phys_cpuid = cpu_physical_id(cpuid);
  190. xpc_vars_part[partid].nchannels = part->nchannels;
  191. xpc_vars_part[partid].magic = XPC_VP_MAGIC1;
  192. return xpcSuccess;
  193. }
  194. /*
  195. * Create a wrapper that hides the underlying mechanism for pulling a cacheline
  196. * (or multiple cachelines) from a remote partition.
  197. *
  198. * src must be a cacheline aligned physical address on the remote partition.
  199. * dst must be a cacheline aligned virtual address on this partition.
  200. * cnt must be an cacheline sized
  201. */
  202. static enum xpc_retval
  203. xpc_pull_remote_cachelines(struct xpc_partition *part, void *dst,
  204. const void *src, size_t cnt)
  205. {
  206. bte_result_t bte_ret;
  207. DBUG_ON((u64) src != L1_CACHE_ALIGN((u64) src));
  208. DBUG_ON((u64) dst != L1_CACHE_ALIGN((u64) dst));
  209. DBUG_ON(cnt != L1_CACHE_ALIGN(cnt));
  210. if (part->act_state == XPC_P_DEACTIVATING) {
  211. return part->reason;
  212. }
  213. bte_ret = xp_bte_copy((u64) src, (u64) ia64_tpa((u64) dst),
  214. (u64) cnt, (BTE_NORMAL | BTE_WACQUIRE), NULL);
  215. if (bte_ret == BTE_SUCCESS) {
  216. return xpcSuccess;
  217. }
  218. dev_dbg(xpc_chan, "xp_bte_copy() from partition %d failed, ret=%d\n",
  219. XPC_PARTID(part), bte_ret);
  220. return xpc_map_bte_errors(bte_ret);
  221. }
  222. /*
  223. * Pull the remote per partititon specific variables from the specified
  224. * partition.
  225. */
  226. enum xpc_retval
  227. xpc_pull_remote_vars_part(struct xpc_partition *part)
  228. {
  229. u8 buffer[L1_CACHE_BYTES * 2];
  230. struct xpc_vars_part *pulled_entry_cacheline =
  231. (struct xpc_vars_part *) L1_CACHE_ALIGN((u64) buffer);
  232. struct xpc_vars_part *pulled_entry;
  233. u64 remote_entry_cacheline_pa, remote_entry_pa;
  234. partid_t partid = XPC_PARTID(part);
  235. enum xpc_retval ret;
  236. /* pull the cacheline that contains the variables we're interested in */
  237. DBUG_ON(part->remote_vars_part_pa !=
  238. L1_CACHE_ALIGN(part->remote_vars_part_pa));
  239. DBUG_ON(sizeof(struct xpc_vars_part) != L1_CACHE_BYTES / 2);
  240. remote_entry_pa = part->remote_vars_part_pa +
  241. sn_partition_id * sizeof(struct xpc_vars_part);
  242. remote_entry_cacheline_pa = (remote_entry_pa & ~(L1_CACHE_BYTES - 1));
  243. pulled_entry = (struct xpc_vars_part *) ((u64) pulled_entry_cacheline +
  244. (remote_entry_pa & (L1_CACHE_BYTES - 1)));
  245. ret = xpc_pull_remote_cachelines(part, pulled_entry_cacheline,
  246. (void *) remote_entry_cacheline_pa,
  247. L1_CACHE_BYTES);
  248. if (ret != xpcSuccess) {
  249. dev_dbg(xpc_chan, "failed to pull XPC vars_part from "
  250. "partition %d, ret=%d\n", partid, ret);
  251. return ret;
  252. }
  253. /* see if they've been set up yet */
  254. if (pulled_entry->magic != XPC_VP_MAGIC1 &&
  255. pulled_entry->magic != XPC_VP_MAGIC2) {
  256. if (pulled_entry->magic != 0) {
  257. dev_dbg(xpc_chan, "partition %d's XPC vars_part for "
  258. "partition %d has bad magic value (=0x%lx)\n",
  259. partid, sn_partition_id, pulled_entry->magic);
  260. return xpcBadMagic;
  261. }
  262. /* they've not been initialized yet */
  263. return xpcRetry;
  264. }
  265. if (xpc_vars_part[partid].magic == XPC_VP_MAGIC1) {
  266. /* validate the variables */
  267. if (pulled_entry->GPs_pa == 0 ||
  268. pulled_entry->openclose_args_pa == 0 ||
  269. pulled_entry->IPI_amo_pa == 0) {
  270. dev_err(xpc_chan, "partition %d's XPC vars_part for "
  271. "partition %d are not valid\n", partid,
  272. sn_partition_id);
  273. return xpcInvalidAddress;
  274. }
  275. /* the variables we imported look to be valid */
  276. part->remote_GPs_pa = pulled_entry->GPs_pa;
  277. part->remote_openclose_args_pa =
  278. pulled_entry->openclose_args_pa;
  279. part->remote_IPI_amo_va =
  280. (AMO_t *) __va(pulled_entry->IPI_amo_pa);
  281. part->remote_IPI_nasid = pulled_entry->IPI_nasid;
  282. part->remote_IPI_phys_cpuid = pulled_entry->IPI_phys_cpuid;
  283. if (part->nchannels > pulled_entry->nchannels) {
  284. part->nchannels = pulled_entry->nchannels;
  285. }
  286. /* let the other side know that we've pulled their variables */
  287. xpc_vars_part[partid].magic = XPC_VP_MAGIC2;
  288. }
  289. if (pulled_entry->magic == XPC_VP_MAGIC1) {
  290. return xpcRetry;
  291. }
  292. return xpcSuccess;
  293. }
  294. /*
  295. * Get the IPI flags and pull the openclose args and/or remote GPs as needed.
  296. */
  297. static u64
  298. xpc_get_IPI_flags(struct xpc_partition *part)
  299. {
  300. unsigned long irq_flags;
  301. u64 IPI_amo;
  302. enum xpc_retval ret;
  303. /*
  304. * See if there are any IPI flags to be handled.
  305. */
  306. spin_lock_irqsave(&part->IPI_lock, irq_flags);
  307. if ((IPI_amo = part->local_IPI_amo) != 0) {
  308. part->local_IPI_amo = 0;
  309. }
  310. spin_unlock_irqrestore(&part->IPI_lock, irq_flags);
  311. if (XPC_ANY_OPENCLOSE_IPI_FLAGS_SET(IPI_amo)) {
  312. ret = xpc_pull_remote_cachelines(part,
  313. part->remote_openclose_args,
  314. (void *) part->remote_openclose_args_pa,
  315. XPC_OPENCLOSE_ARGS_SIZE);
  316. if (ret != xpcSuccess) {
  317. XPC_DEACTIVATE_PARTITION(part, ret);
  318. dev_dbg(xpc_chan, "failed to pull openclose args from "
  319. "partition %d, ret=%d\n", XPC_PARTID(part),
  320. ret);
  321. /* don't bother processing IPIs anymore */
  322. IPI_amo = 0;
  323. }
  324. }
  325. if (XPC_ANY_MSG_IPI_FLAGS_SET(IPI_amo)) {
  326. ret = xpc_pull_remote_cachelines(part, part->remote_GPs,
  327. (void *) part->remote_GPs_pa,
  328. XPC_GP_SIZE);
  329. if (ret != xpcSuccess) {
  330. XPC_DEACTIVATE_PARTITION(part, ret);
  331. dev_dbg(xpc_chan, "failed to pull GPs from partition "
  332. "%d, ret=%d\n", XPC_PARTID(part), ret);
  333. /* don't bother processing IPIs anymore */
  334. IPI_amo = 0;
  335. }
  336. }
  337. return IPI_amo;
  338. }
  339. /*
  340. * Allocate the local message queue and the notify queue.
  341. */
  342. static enum xpc_retval
  343. xpc_allocate_local_msgqueue(struct xpc_channel *ch)
  344. {
  345. unsigned long irq_flags;
  346. int nentries;
  347. size_t nbytes;
  348. // >>> may want to check for ch->flags & XPC_C_DISCONNECTING between
  349. // >>> iterations of the for-loop, bail if set?
  350. // >>> should we impose a minumum #of entries? like 4 or 8?
  351. for (nentries = ch->local_nentries; nentries > 0; nentries--) {
  352. nbytes = nentries * ch->msg_size;
  353. ch->local_msgqueue = xpc_kmalloc_cacheline_aligned(nbytes,
  354. (GFP_KERNEL | GFP_DMA),
  355. &ch->local_msgqueue_base);
  356. if (ch->local_msgqueue == NULL) {
  357. continue;
  358. }
  359. memset(ch->local_msgqueue, 0, nbytes);
  360. nbytes = nentries * sizeof(struct xpc_notify);
  361. ch->notify_queue = kmalloc(nbytes, (GFP_KERNEL | GFP_DMA));
  362. if (ch->notify_queue == NULL) {
  363. kfree(ch->local_msgqueue_base);
  364. ch->local_msgqueue = NULL;
  365. continue;
  366. }
  367. memset(ch->notify_queue, 0, nbytes);
  368. spin_lock_irqsave(&ch->lock, irq_flags);
  369. if (nentries < ch->local_nentries) {
  370. dev_dbg(xpc_chan, "nentries=%d local_nentries=%d, "
  371. "partid=%d, channel=%d\n", nentries,
  372. ch->local_nentries, ch->partid, ch->number);
  373. ch->local_nentries = nentries;
  374. }
  375. spin_unlock_irqrestore(&ch->lock, irq_flags);
  376. return xpcSuccess;
  377. }
  378. dev_dbg(xpc_chan, "can't get memory for local message queue and notify "
  379. "queue, partid=%d, channel=%d\n", ch->partid, ch->number);
  380. return xpcNoMemory;
  381. }
  382. /*
  383. * Allocate the cached remote message queue.
  384. */
  385. static enum xpc_retval
  386. xpc_allocate_remote_msgqueue(struct xpc_channel *ch)
  387. {
  388. unsigned long irq_flags;
  389. int nentries;
  390. size_t nbytes;
  391. DBUG_ON(ch->remote_nentries <= 0);
  392. // >>> may want to check for ch->flags & XPC_C_DISCONNECTING between
  393. // >>> iterations of the for-loop, bail if set?
  394. // >>> should we impose a minumum #of entries? like 4 or 8?
  395. for (nentries = ch->remote_nentries; nentries > 0; nentries--) {
  396. nbytes = nentries * ch->msg_size;
  397. ch->remote_msgqueue = xpc_kmalloc_cacheline_aligned(nbytes,
  398. (GFP_KERNEL | GFP_DMA),
  399. &ch->remote_msgqueue_base);
  400. if (ch->remote_msgqueue == NULL) {
  401. continue;
  402. }
  403. memset(ch->remote_msgqueue, 0, nbytes);
  404. spin_lock_irqsave(&ch->lock, irq_flags);
  405. if (nentries < ch->remote_nentries) {
  406. dev_dbg(xpc_chan, "nentries=%d remote_nentries=%d, "
  407. "partid=%d, channel=%d\n", nentries,
  408. ch->remote_nentries, ch->partid, ch->number);
  409. ch->remote_nentries = nentries;
  410. }
  411. spin_unlock_irqrestore(&ch->lock, irq_flags);
  412. return xpcSuccess;
  413. }
  414. dev_dbg(xpc_chan, "can't get memory for cached remote message queue, "
  415. "partid=%d, channel=%d\n", ch->partid, ch->number);
  416. return xpcNoMemory;
  417. }
  418. /*
  419. * Allocate message queues and other stuff associated with a channel.
  420. *
  421. * Note: Assumes all of the channel sizes are filled in.
  422. */
  423. static enum xpc_retval
  424. xpc_allocate_msgqueues(struct xpc_channel *ch)
  425. {
  426. unsigned long irq_flags;
  427. int i;
  428. enum xpc_retval ret;
  429. DBUG_ON(ch->flags & XPC_C_SETUP);
  430. if ((ret = xpc_allocate_local_msgqueue(ch)) != xpcSuccess) {
  431. return ret;
  432. }
  433. if ((ret = xpc_allocate_remote_msgqueue(ch)) != xpcSuccess) {
  434. kfree(ch->local_msgqueue_base);
  435. ch->local_msgqueue = NULL;
  436. kfree(ch->notify_queue);
  437. ch->notify_queue = NULL;
  438. return ret;
  439. }
  440. for (i = 0; i < ch->local_nentries; i++) {
  441. /* use a semaphore as an event wait queue */
  442. sema_init(&ch->notify_queue[i].sema, 0);
  443. }
  444. spin_lock_irqsave(&ch->lock, irq_flags);
  445. ch->flags |= XPC_C_SETUP;
  446. spin_unlock_irqrestore(&ch->lock, irq_flags);
  447. return xpcSuccess;
  448. }
  449. /*
  450. * Process a connect message from a remote partition.
  451. *
  452. * Note: xpc_process_connect() is expecting to be called with the
  453. * spin_lock_irqsave held and will leave it locked upon return.
  454. */
  455. static void
  456. xpc_process_connect(struct xpc_channel *ch, unsigned long *irq_flags)
  457. {
  458. enum xpc_retval ret;
  459. DBUG_ON(!spin_is_locked(&ch->lock));
  460. if (!(ch->flags & XPC_C_OPENREQUEST) ||
  461. !(ch->flags & XPC_C_ROPENREQUEST)) {
  462. /* nothing more to do for now */
  463. return;
  464. }
  465. DBUG_ON(!(ch->flags & XPC_C_CONNECTING));
  466. if (!(ch->flags & XPC_C_SETUP)) {
  467. spin_unlock_irqrestore(&ch->lock, *irq_flags);
  468. ret = xpc_allocate_msgqueues(ch);
  469. spin_lock_irqsave(&ch->lock, *irq_flags);
  470. if (ret != xpcSuccess) {
  471. XPC_DISCONNECT_CHANNEL(ch, ret, irq_flags);
  472. }
  473. if (ch->flags & (XPC_C_CONNECTED | XPC_C_DISCONNECTING)) {
  474. return;
  475. }
  476. DBUG_ON(!(ch->flags & XPC_C_SETUP));
  477. DBUG_ON(ch->local_msgqueue == NULL);
  478. DBUG_ON(ch->remote_msgqueue == NULL);
  479. }
  480. if (!(ch->flags & XPC_C_OPENREPLY)) {
  481. ch->flags |= XPC_C_OPENREPLY;
  482. xpc_IPI_send_openreply(ch, irq_flags);
  483. }
  484. if (!(ch->flags & XPC_C_ROPENREPLY)) {
  485. return;
  486. }
  487. DBUG_ON(ch->remote_msgqueue_pa == 0);
  488. ch->flags = (XPC_C_CONNECTED | XPC_C_SETUP); /* clear all else */
  489. dev_info(xpc_chan, "channel %d to partition %d connected\n",
  490. ch->number, ch->partid);
  491. spin_unlock_irqrestore(&ch->lock, *irq_flags);
  492. xpc_create_kthreads(ch, 1);
  493. spin_lock_irqsave(&ch->lock, *irq_flags);
  494. }
  495. /*
  496. * Notify those who wanted to be notified upon delivery of their message.
  497. */
  498. static void
  499. xpc_notify_senders(struct xpc_channel *ch, enum xpc_retval reason, s64 put)
  500. {
  501. struct xpc_notify *notify;
  502. u8 notify_type;
  503. s64 get = ch->w_remote_GP.get - 1;
  504. while (++get < put && atomic_read(&ch->n_to_notify) > 0) {
  505. notify = &ch->notify_queue[get % ch->local_nentries];
  506. /*
  507. * See if the notify entry indicates it was associated with
  508. * a message who's sender wants to be notified. It is possible
  509. * that it is, but someone else is doing or has done the
  510. * notification.
  511. */
  512. notify_type = notify->type;
  513. if (notify_type == 0 ||
  514. cmpxchg(&notify->type, notify_type, 0) !=
  515. notify_type) {
  516. continue;
  517. }
  518. DBUG_ON(notify_type != XPC_N_CALL);
  519. atomic_dec(&ch->n_to_notify);
  520. if (notify->func != NULL) {
  521. dev_dbg(xpc_chan, "notify->func() called, notify=0x%p, "
  522. "msg_number=%ld, partid=%d, channel=%d\n",
  523. (void *) notify, get, ch->partid, ch->number);
  524. notify->func(reason, ch->partid, ch->number,
  525. notify->key);
  526. dev_dbg(xpc_chan, "notify->func() returned, "
  527. "notify=0x%p, msg_number=%ld, partid=%d, "
  528. "channel=%d\n", (void *) notify, get,
  529. ch->partid, ch->number);
  530. }
  531. }
  532. }
  533. /*
  534. * Free up message queues and other stuff that were allocated for the specified
  535. * channel.
  536. *
  537. * Note: ch->reason and ch->reason_line are left set for debugging purposes,
  538. * they're cleared when XPC_C_DISCONNECTED is cleared.
  539. */
  540. static void
  541. xpc_free_msgqueues(struct xpc_channel *ch)
  542. {
  543. DBUG_ON(!spin_is_locked(&ch->lock));
  544. DBUG_ON(atomic_read(&ch->n_to_notify) != 0);
  545. ch->remote_msgqueue_pa = 0;
  546. ch->func = NULL;
  547. ch->key = NULL;
  548. ch->msg_size = 0;
  549. ch->local_nentries = 0;
  550. ch->remote_nentries = 0;
  551. ch->kthreads_assigned_limit = 0;
  552. ch->kthreads_idle_limit = 0;
  553. ch->local_GP->get = 0;
  554. ch->local_GP->put = 0;
  555. ch->remote_GP.get = 0;
  556. ch->remote_GP.put = 0;
  557. ch->w_local_GP.get = 0;
  558. ch->w_local_GP.put = 0;
  559. ch->w_remote_GP.get = 0;
  560. ch->w_remote_GP.put = 0;
  561. ch->next_msg_to_pull = 0;
  562. if (ch->flags & XPC_C_SETUP) {
  563. ch->flags &= ~XPC_C_SETUP;
  564. dev_dbg(xpc_chan, "ch->flags=0x%x, partid=%d, channel=%d\n",
  565. ch->flags, ch->partid, ch->number);
  566. kfree(ch->local_msgqueue_base);
  567. ch->local_msgqueue = NULL;
  568. kfree(ch->remote_msgqueue_base);
  569. ch->remote_msgqueue = NULL;
  570. kfree(ch->notify_queue);
  571. ch->notify_queue = NULL;
  572. }
  573. }
  574. /*
  575. * spin_lock_irqsave() is expected to be held on entry.
  576. */
  577. static void
  578. xpc_process_disconnect(struct xpc_channel *ch, unsigned long *irq_flags)
  579. {
  580. struct xpc_partition *part = &xpc_partitions[ch->partid];
  581. u32 channel_was_connected = (ch->flags & XPC_C_WASCONNECTED);
  582. DBUG_ON(!spin_is_locked(&ch->lock));
  583. if (!(ch->flags & XPC_C_DISCONNECTING)) {
  584. return;
  585. }
  586. DBUG_ON(!(ch->flags & XPC_C_CLOSEREQUEST));
  587. /* make sure all activity has settled down first */
  588. if (atomic_read(&ch->references) > 0) {
  589. return;
  590. }
  591. DBUG_ON(atomic_read(&ch->kthreads_assigned) != 0);
  592. if (part->act_state == XPC_P_DEACTIVATING) {
  593. /* can't proceed until the other side disengages from us */
  594. if (xpc_partition_engaged(1UL << ch->partid)) {
  595. return;
  596. }
  597. } else {
  598. /* as long as the other side is up do the full protocol */
  599. if (!(ch->flags & XPC_C_RCLOSEREQUEST)) {
  600. return;
  601. }
  602. if (!(ch->flags & XPC_C_CLOSEREPLY)) {
  603. ch->flags |= XPC_C_CLOSEREPLY;
  604. xpc_IPI_send_closereply(ch, irq_flags);
  605. }
  606. if (!(ch->flags & XPC_C_RCLOSEREPLY)) {
  607. return;
  608. }
  609. }
  610. /* wake those waiting for notify completion */
  611. if (atomic_read(&ch->n_to_notify) > 0) {
  612. /* >>> we do callout while holding ch->lock */
  613. xpc_notify_senders(ch, ch->reason, ch->w_local_GP.put);
  614. }
  615. /* both sides are disconnected now */
  616. /* it's now safe to free the channel's message queues */
  617. xpc_free_msgqueues(ch);
  618. /* mark disconnected, clear all other flags except XPC_C_WDISCONNECT */
  619. ch->flags = (XPC_C_DISCONNECTED | (ch->flags & XPC_C_WDISCONNECT));
  620. atomic_dec(&part->nchannels_active);
  621. if (channel_was_connected) {
  622. dev_info(xpc_chan, "channel %d to partition %d disconnected, "
  623. "reason=%d\n", ch->number, ch->partid, ch->reason);
  624. }
  625. /* wake the thread that is waiting for this channel to disconnect */
  626. if (ch->flags & XPC_C_WDISCONNECT) {
  627. spin_unlock_irqrestore(&ch->lock, *irq_flags);
  628. up(&ch->wdisconnect_sema);
  629. spin_lock_irqsave(&ch->lock, *irq_flags);
  630. }
  631. }
  632. /*
  633. * Process a change in the channel's remote connection state.
  634. */
  635. static void
  636. xpc_process_openclose_IPI(struct xpc_partition *part, int ch_number,
  637. u8 IPI_flags)
  638. {
  639. unsigned long irq_flags;
  640. struct xpc_openclose_args *args =
  641. &part->remote_openclose_args[ch_number];
  642. struct xpc_channel *ch = &part->channels[ch_number];
  643. enum xpc_retval reason;
  644. spin_lock_irqsave(&ch->lock, irq_flags);
  645. if (IPI_flags & XPC_IPI_CLOSEREQUEST) {
  646. dev_dbg(xpc_chan, "XPC_IPI_CLOSEREQUEST (reason=%d) received "
  647. "from partid=%d, channel=%d\n", args->reason,
  648. ch->partid, ch->number);
  649. /*
  650. * If RCLOSEREQUEST is set, we're probably waiting for
  651. * RCLOSEREPLY. We should find it and a ROPENREQUEST packed
  652. * with this RCLOSEREQUEST in the IPI_flags.
  653. */
  654. if (ch->flags & XPC_C_RCLOSEREQUEST) {
  655. DBUG_ON(!(ch->flags & XPC_C_DISCONNECTING));
  656. DBUG_ON(!(ch->flags & XPC_C_CLOSEREQUEST));
  657. DBUG_ON(!(ch->flags & XPC_C_CLOSEREPLY));
  658. DBUG_ON(ch->flags & XPC_C_RCLOSEREPLY);
  659. DBUG_ON(!(IPI_flags & XPC_IPI_CLOSEREPLY));
  660. IPI_flags &= ~XPC_IPI_CLOSEREPLY;
  661. ch->flags |= XPC_C_RCLOSEREPLY;
  662. /* both sides have finished disconnecting */
  663. xpc_process_disconnect(ch, &irq_flags);
  664. }
  665. if (ch->flags & XPC_C_DISCONNECTED) {
  666. // >>> explain this section
  667. if (!(IPI_flags & XPC_IPI_OPENREQUEST)) {
  668. DBUG_ON(part->act_state !=
  669. XPC_P_DEACTIVATING);
  670. spin_unlock_irqrestore(&ch->lock, irq_flags);
  671. return;
  672. }
  673. XPC_SET_REASON(ch, 0, 0);
  674. ch->flags &= ~XPC_C_DISCONNECTED;
  675. atomic_inc(&part->nchannels_active);
  676. ch->flags |= (XPC_C_CONNECTING | XPC_C_ROPENREQUEST);
  677. }
  678. IPI_flags &= ~(XPC_IPI_OPENREQUEST | XPC_IPI_OPENREPLY);
  679. /*
  680. * The meaningful CLOSEREQUEST connection state fields are:
  681. * reason = reason connection is to be closed
  682. */
  683. ch->flags |= XPC_C_RCLOSEREQUEST;
  684. if (!(ch->flags & XPC_C_DISCONNECTING)) {
  685. reason = args->reason;
  686. if (reason <= xpcSuccess || reason > xpcUnknownReason) {
  687. reason = xpcUnknownReason;
  688. } else if (reason == xpcUnregistering) {
  689. reason = xpcOtherUnregistering;
  690. }
  691. XPC_DISCONNECT_CHANNEL(ch, reason, &irq_flags);
  692. } else {
  693. xpc_process_disconnect(ch, &irq_flags);
  694. }
  695. }
  696. if (IPI_flags & XPC_IPI_CLOSEREPLY) {
  697. dev_dbg(xpc_chan, "XPC_IPI_CLOSEREPLY received from partid=%d,"
  698. " channel=%d\n", ch->partid, ch->number);
  699. if (ch->flags & XPC_C_DISCONNECTED) {
  700. DBUG_ON(part->act_state != XPC_P_DEACTIVATING);
  701. spin_unlock_irqrestore(&ch->lock, irq_flags);
  702. return;
  703. }
  704. DBUG_ON(!(ch->flags & XPC_C_CLOSEREQUEST));
  705. DBUG_ON(!(ch->flags & XPC_C_RCLOSEREQUEST));
  706. ch->flags |= XPC_C_RCLOSEREPLY;
  707. if (ch->flags & XPC_C_CLOSEREPLY) {
  708. /* both sides have finished disconnecting */
  709. xpc_process_disconnect(ch, &irq_flags);
  710. }
  711. }
  712. if (IPI_flags & XPC_IPI_OPENREQUEST) {
  713. dev_dbg(xpc_chan, "XPC_IPI_OPENREQUEST (msg_size=%d, "
  714. "local_nentries=%d) received from partid=%d, "
  715. "channel=%d\n", args->msg_size, args->local_nentries,
  716. ch->partid, ch->number);
  717. if ((ch->flags & (XPC_C_DISCONNECTING | XPC_C_WDISCONNECT)) ||
  718. part->act_state == XPC_P_DEACTIVATING) {
  719. spin_unlock_irqrestore(&ch->lock, irq_flags);
  720. return;
  721. }
  722. DBUG_ON(!(ch->flags & (XPC_C_DISCONNECTED |
  723. XPC_C_OPENREQUEST)));
  724. DBUG_ON(ch->flags & (XPC_C_ROPENREQUEST | XPC_C_ROPENREPLY |
  725. XPC_C_OPENREPLY | XPC_C_CONNECTED));
  726. /*
  727. * The meaningful OPENREQUEST connection state fields are:
  728. * msg_size = size of channel's messages in bytes
  729. * local_nentries = remote partition's local_nentries
  730. */
  731. DBUG_ON(args->msg_size == 0);
  732. DBUG_ON(args->local_nentries == 0);
  733. ch->flags |= (XPC_C_ROPENREQUEST | XPC_C_CONNECTING);
  734. ch->remote_nentries = args->local_nentries;
  735. if (ch->flags & XPC_C_OPENREQUEST) {
  736. if (args->msg_size != ch->msg_size) {
  737. XPC_DISCONNECT_CHANNEL(ch, xpcUnequalMsgSizes,
  738. &irq_flags);
  739. spin_unlock_irqrestore(&ch->lock, irq_flags);
  740. return;
  741. }
  742. } else {
  743. ch->msg_size = args->msg_size;
  744. XPC_SET_REASON(ch, 0, 0);
  745. ch->flags &= ~XPC_C_DISCONNECTED;
  746. atomic_inc(&part->nchannels_active);
  747. }
  748. xpc_process_connect(ch, &irq_flags);
  749. }
  750. if (IPI_flags & XPC_IPI_OPENREPLY) {
  751. dev_dbg(xpc_chan, "XPC_IPI_OPENREPLY (local_msgqueue_pa=0x%lx, "
  752. "local_nentries=%d, remote_nentries=%d) received from "
  753. "partid=%d, channel=%d\n", args->local_msgqueue_pa,
  754. args->local_nentries, args->remote_nentries,
  755. ch->partid, ch->number);
  756. if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_DISCONNECTED)) {
  757. spin_unlock_irqrestore(&ch->lock, irq_flags);
  758. return;
  759. }
  760. DBUG_ON(!(ch->flags & XPC_C_OPENREQUEST));
  761. DBUG_ON(!(ch->flags & XPC_C_ROPENREQUEST));
  762. DBUG_ON(ch->flags & XPC_C_CONNECTED);
  763. /*
  764. * The meaningful OPENREPLY connection state fields are:
  765. * local_msgqueue_pa = physical address of remote
  766. * partition's local_msgqueue
  767. * local_nentries = remote partition's local_nentries
  768. * remote_nentries = remote partition's remote_nentries
  769. */
  770. DBUG_ON(args->local_msgqueue_pa == 0);
  771. DBUG_ON(args->local_nentries == 0);
  772. DBUG_ON(args->remote_nentries == 0);
  773. ch->flags |= XPC_C_ROPENREPLY;
  774. ch->remote_msgqueue_pa = args->local_msgqueue_pa;
  775. if (args->local_nentries < ch->remote_nentries) {
  776. dev_dbg(xpc_chan, "XPC_IPI_OPENREPLY: new "
  777. "remote_nentries=%d, old remote_nentries=%d, "
  778. "partid=%d, channel=%d\n",
  779. args->local_nentries, ch->remote_nentries,
  780. ch->partid, ch->number);
  781. ch->remote_nentries = args->local_nentries;
  782. }
  783. if (args->remote_nentries < ch->local_nentries) {
  784. dev_dbg(xpc_chan, "XPC_IPI_OPENREPLY: new "
  785. "local_nentries=%d, old local_nentries=%d, "
  786. "partid=%d, channel=%d\n",
  787. args->remote_nentries, ch->local_nentries,
  788. ch->partid, ch->number);
  789. ch->local_nentries = args->remote_nentries;
  790. }
  791. xpc_process_connect(ch, &irq_flags);
  792. }
  793. spin_unlock_irqrestore(&ch->lock, irq_flags);
  794. }
  795. /*
  796. * Attempt to establish a channel connection to a remote partition.
  797. */
  798. static enum xpc_retval
  799. xpc_connect_channel(struct xpc_channel *ch)
  800. {
  801. unsigned long irq_flags;
  802. struct xpc_registration *registration = &xpc_registrations[ch->number];
  803. if (down_interruptible(&registration->sema) != 0) {
  804. return xpcInterrupted;
  805. }
  806. if (!XPC_CHANNEL_REGISTERED(ch->number)) {
  807. up(&registration->sema);
  808. return xpcUnregistered;
  809. }
  810. spin_lock_irqsave(&ch->lock, irq_flags);
  811. DBUG_ON(ch->flags & XPC_C_CONNECTED);
  812. DBUG_ON(ch->flags & XPC_C_OPENREQUEST);
  813. if (ch->flags & XPC_C_DISCONNECTING) {
  814. spin_unlock_irqrestore(&ch->lock, irq_flags);
  815. up(&registration->sema);
  816. return ch->reason;
  817. }
  818. /* add info from the channel connect registration to the channel */
  819. ch->kthreads_assigned_limit = registration->assigned_limit;
  820. ch->kthreads_idle_limit = registration->idle_limit;
  821. DBUG_ON(atomic_read(&ch->kthreads_assigned) != 0);
  822. DBUG_ON(atomic_read(&ch->kthreads_idle) != 0);
  823. DBUG_ON(atomic_read(&ch->kthreads_active) != 0);
  824. ch->func = registration->func;
  825. DBUG_ON(registration->func == NULL);
  826. ch->key = registration->key;
  827. ch->local_nentries = registration->nentries;
  828. if (ch->flags & XPC_C_ROPENREQUEST) {
  829. if (registration->msg_size != ch->msg_size) {
  830. /* the local and remote sides aren't the same */
  831. /*
  832. * Because XPC_DISCONNECT_CHANNEL() can block we're
  833. * forced to up the registration sema before we unlock
  834. * the channel lock. But that's okay here because we're
  835. * done with the part that required the registration
  836. * sema. XPC_DISCONNECT_CHANNEL() requires that the
  837. * channel lock be locked and will unlock and relock
  838. * the channel lock as needed.
  839. */
  840. up(&registration->sema);
  841. XPC_DISCONNECT_CHANNEL(ch, xpcUnequalMsgSizes,
  842. &irq_flags);
  843. spin_unlock_irqrestore(&ch->lock, irq_flags);
  844. return xpcUnequalMsgSizes;
  845. }
  846. } else {
  847. ch->msg_size = registration->msg_size;
  848. XPC_SET_REASON(ch, 0, 0);
  849. ch->flags &= ~XPC_C_DISCONNECTED;
  850. atomic_inc(&xpc_partitions[ch->partid].nchannels_active);
  851. }
  852. up(&registration->sema);
  853. /* initiate the connection */
  854. ch->flags |= (XPC_C_OPENREQUEST | XPC_C_CONNECTING);
  855. xpc_IPI_send_openrequest(ch, &irq_flags);
  856. xpc_process_connect(ch, &irq_flags);
  857. spin_unlock_irqrestore(&ch->lock, irq_flags);
  858. return xpcSuccess;
  859. }
  860. /*
  861. * Clear some of the msg flags in the local message queue.
  862. */
  863. static inline void
  864. xpc_clear_local_msgqueue_flags(struct xpc_channel *ch)
  865. {
  866. struct xpc_msg *msg;
  867. s64 get;
  868. get = ch->w_remote_GP.get;
  869. do {
  870. msg = (struct xpc_msg *) ((u64) ch->local_msgqueue +
  871. (get % ch->local_nentries) * ch->msg_size);
  872. msg->flags = 0;
  873. } while (++get < (volatile s64) ch->remote_GP.get);
  874. }
  875. /*
  876. * Clear some of the msg flags in the remote message queue.
  877. */
  878. static inline void
  879. xpc_clear_remote_msgqueue_flags(struct xpc_channel *ch)
  880. {
  881. struct xpc_msg *msg;
  882. s64 put;
  883. put = ch->w_remote_GP.put;
  884. do {
  885. msg = (struct xpc_msg *) ((u64) ch->remote_msgqueue +
  886. (put % ch->remote_nentries) * ch->msg_size);
  887. msg->flags = 0;
  888. } while (++put < (volatile s64) ch->remote_GP.put);
  889. }
  890. static void
  891. xpc_process_msg_IPI(struct xpc_partition *part, int ch_number)
  892. {
  893. struct xpc_channel *ch = &part->channels[ch_number];
  894. int nmsgs_sent;
  895. ch->remote_GP = part->remote_GPs[ch_number];
  896. /* See what, if anything, has changed for each connected channel */
  897. xpc_msgqueue_ref(ch);
  898. if (ch->w_remote_GP.get == ch->remote_GP.get &&
  899. ch->w_remote_GP.put == ch->remote_GP.put) {
  900. /* nothing changed since GPs were last pulled */
  901. xpc_msgqueue_deref(ch);
  902. return;
  903. }
  904. if (!(ch->flags & XPC_C_CONNECTED)){
  905. xpc_msgqueue_deref(ch);
  906. return;
  907. }
  908. /*
  909. * First check to see if messages recently sent by us have been
  910. * received by the other side. (The remote GET value will have
  911. * changed since we last looked at it.)
  912. */
  913. if (ch->w_remote_GP.get != ch->remote_GP.get) {
  914. /*
  915. * We need to notify any senders that want to be notified
  916. * that their sent messages have been received by their
  917. * intended recipients. We need to do this before updating
  918. * w_remote_GP.get so that we don't allocate the same message
  919. * queue entries prematurely (see xpc_allocate_msg()).
  920. */
  921. if (atomic_read(&ch->n_to_notify) > 0) {
  922. /*
  923. * Notify senders that messages sent have been
  924. * received and delivered by the other side.
  925. */
  926. xpc_notify_senders(ch, xpcMsgDelivered,
  927. ch->remote_GP.get);
  928. }
  929. /*
  930. * Clear msg->flags in previously sent messages, so that
  931. * they're ready for xpc_allocate_msg().
  932. */
  933. xpc_clear_local_msgqueue_flags(ch);
  934. ch->w_remote_GP.get = ch->remote_GP.get;
  935. dev_dbg(xpc_chan, "w_remote_GP.get changed to %ld, partid=%d, "
  936. "channel=%d\n", ch->w_remote_GP.get, ch->partid,
  937. ch->number);
  938. /*
  939. * If anyone was waiting for message queue entries to become
  940. * available, wake them up.
  941. */
  942. if (atomic_read(&ch->n_on_msg_allocate_wq) > 0) {
  943. wake_up(&ch->msg_allocate_wq);
  944. }
  945. }
  946. /*
  947. * Now check for newly sent messages by the other side. (The remote
  948. * PUT value will have changed since we last looked at it.)
  949. */
  950. if (ch->w_remote_GP.put != ch->remote_GP.put) {
  951. /*
  952. * Clear msg->flags in previously received messages, so that
  953. * they're ready for xpc_get_deliverable_msg().
  954. */
  955. xpc_clear_remote_msgqueue_flags(ch);
  956. ch->w_remote_GP.put = ch->remote_GP.put;
  957. dev_dbg(xpc_chan, "w_remote_GP.put changed to %ld, partid=%d, "
  958. "channel=%d\n", ch->w_remote_GP.put, ch->partid,
  959. ch->number);
  960. nmsgs_sent = ch->w_remote_GP.put - ch->w_local_GP.get;
  961. if (nmsgs_sent > 0) {
  962. dev_dbg(xpc_chan, "msgs waiting to be copied and "
  963. "delivered=%d, partid=%d, channel=%d\n",
  964. nmsgs_sent, ch->partid, ch->number);
  965. if (ch->flags & XPC_C_CONNECTCALLOUT) {
  966. xpc_activate_kthreads(ch, nmsgs_sent);
  967. }
  968. }
  969. }
  970. xpc_msgqueue_deref(ch);
  971. }
  972. void
  973. xpc_process_channel_activity(struct xpc_partition *part)
  974. {
  975. unsigned long irq_flags;
  976. u64 IPI_amo, IPI_flags;
  977. struct xpc_channel *ch;
  978. int ch_number;
  979. u32 ch_flags;
  980. IPI_amo = xpc_get_IPI_flags(part);
  981. /*
  982. * Initiate channel connections for registered channels.
  983. *
  984. * For each connected channel that has pending messages activate idle
  985. * kthreads and/or create new kthreads as needed.
  986. */
  987. for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
  988. ch = &part->channels[ch_number];
  989. /*
  990. * Process any open or close related IPI flags, and then deal
  991. * with connecting or disconnecting the channel as required.
  992. */
  993. IPI_flags = XPC_GET_IPI_FLAGS(IPI_amo, ch_number);
  994. if (XPC_ANY_OPENCLOSE_IPI_FLAGS_SET(IPI_flags)) {
  995. xpc_process_openclose_IPI(part, ch_number, IPI_flags);
  996. }
  997. ch_flags = ch->flags; /* need an atomic snapshot of flags */
  998. if (ch_flags & XPC_C_DISCONNECTING) {
  999. spin_lock_irqsave(&ch->lock, irq_flags);
  1000. xpc_process_disconnect(ch, &irq_flags);
  1001. spin_unlock_irqrestore(&ch->lock, irq_flags);
  1002. continue;
  1003. }
  1004. if (part->act_state == XPC_P_DEACTIVATING) {
  1005. continue;
  1006. }
  1007. if (!(ch_flags & XPC_C_CONNECTED)) {
  1008. if (!(ch_flags & XPC_C_OPENREQUEST)) {
  1009. DBUG_ON(ch_flags & XPC_C_SETUP);
  1010. (void) xpc_connect_channel(ch);
  1011. } else {
  1012. spin_lock_irqsave(&ch->lock, irq_flags);
  1013. xpc_process_connect(ch, &irq_flags);
  1014. spin_unlock_irqrestore(&ch->lock, irq_flags);
  1015. }
  1016. continue;
  1017. }
  1018. /*
  1019. * Process any message related IPI flags, this may involve the
  1020. * activation of kthreads to deliver any pending messages sent
  1021. * from the other partition.
  1022. */
  1023. if (XPC_ANY_MSG_IPI_FLAGS_SET(IPI_flags)) {
  1024. xpc_process_msg_IPI(part, ch_number);
  1025. }
  1026. }
  1027. }
  1028. /*
  1029. * XPC's heartbeat code calls this function to inform XPC that a partition is
  1030. * going down. XPC responds by tearing down the XPartition Communication
  1031. * infrastructure used for the just downed partition.
  1032. *
  1033. * XPC's heartbeat code will never call this function and xpc_partition_up()
  1034. * at the same time. Nor will it ever make multiple calls to either function
  1035. * at the same time.
  1036. */
  1037. void
  1038. xpc_partition_going_down(struct xpc_partition *part, enum xpc_retval reason)
  1039. {
  1040. unsigned long irq_flags;
  1041. int ch_number;
  1042. struct xpc_channel *ch;
  1043. dev_dbg(xpc_chan, "deactivating partition %d, reason=%d\n",
  1044. XPC_PARTID(part), reason);
  1045. if (!xpc_part_ref(part)) {
  1046. /* infrastructure for this partition isn't currently set up */
  1047. return;
  1048. }
  1049. /* disconnect channels associated with the partition going down */
  1050. for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
  1051. ch = &part->channels[ch_number];
  1052. xpc_msgqueue_ref(ch);
  1053. spin_lock_irqsave(&ch->lock, irq_flags);
  1054. XPC_DISCONNECT_CHANNEL(ch, reason, &irq_flags);
  1055. spin_unlock_irqrestore(&ch->lock, irq_flags);
  1056. xpc_msgqueue_deref(ch);
  1057. }
  1058. xpc_wakeup_channel_mgr(part);
  1059. xpc_part_deref(part);
  1060. }
  1061. /*
  1062. * Teardown the infrastructure necessary to support XPartition Communication
  1063. * between the specified remote partition and the local one.
  1064. */
  1065. void
  1066. xpc_teardown_infrastructure(struct xpc_partition *part)
  1067. {
  1068. partid_t partid = XPC_PARTID(part);
  1069. /*
  1070. * We start off by making this partition inaccessible to local
  1071. * processes by marking it as no longer setup. Then we make it
  1072. * inaccessible to remote processes by clearing the XPC per partition
  1073. * specific variable's magic # (which indicates that these variables
  1074. * are no longer valid) and by ignoring all XPC notify IPIs sent to
  1075. * this partition.
  1076. */
  1077. DBUG_ON(atomic_read(&part->nchannels_engaged) != 0);
  1078. DBUG_ON(atomic_read(&part->nchannels_active) != 0);
  1079. DBUG_ON(part->setup_state != XPC_P_SETUP);
  1080. part->setup_state = XPC_P_WTEARDOWN;
  1081. xpc_vars_part[partid].magic = 0;
  1082. free_irq(SGI_XPC_NOTIFY, (void *) (u64) partid);
  1083. /*
  1084. * Before proceding with the teardown we have to wait until all
  1085. * existing references cease.
  1086. */
  1087. wait_event(part->teardown_wq, (atomic_read(&part->references) == 0));
  1088. /* now we can begin tearing down the infrastructure */
  1089. part->setup_state = XPC_P_TORNDOWN;
  1090. /* in case we've still got outstanding timers registered... */
  1091. del_timer_sync(&part->dropped_IPI_timer);
  1092. kfree(part->remote_openclose_args_base);
  1093. part->remote_openclose_args = NULL;
  1094. kfree(part->local_openclose_args_base);
  1095. part->local_openclose_args = NULL;
  1096. kfree(part->remote_GPs_base);
  1097. part->remote_GPs = NULL;
  1098. kfree(part->local_GPs_base);
  1099. part->local_GPs = NULL;
  1100. kfree(part->channels);
  1101. part->channels = NULL;
  1102. part->local_IPI_amo_va = NULL;
  1103. }
  1104. /*
  1105. * Called by XP at the time of channel connection registration to cause
  1106. * XPC to establish connections to all currently active partitions.
  1107. */
  1108. void
  1109. xpc_initiate_connect(int ch_number)
  1110. {
  1111. partid_t partid;
  1112. struct xpc_partition *part;
  1113. struct xpc_channel *ch;
  1114. DBUG_ON(ch_number < 0 || ch_number >= XPC_NCHANNELS);
  1115. for (partid = 1; partid < XP_MAX_PARTITIONS; partid++) {
  1116. part = &xpc_partitions[partid];
  1117. if (xpc_part_ref(part)) {
  1118. ch = &part->channels[ch_number];
  1119. if (!(ch->flags & XPC_C_DISCONNECTING)) {
  1120. DBUG_ON(ch->flags & XPC_C_OPENREQUEST);
  1121. DBUG_ON(ch->flags & XPC_C_CONNECTED);
  1122. DBUG_ON(ch->flags & XPC_C_SETUP);
  1123. /*
  1124. * Initiate the establishment of a connection
  1125. * on the newly registered channel to the
  1126. * remote partition.
  1127. */
  1128. xpc_wakeup_channel_mgr(part);
  1129. }
  1130. xpc_part_deref(part);
  1131. }
  1132. }
  1133. }
  1134. void
  1135. xpc_connected_callout(struct xpc_channel *ch)
  1136. {
  1137. unsigned long irq_flags;
  1138. /* let the registerer know that a connection has been established */
  1139. if (ch->func != NULL) {
  1140. dev_dbg(xpc_chan, "ch->func() called, reason=xpcConnected, "
  1141. "partid=%d, channel=%d\n", ch->partid, ch->number);
  1142. ch->func(xpcConnected, ch->partid, ch->number,
  1143. (void *) (u64) ch->local_nentries, ch->key);
  1144. dev_dbg(xpc_chan, "ch->func() returned, reason=xpcConnected, "
  1145. "partid=%d, channel=%d\n", ch->partid, ch->number);
  1146. }
  1147. spin_lock_irqsave(&ch->lock, irq_flags);
  1148. ch->flags |= XPC_C_CONNECTCALLOUT;
  1149. spin_unlock_irqrestore(&ch->lock, irq_flags);
  1150. }
  1151. /*
  1152. * Called by XP at the time of channel connection unregistration to cause
  1153. * XPC to teardown all current connections for the specified channel.
  1154. *
  1155. * Before returning xpc_initiate_disconnect() will wait until all connections
  1156. * on the specified channel have been closed/torndown. So the caller can be
  1157. * assured that they will not be receiving any more callouts from XPC to the
  1158. * function they registered via xpc_connect().
  1159. *
  1160. * Arguments:
  1161. *
  1162. * ch_number - channel # to unregister.
  1163. */
  1164. void
  1165. xpc_initiate_disconnect(int ch_number)
  1166. {
  1167. unsigned long irq_flags;
  1168. partid_t partid;
  1169. struct xpc_partition *part;
  1170. struct xpc_channel *ch;
  1171. DBUG_ON(ch_number < 0 || ch_number >= XPC_NCHANNELS);
  1172. /* initiate the channel disconnect for every active partition */
  1173. for (partid = 1; partid < XP_MAX_PARTITIONS; partid++) {
  1174. part = &xpc_partitions[partid];
  1175. if (xpc_part_ref(part)) {
  1176. ch = &part->channels[ch_number];
  1177. xpc_msgqueue_ref(ch);
  1178. spin_lock_irqsave(&ch->lock, irq_flags);
  1179. if (!(ch->flags & XPC_C_DISCONNECTED)) {
  1180. ch->flags |= XPC_C_WDISCONNECT;
  1181. XPC_DISCONNECT_CHANNEL(ch, xpcUnregistering,
  1182. &irq_flags);
  1183. }
  1184. spin_unlock_irqrestore(&ch->lock, irq_flags);
  1185. xpc_msgqueue_deref(ch);
  1186. xpc_part_deref(part);
  1187. }
  1188. }
  1189. xpc_disconnect_wait(ch_number);
  1190. }
  1191. /*
  1192. * To disconnect a channel, and reflect it back to all who may be waiting.
  1193. *
  1194. * An OPEN is not allowed until XPC_C_DISCONNECTING is cleared by
  1195. * xpc_process_disconnect(), and if set, XPC_C_WDISCONNECT is cleared by
  1196. * xpc_disconnect_wait().
  1197. *
  1198. * THE CHANNEL IS TO BE LOCKED BY THE CALLER AND WILL REMAIN LOCKED UPON RETURN.
  1199. */
  1200. void
  1201. xpc_disconnect_channel(const int line, struct xpc_channel *ch,
  1202. enum xpc_retval reason, unsigned long *irq_flags)
  1203. {
  1204. u32 channel_was_connected = (ch->flags & XPC_C_CONNECTED);
  1205. DBUG_ON(!spin_is_locked(&ch->lock));
  1206. if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_DISCONNECTED)) {
  1207. return;
  1208. }
  1209. DBUG_ON(!(ch->flags & (XPC_C_CONNECTING | XPC_C_CONNECTED)));
  1210. dev_dbg(xpc_chan, "reason=%d, line=%d, partid=%d, channel=%d\n",
  1211. reason, line, ch->partid, ch->number);
  1212. XPC_SET_REASON(ch, reason, line);
  1213. ch->flags |= (XPC_C_CLOSEREQUEST | XPC_C_DISCONNECTING);
  1214. /* some of these may not have been set */
  1215. ch->flags &= ~(XPC_C_OPENREQUEST | XPC_C_OPENREPLY |
  1216. XPC_C_ROPENREQUEST | XPC_C_ROPENREPLY |
  1217. XPC_C_CONNECTING | XPC_C_CONNECTED);
  1218. xpc_IPI_send_closerequest(ch, irq_flags);
  1219. if (channel_was_connected) {
  1220. ch->flags |= XPC_C_WASCONNECTED;
  1221. }
  1222. spin_unlock_irqrestore(&ch->lock, *irq_flags);
  1223. /* wake all idle kthreads so they can exit */
  1224. if (atomic_read(&ch->kthreads_idle) > 0) {
  1225. wake_up_all(&ch->idle_wq);
  1226. }
  1227. /* wake those waiting to allocate an entry from the local msg queue */
  1228. if (atomic_read(&ch->n_on_msg_allocate_wq) > 0) {
  1229. wake_up(&ch->msg_allocate_wq);
  1230. }
  1231. spin_lock_irqsave(&ch->lock, *irq_flags);
  1232. }
  1233. void
  1234. xpc_disconnecting_callout(struct xpc_channel *ch)
  1235. {
  1236. /*
  1237. * Let the channel's registerer know that the channel is being
  1238. * disconnected. We don't want to do this if the registerer was never
  1239. * informed of a connection being made.
  1240. */
  1241. if (ch->func != NULL) {
  1242. dev_dbg(xpc_chan, "ch->func() called, reason=xpcDisconnecting,"
  1243. " partid=%d, channel=%d\n", ch->partid, ch->number);
  1244. ch->func(xpcDisconnecting, ch->partid, ch->number, NULL,
  1245. ch->key);
  1246. dev_dbg(xpc_chan, "ch->func() returned, reason="
  1247. "xpcDisconnecting, partid=%d, channel=%d\n",
  1248. ch->partid, ch->number);
  1249. }
  1250. }
  1251. /*
  1252. * Wait for a message entry to become available for the specified channel,
  1253. * but don't wait any longer than 1 jiffy.
  1254. */
  1255. static enum xpc_retval
  1256. xpc_allocate_msg_wait(struct xpc_channel *ch)
  1257. {
  1258. enum xpc_retval ret;
  1259. if (ch->flags & XPC_C_DISCONNECTING) {
  1260. DBUG_ON(ch->reason == xpcInterrupted); // >>> Is this true?
  1261. return ch->reason;
  1262. }
  1263. atomic_inc(&ch->n_on_msg_allocate_wq);
  1264. ret = interruptible_sleep_on_timeout(&ch->msg_allocate_wq, 1);
  1265. atomic_dec(&ch->n_on_msg_allocate_wq);
  1266. if (ch->flags & XPC_C_DISCONNECTING) {
  1267. ret = ch->reason;
  1268. DBUG_ON(ch->reason == xpcInterrupted); // >>> Is this true?
  1269. } else if (ret == 0) {
  1270. ret = xpcTimeout;
  1271. } else {
  1272. ret = xpcInterrupted;
  1273. }
  1274. return ret;
  1275. }
  1276. /*
  1277. * Allocate an entry for a message from the message queue associated with the
  1278. * specified channel.
  1279. */
  1280. static enum xpc_retval
  1281. xpc_allocate_msg(struct xpc_channel *ch, u32 flags,
  1282. struct xpc_msg **address_of_msg)
  1283. {
  1284. struct xpc_msg *msg;
  1285. enum xpc_retval ret;
  1286. s64 put;
  1287. /* this reference will be dropped in xpc_send_msg() */
  1288. xpc_msgqueue_ref(ch);
  1289. if (ch->flags & XPC_C_DISCONNECTING) {
  1290. xpc_msgqueue_deref(ch);
  1291. return ch->reason;
  1292. }
  1293. if (!(ch->flags & XPC_C_CONNECTED)) {
  1294. xpc_msgqueue_deref(ch);
  1295. return xpcNotConnected;
  1296. }
  1297. /*
  1298. * Get the next available message entry from the local message queue.
  1299. * If none are available, we'll make sure that we grab the latest
  1300. * GP values.
  1301. */
  1302. ret = xpcTimeout;
  1303. while (1) {
  1304. put = (volatile s64) ch->w_local_GP.put;
  1305. if (put - (volatile s64) ch->w_remote_GP.get <
  1306. ch->local_nentries) {
  1307. /* There are available message entries. We need to try
  1308. * to secure one for ourselves. We'll do this by trying
  1309. * to increment w_local_GP.put as long as someone else
  1310. * doesn't beat us to it. If they do, we'll have to
  1311. * try again.
  1312. */
  1313. if (cmpxchg(&ch->w_local_GP.put, put, put + 1) ==
  1314. put) {
  1315. /* we got the entry referenced by put */
  1316. break;
  1317. }
  1318. continue; /* try again */
  1319. }
  1320. /*
  1321. * There aren't any available msg entries at this time.
  1322. *
  1323. * In waiting for a message entry to become available,
  1324. * we set a timeout in case the other side is not
  1325. * sending completion IPIs. This lets us fake an IPI
  1326. * that will cause the IPI handler to fetch the latest
  1327. * GP values as if an IPI was sent by the other side.
  1328. */
  1329. if (ret == xpcTimeout) {
  1330. xpc_IPI_send_local_msgrequest(ch);
  1331. }
  1332. if (flags & XPC_NOWAIT) {
  1333. xpc_msgqueue_deref(ch);
  1334. return xpcNoWait;
  1335. }
  1336. ret = xpc_allocate_msg_wait(ch);
  1337. if (ret != xpcInterrupted && ret != xpcTimeout) {
  1338. xpc_msgqueue_deref(ch);
  1339. return ret;
  1340. }
  1341. }
  1342. /* get the message's address and initialize it */
  1343. msg = (struct xpc_msg *) ((u64) ch->local_msgqueue +
  1344. (put % ch->local_nentries) * ch->msg_size);
  1345. DBUG_ON(msg->flags != 0);
  1346. msg->number = put;
  1347. dev_dbg(xpc_chan, "w_local_GP.put changed to %ld; msg=0x%p, "
  1348. "msg_number=%ld, partid=%d, channel=%d\n", put + 1,
  1349. (void *) msg, msg->number, ch->partid, ch->number);
  1350. *address_of_msg = msg;
  1351. return xpcSuccess;
  1352. }
  1353. /*
  1354. * Allocate an entry for a message from the message queue associated with the
  1355. * specified channel. NOTE that this routine can sleep waiting for a message
  1356. * entry to become available. To not sleep, pass in the XPC_NOWAIT flag.
  1357. *
  1358. * Arguments:
  1359. *
  1360. * partid - ID of partition to which the channel is connected.
  1361. * ch_number - channel #.
  1362. * flags - see xpc.h for valid flags.
  1363. * payload - address of the allocated payload area pointer (filled in on
  1364. * return) in which the user-defined message is constructed.
  1365. */
  1366. enum xpc_retval
  1367. xpc_initiate_allocate(partid_t partid, int ch_number, u32 flags, void **payload)
  1368. {
  1369. struct xpc_partition *part = &xpc_partitions[partid];
  1370. enum xpc_retval ret = xpcUnknownReason;
  1371. struct xpc_msg *msg;
  1372. DBUG_ON(partid <= 0 || partid >= XP_MAX_PARTITIONS);
  1373. DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
  1374. *payload = NULL;
  1375. if (xpc_part_ref(part)) {
  1376. ret = xpc_allocate_msg(&part->channels[ch_number], flags, &msg);
  1377. xpc_part_deref(part);
  1378. if (msg != NULL) {
  1379. *payload = &msg->payload;
  1380. }
  1381. }
  1382. return ret;
  1383. }
  1384. /*
  1385. * Now we actually send the messages that are ready to be sent by advancing
  1386. * the local message queue's Put value and then send an IPI to the recipient
  1387. * partition.
  1388. */
  1389. static void
  1390. xpc_send_msgs(struct xpc_channel *ch, s64 initial_put)
  1391. {
  1392. struct xpc_msg *msg;
  1393. s64 put = initial_put + 1;
  1394. int send_IPI = 0;
  1395. while (1) {
  1396. while (1) {
  1397. if (put == (volatile s64) ch->w_local_GP.put) {
  1398. break;
  1399. }
  1400. msg = (struct xpc_msg *) ((u64) ch->local_msgqueue +
  1401. (put % ch->local_nentries) * ch->msg_size);
  1402. if (!(msg->flags & XPC_M_READY)) {
  1403. break;
  1404. }
  1405. put++;
  1406. }
  1407. if (put == initial_put) {
  1408. /* nothing's changed */
  1409. break;
  1410. }
  1411. if (cmpxchg_rel(&ch->local_GP->put, initial_put, put) !=
  1412. initial_put) {
  1413. /* someone else beat us to it */
  1414. DBUG_ON((volatile s64) ch->local_GP->put < initial_put);
  1415. break;
  1416. }
  1417. /* we just set the new value of local_GP->put */
  1418. dev_dbg(xpc_chan, "local_GP->put changed to %ld, partid=%d, "
  1419. "channel=%d\n", put, ch->partid, ch->number);
  1420. send_IPI = 1;
  1421. /*
  1422. * We need to ensure that the message referenced by
  1423. * local_GP->put is not XPC_M_READY or that local_GP->put
  1424. * equals w_local_GP.put, so we'll go have a look.
  1425. */
  1426. initial_put = put;
  1427. }
  1428. if (send_IPI) {
  1429. xpc_IPI_send_msgrequest(ch);
  1430. }
  1431. }
  1432. /*
  1433. * Common code that does the actual sending of the message by advancing the
  1434. * local message queue's Put value and sends an IPI to the partition the
  1435. * message is being sent to.
  1436. */
  1437. static enum xpc_retval
  1438. xpc_send_msg(struct xpc_channel *ch, struct xpc_msg *msg, u8 notify_type,
  1439. xpc_notify_func func, void *key)
  1440. {
  1441. enum xpc_retval ret = xpcSuccess;
  1442. struct xpc_notify *notify = notify;
  1443. s64 put, msg_number = msg->number;
  1444. DBUG_ON(notify_type == XPC_N_CALL && func == NULL);
  1445. DBUG_ON((((u64) msg - (u64) ch->local_msgqueue) / ch->msg_size) !=
  1446. msg_number % ch->local_nentries);
  1447. DBUG_ON(msg->flags & XPC_M_READY);
  1448. if (ch->flags & XPC_C_DISCONNECTING) {
  1449. /* drop the reference grabbed in xpc_allocate_msg() */
  1450. xpc_msgqueue_deref(ch);
  1451. return ch->reason;
  1452. }
  1453. if (notify_type != 0) {
  1454. /*
  1455. * Tell the remote side to send an ACK interrupt when the
  1456. * message has been delivered.
  1457. */
  1458. msg->flags |= XPC_M_INTERRUPT;
  1459. atomic_inc(&ch->n_to_notify);
  1460. notify = &ch->notify_queue[msg_number % ch->local_nentries];
  1461. notify->func = func;
  1462. notify->key = key;
  1463. notify->type = notify_type;
  1464. // >>> is a mb() needed here?
  1465. if (ch->flags & XPC_C_DISCONNECTING) {
  1466. /*
  1467. * An error occurred between our last error check and
  1468. * this one. We will try to clear the type field from
  1469. * the notify entry. If we succeed then
  1470. * xpc_disconnect_channel() didn't already process
  1471. * the notify entry.
  1472. */
  1473. if (cmpxchg(&notify->type, notify_type, 0) ==
  1474. notify_type) {
  1475. atomic_dec(&ch->n_to_notify);
  1476. ret = ch->reason;
  1477. }
  1478. /* drop the reference grabbed in xpc_allocate_msg() */
  1479. xpc_msgqueue_deref(ch);
  1480. return ret;
  1481. }
  1482. }
  1483. msg->flags |= XPC_M_READY;
  1484. /*
  1485. * The preceding store of msg->flags must occur before the following
  1486. * load of ch->local_GP->put.
  1487. */
  1488. mb();
  1489. /* see if the message is next in line to be sent, if so send it */
  1490. put = ch->local_GP->put;
  1491. if (put == msg_number) {
  1492. xpc_send_msgs(ch, put);
  1493. }
  1494. /* drop the reference grabbed in xpc_allocate_msg() */
  1495. xpc_msgqueue_deref(ch);
  1496. return ret;
  1497. }
  1498. /*
  1499. * Send a message previously allocated using xpc_initiate_allocate() on the
  1500. * specified channel connected to the specified partition.
  1501. *
  1502. * This routine will not wait for the message to be received, nor will
  1503. * notification be given when it does happen. Once this routine has returned
  1504. * the message entry allocated via xpc_initiate_allocate() is no longer
  1505. * accessable to the caller.
  1506. *
  1507. * This routine, although called by users, does not call xpc_part_ref() to
  1508. * ensure that the partition infrastructure is in place. It relies on the
  1509. * fact that we called xpc_msgqueue_ref() in xpc_allocate_msg().
  1510. *
  1511. * Arguments:
  1512. *
  1513. * partid - ID of partition to which the channel is connected.
  1514. * ch_number - channel # to send message on.
  1515. * payload - pointer to the payload area allocated via
  1516. * xpc_initiate_allocate().
  1517. */
  1518. enum xpc_retval
  1519. xpc_initiate_send(partid_t partid, int ch_number, void *payload)
  1520. {
  1521. struct xpc_partition *part = &xpc_partitions[partid];
  1522. struct xpc_msg *msg = XPC_MSG_ADDRESS(payload);
  1523. enum xpc_retval ret;
  1524. dev_dbg(xpc_chan, "msg=0x%p, partid=%d, channel=%d\n", (void *) msg,
  1525. partid, ch_number);
  1526. DBUG_ON(partid <= 0 || partid >= XP_MAX_PARTITIONS);
  1527. DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
  1528. DBUG_ON(msg == NULL);
  1529. ret = xpc_send_msg(&part->channels[ch_number], msg, 0, NULL, NULL);
  1530. return ret;
  1531. }
  1532. /*
  1533. * Send a message previously allocated using xpc_initiate_allocate on the
  1534. * specified channel connected to the specified partition.
  1535. *
  1536. * This routine will not wait for the message to be sent. Once this routine
  1537. * has returned the message entry allocated via xpc_initiate_allocate() is no
  1538. * longer accessable to the caller.
  1539. *
  1540. * Once the remote end of the channel has received the message, the function
  1541. * passed as an argument to xpc_initiate_send_notify() will be called. This
  1542. * allows the sender to free up or re-use any buffers referenced by the
  1543. * message, but does NOT mean the message has been processed at the remote
  1544. * end by a receiver.
  1545. *
  1546. * If this routine returns an error, the caller's function will NOT be called.
  1547. *
  1548. * This routine, although called by users, does not call xpc_part_ref() to
  1549. * ensure that the partition infrastructure is in place. It relies on the
  1550. * fact that we called xpc_msgqueue_ref() in xpc_allocate_msg().
  1551. *
  1552. * Arguments:
  1553. *
  1554. * partid - ID of partition to which the channel is connected.
  1555. * ch_number - channel # to send message on.
  1556. * payload - pointer to the payload area allocated via
  1557. * xpc_initiate_allocate().
  1558. * func - function to call with asynchronous notification of message
  1559. * receipt. THIS FUNCTION MUST BE NON-BLOCKING.
  1560. * key - user-defined key to be passed to the function when it's called.
  1561. */
  1562. enum xpc_retval
  1563. xpc_initiate_send_notify(partid_t partid, int ch_number, void *payload,
  1564. xpc_notify_func func, void *key)
  1565. {
  1566. struct xpc_partition *part = &xpc_partitions[partid];
  1567. struct xpc_msg *msg = XPC_MSG_ADDRESS(payload);
  1568. enum xpc_retval ret;
  1569. dev_dbg(xpc_chan, "msg=0x%p, partid=%d, channel=%d\n", (void *) msg,
  1570. partid, ch_number);
  1571. DBUG_ON(partid <= 0 || partid >= XP_MAX_PARTITIONS);
  1572. DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
  1573. DBUG_ON(msg == NULL);
  1574. DBUG_ON(func == NULL);
  1575. ret = xpc_send_msg(&part->channels[ch_number], msg, XPC_N_CALL,
  1576. func, key);
  1577. return ret;
  1578. }
  1579. static struct xpc_msg *
  1580. xpc_pull_remote_msg(struct xpc_channel *ch, s64 get)
  1581. {
  1582. struct xpc_partition *part = &xpc_partitions[ch->partid];
  1583. struct xpc_msg *remote_msg, *msg;
  1584. u32 msg_index, nmsgs;
  1585. u64 msg_offset;
  1586. enum xpc_retval ret;
  1587. if (down_interruptible(&ch->msg_to_pull_sema) != 0) {
  1588. /* we were interrupted by a signal */
  1589. return NULL;
  1590. }
  1591. while (get >= ch->next_msg_to_pull) {
  1592. /* pull as many messages as are ready and able to be pulled */
  1593. msg_index = ch->next_msg_to_pull % ch->remote_nentries;
  1594. DBUG_ON(ch->next_msg_to_pull >=
  1595. (volatile s64) ch->w_remote_GP.put);
  1596. nmsgs = (volatile s64) ch->w_remote_GP.put -
  1597. ch->next_msg_to_pull;
  1598. if (msg_index + nmsgs > ch->remote_nentries) {
  1599. /* ignore the ones that wrap the msg queue for now */
  1600. nmsgs = ch->remote_nentries - msg_index;
  1601. }
  1602. msg_offset = msg_index * ch->msg_size;
  1603. msg = (struct xpc_msg *) ((u64) ch->remote_msgqueue +
  1604. msg_offset);
  1605. remote_msg = (struct xpc_msg *) (ch->remote_msgqueue_pa +
  1606. msg_offset);
  1607. if ((ret = xpc_pull_remote_cachelines(part, msg, remote_msg,
  1608. nmsgs * ch->msg_size)) != xpcSuccess) {
  1609. dev_dbg(xpc_chan, "failed to pull %d msgs starting with"
  1610. " msg %ld from partition %d, channel=%d, "
  1611. "ret=%d\n", nmsgs, ch->next_msg_to_pull,
  1612. ch->partid, ch->number, ret);
  1613. XPC_DEACTIVATE_PARTITION(part, ret);
  1614. up(&ch->msg_to_pull_sema);
  1615. return NULL;
  1616. }
  1617. mb(); /* >>> this may not be needed, we're not sure */
  1618. ch->next_msg_to_pull += nmsgs;
  1619. }
  1620. up(&ch->msg_to_pull_sema);
  1621. /* return the message we were looking for */
  1622. msg_offset = (get % ch->remote_nentries) * ch->msg_size;
  1623. msg = (struct xpc_msg *) ((u64) ch->remote_msgqueue + msg_offset);
  1624. return msg;
  1625. }
  1626. /*
  1627. * Get a message to be delivered.
  1628. */
  1629. static struct xpc_msg *
  1630. xpc_get_deliverable_msg(struct xpc_channel *ch)
  1631. {
  1632. struct xpc_msg *msg = NULL;
  1633. s64 get;
  1634. do {
  1635. if ((volatile u32) ch->flags & XPC_C_DISCONNECTING) {
  1636. break;
  1637. }
  1638. get = (volatile s64) ch->w_local_GP.get;
  1639. if (get == (volatile s64) ch->w_remote_GP.put) {
  1640. break;
  1641. }
  1642. /* There are messages waiting to be pulled and delivered.
  1643. * We need to try to secure one for ourselves. We'll do this
  1644. * by trying to increment w_local_GP.get and hope that no one
  1645. * else beats us to it. If they do, we'll we'll simply have
  1646. * to try again for the next one.
  1647. */
  1648. if (cmpxchg(&ch->w_local_GP.get, get, get + 1) == get) {
  1649. /* we got the entry referenced by get */
  1650. dev_dbg(xpc_chan, "w_local_GP.get changed to %ld, "
  1651. "partid=%d, channel=%d\n", get + 1,
  1652. ch->partid, ch->number);
  1653. /* pull the message from the remote partition */
  1654. msg = xpc_pull_remote_msg(ch, get);
  1655. DBUG_ON(msg != NULL && msg->number != get);
  1656. DBUG_ON(msg != NULL && (msg->flags & XPC_M_DONE));
  1657. DBUG_ON(msg != NULL && !(msg->flags & XPC_M_READY));
  1658. break;
  1659. }
  1660. } while (1);
  1661. return msg;
  1662. }
  1663. /*
  1664. * Deliver a message to its intended recipient.
  1665. */
  1666. void
  1667. xpc_deliver_msg(struct xpc_channel *ch)
  1668. {
  1669. struct xpc_msg *msg;
  1670. if ((msg = xpc_get_deliverable_msg(ch)) != NULL) {
  1671. /*
  1672. * This ref is taken to protect the payload itself from being
  1673. * freed before the user is finished with it, which the user
  1674. * indicates by calling xpc_initiate_received().
  1675. */
  1676. xpc_msgqueue_ref(ch);
  1677. atomic_inc(&ch->kthreads_active);
  1678. if (ch->func != NULL) {
  1679. dev_dbg(xpc_chan, "ch->func() called, msg=0x%p, "
  1680. "msg_number=%ld, partid=%d, channel=%d\n",
  1681. (void *) msg, msg->number, ch->partid,
  1682. ch->number);
  1683. /* deliver the message to its intended recipient */
  1684. ch->func(xpcMsgReceived, ch->partid, ch->number,
  1685. &msg->payload, ch->key);
  1686. dev_dbg(xpc_chan, "ch->func() returned, msg=0x%p, "
  1687. "msg_number=%ld, partid=%d, channel=%d\n",
  1688. (void *) msg, msg->number, ch->partid,
  1689. ch->number);
  1690. }
  1691. atomic_dec(&ch->kthreads_active);
  1692. }
  1693. }
  1694. /*
  1695. * Now we actually acknowledge the messages that have been delivered and ack'd
  1696. * by advancing the cached remote message queue's Get value and if requested
  1697. * send an IPI to the message sender's partition.
  1698. */
  1699. static void
  1700. xpc_acknowledge_msgs(struct xpc_channel *ch, s64 initial_get, u8 msg_flags)
  1701. {
  1702. struct xpc_msg *msg;
  1703. s64 get = initial_get + 1;
  1704. int send_IPI = 0;
  1705. while (1) {
  1706. while (1) {
  1707. if (get == (volatile s64) ch->w_local_GP.get) {
  1708. break;
  1709. }
  1710. msg = (struct xpc_msg *) ((u64) ch->remote_msgqueue +
  1711. (get % ch->remote_nentries) * ch->msg_size);
  1712. if (!(msg->flags & XPC_M_DONE)) {
  1713. break;
  1714. }
  1715. msg_flags |= msg->flags;
  1716. get++;
  1717. }
  1718. if (get == initial_get) {
  1719. /* nothing's changed */
  1720. break;
  1721. }
  1722. if (cmpxchg_rel(&ch->local_GP->get, initial_get, get) !=
  1723. initial_get) {
  1724. /* someone else beat us to it */
  1725. DBUG_ON((volatile s64) ch->local_GP->get <=
  1726. initial_get);
  1727. break;
  1728. }
  1729. /* we just set the new value of local_GP->get */
  1730. dev_dbg(xpc_chan, "local_GP->get changed to %ld, partid=%d, "
  1731. "channel=%d\n", get, ch->partid, ch->number);
  1732. send_IPI = (msg_flags & XPC_M_INTERRUPT);
  1733. /*
  1734. * We need to ensure that the message referenced by
  1735. * local_GP->get is not XPC_M_DONE or that local_GP->get
  1736. * equals w_local_GP.get, so we'll go have a look.
  1737. */
  1738. initial_get = get;
  1739. }
  1740. if (send_IPI) {
  1741. xpc_IPI_send_msgrequest(ch);
  1742. }
  1743. }
  1744. /*
  1745. * Acknowledge receipt of a delivered message.
  1746. *
  1747. * If a message has XPC_M_INTERRUPT set, send an interrupt to the partition
  1748. * that sent the message.
  1749. *
  1750. * This function, although called by users, does not call xpc_part_ref() to
  1751. * ensure that the partition infrastructure is in place. It relies on the
  1752. * fact that we called xpc_msgqueue_ref() in xpc_deliver_msg().
  1753. *
  1754. * Arguments:
  1755. *
  1756. * partid - ID of partition to which the channel is connected.
  1757. * ch_number - channel # message received on.
  1758. * payload - pointer to the payload area allocated via
  1759. * xpc_initiate_allocate().
  1760. */
  1761. void
  1762. xpc_initiate_received(partid_t partid, int ch_number, void *payload)
  1763. {
  1764. struct xpc_partition *part = &xpc_partitions[partid];
  1765. struct xpc_channel *ch;
  1766. struct xpc_msg *msg = XPC_MSG_ADDRESS(payload);
  1767. s64 get, msg_number = msg->number;
  1768. DBUG_ON(partid <= 0 || partid >= XP_MAX_PARTITIONS);
  1769. DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
  1770. ch = &part->channels[ch_number];
  1771. dev_dbg(xpc_chan, "msg=0x%p, msg_number=%ld, partid=%d, channel=%d\n",
  1772. (void *) msg, msg_number, ch->partid, ch->number);
  1773. DBUG_ON((((u64) msg - (u64) ch->remote_msgqueue) / ch->msg_size) !=
  1774. msg_number % ch->remote_nentries);
  1775. DBUG_ON(msg->flags & XPC_M_DONE);
  1776. msg->flags |= XPC_M_DONE;
  1777. /*
  1778. * The preceding store of msg->flags must occur before the following
  1779. * load of ch->local_GP->get.
  1780. */
  1781. mb();
  1782. /*
  1783. * See if this message is next in line to be acknowledged as having
  1784. * been delivered.
  1785. */
  1786. get = ch->local_GP->get;
  1787. if (get == msg_number) {
  1788. xpc_acknowledge_msgs(ch, get, msg->flags);
  1789. }
  1790. /* the call to xpc_msgqueue_ref() was done by xpc_deliver_msg() */
  1791. xpc_msgqueue_deref(ch);
  1792. }