blob: 9c0c29a2ac867dfe850c6195889ccce74d1f29e8 [file] [log] [blame]
Dean Nelson94bd2702008-07-29 22:34:05 -07001/*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
6 * Copyright (c) 2008 Silicon Graphics, Inc. All Rights Reserved.
7 */
8
9/*
10 * Cross Partition Communication (XPC) sn2-based functions.
11 *
12 * Architecture specific implementation of common functions.
13 *
14 */
15
16#include <linux/kernel.h>
Dean Nelsone17d4162008-07-29 22:34:06 -070017#include <linux/delay.h>
Dean Nelson94bd2702008-07-29 22:34:05 -070018#include <asm/uncached.h>
19#include <asm/sn/sn_sal.h>
20#include "xpc.h"
21
Dean 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 Nelson33ba3c72008-07-29 22:34:07 -070059static struct xpc_vars_sn2 *xpc_vars; /* >>> Add _sn2 suffix? */
Dean Nelsone17d4162008-07-29 22:34:06 -070060static struct xpc_vars_part_sn2 *xpc_vars_part; /* >>> Add _sn2 suffix? */
Dean Nelson94bd2702008-07-29 22:34:05 -070061
Dean Nelson6e410172008-07-29 22:34:09 -070062/* SH_IPI_ACCESS shub register value on startup */
63static u64 xpc_sh1_IPI_access;
64static u64 xpc_sh2_IPI_access0;
65static u64 xpc_sh2_IPI_access1;
66static u64 xpc_sh2_IPI_access2;
67static u64 xpc_sh2_IPI_access3;
68
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
78 /* >>> The following should get moved into SAL. */
79 if (is_shub2()) {
80 xpc_sh2_IPI_access0 =
81 (u64)HUB_L((u64 *)LOCAL_MMR_ADDR(SH2_IPI_ACCESS0));
82 xpc_sh2_IPI_access1 =
83 (u64)HUB_L((u64 *)LOCAL_MMR_ADDR(SH2_IPI_ACCESS1));
84 xpc_sh2_IPI_access2 =
85 (u64)HUB_L((u64 *)LOCAL_MMR_ADDR(SH2_IPI_ACCESS2));
86 xpc_sh2_IPI_access3 =
87 (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 {
101 xpc_sh1_IPI_access =
102 (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
121 /* >>> The following should get moved into SAL. */
122 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),
126 xpc_sh2_IPI_access0);
127 HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS1),
128 xpc_sh2_IPI_access1);
129 HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS2),
130 xpc_sh2_IPI_access2);
131 HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS3),
132 xpc_sh2_IPI_access3);
133 }
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),
138 xpc_sh1_IPI_access);
139 }
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
179 return ((ret == 0) ? xpSuccess : xpPioReadError);
180}
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 Nelsonc39838c2008-07-29 22:34:11 -0700185 struct amo *amo = xpc_vars->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{
213 int w_index = XPC_NASID_W_INDEX(from_nasid);
214 int b_index = XPC_NASID_B_INDEX(from_nasid);
Dean Nelsonc39838c2008-07-29 22:34:11 -0700215 struct amo *amos = (struct amo *)__va(amos_page_pa +
Dean Nelsonee6665e2008-07-29 22:34:13 -0700216 (XPC_ACTIVATE_IRQ_AMOS_SN2 *
Dean Nelsonc39838c2008-07-29 22:34:11 -0700217 sizeof(struct amo)));
Dean Nelson33ba3c72008-07-29 22:34:07 -0700218
Dean Nelson7fb5e592008-07-29 22:34:10 -0700219 (void)xpc_send_IRQ_sn2(&amos[w_index], (1UL << b_index), to_nasid,
Dean Nelson33ba3c72008-07-29 22:34:07 -0700220 to_phys_cpuid, SGI_XPC_ACTIVATE);
221}
222
223static void
Dean Nelson7fb5e592008-07-29 22:34:10 -0700224xpc_send_local_activate_IRQ_sn2(int from_nasid)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700225{
226 int w_index = XPC_NASID_W_INDEX(from_nasid);
227 int b_index = XPC_NASID_B_INDEX(from_nasid);
Dean Nelsonc39838c2008-07-29 22:34:11 -0700228 struct amo *amos = (struct amo *)__va(xpc_vars->amos_page_pa +
Dean Nelsonee6665e2008-07-29 22:34:13 -0700229 (XPC_ACTIVATE_IRQ_AMOS_SN2 *
Dean Nelsonc39838c2008-07-29 22:34:11 -0700230 sizeof(struct amo)));
Dean Nelson33ba3c72008-07-29 22:34:07 -0700231
232 /* fake the sending and receipt of an activate IRQ from remote nasid */
233 FETCHOP_STORE_OP(TO_AMO((u64)&amos[w_index].variable), FETCHOP_OR,
234 (1UL << b_index));
Dean Nelson6e410172008-07-29 22:34:09 -0700235 atomic_inc(&xpc_activate_IRQ_rcvd);
236 wake_up_interruptible(&xpc_activate_IRQ_wq);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700237}
238
Dean Nelson33ba3c72008-07-29 22:34:07 -0700239/*
Dean Nelson7fb5e592008-07-29 22:34:10 -0700240 * Functions associated with SGI_XPC_NOTIFY IRQ.
Dean Nelson33ba3c72008-07-29 22:34:07 -0700241 */
242
243/*
Dean Nelson7fb5e592008-07-29 22:34:10 -0700244 * Check to see if any chctl flags were sent from the specified partition.
Dean Nelsona47d5da2008-07-29 22:34:09 -0700245 */
246static void
Dean Nelson7fb5e592008-07-29 22:34:10 -0700247xpc_check_for_sent_chctl_flags_sn2(struct xpc_partition *part)
Dean Nelsona47d5da2008-07-29 22:34:09 -0700248{
Dean Nelson7fb5e592008-07-29 22:34:10 -0700249 union xpc_channel_ctl_flags chctl;
Dean Nelsona47d5da2008-07-29 22:34:09 -0700250 unsigned long irq_flags;
251
Dean Nelson7fb5e592008-07-29 22:34:10 -0700252 chctl.all_flags = xpc_receive_IRQ_amo_sn2(part->sn.sn2.
253 local_chctl_amo_va);
254 if (chctl.all_flags == 0)
Dean Nelsona47d5da2008-07-29 22:34:09 -0700255 return;
256
Dean Nelson7fb5e592008-07-29 22:34:10 -0700257 spin_lock_irqsave(&part->chctl_lock, irq_flags);
258 part->chctl.all_flags |= chctl.all_flags;
259 spin_unlock_irqrestore(&part->chctl_lock, irq_flags);
Dean Nelsona47d5da2008-07-29 22:34:09 -0700260
Dean Nelson7fb5e592008-07-29 22:34:10 -0700261 dev_dbg(xpc_chan, "received notify IRQ from partid=%d, chctl.all_flags="
262 "0x%lx\n", XPC_PARTID(part), chctl.all_flags);
Dean Nelsona47d5da2008-07-29 22:34:09 -0700263
264 xpc_wakeup_channel_mgr(part);
265}
266
267/*
268 * Handle the receipt of a SGI_XPC_NOTIFY IRQ by seeing whether the specified
269 * partition actually sent it. Since SGI_XPC_NOTIFY IRQs may be shared by more
Dean Nelsonc39838c2008-07-29 22:34:11 -0700270 * than one partition, we use an amo structure per partition to indicate
Dean Nelson7fb5e592008-07-29 22:34:10 -0700271 * whether a partition has sent an IRQ or not. If it has, then wake up the
Dean Nelsona47d5da2008-07-29 22:34:09 -0700272 * associated kthread to handle it.
273 *
Dean Nelson7fb5e592008-07-29 22:34:10 -0700274 * All SGI_XPC_NOTIFY IRQs received by XPC are the result of IRQs sent by XPC
Dean Nelsona47d5da2008-07-29 22:34:09 -0700275 * running on other partitions.
276 *
277 * Noteworthy Arguments:
278 *
279 * irq - Interrupt ReQuest number. NOT USED.
280 *
Dean Nelson7fb5e592008-07-29 22:34:10 -0700281 * dev_id - partid of IRQ's potential sender.
Dean Nelsona47d5da2008-07-29 22:34:09 -0700282 */
283static irqreturn_t
284xpc_handle_notify_IRQ_sn2(int irq, void *dev_id)
285{
286 short partid = (short)(u64)dev_id;
287 struct xpc_partition *part = &xpc_partitions[partid];
288
289 DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
290
291 if (xpc_part_ref(part)) {
Dean Nelson7fb5e592008-07-29 22:34:10 -0700292 xpc_check_for_sent_chctl_flags_sn2(part);
Dean Nelsona47d5da2008-07-29 22:34:09 -0700293
294 xpc_part_deref(part);
295 }
296 return IRQ_HANDLED;
297}
298
299/*
Dean Nelson7fb5e592008-07-29 22:34:10 -0700300 * Check to see if xpc_handle_notify_IRQ_sn2() dropped any IRQs on the floor
301 * because the write to their associated amo variable completed after the IRQ
Dean Nelsona47d5da2008-07-29 22:34:09 -0700302 * was received.
303 */
304static void
Dean Nelson7fb5e592008-07-29 22:34:10 -0700305xpc_check_for_dropped_notify_IRQ_sn2(struct xpc_partition *part)
Dean Nelsona47d5da2008-07-29 22:34:09 -0700306{
307 struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
308
309 if (xpc_part_ref(part)) {
Dean Nelson7fb5e592008-07-29 22:34:10 -0700310 xpc_check_for_sent_chctl_flags_sn2(part);
Dean Nelsona47d5da2008-07-29 22:34:09 -0700311
312 part_sn2->dropped_notify_IRQ_timer.expires = jiffies +
Dean Nelson7fb5e592008-07-29 22:34:10 -0700313 XPC_DROPPED_NOTIFY_IRQ_WAIT_INTERVAL;
Dean Nelsona47d5da2008-07-29 22:34:09 -0700314 add_timer(&part_sn2->dropped_notify_IRQ_timer);
315 xpc_part_deref(part);
316 }
317}
318
319/*
Dean Nelson7fb5e592008-07-29 22:34:10 -0700320 * Send a notify IRQ to the remote partition that is associated with the
Dean Nelson33ba3c72008-07-29 22:34:07 -0700321 * specified channel.
322 */
323static void
Dean Nelson7fb5e592008-07-29 22:34:10 -0700324xpc_send_notify_IRQ_sn2(struct xpc_channel *ch, u8 chctl_flag,
325 char *chctl_flag_string, unsigned long *irq_flags)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700326{
327 struct xpc_partition *part = &xpc_partitions[ch->partid];
Dean Nelsona47d5da2008-07-29 22:34:09 -0700328 struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
Dean Nelson7fb5e592008-07-29 22:34:10 -0700329 union xpc_channel_ctl_flags chctl = { 0 };
Dean Nelson33ba3c72008-07-29 22:34:07 -0700330 enum xp_retval ret;
331
332 if (likely(part->act_state != XPC_P_DEACTIVATING)) {
Dean Nelson7fb5e592008-07-29 22:34:10 -0700333 chctl.flags[ch->number] = chctl_flag;
334 ret = xpc_send_IRQ_sn2(part_sn2->remote_chctl_amo_va,
335 chctl.all_flags,
336 part_sn2->notify_IRQ_nasid,
337 part_sn2->notify_IRQ_phys_cpuid,
Dean Nelson33ba3c72008-07-29 22:34:07 -0700338 SGI_XPC_NOTIFY);
339 dev_dbg(xpc_chan, "%s sent to partid=%d, channel=%d, ret=%d\n",
Dean Nelson7fb5e592008-07-29 22:34:10 -0700340 chctl_flag_string, ch->partid, ch->number, ret);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700341 if (unlikely(ret != xpSuccess)) {
342 if (irq_flags != NULL)
343 spin_unlock_irqrestore(&ch->lock, *irq_flags);
344 XPC_DEACTIVATE_PARTITION(part, ret);
345 if (irq_flags != NULL)
346 spin_lock_irqsave(&ch->lock, *irq_flags);
347 }
348 }
349}
350
Dean Nelson7fb5e592008-07-29 22:34:10 -0700351#define XPC_SEND_NOTIFY_IRQ_SN2(_ch, _ipi_f, _irq_f) \
352 xpc_send_notify_IRQ_sn2(_ch, _ipi_f, #_ipi_f, _irq_f)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700353
354/*
355 * Make it look like the remote partition, which is associated with the
Dean Nelson7fb5e592008-07-29 22:34:10 -0700356 * specified channel, sent us a notify IRQ. This faked IRQ will be handled
357 * by xpc_check_for_dropped_notify_IRQ_sn2().
Dean Nelson33ba3c72008-07-29 22:34:07 -0700358 */
359static void
Dean Nelson7fb5e592008-07-29 22:34:10 -0700360xpc_send_local_notify_IRQ_sn2(struct xpc_channel *ch, u8 chctl_flag,
361 char *chctl_flag_string)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700362{
363 struct xpc_partition *part = &xpc_partitions[ch->partid];
Dean Nelson7fb5e592008-07-29 22:34:10 -0700364 union xpc_channel_ctl_flags chctl = { 0 };
Dean Nelson33ba3c72008-07-29 22:34:07 -0700365
Dean Nelson7fb5e592008-07-29 22:34:10 -0700366 chctl.flags[ch->number] = chctl_flag;
367 FETCHOP_STORE_OP(TO_AMO((u64)&part->sn.sn2.local_chctl_amo_va->
368 variable), FETCHOP_OR, chctl.all_flags);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700369 dev_dbg(xpc_chan, "%s sent local from partid=%d, channel=%d\n",
Dean Nelson7fb5e592008-07-29 22:34:10 -0700370 chctl_flag_string, ch->partid, ch->number);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700371}
372
Dean Nelson7fb5e592008-07-29 22:34:10 -0700373#define XPC_SEND_LOCAL_NOTIFY_IRQ_SN2(_ch, _ipi_f) \
374 xpc_send_local_notify_IRQ_sn2(_ch, _ipi_f, #_ipi_f)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700375
376static void
Dean Nelson7fb5e592008-07-29 22:34:10 -0700377xpc_send_chctl_closerequest_sn2(struct xpc_channel *ch,
378 unsigned long *irq_flags)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700379{
380 struct xpc_openclose_args *args = ch->local_openclose_args;
381
382 args->reason = ch->reason;
Dean Nelson7fb5e592008-07-29 22:34:10 -0700383 XPC_SEND_NOTIFY_IRQ_SN2(ch, XPC_CHCTL_CLOSEREQUEST, irq_flags);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700384}
385
386static void
Dean Nelson7fb5e592008-07-29 22:34:10 -0700387xpc_send_chctl_closereply_sn2(struct xpc_channel *ch, unsigned long *irq_flags)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700388{
Dean Nelson7fb5e592008-07-29 22:34:10 -0700389 XPC_SEND_NOTIFY_IRQ_SN2(ch, XPC_CHCTL_CLOSEREPLY, irq_flags);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700390}
391
392static void
Dean Nelson7fb5e592008-07-29 22:34:10 -0700393xpc_send_chctl_openrequest_sn2(struct xpc_channel *ch, unsigned long *irq_flags)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700394{
395 struct xpc_openclose_args *args = ch->local_openclose_args;
396
397 args->msg_size = ch->msg_size;
398 args->local_nentries = ch->local_nentries;
Dean Nelson7fb5e592008-07-29 22:34:10 -0700399 XPC_SEND_NOTIFY_IRQ_SN2(ch, XPC_CHCTL_OPENREQUEST, irq_flags);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700400}
401
402static void
Dean Nelson7fb5e592008-07-29 22:34:10 -0700403xpc_send_chctl_openreply_sn2(struct xpc_channel *ch, unsigned long *irq_flags)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700404{
405 struct xpc_openclose_args *args = ch->local_openclose_args;
406
407 args->remote_nentries = ch->remote_nentries;
408 args->local_nentries = ch->local_nentries;
409 args->local_msgqueue_pa = __pa(ch->local_msgqueue);
Dean Nelson7fb5e592008-07-29 22:34:10 -0700410 XPC_SEND_NOTIFY_IRQ_SN2(ch, XPC_CHCTL_OPENREPLY, irq_flags);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700411}
412
413static void
Dean Nelson7fb5e592008-07-29 22:34:10 -0700414xpc_send_chctl_msgrequest_sn2(struct xpc_channel *ch)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700415{
Dean Nelson7fb5e592008-07-29 22:34:10 -0700416 XPC_SEND_NOTIFY_IRQ_SN2(ch, XPC_CHCTL_MSGREQUEST, NULL);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700417}
418
419static void
Dean Nelson7fb5e592008-07-29 22:34:10 -0700420xpc_send_chctl_local_msgrequest_sn2(struct xpc_channel *ch)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700421{
Dean Nelson7fb5e592008-07-29 22:34:10 -0700422 XPC_SEND_LOCAL_NOTIFY_IRQ_SN2(ch, XPC_CHCTL_MSGREQUEST);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700423}
424
425/*
426 * This next set of functions are used to keep track of when a partition is
427 * potentially engaged in accessing memory belonging to another partition.
428 */
429
430static void
Dean Nelsona47d5da2008-07-29 22:34:09 -0700431xpc_indicate_partition_engaged_sn2(struct xpc_partition *part)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700432{
433 unsigned long irq_flags;
Dean Nelsonc39838c2008-07-29 22:34:11 -0700434 struct amo *amo = (struct amo *)__va(part->sn.sn2.remote_amos_page_pa +
Dean Nelsonee6665e2008-07-29 22:34:13 -0700435 (XPC_ENGAGED_PARTITIONS_AMO_SN2 *
Dean Nelsonc39838c2008-07-29 22:34:11 -0700436 sizeof(struct amo)));
Dean Nelson33ba3c72008-07-29 22:34:07 -0700437
438 local_irq_save(irq_flags);
439
Dean Nelsonc39838c2008-07-29 22:34:11 -0700440 /* set bit corresponding to our partid in remote partition's amo */
Dean Nelson33ba3c72008-07-29 22:34:07 -0700441 FETCHOP_STORE_OP(TO_AMO((u64)&amo->variable), FETCHOP_OR,
442 (1UL << sn_partition_id));
443 /*
444 * We must always use the nofault function regardless of whether we
445 * are on a Shub 1.1 system or a Shub 1.2 slice 0xc processor. If we
446 * didn't, we'd never know that the other partition is down and would
Dean Nelsonc39838c2008-07-29 22:34:11 -0700447 * keep sending IRQs and amos to it until the heartbeat times out.
Dean Nelson33ba3c72008-07-29 22:34:07 -0700448 */
449 (void)xp_nofault_PIOR((u64 *)GLOBAL_MMR_ADDR(NASID_GET(&amo->
450 variable),
451 xp_nofault_PIOR_target));
452
453 local_irq_restore(irq_flags);
454}
455
456static void
Dean Nelsona47d5da2008-07-29 22:34:09 -0700457xpc_indicate_partition_disengaged_sn2(struct xpc_partition *part)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700458{
Dean Nelsona47d5da2008-07-29 22:34:09 -0700459 struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700460 unsigned long irq_flags;
Dean Nelsonc39838c2008-07-29 22:34:11 -0700461 struct amo *amo = (struct amo *)__va(part_sn2->remote_amos_page_pa +
Dean Nelsonee6665e2008-07-29 22:34:13 -0700462 (XPC_ENGAGED_PARTITIONS_AMO_SN2 *
Dean Nelsonc39838c2008-07-29 22:34:11 -0700463 sizeof(struct amo)));
Dean Nelson33ba3c72008-07-29 22:34:07 -0700464
465 local_irq_save(irq_flags);
466
Dean Nelsonc39838c2008-07-29 22:34:11 -0700467 /* clear bit corresponding to our partid in remote partition's amo */
Dean Nelson33ba3c72008-07-29 22:34:07 -0700468 FETCHOP_STORE_OP(TO_AMO((u64)&amo->variable), FETCHOP_AND,
469 ~(1UL << sn_partition_id));
470 /*
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 Nelsonee6665e2008-07-29 22:34:13 -0700495 struct amo *amo = xpc_vars->amos_page + XPC_ENGAGED_PARTITIONS_AMO_SN2;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700496
Dean Nelsonc39838c2008-07-29 22:34:11 -0700497 /* our partition's amo variable ANDed with partid mask */
Dean Nelson33ba3c72008-07-29 22:34:07 -0700498 return (FETCHOP_LOAD_OP(TO_AMO((u64)&amo->variable), FETCHOP_LOAD) &
Dean Nelsona47d5da2008-07-29 22:34:09 -0700499 (1UL << partid)) != 0;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700500}
501
Dean Nelsona47d5da2008-07-29 22:34:09 -0700502static int
503xpc_any_partition_engaged_sn2(void)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700504{
Dean Nelsonee6665e2008-07-29 22:34:13 -0700505 struct amo *amo = xpc_vars->amos_page + XPC_ENGAGED_PARTITIONS_AMO_SN2;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700506
Dean Nelsonc39838c2008-07-29 22:34:11 -0700507 /* our partition's amo variable */
Dean Nelsona47d5da2008-07-29 22:34:09 -0700508 return FETCHOP_LOAD_OP(TO_AMO((u64)&amo->variable), FETCHOP_LOAD) != 0;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700509}
510
511static void
Dean Nelsona47d5da2008-07-29 22:34:09 -0700512xpc_assume_partition_disengaged_sn2(short partid)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700513{
Dean Nelsonee6665e2008-07-29 22:34:13 -0700514 struct amo *amo = xpc_vars->amos_page + XPC_ENGAGED_PARTITIONS_AMO_SN2;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700515
Dean Nelsonc39838c2008-07-29 22:34:11 -0700516 /* clear bit(s) based on partid mask in our partition's amo */
Dean Nelson33ba3c72008-07-29 22:34:07 -0700517 FETCHOP_STORE_OP(TO_AMO((u64)&amo->variable), FETCHOP_AND,
Dean Nelsona47d5da2008-07-29 22:34:09 -0700518 ~(1UL << partid));
Dean Nelson33ba3c72008-07-29 22:34:07 -0700519}
520
Dean Nelson6e410172008-07-29 22:34:09 -0700521/* original protection values for each node */
522static u64 xpc_prot_vec_sn2[MAX_NUMNODES];
523
524/*
Dean Nelsonc39838c2008-07-29 22:34:11 -0700525 * Change protections to allow amo operations on non-Shub 1.1 systems.
Dean Nelson6e410172008-07-29 22:34:09 -0700526 */
527static enum xp_retval
Dean Nelsonc39838c2008-07-29 22:34:11 -0700528xpc_allow_amo_ops_sn2(struct amo *amos_page)
Dean Nelson6e410172008-07-29 22:34:09 -0700529{
530 u64 nasid_array = 0;
531 int ret;
532
533 /*
534 * On SHUB 1.1, we cannot call sn_change_memprotect() since the BIST
535 * collides with memory operations. On those systems we call
Dean Nelsonc39838c2008-07-29 22:34:11 -0700536 * xpc_allow_amo_ops_shub_wars_1_1_sn2() instead.
Dean Nelson6e410172008-07-29 22:34:09 -0700537 */
538 if (!enable_shub_wars_1_1()) {
539 ret = sn_change_memprotect(ia64_tpa((u64)amos_page), PAGE_SIZE,
540 SN_MEMPROT_ACCESS_CLASS_1,
541 &nasid_array);
542 if (ret != 0)
543 return xpSalError;
544 }
545 return xpSuccess;
546}
547
548/*
Dean Nelsonc39838c2008-07-29 22:34:11 -0700549 * Change protections to allow amo operations on Shub 1.1 systems.
Dean Nelson6e410172008-07-29 22:34:09 -0700550 */
551static void
Dean Nelsonc39838c2008-07-29 22:34:11 -0700552xpc_allow_amo_ops_shub_wars_1_1_sn2(void)
Dean Nelson6e410172008-07-29 22:34:09 -0700553{
554 int node;
555 int nasid;
556
557 if (!enable_shub_wars_1_1())
558 return;
559
560 for_each_online_node(node) {
561 nasid = cnodeid_to_nasid(node);
562 /* save current protection values */
563 xpc_prot_vec_sn2[node] =
564 (u64)HUB_L((u64 *)GLOBAL_MMR_ADDR(nasid,
565 SH1_MD_DQLP_MMR_DIR_PRIVEC0));
566 /* open up everything */
567 HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid,
568 SH1_MD_DQLP_MMR_DIR_PRIVEC0),
569 -1UL);
570 HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid,
571 SH1_MD_DQRP_MMR_DIR_PRIVEC0),
572 -1UL);
573 }
574}
575
Dean Nelson94bd2702008-07-29 22:34:05 -0700576static enum xp_retval
577xpc_rsvd_page_init_sn2(struct xpc_rsvd_page *rp)
578{
Dean Nelsonc39838c2008-07-29 22:34:11 -0700579 struct amo *amos_page;
Dean Nelson94bd2702008-07-29 22:34:05 -0700580 int i;
581 int ret;
582
583 xpc_vars = XPC_RP_VARS(rp);
584
585 rp->sn.vars_pa = __pa(xpc_vars);
586
Dean Nelsone17d4162008-07-29 22:34:06 -0700587 /* vars_part array follows immediately after vars */
588 xpc_vars_part = (struct xpc_vars_part_sn2 *)((u8 *)XPC_RP_VARS(rp) +
589 XPC_RP_VARS_SIZE);
590
Dean Nelson94bd2702008-07-29 22:34:05 -0700591 /*
Dean Nelsonc39838c2008-07-29 22:34:11 -0700592 * Before clearing xpc_vars, see if a page of amos had been previously
Dean Nelson94bd2702008-07-29 22:34:05 -0700593 * allocated. If not we'll need to allocate one and set permissions
Dean Nelsonc39838c2008-07-29 22:34:11 -0700594 * so that cross-partition amos are allowed.
Dean Nelson94bd2702008-07-29 22:34:05 -0700595 *
Dean Nelsonc39838c2008-07-29 22:34:11 -0700596 * The allocated amo page needs MCA reporting to remain disabled after
Dean Nelson94bd2702008-07-29 22:34:05 -0700597 * XPC has unloaded. To make this work, we keep a copy of the pointer
598 * to this page (i.e., amos_page) in the struct xpc_vars structure,
599 * which is pointed to by the reserved page, and re-use that saved copy
Dean Nelsonc39838c2008-07-29 22:34:11 -0700600 * on subsequent loads of XPC. This amo page is never freed, and its
Dean Nelson94bd2702008-07-29 22:34:05 -0700601 * memory protections are never restricted.
602 */
603 amos_page = xpc_vars->amos_page;
604 if (amos_page == NULL) {
Dean Nelsonc39838c2008-07-29 22:34:11 -0700605 amos_page = (struct amo *)TO_AMO(uncached_alloc_page(0, 1));
Dean Nelson94bd2702008-07-29 22:34:05 -0700606 if (amos_page == NULL) {
Dean Nelsonc39838c2008-07-29 22:34:11 -0700607 dev_err(xpc_part, "can't allocate page of amos\n");
Dean Nelson94bd2702008-07-29 22:34:05 -0700608 return xpNoMemory;
609 }
610
611 /*
Dean Nelsonc39838c2008-07-29 22:34:11 -0700612 * Open up amo-R/W to cpu. This is done on Shub 1.1 systems
613 * when xpc_allow_amo_ops_shub_wars_1_1_sn2() is called.
Dean Nelson94bd2702008-07-29 22:34:05 -0700614 */
Dean Nelsonc39838c2008-07-29 22:34:11 -0700615 ret = xpc_allow_amo_ops_sn2(amos_page);
Dean Nelson6e410172008-07-29 22:34:09 -0700616 if (ret != xpSuccess) {
Dean Nelsonc39838c2008-07-29 22:34:11 -0700617 dev_err(xpc_part, "can't allow amo operations\n");
Dean Nelson6e410172008-07-29 22:34:09 -0700618 uncached_free_page(__IA64_UNCACHED_OFFSET |
619 TO_PHYS((u64)amos_page), 1);
620 return ret;
Dean Nelson94bd2702008-07-29 22:34:05 -0700621 }
622 }
623
624 /* clear xpc_vars */
Dean Nelson33ba3c72008-07-29 22:34:07 -0700625 memset(xpc_vars, 0, sizeof(struct xpc_vars_sn2));
Dean Nelson94bd2702008-07-29 22:34:05 -0700626
627 xpc_vars->version = XPC_V_VERSION;
Dean Nelsona47d5da2008-07-29 22:34:09 -0700628 xpc_vars->activate_IRQ_nasid = cpuid_to_nasid(0);
629 xpc_vars->activate_IRQ_phys_cpuid = cpu_physical_id(0);
Dean Nelson94bd2702008-07-29 22:34:05 -0700630 xpc_vars->vars_part_pa = __pa(xpc_vars_part);
631 xpc_vars->amos_page_pa = ia64_tpa((u64)amos_page);
632 xpc_vars->amos_page = amos_page; /* save for next load of XPC */
633
634 /* clear xpc_vars_part */
Dean Nelsone17d4162008-07-29 22:34:06 -0700635 memset((u64 *)xpc_vars_part, 0, sizeof(struct xpc_vars_part_sn2) *
Dean Nelson94bd2702008-07-29 22:34:05 -0700636 xp_max_npartitions);
637
Dean Nelsonc39838c2008-07-29 22:34:11 -0700638 /* initialize the activate IRQ related amo variables */
Dean Nelsonee6665e2008-07-29 22:34:13 -0700639 for (i = 0; i < xpc_nasid_mask_words; i++)
640 (void)xpc_init_IRQ_amo_sn2(XPC_ACTIVATE_IRQ_AMOS_SN2 + i);
Dean Nelson94bd2702008-07-29 22:34:05 -0700641
Dean Nelsonc39838c2008-07-29 22:34:11 -0700642 /* initialize the engaged remote partitions related amo variables */
Dean Nelsonee6665e2008-07-29 22:34:13 -0700643 (void)xpc_init_IRQ_amo_sn2(XPC_ENGAGED_PARTITIONS_AMO_SN2);
644 (void)xpc_init_IRQ_amo_sn2(XPC_DEACTIVATE_REQUEST_AMO_SN2);
Dean Nelson94bd2702008-07-29 22:34:05 -0700645
646 return xpSuccess;
647}
648
Dean Nelson33ba3c72008-07-29 22:34:07 -0700649static void
650xpc_increment_heartbeat_sn2(void)
651{
652 xpc_vars->heartbeat++;
653}
654
655static void
656xpc_offline_heartbeat_sn2(void)
657{
658 xpc_increment_heartbeat_sn2();
659 xpc_vars->heartbeat_offline = 1;
660}
661
662static void
663xpc_online_heartbeat_sn2(void)
664{
665 xpc_increment_heartbeat_sn2();
666 xpc_vars->heartbeat_offline = 0;
667}
668
669static void
670xpc_heartbeat_init_sn2(void)
671{
672 DBUG_ON(xpc_vars == NULL);
673
674 bitmap_zero(xpc_vars->heartbeating_to_mask, XP_MAX_NPARTITIONS_SN2);
675 xpc_heartbeating_to_mask = &xpc_vars->heartbeating_to_mask[0];
676 xpc_online_heartbeat_sn2();
677}
678
679static void
680xpc_heartbeat_exit_sn2(void)
681{
682 xpc_offline_heartbeat_sn2();
683}
684
685/*
686 * At periodic intervals, scan through all active partitions and ensure
687 * their heartbeat is still active. If not, the partition is deactivated.
688 */
689static void
690xpc_check_remote_hb_sn2(void)
691{
692 struct xpc_vars_sn2 *remote_vars;
693 struct xpc_partition *part;
694 short partid;
695 enum xp_retval ret;
696
Dean Nelsonee6665e2008-07-29 22:34:13 -0700697 remote_vars = (struct xpc_vars_sn2 *)xpc_remote_copy_buffer_sn2;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700698
699 for (partid = 0; partid < xp_max_npartitions; partid++) {
700
701 if (xpc_exiting)
702 break;
703
704 if (partid == sn_partition_id)
705 continue;
706
707 part = &xpc_partitions[partid];
708
709 if (part->act_state == XPC_P_INACTIVE ||
710 part->act_state == XPC_P_DEACTIVATING) {
711 continue;
712 }
713
714 /* pull the remote_hb cache line */
715 ret = xp_remote_memcpy(remote_vars,
Dean Nelsona47d5da2008-07-29 22:34:09 -0700716 (void *)part->sn.sn2.remote_vars_pa,
Dean Nelson33ba3c72008-07-29 22:34:07 -0700717 XPC_RP_VARS_SIZE);
718 if (ret != xpSuccess) {
719 XPC_DEACTIVATE_PARTITION(part, ret);
720 continue;
721 }
722
723 dev_dbg(xpc_part, "partid = %d, heartbeat = %ld, last_heartbeat"
724 " = %ld, heartbeat_offline = %ld, HB_mask[0] = 0x%lx\n",
725 partid, remote_vars->heartbeat, part->last_heartbeat,
726 remote_vars->heartbeat_offline,
727 remote_vars->heartbeating_to_mask[0]);
728
729 if (((remote_vars->heartbeat == part->last_heartbeat) &&
730 (remote_vars->heartbeat_offline == 0)) ||
731 !xpc_hb_allowed(sn_partition_id,
732 &remote_vars->heartbeating_to_mask)) {
733
734 XPC_DEACTIVATE_PARTITION(part, xpNoHeartbeat);
735 continue;
736 }
737
738 part->last_heartbeat = remote_vars->heartbeat;
739 }
740}
741
742/*
743 * Get a copy of the remote partition's XPC variables from the reserved page.
744 *
745 * remote_vars points to a buffer that is cacheline aligned for BTE copies and
746 * assumed to be of size XPC_RP_VARS_SIZE.
747 */
748static enum xp_retval
749xpc_get_remote_vars_sn2(u64 remote_vars_pa, struct xpc_vars_sn2 *remote_vars)
750{
751 enum xp_retval ret;
752
753 if (remote_vars_pa == 0)
754 return xpVarsNotSet;
755
756 /* pull over the cross partition variables */
757 ret = xp_remote_memcpy(remote_vars, (void *)remote_vars_pa,
758 XPC_RP_VARS_SIZE);
759 if (ret != xpSuccess)
760 return ret;
761
762 if (XPC_VERSION_MAJOR(remote_vars->version) !=
763 XPC_VERSION_MAJOR(XPC_V_VERSION)) {
764 return xpBadVersion;
765 }
766
767 return xpSuccess;
768}
769
770static void
Dean Nelsona47d5da2008-07-29 22:34:09 -0700771xpc_request_partition_activation_sn2(struct xpc_rsvd_page *remote_rp,
772 u64 remote_rp_pa, int nasid)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700773{
Dean Nelson7fb5e592008-07-29 22:34:10 -0700774 xpc_send_local_activate_IRQ_sn2(nasid);
Dean Nelsona47d5da2008-07-29 22:34:09 -0700775}
776
777static void
778xpc_request_partition_reactivation_sn2(struct xpc_partition *part)
779{
Dean Nelson7fb5e592008-07-29 22:34:10 -0700780 xpc_send_local_activate_IRQ_sn2(part->sn.sn2.activate_IRQ_nasid);
Dean Nelsona47d5da2008-07-29 22:34:09 -0700781}
782
783static void
784xpc_request_partition_deactivation_sn2(struct xpc_partition *part)
785{
786 struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
787 unsigned long irq_flags;
Dean Nelsonc39838c2008-07-29 22:34:11 -0700788 struct amo *amo = (struct amo *)__va(part_sn2->remote_amos_page_pa +
Dean Nelsonee6665e2008-07-29 22:34:13 -0700789 (XPC_DEACTIVATE_REQUEST_AMO_SN2 *
Dean Nelsonc39838c2008-07-29 22:34:11 -0700790 sizeof(struct amo)));
Dean Nelsona47d5da2008-07-29 22:34:09 -0700791
792 local_irq_save(irq_flags);
793
Dean Nelsonc39838c2008-07-29 22:34:11 -0700794 /* set bit corresponding to our partid in remote partition's amo */
Dean Nelsona47d5da2008-07-29 22:34:09 -0700795 FETCHOP_STORE_OP(TO_AMO((u64)&amo->variable), FETCHOP_OR,
796 (1UL << sn_partition_id));
797 /*
798 * We must always use the nofault function regardless of whether we
799 * are on a Shub 1.1 system or a Shub 1.2 slice 0xc processor. If we
800 * didn't, we'd never know that the other partition is down and would
Dean Nelsonc39838c2008-07-29 22:34:11 -0700801 * keep sending IRQs and amos to it until the heartbeat times out.
Dean Nelsona47d5da2008-07-29 22:34:09 -0700802 */
803 (void)xp_nofault_PIOR((u64 *)GLOBAL_MMR_ADDR(NASID_GET(&amo->
804 variable),
805 xp_nofault_PIOR_target));
806
807 local_irq_restore(irq_flags);
808
809 /*
810 * Send activate IRQ to get other side to see that we've set our
Dean Nelsonc39838c2008-07-29 22:34:11 -0700811 * bit in their deactivate request amo.
Dean Nelsona47d5da2008-07-29 22:34:09 -0700812 */
Dean Nelson7fb5e592008-07-29 22:34:10 -0700813 xpc_send_activate_IRQ_sn2(part_sn2->remote_amos_page_pa,
Dean Nelsona47d5da2008-07-29 22:34:09 -0700814 cnodeid_to_nasid(0),
815 part_sn2->activate_IRQ_nasid,
816 part_sn2->activate_IRQ_phys_cpuid);
817}
818
819static void
820xpc_cancel_partition_deactivation_request_sn2(struct xpc_partition *part)
821{
822 unsigned long irq_flags;
Dean Nelsonc39838c2008-07-29 22:34:11 -0700823 struct amo *amo = (struct amo *)__va(part->sn.sn2.remote_amos_page_pa +
Dean Nelsonee6665e2008-07-29 22:34:13 -0700824 (XPC_DEACTIVATE_REQUEST_AMO_SN2 *
Dean Nelsonc39838c2008-07-29 22:34:11 -0700825 sizeof(struct amo)));
Dean Nelsona47d5da2008-07-29 22:34:09 -0700826
827 local_irq_save(irq_flags);
828
Dean Nelsonc39838c2008-07-29 22:34:11 -0700829 /* clear bit corresponding to our partid in remote partition's amo */
Dean Nelsona47d5da2008-07-29 22:34:09 -0700830 FETCHOP_STORE_OP(TO_AMO((u64)&amo->variable), FETCHOP_AND,
831 ~(1UL << sn_partition_id));
832 /*
833 * We must always use the nofault function regardless of whether we
834 * are on a Shub 1.1 system or a Shub 1.2 slice 0xc processor. If we
835 * didn't, we'd never know that the other partition is down and would
Dean Nelsonc39838c2008-07-29 22:34:11 -0700836 * keep sending IRQs and amos to it until the heartbeat times out.
Dean Nelsona47d5da2008-07-29 22:34:09 -0700837 */
838 (void)xp_nofault_PIOR((u64 *)GLOBAL_MMR_ADDR(NASID_GET(&amo->
839 variable),
840 xp_nofault_PIOR_target));
841
842 local_irq_restore(irq_flags);
843}
844
845static int
846xpc_partition_deactivation_requested_sn2(short partid)
847{
Dean Nelsonee6665e2008-07-29 22:34:13 -0700848 struct amo *amo = xpc_vars->amos_page + XPC_DEACTIVATE_REQUEST_AMO_SN2;
Dean Nelsona47d5da2008-07-29 22:34:09 -0700849
Dean Nelsonc39838c2008-07-29 22:34:11 -0700850 /* our partition's amo variable ANDed with partid mask */
Dean Nelsona47d5da2008-07-29 22:34:09 -0700851 return (FETCHOP_LOAD_OP(TO_AMO((u64)&amo->variable), FETCHOP_LOAD) &
852 (1UL << partid)) != 0;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700853}
854
855/*
856 * Update the remote partition's info.
857 */
858static void
859xpc_update_partition_info_sn2(struct xpc_partition *part, u8 remote_rp_version,
Dean Nelsonaaa3cd62008-07-29 22:34:07 -0700860 unsigned long *remote_rp_stamp, u64 remote_rp_pa,
861 u64 remote_vars_pa,
Dean Nelson33ba3c72008-07-29 22:34:07 -0700862 struct xpc_vars_sn2 *remote_vars)
863{
Dean Nelsona47d5da2008-07-29 22:34:09 -0700864 struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
865
Dean Nelson33ba3c72008-07-29 22:34:07 -0700866 part->remote_rp_version = remote_rp_version;
867 dev_dbg(xpc_part, " remote_rp_version = 0x%016x\n",
868 part->remote_rp_version);
869
870 part->remote_rp_stamp = *remote_rp_stamp;
Dean Nelsonaaa3cd62008-07-29 22:34:07 -0700871 dev_dbg(xpc_part, " remote_rp_stamp = 0x%016lx\n",
872 part->remote_rp_stamp);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700873
874 part->remote_rp_pa = remote_rp_pa;
875 dev_dbg(xpc_part, " remote_rp_pa = 0x%016lx\n", part->remote_rp_pa);
876
Dean Nelsona47d5da2008-07-29 22:34:09 -0700877 part_sn2->remote_vars_pa = remote_vars_pa;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700878 dev_dbg(xpc_part, " remote_vars_pa = 0x%016lx\n",
Dean Nelsona47d5da2008-07-29 22:34:09 -0700879 part_sn2->remote_vars_pa);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700880
881 part->last_heartbeat = remote_vars->heartbeat;
882 dev_dbg(xpc_part, " last_heartbeat = 0x%016lx\n",
883 part->last_heartbeat);
884
Dean Nelsona47d5da2008-07-29 22:34:09 -0700885 part_sn2->remote_vars_part_pa = remote_vars->vars_part_pa;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700886 dev_dbg(xpc_part, " remote_vars_part_pa = 0x%016lx\n",
Dean Nelsona47d5da2008-07-29 22:34:09 -0700887 part_sn2->remote_vars_part_pa);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700888
Dean Nelsona47d5da2008-07-29 22:34:09 -0700889 part_sn2->activate_IRQ_nasid = remote_vars->activate_IRQ_nasid;
890 dev_dbg(xpc_part, " activate_IRQ_nasid = 0x%x\n",
891 part_sn2->activate_IRQ_nasid);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700892
Dean Nelsona47d5da2008-07-29 22:34:09 -0700893 part_sn2->activate_IRQ_phys_cpuid =
894 remote_vars->activate_IRQ_phys_cpuid;
895 dev_dbg(xpc_part, " activate_IRQ_phys_cpuid = 0x%x\n",
896 part_sn2->activate_IRQ_phys_cpuid);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700897
Dean Nelsona47d5da2008-07-29 22:34:09 -0700898 part_sn2->remote_amos_page_pa = remote_vars->amos_page_pa;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700899 dev_dbg(xpc_part, " remote_amos_page_pa = 0x%lx\n",
Dean Nelsona47d5da2008-07-29 22:34:09 -0700900 part_sn2->remote_amos_page_pa);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700901
Dean Nelsona47d5da2008-07-29 22:34:09 -0700902 part_sn2->remote_vars_version = remote_vars->version;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700903 dev_dbg(xpc_part, " remote_vars_version = 0x%x\n",
Dean Nelsona47d5da2008-07-29 22:34:09 -0700904 part_sn2->remote_vars_version);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700905}
906
907/*
Dean Nelson7fb5e592008-07-29 22:34:10 -0700908 * Prior code has determined the nasid which generated a activate IRQ.
909 * Inspect that nasid to determine if its partition needs to be activated
910 * or deactivated.
Dean Nelson33ba3c72008-07-29 22:34:07 -0700911 *
Dean Nelson7fb5e592008-07-29 22:34:10 -0700912 * A partition is considered "awaiting activation" if our partition
Dean Nelson33ba3c72008-07-29 22:34:07 -0700913 * flags indicate it is not active and it has a heartbeat. A
914 * partition is considered "awaiting deactivation" if our partition
915 * flags indicate it is active but it has no heartbeat or it is not
916 * sending its heartbeat to us.
917 *
918 * To determine the heartbeat, the remote nasid must have a properly
919 * initialized reserved page.
920 */
921static void
Dean Nelson6e410172008-07-29 22:34:09 -0700922xpc_identify_activate_IRQ_req_sn2(int nasid)
Dean Nelson33ba3c72008-07-29 22:34:07 -0700923{
924 struct xpc_rsvd_page *remote_rp;
925 struct xpc_vars_sn2 *remote_vars;
926 u64 remote_rp_pa;
927 u64 remote_vars_pa;
928 int remote_rp_version;
929 int reactivate = 0;
Dean Nelsonaaa3cd62008-07-29 22:34:07 -0700930 unsigned long remote_rp_stamp = 0;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700931 short partid;
932 struct xpc_partition *part;
Dean Nelsona47d5da2008-07-29 22:34:09 -0700933 struct xpc_partition_sn2 *part_sn2;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700934 enum xp_retval ret;
935
936 /* pull over the reserved page structure */
937
Dean Nelsonee6665e2008-07-29 22:34:13 -0700938 remote_rp = (struct xpc_rsvd_page *)xpc_remote_copy_buffer_sn2;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700939
940 ret = xpc_get_remote_rp(nasid, NULL, remote_rp, &remote_rp_pa);
941 if (ret != xpSuccess) {
942 dev_warn(xpc_part, "unable to get reserved page from nasid %d, "
943 "which sent interrupt, reason=%d\n", nasid, ret);
944 return;
945 }
946
947 remote_vars_pa = remote_rp->sn.vars_pa;
948 remote_rp_version = remote_rp->version;
Dean Nelsona47d5da2008-07-29 22:34:09 -0700949 remote_rp_stamp = remote_rp->stamp;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700950
951 partid = remote_rp->SAL_partid;
952 part = &xpc_partitions[partid];
Dean Nelsona47d5da2008-07-29 22:34:09 -0700953 part_sn2 = &part->sn.sn2;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700954
955 /* pull over the cross partition variables */
956
Dean Nelsonee6665e2008-07-29 22:34:13 -0700957 remote_vars = (struct xpc_vars_sn2 *)xpc_remote_copy_buffer_sn2;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700958
959 ret = xpc_get_remote_vars_sn2(remote_vars_pa, remote_vars);
960 if (ret != xpSuccess) {
Dean Nelson33ba3c72008-07-29 22:34:07 -0700961 dev_warn(xpc_part, "unable to get XPC variables from nasid %d, "
962 "which sent interrupt, reason=%d\n", nasid, ret);
963
964 XPC_DEACTIVATE_PARTITION(part, ret);
965 return;
966 }
967
Dean Nelson6e410172008-07-29 22:34:09 -0700968 part->activate_IRQ_rcvd++;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700969
970 dev_dbg(xpc_part, "partid for nasid %d is %d; IRQs = %d; HB = "
Dean Nelson6e410172008-07-29 22:34:09 -0700971 "%ld:0x%lx\n", (int)nasid, (int)partid, part->activate_IRQ_rcvd,
Dean Nelson33ba3c72008-07-29 22:34:07 -0700972 remote_vars->heartbeat, remote_vars->heartbeating_to_mask[0]);
973
974 if (xpc_partition_disengaged(part) &&
975 part->act_state == XPC_P_INACTIVE) {
976
977 xpc_update_partition_info_sn2(part, remote_rp_version,
978 &remote_rp_stamp, remote_rp_pa,
979 remote_vars_pa, remote_vars);
980
Dean Nelsona47d5da2008-07-29 22:34:09 -0700981 if (xpc_partition_deactivation_requested_sn2(partid)) {
982 /*
983 * Other side is waiting on us to deactivate even though
984 * we already have.
985 */
986 return;
Dean Nelson33ba3c72008-07-29 22:34:07 -0700987 }
988
989 xpc_activate_partition(part);
990 return;
991 }
992
993 DBUG_ON(part->remote_rp_version == 0);
Dean Nelsona47d5da2008-07-29 22:34:09 -0700994 DBUG_ON(part_sn2->remote_vars_version == 0);
Dean Nelson33ba3c72008-07-29 22:34:07 -0700995
Dean Nelsona47d5da2008-07-29 22:34:09 -0700996 if (remote_rp_stamp != part->remote_rp_stamp) {
Dean Nelson33ba3c72008-07-29 22:34:07 -0700997
Dean Nelsona47d5da2008-07-29 22:34:09 -0700998 /* the other side rebooted */
Dean Nelson33ba3c72008-07-29 22:34:07 -0700999
Dean Nelsona47d5da2008-07-29 22:34:09 -07001000 DBUG_ON(xpc_partition_engaged_sn2(partid));
1001 DBUG_ON(xpc_partition_deactivation_requested_sn2(partid));
Dean Nelson33ba3c72008-07-29 22:34:07 -07001002
1003 xpc_update_partition_info_sn2(part, remote_rp_version,
1004 &remote_rp_stamp, remote_rp_pa,
1005 remote_vars_pa, remote_vars);
1006 reactivate = 1;
Dean Nelson33ba3c72008-07-29 22:34:07 -07001007 }
1008
Dean Nelsona47d5da2008-07-29 22:34:09 -07001009 if (part->disengage_timeout > 0 && !xpc_partition_disengaged(part)) {
Dean Nelson33ba3c72008-07-29 22:34:07 -07001010 /* still waiting on other side to disengage from us */
1011 return;
1012 }
1013
Dean Nelsona47d5da2008-07-29 22:34:09 -07001014 if (reactivate)
Dean Nelson33ba3c72008-07-29 22:34:07 -07001015 XPC_DEACTIVATE_PARTITION(part, xpReactivating);
Dean Nelsona47d5da2008-07-29 22:34:09 -07001016 else if (xpc_partition_deactivation_requested_sn2(partid))
Dean Nelson33ba3c72008-07-29 22:34:07 -07001017 XPC_DEACTIVATE_PARTITION(part, xpOtherGoingDown);
Dean Nelson33ba3c72008-07-29 22:34:07 -07001018}
1019
1020/*
Dean Nelsonc39838c2008-07-29 22:34:11 -07001021 * Loop through the activation amo variables and process any bits
Dean Nelson33ba3c72008-07-29 22:34:07 -07001022 * which are set. Each bit indicates a nasid sending a partition
1023 * activation or deactivation request.
1024 *
1025 * Return #of IRQs detected.
1026 */
1027int
Dean Nelson6e410172008-07-29 22:34:09 -07001028xpc_identify_activate_IRQ_sender_sn2(void)
Dean Nelson33ba3c72008-07-29 22:34:07 -07001029{
1030 int word, bit;
1031 u64 nasid_mask;
1032 u64 nasid; /* remote nasid */
1033 int n_IRQs_detected = 0;
Dean Nelsonc39838c2008-07-29 22:34:11 -07001034 struct amo *act_amos;
Dean Nelson33ba3c72008-07-29 22:34:07 -07001035
Dean Nelsonee6665e2008-07-29 22:34:13 -07001036 act_amos = xpc_vars->amos_page + XPC_ACTIVATE_IRQ_AMOS_SN2;
Dean Nelson33ba3c72008-07-29 22:34:07 -07001037
Dean Nelsonc39838c2008-07-29 22:34:11 -07001038 /* scan through act amo variable looking for non-zero entries */
Dean Nelsonee6665e2008-07-29 22:34:13 -07001039 for (word = 0; word < xpc_nasid_mask_words; word++) {
Dean Nelson33ba3c72008-07-29 22:34:07 -07001040
1041 if (xpc_exiting)
1042 break;
1043
Dean Nelson7fb5e592008-07-29 22:34:10 -07001044 nasid_mask = xpc_receive_IRQ_amo_sn2(&act_amos[word]);
Dean Nelson33ba3c72008-07-29 22:34:07 -07001045 if (nasid_mask == 0) {
1046 /* no IRQs from nasids in this variable */
1047 continue;
1048 }
1049
Dean Nelsonc39838c2008-07-29 22:34:11 -07001050 dev_dbg(xpc_part, "amo[%d] gave back 0x%lx\n", word,
Dean Nelson33ba3c72008-07-29 22:34:07 -07001051 nasid_mask);
1052
1053 /*
1054 * If this nasid has been added to the machine since
1055 * our partition was reset, this will retain the
1056 * remote nasid in our reserved pages machine mask.
1057 * This is used in the event of module reload.
1058 */
1059 xpc_mach_nasids[word] |= nasid_mask;
1060
1061 /* locate the nasid(s) which sent interrupts */
1062
1063 for (bit = 0; bit < (8 * sizeof(u64)); bit++) {
1064 if (nasid_mask & (1UL << bit)) {
1065 n_IRQs_detected++;
1066 nasid = XPC_NASID_FROM_W_B(word, bit);
1067 dev_dbg(xpc_part, "interrupt from nasid %ld\n",
1068 nasid);
Dean Nelson6e410172008-07-29 22:34:09 -07001069 xpc_identify_activate_IRQ_req_sn2(nasid);
Dean Nelson33ba3c72008-07-29 22:34:07 -07001070 }
1071 }
1072 }
1073 return n_IRQs_detected;
1074}
1075
1076static void
Dean Nelson6e410172008-07-29 22:34:09 -07001077xpc_process_activate_IRQ_rcvd_sn2(int n_IRQs_expected)
Dean Nelson33ba3c72008-07-29 22:34:07 -07001078{
1079 int n_IRQs_detected;
1080
Dean Nelson6e410172008-07-29 22:34:09 -07001081 n_IRQs_detected = xpc_identify_activate_IRQ_sender_sn2();
Dean Nelson33ba3c72008-07-29 22:34:07 -07001082 if (n_IRQs_detected < n_IRQs_expected) {
Dean Nelsonc39838c2008-07-29 22:34:11 -07001083 /* retry once to help avoid missing amo */
Dean Nelson6e410172008-07-29 22:34:09 -07001084 (void)xpc_identify_activate_IRQ_sender_sn2();
Dean Nelson33ba3c72008-07-29 22:34:07 -07001085 }
1086}
1087
Dean Nelsone17d4162008-07-29 22:34:06 -07001088/*
Dean Nelson185c3a12008-07-29 22:34:11 -07001089 * Guarantee that the kzalloc'd memory is cacheline aligned.
1090 */
1091static void *
1092xpc_kzalloc_cacheline_aligned_sn2(size_t size, gfp_t flags, void **base)
1093{
1094 /* see if kzalloc will give us cachline aligned memory by default */
1095 *base = kzalloc(size, flags);
1096 if (*base == NULL)
1097 return NULL;
1098
1099 if ((u64)*base == L1_CACHE_ALIGN((u64)*base))
1100 return *base;
1101
1102 kfree(*base);
1103
1104 /* nope, we'll have to do it ourselves */
1105 *base = kzalloc(size + L1_CACHE_BYTES, flags);
1106 if (*base == NULL)
1107 return NULL;
1108
1109 return (void *)L1_CACHE_ALIGN((u64)*base);
1110}
1111
1112/*
Dean Nelsone17d4162008-07-29 22:34:06 -07001113 * Setup the infrastructure necessary to support XPartition Communication
1114 * between the specified remote partition and the local one.
1115 */
1116static enum xp_retval
1117xpc_setup_infrastructure_sn2(struct xpc_partition *part)
1118{
Dean Nelsona47d5da2008-07-29 22:34:09 -07001119 struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
Dean Nelsone17d4162008-07-29 22:34:06 -07001120 enum xp_retval retval;
1121 int ret;
1122 int cpuid;
1123 int ch_number;
1124 struct xpc_channel *ch;
1125 struct timer_list *timer;
1126 short partid = XPC_PARTID(part);
1127
1128 /*
1129 * Allocate all of the channel structures as a contiguous chunk of
1130 * memory.
1131 */
1132 DBUG_ON(part->channels != NULL);
1133 part->channels = kzalloc(sizeof(struct xpc_channel) * XPC_MAX_NCHANNELS,
1134 GFP_KERNEL);
1135 if (part->channels == NULL) {
1136 dev_err(xpc_chan, "can't get memory for channels\n");
1137 return xpNoMemory;
1138 }
1139
1140 /* allocate all the required GET/PUT values */
1141
Dean Nelson185c3a12008-07-29 22:34:11 -07001142 part_sn2->local_GPs =
1143 xpc_kzalloc_cacheline_aligned_sn2(XPC_GP_SIZE, GFP_KERNEL,
1144 &part_sn2->local_GPs_base);
Dean Nelsona47d5da2008-07-29 22:34:09 -07001145 if (part_sn2->local_GPs == NULL) {
Dean Nelsone17d4162008-07-29 22:34:06 -07001146 dev_err(xpc_chan, "can't get memory for local get/put "
1147 "values\n");
1148 retval = xpNoMemory;
1149 goto out_1;
1150 }
1151
Dean Nelson185c3a12008-07-29 22:34:11 -07001152 part_sn2->remote_GPs =
1153 xpc_kzalloc_cacheline_aligned_sn2(XPC_GP_SIZE, GFP_KERNEL,
1154 &part_sn2->remote_GPs_base);
Dean Nelsona47d5da2008-07-29 22:34:09 -07001155 if (part_sn2->remote_GPs == NULL) {
Dean Nelsone17d4162008-07-29 22:34:06 -07001156 dev_err(xpc_chan, "can't get memory for remote get/put "
1157 "values\n");
1158 retval = xpNoMemory;
1159 goto out_2;
1160 }
1161
Dean Nelsona47d5da2008-07-29 22:34:09 -07001162 part_sn2->remote_GPs_pa = 0;
Dean Nelsone17d4162008-07-29 22:34:06 -07001163
1164 /* allocate all the required open and close args */
1165
1166 part->local_openclose_args =
Dean Nelson185c3a12008-07-29 22:34:11 -07001167 xpc_kzalloc_cacheline_aligned_sn2(XPC_OPENCLOSE_ARGS_SIZE,
1168 GFP_KERNEL,
1169 &part->local_openclose_args_base);
Dean Nelsone17d4162008-07-29 22:34:06 -07001170 if (part->local_openclose_args == NULL) {
1171 dev_err(xpc_chan, "can't get memory for local connect args\n");
1172 retval = xpNoMemory;
1173 goto out_3;
1174 }
1175
1176 part->remote_openclose_args =
Dean Nelson185c3a12008-07-29 22:34:11 -07001177 xpc_kzalloc_cacheline_aligned_sn2(XPC_OPENCLOSE_ARGS_SIZE,
1178 GFP_KERNEL,
1179 &part->remote_openclose_args_base);
Dean Nelsone17d4162008-07-29 22:34:06 -07001180 if (part->remote_openclose_args == NULL) {
1181 dev_err(xpc_chan, "can't get memory for remote connect args\n");
1182 retval = xpNoMemory;
1183 goto out_4;
1184 }
1185
Dean Nelsona47d5da2008-07-29 22:34:09 -07001186 part_sn2->remote_openclose_args_pa = 0;
Dean Nelsone17d4162008-07-29 22:34:06 -07001187
Dean Nelson7fb5e592008-07-29 22:34:10 -07001188 part_sn2->local_chctl_amo_va = xpc_init_IRQ_amo_sn2(partid);
1189 part->chctl.all_flags = 0;
1190 spin_lock_init(&part->chctl_lock);
Dean Nelsone17d4162008-07-29 22:34:06 -07001191
Dean Nelson7fb5e592008-07-29 22:34:10 -07001192 part_sn2->notify_IRQ_nasid = 0;
1193 part_sn2->notify_IRQ_phys_cpuid = 0;
1194 part_sn2->remote_chctl_amo_va = NULL;
Dean Nelsone17d4162008-07-29 22:34:06 -07001195
1196 atomic_set(&part->channel_mgr_requests, 1);
1197 init_waitqueue_head(&part->channel_mgr_wq);
1198
Dean Nelson7fb5e592008-07-29 22:34:10 -07001199 sprintf(part_sn2->notify_IRQ_owner, "xpc%02d", partid);
Dean Nelsona47d5da2008-07-29 22:34:09 -07001200 ret = request_irq(SGI_XPC_NOTIFY, xpc_handle_notify_IRQ_sn2,
Dean Nelson7fb5e592008-07-29 22:34:10 -07001201 IRQF_SHARED, part_sn2->notify_IRQ_owner,
Dean Nelsona47d5da2008-07-29 22:34:09 -07001202 (void *)(u64)partid);
Dean Nelsone17d4162008-07-29 22:34:06 -07001203 if (ret != 0) {
1204 dev_err(xpc_chan, "can't register NOTIFY IRQ handler, "
1205 "errno=%d\n", -ret);
1206 retval = xpLackOfResources;
1207 goto out_5;
1208 }
1209
Dean Nelson7fb5e592008-07-29 22:34:10 -07001210 /* Setup a timer to check for dropped notify IRQs */
Dean Nelsona47d5da2008-07-29 22:34:09 -07001211 timer = &part_sn2->dropped_notify_IRQ_timer;
Dean Nelsone17d4162008-07-29 22:34:06 -07001212 init_timer(timer);
Dean Nelsona47d5da2008-07-29 22:34:09 -07001213 timer->function =
Dean Nelson7fb5e592008-07-29 22:34:10 -07001214 (void (*)(unsigned long))xpc_check_for_dropped_notify_IRQ_sn2;
Dean Nelsone17d4162008-07-29 22:34:06 -07001215 timer->data = (unsigned long)part;
Dean Nelson7fb5e592008-07-29 22:34:10 -07001216 timer->expires = jiffies + XPC_DROPPED_NOTIFY_IRQ_WAIT_INTERVAL;
Dean Nelsone17d4162008-07-29 22:34:06 -07001217 add_timer(timer);
1218
1219 part->nchannels = XPC_MAX_NCHANNELS;
1220
1221 atomic_set(&part->nchannels_active, 0);
1222 atomic_set(&part->nchannels_engaged, 0);
1223
1224 for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
1225 ch = &part->channels[ch_number];
1226
1227 ch->partid = partid;
1228 ch->number = ch_number;
1229 ch->flags = XPC_C_DISCONNECTED;
1230
Dean Nelsona47d5da2008-07-29 22:34:09 -07001231 ch->sn.sn2.local_GP = &part_sn2->local_GPs[ch_number];
Dean Nelsone17d4162008-07-29 22:34:06 -07001232 ch->local_openclose_args =
1233 &part->local_openclose_args[ch_number];
1234
1235 atomic_set(&ch->kthreads_assigned, 0);
1236 atomic_set(&ch->kthreads_idle, 0);
1237 atomic_set(&ch->kthreads_active, 0);
1238
1239 atomic_set(&ch->references, 0);
1240 atomic_set(&ch->n_to_notify, 0);
1241
1242 spin_lock_init(&ch->lock);
Dean Nelsona47d5da2008-07-29 22:34:09 -07001243 mutex_init(&ch->sn.sn2.msg_to_pull_mutex);
Dean Nelsone17d4162008-07-29 22:34:06 -07001244 init_completion(&ch->wdisconnect_wait);
1245
1246 atomic_set(&ch->n_on_msg_allocate_wq, 0);
1247 init_waitqueue_head(&ch->msg_allocate_wq);
1248 init_waitqueue_head(&ch->idle_wq);
1249 }
1250
1251 /*
1252 * With the setting of the partition setup_state to XPC_P_SETUP, we're
1253 * declaring that this partition is ready to go.
1254 */
1255 part->setup_state = XPC_P_SETUP;
1256
1257 /*
1258 * Setup the per partition specific variables required by the
1259 * remote partition to establish channel connections with us.
1260 *
1261 * The setting of the magic # indicates that these per partition
1262 * specific variables are ready to be used.
1263 */
Dean Nelsona47d5da2008-07-29 22:34:09 -07001264 xpc_vars_part[partid].GPs_pa = __pa(part_sn2->local_GPs);
Dean Nelsone17d4162008-07-29 22:34:06 -07001265 xpc_vars_part[partid].openclose_args_pa =
1266 __pa(part->local_openclose_args);
Dean Nelson7fb5e592008-07-29 22:34:10 -07001267 xpc_vars_part[partid].chctl_amo_pa = __pa(part_sn2->local_chctl_amo_va);
Dean Nelsone17d4162008-07-29 22:34:06 -07001268 cpuid = raw_smp_processor_id(); /* any CPU in this partition will do */
Dean Nelson7fb5e592008-07-29 22:34:10 -07001269 xpc_vars_part[partid].notify_IRQ_nasid = cpuid_to_nasid(cpuid);
1270 xpc_vars_part[partid].notify_IRQ_phys_cpuid = cpu_physical_id(cpuid);
Dean Nelsone17d4162008-07-29 22:34:06 -07001271 xpc_vars_part[partid].nchannels = part->nchannels;
1272 xpc_vars_part[partid].magic = XPC_VP_MAGIC1;
1273
1274 return xpSuccess;
1275
1276 /* setup of infrastructure failed */
1277out_5:
1278 kfree(part->remote_openclose_args_base);
1279 part->remote_openclose_args = NULL;
1280out_4:
1281 kfree(part->local_openclose_args_base);
1282 part->local_openclose_args = NULL;
1283out_3:
Dean Nelsona47d5da2008-07-29 22:34:09 -07001284 kfree(part_sn2->remote_GPs_base);
1285 part_sn2->remote_GPs = NULL;
Dean Nelsone17d4162008-07-29 22:34:06 -07001286out_2:
Dean Nelsona47d5da2008-07-29 22:34:09 -07001287 kfree(part_sn2->local_GPs_base);
1288 part_sn2->local_GPs = NULL;
Dean Nelsone17d4162008-07-29 22:34:06 -07001289out_1:
1290 kfree(part->channels);
1291 part->channels = NULL;
1292 return retval;
1293}
1294
1295/*
1296 * Teardown the infrastructure necessary to support XPartition Communication
1297 * between the specified remote partition and the local one.
1298 */
1299static void
1300xpc_teardown_infrastructure_sn2(struct xpc_partition *part)
1301{
Dean Nelsona47d5da2008-07-29 22:34:09 -07001302 struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
Dean Nelsone17d4162008-07-29 22:34:06 -07001303 short partid = XPC_PARTID(part);
1304
1305 /*
1306 * We start off by making this partition inaccessible to local
1307 * processes by marking it as no longer setup. Then we make it
1308 * inaccessible to remote processes by clearing the XPC per partition
1309 * specific variable's magic # (which indicates that these variables
Dean Nelson7fb5e592008-07-29 22:34:10 -07001310 * are no longer valid) and by ignoring all XPC notify IRQs sent to
Dean Nelsone17d4162008-07-29 22:34:06 -07001311 * this partition.
1312 */
1313
1314 DBUG_ON(atomic_read(&part->nchannels_engaged) != 0);
1315 DBUG_ON(atomic_read(&part->nchannels_active) != 0);
1316 DBUG_ON(part->setup_state != XPC_P_SETUP);
1317 part->setup_state = XPC_P_WTEARDOWN;
1318
1319 xpc_vars_part[partid].magic = 0;
1320
1321 free_irq(SGI_XPC_NOTIFY, (void *)(u64)partid);
1322
1323 /*
1324 * Before proceeding with the teardown we have to wait until all
1325 * existing references cease.
1326 */
1327 wait_event(part->teardown_wq, (atomic_read(&part->references) == 0));
1328
1329 /* now we can begin tearing down the infrastructure */
1330
1331 part->setup_state = XPC_P_TORNDOWN;
1332
1333 /* in case we've still got outstanding timers registered... */
Dean Nelsona47d5da2008-07-29 22:34:09 -07001334 del_timer_sync(&part_sn2->dropped_notify_IRQ_timer);
Dean Nelsone17d4162008-07-29 22:34:06 -07001335
1336 kfree(part->remote_openclose_args_base);
1337 part->remote_openclose_args = NULL;
1338 kfree(part->local_openclose_args_base);
1339 part->local_openclose_args = NULL;
Dean Nelsona47d5da2008-07-29 22:34:09 -07001340 kfree(part_sn2->remote_GPs_base);
1341 part_sn2->remote_GPs = NULL;
1342 kfree(part_sn2->local_GPs_base);
1343 part_sn2->local_GPs = NULL;
Dean Nelsone17d4162008-07-29 22:34:06 -07001344 kfree(part->channels);
1345 part->channels = NULL;
Dean Nelson7fb5e592008-07-29 22:34:10 -07001346 part_sn2->local_chctl_amo_va = NULL;
Dean Nelsone17d4162008-07-29 22:34:06 -07001347}
1348
1349/*
1350 * Create a wrapper that hides the underlying mechanism for pulling a cacheline
1351 * (or multiple cachelines) from a remote partition.
1352 *
1353 * src must be a cacheline aligned physical address on the remote partition.
1354 * dst must be a cacheline aligned virtual address on this partition.
1355 * cnt must be cacheline sized
1356 */
1357/* >>> Replace this function by call to xp_remote_memcpy() or bte_copy()? */
1358static enum xp_retval
1359xpc_pull_remote_cachelines_sn2(struct xpc_partition *part, void *dst,
1360 const void *src, size_t cnt)
1361{
1362 enum xp_retval ret;
1363
1364 DBUG_ON((u64)src != L1_CACHE_ALIGN((u64)src));
1365 DBUG_ON((u64)dst != L1_CACHE_ALIGN((u64)dst));
1366 DBUG_ON(cnt != L1_CACHE_ALIGN(cnt));
1367
1368 if (part->act_state == XPC_P_DEACTIVATING)
1369 return part->reason;
1370
1371 ret = xp_remote_memcpy(dst, src, cnt);
1372 if (ret != xpSuccess) {
1373 dev_dbg(xpc_chan, "xp_remote_memcpy() from partition %d failed,"
1374 " ret=%d\n", XPC_PARTID(part), ret);
1375 }
1376 return ret;
1377}
1378
1379/*
1380 * Pull the remote per partition specific variables from the specified
1381 * partition.
1382 */
1383static enum xp_retval
1384xpc_pull_remote_vars_part_sn2(struct xpc_partition *part)
1385{
Dean Nelsona47d5da2008-07-29 22:34:09 -07001386 struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
Dean Nelsone17d4162008-07-29 22:34:06 -07001387 u8 buffer[L1_CACHE_BYTES * 2];
1388 struct xpc_vars_part_sn2 *pulled_entry_cacheline =
1389 (struct xpc_vars_part_sn2 *)L1_CACHE_ALIGN((u64)buffer);
1390 struct xpc_vars_part_sn2 *pulled_entry;
1391 u64 remote_entry_cacheline_pa, remote_entry_pa;
1392 short partid = XPC_PARTID(part);
1393 enum xp_retval ret;
1394
1395 /* pull the cacheline that contains the variables we're interested in */
1396
Dean Nelsona47d5da2008-07-29 22:34:09 -07001397 DBUG_ON(part_sn2->remote_vars_part_pa !=
1398 L1_CACHE_ALIGN(part_sn2->remote_vars_part_pa));
Dean Nelsone17d4162008-07-29 22:34:06 -07001399 DBUG_ON(sizeof(struct xpc_vars_part_sn2) != L1_CACHE_BYTES / 2);
1400
Dean Nelsona47d5da2008-07-29 22:34:09 -07001401 remote_entry_pa = part_sn2->remote_vars_part_pa +
Dean Nelsone17d4162008-07-29 22:34:06 -07001402 sn_partition_id * sizeof(struct xpc_vars_part_sn2);
1403
1404 remote_entry_cacheline_pa = (remote_entry_pa & ~(L1_CACHE_BYTES - 1));
1405
1406 pulled_entry = (struct xpc_vars_part_sn2 *)((u64)pulled_entry_cacheline
1407 + (remote_entry_pa &
1408 (L1_CACHE_BYTES - 1)));
1409
1410 ret = xpc_pull_remote_cachelines_sn2(part, pulled_entry_cacheline,
1411 (void *)remote_entry_cacheline_pa,
1412 L1_CACHE_BYTES);
1413 if (ret != xpSuccess) {
1414 dev_dbg(xpc_chan, "failed to pull XPC vars_part from "
1415 "partition %d, ret=%d\n", partid, ret);
1416 return ret;
1417 }
1418
1419 /* see if they've been set up yet */
1420
1421 if (pulled_entry->magic != XPC_VP_MAGIC1 &&
1422 pulled_entry->magic != XPC_VP_MAGIC2) {
1423
1424 if (pulled_entry->magic != 0) {
1425 dev_dbg(xpc_chan, "partition %d's XPC vars_part for "
1426 "partition %d has bad magic value (=0x%lx)\n",
1427 partid, sn_partition_id, pulled_entry->magic);
1428 return xpBadMagic;
1429 }
1430
1431 /* they've not been initialized yet */
1432 return xpRetry;
1433 }
1434
1435 if (xpc_vars_part[partid].magic == XPC_VP_MAGIC1) {
1436
1437 /* validate the variables */
1438
1439 if (pulled_entry->GPs_pa == 0 ||
1440 pulled_entry->openclose_args_pa == 0 ||
Dean Nelson7fb5e592008-07-29 22:34:10 -07001441 pulled_entry->chctl_amo_pa == 0) {
Dean Nelsone17d4162008-07-29 22:34:06 -07001442
1443 dev_err(xpc_chan, "partition %d's XPC vars_part for "
1444 "partition %d are not valid\n", partid,
1445 sn_partition_id);
1446 return xpInvalidAddress;
1447 }
1448
1449 /* the variables we imported look to be valid */
1450
Dean Nelsona47d5da2008-07-29 22:34:09 -07001451 part_sn2->remote_GPs_pa = pulled_entry->GPs_pa;
1452 part_sn2->remote_openclose_args_pa =
Dean Nelsone17d4162008-07-29 22:34:06 -07001453 pulled_entry->openclose_args_pa;
Dean Nelson7fb5e592008-07-29 22:34:10 -07001454 part_sn2->remote_chctl_amo_va =
Dean Nelsonc39838c2008-07-29 22:34:11 -07001455 (struct amo *)__va(pulled_entry->chctl_amo_pa);
Dean Nelson7fb5e592008-07-29 22:34:10 -07001456 part_sn2->notify_IRQ_nasid = pulled_entry->notify_IRQ_nasid;
1457 part_sn2->notify_IRQ_phys_cpuid =
1458 pulled_entry->notify_IRQ_phys_cpuid;
Dean Nelsone17d4162008-07-29 22:34:06 -07001459
1460 if (part->nchannels > pulled_entry->nchannels)
1461 part->nchannels = pulled_entry->nchannels;
1462
1463 /* let the other side know that we've pulled their variables */
1464
1465 xpc_vars_part[partid].magic = XPC_VP_MAGIC2;
1466 }
1467
1468 if (pulled_entry->magic == XPC_VP_MAGIC1)
1469 return xpRetry;
1470
1471 return xpSuccess;
1472}
1473
1474/*
1475 * Establish first contact with the remote partititon. This involves pulling
1476 * the XPC per partition variables from the remote partition and waiting for
1477 * the remote partition to pull ours.
1478 */
1479static enum xp_retval
1480xpc_make_first_contact_sn2(struct xpc_partition *part)
1481{
Dean Nelsona47d5da2008-07-29 22:34:09 -07001482 struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
Dean Nelsone17d4162008-07-29 22:34:06 -07001483 enum xp_retval ret;
1484
Dean Nelson33ba3c72008-07-29 22:34:07 -07001485 /*
Dean Nelsonc39838c2008-07-29 22:34:11 -07001486 * Register the remote partition's amos with SAL so it can handle
Dean Nelson33ba3c72008-07-29 22:34:07 -07001487 * and cleanup errors within that address range should the remote
1488 * partition go down. We don't unregister this range because it is
1489 * difficult to tell when outstanding writes to the remote partition
1490 * are finished and thus when it is safe to unregister. This should
1491 * not result in wasted space in the SAL xp_addr_region table because
1492 * we should get the same page for remote_amos_page_pa after module
1493 * reloads and system reboots.
1494 */
Dean Nelsona47d5da2008-07-29 22:34:09 -07001495 if (sn_register_xp_addr_region(part_sn2->remote_amos_page_pa,
Dean Nelson33ba3c72008-07-29 22:34:07 -07001496 PAGE_SIZE, 1) < 0) {
1497 dev_warn(xpc_part, "xpc_activating(%d) failed to register "
1498 "xp_addr region\n", XPC_PARTID(part));
1499
1500 ret = xpPhysAddrRegFailed;
1501 XPC_DEACTIVATE_PARTITION(part, ret);
1502 return ret;
1503 }
1504
Dean Nelsona47d5da2008-07-29 22:34:09 -07001505 /*
1506 * Send activate IRQ to get other side to activate if they've not
1507 * already begun to do so.
1508 */
Dean Nelson7fb5e592008-07-29 22:34:10 -07001509 xpc_send_activate_IRQ_sn2(part_sn2->remote_amos_page_pa,
Dean Nelsona47d5da2008-07-29 22:34:09 -07001510 cnodeid_to_nasid(0),
1511 part_sn2->activate_IRQ_nasid,
1512 part_sn2->activate_IRQ_phys_cpuid);
Dean Nelson33ba3c72008-07-29 22:34:07 -07001513
Dean Nelsone17d4162008-07-29 22:34:06 -07001514 while ((ret = xpc_pull_remote_vars_part_sn2(part)) != xpSuccess) {
1515 if (ret != xpRetry) {
1516 XPC_DEACTIVATE_PARTITION(part, ret);
1517 return ret;
1518 }
1519
1520 dev_dbg(xpc_part, "waiting to make first contact with "
1521 "partition %d\n", XPC_PARTID(part));
1522
1523 /* wait a 1/4 of a second or so */
1524 (void)msleep_interruptible(250);
1525
1526 if (part->act_state == XPC_P_DEACTIVATING)
1527 return part->reason;
1528 }
1529
1530 return xpSuccess;
1531}
1532
1533/*
Dean Nelson7fb5e592008-07-29 22:34:10 -07001534 * Get the chctl flags and pull the openclose args and/or remote GPs as needed.
Dean Nelsone17d4162008-07-29 22:34:06 -07001535 */
1536static u64
Dean Nelson7fb5e592008-07-29 22:34:10 -07001537xpc_get_chctl_all_flags_sn2(struct xpc_partition *part)
Dean Nelsone17d4162008-07-29 22:34:06 -07001538{
Dean Nelsona47d5da2008-07-29 22:34:09 -07001539 struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
Dean Nelsone17d4162008-07-29 22:34:06 -07001540 unsigned long irq_flags;
Dean Nelson7fb5e592008-07-29 22:34:10 -07001541 union xpc_channel_ctl_flags chctl;
Dean Nelsone17d4162008-07-29 22:34:06 -07001542 enum xp_retval ret;
1543
1544 /*
Dean Nelson7fb5e592008-07-29 22:34:10 -07001545 * See if there are any chctl flags to be handled.
Dean Nelsone17d4162008-07-29 22:34:06 -07001546 */
1547
Dean Nelson7fb5e592008-07-29 22:34:10 -07001548 spin_lock_irqsave(&part->chctl_lock, irq_flags);
1549 chctl = part->chctl;
1550 if (chctl.all_flags != 0)
1551 part->chctl.all_flags = 0;
Dean Nelsone17d4162008-07-29 22:34:06 -07001552
Dean Nelson7fb5e592008-07-29 22:34:10 -07001553 spin_unlock_irqrestore(&part->chctl_lock, irq_flags);
Dean Nelsone17d4162008-07-29 22:34:06 -07001554
Dean Nelson7fb5e592008-07-29 22:34:10 -07001555 if (xpc_any_openclose_chctl_flags_set(&chctl)) {
Dean Nelsona47d5da2008-07-29 22:34:09 -07001556 ret = xpc_pull_remote_cachelines_sn2(part, part->
1557 remote_openclose_args,
1558 (void *)part_sn2->
Dean Nelsone17d4162008-07-29 22:34:06 -07001559 remote_openclose_args_pa,
1560 XPC_OPENCLOSE_ARGS_SIZE);
1561 if (ret != xpSuccess) {
1562 XPC_DEACTIVATE_PARTITION(part, ret);
1563
1564 dev_dbg(xpc_chan, "failed to pull openclose args from "
1565 "partition %d, ret=%d\n", XPC_PARTID(part),
1566 ret);
1567
Dean Nelson7fb5e592008-07-29 22:34:10 -07001568 /* don't bother processing chctl flags anymore */
1569 chctl.all_flags = 0;
Dean Nelsone17d4162008-07-29 22:34:06 -07001570 }
1571 }
1572
Dean Nelson7fb5e592008-07-29 22:34:10 -07001573 if (xpc_any_msg_chctl_flags_set(&chctl)) {
Dean Nelsona47d5da2008-07-29 22:34:09 -07001574 ret = xpc_pull_remote_cachelines_sn2(part, part_sn2->remote_GPs,
1575 (void *)part_sn2->remote_GPs_pa,
Dean Nelsone17d4162008-07-29 22:34:06 -07001576 XPC_GP_SIZE);
1577 if (ret != xpSuccess) {
1578 XPC_DEACTIVATE_PARTITION(part, ret);
1579
1580 dev_dbg(xpc_chan, "failed to pull GPs from partition "
1581 "%d, ret=%d\n", XPC_PARTID(part), ret);
1582
Dean Nelson7fb5e592008-07-29 22:34:10 -07001583 /* don't bother processing chctl flags anymore */
1584 chctl.all_flags = 0;
Dean Nelsone17d4162008-07-29 22:34:06 -07001585 }
1586 }
1587
Dean Nelson7fb5e592008-07-29 22:34:10 -07001588 return chctl.all_flags;
Dean Nelsone17d4162008-07-29 22:34:06 -07001589}
1590
Dean Nelsona47d5da2008-07-29 22:34:09 -07001591/*
Dean Nelson185c3a12008-07-29 22:34:11 -07001592 * Allocate the local message queue and the notify queue.
1593 */
1594static enum xp_retval
1595xpc_allocate_local_msgqueue_sn2(struct xpc_channel *ch)
1596{
1597 unsigned long irq_flags;
1598 int nentries;
1599 size_t nbytes;
1600
1601 for (nentries = ch->local_nentries; nentries > 0; nentries--) {
1602
1603 nbytes = nentries * ch->msg_size;
1604 ch->local_msgqueue =
1605 xpc_kzalloc_cacheline_aligned_sn2(nbytes, GFP_KERNEL,
1606 &ch->local_msgqueue_base);
1607 if (ch->local_msgqueue == NULL)
1608 continue;
1609
1610 nbytes = nentries * sizeof(struct xpc_notify);
1611 ch->notify_queue = kzalloc(nbytes, GFP_KERNEL);
1612 if (ch->notify_queue == NULL) {
1613 kfree(ch->local_msgqueue_base);
1614 ch->local_msgqueue = NULL;
1615 continue;
1616 }
1617
1618 spin_lock_irqsave(&ch->lock, irq_flags);
1619 if (nentries < ch->local_nentries) {
1620 dev_dbg(xpc_chan, "nentries=%d local_nentries=%d, "
1621 "partid=%d, channel=%d\n", nentries,
1622 ch->local_nentries, ch->partid, ch->number);
1623
1624 ch->local_nentries = nentries;
1625 }
1626 spin_unlock_irqrestore(&ch->lock, irq_flags);
1627 return xpSuccess;
1628 }
1629
1630 dev_dbg(xpc_chan, "can't get memory for local message queue and notify "
1631 "queue, partid=%d, channel=%d\n", ch->partid, ch->number);
1632 return xpNoMemory;
1633}
1634
1635/*
1636 * Allocate the cached remote message queue.
1637 */
1638static enum xp_retval
1639xpc_allocate_remote_msgqueue_sn2(struct xpc_channel *ch)
1640{
1641 unsigned long irq_flags;
1642 int nentries;
1643 size_t nbytes;
1644
1645 DBUG_ON(ch->remote_nentries <= 0);
1646
1647 for (nentries = ch->remote_nentries; nentries > 0; nentries--) {
1648
1649 nbytes = nentries * ch->msg_size;
1650 ch->remote_msgqueue =
1651 xpc_kzalloc_cacheline_aligned_sn2(nbytes, GFP_KERNEL,
1652 &ch->remote_msgqueue_base);
1653 if (ch->remote_msgqueue == NULL)
1654 continue;
1655
1656 spin_lock_irqsave(&ch->lock, irq_flags);
1657 if (nentries < ch->remote_nentries) {
1658 dev_dbg(xpc_chan, "nentries=%d remote_nentries=%d, "
1659 "partid=%d, channel=%d\n", nentries,
1660 ch->remote_nentries, ch->partid, ch->number);
1661
1662 ch->remote_nentries = nentries;
1663 }
1664 spin_unlock_irqrestore(&ch->lock, irq_flags);
1665 return xpSuccess;
1666 }
1667
1668 dev_dbg(xpc_chan, "can't get memory for cached remote message queue, "
1669 "partid=%d, channel=%d\n", ch->partid, ch->number);
1670 return xpNoMemory;
1671}
1672
1673/*
1674 * Allocate message queues and other stuff associated with a channel.
1675 *
1676 * Note: Assumes all of the channel sizes are filled in.
1677 */
1678static enum xp_retval
1679xpc_allocate_msgqueues_sn2(struct xpc_channel *ch)
1680{
1681 enum xp_retval ret;
1682
1683 DBUG_ON(ch->flags & XPC_C_SETUP);
1684
1685 ret = xpc_allocate_local_msgqueue_sn2(ch);
1686 if (ret == xpSuccess) {
1687
1688 ret = xpc_allocate_remote_msgqueue_sn2(ch);
1689 if (ret != xpSuccess) {
1690 kfree(ch->local_msgqueue_base);
1691 ch->local_msgqueue = NULL;
1692 kfree(ch->notify_queue);
1693 ch->notify_queue = NULL;
1694 }
1695 }
1696 return ret;
1697}
1698
1699/*
1700 * Free up message queues and other stuff that were allocated for the specified
1701 * channel.
1702 *
1703 * Note: ch->reason and ch->reason_line are left set for debugging purposes,
1704 * they're cleared when XPC_C_DISCONNECTED is cleared.
1705 */
1706static void
1707xpc_free_msgqueues_sn2(struct xpc_channel *ch)
1708{
1709 struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
1710
1711 DBUG_ON(!spin_is_locked(&ch->lock));
1712 DBUG_ON(atomic_read(&ch->n_to_notify) != 0);
1713
1714 ch->remote_msgqueue_pa = 0;
1715 ch->func = NULL;
1716 ch->key = NULL;
1717 ch->msg_size = 0;
1718 ch->local_nentries = 0;
1719 ch->remote_nentries = 0;
1720 ch->kthreads_assigned_limit = 0;
1721 ch->kthreads_idle_limit = 0;
1722
1723 ch_sn2->local_GP->get = 0;
1724 ch_sn2->local_GP->put = 0;
1725 ch_sn2->remote_GP.get = 0;
1726 ch_sn2->remote_GP.put = 0;
1727 ch_sn2->w_local_GP.get = 0;
1728 ch_sn2->w_local_GP.put = 0;
1729 ch_sn2->w_remote_GP.get = 0;
1730 ch_sn2->w_remote_GP.put = 0;
1731 ch_sn2->next_msg_to_pull = 0;
1732
1733 if (ch->flags & XPC_C_SETUP) {
1734 dev_dbg(xpc_chan, "ch->flags=0x%x, partid=%d, channel=%d\n",
1735 ch->flags, ch->partid, ch->number);
1736
1737 kfree(ch->local_msgqueue_base);
1738 ch->local_msgqueue = NULL;
1739 kfree(ch->remote_msgqueue_base);
1740 ch->remote_msgqueue = NULL;
1741 kfree(ch->notify_queue);
1742 ch->notify_queue = NULL;
1743 }
1744}
1745
1746/*
Dean Nelsona47d5da2008-07-29 22:34:09 -07001747 * Notify those who wanted to be notified upon delivery of their message.
1748 */
1749static void
1750xpc_notify_senders_sn2(struct xpc_channel *ch, enum xp_retval reason, s64 put)
1751{
1752 struct xpc_notify *notify;
1753 u8 notify_type;
1754 s64 get = ch->sn.sn2.w_remote_GP.get - 1;
1755
1756 while (++get < put && atomic_read(&ch->n_to_notify) > 0) {
1757
1758 notify = &ch->notify_queue[get % ch->local_nentries];
1759
1760 /*
1761 * See if the notify entry indicates it was associated with
1762 * a message who's sender wants to be notified. It is possible
1763 * that it is, but someone else is doing or has done the
1764 * notification.
1765 */
1766 notify_type = notify->type;
1767 if (notify_type == 0 ||
1768 cmpxchg(&notify->type, notify_type, 0) != notify_type) {
1769 continue;
1770 }
1771
1772 DBUG_ON(notify_type != XPC_N_CALL);
1773
1774 atomic_dec(&ch->n_to_notify);
1775
1776 if (notify->func != NULL) {
1777 dev_dbg(xpc_chan, "notify->func() called, notify=0x%p, "
1778 "msg_number=%ld, partid=%d, channel=%d\n",
1779 (void *)notify, get, ch->partid, ch->number);
1780
1781 notify->func(reason, ch->partid, ch->number,
1782 notify->key);
1783
1784 dev_dbg(xpc_chan, "notify->func() returned, "
1785 "notify=0x%p, msg_number=%ld, partid=%d, "
1786 "channel=%d\n", (void *)notify, get,
1787 ch->partid, ch->number);
1788 }
1789 }
1790}
1791
1792static void
1793xpc_notify_senders_of_disconnect_sn2(struct xpc_channel *ch)
1794{
1795 xpc_notify_senders_sn2(ch, ch->reason, ch->sn.sn2.w_local_GP.put);
1796}
1797
1798/*
1799 * Clear some of the msg flags in the local message queue.
1800 */
1801static inline void
1802xpc_clear_local_msgqueue_flags_sn2(struct xpc_channel *ch)
1803{
1804 struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
1805 struct xpc_msg *msg;
1806 s64 get;
1807
1808 get = ch_sn2->w_remote_GP.get;
1809 do {
1810 msg = (struct xpc_msg *)((u64)ch->local_msgqueue +
1811 (get % ch->local_nentries) *
1812 ch->msg_size);
1813 msg->flags = 0;
1814 } while (++get < ch_sn2->remote_GP.get);
1815}
1816
1817/*
1818 * Clear some of the msg flags in the remote message queue.
1819 */
1820static inline void
1821xpc_clear_remote_msgqueue_flags_sn2(struct xpc_channel *ch)
1822{
1823 struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
1824 struct xpc_msg *msg;
1825 s64 put;
1826
1827 put = ch_sn2->w_remote_GP.put;
1828 do {
1829 msg = (struct xpc_msg *)((u64)ch->remote_msgqueue +
1830 (put % ch->remote_nentries) *
1831 ch->msg_size);
1832 msg->flags = 0;
1833 } while (++put < ch_sn2->remote_GP.put);
1834}
1835
1836static void
Dean Nelson7fb5e592008-07-29 22:34:10 -07001837xpc_process_msg_chctl_flags_sn2(struct xpc_partition *part, int ch_number)
Dean Nelsona47d5da2008-07-29 22:34:09 -07001838{
1839 struct xpc_channel *ch = &part->channels[ch_number];
1840 struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
1841 int nmsgs_sent;
1842
1843 ch_sn2->remote_GP = part->sn.sn2.remote_GPs[ch_number];
1844
1845 /* See what, if anything, has changed for each connected channel */
1846
1847 xpc_msgqueue_ref(ch);
1848
1849 if (ch_sn2->w_remote_GP.get == ch_sn2->remote_GP.get &&
1850 ch_sn2->w_remote_GP.put == ch_sn2->remote_GP.put) {
1851 /* nothing changed since GPs were last pulled */
1852 xpc_msgqueue_deref(ch);
1853 return;
1854 }
1855
1856 if (!(ch->flags & XPC_C_CONNECTED)) {
1857 xpc_msgqueue_deref(ch);
1858 return;
1859 }
1860
1861 /*
1862 * First check to see if messages recently sent by us have been
1863 * received by the other side. (The remote GET value will have
1864 * changed since we last looked at it.)
1865 */
1866
1867 if (ch_sn2->w_remote_GP.get != ch_sn2->remote_GP.get) {
1868
1869 /*
1870 * We need to notify any senders that want to be notified
1871 * that their sent messages have been received by their
1872 * intended recipients. We need to do this before updating
1873 * w_remote_GP.get so that we don't allocate the same message
1874 * queue entries prematurely (see xpc_allocate_msg()).
1875 */
1876 if (atomic_read(&ch->n_to_notify) > 0) {
1877 /*
1878 * Notify senders that messages sent have been
1879 * received and delivered by the other side.
1880 */
1881 xpc_notify_senders_sn2(ch, xpMsgDelivered,
1882 ch_sn2->remote_GP.get);
1883 }
1884
1885 /*
1886 * Clear msg->flags in previously sent messages, so that
1887 * they're ready for xpc_allocate_msg().
1888 */
1889 xpc_clear_local_msgqueue_flags_sn2(ch);
1890
1891 ch_sn2->w_remote_GP.get = ch_sn2->remote_GP.get;
1892
1893 dev_dbg(xpc_chan, "w_remote_GP.get changed to %ld, partid=%d, "
1894 "channel=%d\n", ch_sn2->w_remote_GP.get, ch->partid,
1895 ch->number);
1896
1897 /*
1898 * If anyone was waiting for message queue entries to become
1899 * available, wake them up.
1900 */
1901 if (atomic_read(&ch->n_on_msg_allocate_wq) > 0)
1902 wake_up(&ch->msg_allocate_wq);
1903 }
1904
1905 /*
1906 * Now check for newly sent messages by the other side. (The remote
1907 * PUT value will have changed since we last looked at it.)
1908 */
1909
1910 if (ch_sn2->w_remote_GP.put != ch_sn2->remote_GP.put) {
1911 /*
1912 * Clear msg->flags in previously received messages, so that
1913 * they're ready for xpc_get_deliverable_msg().
1914 */
1915 xpc_clear_remote_msgqueue_flags_sn2(ch);
1916
1917 ch_sn2->w_remote_GP.put = ch_sn2->remote_GP.put;
1918
1919 dev_dbg(xpc_chan, "w_remote_GP.put changed to %ld, partid=%d, "
1920 "channel=%d\n", ch_sn2->w_remote_GP.put, ch->partid,
1921 ch->number);
1922
1923 nmsgs_sent = ch_sn2->w_remote_GP.put - ch_sn2->w_local_GP.get;
1924 if (nmsgs_sent > 0) {
1925 dev_dbg(xpc_chan, "msgs waiting to be copied and "
1926 "delivered=%d, partid=%d, channel=%d\n",
1927 nmsgs_sent, ch->partid, ch->number);
1928
1929 if (ch->flags & XPC_C_CONNECTEDCALLOUT_MADE)
1930 xpc_activate_kthreads(ch, nmsgs_sent);
1931 }
1932 }
1933
1934 xpc_msgqueue_deref(ch);
1935}
1936
Dean Nelsone17d4162008-07-29 22:34:06 -07001937static struct xpc_msg *
1938xpc_pull_remote_msg_sn2(struct xpc_channel *ch, s64 get)
1939{
1940 struct xpc_partition *part = &xpc_partitions[ch->partid];
Dean Nelsona47d5da2008-07-29 22:34:09 -07001941 struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
Dean Nelsone17d4162008-07-29 22:34:06 -07001942 struct xpc_msg *remote_msg, *msg;
1943 u32 msg_index, nmsgs;
1944 u64 msg_offset;
1945 enum xp_retval ret;
1946
Dean Nelsona47d5da2008-07-29 22:34:09 -07001947 if (mutex_lock_interruptible(&ch_sn2->msg_to_pull_mutex) != 0) {
Dean Nelsone17d4162008-07-29 22:34:06 -07001948 /* we were interrupted by a signal */
1949 return NULL;
1950 }
1951
Dean Nelsona47d5da2008-07-29 22:34:09 -07001952 while (get >= ch_sn2->next_msg_to_pull) {
Dean Nelsone17d4162008-07-29 22:34:06 -07001953
1954 /* pull as many messages as are ready and able to be pulled */
1955
Dean Nelsona47d5da2008-07-29 22:34:09 -07001956 msg_index = ch_sn2->next_msg_to_pull % ch->remote_nentries;
Dean Nelsone17d4162008-07-29 22:34:06 -07001957
Dean Nelsona47d5da2008-07-29 22:34:09 -07001958 DBUG_ON(ch_sn2->next_msg_to_pull >= ch_sn2->w_remote_GP.put);
1959 nmsgs = ch_sn2->w_remote_GP.put - ch_sn2->next_msg_to_pull;
Dean Nelsone17d4162008-07-29 22:34:06 -07001960 if (msg_index + nmsgs > ch->remote_nentries) {
1961 /* ignore the ones that wrap the msg queue for now */
1962 nmsgs = ch->remote_nentries - msg_index;
1963 }
1964
1965 msg_offset = msg_index * ch->msg_size;
1966 msg = (struct xpc_msg *)((u64)ch->remote_msgqueue + msg_offset);
1967 remote_msg = (struct xpc_msg *)(ch->remote_msgqueue_pa +
1968 msg_offset);
1969
1970 ret = xpc_pull_remote_cachelines_sn2(part, msg, remote_msg,
1971 nmsgs * ch->msg_size);
1972 if (ret != xpSuccess) {
1973
1974 dev_dbg(xpc_chan, "failed to pull %d msgs starting with"
1975 " msg %ld from partition %d, channel=%d, "
Dean Nelsona47d5da2008-07-29 22:34:09 -07001976 "ret=%d\n", nmsgs, ch_sn2->next_msg_to_pull,
Dean Nelsone17d4162008-07-29 22:34:06 -07001977 ch->partid, ch->number, ret);
1978
1979 XPC_DEACTIVATE_PARTITION(part, ret);
1980
Dean Nelsona47d5da2008-07-29 22:34:09 -07001981 mutex_unlock(&ch_sn2->msg_to_pull_mutex);
Dean Nelsone17d4162008-07-29 22:34:06 -07001982 return NULL;
1983 }
1984
Dean Nelsona47d5da2008-07-29 22:34:09 -07001985 ch_sn2->next_msg_to_pull += nmsgs;
Dean Nelsone17d4162008-07-29 22:34:06 -07001986 }
1987
Dean Nelsona47d5da2008-07-29 22:34:09 -07001988 mutex_unlock(&ch_sn2->msg_to_pull_mutex);
Dean Nelsone17d4162008-07-29 22:34:06 -07001989
1990 /* return the message we were looking for */
1991 msg_offset = (get % ch->remote_nentries) * ch->msg_size;
1992 msg = (struct xpc_msg *)((u64)ch->remote_msgqueue + msg_offset);
1993
1994 return msg;
1995}
1996
Dean Nelsona47d5da2008-07-29 22:34:09 -07001997static int
1998xpc_n_of_deliverable_msgs_sn2(struct xpc_channel *ch)
1999{
2000 return ch->sn.sn2.w_remote_GP.put - ch->sn.sn2.w_local_GP.get;
2001}
2002
Dean Nelsone17d4162008-07-29 22:34:06 -07002003/*
2004 * Get a message to be delivered.
2005 */
2006static struct xpc_msg *
2007xpc_get_deliverable_msg_sn2(struct xpc_channel *ch)
2008{
Dean Nelsona47d5da2008-07-29 22:34:09 -07002009 struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
Dean Nelsone17d4162008-07-29 22:34:06 -07002010 struct xpc_msg *msg = NULL;
2011 s64 get;
2012
2013 do {
2014 if (ch->flags & XPC_C_DISCONNECTING)
2015 break;
2016
Dean Nelsona47d5da2008-07-29 22:34:09 -07002017 get = ch_sn2->w_local_GP.get;
Dean Nelsone17d4162008-07-29 22:34:06 -07002018 rmb(); /* guarantee that .get loads before .put */
Dean Nelsona47d5da2008-07-29 22:34:09 -07002019 if (get == ch_sn2->w_remote_GP.put)
Dean Nelsone17d4162008-07-29 22:34:06 -07002020 break;
2021
2022 /* There are messages waiting to be pulled and delivered.
2023 * We need to try to secure one for ourselves. We'll do this
2024 * by trying to increment w_local_GP.get and hope that no one
2025 * else beats us to it. If they do, we'll we'll simply have
2026 * to try again for the next one.
2027 */
2028
Dean Nelsona47d5da2008-07-29 22:34:09 -07002029 if (cmpxchg(&ch_sn2->w_local_GP.get, get, get + 1) == get) {
Dean Nelsone17d4162008-07-29 22:34:06 -07002030 /* we got the entry referenced by get */
2031
2032 dev_dbg(xpc_chan, "w_local_GP.get changed to %ld, "
2033 "partid=%d, channel=%d\n", get + 1,
2034 ch->partid, ch->number);
2035
2036 /* pull the message from the remote partition */
2037
2038 msg = xpc_pull_remote_msg_sn2(ch, get);
2039
2040 DBUG_ON(msg != NULL && msg->number != get);
2041 DBUG_ON(msg != NULL && (msg->flags & XPC_M_DONE));
2042 DBUG_ON(msg != NULL && !(msg->flags & XPC_M_READY));
2043
2044 break;
2045 }
2046
2047 } while (1);
2048
2049 return msg;
2050}
2051
Dean Nelson33ba3c72008-07-29 22:34:07 -07002052/*
2053 * Now we actually send the messages that are ready to be sent by advancing
Dean Nelson7fb5e592008-07-29 22:34:10 -07002054 * the local message queue's Put value and then send a chctl msgrequest to the
2055 * recipient partition.
Dean Nelson33ba3c72008-07-29 22:34:07 -07002056 */
2057static void
2058xpc_send_msgs_sn2(struct xpc_channel *ch, s64 initial_put)
2059{
Dean Nelsona47d5da2008-07-29 22:34:09 -07002060 struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002061 struct xpc_msg *msg;
2062 s64 put = initial_put + 1;
Dean Nelson7fb5e592008-07-29 22:34:10 -07002063 int send_msgrequest = 0;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002064
2065 while (1) {
2066
2067 while (1) {
Dean Nelsona47d5da2008-07-29 22:34:09 -07002068 if (put == ch_sn2->w_local_GP.put)
Dean Nelson33ba3c72008-07-29 22:34:07 -07002069 break;
2070
2071 msg = (struct xpc_msg *)((u64)ch->local_msgqueue +
2072 (put % ch->local_nentries) *
2073 ch->msg_size);
2074
2075 if (!(msg->flags & XPC_M_READY))
2076 break;
2077
2078 put++;
2079 }
2080
2081 if (put == initial_put) {
2082 /* nothing's changed */
2083 break;
2084 }
2085
Dean Nelsona47d5da2008-07-29 22:34:09 -07002086 if (cmpxchg_rel(&ch_sn2->local_GP->put, initial_put, put) !=
Dean Nelson33ba3c72008-07-29 22:34:07 -07002087 initial_put) {
2088 /* someone else beat us to it */
Dean Nelsona47d5da2008-07-29 22:34:09 -07002089 DBUG_ON(ch_sn2->local_GP->put < initial_put);
Dean Nelson33ba3c72008-07-29 22:34:07 -07002090 break;
2091 }
2092
2093 /* we just set the new value of local_GP->put */
2094
2095 dev_dbg(xpc_chan, "local_GP->put changed to %ld, partid=%d, "
2096 "channel=%d\n", put, ch->partid, ch->number);
2097
Dean Nelson7fb5e592008-07-29 22:34:10 -07002098 send_msgrequest = 1;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002099
2100 /*
2101 * We need to ensure that the message referenced by
2102 * local_GP->put is not XPC_M_READY or that local_GP->put
2103 * equals w_local_GP.put, so we'll go have a look.
2104 */
2105 initial_put = put;
2106 }
2107
Dean Nelson7fb5e592008-07-29 22:34:10 -07002108 if (send_msgrequest)
2109 xpc_send_chctl_msgrequest_sn2(ch);
Dean Nelson33ba3c72008-07-29 22:34:07 -07002110}
2111
2112/*
2113 * Allocate an entry for a message from the message queue associated with the
2114 * specified channel.
2115 */
2116static enum xp_retval
2117xpc_allocate_msg_sn2(struct xpc_channel *ch, u32 flags,
2118 struct xpc_msg **address_of_msg)
2119{
Dean Nelsona47d5da2008-07-29 22:34:09 -07002120 struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002121 struct xpc_msg *msg;
2122 enum xp_retval ret;
2123 s64 put;
2124
Dean Nelson33ba3c72008-07-29 22:34:07 -07002125 /*
2126 * Get the next available message entry from the local message queue.
2127 * If none are available, we'll make sure that we grab the latest
2128 * GP values.
2129 */
2130 ret = xpTimeout;
2131
2132 while (1) {
2133
Dean Nelsona47d5da2008-07-29 22:34:09 -07002134 put = ch_sn2->w_local_GP.put;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002135 rmb(); /* guarantee that .put loads before .get */
Dean Nelsona47d5da2008-07-29 22:34:09 -07002136 if (put - ch_sn2->w_remote_GP.get < ch->local_nentries) {
Dean Nelson33ba3c72008-07-29 22:34:07 -07002137
2138 /* There are available message entries. We need to try
2139 * to secure one for ourselves. We'll do this by trying
2140 * to increment w_local_GP.put as long as someone else
2141 * doesn't beat us to it. If they do, we'll have to
2142 * try again.
2143 */
Dean Nelsona47d5da2008-07-29 22:34:09 -07002144 if (cmpxchg(&ch_sn2->w_local_GP.put, put, put + 1) ==
2145 put) {
Dean Nelson33ba3c72008-07-29 22:34:07 -07002146 /* we got the entry referenced by put */
2147 break;
2148 }
2149 continue; /* try again */
2150 }
2151
2152 /*
2153 * There aren't any available msg entries at this time.
2154 *
2155 * In waiting for a message entry to become available,
Dean Nelson7fb5e592008-07-29 22:34:10 -07002156 * we set a timeout in case the other side is not sending
2157 * completion interrupts. This lets us fake a notify IRQ
2158 * that will cause the notify IRQ handler to fetch the latest
2159 * GP values as if an interrupt was sent by the other side.
Dean Nelson33ba3c72008-07-29 22:34:07 -07002160 */
2161 if (ret == xpTimeout)
Dean Nelson7fb5e592008-07-29 22:34:10 -07002162 xpc_send_chctl_local_msgrequest_sn2(ch);
Dean Nelson33ba3c72008-07-29 22:34:07 -07002163
Dean Nelson97bf1aa2008-07-29 22:34:08 -07002164 if (flags & XPC_NOWAIT)
Dean Nelson33ba3c72008-07-29 22:34:07 -07002165 return xpNoWait;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002166
2167 ret = xpc_allocate_msg_wait(ch);
Dean Nelson97bf1aa2008-07-29 22:34:08 -07002168 if (ret != xpInterrupted && ret != xpTimeout)
Dean Nelson33ba3c72008-07-29 22:34:07 -07002169 return ret;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002170 }
2171
2172 /* get the message's address and initialize it */
2173 msg = (struct xpc_msg *)((u64)ch->local_msgqueue +
2174 (put % ch->local_nentries) * ch->msg_size);
2175
2176 DBUG_ON(msg->flags != 0);
2177 msg->number = put;
2178
2179 dev_dbg(xpc_chan, "w_local_GP.put changed to %ld; msg=0x%p, "
2180 "msg_number=%ld, partid=%d, channel=%d\n", put + 1,
2181 (void *)msg, msg->number, ch->partid, ch->number);
2182
2183 *address_of_msg = msg;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002184 return xpSuccess;
2185}
2186
2187/*
2188 * Common code that does the actual sending of the message by advancing the
Dean Nelson7fb5e592008-07-29 22:34:10 -07002189 * local message queue's Put value and sends a chctl msgrequest to the
2190 * partition the message is being sent to.
Dean Nelson33ba3c72008-07-29 22:34:07 -07002191 */
2192static enum xp_retval
Dean Nelson97bf1aa2008-07-29 22:34:08 -07002193xpc_send_msg_sn2(struct xpc_channel *ch, u32 flags, void *payload,
2194 u16 payload_size, u8 notify_type, xpc_notify_func func,
2195 void *key)
Dean Nelson33ba3c72008-07-29 22:34:07 -07002196{
2197 enum xp_retval ret = xpSuccess;
Dean Nelson97bf1aa2008-07-29 22:34:08 -07002198 struct xpc_msg *msg = msg;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002199 struct xpc_notify *notify = notify;
Dean Nelson97bf1aa2008-07-29 22:34:08 -07002200 s64 msg_number;
2201 s64 put;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002202
2203 DBUG_ON(notify_type == XPC_N_CALL && func == NULL);
Dean Nelson97bf1aa2008-07-29 22:34:08 -07002204
2205 if (XPC_MSG_SIZE(payload_size) > ch->msg_size)
2206 return xpPayloadTooBig;
2207
2208 xpc_msgqueue_ref(ch);
Dean Nelson33ba3c72008-07-29 22:34:07 -07002209
2210 if (ch->flags & XPC_C_DISCONNECTING) {
Dean Nelson97bf1aa2008-07-29 22:34:08 -07002211 ret = ch->reason;
2212 goto out_1;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002213 }
Dean Nelson97bf1aa2008-07-29 22:34:08 -07002214 if (!(ch->flags & XPC_C_CONNECTED)) {
2215 ret = xpNotConnected;
2216 goto out_1;
2217 }
2218
2219 ret = xpc_allocate_msg_sn2(ch, flags, &msg);
2220 if (ret != xpSuccess)
2221 goto out_1;
2222
2223 msg_number = msg->number;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002224
2225 if (notify_type != 0) {
2226 /*
2227 * Tell the remote side to send an ACK interrupt when the
2228 * message has been delivered.
2229 */
2230 msg->flags |= XPC_M_INTERRUPT;
2231
2232 atomic_inc(&ch->n_to_notify);
2233
2234 notify = &ch->notify_queue[msg_number % ch->local_nentries];
2235 notify->func = func;
2236 notify->key = key;
2237 notify->type = notify_type;
2238
2239 /* >>> is a mb() needed here? */
2240
2241 if (ch->flags & XPC_C_DISCONNECTING) {
2242 /*
2243 * An error occurred between our last error check and
2244 * this one. We will try to clear the type field from
2245 * the notify entry. If we succeed then
2246 * xpc_disconnect_channel() didn't already process
2247 * the notify entry.
2248 */
2249 if (cmpxchg(&notify->type, notify_type, 0) ==
2250 notify_type) {
2251 atomic_dec(&ch->n_to_notify);
2252 ret = ch->reason;
2253 }
Dean Nelson97bf1aa2008-07-29 22:34:08 -07002254 goto out_1;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002255 }
2256 }
2257
Dean Nelson97bf1aa2008-07-29 22:34:08 -07002258 memcpy(&msg->payload, payload, payload_size);
2259
Dean Nelson33ba3c72008-07-29 22:34:07 -07002260 msg->flags |= XPC_M_READY;
2261
2262 /*
2263 * The preceding store of msg->flags must occur before the following
Dean Nelsona47d5da2008-07-29 22:34:09 -07002264 * load of local_GP->put.
Dean Nelson33ba3c72008-07-29 22:34:07 -07002265 */
2266 mb();
2267
2268 /* see if the message is next in line to be sent, if so send it */
2269
Dean Nelsona47d5da2008-07-29 22:34:09 -07002270 put = ch->sn.sn2.local_GP->put;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002271 if (put == msg_number)
2272 xpc_send_msgs_sn2(ch, put);
2273
Dean Nelson97bf1aa2008-07-29 22:34:08 -07002274out_1:
Dean Nelson33ba3c72008-07-29 22:34:07 -07002275 xpc_msgqueue_deref(ch);
2276 return ret;
2277}
2278
2279/*
2280 * Now we actually acknowledge the messages that have been delivered and ack'd
2281 * by advancing the cached remote message queue's Get value and if requested
Dean Nelson7fb5e592008-07-29 22:34:10 -07002282 * send a chctl msgrequest to the message sender's partition.
Dean Nelson33ba3c72008-07-29 22:34:07 -07002283 */
2284static void
2285xpc_acknowledge_msgs_sn2(struct xpc_channel *ch, s64 initial_get, u8 msg_flags)
2286{
Dean Nelsona47d5da2008-07-29 22:34:09 -07002287 struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002288 struct xpc_msg *msg;
2289 s64 get = initial_get + 1;
Dean Nelson7fb5e592008-07-29 22:34:10 -07002290 int send_msgrequest = 0;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002291
2292 while (1) {
2293
2294 while (1) {
Dean Nelsona47d5da2008-07-29 22:34:09 -07002295 if (get == ch_sn2->w_local_GP.get)
Dean Nelson33ba3c72008-07-29 22:34:07 -07002296 break;
2297
2298 msg = (struct xpc_msg *)((u64)ch->remote_msgqueue +
2299 (get % ch->remote_nentries) *
2300 ch->msg_size);
2301
2302 if (!(msg->flags & XPC_M_DONE))
2303 break;
2304
2305 msg_flags |= msg->flags;
2306 get++;
2307 }
2308
2309 if (get == initial_get) {
2310 /* nothing's changed */
2311 break;
2312 }
2313
Dean Nelsona47d5da2008-07-29 22:34:09 -07002314 if (cmpxchg_rel(&ch_sn2->local_GP->get, initial_get, get) !=
Dean Nelson33ba3c72008-07-29 22:34:07 -07002315 initial_get) {
2316 /* someone else beat us to it */
Dean Nelsona47d5da2008-07-29 22:34:09 -07002317 DBUG_ON(ch_sn2->local_GP->get <= initial_get);
Dean Nelson33ba3c72008-07-29 22:34:07 -07002318 break;
2319 }
2320
2321 /* we just set the new value of local_GP->get */
2322
2323 dev_dbg(xpc_chan, "local_GP->get changed to %ld, partid=%d, "
2324 "channel=%d\n", get, ch->partid, ch->number);
2325
Dean Nelson7fb5e592008-07-29 22:34:10 -07002326 send_msgrequest = (msg_flags & XPC_M_INTERRUPT);
Dean Nelson33ba3c72008-07-29 22:34:07 -07002327
2328 /*
2329 * We need to ensure that the message referenced by
2330 * local_GP->get is not XPC_M_DONE or that local_GP->get
2331 * equals w_local_GP.get, so we'll go have a look.
2332 */
2333 initial_get = get;
2334 }
2335
Dean Nelson7fb5e592008-07-29 22:34:10 -07002336 if (send_msgrequest)
2337 xpc_send_chctl_msgrequest_sn2(ch);
Dean Nelson33ba3c72008-07-29 22:34:07 -07002338}
2339
2340static void
2341xpc_received_msg_sn2(struct xpc_channel *ch, struct xpc_msg *msg)
2342{
2343 s64 get;
2344 s64 msg_number = msg->number;
2345
2346 dev_dbg(xpc_chan, "msg=0x%p, msg_number=%ld, partid=%d, channel=%d\n",
2347 (void *)msg, msg_number, ch->partid, ch->number);
2348
2349 DBUG_ON((((u64)msg - (u64)ch->remote_msgqueue) / ch->msg_size) !=
2350 msg_number % ch->remote_nentries);
2351 DBUG_ON(msg->flags & XPC_M_DONE);
2352
2353 msg->flags |= XPC_M_DONE;
2354
2355 /*
2356 * The preceding store of msg->flags must occur before the following
Dean Nelsona47d5da2008-07-29 22:34:09 -07002357 * load of local_GP->get.
Dean Nelson33ba3c72008-07-29 22:34:07 -07002358 */
2359 mb();
2360
2361 /*
2362 * See if this message is next in line to be acknowledged as having
2363 * been delivered.
2364 */
Dean Nelsona47d5da2008-07-29 22:34:09 -07002365 get = ch->sn.sn2.local_GP->get;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002366 if (get == msg_number)
2367 xpc_acknowledge_msgs_sn2(ch, get, msg->flags);
2368}
2369
Dean Nelson6e410172008-07-29 22:34:09 -07002370int
Dean Nelson94bd2702008-07-29 22:34:05 -07002371xpc_init_sn2(void)
2372{
Dean Nelson6e410172008-07-29 22:34:09 -07002373 int ret;
Dean Nelsonee6665e2008-07-29 22:34:13 -07002374 size_t buf_size;
Dean Nelson6e410172008-07-29 22:34:09 -07002375
Dean Nelson94bd2702008-07-29 22:34:05 -07002376 xpc_rsvd_page_init = xpc_rsvd_page_init_sn2;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002377 xpc_increment_heartbeat = xpc_increment_heartbeat_sn2;
2378 xpc_offline_heartbeat = xpc_offline_heartbeat_sn2;
2379 xpc_online_heartbeat = xpc_online_heartbeat_sn2;
2380 xpc_heartbeat_init = xpc_heartbeat_init_sn2;
2381 xpc_heartbeat_exit = xpc_heartbeat_exit_sn2;
2382 xpc_check_remote_hb = xpc_check_remote_hb_sn2;
2383
Dean Nelsona47d5da2008-07-29 22:34:09 -07002384 xpc_request_partition_activation = xpc_request_partition_activation_sn2;
2385 xpc_request_partition_reactivation =
2386 xpc_request_partition_reactivation_sn2;
2387 xpc_request_partition_deactivation =
2388 xpc_request_partition_deactivation_sn2;
2389 xpc_cancel_partition_deactivation_request =
2390 xpc_cancel_partition_deactivation_request_sn2;
2391
Dean Nelson6e410172008-07-29 22:34:09 -07002392 xpc_process_activate_IRQ_rcvd = xpc_process_activate_IRQ_rcvd_sn2;
Dean Nelsone17d4162008-07-29 22:34:06 -07002393 xpc_setup_infrastructure = xpc_setup_infrastructure_sn2;
2394 xpc_teardown_infrastructure = xpc_teardown_infrastructure_sn2;
2395 xpc_make_first_contact = xpc_make_first_contact_sn2;
Dean Nelson7fb5e592008-07-29 22:34:10 -07002396 xpc_get_chctl_all_flags = xpc_get_chctl_all_flags_sn2;
Dean Nelson185c3a12008-07-29 22:34:11 -07002397 xpc_allocate_msgqueues = xpc_allocate_msgqueues_sn2;
2398 xpc_free_msgqueues = xpc_free_msgqueues_sn2;
Dean Nelsona47d5da2008-07-29 22:34:09 -07002399 xpc_notify_senders_of_disconnect = xpc_notify_senders_of_disconnect_sn2;
Dean Nelson7fb5e592008-07-29 22:34:10 -07002400 xpc_process_msg_chctl_flags = xpc_process_msg_chctl_flags_sn2;
Dean Nelsona47d5da2008-07-29 22:34:09 -07002401 xpc_n_of_deliverable_msgs = xpc_n_of_deliverable_msgs_sn2;
Dean Nelsone17d4162008-07-29 22:34:06 -07002402 xpc_get_deliverable_msg = xpc_get_deliverable_msg_sn2;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002403
Dean Nelsona47d5da2008-07-29 22:34:09 -07002404 xpc_indicate_partition_engaged = xpc_indicate_partition_engaged_sn2;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002405 xpc_partition_engaged = xpc_partition_engaged_sn2;
Dean Nelsona47d5da2008-07-29 22:34:09 -07002406 xpc_any_partition_engaged = xpc_any_partition_engaged_sn2;
2407 xpc_indicate_partition_disengaged =
2408 xpc_indicate_partition_disengaged_sn2;
2409 xpc_assume_partition_disengaged = xpc_assume_partition_disengaged_sn2;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002410
Dean Nelson7fb5e592008-07-29 22:34:10 -07002411 xpc_send_chctl_closerequest = xpc_send_chctl_closerequest_sn2;
2412 xpc_send_chctl_closereply = xpc_send_chctl_closereply_sn2;
2413 xpc_send_chctl_openrequest = xpc_send_chctl_openrequest_sn2;
2414 xpc_send_chctl_openreply = xpc_send_chctl_openreply_sn2;
Dean Nelson33ba3c72008-07-29 22:34:07 -07002415
Dean Nelson33ba3c72008-07-29 22:34:07 -07002416 xpc_send_msg = xpc_send_msg_sn2;
2417 xpc_received_msg = xpc_received_msg_sn2;
Dean Nelson6e410172008-07-29 22:34:09 -07002418
Dean Nelsonee6665e2008-07-29 22:34:13 -07002419 buf_size = max(XPC_RP_VARS_SIZE,
2420 XPC_RP_HEADER_SIZE + XP_NASID_MASK_BYTES_SN2);
2421 xpc_remote_copy_buffer_sn2 = xpc_kmalloc_cacheline_aligned(buf_size,
2422 GFP_KERNEL,
2423 &xpc_remote_copy_buffer_base_sn2);
2424 if (xpc_remote_copy_buffer_sn2 == NULL) {
2425 dev_err(xpc_part, "can't get memory for remote copy buffer\n");
2426 return -ENOMEM;
2427 }
2428
Dean Nelsonc39838c2008-07-29 22:34:11 -07002429 /* open up protections for IPI and [potentially] amo operations */
Dean Nelson6e410172008-07-29 22:34:09 -07002430 xpc_allow_IPI_ops_sn2();
Dean Nelsonc39838c2008-07-29 22:34:11 -07002431 xpc_allow_amo_ops_shub_wars_1_1_sn2();
Dean Nelson6e410172008-07-29 22:34:09 -07002432
2433 /*
2434 * This is safe to do before the xpc_hb_checker thread has started
2435 * because the handler releases a wait queue. If an interrupt is
2436 * received before the thread is waiting, it will not go to sleep,
2437 * but rather immediately process the interrupt.
2438 */
2439 ret = request_irq(SGI_XPC_ACTIVATE, xpc_handle_activate_IRQ_sn2, 0,
2440 "xpc hb", NULL);
2441 if (ret != 0) {
2442 dev_err(xpc_part, "can't register ACTIVATE IRQ handler, "
2443 "errno=%d\n", -ret);
2444 xpc_disallow_IPI_ops_sn2();
Dean Nelsonee6665e2008-07-29 22:34:13 -07002445 kfree(xpc_remote_copy_buffer_base_sn2);
Dean Nelson6e410172008-07-29 22:34:09 -07002446 }
2447 return ret;
Dean Nelson94bd2702008-07-29 22:34:05 -07002448}
2449
2450void
2451xpc_exit_sn2(void)
2452{
Dean Nelson6e410172008-07-29 22:34:09 -07002453 free_irq(SGI_XPC_ACTIVATE, NULL);
2454 xpc_disallow_IPI_ops_sn2();
Dean Nelsonee6665e2008-07-29 22:34:13 -07002455 kfree(xpc_remote_copy_buffer_base_sn2);
Dean Nelson94bd2702008-07-29 22:34:05 -07002456}