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