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