blob: 0fef7d86a5a2fe696293505f2012cf53c92f65af [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
16#include <linux/kernel.h>
Dean Nelsone17d4162008-07-29 22:34:06 -070017#include <linux/delay.h>
Dean Nelson94bd2702008-07-29 22:34:05 -070018#include <asm/uncached.h>
19#include <asm/sn/sn_sal.h>
20#include "xpc.h"
21
Dean Nelson33ba3c72008-07-29 22:34:07 -070022static struct xpc_vars_sn2 *xpc_vars; /* >>> Add _sn2 suffix? */
Dean Nelsone17d4162008-07-29 22:34:06 -070023static struct xpc_vars_part_sn2 *xpc_vars_part; /* >>> Add _sn2 suffix? */
Dean Nelson94bd2702008-07-29 22:34:05 -070024
Dean Nelson6e410172008-07-29 22:34:09 -070025/* SH_IPI_ACCESS shub register value on startup */
26static u64 xpc_sh1_IPI_access;
27static u64 xpc_sh2_IPI_access0;
28static u64 xpc_sh2_IPI_access1;
29static u64 xpc_sh2_IPI_access2;
30static u64 xpc_sh2_IPI_access3;
31
32/*
33 * Change protections to allow IPI operations.
34 */
35static void
36xpc_allow_IPI_ops_sn2(void)
37{
38 int node;
39 int nasid;
40
41 /* >>> The following should get moved into SAL. */
42 if (is_shub2()) {
43 xpc_sh2_IPI_access0 =
44 (u64)HUB_L((u64 *)LOCAL_MMR_ADDR(SH2_IPI_ACCESS0));
45 xpc_sh2_IPI_access1 =
46 (u64)HUB_L((u64 *)LOCAL_MMR_ADDR(SH2_IPI_ACCESS1));
47 xpc_sh2_IPI_access2 =
48 (u64)HUB_L((u64 *)LOCAL_MMR_ADDR(SH2_IPI_ACCESS2));
49 xpc_sh2_IPI_access3 =
50 (u64)HUB_L((u64 *)LOCAL_MMR_ADDR(SH2_IPI_ACCESS3));
51
52 for_each_online_node(node) {
53 nasid = cnodeid_to_nasid(node);
54 HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS0),
55 -1UL);
56 HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS1),
57 -1UL);
58 HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS2),
59 -1UL);
60 HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS3),
61 -1UL);
62 }
63 } else {
64 xpc_sh1_IPI_access =
65 (u64)HUB_L((u64 *)LOCAL_MMR_ADDR(SH1_IPI_ACCESS));
66
67 for_each_online_node(node) {
68 nasid = cnodeid_to_nasid(node);
69 HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH1_IPI_ACCESS),
70 -1UL);
71 }
72 }
73}
74
75/*
76 * Restrict protections to disallow IPI operations.
77 */
78static void
79xpc_disallow_IPI_ops_sn2(void)
80{
81 int node;
82 int nasid;
83
84 /* >>> The following should get moved into SAL. */
85 if (is_shub2()) {
86 for_each_online_node(node) {
87 nasid = cnodeid_to_nasid(node);
88 HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS0),
89 xpc_sh2_IPI_access0);
90 HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS1),
91 xpc_sh2_IPI_access1);
92 HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS2),
93 xpc_sh2_IPI_access2);
94 HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS3),
95 xpc_sh2_IPI_access3);
96 }
97 } else {
98 for_each_online_node(node) {
99 nasid = cnodeid_to_nasid(node);
100 HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH1_IPI_ACCESS),
101 xpc_sh1_IPI_access);
102 }
103 }
104}
105
Dean Nelson33ba3c72008-07-29 22:34:07 -0700106/*
Dean Nelson7fb5e592008-07-29 22:34:10 -0700107 * The following set of functions are used for the sending and receiving of
108 * IRQs (also known as IPIs). There are two flavors of IRQs, one that is
109 * associated with partition activity (SGI_XPC_ACTIVATE) and the other that
110 * is associated with channel activity (SGI_XPC_NOTIFY).
Dean Nelson33ba3c72008-07-29 22:34:07 -0700111 */
112
113static u64
Dean Nelson7fb5e592008-07-29 22:34:10 -0700114xpc_receive_IRQ_amo_sn2(AMO_t *amo)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700115{
116 return FETCHOP_LOAD_OP(TO_AMO((u64)&amo->variable), FETCHOP_CLEAR);
117}
118
119static enum xp_retval
Dean Nelson7fb5e592008-07-29 22:34:10 -0700120xpc_send_IRQ_sn2(AMO_t *amo, u64 flag, int nasid, int phys_cpuid, int vector)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700121{
122 int ret = 0;
123 unsigned long irq_flags;
124
125 local_irq_save(irq_flags);
126
127 FETCHOP_STORE_OP(TO_AMO((u64)&amo->variable), FETCHOP_OR, flag);
128 sn_send_IPI_phys(nasid, phys_cpuid, vector, 0);
129
130 /*
131 * We must always use the nofault function regardless of whether we
132 * are on a Shub 1.1 system or a Shub 1.2 slice 0xc processor. If we
133 * didn't, we'd never know that the other partition is down and would
Dean Nelson7fb5e592008-07-29 22:34:10 -0700134 * keep sending IRQs and AMOs to it until the heartbeat times out.
Dean Nelson33ba3c72008-07-29 22:34:07 -0700135 */
136 ret = xp_nofault_PIOR((u64 *)GLOBAL_MMR_ADDR(NASID_GET(&amo->variable),
137 xp_nofault_PIOR_target));
138
139 local_irq_restore(irq_flags);
140
141 return ((ret == 0) ? xpSuccess : xpPioReadError);
142}
143
144static AMO_t *
Dean Nelson7fb5e592008-07-29 22:34:10 -0700145xpc_init_IRQ_amo_sn2(int index)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700146{
147 AMO_t *amo = xpc_vars->amos_page + index;
148
Dean Nelson7fb5e592008-07-29 22:34:10 -0700149 (void)xpc_receive_IRQ_amo_sn2(amo); /* clear AMO variable */
Dean Nelson33ba3c72008-07-29 22:34:07 -0700150 return amo;
151}
152
153/*
Dean Nelson7fb5e592008-07-29 22:34:10 -0700154 * Functions associated with SGI_XPC_ACTIVATE IRQ.
Dean Nelson33ba3c72008-07-29 22:34:07 -0700155 */
156
157/*
Dean Nelson6e410172008-07-29 22:34:09 -0700158 * Notify the heartbeat check thread that an activate IRQ has been received.
159 */
160static irqreturn_t
161xpc_handle_activate_IRQ_sn2(int irq, void *dev_id)
162{
163 atomic_inc(&xpc_activate_IRQ_rcvd);
164 wake_up_interruptible(&xpc_activate_IRQ_wq);
165 return IRQ_HANDLED;
166}
167
168/*
Dean Nelson7fb5e592008-07-29 22:34:10 -0700169 * Flag the appropriate AMO variable and send an IRQ to the specified node.
Dean Nelson33ba3c72008-07-29 22:34:07 -0700170 */
171static void
Dean Nelson7fb5e592008-07-29 22:34:10 -0700172xpc_send_activate_IRQ_sn2(u64 amos_page_pa, int from_nasid, int to_nasid,
173 int to_phys_cpuid)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700174{
175 int w_index = XPC_NASID_W_INDEX(from_nasid);
176 int b_index = XPC_NASID_B_INDEX(from_nasid);
177 AMO_t *amos = (AMO_t *)__va(amos_page_pa +
178 (XPC_ACTIVATE_IRQ_AMOS * sizeof(AMO_t)));
179
Dean Nelson7fb5e592008-07-29 22:34:10 -0700180 (void)xpc_send_IRQ_sn2(&amos[w_index], (1UL << b_index), to_nasid,
Dean Nelson33ba3c72008-07-29 22:34:07 -0700181 to_phys_cpuid, SGI_XPC_ACTIVATE);
182}
183
184static void
Dean Nelson7fb5e592008-07-29 22:34:10 -0700185xpc_send_local_activate_IRQ_sn2(int from_nasid)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700186{
187 int w_index = XPC_NASID_W_INDEX(from_nasid);
188 int b_index = XPC_NASID_B_INDEX(from_nasid);
189 AMO_t *amos = (AMO_t *)__va(xpc_vars->amos_page_pa +
190 (XPC_ACTIVATE_IRQ_AMOS * sizeof(AMO_t)));
191
192 /* fake the sending and receipt of an activate IRQ from remote nasid */
193 FETCHOP_STORE_OP(TO_AMO((u64)&amos[w_index].variable), FETCHOP_OR,
194 (1UL << b_index));
Dean Nelson6e410172008-07-29 22:34:09 -0700195 atomic_inc(&xpc_activate_IRQ_rcvd);
196 wake_up_interruptible(&xpc_activate_IRQ_wq);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700197}
198
Dean Nelson33ba3c72008-07-29 22:34:07 -0700199/*
Dean Nelson7fb5e592008-07-29 22:34:10 -0700200 * Functions associated with SGI_XPC_NOTIFY IRQ.
Dean Nelson33ba3c72008-07-29 22:34:07 -0700201 */
202
203/*
Dean Nelson7fb5e592008-07-29 22:34:10 -0700204 * Check to see if any chctl flags were sent from the specified partition.
Dean Nelsona47d5da2008-07-29 22:34:09 -0700205 */
206static void
Dean Nelson7fb5e592008-07-29 22:34:10 -0700207xpc_check_for_sent_chctl_flags_sn2(struct xpc_partition *part)
Dean Nelsona47d5da2008-07-29 22:34:09 -0700208{
Dean Nelson7fb5e592008-07-29 22:34:10 -0700209 union xpc_channel_ctl_flags chctl;
Dean Nelsona47d5da2008-07-29 22:34:09 -0700210 unsigned long irq_flags;
211
Dean Nelson7fb5e592008-07-29 22:34:10 -0700212 chctl.all_flags = xpc_receive_IRQ_amo_sn2(part->sn.sn2.
213 local_chctl_amo_va);
214 if (chctl.all_flags == 0)
Dean Nelsona47d5da2008-07-29 22:34:09 -0700215 return;
216
Dean Nelson7fb5e592008-07-29 22:34:10 -0700217 spin_lock_irqsave(&part->chctl_lock, irq_flags);
218 part->chctl.all_flags |= chctl.all_flags;
219 spin_unlock_irqrestore(&part->chctl_lock, irq_flags);
Dean Nelsona47d5da2008-07-29 22:34:09 -0700220
Dean Nelson7fb5e592008-07-29 22:34:10 -0700221 dev_dbg(xpc_chan, "received notify IRQ from partid=%d, chctl.all_flags="
222 "0x%lx\n", XPC_PARTID(part), chctl.all_flags);
Dean Nelsona47d5da2008-07-29 22:34:09 -0700223
224 xpc_wakeup_channel_mgr(part);
225}
226
227/*
228 * Handle the receipt of a SGI_XPC_NOTIFY IRQ by seeing whether the specified
229 * partition actually sent it. Since SGI_XPC_NOTIFY IRQs may be shared by more
230 * than one partition, we use an AMO_t structure per partition to indicate
Dean Nelson7fb5e592008-07-29 22:34:10 -0700231 * whether a partition has sent an IRQ or not. If it has, then wake up the
Dean Nelsona47d5da2008-07-29 22:34:09 -0700232 * associated kthread to handle it.
233 *
Dean Nelson7fb5e592008-07-29 22:34:10 -0700234 * All SGI_XPC_NOTIFY IRQs received by XPC are the result of IRQs sent by XPC
Dean Nelsona47d5da2008-07-29 22:34:09 -0700235 * running on other partitions.
236 *
237 * Noteworthy Arguments:
238 *
239 * irq - Interrupt ReQuest number. NOT USED.
240 *
Dean Nelson7fb5e592008-07-29 22:34:10 -0700241 * dev_id - partid of IRQ's potential sender.
Dean Nelsona47d5da2008-07-29 22:34:09 -0700242 */
243static irqreturn_t
244xpc_handle_notify_IRQ_sn2(int irq, void *dev_id)
245{
246 short partid = (short)(u64)dev_id;
247 struct xpc_partition *part = &xpc_partitions[partid];
248
249 DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
250
251 if (xpc_part_ref(part)) {
Dean Nelson7fb5e592008-07-29 22:34:10 -0700252 xpc_check_for_sent_chctl_flags_sn2(part);
Dean Nelsona47d5da2008-07-29 22:34:09 -0700253
254 xpc_part_deref(part);
255 }
256 return IRQ_HANDLED;
257}
258
259/*
Dean Nelson7fb5e592008-07-29 22:34:10 -0700260 * Check to see if xpc_handle_notify_IRQ_sn2() dropped any IRQs on the floor
261 * because the write to their associated amo variable completed after the IRQ
Dean Nelsona47d5da2008-07-29 22:34:09 -0700262 * was received.
263 */
264static void
Dean Nelson7fb5e592008-07-29 22:34:10 -0700265xpc_check_for_dropped_notify_IRQ_sn2(struct xpc_partition *part)
Dean Nelsona47d5da2008-07-29 22:34:09 -0700266{
267 struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
268
269 if (xpc_part_ref(part)) {
Dean Nelson7fb5e592008-07-29 22:34:10 -0700270 xpc_check_for_sent_chctl_flags_sn2(part);
Dean Nelsona47d5da2008-07-29 22:34:09 -0700271
272 part_sn2->dropped_notify_IRQ_timer.expires = jiffies +
Dean Nelson7fb5e592008-07-29 22:34:10 -0700273 XPC_DROPPED_NOTIFY_IRQ_WAIT_INTERVAL;
Dean Nelsona47d5da2008-07-29 22:34:09 -0700274 add_timer(&part_sn2->dropped_notify_IRQ_timer);
275 xpc_part_deref(part);
276 }
277}
278
279/*
Dean Nelson7fb5e592008-07-29 22:34:10 -0700280 * Send a notify IRQ to the remote partition that is associated with the
Dean Nelson33ba3c72008-07-29 22:34:07 -0700281 * specified channel.
282 */
283static void
Dean Nelson7fb5e592008-07-29 22:34:10 -0700284xpc_send_notify_IRQ_sn2(struct xpc_channel *ch, u8 chctl_flag,
285 char *chctl_flag_string, unsigned long *irq_flags)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700286{
287 struct xpc_partition *part = &xpc_partitions[ch->partid];
Dean Nelsona47d5da2008-07-29 22:34:09 -0700288 struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
Dean Nelson7fb5e592008-07-29 22:34:10 -0700289 union xpc_channel_ctl_flags chctl = { 0 };
Dean Nelson33ba3c72008-07-29 22:34:07 -0700290 enum xp_retval ret;
291
292 if (likely(part->act_state != XPC_P_DEACTIVATING)) {
Dean Nelson7fb5e592008-07-29 22:34:10 -0700293 chctl.flags[ch->number] = chctl_flag;
294 ret = xpc_send_IRQ_sn2(part_sn2->remote_chctl_amo_va,
295 chctl.all_flags,
296 part_sn2->notify_IRQ_nasid,
297 part_sn2->notify_IRQ_phys_cpuid,
Dean Nelson33ba3c72008-07-29 22:34:07 -0700298 SGI_XPC_NOTIFY);
299 dev_dbg(xpc_chan, "%s sent to partid=%d, channel=%d, ret=%d\n",
Dean Nelson7fb5e592008-07-29 22:34:10 -0700300 chctl_flag_string, ch->partid, ch->number, ret);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700301 if (unlikely(ret != xpSuccess)) {
302 if (irq_flags != NULL)
303 spin_unlock_irqrestore(&ch->lock, *irq_flags);
304 XPC_DEACTIVATE_PARTITION(part, ret);
305 if (irq_flags != NULL)
306 spin_lock_irqsave(&ch->lock, *irq_flags);
307 }
308 }
309}
310
Dean Nelson7fb5e592008-07-29 22:34:10 -0700311#define XPC_SEND_NOTIFY_IRQ_SN2(_ch, _ipi_f, _irq_f) \
312 xpc_send_notify_IRQ_sn2(_ch, _ipi_f, #_ipi_f, _irq_f)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700313
314/*
315 * Make it look like the remote partition, which is associated with the
Dean Nelson7fb5e592008-07-29 22:34:10 -0700316 * specified channel, sent us a notify IRQ. This faked IRQ will be handled
317 * by xpc_check_for_dropped_notify_IRQ_sn2().
Dean Nelson33ba3c72008-07-29 22:34:07 -0700318 */
319static void
Dean Nelson7fb5e592008-07-29 22:34:10 -0700320xpc_send_local_notify_IRQ_sn2(struct xpc_channel *ch, u8 chctl_flag,
321 char *chctl_flag_string)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700322{
323 struct xpc_partition *part = &xpc_partitions[ch->partid];
Dean Nelson7fb5e592008-07-29 22:34:10 -0700324 union xpc_channel_ctl_flags chctl = { 0 };
Dean Nelson33ba3c72008-07-29 22:34:07 -0700325
Dean Nelson7fb5e592008-07-29 22:34:10 -0700326 chctl.flags[ch->number] = chctl_flag;
327 FETCHOP_STORE_OP(TO_AMO((u64)&part->sn.sn2.local_chctl_amo_va->
328 variable), FETCHOP_OR, chctl.all_flags);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700329 dev_dbg(xpc_chan, "%s sent local from partid=%d, channel=%d\n",
Dean Nelson7fb5e592008-07-29 22:34:10 -0700330 chctl_flag_string, ch->partid, ch->number);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700331}
332
Dean Nelson7fb5e592008-07-29 22:34:10 -0700333#define XPC_SEND_LOCAL_NOTIFY_IRQ_SN2(_ch, _ipi_f) \
334 xpc_send_local_notify_IRQ_sn2(_ch, _ipi_f, #_ipi_f)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700335
336static void
Dean Nelson7fb5e592008-07-29 22:34:10 -0700337xpc_send_chctl_closerequest_sn2(struct xpc_channel *ch,
338 unsigned long *irq_flags)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700339{
340 struct xpc_openclose_args *args = ch->local_openclose_args;
341
342 args->reason = ch->reason;
Dean Nelson7fb5e592008-07-29 22:34:10 -0700343 XPC_SEND_NOTIFY_IRQ_SN2(ch, XPC_CHCTL_CLOSEREQUEST, irq_flags);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700344}
345
346static void
Dean Nelson7fb5e592008-07-29 22:34:10 -0700347xpc_send_chctl_closereply_sn2(struct xpc_channel *ch, unsigned long *irq_flags)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700348{
Dean Nelson7fb5e592008-07-29 22:34:10 -0700349 XPC_SEND_NOTIFY_IRQ_SN2(ch, XPC_CHCTL_CLOSEREPLY, irq_flags);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700350}
351
352static void
Dean Nelson7fb5e592008-07-29 22:34:10 -0700353xpc_send_chctl_openrequest_sn2(struct xpc_channel *ch, unsigned long *irq_flags)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700354{
355 struct xpc_openclose_args *args = ch->local_openclose_args;
356
357 args->msg_size = ch->msg_size;
358 args->local_nentries = ch->local_nentries;
Dean Nelson7fb5e592008-07-29 22:34:10 -0700359 XPC_SEND_NOTIFY_IRQ_SN2(ch, XPC_CHCTL_OPENREQUEST, irq_flags);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700360}
361
362static void
Dean Nelson7fb5e592008-07-29 22:34:10 -0700363xpc_send_chctl_openreply_sn2(struct xpc_channel *ch, unsigned long *irq_flags)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700364{
365 struct xpc_openclose_args *args = ch->local_openclose_args;
366
367 args->remote_nentries = ch->remote_nentries;
368 args->local_nentries = ch->local_nentries;
369 args->local_msgqueue_pa = __pa(ch->local_msgqueue);
Dean Nelson7fb5e592008-07-29 22:34:10 -0700370 XPC_SEND_NOTIFY_IRQ_SN2(ch, XPC_CHCTL_OPENREPLY, irq_flags);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700371}
372
373static void
Dean Nelson7fb5e592008-07-29 22:34:10 -0700374xpc_send_chctl_msgrequest_sn2(struct xpc_channel *ch)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700375{
Dean Nelson7fb5e592008-07-29 22:34:10 -0700376 XPC_SEND_NOTIFY_IRQ_SN2(ch, XPC_CHCTL_MSGREQUEST, NULL);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700377}
378
379static void
Dean Nelson7fb5e592008-07-29 22:34:10 -0700380xpc_send_chctl_local_msgrequest_sn2(struct xpc_channel *ch)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700381{
Dean Nelson7fb5e592008-07-29 22:34:10 -0700382 XPC_SEND_LOCAL_NOTIFY_IRQ_SN2(ch, XPC_CHCTL_MSGREQUEST);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700383}
384
385/*
386 * This next set of functions are used to keep track of when a partition is
387 * potentially engaged in accessing memory belonging to another partition.
388 */
389
390static void
Dean Nelsona47d5da2008-07-29 22:34:09 -0700391xpc_indicate_partition_engaged_sn2(struct xpc_partition *part)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700392{
393 unsigned long irq_flags;
Dean Nelsona47d5da2008-07-29 22:34:09 -0700394 AMO_t *amo = (AMO_t *)__va(part->sn.sn2.remote_amos_page_pa +
Dean Nelson33ba3c72008-07-29 22:34:07 -0700395 (XPC_ENGAGED_PARTITIONS_AMO *
396 sizeof(AMO_t)));
397
398 local_irq_save(irq_flags);
399
400 /* set bit corresponding to our partid in remote partition's AMO */
401 FETCHOP_STORE_OP(TO_AMO((u64)&amo->variable), FETCHOP_OR,
402 (1UL << sn_partition_id));
403 /*
404 * We must always use the nofault function regardless of whether we
405 * are on a Shub 1.1 system or a Shub 1.2 slice 0xc processor. If we
406 * didn't, we'd never know that the other partition is down and would
Dean Nelson7fb5e592008-07-29 22:34:10 -0700407 * keep sending IRQs and AMOs to it until the heartbeat times out.
Dean Nelson33ba3c72008-07-29 22:34:07 -0700408 */
409 (void)xp_nofault_PIOR((u64 *)GLOBAL_MMR_ADDR(NASID_GET(&amo->
410 variable),
411 xp_nofault_PIOR_target));
412
413 local_irq_restore(irq_flags);
414}
415
416static void
Dean Nelsona47d5da2008-07-29 22:34:09 -0700417xpc_indicate_partition_disengaged_sn2(struct xpc_partition *part)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700418{
Dean Nelsona47d5da2008-07-29 22:34:09 -0700419 struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700420 unsigned long irq_flags;
Dean Nelsona47d5da2008-07-29 22:34:09 -0700421 AMO_t *amo = (AMO_t *)__va(part_sn2->remote_amos_page_pa +
Dean Nelson33ba3c72008-07-29 22:34:07 -0700422 (XPC_ENGAGED_PARTITIONS_AMO *
423 sizeof(AMO_t)));
424
425 local_irq_save(irq_flags);
426
427 /* clear bit corresponding to our partid in remote partition's AMO */
428 FETCHOP_STORE_OP(TO_AMO((u64)&amo->variable), FETCHOP_AND,
429 ~(1UL << sn_partition_id));
430 /*
431 * We must always use the nofault function regardless of whether we
432 * are on a Shub 1.1 system or a Shub 1.2 slice 0xc processor. If we
433 * didn't, we'd never know that the other partition is down and would
Dean Nelson7fb5e592008-07-29 22:34:10 -0700434 * keep sending IRQs and AMOs to it until the heartbeat times out.
Dean Nelson33ba3c72008-07-29 22:34:07 -0700435 */
436 (void)xp_nofault_PIOR((u64 *)GLOBAL_MMR_ADDR(NASID_GET(&amo->
437 variable),
438 xp_nofault_PIOR_target));
439
440 local_irq_restore(irq_flags);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700441
Dean Nelson33ba3c72008-07-29 22:34:07 -0700442 /*
Dean Nelsona47d5da2008-07-29 22:34:09 -0700443 * Send activate IRQ to get other side to see that we've cleared our
444 * bit in their engaged partitions AMO.
Dean Nelson33ba3c72008-07-29 22:34:07 -0700445 */
Dean Nelson7fb5e592008-07-29 22:34:10 -0700446 xpc_send_activate_IRQ_sn2(part_sn2->remote_amos_page_pa,
Dean Nelsona47d5da2008-07-29 22:34:09 -0700447 cnodeid_to_nasid(0),
448 part_sn2->activate_IRQ_nasid,
449 part_sn2->activate_IRQ_phys_cpuid);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700450}
451
Dean Nelsona47d5da2008-07-29 22:34:09 -0700452static int
453xpc_partition_engaged_sn2(short partid)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700454{
455 AMO_t *amo = xpc_vars->amos_page + XPC_ENGAGED_PARTITIONS_AMO;
456
Dean Nelsona47d5da2008-07-29 22:34:09 -0700457 /* our partition's AMO variable ANDed with partid mask */
Dean Nelson33ba3c72008-07-29 22:34:07 -0700458 return (FETCHOP_LOAD_OP(TO_AMO((u64)&amo->variable), FETCHOP_LOAD) &
Dean Nelsona47d5da2008-07-29 22:34:09 -0700459 (1UL << partid)) != 0;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700460}
461
Dean Nelsona47d5da2008-07-29 22:34:09 -0700462static int
463xpc_any_partition_engaged_sn2(void)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700464{
465 AMO_t *amo = xpc_vars->amos_page + XPC_ENGAGED_PARTITIONS_AMO;
466
Dean Nelsona47d5da2008-07-29 22:34:09 -0700467 /* our partition's AMO variable */
468 return FETCHOP_LOAD_OP(TO_AMO((u64)&amo->variable), FETCHOP_LOAD) != 0;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700469}
470
471static void
Dean Nelsona47d5da2008-07-29 22:34:09 -0700472xpc_assume_partition_disengaged_sn2(short partid)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700473{
Dean Nelsona47d5da2008-07-29 22:34:09 -0700474 AMO_t *amo = xpc_vars->amos_page + XPC_ENGAGED_PARTITIONS_AMO;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700475
Dean Nelsona47d5da2008-07-29 22:34:09 -0700476 /* clear bit(s) based on partid mask in our partition's AMO */
Dean Nelson33ba3c72008-07-29 22:34:07 -0700477 FETCHOP_STORE_OP(TO_AMO((u64)&amo->variable), FETCHOP_AND,
Dean Nelsona47d5da2008-07-29 22:34:09 -0700478 ~(1UL << partid));
Dean Nelson33ba3c72008-07-29 22:34:07 -0700479}
480
Dean Nelson6e410172008-07-29 22:34:09 -0700481/* original protection values for each node */
482static u64 xpc_prot_vec_sn2[MAX_NUMNODES];
483
484/*
485 * Change protections to allow AMO operations on non-Shub 1.1 systems.
486 */
487static enum xp_retval
488xpc_allow_AMO_ops_sn2(AMO_t *amos_page)
489{
490 u64 nasid_array = 0;
491 int ret;
492
493 /*
494 * On SHUB 1.1, we cannot call sn_change_memprotect() since the BIST
495 * collides with memory operations. On those systems we call
496 * xpc_allow_AMO_ops_shub_wars_1_1_sn2() instead.
497 */
498 if (!enable_shub_wars_1_1()) {
499 ret = sn_change_memprotect(ia64_tpa((u64)amos_page), PAGE_SIZE,
500 SN_MEMPROT_ACCESS_CLASS_1,
501 &nasid_array);
502 if (ret != 0)
503 return xpSalError;
504 }
505 return xpSuccess;
506}
507
508/*
509 * Change protections to allow AMO operations on Shub 1.1 systems.
510 */
511static void
512xpc_allow_AMO_ops_shub_wars_1_1_sn2(void)
513{
514 int node;
515 int nasid;
516
517 if (!enable_shub_wars_1_1())
518 return;
519
520 for_each_online_node(node) {
521 nasid = cnodeid_to_nasid(node);
522 /* save current protection values */
523 xpc_prot_vec_sn2[node] =
524 (u64)HUB_L((u64 *)GLOBAL_MMR_ADDR(nasid,
525 SH1_MD_DQLP_MMR_DIR_PRIVEC0));
526 /* open up everything */
527 HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid,
528 SH1_MD_DQLP_MMR_DIR_PRIVEC0),
529 -1UL);
530 HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid,
531 SH1_MD_DQRP_MMR_DIR_PRIVEC0),
532 -1UL);
533 }
534}
535
Dean Nelson94bd2702008-07-29 22:34:05 -0700536static enum xp_retval
537xpc_rsvd_page_init_sn2(struct xpc_rsvd_page *rp)
538{
539 AMO_t *amos_page;
Dean Nelson94bd2702008-07-29 22:34:05 -0700540 int i;
541 int ret;
542
543 xpc_vars = XPC_RP_VARS(rp);
544
545 rp->sn.vars_pa = __pa(xpc_vars);
546
Dean Nelsone17d4162008-07-29 22:34:06 -0700547 /* vars_part array follows immediately after vars */
548 xpc_vars_part = (struct xpc_vars_part_sn2 *)((u8 *)XPC_RP_VARS(rp) +
549 XPC_RP_VARS_SIZE);
550
Dean Nelson94bd2702008-07-29 22:34:05 -0700551 /*
552 * Before clearing xpc_vars, see if a page of AMOs had been previously
553 * allocated. If not we'll need to allocate one and set permissions
554 * so that cross-partition AMOs are allowed.
555 *
556 * The allocated AMO page needs MCA reporting to remain disabled after
557 * XPC has unloaded. To make this work, we keep a copy of the pointer
558 * to this page (i.e., amos_page) in the struct xpc_vars structure,
559 * which is pointed to by the reserved page, and re-use that saved copy
560 * on subsequent loads of XPC. This AMO page is never freed, and its
561 * memory protections are never restricted.
562 */
563 amos_page = xpc_vars->amos_page;
564 if (amos_page == NULL) {
565 amos_page = (AMO_t *)TO_AMO(uncached_alloc_page(0, 1));
566 if (amos_page == NULL) {
567 dev_err(xpc_part, "can't allocate page of AMOs\n");
568 return xpNoMemory;
569 }
570
571 /*
Dean Nelson6e410172008-07-29 22:34:09 -0700572 * Open up AMO-R/W to cpu. This is done on Shub 1.1 systems
573 * when xpc_allow_AMO_ops_shub_wars_1_1_sn2() is called.
Dean Nelson94bd2702008-07-29 22:34:05 -0700574 */
Dean Nelson6e410172008-07-29 22:34:09 -0700575 ret = xpc_allow_AMO_ops_sn2(amos_page);
576 if (ret != xpSuccess) {
577 dev_err(xpc_part, "can't allow AMO operations\n");
578 uncached_free_page(__IA64_UNCACHED_OFFSET |
579 TO_PHYS((u64)amos_page), 1);
580 return ret;
Dean Nelson94bd2702008-07-29 22:34:05 -0700581 }
582 }
583
584 /* clear xpc_vars */
Dean Nelson33ba3c72008-07-29 22:34:07 -0700585 memset(xpc_vars, 0, sizeof(struct xpc_vars_sn2));
Dean Nelson94bd2702008-07-29 22:34:05 -0700586
587 xpc_vars->version = XPC_V_VERSION;
Dean Nelsona47d5da2008-07-29 22:34:09 -0700588 xpc_vars->activate_IRQ_nasid = cpuid_to_nasid(0);
589 xpc_vars->activate_IRQ_phys_cpuid = cpu_physical_id(0);
Dean Nelson94bd2702008-07-29 22:34:05 -0700590 xpc_vars->vars_part_pa = __pa(xpc_vars_part);
591 xpc_vars->amos_page_pa = ia64_tpa((u64)amos_page);
592 xpc_vars->amos_page = amos_page; /* save for next load of XPC */
593
594 /* clear xpc_vars_part */
Dean Nelsone17d4162008-07-29 22:34:06 -0700595 memset((u64 *)xpc_vars_part, 0, sizeof(struct xpc_vars_part_sn2) *
Dean Nelson94bd2702008-07-29 22:34:05 -0700596 xp_max_npartitions);
597
598 /* initialize the activate IRQ related AMO variables */
599 for (i = 0; i < xp_nasid_mask_words; i++)
Dean Nelson7fb5e592008-07-29 22:34:10 -0700600 (void)xpc_init_IRQ_amo_sn2(XPC_ACTIVATE_IRQ_AMOS + i);
Dean Nelson94bd2702008-07-29 22:34:05 -0700601
602 /* initialize the engaged remote partitions related AMO variables */
Dean Nelson7fb5e592008-07-29 22:34:10 -0700603 (void)xpc_init_IRQ_amo_sn2(XPC_ENGAGED_PARTITIONS_AMO);
604 (void)xpc_init_IRQ_amo_sn2(XPC_DEACTIVATE_REQUEST_AMO);
Dean Nelson94bd2702008-07-29 22:34:05 -0700605
606 return xpSuccess;
607}
608
Dean Nelson33ba3c72008-07-29 22:34:07 -0700609static void
610xpc_increment_heartbeat_sn2(void)
611{
612 xpc_vars->heartbeat++;
613}
614
615static void
616xpc_offline_heartbeat_sn2(void)
617{
618 xpc_increment_heartbeat_sn2();
619 xpc_vars->heartbeat_offline = 1;
620}
621
622static void
623xpc_online_heartbeat_sn2(void)
624{
625 xpc_increment_heartbeat_sn2();
626 xpc_vars->heartbeat_offline = 0;
627}
628
629static void
630xpc_heartbeat_init_sn2(void)
631{
632 DBUG_ON(xpc_vars == NULL);
633
634 bitmap_zero(xpc_vars->heartbeating_to_mask, XP_MAX_NPARTITIONS_SN2);
635 xpc_heartbeating_to_mask = &xpc_vars->heartbeating_to_mask[0];
636 xpc_online_heartbeat_sn2();
637}
638
639static void
640xpc_heartbeat_exit_sn2(void)
641{
642 xpc_offline_heartbeat_sn2();
643}
644
645/*
646 * At periodic intervals, scan through all active partitions and ensure
647 * their heartbeat is still active. If not, the partition is deactivated.
648 */
649static void
650xpc_check_remote_hb_sn2(void)
651{
652 struct xpc_vars_sn2 *remote_vars;
653 struct xpc_partition *part;
654 short partid;
655 enum xp_retval ret;
656
657 remote_vars = (struct xpc_vars_sn2 *)xpc_remote_copy_buffer;
658
659 for (partid = 0; partid < xp_max_npartitions; partid++) {
660
661 if (xpc_exiting)
662 break;
663
664 if (partid == sn_partition_id)
665 continue;
666
667 part = &xpc_partitions[partid];
668
669 if (part->act_state == XPC_P_INACTIVE ||
670 part->act_state == XPC_P_DEACTIVATING) {
671 continue;
672 }
673
674 /* pull the remote_hb cache line */
675 ret = xp_remote_memcpy(remote_vars,
Dean Nelsona47d5da2008-07-29 22:34:09 -0700676 (void *)part->sn.sn2.remote_vars_pa,
Dean Nelson33ba3c72008-07-29 22:34:07 -0700677 XPC_RP_VARS_SIZE);
678 if (ret != xpSuccess) {
679 XPC_DEACTIVATE_PARTITION(part, ret);
680 continue;
681 }
682
683 dev_dbg(xpc_part, "partid = %d, heartbeat = %ld, last_heartbeat"
684 " = %ld, heartbeat_offline = %ld, HB_mask[0] = 0x%lx\n",
685 partid, remote_vars->heartbeat, part->last_heartbeat,
686 remote_vars->heartbeat_offline,
687 remote_vars->heartbeating_to_mask[0]);
688
689 if (((remote_vars->heartbeat == part->last_heartbeat) &&
690 (remote_vars->heartbeat_offline == 0)) ||
691 !xpc_hb_allowed(sn_partition_id,
692 &remote_vars->heartbeating_to_mask)) {
693
694 XPC_DEACTIVATE_PARTITION(part, xpNoHeartbeat);
695 continue;
696 }
697
698 part->last_heartbeat = remote_vars->heartbeat;
699 }
700}
701
702/*
703 * Get a copy of the remote partition's XPC variables from the reserved page.
704 *
705 * remote_vars points to a buffer that is cacheline aligned for BTE copies and
706 * assumed to be of size XPC_RP_VARS_SIZE.
707 */
708static enum xp_retval
709xpc_get_remote_vars_sn2(u64 remote_vars_pa, struct xpc_vars_sn2 *remote_vars)
710{
711 enum xp_retval ret;
712
713 if (remote_vars_pa == 0)
714 return xpVarsNotSet;
715
716 /* pull over the cross partition variables */
717 ret = xp_remote_memcpy(remote_vars, (void *)remote_vars_pa,
718 XPC_RP_VARS_SIZE);
719 if (ret != xpSuccess)
720 return ret;
721
722 if (XPC_VERSION_MAJOR(remote_vars->version) !=
723 XPC_VERSION_MAJOR(XPC_V_VERSION)) {
724 return xpBadVersion;
725 }
726
727 return xpSuccess;
728}
729
730static void
Dean Nelsona47d5da2008-07-29 22:34:09 -0700731xpc_request_partition_activation_sn2(struct xpc_rsvd_page *remote_rp,
732 u64 remote_rp_pa, int nasid)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700733{
Dean Nelson7fb5e592008-07-29 22:34:10 -0700734 xpc_send_local_activate_IRQ_sn2(nasid);
Dean Nelsona47d5da2008-07-29 22:34:09 -0700735}
736
737static void
738xpc_request_partition_reactivation_sn2(struct xpc_partition *part)
739{
Dean Nelson7fb5e592008-07-29 22:34:10 -0700740 xpc_send_local_activate_IRQ_sn2(part->sn.sn2.activate_IRQ_nasid);
Dean Nelsona47d5da2008-07-29 22:34:09 -0700741}
742
743static void
744xpc_request_partition_deactivation_sn2(struct xpc_partition *part)
745{
746 struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
747 unsigned long irq_flags;
748 AMO_t *amo = (AMO_t *)__va(part_sn2->remote_amos_page_pa +
749 (XPC_DEACTIVATE_REQUEST_AMO * sizeof(AMO_t)));
750
751 local_irq_save(irq_flags);
752
753 /* set bit corresponding to our partid in remote partition's AMO */
754 FETCHOP_STORE_OP(TO_AMO((u64)&amo->variable), FETCHOP_OR,
755 (1UL << sn_partition_id));
756 /*
757 * We must always use the nofault function regardless of whether we
758 * are on a Shub 1.1 system or a Shub 1.2 slice 0xc processor. If we
759 * didn't, we'd never know that the other partition is down and would
Dean Nelson7fb5e592008-07-29 22:34:10 -0700760 * keep sending IRQs and AMOs to it until the heartbeat times out.
Dean Nelsona47d5da2008-07-29 22:34:09 -0700761 */
762 (void)xp_nofault_PIOR((u64 *)GLOBAL_MMR_ADDR(NASID_GET(&amo->
763 variable),
764 xp_nofault_PIOR_target));
765
766 local_irq_restore(irq_flags);
767
768 /*
769 * Send activate IRQ to get other side to see that we've set our
770 * bit in their deactivate request AMO.
771 */
Dean Nelson7fb5e592008-07-29 22:34:10 -0700772 xpc_send_activate_IRQ_sn2(part_sn2->remote_amos_page_pa,
Dean Nelsona47d5da2008-07-29 22:34:09 -0700773 cnodeid_to_nasid(0),
774 part_sn2->activate_IRQ_nasid,
775 part_sn2->activate_IRQ_phys_cpuid);
776}
777
778static void
779xpc_cancel_partition_deactivation_request_sn2(struct xpc_partition *part)
780{
781 unsigned long irq_flags;
782 AMO_t *amo = (AMO_t *)__va(part->sn.sn2.remote_amos_page_pa +
783 (XPC_DEACTIVATE_REQUEST_AMO * sizeof(AMO_t)));
784
785 local_irq_save(irq_flags);
786
787 /* clear bit corresponding to our partid in remote partition's AMO */
788 FETCHOP_STORE_OP(TO_AMO((u64)&amo->variable), FETCHOP_AND,
789 ~(1UL << sn_partition_id));
790 /*
791 * We must always use the nofault function regardless of whether we
792 * are on a Shub 1.1 system or a Shub 1.2 slice 0xc processor. If we
793 * didn't, we'd never know that the other partition is down and would
Dean Nelson7fb5e592008-07-29 22:34:10 -0700794 * keep sending IRQs and AMOs to it until the heartbeat times out.
Dean Nelsona47d5da2008-07-29 22:34:09 -0700795 */
796 (void)xp_nofault_PIOR((u64 *)GLOBAL_MMR_ADDR(NASID_GET(&amo->
797 variable),
798 xp_nofault_PIOR_target));
799
800 local_irq_restore(irq_flags);
801}
802
803static int
804xpc_partition_deactivation_requested_sn2(short partid)
805{
806 AMO_t *amo = xpc_vars->amos_page + XPC_DEACTIVATE_REQUEST_AMO;
807
808 /* our partition's AMO variable ANDed with partid mask */
809 return (FETCHOP_LOAD_OP(TO_AMO((u64)&amo->variable), FETCHOP_LOAD) &
810 (1UL << partid)) != 0;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700811}
812
813/*
814 * Update the remote partition's info.
815 */
816static void
817xpc_update_partition_info_sn2(struct xpc_partition *part, u8 remote_rp_version,
Dean Nelsonaaa3cd62008-07-29 22:34:07 -0700818 unsigned long *remote_rp_stamp, u64 remote_rp_pa,
819 u64 remote_vars_pa,
Dean Nelson33ba3c72008-07-29 22:34:07 -0700820 struct xpc_vars_sn2 *remote_vars)
821{
Dean Nelsona47d5da2008-07-29 22:34:09 -0700822 struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
823
Dean Nelson33ba3c72008-07-29 22:34:07 -0700824 part->remote_rp_version = remote_rp_version;
825 dev_dbg(xpc_part, " remote_rp_version = 0x%016x\n",
826 part->remote_rp_version);
827
828 part->remote_rp_stamp = *remote_rp_stamp;
Dean Nelsonaaa3cd62008-07-29 22:34:07 -0700829 dev_dbg(xpc_part, " remote_rp_stamp = 0x%016lx\n",
830 part->remote_rp_stamp);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700831
832 part->remote_rp_pa = remote_rp_pa;
833 dev_dbg(xpc_part, " remote_rp_pa = 0x%016lx\n", part->remote_rp_pa);
834
Dean Nelsona47d5da2008-07-29 22:34:09 -0700835 part_sn2->remote_vars_pa = remote_vars_pa;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700836 dev_dbg(xpc_part, " remote_vars_pa = 0x%016lx\n",
Dean Nelsona47d5da2008-07-29 22:34:09 -0700837 part_sn2->remote_vars_pa);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700838
839 part->last_heartbeat = remote_vars->heartbeat;
840 dev_dbg(xpc_part, " last_heartbeat = 0x%016lx\n",
841 part->last_heartbeat);
842
Dean Nelsona47d5da2008-07-29 22:34:09 -0700843 part_sn2->remote_vars_part_pa = remote_vars->vars_part_pa;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700844 dev_dbg(xpc_part, " remote_vars_part_pa = 0x%016lx\n",
Dean Nelsona47d5da2008-07-29 22:34:09 -0700845 part_sn2->remote_vars_part_pa);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700846
Dean Nelsona47d5da2008-07-29 22:34:09 -0700847 part_sn2->activate_IRQ_nasid = remote_vars->activate_IRQ_nasid;
848 dev_dbg(xpc_part, " activate_IRQ_nasid = 0x%x\n",
849 part_sn2->activate_IRQ_nasid);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700850
Dean Nelsona47d5da2008-07-29 22:34:09 -0700851 part_sn2->activate_IRQ_phys_cpuid =
852 remote_vars->activate_IRQ_phys_cpuid;
853 dev_dbg(xpc_part, " activate_IRQ_phys_cpuid = 0x%x\n",
854 part_sn2->activate_IRQ_phys_cpuid);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700855
Dean Nelsona47d5da2008-07-29 22:34:09 -0700856 part_sn2->remote_amos_page_pa = remote_vars->amos_page_pa;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700857 dev_dbg(xpc_part, " remote_amos_page_pa = 0x%lx\n",
Dean Nelsona47d5da2008-07-29 22:34:09 -0700858 part_sn2->remote_amos_page_pa);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700859
Dean Nelsona47d5da2008-07-29 22:34:09 -0700860 part_sn2->remote_vars_version = remote_vars->version;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700861 dev_dbg(xpc_part, " remote_vars_version = 0x%x\n",
Dean Nelsona47d5da2008-07-29 22:34:09 -0700862 part_sn2->remote_vars_version);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700863}
864
865/*
Dean Nelson7fb5e592008-07-29 22:34:10 -0700866 * Prior code has determined the nasid which generated a activate IRQ.
867 * Inspect that nasid to determine if its partition needs to be activated
868 * or deactivated.
Dean Nelson33ba3c72008-07-29 22:34:07 -0700869 *
Dean Nelson7fb5e592008-07-29 22:34:10 -0700870 * A partition is considered "awaiting activation" if our partition
Dean Nelson33ba3c72008-07-29 22:34:07 -0700871 * flags indicate it is not active and it has a heartbeat. A
872 * partition is considered "awaiting deactivation" if our partition
873 * flags indicate it is active but it has no heartbeat or it is not
874 * sending its heartbeat to us.
875 *
876 * To determine the heartbeat, the remote nasid must have a properly
877 * initialized reserved page.
878 */
879static void
Dean Nelson6e410172008-07-29 22:34:09 -0700880xpc_identify_activate_IRQ_req_sn2(int nasid)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700881{
882 struct xpc_rsvd_page *remote_rp;
883 struct xpc_vars_sn2 *remote_vars;
884 u64 remote_rp_pa;
885 u64 remote_vars_pa;
886 int remote_rp_version;
887 int reactivate = 0;
Dean Nelsonaaa3cd62008-07-29 22:34:07 -0700888 unsigned long remote_rp_stamp = 0;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700889 short partid;
890 struct xpc_partition *part;
Dean Nelsona47d5da2008-07-29 22:34:09 -0700891 struct xpc_partition_sn2 *part_sn2;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700892 enum xp_retval ret;
893
894 /* pull over the reserved page structure */
895
896 remote_rp = (struct xpc_rsvd_page *)xpc_remote_copy_buffer;
897
898 ret = xpc_get_remote_rp(nasid, NULL, remote_rp, &remote_rp_pa);
899 if (ret != xpSuccess) {
900 dev_warn(xpc_part, "unable to get reserved page from nasid %d, "
901 "which sent interrupt, reason=%d\n", nasid, ret);
902 return;
903 }
904
905 remote_vars_pa = remote_rp->sn.vars_pa;
906 remote_rp_version = remote_rp->version;
Dean Nelsona47d5da2008-07-29 22:34:09 -0700907 remote_rp_stamp = remote_rp->stamp;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700908
909 partid = remote_rp->SAL_partid;
910 part = &xpc_partitions[partid];
Dean Nelsona47d5da2008-07-29 22:34:09 -0700911 part_sn2 = &part->sn.sn2;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700912
913 /* pull over the cross partition variables */
914
915 remote_vars = (struct xpc_vars_sn2 *)xpc_remote_copy_buffer;
916
917 ret = xpc_get_remote_vars_sn2(remote_vars_pa, remote_vars);
918 if (ret != xpSuccess) {
Dean Nelson33ba3c72008-07-29 22:34:07 -0700919 dev_warn(xpc_part, "unable to get XPC variables from nasid %d, "
920 "which sent interrupt, reason=%d\n", nasid, ret);
921
922 XPC_DEACTIVATE_PARTITION(part, ret);
923 return;
924 }
925
Dean Nelson6e410172008-07-29 22:34:09 -0700926 part->activate_IRQ_rcvd++;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700927
928 dev_dbg(xpc_part, "partid for nasid %d is %d; IRQs = %d; HB = "
Dean Nelson6e410172008-07-29 22:34:09 -0700929 "%ld:0x%lx\n", (int)nasid, (int)partid, part->activate_IRQ_rcvd,
Dean Nelson33ba3c72008-07-29 22:34:07 -0700930 remote_vars->heartbeat, remote_vars->heartbeating_to_mask[0]);
931
932 if (xpc_partition_disengaged(part) &&
933 part->act_state == XPC_P_INACTIVE) {
934
935 xpc_update_partition_info_sn2(part, remote_rp_version,
936 &remote_rp_stamp, remote_rp_pa,
937 remote_vars_pa, remote_vars);
938
Dean Nelsona47d5da2008-07-29 22:34:09 -0700939 if (xpc_partition_deactivation_requested_sn2(partid)) {
940 /*
941 * Other side is waiting on us to deactivate even though
942 * we already have.
943 */
944 return;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700945 }
946
947 xpc_activate_partition(part);
948 return;
949 }
950
951 DBUG_ON(part->remote_rp_version == 0);
Dean Nelsona47d5da2008-07-29 22:34:09 -0700952 DBUG_ON(part_sn2->remote_vars_version == 0);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700953
Dean Nelsona47d5da2008-07-29 22:34:09 -0700954 if (remote_rp_stamp != part->remote_rp_stamp) {
Dean Nelson33ba3c72008-07-29 22:34:07 -0700955
Dean Nelsona47d5da2008-07-29 22:34:09 -0700956 /* the other side rebooted */
Dean Nelson33ba3c72008-07-29 22:34:07 -0700957
Dean Nelsona47d5da2008-07-29 22:34:09 -0700958 DBUG_ON(xpc_partition_engaged_sn2(partid));
959 DBUG_ON(xpc_partition_deactivation_requested_sn2(partid));
Dean Nelson33ba3c72008-07-29 22:34:07 -0700960
961 xpc_update_partition_info_sn2(part, remote_rp_version,
962 &remote_rp_stamp, remote_rp_pa,
963 remote_vars_pa, remote_vars);
964 reactivate = 1;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700965 }
966
Dean Nelsona47d5da2008-07-29 22:34:09 -0700967 if (part->disengage_timeout > 0 && !xpc_partition_disengaged(part)) {
Dean Nelson33ba3c72008-07-29 22:34:07 -0700968 /* still waiting on other side to disengage from us */
969 return;
970 }
971
Dean Nelsona47d5da2008-07-29 22:34:09 -0700972 if (reactivate)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700973 XPC_DEACTIVATE_PARTITION(part, xpReactivating);
Dean Nelsona47d5da2008-07-29 22:34:09 -0700974 else if (xpc_partition_deactivation_requested_sn2(partid))
Dean Nelson33ba3c72008-07-29 22:34:07 -0700975 XPC_DEACTIVATE_PARTITION(part, xpOtherGoingDown);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700976}
977
978/*
979 * Loop through the activation AMO variables and process any bits
980 * which are set. Each bit indicates a nasid sending a partition
981 * activation or deactivation request.
982 *
983 * Return #of IRQs detected.
984 */
985int
Dean Nelson6e410172008-07-29 22:34:09 -0700986xpc_identify_activate_IRQ_sender_sn2(void)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700987{
988 int word, bit;
989 u64 nasid_mask;
990 u64 nasid; /* remote nasid */
991 int n_IRQs_detected = 0;
992 AMO_t *act_amos;
993
994 act_amos = xpc_vars->amos_page + XPC_ACTIVATE_IRQ_AMOS;
995
996 /* scan through act AMO variable looking for non-zero entries */
997 for (word = 0; word < xp_nasid_mask_words; word++) {
998
999 if (xpc_exiting)
1000 break;
1001
Dean Nelson7fb5e592008-07-29 22:34:10 -07001002 nasid_mask = xpc_receive_IRQ_amo_sn2(&act_amos[word]);
Dean Nelson33ba3c72008-07-29 22:34:07 -07001003 if (nasid_mask == 0) {
1004 /* no IRQs from nasids in this variable */
1005 continue;
1006 }
1007
1008 dev_dbg(xpc_part, "AMO[%d] gave back 0x%lx\n", word,
1009 nasid_mask);
1010
1011 /*
1012 * If this nasid has been added to the machine since
1013 * our partition was reset, this will retain the
1014 * remote nasid in our reserved pages machine mask.
1015 * This is used in the event of module reload.
1016 */
1017 xpc_mach_nasids[word] |= nasid_mask;
1018
1019 /* locate the nasid(s) which sent interrupts */
1020
1021 for (bit = 0; bit < (8 * sizeof(u64)); bit++) {
1022 if (nasid_mask & (1UL << bit)) {
1023 n_IRQs_detected++;
1024 nasid = XPC_NASID_FROM_W_B(word, bit);
1025 dev_dbg(xpc_part, "interrupt from nasid %ld\n",
1026 nasid);
Dean Nelson6e410172008-07-29 22:34:09 -07001027 xpc_identify_activate_IRQ_req_sn2(nasid);
Dean Nelson33ba3c72008-07-29 22:34:07 -07001028 }
1029 }
1030 }
1031 return n_IRQs_detected;
1032}
1033
1034static void
Dean Nelson6e410172008-07-29 22:34:09 -07001035xpc_process_activate_IRQ_rcvd_sn2(int n_IRQs_expected)
Dean Nelson33ba3c72008-07-29 22:34:07 -07001036{
1037 int n_IRQs_detected;
1038
Dean Nelson6e410172008-07-29 22:34:09 -07001039 n_IRQs_detected = xpc_identify_activate_IRQ_sender_sn2();
Dean Nelson33ba3c72008-07-29 22:34:07 -07001040 if (n_IRQs_detected < n_IRQs_expected) {
1041 /* retry once to help avoid missing AMO */
Dean Nelson6e410172008-07-29 22:34:09 -07001042 (void)xpc_identify_activate_IRQ_sender_sn2();
Dean Nelson33ba3c72008-07-29 22:34:07 -07001043 }
1044}
1045
Dean Nelsone17d4162008-07-29 22:34:06 -07001046/*
1047 * Setup the infrastructure necessary to support XPartition Communication
1048 * between the specified remote partition and the local one.
1049 */
1050static enum xp_retval
1051xpc_setup_infrastructure_sn2(struct xpc_partition *part)
1052{
Dean Nelsona47d5da2008-07-29 22:34:09 -07001053 struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
Dean Nelsone17d4162008-07-29 22:34:06 -07001054 enum xp_retval retval;
1055 int ret;
1056 int cpuid;
1057 int ch_number;
1058 struct xpc_channel *ch;
1059 struct timer_list *timer;
1060 short partid = XPC_PARTID(part);
1061
1062 /*
1063 * Allocate all of the channel structures as a contiguous chunk of
1064 * memory.
1065 */
1066 DBUG_ON(part->channels != NULL);
1067 part->channels = kzalloc(sizeof(struct xpc_channel) * XPC_MAX_NCHANNELS,
1068 GFP_KERNEL);
1069 if (part->channels == NULL) {
1070 dev_err(xpc_chan, "can't get memory for channels\n");
1071 return xpNoMemory;
1072 }
1073
1074 /* allocate all the required GET/PUT values */
1075
Dean Nelsona47d5da2008-07-29 22:34:09 -07001076 part_sn2->local_GPs = xpc_kzalloc_cacheline_aligned(XPC_GP_SIZE,
1077 GFP_KERNEL,
1078 &part_sn2->
1079 local_GPs_base);
1080 if (part_sn2->local_GPs == NULL) {
Dean Nelsone17d4162008-07-29 22:34:06 -07001081 dev_err(xpc_chan, "can't get memory for local get/put "
1082 "values\n");
1083 retval = xpNoMemory;
1084 goto out_1;
1085 }
1086
Dean Nelsona47d5da2008-07-29 22:34:09 -07001087 part_sn2->remote_GPs = xpc_kzalloc_cacheline_aligned(XPC_GP_SIZE,
1088 GFP_KERNEL,
1089 &part_sn2->
1090 remote_GPs_base);
1091 if (part_sn2->remote_GPs == NULL) {
Dean Nelsone17d4162008-07-29 22:34:06 -07001092 dev_err(xpc_chan, "can't get memory for remote get/put "
1093 "values\n");
1094 retval = xpNoMemory;
1095 goto out_2;
1096 }
1097
Dean Nelsona47d5da2008-07-29 22:34:09 -07001098 part_sn2->remote_GPs_pa = 0;
Dean Nelsone17d4162008-07-29 22:34:06 -07001099
1100 /* allocate all the required open and close args */
1101
1102 part->local_openclose_args =
1103 xpc_kzalloc_cacheline_aligned(XPC_OPENCLOSE_ARGS_SIZE, GFP_KERNEL,
1104 &part->local_openclose_args_base);
1105 if (part->local_openclose_args == NULL) {
1106 dev_err(xpc_chan, "can't get memory for local connect args\n");
1107 retval = xpNoMemory;
1108 goto out_3;
1109 }
1110
1111 part->remote_openclose_args =
1112 xpc_kzalloc_cacheline_aligned(XPC_OPENCLOSE_ARGS_SIZE, GFP_KERNEL,
1113 &part->remote_openclose_args_base);
1114 if (part->remote_openclose_args == NULL) {
1115 dev_err(xpc_chan, "can't get memory for remote connect args\n");
1116 retval = xpNoMemory;
1117 goto out_4;
1118 }
1119
Dean Nelsona47d5da2008-07-29 22:34:09 -07001120 part_sn2->remote_openclose_args_pa = 0;
Dean Nelsone17d4162008-07-29 22:34:06 -07001121
Dean Nelson7fb5e592008-07-29 22:34:10 -07001122 part_sn2->local_chctl_amo_va = xpc_init_IRQ_amo_sn2(partid);
1123 part->chctl.all_flags = 0;
1124 spin_lock_init(&part->chctl_lock);
Dean Nelsone17d4162008-07-29 22:34:06 -07001125
Dean Nelson7fb5e592008-07-29 22:34:10 -07001126 part_sn2->notify_IRQ_nasid = 0;
1127 part_sn2->notify_IRQ_phys_cpuid = 0;
1128 part_sn2->remote_chctl_amo_va = NULL;
Dean Nelsone17d4162008-07-29 22:34:06 -07001129
1130 atomic_set(&part->channel_mgr_requests, 1);
1131 init_waitqueue_head(&part->channel_mgr_wq);
1132
Dean Nelson7fb5e592008-07-29 22:34:10 -07001133 sprintf(part_sn2->notify_IRQ_owner, "xpc%02d", partid);
Dean Nelsona47d5da2008-07-29 22:34:09 -07001134 ret = request_irq(SGI_XPC_NOTIFY, xpc_handle_notify_IRQ_sn2,
Dean Nelson7fb5e592008-07-29 22:34:10 -07001135 IRQF_SHARED, part_sn2->notify_IRQ_owner,
Dean Nelsona47d5da2008-07-29 22:34:09 -07001136 (void *)(u64)partid);
Dean Nelsone17d4162008-07-29 22:34:06 -07001137 if (ret != 0) {
1138 dev_err(xpc_chan, "can't register NOTIFY IRQ handler, "
1139 "errno=%d\n", -ret);
1140 retval = xpLackOfResources;
1141 goto out_5;
1142 }
1143
Dean Nelson7fb5e592008-07-29 22:34:10 -07001144 /* Setup a timer to check for dropped notify IRQs */
Dean Nelsona47d5da2008-07-29 22:34:09 -07001145 timer = &part_sn2->dropped_notify_IRQ_timer;
Dean Nelsone17d4162008-07-29 22:34:06 -07001146 init_timer(timer);
Dean Nelsona47d5da2008-07-29 22:34:09 -07001147 timer->function =
Dean Nelson7fb5e592008-07-29 22:34:10 -07001148 (void (*)(unsigned long))xpc_check_for_dropped_notify_IRQ_sn2;
Dean Nelsone17d4162008-07-29 22:34:06 -07001149 timer->data = (unsigned long)part;
Dean Nelson7fb5e592008-07-29 22:34:10 -07001150 timer->expires = jiffies + XPC_DROPPED_NOTIFY_IRQ_WAIT_INTERVAL;
Dean Nelsone17d4162008-07-29 22:34:06 -07001151 add_timer(timer);
1152
1153 part->nchannels = XPC_MAX_NCHANNELS;
1154
1155 atomic_set(&part->nchannels_active, 0);
1156 atomic_set(&part->nchannels_engaged, 0);
1157
1158 for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
1159 ch = &part->channels[ch_number];
1160
1161 ch->partid = partid;
1162 ch->number = ch_number;
1163 ch->flags = XPC_C_DISCONNECTED;
1164
Dean Nelsona47d5da2008-07-29 22:34:09 -07001165 ch->sn.sn2.local_GP = &part_sn2->local_GPs[ch_number];
Dean Nelsone17d4162008-07-29 22:34:06 -07001166 ch->local_openclose_args =
1167 &part->local_openclose_args[ch_number];
1168
1169 atomic_set(&ch->kthreads_assigned, 0);
1170 atomic_set(&ch->kthreads_idle, 0);
1171 atomic_set(&ch->kthreads_active, 0);
1172
1173 atomic_set(&ch->references, 0);
1174 atomic_set(&ch->n_to_notify, 0);
1175
1176 spin_lock_init(&ch->lock);
Dean Nelsona47d5da2008-07-29 22:34:09 -07001177 mutex_init(&ch->sn.sn2.msg_to_pull_mutex);
Dean Nelsone17d4162008-07-29 22:34:06 -07001178 init_completion(&ch->wdisconnect_wait);
1179
1180 atomic_set(&ch->n_on_msg_allocate_wq, 0);
1181 init_waitqueue_head(&ch->msg_allocate_wq);
1182 init_waitqueue_head(&ch->idle_wq);
1183 }
1184
1185 /*
1186 * With the setting of the partition setup_state to XPC_P_SETUP, we're
1187 * declaring that this partition is ready to go.
1188 */
1189 part->setup_state = XPC_P_SETUP;
1190
1191 /*
1192 * Setup the per partition specific variables required by the
1193 * remote partition to establish channel connections with us.
1194 *
1195 * The setting of the magic # indicates that these per partition
1196 * specific variables are ready to be used.
1197 */
Dean Nelsona47d5da2008-07-29 22:34:09 -07001198 xpc_vars_part[partid].GPs_pa = __pa(part_sn2->local_GPs);
Dean Nelsone17d4162008-07-29 22:34:06 -07001199 xpc_vars_part[partid].openclose_args_pa =
1200 __pa(part->local_openclose_args);
Dean Nelson7fb5e592008-07-29 22:34:10 -07001201 xpc_vars_part[partid].chctl_amo_pa = __pa(part_sn2->local_chctl_amo_va);
Dean Nelsone17d4162008-07-29 22:34:06 -07001202 cpuid = raw_smp_processor_id(); /* any CPU in this partition will do */
Dean Nelson7fb5e592008-07-29 22:34:10 -07001203 xpc_vars_part[partid].notify_IRQ_nasid = cpuid_to_nasid(cpuid);
1204 xpc_vars_part[partid].notify_IRQ_phys_cpuid = cpu_physical_id(cpuid);
Dean Nelsone17d4162008-07-29 22:34:06 -07001205 xpc_vars_part[partid].nchannels = part->nchannels;
1206 xpc_vars_part[partid].magic = XPC_VP_MAGIC1;
1207
1208 return xpSuccess;
1209
1210 /* setup of infrastructure failed */
1211out_5:
1212 kfree(part->remote_openclose_args_base);
1213 part->remote_openclose_args = NULL;
1214out_4:
1215 kfree(part->local_openclose_args_base);
1216 part->local_openclose_args = NULL;
1217out_3:
Dean Nelsona47d5da2008-07-29 22:34:09 -07001218 kfree(part_sn2->remote_GPs_base);
1219 part_sn2->remote_GPs = NULL;
Dean Nelsone17d4162008-07-29 22:34:06 -07001220out_2:
Dean Nelsona47d5da2008-07-29 22:34:09 -07001221 kfree(part_sn2->local_GPs_base);
1222 part_sn2->local_GPs = NULL;
Dean Nelsone17d4162008-07-29 22:34:06 -07001223out_1:
1224 kfree(part->channels);
1225 part->channels = NULL;
1226 return retval;
1227}
1228
1229/*
1230 * Teardown the infrastructure necessary to support XPartition Communication
1231 * between the specified remote partition and the local one.
1232 */
1233static void
1234xpc_teardown_infrastructure_sn2(struct xpc_partition *part)
1235{
Dean Nelsona47d5da2008-07-29 22:34:09 -07001236 struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
Dean Nelsone17d4162008-07-29 22:34:06 -07001237 short partid = XPC_PARTID(part);
1238
1239 /*
1240 * We start off by making this partition inaccessible to local
1241 * processes by marking it as no longer setup. Then we make it
1242 * inaccessible to remote processes by clearing the XPC per partition
1243 * specific variable's magic # (which indicates that these variables
Dean Nelson7fb5e592008-07-29 22:34:10 -07001244 * are no longer valid) and by ignoring all XPC notify IRQs sent to
Dean Nelsone17d4162008-07-29 22:34:06 -07001245 * this partition.
1246 */
1247
1248 DBUG_ON(atomic_read(&part->nchannels_engaged) != 0);
1249 DBUG_ON(atomic_read(&part->nchannels_active) != 0);
1250 DBUG_ON(part->setup_state != XPC_P_SETUP);
1251 part->setup_state = XPC_P_WTEARDOWN;
1252
1253 xpc_vars_part[partid].magic = 0;
1254
1255 free_irq(SGI_XPC_NOTIFY, (void *)(u64)partid);
1256
1257 /*
1258 * Before proceeding with the teardown we have to wait until all
1259 * existing references cease.
1260 */
1261 wait_event(part->teardown_wq, (atomic_read(&part->references) == 0));
1262
1263 /* now we can begin tearing down the infrastructure */
1264
1265 part->setup_state = XPC_P_TORNDOWN;
1266
1267 /* in case we've still got outstanding timers registered... */
Dean Nelsona47d5da2008-07-29 22:34:09 -07001268 del_timer_sync(&part_sn2->dropped_notify_IRQ_timer);
Dean Nelsone17d4162008-07-29 22:34:06 -07001269
1270 kfree(part->remote_openclose_args_base);
1271 part->remote_openclose_args = NULL;
1272 kfree(part->local_openclose_args_base);
1273 part->local_openclose_args = NULL;
Dean Nelsona47d5da2008-07-29 22:34:09 -07001274 kfree(part_sn2->remote_GPs_base);
1275 part_sn2->remote_GPs = NULL;
1276 kfree(part_sn2->local_GPs_base);
1277 part_sn2->local_GPs = NULL;
Dean Nelsone17d4162008-07-29 22:34:06 -07001278 kfree(part->channels);
1279 part->channels = NULL;
Dean Nelson7fb5e592008-07-29 22:34:10 -07001280 part_sn2->local_chctl_amo_va = NULL;
Dean Nelsone17d4162008-07-29 22:34:06 -07001281}
1282
1283/*
1284 * Create a wrapper that hides the underlying mechanism for pulling a cacheline
1285 * (or multiple cachelines) from a remote partition.
1286 *
1287 * src must be a cacheline aligned physical address on the remote partition.
1288 * dst must be a cacheline aligned virtual address on this partition.
1289 * cnt must be cacheline sized
1290 */
1291/* >>> Replace this function by call to xp_remote_memcpy() or bte_copy()? */
1292static enum xp_retval
1293xpc_pull_remote_cachelines_sn2(struct xpc_partition *part, void *dst,
1294 const void *src, size_t cnt)
1295{
1296 enum xp_retval ret;
1297
1298 DBUG_ON((u64)src != L1_CACHE_ALIGN((u64)src));
1299 DBUG_ON((u64)dst != L1_CACHE_ALIGN((u64)dst));
1300 DBUG_ON(cnt != L1_CACHE_ALIGN(cnt));
1301
1302 if (part->act_state == XPC_P_DEACTIVATING)
1303 return part->reason;
1304
1305 ret = xp_remote_memcpy(dst, src, cnt);
1306 if (ret != xpSuccess) {
1307 dev_dbg(xpc_chan, "xp_remote_memcpy() from partition %d failed,"
1308 " ret=%d\n", XPC_PARTID(part), ret);
1309 }
1310 return ret;
1311}
1312
1313/*
1314 * Pull the remote per partition specific variables from the specified
1315 * partition.
1316 */
1317static enum xp_retval
1318xpc_pull_remote_vars_part_sn2(struct xpc_partition *part)
1319{
Dean Nelsona47d5da2008-07-29 22:34:09 -07001320 struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
Dean Nelsone17d4162008-07-29 22:34:06 -07001321 u8 buffer[L1_CACHE_BYTES * 2];
1322 struct xpc_vars_part_sn2 *pulled_entry_cacheline =
1323 (struct xpc_vars_part_sn2 *)L1_CACHE_ALIGN((u64)buffer);
1324 struct xpc_vars_part_sn2 *pulled_entry;
1325 u64 remote_entry_cacheline_pa, remote_entry_pa;
1326 short partid = XPC_PARTID(part);
1327 enum xp_retval ret;
1328
1329 /* pull the cacheline that contains the variables we're interested in */
1330
Dean Nelsona47d5da2008-07-29 22:34:09 -07001331 DBUG_ON(part_sn2->remote_vars_part_pa !=
1332 L1_CACHE_ALIGN(part_sn2->remote_vars_part_pa));
Dean Nelsone17d4162008-07-29 22:34:06 -07001333 DBUG_ON(sizeof(struct xpc_vars_part_sn2) != L1_CACHE_BYTES / 2);
1334
Dean Nelsona47d5da2008-07-29 22:34:09 -07001335 remote_entry_pa = part_sn2->remote_vars_part_pa +
Dean Nelsone17d4162008-07-29 22:34:06 -07001336 sn_partition_id * sizeof(struct xpc_vars_part_sn2);
1337
1338 remote_entry_cacheline_pa = (remote_entry_pa & ~(L1_CACHE_BYTES - 1));
1339
1340 pulled_entry = (struct xpc_vars_part_sn2 *)((u64)pulled_entry_cacheline
1341 + (remote_entry_pa &
1342 (L1_CACHE_BYTES - 1)));
1343
1344 ret = xpc_pull_remote_cachelines_sn2(part, pulled_entry_cacheline,
1345 (void *)remote_entry_cacheline_pa,
1346 L1_CACHE_BYTES);
1347 if (ret != xpSuccess) {
1348 dev_dbg(xpc_chan, "failed to pull XPC vars_part from "
1349 "partition %d, ret=%d\n", partid, ret);
1350 return ret;
1351 }
1352
1353 /* see if they've been set up yet */
1354
1355 if (pulled_entry->magic != XPC_VP_MAGIC1 &&
1356 pulled_entry->magic != XPC_VP_MAGIC2) {
1357
1358 if (pulled_entry->magic != 0) {
1359 dev_dbg(xpc_chan, "partition %d's XPC vars_part for "
1360 "partition %d has bad magic value (=0x%lx)\n",
1361 partid, sn_partition_id, pulled_entry->magic);
1362 return xpBadMagic;
1363 }
1364
1365 /* they've not been initialized yet */
1366 return xpRetry;
1367 }
1368
1369 if (xpc_vars_part[partid].magic == XPC_VP_MAGIC1) {
1370
1371 /* validate the variables */
1372
1373 if (pulled_entry->GPs_pa == 0 ||
1374 pulled_entry->openclose_args_pa == 0 ||
Dean Nelson7fb5e592008-07-29 22:34:10 -07001375 pulled_entry->chctl_amo_pa == 0) {
Dean Nelsone17d4162008-07-29 22:34:06 -07001376
1377 dev_err(xpc_chan, "partition %d's XPC vars_part for "
1378 "partition %d are not valid\n", partid,
1379 sn_partition_id);
1380 return xpInvalidAddress;
1381 }
1382
1383 /* the variables we imported look to be valid */
1384
Dean Nelsona47d5da2008-07-29 22:34:09 -07001385 part_sn2->remote_GPs_pa = pulled_entry->GPs_pa;
1386 part_sn2->remote_openclose_args_pa =
Dean Nelsone17d4162008-07-29 22:34:06 -07001387 pulled_entry->openclose_args_pa;
Dean Nelson7fb5e592008-07-29 22:34:10 -07001388 part_sn2->remote_chctl_amo_va =
1389 (AMO_t *)__va(pulled_entry->chctl_amo_pa);
1390 part_sn2->notify_IRQ_nasid = pulled_entry->notify_IRQ_nasid;
1391 part_sn2->notify_IRQ_phys_cpuid =
1392 pulled_entry->notify_IRQ_phys_cpuid;
Dean Nelsone17d4162008-07-29 22:34:06 -07001393
1394 if (part->nchannels > pulled_entry->nchannels)
1395 part->nchannels = pulled_entry->nchannels;
1396
1397 /* let the other side know that we've pulled their variables */
1398
1399 xpc_vars_part[partid].magic = XPC_VP_MAGIC2;
1400 }
1401
1402 if (pulled_entry->magic == XPC_VP_MAGIC1)
1403 return xpRetry;
1404
1405 return xpSuccess;
1406}
1407
1408/*
1409 * Establish first contact with the remote partititon. This involves pulling
1410 * the XPC per partition variables from the remote partition and waiting for
1411 * the remote partition to pull ours.
1412 */
1413static enum xp_retval
1414xpc_make_first_contact_sn2(struct xpc_partition *part)
1415{
Dean Nelsona47d5da2008-07-29 22:34:09 -07001416 struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
Dean Nelsone17d4162008-07-29 22:34:06 -07001417 enum xp_retval ret;
1418
Dean Nelson33ba3c72008-07-29 22:34:07 -07001419 /*
1420 * Register the remote partition's AMOs with SAL so it can handle
1421 * and cleanup errors within that address range should the remote
1422 * partition go down. We don't unregister this range because it is
1423 * difficult to tell when outstanding writes to the remote partition
1424 * are finished and thus when it is safe to unregister. This should
1425 * not result in wasted space in the SAL xp_addr_region table because
1426 * we should get the same page for remote_amos_page_pa after module
1427 * reloads and system reboots.
1428 */
Dean Nelsona47d5da2008-07-29 22:34:09 -07001429 if (sn_register_xp_addr_region(part_sn2->remote_amos_page_pa,
Dean Nelson33ba3c72008-07-29 22:34:07 -07001430 PAGE_SIZE, 1) < 0) {
1431 dev_warn(xpc_part, "xpc_activating(%d) failed to register "
1432 "xp_addr region\n", XPC_PARTID(part));
1433
1434 ret = xpPhysAddrRegFailed;
1435 XPC_DEACTIVATE_PARTITION(part, ret);
1436 return ret;
1437 }
1438
Dean Nelsona47d5da2008-07-29 22:34:09 -07001439 /*
1440 * Send activate IRQ to get other side to activate if they've not
1441 * already begun to do so.
1442 */
Dean Nelson7fb5e592008-07-29 22:34:10 -07001443 xpc_send_activate_IRQ_sn2(part_sn2->remote_amos_page_pa,
Dean Nelsona47d5da2008-07-29 22:34:09 -07001444 cnodeid_to_nasid(0),
1445 part_sn2->activate_IRQ_nasid,
1446 part_sn2->activate_IRQ_phys_cpuid);
Dean Nelson33ba3c72008-07-29 22:34:07 -07001447
Dean Nelsone17d4162008-07-29 22:34:06 -07001448 while ((ret = xpc_pull_remote_vars_part_sn2(part)) != xpSuccess) {
1449 if (ret != xpRetry) {
1450 XPC_DEACTIVATE_PARTITION(part, ret);
1451 return ret;
1452 }
1453
1454 dev_dbg(xpc_part, "waiting to make first contact with "
1455 "partition %d\n", XPC_PARTID(part));
1456
1457 /* wait a 1/4 of a second or so */
1458 (void)msleep_interruptible(250);
1459
1460 if (part->act_state == XPC_P_DEACTIVATING)
1461 return part->reason;
1462 }
1463
1464 return xpSuccess;
1465}
1466
1467/*
Dean Nelson7fb5e592008-07-29 22:34:10 -07001468 * Get the chctl flags and pull the openclose args and/or remote GPs as needed.
Dean Nelsone17d4162008-07-29 22:34:06 -07001469 */
1470static u64
Dean Nelson7fb5e592008-07-29 22:34:10 -07001471xpc_get_chctl_all_flags_sn2(struct xpc_partition *part)
Dean Nelsone17d4162008-07-29 22:34:06 -07001472{
Dean Nelsona47d5da2008-07-29 22:34:09 -07001473 struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
Dean Nelsone17d4162008-07-29 22:34:06 -07001474 unsigned long irq_flags;
Dean Nelson7fb5e592008-07-29 22:34:10 -07001475 union xpc_channel_ctl_flags chctl;
Dean Nelsone17d4162008-07-29 22:34:06 -07001476 enum xp_retval ret;
1477
1478 /*
Dean Nelson7fb5e592008-07-29 22:34:10 -07001479 * See if there are any chctl flags to be handled.
Dean Nelsone17d4162008-07-29 22:34:06 -07001480 */
1481
Dean Nelson7fb5e592008-07-29 22:34:10 -07001482 spin_lock_irqsave(&part->chctl_lock, irq_flags);
1483 chctl = part->chctl;
1484 if (chctl.all_flags != 0)
1485 part->chctl.all_flags = 0;
Dean Nelsone17d4162008-07-29 22:34:06 -07001486
Dean Nelson7fb5e592008-07-29 22:34:10 -07001487 spin_unlock_irqrestore(&part->chctl_lock, irq_flags);
Dean Nelsone17d4162008-07-29 22:34:06 -07001488
Dean Nelson7fb5e592008-07-29 22:34:10 -07001489 if (xpc_any_openclose_chctl_flags_set(&chctl)) {
Dean Nelsona47d5da2008-07-29 22:34:09 -07001490 ret = xpc_pull_remote_cachelines_sn2(part, part->
1491 remote_openclose_args,
1492 (void *)part_sn2->
Dean Nelsone17d4162008-07-29 22:34:06 -07001493 remote_openclose_args_pa,
1494 XPC_OPENCLOSE_ARGS_SIZE);
1495 if (ret != xpSuccess) {
1496 XPC_DEACTIVATE_PARTITION(part, ret);
1497
1498 dev_dbg(xpc_chan, "failed to pull openclose args from "
1499 "partition %d, ret=%d\n", XPC_PARTID(part),
1500 ret);
1501
Dean Nelson7fb5e592008-07-29 22:34:10 -07001502 /* don't bother processing chctl flags anymore */
1503 chctl.all_flags = 0;
Dean Nelsone17d4162008-07-29 22:34:06 -07001504 }
1505 }
1506
Dean Nelson7fb5e592008-07-29 22:34:10 -07001507 if (xpc_any_msg_chctl_flags_set(&chctl)) {
Dean Nelsona47d5da2008-07-29 22:34:09 -07001508 ret = xpc_pull_remote_cachelines_sn2(part, part_sn2->remote_GPs,
1509 (void *)part_sn2->remote_GPs_pa,
Dean Nelsone17d4162008-07-29 22:34:06 -07001510 XPC_GP_SIZE);
1511 if (ret != xpSuccess) {
1512 XPC_DEACTIVATE_PARTITION(part, ret);
1513
1514 dev_dbg(xpc_chan, "failed to pull GPs from partition "
1515 "%d, ret=%d\n", XPC_PARTID(part), ret);
1516
Dean Nelson7fb5e592008-07-29 22:34:10 -07001517 /* don't bother processing chctl flags anymore */
1518 chctl.all_flags = 0;
Dean Nelsone17d4162008-07-29 22:34:06 -07001519 }
1520 }
1521
Dean Nelson7fb5e592008-07-29 22:34:10 -07001522 return chctl.all_flags;
Dean Nelsone17d4162008-07-29 22:34:06 -07001523}
1524
Dean Nelsona47d5da2008-07-29 22:34:09 -07001525/*
1526 * Notify those who wanted to be notified upon delivery of their message.
1527 */
1528static void
1529xpc_notify_senders_sn2(struct xpc_channel *ch, enum xp_retval reason, s64 put)
1530{
1531 struct xpc_notify *notify;
1532 u8 notify_type;
1533 s64 get = ch->sn.sn2.w_remote_GP.get - 1;
1534
1535 while (++get < put && atomic_read(&ch->n_to_notify) > 0) {
1536
1537 notify = &ch->notify_queue[get % ch->local_nentries];
1538
1539 /*
1540 * See if the notify entry indicates it was associated with
1541 * a message who's sender wants to be notified. It is possible
1542 * that it is, but someone else is doing or has done the
1543 * notification.
1544 */
1545 notify_type = notify->type;
1546 if (notify_type == 0 ||
1547 cmpxchg(&notify->type, notify_type, 0) != notify_type) {
1548 continue;
1549 }
1550
1551 DBUG_ON(notify_type != XPC_N_CALL);
1552
1553 atomic_dec(&ch->n_to_notify);
1554
1555 if (notify->func != NULL) {
1556 dev_dbg(xpc_chan, "notify->func() called, notify=0x%p, "
1557 "msg_number=%ld, partid=%d, channel=%d\n",
1558 (void *)notify, get, ch->partid, ch->number);
1559
1560 notify->func(reason, ch->partid, ch->number,
1561 notify->key);
1562
1563 dev_dbg(xpc_chan, "notify->func() returned, "
1564 "notify=0x%p, msg_number=%ld, partid=%d, "
1565 "channel=%d\n", (void *)notify, get,
1566 ch->partid, ch->number);
1567 }
1568 }
1569}
1570
1571static void
1572xpc_notify_senders_of_disconnect_sn2(struct xpc_channel *ch)
1573{
1574 xpc_notify_senders_sn2(ch, ch->reason, ch->sn.sn2.w_local_GP.put);
1575}
1576
1577/*
1578 * Clear some of the msg flags in the local message queue.
1579 */
1580static inline void
1581xpc_clear_local_msgqueue_flags_sn2(struct xpc_channel *ch)
1582{
1583 struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
1584 struct xpc_msg *msg;
1585 s64 get;
1586
1587 get = ch_sn2->w_remote_GP.get;
1588 do {
1589 msg = (struct xpc_msg *)((u64)ch->local_msgqueue +
1590 (get % ch->local_nentries) *
1591 ch->msg_size);
1592 msg->flags = 0;
1593 } while (++get < ch_sn2->remote_GP.get);
1594}
1595
1596/*
1597 * Clear some of the msg flags in the remote message queue.
1598 */
1599static inline void
1600xpc_clear_remote_msgqueue_flags_sn2(struct xpc_channel *ch)
1601{
1602 struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
1603 struct xpc_msg *msg;
1604 s64 put;
1605
1606 put = ch_sn2->w_remote_GP.put;
1607 do {
1608 msg = (struct xpc_msg *)((u64)ch->remote_msgqueue +
1609 (put % ch->remote_nentries) *
1610 ch->msg_size);
1611 msg->flags = 0;
1612 } while (++put < ch_sn2->remote_GP.put);
1613}
1614
1615static void
Dean Nelson7fb5e592008-07-29 22:34:10 -07001616xpc_process_msg_chctl_flags_sn2(struct xpc_partition *part, int ch_number)
Dean Nelsona47d5da2008-07-29 22:34:09 -07001617{
1618 struct xpc_channel *ch = &part->channels[ch_number];
1619 struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
1620 int nmsgs_sent;
1621
1622 ch_sn2->remote_GP = part->sn.sn2.remote_GPs[ch_number];
1623
1624 /* See what, if anything, has changed for each connected channel */
1625
1626 xpc_msgqueue_ref(ch);
1627
1628 if (ch_sn2->w_remote_GP.get == ch_sn2->remote_GP.get &&
1629 ch_sn2->w_remote_GP.put == ch_sn2->remote_GP.put) {
1630 /* nothing changed since GPs were last pulled */
1631 xpc_msgqueue_deref(ch);
1632 return;
1633 }
1634
1635 if (!(ch->flags & XPC_C_CONNECTED)) {
1636 xpc_msgqueue_deref(ch);
1637 return;
1638 }
1639
1640 /*
1641 * First check to see if messages recently sent by us have been
1642 * received by the other side. (The remote GET value will have
1643 * changed since we last looked at it.)
1644 */
1645
1646 if (ch_sn2->w_remote_GP.get != ch_sn2->remote_GP.get) {
1647
1648 /*
1649 * We need to notify any senders that want to be notified
1650 * that their sent messages have been received by their
1651 * intended recipients. We need to do this before updating
1652 * w_remote_GP.get so that we don't allocate the same message
1653 * queue entries prematurely (see xpc_allocate_msg()).
1654 */
1655 if (atomic_read(&ch->n_to_notify) > 0) {
1656 /*
1657 * Notify senders that messages sent have been
1658 * received and delivered by the other side.
1659 */
1660 xpc_notify_senders_sn2(ch, xpMsgDelivered,
1661 ch_sn2->remote_GP.get);
1662 }
1663
1664 /*
1665 * Clear msg->flags in previously sent messages, so that
1666 * they're ready for xpc_allocate_msg().
1667 */
1668 xpc_clear_local_msgqueue_flags_sn2(ch);
1669
1670 ch_sn2->w_remote_GP.get = ch_sn2->remote_GP.get;
1671
1672 dev_dbg(xpc_chan, "w_remote_GP.get changed to %ld, partid=%d, "
1673 "channel=%d\n", ch_sn2->w_remote_GP.get, ch->partid,
1674 ch->number);
1675
1676 /*
1677 * If anyone was waiting for message queue entries to become
1678 * available, wake them up.
1679 */
1680 if (atomic_read(&ch->n_on_msg_allocate_wq) > 0)
1681 wake_up(&ch->msg_allocate_wq);
1682 }
1683
1684 /*
1685 * Now check for newly sent messages by the other side. (The remote
1686 * PUT value will have changed since we last looked at it.)
1687 */
1688
1689 if (ch_sn2->w_remote_GP.put != ch_sn2->remote_GP.put) {
1690 /*
1691 * Clear msg->flags in previously received messages, so that
1692 * they're ready for xpc_get_deliverable_msg().
1693 */
1694 xpc_clear_remote_msgqueue_flags_sn2(ch);
1695
1696 ch_sn2->w_remote_GP.put = ch_sn2->remote_GP.put;
1697
1698 dev_dbg(xpc_chan, "w_remote_GP.put changed to %ld, partid=%d, "
1699 "channel=%d\n", ch_sn2->w_remote_GP.put, ch->partid,
1700 ch->number);
1701
1702 nmsgs_sent = ch_sn2->w_remote_GP.put - ch_sn2->w_local_GP.get;
1703 if (nmsgs_sent > 0) {
1704 dev_dbg(xpc_chan, "msgs waiting to be copied and "
1705 "delivered=%d, partid=%d, channel=%d\n",
1706 nmsgs_sent, ch->partid, ch->number);
1707
1708 if (ch->flags & XPC_C_CONNECTEDCALLOUT_MADE)
1709 xpc_activate_kthreads(ch, nmsgs_sent);
1710 }
1711 }
1712
1713 xpc_msgqueue_deref(ch);
1714}
1715
Dean Nelsone17d4162008-07-29 22:34:06 -07001716static struct xpc_msg *
1717xpc_pull_remote_msg_sn2(struct xpc_channel *ch, s64 get)
1718{
1719 struct xpc_partition *part = &xpc_partitions[ch->partid];
Dean Nelsona47d5da2008-07-29 22:34:09 -07001720 struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
Dean Nelsone17d4162008-07-29 22:34:06 -07001721 struct xpc_msg *remote_msg, *msg;
1722 u32 msg_index, nmsgs;
1723 u64 msg_offset;
1724 enum xp_retval ret;
1725
Dean Nelsona47d5da2008-07-29 22:34:09 -07001726 if (mutex_lock_interruptible(&ch_sn2->msg_to_pull_mutex) != 0) {
Dean Nelsone17d4162008-07-29 22:34:06 -07001727 /* we were interrupted by a signal */
1728 return NULL;
1729 }
1730
Dean Nelsona47d5da2008-07-29 22:34:09 -07001731 while (get >= ch_sn2->next_msg_to_pull) {
Dean Nelsone17d4162008-07-29 22:34:06 -07001732
1733 /* pull as many messages as are ready and able to be pulled */
1734
Dean Nelsona47d5da2008-07-29 22:34:09 -07001735 msg_index = ch_sn2->next_msg_to_pull % ch->remote_nentries;
Dean Nelsone17d4162008-07-29 22:34:06 -07001736
Dean Nelsona47d5da2008-07-29 22:34:09 -07001737 DBUG_ON(ch_sn2->next_msg_to_pull >= ch_sn2->w_remote_GP.put);
1738 nmsgs = ch_sn2->w_remote_GP.put - ch_sn2->next_msg_to_pull;
Dean Nelsone17d4162008-07-29 22:34:06 -07001739 if (msg_index + nmsgs > ch->remote_nentries) {
1740 /* ignore the ones that wrap the msg queue for now */
1741 nmsgs = ch->remote_nentries - msg_index;
1742 }
1743
1744 msg_offset = msg_index * ch->msg_size;
1745 msg = (struct xpc_msg *)((u64)ch->remote_msgqueue + msg_offset);
1746 remote_msg = (struct xpc_msg *)(ch->remote_msgqueue_pa +
1747 msg_offset);
1748
1749 ret = xpc_pull_remote_cachelines_sn2(part, msg, remote_msg,
1750 nmsgs * ch->msg_size);
1751 if (ret != xpSuccess) {
1752
1753 dev_dbg(xpc_chan, "failed to pull %d msgs starting with"
1754 " msg %ld from partition %d, channel=%d, "
Dean Nelsona47d5da2008-07-29 22:34:09 -07001755 "ret=%d\n", nmsgs, ch_sn2->next_msg_to_pull,
Dean Nelsone17d4162008-07-29 22:34:06 -07001756 ch->partid, ch->number, ret);
1757
1758 XPC_DEACTIVATE_PARTITION(part, ret);
1759
Dean Nelsona47d5da2008-07-29 22:34:09 -07001760 mutex_unlock(&ch_sn2->msg_to_pull_mutex);
Dean Nelsone17d4162008-07-29 22:34:06 -07001761 return NULL;
1762 }
1763
Dean Nelsona47d5da2008-07-29 22:34:09 -07001764 ch_sn2->next_msg_to_pull += nmsgs;
Dean Nelsone17d4162008-07-29 22:34:06 -07001765 }
1766
Dean Nelsona47d5da2008-07-29 22:34:09 -07001767 mutex_unlock(&ch_sn2->msg_to_pull_mutex);
Dean Nelsone17d4162008-07-29 22:34:06 -07001768
1769 /* return the message we were looking for */
1770 msg_offset = (get % ch->remote_nentries) * ch->msg_size;
1771 msg = (struct xpc_msg *)((u64)ch->remote_msgqueue + msg_offset);
1772
1773 return msg;
1774}
1775
Dean Nelsona47d5da2008-07-29 22:34:09 -07001776static int
1777xpc_n_of_deliverable_msgs_sn2(struct xpc_channel *ch)
1778{
1779 return ch->sn.sn2.w_remote_GP.put - ch->sn.sn2.w_local_GP.get;
1780}
1781
Dean Nelsone17d4162008-07-29 22:34:06 -07001782/*
1783 * Get a message to be delivered.
1784 */
1785static struct xpc_msg *
1786xpc_get_deliverable_msg_sn2(struct xpc_channel *ch)
1787{
Dean Nelsona47d5da2008-07-29 22:34:09 -07001788 struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
Dean Nelsone17d4162008-07-29 22:34:06 -07001789 struct xpc_msg *msg = NULL;
1790 s64 get;
1791
1792 do {
1793 if (ch->flags & XPC_C_DISCONNECTING)
1794 break;
1795
Dean Nelsona47d5da2008-07-29 22:34:09 -07001796 get = ch_sn2->w_local_GP.get;
Dean Nelsone17d4162008-07-29 22:34:06 -07001797 rmb(); /* guarantee that .get loads before .put */
Dean Nelsona47d5da2008-07-29 22:34:09 -07001798 if (get == ch_sn2->w_remote_GP.put)
Dean Nelsone17d4162008-07-29 22:34:06 -07001799 break;
1800
1801 /* There are messages waiting to be pulled and delivered.
1802 * We need to try to secure one for ourselves. We'll do this
1803 * by trying to increment w_local_GP.get and hope that no one
1804 * else beats us to it. If they do, we'll we'll simply have
1805 * to try again for the next one.
1806 */
1807
Dean Nelsona47d5da2008-07-29 22:34:09 -07001808 if (cmpxchg(&ch_sn2->w_local_GP.get, get, get + 1) == get) {
Dean Nelsone17d4162008-07-29 22:34:06 -07001809 /* we got the entry referenced by get */
1810
1811 dev_dbg(xpc_chan, "w_local_GP.get changed to %ld, "
1812 "partid=%d, channel=%d\n", get + 1,
1813 ch->partid, ch->number);
1814
1815 /* pull the message from the remote partition */
1816
1817 msg = xpc_pull_remote_msg_sn2(ch, get);
1818
1819 DBUG_ON(msg != NULL && msg->number != get);
1820 DBUG_ON(msg != NULL && (msg->flags & XPC_M_DONE));
1821 DBUG_ON(msg != NULL && !(msg->flags & XPC_M_READY));
1822
1823 break;
1824 }
1825
1826 } while (1);
1827
1828 return msg;
1829}
1830
Dean Nelson33ba3c72008-07-29 22:34:07 -07001831/*
1832 * Now we actually send the messages that are ready to be sent by advancing
Dean Nelson7fb5e592008-07-29 22:34:10 -07001833 * the local message queue's Put value and then send a chctl msgrequest to the
1834 * recipient partition.
Dean Nelson33ba3c72008-07-29 22:34:07 -07001835 */
1836static void
1837xpc_send_msgs_sn2(struct xpc_channel *ch, s64 initial_put)
1838{
Dean Nelsona47d5da2008-07-29 22:34:09 -07001839 struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
Dean Nelson33ba3c72008-07-29 22:34:07 -07001840 struct xpc_msg *msg;
1841 s64 put = initial_put + 1;
Dean Nelson7fb5e592008-07-29 22:34:10 -07001842 int send_msgrequest = 0;
Dean Nelson33ba3c72008-07-29 22:34:07 -07001843
1844 while (1) {
1845
1846 while (1) {
Dean Nelsona47d5da2008-07-29 22:34:09 -07001847 if (put == ch_sn2->w_local_GP.put)
Dean Nelson33ba3c72008-07-29 22:34:07 -07001848 break;
1849
1850 msg = (struct xpc_msg *)((u64)ch->local_msgqueue +
1851 (put % ch->local_nentries) *
1852 ch->msg_size);
1853
1854 if (!(msg->flags & XPC_M_READY))
1855 break;
1856
1857 put++;
1858 }
1859
1860 if (put == initial_put) {
1861 /* nothing's changed */
1862 break;
1863 }
1864
Dean Nelsona47d5da2008-07-29 22:34:09 -07001865 if (cmpxchg_rel(&ch_sn2->local_GP->put, initial_put, put) !=
Dean Nelson33ba3c72008-07-29 22:34:07 -07001866 initial_put) {
1867 /* someone else beat us to it */
Dean Nelsona47d5da2008-07-29 22:34:09 -07001868 DBUG_ON(ch_sn2->local_GP->put < initial_put);
Dean Nelson33ba3c72008-07-29 22:34:07 -07001869 break;
1870 }
1871
1872 /* we just set the new value of local_GP->put */
1873
1874 dev_dbg(xpc_chan, "local_GP->put changed to %ld, partid=%d, "
1875 "channel=%d\n", put, ch->partid, ch->number);
1876
Dean Nelson7fb5e592008-07-29 22:34:10 -07001877 send_msgrequest = 1;
Dean Nelson33ba3c72008-07-29 22:34:07 -07001878
1879 /*
1880 * We need to ensure that the message referenced by
1881 * local_GP->put is not XPC_M_READY or that local_GP->put
1882 * equals w_local_GP.put, so we'll go have a look.
1883 */
1884 initial_put = put;
1885 }
1886
Dean Nelson7fb5e592008-07-29 22:34:10 -07001887 if (send_msgrequest)
1888 xpc_send_chctl_msgrequest_sn2(ch);
Dean Nelson33ba3c72008-07-29 22:34:07 -07001889}
1890
1891/*
1892 * Allocate an entry for a message from the message queue associated with the
1893 * specified channel.
1894 */
1895static enum xp_retval
1896xpc_allocate_msg_sn2(struct xpc_channel *ch, u32 flags,
1897 struct xpc_msg **address_of_msg)
1898{
Dean Nelsona47d5da2008-07-29 22:34:09 -07001899 struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
Dean Nelson33ba3c72008-07-29 22:34:07 -07001900 struct xpc_msg *msg;
1901 enum xp_retval ret;
1902 s64 put;
1903
Dean Nelson33ba3c72008-07-29 22:34:07 -07001904 /*
1905 * Get the next available message entry from the local message queue.
1906 * If none are available, we'll make sure that we grab the latest
1907 * GP values.
1908 */
1909 ret = xpTimeout;
1910
1911 while (1) {
1912
Dean Nelsona47d5da2008-07-29 22:34:09 -07001913 put = ch_sn2->w_local_GP.put;
Dean Nelson33ba3c72008-07-29 22:34:07 -07001914 rmb(); /* guarantee that .put loads before .get */
Dean Nelsona47d5da2008-07-29 22:34:09 -07001915 if (put - ch_sn2->w_remote_GP.get < ch->local_nentries) {
Dean Nelson33ba3c72008-07-29 22:34:07 -07001916
1917 /* There are available message entries. We need to try
1918 * to secure one for ourselves. We'll do this by trying
1919 * to increment w_local_GP.put as long as someone else
1920 * doesn't beat us to it. If they do, we'll have to
1921 * try again.
1922 */
Dean Nelsona47d5da2008-07-29 22:34:09 -07001923 if (cmpxchg(&ch_sn2->w_local_GP.put, put, put + 1) ==
1924 put) {
Dean Nelson33ba3c72008-07-29 22:34:07 -07001925 /* we got the entry referenced by put */
1926 break;
1927 }
1928 continue; /* try again */
1929 }
1930
1931 /*
1932 * There aren't any available msg entries at this time.
1933 *
1934 * In waiting for a message entry to become available,
Dean Nelson7fb5e592008-07-29 22:34:10 -07001935 * we set a timeout in case the other side is not sending
1936 * completion interrupts. This lets us fake a notify IRQ
1937 * that will cause the notify IRQ handler to fetch the latest
1938 * GP values as if an interrupt was sent by the other side.
Dean Nelson33ba3c72008-07-29 22:34:07 -07001939 */
1940 if (ret == xpTimeout)
Dean Nelson7fb5e592008-07-29 22:34:10 -07001941 xpc_send_chctl_local_msgrequest_sn2(ch);
Dean Nelson33ba3c72008-07-29 22:34:07 -07001942
Dean Nelson97bf1aa2008-07-29 22:34:08 -07001943 if (flags & XPC_NOWAIT)
Dean Nelson33ba3c72008-07-29 22:34:07 -07001944 return xpNoWait;
Dean Nelson33ba3c72008-07-29 22:34:07 -07001945
1946 ret = xpc_allocate_msg_wait(ch);
Dean Nelson97bf1aa2008-07-29 22:34:08 -07001947 if (ret != xpInterrupted && ret != xpTimeout)
Dean Nelson33ba3c72008-07-29 22:34:07 -07001948 return ret;
Dean Nelson33ba3c72008-07-29 22:34:07 -07001949 }
1950
1951 /* get the message's address and initialize it */
1952 msg = (struct xpc_msg *)((u64)ch->local_msgqueue +
1953 (put % ch->local_nentries) * ch->msg_size);
1954
1955 DBUG_ON(msg->flags != 0);
1956 msg->number = put;
1957
1958 dev_dbg(xpc_chan, "w_local_GP.put changed to %ld; msg=0x%p, "
1959 "msg_number=%ld, partid=%d, channel=%d\n", put + 1,
1960 (void *)msg, msg->number, ch->partid, ch->number);
1961
1962 *address_of_msg = msg;
Dean Nelson33ba3c72008-07-29 22:34:07 -07001963 return xpSuccess;
1964}
1965
1966/*
1967 * Common code that does the actual sending of the message by advancing the
Dean Nelson7fb5e592008-07-29 22:34:10 -07001968 * local message queue's Put value and sends a chctl msgrequest to the
1969 * partition the message is being sent to.
Dean Nelson33ba3c72008-07-29 22:34:07 -07001970 */
1971static enum xp_retval
Dean Nelson97bf1aa2008-07-29 22:34:08 -07001972xpc_send_msg_sn2(struct xpc_channel *ch, u32 flags, void *payload,
1973 u16 payload_size, u8 notify_type, xpc_notify_func func,
1974 void *key)
Dean Nelson33ba3c72008-07-29 22:34:07 -07001975{
1976 enum xp_retval ret = xpSuccess;
Dean Nelson97bf1aa2008-07-29 22:34:08 -07001977 struct xpc_msg *msg = msg;
Dean Nelson33ba3c72008-07-29 22:34:07 -07001978 struct xpc_notify *notify = notify;
Dean Nelson97bf1aa2008-07-29 22:34:08 -07001979 s64 msg_number;
1980 s64 put;
Dean Nelson33ba3c72008-07-29 22:34:07 -07001981
1982 DBUG_ON(notify_type == XPC_N_CALL && func == NULL);
Dean Nelson97bf1aa2008-07-29 22:34:08 -07001983
1984 if (XPC_MSG_SIZE(payload_size) > ch->msg_size)
1985 return xpPayloadTooBig;
1986
1987 xpc_msgqueue_ref(ch);
Dean Nelson33ba3c72008-07-29 22:34:07 -07001988
1989 if (ch->flags & XPC_C_DISCONNECTING) {
Dean Nelson97bf1aa2008-07-29 22:34:08 -07001990 ret = ch->reason;
1991 goto out_1;
Dean Nelson33ba3c72008-07-29 22:34:07 -07001992 }
Dean Nelson97bf1aa2008-07-29 22:34:08 -07001993 if (!(ch->flags & XPC_C_CONNECTED)) {
1994 ret = xpNotConnected;
1995 goto out_1;
1996 }
1997
1998 ret = xpc_allocate_msg_sn2(ch, flags, &msg);
1999 if (ret != xpSuccess)
2000 goto out_1;
2001
2002 msg_number = msg->number;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002003
2004 if (notify_type != 0) {
2005 /*
2006 * Tell the remote side to send an ACK interrupt when the
2007 * message has been delivered.
2008 */
2009 msg->flags |= XPC_M_INTERRUPT;
2010
2011 atomic_inc(&ch->n_to_notify);
2012
2013 notify = &ch->notify_queue[msg_number % ch->local_nentries];
2014 notify->func = func;
2015 notify->key = key;
2016 notify->type = notify_type;
2017
2018 /* >>> is a mb() needed here? */
2019
2020 if (ch->flags & XPC_C_DISCONNECTING) {
2021 /*
2022 * An error occurred between our last error check and
2023 * this one. We will try to clear the type field from
2024 * the notify entry. If we succeed then
2025 * xpc_disconnect_channel() didn't already process
2026 * the notify entry.
2027 */
2028 if (cmpxchg(&notify->type, notify_type, 0) ==
2029 notify_type) {
2030 atomic_dec(&ch->n_to_notify);
2031 ret = ch->reason;
2032 }
Dean Nelson97bf1aa2008-07-29 22:34:08 -07002033 goto out_1;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002034 }
2035 }
2036
Dean Nelson97bf1aa2008-07-29 22:34:08 -07002037 memcpy(&msg->payload, payload, payload_size);
2038
Dean Nelson33ba3c72008-07-29 22:34:07 -07002039 msg->flags |= XPC_M_READY;
2040
2041 /*
2042 * The preceding store of msg->flags must occur before the following
Dean Nelsona47d5da2008-07-29 22:34:09 -07002043 * load of local_GP->put.
Dean Nelson33ba3c72008-07-29 22:34:07 -07002044 */
2045 mb();
2046
2047 /* see if the message is next in line to be sent, if so send it */
2048
Dean Nelsona47d5da2008-07-29 22:34:09 -07002049 put = ch->sn.sn2.local_GP->put;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002050 if (put == msg_number)
2051 xpc_send_msgs_sn2(ch, put);
2052
Dean Nelson97bf1aa2008-07-29 22:34:08 -07002053out_1:
Dean Nelson33ba3c72008-07-29 22:34:07 -07002054 xpc_msgqueue_deref(ch);
2055 return ret;
2056}
2057
2058/*
2059 * Now we actually acknowledge the messages that have been delivered and ack'd
2060 * by advancing the cached remote message queue's Get value and if requested
Dean Nelson7fb5e592008-07-29 22:34:10 -07002061 * send a chctl msgrequest to the message sender's partition.
Dean Nelson33ba3c72008-07-29 22:34:07 -07002062 */
2063static void
2064xpc_acknowledge_msgs_sn2(struct xpc_channel *ch, s64 initial_get, u8 msg_flags)
2065{
Dean Nelsona47d5da2008-07-29 22:34:09 -07002066 struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002067 struct xpc_msg *msg;
2068 s64 get = initial_get + 1;
Dean Nelson7fb5e592008-07-29 22:34:10 -07002069 int send_msgrequest = 0;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002070
2071 while (1) {
2072
2073 while (1) {
Dean Nelsona47d5da2008-07-29 22:34:09 -07002074 if (get == ch_sn2->w_local_GP.get)
Dean Nelson33ba3c72008-07-29 22:34:07 -07002075 break;
2076
2077 msg = (struct xpc_msg *)((u64)ch->remote_msgqueue +
2078 (get % ch->remote_nentries) *
2079 ch->msg_size);
2080
2081 if (!(msg->flags & XPC_M_DONE))
2082 break;
2083
2084 msg_flags |= msg->flags;
2085 get++;
2086 }
2087
2088 if (get == initial_get) {
2089 /* nothing's changed */
2090 break;
2091 }
2092
Dean Nelsona47d5da2008-07-29 22:34:09 -07002093 if (cmpxchg_rel(&ch_sn2->local_GP->get, initial_get, get) !=
Dean Nelson33ba3c72008-07-29 22:34:07 -07002094 initial_get) {
2095 /* someone else beat us to it */
Dean Nelsona47d5da2008-07-29 22:34:09 -07002096 DBUG_ON(ch_sn2->local_GP->get <= initial_get);
Dean Nelson33ba3c72008-07-29 22:34:07 -07002097 break;
2098 }
2099
2100 /* we just set the new value of local_GP->get */
2101
2102 dev_dbg(xpc_chan, "local_GP->get changed to %ld, partid=%d, "
2103 "channel=%d\n", get, ch->partid, ch->number);
2104
Dean Nelson7fb5e592008-07-29 22:34:10 -07002105 send_msgrequest = (msg_flags & XPC_M_INTERRUPT);
Dean Nelson33ba3c72008-07-29 22:34:07 -07002106
2107 /*
2108 * We need to ensure that the message referenced by
2109 * local_GP->get is not XPC_M_DONE or that local_GP->get
2110 * equals w_local_GP.get, so we'll go have a look.
2111 */
2112 initial_get = get;
2113 }
2114
Dean Nelson7fb5e592008-07-29 22:34:10 -07002115 if (send_msgrequest)
2116 xpc_send_chctl_msgrequest_sn2(ch);
Dean Nelson33ba3c72008-07-29 22:34:07 -07002117}
2118
2119static void
2120xpc_received_msg_sn2(struct xpc_channel *ch, struct xpc_msg *msg)
2121{
2122 s64 get;
2123 s64 msg_number = msg->number;
2124
2125 dev_dbg(xpc_chan, "msg=0x%p, msg_number=%ld, partid=%d, channel=%d\n",
2126 (void *)msg, msg_number, ch->partid, ch->number);
2127
2128 DBUG_ON((((u64)msg - (u64)ch->remote_msgqueue) / ch->msg_size) !=
2129 msg_number % ch->remote_nentries);
2130 DBUG_ON(msg->flags & XPC_M_DONE);
2131
2132 msg->flags |= XPC_M_DONE;
2133
2134 /*
2135 * The preceding store of msg->flags must occur before the following
Dean Nelsona47d5da2008-07-29 22:34:09 -07002136 * load of local_GP->get.
Dean Nelson33ba3c72008-07-29 22:34:07 -07002137 */
2138 mb();
2139
2140 /*
2141 * See if this message is next in line to be acknowledged as having
2142 * been delivered.
2143 */
Dean Nelsona47d5da2008-07-29 22:34:09 -07002144 get = ch->sn.sn2.local_GP->get;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002145 if (get == msg_number)
2146 xpc_acknowledge_msgs_sn2(ch, get, msg->flags);
2147}
2148
Dean Nelson6e410172008-07-29 22:34:09 -07002149int
Dean Nelson94bd2702008-07-29 22:34:05 -07002150xpc_init_sn2(void)
2151{
Dean Nelson6e410172008-07-29 22:34:09 -07002152 int ret;
2153
Dean Nelson94bd2702008-07-29 22:34:05 -07002154 xpc_rsvd_page_init = xpc_rsvd_page_init_sn2;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002155 xpc_increment_heartbeat = xpc_increment_heartbeat_sn2;
2156 xpc_offline_heartbeat = xpc_offline_heartbeat_sn2;
2157 xpc_online_heartbeat = xpc_online_heartbeat_sn2;
2158 xpc_heartbeat_init = xpc_heartbeat_init_sn2;
2159 xpc_heartbeat_exit = xpc_heartbeat_exit_sn2;
2160 xpc_check_remote_hb = xpc_check_remote_hb_sn2;
2161
Dean Nelsona47d5da2008-07-29 22:34:09 -07002162 xpc_request_partition_activation = xpc_request_partition_activation_sn2;
2163 xpc_request_partition_reactivation =
2164 xpc_request_partition_reactivation_sn2;
2165 xpc_request_partition_deactivation =
2166 xpc_request_partition_deactivation_sn2;
2167 xpc_cancel_partition_deactivation_request =
2168 xpc_cancel_partition_deactivation_request_sn2;
2169
Dean Nelson6e410172008-07-29 22:34:09 -07002170 xpc_process_activate_IRQ_rcvd = xpc_process_activate_IRQ_rcvd_sn2;
Dean Nelsone17d4162008-07-29 22:34:06 -07002171 xpc_setup_infrastructure = xpc_setup_infrastructure_sn2;
2172 xpc_teardown_infrastructure = xpc_teardown_infrastructure_sn2;
2173 xpc_make_first_contact = xpc_make_first_contact_sn2;
Dean Nelson7fb5e592008-07-29 22:34:10 -07002174 xpc_get_chctl_all_flags = xpc_get_chctl_all_flags_sn2;
Dean Nelsona47d5da2008-07-29 22:34:09 -07002175 xpc_notify_senders_of_disconnect = xpc_notify_senders_of_disconnect_sn2;
Dean Nelson7fb5e592008-07-29 22:34:10 -07002176 xpc_process_msg_chctl_flags = xpc_process_msg_chctl_flags_sn2;
Dean Nelsona47d5da2008-07-29 22:34:09 -07002177 xpc_n_of_deliverable_msgs = xpc_n_of_deliverable_msgs_sn2;
Dean Nelsone17d4162008-07-29 22:34:06 -07002178 xpc_get_deliverable_msg = xpc_get_deliverable_msg_sn2;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002179
Dean Nelsona47d5da2008-07-29 22:34:09 -07002180 xpc_indicate_partition_engaged = xpc_indicate_partition_engaged_sn2;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002181 xpc_partition_engaged = xpc_partition_engaged_sn2;
Dean Nelsona47d5da2008-07-29 22:34:09 -07002182 xpc_any_partition_engaged = xpc_any_partition_engaged_sn2;
2183 xpc_indicate_partition_disengaged =
2184 xpc_indicate_partition_disengaged_sn2;
2185 xpc_assume_partition_disengaged = xpc_assume_partition_disengaged_sn2;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002186
Dean Nelson7fb5e592008-07-29 22:34:10 -07002187 xpc_send_chctl_closerequest = xpc_send_chctl_closerequest_sn2;
2188 xpc_send_chctl_closereply = xpc_send_chctl_closereply_sn2;
2189 xpc_send_chctl_openrequest = xpc_send_chctl_openrequest_sn2;
2190 xpc_send_chctl_openreply = xpc_send_chctl_openreply_sn2;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002191
Dean Nelson33ba3c72008-07-29 22:34:07 -07002192 xpc_send_msg = xpc_send_msg_sn2;
2193 xpc_received_msg = xpc_received_msg_sn2;
Dean Nelson6e410172008-07-29 22:34:09 -07002194
2195 /* open up protections for IPI and [potentially] AMO operations */
2196 xpc_allow_IPI_ops_sn2();
2197 xpc_allow_AMO_ops_shub_wars_1_1_sn2();
2198
2199 /*
2200 * This is safe to do before the xpc_hb_checker thread has started
2201 * because the handler releases a wait queue. If an interrupt is
2202 * received before the thread is waiting, it will not go to sleep,
2203 * but rather immediately process the interrupt.
2204 */
2205 ret = request_irq(SGI_XPC_ACTIVATE, xpc_handle_activate_IRQ_sn2, 0,
2206 "xpc hb", NULL);
2207 if (ret != 0) {
2208 dev_err(xpc_part, "can't register ACTIVATE IRQ handler, "
2209 "errno=%d\n", -ret);
2210 xpc_disallow_IPI_ops_sn2();
2211 }
2212 return ret;
Dean Nelson94bd2702008-07-29 22:34:05 -07002213}
2214
2215void
2216xpc_exit_sn2(void)
2217{
Dean Nelson6e410172008-07-29 22:34:09 -07002218 free_irq(SGI_XPC_ACTIVATE, NULL);
2219 xpc_disallow_IPI_ops_sn2();
Dean Nelson94bd2702008-07-29 22:34:05 -07002220}