// SPDX-License-Identifier: GPL-2.0-only /* * Copyright (c) 2009, Microsoft Corporation. * * Authors: * Haiyang Zhang <[email protected]> * Hank Janssen <[email protected]> */ #define pr_fmt(fmt) … #include <linux/kernel.h> #include <linux/sched.h> #include <linux/wait.h> #include <linux/mm.h> #include <linux/slab.h> #include <linux/module.h> #include <linux/hyperv.h> #include <linux/uio.h> #include <linux/interrupt.h> #include <linux/set_memory.h> #include <asm/page.h> #include <asm/mshyperv.h> #include "hyperv_vmbus.h" /* * hv_gpadl_size - Return the real size of a gpadl, the size that Hyper-V uses * * For BUFFER gpadl, Hyper-V uses the exact same size as the guest does. * * For RING gpadl, in each ring, the guest uses one PAGE_SIZE as the header * (because of the alignment requirement), however, the hypervisor only * uses the first HV_HYP_PAGE_SIZE as the header, therefore leaving a * (PAGE_SIZE - HV_HYP_PAGE_SIZE) gap. And since there are two rings in a * ringbuffer, the total size for a RING gpadl that Hyper-V uses is the * total size that the guest uses minus twice of the gap size. */ static inline u32 hv_gpadl_size(enum hv_gpadl_type type, u32 size) { … } /* * hv_ring_gpadl_send_hvpgoffset - Calculate the send offset (in unit of * HV_HYP_PAGE) in a ring gpadl based on the * offset in the guest * * @offset: the offset (in bytes) where the send ringbuffer starts in the * virtual address space of the guest */ static inline u32 hv_ring_gpadl_send_hvpgoffset(u32 offset) { … } /* * hv_gpadl_hvpfn - Return the Hyper-V page PFN of the @i th Hyper-V page in * the gpadl * * @type: the type of the gpadl * @kbuffer: the pointer to the gpadl in the guest * @size: the total size (in bytes) of the gpadl * @send_offset: the offset (in bytes) where the send ringbuffer starts in the * virtual address space of the guest * @i: the index */ static inline u64 hv_gpadl_hvpfn(enum hv_gpadl_type type, void *kbuffer, u32 size, u32 send_offset, int i) { … } /* * vmbus_setevent- Trigger an event notification on the specified * channel. */ void vmbus_setevent(struct vmbus_channel *channel) { … } EXPORT_SYMBOL_GPL(…); /* vmbus_free_ring - drop mapping of ring buffer */ void vmbus_free_ring(struct vmbus_channel *channel) { … } EXPORT_SYMBOL_GPL(…); /* vmbus_alloc_ring - allocate and map pages for ring buffer */ int vmbus_alloc_ring(struct vmbus_channel *newchannel, u32 send_size, u32 recv_size) { … } EXPORT_SYMBOL_GPL(…); /* Used for Hyper-V Socket: a guest client's connect() to the host */ int vmbus_send_tl_connect_request(const guid_t *shv_guest_servie_id, const guid_t *shv_host_servie_id) { … } EXPORT_SYMBOL_GPL(…); static int send_modifychannel_without_ack(struct vmbus_channel *channel, u32 target_vp) { … } static int send_modifychannel_with_ack(struct vmbus_channel *channel, u32 target_vp) { … } /* * Set/change the vCPU (@target_vp) the channel (@child_relid) will interrupt. * * CHANNELMSG_MODIFYCHANNEL messages are aynchronous. When VMbus version 5.3 * or later is negotiated, Hyper-V always sends an ACK in response to such a * message. For VMbus version 5.2 and earlier, it never sends an ACK. With- * out an ACK, we can not know when the host will stop interrupting the "old" * vCPU and start interrupting the "new" vCPU for the given channel. * * The CHANNELMSG_MODIFYCHANNEL message type is supported since VMBus version * VERSION_WIN10_V4_1. */ int vmbus_send_modifychannel(struct vmbus_channel *channel, u32 target_vp) { … } EXPORT_SYMBOL_GPL(…); /* * create_gpadl_header - Creates a gpadl for the specified buffer */ static int create_gpadl_header(enum hv_gpadl_type type, void *kbuffer, u32 size, u32 send_offset, struct vmbus_channel_msginfo **msginfo) { … } /* * __vmbus_establish_gpadl - Establish a GPADL for a buffer or ringbuffer * * @channel: a channel * @type: the type of the corresponding GPADL, only meaningful for the guest. * @kbuffer: from kmalloc or vmalloc * @size: page-size multiple * @send_offset: the offset (in bytes) where the send ring buffer starts, * should be 0 for BUFFER type gpadl * @gpadl_handle: some funky thing */ static int __vmbus_establish_gpadl(struct vmbus_channel *channel, enum hv_gpadl_type type, void *kbuffer, u32 size, u32 send_offset, struct vmbus_gpadl *gpadl) { … } /* * vmbus_establish_gpadl - Establish a GPADL for the specified buffer * * @channel: a channel * @kbuffer: from kmalloc or vmalloc * @size: page-size multiple * @gpadl_handle: some funky thing */ int vmbus_establish_gpadl(struct vmbus_channel *channel, void *kbuffer, u32 size, struct vmbus_gpadl *gpadl) { … } EXPORT_SYMBOL_GPL(…); /** * request_arr_init - Allocates memory for the requestor array. Each slot * keeps track of the next available slot in the array. Initially, each * slot points to the next one (as in a Linked List). The last slot * does not point to anything, so its value is U64_MAX by default. * @size The size of the array */ static u64 *request_arr_init(u32 size) { … } /* * vmbus_alloc_requestor - Initializes @rqstor's fields. * Index 0 is the first free slot * @size: Size of the requestor array */ static int vmbus_alloc_requestor(struct vmbus_requestor *rqstor, u32 size) { … } /* * vmbus_free_requestor - Frees memory allocated for @rqstor * @rqstor: Pointer to the requestor struct */ static void vmbus_free_requestor(struct vmbus_requestor *rqstor) { … } static int __vmbus_open(struct vmbus_channel *newchannel, void *userdata, u32 userdatalen, void (*onchannelcallback)(void *context), void *context) { … } /* * vmbus_connect_ring - Open the channel but reuse ring buffer */ int vmbus_connect_ring(struct vmbus_channel *newchannel, void (*onchannelcallback)(void *context), void *context) { … } EXPORT_SYMBOL_GPL(…); /* * vmbus_open - Open the specified channel. */ int vmbus_open(struct vmbus_channel *newchannel, u32 send_ringbuffer_size, u32 recv_ringbuffer_size, void *userdata, u32 userdatalen, void (*onchannelcallback)(void *context), void *context) { … } EXPORT_SYMBOL_GPL(…); /* * vmbus_teardown_gpadl -Teardown the specified GPADL handle */ int vmbus_teardown_gpadl(struct vmbus_channel *channel, struct vmbus_gpadl *gpadl) { … } EXPORT_SYMBOL_GPL(…); void vmbus_reset_channel_cb(struct vmbus_channel *channel) { … } static int vmbus_close_internal(struct vmbus_channel *channel) { … } /* disconnect ring - close all channels */ int vmbus_disconnect_ring(struct vmbus_channel *channel) { … } EXPORT_SYMBOL_GPL(…); /* * vmbus_close - Close the specified channel */ void vmbus_close(struct vmbus_channel *channel) { … } EXPORT_SYMBOL_GPL(…); /** * vmbus_sendpacket_getid() - Send the specified buffer on the given channel * @channel: Pointer to vmbus_channel structure * @buffer: Pointer to the buffer you want to send the data from. * @bufferlen: Maximum size of what the buffer holds. * @requestid: Identifier of the request * @trans_id: Identifier of the transaction associated to this request, if * the send is successful; undefined, otherwise. * @type: Type of packet that is being sent e.g. negotiate, time * packet etc. * @flags: 0 or VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED * * Sends data in @buffer directly to Hyper-V via the vmbus. * This will send the data unparsed to Hyper-V. * * Mainly used by Hyper-V drivers. */ int vmbus_sendpacket_getid(struct vmbus_channel *channel, void *buffer, u32 bufferlen, u64 requestid, u64 *trans_id, enum vmbus_packet_type type, u32 flags) { … } EXPORT_SYMBOL(…); /** * vmbus_sendpacket() - Send the specified buffer on the given channel * @channel: Pointer to vmbus_channel structure * @buffer: Pointer to the buffer you want to send the data from. * @bufferlen: Maximum size of what the buffer holds. * @requestid: Identifier of the request * @type: Type of packet that is being sent e.g. negotiate, time * packet etc. * @flags: 0 or VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED * * Sends data in @buffer directly to Hyper-V via the vmbus. * This will send the data unparsed to Hyper-V. * * Mainly used by Hyper-V drivers. */ int vmbus_sendpacket(struct vmbus_channel *channel, void *buffer, u32 bufferlen, u64 requestid, enum vmbus_packet_type type, u32 flags) { … } EXPORT_SYMBOL(…); /* * vmbus_sendpacket_pagebuffer - Send a range of single-page buffer * packets using a GPADL Direct packet type. This interface allows you * to control notifying the host. This will be useful for sending * batched data. Also the sender can control the send flags * explicitly. */ int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel, struct hv_page_buffer pagebuffers[], u32 pagecount, void *buffer, u32 bufferlen, u64 requestid) { … } EXPORT_SYMBOL_GPL(…); /* * vmbus_sendpacket_multipagebuffer - Send a multi-page buffer packet * using a GPADL Direct packet type. * The buffer includes the vmbus descriptor. */ int vmbus_sendpacket_mpb_desc(struct vmbus_channel *channel, struct vmbus_packet_mpb_array *desc, u32 desc_size, void *buffer, u32 bufferlen, u64 requestid) { … } EXPORT_SYMBOL_GPL(…); /** * __vmbus_recvpacket() - Retrieve the user packet on the specified channel * @channel: Pointer to vmbus_channel structure * @buffer: Pointer to the buffer you want to receive the data into. * @bufferlen: Maximum size of what the buffer can hold. * @buffer_actual_len: The actual size of the data after it was received. * @requestid: Identifier of the request * @raw: true means keep the vmpacket_descriptor header in the received data. * * Receives directly from the hyper-v vmbus and puts the data it received * into Buffer. This will receive the data unparsed from hyper-v. * * Mainly used by Hyper-V drivers. */ static inline int __vmbus_recvpacket(struct vmbus_channel *channel, void *buffer, u32 bufferlen, u32 *buffer_actual_len, u64 *requestid, bool raw) { … } int vmbus_recvpacket(struct vmbus_channel *channel, void *buffer, u32 bufferlen, u32 *buffer_actual_len, u64 *requestid) { … } EXPORT_SYMBOL(…); /* * vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel */ int vmbus_recvpacket_raw(struct vmbus_channel *channel, void *buffer, u32 bufferlen, u32 *buffer_actual_len, u64 *requestid) { … } EXPORT_SYMBOL_GPL(…); /* * vmbus_next_request_id - Returns a new request id. It is also * the index at which the guest memory address is stored. * Uses a spin lock to avoid race conditions. * @channel: Pointer to the VMbus channel struct * @rqst_add: Guest memory address to be stored in the array */ u64 vmbus_next_request_id(struct vmbus_channel *channel, u64 rqst_addr) { … } EXPORT_SYMBOL_GPL(…); /* As in vmbus_request_addr_match() but without the requestor lock */ u64 __vmbus_request_addr_match(struct vmbus_channel *channel, u64 trans_id, u64 rqst_addr) { … } EXPORT_SYMBOL_GPL(…); /* * vmbus_request_addr_match - Clears/removes @trans_id from the @channel's * requestor, provided the memory address stored at @trans_id equals @rqst_addr * (or provided @rqst_addr matches the sentinel value VMBUS_RQST_ADDR_ANY). * * Returns the memory address stored at @trans_id, or VMBUS_RQST_ERROR if * @trans_id is not contained in the requestor. * * Acquires and releases the requestor spin lock. */ u64 vmbus_request_addr_match(struct vmbus_channel *channel, u64 trans_id, u64 rqst_addr) { … } EXPORT_SYMBOL_GPL(…); /* * vmbus_request_addr - Returns the memory address stored at @trans_id * in @rqstor. Uses a spin lock to avoid race conditions. * @channel: Pointer to the VMbus channel struct * @trans_id: Request id sent back from Hyper-V. Becomes the requestor's * next request id. */ u64 vmbus_request_addr(struct vmbus_channel *channel, u64 trans_id) { … } EXPORT_SYMBOL_GPL(…);