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