blob: da2d346883704a60dc106ed6497f5a28037c1b74 [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>
KY Srinivasanc25aaf82014-04-30 10:14:31 -070031#include <asm/sync_bitops.h>
K. Y. Srinivasan3f335ea2011-05-12 19:34:15 -070032
K. Y. Srinivasan5ca72522011-05-12 19:34:37 -070033#include "hyperv_net.h"
Hank Janssenfceaf242009-07-13 15:34:54 -070034
35
Haiyang Zhang5a71ae32010-12-10 12:03:55 -080036static struct netvsc_device *alloc_net_device(struct hv_device *device)
Hank Janssenfceaf242009-07-13 15:34:54 -070037{
Haiyang Zhang85799a32010-12-10 12:03:54 -080038 struct netvsc_device *net_device;
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -070039 struct net_device *ndev = hv_get_drvdata(device);
Hank Janssenfceaf242009-07-13 15:34:54 -070040
Haiyang Zhang85799a32010-12-10 12:03:54 -080041 net_device = kzalloc(sizeof(struct netvsc_device), GFP_KERNEL);
42 if (!net_device)
Hank Janssenfceaf242009-07-13 15:34:54 -070043 return NULL;
44
Haiyang Zhangdc5cd892012-06-04 06:42:38 +000045 init_waitqueue_head(&net_device->wait_drain);
Haiyang Zhang4d447c92011-12-15 13:45:17 -080046 net_device->start_remove = false;
K. Y. Srinivasanc38b9c72011-08-27 11:31:12 -070047 net_device->destroy = false;
Haiyang Zhang53d21fd2010-12-10 12:03:59 -080048 net_device->dev = device;
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -070049 net_device->ndev = ndev;
Hank Janssenfceaf242009-07-13 15:34:54 -070050
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -070051 hv_set_drvdata(device, net_device);
Haiyang Zhang85799a32010-12-10 12:03:54 -080052 return net_device;
Hank Janssenfceaf242009-07-13 15:34:54 -070053}
54
Haiyang Zhang5a71ae32010-12-10 12:03:55 -080055static struct netvsc_device *get_outbound_net_device(struct hv_device *device)
Hank Janssenfceaf242009-07-13 15:34:54 -070056{
Haiyang Zhang85799a32010-12-10 12:03:54 -080057 struct netvsc_device *net_device;
Hank Janssenfceaf242009-07-13 15:34:54 -070058
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -070059 net_device = hv_get_drvdata(device);
K. Y. Srinivasan9d88f332011-08-27 11:31:16 -070060 if (net_device && net_device->destroy)
Haiyang Zhang85799a32010-12-10 12:03:54 -080061 net_device = NULL;
Hank Janssenfceaf242009-07-13 15:34:54 -070062
Haiyang Zhang85799a32010-12-10 12:03:54 -080063 return net_device;
Hank Janssenfceaf242009-07-13 15:34:54 -070064}
65
Haiyang Zhang5a71ae32010-12-10 12:03:55 -080066static struct netvsc_device *get_inbound_net_device(struct hv_device *device)
Hank Janssenfceaf242009-07-13 15:34:54 -070067{
Haiyang Zhang85799a32010-12-10 12:03:54 -080068 struct netvsc_device *net_device;
Hank Janssenfceaf242009-07-13 15:34:54 -070069
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -070070 net_device = hv_get_drvdata(device);
K. Y. Srinivasan9d88f332011-08-27 11:31:16 -070071
72 if (!net_device)
73 goto get_in_err;
74
75 if (net_device->destroy &&
76 atomic_read(&net_device->num_outstanding_sends) == 0)
Haiyang Zhang85799a32010-12-10 12:03:54 -080077 net_device = NULL;
Hank Janssenfceaf242009-07-13 15:34:54 -070078
K. Y. Srinivasan9d88f332011-08-27 11:31:16 -070079get_in_err:
Haiyang Zhang85799a32010-12-10 12:03:54 -080080 return net_device;
Hank Janssenfceaf242009-07-13 15:34:54 -070081}
82
Hank Janssenfceaf242009-07-13 15:34:54 -070083
KY Srinivasanc25aaf82014-04-30 10:14:31 -070084static int netvsc_destroy_buf(struct netvsc_device *net_device)
Haiyang Zhangec91cd02011-04-21 12:30:43 -070085{
86 struct nvsp_message *revoke_packet;
87 int ret = 0;
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -070088 struct net_device *ndev = net_device->ndev;
Haiyang Zhangec91cd02011-04-21 12:30:43 -070089
90 /*
91 * If we got a section count, it means we received a
92 * SendReceiveBufferComplete msg (ie sent
93 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
94 * to send a revoke msg here
95 */
96 if (net_device->recv_section_cnt) {
97 /* Send the revoke receive buffer */
98 revoke_packet = &net_device->revoke_packet;
99 memset(revoke_packet, 0, sizeof(struct nvsp_message));
100
101 revoke_packet->hdr.msg_type =
102 NVSP_MSG1_TYPE_REVOKE_RECV_BUF;
103 revoke_packet->msg.v1_msg.
104 revoke_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
105
106 ret = vmbus_sendpacket(net_device->dev->channel,
107 revoke_packet,
108 sizeof(struct nvsp_message),
109 (unsigned long)revoke_packet,
110 VM_PKT_DATA_INBAND, 0);
111 /*
112 * If we failed here, we might as well return and
113 * have a leak rather than continue and a bugchk
114 */
115 if (ret != 0) {
Haiyang Zhangd9871152011-09-01 12:19:41 -0700116 netdev_err(ndev, "unable to send "
Haiyang Zhangc909ebb2011-09-01 12:19:40 -0700117 "revoke receive buffer to netvsp\n");
K. Y. Srinivasana3e00532011-08-25 09:49:12 -0700118 return ret;
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700119 }
120 }
121
122 /* Teardown the gpadl on the vsp end */
123 if (net_device->recv_buf_gpadl_handle) {
124 ret = vmbus_teardown_gpadl(net_device->dev->channel,
125 net_device->recv_buf_gpadl_handle);
126
127 /* If we failed here, we might as well return and have a leak
128 * rather than continue and a bugchk
129 */
130 if (ret != 0) {
Haiyang Zhangd9871152011-09-01 12:19:41 -0700131 netdev_err(ndev,
Haiyang Zhangc909ebb2011-09-01 12:19:40 -0700132 "unable to teardown receive buffer's gpadl\n");
Dan Carpenter7f9615e2011-08-27 14:06:07 +0300133 return ret;
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700134 }
135 net_device->recv_buf_gpadl_handle = 0;
136 }
137
138 if (net_device->recv_buf) {
139 /* Free up the receive buffer */
Haiyang Zhangb679ef72014-01-27 15:03:42 -0800140 vfree(net_device->recv_buf);
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700141 net_device->recv_buf = NULL;
142 }
143
144 if (net_device->recv_section) {
145 net_device->recv_section_cnt = 0;
146 kfree(net_device->recv_section);
147 net_device->recv_section = NULL;
148 }
149
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700150 /* Deal with the send buffer we may have setup.
151 * If we got a send section size, it means we received a
152 * SendsendBufferComplete msg (ie sent
153 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
154 * to send a revoke msg here
155 */
156 if (net_device->send_section_size) {
157 /* Send the revoke receive buffer */
158 revoke_packet = &net_device->revoke_packet;
159 memset(revoke_packet, 0, sizeof(struct nvsp_message));
160
161 revoke_packet->hdr.msg_type =
162 NVSP_MSG1_TYPE_REVOKE_SEND_BUF;
163 revoke_packet->msg.v1_msg.revoke_recv_buf.id = 0;
164
165 ret = vmbus_sendpacket(net_device->dev->channel,
166 revoke_packet,
167 sizeof(struct nvsp_message),
168 (unsigned long)revoke_packet,
169 VM_PKT_DATA_INBAND, 0);
170 /* If we failed here, we might as well return and
171 * have a leak rather than continue and a bugchk
172 */
173 if (ret != 0) {
174 netdev_err(ndev, "unable to send "
175 "revoke send buffer to netvsp\n");
176 return ret;
177 }
178 }
179 /* Teardown the gpadl on the vsp end */
180 if (net_device->send_buf_gpadl_handle) {
181 ret = vmbus_teardown_gpadl(net_device->dev->channel,
182 net_device->send_buf_gpadl_handle);
183
184 /* If we failed here, we might as well return and have a leak
185 * rather than continue and a bugchk
186 */
187 if (ret != 0) {
188 netdev_err(ndev,
189 "unable to teardown send buffer's gpadl\n");
190 return ret;
191 }
Dave Jones2f184232014-06-16 16:59:02 -0400192 net_device->send_buf_gpadl_handle = 0;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700193 }
194 if (net_device->send_buf) {
195 /* Free up the receive buffer */
KY Srinivasan06b47aa2014-08-02 10:42:02 -0700196 vfree(net_device->send_buf);
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700197 net_device->send_buf = NULL;
198 }
199 kfree(net_device->send_section_map);
200
Haiyang Zhangec91cd02011-04-21 12:30:43 -0700201 return ret;
202}
203
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700204static int netvsc_init_buf(struct hv_device *device)
Hank Janssenfceaf242009-07-13 15:34:54 -0700205{
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700206 int ret = 0;
K. Y. Srinivasan35abb212011-05-10 07:55:41 -0700207 int t;
Haiyang Zhang85799a32010-12-10 12:03:54 -0800208 struct netvsc_device *net_device;
209 struct nvsp_message *init_packet;
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -0700210 struct net_device *ndev;
Hank Janssenfceaf242009-07-13 15:34:54 -0700211
Haiyang Zhang5a71ae32010-12-10 12:03:55 -0800212 net_device = get_outbound_net_device(device);
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -0700213 if (!net_device)
K. Y. Srinivasan927bc332011-08-25 09:49:13 -0700214 return -ENODEV;
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -0700215 ndev = net_device->ndev;
Hank Janssenfceaf242009-07-13 15:34:54 -0700216
Haiyang Zhangb679ef72014-01-27 15:03:42 -0800217 net_device->recv_buf = vzalloc(net_device->recv_buf_size);
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800218 if (!net_device->recv_buf) {
Haiyang Zhangd9871152011-09-01 12:19:41 -0700219 netdev_err(ndev, "unable to allocate receive "
Haiyang Zhangc909ebb2011-09-01 12:19:40 -0700220 "buffer of size %d\n", net_device->recv_buf_size);
K. Y. Srinivasan927bc332011-08-25 09:49:13 -0700221 ret = -ENOMEM;
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800222 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700223 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700224
Bill Pemberton454f18a2009-07-27 16:47:24 -0400225 /*
226 * Establish the gpadl handle for this buffer on this
227 * channel. Note: This call uses the vmbus connection rather
228 * than the channel to establish the gpadl handle.
229 */
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800230 ret = vmbus_establish_gpadl(device->channel, net_device->recv_buf,
231 net_device->recv_buf_size,
232 &net_device->recv_buf_gpadl_handle);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700233 if (ret != 0) {
Haiyang Zhangd9871152011-09-01 12:19:41 -0700234 netdev_err(ndev,
Haiyang Zhangc909ebb2011-09-01 12:19:40 -0700235 "unable to establish receive buffer's gpadl\n");
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800236 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700237 }
238
Hank Janssenfceaf242009-07-13 15:34:54 -0700239
Bill Pemberton454f18a2009-07-27 16:47:24 -0400240 /* Notify the NetVsp of the gpadl handle */
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800241 init_packet = &net_device->channel_init_pkt;
Hank Janssenfceaf242009-07-13 15:34:54 -0700242
Haiyang Zhang85799a32010-12-10 12:03:54 -0800243 memset(init_packet, 0, sizeof(struct nvsp_message));
Hank Janssenfceaf242009-07-13 15:34:54 -0700244
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800245 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_RECV_BUF;
246 init_packet->msg.v1_msg.send_recv_buf.
247 gpadl_handle = net_device->recv_buf_gpadl_handle;
248 init_packet->msg.v1_msg.
249 send_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
Hank Janssenfceaf242009-07-13 15:34:54 -0700250
Bill Pemberton454f18a2009-07-27 16:47:24 -0400251 /* Send the gpadl notification request */
Haiyang Zhang85799a32010-12-10 12:03:54 -0800252 ret = vmbus_sendpacket(device->channel, init_packet,
Greg Kroah-Hartman5a4df292010-10-21 09:43:24 -0700253 sizeof(struct nvsp_message),
Haiyang Zhang85799a32010-12-10 12:03:54 -0800254 (unsigned long)init_packet,
Haiyang Zhang415f2282011-01-26 12:12:13 -0800255 VM_PKT_DATA_INBAND,
Greg Kroah-Hartman5a4df292010-10-21 09:43:24 -0700256 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700257 if (ret != 0) {
Haiyang Zhangd9871152011-09-01 12:19:41 -0700258 netdev_err(ndev,
Haiyang Zhangc909ebb2011-09-01 12:19:40 -0700259 "unable to send receive buffer's gpadl to netvsp\n");
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800260 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700261 }
262
K. Y. Srinivasan5c5781b32011-06-16 13:16:35 -0700263 t = wait_for_completion_timeout(&net_device->channel_init_wait, 5*HZ);
K. Y. Srinivasan35abb212011-05-10 07:55:41 -0700264 BUG_ON(t == 0);
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800265
Hank Janssenfceaf242009-07-13 15:34:54 -0700266
Bill Pemberton454f18a2009-07-27 16:47:24 -0400267 /* Check the response */
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800268 if (init_packet->msg.v1_msg.
269 send_recv_buf_complete.status != NVSP_STAT_SUCCESS) {
Haiyang Zhangd9871152011-09-01 12:19:41 -0700270 netdev_err(ndev, "Unable to complete receive buffer "
Haiyang Zhang8bff33a2011-09-01 12:19:48 -0700271 "initialization with NetVsp - status %d\n",
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800272 init_packet->msg.v1_msg.
273 send_recv_buf_complete.status);
K. Y. Srinivasan927bc332011-08-25 09:49:13 -0700274 ret = -EINVAL;
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800275 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700276 }
277
Bill Pemberton454f18a2009-07-27 16:47:24 -0400278 /* Parse the response */
Hank Janssenfceaf242009-07-13 15:34:54 -0700279
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800280 net_device->recv_section_cnt = init_packet->msg.
281 v1_msg.send_recv_buf_complete.num_sections;
Hank Janssenfceaf242009-07-13 15:34:54 -0700282
Haiyang Zhangc1813202011-11-30 07:19:07 -0800283 net_device->recv_section = kmemdup(
284 init_packet->msg.v1_msg.send_recv_buf_complete.sections,
285 net_device->recv_section_cnt *
286 sizeof(struct nvsp_1_receive_buffer_section),
287 GFP_KERNEL);
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800288 if (net_device->recv_section == NULL) {
K. Y. Srinivasan927bc332011-08-25 09:49:13 -0700289 ret = -EINVAL;
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800290 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700291 }
292
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700293 /*
294 * For 1st release, there should only be 1 section that represents the
295 * entire receive buffer
296 */
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800297 if (net_device->recv_section_cnt != 1 ||
298 net_device->recv_section->offset != 0) {
K. Y. Srinivasan927bc332011-08-25 09:49:13 -0700299 ret = -EINVAL;
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800300 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700301 }
302
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700303 /* Now setup the send buffer.
304 */
KY Srinivasan06b47aa2014-08-02 10:42:02 -0700305 net_device->send_buf = vzalloc(net_device->send_buf_size);
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700306 if (!net_device->send_buf) {
307 netdev_err(ndev, "unable to allocate send "
308 "buffer of size %d\n", net_device->send_buf_size);
309 ret = -ENOMEM;
310 goto cleanup;
311 }
312
313 /* Establish the gpadl handle for this buffer on this
314 * channel. Note: This call uses the vmbus connection rather
315 * than the channel to establish the gpadl handle.
316 */
317 ret = vmbus_establish_gpadl(device->channel, net_device->send_buf,
318 net_device->send_buf_size,
319 &net_device->send_buf_gpadl_handle);
320 if (ret != 0) {
321 netdev_err(ndev,
322 "unable to establish send buffer's gpadl\n");
323 goto cleanup;
324 }
325
326 /* Notify the NetVsp of the gpadl handle */
327 init_packet = &net_device->channel_init_pkt;
328 memset(init_packet, 0, sizeof(struct nvsp_message));
329 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_SEND_BUF;
330 init_packet->msg.v1_msg.send_recv_buf.gpadl_handle =
331 net_device->send_buf_gpadl_handle;
332 init_packet->msg.v1_msg.send_recv_buf.id = 0;
333
334 /* Send the gpadl notification request */
335 ret = vmbus_sendpacket(device->channel, init_packet,
336 sizeof(struct nvsp_message),
337 (unsigned long)init_packet,
338 VM_PKT_DATA_INBAND,
339 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
340 if (ret != 0) {
341 netdev_err(ndev,
342 "unable to send send buffer's gpadl to netvsp\n");
343 goto cleanup;
344 }
345
346 t = wait_for_completion_timeout(&net_device->channel_init_wait, 5*HZ);
347 BUG_ON(t == 0);
348
349 /* Check the response */
350 if (init_packet->msg.v1_msg.
351 send_send_buf_complete.status != NVSP_STAT_SUCCESS) {
352 netdev_err(ndev, "Unable to complete send buffer "
353 "initialization with NetVsp - status %d\n",
354 init_packet->msg.v1_msg.
355 send_recv_buf_complete.status);
356 ret = -EINVAL;
357 goto cleanup;
358 }
359
360 /* Parse the response */
361 net_device->send_section_size = init_packet->msg.
362 v1_msg.send_send_buf_complete.section_size;
363
364 /* Section count is simply the size divided by the section size.
365 */
366 net_device->send_section_cnt =
367 net_device->send_buf_size/net_device->send_section_size;
368
369 dev_info(&device->device, "Send section size: %d, Section count:%d\n",
370 net_device->send_section_size, net_device->send_section_cnt);
371
372 /* Setup state for managing the send buffer. */
373 net_device->map_words = DIV_ROUND_UP(net_device->send_section_cnt,
374 BITS_PER_LONG);
375
376 net_device->send_section_map =
377 kzalloc(net_device->map_words * sizeof(ulong), GFP_KERNEL);
Wei Yongjundd1d3f82014-07-23 09:00:35 +0800378 if (net_device->send_section_map == NULL) {
379 ret = -ENOMEM;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700380 goto cleanup;
Wei Yongjundd1d3f82014-07-23 09:00:35 +0800381 }
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700382
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800383 goto exit;
Hank Janssenfceaf242009-07-13 15:34:54 -0700384
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800385cleanup:
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700386 netvsc_destroy_buf(net_device);
Hank Janssenfceaf242009-07-13 15:34:54 -0700387
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800388exit:
Hank Janssenfceaf242009-07-13 15:34:54 -0700389 return ret;
390}
391
Hank Janssenfceaf242009-07-13 15:34:54 -0700392
Haiyang Zhangf157e782011-12-15 13:45:16 -0800393/* Negotiate NVSP protocol version */
394static int negotiate_nvsp_ver(struct hv_device *device,
395 struct netvsc_device *net_device,
396 struct nvsp_message *init_packet,
397 u32 nvsp_ver)
Hank Janssenfceaf242009-07-13 15:34:54 -0700398{
K. Y. Srinivasan35abb212011-05-10 07:55:41 -0700399 int ret, t;
Haiyang Zhangf157e782011-12-15 13:45:16 -0800400
401 memset(init_packet, 0, sizeof(struct nvsp_message));
402 init_packet->hdr.msg_type = NVSP_MSG_TYPE_INIT;
403 init_packet->msg.init_msg.init.min_protocol_ver = nvsp_ver;
404 init_packet->msg.init_msg.init.max_protocol_ver = nvsp_ver;
405
406 /* Send the init request */
407 ret = vmbus_sendpacket(device->channel, init_packet,
408 sizeof(struct nvsp_message),
409 (unsigned long)init_packet,
410 VM_PKT_DATA_INBAND,
411 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
412
413 if (ret != 0)
414 return ret;
415
416 t = wait_for_completion_timeout(&net_device->channel_init_wait, 5*HZ);
417
418 if (t == 0)
419 return -ETIMEDOUT;
420
421 if (init_packet->msg.init_msg.init_complete.status !=
422 NVSP_STAT_SUCCESS)
423 return -EINVAL;
424
Haiyang Zhanga1eabb02014-02-19 15:49:45 -0800425 if (nvsp_ver == NVSP_PROTOCOL_VERSION_1)
Haiyang Zhangf157e782011-12-15 13:45:16 -0800426 return 0;
427
428 /* NVSPv2 only: Send NDIS config */
429 memset(init_packet, 0, sizeof(struct nvsp_message));
430 init_packet->hdr.msg_type = NVSP_MSG2_TYPE_SEND_NDIS_CONFIG;
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800431 init_packet->msg.v2_msg.send_ndis_config.mtu = net_device->ndev->mtu;
Haiyang Zhang1f5f3a72012-03-12 10:20:50 +0000432 init_packet->msg.v2_msg.send_ndis_config.capability.ieee8021q = 1;
Haiyang Zhangf157e782011-12-15 13:45:16 -0800433
434 ret = vmbus_sendpacket(device->channel, init_packet,
435 sizeof(struct nvsp_message),
436 (unsigned long)init_packet,
437 VM_PKT_DATA_INBAND, 0);
438
439 return ret;
440}
441
442static int netvsc_connect_vsp(struct hv_device *device)
443{
444 int ret;
Haiyang Zhang85799a32010-12-10 12:03:54 -0800445 struct netvsc_device *net_device;
446 struct nvsp_message *init_packet;
447 int ndis_version;
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -0700448 struct net_device *ndev;
Haiyang Zhanga1eabb02014-02-19 15:49:45 -0800449 u32 ver_list[] = { NVSP_PROTOCOL_VERSION_1, NVSP_PROTOCOL_VERSION_2,
450 NVSP_PROTOCOL_VERSION_4, NVSP_PROTOCOL_VERSION_5 };
451 int i, num_ver = 4; /* number of different NVSP versions */
Hank Janssenfceaf242009-07-13 15:34:54 -0700452
Haiyang Zhang5a71ae32010-12-10 12:03:55 -0800453 net_device = get_outbound_net_device(device);
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -0700454 if (!net_device)
K. Y. Srinivasan0f48c722011-08-25 09:49:14 -0700455 return -ENODEV;
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -0700456 ndev = net_device->ndev;
Hank Janssenfceaf242009-07-13 15:34:54 -0700457
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800458 init_packet = &net_device->channel_init_pkt;
Hank Janssenfceaf242009-07-13 15:34:54 -0700459
Haiyang Zhangf157e782011-12-15 13:45:16 -0800460 /* Negotiate the latest NVSP protocol supported */
Haiyang Zhanga1eabb02014-02-19 15:49:45 -0800461 for (i = num_ver - 1; i >= 0; i--)
462 if (negotiate_nvsp_ver(device, net_device, init_packet,
463 ver_list[i]) == 0) {
464 net_device->nvsp_version = ver_list[i];
465 break;
466 }
467
468 if (i < 0) {
K. Y. Srinivasan0f48c722011-08-25 09:49:14 -0700469 ret = -EPROTO;
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800470 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700471 }
Haiyang Zhangf157e782011-12-15 13:45:16 -0800472
473 pr_debug("Negotiated NVSP version:%x\n", net_device->nvsp_version);
474
Bill Pemberton454f18a2009-07-27 16:47:24 -0400475 /* Send the ndis version */
Haiyang Zhang85799a32010-12-10 12:03:54 -0800476 memset(init_packet, 0, sizeof(struct nvsp_message));
Hank Janssenfceaf242009-07-13 15:34:54 -0700477
Haiyang Zhanga1eabb02014-02-19 15:49:45 -0800478 if (net_device->nvsp_version <= NVSP_PROTOCOL_VERSION_4)
KY Srinivasan1f73db42014-04-09 15:00:46 -0700479 ndis_version = 0x00060001;
Haiyang Zhanga1eabb02014-02-19 15:49:45 -0800480 else
481 ndis_version = 0x0006001e;
Hank Janssenfceaf242009-07-13 15:34:54 -0700482
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800483 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_NDIS_VER;
484 init_packet->msg.v1_msg.
485 send_ndis_ver.ndis_major_ver =
Haiyang Zhang85799a32010-12-10 12:03:54 -0800486 (ndis_version & 0xFFFF0000) >> 16;
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800487 init_packet->msg.v1_msg.
488 send_ndis_ver.ndis_minor_ver =
Haiyang Zhang85799a32010-12-10 12:03:54 -0800489 ndis_version & 0xFFFF;
Hank Janssenfceaf242009-07-13 15:34:54 -0700490
Bill Pemberton454f18a2009-07-27 16:47:24 -0400491 /* Send the init request */
Haiyang Zhang85799a32010-12-10 12:03:54 -0800492 ret = vmbus_sendpacket(device->channel, init_packet,
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800493 sizeof(struct nvsp_message),
494 (unsigned long)init_packet,
495 VM_PKT_DATA_INBAND, 0);
K. Y. Srinivasan0f48c722011-08-25 09:49:14 -0700496 if (ret != 0)
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800497 goto cleanup;
Hank Janssenfceaf242009-07-13 15:34:54 -0700498
Bill Pemberton454f18a2009-07-27 16:47:24 -0400499 /* Post the big receive buffer to NetVSP */
Haiyang Zhang99d30162014-03-09 16:10:59 -0700500 if (net_device->nvsp_version <= NVSP_PROTOCOL_VERSION_2)
501 net_device->recv_buf_size = NETVSC_RECEIVE_BUFFER_SIZE_LEGACY;
502 else
503 net_device->recv_buf_size = NETVSC_RECEIVE_BUFFER_SIZE;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700504 net_device->send_buf_size = NETVSC_SEND_BUFFER_SIZE;
Haiyang Zhang99d30162014-03-09 16:10:59 -0700505
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700506 ret = netvsc_init_buf(device);
Hank Janssenfceaf242009-07-13 15:34:54 -0700507
K. Y. Srinivasan0c3b7b22011-02-11 09:59:43 -0800508cleanup:
Hank Janssenfceaf242009-07-13 15:34:54 -0700509 return ret;
510}
511
Haiyang Zhang648dc592011-04-21 12:30:47 -0700512static void netvsc_disconnect_vsp(struct netvsc_device *net_device)
Hank Janssenfceaf242009-07-13 15:34:54 -0700513{
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700514 netvsc_destroy_buf(net_device);
Hank Janssenfceaf242009-07-13 15:34:54 -0700515}
516
Hank Janssen3e189512010-03-04 22:11:00 +0000517/*
Haiyang Zhang5a71ae32010-12-10 12:03:55 -0800518 * netvsc_device_remove - Callback when the root bus device is removed
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700519 */
K. Y. Srinivasan905620d2011-05-10 07:54:54 -0700520int netvsc_device_remove(struct hv_device *device)
Hank Janssenfceaf242009-07-13 15:34:54 -0700521{
Haiyang Zhang85799a32010-12-10 12:03:54 -0800522 struct netvsc_device *net_device;
K. Y. Srinivasanc38b9c72011-08-27 11:31:12 -0700523 unsigned long flags;
Hank Janssenfceaf242009-07-13 15:34:54 -0700524
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -0700525 net_device = hv_get_drvdata(device);
Hank Janssenfceaf242009-07-13 15:34:54 -0700526
Haiyang Zhang648dc592011-04-21 12:30:47 -0700527 netvsc_disconnect_vsp(net_device);
Hank Janssenfceaf242009-07-13 15:34:54 -0700528
K. Y. Srinivasan38524092011-08-27 11:31:14 -0700529 /*
K. Y. Srinivasan9d88f332011-08-27 11:31:16 -0700530 * Since we have already drained, we don't need to busy wait
531 * as was done in final_release_stor_device()
532 * Note that we cannot set the ext pointer to NULL until
533 * we have drained - to drain the outgoing packets, we need to
534 * allow incoming packets.
K. Y. Srinivasan38524092011-08-27 11:31:14 -0700535 */
K. Y. Srinivasan9d88f332011-08-27 11:31:16 -0700536
537 spin_lock_irqsave(&device->channel->inbound_lock, flags);
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -0700538 hv_set_drvdata(device, NULL);
K. Y. Srinivasan9d88f332011-08-27 11:31:16 -0700539 spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
K. Y. Srinivasan38524092011-08-27 11:31:14 -0700540
K. Y. Srinivasan86c921a2011-09-13 10:59:54 -0700541 /*
542 * At this point, no one should be accessing net_device
543 * except in here
544 */
Haiyang Zhangc909ebb2011-09-01 12:19:40 -0700545 dev_notice(&device->device, "net device safe to remove\n");
Hank Janssenfceaf242009-07-13 15:34:54 -0700546
Bill Pemberton454f18a2009-07-27 16:47:24 -0400547 /* Now, we can close the channel safely */
Haiyang Zhang85799a32010-12-10 12:03:54 -0800548 vmbus_close(device->channel);
Hank Janssenfceaf242009-07-13 15:34:54 -0700549
Bill Pemberton454f18a2009-07-27 16:47:24 -0400550 /* Release all resources */
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700551 if (net_device->sub_cb_buf)
552 vfree(net_device->sub_cb_buf);
553
K. Y. Srinivasan356c4652011-08-27 11:31:10 -0700554 kfree(net_device);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700555 return 0;
Hank Janssenfceaf242009-07-13 15:34:54 -0700556}
557
Haiyang Zhang33be96e2012-03-27 13:20:45 +0000558
559#define RING_AVAIL_PERCENT_HIWATER 20
560#define RING_AVAIL_PERCENT_LOWATER 10
561
562/*
563 * Get the percentage of available bytes to write in the ring.
564 * The return value is in range from 0 to 100.
565 */
566static inline u32 hv_ringbuf_avail_percent(
567 struct hv_ring_buffer_info *ring_info)
568{
569 u32 avail_read, avail_write;
570
571 hv_get_ringbuffer_availbytes(ring_info, &avail_read, &avail_write);
572
573 return avail_write * 100 / ring_info->ring_datasize;
574}
575
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700576static inline void netvsc_free_send_slot(struct netvsc_device *net_device,
577 u32 index)
578{
579 sync_change_bit(index, net_device->send_section_map);
580}
581
KY Srinivasan97c17232014-02-16 16:38:44 -0800582static void netvsc_send_completion(struct netvsc_device *net_device,
583 struct hv_device *device,
Haiyang Zhang85799a32010-12-10 12:03:54 -0800584 struct vmpacket_descriptor *packet)
Hank Janssenfceaf242009-07-13 15:34:54 -0700585{
Haiyang Zhang85799a32010-12-10 12:03:54 -0800586 struct nvsp_message *nvsp_packet;
587 struct hv_netvsc_packet *nvsc_packet;
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -0700588 struct net_device *ndev;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700589 u32 send_index;
Hank Janssenfceaf242009-07-13 15:34:54 -0700590
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -0700591 ndev = net_device->ndev;
Hank Janssenfceaf242009-07-13 15:34:54 -0700592
Haiyang Zhang85799a32010-12-10 12:03:54 -0800593 nvsp_packet = (struct nvsp_message *)((unsigned long)packet +
Haiyang Zhang415f2282011-01-26 12:12:13 -0800594 (packet->offset8 << 3));
Hank Janssenfceaf242009-07-13 15:34:54 -0700595
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800596 if ((nvsp_packet->hdr.msg_type == NVSP_MSG_TYPE_INIT_COMPLETE) ||
597 (nvsp_packet->hdr.msg_type ==
598 NVSP_MSG1_TYPE_SEND_RECV_BUF_COMPLETE) ||
599 (nvsp_packet->hdr.msg_type ==
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700600 NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE) ||
601 (nvsp_packet->hdr.msg_type ==
602 NVSP_MSG5_TYPE_SUBCHANNEL)) {
Bill Pemberton454f18a2009-07-27 16:47:24 -0400603 /* Copy the response back */
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800604 memcpy(&net_device->channel_init_pkt, nvsp_packet,
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700605 sizeof(struct nvsp_message));
K. Y. Srinivasan35abb212011-05-10 07:55:41 -0700606 complete(&net_device->channel_init_wait);
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800607 } else if (nvsp_packet->hdr.msg_type ==
608 NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE) {
Haiyang Zhang33be96e2012-03-27 13:20:45 +0000609 int num_outstanding_sends;
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700610 u16 q_idx = 0;
611 struct vmbus_channel *channel = device->channel;
612 int queue_sends;
Haiyang Zhang33be96e2012-03-27 13:20:45 +0000613
Bill Pemberton454f18a2009-07-27 16:47:24 -0400614 /* Get the send context */
Haiyang Zhang85799a32010-12-10 12:03:54 -0800615 nvsc_packet = (struct hv_netvsc_packet *)(unsigned long)
Haiyang Zhang415f2282011-01-26 12:12:13 -0800616 packet->trans_id;
Hank Janssenfceaf242009-07-13 15:34:54 -0700617
Bill Pemberton454f18a2009-07-27 16:47:24 -0400618 /* Notify the layer above us */
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700619 if (nvsc_packet) {
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700620 send_index = nvsc_packet->send_buf_index;
621 if (send_index != NETVSC_INVALID_INDEX)
622 netvsc_free_send_slot(net_device, send_index);
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700623 q_idx = nvsc_packet->q_idx;
624 channel = nvsc_packet->channel;
Haiyang Zhang893f6622014-04-21 14:54:44 -0700625 nvsc_packet->send_completion(nvsc_packet->
626 send_completion_ctx);
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700627 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700628
Haiyang Zhang33be96e2012-03-27 13:20:45 +0000629 num_outstanding_sends =
630 atomic_dec_return(&net_device->num_outstanding_sends);
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700631 queue_sends = atomic_dec_return(&net_device->
632 queue_sends[q_idx]);
Haiyang Zhang1d068252011-12-02 11:56:25 -0800633
Haiyang Zhangdc5cd892012-06-04 06:42:38 +0000634 if (net_device->destroy && num_outstanding_sends == 0)
635 wake_up(&net_device->wait_drain);
636
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700637 if (netif_tx_queue_stopped(netdev_get_tx_queue(ndev, q_idx)) &&
638 !net_device->start_remove &&
639 (hv_ringbuf_avail_percent(&channel->outbound) >
640 RING_AVAIL_PERCENT_HIWATER || queue_sends < 1))
641 netif_tx_wake_queue(netdev_get_tx_queue(
642 ndev, q_idx));
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700643 } else {
Haiyang Zhangd9871152011-09-01 12:19:41 -0700644 netdev_err(ndev, "Unknown send completion packet type- "
Haiyang Zhangc909ebb2011-09-01 12:19:40 -0700645 "%d received!!\n", nvsp_packet->hdr.msg_type);
Hank Janssenfceaf242009-07-13 15:34:54 -0700646 }
647
Hank Janssenfceaf242009-07-13 15:34:54 -0700648}
649
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700650static u32 netvsc_get_next_send_section(struct netvsc_device *net_device)
651{
652 unsigned long index;
653 u32 max_words = net_device->map_words;
654 unsigned long *map_addr = (unsigned long *)net_device->send_section_map;
655 u32 section_cnt = net_device->send_section_cnt;
656 int ret_val = NETVSC_INVALID_INDEX;
657 int i;
658 int prev_val;
659
660 for (i = 0; i < max_words; i++) {
661 if (!~(map_addr[i]))
662 continue;
663 index = ffz(map_addr[i]);
664 prev_val = sync_test_and_set_bit(index, &map_addr[i]);
665 if (prev_val)
666 continue;
667 if ((index + (i * BITS_PER_LONG)) >= section_cnt)
668 break;
669 ret_val = (index + (i * BITS_PER_LONG));
670 break;
671 }
672 return ret_val;
673}
674
675u32 netvsc_copy_to_send_buf(struct netvsc_device *net_device,
676 unsigned int section_index,
677 struct hv_netvsc_packet *packet)
678{
679 char *start = net_device->send_buf;
680 char *dest = (start + (section_index * net_device->send_section_size));
681 int i;
682 u32 msg_size = 0;
683
684 for (i = 0; i < packet->page_buf_cnt; i++) {
685 char *src = phys_to_virt(packet->page_buf[i].pfn << PAGE_SHIFT);
686 u32 offset = packet->page_buf[i].offset;
687 u32 len = packet->page_buf[i].len;
688
689 memcpy(dest, (src + offset), len);
690 msg_size += len;
691 dest += len;
692 }
693 return msg_size;
694}
695
K. Y. Srinivasanf9819f02011-05-12 19:34:49 -0700696int netvsc_send(struct hv_device *device,
Haiyang Zhang85799a32010-12-10 12:03:54 -0800697 struct hv_netvsc_packet *packet)
Hank Janssenfceaf242009-07-13 15:34:54 -0700698{
Haiyang Zhang85799a32010-12-10 12:03:54 -0800699 struct netvsc_device *net_device;
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700700 int ret = 0;
Greg Kroah-Hartman223c1aa2009-08-28 16:20:53 -0700701 struct nvsp_message sendMessage;
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -0700702 struct net_device *ndev;
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700703 struct vmbus_channel *out_channel = NULL;
Haiyang Zhangf1ea3cd2013-04-05 11:44:40 +0000704 u64 req_id;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700705 unsigned int section_index = NETVSC_INVALID_INDEX;
706 u32 msg_size = 0;
707 struct sk_buff *skb;
KY Srinivasan3a67c9c2014-10-05 10:42:51 -0700708 u16 q_idx = packet->q_idx;
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700709
Hank Janssenfceaf242009-07-13 15:34:54 -0700710
Haiyang Zhang5a71ae32010-12-10 12:03:55 -0800711 net_device = get_outbound_net_device(device);
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -0700712 if (!net_device)
K. Y. Srinivasanff2bd692011-08-25 09:49:15 -0700713 return -ENODEV;
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -0700714 ndev = net_device->ndev;
Hank Janssenfceaf242009-07-13 15:34:54 -0700715
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800716 sendMessage.hdr.msg_type = NVSP_MSG1_TYPE_SEND_RNDIS_PKT;
Haiyang Zhang72a2f5b2010-12-10 12:03:58 -0800717 if (packet->is_data_pkt) {
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700718 /* 0 is RMC_DATA; */
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800719 sendMessage.msg.v1_msg.send_rndis_pkt.channel_type = 0;
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700720 } else {
721 /* 1 is RMC_CONTROL; */
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800722 sendMessage.msg.v1_msg.send_rndis_pkt.channel_type = 1;
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700723 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700724
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700725 /* Attempt to send via sendbuf */
726 if (packet->total_data_buflen < net_device->send_section_size) {
727 section_index = netvsc_get_next_send_section(net_device);
728 if (section_index != NETVSC_INVALID_INDEX) {
729 msg_size = netvsc_copy_to_send_buf(net_device,
730 section_index,
731 packet);
732 skb = (struct sk_buff *)
733 (unsigned long)packet->send_completion_tid;
734 if (skb)
735 dev_kfree_skb_any(skb);
736 packet->page_buf_cnt = 0;
737 }
738 }
739 packet->send_buf_index = section_index;
740
741
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800742 sendMessage.msg.v1_msg.send_rndis_pkt.send_buf_section_index =
KY Srinivasanc25aaf82014-04-30 10:14:31 -0700743 section_index;
744 sendMessage.msg.v1_msg.send_rndis_pkt.send_buf_section_size = msg_size;
Hank Janssenfceaf242009-07-13 15:34:54 -0700745
Haiyang Zhang893f6622014-04-21 14:54:44 -0700746 if (packet->send_completion)
Haiyang Zhang00ca8f02013-04-26 08:25:55 +0000747 req_id = (ulong)packet;
Haiyang Zhangf1ea3cd2013-04-05 11:44:40 +0000748 else
749 req_id = 0;
750
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700751 out_channel = net_device->chn_table[packet->q_idx];
752 if (out_channel == NULL)
753 out_channel = device->channel;
754 packet->channel = out_channel;
755
Haiyang Zhang72a2f5b2010-12-10 12:03:58 -0800756 if (packet->page_buf_cnt) {
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700757 ret = vmbus_sendpacket_pagebuffer(out_channel,
Haiyang Zhang72a2f5b2010-12-10 12:03:58 -0800758 packet->page_buf,
759 packet->page_buf_cnt,
Greg Kroah-Hartmanff3f8ee2010-10-21 09:32:46 -0700760 &sendMessage,
761 sizeof(struct nvsp_message),
Haiyang Zhangf1ea3cd2013-04-05 11:44:40 +0000762 req_id);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700763 } else {
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700764 ret = vmbus_sendpacket(out_channel, &sendMessage,
Haiyang Zhange4d59ac2011-06-17 07:58:04 -0700765 sizeof(struct nvsp_message),
Haiyang Zhangf1ea3cd2013-04-05 11:44:40 +0000766 req_id,
Haiyang Zhange4d59ac2011-06-17 07:58:04 -0700767 VM_PKT_DATA_INBAND,
768 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
Hank Janssenfceaf242009-07-13 15:34:54 -0700769 }
770
Haiyang Zhang1d068252011-12-02 11:56:25 -0800771 if (ret == 0) {
772 atomic_inc(&net_device->num_outstanding_sends);
KY Srinivasan3a67c9c2014-10-05 10:42:51 -0700773 atomic_inc(&net_device->queue_sends[q_idx]);
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700774
775 if (hv_ringbuf_avail_percent(&out_channel->outbound) <
Haiyang Zhang33be96e2012-03-27 13:20:45 +0000776 RING_AVAIL_PERCENT_LOWATER) {
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700777 netif_tx_stop_queue(netdev_get_tx_queue(
KY Srinivasan3a67c9c2014-10-05 10:42:51 -0700778 ndev, q_idx));
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700779
Haiyang Zhang33be96e2012-03-27 13:20:45 +0000780 if (atomic_read(&net_device->
KY Srinivasan3a67c9c2014-10-05 10:42:51 -0700781 queue_sends[q_idx]) < 1)
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700782 netif_tx_wake_queue(netdev_get_tx_queue(
KY Srinivasan3a67c9c2014-10-05 10:42:51 -0700783 ndev, q_idx));
Haiyang Zhang33be96e2012-03-27 13:20:45 +0000784 }
Haiyang Zhang1d068252011-12-02 11:56:25 -0800785 } else if (ret == -EAGAIN) {
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700786 netif_tx_stop_queue(netdev_get_tx_queue(
KY Srinivasan3a67c9c2014-10-05 10:42:51 -0700787 ndev, q_idx));
788 if (atomic_read(&net_device->queue_sends[q_idx]) < 1) {
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700789 netif_tx_wake_queue(netdev_get_tx_queue(
KY Srinivasan3a67c9c2014-10-05 10:42:51 -0700790 ndev, q_idx));
Haiyang Zhang33be96e2012-03-27 13:20:45 +0000791 ret = -ENOSPC;
792 }
Haiyang Zhang1d068252011-12-02 11:56:25 -0800793 } else {
Haiyang Zhangd9871152011-09-01 12:19:41 -0700794 netdev_err(ndev, "Unable to send packet %p ret %d\n",
Haiyang Zhang85799a32010-12-10 12:03:54 -0800795 packet, ret);
Haiyang Zhang1d068252011-12-02 11:56:25 -0800796 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700797
Hank Janssenfceaf242009-07-13 15:34:54 -0700798 return ret;
799}
800
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -0700801static void netvsc_send_recv_completion(struct hv_device *device,
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700802 struct vmbus_channel *channel,
KY Srinivasan97c17232014-02-16 16:38:44 -0800803 struct netvsc_device *net_device,
Haiyang Zhang63f69212012-10-02 05:30:23 +0000804 u64 transaction_id, u32 status)
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -0700805{
806 struct nvsp_message recvcompMessage;
807 int retries = 0;
808 int ret;
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -0700809 struct net_device *ndev;
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -0700810
811 ndev = net_device->ndev;
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -0700812
813 recvcompMessage.hdr.msg_type =
814 NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE;
815
Haiyang Zhang63f69212012-10-02 05:30:23 +0000816 recvcompMessage.msg.v1_msg.send_rndis_pkt_complete.status = status;
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -0700817
818retry_send_cmplt:
819 /* Send the completion */
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700820 ret = vmbus_sendpacket(channel, &recvcompMessage,
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -0700821 sizeof(struct nvsp_message), transaction_id,
822 VM_PKT_COMP, 0);
823 if (ret == 0) {
824 /* success */
825 /* no-op */
K. Y. Srinivasand2598f02011-08-25 09:48:58 -0700826 } else if (ret == -EAGAIN) {
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -0700827 /* no more room...wait a bit and attempt to retry 3 times */
828 retries++;
Haiyang Zhangd9871152011-09-01 12:19:41 -0700829 netdev_err(ndev, "unable to send receive completion pkt"
Haiyang Zhangc909ebb2011-09-01 12:19:40 -0700830 " (tid %llx)...retrying %d\n", transaction_id, retries);
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -0700831
832 if (retries < 4) {
833 udelay(100);
834 goto retry_send_cmplt;
835 } else {
Haiyang Zhangd9871152011-09-01 12:19:41 -0700836 netdev_err(ndev, "unable to send receive "
Haiyang Zhangc909ebb2011-09-01 12:19:40 -0700837 "completion pkt (tid %llx)...give up retrying\n",
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -0700838 transaction_id);
839 }
840 } else {
Haiyang Zhangd9871152011-09-01 12:19:41 -0700841 netdev_err(ndev, "unable to send receive "
Haiyang Zhangc909ebb2011-09-01 12:19:40 -0700842 "completion pkt - %llx\n", transaction_id);
Haiyang Zhang5fa9d3c2011-04-21 12:30:42 -0700843 }
844}
845
KY Srinivasan97c17232014-02-16 16:38:44 -0800846static void netvsc_receive(struct netvsc_device *net_device,
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700847 struct vmbus_channel *channel,
KY Srinivasan97c17232014-02-16 16:38:44 -0800848 struct hv_device *device,
849 struct vmpacket_descriptor *packet)
Hank Janssenfceaf242009-07-13 15:34:54 -0700850{
Haiyang Zhang85799a32010-12-10 12:03:54 -0800851 struct vmtransfer_page_packet_header *vmxferpage_packet;
852 struct nvsp_message *nvsp_packet;
Haiyang Zhang4baab262014-04-21 14:54:43 -0700853 struct hv_netvsc_packet nv_pkt;
854 struct hv_netvsc_packet *netvsc_packet = &nv_pkt;
855 u32 status = NVSP_STAT_SUCCESS;
Haiyang Zhang45326342011-12-15 13:45:15 -0800856 int i;
857 int count = 0;
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -0700858 struct net_device *ndev;
K. Y. Srinivasan779b4d12011-04-26 09:20:22 -0700859
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -0700860 ndev = net_device->ndev;
Hank Janssenfceaf242009-07-13 15:34:54 -0700861
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700862 /*
863 * All inbound packets other than send completion should be xfer page
864 * packet
865 */
Haiyang Zhang415f2282011-01-26 12:12:13 -0800866 if (packet->type != VM_PKT_DATA_USING_XFER_PAGES) {
Haiyang Zhangd9871152011-09-01 12:19:41 -0700867 netdev_err(ndev, "Unknown packet type received - %d\n",
Haiyang Zhang415f2282011-01-26 12:12:13 -0800868 packet->type);
Hank Janssenfceaf242009-07-13 15:34:54 -0700869 return;
870 }
871
Haiyang Zhang85799a32010-12-10 12:03:54 -0800872 nvsp_packet = (struct nvsp_message *)((unsigned long)packet +
Haiyang Zhang415f2282011-01-26 12:12:13 -0800873 (packet->offset8 << 3));
Hank Janssenfceaf242009-07-13 15:34:54 -0700874
Bill Pemberton454f18a2009-07-27 16:47:24 -0400875 /* Make sure this is a valid nvsp packet */
Haiyang Zhang53d21fd2010-12-10 12:03:59 -0800876 if (nvsp_packet->hdr.msg_type !=
877 NVSP_MSG1_TYPE_SEND_RNDIS_PKT) {
Haiyang Zhangd9871152011-09-01 12:19:41 -0700878 netdev_err(ndev, "Unknown nvsp packet type received-"
Haiyang Zhangc909ebb2011-09-01 12:19:40 -0700879 " %d\n", nvsp_packet->hdr.msg_type);
Hank Janssenfceaf242009-07-13 15:34:54 -0700880 return;
881 }
882
Haiyang Zhang85799a32010-12-10 12:03:54 -0800883 vmxferpage_packet = (struct vmtransfer_page_packet_header *)packet;
Hank Janssenfceaf242009-07-13 15:34:54 -0700884
Haiyang Zhang415f2282011-01-26 12:12:13 -0800885 if (vmxferpage_packet->xfer_pageset_id != NETVSC_RECEIVE_BUFFER_ID) {
Haiyang Zhangd9871152011-09-01 12:19:41 -0700886 netdev_err(ndev, "Invalid xfer page set id - "
Haiyang Zhangc909ebb2011-09-01 12:19:40 -0700887 "expecting %x got %x\n", NETVSC_RECEIVE_BUFFER_ID,
Haiyang Zhang415f2282011-01-26 12:12:13 -0800888 vmxferpage_packet->xfer_pageset_id);
Hank Janssenfceaf242009-07-13 15:34:54 -0700889 return;
890 }
891
Haiyang Zhang4baab262014-04-21 14:54:43 -0700892 count = vmxferpage_packet->range_cnt;
893 netvsc_packet->device = device;
894 netvsc_packet->channel = channel;
Hank Janssenfceaf242009-07-13 15:34:54 -0700895
Bill Pemberton454f18a2009-07-27 16:47:24 -0400896 /* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */
Haiyang Zhang4baab262014-04-21 14:54:43 -0700897 for (i = 0; i < count; i++) {
Bill Pemberton454f18a2009-07-27 16:47:24 -0400898 /* Initialize the netvsc packet */
Haiyang Zhang63f69212012-10-02 05:30:23 +0000899 netvsc_packet->status = NVSP_STAT_SUCCESS;
Haiyang Zhang45326342011-12-15 13:45:15 -0800900 netvsc_packet->data = (void *)((unsigned long)net_device->
901 recv_buf + vmxferpage_packet->ranges[i].byte_offset);
Haiyang Zhang72a2f5b2010-12-10 12:03:58 -0800902 netvsc_packet->total_data_buflen =
Haiyang Zhang415f2282011-01-26 12:12:13 -0800903 vmxferpage_packet->ranges[i].byte_count;
Hank Janssenfceaf242009-07-13 15:34:54 -0700904
Bill Pemberton454f18a2009-07-27 16:47:24 -0400905 /* Pass it to the upper layer */
K. Y. Srinivasanac6f7852011-05-12 19:34:58 -0700906 rndis_filter_receive(device, netvsc_packet);
Hank Janssenfceaf242009-07-13 15:34:54 -0700907
Haiyang Zhang4baab262014-04-21 14:54:43 -0700908 if (netvsc_packet->status != NVSP_STAT_SUCCESS)
909 status = NVSP_STAT_FAIL;
Hank Janssenfceaf242009-07-13 15:34:54 -0700910 }
911
Haiyang Zhang4baab262014-04-21 14:54:43 -0700912 netvsc_send_recv_completion(device, channel, net_device,
913 vmxferpage_packet->d.trans_id, status);
Hank Janssenfceaf242009-07-13 15:34:54 -0700914}
915
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700916
917static void netvsc_send_table(struct hv_device *hdev,
918 struct vmpacket_descriptor *vmpkt)
919{
920 struct netvsc_device *nvscdev;
921 struct net_device *ndev;
922 struct nvsp_message *nvmsg;
923 int i;
924 u32 count, *tab;
925
926 nvscdev = get_outbound_net_device(hdev);
927 if (!nvscdev)
928 return;
929 ndev = nvscdev->ndev;
930
931 nvmsg = (struct nvsp_message *)((unsigned long)vmpkt +
932 (vmpkt->offset8 << 3));
933
934 if (nvmsg->hdr.msg_type != NVSP_MSG5_TYPE_SEND_INDIRECTION_TABLE)
935 return;
936
937 count = nvmsg->msg.v5_msg.send_table.count;
938 if (count != VRSS_SEND_TAB_SIZE) {
939 netdev_err(ndev, "Received wrong send-table size:%u\n", count);
940 return;
941 }
942
943 tab = (u32 *)((unsigned long)&nvmsg->msg.v5_msg.send_table +
944 nvmsg->msg.v5_msg.send_table.offset);
945
946 for (i = 0; i < count; i++)
947 nvscdev->send_table[i] = tab[i];
948}
949
950void netvsc_channel_cb(void *context)
Hank Janssenfceaf242009-07-13 15:34:54 -0700951{
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700952 int ret;
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700953 struct vmbus_channel *channel = (struct vmbus_channel *)context;
954 struct hv_device *device;
Haiyang Zhang85799a32010-12-10 12:03:54 -0800955 struct netvsc_device *net_device;
956 u32 bytes_recvd;
957 u64 request_id;
Greg Kroah-Hartman8dc0a062009-08-27 16:02:36 -0700958 struct vmpacket_descriptor *desc;
Bill Pembertonc6fcf0b2010-04-27 16:23:47 -0400959 unsigned char *buffer;
960 int bufferlen = NETVSC_PACKET_SIZE;
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -0700961 struct net_device *ndev;
Hank Janssenfceaf242009-07-13 15:34:54 -0700962
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700963 if (channel->primary_channel != NULL)
964 device = channel->primary_channel->device_obj;
965 else
966 device = channel->device_obj;
967
Haiyang Zhang5a71ae32010-12-10 12:03:55 -0800968 net_device = get_inbound_net_device(device);
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -0700969 if (!net_device)
KY Srinivasanee0c4c32014-02-16 16:38:45 -0800970 return;
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -0700971 ndev = net_device->ndev;
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700972 buffer = get_per_channel_state(channel);
Hank Janssenfceaf242009-07-13 15:34:54 -0700973
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700974 do {
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700975 ret = vmbus_recvpacket_raw(channel, buffer, bufferlen,
Haiyang Zhang85799a32010-12-10 12:03:54 -0800976 &bytes_recvd, &request_id);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700977 if (ret == 0) {
Haiyang Zhang85799a32010-12-10 12:03:54 -0800978 if (bytes_recvd > 0) {
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700979 desc = (struct vmpacket_descriptor *)buffer;
Haiyang Zhang415f2282011-01-26 12:12:13 -0800980 switch (desc->type) {
981 case VM_PKT_COMP:
KY Srinivasan97c17232014-02-16 16:38:44 -0800982 netvsc_send_completion(net_device,
983 device, desc);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700984 break;
Hank Janssenfceaf242009-07-13 15:34:54 -0700985
Haiyang Zhang415f2282011-01-26 12:12:13 -0800986 case VM_PKT_DATA_USING_XFER_PAGES:
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700987 netvsc_receive(net_device, channel,
988 device, desc);
989 break;
990
991 case VM_PKT_DATA_INBAND:
992 netvsc_send_table(device, desc);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700993 break;
Hank Janssenfceaf242009-07-13 15:34:54 -0700994
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700995 default:
Haiyang Zhangd9871152011-09-01 12:19:41 -0700996 netdev_err(ndev,
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -0700997 "unhandled packet type %d, "
998 "tid %llx len %d\n",
Haiyang Zhang415f2282011-01-26 12:12:13 -0800999 desc->type, request_id,
Haiyang Zhang85799a32010-12-10 12:03:54 -08001000 bytes_recvd);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001001 break;
Hank Janssenfceaf242009-07-13 15:34:54 -07001002 }
1003
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001004 } else {
KY Srinivasanee0c4c32014-02-16 16:38:45 -08001005 /*
1006 * We are done for this pass.
1007 */
Hank Janssenfceaf242009-07-13 15:34:54 -07001008 break;
1009 }
KY Srinivasanee0c4c32014-02-16 16:38:45 -08001010
K. Y. Srinivasan3d5cad92011-08-25 09:48:59 -07001011 } else if (ret == -ENOBUFS) {
KY Srinivasanee0c4c32014-02-16 16:38:45 -08001012 if (bufferlen > NETVSC_PACKET_SIZE)
1013 kfree(buffer);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001014 /* Handle large packet */
Haiyang Zhang85799a32010-12-10 12:03:54 -08001015 buffer = kmalloc(bytes_recvd, GFP_ATOMIC);
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001016 if (buffer == NULL) {
Bill Pemberton454f18a2009-07-27 16:47:24 -04001017 /* Try again next time around */
Haiyang Zhangd9871152011-09-01 12:19:41 -07001018 netdev_err(ndev,
Greg Kroah-Hartman21a808202009-09-02 10:33:05 -07001019 "unable to allocate buffer of size "
Haiyang Zhangc909ebb2011-09-01 12:19:40 -07001020 "(%d)!!\n", bytes_recvd);
Hank Janssenfceaf242009-07-13 15:34:54 -07001021 break;
1022 }
1023
Haiyang Zhang85799a32010-12-10 12:03:54 -08001024 bufferlen = bytes_recvd;
Hank Janssenfceaf242009-07-13 15:34:54 -07001025 }
1026 } while (1);
1027
KY Srinivasanee0c4c32014-02-16 16:38:45 -08001028 if (bufferlen > NETVSC_PACKET_SIZE)
1029 kfree(buffer);
Hank Janssenfceaf242009-07-13 15:34:54 -07001030 return;
1031}
Haiyang Zhangaf24ce42011-04-21 12:30:40 -07001032
1033/*
Haiyang Zhangb637e022011-04-21 12:30:45 -07001034 * netvsc_device_add - Callback when the device belonging to this
1035 * driver is added
1036 */
K. Y. Srinivasan7bd23a42011-05-10 07:54:53 -07001037int netvsc_device_add(struct hv_device *device, void *additional_info)
Haiyang Zhangb637e022011-04-21 12:30:45 -07001038{
1039 int ret = 0;
K. Y. Srinivasanaae23982011-05-12 19:35:05 -07001040 int ring_size =
1041 ((struct netvsc_device_info *)additional_info)->ring_size;
Haiyang Zhangb637e022011-04-21 12:30:45 -07001042 struct netvsc_device *net_device;
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -07001043 struct net_device *ndev;
Haiyang Zhangb637e022011-04-21 12:30:45 -07001044
1045 net_device = alloc_net_device(device);
1046 if (!net_device) {
K. Y. Srinivasanace163a2011-08-25 09:49:16 -07001047 ret = -ENOMEM;
Haiyang Zhangb637e022011-04-21 12:30:45 -07001048 goto cleanup;
1049 }
1050
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001051 net_device->ring_size = ring_size;
1052
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -07001053 /*
1054 * Coming into this function, struct net_device * is
1055 * registered as the driver private data.
1056 * In alloc_net_device(), we register struct netvsc_device *
1057 * as the driver private data and stash away struct net_device *
1058 * in struct netvsc_device *.
1059 */
1060 ndev = net_device->ndev;
1061
Haiyang Zhangb637e022011-04-21 12:30:45 -07001062 /* Initialize the NetVSC channel extension */
K. Y. Srinivasan35abb212011-05-10 07:55:41 -07001063 init_completion(&net_device->channel_init_wait);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001064
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001065 set_per_channel_state(device->channel, net_device->cb_buffer);
1066
Haiyang Zhangb637e022011-04-21 12:30:45 -07001067 /* Open the channel */
K. Y. Srinivasanaae23982011-05-12 19:35:05 -07001068 ret = vmbus_open(device->channel, ring_size * PAGE_SIZE,
1069 ring_size * PAGE_SIZE, NULL, 0,
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001070 netvsc_channel_cb, device->channel);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001071
1072 if (ret != 0) {
Haiyang Zhangd9871152011-09-01 12:19:41 -07001073 netdev_err(ndev, "unable to open channel: %d\n", ret);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001074 goto cleanup;
1075 }
1076
1077 /* Channel is opened */
Haiyang Zhangc909ebb2011-09-01 12:19:40 -07001078 pr_info("hv_netvsc channel opened successfully\n");
Haiyang Zhangb637e022011-04-21 12:30:45 -07001079
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001080 net_device->chn_table[0] = device->channel;
1081
Haiyang Zhangb637e022011-04-21 12:30:45 -07001082 /* Connect with the NetVsp */
1083 ret = netvsc_connect_vsp(device);
1084 if (ret != 0) {
Haiyang Zhangd9871152011-09-01 12:19:41 -07001085 netdev_err(ndev,
Haiyang Zhangc909ebb2011-09-01 12:19:40 -07001086 "unable to connect to NetVSP - %d\n", ret);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001087 goto close;
1088 }
1089
1090 return ret;
1091
1092close:
1093 /* Now, we can close the channel safely */
1094 vmbus_close(device->channel);
1095
1096cleanup:
Fabian Frederickbd4578bc2014-06-28 20:44:19 +02001097 kfree(net_device);
Haiyang Zhangb637e022011-04-21 12:30:45 -07001098
1099 return ret;
1100}