blob: 241ea5f01aaa731457f0d69101ec9dc6ef5cbe7a [file] [log] [blame]
Dean Nelson94bd2702008-07-29 22:34:05 -07001/*
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 *
Dean Nelson361916a2009-02-04 15:12:24 -08006 * Copyright (c) 2008-2009 Silicon Graphics, Inc. All Rights Reserved.
Dean Nelson94bd2702008-07-29 22:34:05 -07007 */
8
9/*
10 * Cross Partition Communication (XPC) uv-based functions.
11 *
12 * Architecture specific implementation of common functions.
13 *
14 */
15
16#include <linux/kernel.h>
Dean Nelson5b8669d2008-07-29 22:34:18 -070017#include <linux/mm.h>
18#include <linux/interrupt.h>
19#include <linux/delay.h>
20#include <linux/device.h>
Dean Nelson25257892008-11-05 17:28:00 -060021#include <linux/err.h>
Dean Nelson261f3b42008-07-29 22:34:16 -070022#include <asm/uv/uv_hub.h>
Dean Nelson25257892008-11-05 17:28:00 -060023#if defined CONFIG_X86_64
24#include <asm/uv/bios.h>
25#include <asm/uv/uv_irq.h>
26#elif defined CONFIG_IA64_GENERIC || defined CONFIG_IA64_SGI_UV
27#include <asm/sn/intr.h>
28#include <asm/sn/sn_sal.h>
29#endif
Dean Nelson5b8669d2008-07-29 22:34:18 -070030#include "../sgi-gru/gru.h"
Dean Nelson261f3b42008-07-29 22:34:16 -070031#include "../sgi-gru/grukservices.h"
Dean Nelson94bd2702008-07-29 22:34:05 -070032#include "xpc.h"
33
Jack Steiner6f2584f2009-04-02 16:59:10 -070034#if defined CONFIG_IA64_GENERIC || defined CONFIG_IA64_SGI_UV
35struct uv_IO_APIC_route_entry {
36 __u64 vector : 8,
37 delivery_mode : 3,
38 dest_mode : 1,
39 delivery_status : 1,
40 polarity : 1,
41 __reserved_1 : 1,
42 trigger : 1,
43 mask : 1,
44 __reserved_2 : 15,
45 dest : 32;
46};
47#endif
48
Robin Holta374c572009-04-13 14:40:18 -070049static struct xpc_heartbeat_uv *xpc_heartbeat_uv;
Dean Nelson33ba3c72008-07-29 22:34:07 -070050
Dean Nelson5b8669d2008-07-29 22:34:18 -070051#define XPC_ACTIVATE_MSG_SIZE_UV (1 * GRU_CACHE_LINE_BYTES)
Dean Nelson25257892008-11-05 17:28:00 -060052#define XPC_ACTIVATE_MQ_SIZE_UV (4 * XP_MAX_NPARTITIONS_UV * \
53 XPC_ACTIVATE_MSG_SIZE_UV)
54#define XPC_ACTIVATE_IRQ_NAME "xpc_activate"
55
Dean Nelson5b8669d2008-07-29 22:34:18 -070056#define XPC_NOTIFY_MSG_SIZE_UV (2 * GRU_CACHE_LINE_BYTES)
Dean Nelson25257892008-11-05 17:28:00 -060057#define XPC_NOTIFY_MQ_SIZE_UV (4 * XP_MAX_NPARTITIONS_UV * \
58 XPC_NOTIFY_MSG_SIZE_UV)
59#define XPC_NOTIFY_IRQ_NAME "xpc_notify"
Dean Nelson5b8669d2008-07-29 22:34:18 -070060
Dean Nelson25257892008-11-05 17:28:00 -060061static struct xpc_gru_mq_uv *xpc_activate_mq_uv;
62static struct xpc_gru_mq_uv *xpc_notify_mq_uv;
Dean Nelson5b8669d2008-07-29 22:34:18 -070063
64static int
Robin Holta7665b02009-04-13 14:40:19 -070065xpc_setup_partitions_uv(void)
Dean Nelson5b8669d2008-07-29 22:34:18 -070066{
67 short partid;
68 struct xpc_partition_uv *part_uv;
69
70 for (partid = 0; partid < XP_MAX_NPARTITIONS_UV; partid++) {
71 part_uv = &xpc_partitions[partid].sn.uv;
72
Jack Steiner6f2584f2009-04-02 16:59:10 -070073 mutex_init(&part_uv->cached_activate_gru_mq_desc_mutex);
Dean Nelson5b8669d2008-07-29 22:34:18 -070074 spin_lock_init(&part_uv->flags_lock);
75 part_uv->remote_act_state = XPC_P_AS_INACTIVE;
76 }
77 return 0;
78}
79
Jack Steiner6f2584f2009-04-02 16:59:10 -070080static void
Robin Holta7665b02009-04-13 14:40:19 -070081xpc_teardown_partitions_uv(void)
Jack Steiner6f2584f2009-04-02 16:59:10 -070082{
83 short partid;
84 struct xpc_partition_uv *part_uv;
85 unsigned long irq_flags;
86
87 for (partid = 0; partid < XP_MAX_NPARTITIONS_UV; partid++) {
88 part_uv = &xpc_partitions[partid].sn.uv;
89
90 if (part_uv->cached_activate_gru_mq_desc != NULL) {
91 mutex_lock(&part_uv->cached_activate_gru_mq_desc_mutex);
92 spin_lock_irqsave(&part_uv->flags_lock, irq_flags);
93 part_uv->flags &= ~XPC_P_CACHED_ACTIVATE_GRU_MQ_DESC_UV;
94 spin_unlock_irqrestore(&part_uv->flags_lock, irq_flags);
95 kfree(part_uv->cached_activate_gru_mq_desc);
96 part_uv->cached_activate_gru_mq_desc = NULL;
97 mutex_unlock(&part_uv->
98 cached_activate_gru_mq_desc_mutex);
99 }
100 }
101}
102
Dean Nelson25257892008-11-05 17:28:00 -0600103static int
104xpc_get_gru_mq_irq_uv(struct xpc_gru_mq_uv *mq, int cpu, char *irq_name)
Dean Nelson5b8669d2008-07-29 22:34:18 -0700105{
Jack Steiner6f2584f2009-04-02 16:59:10 -0700106 int mmr_pnode = uv_blade_to_pnode(mq->mmr_blade);
107
Dean Nelson25257892008-11-05 17:28:00 -0600108#if defined CONFIG_X86_64
Dimitri Sivanich6c2c5022009-09-30 11:02:59 -0500109 mq->irq = uv_setup_irq(irq_name, cpu, mq->mmr_blade, mq->mmr_offset,
110 UV_AFFINITY_CPU);
Dean Nelson25257892008-11-05 17:28:00 -0600111 if (mq->irq < 0) {
112 dev_err(xpc_part, "uv_setup_irq() returned error=%d\n",
Jack Steiner6f2584f2009-04-02 16:59:10 -0700113 -mq->irq);
114 return mq->irq;
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700115 }
Dean Nelson5b8669d2008-07-29 22:34:18 -0700116
Jack Steiner6f2584f2009-04-02 16:59:10 -0700117 mq->mmr_value = uv_read_global_mmr64(mmr_pnode, mq->mmr_offset);
Dean Nelson5b8669d2008-07-29 22:34:18 -0700118
Jack Steiner6f2584f2009-04-02 16:59:10 -0700119#elif defined CONFIG_IA64_GENERIC || defined CONFIG_IA64_SGI_UV
Dean Nelson25257892008-11-05 17:28:00 -0600120 if (strcmp(irq_name, XPC_ACTIVATE_IRQ_NAME) == 0)
121 mq->irq = SGI_XPC_ACTIVATE;
122 else if (strcmp(irq_name, XPC_NOTIFY_IRQ_NAME) == 0)
123 mq->irq = SGI_XPC_NOTIFY;
124 else
125 return -EINVAL;
Dean Nelson5b8669d2008-07-29 22:34:18 -0700126
Jack Steiner6f2584f2009-04-02 16:59:10 -0700127 mq->mmr_value = (unsigned long)cpu_physical_id(cpu) << 32 | mq->irq;
128 uv_write_global_mmr64(mmr_pnode, mq->mmr_offset, mq->mmr_value);
Dean Nelson25257892008-11-05 17:28:00 -0600129#else
130 #error not a supported configuration
131#endif
Dean Nelson5b8669d2008-07-29 22:34:18 -0700132
Dean Nelson25257892008-11-05 17:28:00 -0600133 return 0;
Dean Nelson5b8669d2008-07-29 22:34:18 -0700134}
Dean Nelson94bd2702008-07-29 22:34:05 -0700135
Dean Nelson33ba3c72008-07-29 22:34:07 -0700136static void
Dean Nelson25257892008-11-05 17:28:00 -0600137xpc_release_gru_mq_irq_uv(struct xpc_gru_mq_uv *mq)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700138{
Dean Nelson25257892008-11-05 17:28:00 -0600139#if defined CONFIG_X86_64
Dimitri Sivanich6c2c5022009-09-30 11:02:59 -0500140 uv_teardown_irq(mq->irq);
Dean Nelson5b8669d2008-07-29 22:34:18 -0700141
Dean Nelson25257892008-11-05 17:28:00 -0600142#elif defined CONFIG_IA64_GENERIC || defined CONFIG_IA64_SGI_UV
143 int mmr_pnode;
144 unsigned long mmr_value;
Dean Nelson5b8669d2008-07-29 22:34:18 -0700145
Dean Nelson25257892008-11-05 17:28:00 -0600146 mmr_pnode = uv_blade_to_pnode(mq->mmr_blade);
147 mmr_value = 1UL << 16;
Dean Nelson5b8669d2008-07-29 22:34:18 -0700148
Dean Nelson25257892008-11-05 17:28:00 -0600149 uv_write_global_mmr64(mmr_pnode, mq->mmr_offset, mmr_value);
150#else
151 #error not a supported configuration
152#endif
153}
154
155static int
156xpc_gru_mq_watchlist_alloc_uv(struct xpc_gru_mq_uv *mq)
157{
158 int ret;
159
Robin Holtc2c9f112009-12-15 16:47:56 -0800160#if defined CONFIG_IA64_GENERIC || defined CONFIG_IA64_SGI_UV
161 int mmr_pnode = uv_blade_to_pnode(mq->mmr_blade);
162
163 ret = sn_mq_watchlist_alloc(mmr_pnode, (void *)uv_gpa(mq->address),
Russ Andersonc8182f02008-12-12 11:07:00 -0600164 mq->order, &mq->mmr_offset);
Dean Nelson25257892008-11-05 17:28:00 -0600165 if (ret < 0) {
166 dev_err(xpc_part, "sn_mq_watchlist_alloc() failed, ret=%d\n",
167 ret);
168 return -EBUSY;
169 }
Robin Holtc2c9f112009-12-15 16:47:56 -0800170#elif defined CONFIG_X86_64
171 ret = uv_bios_mq_watchlist_alloc(uv_gpa(mq->address),
172 mq->order, &mq->mmr_offset);
173 if (ret < 0) {
174 dev_err(xpc_part, "uv_bios_mq_watchlist_alloc() failed, "
175 "ret=%d\n", ret);
176 return ret;
177 }
Dean Nelson25257892008-11-05 17:28:00 -0600178#else
179 #error not a supported configuration
180#endif
181
182 mq->watchlist_num = ret;
183 return 0;
184}
185
186static void
187xpc_gru_mq_watchlist_free_uv(struct xpc_gru_mq_uv *mq)
188{
189 int ret;
Robin Holtc2c9f112009-12-15 16:47:56 -0800190 int mmr_pnode = uv_blade_to_pnode(mq->mmr_blade);
Dean Nelson25257892008-11-05 17:28:00 -0600191
192#if defined CONFIG_X86_64
Robin Holtc2c9f112009-12-15 16:47:56 -0800193 ret = uv_bios_mq_watchlist_free(mmr_pnode, mq->watchlist_num);
Dean Nelson25257892008-11-05 17:28:00 -0600194 BUG_ON(ret != BIOS_STATUS_SUCCESS);
195#elif defined CONFIG_IA64_GENERIC || defined CONFIG_IA64_SGI_UV
Robin Holtc2c9f112009-12-15 16:47:56 -0800196 ret = sn_mq_watchlist_free(mmr_pnode, mq->watchlist_num);
Dean Nelson25257892008-11-05 17:28:00 -0600197 BUG_ON(ret != SALRET_OK);
198#else
199 #error not a supported configuration
200#endif
201}
202
203static struct xpc_gru_mq_uv *
204xpc_create_gru_mq_uv(unsigned int mq_size, int cpu, char *irq_name,
205 irq_handler_t irq_handler)
206{
207 enum xp_retval xp_ret;
208 int ret;
209 int nid;
210 int pg_order;
211 struct page *page;
212 struct xpc_gru_mq_uv *mq;
Jack Steiner6f2584f2009-04-02 16:59:10 -0700213 struct uv_IO_APIC_route_entry *mmr_value;
Dean Nelson25257892008-11-05 17:28:00 -0600214
215 mq = kmalloc(sizeof(struct xpc_gru_mq_uv), GFP_KERNEL);
216 if (mq == NULL) {
217 dev_err(xpc_part, "xpc_create_gru_mq_uv() failed to kmalloc() "
218 "a xpc_gru_mq_uv structure\n");
219 ret = -ENOMEM;
Jack Steiner6f2584f2009-04-02 16:59:10 -0700220 goto out_0;
221 }
222
223 mq->gru_mq_desc = kzalloc(sizeof(struct gru_message_queue_desc),
224 GFP_KERNEL);
225 if (mq->gru_mq_desc == NULL) {
226 dev_err(xpc_part, "xpc_create_gru_mq_uv() failed to kmalloc() "
227 "a gru_message_queue_desc structure\n");
228 ret = -ENOMEM;
Dean Nelson25257892008-11-05 17:28:00 -0600229 goto out_1;
230 }
231
232 pg_order = get_order(mq_size);
233 mq->order = pg_order + PAGE_SHIFT;
234 mq_size = 1UL << mq->order;
235
236 mq->mmr_blade = uv_cpu_to_blade_id(cpu);
237
238 nid = cpu_to_node(cpu);
Mel Gorman6484eb32009-06-16 15:31:54 -0700239 page = alloc_pages_exact_node(nid, GFP_KERNEL | __GFP_ZERO | GFP_THISNODE,
Dean Nelson25257892008-11-05 17:28:00 -0600240 pg_order);
241 if (page == NULL) {
242 dev_err(xpc_part, "xpc_create_gru_mq_uv() failed to alloc %d "
243 "bytes of memory on nid=%d for GRU mq\n", mq_size, nid);
244 ret = -ENOMEM;
245 goto out_2;
246 }
247 mq->address = page_address(page);
248
Dean Nelson25257892008-11-05 17:28:00 -0600249 /* enable generation of irq when GRU mq operation occurs to this mq */
250 ret = xpc_gru_mq_watchlist_alloc_uv(mq);
251 if (ret != 0)
252 goto out_3;
253
254 ret = xpc_get_gru_mq_irq_uv(mq, cpu, irq_name);
255 if (ret != 0)
256 goto out_4;
257
258 ret = request_irq(mq->irq, irq_handler, 0, irq_name, NULL);
259 if (ret != 0) {
260 dev_err(xpc_part, "request_irq(irq=%d) returned error=%d\n",
Jack Steiner6f2584f2009-04-02 16:59:10 -0700261 mq->irq, -ret);
Dean Nelson25257892008-11-05 17:28:00 -0600262 goto out_5;
263 }
264
Jack Steiner6f2584f2009-04-02 16:59:10 -0700265 mmr_value = (struct uv_IO_APIC_route_entry *)&mq->mmr_value;
266 ret = gru_create_message_queue(mq->gru_mq_desc, mq->address, mq_size,
267 nid, mmr_value->vector, mmr_value->dest);
268 if (ret != 0) {
269 dev_err(xpc_part, "gru_create_message_queue() returned "
270 "error=%d\n", ret);
271 ret = -EINVAL;
272 goto out_6;
273 }
274
Dean Nelson25257892008-11-05 17:28:00 -0600275 /* allow other partitions to access this GRU mq */
276 xp_ret = xp_expand_memprotect(xp_pa(mq->address), mq_size);
277 if (xp_ret != xpSuccess) {
278 ret = -EACCES;
279 goto out_6;
280 }
281
282 return mq;
283
284 /* something went wrong */
285out_6:
286 free_irq(mq->irq, NULL);
287out_5:
288 xpc_release_gru_mq_irq_uv(mq);
289out_4:
290 xpc_gru_mq_watchlist_free_uv(mq);
291out_3:
292 free_pages((unsigned long)mq->address, pg_order);
293out_2:
Jack Steiner6f2584f2009-04-02 16:59:10 -0700294 kfree(mq->gru_mq_desc);
Dean Nelson25257892008-11-05 17:28:00 -0600295out_1:
Jack Steiner6f2584f2009-04-02 16:59:10 -0700296 kfree(mq);
297out_0:
Dean Nelson25257892008-11-05 17:28:00 -0600298 return ERR_PTR(ret);
299}
300
301static void
302xpc_destroy_gru_mq_uv(struct xpc_gru_mq_uv *mq)
303{
304 unsigned int mq_size;
305 int pg_order;
306 int ret;
307
308 /* disallow other partitions to access GRU mq */
309 mq_size = 1UL << mq->order;
310 ret = xp_restrict_memprotect(xp_pa(mq->address), mq_size);
311 BUG_ON(ret != xpSuccess);
312
313 /* unregister irq handler and release mq irq/vector mapping */
314 free_irq(mq->irq, NULL);
315 xpc_release_gru_mq_irq_uv(mq);
316
317 /* disable generation of irq when GRU mq op occurs to this mq */
318 xpc_gru_mq_watchlist_free_uv(mq);
319
320 pg_order = mq->order - PAGE_SHIFT;
321 free_pages((unsigned long)mq->address, pg_order);
322
323 kfree(mq);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700324}
325
Dean Nelson94bd2702008-07-29 22:34:05 -0700326static enum xp_retval
Jack Steiner6f2584f2009-04-02 16:59:10 -0700327xpc_send_gru_msg(struct gru_message_queue_desc *gru_mq_desc, void *msg,
328 size_t msg_size)
Dean Nelson94bd2702008-07-29 22:34:05 -0700329{
Dean Nelson5b8669d2008-07-29 22:34:18 -0700330 enum xp_retval xp_ret;
331 int ret;
332
333 while (1) {
Jack Steiner6f2584f2009-04-02 16:59:10 -0700334 ret = gru_send_message_gpa(gru_mq_desc, msg, msg_size);
Dean Nelson5b8669d2008-07-29 22:34:18 -0700335 if (ret == MQE_OK) {
336 xp_ret = xpSuccess;
337 break;
338 }
339
340 if (ret == MQE_QUEUE_FULL) {
341 dev_dbg(xpc_chan, "gru_send_message_gpa() returned "
342 "error=MQE_QUEUE_FULL\n");
343 /* !!! handle QLimit reached; delay & try again */
344 /* ??? Do we add a limit to the number of retries? */
345 (void)msleep_interruptible(10);
346 } else if (ret == MQE_CONGESTION) {
347 dev_dbg(xpc_chan, "gru_send_message_gpa() returned "
348 "error=MQE_CONGESTION\n");
349 /* !!! handle LB Overflow; simply try again */
350 /* ??? Do we add a limit to the number of retries? */
351 } else {
352 /* !!! Currently this is MQE_UNEXPECTED_CB_ERR */
353 dev_err(xpc_chan, "gru_send_message_gpa() returned "
354 "error=%d\n", ret);
355 xp_ret = xpGruSendMqError;
356 break;
357 }
358 }
359 return xp_ret;
360}
361
362static void
363xpc_process_activate_IRQ_rcvd_uv(void)
364{
365 unsigned long irq_flags;
366 short partid;
367 struct xpc_partition *part;
368 u8 act_state_req;
369
370 DBUG_ON(xpc_activate_IRQ_rcvd == 0);
371
372 spin_lock_irqsave(&xpc_activate_IRQ_rcvd_lock, irq_flags);
373 for (partid = 0; partid < XP_MAX_NPARTITIONS_UV; partid++) {
374 part = &xpc_partitions[partid];
375
376 if (part->sn.uv.act_state_req == 0)
377 continue;
378
379 xpc_activate_IRQ_rcvd--;
380 BUG_ON(xpc_activate_IRQ_rcvd < 0);
381
382 act_state_req = part->sn.uv.act_state_req;
383 part->sn.uv.act_state_req = 0;
384 spin_unlock_irqrestore(&xpc_activate_IRQ_rcvd_lock, irq_flags);
385
386 if (act_state_req == XPC_P_ASR_ACTIVATE_UV) {
387 if (part->act_state == XPC_P_AS_INACTIVE)
388 xpc_activate_partition(part);
389 else if (part->act_state == XPC_P_AS_DEACTIVATING)
390 XPC_DEACTIVATE_PARTITION(part, xpReactivating);
391
392 } else if (act_state_req == XPC_P_ASR_REACTIVATE_UV) {
393 if (part->act_state == XPC_P_AS_INACTIVE)
394 xpc_activate_partition(part);
395 else
396 XPC_DEACTIVATE_PARTITION(part, xpReactivating);
397
398 } else if (act_state_req == XPC_P_ASR_DEACTIVATE_UV) {
399 XPC_DEACTIVATE_PARTITION(part, part->sn.uv.reason);
400
401 } else {
402 BUG();
403 }
404
405 spin_lock_irqsave(&xpc_activate_IRQ_rcvd_lock, irq_flags);
406 if (xpc_activate_IRQ_rcvd == 0)
407 break;
408 }
409 spin_unlock_irqrestore(&xpc_activate_IRQ_rcvd_lock, irq_flags);
410
411}
412
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700413static void
414xpc_handle_activate_mq_msg_uv(struct xpc_partition *part,
415 struct xpc_activate_mq_msghdr_uv *msg_hdr,
416 int *wakeup_hb_checker)
417{
418 unsigned long irq_flags;
419 struct xpc_partition_uv *part_uv = &part->sn.uv;
420 struct xpc_openclose_args *args;
421
422 part_uv->remote_act_state = msg_hdr->act_state;
423
424 switch (msg_hdr->type) {
425 case XPC_ACTIVATE_MQ_MSG_SYNC_ACT_STATE_UV:
426 /* syncing of remote_act_state was just done above */
427 break;
428
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700429 case XPC_ACTIVATE_MQ_MSG_ACTIVATE_REQ_UV: {
430 struct xpc_activate_mq_msg_activate_req_uv *msg;
431
432 /*
433 * ??? Do we deal here with ts_jiffies being different
434 * ??? if act_state != XPC_P_AS_INACTIVE instead of
435 * ??? below?
436 */
437 msg = container_of(msg_hdr, struct
438 xpc_activate_mq_msg_activate_req_uv, hdr);
439
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_ACTIVATE_UV;
444 part->remote_rp_pa = msg->rp_gpa; /* !!! _pa is _gpa */
445 part->remote_rp_ts_jiffies = msg_hdr->rp_ts_jiffies;
Robin Holta374c572009-04-13 14:40:18 -0700446 part_uv->heartbeat_gpa = msg->heartbeat_gpa;
Jack Steiner6f2584f2009-04-02 16:59:10 -0700447
448 if (msg->activate_gru_mq_desc_gpa !=
449 part_uv->activate_gru_mq_desc_gpa) {
450 spin_lock_irqsave(&part_uv->flags_lock, irq_flags);
451 part_uv->flags &= ~XPC_P_CACHED_ACTIVATE_GRU_MQ_DESC_UV;
452 spin_unlock_irqrestore(&part_uv->flags_lock, irq_flags);
453 part_uv->activate_gru_mq_desc_gpa =
454 msg->activate_gru_mq_desc_gpa;
455 }
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700456 spin_unlock_irqrestore(&xpc_activate_IRQ_rcvd_lock, irq_flags);
457
458 (*wakeup_hb_checker)++;
459 break;
460 }
461 case XPC_ACTIVATE_MQ_MSG_DEACTIVATE_REQ_UV: {
462 struct xpc_activate_mq_msg_deactivate_req_uv *msg;
463
464 msg = container_of(msg_hdr, struct
465 xpc_activate_mq_msg_deactivate_req_uv, hdr);
466
467 spin_lock_irqsave(&xpc_activate_IRQ_rcvd_lock, irq_flags);
468 if (part_uv->act_state_req == 0)
469 xpc_activate_IRQ_rcvd++;
470 part_uv->act_state_req = XPC_P_ASR_DEACTIVATE_UV;
471 part_uv->reason = msg->reason;
472 spin_unlock_irqrestore(&xpc_activate_IRQ_rcvd_lock, irq_flags);
473
474 (*wakeup_hb_checker)++;
475 return;
476 }
477 case XPC_ACTIVATE_MQ_MSG_CHCTL_CLOSEREQUEST_UV: {
478 struct xpc_activate_mq_msg_chctl_closerequest_uv *msg;
479
480 msg = container_of(msg_hdr, struct
481 xpc_activate_mq_msg_chctl_closerequest_uv,
482 hdr);
483 args = &part->remote_openclose_args[msg->ch_number];
484 args->reason = msg->reason;
485
486 spin_lock_irqsave(&part->chctl_lock, irq_flags);
487 part->chctl.flags[msg->ch_number] |= XPC_CHCTL_CLOSEREQUEST;
488 spin_unlock_irqrestore(&part->chctl_lock, irq_flags);
489
490 xpc_wakeup_channel_mgr(part);
491 break;
492 }
493 case XPC_ACTIVATE_MQ_MSG_CHCTL_CLOSEREPLY_UV: {
494 struct xpc_activate_mq_msg_chctl_closereply_uv *msg;
495
496 msg = container_of(msg_hdr, struct
497 xpc_activate_mq_msg_chctl_closereply_uv,
498 hdr);
499
500 spin_lock_irqsave(&part->chctl_lock, irq_flags);
501 part->chctl.flags[msg->ch_number] |= XPC_CHCTL_CLOSEREPLY;
502 spin_unlock_irqrestore(&part->chctl_lock, irq_flags);
503
504 xpc_wakeup_channel_mgr(part);
505 break;
506 }
507 case XPC_ACTIVATE_MQ_MSG_CHCTL_OPENREQUEST_UV: {
508 struct xpc_activate_mq_msg_chctl_openrequest_uv *msg;
509
510 msg = container_of(msg_hdr, struct
511 xpc_activate_mq_msg_chctl_openrequest_uv,
512 hdr);
513 args = &part->remote_openclose_args[msg->ch_number];
514 args->entry_size = msg->entry_size;
515 args->local_nentries = msg->local_nentries;
516
517 spin_lock_irqsave(&part->chctl_lock, irq_flags);
518 part->chctl.flags[msg->ch_number] |= XPC_CHCTL_OPENREQUEST;
519 spin_unlock_irqrestore(&part->chctl_lock, irq_flags);
520
521 xpc_wakeup_channel_mgr(part);
522 break;
523 }
524 case XPC_ACTIVATE_MQ_MSG_CHCTL_OPENREPLY_UV: {
525 struct xpc_activate_mq_msg_chctl_openreply_uv *msg;
526
527 msg = container_of(msg_hdr, struct
528 xpc_activate_mq_msg_chctl_openreply_uv, hdr);
529 args = &part->remote_openclose_args[msg->ch_number];
530 args->remote_nentries = msg->remote_nentries;
531 args->local_nentries = msg->local_nentries;
Jack Steiner6f2584f2009-04-02 16:59:10 -0700532 args->local_msgqueue_pa = msg->notify_gru_mq_desc_gpa;
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700533
534 spin_lock_irqsave(&part->chctl_lock, irq_flags);
535 part->chctl.flags[msg->ch_number] |= XPC_CHCTL_OPENREPLY;
536 spin_unlock_irqrestore(&part->chctl_lock, irq_flags);
537
538 xpc_wakeup_channel_mgr(part);
539 break;
540 }
Robin Holtefdd06e2009-04-13 14:40:19 -0700541 case XPC_ACTIVATE_MQ_MSG_CHCTL_OPENCOMPLETE_UV: {
542 struct xpc_activate_mq_msg_chctl_opencomplete_uv *msg;
543
544 msg = container_of(msg_hdr, struct
545 xpc_activate_mq_msg_chctl_opencomplete_uv, hdr);
546 spin_lock_irqsave(&part->chctl_lock, irq_flags);
547 part->chctl.flags[msg->ch_number] |= XPC_CHCTL_OPENCOMPLETE;
548 spin_unlock_irqrestore(&part->chctl_lock, irq_flags);
549
550 xpc_wakeup_channel_mgr(part);
551 }
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700552 case XPC_ACTIVATE_MQ_MSG_MARK_ENGAGED_UV:
553 spin_lock_irqsave(&part_uv->flags_lock, irq_flags);
554 part_uv->flags |= XPC_P_ENGAGED_UV;
555 spin_unlock_irqrestore(&part_uv->flags_lock, irq_flags);
556 break;
557
558 case XPC_ACTIVATE_MQ_MSG_MARK_DISENGAGED_UV:
559 spin_lock_irqsave(&part_uv->flags_lock, irq_flags);
560 part_uv->flags &= ~XPC_P_ENGAGED_UV;
561 spin_unlock_irqrestore(&part_uv->flags_lock, irq_flags);
562 break;
563
564 default:
565 dev_err(xpc_part, "received unknown activate_mq msg type=%d "
566 "from partition=%d\n", msg_hdr->type, XPC_PARTID(part));
567
568 /* get hb checker to deactivate from the remote partition */
569 spin_lock_irqsave(&xpc_activate_IRQ_rcvd_lock, irq_flags);
570 if (part_uv->act_state_req == 0)
571 xpc_activate_IRQ_rcvd++;
572 part_uv->act_state_req = XPC_P_ASR_DEACTIVATE_UV;
573 part_uv->reason = xpBadMsgType;
574 spin_unlock_irqrestore(&xpc_activate_IRQ_rcvd_lock, irq_flags);
575
576 (*wakeup_hb_checker)++;
577 return;
578 }
579
580 if (msg_hdr->rp_ts_jiffies != part->remote_rp_ts_jiffies &&
581 part->remote_rp_ts_jiffies != 0) {
582 /*
583 * ??? Does what we do here need to be sensitive to
584 * ??? act_state or remote_act_state?
585 */
586 spin_lock_irqsave(&xpc_activate_IRQ_rcvd_lock, irq_flags);
587 if (part_uv->act_state_req == 0)
588 xpc_activate_IRQ_rcvd++;
589 part_uv->act_state_req = XPC_P_ASR_REACTIVATE_UV;
590 spin_unlock_irqrestore(&xpc_activate_IRQ_rcvd_lock, irq_flags);
591
592 (*wakeup_hb_checker)++;
593 }
594}
595
Dean Nelson5b8669d2008-07-29 22:34:18 -0700596static irqreturn_t
597xpc_handle_activate_IRQ_uv(int irq, void *dev_id)
598{
Dean Nelson5b8669d2008-07-29 22:34:18 -0700599 struct xpc_activate_mq_msghdr_uv *msg_hdr;
600 short partid;
601 struct xpc_partition *part;
Dean Nelson5b8669d2008-07-29 22:34:18 -0700602 int wakeup_hb_checker = 0;
Jack Steiner6f2584f2009-04-02 16:59:10 -0700603 int part_referenced;
Dean Nelson5b8669d2008-07-29 22:34:18 -0700604
Dean Nelson25257892008-11-05 17:28:00 -0600605 while (1) {
Jack Steiner6f2584f2009-04-02 16:59:10 -0700606 msg_hdr = gru_get_next_message(xpc_activate_mq_uv->gru_mq_desc);
Dean Nelson25257892008-11-05 17:28:00 -0600607 if (msg_hdr == NULL)
608 break;
Dean Nelson5b8669d2008-07-29 22:34:18 -0700609
610 partid = msg_hdr->partid;
611 if (partid < 0 || partid >= XP_MAX_NPARTITIONS_UV) {
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700612 dev_err(xpc_part, "xpc_handle_activate_IRQ_uv() "
613 "received invalid partid=0x%x in message\n",
Dean Nelson5b8669d2008-07-29 22:34:18 -0700614 partid);
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700615 } else {
616 part = &xpc_partitions[partid];
Jack Steiner6f2584f2009-04-02 16:59:10 -0700617
618 part_referenced = xpc_part_ref(part);
619 xpc_handle_activate_mq_msg_uv(part, msg_hdr,
620 &wakeup_hb_checker);
621 if (part_referenced)
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700622 xpc_part_deref(part);
Dean Nelson5b8669d2008-07-29 22:34:18 -0700623 }
624
Jack Steiner6f2584f2009-04-02 16:59:10 -0700625 gru_free_message(xpc_activate_mq_uv->gru_mq_desc, msg_hdr);
Dean Nelson5b8669d2008-07-29 22:34:18 -0700626 }
627
628 if (wakeup_hb_checker)
629 wake_up_interruptible(&xpc_activate_IRQ_wq);
630
631 return IRQ_HANDLED;
632}
633
634static enum xp_retval
Jack Steiner6f2584f2009-04-02 16:59:10 -0700635xpc_cache_remote_gru_mq_desc_uv(struct gru_message_queue_desc *gru_mq_desc,
636 unsigned long gru_mq_desc_gpa)
637{
638 enum xp_retval ret;
639
640 ret = xp_remote_memcpy(uv_gpa(gru_mq_desc), gru_mq_desc_gpa,
641 sizeof(struct gru_message_queue_desc));
642 if (ret == xpSuccess)
643 gru_mq_desc->mq = NULL;
644
645 return ret;
646}
647
648static enum xp_retval
Dean Nelson5b8669d2008-07-29 22:34:18 -0700649xpc_send_activate_IRQ_uv(struct xpc_partition *part, void *msg, size_t msg_size,
650 int msg_type)
651{
652 struct xpc_activate_mq_msghdr_uv *msg_hdr = msg;
Jack Steiner6f2584f2009-04-02 16:59:10 -0700653 struct xpc_partition_uv *part_uv = &part->sn.uv;
654 struct gru_message_queue_desc *gru_mq_desc;
655 unsigned long irq_flags;
656 enum xp_retval ret;
Dean Nelson5b8669d2008-07-29 22:34:18 -0700657
658 DBUG_ON(msg_size > XPC_ACTIVATE_MSG_SIZE_UV);
659
660 msg_hdr->type = msg_type;
Jack Steiner6f2584f2009-04-02 16:59:10 -0700661 msg_hdr->partid = xp_partition_id;
Dean Nelson5b8669d2008-07-29 22:34:18 -0700662 msg_hdr->act_state = part->act_state;
663 msg_hdr->rp_ts_jiffies = xpc_rsvd_page->ts_jiffies;
664
Jack Steiner6f2584f2009-04-02 16:59:10 -0700665 mutex_lock(&part_uv->cached_activate_gru_mq_desc_mutex);
666again:
667 if (!(part_uv->flags & XPC_P_CACHED_ACTIVATE_GRU_MQ_DESC_UV)) {
668 gru_mq_desc = part_uv->cached_activate_gru_mq_desc;
669 if (gru_mq_desc == NULL) {
670 gru_mq_desc = kmalloc(sizeof(struct
671 gru_message_queue_desc),
672 GFP_KERNEL);
673 if (gru_mq_desc == NULL) {
674 ret = xpNoMemory;
675 goto done;
676 }
677 part_uv->cached_activate_gru_mq_desc = gru_mq_desc;
678 }
679
680 ret = xpc_cache_remote_gru_mq_desc_uv(gru_mq_desc,
681 part_uv->
682 activate_gru_mq_desc_gpa);
683 if (ret != xpSuccess)
684 goto done;
685
686 spin_lock_irqsave(&part_uv->flags_lock, irq_flags);
687 part_uv->flags |= XPC_P_CACHED_ACTIVATE_GRU_MQ_DESC_UV;
688 spin_unlock_irqrestore(&part_uv->flags_lock, irq_flags);
689 }
690
Dean Nelson5b8669d2008-07-29 22:34:18 -0700691 /* ??? Is holding a spin_lock (ch->lock) during this call a bad idea? */
Jack Steiner6f2584f2009-04-02 16:59:10 -0700692 ret = xpc_send_gru_msg(part_uv->cached_activate_gru_mq_desc, msg,
693 msg_size);
694 if (ret != xpSuccess) {
695 smp_rmb(); /* ensure a fresh copy of part_uv->flags */
696 if (!(part_uv->flags & XPC_P_CACHED_ACTIVATE_GRU_MQ_DESC_UV))
697 goto again;
698 }
699done:
700 mutex_unlock(&part_uv->cached_activate_gru_mq_desc_mutex);
701 return ret;
Dean Nelson5b8669d2008-07-29 22:34:18 -0700702}
703
704static void
705xpc_send_activate_IRQ_part_uv(struct xpc_partition *part, void *msg,
706 size_t msg_size, int msg_type)
707{
708 enum xp_retval ret;
709
710 ret = xpc_send_activate_IRQ_uv(part, msg, msg_size, msg_type);
711 if (unlikely(ret != xpSuccess))
712 XPC_DEACTIVATE_PARTITION(part, ret);
713}
714
715static void
716xpc_send_activate_IRQ_ch_uv(struct xpc_channel *ch, unsigned long *irq_flags,
717 void *msg, size_t msg_size, int msg_type)
718{
Jack Steiner6f2584f2009-04-02 16:59:10 -0700719 struct xpc_partition *part = &xpc_partitions[ch->partid];
Dean Nelson5b8669d2008-07-29 22:34:18 -0700720 enum xp_retval ret;
721
722 ret = xpc_send_activate_IRQ_uv(part, msg, msg_size, msg_type);
723 if (unlikely(ret != xpSuccess)) {
724 if (irq_flags != NULL)
725 spin_unlock_irqrestore(&ch->lock, *irq_flags);
726
727 XPC_DEACTIVATE_PARTITION(part, ret);
728
729 if (irq_flags != NULL)
730 spin_lock_irqsave(&ch->lock, *irq_flags);
731 }
732}
733
734static void
735xpc_send_local_activate_IRQ_uv(struct xpc_partition *part, int act_state_req)
736{
737 unsigned long irq_flags;
738 struct xpc_partition_uv *part_uv = &part->sn.uv;
739
740 /*
Dean Nelson7d9d1f22008-11-05 17:29:48 -0600741 * !!! Make our side think that the remote partition sent an activate
Robin Holta374c572009-04-13 14:40:18 -0700742 * !!! mq message our way by doing what the activate IRQ handler would
Dean Nelson5b8669d2008-07-29 22:34:18 -0700743 * !!! do had one really been sent.
744 */
745
746 spin_lock_irqsave(&xpc_activate_IRQ_rcvd_lock, irq_flags);
747 if (part_uv->act_state_req == 0)
748 xpc_activate_IRQ_rcvd++;
749 part_uv->act_state_req = act_state_req;
750 spin_unlock_irqrestore(&xpc_activate_IRQ_rcvd_lock, irq_flags);
751
752 wake_up_interruptible(&xpc_activate_IRQ_wq);
753}
754
755static enum xp_retval
756xpc_get_partition_rsvd_page_pa_uv(void *buf, u64 *cookie, unsigned long *rp_pa,
757 size_t *len)
758{
Dean Nelson7d9d1f22008-11-05 17:29:48 -0600759 s64 status;
760 enum xp_retval ret;
761
762#if defined CONFIG_X86_64
763 status = uv_bios_reserved_page_pa((u64)buf, cookie, (u64 *)rp_pa,
764 (u64 *)len);
765 if (status == BIOS_STATUS_SUCCESS)
766 ret = xpSuccess;
767 else if (status == BIOS_STATUS_MORE_PASSES)
768 ret = xpNeedMoreInfo;
769 else
770 ret = xpBiosError;
771
772#elif defined CONFIG_IA64_GENERIC || defined CONFIG_IA64_SGI_UV
773 status = sn_partition_reserved_page_pa((u64)buf, cookie, rp_pa, len);
774 if (status == SALRET_OK)
775 ret = xpSuccess;
776 else if (status == SALRET_MORE_PASSES)
777 ret = xpNeedMoreInfo;
778 else
779 ret = xpSalError;
780
781#else
782 #error not a supported configuration
783#endif
784
785 return ret;
Dean Nelson5b8669d2008-07-29 22:34:18 -0700786}
787
788static int
Robin Holta7665b02009-04-13 14:40:19 -0700789xpc_setup_rsvd_page_uv(struct xpc_rsvd_page *rp)
Dean Nelson5b8669d2008-07-29 22:34:18 -0700790{
Robin Holta374c572009-04-13 14:40:18 -0700791 xpc_heartbeat_uv =
792 &xpc_partitions[sn_partition_id].sn.uv.cached_heartbeat;
793 rp->sn.uv.heartbeat_gpa = uv_gpa(xpc_heartbeat_uv);
794 rp->sn.uv.activate_gru_mq_desc_gpa =
Jack Steiner6f2584f2009-04-02 16:59:10 -0700795 uv_gpa(xpc_activate_mq_uv->gru_mq_desc);
Dean Nelson5b8669d2008-07-29 22:34:18 -0700796 return 0;
797}
798
799static void
Robin Holta374c572009-04-13 14:40:18 -0700800xpc_allow_hb_uv(short partid)
Dean Nelson5b8669d2008-07-29 22:34:18 -0700801{
Robin Holta374c572009-04-13 14:40:18 -0700802}
Dean Nelson5b8669d2008-07-29 22:34:18 -0700803
Robin Holta374c572009-04-13 14:40:18 -0700804static void
805xpc_disallow_hb_uv(short partid)
806{
807}
Dean Nelson5b8669d2008-07-29 22:34:18 -0700808
Robin Holta374c572009-04-13 14:40:18 -0700809static void
810xpc_disallow_all_hbs_uv(void)
811{
Dean Nelson94bd2702008-07-29 22:34:05 -0700812}
813
Dean Nelson33ba3c72008-07-29 22:34:07 -0700814static void
815xpc_increment_heartbeat_uv(void)
816{
Robin Holta374c572009-04-13 14:40:18 -0700817 xpc_heartbeat_uv->value++;
Dean Nelson5b8669d2008-07-29 22:34:18 -0700818}
819
820static void
821xpc_offline_heartbeat_uv(void)
822{
Robin Holta374c572009-04-13 14:40:18 -0700823 xpc_increment_heartbeat_uv();
824 xpc_heartbeat_uv->offline = 1;
Dean Nelson5b8669d2008-07-29 22:34:18 -0700825}
826
827static void
828xpc_online_heartbeat_uv(void)
829{
Robin Holta374c572009-04-13 14:40:18 -0700830 xpc_increment_heartbeat_uv();
831 xpc_heartbeat_uv->offline = 0;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700832}
833
834static void
835xpc_heartbeat_init_uv(void)
836{
Robin Holta374c572009-04-13 14:40:18 -0700837 xpc_heartbeat_uv->value = 1;
838 xpc_heartbeat_uv->offline = 0;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700839}
840
841static void
842xpc_heartbeat_exit_uv(void)
843{
Robin Holta374c572009-04-13 14:40:18 -0700844 xpc_offline_heartbeat_uv();
Dean Nelson5b8669d2008-07-29 22:34:18 -0700845}
846
847static enum xp_retval
848xpc_get_remote_heartbeat_uv(struct xpc_partition *part)
849{
850 struct xpc_partition_uv *part_uv = &part->sn.uv;
Robin Holta374c572009-04-13 14:40:18 -0700851 enum xp_retval ret;
Dean Nelson5b8669d2008-07-29 22:34:18 -0700852
Robin Holta374c572009-04-13 14:40:18 -0700853 ret = xp_remote_memcpy(uv_gpa(&part_uv->cached_heartbeat),
854 part_uv->heartbeat_gpa,
855 sizeof(struct xpc_heartbeat_uv));
856 if (ret != xpSuccess)
857 return ret;
Dean Nelson5b8669d2008-07-29 22:34:18 -0700858
Robin Holta374c572009-04-13 14:40:18 -0700859 if (part_uv->cached_heartbeat.value == part->last_heartbeat &&
860 !part_uv->cached_heartbeat.offline) {
Dean Nelson5b8669d2008-07-29 22:34:18 -0700861
Robin Holta374c572009-04-13 14:40:18 -0700862 ret = xpNoHeartbeat;
863 } else {
864 part->last_heartbeat = part_uv->cached_heartbeat.value;
Dean Nelson5b8669d2008-07-29 22:34:18 -0700865 }
866 return ret;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700867}
868
869static void
Dean Nelsona47d5da2008-07-29 22:34:09 -0700870xpc_request_partition_activation_uv(struct xpc_rsvd_page *remote_rp,
Dean Nelson5b8669d2008-07-29 22:34:18 -0700871 unsigned long remote_rp_gpa, int nasid)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700872{
873 short partid = remote_rp->SAL_partid;
874 struct xpc_partition *part = &xpc_partitions[partid];
Dean Nelson5b8669d2008-07-29 22:34:18 -0700875 struct xpc_activate_mq_msg_activate_req_uv msg;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700876
Dean Nelson5b8669d2008-07-29 22:34:18 -0700877 part->remote_rp_pa = remote_rp_gpa; /* !!! _pa here is really _gpa */
878 part->remote_rp_ts_jiffies = remote_rp->ts_jiffies;
Robin Holta374c572009-04-13 14:40:18 -0700879 part->sn.uv.heartbeat_gpa = remote_rp->sn.uv.heartbeat_gpa;
Jack Steiner6f2584f2009-04-02 16:59:10 -0700880 part->sn.uv.activate_gru_mq_desc_gpa =
Robin Holta374c572009-04-13 14:40:18 -0700881 remote_rp->sn.uv.activate_gru_mq_desc_gpa;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700882
Dean Nelson5b8669d2008-07-29 22:34:18 -0700883 /*
884 * ??? Is it a good idea to make this conditional on what is
885 * ??? potentially stale state information?
886 */
887 if (part->sn.uv.remote_act_state == XPC_P_AS_INACTIVE) {
888 msg.rp_gpa = uv_gpa(xpc_rsvd_page);
Robin Holta374c572009-04-13 14:40:18 -0700889 msg.heartbeat_gpa = xpc_rsvd_page->sn.uv.heartbeat_gpa;
Jack Steiner6f2584f2009-04-02 16:59:10 -0700890 msg.activate_gru_mq_desc_gpa =
Robin Holta374c572009-04-13 14:40:18 -0700891 xpc_rsvd_page->sn.uv.activate_gru_mq_desc_gpa;
Dean Nelson5b8669d2008-07-29 22:34:18 -0700892 xpc_send_activate_IRQ_part_uv(part, &msg, sizeof(msg),
893 XPC_ACTIVATE_MQ_MSG_ACTIVATE_REQ_UV);
894 }
895
896 if (part->act_state == XPC_P_AS_INACTIVE)
897 xpc_send_local_activate_IRQ_uv(part, XPC_P_ASR_ACTIVATE_UV);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700898}
899
Dean Nelsona47d5da2008-07-29 22:34:09 -0700900static void
901xpc_request_partition_reactivation_uv(struct xpc_partition *part)
902{
Dean Nelson5b8669d2008-07-29 22:34:18 -0700903 xpc_send_local_activate_IRQ_uv(part, XPC_P_ASR_ACTIVATE_UV);
904}
905
906static void
907xpc_request_partition_deactivation_uv(struct xpc_partition *part)
908{
909 struct xpc_activate_mq_msg_deactivate_req_uv msg;
910
911 /*
912 * ??? Is it a good idea to make this conditional on what is
913 * ??? potentially stale state information?
914 */
915 if (part->sn.uv.remote_act_state != XPC_P_AS_DEACTIVATING &&
916 part->sn.uv.remote_act_state != XPC_P_AS_INACTIVE) {
917
918 msg.reason = part->reason;
919 xpc_send_activate_IRQ_part_uv(part, &msg, sizeof(msg),
920 XPC_ACTIVATE_MQ_MSG_DEACTIVATE_REQ_UV);
921 }
Dean Nelsona47d5da2008-07-29 22:34:09 -0700922}
923
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700924static void
925xpc_cancel_partition_deactivation_request_uv(struct xpc_partition *part)
926{
927 /* nothing needs to be done */
928 return;
929}
930
931static void
932xpc_init_fifo_uv(struct xpc_fifo_head_uv *head)
933{
934 head->first = NULL;
935 head->last = NULL;
936 spin_lock_init(&head->lock);
937 head->n_entries = 0;
938}
939
940static void *
941xpc_get_fifo_entry_uv(struct xpc_fifo_head_uv *head)
942{
943 unsigned long irq_flags;
944 struct xpc_fifo_entry_uv *first;
945
946 spin_lock_irqsave(&head->lock, irq_flags);
947 first = head->first;
948 if (head->first != NULL) {
949 head->first = first->next;
950 if (head->first == NULL)
951 head->last = NULL;
Robin Holt15b87d62009-12-15 16:47:57 -0800952
953 head->n_entries--;
954 BUG_ON(head->n_entries < 0);
955
956 first->next = NULL;
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700957 }
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700958 spin_unlock_irqrestore(&head->lock, irq_flags);
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700959 return first;
960}
961
962static void
963xpc_put_fifo_entry_uv(struct xpc_fifo_head_uv *head,
964 struct xpc_fifo_entry_uv *last)
965{
966 unsigned long irq_flags;
967
968 last->next = NULL;
969 spin_lock_irqsave(&head->lock, irq_flags);
970 if (head->last != NULL)
971 head->last->next = last;
972 else
973 head->first = last;
974 head->last = last;
Jack Steiner6f2584f2009-04-02 16:59:10 -0700975 head->n_entries++;
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700976 spin_unlock_irqrestore(&head->lock, irq_flags);
977}
978
979static int
980xpc_n_of_fifo_entries_uv(struct xpc_fifo_head_uv *head)
981{
982 return head->n_entries;
983}
984
Dean Nelsone17d4162008-07-29 22:34:06 -0700985/*
Dean Nelson5b8669d2008-07-29 22:34:18 -0700986 * Setup the channel structures that are uv specific.
Dean Nelsone17d4162008-07-29 22:34:06 -0700987 */
988static enum xp_retval
Robin Holta7665b02009-04-13 14:40:19 -0700989xpc_setup_ch_structures_uv(struct xpc_partition *part)
Dean Nelsone17d4162008-07-29 22:34:06 -0700990{
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700991 struct xpc_channel_uv *ch_uv;
992 int ch_number;
993
994 for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
995 ch_uv = &part->channels[ch_number].sn.uv;
996
997 xpc_init_fifo_uv(&ch_uv->msg_slot_free_list);
998 xpc_init_fifo_uv(&ch_uv->recv_msg_list);
999 }
1000
1001 return xpSuccess;
Dean Nelsone17d4162008-07-29 22:34:06 -07001002}
1003
1004/*
Dean Nelson5b8669d2008-07-29 22:34:18 -07001005 * Teardown the channel structures that are uv specific.
Dean Nelsone17d4162008-07-29 22:34:06 -07001006 */
1007static void
Robin Holta7665b02009-04-13 14:40:19 -07001008xpc_teardown_ch_structures_uv(struct xpc_partition *part)
Dean Nelsone17d4162008-07-29 22:34:06 -07001009{
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07001010 /* nothing needs to be done */
Dean Nelsone17d4162008-07-29 22:34:06 -07001011 return;
1012}
1013
1014static enum xp_retval
1015xpc_make_first_contact_uv(struct xpc_partition *part)
1016{
Dean Nelson5b8669d2008-07-29 22:34:18 -07001017 struct xpc_activate_mq_msg_uv msg;
1018
1019 /*
1020 * We send a sync msg to get the remote partition's remote_act_state
1021 * updated to our current act_state which at this point should
1022 * be XPC_P_AS_ACTIVATING.
1023 */
1024 xpc_send_activate_IRQ_part_uv(part, &msg, sizeof(msg),
1025 XPC_ACTIVATE_MQ_MSG_SYNC_ACT_STATE_UV);
1026
Robin Holtdbd29182009-12-15 16:47:58 -08001027 while (!((part->sn.uv.remote_act_state == XPC_P_AS_ACTIVATING) ||
1028 (part->sn.uv.remote_act_state == XPC_P_AS_ACTIVE))) {
Dean Nelson5b8669d2008-07-29 22:34:18 -07001029
1030 dev_dbg(xpc_part, "waiting to make first contact with "
1031 "partition %d\n", XPC_PARTID(part));
1032
1033 /* wait a 1/4 of a second or so */
1034 (void)msleep_interruptible(250);
1035
1036 if (part->act_state == XPC_P_AS_DEACTIVATING)
1037 return part->reason;
1038 }
1039
1040 return xpSuccess;
Dean Nelsone17d4162008-07-29 22:34:06 -07001041}
1042
1043static u64
Dean Nelson7fb5e592008-07-29 22:34:10 -07001044xpc_get_chctl_all_flags_uv(struct xpc_partition *part)
Dean Nelsone17d4162008-07-29 22:34:06 -07001045{
Dean Nelson5b8669d2008-07-29 22:34:18 -07001046 unsigned long irq_flags;
1047 union xpc_channel_ctl_flags chctl;
1048
1049 spin_lock_irqsave(&part->chctl_lock, irq_flags);
1050 chctl = part->chctl;
1051 if (chctl.all_flags != 0)
1052 part->chctl.all_flags = 0;
1053
1054 spin_unlock_irqrestore(&part->chctl_lock, irq_flags);
1055 return chctl.all_flags;
1056}
1057
1058static enum xp_retval
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07001059xpc_allocate_send_msg_slot_uv(struct xpc_channel *ch)
Dean Nelson5b8669d2008-07-29 22:34:18 -07001060{
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07001061 struct xpc_channel_uv *ch_uv = &ch->sn.uv;
1062 struct xpc_send_msg_slot_uv *msg_slot;
1063 unsigned long irq_flags;
1064 int nentries;
1065 int entry;
1066 size_t nbytes;
1067
1068 for (nentries = ch->local_nentries; nentries > 0; nentries--) {
1069 nbytes = nentries * sizeof(struct xpc_send_msg_slot_uv);
1070 ch_uv->send_msg_slots = kzalloc(nbytes, GFP_KERNEL);
1071 if (ch_uv->send_msg_slots == NULL)
1072 continue;
1073
1074 for (entry = 0; entry < nentries; entry++) {
1075 msg_slot = &ch_uv->send_msg_slots[entry];
1076
1077 msg_slot->msg_slot_number = entry;
1078 xpc_put_fifo_entry_uv(&ch_uv->msg_slot_free_list,
1079 &msg_slot->next);
1080 }
1081
1082 spin_lock_irqsave(&ch->lock, irq_flags);
1083 if (nentries < ch->local_nentries)
1084 ch->local_nentries = nentries;
1085 spin_unlock_irqrestore(&ch->lock, irq_flags);
1086 return xpSuccess;
1087 }
1088
1089 return xpNoMemory;
Dean Nelson5b8669d2008-07-29 22:34:18 -07001090}
1091
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07001092static enum xp_retval
1093xpc_allocate_recv_msg_slot_uv(struct xpc_channel *ch)
1094{
1095 struct xpc_channel_uv *ch_uv = &ch->sn.uv;
1096 struct xpc_notify_mq_msg_uv *msg_slot;
1097 unsigned long irq_flags;
1098 int nentries;
1099 int entry;
1100 size_t nbytes;
1101
1102 for (nentries = ch->remote_nentries; nentries > 0; nentries--) {
1103 nbytes = nentries * ch->entry_size;
1104 ch_uv->recv_msg_slots = kzalloc(nbytes, GFP_KERNEL);
1105 if (ch_uv->recv_msg_slots == NULL)
1106 continue;
1107
1108 for (entry = 0; entry < nentries; entry++) {
Dean Nelson361916a2009-02-04 15:12:24 -08001109 msg_slot = ch_uv->recv_msg_slots +
1110 entry * ch->entry_size;
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07001111
1112 msg_slot->hdr.msg_slot_number = entry;
1113 }
1114
1115 spin_lock_irqsave(&ch->lock, irq_flags);
1116 if (nentries < ch->remote_nentries)
1117 ch->remote_nentries = nentries;
1118 spin_unlock_irqrestore(&ch->lock, irq_flags);
1119 return xpSuccess;
1120 }
1121
1122 return xpNoMemory;
1123}
1124
1125/*
1126 * Allocate msg_slots associated with the channel.
1127 */
1128static enum xp_retval
1129xpc_setup_msg_structures_uv(struct xpc_channel *ch)
1130{
1131 static enum xp_retval ret;
1132 struct xpc_channel_uv *ch_uv = &ch->sn.uv;
1133
1134 DBUG_ON(ch->flags & XPC_C_SETUP);
1135
Jack Steiner6f2584f2009-04-02 16:59:10 -07001136 ch_uv->cached_notify_gru_mq_desc = kmalloc(sizeof(struct
1137 gru_message_queue_desc),
1138 GFP_KERNEL);
1139 if (ch_uv->cached_notify_gru_mq_desc == NULL)
1140 return xpNoMemory;
1141
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07001142 ret = xpc_allocate_send_msg_slot_uv(ch);
1143 if (ret == xpSuccess) {
1144
1145 ret = xpc_allocate_recv_msg_slot_uv(ch);
1146 if (ret != xpSuccess) {
1147 kfree(ch_uv->send_msg_slots);
1148 xpc_init_fifo_uv(&ch_uv->msg_slot_free_list);
1149 }
1150 }
1151 return ret;
1152}
1153
1154/*
1155 * Free up msg_slots and clear other stuff that were setup for the specified
1156 * channel.
1157 */
Dean Nelson5b8669d2008-07-29 22:34:18 -07001158static void
1159xpc_teardown_msg_structures_uv(struct xpc_channel *ch)
1160{
1161 struct xpc_channel_uv *ch_uv = &ch->sn.uv;
1162
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07001163 DBUG_ON(!spin_is_locked(&ch->lock));
1164
Jack Steiner6f2584f2009-04-02 16:59:10 -07001165 kfree(ch_uv->cached_notify_gru_mq_desc);
1166 ch_uv->cached_notify_gru_mq_desc = NULL;
Dean Nelson5b8669d2008-07-29 22:34:18 -07001167
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07001168 if (ch->flags & XPC_C_SETUP) {
1169 xpc_init_fifo_uv(&ch_uv->msg_slot_free_list);
1170 kfree(ch_uv->send_msg_slots);
1171 xpc_init_fifo_uv(&ch_uv->recv_msg_list);
1172 kfree(ch_uv->recv_msg_slots);
1173 }
Dean Nelson5b8669d2008-07-29 22:34:18 -07001174}
1175
1176static void
1177xpc_send_chctl_closerequest_uv(struct xpc_channel *ch, unsigned long *irq_flags)
1178{
1179 struct xpc_activate_mq_msg_chctl_closerequest_uv msg;
1180
1181 msg.ch_number = ch->number;
1182 msg.reason = ch->reason;
1183 xpc_send_activate_IRQ_ch_uv(ch, irq_flags, &msg, sizeof(msg),
1184 XPC_ACTIVATE_MQ_MSG_CHCTL_CLOSEREQUEST_UV);
1185}
1186
1187static void
1188xpc_send_chctl_closereply_uv(struct xpc_channel *ch, unsigned long *irq_flags)
1189{
1190 struct xpc_activate_mq_msg_chctl_closereply_uv msg;
1191
1192 msg.ch_number = ch->number;
1193 xpc_send_activate_IRQ_ch_uv(ch, irq_flags, &msg, sizeof(msg),
1194 XPC_ACTIVATE_MQ_MSG_CHCTL_CLOSEREPLY_UV);
1195}
1196
1197static void
1198xpc_send_chctl_openrequest_uv(struct xpc_channel *ch, unsigned long *irq_flags)
1199{
1200 struct xpc_activate_mq_msg_chctl_openrequest_uv msg;
1201
1202 msg.ch_number = ch->number;
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07001203 msg.entry_size = ch->entry_size;
Dean Nelson5b8669d2008-07-29 22:34:18 -07001204 msg.local_nentries = ch->local_nentries;
1205 xpc_send_activate_IRQ_ch_uv(ch, irq_flags, &msg, sizeof(msg),
1206 XPC_ACTIVATE_MQ_MSG_CHCTL_OPENREQUEST_UV);
1207}
1208
1209static void
1210xpc_send_chctl_openreply_uv(struct xpc_channel *ch, unsigned long *irq_flags)
1211{
1212 struct xpc_activate_mq_msg_chctl_openreply_uv msg;
1213
1214 msg.ch_number = ch->number;
1215 msg.local_nentries = ch->local_nentries;
1216 msg.remote_nentries = ch->remote_nentries;
Jack Steiner6f2584f2009-04-02 16:59:10 -07001217 msg.notify_gru_mq_desc_gpa = uv_gpa(xpc_notify_mq_uv->gru_mq_desc);
Dean Nelson5b8669d2008-07-29 22:34:18 -07001218 xpc_send_activate_IRQ_ch_uv(ch, irq_flags, &msg, sizeof(msg),
1219 XPC_ACTIVATE_MQ_MSG_CHCTL_OPENREPLY_UV);
1220}
1221
1222static void
Robin Holtefdd06e2009-04-13 14:40:19 -07001223xpc_send_chctl_opencomplete_uv(struct xpc_channel *ch, unsigned long *irq_flags)
1224{
1225 struct xpc_activate_mq_msg_chctl_opencomplete_uv msg;
1226
1227 msg.ch_number = ch->number;
1228 xpc_send_activate_IRQ_ch_uv(ch, irq_flags, &msg, sizeof(msg),
1229 XPC_ACTIVATE_MQ_MSG_CHCTL_OPENCOMPLETE_UV);
1230}
1231
1232static void
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07001233xpc_send_chctl_local_msgrequest_uv(struct xpc_partition *part, int ch_number)
1234{
1235 unsigned long irq_flags;
1236
1237 spin_lock_irqsave(&part->chctl_lock, irq_flags);
1238 part->chctl.flags[ch_number] |= XPC_CHCTL_MSGREQUEST;
1239 spin_unlock_irqrestore(&part->chctl_lock, irq_flags);
1240
1241 xpc_wakeup_channel_mgr(part);
1242}
1243
Jack Steiner6f2584f2009-04-02 16:59:10 -07001244static enum xp_retval
Dean Nelson5b8669d2008-07-29 22:34:18 -07001245xpc_save_remote_msgqueue_pa_uv(struct xpc_channel *ch,
Jack Steiner6f2584f2009-04-02 16:59:10 -07001246 unsigned long gru_mq_desc_gpa)
Dean Nelson5b8669d2008-07-29 22:34:18 -07001247{
Jack Steiner6f2584f2009-04-02 16:59:10 -07001248 struct xpc_channel_uv *ch_uv = &ch->sn.uv;
1249
1250 DBUG_ON(ch_uv->cached_notify_gru_mq_desc == NULL);
1251 return xpc_cache_remote_gru_mq_desc_uv(ch_uv->cached_notify_gru_mq_desc,
1252 gru_mq_desc_gpa);
Dean Nelson5b8669d2008-07-29 22:34:18 -07001253}
1254
1255static void
1256xpc_indicate_partition_engaged_uv(struct xpc_partition *part)
1257{
1258 struct xpc_activate_mq_msg_uv msg;
1259
1260 xpc_send_activate_IRQ_part_uv(part, &msg, sizeof(msg),
1261 XPC_ACTIVATE_MQ_MSG_MARK_ENGAGED_UV);
1262}
1263
1264static void
1265xpc_indicate_partition_disengaged_uv(struct xpc_partition *part)
1266{
1267 struct xpc_activate_mq_msg_uv msg;
1268
1269 xpc_send_activate_IRQ_part_uv(part, &msg, sizeof(msg),
1270 XPC_ACTIVATE_MQ_MSG_MARK_DISENGAGED_UV);
1271}
1272
1273static void
1274xpc_assume_partition_disengaged_uv(short partid)
1275{
1276 struct xpc_partition_uv *part_uv = &xpc_partitions[partid].sn.uv;
1277 unsigned long irq_flags;
1278
1279 spin_lock_irqsave(&part_uv->flags_lock, irq_flags);
1280 part_uv->flags &= ~XPC_P_ENGAGED_UV;
1281 spin_unlock_irqrestore(&part_uv->flags_lock, irq_flags);
1282}
1283
1284static int
1285xpc_partition_engaged_uv(short partid)
1286{
1287 return (xpc_partitions[partid].sn.uv.flags & XPC_P_ENGAGED_UV) != 0;
1288}
1289
1290static int
1291xpc_any_partition_engaged_uv(void)
1292{
1293 struct xpc_partition_uv *part_uv;
1294 short partid;
1295
1296 for (partid = 0; partid < XP_MAX_NPARTITIONS_UV; partid++) {
1297 part_uv = &xpc_partitions[partid].sn.uv;
1298 if ((part_uv->flags & XPC_P_ENGAGED_UV) != 0)
1299 return 1;
1300 }
1301 return 0;
Dean Nelsone17d4162008-07-29 22:34:06 -07001302}
1303
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07001304static enum xp_retval
1305xpc_allocate_msg_slot_uv(struct xpc_channel *ch, u32 flags,
1306 struct xpc_send_msg_slot_uv **address_of_msg_slot)
Dean Nelsone17d4162008-07-29 22:34:06 -07001307{
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07001308 enum xp_retval ret;
1309 struct xpc_send_msg_slot_uv *msg_slot;
1310 struct xpc_fifo_entry_uv *entry;
1311
1312 while (1) {
1313 entry = xpc_get_fifo_entry_uv(&ch->sn.uv.msg_slot_free_list);
1314 if (entry != NULL)
1315 break;
1316
1317 if (flags & XPC_NOWAIT)
1318 return xpNoWait;
1319
1320 ret = xpc_allocate_msg_wait(ch);
1321 if (ret != xpInterrupted && ret != xpTimeout)
1322 return ret;
1323 }
1324
1325 msg_slot = container_of(entry, struct xpc_send_msg_slot_uv, next);
1326 *address_of_msg_slot = msg_slot;
1327 return xpSuccess;
1328}
1329
1330static void
1331xpc_free_msg_slot_uv(struct xpc_channel *ch,
1332 struct xpc_send_msg_slot_uv *msg_slot)
1333{
1334 xpc_put_fifo_entry_uv(&ch->sn.uv.msg_slot_free_list, &msg_slot->next);
1335
1336 /* wakeup anyone waiting for a free msg slot */
1337 if (atomic_read(&ch->n_on_msg_allocate_wq) > 0)
1338 wake_up(&ch->msg_allocate_wq);
1339}
1340
1341static void
1342xpc_notify_sender_uv(struct xpc_channel *ch,
1343 struct xpc_send_msg_slot_uv *msg_slot,
1344 enum xp_retval reason)
1345{
1346 xpc_notify_func func = msg_slot->func;
1347
1348 if (func != NULL && cmpxchg(&msg_slot->func, func, NULL) == func) {
1349
1350 atomic_dec(&ch->n_to_notify);
1351
1352 dev_dbg(xpc_chan, "msg_slot->func() called, msg_slot=0x%p "
1353 "msg_slot_number=%d partid=%d channel=%d\n", msg_slot,
1354 msg_slot->msg_slot_number, ch->partid, ch->number);
1355
1356 func(reason, ch->partid, ch->number, msg_slot->key);
1357
1358 dev_dbg(xpc_chan, "msg_slot->func() returned, msg_slot=0x%p "
1359 "msg_slot_number=%d partid=%d channel=%d\n", msg_slot,
1360 msg_slot->msg_slot_number, ch->partid, ch->number);
1361 }
1362}
1363
1364static void
1365xpc_handle_notify_mq_ack_uv(struct xpc_channel *ch,
1366 struct xpc_notify_mq_msg_uv *msg)
1367{
1368 struct xpc_send_msg_slot_uv *msg_slot;
1369 int entry = msg->hdr.msg_slot_number % ch->local_nentries;
1370
1371 msg_slot = &ch->sn.uv.send_msg_slots[entry];
1372
1373 BUG_ON(msg_slot->msg_slot_number != msg->hdr.msg_slot_number);
1374 msg_slot->msg_slot_number += ch->local_nentries;
1375
1376 if (msg_slot->func != NULL)
1377 xpc_notify_sender_uv(ch, msg_slot, xpMsgDelivered);
1378
1379 xpc_free_msg_slot_uv(ch, msg_slot);
1380}
1381
1382static void
1383xpc_handle_notify_mq_msg_uv(struct xpc_partition *part,
1384 struct xpc_notify_mq_msg_uv *msg)
1385{
1386 struct xpc_partition_uv *part_uv = &part->sn.uv;
1387 struct xpc_channel *ch;
1388 struct xpc_channel_uv *ch_uv;
1389 struct xpc_notify_mq_msg_uv *msg_slot;
1390 unsigned long irq_flags;
1391 int ch_number = msg->hdr.ch_number;
1392
1393 if (unlikely(ch_number >= part->nchannels)) {
1394 dev_err(xpc_part, "xpc_handle_notify_IRQ_uv() received invalid "
1395 "channel number=0x%x in message from partid=%d\n",
1396 ch_number, XPC_PARTID(part));
1397
1398 /* get hb checker to deactivate from the remote partition */
1399 spin_lock_irqsave(&xpc_activate_IRQ_rcvd_lock, irq_flags);
1400 if (part_uv->act_state_req == 0)
1401 xpc_activate_IRQ_rcvd++;
1402 part_uv->act_state_req = XPC_P_ASR_DEACTIVATE_UV;
1403 part_uv->reason = xpBadChannelNumber;
1404 spin_unlock_irqrestore(&xpc_activate_IRQ_rcvd_lock, irq_flags);
1405
1406 wake_up_interruptible(&xpc_activate_IRQ_wq);
1407 return;
1408 }
1409
1410 ch = &part->channels[ch_number];
1411 xpc_msgqueue_ref(ch);
1412
1413 if (!(ch->flags & XPC_C_CONNECTED)) {
1414 xpc_msgqueue_deref(ch);
1415 return;
1416 }
1417
1418 /* see if we're really dealing with an ACK for a previously sent msg */
1419 if (msg->hdr.size == 0) {
1420 xpc_handle_notify_mq_ack_uv(ch, msg);
1421 xpc_msgqueue_deref(ch);
1422 return;
1423 }
1424
1425 /* we're dealing with a normal message sent via the notify_mq */
1426 ch_uv = &ch->sn.uv;
1427
Dean Nelson361916a2009-02-04 15:12:24 -08001428 msg_slot = ch_uv->recv_msg_slots +
1429 (msg->hdr.msg_slot_number % ch->remote_nentries) * ch->entry_size;
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07001430
1431 BUG_ON(msg->hdr.msg_slot_number != msg_slot->hdr.msg_slot_number);
1432 BUG_ON(msg_slot->hdr.size != 0);
1433
1434 memcpy(msg_slot, msg, msg->hdr.size);
1435
1436 xpc_put_fifo_entry_uv(&ch_uv->recv_msg_list, &msg_slot->hdr.u.next);
1437
1438 if (ch->flags & XPC_C_CONNECTEDCALLOUT_MADE) {
1439 /*
1440 * If there is an existing idle kthread get it to deliver
1441 * the payload, otherwise we'll have to get the channel mgr
1442 * for this partition to create a kthread to do the delivery.
1443 */
1444 if (atomic_read(&ch->kthreads_idle) > 0)
1445 wake_up_nr(&ch->idle_wq, 1);
1446 else
1447 xpc_send_chctl_local_msgrequest_uv(part, ch->number);
1448 }
1449 xpc_msgqueue_deref(ch);
1450}
1451
1452static irqreturn_t
1453xpc_handle_notify_IRQ_uv(int irq, void *dev_id)
1454{
1455 struct xpc_notify_mq_msg_uv *msg;
1456 short partid;
1457 struct xpc_partition *part;
1458
Jack Steiner6f2584f2009-04-02 16:59:10 -07001459 while ((msg = gru_get_next_message(xpc_notify_mq_uv->gru_mq_desc)) !=
1460 NULL) {
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07001461
1462 partid = msg->hdr.partid;
1463 if (partid < 0 || partid >= XP_MAX_NPARTITIONS_UV) {
1464 dev_err(xpc_part, "xpc_handle_notify_IRQ_uv() received "
1465 "invalid partid=0x%x in message\n", partid);
1466 } else {
1467 part = &xpc_partitions[partid];
1468
1469 if (xpc_part_ref(part)) {
1470 xpc_handle_notify_mq_msg_uv(part, msg);
1471 xpc_part_deref(part);
1472 }
1473 }
1474
Jack Steiner6f2584f2009-04-02 16:59:10 -07001475 gru_free_message(xpc_notify_mq_uv->gru_mq_desc, msg);
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07001476 }
1477
1478 return IRQ_HANDLED;
1479}
1480
1481static int
1482xpc_n_of_deliverable_payloads_uv(struct xpc_channel *ch)
1483{
1484 return xpc_n_of_fifo_entries_uv(&ch->sn.uv.recv_msg_list);
1485}
1486
1487static void
1488xpc_process_msg_chctl_flags_uv(struct xpc_partition *part, int ch_number)
1489{
1490 struct xpc_channel *ch = &part->channels[ch_number];
1491 int ndeliverable_payloads;
1492
1493 xpc_msgqueue_ref(ch);
1494
1495 ndeliverable_payloads = xpc_n_of_deliverable_payloads_uv(ch);
1496
1497 if (ndeliverable_payloads > 0 &&
1498 (ch->flags & XPC_C_CONNECTED) &&
1499 (ch->flags & XPC_C_CONNECTEDCALLOUT_MADE)) {
1500
1501 xpc_activate_kthreads(ch, ndeliverable_payloads);
1502 }
1503
1504 xpc_msgqueue_deref(ch);
1505}
1506
1507static enum xp_retval
1508xpc_send_payload_uv(struct xpc_channel *ch, u32 flags, void *payload,
1509 u16 payload_size, u8 notify_type, xpc_notify_func func,
1510 void *key)
1511{
1512 enum xp_retval ret = xpSuccess;
1513 struct xpc_send_msg_slot_uv *msg_slot = NULL;
1514 struct xpc_notify_mq_msg_uv *msg;
1515 u8 msg_buffer[XPC_NOTIFY_MSG_SIZE_UV];
1516 size_t msg_size;
1517
1518 DBUG_ON(notify_type != XPC_N_CALL);
1519
1520 msg_size = sizeof(struct xpc_notify_mq_msghdr_uv) + payload_size;
1521 if (msg_size > ch->entry_size)
1522 return xpPayloadTooBig;
1523
1524 xpc_msgqueue_ref(ch);
1525
1526 if (ch->flags & XPC_C_DISCONNECTING) {
1527 ret = ch->reason;
1528 goto out_1;
1529 }
1530 if (!(ch->flags & XPC_C_CONNECTED)) {
1531 ret = xpNotConnected;
1532 goto out_1;
1533 }
1534
1535 ret = xpc_allocate_msg_slot_uv(ch, flags, &msg_slot);
1536 if (ret != xpSuccess)
1537 goto out_1;
1538
1539 if (func != NULL) {
1540 atomic_inc(&ch->n_to_notify);
1541
1542 msg_slot->key = key;
Robin Holt69b3bb62009-01-29 14:25:06 -08001543 smp_wmb(); /* a non-NULL func must hit memory after the key */
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07001544 msg_slot->func = func;
1545
1546 if (ch->flags & XPC_C_DISCONNECTING) {
1547 ret = ch->reason;
1548 goto out_2;
1549 }
1550 }
1551
1552 msg = (struct xpc_notify_mq_msg_uv *)&msg_buffer;
1553 msg->hdr.partid = xp_partition_id;
1554 msg->hdr.ch_number = ch->number;
1555 msg->hdr.size = msg_size;
1556 msg->hdr.msg_slot_number = msg_slot->msg_slot_number;
1557 memcpy(&msg->payload, payload, payload_size);
1558
Jack Steiner6f2584f2009-04-02 16:59:10 -07001559 ret = xpc_send_gru_msg(ch->sn.uv.cached_notify_gru_mq_desc, msg,
1560 msg_size);
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07001561 if (ret == xpSuccess)
1562 goto out_1;
1563
1564 XPC_DEACTIVATE_PARTITION(&xpc_partitions[ch->partid], ret);
1565out_2:
1566 if (func != NULL) {
1567 /*
1568 * Try to NULL the msg_slot's func field. If we fail, then
1569 * xpc_notify_senders_of_disconnect_uv() beat us to it, in which
1570 * case we need to pretend we succeeded to send the message
1571 * since the user will get a callout for the disconnect error
1572 * by xpc_notify_senders_of_disconnect_uv(), and to also get an
1573 * error returned here will confuse them. Additionally, since
1574 * in this case the channel is being disconnected we don't need
1575 * to put the the msg_slot back on the free list.
1576 */
1577 if (cmpxchg(&msg_slot->func, func, NULL) != func) {
1578 ret = xpSuccess;
1579 goto out_1;
1580 }
1581
1582 msg_slot->key = NULL;
1583 atomic_dec(&ch->n_to_notify);
1584 }
1585 xpc_free_msg_slot_uv(ch, msg_slot);
1586out_1:
1587 xpc_msgqueue_deref(ch);
1588 return ret;
1589}
1590
1591/*
1592 * Tell the callers of xpc_send_notify() that the status of their payloads
1593 * is unknown because the channel is now disconnecting.
1594 *
1595 * We don't worry about putting these msg_slots on the free list since the
1596 * msg_slots themselves are about to be kfree'd.
1597 */
1598static void
1599xpc_notify_senders_of_disconnect_uv(struct xpc_channel *ch)
1600{
1601 struct xpc_send_msg_slot_uv *msg_slot;
1602 int entry;
1603
1604 DBUG_ON(!(ch->flags & XPC_C_DISCONNECTING));
1605
1606 for (entry = 0; entry < ch->local_nentries; entry++) {
1607
1608 if (atomic_read(&ch->n_to_notify) == 0)
1609 break;
1610
1611 msg_slot = &ch->sn.uv.send_msg_slots[entry];
1612 if (msg_slot->func != NULL)
1613 xpc_notify_sender_uv(ch, msg_slot, ch->reason);
1614 }
1615}
1616
1617/*
1618 * Get the next deliverable message's payload.
1619 */
1620static void *
1621xpc_get_deliverable_payload_uv(struct xpc_channel *ch)
1622{
1623 struct xpc_fifo_entry_uv *entry;
1624 struct xpc_notify_mq_msg_uv *msg;
1625 void *payload = NULL;
1626
1627 if (!(ch->flags & XPC_C_DISCONNECTING)) {
1628 entry = xpc_get_fifo_entry_uv(&ch->sn.uv.recv_msg_list);
1629 if (entry != NULL) {
1630 msg = container_of(entry, struct xpc_notify_mq_msg_uv,
1631 hdr.u.next);
1632 payload = &msg->payload;
1633 }
1634 }
1635 return payload;
1636}
1637
1638static void
1639xpc_received_payload_uv(struct xpc_channel *ch, void *payload)
1640{
1641 struct xpc_notify_mq_msg_uv *msg;
1642 enum xp_retval ret;
1643
1644 msg = container_of(payload, struct xpc_notify_mq_msg_uv, payload);
1645
1646 /* return an ACK to the sender of this message */
1647
1648 msg->hdr.partid = xp_partition_id;
1649 msg->hdr.size = 0; /* size of zero indicates this is an ACK */
1650
Jack Steiner6f2584f2009-04-02 16:59:10 -07001651 ret = xpc_send_gru_msg(ch->sn.uv.cached_notify_gru_mq_desc, msg,
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07001652 sizeof(struct xpc_notify_mq_msghdr_uv));
1653 if (ret != xpSuccess)
1654 XPC_DEACTIVATE_PARTITION(&xpc_partitions[ch->partid], ret);
1655
1656 msg->hdr.msg_slot_number += ch->remote_nentries;
Dean Nelsone17d4162008-07-29 22:34:06 -07001657}
1658
Robin Holta7665b02009-04-13 14:40:19 -07001659static struct xpc_arch_operations xpc_arch_ops_uv = {
1660 .setup_partitions = xpc_setup_partitions_uv,
1661 .teardown_partitions = xpc_teardown_partitions_uv,
1662 .process_activate_IRQ_rcvd = xpc_process_activate_IRQ_rcvd_uv,
1663 .get_partition_rsvd_page_pa = xpc_get_partition_rsvd_page_pa_uv,
1664 .setup_rsvd_page = xpc_setup_rsvd_page_uv,
1665
1666 .allow_hb = xpc_allow_hb_uv,
1667 .disallow_hb = xpc_disallow_hb_uv,
1668 .disallow_all_hbs = xpc_disallow_all_hbs_uv,
1669 .increment_heartbeat = xpc_increment_heartbeat_uv,
1670 .offline_heartbeat = xpc_offline_heartbeat_uv,
1671 .online_heartbeat = xpc_online_heartbeat_uv,
1672 .heartbeat_init = xpc_heartbeat_init_uv,
1673 .heartbeat_exit = xpc_heartbeat_exit_uv,
1674 .get_remote_heartbeat = xpc_get_remote_heartbeat_uv,
1675
1676 .request_partition_activation =
1677 xpc_request_partition_activation_uv,
1678 .request_partition_reactivation =
1679 xpc_request_partition_reactivation_uv,
1680 .request_partition_deactivation =
1681 xpc_request_partition_deactivation_uv,
1682 .cancel_partition_deactivation_request =
1683 xpc_cancel_partition_deactivation_request_uv,
1684
1685 .setup_ch_structures = xpc_setup_ch_structures_uv,
1686 .teardown_ch_structures = xpc_teardown_ch_structures_uv,
1687
1688 .make_first_contact = xpc_make_first_contact_uv,
1689
1690 .get_chctl_all_flags = xpc_get_chctl_all_flags_uv,
1691 .send_chctl_closerequest = xpc_send_chctl_closerequest_uv,
1692 .send_chctl_closereply = xpc_send_chctl_closereply_uv,
1693 .send_chctl_openrequest = xpc_send_chctl_openrequest_uv,
1694 .send_chctl_openreply = xpc_send_chctl_openreply_uv,
1695 .send_chctl_opencomplete = xpc_send_chctl_opencomplete_uv,
1696 .process_msg_chctl_flags = xpc_process_msg_chctl_flags_uv,
1697
1698 .save_remote_msgqueue_pa = xpc_save_remote_msgqueue_pa_uv,
1699
1700 .setup_msg_structures = xpc_setup_msg_structures_uv,
1701 .teardown_msg_structures = xpc_teardown_msg_structures_uv,
1702
1703 .indicate_partition_engaged = xpc_indicate_partition_engaged_uv,
1704 .indicate_partition_disengaged = xpc_indicate_partition_disengaged_uv,
1705 .assume_partition_disengaged = xpc_assume_partition_disengaged_uv,
1706 .partition_engaged = xpc_partition_engaged_uv,
1707 .any_partition_engaged = xpc_any_partition_engaged_uv,
1708
1709 .n_of_deliverable_payloads = xpc_n_of_deliverable_payloads_uv,
1710 .send_payload = xpc_send_payload_uv,
1711 .get_deliverable_payload = xpc_get_deliverable_payload_uv,
1712 .received_payload = xpc_received_payload_uv,
1713 .notify_senders_of_disconnect = xpc_notify_senders_of_disconnect_uv,
1714};
1715
Dean Nelson5b8669d2008-07-29 22:34:18 -07001716int
Dean Nelson94bd2702008-07-29 22:34:05 -07001717xpc_init_uv(void)
1718{
Robin Holta7665b02009-04-13 14:40:19 -07001719 xpc_arch_ops = xpc_arch_ops_uv;
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07001720
1721 if (sizeof(struct xpc_notify_mq_msghdr_uv) > XPC_MSG_HDR_MAX_SIZE) {
1722 dev_err(xpc_part, "xpc_notify_mq_msghdr_uv is larger than %d\n",
1723 XPC_MSG_HDR_MAX_SIZE);
1724 return -E2BIG;
1725 }
Dean Nelson5b8669d2008-07-29 22:34:18 -07001726
Dean Nelson25257892008-11-05 17:28:00 -06001727 xpc_activate_mq_uv = xpc_create_gru_mq_uv(XPC_ACTIVATE_MQ_SIZE_UV, 0,
1728 XPC_ACTIVATE_IRQ_NAME,
Dean Nelson5b8669d2008-07-29 22:34:18 -07001729 xpc_handle_activate_IRQ_uv);
Dean Nelson25257892008-11-05 17:28:00 -06001730 if (IS_ERR(xpc_activate_mq_uv))
1731 return PTR_ERR(xpc_activate_mq_uv);
Dean Nelson5b8669d2008-07-29 22:34:18 -07001732
Dean Nelson25257892008-11-05 17:28:00 -06001733 xpc_notify_mq_uv = xpc_create_gru_mq_uv(XPC_NOTIFY_MQ_SIZE_UV, 0,
1734 XPC_NOTIFY_IRQ_NAME,
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07001735 xpc_handle_notify_IRQ_uv);
Dean Nelson25257892008-11-05 17:28:00 -06001736 if (IS_ERR(xpc_notify_mq_uv)) {
1737 xpc_destroy_gru_mq_uv(xpc_activate_mq_uv);
1738 return PTR_ERR(xpc_notify_mq_uv);
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07001739 }
1740
Dean Nelson5b8669d2008-07-29 22:34:18 -07001741 return 0;
Dean Nelson94bd2702008-07-29 22:34:05 -07001742}
1743
1744void
1745xpc_exit_uv(void)
1746{
Dean Nelson25257892008-11-05 17:28:00 -06001747 xpc_destroy_gru_mq_uv(xpc_notify_mq_uv);
1748 xpc_destroy_gru_mq_uv(xpc_activate_mq_uv);
Dean Nelson94bd2702008-07-29 22:34:05 -07001749}