hv_netvsc: style cleanups
[cascardo/linux.git] / drivers / net / hyperv / 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, see <http://www.gnu.org/licenses/>.
15  *
16  * Authors:
17  *   Haiyang Zhang <haiyangz@microsoft.com>
18  *   Hank Janssen  <hjanssen@microsoft.com>
19  */
20 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21
22 #include <linux/kernel.h>
23 #include <linux/sched.h>
24 #include <linux/wait.h>
25 #include <linux/mm.h>
26 #include <linux/delay.h>
27 #include <linux/io.h>
28 #include <linux/slab.h>
29 #include <linux/netdevice.h>
30 #include <linux/if_ether.h>
31 #include <linux/vmalloc.h>
32 #include <asm/sync_bitops.h>
33
34 #include "hyperv_net.h"
35
36 /*
37  * Switch the data path from the synthetic interface to the VF
38  * interface.
39  */
40 void netvsc_switch_datapath(struct net_device *ndev, bool vf)
41 {
42         struct net_device_context *net_device_ctx = netdev_priv(ndev);
43         struct hv_device *dev = net_device_ctx->device_ctx;
44         struct netvsc_device *nv_dev = net_device_ctx->nvdev;
45         struct nvsp_message *init_pkt = &nv_dev->channel_init_pkt;
46
47         memset(init_pkt, 0, sizeof(struct nvsp_message));
48         init_pkt->hdr.msg_type = NVSP_MSG4_TYPE_SWITCH_DATA_PATH;
49         if (vf)
50                 init_pkt->msg.v4_msg.active_dp.active_datapath =
51                         NVSP_DATAPATH_VF;
52         else
53                 init_pkt->msg.v4_msg.active_dp.active_datapath =
54                         NVSP_DATAPATH_SYNTHETIC;
55
56         vmbus_sendpacket(dev->channel, init_pkt,
57                                sizeof(struct nvsp_message),
58                                (unsigned long)init_pkt,
59                                VM_PKT_DATA_INBAND, 0);
60 }
61
62 static struct netvsc_device *alloc_net_device(void)
63 {
64         struct netvsc_device *net_device;
65
66         net_device = kzalloc(sizeof(struct netvsc_device), GFP_KERNEL);
67         if (!net_device)
68                 return NULL;
69
70         net_device->cb_buffer = kzalloc(NETVSC_PACKET_SIZE, GFP_KERNEL);
71         if (!net_device->cb_buffer) {
72                 kfree(net_device);
73                 return NULL;
74         }
75
76         net_device->mrc[0].buf = vzalloc(NETVSC_RECVSLOT_MAX *
77                                          sizeof(struct recv_comp_data));
78
79         init_waitqueue_head(&net_device->wait_drain);
80         net_device->destroy = false;
81         atomic_set(&net_device->open_cnt, 0);
82         net_device->max_pkt = RNDIS_MAX_PKT_DEFAULT;
83         net_device->pkt_align = RNDIS_PKT_ALIGN_DEFAULT;
84
85         return net_device;
86 }
87
88 static void free_netvsc_device(struct netvsc_device *nvdev)
89 {
90         int i;
91
92         for (i = 0; i < VRSS_CHANNEL_MAX; i++)
93                 vfree(nvdev->mrc[i].buf);
94
95         kfree(nvdev->cb_buffer);
96         kfree(nvdev);
97 }
98
99 static struct netvsc_device *get_outbound_net_device(struct hv_device *device)
100 {
101         struct netvsc_device *net_device = hv_device_to_netvsc_device(device);
102
103         if (net_device && net_device->destroy)
104                 net_device = NULL;
105
106         return net_device;
107 }
108
109 static struct netvsc_device *get_inbound_net_device(struct hv_device *device)
110 {
111         struct netvsc_device *net_device = hv_device_to_netvsc_device(device);
112
113         if (!net_device)
114                 goto get_in_err;
115
116         if (net_device->destroy &&
117             atomic_read(&net_device->num_outstanding_sends) == 0 &&
118             atomic_read(&net_device->num_outstanding_recvs) == 0)
119                 net_device = NULL;
120
121 get_in_err:
122         return net_device;
123 }
124
125 static int netvsc_destroy_buf(struct hv_device *device)
126 {
127         struct nvsp_message *revoke_packet;
128         int ret = 0;
129         struct net_device *ndev = hv_get_drvdata(device);
130         struct netvsc_device *net_device = net_device_to_netvsc_device(ndev);
131
132         /*
133          * If we got a section count, it means we received a
134          * SendReceiveBufferComplete msg (ie sent
135          * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
136          * to send a revoke msg here
137          */
138         if (net_device->recv_section_cnt) {
139                 /* Send the revoke receive buffer */
140                 revoke_packet = &net_device->revoke_packet;
141                 memset(revoke_packet, 0, sizeof(struct nvsp_message));
142
143                 revoke_packet->hdr.msg_type =
144                         NVSP_MSG1_TYPE_REVOKE_RECV_BUF;
145                 revoke_packet->msg.v1_msg.
146                 revoke_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
147
148                 ret = vmbus_sendpacket(device->channel,
149                                        revoke_packet,
150                                        sizeof(struct nvsp_message),
151                                        (unsigned long)revoke_packet,
152                                        VM_PKT_DATA_INBAND, 0);
153                 /*
154                  * If we failed here, we might as well return and
155                  * have a leak rather than continue and a bugchk
156                  */
157                 if (ret != 0) {
158                         netdev_err(ndev, "unable to send "
159                                 "revoke receive buffer to netvsp\n");
160                         return ret;
161                 }
162         }
163
164         /* Teardown the gpadl on the vsp end */
165         if (net_device->recv_buf_gpadl_handle) {
166                 ret = vmbus_teardown_gpadl(device->channel,
167                                            net_device->recv_buf_gpadl_handle);
168
169                 /* If we failed here, we might as well return and have a leak
170                  * rather than continue and a bugchk
171                  */
172                 if (ret != 0) {
173                         netdev_err(ndev,
174                                    "unable to teardown receive buffer's gpadl\n");
175                         return ret;
176                 }
177                 net_device->recv_buf_gpadl_handle = 0;
178         }
179
180         if (net_device->recv_buf) {
181                 /* Free up the receive buffer */
182                 vfree(net_device->recv_buf);
183                 net_device->recv_buf = NULL;
184         }
185
186         if (net_device->recv_section) {
187                 net_device->recv_section_cnt = 0;
188                 kfree(net_device->recv_section);
189                 net_device->recv_section = NULL;
190         }
191
192         /* Deal with the send buffer we may have setup.
193          * If we got a  send section size, it means we received a
194          * NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE msg (ie sent
195          * NVSP_MSG1_TYPE_SEND_SEND_BUF msg) therefore, we need
196          * to send a revoke msg here
197          */
198         if (net_device->send_section_size) {
199                 /* Send the revoke receive buffer */
200                 revoke_packet = &net_device->revoke_packet;
201                 memset(revoke_packet, 0, sizeof(struct nvsp_message));
202
203                 revoke_packet->hdr.msg_type =
204                         NVSP_MSG1_TYPE_REVOKE_SEND_BUF;
205                 revoke_packet->msg.v1_msg.revoke_send_buf.id =
206                         NETVSC_SEND_BUFFER_ID;
207
208                 ret = vmbus_sendpacket(device->channel,
209                                        revoke_packet,
210                                        sizeof(struct nvsp_message),
211                                        (unsigned long)revoke_packet,
212                                        VM_PKT_DATA_INBAND, 0);
213                 /* If we failed here, we might as well return and
214                  * have a leak rather than continue and a bugchk
215                  */
216                 if (ret != 0) {
217                         netdev_err(ndev, "unable to send "
218                                    "revoke send buffer to netvsp\n");
219                         return ret;
220                 }
221         }
222         /* Teardown the gpadl on the vsp end */
223         if (net_device->send_buf_gpadl_handle) {
224                 ret = vmbus_teardown_gpadl(device->channel,
225                                            net_device->send_buf_gpadl_handle);
226
227                 /* If we failed here, we might as well return and have a leak
228                  * rather than continue and a bugchk
229                  */
230                 if (ret != 0) {
231                         netdev_err(ndev,
232                                    "unable to teardown send buffer's gpadl\n");
233                         return ret;
234                 }
235                 net_device->send_buf_gpadl_handle = 0;
236         }
237         if (net_device->send_buf) {
238                 /* Free up the send buffer */
239                 vfree(net_device->send_buf);
240                 net_device->send_buf = NULL;
241         }
242         kfree(net_device->send_section_map);
243
244         return ret;
245 }
246
247 static int netvsc_init_buf(struct hv_device *device)
248 {
249         int ret = 0;
250         struct netvsc_device *net_device;
251         struct nvsp_message *init_packet;
252         struct net_device *ndev;
253         int node;
254
255         net_device = get_outbound_net_device(device);
256         if (!net_device)
257                 return -ENODEV;
258         ndev = hv_get_drvdata(device);
259
260         node = cpu_to_node(device->channel->target_cpu);
261         net_device->recv_buf = vzalloc_node(net_device->recv_buf_size, node);
262         if (!net_device->recv_buf)
263                 net_device->recv_buf = vzalloc(net_device->recv_buf_size);
264
265         if (!net_device->recv_buf) {
266                 netdev_err(ndev, "unable to allocate receive "
267                         "buffer of size %d\n", net_device->recv_buf_size);
268                 ret = -ENOMEM;
269                 goto cleanup;
270         }
271
272         /*
273          * Establish the gpadl handle for this buffer on this
274          * channel.  Note: This call uses the vmbus connection rather
275          * than the channel to establish the gpadl handle.
276          */
277         ret = vmbus_establish_gpadl(device->channel, net_device->recv_buf,
278                                     net_device->recv_buf_size,
279                                     &net_device->recv_buf_gpadl_handle);
280         if (ret != 0) {
281                 netdev_err(ndev,
282                         "unable to establish receive buffer's gpadl\n");
283                 goto cleanup;
284         }
285
286         /* Notify the NetVsp of the gpadl handle */
287         init_packet = &net_device->channel_init_pkt;
288
289         memset(init_packet, 0, sizeof(struct nvsp_message));
290
291         init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_RECV_BUF;
292         init_packet->msg.v1_msg.send_recv_buf.
293                 gpadl_handle = net_device->recv_buf_gpadl_handle;
294         init_packet->msg.v1_msg.
295                 send_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
296
297         /* Send the gpadl notification request */
298         ret = vmbus_sendpacket(device->channel, init_packet,
299                                sizeof(struct nvsp_message),
300                                (unsigned long)init_packet,
301                                VM_PKT_DATA_INBAND,
302                                VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
303         if (ret != 0) {
304                 netdev_err(ndev,
305                         "unable to send receive buffer's gpadl to netvsp\n");
306                 goto cleanup;
307         }
308
309         wait_for_completion(&net_device->channel_init_wait);
310
311         /* Check the response */
312         if (init_packet->msg.v1_msg.
313             send_recv_buf_complete.status != NVSP_STAT_SUCCESS) {
314                 netdev_err(ndev, "Unable to complete receive buffer "
315                            "initialization with NetVsp - status %d\n",
316                            init_packet->msg.v1_msg.
317                            send_recv_buf_complete.status);
318                 ret = -EINVAL;
319                 goto cleanup;
320         }
321
322         /* Parse the response */
323
324         net_device->recv_section_cnt = init_packet->msg.
325                 v1_msg.send_recv_buf_complete.num_sections;
326
327         net_device->recv_section = kmemdup(
328                 init_packet->msg.v1_msg.send_recv_buf_complete.sections,
329                 net_device->recv_section_cnt *
330                 sizeof(struct nvsp_1_receive_buffer_section),
331                 GFP_KERNEL);
332         if (net_device->recv_section == NULL) {
333                 ret = -EINVAL;
334                 goto cleanup;
335         }
336
337         /*
338          * For 1st release, there should only be 1 section that represents the
339          * entire receive buffer
340          */
341         if (net_device->recv_section_cnt != 1 ||
342             net_device->recv_section->offset != 0) {
343                 ret = -EINVAL;
344                 goto cleanup;
345         }
346
347         /* Now setup the send buffer.
348          */
349         net_device->send_buf = vzalloc_node(net_device->send_buf_size, node);
350         if (!net_device->send_buf)
351                 net_device->send_buf = vzalloc(net_device->send_buf_size);
352         if (!net_device->send_buf) {
353                 netdev_err(ndev, "unable to allocate send "
354                            "buffer of size %d\n", net_device->send_buf_size);
355                 ret = -ENOMEM;
356                 goto cleanup;
357         }
358
359         /* Establish the gpadl handle for this buffer on this
360          * channel.  Note: This call uses the vmbus connection rather
361          * than the channel to establish the gpadl handle.
362          */
363         ret = vmbus_establish_gpadl(device->channel, net_device->send_buf,
364                                     net_device->send_buf_size,
365                                     &net_device->send_buf_gpadl_handle);
366         if (ret != 0) {
367                 netdev_err(ndev,
368                            "unable to establish send buffer's gpadl\n");
369                 goto cleanup;
370         }
371
372         /* Notify the NetVsp of the gpadl handle */
373         init_packet = &net_device->channel_init_pkt;
374         memset(init_packet, 0, sizeof(struct nvsp_message));
375         init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_SEND_BUF;
376         init_packet->msg.v1_msg.send_send_buf.gpadl_handle =
377                 net_device->send_buf_gpadl_handle;
378         init_packet->msg.v1_msg.send_send_buf.id = NETVSC_SEND_BUFFER_ID;
379
380         /* Send the gpadl notification request */
381         ret = vmbus_sendpacket(device->channel, init_packet,
382                                sizeof(struct nvsp_message),
383                                (unsigned long)init_packet,
384                                VM_PKT_DATA_INBAND,
385                                VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
386         if (ret != 0) {
387                 netdev_err(ndev,
388                            "unable to send send buffer's gpadl to netvsp\n");
389                 goto cleanup;
390         }
391
392         wait_for_completion(&net_device->channel_init_wait);
393
394         /* Check the response */
395         if (init_packet->msg.v1_msg.
396             send_send_buf_complete.status != NVSP_STAT_SUCCESS) {
397                 netdev_err(ndev, "Unable to complete send buffer "
398                            "initialization with NetVsp - status %d\n",
399                            init_packet->msg.v1_msg.
400                            send_send_buf_complete.status);
401                 ret = -EINVAL;
402                 goto cleanup;
403         }
404
405         /* Parse the response */
406         net_device->send_section_size = init_packet->msg.
407                                 v1_msg.send_send_buf_complete.section_size;
408
409         /* Section count is simply the size divided by the section size.
410          */
411         net_device->send_section_cnt =
412                 net_device->send_buf_size / net_device->send_section_size;
413
414         dev_info(&device->device, "Send section size: %d, Section count:%d\n",
415                  net_device->send_section_size, net_device->send_section_cnt);
416
417         /* Setup state for managing the send buffer. */
418         net_device->map_words = DIV_ROUND_UP(net_device->send_section_cnt,
419                                              BITS_PER_LONG);
420
421         net_device->send_section_map = kcalloc(net_device->map_words,
422                                                sizeof(ulong), GFP_KERNEL);
423         if (net_device->send_section_map == NULL) {
424                 ret = -ENOMEM;
425                 goto cleanup;
426         }
427
428         goto exit;
429
430 cleanup:
431         netvsc_destroy_buf(device);
432
433 exit:
434         return ret;
435 }
436
437 /* Negotiate NVSP protocol version */
438 static int negotiate_nvsp_ver(struct hv_device *device,
439                               struct netvsc_device *net_device,
440                               struct nvsp_message *init_packet,
441                               u32 nvsp_ver)
442 {
443         struct net_device *ndev = hv_get_drvdata(device);
444         int ret;
445
446         memset(init_packet, 0, sizeof(struct nvsp_message));
447         init_packet->hdr.msg_type = NVSP_MSG_TYPE_INIT;
448         init_packet->msg.init_msg.init.min_protocol_ver = nvsp_ver;
449         init_packet->msg.init_msg.init.max_protocol_ver = nvsp_ver;
450
451         /* Send the init request */
452         ret = vmbus_sendpacket(device->channel, init_packet,
453                                sizeof(struct nvsp_message),
454                                (unsigned long)init_packet,
455                                VM_PKT_DATA_INBAND,
456                                VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
457
458         if (ret != 0)
459                 return ret;
460
461         wait_for_completion(&net_device->channel_init_wait);
462
463         if (init_packet->msg.init_msg.init_complete.status !=
464             NVSP_STAT_SUCCESS)
465                 return -EINVAL;
466
467         if (nvsp_ver == NVSP_PROTOCOL_VERSION_1)
468                 return 0;
469
470         /* NVSPv2 or later: Send NDIS config */
471         memset(init_packet, 0, sizeof(struct nvsp_message));
472         init_packet->hdr.msg_type = NVSP_MSG2_TYPE_SEND_NDIS_CONFIG;
473         init_packet->msg.v2_msg.send_ndis_config.mtu = ndev->mtu + ETH_HLEN;
474         init_packet->msg.v2_msg.send_ndis_config.capability.ieee8021q = 1;
475
476         if (nvsp_ver >= NVSP_PROTOCOL_VERSION_5) {
477                 init_packet->msg.v2_msg.send_ndis_config.capability.sriov = 1;
478
479                 /* Teaming bit is needed to receive link speed updates */
480                 init_packet->msg.v2_msg.send_ndis_config.capability.teaming = 1;
481         }
482
483         ret = vmbus_sendpacket(device->channel, init_packet,
484                                 sizeof(struct nvsp_message),
485                                 (unsigned long)init_packet,
486                                 VM_PKT_DATA_INBAND, 0);
487
488         return ret;
489 }
490
491 static int netvsc_connect_vsp(struct hv_device *device)
492 {
493         int ret;
494         struct netvsc_device *net_device;
495         struct nvsp_message *init_packet;
496         int ndis_version;
497         u32 ver_list[] = { NVSP_PROTOCOL_VERSION_1, NVSP_PROTOCOL_VERSION_2,
498                 NVSP_PROTOCOL_VERSION_4, NVSP_PROTOCOL_VERSION_5 };
499         int i, num_ver = 4; /* number of different NVSP versions */
500
501         net_device = get_outbound_net_device(device);
502         if (!net_device)
503                 return -ENODEV;
504
505         init_packet = &net_device->channel_init_pkt;
506
507         /* Negotiate the latest NVSP protocol supported */
508         for (i = num_ver - 1; i >= 0; i--)
509                 if (negotiate_nvsp_ver(device, net_device, init_packet,
510                                        ver_list[i])  == 0) {
511                         net_device->nvsp_version = ver_list[i];
512                         break;
513                 }
514
515         if (i < 0) {
516                 ret = -EPROTO;
517                 goto cleanup;
518         }
519
520         pr_debug("Negotiated NVSP version:%x\n", net_device->nvsp_version);
521
522         /* Send the ndis version */
523         memset(init_packet, 0, sizeof(struct nvsp_message));
524
525         if (net_device->nvsp_version <= NVSP_PROTOCOL_VERSION_4)
526                 ndis_version = 0x00060001;
527         else
528                 ndis_version = 0x0006001e;
529
530         init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_NDIS_VER;
531         init_packet->msg.v1_msg.
532                 send_ndis_ver.ndis_major_ver =
533                                 (ndis_version & 0xFFFF0000) >> 16;
534         init_packet->msg.v1_msg.
535                 send_ndis_ver.ndis_minor_ver =
536                                 ndis_version & 0xFFFF;
537
538         /* Send the init request */
539         ret = vmbus_sendpacket(device->channel, init_packet,
540                                 sizeof(struct nvsp_message),
541                                 (unsigned long)init_packet,
542                                 VM_PKT_DATA_INBAND, 0);
543         if (ret != 0)
544                 goto cleanup;
545
546         /* Post the big receive buffer to NetVSP */
547         if (net_device->nvsp_version <= NVSP_PROTOCOL_VERSION_2)
548                 net_device->recv_buf_size = NETVSC_RECEIVE_BUFFER_SIZE_LEGACY;
549         else
550                 net_device->recv_buf_size = NETVSC_RECEIVE_BUFFER_SIZE;
551         net_device->send_buf_size = NETVSC_SEND_BUFFER_SIZE;
552
553         ret = netvsc_init_buf(device);
554
555 cleanup:
556         return ret;
557 }
558
559 static void netvsc_disconnect_vsp(struct hv_device *device)
560 {
561         netvsc_destroy_buf(device);
562 }
563
564 /*
565  * netvsc_device_remove - Callback when the root bus device is removed
566  */
567 int netvsc_device_remove(struct hv_device *device)
568 {
569         struct net_device *ndev = hv_get_drvdata(device);
570         struct net_device_context *net_device_ctx = netdev_priv(ndev);
571         struct netvsc_device *net_device = net_device_ctx->nvdev;
572
573         netvsc_disconnect_vsp(device);
574
575         net_device_ctx->nvdev = NULL;
576
577         /*
578          * At this point, no one should be accessing net_device
579          * except in here
580          */
581         dev_notice(&device->device, "net device safe to remove\n");
582
583         /* Now, we can close the channel safely */
584         vmbus_close(device->channel);
585
586         /* Release all resources */
587         vfree(net_device->sub_cb_buf);
588         free_netvsc_device(net_device);
589         return 0;
590 }
591
592 #define RING_AVAIL_PERCENT_HIWATER 20
593 #define RING_AVAIL_PERCENT_LOWATER 10
594
595 /*
596  * Get the percentage of available bytes to write in the ring.
597  * The return value is in range from 0 to 100.
598  */
599 static inline u32 hv_ringbuf_avail_percent(
600                 struct hv_ring_buffer_info *ring_info)
601 {
602         u32 avail_read, avail_write;
603
604         hv_get_ringbuffer_availbytes(ring_info, &avail_read, &avail_write);
605
606         return avail_write * 100 / ring_info->ring_datasize;
607 }
608
609 static inline void netvsc_free_send_slot(struct netvsc_device *net_device,
610                                          u32 index)
611 {
612         sync_change_bit(index, net_device->send_section_map);
613 }
614
615 static void netvsc_send_completion(struct netvsc_device *net_device,
616                                    struct vmbus_channel *incoming_channel,
617                                    struct hv_device *device,
618                                    struct vmpacket_descriptor *packet)
619 {
620         struct nvsp_message *nvsp_packet;
621         struct hv_netvsc_packet *nvsc_packet;
622         struct net_device *ndev = hv_get_drvdata(device);
623         struct net_device_context *net_device_ctx = netdev_priv(ndev);
624         u32 send_index;
625         struct sk_buff *skb;
626
627         nvsp_packet = (struct nvsp_message *)((unsigned long)packet +
628                         (packet->offset8 << 3));
629
630         if ((nvsp_packet->hdr.msg_type == NVSP_MSG_TYPE_INIT_COMPLETE) ||
631             (nvsp_packet->hdr.msg_type ==
632              NVSP_MSG1_TYPE_SEND_RECV_BUF_COMPLETE) ||
633             (nvsp_packet->hdr.msg_type ==
634              NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE) ||
635             (nvsp_packet->hdr.msg_type ==
636              NVSP_MSG5_TYPE_SUBCHANNEL)) {
637                 /* Copy the response back */
638                 memcpy(&net_device->channel_init_pkt, nvsp_packet,
639                        sizeof(struct nvsp_message));
640                 complete(&net_device->channel_init_wait);
641         } else if (nvsp_packet->hdr.msg_type ==
642                    NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE) {
643                 int num_outstanding_sends;
644                 u16 q_idx = 0;
645                 struct vmbus_channel *channel = device->channel;
646                 int queue_sends;
647
648                 /* Get the send context */
649                 skb = (struct sk_buff *)(unsigned long)packet->trans_id;
650
651                 /* Notify the layer above us */
652                 if (skb) {
653                         nvsc_packet = (struct hv_netvsc_packet *) skb->cb;
654                         send_index = nvsc_packet->send_buf_index;
655                         if (send_index != NETVSC_INVALID_INDEX)
656                                 netvsc_free_send_slot(net_device, send_index);
657                         q_idx = nvsc_packet->q_idx;
658                         channel = incoming_channel;
659                         dev_kfree_skb_any(skb);
660                 }
661
662                 num_outstanding_sends =
663                         atomic_dec_return(&net_device->num_outstanding_sends);
664                 queue_sends = atomic_dec_return(&net_device->
665                                                 queue_sends[q_idx]);
666
667                 if (net_device->destroy && num_outstanding_sends == 0)
668                         wake_up(&net_device->wait_drain);
669
670                 if (netif_tx_queue_stopped(netdev_get_tx_queue(ndev, q_idx)) &&
671                     !net_device_ctx->start_remove &&
672                     (hv_ringbuf_avail_percent(&channel->outbound) >
673                      RING_AVAIL_PERCENT_HIWATER || queue_sends < 1))
674                         netif_tx_wake_queue(netdev_get_tx_queue(ndev, q_idx));
675         } else {
676                 netdev_err(ndev, "Unknown send completion packet type- "
677                            "%d received!!\n", nvsp_packet->hdr.msg_type);
678         }
679 }
680
681 static u32 netvsc_get_next_send_section(struct netvsc_device *net_device)
682 {
683         unsigned long index;
684         u32 max_words = net_device->map_words;
685         unsigned long *map_addr = (unsigned long *)net_device->send_section_map;
686         u32 section_cnt = net_device->send_section_cnt;
687         int ret_val = NETVSC_INVALID_INDEX;
688         int i;
689         int prev_val;
690
691         for (i = 0; i < max_words; i++) {
692                 if (!~(map_addr[i]))
693                         continue;
694                 index = ffz(map_addr[i]);
695                 prev_val = sync_test_and_set_bit(index, &map_addr[i]);
696                 if (prev_val)
697                         continue;
698                 if ((index + (i * BITS_PER_LONG)) >= section_cnt)
699                         break;
700                 ret_val = (index + (i * BITS_PER_LONG));
701                 break;
702         }
703         return ret_val;
704 }
705
706 static u32 netvsc_copy_to_send_buf(struct netvsc_device *net_device,
707                                    unsigned int section_index,
708                                    u32 pend_size,
709                                    struct hv_netvsc_packet *packet,
710                                    struct rndis_message *rndis_msg,
711                                    struct hv_page_buffer **pb,
712                                    struct sk_buff *skb)
713 {
714         char *start = net_device->send_buf;
715         char *dest = start + (section_index * net_device->send_section_size)
716                      + pend_size;
717         int i;
718         bool is_data_pkt = (skb != NULL) ? true : false;
719         bool xmit_more = (skb != NULL) ? skb->xmit_more : false;
720         u32 msg_size = 0;
721         u32 padding = 0;
722         u32 remain = packet->total_data_buflen % net_device->pkt_align;
723         u32 page_count = packet->cp_partial ? packet->rmsg_pgcnt :
724                 packet->page_buf_cnt;
725
726         /* Add padding */
727         if (is_data_pkt && xmit_more && remain &&
728             !packet->cp_partial) {
729                 padding = net_device->pkt_align - remain;
730                 rndis_msg->msg_len += padding;
731                 packet->total_data_buflen += padding;
732         }
733
734         for (i = 0; i < page_count; i++) {
735                 char *src = phys_to_virt((*pb)[i].pfn << PAGE_SHIFT);
736                 u32 offset = (*pb)[i].offset;
737                 u32 len = (*pb)[i].len;
738
739                 memcpy(dest, (src + offset), len);
740                 msg_size += len;
741                 dest += len;
742         }
743
744         if (padding) {
745                 memset(dest, 0, padding);
746                 msg_size += padding;
747         }
748
749         return msg_size;
750 }
751
752 static inline int netvsc_send_pkt(
753         struct hv_device *device,
754         struct hv_netvsc_packet *packet,
755         struct netvsc_device *net_device,
756         struct hv_page_buffer **pb,
757         struct sk_buff *skb)
758 {
759         struct nvsp_message nvmsg;
760         u16 q_idx = packet->q_idx;
761         struct vmbus_channel *out_channel = net_device->chn_table[q_idx];
762         struct net_device *ndev = hv_get_drvdata(device);
763         u64 req_id;
764         int ret;
765         struct hv_page_buffer *pgbuf;
766         u32 ring_avail = hv_ringbuf_avail_percent(&out_channel->outbound);
767         bool xmit_more = (skb != NULL) ? skb->xmit_more : false;
768
769         nvmsg.hdr.msg_type = NVSP_MSG1_TYPE_SEND_RNDIS_PKT;
770         if (skb != NULL) {
771                 /* 0 is RMC_DATA; */
772                 nvmsg.msg.v1_msg.send_rndis_pkt.channel_type = 0;
773         } else {
774                 /* 1 is RMC_CONTROL; */
775                 nvmsg.msg.v1_msg.send_rndis_pkt.channel_type = 1;
776         }
777
778         nvmsg.msg.v1_msg.send_rndis_pkt.send_buf_section_index =
779                 packet->send_buf_index;
780         if (packet->send_buf_index == NETVSC_INVALID_INDEX)
781                 nvmsg.msg.v1_msg.send_rndis_pkt.send_buf_section_size = 0;
782         else
783                 nvmsg.msg.v1_msg.send_rndis_pkt.send_buf_section_size =
784                         packet->total_data_buflen;
785
786         req_id = (ulong)skb;
787
788         if (out_channel->rescind)
789                 return -ENODEV;
790
791         /*
792          * It is possible that once we successfully place this packet
793          * on the ringbuffer, we may stop the queue. In that case, we want
794          * to notify the host independent of the xmit_more flag. We don't
795          * need to be precise here; in the worst case we may signal the host
796          * unnecessarily.
797          */
798         if (ring_avail < (RING_AVAIL_PERCENT_LOWATER + 1))
799                 xmit_more = false;
800
801         if (packet->page_buf_cnt) {
802                 pgbuf = packet->cp_partial ? (*pb) +
803                         packet->rmsg_pgcnt : (*pb);
804                 ret = vmbus_sendpacket_pagebuffer_ctl(out_channel,
805                                                       pgbuf,
806                                                       packet->page_buf_cnt,
807                                                       &nvmsg,
808                                                       sizeof(struct nvsp_message),
809                                                       req_id,
810                                                       VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED,
811                                                       !xmit_more);
812         } else {
813                 ret = vmbus_sendpacket_ctl(out_channel, &nvmsg,
814                                            sizeof(struct nvsp_message),
815                                            req_id,
816                                            VM_PKT_DATA_INBAND,
817                                            VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED,
818                                            !xmit_more);
819         }
820
821         if (ret == 0) {
822                 atomic_inc(&net_device->num_outstanding_sends);
823                 atomic_inc(&net_device->queue_sends[q_idx]);
824
825                 if (ring_avail < RING_AVAIL_PERCENT_LOWATER) {
826                         netif_tx_stop_queue(netdev_get_tx_queue(ndev, q_idx));
827
828                         if (atomic_read(&net_device->
829                                 queue_sends[q_idx]) < 1)
830                                 netif_tx_wake_queue(netdev_get_tx_queue(
831                                                     ndev, q_idx));
832                 }
833         } else if (ret == -EAGAIN) {
834                 netif_tx_stop_queue(netdev_get_tx_queue(
835                                     ndev, q_idx));
836                 if (atomic_read(&net_device->queue_sends[q_idx]) < 1) {
837                         netif_tx_wake_queue(netdev_get_tx_queue(
838                                             ndev, q_idx));
839                         ret = -ENOSPC;
840                 }
841         } else {
842                 netdev_err(ndev, "Unable to send packet %p ret %d\n",
843                            packet, ret);
844         }
845
846         return ret;
847 }
848
849 /* Move packet out of multi send data (msd), and clear msd */
850 static inline void move_pkt_msd(struct hv_netvsc_packet **msd_send,
851                                 struct sk_buff **msd_skb,
852                                 struct multi_send_data *msdp)
853 {
854         *msd_skb = msdp->skb;
855         *msd_send = msdp->pkt;
856         msdp->skb = NULL;
857         msdp->pkt = NULL;
858         msdp->count = 0;
859 }
860
861 int netvsc_send(struct hv_device *device,
862                 struct hv_netvsc_packet *packet,
863                 struct rndis_message *rndis_msg,
864                 struct hv_page_buffer **pb,
865                 struct sk_buff *skb)
866 {
867         struct netvsc_device *net_device;
868         int ret = 0, m_ret = 0;
869         struct vmbus_channel *out_channel;
870         u16 q_idx = packet->q_idx;
871         u32 pktlen = packet->total_data_buflen, msd_len = 0;
872         unsigned int section_index = NETVSC_INVALID_INDEX;
873         struct multi_send_data *msdp;
874         struct hv_netvsc_packet *msd_send = NULL, *cur_send = NULL;
875         struct sk_buff *msd_skb = NULL;
876         bool try_batch;
877         bool xmit_more = (skb != NULL) ? skb->xmit_more : false;
878
879         net_device = get_outbound_net_device(device);
880         if (!net_device)
881                 return -ENODEV;
882
883         out_channel = net_device->chn_table[q_idx];
884
885         packet->send_buf_index = NETVSC_INVALID_INDEX;
886         packet->cp_partial = false;
887
888         /* Send control message directly without accessing msd (Multi-Send
889          * Data) field which may be changed during data packet processing.
890          */
891         if (!skb) {
892                 cur_send = packet;
893                 goto send_now;
894         }
895
896         msdp = &net_device->msd[q_idx];
897
898         /* batch packets in send buffer if possible */
899         if (msdp->pkt)
900                 msd_len = msdp->pkt->total_data_buflen;
901
902         try_batch = (skb != NULL) && msd_len > 0 && msdp->count <
903                     net_device->max_pkt;
904
905         if (try_batch && msd_len + pktlen + net_device->pkt_align <
906             net_device->send_section_size) {
907                 section_index = msdp->pkt->send_buf_index;
908
909         } else if (try_batch && msd_len + packet->rmsg_size <
910                    net_device->send_section_size) {
911                 section_index = msdp->pkt->send_buf_index;
912                 packet->cp_partial = true;
913
914         } else if ((skb != NULL) && pktlen + net_device->pkt_align <
915                    net_device->send_section_size) {
916                 section_index = netvsc_get_next_send_section(net_device);
917                 if (section_index != NETVSC_INVALID_INDEX) {
918                         move_pkt_msd(&msd_send, &msd_skb, msdp);
919                         msd_len = 0;
920                 }
921         }
922
923         if (section_index != NETVSC_INVALID_INDEX) {
924                 netvsc_copy_to_send_buf(net_device,
925                                         section_index, msd_len,
926                                         packet, rndis_msg, pb, skb);
927
928                 packet->send_buf_index = section_index;
929
930                 if (packet->cp_partial) {
931                         packet->page_buf_cnt -= packet->rmsg_pgcnt;
932                         packet->total_data_buflen = msd_len + packet->rmsg_size;
933                 } else {
934                         packet->page_buf_cnt = 0;
935                         packet->total_data_buflen += msd_len;
936                 }
937
938                 if (msdp->skb)
939                         dev_kfree_skb_any(msdp->skb);
940
941                 if (xmit_more && !packet->cp_partial) {
942                         msdp->skb = skb;
943                         msdp->pkt = packet;
944                         msdp->count++;
945                 } else {
946                         cur_send = packet;
947                         msdp->skb = NULL;
948                         msdp->pkt = NULL;
949                         msdp->count = 0;
950                 }
951         } else {
952                 move_pkt_msd(&msd_send, &msd_skb, msdp);
953                 cur_send = packet;
954         }
955
956         if (msd_send) {
957                 m_ret = netvsc_send_pkt(device, msd_send, net_device,
958                                         NULL, msd_skb);
959
960                 if (m_ret != 0) {
961                         netvsc_free_send_slot(net_device,
962                                               msd_send->send_buf_index);
963                         dev_kfree_skb_any(msd_skb);
964                 }
965         }
966
967 send_now:
968         if (cur_send)
969                 ret = netvsc_send_pkt(device, cur_send, net_device, pb, skb);
970
971         if (ret != 0 && section_index != NETVSC_INVALID_INDEX)
972                 netvsc_free_send_slot(net_device, section_index);
973
974         return ret;
975 }
976
977 static int netvsc_send_recv_completion(struct vmbus_channel *channel,
978                                        u64 transaction_id, u32 status)
979 {
980         struct nvsp_message recvcompMessage;
981         int ret;
982
983         recvcompMessage.hdr.msg_type =
984                                 NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE;
985
986         recvcompMessage.msg.v1_msg.send_rndis_pkt_complete.status = status;
987
988         /* Send the completion */
989         ret = vmbus_sendpacket(channel, &recvcompMessage,
990                                sizeof(struct nvsp_message_header) + sizeof(u32),
991                                transaction_id, VM_PKT_COMP, 0);
992
993         return ret;
994 }
995
996 static inline void count_recv_comp_slot(struct netvsc_device *nvdev, u16 q_idx,
997                                         u32 *filled, u32 *avail)
998 {
999         u32 first = nvdev->mrc[q_idx].first;
1000         u32 next = nvdev->mrc[q_idx].next;
1001
1002         *filled = (first > next) ? NETVSC_RECVSLOT_MAX - first + next :
1003                   next - first;
1004
1005         *avail = NETVSC_RECVSLOT_MAX - *filled - 1;
1006 }
1007
1008 /* Read the first filled slot, no change to index */
1009 static inline struct recv_comp_data *read_recv_comp_slot(struct netvsc_device
1010                                                          *nvdev, u16 q_idx)
1011 {
1012         u32 filled, avail;
1013
1014         if (!nvdev->mrc[q_idx].buf)
1015                 return NULL;
1016
1017         count_recv_comp_slot(nvdev, q_idx, &filled, &avail);
1018         if (!filled)
1019                 return NULL;
1020
1021         return nvdev->mrc[q_idx].buf + nvdev->mrc[q_idx].first *
1022                sizeof(struct recv_comp_data);
1023 }
1024
1025 /* Put the first filled slot back to available pool */
1026 static inline void put_recv_comp_slot(struct netvsc_device *nvdev, u16 q_idx)
1027 {
1028         int num_recv;
1029
1030         nvdev->mrc[q_idx].first = (nvdev->mrc[q_idx].first + 1) %
1031                                   NETVSC_RECVSLOT_MAX;
1032
1033         num_recv = atomic_dec_return(&nvdev->num_outstanding_recvs);
1034
1035         if (nvdev->destroy && num_recv == 0)
1036                 wake_up(&nvdev->wait_drain);
1037 }
1038
1039 /* Check and send pending recv completions */
1040 static void netvsc_chk_recv_comp(struct netvsc_device *nvdev,
1041                                  struct vmbus_channel *channel, u16 q_idx)
1042 {
1043         struct recv_comp_data *rcd;
1044         int ret;
1045
1046         while (true) {
1047                 rcd = read_recv_comp_slot(nvdev, q_idx);
1048                 if (!rcd)
1049                         break;
1050
1051                 ret = netvsc_send_recv_completion(channel, rcd->tid,
1052                                                   rcd->status);
1053                 if (ret)
1054                         break;
1055
1056                 put_recv_comp_slot(nvdev, q_idx);
1057         }
1058 }
1059
1060 #define NETVSC_RCD_WATERMARK 80
1061
1062 /* Get next available slot */
1063 static inline struct recv_comp_data *get_recv_comp_slot(
1064         struct netvsc_device *nvdev, struct vmbus_channel *channel, u16 q_idx)
1065 {
1066         u32 filled, avail, next;
1067         struct recv_comp_data *rcd;
1068
1069         if (!nvdev->recv_section)
1070                 return NULL;
1071
1072         if (!nvdev->mrc[q_idx].buf)
1073                 return NULL;
1074
1075         if (atomic_read(&nvdev->num_outstanding_recvs) >
1076             nvdev->recv_section->num_sub_allocs * NETVSC_RCD_WATERMARK / 100)
1077                 netvsc_chk_recv_comp(nvdev, channel, q_idx);
1078
1079         count_recv_comp_slot(nvdev, q_idx, &filled, &avail);
1080         if (!avail)
1081                 return NULL;
1082
1083         next = nvdev->mrc[q_idx].next;
1084         rcd = nvdev->mrc[q_idx].buf + next * sizeof(struct recv_comp_data);
1085         nvdev->mrc[q_idx].next = (next + 1) % NETVSC_RECVSLOT_MAX;
1086
1087         atomic_inc(&nvdev->num_outstanding_recvs);
1088
1089         return rcd;
1090 }
1091
1092 static void netvsc_receive(struct netvsc_device *net_device,
1093                         struct vmbus_channel *channel,
1094                         struct hv_device *device,
1095                         struct vmpacket_descriptor *packet)
1096 {
1097         struct vmtransfer_page_packet_header *vmxferpage_packet;
1098         struct nvsp_message *nvsp_packet;
1099         struct hv_netvsc_packet nv_pkt;
1100         struct hv_netvsc_packet *netvsc_packet = &nv_pkt;
1101         u32 status = NVSP_STAT_SUCCESS;
1102         int i;
1103         int count = 0;
1104         struct net_device *ndev = hv_get_drvdata(device);
1105         void *data;
1106         int ret;
1107         struct recv_comp_data *rcd;
1108         u16 q_idx = channel->offermsg.offer.sub_channel_index;
1109
1110         /*
1111          * All inbound packets other than send completion should be xfer page
1112          * packet
1113          */
1114         if (packet->type != VM_PKT_DATA_USING_XFER_PAGES) {
1115                 netdev_err(ndev, "Unknown packet type received - %d\n",
1116                            packet->type);
1117                 return;
1118         }
1119
1120         nvsp_packet = (struct nvsp_message *)((unsigned long)packet +
1121                         (packet->offset8 << 3));
1122
1123         /* Make sure this is a valid nvsp packet */
1124         if (nvsp_packet->hdr.msg_type !=
1125             NVSP_MSG1_TYPE_SEND_RNDIS_PKT) {
1126                 netdev_err(ndev, "Unknown nvsp packet type received-"
1127                         " %d\n", nvsp_packet->hdr.msg_type);
1128                 return;
1129         }
1130
1131         vmxferpage_packet = (struct vmtransfer_page_packet_header *)packet;
1132
1133         if (vmxferpage_packet->xfer_pageset_id != NETVSC_RECEIVE_BUFFER_ID) {
1134                 netdev_err(ndev, "Invalid xfer page set id - "
1135                            "expecting %x got %x\n", NETVSC_RECEIVE_BUFFER_ID,
1136                            vmxferpage_packet->xfer_pageset_id);
1137                 return;
1138         }
1139
1140         count = vmxferpage_packet->range_cnt;
1141
1142         /* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */
1143         for (i = 0; i < count; i++) {
1144                 /* Initialize the netvsc packet */
1145                 data = (void *)((unsigned long)net_device->
1146                         recv_buf + vmxferpage_packet->ranges[i].byte_offset);
1147                 netvsc_packet->total_data_buflen =
1148                                         vmxferpage_packet->ranges[i].byte_count;
1149
1150                 /* Pass it to the upper layer */
1151                 status = rndis_filter_receive(device, netvsc_packet, &data,
1152                                               channel);
1153         }
1154
1155         if (!net_device->mrc[q_idx].buf) {
1156                 ret = netvsc_send_recv_completion(channel,
1157                                                   vmxferpage_packet->d.trans_id,
1158                                                   status);
1159                 if (ret)
1160                         netdev_err(ndev, "Recv_comp q:%hd, tid:%llx, err:%d\n",
1161                                    q_idx, vmxferpage_packet->d.trans_id, ret);
1162                 return;
1163         }
1164
1165         rcd = get_recv_comp_slot(net_device, channel, q_idx);
1166
1167         if (!rcd) {
1168                 netdev_err(ndev, "Recv_comp full buf q:%hd, tid:%llx\n",
1169                            q_idx, vmxferpage_packet->d.trans_id);
1170                 return;
1171         }
1172
1173         rcd->tid = vmxferpage_packet->d.trans_id;
1174         rcd->status = status;
1175 }
1176
1177 static void netvsc_send_table(struct hv_device *hdev,
1178                               struct nvsp_message *nvmsg)
1179 {
1180         struct netvsc_device *nvscdev;
1181         struct net_device *ndev = hv_get_drvdata(hdev);
1182         int i;
1183         u32 count, *tab;
1184
1185         nvscdev = get_outbound_net_device(hdev);
1186         if (!nvscdev)
1187                 return;
1188
1189         count = nvmsg->msg.v5_msg.send_table.count;
1190         if (count != VRSS_SEND_TAB_SIZE) {
1191                 netdev_err(ndev, "Received wrong send-table size:%u\n", count);
1192                 return;
1193         }
1194
1195         tab = (u32 *)((unsigned long)&nvmsg->msg.v5_msg.send_table +
1196                       nvmsg->msg.v5_msg.send_table.offset);
1197
1198         for (i = 0; i < count; i++)
1199                 nvscdev->send_table[i] = tab[i];
1200 }
1201
1202 static void netvsc_send_vf(struct net_device_context *net_device_ctx,
1203                            struct nvsp_message *nvmsg)
1204 {
1205         net_device_ctx->vf_alloc = nvmsg->msg.v4_msg.vf_assoc.allocated;
1206         net_device_ctx->vf_serial = nvmsg->msg.v4_msg.vf_assoc.serial;
1207 }
1208
1209 static inline void netvsc_receive_inband(struct hv_device *hdev,
1210                                  struct net_device_context *net_device_ctx,
1211                                  struct nvsp_message *nvmsg)
1212 {
1213         switch (nvmsg->hdr.msg_type) {
1214         case NVSP_MSG5_TYPE_SEND_INDIRECTION_TABLE:
1215                 netvsc_send_table(hdev, nvmsg);
1216                 break;
1217
1218         case NVSP_MSG4_TYPE_SEND_VF_ASSOCIATION:
1219                 netvsc_send_vf(net_device_ctx, nvmsg);
1220                 break;
1221         }
1222 }
1223
1224 static void netvsc_process_raw_pkt(struct hv_device *device,
1225                                    struct vmbus_channel *channel,
1226                                    struct netvsc_device *net_device,
1227                                    struct net_device *ndev,
1228                                    u64 request_id,
1229                                    struct vmpacket_descriptor *desc)
1230 {
1231         struct nvsp_message *nvmsg;
1232         struct net_device_context *net_device_ctx = netdev_priv(ndev);
1233
1234         nvmsg = (struct nvsp_message *)((unsigned long)
1235                 desc + (desc->offset8 << 3));
1236
1237         switch (desc->type) {
1238         case VM_PKT_COMP:
1239                 netvsc_send_completion(net_device, channel, device, desc);
1240                 break;
1241
1242         case VM_PKT_DATA_USING_XFER_PAGES:
1243                 netvsc_receive(net_device, channel, device, desc);
1244                 break;
1245
1246         case VM_PKT_DATA_INBAND:
1247                 netvsc_receive_inband(device, net_device_ctx, nvmsg);
1248                 break;
1249
1250         default:
1251                 netdev_err(ndev, "unhandled packet type %d, tid %llx\n",
1252                            desc->type, request_id);
1253                 break;
1254         }
1255 }
1256
1257 void netvsc_channel_cb(void *context)
1258 {
1259         int ret;
1260         struct vmbus_channel *channel = (struct vmbus_channel *)context;
1261         u16 q_idx = channel->offermsg.offer.sub_channel_index;
1262         struct hv_device *device;
1263         struct netvsc_device *net_device;
1264         u32 bytes_recvd;
1265         u64 request_id;
1266         struct vmpacket_descriptor *desc;
1267         unsigned char *buffer;
1268         int bufferlen = NETVSC_PACKET_SIZE;
1269         struct net_device *ndev;
1270         bool need_to_commit = false;
1271
1272         if (channel->primary_channel != NULL)
1273                 device = channel->primary_channel->device_obj;
1274         else
1275                 device = channel->device_obj;
1276
1277         net_device = get_inbound_net_device(device);
1278         if (!net_device)
1279                 return;
1280         ndev = hv_get_drvdata(device);
1281         buffer = get_per_channel_state(channel);
1282
1283         do {
1284                 desc = get_next_pkt_raw(channel);
1285                 if (desc != NULL) {
1286                         netvsc_process_raw_pkt(device,
1287                                                channel,
1288                                                net_device,
1289                                                ndev,
1290                                                desc->trans_id,
1291                                                desc);
1292
1293                         put_pkt_raw(channel, desc);
1294                         need_to_commit = true;
1295                         continue;
1296                 }
1297                 if (need_to_commit) {
1298                         need_to_commit = false;
1299                         commit_rd_index(channel);
1300                 }
1301
1302                 ret = vmbus_recvpacket_raw(channel, buffer, bufferlen,
1303                                            &bytes_recvd, &request_id);
1304                 if (ret == 0) {
1305                         if (bytes_recvd > 0) {
1306                                 desc = (struct vmpacket_descriptor *)buffer;
1307                                 netvsc_process_raw_pkt(device,
1308                                                        channel,
1309                                                        net_device,
1310                                                        ndev,
1311                                                        request_id,
1312                                                        desc);
1313                         } else {
1314                                 /*
1315                                  * We are done for this pass.
1316                                  */
1317                                 break;
1318                         }
1319
1320                 } else if (ret == -ENOBUFS) {
1321                         if (bufferlen > NETVSC_PACKET_SIZE)
1322                                 kfree(buffer);
1323                         /* Handle large packet */
1324                         buffer = kmalloc(bytes_recvd, GFP_ATOMIC);
1325                         if (buffer == NULL) {
1326                                 /* Try again next time around */
1327                                 netdev_err(ndev,
1328                                            "unable to allocate buffer of size "
1329                                            "(%d)!!\n", bytes_recvd);
1330                                 break;
1331                         }
1332
1333                         bufferlen = bytes_recvd;
1334                 }
1335         } while (1);
1336
1337         if (bufferlen > NETVSC_PACKET_SIZE)
1338                 kfree(buffer);
1339
1340         netvsc_chk_recv_comp(net_device, channel, q_idx);
1341 }
1342
1343 /*
1344  * netvsc_device_add - Callback when the device belonging to this
1345  * driver is added
1346  */
1347 int netvsc_device_add(struct hv_device *device, void *additional_info)
1348 {
1349         int i, ret = 0;
1350         int ring_size =
1351         ((struct netvsc_device_info *)additional_info)->ring_size;
1352         struct netvsc_device *net_device;
1353         struct net_device *ndev = hv_get_drvdata(device);
1354         struct net_device_context *net_device_ctx = netdev_priv(ndev);
1355
1356         net_device = alloc_net_device();
1357         if (!net_device)
1358                 return -ENOMEM;
1359
1360         net_device->ring_size = ring_size;
1361
1362         /* Initialize the NetVSC channel extension */
1363         init_completion(&net_device->channel_init_wait);
1364
1365         set_per_channel_state(device->channel, net_device->cb_buffer);
1366
1367         /* Open the channel */
1368         ret = vmbus_open(device->channel, ring_size * PAGE_SIZE,
1369                          ring_size * PAGE_SIZE, NULL, 0,
1370                          netvsc_channel_cb, device->channel);
1371
1372         if (ret != 0) {
1373                 netdev_err(ndev, "unable to open channel: %d\n", ret);
1374                 goto cleanup;
1375         }
1376
1377         /* Channel is opened */
1378         pr_info("hv_netvsc channel opened successfully\n");
1379
1380         /* If we're reopening the device we may have multiple queues, fill the
1381          * chn_table with the default channel to use it before subchannels are
1382          * opened.
1383          */
1384         for (i = 0; i < VRSS_CHANNEL_MAX; i++)
1385                 net_device->chn_table[i] = device->channel;
1386
1387         /* Writing nvdev pointer unlocks netvsc_send(), make sure chn_table is
1388          * populated.
1389          */
1390         wmb();
1391
1392         net_device_ctx->nvdev = net_device;
1393
1394         /* Connect with the NetVsp */
1395         ret = netvsc_connect_vsp(device);
1396         if (ret != 0) {
1397                 netdev_err(ndev,
1398                         "unable to connect to NetVSP - %d\n", ret);
1399                 goto close;
1400         }
1401
1402         return ret;
1403
1404 close:
1405         /* Now, we can close the channel safely */
1406         vmbus_close(device->channel);
1407
1408 cleanup:
1409         free_netvsc_device(net_device);
1410
1411         return ret;
1412 }