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