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