Merge git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging-2.6
[pandora-kernel.git] / drivers / staging / hv / vmbus.c
1 /*
2  * Copyright (c) 2009, Microsoft Corporation.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms and conditions of the GNU General Public License,
6  * version 2, as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11  * more details.
12  *
13  * You should have received a copy of the GNU General Public License along with
14  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15  * Place - Suite 330, Boston, MA 02111-1307 USA.
16  *
17  * Authors:
18  *   Haiyang Zhang <haiyangz@microsoft.com>
19  *   Hank Janssen  <hjanssen@microsoft.com>
20  *
21  */
22 #include <linux/kernel.h>
23 #include <linux/mm.h>
24 #include <linux/slab.h>
25 #include "osd.h"
26 #include "logging.h"
27 #include "version_info.h"
28 #include "vmbus_private.h"
29
30 static const char *gDriverName = "vmbus";
31
32 /*
33  * Windows vmbus does not defined this.
34  * We defined this to be consistent with other devices
35  */
36 /* {c5295816-f63a-4d5f-8d1a-4daf999ca185} */
37 static const struct hv_guid gVmbusDeviceType = {
38         .data = {
39                 0x16, 0x58, 0x29, 0xc5, 0x3a, 0xf6, 0x5f, 0x4d,
40                 0x8d, 0x1a, 0x4d, 0xaf, 0x99, 0x9c, 0xa1, 0x85
41         }
42 };
43
44 /* {ac3760fc-9adf-40aa-9427-a70ed6de95c5} */
45 static const struct hv_guid gVmbusDeviceId = {
46         .data = {
47                 0xfc, 0x60, 0x37, 0xac, 0xdf, 0x9a, 0xaa, 0x40,
48                 0x94, 0x27, 0xa7, 0x0e, 0xd6, 0xde, 0x95, 0xc5
49         }
50 };
51
52 static struct hv_driver *gDriver; /* vmbus driver object */
53 static struct hv_device *gDevice; /* vmbus root device */
54
55 /*
56  * VmbusGetChannelOffers - Retrieve the channel offers from the parent partition
57  */
58 static void VmbusGetChannelOffers(void)
59 {
60         vmbus_request_offers();
61 }
62
63 /*
64  * VmbusCreateChildDevice - Creates the child device on the bus that represents the channel offer
65  */
66 struct hv_device *VmbusChildDeviceCreate(struct hv_guid *DeviceType,
67                                          struct hv_guid *DeviceInstance,
68                                          struct vmbus_channel *channel)
69 {
70         struct vmbus_driver *vmbusDriver = (struct vmbus_driver *)gDriver;
71
72         return vmbusDriver->OnChildDeviceCreate(DeviceType, DeviceInstance,
73                                                 channel);
74 }
75
76 /*
77  * VmbusChildDeviceAdd - Registers the child device with the vmbus
78  */
79 int VmbusChildDeviceAdd(struct hv_device *ChildDevice)
80 {
81         struct vmbus_driver *vmbusDriver = (struct vmbus_driver *)gDriver;
82
83         return vmbusDriver->OnChildDeviceAdd(gDevice, ChildDevice);
84 }
85
86 /*
87  * VmbusChildDeviceRemove Unregisters the child device from the vmbus
88  */
89 void VmbusChildDeviceRemove(struct hv_device *ChildDevice)
90 {
91         struct vmbus_driver *vmbusDriver = (struct vmbus_driver *)gDriver;
92
93         vmbusDriver->OnChildDeviceRemove(ChildDevice);
94 }
95
96 /*
97  * VmbusOnDeviceAdd - Callback when the root bus device is added
98  */
99 static int VmbusOnDeviceAdd(struct hv_device *dev, void *AdditionalInfo)
100 {
101         u32 *irqvector = AdditionalInfo;
102         int ret;
103
104         gDevice = dev;
105
106         memcpy(&gDevice->deviceType, &gVmbusDeviceType, sizeof(struct hv_guid));
107         memcpy(&gDevice->deviceInstance, &gVmbusDeviceId,
108                sizeof(struct hv_guid));
109
110         /* strcpy(dev->name, "vmbus"); */
111         /* SynIC setup... */
112         on_each_cpu(HvSynicInit, (void *)irqvector, 1);
113
114         /* Connect to VMBus in the root partition */
115         ret = VmbusConnect();
116
117         /* VmbusSendEvent(device->localPortId+1); */
118         return ret;
119 }
120
121 /*
122  * VmbusOnDeviceRemove - Callback when the root bus device is removed
123  */
124 static int VmbusOnDeviceRemove(struct hv_device *dev)
125 {
126         int ret = 0;
127
128         vmbus_release_unattached_channels();
129         VmbusDisconnect();
130         on_each_cpu(HvSynicCleanup, NULL, 1);
131         return ret;
132 }
133
134 /*
135  * VmbusOnCleanup - Perform any cleanup when the driver is removed
136  */
137 static void VmbusOnCleanup(struct hv_driver *drv)
138 {
139         /* struct vmbus_driver *driver = (struct vmbus_driver *)drv; */
140
141         HvCleanup();
142 }
143
144 /*
145  * VmbusOnMsgDPC - DPC routine to handle messages from the hypervisior
146  */
147 static void VmbusOnMsgDPC(struct hv_driver *drv)
148 {
149         int cpu = smp_processor_id();
150         void *page_addr = gHvContext.synICMessagePage[cpu];
151         struct hv_message *msg = (struct hv_message *)page_addr +
152                                   VMBUS_MESSAGE_SINT;
153         struct hv_message *copied;
154
155         while (1) {
156                 if (msg->Header.MessageType == HvMessageTypeNone) {
157                         /* no msg */
158                         break;
159                 } else {
160                         copied = kmemdup(msg, sizeof(*copied), GFP_ATOMIC);
161                         if (copied == NULL)
162                                 continue;
163
164                         osd_schedule_callback(gVmbusConnection.WorkQueue,
165                                               vmbus_onmessage,
166                                               (void *)copied);
167                 }
168
169                 msg->Header.MessageType = HvMessageTypeNone;
170
171                 /*
172                  * Make sure the write to MessageType (ie set to
173                  * HvMessageTypeNone) happens before we read the
174                  * MessagePending and EOMing. Otherwise, the EOMing
175                  * will not deliver any more messages since there is
176                  * no empty slot
177                  */
178                 mb();
179
180                 if (msg->Header.MessageFlags.MessagePending) {
181                         /*
182                          * This will cause message queue rescan to
183                          * possibly deliver another msg from the
184                          * hypervisor
185                          */
186                         wrmsrl(HV_X64_MSR_EOM, 0);
187                 }
188         }
189 }
190
191 /*
192  * VmbusOnEventDPC - DPC routine to handle events from the hypervisior
193  */
194 static void VmbusOnEventDPC(struct hv_driver *drv)
195 {
196         /* TODO: Process any events */
197         VmbusOnEvents();
198 }
199
200 /*
201  * VmbusOnISR - ISR routine
202  */
203 static int VmbusOnISR(struct hv_driver *drv)
204 {
205         int ret = 0;
206         int cpu = smp_processor_id();
207         void *page_addr;
208         struct hv_message *msg;
209         union hv_synic_event_flags *event;
210
211         page_addr = gHvContext.synICMessagePage[cpu];
212         msg = (struct hv_message *)page_addr + VMBUS_MESSAGE_SINT;
213
214         /* Check if there are actual msgs to be process */
215         if (msg->Header.MessageType != HvMessageTypeNone) {
216                 DPRINT_DBG(VMBUS, "received msg type %d size %d",
217                                 msg->Header.MessageType,
218                                 msg->Header.PayloadSize);
219                 ret |= 0x1;
220         }
221
222         /* TODO: Check if there are events to be process */
223         page_addr = gHvContext.synICEventPage[cpu];
224         event = (union hv_synic_event_flags *)page_addr + VMBUS_MESSAGE_SINT;
225
226         /* Since we are a child, we only need to check bit 0 */
227         if (test_and_clear_bit(0, (unsigned long *) &event->Flags32[0])) {
228                 DPRINT_DBG(VMBUS, "received event %d", event->Flags32[0]);
229                 ret |= 0x2;
230         }
231
232         return ret;
233 }
234
235 /*
236  * VmbusInitialize - Main entry point
237  */
238 int VmbusInitialize(struct hv_driver *drv)
239 {
240         struct vmbus_driver *driver = (struct vmbus_driver *)drv;
241         int ret;
242
243         DPRINT_INFO(VMBUS, "+++++++ HV Driver version = %s +++++++",
244                     HV_DRV_VERSION);
245         DPRINT_INFO(VMBUS, "+++++++ Vmbus supported version = %d +++++++",
246                         VMBUS_REVISION_NUMBER);
247         DPRINT_INFO(VMBUS, "+++++++ Vmbus using SINT %d +++++++",
248                         VMBUS_MESSAGE_SINT);
249         DPRINT_DBG(VMBUS, "sizeof(vmbus_channel_packet_page_buffer)=%zd, "
250                         "sizeof(VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER)=%zd",
251                         sizeof(struct vmbus_channel_packet_page_buffer),
252                         sizeof(struct vmbus_channel_packet_multipage_buffer));
253
254         drv->name = gDriverName;
255         memcpy(&drv->deviceType, &gVmbusDeviceType, sizeof(struct hv_guid));
256
257         /* Setup dispatch table */
258         driver->Base.OnDeviceAdd        = VmbusOnDeviceAdd;
259         driver->Base.OnDeviceRemove     = VmbusOnDeviceRemove;
260         driver->Base.OnCleanup          = VmbusOnCleanup;
261         driver->OnIsr                   = VmbusOnISR;
262         driver->OnMsgDpc                = VmbusOnMsgDPC;
263         driver->OnEventDpc              = VmbusOnEventDPC;
264         driver->GetChannelOffers        = VmbusGetChannelOffers;
265
266         /* Hypervisor initialization...setup hypercall page..etc */
267         ret = HvInit();
268         if (ret != 0)
269                 DPRINT_ERR(VMBUS, "Unable to initialize the hypervisor - 0x%x",
270                                 ret);
271         gDriver = drv;
272
273         return ret;
274 }