1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0+ 2*4882a593Smuzhiyun /* 3*4882a593Smuzhiyun * Copyright (C) 2010 - 2013 UNISYS CORPORATION 4*4882a593Smuzhiyun * All rights reserved. 5*4882a593Smuzhiyun */ 6*4882a593Smuzhiyun 7*4882a593Smuzhiyun /* 8*4882a593Smuzhiyun * This header file is to be included by other kernel mode components that 9*4882a593Smuzhiyun * implement a particular kind of visor_device. Each of these other kernel 10*4882a593Smuzhiyun * mode components is called a visor device driver. Refer to visortemplate 11*4882a593Smuzhiyun * for a minimal sample visor device driver. 12*4882a593Smuzhiyun * 13*4882a593Smuzhiyun * There should be nothing in this file that is private to the visorbus 14*4882a593Smuzhiyun * bus implementation itself. 15*4882a593Smuzhiyun */ 16*4882a593Smuzhiyun 17*4882a593Smuzhiyun #ifndef __VISORBUS_H__ 18*4882a593Smuzhiyun #define __VISORBUS_H__ 19*4882a593Smuzhiyun 20*4882a593Smuzhiyun #include <linux/device.h> 21*4882a593Smuzhiyun 22*4882a593Smuzhiyun #define VISOR_CHANNEL_SIGNATURE ('L' << 24 | 'N' << 16 | 'C' << 8 | 'E') 23*4882a593Smuzhiyun 24*4882a593Smuzhiyun /* 25*4882a593Smuzhiyun * enum channel_serverstate 26*4882a593Smuzhiyun * @CHANNELSRV_UNINITIALIZED: Channel is in an undefined state. 27*4882a593Smuzhiyun * @CHANNELSRV_READY: Channel has been initialized by server. 28*4882a593Smuzhiyun */ 29*4882a593Smuzhiyun enum channel_serverstate { 30*4882a593Smuzhiyun CHANNELSRV_UNINITIALIZED = 0, 31*4882a593Smuzhiyun CHANNELSRV_READY = 1 32*4882a593Smuzhiyun }; 33*4882a593Smuzhiyun 34*4882a593Smuzhiyun /* 35*4882a593Smuzhiyun * enum channel_clientstate 36*4882a593Smuzhiyun * @CHANNELCLI_DETACHED: 37*4882a593Smuzhiyun * @CHANNELCLI_DISABLED: Client can see channel but is NOT allowed to use it 38*4882a593Smuzhiyun * unless given TBD* explicit request 39*4882a593Smuzhiyun * (should actually be < DETACHED). 40*4882a593Smuzhiyun * @CHANNELCLI_ATTACHING: Legacy EFI client request for EFI server to attach. 41*4882a593Smuzhiyun * @CHANNELCLI_ATTACHED: Idle, but client may want to use channel any time. 42*4882a593Smuzhiyun * @CHANNELCLI_BUSY: Client either wants to use or is using channel. 43*4882a593Smuzhiyun * @CHANNELCLI_OWNED: "No worries" state - client can access channel 44*4882a593Smuzhiyun * anytime. 45*4882a593Smuzhiyun */ 46*4882a593Smuzhiyun enum channel_clientstate { 47*4882a593Smuzhiyun CHANNELCLI_DETACHED = 0, 48*4882a593Smuzhiyun CHANNELCLI_DISABLED = 1, 49*4882a593Smuzhiyun CHANNELCLI_ATTACHING = 2, 50*4882a593Smuzhiyun CHANNELCLI_ATTACHED = 3, 51*4882a593Smuzhiyun CHANNELCLI_BUSY = 4, 52*4882a593Smuzhiyun CHANNELCLI_OWNED = 5 53*4882a593Smuzhiyun }; 54*4882a593Smuzhiyun 55*4882a593Smuzhiyun /* 56*4882a593Smuzhiyun * Values for VISOR_CHANNEL_PROTOCOL.Features: This define exists so that 57*4882a593Smuzhiyun * a guest can look at the FeatureFlags in the io channel, and configure the 58*4882a593Smuzhiyun * driver to use interrupts or not based on this setting. All feature bits for 59*4882a593Smuzhiyun * all channels should be defined here. The io channel feature bits are defined 60*4882a593Smuzhiyun * below. 61*4882a593Smuzhiyun */ 62*4882a593Smuzhiyun #define VISOR_DRIVER_ENABLES_INTS (0x1ULL << 1) 63*4882a593Smuzhiyun #define VISOR_CHANNEL_IS_POLLING (0x1ULL << 3) 64*4882a593Smuzhiyun #define VISOR_IOVM_OK_DRIVER_DISABLING_INTS (0x1ULL << 4) 65*4882a593Smuzhiyun #define VISOR_DRIVER_DISABLES_INTS (0x1ULL << 5) 66*4882a593Smuzhiyun #define VISOR_DRIVER_ENHANCED_RCVBUF_CHECKING (0x1ULL << 6) 67*4882a593Smuzhiyun 68*4882a593Smuzhiyun /* 69*4882a593Smuzhiyun * struct channel_header - Common Channel Header 70*4882a593Smuzhiyun * @signature: Signature. 71*4882a593Smuzhiyun * @legacy_state: DEPRECATED - being replaced by. 72*4882a593Smuzhiyun * @header_size: sizeof(struct channel_header). 73*4882a593Smuzhiyun * @size: Total size of this channel in bytes. 74*4882a593Smuzhiyun * @features: Flags to modify behavior. 75*4882a593Smuzhiyun * @chtype: Channel type: data, bus, control, etc.. 76*4882a593Smuzhiyun * @partition_handle: ID of guest partition. 77*4882a593Smuzhiyun * @handle: Device number of this channel in client. 78*4882a593Smuzhiyun * @ch_space_offset: Offset in bytes to channel specific area. 79*4882a593Smuzhiyun * @version_id: Struct channel_header Version ID. 80*4882a593Smuzhiyun * @partition_index: Index of guest partition. 81*4882a593Smuzhiyun * @zone_uuid: Guid of Channel's zone. 82*4882a593Smuzhiyun * @cli_str_offset: Offset from channel header to null-terminated 83*4882a593Smuzhiyun * ClientString (0 if ClientString not present). 84*4882a593Smuzhiyun * @cli_state_boot: CHANNEL_CLIENTSTATE of pre-boot EFI client of this 85*4882a593Smuzhiyun * channel. 86*4882a593Smuzhiyun * @cmd_state_cli: CHANNEL_COMMANDSTATE (overloaded in Windows drivers, see 87*4882a593Smuzhiyun * ServerStateUp, ServerStateDown, etc). 88*4882a593Smuzhiyun * @cli_state_os: CHANNEL_CLIENTSTATE of Guest OS client of this channel. 89*4882a593Smuzhiyun * @ch_characteristic: CHANNEL_CHARACTERISTIC_<xxx>. 90*4882a593Smuzhiyun * @cmd_state_srv: CHANNEL_COMMANDSTATE (overloaded in Windows drivers, see 91*4882a593Smuzhiyun * ServerStateUp, ServerStateDown, etc). 92*4882a593Smuzhiyun * @srv_state: CHANNEL_SERVERSTATE. 93*4882a593Smuzhiyun * @cli_error_boot: Bits to indicate err states for boot clients, so err 94*4882a593Smuzhiyun * messages can be throttled. 95*4882a593Smuzhiyun * @cli_error_os: Bits to indicate err states for OS clients, so err 96*4882a593Smuzhiyun * messages can be throttled. 97*4882a593Smuzhiyun * @filler: Pad out to 128 byte cacheline. 98*4882a593Smuzhiyun * @recover_channel: Please add all new single-byte values below here. 99*4882a593Smuzhiyun */ 100*4882a593Smuzhiyun struct channel_header { 101*4882a593Smuzhiyun u64 signature; 102*4882a593Smuzhiyun u32 legacy_state; 103*4882a593Smuzhiyun /* SrvState, CliStateBoot, and CliStateOS below */ 104*4882a593Smuzhiyun u32 header_size; 105*4882a593Smuzhiyun u64 size; 106*4882a593Smuzhiyun u64 features; 107*4882a593Smuzhiyun guid_t chtype; 108*4882a593Smuzhiyun u64 partition_handle; 109*4882a593Smuzhiyun u64 handle; 110*4882a593Smuzhiyun u64 ch_space_offset; 111*4882a593Smuzhiyun u32 version_id; 112*4882a593Smuzhiyun u32 partition_index; 113*4882a593Smuzhiyun guid_t zone_guid; 114*4882a593Smuzhiyun u32 cli_str_offset; 115*4882a593Smuzhiyun u32 cli_state_boot; 116*4882a593Smuzhiyun u32 cmd_state_cli; 117*4882a593Smuzhiyun u32 cli_state_os; 118*4882a593Smuzhiyun u32 ch_characteristic; 119*4882a593Smuzhiyun u32 cmd_state_srv; 120*4882a593Smuzhiyun u32 srv_state; 121*4882a593Smuzhiyun u8 cli_error_boot; 122*4882a593Smuzhiyun u8 cli_error_os; 123*4882a593Smuzhiyun u8 filler[1]; 124*4882a593Smuzhiyun u8 recover_channel; 125*4882a593Smuzhiyun } __packed; 126*4882a593Smuzhiyun 127*4882a593Smuzhiyun #define VISOR_CHANNEL_ENABLE_INTS (0x1ULL << 0) 128*4882a593Smuzhiyun 129*4882a593Smuzhiyun /* 130*4882a593Smuzhiyun * struct signal_queue_header - Subheader for the Signal Type variation of the 131*4882a593Smuzhiyun * Common Channel. 132*4882a593Smuzhiyun * @version: SIGNAL_QUEUE_HEADER Version ID. 133*4882a593Smuzhiyun * @chtype: Queue type: storage, network. 134*4882a593Smuzhiyun * @size: Total size of this queue in bytes. 135*4882a593Smuzhiyun * @sig_base_offset: Offset to signal queue area. 136*4882a593Smuzhiyun * @features: Flags to modify behavior. 137*4882a593Smuzhiyun * @num_sent: Total # of signals placed in this queue. 138*4882a593Smuzhiyun * @num_overflows: Total # of inserts failed due to full queue. 139*4882a593Smuzhiyun * @signal_size: Total size of a signal for this queue. 140*4882a593Smuzhiyun * @max_slots: Max # of slots in queue, 1 slot is always empty. 141*4882a593Smuzhiyun * @max_signals: Max # of signals in queue (MaxSignalSlots-1). 142*4882a593Smuzhiyun * @head: Queue head signal #. 143*4882a593Smuzhiyun * @num_received: Total # of signals removed from this queue. 144*4882a593Smuzhiyun * @tail: Queue tail signal. 145*4882a593Smuzhiyun * @reserved1: Reserved field. 146*4882a593Smuzhiyun * @reserved2: Reserved field. 147*4882a593Smuzhiyun * @client_queue: 148*4882a593Smuzhiyun * @num_irq_received: Total # of Interrupts received. This is incremented by the 149*4882a593Smuzhiyun * ISR in the guest windows driver. 150*4882a593Smuzhiyun * @num_empty: Number of times that visor_signal_remove is called and 151*4882a593Smuzhiyun * returned Empty Status. 152*4882a593Smuzhiyun * @errorflags: Error bits set during SignalReinit to denote trouble with 153*4882a593Smuzhiyun * client's fields. 154*4882a593Smuzhiyun * @filler: Pad out to 64 byte cacheline. 155*4882a593Smuzhiyun */ 156*4882a593Smuzhiyun struct signal_queue_header { 157*4882a593Smuzhiyun /* 1st cache line */ 158*4882a593Smuzhiyun u32 version; 159*4882a593Smuzhiyun u32 chtype; 160*4882a593Smuzhiyun u64 size; 161*4882a593Smuzhiyun u64 sig_base_offset; 162*4882a593Smuzhiyun u64 features; 163*4882a593Smuzhiyun u64 num_sent; 164*4882a593Smuzhiyun u64 num_overflows; 165*4882a593Smuzhiyun u32 signal_size; 166*4882a593Smuzhiyun u32 max_slots; 167*4882a593Smuzhiyun u32 max_signals; 168*4882a593Smuzhiyun u32 head; 169*4882a593Smuzhiyun /* 2nd cache line */ 170*4882a593Smuzhiyun u64 num_received; 171*4882a593Smuzhiyun u32 tail; 172*4882a593Smuzhiyun u32 reserved1; 173*4882a593Smuzhiyun u64 reserved2; 174*4882a593Smuzhiyun u64 client_queue; 175*4882a593Smuzhiyun u64 num_irq_received; 176*4882a593Smuzhiyun u64 num_empty; 177*4882a593Smuzhiyun u32 errorflags; 178*4882a593Smuzhiyun u8 filler[12]; 179*4882a593Smuzhiyun } __packed; 180*4882a593Smuzhiyun 181*4882a593Smuzhiyun /* VISORCHANNEL Guids */ 182*4882a593Smuzhiyun /* {414815ed-c58c-11da-95a9-00e08161165f} */ 183*4882a593Smuzhiyun #define VISOR_VHBA_CHANNEL_GUID \ 184*4882a593Smuzhiyun GUID_INIT(0x414815ed, 0xc58c, 0x11da, \ 185*4882a593Smuzhiyun 0x95, 0xa9, 0x0, 0xe0, 0x81, 0x61, 0x16, 0x5f) 186*4882a593Smuzhiyun #define VISOR_VHBA_CHANNEL_GUID_STR \ 187*4882a593Smuzhiyun "414815ed-c58c-11da-95a9-00e08161165f" 188*4882a593Smuzhiyun struct visorchipset_state { 189*4882a593Smuzhiyun u32 created:1; 190*4882a593Smuzhiyun u32 attached:1; 191*4882a593Smuzhiyun u32 configured:1; 192*4882a593Smuzhiyun u32 running:1; 193*4882a593Smuzhiyun /* Remaining bits in this 32-bit word are reserved. */ 194*4882a593Smuzhiyun }; 195*4882a593Smuzhiyun 196*4882a593Smuzhiyun /** 197*4882a593Smuzhiyun * struct visor_device - A device type for things "plugged" into the visorbus 198*4882a593Smuzhiyun * bus 199*4882a593Smuzhiyun * @visorchannel: Points to the channel that the device is 200*4882a593Smuzhiyun * associated with. 201*4882a593Smuzhiyun * @channel_type_guid: Identifies the channel type to the bus driver. 202*4882a593Smuzhiyun * @device: Device struct meant for use by the bus driver 203*4882a593Smuzhiyun * only. 204*4882a593Smuzhiyun * @list_all: Used by the bus driver to enumerate devices. 205*4882a593Smuzhiyun * @timer: Timer fired periodically to do interrupt-type 206*4882a593Smuzhiyun * activity. 207*4882a593Smuzhiyun * @being_removed: Indicates that the device is being removed from 208*4882a593Smuzhiyun * the bus. Private bus driver use only. 209*4882a593Smuzhiyun * @visordriver_callback_lock: Used by the bus driver to lock when adding and 210*4882a593Smuzhiyun * removing devices. 211*4882a593Smuzhiyun * @pausing: Indicates that a change towards a paused state. 212*4882a593Smuzhiyun * is in progress. Only modified by the bus driver. 213*4882a593Smuzhiyun * @resuming: Indicates that a change towards a running state 214*4882a593Smuzhiyun * is in progress. Only modified by the bus driver. 215*4882a593Smuzhiyun * @chipset_bus_no: Private field used by the bus driver. 216*4882a593Smuzhiyun * @chipset_dev_no: Private field used the bus driver. 217*4882a593Smuzhiyun * @state: Used to indicate the current state of the 218*4882a593Smuzhiyun * device. 219*4882a593Smuzhiyun * @inst: Unique GUID for this instance of the device. 220*4882a593Smuzhiyun * @name: Name of the device. 221*4882a593Smuzhiyun * @pending_msg_hdr: For private use by bus driver to respond to 222*4882a593Smuzhiyun * hypervisor requests. 223*4882a593Smuzhiyun * @vbus_hdr_info: A pointer to header info. Private use by bus 224*4882a593Smuzhiyun * driver. 225*4882a593Smuzhiyun * @partition_guid: Indicates client partion id. This should be the 226*4882a593Smuzhiyun * same across all visor_devices in the current 227*4882a593Smuzhiyun * guest. Private use by bus driver only. 228*4882a593Smuzhiyun */ 229*4882a593Smuzhiyun struct visor_device { 230*4882a593Smuzhiyun struct visorchannel *visorchannel; 231*4882a593Smuzhiyun guid_t channel_type_guid; 232*4882a593Smuzhiyun /* These fields are for private use by the bus driver only. */ 233*4882a593Smuzhiyun struct device device; 234*4882a593Smuzhiyun struct list_head list_all; 235*4882a593Smuzhiyun struct timer_list timer; 236*4882a593Smuzhiyun bool timer_active; 237*4882a593Smuzhiyun bool being_removed; 238*4882a593Smuzhiyun struct mutex visordriver_callback_lock; /* synchronize probe/remove */ 239*4882a593Smuzhiyun bool pausing; 240*4882a593Smuzhiyun bool resuming; 241*4882a593Smuzhiyun u32 chipset_bus_no; 242*4882a593Smuzhiyun u32 chipset_dev_no; 243*4882a593Smuzhiyun struct visorchipset_state state; 244*4882a593Smuzhiyun guid_t inst; 245*4882a593Smuzhiyun u8 *name; 246*4882a593Smuzhiyun struct controlvm_message_header *pending_msg_hdr; 247*4882a593Smuzhiyun void *vbus_hdr_info; 248*4882a593Smuzhiyun guid_t partition_guid; 249*4882a593Smuzhiyun struct dentry *debugfs_dir; 250*4882a593Smuzhiyun struct dentry *debugfs_bus_info; 251*4882a593Smuzhiyun }; 252*4882a593Smuzhiyun 253*4882a593Smuzhiyun #define to_visor_device(x) container_of(x, struct visor_device, device) 254*4882a593Smuzhiyun 255*4882a593Smuzhiyun typedef void (*visorbus_state_complete_func) (struct visor_device *dev, 256*4882a593Smuzhiyun int status); 257*4882a593Smuzhiyun 258*4882a593Smuzhiyun /* 259*4882a593Smuzhiyun * This struct describes a specific visor channel, by providing its GUID, name, 260*4882a593Smuzhiyun * and sizes. 261*4882a593Smuzhiyun */ 262*4882a593Smuzhiyun struct visor_channeltype_descriptor { 263*4882a593Smuzhiyun const guid_t guid; 264*4882a593Smuzhiyun const char *name; 265*4882a593Smuzhiyun u64 min_bytes; 266*4882a593Smuzhiyun u32 version; 267*4882a593Smuzhiyun }; 268*4882a593Smuzhiyun 269*4882a593Smuzhiyun /** 270*4882a593Smuzhiyun * struct visor_driver - Information provided by each visor driver when it 271*4882a593Smuzhiyun * registers with the visorbus driver 272*4882a593Smuzhiyun * @name: Name of the visor driver. 273*4882a593Smuzhiyun * @owner: The module owner. 274*4882a593Smuzhiyun * @channel_types: Types of channels handled by this driver, ending with 275*4882a593Smuzhiyun * a zero GUID. Our specialized BUS.match() method knows 276*4882a593Smuzhiyun * about this list, and uses it to determine whether this 277*4882a593Smuzhiyun * driver will in fact handle a new device that it has 278*4882a593Smuzhiyun * detected. 279*4882a593Smuzhiyun * @probe: Called when a new device comes online, by our probe() 280*4882a593Smuzhiyun * function specified by driver.probe() (triggered 281*4882a593Smuzhiyun * ultimately by some call to driver_register(), 282*4882a593Smuzhiyun * bus_add_driver(), or driver_attach()). 283*4882a593Smuzhiyun * @remove: Called when a new device is removed, by our remove() 284*4882a593Smuzhiyun * function specified by driver.remove() (triggered 285*4882a593Smuzhiyun * ultimately by some call to device_release_driver()). 286*4882a593Smuzhiyun * @channel_interrupt: Called periodically, whenever there is a possiblity 287*4882a593Smuzhiyun * that "something interesting" may have happened to the 288*4882a593Smuzhiyun * channel. 289*4882a593Smuzhiyun * @pause: Called to initiate a change of the device's state. If 290*4882a593Smuzhiyun * the return valu`e is < 0, there was an error and the 291*4882a593Smuzhiyun * state transition will NOT occur. If the return value 292*4882a593Smuzhiyun * is >= 0, then the state transition was INITIATED 293*4882a593Smuzhiyun * successfully, and complete_func() will be called (or 294*4882a593Smuzhiyun * was just called) with the final status when either the 295*4882a593Smuzhiyun * state transition fails or completes successfully. 296*4882a593Smuzhiyun * @resume: Behaves similar to pause. 297*4882a593Smuzhiyun * @driver: Private reference to the device driver. For use by bus 298*4882a593Smuzhiyun * driver only. 299*4882a593Smuzhiyun */ 300*4882a593Smuzhiyun struct visor_driver { 301*4882a593Smuzhiyun const char *name; 302*4882a593Smuzhiyun struct module *owner; 303*4882a593Smuzhiyun struct visor_channeltype_descriptor *channel_types; 304*4882a593Smuzhiyun int (*probe)(struct visor_device *dev); 305*4882a593Smuzhiyun void (*remove)(struct visor_device *dev); 306*4882a593Smuzhiyun void (*channel_interrupt)(struct visor_device *dev); 307*4882a593Smuzhiyun int (*pause)(struct visor_device *dev, 308*4882a593Smuzhiyun visorbus_state_complete_func complete_func); 309*4882a593Smuzhiyun int (*resume)(struct visor_device *dev, 310*4882a593Smuzhiyun visorbus_state_complete_func complete_func); 311*4882a593Smuzhiyun 312*4882a593Smuzhiyun /* These fields are for private use by the bus driver only. */ 313*4882a593Smuzhiyun struct device_driver driver; 314*4882a593Smuzhiyun }; 315*4882a593Smuzhiyun 316*4882a593Smuzhiyun #define to_visor_driver(x) (container_of(x, struct visor_driver, driver)) 317*4882a593Smuzhiyun 318*4882a593Smuzhiyun int visor_check_channel(struct channel_header *ch, struct device *dev, 319*4882a593Smuzhiyun const guid_t *expected_uuid, char *chname, 320*4882a593Smuzhiyun u64 expected_min_bytes, u32 expected_version, 321*4882a593Smuzhiyun u64 expected_signature); 322*4882a593Smuzhiyun 323*4882a593Smuzhiyun int visorbus_register_visor_driver(struct visor_driver *drv); 324*4882a593Smuzhiyun void visorbus_unregister_visor_driver(struct visor_driver *drv); 325*4882a593Smuzhiyun int visorbus_read_channel(struct visor_device *dev, 326*4882a593Smuzhiyun unsigned long offset, void *dest, 327*4882a593Smuzhiyun unsigned long nbytes); 328*4882a593Smuzhiyun int visorbus_write_channel(struct visor_device *dev, 329*4882a593Smuzhiyun unsigned long offset, void *src, 330*4882a593Smuzhiyun unsigned long nbytes); 331*4882a593Smuzhiyun int visorbus_enable_channel_interrupts(struct visor_device *dev); 332*4882a593Smuzhiyun void visorbus_disable_channel_interrupts(struct visor_device *dev); 333*4882a593Smuzhiyun 334*4882a593Smuzhiyun int visorchannel_signalremove(struct visorchannel *channel, u32 queue, 335*4882a593Smuzhiyun void *msg); 336*4882a593Smuzhiyun int visorchannel_signalinsert(struct visorchannel *channel, u32 queue, 337*4882a593Smuzhiyun void *msg); 338*4882a593Smuzhiyun bool visorchannel_signalempty(struct visorchannel *channel, u32 queue); 339*4882a593Smuzhiyun const guid_t *visorchannel_get_guid(struct visorchannel *channel); 340*4882a593Smuzhiyun 341*4882a593Smuzhiyun #define BUS_ROOT_DEVICE UINT_MAX 342*4882a593Smuzhiyun struct visor_device *visorbus_get_device_by_id(u32 bus_no, u32 dev_no, 343*4882a593Smuzhiyun struct visor_device *from); 344*4882a593Smuzhiyun #endif 345