Staging: hv: util: Perform some service specific init/deinit in probe/remove
authorK. Y. Srinivasan <kys@microsoft.com>
Sun, 18 Sep 2011 17:31:33 +0000 (10:31 -0700)
committerGreg Kroah-Hartman <gregkh@suse.de>
Tue, 20 Sep 2011 20:00:53 +0000 (13:00 -0700)
In preparation for modifying the util driver to fully conform to the
Linux Driver Model, perform some service specific init and de-init
operations in util_probe()/util_remove()  as opposed to in
init_hyperv_utils()/exit_hyperv_utils() as is currently done.

Signed-off-by: K. Y. Srinivasan <kys@microsoft.com>
Signed-off-by: Haiyang Zhang <haiyangz@microsoft.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
drivers/staging/hv/hv_kvp.c
drivers/staging/hv/hv_kvp.h
drivers/staging/hv/hv_util.c
drivers/staging/hv/hyperv.h

index 13b0ecf7d5d61ca9405bec1693a616181c584d00..ff0d9ab2e4a12b33125e41b883f93c590f765b78 100644 (file)
@@ -312,16 +312,14 @@ callback_done:
 }
 
 int
-hv_kvp_init(void)
+hv_kvp_init(struct hv_util_service *srv)
 {
        int err;
 
        err = cn_add_callback(&kvp_id, kvp_name, kvp_cn_callback);
        if (err)
                return err;
-       recv_buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
-       if (!recv_buffer)
-               return -ENOMEM;
+       recv_buffer = srv->recv_buffer;
 
        return 0;
 }
@@ -330,5 +328,4 @@ void hv_kvp_deinit(void)
 {
        cn_del_callback(&kvp_id);
        cancel_delayed_work_sync(&kvp_work);
-       kfree(recv_buffer);
 }
index 8c402f357d37ee676ea28e51aecafcfd02763e2b..9b765d7df83808b855630f26183fbb5ba527c103 100644 (file)
@@ -175,7 +175,7 @@ struct hv_kvp_msg {
        struct hv_kvp_msg_enumerate     kvp_data;
 };
 
-int hv_kvp_init(void);
+int hv_kvp_init(struct hv_util_service *);
 void hv_kvp_deinit(void);
 void hv_kvp_onchannelcallback(void *);
 
index d9460fdd9e4f6a9479e7ee623780a9a077c1febc..a1539a7814d4ea32c15f8db2ee09acd118083692 100644 (file)
 #include "hyperv.h"
 #include "hv_kvp.h"
 
-static u8 *shut_txf_buf;
-static u8 *time_txf_buf;
-static u8 *hbeat_txf_buf;
+
+static void shutdown_onchannelcallback(void *context);
+static struct hv_util_service util_shutdown = {
+       .util_cb = shutdown_onchannelcallback,
+};
+
+static void timesync_onchannelcallback(void *context);
+static struct hv_util_service util_timesynch = {
+       .util_cb = timesync_onchannelcallback,
+};
+
+static void heartbeat_onchannelcallback(void *context);
+static struct hv_util_service util_heartbeat = {
+       .util_cb = heartbeat_onchannelcallback,
+};
+
+static struct hv_util_service util_kvp = {
+       .util_cb = hv_kvp_onchannelcallback,
+       .util_init = hv_kvp_init,
+       .util_deinit = hv_kvp_deinit,
+};
 
 static void shutdown_onchannelcallback(void *context)
 {
@@ -40,6 +58,7 @@ static void shutdown_onchannelcallback(void *context)
        u32 recvlen;
        u64 requestid;
        u8  execute_shutdown = false;
+       u8  *shut_txf_buf = util_shutdown.recv_buffer;
 
        struct shutdown_msg_data *shutdown_msg;
 
@@ -169,6 +188,7 @@ static void timesync_onchannelcallback(void *context)
        u64 requestid;
        struct icmsg_hdr *icmsghdrp;
        struct ictimesync_data *timedatap;
+       u8 *time_txf_buf = util_timesynch.recv_buffer;
 
        vmbus_recvpacket(channel, time_txf_buf,
                         PAGE_SIZE, &recvlen, &requestid);
@@ -207,6 +227,7 @@ static void heartbeat_onchannelcallback(void *context)
        u64 requestid;
        struct icmsg_hdr *icmsghdrp;
        struct heartbeat_msg_data *heartbeat_msg;
+       u8 *hbeat_txf_buf = util_heartbeat.recv_buffer;
 
        vmbus_recvpacket(channel, hbeat_txf_buf,
                         PAGE_SIZE, &recvlen, &requestid);
@@ -235,34 +256,56 @@ static void heartbeat_onchannelcallback(void *context)
        }
 }
 
