blob: fde870aebcb98c482d72ad43a1723415caa028c6 [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 */
56static char *xpc_remote_copy_buffer_sn2;
57static void *xpc_remote_copy_buffer_base_sn2;
58
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 Nelson6e410172008-07-29 22:34:09 -070062/* SH_IPI_ACCESS shub register value on startup */
Dean Nelson8e85c232008-07-29 22:34:13 -070063static u64 xpc_sh1_IPI_access_sn2;
64static u64 xpc_sh2_IPI_access0_sn2;
65static u64 xpc_sh2_IPI_access1_sn2;
66static u64 xpc_sh2_IPI_access2_sn2;
67static u64 xpc_sh2_IPI_access3_sn2;
Dean Nelson6e410172008-07-29 22:34:09 -070068
69/*
70 * Change protections to allow IPI operations.
71 */
72static void
73xpc_allow_IPI_ops_sn2(void)
74{
75 int node;
76 int nasid;
77
Dean Nelsonea57f802008-07-29 22:34:14 -070078 /* !!! The following should get moved into SAL. */
Dean Nelson6e410172008-07-29 22:34:09 -070079 if (is_shub2()) {
Dean Nelson8e85c232008-07-29 22:34:13 -070080 xpc_sh2_IPI_access0_sn2 =
Dean Nelson6e410172008-07-29 22:34:09 -070081 (u64)HUB_L((u64 *)LOCAL_MMR_ADDR(SH2_IPI_ACCESS0));
Dean Nelson8e85c232008-07-29 22:34:13 -070082 xpc_sh2_IPI_access1_sn2 =
Dean Nelson6e410172008-07-29 22:34:09 -070083 (u64)HUB_L((u64 *)LOCAL_MMR_ADDR(SH2_IPI_ACCESS1));
Dean Nelson8e85c232008-07-29 22:34:13 -070084 xpc_sh2_IPI_access2_sn2 =
Dean Nelson6e410172008-07-29 22:34:09 -070085 (u64)HUB_L((u64 *)LOCAL_MMR_ADDR(SH2_IPI_ACCESS2));
Dean Nelson8e85c232008-07-29 22:34:13 -070086 xpc_sh2_IPI_access3_sn2 =
Dean Nelson6e410172008-07-29 22:34:09 -070087 (u64)HUB_L((u64 *)LOCAL_MMR_ADDR(SH2_IPI_ACCESS3));
88
89 for_each_online_node(node) {
90 nasid = cnodeid_to_nasid(node);
91 HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS0),
92 -1UL);
93 HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS1),
94 -1UL);
95 HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS2),
96 -1UL);
97 HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS3),
98 -1UL);
99 }
100 } else {
Dean Nelson8e85c232008-07-29 22:34:13 -0700101 xpc_sh1_IPI_access_sn2 =
Dean Nelson6e410172008-07-29 22:34:09 -0700102 (u64)HUB_L((u64 *)LOCAL_MMR_ADDR(SH1_IPI_ACCESS));
103
104 for_each_online_node(node) {
105 nasid = cnodeid_to_nasid(node);
106 HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH1_IPI_ACCESS),
107 -1UL);
108 }
109 }
110}
111
112/*
113 * Restrict protections to disallow IPI operations.
114 */
115static void
116xpc_disallow_IPI_ops_sn2(void)
117{
118 int node;
119 int nasid;
120
Dean Nelsonea57f802008-07-29 22:34:14 -0700121 /* !!! The following should get moved into SAL. */
Dean Nelson6e410172008-07-29 22:34:09 -0700122 if (is_shub2()) {
123 for_each_online_node(node) {
124 nasid = cnodeid_to_nasid(node);
125 HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS0),
Dean Nelson8e85c232008-07-29 22:34:13 -0700126 xpc_sh2_IPI_access0_sn2);
Dean Nelson6e410172008-07-29 22:34:09 -0700127 HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS1),
Dean Nelson8e85c232008-07-29 22:34:13 -0700128 xpc_sh2_IPI_access1_sn2);
Dean Nelson6e410172008-07-29 22:34:09 -0700129 HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS2),
Dean Nelson8e85c232008-07-29 22:34:13 -0700130 xpc_sh2_IPI_access2_sn2);
Dean Nelson6e410172008-07-29 22:34:09 -0700131 HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS3),
Dean Nelson8e85c232008-07-29 22:34:13 -0700132 xpc_sh2_IPI_access3_sn2);
Dean Nelson6e410172008-07-29 22:34:09 -0700133 }
134 } else {
135 for_each_online_node(node) {
136 nasid = cnodeid_to_nasid(node);
137 HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH1_IPI_ACCESS),
Dean Nelson8e85c232008-07-29 22:34:13 -0700138 xpc_sh1_IPI_access_sn2);
Dean Nelson6e410172008-07-29 22:34:09 -0700139 }
140 }
141}
142
Dean Nelson33ba3c72008-07-29 22:34:07 -0700143/*
Dean Nelson7fb5e592008-07-29 22:34:10 -0700144 * The following set of functions are used for the sending and receiving of
145 * IRQs (also known as IPIs). There are two flavors of IRQs, one that is
146 * associated with partition activity (SGI_XPC_ACTIVATE) and the other that
147 * is associated with channel activity (SGI_XPC_NOTIFY).
Dean Nelson33ba3c72008-07-29 22:34:07 -0700148 */
149
150static u64
Dean Nelsonc39838c2008-07-29 22:34:11 -0700151xpc_receive_IRQ_amo_sn2(struct amo *amo)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700152{
153 return FETCHOP_LOAD_OP(TO_AMO((u64)&amo->variable), FETCHOP_CLEAR);
154}
155
156static enum xp_retval
Dean Nelsonc39838c2008-07-29 22:34:11 -0700157xpc_send_IRQ_sn2(struct amo *amo, u64 flag, int nasid, int phys_cpuid,
158 int vector)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700159{
160 int ret = 0;
161 unsigned long irq_flags;
162
163 local_irq_save(irq_flags);
164
165 FETCHOP_STORE_OP(TO_AMO((u64)&amo->variable), FETCHOP_OR, flag);
166 sn_send_IPI_phys(nasid, phys_cpuid, vector, 0);
167
168 /*
169 * We must always use the nofault function regardless of whether we
170 * are on a Shub 1.1 system or a Shub 1.2 slice 0xc processor. If we
171 * didn't, we'd never know that the other partition is down and would
Dean Nelsonc39838c2008-07-29 22:34:11 -0700172 * keep sending IRQs and amos to it until the heartbeat times out.
Dean Nelson33ba3c72008-07-29 22:34:07 -0700173 */
174 ret = xp_nofault_PIOR((u64 *)GLOBAL_MMR_ADDR(NASID_GET(&amo->variable),
175 xp_nofault_PIOR_target));
176
177 local_irq_restore(irq_flags);
178
Dean Nelson261f3b42008-07-29 22:34:16 -0700179 return (ret == 0) ? xpSuccess : xpPioReadError;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700180}
181
Dean Nelsonc39838c2008-07-29 22:34:11 -0700182static struct amo *
Dean Nelson7fb5e592008-07-29 22:34:10 -0700183xpc_init_IRQ_amo_sn2(int index)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700184{
Dean Nelson8e85c232008-07-29 22:34:13 -0700185 struct amo *amo = xpc_vars_sn2->amos_page + index;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700186
Dean Nelsonc39838c2008-07-29 22:34:11 -0700187 (void)xpc_receive_IRQ_amo_sn2(amo); /* clear amo variable */
Dean Nelson33ba3c72008-07-29 22:34:07 -0700188 return amo;
189}
190
191/*
Dean Nelson7fb5e592008-07-29 22:34:10 -0700192 * Functions associated with SGI_XPC_ACTIVATE IRQ.
Dean Nelson33ba3c72008-07-29 22:34:07 -0700193 */
194
195/*
Dean Nelson6e410172008-07-29 22:34:09 -0700196 * Notify the heartbeat check thread that an activate IRQ has been received.
197 */
198static irqreturn_t
199xpc_handle_activate_IRQ_sn2(int irq, void *dev_id)
200{
201 atomic_inc(&xpc_activate_IRQ_rcvd);
202 wake_up_interruptible(&xpc_activate_IRQ_wq);
203 return IRQ_HANDLED;
204}
205
206/*
Dean Nelsonc39838c2008-07-29 22:34:11 -0700207 * Flag the appropriate amo variable and send an IRQ to the specified node.
Dean Nelson33ba3c72008-07-29 22:34:07 -0700208 */
209static void
Dean Nelson7fb5e592008-07-29 22:34:10 -0700210xpc_send_activate_IRQ_sn2(u64 amos_page_pa, int from_nasid, int to_nasid,
211 int to_phys_cpuid)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700212{
Dean Nelsonc39838c2008-07-29 22:34:11 -0700213 struct amo *amos = (struct amo *)__va(amos_page_pa +
Dean Nelsonee6665e2008-07-29 22:34:13 -0700214 (XPC_ACTIVATE_IRQ_AMOS_SN2 *
Dean Nelsonc39838c2008-07-29 22:34:11 -0700215 sizeof(struct amo)));
Dean Nelson33ba3c72008-07-29 22:34:07 -0700216
Dean Nelson04de7412008-07-29 22:34:14 -0700217 (void)xpc_send_IRQ_sn2(&amos[BIT_WORD(from_nasid / 2)],
218 BIT_MASK(from_nasid / 2), to_nasid,
Dean Nelson33ba3c72008-07-29 22:34:07 -0700219 to_phys_cpuid, SGI_XPC_ACTIVATE);
220}
221
222static void
Dean Nelson7fb5e592008-07-29 22:34:10 -0700223xpc_send_local_activate_IRQ_sn2(int from_nasid)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700224{
Dean Nelson8e85c232008-07-29 22:34:13 -0700225 struct amo *amos = (struct amo *)__va(xpc_vars_sn2->amos_page_pa +
Dean Nelsonee6665e2008-07-29 22:34:13 -0700226 (XPC_ACTIVATE_IRQ_AMOS_SN2 *
Dean Nelsonc39838c2008-07-29 22:34:11 -0700227 sizeof(struct amo)));
Dean Nelson33ba3c72008-07-29 22:34:07 -0700228
229 /* fake the sending and receipt of an activate IRQ from remote nasid */
Dean Nelson04de7412008-07-29 22:34:14 -0700230 FETCHOP_STORE_OP(TO_AMO((u64)&amos[BIT_WORD(from_nasid / 2)].variable),
231 FETCHOP_OR, BIT_MASK(from_nasid / 2));
232
Dean Nelson6e410172008-07-29 22:34:09 -0700233 atomic_inc(&xpc_activate_IRQ_rcvd);
234 wake_up_interruptible(&xpc_activate_IRQ_wq);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700235}
236
Dean Nelson33ba3c72008-07-29 22:34:07 -0700237/*
Dean Nelson7fb5e592008-07-29 22:34:10 -0700238 * Functions associated with SGI_XPC_NOTIFY IRQ.
Dean Nelson33ba3c72008-07-29 22:34:07 -0700239 */
240
241/*
Dean Nelson7fb5e592008-07-29 22:34:10 -0700242 * Check to see if any chctl flags were sent from the specified partition.
Dean Nelsona47d5da2008-07-29 22:34:09 -0700243 */
244static void
Dean Nelson7fb5e592008-07-29 22:34:10 -0700245xpc_check_for_sent_chctl_flags_sn2(struct xpc_partition *part)
Dean Nelsona47d5da2008-07-29 22:34:09 -0700246{
Dean Nelson7fb5e592008-07-29 22:34:10 -0700247 union xpc_channel_ctl_flags chctl;
Dean Nelsona47d5da2008-07-29 22:34:09 -0700248 unsigned long irq_flags;
249
Dean Nelson7fb5e592008-07-29 22:34:10 -0700250 chctl.all_flags = xpc_receive_IRQ_amo_sn2(part->sn.sn2.
251 local_chctl_amo_va);
252 if (chctl.all_flags == 0)
Dean Nelsona47d5da2008-07-29 22:34:09 -0700253 return;
254
Dean Nelson7fb5e592008-07-29 22:34:10 -0700255 spin_lock_irqsave(&part->chctl_lock, irq_flags);
256 part->chctl.all_flags |= chctl.all_flags;
257 spin_unlock_irqrestore(&part->chctl_lock, irq_flags);
Dean Nelsona47d5da2008-07-29 22:34:09 -0700258
Dean Nelson7fb5e592008-07-29 22:34:10 -0700259 dev_dbg(xpc_chan, "received notify IRQ from partid=%d, chctl.all_flags="
260 "0x%lx\n", XPC_PARTID(part), chctl.all_flags);
Dean Nelsona47d5da2008-07-29 22:34:09 -0700261
262 xpc_wakeup_channel_mgr(part);
263}
264
265/*
266 * Handle the receipt of a SGI_XPC_NOTIFY IRQ by seeing whether the specified
267 * partition actually sent it. Since SGI_XPC_NOTIFY IRQs may be shared by more
Dean Nelsonc39838c2008-07-29 22:34:11 -0700268 * than one partition, we use an amo structure per partition to indicate
Dean Nelson7fb5e592008-07-29 22:34:10 -0700269 * whether a partition has sent an IRQ or not. If it has, then wake up the
Dean Nelsona47d5da2008-07-29 22:34:09 -0700270 * associated kthread to handle it.
271 *
Dean Nelson7fb5e592008-07-29 22:34:10 -0700272 * All SGI_XPC_NOTIFY IRQs received by XPC are the result of IRQs sent by XPC
Dean Nelsona47d5da2008-07-29 22:34:09 -0700273 * running on other partitions.
274 *
275 * Noteworthy Arguments:
276 *
277 * irq - Interrupt ReQuest number. NOT USED.
278 *
Dean Nelson7fb5e592008-07-29 22:34:10 -0700279 * dev_id - partid of IRQ's potential sender.
Dean Nelsona47d5da2008-07-29 22:34:09 -0700280 */
281static irqreturn_t
282xpc_handle_notify_IRQ_sn2(int irq, void *dev_id)
283{
284 short partid = (short)(u64)dev_id;
285 struct xpc_partition *part = &xpc_partitions[partid];
286
Dean Nelson261f3b42008-07-29 22:34:16 -0700287 DBUG_ON(partid < 0 || partid >= XP_MAX_NPARTITIONS_SN2);
Dean Nelsona47d5da2008-07-29 22:34:09 -0700288
289 if (xpc_part_ref(part)) {
Dean Nelson7fb5e592008-07-29 22:34:10 -0700290 xpc_check_for_sent_chctl_flags_sn2(part);
Dean Nelsona47d5da2008-07-29 22:34:09 -0700291
292 xpc_part_deref(part);
293 }
294 return IRQ_HANDLED;
295}
296
297/*
Dean Nelson7fb5e592008-07-29 22:34:10 -0700298 * Check to see if xpc_handle_notify_IRQ_sn2() dropped any IRQs on the floor
299 * because the write to their associated amo variable completed after the IRQ
Dean Nelsona47d5da2008-07-29 22:34:09 -0700300 * was received.
301 */
302static void
Dean Nelson7fb5e592008-07-29 22:34:10 -0700303xpc_check_for_dropped_notify_IRQ_sn2(struct xpc_partition *part)
Dean Nelsona47d5da2008-07-29 22:34:09 -0700304{
305 struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
306
307 if (xpc_part_ref(part)) {
Dean Nelson7fb5e592008-07-29 22:34:10 -0700308 xpc_check_for_sent_chctl_flags_sn2(part);
Dean Nelsona47d5da2008-07-29 22:34:09 -0700309
310 part_sn2->dropped_notify_IRQ_timer.expires = jiffies +
Dean Nelson7fb5e592008-07-29 22:34:10 -0700311 XPC_DROPPED_NOTIFY_IRQ_WAIT_INTERVAL;
Dean Nelsona47d5da2008-07-29 22:34:09 -0700312 add_timer(&part_sn2->dropped_notify_IRQ_timer);
313 xpc_part_deref(part);
314 }
315}
316
317/*
Dean Nelson7fb5e592008-07-29 22:34:10 -0700318 * Send a notify IRQ to the remote partition that is associated with the
Dean Nelson33ba3c72008-07-29 22:34:07 -0700319 * specified channel.
320 */
321static void
Dean Nelson7fb5e592008-07-29 22:34:10 -0700322xpc_send_notify_IRQ_sn2(struct xpc_channel *ch, u8 chctl_flag,
323 char *chctl_flag_string, unsigned long *irq_flags)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700324{
325 struct xpc_partition *part = &xpc_partitions[ch->partid];
Dean Nelsona47d5da2008-07-29 22:34:09 -0700326 struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
Dean Nelson7fb5e592008-07-29 22:34:10 -0700327 union xpc_channel_ctl_flags chctl = { 0 };
Dean Nelson33ba3c72008-07-29 22:34:07 -0700328 enum xp_retval ret;
329
330 if (likely(part->act_state != XPC_P_DEACTIVATING)) {
Dean Nelson7fb5e592008-07-29 22:34:10 -0700331 chctl.flags[ch->number] = chctl_flag;
332 ret = xpc_send_IRQ_sn2(part_sn2->remote_chctl_amo_va,
333 chctl.all_flags,
334 part_sn2->notify_IRQ_nasid,
335 part_sn2->notify_IRQ_phys_cpuid,
Dean Nelson33ba3c72008-07-29 22:34:07 -0700336 SGI_XPC_NOTIFY);
337 dev_dbg(xpc_chan, "%s sent to partid=%d, channel=%d, ret=%d\n",
Dean Nelson7fb5e592008-07-29 22:34:10 -0700338 chctl_flag_string, ch->partid, ch->number, ret);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700339 if (unlikely(ret != xpSuccess)) {
340 if (irq_flags != NULL)
341 spin_unlock_irqrestore(&ch->lock, *irq_flags);
342 XPC_DEACTIVATE_PARTITION(part, ret);
343 if (irq_flags != NULL)
344 spin_lock_irqsave(&ch->lock, *irq_flags);
345 }
346 }
347}
348
Dean Nelson7fb5e592008-07-29 22:34:10 -0700349#define XPC_SEND_NOTIFY_IRQ_SN2(_ch, _ipi_f, _irq_f) \
350 xpc_send_notify_IRQ_sn2(_ch, _ipi_f, #_ipi_f, _irq_f)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700351
352/*
353 * Make it look like the remote partition, which is associated with the
Dean Nelson7fb5e592008-07-29 22:34:10 -0700354 * specified channel, sent us a notify IRQ. This faked IRQ will be handled
355 * by xpc_check_for_dropped_notify_IRQ_sn2().
Dean Nelson33ba3c72008-07-29 22:34:07 -0700356 */
357static void
Dean Nelson7fb5e592008-07-29 22:34:10 -0700358xpc_send_local_notify_IRQ_sn2(struct xpc_channel *ch, u8 chctl_flag,
359 char *chctl_flag_string)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700360{
361 struct xpc_partition *part = &xpc_partitions[ch->partid];
Dean Nelson7fb5e592008-07-29 22:34:10 -0700362 union xpc_channel_ctl_flags chctl = { 0 };
Dean Nelson33ba3c72008-07-29 22:34:07 -0700363
Dean Nelson7fb5e592008-07-29 22:34:10 -0700364 chctl.flags[ch->number] = chctl_flag;
365 FETCHOP_STORE_OP(TO_AMO((u64)&part->sn.sn2.local_chctl_amo_va->
366 variable), FETCHOP_OR, chctl.all_flags);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700367 dev_dbg(xpc_chan, "%s sent local from partid=%d, channel=%d\n",
Dean Nelson7fb5e592008-07-29 22:34:10 -0700368 chctl_flag_string, ch->partid, ch->number);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700369}
370
Dean Nelson7fb5e592008-07-29 22:34:10 -0700371#define XPC_SEND_LOCAL_NOTIFY_IRQ_SN2(_ch, _ipi_f) \
372 xpc_send_local_notify_IRQ_sn2(_ch, _ipi_f, #_ipi_f)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700373
374static void
Dean Nelson7fb5e592008-07-29 22:34:10 -0700375xpc_send_chctl_closerequest_sn2(struct xpc_channel *ch,
376 unsigned long *irq_flags)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700377{
378 struct xpc_openclose_args *args = ch->local_openclose_args;
379
380 args->reason = ch->reason;
Dean Nelson7fb5e592008-07-29 22:34:10 -0700381 XPC_SEND_NOTIFY_IRQ_SN2(ch, XPC_CHCTL_CLOSEREQUEST, irq_flags);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700382}
383
384static void
Dean Nelson7fb5e592008-07-29 22:34:10 -0700385xpc_send_chctl_closereply_sn2(struct xpc_channel *ch, unsigned long *irq_flags)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700386{
Dean Nelson7fb5e592008-07-29 22:34:10 -0700387 XPC_SEND_NOTIFY_IRQ_SN2(ch, XPC_CHCTL_CLOSEREPLY, irq_flags);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700388}
389
390static void
Dean Nelson7fb5e592008-07-29 22:34:10 -0700391xpc_send_chctl_openrequest_sn2(struct xpc_channel *ch, unsigned long *irq_flags)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700392{
393 struct xpc_openclose_args *args = ch->local_openclose_args;
394
395 args->msg_size = ch->msg_size;
396 args->local_nentries = ch->local_nentries;
Dean Nelson7fb5e592008-07-29 22:34:10 -0700397 XPC_SEND_NOTIFY_IRQ_SN2(ch, XPC_CHCTL_OPENREQUEST, irq_flags);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700398}
399
400static void
Dean Nelson7fb5e592008-07-29 22:34:10 -0700401xpc_send_chctl_openreply_sn2(struct xpc_channel *ch, unsigned long *irq_flags)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700402{
403 struct xpc_openclose_args *args = ch->local_openclose_args;
404
405 args->remote_nentries = ch->remote_nentries;
406 args->local_nentries = ch->local_nentries;
407 args->local_msgqueue_pa = __pa(ch->local_msgqueue);
Dean Nelson7fb5e592008-07-29 22:34:10 -0700408 XPC_SEND_NOTIFY_IRQ_SN2(ch, XPC_CHCTL_OPENREPLY, irq_flags);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700409}
410
411static void
Dean Nelson7fb5e592008-07-29 22:34:10 -0700412xpc_send_chctl_msgrequest_sn2(struct xpc_channel *ch)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700413{
Dean Nelson7fb5e592008-07-29 22:34:10 -0700414 XPC_SEND_NOTIFY_IRQ_SN2(ch, XPC_CHCTL_MSGREQUEST, NULL);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700415}
416
417static void
Dean Nelson7fb5e592008-07-29 22:34:10 -0700418xpc_send_chctl_local_msgrequest_sn2(struct xpc_channel *ch)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700419{
Dean Nelson7fb5e592008-07-29 22:34:10 -0700420 XPC_SEND_LOCAL_NOTIFY_IRQ_SN2(ch, XPC_CHCTL_MSGREQUEST);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700421}
422
423/*
424 * This next set of functions are used to keep track of when a partition is
425 * potentially engaged in accessing memory belonging to another partition.
426 */
427
428static void
Dean Nelsona47d5da2008-07-29 22:34:09 -0700429xpc_indicate_partition_engaged_sn2(struct xpc_partition *part)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700430{
431 unsigned long irq_flags;
Dean Nelsonc39838c2008-07-29 22:34:11 -0700432 struct amo *amo = (struct amo *)__va(part->sn.sn2.remote_amos_page_pa +
Dean Nelsonee6665e2008-07-29 22:34:13 -0700433 (XPC_ENGAGED_PARTITIONS_AMO_SN2 *
Dean Nelsonc39838c2008-07-29 22:34:11 -0700434 sizeof(struct amo)));
Dean Nelson33ba3c72008-07-29 22:34:07 -0700435
436 local_irq_save(irq_flags);
437
Dean Nelsonc39838c2008-07-29 22:34:11 -0700438 /* set bit corresponding to our partid in remote partition's amo */
Dean Nelson33ba3c72008-07-29 22:34:07 -0700439 FETCHOP_STORE_OP(TO_AMO((u64)&amo->variable), FETCHOP_OR,
Dean Nelson04de7412008-07-29 22:34:14 -0700440 BIT(sn_partition_id));
441
Dean Nelson33ba3c72008-07-29 22:34:07 -0700442 /*
443 * We must always use the nofault function regardless of whether we
444 * are on a Shub 1.1 system or a Shub 1.2 slice 0xc processor. If we
445 * didn't, we'd never know that the other partition is down and would
Dean Nelsonc39838c2008-07-29 22:34:11 -0700446 * keep sending IRQs and amos to it until the heartbeat times out.
Dean Nelson33ba3c72008-07-29 22:34:07 -0700447 */
448 (void)xp_nofault_PIOR((u64 *)GLOBAL_MMR_ADDR(NASID_GET(&amo->
449 variable),
450 xp_nofault_PIOR_target));
451
452 local_irq_restore(irq_flags);
453}
454
455static void
Dean Nelsona47d5da2008-07-29 22:34:09 -0700456xpc_indicate_partition_disengaged_sn2(struct xpc_partition *part)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700457{
Dean Nelsona47d5da2008-07-29 22:34:09 -0700458 struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700459 unsigned long irq_flags;
Dean Nelsonc39838c2008-07-29 22:34:11 -0700460 struct amo *amo = (struct amo *)__va(part_sn2->remote_amos_page_pa +
Dean Nelsonee6665e2008-07-29 22:34:13 -0700461 (XPC_ENGAGED_PARTITIONS_AMO_SN2 *
Dean Nelsonc39838c2008-07-29 22:34:11 -0700462 sizeof(struct amo)));
Dean Nelson33ba3c72008-07-29 22:34:07 -0700463
464 local_irq_save(irq_flags);
465
Dean Nelsonc39838c2008-07-29 22:34:11 -0700466 /* clear bit corresponding to our partid in remote partition's amo */
Dean Nelson33ba3c72008-07-29 22:34:07 -0700467 FETCHOP_STORE_OP(TO_AMO((u64)&amo->variable), FETCHOP_AND,
Dean Nelson04de7412008-07-29 22:34:14 -0700468 ~BIT(sn_partition_id));
469
Dean Nelson33ba3c72008-07-29 22:34:07 -0700470 /*
471 * We must always use the nofault function regardless of whether we
472 * are on a Shub 1.1 system or a Shub 1.2 slice 0xc processor. If we
473 * didn't, we'd never know that the other partition is down and would
Dean Nelsonc39838c2008-07-29 22:34:11 -0700474 * keep sending IRQs and amos to it until the heartbeat times out.
Dean Nelson33ba3c72008-07-29 22:34:07 -0700475 */
476 (void)xp_nofault_PIOR((u64 *)GLOBAL_MMR_ADDR(NASID_GET(&amo->
477 variable),
478 xp_nofault_PIOR_target));
479
480 local_irq_restore(irq_flags);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700481
Dean Nelson33ba3c72008-07-29 22:34:07 -0700482 /*
Dean Nelsona47d5da2008-07-29 22:34:09 -0700483 * Send activate IRQ to get other side to see that we've cleared our
Dean Nelsonc39838c2008-07-29 22:34:11 -0700484 * bit in their engaged partitions amo.
Dean Nelson33ba3c72008-07-29 22:34:07 -0700485 */
Dean Nelson7fb5e592008-07-29 22:34:10 -0700486 xpc_send_activate_IRQ_sn2(part_sn2->remote_amos_page_pa,
Dean Nelsona47d5da2008-07-29 22:34:09 -0700487 cnodeid_to_nasid(0),
488 part_sn2->activate_IRQ_nasid,
489 part_sn2->activate_IRQ_phys_cpuid);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700490}
491
Dean Nelsona47d5da2008-07-29 22:34:09 -0700492static int
493xpc_partition_engaged_sn2(short partid)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700494{
Dean Nelson8e85c232008-07-29 22:34:13 -0700495 struct amo *amo = xpc_vars_sn2->amos_page +
496 XPC_ENGAGED_PARTITIONS_AMO_SN2;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700497
Dean Nelsonc39838c2008-07-29 22:34:11 -0700498 /* our partition's amo variable ANDed with partid mask */
Dean Nelson33ba3c72008-07-29 22:34:07 -0700499 return (FETCHOP_LOAD_OP(TO_AMO((u64)&amo->variable), FETCHOP_LOAD) &
Dean Nelson04de7412008-07-29 22:34:14 -0700500 BIT(partid)) != 0;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700501}
502
Dean Nelsona47d5da2008-07-29 22:34:09 -0700503static int
504xpc_any_partition_engaged_sn2(void)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700505{
Dean Nelson8e85c232008-07-29 22:34:13 -0700506 struct amo *amo = xpc_vars_sn2->amos_page +
507 XPC_ENGAGED_PARTITIONS_AMO_SN2;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700508
Dean Nelsonc39838c2008-07-29 22:34:11 -0700509 /* our partition's amo variable */
Dean Nelsona47d5da2008-07-29 22:34:09 -0700510 return FETCHOP_LOAD_OP(TO_AMO((u64)&amo->variable), FETCHOP_LOAD) != 0;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700511}
512
513static void
Dean Nelsona47d5da2008-07-29 22:34:09 -0700514xpc_assume_partition_disengaged_sn2(short partid)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700515{
Dean Nelson8e85c232008-07-29 22:34:13 -0700516 struct amo *amo = xpc_vars_sn2->amos_page +
517 XPC_ENGAGED_PARTITIONS_AMO_SN2;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700518
Dean Nelsonc39838c2008-07-29 22:34:11 -0700519 /* clear bit(s) based on partid mask in our partition's amo */
Dean Nelson33ba3c72008-07-29 22:34:07 -0700520 FETCHOP_STORE_OP(TO_AMO((u64)&amo->variable), FETCHOP_AND,
Dean Nelson04de7412008-07-29 22:34:14 -0700521 ~BIT(partid));
Dean Nelson33ba3c72008-07-29 22:34:07 -0700522}
523
Dean Nelson6e410172008-07-29 22:34:09 -0700524/* original protection values for each node */
525static u64 xpc_prot_vec_sn2[MAX_NUMNODES];
526
527/*
Dean Nelsonc39838c2008-07-29 22:34:11 -0700528 * Change protections to allow amo operations on non-Shub 1.1 systems.
Dean Nelson6e410172008-07-29 22:34:09 -0700529 */
530static enum xp_retval
Dean Nelsonc39838c2008-07-29 22:34:11 -0700531xpc_allow_amo_ops_sn2(struct amo *amos_page)
Dean Nelson6e410172008-07-29 22:34:09 -0700532{
533 u64 nasid_array = 0;
534 int ret;
535
536 /*
537 * On SHUB 1.1, we cannot call sn_change_memprotect() since the BIST
538 * collides with memory operations. On those systems we call
Dean Nelsonc39838c2008-07-29 22:34:11 -0700539 * xpc_allow_amo_ops_shub_wars_1_1_sn2() instead.
Dean Nelson6e410172008-07-29 22:34:09 -0700540 */
541 if (!enable_shub_wars_1_1()) {
542 ret = sn_change_memprotect(ia64_tpa((u64)amos_page), PAGE_SIZE,
543 SN_MEMPROT_ACCESS_CLASS_1,
544 &nasid_array);
545 if (ret != 0)
546 return xpSalError;
547 }
548 return xpSuccess;
549}
550
551/*
Dean Nelsonc39838c2008-07-29 22:34:11 -0700552 * Change protections to allow amo operations on Shub 1.1 systems.
Dean Nelson6e410172008-07-29 22:34:09 -0700553 */
554static void
Dean Nelsonc39838c2008-07-29 22:34:11 -0700555xpc_allow_amo_ops_shub_wars_1_1_sn2(void)
Dean Nelson6e410172008-07-29 22:34:09 -0700556{
557 int node;
558 int nasid;
559
560 if (!enable_shub_wars_1_1())
561 return;
562
563 for_each_online_node(node) {
564 nasid = cnodeid_to_nasid(node);
565 /* save current protection values */
566 xpc_prot_vec_sn2[node] =
567 (u64)HUB_L((u64 *)GLOBAL_MMR_ADDR(nasid,
568 SH1_MD_DQLP_MMR_DIR_PRIVEC0));
569 /* open up everything */
570 HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid,
571 SH1_MD_DQLP_MMR_DIR_PRIVEC0),
572 -1UL);
573 HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid,
574 SH1_MD_DQRP_MMR_DIR_PRIVEC0),
575 -1UL);
576 }
577}
578
Dean Nelson94bd2702008-07-29 22:34:05 -0700579static enum xp_retval
Dean Nelson261f3b42008-07-29 22:34:16 -0700580xpc_get_partition_rsvd_page_pa_sn2(u64 buf, u64 *cookie, u64 *paddr,
581 size_t *len)
582{
583 s64 status;
584 enum xp_retval ret;
585
586 status = sn_partition_reserved_page_pa(buf, cookie, paddr, len);
587 if (status == SALRET_OK)
588 ret = xpSuccess;
589 else if (status == SALRET_MORE_PASSES)
590 ret = xpNeedMoreInfo;
591 else
592 ret = xpSalError;
593
594 return ret;
595}
596
597
598static enum xp_retval
Dean Nelson94bd2702008-07-29 22:34:05 -0700599xpc_rsvd_page_init_sn2(struct xpc_rsvd_page *rp)
600{
Dean Nelsonc39838c2008-07-29 22:34:11 -0700601 struct amo *amos_page;
Dean Nelson94bd2702008-07-29 22:34:05 -0700602 int i;
603 int ret;
604
Dean Nelson8e85c232008-07-29 22:34:13 -0700605 xpc_vars_sn2 = XPC_RP_VARS(rp);
Dean Nelson94bd2702008-07-29 22:34:05 -0700606
Dean Nelson8e85c232008-07-29 22:34:13 -0700607 rp->sn.vars_pa = __pa(xpc_vars_sn2);
Dean Nelson94bd2702008-07-29 22:34:05 -0700608
Dean Nelsone17d4162008-07-29 22:34:06 -0700609 /* vars_part array follows immediately after vars */
Dean Nelson8e85c232008-07-29 22:34:13 -0700610 xpc_vars_part_sn2 = (struct xpc_vars_part_sn2 *)((u8 *)XPC_RP_VARS(rp) +
611 XPC_RP_VARS_SIZE);
Dean Nelsone17d4162008-07-29 22:34:06 -0700612
Dean Nelson94bd2702008-07-29 22:34:05 -0700613 /*
Dean Nelson8e85c232008-07-29 22:34:13 -0700614 * Before clearing xpc_vars_sn2, see if a page of amos had been
615 * previously allocated. If not we'll need to allocate one and set
616 * permissions so that cross-partition amos are allowed.
Dean Nelson94bd2702008-07-29 22:34:05 -0700617 *
Dean Nelsonc39838c2008-07-29 22:34:11 -0700618 * The allocated amo page needs MCA reporting to remain disabled after
Dean Nelson94bd2702008-07-29 22:34:05 -0700619 * XPC has unloaded. To make this work, we keep a copy of the pointer
Dean Nelson8e85c232008-07-29 22:34:13 -0700620 * to this page (i.e., amos_page) in the struct xpc_vars_sn2 structure,
Dean Nelson94bd2702008-07-29 22:34:05 -0700621 * which is pointed to by the reserved page, and re-use that saved copy
Dean Nelsonc39838c2008-07-29 22:34:11 -0700622 * on subsequent loads of XPC. This amo page is never freed, and its
Dean Nelson94bd2702008-07-29 22:34:05 -0700623 * memory protections are never restricted.
624 */
Dean Nelson8e85c232008-07-29 22:34:13 -0700625 amos_page = xpc_vars_sn2->amos_page;
Dean Nelson94bd2702008-07-29 22:34:05 -0700626 if (amos_page == NULL) {
Dean Nelsonc39838c2008-07-29 22:34:11 -0700627 amos_page = (struct amo *)TO_AMO(uncached_alloc_page(0, 1));
Dean Nelson94bd2702008-07-29 22:34:05 -0700628 if (amos_page == NULL) {
Dean Nelsonc39838c2008-07-29 22:34:11 -0700629 dev_err(xpc_part, "can't allocate page of amos\n");
Dean Nelson94bd2702008-07-29 22:34:05 -0700630 return xpNoMemory;
631 }
632
633 /*
Dean Nelsonc39838c2008-07-29 22:34:11 -0700634 * Open up amo-R/W to cpu. This is done on Shub 1.1 systems
635 * when xpc_allow_amo_ops_shub_wars_1_1_sn2() is called.
Dean Nelson94bd2702008-07-29 22:34:05 -0700636 */
Dean Nelsonc39838c2008-07-29 22:34:11 -0700637 ret = xpc_allow_amo_ops_sn2(amos_page);
Dean Nelson6e410172008-07-29 22:34:09 -0700638 if (ret != xpSuccess) {
Dean Nelsonc39838c2008-07-29 22:34:11 -0700639 dev_err(xpc_part, "can't allow amo operations\n");
Dean Nelson6e410172008-07-29 22:34:09 -0700640 uncached_free_page(__IA64_UNCACHED_OFFSET |
641 TO_PHYS((u64)amos_page), 1);
642 return ret;
Dean Nelson94bd2702008-07-29 22:34:05 -0700643 }
644 }
645
Dean Nelson8e85c232008-07-29 22:34:13 -0700646 /* clear xpc_vars_sn2 */
647 memset(xpc_vars_sn2, 0, sizeof(struct xpc_vars_sn2));
Dean Nelson94bd2702008-07-29 22:34:05 -0700648
Dean Nelson8e85c232008-07-29 22:34:13 -0700649 xpc_vars_sn2->version = XPC_V_VERSION;
650 xpc_vars_sn2->activate_IRQ_nasid = cpuid_to_nasid(0);
651 xpc_vars_sn2->activate_IRQ_phys_cpuid = cpu_physical_id(0);
652 xpc_vars_sn2->vars_part_pa = __pa(xpc_vars_part_sn2);
653 xpc_vars_sn2->amos_page_pa = ia64_tpa((u64)amos_page);
654 xpc_vars_sn2->amos_page = amos_page; /* save for next load of XPC */
Dean Nelson94bd2702008-07-29 22:34:05 -0700655
Dean Nelson8e85c232008-07-29 22:34:13 -0700656 /* clear xpc_vars_part_sn2 */
657 memset((u64 *)xpc_vars_part_sn2, 0, sizeof(struct xpc_vars_part_sn2) *
Dean Nelson261f3b42008-07-29 22:34:16 -0700658 XP_MAX_NPARTITIONS_SN2);
Dean Nelson94bd2702008-07-29 22:34:05 -0700659
Dean Nelsonc39838c2008-07-29 22:34:11 -0700660 /* initialize the activate IRQ related amo variables */
Dean Nelson04de7412008-07-29 22:34:14 -0700661 for (i = 0; i < xpc_nasid_mask_nlongs; i++)
Dean Nelsonee6665e2008-07-29 22:34:13 -0700662 (void)xpc_init_IRQ_amo_sn2(XPC_ACTIVATE_IRQ_AMOS_SN2 + i);
Dean Nelson94bd2702008-07-29 22:34:05 -0700663
Dean Nelsonc39838c2008-07-29 22:34:11 -0700664 /* initialize the engaged remote partitions related amo variables */
Dean Nelsonee6665e2008-07-29 22:34:13 -0700665 (void)xpc_init_IRQ_amo_sn2(XPC_ENGAGED_PARTITIONS_AMO_SN2);
666 (void)xpc_init_IRQ_amo_sn2(XPC_DEACTIVATE_REQUEST_AMO_SN2);
Dean Nelson94bd2702008-07-29 22:34:05 -0700667
668 return xpSuccess;
669}
670
Dean Nelson33ba3c72008-07-29 22:34:07 -0700671static void
672xpc_increment_heartbeat_sn2(void)
673{
Dean Nelson8e85c232008-07-29 22:34:13 -0700674 xpc_vars_sn2->heartbeat++;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700675}
676
677static void
678xpc_offline_heartbeat_sn2(void)
679{
680 xpc_increment_heartbeat_sn2();
Dean Nelson8e85c232008-07-29 22:34:13 -0700681 xpc_vars_sn2->heartbeat_offline = 1;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700682}
683
684static void
685xpc_online_heartbeat_sn2(void)
686{
687 xpc_increment_heartbeat_sn2();
Dean Nelson8e85c232008-07-29 22:34:13 -0700688 xpc_vars_sn2->heartbeat_offline = 0;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700689}
690
691static void
692xpc_heartbeat_init_sn2(void)
693{
Dean Nelson8e85c232008-07-29 22:34:13 -0700694 DBUG_ON(xpc_vars_sn2 == NULL);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700695
Dean Nelson8e85c232008-07-29 22:34:13 -0700696 bitmap_zero(xpc_vars_sn2->heartbeating_to_mask, XP_MAX_NPARTITIONS_SN2);
697 xpc_heartbeating_to_mask = &xpc_vars_sn2->heartbeating_to_mask[0];
Dean Nelson33ba3c72008-07-29 22:34:07 -0700698 xpc_online_heartbeat_sn2();
699}
700
701static void
702xpc_heartbeat_exit_sn2(void)
703{
704 xpc_offline_heartbeat_sn2();
705}
706
707/*
708 * At periodic intervals, scan through all active partitions and ensure
709 * their heartbeat is still active. If not, the partition is deactivated.
710 */
711static void
712xpc_check_remote_hb_sn2(void)
713{
714 struct xpc_vars_sn2 *remote_vars;
715 struct xpc_partition *part;
716 short partid;
717 enum xp_retval ret;
718
Dean Nelsonee6665e2008-07-29 22:34:13 -0700719 remote_vars = (struct xpc_vars_sn2 *)xpc_remote_copy_buffer_sn2;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700720
Dean Nelson261f3b42008-07-29 22:34:16 -0700721 for (partid = 0; partid < XP_MAX_NPARTITIONS_SN2; partid++) {
Dean Nelson33ba3c72008-07-29 22:34:07 -0700722
723 if (xpc_exiting)
724 break;
725
726 if (partid == sn_partition_id)
727 continue;
728
729 part = &xpc_partitions[partid];
730
731 if (part->act_state == XPC_P_INACTIVE ||
732 part->act_state == XPC_P_DEACTIVATING) {
733 continue;
734 }
735
736 /* pull the remote_hb cache line */
737 ret = xp_remote_memcpy(remote_vars,
Dean Nelsona47d5da2008-07-29 22:34:09 -0700738 (void *)part->sn.sn2.remote_vars_pa,
Dean Nelson33ba3c72008-07-29 22:34:07 -0700739 XPC_RP_VARS_SIZE);
740 if (ret != xpSuccess) {
741 XPC_DEACTIVATE_PARTITION(part, ret);
742 continue;
743 }
744
745 dev_dbg(xpc_part, "partid = %d, heartbeat = %ld, last_heartbeat"
746 " = %ld, heartbeat_offline = %ld, HB_mask[0] = 0x%lx\n",
747 partid, remote_vars->heartbeat, part->last_heartbeat,
748 remote_vars->heartbeat_offline,
749 remote_vars->heartbeating_to_mask[0]);
750
751 if (((remote_vars->heartbeat == part->last_heartbeat) &&
752 (remote_vars->heartbeat_offline == 0)) ||
753 !xpc_hb_allowed(sn_partition_id,
754 &remote_vars->heartbeating_to_mask)) {
755
756 XPC_DEACTIVATE_PARTITION(part, xpNoHeartbeat);
757 continue;
758 }
759
760 part->last_heartbeat = remote_vars->heartbeat;
761 }
762}
763
764/*
765 * Get a copy of the remote partition's XPC variables from the reserved page.
766 *
767 * remote_vars points to a buffer that is cacheline aligned for BTE copies and
768 * assumed to be of size XPC_RP_VARS_SIZE.
769 */
770static enum xp_retval
771xpc_get_remote_vars_sn2(u64 remote_vars_pa, struct xpc_vars_sn2 *remote_vars)
772{
773 enum xp_retval ret;
774
775 if (remote_vars_pa == 0)
776 return xpVarsNotSet;
777
778 /* pull over the cross partition variables */
779 ret = xp_remote_memcpy(remote_vars, (void *)remote_vars_pa,
780 XPC_RP_VARS_SIZE);
781 if (ret != xpSuccess)
782 return ret;
783
784 if (XPC_VERSION_MAJOR(remote_vars->version) !=
785 XPC_VERSION_MAJOR(XPC_V_VERSION)) {
786 return xpBadVersion;
787 }
788
789 return xpSuccess;
790}
791
792static void
Dean Nelsona47d5da2008-07-29 22:34:09 -0700793xpc_request_partition_activation_sn2(struct xpc_rsvd_page *remote_rp,
794 u64 remote_rp_pa, int nasid)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700795{
Dean Nelson7fb5e592008-07-29 22:34:10 -0700796 xpc_send_local_activate_IRQ_sn2(nasid);
Dean Nelsona47d5da2008-07-29 22:34:09 -0700797}
798
799static void
800xpc_request_partition_reactivation_sn2(struct xpc_partition *part)
801{
Dean Nelson7fb5e592008-07-29 22:34:10 -0700802 xpc_send_local_activate_IRQ_sn2(part->sn.sn2.activate_IRQ_nasid);
Dean Nelsona47d5da2008-07-29 22:34:09 -0700803}
804
805static void
806xpc_request_partition_deactivation_sn2(struct xpc_partition *part)
807{
808 struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
809 unsigned long irq_flags;
Dean Nelsonc39838c2008-07-29 22:34:11 -0700810 struct amo *amo = (struct amo *)__va(part_sn2->remote_amos_page_pa +
Dean Nelsonee6665e2008-07-29 22:34:13 -0700811 (XPC_DEACTIVATE_REQUEST_AMO_SN2 *
Dean Nelsonc39838c2008-07-29 22:34:11 -0700812 sizeof(struct amo)));
Dean Nelsona47d5da2008-07-29 22:34:09 -0700813
814 local_irq_save(irq_flags);
815
Dean Nelsonc39838c2008-07-29 22:34:11 -0700816 /* set bit corresponding to our partid in remote partition's amo */
Dean Nelsona47d5da2008-07-29 22:34:09 -0700817 FETCHOP_STORE_OP(TO_AMO((u64)&amo->variable), FETCHOP_OR,
Dean Nelson04de7412008-07-29 22:34:14 -0700818 BIT(sn_partition_id));
819
Dean Nelsona47d5da2008-07-29 22:34:09 -0700820 /*
821 * We must always use the nofault function regardless of whether we
822 * are on a Shub 1.1 system or a Shub 1.2 slice 0xc processor. If we
823 * didn't, we'd never know that the other partition is down and would
Dean Nelsonc39838c2008-07-29 22:34:11 -0700824 * keep sending IRQs and amos to it until the heartbeat times out.
Dean Nelsona47d5da2008-07-29 22:34:09 -0700825 */
826 (void)xp_nofault_PIOR((u64 *)GLOBAL_MMR_ADDR(NASID_GET(&amo->
827 variable),
828 xp_nofault_PIOR_target));
829
830 local_irq_restore(irq_flags);
831
832 /*
833 * Send activate IRQ to get other side to see that we've set our
Dean Nelsonc39838c2008-07-29 22:34:11 -0700834 * bit in their deactivate request amo.
Dean Nelsona47d5da2008-07-29 22:34:09 -0700835 */
Dean Nelson7fb5e592008-07-29 22:34:10 -0700836 xpc_send_activate_IRQ_sn2(part_sn2->remote_amos_page_pa,
Dean Nelsona47d5da2008-07-29 22:34:09 -0700837 cnodeid_to_nasid(0),
838 part_sn2->activate_IRQ_nasid,
839 part_sn2->activate_IRQ_phys_cpuid);
840}
841
842static void
843xpc_cancel_partition_deactivation_request_sn2(struct xpc_partition *part)
844{
845 unsigned long irq_flags;
Dean Nelsonc39838c2008-07-29 22:34:11 -0700846 struct amo *amo = (struct amo *)__va(part->sn.sn2.remote_amos_page_pa +
Dean Nelsonee6665e2008-07-29 22:34:13 -0700847 (XPC_DEACTIVATE_REQUEST_AMO_SN2 *
Dean Nelsonc39838c2008-07-29 22:34:11 -0700848 sizeof(struct amo)));
Dean Nelsona47d5da2008-07-29 22:34:09 -0700849
850 local_irq_save(irq_flags);
851
Dean Nelsonc39838c2008-07-29 22:34:11 -0700852 /* clear bit corresponding to our partid in remote partition's amo */
Dean Nelsona47d5da2008-07-29 22:34:09 -0700853 FETCHOP_STORE_OP(TO_AMO((u64)&amo->variable), FETCHOP_AND,
Dean Nelson04de7412008-07-29 22:34:14 -0700854 ~BIT(sn_partition_id));
855
Dean Nelsona47d5da2008-07-29 22:34:09 -0700856 /*
857 * We must always use the nofault function regardless of whether we
858 * are on a Shub 1.1 system or a Shub 1.2 slice 0xc processor. If we
859 * didn't, we'd never know that the other partition is down and would
Dean Nelsonc39838c2008-07-29 22:34:11 -0700860 * keep sending IRQs and amos to it until the heartbeat times out.
Dean Nelsona47d5da2008-07-29 22:34:09 -0700861 */
862 (void)xp_nofault_PIOR((u64 *)GLOBAL_MMR_ADDR(NASID_GET(&amo->
863 variable),
864 xp_nofault_PIOR_target));
865
866 local_irq_restore(irq_flags);
867}
868
869static int
870xpc_partition_deactivation_requested_sn2(short partid)
871{
Dean Nelson8e85c232008-07-29 22:34:13 -0700872 struct amo *amo = xpc_vars_sn2->amos_page +
873 XPC_DEACTIVATE_REQUEST_AMO_SN2;
Dean Nelsona47d5da2008-07-29 22:34:09 -0700874
Dean Nelsonc39838c2008-07-29 22:34:11 -0700875 /* our partition's amo variable ANDed with partid mask */
Dean Nelsona47d5da2008-07-29 22:34:09 -0700876 return (FETCHOP_LOAD_OP(TO_AMO((u64)&amo->variable), FETCHOP_LOAD) &
Dean Nelson04de7412008-07-29 22:34:14 -0700877 BIT(partid)) != 0;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700878}
879
880/*
881 * Update the remote partition's info.
882 */
883static void
884xpc_update_partition_info_sn2(struct xpc_partition *part, u8 remote_rp_version,
Dean Nelson81fe7882008-07-29 22:34:15 -0700885 unsigned long *remote_rp_ts_jiffies,
886 u64 remote_rp_pa, u64 remote_vars_pa,
Dean Nelson33ba3c72008-07-29 22:34:07 -0700887 struct xpc_vars_sn2 *remote_vars)
888{
Dean Nelsona47d5da2008-07-29 22:34:09 -0700889 struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
890
Dean Nelson33ba3c72008-07-29 22:34:07 -0700891 part->remote_rp_version = remote_rp_version;
892 dev_dbg(xpc_part, " remote_rp_version = 0x%016x\n",
893 part->remote_rp_version);
894
Dean Nelson81fe7882008-07-29 22:34:15 -0700895 part->remote_rp_ts_jiffies = *remote_rp_ts_jiffies;
896 dev_dbg(xpc_part, " remote_rp_ts_jiffies = 0x%016lx\n",
897 part->remote_rp_ts_jiffies);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700898
899 part->remote_rp_pa = remote_rp_pa;
900 dev_dbg(xpc_part, " remote_rp_pa = 0x%016lx\n", part->remote_rp_pa);
901
Dean Nelsona47d5da2008-07-29 22:34:09 -0700902 part_sn2->remote_vars_pa = remote_vars_pa;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700903 dev_dbg(xpc_part, " remote_vars_pa = 0x%016lx\n",
Dean Nelsona47d5da2008-07-29 22:34:09 -0700904 part_sn2->remote_vars_pa);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700905
906 part->last_heartbeat = remote_vars->heartbeat;
907 dev_dbg(xpc_part, " last_heartbeat = 0x%016lx\n",
908 part->last_heartbeat);
909
Dean Nelsona47d5da2008-07-29 22:34:09 -0700910 part_sn2->remote_vars_part_pa = remote_vars->vars_part_pa;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700911 dev_dbg(xpc_part, " remote_vars_part_pa = 0x%016lx\n",
Dean Nelsona47d5da2008-07-29 22:34:09 -0700912 part_sn2->remote_vars_part_pa);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700913
Dean Nelsona47d5da2008-07-29 22:34:09 -0700914 part_sn2->activate_IRQ_nasid = remote_vars->activate_IRQ_nasid;
915 dev_dbg(xpc_part, " activate_IRQ_nasid = 0x%x\n",
916 part_sn2->activate_IRQ_nasid);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700917
Dean Nelsona47d5da2008-07-29 22:34:09 -0700918 part_sn2->activate_IRQ_phys_cpuid =
919 remote_vars->activate_IRQ_phys_cpuid;
920 dev_dbg(xpc_part, " activate_IRQ_phys_cpuid = 0x%x\n",
921 part_sn2->activate_IRQ_phys_cpuid);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700922
Dean Nelsona47d5da2008-07-29 22:34:09 -0700923 part_sn2->remote_amos_page_pa = remote_vars->amos_page_pa;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700924 dev_dbg(xpc_part, " remote_amos_page_pa = 0x%lx\n",
Dean Nelsona47d5da2008-07-29 22:34:09 -0700925 part_sn2->remote_amos_page_pa);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700926
Dean Nelsona47d5da2008-07-29 22:34:09 -0700927 part_sn2->remote_vars_version = remote_vars->version;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700928 dev_dbg(xpc_part, " remote_vars_version = 0x%x\n",
Dean Nelsona47d5da2008-07-29 22:34:09 -0700929 part_sn2->remote_vars_version);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700930}
931
932/*
Dean Nelson7fb5e592008-07-29 22:34:10 -0700933 * Prior code has determined the nasid which generated a activate IRQ.
934 * Inspect that nasid to determine if its partition needs to be activated
935 * or deactivated.
Dean Nelson33ba3c72008-07-29 22:34:07 -0700936 *
Dean Nelson7fb5e592008-07-29 22:34:10 -0700937 * A partition is considered "awaiting activation" if our partition
Dean Nelson33ba3c72008-07-29 22:34:07 -0700938 * flags indicate it is not active and it has a heartbeat. A
939 * partition is considered "awaiting deactivation" if our partition
940 * flags indicate it is active but it has no heartbeat or it is not
941 * sending its heartbeat to us.
942 *
943 * To determine the heartbeat, the remote nasid must have a properly
944 * initialized reserved page.
945 */
946static void
Dean Nelson6e410172008-07-29 22:34:09 -0700947xpc_identify_activate_IRQ_req_sn2(int nasid)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700948{
949 struct xpc_rsvd_page *remote_rp;
950 struct xpc_vars_sn2 *remote_vars;
951 u64 remote_rp_pa;
952 u64 remote_vars_pa;
953 int remote_rp_version;
954 int reactivate = 0;
Dean Nelson81fe7882008-07-29 22:34:15 -0700955 unsigned long remote_rp_ts_jiffies = 0;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700956 short partid;
957 struct xpc_partition *part;
Dean Nelsona47d5da2008-07-29 22:34:09 -0700958 struct xpc_partition_sn2 *part_sn2;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700959 enum xp_retval ret;
960
961 /* pull over the reserved page structure */
962
Dean Nelsonee6665e2008-07-29 22:34:13 -0700963 remote_rp = (struct xpc_rsvd_page *)xpc_remote_copy_buffer_sn2;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700964
965 ret = xpc_get_remote_rp(nasid, NULL, remote_rp, &remote_rp_pa);
966 if (ret != xpSuccess) {
967 dev_warn(xpc_part, "unable to get reserved page from nasid %d, "
968 "which sent interrupt, reason=%d\n", nasid, ret);
969 return;
970 }
971
972 remote_vars_pa = remote_rp->sn.vars_pa;
973 remote_rp_version = remote_rp->version;
Dean Nelson81fe7882008-07-29 22:34:15 -0700974 remote_rp_ts_jiffies = remote_rp->ts_jiffies;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700975
976 partid = remote_rp->SAL_partid;
977 part = &xpc_partitions[partid];
Dean Nelsona47d5da2008-07-29 22:34:09 -0700978 part_sn2 = &part->sn.sn2;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700979
980 /* pull over the cross partition variables */
981
Dean Nelsonee6665e2008-07-29 22:34:13 -0700982 remote_vars = (struct xpc_vars_sn2 *)xpc_remote_copy_buffer_sn2;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700983
984 ret = xpc_get_remote_vars_sn2(remote_vars_pa, remote_vars);
985 if (ret != xpSuccess) {
Dean Nelson33ba3c72008-07-29 22:34:07 -0700986 dev_warn(xpc_part, "unable to get XPC variables from nasid %d, "
987 "which sent interrupt, reason=%d\n", nasid, ret);
988
989 XPC_DEACTIVATE_PARTITION(part, ret);
990 return;
991 }
992
Dean Nelson6e410172008-07-29 22:34:09 -0700993 part->activate_IRQ_rcvd++;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700994
995 dev_dbg(xpc_part, "partid for nasid %d is %d; IRQs = %d; HB = "
Dean Nelson6e410172008-07-29 22:34:09 -0700996 "%ld:0x%lx\n", (int)nasid, (int)partid, part->activate_IRQ_rcvd,
Dean Nelson33ba3c72008-07-29 22:34:07 -0700997 remote_vars->heartbeat, remote_vars->heartbeating_to_mask[0]);
998
999 if (xpc_partition_disengaged(part) &&
1000 part->act_state == XPC_P_INACTIVE) {
1001
1002 xpc_update_partition_info_sn2(part, remote_rp_version,
Dean Nelson81fe7882008-07-29 22:34:15 -07001003 &remote_rp_ts_jiffies,
1004 remote_rp_pa, remote_vars_pa,
1005 remote_vars);
Dean Nelson33ba3c72008-07-29 22:34:07 -07001006
Dean Nelsona47d5da2008-07-29 22:34:09 -07001007 if (xpc_partition_deactivation_requested_sn2(partid)) {
1008 /*
1009 * Other side is waiting on us to deactivate even though
1010 * we already have.
1011 */
1012 return;
Dean Nelson33ba3c72008-07-29 22:34:07 -07001013 }
1014
1015 xpc_activate_partition(part);
1016 return;
1017 }
1018
1019 DBUG_ON(part->remote_rp_version == 0);
Dean Nelsona47d5da2008-07-29 22:34:09 -07001020 DBUG_ON(part_sn2->remote_vars_version == 0);
Dean Nelson33ba3c72008-07-29 22:34:07 -07001021
Dean Nelson81fe7882008-07-29 22:34:15 -07001022 if (remote_rp_ts_jiffies != part->remote_rp_ts_jiffies) {
Dean Nelson33ba3c72008-07-29 22:34:07 -07001023
Dean Nelsona47d5da2008-07-29 22:34:09 -07001024 /* the other side rebooted */
Dean Nelson33ba3c72008-07-29 22:34:07 -07001025
Dean Nelsona47d5da2008-07-29 22:34:09 -07001026 DBUG_ON(xpc_partition_engaged_sn2(partid));
1027 DBUG_ON(xpc_partition_deactivation_requested_sn2(partid));
Dean Nelson33ba3c72008-07-29 22:34:07 -07001028
1029 xpc_update_partition_info_sn2(part, remote_rp_version,
Dean Nelson81fe7882008-07-29 22:34:15 -07001030 &remote_rp_ts_jiffies,
1031 remote_rp_pa, remote_vars_pa,
1032 remote_vars);
Dean Nelson33ba3c72008-07-29 22:34:07 -07001033 reactivate = 1;
Dean Nelson33ba3c72008-07-29 22:34:07 -07001034 }
1035
Dean Nelsona47d5da2008-07-29 22:34:09 -07001036 if (part->disengage_timeout > 0 && !xpc_partition_disengaged(part)) {
Dean Nelson33ba3c72008-07-29 22:34:07 -07001037 /* still waiting on other side to disengage from us */
1038 return;
1039 }
1040
Dean Nelsona47d5da2008-07-29 22:34:09 -07001041 if (reactivate)
Dean Nelson33ba3c72008-07-29 22:34:07 -07001042 XPC_DEACTIVATE_PARTITION(part, xpReactivating);
Dean Nelsona47d5da2008-07-29 22:34:09 -07001043 else if (xpc_partition_deactivation_requested_sn2(partid))
Dean Nelson33ba3c72008-07-29 22:34:07 -07001044 XPC_DEACTIVATE_PARTITION(part, xpOtherGoingDown);
Dean Nelson33ba3c72008-07-29 22:34:07 -07001045}
1046
1047/*
Dean Nelsonc39838c2008-07-29 22:34:11 -07001048 * Loop through the activation amo variables and process any bits
Dean Nelson33ba3c72008-07-29 22:34:07 -07001049 * which are set. Each bit indicates a nasid sending a partition
1050 * activation or deactivation request.
1051 *
1052 * Return #of IRQs detected.
1053 */
1054int
Dean Nelson6e410172008-07-29 22:34:09 -07001055xpc_identify_activate_IRQ_sender_sn2(void)
Dean Nelson33ba3c72008-07-29 22:34:07 -07001056{
Dean Nelson04de7412008-07-29 22:34:14 -07001057 int l;
1058 int b;
1059 unsigned long nasid_mask_long;
Dean Nelson33ba3c72008-07-29 22:34:07 -07001060 u64 nasid; /* remote nasid */
1061 int n_IRQs_detected = 0;
Dean Nelsonc39838c2008-07-29 22:34:11 -07001062 struct amo *act_amos;
Dean Nelson33ba3c72008-07-29 22:34:07 -07001063
Dean Nelson8e85c232008-07-29 22:34:13 -07001064 act_amos = xpc_vars_sn2->amos_page + XPC_ACTIVATE_IRQ_AMOS_SN2;
Dean Nelson33ba3c72008-07-29 22:34:07 -07001065
Dean Nelson04de7412008-07-29 22:34:14 -07001066 /* scan through activate amo variables looking for non-zero entries */
1067 for (l = 0; l < xpc_nasid_mask_nlongs; l++) {
Dean Nelson33ba3c72008-07-29 22:34:07 -07001068
1069 if (xpc_exiting)
1070 break;
1071
Dean Nelson04de7412008-07-29 22:34:14 -07001072 nasid_mask_long = xpc_receive_IRQ_amo_sn2(&act_amos[l]);
1073
1074 b = find_first_bit(&nasid_mask_long, BITS_PER_LONG);
1075 if (b >= BITS_PER_LONG) {
1076 /* no IRQs from nasids in this amo variable */
Dean Nelson33ba3c72008-07-29 22:34:07 -07001077 continue;
1078 }
1079
Dean Nelson04de7412008-07-29 22:34:14 -07001080 dev_dbg(xpc_part, "amo[%d] gave back 0x%lx\n", l,
1081 nasid_mask_long);
Dean Nelson33ba3c72008-07-29 22:34:07 -07001082
1083 /*
1084 * If this nasid has been added to the machine since
1085 * our partition was reset, this will retain the
1086 * remote nasid in our reserved pages machine mask.
1087 * This is used in the event of module reload.
1088 */
Dean Nelson04de7412008-07-29 22:34:14 -07001089 xpc_mach_nasids[l] |= nasid_mask_long;
Dean Nelson33ba3c72008-07-29 22:34:07 -07001090
1091 /* locate the nasid(s) which sent interrupts */
1092
Dean Nelson04de7412008-07-29 22:34:14 -07001093 do {
1094 n_IRQs_detected++;
1095 nasid = (l * BITS_PER_LONG + b) * 2;
1096 dev_dbg(xpc_part, "interrupt from nasid %ld\n", nasid);
1097 xpc_identify_activate_IRQ_req_sn2(nasid);
1098
1099 b = find_next_bit(&nasid_mask_long, BITS_PER_LONG,
1100 b + 1);
1101 } while (b < BITS_PER_LONG);
Dean Nelson33ba3c72008-07-29 22:34:07 -07001102 }
1103 return n_IRQs_detected;
1104}
1105
1106static void
Dean Nelson6e410172008-07-29 22:34:09 -07001107xpc_process_activate_IRQ_rcvd_sn2(int n_IRQs_expected)
Dean Nelson33ba3c72008-07-29 22:34:07 -07001108{
1109 int n_IRQs_detected;
1110
Dean Nelson6e410172008-07-29 22:34:09 -07001111 n_IRQs_detected = xpc_identify_activate_IRQ_sender_sn2();
Dean Nelson33ba3c72008-07-29 22:34:07 -07001112 if (n_IRQs_detected < n_IRQs_expected) {
Dean Nelsonc39838c2008-07-29 22:34:11 -07001113 /* retry once to help avoid missing amo */
Dean Nelson6e410172008-07-29 22:34:09 -07001114 (void)xpc_identify_activate_IRQ_sender_sn2();
Dean Nelson33ba3c72008-07-29 22:34:07 -07001115 }
1116}
1117
Dean Nelsone17d4162008-07-29 22:34:06 -07001118/*
Dean Nelson185c3a12008-07-29 22:34:11 -07001119 * Guarantee that the kzalloc'd memory is cacheline aligned.
1120 */
1121static void *
1122xpc_kzalloc_cacheline_aligned_sn2(size_t size, gfp_t flags, void **base)
1123{
1124 /* see if kzalloc will give us cachline aligned memory by default */
1125 *base = kzalloc(size, flags);
1126 if (*base == NULL)
1127 return NULL;
1128
1129 if ((u64)*base == L1_CACHE_ALIGN((u64)*base))
1130 return *base;
1131
1132 kfree(*base);
1133
1134 /* nope, we'll have to do it ourselves */
1135 *base = kzalloc(size + L1_CACHE_BYTES, flags);
1136 if (*base == NULL)
1137 return NULL;
1138
1139 return (void *)L1_CACHE_ALIGN((u64)*base);
1140}
1141
1142/*
Dean Nelsone17d4162008-07-29 22:34:06 -07001143 * Setup the infrastructure necessary to support XPartition Communication
1144 * between the specified remote partition and the local one.
1145 */
1146static enum xp_retval
1147xpc_setup_infrastructure_sn2(struct xpc_partition *part)
1148{
Dean Nelsona47d5da2008-07-29 22:34:09 -07001149 struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
Dean Nelsone17d4162008-07-29 22:34:06 -07001150 enum xp_retval retval;
1151 int ret;
1152 int cpuid;
1153 int ch_number;
1154 struct xpc_channel *ch;
1155 struct timer_list *timer;
1156 short partid = XPC_PARTID(part);
1157
1158 /*
1159 * Allocate all of the channel structures as a contiguous chunk of
1160 * memory.
1161 */
1162 DBUG_ON(part->channels != NULL);
1163 part->channels = kzalloc(sizeof(struct xpc_channel) * XPC_MAX_NCHANNELS,
1164 GFP_KERNEL);
1165 if (part->channels == NULL) {
1166 dev_err(xpc_chan, "can't get memory for channels\n");
1167 return xpNoMemory;
1168 }
1169
1170 /* allocate all the required GET/PUT values */
1171
Dean Nelson185c3a12008-07-29 22:34:11 -07001172 part_sn2->local_GPs =
1173 xpc_kzalloc_cacheline_aligned_sn2(XPC_GP_SIZE, GFP_KERNEL,
1174 &part_sn2->local_GPs_base);
Dean Nelsona47d5da2008-07-29 22:34:09 -07001175 if (part_sn2->local_GPs == NULL) {
Dean Nelsone17d4162008-07-29 22:34:06 -07001176 dev_err(xpc_chan, "can't get memory for local get/put "
1177 "values\n");
1178 retval = xpNoMemory;
1179 goto out_1;
1180 }
1181
Dean Nelson185c3a12008-07-29 22:34:11 -07001182 part_sn2->remote_GPs =
1183 xpc_kzalloc_cacheline_aligned_sn2(XPC_GP_SIZE, GFP_KERNEL,
1184 &part_sn2->remote_GPs_base);
Dean Nelsona47d5da2008-07-29 22:34:09 -07001185 if (part_sn2->remote_GPs == NULL) {
Dean Nelsone17d4162008-07-29 22:34:06 -07001186 dev_err(xpc_chan, "can't get memory for remote get/put "
1187 "values\n");
1188 retval = xpNoMemory;
1189 goto out_2;
1190 }
1191
Dean Nelsona47d5da2008-07-29 22:34:09 -07001192 part_sn2->remote_GPs_pa = 0;
Dean Nelsone17d4162008-07-29 22:34:06 -07001193
1194 /* allocate all the required open and close args */
1195
1196 part->local_openclose_args =
Dean Nelson185c3a12008-07-29 22:34:11 -07001197 xpc_kzalloc_cacheline_aligned_sn2(XPC_OPENCLOSE_ARGS_SIZE,
1198 GFP_KERNEL,
1199 &part->local_openclose_args_base);
Dean Nelsone17d4162008-07-29 22:34:06 -07001200 if (part->local_openclose_args == NULL) {
1201 dev_err(xpc_chan, "can't get memory for local connect args\n");
1202 retval = xpNoMemory;
1203 goto out_3;
1204 }
1205
1206 part->remote_openclose_args =
Dean Nelson185c3a12008-07-29 22:34:11 -07001207 xpc_kzalloc_cacheline_aligned_sn2(XPC_OPENCLOSE_ARGS_SIZE,
1208 GFP_KERNEL,
1209 &part->remote_openclose_args_base);
Dean Nelsone17d4162008-07-29 22:34:06 -07001210 if (part->remote_openclose_args == NULL) {
1211 dev_err(xpc_chan, "can't get memory for remote connect args\n");
1212 retval = xpNoMemory;
1213 goto out_4;
1214 }
1215
Dean Nelsona47d5da2008-07-29 22:34:09 -07001216 part_sn2->remote_openclose_args_pa = 0;
Dean Nelsone17d4162008-07-29 22:34:06 -07001217
Dean Nelson7fb5e592008-07-29 22:34:10 -07001218 part_sn2->local_chctl_amo_va = xpc_init_IRQ_amo_sn2(partid);
1219 part->chctl.all_flags = 0;
1220 spin_lock_init(&part->chctl_lock);
Dean Nelsone17d4162008-07-29 22:34:06 -07001221
Dean Nelson7fb5e592008-07-29 22:34:10 -07001222 part_sn2->notify_IRQ_nasid = 0;
1223 part_sn2->notify_IRQ_phys_cpuid = 0;
1224 part_sn2->remote_chctl_amo_va = NULL;
Dean Nelsone17d4162008-07-29 22:34:06 -07001225
1226 atomic_set(&part->channel_mgr_requests, 1);
1227 init_waitqueue_head(&part->channel_mgr_wq);
1228
Dean Nelson7fb5e592008-07-29 22:34:10 -07001229 sprintf(part_sn2->notify_IRQ_owner, "xpc%02d", partid);
Dean Nelsona47d5da2008-07-29 22:34:09 -07001230 ret = request_irq(SGI_XPC_NOTIFY, xpc_handle_notify_IRQ_sn2,
Dean Nelson7fb5e592008-07-29 22:34:10 -07001231 IRQF_SHARED, part_sn2->notify_IRQ_owner,
Dean Nelsona47d5da2008-07-29 22:34:09 -07001232 (void *)(u64)partid);
Dean Nelsone17d4162008-07-29 22:34:06 -07001233 if (ret != 0) {
1234 dev_err(xpc_chan, "can't register NOTIFY IRQ handler, "
1235 "errno=%d\n", -ret);
1236 retval = xpLackOfResources;
1237 goto out_5;
1238 }
1239
Dean Nelson7fb5e592008-07-29 22:34:10 -07001240 /* Setup a timer to check for dropped notify IRQs */
Dean Nelsona47d5da2008-07-29 22:34:09 -07001241 timer = &part_sn2->dropped_notify_IRQ_timer;
Dean Nelsone17d4162008-07-29 22:34:06 -07001242 init_timer(timer);
Dean Nelsona47d5da2008-07-29 22:34:09 -07001243 timer->function =
Dean Nelson7fb5e592008-07-29 22:34:10 -07001244 (void (*)(unsigned long))xpc_check_for_dropped_notify_IRQ_sn2;
Dean Nelsone17d4162008-07-29 22:34:06 -07001245 timer->data = (unsigned long)part;
Dean Nelson7fb5e592008-07-29 22:34:10 -07001246 timer->expires = jiffies + XPC_DROPPED_NOTIFY_IRQ_WAIT_INTERVAL;
Dean Nelsone17d4162008-07-29 22:34:06 -07001247 add_timer(timer);
1248
1249 part->nchannels = XPC_MAX_NCHANNELS;
1250
1251 atomic_set(&part->nchannels_active, 0);
1252 atomic_set(&part->nchannels_engaged, 0);
1253
1254 for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
1255 ch = &part->channels[ch_number];
1256
1257 ch->partid = partid;
1258 ch->number = ch_number;
1259 ch->flags = XPC_C_DISCONNECTED;
1260
Dean Nelsona47d5da2008-07-29 22:34:09 -07001261 ch->sn.sn2.local_GP = &part_sn2->local_GPs[ch_number];
Dean Nelsone17d4162008-07-29 22:34:06 -07001262 ch->local_openclose_args =
1263 &part->local_openclose_args[ch_number];
1264
1265 atomic_set(&ch->kthreads_assigned, 0);
1266 atomic_set(&ch->kthreads_idle, 0);
1267 atomic_set(&ch->kthreads_active, 0);
1268
1269 atomic_set(&ch->references, 0);
1270 atomic_set(&ch->n_to_notify, 0);
1271
1272 spin_lock_init(&ch->lock);
Dean Nelsona47d5da2008-07-29 22:34:09 -07001273 mutex_init(&ch->sn.sn2.msg_to_pull_mutex);
Dean Nelsone17d4162008-07-29 22:34:06 -07001274 init_completion(&ch->wdisconnect_wait);
1275
1276 atomic_set(&ch->n_on_msg_allocate_wq, 0);
1277 init_waitqueue_head(&ch->msg_allocate_wq);
1278 init_waitqueue_head(&ch->idle_wq);
1279 }
1280
1281 /*
1282 * With the setting of the partition setup_state to XPC_P_SETUP, we're
1283 * declaring that this partition is ready to go.
1284 */
1285 part->setup_state = XPC_P_SETUP;
1286
1287 /*
1288 * Setup the per partition specific variables required by the
1289 * remote partition to establish channel connections with us.
1290 *
1291 * The setting of the magic # indicates that these per partition
1292 * specific variables are ready to be used.
1293 */
Dean Nelson8e85c232008-07-29 22:34:13 -07001294 xpc_vars_part_sn2[partid].GPs_pa = __pa(part_sn2->local_GPs);
1295 xpc_vars_part_sn2[partid].openclose_args_pa =
Dean Nelsone17d4162008-07-29 22:34:06 -07001296 __pa(part->local_openclose_args);
Dean Nelson8e85c232008-07-29 22:34:13 -07001297 xpc_vars_part_sn2[partid].chctl_amo_pa =
1298 __pa(part_sn2->local_chctl_amo_va);
Dean Nelsone17d4162008-07-29 22:34:06 -07001299 cpuid = raw_smp_processor_id(); /* any CPU in this partition will do */
Dean Nelson8e85c232008-07-29 22:34:13 -07001300 xpc_vars_part_sn2[partid].notify_IRQ_nasid = cpuid_to_nasid(cpuid);
1301 xpc_vars_part_sn2[partid].notify_IRQ_phys_cpuid =
1302 cpu_physical_id(cpuid);
1303 xpc_vars_part_sn2[partid].nchannels = part->nchannels;
1304 xpc_vars_part_sn2[partid].magic = XPC_VP_MAGIC1;
Dean Nelsone17d4162008-07-29 22:34:06 -07001305
1306 return xpSuccess;
1307
1308 /* setup of infrastructure failed */
1309out_5:
1310 kfree(part->remote_openclose_args_base);
1311 part->remote_openclose_args = NULL;
1312out_4:
1313 kfree(part->local_openclose_args_base);
1314 part->local_openclose_args = NULL;
1315out_3:
Dean Nelsona47d5da2008-07-29 22:34:09 -07001316 kfree(part_sn2->remote_GPs_base);
1317 part_sn2->remote_GPs = NULL;
Dean Nelsone17d4162008-07-29 22:34:06 -07001318out_2:
Dean Nelsona47d5da2008-07-29 22:34:09 -07001319 kfree(part_sn2->local_GPs_base);
1320 part_sn2->local_GPs = NULL;
Dean Nelsone17d4162008-07-29 22:34:06 -07001321out_1:
1322 kfree(part->channels);
1323 part->channels = NULL;
1324 return retval;
1325}
1326
1327/*
1328 * Teardown the infrastructure necessary to support XPartition Communication
1329 * between the specified remote partition and the local one.
1330 */
1331static void
1332xpc_teardown_infrastructure_sn2(struct xpc_partition *part)
1333{
Dean Nelsona47d5da2008-07-29 22:34:09 -07001334 struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
Dean Nelsone17d4162008-07-29 22:34:06 -07001335 short partid = XPC_PARTID(part);
1336
1337 /*
1338 * We start off by making this partition inaccessible to local
1339 * processes by marking it as no longer setup. Then we make it
1340 * inaccessible to remote processes by clearing the XPC per partition
1341 * specific variable's magic # (which indicates that these variables
Dean Nelson7fb5e592008-07-29 22:34:10 -07001342 * are no longer valid) and by ignoring all XPC notify IRQs sent to
Dean Nelsone17d4162008-07-29 22:34:06 -07001343 * this partition.
1344 */
1345
1346 DBUG_ON(atomic_read(&part->nchannels_engaged) != 0);
1347 DBUG_ON(atomic_read(&part->nchannels_active) != 0);
1348 DBUG_ON(part->setup_state != XPC_P_SETUP);
1349 part->setup_state = XPC_P_WTEARDOWN;
1350
Dean Nelson8e85c232008-07-29 22:34:13 -07001351 xpc_vars_part_sn2[partid].magic = 0;
Dean Nelsone17d4162008-07-29 22:34:06 -07001352
1353 free_irq(SGI_XPC_NOTIFY, (void *)(u64)partid);
1354
1355 /*
1356 * Before proceeding with the teardown we have to wait until all
1357 * existing references cease.
1358 */
1359 wait_event(part->teardown_wq, (atomic_read(&part->references) == 0));
1360
1361 /* now we can begin tearing down the infrastructure */
1362
1363 part->setup_state = XPC_P_TORNDOWN;
1364
1365 /* in case we've still got outstanding timers registered... */
Dean Nelsona47d5da2008-07-29 22:34:09 -07001366 del_timer_sync(&part_sn2->dropped_notify_IRQ_timer);
Dean Nelsone17d4162008-07-29 22:34:06 -07001367
1368 kfree(part->remote_openclose_args_base);
1369 part->remote_openclose_args = NULL;
1370 kfree(part->local_openclose_args_base);
1371 part->local_openclose_args = NULL;
Dean Nelsona47d5da2008-07-29 22:34:09 -07001372 kfree(part_sn2->remote_GPs_base);
1373 part_sn2->remote_GPs = NULL;
1374 kfree(part_sn2->local_GPs_base);
1375 part_sn2->local_GPs = NULL;
Dean Nelsone17d4162008-07-29 22:34:06 -07001376 kfree(part->channels);
1377 part->channels = NULL;
Dean Nelson7fb5e592008-07-29 22:34:10 -07001378 part_sn2->local_chctl_amo_va = NULL;
Dean Nelsone17d4162008-07-29 22:34:06 -07001379}
1380
1381/*
1382 * Create a wrapper that hides the underlying mechanism for pulling a cacheline
1383 * (or multiple cachelines) from a remote partition.
1384 *
1385 * src must be a cacheline aligned physical address on the remote partition.
1386 * dst must be a cacheline aligned virtual address on this partition.
1387 * cnt must be cacheline sized
1388 */
Dean Nelsonea57f802008-07-29 22:34:14 -07001389/* ??? Replace this function by call to xp_remote_memcpy() or bte_copy()? */
Dean Nelsone17d4162008-07-29 22:34:06 -07001390static enum xp_retval
1391xpc_pull_remote_cachelines_sn2(struct xpc_partition *part, void *dst,
1392 const void *src, size_t cnt)
1393{
1394 enum xp_retval ret;
1395
1396 DBUG_ON((u64)src != L1_CACHE_ALIGN((u64)src));
1397 DBUG_ON((u64)dst != L1_CACHE_ALIGN((u64)dst));
1398 DBUG_ON(cnt != L1_CACHE_ALIGN(cnt));
1399
1400 if (part->act_state == XPC_P_DEACTIVATING)
1401 return part->reason;
1402
1403 ret = xp_remote_memcpy(dst, src, cnt);
1404 if (ret != xpSuccess) {
1405 dev_dbg(xpc_chan, "xp_remote_memcpy() from partition %d failed,"
1406 " ret=%d\n", XPC_PARTID(part), ret);
1407 }
1408 return ret;
1409}
1410
1411/*
1412 * Pull the remote per partition specific variables from the specified
1413 * partition.
1414 */
1415static enum xp_retval
1416xpc_pull_remote_vars_part_sn2(struct xpc_partition *part)
1417{
Dean Nelsona47d5da2008-07-29 22:34:09 -07001418 struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
Dean Nelsone17d4162008-07-29 22:34:06 -07001419 u8 buffer[L1_CACHE_BYTES * 2];
1420 struct xpc_vars_part_sn2 *pulled_entry_cacheline =
1421 (struct xpc_vars_part_sn2 *)L1_CACHE_ALIGN((u64)buffer);
1422 struct xpc_vars_part_sn2 *pulled_entry;
1423 u64 remote_entry_cacheline_pa, remote_entry_pa;
1424 short partid = XPC_PARTID(part);
1425 enum xp_retval ret;
1426
1427 /* pull the cacheline that contains the variables we're interested in */
1428
Dean Nelsona47d5da2008-07-29 22:34:09 -07001429 DBUG_ON(part_sn2->remote_vars_part_pa !=
1430 L1_CACHE_ALIGN(part_sn2->remote_vars_part_pa));
Dean Nelsone17d4162008-07-29 22:34:06 -07001431 DBUG_ON(sizeof(struct xpc_vars_part_sn2) != L1_CACHE_BYTES / 2);
1432
Dean Nelsona47d5da2008-07-29 22:34:09 -07001433 remote_entry_pa = part_sn2->remote_vars_part_pa +
Dean Nelsone17d4162008-07-29 22:34:06 -07001434 sn_partition_id * sizeof(struct xpc_vars_part_sn2);
1435
1436 remote_entry_cacheline_pa = (remote_entry_pa & ~(L1_CACHE_BYTES - 1));
1437
1438 pulled_entry = (struct xpc_vars_part_sn2 *)((u64)pulled_entry_cacheline
1439 + (remote_entry_pa &
1440 (L1_CACHE_BYTES - 1)));
1441
1442 ret = xpc_pull_remote_cachelines_sn2(part, pulled_entry_cacheline,
1443 (void *)remote_entry_cacheline_pa,
1444 L1_CACHE_BYTES);
1445 if (ret != xpSuccess) {
1446 dev_dbg(xpc_chan, "failed to pull XPC vars_part from "
1447 "partition %d, ret=%d\n", partid, ret);
1448 return ret;
1449 }
1450
1451 /* see if they've been set up yet */
1452
1453 if (pulled_entry->magic != XPC_VP_MAGIC1 &&
1454 pulled_entry->magic != XPC_VP_MAGIC2) {
1455
1456 if (pulled_entry->magic != 0) {
1457 dev_dbg(xpc_chan, "partition %d's XPC vars_part for "
1458 "partition %d has bad magic value (=0x%lx)\n",
1459 partid, sn_partition_id, pulled_entry->magic);
1460 return xpBadMagic;
1461 }
1462
1463 /* they've not been initialized yet */
1464 return xpRetry;
1465 }
1466
Dean Nelson8e85c232008-07-29 22:34:13 -07001467 if (xpc_vars_part_sn2[partid].magic == XPC_VP_MAGIC1) {
Dean Nelsone17d4162008-07-29 22:34:06 -07001468
1469 /* validate the variables */
1470
1471 if (pulled_entry->GPs_pa == 0 ||
1472 pulled_entry->openclose_args_pa == 0 ||
Dean Nelson7fb5e592008-07-29 22:34:10 -07001473 pulled_entry->chctl_amo_pa == 0) {
Dean Nelsone17d4162008-07-29 22:34:06 -07001474
1475 dev_err(xpc_chan, "partition %d's XPC vars_part for "
1476 "partition %d are not valid\n", partid,
1477 sn_partition_id);
1478 return xpInvalidAddress;
1479 }
1480
1481 /* the variables we imported look to be valid */
1482
Dean Nelsona47d5da2008-07-29 22:34:09 -07001483 part_sn2->remote_GPs_pa = pulled_entry->GPs_pa;
1484 part_sn2->remote_openclose_args_pa =
Dean Nelsone17d4162008-07-29 22:34:06 -07001485 pulled_entry->openclose_args_pa;
Dean Nelson7fb5e592008-07-29 22:34:10 -07001486 part_sn2->remote_chctl_amo_va =
Dean Nelsonc39838c2008-07-29 22:34:11 -07001487 (struct amo *)__va(pulled_entry->chctl_amo_pa);
Dean Nelson7fb5e592008-07-29 22:34:10 -07001488 part_sn2->notify_IRQ_nasid = pulled_entry->notify_IRQ_nasid;
1489 part_sn2->notify_IRQ_phys_cpuid =
1490 pulled_entry->notify_IRQ_phys_cpuid;
Dean Nelsone17d4162008-07-29 22:34:06 -07001491
1492 if (part->nchannels > pulled_entry->nchannels)
1493 part->nchannels = pulled_entry->nchannels;
1494
1495 /* let the other side know that we've pulled their variables */
1496
Dean Nelson8e85c232008-07-29 22:34:13 -07001497 xpc_vars_part_sn2[partid].magic = XPC_VP_MAGIC2;
Dean Nelsone17d4162008-07-29 22:34:06 -07001498 }
1499
1500 if (pulled_entry->magic == XPC_VP_MAGIC1)
1501 return xpRetry;
1502
1503 return xpSuccess;
1504}
1505
1506/*
1507 * Establish first contact with the remote partititon. This involves pulling
1508 * the XPC per partition variables from the remote partition and waiting for
1509 * the remote partition to pull ours.
1510 */
1511static enum xp_retval
1512xpc_make_first_contact_sn2(struct xpc_partition *part)
1513{
Dean Nelsona47d5da2008-07-29 22:34:09 -07001514 struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
Dean Nelsone17d4162008-07-29 22:34:06 -07001515 enum xp_retval ret;
1516
Dean Nelson33ba3c72008-07-29 22:34:07 -07001517 /*
Dean Nelsonc39838c2008-07-29 22:34:11 -07001518 * Register the remote partition's amos with SAL so it can handle
Dean Nelson33ba3c72008-07-29 22:34:07 -07001519 * and cleanup errors within that address range should the remote
1520 * partition go down. We don't unregister this range because it is
1521 * difficult to tell when outstanding writes to the remote partition
1522 * are finished and thus when it is safe to unregister. This should
1523 * not result in wasted space in the SAL xp_addr_region table because
1524 * we should get the same page for remote_amos_page_pa after module
1525 * reloads and system reboots.
1526 */
Dean Nelsona47d5da2008-07-29 22:34:09 -07001527 if (sn_register_xp_addr_region(part_sn2->remote_amos_page_pa,
Dean Nelson33ba3c72008-07-29 22:34:07 -07001528 PAGE_SIZE, 1) < 0) {
1529 dev_warn(xpc_part, "xpc_activating(%d) failed to register "
1530 "xp_addr region\n", XPC_PARTID(part));
1531
1532 ret = xpPhysAddrRegFailed;
1533 XPC_DEACTIVATE_PARTITION(part, ret);
1534 return ret;
1535 }
1536
Dean Nelsona47d5da2008-07-29 22:34:09 -07001537 /*
1538 * Send activate IRQ to get other side to activate if they've not
1539 * already begun to do so.
1540 */
Dean Nelson7fb5e592008-07-29 22:34:10 -07001541 xpc_send_activate_IRQ_sn2(part_sn2->remote_amos_page_pa,
Dean Nelsona47d5da2008-07-29 22:34:09 -07001542 cnodeid_to_nasid(0),
1543 part_sn2->activate_IRQ_nasid,
1544 part_sn2->activate_IRQ_phys_cpuid);
Dean Nelson33ba3c72008-07-29 22:34:07 -07001545
Dean Nelsone17d4162008-07-29 22:34:06 -07001546 while ((ret = xpc_pull_remote_vars_part_sn2(part)) != xpSuccess) {
1547 if (ret != xpRetry) {
1548 XPC_DEACTIVATE_PARTITION(part, ret);
1549 return ret;
1550 }
1551
1552 dev_dbg(xpc_part, "waiting to make first contact with "
1553 "partition %d\n", XPC_PARTID(part));
1554
1555 /* wait a 1/4 of a second or so */
1556 (void)msleep_interruptible(250);
1557
1558 if (part->act_state == XPC_P_DEACTIVATING)
1559 return part->reason;
1560 }
1561
1562 return xpSuccess;
1563}
1564
1565/*
Dean Nelson7fb5e592008-07-29 22:34:10 -07001566 * Get the chctl flags and pull the openclose args and/or remote GPs as needed.
Dean Nelsone17d4162008-07-29 22:34:06 -07001567 */
1568static u64
Dean Nelson7fb5e592008-07-29 22:34:10 -07001569xpc_get_chctl_all_flags_sn2(struct xpc_partition *part)
Dean Nelsone17d4162008-07-29 22:34:06 -07001570{
Dean Nelsona47d5da2008-07-29 22:34:09 -07001571 struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
Dean Nelsone17d4162008-07-29 22:34:06 -07001572 unsigned long irq_flags;
Dean Nelson7fb5e592008-07-29 22:34:10 -07001573 union xpc_channel_ctl_flags chctl;
Dean Nelsone17d4162008-07-29 22:34:06 -07001574 enum xp_retval ret;
1575
1576 /*
Dean Nelson7fb5e592008-07-29 22:34:10 -07001577 * See if there are any chctl flags to be handled.
Dean Nelsone17d4162008-07-29 22:34:06 -07001578 */
1579
Dean Nelson7fb5e592008-07-29 22:34:10 -07001580 spin_lock_irqsave(&part->chctl_lock, irq_flags);
1581 chctl = part->chctl;
1582 if (chctl.all_flags != 0)
1583 part->chctl.all_flags = 0;
Dean Nelsone17d4162008-07-29 22:34:06 -07001584
Dean Nelson7fb5e592008-07-29 22:34:10 -07001585 spin_unlock_irqrestore(&part->chctl_lock, irq_flags);
Dean Nelsone17d4162008-07-29 22:34:06 -07001586
Dean Nelson7fb5e592008-07-29 22:34:10 -07001587 if (xpc_any_openclose_chctl_flags_set(&chctl)) {
Dean Nelsona47d5da2008-07-29 22:34:09 -07001588 ret = xpc_pull_remote_cachelines_sn2(part, part->
1589 remote_openclose_args,
1590 (void *)part_sn2->
Dean Nelsone17d4162008-07-29 22:34:06 -07001591 remote_openclose_args_pa,
1592 XPC_OPENCLOSE_ARGS_SIZE);
1593 if (ret != xpSuccess) {
1594 XPC_DEACTIVATE_PARTITION(part, ret);
1595
1596 dev_dbg(xpc_chan, "failed to pull openclose args from "
1597 "partition %d, ret=%d\n", XPC_PARTID(part),
1598 ret);
1599
Dean Nelson7fb5e592008-07-29 22:34:10 -07001600 /* don't bother processing chctl flags anymore */
1601 chctl.all_flags = 0;
Dean Nelsone17d4162008-07-29 22:34:06 -07001602 }
1603 }
1604
Dean Nelson7fb5e592008-07-29 22:34:10 -07001605 if (xpc_any_msg_chctl_flags_set(&chctl)) {
Dean Nelsona47d5da2008-07-29 22:34:09 -07001606 ret = xpc_pull_remote_cachelines_sn2(part, part_sn2->remote_GPs,
1607 (void *)part_sn2->remote_GPs_pa,
Dean Nelsone17d4162008-07-29 22:34:06 -07001608 XPC_GP_SIZE);
1609 if (ret != xpSuccess) {
1610 XPC_DEACTIVATE_PARTITION(part, ret);
1611
1612 dev_dbg(xpc_chan, "failed to pull GPs from partition "
1613 "%d, ret=%d\n", XPC_PARTID(part), ret);
1614
Dean Nelson7fb5e592008-07-29 22:34:10 -07001615 /* don't bother processing chctl flags anymore */
1616 chctl.all_flags = 0;
Dean Nelsone17d4162008-07-29 22:34:06 -07001617 }
1618 }
1619
Dean Nelson7fb5e592008-07-29 22:34:10 -07001620 return chctl.all_flags;
Dean Nelsone17d4162008-07-29 22:34:06 -07001621}
1622
Dean Nelsona47d5da2008-07-29 22:34:09 -07001623/*
Dean Nelson185c3a12008-07-29 22:34:11 -07001624 * Allocate the local message queue and the notify queue.
1625 */
1626static enum xp_retval
1627xpc_allocate_local_msgqueue_sn2(struct xpc_channel *ch)
1628{
1629 unsigned long irq_flags;
1630 int nentries;
1631 size_t nbytes;
1632
1633 for (nentries = ch->local_nentries; nentries > 0; nentries--) {
1634
1635 nbytes = nentries * ch->msg_size;
1636 ch->local_msgqueue =
1637 xpc_kzalloc_cacheline_aligned_sn2(nbytes, GFP_KERNEL,
1638 &ch->local_msgqueue_base);
1639 if (ch->local_msgqueue == NULL)
1640 continue;
1641
1642 nbytes = nentries * sizeof(struct xpc_notify);
1643 ch->notify_queue = kzalloc(nbytes, GFP_KERNEL);
1644 if (ch->notify_queue == NULL) {
1645 kfree(ch->local_msgqueue_base);
1646 ch->local_msgqueue = NULL;
1647 continue;
1648 }
1649
1650 spin_lock_irqsave(&ch->lock, irq_flags);
1651 if (nentries < ch->local_nentries) {
1652 dev_dbg(xpc_chan, "nentries=%d local_nentries=%d, "
1653 "partid=%d, channel=%d\n", nentries,
1654 ch->local_nentries, ch->partid, ch->number);
1655
1656 ch->local_nentries = nentries;
1657 }
1658 spin_unlock_irqrestore(&ch->lock, irq_flags);
1659 return xpSuccess;
1660 }
1661
1662 dev_dbg(xpc_chan, "can't get memory for local message queue and notify "
1663 "queue, partid=%d, channel=%d\n", ch->partid, ch->number);
1664 return xpNoMemory;
1665}
1666
1667/*
1668 * Allocate the cached remote message queue.
1669 */
1670static enum xp_retval
1671xpc_allocate_remote_msgqueue_sn2(struct xpc_channel *ch)
1672{
1673 unsigned long irq_flags;
1674 int nentries;
1675 size_t nbytes;
1676
1677 DBUG_ON(ch->remote_nentries <= 0);
1678
1679 for (nentries = ch->remote_nentries; nentries > 0; nentries--) {
1680
1681 nbytes = nentries * ch->msg_size;
1682 ch->remote_msgqueue =
1683 xpc_kzalloc_cacheline_aligned_sn2(nbytes, GFP_KERNEL,
1684 &ch->remote_msgqueue_base);
1685 if (ch->remote_msgqueue == NULL)
1686 continue;
1687
1688 spin_lock_irqsave(&ch->lock, irq_flags);
1689 if (nentries < ch->remote_nentries) {
1690 dev_dbg(xpc_chan, "nentries=%d remote_nentries=%d, "
1691 "partid=%d, channel=%d\n", nentries,
1692 ch->remote_nentries, ch->partid, ch->number);
1693
1694 ch->remote_nentries = nentries;
1695 }
1696 spin_unlock_irqrestore(&ch->lock, irq_flags);
1697 return xpSuccess;
1698 }
1699
1700 dev_dbg(xpc_chan, "can't get memory for cached remote message queue, "
1701 "partid=%d, channel=%d\n", ch->partid, ch->number);
1702 return xpNoMemory;
1703}
1704
1705/*
1706 * Allocate message queues and other stuff associated with a channel.
1707 *
1708 * Note: Assumes all of the channel sizes are filled in.
1709 */
1710static enum xp_retval
1711xpc_allocate_msgqueues_sn2(struct xpc_channel *ch)
1712{
1713 enum xp_retval ret;
1714
1715 DBUG_ON(ch->flags & XPC_C_SETUP);
1716
1717 ret = xpc_allocate_local_msgqueue_sn2(ch);
1718 if (ret == xpSuccess) {
1719
1720 ret = xpc_allocate_remote_msgqueue_sn2(ch);
1721 if (ret != xpSuccess) {
1722 kfree(ch->local_msgqueue_base);
1723 ch->local_msgqueue = NULL;
1724 kfree(ch->notify_queue);
1725 ch->notify_queue = NULL;
1726 }
1727 }
1728 return ret;
1729}
1730
1731/*
1732 * Free up message queues and other stuff that were allocated for the specified
1733 * channel.
1734 *
1735 * Note: ch->reason and ch->reason_line are left set for debugging purposes,
1736 * they're cleared when XPC_C_DISCONNECTED is cleared.
1737 */
1738static void
1739xpc_free_msgqueues_sn2(struct xpc_channel *ch)
1740{
1741 struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
1742
1743 DBUG_ON(!spin_is_locked(&ch->lock));
1744 DBUG_ON(atomic_read(&ch->n_to_notify) != 0);
1745
1746 ch->remote_msgqueue_pa = 0;
1747 ch->func = NULL;
1748 ch->key = NULL;
1749 ch->msg_size = 0;
1750 ch->local_nentries = 0;
1751 ch->remote_nentries = 0;
1752 ch->kthreads_assigned_limit = 0;
1753 ch->kthreads_idle_limit = 0;
1754
1755 ch_sn2->local_GP->get = 0;
1756 ch_sn2->local_GP->put = 0;
1757 ch_sn2->remote_GP.get = 0;
1758 ch_sn2->remote_GP.put = 0;
1759 ch_sn2->w_local_GP.get = 0;
1760 ch_sn2->w_local_GP.put = 0;
1761 ch_sn2->w_remote_GP.get = 0;
1762 ch_sn2->w_remote_GP.put = 0;
1763 ch_sn2->next_msg_to_pull = 0;
1764
1765 if (ch->flags & XPC_C_SETUP) {
1766 dev_dbg(xpc_chan, "ch->flags=0x%x, partid=%d, channel=%d\n",
1767 ch->flags, ch->partid, ch->number);
1768
1769 kfree(ch->local_msgqueue_base);
1770 ch->local_msgqueue = NULL;
1771 kfree(ch->remote_msgqueue_base);
1772 ch->remote_msgqueue = NULL;
1773 kfree(ch->notify_queue);
1774 ch->notify_queue = NULL;
1775 }
1776}
1777
1778/*
Dean Nelsona47d5da2008-07-29 22:34:09 -07001779 * Notify those who wanted to be notified upon delivery of their message.
1780 */
1781static void
1782xpc_notify_senders_sn2(struct xpc_channel *ch, enum xp_retval reason, s64 put)
1783{
1784 struct xpc_notify *notify;
1785 u8 notify_type;
1786 s64 get = ch->sn.sn2.w_remote_GP.get - 1;
1787
1788 while (++get < put && atomic_read(&ch->n_to_notify) > 0) {
1789
1790 notify = &ch->notify_queue[get % ch->local_nentries];
1791
1792 /*
1793 * See if the notify entry indicates it was associated with
1794 * a message who's sender wants to be notified. It is possible
1795 * that it is, but someone else is doing or has done the
1796 * notification.
1797 */
1798 notify_type = notify->type;
1799 if (notify_type == 0 ||
1800 cmpxchg(&notify->type, notify_type, 0) != notify_type) {
1801 continue;
1802 }
1803
1804 DBUG_ON(notify_type != XPC_N_CALL);
1805
1806 atomic_dec(&ch->n_to_notify);
1807
1808 if (notify->func != NULL) {
1809 dev_dbg(xpc_chan, "notify->func() called, notify=0x%p, "
1810 "msg_number=%ld, partid=%d, channel=%d\n",
1811 (void *)notify, get, ch->partid, ch->number);
1812
1813 notify->func(reason, ch->partid, ch->number,
1814 notify->key);
1815
1816 dev_dbg(xpc_chan, "notify->func() returned, "
1817 "notify=0x%p, msg_number=%ld, partid=%d, "
1818 "channel=%d\n", (void *)notify, get,
1819 ch->partid, ch->number);
1820 }
1821 }
1822}
1823
1824static void
1825xpc_notify_senders_of_disconnect_sn2(struct xpc_channel *ch)
1826{
1827 xpc_notify_senders_sn2(ch, ch->reason, ch->sn.sn2.w_local_GP.put);
1828}
1829
1830/*
1831 * Clear some of the msg flags in the local message queue.
1832 */
1833static inline void
1834xpc_clear_local_msgqueue_flags_sn2(struct xpc_channel *ch)
1835{
1836 struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
1837 struct xpc_msg *msg;
1838 s64 get;
1839
1840 get = ch_sn2->w_remote_GP.get;
1841 do {
1842 msg = (struct xpc_msg *)((u64)ch->local_msgqueue +
1843 (get % ch->local_nentries) *
1844 ch->msg_size);
1845 msg->flags = 0;
1846 } while (++get < ch_sn2->remote_GP.get);
1847}
1848
1849/*
1850 * Clear some of the msg flags in the remote message queue.
1851 */
1852static inline void
1853xpc_clear_remote_msgqueue_flags_sn2(struct xpc_channel *ch)
1854{
1855 struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
1856 struct xpc_msg *msg;
1857 s64 put;
1858
1859 put = ch_sn2->w_remote_GP.put;
1860 do {
1861 msg = (struct xpc_msg *)((u64)ch->remote_msgqueue +
1862 (put % ch->remote_nentries) *
1863 ch->msg_size);
1864 msg->flags = 0;
1865 } while (++put < ch_sn2->remote_GP.put);
1866}
1867
1868static void
Dean Nelson7fb5e592008-07-29 22:34:10 -07001869xpc_process_msg_chctl_flags_sn2(struct xpc_partition *part, int ch_number)
Dean Nelsona47d5da2008-07-29 22:34:09 -07001870{
1871 struct xpc_channel *ch = &part->channels[ch_number];
1872 struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
1873 int nmsgs_sent;
1874
1875 ch_sn2->remote_GP = part->sn.sn2.remote_GPs[ch_number];
1876
1877 /* See what, if anything, has changed for each connected channel */
1878
1879 xpc_msgqueue_ref(ch);
1880
1881 if (ch_sn2->w_remote_GP.get == ch_sn2->remote_GP.get &&
1882 ch_sn2->w_remote_GP.put == ch_sn2->remote_GP.put) {
1883 /* nothing changed since GPs were last pulled */
1884 xpc_msgqueue_deref(ch);
1885 return;
1886 }
1887
1888 if (!(ch->flags & XPC_C_CONNECTED)) {
1889 xpc_msgqueue_deref(ch);
1890 return;
1891 }
1892
1893 /*
1894 * First check to see if messages recently sent by us have been
1895 * received by the other side. (The remote GET value will have
1896 * changed since we last looked at it.)
1897 */
1898
1899 if (ch_sn2->w_remote_GP.get != ch_sn2->remote_GP.get) {
1900
1901 /*
1902 * We need to notify any senders that want to be notified
1903 * that their sent messages have been received by their
1904 * intended recipients. We need to do this before updating
1905 * w_remote_GP.get so that we don't allocate the same message
1906 * queue entries prematurely (see xpc_allocate_msg()).
1907 */
1908 if (atomic_read(&ch->n_to_notify) > 0) {
1909 /*
1910 * Notify senders that messages sent have been
1911 * received and delivered by the other side.
1912 */
1913 xpc_notify_senders_sn2(ch, xpMsgDelivered,
1914 ch_sn2->remote_GP.get);
1915 }
1916
1917 /*
1918 * Clear msg->flags in previously sent messages, so that
1919 * they're ready for xpc_allocate_msg().
1920 */
1921 xpc_clear_local_msgqueue_flags_sn2(ch);
1922
1923 ch_sn2->w_remote_GP.get = ch_sn2->remote_GP.get;
1924
1925 dev_dbg(xpc_chan, "w_remote_GP.get changed to %ld, partid=%d, "
1926 "channel=%d\n", ch_sn2->w_remote_GP.get, ch->partid,
1927 ch->number);
1928
1929 /*
1930 * If anyone was waiting for message queue entries to become
1931 * available, wake them up.
1932 */
1933 if (atomic_read(&ch->n_on_msg_allocate_wq) > 0)
1934 wake_up(&ch->msg_allocate_wq);
1935 }
1936
1937 /*
1938 * Now check for newly sent messages by the other side. (The remote
1939 * PUT value will have changed since we last looked at it.)
1940 */
1941
1942 if (ch_sn2->w_remote_GP.put != ch_sn2->remote_GP.put) {
1943 /*
1944 * Clear msg->flags in previously received messages, so that
1945 * they're ready for xpc_get_deliverable_msg().
1946 */
1947 xpc_clear_remote_msgqueue_flags_sn2(ch);
1948
1949 ch_sn2->w_remote_GP.put = ch_sn2->remote_GP.put;
1950
1951 dev_dbg(xpc_chan, "w_remote_GP.put changed to %ld, partid=%d, "
1952 "channel=%d\n", ch_sn2->w_remote_GP.put, ch->partid,
1953 ch->number);
1954
1955 nmsgs_sent = ch_sn2->w_remote_GP.put - ch_sn2->w_local_GP.get;
1956 if (nmsgs_sent > 0) {
1957 dev_dbg(xpc_chan, "msgs waiting to be copied and "
1958 "delivered=%d, partid=%d, channel=%d\n",
1959 nmsgs_sent, ch->partid, ch->number);
1960
1961 if (ch->flags & XPC_C_CONNECTEDCALLOUT_MADE)
1962 xpc_activate_kthreads(ch, nmsgs_sent);
1963 }
1964 }
1965
1966 xpc_msgqueue_deref(ch);
1967}
1968
Dean Nelsone17d4162008-07-29 22:34:06 -07001969static struct xpc_msg *
1970xpc_pull_remote_msg_sn2(struct xpc_channel *ch, s64 get)
1971{
1972 struct xpc_partition *part = &xpc_partitions[ch->partid];
Dean Nelsona47d5da2008-07-29 22:34:09 -07001973 struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
Dean Nelsone17d4162008-07-29 22:34:06 -07001974 struct xpc_msg *remote_msg, *msg;
1975 u32 msg_index, nmsgs;
1976 u64 msg_offset;
1977 enum xp_retval ret;
1978
Dean Nelsona47d5da2008-07-29 22:34:09 -07001979 if (mutex_lock_interruptible(&ch_sn2->msg_to_pull_mutex) != 0) {
Dean Nelsone17d4162008-07-29 22:34:06 -07001980 /* we were interrupted by a signal */
1981 return NULL;
1982 }
1983
Dean Nelsona47d5da2008-07-29 22:34:09 -07001984 while (get >= ch_sn2->next_msg_to_pull) {
Dean Nelsone17d4162008-07-29 22:34:06 -07001985
1986 /* pull as many messages as are ready and able to be pulled */
1987
Dean Nelsona47d5da2008-07-29 22:34:09 -07001988 msg_index = ch_sn2->next_msg_to_pull % ch->remote_nentries;
Dean Nelsone17d4162008-07-29 22:34:06 -07001989
Dean Nelsona47d5da2008-07-29 22:34:09 -07001990 DBUG_ON(ch_sn2->next_msg_to_pull >= ch_sn2->w_remote_GP.put);
1991 nmsgs = ch_sn2->w_remote_GP.put - ch_sn2->next_msg_to_pull;
Dean Nelsone17d4162008-07-29 22:34:06 -07001992 if (msg_index + nmsgs > ch->remote_nentries) {
1993 /* ignore the ones that wrap the msg queue for now */
1994 nmsgs = ch->remote_nentries - msg_index;
1995 }
1996
1997 msg_offset = msg_index * ch->msg_size;
1998 msg = (struct xpc_msg *)((u64)ch->remote_msgqueue + msg_offset);
1999 remote_msg = (struct xpc_msg *)(ch->remote_msgqueue_pa +
2000 msg_offset);
2001
2002 ret = xpc_pull_remote_cachelines_sn2(part, msg, remote_msg,
2003 nmsgs * ch->msg_size);
2004 if (ret != xpSuccess) {
2005
2006 dev_dbg(xpc_chan, "failed to pull %d msgs starting with"
2007 " msg %ld from partition %d, channel=%d, "
Dean Nelsona47d5da2008-07-29 22:34:09 -07002008 "ret=%d\n", nmsgs, ch_sn2->next_msg_to_pull,
Dean Nelsone17d4162008-07-29 22:34:06 -07002009 ch->partid, ch->number, ret);
2010
2011 XPC_DEACTIVATE_PARTITION(part, ret);
2012
Dean Nelsona47d5da2008-07-29 22:34:09 -07002013 mutex_unlock(&ch_sn2->msg_to_pull_mutex);
Dean Nelsone17d4162008-07-29 22:34:06 -07002014 return NULL;
2015 }
2016
Dean Nelsona47d5da2008-07-29 22:34:09 -07002017 ch_sn2->next_msg_to_pull += nmsgs;
Dean Nelsone17d4162008-07-29 22:34:06 -07002018 }
2019
Dean Nelsona47d5da2008-07-29 22:34:09 -07002020 mutex_unlock(&ch_sn2->msg_to_pull_mutex);
Dean Nelsone17d4162008-07-29 22:34:06 -07002021
2022 /* return the message we were looking for */
2023 msg_offset = (get % ch->remote_nentries) * ch->msg_size;
2024 msg = (struct xpc_msg *)((u64)ch->remote_msgqueue + msg_offset);
2025
2026 return msg;
2027}
2028
Dean Nelsona47d5da2008-07-29 22:34:09 -07002029static int
2030xpc_n_of_deliverable_msgs_sn2(struct xpc_channel *ch)
2031{
2032 return ch->sn.sn2.w_remote_GP.put - ch->sn.sn2.w_local_GP.get;
2033}
2034
Dean Nelsone17d4162008-07-29 22:34:06 -07002035/*
2036 * Get a message to be delivered.
2037 */
2038static struct xpc_msg *
2039xpc_get_deliverable_msg_sn2(struct xpc_channel *ch)
2040{
Dean Nelsona47d5da2008-07-29 22:34:09 -07002041 struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
Dean Nelsone17d4162008-07-29 22:34:06 -07002042 struct xpc_msg *msg = NULL;
2043 s64 get;
2044
2045 do {
2046 if (ch->flags & XPC_C_DISCONNECTING)
2047 break;
2048
Dean Nelsona47d5da2008-07-29 22:34:09 -07002049 get = ch_sn2->w_local_GP.get;
Dean Nelsone17d4162008-07-29 22:34:06 -07002050 rmb(); /* guarantee that .get loads before .put */
Dean Nelsona47d5da2008-07-29 22:34:09 -07002051 if (get == ch_sn2->w_remote_GP.put)
Dean Nelsone17d4162008-07-29 22:34:06 -07002052 break;
2053
2054 /* There are messages waiting to be pulled and delivered.
2055 * We need to try to secure one for ourselves. We'll do this
2056 * by trying to increment w_local_GP.get and hope that no one
2057 * else beats us to it. If they do, we'll we'll simply have
2058 * to try again for the next one.
2059 */
2060
Dean Nelsona47d5da2008-07-29 22:34:09 -07002061 if (cmpxchg(&ch_sn2->w_local_GP.get, get, get + 1) == get) {
Dean Nelsone17d4162008-07-29 22:34:06 -07002062 /* we got the entry referenced by get */
2063
2064 dev_dbg(xpc_chan, "w_local_GP.get changed to %ld, "
2065 "partid=%d, channel=%d\n", get + 1,
2066 ch->partid, ch->number);
2067
2068 /* pull the message from the remote partition */
2069
2070 msg = xpc_pull_remote_msg_sn2(ch, get);
2071
2072 DBUG_ON(msg != NULL && msg->number != get);
2073 DBUG_ON(msg != NULL && (msg->flags & XPC_M_DONE));
2074 DBUG_ON(msg != NULL && !(msg->flags & XPC_M_READY));
2075
2076 break;
2077 }
2078
2079 } while (1);
2080
2081 return msg;
2082}
2083
Dean Nelson33ba3c72008-07-29 22:34:07 -07002084/*
2085 * Now we actually send the messages that are ready to be sent by advancing
Dean Nelson7fb5e592008-07-29 22:34:10 -07002086 * the local message queue's Put value and then send a chctl msgrequest to the
2087 * recipient partition.
Dean Nelson33ba3c72008-07-29 22:34:07 -07002088 */
2089static void
2090xpc_send_msgs_sn2(struct xpc_channel *ch, s64 initial_put)
2091{
Dean Nelsona47d5da2008-07-29 22:34:09 -07002092 struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002093 struct xpc_msg *msg;
2094 s64 put = initial_put + 1;
Dean Nelson7fb5e592008-07-29 22:34:10 -07002095 int send_msgrequest = 0;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002096
2097 while (1) {
2098
2099 while (1) {
Dean Nelsona47d5da2008-07-29 22:34:09 -07002100 if (put == ch_sn2->w_local_GP.put)
Dean Nelson33ba3c72008-07-29 22:34:07 -07002101 break;
2102
2103 msg = (struct xpc_msg *)((u64)ch->local_msgqueue +
2104 (put % ch->local_nentries) *
2105 ch->msg_size);
2106
2107 if (!(msg->flags & XPC_M_READY))
2108 break;
2109
2110 put++;
2111 }
2112
2113 if (put == initial_put) {
2114 /* nothing's changed */
2115 break;
2116 }
2117
Dean Nelsona47d5da2008-07-29 22:34:09 -07002118 if (cmpxchg_rel(&ch_sn2->local_GP->put, initial_put, put) !=
Dean Nelson33ba3c72008-07-29 22:34:07 -07002119 initial_put) {
2120 /* someone else beat us to it */
Dean Nelsona47d5da2008-07-29 22:34:09 -07002121 DBUG_ON(ch_sn2->local_GP->put < initial_put);
Dean Nelson33ba3c72008-07-29 22:34:07 -07002122 break;
2123 }
2124
2125 /* we just set the new value of local_GP->put */
2126
2127 dev_dbg(xpc_chan, "local_GP->put changed to %ld, partid=%d, "
2128 "channel=%d\n", put, ch->partid, ch->number);
2129
Dean Nelson7fb5e592008-07-29 22:34:10 -07002130 send_msgrequest = 1;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002131
2132 /*
2133 * We need to ensure that the message referenced by
2134 * local_GP->put is not XPC_M_READY or that local_GP->put
2135 * equals w_local_GP.put, so we'll go have a look.
2136 */
2137 initial_put = put;
2138 }
2139
Dean Nelson7fb5e592008-07-29 22:34:10 -07002140 if (send_msgrequest)
2141 xpc_send_chctl_msgrequest_sn2(ch);
Dean Nelson33ba3c72008-07-29 22:34:07 -07002142}
2143
2144/*
2145 * Allocate an entry for a message from the message queue associated with the
2146 * specified channel.
2147 */
2148static enum xp_retval
2149xpc_allocate_msg_sn2(struct xpc_channel *ch, u32 flags,
2150 struct xpc_msg **address_of_msg)
2151{
Dean Nelsona47d5da2008-07-29 22:34:09 -07002152 struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002153 struct xpc_msg *msg;
2154 enum xp_retval ret;
2155 s64 put;
2156
Dean Nelson33ba3c72008-07-29 22:34:07 -07002157 /*
2158 * Get the next available message entry from the local message queue.
2159 * If none are available, we'll make sure that we grab the latest
2160 * GP values.
2161 */
2162 ret = xpTimeout;
2163
2164 while (1) {
2165
Dean Nelsona47d5da2008-07-29 22:34:09 -07002166 put = ch_sn2->w_local_GP.put;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002167 rmb(); /* guarantee that .put loads before .get */
Dean Nelsona47d5da2008-07-29 22:34:09 -07002168 if (put - ch_sn2->w_remote_GP.get < ch->local_nentries) {
Dean Nelson33ba3c72008-07-29 22:34:07 -07002169
2170 /* There are available message entries. We need to try
2171 * to secure one for ourselves. We'll do this by trying
2172 * to increment w_local_GP.put as long as someone else
2173 * doesn't beat us to it. If they do, we'll have to
2174 * try again.
2175 */
Dean Nelsona47d5da2008-07-29 22:34:09 -07002176 if (cmpxchg(&ch_sn2->w_local_GP.put, put, put + 1) ==
2177 put) {
Dean Nelson33ba3c72008-07-29 22:34:07 -07002178 /* we got the entry referenced by put */
2179 break;
2180 }
2181 continue; /* try again */
2182 }
2183
2184 /*
2185 * There aren't any available msg entries at this time.
2186 *
2187 * In waiting for a message entry to become available,
Dean Nelson7fb5e592008-07-29 22:34:10 -07002188 * we set a timeout in case the other side is not sending
2189 * completion interrupts. This lets us fake a notify IRQ
2190 * that will cause the notify IRQ handler to fetch the latest
2191 * GP values as if an interrupt was sent by the other side.
Dean Nelson33ba3c72008-07-29 22:34:07 -07002192 */
2193 if (ret == xpTimeout)
Dean Nelson7fb5e592008-07-29 22:34:10 -07002194 xpc_send_chctl_local_msgrequest_sn2(ch);
Dean Nelson33ba3c72008-07-29 22:34:07 -07002195
Dean Nelson97bf1aa2008-07-29 22:34:08 -07002196 if (flags & XPC_NOWAIT)
Dean Nelson33ba3c72008-07-29 22:34:07 -07002197 return xpNoWait;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002198
2199 ret = xpc_allocate_msg_wait(ch);
Dean Nelson97bf1aa2008-07-29 22:34:08 -07002200 if (ret != xpInterrupted && ret != xpTimeout)
Dean Nelson33ba3c72008-07-29 22:34:07 -07002201 return ret;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002202 }
2203
2204 /* get the message's address and initialize it */
2205 msg = (struct xpc_msg *)((u64)ch->local_msgqueue +
2206 (put % ch->local_nentries) * ch->msg_size);
2207
2208 DBUG_ON(msg->flags != 0);
2209 msg->number = put;
2210
2211 dev_dbg(xpc_chan, "w_local_GP.put changed to %ld; msg=0x%p, "
2212 "msg_number=%ld, partid=%d, channel=%d\n", put + 1,
2213 (void *)msg, msg->number, ch->partid, ch->number);
2214
2215 *address_of_msg = msg;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002216 return xpSuccess;
2217}
2218
2219/*
2220 * Common code that does the actual sending of the message by advancing the
Dean Nelson7fb5e592008-07-29 22:34:10 -07002221 * local message queue's Put value and sends a chctl msgrequest to the
2222 * partition the message is being sent to.
Dean Nelson33ba3c72008-07-29 22:34:07 -07002223 */
2224static enum xp_retval
Dean Nelson97bf1aa2008-07-29 22:34:08 -07002225xpc_send_msg_sn2(struct xpc_channel *ch, u32 flags, void *payload,
2226 u16 payload_size, u8 notify_type, xpc_notify_func func,
2227 void *key)
Dean Nelson33ba3c72008-07-29 22:34:07 -07002228{
2229 enum xp_retval ret = xpSuccess;
Dean Nelson97bf1aa2008-07-29 22:34:08 -07002230 struct xpc_msg *msg = msg;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002231 struct xpc_notify *notify = notify;
Dean Nelson97bf1aa2008-07-29 22:34:08 -07002232 s64 msg_number;
2233 s64 put;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002234
2235 DBUG_ON(notify_type == XPC_N_CALL && func == NULL);
Dean Nelson97bf1aa2008-07-29 22:34:08 -07002236
2237 if (XPC_MSG_SIZE(payload_size) > ch->msg_size)
2238 return xpPayloadTooBig;
2239
2240 xpc_msgqueue_ref(ch);
Dean Nelson33ba3c72008-07-29 22:34:07 -07002241
2242 if (ch->flags & XPC_C_DISCONNECTING) {
Dean Nelson97bf1aa2008-07-29 22:34:08 -07002243 ret = ch->reason;
2244 goto out_1;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002245 }
Dean Nelson97bf1aa2008-07-29 22:34:08 -07002246 if (!(ch->flags & XPC_C_CONNECTED)) {
2247 ret = xpNotConnected;
2248 goto out_1;
2249 }
2250
2251 ret = xpc_allocate_msg_sn2(ch, flags, &msg);
2252 if (ret != xpSuccess)
2253 goto out_1;
2254
2255 msg_number = msg->number;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002256
2257 if (notify_type != 0) {
2258 /*
2259 * Tell the remote side to send an ACK interrupt when the
2260 * message has been delivered.
2261 */
2262 msg->flags |= XPC_M_INTERRUPT;
2263
2264 atomic_inc(&ch->n_to_notify);
2265
2266 notify = &ch->notify_queue[msg_number % ch->local_nentries];
2267 notify->func = func;
2268 notify->key = key;
2269 notify->type = notify_type;
2270
Dean Nelsonea57f802008-07-29 22:34:14 -07002271 /* ??? Is a mb() needed here? */
Dean Nelson33ba3c72008-07-29 22:34:07 -07002272
2273 if (ch->flags & XPC_C_DISCONNECTING) {
2274 /*
2275 * An error occurred between our last error check and
2276 * this one. We will try to clear the type field from
2277 * the notify entry. If we succeed then
2278 * xpc_disconnect_channel() didn't already process
2279 * the notify entry.
2280 */
2281 if (cmpxchg(&notify->type, notify_type, 0) ==
2282 notify_type) {
2283 atomic_dec(&ch->n_to_notify);
2284 ret = ch->reason;
2285 }
Dean Nelson97bf1aa2008-07-29 22:34:08 -07002286 goto out_1;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002287 }
2288 }
2289
Dean Nelson97bf1aa2008-07-29 22:34:08 -07002290 memcpy(&msg->payload, payload, payload_size);
2291
Dean Nelson33ba3c72008-07-29 22:34:07 -07002292 msg->flags |= XPC_M_READY;
2293
2294 /*
2295 * The preceding store of msg->flags must occur before the following
Dean Nelsona47d5da2008-07-29 22:34:09 -07002296 * load of local_GP->put.
Dean Nelson33ba3c72008-07-29 22:34:07 -07002297 */
2298 mb();
2299
2300 /* see if the message is next in line to be sent, if so send it */
2301
Dean Nelsona47d5da2008-07-29 22:34:09 -07002302 put = ch->sn.sn2.local_GP->put;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002303 if (put == msg_number)
2304 xpc_send_msgs_sn2(ch, put);
2305
Dean Nelson97bf1aa2008-07-29 22:34:08 -07002306out_1:
Dean Nelson33ba3c72008-07-29 22:34:07 -07002307 xpc_msgqueue_deref(ch);
2308 return ret;
2309}
2310
2311/*
2312 * Now we actually acknowledge the messages that have been delivered and ack'd
2313 * by advancing the cached remote message queue's Get value and if requested
Dean Nelson7fb5e592008-07-29 22:34:10 -07002314 * send a chctl msgrequest to the message sender's partition.
Dean Nelson33ba3c72008-07-29 22:34:07 -07002315 */
2316static void
2317xpc_acknowledge_msgs_sn2(struct xpc_channel *ch, s64 initial_get, u8 msg_flags)
2318{
Dean Nelsona47d5da2008-07-29 22:34:09 -07002319 struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002320 struct xpc_msg *msg;
2321 s64 get = initial_get + 1;
Dean Nelson7fb5e592008-07-29 22:34:10 -07002322 int send_msgrequest = 0;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002323
2324 while (1) {
2325
2326 while (1) {
Dean Nelsona47d5da2008-07-29 22:34:09 -07002327 if (get == ch_sn2->w_local_GP.get)
Dean Nelson33ba3c72008-07-29 22:34:07 -07002328 break;
2329
2330 msg = (struct xpc_msg *)((u64)ch->remote_msgqueue +
2331 (get % ch->remote_nentries) *
2332 ch->msg_size);
2333
2334 if (!(msg->flags & XPC_M_DONE))
2335 break;
2336
2337 msg_flags |= msg->flags;
2338 get++;
2339 }
2340
2341 if (get == initial_get) {
2342 /* nothing's changed */
2343 break;
2344 }
2345
Dean Nelsona47d5da2008-07-29 22:34:09 -07002346 if (cmpxchg_rel(&ch_sn2->local_GP->get, initial_get, get) !=
Dean Nelson33ba3c72008-07-29 22:34:07 -07002347 initial_get) {
2348 /* someone else beat us to it */
Dean Nelsona47d5da2008-07-29 22:34:09 -07002349 DBUG_ON(ch_sn2->local_GP->get <= initial_get);
Dean Nelson33ba3c72008-07-29 22:34:07 -07002350 break;
2351 }
2352
2353 /* we just set the new value of local_GP->get */
2354
2355 dev_dbg(xpc_chan, "local_GP->get changed to %ld, partid=%d, "
2356 "channel=%d\n", get, ch->partid, ch->number);
2357
Dean Nelson7fb5e592008-07-29 22:34:10 -07002358 send_msgrequest = (msg_flags & XPC_M_INTERRUPT);
Dean Nelson33ba3c72008-07-29 22:34:07 -07002359
2360 /*
2361 * We need to ensure that the message referenced by
2362 * local_GP->get is not XPC_M_DONE or that local_GP->get
2363 * equals w_local_GP.get, so we'll go have a look.
2364 */
2365 initial_get = get;
2366 }
2367
Dean Nelson7fb5e592008-07-29 22:34:10 -07002368 if (send_msgrequest)
2369 xpc_send_chctl_msgrequest_sn2(ch);
Dean Nelson33ba3c72008-07-29 22:34:07 -07002370}
2371
2372static void
2373xpc_received_msg_sn2(struct xpc_channel *ch, struct xpc_msg *msg)
2374{
2375 s64 get;
2376 s64 msg_number = msg->number;
2377
2378 dev_dbg(xpc_chan, "msg=0x%p, msg_number=%ld, partid=%d, channel=%d\n",
2379 (void *)msg, msg_number, ch->partid, ch->number);
2380
2381 DBUG_ON((((u64)msg - (u64)ch->remote_msgqueue) / ch->msg_size) !=
2382 msg_number % ch->remote_nentries);
2383 DBUG_ON(msg->flags & XPC_M_DONE);
2384
2385 msg->flags |= XPC_M_DONE;
2386
2387 /*
2388 * The preceding store of msg->flags must occur before the following
Dean Nelsona47d5da2008-07-29 22:34:09 -07002389 * load of local_GP->get.
Dean Nelson33ba3c72008-07-29 22:34:07 -07002390 */
2391 mb();
2392
2393 /*
2394 * See if this message is next in line to be acknowledged as having
2395 * been delivered.
2396 */
Dean Nelsona47d5da2008-07-29 22:34:09 -07002397 get = ch->sn.sn2.local_GP->get;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002398 if (get == msg_number)
2399 xpc_acknowledge_msgs_sn2(ch, get, msg->flags);
2400}
2401
Dean Nelson6e410172008-07-29 22:34:09 -07002402int
Dean Nelson94bd2702008-07-29 22:34:05 -07002403xpc_init_sn2(void)
2404{
Dean Nelson6e410172008-07-29 22:34:09 -07002405 int ret;
Dean Nelsonee6665e2008-07-29 22:34:13 -07002406 size_t buf_size;
Dean Nelson6e410172008-07-29 22:34:09 -07002407
Dean Nelson261f3b42008-07-29 22:34:16 -07002408 xpc_get_partition_rsvd_page_pa = xpc_get_partition_rsvd_page_pa_sn2;
Dean Nelson94bd2702008-07-29 22:34:05 -07002409 xpc_rsvd_page_init = xpc_rsvd_page_init_sn2;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002410 xpc_increment_heartbeat = xpc_increment_heartbeat_sn2;
2411 xpc_offline_heartbeat = xpc_offline_heartbeat_sn2;
2412 xpc_online_heartbeat = xpc_online_heartbeat_sn2;
2413 xpc_heartbeat_init = xpc_heartbeat_init_sn2;
2414 xpc_heartbeat_exit = xpc_heartbeat_exit_sn2;
2415 xpc_check_remote_hb = xpc_check_remote_hb_sn2;
2416
Dean Nelsona47d5da2008-07-29 22:34:09 -07002417 xpc_request_partition_activation = xpc_request_partition_activation_sn2;
2418 xpc_request_partition_reactivation =
2419 xpc_request_partition_reactivation_sn2;
2420 xpc_request_partition_deactivation =
2421 xpc_request_partition_deactivation_sn2;
2422 xpc_cancel_partition_deactivation_request =
2423 xpc_cancel_partition_deactivation_request_sn2;
2424
Dean Nelson6e410172008-07-29 22:34:09 -07002425 xpc_process_activate_IRQ_rcvd = xpc_process_activate_IRQ_rcvd_sn2;
Dean Nelsone17d4162008-07-29 22:34:06 -07002426 xpc_setup_infrastructure = xpc_setup_infrastructure_sn2;
2427 xpc_teardown_infrastructure = xpc_teardown_infrastructure_sn2;
2428 xpc_make_first_contact = xpc_make_first_contact_sn2;
Dean Nelson7fb5e592008-07-29 22:34:10 -07002429 xpc_get_chctl_all_flags = xpc_get_chctl_all_flags_sn2;
Dean Nelson185c3a12008-07-29 22:34:11 -07002430 xpc_allocate_msgqueues = xpc_allocate_msgqueues_sn2;
2431 xpc_free_msgqueues = xpc_free_msgqueues_sn2;
Dean Nelsona47d5da2008-07-29 22:34:09 -07002432 xpc_notify_senders_of_disconnect = xpc_notify_senders_of_disconnect_sn2;
Dean Nelson7fb5e592008-07-29 22:34:10 -07002433 xpc_process_msg_chctl_flags = xpc_process_msg_chctl_flags_sn2;
Dean Nelsona47d5da2008-07-29 22:34:09 -07002434 xpc_n_of_deliverable_msgs = xpc_n_of_deliverable_msgs_sn2;
Dean Nelsone17d4162008-07-29 22:34:06 -07002435 xpc_get_deliverable_msg = xpc_get_deliverable_msg_sn2;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002436
Dean Nelsona47d5da2008-07-29 22:34:09 -07002437 xpc_indicate_partition_engaged = xpc_indicate_partition_engaged_sn2;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002438 xpc_partition_engaged = xpc_partition_engaged_sn2;
Dean Nelsona47d5da2008-07-29 22:34:09 -07002439 xpc_any_partition_engaged = xpc_any_partition_engaged_sn2;
2440 xpc_indicate_partition_disengaged =
2441 xpc_indicate_partition_disengaged_sn2;
2442 xpc_assume_partition_disengaged = xpc_assume_partition_disengaged_sn2;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002443
Dean Nelson7fb5e592008-07-29 22:34:10 -07002444 xpc_send_chctl_closerequest = xpc_send_chctl_closerequest_sn2;
2445 xpc_send_chctl_closereply = xpc_send_chctl_closereply_sn2;
2446 xpc_send_chctl_openrequest = xpc_send_chctl_openrequest_sn2;
2447 xpc_send_chctl_openreply = xpc_send_chctl_openreply_sn2;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002448
Dean Nelson33ba3c72008-07-29 22:34:07 -07002449 xpc_send_msg = xpc_send_msg_sn2;
2450 xpc_received_msg = xpc_received_msg_sn2;
Dean Nelson6e410172008-07-29 22:34:09 -07002451
Dean Nelsonee6665e2008-07-29 22:34:13 -07002452 buf_size = max(XPC_RP_VARS_SIZE,
2453 XPC_RP_HEADER_SIZE + XP_NASID_MASK_BYTES_SN2);
2454 xpc_remote_copy_buffer_sn2 = xpc_kmalloc_cacheline_aligned(buf_size,
2455 GFP_KERNEL,
2456 &xpc_remote_copy_buffer_base_sn2);
2457 if (xpc_remote_copy_buffer_sn2 == NULL) {
2458 dev_err(xpc_part, "can't get memory for remote copy buffer\n");
2459 return -ENOMEM;
2460 }
2461
Dean Nelsonc39838c2008-07-29 22:34:11 -07002462 /* open up protections for IPI and [potentially] amo operations */
Dean Nelson6e410172008-07-29 22:34:09 -07002463 xpc_allow_IPI_ops_sn2();
Dean Nelsonc39838c2008-07-29 22:34:11 -07002464 xpc_allow_amo_ops_shub_wars_1_1_sn2();
Dean Nelson6e410172008-07-29 22:34:09 -07002465
2466 /*
2467 * This is safe to do before the xpc_hb_checker thread has started
2468 * because the handler releases a wait queue. If an interrupt is
2469 * received before the thread is waiting, it will not go to sleep,
2470 * but rather immediately process the interrupt.
2471 */
2472 ret = request_irq(SGI_XPC_ACTIVATE, xpc_handle_activate_IRQ_sn2, 0,
2473 "xpc hb", NULL);
2474 if (ret != 0) {
2475 dev_err(xpc_part, "can't register ACTIVATE IRQ handler, "
2476 "errno=%d\n", -ret);
2477 xpc_disallow_IPI_ops_sn2();
Dean Nelsonee6665e2008-07-29 22:34:13 -07002478 kfree(xpc_remote_copy_buffer_base_sn2);
Dean Nelson6e410172008-07-29 22:34:09 -07002479 }
2480 return ret;
Dean Nelson94bd2702008-07-29 22:34:05 -07002481}
2482
2483void
2484xpc_exit_sn2(void)
2485{
Dean Nelson6e410172008-07-29 22:34:09 -07002486 free_irq(SGI_XPC_ACTIVATE, NULL);
2487 xpc_disallow_IPI_ops_sn2();
Dean Nelsonee6665e2008-07-29 22:34:13 -07002488 kfree(xpc_remote_copy_buffer_base_sn2);
Dean Nelson94bd2702008-07-29 22:34:05 -07002489}