sgi-xp: move xpc_allocate() into xpc_send()/xpc_send_notify()
[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 /*
26  * The following set of macros and functions are used for the sending and
27  * receiving of IPIs (also known as IRQs). There are two flavors of IPIs,
28  * one that is associated with partition activity (SGI_XPC_ACTIVATE) and
29  * the other that is associated with channel activity (SGI_XPC_NOTIFY).
30  */
31
32 static u64
33 xpc_IPI_receive_sn2(AMO_t *amo)
34 {
35         return FETCHOP_LOAD_OP(TO_AMO((u64)&amo->variable), FETCHOP_CLEAR);
36 }
37
38 static enum xp_retval
39 xpc_IPI_send_sn2(AMO_t *amo, u64 flag, int nasid, int phys_cpuid, int vector)
40 {
41         int ret = 0;
42         unsigned long irq_flags;
43
44         local_irq_save(irq_flags);
45
46         FETCHOP_STORE_OP(TO_AMO((u64)&amo->variable), FETCHOP_OR, flag);
47         sn_send_IPI_phys(nasid, phys_cpuid, vector, 0);
48
49         /*
50          * We must always use the nofault function regardless of whether we
51          * are on a Shub 1.1 system or a Shub 1.2 slice 0xc processor. If we
52          * didn't, we'd never know that the other partition is down and would
53          * keep sending IPIs and AMOs to it until the heartbeat times out.
54          */
55         ret = xp_nofault_PIOR((u64 *)GLOBAL_MMR_ADDR(NASID_GET(&amo->variable),
56                                                      xp_nofault_PIOR_target));
57
58         local_irq_restore(irq_flags);
59
60         return ((ret == 0) ? xpSuccess : xpPioReadError);
61 }
62
63 static AMO_t *
64 xpc_IPI_init_sn2(int index)
65 {
66         AMO_t *amo = xpc_vars->amos_page + index;
67
68         (void)xpc_IPI_receive_sn2(amo); /* clear AMO variable */
69         return amo;
70 }
71
72 /*
73  * IPIs associated with SGI_XPC_ACTIVATE IRQ.
74  */
75
76 /*
77  * Flag the appropriate AMO variable and send an IPI to the specified node.
78  */
79 static void
80 xpc_activate_IRQ_send_sn2(u64 amos_page_pa, int from_nasid, int to_nasid,
81                       int to_phys_cpuid)
82 {
83         int w_index = XPC_NASID_W_INDEX(from_nasid);
84         int b_index = XPC_NASID_B_INDEX(from_nasid);
85         AMO_t *amos = (AMO_t *)__va(amos_page_pa +
86                                     (XPC_ACTIVATE_IRQ_AMOS * sizeof(AMO_t)));
87
88         (void)xpc_IPI_send_sn2(&amos[w_index], (1UL << b_index), to_nasid,
89                                to_phys_cpuid, SGI_XPC_ACTIVATE);
90 }
91
92 static void
93 xpc_activate_IRQ_send_local_sn2(int from_nasid)
94 {
95         int w_index = XPC_NASID_W_INDEX(from_nasid);
96         int b_index = XPC_NASID_B_INDEX(from_nasid);
97         AMO_t *amos = (AMO_t *)__va(xpc_vars->amos_page_pa +
98                                     (XPC_ACTIVATE_IRQ_AMOS * sizeof(AMO_t)));
99
100         /* fake the sending and receipt of an activate IRQ from remote nasid */
101         FETCHOP_STORE_OP(TO_AMO((u64)&amos[w_index].variable), FETCHOP_OR,
102                          (1UL << b_index));
103         atomic_inc(&xpc_act_IRQ_rcvd);
104         wake_up_interruptible(&xpc_act_IRQ_wq);
105 }
106
107 static void
108 xpc_IPI_send_local_activate_sn2(int from_nasid)
109 {
110         xpc_activate_IRQ_send_local_sn2(from_nasid);
111 }
112
113 static void
114 xpc_IPI_send_activated_sn2(struct xpc_partition *part)
115 {
116         xpc_activate_IRQ_send_sn2(part->remote_amos_page_pa,
117                                   cnodeid_to_nasid(0), part->remote_act_nasid,
118                                   part->remote_act_phys_cpuid);
119 }
120
121 static void
122 xpc_IPI_send_local_reactivate_sn2(int from_nasid)
123 {
124         xpc_activate_IRQ_send_local_sn2(from_nasid);
125 }
126
127 static void
128 xpc_IPI_send_disengage_sn2(struct xpc_partition *part)
129 {
130         xpc_activate_IRQ_send_sn2(part->remote_amos_page_pa,
131                                   cnodeid_to_nasid(0), part->remote_act_nasid,
132                                   part->remote_act_phys_cpuid);
133 }
134
135 /*
136  * IPIs associated with SGI_XPC_NOTIFY IRQ.
137  */
138
139 /*
140  * Send an IPI to the remote partition that is associated with the
141  * specified channel.
142  */
143 static void
144 xpc_notify_IRQ_send_sn2(struct xpc_channel *ch, u8 ipi_flag,
145                         char *ipi_flag_string, unsigned long *irq_flags)
146 {
147         struct xpc_partition *part = &xpc_partitions[ch->partid];
148         enum xp_retval ret;
149
150         if (likely(part->act_state != XPC_P_DEACTIVATING)) {
151                 ret = xpc_IPI_send_sn2(part->remote_IPI_amo_va,
152                                        (u64)ipi_flag << (ch->number * 8),
153                                        part->remote_IPI_nasid,
154                                        part->remote_IPI_phys_cpuid,
155                                        SGI_XPC_NOTIFY);
156                 dev_dbg(xpc_chan, "%s sent to partid=%d, channel=%d, ret=%d\n",
157                         ipi_flag_string, ch->partid, ch->number, ret);
158                 if (unlikely(ret != xpSuccess)) {
159                         if (irq_flags != NULL)
160                                 spin_unlock_irqrestore(&ch->lock, *irq_flags);
161                         XPC_DEACTIVATE_PARTITION(part, ret);
162                         if (irq_flags != NULL)
163                                 spin_lock_irqsave(&ch->lock, *irq_flags);
164                 }
165         }
166 }
167
168 #define XPC_NOTIFY_IRQ_SEND_SN2(_ch, _ipi_f, _irq_f) \
169                 xpc_notify_IRQ_send_sn2(_ch, _ipi_f, #_ipi_f, _irq_f)
170
171 /*
172  * Make it look like the remote partition, which is associated with the
173  * specified channel, sent us an IPI. This faked IPI will be handled
174  * by xpc_dropped_IPI_check().
175  */
176 static void
177 xpc_notify_IRQ_send_local_sn2(struct xpc_channel *ch, u8 ipi_flag,
178                               char *ipi_flag_string)
179 {
180         struct xpc_partition *part = &xpc_partitions[ch->partid];
181
182         FETCHOP_STORE_OP(TO_AMO((u64)&part->local_IPI_amo_va->variable),
183                          FETCHOP_OR, ((u64)ipi_flag << (ch->number * 8)));
184         dev_dbg(xpc_chan, "%s sent local from partid=%d, channel=%d\n",
185                 ipi_flag_string, ch->partid, ch->number);
186 }
187
188 #define XPC_NOTIFY_IRQ_SEND_LOCAL_SN2(_ch, _ipi_f) \
189                 xpc_notify_IRQ_send_local_sn2(_ch, _ipi_f, #_ipi_f)
190
191 static void
192 xpc_IPI_send_closerequest_sn2(struct xpc_channel *ch, unsigned long *irq_flags)
193 {
194         struct xpc_openclose_args *args = ch->local_openclose_args;
195
196         args->reason = ch->reason;
197         XPC_NOTIFY_IRQ_SEND_SN2(ch, XPC_IPI_CLOSEREQUEST, irq_flags);
198 }
199
200 static void
201 xpc_IPI_send_closereply_sn2(struct xpc_channel *ch, unsigned long *irq_flags)
202 {
203         XPC_NOTIFY_IRQ_SEND_SN2(ch, XPC_IPI_CLOSEREPLY, irq_flags);
204 }
205
206 static void
207 xpc_IPI_send_openrequest_sn2(struct xpc_channel *ch, unsigned long *irq_flags)
208 {
209         struct xpc_openclose_args *args = ch->local_openclose_args;
210
211         args->msg_size = ch->msg_size;
212         args->local_nentries = ch->local_nentries;
213         XPC_NOTIFY_IRQ_SEND_SN2(ch, XPC_IPI_OPENREQUEST, irq_flags);
214 }
215
216 static void
217 xpc_IPI_send_openreply_sn2(struct xpc_channel *ch, unsigned long *irq_flags)
218 {
219         struct xpc_openclose_args *args = ch->local_openclose_args;
220
221         args->remote_nentries = ch->remote_nentries;
222         args->local_nentries = ch->local_nentries;
223         args->local_msgqueue_pa = __pa(ch->local_msgqueue);
224         XPC_NOTIFY_IRQ_SEND_SN2(ch, XPC_IPI_OPENREPLY, irq_flags);
225 }
226
227 static void
228 xpc_IPI_send_msgrequest_sn2(struct xpc_channel *ch)
229 {
230         XPC_NOTIFY_IRQ_SEND_SN2(ch, XPC_IPI_MSGREQUEST, NULL);
231 }
232
233 static void
234 xpc_IPI_send_local_msgrequest_sn2(struct xpc_channel *ch)
235 {
236         XPC_NOTIFY_IRQ_SEND_LOCAL_SN2(ch, XPC_IPI_MSGREQUEST);
237 }
238
239 /*
240  * This next set of functions are used to keep track of when a partition is
241  * potentially engaged in accessing memory belonging to another partition.
242  */
243
244 static void
245 xpc_mark_partition_engaged_sn2(struct xpc_partition *part)
246 {
247         unsigned long irq_flags;
248         AMO_t *amo = (AMO_t *)__va(part->remote_amos_page_pa +
249                                    (XPC_ENGAGED_PARTITIONS_AMO *
250                                     sizeof(AMO_t)));
251
252         local_irq_save(irq_flags);
253
254         /* set bit corresponding to our partid in remote partition's AMO */
255         FETCHOP_STORE_OP(TO_AMO((u64)&amo->variable), FETCHOP_OR,
256                          (1UL << sn_partition_id));
257         /*
258          * We must always use the nofault function regardless of whether we
259          * are on a Shub 1.1 system or a Shub 1.2 slice 0xc processor. If we
260          * didn't, we'd never know that the other partition is down and would
261          * keep sending IPIs and AMOs to it until the heartbeat times out.
262          */
263         (void)xp_nofault_PIOR((u64 *)GLOBAL_MMR_ADDR(NASID_GET(&amo->
264                                                                variable),
265                                                      xp_nofault_PIOR_target));
266
267         local_irq_restore(irq_flags);
268 }
269
270 static void
271 xpc_mark_partition_disengaged_sn2(struct xpc_partition *part)
272 {
273         unsigned long irq_flags;
274         AMO_t *amo = (AMO_t *)__va(part->remote_amos_page_pa +
275                                    (XPC_ENGAGED_PARTITIONS_AMO *
276                                     sizeof(AMO_t)));
277
278         local_irq_save(irq_flags);
279
280         /* clear bit corresponding to our partid in remote partition's AMO */
281         FETCHOP_STORE_OP(TO_AMO((u64)&amo->variable), FETCHOP_AND,
282                          ~(1UL << sn_partition_id));
283         /*
284          * We must always use the nofault function regardless of whether we
285          * are on a Shub 1.1 system or a Shub 1.2 slice 0xc processor. If we
286          * didn't, we'd never know that the other partition is down and would
287          * keep sending IPIs and AMOs to it until the heartbeat times out.
288          */
289         (void)xp_nofault_PIOR((u64 *)GLOBAL_MMR_ADDR(NASID_GET(&amo->
290                                                                variable),
291                                                      xp_nofault_PIOR_target));
292
293         local_irq_restore(irq_flags);
294 }
295
296 static void
297 xpc_request_partition_disengage_sn2(struct xpc_partition *part)
298 {
299         unsigned long irq_flags;
300         AMO_t *amo = (AMO_t *)__va(part->remote_amos_page_pa +
301                                    (XPC_DISENGAGE_REQUEST_AMO * sizeof(AMO_t)));
302
303         local_irq_save(irq_flags);
304
305         /* set bit corresponding to our partid in remote partition's AMO */
306         FETCHOP_STORE_OP(TO_AMO((u64)&amo->variable), FETCHOP_OR,
307                          (1UL << sn_partition_id));
308         /*
309          * We must always use the nofault function regardless of whether we
310          * are on a Shub 1.1 system or a Shub 1.2 slice 0xc processor. If we
311          * didn't, we'd never know that the other partition is down and would
312          * keep sending IPIs and AMOs to it until the heartbeat times out.
313          */
314         (void)xp_nofault_PIOR((u64 *)GLOBAL_MMR_ADDR(NASID_GET(&amo->
315                                                                variable),
316                                                      xp_nofault_PIOR_target));
317
318         local_irq_restore(irq_flags);
319 }
320
321 static void
322 xpc_cancel_partition_disengage_request_sn2(struct xpc_partition *part)
323 {
324         unsigned long irq_flags;
325         AMO_t *amo = (AMO_t *)__va(part->remote_amos_page_pa +
326                                    (XPC_DISENGAGE_REQUEST_AMO * sizeof(AMO_t)));
327
328         local_irq_save(irq_flags);
329
330         /* clear bit corresponding to our partid in remote partition's AMO */
331         FETCHOP_STORE_OP(TO_AMO((u64)&amo->variable), FETCHOP_AND,
332                          ~(1UL << sn_partition_id));
333         /*
334          * We must always use the nofault function regardless of whether we
335          * are on a Shub 1.1 system or a Shub 1.2 slice 0xc processor. If we
336          * didn't, we'd never know that the other partition is down and would
337          * keep sending IPIs and AMOs to it until the heartbeat times out.
338          */
339         (void)xp_nofault_PIOR((u64 *)GLOBAL_MMR_ADDR(NASID_GET(&amo->
340                                                                variable),
341                                                      xp_nofault_PIOR_target));
342
343         local_irq_restore(irq_flags);
344 }
345
346 static u64
347 xpc_partition_engaged_sn2(u64 partid_mask)
348 {
349         AMO_t *amo = xpc_vars->amos_page + XPC_ENGAGED_PARTITIONS_AMO;
350
351         /* return our partition's AMO variable ANDed with partid_mask */
352         return (FETCHOP_LOAD_OP(TO_AMO((u64)&amo->variable), FETCHOP_LOAD) &
353                 partid_mask);
354 }
355
356 static u64
357 xpc_partition_disengage_requested_sn2(u64 partid_mask)
358 {
359         AMO_t *amo = xpc_vars->amos_page + XPC_DISENGAGE_REQUEST_AMO;
360
361         /* return our partition's AMO variable ANDed with partid_mask */
362         return (FETCHOP_LOAD_OP(TO_AMO((u64)&amo->variable), FETCHOP_LOAD) &
363                 partid_mask);
364 }
365
366 static void
367 xpc_clear_partition_engaged_sn2(u64 partid_mask)
368 {
369         AMO_t *amo = xpc_vars->amos_page + XPC_ENGAGED_PARTITIONS_AMO;
370
371         /* clear bit(s) based on partid_mask in our partition's AMO */
372         FETCHOP_STORE_OP(TO_AMO((u64)&amo->variable), FETCHOP_AND,
373                          ~partid_mask);
374 }
375
376 static void
377 xpc_clear_partition_disengage_request_sn2(u64 partid_mask)
378 {
379         AMO_t *amo = xpc_vars->amos_page + XPC_DISENGAGE_REQUEST_AMO;
380
381         /* clear bit(s) based on partid_mask in our partition's AMO */
382         FETCHOP_STORE_OP(TO_AMO((u64)&amo->variable), FETCHOP_AND,
383                          ~partid_mask);
384 }
385
386 static enum xp_retval
387 xpc_rsvd_page_init_sn2(struct xpc_rsvd_page *rp)
388 {
389         AMO_t *amos_page;
390         u64 nasid_array = 0;
391         int i;
392         int ret;
393
394         xpc_vars = XPC_RP_VARS(rp);
395
396         rp->sn.vars_pa = __pa(xpc_vars);
397
398         /* vars_part array follows immediately after vars */
399         xpc_vars_part = (struct xpc_vars_part_sn2 *)((u8 *)XPC_RP_VARS(rp) +
400                                                      XPC_RP_VARS_SIZE);
401
402
403         /*
404          * Before clearing xpc_vars, see if a page of AMOs had been previously
405          * allocated. If not we'll need to allocate one and set permissions
406          * so that cross-partition AMOs are allowed.
407          *
408          * The allocated AMO page needs MCA reporting to remain disabled after
409          * XPC has unloaded.  To make this work, we keep a copy of the pointer
410          * to this page (i.e., amos_page) in the struct xpc_vars structure,
411          * which is pointed to by the reserved page, and re-use that saved copy
412          * on subsequent loads of XPC. This AMO page is never freed, and its
413          * memory protections are never restricted.
414          */
415         amos_page = xpc_vars->amos_page;
416         if (amos_page == NULL) {
417                 amos_page = (AMO_t *)TO_AMO(uncached_alloc_page(0, 1));
418                 if (amos_page == NULL) {
419                         dev_err(xpc_part, "can't allocate page of AMOs\n");
420                         return xpNoMemory;
421                 }
422
423                 /*
424                  * Open up AMO-R/W to cpu.  This is done for Shub 1.1 systems
425                  * when xpc_allow_IPI_ops() is called via xpc_hb_init().
426                  */
427                 if (!enable_shub_wars_1_1()) {
428                         ret = sn_change_memprotect(ia64_tpa((u64)amos_page),
429                                                    PAGE_SIZE,
430                                                    SN_MEMPROT_ACCESS_CLASS_1,
431                                                    &nasid_array);
432                         if (ret != 0) {
433                                 dev_err(xpc_part, "can't change memory "
434                                         "protections\n");
435                                 uncached_free_page(__IA64_UNCACHED_OFFSET |
436                                                    TO_PHYS((u64)amos_page), 1);
437                                 return xpSalError;
438                         }
439                 }
440         }
441
442         /* clear xpc_vars */
443         memset(xpc_vars, 0, sizeof(struct xpc_vars_sn2));
444
445         xpc_vars->version = XPC_V_VERSION;
446         xpc_vars->act_nasid = cpuid_to_nasid(0);
447         xpc_vars->act_phys_cpuid = cpu_physical_id(0);
448         xpc_vars->vars_part_pa = __pa(xpc_vars_part);
449         xpc_vars->amos_page_pa = ia64_tpa((u64)amos_page);
450         xpc_vars->amos_page = amos_page;        /* save for next load of XPC */
451
452         /* clear xpc_vars_part */
453         memset((u64 *)xpc_vars_part, 0, sizeof(struct xpc_vars_part_sn2) *
454                xp_max_npartitions);
455
456         /* initialize the activate IRQ related AMO variables */
457         for (i = 0; i < xp_nasid_mask_words; i++)
458                 (void)xpc_IPI_init_sn2(XPC_ACTIVATE_IRQ_AMOS + i);
459
460         /* initialize the engaged remote partitions related AMO variables */
461         (void)xpc_IPI_init_sn2(XPC_ENGAGED_PARTITIONS_AMO);
462         (void)xpc_IPI_init_sn2(XPC_DISENGAGE_REQUEST_AMO);
463
464         return xpSuccess;
465 }
466
467 static void
468 xpc_increment_heartbeat_sn2(void)
469 {
470         xpc_vars->heartbeat++;
471 }
472
473 static void
474 xpc_offline_heartbeat_sn2(void)
475 {
476         xpc_increment_heartbeat_sn2();
477         xpc_vars->heartbeat_offline = 1;
478 }
479
480 static void
481 xpc_online_heartbeat_sn2(void)
482 {
483         xpc_increment_heartbeat_sn2();
484         xpc_vars->heartbeat_offline = 0;
485 }
486
487 static void
488 xpc_heartbeat_init_sn2(void)
489 {
490         DBUG_ON(xpc_vars == NULL);
491
492         bitmap_zero(xpc_vars->heartbeating_to_mask, XP_MAX_NPARTITIONS_SN2);
493         xpc_heartbeating_to_mask = &xpc_vars->heartbeating_to_mask[0];
494         xpc_online_heartbeat_sn2();
495 }
496
497 static void
498 xpc_heartbeat_exit_sn2(void)
499 {
500         xpc_offline_heartbeat_sn2();
501 }
502
503 /*
504  * At periodic intervals, scan through all active partitions and ensure
505  * their heartbeat is still active.  If not, the partition is deactivated.
506  */
507 static void
508 xpc_check_remote_hb_sn2(void)
509 {
510         struct xpc_vars_sn2 *remote_vars;
511         struct xpc_partition *part;
512         short partid;
513         enum xp_retval ret;
514
515         remote_vars = (struct xpc_vars_sn2 *)xpc_remote_copy_buffer;
516
517         for (partid = 0; partid < xp_max_npartitions; partid++) {
518
519                 if (xpc_exiting)
520                         break;
521
522                 if (partid == sn_partition_id)
523                         continue;
524
525                 part = &xpc_partitions[partid];
526
527                 if (part->act_state == XPC_P_INACTIVE ||
528                     part->act_state == XPC_P_DEACTIVATING) {
529                         continue;
530                 }
531
532                 /* pull the remote_hb cache line */
533                 ret = xp_remote_memcpy(remote_vars,
534                                        (void *)part->remote_vars_pa,
535                                        XPC_RP_VARS_SIZE);
536                 if (ret != xpSuccess) {
537                         XPC_DEACTIVATE_PARTITION(part, ret);
538                         continue;
539                 }
540
541                 dev_dbg(xpc_part, "partid = %d, heartbeat = %ld, last_heartbeat"
542                         " = %ld, heartbeat_offline = %ld, HB_mask[0] = 0x%lx\n",
543                         partid, remote_vars->heartbeat, part->last_heartbeat,
544                         remote_vars->heartbeat_offline,
545                         remote_vars->heartbeating_to_mask[0]);
546
547                 if (((remote_vars->heartbeat == part->last_heartbeat) &&
548                      (remote_vars->heartbeat_offline == 0)) ||
549                     !xpc_hb_allowed(sn_partition_id,
550                                     &remote_vars->heartbeating_to_mask)) {
551
552                         XPC_DEACTIVATE_PARTITION(part, xpNoHeartbeat);
553                         continue;
554                 }
555
556                 part->last_heartbeat = remote_vars->heartbeat;
557         }
558 }
559
560 /*
561  * Get a copy of the remote partition's XPC variables from the reserved page.
562  *
563  * remote_vars points to a buffer that is cacheline aligned for BTE copies and
564  * assumed to be of size XPC_RP_VARS_SIZE.
565  */
566 static enum xp_retval
567 xpc_get_remote_vars_sn2(u64 remote_vars_pa, struct xpc_vars_sn2 *remote_vars)
568 {
569         enum xp_retval ret;
570
571         if (remote_vars_pa == 0)
572                 return xpVarsNotSet;
573
574         /* pull over the cross partition variables */
575         ret = xp_remote_memcpy(remote_vars, (void *)remote_vars_pa,
576                                XPC_RP_VARS_SIZE);
577         if (ret != xpSuccess)
578                 return ret;
579
580         if (XPC_VERSION_MAJOR(remote_vars->version) !=
581             XPC_VERSION_MAJOR(XPC_V_VERSION)) {
582                 return xpBadVersion;
583         }
584
585         return xpSuccess;
586 }
587
588 static void
589 xpc_initiate_partition_activation_sn2(struct xpc_rsvd_page *remote_rp,
590                                       u64 remote_rp_pa, int nasid)
591 {
592         xpc_IPI_send_local_activate(nasid);
593 }
594
595 /*
596  * Update the remote partition's info.
597  */
598 static void
599 xpc_update_partition_info_sn2(struct xpc_partition *part, u8 remote_rp_version,
600                               unsigned long *remote_rp_stamp, u64 remote_rp_pa,
601                               u64 remote_vars_pa,
602                               struct xpc_vars_sn2 *remote_vars)
603 {
604         part->remote_rp_version = remote_rp_version;
605         dev_dbg(xpc_part, "  remote_rp_version = 0x%016x\n",
606                 part->remote_rp_version);
607
608         part->remote_rp_stamp = *remote_rp_stamp;
609         dev_dbg(xpc_part, "  remote_rp_stamp = 0x%016lx\n",
610                 part->remote_rp_stamp);
611
612         part->remote_rp_pa = remote_rp_pa;
613         dev_dbg(xpc_part, "  remote_rp_pa = 0x%016lx\n", part->remote_rp_pa);
614
615         part->remote_vars_pa = remote_vars_pa;
616         dev_dbg(xpc_part, "  remote_vars_pa = 0x%016lx\n",
617                 part->remote_vars_pa);
618
619         part->last_heartbeat = remote_vars->heartbeat;
620         dev_dbg(xpc_part, "  last_heartbeat = 0x%016lx\n",
621                 part->last_heartbeat);
622
623         part->remote_vars_part_pa = remote_vars->vars_part_pa;
624         dev_dbg(xpc_part, "  remote_vars_part_pa = 0x%016lx\n",
625                 part->remote_vars_part_pa);
626
627         part->remote_act_nasid = remote_vars->act_nasid;
628         dev_dbg(xpc_part, "  remote_act_nasid = 0x%x\n",
629                 part->remote_act_nasid);
630
631         part->remote_act_phys_cpuid = remote_vars->act_phys_cpuid;
632         dev_dbg(xpc_part, "  remote_act_phys_cpuid = 0x%x\n",
633                 part->remote_act_phys_cpuid);
634
635         part->remote_amos_page_pa = remote_vars->amos_page_pa;
636         dev_dbg(xpc_part, "  remote_amos_page_pa = 0x%lx\n",
637                 part->remote_amos_page_pa);
638
639         part->remote_vars_version = remote_vars->version;
640         dev_dbg(xpc_part, "  remote_vars_version = 0x%x\n",
641                 part->remote_vars_version);
642 }
643
644 /*
645  * Prior code has determined the nasid which generated an IPI.  Inspect
646  * that nasid to determine if its partition needs to be activated or
647  * deactivated.
648  *
649  * A partition is consider "awaiting activation" if our partition
650  * flags indicate it is not active and it has a heartbeat.  A
651  * partition is considered "awaiting deactivation" if our partition
652  * flags indicate it is active but it has no heartbeat or it is not
653  * sending its heartbeat to us.
654  *
655  * To determine the heartbeat, the remote nasid must have a properly
656  * initialized reserved page.
657  */
658 static void
659 xpc_identify_act_IRQ_req_sn2(int nasid)
660 {
661         struct xpc_rsvd_page *remote_rp;
662         struct xpc_vars_sn2 *remote_vars;
663         u64 remote_rp_pa;
664         u64 remote_vars_pa;
665         int remote_rp_version;
666         int reactivate = 0;
667         unsigned long remote_rp_stamp = 0;
668         short partid;
669         struct xpc_partition *part;
670         enum xp_retval ret;
671
672         /* pull over the reserved page structure */
673
674         remote_rp = (struct xpc_rsvd_page *)xpc_remote_copy_buffer;
675
676         ret = xpc_get_remote_rp(nasid, NULL, remote_rp, &remote_rp_pa);
677         if (ret != xpSuccess) {
678                 dev_warn(xpc_part, "unable to get reserved page from nasid %d, "
679                          "which sent interrupt, reason=%d\n", nasid, ret);
680                 return;
681         }
682
683         remote_vars_pa = remote_rp->sn.vars_pa;
684         remote_rp_version = remote_rp->version;
685         if (XPC_SUPPORTS_RP_STAMP(remote_rp_version))
686                 remote_rp_stamp = remote_rp->stamp;
687
688         partid = remote_rp->SAL_partid;
689         part = &xpc_partitions[partid];
690
691         /* pull over the cross partition variables */
692
693         remote_vars = (struct xpc_vars_sn2 *)xpc_remote_copy_buffer;
694
695         ret = xpc_get_remote_vars_sn2(remote_vars_pa, remote_vars);
696         if (ret != xpSuccess) {
697
698                 dev_warn(xpc_part, "unable to get XPC variables from nasid %d, "
699                          "which sent interrupt, reason=%d\n", nasid, ret);
700
701                 XPC_DEACTIVATE_PARTITION(part, ret);
702                 return;
703         }
704
705         part->act_IRQ_rcvd++;
706
707         dev_dbg(xpc_part, "partid for nasid %d is %d; IRQs = %d; HB = "
708                 "%ld:0x%lx\n", (int)nasid, (int)partid, part->act_IRQ_rcvd,
709                 remote_vars->heartbeat, remote_vars->heartbeating_to_mask[0]);
710
711         if (xpc_partition_disengaged(part) &&
712             part->act_state == XPC_P_INACTIVE) {
713
714                 xpc_update_partition_info_sn2(part, remote_rp_version,
715                                               &remote_rp_stamp, remote_rp_pa,
716                                               remote_vars_pa, remote_vars);
717
718                 if (XPC_SUPPORTS_DISENGAGE_REQUEST(part->remote_vars_version)) {
719                         if (xpc_partition_disengage_requested(1UL << partid)) {
720                                 /*
721                                  * Other side is waiting on us to disengage,
722                                  * even though we already have.
723                                  */
724                                 return;
725                         }
726
727                 } else {
728                         /* other side doesn't support disengage requests */
729                         xpc_clear_partition_disengage_request(1UL << partid);
730                 }
731
732                 xpc_activate_partition(part);
733                 return;
734         }
735
736         DBUG_ON(part->remote_rp_version == 0);
737         DBUG_ON(part->remote_vars_version == 0);
738
739         if (!XPC_SUPPORTS_RP_STAMP(part->remote_rp_version)) {
740                 DBUG_ON(XPC_SUPPORTS_DISENGAGE_REQUEST(part->
741                                                        remote_vars_version));
742
743                 if (!XPC_SUPPORTS_RP_STAMP(remote_rp_version)) {
744                         DBUG_ON(XPC_SUPPORTS_DISENGAGE_REQUEST(remote_vars->
745                                                                version));
746                         /* see if the other side rebooted */
747                         if (part->remote_amos_page_pa ==
748                             remote_vars->amos_page_pa &&
749                             xpc_hb_allowed(sn_partition_id,
750                                           &remote_vars->heartbeating_to_mask)) {
751                                 /* doesn't look that way, so ignore the IPI */
752                                 return;
753                         }
754                 }
755
756                 /*
757                  * Other side rebooted and previous XPC didn't support the
758                  * disengage request, so we don't need to do anything special.
759                  */
760
761                 xpc_update_partition_info_sn2(part, remote_rp_version,
762                                               &remote_rp_stamp, remote_rp_pa,
763                                               remote_vars_pa, remote_vars);
764                 part->reactivate_nasid = nasid;
765                 XPC_DEACTIVATE_PARTITION(part, xpReactivating);
766                 return;
767         }
768
769         DBUG_ON(!XPC_SUPPORTS_DISENGAGE_REQUEST(part->remote_vars_version));
770
771         if (!XPC_SUPPORTS_RP_STAMP(remote_rp_version)) {
772                 DBUG_ON(!XPC_SUPPORTS_DISENGAGE_REQUEST(remote_vars->version));
773
774                 /*
775                  * Other side rebooted and previous XPC did support the
776                  * disengage request, but the new one doesn't.
777                  */
778
779                 xpc_clear_partition_engaged(1UL << partid);
780                 xpc_clear_partition_disengage_request(1UL << partid);
781
782                 xpc_update_partition_info_sn2(part, remote_rp_version,
783                                               &remote_rp_stamp, remote_rp_pa,
784                                               remote_vars_pa, remote_vars);
785                 reactivate = 1;
786
787         } else {
788                 DBUG_ON(!XPC_SUPPORTS_DISENGAGE_REQUEST(remote_vars->version));
789
790                 if (remote_rp_stamp != part->remote_rp_stamp) {
791
792                         /*
793                          * Other side rebooted and the previous XPC did support
794                          * the disengage request, as does the new one.
795                          */
796
797                         DBUG_ON(xpc_partition_engaged(1UL << partid));
798                         DBUG_ON(xpc_partition_disengage_requested(1UL <<
799                                                                   partid));
800
801                         xpc_update_partition_info_sn2(part, remote_rp_version,
802                                                       &remote_rp_stamp,
803                                                       remote_rp_pa,
804                                                       remote_vars_pa,
805                                                       remote_vars);
806                         reactivate = 1;
807                 }
808         }
809
810         if (part->disengage_request_timeout > 0 &&
811             !xpc_partition_disengaged(part)) {
812                 /* still waiting on other side to disengage from us */
813                 return;
814         }
815
816         if (reactivate) {
817                 part->reactivate_nasid = nasid;
818                 XPC_DEACTIVATE_PARTITION(part, xpReactivating);
819
820         } else if (XPC_SUPPORTS_DISENGAGE_REQUEST(part->remote_vars_version) &&
821                    xpc_partition_disengage_requested(1UL << partid)) {
822                 XPC_DEACTIVATE_PARTITION(part, xpOtherGoingDown);
823         }
824 }
825
826 /*
827  * Loop through the activation AMO variables and process any bits
828  * which are set.  Each bit indicates a nasid sending a partition
829  * activation or deactivation request.
830  *
831  * Return #of IRQs detected.
832  */
833 int
834 xpc_identify_act_IRQ_sender_sn2(void)
835 {
836         int word, bit;
837         u64 nasid_mask;
838         u64 nasid;              /* remote nasid */
839         int n_IRQs_detected = 0;
840         AMO_t *act_amos;
841
842         act_amos = xpc_vars->amos_page + XPC_ACTIVATE_IRQ_AMOS;
843
844         /* scan through act AMO variable looking for non-zero entries */
845         for (word = 0; word < xp_nasid_mask_words; word++) {
846
847                 if (xpc_exiting)
848                         break;
849
850                 nasid_mask = xpc_IPI_receive_sn2(&act_amos[word]);
851                 if (nasid_mask == 0) {
852                         /* no IRQs from nasids in this variable */
853                         continue;
854                 }
855
856                 dev_dbg(xpc_part, "AMO[%d] gave back 0x%lx\n", word,
857                         nasid_mask);
858
859                 /*
860                  * If this nasid has been added to the machine since
861                  * our partition was reset, this will retain the
862                  * remote nasid in our reserved pages machine mask.
863                  * This is used in the event of module reload.
864                  */
865                 xpc_mach_nasids[word] |= nasid_mask;
866
867                 /* locate the nasid(s) which sent interrupts */
868
869                 for (bit = 0; bit < (8 * sizeof(u64)); bit++) {
870                         if (nasid_mask & (1UL << bit)) {
871                                 n_IRQs_detected++;
872                                 nasid = XPC_NASID_FROM_W_B(word, bit);
873                                 dev_dbg(xpc_part, "interrupt from nasid %ld\n",
874                                         nasid);
875                                 xpc_identify_act_IRQ_req_sn2(nasid);
876                         }
877                 }
878         }
879         return n_IRQs_detected;
880 }
881
882 static void
883 xpc_process_act_IRQ_rcvd_sn2(int n_IRQs_expected)
884 {
885         int n_IRQs_detected;
886
887         n_IRQs_detected = xpc_identify_act_IRQ_sender_sn2();
888         if (n_IRQs_detected < n_IRQs_expected) {
889                 /* retry once to help avoid missing AMO */
890                 (void)xpc_identify_act_IRQ_sender_sn2();
891         }
892 }
893
894 /*
895  * Setup the infrastructure necessary to support XPartition Communication
896  * between the specified remote partition and the local one.
897  */
898 static enum xp_retval
899 xpc_setup_infrastructure_sn2(struct xpc_partition *part)
900 {
901         enum xp_retval retval;
902         int ret;
903         int cpuid;
904         int ch_number;
905         struct xpc_channel *ch;
906         struct timer_list *timer;
907         short partid = XPC_PARTID(part);
908
909         /*
910          * Allocate all of the channel structures as a contiguous chunk of
911          * memory.
912          */
913         DBUG_ON(part->channels != NULL);
914         part->channels = kzalloc(sizeof(struct xpc_channel) * XPC_MAX_NCHANNELS,
915                                  GFP_KERNEL);
916         if (part->channels == NULL) {
917                 dev_err(xpc_chan, "can't get memory for channels\n");
918                 return xpNoMemory;
919         }
920
921         /* allocate all the required GET/PUT values */
922
923         part->local_GPs = xpc_kzalloc_cacheline_aligned(XPC_GP_SIZE,
924                                                         GFP_KERNEL,
925                                                         &part->local_GPs_base);
926         if (part->local_GPs == NULL) {
927                 dev_err(xpc_chan, "can't get memory for local get/put "
928                         "values\n");
929                 retval = xpNoMemory;
930                 goto out_1;
931         }
932
933         part->remote_GPs = xpc_kzalloc_cacheline_aligned(XPC_GP_SIZE,
934                                                          GFP_KERNEL,
935                                                          &part->
936                                                          remote_GPs_base);
937         if (part->remote_GPs == NULL) {
938                 dev_err(xpc_chan, "can't get memory for remote get/put "
939                         "values\n");
940                 retval = xpNoMemory;
941                 goto out_2;
942         }
943
944         part->remote_GPs_pa = 0;
945
946         /* allocate all the required open and close args */
947
948         part->local_openclose_args =
949             xpc_kzalloc_cacheline_aligned(XPC_OPENCLOSE_ARGS_SIZE, GFP_KERNEL,
950                                           &part->local_openclose_args_base);
951         if (part->local_openclose_args == NULL) {
952                 dev_err(xpc_chan, "can't get memory for local connect args\n");
953                 retval = xpNoMemory;
954                 goto out_3;
955         }
956
957         part->remote_openclose_args =
958             xpc_kzalloc_cacheline_aligned(XPC_OPENCLOSE_ARGS_SIZE, GFP_KERNEL,
959                                           &part->remote_openclose_args_base);
960         if (part->remote_openclose_args == NULL) {
961                 dev_err(xpc_chan, "can't get memory for remote connect args\n");
962                 retval = xpNoMemory;
963                 goto out_4;
964         }
965
966         part->remote_openclose_args_pa = 0;
967
968         part->local_IPI_amo_va = xpc_IPI_init_sn2(partid);
969         part->local_IPI_amo = 0;
970         spin_lock_init(&part->IPI_lock);
971
972         part->remote_IPI_nasid = 0;
973         part->remote_IPI_phys_cpuid = 0;
974         part->remote_IPI_amo_va = NULL;
975
976         atomic_set(&part->channel_mgr_requests, 1);
977         init_waitqueue_head(&part->channel_mgr_wq);
978
979         sprintf(part->IPI_owner, "xpc%02d", partid);
980         ret = request_irq(SGI_XPC_NOTIFY, xpc_notify_IRQ_handler, IRQF_SHARED,
981                           part->IPI_owner, (void *)(u64)partid);
982         if (ret != 0) {
983                 dev_err(xpc_chan, "can't register NOTIFY IRQ handler, "
984                         "errno=%d\n", -ret);
985                 retval = xpLackOfResources;
986                 goto out_5;
987         }
988
989         /* Setup a timer to check for dropped IPIs */
990         timer = &part->dropped_IPI_timer;
991         init_timer(timer);
992         timer->function = (void (*)(unsigned long))xpc_dropped_IPI_check;
993         timer->data = (unsigned long)part;
994         timer->expires = jiffies + XPC_P_DROPPED_IPI_WAIT_INTERVAL;
995         add_timer(timer);
996
997         part->nchannels = XPC_MAX_NCHANNELS;
998
999         atomic_set(&part->nchannels_active, 0);
1000         atomic_set(&part->nchannels_engaged, 0);
1001
1002         for (ch_number = 0; ch_number < part->nchannels; ch_number++) {
1003                 ch = &part->channels[ch_number];
1004
1005                 ch->partid = partid;
1006                 ch->number = ch_number;
1007                 ch->flags = XPC_C_DISCONNECTED;
1008
1009                 ch->local_GP = &part->local_GPs[ch_number];
1010                 ch->local_openclose_args =
1011                     &part->local_openclose_args[ch_number];
1012
1013                 atomic_set(&ch->kthreads_assigned, 0);
1014                 atomic_set(&ch->kthreads_idle, 0);
1015                 atomic_set(&ch->kthreads_active, 0);
1016
1017                 atomic_set(&ch->references, 0);
1018                 atomic_set(&ch->n_to_notify, 0);
1019
1020                 spin_lock_init(&ch->lock);
1021                 mutex_init(&ch->msg_to_pull_mutex);
1022                 init_completion(&ch->wdisconnect_wait);
1023
1024                 atomic_set(&ch->n_on_msg_allocate_wq, 0);
1025                 init_waitqueue_head(&ch->msg_allocate_wq);
1026                 init_waitqueue_head(&ch->idle_wq);
1027         }
1028
1029         /*
1030          * With the setting of the partition setup_state to XPC_P_SETUP, we're
1031          * declaring that this partition is ready to go.
1032          */
1033         part->setup_state = XPC_P_SETUP;
1034
1035         /*
1036          * Setup the per partition specific variables required by the
1037          * remote partition to establish channel connections with us.
1038          *
1039          * The setting of the magic # indicates that these per partition
1040          * specific variables are ready to be used.
1041          */
1042         xpc_vars_part[partid].GPs_pa = __pa(part->local_GPs);
1043         xpc_vars_part[partid].openclose_args_pa =
1044             __pa(part->local_openclose_args);
1045         xpc_vars_part[partid].IPI_amo_pa = __pa(part->local_IPI_amo_va);
1046         cpuid = raw_smp_processor_id(); /* any CPU in this partition will do */
1047         xpc_vars_part[partid].IPI_nasid = cpuid_to_nasid(cpuid);
1048         xpc_vars_part[partid].IPI_phys_cpuid = cpu_physical_id(cpuid);
1049         xpc_vars_part[partid].nchannels = part->nchannels;
1050         xpc_vars_part[partid].magic = XPC_VP_MAGIC1;
1051
1052         return xpSuccess;
1053
1054         /* setup of infrastructure failed */
1055 out_5:
1056         kfree(part->remote_openclose_args_base);
1057         part->remote_openclose_args = NULL;
1058 out_4:
1059         kfree(part->local_openclose_args_base);
1060         part->local_openclose_args = NULL;
1061 out_3:
1062         kfree(part->remote_GPs_base);
1063         part->remote_GPs = NULL;
1064 out_2:
1065         kfree(part->local_GPs_base);
1066         part->local_GPs = NULL;
1067 out_1:
1068         kfree(part->channels);
1069         part->channels = NULL;
1070         return retval;
1071 }
1072
1073 /*
1074  * Teardown the infrastructure necessary to support XPartition Communication
1075  * between the specified remote partition and the local one.
1076  */
1077 static void
1078 xpc_teardown_infrastructure_sn2(struct xpc_partition *part)
1079 {
1080         short partid = XPC_PARTID(part);
1081
1082         /*
1083          * We start off by making this partition inaccessible to local
1084          * processes by marking it as no longer setup. Then we make it
1085          * inaccessible to remote processes by clearing the XPC per partition
1086          * specific variable's magic # (which indicates that these variables
1087          * are no longer valid) and by ignoring all XPC notify IPIs sent to
1088          * this partition.
1089          */
1090
1091         DBUG_ON(atomic_read(&part->nchannels_engaged) != 0);
1092         DBUG_ON(atomic_read(&part->nchannels_active) != 0);
1093         DBUG_ON(part->setup_state != XPC_P_SETUP);
1094         part->setup_state = XPC_P_WTEARDOWN;
1095
1096         xpc_vars_part[partid].magic = 0;
1097
1098         free_irq(SGI_XPC_NOTIFY, (void *)(u64)partid);
1099
1100         /*
1101          * Before proceeding with the teardown we have to wait until all
1102          * existing references cease.
1103          */
1104         wait_event(part->teardown_wq, (atomic_read(&part->references) == 0));
1105
1106         /* now we can begin tearing down the infrastructure */
1107
1108         part->setup_state = XPC_P_TORNDOWN;
1109
1110         /* in case we've still got outstanding timers registered... */
1111         del_timer_sync(&part->dropped_IPI_timer);
1112
1113         kfree(part->remote_openclose_args_base);
1114         part->remote_openclose_args = NULL;
1115         kfree(part->local_openclose_args_base);
1116         part->local_openclose_args = NULL;
1117         kfree(part->remote_GPs_base);
1118         part->remote_GPs = NULL;
1119         kfree(part->local_GPs_base);
1120         part->local_GPs = NULL;
1121         kfree(part->channels);
1122         part->channels = NULL;
1123         part->local_IPI_amo_va = NULL;
1124 }
1125
1126 /*
1127  * Create a wrapper that hides the underlying mechanism for pulling a cacheline
1128  * (or multiple cachelines) from a remote partition.
1129  *
1130  * src must be a cacheline aligned physical address on the remote partition.
1131  * dst must be a cacheline aligned virtual address on this partition.
1132  * cnt must be cacheline sized
1133  */
1134 /* >>> Replace this function by call to xp_remote_memcpy() or bte_copy()? */
1135 static enum xp_retval
1136 xpc_pull_remote_cachelines_sn2(struct xpc_partition *part, void *dst,
1137                                const void *src, size_t cnt)
1138 {
1139         enum xp_retval ret;
1140
1141         DBUG_ON((u64)src != L1_CACHE_ALIGN((u64)src));
1142         DBUG_ON((u64)dst != L1_CACHE_ALIGN((u64)dst));
1143         DBUG_ON(cnt != L1_CACHE_ALIGN(cnt));
1144
1145         if (part->act_state == XPC_P_DEACTIVATING)
1146                 return part->reason;
1147
1148         ret = xp_remote_memcpy(dst, src, cnt);
1149         if (ret != xpSuccess) {
1150                 dev_dbg(xpc_chan, "xp_remote_memcpy() from partition %d failed,"
1151                         " ret=%d\n", XPC_PARTID(part), ret);
1152         }
1153         return ret;
1154 }
1155
1156 /*
1157  * Pull the remote per partition specific variables from the specified
1158  * partition.
1159  */
1160 static enum xp_retval
1161 xpc_pull_remote_vars_part_sn2(struct xpc_partition *part)
1162 {
1163         u8 buffer[L1_CACHE_BYTES * 2];
1164         struct xpc_vars_part_sn2 *pulled_entry_cacheline =
1165             (struct xpc_vars_part_sn2 *)L1_CACHE_ALIGN((u64)buffer);
1166         struct xpc_vars_part_sn2 *pulled_entry;
1167         u64 remote_entry_cacheline_pa, remote_entry_pa;
1168         short partid = XPC_PARTID(part);
1169         enum xp_retval ret;
1170
1171         /* pull the cacheline that contains the variables we're interested in */
1172
1173         DBUG_ON(part->remote_vars_part_pa !=
1174                 L1_CACHE_ALIGN(part->remote_vars_part_pa));
1175         DBUG_ON(sizeof(struct xpc_vars_part_sn2) != L1_CACHE_BYTES / 2);
1176
1177         remote_entry_pa = part->remote_vars_part_pa +
1178             sn_partition_id * sizeof(struct xpc_vars_part_sn2);
1179
1180         remote_entry_cacheline_pa = (remote_entry_pa & ~(L1_CACHE_BYTES - 1));
1181
1182         pulled_entry = (struct xpc_vars_part_sn2 *)((u64)pulled_entry_cacheline
1183                                                     + (remote_entry_pa &
1184                                                     (L1_CACHE_BYTES - 1)));
1185
1186         ret = xpc_pull_remote_cachelines_sn2(part, pulled_entry_cacheline,
1187                                              (void *)remote_entry_cacheline_pa,
1188                                              L1_CACHE_BYTES);
1189         if (ret != xpSuccess) {
1190                 dev_dbg(xpc_chan, "failed to pull XPC vars_part from "
1191                         "partition %d, ret=%d\n", partid, ret);
1192                 return ret;
1193         }
1194
1195         /* see if they've been set up yet */
1196
1197         if (pulled_entry->magic != XPC_VP_MAGIC1 &&
1198             pulled_entry->magic != XPC_VP_MAGIC2) {
1199
1200                 if (pulled_entry->magic != 0) {
1201                         dev_dbg(xpc_chan, "partition %d's XPC vars_part for "
1202                                 "partition %d has bad magic value (=0x%lx)\n",
1203                                 partid, sn_partition_id, pulled_entry->magic);
1204                         return xpBadMagic;
1205                 }
1206
1207                 /* they've not been initialized yet */
1208                 return xpRetry;
1209         }
1210
1211         if (xpc_vars_part[partid].magic == XPC_VP_MAGIC1) {
1212
1213                 /* validate the variables */
1214
1215                 if (pulled_entry->GPs_pa == 0 ||
1216                     pulled_entry->openclose_args_pa == 0 ||
1217                     pulled_entry->IPI_amo_pa == 0) {
1218
1219                         dev_err(xpc_chan, "partition %d's XPC vars_part for "
1220                                 "partition %d are not valid\n", partid,
1221                                 sn_partition_id);
1222                         return xpInvalidAddress;
1223                 }
1224
1225                 /* the variables we imported look to be valid */
1226
1227                 part->remote_GPs_pa = pulled_entry->GPs_pa;
1228                 part->remote_openclose_args_pa =
1229                     pulled_entry->openclose_args_pa;
1230                 part->remote_IPI_amo_va =
1231                     (AMO_t *)__va(pulled_entry->IPI_amo_pa);
1232                 part->remote_IPI_nasid = pulled_entry->IPI_nasid;
1233                 part->remote_IPI_phys_cpuid = pulled_entry->IPI_phys_cpuid;
1234
1235                 if (part->nchannels > pulled_entry->nchannels)
1236                         part->nchannels = pulled_entry->nchannels;
1237
1238                 /* let the other side know that we've pulled their variables */
1239
1240                 xpc_vars_part[partid].magic = XPC_VP_MAGIC2;
1241         }
1242
1243         if (pulled_entry->magic == XPC_VP_MAGIC1)
1244                 return xpRetry;
1245
1246         return xpSuccess;
1247 }
1248
1249 /*
1250  * Establish first contact with the remote partititon. This involves pulling
1251  * the XPC per partition variables from the remote partition and waiting for
1252  * the remote partition to pull ours.
1253  */
1254 static enum xp_retval
1255 xpc_make_first_contact_sn2(struct xpc_partition *part)
1256 {
1257         enum xp_retval ret;
1258
1259         /*
1260          * Register the remote partition's AMOs with SAL so it can handle
1261          * and cleanup errors within that address range should the remote
1262          * partition go down. We don't unregister this range because it is
1263          * difficult to tell when outstanding writes to the remote partition
1264          * are finished and thus when it is safe to unregister. This should
1265          * not result in wasted space in the SAL xp_addr_region table because
1266          * we should get the same page for remote_amos_page_pa after module
1267          * reloads and system reboots.
1268          */
1269         if (sn_register_xp_addr_region(part->remote_amos_page_pa,
1270                                        PAGE_SIZE, 1) < 0) {
1271                 dev_warn(xpc_part, "xpc_activating(%d) failed to register "
1272                          "xp_addr region\n", XPC_PARTID(part));
1273
1274                 ret = xpPhysAddrRegFailed;
1275                 XPC_DEACTIVATE_PARTITION(part, ret);
1276                 return ret;
1277         }
1278
1279         xpc_IPI_send_activated(part);
1280
1281         while ((ret = xpc_pull_remote_vars_part_sn2(part)) != xpSuccess) {
1282                 if (ret != xpRetry) {
1283                         XPC_DEACTIVATE_PARTITION(part, ret);
1284                         return ret;
1285                 }
1286
1287                 dev_dbg(xpc_part, "waiting to make first contact with "
1288                         "partition %d\n", XPC_PARTID(part));
1289
1290                 /* wait a 1/4 of a second or so */
1291                 (void)msleep_interruptible(250);
1292
1293                 if (part->act_state == XPC_P_DEACTIVATING)
1294                         return part->reason;
1295         }
1296
1297         return xpSuccess;
1298 }
1299
1300 /*
1301  * Get the IPI flags and pull the openclose args and/or remote GPs as needed.
1302  */
1303 static u64
1304 xpc_get_IPI_flags_sn2(struct xpc_partition *part)
1305 {
1306         unsigned long irq_flags;
1307         u64 IPI_amo;
1308         enum xp_retval ret;
1309
1310         /*
1311          * See if there are any IPI flags to be handled.
1312          */
1313
1314         spin_lock_irqsave(&part->IPI_lock, irq_flags);
1315         IPI_amo = part->local_IPI_amo;
1316         if (IPI_amo != 0)
1317                 part->local_IPI_amo = 0;
1318
1319         spin_unlock_irqrestore(&part->IPI_lock, irq_flags);
1320
1321         if (XPC_ANY_OPENCLOSE_IPI_FLAGS_SET(IPI_amo)) {
1322                 ret = xpc_pull_remote_cachelines_sn2(part,
1323                                                     part->remote_openclose_args,
1324                                                      (void *)part->
1325                                                      remote_openclose_args_pa,
1326                                                      XPC_OPENCLOSE_ARGS_SIZE);
1327                 if (ret != xpSuccess) {
1328                         XPC_DEACTIVATE_PARTITION(part, ret);
1329
1330                         dev_dbg(xpc_chan, "failed to pull openclose args from "
1331                                 "partition %d, ret=%d\n", XPC_PARTID(part),
1332                                 ret);
1333
1334                         /* don't bother processing IPIs anymore */
1335                         IPI_amo = 0;
1336                 }
1337         }
1338
1339         if (XPC_ANY_MSG_IPI_FLAGS_SET(IPI_amo)) {
1340                 ret = xpc_pull_remote_cachelines_sn2(part, part->remote_GPs,
1341                                                     (void *)part->remote_GPs_pa,
1342                                                      XPC_GP_SIZE);
1343                 if (ret != xpSuccess) {
1344                         XPC_DEACTIVATE_PARTITION(part, ret);
1345
1346                         dev_dbg(xpc_chan, "failed to pull GPs from partition "
1347                                 "%d, ret=%d\n", XPC_PARTID(part), ret);
1348
1349                         /* don't bother processing IPIs anymore */
1350                         IPI_amo = 0;
1351                 }
1352         }
1353
1354         return IPI_amo;
1355 }
1356
1357 static struct xpc_msg *
1358 xpc_pull_remote_msg_sn2(struct xpc_channel *ch, s64 get)
1359 {
1360         struct xpc_partition *part = &xpc_partitions[ch->partid];
1361         struct xpc_msg *remote_msg, *msg;
1362         u32 msg_index, nmsgs;
1363         u64 msg_offset;
1364         enum xp_retval ret;
1365
1366         if (mutex_lock_interruptible(&ch->msg_to_pull_mutex) != 0) {
1367                 /* we were interrupted by a signal */
1368                 return NULL;
1369         }
1370
1371         while (get >= ch->next_msg_to_pull) {
1372
1373                 /* pull as many messages as are ready and able to be pulled */
1374
1375                 msg_index = ch->next_msg_to_pull % ch->remote_nentries;
1376
1377                 DBUG_ON(ch->next_msg_to_pull >= ch->w_remote_GP.put);
1378                 nmsgs = ch->w_remote_GP.put - ch->next_msg_to_pull;
1379                 if (msg_index + nmsgs > ch->remote_nentries) {
1380                         /* ignore the ones that wrap the msg queue for now */
1381                         nmsgs = ch->remote_nentries - msg_index;
1382                 }
1383
1384                 msg_offset = msg_index * ch->msg_size;
1385                 msg = (struct xpc_msg *)((u64)ch->remote_msgqueue + msg_offset);
1386                 remote_msg = (struct xpc_msg *)(ch->remote_msgqueue_pa +
1387                                                 msg_offset);
1388
1389                 ret = xpc_pull_remote_cachelines_sn2(part, msg, remote_msg,
1390                                                      nmsgs * ch->msg_size);
1391                 if (ret != xpSuccess) {
1392
1393                         dev_dbg(xpc_chan, "failed to pull %d msgs starting with"
1394                                 " msg %ld from partition %d, channel=%d, "
1395                                 "ret=%d\n", nmsgs, ch->next_msg_to_pull,
1396                                 ch->partid, ch->number, ret);
1397
1398                         XPC_DEACTIVATE_PARTITION(part, ret);
1399
1400                         mutex_unlock(&ch->msg_to_pull_mutex);
1401                         return NULL;
1402                 }
1403
1404                 ch->next_msg_to_pull += nmsgs;
1405         }
1406
1407         mutex_unlock(&ch->msg_to_pull_mutex);
1408
1409         /* return the message we were looking for */
1410         msg_offset = (get % ch->remote_nentries) * ch->msg_size;
1411         msg = (struct xpc_msg *)((u64)ch->remote_msgqueue + msg_offset);
1412
1413         return msg;
1414 }
1415
1416 /*
1417  * Get a message to be delivered.
1418  */
1419 static struct xpc_msg *
1420 xpc_get_deliverable_msg_sn2(struct xpc_channel *ch)
1421 {
1422         struct xpc_msg *msg = NULL;
1423         s64 get;
1424
1425         do {
1426                 if (ch->flags & XPC_C_DISCONNECTING)
1427                         break;
1428
1429                 get = ch->w_local_GP.get;
1430                 rmb();  /* guarantee that .get loads before .put */
1431                 if (get == ch->w_remote_GP.put)
1432                         break;
1433
1434                 /* There are messages waiting to be pulled and delivered.
1435                  * We need to try to secure one for ourselves. We'll do this
1436                  * by trying to increment w_local_GP.get and hope that no one
1437                  * else beats us to it. If they do, we'll we'll simply have
1438                  * to try again for the next one.
1439                  */
1440
1441                 if (cmpxchg(&ch->w_local_GP.get, get, get + 1) == get) {
1442                         /* we got the entry referenced by get */
1443
1444                         dev_dbg(xpc_chan, "w_local_GP.get changed to %ld, "
1445                                 "partid=%d, channel=%d\n", get + 1,
1446                                 ch->partid, ch->number);
1447
1448                         /* pull the message from the remote partition */
1449
1450                         msg = xpc_pull_remote_msg_sn2(ch, get);
1451
1452                         DBUG_ON(msg != NULL && msg->number != get);
1453                         DBUG_ON(msg != NULL && (msg->flags & XPC_M_DONE));
1454                         DBUG_ON(msg != NULL && !(msg->flags & XPC_M_READY));
1455
1456                         break;
1457                 }
1458
1459         } while (1);
1460
1461         return msg;
1462 }
1463
1464 /*
1465  * Now we actually send the messages that are ready to be sent by advancing
1466  * the local message queue's Put value and then send an IPI to the recipient
1467  * partition.
1468  */
1469 static void
1470 xpc_send_msgs_sn2(struct xpc_channel *ch, s64 initial_put)
1471 {
1472         struct xpc_msg *msg;
1473         s64 put = initial_put + 1;
1474         int send_IPI = 0;
1475
1476         while (1) {
1477
1478                 while (1) {
1479                         if (put == ch->w_local_GP.put)
1480                                 break;
1481
1482                         msg = (struct xpc_msg *)((u64)ch->local_msgqueue +
1483                                                  (put % ch->local_nentries) *
1484                                                  ch->msg_size);
1485
1486                         if (!(msg->flags & XPC_M_READY))
1487                                 break;
1488
1489                         put++;
1490                 }
1491
1492                 if (put == initial_put) {
1493                         /* nothing's changed */
1494                         break;
1495                 }
1496
1497                 if (cmpxchg_rel(&ch->local_GP->put, initial_put, put) !=
1498                     initial_put) {
1499                         /* someone else beat us to it */
1500                         DBUG_ON(ch->local_GP->put < initial_put);
1501                         break;
1502                 }
1503
1504                 /* we just set the new value of local_GP->put */
1505
1506                 dev_dbg(xpc_chan, "local_GP->put changed to %ld, partid=%d, "
1507                         "channel=%d\n", put, ch->partid, ch->number);
1508
1509                 send_IPI = 1;
1510
1511                 /*
1512                  * We need to ensure that the message referenced by
1513                  * local_GP->put is not XPC_M_READY or that local_GP->put
1514                  * equals w_local_GP.put, so we'll go have a look.
1515                  */
1516                 initial_put = put;
1517         }
1518
1519         if (send_IPI)
1520                 xpc_IPI_send_msgrequest_sn2(ch);
1521 }
1522
1523 /*
1524  * Allocate an entry for a message from the message queue associated with the
1525  * specified channel.
1526  */
1527 static enum xp_retval
1528 xpc_allocate_msg_sn2(struct xpc_channel *ch, u32 flags,
1529                      struct xpc_msg **address_of_msg)
1530 {
1531         struct xpc_msg *msg;
1532         enum xp_retval ret;
1533         s64 put;
1534
1535         /*
1536          * Get the next available message entry from the local message queue.
1537          * If none are available, we'll make sure that we grab the latest
1538          * GP values.
1539          */
1540         ret = xpTimeout;
1541
1542         while (1) {
1543
1544                 put = ch->w_local_GP.put;
1545                 rmb();  /* guarantee that .put loads before .get */
1546                 if (put - ch->w_remote_GP.get < ch->local_nentries) {
1547
1548                         /* There are available message entries. We need to try
1549                          * to secure one for ourselves. We'll do this by trying
1550                          * to increment w_local_GP.put as long as someone else
1551                          * doesn't beat us to it. If they do, we'll have to
1552                          * try again.
1553                          */
1554                         if (cmpxchg(&ch->w_local_GP.put, put, put + 1) == put) {
1555                                 /* we got the entry referenced by put */
1556                                 break;
1557                         }
1558                         continue;       /* try again */
1559                 }
1560
1561                 /*
1562                  * There aren't any available msg entries at this time.
1563                  *
1564                  * In waiting for a message entry to become available,
1565                  * we set a timeout in case the other side is not
1566                  * sending completion IPIs. This lets us fake an IPI
1567                  * that will cause the IPI handler to fetch the latest
1568                  * GP values as if an IPI was sent by the other side.
1569                  */
1570                 if (ret == xpTimeout)
1571                         xpc_IPI_send_local_msgrequest_sn2(ch);
1572
1573                 if (flags & XPC_NOWAIT)
1574                         return xpNoWait;
1575
1576                 ret = xpc_allocate_msg_wait(ch);
1577                 if (ret != xpInterrupted && ret != xpTimeout)
1578                         return ret;
1579         }
1580
1581         /* get the message's address and initialize it */
1582         msg = (struct xpc_msg *)((u64)ch->local_msgqueue +
1583                                  (put % ch->local_nentries) * ch->msg_size);
1584
1585         DBUG_ON(msg->flags != 0);
1586         msg->number = put;
1587
1588         dev_dbg(xpc_chan, "w_local_GP.put changed to %ld; msg=0x%p, "
1589                 "msg_number=%ld, partid=%d, channel=%d\n", put + 1,
1590                 (void *)msg, msg->number, ch->partid, ch->number);
1591
1592         *address_of_msg = msg;
1593         return xpSuccess;
1594 }
1595
1596 /*
1597  * Common code that does the actual sending of the message by advancing the
1598  * local message queue's Put value and sends an IPI to the partition the
1599  * message is being sent to.
1600  */
1601 static enum xp_retval
1602 xpc_send_msg_sn2(struct xpc_channel *ch, u32 flags, void *payload,
1603                  u16 payload_size, u8 notify_type, xpc_notify_func func,
1604                  void *key)
1605 {
1606         enum xp_retval ret = xpSuccess;
1607         struct xpc_msg *msg = msg;
1608         struct xpc_notify *notify = notify;
1609         s64 msg_number;
1610         s64 put;
1611
1612         DBUG_ON(notify_type == XPC_N_CALL && func == NULL);
1613
1614         if (XPC_MSG_SIZE(payload_size) > ch->msg_size)
1615                 return xpPayloadTooBig;
1616
1617         xpc_msgqueue_ref(ch);
1618
1619         if (ch->flags & XPC_C_DISCONNECTING) {
1620                 ret = ch->reason;
1621                 goto out_1;
1622         }
1623         if (!(ch->flags & XPC_C_CONNECTED)) {
1624                 ret = xpNotConnected;
1625                 goto out_1;
1626         }
1627
1628         ret = xpc_allocate_msg_sn2(ch, flags, &msg);
1629         if (ret != xpSuccess)
1630                 goto out_1;
1631
1632         msg_number = msg->number;
1633
1634         if (notify_type != 0) {
1635                 /*
1636                  * Tell the remote side to send an ACK interrupt when the
1637                  * message has been delivered.
1638                  */
1639                 msg->flags |= XPC_M_INTERRUPT;
1640
1641                 atomic_inc(&ch->n_to_notify);
1642
1643                 notify = &ch->notify_queue[msg_number % ch->local_nentries];
1644                 notify->func = func;
1645                 notify->key = key;
1646                 notify->type = notify_type;
1647
1648                 /* >>> is a mb() needed here? */
1649
1650                 if (ch->flags & XPC_C_DISCONNECTING) {
1651                         /*
1652                          * An error occurred between our last error check and
1653                          * this one. We will try to clear the type field from
1654                          * the notify entry. If we succeed then
1655                          * xpc_disconnect_channel() didn't already process
1656                          * the notify entry.
1657                          */
1658                         if (cmpxchg(&notify->type, notify_type, 0) ==
1659                             notify_type) {
1660                                 atomic_dec(&ch->n_to_notify);
1661                                 ret = ch->reason;
1662                         }
1663                         goto out_1;
1664                 }
1665         }
1666
1667         memcpy(&msg->payload, payload, payload_size);
1668
1669         msg->flags |= XPC_M_READY;
1670
1671         /*
1672          * The preceding store of msg->flags must occur before the following
1673          * load of ch->local_GP->put.
1674          */
1675         mb();
1676
1677         /* see if the message is next in line to be sent, if so send it */
1678
1679         put = ch->local_GP->put;
1680         if (put == msg_number)
1681                 xpc_send_msgs_sn2(ch, put);
1682
1683 out_1:
1684         xpc_msgqueue_deref(ch);
1685         return ret;
1686 }
1687
1688 /*
1689  * Now we actually acknowledge the messages that have been delivered and ack'd
1690  * by advancing the cached remote message queue's Get value and if requested
1691  * send an IPI to the message sender's partition.
1692  */
1693 static void
1694 xpc_acknowledge_msgs_sn2(struct xpc_channel *ch, s64 initial_get, u8 msg_flags)
1695 {
1696         struct xpc_msg *msg;
1697         s64 get = initial_get + 1;
1698         int send_IPI = 0;
1699
1700         while (1) {
1701
1702                 while (1) {
1703                         if (get == ch->w_local_GP.get)
1704                                 break;
1705
1706                         msg = (struct xpc_msg *)((u64)ch->remote_msgqueue +
1707                                                  (get % ch->remote_nentries) *
1708                                                  ch->msg_size);
1709
1710                         if (!(msg->flags & XPC_M_DONE))
1711                                 break;
1712
1713                         msg_flags |= msg->flags;
1714                         get++;
1715                 }
1716
1717                 if (get == initial_get) {
1718                         /* nothing's changed */
1719                         break;
1720                 }
1721
1722                 if (cmpxchg_rel(&ch->local_GP->get, initial_get, get) !=
1723                     initial_get) {
1724                         /* someone else beat us to it */
1725                         DBUG_ON(ch->local_GP->get <= initial_get);
1726                         break;
1727                 }
1728
1729                 /* we just set the new value of local_GP->get */
1730
1731                 dev_dbg(xpc_chan, "local_GP->get changed to %ld, partid=%d, "
1732                         "channel=%d\n", get, ch->partid, ch->number);
1733
1734                 send_IPI = (msg_flags & XPC_M_INTERRUPT);
1735
1736                 /*
1737                  * We need to ensure that the message referenced by
1738                  * local_GP->get is not XPC_M_DONE or that local_GP->get
1739                  * equals w_local_GP.get, so we'll go have a look.
1740                  */
1741                 initial_get = get;
1742         }
1743
1744         if (send_IPI)
1745                 xpc_IPI_send_msgrequest_sn2(ch);
1746 }
1747
1748 static void
1749 xpc_received_msg_sn2(struct xpc_channel *ch, struct xpc_msg *msg)
1750 {
1751         s64 get;
1752         s64 msg_number = msg->number;
1753
1754         dev_dbg(xpc_chan, "msg=0x%p, msg_number=%ld, partid=%d, channel=%d\n",
1755                 (void *)msg, msg_number, ch->partid, ch->number);
1756
1757         DBUG_ON((((u64)msg - (u64)ch->remote_msgqueue) / ch->msg_size) !=
1758                 msg_number % ch->remote_nentries);
1759         DBUG_ON(msg->flags & XPC_M_DONE);
1760
1761         msg->flags |= XPC_M_DONE;
1762
1763         /*
1764          * The preceding store of msg->flags must occur before the following
1765          * load of ch->local_GP->get.
1766          */
1767         mb();
1768
1769         /*
1770          * See if this message is next in line to be acknowledged as having
1771          * been delivered.
1772          */
1773         get = ch->local_GP->get;
1774         if (get == msg_number)
1775                 xpc_acknowledge_msgs_sn2(ch, get, msg->flags);
1776 }
1777
1778 void
1779 xpc_init_sn2(void)
1780 {
1781         xpc_rsvd_page_init = xpc_rsvd_page_init_sn2;
1782         xpc_increment_heartbeat = xpc_increment_heartbeat_sn2;
1783         xpc_offline_heartbeat = xpc_offline_heartbeat_sn2;
1784         xpc_online_heartbeat = xpc_online_heartbeat_sn2;
1785         xpc_heartbeat_init = xpc_heartbeat_init_sn2;
1786         xpc_heartbeat_exit = xpc_heartbeat_exit_sn2;
1787         xpc_check_remote_hb = xpc_check_remote_hb_sn2;
1788
1789         xpc_initiate_partition_activation =
1790             xpc_initiate_partition_activation_sn2;
1791         xpc_process_act_IRQ_rcvd = xpc_process_act_IRQ_rcvd_sn2;
1792         xpc_setup_infrastructure = xpc_setup_infrastructure_sn2;
1793         xpc_teardown_infrastructure = xpc_teardown_infrastructure_sn2;
1794         xpc_make_first_contact = xpc_make_first_contact_sn2;
1795         xpc_get_IPI_flags = xpc_get_IPI_flags_sn2;
1796         xpc_get_deliverable_msg = xpc_get_deliverable_msg_sn2;
1797
1798         xpc_mark_partition_engaged = xpc_mark_partition_engaged_sn2;
1799         xpc_mark_partition_disengaged = xpc_mark_partition_disengaged_sn2;
1800         xpc_request_partition_disengage = xpc_request_partition_disengage_sn2;
1801         xpc_cancel_partition_disengage_request =
1802             xpc_cancel_partition_disengage_request_sn2;
1803         xpc_partition_engaged = xpc_partition_engaged_sn2;
1804         xpc_partition_disengage_requested =
1805             xpc_partition_disengage_requested_sn2;
1806         xpc_clear_partition_engaged = xpc_clear_partition_engaged_sn2;
1807         xpc_clear_partition_disengage_request =
1808             xpc_clear_partition_disengage_request_sn2;
1809
1810         xpc_IPI_send_local_activate = xpc_IPI_send_local_activate_sn2;
1811         xpc_IPI_send_activated = xpc_IPI_send_activated_sn2;
1812         xpc_IPI_send_local_reactivate = xpc_IPI_send_local_reactivate_sn2;
1813         xpc_IPI_send_disengage = xpc_IPI_send_disengage_sn2;
1814
1815         xpc_IPI_send_closerequest = xpc_IPI_send_closerequest_sn2;
1816         xpc_IPI_send_closereply = xpc_IPI_send_closereply_sn2;
1817         xpc_IPI_send_openrequest = xpc_IPI_send_openrequest_sn2;
1818         xpc_IPI_send_openreply = xpc_IPI_send_openreply_sn2;
1819
1820         xpc_send_msg = xpc_send_msg_sn2;
1821         xpc_received_msg = xpc_received_msg_sn2;
1822 }
1823
1824 void
1825 xpc_exit_sn2(void)
1826 {
1827 }