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