Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * An implementation of file copy service.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2014, Microsoft, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Author : K. Y. Srinivasan <ksrinivasan@novell.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/nls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/hyperv.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <asm/hyperv-tlfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include "hyperv_vmbus.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include "hv_utils_transport.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #define WIN8_SRV_MAJOR		1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #define WIN8_SRV_MINOR		1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #define WIN8_SRV_VERSION	(WIN8_SRV_MAJOR << 16 | WIN8_SRV_MINOR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #define FCOPY_VER_COUNT 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) static const int fcopy_versions[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	WIN8_SRV_VERSION
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #define FW_VER_COUNT 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) static const int fw_versions[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	UTIL_FW_VERSION
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36)  * Global state maintained for transaction that is being processed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)  * For a class of integration services, including the "file copy service",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38)  * the specified protocol is a "request/response" protocol which means that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39)  * there can only be single outstanding transaction from the host at any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40)  * given point in time. We use this to simplify memory management in this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41)  * driver - we cache and process only one message at a time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43)  * While the request/response protocol is guaranteed by the host, we further
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44)  * ensure this by serializing packet processing in this driver - we do not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45)  * read additional packets from the VMBUs until the current packet is fully
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46)  * handled.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) static struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	int state;   /* hvutil_device_state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	int recv_len; /* number of bytes received. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	struct hv_fcopy_hdr  *fcopy_msg; /* current message */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	struct vmbus_channel *recv_channel; /* chn we got the request */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	u64 recv_req_id; /* request ID. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) } fcopy_transaction;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) static void fcopy_respond_to_host(int error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) static void fcopy_send_data(struct work_struct *dummy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) static void fcopy_timeout_func(struct work_struct *dummy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) static DECLARE_DELAYED_WORK(fcopy_timeout_work, fcopy_timeout_func);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) static DECLARE_WORK(fcopy_send_work, fcopy_send_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) static const char fcopy_devname[] = "vmbus/hv_fcopy";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) static u8 *recv_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) static struct hvutil_transport *hvt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66)  * This state maintains the version number registered by the daemon.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) static int dm_reg_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) static void fcopy_poll_wrapper(void *channel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	/* Transaction is finished, reset the state here to avoid races. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	fcopy_transaction.state = HVUTIL_READY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	tasklet_schedule(&((struct vmbus_channel *)channel)->callback_event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) static void fcopy_timeout_func(struct work_struct *dummy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	 * If the timer fires, the user-mode component has not responded;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	 * process the pending transaction.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	fcopy_respond_to_host(HV_E_FAIL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	hv_poll_channel(fcopy_transaction.recv_channel, fcopy_poll_wrapper);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) static void fcopy_register_done(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	pr_debug("FCP: userspace daemon registered\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	hv_poll_channel(fcopy_transaction.recv_channel, fcopy_poll_wrapper);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) static int fcopy_handle_handshake(u32 version)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	u32 our_ver = FCOPY_CURRENT_VERSION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	switch (version) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	case FCOPY_VERSION_0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		/* Daemon doesn't expect us to reply */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		dm_reg_value = version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	case FCOPY_VERSION_1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		/* Daemon expects us to reply with our own version */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		if (hvutil_transport_send(hvt, &our_ver, sizeof(our_ver),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		    fcopy_register_done))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		dm_reg_value = version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		 * For now we will fail the registration.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		 * If and when we have multiple versions to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		 * deal with, we will be backward compatible.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		 * We will add this code when needed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	pr_debug("FCP: userspace daemon ver. %d connected\n", version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) static void fcopy_send_data(struct work_struct *dummy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	struct hv_start_fcopy *smsg_out = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	int operation = fcopy_transaction.fcopy_msg->operation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	struct hv_start_fcopy *smsg_in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	void *out_src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	int rc, out_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	 * The  strings sent from the host are encoded in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	 * in utf16; convert it to utf8 strings.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	 * The host assures us that the utf16 strings will not exceed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	 * the max lengths specified. We will however, reserve room
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	 * for the string terminating character - in the utf16s_utf8s()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	 * function we limit the size of the buffer where the converted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	 * string is placed to W_MAX_PATH -1 to guarantee
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	 * that the strings can be properly terminated!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	switch (operation) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	case START_FILE_COPY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		out_len = sizeof(struct hv_start_fcopy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		smsg_out = kzalloc(sizeof(*smsg_out), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		if (!smsg_out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		smsg_out->hdr.operation = operation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		smsg_in = (struct hv_start_fcopy *)fcopy_transaction.fcopy_msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		utf16s_to_utf8s((wchar_t *)smsg_in->file_name, W_MAX_PATH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 				UTF16_LITTLE_ENDIAN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 				(__u8 *)&smsg_out->file_name, W_MAX_PATH - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		utf16s_to_utf8s((wchar_t *)smsg_in->path_name, W_MAX_PATH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 				UTF16_LITTLE_ENDIAN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 				(__u8 *)&smsg_out->path_name, W_MAX_PATH - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		smsg_out->copy_flags = smsg_in->copy_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		smsg_out->file_size = smsg_in->file_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		out_src = smsg_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	case WRITE_TO_FILE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		out_src = fcopy_transaction.fcopy_msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 		out_len = sizeof(struct hv_do_fcopy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		out_src = fcopy_transaction.fcopy_msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		out_len = fcopy_transaction.recv_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	fcopy_transaction.state = HVUTIL_USERSPACE_REQ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	rc = hvutil_transport_send(hvt, out_src, out_len, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		pr_debug("FCP: failed to communicate to the daemon: %d\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		if (cancel_delayed_work_sync(&fcopy_timeout_work)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 			fcopy_respond_to_host(HV_E_FAIL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 			fcopy_transaction.state = HVUTIL_READY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	kfree(smsg_out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)  * Send a response back to the host.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) fcopy_respond_to_host(int error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	struct icmsg_hdr *icmsghdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	u32 buf_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	struct vmbus_channel *channel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	u64 req_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	 * Copy the global state for completing the transaction. Note that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	 * only one transaction can be active at a time. This is guaranteed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	 * by the file copy protocol implemented by the host. Furthermore,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	 * the "transaction active" state we maintain ensures that there can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	 * only be one active transaction at a time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	buf_len = fcopy_transaction.recv_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	channel = fcopy_transaction.recv_channel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	req_id = fcopy_transaction.recv_req_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	icmsghdr = (struct icmsg_hdr *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 			&recv_buffer[sizeof(struct vmbuspipe_hdr)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	if (channel->onchannel_callback == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 		 * We have raced with util driver being unloaded;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		 * silently return.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	icmsghdr->status = error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	icmsghdr->icflags = ICMSGHDRFLAG_TRANSACTION | ICMSGHDRFLAG_RESPONSE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	vmbus_sendpacket(channel, recv_buffer, buf_len, req_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 				VM_PKT_DATA_INBAND, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) void hv_fcopy_onchannelcallback(void *context)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	struct vmbus_channel *channel = context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	u32 recvlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	u64 requestid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	struct hv_fcopy_hdr *fcopy_msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	struct icmsg_hdr *icmsghdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	int fcopy_srv_version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	if (fcopy_transaction.state > HVUTIL_READY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	vmbus_recvpacket(channel, recv_buffer, HV_HYP_PAGE_SIZE * 2, &recvlen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 			 &requestid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	if (recvlen <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	icmsghdr = (struct icmsg_hdr *)&recv_buffer[
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 			sizeof(struct vmbuspipe_hdr)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	if (icmsghdr->icmsgtype == ICMSGTYPE_NEGOTIATE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		if (vmbus_prep_negotiate_resp(icmsghdr, recv_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 				fw_versions, FW_VER_COUNT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 				fcopy_versions, FCOPY_VER_COUNT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 				NULL, &fcopy_srv_version)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 			pr_info("FCopy IC version %d.%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 				fcopy_srv_version >> 16,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 				fcopy_srv_version & 0xFFFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		fcopy_msg = (struct hv_fcopy_hdr *)&recv_buffer[
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 				sizeof(struct vmbuspipe_hdr) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 				sizeof(struct icmsg_hdr)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		 * Stash away this global state for completing the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		 * transaction; note transactions are serialized.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		fcopy_transaction.recv_len = recvlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		fcopy_transaction.recv_req_id = requestid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		fcopy_transaction.fcopy_msg = fcopy_msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		if (fcopy_transaction.state < HVUTIL_READY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 			/* Userspace is not registered yet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 			fcopy_respond_to_host(HV_E_FAIL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		fcopy_transaction.state = HVUTIL_HOSTMSG_RECEIVED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 		 * Send the information to the user-level daemon.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		schedule_work(&fcopy_send_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		schedule_delayed_work(&fcopy_timeout_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 				      HV_UTIL_TIMEOUT * HZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	icmsghdr->icflags = ICMSGHDRFLAG_TRANSACTION | ICMSGHDRFLAG_RESPONSE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	vmbus_sendpacket(channel, recv_buffer, recvlen, requestid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 			VM_PKT_DATA_INBAND, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) /* Callback when data is received from userspace */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) static int fcopy_on_msg(void *msg, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	int *val = (int *)msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	if (len != sizeof(int))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	if (fcopy_transaction.state == HVUTIL_DEVICE_INIT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		return fcopy_handle_handshake(*val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	if (fcopy_transaction.state != HVUTIL_USERSPACE_REQ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	 * Complete the transaction by forwarding the result
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	 * to the host. But first, cancel the timeout.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	if (cancel_delayed_work_sync(&fcopy_timeout_work)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		fcopy_transaction.state = HVUTIL_USERSPACE_RECV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		fcopy_respond_to_host(*val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		hv_poll_channel(fcopy_transaction.recv_channel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 				fcopy_poll_wrapper);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) static void fcopy_on_reset(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	 * The daemon has exited; reset the state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	fcopy_transaction.state = HVUTIL_DEVICE_INIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	if (cancel_delayed_work_sync(&fcopy_timeout_work))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 		fcopy_respond_to_host(HV_E_FAIL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) int hv_fcopy_init(struct hv_util_service *srv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	recv_buffer = srv->recv_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	fcopy_transaction.recv_channel = srv->channel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	 * When this driver loads, the user level daemon that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	 * processes the host requests may not yet be running.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	 * Defer processing channel callbacks until the daemon
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	 * has registered.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	fcopy_transaction.state = HVUTIL_DEVICE_INIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	hvt = hvutil_transport_init(fcopy_devname, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 				    fcopy_on_msg, fcopy_on_reset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	if (!hvt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) static void hv_fcopy_cancel_work(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	cancel_delayed_work_sync(&fcopy_timeout_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	cancel_work_sync(&fcopy_send_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) int hv_fcopy_pre_suspend(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	struct vmbus_channel *channel = fcopy_transaction.recv_channel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	struct hv_fcopy_hdr *fcopy_msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	 * Fake a CANCEL_FCOPY message for the user space daemon in case the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	 * daemon is in the middle of copying some file. It doesn't matter if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	 * there is already a message pending to be delivered to the user
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	 * space since we force fcopy_transaction.state to be HVUTIL_READY, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	 * the user space daemon's write() will fail with EINVAL (see
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	 * fcopy_on_msg()), and the daemon will reset the device by closing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	 * and re-opening it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	fcopy_msg = kzalloc(sizeof(*fcopy_msg), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	if (!fcopy_msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	tasklet_disable(&channel->callback_event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	fcopy_msg->operation = CANCEL_FCOPY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	hv_fcopy_cancel_work();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	/* We don't care about the return value. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	hvutil_transport_send(hvt, fcopy_msg, sizeof(*fcopy_msg), NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	kfree(fcopy_msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	fcopy_transaction.state = HVUTIL_READY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	/* tasklet_enable() will be called in hv_fcopy_pre_resume(). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) int hv_fcopy_pre_resume(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	struct vmbus_channel *channel = fcopy_transaction.recv_channel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	tasklet_enable(&channel->callback_event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) void hv_fcopy_deinit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	fcopy_transaction.state = HVUTIL_DEVICE_DYING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	hv_fcopy_cancel_work();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	hvutil_transport_destroy(hvt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) }