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