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