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