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