xpc_channel.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (c) 2004-2008 Silicon Graphics, Inc. All Rights Reserved.
  7. */
  8. /*
  9. * Cross Partition Communication (XPC) channel support.
  10. *
  11. * This is the part of XPC that manages the channels and
  12. * sends/receives messages across them to/from other partitions.
  13. *
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/init.h>
  17. #include <linux/sched.h>
  18. #include <linux/cache.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/mutex.h>
  21. #include <linux/completion.h>
  22. #include <asm/sn/sn_sal.h>
  23. #include "xpc.h"
  24. /*
  25. * Guarantee that the kzalloc'd memory is cacheline aligned.
  26. */
  27. void *
  28. xpc_kzalloc_cacheline_aligned(size_t size, gfp_t flags, void **base)
  29. {
  30. /* see if kzalloc will give us cachline aligned memory by default */
  31. *base = kzalloc(size, flags);
  32. if (*base == NULL)
  33. return NULL;
  34. if ((u64)*base == L1_CACHE_ALIGN((u64)*base))
  35. return *base;
  36. kfree(*base);
  37. /* nope, we'll have to do it ourselves */
  38. *base = kzalloc(size + L1_CACHE_BYTES, flags);
  39. if (*base == NULL)
  40. return NULL;
  41. return (void *)L1_CACHE_ALIGN((u64)*base);
  42. }
  43. /*
  44. * Allocate the local message queue and the notify queue.
  45. */
  46. static enum xp_retval
  47. xpc_allocate_local_msgqueue(struct xpc_channel *ch)
  48. {
  49. unsigned long irq_flags;
  50. int nentries;
  51. size_t nbytes;
  52. for (nentries = ch->local_nentries; nentries > 0; nentries--) {
  53. nbytes = nentries * ch->msg_size;
  54. ch->local_msgqueue = xpc_kzalloc_cacheline_aligned(nbytes,
  55. GFP_KERNEL,
  56. &ch->local_msgqueue_base);
  57. if (ch->local_msgqueue == NULL)
  58. continue;
  59. nbytes = nentries * sizeof(struct xpc_notify);
  60. ch->notify_queue = kzalloc(nbytes, GFP_KERNEL);
  61. if (ch->notify_queue == NULL) {
  62. kfree(ch->local_msgqueue_base);
  63. ch->local_msgqueue = NULL;
  64. continue;
  65. }
  66. spin_lock_irqsave(&ch->lock, irq_flags);
  67. if (nentries < ch->local_nentries) {
  68. dev_dbg(xpc_chan, "nentries=%d local_nentries=%d, "
  69. "partid=%d, channel=%d\n", nentries,
  70. ch->local_nentries, ch->partid, ch->number);
  71. ch->local_nentries = nentries;
  72. }
  73. spin_unlock_irqrestore(&ch->lock, irq_flags);
  74. return xpSuccess;
  75. }
  76. dev_dbg(xpc_chan, "can't get memory for local message queue and notify "
  77. "queue, partid=%d, channel=%d\n", ch->partid, ch->number);
  78. return xpNoMemory;
  79. }
  80. /*
  81. * Allocate the cached remote message queue.
  82. */
  83. static enum xp_retval
  84. xpc_allocate_remote_msgqueue(struct xpc_channel *ch)
  85. {
  86. unsigned long irq_flags;
  87. int nentries;
  88. size_t nbytes;
  89. DBUG_ON(ch->remote_nentries <= 0);
  90. for (nentries = ch->remote_nentries; nentries > 0; nentries--) {
  91. nbytes = nentries * ch->msg_size;
  92. ch->remote_msgqueue = xpc_kzalloc_cacheline_aligned(nbytes,
  93. GFP_KERNEL,
  94. &ch->remote_msgqueue_base);
  95. if (ch->remote_msgqueue == NULL)
  96. continue;
  97. spin_lock_irqsave(&ch->lock, irq_flags);
  98. if (nentries < ch->remote_nentries) {
  99. dev_dbg(xpc_chan, "nentries=%d remote_nentries=%d, "
  100. "partid=%d, channel=%d\n", nentries,
  101. ch->remote_nentries, ch->partid, ch->number);
  102. ch->remote_nentries = nentries;
  103. }
  104. spin_unlock_irqrestore(&ch->lock, irq_flags);
  105. return xpSuccess;
  106. }
  107. dev_dbg(xpc_chan, "can't get memory for cached remote message queue, "
  108. "partid=%d, channel=%d\n", ch->partid, ch->number);
  109. return xpNoMemory;
  110. }
  111. /*
  112. * Allocate message queues and other stuff associated with a channel.
  113. *
  114. * Note: Assumes all of the channel sizes are filled in.
  115. */
  116. static enum xp_retval
  117. xpc_allocate_msgqueues(struct xpc_channel *ch)
  118. {
  119. unsigned long irq_flags;
  120. enum xp_retval ret;
  121. DBUG_ON(ch->flags & XPC_C_SETUP);
  122. ret = xpc_allocate_local_msgqueue(ch);
  123. if (ret != xpSuccess)
  124. return ret;
  125. ret = xpc_allocate_remote_msgqueue(ch);
  126. if (ret != xpSuccess) {
  127. kfree(ch->local_msgqueue_base);
  128. ch->local_msgqueue = NULL;
  129. kfree(ch->notify_queue);
  130. ch->notify_queue = NULL;
  131. return ret;
  132. }
  133. spin_lock_irqsave(&ch->lock, irq_flags);
  134. ch->flags |= XPC_C_SETUP;
  135. spin_unlock_irqrestore(&ch->lock, irq_flags);
  136. return xpSuccess;
  137. }
  138. /*
  139. * Process a connect message from a remote partition.
  140. *
  141. * Note: xpc_process_connect() is expecting to be called with the
  142. * spin_lock_irqsave held and will leave it locked upon return.
  143. */
  144. static void
  145. xpc_process_connect(struct xpc_channel *ch, unsigned long *irq_flags)
  146. {
  147. enum xp_retval ret;
  148. DBUG_ON(!spin_is_locked(&ch->lock));
  149. if (!(ch->flags & XPC_C_OPENREQUEST) ||
  150. !(ch->flags & XPC_C_ROPENREQUEST)) {
  151. /* nothing more to do for now */
  152. return;
  153. }
  154. DBUG_ON(!(ch->flags & XPC_C_CONNECTING));
  155. if (!(ch->flags & XPC_C_SETUP)) {
  156. spin_unlock_irqrestore(&ch->lock, *irq_flags);
  157. ret = xpc_allocate_msgqueues(ch);
  158. spin_lock_irqsave(&ch->lock, *irq_flags);
  159. if (ret != xpSuccess)
  160. XPC_DISCONNECT_CHANNEL(ch, ret, irq_flags);
  161. if (ch->flags & (XPC_C_CONNECTED | XPC_C_DISCONNECTING))
  162. return;
  163. DBUG_ON(!(ch->flags & XPC_C_SETUP));
  164. DBUG_ON(ch->local_msgqueue == NULL);
  165. DBUG_ON(ch->remote_msgqueue == NULL);
  166. }
  167. if (!(ch->flags & XPC_C_OPENREPLY)) {
  168. ch->flags |= XPC_C_OPENREPLY;
  169. xpc_IPI_send_openreply(ch, irq_flags);
  170. }
  171. if (!(ch->flags & XPC_C_ROPENREPLY))
  172. return;
  173. DBUG_ON(ch->remote_msgqueue_pa == 0);
  174. ch->flags = (XPC_C_CONNECTED | XPC_C_SETUP); /* clear all else */
  175. dev_info(xpc_chan, "channel %d to partition %d connected\n",
  176. ch->number, ch->partid);
  177. spin_unlock_irqrestore(&ch->lock, *irq_flags);
  178. xpc_create_kthreads(ch, 1, 0);
  179. spin_lock_irqsave(&ch->lock, *irq_flags);
  180. }
  181. /*
  182. * Notify those who wanted to be notified upon delivery of their message.
  183. */
  184. static void
  185. xpc_notify_senders(struct xpc_channel *ch, enum xp_retval reason, s64 put)
  186. {
  187. struct xpc_notify *notify;
  188. u8 notify_type;
  189. s64 get = ch->w_remote_GP.get - 1;
  190. while (++get < put && atomic_read(&ch->n_to_notify) > 0) {
  191. notify = &ch->notify_queue[get % ch->local_nentries];
  192. /*
  193. * See if the notify entry indicates it was associated with
  194. * a message who's sender wants to be notified. It is possible
  195. * that it is, but someone else is doing or has done the
  196. * notification.
  197. */
  198. notify_type = notify->type;
  199. if (notify_type == 0 ||
  200. cmpxchg(&notify->type, notify_type, 0) != notify_type) {
  201. continue;
  202. }
  203. DBUG_ON(notify_type != XPC_N_CALL);
  204. atomic_dec(&ch->n_to_notify);
  205. if (notify->func != NULL) {
  206. dev_dbg(xpc_chan, "notify->func() called, notify=0x%p, "
  207. "msg_number=%ld, partid=%d, channel=%d\n",
  208. (void *)notify, get, ch->partid, ch->number);
  209. notify->func(reason, ch->partid, ch->number,
  210. notify->key);
  211. dev_dbg(xpc_chan, "notify->func() returned, "
  212. "notify=0x%p, msg_number=%ld, partid=%d, "
  213. "channel=%d\n", (void *)notify, get,
  214. ch->partid, ch->number);
  215. }
  216. }
  217. }
  218. /*
  219. * Free up message queues and other stuff that were allocated for the specified
  220. * channel.
  221. *
  222. * Note: ch->reason and ch->reason_line are left set for debugging purposes,
  223. * they're cleared when XPC_C_DISCONNECTED is cleared.
  224. */
  225. static void
  226. xpc_free_msgqueues(struct xpc_channel *ch)
  227. {
  228. DBUG_ON(!spin_is_locked(&ch->lock));
  229. DBUG_ON(atomic_read(&ch->n_to_notify) != 0);
  230. ch->remote_msgqueue_pa = 0;
  231. ch->func = NULL;
  232. ch->key = NULL;
  233. ch->msg_size = 0;
  234. ch->local_nentries = 0;
  235. ch->remote_nentries = 0;
  236. ch->kthreads_assigned_limit = 0;
  237. ch->kthreads_idle_limit = 0;
  238. ch->local_GP->get = 0;
  239. ch->local_GP->put = 0;
  240. ch->remote_GP.get = 0;
  241. ch->remote_GP.put = 0;
  242. ch->w_local_GP.get = 0;
  243. ch->w_local_GP.put = 0;
  244. ch->w_remote_GP.get = 0;
  245. ch->w_remote_GP.put = 0;
  246. ch->next_msg_to_pull = 0;
  247. if (ch->flags & XPC_C_SETUP) {
  248. ch->flags &= ~XPC_C_SETUP;
  249. dev_dbg(xpc_chan, "ch->flags=0x%x, partid=%d, channel=%d\n",
  250. ch->flags, ch->partid, ch->number);
  251. kfree(ch->local_msgqueue_base);
  252. ch->local_msgqueue = NULL;
  253. kfree(ch->remote_msgqueue_base);
  254. ch->remote_msgqueue = NULL;
  255. kfree(ch->notify_queue);
  256. ch->notify_queue = NULL;
  257. }
  258. }
  259. /*
  260. * spin_lock_irqsave() is expected to be held on entry.
  261. */
  262. static void
  263. xpc_process_disconnect(struct xpc_channel *ch, unsigned long *irq_flags)
  264. {
  265. struct xpc_partition *part = &xpc_partitions[ch->partid];
  266. u32 channel_was_connected = (ch->flags & XPC_C_WASCONNECTED);
  267. DBUG_ON(!spin_is_locked(&ch->lock));
  268. if (!(ch->flags & XPC_C_DISCONNECTING))
  269. return;
  270. DBUG_ON(!(ch->flags & XPC_C_CLOSEREQUEST));
  271. /* make sure all activity has settled down first */
  272. if (atomic_read(&ch->kthreads_assigned) > 0 ||
  273. atomic_read(&ch->references) > 0) {
  274. return;
  275. }
  276. DBUG_ON((ch->flags & XPC_C_CONNECTEDCALLOUT_MADE) &&
  277. !(ch->flags & XPC_C_DISCONNECTINGCALLOUT_MADE));
  278. if (part->act_state == XPC_P_DEACTIVATING) {
  279. /* can't proceed until the other side disengages from us */
  280. if (xpc_partition_engaged(1UL << ch->partid))
  281. return;
  282. } else {
  283. /* as long as the other side is up do the full protocol */
  284. if (!(ch->flags & XPC_C_RCLOSEREQUEST))
  285. return;
  286. if (!(ch->flags & XPC_C_CLOSEREPLY)) {
  287. ch->flags |= XPC_C_CLOSEREPLY;
  288. xpc_IPI_send_closereply(ch, irq_flags);
  289. }
  290. if (!(ch->flags & XPC_C_RCLOSEREPLY))
  291. return;
  292. }
  293. /* wake those waiting for notify completion */
  294. if (atomic_read(&ch->n_to_notify) > 0) {
  295. /* >>> we do callout while holding ch->lock */
  296. xpc_notify_senders(ch, ch->reason, ch->w_local_GP.put);
  297. }
  298. /* both sides are disconnected now */
  299. if (ch->flags & XPC_C_DISCONNECTINGCALLOUT_MADE) {
  300. spin_unlock_irqrestore(&ch->lock, *irq_flags);
  301. xpc_disconnect_callout(ch, xpDisconnected);
  302. spin_lock_irqsave(&ch->lock, *irq_flags);
  303. }
  304. /* it's now safe to free the channel's message queues */
  305. xpc_free_msgqueues(ch);
  306. /* mark disconnected, clear all other flags except XPC_C_WDISCONNECT */
  307. ch->flags = (XPC_C_DISCONNECTED | (ch->flags & XPC_C_WDISCONNECT));
  308. atomic_dec(&part->nchannels_active);
  309. if (channel_was_connected) {
  310. dev_info(xpc_chan, "channel %d to partition %d disconnected, "
  311. "reason=%d\n", ch->number, ch->partid, ch->reason);
  312. }
  313. if (ch->flags & XPC_C_WDISCONNECT) {
  314. /* we won't lose the CPU since we're holding ch->lock */
  315. complete(&ch->wdisconnect_wait);
  316. } else if (ch->delayed_IPI_flags) {
  317. if (part->act_state != XPC_P_DEACTIVATING) {
  318. /* time to take action on any delayed IPI flags */
  319. spin_lock(&part->IPI_lock);
  320. XPC_SET_IPI_FLAGS(part->local_IPI_amo, ch->number,
  321. ch->delayed_IPI_flags);
  322. spin_unlock(&part->IPI_lock);
  323. }
  324. ch->delayed_IPI_flags = 0;
  325. }
  326. }
  327. /*
  328. * Process a change in the channel's remote connection state.
  329. */
  330. static void
  331. xpc_process_openclose_IPI(struct xpc_partition *part, int ch_number,
  332. u8 IPI_flags)
  333. {
  334. unsigned long irq_flags;
  335. struct xpc_openclose_args *args =
  336. &part->remote_openclose_args[ch_number];
  337. struct xpc_channel *ch = &part->channels[ch_number];
  338. enum xp_retval reason;
  339. spin_lock_irqsave(&ch->lock, irq_flags);
  340. again:
  341. if ((ch->flags & XPC_C_DISCONNECTED) &&
  342. (ch->flags & XPC_C_WDISCONNECT)) {
  343. /*
  344. * Delay processing IPI flags until thread waiting disconnect
  345. * has had a chance to see that the channel is disconnected.
  346. */
  347. ch->delayed_IPI_flags |= IPI_flags;
  348. spin_unlock_irqrestore(&ch->lock, irq_flags);
  349. return;
  350. }
  351. if (IPI_flags & XPC_IPI_CLOSEREQUEST) {
  352. dev_dbg(xpc_chan, "XPC_IPI_CLOSEREQUEST (reason=%d) received "
  353. "from partid=%d, channel=%d\n", args->reason,
  354. ch->partid, ch->number);
  355. /*
  356. * If RCLOSEREQUEST is set, we're probably waiting for
  357. * RCLOSEREPLY. We should find it and a ROPENREQUEST packed
  358. * with this RCLOSEREQUEST in the IPI_flags.
  359. */
  360. if (ch->flags & XPC_C_RCLOSEREQUEST) {
  361. DBUG_ON(!(ch->flags & XPC_C_DISCONNECTING));
  362. DBUG_ON(!(ch->flags & XPC_C_CLOSEREQUEST));
  363. DBUG_ON(!(ch->flags & XPC_C_CLOSEREPLY));
  364. DBUG_ON(ch->flags & XPC_C_RCLOSEREPLY);
  365. DBUG_ON(!(IPI_flags & XPC_IPI_CLOSEREPLY));
  366. IPI_flags &= ~XPC_IPI_CLOSEREPLY;
  367. ch->flags |= XPC_C_RCLOSEREPLY;
  368. /* both sides have finished disconnecting */
  369. xpc_process_disconnect(ch, &irq_flags);
  370. DBUG_ON(!(ch->flags & XPC_C_DISCONNECTED));
  371. goto again;
  372. }
  373. if (ch->flags & XPC_C_DISCONNECTED) {
  374. if (!(IPI_flags & XPC_IPI_OPENREQUEST)) {
  375. if ((XPC_GET_IPI_FLAGS(part->local_IPI_amo,
  376. ch_number) &
  377. XPC_IPI_OPENREQUEST)) {
  378. DBUG_ON(ch->delayed_IPI_flags != 0);
  379. spin_lock(&part->IPI_lock);
  380. XPC_SET_IPI_FLAGS(part->local_IPI_amo,
  381. ch_number,
  382. XPC_IPI_CLOSEREQUEST);
  383. spin_unlock(&part->IPI_lock);
  384. }
  385. spin_unlock_irqrestore(&ch->lock, irq_flags);
  386. return;
  387. }
  388. XPC_SET_REASON(ch, 0, 0);
  389. ch->flags &= ~XPC_C_DISCONNECTED;
  390. atomic_inc(&part->nchannels_active);
  391. ch->flags |= (XPC_C_CONNECTING | XPC_C_ROPENREQUEST);
  392. }
  393. IPI_flags &= ~(XPC_IPI_OPENREQUEST | XPC_IPI_OPENREPLY);
  394. /*
  395. * The meaningful CLOSEREQUEST connection state fields are:
  396. * reason = reason connection is to be closed
  397. */
  398. ch->flags |= XPC_C_RCLOSEREQUEST;
  399. if (!(ch->flags & XPC_C_DISCONNECTING)) {
  400. reason = args->reason;
  401. if (reason <= xpSuccess || reason > xpUnknownReason)
  402. reason = xpUnknownReason;
  403. else if (reason == xpUnregistering)
  404. reason = xpOtherUnregistering;
  405. XPC_DISCONNECT_CHANNEL(ch, reason, &irq_flags);
  406. DBUG_ON(IPI_flags & XPC_IPI_CLOSEREPLY);
  407. spin_unlock_irqrestore(&ch->lock, irq_flags);
  408. return;
  409. }
  410. xpc_process_disconnect(ch, &irq_flags);
  411. }
  412. if (IPI_flags & XPC_IPI_CLOSEREPLY) {
  413. dev_dbg(xpc_chan, "XPC_IPI_CLOSEREPLY received from partid=%d,"
  414. " channel=%d\n", ch->partid, ch->number);
  415. if (ch->flags & XPC_C_DISCONNECTED) {
  416. DBUG_ON(part->act_state != XPC_P_DEACTIVATING);
  417. spin_unlock_irqrestore(&ch->lock, irq_flags);
  418. return;
  419. }
  420. DBUG_ON(!(ch->flags & XPC_C_CLOSEREQUEST));
  421. if (!(ch->flags & XPC_C_RCLOSEREQUEST)) {
  422. if ((XPC_GET_IPI_FLAGS(part->local_IPI_amo, ch_number)
  423. & XPC_IPI_CLOSEREQUEST)) {
  424. DBUG_ON(ch->delayed_IPI_flags != 0);
  425. spin_lock(&part->IPI_lock);
  426. XPC_SET_IPI_FLAGS(part->local_IPI_amo,
  427. ch_number,
  428. XPC_IPI_CLOSEREPLY);
  429. spin_unlock(&part->IPI_lock);
  430. }
  431. spin_unlock_irqrestore(&ch->lock, irq_flags);
  432. return;
  433. }
  434. ch->flags |= XPC_C_RCLOSEREPLY;
  435. if (ch->flags & XPC_C_CLOSEREPLY) {
  436. /* both sides have finished disconnecting */
  437. xpc_process_disconnect(ch, &irq_flags);
  438. }
  439. }
  440. if (IPI_flags & XPC_IPI_OPENREQUEST) {
  441. dev_dbg(xpc_chan, "XPC_IPI_OPENREQUEST (msg_size=%d, "
  442. "local_nentries=%d) received from partid=%d, "
  443. "channel=%d\n", args->msg_size, args->local_nentries,
  444. ch->partid, ch->number);
  445. if (part->act_state == XPC_P_DEACTIVATING ||
  446. (ch->flags & XPC_C_ROPENREQUEST)) {
  447. spin_unlock_irqrestore(&ch->lock, irq_flags);
  448. return;
  449. }
  450. if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_WDISCONNECT)) {
  451. ch->delayed_IPI_flags |= XPC_IPI_OPENREQUEST;
  452. spin_unlock_irqrestore(&ch->lock, irq_flags);
  453. return;
  454. }
  455. DBUG_ON(!(ch->flags & (XPC_C_DISCONNECTED |
  456. XPC_C_OPENREQUEST)));
  457. DBUG_ON(ch->flags & (XPC_C_ROPENREQUEST | XPC_C_ROPENREPLY |
  458. XPC_C_OPENREPLY | XPC_C_CONNECTED));
  459. /*
  460. * The meaningful OPENREQUEST connection state fields are:
  461. * msg_size = size of channel's messages in bytes
  462. * local_nentries = remote partition's local_nentries
  463. */
  464. if (args->msg_size == 0 || args->local_nentries == 0) {
  465. /* assume OPENREQUEST was delayed by mistake */
  466. spin_unlock_irqrestore(&ch->lock, irq_flags);
  467. return;
  468. }
  469. ch->flags |= (XPC_C_ROPENREQUEST | XPC_C_CONNECTING);
  470. ch->remote_nentries = args->local_nentries;
  471. if (ch->flags & XPC_C_OPENREQUEST) {
  472. if (args->msg_size != ch->msg_size) {
  473. XPC_DISCONNECT_CHANNEL(ch, xpUnequalMsgSizes,
  474. &irq_flags);
  475. spin_unlock_irqrestore(&ch->lock, irq_flags);
  476. return;
  477. }
  478. } else {
  479. ch->msg_size = args->msg_size;
  480. XPC_SET_REASON(ch, 0, 0);
  481. ch->flags &= ~XPC_C_DISCONNECTED;
  482. atomic_inc(&part->nchannels_active);
  483. }
  484. xpc_process_connect(ch, &irq_flags);
  485. }
  486. if (IPI_flags & XPC_IPI_OPENREPLY) {
  487. dev_dbg(xpc_chan, "XPC_IPI_OPENREPLY (local_msgqueue_pa=0x%lx, "
  488. "local_nentries=%d, remote_nentries=%d) received from "
  489. "partid=%d, channel=%d\n", args->local_msgqueue_pa,
  490. args->local_nentries, args->remote_nentries,
  491. ch->partid, ch->number);
  492. if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_DISCONNECTED)) {
  493. spin_unlock_irqrestore(&ch->lock, irq_flags);
  494. return;
  495. }
  496. if (!(ch->flags & XPC_C_OPENREQUEST)) {
  497. XPC_DISCONNECT_CHANNEL(ch, xpOpenCloseError,
  498. &irq_flags);
  499. spin_unlock_irqrestore(&ch->lock, irq_flags);
  500. return;
  501. }
  502. DBUG_ON(!(ch->flags & XPC_C_ROPENREQUEST));
  503. DBUG_ON(ch->flags & XPC_C_CONNECTED);
  504. /*
  505. * The meaningful OPENREPLY connection state fields are:
  506. * local_msgqueue_pa = physical address of remote
  507. * partition's local_msgqueue
  508. * local_nentries = remote partition's local_nentries
  509. * remote_nentries = remote partition's remote_nentries
  510. */
  511. DBUG_ON(args->local_msgqueue_pa == 0);
  512. DBUG_ON(args->local_nentries == 0);
  513. DBUG_ON(args->remote_nentries == 0);
  514. ch->flags |= XPC_C_ROPENREPLY;
  515. ch->remote_msgqueue_pa = args->local_msgqueue_pa;
  516. if (args->local_nentries < ch->remote_nentries) {
  517. dev_dbg(xpc_chan, "XPC_IPI_OPENREPLY: new "
  518. "remote_nentries=%d, old remote_nentries=%d, "
  519. "partid=%d, channel=%d\n",
  520. args->local_nentries, ch->remote_nentries,
  521. ch->partid, ch->number);
  522. ch->remote_nentries = args->local_nentries;
  523. }
  524. if (args->remote_nentries < ch->local_nentries) {
  525. dev_dbg(xpc_chan, "XPC_IPI_OPENREPLY: new "
  526. "local_nentries=%d, old local_nentries=%d, "
  527. "partid=%d, channel=%d\n",
  528. args->remote_nentries, ch->local_nentries,
  529. ch->partid, ch->number);
  530. ch->local_nentries = args->remote_nentries;
  531. }
  532. xpc_process_connect(ch, &irq_flags);
  533. }
  534. spin_unlock_irqrestore(&ch->lock, irq_flags);
  535. }
  536. /*
  537. * Attempt to establish a channel connection to a remote partition.
  538. */
  539. static enum xp_retval
  540. xpc_connect_channel(struct xpc_channel *ch)
  541. {
  542. unsigned long irq_flags;
  543. struct xpc_registration *registration = &xpc_registrations[ch->number];
  544. if (mutex_trylock(&registration->mutex) == 0)
  545. return xpRetry;
  546. if (!XPC_CHANNEL_REGISTERED(ch->number)) {
  547. mutex_unlock(&registration->mutex);
  548. return xpUnregistered;
  549. }
  550. spin_lock_irqsave(&ch->lock, irq_flags);
  551. DBUG_ON(ch->flags & XPC_C_CONNECTED);
  552. DBUG_ON(ch->flags & XPC_C_OPENREQUEST);
  553. if (ch->flags & XPC_C_DISCONNECTING) {
  554. spin_unlock_irqrestore(&ch->lock, irq_flags);
  555. mutex_unlock(&registration->mutex);
  556. return ch->reason;
  557. }
  558. /* add info from the channel connect registration to the channel */
  559. ch->kthreads_assigned_limit = registration->assigned_limit;
  560. ch->kthreads_idle_limit = registration->idle_limit;
  561. DBUG_ON(atomic_read(&ch->kthreads_assigned) != 0);
  562. DBUG_ON(atomic_read(&ch->kthreads_idle) != 0);
  563. DBUG_ON(atomic_read(&ch->kthreads_active) != 0);
  564. ch->func = registration->func;
  565. DBUG_ON(registration->func == NULL);
  566. ch->key = registration->key;
  567. ch->local_nentries = registration->nentries;
  568. if (ch->flags & XPC_C_ROPENREQUEST) {
  569. if (registration->msg_size != ch->msg_size) {
  570. /* the local and remote sides aren't the same */
  571. /*
  572. * Because XPC_DISCONNECT_CHANNEL() can block we're
  573. * forced to up the registration sema before we unlock
  574. * the channel lock. But that's okay here because we're
  575. * done with the part that required the registration
  576. * sema. XPC_DISCONNECT_CHANNEL() requires that the
  577. * channel lock be locked and will unlock and relock
  578. * the channel lock as needed.
  579. */
  580. mutex_unlock(&registration->mutex);
  581. XPC_DISCONNECT_CHANNEL(ch, xpUnequalMsgSizes,
  582. &irq_flags);
  583. spin_unlock_irqrestore(&ch->lock, irq_flags);
  584. return xpUnequalMsgSizes;
  585. }
  586. } else {
  587. ch->msg_size = registration->msg_size;
  588. XPC_SET_REASON(ch, 0, 0);
  589. ch->flags &= ~XPC_C_DISCONNECTED;
  590. atomic_inc(&xpc_partitions[ch->partid].nchannels_active);
  591. }
  592. mutex_unlock(&registration->mutex);
  593. /* initiate the connection */
  594. ch->flags |= (XPC_C_OPENREQUEST | XPC_C_CONNECTING);
  595. xpc_IPI_send_openrequest(ch, &irq_flags);
  596. xpc_process_connect(ch, &irq_flags);
  597. spin_unlock_irqrestore(&ch->lock, irq_flags);
  598. return xpSuccess;
  599. }
  600. /*
  601. * Clear some of the msg flags in the local message queue.
  602. */
  603. static inline void
  604. xpc_clear_local_msgqueue_flags(struct xpc_channel *ch)
  605. {
  606. struct xpc_msg *msg;
  607. s64 get;
  608. get = ch->w_remote_GP.get;
  609. do {
  610. msg = (struct xpc_msg *)((u64)ch->local_msgqueue +
  611. (get % ch->local_nentries) *
  612. ch->msg_size);
  613. msg->flags = 0;
  614. } while (++get < ch->remote_GP.get);
  615. }
  616. /*
  617. * Clear some of the msg flags in the remote message queue.
  618. */
  619. static inline void
  620. xpc_clear_remote_msgqueue_flags(struct xpc_channel *ch)
  621. {
  622. struct xpc_msg *msg;
  623. s64 put;
  624. put = ch->w_remote_GP.put;
  625. do {
  626. msg = (struct xpc_msg *)((u64)ch->remote_msgqueue +
  627. (put % ch->remote_nentries) *
  628. ch->msg_size);
  629. msg->flags = 0;
  630. } while (++put < ch->remote_GP.put);
  631. }
  632. static void
  633. xpc_process_msg_IPI(struct xpc_partition *part, int ch_number)
  634. {
  635. struct xpc_channel *ch = &part->channels[ch_number];
  636. int nmsgs_sent;
  637. ch->remote_GP = part->remote_GPs[ch_number];
  638. /* See what, if anything, has changed for each connected channel */
  639. xpc_msgqueue_ref(ch);
  640. if (ch->w_remote_GP.get == ch->remote_GP.get &&
  641. ch->w_remote_GP.put == ch->remote_GP.put) {
  642. /* nothing changed since GPs were last pulled */
  643. xpc_msgqueue_deref(ch);
  644. return;
  645. }
  646. if (!(ch->flags & XPC_C_CONNECTED)) {
  647. xpc_msgqueue_deref(ch);
  648. return;
  649. }
  650. /*
  651. * First check to see if messages recently sent by us have been
  652. * received by the other side. (The remote GET value will have
  653. * changed since we last looked at it.)
  654. */
  655. if (ch->w_remote_GP.get != ch->remote_GP.get) {
  656. /*
  657. * We need to notify any senders that want to be notified
  658. * that their sent messages have been received by their
  659. * intended recipients. We need to do this before updating
  660. * w_remote_GP.get so that we don't allocate the same message
  661. * queue entries prematurely (see xpc_allocate_msg()).
  662. */
  663. if (atomic_read(&ch->n_to_notify) > 0) {
  664. /*
  665. * Notify senders that messages sent have been
  666. * received and delivered by the other side.
  667. */
  668. xpc_notify_senders(ch, xpMsgDelivered,
  669. ch->remote_GP.get);
  670. }
  671. /*
  672. * Clear msg->flags in previously sent messages, so that
  673. * they're ready for xpc_allocate_msg().
  674. */
  675. xpc_clear_local_msgqueue_flags(ch);
  676. ch->w_remote_GP.get = ch->remote_GP.get;
  677. dev_dbg(xpc_chan, "w_remote_GP.get changed to %ld, partid=%d, "
  678. "channel=%d\n", ch->w_remote_GP.get, ch->partid,
  679. ch->number);
  680. /*
  681. * If anyone was waiting for message queue entries to become
  682. * available, wake them up.
  683. */
  684. if (atomic_read(&ch->n_on_msg_allocate_wq) > 0)
  685. wake_up(&ch->msg_allocate_wq);
  686. }
  687. /*
  688. * Now check for newly sent messages by the other side. (The remote
  689. * PUT value will have changed since we last looked at it.)
  690. */
  691. if (ch->w_remote_GP.put != ch->remote_GP.put) {
  692. /*
  693. * Clear msg->flags in previously received messages, so that
  694. * they're ready for xpc_get_deliverable_msg().
  695. */
  696. xpc_clear_remote_msgqueue_flags(ch);
  697. ch->w_remote_GP.put = ch->remote_GP.put;
  698. dev_dbg(xpc_chan, "w_remote_GP.put changed to %ld, partid=%d, "
  699. "channel=%d\n", ch->w_remote_GP.put, ch->partid,
  700. ch->number);
  701. nmsgs_sent = ch->w_remote_GP.put - ch->w_local_GP.get;
  702. if (nmsgs_sent > 0) {
  703. dev_dbg(xpc_chan, "msgs waiting to be copied and "
  704. "delivered=%d, partid=%d, channel=%d\n",
  705. nmsgs_sent, ch->partid, ch->number);
  706. if (ch->flags & XPC_C_CONNECTEDCALLOUT_MADE)
  707. xpc_activate_kthreads(ch, nmsgs_sent);
  708. }
  709. }
  710. xpc_msgqueue_deref(ch);
  711. }
  712. void
  713. xpc_process_channel_activity(struct xpc_partition *part)
  714. {
  715. unsigned long irq_flags;
  716. u64 IPI_amo, IPI_flags;
  717. struct xpc_channel *ch;
  718. int ch_number;
  719. u32 ch_flags;
  720. IPI_amo = xpc_get_IPI_flags(part);
  721. /*
  722. * Initiate channel connections for registered channels.
  723. *
  724. * For each connected channel that has pending messages activate idle
  725. * kthreads and/or create new kthreads as needed.
  726. */
  727. for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
  728. ch = &part->channels[ch_number];
  729. /*
  730. * Process any open or close related IPI flags, and then deal
  731. * with connecting or disconnecting the channel as required.
  732. */
  733. IPI_flags = XPC_GET_IPI_FLAGS(IPI_amo, ch_number);
  734. if (XPC_ANY_OPENCLOSE_IPI_FLAGS_SET(IPI_flags))
  735. xpc_process_openclose_IPI(part, ch_number, IPI_flags);
  736. ch_flags = ch->flags; /* need an atomic snapshot of flags */
  737. if (ch_flags & XPC_C_DISCONNECTING) {
  738. spin_lock_irqsave(&ch->lock, irq_flags);
  739. xpc_process_disconnect(ch, &irq_flags);
  740. spin_unlock_irqrestore(&ch->lock, irq_flags);
  741. continue;
  742. }
  743. if (part->act_state == XPC_P_DEACTIVATING)
  744. continue;
  745. if (!(ch_flags & XPC_C_CONNECTED)) {
  746. if (!(ch_flags & XPC_C_OPENREQUEST)) {
  747. DBUG_ON(ch_flags & XPC_C_SETUP);
  748. (void)xpc_connect_channel(ch);
  749. } else {
  750. spin_lock_irqsave(&ch->lock, irq_flags);
  751. xpc_process_connect(ch, &irq_flags);
  752. spin_unlock_irqrestore(&ch->lock, irq_flags);
  753. }
  754. continue;
  755. }
  756. /*
  757. * Process any message related IPI flags, this may involve the
  758. * activation of kthreads to deliver any pending messages sent
  759. * from the other partition.
  760. */
  761. if (XPC_ANY_MSG_IPI_FLAGS_SET(IPI_flags))
  762. xpc_process_msg_IPI(part, ch_number);
  763. }
  764. }
  765. /*
  766. * XPC's heartbeat code calls this function to inform XPC that a partition is
  767. * going down. XPC responds by tearing down the XPartition Communication
  768. * infrastructure used for the just downed partition.
  769. *
  770. * XPC's heartbeat code will never call this function and xpc_partition_up()
  771. * at the same time. Nor will it ever make multiple calls to either function
  772. * at the same time.
  773. */
  774. void
  775. xpc_partition_going_down(struct xpc_partition *part, enum xp_retval reason)
  776. {
  777. unsigned long irq_flags;
  778. int ch_number;
  779. struct xpc_channel *ch;
  780. dev_dbg(xpc_chan, "deactivating partition %d, reason=%d\n",
  781. XPC_PARTID(part), reason);
  782. if (!xpc_part_ref(part)) {
  783. /* infrastructure for this partition isn't currently set up */
  784. return;
  785. }
  786. /* disconnect channels associated with the partition going down */
  787. for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
  788. ch = &part->channels[ch_number];
  789. xpc_msgqueue_ref(ch);
  790. spin_lock_irqsave(&ch->lock, irq_flags);
  791. XPC_DISCONNECT_CHANNEL(ch, reason, &irq_flags);
  792. spin_unlock_irqrestore(&ch->lock, irq_flags);
  793. xpc_msgqueue_deref(ch);
  794. }
  795. xpc_wakeup_channel_mgr(part);
  796. xpc_part_deref(part);
  797. }
  798. /*
  799. * Called by XP at the time of channel connection registration to cause
  800. * XPC to establish connections to all currently active partitions.
  801. */
  802. void
  803. xpc_initiate_connect(int ch_number)
  804. {
  805. short partid;
  806. struct xpc_partition *part;
  807. struct xpc_channel *ch;
  808. DBUG_ON(ch_number < 0 || ch_number >= XPC_MAX_NCHANNELS);
  809. for (partid = 0; partid < xp_max_npartitions; partid++) {
  810. part = &xpc_partitions[partid];
  811. if (xpc_part_ref(part)) {
  812. ch = &part->channels[ch_number];
  813. /*
  814. * Initiate the establishment of a connection on the
  815. * newly registered channel to the remote partition.
  816. */
  817. xpc_wakeup_channel_mgr(part);
  818. xpc_part_deref(part);
  819. }
  820. }
  821. }
  822. void
  823. xpc_connected_callout(struct xpc_channel *ch)
  824. {
  825. /* let the registerer know that a connection has been established */
  826. if (ch->func != NULL) {
  827. dev_dbg(xpc_chan, "ch->func() called, reason=xpConnected, "
  828. "partid=%d, channel=%d\n", ch->partid, ch->number);
  829. ch->func(xpConnected, ch->partid, ch->number,
  830. (void *)(u64)ch->local_nentries, ch->key);
  831. dev_dbg(xpc_chan, "ch->func() returned, reason=xpConnected, "
  832. "partid=%d, channel=%d\n", ch->partid, ch->number);
  833. }
  834. }
  835. /*
  836. * Called by XP at the time of channel connection unregistration to cause
  837. * XPC to teardown all current connections for the specified channel.
  838. *
  839. * Before returning xpc_initiate_disconnect() will wait until all connections
  840. * on the specified channel have been closed/torndown. So the caller can be
  841. * assured that they will not be receiving any more callouts from XPC to the
  842. * function they registered via xpc_connect().
  843. *
  844. * Arguments:
  845. *
  846. * ch_number - channel # to unregister.
  847. */
  848. void
  849. xpc_initiate_disconnect(int ch_number)
  850. {
  851. unsigned long irq_flags;
  852. short partid;
  853. struct xpc_partition *part;
  854. struct xpc_channel *ch;
  855. DBUG_ON(ch_number < 0 || ch_number >= XPC_MAX_NCHANNELS);
  856. /* initiate the channel disconnect for every active partition */
  857. for (partid = 0; partid < xp_max_npartitions; partid++) {
  858. part = &xpc_partitions[partid];
  859. if (xpc_part_ref(part)) {
  860. ch = &part->channels[ch_number];
  861. xpc_msgqueue_ref(ch);
  862. spin_lock_irqsave(&ch->lock, irq_flags);
  863. if (!(ch->flags & XPC_C_DISCONNECTED)) {
  864. ch->flags |= XPC_C_WDISCONNECT;
  865. XPC_DISCONNECT_CHANNEL(ch, xpUnregistering,
  866. &irq_flags);
  867. }
  868. spin_unlock_irqrestore(&ch->lock, irq_flags);
  869. xpc_msgqueue_deref(ch);
  870. xpc_part_deref(part);
  871. }
  872. }
  873. xpc_disconnect_wait(ch_number);
  874. }
  875. /*
  876. * To disconnect a channel, and reflect it back to all who may be waiting.
  877. *
  878. * An OPEN is not allowed until XPC_C_DISCONNECTING is cleared by
  879. * xpc_process_disconnect(), and if set, XPC_C_WDISCONNECT is cleared by
  880. * xpc_disconnect_wait().
  881. *
  882. * THE CHANNEL IS TO BE LOCKED BY THE CALLER AND WILL REMAIN LOCKED UPON RETURN.
  883. */
  884. void
  885. xpc_disconnect_channel(const int line, struct xpc_channel *ch,
  886. enum xp_retval reason, unsigned long *irq_flags)
  887. {
  888. u32 channel_was_connected = (ch->flags & XPC_C_CONNECTED);
  889. DBUG_ON(!spin_is_locked(&ch->lock));
  890. if (ch->flags & (XPC_C_DISCONNECTING | XPC_C_DISCONNECTED))
  891. return;
  892. DBUG_ON(!(ch->flags & (XPC_C_CONNECTING | XPC_C_CONNECTED)));
  893. dev_dbg(xpc_chan, "reason=%d, line=%d, partid=%d, channel=%d\n",
  894. reason, line, ch->partid, ch->number);
  895. XPC_SET_REASON(ch, reason, line);
  896. ch->flags |= (XPC_C_CLOSEREQUEST | XPC_C_DISCONNECTING);
  897. /* some of these may not have been set */
  898. ch->flags &= ~(XPC_C_OPENREQUEST | XPC_C_OPENREPLY |
  899. XPC_C_ROPENREQUEST | XPC_C_ROPENREPLY |
  900. XPC_C_CONNECTING | XPC_C_CONNECTED);
  901. xpc_IPI_send_closerequest(ch, irq_flags);
  902. if (channel_was_connected)
  903. ch->flags |= XPC_C_WASCONNECTED;
  904. spin_unlock_irqrestore(&ch->lock, *irq_flags);
  905. /* wake all idle kthreads so they can exit */
  906. if (atomic_read(&ch->kthreads_idle) > 0) {
  907. wake_up_all(&ch->idle_wq);
  908. } else if ((ch->flags & XPC_C_CONNECTEDCALLOUT_MADE) &&
  909. !(ch->flags & XPC_C_DISCONNECTINGCALLOUT)) {
  910. /* start a kthread that will do the xpDisconnecting callout */
  911. xpc_create_kthreads(ch, 1, 1);
  912. }
  913. /* wake those waiting to allocate an entry from the local msg queue */
  914. if (atomic_read(&ch->n_on_msg_allocate_wq) > 0)
  915. wake_up(&ch->msg_allocate_wq);
  916. spin_lock_irqsave(&ch->lock, *irq_flags);
  917. }
  918. void
  919. xpc_disconnect_callout(struct xpc_channel *ch, enum xp_retval reason)
  920. {
  921. /*
  922. * Let the channel's registerer know that the channel is being
  923. * disconnected. We don't want to do this if the registerer was never
  924. * informed of a connection being made.
  925. */
  926. if (ch->func != NULL) {
  927. dev_dbg(xpc_chan, "ch->func() called, reason=%d, partid=%d, "
  928. "channel=%d\n", reason, ch->partid, ch->number);
  929. ch->func(reason, ch->partid, ch->number, NULL, ch->key);
  930. dev_dbg(xpc_chan, "ch->func() returned, reason=%d, partid=%d, "
  931. "channel=%d\n", reason, ch->partid, ch->number);
  932. }
  933. }
  934. /*
  935. * Wait for a message entry to become available for the specified channel,
  936. * but don't wait any longer than 1 jiffy.
  937. */
  938. enum xp_retval
  939. xpc_allocate_msg_wait(struct xpc_channel *ch)
  940. {
  941. enum xp_retval ret;
  942. if (ch->flags & XPC_C_DISCONNECTING) {
  943. DBUG_ON(ch->reason == xpInterrupted);
  944. return ch->reason;
  945. }
  946. atomic_inc(&ch->n_on_msg_allocate_wq);
  947. ret = interruptible_sleep_on_timeout(&ch->msg_allocate_wq, 1);
  948. atomic_dec(&ch->n_on_msg_allocate_wq);
  949. if (ch->flags & XPC_C_DISCONNECTING) {
  950. ret = ch->reason;
  951. DBUG_ON(ch->reason == xpInterrupted);
  952. } else if (ret == 0) {
  953. ret = xpTimeout;
  954. } else {
  955. ret = xpInterrupted;
  956. }
  957. return ret;
  958. }
  959. /*
  960. * Send a message that contains the user's payload on the specified channel
  961. * connected to the specified partition.
  962. *
  963. * NOTE that this routine can sleep waiting for a message entry to become
  964. * available. To not sleep, pass in the XPC_NOWAIT flag.
  965. *
  966. * Once sent, this routine will not wait for the message to be received, nor
  967. * will notification be given when it does happen.
  968. *
  969. * Arguments:
  970. *
  971. * partid - ID of partition to which the channel is connected.
  972. * ch_number - channel # to send message on.
  973. * flags - see xp.h for valid flags.
  974. * payload - pointer to the payload which is to be sent.
  975. * payload_size - size of the payload in bytes.
  976. */
  977. enum xp_retval
  978. xpc_initiate_send(short partid, int ch_number, u32 flags, void *payload,
  979. u16 payload_size)
  980. {
  981. struct xpc_partition *part = &xpc_partitions[partid];
  982. enum xp_retval ret = xpUnknownReason;
  983. dev_dbg(xpc_chan, "payload=0x%p, partid=%d, channel=%d\n", payload,
  984. partid, ch_number);
  985. DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
  986. DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
  987. DBUG_ON(payload == NULL);
  988. if (xpc_part_ref(part)) {
  989. ret = xpc_send_msg(&part->channels[ch_number], flags, payload,
  990. payload_size, 0, NULL, NULL);
  991. xpc_part_deref(part);
  992. }
  993. return ret;
  994. }
  995. /*
  996. * Send a message that contains the user's payload on the specified channel
  997. * connected to the specified partition.
  998. *
  999. * NOTE that this routine can sleep waiting for a message entry to become
  1000. * available. To not sleep, pass in the XPC_NOWAIT flag.
  1001. *
  1002. * This routine will not wait for the message to be sent or received.
  1003. *
  1004. * Once the remote end of the channel has received the message, the function
  1005. * passed as an argument to xpc_initiate_send_notify() will be called. This
  1006. * allows the sender to free up or re-use any buffers referenced by the
  1007. * message, but does NOT mean the message has been processed at the remote
  1008. * end by a receiver.
  1009. *
  1010. * If this routine returns an error, the caller's function will NOT be called.
  1011. *
  1012. * Arguments:
  1013. *
  1014. * partid - ID of partition to which the channel is connected.
  1015. * ch_number - channel # to send message on.
  1016. * flags - see xp.h for valid flags.
  1017. * payload - pointer to the payload which is to be sent.
  1018. * payload_size - size of the payload in bytes.
  1019. * func - function to call with asynchronous notification of message
  1020. * receipt. THIS FUNCTION MUST BE NON-BLOCKING.
  1021. * key - user-defined key to be passed to the function when it's called.
  1022. */
  1023. enum xp_retval
  1024. xpc_initiate_send_notify(short partid, int ch_number, u32 flags, void *payload,
  1025. u16 payload_size, xpc_notify_func func, void *key)
  1026. {
  1027. struct xpc_partition *part = &xpc_partitions[partid];
  1028. enum xp_retval ret = xpUnknownReason;
  1029. dev_dbg(xpc_chan, "payload=0x%p, partid=%d, channel=%d\n", payload,
  1030. partid, ch_number);
  1031. DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
  1032. DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
  1033. DBUG_ON(payload == NULL);
  1034. DBUG_ON(func == NULL);
  1035. if (xpc_part_ref(part)) {
  1036. ret = xpc_send_msg(&part->channels[ch_number], flags, payload,
  1037. payload_size, XPC_N_CALL, func, key);
  1038. xpc_part_deref(part);
  1039. }
  1040. return ret;
  1041. }
  1042. /*
  1043. * Deliver a message to its intended recipient.
  1044. */
  1045. void
  1046. xpc_deliver_msg(struct xpc_channel *ch)
  1047. {
  1048. struct xpc_msg *msg;
  1049. msg = xpc_get_deliverable_msg(ch);
  1050. if (msg != NULL) {
  1051. /*
  1052. * This ref is taken to protect the payload itself from being
  1053. * freed before the user is finished with it, which the user
  1054. * indicates by calling xpc_initiate_received().
  1055. */
  1056. xpc_msgqueue_ref(ch);
  1057. atomic_inc(&ch->kthreads_active);
  1058. if (ch->func != NULL) {
  1059. dev_dbg(xpc_chan, "ch->func() called, msg=0x%p, "
  1060. "msg_number=%ld, partid=%d, channel=%d\n",
  1061. (void *)msg, msg->number, ch->partid,
  1062. ch->number);
  1063. /* deliver the message to its intended recipient */
  1064. ch->func(xpMsgReceived, ch->partid, ch->number,
  1065. &msg->payload, ch->key);
  1066. dev_dbg(xpc_chan, "ch->func() returned, msg=0x%p, "
  1067. "msg_number=%ld, partid=%d, channel=%d\n",
  1068. (void *)msg, msg->number, ch->partid,
  1069. ch->number);
  1070. }
  1071. atomic_dec(&ch->kthreads_active);
  1072. }
  1073. }
  1074. /*
  1075. * Acknowledge receipt of a delivered message.
  1076. *
  1077. * If a message has XPC_M_INTERRUPT set, send an interrupt to the partition
  1078. * that sent the message.
  1079. *
  1080. * This function, although called by users, does not call xpc_part_ref() to
  1081. * ensure that the partition infrastructure is in place. It relies on the
  1082. * fact that we called xpc_msgqueue_ref() in xpc_deliver_msg().
  1083. *
  1084. * Arguments:
  1085. *
  1086. * partid - ID of partition to which the channel is connected.
  1087. * ch_number - channel # message received on.
  1088. * payload - pointer to the payload area allocated via
  1089. * xpc_initiate_send() or xpc_initiate_send_notify().
  1090. */
  1091. void
  1092. xpc_initiate_received(short partid, int ch_number, void *payload)
  1093. {
  1094. struct xpc_partition *part = &xpc_partitions[partid];
  1095. struct xpc_channel *ch;
  1096. struct xpc_msg *msg = XPC_MSG_ADDRESS(payload);
  1097. DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
  1098. DBUG_ON(ch_number < 0 || ch_number >= part->nchannels);
  1099. ch = &part->channels[ch_number];
  1100. xpc_received_msg(ch, msg);
  1101. /* the call to xpc_msgqueue_ref() was done by xpc_deliver_msg() */
  1102. xpc_msgqueue_deref(ch);
  1103. }