blob: a760dd08e4259f67dfc2e4774c286dc3e57323d9 [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 *
6 * Copyright (c) 2008 Silicon Graphics, Inc. All Rights Reserved.
7 */
8
9/*
10 * Cross Partition Communication (XPC) sn2-based functions.
11 *
12 * Architecture specific implementation of common functions.
13 *
14 */
15
Dean Nelsone17d4162008-07-29 22:34:06 -070016#include <linux/delay.h>
Dean Nelson94bd2702008-07-29 22:34:05 -070017#include <asm/uncached.h>
Dean Nelson261f3b42008-07-29 22:34:16 -070018#include <asm/sn/mspec.h>
Dean Nelson94bd2702008-07-29 22:34:05 -070019#include <asm/sn/sn_sal.h>
20#include "xpc.h"
21
Dean Nelsonee6665e2008-07-29 22:34:13 -070022/*
23 * Define the number of u64s required to represent all the C-brick nasids
24 * as a bitmap. The cross-partition kernel modules deal only with
25 * C-brick nasids, thus the need for bitmaps which don't account for
26 * odd-numbered (non C-brick) nasids.
27 */
28#define XPC_MAX_PHYSNODES_SN2 (MAX_NUMALINK_NODES / 2)
29#define XP_NASID_MASK_BYTES_SN2 ((XPC_MAX_PHYSNODES_SN2 + 7) / 8)
30#define XP_NASID_MASK_WORDS_SN2 ((XPC_MAX_PHYSNODES_SN2 + 63) / 64)
31
32/*
33 * Memory for XPC's amo variables is allocated by the MSPEC driver. These
34 * pages are located in the lowest granule. The lowest granule uses 4k pages
35 * for cached references and an alternate TLB handler to never provide a
36 * cacheable mapping for the entire region. This will prevent speculative
37 * reading of cached copies of our lines from being issued which will cause
38 * a PI FSB Protocol error to be generated by the SHUB. For XPC, we need 64
39 * amo variables (based on XP_MAX_NPARTITIONS_SN2) to identify the senders of
40 * NOTIFY IRQs, 128 amo variables (based on XP_NASID_MASK_WORDS_SN2) to identify
41 * the senders of ACTIVATE IRQs, 1 amo variable to identify which remote
42 * partitions (i.e., XPCs) consider themselves currently engaged with the
43 * local XPC and 1 amo variable to request partition deactivation.
44 */
45#define XPC_NOTIFY_IRQ_AMOS_SN2 0
46#define XPC_ACTIVATE_IRQ_AMOS_SN2 (XPC_NOTIFY_IRQ_AMOS_SN2 + \
47 XP_MAX_NPARTITIONS_SN2)
48#define XPC_ENGAGED_PARTITIONS_AMO_SN2 (XPC_ACTIVATE_IRQ_AMOS_SN2 + \
49 XP_NASID_MASK_WORDS_SN2)
50#define XPC_DEACTIVATE_REQUEST_AMO_SN2 (XPC_ENGAGED_PARTITIONS_AMO_SN2 + 1)
51
52/*
53 * Buffer used to store a local copy of portions of a remote partition's
54 * reserved page (either its header and part_nasids mask, or its vars).
55 */
Dean Nelsonee6665e2008-07-29 22:34:13 -070056static void *xpc_remote_copy_buffer_base_sn2;
Dean Nelson5b8669d2008-07-29 22:34:18 -070057static char *xpc_remote_copy_buffer_sn2;
Dean Nelsonee6665e2008-07-29 22:34:13 -070058
Dean Nelson8e85c232008-07-29 22:34:13 -070059static struct xpc_vars_sn2 *xpc_vars_sn2;
60static struct xpc_vars_part_sn2 *xpc_vars_part_sn2;
Dean Nelson94bd2702008-07-29 22:34:05 -070061
Dean Nelson5b8669d2008-07-29 22:34:18 -070062static int
63xpc_setup_partitions_sn_sn2(void)
64{
65 /* nothing needs to be done */
66 return 0;
67}
68
Jack Steiner6f2584f2009-04-02 16:59:10 -070069static void
70xpc_teardown_partitions_sn_sn2(void)
71{
72 /* nothing needs to be done */
73}
74
Dean Nelson6e41017a2008-07-29 22:34:09 -070075/* SH_IPI_ACCESS shub register value on startup */
Dean Nelson8e85c232008-07-29 22:34:13 -070076static u64 xpc_sh1_IPI_access_sn2;
77static u64 xpc_sh2_IPI_access0_sn2;
78static u64 xpc_sh2_IPI_access1_sn2;
79static u64 xpc_sh2_IPI_access2_sn2;
80static u64 xpc_sh2_IPI_access3_sn2;
Dean Nelson6e41017a2008-07-29 22:34:09 -070081
82/*
83 * Change protections to allow IPI operations.
84 */
85static void
86xpc_allow_IPI_ops_sn2(void)
87{
88 int node;
89 int nasid;
90
Dean Nelsonea57f802008-07-29 22:34:14 -070091 /* !!! The following should get moved into SAL. */
Dean Nelson6e41017a2008-07-29 22:34:09 -070092 if (is_shub2()) {
Dean Nelson8e85c232008-07-29 22:34:13 -070093 xpc_sh2_IPI_access0_sn2 =
Dean Nelson6e41017a2008-07-29 22:34:09 -070094 (u64)HUB_L((u64 *)LOCAL_MMR_ADDR(SH2_IPI_ACCESS0));
Dean Nelson8e85c232008-07-29 22:34:13 -070095 xpc_sh2_IPI_access1_sn2 =
Dean Nelson6e41017a2008-07-29 22:34:09 -070096 (u64)HUB_L((u64 *)LOCAL_MMR_ADDR(SH2_IPI_ACCESS1));
Dean Nelson8e85c232008-07-29 22:34:13 -070097 xpc_sh2_IPI_access2_sn2 =
Dean Nelson6e41017a2008-07-29 22:34:09 -070098 (u64)HUB_L((u64 *)LOCAL_MMR_ADDR(SH2_IPI_ACCESS2));
Dean Nelson8e85c232008-07-29 22:34:13 -070099 xpc_sh2_IPI_access3_sn2 =
Dean Nelson6e41017a2008-07-29 22:34:09 -0700100 (u64)HUB_L((u64 *)LOCAL_MMR_ADDR(SH2_IPI_ACCESS3));
101
102 for_each_online_node(node) {
103 nasid = cnodeid_to_nasid(node);
104 HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS0),
105 -1UL);
106 HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS1),
107 -1UL);
108 HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS2),
109 -1UL);
110 HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS3),
111 -1UL);
112 }
113 } else {
Dean Nelson8e85c232008-07-29 22:34:13 -0700114 xpc_sh1_IPI_access_sn2 =
Dean Nelson6e41017a2008-07-29 22:34:09 -0700115 (u64)HUB_L((u64 *)LOCAL_MMR_ADDR(SH1_IPI_ACCESS));
116
117 for_each_online_node(node) {
118 nasid = cnodeid_to_nasid(node);
119 HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH1_IPI_ACCESS),
120 -1UL);
121 }
122 }
123}
124
125/*
126 * Restrict protections to disallow IPI operations.
127 */
128static void
129xpc_disallow_IPI_ops_sn2(void)
130{
131 int node;
132 int nasid;
133
Dean Nelsonea57f802008-07-29 22:34:14 -0700134 /* !!! The following should get moved into SAL. */
Dean Nelson6e41017a2008-07-29 22:34:09 -0700135 if (is_shub2()) {
136 for_each_online_node(node) {
137 nasid = cnodeid_to_nasid(node);
138 HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS0),
Dean Nelson8e85c232008-07-29 22:34:13 -0700139 xpc_sh2_IPI_access0_sn2);
Dean Nelson6e41017a2008-07-29 22:34:09 -0700140 HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS1),
Dean Nelson8e85c232008-07-29 22:34:13 -0700141 xpc_sh2_IPI_access1_sn2);
Dean Nelson6e41017a2008-07-29 22:34:09 -0700142 HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS2),
Dean Nelson8e85c232008-07-29 22:34:13 -0700143 xpc_sh2_IPI_access2_sn2);
Dean Nelson6e41017a2008-07-29 22:34:09 -0700144 HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS3),
Dean Nelson8e85c232008-07-29 22:34:13 -0700145 xpc_sh2_IPI_access3_sn2);
Dean Nelson6e41017a2008-07-29 22:34:09 -0700146 }
147 } else {
148 for_each_online_node(node) {
149 nasid = cnodeid_to_nasid(node);
150 HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH1_IPI_ACCESS),
Dean Nelson8e85c232008-07-29 22:34:13 -0700151 xpc_sh1_IPI_access_sn2);
Dean Nelson6e41017a2008-07-29 22:34:09 -0700152 }
153 }
154}
155
Dean Nelson33ba3c72008-07-29 22:34:07 -0700156/*
Dean Nelson7fb5e592008-07-29 22:34:10 -0700157 * The following set of functions are used for the sending and receiving of
158 * IRQs (also known as IPIs). There are two flavors of IRQs, one that is
159 * associated with partition activity (SGI_XPC_ACTIVATE) and the other that
160 * is associated with channel activity (SGI_XPC_NOTIFY).
Dean Nelson33ba3c72008-07-29 22:34:07 -0700161 */
162
163static u64
Dean Nelsonc39838c2008-07-29 22:34:11 -0700164xpc_receive_IRQ_amo_sn2(struct amo *amo)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700165{
166 return FETCHOP_LOAD_OP(TO_AMO((u64)&amo->variable), FETCHOP_CLEAR);
167}
168
169static enum xp_retval
Dean Nelsonc39838c2008-07-29 22:34:11 -0700170xpc_send_IRQ_sn2(struct amo *amo, u64 flag, int nasid, int phys_cpuid,
171 int vector)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700172{
173 int ret = 0;
174 unsigned long irq_flags;
175
176 local_irq_save(irq_flags);
177
178 FETCHOP_STORE_OP(TO_AMO((u64)&amo->variable), FETCHOP_OR, flag);
179 sn_send_IPI_phys(nasid, phys_cpuid, vector, 0);
180
181 /*
182 * We must always use the nofault function regardless of whether we
183 * are on a Shub 1.1 system or a Shub 1.2 slice 0xc processor. If we
184 * didn't, we'd never know that the other partition is down and would
Dean Nelsonc39838c2008-07-29 22:34:11 -0700185 * keep sending IRQs and amos to it until the heartbeat times out.
Dean Nelson33ba3c72008-07-29 22:34:07 -0700186 */
187 ret = xp_nofault_PIOR((u64 *)GLOBAL_MMR_ADDR(NASID_GET(&amo->variable),
188 xp_nofault_PIOR_target));
189
190 local_irq_restore(irq_flags);
191
Dean Nelson261f3b42008-07-29 22:34:16 -0700192 return (ret == 0) ? xpSuccess : xpPioReadError;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700193}
194
Dean Nelsonc39838c2008-07-29 22:34:11 -0700195static struct amo *
Dean Nelson7fb5e592008-07-29 22:34:10 -0700196xpc_init_IRQ_amo_sn2(int index)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700197{
Dean Nelson8e85c232008-07-29 22:34:13 -0700198 struct amo *amo = xpc_vars_sn2->amos_page + index;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700199
Dean Nelsonc39838c2008-07-29 22:34:11 -0700200 (void)xpc_receive_IRQ_amo_sn2(amo); /* clear amo variable */
Dean Nelson33ba3c72008-07-29 22:34:07 -0700201 return amo;
202}
203
204/*
Dean Nelson7fb5e592008-07-29 22:34:10 -0700205 * Functions associated with SGI_XPC_ACTIVATE IRQ.
Dean Nelson33ba3c72008-07-29 22:34:07 -0700206 */
207
208/*
Dean Nelson6e41017a2008-07-29 22:34:09 -0700209 * Notify the heartbeat check thread that an activate IRQ has been received.
210 */
211static irqreturn_t
212xpc_handle_activate_IRQ_sn2(int irq, void *dev_id)
213{
Dean Nelson5b8669d2008-07-29 22:34:18 -0700214 unsigned long irq_flags;
215
216 spin_lock_irqsave(&xpc_activate_IRQ_rcvd_lock, irq_flags);
217 xpc_activate_IRQ_rcvd++;
218 spin_unlock_irqrestore(&xpc_activate_IRQ_rcvd_lock, irq_flags);
219
Dean Nelson6e41017a2008-07-29 22:34:09 -0700220 wake_up_interruptible(&xpc_activate_IRQ_wq);
221 return IRQ_HANDLED;
222}
223
224/*
Dean Nelsonc39838c2008-07-29 22:34:11 -0700225 * Flag the appropriate amo variable and send an IRQ to the specified node.
Dean Nelson33ba3c72008-07-29 22:34:07 -0700226 */
227static void
Dean Nelsona812dcc2008-07-29 22:34:16 -0700228xpc_send_activate_IRQ_sn2(unsigned long amos_page_pa, int from_nasid,
229 int to_nasid, int to_phys_cpuid)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700230{
Dean Nelsonc39838c2008-07-29 22:34:11 -0700231 struct amo *amos = (struct amo *)__va(amos_page_pa +
Dean Nelsonee6665e2008-07-29 22:34:13 -0700232 (XPC_ACTIVATE_IRQ_AMOS_SN2 *
Dean Nelsonc39838c2008-07-29 22:34:11 -0700233 sizeof(struct amo)));
Dean Nelson33ba3c72008-07-29 22:34:07 -0700234
Dean Nelson04de7412008-07-29 22:34:14 -0700235 (void)xpc_send_IRQ_sn2(&amos[BIT_WORD(from_nasid / 2)],
236 BIT_MASK(from_nasid / 2), to_nasid,
Dean Nelson33ba3c72008-07-29 22:34:07 -0700237 to_phys_cpuid, SGI_XPC_ACTIVATE);
238}
239
240static void
Dean Nelson7fb5e592008-07-29 22:34:10 -0700241xpc_send_local_activate_IRQ_sn2(int from_nasid)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700242{
Dean Nelson5b8669d2008-07-29 22:34:18 -0700243 unsigned long irq_flags;
Dean Nelson8e85c232008-07-29 22:34:13 -0700244 struct amo *amos = (struct amo *)__va(xpc_vars_sn2->amos_page_pa +
Dean Nelsonee6665e2008-07-29 22:34:13 -0700245 (XPC_ACTIVATE_IRQ_AMOS_SN2 *
Dean Nelsonc39838c2008-07-29 22:34:11 -0700246 sizeof(struct amo)));
Dean Nelson33ba3c72008-07-29 22:34:07 -0700247
248 /* fake the sending and receipt of an activate IRQ from remote nasid */
Dean Nelson04de7412008-07-29 22:34:14 -0700249 FETCHOP_STORE_OP(TO_AMO((u64)&amos[BIT_WORD(from_nasid / 2)].variable),
250 FETCHOP_OR, BIT_MASK(from_nasid / 2));
251
Dean Nelson5b8669d2008-07-29 22:34:18 -0700252 spin_lock_irqsave(&xpc_activate_IRQ_rcvd_lock, irq_flags);
253 xpc_activate_IRQ_rcvd++;
254 spin_unlock_irqrestore(&xpc_activate_IRQ_rcvd_lock, irq_flags);
255
Dean Nelson6e41017a2008-07-29 22:34:09 -0700256 wake_up_interruptible(&xpc_activate_IRQ_wq);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700257}
258
Dean Nelson33ba3c72008-07-29 22:34:07 -0700259/*
Dean Nelson7fb5e592008-07-29 22:34:10 -0700260 * Functions associated with SGI_XPC_NOTIFY IRQ.
Dean Nelson33ba3c72008-07-29 22:34:07 -0700261 */
262
263/*
Dean Nelson7fb5e592008-07-29 22:34:10 -0700264 * Check to see if any chctl flags were sent from the specified partition.
Dean Nelsona47d5da2008-07-29 22:34:09 -0700265 */
266static void
Dean Nelson7fb5e592008-07-29 22:34:10 -0700267xpc_check_for_sent_chctl_flags_sn2(struct xpc_partition *part)
Dean Nelsona47d5da2008-07-29 22:34:09 -0700268{
Dean Nelson7fb5e592008-07-29 22:34:10 -0700269 union xpc_channel_ctl_flags chctl;
Dean Nelsona47d5da2008-07-29 22:34:09 -0700270 unsigned long irq_flags;
271
Dean Nelson7fb5e592008-07-29 22:34:10 -0700272 chctl.all_flags = xpc_receive_IRQ_amo_sn2(part->sn.sn2.
273 local_chctl_amo_va);
274 if (chctl.all_flags == 0)
Dean Nelsona47d5da2008-07-29 22:34:09 -0700275 return;
276
Dean Nelson7fb5e592008-07-29 22:34:10 -0700277 spin_lock_irqsave(&part->chctl_lock, irq_flags);
278 part->chctl.all_flags |= chctl.all_flags;
279 spin_unlock_irqrestore(&part->chctl_lock, irq_flags);
Dean Nelsona47d5da2008-07-29 22:34:09 -0700280
Dean Nelson7fb5e592008-07-29 22:34:10 -0700281 dev_dbg(xpc_chan, "received notify IRQ from partid=%d, chctl.all_flags="
282 "0x%lx\n", XPC_PARTID(part), chctl.all_flags);
Dean Nelsona47d5da2008-07-29 22:34:09 -0700283
284 xpc_wakeup_channel_mgr(part);
285}
286
287/*
288 * Handle the receipt of a SGI_XPC_NOTIFY IRQ by seeing whether the specified
289 * partition actually sent it. Since SGI_XPC_NOTIFY IRQs may be shared by more
Dean Nelsonc39838c2008-07-29 22:34:11 -0700290 * than one partition, we use an amo structure per partition to indicate
Dean Nelson7fb5e592008-07-29 22:34:10 -0700291 * whether a partition has sent an IRQ or not. If it has, then wake up the
Dean Nelsona47d5da2008-07-29 22:34:09 -0700292 * associated kthread to handle it.
293 *
Dean Nelson7fb5e592008-07-29 22:34:10 -0700294 * All SGI_XPC_NOTIFY IRQs received by XPC are the result of IRQs sent by XPC
Dean Nelsona47d5da2008-07-29 22:34:09 -0700295 * running on other partitions.
296 *
297 * Noteworthy Arguments:
298 *
299 * irq - Interrupt ReQuest number. NOT USED.
300 *
Dean Nelson7fb5e592008-07-29 22:34:10 -0700301 * dev_id - partid of IRQ's potential sender.
Dean Nelsona47d5da2008-07-29 22:34:09 -0700302 */
303static irqreturn_t
304xpc_handle_notify_IRQ_sn2(int irq, void *dev_id)
305{
306 short partid = (short)(u64)dev_id;
307 struct xpc_partition *part = &xpc_partitions[partid];
308
Dean Nelson261f3b42008-07-29 22:34:16 -0700309 DBUG_ON(partid < 0 || partid >= XP_MAX_NPARTITIONS_SN2);
Dean Nelsona47d5da2008-07-29 22:34:09 -0700310
311 if (xpc_part_ref(part)) {
Dean Nelson7fb5e592008-07-29 22:34:10 -0700312 xpc_check_for_sent_chctl_flags_sn2(part);
Dean Nelsona47d5da2008-07-29 22:34:09 -0700313
314 xpc_part_deref(part);
315 }
316 return IRQ_HANDLED;
317}
318
319/*
Dean Nelson7fb5e592008-07-29 22:34:10 -0700320 * Check to see if xpc_handle_notify_IRQ_sn2() dropped any IRQs on the floor
321 * because the write to their associated amo variable completed after the IRQ
Dean Nelsona47d5da2008-07-29 22:34:09 -0700322 * was received.
323 */
324static void
Dean Nelson7fb5e592008-07-29 22:34:10 -0700325xpc_check_for_dropped_notify_IRQ_sn2(struct xpc_partition *part)
Dean Nelsona47d5da2008-07-29 22:34:09 -0700326{
327 struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
328
329 if (xpc_part_ref(part)) {
Dean Nelson7fb5e592008-07-29 22:34:10 -0700330 xpc_check_for_sent_chctl_flags_sn2(part);
Dean Nelsona47d5da2008-07-29 22:34:09 -0700331
332 part_sn2->dropped_notify_IRQ_timer.expires = jiffies +
Dean Nelson7fb5e592008-07-29 22:34:10 -0700333 XPC_DROPPED_NOTIFY_IRQ_WAIT_INTERVAL;
Dean Nelsona47d5da2008-07-29 22:34:09 -0700334 add_timer(&part_sn2->dropped_notify_IRQ_timer);
335 xpc_part_deref(part);
336 }
337}
338
339/*
Dean Nelson7fb5e592008-07-29 22:34:10 -0700340 * Send a notify IRQ to the remote partition that is associated with the
Dean Nelson33ba3c72008-07-29 22:34:07 -0700341 * specified channel.
342 */
343static void
Dean Nelson7fb5e592008-07-29 22:34:10 -0700344xpc_send_notify_IRQ_sn2(struct xpc_channel *ch, u8 chctl_flag,
345 char *chctl_flag_string, unsigned long *irq_flags)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700346{
347 struct xpc_partition *part = &xpc_partitions[ch->partid];
Dean Nelsona47d5da2008-07-29 22:34:09 -0700348 struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
Dean Nelson7fb5e592008-07-29 22:34:10 -0700349 union xpc_channel_ctl_flags chctl = { 0 };
Dean Nelson33ba3c72008-07-29 22:34:07 -0700350 enum xp_retval ret;
351
Dean Nelson83469b52008-07-29 22:34:18 -0700352 if (likely(part->act_state != XPC_P_AS_DEACTIVATING)) {
Dean Nelson7fb5e592008-07-29 22:34:10 -0700353 chctl.flags[ch->number] = chctl_flag;
354 ret = xpc_send_IRQ_sn2(part_sn2->remote_chctl_amo_va,
355 chctl.all_flags,
356 part_sn2->notify_IRQ_nasid,
357 part_sn2->notify_IRQ_phys_cpuid,
Dean Nelson33ba3c72008-07-29 22:34:07 -0700358 SGI_XPC_NOTIFY);
359 dev_dbg(xpc_chan, "%s sent to partid=%d, channel=%d, ret=%d\n",
Dean Nelson7fb5e592008-07-29 22:34:10 -0700360 chctl_flag_string, ch->partid, ch->number, ret);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700361 if (unlikely(ret != xpSuccess)) {
362 if (irq_flags != NULL)
363 spin_unlock_irqrestore(&ch->lock, *irq_flags);
364 XPC_DEACTIVATE_PARTITION(part, ret);
365 if (irq_flags != NULL)
366 spin_lock_irqsave(&ch->lock, *irq_flags);
367 }
368 }
369}
370
Dean Nelson7fb5e592008-07-29 22:34:10 -0700371#define XPC_SEND_NOTIFY_IRQ_SN2(_ch, _ipi_f, _irq_f) \
372 xpc_send_notify_IRQ_sn2(_ch, _ipi_f, #_ipi_f, _irq_f)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700373
374/*
375 * Make it look like the remote partition, which is associated with the
Dean Nelson7fb5e592008-07-29 22:34:10 -0700376 * specified channel, sent us a notify IRQ. This faked IRQ will be handled
377 * by xpc_check_for_dropped_notify_IRQ_sn2().
Dean Nelson33ba3c72008-07-29 22:34:07 -0700378 */
379static void
Dean Nelson7fb5e592008-07-29 22:34:10 -0700380xpc_send_local_notify_IRQ_sn2(struct xpc_channel *ch, u8 chctl_flag,
381 char *chctl_flag_string)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700382{
383 struct xpc_partition *part = &xpc_partitions[ch->partid];
Dean Nelson7fb5e592008-07-29 22:34:10 -0700384 union xpc_channel_ctl_flags chctl = { 0 };
Dean Nelson33ba3c72008-07-29 22:34:07 -0700385
Dean Nelson7fb5e592008-07-29 22:34:10 -0700386 chctl.flags[ch->number] = chctl_flag;
387 FETCHOP_STORE_OP(TO_AMO((u64)&part->sn.sn2.local_chctl_amo_va->
388 variable), FETCHOP_OR, chctl.all_flags);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700389 dev_dbg(xpc_chan, "%s sent local from partid=%d, channel=%d\n",
Dean Nelson7fb5e592008-07-29 22:34:10 -0700390 chctl_flag_string, ch->partid, ch->number);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700391}
392
Dean Nelson7fb5e592008-07-29 22:34:10 -0700393#define XPC_SEND_LOCAL_NOTIFY_IRQ_SN2(_ch, _ipi_f) \
394 xpc_send_local_notify_IRQ_sn2(_ch, _ipi_f, #_ipi_f)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700395
396static void
Dean Nelson7fb5e592008-07-29 22:34:10 -0700397xpc_send_chctl_closerequest_sn2(struct xpc_channel *ch,
398 unsigned long *irq_flags)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700399{
Dean Nelson5b8669d2008-07-29 22:34:18 -0700400 struct xpc_openclose_args *args = ch->sn.sn2.local_openclose_args;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700401
402 args->reason = ch->reason;
Dean Nelson7fb5e592008-07-29 22:34:10 -0700403 XPC_SEND_NOTIFY_IRQ_SN2(ch, XPC_CHCTL_CLOSEREQUEST, irq_flags);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700404}
405
406static void
Dean Nelson7fb5e592008-07-29 22:34:10 -0700407xpc_send_chctl_closereply_sn2(struct xpc_channel *ch, unsigned long *irq_flags)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700408{
Dean Nelson7fb5e592008-07-29 22:34:10 -0700409 XPC_SEND_NOTIFY_IRQ_SN2(ch, XPC_CHCTL_CLOSEREPLY, irq_flags);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700410}
411
412static void
Dean Nelson7fb5e592008-07-29 22:34:10 -0700413xpc_send_chctl_openrequest_sn2(struct xpc_channel *ch, unsigned long *irq_flags)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700414{
Dean Nelson5b8669d2008-07-29 22:34:18 -0700415 struct xpc_openclose_args *args = ch->sn.sn2.local_openclose_args;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700416
Dean Nelsonbd3e64c2008-07-29 22:34:19 -0700417 args->entry_size = ch->entry_size;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700418 args->local_nentries = ch->local_nentries;
Dean Nelson7fb5e592008-07-29 22:34:10 -0700419 XPC_SEND_NOTIFY_IRQ_SN2(ch, XPC_CHCTL_OPENREQUEST, irq_flags);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700420}
421
422static void
Dean Nelson7fb5e592008-07-29 22:34:10 -0700423xpc_send_chctl_openreply_sn2(struct xpc_channel *ch, unsigned long *irq_flags)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700424{
Dean Nelson5b8669d2008-07-29 22:34:18 -0700425 struct xpc_openclose_args *args = ch->sn.sn2.local_openclose_args;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700426
427 args->remote_nentries = ch->remote_nentries;
428 args->local_nentries = ch->local_nentries;
Dean Nelson5b8669d2008-07-29 22:34:18 -0700429 args->local_msgqueue_pa = xp_pa(ch->sn.sn2.local_msgqueue);
Dean Nelson7fb5e592008-07-29 22:34:10 -0700430 XPC_SEND_NOTIFY_IRQ_SN2(ch, XPC_CHCTL_OPENREPLY, irq_flags);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700431}
432
433static void
Dean Nelson7fb5e592008-07-29 22:34:10 -0700434xpc_send_chctl_msgrequest_sn2(struct xpc_channel *ch)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700435{
Dean Nelson7fb5e592008-07-29 22:34:10 -0700436 XPC_SEND_NOTIFY_IRQ_SN2(ch, XPC_CHCTL_MSGREQUEST, NULL);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700437}
438
439static void
Dean Nelson7fb5e592008-07-29 22:34:10 -0700440xpc_send_chctl_local_msgrequest_sn2(struct xpc_channel *ch)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700441{
Dean Nelson7fb5e592008-07-29 22:34:10 -0700442 XPC_SEND_LOCAL_NOTIFY_IRQ_SN2(ch, XPC_CHCTL_MSGREQUEST);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700443}
444
Jack Steiner6f2584f2009-04-02 16:59:10 -0700445static enum xp_retval
Dean Nelson5b8669d2008-07-29 22:34:18 -0700446xpc_save_remote_msgqueue_pa_sn2(struct xpc_channel *ch,
447 unsigned long msgqueue_pa)
448{
449 ch->sn.sn2.remote_msgqueue_pa = msgqueue_pa;
Jack Steiner6f2584f2009-04-02 16:59:10 -0700450 return xpSuccess;
Dean Nelson5b8669d2008-07-29 22:34:18 -0700451}
452
Dean Nelson33ba3c72008-07-29 22:34:07 -0700453/*
454 * This next set of functions are used to keep track of when a partition is
455 * potentially engaged in accessing memory belonging to another partition.
456 */
457
458static void
Dean Nelsona47d5da2008-07-29 22:34:09 -0700459xpc_indicate_partition_engaged_sn2(struct xpc_partition *part)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700460{
461 unsigned long irq_flags;
Dean Nelsonc39838c2008-07-29 22:34:11 -0700462 struct amo *amo = (struct amo *)__va(part->sn.sn2.remote_amos_page_pa +
Dean Nelsonee6665e2008-07-29 22:34:13 -0700463 (XPC_ENGAGED_PARTITIONS_AMO_SN2 *
Dean Nelsonc39838c2008-07-29 22:34:11 -0700464 sizeof(struct amo)));
Dean Nelson33ba3c72008-07-29 22:34:07 -0700465
466 local_irq_save(irq_flags);
467
Dean Nelsonc39838c2008-07-29 22:34:11 -0700468 /* set bit corresponding to our partid in remote partition's amo */
Dean Nelson33ba3c72008-07-29 22:34:07 -0700469 FETCHOP_STORE_OP(TO_AMO((u64)&amo->variable), FETCHOP_OR,
Dean Nelson04de7412008-07-29 22:34:14 -0700470 BIT(sn_partition_id));
471
Dean Nelson33ba3c72008-07-29 22:34:07 -0700472 /*
473 * We must always use the nofault function regardless of whether we
474 * are on a Shub 1.1 system or a Shub 1.2 slice 0xc processor. If we
475 * didn't, we'd never know that the other partition is down and would
Dean Nelsonc39838c2008-07-29 22:34:11 -0700476 * keep sending IRQs and amos to it until the heartbeat times out.
Dean Nelson33ba3c72008-07-29 22:34:07 -0700477 */
478 (void)xp_nofault_PIOR((u64 *)GLOBAL_MMR_ADDR(NASID_GET(&amo->
479 variable),
480 xp_nofault_PIOR_target));
481
482 local_irq_restore(irq_flags);
483}
484
485static void
Dean Nelsona47d5da2008-07-29 22:34:09 -0700486xpc_indicate_partition_disengaged_sn2(struct xpc_partition *part)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700487{
Dean Nelsona47d5da2008-07-29 22:34:09 -0700488 struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700489 unsigned long irq_flags;
Dean Nelsonc39838c2008-07-29 22:34:11 -0700490 struct amo *amo = (struct amo *)__va(part_sn2->remote_amos_page_pa +
Dean Nelsonee6665e2008-07-29 22:34:13 -0700491 (XPC_ENGAGED_PARTITIONS_AMO_SN2 *
Dean Nelsonc39838c2008-07-29 22:34:11 -0700492 sizeof(struct amo)));
Dean Nelson33ba3c72008-07-29 22:34:07 -0700493
494 local_irq_save(irq_flags);
495
Dean Nelsonc39838c2008-07-29 22:34:11 -0700496 /* clear bit corresponding to our partid in remote partition's amo */
Dean Nelson33ba3c72008-07-29 22:34:07 -0700497 FETCHOP_STORE_OP(TO_AMO((u64)&amo->variable), FETCHOP_AND,
Dean Nelson04de7412008-07-29 22:34:14 -0700498 ~BIT(sn_partition_id));
499
Dean Nelson33ba3c72008-07-29 22:34:07 -0700500 /*
501 * We must always use the nofault function regardless of whether we
502 * are on a Shub 1.1 system or a Shub 1.2 slice 0xc processor. If we
503 * didn't, we'd never know that the other partition is down and would
Dean Nelsonc39838c2008-07-29 22:34:11 -0700504 * keep sending IRQs and amos to it until the heartbeat times out.
Dean Nelson33ba3c72008-07-29 22:34:07 -0700505 */
506 (void)xp_nofault_PIOR((u64 *)GLOBAL_MMR_ADDR(NASID_GET(&amo->
507 variable),
508 xp_nofault_PIOR_target));
509
510 local_irq_restore(irq_flags);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700511
Dean Nelson33ba3c72008-07-29 22:34:07 -0700512 /*
Dean Nelsona47d5da2008-07-29 22:34:09 -0700513 * Send activate IRQ to get other side to see that we've cleared our
Dean Nelsonc39838c2008-07-29 22:34:11 -0700514 * bit in their engaged partitions amo.
Dean Nelson33ba3c72008-07-29 22:34:07 -0700515 */
Dean Nelson7fb5e592008-07-29 22:34:10 -0700516 xpc_send_activate_IRQ_sn2(part_sn2->remote_amos_page_pa,
Dean Nelsona47d5da2008-07-29 22:34:09 -0700517 cnodeid_to_nasid(0),
518 part_sn2->activate_IRQ_nasid,
519 part_sn2->activate_IRQ_phys_cpuid);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700520}
521
Dean Nelson5b8669d2008-07-29 22:34:18 -0700522static void
523xpc_assume_partition_disengaged_sn2(short partid)
524{
525 struct amo *amo = xpc_vars_sn2->amos_page +
526 XPC_ENGAGED_PARTITIONS_AMO_SN2;
527
528 /* clear bit(s) based on partid mask in our partition's amo */
529 FETCHOP_STORE_OP(TO_AMO((u64)&amo->variable), FETCHOP_AND,
530 ~BIT(partid));
531}
532
Dean Nelsona47d5da2008-07-29 22:34:09 -0700533static int
534xpc_partition_engaged_sn2(short partid)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700535{
Dean Nelson8e85c232008-07-29 22:34:13 -0700536 struct amo *amo = xpc_vars_sn2->amos_page +
537 XPC_ENGAGED_PARTITIONS_AMO_SN2;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700538
Dean Nelsonc39838c2008-07-29 22:34:11 -0700539 /* our partition's amo variable ANDed with partid mask */
Dean Nelson33ba3c72008-07-29 22:34:07 -0700540 return (FETCHOP_LOAD_OP(TO_AMO((u64)&amo->variable), FETCHOP_LOAD) &
Dean Nelson04de7412008-07-29 22:34:14 -0700541 BIT(partid)) != 0;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700542}
543
Dean Nelsona47d5da2008-07-29 22:34:09 -0700544static int
545xpc_any_partition_engaged_sn2(void)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700546{
Dean Nelson8e85c232008-07-29 22:34:13 -0700547 struct amo *amo = xpc_vars_sn2->amos_page +
548 XPC_ENGAGED_PARTITIONS_AMO_SN2;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700549
Dean Nelsonc39838c2008-07-29 22:34:11 -0700550 /* our partition's amo variable */
Dean Nelsona47d5da2008-07-29 22:34:09 -0700551 return FETCHOP_LOAD_OP(TO_AMO((u64)&amo->variable), FETCHOP_LOAD) != 0;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700552}
553
Dean Nelson6e41017a2008-07-29 22:34:09 -0700554/* original protection values for each node */
555static u64 xpc_prot_vec_sn2[MAX_NUMNODES];
556
557/*
Dean Nelsonc39838c2008-07-29 22:34:11 -0700558 * Change protections to allow amo operations on non-Shub 1.1 systems.
Dean Nelson6e41017a2008-07-29 22:34:09 -0700559 */
560static enum xp_retval
Dean Nelsonc39838c2008-07-29 22:34:11 -0700561xpc_allow_amo_ops_sn2(struct amo *amos_page)
Dean Nelson6e41017a2008-07-29 22:34:09 -0700562{
Dean Nelson6c1c3252008-11-05 17:27:22 -0600563 enum xp_retval ret = xpSuccess;
Dean Nelson6e41017a2008-07-29 22:34:09 -0700564
565 /*
566 * On SHUB 1.1, we cannot call sn_change_memprotect() since the BIST
567 * collides with memory operations. On those systems we call
Dean Nelsonc39838c2008-07-29 22:34:11 -0700568 * xpc_allow_amo_ops_shub_wars_1_1_sn2() instead.
Dean Nelson6e41017a2008-07-29 22:34:09 -0700569 */
Dean Nelson6c1c3252008-11-05 17:27:22 -0600570 if (!enable_shub_wars_1_1())
571 ret = xp_expand_memprotect(ia64_tpa((u64)amos_page), PAGE_SIZE);
572
573 return ret;
Dean Nelson6e41017a2008-07-29 22:34:09 -0700574}
575
576/*
Dean Nelsonc39838c2008-07-29 22:34:11 -0700577 * Change protections to allow amo operations on Shub 1.1 systems.
Dean Nelson6e41017a2008-07-29 22:34:09 -0700578 */
579static void
Dean Nelsonc39838c2008-07-29 22:34:11 -0700580xpc_allow_amo_ops_shub_wars_1_1_sn2(void)
Dean Nelson6e41017a2008-07-29 22:34:09 -0700581{
582 int node;
583 int nasid;
584
585 if (!enable_shub_wars_1_1())
586 return;
587
588 for_each_online_node(node) {
589 nasid = cnodeid_to_nasid(node);
590 /* save current protection values */
591 xpc_prot_vec_sn2[node] =
592 (u64)HUB_L((u64 *)GLOBAL_MMR_ADDR(nasid,
593 SH1_MD_DQLP_MMR_DIR_PRIVEC0));
594 /* open up everything */
595 HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid,
596 SH1_MD_DQLP_MMR_DIR_PRIVEC0),
597 -1UL);
598 HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid,
599 SH1_MD_DQRP_MMR_DIR_PRIVEC0),
600 -1UL);
601 }
602}
603
Dean Nelson94bd2702008-07-29 22:34:05 -0700604static enum xp_retval
Dean Nelsona812dcc2008-07-29 22:34:16 -0700605xpc_get_partition_rsvd_page_pa_sn2(void *buf, u64 *cookie, unsigned long *rp_pa,
Dean Nelson261f3b42008-07-29 22:34:16 -0700606 size_t *len)
607{
608 s64 status;
609 enum xp_retval ret;
610
Dean Nelsona812dcc2008-07-29 22:34:16 -0700611 status = sn_partition_reserved_page_pa((u64)buf, cookie, rp_pa, len);
Dean Nelson261f3b42008-07-29 22:34:16 -0700612 if (status == SALRET_OK)
613 ret = xpSuccess;
614 else if (status == SALRET_MORE_PASSES)
615 ret = xpNeedMoreInfo;
616 else
617 ret = xpSalError;
618
619 return ret;
620}
621
622
Dean Nelson5b8669d2008-07-29 22:34:18 -0700623static int
624xpc_setup_rsvd_page_sn_sn2(struct xpc_rsvd_page *rp)
Dean Nelson94bd2702008-07-29 22:34:05 -0700625{
Dean Nelsonc39838c2008-07-29 22:34:11 -0700626 struct amo *amos_page;
Dean Nelson94bd2702008-07-29 22:34:05 -0700627 int i;
628 int ret;
629
Dean Nelson8e85c232008-07-29 22:34:13 -0700630 xpc_vars_sn2 = XPC_RP_VARS(rp);
Dean Nelson94bd2702008-07-29 22:34:05 -0700631
Dean Nelsona812dcc2008-07-29 22:34:16 -0700632 rp->sn.vars_pa = xp_pa(xpc_vars_sn2);
Dean Nelson94bd2702008-07-29 22:34:05 -0700633
Dean Nelsone17d4162008-07-29 22:34:06 -0700634 /* vars_part array follows immediately after vars */
Dean Nelson8e85c232008-07-29 22:34:13 -0700635 xpc_vars_part_sn2 = (struct xpc_vars_part_sn2 *)((u8 *)XPC_RP_VARS(rp) +
636 XPC_RP_VARS_SIZE);
Dean Nelsone17d4162008-07-29 22:34:06 -0700637
Dean Nelson94bd2702008-07-29 22:34:05 -0700638 /*
Dean Nelson8e85c232008-07-29 22:34:13 -0700639 * Before clearing xpc_vars_sn2, see if a page of amos had been
640 * previously allocated. If not we'll need to allocate one and set
641 * permissions so that cross-partition amos are allowed.
Dean Nelson94bd2702008-07-29 22:34:05 -0700642 *
Dean Nelsonc39838c2008-07-29 22:34:11 -0700643 * The allocated amo page needs MCA reporting to remain disabled after
Dean Nelson94bd2702008-07-29 22:34:05 -0700644 * XPC has unloaded. To make this work, we keep a copy of the pointer
Dean Nelson8e85c232008-07-29 22:34:13 -0700645 * to this page (i.e., amos_page) in the struct xpc_vars_sn2 structure,
Dean Nelson94bd2702008-07-29 22:34:05 -0700646 * which is pointed to by the reserved page, and re-use that saved copy
Dean Nelsonc39838c2008-07-29 22:34:11 -0700647 * on subsequent loads of XPC. This amo page is never freed, and its
Dean Nelson94bd2702008-07-29 22:34:05 -0700648 * memory protections are never restricted.
649 */
Dean Nelson8e85c232008-07-29 22:34:13 -0700650 amos_page = xpc_vars_sn2->amos_page;
Dean Nelson94bd2702008-07-29 22:34:05 -0700651 if (amos_page == NULL) {
Dean Nelsonc39838c2008-07-29 22:34:11 -0700652 amos_page = (struct amo *)TO_AMO(uncached_alloc_page(0, 1));
Dean Nelson94bd2702008-07-29 22:34:05 -0700653 if (amos_page == NULL) {
Dean Nelsonc39838c2008-07-29 22:34:11 -0700654 dev_err(xpc_part, "can't allocate page of amos\n");
Dean Nelson5b8669d2008-07-29 22:34:18 -0700655 return -ENOMEM;
Dean Nelson94bd2702008-07-29 22:34:05 -0700656 }
657
658 /*
Dean Nelsonc39838c2008-07-29 22:34:11 -0700659 * Open up amo-R/W to cpu. This is done on Shub 1.1 systems
660 * when xpc_allow_amo_ops_shub_wars_1_1_sn2() is called.
Dean Nelson94bd2702008-07-29 22:34:05 -0700661 */
Dean Nelsonc39838c2008-07-29 22:34:11 -0700662 ret = xpc_allow_amo_ops_sn2(amos_page);
Dean Nelson6e41017a2008-07-29 22:34:09 -0700663 if (ret != xpSuccess) {
Dean Nelsonc39838c2008-07-29 22:34:11 -0700664 dev_err(xpc_part, "can't allow amo operations\n");
Dean Nelson6e41017a2008-07-29 22:34:09 -0700665 uncached_free_page(__IA64_UNCACHED_OFFSET |
666 TO_PHYS((u64)amos_page), 1);
Dean Nelson5b8669d2008-07-29 22:34:18 -0700667 return -EPERM;
Dean Nelson94bd2702008-07-29 22:34:05 -0700668 }
669 }
670
Dean Nelson8e85c232008-07-29 22:34:13 -0700671 /* clear xpc_vars_sn2 */
672 memset(xpc_vars_sn2, 0, sizeof(struct xpc_vars_sn2));
Dean Nelson94bd2702008-07-29 22:34:05 -0700673
Dean Nelson8e85c232008-07-29 22:34:13 -0700674 xpc_vars_sn2->version = XPC_V_VERSION;
675 xpc_vars_sn2->activate_IRQ_nasid = cpuid_to_nasid(0);
676 xpc_vars_sn2->activate_IRQ_phys_cpuid = cpu_physical_id(0);
Dean Nelsona812dcc2008-07-29 22:34:16 -0700677 xpc_vars_sn2->vars_part_pa = xp_pa(xpc_vars_part_sn2);
Dean Nelson8e85c232008-07-29 22:34:13 -0700678 xpc_vars_sn2->amos_page_pa = ia64_tpa((u64)amos_page);
679 xpc_vars_sn2->amos_page = amos_page; /* save for next load of XPC */
Dean Nelson94bd2702008-07-29 22:34:05 -0700680
Dean Nelson8e85c232008-07-29 22:34:13 -0700681 /* clear xpc_vars_part_sn2 */
682 memset((u64 *)xpc_vars_part_sn2, 0, sizeof(struct xpc_vars_part_sn2) *
Dean Nelson261f3b42008-07-29 22:34:16 -0700683 XP_MAX_NPARTITIONS_SN2);
Dean Nelson94bd2702008-07-29 22:34:05 -0700684
Dean Nelsonc39838c2008-07-29 22:34:11 -0700685 /* initialize the activate IRQ related amo variables */
Dean Nelson04de7412008-07-29 22:34:14 -0700686 for (i = 0; i < xpc_nasid_mask_nlongs; i++)
Dean Nelsonee6665e2008-07-29 22:34:13 -0700687 (void)xpc_init_IRQ_amo_sn2(XPC_ACTIVATE_IRQ_AMOS_SN2 + i);
Dean Nelson94bd2702008-07-29 22:34:05 -0700688
Dean Nelsonc39838c2008-07-29 22:34:11 -0700689 /* initialize the engaged remote partitions related amo variables */
Dean Nelsonee6665e2008-07-29 22:34:13 -0700690 (void)xpc_init_IRQ_amo_sn2(XPC_ENGAGED_PARTITIONS_AMO_SN2);
691 (void)xpc_init_IRQ_amo_sn2(XPC_DEACTIVATE_REQUEST_AMO_SN2);
Dean Nelson94bd2702008-07-29 22:34:05 -0700692
Dean Nelson5b8669d2008-07-29 22:34:18 -0700693 return 0;
Dean Nelson94bd2702008-07-29 22:34:05 -0700694}
695
Dean Nelson33ba3c72008-07-29 22:34:07 -0700696static void
697xpc_increment_heartbeat_sn2(void)
698{
Dean Nelson8e85c232008-07-29 22:34:13 -0700699 xpc_vars_sn2->heartbeat++;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700700}
701
702static void
703xpc_offline_heartbeat_sn2(void)
704{
705 xpc_increment_heartbeat_sn2();
Dean Nelson8e85c232008-07-29 22:34:13 -0700706 xpc_vars_sn2->heartbeat_offline = 1;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700707}
708
709static void
710xpc_online_heartbeat_sn2(void)
711{
712 xpc_increment_heartbeat_sn2();
Dean Nelson8e85c232008-07-29 22:34:13 -0700713 xpc_vars_sn2->heartbeat_offline = 0;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700714}
715
716static void
717xpc_heartbeat_init_sn2(void)
718{
Dean Nelson8e85c232008-07-29 22:34:13 -0700719 DBUG_ON(xpc_vars_sn2 == NULL);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700720
Dean Nelson8e85c232008-07-29 22:34:13 -0700721 bitmap_zero(xpc_vars_sn2->heartbeating_to_mask, XP_MAX_NPARTITIONS_SN2);
722 xpc_heartbeating_to_mask = &xpc_vars_sn2->heartbeating_to_mask[0];
Dean Nelson33ba3c72008-07-29 22:34:07 -0700723 xpc_online_heartbeat_sn2();
724}
725
726static void
727xpc_heartbeat_exit_sn2(void)
728{
729 xpc_offline_heartbeat_sn2();
730}
731
Dean Nelson61deb862008-07-29 22:34:17 -0700732static enum xp_retval
733xpc_get_remote_heartbeat_sn2(struct xpc_partition *part)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700734{
735 struct xpc_vars_sn2 *remote_vars;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700736 enum xp_retval ret;
737
Dean Nelsonee6665e2008-07-29 22:34:13 -0700738 remote_vars = (struct xpc_vars_sn2 *)xpc_remote_copy_buffer_sn2;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700739
Dean Nelson61deb862008-07-29 22:34:17 -0700740 /* pull the remote vars structure that contains the heartbeat */
741 ret = xp_remote_memcpy(xp_pa(remote_vars),
742 part->sn.sn2.remote_vars_pa,
743 XPC_RP_VARS_SIZE);
744 if (ret != xpSuccess)
745 return ret;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700746
Dean Nelson61deb862008-07-29 22:34:17 -0700747 dev_dbg(xpc_part, "partid=%d, heartbeat=%ld, last_heartbeat=%ld, "
748 "heartbeat_offline=%ld, HB_mask[0]=0x%lx\n", XPC_PARTID(part),
749 remote_vars->heartbeat, part->last_heartbeat,
750 remote_vars->heartbeat_offline,
751 remote_vars->heartbeating_to_mask[0]);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700752
Dean Nelson61deb862008-07-29 22:34:17 -0700753 if ((remote_vars->heartbeat == part->last_heartbeat &&
754 remote_vars->heartbeat_offline == 0) ||
755 !xpc_hb_allowed(sn_partition_id,
756 &remote_vars->heartbeating_to_mask)) {
757 ret = xpNoHeartbeat;
758 } else {
Dean Nelson33ba3c72008-07-29 22:34:07 -0700759 part->last_heartbeat = remote_vars->heartbeat;
760 }
Dean Nelson61deb862008-07-29 22:34:17 -0700761
762 return ret;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700763}
764
765/*
766 * Get a copy of the remote partition's XPC variables from the reserved page.
767 *
768 * remote_vars points to a buffer that is cacheline aligned for BTE copies and
769 * assumed to be of size XPC_RP_VARS_SIZE.
770 */
771static enum xp_retval
Dean Nelsona812dcc2008-07-29 22:34:16 -0700772xpc_get_remote_vars_sn2(unsigned long remote_vars_pa,
773 struct xpc_vars_sn2 *remote_vars)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700774{
775 enum xp_retval ret;
776
777 if (remote_vars_pa == 0)
778 return xpVarsNotSet;
779
780 /* pull over the cross partition variables */
Dean Nelsona812dcc2008-07-29 22:34:16 -0700781 ret = xp_remote_memcpy(xp_pa(remote_vars), remote_vars_pa,
Dean Nelson33ba3c72008-07-29 22:34:07 -0700782 XPC_RP_VARS_SIZE);
783 if (ret != xpSuccess)
784 return ret;
785
786 if (XPC_VERSION_MAJOR(remote_vars->version) !=
787 XPC_VERSION_MAJOR(XPC_V_VERSION)) {
788 return xpBadVersion;
789 }
790
791 return xpSuccess;
792}
793
794static void
Dean Nelsona47d5da2008-07-29 22:34:09 -0700795xpc_request_partition_activation_sn2(struct xpc_rsvd_page *remote_rp,
Dean Nelsona812dcc2008-07-29 22:34:16 -0700796 unsigned long remote_rp_pa, int nasid)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700797{
Dean Nelson7fb5e592008-07-29 22:34:10 -0700798 xpc_send_local_activate_IRQ_sn2(nasid);
Dean Nelsona47d5da2008-07-29 22:34:09 -0700799}
800
801static void
802xpc_request_partition_reactivation_sn2(struct xpc_partition *part)
803{
Dean Nelson7fb5e592008-07-29 22:34:10 -0700804 xpc_send_local_activate_IRQ_sn2(part->sn.sn2.activate_IRQ_nasid);
Dean Nelsona47d5da2008-07-29 22:34:09 -0700805}
806
807static void
808xpc_request_partition_deactivation_sn2(struct xpc_partition *part)
809{
810 struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
811 unsigned long irq_flags;
Dean Nelsonc39838c2008-07-29 22:34:11 -0700812 struct amo *amo = (struct amo *)__va(part_sn2->remote_amos_page_pa +
Dean Nelsonee6665e2008-07-29 22:34:13 -0700813 (XPC_DEACTIVATE_REQUEST_AMO_SN2 *
Dean Nelsonc39838c2008-07-29 22:34:11 -0700814 sizeof(struct amo)));
Dean Nelsona47d5da2008-07-29 22:34:09 -0700815
816 local_irq_save(irq_flags);
817
Dean Nelsonc39838c2008-07-29 22:34:11 -0700818 /* set bit corresponding to our partid in remote partition's amo */
Dean Nelsona47d5da2008-07-29 22:34:09 -0700819 FETCHOP_STORE_OP(TO_AMO((u64)&amo->variable), FETCHOP_OR,
Dean Nelson04de7412008-07-29 22:34:14 -0700820 BIT(sn_partition_id));
821
Dean Nelsona47d5da2008-07-29 22:34:09 -0700822 /*
823 * We must always use the nofault function regardless of whether we
824 * are on a Shub 1.1 system or a Shub 1.2 slice 0xc processor. If we
825 * didn't, we'd never know that the other partition is down and would
Dean Nelsonc39838c2008-07-29 22:34:11 -0700826 * keep sending IRQs and amos to it until the heartbeat times out.
Dean Nelsona47d5da2008-07-29 22:34:09 -0700827 */
828 (void)xp_nofault_PIOR((u64 *)GLOBAL_MMR_ADDR(NASID_GET(&amo->
829 variable),
830 xp_nofault_PIOR_target));
831
832 local_irq_restore(irq_flags);
833
834 /*
835 * Send activate IRQ to get other side to see that we've set our
Dean Nelsonc39838c2008-07-29 22:34:11 -0700836 * bit in their deactivate request amo.
Dean Nelsona47d5da2008-07-29 22:34:09 -0700837 */
Dean Nelson7fb5e592008-07-29 22:34:10 -0700838 xpc_send_activate_IRQ_sn2(part_sn2->remote_amos_page_pa,
Dean Nelsona47d5da2008-07-29 22:34:09 -0700839 cnodeid_to_nasid(0),
840 part_sn2->activate_IRQ_nasid,
841 part_sn2->activate_IRQ_phys_cpuid);
842}
843
844static void
845xpc_cancel_partition_deactivation_request_sn2(struct xpc_partition *part)
846{
847 unsigned long irq_flags;
Dean Nelsonc39838c2008-07-29 22:34:11 -0700848 struct amo *amo = (struct amo *)__va(part->sn.sn2.remote_amos_page_pa +
Dean Nelsonee6665e2008-07-29 22:34:13 -0700849 (XPC_DEACTIVATE_REQUEST_AMO_SN2 *
Dean Nelsonc39838c2008-07-29 22:34:11 -0700850 sizeof(struct amo)));
Dean Nelsona47d5da2008-07-29 22:34:09 -0700851
852 local_irq_save(irq_flags);
853
Dean Nelsonc39838c2008-07-29 22:34:11 -0700854 /* clear bit corresponding to our partid in remote partition's amo */
Dean Nelsona47d5da2008-07-29 22:34:09 -0700855 FETCHOP_STORE_OP(TO_AMO((u64)&amo->variable), FETCHOP_AND,
Dean Nelson04de7412008-07-29 22:34:14 -0700856 ~BIT(sn_partition_id));
857
Dean Nelsona47d5da2008-07-29 22:34:09 -0700858 /*
859 * We must always use the nofault function regardless of whether we
860 * are on a Shub 1.1 system or a Shub 1.2 slice 0xc processor. If we
861 * didn't, we'd never know that the other partition is down and would
Dean Nelsonc39838c2008-07-29 22:34:11 -0700862 * keep sending IRQs and amos to it until the heartbeat times out.
Dean Nelsona47d5da2008-07-29 22:34:09 -0700863 */
864 (void)xp_nofault_PIOR((u64 *)GLOBAL_MMR_ADDR(NASID_GET(&amo->
865 variable),
866 xp_nofault_PIOR_target));
867
868 local_irq_restore(irq_flags);
869}
870
871static int
872xpc_partition_deactivation_requested_sn2(short partid)
873{
Dean Nelson8e85c232008-07-29 22:34:13 -0700874 struct amo *amo = xpc_vars_sn2->amos_page +
875 XPC_DEACTIVATE_REQUEST_AMO_SN2;
Dean Nelsona47d5da2008-07-29 22:34:09 -0700876
Dean Nelsonc39838c2008-07-29 22:34:11 -0700877 /* our partition's amo variable ANDed with partid mask */
Dean Nelsona47d5da2008-07-29 22:34:09 -0700878 return (FETCHOP_LOAD_OP(TO_AMO((u64)&amo->variable), FETCHOP_LOAD) &
Dean Nelson04de7412008-07-29 22:34:14 -0700879 BIT(partid)) != 0;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700880}
881
882/*
883 * Update the remote partition's info.
884 */
885static void
886xpc_update_partition_info_sn2(struct xpc_partition *part, u8 remote_rp_version,
Dean Nelson81fe7882008-07-29 22:34:15 -0700887 unsigned long *remote_rp_ts_jiffies,
Dean Nelsona812dcc2008-07-29 22:34:16 -0700888 unsigned long remote_rp_pa,
889 unsigned long remote_vars_pa,
Dean Nelson33ba3c72008-07-29 22:34:07 -0700890 struct xpc_vars_sn2 *remote_vars)
891{
Dean Nelsona47d5da2008-07-29 22:34:09 -0700892 struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
893
Dean Nelson33ba3c72008-07-29 22:34:07 -0700894 part->remote_rp_version = remote_rp_version;
895 dev_dbg(xpc_part, " remote_rp_version = 0x%016x\n",
896 part->remote_rp_version);
897
Dean Nelson81fe7882008-07-29 22:34:15 -0700898 part->remote_rp_ts_jiffies = *remote_rp_ts_jiffies;
899 dev_dbg(xpc_part, " remote_rp_ts_jiffies = 0x%016lx\n",
900 part->remote_rp_ts_jiffies);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700901
902 part->remote_rp_pa = remote_rp_pa;
903 dev_dbg(xpc_part, " remote_rp_pa = 0x%016lx\n", part->remote_rp_pa);
904
Dean Nelsona47d5da2008-07-29 22:34:09 -0700905 part_sn2->remote_vars_pa = remote_vars_pa;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700906 dev_dbg(xpc_part, " remote_vars_pa = 0x%016lx\n",
Dean Nelsona47d5da2008-07-29 22:34:09 -0700907 part_sn2->remote_vars_pa);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700908
Dean Nelson158bc692009-01-15 13:50:57 -0800909 part->last_heartbeat = remote_vars->heartbeat - 1;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700910 dev_dbg(xpc_part, " last_heartbeat = 0x%016lx\n",
911 part->last_heartbeat);
912
Dean Nelsona47d5da2008-07-29 22:34:09 -0700913 part_sn2->remote_vars_part_pa = remote_vars->vars_part_pa;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700914 dev_dbg(xpc_part, " remote_vars_part_pa = 0x%016lx\n",
Dean Nelsona47d5da2008-07-29 22:34:09 -0700915 part_sn2->remote_vars_part_pa);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700916
Dean Nelsona47d5da2008-07-29 22:34:09 -0700917 part_sn2->activate_IRQ_nasid = remote_vars->activate_IRQ_nasid;
918 dev_dbg(xpc_part, " activate_IRQ_nasid = 0x%x\n",
919 part_sn2->activate_IRQ_nasid);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700920
Dean Nelsona47d5da2008-07-29 22:34:09 -0700921 part_sn2->activate_IRQ_phys_cpuid =
922 remote_vars->activate_IRQ_phys_cpuid;
923 dev_dbg(xpc_part, " activate_IRQ_phys_cpuid = 0x%x\n",
924 part_sn2->activate_IRQ_phys_cpuid);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700925
Dean Nelsona47d5da2008-07-29 22:34:09 -0700926 part_sn2->remote_amos_page_pa = remote_vars->amos_page_pa;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700927 dev_dbg(xpc_part, " remote_amos_page_pa = 0x%lx\n",
Dean Nelsona47d5da2008-07-29 22:34:09 -0700928 part_sn2->remote_amos_page_pa);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700929
Dean Nelsona47d5da2008-07-29 22:34:09 -0700930 part_sn2->remote_vars_version = remote_vars->version;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700931 dev_dbg(xpc_part, " remote_vars_version = 0x%x\n",
Dean Nelsona47d5da2008-07-29 22:34:09 -0700932 part_sn2->remote_vars_version);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700933}
934
935/*
Dean Nelson7fb5e592008-07-29 22:34:10 -0700936 * Prior code has determined the nasid which generated a activate IRQ.
937 * Inspect that nasid to determine if its partition needs to be activated
938 * or deactivated.
Dean Nelson33ba3c72008-07-29 22:34:07 -0700939 *
Dean Nelson7fb5e592008-07-29 22:34:10 -0700940 * A partition is considered "awaiting activation" if our partition
Dean Nelson33ba3c72008-07-29 22:34:07 -0700941 * flags indicate it is not active and it has a heartbeat. A
942 * partition is considered "awaiting deactivation" if our partition
943 * flags indicate it is active but it has no heartbeat or it is not
944 * sending its heartbeat to us.
945 *
946 * To determine the heartbeat, the remote nasid must have a properly
947 * initialized reserved page.
948 */
949static void
Dean Nelson6e41017a2008-07-29 22:34:09 -0700950xpc_identify_activate_IRQ_req_sn2(int nasid)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700951{
952 struct xpc_rsvd_page *remote_rp;
953 struct xpc_vars_sn2 *remote_vars;
Dean Nelsona812dcc2008-07-29 22:34:16 -0700954 unsigned long remote_rp_pa;
955 unsigned long remote_vars_pa;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700956 int remote_rp_version;
957 int reactivate = 0;
Dean Nelson81fe7882008-07-29 22:34:15 -0700958 unsigned long remote_rp_ts_jiffies = 0;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700959 short partid;
960 struct xpc_partition *part;
Dean Nelsona47d5da2008-07-29 22:34:09 -0700961 struct xpc_partition_sn2 *part_sn2;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700962 enum xp_retval ret;
963
964 /* pull over the reserved page structure */
965
Dean Nelsonee6665e2008-07-29 22:34:13 -0700966 remote_rp = (struct xpc_rsvd_page *)xpc_remote_copy_buffer_sn2;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700967
968 ret = xpc_get_remote_rp(nasid, NULL, remote_rp, &remote_rp_pa);
969 if (ret != xpSuccess) {
970 dev_warn(xpc_part, "unable to get reserved page from nasid %d, "
971 "which sent interrupt, reason=%d\n", nasid, ret);
972 return;
973 }
974
975 remote_vars_pa = remote_rp->sn.vars_pa;
976 remote_rp_version = remote_rp->version;
Dean Nelson81fe7882008-07-29 22:34:15 -0700977 remote_rp_ts_jiffies = remote_rp->ts_jiffies;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700978
979 partid = remote_rp->SAL_partid;
980 part = &xpc_partitions[partid];
Dean Nelsona47d5da2008-07-29 22:34:09 -0700981 part_sn2 = &part->sn.sn2;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700982
983 /* pull over the cross partition variables */
984
Dean Nelsonee6665e2008-07-29 22:34:13 -0700985 remote_vars = (struct xpc_vars_sn2 *)xpc_remote_copy_buffer_sn2;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700986
987 ret = xpc_get_remote_vars_sn2(remote_vars_pa, remote_vars);
988 if (ret != xpSuccess) {
Dean Nelson33ba3c72008-07-29 22:34:07 -0700989 dev_warn(xpc_part, "unable to get XPC variables from nasid %d, "
990 "which sent interrupt, reason=%d\n", nasid, ret);
991
992 XPC_DEACTIVATE_PARTITION(part, ret);
993 return;
994 }
995
Dean Nelson6e41017a2008-07-29 22:34:09 -0700996 part->activate_IRQ_rcvd++;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700997
998 dev_dbg(xpc_part, "partid for nasid %d is %d; IRQs = %d; HB = "
Dean Nelson6e41017a2008-07-29 22:34:09 -0700999 "%ld:0x%lx\n", (int)nasid, (int)partid, part->activate_IRQ_rcvd,
Dean Nelson33ba3c72008-07-29 22:34:07 -07001000 remote_vars->heartbeat, remote_vars->heartbeating_to_mask[0]);
1001
1002 if (xpc_partition_disengaged(part) &&
Dean Nelson83469b52008-07-29 22:34:18 -07001003 part->act_state == XPC_P_AS_INACTIVE) {
Dean Nelson33ba3c72008-07-29 22:34:07 -07001004
1005 xpc_update_partition_info_sn2(part, remote_rp_version,
Dean Nelson81fe7882008-07-29 22:34:15 -07001006 &remote_rp_ts_jiffies,
1007 remote_rp_pa, remote_vars_pa,
1008 remote_vars);
Dean Nelson33ba3c72008-07-29 22:34:07 -07001009
Dean Nelsona47d5da2008-07-29 22:34:09 -07001010 if (xpc_partition_deactivation_requested_sn2(partid)) {
1011 /*
1012 * Other side is waiting on us to deactivate even though
1013 * we already have.
1014 */
1015 return;
Dean Nelson33ba3c72008-07-29 22:34:07 -07001016 }
1017
1018 xpc_activate_partition(part);
1019 return;
1020 }
1021
1022 DBUG_ON(part->remote_rp_version == 0);
Dean Nelsona47d5da2008-07-29 22:34:09 -07001023 DBUG_ON(part_sn2->remote_vars_version == 0);
Dean Nelson33ba3c72008-07-29 22:34:07 -07001024
Dean Nelson81fe7882008-07-29 22:34:15 -07001025 if (remote_rp_ts_jiffies != part->remote_rp_ts_jiffies) {
Dean Nelson33ba3c72008-07-29 22:34:07 -07001026
Dean Nelsona47d5da2008-07-29 22:34:09 -07001027 /* the other side rebooted */
Dean Nelson33ba3c72008-07-29 22:34:07 -07001028
Dean Nelsona47d5da2008-07-29 22:34:09 -07001029 DBUG_ON(xpc_partition_engaged_sn2(partid));
1030 DBUG_ON(xpc_partition_deactivation_requested_sn2(partid));
Dean Nelson33ba3c72008-07-29 22:34:07 -07001031
1032 xpc_update_partition_info_sn2(part, remote_rp_version,
Dean Nelson81fe7882008-07-29 22:34:15 -07001033 &remote_rp_ts_jiffies,
1034 remote_rp_pa, remote_vars_pa,
1035 remote_vars);
Dean Nelson33ba3c72008-07-29 22:34:07 -07001036 reactivate = 1;
Dean Nelson33ba3c72008-07-29 22:34:07 -07001037 }
1038
Dean Nelsona47d5da2008-07-29 22:34:09 -07001039 if (part->disengage_timeout > 0 && !xpc_partition_disengaged(part)) {
Dean Nelson33ba3c72008-07-29 22:34:07 -07001040 /* still waiting on other side to disengage from us */
1041 return;
1042 }
1043
Dean Nelsona47d5da2008-07-29 22:34:09 -07001044 if (reactivate)
Dean Nelson33ba3c72008-07-29 22:34:07 -07001045 XPC_DEACTIVATE_PARTITION(part, xpReactivating);
Dean Nelsona47d5da2008-07-29 22:34:09 -07001046 else if (xpc_partition_deactivation_requested_sn2(partid))
Dean Nelson33ba3c72008-07-29 22:34:07 -07001047 XPC_DEACTIVATE_PARTITION(part, xpOtherGoingDown);
Dean Nelson33ba3c72008-07-29 22:34:07 -07001048}
1049
1050/*
Dean Nelsonc39838c2008-07-29 22:34:11 -07001051 * Loop through the activation amo variables and process any bits
Dean Nelson33ba3c72008-07-29 22:34:07 -07001052 * which are set. Each bit indicates a nasid sending a partition
1053 * activation or deactivation request.
1054 *
1055 * Return #of IRQs detected.
1056 */
1057int
Dean Nelson6e41017a2008-07-29 22:34:09 -07001058xpc_identify_activate_IRQ_sender_sn2(void)
Dean Nelson33ba3c72008-07-29 22:34:07 -07001059{
Dean Nelson04de7412008-07-29 22:34:14 -07001060 int l;
1061 int b;
1062 unsigned long nasid_mask_long;
Dean Nelson33ba3c72008-07-29 22:34:07 -07001063 u64 nasid; /* remote nasid */
1064 int n_IRQs_detected = 0;
Dean Nelsonc39838c2008-07-29 22:34:11 -07001065 struct amo *act_amos;
Dean Nelson33ba3c72008-07-29 22:34:07 -07001066
Dean Nelson8e85c232008-07-29 22:34:13 -07001067 act_amos = xpc_vars_sn2->amos_page + XPC_ACTIVATE_IRQ_AMOS_SN2;
Dean Nelson33ba3c72008-07-29 22:34:07 -07001068
Dean Nelson04de7412008-07-29 22:34:14 -07001069 /* scan through activate amo variables looking for non-zero entries */
1070 for (l = 0; l < xpc_nasid_mask_nlongs; l++) {
Dean Nelson33ba3c72008-07-29 22:34:07 -07001071
1072 if (xpc_exiting)
1073 break;
1074
Dean Nelson04de7412008-07-29 22:34:14 -07001075 nasid_mask_long = xpc_receive_IRQ_amo_sn2(&act_amos[l]);
1076
1077 b = find_first_bit(&nasid_mask_long, BITS_PER_LONG);
1078 if (b >= BITS_PER_LONG) {
1079 /* no IRQs from nasids in this amo variable */
Dean Nelson33ba3c72008-07-29 22:34:07 -07001080 continue;
1081 }
1082
Dean Nelson04de7412008-07-29 22:34:14 -07001083 dev_dbg(xpc_part, "amo[%d] gave back 0x%lx\n", l,
1084 nasid_mask_long);
Dean Nelson33ba3c72008-07-29 22:34:07 -07001085
1086 /*
1087 * If this nasid has been added to the machine since
1088 * our partition was reset, this will retain the
1089 * remote nasid in our reserved pages machine mask.
1090 * This is used in the event of module reload.
1091 */
Dean Nelson04de7412008-07-29 22:34:14 -07001092 xpc_mach_nasids[l] |= nasid_mask_long;
Dean Nelson33ba3c72008-07-29 22:34:07 -07001093
1094 /* locate the nasid(s) which sent interrupts */
1095
Dean Nelson04de7412008-07-29 22:34:14 -07001096 do {
1097 n_IRQs_detected++;
1098 nasid = (l * BITS_PER_LONG + b) * 2;
1099 dev_dbg(xpc_part, "interrupt from nasid %ld\n", nasid);
1100 xpc_identify_activate_IRQ_req_sn2(nasid);
1101
1102 b = find_next_bit(&nasid_mask_long, BITS_PER_LONG,
1103 b + 1);
1104 } while (b < BITS_PER_LONG);
Dean Nelson33ba3c72008-07-29 22:34:07 -07001105 }
1106 return n_IRQs_detected;
1107}
1108
1109static void
Dean Nelson5b8669d2008-07-29 22:34:18 -07001110xpc_process_activate_IRQ_rcvd_sn2(void)
Dean Nelson33ba3c72008-07-29 22:34:07 -07001111{
Dean Nelson5b8669d2008-07-29 22:34:18 -07001112 unsigned long irq_flags;
1113 int n_IRQs_expected;
Dean Nelson33ba3c72008-07-29 22:34:07 -07001114 int n_IRQs_detected;
1115
Dean Nelson5b8669d2008-07-29 22:34:18 -07001116 spin_lock_irqsave(&xpc_activate_IRQ_rcvd_lock, irq_flags);
1117 n_IRQs_expected = xpc_activate_IRQ_rcvd;
1118 xpc_activate_IRQ_rcvd = 0;
1119 spin_unlock_irqrestore(&xpc_activate_IRQ_rcvd_lock, irq_flags);
1120
Dean Nelson6e41017a2008-07-29 22:34:09 -07001121 n_IRQs_detected = xpc_identify_activate_IRQ_sender_sn2();
Dean Nelson33ba3c72008-07-29 22:34:07 -07001122 if (n_IRQs_detected < n_IRQs_expected) {
Dean Nelsonc39838c2008-07-29 22:34:11 -07001123 /* retry once to help avoid missing amo */
Dean Nelson6e41017a2008-07-29 22:34:09 -07001124 (void)xpc_identify_activate_IRQ_sender_sn2();
Dean Nelson33ba3c72008-07-29 22:34:07 -07001125 }
1126}
1127
Dean Nelsone17d4162008-07-29 22:34:06 -07001128/*
Dean Nelson5b8669d2008-07-29 22:34:18 -07001129 * Setup the channel structures that are sn2 specific.
Dean Nelsone17d4162008-07-29 22:34:06 -07001130 */
1131static enum xp_retval
Dean Nelson5b8669d2008-07-29 22:34:18 -07001132xpc_setup_ch_structures_sn_sn2(struct xpc_partition *part)
Dean Nelsone17d4162008-07-29 22:34:06 -07001133{
Dean Nelsona47d5da2008-07-29 22:34:09 -07001134 struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
Dean Nelson5b8669d2008-07-29 22:34:18 -07001135 struct xpc_channel_sn2 *ch_sn2;
Dean Nelsone17d4162008-07-29 22:34:06 -07001136 enum xp_retval retval;
1137 int ret;
1138 int cpuid;
1139 int ch_number;
Dean Nelsone17d4162008-07-29 22:34:06 -07001140 struct timer_list *timer;
1141 short partid = XPC_PARTID(part);
1142
Dean Nelsone17d4162008-07-29 22:34:06 -07001143 /* allocate all the required GET/PUT values */
1144
Dean Nelson185c3a12008-07-29 22:34:11 -07001145 part_sn2->local_GPs =
Dean Nelson5b8669d2008-07-29 22:34:18 -07001146 xpc_kzalloc_cacheline_aligned(XPC_GP_SIZE, GFP_KERNEL,
1147 &part_sn2->local_GPs_base);
Dean Nelsona47d5da2008-07-29 22:34:09 -07001148 if (part_sn2->local_GPs == NULL) {
Dean Nelsone17d4162008-07-29 22:34:06 -07001149 dev_err(xpc_chan, "can't get memory for local get/put "
1150 "values\n");
Dean Nelson5b8669d2008-07-29 22:34:18 -07001151 return xpNoMemory;
Dean Nelsone17d4162008-07-29 22:34:06 -07001152 }
1153
Dean Nelson185c3a12008-07-29 22:34:11 -07001154 part_sn2->remote_GPs =
Dean Nelson5b8669d2008-07-29 22:34:18 -07001155 xpc_kzalloc_cacheline_aligned(XPC_GP_SIZE, GFP_KERNEL,
1156 &part_sn2->remote_GPs_base);
Dean Nelsona47d5da2008-07-29 22:34:09 -07001157 if (part_sn2->remote_GPs == NULL) {
Dean Nelsone17d4162008-07-29 22:34:06 -07001158 dev_err(xpc_chan, "can't get memory for remote get/put "
1159 "values\n");
1160 retval = xpNoMemory;
Dean Nelson5b8669d2008-07-29 22:34:18 -07001161 goto out_1;
Dean Nelsone17d4162008-07-29 22:34:06 -07001162 }
1163
Dean Nelsona47d5da2008-07-29 22:34:09 -07001164 part_sn2->remote_GPs_pa = 0;
Dean Nelsone17d4162008-07-29 22:34:06 -07001165
1166 /* allocate all the required open and close args */
1167
Dean Nelson5b8669d2008-07-29 22:34:18 -07001168 part_sn2->local_openclose_args =
1169 xpc_kzalloc_cacheline_aligned(XPC_OPENCLOSE_ARGS_SIZE,
1170 GFP_KERNEL, &part_sn2->
1171 local_openclose_args_base);
1172 if (part_sn2->local_openclose_args == NULL) {
Dean Nelsone17d4162008-07-29 22:34:06 -07001173 dev_err(xpc_chan, "can't get memory for local connect args\n");
1174 retval = xpNoMemory;
Dean Nelson5b8669d2008-07-29 22:34:18 -07001175 goto out_2;
Dean Nelsone17d4162008-07-29 22:34:06 -07001176 }
1177
Dean Nelsona47d5da2008-07-29 22:34:09 -07001178 part_sn2->remote_openclose_args_pa = 0;
Dean Nelsone17d4162008-07-29 22:34:06 -07001179
Dean Nelson7fb5e592008-07-29 22:34:10 -07001180 part_sn2->local_chctl_amo_va = xpc_init_IRQ_amo_sn2(partid);
Dean Nelsone17d4162008-07-29 22:34:06 -07001181
Dean Nelson7fb5e592008-07-29 22:34:10 -07001182 part_sn2->notify_IRQ_nasid = 0;
1183 part_sn2->notify_IRQ_phys_cpuid = 0;
1184 part_sn2->remote_chctl_amo_va = NULL;
Dean Nelsone17d4162008-07-29 22:34:06 -07001185
Dean Nelson7fb5e592008-07-29 22:34:10 -07001186 sprintf(part_sn2->notify_IRQ_owner, "xpc%02d", partid);
Dean Nelsona47d5da2008-07-29 22:34:09 -07001187 ret = request_irq(SGI_XPC_NOTIFY, xpc_handle_notify_IRQ_sn2,
Dean Nelson7fb5e592008-07-29 22:34:10 -07001188 IRQF_SHARED, part_sn2->notify_IRQ_owner,
Dean Nelsona47d5da2008-07-29 22:34:09 -07001189 (void *)(u64)partid);
Dean Nelsone17d4162008-07-29 22:34:06 -07001190 if (ret != 0) {
1191 dev_err(xpc_chan, "can't register NOTIFY IRQ handler, "
1192 "errno=%d\n", -ret);
1193 retval = xpLackOfResources;
Dean Nelson5b8669d2008-07-29 22:34:18 -07001194 goto out_3;
Dean Nelsone17d4162008-07-29 22:34:06 -07001195 }
1196
Dean Nelson7fb5e592008-07-29 22:34:10 -07001197 /* Setup a timer to check for dropped notify IRQs */
Dean Nelsona47d5da2008-07-29 22:34:09 -07001198 timer = &part_sn2->dropped_notify_IRQ_timer;
Dean Nelsone17d4162008-07-29 22:34:06 -07001199 init_timer(timer);
Dean Nelsona47d5da2008-07-29 22:34:09 -07001200 timer->function =
Dean Nelson7fb5e592008-07-29 22:34:10 -07001201 (void (*)(unsigned long))xpc_check_for_dropped_notify_IRQ_sn2;
Dean Nelsone17d4162008-07-29 22:34:06 -07001202 timer->data = (unsigned long)part;
Dean Nelson7fb5e592008-07-29 22:34:10 -07001203 timer->expires = jiffies + XPC_DROPPED_NOTIFY_IRQ_WAIT_INTERVAL;
Dean Nelsone17d4162008-07-29 22:34:06 -07001204 add_timer(timer);
1205
Dean Nelsone17d4162008-07-29 22:34:06 -07001206 for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
Dean Nelson5b8669d2008-07-29 22:34:18 -07001207 ch_sn2 = &part->channels[ch_number].sn.sn2;
Dean Nelsone17d4162008-07-29 22:34:06 -07001208
Dean Nelson5b8669d2008-07-29 22:34:18 -07001209 ch_sn2->local_GP = &part_sn2->local_GPs[ch_number];
1210 ch_sn2->local_openclose_args =
1211 &part_sn2->local_openclose_args[ch_number];
Dean Nelsone17d4162008-07-29 22:34:06 -07001212
Dean Nelson5b8669d2008-07-29 22:34:18 -07001213 mutex_init(&ch_sn2->msg_to_pull_mutex);
Dean Nelsone17d4162008-07-29 22:34:06 -07001214 }
1215
1216 /*
Dean Nelsone17d4162008-07-29 22:34:06 -07001217 * Setup the per partition specific variables required by the
1218 * remote partition to establish channel connections with us.
1219 *
1220 * The setting of the magic # indicates that these per partition
1221 * specific variables are ready to be used.
1222 */
Dean Nelsona812dcc2008-07-29 22:34:16 -07001223 xpc_vars_part_sn2[partid].GPs_pa = xp_pa(part_sn2->local_GPs);
Dean Nelson8e85c232008-07-29 22:34:13 -07001224 xpc_vars_part_sn2[partid].openclose_args_pa =
Dean Nelson5b8669d2008-07-29 22:34:18 -07001225 xp_pa(part_sn2->local_openclose_args);
Dean Nelson8e85c232008-07-29 22:34:13 -07001226 xpc_vars_part_sn2[partid].chctl_amo_pa =
Dean Nelsona812dcc2008-07-29 22:34:16 -07001227 xp_pa(part_sn2->local_chctl_amo_va);
Dean Nelsone17d4162008-07-29 22:34:06 -07001228 cpuid = raw_smp_processor_id(); /* any CPU in this partition will do */
Dean Nelson8e85c232008-07-29 22:34:13 -07001229 xpc_vars_part_sn2[partid].notify_IRQ_nasid = cpuid_to_nasid(cpuid);
1230 xpc_vars_part_sn2[partid].notify_IRQ_phys_cpuid =
1231 cpu_physical_id(cpuid);
1232 xpc_vars_part_sn2[partid].nchannels = part->nchannels;
Dean Nelson5b8669d2008-07-29 22:34:18 -07001233 xpc_vars_part_sn2[partid].magic = XPC_VP_MAGIC1_SN2;
Dean Nelsone17d4162008-07-29 22:34:06 -07001234
1235 return xpSuccess;
1236
Dean Nelson5b8669d2008-07-29 22:34:18 -07001237 /* setup of ch structures failed */
Dean Nelsone17d4162008-07-29 22:34:06 -07001238out_3:
Dean Nelson5b8669d2008-07-29 22:34:18 -07001239 kfree(part_sn2->local_openclose_args_base);
1240 part_sn2->local_openclose_args = NULL;
1241out_2:
Dean Nelsona47d5da2008-07-29 22:34:09 -07001242 kfree(part_sn2->remote_GPs_base);
1243 part_sn2->remote_GPs = NULL;
Dean Nelson5b8669d2008-07-29 22:34:18 -07001244out_1:
Dean Nelsona47d5da2008-07-29 22:34:09 -07001245 kfree(part_sn2->local_GPs_base);
1246 part_sn2->local_GPs = NULL;
Dean Nelsone17d4162008-07-29 22:34:06 -07001247 return retval;
1248}
1249
1250/*
Dean Nelson5b8669d2008-07-29 22:34:18 -07001251 * Teardown the channel structures that are sn2 specific.
Dean Nelsone17d4162008-07-29 22:34:06 -07001252 */
1253static void
Dean Nelson5b8669d2008-07-29 22:34:18 -07001254xpc_teardown_ch_structures_sn_sn2(struct xpc_partition *part)
Dean Nelsone17d4162008-07-29 22:34:06 -07001255{
Dean Nelsona47d5da2008-07-29 22:34:09 -07001256 struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
Dean Nelsone17d4162008-07-29 22:34:06 -07001257 short partid = XPC_PARTID(part);
1258
1259 /*
Dean Nelson5b8669d2008-07-29 22:34:18 -07001260 * Indicate that the variables specific to the remote partition are no
1261 * longer available for its use.
Dean Nelsone17d4162008-07-29 22:34:06 -07001262 */
Dean Nelson8e85c232008-07-29 22:34:13 -07001263 xpc_vars_part_sn2[partid].magic = 0;
Dean Nelsone17d4162008-07-29 22:34:06 -07001264
Dean Nelsone17d4162008-07-29 22:34:06 -07001265 /* in case we've still got outstanding timers registered... */
Dean Nelsona47d5da2008-07-29 22:34:09 -07001266 del_timer_sync(&part_sn2->dropped_notify_IRQ_timer);
Dean Nelson5b8669d2008-07-29 22:34:18 -07001267 free_irq(SGI_XPC_NOTIFY, (void *)(u64)partid);
Dean Nelsone17d4162008-07-29 22:34:06 -07001268
Dean Nelson5b8669d2008-07-29 22:34:18 -07001269 kfree(part_sn2->local_openclose_args_base);
1270 part_sn2->local_openclose_args = NULL;
Dean Nelsona47d5da2008-07-29 22:34:09 -07001271 kfree(part_sn2->remote_GPs_base);
1272 part_sn2->remote_GPs = NULL;
1273 kfree(part_sn2->local_GPs_base);
1274 part_sn2->local_GPs = NULL;
Dean Nelson7fb5e592008-07-29 22:34:10 -07001275 part_sn2->local_chctl_amo_va = NULL;
Dean Nelsone17d4162008-07-29 22:34:06 -07001276}
1277
1278/*
1279 * Create a wrapper that hides the underlying mechanism for pulling a cacheline
1280 * (or multiple cachelines) from a remote partition.
1281 *
Dean Nelsona812dcc2008-07-29 22:34:16 -07001282 * src_pa must be a cacheline aligned physical address on the remote partition.
Dean Nelsone17d4162008-07-29 22:34:06 -07001283 * dst must be a cacheline aligned virtual address on this partition.
1284 * cnt must be cacheline sized
1285 */
Dean Nelsonea57f802008-07-29 22:34:14 -07001286/* ??? Replace this function by call to xp_remote_memcpy() or bte_copy()? */
Dean Nelsone17d4162008-07-29 22:34:06 -07001287static enum xp_retval
1288xpc_pull_remote_cachelines_sn2(struct xpc_partition *part, void *dst,
Dean Nelsona812dcc2008-07-29 22:34:16 -07001289 const unsigned long src_pa, size_t cnt)
Dean Nelsone17d4162008-07-29 22:34:06 -07001290{
1291 enum xp_retval ret;
1292
Dean Nelsona812dcc2008-07-29 22:34:16 -07001293 DBUG_ON(src_pa != L1_CACHE_ALIGN(src_pa));
1294 DBUG_ON((unsigned long)dst != L1_CACHE_ALIGN((unsigned long)dst));
Dean Nelsone17d4162008-07-29 22:34:06 -07001295 DBUG_ON(cnt != L1_CACHE_ALIGN(cnt));
1296
Dean Nelson83469b52008-07-29 22:34:18 -07001297 if (part->act_state == XPC_P_AS_DEACTIVATING)
Dean Nelsone17d4162008-07-29 22:34:06 -07001298 return part->reason;
1299
Dean Nelsona812dcc2008-07-29 22:34:16 -07001300 ret = xp_remote_memcpy(xp_pa(dst), src_pa, cnt);
Dean Nelsone17d4162008-07-29 22:34:06 -07001301 if (ret != xpSuccess) {
1302 dev_dbg(xpc_chan, "xp_remote_memcpy() from partition %d failed,"
1303 " ret=%d\n", XPC_PARTID(part), ret);
1304 }
1305 return ret;
1306}
1307
1308/*
1309 * Pull the remote per partition specific variables from the specified
1310 * partition.
1311 */
1312static enum xp_retval
1313xpc_pull_remote_vars_part_sn2(struct xpc_partition *part)
1314{
Dean Nelsona47d5da2008-07-29 22:34:09 -07001315 struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
Dean Nelsone17d4162008-07-29 22:34:06 -07001316 u8 buffer[L1_CACHE_BYTES * 2];
1317 struct xpc_vars_part_sn2 *pulled_entry_cacheline =
1318 (struct xpc_vars_part_sn2 *)L1_CACHE_ALIGN((u64)buffer);
1319 struct xpc_vars_part_sn2 *pulled_entry;
Dean Nelsona812dcc2008-07-29 22:34:16 -07001320 unsigned long remote_entry_cacheline_pa;
1321 unsigned long remote_entry_pa;
Dean Nelsone17d4162008-07-29 22:34:06 -07001322 short partid = XPC_PARTID(part);
1323 enum xp_retval ret;
1324
1325 /* pull the cacheline that contains the variables we're interested in */
1326
Dean Nelsona47d5da2008-07-29 22:34:09 -07001327 DBUG_ON(part_sn2->remote_vars_part_pa !=
1328 L1_CACHE_ALIGN(part_sn2->remote_vars_part_pa));
Dean Nelsone17d4162008-07-29 22:34:06 -07001329 DBUG_ON(sizeof(struct xpc_vars_part_sn2) != L1_CACHE_BYTES / 2);
1330
Dean Nelsona47d5da2008-07-29 22:34:09 -07001331 remote_entry_pa = part_sn2->remote_vars_part_pa +
Dean Nelsone17d4162008-07-29 22:34:06 -07001332 sn_partition_id * sizeof(struct xpc_vars_part_sn2);
1333
1334 remote_entry_cacheline_pa = (remote_entry_pa & ~(L1_CACHE_BYTES - 1));
1335
1336 pulled_entry = (struct xpc_vars_part_sn2 *)((u64)pulled_entry_cacheline
1337 + (remote_entry_pa &
1338 (L1_CACHE_BYTES - 1)));
1339
1340 ret = xpc_pull_remote_cachelines_sn2(part, pulled_entry_cacheline,
Dean Nelsona812dcc2008-07-29 22:34:16 -07001341 remote_entry_cacheline_pa,
Dean Nelsone17d4162008-07-29 22:34:06 -07001342 L1_CACHE_BYTES);
1343 if (ret != xpSuccess) {
1344 dev_dbg(xpc_chan, "failed to pull XPC vars_part from "
1345 "partition %d, ret=%d\n", partid, ret);
1346 return ret;
1347 }
1348
1349 /* see if they've been set up yet */
1350
Dean Nelson5b8669d2008-07-29 22:34:18 -07001351 if (pulled_entry->magic != XPC_VP_MAGIC1_SN2 &&
1352 pulled_entry->magic != XPC_VP_MAGIC2_SN2) {
Dean Nelsone17d4162008-07-29 22:34:06 -07001353
1354 if (pulled_entry->magic != 0) {
1355 dev_dbg(xpc_chan, "partition %d's XPC vars_part for "
1356 "partition %d has bad magic value (=0x%lx)\n",
1357 partid, sn_partition_id, pulled_entry->magic);
1358 return xpBadMagic;
1359 }
1360
1361 /* they've not been initialized yet */
1362 return xpRetry;
1363 }
1364
Dean Nelson5b8669d2008-07-29 22:34:18 -07001365 if (xpc_vars_part_sn2[partid].magic == XPC_VP_MAGIC1_SN2) {
Dean Nelsone17d4162008-07-29 22:34:06 -07001366
1367 /* validate the variables */
1368
1369 if (pulled_entry->GPs_pa == 0 ||
1370 pulled_entry->openclose_args_pa == 0 ||
Dean Nelson7fb5e592008-07-29 22:34:10 -07001371 pulled_entry->chctl_amo_pa == 0) {
Dean Nelsone17d4162008-07-29 22:34:06 -07001372
1373 dev_err(xpc_chan, "partition %d's XPC vars_part for "
1374 "partition %d are not valid\n", partid,
1375 sn_partition_id);
1376 return xpInvalidAddress;
1377 }
1378
1379 /* the variables we imported look to be valid */
1380
Dean Nelsona47d5da2008-07-29 22:34:09 -07001381 part_sn2->remote_GPs_pa = pulled_entry->GPs_pa;
1382 part_sn2->remote_openclose_args_pa =
Dean Nelsone17d4162008-07-29 22:34:06 -07001383 pulled_entry->openclose_args_pa;
Dean Nelson7fb5e592008-07-29 22:34:10 -07001384 part_sn2->remote_chctl_amo_va =
Dean Nelsonc39838c2008-07-29 22:34:11 -07001385 (struct amo *)__va(pulled_entry->chctl_amo_pa);
Dean Nelson7fb5e592008-07-29 22:34:10 -07001386 part_sn2->notify_IRQ_nasid = pulled_entry->notify_IRQ_nasid;
1387 part_sn2->notify_IRQ_phys_cpuid =
1388 pulled_entry->notify_IRQ_phys_cpuid;
Dean Nelsone17d4162008-07-29 22:34:06 -07001389
1390 if (part->nchannels > pulled_entry->nchannels)
1391 part->nchannels = pulled_entry->nchannels;
1392
1393 /* let the other side know that we've pulled their variables */
1394
Dean Nelson5b8669d2008-07-29 22:34:18 -07001395 xpc_vars_part_sn2[partid].magic = XPC_VP_MAGIC2_SN2;
Dean Nelsone17d4162008-07-29 22:34:06 -07001396 }
1397
Dean Nelson5b8669d2008-07-29 22:34:18 -07001398 if (pulled_entry->magic == XPC_VP_MAGIC1_SN2)
Dean Nelsone17d4162008-07-29 22:34:06 -07001399 return xpRetry;
1400
1401 return xpSuccess;
1402}
1403
1404/*
1405 * Establish first contact with the remote partititon. This involves pulling
1406 * the XPC per partition variables from the remote partition and waiting for
1407 * the remote partition to pull ours.
1408 */
1409static enum xp_retval
1410xpc_make_first_contact_sn2(struct xpc_partition *part)
1411{
Dean Nelsona47d5da2008-07-29 22:34:09 -07001412 struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
Dean Nelsone17d4162008-07-29 22:34:06 -07001413 enum xp_retval ret;
1414
Dean Nelson33ba3c72008-07-29 22:34:07 -07001415 /*
Dean Nelsonc39838c2008-07-29 22:34:11 -07001416 * Register the remote partition's amos with SAL so it can handle
Dean Nelson33ba3c72008-07-29 22:34:07 -07001417 * and cleanup errors within that address range should the remote
1418 * partition go down. We don't unregister this range because it is
1419 * difficult to tell when outstanding writes to the remote partition
1420 * are finished and thus when it is safe to unregister. This should
1421 * not result in wasted space in the SAL xp_addr_region table because
1422 * we should get the same page for remote_amos_page_pa after module
1423 * reloads and system reboots.
1424 */
Dean Nelsona47d5da2008-07-29 22:34:09 -07001425 if (sn_register_xp_addr_region(part_sn2->remote_amos_page_pa,
Dean Nelson33ba3c72008-07-29 22:34:07 -07001426 PAGE_SIZE, 1) < 0) {
1427 dev_warn(xpc_part, "xpc_activating(%d) failed to register "
1428 "xp_addr region\n", XPC_PARTID(part));
1429
1430 ret = xpPhysAddrRegFailed;
1431 XPC_DEACTIVATE_PARTITION(part, ret);
1432 return ret;
1433 }
1434
Dean Nelsona47d5da2008-07-29 22:34:09 -07001435 /*
1436 * Send activate IRQ to get other side to activate if they've not
1437 * already begun to do so.
1438 */
Dean Nelson7fb5e592008-07-29 22:34:10 -07001439 xpc_send_activate_IRQ_sn2(part_sn2->remote_amos_page_pa,
Dean Nelsona47d5da2008-07-29 22:34:09 -07001440 cnodeid_to_nasid(0),
1441 part_sn2->activate_IRQ_nasid,
1442 part_sn2->activate_IRQ_phys_cpuid);
Dean Nelson33ba3c72008-07-29 22:34:07 -07001443
Dean Nelsone17d4162008-07-29 22:34:06 -07001444 while ((ret = xpc_pull_remote_vars_part_sn2(part)) != xpSuccess) {
1445 if (ret != xpRetry) {
1446 XPC_DEACTIVATE_PARTITION(part, ret);
1447 return ret;
1448 }
1449
1450 dev_dbg(xpc_part, "waiting to make first contact with "
1451 "partition %d\n", XPC_PARTID(part));
1452
1453 /* wait a 1/4 of a second or so */
1454 (void)msleep_interruptible(250);
1455
Dean Nelson83469b52008-07-29 22:34:18 -07001456 if (part->act_state == XPC_P_AS_DEACTIVATING)
Dean Nelsone17d4162008-07-29 22:34:06 -07001457 return part->reason;
1458 }
1459
1460 return xpSuccess;
1461}
1462
1463/*
Dean Nelson7fb5e592008-07-29 22:34:10 -07001464 * Get the chctl flags and pull the openclose args and/or remote GPs as needed.
Dean Nelsone17d4162008-07-29 22:34:06 -07001465 */
1466static u64
Dean Nelson7fb5e592008-07-29 22:34:10 -07001467xpc_get_chctl_all_flags_sn2(struct xpc_partition *part)
Dean Nelsone17d4162008-07-29 22:34:06 -07001468{
Dean Nelsona47d5da2008-07-29 22:34:09 -07001469 struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
Dean Nelsone17d4162008-07-29 22:34:06 -07001470 unsigned long irq_flags;
Dean Nelson7fb5e592008-07-29 22:34:10 -07001471 union xpc_channel_ctl_flags chctl;
Dean Nelsone17d4162008-07-29 22:34:06 -07001472 enum xp_retval ret;
1473
1474 /*
Dean Nelson7fb5e592008-07-29 22:34:10 -07001475 * See if there are any chctl flags to be handled.
Dean Nelsone17d4162008-07-29 22:34:06 -07001476 */
1477
Dean Nelson7fb5e592008-07-29 22:34:10 -07001478 spin_lock_irqsave(&part->chctl_lock, irq_flags);
1479 chctl = part->chctl;
1480 if (chctl.all_flags != 0)
1481 part->chctl.all_flags = 0;
Dean Nelsone17d4162008-07-29 22:34:06 -07001482
Dean Nelson7fb5e592008-07-29 22:34:10 -07001483 spin_unlock_irqrestore(&part->chctl_lock, irq_flags);
Dean Nelsone17d4162008-07-29 22:34:06 -07001484
Dean Nelson7fb5e592008-07-29 22:34:10 -07001485 if (xpc_any_openclose_chctl_flags_set(&chctl)) {
Dean Nelsona47d5da2008-07-29 22:34:09 -07001486 ret = xpc_pull_remote_cachelines_sn2(part, part->
1487 remote_openclose_args,
Dean Nelsona812dcc2008-07-29 22:34:16 -07001488 part_sn2->
Dean Nelsone17d4162008-07-29 22:34:06 -07001489 remote_openclose_args_pa,
1490 XPC_OPENCLOSE_ARGS_SIZE);
1491 if (ret != xpSuccess) {
1492 XPC_DEACTIVATE_PARTITION(part, ret);
1493
1494 dev_dbg(xpc_chan, "failed to pull openclose args from "
1495 "partition %d, ret=%d\n", XPC_PARTID(part),
1496 ret);
1497
Dean Nelson7fb5e592008-07-29 22:34:10 -07001498 /* don't bother processing chctl flags anymore */
1499 chctl.all_flags = 0;
Dean Nelsone17d4162008-07-29 22:34:06 -07001500 }
1501 }
1502
Dean Nelson7fb5e592008-07-29 22:34:10 -07001503 if (xpc_any_msg_chctl_flags_set(&chctl)) {
Dean Nelsona47d5da2008-07-29 22:34:09 -07001504 ret = xpc_pull_remote_cachelines_sn2(part, part_sn2->remote_GPs,
Dean Nelsona812dcc2008-07-29 22:34:16 -07001505 part_sn2->remote_GPs_pa,
Dean Nelsone17d4162008-07-29 22:34:06 -07001506 XPC_GP_SIZE);
1507 if (ret != xpSuccess) {
1508 XPC_DEACTIVATE_PARTITION(part, ret);
1509
1510 dev_dbg(xpc_chan, "failed to pull GPs from partition "
1511 "%d, ret=%d\n", XPC_PARTID(part), ret);
1512
Dean Nelson7fb5e592008-07-29 22:34:10 -07001513 /* don't bother processing chctl flags anymore */
1514 chctl.all_flags = 0;
Dean Nelsone17d4162008-07-29 22:34:06 -07001515 }
1516 }
1517
Dean Nelson7fb5e592008-07-29 22:34:10 -07001518 return chctl.all_flags;
Dean Nelsone17d4162008-07-29 22:34:06 -07001519}
1520
Dean Nelsona47d5da2008-07-29 22:34:09 -07001521/*
Dean Nelson185c3a12008-07-29 22:34:11 -07001522 * Allocate the local message queue and the notify queue.
1523 */
1524static enum xp_retval
1525xpc_allocate_local_msgqueue_sn2(struct xpc_channel *ch)
1526{
Dean Nelson5b8669d2008-07-29 22:34:18 -07001527 struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
Dean Nelson185c3a12008-07-29 22:34:11 -07001528 unsigned long irq_flags;
1529 int nentries;
1530 size_t nbytes;
1531
1532 for (nentries = ch->local_nentries; nentries > 0; nentries--) {
1533
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07001534 nbytes = nentries * ch->entry_size;
Dean Nelson5b8669d2008-07-29 22:34:18 -07001535 ch_sn2->local_msgqueue =
1536 xpc_kzalloc_cacheline_aligned(nbytes, GFP_KERNEL,
1537 &ch_sn2->local_msgqueue_base);
1538 if (ch_sn2->local_msgqueue == NULL)
Dean Nelson185c3a12008-07-29 22:34:11 -07001539 continue;
1540
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07001541 nbytes = nentries * sizeof(struct xpc_notify_sn2);
Dean Nelson5b8669d2008-07-29 22:34:18 -07001542 ch_sn2->notify_queue = kzalloc(nbytes, GFP_KERNEL);
1543 if (ch_sn2->notify_queue == NULL) {
1544 kfree(ch_sn2->local_msgqueue_base);
1545 ch_sn2->local_msgqueue = NULL;
Dean Nelson185c3a12008-07-29 22:34:11 -07001546 continue;
1547 }
1548
1549 spin_lock_irqsave(&ch->lock, irq_flags);
1550 if (nentries < ch->local_nentries) {
1551 dev_dbg(xpc_chan, "nentries=%d local_nentries=%d, "
1552 "partid=%d, channel=%d\n", nentries,
1553 ch->local_nentries, ch->partid, ch->number);
1554
1555 ch->local_nentries = nentries;
1556 }
1557 spin_unlock_irqrestore(&ch->lock, irq_flags);
1558 return xpSuccess;
1559 }
1560
1561 dev_dbg(xpc_chan, "can't get memory for local message queue and notify "
1562 "queue, partid=%d, channel=%d\n", ch->partid, ch->number);
1563 return xpNoMemory;
1564}
1565
1566/*
1567 * Allocate the cached remote message queue.
1568 */
1569static enum xp_retval
1570xpc_allocate_remote_msgqueue_sn2(struct xpc_channel *ch)
1571{
Dean Nelson5b8669d2008-07-29 22:34:18 -07001572 struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
Dean Nelson185c3a12008-07-29 22:34:11 -07001573 unsigned long irq_flags;
1574 int nentries;
1575 size_t nbytes;
1576
1577 DBUG_ON(ch->remote_nentries <= 0);
1578
1579 for (nentries = ch->remote_nentries; nentries > 0; nentries--) {
1580
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07001581 nbytes = nentries * ch->entry_size;
Dean Nelson5b8669d2008-07-29 22:34:18 -07001582 ch_sn2->remote_msgqueue =
1583 xpc_kzalloc_cacheline_aligned(nbytes, GFP_KERNEL, &ch_sn2->
1584 remote_msgqueue_base);
1585 if (ch_sn2->remote_msgqueue == NULL)
Dean Nelson185c3a12008-07-29 22:34:11 -07001586 continue;
1587
1588 spin_lock_irqsave(&ch->lock, irq_flags);
1589 if (nentries < ch->remote_nentries) {
1590 dev_dbg(xpc_chan, "nentries=%d remote_nentries=%d, "
1591 "partid=%d, channel=%d\n", nentries,
1592 ch->remote_nentries, ch->partid, ch->number);
1593
1594 ch->remote_nentries = nentries;
1595 }
1596 spin_unlock_irqrestore(&ch->lock, irq_flags);
1597 return xpSuccess;
1598 }
1599
1600 dev_dbg(xpc_chan, "can't get memory for cached remote message queue, "
1601 "partid=%d, channel=%d\n", ch->partid, ch->number);
1602 return xpNoMemory;
1603}
1604
1605/*
1606 * Allocate message queues and other stuff associated with a channel.
1607 *
1608 * Note: Assumes all of the channel sizes are filled in.
1609 */
1610static enum xp_retval
Dean Nelson5b8669d2008-07-29 22:34:18 -07001611xpc_setup_msg_structures_sn2(struct xpc_channel *ch)
Dean Nelson185c3a12008-07-29 22:34:11 -07001612{
Dean Nelson5b8669d2008-07-29 22:34:18 -07001613 struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
Dean Nelson185c3a12008-07-29 22:34:11 -07001614 enum xp_retval ret;
1615
1616 DBUG_ON(ch->flags & XPC_C_SETUP);
1617
1618 ret = xpc_allocate_local_msgqueue_sn2(ch);
1619 if (ret == xpSuccess) {
1620
1621 ret = xpc_allocate_remote_msgqueue_sn2(ch);
1622 if (ret != xpSuccess) {
Dean Nelson5b8669d2008-07-29 22:34:18 -07001623 kfree(ch_sn2->local_msgqueue_base);
1624 ch_sn2->local_msgqueue = NULL;
1625 kfree(ch_sn2->notify_queue);
1626 ch_sn2->notify_queue = NULL;
Dean Nelson185c3a12008-07-29 22:34:11 -07001627 }
1628 }
1629 return ret;
1630}
1631
1632/*
1633 * Free up message queues and other stuff that were allocated for the specified
1634 * channel.
Dean Nelson185c3a12008-07-29 22:34:11 -07001635 */
1636static void
Dean Nelson5b8669d2008-07-29 22:34:18 -07001637xpc_teardown_msg_structures_sn2(struct xpc_channel *ch)
Dean Nelson185c3a12008-07-29 22:34:11 -07001638{
1639 struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
1640
1641 DBUG_ON(!spin_is_locked(&ch->lock));
Dean Nelson185c3a12008-07-29 22:34:11 -07001642
Dean Nelson5b8669d2008-07-29 22:34:18 -07001643 ch_sn2->remote_msgqueue_pa = 0;
Dean Nelson185c3a12008-07-29 22:34:11 -07001644
1645 ch_sn2->local_GP->get = 0;
1646 ch_sn2->local_GP->put = 0;
1647 ch_sn2->remote_GP.get = 0;
1648 ch_sn2->remote_GP.put = 0;
1649 ch_sn2->w_local_GP.get = 0;
1650 ch_sn2->w_local_GP.put = 0;
1651 ch_sn2->w_remote_GP.get = 0;
1652 ch_sn2->w_remote_GP.put = 0;
1653 ch_sn2->next_msg_to_pull = 0;
1654
1655 if (ch->flags & XPC_C_SETUP) {
1656 dev_dbg(xpc_chan, "ch->flags=0x%x, partid=%d, channel=%d\n",
1657 ch->flags, ch->partid, ch->number);
1658
Dean Nelson5b8669d2008-07-29 22:34:18 -07001659 kfree(ch_sn2->local_msgqueue_base);
1660 ch_sn2->local_msgqueue = NULL;
1661 kfree(ch_sn2->remote_msgqueue_base);
1662 ch_sn2->remote_msgqueue = NULL;
1663 kfree(ch_sn2->notify_queue);
1664 ch_sn2->notify_queue = NULL;
Dean Nelson185c3a12008-07-29 22:34:11 -07001665 }
1666}
1667
1668/*
Dean Nelsona47d5da2008-07-29 22:34:09 -07001669 * Notify those who wanted to be notified upon delivery of their message.
1670 */
1671static void
1672xpc_notify_senders_sn2(struct xpc_channel *ch, enum xp_retval reason, s64 put)
1673{
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07001674 struct xpc_notify_sn2 *notify;
Dean Nelsona47d5da2008-07-29 22:34:09 -07001675 u8 notify_type;
1676 s64 get = ch->sn.sn2.w_remote_GP.get - 1;
1677
1678 while (++get < put && atomic_read(&ch->n_to_notify) > 0) {
1679
Dean Nelson5b8669d2008-07-29 22:34:18 -07001680 notify = &ch->sn.sn2.notify_queue[get % ch->local_nentries];
Dean Nelsona47d5da2008-07-29 22:34:09 -07001681
1682 /*
1683 * See if the notify entry indicates it was associated with
1684 * a message who's sender wants to be notified. It is possible
1685 * that it is, but someone else is doing or has done the
1686 * notification.
1687 */
1688 notify_type = notify->type;
1689 if (notify_type == 0 ||
1690 cmpxchg(&notify->type, notify_type, 0) != notify_type) {
1691 continue;
1692 }
1693
1694 DBUG_ON(notify_type != XPC_N_CALL);
1695
1696 atomic_dec(&ch->n_to_notify);
1697
1698 if (notify->func != NULL) {
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07001699 dev_dbg(xpc_chan, "notify->func() called, notify=0x%p "
1700 "msg_number=%ld partid=%d channel=%d\n",
Dean Nelsona47d5da2008-07-29 22:34:09 -07001701 (void *)notify, get, ch->partid, ch->number);
1702
1703 notify->func(reason, ch->partid, ch->number,
1704 notify->key);
1705
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07001706 dev_dbg(xpc_chan, "notify->func() returned, notify=0x%p"
1707 " msg_number=%ld partid=%d channel=%d\n",
1708 (void *)notify, get, ch->partid, ch->number);
Dean Nelsona47d5da2008-07-29 22:34:09 -07001709 }
1710 }
1711}
1712
1713static void
1714xpc_notify_senders_of_disconnect_sn2(struct xpc_channel *ch)
1715{
1716 xpc_notify_senders_sn2(ch, ch->reason, ch->sn.sn2.w_local_GP.put);
1717}
1718
1719/*
1720 * Clear some of the msg flags in the local message queue.
1721 */
1722static inline void
1723xpc_clear_local_msgqueue_flags_sn2(struct xpc_channel *ch)
1724{
1725 struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07001726 struct xpc_msg_sn2 *msg;
Dean Nelsona47d5da2008-07-29 22:34:09 -07001727 s64 get;
1728
1729 get = ch_sn2->w_remote_GP.get;
1730 do {
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07001731 msg = (struct xpc_msg_sn2 *)((u64)ch_sn2->local_msgqueue +
1732 (get % ch->local_nentries) *
1733 ch->entry_size);
Robin Holt252523e2009-01-29 14:25:07 -08001734 DBUG_ON(!(msg->flags & XPC_M_SN2_READY));
Dean Nelsona47d5da2008-07-29 22:34:09 -07001735 msg->flags = 0;
1736 } while (++get < ch_sn2->remote_GP.get);
1737}
1738
1739/*
1740 * Clear some of the msg flags in the remote message queue.
1741 */
1742static inline void
1743xpc_clear_remote_msgqueue_flags_sn2(struct xpc_channel *ch)
1744{
1745 struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07001746 struct xpc_msg_sn2 *msg;
Dean Nelsona47d5da2008-07-29 22:34:09 -07001747 s64 put;
1748
Robin Holt252523e2009-01-29 14:25:07 -08001749 /* flags are zeroed when the buffer is allocated */
1750 if (ch_sn2->remote_GP.put < ch->remote_nentries)
1751 return;
1752
1753 put = max(ch_sn2->w_remote_GP.put, ch->remote_nentries);
Dean Nelsona47d5da2008-07-29 22:34:09 -07001754 do {
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07001755 msg = (struct xpc_msg_sn2 *)((u64)ch_sn2->remote_msgqueue +
1756 (put % ch->remote_nentries) *
1757 ch->entry_size);
Robin Holt252523e2009-01-29 14:25:07 -08001758 DBUG_ON(!(msg->flags & XPC_M_SN2_READY));
1759 DBUG_ON(!(msg->flags & XPC_M_SN2_DONE));
1760 DBUG_ON(msg->number != put - ch->remote_nentries);
Dean Nelsona47d5da2008-07-29 22:34:09 -07001761 msg->flags = 0;
1762 } while (++put < ch_sn2->remote_GP.put);
1763}
1764
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07001765static int
1766xpc_n_of_deliverable_payloads_sn2(struct xpc_channel *ch)
1767{
1768 return ch->sn.sn2.w_remote_GP.put - ch->sn.sn2.w_local_GP.get;
1769}
1770
Dean Nelsona47d5da2008-07-29 22:34:09 -07001771static void
Dean Nelson7fb5e592008-07-29 22:34:10 -07001772xpc_process_msg_chctl_flags_sn2(struct xpc_partition *part, int ch_number)
Dean Nelsona47d5da2008-07-29 22:34:09 -07001773{
1774 struct xpc_channel *ch = &part->channels[ch_number];
1775 struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07001776 int npayloads_sent;
Dean Nelsona47d5da2008-07-29 22:34:09 -07001777
1778 ch_sn2->remote_GP = part->sn.sn2.remote_GPs[ch_number];
1779
1780 /* See what, if anything, has changed for each connected channel */
1781
1782 xpc_msgqueue_ref(ch);
1783
1784 if (ch_sn2->w_remote_GP.get == ch_sn2->remote_GP.get &&
1785 ch_sn2->w_remote_GP.put == ch_sn2->remote_GP.put) {
1786 /* nothing changed since GPs were last pulled */
1787 xpc_msgqueue_deref(ch);
1788 return;
1789 }
1790
1791 if (!(ch->flags & XPC_C_CONNECTED)) {
1792 xpc_msgqueue_deref(ch);
1793 return;
1794 }
1795
1796 /*
1797 * First check to see if messages recently sent by us have been
1798 * received by the other side. (The remote GET value will have
1799 * changed since we last looked at it.)
1800 */
1801
1802 if (ch_sn2->w_remote_GP.get != ch_sn2->remote_GP.get) {
1803
1804 /*
1805 * We need to notify any senders that want to be notified
1806 * that their sent messages have been received by their
1807 * intended recipients. We need to do this before updating
1808 * w_remote_GP.get so that we don't allocate the same message
1809 * queue entries prematurely (see xpc_allocate_msg()).
1810 */
1811 if (atomic_read(&ch->n_to_notify) > 0) {
1812 /*
1813 * Notify senders that messages sent have been
1814 * received and delivered by the other side.
1815 */
1816 xpc_notify_senders_sn2(ch, xpMsgDelivered,
1817 ch_sn2->remote_GP.get);
1818 }
1819
1820 /*
1821 * Clear msg->flags in previously sent messages, so that
1822 * they're ready for xpc_allocate_msg().
1823 */
1824 xpc_clear_local_msgqueue_flags_sn2(ch);
1825
1826 ch_sn2->w_remote_GP.get = ch_sn2->remote_GP.get;
1827
1828 dev_dbg(xpc_chan, "w_remote_GP.get changed to %ld, partid=%d, "
1829 "channel=%d\n", ch_sn2->w_remote_GP.get, ch->partid,
1830 ch->number);
1831
1832 /*
1833 * If anyone was waiting for message queue entries to become
1834 * available, wake them up.
1835 */
1836 if (atomic_read(&ch->n_on_msg_allocate_wq) > 0)
1837 wake_up(&ch->msg_allocate_wq);
1838 }
1839
1840 /*
1841 * Now check for newly sent messages by the other side. (The remote
1842 * PUT value will have changed since we last looked at it.)
1843 */
1844
1845 if (ch_sn2->w_remote_GP.put != ch_sn2->remote_GP.put) {
1846 /*
1847 * Clear msg->flags in previously received messages, so that
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07001848 * they're ready for xpc_get_deliverable_payload_sn2().
Dean Nelsona47d5da2008-07-29 22:34:09 -07001849 */
1850 xpc_clear_remote_msgqueue_flags_sn2(ch);
1851
Robin Holt69b3bb62009-01-29 14:25:06 -08001852 smp_wmb(); /* ensure flags have been cleared before bte_copy */
Dean Nelsona47d5da2008-07-29 22:34:09 -07001853 ch_sn2->w_remote_GP.put = ch_sn2->remote_GP.put;
1854
1855 dev_dbg(xpc_chan, "w_remote_GP.put changed to %ld, partid=%d, "
1856 "channel=%d\n", ch_sn2->w_remote_GP.put, ch->partid,
1857 ch->number);
1858
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07001859 npayloads_sent = xpc_n_of_deliverable_payloads_sn2(ch);
1860 if (npayloads_sent > 0) {
Dean Nelsona47d5da2008-07-29 22:34:09 -07001861 dev_dbg(xpc_chan, "msgs waiting to be copied and "
1862 "delivered=%d, partid=%d, channel=%d\n",
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07001863 npayloads_sent, ch->partid, ch->number);
Dean Nelsona47d5da2008-07-29 22:34:09 -07001864
1865 if (ch->flags & XPC_C_CONNECTEDCALLOUT_MADE)
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07001866 xpc_activate_kthreads(ch, npayloads_sent);
Dean Nelsona47d5da2008-07-29 22:34:09 -07001867 }
1868 }
1869
1870 xpc_msgqueue_deref(ch);
1871}
1872
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07001873static struct xpc_msg_sn2 *
Dean Nelsone17d4162008-07-29 22:34:06 -07001874xpc_pull_remote_msg_sn2(struct xpc_channel *ch, s64 get)
1875{
1876 struct xpc_partition *part = &xpc_partitions[ch->partid];
Dean Nelsona47d5da2008-07-29 22:34:09 -07001877 struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
Dean Nelsona812dcc2008-07-29 22:34:16 -07001878 unsigned long remote_msg_pa;
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07001879 struct xpc_msg_sn2 *msg;
Dean Nelsona812dcc2008-07-29 22:34:16 -07001880 u32 msg_index;
1881 u32 nmsgs;
Dean Nelsone17d4162008-07-29 22:34:06 -07001882 u64 msg_offset;
1883 enum xp_retval ret;
1884
Dean Nelsona47d5da2008-07-29 22:34:09 -07001885 if (mutex_lock_interruptible(&ch_sn2->msg_to_pull_mutex) != 0) {
Dean Nelsone17d4162008-07-29 22:34:06 -07001886 /* we were interrupted by a signal */
1887 return NULL;
1888 }
1889
Dean Nelsona47d5da2008-07-29 22:34:09 -07001890 while (get >= ch_sn2->next_msg_to_pull) {
Dean Nelsone17d4162008-07-29 22:34:06 -07001891
1892 /* pull as many messages as are ready and able to be pulled */
1893
Dean Nelsona47d5da2008-07-29 22:34:09 -07001894 msg_index = ch_sn2->next_msg_to_pull % ch->remote_nentries;
Dean Nelsone17d4162008-07-29 22:34:06 -07001895
Dean Nelsona47d5da2008-07-29 22:34:09 -07001896 DBUG_ON(ch_sn2->next_msg_to_pull >= ch_sn2->w_remote_GP.put);
1897 nmsgs = ch_sn2->w_remote_GP.put - ch_sn2->next_msg_to_pull;
Dean Nelsone17d4162008-07-29 22:34:06 -07001898 if (msg_index + nmsgs > ch->remote_nentries) {
1899 /* ignore the ones that wrap the msg queue for now */
1900 nmsgs = ch->remote_nentries - msg_index;
1901 }
1902
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07001903 msg_offset = msg_index * ch->entry_size;
1904 msg = (struct xpc_msg_sn2 *)((u64)ch_sn2->remote_msgqueue +
Dean Nelson5b8669d2008-07-29 22:34:18 -07001905 msg_offset);
1906 remote_msg_pa = ch_sn2->remote_msgqueue_pa + msg_offset;
Dean Nelsone17d4162008-07-29 22:34:06 -07001907
Dean Nelsona812dcc2008-07-29 22:34:16 -07001908 ret = xpc_pull_remote_cachelines_sn2(part, msg, remote_msg_pa,
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07001909 nmsgs * ch->entry_size);
Dean Nelsone17d4162008-07-29 22:34:06 -07001910 if (ret != xpSuccess) {
1911
1912 dev_dbg(xpc_chan, "failed to pull %d msgs starting with"
1913 " msg %ld from partition %d, channel=%d, "
Dean Nelsona47d5da2008-07-29 22:34:09 -07001914 "ret=%d\n", nmsgs, ch_sn2->next_msg_to_pull,
Dean Nelsone17d4162008-07-29 22:34:06 -07001915 ch->partid, ch->number, ret);
1916
1917 XPC_DEACTIVATE_PARTITION(part, ret);
1918
Dean Nelsona47d5da2008-07-29 22:34:09 -07001919 mutex_unlock(&ch_sn2->msg_to_pull_mutex);
Dean Nelsone17d4162008-07-29 22:34:06 -07001920 return NULL;
1921 }
1922
Dean Nelsona47d5da2008-07-29 22:34:09 -07001923 ch_sn2->next_msg_to_pull += nmsgs;
Dean Nelsone17d4162008-07-29 22:34:06 -07001924 }
1925
Dean Nelsona47d5da2008-07-29 22:34:09 -07001926 mutex_unlock(&ch_sn2->msg_to_pull_mutex);
Dean Nelsone17d4162008-07-29 22:34:06 -07001927
1928 /* return the message we were looking for */
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07001929 msg_offset = (get % ch->remote_nentries) * ch->entry_size;
1930 msg = (struct xpc_msg_sn2 *)((u64)ch_sn2->remote_msgqueue + msg_offset);
Dean Nelsone17d4162008-07-29 22:34:06 -07001931
1932 return msg;
1933}
1934
1935/*
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07001936 * Get the next deliverable message's payload.
Dean Nelsone17d4162008-07-29 22:34:06 -07001937 */
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07001938static void *
1939xpc_get_deliverable_payload_sn2(struct xpc_channel *ch)
Dean Nelsone17d4162008-07-29 22:34:06 -07001940{
Dean Nelsona47d5da2008-07-29 22:34:09 -07001941 struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07001942 struct xpc_msg_sn2 *msg;
1943 void *payload = NULL;
Dean Nelsone17d4162008-07-29 22:34:06 -07001944 s64 get;
1945
1946 do {
1947 if (ch->flags & XPC_C_DISCONNECTING)
1948 break;
1949
Dean Nelsona47d5da2008-07-29 22:34:09 -07001950 get = ch_sn2->w_local_GP.get;
Robin Holt69b3bb62009-01-29 14:25:06 -08001951 smp_rmb(); /* guarantee that .get loads before .put */
Dean Nelsona47d5da2008-07-29 22:34:09 -07001952 if (get == ch_sn2->w_remote_GP.put)
Dean Nelsone17d4162008-07-29 22:34:06 -07001953 break;
1954
1955 /* There are messages waiting to be pulled and delivered.
1956 * We need to try to secure one for ourselves. We'll do this
1957 * by trying to increment w_local_GP.get and hope that no one
1958 * else beats us to it. If they do, we'll we'll simply have
1959 * to try again for the next one.
1960 */
1961
Dean Nelsona47d5da2008-07-29 22:34:09 -07001962 if (cmpxchg(&ch_sn2->w_local_GP.get, get, get + 1) == get) {
Dean Nelsone17d4162008-07-29 22:34:06 -07001963 /* we got the entry referenced by get */
1964
1965 dev_dbg(xpc_chan, "w_local_GP.get changed to %ld, "
1966 "partid=%d, channel=%d\n", get + 1,
1967 ch->partid, ch->number);
1968
1969 /* pull the message from the remote partition */
1970
1971 msg = xpc_pull_remote_msg_sn2(ch, get);
1972
Robin Holt17e21612009-01-29 14:25:07 -08001973 if (msg != NULL) {
1974 DBUG_ON(msg->number != get);
1975 DBUG_ON(msg->flags & XPC_M_SN2_DONE);
1976 DBUG_ON(!(msg->flags & XPC_M_SN2_READY));
Dean Nelsone17d4162008-07-29 22:34:06 -07001977
Robin Holt17e21612009-01-29 14:25:07 -08001978 payload = &msg->payload;
1979 }
Dean Nelsone17d4162008-07-29 22:34:06 -07001980 break;
1981 }
1982
1983 } while (1);
1984
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07001985 return payload;
Dean Nelsone17d4162008-07-29 22:34:06 -07001986}
1987
Dean Nelson33ba3c72008-07-29 22:34:07 -07001988/*
1989 * Now we actually send the messages that are ready to be sent by advancing
Dean Nelson7fb5e592008-07-29 22:34:10 -07001990 * the local message queue's Put value and then send a chctl msgrequest to the
1991 * recipient partition.
Dean Nelson33ba3c72008-07-29 22:34:07 -07001992 */
1993static void
1994xpc_send_msgs_sn2(struct xpc_channel *ch, s64 initial_put)
1995{
Dean Nelsona47d5da2008-07-29 22:34:09 -07001996 struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07001997 struct xpc_msg_sn2 *msg;
Dean Nelson33ba3c72008-07-29 22:34:07 -07001998 s64 put = initial_put + 1;
Dean Nelson7fb5e592008-07-29 22:34:10 -07001999 int send_msgrequest = 0;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002000
2001 while (1) {
2002
2003 while (1) {
Dean Nelsona47d5da2008-07-29 22:34:09 -07002004 if (put == ch_sn2->w_local_GP.put)
Dean Nelson33ba3c72008-07-29 22:34:07 -07002005 break;
2006
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07002007 msg = (struct xpc_msg_sn2 *)((u64)ch_sn2->
2008 local_msgqueue + (put %
2009 ch->local_nentries) *
2010 ch->entry_size);
Dean Nelson33ba3c72008-07-29 22:34:07 -07002011
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07002012 if (!(msg->flags & XPC_M_SN2_READY))
Dean Nelson33ba3c72008-07-29 22:34:07 -07002013 break;
2014
2015 put++;
2016 }
2017
2018 if (put == initial_put) {
2019 /* nothing's changed */
2020 break;
2021 }
2022
Dean Nelsona47d5da2008-07-29 22:34:09 -07002023 if (cmpxchg_rel(&ch_sn2->local_GP->put, initial_put, put) !=
Dean Nelson33ba3c72008-07-29 22:34:07 -07002024 initial_put) {
2025 /* someone else beat us to it */
Dean Nelsona47d5da2008-07-29 22:34:09 -07002026 DBUG_ON(ch_sn2->local_GP->put < initial_put);
Dean Nelson33ba3c72008-07-29 22:34:07 -07002027 break;
2028 }
2029
2030 /* we just set the new value of local_GP->put */
2031
2032 dev_dbg(xpc_chan, "local_GP->put changed to %ld, partid=%d, "
2033 "channel=%d\n", put, ch->partid, ch->number);
2034
Dean Nelson7fb5e592008-07-29 22:34:10 -07002035 send_msgrequest = 1;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002036
2037 /*
2038 * We need to ensure that the message referenced by
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07002039 * local_GP->put is not XPC_M_SN2_READY or that local_GP->put
Dean Nelson33ba3c72008-07-29 22:34:07 -07002040 * equals w_local_GP.put, so we'll go have a look.
2041 */
2042 initial_put = put;
2043 }
2044
Dean Nelson7fb5e592008-07-29 22:34:10 -07002045 if (send_msgrequest)
2046 xpc_send_chctl_msgrequest_sn2(ch);
Dean Nelson33ba3c72008-07-29 22:34:07 -07002047}
2048
2049/*
2050 * Allocate an entry for a message from the message queue associated with the
2051 * specified channel.
2052 */
2053static enum xp_retval
2054xpc_allocate_msg_sn2(struct xpc_channel *ch, u32 flags,
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07002055 struct xpc_msg_sn2 **address_of_msg)
Dean Nelson33ba3c72008-07-29 22:34:07 -07002056{
Dean Nelsona47d5da2008-07-29 22:34:09 -07002057 struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07002058 struct xpc_msg_sn2 *msg;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002059 enum xp_retval ret;
2060 s64 put;
2061
Dean Nelson33ba3c72008-07-29 22:34:07 -07002062 /*
2063 * Get the next available message entry from the local message queue.
2064 * If none are available, we'll make sure that we grab the latest
2065 * GP values.
2066 */
2067 ret = xpTimeout;
2068
2069 while (1) {
2070
Dean Nelsona47d5da2008-07-29 22:34:09 -07002071 put = ch_sn2->w_local_GP.put;
Robin Holt69b3bb62009-01-29 14:25:06 -08002072 smp_rmb(); /* guarantee that .put loads before .get */
Dean Nelsona47d5da2008-07-29 22:34:09 -07002073 if (put - ch_sn2->w_remote_GP.get < ch->local_nentries) {
Dean Nelson33ba3c72008-07-29 22:34:07 -07002074
2075 /* There are available message entries. We need to try
2076 * to secure one for ourselves. We'll do this by trying
2077 * to increment w_local_GP.put as long as someone else
2078 * doesn't beat us to it. If they do, we'll have to
2079 * try again.
2080 */
Dean Nelsona47d5da2008-07-29 22:34:09 -07002081 if (cmpxchg(&ch_sn2->w_local_GP.put, put, put + 1) ==
2082 put) {
Dean Nelson33ba3c72008-07-29 22:34:07 -07002083 /* we got the entry referenced by put */
2084 break;
2085 }
2086 continue; /* try again */
2087 }
2088
2089 /*
2090 * There aren't any available msg entries at this time.
2091 *
2092 * In waiting for a message entry to become available,
Dean Nelson7fb5e592008-07-29 22:34:10 -07002093 * we set a timeout in case the other side is not sending
2094 * completion interrupts. This lets us fake a notify IRQ
2095 * that will cause the notify IRQ handler to fetch the latest
2096 * GP values as if an interrupt was sent by the other side.
Dean Nelson33ba3c72008-07-29 22:34:07 -07002097 */
2098 if (ret == xpTimeout)
Dean Nelson7fb5e592008-07-29 22:34:10 -07002099 xpc_send_chctl_local_msgrequest_sn2(ch);
Dean Nelson33ba3c72008-07-29 22:34:07 -07002100
Dean Nelson97bf1aa2008-07-29 22:34:08 -07002101 if (flags & XPC_NOWAIT)
Dean Nelson33ba3c72008-07-29 22:34:07 -07002102 return xpNoWait;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002103
2104 ret = xpc_allocate_msg_wait(ch);
Dean Nelson97bf1aa2008-07-29 22:34:08 -07002105 if (ret != xpInterrupted && ret != xpTimeout)
Dean Nelson33ba3c72008-07-29 22:34:07 -07002106 return ret;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002107 }
2108
2109 /* get the message's address and initialize it */
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07002110 msg = (struct xpc_msg_sn2 *)((u64)ch_sn2->local_msgqueue +
2111 (put % ch->local_nentries) *
2112 ch->entry_size);
Dean Nelson33ba3c72008-07-29 22:34:07 -07002113
2114 DBUG_ON(msg->flags != 0);
2115 msg->number = put;
2116
2117 dev_dbg(xpc_chan, "w_local_GP.put changed to %ld; msg=0x%p, "
2118 "msg_number=%ld, partid=%d, channel=%d\n", put + 1,
2119 (void *)msg, msg->number, ch->partid, ch->number);
2120
2121 *address_of_msg = msg;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002122 return xpSuccess;
2123}
2124
2125/*
2126 * Common code that does the actual sending of the message by advancing the
Dean Nelson7fb5e592008-07-29 22:34:10 -07002127 * local message queue's Put value and sends a chctl msgrequest to the
2128 * partition the message is being sent to.
Dean Nelson33ba3c72008-07-29 22:34:07 -07002129 */
2130static enum xp_retval
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07002131xpc_send_payload_sn2(struct xpc_channel *ch, u32 flags, void *payload,
2132 u16 payload_size, u8 notify_type, xpc_notify_func func,
2133 void *key)
Dean Nelson33ba3c72008-07-29 22:34:07 -07002134{
2135 enum xp_retval ret = xpSuccess;
Dean Nelson5b8669d2008-07-29 22:34:18 -07002136 struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07002137 struct xpc_msg_sn2 *msg = msg;
2138 struct xpc_notify_sn2 *notify = notify;
Dean Nelson97bf1aa2008-07-29 22:34:08 -07002139 s64 msg_number;
2140 s64 put;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002141
2142 DBUG_ON(notify_type == XPC_N_CALL && func == NULL);
Dean Nelson97bf1aa2008-07-29 22:34:08 -07002143
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07002144 if (XPC_MSG_SIZE(payload_size) > ch->entry_size)
Dean Nelson97bf1aa2008-07-29 22:34:08 -07002145 return xpPayloadTooBig;
2146
2147 xpc_msgqueue_ref(ch);
Dean Nelson33ba3c72008-07-29 22:34:07 -07002148
2149 if (ch->flags & XPC_C_DISCONNECTING) {
Dean Nelson97bf1aa2008-07-29 22:34:08 -07002150 ret = ch->reason;
2151 goto out_1;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002152 }
Dean Nelson97bf1aa2008-07-29 22:34:08 -07002153 if (!(ch->flags & XPC_C_CONNECTED)) {
2154 ret = xpNotConnected;
2155 goto out_1;
2156 }
2157
2158 ret = xpc_allocate_msg_sn2(ch, flags, &msg);
2159 if (ret != xpSuccess)
2160 goto out_1;
2161
2162 msg_number = msg->number;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002163
2164 if (notify_type != 0) {
2165 /*
2166 * Tell the remote side to send an ACK interrupt when the
2167 * message has been delivered.
2168 */
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07002169 msg->flags |= XPC_M_SN2_INTERRUPT;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002170
2171 atomic_inc(&ch->n_to_notify);
2172
Dean Nelson5b8669d2008-07-29 22:34:18 -07002173 notify = &ch_sn2->notify_queue[msg_number % ch->local_nentries];
Dean Nelson33ba3c72008-07-29 22:34:07 -07002174 notify->func = func;
2175 notify->key = key;
2176 notify->type = notify_type;
2177
Dean Nelsonea57f802008-07-29 22:34:14 -07002178 /* ??? Is a mb() needed here? */
Dean Nelson33ba3c72008-07-29 22:34:07 -07002179
2180 if (ch->flags & XPC_C_DISCONNECTING) {
2181 /*
2182 * An error occurred between our last error check and
2183 * this one. We will try to clear the type field from
2184 * the notify entry. If we succeed then
2185 * xpc_disconnect_channel() didn't already process
2186 * the notify entry.
2187 */
2188 if (cmpxchg(&notify->type, notify_type, 0) ==
2189 notify_type) {
2190 atomic_dec(&ch->n_to_notify);
2191 ret = ch->reason;
2192 }
Dean Nelson97bf1aa2008-07-29 22:34:08 -07002193 goto out_1;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002194 }
2195 }
2196
Dean Nelson97bf1aa2008-07-29 22:34:08 -07002197 memcpy(&msg->payload, payload, payload_size);
2198
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07002199 msg->flags |= XPC_M_SN2_READY;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002200
2201 /*
2202 * The preceding store of msg->flags must occur before the following
Dean Nelsona47d5da2008-07-29 22:34:09 -07002203 * load of local_GP->put.
Dean Nelson33ba3c72008-07-29 22:34:07 -07002204 */
Robin Holt69b3bb62009-01-29 14:25:06 -08002205 smp_mb();
Dean Nelson33ba3c72008-07-29 22:34:07 -07002206
2207 /* see if the message is next in line to be sent, if so send it */
2208
Dean Nelson5b8669d2008-07-29 22:34:18 -07002209 put = ch_sn2->local_GP->put;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002210 if (put == msg_number)
2211 xpc_send_msgs_sn2(ch, put);
2212
Dean Nelson97bf1aa2008-07-29 22:34:08 -07002213out_1:
Dean Nelson33ba3c72008-07-29 22:34:07 -07002214 xpc_msgqueue_deref(ch);
2215 return ret;
2216}
2217
2218/*
2219 * Now we actually acknowledge the messages that have been delivered and ack'd
2220 * by advancing the cached remote message queue's Get value and if requested
Dean Nelson7fb5e592008-07-29 22:34:10 -07002221 * send a chctl msgrequest to the message sender's partition.
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07002222 *
2223 * If a message has XPC_M_SN2_INTERRUPT set, send an interrupt to the partition
2224 * that sent the message.
Dean Nelson33ba3c72008-07-29 22:34:07 -07002225 */
2226static void
2227xpc_acknowledge_msgs_sn2(struct xpc_channel *ch, s64 initial_get, u8 msg_flags)
2228{
Dean Nelsona47d5da2008-07-29 22:34:09 -07002229 struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07002230 struct xpc_msg_sn2 *msg;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002231 s64 get = initial_get + 1;
Dean Nelson7fb5e592008-07-29 22:34:10 -07002232 int send_msgrequest = 0;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002233
2234 while (1) {
2235
2236 while (1) {
Dean Nelsona47d5da2008-07-29 22:34:09 -07002237 if (get == ch_sn2->w_local_GP.get)
Dean Nelson33ba3c72008-07-29 22:34:07 -07002238 break;
2239
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07002240 msg = (struct xpc_msg_sn2 *)((u64)ch_sn2->
2241 remote_msgqueue + (get %
2242 ch->remote_nentries) *
2243 ch->entry_size);
Dean Nelson33ba3c72008-07-29 22:34:07 -07002244
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07002245 if (!(msg->flags & XPC_M_SN2_DONE))
Dean Nelson33ba3c72008-07-29 22:34:07 -07002246 break;
2247
2248 msg_flags |= msg->flags;
2249 get++;
2250 }
2251
2252 if (get == initial_get) {
2253 /* nothing's changed */
2254 break;
2255 }
2256
Dean Nelsona47d5da2008-07-29 22:34:09 -07002257 if (cmpxchg_rel(&ch_sn2->local_GP->get, initial_get, get) !=
Dean Nelson33ba3c72008-07-29 22:34:07 -07002258 initial_get) {
2259 /* someone else beat us to it */
Dean Nelsona47d5da2008-07-29 22:34:09 -07002260 DBUG_ON(ch_sn2->local_GP->get <= initial_get);
Dean Nelson33ba3c72008-07-29 22:34:07 -07002261 break;
2262 }
2263
2264 /* we just set the new value of local_GP->get */
2265
2266 dev_dbg(xpc_chan, "local_GP->get changed to %ld, partid=%d, "
2267 "channel=%d\n", get, ch->partid, ch->number);
2268
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07002269 send_msgrequest = (msg_flags & XPC_M_SN2_INTERRUPT);
Dean Nelson33ba3c72008-07-29 22:34:07 -07002270
2271 /*
2272 * We need to ensure that the message referenced by
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07002273 * local_GP->get is not XPC_M_SN2_DONE or that local_GP->get
Dean Nelson33ba3c72008-07-29 22:34:07 -07002274 * equals w_local_GP.get, so we'll go have a look.
2275 */
2276 initial_get = get;
2277 }
2278
Dean Nelson7fb5e592008-07-29 22:34:10 -07002279 if (send_msgrequest)
2280 xpc_send_chctl_msgrequest_sn2(ch);
Dean Nelson33ba3c72008-07-29 22:34:07 -07002281}
2282
2283static void
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07002284xpc_received_payload_sn2(struct xpc_channel *ch, void *payload)
Dean Nelson33ba3c72008-07-29 22:34:07 -07002285{
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07002286 struct xpc_msg_sn2 *msg;
2287 s64 msg_number;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002288 s64 get;
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07002289
2290 msg = container_of(payload, struct xpc_msg_sn2, payload);
2291 msg_number = msg->number;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002292
2293 dev_dbg(xpc_chan, "msg=0x%p, msg_number=%ld, partid=%d, channel=%d\n",
2294 (void *)msg, msg_number, ch->partid, ch->number);
2295
Robin Holt252523e2009-01-29 14:25:07 -08002296 DBUG_ON((((u64)msg - (u64)ch->sn.sn2.remote_msgqueue) / ch->entry_size) !=
Dean Nelson33ba3c72008-07-29 22:34:07 -07002297 msg_number % ch->remote_nentries);
Robin Holt252523e2009-01-29 14:25:07 -08002298 DBUG_ON(!(msg->flags & XPC_M_SN2_READY));
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07002299 DBUG_ON(msg->flags & XPC_M_SN2_DONE);
Dean Nelson33ba3c72008-07-29 22:34:07 -07002300
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07002301 msg->flags |= XPC_M_SN2_DONE;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002302
2303 /*
2304 * The preceding store of msg->flags must occur before the following
Dean Nelsona47d5da2008-07-29 22:34:09 -07002305 * load of local_GP->get.
Dean Nelson33ba3c72008-07-29 22:34:07 -07002306 */
Robin Holt69b3bb62009-01-29 14:25:06 -08002307 smp_mb();
Dean Nelson33ba3c72008-07-29 22:34:07 -07002308
2309 /*
2310 * See if this message is next in line to be acknowledged as having
2311 * been delivered.
2312 */
Dean Nelsona47d5da2008-07-29 22:34:09 -07002313 get = ch->sn.sn2.local_GP->get;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002314 if (get == msg_number)
2315 xpc_acknowledge_msgs_sn2(ch, get, msg->flags);
2316}
2317
Dean Nelson6e41017a2008-07-29 22:34:09 -07002318int
Dean Nelson94bd2702008-07-29 22:34:05 -07002319xpc_init_sn2(void)
2320{
Dean Nelson6e41017a2008-07-29 22:34:09 -07002321 int ret;
Dean Nelsonee6665e2008-07-29 22:34:13 -07002322 size_t buf_size;
Dean Nelson6e41017a2008-07-29 22:34:09 -07002323
Dean Nelson5b8669d2008-07-29 22:34:18 -07002324 xpc_setup_partitions_sn = xpc_setup_partitions_sn_sn2;
Jack Steiner6f2584f2009-04-02 16:59:10 -07002325 xpc_teardown_partitions_sn = xpc_teardown_partitions_sn_sn2;
Dean Nelson261f3b42008-07-29 22:34:16 -07002326 xpc_get_partition_rsvd_page_pa = xpc_get_partition_rsvd_page_pa_sn2;
Dean Nelson5b8669d2008-07-29 22:34:18 -07002327 xpc_setup_rsvd_page_sn = xpc_setup_rsvd_page_sn_sn2;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002328 xpc_increment_heartbeat = xpc_increment_heartbeat_sn2;
2329 xpc_offline_heartbeat = xpc_offline_heartbeat_sn2;
2330 xpc_online_heartbeat = xpc_online_heartbeat_sn2;
2331 xpc_heartbeat_init = xpc_heartbeat_init_sn2;
2332 xpc_heartbeat_exit = xpc_heartbeat_exit_sn2;
Dean Nelson61deb862008-07-29 22:34:17 -07002333 xpc_get_remote_heartbeat = xpc_get_remote_heartbeat_sn2;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002334
Dean Nelsona47d5da2008-07-29 22:34:09 -07002335 xpc_request_partition_activation = xpc_request_partition_activation_sn2;
2336 xpc_request_partition_reactivation =
2337 xpc_request_partition_reactivation_sn2;
2338 xpc_request_partition_deactivation =
2339 xpc_request_partition_deactivation_sn2;
2340 xpc_cancel_partition_deactivation_request =
2341 xpc_cancel_partition_deactivation_request_sn2;
2342
Dean Nelson6e41017a2008-07-29 22:34:09 -07002343 xpc_process_activate_IRQ_rcvd = xpc_process_activate_IRQ_rcvd_sn2;
Dean Nelson5b8669d2008-07-29 22:34:18 -07002344 xpc_setup_ch_structures_sn = xpc_setup_ch_structures_sn_sn2;
2345 xpc_teardown_ch_structures_sn = xpc_teardown_ch_structures_sn_sn2;
Dean Nelsone17d4162008-07-29 22:34:06 -07002346 xpc_make_first_contact = xpc_make_first_contact_sn2;
Dean Nelson5b8669d2008-07-29 22:34:18 -07002347
Dean Nelson7fb5e592008-07-29 22:34:10 -07002348 xpc_get_chctl_all_flags = xpc_get_chctl_all_flags_sn2;
Dean Nelson5b8669d2008-07-29 22:34:18 -07002349 xpc_send_chctl_closerequest = xpc_send_chctl_closerequest_sn2;
2350 xpc_send_chctl_closereply = xpc_send_chctl_closereply_sn2;
2351 xpc_send_chctl_openrequest = xpc_send_chctl_openrequest_sn2;
2352 xpc_send_chctl_openreply = xpc_send_chctl_openreply_sn2;
2353
2354 xpc_save_remote_msgqueue_pa = xpc_save_remote_msgqueue_pa_sn2;
2355
2356 xpc_setup_msg_structures = xpc_setup_msg_structures_sn2;
2357 xpc_teardown_msg_structures = xpc_teardown_msg_structures_sn2;
2358
Dean Nelsona47d5da2008-07-29 22:34:09 -07002359 xpc_notify_senders_of_disconnect = xpc_notify_senders_of_disconnect_sn2;
Dean Nelson7fb5e592008-07-29 22:34:10 -07002360 xpc_process_msg_chctl_flags = xpc_process_msg_chctl_flags_sn2;
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07002361 xpc_n_of_deliverable_payloads = xpc_n_of_deliverable_payloads_sn2;
2362 xpc_get_deliverable_payload = xpc_get_deliverable_payload_sn2;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002363
Dean Nelsona47d5da2008-07-29 22:34:09 -07002364 xpc_indicate_partition_engaged = xpc_indicate_partition_engaged_sn2;
Dean Nelsona47d5da2008-07-29 22:34:09 -07002365 xpc_indicate_partition_disengaged =
2366 xpc_indicate_partition_disengaged_sn2;
Dean Nelson5b8669d2008-07-29 22:34:18 -07002367 xpc_partition_engaged = xpc_partition_engaged_sn2;
2368 xpc_any_partition_engaged = xpc_any_partition_engaged_sn2;
Dean Nelsona47d5da2008-07-29 22:34:09 -07002369 xpc_assume_partition_disengaged = xpc_assume_partition_disengaged_sn2;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002370
Dean Nelsonbd3e64c2008-07-29 22:34:19 -07002371 xpc_send_payload = xpc_send_payload_sn2;
2372 xpc_received_payload = xpc_received_payload_sn2;
2373
2374 if (offsetof(struct xpc_msg_sn2, payload) > XPC_MSG_HDR_MAX_SIZE) {
2375 dev_err(xpc_part, "header portion of struct xpc_msg_sn2 is "
2376 "larger than %d\n", XPC_MSG_HDR_MAX_SIZE);
2377 return -E2BIG;
2378 }
Dean Nelson6e41017a2008-07-29 22:34:09 -07002379
Dean Nelsonee6665e2008-07-29 22:34:13 -07002380 buf_size = max(XPC_RP_VARS_SIZE,
2381 XPC_RP_HEADER_SIZE + XP_NASID_MASK_BYTES_SN2);
2382 xpc_remote_copy_buffer_sn2 = xpc_kmalloc_cacheline_aligned(buf_size,
2383 GFP_KERNEL,
2384 &xpc_remote_copy_buffer_base_sn2);
2385 if (xpc_remote_copy_buffer_sn2 == NULL) {
2386 dev_err(xpc_part, "can't get memory for remote copy buffer\n");
2387 return -ENOMEM;
2388 }
2389
Dean Nelsonc39838c2008-07-29 22:34:11 -07002390 /* open up protections for IPI and [potentially] amo operations */
Dean Nelson6e41017a2008-07-29 22:34:09 -07002391 xpc_allow_IPI_ops_sn2();
Dean Nelsonc39838c2008-07-29 22:34:11 -07002392 xpc_allow_amo_ops_shub_wars_1_1_sn2();
Dean Nelson6e41017a2008-07-29 22:34:09 -07002393
2394 /*
2395 * This is safe to do before the xpc_hb_checker thread has started
2396 * because the handler releases a wait queue. If an interrupt is
2397 * received before the thread is waiting, it will not go to sleep,
2398 * but rather immediately process the interrupt.
2399 */
2400 ret = request_irq(SGI_XPC_ACTIVATE, xpc_handle_activate_IRQ_sn2, 0,
2401 "xpc hb", NULL);
2402 if (ret != 0) {
2403 dev_err(xpc_part, "can't register ACTIVATE IRQ handler, "
2404 "errno=%d\n", -ret);
2405 xpc_disallow_IPI_ops_sn2();
Dean Nelsonee6665e2008-07-29 22:34:13 -07002406 kfree(xpc_remote_copy_buffer_base_sn2);
Dean Nelson6e41017a2008-07-29 22:34:09 -07002407 }
2408 return ret;
Dean Nelson94bd2702008-07-29 22:34:05 -07002409}
2410
2411void
2412xpc_exit_sn2(void)
2413{
Dean Nelson6e41017a2008-07-29 22:34:09 -07002414 free_irq(SGI_XPC_ACTIVATE, NULL);
2415 xpc_disallow_IPI_ops_sn2();
Dean Nelsonee6665e2008-07-29 22:34:13 -07002416 kfree(xpc_remote_copy_buffer_base_sn2);
Dean Nelson94bd2702008-07-29 22:34:05 -07002417}