Merge tag 'spi-v4.9' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie/spi
[cascardo/linux.git] / drivers / hv / hv_kvp.c
index 9b9b370..5e1fdc8 100644 (file)
@@ -78,9 +78,11 @@ static void kvp_send_key(struct work_struct *dummy);
 
 static void kvp_respond_to_host(struct hv_kvp_msg *msg, int error);
 static void kvp_timeout_func(struct work_struct *dummy);
+static void kvp_host_handshake_func(struct work_struct *dummy);
 static void kvp_register(int);
 
 static DECLARE_DELAYED_WORK(kvp_timeout_work, kvp_timeout_func);
+static DECLARE_DELAYED_WORK(kvp_host_handshake_work, kvp_host_handshake_func);
 static DECLARE_WORK(kvp_sendkey_work, kvp_send_key);
 
 static const char kvp_devname[] = "vmbus/hv_kvp";
@@ -100,6 +102,17 @@ static void kvp_poll_wrapper(void *channel)
        hv_kvp_onchannelcallback(channel);
 }
 
+static void kvp_register_done(void)
+{
+       /*
+        * If we're still negotiating with the host cancel the timeout
+        * work to not poll the channel twice.
+        */
+       pr_debug("KVP: userspace daemon registered\n");
+       cancel_delayed_work_sync(&kvp_host_handshake_work);
+       hv_poll_channel(kvp_transaction.recv_channel, kvp_poll_wrapper);
+}
+
 static void
 kvp_register(int reg_value)
 {
@@ -114,7 +127,8 @@ kvp_register(int reg_value)
                kvp_msg->kvp_hdr.operation = reg_value;
                strcpy(version, HV_DRV_VERSION);
 
-               hvutil_transport_send(hvt, kvp_msg, sizeof(*kvp_msg));
+               hvutil_transport_send(hvt, kvp_msg, sizeof(*kvp_msg),
+                                     kvp_register_done);
                kfree(kvp_msg);
        }
 }
@@ -130,6 +144,11 @@ static void kvp_timeout_func(struct work_struct *dummy)
        hv_poll_channel(kvp_transaction.recv_channel, kvp_poll_wrapper);
 }
 
+static void kvp_host_handshake_func(struct work_struct *dummy)
+{
+       hv_poll_channel(kvp_transaction.recv_channel, hv_kvp_onchannelcallback);
+}
+
 static int kvp_handle_handshake(struct hv_kvp_msg *msg)
 {
        switch (msg->kvp_hdr.operation) {
@@ -151,10 +170,9 @@ static int kvp_handle_handshake(struct hv_kvp_msg *msg)
        /*
         * We have a compatible daemon; complete the handshake.
         */
-       pr_debug("KVP: userspace daemon ver. %d registered\n",
-                KVP_OP_REGISTER);
+       pr_debug("KVP: userspace daemon ver. %d connected\n",
+                msg->kvp_hdr.operation);
        kvp_register(dm_reg_value);
-       hv_poll_channel(kvp_transaction.recv_channel, kvp_poll_wrapper);
 
        return 0;
 }
@@ -442,7 +460,7 @@ kvp_send_key(struct work_struct *dummy)
        }
 
        kvp_transaction.state = HVUTIL_USERSPACE_REQ;
-       rc = hvutil_transport_send(hvt, message, sizeof(*message));
+       rc = hvutil_transport_send(hvt, message, sizeof(*message), NULL);
        if (rc) {
                pr_debug("KVP: failed to communicate to the daemon: %d\n", rc);
                if (cancel_delayed_work_sync(&kvp_timeout_work)) {
@@ -594,7 +612,22 @@ void hv_kvp_onchannelcallback(void *context)
        struct icmsg_negotiate *negop = NULL;
        int util_fw_version;
        int kvp_srv_version;
+       static enum {NEGO_NOT_STARTED,
+                    NEGO_IN_PROGRESS,
+                    NEGO_FINISHED} host_negotiatied = NEGO_NOT_STARTED;
 
+       if (host_negotiatied == NEGO_NOT_STARTED &&
+           kvp_transaction.state < HVUTIL_READY) {
+               /*
+                * If userspace daemon is not connected and host is asking
+                * us to negotiate we need to delay to not lose messages.
+                * This is important for Failover IP setting.
+                */
+               host_negotiatied = NEGO_IN_PROGRESS;
+               schedule_delayed_work(&kvp_host_handshake_work,
+                                     HV_UTIL_NEGO_TIMEOUT * HZ);
+               return;
+       }
        if (kvp_transaction.state > HVUTIL_READY)
                return;
 
@@ -672,6 +705,8 @@ void hv_kvp_onchannelcallback(void *context)
                vmbus_sendpacket(channel, recv_buffer,
                                       recvlen, requestid,
                                       VM_PKT_DATA_INBAND, 0);
+
+               host_negotiatied = NEGO_FINISHED;
        }
 
 }
@@ -708,6 +743,7 @@ hv_kvp_init(struct hv_util_service *srv)
 void hv_kvp_deinit(void)
 {
        kvp_transaction.state = HVUTIL_DEVICE_DYING;
+       cancel_delayed_work_sync(&kvp_host_handshake_work);
        cancel_delayed_work_sync(&kvp_timeout_work);
        cancel_work_sync(&kvp_sendkey_work);
        hvutil_transport_destroy(hvt);