145d9ca49SDean Nelson /* 245d9ca49SDean Nelson * This file is subject to the terms and conditions of the GNU General Public 345d9ca49SDean Nelson * License. See the file "COPYING" in the main directory of this archive 445d9ca49SDean Nelson * for more details. 545d9ca49SDean Nelson * 645d9ca49SDean Nelson * Copyright (c) 2004-2008 Silicon Graphics, Inc. All Rights Reserved. 745d9ca49SDean Nelson */ 845d9ca49SDean Nelson 945d9ca49SDean Nelson /* 1045d9ca49SDean Nelson * Cross Partition (XP) base. 1145d9ca49SDean Nelson * 1245d9ca49SDean Nelson * XP provides a base from which its users can interact 1345d9ca49SDean Nelson * with XPC, yet not be dependent on XPC. 1445d9ca49SDean Nelson * 1545d9ca49SDean Nelson */ 1645d9ca49SDean Nelson 1745d9ca49SDean Nelson #include <linux/module.h> 18bc63d387SDean Nelson #include <linux/device.h> 1945d9ca49SDean Nelson #include "xp.h" 2045d9ca49SDean Nelson 21bc63d387SDean Nelson /* define the XP debug device structures to be used with dev_dbg() et al */ 222c2b94f9SDean Nelson 23bc63d387SDean Nelson struct device_driver xp_dbg_name = { 24bc63d387SDean Nelson .name = "xp" 25bc63d387SDean Nelson }; 26bc63d387SDean Nelson 27bc63d387SDean Nelson struct device xp_dbg_subname = { 28bb0dc43eSKay Sievers .init_name = "", /* set to "" */ 29bc63d387SDean Nelson .driver = &xp_dbg_name 30bc63d387SDean Nelson }; 31bc63d387SDean Nelson 32bc63d387SDean Nelson struct device *xp = &xp_dbg_subname; 33bc63d387SDean Nelson 34bc63d387SDean Nelson /* max #of partitions possible */ 35bc63d387SDean Nelson short xp_max_npartitions; 36bc63d387SDean Nelson EXPORT_SYMBOL_GPL(xp_max_npartitions); 3745d9ca49SDean Nelson 38261f3b49SDean Nelson short xp_partition_id; 39261f3b49SDean Nelson EXPORT_SYMBOL_GPL(xp_partition_id); 40261f3b49SDean Nelson 41261f3b49SDean Nelson u8 xp_region_size; 42261f3b49SDean Nelson EXPORT_SYMBOL_GPL(xp_region_size); 43261f3b49SDean Nelson 44a812dcc3SDean Nelson unsigned long (*xp_pa) (void *addr); 45a812dcc3SDean Nelson EXPORT_SYMBOL_GPL(xp_pa); 46a812dcc3SDean Nelson 47a812dcc3SDean Nelson enum xp_retval (*xp_remote_memcpy) (unsigned long dst_gpa, 48a812dcc3SDean Nelson const unsigned long src_gpa, size_t len); 49908787dbSDean Nelson EXPORT_SYMBOL_GPL(xp_remote_memcpy); 50908787dbSDean Nelson 51261f3b49SDean Nelson int (*xp_cpu_to_nasid) (int cpuid); 52261f3b49SDean Nelson EXPORT_SYMBOL_GPL(xp_cpu_to_nasid); 53261f3b49SDean Nelson 546c1c325dSDean Nelson enum xp_retval (*xp_expand_memprotect) (unsigned long phys_addr, 556c1c325dSDean Nelson unsigned long size); 566c1c325dSDean Nelson EXPORT_SYMBOL_GPL(xp_expand_memprotect); 576c1c325dSDean Nelson enum xp_retval (*xp_restrict_memprotect) (unsigned long phys_addr, 586c1c325dSDean Nelson unsigned long size); 596c1c325dSDean Nelson EXPORT_SYMBOL_GPL(xp_restrict_memprotect); 606c1c325dSDean Nelson 6145d9ca49SDean Nelson /* 6245d9ca49SDean Nelson * xpc_registrations[] keeps track of xpc_connect()'s done by the kernel-level 6345d9ca49SDean Nelson * users of XPC. 6445d9ca49SDean Nelson */ 65bc63d387SDean Nelson struct xpc_registration xpc_registrations[XPC_MAX_NCHANNELS]; 662c2b94f9SDean Nelson EXPORT_SYMBOL_GPL(xpc_registrations); 6745d9ca49SDean Nelson 6845d9ca49SDean Nelson /* 6945d9ca49SDean Nelson * Initialize the XPC interface to indicate that XPC isn't loaded. 7045d9ca49SDean Nelson */ 7165c17b80SDean Nelson static enum xp_retval 724a3ad2ddSDean Nelson xpc_notloaded(void) 734a3ad2ddSDean Nelson { 7465c17b80SDean Nelson return xpNotLoaded; 754a3ad2ddSDean Nelson } 7645d9ca49SDean Nelson 7745d9ca49SDean Nelson struct xpc_interface xpc_interface = { 7845d9ca49SDean Nelson (void (*)(int))xpc_notloaded, 7945d9ca49SDean Nelson (void (*)(int))xpc_notloaded, 8097bf1aa1SDean Nelson (enum xp_retval(*)(short, int, u32, void *, u16))xpc_notloaded, 8197bf1aa1SDean Nelson (enum xp_retval(*)(short, int, u32, void *, u16, xpc_notify_func, 8297bf1aa1SDean Nelson void *))xpc_notloaded, 8364d032baSDean Nelson (void (*)(short, int, void *))xpc_notloaded, 8464d032baSDean Nelson (enum xp_retval(*)(short, void *))xpc_notloaded 8545d9ca49SDean Nelson }; 862c2b94f9SDean Nelson EXPORT_SYMBOL_GPL(xpc_interface); 8745d9ca49SDean Nelson 8845d9ca49SDean Nelson /* 8945d9ca49SDean Nelson * XPC calls this when it (the XPC module) has been loaded. 9045d9ca49SDean Nelson */ 9145d9ca49SDean Nelson void 9245d9ca49SDean Nelson xpc_set_interface(void (*connect) (int), 9345d9ca49SDean Nelson void (*disconnect) (int), 9497bf1aa1SDean Nelson enum xp_retval (*send) (short, int, u32, void *, u16), 9597bf1aa1SDean Nelson enum xp_retval (*send_notify) (short, int, u32, void *, u16, 9645d9ca49SDean Nelson xpc_notify_func, void *), 9764d032baSDean Nelson void (*received) (short, int, void *), 9864d032baSDean Nelson enum xp_retval (*partid_to_nasids) (short, void *)) 9945d9ca49SDean Nelson { 10045d9ca49SDean Nelson xpc_interface.connect = connect; 10145d9ca49SDean Nelson xpc_interface.disconnect = disconnect; 10245d9ca49SDean Nelson xpc_interface.send = send; 10345d9ca49SDean Nelson xpc_interface.send_notify = send_notify; 10445d9ca49SDean Nelson xpc_interface.received = received; 10545d9ca49SDean Nelson xpc_interface.partid_to_nasids = partid_to_nasids; 10645d9ca49SDean Nelson } 1072c2b94f9SDean Nelson EXPORT_SYMBOL_GPL(xpc_set_interface); 10845d9ca49SDean Nelson 10945d9ca49SDean Nelson /* 11045d9ca49SDean Nelson * XPC calls this when it (the XPC module) is being unloaded. 11145d9ca49SDean Nelson */ 11245d9ca49SDean Nelson void 11345d9ca49SDean Nelson xpc_clear_interface(void) 11445d9ca49SDean Nelson { 11545d9ca49SDean Nelson xpc_interface.connect = (void (*)(int))xpc_notloaded; 11645d9ca49SDean Nelson xpc_interface.disconnect = (void (*)(int))xpc_notloaded; 11797bf1aa1SDean Nelson xpc_interface.send = (enum xp_retval(*)(short, int, u32, void *, u16)) 11845d9ca49SDean Nelson xpc_notloaded; 11997bf1aa1SDean Nelson xpc_interface.send_notify = (enum xp_retval(*)(short, int, u32, void *, 12097bf1aa1SDean Nelson u16, xpc_notify_func, 1214a3ad2ddSDean Nelson void *))xpc_notloaded; 12264d032baSDean Nelson xpc_interface.received = (void (*)(short, int, void *)) 12345d9ca49SDean Nelson xpc_notloaded; 12464d032baSDean Nelson xpc_interface.partid_to_nasids = (enum xp_retval(*)(short, void *)) 12545d9ca49SDean Nelson xpc_notloaded; 12645d9ca49SDean Nelson } 1272c2b94f9SDean Nelson EXPORT_SYMBOL_GPL(xpc_clear_interface); 12845d9ca49SDean Nelson 12945d9ca49SDean Nelson /* 13045d9ca49SDean Nelson * Register for automatic establishment of a channel connection whenever 13145d9ca49SDean Nelson * a partition comes up. 13245d9ca49SDean Nelson * 13345d9ca49SDean Nelson * Arguments: 13445d9ca49SDean Nelson * 13545d9ca49SDean Nelson * ch_number - channel # to register for connection. 13645d9ca49SDean Nelson * func - function to call for asynchronous notification of channel 13745d9ca49SDean Nelson * state changes (i.e., connection, disconnection, error) and 13845d9ca49SDean Nelson * the arrival of incoming messages. 13945d9ca49SDean Nelson * key - pointer to optional user-defined value that gets passed back 14045d9ca49SDean Nelson * to the user on any callouts made to func. 14145d9ca49SDean Nelson * payload_size - size in bytes of the XPC message's payload area which 14245d9ca49SDean Nelson * contains a user-defined message. The user should make 14345d9ca49SDean Nelson * this large enough to hold their largest message. 14445d9ca49SDean Nelson * nentries - max #of XPC message entries a message queue can contain. 14545d9ca49SDean Nelson * The actual number, which is determined when a connection 14645d9ca49SDean Nelson * is established and may be less then requested, will be 14765c17b80SDean Nelson * passed to the user via the xpConnected callout. 14845d9ca49SDean Nelson * assigned_limit - max number of kthreads allowed to be processing 14945d9ca49SDean Nelson * messages (per connection) at any given instant. 15045d9ca49SDean Nelson * idle_limit - max number of kthreads allowed to be idle at any given 15145d9ca49SDean Nelson * instant. 15245d9ca49SDean Nelson */ 15365c17b80SDean Nelson enum xp_retval 15445d9ca49SDean Nelson xpc_connect(int ch_number, xpc_channel_func func, void *key, u16 payload_size, 15545d9ca49SDean Nelson u16 nentries, u32 assigned_limit, u32 idle_limit) 15645d9ca49SDean Nelson { 15745d9ca49SDean Nelson struct xpc_registration *registration; 15845d9ca49SDean Nelson 159bc63d387SDean Nelson DBUG_ON(ch_number < 0 || ch_number >= XPC_MAX_NCHANNELS); 16045d9ca49SDean Nelson DBUG_ON(payload_size == 0 || nentries == 0); 16145d9ca49SDean Nelson DBUG_ON(func == NULL); 16245d9ca49SDean Nelson DBUG_ON(assigned_limit == 0 || idle_limit > assigned_limit); 16345d9ca49SDean Nelson 164bd3e64c1SDean Nelson if (XPC_MSG_SIZE(payload_size) > XPC_MSG_MAX_SIZE) 165bd3e64c1SDean Nelson return xpPayloadTooBig; 166bd3e64c1SDean Nelson 16745d9ca49SDean Nelson registration = &xpc_registrations[ch_number]; 16845d9ca49SDean Nelson 1692c2b94f9SDean Nelson if (mutex_lock_interruptible(®istration->mutex) != 0) 17065c17b80SDean Nelson return xpInterrupted; 17145d9ca49SDean Nelson 17245d9ca49SDean Nelson /* if XPC_CHANNEL_REGISTERED(ch_number) */ 17345d9ca49SDean Nelson if (registration->func != NULL) { 17445d9ca49SDean Nelson mutex_unlock(®istration->mutex); 17565c17b80SDean Nelson return xpAlreadyRegistered; 17645d9ca49SDean Nelson } 17745d9ca49SDean Nelson 17845d9ca49SDean Nelson /* register the channel for connection */ 179bd3e64c1SDean Nelson registration->entry_size = XPC_MSG_SIZE(payload_size); 18045d9ca49SDean Nelson registration->nentries = nentries; 18145d9ca49SDean Nelson registration->assigned_limit = assigned_limit; 18245d9ca49SDean Nelson registration->idle_limit = idle_limit; 18345d9ca49SDean Nelson registration->key = key; 18445d9ca49SDean Nelson registration->func = func; 18545d9ca49SDean Nelson 18645d9ca49SDean Nelson mutex_unlock(®istration->mutex); 18745d9ca49SDean Nelson 18845d9ca49SDean Nelson xpc_interface.connect(ch_number); 18945d9ca49SDean Nelson 19065c17b80SDean Nelson return xpSuccess; 19145d9ca49SDean Nelson } 1922c2b94f9SDean Nelson EXPORT_SYMBOL_GPL(xpc_connect); 19345d9ca49SDean Nelson 19445d9ca49SDean Nelson /* 19545d9ca49SDean Nelson * Remove the registration for automatic connection of the specified channel 19645d9ca49SDean Nelson * when a partition comes up. 19745d9ca49SDean Nelson * 19845d9ca49SDean Nelson * Before returning this xpc_disconnect() will wait for all connections on the 19945d9ca49SDean Nelson * specified channel have been closed/torndown. So the caller can be assured 20045d9ca49SDean Nelson * that they will not be receiving any more callouts from XPC to their 20145d9ca49SDean Nelson * function registered via xpc_connect(). 20245d9ca49SDean Nelson * 20345d9ca49SDean Nelson * Arguments: 20445d9ca49SDean Nelson * 20545d9ca49SDean Nelson * ch_number - channel # to unregister. 20645d9ca49SDean Nelson */ 20745d9ca49SDean Nelson void 20845d9ca49SDean Nelson xpc_disconnect(int ch_number) 20945d9ca49SDean Nelson { 21045d9ca49SDean Nelson struct xpc_registration *registration; 21145d9ca49SDean Nelson 212bc63d387SDean Nelson DBUG_ON(ch_number < 0 || ch_number >= XPC_MAX_NCHANNELS); 21345d9ca49SDean Nelson 21445d9ca49SDean Nelson registration = &xpc_registrations[ch_number]; 21545d9ca49SDean Nelson 21645d9ca49SDean Nelson /* 21745d9ca49SDean Nelson * We've decided not to make this a down_interruptible(), since we 21845d9ca49SDean Nelson * figured XPC's users will just turn around and call xpc_disconnect() 21945d9ca49SDean Nelson * again anyways, so we might as well wait, if need be. 22045d9ca49SDean Nelson */ 22145d9ca49SDean Nelson mutex_lock(®istration->mutex); 22245d9ca49SDean Nelson 22345d9ca49SDean Nelson /* if !XPC_CHANNEL_REGISTERED(ch_number) */ 22445d9ca49SDean Nelson if (registration->func == NULL) { 22545d9ca49SDean Nelson mutex_unlock(®istration->mutex); 22645d9ca49SDean Nelson return; 22745d9ca49SDean Nelson } 22845d9ca49SDean Nelson 22945d9ca49SDean Nelson /* remove the connection registration for the specified channel */ 23045d9ca49SDean Nelson registration->func = NULL; 23145d9ca49SDean Nelson registration->key = NULL; 23245d9ca49SDean Nelson registration->nentries = 0; 233bd3e64c1SDean Nelson registration->entry_size = 0; 23445d9ca49SDean Nelson registration->assigned_limit = 0; 23545d9ca49SDean Nelson registration->idle_limit = 0; 23645d9ca49SDean Nelson 23745d9ca49SDean Nelson xpc_interface.disconnect(ch_number); 23845d9ca49SDean Nelson 23945d9ca49SDean Nelson mutex_unlock(®istration->mutex); 24045d9ca49SDean Nelson 24145d9ca49SDean Nelson return; 24245d9ca49SDean Nelson } 2432c2b94f9SDean Nelson EXPORT_SYMBOL_GPL(xpc_disconnect); 24445d9ca49SDean Nelson 24545d9ca49SDean Nelson int __init 24645d9ca49SDean Nelson xp_init(void) 24745d9ca49SDean Nelson { 248bc63d387SDean Nelson enum xp_retval ret; 249bc63d387SDean Nelson int ch_number; 25045d9ca49SDean Nelson 251bc63d387SDean Nelson if (is_shub()) 252bc63d387SDean Nelson ret = xp_init_sn2(); 253bc63d387SDean Nelson else if (is_uv()) 254bc63d387SDean Nelson ret = xp_init_uv(); 255bc63d387SDean Nelson else 256bc63d387SDean Nelson ret = xpUnsupported; 257bc63d387SDean Nelson 258bc63d387SDean Nelson if (ret != xpSuccess) 25945d9ca49SDean Nelson return -ENODEV; 26045d9ca49SDean Nelson 26145d9ca49SDean Nelson /* initialize the connection registration mutex */ 262bc63d387SDean Nelson for (ch_number = 0; ch_number < XPC_MAX_NCHANNELS; ch_number++) 26345d9ca49SDean Nelson mutex_init(&xpc_registrations[ch_number].mutex); 26445d9ca49SDean Nelson 26545d9ca49SDean Nelson return 0; 26645d9ca49SDean Nelson } 26745d9ca49SDean Nelson 2684a3ad2ddSDean Nelson module_init(xp_init); 26945d9ca49SDean Nelson 27045d9ca49SDean Nelson void __exit 27145d9ca49SDean Nelson xp_exit(void) 27245d9ca49SDean Nelson { 273bc63d387SDean Nelson if (is_shub()) 274bc63d387SDean Nelson xp_exit_sn2(); 275bc63d387SDean Nelson else if (is_uv()) 276bc63d387SDean Nelson xp_exit_uv(); 27745d9ca49SDean Nelson } 27845d9ca49SDean Nelson 2794a3ad2ddSDean Nelson module_exit(xp_exit); 28045d9ca49SDean Nelson 28145d9ca49SDean Nelson MODULE_AUTHOR("Silicon Graphics, Inc."); 28245d9ca49SDean Nelson MODULE_DESCRIPTION("Cross Partition (XP) base"); 28345d9ca49SDean Nelson MODULE_LICENSE("GPL"); 284