2019-05-30 07:57:35 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2009-07-14 07:02:34 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2009, Microsoft Corporation.
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Haiyang Zhang <haiyangz@microsoft.com>
|
|
|
|
* Hank Janssen <hjanssen@microsoft.com>
|
|
|
|
*/
|
2011-03-30 04:58:47 +08:00
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
|
|
|
|
2009-07-15 06:08:20 +08:00
|
|
|
#include <linux/kernel.h>
|
2011-02-12 01:59:43 +08:00
|
|
|
#include <linux/sched.h>
|
|
|
|
#include <linux/wait.h>
|
2009-08-18 08:22:08 +08:00
|
|
|
#include <linux/mm.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 16:04:11 +08:00
|
|
|
#include <linux/slab.h>
|
2010-05-05 06:55:05 +08:00
|
|
|
#include <linux/module.h>
|
2011-10-05 03:29:52 +08:00
|
|
|
#include <linux/hyperv.h>
|
2014-02-02 11:02:20 +08:00
|
|
|
#include <linux/uio.h>
|
2015-12-15 08:01:47 +08:00
|
|
|
#include <linux/interrupt.h>
|
2018-08-02 11:08:24 +08:00
|
|
|
#include <asm/page.h>
|
2020-06-18 00:46:35 +08:00
|
|
|
#include <asm/mshyperv.h>
|
2011-05-13 10:34:15 +08:00
|
|
|
|
2011-05-13 10:34:28 +08:00
|
|
|
#include "hyperv_vmbus.h"
|
2009-07-14 07:02:34 +08:00
|
|
|
|
2020-09-16 11:48:09 +08:00
|
|
|
/*
|
|
|
|
* 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)
|
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
case HV_GPADL_BUFFER:
|
|
|
|
return size;
|
|
|
|
case HV_GPADL_RING:
|
|
|
|
/* The size of a ringbuffer must be page-aligned */
|
|
|
|
BUG_ON(size % PAGE_SIZE);
|
|
|
|
/*
|
|
|
|
* Two things to notice here:
|
|
|
|
* 1) We're processing two ring buffers as a unit
|
|
|
|
* 2) We're skipping any space larger than HV_HYP_PAGE_SIZE in
|
|
|
|
* the first guest-size page of each of the two ring buffers.
|
|
|
|
* So we effectively subtract out two guest-size pages, and add
|
|
|
|
* back two Hyper-V size pages.
|
|
|
|
*/
|
|
|
|
return size - 2 * (PAGE_SIZE - HV_HYP_PAGE_SIZE);
|
|
|
|
}
|
|
|
|
BUG();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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)
|
|
|
|
{
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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 to calculate the effective send offset in gpadl, we need to
|
|
|
|
* substract this gap.
|
|
|
|
*/
|
|
|
|
return (offset - (PAGE_SIZE - HV_HYP_PAGE_SIZE)) >> HV_HYP_PAGE_SHIFT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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)
|
|
|
|
{
|
|
|
|
int send_idx = hv_ring_gpadl_send_hvpgoffset(send_offset);
|
|
|
|
unsigned long delta = 0UL;
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case HV_GPADL_BUFFER:
|
|
|
|
break;
|
|
|
|
case HV_GPADL_RING:
|
|
|
|
if (i == 0)
|
|
|
|
delta = 0;
|
|
|
|
else if (i <= send_idx)
|
|
|
|
delta = PAGE_SIZE - HV_HYP_PAGE_SIZE;
|
|
|
|
else
|
|
|
|
delta = 2 * (PAGE_SIZE - HV_HYP_PAGE_SIZE);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
BUG();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return virt_to_hvpfn(kbuffer + delta + (HV_HYP_PAGE_SIZE * i));
|
|
|
|
}
|
|
|
|
|
2010-03-05 06:11:00 +08:00
|
|
|
/*
|
2010-10-08 02:40:08 +08:00
|
|
|
* vmbus_setevent- Trigger an event notification on the specified
|
2010-03-05 06:11:00 +08:00
|
|
|
* channel.
|
2009-09-02 08:24:57 +08:00
|
|
|
*/
|
2016-11-07 05:14:17 +08:00
|
|
|
void vmbus_setevent(struct vmbus_channel *channel)
|
2009-07-14 07:02:34 +08:00
|
|
|
{
|
2010-10-01 01:52:13 +08:00
|
|
|
struct hv_monitor_page *monitorpage;
|
2009-07-14 07:02:34 +08:00
|
|
|
|
2017-10-30 03:21:16 +08:00
|
|
|
trace_vmbus_setevent(channel);
|
|
|
|
|
2016-07-02 07:26:37 +08:00
|
|
|
/*
|
|
|
|
* For channels marked as in "low latency" mode
|
|
|
|
* bypass the monitor page mechanism.
|
|
|
|
*/
|
2017-02-06 08:20:31 +08:00
|
|
|
if (channel->offermsg.monitor_allocated && !channel->low_latency) {
|
|
|
|
vmbus_send_interrupt(channel->offermsg.child_relid);
|
2009-07-14 07:02:34 +08:00
|
|
|
|
2013-09-14 02:32:55 +08:00
|
|
|
/* Get the child to parent monitor page */
|
|
|
|
monitorpage = vmbus_connection.monitor_pages[1];
|
2009-07-14 07:02:34 +08:00
|
|
|
|
2011-03-21 21:41:37 +08:00
|
|
|
sync_set_bit(channel->monitor_bit,
|
2010-11-09 06:04:39 +08:00
|
|
|
(unsigned long *)&monitorpage->trigger_group
|
|
|
|
[channel->monitor_grp].pending);
|
2009-07-30 05:00:11 +08:00
|
|
|
|
2009-09-02 08:24:57 +08:00
|
|
|
} else {
|
2012-12-01 22:46:43 +08:00
|
|
|
vmbus_set_event(channel);
|
2009-07-14 07:02:34 +08:00
|
|
|
}
|
|
|
|
}
|
2016-11-07 05:14:17 +08:00
|
|
|
EXPORT_SYMBOL_GPL(vmbus_setevent);
|
2009-07-14 07:02:34 +08:00
|
|
|
|
2018-09-15 00:10:17 +08:00
|
|
|
/* vmbus_free_ring - drop mapping of ring buffer */
|
|
|
|
void vmbus_free_ring(struct vmbus_channel *channel)
|
2009-07-14 07:02:34 +08:00
|
|
|
{
|
2018-09-15 00:10:17 +08:00
|
|
|
hv_ringbuffer_cleanup(&channel->outbound);
|
|
|
|
hv_ringbuffer_cleanup(&channel->inbound);
|
2009-07-14 07:02:34 +08:00
|
|
|
|
2018-09-15 00:10:17 +08:00
|
|
|
if (channel->ringbuffer_page) {
|
|
|
|
__free_pages(channel->ringbuffer_page,
|
|
|
|
get_order(channel->ringbuffer_pagecount
|
|
|
|
<< PAGE_SHIFT));
|
|
|
|
channel->ringbuffer_page = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(vmbus_free_ring);
|
2016-09-02 20:58:19 +08:00
|
|
|
|
2018-09-15 00:10:17 +08:00
|
|
|
/* vmbus_alloc_ring - allocate and map pages for ring buffer */
|
|
|
|
int vmbus_alloc_ring(struct vmbus_channel *newchannel,
|
|
|
|
u32 send_size, u32 recv_size)
|
|
|
|
{
|
|
|
|
struct page *page;
|
|
|
|
int order;
|
2018-09-15 00:10:16 +08:00
|
|
|
|
2018-09-15 00:10:17 +08:00
|
|
|
if (send_size % PAGE_SIZE || recv_size % PAGE_SIZE)
|
2013-05-24 03:02:32 +08:00
|
|
|
return -EINVAL;
|
2009-07-14 07:02:34 +08:00
|
|
|
|
2009-07-28 04:47:24 +08:00
|
|
|
/* Allocate the ring buffer */
|
2018-09-15 00:10:17 +08:00
|
|
|
order = get_order(send_size + recv_size);
|
2015-06-01 12:27:03 +08:00
|
|
|
page = alloc_pages_node(cpu_to_node(newchannel->target_cpu),
|
2018-09-15 00:10:16 +08:00
|
|
|
GFP_KERNEL|__GFP_ZERO, order);
|
2015-06-01 12:27:03 +08:00
|
|
|
|
|
|
|
if (!page)
|
2018-09-15 00:10:16 +08:00
|
|
|
page = alloc_pages(GFP_KERNEL|__GFP_ZERO, order);
|
2011-02-12 01:59:00 +08:00
|
|
|
|
2018-09-15 00:10:17 +08:00
|
|
|
if (!page)
|
|
|
|
return -ENOMEM;
|
2009-07-14 07:02:34 +08:00
|
|
|
|
2018-09-15 00:10:16 +08:00
|
|
|
newchannel->ringbuffer_page = page;
|
2018-09-15 00:10:17 +08:00
|
|
|
newchannel->ringbuffer_pagecount = (send_size + recv_size) >> PAGE_SHIFT;
|
|
|
|
newchannel->ringbuffer_send_offset = send_size >> PAGE_SHIFT;
|
2009-07-14 07:02:34 +08:00
|
|
|
|
2018-09-15 00:10:17 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(vmbus_alloc_ring);
|
2011-05-10 22:55:21 +08:00
|
|
|
|
2016-01-28 14:29:40 +08:00
|
|
|
/* Used for Hyper-V Socket: a guest client's connect() to the host */
|
2019-01-10 22:25:32 +08:00
|
|
|
int vmbus_send_tl_connect_request(const guid_t *shv_guest_servie_id,
|
|
|
|
const guid_t *shv_host_servie_id)
|
2016-01-28 14:29:40 +08:00
|
|
|
{
|
|
|
|
struct vmbus_channel_tl_connect_request conn_msg;
|
2017-10-30 03:21:15 +08:00
|
|
|
int ret;
|
2016-01-28 14:29:40 +08:00
|
|
|
|
|
|
|
memset(&conn_msg, 0, sizeof(conn_msg));
|
|
|
|
conn_msg.header.msgtype = CHANNELMSG_TL_CONNECT_REQUEST;
|
|
|
|
conn_msg.guest_endpoint_id = *shv_guest_servie_id;
|
|
|
|
conn_msg.host_service_id = *shv_host_servie_id;
|
|
|
|
|
2017-10-30 03:21:15 +08:00
|
|
|
ret = vmbus_post_msg(&conn_msg, sizeof(conn_msg), true);
|
|
|
|
|
|
|
|
trace_vmbus_send_tl_connect_request(&conn_msg, ret);
|
|
|
|
|
|
|
|
return ret;
|
2016-01-28 14:29:40 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(vmbus_send_tl_connect_request);
|
|
|
|
|
2021-04-16 22:34:48 +08:00
|
|
|
static int send_modifychannel_without_ack(struct vmbus_channel *channel, u32 target_vp)
|
|
|
|
{
|
|
|
|
struct vmbus_channel_modifychannel msg;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
memset(&msg, 0, sizeof(msg));
|
|
|
|
msg.header.msgtype = CHANNELMSG_MODIFYCHANNEL;
|
|
|
|
msg.child_relid = channel->offermsg.child_relid;
|
|
|
|
msg.target_vp = target_vp;
|
|
|
|
|
|
|
|
ret = vmbus_post_msg(&msg, sizeof(msg), true);
|
|
|
|
trace_vmbus_send_modifychannel(&msg, ret);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int send_modifychannel_with_ack(struct vmbus_channel *channel, u32 target_vp)
|
|
|
|
{
|
|
|
|
struct vmbus_channel_modifychannel *msg;
|
|
|
|
struct vmbus_channel_msginfo *info;
|
|
|
|
unsigned long flags;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
info = kzalloc(sizeof(struct vmbus_channel_msginfo) +
|
|
|
|
sizeof(struct vmbus_channel_modifychannel),
|
|
|
|
GFP_KERNEL);
|
|
|
|
if (!info)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
init_completion(&info->waitevent);
|
|
|
|
info->waiting_channel = channel;
|
|
|
|
|
|
|
|
msg = (struct vmbus_channel_modifychannel *)info->msg;
|
|
|
|
msg->header.msgtype = CHANNELMSG_MODIFYCHANNEL;
|
|
|
|
msg->child_relid = channel->offermsg.child_relid;
|
|
|
|
msg->target_vp = target_vp;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
|
|
|
|
list_add_tail(&info->msglistentry, &vmbus_connection.chn_msg_list);
|
|
|
|
spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
|
|
|
|
|
|
|
|
ret = vmbus_post_msg(msg, sizeof(*msg), true);
|
|
|
|
trace_vmbus_send_modifychannel(msg, ret);
|
|
|
|
if (ret != 0) {
|
|
|
|
spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
|
|
|
|
list_del(&info->msglistentry);
|
|
|
|
spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
|
|
|
|
goto free_info;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Release channel_mutex; otherwise, vmbus_onoffer_rescind() could block on
|
|
|
|
* the mutex and be unable to signal the completion.
|
|
|
|
*
|
|
|
|
* See the caller target_cpu_store() for information about the usage of the
|
|
|
|
* mutex.
|
|
|
|
*/
|
|
|
|
mutex_unlock(&vmbus_connection.channel_mutex);
|
|
|
|
wait_for_completion(&info->waitevent);
|
|
|
|
mutex_lock(&vmbus_connection.channel_mutex);
|
|
|
|
|
|
|
|
spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
|
|
|
|
list_del(&info->msglistentry);
|
|
|
|
spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
|
|
|
|
|
|
|
|
if (info->response.modify_response.status)
|
|
|
|
ret = -EAGAIN;
|
|
|
|
|
|
|
|
free_info:
|
|
|
|
kfree(info);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-04-06 08:15:13 +08:00
|
|
|
/*
|
|
|
|
* Set/change the vCPU (@target_vp) the channel (@child_relid) will interrupt.
|
|
|
|
*
|
2021-04-16 22:34:48 +08:00
|
|
|
* 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.
|
2020-04-06 08:15:13 +08:00
|
|
|
*
|
|
|
|
* The CHANNELMSG_MODIFYCHANNEL message type is supported since VMBus version
|
|
|
|
* VERSION_WIN10_V4_1.
|
|
|
|
*/
|
2021-04-16 22:34:48 +08:00
|
|
|
int vmbus_send_modifychannel(struct vmbus_channel *channel, u32 target_vp)
|
2020-04-06 08:15:13 +08:00
|
|
|
{
|
2021-04-16 22:34:48 +08:00
|
|
|
if (vmbus_proto_version >= VERSION_WIN10_V5_3)
|
|
|
|
return send_modifychannel_with_ack(channel, target_vp);
|
|
|
|
return send_modifychannel_without_ack(channel, target_vp);
|
2020-04-06 08:15:13 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(vmbus_send_modifychannel);
|
|
|
|
|
2010-03-05 06:11:00 +08:00
|
|
|
/*
|
2010-10-08 02:40:08 +08:00
|
|
|
* create_gpadl_header - Creates a gpadl for the specified buffer
|
2009-09-02 08:24:57 +08:00
|
|
|
*/
|
2020-09-16 11:48:09 +08:00
|
|
|
static int create_gpadl_header(enum hv_gpadl_type type, void *kbuffer,
|
|
|
|
u32 size, u32 send_offset,
|
2016-06-04 08:09:23 +08:00
|
|
|
struct vmbus_channel_msginfo **msginfo)
|
2009-07-14 07:02:34 +08:00
|
|
|
{
|
|
|
|
int i;
|
2010-10-01 01:52:13 +08:00
|
|
|
int pagecount;
|
|
|
|
struct vmbus_channel_gpadl_header *gpadl_header;
|
|
|
|
struct vmbus_channel_gpadl_body *gpadl_body;
|
|
|
|
struct vmbus_channel_msginfo *msgheader;
|
|
|
|
struct vmbus_channel_msginfo *msgbody = NULL;
|
|
|
|
u32 msgsize;
|
2009-07-14 07:02:34 +08:00
|
|
|
|
2010-10-01 01:52:13 +08:00
|
|
|
int pfnsum, pfncount, pfnleft, pfncurr, pfnsize;
|
2009-07-14 07:02:34 +08:00
|
|
|
|
2020-09-16 11:48:09 +08:00
|
|
|
pagecount = hv_gpadl_size(type, size) >> HV_HYP_PAGE_SHIFT;
|
2009-07-14 07:02:34 +08:00
|
|
|
|
2009-07-28 04:47:24 +08:00
|
|
|
/* do we need a gpadl body msg */
|
2010-10-01 01:52:13 +08:00
|
|
|
pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
|
2009-09-02 08:24:57 +08:00
|
|
|
sizeof(struct vmbus_channel_gpadl_header) -
|
|
|
|
sizeof(struct gpa_range);
|
2010-10-01 01:52:13 +08:00
|
|
|
pfncount = pfnsize / sizeof(u64);
|
2009-07-14 07:02:34 +08:00
|
|
|
|
2010-10-01 01:52:13 +08:00
|
|
|
if (pagecount > pfncount) {
|
2009-09-02 08:24:57 +08:00
|
|
|
/* we need a gpadl body */
|
2009-07-28 04:47:24 +08:00
|
|
|
/* fill in the header */
|
2010-10-01 01:52:13 +08:00
|
|
|
msgsize = sizeof(struct vmbus_channel_msginfo) +
|
2009-09-02 08:24:57 +08:00
|
|
|
sizeof(struct vmbus_channel_gpadl_header) +
|
2010-10-01 01:52:13 +08:00
|
|
|
sizeof(struct gpa_range) + pfncount * sizeof(u64);
|
|
|
|
msgheader = kzalloc(msgsize, GFP_KERNEL);
|
|
|
|
if (!msgheader)
|
2010-05-06 03:27:35 +08:00
|
|
|
goto nomem;
|
2009-07-14 07:02:34 +08:00
|
|
|
|
2010-11-09 06:04:38 +08:00
|
|
|
INIT_LIST_HEAD(&msgheader->submsglist);
|
|
|
|
msgheader->msgsize = msgsize;
|
2009-07-14 07:02:34 +08:00
|
|
|
|
2010-10-01 01:52:13 +08:00
|
|
|
gpadl_header = (struct vmbus_channel_gpadl_header *)
|
2010-11-09 06:04:38 +08:00
|
|
|
msgheader->msg;
|
|
|
|
gpadl_header->rangecount = 1;
|
|
|
|
gpadl_header->range_buflen = sizeof(struct gpa_range) +
|
2010-10-01 01:52:13 +08:00
|
|
|
pagecount * sizeof(u64);
|
2011-01-27 04:12:13 +08:00
|
|
|
gpadl_header->range[0].byte_offset = 0;
|
2020-09-16 11:48:09 +08:00
|
|
|
gpadl_header->range[0].byte_count = hv_gpadl_size(type, size);
|
2010-10-01 01:52:13 +08:00
|
|
|
for (i = 0; i < pfncount; i++)
|
2020-09-16 11:48:09 +08:00
|
|
|
gpadl_header->range[0].pfn_array[i] = hv_gpadl_hvpfn(
|
|
|
|
type, kbuffer, size, send_offset, i);
|
2010-10-01 01:52:13 +08:00
|
|
|
*msginfo = msgheader;
|
2009-07-14 07:02:34 +08:00
|
|
|
|
2010-10-01 01:52:13 +08:00
|
|
|
pfnsum = pfncount;
|
|
|
|
pfnleft = pagecount - pfncount;
|
2009-07-14 07:02:34 +08:00
|
|
|
|
2009-07-28 04:47:24 +08:00
|
|
|
/* how many pfns can we fit */
|
2010-10-01 01:52:13 +08:00
|
|
|
pfnsize = MAX_SIZE_CHANNEL_MESSAGE -
|
2009-09-02 08:24:57 +08:00
|
|
|
sizeof(struct vmbus_channel_gpadl_body);
|
2010-10-01 01:52:13 +08:00
|
|
|
pfncount = pfnsize / sizeof(u64);
|
2009-07-14 07:02:34 +08:00
|
|
|
|
2009-07-28 04:47:24 +08:00
|
|
|
/* fill in the body */
|
2010-10-01 01:52:13 +08:00
|
|
|
while (pfnleft) {
|
|
|
|
if (pfnleft > pfncount)
|
|
|
|
pfncurr = pfncount;
|
2009-07-14 07:02:34 +08:00
|
|
|
else
|
2010-10-01 01:52:13 +08:00
|
|
|
pfncurr = pfnleft;
|
2009-07-14 07:02:34 +08:00
|
|
|
|
2010-10-01 01:52:13 +08:00
|
|
|
msgsize = sizeof(struct vmbus_channel_msginfo) +
|
2009-09-02 08:24:57 +08:00
|
|
|
sizeof(struct vmbus_channel_gpadl_body) +
|
2010-10-01 01:52:13 +08:00
|
|
|
pfncurr * sizeof(u64);
|
|
|
|
msgbody = kzalloc(msgsize, GFP_KERNEL);
|
2011-06-07 06:50:06 +08:00
|
|
|
|
|
|
|
if (!msgbody) {
|
|
|
|
struct vmbus_channel_msginfo *pos = NULL;
|
|
|
|
struct vmbus_channel_msginfo *tmp = NULL;
|
|
|
|
/*
|
|
|
|
* Free up all the allocated messages.
|
|
|
|
*/
|
|
|
|
list_for_each_entry_safe(pos, tmp,
|
|
|
|
&msgheader->submsglist,
|
|
|
|
msglistentry) {
|
|
|
|
|
|
|
|
list_del(&pos->msglistentry);
|
|
|
|
kfree(pos);
|
|
|
|
}
|
|
|
|
|
2010-05-06 03:27:35 +08:00
|
|
|
goto nomem;
|
2011-06-07 06:50:06 +08:00
|
|
|
}
|
|
|
|
|
2010-11-09 06:04:38 +08:00
|
|
|
msgbody->msgsize = msgsize;
|
2010-10-01 01:52:13 +08:00
|
|
|
gpadl_body =
|
2010-11-09 06:04:38 +08:00
|
|
|
(struct vmbus_channel_gpadl_body *)msgbody->msg;
|
2009-09-02 08:24:57 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Gpadl is u32 and we are using a pointer which could
|
|
|
|
* be 64-bit
|
2011-06-07 06:49:56 +08:00
|
|
|
* This is governed by the guest/host protocol and
|
2017-03-05 09:27:14 +08:00
|
|
|
* so the hypervisor guarantees that this is ok.
|
2009-09-02 08:24:57 +08:00
|
|
|
*/
|
2010-10-01 01:52:13 +08:00
|
|
|
for (i = 0; i < pfncurr; i++)
|
2020-09-16 11:48:09 +08:00
|
|
|
gpadl_body->pfn[i] = hv_gpadl_hvpfn(type,
|
|
|
|
kbuffer, size, send_offset, pfnsum + i);
|
2009-07-14 07:02:34 +08:00
|
|
|
|
2009-07-28 04:47:24 +08:00
|
|
|
/* add to msg header */
|
2010-11-09 06:04:38 +08:00
|
|
|
list_add_tail(&msgbody->msglistentry,
|
|
|
|
&msgheader->submsglist);
|
2010-10-01 01:52:13 +08:00
|
|
|
pfnsum += pfncurr;
|
|
|
|
pfnleft -= pfncurr;
|
2009-07-14 07:02:34 +08:00
|
|
|
}
|
2009-09-02 08:24:57 +08:00
|
|
|
} else {
|
2009-07-28 04:47:24 +08:00
|
|
|
/* everything fits in a header */
|
2010-10-01 01:52:13 +08:00
|
|
|
msgsize = sizeof(struct vmbus_channel_msginfo) +
|
2009-09-02 08:24:57 +08:00
|
|
|
sizeof(struct vmbus_channel_gpadl_header) +
|
2010-10-01 01:52:13 +08:00
|
|
|
sizeof(struct gpa_range) + pagecount * sizeof(u64);
|
|
|
|
msgheader = kzalloc(msgsize, GFP_KERNEL);
|
|
|
|
if (msgheader == NULL)
|
2010-07-17 00:13:51 +08:00
|
|
|
goto nomem;
|
2016-06-04 08:09:23 +08:00
|
|
|
|
|
|
|
INIT_LIST_HEAD(&msgheader->submsglist);
|
2010-11-09 06:04:38 +08:00
|
|
|
msgheader->msgsize = msgsize;
|
2010-10-01 01:52:13 +08:00
|
|
|
|
|
|
|
gpadl_header = (struct vmbus_channel_gpadl_header *)
|
2010-11-09 06:04:38 +08:00
|
|
|
msgheader->msg;
|
|
|
|
gpadl_header->rangecount = 1;
|
|
|
|
gpadl_header->range_buflen = sizeof(struct gpa_range) +
|
2010-10-01 01:52:13 +08:00
|
|
|
pagecount * sizeof(u64);
|
2011-01-27 04:12:13 +08:00
|
|
|
gpadl_header->range[0].byte_offset = 0;
|
2020-09-16 11:48:09 +08:00
|
|
|
gpadl_header->range[0].byte_count = hv_gpadl_size(type, size);
|
2010-10-01 01:52:13 +08:00
|
|
|
for (i = 0; i < pagecount; i++)
|
2020-09-16 11:48:09 +08:00
|
|
|
gpadl_header->range[0].pfn_array[i] = hv_gpadl_hvpfn(
|
|
|
|
type, kbuffer, size, send_offset, i);
|
2010-10-01 01:52:13 +08:00
|
|
|
|
|
|
|
*msginfo = msgheader;
|
2009-07-14 07:02:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2010-05-06 03:27:35 +08:00
|
|
|
nomem:
|
2010-10-01 01:52:13 +08:00
|
|
|
kfree(msgheader);
|
|
|
|
kfree(msgbody);
|
2010-05-06 03:27:35 +08:00
|
|
|
return -ENOMEM;
|
2009-07-14 07:02:34 +08:00
|
|
|
}
|
|
|
|
|
2010-03-05 06:11:00 +08:00
|
|
|
/*
|
2020-09-16 11:48:09 +08:00
|
|
|
* __vmbus_establish_gpadl - Establish a GPADL for a buffer or ringbuffer
|
2009-09-02 08:24:57 +08:00
|
|
|
*
|
2010-10-01 01:52:13 +08:00
|
|
|
* @channel: a channel
|
2020-09-16 11:48:09 +08:00
|
|
|
* @type: the type of the corresponding GPADL, only meaningful for the guest.
|
2014-01-28 07:03:42 +08:00
|
|
|
* @kbuffer: from kmalloc or vmalloc
|
2010-10-01 01:52:13 +08:00
|
|
|
* @size: page-size multiple
|
2020-09-16 11:48:09 +08:00
|
|
|
* @send_offset: the offset (in bytes) where the send ring buffer starts,
|
2021-02-20 01:13:11 +08:00
|
|
|
* should be 0 for BUFFER type gpadl
|
2010-10-01 01:52:13 +08:00
|
|
|
* @gpadl_handle: some funky thing
|
2009-09-02 08:24:57 +08:00
|
|
|
*/
|
2020-09-16 11:48:09 +08:00
|
|
|
static int __vmbus_establish_gpadl(struct vmbus_channel *channel,
|
|
|
|
enum hv_gpadl_type type, void *kbuffer,
|
|
|
|
u32 size, u32 send_offset,
|
|
|
|
u32 *gpadl_handle)
|
2009-07-14 07:02:34 +08:00
|
|
|
{
|
2010-10-01 01:52:13 +08:00
|
|
|
struct vmbus_channel_gpadl_header *gpadlmsg;
|
|
|
|
struct vmbus_channel_gpadl_body *gpadl_body;
|
|
|
|
struct vmbus_channel_msginfo *msginfo = NULL;
|
2016-06-04 08:09:24 +08:00
|
|
|
struct vmbus_channel_msginfo *submsginfo, *tmp;
|
2009-09-12 09:46:44 +08:00
|
|
|
struct list_head *curr;
|
2010-10-01 01:52:13 +08:00
|
|
|
u32 next_gpadl_handle;
|
2009-07-16 05:56:45 +08:00
|
|
|
unsigned long flags;
|
2010-05-06 03:27:34 +08:00
|
|
|
int ret = 0;
|
2009-07-14 07:02:34 +08:00
|
|
|
|
2015-01-10 15:54:33 +08:00
|
|
|
next_gpadl_handle =
|
|
|
|
(atomic_inc_return(&vmbus_connection.next_gpadl_handle) - 1);
|
2009-07-14 07:02:34 +08:00
|
|
|
|
2020-09-16 11:48:09 +08:00
|
|
|
ret = create_gpadl_header(type, kbuffer, size, send_offset, &msginfo);
|
2010-05-06 03:27:34 +08:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2009-07-14 07:02:34 +08:00
|
|
|
|
2011-05-10 22:55:39 +08:00
|
|
|
init_completion(&msginfo->waitevent);
|
2016-12-23 08:54:00 +08:00
|
|
|
msginfo->waiting_channel = channel;
|
2010-05-06 03:27:34 +08:00
|
|
|
|
2010-11-09 06:04:38 +08:00
|
|
|
gpadlmsg = (struct vmbus_channel_gpadl_header *)msginfo->msg;
|
|
|
|
gpadlmsg->header.msgtype = CHANNELMSG_GPADL_HEADER;
|
|
|
|
gpadlmsg->child_relid = channel->offermsg.child_relid;
|
|
|
|
gpadlmsg->gpadl = next_gpadl_handle;
|
2009-07-14 07:02:34 +08:00
|
|
|
|
|
|
|
|
2011-01-27 04:12:07 +08:00
|
|
|
spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
|
2010-11-09 06:04:38 +08:00
|
|
|
list_add_tail(&msginfo->msglistentry,
|
2011-01-27 04:12:14 +08:00
|
|
|
&vmbus_connection.chn_msg_list);
|
2009-07-14 07:02:34 +08:00
|
|
|
|
2011-01-27 04:12:07 +08:00
|
|
|
spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
|
2009-07-14 07:02:34 +08:00
|
|
|
|
2017-08-12 01:03:59 +08:00
|
|
|
if (channel->rescind) {
|
|
|
|
ret = -ENODEV;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2011-01-27 04:12:08 +08:00
|
|
|
ret = vmbus_post_msg(gpadlmsg, msginfo->msgsize -
|
2016-12-07 17:16:24 +08:00
|
|
|
sizeof(*msginfo), true);
|
2017-10-30 03:21:11 +08:00
|
|
|
|
|
|
|
trace_vmbus_establish_gpadl_header(gpadlmsg, ret);
|
|
|
|
|
2011-03-30 04:58:44 +08:00
|
|
|
if (ret != 0)
|
2011-06-07 06:50:12 +08:00
|
|
|
goto cleanup;
|
2009-07-14 07:02:34 +08:00
|
|
|
|
2016-06-04 08:09:23 +08:00
|
|
|
list_for_each(curr, &msginfo->submsglist) {
|
|
|
|
submsginfo = (struct vmbus_channel_msginfo *)curr;
|
|
|
|
gpadl_body =
|
|
|
|
(struct vmbus_channel_gpadl_body *)submsginfo->msg;
|
2009-09-12 09:46:44 +08:00
|
|
|
|
2016-06-04 08:09:23 +08:00
|
|
|
gpadl_body->header.msgtype =
|
|
|
|
CHANNELMSG_GPADL_BODY;
|
|
|
|
gpadl_body->gpadl = next_gpadl_handle;
|
2009-07-14 07:02:34 +08:00
|
|
|
|
2016-06-04 08:09:23 +08:00
|
|
|
ret = vmbus_post_msg(gpadl_body,
|
2016-12-07 17:16:24 +08:00
|
|
|
submsginfo->msgsize - sizeof(*submsginfo),
|
|
|
|
true);
|
2017-10-30 03:21:11 +08:00
|
|
|
|
|
|
|
trace_vmbus_establish_gpadl_body(gpadl_body, ret);
|
|
|
|
|
2016-06-04 08:09:23 +08:00
|
|
|
if (ret != 0)
|
|
|
|
goto cleanup;
|
2009-07-14 07:02:34 +08:00
|
|
|
|
|
|
|
}
|
2014-08-28 07:25:34 +08:00
|
|
|
wait_for_completion(&msginfo->waitevent);
|
2009-07-14 07:02:34 +08:00
|
|
|
|
2018-11-26 10:29:56 +08:00
|
|
|
if (msginfo->response.gpadl_created.creation_status != 0) {
|
|
|
|
pr_err("Failed to establish GPADL: err = 0x%x\n",
|
|
|
|
msginfo->response.gpadl_created.creation_status);
|
|
|
|
|
|
|
|
ret = -EDQUOT;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2016-12-23 08:54:00 +08:00
|
|
|
if (channel->rescind) {
|
|
|
|
ret = -ENODEV;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2009-07-28 04:47:24 +08:00
|
|
|
/* At this point, we received the gpadl created msg */
|
2010-11-09 06:04:38 +08:00
|
|
|
*gpadl_handle = gpadlmsg->gpadl;
|
2009-07-14 07:02:34 +08:00
|
|
|
|
2011-06-07 06:50:12 +08:00
|
|
|
cleanup:
|
2011-01-27 04:12:07 +08:00
|
|
|
spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
|
2010-11-09 06:04:38 +08:00
|
|
|
list_del(&msginfo->msglistentry);
|
2011-01-27 04:12:07 +08:00
|
|
|
spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
|
2016-06-04 08:09:24 +08:00
|
|
|
list_for_each_entry_safe(submsginfo, tmp, &msginfo->submsglist,
|
|
|
|
msglistentry) {
|
|
|
|
kfree(submsginfo);
|
|
|
|
}
|
2009-07-14 07:02:34 +08:00
|
|
|
|
2010-10-01 01:52:13 +08:00
|
|
|
kfree(msginfo);
|
2009-07-14 07:02:34 +08:00
|
|
|
return ret;
|
|
|
|
}
|
2020-09-16 11:48:09 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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, u32 *gpadl_handle)
|
|
|
|
{
|
|
|
|
return __vmbus_establish_gpadl(channel, HV_GPADL_BUFFER, kbuffer, size,
|
|
|
|
0U, gpadl_handle);
|
|
|
|
}
|
2010-10-21 23:47:43 +08:00
|
|
|
EXPORT_SYMBOL_GPL(vmbus_establish_gpadl);
|
2009-07-14 07:02:34 +08:00
|
|
|
|
2020-11-09 18:04:00 +08:00
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
u64 *req_arr;
|
|
|
|
|
|
|
|
req_arr = kcalloc(size, sizeof(u64), GFP_KERNEL);
|
|
|
|
if (!req_arr)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for (i = 0; i < size - 1; i++)
|
|
|
|
req_arr[i] = i + 1;
|
|
|
|
|
|
|
|
/* Last slot (no more available slots) */
|
|
|
|
req_arr[i] = U64_MAX;
|
|
|
|
|
|
|
|
return req_arr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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)
|
|
|
|
{
|
|
|
|
u64 *rqst_arr;
|
|
|
|
unsigned long *bitmap;
|
|
|
|
|
|
|
|
rqst_arr = request_arr_init(size);
|
|
|
|
if (!rqst_arr)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
bitmap = bitmap_zalloc(size, GFP_KERNEL);
|
|
|
|
if (!bitmap) {
|
|
|
|
kfree(rqst_arr);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
rqstor->req_arr = rqst_arr;
|
|
|
|
rqstor->req_bitmap = bitmap;
|
|
|
|
rqstor->size = size;
|
|
|
|
rqstor->next_request_id = 0;
|
|
|
|
spin_lock_init(&rqstor->req_lock);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* vmbus_free_requestor - Frees memory allocated for @rqstor
|
|
|
|
* @rqstor: Pointer to the requestor struct
|
|
|
|
*/
|
|
|
|
static void vmbus_free_requestor(struct vmbus_requestor *rqstor)
|
|
|
|
{
|
|
|
|
kfree(rqstor->req_arr);
|
|
|
|
bitmap_free(rqstor->req_bitmap);
|
|
|
|
}
|
|
|
|
|
2020-09-16 11:48:08 +08:00
|
|
|
static int __vmbus_open(struct vmbus_channel *newchannel,
|
|
|
|
void *userdata, u32 userdatalen,
|
|
|
|
void (*onchannelcallback)(void *context), void *context)
|
|
|
|
{
|
|
|
|
struct vmbus_channel_open_channel *open_msg;
|
|
|
|
struct vmbus_channel_msginfo *open_info = NULL;
|
|
|
|
struct page *page = newchannel->ringbuffer_page;
|
|
|
|
u32 send_pages, recv_pages;
|
|
|
|
unsigned long flags;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (userdatalen > MAX_USER_DEFINED_BYTES)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
send_pages = newchannel->ringbuffer_send_offset;
|
|
|
|
recv_pages = newchannel->ringbuffer_pagecount - send_pages;
|
|
|
|
|
|
|
|
if (newchannel->state != CHANNEL_OPEN_STATE)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2020-11-09 18:04:00 +08:00
|
|
|
/* Create and init requestor */
|
|
|
|
if (newchannel->rqstor_size) {
|
|
|
|
if (vmbus_alloc_requestor(&newchannel->requestor, newchannel->rqstor_size))
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2020-09-16 11:48:08 +08:00
|
|
|
newchannel->state = CHANNEL_OPENING_STATE;
|
|
|
|
newchannel->onchannel_callback = onchannelcallback;
|
|
|
|
newchannel->channel_callback_context = context;
|
|
|
|
|
2021-04-09 00:14:39 +08:00
|
|
|
if (!newchannel->max_pkt_size)
|
|
|
|
newchannel->max_pkt_size = VMBUS_DEFAULT_MAX_PKT_SIZE;
|
|
|
|
|
|
|
|
err = hv_ringbuffer_init(&newchannel->outbound, page, send_pages, 0);
|
2020-09-16 11:48:08 +08:00
|
|
|
if (err)
|
|
|
|
goto error_clean_ring;
|
|
|
|
|
2021-04-09 00:14:39 +08:00
|
|
|
err = hv_ringbuffer_init(&newchannel->inbound, &page[send_pages],
|
|
|
|
recv_pages, newchannel->max_pkt_size);
|
2020-09-16 11:48:08 +08:00
|
|
|
if (err)
|
|
|
|
goto error_clean_ring;
|
|
|
|
|
|
|
|
/* Establish the gpadl for the ring buffer */
|
|
|
|
newchannel->ringbuffer_gpadlhandle = 0;
|
|
|
|
|
2020-09-16 11:48:09 +08:00
|
|
|
err = __vmbus_establish_gpadl(newchannel, HV_GPADL_RING,
|
|
|
|
page_address(newchannel->ringbuffer_page),
|
|
|
|
(send_pages + recv_pages) << PAGE_SHIFT,
|
|
|
|
newchannel->ringbuffer_send_offset << PAGE_SHIFT,
|
|
|
|
&newchannel->ringbuffer_gpadlhandle);
|
2020-09-16 11:48:08 +08:00
|
|
|
if (err)
|
|
|
|
goto error_clean_ring;
|
|
|
|
|
|
|
|
/* Create and init the channel open message */
|
2020-12-09 15:08:22 +08:00
|
|
|
open_info = kzalloc(sizeof(*open_info) +
|
2020-09-16 11:48:08 +08:00
|
|
|
sizeof(struct vmbus_channel_open_channel),
|
|
|
|
GFP_KERNEL);
|
|
|
|
if (!open_info) {
|
|
|
|
err = -ENOMEM;
|
|
|
|
goto error_free_gpadl;
|
|
|
|
}
|
|
|
|
|
|
|
|
init_completion(&open_info->waitevent);
|
|
|
|
open_info->waiting_channel = newchannel;
|
|
|
|
|
|
|
|
open_msg = (struct vmbus_channel_open_channel *)open_info->msg;
|
|
|
|
open_msg->header.msgtype = CHANNELMSG_OPENCHANNEL;
|
|
|
|
open_msg->openid = newchannel->offermsg.child_relid;
|
|
|
|
open_msg->child_relid = newchannel->offermsg.child_relid;
|
|
|
|
open_msg->ringbuffer_gpadlhandle = newchannel->ringbuffer_gpadlhandle;
|
2020-09-16 11:48:09 +08:00
|
|
|
/*
|
|
|
|
* The unit of ->downstream_ringbuffer_pageoffset is HV_HYP_PAGE and
|
|
|
|
* the unit of ->ringbuffer_send_offset (i.e. send_pages) is PAGE, so
|
|
|
|
* here we calculate it into HV_HYP_PAGE.
|
|
|
|
*/
|
|
|
|
open_msg->downstream_ringbuffer_pageoffset =
|
|
|
|
hv_ring_gpadl_send_hvpgoffset(send_pages << PAGE_SHIFT);
|
2020-09-16 11:48:08 +08:00
|
|
|
open_msg->target_vp = hv_cpu_number_to_vp_number(newchannel->target_cpu);
|
|
|
|
|
|
|
|
if (userdatalen)
|
|
|
|
memcpy(open_msg->userdata, userdata, userdatalen);
|
|
|
|
|
|
|
|
spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
|
|
|
|
list_add_tail(&open_info->msglistentry,
|
|
|
|
&vmbus_connection.chn_msg_list);
|
|
|
|
spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
|
|
|
|
|
|
|
|
if (newchannel->rescind) {
|
|
|
|
err = -ENODEV;
|
2021-04-13 18:50:04 +08:00
|
|
|
goto error_clean_msglist;
|
2020-09-16 11:48:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
err = vmbus_post_msg(open_msg,
|
|
|
|
sizeof(struct vmbus_channel_open_channel), true);
|
|
|
|
|
|
|
|
trace_vmbus_open(open_msg, err);
|
|
|
|
|
|
|
|
if (err != 0)
|
|
|
|
goto error_clean_msglist;
|
|
|
|
|
|
|
|
wait_for_completion(&open_info->waitevent);
|
|
|
|
|
|
|
|
spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
|
|
|
|
list_del(&open_info->msglistentry);
|
|
|
|
spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
|
|
|
|
|
|
|
|
if (newchannel->rescind) {
|
|
|
|
err = -ENODEV;
|
|
|
|
goto error_free_info;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (open_info->response.open_result.status) {
|
|
|
|
err = -EAGAIN;
|
|
|
|
goto error_free_info;
|
|
|
|
}
|
|
|
|
|
|
|
|
newchannel->state = CHANNEL_OPENED_STATE;
|
|
|
|
kfree(open_info);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
error_clean_msglist:
|
|
|
|
spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
|
|
|
|
list_del(&open_info->msglistentry);
|
|
|
|
spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
|
|
|
|
error_free_info:
|
|
|
|
kfree(open_info);
|
|
|
|
error_free_gpadl:
|
|
|
|
vmbus_teardown_gpadl(newchannel, newchannel->ringbuffer_gpadlhandle);
|
|
|
|
newchannel->ringbuffer_gpadlhandle = 0;
|
|
|
|
error_clean_ring:
|
|
|
|
hv_ringbuffer_cleanup(&newchannel->outbound);
|
|
|
|
hv_ringbuffer_cleanup(&newchannel->inbound);
|
2020-11-09 18:04:00 +08:00
|
|
|
vmbus_free_requestor(&newchannel->requestor);
|
2020-09-16 11:48:08 +08:00
|
|
|
newchannel->state = CHANNEL_OPEN_STATE;
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* vmbus_connect_ring - Open the channel but reuse ring buffer
|
|
|
|
*/
|
|
|
|
int vmbus_connect_ring(struct vmbus_channel *newchannel,
|
|
|
|
void (*onchannelcallback)(void *context), void *context)
|
|
|
|
{
|
|
|
|
return __vmbus_open(newchannel, NULL, 0, onchannelcallback, context);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(vmbus_connect_ring);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = vmbus_alloc_ring(newchannel, send_ringbuffer_size,
|
|
|
|
recv_ringbuffer_size);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
err = __vmbus_open(newchannel, userdata, userdatalen,
|
|
|
|
onchannelcallback, context);
|
|
|
|
if (err)
|
|
|
|
vmbus_free_ring(newchannel);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(vmbus_open);
|
|
|
|
|
2010-03-05 06:11:00 +08:00
|
|
|
/*
|
2010-10-08 02:40:08 +08:00
|
|
|
* vmbus_teardown_gpadl -Teardown the specified GPADL handle
|
2009-09-02 08:24:57 +08:00
|
|
|
*/
|
2010-10-08 02:40:08 +08:00
|
|
|
int vmbus_teardown_gpadl(struct vmbus_channel *channel, u32 gpadl_handle)
|
2009-07-14 07:02:34 +08:00
|
|
|
{
|
2009-08-27 06:16:04 +08:00
|
|
|
struct vmbus_channel_gpadl_teardown *msg;
|
2009-08-19 06:21:19 +08:00
|
|
|
struct vmbus_channel_msginfo *info;
|
2009-07-16 05:56:45 +08:00
|
|
|
unsigned long flags;
|
2014-08-28 07:25:32 +08:00
|
|
|
int ret;
|
2009-07-14 07:02:34 +08:00
|
|
|
|
2020-12-09 15:08:22 +08:00
|
|
|
info = kzalloc(sizeof(*info) +
|
2009-09-02 08:24:57 +08:00
|
|
|
sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL);
|
2010-05-06 03:27:34 +08:00
|
|
|
if (!info)
|
|
|
|
return -ENOMEM;
|
2009-07-14 07:02:34 +08:00
|
|
|
|
2011-05-10 22:55:39 +08:00
|
|
|
init_completion(&info->waitevent);
|
2016-12-23 08:54:00 +08:00
|
|
|
info->waiting_channel = channel;
|
2009-07-14 07:02:34 +08:00
|
|
|
|
2010-11-09 06:04:38 +08:00
|
|
|
msg = (struct vmbus_channel_gpadl_teardown *)info->msg;
|
2009-07-14 07:02:34 +08:00
|
|
|
|
2010-11-09 06:04:38 +08:00
|
|
|
msg->header.msgtype = CHANNELMSG_GPADL_TEARDOWN;
|
|
|
|
msg->child_relid = channel->offermsg.child_relid;
|
|
|
|
msg->gpadl = gpadl_handle;
|
2009-07-14 07:02:34 +08:00
|
|
|
|
2011-01-27 04:12:07 +08:00
|
|
|
spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
|
2010-11-09 06:04:38 +08:00
|
|
|
list_add_tail(&info->msglistentry,
|
2011-01-27 04:12:14 +08:00
|
|
|
&vmbus_connection.chn_msg_list);
|
2011-01-27 04:12:07 +08:00
|
|
|
spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
|
2017-08-12 01:03:59 +08:00
|
|
|
|
|
|
|
if (channel->rescind)
|
|
|
|
goto post_msg_err;
|
|
|
|
|
2016-12-07 17:16:24 +08:00
|
|
|
ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_gpadl_teardown),
|
|
|
|
true);
|
2009-07-14 07:02:34 +08:00
|
|
|
|
2017-10-30 03:21:12 +08:00
|
|
|
trace_vmbus_teardown_gpadl(msg, ret);
|
|
|
|
|
2014-08-28 07:25:32 +08:00
|
|
|
if (ret)
|
|
|
|
goto post_msg_err;
|
|
|
|
|
|
|
|
wait_for_completion(&info->waitevent);
|
2009-07-14 07:02:34 +08:00
|
|
|
|
2014-08-28 07:25:32 +08:00
|
|
|
post_msg_err:
|
2017-03-13 11:00:30 +08:00
|
|
|
/*
|
|
|
|
* If the channel has been rescinded;
|
|
|
|
* we will be awakened by the rescind
|
|
|
|
* handler; set the error code to zero so we don't leak memory.
|
|
|
|
*/
|
|
|
|
if (channel->rescind)
|
|
|
|
ret = 0;
|
|
|
|
|
2011-01-27 04:12:07 +08:00
|
|
|
spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
|
2010-11-09 06:04:38 +08:00
|
|
|
list_del(&info->msglistentry);
|
2011-01-27 04:12:07 +08:00
|
|
|
spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
|
2009-07-14 07:02:34 +08:00
|
|
|
|
2009-07-16 03:48:29 +08:00
|
|
|
kfree(info);
|
2009-07-14 07:02:34 +08:00
|
|
|
return ret;
|
|
|
|
}
|
2010-10-21 23:39:59 +08:00
|
|
|
EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl);
|
2009-07-14 07:02:34 +08:00
|
|
|
|
2018-08-02 11:08:23 +08:00
|
|
|
void vmbus_reset_channel_cb(struct vmbus_channel *channel)
|
2009-07-14 07:02:34 +08:00
|
|
|
{
|
2020-04-06 08:15:09 +08:00
|
|
|
unsigned long flags;
|
|
|
|
|
2015-12-15 08:01:47 +08:00
|
|
|
/*
|
2017-03-05 09:13:58 +08:00
|
|
|
* vmbus_on_event(), running in the per-channel tasklet, can race
|
2015-12-15 08:01:47 +08:00
|
|
|
* with vmbus_close_internal() in the case of SMP guest, e.g., when
|
|
|
|
* the former is accessing channel->inbound.ring_buffer, the latter
|
2017-03-05 09:13:58 +08:00
|
|
|
* could be freeing the ring_buffer pages, so here we must stop it
|
|
|
|
* first.
|
2020-04-06 08:15:07 +08:00
|
|
|
*
|
|
|
|
* vmbus_chan_sched() might call the netvsc driver callback function
|
|
|
|
* that ends up scheduling NAPI work that accesses the ring buffer.
|
|
|
|
* At this point, we have to ensure that any such work is completed
|
|
|
|
* and that the channel ring buffer is no longer being accessed, cf.
|
|
|
|
* the calls to napi_disable() in netvsc_device_remove().
|
2015-12-15 08:01:47 +08:00
|
|
|
*/
|
2017-03-05 09:13:58 +08:00
|
|
|
tasklet_disable(&channel->callback_event);
|
2015-12-15 08:01:47 +08:00
|
|
|
|
2020-04-06 08:15:09 +08:00
|
|
|
/* See the inline comments in vmbus_chan_sched(). */
|
|
|
|
spin_lock_irqsave(&channel->sched_lock, flags);
|
|
|
|
channel->onchannel_callback = NULL;
|
|
|
|
spin_unlock_irqrestore(&channel->sched_lock, flags);
|
2018-08-02 11:08:23 +08:00
|
|
|
|
2020-04-06 08:15:09 +08:00
|
|
|
channel->sc_creation_callback = NULL;
|
2018-08-02 11:08:23 +08:00
|
|
|
|
|
|
|
/* Re-enable tasklet for use on re-open */
|
|
|
|
tasklet_enable(&channel->callback_event);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int vmbus_close_internal(struct vmbus_channel *channel)
|
|
|
|
{
|
|
|
|
struct vmbus_channel_close_channel *msg;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
vmbus_reset_channel_cb(channel);
|
|
|
|
|
2015-12-15 08:01:48 +08:00
|
|
|
/*
|
|
|
|
* In case a device driver's probe() fails (e.g.,
|
|
|
|
* util_probe() -> vmbus_open() returns -ENOMEM) and the device is
|
2017-03-10 08:16:31 +08:00
|
|
|
* rescinded later (e.g., we dynamically disable an Integrated Service
|
2015-12-15 08:01:48 +08:00
|
|
|
* in Hyper-V Manager), the driver's remove() invokes vmbus_close():
|
|
|
|
* here we should skip most of the below cleanup work.
|
|
|
|
*/
|
2018-09-15 00:10:17 +08:00
|
|
|
if (channel->state != CHANNEL_OPENED_STATE)
|
|
|
|
return -EINVAL;
|
2015-12-15 08:01:48 +08:00
|
|
|
|
2013-05-24 03:02:32 +08:00
|
|
|
channel->state = CHANNEL_OPEN_STATE;
|
2009-07-14 07:02:34 +08:00
|
|
|
|
2009-07-28 04:47:24 +08:00
|
|
|
/* Send a closing message */
|
2009-07-14 07:02:34 +08:00
|
|
|
|
2011-06-07 06:49:59 +08:00
|
|
|
msg = &channel->close_msg.msg;
|
2009-07-14 07:02:34 +08:00
|
|
|
|
2010-11-09 06:04:38 +08:00
|
|
|
msg->header.msgtype = CHANNELMSG_CLOSECHANNEL;
|
|
|
|
msg->child_relid = channel->offermsg.child_relid;
|
2009-07-14 07:02:34 +08:00
|
|
|
|
2016-12-07 17:16:24 +08:00
|
|
|
ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_close_channel),
|
|
|
|
true);
|
2009-07-14 07:02:34 +08:00
|
|
|
|
2017-10-30 03:21:10 +08:00
|
|
|
trace_vmbus_close_internal(msg, ret);
|
|
|
|
|
2014-08-28 07:25:33 +08:00
|
|
|
if (ret) {
|
|
|
|
pr_err("Close failed: close post msg return is %d\n", ret);
|
|
|
|
/*
|
|
|
|
* If we failed to post the close msg,
|
|
|
|
* it is perhaps better to leak memory.
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
2009-07-28 04:47:24 +08:00
|
|
|
/* Tear down the gpadl for the channel's ring buffer */
|
2018-09-15 00:10:17 +08:00
|
|
|
else if (channel->ringbuffer_gpadlhandle) {
|
2014-08-28 07:25:33 +08:00
|
|
|
ret = vmbus_teardown_gpadl(channel,
|
|
|
|
channel->ringbuffer_gpadlhandle);
|
|
|
|
if (ret) {
|
|
|
|
pr_err("Close failed: teardown gpadl return %d\n", ret);
|
|
|
|
/*
|
|
|
|
* If we failed to teardown gpadl,
|
|
|
|
* it is perhaps better to leak memory.
|
|
|
|
*/
|
|
|
|
}
|
2009-07-14 07:02:34 +08:00
|
|
|
|
2018-09-15 00:10:17 +08:00
|
|
|
channel->ringbuffer_gpadlhandle = 0;
|
|
|
|
}
|
2009-07-14 07:02:34 +08:00
|
|
|
|
2020-11-09 18:04:00 +08:00
|
|
|
if (!ret)
|
|
|
|
vmbus_free_requestor(&channel->requestor);
|
|
|
|
|
2014-08-28 07:25:33 +08:00
|
|
|
return ret;
|
2009-07-14 07:02:34 +08:00
|
|
|
}
|
2013-05-24 03:02:32 +08:00
|
|
|
|
2018-09-15 00:10:17 +08:00
|
|
|
/* disconnect ring - close all channels */
|
|
|
|
int vmbus_disconnect_ring(struct vmbus_channel *channel)
|
2013-05-24 03:02:32 +08:00
|
|
|
{
|
2018-09-15 00:10:17 +08:00
|
|
|
struct vmbus_channel *cur_channel, *tmp;
|
|
|
|
int ret;
|
2013-05-24 03:02:32 +08:00
|
|
|
|
2018-09-15 00:10:17 +08:00
|
|
|
if (channel->primary_channel != NULL)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2019-01-10 04:56:06 +08:00
|
|
|
list_for_each_entry_safe(cur_channel, tmp, &channel->sc_list, sc_list) {
|
2018-09-15 00:10:17 +08:00
|
|
|
if (cur_channel->rescind)
|
2017-11-14 21:53:33 +08:00
|
|
|
wait_for_completion(&cur_channel->rescind_event);
|
2018-09-15 00:10:17 +08:00
|
|
|
|
|
|
|
mutex_lock(&vmbus_connection.channel_mutex);
|
|
|
|
if (vmbus_close_internal(cur_channel) == 0) {
|
|
|
|
vmbus_free_ring(cur_channel);
|
|
|
|
|
|
|
|
if (cur_channel->rescind)
|
|
|
|
hv_process_channel_removal(cur_channel);
|
2017-05-01 07:21:18 +08:00
|
|
|
}
|
2017-11-14 21:53:33 +08:00
|
|
|
mutex_unlock(&vmbus_connection.channel_mutex);
|
2013-05-24 03:02:32 +08:00
|
|
|
}
|
2018-09-15 00:10:17 +08:00
|
|
|
|
2013-05-24 03:02:32 +08:00
|
|
|
/*
|
|
|
|
* Now close the primary.
|
|
|
|
*/
|
2017-11-14 21:53:33 +08:00
|
|
|
mutex_lock(&vmbus_connection.channel_mutex);
|
2018-09-15 00:10:17 +08:00
|
|
|
ret = vmbus_close_internal(channel);
|
2017-09-30 12:09:36 +08:00
|
|
|
mutex_unlock(&vmbus_connection.channel_mutex);
|
2018-09-15 00:10:17 +08:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(vmbus_disconnect_ring);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* vmbus_close - Close the specified channel
|
|
|
|
*/
|
|
|
|
void vmbus_close(struct vmbus_channel *channel)
|
|
|
|
{
|
|
|
|
if (vmbus_disconnect_ring(channel) == 0)
|
|
|
|
vmbus_free_ring(channel);
|
2013-05-24 03:02:32 +08:00
|
|
|
}
|
2010-10-22 00:52:22 +08:00
|
|
|
EXPORT_SYMBOL_GPL(vmbus_close);
|
2009-07-14 07:02:34 +08:00
|
|
|
|
2017-08-16 23:56:26 +08:00
|
|
|
/**
|
|
|
|
* vmbus_sendpacket() - Send the specified buffer on the given channel
|
2018-09-24 05:10:41 +08:00
|
|
|
* @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.
|
2017-08-16 23:56:26 +08:00
|
|
|
* @requestid: Identifier of the request
|
2018-09-24 05:10:41 +08:00
|
|
|
* @type: Type of packet that is being sent e.g. negotiate, time
|
|
|
|
* packet etc.
|
|
|
|
* @flags: 0 or VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
|
2017-08-16 23:56:26 +08:00
|
|
|
*
|
2018-09-24 05:10:41 +08:00
|
|
|
* Sends data in @buffer directly to Hyper-V via the vmbus.
|
|
|
|
* This will send the data unparsed to Hyper-V.
|
2017-08-16 23:56:26 +08:00
|
|
|
*
|
|
|
|
* 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)
|
2009-07-14 07:02:34 +08:00
|
|
|
{
|
2009-08-28 07:02:36 +08:00
|
|
|
struct vmpacket_descriptor desc;
|
2010-10-01 01:52:13 +08:00
|
|
|
u32 packetlen = sizeof(struct vmpacket_descriptor) + bufferlen;
|
2011-01-20 16:32:01 +08:00
|
|
|
u32 packetlen_aligned = ALIGN(packetlen, sizeof(u64));
|
2014-02-02 11:02:20 +08:00
|
|
|
struct kvec bufferlist[3];
|
2010-10-01 01:52:13 +08:00
|
|
|
u64 aligned_data = 0;
|
2015-08-02 07:08:14 +08:00
|
|
|
int num_vecs = ((bufferlen != 0) ? 3 : 1);
|
2009-07-14 07:02:34 +08:00
|
|
|
|
|
|
|
|
2009-07-28 04:47:24 +08:00
|
|
|
/* Setup the descriptor */
|
2011-01-27 04:12:13 +08:00
|
|
|
desc.type = type; /* VmbusPacketTypeDataInBand; */
|
|
|
|
desc.flags = flags; /* VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED; */
|
2009-09-02 08:24:57 +08:00
|
|
|
/* in 8-bytes granularity */
|
2011-01-27 04:12:13 +08:00
|
|
|
desc.offset8 = sizeof(struct vmpacket_descriptor) >> 3;
|
|
|
|
desc.len8 = (u16)(packetlen_aligned >> 3);
|
2020-11-09 18:04:00 +08:00
|
|
|
desc.trans_id = VMBUS_RQST_ERROR; /* will be updated in hv_ringbuffer_write() */
|
2009-07-14 07:02:34 +08:00
|
|
|
|
2014-02-02 11:02:20 +08:00
|
|
|
bufferlist[0].iov_base = &desc;
|
|
|
|
bufferlist[0].iov_len = sizeof(struct vmpacket_descriptor);
|
|
|
|
bufferlist[1].iov_base = buffer;
|
|
|
|
bufferlist[1].iov_len = bufferlen;
|
|
|
|
bufferlist[2].iov_base = &aligned_data;
|
|
|
|
bufferlist[2].iov_len = (packetlen_aligned - packetlen);
|
2009-07-14 07:02:34 +08:00
|
|
|
|
2020-11-09 18:04:00 +08:00
|
|
|
return hv_ringbuffer_write(channel, bufferlist, num_vecs, requestid);
|
2009-07-14 07:02:34 +08:00
|
|
|
}
|
2010-10-08 02:40:08 +08:00
|
|
|
EXPORT_SYMBOL(vmbus_sendpacket);
|
2009-07-14 07:02:34 +08:00
|
|
|
|
2010-03-05 06:11:00 +08:00
|
|
|
/*
|
2017-08-16 23:56:25 +08:00
|
|
|
* vmbus_sendpacket_pagebuffer - Send a range of single-page buffer
|
2015-03-01 03:39:03 +08:00
|
|
|
* 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.
|
2009-09-02 08:24:57 +08:00
|
|
|
*/
|
2017-08-16 23:56:25 +08:00
|
|
|
int vmbus_sendpacket_pagebuffer(struct vmbus_channel *channel,
|
|
|
|
struct hv_page_buffer pagebuffers[],
|
|
|
|
u32 pagecount, void *buffer, u32 bufferlen,
|
|
|
|
u64 requestid)
|
2009-07-14 07:02:34 +08:00
|
|
|
{
|
2009-09-02 08:24:57 +08:00
|
|
|
int i;
|
2010-09-21 05:07:51 +08:00
|
|
|
struct vmbus_channel_packet_page_buffer desc;
|
2010-10-01 01:52:13 +08:00
|
|
|
u32 descsize;
|
|
|
|
u32 packetlen;
|
|
|
|
u32 packetlen_aligned;
|
2014-02-02 11:02:20 +08:00
|
|
|
struct kvec bufferlist[3];
|
2010-10-01 01:52:13 +08:00
|
|
|
u64 aligned_data = 0;
|
2009-07-14 07:02:34 +08:00
|
|
|
|
2010-10-01 01:52:13 +08:00
|
|
|
if (pagecount > MAX_PAGE_BUFFER_COUNT)
|
2010-05-06 03:27:39 +08:00
|
|
|
return -EINVAL;
|
2009-07-14 07:02:34 +08:00
|
|
|
|
2009-09-02 08:24:57 +08:00
|
|
|
/*
|
2010-09-21 05:07:51 +08:00
|
|
|
* Adjust the size down since vmbus_channel_packet_page_buffer is the
|
2009-09-02 08:24:57 +08:00
|
|
|
* largest size we support
|
|
|
|
*/
|
2010-10-01 01:52:13 +08:00
|
|
|
descsize = sizeof(struct vmbus_channel_packet_page_buffer) -
|
|
|
|
((MAX_PAGE_BUFFER_COUNT - pagecount) *
|
2009-09-02 08:24:57 +08:00
|
|
|
sizeof(struct hv_page_buffer));
|
2010-10-01 01:52:13 +08:00
|
|
|
packetlen = descsize + bufferlen;
|
2011-01-20 16:32:01 +08:00
|
|
|
packetlen_aligned = ALIGN(packetlen, sizeof(u64));
|
2009-07-14 07:02:34 +08:00
|
|
|
|
2009-07-28 04:47:24 +08:00
|
|
|
/* Setup the descriptor */
|
2011-01-27 04:12:13 +08:00
|
|
|
desc.type = VM_PKT_DATA_USING_GPA_DIRECT;
|
2017-08-16 23:56:25 +08:00
|
|
|
desc.flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
|
2017-03-05 09:27:14 +08:00
|
|
|
desc.dataoffset8 = descsize >> 3; /* in 8-bytes granularity */
|
2010-10-01 01:52:13 +08:00
|
|
|
desc.length8 = (u16)(packetlen_aligned >> 3);
|
2020-11-09 18:04:00 +08:00
|
|
|
desc.transactionid = VMBUS_RQST_ERROR; /* will be updated in hv_ringbuffer_write() */
|
2017-10-06 08:35:05 +08:00
|
|
|
desc.reserved = 0;
|
2010-10-01 01:52:13 +08:00
|
|
|
desc.rangecount = pagecount;
|
|
|
|
|
|
|
|
for (i = 0; i < pagecount; i++) {
|
2011-01-27 04:12:11 +08:00
|
|
|
desc.range[i].len = pagebuffers[i].len;
|
|
|
|
desc.range[i].offset = pagebuffers[i].offset;
|
|
|
|
desc.range[i].pfn = pagebuffers[i].pfn;
|
2009-07-14 07:02:34 +08:00
|
|
|
}
|
|
|
|
|
2014-02-02 11:02:20 +08:00
|
|
|
bufferlist[0].iov_base = &desc;
|
|
|
|
bufferlist[0].iov_len = descsize;
|
|
|
|
bufferlist[1].iov_base = buffer;
|
|
|
|
bufferlist[1].iov_len = bufferlen;
|
|
|
|
bufferlist[2].iov_base = &aligned_data;
|
|
|
|
bufferlist[2].iov_len = (packetlen_aligned - packetlen);
|
2009-07-14 07:02:34 +08:00
|
|
|
|
2020-11-09 18:04:00 +08:00
|
|
|
return hv_ringbuffer_write(channel, bufferlist, 3, requestid);
|
2009-07-14 07:02:34 +08:00
|
|
|
}
|
2010-10-22 00:29:54 +08:00
|
|
|
EXPORT_SYMBOL_GPL(vmbus_sendpacket_pagebuffer);
|
2009-07-14 07:02:34 +08:00
|
|
|
|
2015-01-10 15:54:34 +08:00
|
|
|
/*
|
|
|
|
* 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)
|
|
|
|
{
|
|
|
|
u32 packetlen;
|
|
|
|
u32 packetlen_aligned;
|
|
|
|
struct kvec bufferlist[3];
|
|
|
|
u64 aligned_data = 0;
|
|
|
|
|
|
|
|
packetlen = desc_size + bufferlen;
|
|
|
|
packetlen_aligned = ALIGN(packetlen, sizeof(u64));
|
|
|
|
|
|
|
|
/* Setup the descriptor */
|
|
|
|
desc->type = VM_PKT_DATA_USING_GPA_DIRECT;
|
|
|
|
desc->flags = VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED;
|
2017-03-05 09:27:14 +08:00
|
|
|
desc->dataoffset8 = desc_size >> 3; /* in 8-bytes granularity */
|
2015-01-10 15:54:34 +08:00
|
|
|
desc->length8 = (u16)(packetlen_aligned >> 3);
|
2020-11-09 18:04:00 +08:00
|
|
|
desc->transactionid = VMBUS_RQST_ERROR; /* will be updated in hv_ringbuffer_write() */
|
2017-10-06 08:35:05 +08:00
|
|
|
desc->reserved = 0;
|
2015-01-10 15:54:34 +08:00
|
|
|
desc->rangecount = 1;
|
|
|
|
|
|
|
|
bufferlist[0].iov_base = desc;
|
|
|
|
bufferlist[0].iov_len = desc_size;
|
|
|
|
bufferlist[1].iov_base = buffer;
|
|
|
|
bufferlist[1].iov_len = bufferlen;
|
|
|
|
bufferlist[2].iov_base = &aligned_data;
|
|
|
|
bufferlist[2].iov_len = (packetlen_aligned - packetlen);
|
|
|
|
|
2020-11-09 18:04:00 +08:00
|
|
|
return hv_ringbuffer_write(channel, bufferlist, 3, requestid);
|
2015-01-10 15:54:34 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(vmbus_sendpacket_mpb_desc);
|
|
|
|
|
2010-05-05 06:55:05 +08:00
|
|
|
/**
|
2018-09-24 05:10:41 +08:00
|
|
|
* __vmbus_recvpacket() - Retrieve the user packet on the specified channel
|
|
|
|
* @channel: Pointer to vmbus_channel structure
|
2010-10-01 01:52:13 +08:00
|
|
|
* @buffer: Pointer to the buffer you want to receive the data into.
|
2018-09-24 05:10:41 +08:00
|
|
|
* @bufferlen: Maximum size of what the buffer can hold.
|
|
|
|
* @buffer_actual_len: The actual size of the data after it was received.
|
2010-10-01 01:52:13 +08:00
|
|
|
* @requestid: Identifier of the request
|
2018-09-24 05:10:41 +08:00
|
|
|
* @raw: true means keep the vmpacket_descriptor header in the received data.
|
2010-05-05 06:55:05 +08:00
|
|
|
*
|
|
|
|
* 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.
|
2009-09-02 08:24:57 +08:00
|
|
|
*/
|
2015-12-15 11:02:00 +08:00
|
|
|
static inline int
|
|
|
|
__vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
|
|
|
|
u32 bufferlen, u32 *buffer_actual_len, u64 *requestid,
|
|
|
|
bool raw)
|
2009-07-14 07:02:34 +08:00
|
|
|
{
|
2016-11-07 05:14:18 +08:00
|
|
|
return hv_ringbuffer_read(channel, buffer, bufferlen,
|
|
|
|
buffer_actual_len, requestid, raw);
|
2009-07-14 07:02:34 +08:00
|
|
|
|
2015-12-15 11:02:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int vmbus_recvpacket(struct vmbus_channel *channel, void *buffer,
|
|
|
|
u32 bufferlen, u32 *buffer_actual_len,
|
|
|
|
u64 *requestid)
|
|
|
|
{
|
|
|
|
return __vmbus_recvpacket(channel, buffer, bufferlen,
|
|
|
|
buffer_actual_len, requestid, false);
|
2009-07-14 07:02:34 +08:00
|
|
|
}
|
2010-10-08 02:40:08 +08:00
|
|
|
EXPORT_SYMBOL(vmbus_recvpacket);
|
2009-07-14 07:02:34 +08:00
|
|
|
|
2010-03-05 06:11:00 +08:00
|
|
|
/*
|
2010-10-08 02:40:08 +08:00
|
|
|
* vmbus_recvpacket_raw - Retrieve the raw packet on the specified channel
|
2009-09-02 08:24:57 +08:00
|
|
|
*/
|
2010-10-08 02:40:08 +08:00
|
|
|
int vmbus_recvpacket_raw(struct vmbus_channel *channel, void *buffer,
|
2010-10-01 01:52:13 +08:00
|
|
|
u32 bufferlen, u32 *buffer_actual_len,
|
|
|
|
u64 *requestid)
|
2009-07-14 07:02:34 +08:00
|
|
|
{
|
2015-12-15 11:02:00 +08:00
|
|
|
return __vmbus_recvpacket(channel, buffer, bufferlen,
|
|
|
|
buffer_actual_len, requestid, true);
|
2009-07-14 07:02:34 +08:00
|
|
|
}
|
2010-10-22 00:09:23 +08:00
|
|
|
EXPORT_SYMBOL_GPL(vmbus_recvpacket_raw);
|
2020-11-09 18:04:00 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
2021-05-11 05:08:41 +08:00
|
|
|
* @channel: Pointer to the VMbus channel struct
|
2020-11-09 18:04:00 +08:00
|
|
|
* @rqst_add: Guest memory address to be stored in the array
|
|
|
|
*/
|
2021-05-11 05:08:41 +08:00
|
|
|
u64 vmbus_next_request_id(struct vmbus_channel *channel, u64 rqst_addr)
|
2020-11-09 18:04:00 +08:00
|
|
|
{
|
2021-05-11 05:08:41 +08:00
|
|
|
struct vmbus_requestor *rqstor = &channel->requestor;
|
2020-11-09 18:04:00 +08:00
|
|
|
unsigned long flags;
|
|
|
|
u64 current_id;
|
|
|
|
|
|
|
|
/* Check rqstor has been initialized */
|
|
|
|
if (!channel->rqstor_size)
|
|
|
|
return VMBUS_NO_RQSTOR;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&rqstor->req_lock, flags);
|
|
|
|
current_id = rqstor->next_request_id;
|
|
|
|
|
|
|
|
/* Requestor array is full */
|
|
|
|
if (current_id >= rqstor->size) {
|
|
|
|
spin_unlock_irqrestore(&rqstor->req_lock, flags);
|
|
|
|
return VMBUS_RQST_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
rqstor->next_request_id = rqstor->req_arr[current_id];
|
|
|
|
rqstor->req_arr[current_id] = rqst_addr;
|
|
|
|
|
|
|
|
/* The already held spin lock provides atomicity */
|
|
|
|
bitmap_set(rqstor->req_bitmap, current_id, 1);
|
|
|
|
|
|
|
|
spin_unlock_irqrestore(&rqstor->req_lock, flags);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Cannot return an ID of 0, which is reserved for an unsolicited
|
|
|
|
* message from Hyper-V.
|
|
|
|
*/
|
|
|
|
return current_id + 1;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(vmbus_next_request_id);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* vmbus_request_addr - Returns the memory address stored at @trans_id
|
|
|
|
* in @rqstor. Uses a spin lock to avoid race conditions.
|
2021-05-11 05:08:41 +08:00
|
|
|
* @channel: Pointer to the VMbus channel struct
|
2020-11-09 18:04:00 +08:00
|
|
|
* @trans_id: Request id sent back from Hyper-V. Becomes the requestor's
|
|
|
|
* next request id.
|
|
|
|
*/
|
2021-05-11 05:08:41 +08:00
|
|
|
u64 vmbus_request_addr(struct vmbus_channel *channel, u64 trans_id)
|
2020-11-09 18:04:00 +08:00
|
|
|
{
|
2021-05-11 05:08:41 +08:00
|
|
|
struct vmbus_requestor *rqstor = &channel->requestor;
|
2020-11-09 18:04:00 +08:00
|
|
|
unsigned long flags;
|
|
|
|
u64 req_addr;
|
|
|
|
|
|
|
|
/* Check rqstor has been initialized */
|
|
|
|
if (!channel->rqstor_size)
|
|
|
|
return VMBUS_NO_RQSTOR;
|
|
|
|
|
|
|
|
/* Hyper-V can send an unsolicited message with ID of 0 */
|
|
|
|
if (!trans_id)
|
|
|
|
return trans_id;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&rqstor->req_lock, flags);
|
|
|
|
|
|
|
|
/* Data corresponding to trans_id is stored at trans_id - 1 */
|
|
|
|
trans_id--;
|
|
|
|
|
|
|
|
/* Invalid trans_id */
|
|
|
|
if (trans_id >= rqstor->size || !test_bit(trans_id, rqstor->req_bitmap)) {
|
|
|
|
spin_unlock_irqrestore(&rqstor->req_lock, flags);
|
|
|
|
return VMBUS_RQST_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
req_addr = rqstor->req_arr[trans_id];
|
|
|
|
rqstor->req_arr[trans_id] = rqstor->next_request_id;
|
|
|
|
rqstor->next_request_id = trans_id;
|
|
|
|
|
|
|
|
/* The already held spin lock provides atomicity */
|
|
|
|
bitmap_clear(rqstor->req_bitmap, trans_id, 1);
|
|
|
|
|
|
|
|
spin_unlock_irqrestore(&rqstor->req_lock, flags);
|
|
|
|
return req_addr;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(vmbus_request_addr);
|