PageRenderTime 1695ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/misc/sgi-xp/xpc_uv.c

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