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