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