xpc_channel.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362
  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-2006 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 <asm/sn/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. if (ch->flags & XPC_C_CONNECTCALLOUT) {
  617. spin_unlock_irqrestore(&ch->lock, *irq_flags);
  618. xpc_disconnect_callout(ch, xpcDisconnected);
  619. spin_lock_irqsave(&ch->lock, *irq_flags);
  620. }
  621. /* it's now safe to free the channel's message queues */
  622. xpc_free_msgqueues(ch);
  623. /* mark disconnected, clear all other flags except XPC_C_WDISCONNECT */
  624. ch->flags = (XPC_C_DISCONNECTED | (ch->flags & XPC_C_WDISCONNECT));
  625. atomic_dec(&part->nchannels_active);
  626. if (channel_was_connected) {
  627. dev_info(xpc_chan, "channel %d to partition %d disconnected, "
  628. "reason=%d\n", ch->number, ch->partid, ch->reason);
  629. }
  630. if (ch->flags & XPC_C_WDISCONNECT) {
  631. spin_unlock_irqrestore(&ch->lock, *irq_flags);
  632. up(&ch->wdisconnect_sema);
  633. spin_lock_irqsave(&ch->lock, *irq_flags);
  634. } else if (ch->delayed_IPI_flags) {
  635. if (part->act_state != XPC_P_DEACTIVATING) {
  636. /* time to take action on any delayed IPI flags */
  637. spin_lock(&part->IPI_lock);
  638. XPC_SET_IPI_FLAGS(part->local_IPI_amo, ch->number,
  639. ch->delayed_IPI_flags);
  640. spin_unlock(&part->IPI_lock);
  641. }
  642. ch->delayed_IPI_flags = 0;
  643. }
  644. }
  645. /*
  646. * Process a change in the channel's remote connection state.
  647. */
  648. static void
  649. xpc_process_openclose_IPI(struct xpc_partition *part, int ch_number,
  650. u8 IPI_flags)
  651. {
  652. unsigned long irq_flags;
  653. struct xpc_openclose_args *args =
  654. &part->remote_openclose_args[ch_number];
  655. struct xpc_channel *ch = &part->channels[ch_number];
  656. enum xpc_retval reason;
  657. spin_lock_irqsave(&ch->lock, irq_flags);
  658. again:
  659. if ((ch->flags & XPC_C_DISCONNECTED) &&
  660. (ch->flags & XPC_C_WDISCONNECT)) {
  661. /*
  662. * Delay processing IPI flags until thread waiting disconnect
  663. * has had a chance to see that the channel is disconnected.
  664. */
  665. ch->delayed_IPI_flags |= IPI_flags;
  666. spin_unlock_irqrestore(&ch->lock, irq_flags);
  667. return;
  668. }
  669. if (IPI_flags & XPC_IPI_CLOSEREQUEST) {
  670. dev_dbg(xpc_chan, "XPC_IPI_CLOSEREQUEST (reason=%d) received "
  671. "from partid=%d, channel=%d\n", args->reason,
  672. ch->partid, ch->number);
  673. /*
  674. * If RCLOSEREQUEST is set, we're probably waiting for
  675. * RCLOSEREPLY. We should find it and a ROPENREQUEST packed
  676. * with this RCLOSEREQUEST in the IPI_flags.
  677. */
  678. if (ch->flags & XPC_C_RCLOSEREQUEST) {
  679. DBUG_ON(!(ch->flags & XPC_C_DISCONNECTING));
  680. DBUG_ON(!(ch->flags & XPC_C_CLOSEREQUEST));
  681. DBUG_ON(!(ch->flags & XPC_C_CLOSEREPLY));
  682. DBUG_ON(ch->flags & XPC_C_RCLOSEREPLY);
  683. DBUG_ON(!(IPI_flags & XPC_IPI_CLOSEREPLY));
  684. IPI_flags &= ~XPC_IPI_CLOSEREPLY;
  685. ch->flags |= XPC_C_RCLOSEREPLY;
  686. /* both sides have finished disconnecting */
  687. xpc_process_disconnect(ch, &irq_flags);
  688. DBUG_ON(!(ch->flags & XPC_C_DISCONNECTED));
  689. goto again;
  690. }
  691. if (ch->flags & XPC_C_DISCONNECTED) {
  692. if (!(IPI_flags & XPC_IPI_OPENREQUEST)) {
  693. if ((XPC_GET_IPI_FLAGS(part->local_IPI_amo,
  694. ch_number) & XPC_IPI_OPENREQUEST)) {
  695. DBUG_ON(ch->delayed_IPI_flags != 0);
  696. spin_lock(&part->IPI_lock);
  697. XPC_SET_IPI_FLAGS(part->local_IPI_amo,
  698. ch_number,
  699. XPC_IPI_CLOSEREQUEST);
  700. spin_unlock(&part->IPI_lock);
  701. }
  702. spin_unlock_irqrestore(&ch->lock, irq_flags);
  703. return;
  704. }
  705. XPC_SET_REASON(ch, 0, 0);
  706. ch->flags &= ~XPC_C_DISCONNECTED;
  707. atomic_inc(&part->nchannels_active);
  708. ch->flags |= (XPC_C_CONNECTING | XPC_C_ROPENREQUEST);
  709. }
  710. IPI_flags &= ~(XPC_IPI_OPENREQUEST | XPC_IPI_OPENREPLY);
  711. /*
  712. * The meaningful CLOSEREQUEST connection state fields are:
  713. * reason = reason connection is to be closed
  714. */
  715. ch->flags |= XPC_C_RCLOSEREQUEST;
  716. if (!(ch->flags & XPC_C_DISCONNECTING)) {
  717. reason = args->reason;
  718. if (reason <= xpcSuccess || reason > xpcUnknownReason) {
  719. reason = xpcUnknownReason;
  720. } else if (reason == xpcUnregistering) {
  721. reason = xpcOtherUnregistering;
  722. }
  723. XPC_DISCONNECT_CHANNEL(ch, reason, &irq_flags);
  724. DBUG_ON(IPI_flags & XPC_IPI_CLOSEREPLY);
  725. spin_unlock_irqrestore(&ch->lock, irq_flags);
  726. return;
  727. }
  728. xpc_process_disconnect(ch, &irq_flags);
  729. }
  730. if (IPI_flags & XPC_IPI_CLOSEREPLY) {
  731. dev_dbg(xpc_chan, "XPC_IPI_CLOSEREPLY received from partid=%d,"
  732. " channel=%d\n", ch->partid, ch->number);
  733. if (ch->flags & XPC_C_DISCONNECTED) {
  734. DBUG_ON(part->act_state != XPC_P_DEACTIVATING);
  735. spin_unlock_irqrestore(&ch->lock, irq_flags);
  736. return;
  737. }
  738. DBUG_ON(!(ch->flags & XPC_C_CLOSEREQUEST));
  739. if (!(ch->flags & XPC_C_RCLOSEREQUEST)) {
  740. if ((XPC_GET_IPI_FLAGS(part->local_IPI_amo, ch_number)
  741. & XPC_IPI_CLOSEREQUEST)) {
  742. DBUG_ON(ch->delayed_IPI_flags != 0);
  743. spin_lock(&part->IPI_lock);
  744. XPC_SET_IPI_FLAGS(part->local_IPI_amo,
  745. ch_number, XPC_IPI_CLOSEREPLY);
  746. spin_unlock(&part->IPI_lock);
  747. }
  748. spin_unlock_irqrestore(&ch->lock, irq_flags);
  749. return;
  750. }
  751. ch->flags |= XPC_C_RCLOSEREPLY;
  752. if (ch->flags & XPC_C_CLOSEREPLY) {
  753. /* both sides have finished disconnecting */
  754. xpc_process_disconnect(ch, &irq_flags);
  755. }
  756. }
  757. if (IPI_flags & XPC_IPI_OPENREQUEST) {
  758. dev_dbg(xpc_chan, "XPC_IPI_OPENREQUEST (msg_size=%d, "
  759. "local_nentries=%d) received from partid=%d, "
  760. "channel=%d\n", args->msg_size, args->local_nentries,
  761. ch->partid, ch->number);
  762. if (part->act_state == XPC_P_DEACTIVATING ||
  763. (ch->flags & XPC_C_ROPENREQUEST)) {
  764. spin_unlock_irqrestore(&ch->lock, irq_flags);
  765. return;
  766. }
  767. if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_WDISCONNECT)) {
  768. ch->delayed_IPI_flags |= XPC_IPI_OPENREQUEST;
  769. spin_unlock_irqrestore(&ch->lock, irq_flags);
  770. return;
  771. }
  772. DBUG_ON(!(ch->flags & (XPC_C_DISCONNECTED |
  773. XPC_C_OPENREQUEST)));
  774. DBUG_ON(ch->flags & (XPC_C_ROPENREQUEST | XPC_C_ROPENREPLY |
  775. XPC_C_OPENREPLY | XPC_C_CONNECTED));
  776. /*
  777. * The meaningful OPENREQUEST connection state fields are:
  778. * msg_size = size of channel's messages in bytes
  779. * local_nentries = remote partition's local_nentries
  780. */
  781. if (args->msg_size == 0 || args->local_nentries == 0) {
  782. /* assume OPENREQUEST was delayed by mistake */
  783. spin_unlock_irqrestore(&ch->lock, irq_flags);
  784. return;
  785. }
  786. ch->flags |= (XPC_C_ROPENREQUEST | XPC_C_CONNECTING);
  787. ch->remote_nentries = args->local_nentries;
  788. if (ch->flags & XPC_C_OPENREQUEST) {
  789. if (args->msg_size != ch->msg_size) {
  790. XPC_DISCONNECT_CHANNEL(ch, xpcUnequalMsgSizes,
  791. &irq_flags);
  792. spin_unlock_irqrestore(&ch->lock, irq_flags);
  793. return;
  794. }
  795. } else {
  796. ch->msg_size = args->msg_size;
  797. XPC_SET_REASON(ch, 0, 0);
  798. ch->flags &= ~XPC_C_DISCONNECTED;
  799. atomic_inc(&part->nchannels_active);
  800. }
  801. xpc_process_connect(ch, &irq_flags);
  802. }
  803. if (IPI_flags & XPC_IPI_OPENREPLY) {
  804. dev_dbg(xpc_chan, "XPC_IPI_OPENREPLY (local_msgqueue_pa=0x%lx, "
  805. "local_nentries=%d, remote_nentries=%d) received from "
  806. "partid=%d, channel=%d\n", args->local_msgqueue_pa,
  807. args->local_nentries, args->remote_nentries,
  808. ch->partid, ch->number);
  809. if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_DISCONNECTED)) {
  810. spin_unlock_irqrestore(&ch->lock, irq_flags);
  811. return;
  812. }
  813. if (!(ch->flags & XPC_C_OPENREQUEST)) {
  814. XPC_DISCONNECT_CHANNEL(ch, xpcOpenCloseError,
  815. &irq_flags);
  816. spin_unlock_irqrestore(&ch->lock, irq_flags);
  817. return;
  818. }
  819. DBUG_ON(!(ch->flags & XPC_C_ROPENREQUEST));
  820. DBUG_ON(ch->flags & XPC_C_CONNECTED);
  821. /*
  822. * The meaningful OPENREPLY connection state fields are:
  823. * local_msgqueue_pa = physical address of remote
  824. * partition's local_msgqueue
  825. * local_nentries = remote partition's local_nentries
  826. * remote_nentries = remote partition's remote_nentries
  827. */
  828. DBUG_ON(args->local_msgqueue_pa == 0);
  829. DBUG_ON(args->local_nentries == 0);
  830. DBUG_ON(args->remote_nentries == 0);
  831. ch->flags |= XPC_C_ROPENREPLY;
  832. ch->remote_msgqueue_pa = args->local_msgqueue_pa;
  833. if (args->local_nentries < ch->remote_nentries) {
  834. dev_dbg(xpc_chan, "XPC_IPI_OPENREPLY: new "
  835. "remote_nentries=%d, old remote_nentries=%d, "
  836. "partid=%d, channel=%d\n",
  837. args->local_nentries, ch->remote_nentries,
  838. ch->partid, ch->number);
  839. ch->remote_nentries = args->local_nentries;
  840. }
  841. if (args->remote_nentries < ch->local_nentries) {
  842. dev_dbg(xpc_chan, "XPC_IPI_OPENREPLY: new "
  843. "local_nentries=%d, old local_nentries=%d, "
  844. "partid=%d, channel=%d\n",
  845. args->remote_nentries, ch->local_nentries,
  846. ch->partid, ch->number);
  847. ch->local_nentries = args->remote_nentries;
  848. }
  849. xpc_process_connect(ch, &irq_flags);
  850. }
  851. spin_unlock_irqrestore(&ch->lock, irq_flags);
  852. }
  853. /*
  854. * Attempt to establish a channel connection to a remote partition.
  855. */
  856. static enum xpc_retval
  857. xpc_connect_channel(struct xpc_channel *ch)
  858. {
  859. unsigned long irq_flags;
  860. struct xpc_registration *registration = &xpc_registrations[ch->number];
  861. if (down_trylock(&registration->sema) != 0) {
  862. return xpcRetry;
  863. }
  864. if (!XPC_CHANNEL_REGISTERED(ch->number)) {
  865. up(&registration->sema);
  866. return xpcUnregistered;
  867. }
  868. spin_lock_irqsave(&ch->lock, irq_flags);
  869. DBUG_ON(ch->flags & XPC_C_CONNECTED);
  870. DBUG_ON(ch->flags & XPC_C_OPENREQUEST);
  871. if (ch->flags & XPC_C_DISCONNECTING) {
  872. spin_unlock_irqrestore(&ch->lock, irq_flags);
  873. up(&registration->sema);
  874. return ch->reason;
  875. }
  876. /* add info from the channel connect registration to the channel */
  877. ch->kthreads_assigned_limit = registration->assigned_limit;
  878. ch->kthreads_idle_limit = registration->idle_limit;
  879. DBUG_ON(atomic_read(&ch->kthreads_assigned) != 0);
  880. DBUG_ON(atomic_read(&ch->kthreads_idle) != 0);
  881. DBUG_ON(atomic_read(&ch->kthreads_active) != 0);
  882. ch->func = registration->func;
  883. DBUG_ON(registration->func == NULL);
  884. ch->key = registration->key;
  885. ch->local_nentries = registration->nentries;
  886. if (ch->flags & XPC_C_ROPENREQUEST) {
  887. if (registration->msg_size != ch->msg_size) {
  888. /* the local and remote sides aren't the same */
  889. /*
  890. * Because XPC_DISCONNECT_CHANNEL() can block we're
  891. * forced to up the registration sema before we unlock
  892. * the channel lock. But that's okay here because we're
  893. * done with the part that required the registration
  894. * sema. XPC_DISCONNECT_CHANNEL() requires that the
  895. * channel lock be locked and will unlock and relock
  896. * the channel lock as needed.
  897. */
  898. up(&registration->sema);
  899. XPC_DISCONNECT_CHANNEL(ch, xpcUnequalMsgSizes,
  900. &irq_flags);
  901. spin_unlock_irqrestore(&ch->lock, irq_flags);
  902. return xpcUnequalMsgSizes;
  903. }
  904. } else {
  905. ch->msg_size = registration->msg_size;
  906. XPC_SET_REASON(ch, 0, 0);
  907. ch->flags &= ~XPC_C_DISCONNECTED;
  908. atomic_inc(&xpc_partitions[ch->partid].nchannels_active);
  909. }
  910. up(&registration->sema);
  911. /* initiate the connection */
  912. ch->flags |= (XPC_C_OPENREQUEST | XPC_C_CONNECTING);
  913. xpc_IPI_send_openrequest(ch, &irq_flags);
  914. xpc_process_connect(ch, &irq_flags);
  915. spin_unlock_irqrestore(&ch->lock, irq_flags);
  916. return xpcSuccess;
  917. }
  918. /*
  919. * Clear some of the msg flags in the local message queue.
  920. */
  921. static inline void
  922. xpc_clear_local_msgqueue_flags(struct xpc_channel *ch)
  923. {
  924. struct xpc_msg *msg;
  925. s64 get;
  926. get = ch->w_remote_GP.get;
  927. do {
  928. msg = (struct xpc_msg *) ((u64) ch->local_msgqueue +
  929. (get % ch->local_nentries) * ch->msg_size);
  930. msg->flags = 0;
  931. } while (++get < (volatile s64) ch->remote_GP.get);
  932. }
  933. /*
  934. * Clear some of the msg flags in the remote message queue.
  935. */
  936. static inline void
  937. xpc_clear_remote_msgqueue_flags(struct xpc_channel *ch)
  938. {
  939. struct xpc_msg *msg;
  940. s64 put;
  941. put = ch->w_remote_GP.put;
  942. do {
  943. msg = (struct xpc_msg *) ((u64) ch->remote_msgqueue +
  944. (put % ch->remote_nentries) * ch->msg_size);
  945. msg->flags = 0;
  946. } while (++put < (volatile s64) ch->remote_GP.put);
  947. }
  948. static void
  949. xpc_process_msg_IPI(struct xpc_partition *part, int ch_number)
  950. {
  951. struct xpc_channel *ch = &part->channels[ch_number];
  952. int nmsgs_sent;
  953. ch->remote_GP = part->remote_GPs[ch_number];
  954. /* See what, if anything, has changed for each connected channel */
  955. xpc_msgqueue_ref(ch);
  956. if (ch->w_remote_GP.get == ch->remote_GP.get &&
  957. ch->w_remote_GP.put == ch->remote_GP.put) {
  958. /* nothing changed since GPs were last pulled */
  959. xpc_msgqueue_deref(ch);
  960. return;
  961. }
  962. if (!(ch->flags & XPC_C_CONNECTED)){
  963. xpc_msgqueue_deref(ch);
  964. return;
  965. }
  966. /*
  967. * First check to see if messages recently sent by us have been
  968. * received by the other side. (The remote GET value will have
  969. * changed since we last looked at it.)
  970. */
  971. if (ch->w_remote_GP.get != ch->remote_GP.get) {
  972. /*
  973. * We need to notify any senders that want to be notified
  974. * that their sent messages have been received by their
  975. * intended recipients. We need to do this before updating
  976. * w_remote_GP.get so that we don't allocate the same message
  977. * queue entries prematurely (see xpc_allocate_msg()).
  978. */
  979. if (atomic_read(&ch->n_to_notify) > 0) {
  980. /*
  981. * Notify senders that messages sent have been
  982. * received and delivered by the other side.
  983. */
  984. xpc_notify_senders(ch, xpcMsgDelivered,
  985. ch->remote_GP.get);
  986. }
  987. /*
  988. * Clear msg->flags in previously sent messages, so that
  989. * they're ready for xpc_allocate_msg().
  990. */
  991. xpc_clear_local_msgqueue_flags(ch);
  992. ch->w_remote_GP.get = ch->remote_GP.get;
  993. dev_dbg(xpc_chan, "w_remote_GP.get changed to %ld, partid=%d, "
  994. "channel=%d\n", ch->w_remote_GP.get, ch->partid,
  995. ch->number);
  996. /*
  997. * If anyone was waiting for message queue entries to become
  998. * available, wake them up.
  999. */
  1000. if (atomic_read(&ch->n_on_msg_allocate_wq) > 0) {
  1001. wake_up(&ch->msg_allocate_wq);
  1002. }
  1003. }
  1004. /*
  1005. * Now check for newly sent messages by the other side. (The remote
  1006. * PUT value will have changed since we last looked at it.)
  1007. */
  1008. if (ch->w_remote_GP.put != ch->remote_GP.put) {
  1009. /*
  1010. * Clear msg->flags in previously received messages, so that
  1011. * they're ready for xpc_get_deliverable_msg().
  1012. */
  1013. xpc_clear_remote_msgqueue_flags(ch);
  1014. ch->w_remote_GP.put = ch->remote_GP.put;
  1015. dev_dbg(xpc_chan, "w_remote_GP.put changed to %ld, partid=%d, "
  1016. "channel=%d\n", ch->w_remote_GP.put, ch->partid,
  1017. ch->number);
  1018. nmsgs_sent = ch->w_remote_GP.put - ch->w_local_GP.get;
  1019. if (nmsgs_sent > 0) {
  1020. dev_dbg(xpc_chan, "msgs waiting to be copied and "
  1021. "delivered=%d, partid=%d, channel=%d\n",
  1022. nmsgs_sent, ch->partid, ch->number);
  1023. if (ch->flags & XPC_C_CONNECTCALLOUT) {
  1024. xpc_activate_kthreads(ch, nmsgs_sent);
  1025. }
  1026. }
  1027. }
  1028. xpc_msgqueue_deref(ch);
  1029. }
  1030. void
  1031. xpc_process_channel_activity(struct xpc_partition *part)
  1032. {
  1033. unsigned long irq_flags;
  1034. u64 IPI_amo, IPI_flags;
  1035. struct xpc_channel *ch;
  1036. int ch_number;
  1037. u32 ch_flags;
  1038. IPI_amo = xpc_get_IPI_flags(part);
  1039. /*
  1040. * Initiate channel connections for registered channels.
  1041. *
  1042. * For each connected channel that has pending messages activate idle
  1043. * kthreads and/or create new kthreads as needed.
  1044. */
  1045. for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
  1046. ch = &part->channels[ch_number];
  1047. /*
  1048. * Process any open or close related IPI flags, and then deal
  1049. * with connecting or disconnecting the channel as required.
  1050. */
  1051. IPI_flags = XPC_GET_IPI_FLAGS(IPI_amo, ch_number);
  1052. if (XPC_ANY_OPENCLOSE_IPI_FLAGS_SET(IPI_flags)) {
  1053. xpc_process_openclose_IPI(part, ch_number, IPI_flags);
  1054. }
  1055. ch_flags = ch->flags; /* need an atomic snapshot of flags */
  1056. if (ch_flags & XPC_C_DISCONNECTING) {
  1057. spin_lock_irqsave(&ch->lock, irq_flags);
  1058. xpc_process_disconnect(ch, &irq_flags);
  1059. spin_unlock_irqrestore(&ch->lock, irq_flags);
  1060. continue;
  1061. }
  1062. if (part->act_state == XPC_P_DEACTIVATING) {
  1063. continue;
  1064. }
  1065. if (!(ch_flags & XPC_C_CONNECTED)) {
  1066. if (!(ch_flags & XPC_C_OPENREQUEST)) {
  1067. DBUG_ON(ch_flags & XPC_C_SETUP);
  1068. (void) xpc_connect_channel(ch);
  1069. } else {
  1070. spin_lock_irqsave(&ch->lock, irq_flags);
  1071. xpc_process_connect(ch, &irq_flags);
  1072. spin_unlock_irqrestore(&ch->lock, irq_flags);
  1073. }
  1074. continue;
  1075. }
  1076. /*
  1077. * Process any message related IPI flags, this may involve the
  1078. * activation of kthreads to deliver any pending messages sent
  1079. * from the other partition.
  1080. */
  1081. if (XPC_ANY_MSG_IPI_FLAGS_SET(IPI_flags)) {
  1082. xpc_process_msg_IPI(part, ch_number);
  1083. }
  1084. }
  1085. }
  1086. /*
  1087. * XPC's heartbeat code calls this function to inform XPC that a partition is
  1088. * going down. XPC responds by tearing down the XPartition Communication
  1089. * infrastructure used for the just downed partition.
  1090. *
  1091. * XPC's heartbeat code will never call this function and xpc_partition_up()
  1092. * at the same time. Nor will it ever make multiple calls to either function
  1093. * at the same time.
  1094. */
  1095. void
  1096. xpc_partition_going_down(struct xpc_partition *part, enum xpc_retval reason)
  1097. {
  1098. unsigned long irq_flags;
  1099. int ch_number;
  1100. struct xpc_channel *ch;
  1101. dev_dbg(xpc_chan, "deactivating partition %d, reason=%d\n",
  1102. XPC_PARTID(part), reason);
  1103. if (!xpc_part_ref(part)) {
  1104. /* infrastructure for this partition isn't currently set up */
  1105. return;
  1106. }
  1107. /* disconnect channels associated with the partition going down */
  1108. for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
  1109. ch = &part->channels[ch_number];
  1110. xpc_msgqueue_ref(ch);
  1111. spin_lock_irqsave(&ch->lock, irq_flags);
  1112. XPC_DISCONNECT_CHANNEL(ch, reason, &irq_flags);
  1113. spin_unlock_irqrestore(&ch->lock, irq_flags);
  1114. xpc_msgqueue_deref(ch);
  1115. }
  1116. xpc_wakeup_channel_mgr(part);
  1117. xpc_part_deref(part);
  1118. }
  1119. /*
  1120. * Teardown the infrastructure necessary to support XPartition Communication
  1121. * between the specified remote partition and the local one.
  1122. */
  1123. void
  1124. xpc_teardown_infrastructure(struct xpc_partition *part)
  1125. {
  1126. partid_t partid = XPC_PARTID(part);
  1127. /*
  1128. * We start off by making this partition inaccessible to local
  1129. * processes by marking it as no longer setup. Then we make it
  1130. * inaccessible to remote processes by clearing the XPC per partition
  1131. * specific variable's magic # (which indicates that these variables
  1132. * are no longer valid) and by ignoring all XPC notify IPIs sent to
  1133. * this partition.
  1134. */
  1135. DBUG_ON(atomic_read(&part->nchannels_engaged) != 0);
  1136. DBUG_ON(atomic_read(&part->nchannels_active) != 0);
  1137. DBUG_ON(part->setup_state != XPC_P_SETUP);
  1138. part->setup_state = XPC_P_WTEARDOWN;
  1139. xpc_vars_part[partid].magic = 0;
  1140. free_irq(SGI_XPC_NOTIFY, (void *) (u64) partid);
  1141. /*
  1142. * Before proceding with the teardown we have to wait until all
  1143. * existing references cease.
  1144. */
  1145. wait_event(part->teardown_wq, (atomic_read(&part->references) == 0));
  1146. /* now we can begin tearing down the infrastructure */
  1147. part->setup_state = XPC_P_TORNDOWN;
  1148. /* in case we've still got outstanding timers registered... */
  1149. del_timer_sync(&part->dropped_IPI_timer);
  1150. kfree(part->remote_openclose_args_base);
  1151. part->remote_openclose_args = NULL;
  1152. kfree(part->local_openclose_args_base);
  1153. part->local_openclose_args = NULL;
  1154. kfree(part->remote_GPs_base);
  1155. part->remote_GPs = NULL;
  1156. kfree(part->local_GPs_base);
  1157. part->local_GPs = NULL;
  1158. kfree(part->channels);
  1159. part->channels = NULL;
  1160. part->local_IPI_amo_va = NULL;
  1161. }
  1162. /*
  1163. * Called by XP at the time of channel connection registration to cause
  1164. * XPC to establish connections to all currently active partitions.
  1165. */
  1166. void
  1167. xpc_initiate_connect(int ch_number)
  1168. {
  1169. partid_t partid;
  1170. struct xpc_partition *part;
  1171. struct xpc_channel *ch;
  1172. DBUG_ON(ch_number < 0 || ch_number >= XPC_NCHANNELS);
  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. /*
  1178. * Initiate the establishment of a connection on the
  1179. * newly registered channel to the remote partition.
  1180. */
  1181. xpc_wakeup_channel_mgr(part);
  1182. xpc_part_deref(part);
  1183. }
  1184. }
  1185. }
  1186. void
  1187. xpc_connected_callout(struct xpc_channel *ch)
  1188. {
  1189. /* let the registerer know that a connection has been established */
  1190. if (ch->func != NULL) {
  1191. dev_dbg(xpc_chan, "ch->func() called, reason=xpcConnected, "
  1192. "partid=%d, channel=%d\n", ch->partid, ch->number);
  1193. ch->func(xpcConnected, ch->partid, ch->number,
  1194. (void *) (u64) ch->local_nentries, ch->key);
  1195. dev_dbg(xpc_chan, "ch->func() returned, reason=xpcConnected, "
  1196. "partid=%d, channel=%d\n", ch->partid, ch->number);
  1197. }
  1198. }
  1199. /*
  1200. * Called by XP at the time of channel connection unregistration to cause
  1201. * XPC to teardown all current connections for the specified channel.
  1202. *
  1203. * Before returning xpc_initiate_disconnect() will wait until all connections
  1204. * on the specified channel have been closed/torndown. So the caller can be
  1205. * assured that they will not be receiving any more callouts from XPC to the
  1206. * function they registered via xpc_connect().
  1207. *
  1208. * Arguments:
  1209. *
  1210. * ch_number - channel # to unregister.
  1211. */
  1212. void
  1213. xpc_initiate_disconnect(int ch_number)
  1214. {
  1215. unsigned long irq_flags;
  1216. partid_t partid;
  1217. struct xpc_partition *part;
  1218. struct xpc_channel *ch;
  1219. DBUG_ON(ch_number < 0 || ch_number >= XPC_NCHANNELS);
  1220. /* initiate the channel disconnect for every active partition */
  1221. for (partid = 1; partid < XP_MAX_PARTITIONS; partid++) {
  1222. part = &xpc_partitions[partid];
  1223. if (xpc_part_ref(part)) {
  1224. ch = &part->channels[ch_number];
  1225. xpc_msgqueue_ref(ch);
  1226. spin_lock_irqsave(&ch->lock, irq_flags);
  1227. if (!(ch->flags & XPC_C_DISCONNECTED)) {
  1228. ch->flags |= XPC_C_WDISCONNECT;
  1229. XPC_DISCONNECT_CHANNEL(ch, xpcUnregistering,
  1230. &irq_flags);
  1231. }
  1232. spin_unlock_irqrestore(&ch->lock, irq_flags);
  1233. xpc_msgqueue_deref(ch);
  1234. xpc_part_deref(part);
  1235. }
  1236. }
  1237. xpc_disconnect_wait(ch_number);
  1238. }
  1239. /*
  1240. * To disconnect a channel, and reflect it back to all who may be waiting.
  1241. *
  1242. * An OPEN is not allowed until XPC_C_DISCONNECTING is cleared by
  1243. * xpc_process_disconnect(), and if set, XPC_C_WDISCONNECT is cleared by
  1244. * xpc_disconnect_wait().
  1245. *
  1246. * THE CHANNEL IS TO BE LOCKED BY THE CALLER AND WILL REMAIN LOCKED UPON RETURN.
  1247. */
  1248. void
  1249. xpc_disconnect_channel(const int line, struct xpc_channel *ch,
  1250. enum xpc_retval reason, unsigned long *irq_flags)
  1251. {
  1252. u32 channel_was_connected = (ch->flags & XPC_C_CONNECTED);
  1253. DBUG_ON(!spin_is_locked(&ch->lock));
  1254. if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_DISCONNECTED)) {
  1255. return;
  1256. }
  1257. DBUG_ON(!(ch->flags & (XPC_C_CONNECTING | XPC_C_CONNECTED)));
  1258. dev_dbg(xpc_chan, "reason=%d, line=%d, partid=%d, channel=%d\n",
  1259. reason, line, ch->partid, ch->number);
  1260. XPC_SET_REASON(ch, reason, line);
  1261. ch->flags |= (XPC_C_CLOSEREQUEST | XPC_C_DISCONNECTING);
  1262. /* some of these may not have been set */
  1263. ch->flags &= ~(XPC_C_OPENREQUEST | XPC_C_OPENREPLY |
  1264. XPC_C_ROPENREQUEST | XPC_C_ROPENREPLY |
  1265. XPC_C_CONNECTING | XPC_C_CONNECTED);
  1266. xpc_IPI_send_closerequest(ch, irq_flags);
  1267. if (channel_was_connected) {
  1268. ch->flags |= XPC_C_WASCONNECTED;
  1269. }
  1270. spin_unlock_irqrestore(&ch->lock, *irq_flags);
  1271. /* wake all idle kthreads so they can exit */
  1272. if (atomic_read(&ch->kthreads_idle) > 0) {
  1273. wake_up_all(&ch->idle_wq);
  1274. }
  1275. /* wake those waiting to allocate an entry from the local msg queue */
  1276. if (atomic_read(&ch->n_on_msg_allocate_wq) > 0) {
  1277. wake_up(&ch->msg_allocate_wq);
  1278. }
  1279. spin_lock_irqsave(&ch->lock, *irq_flags);
  1280. }
  1281. void
  1282. xpc_disconnect_callout(struct xpc_channel *ch, enum xpc_retval reason)
  1283. {
  1284. /*
  1285. * Let the channel's registerer know that the channel is being
  1286. * disconnected. We don't want to do this if the registerer was never
  1287. * informed of a connection being made.
  1288. */
  1289. if (ch->func != NULL) {
  1290. dev_dbg(xpc_chan, "ch->func() called, reason=%d, partid=%d, "
  1291. "channel=%d\n", reason, ch->partid, ch->number);
  1292. ch->func(reason, ch->partid, ch->number, NULL, ch->key);
  1293. dev_dbg(xpc_chan, "ch->func() returned, reason=%d, partid=%d, "
  1294. "channel=%d\n", reason, ch->partid, ch->number);
  1295. }
  1296. }
  1297. /*
  1298. * Wait for a message entry to become available for the specified channel,
  1299. * but don't wait any longer than 1 jiffy.
  1300. */
  1301. static enum xpc_retval
  1302. xpc_allocate_msg_wait(struct xpc_channel *ch)
  1303. {
  1304. enum xpc_retval ret;
  1305. if (ch->flags & XPC_C_DISCONNECTING) {
  1306. DBUG_ON(ch->reason == xpcInterrupted); // >>> Is this true?
  1307. return ch->reason;
  1308. }
  1309. atomic_inc(&ch->n_on_msg_allocate_wq);
  1310. ret = interruptible_sleep_on_timeout(&ch->msg_allocate_wq, 1);
  1311. atomic_dec(&ch->n_on_msg_allocate_wq);
  1312. if (ch->flags & XPC_C_DISCONNECTING) {
  1313. ret = ch->reason;
  1314. DBUG_ON(ch->reason == xpcInterrupted); // >>> Is this true?
  1315. } else if (ret == 0) {
  1316. ret = xpcTimeout;
  1317. } else {
  1318. ret = xpcInterrupted;
  1319. }
  1320. return ret;
  1321. }
  1322. /*
  1323. * Allocate an entry for a message from the message queue associated with the
  1324. * specified channel.
  1325. */
  1326. static enum xpc_retval
  1327. xpc_allocate_msg(struct xpc_channel *ch, u32 flags,
  1328. struct xpc_msg **address_of_msg)
  1329. {
  1330. struct xpc_msg *msg;
  1331. enum xpc_retval ret;
  1332. s64 put;
  1333. /* this reference will be dropped in xpc_send_msg() */
  1334. xpc_msgqueue_ref(ch);
  1335. if (ch->flags & XPC_C_DISCONNECTING) {
  1336. xpc_msgqueue_deref(ch);
  1337. return ch->reason;
  1338. }
  1339. if (!(ch->flags & XPC_C_CONNECTED)) {
  1340. xpc_msgqueue_deref(ch);
  1341. return xpcNotConnected;
  1342. }
  1343. /*
  1344. * Get the next available message entry from the local message queue.
  1345. * If none are available, we'll make sure that we grab the latest
  1346. * GP values.
  1347. */
  1348. ret = xpcTimeout;
  1349. while (1) {
  1350. put = (volatile s64) ch->w_local_GP.put;
  1351. if (put - (volatile s64) ch->w_remote_GP.get <
  1352. ch->local_nentries) {
  1353. /* There are available message entries. We need to try
  1354. * to secure one for ourselves. We'll do this by trying
  1355. * to increment w_local_GP.put as long as someone else
  1356. * doesn't beat us to it. If they do, we'll have to
  1357. * try again.
  1358. */
  1359. if (cmpxchg(&ch->w_local_GP.put, put, put + 1) ==
  1360. put) {
  1361. /* we got the entry referenced by put */
  1362. break;
  1363. }
  1364. continue; /* try again */
  1365. }
  1366. /*
  1367. * There aren't any available msg entries at this time.
  1368. *
  1369. * In waiting for a message entry to become available,
  1370. * we set a timeout in case the other side is not
  1371. * sending completion IPIs. This lets us fake an IPI
  1372. * that will cause the IPI handler to fetch the latest
  1373. * GP values as if an IPI was sent by the other side.
  1374. */
  1375. if (ret == xpcTimeout) {
  1376. xpc_IPI_send_local_msgrequest(ch);
  1377. }
  1378. if (flags & XPC_NOWAIT) {
  1379. xpc_msgqueue_deref(ch);
  1380. return xpcNoWait;
  1381. }
  1382. ret = xpc_allocate_msg_wait(ch);
  1383. if (ret != xpcInterrupted && ret != xpcTimeout) {
  1384. xpc_msgqueue_deref(ch);
  1385. return ret;
  1386. }
  1387. }
  1388. /* get the message's address and initialize it */
  1389. msg = (struct xpc_msg *) ((u64) ch->local_msgqueue +
  1390. (put % ch->local_nentries) * ch->msg_size);
  1391. DBUG_ON(msg->flags != 0);
  1392. msg->number = put;
  1393. dev_dbg(xpc_chan, "w_local_GP.put changed to %ld; msg=0x%p, "
  1394. "msg_number=%ld, partid=%d, channel=%d\n", put + 1,
  1395. (void *) msg, msg->number, ch->partid, ch->number);
  1396. *address_of_msg = msg;
  1397. return xpcSuccess;
  1398. }
  1399. /*
  1400. * Allocate an entry for a message from the message queue associated with the
  1401. * specified channel. NOTE that this routine can sleep waiting for a message
  1402. * entry to become available. To not sleep, pass in the XPC_NOWAIT flag.
  1403. *
  1404. * Arguments:
  1405. *
  1406. * partid - ID of partition to which the channel is connected.
  1407. * ch_number - channel #.
  1408. * flags - see xpc.h for valid flags.
  1409. * payload - address of the allocated payload area pointer (filled in on
  1410. * return) in which the user-defined message is constructed.
  1411. */
  1412. enum xpc_retval
  1413. xpc_initiate_allocate(partid_t partid, int ch_number, u32 flags, void **payload)
  1414. {
  1415. struct xpc_partition *part = &xpc_partitions[partid];
  1416. enum xpc_retval ret = xpcUnknownReason;
  1417. struct xpc_msg *msg;
  1418. DBUG_ON(partid <= 0 || partid >= XP_MAX_PARTITIONS);
  1419. DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
  1420. *payload = NULL;
  1421. if (xpc_part_ref(part)) {
  1422. ret = xpc_allocate_msg(&part->channels[ch_number], flags, &msg);
  1423. xpc_part_deref(part);
  1424. if (msg != NULL) {
  1425. *payload = &msg->payload;
  1426. }
  1427. }
  1428. return ret;
  1429. }
  1430. /*
  1431. * Now we actually send the messages that are ready to be sent by advancing
  1432. * the local message queue's Put value and then send an IPI to the recipient
  1433. * partition.
  1434. */
  1435. static void
  1436. xpc_send_msgs(struct xpc_channel *ch, s64 initial_put)
  1437. {
  1438. struct xpc_msg *msg;
  1439. s64 put = initial_put + 1;
  1440. int send_IPI = 0;
  1441. while (1) {
  1442. while (1) {
  1443. if (put == (volatile s64) ch->w_local_GP.put) {
  1444. break;
  1445. }
  1446. msg = (struct xpc_msg *) ((u64) ch->local_msgqueue +
  1447. (put % ch->local_nentries) * ch->msg_size);
  1448. if (!(msg->flags & XPC_M_READY)) {
  1449. break;
  1450. }
  1451. put++;
  1452. }
  1453. if (put == initial_put) {
  1454. /* nothing's changed */
  1455. break;
  1456. }
  1457. if (cmpxchg_rel(&ch->local_GP->put, initial_put, put) !=
  1458. initial_put) {
  1459. /* someone else beat us to it */
  1460. DBUG_ON((volatile s64) ch->local_GP->put < initial_put);
  1461. break;
  1462. }
  1463. /* we just set the new value of local_GP->put */
  1464. dev_dbg(xpc_chan, "local_GP->put changed to %ld, partid=%d, "
  1465. "channel=%d\n", put, ch->partid, ch->number);
  1466. send_IPI = 1;
  1467. /*
  1468. * We need to ensure that the message referenced by
  1469. * local_GP->put is not XPC_M_READY or that local_GP->put
  1470. * equals w_local_GP.put, so we'll go have a look.
  1471. */
  1472. initial_put = put;
  1473. }
  1474. if (send_IPI) {
  1475. xpc_IPI_send_msgrequest(ch);
  1476. }
  1477. }
  1478. /*
  1479. * Common code that does the actual sending of the message by advancing the
  1480. * local message queue's Put value and sends an IPI to the partition the
  1481. * message is being sent to.
  1482. */
  1483. static enum xpc_retval
  1484. xpc_send_msg(struct xpc_channel *ch, struct xpc_msg *msg, u8 notify_type,
  1485. xpc_notify_func func, void *key)
  1486. {
  1487. enum xpc_retval ret = xpcSuccess;
  1488. struct xpc_notify *notify = notify;
  1489. s64 put, msg_number = msg->number;
  1490. DBUG_ON(notify_type == XPC_N_CALL && func == NULL);
  1491. DBUG_ON((((u64) msg - (u64) ch->local_msgqueue) / ch->msg_size) !=
  1492. msg_number % ch->local_nentries);
  1493. DBUG_ON(msg->flags & XPC_M_READY);
  1494. if (ch->flags & XPC_C_DISCONNECTING) {
  1495. /* drop the reference grabbed in xpc_allocate_msg() */
  1496. xpc_msgqueue_deref(ch);
  1497. return ch->reason;
  1498. }
  1499. if (notify_type != 0) {
  1500. /*
  1501. * Tell the remote side to send an ACK interrupt when the
  1502. * message has been delivered.
  1503. */
  1504. msg->flags |= XPC_M_INTERRUPT;
  1505. atomic_inc(&ch->n_to_notify);
  1506. notify = &ch->notify_queue[msg_number % ch->local_nentries];
  1507. notify->func = func;
  1508. notify->key = key;
  1509. notify->type = notify_type;
  1510. // >>> is a mb() needed here?
  1511. if (ch->flags & XPC_C_DISCONNECTING) {
  1512. /*
  1513. * An error occurred between our last error check and
  1514. * this one. We will try to clear the type field from
  1515. * the notify entry. If we succeed then
  1516. * xpc_disconnect_channel() didn't already process
  1517. * the notify entry.
  1518. */
  1519. if (cmpxchg(&notify->type, notify_type, 0) ==
  1520. notify_type) {
  1521. atomic_dec(&ch->n_to_notify);
  1522. ret = ch->reason;
  1523. }
  1524. /* drop the reference grabbed in xpc_allocate_msg() */
  1525. xpc_msgqueue_deref(ch);
  1526. return ret;
  1527. }
  1528. }
  1529. msg->flags |= XPC_M_READY;
  1530. /*
  1531. * The preceding store of msg->flags must occur before the following
  1532. * load of ch->local_GP->put.
  1533. */
  1534. mb();
  1535. /* see if the message is next in line to be sent, if so send it */
  1536. put = ch->local_GP->put;
  1537. if (put == msg_number) {
  1538. xpc_send_msgs(ch, put);
  1539. }
  1540. /* drop the reference grabbed in xpc_allocate_msg() */
  1541. xpc_msgqueue_deref(ch);
  1542. return ret;
  1543. }
  1544. /*
  1545. * Send a message previously allocated using xpc_initiate_allocate() on the
  1546. * specified channel connected to the specified partition.
  1547. *
  1548. * This routine will not wait for the message to be received, nor will
  1549. * notification be given when it does happen. Once this routine has returned
  1550. * the message entry allocated via xpc_initiate_allocate() is no longer
  1551. * accessable to the caller.
  1552. *
  1553. * This routine, although called by users, does not call xpc_part_ref() to
  1554. * ensure that the partition infrastructure is in place. It relies on the
  1555. * fact that we called xpc_msgqueue_ref() in xpc_allocate_msg().
  1556. *
  1557. * Arguments:
  1558. *
  1559. * partid - ID of partition to which the channel is connected.
  1560. * ch_number - channel # to send message on.
  1561. * payload - pointer to the payload area allocated via
  1562. * xpc_initiate_allocate().
  1563. */
  1564. enum xpc_retval
  1565. xpc_initiate_send(partid_t partid, int ch_number, void *payload)
  1566. {
  1567. struct xpc_partition *part = &xpc_partitions[partid];
  1568. struct xpc_msg *msg = XPC_MSG_ADDRESS(payload);
  1569. enum xpc_retval ret;
  1570. dev_dbg(xpc_chan, "msg=0x%p, partid=%d, channel=%d\n", (void *) msg,
  1571. partid, ch_number);
  1572. DBUG_ON(partid <= 0 || partid >= XP_MAX_PARTITIONS);
  1573. DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
  1574. DBUG_ON(msg == NULL);
  1575. ret = xpc_send_msg(&part->channels[ch_number], msg, 0, NULL, NULL);
  1576. return ret;
  1577. }
  1578. /*
  1579. * Send a message previously allocated using xpc_initiate_allocate on the
  1580. * specified channel connected to the specified partition.
  1581. *
  1582. * This routine will not wait for the message to be sent. Once this routine
  1583. * has returned the message entry allocated via xpc_initiate_allocate() is no
  1584. * longer accessable to the caller.
  1585. *
  1586. * Once the remote end of the channel has received the message, the function
  1587. * passed as an argument to xpc_initiate_send_notify() will be called. This
  1588. * allows the sender to free up or re-use any buffers referenced by the
  1589. * message, but does NOT mean the message has been processed at the remote
  1590. * end by a receiver.
  1591. *
  1592. * If this routine returns an error, the caller's function will NOT be called.
  1593. *
  1594. * This routine, although called by users, does not call xpc_part_ref() to
  1595. * ensure that the partition infrastructure is in place. It relies on the
  1596. * fact that we called xpc_msgqueue_ref() in xpc_allocate_msg().
  1597. *
  1598. * Arguments:
  1599. *
  1600. * partid - ID of partition to which the channel is connected.
  1601. * ch_number - channel # to send message on.
  1602. * payload - pointer to the payload area allocated via
  1603. * xpc_initiate_allocate().
  1604. * func - function to call with asynchronous notification of message
  1605. * receipt. THIS FUNCTION MUST BE NON-BLOCKING.
  1606. * key - user-defined key to be passed to the function when it's called.
  1607. */
  1608. enum xpc_retval
  1609. xpc_initiate_send_notify(partid_t partid, int ch_number, void *payload,
  1610. xpc_notify_func func, void *key)
  1611. {
  1612. struct xpc_partition *part = &xpc_partitions[partid];
  1613. struct xpc_msg *msg = XPC_MSG_ADDRESS(payload);
  1614. enum xpc_retval ret;
  1615. dev_dbg(xpc_chan, "msg=0x%p, partid=%d, channel=%d\n", (void *) msg,
  1616. partid, ch_number);
  1617. DBUG_ON(partid <= 0 || partid >= XP_MAX_PARTITIONS);
  1618. DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
  1619. DBUG_ON(msg == NULL);
  1620. DBUG_ON(func == NULL);
  1621. ret = xpc_send_msg(&part->channels[ch_number], msg, XPC_N_CALL,
  1622. func, key);
  1623. return ret;
  1624. }
  1625. static struct xpc_msg *
  1626. xpc_pull_remote_msg(struct xpc_channel *ch, s64 get)
  1627. {
  1628. struct xpc_partition *part = &xpc_partitions[ch->partid];
  1629. struct xpc_msg *remote_msg, *msg;
  1630. u32 msg_index, nmsgs;
  1631. u64 msg_offset;
  1632. enum xpc_retval ret;
  1633. if (down_interruptible(&ch->msg_to_pull_sema) != 0) {
  1634. /* we were interrupted by a signal */
  1635. return NULL;
  1636. }
  1637. while (get >= ch->next_msg_to_pull) {
  1638. /* pull as many messages as are ready and able to be pulled */
  1639. msg_index = ch->next_msg_to_pull % ch->remote_nentries;
  1640. DBUG_ON(ch->next_msg_to_pull >=
  1641. (volatile s64) ch->w_remote_GP.put);
  1642. nmsgs = (volatile s64) ch->w_remote_GP.put -
  1643. ch->next_msg_to_pull;
  1644. if (msg_index + nmsgs > ch->remote_nentries) {
  1645. /* ignore the ones that wrap the msg queue for now */
  1646. nmsgs = ch->remote_nentries - msg_index;
  1647. }
  1648. msg_offset = msg_index * ch->msg_size;
  1649. msg = (struct xpc_msg *) ((u64) ch->remote_msgqueue +
  1650. msg_offset);
  1651. remote_msg = (struct xpc_msg *) (ch->remote_msgqueue_pa +
  1652. msg_offset);
  1653. if ((ret = xpc_pull_remote_cachelines(part, msg, remote_msg,
  1654. nmsgs * ch->msg_size)) != xpcSuccess) {
  1655. dev_dbg(xpc_chan, "failed to pull %d msgs starting with"
  1656. " msg %ld from partition %d, channel=%d, "
  1657. "ret=%d\n", nmsgs, ch->next_msg_to_pull,
  1658. ch->partid, ch->number, ret);
  1659. XPC_DEACTIVATE_PARTITION(part, ret);
  1660. up(&ch->msg_to_pull_sema);
  1661. return NULL;
  1662. }
  1663. mb(); /* >>> this may not be needed, we're not sure */
  1664. ch->next_msg_to_pull += nmsgs;
  1665. }
  1666. up(&ch->msg_to_pull_sema);
  1667. /* return the message we were looking for */
  1668. msg_offset = (get % ch->remote_nentries) * ch->msg_size;
  1669. msg = (struct xpc_msg *) ((u64) ch->remote_msgqueue + msg_offset);
  1670. return msg;
  1671. }
  1672. /*
  1673. * Get a message to be delivered.
  1674. */
  1675. static struct xpc_msg *
  1676. xpc_get_deliverable_msg(struct xpc_channel *ch)
  1677. {
  1678. struct xpc_msg *msg = NULL;
  1679. s64 get;
  1680. do {
  1681. if ((volatile u32) ch->flags & XPC_C_DISCONNECTING) {
  1682. break;
  1683. }
  1684. get = (volatile s64) ch->w_local_GP.get;
  1685. if (get == (volatile s64) ch->w_remote_GP.put) {
  1686. break;
  1687. }
  1688. /* There are messages waiting to be pulled and delivered.
  1689. * We need to try to secure one for ourselves. We'll do this
  1690. * by trying to increment w_local_GP.get and hope that no one
  1691. * else beats us to it. If they do, we'll we'll simply have
  1692. * to try again for the next one.
  1693. */
  1694. if (cmpxchg(&ch->w_local_GP.get, get, get + 1) == get) {
  1695. /* we got the entry referenced by get */
  1696. dev_dbg(xpc_chan, "w_local_GP.get changed to %ld, "
  1697. "partid=%d, channel=%d\n", get + 1,
  1698. ch->partid, ch->number);
  1699. /* pull the message from the remote partition */
  1700. msg = xpc_pull_remote_msg(ch, get);
  1701. DBUG_ON(msg != NULL && msg->number != get);
  1702. DBUG_ON(msg != NULL && (msg->flags & XPC_M_DONE));
  1703. DBUG_ON(msg != NULL && !(msg->flags & XPC_M_READY));
  1704. break;
  1705. }
  1706. } while (1);
  1707. return msg;
  1708. }
  1709. /*
  1710. * Deliver a message to its intended recipient.
  1711. */
  1712. void
  1713. xpc_deliver_msg(struct xpc_channel *ch)
  1714. {
  1715. struct xpc_msg *msg;
  1716. if ((msg = xpc_get_deliverable_msg(ch)) != NULL) {
  1717. /*
  1718. * This ref is taken to protect the payload itself from being
  1719. * freed before the user is finished with it, which the user
  1720. * indicates by calling xpc_initiate_received().
  1721. */
  1722. xpc_msgqueue_ref(ch);
  1723. atomic_inc(&ch->kthreads_active);
  1724. if (ch->func != NULL) {
  1725. dev_dbg(xpc_chan, "ch->func() called, msg=0x%p, "
  1726. "msg_number=%ld, partid=%d, channel=%d\n",
  1727. (void *) msg, msg->number, ch->partid,
  1728. ch->number);
  1729. /* deliver the message to its intended recipient */
  1730. ch->func(xpcMsgReceived, ch->partid, ch->number,
  1731. &msg->payload, ch->key);
  1732. dev_dbg(xpc_chan, "ch->func() returned, msg=0x%p, "
  1733. "msg_number=%ld, partid=%d, channel=%d\n",
  1734. (void *) msg, msg->number, ch->partid,
  1735. ch->number);
  1736. }
  1737. atomic_dec(&ch->kthreads_active);
  1738. }
  1739. }
  1740. /*
  1741. * Now we actually acknowledge the messages that have been delivered and ack'd
  1742. * by advancing the cached remote message queue's Get value and if requested
  1743. * send an IPI to the message sender's partition.
  1744. */
  1745. static void
  1746. xpc_acknowledge_msgs(struct xpc_channel *ch, s64 initial_get, u8 msg_flags)
  1747. {
  1748. struct xpc_msg *msg;
  1749. s64 get = initial_get + 1;
  1750. int send_IPI = 0;
  1751. while (1) {
  1752. while (1) {
  1753. if (get == (volatile s64) ch->w_local_GP.get) {
  1754. break;
  1755. }
  1756. msg = (struct xpc_msg *) ((u64) ch->remote_msgqueue +
  1757. (get % ch->remote_nentries) * ch->msg_size);
  1758. if (!(msg->flags & XPC_M_DONE)) {
  1759. break;
  1760. }
  1761. msg_flags |= msg->flags;
  1762. get++;
  1763. }
  1764. if (get == initial_get) {
  1765. /* nothing's changed */
  1766. break;
  1767. }
  1768. if (cmpxchg_rel(&ch->local_GP->get, initial_get, get) !=
  1769. initial_get) {
  1770. /* someone else beat us to it */
  1771. DBUG_ON((volatile s64) ch->local_GP->get <=
  1772. initial_get);
  1773. break;
  1774. }
  1775. /* we just set the new value of local_GP->get */
  1776. dev_dbg(xpc_chan, "local_GP->get changed to %ld, partid=%d, "
  1777. "channel=%d\n", get, ch->partid, ch->number);
  1778. send_IPI = (msg_flags & XPC_M_INTERRUPT);
  1779. /*
  1780. * We need to ensure that the message referenced by
  1781. * local_GP->get is not XPC_M_DONE or that local_GP->get
  1782. * equals w_local_GP.get, so we'll go have a look.
  1783. */
  1784. initial_get = get;
  1785. }
  1786. if (send_IPI) {
  1787. xpc_IPI_send_msgrequest(ch);
  1788. }
  1789. }
  1790. /*
  1791. * Acknowledge receipt of a delivered message.
  1792. *
  1793. * If a message has XPC_M_INTERRUPT set, send an interrupt to the partition
  1794. * that sent the message.
  1795. *
  1796. * This function, although called by users, does not call xpc_part_ref() to
  1797. * ensure that the partition infrastructure is in place. It relies on the
  1798. * fact that we called xpc_msgqueue_ref() in xpc_deliver_msg().
  1799. *
  1800. * Arguments:
  1801. *
  1802. * partid - ID of partition to which the channel is connected.
  1803. * ch_number - channel # message received on.
  1804. * payload - pointer to the payload area allocated via
  1805. * xpc_initiate_allocate().
  1806. */
  1807. void
  1808. xpc_initiate_received(partid_t partid, int ch_number, void *payload)
  1809. {
  1810. struct xpc_partition *part = &xpc_partitions[partid];
  1811. struct xpc_channel *ch;
  1812. struct xpc_msg *msg = XPC_MSG_ADDRESS(payload);
  1813. s64 get, msg_number = msg->number;
  1814. DBUG_ON(partid <= 0 || partid >= XP_MAX_PARTITIONS);
  1815. DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
  1816. ch = &part->channels[ch_number];
  1817. dev_dbg(xpc_chan, "msg=0x%p, msg_number=%ld, partid=%d, channel=%d\n",
  1818. (void *) msg, msg_number, ch->partid, ch->number);
  1819. DBUG_ON((((u64) msg - (u64) ch->remote_msgqueue) / ch->msg_size) !=
  1820. msg_number % ch->remote_nentries);
  1821. DBUG_ON(msg->flags & XPC_M_DONE);
  1822. msg->flags |= XPC_M_DONE;
  1823. /*
  1824. * The preceding store of msg->flags must occur before the following
  1825. * load of ch->local_GP->get.
  1826. */
  1827. mb();
  1828. /*
  1829. * See if this message is next in line to be acknowledged as having
  1830. * been delivered.
  1831. */
  1832. get = ch->local_GP->get;
  1833. if (get == msg_number) {
  1834. xpc_acknowledge_msgs(ch, get, msg->flags);
  1835. }
  1836. /* the call to xpc_msgqueue_ref() was done by xpc_deliver_msg() */
  1837. xpc_msgqueue_deref(ch);
  1838. }