xpc_channel.c 60 KB

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