blob: 110bbb8a0baac50187d9d77c6cbc48bd8fd9557a [file] [log] [blame]
Hank Janssenfceaf242009-07-13 15:34:54 -07001/*
Hank Janssenfceaf242009-07-13 15:34:54 -07002 * 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
Jeff Kirsheradf8d3f2013-12-06 06:28:47 -080014 * this program; if not, see <http://www.gnu.org/licenses/>.
Hank Janssenfceaf242009-07-13 15:34:54 -070015 *
16 * Authors:
Haiyang Zhangd0e94d12009-11-23 17:00:22 +000017 * Haiyang Zhang <haiyangz@microsoft.com>
Hank Janssenfceaf242009-07-13 15:34:54 -070018 * Hank Janssen <hjanssen@microsoft.com>
Hank Janssenfceaf242009-07-13 15:34:54 -070019 */
Hank Jansseneb335bc2011-03-29 13:58:48 -070020#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21
Greg Kroah-Hartman5654e932009-07-14 15:08:20 -070022#include <linux/kernel.h>
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -080023#include <linux/sched.h>
24#include <linux/wait.h>
Greg Kroah-Hartman0ffa63b2009-07-15 11:06:01 -070025#include <linux/mm.h>
Greg Kroah-Hartmanb4362c92009-07-16 11:50:41 -070026#include <linux/delay.h>
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -070027#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Haiyang Zhangd9871152011-09-01 12:19:41 -070029#include <linux/netdevice.h>
Haiyang Zhangf157e782011-12-15 13:45:16 -080030#include <linux/if_ether.h>
Stephen Rothwelld6472302015-06-02 19:01:38 +100031#include <linux/vmalloc.h>
KY Srinivasanc25aaf82014-04-30 10:14:31 -070032#include <asm/sync_bitops.h>
K. Y. Srinivasan3f335ea2011-05-12 19:34:15 -070033
K. Y. Srinivasan5ca72522011-05-12 19:34:37 -070034#include "hyperv_net.h"
Hank Janssenfceaf242009-07-13 15:34:54 -070035
KY Srinivasan84bf9ce2016-04-14 16:31:54 -070036/*
Stephen Hemminger30d1de02016-08-23 12:17:48 -070037 * An API to support in-place processing of incoming VMBUS packets.
38 */
39#define VMBUS_PKT_TRAILER 8
40
41static struct vmpacket_descriptor *
42get_next_pkt_raw(struct vmbus_channel *channel)
43{
44 struct hv_ring_buffer_info *ring_info = &channel->inbound;
45 u32 read_loc = ring_info->priv_read_index;
46 void *ring_buffer = hv_get_ring_buffer(ring_info);
47 struct vmpacket_descriptor *cur_desc;
48 u32 packetlen;
49 u32 dsize = ring_info->ring_datasize;
50 u32 delta = read_loc - ring_info->ring_buffer->read_index;
51 u32 bytes_avail_toread = (hv_get_bytes_to_read(ring_info) - delta);
52
53 if (bytes_avail_toread < sizeof(struct vmpacket_descriptor))
54 return NULL;
55
56 if ((read_loc + sizeof(*cur_desc)) > dsize)
57 return NULL;
58
59 cur_desc = ring_buffer + read_loc;
60 packetlen = cur_desc->len8 << 3;
61
62 /*
63 * If the packet under consideration is wrapping around,
64 * return failure.
65 */
66 if ((read_loc + packetlen + VMBUS_PKT_TRAILER) > (dsize - 1))
67 return NULL;
68
69 return cur_desc;
70}
71
72/*
73 * A helper function to step through packets "in-place"
74 * This API is to be called after each successful call
75 * get_next_pkt_raw().
76 */
77static void put_pkt_raw(struct vmbus_channel *channel,
78 struct vmpacket_descriptor *desc)
79{
80 struct hv_ring_buffer_info *ring_info = &channel->inbound;
81 u32 read_loc = ring_info->priv_read_index;
82 u32 packetlen = desc->len8 << 3;
83 u32 dsize = ring_info->ring_datasize;
84
85 BUG_ON((read_loc + packetlen + VMBUS_PKT_TRAILER) > dsize);
86
87 /*
88 * Include the packet trailer.
89 */
90 ring_info->priv_read_index += packetlen + VMBUS_PKT_TRAILER;
91}
92
93/*
94 * This call commits the read index and potentially signals the host.
95 * Here is the pattern for using the "in-place" consumption APIs:
96 *
97 * while (get_next_pkt_raw() {
98 * process the packet "in-place";
99 * put_pkt_raw();
100 * }
101 * if (packets processed in place)
102 * commit_rd_index();
103 */
104static void commit_rd_index(struct vmbus_channel *channel)
105{
106 struct hv_ring_buffer_info *ring_info = &channel->inbound;
107 /*
108 * Make sure all reads are done before we update the read index since
109 * the writer may start writing to the read area once the read index
110 * is updated.
111 */
112 virt_rmb();
113 ring_info->ring_buffer->read_index = ring_info->priv_read_index;
114
115 if (hv_need_to_signal_on_read(ring_info))
116 vmbus_set_event(channel);
117}
118
119/*
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700120 * Switch the data path from the synthetic interface to the VF
121 * interface.
122 */
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +0200123void netvsc_switch_datapath(struct net_device *ndev, bool vf)
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700124{
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200125 struct net_device_context *net_device_ctx = netdev_priv(ndev);
126 struct hv_device *dev = net_device_ctx->device_ctx;
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +0200127 struct netvsc_device *nv_dev = net_device_ctx->nvdev;
128 struct nvsp_message *init_pkt = &nv_dev->channel_init_pkt;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700129
130 memset(init_pkt, 0, sizeof(struct nvsp_message));
131 init_pkt->hdr.msg_type = NVSP_MSG4_TYPE_SWITCH_DATA_PATH;
132 if (vf)
133 init_pkt->msg.v4_msg.active_dp.active_datapath =
134 NVSP_DATAPATH_VF;
135 else
136 init_pkt->msg.v4_msg.active_dp.active_datapath =
137 NVSP_DATAPATH_SYNTHETIC;
138
139 vmbus_sendpacket(dev->channel, init_pkt,
140 sizeof(struct nvsp_message),
141 (unsigned long)init_pkt,
142 VM_PKT_DATA_INBAND, 0);
143}
144
Vitaly Kuznetsov88098832016-05-13 13:55:25 +0200145static struct netvsc_device *alloc_net_device(void)
Hank Janssenfceaf242009-07-13 15:34:54 -0700146{
Haiyang Zhang85799a32010-12-10 12:03:54 -0800147 struct netvsc_device *net_device;
Hank Janssenfceaf242009-07-13 15:34:54 -0700148
Haiyang Zhang85799a32010-12-10 12:03:54 -0800149 net_device = kzalloc(sizeof(struct netvsc_device), GFP_KERNEL);
150 if (!net_device)
Hank Janssenfceaf242009-07-13 15:34:54 -0700151 return NULL;
152
Haiyang Zhangf90251c2014-08-15 19:18:19 +0000153 net_device->cb_buffer = kzalloc(NETVSC_PACKET_SIZE, GFP_KERNEL);
154 if (!net_device->cb_buffer) {
155 kfree(net_device);
156 return NULL;
157 }
158
Haiyang Zhangc0b558e2016-08-19 14:47:09 -0700159 net_device->mrc[0].buf = vzalloc(NETVSC_RECVSLOT_MAX *
160 sizeof(struct recv_comp_data));
161
Haiyang Zhangdc5cd892012-06-04 06:42:38 +0000162 init_waitqueue_head(&net_device->wait_drain);
K. Y. Srinivasanc38b9c72011-08-27 11:31:12 -0700163 net_device->destroy = false;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700164 atomic_set(&net_device->open_cnt, 0);
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700165 net_device->max_pkt = RNDIS_MAX_PKT_DEFAULT;
166 net_device->pkt_align = RNDIS_PKT_ALIGN_DEFAULT;
167
Haiyang Zhang85799a32010-12-10 12:03:54 -0800168 return net_device;
Hank Janssenfceaf242009-07-13 15:34:54 -0700169}
170
Haiyang Zhangf90251c2014-08-15 19:18:19 +0000171static void free_netvsc_device(struct netvsc_device *nvdev)
172{
Haiyang Zhangc0b558e2016-08-19 14:47:09 -0700173 int i;
174
175 for (i = 0; i < VRSS_CHANNEL_MAX; i++)
176 vfree(nvdev->mrc[i].buf);
177
Haiyang Zhangf90251c2014-08-15 19:18:19 +0000178 kfree(nvdev->cb_buffer);
179 kfree(nvdev);
180}
181
Haiyang Zhang5a71ae32010-12-10 12:03:55 -0800182static struct netvsc_device *get_outbound_net_device(struct hv_device *device)
Hank Janssenfceaf242009-07-13 15:34:54 -0700183{
Vitaly Kuznetsov26254662016-06-03 17:50:59 +0200184 struct netvsc_device *net_device = hv_device_to_netvsc_device(device);
Hank Janssenfceaf242009-07-13 15:34:54 -0700185
K. Y. Srinivasan9d88f332011-08-27 11:31:16 -0700186 if (net_device && net_device->destroy)
Haiyang Zhang85799a32010-12-10 12:03:54 -0800187 net_device = NULL;
Hank Janssenfceaf242009-07-13 15:34:54 -0700188
Haiyang Zhang85799a32010-12-10 12:03:54 -0800189 return net_device;
Hank Janssenfceaf242009-07-13 15:34:54 -0700190}
191
Haiyang Zhang5a71ae32010-12-10 12:03:55 -0800192static struct netvsc_device *get_inbound_net_device(struct hv_device *device)
Hank Janssenfceaf242009-07-13 15:34:54 -0700193{
Vitaly Kuznetsov26254662016-06-03 17:50:59 +0200194 struct netvsc_device *net_device = hv_device_to_netvsc_device(device);
K. Y. Srinivasan9d88f332011-08-27 11:31:16 -0700195
196 if (!net_device)
197 goto get_in_err;
198
199 if (net_device->destroy &&
Haiyang Zhangc0b558e2016-08-19 14:47:09 -0700200 atomic_read(&net_device->num_outstanding_sends) == 0 &&
201 atomic_read(&net_device->num_outstanding_recvs) == 0)
Haiyang Zhang85799a32010-12-10 12:03:54 -0800202 net_device = NULL;
Hank Janssenfceaf242009-07-13 15:34:54 -0700203
K. Y. Srinivasan9d88f332011-08-27 11:31:16 -0700204get_in_err:
Haiyang Zhang85799a32010-12-10 12:03:54 -0800205 return net_device;
Hank Janssenfceaf242009-07-13 15:34:54 -0700206}
207
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200208static int netvsc_destroy_buf(struct hv_device *device)
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700209{
210 struct nvsp_message *revoke_packet;
211 int ret = 0;
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200212 struct net_device *ndev = hv_get_drvdata(device);
Vitaly Kuznetsov26254662016-06-03 17:50:59 +0200213 struct netvsc_device *net_device = net_device_to_netvsc_device(ndev);
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700214
215 /*
216 * If we got a section count, it means we received a
217 * SendReceiveBufferComplete msg (ie sent
218 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
219 * to send a revoke msg here
220 */
221 if (net_device->recv_section_cnt) {
222 /* Send the revoke receive buffer */
223 revoke_packet = &net_device->revoke_packet;
224 memset(revoke_packet, 0, sizeof(struct nvsp_message));
225
226 revoke_packet->hdr.msg_type =
227 NVSP_MSG1_TYPE_REVOKE_RECV_BUF;
228 revoke_packet->msg.v1_msg.
229 revoke_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
230
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200231 ret = vmbus_sendpacket(device->channel,
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700232 revoke_packet,
233 sizeof(struct nvsp_message),
234 (unsigned long)revoke_packet,
235 VM_PKT_DATA_INBAND, 0);
236 /*
237 * If we failed here, we might as well return and
238 * have a leak rather than continue and a bugchk
239 */
240 if (ret != 0) {
Haiyang Zhangd9871152011-09-01 12:19:41 -0700241 netdev_err(ndev, "unable to send "
Haiyang Zhangc909ebb2011-09-01 12:19:40 -0700242 "revoke receive buffer to netvsp\n");
K. Y. Srinivasana3e00532011-08-25 09:49:12 -0700243 return ret;
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700244 }
245 }
246
247 /* Teardown the gpadl on the vsp end */
248 if (net_device->recv_buf_gpadl_handle) {
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200249 ret = vmbus_teardown_gpadl(device->channel,
250 net_device->recv_buf_gpadl_handle);
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700251
252 /* If we failed here, we might as well return and have a leak
253 * rather than continue and a bugchk
254 */
255 if (ret != 0) {
Haiyang Zhangd9871152011-09-01 12:19:41 -0700256 netdev_err(ndev,
Haiyang Zhangc909ebb2011-09-01 12:19:40 -0700257 "unable to teardown receive buffer's gpadl\n");
Dan Carpenter7f9615e2011-08-27 14:06:07 +0300258 return ret;
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700259 }
260 net_device->recv_buf_gpadl_handle = 0;
261 }
262
263 if (net_device->recv_buf) {
264 /* Free up the receive buffer */
Haiyang Zhangb679ef72014-01-27 15:03:42 -0800265 vfree(net_device->recv_buf);
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700266 net_device->recv_buf = NULL;
267 }
268
269 if (net_device->recv_section) {
270 net_device->recv_section_cnt = 0;
271 kfree(net_device->recv_section);
272 net_device->recv_section = NULL;
273 }
274
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700275 /* Deal with the send buffer we may have setup.
276 * If we got a send section size, it means we received a
Haiyang Zhangc51ed182014-12-19 18:25:18 -0800277 * NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE msg (ie sent
278 * NVSP_MSG1_TYPE_SEND_SEND_BUF msg) therefore, we need
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700279 * to send a revoke msg here
280 */
281 if (net_device->send_section_size) {
282 /* Send the revoke receive buffer */
283 revoke_packet = &net_device->revoke_packet;
284 memset(revoke_packet, 0, sizeof(struct nvsp_message));
285
286 revoke_packet->hdr.msg_type =
287 NVSP_MSG1_TYPE_REVOKE_SEND_BUF;
Haiyang Zhangc51ed182014-12-19 18:25:18 -0800288 revoke_packet->msg.v1_msg.revoke_send_buf.id =
289 NETVSC_SEND_BUFFER_ID;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700290
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200291 ret = vmbus_sendpacket(device->channel,
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700292 revoke_packet,
293 sizeof(struct nvsp_message),
294 (unsigned long)revoke_packet,
295 VM_PKT_DATA_INBAND, 0);
296 /* If we failed here, we might as well return and
297 * have a leak rather than continue and a bugchk
298 */
299 if (ret != 0) {
300 netdev_err(ndev, "unable to send "
301 "revoke send buffer to netvsp\n");
302 return ret;
303 }
304 }
305 /* Teardown the gpadl on the vsp end */
306 if (net_device->send_buf_gpadl_handle) {
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200307 ret = vmbus_teardown_gpadl(device->channel,
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700308 net_device->send_buf_gpadl_handle);
309
310 /* If we failed here, we might as well return and have a leak
311 * rather than continue and a bugchk
312 */
313 if (ret != 0) {
314 netdev_err(ndev,
315 "unable to teardown send buffer's gpadl\n");
316 return ret;
317 }
Dave Jones2f184232014-06-16 16:59:02 -0400318 net_device->send_buf_gpadl_handle = 0;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700319 }
320 if (net_device->send_buf) {
Haiyang Zhangc51ed182014-12-19 18:25:18 -0800321 /* Free up the send buffer */
KY Srinivasan06b47aa2014-08-02 10:42:02 -0700322 vfree(net_device->send_buf);
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700323 net_device->send_buf = NULL;
324 }
325 kfree(net_device->send_section_map);
326
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700327 return ret;
328}
329
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700330static int netvsc_init_buf(struct hv_device *device)
Hank Janssenfceaf242009-07-13 15:34:54 -0700331{
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700332 int ret = 0;
Haiyang Zhang85799a32010-12-10 12:03:54 -0800333 struct netvsc_device *net_device;
334 struct nvsp_message *init_packet;
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -0700335 struct net_device *ndev;
K. Y. Srinivasan0a726c22015-05-28 17:08:06 -0700336 int node;
Hank Janssenfceaf242009-07-13 15:34:54 -0700337
Haiyang Zhang5a71ae32010-12-10 12:03:55 -0800338 net_device = get_outbound_net_device(device);
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -0700339 if (!net_device)
K. Y. Srinivasan927bc332011-08-25 09:49:13 -0700340 return -ENODEV;
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +0200341 ndev = hv_get_drvdata(device);
Hank Janssenfceaf242009-07-13 15:34:54 -0700342
K. Y. Srinivasan0a726c22015-05-28 17:08:06 -0700343 node = cpu_to_node(device->channel->target_cpu);
344 net_device->recv_buf = vzalloc_node(net_device->recv_buf_size, node);
345 if (!net_device->recv_buf)
346 net_device->recv_buf = vzalloc(net_device->recv_buf_size);
347
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800348 if (!net_device->recv_buf) {
Haiyang Zhangd9871152011-09-01 12:19:41 -0700349 netdev_err(ndev, "unable to allocate receive "
Haiyang Zhangc909ebb2011-09-01 12:19:40 -0700350 "buffer of size %d\n", net_device->recv_buf_size);
K. Y. Srinivasan927bc332011-08-25 09:49:13 -0700351 ret = -ENOMEM;
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800352 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700353 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700354
Bill Pemberton454f18a2009-07-27 16:47:24 -0400355 /*
356 * Establish the gpadl handle for this buffer on this
357 * channel. Note: This call uses the vmbus connection rather
358 * than the channel to establish the gpadl handle.
359 */
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800360 ret = vmbus_establish_gpadl(device->channel, net_device->recv_buf,
361 net_device->recv_buf_size,
362 &net_device->recv_buf_gpadl_handle);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700363 if (ret != 0) {
Haiyang Zhangd9871152011-09-01 12:19:41 -0700364 netdev_err(ndev,
Haiyang Zhangc909ebb2011-09-01 12:19:40 -0700365 "unable to establish receive buffer's gpadl\n");
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800366 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700367 }
368
Bill Pemberton454f18a2009-07-27 16:47:24 -0400369 /* Notify the NetVsp of the gpadl handle */
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800370 init_packet = &net_device->channel_init_pkt;
Hank Janssenfceaf242009-07-13 15:34:54 -0700371
Haiyang Zhang85799a32010-12-10 12:03:54 -0800372 memset(init_packet, 0, sizeof(struct nvsp_message));
Hank Janssenfceaf242009-07-13 15:34:54 -0700373
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800374 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_RECV_BUF;
375 init_packet->msg.v1_msg.send_recv_buf.
376 gpadl_handle = net_device->recv_buf_gpadl_handle;
377 init_packet->msg.v1_msg.
378 send_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
Hank Janssenfceaf242009-07-13 15:34:54 -0700379
Bill Pemberton454f18a2009-07-27 16:47:24 -0400380 /* Send the gpadl notification request */
Haiyang Zhang85799a32010-12-10 12:03:54 -0800381 ret = vmbus_sendpacket(device->channel, init_packet,
Greg Kroah-Hartman5a4df292010-10-21 09:43:24 -0700382 sizeof(struct nvsp_message),
Haiyang Zhang85799a32010-12-10 12:03:54 -0800383 (unsigned long)init_packet,
Haiyang Zhang415f2282011-01-26 12:12:13 -0800384 VM_PKT_DATA_INBAND,
Greg Kroah-Hartman5a4df292010-10-21 09:43:24 -0700385 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700386 if (ret != 0) {
Haiyang Zhangd9871152011-09-01 12:19:41 -0700387 netdev_err(ndev,
Haiyang Zhangc909ebb2011-09-01 12:19:40 -0700388 "unable to send receive buffer's gpadl to netvsp\n");
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800389 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700390 }
391
Vitaly Kuznetsov53628552016-06-09 12:44:03 +0200392 wait_for_completion(&net_device->channel_init_wait);
Hank Janssenfceaf242009-07-13 15:34:54 -0700393
Bill Pemberton454f18a2009-07-27 16:47:24 -0400394 /* Check the response */
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800395 if (init_packet->msg.v1_msg.
396 send_recv_buf_complete.status != NVSP_STAT_SUCCESS) {
Haiyang Zhangd9871152011-09-01 12:19:41 -0700397 netdev_err(ndev, "Unable to complete receive buffer "
Haiyang Zhang8bff33a2011-09-01 12:19:48 -0700398 "initialization with NetVsp - status %d\n",
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800399 init_packet->msg.v1_msg.
400 send_recv_buf_complete.status);
K. Y. Srinivasan927bc332011-08-25 09:49:13 -0700401 ret = -EINVAL;
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800402 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700403 }
404
Bill Pemberton454f18a2009-07-27 16:47:24 -0400405 /* Parse the response */
Hank Janssenfceaf242009-07-13 15:34:54 -0700406
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800407 net_device->recv_section_cnt = init_packet->msg.
408 v1_msg.send_recv_buf_complete.num_sections;
Hank Janssenfceaf242009-07-13 15:34:54 -0700409
Haiyang Zhangc1813202011-11-30 07:19:07 -0800410 net_device->recv_section = kmemdup(
411 init_packet->msg.v1_msg.send_recv_buf_complete.sections,
412 net_device->recv_section_cnt *
413 sizeof(struct nvsp_1_receive_buffer_section),
414 GFP_KERNEL);
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800415 if (net_device->recv_section == NULL) {
K. Y. Srinivasan927bc332011-08-25 09:49:13 -0700416 ret = -EINVAL;
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800417 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700418 }
419
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700420 /*
421 * For 1st release, there should only be 1 section that represents the
422 * entire receive buffer
423 */
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800424 if (net_device->recv_section_cnt != 1 ||
425 net_device->recv_section->offset != 0) {
K. Y. Srinivasan927bc332011-08-25 09:49:13 -0700426 ret = -EINVAL;
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800427 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700428 }
429
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700430 /* Now setup the send buffer.
431 */
K. Y. Srinivasan5defde52015-05-28 17:08:07 -0700432 net_device->send_buf = vzalloc_node(net_device->send_buf_size, node);
433 if (!net_device->send_buf)
434 net_device->send_buf = vzalloc(net_device->send_buf_size);
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700435 if (!net_device->send_buf) {
436 netdev_err(ndev, "unable to allocate send "
437 "buffer of size %d\n", net_device->send_buf_size);
438 ret = -ENOMEM;
439 goto cleanup;
440 }
441
442 /* Establish the gpadl handle for this buffer on this
443 * channel. Note: This call uses the vmbus connection rather
444 * than the channel to establish the gpadl handle.
445 */
446 ret = vmbus_establish_gpadl(device->channel, net_device->send_buf,
447 net_device->send_buf_size,
448 &net_device->send_buf_gpadl_handle);
449 if (ret != 0) {
450 netdev_err(ndev,
451 "unable to establish send buffer's gpadl\n");
452 goto cleanup;
453 }
454
455 /* Notify the NetVsp of the gpadl handle */
456 init_packet = &net_device->channel_init_pkt;
457 memset(init_packet, 0, sizeof(struct nvsp_message));
458 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_SEND_BUF;
Haiyang Zhangc51ed182014-12-19 18:25:18 -0800459 init_packet->msg.v1_msg.send_send_buf.gpadl_handle =
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700460 net_device->send_buf_gpadl_handle;
Haiyang Zhangc51ed182014-12-19 18:25:18 -0800461 init_packet->msg.v1_msg.send_send_buf.id = NETVSC_SEND_BUFFER_ID;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700462
463 /* Send the gpadl notification request */
464 ret = vmbus_sendpacket(device->channel, init_packet,
465 sizeof(struct nvsp_message),
466 (unsigned long)init_packet,
467 VM_PKT_DATA_INBAND,
468 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
469 if (ret != 0) {
470 netdev_err(ndev,
471 "unable to send send buffer's gpadl to netvsp\n");
472 goto cleanup;
473 }
474
Vitaly Kuznetsov53628552016-06-09 12:44:03 +0200475 wait_for_completion(&net_device->channel_init_wait);
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700476
477 /* Check the response */
478 if (init_packet->msg.v1_msg.
479 send_send_buf_complete.status != NVSP_STAT_SUCCESS) {
480 netdev_err(ndev, "Unable to complete send buffer "
481 "initialization with NetVsp - status %d\n",
482 init_packet->msg.v1_msg.
Haiyang Zhangc51ed182014-12-19 18:25:18 -0800483 send_send_buf_complete.status);
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700484 ret = -EINVAL;
485 goto cleanup;
486 }
487
488 /* Parse the response */
489 net_device->send_section_size = init_packet->msg.
490 v1_msg.send_send_buf_complete.section_size;
491
492 /* Section count is simply the size divided by the section size.
493 */
494 net_device->send_section_cnt =
Stephen Hemminger796cc882016-08-23 12:17:47 -0700495 net_device->send_buf_size / net_device->send_section_size;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700496
497 dev_info(&device->device, "Send section size: %d, Section count:%d\n",
498 net_device->send_section_size, net_device->send_section_cnt);
499
500 /* Setup state for managing the send buffer. */
501 net_device->map_words = DIV_ROUND_UP(net_device->send_section_cnt,
502 BITS_PER_LONG);
503
Stephen Hemmingere53a9c22016-08-23 12:17:46 -0700504 net_device->send_section_map = kcalloc(net_device->map_words,
505 sizeof(ulong), GFP_KERNEL);
Wei Yongjundd1d3f82014-07-23 09:00:35 +0800506 if (net_device->send_section_map == NULL) {
507 ret = -ENOMEM;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700508 goto cleanup;
Wei Yongjundd1d3f82014-07-23 09:00:35 +0800509 }
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700510
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800511 goto exit;
Hank Janssenfceaf242009-07-13 15:34:54 -0700512
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800513cleanup:
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200514 netvsc_destroy_buf(device);
Hank Janssenfceaf242009-07-13 15:34:54 -0700515
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800516exit:
Hank Janssenfceaf242009-07-13 15:34:54 -0700517 return ret;
518}
519
Haiyang Zhangf157e782011-12-15 13:45:16 -0800520/* Negotiate NVSP protocol version */
521static int negotiate_nvsp_ver(struct hv_device *device,
522 struct netvsc_device *net_device,
523 struct nvsp_message *init_packet,
524 u32 nvsp_ver)
Hank Janssenfceaf242009-07-13 15:34:54 -0700525{
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +0200526 struct net_device *ndev = hv_get_drvdata(device);
Nicholas Mc Guire7390fe92015-01-25 15:46:31 +0100527 int ret;
Haiyang Zhangf157e782011-12-15 13:45:16 -0800528
529 memset(init_packet, 0, sizeof(struct nvsp_message));
530 init_packet->hdr.msg_type = NVSP_MSG_TYPE_INIT;
531 init_packet->msg.init_msg.init.min_protocol_ver = nvsp_ver;
532 init_packet->msg.init_msg.init.max_protocol_ver = nvsp_ver;
533
534 /* Send the init request */
535 ret = vmbus_sendpacket(device->channel, init_packet,
536 sizeof(struct nvsp_message),
537 (unsigned long)init_packet,
538 VM_PKT_DATA_INBAND,
539 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
540
541 if (ret != 0)
542 return ret;
543
Vitaly Kuznetsov53628552016-06-09 12:44:03 +0200544 wait_for_completion(&net_device->channel_init_wait);
Haiyang Zhangf157e782011-12-15 13:45:16 -0800545
546 if (init_packet->msg.init_msg.init_complete.status !=
547 NVSP_STAT_SUCCESS)
548 return -EINVAL;
549
Haiyang Zhanga1eabb02014-02-19 15:49:45 -0800550 if (nvsp_ver == NVSP_PROTOCOL_VERSION_1)
Haiyang Zhangf157e782011-12-15 13:45:16 -0800551 return 0;
552
Haiyang Zhang71790a22015-07-24 10:08:40 -0700553 /* NVSPv2 or later: Send NDIS config */
Haiyang Zhangf157e782011-12-15 13:45:16 -0800554 memset(init_packet, 0, sizeof(struct nvsp_message));
555 init_packet->hdr.msg_type = NVSP_MSG2_TYPE_SEND_NDIS_CONFIG;
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +0200556 init_packet->msg.v2_msg.send_ndis_config.mtu = ndev->mtu + ETH_HLEN;
Haiyang Zhang1f5f3a72012-03-12 10:20:50 +0000557 init_packet->msg.v2_msg.send_ndis_config.capability.ieee8021q = 1;
Haiyang Zhangf157e782011-12-15 13:45:16 -0800558
Haiyang Zhang7f5d5af2016-08-04 10:42:15 -0700559 if (nvsp_ver >= NVSP_PROTOCOL_VERSION_5) {
Haiyang Zhang71790a22015-07-24 10:08:40 -0700560 init_packet->msg.v2_msg.send_ndis_config.capability.sriov = 1;
561
Haiyang Zhang7f5d5af2016-08-04 10:42:15 -0700562 /* Teaming bit is needed to receive link speed updates */
563 init_packet->msg.v2_msg.send_ndis_config.capability.teaming = 1;
564 }
565
Haiyang Zhangf157e782011-12-15 13:45:16 -0800566 ret = vmbus_sendpacket(device->channel, init_packet,
567 sizeof(struct nvsp_message),
568 (unsigned long)init_packet,
569 VM_PKT_DATA_INBAND, 0);
570
571 return ret;
572}
573
574static int netvsc_connect_vsp(struct hv_device *device)
575{
576 int ret;
Haiyang Zhang85799a32010-12-10 12:03:54 -0800577 struct netvsc_device *net_device;
578 struct nvsp_message *init_packet;
579 int ndis_version;
Stephen Hemmingere5a78fa2016-08-23 12:17:49 -0700580 const u32 ver_list[] = {
581 NVSP_PROTOCOL_VERSION_1, NVSP_PROTOCOL_VERSION_2,
Haiyang Zhanga1eabb02014-02-19 15:49:45 -0800582 NVSP_PROTOCOL_VERSION_4, NVSP_PROTOCOL_VERSION_5 };
Stephen Hemmingere5a78fa2016-08-23 12:17:49 -0700583 int i;
Hank Janssenfceaf242009-07-13 15:34:54 -0700584
Haiyang Zhang5a71ae32010-12-10 12:03:55 -0800585 net_device = get_outbound_net_device(device);
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -0700586 if (!net_device)
K. Y. Srinivasan0f48c722011-08-25 09:49:14 -0700587 return -ENODEV;
Hank Janssenfceaf242009-07-13 15:34:54 -0700588
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800589 init_packet = &net_device->channel_init_pkt;
Hank Janssenfceaf242009-07-13 15:34:54 -0700590
Haiyang Zhangf157e782011-12-15 13:45:16 -0800591 /* Negotiate the latest NVSP protocol supported */
Stephen Hemmingere5a78fa2016-08-23 12:17:49 -0700592 for (i = ARRAY_SIZE(ver_list) - 1; i >= 0; i--)
Haiyang Zhanga1eabb02014-02-19 15:49:45 -0800593 if (negotiate_nvsp_ver(device, net_device, init_packet,
594 ver_list[i]) == 0) {
595 net_device->nvsp_version = ver_list[i];
596 break;
597 }
598
599 if (i < 0) {
K. Y. Srinivasan0f48c722011-08-25 09:49:14 -0700600 ret = -EPROTO;
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800601 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700602 }
Haiyang Zhangf157e782011-12-15 13:45:16 -0800603
604 pr_debug("Negotiated NVSP version:%x\n", net_device->nvsp_version);
605
Bill Pemberton454f18a2009-07-27 16:47:24 -0400606 /* Send the ndis version */
Haiyang Zhang85799a32010-12-10 12:03:54 -0800607 memset(init_packet, 0, sizeof(struct nvsp_message));
Hank Janssenfceaf242009-07-13 15:34:54 -0700608
Haiyang Zhanga1eabb02014-02-19 15:49:45 -0800609 if (net_device->nvsp_version <= NVSP_PROTOCOL_VERSION_4)
KY Srinivasan1f73db42014-04-09 15:00:46 -0700610 ndis_version = 0x00060001;
Haiyang Zhanga1eabb02014-02-19 15:49:45 -0800611 else
612 ndis_version = 0x0006001e;
Hank Janssenfceaf242009-07-13 15:34:54 -0700613
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800614 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_NDIS_VER;
615 init_packet->msg.v1_msg.
616 send_ndis_ver.ndis_major_ver =
Haiyang Zhang85799a32010-12-10 12:03:54 -0800617 (ndis_version & 0xFFFF0000) >> 16;
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800618 init_packet->msg.v1_msg.
619 send_ndis_ver.ndis_minor_ver =
Haiyang Zhang85799a32010-12-10 12:03:54 -0800620 ndis_version & 0xFFFF;
Hank Janssenfceaf242009-07-13 15:34:54 -0700621
Bill Pemberton454f18a2009-07-27 16:47:24 -0400622 /* Send the init request */
Haiyang Zhang85799a32010-12-10 12:03:54 -0800623 ret = vmbus_sendpacket(device->channel, init_packet,
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800624 sizeof(struct nvsp_message),
625 (unsigned long)init_packet,
626 VM_PKT_DATA_INBAND, 0);
K. Y. Srinivasan0f48c722011-08-25 09:49:14 -0700627 if (ret != 0)
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800628 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700629
Bill Pemberton454f18a2009-07-27 16:47:24 -0400630 /* Post the big receive buffer to NetVSP */
Haiyang Zhang99d30162014-03-09 16:10:59 -0700631 if (net_device->nvsp_version <= NVSP_PROTOCOL_VERSION_2)
632 net_device->recv_buf_size = NETVSC_RECEIVE_BUFFER_SIZE_LEGACY;
633 else
634 net_device->recv_buf_size = NETVSC_RECEIVE_BUFFER_SIZE;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700635 net_device->send_buf_size = NETVSC_SEND_BUFFER_SIZE;
Haiyang Zhang99d30162014-03-09 16:10:59 -0700636
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700637 ret = netvsc_init_buf(device);
Hank Janssenfceaf242009-07-13 15:34:54 -0700638
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800639cleanup:
Hank Janssenfceaf242009-07-13 15:34:54 -0700640 return ret;
641}
642
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200643static void netvsc_disconnect_vsp(struct hv_device *device)
Hank Janssenfceaf242009-07-13 15:34:54 -0700644{
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200645 netvsc_destroy_buf(device);
Hank Janssenfceaf242009-07-13 15:34:54 -0700646}
647
Hank Janssen3e189512010-03-04 22:11:00 +0000648/*
Haiyang Zhang5a71ae32010-12-10 12:03:55 -0800649 * netvsc_device_remove - Callback when the root bus device is removed
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700650 */
K. Y. Srinivasan905620d2011-05-10 07:54:54 -0700651int netvsc_device_remove(struct hv_device *device)
Hank Janssenfceaf242009-07-13 15:34:54 -0700652{
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200653 struct net_device *ndev = hv_get_drvdata(device);
654 struct net_device_context *net_device_ctx = netdev_priv(ndev);
655 struct netvsc_device *net_device = net_device_ctx->nvdev;
Hank Janssenfceaf242009-07-13 15:34:54 -0700656
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200657 netvsc_disconnect_vsp(device);
Hank Janssenfceaf242009-07-13 15:34:54 -0700658
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200659 net_device_ctx->nvdev = NULL;
K. Y. Srinivasan38524092011-08-27 11:31:14 -0700660
K. Y. Srinivasan86c921a2011-09-13 10:59:54 -0700661 /*
662 * At this point, no one should be accessing net_device
663 * except in here
664 */
Haiyang Zhangc909ebb2011-09-01 12:19:40 -0700665 dev_notice(&device->device, "net device safe to remove\n");
Hank Janssenfceaf242009-07-13 15:34:54 -0700666
Bill Pemberton454f18a2009-07-27 16:47:24 -0400667 /* Now, we can close the channel safely */
Haiyang Zhang85799a32010-12-10 12:03:54 -0800668 vmbus_close(device->channel);
Hank Janssenfceaf242009-07-13 15:34:54 -0700669
Bill Pemberton454f18a2009-07-27 16:47:24 -0400670 /* Release all resources */
Markus Elfringaa99c472014-11-25 22:33:45 +0100671 vfree(net_device->sub_cb_buf);
Haiyang Zhangf90251c2014-08-15 19:18:19 +0000672 free_netvsc_device(net_device);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700673 return 0;
Hank Janssenfceaf242009-07-13 15:34:54 -0700674}
675
Haiyang Zhang33be96e2012-03-27 13:20:45 +0000676#define RING_AVAIL_PERCENT_HIWATER 20
677#define RING_AVAIL_PERCENT_LOWATER 10
678
679/*
680 * Get the percentage of available bytes to write in the ring.
681 * The return value is in range from 0 to 100.
682 */
683static inline u32 hv_ringbuf_avail_percent(
684 struct hv_ring_buffer_info *ring_info)
685{
686 u32 avail_read, avail_write;
687
688 hv_get_ringbuffer_availbytes(ring_info, &avail_read, &avail_write);
689
690 return avail_write * 100 / ring_info->ring_datasize;
691}
692
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700693static inline void netvsc_free_send_slot(struct netvsc_device *net_device,
694 u32 index)
695{
696 sync_change_bit(index, net_device->send_section_map);
697}
698
KY Srinivasan97c17232014-02-16 16:38:44 -0800699static void netvsc_send_completion(struct netvsc_device *net_device,
KY Srinivasan25b85ee2015-12-01 16:43:05 -0800700 struct vmbus_channel *incoming_channel,
KY Srinivasan97c17232014-02-16 16:38:44 -0800701 struct hv_device *device,
Haiyang Zhang85799a32010-12-10 12:03:54 -0800702 struct vmpacket_descriptor *packet)
Hank Janssenfceaf242009-07-13 15:34:54 -0700703{
Haiyang Zhang85799a32010-12-10 12:03:54 -0800704 struct nvsp_message *nvsp_packet;
705 struct hv_netvsc_packet *nvsc_packet;
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200706 struct net_device *ndev = hv_get_drvdata(device);
707 struct net_device_context *net_device_ctx = netdev_priv(ndev);
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700708 u32 send_index;
KY Srinivasan3a3d9a02015-12-01 16:43:14 -0800709 struct sk_buff *skb;
Hank Janssenfceaf242009-07-13 15:34:54 -0700710
Haiyang Zhang85799a32010-12-10 12:03:54 -0800711 nvsp_packet = (struct nvsp_message *)((unsigned long)packet +
Haiyang Zhang415f2282011-01-26 12:12:13 -0800712 (packet->offset8 << 3));
Hank Janssenfceaf242009-07-13 15:34:54 -0700713
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800714 if ((nvsp_packet->hdr.msg_type == NVSP_MSG_TYPE_INIT_COMPLETE) ||
715 (nvsp_packet->hdr.msg_type ==
716 NVSP_MSG1_TYPE_SEND_RECV_BUF_COMPLETE) ||
717 (nvsp_packet->hdr.msg_type ==
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700718 NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE) ||
719 (nvsp_packet->hdr.msg_type ==
720 NVSP_MSG5_TYPE_SUBCHANNEL)) {
Bill Pemberton454f18a2009-07-27 16:47:24 -0400721 /* Copy the response back */
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800722 memcpy(&net_device->channel_init_pkt, nvsp_packet,
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700723 sizeof(struct nvsp_message));
K. Y. Srinivasan35abb212011-05-10 07:55:41 -0700724 complete(&net_device->channel_init_wait);
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800725 } else if (nvsp_packet->hdr.msg_type ==
726 NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE) {
Haiyang Zhang33be96e2012-03-27 13:20:45 +0000727 int num_outstanding_sends;
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700728 u16 q_idx = 0;
729 struct vmbus_channel *channel = device->channel;
730 int queue_sends;
Haiyang Zhang33be96e2012-03-27 13:20:45 +0000731
Bill Pemberton454f18a2009-07-27 16:47:24 -0400732 /* Get the send context */
KY Srinivasan3a3d9a02015-12-01 16:43:14 -0800733 skb = (struct sk_buff *)(unsigned long)packet->trans_id;
Hank Janssenfceaf242009-07-13 15:34:54 -0700734
Bill Pemberton454f18a2009-07-27 16:47:24 -0400735 /* Notify the layer above us */
KY Srinivasan3a3d9a02015-12-01 16:43:14 -0800736 if (skb) {
737 nvsc_packet = (struct hv_netvsc_packet *) skb->cb;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700738 send_index = nvsc_packet->send_buf_index;
739 if (send_index != NETVSC_INVALID_INDEX)
740 netvsc_free_send_slot(net_device, send_index);
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700741 q_idx = nvsc_packet->q_idx;
KY Srinivasan25b85ee2015-12-01 16:43:05 -0800742 channel = incoming_channel;
KY Srinivasan3a3d9a02015-12-01 16:43:14 -0800743 dev_kfree_skb_any(skb);
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700744 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700745
Haiyang Zhang33be96e2012-03-27 13:20:45 +0000746 num_outstanding_sends =
747 atomic_dec_return(&net_device->num_outstanding_sends);
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700748 queue_sends = atomic_dec_return(&net_device->
749 queue_sends[q_idx]);
Haiyang Zhang1d068252011-12-02 11:56:25 -0800750
Haiyang Zhangdc5cd892012-06-04 06:42:38 +0000751 if (net_device->destroy && num_outstanding_sends == 0)
752 wake_up(&net_device->wait_drain);
753
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700754 if (netif_tx_queue_stopped(netdev_get_tx_queue(ndev, q_idx)) &&
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200755 !net_device_ctx->start_remove &&
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700756 (hv_ringbuf_avail_percent(&channel->outbound) >
757 RING_AVAIL_PERCENT_HIWATER || queue_sends < 1))
Stephen Hemminger796cc882016-08-23 12:17:47 -0700758 netif_tx_wake_queue(netdev_get_tx_queue(ndev, q_idx));
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700759 } else {
Haiyang Zhangd9871152011-09-01 12:19:41 -0700760 netdev_err(ndev, "Unknown send completion packet type- "
Haiyang Zhangc909ebb2011-09-01 12:19:40 -0700761 "%d received!!\n", nvsp_packet->hdr.msg_type);
Hank Janssenfceaf242009-07-13 15:34:54 -0700762 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700763}
764
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700765static u32 netvsc_get_next_send_section(struct netvsc_device *net_device)
766{
767 unsigned long index;
768 u32 max_words = net_device->map_words;
769 unsigned long *map_addr = (unsigned long *)net_device->send_section_map;
770 u32 section_cnt = net_device->send_section_cnt;
771 int ret_val = NETVSC_INVALID_INDEX;
772 int i;
773 int prev_val;
774
775 for (i = 0; i < max_words; i++) {
776 if (!~(map_addr[i]))
777 continue;
778 index = ffz(map_addr[i]);
779 prev_val = sync_test_and_set_bit(index, &map_addr[i]);
780 if (prev_val)
781 continue;
782 if ((index + (i * BITS_PER_LONG)) >= section_cnt)
783 break;
784 ret_val = (index + (i * BITS_PER_LONG));
785 break;
786 }
787 return ret_val;
788}
789
Lad, Prabhakarda19fcd2015-02-05 15:06:33 +0000790static u32 netvsc_copy_to_send_buf(struct netvsc_device *net_device,
791 unsigned int section_index,
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700792 u32 pend_size,
KY Srinivasan24476762015-12-01 16:43:06 -0800793 struct hv_netvsc_packet *packet,
KY Srinivasana9f2e2d2015-12-01 16:43:13 -0800794 struct rndis_message *rndis_msg,
KY Srinivasan694a9fb2015-12-01 16:43:15 -0800795 struct hv_page_buffer **pb,
796 struct sk_buff *skb)
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700797{
798 char *start = net_device->send_buf;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700799 char *dest = start + (section_index * net_device->send_section_size)
800 + pend_size;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700801 int i;
KY Srinivasan694a9fb2015-12-01 16:43:15 -0800802 bool is_data_pkt = (skb != NULL) ? true : false;
KY Srinivasanbde79be2015-12-01 16:43:17 -0800803 bool xmit_more = (skb != NULL) ? skb->xmit_more : false;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700804 u32 msg_size = 0;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700805 u32 padding = 0;
806 u32 remain = packet->total_data_buflen % net_device->pkt_align;
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700807 u32 page_count = packet->cp_partial ? packet->rmsg_pgcnt :
808 packet->page_buf_cnt;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700809
810 /* Add padding */
KY Srinivasanbde79be2015-12-01 16:43:17 -0800811 if (is_data_pkt && xmit_more && remain &&
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700812 !packet->cp_partial) {
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700813 padding = net_device->pkt_align - remain;
KY Srinivasan24476762015-12-01 16:43:06 -0800814 rndis_msg->msg_len += padding;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700815 packet->total_data_buflen += padding;
816 }
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700817
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700818 for (i = 0; i < page_count; i++) {
KY Srinivasana9f2e2d2015-12-01 16:43:13 -0800819 char *src = phys_to_virt((*pb)[i].pfn << PAGE_SHIFT);
820 u32 offset = (*pb)[i].offset;
821 u32 len = (*pb)[i].len;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700822
823 memcpy(dest, (src + offset), len);
824 msg_size += len;
825 dest += len;
826 }
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700827
828 if (padding) {
829 memset(dest, 0, padding);
830 msg_size += padding;
831 }
832
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700833 return msg_size;
834}
835
Stephen Hemminger30d1de02016-08-23 12:17:48 -0700836static int netvsc_send_pkt(
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +0200837 struct hv_device *device,
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700838 struct hv_netvsc_packet *packet,
KY Srinivasana9f2e2d2015-12-01 16:43:13 -0800839 struct netvsc_device *net_device,
KY Srinivasan3a3d9a02015-12-01 16:43:14 -0800840 struct hv_page_buffer **pb,
841 struct sk_buff *skb)
Hank Janssenfceaf242009-07-13 15:34:54 -0700842{
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700843 struct nvsp_message nvmsg;
KY Srinivasan3a67c9c2014-10-05 10:42:51 -0700844 u16 q_idx = packet->q_idx;
Vitaly Kuznetsov8b9fbe12015-12-01 16:43:11 -0800845 struct vmbus_channel *out_channel = net_device->chn_table[q_idx];
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +0200846 struct net_device *ndev = hv_get_drvdata(device);
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700847 u64 req_id;
848 int ret;
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700849 struct hv_page_buffer *pgbuf;
KY Srinivasan82fa3c72015-05-11 15:39:46 -0700850 u32 ring_avail = hv_ringbuf_avail_percent(&out_channel->outbound);
KY Srinivasanbde79be2015-12-01 16:43:17 -0800851 bool xmit_more = (skb != NULL) ? skb->xmit_more : false;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700852
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700853 nvmsg.hdr.msg_type = NVSP_MSG1_TYPE_SEND_RNDIS_PKT;
KY Srinivasan694a9fb2015-12-01 16:43:15 -0800854 if (skb != NULL) {
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700855 /* 0 is RMC_DATA; */
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700856 nvmsg.msg.v1_msg.send_rndis_pkt.channel_type = 0;
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700857 } else {
858 /* 1 is RMC_CONTROL; */
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700859 nvmsg.msg.v1_msg.send_rndis_pkt.channel_type = 1;
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700860 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700861
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700862 nvmsg.msg.v1_msg.send_rndis_pkt.send_buf_section_index =
863 packet->send_buf_index;
864 if (packet->send_buf_index == NETVSC_INVALID_INDEX)
865 nvmsg.msg.v1_msg.send_rndis_pkt.send_buf_section_size = 0;
866 else
867 nvmsg.msg.v1_msg.send_rndis_pkt.send_buf_section_size =
868 packet->total_data_buflen;
Hank Janssenfceaf242009-07-13 15:34:54 -0700869
KY Srinivasan3a3d9a02015-12-01 16:43:14 -0800870 req_id = (ulong)skb;
Haiyang Zhangf1ea3cd2013-04-05 11:44:40 +0000871
Haiyang Zhangc3582a22014-12-01 13:28:39 -0800872 if (out_channel->rescind)
873 return -ENODEV;
874
KY Srinivasan82fa3c72015-05-11 15:39:46 -0700875 /*
876 * It is possible that once we successfully place this packet
877 * on the ringbuffer, we may stop the queue. In that case, we want
878 * to notify the host independent of the xmit_more flag. We don't
879 * need to be precise here; in the worst case we may signal the host
880 * unnecessarily.
881 */
882 if (ring_avail < (RING_AVAIL_PERCENT_LOWATER + 1))
KY Srinivasanbde79be2015-12-01 16:43:17 -0800883 xmit_more = false;
KY Srinivasan82fa3c72015-05-11 15:39:46 -0700884
Haiyang Zhang72a2f5b2010-12-10 12:03:58 -0800885 if (packet->page_buf_cnt) {
KY Srinivasana9f2e2d2015-12-01 16:43:13 -0800886 pgbuf = packet->cp_partial ? (*pb) +
887 packet->rmsg_pgcnt : (*pb);
KY Srinivasan82fa3c72015-05-11 15:39:46 -0700888 ret = vmbus_sendpacket_pagebuffer_ctl(out_channel,
889 pgbuf,
890 packet->page_buf_cnt,
891 &nvmsg,
892 sizeof(struct nvsp_message),
893 req_id,
894 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED,
KY Srinivasanbde79be2015-12-01 16:43:17 -0800895 !xmit_more);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700896 } else {
KY Srinivasan82fa3c72015-05-11 15:39:46 -0700897 ret = vmbus_sendpacket_ctl(out_channel, &nvmsg,
898 sizeof(struct nvsp_message),
899 req_id,
900 VM_PKT_DATA_INBAND,
901 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED,
KY Srinivasanbde79be2015-12-01 16:43:17 -0800902 !xmit_more);
Hank Janssenfceaf242009-07-13 15:34:54 -0700903 }
904
Haiyang Zhang1d068252011-12-02 11:56:25 -0800905 if (ret == 0) {
906 atomic_inc(&net_device->num_outstanding_sends);
KY Srinivasan3a67c9c2014-10-05 10:42:51 -0700907 atomic_inc(&net_device->queue_sends[q_idx]);
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700908
KY Srinivasan82fa3c72015-05-11 15:39:46 -0700909 if (ring_avail < RING_AVAIL_PERCENT_LOWATER) {
910 netif_tx_stop_queue(netdev_get_tx_queue(ndev, q_idx));
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700911
Haiyang Zhang33be96e2012-03-27 13:20:45 +0000912 if (atomic_read(&net_device->
KY Srinivasan3a67c9c2014-10-05 10:42:51 -0700913 queue_sends[q_idx]) < 1)
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700914 netif_tx_wake_queue(netdev_get_tx_queue(
KY Srinivasan3a67c9c2014-10-05 10:42:51 -0700915 ndev, q_idx));
Haiyang Zhang33be96e2012-03-27 13:20:45 +0000916 }
Haiyang Zhang1d068252011-12-02 11:56:25 -0800917 } else if (ret == -EAGAIN) {
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700918 netif_tx_stop_queue(netdev_get_tx_queue(
KY Srinivasan3a67c9c2014-10-05 10:42:51 -0700919 ndev, q_idx));
920 if (atomic_read(&net_device->queue_sends[q_idx]) < 1) {
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700921 netif_tx_wake_queue(netdev_get_tx_queue(
KY Srinivasan3a67c9c2014-10-05 10:42:51 -0700922 ndev, q_idx));
Haiyang Zhang33be96e2012-03-27 13:20:45 +0000923 ret = -ENOSPC;
924 }
Haiyang Zhang1d068252011-12-02 11:56:25 -0800925 } else {
Haiyang Zhangd9871152011-09-01 12:19:41 -0700926 netdev_err(ndev, "Unable to send packet %p ret %d\n",
Haiyang Zhang85799a32010-12-10 12:03:54 -0800927 packet, ret);
Haiyang Zhang1d068252011-12-02 11:56:25 -0800928 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700929
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700930 return ret;
931}
932
Haiyang Zhangc85e4922016-01-25 09:49:31 -0800933/* Move packet out of multi send data (msd), and clear msd */
934static inline void move_pkt_msd(struct hv_netvsc_packet **msd_send,
935 struct sk_buff **msd_skb,
936 struct multi_send_data *msdp)
937{
938 *msd_skb = msdp->skb;
939 *msd_send = msdp->pkt;
940 msdp->skb = NULL;
941 msdp->pkt = NULL;
942 msdp->count = 0;
943}
944
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700945int netvsc_send(struct hv_device *device,
KY Srinivasan24476762015-12-01 16:43:06 -0800946 struct hv_netvsc_packet *packet,
KY Srinivasana9f2e2d2015-12-01 16:43:13 -0800947 struct rndis_message *rndis_msg,
KY Srinivasan3a3d9a02015-12-01 16:43:14 -0800948 struct hv_page_buffer **pb,
949 struct sk_buff *skb)
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700950{
951 struct netvsc_device *net_device;
952 int ret = 0, m_ret = 0;
953 struct vmbus_channel *out_channel;
954 u16 q_idx = packet->q_idx;
955 u32 pktlen = packet->total_data_buflen, msd_len = 0;
956 unsigned int section_index = NETVSC_INVALID_INDEX;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700957 struct multi_send_data *msdp;
958 struct hv_netvsc_packet *msd_send = NULL, *cur_send = NULL;
Haiyang Zhangc85e4922016-01-25 09:49:31 -0800959 struct sk_buff *msd_skb = NULL;
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700960 bool try_batch;
KY Srinivasanbde79be2015-12-01 16:43:17 -0800961 bool xmit_more = (skb != NULL) ? skb->xmit_more : false;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700962
963 net_device = get_outbound_net_device(device);
964 if (!net_device)
965 return -ENODEV;
966
Vitaly Kuznetsov8b9fbe12015-12-01 16:43:11 -0800967 out_channel = net_device->chn_table[q_idx];
KY Srinivasan25b85ee2015-12-01 16:43:05 -0800968
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700969 packet->send_buf_index = NETVSC_INVALID_INDEX;
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700970 packet->cp_partial = false;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700971
Haiyang Zhangcf8190e2015-12-10 12:19:35 -0800972 /* Send control message directly without accessing msd (Multi-Send
973 * Data) field which may be changed during data packet processing.
974 */
975 if (!skb) {
976 cur_send = packet;
977 goto send_now;
978 }
979
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700980 msdp = &net_device->msd[q_idx];
981
982 /* batch packets in send buffer if possible */
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700983 if (msdp->pkt)
984 msd_len = msdp->pkt->total_data_buflen;
985
KY Srinivasan694a9fb2015-12-01 16:43:15 -0800986 try_batch = (skb != NULL) && msd_len > 0 && msdp->count <
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700987 net_device->max_pkt;
988
989 if (try_batch && msd_len + pktlen + net_device->pkt_align <
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700990 net_device->send_section_size) {
991 section_index = msdp->pkt->send_buf_index;
992
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700993 } else if (try_batch && msd_len + packet->rmsg_size <
994 net_device->send_section_size) {
995 section_index = msdp->pkt->send_buf_index;
996 packet->cp_partial = true;
997
KY Srinivasan694a9fb2015-12-01 16:43:15 -0800998 } else if ((skb != NULL) && pktlen + net_device->pkt_align <
Haiyang Zhang7c3877f2015-03-26 09:03:37 -0700999 net_device->send_section_size) {
1000 section_index = netvsc_get_next_send_section(net_device);
1001 if (section_index != NETVSC_INVALID_INDEX) {
Haiyang Zhangc85e4922016-01-25 09:49:31 -08001002 move_pkt_msd(&msd_send, &msd_skb, msdp);
1003 msd_len = 0;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -07001004 }
1005 }
1006
1007 if (section_index != NETVSC_INVALID_INDEX) {
1008 netvsc_copy_to_send_buf(net_device,
1009 section_index, msd_len,
KY Srinivasan694a9fb2015-12-01 16:43:15 -08001010 packet, rndis_msg, pb, skb);
KY Srinivasanb08cc792015-03-29 21:08:42 -07001011
Haiyang Zhang7c3877f2015-03-26 09:03:37 -07001012 packet->send_buf_index = section_index;
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -07001013
1014 if (packet->cp_partial) {
1015 packet->page_buf_cnt -= packet->rmsg_pgcnt;
1016 packet->total_data_buflen = msd_len + packet->rmsg_size;
1017 } else {
1018 packet->page_buf_cnt = 0;
1019 packet->total_data_buflen += msd_len;
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -07001020 }
Haiyang Zhang7c3877f2015-03-26 09:03:37 -07001021
Haiyang Zhangc85e4922016-01-25 09:49:31 -08001022 if (msdp->skb)
1023 dev_kfree_skb_any(msdp->skb);
Haiyang Zhangee90b812015-04-06 15:22:54 -07001024
KY Srinivasanbde79be2015-12-01 16:43:17 -08001025 if (xmit_more && !packet->cp_partial) {
Haiyang Zhangc85e4922016-01-25 09:49:31 -08001026 msdp->skb = skb;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -07001027 msdp->pkt = packet;
1028 msdp->count++;
1029 } else {
1030 cur_send = packet;
Haiyang Zhangc85e4922016-01-25 09:49:31 -08001031 msdp->skb = NULL;
Haiyang Zhang7c3877f2015-03-26 09:03:37 -07001032 msdp->pkt = NULL;
1033 msdp->count = 0;
1034 }
1035 } else {
Haiyang Zhangc85e4922016-01-25 09:49:31 -08001036 move_pkt_msd(&msd_send, &msd_skb, msdp);
Haiyang Zhang7c3877f2015-03-26 09:03:37 -07001037 cur_send = packet;
1038 }
1039
Haiyang Zhang7c3877f2015-03-26 09:03:37 -07001040 if (msd_send) {
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001041 m_ret = netvsc_send_pkt(device, msd_send, net_device,
1042 NULL, msd_skb);
Haiyang Zhang7c3877f2015-03-26 09:03:37 -07001043
1044 if (m_ret != 0) {
1045 netvsc_free_send_slot(net_device,
1046 msd_send->send_buf_index);
Haiyang Zhangc85e4922016-01-25 09:49:31 -08001047 dev_kfree_skb_any(msd_skb);
Haiyang Zhang7c3877f2015-03-26 09:03:37 -07001048 }
1049 }
1050
Haiyang Zhangcf8190e2015-12-10 12:19:35 -08001051send_now:
Haiyang Zhang7c3877f2015-03-26 09:03:37 -07001052 if (cur_send)
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001053 ret = netvsc_send_pkt(device, cur_send, net_device, pb, skb);
Haiyang Zhang7c3877f2015-03-26 09:03:37 -07001054
Jerry Snitselaar7aab5152015-05-04 10:57:16 -07001055 if (ret != 0 && section_index != NETVSC_INVALID_INDEX)
1056 netvsc_free_send_slot(net_device, section_index);
Haiyang Zhangd953ca42015-01-29 12:34:49 -08001057
Hank Janssenfceaf242009-07-13 15:34:54 -07001058 return ret;
1059}
1060
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001061static int netvsc_send_recv_completion(struct vmbus_channel *channel,
1062 u64 transaction_id, u32 status)
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -07001063{
1064 struct nvsp_message recvcompMessage;
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -07001065 int ret;
1066
1067 recvcompMessage.hdr.msg_type =
1068 NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE;
1069
Haiyang Zhang63f69212012-10-02 05:30:23 +00001070 recvcompMessage.msg.v1_msg.send_rndis_pkt_complete.status = status;
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -07001071
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -07001072 /* Send the completion */
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001073 ret = vmbus_sendpacket(channel, &recvcompMessage,
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001074 sizeof(struct nvsp_message_header) + sizeof(u32),
1075 transaction_id, VM_PKT_COMP, 0);
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -07001076
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001077 return ret;
1078}
1079
1080static inline void count_recv_comp_slot(struct netvsc_device *nvdev, u16 q_idx,
1081 u32 *filled, u32 *avail)
1082{
1083 u32 first = nvdev->mrc[q_idx].first;
1084 u32 next = nvdev->mrc[q_idx].next;
1085
1086 *filled = (first > next) ? NETVSC_RECVSLOT_MAX - first + next :
1087 next - first;
1088
1089 *avail = NETVSC_RECVSLOT_MAX - *filled - 1;
1090}
1091
1092/* Read the first filled slot, no change to index */
1093static inline struct recv_comp_data *read_recv_comp_slot(struct netvsc_device
1094 *nvdev, u16 q_idx)
1095{
1096 u32 filled, avail;
1097
1098 if (!nvdev->mrc[q_idx].buf)
1099 return NULL;
1100
1101 count_recv_comp_slot(nvdev, q_idx, &filled, &avail);
1102 if (!filled)
1103 return NULL;
1104
1105 return nvdev->mrc[q_idx].buf + nvdev->mrc[q_idx].first *
1106 sizeof(struct recv_comp_data);
1107}
1108
1109/* Put the first filled slot back to available pool */
1110static inline void put_recv_comp_slot(struct netvsc_device *nvdev, u16 q_idx)
1111{
1112 int num_recv;
1113
1114 nvdev->mrc[q_idx].first = (nvdev->mrc[q_idx].first + 1) %
1115 NETVSC_RECVSLOT_MAX;
1116
1117 num_recv = atomic_dec_return(&nvdev->num_outstanding_recvs);
1118
1119 if (nvdev->destroy && num_recv == 0)
1120 wake_up(&nvdev->wait_drain);
1121}
1122
1123/* Check and send pending recv completions */
1124static void netvsc_chk_recv_comp(struct netvsc_device *nvdev,
1125 struct vmbus_channel *channel, u16 q_idx)
1126{
1127 struct recv_comp_data *rcd;
1128 int ret;
1129
1130 while (true) {
1131 rcd = read_recv_comp_slot(nvdev, q_idx);
1132 if (!rcd)
1133 break;
1134
1135 ret = netvsc_send_recv_completion(channel, rcd->tid,
1136 rcd->status);
1137 if (ret)
1138 break;
1139
1140 put_recv_comp_slot(nvdev, q_idx);
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -07001141 }
1142}
1143
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001144#define NETVSC_RCD_WATERMARK 80
1145
1146/* Get next available slot */
1147static inline struct recv_comp_data *get_recv_comp_slot(
1148 struct netvsc_device *nvdev, struct vmbus_channel *channel, u16 q_idx)
1149{
1150 u32 filled, avail, next;
1151 struct recv_comp_data *rcd;
1152
1153 if (!nvdev->recv_section)
1154 return NULL;
1155
1156 if (!nvdev->mrc[q_idx].buf)
1157 return NULL;
1158
1159 if (atomic_read(&nvdev->num_outstanding_recvs) >
1160 nvdev->recv_section->num_sub_allocs * NETVSC_RCD_WATERMARK / 100)
1161 netvsc_chk_recv_comp(nvdev, channel, q_idx);
1162
1163 count_recv_comp_slot(nvdev, q_idx, &filled, &avail);
1164 if (!avail)
1165 return NULL;
1166
1167 next = nvdev->mrc[q_idx].next;
1168 rcd = nvdev->mrc[q_idx].buf + next * sizeof(struct recv_comp_data);
1169 nvdev->mrc[q_idx].next = (next + 1) % NETVSC_RECVSLOT_MAX;
1170
1171 atomic_inc(&nvdev->num_outstanding_recvs);
1172
1173 return rcd;
1174}
1175
KY Srinivasan97c17232014-02-16 16:38:44 -08001176static void netvsc_receive(struct netvsc_device *net_device,
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001177 struct vmbus_channel *channel,
KY Srinivasan97c17232014-02-16 16:38:44 -08001178 struct hv_device *device,
1179 struct vmpacket_descriptor *packet)
Hank Janssenfceaf242009-07-13 15:34:54 -07001180{
Haiyang Zhang85799a32010-12-10 12:03:54 -08001181 struct vmtransfer_page_packet_header *vmxferpage_packet;
1182 struct nvsp_message *nvsp_packet;
Haiyang Zhang4baab262014-04-21 14:54:43 -07001183 struct hv_netvsc_packet nv_pkt;
1184 struct hv_netvsc_packet *netvsc_packet = &nv_pkt;
1185 u32 status = NVSP_STAT_SUCCESS;
Haiyang Zhang45326342011-12-15 13:45:15 -08001186 int i;
1187 int count = 0;
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001188 struct net_device *ndev = hv_get_drvdata(device);
KY Srinivasanc4b20c62015-12-01 16:43:07 -08001189 void *data;
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001190 int ret;
1191 struct recv_comp_data *rcd;
1192 u16 q_idx = channel->offermsg.offer.sub_channel_index;
K. Y. Srinivasan779b4d12011-04-26 09:20:22 -07001193
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001194 /*
1195 * All inbound packets other than send completion should be xfer page
1196 * packet
1197 */
Haiyang Zhang415f2282011-01-26 12:12:13 -08001198 if (packet->type != VM_PKT_DATA_USING_XFER_PAGES) {
Haiyang Zhangd9871152011-09-01 12:19:41 -07001199 netdev_err(ndev, "Unknown packet type received - %d\n",
Haiyang Zhang415f2282011-01-26 12:12:13 -08001200 packet->type);
Hank Janssenfceaf242009-07-13 15:34:54 -07001201 return;
1202 }
1203
Haiyang Zhang85799a32010-12-10 12:03:54 -08001204 nvsp_packet = (struct nvsp_message *)((unsigned long)packet +
Haiyang Zhang415f2282011-01-26 12:12:13 -08001205 (packet->offset8 << 3));
Hank Janssenfceaf242009-07-13 15:34:54 -07001206
Bill Pemberton454f18a2009-07-27 16:47:24 -04001207 /* Make sure this is a valid nvsp packet */
Haiyang Zhang53d21fd2010-12-10 12:03:59 -08001208 if (nvsp_packet->hdr.msg_type !=
1209 NVSP_MSG1_TYPE_SEND_RNDIS_PKT) {
Haiyang Zhangd9871152011-09-01 12:19:41 -07001210 netdev_err(ndev, "Unknown nvsp packet type received-"
Haiyang Zhangc909ebb2011-09-01 12:19:40 -07001211 " %d\n", nvsp_packet->hdr.msg_type);
Hank Janssenfceaf242009-07-13 15:34:54 -07001212 return;
1213 }
1214
Haiyang Zhang85799a32010-12-10 12:03:54 -08001215 vmxferpage_packet = (struct vmtransfer_page_packet_header *)packet;
Hank Janssenfceaf242009-07-13 15:34:54 -07001216
Haiyang Zhang415f2282011-01-26 12:12:13 -08001217 if (vmxferpage_packet->xfer_pageset_id != NETVSC_RECEIVE_BUFFER_ID) {
Haiyang Zhangd9871152011-09-01 12:19:41 -07001218 netdev_err(ndev, "Invalid xfer page set id - "
Haiyang Zhangc909ebb2011-09-01 12:19:40 -07001219 "expecting %x got %x\n", NETVSC_RECEIVE_BUFFER_ID,
Haiyang Zhang415f2282011-01-26 12:12:13 -08001220 vmxferpage_packet->xfer_pageset_id);
Hank Janssenfceaf242009-07-13 15:34:54 -07001221 return;
1222 }
1223
Haiyang Zhang4baab262014-04-21 14:54:43 -07001224 count = vmxferpage_packet->range_cnt;
Hank Janssenfceaf242009-07-13 15:34:54 -07001225
Bill Pemberton454f18a2009-07-27 16:47:24 -04001226 /* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */
Haiyang Zhang4baab262014-04-21 14:54:43 -07001227 for (i = 0; i < count; i++) {
Bill Pemberton454f18a2009-07-27 16:47:24 -04001228 /* Initialize the netvsc packet */
KY Srinivasanc4b20c62015-12-01 16:43:07 -08001229 data = (void *)((unsigned long)net_device->
Haiyang Zhang45326342011-12-15 13:45:15 -08001230 recv_buf + vmxferpage_packet->ranges[i].byte_offset);
Haiyang Zhang72a2f5b2010-12-10 12:03:58 -08001231 netvsc_packet->total_data_buflen =
Haiyang Zhang415f2282011-01-26 12:12:13 -08001232 vmxferpage_packet->ranges[i].byte_count;
Hank Janssenfceaf242009-07-13 15:34:54 -07001233
Bill Pemberton454f18a2009-07-27 16:47:24 -04001234 /* Pass it to the upper layer */
KY Srinivasan10082f92015-12-01 16:43:18 -08001235 status = rndis_filter_receive(device, netvsc_packet, &data,
1236 channel);
Hank Janssenfceaf242009-07-13 15:34:54 -07001237 }
1238
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001239 if (!net_device->mrc[q_idx].buf) {
1240 ret = netvsc_send_recv_completion(channel,
1241 vmxferpage_packet->d.trans_id,
1242 status);
1243 if (ret)
1244 netdev_err(ndev, "Recv_comp q:%hd, tid:%llx, err:%d\n",
1245 q_idx, vmxferpage_packet->d.trans_id, ret);
1246 return;
1247 }
1248
1249 rcd = get_recv_comp_slot(net_device, channel, q_idx);
1250
1251 if (!rcd) {
1252 netdev_err(ndev, "Recv_comp full buf q:%hd, tid:%llx\n",
1253 q_idx, vmxferpage_packet->d.trans_id);
1254 return;
1255 }
1256
1257 rcd->tid = vmxferpage_packet->d.trans_id;
1258 rcd->status = status;
Hank Janssenfceaf242009-07-13 15:34:54 -07001259}
1260
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001261static void netvsc_send_table(struct hv_device *hdev,
Haiyang Zhang71790a22015-07-24 10:08:40 -07001262 struct nvsp_message *nvmsg)
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001263{
1264 struct netvsc_device *nvscdev;
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001265 struct net_device *ndev = hv_get_drvdata(hdev);
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001266 int i;
1267 u32 count, *tab;
1268
1269 nvscdev = get_outbound_net_device(hdev);
1270 if (!nvscdev)
1271 return;
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001272
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001273 count = nvmsg->msg.v5_msg.send_table.count;
1274 if (count != VRSS_SEND_TAB_SIZE) {
1275 netdev_err(ndev, "Received wrong send-table size:%u\n", count);
1276 return;
1277 }
1278
1279 tab = (u32 *)((unsigned long)&nvmsg->msg.v5_msg.send_table +
1280 nvmsg->msg.v5_msg.send_table.offset);
1281
1282 for (i = 0; i < count; i++)
1283 nvscdev->send_table[i] = tab[i];
1284}
1285
Vitaly Kuznetsovf9a7da92016-08-15 17:48:39 +02001286static void netvsc_send_vf(struct net_device_context *net_device_ctx,
Haiyang Zhang71790a22015-07-24 10:08:40 -07001287 struct nvsp_message *nvmsg)
1288{
Vitaly Kuznetsovf9a7da92016-08-15 17:48:39 +02001289 net_device_ctx->vf_alloc = nvmsg->msg.v4_msg.vf_assoc.allocated;
1290 net_device_ctx->vf_serial = nvmsg->msg.v4_msg.vf_assoc.serial;
Haiyang Zhang71790a22015-07-24 10:08:40 -07001291}
1292
1293static inline void netvsc_receive_inband(struct hv_device *hdev,
Vitaly Kuznetsovf9a7da92016-08-15 17:48:39 +02001294 struct net_device_context *net_device_ctx,
1295 struct nvsp_message *nvmsg)
Haiyang Zhang71790a22015-07-24 10:08:40 -07001296{
1297 switch (nvmsg->hdr.msg_type) {
1298 case NVSP_MSG5_TYPE_SEND_INDIRECTION_TABLE:
1299 netvsc_send_table(hdev, nvmsg);
1300 break;
1301
1302 case NVSP_MSG4_TYPE_SEND_VF_ASSOCIATION:
Vitaly Kuznetsovf9a7da92016-08-15 17:48:39 +02001303 netvsc_send_vf(net_device_ctx, nvmsg);
Haiyang Zhang71790a22015-07-24 10:08:40 -07001304 break;
1305 }
1306}
1307
K. Y. Srinivasan99a50bb2016-07-05 16:52:46 -07001308static void netvsc_process_raw_pkt(struct hv_device *device,
1309 struct vmbus_channel *channel,
1310 struct netvsc_device *net_device,
1311 struct net_device *ndev,
1312 u64 request_id,
1313 struct vmpacket_descriptor *desc)
1314{
1315 struct nvsp_message *nvmsg;
Vitaly Kuznetsovf9a7da92016-08-15 17:48:39 +02001316 struct net_device_context *net_device_ctx = netdev_priv(ndev);
K. Y. Srinivasan99a50bb2016-07-05 16:52:46 -07001317
1318 nvmsg = (struct nvsp_message *)((unsigned long)
1319 desc + (desc->offset8 << 3));
1320
1321 switch (desc->type) {
1322 case VM_PKT_COMP:
1323 netvsc_send_completion(net_device, channel, device, desc);
1324 break;
1325
1326 case VM_PKT_DATA_USING_XFER_PAGES:
1327 netvsc_receive(net_device, channel, device, desc);
1328 break;
1329
1330 case VM_PKT_DATA_INBAND:
Vitaly Kuznetsovf9a7da92016-08-15 17:48:39 +02001331 netvsc_receive_inband(device, net_device_ctx, nvmsg);
K. Y. Srinivasan99a50bb2016-07-05 16:52:46 -07001332 break;
1333
1334 default:
1335 netdev_err(ndev, "unhandled packet type %d, tid %llx\n",
1336 desc->type, request_id);
1337 break;
1338 }
1339}
1340
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001341void netvsc_channel_cb(void *context)
Hank Janssenfceaf242009-07-13 15:34:54 -07001342{
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001343 int ret;
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001344 struct vmbus_channel *channel = (struct vmbus_channel *)context;
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001345 u16 q_idx = channel->offermsg.offer.sub_channel_index;
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001346 struct hv_device *device;
Haiyang Zhang85799a32010-12-10 12:03:54 -08001347 struct netvsc_device *net_device;
1348 u32 bytes_recvd;
1349 u64 request_id;
Greg Kroah-Hartman8dc0a062009-08-27 16:02:36 -07001350 struct vmpacket_descriptor *desc;
Bill Pembertonc6fcf0b2010-04-27 16:23:47 -04001351 unsigned char *buffer;
1352 int bufferlen = NETVSC_PACKET_SIZE;
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -07001353 struct net_device *ndev;
K. Y. Srinivasan99a50bb2016-07-05 16:52:46 -07001354 bool need_to_commit = false;
Hank Janssenfceaf242009-07-13 15:34:54 -07001355
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001356 if (channel->primary_channel != NULL)
1357 device = channel->primary_channel->device_obj;
1358 else
1359 device = channel->device_obj;
1360
Haiyang Zhang5a71ae32010-12-10 12:03:55 -08001361 net_device = get_inbound_net_device(device);
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -07001362 if (!net_device)
KY Srinivasanee0c4c32014-02-16 16:38:45 -08001363 return;
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001364 ndev = hv_get_drvdata(device);
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001365 buffer = get_per_channel_state(channel);
Hank Janssenfceaf242009-07-13 15:34:54 -07001366
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001367 do {
K. Y. Srinivasan99a50bb2016-07-05 16:52:46 -07001368 desc = get_next_pkt_raw(channel);
1369 if (desc != NULL) {
1370 netvsc_process_raw_pkt(device,
1371 channel,
1372 net_device,
1373 ndev,
1374 desc->trans_id,
1375 desc);
1376
1377 put_pkt_raw(channel, desc);
1378 need_to_commit = true;
1379 continue;
1380 }
1381 if (need_to_commit) {
1382 need_to_commit = false;
1383 commit_rd_index(channel);
1384 }
1385
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001386 ret = vmbus_recvpacket_raw(channel, buffer, bufferlen,
Haiyang Zhang85799a32010-12-10 12:03:54 -08001387 &bytes_recvd, &request_id);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001388 if (ret == 0) {
Haiyang Zhang85799a32010-12-10 12:03:54 -08001389 if (bytes_recvd > 0) {
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001390 desc = (struct vmpacket_descriptor *)buffer;
K. Y. Srinivasan99a50bb2016-07-05 16:52:46 -07001391 netvsc_process_raw_pkt(device,
1392 channel,
1393 net_device,
1394 ndev,
1395 request_id,
1396 desc);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001397 } else {
KY Srinivasanee0c4c32014-02-16 16:38:45 -08001398 /*
1399 * We are done for this pass.
1400 */
Hank Janssenfceaf242009-07-13 15:34:54 -07001401 break;
1402 }
KY Srinivasanee0c4c32014-02-16 16:38:45 -08001403
K. Y. Srinivasan3d5cad92011-08-25 09:48:59 -07001404 } else if (ret == -ENOBUFS) {
KY Srinivasanee0c4c32014-02-16 16:38:45 -08001405 if (bufferlen > NETVSC_PACKET_SIZE)
1406 kfree(buffer);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001407 /* Handle large packet */
Haiyang Zhang85799a32010-12-10 12:03:54 -08001408 buffer = kmalloc(bytes_recvd, GFP_ATOMIC);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001409 if (buffer == NULL) {
Bill Pemberton454f18a2009-07-27 16:47:24 -04001410 /* Try again next time around */
Haiyang Zhangd9871152011-09-01 12:19:41 -07001411 netdev_err(ndev,
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001412 "unable to allocate buffer of size "
Haiyang Zhangc909ebb2011-09-01 12:19:40 -07001413 "(%d)!!\n", bytes_recvd);
Hank Janssenfceaf242009-07-13 15:34:54 -07001414 break;
1415 }
1416
Haiyang Zhang85799a32010-12-10 12:03:54 -08001417 bufferlen = bytes_recvd;
Hank Janssenfceaf242009-07-13 15:34:54 -07001418 }
1419 } while (1);
1420
KY Srinivasanee0c4c32014-02-16 16:38:45 -08001421 if (bufferlen > NETVSC_PACKET_SIZE)
1422 kfree(buffer);
Haiyang Zhangc0b558e2016-08-19 14:47:09 -07001423
1424 netvsc_chk_recv_comp(net_device, channel, q_idx);
Hank Janssenfceaf242009-07-13 15:34:54 -07001425}
Haiyang Zhangaf24ce42011-04-21 12:30:40 -07001426
1427/*
Haiyang Zhangb637e022011-04-21 12:30:45 -07001428 * netvsc_device_add - Callback when the device belonging to this
1429 * driver is added
1430 */
K. Y. Srinivasan7bd23a42011-05-10 07:54:53 -07001431int netvsc_device_add(struct hv_device *device, void *additional_info)
Haiyang Zhangb637e022011-04-21 12:30:45 -07001432{
Vitaly Kuznetsov88098832016-05-13 13:55:25 +02001433 int i, ret = 0;
K. Y. Srinivasanaae23982011-05-12 19:35:05 -07001434 int ring_size =
1435 ((struct netvsc_device_info *)additional_info)->ring_size;
Haiyang Zhangb637e022011-04-21 12:30:45 -07001436 struct netvsc_device *net_device;
Vitaly Kuznetsov88098832016-05-13 13:55:25 +02001437 struct net_device *ndev = hv_get_drvdata(device);
1438 struct net_device_context *net_device_ctx = netdev_priv(ndev);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001439
Vitaly Kuznetsov88098832016-05-13 13:55:25 +02001440 net_device = alloc_net_device();
Dan Carpenterb1c84922014-09-04 14:11:23 +03001441 if (!net_device)
1442 return -ENOMEM;
Haiyang Zhangb637e022011-04-21 12:30:45 -07001443
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001444 net_device->ring_size = ring_size;
1445
Haiyang Zhangb637e022011-04-21 12:30:45 -07001446 /* Initialize the NetVSC channel extension */
K. Y. Srinivasan35abb212011-05-10 07:55:41 -07001447 init_completion(&net_device->channel_init_wait);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001448
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001449 set_per_channel_state(device->channel, net_device->cb_buffer);
1450
Haiyang Zhangb637e022011-04-21 12:30:45 -07001451 /* Open the channel */
K. Y. Srinivasanaae23982011-05-12 19:35:05 -07001452 ret = vmbus_open(device->channel, ring_size * PAGE_SIZE,
1453 ring_size * PAGE_SIZE, NULL, 0,
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001454 netvsc_channel_cb, device->channel);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001455
1456 if (ret != 0) {
Haiyang Zhangd9871152011-09-01 12:19:41 -07001457 netdev_err(ndev, "unable to open channel: %d\n", ret);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001458 goto cleanup;
1459 }
1460
1461 /* Channel is opened */
Haiyang Zhangc909ebb2011-09-01 12:19:40 -07001462 pr_info("hv_netvsc channel opened successfully\n");
Haiyang Zhangb637e022011-04-21 12:30:45 -07001463
Vitaly Kuznetsov88098832016-05-13 13:55:25 +02001464 /* If we're reopening the device we may have multiple queues, fill the
1465 * chn_table with the default channel to use it before subchannels are
1466 * opened.
1467 */
1468 for (i = 0; i < VRSS_CHANNEL_MAX; i++)
1469 net_device->chn_table[i] = device->channel;
1470
1471 /* Writing nvdev pointer unlocks netvsc_send(), make sure chn_table is
1472 * populated.
1473 */
1474 wmb();
1475
1476 net_device_ctx->nvdev = net_device;
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001477
Haiyang Zhangb637e022011-04-21 12:30:45 -07001478 /* Connect with the NetVsp */
1479 ret = netvsc_connect_vsp(device);
1480 if (ret != 0) {
Haiyang Zhangd9871152011-09-01 12:19:41 -07001481 netdev_err(ndev,
Haiyang Zhangc909ebb2011-09-01 12:19:40 -07001482 "unable to connect to NetVSP - %d\n", ret);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001483 goto close;
1484 }
1485
1486 return ret;
1487
1488close:
1489 /* Now, we can close the channel safely */
1490 vmbus_close(device->channel);
1491
1492cleanup:
Haiyang Zhangf90251c2014-08-15 19:18:19 +00001493 free_netvsc_device(net_device);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001494
1495 return ret;
1496}