xpc_uv.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621
  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) 2008 Silicon Graphics, Inc. All Rights Reserved.
  7. */
  8. /*
  9. * Cross Partition Communication (XPC) uv-based functions.
  10. *
  11. * Architecture specific implementation of common functions.
  12. *
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/mm.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/delay.h>
  18. #include <linux/device.h>
  19. #include <linux/err.h>
  20. #include <asm/uv/uv_hub.h>
  21. #if defined CONFIG_X86_64
  22. #include <asm/uv/bios.h>
  23. #include <asm/uv/uv_irq.h>
  24. #elif defined CONFIG_IA64_GENERIC || defined CONFIG_IA64_SGI_UV
  25. #include <asm/sn/intr.h>
  26. #include <asm/sn/sn_sal.h>
  27. #endif
  28. #include "../sgi-gru/gru.h"
  29. #include "../sgi-gru/grukservices.h"
  30. #include "xpc.h"
  31. static atomic64_t xpc_heartbeat_uv;
  32. static DECLARE_BITMAP(xpc_heartbeating_to_mask_uv, XP_MAX_NPARTITIONS_UV);
  33. #define XPC_ACTIVATE_MSG_SIZE_UV (1 * GRU_CACHE_LINE_BYTES)
  34. #define XPC_ACTIVATE_MQ_SIZE_UV (4 * XP_MAX_NPARTITIONS_UV * \
  35. XPC_ACTIVATE_MSG_SIZE_UV)
  36. #define XPC_ACTIVATE_IRQ_NAME "xpc_activate"
  37. #define XPC_NOTIFY_MSG_SIZE_UV (2 * GRU_CACHE_LINE_BYTES)
  38. #define XPC_NOTIFY_MQ_SIZE_UV (4 * XP_MAX_NPARTITIONS_UV * \
  39. XPC_NOTIFY_MSG_SIZE_UV)
  40. #define XPC_NOTIFY_IRQ_NAME "xpc_notify"
  41. static struct xpc_gru_mq_uv *xpc_activate_mq_uv;
  42. static struct xpc_gru_mq_uv *xpc_notify_mq_uv;
  43. static int
  44. xpc_setup_partitions_sn_uv(void)
  45. {
  46. short partid;
  47. struct xpc_partition_uv *part_uv;
  48. for (partid = 0; partid < XP_MAX_NPARTITIONS_UV; partid++) {
  49. part_uv = &xpc_partitions[partid].sn.uv;
  50. spin_lock_init(&part_uv->flags_lock);
  51. part_uv->remote_act_state = XPC_P_AS_INACTIVE;
  52. }
  53. return 0;
  54. }
  55. static int
  56. xpc_get_gru_mq_irq_uv(struct xpc_gru_mq_uv *mq, int cpu, char *irq_name)
  57. {
  58. #if defined CONFIG_X86_64
  59. mq->irq = uv_setup_irq(irq_name, cpu, mq->mmr_blade, mq->mmr_offset);
  60. if (mq->irq < 0) {
  61. dev_err(xpc_part, "uv_setup_irq() returned error=%d\n",
  62. mq->irq);
  63. }
  64. #elif defined CONFIG_IA64_GENERIC || defined CONFIG_IA64_SGI_UV
  65. int mmr_pnode;
  66. unsigned long mmr_value;
  67. if (strcmp(irq_name, XPC_ACTIVATE_IRQ_NAME) == 0)
  68. mq->irq = SGI_XPC_ACTIVATE;
  69. else if (strcmp(irq_name, XPC_NOTIFY_IRQ_NAME) == 0)
  70. mq->irq = SGI_XPC_NOTIFY;
  71. else
  72. return -EINVAL;
  73. mmr_pnode = uv_blade_to_pnode(mq->mmr_blade);
  74. mmr_value = (unsigned long)cpu_physical_id(cpu) << 32 | mq->irq;
  75. uv_write_global_mmr64(mmr_pnode, mq->mmr_offset, mmr_value);
  76. #else
  77. #error not a supported configuration
  78. #endif
  79. return 0;
  80. }
  81. static void
  82. xpc_release_gru_mq_irq_uv(struct xpc_gru_mq_uv *mq)
  83. {
  84. #if defined CONFIG_X86_64
  85. uv_teardown_irq(mq->irq, mq->mmr_blade, mq->mmr_offset);
  86. #elif defined CONFIG_IA64_GENERIC || defined CONFIG_IA64_SGI_UV
  87. int mmr_pnode;
  88. unsigned long mmr_value;
  89. mmr_pnode = uv_blade_to_pnode(mq->mmr_blade);
  90. mmr_value = 1UL << 16;
  91. uv_write_global_mmr64(mmr_pnode, mq->mmr_offset, mmr_value);
  92. #else
  93. #error not a supported configuration
  94. #endif
  95. }
  96. static int
  97. xpc_gru_mq_watchlist_alloc_uv(struct xpc_gru_mq_uv *mq)
  98. {
  99. int ret;
  100. #if defined CONFIG_X86_64
  101. ret = uv_bios_mq_watchlist_alloc(mq->mmr_blade, uv_gpa(mq->address),
  102. mq->order, &mq->mmr_offset);
  103. if (ret < 0) {
  104. dev_err(xpc_part, "uv_bios_mq_watchlist_alloc() failed, "
  105. "ret=%d\n", ret);
  106. return ret;
  107. }
  108. #elif defined CONFIG_IA64_GENERIC || defined CONFIG_IA64_SGI_UV
  109. ret = sn_mq_watchlist_alloc(mq->mmr_blade, uv_gpa(mq->address),
  110. mq->order, &mq->mmr_offset);
  111. if (ret < 0) {
  112. dev_err(xpc_part, "sn_mq_watchlist_alloc() failed, ret=%d\n",
  113. ret);
  114. return -EBUSY;
  115. }
  116. #else
  117. #error not a supported configuration
  118. #endif
  119. mq->watchlist_num = ret;
  120. return 0;
  121. }
  122. static void
  123. xpc_gru_mq_watchlist_free_uv(struct xpc_gru_mq_uv *mq)
  124. {
  125. int ret;
  126. #if defined CONFIG_X86_64
  127. ret = uv_bios_mq_watchlist_free(mq->mmr_blade, mq->watchlist_num);
  128. BUG_ON(ret != BIOS_STATUS_SUCCESS);
  129. #elif defined CONFIG_IA64_GENERIC || defined CONFIG_IA64_SGI_UV
  130. ret = sn_mq_watchlist_free(mq->mmr_blade, mq->watchlist_num);
  131. BUG_ON(ret != SALRET_OK);
  132. #else
  133. #error not a supported configuration
  134. #endif
  135. }
  136. static struct xpc_gru_mq_uv *
  137. xpc_create_gru_mq_uv(unsigned int mq_size, int cpu, char *irq_name,
  138. irq_handler_t irq_handler)
  139. {
  140. enum xp_retval xp_ret;
  141. int ret;
  142. int nid;
  143. int pg_order;
  144. struct page *page;
  145. struct xpc_gru_mq_uv *mq;
  146. mq = kmalloc(sizeof(struct xpc_gru_mq_uv), GFP_KERNEL);
  147. if (mq == NULL) {
  148. dev_err(xpc_part, "xpc_create_gru_mq_uv() failed to kmalloc() "
  149. "a xpc_gru_mq_uv structure\n");
  150. ret = -ENOMEM;
  151. goto out_1;
  152. }
  153. pg_order = get_order(mq_size);
  154. mq->order = pg_order + PAGE_SHIFT;
  155. mq_size = 1UL << mq->order;
  156. mq->mmr_blade = uv_cpu_to_blade_id(cpu);
  157. nid = cpu_to_node(cpu);
  158. page = alloc_pages_node(nid, GFP_KERNEL | __GFP_ZERO | GFP_THISNODE,
  159. pg_order);
  160. if (page == NULL) {
  161. dev_err(xpc_part, "xpc_create_gru_mq_uv() failed to alloc %d "
  162. "bytes of memory on nid=%d for GRU mq\n", mq_size, nid);
  163. ret = -ENOMEM;
  164. goto out_2;
  165. }
  166. mq->address = page_address(page);
  167. ret = gru_create_message_queue(mq->address, mq_size);
  168. if (ret != 0) {
  169. dev_err(xpc_part, "gru_create_message_queue() returned "
  170. "error=%d\n", ret);
  171. ret = -EINVAL;
  172. goto out_3;
  173. }
  174. /* enable generation of irq when GRU mq operation occurs to this mq */
  175. ret = xpc_gru_mq_watchlist_alloc_uv(mq);
  176. if (ret != 0)
  177. goto out_3;
  178. ret = xpc_get_gru_mq_irq_uv(mq, cpu, irq_name);
  179. if (ret != 0)
  180. goto out_4;
  181. ret = request_irq(mq->irq, irq_handler, 0, irq_name, NULL);
  182. if (ret != 0) {
  183. dev_err(xpc_part, "request_irq(irq=%d) returned error=%d\n",
  184. mq->irq, ret);
  185. goto out_5;
  186. }
  187. /* allow other partitions to access this GRU mq */
  188. xp_ret = xp_expand_memprotect(xp_pa(mq->address), mq_size);
  189. if (xp_ret != xpSuccess) {
  190. ret = -EACCES;
  191. goto out_6;
  192. }
  193. return mq;
  194. /* something went wrong */
  195. out_6:
  196. free_irq(mq->irq, NULL);
  197. out_5:
  198. xpc_release_gru_mq_irq_uv(mq);
  199. out_4:
  200. xpc_gru_mq_watchlist_free_uv(mq);
  201. out_3:
  202. free_pages((unsigned long)mq->address, pg_order);
  203. out_2:
  204. kfree(mq);
  205. out_1:
  206. return ERR_PTR(ret);
  207. }
  208. static void
  209. xpc_destroy_gru_mq_uv(struct xpc_gru_mq_uv *mq)
  210. {
  211. unsigned int mq_size;
  212. int pg_order;
  213. int ret;
  214. /* disallow other partitions to access GRU mq */
  215. mq_size = 1UL << mq->order;
  216. ret = xp_restrict_memprotect(xp_pa(mq->address), mq_size);
  217. BUG_ON(ret != xpSuccess);
  218. /* unregister irq handler and release mq irq/vector mapping */
  219. free_irq(mq->irq, NULL);
  220. xpc_release_gru_mq_irq_uv(mq);
  221. /* disable generation of irq when GRU mq op occurs to this mq */
  222. xpc_gru_mq_watchlist_free_uv(mq);
  223. pg_order = mq->order - PAGE_SHIFT;
  224. free_pages((unsigned long)mq->address, pg_order);
  225. kfree(mq);
  226. }
  227. static enum xp_retval
  228. xpc_send_gru_msg(unsigned long mq_gpa, void *msg, size_t msg_size)
  229. {
  230. enum xp_retval xp_ret;
  231. int ret;
  232. while (1) {
  233. ret = gru_send_message_gpa(mq_gpa, msg, msg_size);
  234. if (ret == MQE_OK) {
  235. xp_ret = xpSuccess;
  236. break;
  237. }
  238. if (ret == MQE_QUEUE_FULL) {
  239. dev_dbg(xpc_chan, "gru_send_message_gpa() returned "
  240. "error=MQE_QUEUE_FULL\n");
  241. /* !!! handle QLimit reached; delay & try again */
  242. /* ??? Do we add a limit to the number of retries? */
  243. (void)msleep_interruptible(10);
  244. } else if (ret == MQE_CONGESTION) {
  245. dev_dbg(xpc_chan, "gru_send_message_gpa() returned "
  246. "error=MQE_CONGESTION\n");
  247. /* !!! handle LB Overflow; simply try again */
  248. /* ??? Do we add a limit to the number of retries? */
  249. } else {
  250. /* !!! Currently this is MQE_UNEXPECTED_CB_ERR */
  251. dev_err(xpc_chan, "gru_send_message_gpa() returned "
  252. "error=%d\n", ret);
  253. xp_ret = xpGruSendMqError;
  254. break;
  255. }
  256. }
  257. return xp_ret;
  258. }
  259. static void
  260. xpc_process_activate_IRQ_rcvd_uv(void)
  261. {
  262. unsigned long irq_flags;
  263. short partid;
  264. struct xpc_partition *part;
  265. u8 act_state_req;
  266. DBUG_ON(xpc_activate_IRQ_rcvd == 0);
  267. spin_lock_irqsave(&xpc_activate_IRQ_rcvd_lock, irq_flags);
  268. for (partid = 0; partid < XP_MAX_NPARTITIONS_UV; partid++) {
  269. part = &xpc_partitions[partid];
  270. if (part->sn.uv.act_state_req == 0)
  271. continue;
  272. xpc_activate_IRQ_rcvd--;
  273. BUG_ON(xpc_activate_IRQ_rcvd < 0);
  274. act_state_req = part->sn.uv.act_state_req;
  275. part->sn.uv.act_state_req = 0;
  276. spin_unlock_irqrestore(&xpc_activate_IRQ_rcvd_lock, irq_flags);
  277. if (act_state_req == XPC_P_ASR_ACTIVATE_UV) {
  278. if (part->act_state == XPC_P_AS_INACTIVE)
  279. xpc_activate_partition(part);
  280. else if (part->act_state == XPC_P_AS_DEACTIVATING)
  281. XPC_DEACTIVATE_PARTITION(part, xpReactivating);
  282. } else if (act_state_req == XPC_P_ASR_REACTIVATE_UV) {
  283. if (part->act_state == XPC_P_AS_INACTIVE)
  284. xpc_activate_partition(part);
  285. else
  286. XPC_DEACTIVATE_PARTITION(part, xpReactivating);
  287. } else if (act_state_req == XPC_P_ASR_DEACTIVATE_UV) {
  288. XPC_DEACTIVATE_PARTITION(part, part->sn.uv.reason);
  289. } else {
  290. BUG();
  291. }
  292. spin_lock_irqsave(&xpc_activate_IRQ_rcvd_lock, irq_flags);
  293. if (xpc_activate_IRQ_rcvd == 0)
  294. break;
  295. }
  296. spin_unlock_irqrestore(&xpc_activate_IRQ_rcvd_lock, irq_flags);
  297. }
  298. static void
  299. xpc_handle_activate_mq_msg_uv(struct xpc_partition *part,
  300. struct xpc_activate_mq_msghdr_uv *msg_hdr,
  301. int *wakeup_hb_checker)
  302. {
  303. unsigned long irq_flags;
  304. struct xpc_partition_uv *part_uv = &part->sn.uv;
  305. struct xpc_openclose_args *args;
  306. part_uv->remote_act_state = msg_hdr->act_state;
  307. switch (msg_hdr->type) {
  308. case XPC_ACTIVATE_MQ_MSG_SYNC_ACT_STATE_UV:
  309. /* syncing of remote_act_state was just done above */
  310. break;
  311. case XPC_ACTIVATE_MQ_MSG_INC_HEARTBEAT_UV: {
  312. struct xpc_activate_mq_msg_heartbeat_req_uv *msg;
  313. msg = container_of(msg_hdr,
  314. struct xpc_activate_mq_msg_heartbeat_req_uv,
  315. hdr);
  316. part_uv->heartbeat = msg->heartbeat;
  317. break;
  318. }
  319. case XPC_ACTIVATE_MQ_MSG_OFFLINE_HEARTBEAT_UV: {
  320. struct xpc_activate_mq_msg_heartbeat_req_uv *msg;
  321. msg = container_of(msg_hdr,
  322. struct xpc_activate_mq_msg_heartbeat_req_uv,
  323. hdr);
  324. part_uv->heartbeat = msg->heartbeat;
  325. spin_lock_irqsave(&part_uv->flags_lock, irq_flags);
  326. part_uv->flags |= XPC_P_HEARTBEAT_OFFLINE_UV;
  327. spin_unlock_irqrestore(&part_uv->flags_lock, irq_flags);
  328. break;
  329. }
  330. case XPC_ACTIVATE_MQ_MSG_ONLINE_HEARTBEAT_UV: {
  331. struct xpc_activate_mq_msg_heartbeat_req_uv *msg;
  332. msg = container_of(msg_hdr,
  333. struct xpc_activate_mq_msg_heartbeat_req_uv,
  334. hdr);
  335. part_uv->heartbeat = msg->heartbeat;
  336. spin_lock_irqsave(&part_uv->flags_lock, irq_flags);
  337. part_uv->flags &= ~XPC_P_HEARTBEAT_OFFLINE_UV;
  338. spin_unlock_irqrestore(&part_uv->flags_lock, irq_flags);
  339. break;
  340. }
  341. case XPC_ACTIVATE_MQ_MSG_ACTIVATE_REQ_UV: {
  342. struct xpc_activate_mq_msg_activate_req_uv *msg;
  343. /*
  344. * ??? Do we deal here with ts_jiffies being different
  345. * ??? if act_state != XPC_P_AS_INACTIVE instead of
  346. * ??? below?
  347. */
  348. msg = container_of(msg_hdr, struct
  349. xpc_activate_mq_msg_activate_req_uv, hdr);
  350. spin_lock_irqsave(&xpc_activate_IRQ_rcvd_lock, irq_flags);
  351. if (part_uv->act_state_req == 0)
  352. xpc_activate_IRQ_rcvd++;
  353. part_uv->act_state_req = XPC_P_ASR_ACTIVATE_UV;
  354. part->remote_rp_pa = msg->rp_gpa; /* !!! _pa is _gpa */
  355. part->remote_rp_ts_jiffies = msg_hdr->rp_ts_jiffies;
  356. part_uv->remote_activate_mq_gpa = msg->activate_mq_gpa;
  357. spin_unlock_irqrestore(&xpc_activate_IRQ_rcvd_lock, irq_flags);
  358. (*wakeup_hb_checker)++;
  359. break;
  360. }
  361. case XPC_ACTIVATE_MQ_MSG_DEACTIVATE_REQ_UV: {
  362. struct xpc_activate_mq_msg_deactivate_req_uv *msg;
  363. msg = container_of(msg_hdr, struct
  364. xpc_activate_mq_msg_deactivate_req_uv, hdr);
  365. spin_lock_irqsave(&xpc_activate_IRQ_rcvd_lock, irq_flags);
  366. if (part_uv->act_state_req == 0)
  367. xpc_activate_IRQ_rcvd++;
  368. part_uv->act_state_req = XPC_P_ASR_DEACTIVATE_UV;
  369. part_uv->reason = msg->reason;
  370. spin_unlock_irqrestore(&xpc_activate_IRQ_rcvd_lock, irq_flags);
  371. (*wakeup_hb_checker)++;
  372. return;
  373. }
  374. case XPC_ACTIVATE_MQ_MSG_CHCTL_CLOSEREQUEST_UV: {
  375. struct xpc_activate_mq_msg_chctl_closerequest_uv *msg;
  376. msg = container_of(msg_hdr, struct
  377. xpc_activate_mq_msg_chctl_closerequest_uv,
  378. hdr);
  379. args = &part->remote_openclose_args[msg->ch_number];
  380. args->reason = msg->reason;
  381. spin_lock_irqsave(&part->chctl_lock, irq_flags);
  382. part->chctl.flags[msg->ch_number] |= XPC_CHCTL_CLOSEREQUEST;
  383. spin_unlock_irqrestore(&part->chctl_lock, irq_flags);
  384. xpc_wakeup_channel_mgr(part);
  385. break;
  386. }
  387. case XPC_ACTIVATE_MQ_MSG_CHCTL_CLOSEREPLY_UV: {
  388. struct xpc_activate_mq_msg_chctl_closereply_uv *msg;
  389. msg = container_of(msg_hdr, struct
  390. xpc_activate_mq_msg_chctl_closereply_uv,
  391. hdr);
  392. spin_lock_irqsave(&part->chctl_lock, irq_flags);
  393. part->chctl.flags[msg->ch_number] |= XPC_CHCTL_CLOSEREPLY;
  394. spin_unlock_irqrestore(&part->chctl_lock, irq_flags);
  395. xpc_wakeup_channel_mgr(part);
  396. break;
  397. }
  398. case XPC_ACTIVATE_MQ_MSG_CHCTL_OPENREQUEST_UV: {
  399. struct xpc_activate_mq_msg_chctl_openrequest_uv *msg;
  400. msg = container_of(msg_hdr, struct
  401. xpc_activate_mq_msg_chctl_openrequest_uv,
  402. hdr);
  403. args = &part->remote_openclose_args[msg->ch_number];
  404. args->entry_size = msg->entry_size;
  405. args->local_nentries = msg->local_nentries;
  406. spin_lock_irqsave(&part->chctl_lock, irq_flags);
  407. part->chctl.flags[msg->ch_number] |= XPC_CHCTL_OPENREQUEST;
  408. spin_unlock_irqrestore(&part->chctl_lock, irq_flags);
  409. xpc_wakeup_channel_mgr(part);
  410. break;
  411. }
  412. case XPC_ACTIVATE_MQ_MSG_CHCTL_OPENREPLY_UV: {
  413. struct xpc_activate_mq_msg_chctl_openreply_uv *msg;
  414. msg = container_of(msg_hdr, struct
  415. xpc_activate_mq_msg_chctl_openreply_uv, hdr);
  416. args = &part->remote_openclose_args[msg->ch_number];
  417. args->remote_nentries = msg->remote_nentries;
  418. args->local_nentries = msg->local_nentries;
  419. args->local_msgqueue_pa = msg->local_notify_mq_gpa;
  420. spin_lock_irqsave(&part->chctl_lock, irq_flags);
  421. part->chctl.flags[msg->ch_number] |= XPC_CHCTL_OPENREPLY;
  422. spin_unlock_irqrestore(&part->chctl_lock, irq_flags);
  423. xpc_wakeup_channel_mgr(part);
  424. break;
  425. }
  426. case XPC_ACTIVATE_MQ_MSG_MARK_ENGAGED_UV:
  427. spin_lock_irqsave(&part_uv->flags_lock, irq_flags);
  428. part_uv->flags |= XPC_P_ENGAGED_UV;
  429. spin_unlock_irqrestore(&part_uv->flags_lock, irq_flags);
  430. break;
  431. case XPC_ACTIVATE_MQ_MSG_MARK_DISENGAGED_UV:
  432. spin_lock_irqsave(&part_uv->flags_lock, irq_flags);
  433. part_uv->flags &= ~XPC_P_ENGAGED_UV;
  434. spin_unlock_irqrestore(&part_uv->flags_lock, irq_flags);
  435. break;
  436. default:
  437. dev_err(xpc_part, "received unknown activate_mq msg type=%d "
  438. "from partition=%d\n", msg_hdr->type, XPC_PARTID(part));
  439. /* get hb checker to deactivate from the remote partition */
  440. spin_lock_irqsave(&xpc_activate_IRQ_rcvd_lock, irq_flags);
  441. if (part_uv->act_state_req == 0)
  442. xpc_activate_IRQ_rcvd++;
  443. part_uv->act_state_req = XPC_P_ASR_DEACTIVATE_UV;
  444. part_uv->reason = xpBadMsgType;
  445. spin_unlock_irqrestore(&xpc_activate_IRQ_rcvd_lock, irq_flags);
  446. (*wakeup_hb_checker)++;
  447. return;
  448. }
  449. if (msg_hdr->rp_ts_jiffies != part->remote_rp_ts_jiffies &&
  450. part->remote_rp_ts_jiffies != 0) {
  451. /*
  452. * ??? Does what we do here need to be sensitive to
  453. * ??? act_state or remote_act_state?
  454. */
  455. spin_lock_irqsave(&xpc_activate_IRQ_rcvd_lock, irq_flags);
  456. if (part_uv->act_state_req == 0)
  457. xpc_activate_IRQ_rcvd++;
  458. part_uv->act_state_req = XPC_P_ASR_REACTIVATE_UV;
  459. spin_unlock_irqrestore(&xpc_activate_IRQ_rcvd_lock, irq_flags);
  460. (*wakeup_hb_checker)++;
  461. }
  462. }
  463. static irqreturn_t
  464. xpc_handle_activate_IRQ_uv(int irq, void *dev_id)
  465. {
  466. struct xpc_activate_mq_msghdr_uv *msg_hdr;
  467. short partid;
  468. struct xpc_partition *part;
  469. int wakeup_hb_checker = 0;
  470. while (1) {
  471. msg_hdr = gru_get_next_message(xpc_activate_mq_uv->address);
  472. if (msg_hdr == NULL)
  473. break;
  474. partid = msg_hdr->partid;
  475. if (partid < 0 || partid >= XP_MAX_NPARTITIONS_UV) {
  476. dev_err(xpc_part, "xpc_handle_activate_IRQ_uv() "
  477. "received invalid partid=0x%x in message\n",
  478. partid);
  479. } else {
  480. part = &xpc_partitions[partid];
  481. if (xpc_part_ref(part)) {
  482. xpc_handle_activate_mq_msg_uv(part, msg_hdr,
  483. &wakeup_hb_checker);
  484. xpc_part_deref(part);
  485. }
  486. }
  487. gru_free_message(xpc_activate_mq_uv->address, msg_hdr);
  488. }
  489. if (wakeup_hb_checker)
  490. wake_up_interruptible(&xpc_activate_IRQ_wq);
  491. return IRQ_HANDLED;
  492. }
  493. static enum xp_retval
  494. xpc_send_activate_IRQ_uv(struct xpc_partition *part, void *msg, size_t msg_size,
  495. int msg_type)
  496. {
  497. struct xpc_activate_mq_msghdr_uv *msg_hdr = msg;
  498. DBUG_ON(msg_size > XPC_ACTIVATE_MSG_SIZE_UV);
  499. msg_hdr->type = msg_type;
  500. msg_hdr->partid = XPC_PARTID(part);
  501. msg_hdr->act_state = part->act_state;
  502. msg_hdr->rp_ts_jiffies = xpc_rsvd_page->ts_jiffies;
  503. /* ??? Is holding a spin_lock (ch->lock) during this call a bad idea? */
  504. return xpc_send_gru_msg(part->sn.uv.remote_activate_mq_gpa, msg,
  505. msg_size);
  506. }
  507. static void
  508. xpc_send_activate_IRQ_part_uv(struct xpc_partition *part, void *msg,
  509. size_t msg_size, int msg_type)
  510. {
  511. enum xp_retval ret;
  512. ret = xpc_send_activate_IRQ_uv(part, msg, msg_size, msg_type);
  513. if (unlikely(ret != xpSuccess))
  514. XPC_DEACTIVATE_PARTITION(part, ret);
  515. }
  516. static void
  517. xpc_send_activate_IRQ_ch_uv(struct xpc_channel *ch, unsigned long *irq_flags,
  518. void *msg, size_t msg_size, int msg_type)
  519. {
  520. struct xpc_partition *part = &xpc_partitions[ch->number];
  521. enum xp_retval ret;
  522. ret = xpc_send_activate_IRQ_uv(part, msg, msg_size, msg_type);
  523. if (unlikely(ret != xpSuccess)) {
  524. if (irq_flags != NULL)
  525. spin_unlock_irqrestore(&ch->lock, *irq_flags);
  526. XPC_DEACTIVATE_PARTITION(part, ret);
  527. if (irq_flags != NULL)
  528. spin_lock_irqsave(&ch->lock, *irq_flags);
  529. }
  530. }
  531. static void
  532. xpc_send_local_activate_IRQ_uv(struct xpc_partition *part, int act_state_req)
  533. {
  534. unsigned long irq_flags;
  535. struct xpc_partition_uv *part_uv = &part->sn.uv;
  536. /*
  537. * !!! Make our side think that the remote partition sent an activate
  538. * !!! message our way by doing what the activate IRQ handler would
  539. * !!! do had one really been sent.
  540. */
  541. spin_lock_irqsave(&xpc_activate_IRQ_rcvd_lock, irq_flags);
  542. if (part_uv->act_state_req == 0)
  543. xpc_activate_IRQ_rcvd++;
  544. part_uv->act_state_req = act_state_req;
  545. spin_unlock_irqrestore(&xpc_activate_IRQ_rcvd_lock, irq_flags);
  546. wake_up_interruptible(&xpc_activate_IRQ_wq);
  547. }
  548. static enum xp_retval
  549. xpc_get_partition_rsvd_page_pa_uv(void *buf, u64 *cookie, unsigned long *rp_pa,
  550. size_t *len)
  551. {
  552. s64 status;
  553. enum xp_retval ret;
  554. #if defined CONFIG_X86_64
  555. status = uv_bios_reserved_page_pa((u64)buf, cookie, (u64 *)rp_pa,
  556. (u64 *)len);
  557. if (status == BIOS_STATUS_SUCCESS)
  558. ret = xpSuccess;
  559. else if (status == BIOS_STATUS_MORE_PASSES)
  560. ret = xpNeedMoreInfo;
  561. else
  562. ret = xpBiosError;
  563. #elif defined CONFIG_IA64_GENERIC || defined CONFIG_IA64_SGI_UV
  564. status = sn_partition_reserved_page_pa((u64)buf, cookie, rp_pa, len);
  565. if (status == SALRET_OK)
  566. ret = xpSuccess;
  567. else if (status == SALRET_MORE_PASSES)
  568. ret = xpNeedMoreInfo;
  569. else
  570. ret = xpSalError;
  571. #else
  572. #error not a supported configuration
  573. #endif
  574. return ret;
  575. }
  576. static int
  577. xpc_setup_rsvd_page_sn_uv(struct xpc_rsvd_page *rp)
  578. {
  579. rp->sn.activate_mq_gpa = uv_gpa(xpc_activate_mq_uv->address);
  580. return 0;
  581. }
  582. static void
  583. xpc_send_heartbeat_uv(int msg_type)
  584. {
  585. short partid;
  586. struct xpc_partition *part;
  587. struct xpc_activate_mq_msg_heartbeat_req_uv msg;
  588. /*
  589. * !!! On uv we're broadcasting a heartbeat message every 5 seconds.
  590. * !!! Whereas on sn2 we're bte_copy'ng the heartbeat info every 20
  591. * !!! seconds. This is an increase in numalink traffic.
  592. * ??? Is this good?
  593. */
  594. msg.heartbeat = atomic64_inc_return(&xpc_heartbeat_uv);
  595. partid = find_first_bit(xpc_heartbeating_to_mask_uv,
  596. XP_MAX_NPARTITIONS_UV);
  597. while (partid < XP_MAX_NPARTITIONS_UV) {
  598. part = &xpc_partitions[partid];
  599. xpc_send_activate_IRQ_part_uv(part, &msg, sizeof(msg),
  600. msg_type);
  601. partid = find_next_bit(xpc_heartbeating_to_mask_uv,
  602. XP_MAX_NPARTITIONS_UV, partid + 1);
  603. }
  604. }
  605. static void
  606. xpc_increment_heartbeat_uv(void)
  607. {
  608. xpc_send_heartbeat_uv(XPC_ACTIVATE_MQ_MSG_INC_HEARTBEAT_UV);
  609. }
  610. static void
  611. xpc_offline_heartbeat_uv(void)
  612. {
  613. xpc_send_heartbeat_uv(XPC_ACTIVATE_MQ_MSG_OFFLINE_HEARTBEAT_UV);
  614. }
  615. static void
  616. xpc_online_heartbeat_uv(void)
  617. {
  618. xpc_send_heartbeat_uv(XPC_ACTIVATE_MQ_MSG_ONLINE_HEARTBEAT_UV);
  619. }
  620. static void
  621. xpc_heartbeat_init_uv(void)
  622. {
  623. atomic64_set(&xpc_heartbeat_uv, 0);
  624. bitmap_zero(xpc_heartbeating_to_mask_uv, XP_MAX_NPARTITIONS_UV);
  625. xpc_heartbeating_to_mask = &xpc_heartbeating_to_mask_uv[0];
  626. }
  627. static void
  628. xpc_heartbeat_exit_uv(void)
  629. {
  630. xpc_send_heartbeat_uv(XPC_ACTIVATE_MQ_MSG_OFFLINE_HEARTBEAT_UV);
  631. }
  632. static enum xp_retval
  633. xpc_get_remote_heartbeat_uv(struct xpc_partition *part)
  634. {
  635. struct xpc_partition_uv *part_uv = &part->sn.uv;
  636. enum xp_retval ret = xpNoHeartbeat;
  637. if (part_uv->remote_act_state != XPC_P_AS_INACTIVE &&
  638. part_uv->remote_act_state != XPC_P_AS_DEACTIVATING) {
  639. if (part_uv->heartbeat != part->last_heartbeat ||
  640. (part_uv->flags & XPC_P_HEARTBEAT_OFFLINE_UV)) {
  641. part->last_heartbeat = part_uv->heartbeat;
  642. ret = xpSuccess;
  643. }
  644. }
  645. return ret;
  646. }
  647. static void
  648. xpc_request_partition_activation_uv(struct xpc_rsvd_page *remote_rp,
  649. unsigned long remote_rp_gpa, int nasid)
  650. {
  651. short partid = remote_rp->SAL_partid;
  652. struct xpc_partition *part = &xpc_partitions[partid];
  653. struct xpc_activate_mq_msg_activate_req_uv msg;
  654. part->remote_rp_pa = remote_rp_gpa; /* !!! _pa here is really _gpa */
  655. part->remote_rp_ts_jiffies = remote_rp->ts_jiffies;
  656. part->sn.uv.remote_activate_mq_gpa = remote_rp->sn.activate_mq_gpa;
  657. /*
  658. * ??? Is it a good idea to make this conditional on what is
  659. * ??? potentially stale state information?
  660. */
  661. if (part->sn.uv.remote_act_state == XPC_P_AS_INACTIVE) {
  662. msg.rp_gpa = uv_gpa(xpc_rsvd_page);
  663. msg.activate_mq_gpa = xpc_rsvd_page->sn.activate_mq_gpa;
  664. xpc_send_activate_IRQ_part_uv(part, &msg, sizeof(msg),
  665. XPC_ACTIVATE_MQ_MSG_ACTIVATE_REQ_UV);
  666. }
  667. if (part->act_state == XPC_P_AS_INACTIVE)
  668. xpc_send_local_activate_IRQ_uv(part, XPC_P_ASR_ACTIVATE_UV);
  669. }
  670. static void
  671. xpc_request_partition_reactivation_uv(struct xpc_partition *part)
  672. {
  673. xpc_send_local_activate_IRQ_uv(part, XPC_P_ASR_ACTIVATE_UV);
  674. }
  675. static void
  676. xpc_request_partition_deactivation_uv(struct xpc_partition *part)
  677. {
  678. struct xpc_activate_mq_msg_deactivate_req_uv msg;
  679. /*
  680. * ??? Is it a good idea to make this conditional on what is
  681. * ??? potentially stale state information?
  682. */
  683. if (part->sn.uv.remote_act_state != XPC_P_AS_DEACTIVATING &&
  684. part->sn.uv.remote_act_state != XPC_P_AS_INACTIVE) {
  685. msg.reason = part->reason;
  686. xpc_send_activate_IRQ_part_uv(part, &msg, sizeof(msg),
  687. XPC_ACTIVATE_MQ_MSG_DEACTIVATE_REQ_UV);
  688. }
  689. }
  690. static void
  691. xpc_cancel_partition_deactivation_request_uv(struct xpc_partition *part)
  692. {
  693. /* nothing needs to be done */
  694. return;
  695. }
  696. static void
  697. xpc_init_fifo_uv(struct xpc_fifo_head_uv *head)
  698. {
  699. head->first = NULL;
  700. head->last = NULL;
  701. spin_lock_init(&head->lock);
  702. head->n_entries = 0;
  703. }
  704. static void *
  705. xpc_get_fifo_entry_uv(struct xpc_fifo_head_uv *head)
  706. {
  707. unsigned long irq_flags;
  708. struct xpc_fifo_entry_uv *first;
  709. spin_lock_irqsave(&head->lock, irq_flags);
  710. first = head->first;
  711. if (head->first != NULL) {
  712. head->first = first->next;
  713. if (head->first == NULL)
  714. head->last = NULL;
  715. }
  716. head->n_entries++;
  717. spin_unlock_irqrestore(&head->lock, irq_flags);
  718. first->next = NULL;
  719. return first;
  720. }
  721. static void
  722. xpc_put_fifo_entry_uv(struct xpc_fifo_head_uv *head,
  723. struct xpc_fifo_entry_uv *last)
  724. {
  725. unsigned long irq_flags;
  726. last->next = NULL;
  727. spin_lock_irqsave(&head->lock, irq_flags);
  728. if (head->last != NULL)
  729. head->last->next = last;
  730. else
  731. head->first = last;
  732. head->last = last;
  733. head->n_entries--;
  734. BUG_ON(head->n_entries < 0);
  735. spin_unlock_irqrestore(&head->lock, irq_flags);
  736. }
  737. static int
  738. xpc_n_of_fifo_entries_uv(struct xpc_fifo_head_uv *head)
  739. {
  740. return head->n_entries;
  741. }
  742. /*
  743. * Setup the channel structures that are uv specific.
  744. */
  745. static enum xp_retval
  746. xpc_setup_ch_structures_sn_uv(struct xpc_partition *part)
  747. {
  748. struct xpc_channel_uv *ch_uv;
  749. int ch_number;
  750. for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
  751. ch_uv = &part->channels[ch_number].sn.uv;
  752. xpc_init_fifo_uv(&ch_uv->msg_slot_free_list);
  753. xpc_init_fifo_uv(&ch_uv->recv_msg_list);
  754. }
  755. return xpSuccess;
  756. }
  757. /*
  758. * Teardown the channel structures that are uv specific.
  759. */
  760. static void
  761. xpc_teardown_ch_structures_sn_uv(struct xpc_partition *part)
  762. {
  763. /* nothing needs to be done */
  764. return;
  765. }
  766. static enum xp_retval
  767. xpc_make_first_contact_uv(struct xpc_partition *part)
  768. {
  769. struct xpc_activate_mq_msg_uv msg;
  770. /*
  771. * We send a sync msg to get the remote partition's remote_act_state
  772. * updated to our current act_state which at this point should
  773. * be XPC_P_AS_ACTIVATING.
  774. */
  775. xpc_send_activate_IRQ_part_uv(part, &msg, sizeof(msg),
  776. XPC_ACTIVATE_MQ_MSG_SYNC_ACT_STATE_UV);
  777. while (part->sn.uv.remote_act_state != XPC_P_AS_ACTIVATING) {
  778. dev_dbg(xpc_part, "waiting to make first contact with "
  779. "partition %d\n", XPC_PARTID(part));
  780. /* wait a 1/4 of a second or so */
  781. (void)msleep_interruptible(250);
  782. if (part->act_state == XPC_P_AS_DEACTIVATING)
  783. return part->reason;
  784. }
  785. return xpSuccess;
  786. }
  787. static u64
  788. xpc_get_chctl_all_flags_uv(struct xpc_partition *part)
  789. {
  790. unsigned long irq_flags;
  791. union xpc_channel_ctl_flags chctl;
  792. spin_lock_irqsave(&part->chctl_lock, irq_flags);
  793. chctl = part->chctl;
  794. if (chctl.all_flags != 0)
  795. part->chctl.all_flags = 0;
  796. spin_unlock_irqrestore(&part->chctl_lock, irq_flags);
  797. return chctl.all_flags;
  798. }
  799. static enum xp_retval
  800. xpc_allocate_send_msg_slot_uv(struct xpc_channel *ch)
  801. {
  802. struct xpc_channel_uv *ch_uv = &ch->sn.uv;
  803. struct xpc_send_msg_slot_uv *msg_slot;
  804. unsigned long irq_flags;
  805. int nentries;
  806. int entry;
  807. size_t nbytes;
  808. for (nentries = ch->local_nentries; nentries > 0; nentries--) {
  809. nbytes = nentries * sizeof(struct xpc_send_msg_slot_uv);
  810. ch_uv->send_msg_slots = kzalloc(nbytes, GFP_KERNEL);
  811. if (ch_uv->send_msg_slots == NULL)
  812. continue;
  813. for (entry = 0; entry < nentries; entry++) {
  814. msg_slot = &ch_uv->send_msg_slots[entry];
  815. msg_slot->msg_slot_number = entry;
  816. xpc_put_fifo_entry_uv(&ch_uv->msg_slot_free_list,
  817. &msg_slot->next);
  818. }
  819. spin_lock_irqsave(&ch->lock, irq_flags);
  820. if (nentries < ch->local_nentries)
  821. ch->local_nentries = nentries;
  822. spin_unlock_irqrestore(&ch->lock, irq_flags);
  823. return xpSuccess;
  824. }
  825. return xpNoMemory;
  826. }
  827. static enum xp_retval
  828. xpc_allocate_recv_msg_slot_uv(struct xpc_channel *ch)
  829. {
  830. struct xpc_channel_uv *ch_uv = &ch->sn.uv;
  831. struct xpc_notify_mq_msg_uv *msg_slot;
  832. unsigned long irq_flags;
  833. int nentries;
  834. int entry;
  835. size_t nbytes;
  836. for (nentries = ch->remote_nentries; nentries > 0; nentries--) {
  837. nbytes = nentries * ch->entry_size;
  838. ch_uv->recv_msg_slots = kzalloc(nbytes, GFP_KERNEL);
  839. if (ch_uv->recv_msg_slots == NULL)
  840. continue;
  841. for (entry = 0; entry < nentries; entry++) {
  842. msg_slot = ch_uv->recv_msg_slots + entry *
  843. ch->entry_size;
  844. msg_slot->hdr.msg_slot_number = entry;
  845. }
  846. spin_lock_irqsave(&ch->lock, irq_flags);
  847. if (nentries < ch->remote_nentries)
  848. ch->remote_nentries = nentries;
  849. spin_unlock_irqrestore(&ch->lock, irq_flags);
  850. return xpSuccess;
  851. }
  852. return xpNoMemory;
  853. }
  854. /*
  855. * Allocate msg_slots associated with the channel.
  856. */
  857. static enum xp_retval
  858. xpc_setup_msg_structures_uv(struct xpc_channel *ch)
  859. {
  860. static enum xp_retval ret;
  861. struct xpc_channel_uv *ch_uv = &ch->sn.uv;
  862. DBUG_ON(ch->flags & XPC_C_SETUP);
  863. ret = xpc_allocate_send_msg_slot_uv(ch);
  864. if (ret == xpSuccess) {
  865. ret = xpc_allocate_recv_msg_slot_uv(ch);
  866. if (ret != xpSuccess) {
  867. kfree(ch_uv->send_msg_slots);
  868. xpc_init_fifo_uv(&ch_uv->msg_slot_free_list);
  869. }
  870. }
  871. return ret;
  872. }
  873. /*
  874. * Free up msg_slots and clear other stuff that were setup for the specified
  875. * channel.
  876. */
  877. static void
  878. xpc_teardown_msg_structures_uv(struct xpc_channel *ch)
  879. {
  880. struct xpc_channel_uv *ch_uv = &ch->sn.uv;
  881. DBUG_ON(!spin_is_locked(&ch->lock));
  882. ch_uv->remote_notify_mq_gpa = 0;
  883. if (ch->flags & XPC_C_SETUP) {
  884. xpc_init_fifo_uv(&ch_uv->msg_slot_free_list);
  885. kfree(ch_uv->send_msg_slots);
  886. xpc_init_fifo_uv(&ch_uv->recv_msg_list);
  887. kfree(ch_uv->recv_msg_slots);
  888. }
  889. }
  890. static void
  891. xpc_send_chctl_closerequest_uv(struct xpc_channel *ch, unsigned long *irq_flags)
  892. {
  893. struct xpc_activate_mq_msg_chctl_closerequest_uv msg;
  894. msg.ch_number = ch->number;
  895. msg.reason = ch->reason;
  896. xpc_send_activate_IRQ_ch_uv(ch, irq_flags, &msg, sizeof(msg),
  897. XPC_ACTIVATE_MQ_MSG_CHCTL_CLOSEREQUEST_UV);
  898. }
  899. static void
  900. xpc_send_chctl_closereply_uv(struct xpc_channel *ch, unsigned long *irq_flags)
  901. {
  902. struct xpc_activate_mq_msg_chctl_closereply_uv msg;
  903. msg.ch_number = ch->number;
  904. xpc_send_activate_IRQ_ch_uv(ch, irq_flags, &msg, sizeof(msg),
  905. XPC_ACTIVATE_MQ_MSG_CHCTL_CLOSEREPLY_UV);
  906. }
  907. static void
  908. xpc_send_chctl_openrequest_uv(struct xpc_channel *ch, unsigned long *irq_flags)
  909. {
  910. struct xpc_activate_mq_msg_chctl_openrequest_uv msg;
  911. msg.ch_number = ch->number;
  912. msg.entry_size = ch->entry_size;
  913. msg.local_nentries = ch->local_nentries;
  914. xpc_send_activate_IRQ_ch_uv(ch, irq_flags, &msg, sizeof(msg),
  915. XPC_ACTIVATE_MQ_MSG_CHCTL_OPENREQUEST_UV);
  916. }
  917. static void
  918. xpc_send_chctl_openreply_uv(struct xpc_channel *ch, unsigned long *irq_flags)
  919. {
  920. struct xpc_activate_mq_msg_chctl_openreply_uv msg;
  921. msg.ch_number = ch->number;
  922. msg.local_nentries = ch->local_nentries;
  923. msg.remote_nentries = ch->remote_nentries;
  924. msg.local_notify_mq_gpa = uv_gpa(xpc_notify_mq_uv);
  925. xpc_send_activate_IRQ_ch_uv(ch, irq_flags, &msg, sizeof(msg),
  926. XPC_ACTIVATE_MQ_MSG_CHCTL_OPENREPLY_UV);
  927. }
  928. static void
  929. xpc_send_chctl_local_msgrequest_uv(struct xpc_partition *part, int ch_number)
  930. {
  931. unsigned long irq_flags;
  932. spin_lock_irqsave(&part->chctl_lock, irq_flags);
  933. part->chctl.flags[ch_number] |= XPC_CHCTL_MSGREQUEST;
  934. spin_unlock_irqrestore(&part->chctl_lock, irq_flags);
  935. xpc_wakeup_channel_mgr(part);
  936. }
  937. static void
  938. xpc_save_remote_msgqueue_pa_uv(struct xpc_channel *ch,
  939. unsigned long msgqueue_pa)
  940. {
  941. ch->sn.uv.remote_notify_mq_gpa = msgqueue_pa;
  942. }
  943. static void
  944. xpc_indicate_partition_engaged_uv(struct xpc_partition *part)
  945. {
  946. struct xpc_activate_mq_msg_uv msg;
  947. xpc_send_activate_IRQ_part_uv(part, &msg, sizeof(msg),
  948. XPC_ACTIVATE_MQ_MSG_MARK_ENGAGED_UV);
  949. }
  950. static void
  951. xpc_indicate_partition_disengaged_uv(struct xpc_partition *part)
  952. {
  953. struct xpc_activate_mq_msg_uv msg;
  954. xpc_send_activate_IRQ_part_uv(part, &msg, sizeof(msg),
  955. XPC_ACTIVATE_MQ_MSG_MARK_DISENGAGED_UV);
  956. }
  957. static void
  958. xpc_assume_partition_disengaged_uv(short partid)
  959. {
  960. struct xpc_partition_uv *part_uv = &xpc_partitions[partid].sn.uv;
  961. unsigned long irq_flags;
  962. spin_lock_irqsave(&part_uv->flags_lock, irq_flags);
  963. part_uv->flags &= ~XPC_P_ENGAGED_UV;
  964. spin_unlock_irqrestore(&part_uv->flags_lock, irq_flags);
  965. }
  966. static int
  967. xpc_partition_engaged_uv(short partid)
  968. {
  969. return (xpc_partitions[partid].sn.uv.flags & XPC_P_ENGAGED_UV) != 0;
  970. }
  971. static int
  972. xpc_any_partition_engaged_uv(void)
  973. {
  974. struct xpc_partition_uv *part_uv;
  975. short partid;
  976. for (partid = 0; partid < XP_MAX_NPARTITIONS_UV; partid++) {
  977. part_uv = &xpc_partitions[partid].sn.uv;
  978. if ((part_uv->flags & XPC_P_ENGAGED_UV) != 0)
  979. return 1;
  980. }
  981. return 0;
  982. }
  983. static enum xp_retval
  984. xpc_allocate_msg_slot_uv(struct xpc_channel *ch, u32 flags,
  985. struct xpc_send_msg_slot_uv **address_of_msg_slot)
  986. {
  987. enum xp_retval ret;
  988. struct xpc_send_msg_slot_uv *msg_slot;
  989. struct xpc_fifo_entry_uv *entry;
  990. while (1) {
  991. entry = xpc_get_fifo_entry_uv(&ch->sn.uv.msg_slot_free_list);
  992. if (entry != NULL)
  993. break;
  994. if (flags & XPC_NOWAIT)
  995. return xpNoWait;
  996. ret = xpc_allocate_msg_wait(ch);
  997. if (ret != xpInterrupted && ret != xpTimeout)
  998. return ret;
  999. }
  1000. msg_slot = container_of(entry, struct xpc_send_msg_slot_uv, next);
  1001. *address_of_msg_slot = msg_slot;
  1002. return xpSuccess;
  1003. }
  1004. static void
  1005. xpc_free_msg_slot_uv(struct xpc_channel *ch,
  1006. struct xpc_send_msg_slot_uv *msg_slot)
  1007. {
  1008. xpc_put_fifo_entry_uv(&ch->sn.uv.msg_slot_free_list, &msg_slot->next);
  1009. /* wakeup anyone waiting for a free msg slot */
  1010. if (atomic_read(&ch->n_on_msg_allocate_wq) > 0)
  1011. wake_up(&ch->msg_allocate_wq);
  1012. }
  1013. static void
  1014. xpc_notify_sender_uv(struct xpc_channel *ch,
  1015. struct xpc_send_msg_slot_uv *msg_slot,
  1016. enum xp_retval reason)
  1017. {
  1018. xpc_notify_func func = msg_slot->func;
  1019. if (func != NULL && cmpxchg(&msg_slot->func, func, NULL) == func) {
  1020. atomic_dec(&ch->n_to_notify);
  1021. dev_dbg(xpc_chan, "msg_slot->func() called, msg_slot=0x%p "
  1022. "msg_slot_number=%d partid=%d channel=%d\n", msg_slot,
  1023. msg_slot->msg_slot_number, ch->partid, ch->number);
  1024. func(reason, ch->partid, ch->number, msg_slot->key);
  1025. dev_dbg(xpc_chan, "msg_slot->func() returned, msg_slot=0x%p "
  1026. "msg_slot_number=%d partid=%d channel=%d\n", msg_slot,
  1027. msg_slot->msg_slot_number, ch->partid, ch->number);
  1028. }
  1029. }
  1030. static void
  1031. xpc_handle_notify_mq_ack_uv(struct xpc_channel *ch,
  1032. struct xpc_notify_mq_msg_uv *msg)
  1033. {
  1034. struct xpc_send_msg_slot_uv *msg_slot;
  1035. int entry = msg->hdr.msg_slot_number % ch->local_nentries;
  1036. msg_slot = &ch->sn.uv.send_msg_slots[entry];
  1037. BUG_ON(msg_slot->msg_slot_number != msg->hdr.msg_slot_number);
  1038. msg_slot->msg_slot_number += ch->local_nentries;
  1039. if (msg_slot->func != NULL)
  1040. xpc_notify_sender_uv(ch, msg_slot, xpMsgDelivered);
  1041. xpc_free_msg_slot_uv(ch, msg_slot);
  1042. }
  1043. static void
  1044. xpc_handle_notify_mq_msg_uv(struct xpc_partition *part,
  1045. struct xpc_notify_mq_msg_uv *msg)
  1046. {
  1047. struct xpc_partition_uv *part_uv = &part->sn.uv;
  1048. struct xpc_channel *ch;
  1049. struct xpc_channel_uv *ch_uv;
  1050. struct xpc_notify_mq_msg_uv *msg_slot;
  1051. unsigned long irq_flags;
  1052. int ch_number = msg->hdr.ch_number;
  1053. if (unlikely(ch_number >= part->nchannels)) {
  1054. dev_err(xpc_part, "xpc_handle_notify_IRQ_uv() received invalid "
  1055. "channel number=0x%x in message from partid=%d\n",
  1056. ch_number, XPC_PARTID(part));
  1057. /* get hb checker to deactivate from the remote partition */
  1058. spin_lock_irqsave(&xpc_activate_IRQ_rcvd_lock, irq_flags);
  1059. if (part_uv->act_state_req == 0)
  1060. xpc_activate_IRQ_rcvd++;
  1061. part_uv->act_state_req = XPC_P_ASR_DEACTIVATE_UV;
  1062. part_uv->reason = xpBadChannelNumber;
  1063. spin_unlock_irqrestore(&xpc_activate_IRQ_rcvd_lock, irq_flags);
  1064. wake_up_interruptible(&xpc_activate_IRQ_wq);
  1065. return;
  1066. }
  1067. ch = &part->channels[ch_number];
  1068. xpc_msgqueue_ref(ch);
  1069. if (!(ch->flags & XPC_C_CONNECTED)) {
  1070. xpc_msgqueue_deref(ch);
  1071. return;
  1072. }
  1073. /* see if we're really dealing with an ACK for a previously sent msg */
  1074. if (msg->hdr.size == 0) {
  1075. xpc_handle_notify_mq_ack_uv(ch, msg);
  1076. xpc_msgqueue_deref(ch);
  1077. return;
  1078. }
  1079. /* we're dealing with a normal message sent via the notify_mq */
  1080. ch_uv = &ch->sn.uv;
  1081. msg_slot = (struct xpc_notify_mq_msg_uv *)((u64)ch_uv->recv_msg_slots +
  1082. (msg->hdr.msg_slot_number % ch->remote_nentries) *
  1083. ch->entry_size);
  1084. BUG_ON(msg->hdr.msg_slot_number != msg_slot->hdr.msg_slot_number);
  1085. BUG_ON(msg_slot->hdr.size != 0);
  1086. memcpy(msg_slot, msg, msg->hdr.size);
  1087. xpc_put_fifo_entry_uv(&ch_uv->recv_msg_list, &msg_slot->hdr.u.next);
  1088. if (ch->flags & XPC_C_CONNECTEDCALLOUT_MADE) {
  1089. /*
  1090. * If there is an existing idle kthread get it to deliver
  1091. * the payload, otherwise we'll have to get the channel mgr
  1092. * for this partition to create a kthread to do the delivery.
  1093. */
  1094. if (atomic_read(&ch->kthreads_idle) > 0)
  1095. wake_up_nr(&ch->idle_wq, 1);
  1096. else
  1097. xpc_send_chctl_local_msgrequest_uv(part, ch->number);
  1098. }
  1099. xpc_msgqueue_deref(ch);
  1100. }
  1101. static irqreturn_t
  1102. xpc_handle_notify_IRQ_uv(int irq, void *dev_id)
  1103. {
  1104. struct xpc_notify_mq_msg_uv *msg;
  1105. short partid;
  1106. struct xpc_partition *part;
  1107. while ((msg = gru_get_next_message(xpc_notify_mq_uv)) != NULL) {
  1108. partid = msg->hdr.partid;
  1109. if (partid < 0 || partid >= XP_MAX_NPARTITIONS_UV) {
  1110. dev_err(xpc_part, "xpc_handle_notify_IRQ_uv() received "
  1111. "invalid partid=0x%x in message\n", partid);
  1112. } else {
  1113. part = &xpc_partitions[partid];
  1114. if (xpc_part_ref(part)) {
  1115. xpc_handle_notify_mq_msg_uv(part, msg);
  1116. xpc_part_deref(part);
  1117. }
  1118. }
  1119. gru_free_message(xpc_notify_mq_uv, msg);
  1120. }
  1121. return IRQ_HANDLED;
  1122. }
  1123. static int
  1124. xpc_n_of_deliverable_payloads_uv(struct xpc_channel *ch)
  1125. {
  1126. return xpc_n_of_fifo_entries_uv(&ch->sn.uv.recv_msg_list);
  1127. }
  1128. static void
  1129. xpc_process_msg_chctl_flags_uv(struct xpc_partition *part, int ch_number)
  1130. {
  1131. struct xpc_channel *ch = &part->channels[ch_number];
  1132. int ndeliverable_payloads;
  1133. xpc_msgqueue_ref(ch);
  1134. ndeliverable_payloads = xpc_n_of_deliverable_payloads_uv(ch);
  1135. if (ndeliverable_payloads > 0 &&
  1136. (ch->flags & XPC_C_CONNECTED) &&
  1137. (ch->flags & XPC_C_CONNECTEDCALLOUT_MADE)) {
  1138. xpc_activate_kthreads(ch, ndeliverable_payloads);
  1139. }
  1140. xpc_msgqueue_deref(ch);
  1141. }
  1142. static enum xp_retval
  1143. xpc_send_payload_uv(struct xpc_channel *ch, u32 flags, void *payload,
  1144. u16 payload_size, u8 notify_type, xpc_notify_func func,
  1145. void *key)
  1146. {
  1147. enum xp_retval ret = xpSuccess;
  1148. struct xpc_send_msg_slot_uv *msg_slot = NULL;
  1149. struct xpc_notify_mq_msg_uv *msg;
  1150. u8 msg_buffer[XPC_NOTIFY_MSG_SIZE_UV];
  1151. size_t msg_size;
  1152. DBUG_ON(notify_type != XPC_N_CALL);
  1153. msg_size = sizeof(struct xpc_notify_mq_msghdr_uv) + payload_size;
  1154. if (msg_size > ch->entry_size)
  1155. return xpPayloadTooBig;
  1156. xpc_msgqueue_ref(ch);
  1157. if (ch->flags & XPC_C_DISCONNECTING) {
  1158. ret = ch->reason;
  1159. goto out_1;
  1160. }
  1161. if (!(ch->flags & XPC_C_CONNECTED)) {
  1162. ret = xpNotConnected;
  1163. goto out_1;
  1164. }
  1165. ret = xpc_allocate_msg_slot_uv(ch, flags, &msg_slot);
  1166. if (ret != xpSuccess)
  1167. goto out_1;
  1168. if (func != NULL) {
  1169. atomic_inc(&ch->n_to_notify);
  1170. msg_slot->key = key;
  1171. wmb(); /* a non-NULL func must hit memory after the key */
  1172. msg_slot->func = func;
  1173. if (ch->flags & XPC_C_DISCONNECTING) {
  1174. ret = ch->reason;
  1175. goto out_2;
  1176. }
  1177. }
  1178. msg = (struct xpc_notify_mq_msg_uv *)&msg_buffer;
  1179. msg->hdr.partid = xp_partition_id;
  1180. msg->hdr.ch_number = ch->number;
  1181. msg->hdr.size = msg_size;
  1182. msg->hdr.msg_slot_number = msg_slot->msg_slot_number;
  1183. memcpy(&msg->payload, payload, payload_size);
  1184. ret = xpc_send_gru_msg(ch->sn.uv.remote_notify_mq_gpa, msg, msg_size);
  1185. if (ret == xpSuccess)
  1186. goto out_1;
  1187. XPC_DEACTIVATE_PARTITION(&xpc_partitions[ch->partid], ret);
  1188. out_2:
  1189. if (func != NULL) {
  1190. /*
  1191. * Try to NULL the msg_slot's func field. If we fail, then
  1192. * xpc_notify_senders_of_disconnect_uv() beat us to it, in which
  1193. * case we need to pretend we succeeded to send the message
  1194. * since the user will get a callout for the disconnect error
  1195. * by xpc_notify_senders_of_disconnect_uv(), and to also get an
  1196. * error returned here will confuse them. Additionally, since
  1197. * in this case the channel is being disconnected we don't need
  1198. * to put the the msg_slot back on the free list.
  1199. */
  1200. if (cmpxchg(&msg_slot->func, func, NULL) != func) {
  1201. ret = xpSuccess;
  1202. goto out_1;
  1203. }
  1204. msg_slot->key = NULL;
  1205. atomic_dec(&ch->n_to_notify);
  1206. }
  1207. xpc_free_msg_slot_uv(ch, msg_slot);
  1208. out_1:
  1209. xpc_msgqueue_deref(ch);
  1210. return ret;
  1211. }
  1212. /*
  1213. * Tell the callers of xpc_send_notify() that the status of their payloads
  1214. * is unknown because the channel is now disconnecting.
  1215. *
  1216. * We don't worry about putting these msg_slots on the free list since the
  1217. * msg_slots themselves are about to be kfree'd.
  1218. */
  1219. static void
  1220. xpc_notify_senders_of_disconnect_uv(struct xpc_channel *ch)
  1221. {
  1222. struct xpc_send_msg_slot_uv *msg_slot;
  1223. int entry;
  1224. DBUG_ON(!(ch->flags & XPC_C_DISCONNECTING));
  1225. for (entry = 0; entry < ch->local_nentries; entry++) {
  1226. if (atomic_read(&ch->n_to_notify) == 0)
  1227. break;
  1228. msg_slot = &ch->sn.uv.send_msg_slots[entry];
  1229. if (msg_slot->func != NULL)
  1230. xpc_notify_sender_uv(ch, msg_slot, ch->reason);
  1231. }
  1232. }
  1233. /*
  1234. * Get the next deliverable message's payload.
  1235. */
  1236. static void *
  1237. xpc_get_deliverable_payload_uv(struct xpc_channel *ch)
  1238. {
  1239. struct xpc_fifo_entry_uv *entry;
  1240. struct xpc_notify_mq_msg_uv *msg;
  1241. void *payload = NULL;
  1242. if (!(ch->flags & XPC_C_DISCONNECTING)) {
  1243. entry = xpc_get_fifo_entry_uv(&ch->sn.uv.recv_msg_list);
  1244. if (entry != NULL) {
  1245. msg = container_of(entry, struct xpc_notify_mq_msg_uv,
  1246. hdr.u.next);
  1247. payload = &msg->payload;
  1248. }
  1249. }
  1250. return payload;
  1251. }
  1252. static void
  1253. xpc_received_payload_uv(struct xpc_channel *ch, void *payload)
  1254. {
  1255. struct xpc_notify_mq_msg_uv *msg;
  1256. enum xp_retval ret;
  1257. msg = container_of(payload, struct xpc_notify_mq_msg_uv, payload);
  1258. /* return an ACK to the sender of this message */
  1259. msg->hdr.partid = xp_partition_id;
  1260. msg->hdr.size = 0; /* size of zero indicates this is an ACK */
  1261. ret = xpc_send_gru_msg(ch->sn.uv.remote_notify_mq_gpa, msg,
  1262. sizeof(struct xpc_notify_mq_msghdr_uv));
  1263. if (ret != xpSuccess)
  1264. XPC_DEACTIVATE_PARTITION(&xpc_partitions[ch->partid], ret);
  1265. msg->hdr.msg_slot_number += ch->remote_nentries;
  1266. }
  1267. int
  1268. xpc_init_uv(void)
  1269. {
  1270. xpc_setup_partitions_sn = xpc_setup_partitions_sn_uv;
  1271. xpc_process_activate_IRQ_rcvd = xpc_process_activate_IRQ_rcvd_uv;
  1272. xpc_get_partition_rsvd_page_pa = xpc_get_partition_rsvd_page_pa_uv;
  1273. xpc_setup_rsvd_page_sn = xpc_setup_rsvd_page_sn_uv;
  1274. xpc_increment_heartbeat = xpc_increment_heartbeat_uv;
  1275. xpc_offline_heartbeat = xpc_offline_heartbeat_uv;
  1276. xpc_online_heartbeat = xpc_online_heartbeat_uv;
  1277. xpc_heartbeat_init = xpc_heartbeat_init_uv;
  1278. xpc_heartbeat_exit = xpc_heartbeat_exit_uv;
  1279. xpc_get_remote_heartbeat = xpc_get_remote_heartbeat_uv;
  1280. xpc_request_partition_activation = xpc_request_partition_activation_uv;
  1281. xpc_request_partition_reactivation =
  1282. xpc_request_partition_reactivation_uv;
  1283. xpc_request_partition_deactivation =
  1284. xpc_request_partition_deactivation_uv;
  1285. xpc_cancel_partition_deactivation_request =
  1286. xpc_cancel_partition_deactivation_request_uv;
  1287. xpc_setup_ch_structures_sn = xpc_setup_ch_structures_sn_uv;
  1288. xpc_teardown_ch_structures_sn = xpc_teardown_ch_structures_sn_uv;
  1289. xpc_make_first_contact = xpc_make_first_contact_uv;
  1290. xpc_get_chctl_all_flags = xpc_get_chctl_all_flags_uv;
  1291. xpc_send_chctl_closerequest = xpc_send_chctl_closerequest_uv;
  1292. xpc_send_chctl_closereply = xpc_send_chctl_closereply_uv;
  1293. xpc_send_chctl_openrequest = xpc_send_chctl_openrequest_uv;
  1294. xpc_send_chctl_openreply = xpc_send_chctl_openreply_uv;
  1295. xpc_save_remote_msgqueue_pa = xpc_save_remote_msgqueue_pa_uv;
  1296. xpc_setup_msg_structures = xpc_setup_msg_structures_uv;
  1297. xpc_teardown_msg_structures = xpc_teardown_msg_structures_uv;
  1298. xpc_indicate_partition_engaged = xpc_indicate_partition_engaged_uv;
  1299. xpc_indicate_partition_disengaged =
  1300. xpc_indicate_partition_disengaged_uv;
  1301. xpc_assume_partition_disengaged = xpc_assume_partition_disengaged_uv;
  1302. xpc_partition_engaged = xpc_partition_engaged_uv;
  1303. xpc_any_partition_engaged = xpc_any_partition_engaged_uv;
  1304. xpc_n_of_deliverable_payloads = xpc_n_of_deliverable_payloads_uv;
  1305. xpc_process_msg_chctl_flags = xpc_process_msg_chctl_flags_uv;
  1306. xpc_send_payload = xpc_send_payload_uv;
  1307. xpc_notify_senders_of_disconnect = xpc_notify_senders_of_disconnect_uv;
  1308. xpc_get_deliverable_payload = xpc_get_deliverable_payload_uv;
  1309. xpc_received_payload = xpc_received_payload_uv;
  1310. if (sizeof(struct xpc_notify_mq_msghdr_uv) > XPC_MSG_HDR_MAX_SIZE) {
  1311. dev_err(xpc_part, "xpc_notify_mq_msghdr_uv is larger than %d\n",
  1312. XPC_MSG_HDR_MAX_SIZE);
  1313. return -E2BIG;
  1314. }
  1315. xpc_activate_mq_uv = xpc_create_gru_mq_uv(XPC_ACTIVATE_MQ_SIZE_UV, 0,
  1316. XPC_ACTIVATE_IRQ_NAME,
  1317. xpc_handle_activate_IRQ_uv);
  1318. if (IS_ERR(xpc_activate_mq_uv))
  1319. return PTR_ERR(xpc_activate_mq_uv);
  1320. xpc_notify_mq_uv = xpc_create_gru_mq_uv(XPC_NOTIFY_MQ_SIZE_UV, 0,
  1321. XPC_NOTIFY_IRQ_NAME,
  1322. xpc_handle_notify_IRQ_uv);
  1323. if (IS_ERR(xpc_notify_mq_uv)) {
  1324. xpc_destroy_gru_mq_uv(xpc_activate_mq_uv);
  1325. return PTR_ERR(xpc_notify_mq_uv);
  1326. }
  1327. return 0;
  1328. }
  1329. void
  1330. xpc_exit_uv(void)
  1331. {
  1332. xpc_destroy_gru_mq_uv(xpc_notify_mq_uv);
  1333. xpc_destroy_gru_mq_uv(xpc_activate_mq_uv);
  1334. }