blob: 4594277f555e3120cbf0c2a71fb33050dcdd684f [file] [log] [blame]
Hank Janssenfceaf242009-07-13 15:34:54 -07001/*
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 * Hank Janssen <hjanssen@microsoft.com>
20 *
21 */
22
Greg Kroah-Hartman5654e932009-07-14 15:08:20 -070023#include <linux/kernel.h>
Greg Kroah-Hartman0ffa63b2009-07-15 11:06:01 -070024#include <linux/mm.h>
Greg Kroah-Hartmanb4362c92009-07-16 11:50:41 -070025#include <linux/delay.h>
Greg Kroah-Hartmanfa56d362009-07-29 15:39:27 -070026#include <asm/io.h>
Greg Kroah-Hartman4983b392009-08-19 16:14:47 -070027#include "osd.h"
Greg Kroah-Hartmanc86f3e22009-07-14 10:59:56 -070028#include "include/logging.h"
Hank Janssenfceaf242009-07-13 15:34:54 -070029#include "NetVsc.h"
30#include "RndisFilter.h"
31
32
Bill Pemberton454f18a2009-07-27 16:47:24 -040033/* Globals */
Hank Janssenfceaf242009-07-13 15:34:54 -070034static const char* gDriverName="netvsc";
35
Bill Pemberton454f18a2009-07-27 16:47:24 -040036/* {F8615163-DF3E-46c5-913F-F2D2F965ED0E} */
Greg Kroah-Hartmancaf26a32009-08-19 16:17:03 -070037static const struct hv_guid gNetVscDeviceType = {
38 .data = {
39 0x63, 0x51, 0x61, 0xF8, 0x3E, 0xDF, 0xc5, 0x46,
40 0x91, 0x3F, 0xF2, 0xD2, 0xF9, 0x65, 0xED, 0x0E
41 }
Hank Janssenfceaf242009-07-13 15:34:54 -070042};
43
44
Bill Pemberton454f18a2009-07-27 16:47:24 -040045/* Internal routines */
Hank Janssenfceaf242009-07-13 15:34:54 -070046static int
47NetVscOnDeviceAdd(
Nicolas Palix3d3b5512009-07-28 17:32:53 +020048 struct hv_device *Device,
Hank Janssenfceaf242009-07-13 15:34:54 -070049 void *AdditionalInfo
50 );
51
52static int
53NetVscOnDeviceRemove(
Nicolas Palix3d3b5512009-07-28 17:32:53 +020054 struct hv_device *Device
Hank Janssenfceaf242009-07-13 15:34:54 -070055 );
56
57static void
58NetVscOnCleanup(
Nicolas Palix775ef252009-07-29 14:09:45 +020059 struct hv_driver *Driver
Hank Janssenfceaf242009-07-13 15:34:54 -070060 );
61
62static void
63NetVscOnChannelCallback(
Greg Kroah-Hartman8282c402009-07-14 15:06:28 -070064 void * context
Hank Janssenfceaf242009-07-13 15:34:54 -070065 );
66
67static int
68NetVscInitializeSendBufferWithNetVsp(
Nicolas Palix3d3b5512009-07-28 17:32:53 +020069 struct hv_device *Device
Hank Janssenfceaf242009-07-13 15:34:54 -070070 );
71
72static int
73NetVscInitializeReceiveBufferWithNetVsp(
Nicolas Palix3d3b5512009-07-28 17:32:53 +020074 struct hv_device *Device
Hank Janssenfceaf242009-07-13 15:34:54 -070075 );
76
77static int
78NetVscDestroySendBuffer(
Bill Pembertond1af1db72009-07-27 16:47:44 -040079 struct NETVSC_DEVICE *NetDevice
Hank Janssenfceaf242009-07-13 15:34:54 -070080 );
81
82static int
83NetVscDestroyReceiveBuffer(
Bill Pembertond1af1db72009-07-27 16:47:44 -040084 struct NETVSC_DEVICE *NetDevice
Hank Janssenfceaf242009-07-13 15:34:54 -070085 );
86
87static int
88NetVscConnectToVsp(
Nicolas Palix3d3b5512009-07-28 17:32:53 +020089 struct hv_device *Device
Hank Janssenfceaf242009-07-13 15:34:54 -070090 );
91
92static void
93NetVscOnSendCompletion(
Nicolas Palix3d3b5512009-07-28 17:32:53 +020094 struct hv_device *Device,
Hank Janssenfceaf242009-07-13 15:34:54 -070095 VMPACKET_DESCRIPTOR *Packet
96 );
97
98static int
99NetVscOnSend(
Nicolas Palix3d3b5512009-07-28 17:32:53 +0200100 struct hv_device *Device,
Nicolas Palix4193d4f2009-07-29 14:10:10 +0200101 struct hv_netvsc_packet *Packet
Hank Janssenfceaf242009-07-13 15:34:54 -0700102 );
103
104static void
105NetVscOnReceive(
Nicolas Palix3d3b5512009-07-28 17:32:53 +0200106 struct hv_device *Device,
Hank Janssenfceaf242009-07-13 15:34:54 -0700107 VMPACKET_DESCRIPTOR *Packet
108 );
109
110static void
111NetVscOnReceiveCompletion(
Greg Kroah-Hartman8282c402009-07-14 15:06:28 -0700112 void * Context
Hank Janssenfceaf242009-07-13 15:34:54 -0700113 );
114
115static void
116NetVscSendReceiveCompletion(
Nicolas Palix3d3b5512009-07-28 17:32:53 +0200117 struct hv_device *Device,
Greg Kroah-Hartman59471432009-07-14 15:10:26 -0700118 u64 TransactionId
Hank Janssenfceaf242009-07-13 15:34:54 -0700119 );
120
Nicolas Palix3d3b5512009-07-28 17:32:53 +0200121static inline struct NETVSC_DEVICE *AllocNetDevice(struct hv_device *Device)
Hank Janssenfceaf242009-07-13 15:34:54 -0700122{
Bill Pembertond1af1db72009-07-27 16:47:44 -0400123 struct NETVSC_DEVICE *netDevice;
Hank Janssenfceaf242009-07-13 15:34:54 -0700124
Bill Pembertond1af1db72009-07-27 16:47:44 -0400125 netDevice = kzalloc(sizeof(struct NETVSC_DEVICE), GFP_KERNEL);
Hank Janssenfceaf242009-07-13 15:34:54 -0700126 if (!netDevice)
127 return NULL;
128
Bill Pemberton454f18a2009-07-27 16:47:24 -0400129 /* Set to 2 to allow both inbound and outbound traffic */
Bill Pembertonf4888412009-07-29 17:00:12 -0400130 atomic_cmpxchg(&netDevice->RefCount, 0, 2);
Hank Janssenfceaf242009-07-13 15:34:54 -0700131
132 netDevice->Device = Device;
133 Device->Extension = netDevice;
134
135 return netDevice;
136}
137
Bill Pembertond1af1db72009-07-27 16:47:44 -0400138static inline void FreeNetDevice(struct NETVSC_DEVICE *Device)
Hank Janssenfceaf242009-07-13 15:34:54 -0700139{
Bill Pembertonf4888412009-07-29 17:00:12 -0400140 ASSERT(atomic_read(&Device->RefCount) == 0);
Hank Janssenfceaf242009-07-13 15:34:54 -0700141 Device->Device->Extension = NULL;
Greg Kroah-Hartman8c69f522009-07-15 12:48:29 -0700142 kfree(Device);
Hank Janssenfceaf242009-07-13 15:34:54 -0700143}
144
145
Bill Pemberton454f18a2009-07-27 16:47:24 -0400146/* Get the net device object iff exists and its refcount > 1 */
Nicolas Palix3d3b5512009-07-28 17:32:53 +0200147static inline struct NETVSC_DEVICE *GetOutboundNetDevice(struct hv_device *Device)
Hank Janssenfceaf242009-07-13 15:34:54 -0700148{
Bill Pembertond1af1db72009-07-27 16:47:44 -0400149 struct NETVSC_DEVICE *netDevice;
Hank Janssenfceaf242009-07-13 15:34:54 -0700150
Bill Pembertond1af1db72009-07-27 16:47:44 -0400151 netDevice = (struct NETVSC_DEVICE*)Device->Extension;
Bill Pembertonf4888412009-07-29 17:00:12 -0400152 if (netDevice && atomic_read(&netDevice->RefCount) > 1)
153 atomic_inc(&netDevice->RefCount);
Hank Janssenfceaf242009-07-13 15:34:54 -0700154 else
Hank Janssenfceaf242009-07-13 15:34:54 -0700155 netDevice = NULL;
Hank Janssenfceaf242009-07-13 15:34:54 -0700156
157 return netDevice;
158}
159
Bill Pemberton454f18a2009-07-27 16:47:24 -0400160/* Get the net device object iff exists and its refcount > 0 */
Nicolas Palix3d3b5512009-07-28 17:32:53 +0200161static inline struct NETVSC_DEVICE *GetInboundNetDevice(struct hv_device *Device)
Hank Janssenfceaf242009-07-13 15:34:54 -0700162{
Bill Pembertond1af1db72009-07-27 16:47:44 -0400163 struct NETVSC_DEVICE *netDevice;
Hank Janssenfceaf242009-07-13 15:34:54 -0700164
Bill Pembertond1af1db72009-07-27 16:47:44 -0400165 netDevice = (struct NETVSC_DEVICE*)Device->Extension;
Bill Pembertonf4888412009-07-29 17:00:12 -0400166 if (netDevice && atomic_read(&netDevice->RefCount))
167 atomic_inc(&netDevice->RefCount);
Hank Janssenfceaf242009-07-13 15:34:54 -0700168 else
Hank Janssenfceaf242009-07-13 15:34:54 -0700169 netDevice = NULL;
Hank Janssenfceaf242009-07-13 15:34:54 -0700170
171 return netDevice;
172}
173
Nicolas Palix3d3b5512009-07-28 17:32:53 +0200174static inline void PutNetDevice(struct hv_device *Device)
Hank Janssenfceaf242009-07-13 15:34:54 -0700175{
Bill Pembertond1af1db72009-07-27 16:47:44 -0400176 struct NETVSC_DEVICE *netDevice;
Hank Janssenfceaf242009-07-13 15:34:54 -0700177
Bill Pembertond1af1db72009-07-27 16:47:44 -0400178 netDevice = (struct NETVSC_DEVICE*)Device->Extension;
Hank Janssenfceaf242009-07-13 15:34:54 -0700179 ASSERT(netDevice);
180
Bill Pembertonf4888412009-07-29 17:00:12 -0400181 atomic_dec(&netDevice->RefCount);
Hank Janssenfceaf242009-07-13 15:34:54 -0700182}
183
Nicolas Palix3d3b5512009-07-28 17:32:53 +0200184static inline struct NETVSC_DEVICE *ReleaseOutboundNetDevice(struct hv_device *Device)
Hank Janssenfceaf242009-07-13 15:34:54 -0700185{
Bill Pembertond1af1db72009-07-27 16:47:44 -0400186 struct NETVSC_DEVICE *netDevice;
Hank Janssenfceaf242009-07-13 15:34:54 -0700187
Bill Pembertond1af1db72009-07-27 16:47:44 -0400188 netDevice = (struct NETVSC_DEVICE*)Device->Extension;
Hank Janssenfceaf242009-07-13 15:34:54 -0700189 if (netDevice == NULL)
190 return NULL;
191
Bill Pemberton454f18a2009-07-27 16:47:24 -0400192 /* Busy wait until the ref drop to 2, then set it to 1 */
Bill Pembertonf4888412009-07-29 17:00:12 -0400193 while (atomic_cmpxchg(&netDevice->RefCount, 2, 1) != 2)
Hank Janssenfceaf242009-07-13 15:34:54 -0700194 {
Greg Kroah-Hartmanb4362c92009-07-16 11:50:41 -0700195 udelay(100);
Hank Janssenfceaf242009-07-13 15:34:54 -0700196 }
197
198 return netDevice;
199}
200
Nicolas Palix3d3b5512009-07-28 17:32:53 +0200201static inline struct NETVSC_DEVICE *ReleaseInboundNetDevice(struct hv_device *Device)
Hank Janssenfceaf242009-07-13 15:34:54 -0700202{
Bill Pembertond1af1db72009-07-27 16:47:44 -0400203 struct NETVSC_DEVICE *netDevice;
Hank Janssenfceaf242009-07-13 15:34:54 -0700204
Bill Pembertond1af1db72009-07-27 16:47:44 -0400205 netDevice = (struct NETVSC_DEVICE*)Device->Extension;
Hank Janssenfceaf242009-07-13 15:34:54 -0700206 if (netDevice == NULL)
207 return NULL;
208
Bill Pemberton454f18a2009-07-27 16:47:24 -0400209 /* Busy wait until the ref drop to 1, then set it to 0 */
Bill Pembertonf4888412009-07-29 17:00:12 -0400210 while (atomic_cmpxchg(&netDevice->RefCount, 1, 0) != 1)
Hank Janssenfceaf242009-07-13 15:34:54 -0700211 {
Greg Kroah-Hartmanb4362c92009-07-16 11:50:41 -0700212 udelay(100);
Hank Janssenfceaf242009-07-13 15:34:54 -0700213 }
214
215 Device->Extension = NULL;
216 return netDevice;
217}
218
219/*++;
220
221
222Name:
223 NetVscInitialize()
224
225Description:
226 Main entry point
227
228--*/
229int
230NetVscInitialize(
Nicolas Palix775ef252009-07-29 14:09:45 +0200231 struct hv_driver *drv
Hank Janssenfceaf242009-07-13 15:34:54 -0700232 )
233{
234 NETVSC_DRIVER_OBJECT* driver = (NETVSC_DRIVER_OBJECT*)drv;
235 int ret=0;
236
237 DPRINT_ENTER(NETVSC);
238
Nicolas Palix4193d4f2009-07-29 14:10:10 +0200239 DPRINT_DBG(NETVSC, "sizeof(struct hv_netvsc_packet)=%zd, sizeof(NVSP_MESSAGE)=%zd, sizeof(VMTRANSFER_PAGE_PACKET_HEADER)=%zd",
240 sizeof(struct hv_netvsc_packet), sizeof(NVSP_MESSAGE), sizeof(VMTRANSFER_PAGE_PACKET_HEADER));
Hank Janssenfceaf242009-07-13 15:34:54 -0700241
Bill Pemberton454f18a2009-07-27 16:47:24 -0400242 /* Make sure we are at least 2 pages since 1 page is used for control */
Hank Janssenfceaf242009-07-13 15:34:54 -0700243 ASSERT(driver->RingBufferSize >= (PAGE_SIZE << 1));
244
245 drv->name = gDriverName;
Greg Kroah-Hartmancaf26a32009-08-19 16:17:03 -0700246 memcpy(&drv->deviceType, &gNetVscDeviceType, sizeof(struct hv_guid));
Hank Janssenfceaf242009-07-13 15:34:54 -0700247
Bill Pemberton454f18a2009-07-27 16:47:24 -0400248 /* Make sure it is set by the caller */
Hank Janssenfceaf242009-07-13 15:34:54 -0700249 ASSERT(driver->OnReceiveCallback);
250 ASSERT(driver->OnLinkStatusChanged);
251
Bill Pemberton454f18a2009-07-27 16:47:24 -0400252 /* Setup the dispatch table */
Hank Janssenfceaf242009-07-13 15:34:54 -0700253 driver->Base.OnDeviceAdd = NetVscOnDeviceAdd;
254 driver->Base.OnDeviceRemove = NetVscOnDeviceRemove;
255 driver->Base.OnCleanup = NetVscOnCleanup;
256
257 driver->OnSend = NetVscOnSend;
258
259 RndisFilterInit(driver);
260
261 DPRINT_EXIT(NETVSC);
262
263 return ret;
264}
265
266static int
267NetVscInitializeReceiveBufferWithNetVsp(
Nicolas Palix3d3b5512009-07-28 17:32:53 +0200268 struct hv_device *Device
Hank Janssenfceaf242009-07-13 15:34:54 -0700269 )
270{
271 int ret=0;
Bill Pembertond1af1db72009-07-27 16:47:44 -0400272 struct NETVSC_DEVICE *netDevice;
Hank Janssenfceaf242009-07-13 15:34:54 -0700273 NVSP_MESSAGE *initPacket;
274
275 DPRINT_ENTER(NETVSC);
276
277 netDevice = GetOutboundNetDevice(Device);
278 if (!netDevice)
279 {
280 DPRINT_ERR(NETVSC, "unable to get net device...device being destroyed?");
281 DPRINT_EXIT(NETVSC);
282 return -1;
283 }
284 ASSERT(netDevice->ReceiveBufferSize > 0);
Bill Pemberton454f18a2009-07-27 16:47:24 -0400285 ASSERT((netDevice->ReceiveBufferSize & (PAGE_SIZE-1)) == 0); /* page-size grandularity */
Hank Janssenfceaf242009-07-13 15:34:54 -0700286
Greg Kroah-Hartmanbfc30aa2009-07-29 15:40:18 -0700287 netDevice->ReceiveBuffer = osd_PageAlloc(netDevice->ReceiveBufferSize >> PAGE_SHIFT);
Hank Janssenfceaf242009-07-13 15:34:54 -0700288 if (!netDevice->ReceiveBuffer)
289 {
290 DPRINT_ERR(NETVSC, "unable to allocate receive buffer of size %d", netDevice->ReceiveBufferSize);
291 ret = -1;
292 goto Cleanup;
293 }
Bill Pemberton454f18a2009-07-27 16:47:24 -0400294 ASSERT(((unsigned long)netDevice->ReceiveBuffer & (PAGE_SIZE-1)) == 0); /* page-aligned buffer */
Hank Janssenfceaf242009-07-13 15:34:54 -0700295
296 DPRINT_INFO(NETVSC, "Establishing receive buffer's GPADL...");
297
Bill Pemberton454f18a2009-07-27 16:47:24 -0400298 /*
299 * Establish the gpadl handle for this buffer on this
300 * channel. Note: This call uses the vmbus connection rather
301 * than the channel to establish the gpadl handle.
302 */
Hank Janssenfceaf242009-07-13 15:34:54 -0700303 ret = Device->Driver->VmbusChannelInterface.EstablishGpadl(Device,
304 netDevice->ReceiveBuffer,
305 netDevice->ReceiveBufferSize,
306 &netDevice->ReceiveBufferGpadlHandle);
307
308 if (ret != 0)
309 {
310 DPRINT_ERR(NETVSC, "unable to establish receive buffer's gpadl");
311 goto Cleanup;
312 }
313
Greg Kroah-Hartmanbfc30aa2009-07-29 15:40:18 -0700314 /* osd_WaitEventWait(ext->ChannelInitEvent); */
Hank Janssenfceaf242009-07-13 15:34:54 -0700315
Bill Pemberton454f18a2009-07-27 16:47:24 -0400316 /* Notify the NetVsp of the gpadl handle */
Hank Janssenfceaf242009-07-13 15:34:54 -0700317 DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendReceiveBuffer...");
318
319 initPacket = &netDevice->ChannelInitPacket;
320
321 memset(initPacket, 0, sizeof(NVSP_MESSAGE));
322
323 initPacket->Header.MessageType = NvspMessage1TypeSendReceiveBuffer;
324 initPacket->Messages.Version1Messages.SendReceiveBuffer.GpadlHandle = netDevice->ReceiveBufferGpadlHandle;
325 initPacket->Messages.Version1Messages.SendReceiveBuffer.Id = NETVSC_RECEIVE_BUFFER_ID;
326
Bill Pemberton454f18a2009-07-27 16:47:24 -0400327 /* Send the gpadl notification request */
Hank Janssenfceaf242009-07-13 15:34:54 -0700328 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
329 initPacket,
330 sizeof(NVSP_MESSAGE),
Greg Kroah-Hartmanc4b0bc92009-07-14 15:12:46 -0700331 (unsigned long)initPacket,
Hank Janssenfceaf242009-07-13 15:34:54 -0700332 VmbusPacketTypeDataInBand,
333 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
334 if (ret != 0)
335 {
336 DPRINT_ERR(NETVSC, "unable to send receive buffer's gpadl to netvsp");
337 goto Cleanup;
338 }
339
Greg Kroah-Hartmanbfc30aa2009-07-29 15:40:18 -0700340 osd_WaitEventWait(netDevice->ChannelInitEvent);
Hank Janssenfceaf242009-07-13 15:34:54 -0700341
Bill Pemberton454f18a2009-07-27 16:47:24 -0400342 /* Check the response */
Hank Janssenfceaf242009-07-13 15:34:54 -0700343 if (initPacket->Messages.Version1Messages.SendReceiveBufferComplete.Status != NvspStatusSuccess)
344 {
345 DPRINT_ERR(NETVSC,
346 "Unable to complete receive buffer initialzation with NetVsp - status %d",
347 initPacket->Messages.Version1Messages.SendReceiveBufferComplete.Status);
348 ret = -1;
349 goto Cleanup;
350 }
351
Bill Pemberton454f18a2009-07-27 16:47:24 -0400352 /* Parse the response */
Hank Janssenfceaf242009-07-13 15:34:54 -0700353 ASSERT(netDevice->ReceiveSectionCount == 0);
354 ASSERT(netDevice->ReceiveSections == NULL);
355
356 netDevice->ReceiveSectionCount = initPacket->Messages.Version1Messages.SendReceiveBufferComplete.NumSections;
357
Greg Kroah-Hartmane40d37c2009-07-15 12:47:22 -0700358 netDevice->ReceiveSections = kmalloc(netDevice->ReceiveSectionCount * sizeof(NVSP_1_RECEIVE_BUFFER_SECTION), GFP_KERNEL);
Hank Janssenfceaf242009-07-13 15:34:54 -0700359 if (netDevice->ReceiveSections == NULL)
360 {
361 ret = -1;
362 goto Cleanup;
363 }
364
365 memcpy(netDevice->ReceiveSections,
366 initPacket->Messages.Version1Messages.SendReceiveBufferComplete.Sections,
367 netDevice->ReceiveSectionCount * sizeof(NVSP_1_RECEIVE_BUFFER_SECTION));
368
369 DPRINT_INFO(NETVSC,
370 "Receive sections info (count %d, offset %d, endoffset %d, suballoc size %d, num suballocs %d)",
371 netDevice->ReceiveSectionCount, netDevice->ReceiveSections[0].Offset, netDevice->ReceiveSections[0].EndOffset,
372 netDevice->ReceiveSections[0].SubAllocationSize, netDevice->ReceiveSections[0].NumSubAllocations);
373
374
Bill Pemberton454f18a2009-07-27 16:47:24 -0400375 /* For 1st release, there should only be 1 section that represents the entire receive buffer */
Hank Janssenfceaf242009-07-13 15:34:54 -0700376 if (netDevice->ReceiveSectionCount != 1 ||
377 netDevice->ReceiveSections->Offset != 0 )
378 {
379 ret = -1;
380 goto Cleanup;
381 }
382
383 goto Exit;
384
385Cleanup:
386 NetVscDestroyReceiveBuffer(netDevice);
387
388Exit:
389 PutNetDevice(Device);
390 DPRINT_EXIT(NETVSC);
391 return ret;
392}
393
394
395static int
396NetVscInitializeSendBufferWithNetVsp(
Nicolas Palix3d3b5512009-07-28 17:32:53 +0200397 struct hv_device *Device
Hank Janssenfceaf242009-07-13 15:34:54 -0700398 )
399{
400 int ret=0;
Bill Pembertond1af1db72009-07-27 16:47:44 -0400401 struct NETVSC_DEVICE *netDevice;
Hank Janssenfceaf242009-07-13 15:34:54 -0700402 NVSP_MESSAGE *initPacket;
403
404 DPRINT_ENTER(NETVSC);
405
406 netDevice = GetOutboundNetDevice(Device);
407 if (!netDevice)
408 {
409 DPRINT_ERR(NETVSC, "unable to get net device...device being destroyed?");
410 DPRINT_EXIT(NETVSC);
411 return -1;
412 }
413 ASSERT(netDevice->SendBufferSize > 0);
Bill Pemberton454f18a2009-07-27 16:47:24 -0400414 ASSERT((netDevice->SendBufferSize & (PAGE_SIZE-1)) == 0); /* page-size grandularity */
Hank Janssenfceaf242009-07-13 15:34:54 -0700415
Greg Kroah-Hartmanbfc30aa2009-07-29 15:40:18 -0700416 netDevice->SendBuffer = osd_PageAlloc(netDevice->SendBufferSize >> PAGE_SHIFT);
Hank Janssenfceaf242009-07-13 15:34:54 -0700417 if (!netDevice->SendBuffer)
418 {
419 DPRINT_ERR(NETVSC, "unable to allocate send buffer of size %d", netDevice->SendBufferSize);
420 ret = -1;
421 goto Cleanup;
422 }
Bill Pemberton454f18a2009-07-27 16:47:24 -0400423 ASSERT(((unsigned long)netDevice->SendBuffer & (PAGE_SIZE-1)) == 0); /* page-aligned buffer */
Hank Janssenfceaf242009-07-13 15:34:54 -0700424
425 DPRINT_INFO(NETVSC, "Establishing send buffer's GPADL...");
426
Bill Pemberton454f18a2009-07-27 16:47:24 -0400427 /*
428 * Establish the gpadl handle for this buffer on this
429 * channel. Note: This call uses the vmbus connection rather
430 * than the channel to establish the gpadl handle.
431 */
Hank Janssenfceaf242009-07-13 15:34:54 -0700432 ret = Device->Driver->VmbusChannelInterface.EstablishGpadl(Device,
Bill Pemberton454f18a2009-07-27 16:47:24 -0400433 netDevice->SendBuffer,
434 netDevice->SendBufferSize,
435 &netDevice->SendBufferGpadlHandle);
Hank Janssenfceaf242009-07-13 15:34:54 -0700436
437 if (ret != 0)
438 {
439 DPRINT_ERR(NETVSC, "unable to establish send buffer's gpadl");
440 goto Cleanup;
441 }
442
Greg Kroah-Hartmanbfc30aa2009-07-29 15:40:18 -0700443 /* osd_WaitEventWait(ext->ChannelInitEvent); */
Hank Janssenfceaf242009-07-13 15:34:54 -0700444
Bill Pemberton454f18a2009-07-27 16:47:24 -0400445 /* Notify the NetVsp of the gpadl handle */
Hank Janssenfceaf242009-07-13 15:34:54 -0700446 DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendSendBuffer...");
447
448 initPacket = &netDevice->ChannelInitPacket;
449
450 memset(initPacket, 0, sizeof(NVSP_MESSAGE));
451
452 initPacket->Header.MessageType = NvspMessage1TypeSendSendBuffer;
453 initPacket->Messages.Version1Messages.SendReceiveBuffer.GpadlHandle = netDevice->SendBufferGpadlHandle;
454 initPacket->Messages.Version1Messages.SendReceiveBuffer.Id = NETVSC_SEND_BUFFER_ID;
455
Bill Pemberton454f18a2009-07-27 16:47:24 -0400456 /* Send the gpadl notification request */
Hank Janssenfceaf242009-07-13 15:34:54 -0700457 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
458 initPacket,
459 sizeof(NVSP_MESSAGE),
Greg Kroah-Hartmanc4b0bc92009-07-14 15:12:46 -0700460 (unsigned long)initPacket,
Hank Janssenfceaf242009-07-13 15:34:54 -0700461 VmbusPacketTypeDataInBand,
462 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
463 if (ret != 0)
464 {
465 DPRINT_ERR(NETVSC, "unable to send receive buffer's gpadl to netvsp");
466 goto Cleanup;
467 }
468
Greg Kroah-Hartmanbfc30aa2009-07-29 15:40:18 -0700469 osd_WaitEventWait(netDevice->ChannelInitEvent);
Hank Janssenfceaf242009-07-13 15:34:54 -0700470
Bill Pemberton454f18a2009-07-27 16:47:24 -0400471 /* Check the response */
Hank Janssenfceaf242009-07-13 15:34:54 -0700472 if (initPacket->Messages.Version1Messages.SendSendBufferComplete.Status != NvspStatusSuccess)
473 {
474 DPRINT_ERR(NETVSC,
475 "Unable to complete send buffer initialzation with NetVsp - status %d",
476 initPacket->Messages.Version1Messages.SendSendBufferComplete.Status);
477 ret = -1;
478 goto Cleanup;
479 }
480
481 netDevice->SendSectionSize = initPacket->Messages.Version1Messages.SendSendBufferComplete.SectionSize;
482
483 goto Exit;
484
485Cleanup:
486 NetVscDestroySendBuffer(netDevice);
487
488Exit:
489 PutNetDevice(Device);
490 DPRINT_EXIT(NETVSC);
491 return ret;
492}
493
494static int
495NetVscDestroyReceiveBuffer(
Bill Pembertond1af1db72009-07-27 16:47:44 -0400496 struct NETVSC_DEVICE *NetDevice
Hank Janssenfceaf242009-07-13 15:34:54 -0700497 )
498{
499 NVSP_MESSAGE *revokePacket;
500 int ret=0;
501
502
503 DPRINT_ENTER(NETVSC);
504
Bill Pemberton454f18a2009-07-27 16:47:24 -0400505 /*
506 * If we got a section count, it means we received a
507 * SendReceiveBufferComplete msg (ie sent
508 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
509 * to send a revoke msg here
510 */
Hank Janssenfceaf242009-07-13 15:34:54 -0700511 if (NetDevice->ReceiveSectionCount)
512 {
513 DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeRevokeReceiveBuffer...");
514
Bill Pemberton454f18a2009-07-27 16:47:24 -0400515 /* Send the revoke receive buffer */
Hank Janssenfceaf242009-07-13 15:34:54 -0700516 revokePacket = &NetDevice->RevokePacket;
517 memset(revokePacket, 0, sizeof(NVSP_MESSAGE));
518
519 revokePacket->Header.MessageType = NvspMessage1TypeRevokeReceiveBuffer;
520 revokePacket->Messages.Version1Messages.RevokeReceiveBuffer.Id = NETVSC_RECEIVE_BUFFER_ID;
521
522 ret = NetDevice->Device->Driver->VmbusChannelInterface.SendPacket(NetDevice->Device,
523 revokePacket,
524 sizeof(NVSP_MESSAGE),
Greg Kroah-Hartmanc4b0bc92009-07-14 15:12:46 -0700525 (unsigned long)revokePacket,
Hank Janssenfceaf242009-07-13 15:34:54 -0700526 VmbusPacketTypeDataInBand,
527 0);
Bill Pemberton454f18a2009-07-27 16:47:24 -0400528 /*
529 * If we failed here, we might as well return and
530 * have a leak rather than continue and a bugchk
531 */
Hank Janssenfceaf242009-07-13 15:34:54 -0700532 if (ret != 0)
533 {
534 DPRINT_ERR(NETVSC, "unable to send revoke receive buffer to netvsp");
535 DPRINT_EXIT(NETVSC);
536 return -1;
537 }
538 }
539
Bill Pemberton454f18a2009-07-27 16:47:24 -0400540 /* Teardown the gpadl on the vsp end */
Hank Janssenfceaf242009-07-13 15:34:54 -0700541 if (NetDevice->ReceiveBufferGpadlHandle)
542 {
543 DPRINT_INFO(NETVSC, "Tearing down receive buffer's GPADL...");
544
545 ret = NetDevice->Device->Driver->VmbusChannelInterface.TeardownGpadl(NetDevice->Device,
546 NetDevice->ReceiveBufferGpadlHandle);
547
Bill Pemberton454f18a2009-07-27 16:47:24 -0400548 /* If we failed here, we might as well return and have a leak rather than continue and a bugchk */
Hank Janssenfceaf242009-07-13 15:34:54 -0700549 if (ret != 0)
550 {
551 DPRINT_ERR(NETVSC, "unable to teardown receive buffer's gpadl");
552 DPRINT_EXIT(NETVSC);
553 return -1;
554 }
555 NetDevice->ReceiveBufferGpadlHandle = 0;
556 }
557
558 if (NetDevice->ReceiveBuffer)
559 {
560 DPRINT_INFO(NETVSC, "Freeing up receive buffer...");
561
Bill Pemberton454f18a2009-07-27 16:47:24 -0400562 /* Free up the receive buffer */
Greg Kroah-Hartmanbfc30aa2009-07-29 15:40:18 -0700563 osd_PageFree(NetDevice->ReceiveBuffer, NetDevice->ReceiveBufferSize >> PAGE_SHIFT);
Hank Janssenfceaf242009-07-13 15:34:54 -0700564 NetDevice->ReceiveBuffer = NULL;
565 }
566
567 if (NetDevice->ReceiveSections)
568 {
Greg Kroah-Hartman8c69f522009-07-15 12:48:29 -0700569 kfree(NetDevice->ReceiveSections);
Hank Janssenfceaf242009-07-13 15:34:54 -0700570 NetDevice->ReceiveSections = NULL;
571 NetDevice->ReceiveSectionCount = 0;
572 }
573
574 DPRINT_EXIT(NETVSC);
575
576 return ret;
577}
578
579
580
581
582static int
583NetVscDestroySendBuffer(
Bill Pembertond1af1db72009-07-27 16:47:44 -0400584 struct NETVSC_DEVICE *NetDevice
Hank Janssenfceaf242009-07-13 15:34:54 -0700585 )
586{
587 NVSP_MESSAGE *revokePacket;
588 int ret=0;
589
590
591 DPRINT_ENTER(NETVSC);
592
Bill Pemberton454f18a2009-07-27 16:47:24 -0400593 /*
594 * If we got a section count, it means we received a
595 * SendReceiveBufferComplete msg (ie sent
596 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
597 * to send a revoke msg here
598 */
Hank Janssenfceaf242009-07-13 15:34:54 -0700599 if (NetDevice->SendSectionSize)
600 {
601 DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeRevokeSendBuffer...");
602
Bill Pemberton454f18a2009-07-27 16:47:24 -0400603 /* Send the revoke send buffer */
Hank Janssenfceaf242009-07-13 15:34:54 -0700604 revokePacket = &NetDevice->RevokePacket;
605 memset(revokePacket, 0, sizeof(NVSP_MESSAGE));
606
607 revokePacket->Header.MessageType = NvspMessage1TypeRevokeSendBuffer;
608 revokePacket->Messages.Version1Messages.RevokeSendBuffer.Id = NETVSC_SEND_BUFFER_ID;
609
610 ret = NetDevice->Device->Driver->VmbusChannelInterface.SendPacket(NetDevice->Device,
611 revokePacket,
612 sizeof(NVSP_MESSAGE),
Greg Kroah-Hartmanc4b0bc92009-07-14 15:12:46 -0700613 (unsigned long)revokePacket,
Hank Janssenfceaf242009-07-13 15:34:54 -0700614 VmbusPacketTypeDataInBand,
615 0);
Bill Pemberton454f18a2009-07-27 16:47:24 -0400616 /* If we failed here, we might as well return and have a leak rather than continue and a bugchk */
Hank Janssenfceaf242009-07-13 15:34:54 -0700617 if (ret != 0)
618 {
619 DPRINT_ERR(NETVSC, "unable to send revoke send buffer to netvsp");
620 DPRINT_EXIT(NETVSC);
621 return -1;
622 }
623 }
624
Bill Pemberton454f18a2009-07-27 16:47:24 -0400625 /* Teardown the gpadl on the vsp end */
Hank Janssenfceaf242009-07-13 15:34:54 -0700626 if (NetDevice->SendBufferGpadlHandle)
627 {
628 DPRINT_INFO(NETVSC, "Tearing down send buffer's GPADL...");
629
630 ret = NetDevice->Device->Driver->VmbusChannelInterface.TeardownGpadl(NetDevice->Device,
631 NetDevice->SendBufferGpadlHandle);
632
Bill Pemberton454f18a2009-07-27 16:47:24 -0400633 /* If we failed here, we might as well return and have a leak rather than continue and a bugchk */
Hank Janssenfceaf242009-07-13 15:34:54 -0700634 if (ret != 0)
635 {
636 DPRINT_ERR(NETVSC, "unable to teardown send buffer's gpadl");
637 DPRINT_EXIT(NETVSC);
638 return -1;
639 }
640 NetDevice->SendBufferGpadlHandle = 0;
641 }
642
643 if (NetDevice->SendBuffer)
644 {
645 DPRINT_INFO(NETVSC, "Freeing up send buffer...");
646
Bill Pemberton454f18a2009-07-27 16:47:24 -0400647 /* Free up the receive buffer */
Greg Kroah-Hartmanbfc30aa2009-07-29 15:40:18 -0700648 osd_PageFree(NetDevice->SendBuffer, NetDevice->SendBufferSize >> PAGE_SHIFT);
Hank Janssenfceaf242009-07-13 15:34:54 -0700649 NetDevice->SendBuffer = NULL;
650 }
651
652 DPRINT_EXIT(NETVSC);
653
654 return ret;
655}
656
657
658
659static int
660NetVscConnectToVsp(
Nicolas Palix3d3b5512009-07-28 17:32:53 +0200661 struct hv_device *Device
Hank Janssenfceaf242009-07-13 15:34:54 -0700662 )
663{
664 int ret=0;
Bill Pembertond1af1db72009-07-27 16:47:44 -0400665 struct NETVSC_DEVICE *netDevice;
Hank Janssenfceaf242009-07-13 15:34:54 -0700666 NVSP_MESSAGE *initPacket;
667 int ndisVersion;
668
669 DPRINT_ENTER(NETVSC);
670
671 netDevice = GetOutboundNetDevice(Device);
672 if (!netDevice)
673 {
674 DPRINT_ERR(NETVSC, "unable to get net device...device being destroyed?");
675 DPRINT_EXIT(NETVSC);
676 return -1;
677 }
678
679 initPacket = &netDevice->ChannelInitPacket;
680
681 memset(initPacket, 0, sizeof(NVSP_MESSAGE));
682 initPacket->Header.MessageType = NvspMessageTypeInit;
683 initPacket->Messages.InitMessages.Init.MinProtocolVersion = NVSP_MIN_PROTOCOL_VERSION;
684 initPacket->Messages.InitMessages.Init.MaxProtocolVersion = NVSP_MAX_PROTOCOL_VERSION;
685
686 DPRINT_INFO(NETVSC, "Sending NvspMessageTypeInit...");
687
Bill Pemberton454f18a2009-07-27 16:47:24 -0400688 /* Send the init request */
Hank Janssenfceaf242009-07-13 15:34:54 -0700689 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
690 initPacket,
691 sizeof(NVSP_MESSAGE),
Greg Kroah-Hartmanc4b0bc92009-07-14 15:12:46 -0700692 (unsigned long)initPacket,
Hank Janssenfceaf242009-07-13 15:34:54 -0700693 VmbusPacketTypeDataInBand,
694 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
695
696 if( ret != 0)
697 {
698 DPRINT_ERR(NETVSC, "unable to send NvspMessageTypeInit");
699 goto Cleanup;
700 }
701
Greg Kroah-Hartmanbfc30aa2009-07-29 15:40:18 -0700702 osd_WaitEventWait(netDevice->ChannelInitEvent);
Hank Janssenfceaf242009-07-13 15:34:54 -0700703
Bill Pemberton454f18a2009-07-27 16:47:24 -0400704 /* Now, check the response */
705 /* ASSERT(initPacket->Messages.InitMessages.InitComplete.MaximumMdlChainLength <= MAX_MULTIPAGE_BUFFER_COUNT); */
Hank Janssenfceaf242009-07-13 15:34:54 -0700706 DPRINT_INFO(NETVSC, "NvspMessageTypeInit status(%d) max mdl chain (%d)",
707 initPacket->Messages.InitMessages.InitComplete.Status,
708 initPacket->Messages.InitMessages.InitComplete.MaximumMdlChainLength);
709
710 if (initPacket->Messages.InitMessages.InitComplete.Status != NvspStatusSuccess)
711 {
712 DPRINT_ERR(NETVSC, "unable to initialize with netvsp (status 0x%x)", initPacket->Messages.InitMessages.InitComplete.Status);
713 ret = -1;
714 goto Cleanup;
715 }
716
717 if (initPacket->Messages.InitMessages.InitComplete.NegotiatedProtocolVersion != NVSP_PROTOCOL_VERSION_1)
718 {
719 DPRINT_ERR(NETVSC, "unable to initialize with netvsp (version expected 1 got %d)",
720 initPacket->Messages.InitMessages.InitComplete.NegotiatedProtocolVersion);
721 ret = -1;
722 goto Cleanup;
723 }
724 DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendNdisVersion...");
725
Bill Pemberton454f18a2009-07-27 16:47:24 -0400726 /* Send the ndis version */
Hank Janssenfceaf242009-07-13 15:34:54 -0700727 memset(initPacket, 0, sizeof(NVSP_MESSAGE));
728
729 ndisVersion = 0x00050000;
730
731 initPacket->Header.MessageType = NvspMessage1TypeSendNdisVersion;
732 initPacket->Messages.Version1Messages.SendNdisVersion.NdisMajorVersion = (ndisVersion & 0xFFFF0000) >> 16;
733 initPacket->Messages.Version1Messages.SendNdisVersion.NdisMinorVersion = ndisVersion & 0xFFFF;
734
Bill Pemberton454f18a2009-07-27 16:47:24 -0400735 /* Send the init request */
Hank Janssenfceaf242009-07-13 15:34:54 -0700736 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
737 initPacket,
738 sizeof(NVSP_MESSAGE),
Greg Kroah-Hartmanc4b0bc92009-07-14 15:12:46 -0700739 (unsigned long)initPacket,
Hank Janssenfceaf242009-07-13 15:34:54 -0700740 VmbusPacketTypeDataInBand,
741 0);
742 if (ret != 0)
743 {
744 DPRINT_ERR(NETVSC, "unable to send NvspMessage1TypeSendNdisVersion");
745 ret = -1;
746 goto Cleanup;
747 }
Bill Pemberton454f18a2009-07-27 16:47:24 -0400748 /*
749 * BUGBUG - We have to wait for the above msg since the
750 * netvsp uses KMCL which acknowledges packet (completion
751 * packet) since our Vmbus always set the
752 * VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED flag
753 */
Greg Kroah-Hartmanbfc30aa2009-07-29 15:40:18 -0700754 /* osd_WaitEventWait(NetVscChannel->ChannelInitEvent); */
Hank Janssenfceaf242009-07-13 15:34:54 -0700755
Bill Pemberton454f18a2009-07-27 16:47:24 -0400756 /* Post the big receive buffer to NetVSP */
Hank Janssenfceaf242009-07-13 15:34:54 -0700757 ret = NetVscInitializeReceiveBufferWithNetVsp(Device);
758 if (ret == 0)
759 {
760 ret = NetVscInitializeSendBufferWithNetVsp(Device);
761 }
762
763Cleanup:
764 PutNetDevice(Device);
765 DPRINT_EXIT(NETVSC);
766 return ret;
767}
768
769static void
770NetVscDisconnectFromVsp(
Bill Pembertond1af1db72009-07-27 16:47:44 -0400771 struct NETVSC_DEVICE *NetDevice
Hank Janssenfceaf242009-07-13 15:34:54 -0700772 )
773{
774 DPRINT_ENTER(NETVSC);
775
776 NetVscDestroyReceiveBuffer(NetDevice);
777 NetVscDestroySendBuffer(NetDevice);
778
779 DPRINT_EXIT(NETVSC);
780}
781
782
783/*++
784
785Name:
786 NetVscOnDeviceAdd()
787
788Description:
789 Callback when the device belonging to this driver is added
790
791--*/
Greg Kroah-Hartmanbd1de702009-07-29 09:04:51 -0700792static int
Hank Janssenfceaf242009-07-13 15:34:54 -0700793NetVscOnDeviceAdd(
Nicolas Palix3d3b5512009-07-28 17:32:53 +0200794 struct hv_device *Device,
Hank Janssenfceaf242009-07-13 15:34:54 -0700795 void *AdditionalInfo
796 )
797{
798 int ret=0;
799 int i;
800
Bill Pembertond1af1db72009-07-27 16:47:44 -0400801 struct NETVSC_DEVICE *netDevice;
Nicolas Palix4193d4f2009-07-29 14:10:10 +0200802 struct hv_netvsc_packet *packet;
Hank Janssenfceaf242009-07-13 15:34:54 -0700803 LIST_ENTRY *entry;
804
805 NETVSC_DRIVER_OBJECT *netDriver = (NETVSC_DRIVER_OBJECT*) Device->Driver;;
806
807 DPRINT_ENTER(NETVSC);
808
809 netDevice = AllocNetDevice(Device);
810 if (!netDevice)
811 {
812 ret = -1;
813 goto Cleanup;
814 }
815
816 DPRINT_DBG(NETVSC, "netvsc channel object allocated - %p", netDevice);
817
Bill Pemberton454f18a2009-07-27 16:47:24 -0400818 /* Initialize the NetVSC channel extension */
Hank Janssenfceaf242009-07-13 15:34:54 -0700819 netDevice->ReceiveBufferSize = NETVSC_RECEIVE_BUFFER_SIZE;
Greg Kroah-Hartman64368732009-07-15 14:56:15 -0700820 spin_lock_init(&netDevice->receive_packet_list_lock);
Hank Janssenfceaf242009-07-13 15:34:54 -0700821
822 netDevice->SendBufferSize = NETVSC_SEND_BUFFER_SIZE;
823
824 INITIALIZE_LIST_HEAD(&netDevice->ReceivePacketList);
825
826 for (i=0; i < NETVSC_RECEIVE_PACKETLIST_COUNT; i++)
827 {
Nicolas Palix4193d4f2009-07-29 14:10:10 +0200828 packet = kzalloc(sizeof(struct hv_netvsc_packet) + (NETVSC_RECEIVE_SG_COUNT* sizeof(PAGE_BUFFER)), GFP_KERNEL);
Hank Janssenfceaf242009-07-13 15:34:54 -0700829 if (!packet)
830 {
831 DPRINT_DBG(NETVSC, "unable to allocate netvsc pkts for receive pool (wanted %d got %d)", NETVSC_RECEIVE_PACKETLIST_COUNT, i);
832 break;
833 }
834
835 INSERT_TAIL_LIST(&netDevice->ReceivePacketList, &packet->ListEntry);
836 }
Greg Kroah-Hartmanbfc30aa2009-07-29 15:40:18 -0700837 netDevice->ChannelInitEvent = osd_WaitEventCreate();
Hank Janssenfceaf242009-07-13 15:34:54 -0700838
Bill Pemberton454f18a2009-07-27 16:47:24 -0400839 /* Open the channel */
Hank Janssenfceaf242009-07-13 15:34:54 -0700840 ret = Device->Driver->VmbusChannelInterface.Open(Device,
Bill Pemberton454f18a2009-07-27 16:47:24 -0400841 netDriver->RingBufferSize,
842 netDriver->RingBufferSize,
843 NULL, 0,
844 NetVscOnChannelCallback,
845 Device
846 );
Hank Janssenfceaf242009-07-13 15:34:54 -0700847
848 if (ret != 0)
849 {
850 DPRINT_ERR(NETVSC, "unable to open channel: %d", ret);
851 ret = -1;
852 goto Cleanup;
853 }
854
Bill Pemberton454f18a2009-07-27 16:47:24 -0400855 /* Channel is opened */
Hank Janssenfceaf242009-07-13 15:34:54 -0700856 DPRINT_INFO(NETVSC, "*** NetVSC channel opened successfully! ***");
857
Bill Pemberton454f18a2009-07-27 16:47:24 -0400858 /* Connect with the NetVsp */
Hank Janssenfceaf242009-07-13 15:34:54 -0700859 ret = NetVscConnectToVsp(Device);
860 if (ret != 0)
861 {
862 DPRINT_ERR(NETVSC, "unable to connect to NetVSP - %d", ret);
863 ret = -1;
864 goto Close;
865 }
866
867 DPRINT_INFO(NETVSC, "*** NetVSC channel handshake result - %d ***", ret);
868
869 DPRINT_EXIT(NETVSC);
870 return ret;
871
872Close:
Bill Pemberton454f18a2009-07-27 16:47:24 -0400873 /* Now, we can close the channel safely */
Hank Janssenfceaf242009-07-13 15:34:54 -0700874 Device->Driver->VmbusChannelInterface.Close(Device);
875
876Cleanup:
877
878 if (netDevice)
879 {
Bill Pemberton420beac2009-07-29 17:00:10 -0400880 kfree(netDevice->ChannelInitEvent);
Hank Janssenfceaf242009-07-13 15:34:54 -0700881
882 while (!IsListEmpty(&netDevice->ReceivePacketList))
883 {
884 entry = REMOVE_HEAD_LIST(&netDevice->ReceivePacketList);
Nicolas Palix4193d4f2009-07-29 14:10:10 +0200885 packet = CONTAINING_RECORD(entry, struct hv_netvsc_packet, ListEntry);
Greg Kroah-Hartman8c69f522009-07-15 12:48:29 -0700886 kfree(packet);
Hank Janssenfceaf242009-07-13 15:34:54 -0700887 }
888
Hank Janssenfceaf242009-07-13 15:34:54 -0700889 ReleaseOutboundNetDevice(Device);
890 ReleaseInboundNetDevice(Device);
891
892 FreeNetDevice(netDevice);
893 }
894
895 DPRINT_EXIT(NETVSC);
896 return ret;
897}
898
899
900/*++
901
902Name:
903 NetVscOnDeviceRemove()
904
905Description:
906 Callback when the root bus device is removed
907
908--*/
Greg Kroah-Hartmanbd1de702009-07-29 09:04:51 -0700909static int
Hank Janssenfceaf242009-07-13 15:34:54 -0700910NetVscOnDeviceRemove(
Nicolas Palix3d3b5512009-07-28 17:32:53 +0200911 struct hv_device *Device
Hank Janssenfceaf242009-07-13 15:34:54 -0700912 )
913{
Bill Pembertond1af1db72009-07-27 16:47:44 -0400914 struct NETVSC_DEVICE *netDevice;
Nicolas Palix4193d4f2009-07-29 14:10:10 +0200915 struct hv_netvsc_packet *netvscPacket;
Hank Janssenfceaf242009-07-13 15:34:54 -0700916 int ret=0;
917 LIST_ENTRY *entry;
918
919 DPRINT_ENTER(NETVSC);
920
921 DPRINT_INFO(NETVSC, "Disabling outbound traffic on net device (%p)...", Device->Extension);
922
Bill Pemberton454f18a2009-07-27 16:47:24 -0400923 /* Stop outbound traffic ie sends and receives completions */
Hank Janssenfceaf242009-07-13 15:34:54 -0700924 netDevice = ReleaseOutboundNetDevice(Device);
925 if (!netDevice)
926 {
927 DPRINT_ERR(NETVSC, "No net device present!!");
928 return -1;
929 }
930
Bill Pemberton454f18a2009-07-27 16:47:24 -0400931 /* Wait for all send completions */
Bill Pembertonf4888412009-07-29 17:00:12 -0400932 while (atomic_read(&netDevice->NumOutstandingSends))
Hank Janssenfceaf242009-07-13 15:34:54 -0700933 {
Bill Pembertonf4888412009-07-29 17:00:12 -0400934 DPRINT_INFO(NETVSC, "waiting for %d requests to complete...", atomic_read(&netDevice->NumOutstandingSends));
Hank Janssenfceaf242009-07-13 15:34:54 -0700935
Greg Kroah-Hartmanb4362c92009-07-16 11:50:41 -0700936 udelay(100);
Hank Janssenfceaf242009-07-13 15:34:54 -0700937 }
938
939 DPRINT_INFO(NETVSC, "Disconnecting from netvsp...");
940
941 NetVscDisconnectFromVsp(netDevice);
942
943 DPRINT_INFO(NETVSC, "Disabling inbound traffic on net device (%p)...", Device->Extension);
944
Bill Pemberton454f18a2009-07-27 16:47:24 -0400945 /* Stop inbound traffic ie receives and sends completions */
Hank Janssenfceaf242009-07-13 15:34:54 -0700946 netDevice = ReleaseInboundNetDevice(Device);
947
Bill Pemberton454f18a2009-07-27 16:47:24 -0400948 /* At this point, no one should be accessing netDevice except in here */
Hank Janssenfceaf242009-07-13 15:34:54 -0700949 DPRINT_INFO(NETVSC, "net device (%p) safe to remove", netDevice);
950
Bill Pemberton454f18a2009-07-27 16:47:24 -0400951 /* Now, we can close the channel safely */
Hank Janssenfceaf242009-07-13 15:34:54 -0700952 Device->Driver->VmbusChannelInterface.Close(Device);
953
Bill Pemberton454f18a2009-07-27 16:47:24 -0400954 /* Release all resources */
Hank Janssenfceaf242009-07-13 15:34:54 -0700955 while (!IsListEmpty(&netDevice->ReceivePacketList))
956 {
957 entry = REMOVE_HEAD_LIST(&netDevice->ReceivePacketList);
Nicolas Palix4193d4f2009-07-29 14:10:10 +0200958 netvscPacket = CONTAINING_RECORD(entry, struct hv_netvsc_packet, ListEntry);
Hank Janssenfceaf242009-07-13 15:34:54 -0700959
Greg Kroah-Hartman8c69f522009-07-15 12:48:29 -0700960 kfree(netvscPacket);
Hank Janssenfceaf242009-07-13 15:34:54 -0700961 }
962
Bill Pemberton420beac2009-07-29 17:00:10 -0400963 kfree(netDevice->ChannelInitEvent);
Hank Janssenfceaf242009-07-13 15:34:54 -0700964 FreeNetDevice(netDevice);
965
966 DPRINT_EXIT(NETVSC);
967 return ret;
968}
969
970
971
972/*++
973
974Name:
975 NetVscOnCleanup()
976
977Description:
978 Perform any cleanup when the driver is removed
979
980--*/
Greg Kroah-Hartmanbd1de702009-07-29 09:04:51 -0700981static void
Hank Janssenfceaf242009-07-13 15:34:54 -0700982NetVscOnCleanup(
Nicolas Palix775ef252009-07-29 14:09:45 +0200983 struct hv_driver *drv
Hank Janssenfceaf242009-07-13 15:34:54 -0700984 )
985{
986 DPRINT_ENTER(NETVSC);
987
988 DPRINT_EXIT(NETVSC);
989}
990
991static void
992NetVscOnSendCompletion(
Nicolas Palix3d3b5512009-07-28 17:32:53 +0200993 struct hv_device *Device,
Hank Janssenfceaf242009-07-13 15:34:54 -0700994 VMPACKET_DESCRIPTOR *Packet
995 )
996{
Bill Pembertond1af1db72009-07-27 16:47:44 -0400997 struct NETVSC_DEVICE *netDevice;
Hank Janssenfceaf242009-07-13 15:34:54 -0700998 NVSP_MESSAGE *nvspPacket;
Nicolas Palix4193d4f2009-07-29 14:10:10 +0200999 struct hv_netvsc_packet *nvscPacket;
Hank Janssenfceaf242009-07-13 15:34:54 -07001000
1001 DPRINT_ENTER(NETVSC);
1002
1003 netDevice = GetInboundNetDevice(Device);
1004 if (!netDevice)
1005 {
1006 DPRINT_ERR(NETVSC, "unable to get net device...device being destroyed?");
1007 DPRINT_EXIT(NETVSC);
1008 return;
1009 }
1010
Greg Kroah-Hartmanc4b0bc92009-07-14 15:12:46 -07001011 nvspPacket = (NVSP_MESSAGE*)((unsigned long)Packet + (Packet->DataOffset8 << 3));
Hank Janssenfceaf242009-07-13 15:34:54 -07001012
1013 DPRINT_DBG(NETVSC, "send completion packet - type %d", nvspPacket->Header.MessageType);
1014
1015 if (nvspPacket->Header.MessageType == NvspMessageTypeInitComplete ||
1016 nvspPacket->Header.MessageType == NvspMessage1TypeSendReceiveBufferComplete ||
1017 nvspPacket->Header.MessageType == NvspMessage1TypeSendSendBufferComplete)
1018 {
Bill Pemberton454f18a2009-07-27 16:47:24 -04001019 /* Copy the response back */
Hank Janssenfceaf242009-07-13 15:34:54 -07001020 memcpy(&netDevice->ChannelInitPacket, nvspPacket, sizeof(NVSP_MESSAGE));
Greg Kroah-Hartmanbfc30aa2009-07-29 15:40:18 -07001021 osd_WaitEventSet(netDevice->ChannelInitEvent);
Hank Janssenfceaf242009-07-13 15:34:54 -07001022 }
1023 else if (nvspPacket->Header.MessageType == NvspMessage1TypeSendRNDISPacketComplete)
1024 {
Bill Pemberton454f18a2009-07-27 16:47:24 -04001025 /* Get the send context */
Nicolas Palix4193d4f2009-07-29 14:10:10 +02001026 nvscPacket = (struct hv_netvsc_packet *)(unsigned long)Packet->TransactionId;
Hank Janssenfceaf242009-07-13 15:34:54 -07001027 ASSERT(nvscPacket);
1028
Bill Pemberton454f18a2009-07-27 16:47:24 -04001029 /* Notify the layer above us */
Hank Janssenfceaf242009-07-13 15:34:54 -07001030 nvscPacket->Completion.Send.OnSendCompletion(nvscPacket->Completion.Send.SendCompletionContext);
1031
Bill Pembertonf4888412009-07-29 17:00:12 -04001032 atomic_dec(&netDevice->NumOutstandingSends);
Hank Janssenfceaf242009-07-13 15:34:54 -07001033 }
1034 else
1035 {
1036 DPRINT_ERR(NETVSC, "Unknown send completion packet type - %d received!!", nvspPacket->Header.MessageType);
1037 }
1038
1039 PutNetDevice(Device);
1040 DPRINT_EXIT(NETVSC);
1041}
1042
1043
1044
1045static int
1046NetVscOnSend(
Nicolas Palix3d3b5512009-07-28 17:32:53 +02001047 struct hv_device *Device,
Nicolas Palix4193d4f2009-07-29 14:10:10 +02001048 struct hv_netvsc_packet *Packet
Hank Janssenfceaf242009-07-13 15:34:54 -07001049 )
1050{
Bill Pembertond1af1db72009-07-27 16:47:44 -04001051 struct NETVSC_DEVICE *netDevice;
Hank Janssenfceaf242009-07-13 15:34:54 -07001052 int ret=0;
1053
1054 NVSP_MESSAGE sendMessage;
1055
1056 DPRINT_ENTER(NETVSC);
1057
1058 netDevice = GetOutboundNetDevice(Device);
1059 if (!netDevice)
1060 {
1061 DPRINT_ERR(NETVSC, "net device (%p) shutting down...ignoring outbound packets", netDevice);
1062 DPRINT_EXIT(NETVSC);
1063 return -2;
1064 }
1065
1066 sendMessage.Header.MessageType = NvspMessage1TypeSendRNDISPacket;
1067 if (Packet->IsDataPacket)
Bill Pemberton454f18a2009-07-27 16:47:24 -04001068 sendMessage.Messages.Version1Messages.SendRNDISPacket.ChannelType = 0;/* 0 is RMC_DATA; */
Hank Janssenfceaf242009-07-13 15:34:54 -07001069 else
Bill Pemberton454f18a2009-07-27 16:47:24 -04001070 sendMessage.Messages.Version1Messages.SendRNDISPacket.ChannelType = 1;/* 1 is RMC_CONTROL; */
Hank Janssenfceaf242009-07-13 15:34:54 -07001071
Bill Pemberton454f18a2009-07-27 16:47:24 -04001072 /* Not using send buffer section */
Hank Janssenfceaf242009-07-13 15:34:54 -07001073 sendMessage.Messages.Version1Messages.SendRNDISPacket.SendBufferSectionIndex = 0xFFFFFFFF;
1074 sendMessage.Messages.Version1Messages.SendRNDISPacket.SendBufferSectionSize = 0;
1075
1076 if (Packet->PageBufferCount)
1077 {
1078 ret = Device->Driver->VmbusChannelInterface.SendPacketPageBuffer(Device,
1079 Packet->PageBuffers,
1080 Packet->PageBufferCount,
1081 &sendMessage,
1082 sizeof(NVSP_MESSAGE),
Greg Kroah-Hartmanc4b0bc92009-07-14 15:12:46 -07001083 (unsigned long)Packet);
Hank Janssenfceaf242009-07-13 15:34:54 -07001084 }
1085 else
1086 {
1087 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
1088 &sendMessage,
1089 sizeof(NVSP_MESSAGE),
Greg Kroah-Hartmanc4b0bc92009-07-14 15:12:46 -07001090 (unsigned long)Packet,
Hank Janssenfceaf242009-07-13 15:34:54 -07001091 VmbusPacketTypeDataInBand,
1092 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1093
1094 }
1095
1096 if (ret != 0)
1097 {
1098 DPRINT_ERR(NETVSC, "Unable to send packet %p ret %d", Packet, ret);
1099 }
1100
Bill Pembertonf4888412009-07-29 17:00:12 -04001101 atomic_inc(&netDevice->NumOutstandingSends);
Hank Janssenfceaf242009-07-13 15:34:54 -07001102 PutNetDevice(Device);
1103
1104 DPRINT_EXIT(NETVSC);
1105 return ret;
1106}
1107
1108
1109static void
1110NetVscOnReceive(
Nicolas Palix3d3b5512009-07-28 17:32:53 +02001111 struct hv_device *Device,
Hank Janssenfceaf242009-07-13 15:34:54 -07001112 VMPACKET_DESCRIPTOR *Packet
1113 )
1114{
Bill Pembertond1af1db72009-07-27 16:47:44 -04001115 struct NETVSC_DEVICE *netDevice;
Hank Janssenfceaf242009-07-13 15:34:54 -07001116 VMTRANSFER_PAGE_PACKET_HEADER *vmxferpagePacket;
1117 NVSP_MESSAGE *nvspPacket;
Nicolas Palix4193d4f2009-07-29 14:10:10 +02001118 struct hv_netvsc_packet *netvscPacket=NULL;
Hank Janssenfceaf242009-07-13 15:34:54 -07001119 LIST_ENTRY* entry;
Greg Kroah-Hartmanc4b0bc92009-07-14 15:12:46 -07001120 unsigned long start;
1121 unsigned long end, endVirtual;
Bill Pemberton454f18a2009-07-27 16:47:24 -04001122 /* NETVSC_DRIVER_OBJECT *netvscDriver; */
Hank Janssenfceaf242009-07-13 15:34:54 -07001123 XFERPAGE_PACKET *xferpagePacket=NULL;
1124 LIST_ENTRY listHead;
1125
1126 int i=0, j=0;
1127 int count=0, bytesRemain=0;
Greg Kroah-Hartman64368732009-07-15 14:56:15 -07001128 unsigned long flags;
Hank Janssenfceaf242009-07-13 15:34:54 -07001129
1130 DPRINT_ENTER(NETVSC);
1131
1132 netDevice = GetInboundNetDevice(Device);
1133 if (!netDevice)
1134 {
1135 DPRINT_ERR(NETVSC, "unable to get net device...device being destroyed?");
1136 DPRINT_EXIT(NETVSC);
1137 return;
1138 }
1139
Bill Pemberton454f18a2009-07-27 16:47:24 -04001140 /* All inbound packets other than send completion should be xfer page packet */
Hank Janssenfceaf242009-07-13 15:34:54 -07001141 if (Packet->Type != VmbusPacketTypeDataUsingTransferPages)
1142 {
1143 DPRINT_ERR(NETVSC, "Unknown packet type received - %d", Packet->Type);
1144 PutNetDevice(Device);
1145 return;
1146 }
1147
Greg Kroah-Hartmanc4b0bc92009-07-14 15:12:46 -07001148 nvspPacket = (NVSP_MESSAGE*)((unsigned long)Packet + (Packet->DataOffset8 << 3));
Hank Janssenfceaf242009-07-13 15:34:54 -07001149
Bill Pemberton454f18a2009-07-27 16:47:24 -04001150 /* Make sure this is a valid nvsp packet */
Hank Janssenfceaf242009-07-13 15:34:54 -07001151 if (nvspPacket->Header.MessageType != NvspMessage1TypeSendRNDISPacket )
1152 {
1153 DPRINT_ERR(NETVSC, "Unknown nvsp packet type received - %d", nvspPacket->Header.MessageType);
1154 PutNetDevice(Device);
1155 return;
1156 }
1157
1158 DPRINT_DBG(NETVSC, "NVSP packet received - type %d", nvspPacket->Header.MessageType);
1159
1160 vmxferpagePacket = (VMTRANSFER_PAGE_PACKET_HEADER*)Packet;
1161
1162 if (vmxferpagePacket->TransferPageSetId != NETVSC_RECEIVE_BUFFER_ID)
1163 {
1164 DPRINT_ERR(NETVSC, "Invalid xfer page set id - expecting %x got %x", NETVSC_RECEIVE_BUFFER_ID, vmxferpagePacket->TransferPageSetId);
1165 PutNetDevice(Device);
1166 return;
1167 }
1168
1169 DPRINT_DBG(NETVSC, "xfer page - range count %d", vmxferpagePacket->RangeCount);
1170
1171 INITIALIZE_LIST_HEAD(&listHead);
1172
Bill Pemberton454f18a2009-07-27 16:47:24 -04001173 /*
1174 * Grab free packets (range count + 1) to represent this xfer
1175 * page packet. +1 to represent the xfer page packet itself.
1176 * We grab it here so that we know exactly how many we can
1177 * fulfil
1178 */
Greg Kroah-Hartman64368732009-07-15 14:56:15 -07001179 spin_lock_irqsave(&netDevice->receive_packet_list_lock, flags);
Hank Janssenfceaf242009-07-13 15:34:54 -07001180 while (!IsListEmpty(&netDevice->ReceivePacketList))
1181 {
1182 entry = REMOVE_HEAD_LIST(&netDevice->ReceivePacketList);
Nicolas Palix4193d4f2009-07-29 14:10:10 +02001183 netvscPacket = CONTAINING_RECORD(entry, struct hv_netvsc_packet, ListEntry);
Hank Janssenfceaf242009-07-13 15:34:54 -07001184
1185 INSERT_TAIL_LIST(&listHead, &netvscPacket->ListEntry);
1186
1187 if (++count == vmxferpagePacket->RangeCount + 1)
1188 break;
1189 }
Greg Kroah-Hartman64368732009-07-15 14:56:15 -07001190 spin_unlock_irqrestore(&netDevice->receive_packet_list_lock, flags);
Hank Janssenfceaf242009-07-13 15:34:54 -07001191
Bill Pemberton454f18a2009-07-27 16:47:24 -04001192 /*
1193 * We need at least 2 netvsc pkts (1 to represent the xfer
1194 * page and at least 1 for the range) i.e. we can handled
1195 * some of the xfer page packet ranges...
1196 */
Hank Janssenfceaf242009-07-13 15:34:54 -07001197 if (count < 2)
1198 {
1199 DPRINT_ERR(NETVSC, "Got only %d netvsc pkt...needed %d pkts. Dropping this xfer page packet completely!", count, vmxferpagePacket->RangeCount+1);
1200
Bill Pemberton454f18a2009-07-27 16:47:24 -04001201 /* Return it to the freelist */
Greg Kroah-Hartman64368732009-07-15 14:56:15 -07001202 spin_lock_irqsave(&netDevice->receive_packet_list_lock, flags);
Hank Janssenfceaf242009-07-13 15:34:54 -07001203 for (i=count; i != 0; i--)
1204 {
1205 entry = REMOVE_HEAD_LIST(&listHead);
Nicolas Palix4193d4f2009-07-29 14:10:10 +02001206 netvscPacket = CONTAINING_RECORD(entry, struct hv_netvsc_packet, ListEntry);
Hank Janssenfceaf242009-07-13 15:34:54 -07001207
1208 INSERT_TAIL_LIST(&netDevice->ReceivePacketList, &netvscPacket->ListEntry);
1209 }
Greg Kroah-Hartman64368732009-07-15 14:56:15 -07001210 spin_unlock_irqrestore(&netDevice->receive_packet_list_lock, flags);
Hank Janssenfceaf242009-07-13 15:34:54 -07001211
1212 NetVscSendReceiveCompletion(Device, vmxferpagePacket->d.TransactionId);
1213
1214 PutNetDevice(Device);
1215 return;
1216 }
1217
Bill Pemberton454f18a2009-07-27 16:47:24 -04001218 /* Remove the 1st packet to represent the xfer page packet itself */
Hank Janssenfceaf242009-07-13 15:34:54 -07001219 entry = REMOVE_HEAD_LIST(&listHead);
1220 xferpagePacket = CONTAINING_RECORD(entry, XFERPAGE_PACKET, ListEntry);
Bill Pemberton454f18a2009-07-27 16:47:24 -04001221 xferpagePacket->Count = count - 1; /* This is how much we can satisfy */
Hank Janssenfceaf242009-07-13 15:34:54 -07001222 ASSERT(xferpagePacket->Count > 0 && xferpagePacket->Count <= vmxferpagePacket->RangeCount);
1223
1224 if (xferpagePacket->Count != vmxferpagePacket->RangeCount)
1225 {
1226 DPRINT_INFO(NETVSC, "Needed %d netvsc pkts to satisy this xfer page...got %d", vmxferpagePacket->RangeCount, xferpagePacket->Count);
1227 }
1228
Bill Pemberton454f18a2009-07-27 16:47:24 -04001229 /* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */
Hank Janssenfceaf242009-07-13 15:34:54 -07001230 for (i=0; i < (count - 1); i++)
1231 {
1232 entry = REMOVE_HEAD_LIST(&listHead);
Nicolas Palix4193d4f2009-07-29 14:10:10 +02001233 netvscPacket = CONTAINING_RECORD(entry, struct hv_netvsc_packet, ListEntry);
Hank Janssenfceaf242009-07-13 15:34:54 -07001234
Bill Pemberton454f18a2009-07-27 16:47:24 -04001235 /* Initialize the netvsc packet */
Hank Janssenfceaf242009-07-13 15:34:54 -07001236 netvscPacket->XferPagePacket = xferpagePacket;
1237 netvscPacket->Completion.Recv.OnReceiveCompletion = NetVscOnReceiveCompletion;
1238 netvscPacket->Completion.Recv.ReceiveCompletionContext = netvscPacket;
1239 netvscPacket->Device = Device;
Bill Pemberton454f18a2009-07-27 16:47:24 -04001240 netvscPacket->Completion.Recv.ReceiveCompletionTid = vmxferpagePacket->d.TransactionId; /* Save this so that we can send it back */
Hank Janssenfceaf242009-07-13 15:34:54 -07001241
1242 netvscPacket->TotalDataBufferLength = vmxferpagePacket->Ranges[i].ByteCount;
1243 netvscPacket->PageBufferCount = 1;
1244
1245 ASSERT(vmxferpagePacket->Ranges[i].ByteOffset + vmxferpagePacket->Ranges[i].ByteCount < netDevice->ReceiveBufferSize);
1246
1247 netvscPacket->PageBuffers[0].Length = vmxferpagePacket->Ranges[i].ByteCount;
1248
Greg Kroah-Hartmanfa56d362009-07-29 15:39:27 -07001249 start = virt_to_phys((void*)((unsigned long)netDevice->ReceiveBuffer + vmxferpagePacket->Ranges[i].ByteOffset));
Hank Janssenfceaf242009-07-13 15:34:54 -07001250
1251 netvscPacket->PageBuffers[0].Pfn = start >> PAGE_SHIFT;
Greg Kroah-Hartmanc4b0bc92009-07-14 15:12:46 -07001252 endVirtual = (unsigned long)netDevice->ReceiveBuffer
Hank Janssenfceaf242009-07-13 15:34:54 -07001253 + vmxferpagePacket->Ranges[i].ByteOffset
1254 + vmxferpagePacket->Ranges[i].ByteCount -1;
Greg Kroah-Hartmanfa56d362009-07-29 15:39:27 -07001255 end = virt_to_phys((void*)endVirtual);
Hank Janssenfceaf242009-07-13 15:34:54 -07001256
Bill Pemberton454f18a2009-07-27 16:47:24 -04001257 /* Calculate the page relative offset */
Hank Janssenfceaf242009-07-13 15:34:54 -07001258 netvscPacket->PageBuffers[0].Offset = vmxferpagePacket->Ranges[i].ByteOffset & (PAGE_SIZE -1);
1259 if ((end >> PAGE_SHIFT) != (start>>PAGE_SHIFT)) {
Bill Pemberton454f18a2009-07-27 16:47:24 -04001260 /* Handle frame across multiple pages: */
Hank Janssenfceaf242009-07-13 15:34:54 -07001261 netvscPacket->PageBuffers[0].Length =
1262 (netvscPacket->PageBuffers[0].Pfn <<PAGE_SHIFT) + PAGE_SIZE - start;
1263 bytesRemain = netvscPacket->TotalDataBufferLength - netvscPacket->PageBuffers[0].Length;
1264 for (j=1; j<NETVSC_PACKET_MAXPAGE; j++) {
1265 netvscPacket->PageBuffers[j].Offset = 0;
1266 if (bytesRemain <= PAGE_SIZE) {
1267 netvscPacket->PageBuffers[j].Length = bytesRemain;
1268 bytesRemain = 0;
1269 } else {
1270 netvscPacket->PageBuffers[j].Length = PAGE_SIZE;
1271 bytesRemain -= PAGE_SIZE;
1272 }
1273 netvscPacket->PageBuffers[j].Pfn =
Greg Kroah-Hartmanfa56d362009-07-29 15:39:27 -07001274 virt_to_phys((void*)(endVirtual - bytesRemain)) >> PAGE_SHIFT;
Hank Janssenfceaf242009-07-13 15:34:54 -07001275 netvscPacket->PageBufferCount++;
1276 if (bytesRemain == 0)
1277 break;
1278 }
1279 ASSERT(bytesRemain == 0);
1280 }
1281 DPRINT_DBG(NETVSC, "[%d] - (abs offset %u len %u) => (pfn %llx, offset %u, len %u)",
1282 i,
1283 vmxferpagePacket->Ranges[i].ByteOffset,
1284 vmxferpagePacket->Ranges[i].ByteCount,
1285 netvscPacket->PageBuffers[0].Pfn,
1286 netvscPacket->PageBuffers[0].Offset,
1287 netvscPacket->PageBuffers[0].Length);
1288
Bill Pemberton454f18a2009-07-27 16:47:24 -04001289 /* Pass it to the upper layer */
Hank Janssenfceaf242009-07-13 15:34:54 -07001290 ((NETVSC_DRIVER_OBJECT*)Device->Driver)->OnReceiveCallback(Device, netvscPacket);
1291
1292 NetVscOnReceiveCompletion(netvscPacket->Completion.Recv.ReceiveCompletionContext);
1293 }
1294
1295 ASSERT(IsListEmpty(&listHead));
1296
1297 PutNetDevice(Device);
1298 DPRINT_EXIT(NETVSC);
1299}
1300
1301
1302static void
1303NetVscSendReceiveCompletion(
Nicolas Palix3d3b5512009-07-28 17:32:53 +02001304 struct hv_device *Device,
Greg Kroah-Hartman59471432009-07-14 15:10:26 -07001305 u64 TransactionId
Hank Janssenfceaf242009-07-13 15:34:54 -07001306 )
1307{
1308 NVSP_MESSAGE recvcompMessage;
1309 int retries=0;
1310 int ret=0;
1311
1312 DPRINT_DBG(NETVSC, "Sending receive completion pkt - %llx", TransactionId);
1313
1314 recvcompMessage.Header.MessageType = NvspMessage1TypeSendRNDISPacketComplete;
1315
Bill Pemberton454f18a2009-07-27 16:47:24 -04001316 /* FIXME: Pass in the status */
Hank Janssenfceaf242009-07-13 15:34:54 -07001317 recvcompMessage.Messages.Version1Messages.SendRNDISPacketComplete.Status = NvspStatusSuccess;
1318
1319retry_send_cmplt:
Bill Pemberton454f18a2009-07-27 16:47:24 -04001320 /* Send the completion */
Hank Janssenfceaf242009-07-13 15:34:54 -07001321 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
1322 &recvcompMessage,
1323 sizeof(NVSP_MESSAGE),
1324 TransactionId,
1325 VmbusPacketTypeCompletion,
1326 0);
Bill Pemberton454f18a2009-07-27 16:47:24 -04001327 if (ret == 0) /* success */
Hank Janssenfceaf242009-07-13 15:34:54 -07001328 {
Bill Pemberton454f18a2009-07-27 16:47:24 -04001329 /* no-op */
Hank Janssenfceaf242009-07-13 15:34:54 -07001330 }
Bill Pemberton454f18a2009-07-27 16:47:24 -04001331 else if (ret == -1) /* no more room...wait a bit and attempt to retry 3 times */
Hank Janssenfceaf242009-07-13 15:34:54 -07001332 {
1333 retries++;
1334 DPRINT_ERR(NETVSC, "unable to send receive completion pkt (tid %llx)...retrying %d", TransactionId, retries);
1335
1336 if (retries < 4)
1337 {
Greg Kroah-Hartmanb4362c92009-07-16 11:50:41 -07001338 udelay(100);
Hank Janssenfceaf242009-07-13 15:34:54 -07001339 goto retry_send_cmplt;
1340 }
1341 else
1342 {
1343 DPRINT_ERR(NETVSC, "unable to send receive completion pkt (tid %llx)...give up retrying", TransactionId);
1344 }
1345 }
1346 else
1347 {
1348 DPRINT_ERR(NETVSC, "unable to send receive completion pkt - %llx", TransactionId);
1349 }
1350}
1351
Bill Pemberton454f18a2009-07-27 16:47:24 -04001352/* Send a receive completion packet to RNDIS device (ie NetVsp) */
Hank Janssenfceaf242009-07-13 15:34:54 -07001353static void
1354NetVscOnReceiveCompletion(
Greg Kroah-Hartman8282c402009-07-14 15:06:28 -07001355 void * Context)
Hank Janssenfceaf242009-07-13 15:34:54 -07001356{
Nicolas Palix4193d4f2009-07-29 14:10:10 +02001357 struct hv_netvsc_packet *packet = (struct hv_netvsc_packet*)Context;
Nicolas Palix3d3b5512009-07-28 17:32:53 +02001358 struct hv_device *device = (struct hv_device*)packet->Device;
Bill Pembertond1af1db72009-07-27 16:47:44 -04001359 struct NETVSC_DEVICE *netDevice;
Greg Kroah-Hartman59471432009-07-14 15:10:26 -07001360 u64 transactionId=0;
Greg Kroah-Hartman0e727612009-07-15 12:46:44 -07001361 bool fSendReceiveComp = false;
Greg Kroah-Hartman64368732009-07-15 14:56:15 -07001362 unsigned long flags;
Hank Janssenfceaf242009-07-13 15:34:54 -07001363
1364 DPRINT_ENTER(NETVSC);
1365
1366 ASSERT(packet->XferPagePacket);
1367
Bill Pemberton454f18a2009-07-27 16:47:24 -04001368 /* Even though it seems logical to do a GetOutboundNetDevice() here to send out receive completion, */
1369 /* we are using GetInboundNetDevice() since we may have disable outbound traffic already. */
Hank Janssenfceaf242009-07-13 15:34:54 -07001370 netDevice = GetInboundNetDevice(device);
1371 if (!netDevice)
1372 {
1373 DPRINT_ERR(NETVSC, "unable to get net device...device being destroyed?");
1374 DPRINT_EXIT(NETVSC);
1375 return;
1376 }
1377
Bill Pemberton454f18a2009-07-27 16:47:24 -04001378 /* Overloading use of the lock. */
Greg Kroah-Hartman64368732009-07-15 14:56:15 -07001379 spin_lock_irqsave(&netDevice->receive_packet_list_lock, flags);
Hank Janssenfceaf242009-07-13 15:34:54 -07001380
1381 ASSERT(packet->XferPagePacket->Count > 0);
1382 packet->XferPagePacket->Count--;
1383
Bill Pemberton454f18a2009-07-27 16:47:24 -04001384 /* Last one in the line that represent 1 xfer page packet. */
1385 /* Return the xfer page packet itself to the freelist */
Hank Janssenfceaf242009-07-13 15:34:54 -07001386 if (packet->XferPagePacket->Count == 0)
1387 {
Greg Kroah-Hartman0e727612009-07-15 12:46:44 -07001388 fSendReceiveComp = true;
Hank Janssenfceaf242009-07-13 15:34:54 -07001389 transactionId = packet->Completion.Recv.ReceiveCompletionTid;
1390
1391 INSERT_TAIL_LIST(&netDevice->ReceivePacketList, &packet->XferPagePacket->ListEntry);
1392 }
1393
Bill Pemberton454f18a2009-07-27 16:47:24 -04001394 /* Put the packet back */
Hank Janssenfceaf242009-07-13 15:34:54 -07001395 INSERT_TAIL_LIST(&netDevice->ReceivePacketList, &packet->ListEntry);
Greg Kroah-Hartman64368732009-07-15 14:56:15 -07001396 spin_unlock_irqrestore(&netDevice->receive_packet_list_lock, flags);
Hank Janssenfceaf242009-07-13 15:34:54 -07001397
Bill Pemberton454f18a2009-07-27 16:47:24 -04001398 /* Send a receive completion for the xfer page packet */
Hank Janssenfceaf242009-07-13 15:34:54 -07001399 if (fSendReceiveComp)
1400 {
1401 NetVscSendReceiveCompletion(device, transactionId);
1402 }
1403
1404 PutNetDevice(device);
1405 DPRINT_EXIT(NETVSC);
1406}
1407
1408
1409
1410void
1411NetVscOnChannelCallback(
Greg Kroah-Hartman8282c402009-07-14 15:06:28 -07001412 void * Context
Hank Janssenfceaf242009-07-13 15:34:54 -07001413 )
1414{
1415 const int netPacketSize=2048;
1416 int ret=0;
Nicolas Palix3d3b5512009-07-28 17:32:53 +02001417 struct hv_device *device=(struct hv_device*)Context;
Bill Pembertond1af1db72009-07-27 16:47:44 -04001418 struct NETVSC_DEVICE *netDevice;
Hank Janssenfceaf242009-07-13 15:34:54 -07001419
Greg Kroah-Hartman4d643112009-07-14 15:09:36 -07001420 u32 bytesRecvd;
Greg Kroah-Hartman59471432009-07-14 15:10:26 -07001421 u64 requestId;
1422 unsigned char packet[netPacketSize];
Hank Janssenfceaf242009-07-13 15:34:54 -07001423 VMPACKET_DESCRIPTOR *desc;
Greg Kroah-Hartman59471432009-07-14 15:10:26 -07001424 unsigned char *buffer=packet;
Hank Janssenfceaf242009-07-13 15:34:54 -07001425 int bufferlen=netPacketSize;
1426
1427
1428 DPRINT_ENTER(NETVSC);
1429
1430 ASSERT(device);
1431
1432 netDevice = GetInboundNetDevice(device);
1433 if (!netDevice)
1434 {
1435 DPRINT_ERR(NETVSC, "net device (%p) shutting down...ignoring inbound packets", netDevice);
1436 DPRINT_EXIT(NETVSC);
1437 return;
1438 }
1439
1440 do
1441 {
1442 ret = device->Driver->VmbusChannelInterface.RecvPacketRaw(device,
1443 buffer,
1444 bufferlen,
1445 &bytesRecvd,
1446 &requestId);
1447
1448 if (ret == 0)
1449 {
1450 if (bytesRecvd > 0)
1451 {
1452 DPRINT_DBG(NETVSC, "receive %d bytes, tid %llx", bytesRecvd, requestId);
1453
1454 desc = (VMPACKET_DESCRIPTOR*)buffer;
1455 switch (desc->Type)
1456 {
1457 case VmbusPacketTypeCompletion:
1458 NetVscOnSendCompletion(device, desc);
1459 break;
1460
1461 case VmbusPacketTypeDataUsingTransferPages:
1462 NetVscOnReceive(device, desc);
1463 break;
1464
1465 default:
1466 DPRINT_ERR(NETVSC, "unhandled packet type %d, tid %llx len %d\n", desc->Type, requestId, bytesRecvd);
1467 break;
1468 }
1469
Bill Pemberton454f18a2009-07-27 16:47:24 -04001470 /* reset */
Hank Janssenfceaf242009-07-13 15:34:54 -07001471 if (bufferlen > netPacketSize)
1472 {
Greg Kroah-Hartman8c69f522009-07-15 12:48:29 -07001473 kfree(buffer);
Hank Janssenfceaf242009-07-13 15:34:54 -07001474
1475 buffer = packet;
1476 bufferlen = netPacketSize;
1477 }
1478 }
1479 else
1480 {
Bill Pemberton454f18a2009-07-27 16:47:24 -04001481 /* DPRINT_DBG(NETVSC, "nothing else to read..."); */
Hank Janssenfceaf242009-07-13 15:34:54 -07001482
Bill Pemberton454f18a2009-07-27 16:47:24 -04001483 /* reset */
Hank Janssenfceaf242009-07-13 15:34:54 -07001484 if (bufferlen > netPacketSize)
1485 {
Greg Kroah-Hartman8c69f522009-07-15 12:48:29 -07001486 kfree(buffer);
Hank Janssenfceaf242009-07-13 15:34:54 -07001487
1488 buffer = packet;
1489 bufferlen = netPacketSize;
1490 }
1491
1492 break;
1493 }
1494 }
Bill Pemberton454f18a2009-07-27 16:47:24 -04001495 else if (ret == -2) /* Handle large packet */
Hank Janssenfceaf242009-07-13 15:34:54 -07001496 {
Greg Kroah-Hartman0a72f3c2009-07-15 12:48:01 -07001497 buffer = kmalloc(bytesRecvd, GFP_ATOMIC);
Hank Janssenfceaf242009-07-13 15:34:54 -07001498 if (buffer == NULL)
1499 {
Bill Pemberton454f18a2009-07-27 16:47:24 -04001500 /* Try again next time around */
Hank Janssenfceaf242009-07-13 15:34:54 -07001501 DPRINT_ERR(NETVSC, "unable to allocate buffer of size (%d)!!", bytesRecvd);
1502 break;
1503 }
1504
1505 bufferlen = bytesRecvd;
1506 }
1507 else
1508 {
1509 ASSERT(0);
1510 }
1511 } while (1);
1512
1513 PutNetDevice(device);
1514 DPRINT_EXIT(NETVSC);
1515 return;
1516}