-/*
- * The devices managed by the util driver don't need any additional
- * setup.
- */
 static int util_probe(struct hv_device *dev,
                        const struct hv_vmbus_device_id *dev_id)
 {
+       struct hv_util_service *srv =
+               (struct hv_util_service *)dev_id->driver_data;
+       int ret;
+
+       srv->recv_buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
+       if (!srv->recv_buffer)
+               return -ENOMEM;
+       if (srv->util_init) {
+               ret = srv->util_init(srv);
+               if (ret) {
+                       kfree(srv->recv_buffer);
+                       return -ENODEV;
+               }
+       }
+
+       hv_set_drvdata(dev, srv);
        return 0;
 }
 
 static int util_remove(struct hv_device *dev)
 {
+       struct hv_util_service *srv = hv_get_drvdata(dev);
+
+       if (srv->util_deinit)
+               srv->util_deinit();
+       kfree(srv->recv_buffer);
+
        return 0;
 }
 
 static const struct hv_vmbus_device_id id_table[] = {
        /* Shutdown guid */
        { VMBUS_DEVICE(0x31, 0x60, 0x0B, 0X0E, 0x13, 0x52, 0x34, 0x49,
-                      0x81, 0x8B, 0x38, 0XD9, 0x0C, 0xED, 0x39, 0xDB) },
+                      0x81, 0x8B, 0x38, 0XD9, 0x0C, 0xED, 0x39, 0xDB)
+         .driver_data = (unsigned long)&util_shutdown },
        /* Time synch guid */
        { VMBUS_DEVICE(0x30, 0xe6, 0x27, 0x95, 0xae, 0xd0, 0x7b, 0x49,
-                      0xad, 0xce, 0xe8, 0x0a, 0xb0, 0x17, 0x5c, 0xaf) },
+                      0xad, 0xce, 0xe8, 0x0a, 0xb0, 0x17, 0x5c, 0xaf)
+         .driver_data = (unsigned long)&util_timesynch },
        /* Heartbeat guid */
        { VMBUS_DEVICE(0x39, 0x4f, 0x16, 0x57, 0x15, 0x91, 0x78, 0x4e,
-                      0xab, 0x55, 0x38, 0x2f, 0x3b, 0xd5, 0x42, 0x2d) },
+                      0xab, 0x55, 0x38, 0x2f, 0x3b, 0xd5, 0x42, 0x2d)
+         .driver_data = (unsigned long)&util_heartbeat },
        /* KVP guid */
        { VMBUS_DEVICE(0xe7, 0xf4, 0xa0, 0xa9, 0x45, 0x5a, 0x96, 0x4d,
-                      0xb8, 0x27, 0x8a, 0x84, 0x1e, 0x8c, 0x3,  0xe6) },
+                      0xb8, 0x27, 0x8a, 0x84, 0x1e, 0x8c, 0x3,  0xe6)
+         .driver_data = (unsigned long)&util_kvp },
        { },
 };
 
@@ -281,24 +324,11 @@ static int __init init_hyperv_utils(void)
        int ret;
        pr_info("Registering HyperV Utility Driver\n");
 
-       if (hv_kvp_init())
-               return -ENODEV;
-
-
-       shut_txf_buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
-       time_txf_buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
-       hbeat_txf_buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
-
-       if (!shut_txf_buf || !time_txf_buf || !hbeat_txf_buf) {
-               pr_info("Unable to allocate memory for receive buffer\n");
-               ret = -ENOMEM;
-               goto err;
-       }
 
        ret = vmbus_driver_register(&util_drv);
 
        if (ret != 0)
-               goto err;
+               return ret;
 
        hv_cb_utils[HV_SHUTDOWN_MSG].callback = &shutdown_onchannelcallback;
 
@@ -310,12 +340,6 @@ static int __init init_hyperv_utils(void)
 
        return 0;
 
-err:
-       kfree(shut_txf_buf);
-       kfree(time_txf_buf);
-       kfree(hbeat_txf_buf);
-
-       return ret;
 }
 
 static void exit_hyperv_utils(void)
@@ -342,11 +366,6 @@ static void exit_hyperv_utils(void)
                        &chn_cb_negotiate;
        hv_cb_utils[HV_KVP_MSG].callback = NULL;
 
-       hv_kvp_deinit();
-
-       kfree(shut_txf_buf);
-       kfree(time_txf_buf);
-       kfree(hbeat_txf_buf);
        vmbus_driver_unregister(&util_drv);
 }
 
index 76e2ddd437d95077cd91f3fb950d1e52009150b1..5c3e297e11b69469c3098631ae3689c8c6229372 100644 (file)
@@ -889,6 +889,19 @@ void vmbus_driver_unregister(struct hv_driver *hv_driver);
 #define HV_ERROR_NOT_SUPPORTED         0x80070032
 #define HV_ERROR_MACHINE_LOCKED                0x800704F7
 
+/*
+ * While we want to handle util services as regular devices,
+ * there is only one instance of each of these services; so
+ * we statically allocate the service specific state.
+ */
+
+struct hv_util_service {
+       u8 *recv_buffer;
+       void (*util_cb)(void *);
+       int (*util_init)(struct hv_util_service *);
+       void (*util_deinit)(void);
+};
+
 struct vmbuspipe_hdr {
        u32 flags;
        u32 msgsize;