xpc_channel.c 58 KB

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