xpc_channel.c 61 KB

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