Staging: hv: netvsc_drv: Add ring_size element to struct netvsc_device_info
[linux-2.6-block.git] / drivers / staging / hv / netvsc.c
CommitLineData
fceaf24a 1/*
fceaf24a
HJ
2 * Copyright (c) 2009, Microsoft Corporation.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 * Place - Suite 330, Boston, MA 02111-1307 USA.
16 *
17 * Authors:
d0e94d17 18 * Haiyang Zhang <haiyangz@microsoft.com>
fceaf24a 19 * Hank Janssen <hjanssen@microsoft.com>
fceaf24a 20 */
eb335bc4
HJ
21#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
5654e932 23#include <linux/kernel.h>
0c3b7b2f
S
24#include <linux/sched.h>
25#include <linux/wait.h>
0ffa63b0 26#include <linux/mm.h>
b4362c9c 27#include <linux/delay.h>
21a80820 28#include <linux/io.h>
5a0e3ad6 29#include <linux/slab.h>
3f335ea2
S
30
31#include "hyperv.h"
5ca7252a 32#include "hyperv_net.h"
fceaf24a
HJ
33
34
454f18a9 35/* Globals */
85799a37 36static const char *driver_name = "netvsc";
fceaf24a 37
454f18a9 38/* {F8615163-DF3E-46c5-913F-F2D2F965ED0E} */
85799a37 39static const struct hv_guid netvsc_device_type = {
caf26a31
GKH
40 .data = {
41 0x63, 0x51, 0x61, 0xF8, 0x3E, 0xDF, 0xc5, 0x46,
42 0x91, 0x3F, 0xF2, 0xD2, 0xF9, 0x65, 0xED, 0x0E
43 }
fceaf24a
HJ
44};
45
46
5a71ae30 47static struct netvsc_device *alloc_net_device(struct hv_device *device)
fceaf24a 48{
85799a37 49 struct netvsc_device *net_device;
fceaf24a 50
85799a37
HZ
51 net_device = kzalloc(sizeof(struct netvsc_device), GFP_KERNEL);
52 if (!net_device)
fceaf24a
HJ
53 return NULL;
54
454f18a9 55 /* Set to 2 to allow both inbound and outbound traffic */
53d21fdb 56 atomic_cmpxchg(&net_device->refcnt, 0, 2);
fceaf24a 57
53d21fdb 58 net_device->dev = device;
ca623ad3 59 device->ext = net_device;
fceaf24a 60
85799a37 61 return net_device;
fceaf24a
HJ
62}
63
5a71ae30 64static void free_net_device(struct netvsc_device *device)
fceaf24a 65{
31acaa50 66 WARN_ON(atomic_read(&device->refcnt) != 0);
ca623ad3 67 device->dev->ext = NULL;
85799a37 68 kfree(device);
fceaf24a
HJ
69}
70
71
454f18a9 72/* Get the net device object iff exists and its refcount > 1 */
5a71ae30 73static struct netvsc_device *get_outbound_net_device(struct hv_device *device)
fceaf24a 74{
85799a37 75 struct netvsc_device *net_device;
fceaf24a 76
ca623ad3 77 net_device = device->ext;
53d21fdb
HZ
78 if (net_device && atomic_read(&net_device->refcnt) > 1)
79 atomic_inc(&net_device->refcnt);
fceaf24a 80 else
85799a37 81 net_device = NULL;
fceaf24a 82
85799a37 83 return net_device;
fceaf24a
HJ
84}
85
454f18a9 86/* Get the net device object iff exists and its refcount > 0 */
5a71ae30 87static struct netvsc_device *get_inbound_net_device(struct hv_device *device)
fceaf24a 88{
85799a37 89 struct netvsc_device *net_device;
fceaf24a 90
ca623ad3 91 net_device = device->ext;
53d21fdb
HZ
92 if (net_device && atomic_read(&net_device->refcnt))
93 atomic_inc(&net_device->refcnt);
fceaf24a 94 else
85799a37 95 net_device = NULL;
fceaf24a 96
85799a37 97 return net_device;
fceaf24a
HJ
98}
99
5a71ae30 100static void put_net_device(struct hv_device *device)
fceaf24a 101{
85799a37 102 struct netvsc_device *net_device;
fceaf24a 103
ca623ad3 104 net_device = device->ext;
fceaf24a 105
53d21fdb 106 atomic_dec(&net_device->refcnt);
fceaf24a
HJ
107}
108
5a71ae30
HZ
109static struct netvsc_device *release_outbound_net_device(
110 struct hv_device *device)
fceaf24a 111{
85799a37 112 struct netvsc_device *net_device;
fceaf24a 113
ca623ad3 114 net_device = device->ext;
85799a37 115 if (net_device == NULL)
fceaf24a
HJ
116 return NULL;
117
454f18a9 118 /* Busy wait until the ref drop to 2, then set it to 1 */
53d21fdb 119 while (atomic_cmpxchg(&net_device->refcnt, 2, 1) != 2)
b4362c9c 120 udelay(100);
fceaf24a 121
85799a37 122 return net_device;
fceaf24a
HJ
123}
124
5a71ae30
HZ
125static struct netvsc_device *release_inbound_net_device(
126 struct hv_device *device)
fceaf24a 127{
85799a37 128 struct netvsc_device *net_device;
fceaf24a 129
ca623ad3 130 net_device = device->ext;
85799a37 131 if (net_device == NULL)
fceaf24a
HJ
132 return NULL;
133
454f18a9 134 /* Busy wait until the ref drop to 1, then set it to 0 */
53d21fdb 135 while (atomic_cmpxchg(&net_device->refcnt, 1, 0) != 1)
b4362c9c 136 udelay(100);
fceaf24a 137
ca623ad3 138 device->ext = NULL;
85799a37 139 return net_device;
fceaf24a
HJ
140}
141
ec91cd09
HZ
142static int netvsc_destroy_recv_buf(struct netvsc_device *net_device)
143{
144 struct nvsp_message *revoke_packet;
145 int ret = 0;
146
147 /*
148 * If we got a section count, it means we received a
149 * SendReceiveBufferComplete msg (ie sent
150 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
151 * to send a revoke msg here
152 */
153 if (net_device->recv_section_cnt) {
154 /* Send the revoke receive buffer */
155 revoke_packet = &net_device->revoke_packet;
156 memset(revoke_packet, 0, sizeof(struct nvsp_message));
157
158 revoke_packet->hdr.msg_type =
159 NVSP_MSG1_TYPE_REVOKE_RECV_BUF;
160 revoke_packet->msg.v1_msg.
161 revoke_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
162
163 ret = vmbus_sendpacket(net_device->dev->channel,
164 revoke_packet,
165 sizeof(struct nvsp_message),
166 (unsigned long)revoke_packet,
167 VM_PKT_DATA_INBAND, 0);
168 /*
169 * If we failed here, we might as well return and
170 * have a leak rather than continue and a bugchk
171 */
172 if (ret != 0) {
173 dev_err(&net_device->dev->device, "unable to send "
174 "revoke receive buffer to netvsp");
175 return -1;
176 }
177 }
178
179 /* Teardown the gpadl on the vsp end */
180 if (net_device->recv_buf_gpadl_handle) {
181 ret = vmbus_teardown_gpadl(net_device->dev->channel,
182 net_device->recv_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 dev_err(&net_device->dev->device,
189 "unable to teardown receive buffer's gpadl");
190 return -1;
191 }
192 net_device->recv_buf_gpadl_handle = 0;
193 }
194
195 if (net_device->recv_buf) {
196 /* Free up the receive buffer */
197 free_pages((unsigned long)net_device->recv_buf,
198 get_order(net_device->recv_buf_size));
199 net_device->recv_buf = NULL;
200 }
201
202 if (net_device->recv_section) {
203 net_device->recv_section_cnt = 0;
204 kfree(net_device->recv_section);
205 net_device->recv_section = NULL;
206 }
207
208 return ret;
209}
210
5a71ae30 211static int netvsc_init_recv_buf(struct hv_device *device)
fceaf24a 212{
21a80820 213 int ret = 0;
35abb21a 214 int t;
85799a37
HZ
215 struct netvsc_device *net_device;
216 struct nvsp_message *init_packet;
fceaf24a 217
5a71ae30 218 net_device = get_outbound_net_device(device);
85799a37 219 if (!net_device) {
eb335bc4 220 dev_err(&device->device, "unable to get net device..."
21a80820 221 "device being destroyed?");
fceaf24a
HJ
222 return -1;
223 }
fceaf24a 224
53d21fdb 225 net_device->recv_buf =
df3493e0
S
226 (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO,
227 get_order(net_device->recv_buf_size));
53d21fdb 228 if (!net_device->recv_buf) {
eb335bc4
HJ
229 dev_err(&device->device, "unable to allocate receive "
230 "buffer of size %d", net_device->recv_buf_size);
fceaf24a 231 ret = -1;
0c3b7b2f 232 goto cleanup;
fceaf24a 233 }
fceaf24a 234
454f18a9
BP
235 /*
236 * Establish the gpadl handle for this buffer on this
237 * channel. Note: This call uses the vmbus connection rather
238 * than the channel to establish the gpadl handle.
239 */
53d21fdb
HZ
240 ret = vmbus_establish_gpadl(device->channel, net_device->recv_buf,
241 net_device->recv_buf_size,
242 &net_device->recv_buf_gpadl_handle);
21a80820 243 if (ret != 0) {
eb335bc4
HJ
244 dev_err(&device->device,
245 "unable to establish receive buffer's gpadl");
0c3b7b2f 246 goto cleanup;
fceaf24a
HJ
247 }
248
fceaf24a 249
454f18a9 250 /* Notify the NetVsp of the gpadl handle */
53d21fdb 251 init_packet = &net_device->channel_init_pkt;
fceaf24a 252
85799a37 253 memset(init_packet, 0, sizeof(struct nvsp_message));
fceaf24a 254
53d21fdb
HZ
255 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_RECV_BUF;
256 init_packet->msg.v1_msg.send_recv_buf.
257 gpadl_handle = net_device->recv_buf_gpadl_handle;
258 init_packet->msg.v1_msg.
259 send_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
fceaf24a 260
454f18a9 261 /* Send the gpadl notification request */
85799a37 262 ret = vmbus_sendpacket(device->channel, init_packet,
5a4df290 263 sizeof(struct nvsp_message),
85799a37 264 (unsigned long)init_packet,
415f2287 265 VM_PKT_DATA_INBAND,
5a4df290 266 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
21a80820 267 if (ret != 0) {
eb335bc4
HJ
268 dev_err(&device->device,
269 "unable to send receive buffer's gpadl to netvsp");
0c3b7b2f 270 goto cleanup;
fceaf24a
HJ
271 }
272
35abb21a
S
273 t = wait_for_completion_timeout(&net_device->channel_init_wait, HZ);
274 BUG_ON(t == 0);
0c3b7b2f 275
fceaf24a 276
454f18a9 277 /* Check the response */
53d21fdb
HZ
278 if (init_packet->msg.v1_msg.
279 send_recv_buf_complete.status != NVSP_STAT_SUCCESS) {
eb335bc4 280 dev_err(&device->device, "Unable to complete receive buffer "
21a80820 281 "initialzation with NetVsp - status %d",
53d21fdb
HZ
282 init_packet->msg.v1_msg.
283 send_recv_buf_complete.status);
fceaf24a 284 ret = -1;
0c3b7b2f 285 goto cleanup;
fceaf24a
HJ
286 }
287
454f18a9 288 /* Parse the response */
fceaf24a 289
53d21fdb
HZ
290 net_device->recv_section_cnt = init_packet->msg.
291 v1_msg.send_recv_buf_complete.num_sections;
fceaf24a 292
53d21fdb 293 net_device->recv_section = kmalloc(net_device->recv_section_cnt
85799a37 294 * sizeof(struct nvsp_1_receive_buffer_section), GFP_KERNEL);
53d21fdb 295 if (net_device->recv_section == NULL) {
fceaf24a 296 ret = -1;
0c3b7b2f 297 goto cleanup;
fceaf24a
HJ
298 }
299
53d21fdb
HZ
300 memcpy(net_device->recv_section,
301 init_packet->msg.v1_msg.
302 send_recv_buf_complete.sections,
303 net_device->recv_section_cnt *
85799a37 304 sizeof(struct nvsp_1_receive_buffer_section));
fceaf24a 305
21a80820
GKH
306 /*
307 * For 1st release, there should only be 1 section that represents the
308 * entire receive buffer
309 */
53d21fdb
HZ
310 if (net_device->recv_section_cnt != 1 ||
311 net_device->recv_section->offset != 0) {
fceaf24a 312 ret = -1;
0c3b7b2f 313 goto cleanup;
fceaf24a
HJ
314 }
315
0c3b7b2f 316 goto exit;
fceaf24a 317
0c3b7b2f 318cleanup:
5a71ae30 319 netvsc_destroy_recv_buf(net_device);
fceaf24a 320
0c3b7b2f 321exit:
5a71ae30 322 put_net_device(device);
fceaf24a
HJ
323 return ret;
324}
325
0021e71e
HZ
326static int netvsc_destroy_send_buf(struct netvsc_device *net_device)
327{
328 struct nvsp_message *revoke_packet;
329 int ret = 0;
330
331 /*
332 * If we got a section count, it means we received a
333 * SendReceiveBufferComplete msg (ie sent
334 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
335 * to send a revoke msg here
336 */
337 if (net_device->send_section_size) {
338 /* Send the revoke send buffer */
339 revoke_packet = &net_device->revoke_packet;
340 memset(revoke_packet, 0, sizeof(struct nvsp_message));
341
342 revoke_packet->hdr.msg_type =
343 NVSP_MSG1_TYPE_REVOKE_SEND_BUF;
344 revoke_packet->msg.v1_msg.
345 revoke_send_buf.id = NETVSC_SEND_BUFFER_ID;
346
347 ret = vmbus_sendpacket(net_device->dev->channel,
348 revoke_packet,
349 sizeof(struct nvsp_message),
350 (unsigned long)revoke_packet,
351 VM_PKT_DATA_INBAND, 0);
352 /*
353 * If we failed here, we might as well return and have a leak
354 * rather than continue and a bugchk
355 */
356 if (ret != 0) {
357 dev_err(&net_device->dev->device, "unable to send "
358 "revoke send buffer to netvsp");
359 return -1;
360 }
361 }
362
363 /* Teardown the gpadl on the vsp end */
364 if (net_device->send_buf_gpadl_handle) {
365 ret = vmbus_teardown_gpadl(net_device->dev->channel,
366 net_device->send_buf_gpadl_handle);
367
368 /*
369 * If we failed here, we might as well return and have a leak
370 * rather than continue and a bugchk
371 */
372 if (ret != 0) {
373 dev_err(&net_device->dev->device,
374 "unable to teardown send buffer's gpadl");
375 return -1;
376 }
377 net_device->send_buf_gpadl_handle = 0;
378 }
379
380 if (net_device->send_buf) {
381 /* Free up the receive buffer */
382 free_pages((unsigned long)net_device->send_buf,
383 get_order(net_device->send_buf_size));
384 net_device->send_buf = NULL;
385 }
386
387 return ret;
388}
389
5a71ae30 390static int netvsc_init_send_buf(struct hv_device *device)
fceaf24a 391{
21a80820 392 int ret = 0;
35abb21a 393 int t;
85799a37
HZ
394 struct netvsc_device *net_device;
395 struct nvsp_message *init_packet;
fceaf24a 396
5a71ae30 397 net_device = get_outbound_net_device(device);
85799a37 398 if (!net_device) {
eb335bc4 399 dev_err(&device->device, "unable to get net device..."
21a80820 400 "device being destroyed?");
fceaf24a
HJ
401 return -1;
402 }
53d21fdb 403 if (net_device->send_buf_size <= 0) {
79069684 404 ret = -EINVAL;
0c3b7b2f 405 goto cleanup;
79069684
BP
406 }
407
53d21fdb 408 net_device->send_buf =
df3493e0
S
409 (void *)__get_free_pages(GFP_KERNEL|__GFP_ZERO,
410 get_order(net_device->send_buf_size));
53d21fdb 411 if (!net_device->send_buf) {
eb335bc4
HJ
412 dev_err(&device->device, "unable to allocate send "
413 "buffer of size %d", net_device->send_buf_size);
fceaf24a 414 ret = -1;
0c3b7b2f 415 goto cleanup;
fceaf24a 416 }
fceaf24a 417
454f18a9
BP
418 /*
419 * Establish the gpadl handle for this buffer on this
420 * channel. Note: This call uses the vmbus connection rather
421 * than the channel to establish the gpadl handle.
422 */
53d21fdb
HZ
423 ret = vmbus_establish_gpadl(device->channel, net_device->send_buf,
424 net_device->send_buf_size,
425 &net_device->send_buf_gpadl_handle);
21a80820 426 if (ret != 0) {
eb335bc4 427 dev_err(&device->device, "unable to establish send buffer's gpadl");
0c3b7b2f 428 goto cleanup;
fceaf24a
HJ
429 }
430
454f18a9 431 /* Notify the NetVsp of the gpadl handle */
53d21fdb 432 init_packet = &net_device->channel_init_pkt;
fceaf24a 433
85799a37 434 memset(init_packet, 0, sizeof(struct nvsp_message));
fceaf24a 435
53d21fdb
HZ
436 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_SEND_BUF;
437 init_packet->msg.v1_msg.send_recv_buf.
438 gpadl_handle = net_device->send_buf_gpadl_handle;
439 init_packet->msg.v1_msg.send_recv_buf.id =
85799a37 440 NETVSC_SEND_BUFFER_ID;
fceaf24a 441
454f18a9 442 /* Send the gpadl notification request */
85799a37 443 ret = vmbus_sendpacket(device->channel, init_packet,
5a4df290 444 sizeof(struct nvsp_message),
85799a37 445 (unsigned long)init_packet,
415f2287 446 VM_PKT_DATA_INBAND,
5a4df290 447 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
21a80820 448 if (ret != 0) {
eb335bc4 449 dev_err(&device->device,
21a80820 450 "unable to send receive buffer's gpadl to netvsp");
0c3b7b2f 451 goto cleanup;
fceaf24a
HJ
452 }
453
35abb21a
S
454 t = wait_for_completion_timeout(&net_device->channel_init_wait, HZ);
455
456 BUG_ON(t == 0);
fceaf24a 457
454f18a9 458 /* Check the response */
53d21fdb
HZ
459 if (init_packet->msg.v1_msg.
460 send_send_buf_complete.status != NVSP_STAT_SUCCESS) {
eb335bc4 461 dev_err(&device->device, "Unable to complete send buffer "
21a80820 462 "initialzation with NetVsp - status %d",
53d21fdb
HZ
463 init_packet->msg.v1_msg.
464 send_send_buf_complete.status);
fceaf24a 465 ret = -1;
0c3b7b2f 466 goto cleanup;
fceaf24a
HJ
467 }
468
53d21fdb
HZ
469 net_device->send_section_size = init_packet->
470 msg.v1_msg.send_send_buf_complete.section_size;
fceaf24a 471
0c3b7b2f 472 goto exit;
fceaf24a 473
0c3b7b2f 474cleanup:
5a71ae30 475 netvsc_destroy_send_buf(net_device);
fceaf24a 476
0c3b7b2f 477exit:
5a71ae30 478 put_net_device(device);
fceaf24a
HJ
479 return ret;
480}
481
fceaf24a 482
5a71ae30 483static int netvsc_connect_vsp(struct hv_device *device)
fceaf24a 484{
35abb21a 485 int ret, t;
85799a37
HZ
486 struct netvsc_device *net_device;
487 struct nvsp_message *init_packet;
488 int ndis_version;
fceaf24a 489
5a71ae30 490 net_device = get_outbound_net_device(device);
85799a37 491 if (!net_device) {
eb335bc4 492 dev_err(&device->device, "unable to get net device..."
21a80820 493 "device being destroyed?");
fceaf24a
HJ
494 return -1;
495 }
496
53d21fdb 497 init_packet = &net_device->channel_init_pkt;
fceaf24a 498
85799a37 499 memset(init_packet, 0, sizeof(struct nvsp_message));
53d21fdb
HZ
500 init_packet->hdr.msg_type = NVSP_MSG_TYPE_INIT;
501 init_packet->msg.init_msg.init.min_protocol_ver =
85799a37 502 NVSP_MIN_PROTOCOL_VERSION;
53d21fdb 503 init_packet->msg.init_msg.init.max_protocol_ver =
85799a37 504 NVSP_MAX_PROTOCOL_VERSION;
fceaf24a 505
454f18a9 506 /* Send the init request */
85799a37 507 ret = vmbus_sendpacket(device->channel, init_packet,
5a4df290 508 sizeof(struct nvsp_message),
85799a37 509 (unsigned long)init_packet,
415f2287 510 VM_PKT_DATA_INBAND,
5a4df290 511 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
21a80820 512
b8a3d52b 513 if (ret != 0)
0c3b7b2f 514 goto cleanup;
fceaf24a 515
35abb21a
S
516 t = wait_for_completion_timeout(&net_device->channel_init_wait, HZ);
517
518 if (t == 0) {
0c3b7b2f
S
519 ret = -ETIMEDOUT;
520 goto cleanup;
521 }
fceaf24a 522
53d21fdb
HZ
523 if (init_packet->msg.init_msg.init_complete.status !=
524 NVSP_STAT_SUCCESS) {
fceaf24a 525 ret = -1;
0c3b7b2f 526 goto cleanup;
fceaf24a
HJ
527 }
528
53d21fdb
HZ
529 if (init_packet->msg.init_msg.init_complete.
530 negotiated_protocol_ver != NVSP_PROTOCOL_VERSION_1) {
fceaf24a 531 ret = -1;
0c3b7b2f 532 goto cleanup;
fceaf24a 533 }
454f18a9 534 /* Send the ndis version */
85799a37 535 memset(init_packet, 0, sizeof(struct nvsp_message));
fceaf24a 536
85799a37 537 ndis_version = 0x00050000;
fceaf24a 538
53d21fdb
HZ
539 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_NDIS_VER;
540 init_packet->msg.v1_msg.
541 send_ndis_ver.ndis_major_ver =
85799a37 542 (ndis_version & 0xFFFF0000) >> 16;
53d21fdb
HZ
543 init_packet->msg.v1_msg.
544 send_ndis_ver.ndis_minor_ver =
85799a37 545 ndis_version & 0xFFFF;
fceaf24a 546
454f18a9 547 /* Send the init request */
85799a37 548 ret = vmbus_sendpacket(device->channel, init_packet,
0c3b7b2f
S
549 sizeof(struct nvsp_message),
550 (unsigned long)init_packet,
551 VM_PKT_DATA_INBAND, 0);
21a80820 552 if (ret != 0) {
fceaf24a 553 ret = -1;
0c3b7b2f 554 goto cleanup;
fceaf24a 555 }
454f18a9
BP
556
557 /* Post the big receive buffer to NetVSP */
5a71ae30 558 ret = netvsc_init_recv_buf(device);
fceaf24a 559 if (ret == 0)
5a71ae30 560 ret = netvsc_init_send_buf(device);
fceaf24a 561
0c3b7b2f 562cleanup:
5a71ae30 563 put_net_device(device);
fceaf24a
HJ
564 return ret;
565}
566
648dc598 567static void netvsc_disconnect_vsp(struct netvsc_device *net_device)
fceaf24a 568{
5a71ae30
HZ
569 netvsc_destroy_recv_buf(net_device);
570 netvsc_destroy_send_buf(net_device);
fceaf24a
HJ
571}
572
3e189519 573/*
5a71ae30 574 * netvsc_device_remove - Callback when the root bus device is removed
21a80820 575 */
905620d1 576int netvsc_device_remove(struct hv_device *device)
fceaf24a 577{
85799a37
HZ
578 struct netvsc_device *net_device;
579 struct hv_netvsc_packet *netvsc_packet, *pos;
fceaf24a 580
454f18a9 581 /* Stop outbound traffic ie sends and receives completions */
5a71ae30 582 net_device = release_outbound_net_device(device);
85799a37 583 if (!net_device) {
eb335bc4 584 dev_err(&device->device, "No net device present!!");
fceaf24a
HJ
585 return -1;
586 }
587
454f18a9 588 /* Wait for all send completions */
53d21fdb 589 while (atomic_read(&net_device->num_outstanding_sends)) {
eb335bc4
HJ
590 dev_err(&device->device,
591 "waiting for %d requests to complete...",
592 atomic_read(&net_device->num_outstanding_sends));
b4362c9c 593 udelay(100);
fceaf24a
HJ
594 }
595
648dc598 596 netvsc_disconnect_vsp(net_device);
fceaf24a 597
454f18a9 598 /* Stop inbound traffic ie receives and sends completions */
5a71ae30 599 net_device = release_inbound_net_device(device);
fceaf24a 600
454f18a9 601 /* At this point, no one should be accessing netDevice except in here */
eb335bc4 602 dev_notice(&device->device, "net device safe to remove");
fceaf24a 603
454f18a9 604 /* Now, we can close the channel safely */
85799a37 605 vmbus_close(device->channel);
fceaf24a 606
454f18a9 607 /* Release all resources */
85799a37 608 list_for_each_entry_safe(netvsc_packet, pos,
53d21fdb 609 &net_device->recv_pkt_list, list_ent) {
72a2f5bd 610 list_del(&netvsc_packet->list_ent);
85799a37 611 kfree(netvsc_packet);
fceaf24a
HJ
612 }
613
5a71ae30 614 free_net_device(net_device);
21a80820 615 return 0;
fceaf24a
HJ
616}
617
5a71ae30 618static void netvsc_send_completion(struct hv_device *device,
85799a37 619 struct vmpacket_descriptor *packet)
fceaf24a 620{
85799a37
HZ
621 struct netvsc_device *net_device;
622 struct nvsp_message *nvsp_packet;
623 struct hv_netvsc_packet *nvsc_packet;
fceaf24a 624
5a71ae30 625 net_device = get_inbound_net_device(device);
85799a37 626 if (!net_device) {
eb335bc4 627 dev_err(&device->device, "unable to get net device..."
21a80820 628 "device being destroyed?");
fceaf24a
HJ
629 return;
630 }
631
85799a37 632 nvsp_packet = (struct nvsp_message *)((unsigned long)packet +
415f2287 633 (packet->offset8 << 3));
fceaf24a 634
53d21fdb
HZ
635 if ((nvsp_packet->hdr.msg_type == NVSP_MSG_TYPE_INIT_COMPLETE) ||
636 (nvsp_packet->hdr.msg_type ==
637 NVSP_MSG1_TYPE_SEND_RECV_BUF_COMPLETE) ||
638 (nvsp_packet->hdr.msg_type ==
639 NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE)) {
454f18a9 640 /* Copy the response back */
53d21fdb 641 memcpy(&net_device->channel_init_pkt, nvsp_packet,
21a80820 642 sizeof(struct nvsp_message));
35abb21a 643 complete(&net_device->channel_init_wait);
53d21fdb
HZ
644 } else if (nvsp_packet->hdr.msg_type ==
645 NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE) {
454f18a9 646 /* Get the send context */
85799a37 647 nvsc_packet = (struct hv_netvsc_packet *)(unsigned long)
415f2287 648 packet->trans_id;
fceaf24a 649
454f18a9 650 /* Notify the layer above us */
72a2f5bd
HZ
651 nvsc_packet->completion.send.send_completion(
652 nvsc_packet->completion.send.send_completion_ctx);
fceaf24a 653
53d21fdb 654 atomic_dec(&net_device->num_outstanding_sends);
21a80820 655 } else {
eb335bc4 656 dev_err(&device->device, "Unknown send completion packet type- "
53d21fdb 657 "%d received!!", nvsp_packet->hdr.msg_type);
fceaf24a
HJ
658 }
659
5a71ae30 660 put_net_device(device);
fceaf24a
HJ
661}
662
f9819f05 663int netvsc_send(struct hv_device *device,
85799a37 664 struct hv_netvsc_packet *packet)
fceaf24a 665{
85799a37 666 struct netvsc_device *net_device;
21a80820 667 int ret = 0;
fceaf24a 668
223c1aa6 669 struct nvsp_message sendMessage;
fceaf24a 670
5a71ae30 671 net_device = get_outbound_net_device(device);
85799a37 672 if (!net_device) {
eb335bc4 673 dev_err(&device->device, "net device (%p) shutting down..."
85799a37 674 "ignoring outbound packets", net_device);
fceaf24a
HJ
675 return -2;
676 }
677
53d21fdb 678 sendMessage.hdr.msg_type = NVSP_MSG1_TYPE_SEND_RNDIS_PKT;
72a2f5bd 679 if (packet->is_data_pkt) {
21a80820 680 /* 0 is RMC_DATA; */
53d21fdb 681 sendMessage.msg.v1_msg.send_rndis_pkt.channel_type = 0;
21a80820
GKH
682 } else {
683 /* 1 is RMC_CONTROL; */
53d21fdb 684 sendMessage.msg.v1_msg.send_rndis_pkt.channel_type = 1;
21a80820 685 }
fceaf24a 686
454f18a9 687 /* Not using send buffer section */
53d21fdb
HZ
688 sendMessage.msg.v1_msg.send_rndis_pkt.send_buf_section_index =
689 0xFFFFFFFF;
690 sendMessage.msg.v1_msg.send_rndis_pkt.send_buf_section_size = 0;
21a80820 691
72a2f5bd 692 if (packet->page_buf_cnt) {
85799a37 693 ret = vmbus_sendpacket_pagebuffer(device->channel,
72a2f5bd
HZ
694 packet->page_buf,
695 packet->page_buf_cnt,
ff3f8eec
GKH
696 &sendMessage,
697 sizeof(struct nvsp_message),
85799a37 698 (unsigned long)packet);
21a80820 699 } else {
85799a37 700 ret = vmbus_sendpacket(device->channel, &sendMessage,
5a4df290 701 sizeof(struct nvsp_message),
85799a37 702 (unsigned long)packet,
415f2287 703 VM_PKT_DATA_INBAND,
5a4df290 704 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
fceaf24a
HJ
705
706 }
707
708 if (ret != 0)
eb335bc4 709 dev_err(&device->device, "Unable to send packet %p ret %d",
85799a37 710 packet, ret);
fceaf24a 711
53d21fdb 712 atomic_inc(&net_device->num_outstanding_sends);
5a71ae30 713 put_net_device(device);
fceaf24a
HJ
714 return ret;
715}
716
5fa9d3c5
HZ
717static void netvsc_send_recv_completion(struct hv_device *device,
718 u64 transaction_id)
719{
720 struct nvsp_message recvcompMessage;
721 int retries = 0;
722 int ret;
723
724 recvcompMessage.hdr.msg_type =
725 NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE;
726
727 /* FIXME: Pass in the status */
728 recvcompMessage.msg.v1_msg.send_rndis_pkt_complete.status =
729 NVSP_STAT_SUCCESS;
730
731retry_send_cmplt:
732 /* Send the completion */
733 ret = vmbus_sendpacket(device->channel, &recvcompMessage,
734 sizeof(struct nvsp_message), transaction_id,
735 VM_PKT_COMP, 0);
736 if (ret == 0) {
737 /* success */
738 /* no-op */
739 } else if (ret == -1) {
740 /* no more room...wait a bit and attempt to retry 3 times */
741 retries++;
742 dev_err(&device->device, "unable to send receive completion pkt"
743 " (tid %llx)...retrying %d", transaction_id, retries);
744
745 if (retries < 4) {
746 udelay(100);
747 goto retry_send_cmplt;
748 } else {
749 dev_err(&device->device, "unable to send receive "
750 "completion pkt (tid %llx)...give up retrying",
751 transaction_id);
752 }
753 } else {
754 dev_err(&device->device, "unable to send receive "
755 "completion pkt - %llx", transaction_id);
756 }
757}
758
57991156
HZ
759/* Send a receive completion packet to RNDIS device (ie NetVsp) */
760static void netvsc_receive_completion(void *context)
761{
762 struct hv_netvsc_packet *packet = context;
763 struct hv_device *device = (struct hv_device *)packet->device;
764 struct netvsc_device *net_device;
765 u64 transaction_id = 0;
766 bool fsend_receive_comp = false;
767 unsigned long flags;
768
769 /*
770 * Even though it seems logical to do a GetOutboundNetDevice() here to
771 * send out receive completion, we are using GetInboundNetDevice()
772 * since we may have disable outbound traffic already.
773 */
774 net_device = get_inbound_net_device(device);
775 if (!net_device) {
776 dev_err(&device->device, "unable to get net device..."
777 "device being destroyed?");
778 return;
779 }
780
781 /* Overloading use of the lock. */
782 spin_lock_irqsave(&net_device->recv_pkt_list_lock, flags);
783
784 packet->xfer_page_pkt->count--;
785
786 /*
787 * Last one in the line that represent 1 xfer page packet.
788 * Return the xfer page packet itself to the freelist
789 */
790 if (packet->xfer_page_pkt->count == 0) {
791 fsend_receive_comp = true;
792 transaction_id = packet->completion.recv.recv_completion_tid;
793 list_add_tail(&packet->xfer_page_pkt->list_ent,
794 &net_device->recv_pkt_list);
795
796 }
797
798 /* Put the packet back */
799 list_add_tail(&packet->list_ent, &net_device->recv_pkt_list);
800 spin_unlock_irqrestore(&net_device->recv_pkt_list_lock, flags);
801
802 /* Send a receive completion for the xfer page packet */
803 if (fsend_receive_comp)
804 netvsc_send_recv_completion(device, transaction_id);
805
806 put_net_device(device);
807}
808
5a71ae30 809static void netvsc_receive(struct hv_device *device,
85799a37 810 struct vmpacket_descriptor *packet)
fceaf24a 811{
85799a37
HZ
812 struct netvsc_device *net_device;
813 struct vmtransfer_page_packet_header *vmxferpage_packet;
814 struct nvsp_message *nvsp_packet;
815 struct hv_netvsc_packet *netvsc_packet = NULL;
c4b0bc94 816 unsigned long start;
85799a37 817 unsigned long end, end_virtual;
7e23a6e9 818 /* struct netvsc_driver *netvscDriver; */
85799a37 819 struct xferpage_packet *xferpage_packet = NULL;
21a80820 820 int i, j;
85799a37 821 int count = 0, bytes_remain = 0;
6436873a 822 unsigned long flags;
779b4d17 823
d29274ef 824 LIST_HEAD(listHead);
fceaf24a 825
5a71ae30 826 net_device = get_inbound_net_device(device);
85799a37 827 if (!net_device) {
eb335bc4 828 dev_err(&device->device, "unable to get net device..."
21a80820 829 "device being destroyed?");
fceaf24a
HJ
830 return;
831 }
832
21a80820
GKH
833 /*
834 * All inbound packets other than send completion should be xfer page
835 * packet
836 */
415f2287 837 if (packet->type != VM_PKT_DATA_USING_XFER_PAGES) {
eb335bc4 838 dev_err(&device->device, "Unknown packet type received - %d",
415f2287 839 packet->type);
5a71ae30 840 put_net_device(device);
fceaf24a
HJ
841 return;
842 }
843
85799a37 844 nvsp_packet = (struct nvsp_message *)((unsigned long)packet +
415f2287 845 (packet->offset8 << 3));
fceaf24a 846
454f18a9 847 /* Make sure this is a valid nvsp packet */
53d21fdb
HZ
848 if (nvsp_packet->hdr.msg_type !=
849 NVSP_MSG1_TYPE_SEND_RNDIS_PKT) {
eb335bc4
HJ
850 dev_err(&device->device, "Unknown nvsp packet type received-"
851 " %d", nvsp_packet->hdr.msg_type);
5a71ae30 852 put_net_device(device);
fceaf24a
HJ
853 return;
854 }
855
85799a37 856 vmxferpage_packet = (struct vmtransfer_page_packet_header *)packet;
fceaf24a 857
415f2287 858 if (vmxferpage_packet->xfer_pageset_id != NETVSC_RECEIVE_BUFFER_ID) {
eb335bc4 859 dev_err(&device->device, "Invalid xfer page set id - "
21a80820 860 "expecting %x got %x", NETVSC_RECEIVE_BUFFER_ID,
415f2287 861 vmxferpage_packet->xfer_pageset_id);
5a71ae30 862 put_net_device(device);
fceaf24a
HJ
863 return;
864 }
865
454f18a9
BP
866 /*
867 * Grab free packets (range count + 1) to represent this xfer
868 * page packet. +1 to represent the xfer page packet itself.
869 * We grab it here so that we know exactly how many we can
870 * fulfil
871 */
53d21fdb
HZ
872 spin_lock_irqsave(&net_device->recv_pkt_list_lock, flags);
873 while (!list_empty(&net_device->recv_pkt_list)) {
874 list_move_tail(net_device->recv_pkt_list.next, &listHead);
415f2287 875 if (++count == vmxferpage_packet->range_cnt + 1)
fceaf24a
HJ
876 break;
877 }
53d21fdb 878 spin_unlock_irqrestore(&net_device->recv_pkt_list_lock, flags);
fceaf24a 879
454f18a9
BP
880 /*
881 * We need at least 2 netvsc pkts (1 to represent the xfer
882 * page and at least 1 for the range) i.e. we can handled
883 * some of the xfer page packet ranges...
884 */
21a80820 885 if (count < 2) {
eb335bc4
HJ
886 dev_err(&device->device, "Got only %d netvsc pkt...needed "
887 "%d pkts. Dropping this xfer page packet completely!",
888 count, vmxferpage_packet->range_cnt + 1);
fceaf24a 889
454f18a9 890 /* Return it to the freelist */
53d21fdb 891 spin_lock_irqsave(&net_device->recv_pkt_list_lock, flags);
21a80820 892 for (i = count; i != 0; i--) {
92ec0893 893 list_move_tail(listHead.next,
53d21fdb 894 &net_device->recv_pkt_list);
fceaf24a 895 }
53d21fdb 896 spin_unlock_irqrestore(&net_device->recv_pkt_list_lock,
21a80820 897 flags);
fceaf24a 898
5a71ae30 899 netvsc_send_recv_completion(device,
415f2287 900 vmxferpage_packet->d.trans_id);
fceaf24a 901
5a71ae30 902 put_net_device(device);
fceaf24a
HJ
903 return;
904 }
905
454f18a9 906 /* Remove the 1st packet to represent the xfer page packet itself */
85799a37 907 xferpage_packet = (struct xferpage_packet *)listHead.next;
72a2f5bd 908 list_del(&xferpage_packet->list_ent);
d29274ef 909
21a80820 910 /* This is how much we can satisfy */
72a2f5bd 911 xferpage_packet->count = count - 1;
21a80820 912
415f2287 913 if (xferpage_packet->count != vmxferpage_packet->range_cnt) {
eb335bc4
HJ
914 dev_err(&device->device, "Needed %d netvsc pkts to satisy "
915 "this xfer page...got %d",
916 vmxferpage_packet->range_cnt, xferpage_packet->count);
fceaf24a
HJ
917 }
918
454f18a9 919 /* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */
21a80820 920 for (i = 0; i < (count - 1); i++) {
85799a37 921 netvsc_packet = (struct hv_netvsc_packet *)listHead.next;
72a2f5bd 922 list_del(&netvsc_packet->list_ent);
fceaf24a 923
454f18a9 924 /* Initialize the netvsc packet */
72a2f5bd
HZ
925 netvsc_packet->xfer_page_pkt = xferpage_packet;
926 netvsc_packet->completion.recv.recv_completion =
5a71ae30 927 netvsc_receive_completion;
72a2f5bd 928 netvsc_packet->completion.recv.recv_completion_ctx =
85799a37 929 netvsc_packet;
72a2f5bd 930 netvsc_packet->device = device;
21a80820 931 /* Save this so that we can send it back */
72a2f5bd 932 netvsc_packet->completion.recv.recv_completion_tid =
415f2287 933 vmxferpage_packet->d.trans_id;
fceaf24a 934
72a2f5bd 935 netvsc_packet->total_data_buflen =
415f2287 936 vmxferpage_packet->ranges[i].byte_count;
72a2f5bd 937 netvsc_packet->page_buf_cnt = 1;
fceaf24a 938
ca623ad3 939 netvsc_packet->page_buf[0].len =
415f2287 940 vmxferpage_packet->ranges[i].byte_count;
fceaf24a 941
85799a37 942 start = virt_to_phys((void *)((unsigned long)net_device->
415f2287 943 recv_buf + vmxferpage_packet->ranges[i].byte_offset));
fceaf24a 944
ca623ad3 945 netvsc_packet->page_buf[0].pfn = start >> PAGE_SHIFT;
53d21fdb 946 end_virtual = (unsigned long)net_device->recv_buf
415f2287
HZ
947 + vmxferpage_packet->ranges[i].byte_offset
948 + vmxferpage_packet->ranges[i].byte_count - 1;
85799a37 949 end = virt_to_phys((void *)end_virtual);
fceaf24a 950
454f18a9 951 /* Calculate the page relative offset */
ca623ad3 952 netvsc_packet->page_buf[0].offset =
415f2287 953 vmxferpage_packet->ranges[i].byte_offset &
85799a37 954 (PAGE_SIZE - 1);
21a80820
GKH
955 if ((end >> PAGE_SHIFT) != (start >> PAGE_SHIFT)) {
956 /* Handle frame across multiple pages: */
ca623ad3
HZ
957 netvsc_packet->page_buf[0].len =
958 (netvsc_packet->page_buf[0].pfn <<
85799a37 959 PAGE_SHIFT)
21a80820 960 + PAGE_SIZE - start;
72a2f5bd 961 bytes_remain = netvsc_packet->total_data_buflen -
ca623ad3 962 netvsc_packet->page_buf[0].len;
21a80820 963 for (j = 1; j < NETVSC_PACKET_MAXPAGE; j++) {
ca623ad3 964 netvsc_packet->page_buf[j].offset = 0;
85799a37 965 if (bytes_remain <= PAGE_SIZE) {
ca623ad3 966 netvsc_packet->page_buf[j].len =
85799a37
HZ
967 bytes_remain;
968 bytes_remain = 0;
21a80820 969 } else {
ca623ad3 970 netvsc_packet->page_buf[j].len =
85799a37
HZ
971 PAGE_SIZE;
972 bytes_remain -= PAGE_SIZE;
21a80820 973 }
ca623ad3 974 netvsc_packet->page_buf[j].pfn =
85799a37
HZ
975 virt_to_phys((void *)(end_virtual -
976 bytes_remain)) >> PAGE_SHIFT;
72a2f5bd 977 netvsc_packet->page_buf_cnt++;
85799a37 978 if (bytes_remain == 0)
21a80820 979 break;
fceaf24a 980 }
fceaf24a 981 }
fceaf24a 982
454f18a9 983 /* Pass it to the upper layer */
ac6f7859 984 rndis_filter_receive(device, netvsc_packet);
fceaf24a 985
5a71ae30 986 netvsc_receive_completion(netvsc_packet->
72a2f5bd 987 completion.recv.recv_completion_ctx);
fceaf24a
HJ
988 }
989
5a71ae30 990 put_net_device(device);
fceaf24a
HJ
991}
992
5a71ae30 993static void netvsc_channel_cb(void *context)
fceaf24a 994{
21a80820 995 int ret;
85799a37
HZ
996 struct hv_device *device = context;
997 struct netvsc_device *net_device;
998 u32 bytes_recvd;
999 u64 request_id;
c6fcf0ba 1000 unsigned char *packet;
8dc0a06a 1001 struct vmpacket_descriptor *desc;
c6fcf0ba
BP
1002 unsigned char *buffer;
1003 int bufferlen = NETVSC_PACKET_SIZE;
fceaf24a 1004
c6fcf0ba 1005 packet = kzalloc(NETVSC_PACKET_SIZE * sizeof(unsigned char),
d70c6731 1006 GFP_ATOMIC);
c6fcf0ba
BP
1007 if (!packet)
1008 return;
1009 buffer = packet;
1010
5a71ae30 1011 net_device = get_inbound_net_device(device);
85799a37 1012 if (!net_device) {
eb335bc4 1013 dev_err(&device->device, "net device (%p) shutting down..."
85799a37 1014 "ignoring inbound packets", net_device);
c6fcf0ba 1015 goto out;
fceaf24a
HJ
1016 }
1017
21a80820 1018 do {
9f630068 1019 ret = vmbus_recvpacket_raw(device->channel, buffer, bufferlen,
85799a37 1020 &bytes_recvd, &request_id);
21a80820 1021 if (ret == 0) {
85799a37 1022 if (bytes_recvd > 0) {
21a80820 1023 desc = (struct vmpacket_descriptor *)buffer;
415f2287
HZ
1024 switch (desc->type) {
1025 case VM_PKT_COMP:
5a71ae30 1026 netvsc_send_completion(device, desc);
21a80820
GKH
1027 break;
1028
415f2287 1029 case VM_PKT_DATA_USING_XFER_PAGES:
5a71ae30 1030 netvsc_receive(device, desc);
21a80820
GKH
1031 break;
1032
1033 default:
eb335bc4 1034 dev_err(&device->device,
21a80820
GKH
1035 "unhandled packet type %d, "
1036 "tid %llx len %d\n",
415f2287 1037 desc->type, request_id,
85799a37 1038 bytes_recvd);
21a80820 1039 break;
fceaf24a
HJ
1040 }
1041
454f18a9 1042 /* reset */
c6fcf0ba 1043 if (bufferlen > NETVSC_PACKET_SIZE) {
8c69f52a 1044 kfree(buffer);
fceaf24a 1045 buffer = packet;
c6fcf0ba 1046 bufferlen = NETVSC_PACKET_SIZE;
fceaf24a 1047 }
21a80820 1048 } else {
454f18a9 1049 /* reset */
c6fcf0ba 1050 if (bufferlen > NETVSC_PACKET_SIZE) {
8c69f52a 1051 kfree(buffer);
fceaf24a 1052 buffer = packet;
c6fcf0ba 1053 bufferlen = NETVSC_PACKET_SIZE;
fceaf24a
HJ
1054 }
1055
1056 break;
1057 }
21a80820
GKH
1058 } else if (ret == -2) {
1059 /* Handle large packet */
85799a37 1060 buffer = kmalloc(bytes_recvd, GFP_ATOMIC);
21a80820 1061 if (buffer == NULL) {
454f18a9 1062 /* Try again next time around */
eb335bc4 1063 dev_err(&device->device,
21a80820 1064 "unable to allocate buffer of size "
85799a37 1065 "(%d)!!", bytes_recvd);
fceaf24a
HJ
1066 break;
1067 }
1068
85799a37 1069 bufferlen = bytes_recvd;
fceaf24a
HJ
1070 }
1071 } while (1);
1072
5a71ae30 1073 put_net_device(device);
c6fcf0ba
BP
1074out:
1075 kfree(buffer);
fceaf24a
HJ
1076 return;
1077}
af24ce42 1078
b637e023
HZ
1079/*
1080 * netvsc_device_add - Callback when the device belonging to this
1081 * driver is added
1082 */
7bd23a4d 1083int netvsc_device_add(struct hv_device *device, void *additional_info)
b637e023
HZ
1084{
1085 int ret = 0;
1086 int i;
1087 struct netvsc_device *net_device;
1088 struct hv_netvsc_packet *packet, *pos;
1089 struct netvsc_driver *net_driver =
779b4d17 1090 drv_to_netvscdrv(device->device.driver);
b637e023
HZ
1091
1092 net_device = alloc_net_device(device);
1093 if (!net_device) {
1094 ret = -1;
1095 goto cleanup;
1096 }
1097
1098 /* Initialize the NetVSC channel extension */
1099 net_device->recv_buf_size = NETVSC_RECEIVE_BUFFER_SIZE;
1100 spin_lock_init(&net_device->recv_pkt_list_lock);
1101
1102 net_device->send_buf_size = NETVSC_SEND_BUFFER_SIZE;
1103
1104 INIT_LIST_HEAD(&net_device->recv_pkt_list);
1105
1106 for (i = 0; i < NETVSC_RECEIVE_PACKETLIST_COUNT; i++) {
1107 packet = kzalloc(sizeof(struct hv_netvsc_packet) +
1108 (NETVSC_RECEIVE_SG_COUNT *
1109 sizeof(struct hv_page_buffer)), GFP_KERNEL);
1110 if (!packet)
1111 break;
1112
1113 list_add_tail(&packet->list_ent,
1114 &net_device->recv_pkt_list);
1115 }
35abb21a 1116 init_completion(&net_device->channel_init_wait);
b637e023
HZ
1117
1118 /* Open the channel */
1119 ret = vmbus_open(device->channel, net_driver->ring_buf_size,
1120 net_driver->ring_buf_size, NULL, 0,
1121 netvsc_channel_cb, device);
1122
1123 if (ret != 0) {
1124 dev_err(&device->device, "unable to open channel: %d", ret);
1125 ret = -1;
1126 goto cleanup;
1127 }
1128
1129 /* Channel is opened */
1130 pr_info("hv_netvsc channel opened successfully");
1131
1132 /* Connect with the NetVsp */
1133 ret = netvsc_connect_vsp(device);
1134 if (ret != 0) {
1135 dev_err(&device->device,
1136 "unable to connect to NetVSP - %d", ret);
1137 ret = -1;
1138 goto close;
1139 }
1140
1141 return ret;
1142
1143close:
1144 /* Now, we can close the channel safely */
1145 vmbus_close(device->channel);
1146
1147cleanup:
1148
1149 if (net_device) {
1150 list_for_each_entry_safe(packet, pos,
1151 &net_device->recv_pkt_list,
1152 list_ent) {
1153 list_del(&packet->list_ent);
1154 kfree(packet);
1155 }
1156
1157 release_outbound_net_device(device);
1158 release_inbound_net_device(device);
1159
1160 free_net_device(net_device);
1161 }
1162
1163 return ret;
1164}
1165
af24ce42
HZ
1166/*
1167 * netvsc_initialize - Main entry point
1168 */
1169int netvsc_initialize(struct hv_driver *drv)
1170{
779b4d17
S
1171 struct netvsc_driver *driver =
1172 drv_to_netvscdrv(&drv->driver);
af24ce42
HZ
1173
1174 drv->name = driver_name;
1175 memcpy(&drv->dev_type, &netvsc_device_type, sizeof(struct hv_guid));
1176
af24ce42
HZ
1177 rndis_filter_init(driver);
1178 return 0;
1179}