xpc_channel.c 61 KB

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