xpc_channel.c 60 KB

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