1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include <linux/kernel.h>
22#include <linux/mm.h>
23#include <linux/delay.h>
24#include <linux/io.h>
25#include "osd.h"
26#include "logging.h"
27#include "NetVsc.h"
28#include "RndisFilter.h"
29
30
31
32static const char *gDriverName = "netvsc";
33
34
35static const struct hv_guid gNetVscDeviceType = {
36 .data = {
37 0x63, 0x51, 0x61, 0xF8, 0x3E, 0xDF, 0xc5, 0x46,
38 0x91, 0x3F, 0xF2, 0xD2, 0xF9, 0x65, 0xED, 0x0E
39 }
40};
41
42static int NetVscOnDeviceAdd(struct hv_device *Device, void *AdditionalInfo);
43
44static int NetVscOnDeviceRemove(struct hv_device *Device);
45
46static void NetVscOnCleanup(struct hv_driver *Driver);
47
48static void NetVscOnChannelCallback(void *context);
49
50static int NetVscInitializeSendBufferWithNetVsp(struct hv_device *Device);
51
52static int NetVscInitializeReceiveBufferWithNetVsp(struct hv_device *Device);
53
54static int NetVscDestroySendBuffer(struct netvsc_device *NetDevice);
55
56static int NetVscDestroyReceiveBuffer(struct netvsc_device *NetDevice);
57
58static int NetVscConnectToVsp(struct hv_device *Device);
59
60static void NetVscOnSendCompletion(struct hv_device *Device,
61 struct vmpacket_descriptor *Packet);
62
63static int NetVscOnSend(struct hv_device *Device,
64 struct hv_netvsc_packet *Packet);
65
66static void NetVscOnReceive(struct hv_device *Device,
67 struct vmpacket_descriptor *Packet);
68
69static void NetVscOnReceiveCompletion(void *Context);
70
71static void NetVscSendReceiveCompletion(struct hv_device *Device,
72 u64 TransactionId);
73
74
75static struct netvsc_device *AllocNetDevice(struct hv_device *Device)
76{
77 struct netvsc_device *netDevice;
78
79 netDevice = kzalloc(sizeof(struct netvsc_device), GFP_KERNEL);
80 if (!netDevice)
81 return NULL;
82
83
84 atomic_cmpxchg(&netDevice->RefCount, 0, 2);
85
86 netDevice->Device = Device;
87 Device->Extension = netDevice;
88
89 return netDevice;
90}
91
92static void FreeNetDevice(struct netvsc_device *Device)
93{
94 ASSERT(atomic_read(&Device->RefCount) == 0);
95 Device->Device->Extension = NULL;
96 kfree(Device);
97}
98
99
100
101static struct netvsc_device *GetOutboundNetDevice(struct hv_device *Device)
102{
103 struct netvsc_device *netDevice;
104
105 netDevice = Device->Extension;
106 if (netDevice && atomic_read(&netDevice->RefCount) > 1)
107 atomic_inc(&netDevice->RefCount);
108 else
109 netDevice = NULL;
110
111 return netDevice;
112}
113
114
115static struct netvsc_device *GetInboundNetDevice(struct hv_device *Device)
116{
117 struct netvsc_device *netDevice;
118
119 netDevice = Device->Extension;
120 if (netDevice && atomic_read(&netDevice->RefCount))
121 atomic_inc(&netDevice->RefCount);
122 else
123 netDevice = NULL;
124
125 return netDevice;
126}
127
128static void PutNetDevice(struct hv_device *Device)
129{
130 struct netvsc_device *netDevice;
131
132 netDevice = Device->Extension;
133 ASSERT(netDevice);
134
135 atomic_dec(&netDevice->RefCount);
136}
137
138static struct netvsc_device *ReleaseOutboundNetDevice(struct hv_device *Device)
139{
140 struct netvsc_device *netDevice;
141
142 netDevice = Device->Extension;
143 if (netDevice == NULL)
144 return NULL;
145
146
147 while (atomic_cmpxchg(&netDevice->RefCount, 2, 1) != 2)
148 udelay(100);
149
150 return netDevice;
151}
152
153static struct netvsc_device *ReleaseInboundNetDevice(struct hv_device *Device)
154{
155 struct netvsc_device *netDevice;
156
157 netDevice = Device->Extension;
158 if (netDevice == NULL)
159 return NULL;
160
161
162 while (atomic_cmpxchg(&netDevice->RefCount, 1, 0) != 1)
163 udelay(100);
164
165 Device->Extension = NULL;
166 return netDevice;
167}
168
169
170
171
172int NetVscInitialize(struct hv_driver *drv)
173{
174 struct netvsc_driver *driver = (struct netvsc_driver *)drv;
175
176 DPRINT_ENTER(NETVSC);
177
178 DPRINT_DBG(NETVSC, "sizeof(struct hv_netvsc_packet)=%zd, "
179 "sizeof(struct nvsp_message)=%zd, "
180 "sizeof(struct vmtransfer_page_packet_header)=%zd",
181 sizeof(struct hv_netvsc_packet),
182 sizeof(struct nvsp_message),
183 sizeof(struct vmtransfer_page_packet_header));
184
185
186 ASSERT(driver->RingBufferSize >= (PAGE_SIZE << 1));
187
188 drv->name = gDriverName;
189 memcpy(&drv->deviceType, &gNetVscDeviceType, sizeof(struct hv_guid));
190
191
192 ASSERT(driver->OnReceiveCallback);
193 ASSERT(driver->OnLinkStatusChanged);
194
195
196 driver->Base.OnDeviceAdd = NetVscOnDeviceAdd;
197 driver->Base.OnDeviceRemove = NetVscOnDeviceRemove;
198 driver->Base.OnCleanup = NetVscOnCleanup;
199
200 driver->OnSend = NetVscOnSend;
201
202 RndisFilterInit(driver);
203
204 DPRINT_EXIT(NETVSC);
205
206 return 0;
207}
208
209static int NetVscInitializeReceiveBufferWithNetVsp(struct hv_device *Device)
210{
211 int ret = 0;
212 struct netvsc_device *netDevice;
213 struct nvsp_message *initPacket;
214
215 DPRINT_ENTER(NETVSC);
216
217 netDevice = GetOutboundNetDevice(Device);
218 if (!netDevice) {
219 DPRINT_ERR(NETVSC, "unable to get net device..."
220 "device being destroyed?");
221 DPRINT_EXIT(NETVSC);
222 return -1;
223 }
224 ASSERT(netDevice->ReceiveBufferSize > 0);
225
226 ASSERT((netDevice->ReceiveBufferSize & (PAGE_SIZE - 1)) == 0);
227
228 netDevice->ReceiveBuffer =
229 osd_PageAlloc(netDevice->ReceiveBufferSize >> PAGE_SHIFT);
230 if (!netDevice->ReceiveBuffer) {
231 DPRINT_ERR(NETVSC,
232 "unable to allocate receive buffer of size %d",
233 netDevice->ReceiveBufferSize);
234 ret = -1;
235 goto Cleanup;
236 }
237
238 ASSERT(((unsigned long)netDevice->ReceiveBuffer & (PAGE_SIZE - 1)) ==
239 0);
240
241 DPRINT_INFO(NETVSC, "Establishing receive buffer's GPADL...");
242
243
244
245
246
247
248 ret = Device->Driver->VmbusChannelInterface.EstablishGpadl(Device,
249 netDevice->ReceiveBuffer,
250 netDevice->ReceiveBufferSize,
251 &netDevice->ReceiveBufferGpadlHandle);
252 if (ret != 0) {
253 DPRINT_ERR(NETVSC,
254 "unable to establish receive buffer's gpadl");
255 goto Cleanup;
256 }
257
258
259
260
261 DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendReceiveBuffer...");
262
263 initPacket = &netDevice->ChannelInitPacket;
264
265 memset(initPacket, 0, sizeof(struct nvsp_message));
266
267 initPacket->Header.MessageType = NvspMessage1TypeSendReceiveBuffer;
268 initPacket->Messages.Version1Messages.SendReceiveBuffer.GpadlHandle = netDevice->ReceiveBufferGpadlHandle;
269 initPacket->Messages.Version1Messages.SendReceiveBuffer.Id = NETVSC_RECEIVE_BUFFER_ID;
270
271
272 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
273 initPacket,
274 sizeof(struct nvsp_message),
275 (unsigned long)initPacket,
276 VmbusPacketTypeDataInBand,
277 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
278 if (ret != 0) {
279 DPRINT_ERR(NETVSC,
280 "unable to send receive buffer's gpadl to netvsp");
281 goto Cleanup;
282 }
283
284 osd_WaitEventWait(netDevice->ChannelInitEvent);
285
286
287 if (initPacket->Messages.Version1Messages.SendReceiveBufferComplete.Status != NvspStatusSuccess) {
288 DPRINT_ERR(NETVSC, "Unable to complete receive buffer "
289 "initialzation with NetVsp - status %d",
290 initPacket->Messages.Version1Messages.SendReceiveBufferComplete.Status);
291 ret = -1;
292 goto Cleanup;
293 }
294
295
296 ASSERT(netDevice->ReceiveSectionCount == 0);
297 ASSERT(netDevice->ReceiveSections == NULL);
298
299 netDevice->ReceiveSectionCount = initPacket->Messages.Version1Messages.SendReceiveBufferComplete.NumSections;
300
301 netDevice->ReceiveSections = kmalloc(netDevice->ReceiveSectionCount * sizeof(struct nvsp_1_receive_buffer_section), GFP_KERNEL);
302 if (netDevice->ReceiveSections == NULL) {
303 ret = -1;
304 goto Cleanup;
305 }
306
307 memcpy(netDevice->ReceiveSections,
308 initPacket->Messages.Version1Messages.SendReceiveBufferComplete.Sections,
309 netDevice->ReceiveSectionCount * sizeof(struct nvsp_1_receive_buffer_section));
310
311 DPRINT_INFO(NETVSC, "Receive sections info (count %d, offset %d, "
312 "endoffset %d, suballoc size %d, num suballocs %d)",
313 netDevice->ReceiveSectionCount,
314 netDevice->ReceiveSections[0].Offset,
315 netDevice->ReceiveSections[0].EndOffset,
316 netDevice->ReceiveSections[0].SubAllocationSize,
317 netDevice->ReceiveSections[0].NumSubAllocations);
318
319
320
321
322
323 if (netDevice->ReceiveSectionCount != 1 ||
324 netDevice->ReceiveSections->Offset != 0) {
325 ret = -1;
326 goto Cleanup;
327 }
328
329 goto Exit;
330
331Cleanup:
332 NetVscDestroyReceiveBuffer(netDevice);
333
334Exit:
335 PutNetDevice(Device);
336 DPRINT_EXIT(NETVSC);
337 return ret;
338}
339
340static int NetVscInitializeSendBufferWithNetVsp(struct hv_device *Device)
341{
342 int ret = 0;
343 struct netvsc_device *netDevice;
344 struct nvsp_message *initPacket;
345
346 DPRINT_ENTER(NETVSC);
347
348 netDevice = GetOutboundNetDevice(Device);
349 if (!netDevice) {
350 DPRINT_ERR(NETVSC, "unable to get net device..."
351 "device being destroyed?");
352 DPRINT_EXIT(NETVSC);
353 return -1;
354 }
355 ASSERT(netDevice->SendBufferSize > 0);
356
357 ASSERT((netDevice->SendBufferSize & (PAGE_SIZE - 1)) == 0);
358
359 netDevice->SendBuffer =
360 osd_PageAlloc(netDevice->SendBufferSize >> PAGE_SHIFT);
361 if (!netDevice->SendBuffer) {
362 DPRINT_ERR(NETVSC, "unable to allocate send buffer of size %d",
363 netDevice->SendBufferSize);
364 ret = -1;
365 goto Cleanup;
366 }
367
368 ASSERT(((unsigned long)netDevice->SendBuffer & (PAGE_SIZE - 1)) == 0);
369
370 DPRINT_INFO(NETVSC, "Establishing send buffer's GPADL...");
371
372
373
374
375
376
377 ret = Device->Driver->VmbusChannelInterface.EstablishGpadl(Device,
378 netDevice->SendBuffer,
379 netDevice->SendBufferSize,
380 &netDevice->SendBufferGpadlHandle);
381 if (ret != 0) {
382 DPRINT_ERR(NETVSC, "unable to establish send buffer's gpadl");
383 goto Cleanup;
384 }
385
386
387
388
389 DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendSendBuffer...");
390
391 initPacket = &netDevice->ChannelInitPacket;
392
393 memset(initPacket, 0, sizeof(struct nvsp_message));
394
395 initPacket->Header.MessageType = NvspMessage1TypeSendSendBuffer;
396 initPacket->Messages.Version1Messages.SendReceiveBuffer.GpadlHandle = netDevice->SendBufferGpadlHandle;
397 initPacket->Messages.Version1Messages.SendReceiveBuffer.Id = NETVSC_SEND_BUFFER_ID;
398
399
400 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
401 initPacket, sizeof(struct nvsp_message),
402 (unsigned long)initPacket,
403 VmbusPacketTypeDataInBand,
404 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
405 if (ret != 0) {
406 DPRINT_ERR(NETVSC,
407 "unable to send receive buffer's gpadl to netvsp");
408 goto Cleanup;
409 }
410
411 osd_WaitEventWait(netDevice->ChannelInitEvent);
412
413
414 if (initPacket->Messages.Version1Messages.SendSendBufferComplete.Status != NvspStatusSuccess) {
415 DPRINT_ERR(NETVSC, "Unable to complete send buffer "
416 "initialzation with NetVsp - status %d",
417 initPacket->Messages.Version1Messages.SendSendBufferComplete.Status);
418 ret = -1;
419 goto Cleanup;
420 }
421
422 netDevice->SendSectionSize = initPacket->Messages.Version1Messages.SendSendBufferComplete.SectionSize;
423
424 goto Exit;
425
426Cleanup:
427 NetVscDestroySendBuffer(netDevice);
428
429Exit:
430 PutNetDevice(Device);
431 DPRINT_EXIT(NETVSC);
432 return ret;
433}
434
435static int NetVscDestroyReceiveBuffer(struct netvsc_device *NetDevice)
436{
437 struct nvsp_message *revokePacket;
438 int ret = 0;
439
440 DPRINT_ENTER(NETVSC);
441
442
443
444
445
446
447
448 if (NetDevice->ReceiveSectionCount) {
449 DPRINT_INFO(NETVSC,
450 "Sending NvspMessage1TypeRevokeReceiveBuffer...");
451
452
453 revokePacket = &NetDevice->RevokePacket;
454 memset(revokePacket, 0, sizeof(struct nvsp_message));
455
456 revokePacket->Header.MessageType = NvspMessage1TypeRevokeReceiveBuffer;
457 revokePacket->Messages.Version1Messages.RevokeReceiveBuffer.Id = NETVSC_RECEIVE_BUFFER_ID;
458
459 ret = NetDevice->Device->Driver->VmbusChannelInterface.SendPacket(
460 NetDevice->Device,
461 revokePacket,
462 sizeof(struct nvsp_message),
463 (unsigned long)revokePacket,
464 VmbusPacketTypeDataInBand, 0);
465
466
467
468
469 if (ret != 0) {
470 DPRINT_ERR(NETVSC, "unable to send revoke receive "
471 "buffer to netvsp");
472 DPRINT_EXIT(NETVSC);
473 return -1;
474 }
475 }
476
477
478 if (NetDevice->ReceiveBufferGpadlHandle) {
479 DPRINT_INFO(NETVSC, "Tearing down receive buffer's GPADL...");
480
481 ret = NetDevice->Device->Driver->VmbusChannelInterface.TeardownGpadl(
482 NetDevice->Device,
483 NetDevice->ReceiveBufferGpadlHandle);
484
485
486 if (ret != 0) {
487 DPRINT_ERR(NETVSC,
488 "unable to teardown receive buffer's gpadl");
489 DPRINT_EXIT(NETVSC);
490 return -1;
491 }
492 NetDevice->ReceiveBufferGpadlHandle = 0;
493 }
494
495 if (NetDevice->ReceiveBuffer) {
496 DPRINT_INFO(NETVSC, "Freeing up receive buffer...");
497
498
499 osd_PageFree(NetDevice->ReceiveBuffer,
500 NetDevice->ReceiveBufferSize >> PAGE_SHIFT);
501 NetDevice->ReceiveBuffer = NULL;
502 }
503
504 if (NetDevice->ReceiveSections) {
505 NetDevice->ReceiveSectionCount = 0;
506 kfree(NetDevice->ReceiveSections);
507 NetDevice->ReceiveSections = NULL;
508 }
509
510 DPRINT_EXIT(NETVSC);
511
512 return ret;
513}
514
515static int NetVscDestroySendBuffer(struct netvsc_device *NetDevice)
516{
517 struct nvsp_message *revokePacket;
518 int ret = 0;
519
520 DPRINT_ENTER(NETVSC);
521
522
523
524
525
526
527
528 if (NetDevice->SendSectionSize) {
529 DPRINT_INFO(NETVSC,
530 "Sending NvspMessage1TypeRevokeSendBuffer...");
531
532
533 revokePacket = &NetDevice->RevokePacket;
534 memset(revokePacket, 0, sizeof(struct nvsp_message));
535
536 revokePacket->Header.MessageType = NvspMessage1TypeRevokeSendBuffer;
537 revokePacket->Messages.Version1Messages.RevokeSendBuffer.Id = NETVSC_SEND_BUFFER_ID;
538
539 ret = NetDevice->Device->Driver->VmbusChannelInterface.SendPacket(NetDevice->Device,
540 revokePacket,
541 sizeof(struct nvsp_message),
542 (unsigned long)revokePacket,
543 VmbusPacketTypeDataInBand, 0);
544
545
546
547
548 if (ret != 0) {
549 DPRINT_ERR(NETVSC, "unable to send revoke send buffer "
550 "to netvsp");
551 DPRINT_EXIT(NETVSC);
552 return -1;
553 }
554 }
555
556
557 if (NetDevice->SendBufferGpadlHandle) {
558 DPRINT_INFO(NETVSC, "Tearing down send buffer's GPADL...");
559
560 ret = NetDevice->Device->Driver->VmbusChannelInterface.TeardownGpadl(NetDevice->Device, NetDevice->SendBufferGpadlHandle);
561
562
563
564
565
566 if (ret != 0) {
567 DPRINT_ERR(NETVSC, "unable to teardown send buffer's "
568 "gpadl");
569 DPRINT_EXIT(NETVSC);
570 return -1;
571 }
572 NetDevice->SendBufferGpadlHandle = 0;
573 }
574
575 if (NetDevice->SendBuffer) {
576 DPRINT_INFO(NETVSC, "Freeing up send buffer...");
577
578
579 osd_PageFree(NetDevice->SendBuffer,
580 NetDevice->SendBufferSize >> PAGE_SHIFT);
581 NetDevice->SendBuffer = NULL;
582 }
583
584 DPRINT_EXIT(NETVSC);
585
586 return ret;
587}
588
589
590static int NetVscConnectToVsp(struct hv_device *Device)
591{
592 int ret;
593 struct netvsc_device *netDevice;
594 struct nvsp_message *initPacket;
595 int ndisVersion;
596
597 DPRINT_ENTER(NETVSC);
598
599 netDevice = GetOutboundNetDevice(Device);
600 if (!netDevice) {
601 DPRINT_ERR(NETVSC, "unable to get net device..."
602 "device being destroyed?");
603 DPRINT_EXIT(NETVSC);
604 return -1;
605 }
606
607 initPacket = &netDevice->ChannelInitPacket;
608
609 memset(initPacket, 0, sizeof(struct nvsp_message));
610 initPacket->Header.MessageType = NvspMessageTypeInit;
611 initPacket->Messages.InitMessages.Init.MinProtocolVersion = NVSP_MIN_PROTOCOL_VERSION;
612 initPacket->Messages.InitMessages.Init.MaxProtocolVersion = NVSP_MAX_PROTOCOL_VERSION;
613
614 DPRINT_INFO(NETVSC, "Sending NvspMessageTypeInit...");
615
616
617 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
618 initPacket,
619 sizeof(struct nvsp_message),
620 (unsigned long)initPacket,
621 VmbusPacketTypeDataInBand,
622 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
623
624 if (ret != 0) {
625 DPRINT_ERR(NETVSC, "unable to send NvspMessageTypeInit");
626 goto Cleanup;
627 }
628
629 osd_WaitEventWait(netDevice->ChannelInitEvent);
630
631
632
633 DPRINT_INFO(NETVSC, "NvspMessageTypeInit status(%d) max mdl chain (%d)",
634 initPacket->Messages.InitMessages.InitComplete.Status,
635 initPacket->Messages.InitMessages.InitComplete.MaximumMdlChainLength);
636
637 if (initPacket->Messages.InitMessages.InitComplete.Status !=
638 NvspStatusSuccess) {
639 DPRINT_ERR(NETVSC,
640 "unable to initialize with netvsp (status 0x%x)",
641 initPacket->Messages.InitMessages.InitComplete.Status);
642 ret = -1;
643 goto Cleanup;
644 }
645
646 if (initPacket->Messages.InitMessages.InitComplete.NegotiatedProtocolVersion != NVSP_PROTOCOL_VERSION_1) {
647 DPRINT_ERR(NETVSC, "unable to initialize with netvsp "
648 "(version expected 1 got %d)",
649 initPacket->Messages.InitMessages.InitComplete.NegotiatedProtocolVersion);
650 ret = -1;
651 goto Cleanup;
652 }
653 DPRINT_INFO(NETVSC, "Sending NvspMessage1TypeSendNdisVersion...");
654
655
656 memset(initPacket, 0, sizeof(struct nvsp_message));
657
658 ndisVersion = 0x00050000;
659
660 initPacket->Header.MessageType = NvspMessage1TypeSendNdisVersion;
661 initPacket->Messages.Version1Messages.SendNdisVersion.NdisMajorVersion =
662 (ndisVersion & 0xFFFF0000) >> 16;
663 initPacket->Messages.Version1Messages.SendNdisVersion.NdisMinorVersion =
664 ndisVersion & 0xFFFF;
665
666
667 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
668 initPacket,
669 sizeof(struct nvsp_message),
670 (unsigned long)initPacket,
671 VmbusPacketTypeDataInBand, 0);
672 if (ret != 0) {
673 DPRINT_ERR(NETVSC,
674 "unable to send NvspMessage1TypeSendNdisVersion");
675 ret = -1;
676 goto Cleanup;
677 }
678
679
680
681
682
683
684
685
686
687 ret = NetVscInitializeReceiveBufferWithNetVsp(Device);
688 if (ret == 0)
689 ret = NetVscInitializeSendBufferWithNetVsp(Device);
690
691Cleanup:
692 PutNetDevice(Device);
693 DPRINT_EXIT(NETVSC);
694 return ret;
695}
696
697static void NetVscDisconnectFromVsp(struct netvsc_device *NetDevice)
698{
699 DPRINT_ENTER(NETVSC);
700
701 NetVscDestroyReceiveBuffer(NetDevice);
702 NetVscDestroySendBuffer(NetDevice);
703
704 DPRINT_EXIT(NETVSC);
705}
706
707
708
709
710static int NetVscOnDeviceAdd(struct hv_device *Device, void *AdditionalInfo)
711{
712 int ret = 0;
713 int i;
714 struct netvsc_device *netDevice;
715 struct hv_netvsc_packet *packet, *pos;
716 struct netvsc_driver *netDriver =
717 (struct netvsc_driver *)Device->Driver;
718
719 DPRINT_ENTER(NETVSC);
720
721 netDevice = AllocNetDevice(Device);
722 if (!netDevice) {
723 ret = -1;
724 goto Cleanup;
725 }
726
727 DPRINT_DBG(NETVSC, "netvsc channel object allocated - %p", netDevice);
728
729
730 netDevice->ReceiveBufferSize = NETVSC_RECEIVE_BUFFER_SIZE;
731 spin_lock_init(&netDevice->receive_packet_list_lock);
732
733 netDevice->SendBufferSize = NETVSC_SEND_BUFFER_SIZE;
734
735 INIT_LIST_HEAD(&netDevice->ReceivePacketList);
736
737 for (i = 0; i < NETVSC_RECEIVE_PACKETLIST_COUNT; i++) {
738 packet = kzalloc(sizeof(struct hv_netvsc_packet) +
739 (NETVSC_RECEIVE_SG_COUNT *
740 sizeof(struct hv_page_buffer)), GFP_KERNEL);
741 if (!packet) {
742 DPRINT_DBG(NETVSC, "unable to allocate netvsc pkts "
743 "for receive pool (wanted %d got %d)",
744 NETVSC_RECEIVE_PACKETLIST_COUNT, i);
745 break;
746 }
747 list_add_tail(&packet->ListEntry,
748 &netDevice->ReceivePacketList);
749 }
750 netDevice->ChannelInitEvent = osd_WaitEventCreate();
751
752
753 ret = Device->Driver->VmbusChannelInterface.Open(Device,
754 netDriver->RingBufferSize,
755 netDriver->RingBufferSize,
756 NULL, 0,
757 NetVscOnChannelCallback,
758 Device);
759
760 if (ret != 0) {
761 DPRINT_ERR(NETVSC, "unable to open channel: %d", ret);
762 ret = -1;
763 goto Cleanup;
764 }
765
766
767 DPRINT_INFO(NETVSC, "*** NetVSC channel opened successfully! ***");
768
769
770 ret = NetVscConnectToVsp(Device);
771 if (ret != 0) {
772 DPRINT_ERR(NETVSC, "unable to connect to NetVSP - %d", ret);
773 ret = -1;
774 goto Close;
775 }
776
777 DPRINT_INFO(NETVSC, "*** NetVSC channel handshake result - %d ***",
778 ret);
779
780 DPRINT_EXIT(NETVSC);
781 return ret;
782
783Close:
784
785 Device->Driver->VmbusChannelInterface.Close(Device);
786
787Cleanup:
788
789 if (netDevice) {
790 kfree(netDevice->ChannelInitEvent);
791
792 list_for_each_entry_safe(packet, pos,
793 &netDevice->ReceivePacketList,
794 ListEntry) {
795 list_del(&packet->ListEntry);
796 kfree(packet);
797 }
798
799 ReleaseOutboundNetDevice(Device);
800 ReleaseInboundNetDevice(Device);
801
802 FreeNetDevice(netDevice);
803 }
804
805 DPRINT_EXIT(NETVSC);
806 return ret;
807}
808
809
810
811
812static int NetVscOnDeviceRemove(struct hv_device *Device)
813{
814 struct netvsc_device *netDevice;
815 struct hv_netvsc_packet *netvscPacket, *pos;
816
817 DPRINT_ENTER(NETVSC);
818
819 DPRINT_INFO(NETVSC, "Disabling outbound traffic on net device (%p)...",
820 Device->Extension);
821
822
823 netDevice = ReleaseOutboundNetDevice(Device);
824 if (!netDevice) {
825 DPRINT_ERR(NETVSC, "No net device present!!");
826 return -1;
827 }
828
829
830 while (atomic_read(&netDevice->NumOutstandingSends)) {
831 DPRINT_INFO(NETVSC, "waiting for %d requests to complete...",
832 atomic_read(&netDevice->NumOutstandingSends));
833 udelay(100);
834 }
835
836 DPRINT_INFO(NETVSC, "Disconnecting from netvsp...");
837
838 NetVscDisconnectFromVsp(netDevice);
839
840 DPRINT_INFO(NETVSC, "Disabling inbound traffic on net device (%p)...",
841 Device->Extension);
842
843
844 netDevice = ReleaseInboundNetDevice(Device);
845
846
847 DPRINT_INFO(NETVSC, "net device (%p) safe to remove", netDevice);
848
849
850 Device->Driver->VmbusChannelInterface.Close(Device);
851
852
853 list_for_each_entry_safe(netvscPacket, pos,
854 &netDevice->ReceivePacketList, ListEntry) {
855 list_del(&netvscPacket->ListEntry);
856 kfree(netvscPacket);
857 }
858
859 kfree(netDevice->ChannelInitEvent);
860 FreeNetDevice(netDevice);
861
862 DPRINT_EXIT(NETVSC);
863 return 0;
864}
865
866
867
868
869static void NetVscOnCleanup(struct hv_driver *drv)
870{
871 DPRINT_ENTER(NETVSC);
872 DPRINT_EXIT(NETVSC);
873}
874
875static void NetVscOnSendCompletion(struct hv_device *Device,
876 struct vmpacket_descriptor *Packet)
877{
878 struct netvsc_device *netDevice;
879 struct nvsp_message *nvspPacket;
880 struct hv_netvsc_packet *nvscPacket;
881
882 DPRINT_ENTER(NETVSC);
883
884 netDevice = GetInboundNetDevice(Device);
885 if (!netDevice) {
886 DPRINT_ERR(NETVSC, "unable to get net device..."
887 "device being destroyed?");
888 DPRINT_EXIT(NETVSC);
889 return;
890 }
891
892 nvspPacket = (struct nvsp_message *)((unsigned long)Packet + (Packet->DataOffset8 << 3));
893
894 DPRINT_DBG(NETVSC, "send completion packet - type %d",
895 nvspPacket->Header.MessageType);
896
897 if ((nvspPacket->Header.MessageType == NvspMessageTypeInitComplete) ||
898 (nvspPacket->Header.MessageType ==
899 NvspMessage1TypeSendReceiveBufferComplete) ||
900 (nvspPacket->Header.MessageType ==
901 NvspMessage1TypeSendSendBufferComplete)) {
902
903 memcpy(&netDevice->ChannelInitPacket, nvspPacket,
904 sizeof(struct nvsp_message));
905 osd_WaitEventSet(netDevice->ChannelInitEvent);
906 } else if (nvspPacket->Header.MessageType ==
907 NvspMessage1TypeSendRNDISPacketComplete) {
908
909 nvscPacket = (struct hv_netvsc_packet *)(unsigned long)Packet->TransactionId;
910 ASSERT(nvscPacket);
911
912
913 nvscPacket->Completion.Send.OnSendCompletion(nvscPacket->Completion.Send.SendCompletionContext);
914
915 atomic_dec(&netDevice->NumOutstandingSends);
916 } else {
917 DPRINT_ERR(NETVSC, "Unknown send completion packet type - "
918 "%d received!!", nvspPacket->Header.MessageType);
919 }
920
921 PutNetDevice(Device);
922 DPRINT_EXIT(NETVSC);
923}
924
925static int NetVscOnSend(struct hv_device *Device,
926 struct hv_netvsc_packet *Packet)
927{
928 struct netvsc_device *netDevice;
929 int ret = 0;
930
931 struct nvsp_message sendMessage;
932
933 DPRINT_ENTER(NETVSC);
934
935 netDevice = GetOutboundNetDevice(Device);
936 if (!netDevice) {
937 DPRINT_ERR(NETVSC, "net device (%p) shutting down..."
938 "ignoring outbound packets", netDevice);
939 DPRINT_EXIT(NETVSC);
940 return -2;
941 }
942
943 sendMessage.Header.MessageType = NvspMessage1TypeSendRNDISPacket;
944 if (Packet->IsDataPacket) {
945
946 sendMessage.Messages.Version1Messages.SendRNDISPacket.ChannelType = 0;
947 } else {
948
949 sendMessage.Messages.Version1Messages.SendRNDISPacket.ChannelType = 1;
950 }
951
952
953 sendMessage.Messages.Version1Messages.SendRNDISPacket.SendBufferSectionIndex = 0xFFFFFFFF;
954 sendMessage.Messages.Version1Messages.SendRNDISPacket.SendBufferSectionSize = 0;
955
956 if (Packet->PageBufferCount) {
957 ret = Device->Driver->VmbusChannelInterface.SendPacketPageBuffer(
958 Device, Packet->PageBuffers,
959 Packet->PageBufferCount,
960 &sendMessage,
961 sizeof(struct nvsp_message),
962 (unsigned long)Packet);
963 } else {
964 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
965 &sendMessage,
966 sizeof(struct nvsp_message),
967 (unsigned long)Packet,
968 VmbusPacketTypeDataInBand,
969 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
970
971 }
972
973 if (ret != 0)
974 DPRINT_ERR(NETVSC, "Unable to send packet %p ret %d",
975 Packet, ret);
976
977 atomic_inc(&netDevice->NumOutstandingSends);
978 PutNetDevice(Device);
979
980 DPRINT_EXIT(NETVSC);
981 return ret;
982}
983
984static void NetVscOnReceive(struct hv_device *Device,
985 struct vmpacket_descriptor *Packet)
986{
987 struct netvsc_device *netDevice;
988 struct vmtransfer_page_packet_header *vmxferpagePacket;
989 struct nvsp_message *nvspPacket;
990 struct hv_netvsc_packet *netvscPacket = NULL;
991 unsigned long start;
992 unsigned long end, endVirtual;
993
994 struct xferpage_packet *xferpagePacket = NULL;
995 int i, j;
996 int count = 0, bytesRemain = 0;
997 unsigned long flags;
998 LIST_HEAD(listHead);
999
1000 DPRINT_ENTER(NETVSC);
1001
1002 netDevice = GetInboundNetDevice(Device);
1003 if (!netDevice) {
1004 DPRINT_ERR(NETVSC, "unable to get net device..."
1005 "device being destroyed?");
1006 DPRINT_EXIT(NETVSC);
1007 return;
1008 }
1009
1010
1011
1012
1013
1014 if (Packet->Type != VmbusPacketTypeDataUsingTransferPages) {
1015 DPRINT_ERR(NETVSC, "Unknown packet type received - %d",
1016 Packet->Type);
1017 PutNetDevice(Device);
1018 return;
1019 }
1020
1021 nvspPacket = (struct nvsp_message *)((unsigned long)Packet +
1022 (Packet->DataOffset8 << 3));
1023
1024
1025 if (nvspPacket->Header.MessageType != NvspMessage1TypeSendRNDISPacket) {
1026 DPRINT_ERR(NETVSC, "Unknown nvsp packet type received - %d",
1027 nvspPacket->Header.MessageType);
1028 PutNetDevice(Device);
1029 return;
1030 }
1031
1032 DPRINT_DBG(NETVSC, "NVSP packet received - type %d",
1033 nvspPacket->Header.MessageType);
1034
1035 vmxferpagePacket = (struct vmtransfer_page_packet_header *)Packet;
1036
1037 if (vmxferpagePacket->TransferPageSetId != NETVSC_RECEIVE_BUFFER_ID) {
1038 DPRINT_ERR(NETVSC, "Invalid xfer page set id - "
1039 "expecting %x got %x", NETVSC_RECEIVE_BUFFER_ID,
1040 vmxferpagePacket->TransferPageSetId);
1041 PutNetDevice(Device);
1042 return;
1043 }
1044
1045 DPRINT_DBG(NETVSC, "xfer page - range count %d",
1046 vmxferpagePacket->RangeCount);
1047
1048
1049
1050
1051
1052
1053
1054 spin_lock_irqsave(&netDevice->receive_packet_list_lock, flags);
1055 while (!list_empty(&netDevice->ReceivePacketList)) {
1056 list_move_tail(netDevice->ReceivePacketList.next, &listHead);
1057 if (++count == vmxferpagePacket->RangeCount + 1)
1058 break;
1059 }
1060 spin_unlock_irqrestore(&netDevice->receive_packet_list_lock, flags);
1061
1062
1063
1064
1065
1066
1067 if (count < 2) {
1068 DPRINT_ERR(NETVSC, "Got only %d netvsc pkt...needed %d pkts. "
1069 "Dropping this xfer page packet completely!",
1070 count, vmxferpagePacket->RangeCount + 1);
1071
1072
1073 spin_lock_irqsave(&netDevice->receive_packet_list_lock, flags);
1074 for (i = count; i != 0; i--) {
1075 list_move_tail(listHead.next,
1076 &netDevice->ReceivePacketList);
1077 }
1078 spin_unlock_irqrestore(&netDevice->receive_packet_list_lock,
1079 flags);
1080
1081 NetVscSendReceiveCompletion(Device,
1082 vmxferpagePacket->d.TransactionId);
1083
1084 PutNetDevice(Device);
1085 return;
1086 }
1087
1088
1089 xferpagePacket = (struct xferpage_packet*)listHead.next;
1090 list_del(&xferpagePacket->ListEntry);
1091
1092
1093 xferpagePacket->Count = count - 1;
1094 ASSERT(xferpagePacket->Count > 0 && xferpagePacket->Count <=
1095 vmxferpagePacket->RangeCount);
1096
1097 if (xferpagePacket->Count != vmxferpagePacket->RangeCount) {
1098 DPRINT_INFO(NETVSC, "Needed %d netvsc pkts to satisy this xfer "
1099 "page...got %d", vmxferpagePacket->RangeCount,
1100 xferpagePacket->Count);
1101 }
1102
1103
1104 for (i = 0; i < (count - 1); i++) {
1105 netvscPacket = (struct hv_netvsc_packet*)listHead.next;
1106 list_del(&netvscPacket->ListEntry);
1107
1108
1109 netvscPacket->XferPagePacket = xferpagePacket;
1110 netvscPacket->Completion.Recv.OnReceiveCompletion =
1111 NetVscOnReceiveCompletion;
1112 netvscPacket->Completion.Recv.ReceiveCompletionContext =
1113 netvscPacket;
1114 netvscPacket->Device = Device;
1115
1116 netvscPacket->Completion.Recv.ReceiveCompletionTid =
1117 vmxferpagePacket->d.TransactionId;
1118
1119 netvscPacket->TotalDataBufferLength =
1120 vmxferpagePacket->Ranges[i].ByteCount;
1121 netvscPacket->PageBufferCount = 1;
1122
1123 ASSERT(vmxferpagePacket->Ranges[i].ByteOffset +
1124 vmxferpagePacket->Ranges[i].ByteCount <
1125 netDevice->ReceiveBufferSize);
1126
1127 netvscPacket->PageBuffers[0].Length =
1128 vmxferpagePacket->Ranges[i].ByteCount;
1129
1130 start = virt_to_phys((void *)((unsigned long)netDevice->ReceiveBuffer + vmxferpagePacket->Ranges[i].ByteOffset));
1131
1132 netvscPacket->PageBuffers[0].Pfn = start >> PAGE_SHIFT;
1133 endVirtual = (unsigned long)netDevice->ReceiveBuffer
1134 + vmxferpagePacket->Ranges[i].ByteOffset
1135 + vmxferpagePacket->Ranges[i].ByteCount - 1;
1136 end = virt_to_phys((void *)endVirtual);
1137
1138
1139 netvscPacket->PageBuffers[0].Offset =
1140 vmxferpagePacket->Ranges[i].ByteOffset & (PAGE_SIZE - 1);
1141 if ((end >> PAGE_SHIFT) != (start >> PAGE_SHIFT)) {
1142
1143 netvscPacket->PageBuffers[0].Length =
1144 (netvscPacket->PageBuffers[0].Pfn << PAGE_SHIFT)
1145 + PAGE_SIZE - start;
1146 bytesRemain = netvscPacket->TotalDataBufferLength -
1147 netvscPacket->PageBuffers[0].Length;
1148 for (j = 1; j < NETVSC_PACKET_MAXPAGE; j++) {
1149 netvscPacket->PageBuffers[j].Offset = 0;
1150 if (bytesRemain <= PAGE_SIZE) {
1151 netvscPacket->PageBuffers[j].Length = bytesRemain;
1152 bytesRemain = 0;
1153 } else {
1154 netvscPacket->PageBuffers[j].Length = PAGE_SIZE;
1155 bytesRemain -= PAGE_SIZE;
1156 }
1157 netvscPacket->PageBuffers[j].Pfn =
1158 virt_to_phys((void *)(endVirtual - bytesRemain)) >> PAGE_SHIFT;
1159 netvscPacket->PageBufferCount++;
1160 if (bytesRemain == 0)
1161 break;
1162 }
1163 ASSERT(bytesRemain == 0);
1164 }
1165 DPRINT_DBG(NETVSC, "[%d] - (abs offset %u len %u) => "
1166 "(pfn %llx, offset %u, len %u)", i,
1167 vmxferpagePacket->Ranges[i].ByteOffset,
1168 vmxferpagePacket->Ranges[i].ByteCount,
1169 netvscPacket->PageBuffers[0].Pfn,
1170 netvscPacket->PageBuffers[0].Offset,
1171 netvscPacket->PageBuffers[0].Length);
1172
1173
1174 ((struct netvsc_driver *)Device->Driver)->OnReceiveCallback(Device, netvscPacket);
1175
1176 NetVscOnReceiveCompletion(netvscPacket->Completion.Recv.ReceiveCompletionContext);
1177 }
1178
1179 ASSERT(list_empty(&listHead));
1180
1181 PutNetDevice(Device);
1182 DPRINT_EXIT(NETVSC);
1183}
1184
1185static void NetVscSendReceiveCompletion(struct hv_device *Device,
1186 u64 TransactionId)
1187{
1188 struct nvsp_message recvcompMessage;
1189 int retries = 0;
1190 int ret;
1191
1192 DPRINT_DBG(NETVSC, "Sending receive completion pkt - %llx",
1193 TransactionId);
1194
1195 recvcompMessage.Header.MessageType =
1196 NvspMessage1TypeSendRNDISPacketComplete;
1197
1198
1199 recvcompMessage.Messages.Version1Messages.SendRNDISPacketComplete.Status = NvspStatusSuccess;
1200
1201retry_send_cmplt:
1202
1203 ret = Device->Driver->VmbusChannelInterface.SendPacket(Device,
1204 &recvcompMessage,
1205 sizeof(struct nvsp_message),
1206 TransactionId,
1207 VmbusPacketTypeCompletion, 0);
1208 if (ret == 0) {
1209
1210
1211 } else if (ret == -1) {
1212
1213 retries++;
1214 DPRINT_ERR(NETVSC, "unable to send receive completion pkt "
1215 "(tid %llx)...retrying %d", TransactionId, retries);
1216
1217 if (retries < 4) {
1218 udelay(100);
1219 goto retry_send_cmplt;
1220 } else {
1221 DPRINT_ERR(NETVSC, "unable to send receive completion "
1222 "pkt (tid %llx)...give up retrying",
1223 TransactionId);
1224 }
1225 } else {
1226 DPRINT_ERR(NETVSC, "unable to send receive completion pkt - "
1227 "%llx", TransactionId);
1228 }
1229}
1230
1231
1232static void NetVscOnReceiveCompletion(void *Context)
1233{
1234 struct hv_netvsc_packet *packet = Context;
1235 struct hv_device *device = (struct hv_device *)packet->Device;
1236 struct netvsc_device *netDevice;
1237 u64 transactionId = 0;
1238 bool fSendReceiveComp = false;
1239 unsigned long flags;
1240
1241 DPRINT_ENTER(NETVSC);
1242
1243 ASSERT(packet->XferPagePacket);
1244
1245
1246
1247
1248
1249
1250 netDevice = GetInboundNetDevice(device);
1251 if (!netDevice) {
1252 DPRINT_ERR(NETVSC, "unable to get net device..."
1253 "device being destroyed?");
1254 DPRINT_EXIT(NETVSC);
1255 return;
1256 }
1257
1258
1259 spin_lock_irqsave(&netDevice->receive_packet_list_lock, flags);
1260
1261 ASSERT(packet->XferPagePacket->Count > 0);
1262 packet->XferPagePacket->Count--;
1263
1264
1265
1266
1267
1268 if (packet->XferPagePacket->Count == 0) {
1269 fSendReceiveComp = true;
1270 transactionId = packet->Completion.Recv.ReceiveCompletionTid;
1271 list_add_tail(&packet->XferPagePacket->ListEntry,
1272 &netDevice->ReceivePacketList);
1273
1274 }
1275
1276
1277 list_add_tail(&packet->ListEntry, &netDevice->ReceivePacketList);
1278 spin_unlock_irqrestore(&netDevice->receive_packet_list_lock, flags);
1279
1280
1281 if (fSendReceiveComp)
1282 NetVscSendReceiveCompletion(device, transactionId);
1283
1284 PutNetDevice(device);
1285 DPRINT_EXIT(NETVSC);
1286}
1287
1288void NetVscOnChannelCallback(void *Context)
1289{
1290 const int netPacketSize = 2048;
1291 int ret;
1292 struct hv_device *device = Context;
1293 struct netvsc_device *netDevice;
1294 u32 bytesRecvd;
1295 u64 requestId;
1296 unsigned char packet[netPacketSize];
1297 struct vmpacket_descriptor *desc;
1298 unsigned char *buffer = packet;
1299 int bufferlen = netPacketSize;
1300
1301
1302 DPRINT_ENTER(NETVSC);
1303
1304 ASSERT(device);
1305
1306 netDevice = GetInboundNetDevice(device);
1307 if (!netDevice) {
1308 DPRINT_ERR(NETVSC, "net device (%p) shutting down..."
1309 "ignoring inbound packets", netDevice);
1310 DPRINT_EXIT(NETVSC);
1311 return;
1312 }
1313
1314 do {
1315 ret = device->Driver->VmbusChannelInterface.RecvPacketRaw(
1316 device, buffer, bufferlen,
1317 &bytesRecvd, &requestId);
1318 if (ret == 0) {
1319 if (bytesRecvd > 0) {
1320 DPRINT_DBG(NETVSC, "receive %d bytes, tid %llx",
1321 bytesRecvd, requestId);
1322
1323 desc = (struct vmpacket_descriptor *)buffer;
1324 switch (desc->Type) {
1325 case VmbusPacketTypeCompletion:
1326 NetVscOnSendCompletion(device, desc);
1327 break;
1328
1329 case VmbusPacketTypeDataUsingTransferPages:
1330 NetVscOnReceive(device, desc);
1331 break;
1332
1333 default:
1334 DPRINT_ERR(NETVSC,
1335 "unhandled packet type %d, "
1336 "tid %llx len %d\n",
1337 desc->Type, requestId,
1338 bytesRecvd);
1339 break;
1340 }
1341
1342
1343 if (bufferlen > netPacketSize) {
1344 kfree(buffer);
1345 buffer = packet;
1346 bufferlen = netPacketSize;
1347 }
1348 } else {
1349
1350 if (bufferlen > netPacketSize) {
1351 kfree(buffer);
1352 buffer = packet;
1353 bufferlen = netPacketSize;
1354 }
1355
1356 break;
1357 }
1358 } else if (ret == -2) {
1359
1360 buffer = kmalloc(bytesRecvd, GFP_ATOMIC);
1361 if (buffer == NULL) {
1362
1363 DPRINT_ERR(NETVSC,
1364 "unable to allocate buffer of size "
1365 "(%d)!!", bytesRecvd);
1366 break;
1367 }
1368
1369 bufferlen = bytesRecvd;
1370 } else {
1371 ASSERT(0);
1372 }
1373 } while (1);
1374
1375 PutNetDevice(device);
1376 DPRINT_EXIT(NETVSC);
1377 return;
1378}
1379