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> |
K. Y. Srinivasan | 5289d3d | 2011-08-25 09:49:01 -0700 | [diff] [blame] | 28 | #include <linux/delay.h> |
Greg Kroah-Hartman | a0086dc | 2009-08-17 17:22:08 -0700 | [diff] [blame] | 29 | #include <linux/mm.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 30 | #include <linux/slab.h> |
Greg Kroah-Hartman | a0086dc | 2009-08-17 17:22:08 -0700 | [diff] [blame] | 31 | #include <linux/vmalloc.h> |
Greg Kroah-Hartman | 46a9719 | 2011-10-04 12:29:52 -0700 | [diff] [blame] | 32 | #include <linux/hyperv.h> |
Greg Kroah-Hartman | 407dd16 | 2011-10-11 08:36:44 -0600 | [diff] [blame] | 33 | #include <asm/hyperv.h> |
K. Y. Srinivasan | 0f2a661 | 2011-05-12 19:34:28 -0700 | [diff] [blame] | 34 | #include "hyperv_vmbus.h" |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 35 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 36 | |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 37 | struct vmbus_connection vmbus_connection = { |
| 38 | .conn_state = DISCONNECTED, |
| 39 | .next_gpadl_handle = ATOMIC_INIT(0xE1E10), |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 40 | }; |
| 41 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 42 | /* |
Haiyang Zhang | c697767 | 2011-01-26 12:12:08 -0800 | [diff] [blame] | 43 | * vmbus_connect - Sends a connect request on the partition service connection |
Greg Kroah-Hartman | fd8b85e | 2009-08-31 11:40:14 -0700 | [diff] [blame] | 44 | */ |
Haiyang Zhang | c697767 | 2011-01-26 12:12:08 -0800 | [diff] [blame] | 45 | int vmbus_connect(void) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 46 | { |
Greg Kroah-Hartman | fd8b85e | 2009-08-31 11:40:14 -0700 | [diff] [blame] | 47 | int ret = 0; |
K. Y. Srinivasan | 9568a19 | 2011-05-10 07:55:39 -0700 | [diff] [blame] | 48 | int t; |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 49 | struct vmbus_channel_msginfo *msginfo = NULL; |
Greg Kroah-Hartman | 8225021 | 2009-08-26 15:16:04 -0700 | [diff] [blame] | 50 | struct vmbus_channel_initiate_contact *msg; |
Greg Kroah-Hartman | dd0813b | 2009-07-15 14:56:45 -0700 | [diff] [blame] | 51 | unsigned long flags; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 52 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 53 | /* Initialize the vmbus connection */ |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 54 | vmbus_connection.conn_state = CONNECTING; |
| 55 | vmbus_connection.work_queue = create_workqueue("hv_vmbus_con"); |
| 56 | if (!vmbus_connection.work_queue) { |
K. Y. Srinivasan | 3a7546d | 2011-06-06 15:50:10 -0700 | [diff] [blame] | 57 | ret = -ENOMEM; |
K. Y. Srinivasan | b004386 | 2011-05-10 07:55:44 -0700 | [diff] [blame] | 58 | goto cleanup; |
Bill Pemberton | de65a38 | 2009-07-29 17:00:09 -0400 | [diff] [blame] | 59 | } |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 60 | |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 61 | INIT_LIST_HEAD(&vmbus_connection.chn_msg_list); |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 62 | spin_lock_init(&vmbus_connection.channelmsg_lock); |
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_list); |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 65 | spin_lock_init(&vmbus_connection.channel_lock); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 66 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 67 | /* |
| 68 | * Setup the vmbus event connection for channel interrupt |
| 69 | * abstraction stuff |
| 70 | */ |
K. Y. Srinivasan | df3493e | 2011-02-11 09:59:00 -0800 | [diff] [blame] | 71 | vmbus_connection.int_page = |
| 72 | (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO, 0); |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 73 | if (vmbus_connection.int_page == NULL) { |
K. Y. Srinivasan | 3a7546d | 2011-06-06 15:50:10 -0700 | [diff] [blame] | 74 | ret = -ENOMEM; |
K. Y. Srinivasan | b004386 | 2011-05-10 07:55:44 -0700 | [diff] [blame] | 75 | goto cleanup; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 78 | vmbus_connection.recv_int_page = vmbus_connection.int_page; |
| 79 | vmbus_connection.send_int_page = |
| 80 | (void *)((unsigned long)vmbus_connection.int_page + |
Greg Kroah-Hartman | fd8b85e | 2009-08-31 11:40:14 -0700 | [diff] [blame] | 81 | (PAGE_SIZE >> 1)); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 82 | |
Greg Kroah-Hartman | fd8b85e | 2009-08-31 11:40:14 -0700 | [diff] [blame] | 83 | /* |
| 84 | * Setup the monitor notification facility. The 1st page for |
| 85 | * parent->child and the 2nd page for child->parent |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 86 | */ |
K. Y. Srinivasan | df3493e | 2011-02-11 09:59:00 -0800 | [diff] [blame] | 87 | vmbus_connection.monitor_pages = |
| 88 | (void *)__get_free_pages((GFP_KERNEL|__GFP_ZERO), 1); |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 89 | if (vmbus_connection.monitor_pages == NULL) { |
K. Y. Srinivasan | 3a7546d | 2011-06-06 15:50:10 -0700 | [diff] [blame] | 90 | ret = -ENOMEM; |
K. Y. Srinivasan | b004386 | 2011-05-10 07:55:44 -0700 | [diff] [blame] | 91 | goto cleanup; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 94 | msginfo = kzalloc(sizeof(*msginfo) + |
Greg Kroah-Hartman | fd8b85e | 2009-08-31 11:40:14 -0700 | [diff] [blame] | 95 | sizeof(struct vmbus_channel_initiate_contact), |
| 96 | GFP_KERNEL); |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 97 | if (msginfo == NULL) { |
Bill Pemberton | 8cad0af | 2010-05-05 15:27:32 -0400 | [diff] [blame] | 98 | ret = -ENOMEM; |
K. Y. Srinivasan | b004386 | 2011-05-10 07:55:44 -0700 | [diff] [blame] | 99 | goto cleanup; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 100 | } |
| 101 | |
K. Y. Srinivasan | 9568a19 | 2011-05-10 07:55:39 -0700 | [diff] [blame] | 102 | init_completion(&msginfo->waitevent); |
Bill Pemberton | 80d11b2 | 2010-05-05 15:27:33 -0400 | [diff] [blame] | 103 | |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 104 | msg = (struct vmbus_channel_initiate_contact *)msginfo->msg; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 105 | |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 106 | msg->header.msgtype = CHANNELMSG_INITIATE_CONTACT; |
| 107 | msg->vmbus_version_requested = VMBUS_REVISION_NUMBER; |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 108 | msg->interrupt_page = virt_to_phys(vmbus_connection.int_page); |
| 109 | msg->monitor_page1 = virt_to_phys(vmbus_connection.monitor_pages); |
Haiyang Zhang | c50f7fb | 2010-11-08 14:04:38 -0800 | [diff] [blame] | 110 | msg->monitor_page2 = virt_to_phys( |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 111 | (void *)((unsigned long)vmbus_connection.monitor_pages + |
Greg Kroah-Hartman | fd8b85e | 2009-08-31 11:40:14 -0700 | [diff] [blame] | 112 | PAGE_SIZE)); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 113 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 114 | /* |
| 115 | * Add to list before we send the request since we may |
| 116 | * receive the response before returning from this routine |
| 117 | */ |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 118 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
| 119 | list_add_tail(&msginfo->msglistentry, |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 120 | &vmbus_connection.chn_msg_list); |
Bill Pemberton | 53af545 | 2009-09-11 21:46:44 -0400 | [diff] [blame] | 121 | |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 122 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 123 | |
Haiyang Zhang | c697767 | 2011-01-26 12:12:08 -0800 | [diff] [blame] | 124 | ret = vmbus_post_msg(msg, |
Greg Kroah-Hartman | fd8b85e | 2009-08-31 11:40:14 -0700 | [diff] [blame] | 125 | sizeof(struct vmbus_channel_initiate_contact)); |
| 126 | if (ret != 0) { |
K. Y. Srinivasan | 0c3b7b2 | 2011-02-11 09:59:43 -0800 | [diff] [blame] | 127 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 128 | list_del(&msginfo->msglistentry); |
K. Y. Srinivasan | 0c3b7b2 | 2011-02-11 09:59:43 -0800 | [diff] [blame] | 129 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, |
| 130 | flags); |
K. Y. Srinivasan | b004386 | 2011-05-10 07:55:44 -0700 | [diff] [blame] | 131 | goto cleanup; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 134 | /* Wait for the connection response */ |
K. Y. Srinivasan | 2dfde96 | 2011-06-16 13:16:34 -0700 | [diff] [blame] | 135 | t = wait_for_completion_timeout(&msginfo->waitevent, 5*HZ); |
K. Y. Srinivasan | 9568a19 | 2011-05-10 07:55:39 -0700 | [diff] [blame] | 136 | if (t == 0) { |
K. Y. Srinivasan | 0c3b7b2 | 2011-02-11 09:59:43 -0800 | [diff] [blame] | 137 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, |
| 138 | flags); |
| 139 | list_del(&msginfo->msglistentry); |
| 140 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, |
| 141 | flags); |
| 142 | ret = -ETIMEDOUT; |
K. Y. Srinivasan | b004386 | 2011-05-10 07:55:44 -0700 | [diff] [blame] | 143 | goto cleanup; |
K. Y. Srinivasan | 0c3b7b2 | 2011-02-11 09:59:43 -0800 | [diff] [blame] | 144 | } |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 145 | |
K. Y. Srinivasan | 0c3b7b2 | 2011-02-11 09:59:43 -0800 | [diff] [blame] | 146 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 147 | list_del(&msginfo->msglistentry); |
K. Y. Srinivasan | 0c3b7b2 | 2011-02-11 09:59:43 -0800 | [diff] [blame] | 148 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 149 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 150 | /* Check if successful */ |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 151 | if (msginfo->response.version_response.version_supported) { |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 152 | vmbus_connection.conn_state = CONNECTED; |
Greg Kroah-Hartman | fd8b85e | 2009-08-31 11:40:14 -0700 | [diff] [blame] | 153 | } else { |
Hank Janssen | 0a46618 | 2011-03-29 13:58:47 -0700 | [diff] [blame] | 154 | pr_err("Unable to connect, " |
| 155 | "Version %d not supported by Hyper-V\n", |
| 156 | VMBUS_REVISION_NUMBER); |
K. Y. Srinivasan | 3a7546d | 2011-06-06 15:50:10 -0700 | [diff] [blame] | 157 | ret = -ECONNREFUSED; |
K. Y. Srinivasan | b004386 | 2011-05-10 07:55:44 -0700 | [diff] [blame] | 158 | goto cleanup; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 161 | kfree(msginfo); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 162 | return 0; |
| 163 | |
K. Y. Srinivasan | b004386 | 2011-05-10 07:55:44 -0700 | [diff] [blame] | 164 | cleanup: |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 165 | vmbus_connection.conn_state = DISCONNECTED; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 166 | |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 167 | if (vmbus_connection.work_queue) |
| 168 | destroy_workqueue(vmbus_connection.work_queue); |
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.int_page) { |
K. Y. Srinivasan | df3493e | 2011-02-11 09:59:00 -0800 | [diff] [blame] | 171 | free_pages((unsigned long)vmbus_connection.int_page, 0); |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 172 | vmbus_connection.int_page = NULL; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 175 | if (vmbus_connection.monitor_pages) { |
K. Y. Srinivasan | df3493e | 2011-02-11 09:59:00 -0800 | [diff] [blame] | 176 | free_pages((unsigned long)vmbus_connection.monitor_pages, 1); |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 177 | vmbus_connection.monitor_pages = NULL; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Ilia Mirkin | dd9b15d | 2011-03-13 00:29:00 -0500 | [diff] [blame] | 180 | kfree(msginfo); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 181 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 182 | return ret; |
| 183 | } |
| 184 | |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 185 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 186 | /* |
Haiyang Zhang | c697767 | 2011-01-26 12:12:08 -0800 | [diff] [blame] | 187 | * relid2channel - Get the channel object given its |
| 188 | * child relative id (ie channel id) |
Greg Kroah-Hartman | fd8b85e | 2009-08-31 11:40:14 -0700 | [diff] [blame] | 189 | */ |
Haiyang Zhang | c697767 | 2011-01-26 12:12:08 -0800 | [diff] [blame] | 190 | struct vmbus_channel *relid2channel(u32 relid) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 191 | { |
Greg Kroah-Hartman | aded716 | 2009-08-18 15:21:19 -0700 | [diff] [blame] | 192 | struct vmbus_channel *channel; |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 193 | struct vmbus_channel *found_channel = NULL; |
Greg Kroah-Hartman | 0f5e44c | 2009-07-15 14:57:16 -0700 | [diff] [blame] | 194 | unsigned long flags; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 195 | |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 196 | spin_lock_irqsave(&vmbus_connection.channel_lock, flags); |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 197 | list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) { |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 198 | if (channel->offermsg.child_relid == relid) { |
| 199 | found_channel = channel; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 200 | break; |
| 201 | } |
| 202 | } |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 203 | spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 204 | |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 205 | return found_channel; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 208 | /* |
Haiyang Zhang | c697767 | 2011-01-26 12:12:08 -0800 | [diff] [blame] | 209 | * process_chn_event - Process a channel event notification |
Greg Kroah-Hartman | fd8b85e | 2009-08-31 11:40:14 -0700 | [diff] [blame] | 210 | */ |
Olaf Hering | 3543648 | 2011-04-16 18:50:40 +0200 | [diff] [blame] | 211 | static void process_chn_event(u32 relid) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 212 | { |
Greg Kroah-Hartman | aded716 | 2009-08-18 15:21:19 -0700 | [diff] [blame] | 213 | struct vmbus_channel *channel; |
K. Y. Srinivasan | dad76bf | 2011-08-27 11:31:33 -0700 | [diff] [blame] | 214 | unsigned long flags; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 215 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 216 | /* |
| 217 | * Find the channel based on this relid and invokes the |
| 218 | * channel callback to process the event |
| 219 | */ |
Haiyang Zhang | c697767 | 2011-01-26 12:12:08 -0800 | [diff] [blame] | 220 | channel = relid2channel(relid); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 221 | |
K. Y. Srinivasan | 2432603 | 2011-08-31 14:35:57 -0700 | [diff] [blame] | 222 | if (!channel) { |
| 223 | pr_err("channel not found for relid - %u\n", relid); |
| 224 | return; |
| 225 | } |
| 226 | |
| 227 | /* |
| 228 | * A channel once created is persistent even when there |
| 229 | * is no driver handling the device. An unloading driver |
| 230 | * sets the onchannel_callback to NULL under the |
| 231 | * protection of the channel inbound_lock. Thus, checking |
| 232 | * and invoking the driver specific callback takes care of |
| 233 | * orderly unloading of the driver. |
| 234 | */ |
| 235 | |
K. Y. Srinivasan | dad76bf | 2011-08-27 11:31:33 -0700 | [diff] [blame] | 236 | spin_lock_irqsave(&channel->inbound_lock, flags); |
K. Y. Srinivasan | 2432603 | 2011-08-31 14:35:57 -0700 | [diff] [blame] | 237 | if (channel->onchannel_callback != NULL) |
K. Y. Srinivasan | df452fa | 2011-06-06 15:49:47 -0700 | [diff] [blame] | 238 | channel->onchannel_callback(channel->channel_callback_context); |
K. Y. Srinivasan | d9add43b | 2011-08-27 11:31:43 -0700 | [diff] [blame] | 239 | else |
K. Y. Srinivasan | 2432603 | 2011-08-31 14:35:57 -0700 | [diff] [blame] | 240 | pr_err("no channel callback for relid - %u\n", relid); |
K. Y. Srinivasan | d9add43b | 2011-08-27 11:31:43 -0700 | [diff] [blame] | 241 | |
K. Y. Srinivasan | dad76bf | 2011-08-27 11:31:33 -0700 | [diff] [blame] | 242 | spin_unlock_irqrestore(&channel->inbound_lock, flags); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 245 | /* |
Haiyang Zhang | c697767 | 2011-01-26 12:12:08 -0800 | [diff] [blame] | 246 | * vmbus_on_event - Handler for events |
Greg Kroah-Hartman | fd8b85e | 2009-08-31 11:40:14 -0700 | [diff] [blame] | 247 | */ |
K. Y. Srinivasan | 6de3d6a | 2011-03-10 14:05:16 -0800 | [diff] [blame] | 248 | void vmbus_on_event(unsigned long data) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 249 | { |
Olaf Hering | 3543648 | 2011-04-16 18:50:40 +0200 | [diff] [blame] | 250 | u32 dword; |
| 251 | u32 maxdword = MAX_NUM_CHANNELS_SUPPORTED >> 5; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 252 | int bit; |
Olaf Hering | 3543648 | 2011-04-16 18:50:40 +0200 | [diff] [blame] | 253 | u32 relid; |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 254 | u32 *recv_int_page = vmbus_connection.recv_int_page; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 255 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 256 | /* Check events */ |
Olaf Hering | 242b45a | 2011-04-16 18:50:39 +0200 | [diff] [blame] | 257 | if (!recv_int_page) |
| 258 | return; |
| 259 | for (dword = 0; dword < maxdword; dword++) { |
| 260 | if (!recv_int_page[dword]) |
| 261 | continue; |
| 262 | for (bit = 0; bit < 32; bit++) { |
K. Y. Srinivasan | d9add43b | 2011-08-27 11:31:43 -0700 | [diff] [blame] | 263 | if (sync_test_and_clear_bit(bit, |
| 264 | (unsigned long *)&recv_int_page[dword])) { |
Olaf Hering | 242b45a | 2011-04-16 18:50:39 +0200 | [diff] [blame] | 265 | relid = (dword << 5) + bit; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 266 | |
K. Y. Srinivasan | d9add43b | 2011-08-27 11:31:43 -0700 | [diff] [blame] | 267 | if (relid == 0) |
K. Y. Srinivasan | 6d81d33 | 2011-05-10 07:55:45 -0700 | [diff] [blame] | 268 | /* |
| 269 | * Special case - vmbus |
| 270 | * channel protocol msg |
| 271 | */ |
Olaf Hering | 242b45a | 2011-04-16 18:50:39 +0200 | [diff] [blame] | 272 | continue; |
K. Y. Srinivasan | d9add43b | 2011-08-27 11:31:43 -0700 | [diff] [blame] | 273 | |
Olaf Hering | 3543648 | 2011-04-16 18:50:40 +0200 | [diff] [blame] | 274 | process_chn_event(relid); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 275 | } |
Olaf Hering | 242b45a | 2011-04-16 18:50:39 +0200 | [diff] [blame] | 276 | } |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 277 | } |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 280 | /* |
Haiyang Zhang | c697767 | 2011-01-26 12:12:08 -0800 | [diff] [blame] | 281 | * 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] | 282 | */ |
Haiyang Zhang | c697767 | 2011-01-26 12:12:08 -0800 | [diff] [blame] | 283 | int vmbus_post_msg(void *buffer, size_t buflen) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 284 | { |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 285 | union hv_connection_id conn_id; |
K. Y. Srinivasan | 5289d3d | 2011-08-25 09:49:01 -0700 | [diff] [blame] | 286 | int ret = 0; |
| 287 | int retries = 0; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 288 | |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 289 | conn_id.asu32 = 0; |
| 290 | conn_id.u.id = VMBUS_MESSAGE_CONNECTION_ID; |
K. Y. Srinivasan | 5289d3d | 2011-08-25 09:49:01 -0700 | [diff] [blame] | 291 | |
| 292 | /* |
| 293 | * hv_post_message() can have transient failures because of |
| 294 | * insufficient resources. Retry the operation a couple of |
| 295 | * times before giving up. |
| 296 | */ |
| 297 | while (retries < 3) { |
| 298 | ret = hv_post_message(conn_id, 1, buffer, buflen); |
| 299 | if (ret != HV_STATUS_INSUFFICIENT_BUFFERS) |
| 300 | return ret; |
| 301 | retries++; |
| 302 | msleep(100); |
| 303 | } |
| 304 | return ret; |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 305 | } |
| 306 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 307 | /* |
Haiyang Zhang | c697767 | 2011-01-26 12:12:08 -0800 | [diff] [blame] | 308 | * vmbus_set_event - Send an event notification to the parent |
Greg Kroah-Hartman | fd8b85e | 2009-08-31 11:40:14 -0700 | [diff] [blame] | 309 | */ |
Haiyang Zhang | c697767 | 2011-01-26 12:12:08 -0800 | [diff] [blame] | 310 | int vmbus_set_event(u32 child_relid) |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 311 | { |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 312 | /* Each u32 represents 32 channels */ |
Olaf Hering | 2235658 | 2011-03-21 14:41:37 +0100 | [diff] [blame] | 313 | sync_set_bit(child_relid & 31, |
Haiyang Zhang | da9fcb7 | 2011-01-26 12:12:14 -0800 | [diff] [blame] | 314 | (unsigned long *)vmbus_connection.send_int_page + |
Haiyang Zhang | 15b2f64 | 2011-01-26 12:12:07 -0800 | [diff] [blame] | 315 | (child_relid >> 5)); |
Bill Pemberton | 7c369f4 | 2009-07-29 17:00:11 -0400 | [diff] [blame] | 316 | |
Haiyang Zhang | d44890c | 2010-11-08 14:04:42 -0800 | [diff] [blame] | 317 | return hv_signal_event(); |
Hank Janssen | 3e7ee49 | 2009-07-13 16:02:34 -0700 | [diff] [blame] | 318 | } |