Pull xpc-disengage into release branch
[pandora-kernel.git] / include / asm-ia64 / sn / xp.h
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) 2004-2005 Silicon Graphics, Inc. All rights reserved.
7  */
8
9
10 /*
11  * External Cross Partition (XP) structures and defines.
12  */
13
14
15 #ifndef _ASM_IA64_SN_XP_H
16 #define _ASM_IA64_SN_XP_H
17
18
19 #include <linux/cache.h>
20 #include <linux/hardirq.h>
21 #include <asm/sn/types.h>
22 #include <asm/sn/bte.h>
23
24
25 #ifdef USE_DBUG_ON
26 #define DBUG_ON(condition)      BUG_ON(condition)
27 #else
28 #define DBUG_ON(condition)
29 #endif
30
31
32 /*
33  * Define the maximum number of logically defined partitions the system
34  * can support. It is constrained by the maximum number of hardware
35  * partitionable regions. The term 'region' in this context refers to the
36  * minimum number of nodes that can comprise an access protection grouping.
37  * The access protection is in regards to memory, IPI and IOI.
38  *
39  * The maximum number of hardware partitionable regions is equal to the
40  * maximum number of nodes in the entire system divided by the minimum number
41  * of nodes that comprise an access protection grouping.
42  */
43 #define XP_MAX_PARTITIONS       64
44
45
46 /*
47  * Define the number of u64s required to represent all the C-brick nasids
48  * as a bitmap.  The cross-partition kernel modules deal only with
49  * C-brick nasids, thus the need for bitmaps which don't account for
50  * odd-numbered (non C-brick) nasids.
51  */
52 #define XP_MAX_PHYSNODE_ID      (MAX_NUMALINK_NODES / 2)
53 #define XP_NASID_MASK_BYTES     ((XP_MAX_PHYSNODE_ID + 7) / 8)
54 #define XP_NASID_MASK_WORDS     ((XP_MAX_PHYSNODE_ID + 63) / 64)
55
56
57 /*
58  * Wrapper for bte_copy() that should it return a failure status will retry
59  * the bte_copy() once in the hope that the failure was due to a temporary
60  * aberration (i.e., the link going down temporarily).
61  *
62  * See bte_copy for definition of the input parameters.
63  *
64  * Note: xp_bte_copy() should never be called while holding a spinlock.
65  */
66 static inline bte_result_t
67 xp_bte_copy(u64 src, u64 dest, u64 len, u64 mode, void *notification)
68 {
69         bte_result_t ret;
70
71
72         ret = bte_copy(src, dest, len, mode, notification);
73
74         if (ret != BTE_SUCCESS) {
75                 if (!in_interrupt()) {
76                         cond_resched();
77                 }
78                 ret = bte_copy(src, dest, len, mode, notification);
79         }
80
81         return ret;
82 }
83
84
85 /*
86  * XPC establishes channel connections between the local partition and any
87  * other partition that is currently up. Over these channels, kernel-level
88  * `users' can communicate with their counterparts on the other partitions.
89  *
90  * The maxinum number of channels is limited to eight. For performance reasons,
91  * the internal cross partition structures require sixteen bytes per channel,
92  * and eight allows all of this interface-shared info to fit in one cache line.
93  *
94  * XPC_NCHANNELS reflects the total number of channels currently defined.
95  * If the need for additional channels arises, one can simply increase
96  * XPC_NCHANNELS accordingly. If the day should come where that number
97  * exceeds the MAXIMUM number of channels allowed (eight), then one will need
98  * to make changes to the XPC code to allow for this.
99  */
100 #define XPC_MEM_CHANNEL         0       /* memory channel number */
101 #define XPC_NET_CHANNEL         1       /* network channel number */
102
103 #define XPC_NCHANNELS           2       /* #of defined channels */
104 #define XPC_MAX_NCHANNELS       8       /* max #of channels allowed */
105
106 #if XPC_NCHANNELS > XPC_MAX_NCHANNELS
107 #error  XPC_NCHANNELS exceeds MAXIMUM allowed.
108 #endif
109
110
111 /*
112  * The format of an XPC message is as follows:
113  *
114  *      +-------+--------------------------------+
115  *      | flags |////////////////////////////////|
116  *      +-------+--------------------------------+
117  *      |             message #                  |
118  *      +----------------------------------------+
119  *      |     payload (user-defined message)     |
120  *      |                                        |
121  *                      :
122  *      |                                        |
123  *      +----------------------------------------+
124  *
125  * The size of the payload is defined by the user via xpc_connect(). A user-
126  * defined message resides in the payload area.
127  *
128  * The user should have no dealings with the message header, but only the
129  * message's payload. When a message entry is allocated (via xpc_allocate())
130  * a pointer to the payload area is returned and not the actual beginning of
131  * the XPC message. The user then constructs a message in the payload area
132  * and passes that pointer as an argument on xpc_send() or xpc_send_notify().
133  *
134  * The size of a message entry (within a message queue) must be a cacheline
135  * sized multiple in order to facilitate the BTE transfer of messages from one
136  * message queue to another. A macro, XPC_MSG_SIZE(), is provided for the user
137  * that wants to fit as many msg entries as possible in a given memory size
138  * (e.g. a memory page).
139  */
140 struct xpc_msg {
141         u8 flags;               /* FOR XPC INTERNAL USE ONLY */
142         u8 reserved[7];         /* FOR XPC INTERNAL USE ONLY */
143         s64 number;             /* FOR XPC INTERNAL USE ONLY */
144
145         u64 payload;            /* user defined portion of message */
146 };
147
148
149 #define XPC_MSG_PAYLOAD_OFFSET  (u64) (&((struct xpc_msg *)0)->payload)
150 #define XPC_MSG_SIZE(_payload_size) \
151                 L1_CACHE_ALIGN(XPC_MSG_PAYLOAD_OFFSET + (_payload_size))
152
153
154 /*
155  * Define the return values and values passed to user's callout functions.
156  * (It is important to add new value codes at the end just preceding
157  * xpcUnknownReason, which must have the highest numerical value.)
158  */
159 enum xpc_retval {
160         xpcSuccess = 0,
161
162         xpcNotConnected,        /*  1: channel is not connected */
163         xpcConnected,           /*  2: channel connected (opened) */
164         xpcRETIRED1,            /*  3: (formerly xpcDisconnected) */
165
166         xpcMsgReceived,         /*  4: message received */
167         xpcMsgDelivered,        /*  5: message delivered and acknowledged */
168
169         xpcRETIRED2,            /*  6: (formerly xpcTransferFailed) */
170
171         xpcNoWait,              /*  7: operation would require wait */
172         xpcRetry,               /*  8: retry operation */
173         xpcTimeout,             /*  9: timeout in xpc_allocate_msg_wait() */
174         xpcInterrupted,         /* 10: interrupted wait */
175
176         xpcUnequalMsgSizes,     /* 11: message size disparity between sides */
177         xpcInvalidAddress,      /* 12: invalid address */
178
179         xpcNoMemory,            /* 13: no memory available for XPC structures */
180         xpcLackOfResources,     /* 14: insufficient resources for operation */
181         xpcUnregistered,        /* 15: channel is not registered */
182         xpcAlreadyRegistered,   /* 16: channel is already registered */
183
184         xpcPartitionDown,       /* 17: remote partition is down */
185         xpcNotLoaded,           /* 18: XPC module is not loaded */
186         xpcUnloading,           /* 19: this side is unloading XPC module */
187
188         xpcBadMagic,            /* 20: XPC MAGIC string not found */
189
190         xpcReactivating,        /* 21: remote partition was reactivated */
191
192         xpcUnregistering,       /* 22: this side is unregistering channel */
193         xpcOtherUnregistering,  /* 23: other side is unregistering channel */
194
195         xpcCloneKThread,        /* 24: cloning kernel thread */
196         xpcCloneKThreadFailed,  /* 25: cloning kernel thread failed */
197
198         xpcNoHeartbeat,         /* 26: remote partition has no heartbeat */
199
200         xpcPioReadError,        /* 27: PIO read error */
201         xpcPhysAddrRegFailed,   /* 28: registration of phys addr range failed */
202
203         xpcBteDirectoryError,   /* 29: maps to BTEFAIL_DIR */
204         xpcBtePoisonError,      /* 30: maps to BTEFAIL_POISON */
205         xpcBteWriteError,       /* 31: maps to BTEFAIL_WERR */
206         xpcBteAccessError,      /* 32: maps to BTEFAIL_ACCESS */
207         xpcBtePWriteError,      /* 33: maps to BTEFAIL_PWERR */
208         xpcBtePReadError,       /* 34: maps to BTEFAIL_PRERR */
209         xpcBteTimeOutError,     /* 35: maps to BTEFAIL_TOUT */
210         xpcBteXtalkError,       /* 36: maps to BTEFAIL_XTERR */
211         xpcBteNotAvailable,     /* 37: maps to BTEFAIL_NOTAVAIL */
212         xpcBteUnmappedError,    /* 38: unmapped BTEFAIL_ error */
213
214         xpcBadVersion,          /* 39: bad version number */
215         xpcVarsNotSet,          /* 40: the XPC variables are not set up */
216         xpcNoRsvdPageAddr,      /* 41: unable to get rsvd page's phys addr */
217         xpcInvalidPartid,       /* 42: invalid partition ID */
218         xpcLocalPartid,         /* 43: local partition ID */
219
220         xpcOtherGoingDown,      /* 44: other side going down, reason unknown */
221         xpcSystemGoingDown,     /* 45: system is going down, reason unknown */
222         xpcSystemHalt,          /* 46: system is being halted */
223         xpcSystemReboot,        /* 47: system is being rebooted */
224         xpcSystemPoweroff,      /* 48: system is being powered off */
225
226         xpcDisconnecting,       /* 49: channel disconnecting (closing) */
227
228         xpcOpenCloseError,      /* 50: channel open/close protocol error */
229
230         xpcUnknownReason        /* 51: unknown reason -- must be last in list */
231 };
232
233
234 /*
235  * Define the callout function types used by XPC to update the user on
236  * connection activity and state changes (via the user function registered by
237  * xpc_connect()) and to notify them of messages received and delivered (via
238  * the user function registered by xpc_send_notify()).
239  *
240  * The two function types are xpc_channel_func and xpc_notify_func and
241  * both share the following arguments, with the exception of "data", which
242  * only xpc_channel_func has.
243  *
244  * Arguments:
245  *
246  *      reason - reason code. (See following table.)
247  *      partid - partition ID associated with condition.
248  *      ch_number - channel # associated with condition.
249  *      data - pointer to optional data. (See following table.)
250  *      key - pointer to optional user-defined value provided as the "key"
251  *            argument to xpc_connect() or xpc_send_notify().
252  *
253  * In the following table the "Optional Data" column applies to callouts made
254  * to functions registered by xpc_connect(). A "NA" in that column indicates
255  * that this reason code can be passed to functions registered by
256  * xpc_send_notify() (i.e. they don't have data arguments).
257  *
258  * Also, the first three reason codes in the following table indicate
259  * success, whereas the others indicate failure. When a failure reason code
260  * is received, one can assume that the channel is not connected.
261  *
262  *
263  * Reason Code          | Cause                          | Optional Data
264  * =====================+================================+=====================
265  * xpcConnected         | connection has been established| max #of entries
266  *                      | to the specified partition on  | allowed in message
267  *                      | the specified channel          | queue
268  * ---------------------+--------------------------------+---------------------
269  * xpcMsgReceived       | an XPC message arrived from    | address of payload
270  *                      | the specified partition on the |
271  *                      | specified channel              | [the user must call
272  *                      |                                | xpc_received() when
273  *                      |                                | finished with the
274  *                      |                                | payload]
275  * ---------------------+--------------------------------+---------------------
276  * xpcMsgDelivered      | notification that the message  | NA
277  *                      | was delivered to the intended  |
278  *                      | recipient and that they have   |
279  *                      | acknowledged its receipt by    |
280  *                      | calling xpc_received()         |
281  * =====================+================================+=====================
282  * xpcUnequalMsgSizes   | can't connect to the specified | NULL
283  *                      | partition on the specified     |
284  *                      | channel because of mismatched  |
285  *                      | message sizes                  |
286  * ---------------------+--------------------------------+---------------------
287  * xpcNoMemory          | insufficient memory avaiable   | NULL
288  *                      | to allocate message queue      |
289  * ---------------------+--------------------------------+---------------------
290  * xpcLackOfResources   | lack of resources to create    | NULL
291  *                      | the necessary kthreads to      |
292  *                      | support the channel            |
293  * ---------------------+--------------------------------+---------------------
294  * xpcUnregistering     | this side's user has           | NULL or NA
295  *                      | unregistered by calling        |
296  *                      | xpc_disconnect()               |
297  * ---------------------+--------------------------------+---------------------
298  * xpcOtherUnregistering| the other side's user has      | NULL or NA
299  *                      | unregistered by calling        |
300  *                      | xpc_disconnect()               |
301  * ---------------------+--------------------------------+---------------------
302  * xpcNoHeartbeat       | the other side's XPC is no     | NULL or NA
303  *                      | longer heartbeating            |
304  *                      |                                |
305  * ---------------------+--------------------------------+---------------------
306  * xpcUnloading         | this side's XPC module is      | NULL or NA
307  *                      | being unloaded                 |
308  *                      |                                |
309  * ---------------------+--------------------------------+---------------------
310  * xpcOtherUnloading    | the other side's XPC module is | NULL or NA
311  *                      | is being unloaded              |
312  *                      |                                |
313  * ---------------------+--------------------------------+---------------------
314  * xpcPioReadError      | xp_nofault_PIOR() returned an  | NULL or NA
315  *                      | error while sending an IPI     |
316  *                      |                                |
317  * ---------------------+--------------------------------+---------------------
318  * xpcInvalidAddress    | the address either received or | NULL or NA
319  *                      | sent by the specified partition|
320  *                      | is invalid                     |
321  * ---------------------+--------------------------------+---------------------
322  * xpcBteNotAvailable   | attempt to pull data from the  | NULL or NA
323  * xpcBtePoisonError    | specified partition over the   |
324  * xpcBteWriteError     | specified channel via a        |
325  * xpcBteAccessError    | bte_copy() failed              |
326  * xpcBteTimeOutError   |                                |
327  * xpcBteXtalkError     |                                |
328  * xpcBteDirectoryError |                                |
329  * xpcBteGenericError   |                                |
330  * xpcBteUnmappedError  |                                |
331  * ---------------------+--------------------------------+---------------------
332  * xpcUnknownReason     | the specified channel to the   | NULL or NA
333  *                      | specified partition was        |
334  *                      | unavailable for unknown reasons|
335  * =====================+================================+=====================
336  */
337
338 typedef void (*xpc_channel_func)(enum xpc_retval reason, partid_t partid,
339                 int ch_number, void *data, void *key);
340
341 typedef void (*xpc_notify_func)(enum xpc_retval reason, partid_t partid,
342                 int ch_number, void *key);
343
344
345 /*
346  * The following is a registration entry. There is a global array of these,
347  * one per channel. It is used to record the connection registration made
348  * by the users of XPC. As long as a registration entry exists, for any
349  * partition that comes up, XPC will attempt to establish a connection on
350  * that channel. Notification that a connection has been made will occur via
351  * the xpc_channel_func function.
352  *
353  * The 'func' field points to the function to call when aynchronous
354  * notification is required for such events as: a connection established/lost,
355  * or an incoming message received, or an error condition encountered. A
356  * non-NULL 'func' field indicates that there is an active registration for
357  * the channel.
358  */
359 struct xpc_registration {
360         struct semaphore sema;
361         xpc_channel_func func;          /* function to call */
362         void *key;                      /* pointer to user's key */
363         u16 nentries;                   /* #of msg entries in local msg queue */
364         u16 msg_size;                   /* message queue's message size */
365         u32 assigned_limit;             /* limit on #of assigned kthreads */
366         u32 idle_limit;                 /* limit on #of idle kthreads */
367 } ____cacheline_aligned;
368
369
370 #define XPC_CHANNEL_REGISTERED(_c)      (xpc_registrations[_c].func != NULL)
371
372
373 /* the following are valid xpc_allocate() flags */
374 #define XPC_WAIT        0               /* wait flag */
375 #define XPC_NOWAIT      1               /* no wait flag */
376
377
378 struct xpc_interface {
379         void (*connect)(int);
380         void (*disconnect)(int);
381         enum xpc_retval (*allocate)(partid_t, int, u32, void **);
382         enum xpc_retval (*send)(partid_t, int, void *);
383         enum xpc_retval (*send_notify)(partid_t, int, void *,
384                                                 xpc_notify_func, void *);
385         void (*received)(partid_t, int, void *);
386         enum xpc_retval (*partid_to_nasids)(partid_t, void *);
387 };
388
389
390 extern struct xpc_interface xpc_interface;
391
392 extern void xpc_set_interface(void (*)(int),
393                 void (*)(int),
394                 enum xpc_retval (*)(partid_t, int, u32, void **),
395                 enum xpc_retval (*)(partid_t, int, void *),
396                 enum xpc_retval (*)(partid_t, int, void *, xpc_notify_func,
397                                                                 void *),
398                 void (*)(partid_t, int, void *),
399                 enum xpc_retval (*)(partid_t, void *));
400 extern void xpc_clear_interface(void);
401
402
403 extern enum xpc_retval xpc_connect(int, xpc_channel_func, void *, u16,
404                                                 u16, u32, u32);
405 extern void xpc_disconnect(int);
406
407 static inline enum xpc_retval
408 xpc_allocate(partid_t partid, int ch_number, u32 flags, void **payload)
409 {
410         return xpc_interface.allocate(partid, ch_number, flags, payload);
411 }
412
413 static inline enum xpc_retval
414 xpc_send(partid_t partid, int ch_number, void *payload)
415 {
416         return xpc_interface.send(partid, ch_number, payload);
417 }
418
419 static inline enum xpc_retval
420 xpc_send_notify(partid_t partid, int ch_number, void *payload,
421                         xpc_notify_func func, void *key)
422 {
423         return xpc_interface.send_notify(partid, ch_number, payload, func, key);
424 }
425
426 static inline void
427 xpc_received(partid_t partid, int ch_number, void *payload)
428 {
429         return xpc_interface.received(partid, ch_number, payload);
430 }
431
432 static inline enum xpc_retval
433 xpc_partid_to_nasids(partid_t partid, void *nasids)
434 {
435         return xpc_interface.partid_to_nasids(partid, nasids);
436 }
437
438
439 extern u64 xp_nofault_PIOR_target;
440 extern int xp_nofault_PIOR(void *);
441 extern int xp_error_PIOR(void);
442
443
444 #endif /* _ASM_IA64_SN_XP_H */
445