Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright (c) 2009, Microsoft Corporation. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms and conditions of the GNU General Public License, |
| 7 | * version 2, as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 12 | * more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License along with |
| 15 | * this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
| 16 | * Place - Suite 330, Boston, MA 02111-1307 USA. |
| 17 | * |
| 18 | * Authors: |
| 19 | * Haiyang Zhang <haiyangz@microsoft.com> |
| 20 | * Hank Janssen <hjanssen@microsoft.com> |
| 21 | * |
| 22 | */ |
Hank Janssen | 0a46618 | 2011-03-29 13:58:47 -0700 | [diff] [blame] | 23 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 24 | |
Greg Kroah-Hartman | a0086dc | 2009-08-17 17:22:08 -0700 | [diff] [blame] | 25 | #include <linux/kernel.h> |
K. Y. Srinivasan | 0c3b7b2 | 2011-02-11 09:59:43 -0800 | [diff] [blame] | 26 | #include <linux/sched.h> |
| 27 | #include <linux/wait.h> |
Greg Kroah-Hartman | a0086dc | 2009-08-17 17:22:08 -0700 | [diff] [blame] | 28 | #include <linux/mm.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 29 | #include <linux/slab.h> |
Greg Kroah-Hartman | a0086dc | 2009-08-17 17:22:08 -0700 | [diff] [blame] | 30 | #include <linux/vmalloc.h> |
K. Y. Srinivasan | 3f335ea | 2011-05-12 19:34:15 -0700 | [diff] [blame] | 31 | |
| 32 | #include "hyperv.h" |
K. Y. Srinivasan | 0f2a661 | 2011-05-12 19:34:28 -0700 | [diff] [blame] | 33 | #include "hyperv_vmbus.h" |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 34 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 35 | |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 36 | struct vmbus_connection vmbus_connection = { |
| 37 | .conn_state = DISCONNECTED, |
| 38 | .next_gpadl_handle = ATOMIC_INIT(0xE1E10), |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 39 | }; |
| 40 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 41 | /* |
Haiyang Zhang | c697767 | 2011-01-26 12:12:08 -0800 | [diff] [blame] | 42 | * vmbus_connect - Sends a connect request on the partition service connection |
Greg Kroah-Hartman | fd8b85e | 2009-08-31 11:40:14 -0700 | [diff] [blame] | 43 | */ |
Haiyang Zhang | c697767 | 2011-01-26 12:12:08 -0800 | [diff] [blame] | 44 | int vmbus_connect(void) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 45 | { |
Greg Kroah-Hartman | fd8b85e | 2009-08-31 11:40:14 -0700 | [diff] [blame] | 46 | int ret = 0; |
K. Y. Srinivasan | 9568a19 | 2011-05-10 07:55:39 -0700 | [diff] [blame] | 47 | int t; |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 48 | struct vmbus_channel_msginfo *msginfo = NULL; |
Greg Kroah-Hartman | 8225021 | 2009-08-26 15:16:04 -0700 | [diff] [blame] | 49 | struct vmbus_channel_initiate_contact *msg; |
Greg Kroah-Hartman | dd0813b | 2009-07-15 14:56:45 -0700 | [diff] [blame] | 50 | unsigned long flags; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 51 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 52 | /* Make sure we are not connecting or connected */ |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 53 | if (vmbus_connection.conn_state != DISCONNECTED) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 54 | return -1; |
| 55 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 56 | /* Initialize the vmbus connection */ |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 57 | vmbus_connection.conn_state = CONNECTING; |
| 58 | vmbus_connection.work_queue = create_workqueue("hv_vmbus_con"); |
| 59 | if (!vmbus_connection.work_queue) { |
Bill Pemberton | de65a38 | 2009-07-29 17:00:09 -0400 | [diff] [blame] | 60 | ret = -1; |
K. Y. Srinivasan | b004386 | 2011-05-10 07:55:44 -0700 | [diff] [blame] | 61 | goto cleanup; |
Bill Pemberton | de65a38 | 2009-07-29 17:00:09 -0400 | [diff] [blame] | 62 | } |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 63 | |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 64 | INIT_LIST_HEAD(&vmbus_connection.chn_msg_list); |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 65 | spin_lock_init(&vmbus_connection.channelmsg_lock); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 66 | |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 67 | INIT_LIST_HEAD(&vmbus_connection.chn_list); |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 68 | spin_lock_init(&vmbus_connection.channel_lock); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 69 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 70 | /* |
| 71 | * Setup the vmbus event connection for channel interrupt |
| 72 | * abstraction stuff |
| 73 | */ |
K. Y. Srinivasan | df3493e | 2011-02-11 09:59:00 -0800 | [diff] [blame] | 74 | vmbus_connection.int_page = |
| 75 | (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO, 0); |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 76 | if (vmbus_connection.int_page == NULL) { |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 77 | ret = -1; |
K. Y. Srinivasan | b004386 | 2011-05-10 07:55:44 -0700 | [diff] [blame] | 78 | goto cleanup; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 81 | vmbus_connection.recv_int_page = vmbus_connection.int_page; |
| 82 | vmbus_connection.send_int_page = |
| 83 | (void *)((unsigned long)vmbus_connection.int_page + |
Greg Kroah-Hartman | fd8b85e | 2009-08-31 11:40:14 -0700 | [diff] [blame] | 84 | (PAGE_SIZE >> 1)); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 85 | |
Greg Kroah-Hartman | fd8b85e | 2009-08-31 11:40:14 -0700 | [diff] [blame] | 86 | /* |
| 87 | * Setup the monitor notification facility. The 1st page for |
| 88 | * parent->child and the 2nd page for child->parent |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 89 | */ |
K. Y. Srinivasan | df3493e | 2011-02-11 09:59:00 -0800 | [diff] [blame] | 90 | vmbus_connection.monitor_pages = |
| 91 | (void *)__get_free_pages((GFP_KERNEL|__GFP_ZERO), 1); |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 92 | if (vmbus_connection.monitor_pages == NULL) { |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 93 | ret = -1; |
K. Y. Srinivasan | b004386 | 2011-05-10 07:55:44 -0700 | [diff] [blame] | 94 | goto cleanup; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 97 | msginfo = kzalloc(sizeof(*msginfo) + |
Greg Kroah-Hartman | fd8b85e | 2009-08-31 11:40:14 -0700 | [diff] [blame] | 98 | sizeof(struct vmbus_channel_initiate_contact), |
| 99 | GFP_KERNEL); |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 100 | if (msginfo == NULL) { |
Bill Pemberton | 8cad0af | 2010-05-05 15:27:32 -0400 | [diff] [blame] | 101 | ret = -ENOMEM; |
K. Y. Srinivasan | b004386 | 2011-05-10 07:55:44 -0700 | [diff] [blame] | 102 | goto cleanup; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 103 | } |
| 104 | |
K. Y. Srinivasan | 9568a19 | 2011-05-10 07:55:39 -0700 | [diff] [blame] | 105 | init_completion(&msginfo->waitevent); |
Bill Pemberton | 80d11b2 | 2010-05-05 15:27:33 -0400 | [diff] [blame] | 106 | |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 107 | msg = (struct vmbus_channel_initiate_contact *)msginfo->msg; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 108 | |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 109 | msg->header.msgtype = CHANNELMSG_INITIATE_CONTACT; |
| 110 | msg->vmbus_version_requested = VMBUS_REVISION_NUMBER; |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 111 | msg->interrupt_page = virt_to_phys(vmbus_connection.int_page); |
| 112 | msg->monitor_page1 = virt_to_phys(vmbus_connection.monitor_pages); |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 113 | msg->monitor_page2 = virt_to_phys( |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 114 | (void *)((unsigned long)vmbus_connection.monitor_pages + |
Greg Kroah-Hartman | fd8b85e | 2009-08-31 11:40:14 -0700 | [diff] [blame] | 115 | PAGE_SIZE)); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 116 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 117 | /* |
| 118 | * Add to list before we send the request since we may |
| 119 | * receive the response before returning from this routine |
| 120 | */ |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 121 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
| 122 | list_add_tail(&msginfo->msglistentry, |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 123 | &vmbus_connection.chn_msg_list); |
Bill Pemberton | 53af545 | 2009-09-11 21:46:44 -0400 | [diff] [blame] | 124 | |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 125 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 126 | |
Haiyang Zhang | c697767 | 2011-01-26 12:12:08 -0800 | [diff] [blame] | 127 | ret = vmbus_post_msg(msg, |
Greg Kroah-Hartman | fd8b85e | 2009-08-31 11:40:14 -0700 | [diff] [blame] | 128 | sizeof(struct vmbus_channel_initiate_contact)); |
| 129 | if (ret != 0) { |
K. Y. Srinivasan | 0c3b7b2 | 2011-02-11 09:59:43 -0800 | [diff] [blame] | 130 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 131 | list_del(&msginfo->msglistentry); |
K. Y. Srinivasan | 0c3b7b2 | 2011-02-11 09:59:43 -0800 | [diff] [blame] | 132 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, |
| 133 | flags); |
K. Y. Srinivasan | b004386 | 2011-05-10 07:55:44 -0700 | [diff] [blame] | 134 | goto cleanup; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 137 | /* Wait for the connection response */ |
K. Y. Srinivasan | 9568a19 | 2011-05-10 07:55:39 -0700 | [diff] [blame] | 138 | t = wait_for_completion_timeout(&msginfo->waitevent, HZ); |
| 139 | if (t == 0) { |
K. Y. Srinivasan | 0c3b7b2 | 2011-02-11 09:59:43 -0800 | [diff] [blame] | 140 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, |
| 141 | flags); |
| 142 | list_del(&msginfo->msglistentry); |
| 143 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, |
| 144 | flags); |
| 145 | ret = -ETIMEDOUT; |
K. Y. Srinivasan | b004386 | 2011-05-10 07:55:44 -0700 | [diff] [blame] | 146 | goto cleanup; |
K. Y. Srinivasan | 0c3b7b2 | 2011-02-11 09:59:43 -0800 | [diff] [blame] | 147 | } |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 148 | |
K. Y. Srinivasan | 0c3b7b2 | 2011-02-11 09:59:43 -0800 | [diff] [blame] | 149 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 150 | list_del(&msginfo->msglistentry); |
K. Y. Srinivasan | 0c3b7b2 | 2011-02-11 09:59:43 -0800 | [diff] [blame] | 151 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 152 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 153 | /* Check if successful */ |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 154 | if (msginfo->response.version_response.version_supported) { |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 155 | vmbus_connection.conn_state = CONNECTED; |
Greg Kroah-Hartman | fd8b85e | 2009-08-31 11:40:14 -0700 | [diff] [blame] | 156 | } else { |
Hank Janssen | 0a46618 | 2011-03-29 13:58:47 -0700 | [diff] [blame] | 157 | pr_err("Unable to connect, " |
| 158 | "Version %d not supported by Hyper-V\n", |
| 159 | VMBUS_REVISION_NUMBER); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 160 | ret = -1; |
K. Y. Srinivasan | b004386 | 2011-05-10 07:55:44 -0700 | [diff] [blame] | 161 | goto cleanup; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 164 | kfree(msginfo); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 165 | return 0; |
| 166 | |
K. Y. Srinivasan | b004386 | 2011-05-10 07:55:44 -0700 | [diff] [blame] | 167 | cleanup: |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 168 | vmbus_connection.conn_state = DISCONNECTED; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 169 | |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 170 | if (vmbus_connection.work_queue) |
| 171 | destroy_workqueue(vmbus_connection.work_queue); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 172 | |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 173 | if (vmbus_connection.int_page) { |
K. Y. Srinivasan | df3493e | 2011-02-11 09:59:00 -0800 | [diff] [blame] | 174 | free_pages((unsigned long)vmbus_connection.int_page, 0); |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 175 | vmbus_connection.int_page = NULL; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 178 | if (vmbus_connection.monitor_pages) { |
K. Y. Srinivasan | df3493e | 2011-02-11 09:59:00 -0800 | [diff] [blame] | 179 | free_pages((unsigned long)vmbus_connection.monitor_pages, 1); |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 180 | vmbus_connection.monitor_pages = NULL; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Ilia Mirkin | dd9b15d | 2011-03-13 00:29:00 -0500 | [diff] [blame] | 183 | kfree(msginfo); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 184 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 185 | return ret; |
| 186 | } |
| 187 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 188 | /* |
Haiyang Zhang | c697767 | 2011-01-26 12:12:08 -0800 | [diff] [blame] | 189 | * vmbus_disconnect - |
| 190 | * Sends a disconnect request on the partition service connection |
Greg Kroah-Hartman | fd8b85e | 2009-08-31 11:40:14 -0700 | [diff] [blame] | 191 | */ |
Haiyang Zhang | c697767 | 2011-01-26 12:12:08 -0800 | [diff] [blame] | 192 | int vmbus_disconnect(void) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 193 | { |
Greg Kroah-Hartman | fd8b85e | 2009-08-31 11:40:14 -0700 | [diff] [blame] | 194 | int ret = 0; |
Greg Kroah-Hartman | 8225021 | 2009-08-26 15:16:04 -0700 | [diff] [blame] | 195 | struct vmbus_channel_message_header *msg; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 196 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 197 | /* Make sure we are connected */ |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 198 | if (vmbus_connection.conn_state != CONNECTED) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 199 | return -1; |
| 200 | |
Greg Kroah-Hartman | 8225021 | 2009-08-26 15:16:04 -0700 | [diff] [blame] | 201 | msg = kzalloc(sizeof(struct vmbus_channel_message_header), GFP_KERNEL); |
Bill Pemberton | 8cad0af | 2010-05-05 15:27:32 -0400 | [diff] [blame] | 202 | if (!msg) |
| 203 | return -ENOMEM; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 204 | |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 205 | msg->msgtype = CHANNELMSG_UNLOAD; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 206 | |
Haiyang Zhang | c697767 | 2011-01-26 12:12:08 -0800 | [diff] [blame] | 207 | ret = vmbus_post_msg(msg, |
Greg Kroah-Hartman | fd8b85e | 2009-08-31 11:40:14 -0700 | [diff] [blame] | 208 | sizeof(struct vmbus_channel_message_header)); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 209 | if (ret != 0) |
K. Y. Srinivasan | b004386 | 2011-05-10 07:55:44 -0700 | [diff] [blame] | 210 | goto cleanup; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 211 | |
K. Y. Srinivasan | df3493e | 2011-02-11 09:59:00 -0800 | [diff] [blame] | 212 | free_pages((unsigned long)vmbus_connection.int_page, 0); |
| 213 | free_pages((unsigned long)vmbus_connection.monitor_pages, 1); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 214 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 215 | /* TODO: iterate thru the msg list and free up */ |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 216 | destroy_workqueue(vmbus_connection.work_queue); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 217 | |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 218 | vmbus_connection.conn_state = DISCONNECTED; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 219 | |
Hank Janssen | 0a46618 | 2011-03-29 13:58:47 -0700 | [diff] [blame] | 220 | pr_info("hv_vmbus disconnected\n"); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 221 | |
K. Y. Srinivasan | b004386 | 2011-05-10 07:55:44 -0700 | [diff] [blame] | 222 | cleanup: |
Greg Kroah-Hartman | fd8b85e | 2009-08-31 11:40:14 -0700 | [diff] [blame] | 223 | kfree(msg); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 224 | return ret; |
| 225 | } |
| 226 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 227 | /* |
Haiyang Zhang | c697767 | 2011-01-26 12:12:08 -0800 | [diff] [blame] | 228 | * relid2channel - Get the channel object given its |
| 229 | * child relative id (ie channel id) |
Greg Kroah-Hartman | fd8b85e | 2009-08-31 11:40:14 -0700 | [diff] [blame] | 230 | */ |
Haiyang Zhang | c697767 | 2011-01-26 12:12:08 -0800 | [diff] [blame] | 231 | struct vmbus_channel *relid2channel(u32 relid) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 232 | { |
Greg Kroah-Hartman | aded7165 | 2009-08-18 15:21:19 -0700 | [diff] [blame] | 233 | struct vmbus_channel *channel; |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 234 | struct vmbus_channel *found_channel = NULL; |
Greg Kroah-Hartman | 0f5e44c | 2009-07-15 14:57:16 -0700 | [diff] [blame] | 235 | unsigned long flags; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 236 | |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 237 | spin_lock_irqsave(&vmbus_connection.channel_lock, flags); |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 238 | list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) { |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 239 | if (channel->offermsg.child_relid == relid) { |
| 240 | found_channel = channel; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 241 | break; |
| 242 | } |
| 243 | } |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 244 | spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 245 | |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 246 | return found_channel; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 249 | /* |
Haiyang Zhang | c697767 | 2011-01-26 12:12:08 -0800 | [diff] [blame] | 250 | * process_chn_event - Process a channel event notification |
Greg Kroah-Hartman | fd8b85e | 2009-08-31 11:40:14 -0700 | [diff] [blame] | 251 | */ |
Olaf Hering | 3543648 | 2011-04-16 18:50:40 +0200 | [diff] [blame] | 252 | static void process_chn_event(u32 relid) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 253 | { |
Greg Kroah-Hartman | aded7165 | 2009-08-18 15:21:19 -0700 | [diff] [blame] | 254 | struct vmbus_channel *channel; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 255 | |
Bill Pemberton | 972b952 | 2010-05-05 15:27:54 -0400 | [diff] [blame] | 256 | /* ASSERT(relId > 0); */ |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 257 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 258 | /* |
| 259 | * Find the channel based on this relid and invokes the |
| 260 | * channel callback to process the event |
| 261 | */ |
Haiyang Zhang | c697767 | 2011-01-26 12:12:08 -0800 | [diff] [blame] | 262 | channel = relid2channel(relid); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 263 | |
Greg Kroah-Hartman | fd8b85e | 2009-08-31 11:40:14 -0700 | [diff] [blame] | 264 | if (channel) { |
K. Y. Srinivasan | df452fa | 2011-06-06 15:49:47 -0700 | [diff] [blame^] | 265 | channel->onchannel_callback(channel->channel_callback_context); |
Greg Kroah-Hartman | fd8b85e | 2009-08-31 11:40:14 -0700 | [diff] [blame] | 266 | } else { |
Olaf Hering | 3543648 | 2011-04-16 18:50:40 +0200 | [diff] [blame] | 267 | pr_err("channel not found for relid - %u\n", relid); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 271 | /* |
Haiyang Zhang | c697767 | 2011-01-26 12:12:08 -0800 | [diff] [blame] | 272 | * vmbus_on_event - Handler for events |
Greg Kroah-Hartman | fd8b85e | 2009-08-31 11:40:14 -0700 | [diff] [blame] | 273 | */ |
K. Y. Srinivasan | 6de3d6a | 2011-03-10 14:05:16 -0800 | [diff] [blame] | 274 | void vmbus_on_event(unsigned long data) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 275 | { |
Olaf Hering | 3543648 | 2011-04-16 18:50:40 +0200 | [diff] [blame] | 276 | u32 dword; |
| 277 | u32 maxdword = MAX_NUM_CHANNELS_SUPPORTED >> 5; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 278 | int bit; |
Olaf Hering | 3543648 | 2011-04-16 18:50:40 +0200 | [diff] [blame] | 279 | u32 relid; |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 280 | u32 *recv_int_page = vmbus_connection.recv_int_page; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 281 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 282 | /* Check events */ |
Olaf Hering | 242b45a | 2011-04-16 18:50:39 +0200 | [diff] [blame] | 283 | if (!recv_int_page) |
| 284 | return; |
| 285 | for (dword = 0; dword < maxdword; dword++) { |
| 286 | if (!recv_int_page[dword]) |
| 287 | continue; |
| 288 | for (bit = 0; bit < 32; bit++) { |
| 289 | if (sync_test_and_clear_bit(bit, (unsigned long *)&recv_int_page[dword])) { |
| 290 | relid = (dword << 5) + bit; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 291 | |
Olaf Hering | 242b45a | 2011-04-16 18:50:39 +0200 | [diff] [blame] | 292 | if (relid == 0) { |
K. Y. Srinivasan | 6d81d33 | 2011-05-10 07:55:45 -0700 | [diff] [blame] | 293 | /* |
| 294 | * Special case - vmbus |
| 295 | * channel protocol msg |
| 296 | */ |
Olaf Hering | 242b45a | 2011-04-16 18:50:39 +0200 | [diff] [blame] | 297 | continue; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 298 | } |
Olaf Hering | 3543648 | 2011-04-16 18:50:40 +0200 | [diff] [blame] | 299 | process_chn_event(relid); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 300 | } |
Olaf Hering | 242b45a | 2011-04-16 18:50:39 +0200 | [diff] [blame] | 301 | } |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 302 | } |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 303 | } |
| 304 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 305 | /* |
Haiyang Zhang | c697767 | 2011-01-26 12:12:08 -0800 | [diff] [blame] | 306 | * vmbus_post_msg - Send a msg on the vmbus's message connection |
Greg Kroah-Hartman | fd8b85e | 2009-08-31 11:40:14 -0700 | [diff] [blame] | 307 | */ |
Haiyang Zhang | c697767 | 2011-01-26 12:12:08 -0800 | [diff] [blame] | 308 | int vmbus_post_msg(void *buffer, size_t buflen) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 309 | { |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 310 | union hv_connection_id conn_id; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 311 | |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 312 | conn_id.asu32 = 0; |
| 313 | conn_id.u.id = VMBUS_MESSAGE_CONNECTION_ID; |
| 314 | return hv_post_message(conn_id, 1, buffer, buflen); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 315 | } |
| 316 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 317 | /* |
Haiyang Zhang | c697767 | 2011-01-26 12:12:08 -0800 | [diff] [blame] | 318 | * vmbus_set_event - Send an event notification to the parent |
Greg Kroah-Hartman | fd8b85e | 2009-08-31 11:40:14 -0700 | [diff] [blame] | 319 | */ |
Haiyang Zhang | c697767 | 2011-01-26 12:12:08 -0800 | [diff] [blame] | 320 | int vmbus_set_event(u32 child_relid) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 321 | { |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 322 | /* Each u32 represents 32 channels */ |
Olaf Hering | 2235658 | 2011-03-21 14:41:37 +0100 | [diff] [blame] | 323 | sync_set_bit(child_relid & 31, |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 324 | (unsigned long *)vmbus_connection.send_int_page + |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 325 | (child_relid >> 5)); |
Bill Pemberton | 7c369f4 | 2009-07-29 17:00:11 -0400 | [diff] [blame] | 326 | |
Haiyang Zhang | d44890c | 2010-11-08 14:04:42 -0800 | [diff] [blame] | 327 | return hv_signal_event(); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 328 | } |