]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/misc/sgi-xp/xpc_sn2.c
sgi-xp: add 'jiffies' to reserved page's timestamp name
[linux-2.6-omap-h63xx.git] / drivers / misc / sgi-xp / xpc_sn2.c
1 /*
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>
17 #include <linux/delay.h>
18 #include <asm/uncached.h>
19 #include <asm/sn/sn_sal.h>
20 #include "xpc.h"
21
22 /*
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  */
56 static char *xpc_remote_copy_buffer_sn2;
57 static void *xpc_remote_copy_buffer_base_sn2;
58
59 static struct xpc_vars_sn2 *xpc_vars_sn2;
60 static struct xpc_vars_part_sn2 *xpc_vars_part_sn2;
61
62 /* SH_IPI_ACCESS shub register value on startup */
63 static u64 xpc_sh1_IPI_access_sn2;
64 static u64 xpc_sh2_IPI_access0_sn2;
65 static u64 xpc_sh2_IPI_access1_sn2;
66 static u64 xpc_sh2_IPI_access2_sn2;
67 static u64 xpc_sh2_IPI_access3_sn2;
68
69 /*
70  * Change protections to allow IPI operations.
71  */
72 static void
73 xpc_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_sn2 =
81                     (u64)HUB_L((u64 *)LOCAL_MMR_ADDR(SH2_IPI_ACCESS0));
82                 xpc_sh2_IPI_access1_sn2 =
83                     (u64)HUB_L((u64 *)LOCAL_MMR_ADDR(SH2_IPI_ACCESS1));
84                 xpc_sh2_IPI_access2_sn2 =
85                     (u64)HUB_L((u64 *)LOCAL_MMR_ADDR(SH2_IPI_ACCESS2));
86                 xpc_sh2_IPI_access3_sn2 =
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_sn2 =
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  */
115 static void
116 xpc_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_sn2);
127                         HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS1),
128                               xpc_sh2_IPI_access1_sn2);
129                         HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS2),
130                               xpc_sh2_IPI_access2_sn2);
131                         HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid, SH2_IPI_ACCESS3),
132                               xpc_sh2_IPI_access3_sn2);
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_sn2);
139                 }
140         }
141 }
142
143 /*
144  * 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).
148  */
149
150 static u64
151 xpc_receive_IRQ_amo_sn2(struct amo *amo)
152 {
153         return FETCHOP_LOAD_OP(TO_AMO((u64)&amo->variable), FETCHOP_CLEAR);
154 }
155
156 static enum xp_retval
157 xpc_send_IRQ_sn2(struct amo *amo, u64 flag, int nasid, int phys_cpuid,
158                  int vector)
159 {
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
172          * keep sending IRQs and amos to it until the heartbeat times out.
173          */
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
182 static struct amo *
183 xpc_init_IRQ_amo_sn2(int index)
184 {
185         struct amo *amo = xpc_vars_sn2->amos_page + index;
186
187         (void)xpc_receive_IRQ_amo_sn2(amo);     /* clear amo variable */
188         return amo;
189 }
190
191 /*
192  * Functions associated with SGI_XPC_ACTIVATE IRQ.
193  */
194
195 /*
196  * Notify the heartbeat check thread that an activate IRQ has been received.
197  */
198 static irqreturn_t
199 xpc_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 /*
207  * Flag the appropriate amo variable and send an IRQ to the specified node.
208  */
209 static void
210 xpc_send_activate_IRQ_sn2(u64 amos_page_pa, int from_nasid, int to_nasid,
211                           int to_phys_cpuid)
212 {
213         struct amo *amos = (struct amo *)__va(amos_page_pa +
214                                               (XPC_ACTIVATE_IRQ_AMOS_SN2 *
215                                               sizeof(struct amo)));
216
217         (void)xpc_send_IRQ_sn2(&amos[BIT_WORD(from_nasid / 2)],
218                                BIT_MASK(from_nasid / 2), to_nasid,
219                                to_phys_cpuid, SGI_XPC_ACTIVATE);
220 }
221
222 static void
223 xpc_send_local_activate_IRQ_sn2(int from_nasid)
224 {
225         struct amo *amos = (struct amo *)__va(xpc_vars_sn2->amos_page_pa +
226                                               (XPC_ACTIVATE_IRQ_AMOS_SN2 *
227                                               sizeof(struct amo)));
228
229         /* fake the sending and receipt of an activate IRQ from remote nasid */
230         FETCHOP_STORE_OP(TO_AMO((u64)&amos[BIT_WORD(from_nasid / 2)].variable),
231                          FETCHOP_OR, BIT_MASK(from_nasid / 2));
232
233         atomic_inc(&xpc_activate_IRQ_rcvd);
234         wake_up_interruptible(&xpc_activate_IRQ_wq);
235 }
236
237 /*
238  * Functions associated with SGI_XPC_NOTIFY IRQ.
239  */
240
241 /*
242  * Check to see if any chctl flags were sent from the specified partition.
243  */
244 static void
245 xpc_check_for_sent_chctl_flags_sn2(struct xpc_partition *part)
246 {
247         union xpc_channel_ctl_flags chctl;
248         unsigned long irq_flags;
249
250         chctl.all_flags = xpc_receive_IRQ_amo_sn2(part->sn.sn2.
251                                                   local_chctl_amo_va);
252         if (chctl.all_flags == 0)
253                 return;
254
255         spin_lock_irqsave(&part->chctl_lock, irq_flags);
256         part->chctl.all_flags |= chctl.all_flags;
257         spin_unlock_irqrestore(&part->chctl_lock, irq_flags);
258
259         dev_dbg(xpc_chan, "received notify IRQ from partid=%d, chctl.all_flags="
260                 "0x%lx\n", XPC_PARTID(part), chctl.all_flags);
261
262         xpc_wakeup_channel_mgr(part);
263 }
264
265 /*
266  * Handle the receipt of a SGI_XPC_NOTIFY IRQ by seeing whether the specified
267  * partition actually sent it. Since SGI_XPC_NOTIFY IRQs may be shared by more
268  * than one partition, we use an amo structure per partition to indicate
269  * whether a partition has sent an IRQ or not.  If it has, then wake up the
270  * associated kthread to handle it.
271  *
272  * All SGI_XPC_NOTIFY IRQs received by XPC are the result of IRQs sent by XPC
273  * running on other partitions.
274  *
275  * Noteworthy Arguments:
276  *
277  *      irq - Interrupt ReQuest number. NOT USED.
278  *
279  *      dev_id - partid of IRQ's potential sender.
280  */
281 static irqreturn_t
282 xpc_handle_notify_IRQ_sn2(int irq, void *dev_id)
283 {
284         short partid = (short)(u64)dev_id;
285         struct xpc_partition *part = &xpc_partitions[partid];
286
287         DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
288
289         if (xpc_part_ref(part)) {
290                 xpc_check_for_sent_chctl_flags_sn2(part);
291
292                 xpc_part_deref(part);
293         }
294         return IRQ_HANDLED;
295 }
296
297 /*
298  * Check to see if xpc_handle_notify_IRQ_sn2() dropped any IRQs on the floor
299  * because the write to their associated amo variable completed after the IRQ
300  * was received.
301  */
302 static void
303 xpc_check_for_dropped_notify_IRQ_sn2(struct xpc_partition *part)
304 {
305         struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
306
307         if (xpc_part_ref(part)) {
308                 xpc_check_for_sent_chctl_flags_sn2(part);
309
310                 part_sn2->dropped_notify_IRQ_timer.expires = jiffies +
311                     XPC_DROPPED_NOTIFY_IRQ_WAIT_INTERVAL;
312                 add_timer(&part_sn2->dropped_notify_IRQ_timer);
313                 xpc_part_deref(part);
314         }
315 }
316
317 /*
318  * Send a notify IRQ to the remote partition that is associated with the
319  * specified channel.
320  */
321 static void
322 xpc_send_notify_IRQ_sn2(struct xpc_channel *ch, u8 chctl_flag,
323                         char *chctl_flag_string, unsigned long *irq_flags)
324 {
325         struct xpc_partition *part = &xpc_partitions[ch->partid];
326         struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
327         union xpc_channel_ctl_flags chctl = { 0 };
328         enum xp_retval ret;
329
330         if (likely(part->act_state != XPC_P_DEACTIVATING)) {
331                 chctl.flags[ch->number] = chctl_flag;
332                 ret = xpc_send_IRQ_sn2(part_sn2->remote_chctl_amo_va,
333                                        chctl.all_flags,
334                                        part_sn2->notify_IRQ_nasid,
335                                        part_sn2->notify_IRQ_phys_cpuid,
336                                        SGI_XPC_NOTIFY);
337                 dev_dbg(xpc_chan, "%s sent to partid=%d, channel=%d, ret=%d\n",
338                         chctl_flag_string, ch->partid, ch->number, ret);
339                 if (unlikely(ret != xpSuccess)) {
340                         if (irq_flags != NULL)
341                                 spin_unlock_irqrestore(&ch->lock, *irq_flags);
342                         XPC_DEACTIVATE_PARTITION(part, ret);
343                         if (irq_flags != NULL)
344                                 spin_lock_irqsave(&ch->lock, *irq_flags);
345                 }
346         }
347 }
348
349 #define XPC_SEND_NOTIFY_IRQ_SN2(_ch, _ipi_f, _irq_f) \
350                 xpc_send_notify_IRQ_sn2(_ch, _ipi_f, #_ipi_f, _irq_f)
351
352 /*
353  * Make it look like the remote partition, which is associated with the
354  * specified channel, sent us a notify IRQ. This faked IRQ will be handled
355  * by xpc_check_for_dropped_notify_IRQ_sn2().
356  */
357 static void
358 xpc_send_local_notify_IRQ_sn2(struct xpc_channel *ch, u8 chctl_flag,
359                               char *chctl_flag_string)
360 {
361         struct xpc_partition *part = &xpc_partitions[ch->partid];
362         union xpc_channel_ctl_flags chctl = { 0 };
363
364         chctl.flags[ch->number] = chctl_flag;
365         FETCHOP_STORE_OP(TO_AMO((u64)&part->sn.sn2.local_chctl_amo_va->
366                                 variable), FETCHOP_OR, chctl.all_flags);
367         dev_dbg(xpc_chan, "%s sent local from partid=%d, channel=%d\n",
368                 chctl_flag_string, ch->partid, ch->number);
369 }
370
371 #define XPC_SEND_LOCAL_NOTIFY_IRQ_SN2(_ch, _ipi_f) \
372                 xpc_send_local_notify_IRQ_sn2(_ch, _ipi_f, #_ipi_f)
373
374 static void
375 xpc_send_chctl_closerequest_sn2(struct xpc_channel *ch,
376                                 unsigned long *irq_flags)
377 {
378         struct xpc_openclose_args *args = ch->local_openclose_args;
379
380         args->reason = ch->reason;
381         XPC_SEND_NOTIFY_IRQ_SN2(ch, XPC_CHCTL_CLOSEREQUEST, irq_flags);
382 }
383
384 static void
385 xpc_send_chctl_closereply_sn2(struct xpc_channel *ch, unsigned long *irq_flags)
386 {
387         XPC_SEND_NOTIFY_IRQ_SN2(ch, XPC_CHCTL_CLOSEREPLY, irq_flags);
388 }
389
390 static void
391 xpc_send_chctl_openrequest_sn2(struct xpc_channel *ch, unsigned long *irq_flags)
392 {
393         struct xpc_openclose_args *args = ch->local_openclose_args;
394
395         args->msg_size = ch->msg_size;
396         args->local_nentries = ch->local_nentries;
397         XPC_SEND_NOTIFY_IRQ_SN2(ch, XPC_CHCTL_OPENREQUEST, irq_flags);
398 }
399
400 static void
401 xpc_send_chctl_openreply_sn2(struct xpc_channel *ch, unsigned long *irq_flags)
402 {
403         struct xpc_openclose_args *args = ch->local_openclose_args;
404
405         args->remote_nentries = ch->remote_nentries;
406         args->local_nentries = ch->local_nentries;
407         args->local_msgqueue_pa = __pa(ch->local_msgqueue);
408         XPC_SEND_NOTIFY_IRQ_SN2(ch, XPC_CHCTL_OPENREPLY, irq_flags);
409 }
410
411 static void
412 xpc_send_chctl_msgrequest_sn2(struct xpc_channel *ch)
413 {
414         XPC_SEND_NOTIFY_IRQ_SN2(ch, XPC_CHCTL_MSGREQUEST, NULL);
415 }
416
417 static void
418 xpc_send_chctl_local_msgrequest_sn2(struct xpc_channel *ch)
419 {
420         XPC_SEND_LOCAL_NOTIFY_IRQ_SN2(ch, XPC_CHCTL_MSGREQUEST);
421 }
422
423 /*
424  * This next set of functions are used to keep track of when a partition is
425  * potentially engaged in accessing memory belonging to another partition.
426  */
427
428 static void
429 xpc_indicate_partition_engaged_sn2(struct xpc_partition *part)
430 {
431         unsigned long irq_flags;
432         struct amo *amo = (struct amo *)__va(part->sn.sn2.remote_amos_page_pa +
433                                              (XPC_ENGAGED_PARTITIONS_AMO_SN2 *
434                                              sizeof(struct amo)));
435
436         local_irq_save(irq_flags);
437
438         /* set bit corresponding to our partid in remote partition's amo */
439         FETCHOP_STORE_OP(TO_AMO((u64)&amo->variable), FETCHOP_OR,
440                          BIT(sn_partition_id));
441
442         /*
443          * We must always use the nofault function regardless of whether we
444          * are on a Shub 1.1 system or a Shub 1.2 slice 0xc processor. If we
445          * didn't, we'd never know that the other partition is down and would
446          * keep sending IRQs and amos to it until the heartbeat times out.
447          */
448         (void)xp_nofault_PIOR((u64 *)GLOBAL_MMR_ADDR(NASID_GET(&amo->
449                                                                variable),
450                                                      xp_nofault_PIOR_target));
451
452         local_irq_restore(irq_flags);
453 }
454
455 static void
456 xpc_indicate_partition_disengaged_sn2(struct xpc_partition *part)
457 {
458         struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
459         unsigned long irq_flags;
460         struct amo *amo = (struct amo *)__va(part_sn2->remote_amos_page_pa +
461                                              (XPC_ENGAGED_PARTITIONS_AMO_SN2 *
462                                              sizeof(struct amo)));
463
464         local_irq_save(irq_flags);
465
466         /* clear bit corresponding to our partid in remote partition's amo */
467         FETCHOP_STORE_OP(TO_AMO((u64)&amo->variable), FETCHOP_AND,
468                          ~BIT(sn_partition_id));
469
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
474          * keep sending IRQs and amos to it until the heartbeat times out.
475          */
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);
481
482         /*
483          * Send activate IRQ to get other side to see that we've cleared our
484          * bit in their engaged partitions amo.
485          */
486         xpc_send_activate_IRQ_sn2(part_sn2->remote_amos_page_pa,
487                                   cnodeid_to_nasid(0),
488                                   part_sn2->activate_IRQ_nasid,
489                                   part_sn2->activate_IRQ_phys_cpuid);
490 }
491
492 static int
493 xpc_partition_engaged_sn2(short partid)
494 {
495         struct amo *amo = xpc_vars_sn2->amos_page +
496                           XPC_ENGAGED_PARTITIONS_AMO_SN2;
497
498         /* our partition's amo variable ANDed with partid mask */
499         return (FETCHOP_LOAD_OP(TO_AMO((u64)&amo->variable), FETCHOP_LOAD) &
500                 BIT(partid)) != 0;
501 }
502
503 static int
504 xpc_any_partition_engaged_sn2(void)
505 {
506         struct amo *amo = xpc_vars_sn2->amos_page +
507                           XPC_ENGAGED_PARTITIONS_AMO_SN2;
508
509         /* our partition's amo variable */
510         return FETCHOP_LOAD_OP(TO_AMO((u64)&amo->variable), FETCHOP_LOAD) != 0;
511 }
512
513 static void
514 xpc_assume_partition_disengaged_sn2(short partid)
515 {
516         struct amo *amo = xpc_vars_sn2->amos_page +
517                           XPC_ENGAGED_PARTITIONS_AMO_SN2;
518
519         /* clear bit(s) based on partid mask in our partition's amo */
520         FETCHOP_STORE_OP(TO_AMO((u64)&amo->variable), FETCHOP_AND,
521                          ~BIT(partid));
522 }
523
524 /* original protection values for each node */
525 static u64 xpc_prot_vec_sn2[MAX_NUMNODES];
526
527 /*
528  * Change protections to allow amo operations on non-Shub 1.1 systems.
529  */
530 static enum xp_retval
531 xpc_allow_amo_ops_sn2(struct amo *amos_page)
532 {
533         u64 nasid_array = 0;
534         int ret;
535
536         /*
537          * On SHUB 1.1, we cannot call sn_change_memprotect() since the BIST
538          * collides with memory operations. On those systems we call
539          * xpc_allow_amo_ops_shub_wars_1_1_sn2() instead.
540          */
541         if (!enable_shub_wars_1_1()) {
542                 ret = sn_change_memprotect(ia64_tpa((u64)amos_page), PAGE_SIZE,
543                                            SN_MEMPROT_ACCESS_CLASS_1,
544                                            &nasid_array);
545                 if (ret != 0)
546                         return xpSalError;
547         }
548         return xpSuccess;
549 }
550
551 /*
552  * Change protections to allow amo operations on Shub 1.1 systems.
553  */
554 static void
555 xpc_allow_amo_ops_shub_wars_1_1_sn2(void)
556 {
557         int node;
558         int nasid;
559
560         if (!enable_shub_wars_1_1())
561                 return;
562
563         for_each_online_node(node) {
564                 nasid = cnodeid_to_nasid(node);
565                 /* save current protection values */
566                 xpc_prot_vec_sn2[node] =
567                     (u64)HUB_L((u64 *)GLOBAL_MMR_ADDR(nasid,
568                                                   SH1_MD_DQLP_MMR_DIR_PRIVEC0));
569                 /* open up everything */
570                 HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid,
571                                              SH1_MD_DQLP_MMR_DIR_PRIVEC0),
572                       -1UL);
573                 HUB_S((u64 *)GLOBAL_MMR_ADDR(nasid,
574                                              SH1_MD_DQRP_MMR_DIR_PRIVEC0),
575                       -1UL);
576         }
577 }
578
579 static enum xp_retval
580 xpc_rsvd_page_init_sn2(struct xpc_rsvd_page *rp)
581 {
582         struct amo *amos_page;
583         int i;
584         int ret;
585
586         xpc_vars_sn2 = XPC_RP_VARS(rp);
587
588         rp->sn.vars_pa = __pa(xpc_vars_sn2);
589
590         /* vars_part array follows immediately after vars */
591         xpc_vars_part_sn2 = (struct xpc_vars_part_sn2 *)((u8 *)XPC_RP_VARS(rp) +
592                                                          XPC_RP_VARS_SIZE);
593
594         /*
595          * Before clearing xpc_vars_sn2, see if a page of amos had been
596          * previously allocated. If not we'll need to allocate one and set
597          * permissions so that cross-partition amos are allowed.
598          *
599          * The allocated amo page needs MCA reporting to remain disabled after
600          * XPC has unloaded.  To make this work, we keep a copy of the pointer
601          * to this page (i.e., amos_page) in the struct xpc_vars_sn2 structure,
602          * which is pointed to by the reserved page, and re-use that saved copy
603          * on subsequent loads of XPC. This amo page is never freed, and its
604          * memory protections are never restricted.
605          */
606         amos_page = xpc_vars_sn2->amos_page;
607         if (amos_page == NULL) {
608                 amos_page = (struct amo *)TO_AMO(uncached_alloc_page(0, 1));
609                 if (amos_page == NULL) {
610                         dev_err(xpc_part, "can't allocate page of amos\n");
611                         return xpNoMemory;
612                 }
613
614                 /*
615                  * Open up amo-R/W to cpu.  This is done on Shub 1.1 systems
616                  * when xpc_allow_amo_ops_shub_wars_1_1_sn2() is called.
617                  */
618                 ret = xpc_allow_amo_ops_sn2(amos_page);
619                 if (ret != xpSuccess) {
620                         dev_err(xpc_part, "can't allow amo operations\n");
621                         uncached_free_page(__IA64_UNCACHED_OFFSET |
622                                            TO_PHYS((u64)amos_page), 1);
623                         return ret;
624                 }
625         }
626
627         /* clear xpc_vars_sn2 */
628         memset(xpc_vars_sn2, 0, sizeof(struct xpc_vars_sn2));
629
630         xpc_vars_sn2->version = XPC_V_VERSION;
631         xpc_vars_sn2->activate_IRQ_nasid = cpuid_to_nasid(0);
632         xpc_vars_sn2->activate_IRQ_phys_cpuid = cpu_physical_id(0);
633         xpc_vars_sn2->vars_part_pa = __pa(xpc_vars_part_sn2);
634         xpc_vars_sn2->amos_page_pa = ia64_tpa((u64)amos_page);
635         xpc_vars_sn2->amos_page = amos_page;    /* save for next load of XPC */
636
637         /* clear xpc_vars_part_sn2 */
638         memset((u64 *)xpc_vars_part_sn2, 0, sizeof(struct xpc_vars_part_sn2) *
639                xp_max_npartitions);
640
641         /* initialize the activate IRQ related amo variables */
642         for (i = 0; i < xpc_nasid_mask_nlongs; i++)
643                 (void)xpc_init_IRQ_amo_sn2(XPC_ACTIVATE_IRQ_AMOS_SN2 + i);
644
645         /* initialize the engaged remote partitions related amo variables */
646         (void)xpc_init_IRQ_amo_sn2(XPC_ENGAGED_PARTITIONS_AMO_SN2);
647         (void)xpc_init_IRQ_amo_sn2(XPC_DEACTIVATE_REQUEST_AMO_SN2);
648
649         return xpSuccess;
650 }
651
652 static void
653 xpc_increment_heartbeat_sn2(void)
654 {
655         xpc_vars_sn2->heartbeat++;
656 }
657
658 static void
659 xpc_offline_heartbeat_sn2(void)
660 {
661         xpc_increment_heartbeat_sn2();
662         xpc_vars_sn2->heartbeat_offline = 1;
663 }
664
665 static void
666 xpc_online_heartbeat_sn2(void)
667 {
668         xpc_increment_heartbeat_sn2();
669         xpc_vars_sn2->heartbeat_offline = 0;
670 }
671
672 static void
673 xpc_heartbeat_init_sn2(void)
674 {
675         DBUG_ON(xpc_vars_sn2 == NULL);
676
677         bitmap_zero(xpc_vars_sn2->heartbeating_to_mask, XP_MAX_NPARTITIONS_SN2);
678         xpc_heartbeating_to_mask = &xpc_vars_sn2->heartbeating_to_mask[0];
679         xpc_online_heartbeat_sn2();
680 }
681
682 static void
683 xpc_heartbeat_exit_sn2(void)
684 {
685         xpc_offline_heartbeat_sn2();
686 }
687
688 /*
689  * At periodic intervals, scan through all active partitions and ensure
690  * their heartbeat is still active.  If not, the partition is deactivated.
691  */
692 static void
693 xpc_check_remote_hb_sn2(void)
694 {
695         struct xpc_vars_sn2 *remote_vars;
696         struct xpc_partition *part;
697         short partid;
698         enum xp_retval ret;
699
700         remote_vars = (struct xpc_vars_sn2 *)xpc_remote_copy_buffer_sn2;
701
702         for (partid = 0; partid < xp_max_npartitions; partid++) {
703
704                 if (xpc_exiting)
705                         break;
706
707                 if (partid == sn_partition_id)
708                         continue;
709
710                 part = &xpc_partitions[partid];
711
712                 if (part->act_state == XPC_P_INACTIVE ||
713                     part->act_state == XPC_P_DEACTIVATING) {
714                         continue;
715                 }
716
717                 /* pull the remote_hb cache line */
718                 ret = xp_remote_memcpy(remote_vars,
719                                        (void *)part->sn.sn2.remote_vars_pa,
720                                        XPC_RP_VARS_SIZE);
721                 if (ret != xpSuccess) {
722                         XPC_DEACTIVATE_PARTITION(part, ret);
723                         continue;
724                 }
725
726                 dev_dbg(xpc_part, "partid = %d, heartbeat = %ld, last_heartbeat"
727                         " = %ld, heartbeat_offline = %ld, HB_mask[0] = 0x%lx\n",
728                         partid, remote_vars->heartbeat, part->last_heartbeat,
729                         remote_vars->heartbeat_offline,
730                         remote_vars->heartbeating_to_mask[0]);
731
732                 if (((remote_vars->heartbeat == part->last_heartbeat) &&
733                      (remote_vars->heartbeat_offline == 0)) ||
734                     !xpc_hb_allowed(sn_partition_id,
735                                     &remote_vars->heartbeating_to_mask)) {
736
737                         XPC_DEACTIVATE_PARTITION(part, xpNoHeartbeat);
738                         continue;
739                 }
740
741                 part->last_heartbeat = remote_vars->heartbeat;
742         }
743 }
744
745 /*
746  * Get a copy of the remote partition's XPC variables from the reserved page.
747  *
748  * remote_vars points to a buffer that is cacheline aligned for BTE copies and
749  * assumed to be of size XPC_RP_VARS_SIZE.
750  */
751 static enum xp_retval
752 xpc_get_remote_vars_sn2(u64 remote_vars_pa, struct xpc_vars_sn2 *remote_vars)
753 {
754         enum xp_retval ret;
755
756         if (remote_vars_pa == 0)
757                 return xpVarsNotSet;
758
759         /* pull over the cross partition variables */
760         ret = xp_remote_memcpy(remote_vars, (void *)remote_vars_pa,
761                                XPC_RP_VARS_SIZE);
762         if (ret != xpSuccess)
763                 return ret;
764
765         if (XPC_VERSION_MAJOR(remote_vars->version) !=
766             XPC_VERSION_MAJOR(XPC_V_VERSION)) {
767                 return xpBadVersion;
768         }
769
770         return xpSuccess;
771 }
772
773 static void
774 xpc_request_partition_activation_sn2(struct xpc_rsvd_page *remote_rp,
775                                      u64 remote_rp_pa, int nasid)
776 {
777         xpc_send_local_activate_IRQ_sn2(nasid);
778 }
779
780 static void
781 xpc_request_partition_reactivation_sn2(struct xpc_partition *part)
782 {
783         xpc_send_local_activate_IRQ_sn2(part->sn.sn2.activate_IRQ_nasid);
784 }
785
786 static void
787 xpc_request_partition_deactivation_sn2(struct xpc_partition *part)
788 {
789         struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
790         unsigned long irq_flags;
791         struct amo *amo = (struct amo *)__va(part_sn2->remote_amos_page_pa +
792                                              (XPC_DEACTIVATE_REQUEST_AMO_SN2 *
793                                              sizeof(struct amo)));
794
795         local_irq_save(irq_flags);
796
797         /* set bit corresponding to our partid in remote partition's amo */
798         FETCHOP_STORE_OP(TO_AMO((u64)&amo->variable), FETCHOP_OR,
799                          BIT(sn_partition_id));
800
801         /*
802          * We must always use the nofault function regardless of whether we
803          * are on a Shub 1.1 system or a Shub 1.2 slice 0xc processor. If we
804          * didn't, we'd never know that the other partition is down and would
805          * keep sending IRQs and amos to it until the heartbeat times out.
806          */
807         (void)xp_nofault_PIOR((u64 *)GLOBAL_MMR_ADDR(NASID_GET(&amo->
808                                                                variable),
809                                                      xp_nofault_PIOR_target));
810
811         local_irq_restore(irq_flags);
812
813         /*
814          * Send activate IRQ to get other side to see that we've set our
815          * bit in their deactivate request amo.
816          */
817         xpc_send_activate_IRQ_sn2(part_sn2->remote_amos_page_pa,
818                                   cnodeid_to_nasid(0),
819                                   part_sn2->activate_IRQ_nasid,
820                                   part_sn2->activate_IRQ_phys_cpuid);
821 }
822
823 static void
824 xpc_cancel_partition_deactivation_request_sn2(struct xpc_partition *part)
825 {
826         unsigned long irq_flags;
827         struct amo *amo = (struct amo *)__va(part->sn.sn2.remote_amos_page_pa +
828                                              (XPC_DEACTIVATE_REQUEST_AMO_SN2 *
829                                              sizeof(struct amo)));
830
831         local_irq_save(irq_flags);
832
833         /* clear bit corresponding to our partid in remote partition's amo */
834         FETCHOP_STORE_OP(TO_AMO((u64)&amo->variable), FETCHOP_AND,
835                          ~BIT(sn_partition_id));
836
837         /*
838          * We must always use the nofault function regardless of whether we
839          * are on a Shub 1.1 system or a Shub 1.2 slice 0xc processor. If we
840          * didn't, we'd never know that the other partition is down and would
841          * keep sending IRQs and amos to it until the heartbeat times out.
842          */
843         (void)xp_nofault_PIOR((u64 *)GLOBAL_MMR_ADDR(NASID_GET(&amo->
844                                                                variable),
845                                                      xp_nofault_PIOR_target));
846
847         local_irq_restore(irq_flags);
848 }
849
850 static int
851 xpc_partition_deactivation_requested_sn2(short partid)
852 {
853         struct amo *amo = xpc_vars_sn2->amos_page +
854                           XPC_DEACTIVATE_REQUEST_AMO_SN2;
855
856         /* our partition's amo variable ANDed with partid mask */
857         return (FETCHOP_LOAD_OP(TO_AMO((u64)&amo->variable), FETCHOP_LOAD) &
858                 BIT(partid)) != 0;
859 }
860
861 /*
862  * Update the remote partition's info.
863  */
864 static void
865 xpc_update_partition_info_sn2(struct xpc_partition *part, u8 remote_rp_version,
866                               unsigned long *remote_rp_ts_jiffies,
867                               u64 remote_rp_pa, u64 remote_vars_pa,
868                               struct xpc_vars_sn2 *remote_vars)
869 {
870         struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
871
872         part->remote_rp_version = remote_rp_version;
873         dev_dbg(xpc_part, "  remote_rp_version = 0x%016x\n",
874                 part->remote_rp_version);
875
876         part->remote_rp_ts_jiffies = *remote_rp_ts_jiffies;
877         dev_dbg(xpc_part, "  remote_rp_ts_jiffies = 0x%016lx\n",
878                 part->remote_rp_ts_jiffies);
879
880         part->remote_rp_pa = remote_rp_pa;
881         dev_dbg(xpc_part, "  remote_rp_pa = 0x%016lx\n", part->remote_rp_pa);
882
883         part_sn2->remote_vars_pa = remote_vars_pa;
884         dev_dbg(xpc_part, "  remote_vars_pa = 0x%016lx\n",
885                 part_sn2->remote_vars_pa);
886
887         part->last_heartbeat = remote_vars->heartbeat;
888         dev_dbg(xpc_part, "  last_heartbeat = 0x%016lx\n",
889                 part->last_heartbeat);
890
891         part_sn2->remote_vars_part_pa = remote_vars->vars_part_pa;
892         dev_dbg(xpc_part, "  remote_vars_part_pa = 0x%016lx\n",
893                 part_sn2->remote_vars_part_pa);
894
895         part_sn2->activate_IRQ_nasid = remote_vars->activate_IRQ_nasid;
896         dev_dbg(xpc_part, "  activate_IRQ_nasid = 0x%x\n",
897                 part_sn2->activate_IRQ_nasid);
898
899         part_sn2->activate_IRQ_phys_cpuid =
900             remote_vars->activate_IRQ_phys_cpuid;
901         dev_dbg(xpc_part, "  activate_IRQ_phys_cpuid = 0x%x\n",
902                 part_sn2->activate_IRQ_phys_cpuid);
903
904         part_sn2->remote_amos_page_pa = remote_vars->amos_page_pa;
905         dev_dbg(xpc_part, "  remote_amos_page_pa = 0x%lx\n",
906                 part_sn2->remote_amos_page_pa);
907
908         part_sn2->remote_vars_version = remote_vars->version;
909         dev_dbg(xpc_part, "  remote_vars_version = 0x%x\n",
910                 part_sn2->remote_vars_version);
911 }
912
913 /*
914  * Prior code has determined the nasid which generated a activate IRQ.
915  * Inspect that nasid to determine if its partition needs to be activated
916  * or deactivated.
917  *
918  * A partition is considered "awaiting activation" if our partition
919  * flags indicate it is not active and it has a heartbeat.  A
920  * partition is considered "awaiting deactivation" if our partition
921  * flags indicate it is active but it has no heartbeat or it is not
922  * sending its heartbeat to us.
923  *
924  * To determine the heartbeat, the remote nasid must have a properly
925  * initialized reserved page.
926  */
927 static void
928 xpc_identify_activate_IRQ_req_sn2(int nasid)
929 {
930         struct xpc_rsvd_page *remote_rp;
931         struct xpc_vars_sn2 *remote_vars;
932         u64 remote_rp_pa;
933         u64 remote_vars_pa;
934         int remote_rp_version;
935         int reactivate = 0;
936         unsigned long remote_rp_ts_jiffies = 0;
937         short partid;
938         struct xpc_partition *part;
939         struct xpc_partition_sn2 *part_sn2;
940         enum xp_retval ret;
941
942         /* pull over the reserved page structure */
943
944         remote_rp = (struct xpc_rsvd_page *)xpc_remote_copy_buffer_sn2;
945
946         ret = xpc_get_remote_rp(nasid, NULL, remote_rp, &remote_rp_pa);
947         if (ret != xpSuccess) {
948                 dev_warn(xpc_part, "unable to get reserved page from nasid %d, "
949                          "which sent interrupt, reason=%d\n", nasid, ret);
950                 return;
951         }
952
953         remote_vars_pa = remote_rp->sn.vars_pa;
954         remote_rp_version = remote_rp->version;
955         remote_rp_ts_jiffies = remote_rp->ts_jiffies;
956
957         partid = remote_rp->SAL_partid;
958         part = &xpc_partitions[partid];
959         part_sn2 = &part->sn.sn2;
960
961         /* pull over the cross partition variables */
962
963         remote_vars = (struct xpc_vars_sn2 *)xpc_remote_copy_buffer_sn2;
964
965         ret = xpc_get_remote_vars_sn2(remote_vars_pa, remote_vars);
966         if (ret != xpSuccess) {
967                 dev_warn(xpc_part, "unable to get XPC variables from nasid %d, "
968                          "which sent interrupt, reason=%d\n", nasid, ret);
969
970                 XPC_DEACTIVATE_PARTITION(part, ret);
971                 return;
972         }
973
974         part->activate_IRQ_rcvd++;
975
976         dev_dbg(xpc_part, "partid for nasid %d is %d; IRQs = %d; HB = "
977                 "%ld:0x%lx\n", (int)nasid, (int)partid, part->activate_IRQ_rcvd,
978                 remote_vars->heartbeat, remote_vars->heartbeating_to_mask[0]);
979
980         if (xpc_partition_disengaged(part) &&
981             part->act_state == XPC_P_INACTIVE) {
982
983                 xpc_update_partition_info_sn2(part, remote_rp_version,
984                                               &remote_rp_ts_jiffies,
985                                               remote_rp_pa, remote_vars_pa,
986                                               remote_vars);
987
988                 if (xpc_partition_deactivation_requested_sn2(partid)) {
989                         /*
990                          * Other side is waiting on us to deactivate even though
991                          * we already have.
992                          */
993                         return;
994                 }
995
996                 xpc_activate_partition(part);
997                 return;
998         }
999
1000         DBUG_ON(part->remote_rp_version == 0);
1001         DBUG_ON(part_sn2->remote_vars_version == 0);
1002
1003         if (remote_rp_ts_jiffies != part->remote_rp_ts_jiffies) {
1004
1005                 /* the other side rebooted */
1006
1007                 DBUG_ON(xpc_partition_engaged_sn2(partid));
1008                 DBUG_ON(xpc_partition_deactivation_requested_sn2(partid));
1009
1010                 xpc_update_partition_info_sn2(part, remote_rp_version,
1011                                               &remote_rp_ts_jiffies,
1012                                               remote_rp_pa, remote_vars_pa,
1013                                               remote_vars);
1014                 reactivate = 1;
1015         }
1016
1017         if (part->disengage_timeout > 0 && !xpc_partition_disengaged(part)) {
1018                 /* still waiting on other side to disengage from us */
1019                 return;
1020         }
1021
1022         if (reactivate)
1023                 XPC_DEACTIVATE_PARTITION(part, xpReactivating);
1024         else if (xpc_partition_deactivation_requested_sn2(partid))
1025                 XPC_DEACTIVATE_PARTITION(part, xpOtherGoingDown);
1026 }
1027
1028 /*
1029  * Loop through the activation amo variables and process any bits
1030  * which are set.  Each bit indicates a nasid sending a partition
1031  * activation or deactivation request.
1032  *
1033  * Return #of IRQs detected.
1034  */
1035 int
1036 xpc_identify_activate_IRQ_sender_sn2(void)
1037 {
1038         int l;
1039         int b;
1040         unsigned long nasid_mask_long;
1041         u64 nasid;              /* remote nasid */
1042         int n_IRQs_detected = 0;
1043         struct amo *act_amos;
1044
1045         act_amos = xpc_vars_sn2->amos_page + XPC_ACTIVATE_IRQ_AMOS_SN2;
1046
1047         /* scan through activate amo variables looking for non-zero entries */
1048         for (l = 0; l < xpc_nasid_mask_nlongs; l++) {
1049
1050                 if (xpc_exiting)
1051                         break;
1052
1053                 nasid_mask_long = xpc_receive_IRQ_amo_sn2(&act_amos[l]);
1054
1055                 b = find_first_bit(&nasid_mask_long, BITS_PER_LONG);
1056                 if (b >= BITS_PER_LONG) {
1057                         /* no IRQs from nasids in this amo variable */
1058                         continue;
1059                 }
1060
1061                 dev_dbg(xpc_part, "amo[%d] gave back 0x%lx\n", l,
1062                         nasid_mask_long);
1063
1064                 /*
1065                  * If this nasid has been added to the machine since
1066                  * our partition was reset, this will retain the
1067                  * remote nasid in our reserved pages machine mask.
1068                  * This is used in the event of module reload.
1069                  */
1070                 xpc_mach_nasids[l] |= nasid_mask_long;
1071
1072                 /* locate the nasid(s) which sent interrupts */
1073
1074                 do {
1075                         n_IRQs_detected++;
1076                         nasid = (l * BITS_PER_LONG + b) * 2;
1077                         dev_dbg(xpc_part, "interrupt from nasid %ld\n", nasid);
1078                         xpc_identify_activate_IRQ_req_sn2(nasid);
1079
1080                         b = find_next_bit(&nasid_mask_long, BITS_PER_LONG,
1081                                           b + 1);
1082                 } while (b < BITS_PER_LONG);
1083         }
1084         return n_IRQs_detected;
1085 }
1086
1087 static void
1088 xpc_process_activate_IRQ_rcvd_sn2(int n_IRQs_expected)
1089 {
1090         int n_IRQs_detected;
1091
1092         n_IRQs_detected = xpc_identify_activate_IRQ_sender_sn2();
1093         if (n_IRQs_detected < n_IRQs_expected) {
1094                 /* retry once to help avoid missing amo */
1095                 (void)xpc_identify_activate_IRQ_sender_sn2();
1096         }
1097 }
1098
1099 /*
1100  * Guarantee that the kzalloc'd memory is cacheline aligned.
1101  */
1102 static void *
1103 xpc_kzalloc_cacheline_aligned_sn2(size_t size, gfp_t flags, void **base)
1104 {
1105         /* see if kzalloc will give us cachline aligned memory by default */
1106         *base = kzalloc(size, flags);
1107         if (*base == NULL)
1108                 return NULL;
1109
1110         if ((u64)*base == L1_CACHE_ALIGN((u64)*base))
1111                 return *base;
1112
1113         kfree(*base);
1114
1115         /* nope, we'll have to do it ourselves */
1116         *base = kzalloc(size + L1_CACHE_BYTES, flags);
1117         if (*base == NULL)
1118                 return NULL;
1119
1120         return (void *)L1_CACHE_ALIGN((u64)*base);
1121 }
1122
1123 /*
1124  * Setup the infrastructure necessary to support XPartition Communication
1125  * between the specified remote partition and the local one.
1126  */
1127 static enum xp_retval
1128 xpc_setup_infrastructure_sn2(struct xpc_partition *part)
1129 {
1130         struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
1131         enum xp_retval retval;
1132         int ret;
1133         int cpuid;
1134         int ch_number;
1135         struct xpc_channel *ch;
1136         struct timer_list *timer;
1137         short partid = XPC_PARTID(part);
1138
1139         /*
1140          * Allocate all of the channel structures as a contiguous chunk of
1141          * memory.
1142          */
1143         DBUG_ON(part->channels != NULL);
1144         part->channels = kzalloc(sizeof(struct xpc_channel) * XPC_MAX_NCHANNELS,
1145                                  GFP_KERNEL);
1146         if (part->channels == NULL) {
1147                 dev_err(xpc_chan, "can't get memory for channels\n");
1148                 return xpNoMemory;
1149         }
1150
1151         /* allocate all the required GET/PUT values */
1152
1153         part_sn2->local_GPs =
1154             xpc_kzalloc_cacheline_aligned_sn2(XPC_GP_SIZE, GFP_KERNEL,
1155                                               &part_sn2->local_GPs_base);
1156         if (part_sn2->local_GPs == NULL) {
1157                 dev_err(xpc_chan, "can't get memory for local get/put "
1158                         "values\n");
1159                 retval = xpNoMemory;
1160                 goto out_1;
1161         }
1162
1163         part_sn2->remote_GPs =
1164             xpc_kzalloc_cacheline_aligned_sn2(XPC_GP_SIZE, GFP_KERNEL,
1165                                               &part_sn2->remote_GPs_base);
1166         if (part_sn2->remote_GPs == NULL) {
1167                 dev_err(xpc_chan, "can't get memory for remote get/put "
1168                         "values\n");
1169                 retval = xpNoMemory;
1170                 goto out_2;
1171         }
1172
1173         part_sn2->remote_GPs_pa = 0;
1174
1175         /* allocate all the required open and close args */
1176
1177         part->local_openclose_args =
1178             xpc_kzalloc_cacheline_aligned_sn2(XPC_OPENCLOSE_ARGS_SIZE,
1179                                               GFP_KERNEL,
1180                                               &part->local_openclose_args_base);
1181         if (part->local_openclose_args == NULL) {
1182                 dev_err(xpc_chan, "can't get memory for local connect args\n");
1183                 retval = xpNoMemory;
1184                 goto out_3;
1185         }
1186
1187         part->remote_openclose_args =
1188             xpc_kzalloc_cacheline_aligned_sn2(XPC_OPENCLOSE_ARGS_SIZE,
1189                                               GFP_KERNEL,
1190                                              &part->remote_openclose_args_base);
1191         if (part->remote_openclose_args == NULL) {
1192                 dev_err(xpc_chan, "can't get memory for remote connect args\n");
1193                 retval = xpNoMemory;
1194                 goto out_4;
1195         }
1196
1197         part_sn2->remote_openclose_args_pa = 0;
1198
1199         part_sn2->local_chctl_amo_va = xpc_init_IRQ_amo_sn2(partid);
1200         part->chctl.all_flags = 0;
1201         spin_lock_init(&part->chctl_lock);
1202
1203         part_sn2->notify_IRQ_nasid = 0;
1204         part_sn2->notify_IRQ_phys_cpuid = 0;
1205         part_sn2->remote_chctl_amo_va = NULL;
1206
1207         atomic_set(&part->channel_mgr_requests, 1);
1208         init_waitqueue_head(&part->channel_mgr_wq);
1209
1210         sprintf(part_sn2->notify_IRQ_owner, "xpc%02d", partid);
1211         ret = request_irq(SGI_XPC_NOTIFY, xpc_handle_notify_IRQ_sn2,
1212                           IRQF_SHARED, part_sn2->notify_IRQ_owner,
1213                           (void *)(u64)partid);
1214         if (ret != 0) {
1215                 dev_err(xpc_chan, "can't register NOTIFY IRQ handler, "
1216                         "errno=%d\n", -ret);
1217                 retval = xpLackOfResources;
1218                 goto out_5;
1219         }
1220
1221         /* Setup a timer to check for dropped notify IRQs */
1222         timer = &part_sn2->dropped_notify_IRQ_timer;
1223         init_timer(timer);
1224         timer->function =
1225             (void (*)(unsigned long))xpc_check_for_dropped_notify_IRQ_sn2;
1226         timer->data = (unsigned long)part;
1227         timer->expires = jiffies + XPC_DROPPED_NOTIFY_IRQ_WAIT_INTERVAL;
1228         add_timer(timer);
1229
1230         part->nchannels = XPC_MAX_NCHANNELS;
1231
1232         atomic_set(&part->nchannels_active, 0);
1233         atomic_set(&part->nchannels_engaged, 0);
1234
1235         for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
1236                 ch = &part->channels[ch_number];
1237
1238                 ch->partid = partid;
1239                 ch->number = ch_number;
1240                 ch->flags = XPC_C_DISCONNECTED;
1241
1242                 ch->sn.sn2.local_GP = &part_sn2->local_GPs[ch_number];
1243                 ch->local_openclose_args =
1244                     &part->local_openclose_args[ch_number];
1245
1246                 atomic_set(&ch->kthreads_assigned, 0);
1247                 atomic_set(&ch->kthreads_idle, 0);
1248                 atomic_set(&ch->kthreads_active, 0);
1249
1250                 atomic_set(&ch->references, 0);
1251                 atomic_set(&ch->n_to_notify, 0);
1252
1253                 spin_lock_init(&ch->lock);
1254                 mutex_init(&ch->sn.sn2.msg_to_pull_mutex);
1255                 init_completion(&ch->wdisconnect_wait);
1256
1257                 atomic_set(&ch->n_on_msg_allocate_wq, 0);
1258                 init_waitqueue_head(&ch->msg_allocate_wq);
1259                 init_waitqueue_head(&ch->idle_wq);
1260         }
1261
1262         /*
1263          * With the setting of the partition setup_state to XPC_P_SETUP, we're
1264          * declaring that this partition is ready to go.
1265          */
1266         part->setup_state = XPC_P_SETUP;
1267
1268         /*
1269          * Setup the per partition specific variables required by the
1270          * remote partition to establish channel connections with us.
1271          *
1272          * The setting of the magic # indicates that these per partition
1273          * specific variables are ready to be used.
1274          */
1275         xpc_vars_part_sn2[partid].GPs_pa = __pa(part_sn2->local_GPs);
1276         xpc_vars_part_sn2[partid].openclose_args_pa =
1277             __pa(part->local_openclose_args);
1278         xpc_vars_part_sn2[partid].chctl_amo_pa =
1279             __pa(part_sn2->local_chctl_amo_va);
1280         cpuid = raw_smp_processor_id(); /* any CPU in this partition will do */
1281         xpc_vars_part_sn2[partid].notify_IRQ_nasid = cpuid_to_nasid(cpuid);
1282         xpc_vars_part_sn2[partid].notify_IRQ_phys_cpuid =
1283             cpu_physical_id(cpuid);
1284         xpc_vars_part_sn2[partid].nchannels = part->nchannels;
1285         xpc_vars_part_sn2[partid].magic = XPC_VP_MAGIC1;
1286
1287         return xpSuccess;
1288
1289         /* setup of infrastructure failed */
1290 out_5:
1291         kfree(part->remote_openclose_args_base);
1292         part->remote_openclose_args = NULL;
1293 out_4:
1294         kfree(part->local_openclose_args_base);
1295         part->local_openclose_args = NULL;
1296 out_3:
1297         kfree(part_sn2->remote_GPs_base);
1298         part_sn2->remote_GPs = NULL;
1299 out_2:
1300         kfree(part_sn2->local_GPs_base);
1301         part_sn2->local_GPs = NULL;
1302 out_1:
1303         kfree(part->channels);
1304         part->channels = NULL;
1305         return retval;
1306 }
1307
1308 /*
1309  * Teardown the infrastructure necessary to support XPartition Communication
1310  * between the specified remote partition and the local one.
1311  */
1312 static void
1313 xpc_teardown_infrastructure_sn2(struct xpc_partition *part)
1314 {
1315         struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
1316         short partid = XPC_PARTID(part);
1317
1318         /*
1319          * We start off by making this partition inaccessible to local
1320          * processes by marking it as no longer setup. Then we make it
1321          * inaccessible to remote processes by clearing the XPC per partition
1322          * specific variable's magic # (which indicates that these variables
1323          * are no longer valid) and by ignoring all XPC notify IRQs sent to
1324          * this partition.
1325          */
1326
1327         DBUG_ON(atomic_read(&part->nchannels_engaged) != 0);
1328         DBUG_ON(atomic_read(&part->nchannels_active) != 0);
1329         DBUG_ON(part->setup_state != XPC_P_SETUP);
1330         part->setup_state = XPC_P_WTEARDOWN;
1331
1332         xpc_vars_part_sn2[partid].magic = 0;
1333
1334         free_irq(SGI_XPC_NOTIFY, (void *)(u64)partid);
1335
1336         /*
1337          * Before proceeding with the teardown we have to wait until all
1338          * existing references cease.
1339          */
1340         wait_event(part->teardown_wq, (atomic_read(&part->references) == 0));
1341
1342         /* now we can begin tearing down the infrastructure */
1343
1344         part->setup_state = XPC_P_TORNDOWN;
1345
1346         /* in case we've still got outstanding timers registered... */
1347         del_timer_sync(&part_sn2->dropped_notify_IRQ_timer);
1348
1349         kfree(part->remote_openclose_args_base);
1350         part->remote_openclose_args = NULL;
1351         kfree(part->local_openclose_args_base);
1352         part->local_openclose_args = NULL;
1353         kfree(part_sn2->remote_GPs_base);
1354         part_sn2->remote_GPs = NULL;
1355         kfree(part_sn2->local_GPs_base);
1356         part_sn2->local_GPs = NULL;
1357         kfree(part->channels);
1358         part->channels = NULL;
1359         part_sn2->local_chctl_amo_va = NULL;
1360 }
1361
1362 /*
1363  * Create a wrapper that hides the underlying mechanism for pulling a cacheline
1364  * (or multiple cachelines) from a remote partition.
1365  *
1366  * src must be a cacheline aligned physical address on the remote partition.
1367  * dst must be a cacheline aligned virtual address on this partition.
1368  * cnt must be cacheline sized
1369  */
1370 /* ??? Replace this function by call to xp_remote_memcpy() or bte_copy()? */
1371 static enum xp_retval
1372 xpc_pull_remote_cachelines_sn2(struct xpc_partition *part, void *dst,
1373                                const void *src, size_t cnt)
1374 {
1375         enum xp_retval ret;
1376
1377         DBUG_ON((u64)src != L1_CACHE_ALIGN((u64)src));
1378         DBUG_ON((u64)dst != L1_CACHE_ALIGN((u64)dst));
1379         DBUG_ON(cnt != L1_CACHE_ALIGN(cnt));
1380
1381         if (part->act_state == XPC_P_DEACTIVATING)
1382                 return part->reason;
1383
1384         ret = xp_remote_memcpy(dst, src, cnt);
1385         if (ret != xpSuccess) {
1386                 dev_dbg(xpc_chan, "xp_remote_memcpy() from partition %d failed,"
1387                         " ret=%d\n", XPC_PARTID(part), ret);
1388         }
1389         return ret;
1390 }
1391
1392 /*
1393  * Pull the remote per partition specific variables from the specified
1394  * partition.
1395  */
1396 static enum xp_retval
1397 xpc_pull_remote_vars_part_sn2(struct xpc_partition *part)
1398 {
1399         struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
1400         u8 buffer[L1_CACHE_BYTES * 2];
1401         struct xpc_vars_part_sn2 *pulled_entry_cacheline =
1402             (struct xpc_vars_part_sn2 *)L1_CACHE_ALIGN((u64)buffer);
1403         struct xpc_vars_part_sn2 *pulled_entry;
1404         u64 remote_entry_cacheline_pa, remote_entry_pa;
1405         short partid = XPC_PARTID(part);
1406         enum xp_retval ret;
1407
1408         /* pull the cacheline that contains the variables we're interested in */
1409
1410         DBUG_ON(part_sn2->remote_vars_part_pa !=
1411                 L1_CACHE_ALIGN(part_sn2->remote_vars_part_pa));
1412         DBUG_ON(sizeof(struct xpc_vars_part_sn2) != L1_CACHE_BYTES / 2);
1413
1414         remote_entry_pa = part_sn2->remote_vars_part_pa +
1415             sn_partition_id * sizeof(struct xpc_vars_part_sn2);
1416
1417         remote_entry_cacheline_pa = (remote_entry_pa & ~(L1_CACHE_BYTES - 1));
1418
1419         pulled_entry = (struct xpc_vars_part_sn2 *)((u64)pulled_entry_cacheline
1420                                                     + (remote_entry_pa &
1421                                                     (L1_CACHE_BYTES - 1)));
1422
1423         ret = xpc_pull_remote_cachelines_sn2(part, pulled_entry_cacheline,
1424                                              (void *)remote_entry_cacheline_pa,
1425                                              L1_CACHE_BYTES);
1426         if (ret != xpSuccess) {
1427                 dev_dbg(xpc_chan, "failed to pull XPC vars_part from "
1428                         "partition %d, ret=%d\n", partid, ret);
1429                 return ret;
1430         }
1431
1432         /* see if they've been set up yet */
1433
1434         if (pulled_entry->magic != XPC_VP_MAGIC1 &&
1435             pulled_entry->magic != XPC_VP_MAGIC2) {
1436
1437                 if (pulled_entry->magic != 0) {
1438                         dev_dbg(xpc_chan, "partition %d's XPC vars_part for "
1439                                 "partition %d has bad magic value (=0x%lx)\n",
1440                                 partid, sn_partition_id, pulled_entry->magic);
1441                         return xpBadMagic;
1442                 }
1443
1444                 /* they've not been initialized yet */
1445                 return xpRetry;
1446         }
1447
1448         if (xpc_vars_part_sn2[partid].magic == XPC_VP_MAGIC1) {
1449
1450                 /* validate the variables */
1451
1452                 if (pulled_entry->GPs_pa == 0 ||
1453                     pulled_entry->openclose_args_pa == 0 ||
1454                     pulled_entry->chctl_amo_pa == 0) {
1455
1456                         dev_err(xpc_chan, "partition %d's XPC vars_part for "
1457                                 "partition %d are not valid\n", partid,
1458                                 sn_partition_id);
1459                         return xpInvalidAddress;
1460                 }
1461
1462                 /* the variables we imported look to be valid */
1463
1464                 part_sn2->remote_GPs_pa = pulled_entry->GPs_pa;
1465                 part_sn2->remote_openclose_args_pa =
1466                     pulled_entry->openclose_args_pa;
1467                 part_sn2->remote_chctl_amo_va =
1468                     (struct amo *)__va(pulled_entry->chctl_amo_pa);
1469                 part_sn2->notify_IRQ_nasid = pulled_entry->notify_IRQ_nasid;
1470                 part_sn2->notify_IRQ_phys_cpuid =
1471                     pulled_entry->notify_IRQ_phys_cpuid;
1472
1473                 if (part->nchannels > pulled_entry->nchannels)
1474                         part->nchannels = pulled_entry->nchannels;
1475
1476                 /* let the other side know that we've pulled their variables */
1477
1478                 xpc_vars_part_sn2[partid].magic = XPC_VP_MAGIC2;
1479         }
1480
1481         if (pulled_entry->magic == XPC_VP_MAGIC1)
1482                 return xpRetry;
1483
1484         return xpSuccess;
1485 }
1486
1487 /*
1488  * Establish first contact with the remote partititon. This involves pulling
1489  * the XPC per partition variables from the remote partition and waiting for
1490  * the remote partition to pull ours.
1491  */
1492 static enum xp_retval
1493 xpc_make_first_contact_sn2(struct xpc_partition *part)
1494 {
1495         struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
1496         enum xp_retval ret;
1497
1498         /*
1499          * Register the remote partition's amos with SAL so it can handle
1500          * and cleanup errors within that address range should the remote
1501          * partition go down. We don't unregister this range because it is
1502          * difficult to tell when outstanding writes to the remote partition
1503          * are finished and thus when it is safe to unregister. This should
1504          * not result in wasted space in the SAL xp_addr_region table because
1505          * we should get the same page for remote_amos_page_pa after module
1506          * reloads and system reboots.
1507          */
1508         if (sn_register_xp_addr_region(part_sn2->remote_amos_page_pa,
1509                                        PAGE_SIZE, 1) < 0) {
1510                 dev_warn(xpc_part, "xpc_activating(%d) failed to register "
1511                          "xp_addr region\n", XPC_PARTID(part));
1512
1513                 ret = xpPhysAddrRegFailed;
1514                 XPC_DEACTIVATE_PARTITION(part, ret);
1515                 return ret;
1516         }
1517
1518         /*
1519          * Send activate IRQ to get other side to activate if they've not
1520          * already begun to do so.
1521          */
1522         xpc_send_activate_IRQ_sn2(part_sn2->remote_amos_page_pa,
1523                                   cnodeid_to_nasid(0),
1524                                   part_sn2->activate_IRQ_nasid,
1525                                   part_sn2->activate_IRQ_phys_cpuid);
1526
1527         while ((ret = xpc_pull_remote_vars_part_sn2(part)) != xpSuccess) {
1528                 if (ret != xpRetry) {
1529                         XPC_DEACTIVATE_PARTITION(part, ret);
1530                         return ret;
1531                 }
1532
1533                 dev_dbg(xpc_part, "waiting to make first contact with "
1534                         "partition %d\n", XPC_PARTID(part));
1535
1536                 /* wait a 1/4 of a second or so */
1537                 (void)msleep_interruptible(250);
1538
1539                 if (part->act_state == XPC_P_DEACTIVATING)
1540                         return part->reason;
1541         }
1542
1543         return xpSuccess;
1544 }
1545
1546 /*
1547  * Get the chctl flags and pull the openclose args and/or remote GPs as needed.
1548  */
1549 static u64
1550 xpc_get_chctl_all_flags_sn2(struct xpc_partition *part)
1551 {
1552         struct xpc_partition_sn2 *part_sn2 = &part->sn.sn2;
1553         unsigned long irq_flags;
1554         union xpc_channel_ctl_flags chctl;
1555         enum xp_retval ret;
1556
1557         /*
1558          * See if there are any chctl flags to be handled.
1559          */
1560
1561         spin_lock_irqsave(&part->chctl_lock, irq_flags);
1562         chctl = part->chctl;
1563         if (chctl.all_flags != 0)
1564                 part->chctl.all_flags = 0;
1565
1566         spin_unlock_irqrestore(&part->chctl_lock, irq_flags);
1567
1568         if (xpc_any_openclose_chctl_flags_set(&chctl)) {
1569                 ret = xpc_pull_remote_cachelines_sn2(part, part->
1570                                                      remote_openclose_args,
1571                                                      (void *)part_sn2->
1572                                                      remote_openclose_args_pa,
1573                                                      XPC_OPENCLOSE_ARGS_SIZE);
1574                 if (ret != xpSuccess) {
1575                         XPC_DEACTIVATE_PARTITION(part, ret);
1576
1577                         dev_dbg(xpc_chan, "failed to pull openclose args from "
1578                                 "partition %d, ret=%d\n", XPC_PARTID(part),
1579                                 ret);
1580
1581                         /* don't bother processing chctl flags anymore */
1582                         chctl.all_flags = 0;
1583                 }
1584         }
1585
1586         if (xpc_any_msg_chctl_flags_set(&chctl)) {
1587                 ret = xpc_pull_remote_cachelines_sn2(part, part_sn2->remote_GPs,
1588                                                 (void *)part_sn2->remote_GPs_pa,
1589                                                      XPC_GP_SIZE);
1590                 if (ret != xpSuccess) {
1591                         XPC_DEACTIVATE_PARTITION(part, ret);
1592
1593                         dev_dbg(xpc_chan, "failed to pull GPs from partition "
1594                                 "%d, ret=%d\n", XPC_PARTID(part), ret);
1595
1596                         /* don't bother processing chctl flags anymore */
1597                         chctl.all_flags = 0;
1598                 }
1599         }
1600
1601         return chctl.all_flags;
1602 }
1603
1604 /*
1605  * Allocate the local message queue and the notify queue.
1606  */
1607 static enum xp_retval
1608 xpc_allocate_local_msgqueue_sn2(struct xpc_channel *ch)
1609 {
1610         unsigned long irq_flags;
1611         int nentries;
1612         size_t nbytes;
1613
1614         for (nentries = ch->local_nentries; nentries > 0; nentries--) {
1615
1616                 nbytes = nentries * ch->msg_size;
1617                 ch->local_msgqueue =
1618                     xpc_kzalloc_cacheline_aligned_sn2(nbytes, GFP_KERNEL,
1619                                                       &ch->local_msgqueue_base);
1620                 if (ch->local_msgqueue == NULL)
1621                         continue;
1622
1623                 nbytes = nentries * sizeof(struct xpc_notify);
1624                 ch->notify_queue = kzalloc(nbytes, GFP_KERNEL);
1625                 if (ch->notify_queue == NULL) {
1626                         kfree(ch->local_msgqueue_base);
1627                         ch->local_msgqueue = NULL;
1628                         continue;
1629                 }
1630
1631                 spin_lock_irqsave(&ch->lock, irq_flags);
1632                 if (nentries < ch->local_nentries) {
1633                         dev_dbg(xpc_chan, "nentries=%d local_nentries=%d, "
1634                                 "partid=%d, channel=%d\n", nentries,
1635                                 ch->local_nentries, ch->partid, ch->number);
1636
1637                         ch->local_nentries = nentries;
1638                 }
1639                 spin_unlock_irqrestore(&ch->lock, irq_flags);
1640                 return xpSuccess;
1641         }
1642
1643         dev_dbg(xpc_chan, "can't get memory for local message queue and notify "
1644                 "queue, partid=%d, channel=%d\n", ch->partid, ch->number);
1645         return xpNoMemory;
1646 }
1647
1648 /*
1649  * Allocate the cached remote message queue.
1650  */
1651 static enum xp_retval
1652 xpc_allocate_remote_msgqueue_sn2(struct xpc_channel *ch)
1653 {
1654         unsigned long irq_flags;
1655         int nentries;
1656         size_t nbytes;
1657
1658         DBUG_ON(ch->remote_nentries <= 0);
1659
1660         for (nentries = ch->remote_nentries; nentries > 0; nentries--) {
1661
1662                 nbytes = nentries * ch->msg_size;
1663                 ch->remote_msgqueue =
1664                     xpc_kzalloc_cacheline_aligned_sn2(nbytes, GFP_KERNEL,
1665                                                      &ch->remote_msgqueue_base);
1666                 if (ch->remote_msgqueue == NULL)
1667                         continue;
1668
1669                 spin_lock_irqsave(&ch->lock, irq_flags);
1670                 if (nentries < ch->remote_nentries) {
1671                         dev_dbg(xpc_chan, "nentries=%d remote_nentries=%d, "
1672                                 "partid=%d, channel=%d\n", nentries,
1673                                 ch->remote_nentries, ch->partid, ch->number);
1674
1675                         ch->remote_nentries = nentries;
1676                 }
1677                 spin_unlock_irqrestore(&ch->lock, irq_flags);
1678                 return xpSuccess;
1679         }
1680
1681         dev_dbg(xpc_chan, "can't get memory for cached remote message queue, "
1682                 "partid=%d, channel=%d\n", ch->partid, ch->number);
1683         return xpNoMemory;
1684 }
1685
1686 /*
1687  * Allocate message queues and other stuff associated with a channel.
1688  *
1689  * Note: Assumes all of the channel sizes are filled in.
1690  */
1691 static enum xp_retval
1692 xpc_allocate_msgqueues_sn2(struct xpc_channel *ch)
1693 {
1694         enum xp_retval ret;
1695
1696         DBUG_ON(ch->flags & XPC_C_SETUP);
1697
1698         ret = xpc_allocate_local_msgqueue_sn2(ch);
1699         if (ret == xpSuccess) {
1700
1701                 ret = xpc_allocate_remote_msgqueue_sn2(ch);
1702                 if (ret != xpSuccess) {
1703                         kfree(ch->local_msgqueue_base);
1704                         ch->local_msgqueue = NULL;
1705                         kfree(ch->notify_queue);
1706                         ch->notify_queue = NULL;
1707                 }
1708         }
1709         return ret;
1710 }
1711
1712 /*
1713  * Free up message queues and other stuff that were allocated for the specified
1714  * channel.
1715  *
1716  * Note: ch->reason and ch->reason_line are left set for debugging purposes,
1717  * they're cleared when XPC_C_DISCONNECTED is cleared.
1718  */
1719 static void
1720 xpc_free_msgqueues_sn2(struct xpc_channel *ch)
1721 {
1722         struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
1723
1724         DBUG_ON(!spin_is_locked(&ch->lock));
1725         DBUG_ON(atomic_read(&ch->n_to_notify) != 0);
1726
1727         ch->remote_msgqueue_pa = 0;
1728         ch->func = NULL;
1729         ch->key = NULL;
1730         ch->msg_size = 0;
1731         ch->local_nentries = 0;
1732         ch->remote_nentries = 0;
1733         ch->kthreads_assigned_limit = 0;
1734         ch->kthreads_idle_limit = 0;
1735
1736         ch_sn2->local_GP->get = 0;
1737         ch_sn2->local_GP->put = 0;
1738         ch_sn2->remote_GP.get = 0;
1739         ch_sn2->remote_GP.put = 0;
1740         ch_sn2->w_local_GP.get = 0;
1741         ch_sn2->w_local_GP.put = 0;
1742         ch_sn2->w_remote_GP.get = 0;
1743         ch_sn2->w_remote_GP.put = 0;
1744         ch_sn2->next_msg_to_pull = 0;
1745
1746         if (ch->flags & XPC_C_SETUP) {
1747                 dev_dbg(xpc_chan, "ch->flags=0x%x, partid=%d, channel=%d\n",
1748                         ch->flags, ch->partid, ch->number);
1749
1750                 kfree(ch->local_msgqueue_base);
1751                 ch->local_msgqueue = NULL;
1752                 kfree(ch->remote_msgqueue_base);
1753                 ch->remote_msgqueue = NULL;
1754                 kfree(ch->notify_queue);
1755                 ch->notify_queue = NULL;
1756         }
1757 }
1758
1759 /*
1760  * Notify those who wanted to be notified upon delivery of their message.
1761  */
1762 static void
1763 xpc_notify_senders_sn2(struct xpc_channel *ch, enum xp_retval reason, s64 put)
1764 {
1765         struct xpc_notify *notify;
1766         u8 notify_type;
1767         s64 get = ch->sn.sn2.w_remote_GP.get - 1;
1768
1769         while (++get < put && atomic_read(&ch->n_to_notify) > 0) {
1770
1771                 notify = &ch->notify_queue[get % ch->local_nentries];
1772
1773                 /*
1774                  * See if the notify entry indicates it was associated with
1775                  * a message who's sender wants to be notified. It is possible
1776                  * that it is, but someone else is doing or has done the
1777                  * notification.
1778                  */
1779                 notify_type = notify->type;
1780                 if (notify_type == 0 ||
1781                     cmpxchg(&notify->type, notify_type, 0) != notify_type) {
1782                         continue;
1783                 }
1784
1785                 DBUG_ON(notify_type != XPC_N_CALL);
1786
1787                 atomic_dec(&ch->n_to_notify);
1788
1789                 if (notify->func != NULL) {
1790                         dev_dbg(xpc_chan, "notify->func() called, notify=0x%p, "
1791                                 "msg_number=%ld, partid=%d, channel=%d\n",
1792                                 (void *)notify, get, ch->partid, ch->number);
1793
1794                         notify->func(reason, ch->partid, ch->number,
1795                                      notify->key);
1796
1797                         dev_dbg(xpc_chan, "notify->func() returned, "
1798                                 "notify=0x%p, msg_number=%ld, partid=%d, "
1799                                 "channel=%d\n", (void *)notify, get,
1800                                 ch->partid, ch->number);
1801                 }
1802         }
1803 }
1804
1805 static void
1806 xpc_notify_senders_of_disconnect_sn2(struct xpc_channel *ch)
1807 {
1808         xpc_notify_senders_sn2(ch, ch->reason, ch->sn.sn2.w_local_GP.put);
1809 }
1810
1811 /*
1812  * Clear some of the msg flags in the local message queue.
1813  */
1814 static inline void
1815 xpc_clear_local_msgqueue_flags_sn2(struct xpc_channel *ch)
1816 {
1817         struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
1818         struct xpc_msg *msg;
1819         s64 get;
1820
1821         get = ch_sn2->w_remote_GP.get;
1822         do {
1823                 msg = (struct xpc_msg *)((u64)ch->local_msgqueue +
1824                                          (get % ch->local_nentries) *
1825                                          ch->msg_size);
1826                 msg->flags = 0;
1827         } while (++get < ch_sn2->remote_GP.get);
1828 }
1829
1830 /*
1831  * Clear some of the msg flags in the remote message queue.
1832  */
1833 static inline void
1834 xpc_clear_remote_msgqueue_flags_sn2(struct xpc_channel *ch)
1835 {
1836         struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
1837         struct xpc_msg *msg;
1838         s64 put;
1839
1840         put = ch_sn2->w_remote_GP.put;
1841         do {
1842                 msg = (struct xpc_msg *)((u64)ch->remote_msgqueue +
1843                                          (put % ch->remote_nentries) *
1844                                          ch->msg_size);
1845                 msg->flags = 0;
1846         } while (++put < ch_sn2->remote_GP.put);
1847 }
1848
1849 static void
1850 xpc_process_msg_chctl_flags_sn2(struct xpc_partition *part, int ch_number)
1851 {
1852         struct xpc_channel *ch = &part->channels[ch_number];
1853         struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
1854         int nmsgs_sent;
1855
1856         ch_sn2->remote_GP = part->sn.sn2.remote_GPs[ch_number];
1857
1858         /* See what, if anything, has changed for each connected channel */
1859
1860         xpc_msgqueue_ref(ch);
1861
1862         if (ch_sn2->w_remote_GP.get == ch_sn2->remote_GP.get &&
1863             ch_sn2->w_remote_GP.put == ch_sn2->remote_GP.put) {
1864                 /* nothing changed since GPs were last pulled */
1865                 xpc_msgqueue_deref(ch);
1866                 return;
1867         }
1868
1869         if (!(ch->flags & XPC_C_CONNECTED)) {
1870                 xpc_msgqueue_deref(ch);
1871                 return;
1872         }
1873
1874         /*
1875          * First check to see if messages recently sent by us have been
1876          * received by the other side. (The remote GET value will have
1877          * changed since we last looked at it.)
1878          */
1879
1880         if (ch_sn2->w_remote_GP.get != ch_sn2->remote_GP.get) {
1881
1882                 /*
1883                  * We need to notify any senders that want to be notified
1884                  * that their sent messages have been received by their
1885                  * intended recipients. We need to do this before updating
1886                  * w_remote_GP.get so that we don't allocate the same message
1887                  * queue entries prematurely (see xpc_allocate_msg()).
1888                  */
1889                 if (atomic_read(&ch->n_to_notify) > 0) {
1890                         /*
1891                          * Notify senders that messages sent have been
1892                          * received and delivered by the other side.
1893                          */
1894                         xpc_notify_senders_sn2(ch, xpMsgDelivered,
1895                                                ch_sn2->remote_GP.get);
1896                 }
1897
1898                 /*
1899                  * Clear msg->flags in previously sent messages, so that
1900                  * they're ready for xpc_allocate_msg().
1901                  */
1902                 xpc_clear_local_msgqueue_flags_sn2(ch);
1903
1904                 ch_sn2->w_remote_GP.get = ch_sn2->remote_GP.get;
1905
1906                 dev_dbg(xpc_chan, "w_remote_GP.get changed to %ld, partid=%d, "
1907                         "channel=%d\n", ch_sn2->w_remote_GP.get, ch->partid,
1908                         ch->number);
1909
1910                 /*
1911                  * If anyone was waiting for message queue entries to become
1912                  * available, wake them up.
1913                  */
1914                 if (atomic_read(&ch->n_on_msg_allocate_wq) > 0)
1915                         wake_up(&ch->msg_allocate_wq);
1916         }
1917
1918         /*
1919          * Now check for newly sent messages by the other side. (The remote
1920          * PUT value will have changed since we last looked at it.)
1921          */
1922
1923         if (ch_sn2->w_remote_GP.put != ch_sn2->remote_GP.put) {
1924                 /*
1925                  * Clear msg->flags in previously received messages, so that
1926                  * they're ready for xpc_get_deliverable_msg().
1927                  */
1928                 xpc_clear_remote_msgqueue_flags_sn2(ch);
1929
1930                 ch_sn2->w_remote_GP.put = ch_sn2->remote_GP.put;
1931
1932                 dev_dbg(xpc_chan, "w_remote_GP.put changed to %ld, partid=%d, "
1933                         "channel=%d\n", ch_sn2->w_remote_GP.put, ch->partid,
1934                         ch->number);
1935
1936                 nmsgs_sent = ch_sn2->w_remote_GP.put - ch_sn2->w_local_GP.get;
1937                 if (nmsgs_sent > 0) {
1938                         dev_dbg(xpc_chan, "msgs waiting to be copied and "
1939                                 "delivered=%d, partid=%d, channel=%d\n",
1940                                 nmsgs_sent, ch->partid, ch->number);
1941
1942                         if (ch->flags & XPC_C_CONNECTEDCALLOUT_MADE)
1943                                 xpc_activate_kthreads(ch, nmsgs_sent);
1944                 }
1945         }
1946
1947         xpc_msgqueue_deref(ch);
1948 }
1949
1950 static struct xpc_msg *
1951 xpc_pull_remote_msg_sn2(struct xpc_channel *ch, s64 get)
1952 {
1953         struct xpc_partition *part = &xpc_partitions[ch->partid];
1954         struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
1955         struct xpc_msg *remote_msg, *msg;
1956         u32 msg_index, nmsgs;
1957         u64 msg_offset;
1958         enum xp_retval ret;
1959
1960         if (mutex_lock_interruptible(&ch_sn2->msg_to_pull_mutex) != 0) {
1961                 /* we were interrupted by a signal */
1962                 return NULL;
1963         }
1964
1965         while (get >= ch_sn2->next_msg_to_pull) {
1966
1967                 /* pull as many messages as are ready and able to be pulled */
1968
1969                 msg_index = ch_sn2->next_msg_to_pull % ch->remote_nentries;
1970
1971                 DBUG_ON(ch_sn2->next_msg_to_pull >= ch_sn2->w_remote_GP.put);
1972                 nmsgs = ch_sn2->w_remote_GP.put - ch_sn2->next_msg_to_pull;
1973                 if (msg_index + nmsgs > ch->remote_nentries) {
1974                         /* ignore the ones that wrap the msg queue for now */
1975                         nmsgs = ch->remote_nentries - msg_index;
1976                 }
1977
1978                 msg_offset = msg_index * ch->msg_size;
1979                 msg = (struct xpc_msg *)((u64)ch->remote_msgqueue + msg_offset);
1980                 remote_msg = (struct xpc_msg *)(ch->remote_msgqueue_pa +
1981                                                 msg_offset);
1982
1983                 ret = xpc_pull_remote_cachelines_sn2(part, msg, remote_msg,
1984                                                      nmsgs * ch->msg_size);
1985                 if (ret != xpSuccess) {
1986
1987                         dev_dbg(xpc_chan, "failed to pull %d msgs starting with"
1988                                 " msg %ld from partition %d, channel=%d, "
1989                                 "ret=%d\n", nmsgs, ch_sn2->next_msg_to_pull,
1990                                 ch->partid, ch->number, ret);
1991
1992                         XPC_DEACTIVATE_PARTITION(part, ret);
1993
1994                         mutex_unlock(&ch_sn2->msg_to_pull_mutex);
1995                         return NULL;
1996                 }
1997
1998                 ch_sn2->next_msg_to_pull += nmsgs;
1999         }
2000
2001         mutex_unlock(&ch_sn2->msg_to_pull_mutex);
2002
2003         /* return the message we were looking for */
2004         msg_offset = (get % ch->remote_nentries) * ch->msg_size;
2005         msg = (struct xpc_msg *)((u64)ch->remote_msgqueue + msg_offset);
2006
2007         return msg;
2008 }
2009
2010 static int
2011 xpc_n_of_deliverable_msgs_sn2(struct xpc_channel *ch)
2012 {
2013         return ch->sn.sn2.w_remote_GP.put - ch->sn.sn2.w_local_GP.get;
2014 }
2015
2016 /*
2017  * Get a message to be delivered.
2018  */
2019 static struct xpc_msg *
2020 xpc_get_deliverable_msg_sn2(struct xpc_channel *ch)
2021 {
2022         struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
2023         struct xpc_msg *msg = NULL;
2024         s64 get;
2025
2026         do {
2027                 if (ch->flags & XPC_C_DISCONNECTING)
2028                         break;
2029
2030                 get = ch_sn2->w_local_GP.get;
2031                 rmb();  /* guarantee that .get loads before .put */
2032                 if (get == ch_sn2->w_remote_GP.put)
2033                         break;
2034
2035                 /* There are messages waiting to be pulled and delivered.
2036                  * We need to try to secure one for ourselves. We'll do this
2037                  * by trying to increment w_local_GP.get and hope that no one
2038                  * else beats us to it. If they do, we'll we'll simply have
2039                  * to try again for the next one.
2040                  */
2041
2042                 if (cmpxchg(&ch_sn2->w_local_GP.get, get, get + 1) == get) {
2043                         /* we got the entry referenced by get */
2044
2045                         dev_dbg(xpc_chan, "w_local_GP.get changed to %ld, "
2046                                 "partid=%d, channel=%d\n", get + 1,
2047                                 ch->partid, ch->number);
2048
2049                         /* pull the message from the remote partition */
2050
2051                         msg = xpc_pull_remote_msg_sn2(ch, get);
2052
2053                         DBUG_ON(msg != NULL && msg->number != get);
2054                         DBUG_ON(msg != NULL && (msg->flags & XPC_M_DONE));
2055                         DBUG_ON(msg != NULL && !(msg->flags & XPC_M_READY));
2056
2057                         break;
2058                 }
2059
2060         } while (1);
2061
2062         return msg;
2063 }
2064
2065 /*
2066  * Now we actually send the messages that are ready to be sent by advancing
2067  * the local message queue's Put value and then send a chctl msgrequest to the
2068  * recipient partition.
2069  */
2070 static void
2071 xpc_send_msgs_sn2(struct xpc_channel *ch, s64 initial_put)
2072 {
2073         struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
2074         struct xpc_msg *msg;
2075         s64 put = initial_put + 1;
2076         int send_msgrequest = 0;
2077
2078         while (1) {
2079
2080                 while (1) {
2081                         if (put == ch_sn2->w_local_GP.put)
2082                                 break;
2083
2084                         msg = (struct xpc_msg *)((u64)ch->local_msgqueue +
2085                                                  (put % ch->local_nentries) *
2086                                                  ch->msg_size);
2087
2088                         if (!(msg->flags & XPC_M_READY))
2089                                 break;
2090
2091                         put++;
2092                 }
2093
2094                 if (put == initial_put) {
2095                         /* nothing's changed */
2096                         break;
2097                 }
2098
2099                 if (cmpxchg_rel(&ch_sn2->local_GP->put, initial_put, put) !=
2100                     initial_put) {
2101                         /* someone else beat us to it */
2102                         DBUG_ON(ch_sn2->local_GP->put < initial_put);
2103                         break;
2104                 }
2105
2106                 /* we just set the new value of local_GP->put */
2107
2108                 dev_dbg(xpc_chan, "local_GP->put changed to %ld, partid=%d, "
2109                         "channel=%d\n", put, ch->partid, ch->number);
2110
2111                 send_msgrequest = 1;
2112
2113                 /*
2114                  * We need to ensure that the message referenced by
2115                  * local_GP->put is not XPC_M_READY or that local_GP->put
2116                  * equals w_local_GP.put, so we'll go have a look.
2117                  */
2118                 initial_put = put;
2119         }
2120
2121         if (send_msgrequest)
2122                 xpc_send_chctl_msgrequest_sn2(ch);
2123 }
2124
2125 /*
2126  * Allocate an entry for a message from the message queue associated with the
2127  * specified channel.
2128  */
2129 static enum xp_retval
2130 xpc_allocate_msg_sn2(struct xpc_channel *ch, u32 flags,
2131                      struct xpc_msg **address_of_msg)
2132 {
2133         struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
2134         struct xpc_msg *msg;
2135         enum xp_retval ret;
2136         s64 put;
2137
2138         /*
2139          * Get the next available message entry from the local message queue.
2140          * If none are available, we'll make sure that we grab the latest
2141          * GP values.
2142          */
2143         ret = xpTimeout;
2144
2145         while (1) {
2146
2147                 put = ch_sn2->w_local_GP.put;
2148                 rmb();  /* guarantee that .put loads before .get */
2149                 if (put - ch_sn2->w_remote_GP.get < ch->local_nentries) {
2150
2151                         /* There are available message entries. We need to try
2152                          * to secure one for ourselves. We'll do this by trying
2153                          * to increment w_local_GP.put as long as someone else
2154                          * doesn't beat us to it. If they do, we'll have to
2155                          * try again.
2156                          */
2157                         if (cmpxchg(&ch_sn2->w_local_GP.put, put, put + 1) ==
2158                             put) {
2159                                 /* we got the entry referenced by put */
2160                                 break;
2161                         }
2162                         continue;       /* try again */
2163                 }
2164
2165                 /*
2166                  * There aren't any available msg entries at this time.
2167                  *
2168                  * In waiting for a message entry to become available,
2169                  * we set a timeout in case the other side is not sending
2170                  * completion interrupts. This lets us fake a notify IRQ
2171                  * that will cause the notify IRQ handler to fetch the latest
2172                  * GP values as if an interrupt was sent by the other side.
2173                  */
2174                 if (ret == xpTimeout)
2175                         xpc_send_chctl_local_msgrequest_sn2(ch);
2176
2177                 if (flags & XPC_NOWAIT)
2178                         return xpNoWait;
2179
2180                 ret = xpc_allocate_msg_wait(ch);
2181                 if (ret != xpInterrupted && ret != xpTimeout)
2182                         return ret;
2183         }
2184
2185         /* get the message's address and initialize it */
2186         msg = (struct xpc_msg *)((u64)ch->local_msgqueue +
2187                                  (put % ch->local_nentries) * ch->msg_size);
2188
2189         DBUG_ON(msg->flags != 0);
2190         msg->number = put;
2191
2192         dev_dbg(xpc_chan, "w_local_GP.put changed to %ld; msg=0x%p, "
2193                 "msg_number=%ld, partid=%d, channel=%d\n", put + 1,
2194                 (void *)msg, msg->number, ch->partid, ch->number);
2195
2196         *address_of_msg = msg;
2197         return xpSuccess;
2198 }
2199
2200 /*
2201  * Common code that does the actual sending of the message by advancing the
2202  * local message queue's Put value and sends a chctl msgrequest to the
2203  * partition the message is being sent to.
2204  */
2205 static enum xp_retval
2206 xpc_send_msg_sn2(struct xpc_channel *ch, u32 flags, void *payload,
2207                  u16 payload_size, u8 notify_type, xpc_notify_func func,
2208                  void *key)
2209 {
2210         enum xp_retval ret = xpSuccess;
2211         struct xpc_msg *msg = msg;
2212         struct xpc_notify *notify = notify;
2213         s64 msg_number;
2214         s64 put;
2215
2216         DBUG_ON(notify_type == XPC_N_CALL && func == NULL);
2217
2218         if (XPC_MSG_SIZE(payload_size) > ch->msg_size)
2219                 return xpPayloadTooBig;
2220
2221         xpc_msgqueue_ref(ch);
2222
2223         if (ch->flags & XPC_C_DISCONNECTING) {
2224                 ret = ch->reason;
2225                 goto out_1;
2226         }
2227         if (!(ch->flags & XPC_C_CONNECTED)) {
2228                 ret = xpNotConnected;
2229                 goto out_1;
2230         }
2231
2232         ret = xpc_allocate_msg_sn2(ch, flags, &msg);
2233         if (ret != xpSuccess)
2234                 goto out_1;
2235
2236         msg_number = msg->number;
2237
2238         if (notify_type != 0) {
2239                 /*
2240                  * Tell the remote side to send an ACK interrupt when the
2241                  * message has been delivered.
2242                  */
2243                 msg->flags |= XPC_M_INTERRUPT;
2244
2245                 atomic_inc(&ch->n_to_notify);
2246
2247                 notify = &ch->notify_queue[msg_number % ch->local_nentries];
2248                 notify->func = func;
2249                 notify->key = key;
2250                 notify->type = notify_type;
2251
2252                 /* ??? Is a mb() needed here? */
2253
2254                 if (ch->flags & XPC_C_DISCONNECTING) {
2255                         /*
2256                          * An error occurred between our last error check and
2257                          * this one. We will try to clear the type field from
2258                          * the notify entry. If we succeed then
2259                          * xpc_disconnect_channel() didn't already process
2260                          * the notify entry.
2261                          */
2262                         if (cmpxchg(&notify->type, notify_type, 0) ==
2263                             notify_type) {
2264                                 atomic_dec(&ch->n_to_notify);
2265                                 ret = ch->reason;
2266                         }
2267                         goto out_1;
2268                 }
2269         }
2270
2271         memcpy(&msg->payload, payload, payload_size);
2272
2273         msg->flags |= XPC_M_READY;
2274
2275         /*
2276          * The preceding store of msg->flags must occur before the following
2277          * load of local_GP->put.
2278          */
2279         mb();
2280
2281         /* see if the message is next in line to be sent, if so send it */
2282
2283         put = ch->sn.sn2.local_GP->put;
2284         if (put == msg_number)
2285                 xpc_send_msgs_sn2(ch, put);
2286
2287 out_1:
2288         xpc_msgqueue_deref(ch);
2289         return ret;
2290 }
2291
2292 /*
2293  * Now we actually acknowledge the messages that have been delivered and ack'd
2294  * by advancing the cached remote message queue's Get value and if requested
2295  * send a chctl msgrequest to the message sender's partition.
2296  */
2297 static void
2298 xpc_acknowledge_msgs_sn2(struct xpc_channel *ch, s64 initial_get, u8 msg_flags)
2299 {
2300         struct xpc_channel_sn2 *ch_sn2 = &ch->sn.sn2;
2301         struct xpc_msg *msg;
2302         s64 get = initial_get + 1;
2303         int send_msgrequest = 0;
2304
2305         while (1) {
2306
2307                 while (1) {
2308                         if (get == ch_sn2->w_local_GP.get)
2309                                 break;
2310
2311                         msg = (struct xpc_msg *)((u64)ch->remote_msgqueue +
2312                                                  (get % ch->remote_nentries) *
2313                                                  ch->msg_size);
2314
2315                         if (!(msg->flags & XPC_M_DONE))
2316                                 break;
2317
2318                         msg_flags |= msg->flags;
2319                         get++;
2320                 }
2321
2322                 if (get == initial_get) {
2323                         /* nothing's changed */
2324                         break;
2325                 }
2326
2327                 if (cmpxchg_rel(&ch_sn2->local_GP->get, initial_get, get) !=
2328                     initial_get) {
2329                         /* someone else beat us to it */
2330                         DBUG_ON(ch_sn2->local_GP->get <= initial_get);
2331                         break;
2332                 }
2333
2334                 /* we just set the new value of local_GP->get */
2335
2336                 dev_dbg(xpc_chan, "local_GP->get changed to %ld, partid=%d, "
2337                         "channel=%d\n", get, ch->partid, ch->number);
2338
2339                 send_msgrequest = (msg_flags & XPC_M_INTERRUPT);
2340
2341                 /*
2342                  * We need to ensure that the message referenced by
2343                  * local_GP->get is not XPC_M_DONE or that local_GP->get
2344                  * equals w_local_GP.get, so we'll go have a look.
2345                  */
2346                 initial_get = get;
2347         }
2348
2349         if (send_msgrequest)
2350                 xpc_send_chctl_msgrequest_sn2(ch);
2351 }
2352
2353 static void
2354 xpc_received_msg_sn2(struct xpc_channel *ch, struct xpc_msg *msg)
2355 {
2356         s64 get;
2357         s64 msg_number = msg->number;
2358
2359         dev_dbg(xpc_chan, "msg=0x%p, msg_number=%ld, partid=%d, channel=%d\n",
2360                 (void *)msg, msg_number, ch->partid, ch->number);
2361
2362         DBUG_ON((((u64)msg - (u64)ch->remote_msgqueue) / ch->msg_size) !=
2363                 msg_number % ch->remote_nentries);
2364         DBUG_ON(msg->flags & XPC_M_DONE);
2365
2366         msg->flags |= XPC_M_DONE;
2367
2368         /*
2369          * The preceding store of msg->flags must occur before the following
2370          * load of local_GP->get.
2371          */
2372         mb();
2373
2374         /*
2375          * See if this message is next in line to be acknowledged as having
2376          * been delivered.
2377          */
2378         get = ch->sn.sn2.local_GP->get;
2379         if (get == msg_number)
2380                 xpc_acknowledge_msgs_sn2(ch, get, msg->flags);
2381 }
2382
2383 int
2384 xpc_init_sn2(void)
2385 {
2386         int ret;
2387         size_t buf_size;
2388
2389         xpc_rsvd_page_init = xpc_rsvd_page_init_sn2;
2390         xpc_increment_heartbeat = xpc_increment_heartbeat_sn2;
2391         xpc_offline_heartbeat = xpc_offline_heartbeat_sn2;
2392         xpc_online_heartbeat = xpc_online_heartbeat_sn2;
2393         xpc_heartbeat_init = xpc_heartbeat_init_sn2;
2394         xpc_heartbeat_exit = xpc_heartbeat_exit_sn2;
2395         xpc_check_remote_hb = xpc_check_remote_hb_sn2;
2396
2397         xpc_request_partition_activation = xpc_request_partition_activation_sn2;
2398         xpc_request_partition_reactivation =
2399             xpc_request_partition_reactivation_sn2;
2400         xpc_request_partition_deactivation =
2401             xpc_request_partition_deactivation_sn2;
2402         xpc_cancel_partition_deactivation_request =
2403             xpc_cancel_partition_deactivation_request_sn2;
2404
2405         xpc_process_activate_IRQ_rcvd = xpc_process_activate_IRQ_rcvd_sn2;
2406         xpc_setup_infrastructure = xpc_setup_infrastructure_sn2;
2407         xpc_teardown_infrastructure = xpc_teardown_infrastructure_sn2;
2408         xpc_make_first_contact = xpc_make_first_contact_sn2;
2409         xpc_get_chctl_all_flags = xpc_get_chctl_all_flags_sn2;
2410         xpc_allocate_msgqueues = xpc_allocate_msgqueues_sn2;
2411         xpc_free_msgqueues = xpc_free_msgqueues_sn2;
2412         xpc_notify_senders_of_disconnect = xpc_notify_senders_of_disconnect_sn2;
2413         xpc_process_msg_chctl_flags = xpc_process_msg_chctl_flags_sn2;
2414         xpc_n_of_deliverable_msgs = xpc_n_of_deliverable_msgs_sn2;
2415         xpc_get_deliverable_msg = xpc_get_deliverable_msg_sn2;
2416
2417         xpc_indicate_partition_engaged = xpc_indicate_partition_engaged_sn2;
2418         xpc_partition_engaged = xpc_partition_engaged_sn2;
2419         xpc_any_partition_engaged = xpc_any_partition_engaged_sn2;
2420         xpc_indicate_partition_disengaged =
2421             xpc_indicate_partition_disengaged_sn2;
2422         xpc_assume_partition_disengaged = xpc_assume_partition_disengaged_sn2;
2423
2424         xpc_send_chctl_closerequest = xpc_send_chctl_closerequest_sn2;
2425         xpc_send_chctl_closereply = xpc_send_chctl_closereply_sn2;
2426         xpc_send_chctl_openrequest = xpc_send_chctl_openrequest_sn2;
2427         xpc_send_chctl_openreply = xpc_send_chctl_openreply_sn2;
2428
2429         xpc_send_msg = xpc_send_msg_sn2;
2430         xpc_received_msg = xpc_received_msg_sn2;
2431
2432         buf_size = max(XPC_RP_VARS_SIZE,
2433                        XPC_RP_HEADER_SIZE + XP_NASID_MASK_BYTES_SN2);
2434         xpc_remote_copy_buffer_sn2 = xpc_kmalloc_cacheline_aligned(buf_size,
2435                                                                    GFP_KERNEL,
2436                                               &xpc_remote_copy_buffer_base_sn2);
2437         if (xpc_remote_copy_buffer_sn2 == NULL) {
2438                 dev_err(xpc_part, "can't get memory for remote copy buffer\n");
2439                 return -ENOMEM;
2440         }
2441
2442         /* open up protections for IPI and [potentially] amo operations */
2443         xpc_allow_IPI_ops_sn2();
2444         xpc_allow_amo_ops_shub_wars_1_1_sn2();
2445
2446         /*
2447          * This is safe to do before the xpc_hb_checker thread has started
2448          * because the handler releases a wait queue.  If an interrupt is
2449          * received before the thread is waiting, it will not go to sleep,
2450          * but rather immediately process the interrupt.
2451          */
2452         ret = request_irq(SGI_XPC_ACTIVATE, xpc_handle_activate_IRQ_sn2, 0,
2453                           "xpc hb", NULL);
2454         if (ret != 0) {
2455                 dev_err(xpc_part, "can't register ACTIVATE IRQ handler, "
2456                         "errno=%d\n", -ret);
2457                 xpc_disallow_IPI_ops_sn2();
2458                 kfree(xpc_remote_copy_buffer_base_sn2);
2459         }
2460         return ret;
2461 }
2462
2463 void
2464 xpc_exit_sn2(void)
2465 {
2466         free_irq(SGI_XPC_ACTIVATE, NULL);
2467         xpc_disallow_IPI_ops_sn2();
2468         kfree(xpc_remote_copy_buffer_base_sn2);
2469 }