sgi-xp: use standard bitops macros and functions
[safe/jmp/linux-2.6] / drivers / misc / sgi-xp / 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-2008 Silicon Graphics, Inc. All rights reserved.
7  */
8
9 /*
10  * External Cross Partition (XP) structures and defines.
11  */
12
13 #ifndef _DRIVERS_MISC_SGIXP_XP_H
14 #define _DRIVERS_MISC_SGIXP_XP_H
15
16 #include <linux/cache.h>
17 #include <linux/hardirq.h>
18 #include <linux/mutex.h>
19 #include <asm/sn/types.h>
20 #ifdef CONFIG_IA64
21 #include <asm/sn/arch.h>
22 #endif
23
24 #ifdef USE_DBUG_ON
25 #define DBUG_ON(condition)      BUG_ON(condition)
26 #else
27 #define DBUG_ON(condition)
28 #endif
29
30 #ifndef is_shub1
31 #define is_shub1()      0
32 #endif
33
34 #ifndef is_shub2
35 #define is_shub2()      0
36 #endif
37
38 #ifndef is_shub
39 #define is_shub()       (is_shub1() || is_shub2())
40 #endif
41
42 #ifndef is_uv
43 #define is_uv()         0
44 #endif
45
46 /*
47  * Define the maximum number of partitions the system can possibly support.
48  * It is based on the maximum number of hardware partitionable regions. The
49  * term 'region' in this context refers to the minimum number of nodes that
50  * can comprise an access protection grouping. The access protection is in
51  * regards to memory, IPI and IOI.
52  *
53  * The maximum number of hardware partitionable regions is equal to the
54  * maximum number of nodes in the entire system divided by the minimum number
55  * of nodes that comprise an access protection grouping.
56  */
57 #define XP_MAX_NPARTITIONS_SN2  64
58 #define XP_MAX_NPARTITIONS_UV   256
59
60 /*
61  * XPC establishes channel connections between the local partition and any
62  * other partition that is currently up. Over these channels, kernel-level
63  * `users' can communicate with their counterparts on the other partitions.
64  *
65  * If the need for additional channels arises, one can simply increase
66  * XPC_MAX_NCHANNELS accordingly. If the day should come where that number
67  * exceeds the absolute MAXIMUM number of channels possible (eight), then one
68  * will need to make changes to the XPC code to accommodate for this.
69  *
70  * The absolute maximum number of channels possible is limited to eight for
71  * performance reasons on sn2 hardware. The internal cross partition structures
72  * require sixteen bytes per channel, and eight allows all of this
73  * interface-shared info to fit in one 128-byte cacheline.
74  */
75 #define XPC_MEM_CHANNEL         0       /* memory channel number */
76 #define XPC_NET_CHANNEL         1       /* network channel number */
77
78 #define XPC_MAX_NCHANNELS       2       /* max #of channels allowed */
79
80 #if XPC_MAX_NCHANNELS > 8
81 #error  XPC_MAX_NCHANNELS exceeds absolute MAXIMUM possible.
82 #endif
83
84 /*
85  * The format of an XPC message is as follows:
86  *
87  *      +-------+--------------------------------+
88  *      | flags |////////////////////////////////|
89  *      +-------+--------------------------------+
90  *      |             message #                  |
91  *      +----------------------------------------+
92  *      |     payload (user-defined message)     |
93  *      |                                        |
94  *                      :
95  *      |                                        |
96  *      +----------------------------------------+
97  *
98  * The size of the payload is defined by the user via xpc_connect(). A user-
99  * defined message resides in the payload area.
100  *
101  * The size of a message entry (within a message queue) must be a cacheline
102  * sized multiple in order to facilitate the BTE transfer of messages from one
103  * message queue to another. A macro, XPC_MSG_SIZE(), is provided for the user
104  * that wants to fit as many msg entries as possible in a given memory size
105  * (e.g. a memory page).
106  */
107 struct xpc_msg {
108         u8 flags;               /* FOR XPC INTERNAL USE ONLY */
109         u8 reserved[7];         /* FOR XPC INTERNAL USE ONLY */
110         s64 number;             /* FOR XPC INTERNAL USE ONLY */
111
112         u64 payload;            /* user defined portion of message */
113 };
114
115 #define XPC_MSG_PAYLOAD_OFFSET  (u64) (&((struct xpc_msg *)0)->payload)
116 #define XPC_MSG_SIZE(_payload_size) \
117                 L1_CACHE_ALIGN(XPC_MSG_PAYLOAD_OFFSET + (_payload_size))
118
119 /*
120  * Define the return values and values passed to user's callout functions.
121  * (It is important to add new value codes at the end just preceding
122  * xpUnknownReason, which must have the highest numerical value.)
123  */
124 enum xp_retval {
125         xpSuccess = 0,
126
127         xpNotConnected,         /*  1: channel is not connected */
128         xpConnected,            /*  2: channel connected (opened) */
129         xpRETIRED1,             /*  3: (formerly xpDisconnected) */
130
131         xpMsgReceived,          /*  4: message received */
132         xpMsgDelivered,         /*  5: message delivered and acknowledged */
133
134         xpRETIRED2,             /*  6: (formerly xpTransferFailed) */
135
136         xpNoWait,               /*  7: operation would require wait */
137         xpRetry,                /*  8: retry operation */
138         xpTimeout,              /*  9: timeout in xpc_allocate_msg_wait() */
139         xpInterrupted,          /* 10: interrupted wait */
140
141         xpUnequalMsgSizes,      /* 11: message size disparity between sides */
142         xpInvalidAddress,       /* 12: invalid address */
143
144         xpNoMemory,             /* 13: no memory available for XPC structures */
145         xpLackOfResources,      /* 14: insufficient resources for operation */
146         xpUnregistered,         /* 15: channel is not registered */
147         xpAlreadyRegistered,    /* 16: channel is already registered */
148
149         xpPartitionDown,        /* 17: remote partition is down */
150         xpNotLoaded,            /* 18: XPC module is not loaded */
151         xpUnloading,            /* 19: this side is unloading XPC module */
152
153         xpBadMagic,             /* 20: XPC MAGIC string not found */
154
155         xpReactivating,         /* 21: remote partition was reactivated */
156
157         xpUnregistering,        /* 22: this side is unregistering channel */
158         xpOtherUnregistering,   /* 23: other side is unregistering channel */
159
160         xpCloneKThread,         /* 24: cloning kernel thread */
161         xpCloneKThreadFailed,   /* 25: cloning kernel thread failed */
162
163         xpNoHeartbeat,          /* 26: remote partition has no heartbeat */
164
165         xpPioReadError,         /* 27: PIO read error */
166         xpPhysAddrRegFailed,    /* 28: registration of phys addr range failed */
167
168         xpRETIRED3,             /* 29: (formerly xpBteDirectoryError) */
169         xpRETIRED4,             /* 30: (formerly xpBtePoisonError) */
170         xpRETIRED5,             /* 31: (formerly xpBteWriteError) */
171         xpRETIRED6,             /* 32: (formerly xpBteAccessError) */
172         xpRETIRED7,             /* 33: (formerly xpBtePWriteError) */
173         xpRETIRED8,             /* 34: (formerly xpBtePReadError) */
174         xpRETIRED9,             /* 35: (formerly xpBteTimeOutError) */
175         xpRETIRED10,            /* 36: (formerly xpBteXtalkError) */
176         xpRETIRED11,            /* 37: (formerly xpBteNotAvailable) */
177         xpRETIRED12,            /* 38: (formerly xpBteUnmappedError) */
178
179         xpBadVersion,           /* 39: bad version number */
180         xpVarsNotSet,           /* 40: the XPC variables are not set up */
181         xpNoRsvdPageAddr,       /* 41: unable to get rsvd page's phys addr */
182         xpInvalidPartid,        /* 42: invalid partition ID */
183         xpLocalPartid,          /* 43: local partition ID */
184
185         xpOtherGoingDown,       /* 44: other side going down, reason unknown */
186         xpSystemGoingDown,      /* 45: system is going down, reason unknown */
187         xpSystemHalt,           /* 46: system is being halted */
188         xpSystemReboot,         /* 47: system is being rebooted */
189         xpSystemPoweroff,       /* 48: system is being powered off */
190
191         xpDisconnecting,        /* 49: channel disconnecting (closing) */
192
193         xpOpenCloseError,       /* 50: channel open/close protocol error */
194
195         xpDisconnected,         /* 51: channel disconnected (closed) */
196
197         xpBteCopyError,         /* 52: bte_copy() returned error */
198         xpSalError,             /* 53: sn SAL error */
199         xpRsvdPageNotSet,       /* 54: the reserved page is not set up */
200         xpPayloadTooBig,        /* 55: payload too large for message slot */
201
202         xpUnsupported,          /* 56: unsupported functionality or resource */
203         xpUnknownReason         /* 57: unknown reason - must be last in enum */
204 };
205
206 /*
207  * Define the callout function type used by XPC to update the user on
208  * connection activity and state changes via the user function registered
209  * by xpc_connect().
210  *
211  * Arguments:
212  *
213  *      reason - reason code.
214  *      partid - partition ID associated with condition.
215  *      ch_number - channel # associated with condition.
216  *      data - pointer to optional data.
217  *      key - pointer to optional user-defined value provided as the "key"
218  *            argument to xpc_connect().
219  *
220  * A reason code of xpConnected indicates that a connection has been
221  * established to the specified partition on the specified channel. The data
222  * argument indicates the max number of entries allowed in the message queue.
223  *
224  * A reason code of xpMsgReceived indicates that a XPC message arrived from
225  * the specified partition on the specified channel. The data argument
226  * specifies the address of the message's payload. The user must call
227  * xpc_received() when finished with the payload.
228  *
229  * All other reason codes indicate failure. The data argmument is NULL.
230  * When a failure reason code is received, one can assume that the channel
231  * is not connected.
232  */
233 typedef void (*xpc_channel_func) (enum xp_retval reason, short partid,
234                                   int ch_number, void *data, void *key);
235
236 /*
237  * Define the callout function type used by XPC to notify the user of
238  * messages received and delivered via the user function registered by
239  * xpc_send_notify().
240  *
241  * Arguments:
242  *
243  *      reason - reason code.
244  *      partid - partition ID associated with condition.
245  *      ch_number - channel # associated with condition.
246  *      key - pointer to optional user-defined value provided as the "key"
247  *            argument to xpc_send_notify().
248  *
249  * A reason code of xpMsgDelivered indicates that the message was delivered
250  * to the intended recipient and that they have acknowledged its receipt by
251  * calling xpc_received().
252  *
253  * All other reason codes indicate failure.
254  */
255 typedef void (*xpc_notify_func) (enum xp_retval reason, short partid,
256                                  int ch_number, void *key);
257
258 /*
259  * The following is a registration entry. There is a global array of these,
260  * one per channel. It is used to record the connection registration made
261  * by the users of XPC. As long as a registration entry exists, for any
262  * partition that comes up, XPC will attempt to establish a connection on
263  * that channel. Notification that a connection has been made will occur via
264  * the xpc_channel_func function.
265  *
266  * The 'func' field points to the function to call when aynchronous
267  * notification is required for such events as: a connection established/lost,
268  * or an incoming message received, or an error condition encountered. A
269  * non-NULL 'func' field indicates that there is an active registration for
270  * the channel.
271  */
272 struct xpc_registration {
273         struct mutex mutex;
274         xpc_channel_func func;  /* function to call */
275         void *key;              /* pointer to user's key */
276         u16 nentries;           /* #of msg entries in local msg queue */
277         u16 msg_size;           /* message queue's message size */
278         u32 assigned_limit;     /* limit on #of assigned kthreads */
279         u32 idle_limit;         /* limit on #of idle kthreads */
280 } ____cacheline_aligned;
281
282 #define XPC_CHANNEL_REGISTERED(_c)      (xpc_registrations[_c].func != NULL)
283
284 /* the following are valid xpc_send() or xpc_send_notify() flags */
285 #define XPC_WAIT        0       /* wait flag */
286 #define XPC_NOWAIT      1       /* no wait flag */
287
288 struct xpc_interface {
289         void (*connect) (int);
290         void (*disconnect) (int);
291         enum xp_retval (*send) (short, int, u32, void *, u16);
292         enum xp_retval (*send_notify) (short, int, u32, void *, u16,
293                                         xpc_notify_func, void *);
294         void (*received) (short, int, void *);
295         enum xp_retval (*partid_to_nasids) (short, void *);
296 };
297
298 extern struct xpc_interface xpc_interface;
299
300 extern void xpc_set_interface(void (*)(int),
301                               void (*)(int),
302                               enum xp_retval (*)(short, int, u32, void *, u16),
303                               enum xp_retval (*)(short, int, u32, void *, u16,
304                                                  xpc_notify_func, void *),
305                               void (*)(short, int, void *),
306                               enum xp_retval (*)(short, void *));
307 extern void xpc_clear_interface(void);
308
309 extern enum xp_retval xpc_connect(int, xpc_channel_func, void *, u16,
310                                    u16, u32, u32);
311 extern void xpc_disconnect(int);
312
313 static inline enum xp_retval
314 xpc_send(short partid, int ch_number, u32 flags, void *payload,
315          u16 payload_size)
316 {
317         return xpc_interface.send(partid, ch_number, flags, payload,
318                                   payload_size);
319 }
320
321 static inline enum xp_retval
322 xpc_send_notify(short partid, int ch_number, u32 flags, void *payload,
323                 u16 payload_size, xpc_notify_func func, void *key)
324 {
325         return xpc_interface.send_notify(partid, ch_number, flags, payload,
326                                          payload_size, func, key);
327 }
328
329 static inline void
330 xpc_received(short partid, int ch_number, void *payload)
331 {
332         return xpc_interface.received(partid, ch_number, payload);
333 }
334
335 static inline enum xp_retval
336 xpc_partid_to_nasids(short partid, void *nasids)
337 {
338         return xpc_interface.partid_to_nasids(partid, nasids);
339 }
340
341 extern short xp_max_npartitions;
342
343 extern enum xp_retval (*xp_remote_memcpy) (void *, const void *, size_t);
344
345 extern u64 xp_nofault_PIOR_target;
346 extern int xp_nofault_PIOR(void *);
347 extern int xp_error_PIOR(void);
348
349 extern struct device *xp;
350 extern enum xp_retval xp_init_sn2(void);
351 extern enum xp_retval xp_init_uv(void);
352 extern void xp_exit_sn2(void);
353 extern void xp_exit_uv(void);
354
355 #endif /* _DRIVERS_MISC_SGIXP_XP_H */