]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/staging/hv/NetVsc.c
Staging: hv: Use %ld instead of %d for a long ints
[mirror_ubuntu-artful-kernel.git] / drivers / staging / hv / NetVsc.c
CommitLineData
fceaf24a
HJ
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 * Hank Janssen <hjanssen@microsoft.com>
20 *
21 */
22
5654e932 23#include <linux/kernel.h>
0ffa63b0 24#include <linux/mm.h>
b4362c9c 25#include <linux/delay.h>
c86f3e2a 26#include "include/logging.h"
fceaf24a
HJ
27#include "NetVsc.h"
28#include "RndisFilter.h"
29
30
31//
32// Globals
33//
34static const char* gDriverName="netvsc";
35
36// {F8615163-DF3E-46c5-913F-F2D2F965ED0E}
37static const GUID gNetVscDeviceType={
38 .Data = {0x63, 0x51, 0x61, 0xF8, 0x3E, 0xDF, 0xc5, 0x46, 0x91, 0x3F, 0xF2, 0xD2, 0xF9, 0x65, 0xED, 0x0E}
39};
40
41
42//
43// Internal routines
44//
45static int
46NetVscOnDeviceAdd(
47 DEVICE_OBJECT *Device,
48 void *AdditionalInfo
49 );
50
51static int
52NetVscOnDeviceRemove(
53 DEVICE_OBJECT *Device
54 );
55
56static void
57NetVscOnCleanup(
58 DRIVER_OBJECT *Driver
59 );
60
61static void
62NetVscOnChannelCallback(
8282c400 63 void * context
fceaf24a
HJ
64 );
65
66static int
67NetVscInitializeSendBufferWithNetVsp(
68 DEVICE_OBJECT *Device
69 );
70
71static int
72NetVscInitializeReceiveBufferWithNetVsp(
73 DEVICE_OBJECT *Device
74 );
75
76static int
77NetVscDestroySendBuffer(
78 NETVSC_DEVICE *NetDevice
79 );
80
81static int
82NetVscDestroyReceiveBuffer(
83 NETVSC_DEVICE *NetDevice
84 );
85
86static int
87NetVscConnectToVsp(
88 DEVICE_OBJECT *Device
89 );
90
91static void
92NetVscOnSendCompletion(
93 DEVICE_OBJECT *Device,
94 VMPACKET_DESCRIPTOR *Packet
95 );
96
97static int
98NetVscOnSend(
99 DEVICE_OBJECT *Device,
100 NETVSC_PACKET *Packet
101 );
102
103static void
104NetVscOnReceive(
105 DEVICE_OBJECT *Device,
106 VMPACKET_DESCRIPTOR *Packet
107 );
108
109static void
110NetVscOnReceiveCompletion(
8282c400 111 void * Context
fceaf24a
HJ
112 );
113
114static void
115NetVscSendReceiveCompletion(
116 DEVICE_OBJECT *Device,
59471438 117 u64 TransactionId
fceaf24a
HJ
118 );
119
120static inline NETVSC_DEVICE* AllocNetDevice(DEVICE_OBJECT *Device)
121{
122 NETVSC_DEVICE *netDevice;
123
e276a3a5 124 netDevice = kzalloc(sizeof(NETVSC_DEVICE), GFP_KERNEL);
fceaf24a
HJ
125 if (!netDevice)
126 return NULL;
127
128 // Set to 2 to allow both inbound and outbound traffic
129 InterlockedCompareExchange(&netDevice->RefCount, 2, 0);
130
131 netDevice->Device = Device;
132 Device->Extension = netDevice;
133
134 return netDevice;
135}
136
137static inline void FreeNetDevice(NETVSC_DEVICE *Device)
138{
139 ASSERT(Device->RefCount == 0);
140 Device->Device->Extension = NULL;
8c69f52a 141 kfree(Device);
fceaf24a
HJ
142}
143
144
145// Get the net device object iff exists and its refcount > 1
146static inline NETVSC_DEVICE* GetOutboundNetDevice(DEVICE_OBJECT *Device)
147{
148 NETVSC_DEVICE *netDevice;
149
150 netDevice = (NETVSC_DEVICE*)Device->Extension;
151 if (netDevice && netDevice->RefCount > 1)
152 {
153 InterlockedIncrement(&netDevice->RefCount);
154 }
155 else
156 {
157 netDevice = NULL;
158 }
159
160 return netDevice;
161}
162
163// Get the net device object iff exists and its refcount > 0
164static inline NETVSC_DEVICE* GetInboundNetDevice(DEVICE_OBJECT *Device)
165{
166 NETVSC_DEVICE *netDevice;
167
168 netDevice = (NETVSC_DEVICE*)Device->Extension;
169 if (netDevice && netDevice->RefCount)
170 {
171 InterlockedIncrement(&netDevice->RefCount);
172 }
173 else
174 {
175 netDevice = NULL;
176 }
177
178 return netDevice;
179}
180
181static inline void PutNetDevice(DEVICE_OBJECT *Device)
182{
183 NETVSC_DEVICE *netDevice;
184
185 netDevice = (NETVSC_DEVICE*)Device->Extension;
186 ASSERT(netDevice);
187
188 InterlockedDecrement(&netDevice->RefCount);
189}
190
191static inline NETVSC_DEVICE* ReleaseOutboundNetDevice(DEVICE_OBJECT *Device)
192{
193 NETVSC_DEVICE *netDevice;
194
195 netDevice = (NETVSC_DEVICE*)Device->Extension;
196 if (netDevice == NULL)
197 return NULL;
198
199 // Busy wait until the ref drop to 2, then set it to 1
200 while (InterlockedCompareExchange(&netDevice->RefCount, 1, 2) != 2)
201 {
b4362c9c 202 udelay(100);
fceaf24a
HJ
203 }
204
205 return netDevice;
206}
207
208static inline NETVSC_DEVICE* ReleaseInboundNetDevice(DEVICE_OBJECT *Device)
209{
210 NETVSC_DEVICE *netDevice;
211
212 netDevice = (NETVSC_DEVICE*)Device->Extension;
213 if (netDevice == NULL)
214 return NULL;
215
216 // Busy wait until the ref drop to 1, then set it to 0
217 while (InterlockedCompareExchange(&netDevice->RefCount, 0, 1) != 1)
218 {
b4362c9c 219 udelay(100);
fceaf24a
HJ
220 }
221
222 Device->Extension = NULL;
223 return netDevice;
224}
225
226/*++;
227
228
229Name:
230 NetVscInitialize()
231
232Description:
233 Main entry point
234
235--*/
236int
237NetVscInitialize(
238 DRIVER_OBJECT *drv
239 )
240{
241 NETVSC_DRIVER_OBJECT* driver = (NETVSC_DRIVER_OBJECT*)drv;
242 int ret=0;
243
244 DPRINT_ENTER(NETVSC);
245
246 DPRINT_DBG(NETVSC, "sizeof(NETVSC_PACKET)=%d, sizeof(NVSP_MESSAGE)=%d, sizeof(VMTRANSFER_PAGE_PACKET_HEADER)=%d",
247 sizeof(NETVSC_PACKET), sizeof(NVSP_MESSAGE), sizeof(VMTRANSFER_PAGE_PACKET_HEADER));
248
249 // Make sure we are at least 2 pages since 1 page is used for control
250 ASSERT(driver->RingBufferSize >= (PAGE_SIZE << 1));
251
252 drv->name = gDriverName;
253 memcpy(&drv->deviceType, &gNetVscDeviceType, sizeof(GUID));
254
255 // Make sure it is set by the caller
256 ASSERT(driver->OnReceiveCallback);
257 ASSERT(driver->OnLinkStatusChanged);
258
259 // Setup the dispatch table
260 driver->Base.OnDeviceAdd = NetVscOnDeviceAdd;
261 driver->Base.OnDeviceRemove = NetVscOnDeviceRemove;
262 driver->Base.OnCleanup = NetVscOnCleanup;
263
264 driver->OnSend = NetVscOnSend;
265
266 RndisFilterInit(driver);
267
268 DPRINT_EXIT(NETVSC);
269
270 return ret;
271}
272
273static int
274NetVscInitializeReceiveBufferWithNetVsp(
275 DEVICE_OBJECT *Device
276 )
277{
278 int ret=0;
279 NETVSC_DEVICE *netDevice;
280 NVSP_MESSAGE *initPacket;
281
282 DPRINT_ENTER(NETVSC);
283
284 netDevice = GetOutboundNetDevice(Device);
285 if (!netDevice)
286 {
287 DPRINT_ERR(NETVSC, "unable to get net device...device being destroyed?");
288 DPRINT_EXIT(NETVSC);
289 return -1;
290 }
291 ASSERT(netDevice->ReceiveBufferSize > 0);
292 ASSERT((netDevice->ReceiveBufferSize & (PAGE_SIZE-1)) == 0); // page-size grandularity
293
294 netDevice->ReceiveBuffer = PageAlloc(netDevice->ReceiveBufferSize >> PAGE_SHIFT);
295 if (!netDevice->ReceiveBuffer)
296 {
297 DPRINT_ERR(NETVSC, "unable to allocate receive buffer of size %d", netDevice->ReceiveBufferSize);
298 ret = -1;
299 goto Cleanup;
300 }
c4b0bc94 301 ASSERT(((unsigned long)netDevice->ReceiveBuffer & (PAGE_SIZE-1)) == 0); // page-aligned buffer
fceaf24a
HJ
302
303 DPRINT_INFO(NETVSC, "Establishing receive buffer's GPADL...");
304
305 // Establish the gpadl handle for this buffer on this channel.
306 // Note: This call uses the vmbus connection rather than the channel to establish
307 // the gpadl handle.
308 ret = Device->Driver->VmbusChannelInterface.EstablishGpadl(Device,
309 netDevice->ReceiveBuffer,
310 netDevice->ReceiveBufferSize,
311 &netDevice->ReceiveBufferGpadlHandle);
312
313 if (ret != 0)
314 {
315 DPRINT_ERR(NETVSC, "unable to establish receive buffer's gpadl");
316 goto Cleanup;
317 }
318
319 //WaitEventWait(ext->ChannelInitEvent);
320
321 // Notify the NetVsp of the gpadl handle
322 DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendReceiveBuffer...");
323
324 initPacket = &netDevice->ChannelInitPacket;
325
326 memset(initPacket, 0, sizeof(NVSP_MESSAGE));
327
328 initPacket->Header.MessageType = NvspMessage1TypeSendReceiveBuffer;
329 initPacket->Messages.Version1Messages.SendReceiveBuffer.GpadlHandle = netDevice->ReceiveBufferGpadlHandle;
330 initPacket->Messages.Version1Messages.SendReceiveBuffer.Id = NETVSC_RECEIVE_BUFFER_ID;
331
332 // Send the gpadl notification request
333 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
334 initPacket,
335 sizeof(NVSP_MESSAGE),
c4b0bc94 336 (unsigned long)initPacket,
fceaf24a
HJ
337 VmbusPacketTypeDataInBand,
338 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
339 if (ret != 0)
340 {
341 DPRINT_ERR(NETVSC, "unable to send receive buffer's gpadl to netvsp");
342 goto Cleanup;
343 }
344
345 WaitEventWait(netDevice->ChannelInitEvent);
346
347 // Check the response
348 if (initPacket->Messages.Version1Messages.SendReceiveBufferComplete.Status != NvspStatusSuccess)
349 {
350 DPRINT_ERR(NETVSC,
351 "Unable to complete receive buffer initialzation with NetVsp - status %d",
352 initPacket->Messages.Version1Messages.SendReceiveBufferComplete.Status);
353 ret = -1;
354 goto Cleanup;
355 }
356
357 // Parse the response
358 ASSERT(netDevice->ReceiveSectionCount == 0);
359 ASSERT(netDevice->ReceiveSections == NULL);
360
361 netDevice->ReceiveSectionCount = initPacket->Messages.Version1Messages.SendReceiveBufferComplete.NumSections;
362
e40d37cc 363 netDevice->ReceiveSections = kmalloc(netDevice->ReceiveSectionCount * sizeof(NVSP_1_RECEIVE_BUFFER_SECTION), GFP_KERNEL);
fceaf24a
HJ
364 if (netDevice->ReceiveSections == NULL)
365 {
366 ret = -1;
367 goto Cleanup;
368 }
369
370 memcpy(netDevice->ReceiveSections,
371 initPacket->Messages.Version1Messages.SendReceiveBufferComplete.Sections,
372 netDevice->ReceiveSectionCount * sizeof(NVSP_1_RECEIVE_BUFFER_SECTION));
373
374 DPRINT_INFO(NETVSC,
375 "Receive sections info (count %d, offset %d, endoffset %d, suballoc size %d, num suballocs %d)",
376 netDevice->ReceiveSectionCount, netDevice->ReceiveSections[0].Offset, netDevice->ReceiveSections[0].EndOffset,
377 netDevice->ReceiveSections[0].SubAllocationSize, netDevice->ReceiveSections[0].NumSubAllocations);
378
379
380 //For 1st release, there should only be 1 section that represents the entire receive buffer
381 if (netDevice->ReceiveSectionCount != 1 ||
382 netDevice->ReceiveSections->Offset != 0 )
383 {
384 ret = -1;
385 goto Cleanup;
386 }
387
388 goto Exit;
389
390Cleanup:
391 NetVscDestroyReceiveBuffer(netDevice);
392
393Exit:
394 PutNetDevice(Device);
395 DPRINT_EXIT(NETVSC);
396 return ret;
397}
398
399
400static int
401NetVscInitializeSendBufferWithNetVsp(
402 DEVICE_OBJECT *Device
403 )
404{
405 int ret=0;
406 NETVSC_DEVICE *netDevice;
407 NVSP_MESSAGE *initPacket;
408
409 DPRINT_ENTER(NETVSC);
410
411 netDevice = GetOutboundNetDevice(Device);
412 if (!netDevice)
413 {
414 DPRINT_ERR(NETVSC, "unable to get net device...device being destroyed?");
415 DPRINT_EXIT(NETVSC);
416 return -1;
417 }
418 ASSERT(netDevice->SendBufferSize > 0);
419 ASSERT((netDevice->SendBufferSize & (PAGE_SIZE-1)) == 0); // page-size grandularity
420
421 netDevice->SendBuffer = PageAlloc(netDevice->SendBufferSize >> PAGE_SHIFT);
422 if (!netDevice->SendBuffer)
423 {
424 DPRINT_ERR(NETVSC, "unable to allocate send buffer of size %d", netDevice->SendBufferSize);
425 ret = -1;
426 goto Cleanup;
427 }
c4b0bc94 428 ASSERT(((unsigned long)netDevice->SendBuffer & (PAGE_SIZE-1)) == 0); // page-aligned buffer
fceaf24a
HJ
429
430 DPRINT_INFO(NETVSC, "Establishing send buffer's GPADL...");
431
432 // Establish the gpadl handle for this buffer on this channel.
433 // Note: This call uses the vmbus connection rather than the channel to establish
434 // the gpadl handle.
435 ret = Device->Driver->VmbusChannelInterface.EstablishGpadl(Device,
436 netDevice->SendBuffer,
437 netDevice->SendBufferSize,
438 &netDevice->SendBufferGpadlHandle);
439
440 if (ret != 0)
441 {
442 DPRINT_ERR(NETVSC, "unable to establish send buffer's gpadl");
443 goto Cleanup;
444 }
445
446 //WaitEventWait(ext->ChannelInitEvent);
447
448 // Notify the NetVsp of the gpadl handle
449 DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendSendBuffer...");
450
451 initPacket = &netDevice->ChannelInitPacket;
452
453 memset(initPacket, 0, sizeof(NVSP_MESSAGE));
454
455 initPacket->Header.MessageType = NvspMessage1TypeSendSendBuffer;
456 initPacket->Messages.Version1Messages.SendReceiveBuffer.GpadlHandle = netDevice->SendBufferGpadlHandle;
457 initPacket->Messages.Version1Messages.SendReceiveBuffer.Id = NETVSC_SEND_BUFFER_ID;
458
459 // Send the gpadl notification request
460 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
461 initPacket,
462 sizeof(NVSP_MESSAGE),
c4b0bc94 463 (unsigned long)initPacket,
fceaf24a
HJ
464 VmbusPacketTypeDataInBand,
465 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
466 if (ret != 0)
467 {
468 DPRINT_ERR(NETVSC, "unable to send receive buffer's gpadl to netvsp");
469 goto Cleanup;
470 }
471
472 WaitEventWait(netDevice->ChannelInitEvent);
473
474 // Check the response
475 if (initPacket->Messages.Version1Messages.SendSendBufferComplete.Status != NvspStatusSuccess)
476 {
477 DPRINT_ERR(NETVSC,
478 "Unable to complete send buffer initialzation with NetVsp - status %d",
479 initPacket->Messages.Version1Messages.SendSendBufferComplete.Status);
480 ret = -1;
481 goto Cleanup;
482 }
483
484 netDevice->SendSectionSize = initPacket->Messages.Version1Messages.SendSendBufferComplete.SectionSize;
485
486 goto Exit;
487
488Cleanup:
489 NetVscDestroySendBuffer(netDevice);
490
491Exit:
492 PutNetDevice(Device);
493 DPRINT_EXIT(NETVSC);
494 return ret;
495}
496
497static int
498NetVscDestroyReceiveBuffer(
499 NETVSC_DEVICE *NetDevice
500 )
501{
502 NVSP_MESSAGE *revokePacket;
503 int ret=0;
504
505
506 DPRINT_ENTER(NETVSC);
507
508 // If we got a section count, it means we received a SendReceiveBufferComplete msg
509 // (ie sent NvspMessage1TypeSendReceiveBuffer msg) therefore, we need to send a revoke msg here
510 if (NetDevice->ReceiveSectionCount)
511 {
512 DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeRevokeReceiveBuffer...");
513
514 // Send the revoke receive buffer
515 revokePacket = &NetDevice->RevokePacket;
516 memset(revokePacket, 0, sizeof(NVSP_MESSAGE));
517
518 revokePacket->Header.MessageType = NvspMessage1TypeRevokeReceiveBuffer;
519 revokePacket->Messages.Version1Messages.RevokeReceiveBuffer.Id = NETVSC_RECEIVE_BUFFER_ID;
520
521 ret = NetDevice->Device->Driver->VmbusChannelInterface.SendPacket(NetDevice->Device,
522 revokePacket,
523 sizeof(NVSP_MESSAGE),
c4b0bc94 524 (unsigned long)revokePacket,
fceaf24a
HJ
525 VmbusPacketTypeDataInBand,
526 0);
527 // If we failed here, we might as well return and have a leak rather than continue and a bugchk
528 if (ret != 0)
529 {
530 DPRINT_ERR(NETVSC, "unable to send revoke receive buffer to netvsp");
531 DPRINT_EXIT(NETVSC);
532 return -1;
533 }
534 }
535
536 // Teardown the gpadl on the vsp end
537 if (NetDevice->ReceiveBufferGpadlHandle)
538 {
539 DPRINT_INFO(NETVSC, "Tearing down receive buffer's GPADL...");
540
541 ret = NetDevice->Device->Driver->VmbusChannelInterface.TeardownGpadl(NetDevice->Device,
542 NetDevice->ReceiveBufferGpadlHandle);
543
544 // If we failed here, we might as well return and have a leak rather than continue and a bugchk
545 if (ret != 0)
546 {
547 DPRINT_ERR(NETVSC, "unable to teardown receive buffer's gpadl");
548 DPRINT_EXIT(NETVSC);
549 return -1;
550 }
551 NetDevice->ReceiveBufferGpadlHandle = 0;
552 }
553
554 if (NetDevice->ReceiveBuffer)
555 {
556 DPRINT_INFO(NETVSC, "Freeing up receive buffer...");
557
558 // Free up the receive buffer
559 PageFree(NetDevice->ReceiveBuffer, NetDevice->ReceiveBufferSize >> PAGE_SHIFT);
560 NetDevice->ReceiveBuffer = NULL;
561 }
562
563 if (NetDevice->ReceiveSections)
564 {
8c69f52a 565 kfree(NetDevice->ReceiveSections);
fceaf24a
HJ
566 NetDevice->ReceiveSections = NULL;
567 NetDevice->ReceiveSectionCount = 0;
568 }
569
570 DPRINT_EXIT(NETVSC);
571
572 return ret;
573}
574
575
576
577
578static int
579NetVscDestroySendBuffer(
580 NETVSC_DEVICE *NetDevice
581 )
582{
583 NVSP_MESSAGE *revokePacket;
584 int ret=0;
585
586
587 DPRINT_ENTER(NETVSC);
588
589 // If we got a section count, it means we received a SendReceiveBufferComplete msg
590 // (ie sent NvspMessage1TypeSendReceiveBuffer msg) therefore, we need to send a revoke msg here
591 if (NetDevice->SendSectionSize)
592 {
593 DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeRevokeSendBuffer...");
594
595 // Send the revoke send buffer
596 revokePacket = &NetDevice->RevokePacket;
597 memset(revokePacket, 0, sizeof(NVSP_MESSAGE));
598
599 revokePacket->Header.MessageType = NvspMessage1TypeRevokeSendBuffer;
600 revokePacket->Messages.Version1Messages.RevokeSendBuffer.Id = NETVSC_SEND_BUFFER_ID;
601
602 ret = NetDevice->Device->Driver->VmbusChannelInterface.SendPacket(NetDevice->Device,
603 revokePacket,
604 sizeof(NVSP_MESSAGE),
c4b0bc94 605 (unsigned long)revokePacket,
fceaf24a
HJ
606 VmbusPacketTypeDataInBand,
607 0);
608 // If we failed here, we might as well return and have a leak rather than continue and a bugchk
609 if (ret != 0)
610 {
611 DPRINT_ERR(NETVSC, "unable to send revoke send buffer to netvsp");
612 DPRINT_EXIT(NETVSC);
613 return -1;
614 }
615 }
616
617 // Teardown the gpadl on the vsp end
618 if (NetDevice->SendBufferGpadlHandle)
619 {
620 DPRINT_INFO(NETVSC, "Tearing down send buffer's GPADL...");
621
622 ret = NetDevice->Device->Driver->VmbusChannelInterface.TeardownGpadl(NetDevice->Device,
623 NetDevice->SendBufferGpadlHandle);
624
625 // If we failed here, we might as well return and have a leak rather than continue and a bugchk
626 if (ret != 0)
627 {
628 DPRINT_ERR(NETVSC, "unable to teardown send buffer's gpadl");
629 DPRINT_EXIT(NETVSC);
630 return -1;
631 }
632 NetDevice->SendBufferGpadlHandle = 0;
633 }
634
635 if (NetDevice->SendBuffer)
636 {
637 DPRINT_INFO(NETVSC, "Freeing up send buffer...");
638
639 // Free up the receive buffer
640 PageFree(NetDevice->SendBuffer, NetDevice->SendBufferSize >> PAGE_SHIFT);
641 NetDevice->SendBuffer = NULL;
642 }
643
644 DPRINT_EXIT(NETVSC);
645
646 return ret;
647}
648
649
650
651static int
652NetVscConnectToVsp(
653 DEVICE_OBJECT *Device
654 )
655{
656 int ret=0;
657 NETVSC_DEVICE *netDevice;
658 NVSP_MESSAGE *initPacket;
659 int ndisVersion;
660
661 DPRINT_ENTER(NETVSC);
662
663 netDevice = GetOutboundNetDevice(Device);
664 if (!netDevice)
665 {
666 DPRINT_ERR(NETVSC, "unable to get net device...device being destroyed?");
667 DPRINT_EXIT(NETVSC);
668 return -1;
669 }
670
671 initPacket = &netDevice->ChannelInitPacket;
672
673 memset(initPacket, 0, sizeof(NVSP_MESSAGE));
674 initPacket->Header.MessageType = NvspMessageTypeInit;
675 initPacket->Messages.InitMessages.Init.MinProtocolVersion = NVSP_MIN_PROTOCOL_VERSION;
676 initPacket->Messages.InitMessages.Init.MaxProtocolVersion = NVSP_MAX_PROTOCOL_VERSION;
677
678 DPRINT_INFO(NETVSC, "Sending NvspMessageTypeInit...");
679
680 // Send the init request
681 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
682 initPacket,
683 sizeof(NVSP_MESSAGE),
c4b0bc94 684 (unsigned long)initPacket,
fceaf24a
HJ
685 VmbusPacketTypeDataInBand,
686 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
687
688 if( ret != 0)
689 {
690 DPRINT_ERR(NETVSC, "unable to send NvspMessageTypeInit");
691 goto Cleanup;
692 }
693
694 WaitEventWait(netDevice->ChannelInitEvent);
695
696 // Now, check the response
697 //ASSERT(initPacket->Messages.InitMessages.InitComplete.MaximumMdlChainLength <= MAX_MULTIPAGE_BUFFER_COUNT);
698 DPRINT_INFO(NETVSC, "NvspMessageTypeInit status(%d) max mdl chain (%d)",
699 initPacket->Messages.InitMessages.InitComplete.Status,
700 initPacket->Messages.InitMessages.InitComplete.MaximumMdlChainLength);
701
702 if (initPacket->Messages.InitMessages.InitComplete.Status != NvspStatusSuccess)
703 {
704 DPRINT_ERR(NETVSC, "unable to initialize with netvsp (status 0x%x)", initPacket->Messages.InitMessages.InitComplete.Status);
705 ret = -1;
706 goto Cleanup;
707 }
708
709 if (initPacket->Messages.InitMessages.InitComplete.NegotiatedProtocolVersion != NVSP_PROTOCOL_VERSION_1)
710 {
711 DPRINT_ERR(NETVSC, "unable to initialize with netvsp (version expected 1 got %d)",
712 initPacket->Messages.InitMessages.InitComplete.NegotiatedProtocolVersion);
713 ret = -1;
714 goto Cleanup;
715 }
716 DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendNdisVersion...");
717
718 // Send the ndis version
719 memset(initPacket, 0, sizeof(NVSP_MESSAGE));
720
721 ndisVersion = 0x00050000;
722
723 initPacket->Header.MessageType = NvspMessage1TypeSendNdisVersion;
724 initPacket->Messages.Version1Messages.SendNdisVersion.NdisMajorVersion = (ndisVersion & 0xFFFF0000) >> 16;
725 initPacket->Messages.Version1Messages.SendNdisVersion.NdisMinorVersion = ndisVersion & 0xFFFF;
726
727 // Send the init request
728 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
729 initPacket,
730 sizeof(NVSP_MESSAGE),
c4b0bc94 731 (unsigned long)initPacket,
fceaf24a
HJ
732 VmbusPacketTypeDataInBand,
733 0);
734 if (ret != 0)
735 {
736 DPRINT_ERR(NETVSC, "unable to send NvspMessage1TypeSendNdisVersion");
737 ret = -1;
738 goto Cleanup;
739 }
740 //
741 // BUGBUG - We have to wait for the above msg since the netvsp uses KMCL which acknowledges packet (completion packet)
742 // since our Vmbus always set the VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED flag
743 //WaitEventWait(NetVscChannel->ChannelInitEvent);
744
745 // Post the big receive buffer to NetVSP
746 ret = NetVscInitializeReceiveBufferWithNetVsp(Device);
747 if (ret == 0)
748 {
749 ret = NetVscInitializeSendBufferWithNetVsp(Device);
750 }
751
752Cleanup:
753 PutNetDevice(Device);
754 DPRINT_EXIT(NETVSC);
755 return ret;
756}
757
758static void
759NetVscDisconnectFromVsp(
760 NETVSC_DEVICE *NetDevice
761 )
762{
763 DPRINT_ENTER(NETVSC);
764
765 NetVscDestroyReceiveBuffer(NetDevice);
766 NetVscDestroySendBuffer(NetDevice);
767
768 DPRINT_EXIT(NETVSC);
769}
770
771
772/*++
773
774Name:
775 NetVscOnDeviceAdd()
776
777Description:
778 Callback when the device belonging to this driver is added
779
780--*/
781int
782NetVscOnDeviceAdd(
783 DEVICE_OBJECT *Device,
784 void *AdditionalInfo
785 )
786{
787 int ret=0;
788 int i;
789
790 NETVSC_DEVICE* netDevice;
791 NETVSC_PACKET* packet;
792 LIST_ENTRY *entry;
793
794 NETVSC_DRIVER_OBJECT *netDriver = (NETVSC_DRIVER_OBJECT*) Device->Driver;;
795
796 DPRINT_ENTER(NETVSC);
797
798 netDevice = AllocNetDevice(Device);
799 if (!netDevice)
800 {
801 ret = -1;
802 goto Cleanup;
803 }
804
805 DPRINT_DBG(NETVSC, "netvsc channel object allocated - %p", netDevice);
806
807 // Initialize the NetVSC channel extension
808 netDevice->ReceiveBufferSize = NETVSC_RECEIVE_BUFFER_SIZE;
6436873a 809 spin_lock_init(&netDevice->receive_packet_list_lock);
fceaf24a
HJ
810
811 netDevice->SendBufferSize = NETVSC_SEND_BUFFER_SIZE;
812
813 INITIALIZE_LIST_HEAD(&netDevice->ReceivePacketList);
814
815 for (i=0; i < NETVSC_RECEIVE_PACKETLIST_COUNT; i++)
816 {
e276a3a5 817 packet = kzalloc(sizeof(NETVSC_PACKET) + (NETVSC_RECEIVE_SG_COUNT* sizeof(PAGE_BUFFER)), GFP_KERNEL);
fceaf24a
HJ
818 if (!packet)
819 {
820 DPRINT_DBG(NETVSC, "unable to allocate netvsc pkts for receive pool (wanted %d got %d)", NETVSC_RECEIVE_PACKETLIST_COUNT, i);
821 break;
822 }
823
824 INSERT_TAIL_LIST(&netDevice->ReceivePacketList, &packet->ListEntry);
825 }
826 netDevice->ChannelInitEvent = WaitEventCreate();
827
828 // Open the channel
829 ret = Device->Driver->VmbusChannelInterface.Open(Device,
830 netDriver->RingBufferSize,
831 netDriver->RingBufferSize,
832 NULL, 0,
833 NetVscOnChannelCallback,
834 Device
835 );
836
837 if (ret != 0)
838 {
839 DPRINT_ERR(NETVSC, "unable to open channel: %d", ret);
840 ret = -1;
841 goto Cleanup;
842 }
843
844 // Channel is opened
845 DPRINT_INFO(NETVSC, "*** NetVSC channel opened successfully! ***");
846
847 // Connect with the NetVsp
848 ret = NetVscConnectToVsp(Device);
849 if (ret != 0)
850 {
851 DPRINT_ERR(NETVSC, "unable to connect to NetVSP - %d", ret);
852 ret = -1;
853 goto Close;
854 }
855
856 DPRINT_INFO(NETVSC, "*** NetVSC channel handshake result - %d ***", ret);
857
858 DPRINT_EXIT(NETVSC);
859 return ret;
860
861Close:
862 // Now, we can close the channel safely
863 Device->Driver->VmbusChannelInterface.Close(Device);
864
865Cleanup:
866
867 if (netDevice)
868 {
869 WaitEventClose(netDevice->ChannelInitEvent);
870
871 while (!IsListEmpty(&netDevice->ReceivePacketList))
872 {
873 entry = REMOVE_HEAD_LIST(&netDevice->ReceivePacketList);
874 packet = CONTAINING_RECORD(entry, NETVSC_PACKET, ListEntry);
8c69f52a 875 kfree(packet);
fceaf24a
HJ
876 }
877
fceaf24a
HJ
878 ReleaseOutboundNetDevice(Device);
879 ReleaseInboundNetDevice(Device);
880
881 FreeNetDevice(netDevice);
882 }
883
884 DPRINT_EXIT(NETVSC);
885 return ret;
886}
887
888
889/*++
890
891Name:
892 NetVscOnDeviceRemove()
893
894Description:
895 Callback when the root bus device is removed
896
897--*/
898int
899NetVscOnDeviceRemove(
900 DEVICE_OBJECT *Device
901 )
902{
903 NETVSC_DEVICE *netDevice;
904 NETVSC_PACKET *netvscPacket;
905 int ret=0;
906 LIST_ENTRY *entry;
907
908 DPRINT_ENTER(NETVSC);
909
910 DPRINT_INFO(NETVSC, "Disabling outbound traffic on net device (%p)...", Device->Extension);
911
912 // Stop outbound traffic ie sends and receives completions
913 netDevice = ReleaseOutboundNetDevice(Device);
914 if (!netDevice)
915 {
916 DPRINT_ERR(NETVSC, "No net device present!!");
917 return -1;
918 }
919
920 // Wait for all send completions
921 while (netDevice->NumOutstandingSends)
922 {
923 DPRINT_INFO(NETVSC, "waiting for %d requests to complete...", netDevice->NumOutstandingSends);
924
b4362c9c 925 udelay(100);
fceaf24a
HJ
926 }
927
928 DPRINT_INFO(NETVSC, "Disconnecting from netvsp...");
929
930 NetVscDisconnectFromVsp(netDevice);
931
932 DPRINT_INFO(NETVSC, "Disabling inbound traffic on net device (%p)...", Device->Extension);
933
934 // Stop inbound traffic ie receives and sends completions
935 netDevice = ReleaseInboundNetDevice(Device);
936
937 // At this point, no one should be accessing netDevice except in here
938 DPRINT_INFO(NETVSC, "net device (%p) safe to remove", netDevice);
939
940 // Now, we can close the channel safely
941 Device->Driver->VmbusChannelInterface.Close(Device);
942
943 // Release all resources
944 while (!IsListEmpty(&netDevice->ReceivePacketList))
945 {
946 entry = REMOVE_HEAD_LIST(&netDevice->ReceivePacketList);
947 netvscPacket = CONTAINING_RECORD(entry, NETVSC_PACKET, ListEntry);
948
8c69f52a 949 kfree(netvscPacket);
fceaf24a
HJ
950 }
951
fceaf24a
HJ
952 WaitEventClose(netDevice->ChannelInitEvent);
953 FreeNetDevice(netDevice);
954
955 DPRINT_EXIT(NETVSC);
956 return ret;
957}
958
959
960
961/*++
962
963Name:
964 NetVscOnCleanup()
965
966Description:
967 Perform any cleanup when the driver is removed
968
969--*/
970void
971NetVscOnCleanup(
972 DRIVER_OBJECT *drv
973 )
974{
975 DPRINT_ENTER(NETVSC);
976
977 DPRINT_EXIT(NETVSC);
978}
979
980static void
981NetVscOnSendCompletion(
982 DEVICE_OBJECT *Device,
983 VMPACKET_DESCRIPTOR *Packet
984 )
985{
986 NETVSC_DEVICE* netDevice;
987 NVSP_MESSAGE *nvspPacket;
988 NETVSC_PACKET *nvscPacket;
989
990 DPRINT_ENTER(NETVSC);
991
992 netDevice = GetInboundNetDevice(Device);
993 if (!netDevice)
994 {
995 DPRINT_ERR(NETVSC, "unable to get net device...device being destroyed?");
996 DPRINT_EXIT(NETVSC);
997 return;
998 }
999
c4b0bc94 1000 nvspPacket = (NVSP_MESSAGE*)((unsigned long)Packet + (Packet->DataOffset8 << 3));
fceaf24a
HJ
1001
1002 DPRINT_DBG(NETVSC, "send completion packet - type %d", nvspPacket->Header.MessageType);
1003
1004 if (nvspPacket->Header.MessageType == NvspMessageTypeInitComplete ||
1005 nvspPacket->Header.MessageType == NvspMessage1TypeSendReceiveBufferComplete ||
1006 nvspPacket->Header.MessageType == NvspMessage1TypeSendSendBufferComplete)
1007 {
1008 // Copy the response back
1009 memcpy(&netDevice->ChannelInitPacket, nvspPacket, sizeof(NVSP_MESSAGE));
1010 WaitEventSet(netDevice->ChannelInitEvent);
1011 }
1012 else if (nvspPacket->Header.MessageType == NvspMessage1TypeSendRNDISPacketComplete)
1013 {
1014 // Get the send context
c4b0bc94 1015 nvscPacket = (NETVSC_PACKET *)(unsigned long)Packet->TransactionId;
fceaf24a
HJ
1016 ASSERT(nvscPacket);
1017
1018 // Notify the layer above us
1019 nvscPacket->Completion.Send.OnSendCompletion(nvscPacket->Completion.Send.SendCompletionContext);
1020
1021 InterlockedDecrement(&netDevice->NumOutstandingSends);
1022 }
1023 else
1024 {
1025 DPRINT_ERR(NETVSC, "Unknown send completion packet type - %d received!!", nvspPacket->Header.MessageType);
1026 }
1027
1028 PutNetDevice(Device);
1029 DPRINT_EXIT(NETVSC);
1030}
1031
1032
1033
1034static int
1035NetVscOnSend(
1036 DEVICE_OBJECT *Device,
1037 NETVSC_PACKET *Packet
1038 )
1039{
1040 NETVSC_DEVICE* netDevice;
1041 int ret=0;
1042
1043 NVSP_MESSAGE sendMessage;
1044
1045 DPRINT_ENTER(NETVSC);
1046
1047 netDevice = GetOutboundNetDevice(Device);
1048 if (!netDevice)
1049 {
1050 DPRINT_ERR(NETVSC, "net device (%p) shutting down...ignoring outbound packets", netDevice);
1051 DPRINT_EXIT(NETVSC);
1052 return -2;
1053 }
1054
1055 sendMessage.Header.MessageType = NvspMessage1TypeSendRNDISPacket;
1056 if (Packet->IsDataPacket)
1057 sendMessage.Messages.Version1Messages.SendRNDISPacket.ChannelType = 0;// 0 is RMC_DATA;
1058 else
1059 sendMessage.Messages.Version1Messages.SendRNDISPacket.ChannelType = 1;// 1 is RMC_CONTROL;
1060
1061 // Not using send buffer section
1062 sendMessage.Messages.Version1Messages.SendRNDISPacket.SendBufferSectionIndex = 0xFFFFFFFF;
1063 sendMessage.Messages.Version1Messages.SendRNDISPacket.SendBufferSectionSize = 0;
1064
1065 if (Packet->PageBufferCount)
1066 {
1067 ret = Device->Driver->VmbusChannelInterface.SendPacketPageBuffer(Device,
1068 Packet->PageBuffers,
1069 Packet->PageBufferCount,
1070 &sendMessage,
1071 sizeof(NVSP_MESSAGE),
c4b0bc94 1072 (unsigned long)Packet);
fceaf24a
HJ
1073 }
1074 else
1075 {
1076 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
1077 &sendMessage,
1078 sizeof(NVSP_MESSAGE),
c4b0bc94 1079 (unsigned long)Packet,
fceaf24a
HJ
1080 VmbusPacketTypeDataInBand,
1081 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1082
1083 }
1084
1085 if (ret != 0)
1086 {
1087 DPRINT_ERR(NETVSC, "Unable to send packet %p ret %d", Packet, ret);
1088 }
1089
1090 InterlockedIncrement(&netDevice->NumOutstandingSends);
1091 PutNetDevice(Device);
1092
1093 DPRINT_EXIT(NETVSC);
1094 return ret;
1095}
1096
1097
1098static void
1099NetVscOnReceive(
1100 DEVICE_OBJECT *Device,
1101 VMPACKET_DESCRIPTOR *Packet
1102 )
1103{
1104 NETVSC_DEVICE* netDevice;
1105 VMTRANSFER_PAGE_PACKET_HEADER *vmxferpagePacket;
1106 NVSP_MESSAGE *nvspPacket;
1107 NETVSC_PACKET *netvscPacket=NULL;
1108 LIST_ENTRY* entry;
c4b0bc94
GKH
1109 unsigned long start;
1110 unsigned long end, endVirtual;
fceaf24a
HJ
1111 //NETVSC_DRIVER_OBJECT *netvscDriver;
1112 XFERPAGE_PACKET *xferpagePacket=NULL;
1113 LIST_ENTRY listHead;
1114
1115 int i=0, j=0;
1116 int count=0, bytesRemain=0;
6436873a 1117 unsigned long flags;
fceaf24a
HJ
1118
1119 DPRINT_ENTER(NETVSC);
1120
1121 netDevice = GetInboundNetDevice(Device);
1122 if (!netDevice)
1123 {
1124 DPRINT_ERR(NETVSC, "unable to get net device...device being destroyed?");
1125 DPRINT_EXIT(NETVSC);
1126 return;
1127 }
1128
1129 // All inbound packets other than send completion should be xfer page packet
1130 if (Packet->Type != VmbusPacketTypeDataUsingTransferPages)
1131 {
1132 DPRINT_ERR(NETVSC, "Unknown packet type received - %d", Packet->Type);
1133 PutNetDevice(Device);
1134 return;
1135 }
1136
c4b0bc94 1137 nvspPacket = (NVSP_MESSAGE*)((unsigned long)Packet + (Packet->DataOffset8 << 3));
fceaf24a
HJ
1138
1139 // Make sure this is a valid nvsp packet
1140 if (nvspPacket->Header.MessageType != NvspMessage1TypeSendRNDISPacket )
1141 {
1142 DPRINT_ERR(NETVSC, "Unknown nvsp packet type received - %d", nvspPacket->Header.MessageType);
1143 PutNetDevice(Device);
1144 return;
1145 }
1146
1147 DPRINT_DBG(NETVSC, "NVSP packet received - type %d", nvspPacket->Header.MessageType);
1148
1149 vmxferpagePacket = (VMTRANSFER_PAGE_PACKET_HEADER*)Packet;
1150
1151 if (vmxferpagePacket->TransferPageSetId != NETVSC_RECEIVE_BUFFER_ID)
1152 {
1153 DPRINT_ERR(NETVSC, "Invalid xfer page set id - expecting %x got %x", NETVSC_RECEIVE_BUFFER_ID, vmxferpagePacket->TransferPageSetId);
1154 PutNetDevice(Device);
1155 return;
1156 }
1157
1158 DPRINT_DBG(NETVSC, "xfer page - range count %d", vmxferpagePacket->RangeCount);
1159
1160 INITIALIZE_LIST_HEAD(&listHead);
1161
1162 // Grab free packets (range count + 1) to represent this xfer page packet. +1 to represent
1163 // the xfer page packet itself. We grab it here so that we know exactly how many we can fulfil
6436873a 1164 spin_lock_irqsave(&netDevice->receive_packet_list_lock, flags);
fceaf24a
HJ
1165 while (!IsListEmpty(&netDevice->ReceivePacketList))
1166 {
1167 entry = REMOVE_HEAD_LIST(&netDevice->ReceivePacketList);
1168 netvscPacket = CONTAINING_RECORD(entry, NETVSC_PACKET, ListEntry);
1169
1170 INSERT_TAIL_LIST(&listHead, &netvscPacket->ListEntry);
1171
1172 if (++count == vmxferpagePacket->RangeCount + 1)
1173 break;
1174 }
6436873a 1175 spin_unlock_irqrestore(&netDevice->receive_packet_list_lock, flags);
fceaf24a
HJ
1176
1177 // We need at least 2 netvsc pkts (1 to represent the xfer page and at least 1 for the range)
1178 // i.e. we can handled some of the xfer page packet ranges...
1179 if (count < 2)
1180 {
1181 DPRINT_ERR(NETVSC, "Got only %d netvsc pkt...needed %d pkts. Dropping this xfer page packet completely!", count, vmxferpagePacket->RangeCount+1);
1182
1183 // Return it to the freelist
6436873a 1184 spin_lock_irqsave(&netDevice->receive_packet_list_lock, flags);
fceaf24a
HJ
1185 for (i=count; i != 0; i--)
1186 {
1187 entry = REMOVE_HEAD_LIST(&listHead);
1188 netvscPacket = CONTAINING_RECORD(entry, NETVSC_PACKET, ListEntry);
1189
1190 INSERT_TAIL_LIST(&netDevice->ReceivePacketList, &netvscPacket->ListEntry);
1191 }
6436873a 1192 spin_unlock_irqrestore(&netDevice->receive_packet_list_lock, flags);
fceaf24a
HJ
1193
1194 NetVscSendReceiveCompletion(Device, vmxferpagePacket->d.TransactionId);
1195
1196 PutNetDevice(Device);
1197 return;
1198 }
1199
1200 // Remove the 1st packet to represent the xfer page packet itself
1201 entry = REMOVE_HEAD_LIST(&listHead);
1202 xferpagePacket = CONTAINING_RECORD(entry, XFERPAGE_PACKET, ListEntry);
1203 xferpagePacket->Count = count - 1; // This is how much we can satisfy
1204 ASSERT(xferpagePacket->Count > 0 && xferpagePacket->Count <= vmxferpagePacket->RangeCount);
1205
1206 if (xferpagePacket->Count != vmxferpagePacket->RangeCount)
1207 {
1208 DPRINT_INFO(NETVSC, "Needed %d netvsc pkts to satisy this xfer page...got %d", vmxferpagePacket->RangeCount, xferpagePacket->Count);
1209 }
1210
1211 // Each range represents 1 RNDIS pkt that contains 1 ethernet frame
1212 for (i=0; i < (count - 1); i++)
1213 {
1214 entry = REMOVE_HEAD_LIST(&listHead);
1215 netvscPacket = CONTAINING_RECORD(entry, NETVSC_PACKET, ListEntry);
1216
1217 // Initialize the netvsc packet
1218 netvscPacket->XferPagePacket = xferpagePacket;
1219 netvscPacket->Completion.Recv.OnReceiveCompletion = NetVscOnReceiveCompletion;
1220 netvscPacket->Completion.Recv.ReceiveCompletionContext = netvscPacket;
1221 netvscPacket->Device = Device;
1222 netvscPacket->Completion.Recv.ReceiveCompletionTid = vmxferpagePacket->d.TransactionId; // Save this so that we can send it back
1223
1224 netvscPacket->TotalDataBufferLength = vmxferpagePacket->Ranges[i].ByteCount;
1225 netvscPacket->PageBufferCount = 1;
1226
1227 ASSERT(vmxferpagePacket->Ranges[i].ByteOffset + vmxferpagePacket->Ranges[i].ByteCount < netDevice->ReceiveBufferSize);
1228
1229 netvscPacket->PageBuffers[0].Length = vmxferpagePacket->Ranges[i].ByteCount;
1230
c4b0bc94 1231 start = GetPhysicalAddress((void*)((unsigned long)netDevice->ReceiveBuffer + vmxferpagePacket->Ranges[i].ByteOffset));
fceaf24a
HJ
1232
1233 netvscPacket->PageBuffers[0].Pfn = start >> PAGE_SHIFT;
c4b0bc94 1234 endVirtual = (unsigned long)netDevice->ReceiveBuffer
fceaf24a
HJ
1235 + vmxferpagePacket->Ranges[i].ByteOffset
1236 + vmxferpagePacket->Ranges[i].ByteCount -1;
1237 end = GetPhysicalAddress((void*)endVirtual);
1238
1239 // Calculate the page relative offset
1240 netvscPacket->PageBuffers[0].Offset = vmxferpagePacket->Ranges[i].ByteOffset & (PAGE_SIZE -1);
1241 if ((end >> PAGE_SHIFT) != (start>>PAGE_SHIFT)) {
1242 //Handle frame across multiple pages:
1243 netvscPacket->PageBuffers[0].Length =
1244 (netvscPacket->PageBuffers[0].Pfn <<PAGE_SHIFT) + PAGE_SIZE - start;
1245 bytesRemain = netvscPacket->TotalDataBufferLength - netvscPacket->PageBuffers[0].Length;
1246 for (j=1; j<NETVSC_PACKET_MAXPAGE; j++) {
1247 netvscPacket->PageBuffers[j].Offset = 0;
1248 if (bytesRemain <= PAGE_SIZE) {
1249 netvscPacket->PageBuffers[j].Length = bytesRemain;
1250 bytesRemain = 0;
1251 } else {
1252 netvscPacket->PageBuffers[j].Length = PAGE_SIZE;
1253 bytesRemain -= PAGE_SIZE;
1254 }
1255 netvscPacket->PageBuffers[j].Pfn =
1256 GetPhysicalAddress((void*)(endVirtual - bytesRemain)) >> PAGE_SHIFT;
1257 netvscPacket->PageBufferCount++;
1258 if (bytesRemain == 0)
1259 break;
1260 }
1261 ASSERT(bytesRemain == 0);
1262 }
1263 DPRINT_DBG(NETVSC, "[%d] - (abs offset %u len %u) => (pfn %llx, offset %u, len %u)",
1264 i,
1265 vmxferpagePacket->Ranges[i].ByteOffset,
1266 vmxferpagePacket->Ranges[i].ByteCount,
1267 netvscPacket->PageBuffers[0].Pfn,
1268 netvscPacket->PageBuffers[0].Offset,
1269 netvscPacket->PageBuffers[0].Length);
1270
1271 // Pass it to the upper layer
1272 ((NETVSC_DRIVER_OBJECT*)Device->Driver)->OnReceiveCallback(Device, netvscPacket);
1273
1274 NetVscOnReceiveCompletion(netvscPacket->Completion.Recv.ReceiveCompletionContext);
1275 }
1276
1277 ASSERT(IsListEmpty(&listHead));
1278
1279 PutNetDevice(Device);
1280 DPRINT_EXIT(NETVSC);
1281}
1282
1283
1284static void
1285NetVscSendReceiveCompletion(
1286 DEVICE_OBJECT *Device,
59471438 1287 u64 TransactionId
fceaf24a
HJ
1288 )
1289{
1290 NVSP_MESSAGE recvcompMessage;
1291 int retries=0;
1292 int ret=0;
1293
1294 DPRINT_DBG(NETVSC, "Sending receive completion pkt - %llx", TransactionId);
1295
1296 recvcompMessage.Header.MessageType = NvspMessage1TypeSendRNDISPacketComplete;
1297
1298 // FIXME: Pass in the status
1299 recvcompMessage.Messages.Version1Messages.SendRNDISPacketComplete.Status = NvspStatusSuccess;
1300
1301retry_send_cmplt:
1302 // Send the completion
1303 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
1304 &recvcompMessage,
1305 sizeof(NVSP_MESSAGE),
1306 TransactionId,
1307 VmbusPacketTypeCompletion,
1308 0);
1309 if (ret == 0) // success
1310 {
1311 // no-op
1312 }
1313 else if (ret == -1) // no more room...wait a bit and attempt to retry 3 times
1314 {
1315 retries++;
1316 DPRINT_ERR(NETVSC, "unable to send receive completion pkt (tid %llx)...retrying %d", TransactionId, retries);
1317
1318 if (retries < 4)
1319 {
b4362c9c 1320 udelay(100);
fceaf24a
HJ
1321 goto retry_send_cmplt;
1322 }
1323 else
1324 {
1325 DPRINT_ERR(NETVSC, "unable to send receive completion pkt (tid %llx)...give up retrying", TransactionId);
1326 }
1327 }
1328 else
1329 {
1330 DPRINT_ERR(NETVSC, "unable to send receive completion pkt - %llx", TransactionId);
1331 }
1332}
1333
1334//
1335// Send a receive completion packet to RNDIS device (ie NetVsp)
1336//
1337static void
1338NetVscOnReceiveCompletion(
8282c400 1339 void * Context)
fceaf24a
HJ
1340{
1341 NETVSC_PACKET *packet = (NETVSC_PACKET*)Context;
1342 DEVICE_OBJECT *device = (DEVICE_OBJECT*)packet->Device;
1343 NETVSC_DEVICE* netDevice;
59471438 1344 u64 transactionId=0;
0e727613 1345 bool fSendReceiveComp = false;
6436873a 1346 unsigned long flags;
fceaf24a
HJ
1347
1348 DPRINT_ENTER(NETVSC);
1349
1350 ASSERT(packet->XferPagePacket);
1351
1352 // Even though it seems logical to do a GetOutboundNetDevice() here to send out receive completion,
1353 // we are using GetInboundNetDevice() since we may have disable outbound traffic already.
1354 netDevice = GetInboundNetDevice(device);
1355 if (!netDevice)
1356 {
1357 DPRINT_ERR(NETVSC, "unable to get net device...device being destroyed?");
1358 DPRINT_EXIT(NETVSC);
1359 return;
1360 }
1361
1362 // Overloading use of the lock.
6436873a 1363 spin_lock_irqsave(&netDevice->receive_packet_list_lock, flags);
fceaf24a
HJ
1364
1365 ASSERT(packet->XferPagePacket->Count > 0);
1366 packet->XferPagePacket->Count--;
1367
1368 // Last one in the line that represent 1 xfer page packet.
1369 // Return the xfer page packet itself to the freelist
1370 if (packet->XferPagePacket->Count == 0)
1371 {
0e727613 1372 fSendReceiveComp = true;
fceaf24a
HJ
1373 transactionId = packet->Completion.Recv.ReceiveCompletionTid;
1374
1375 INSERT_TAIL_LIST(&netDevice->ReceivePacketList, &packet->XferPagePacket->ListEntry);
1376 }
1377
1378 // Put the packet back
1379 INSERT_TAIL_LIST(&netDevice->ReceivePacketList, &packet->ListEntry);
6436873a 1380 spin_unlock_irqrestore(&netDevice->receive_packet_list_lock, flags);
fceaf24a
HJ
1381
1382 // Send a receive completion for the xfer page packet
1383 if (fSendReceiveComp)
1384 {
1385 NetVscSendReceiveCompletion(device, transactionId);
1386 }
1387
1388 PutNetDevice(device);
1389 DPRINT_EXIT(NETVSC);
1390}
1391
1392
1393
1394void
1395NetVscOnChannelCallback(
8282c400 1396 void * Context
fceaf24a
HJ
1397 )
1398{
1399 const int netPacketSize=2048;
1400 int ret=0;
1401 DEVICE_OBJECT *device=(DEVICE_OBJECT*)Context;
1402 NETVSC_DEVICE *netDevice;
1403
4d643114 1404 u32 bytesRecvd;
59471438
GKH
1405 u64 requestId;
1406 unsigned char packet[netPacketSize];
fceaf24a 1407 VMPACKET_DESCRIPTOR *desc;
59471438 1408 unsigned char *buffer=packet;
fceaf24a
HJ
1409 int bufferlen=netPacketSize;
1410
1411
1412 DPRINT_ENTER(NETVSC);
1413
1414 ASSERT(device);
1415
1416 netDevice = GetInboundNetDevice(device);
1417 if (!netDevice)
1418 {
1419 DPRINT_ERR(NETVSC, "net device (%p) shutting down...ignoring inbound packets", netDevice);
1420 DPRINT_EXIT(NETVSC);
1421 return;
1422 }
1423
1424 do
1425 {
1426 ret = device->Driver->VmbusChannelInterface.RecvPacketRaw(device,
1427 buffer,
1428 bufferlen,
1429 &bytesRecvd,
1430 &requestId);
1431
1432 if (ret == 0)
1433 {
1434 if (bytesRecvd > 0)
1435 {
1436 DPRINT_DBG(NETVSC, "receive %d bytes, tid %llx", bytesRecvd, requestId);
1437
1438 desc = (VMPACKET_DESCRIPTOR*)buffer;
1439 switch (desc->Type)
1440 {
1441 case VmbusPacketTypeCompletion:
1442 NetVscOnSendCompletion(device, desc);
1443 break;
1444
1445 case VmbusPacketTypeDataUsingTransferPages:
1446 NetVscOnReceive(device, desc);
1447 break;
1448
1449 default:
1450 DPRINT_ERR(NETVSC, "unhandled packet type %d, tid %llx len %d\n", desc->Type, requestId, bytesRecvd);
1451 break;
1452 }
1453
1454 // reset
1455 if (bufferlen > netPacketSize)
1456 {
8c69f52a 1457 kfree(buffer);
fceaf24a
HJ
1458
1459 buffer = packet;
1460 bufferlen = netPacketSize;
1461 }
1462 }
1463 else
1464 {
1465 //DPRINT_DBG(NETVSC, "nothing else to read...");
1466
1467 // reset
1468 if (bufferlen > netPacketSize)
1469 {
8c69f52a 1470 kfree(buffer);
fceaf24a
HJ
1471
1472 buffer = packet;
1473 bufferlen = netPacketSize;
1474 }
1475
1476 break;
1477 }
1478 }
1479 else if (ret == -2) // Handle large packet
1480 {
0a72f3cf 1481 buffer = kmalloc(bytesRecvd, GFP_ATOMIC);
fceaf24a
HJ
1482 if (buffer == NULL)
1483 {
1484 // Try again next time around
1485 DPRINT_ERR(NETVSC, "unable to allocate buffer of size (%d)!!", bytesRecvd);
1486 break;
1487 }
1488
1489 bufferlen = bytesRecvd;
1490 }
1491 else
1492 {
1493 ASSERT(0);
1494 }
1495 } while (1);
1496
1497 PutNetDevice(device);
1498 DPRINT_EXIT(NETVSC);
1499 return;
1500}