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