team: avoid race condition in scheduling delayed work
[linux-2.6-block.git] / drivers / net / hyperv / 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
adf8d3ff 14 * this program; if not, see <http://www.gnu.org/licenses/>.
fceaf24a
HJ
15 *
16 * Authors:
d0e94d17 17 * Haiyang Zhang <haiyangz@microsoft.com>
fceaf24a 18 * Hank Janssen <hjanssen@microsoft.com>
fceaf24a 19 */
eb335bc4
HJ
20#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21
5654e932 22#include <linux/kernel.h>
0c3b7b2f
S
23#include <linux/sched.h>
24#include <linux/wait.h>
0ffa63b0 25#include <linux/mm.h>
b4362c9c 26#include <linux/delay.h>
21a80820 27#include <linux/io.h>
5a0e3ad6 28#include <linux/slab.h>
d9871158 29#include <linux/netdevice.h>
f157e78d 30#include <linux/if_ether.h>
c25aaf81 31#include <asm/sync_bitops.h>
3f335ea2 32
5ca7252a 33#include "hyperv_net.h"
fceaf24a
HJ
34
35
5a71ae30 36static struct netvsc_device *alloc_net_device(struct hv_device *device)
fceaf24a 37{
85799a37 38 struct netvsc_device *net_device;
2ddd5e5f 39 struct net_device *ndev = hv_get_drvdata(device);
fceaf24a 40
85799a37
HZ
41 net_device = kzalloc(sizeof(struct netvsc_device), GFP_KERNEL);
42 if (!net_device)
fceaf24a
HJ
43 return NULL;
44
dc5cd894 45 init_waitqueue_head(&net_device->wait_drain);
4d447c9a 46 net_device->start_remove = false;
c38b9c71 47 net_device->destroy = false;
53d21fdb 48 net_device->dev = device;
2ddd5e5f 49 net_device->ndev = ndev;
fceaf24a 50
2ddd5e5f 51 hv_set_drvdata(device, net_device);
85799a37 52 return net_device;
fceaf24a
HJ
53}
54
5a71ae30 55static struct netvsc_device *get_outbound_net_device(struct hv_device *device)
fceaf24a 56{
85799a37 57 struct netvsc_device *net_device;
fceaf24a 58
2ddd5e5f 59 net_device = hv_get_drvdata(device);
9d88f33a 60 if (net_device && net_device->destroy)
85799a37 61 net_device = NULL;
fceaf24a 62
85799a37 63 return net_device;
fceaf24a
HJ
64}
65
5a71ae30 66static struct netvsc_device *get_inbound_net_device(struct hv_device *device)
fceaf24a 67{
85799a37 68 struct netvsc_device *net_device;
fceaf24a 69
2ddd5e5f 70 net_device = hv_get_drvdata(device);
9d88f33a
S
71
72 if (!net_device)
73 goto get_in_err;
74
75 if (net_device->destroy &&
76 atomic_read(&net_device->num_outstanding_sends) == 0)
85799a37 77 net_device = NULL;
fceaf24a 78
9d88f33a 79get_in_err:
85799a37 80 return net_device;
fceaf24a
HJ
81}
82
fceaf24a 83
c25aaf81 84static int netvsc_destroy_buf(struct netvsc_device *net_device)
ec91cd09
HZ
85{
86 struct nvsp_message *revoke_packet;
87 int ret = 0;
2ddd5e5f 88 struct net_device *ndev = net_device->ndev;
ec91cd09
HZ
89
90 /*
91 * If we got a section count, it means we received a
92 * SendReceiveBufferComplete msg (ie sent
93 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
94 * to send a revoke msg here
95 */
96 if (net_device->recv_section_cnt) {
97 /* Send the revoke receive buffer */
98 revoke_packet = &net_device->revoke_packet;
99 memset(revoke_packet, 0, sizeof(struct nvsp_message));
100
101 revoke_packet->hdr.msg_type =
102 NVSP_MSG1_TYPE_REVOKE_RECV_BUF;
103 revoke_packet->msg.v1_msg.
104 revoke_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
105
106 ret = vmbus_sendpacket(net_device->dev->channel,
107 revoke_packet,
108 sizeof(struct nvsp_message),
109 (unsigned long)revoke_packet,
110 VM_PKT_DATA_INBAND, 0);
111 /*
112 * If we failed here, we might as well return and
113 * have a leak rather than continue and a bugchk
114 */
115 if (ret != 0) {
d9871158 116 netdev_err(ndev, "unable to send "
c909ebbd 117 "revoke receive buffer to netvsp\n");
a3e00530 118 return ret;
ec91cd09
HZ
119 }
120 }
121
122 /* Teardown the gpadl on the vsp end */
123 if (net_device->recv_buf_gpadl_handle) {
124 ret = vmbus_teardown_gpadl(net_device->dev->channel,
125 net_device->recv_buf_gpadl_handle);
126
127 /* If we failed here, we might as well return and have a leak
128 * rather than continue and a bugchk
129 */
130 if (ret != 0) {
d9871158 131 netdev_err(ndev,
c909ebbd 132 "unable to teardown receive buffer's gpadl\n");
7f9615e6 133 return ret;
ec91cd09
HZ
134 }
135 net_device->recv_buf_gpadl_handle = 0;
136 }
137
138 if (net_device->recv_buf) {
139 /* Free up the receive buffer */
b679ef73 140 vfree(net_device->recv_buf);
ec91cd09
HZ
141 net_device->recv_buf = NULL;
142 }
143
144 if (net_device->recv_section) {
145 net_device->recv_section_cnt = 0;
146 kfree(net_device->recv_section);
147 net_device->recv_section = NULL;
148 }
149
c25aaf81
KS
150 /* Deal with the send buffer we may have setup.
151 * If we got a send section size, it means we received a
152 * SendsendBufferComplete msg (ie sent
153 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
154 * to send a revoke msg here
155 */
156 if (net_device->send_section_size) {
157 /* Send the revoke receive buffer */
158 revoke_packet = &net_device->revoke_packet;
159 memset(revoke_packet, 0, sizeof(struct nvsp_message));
160
161 revoke_packet->hdr.msg_type =
162 NVSP_MSG1_TYPE_REVOKE_SEND_BUF;
163 revoke_packet->msg.v1_msg.revoke_recv_buf.id = 0;
164
165 ret = vmbus_sendpacket(net_device->dev->channel,
166 revoke_packet,
167 sizeof(struct nvsp_message),
168 (unsigned long)revoke_packet,
169 VM_PKT_DATA_INBAND, 0);
170 /* If we failed here, we might as well return and
171 * have a leak rather than continue and a bugchk
172 */
173 if (ret != 0) {
174 netdev_err(ndev, "unable to send "
175 "revoke send buffer to netvsp\n");
176 return ret;
177 }
178 }
179 /* Teardown the gpadl on the vsp end */
180 if (net_device->send_buf_gpadl_handle) {
181 ret = vmbus_teardown_gpadl(net_device->dev->channel,
182 net_device->send_buf_gpadl_handle);
183
184 /* If we failed here, we might as well return and have a leak
185 * rather than continue and a bugchk
186 */
187 if (ret != 0) {
188 netdev_err(ndev,
189 "unable to teardown send buffer's gpadl\n");
190 return ret;
191 }
2f18423d 192 net_device->send_buf_gpadl_handle = 0;
c25aaf81
KS
193 }
194 if (net_device->send_buf) {
195 /* Free up the receive buffer */
06b47aac 196 vfree(net_device->send_buf);
c25aaf81
KS
197 net_device->send_buf = NULL;
198 }
199 kfree(net_device->send_section_map);
200
ec91cd09
HZ
201 return ret;
202}
203
c25aaf81 204static int netvsc_init_buf(struct hv_device *device)
fceaf24a 205{
21a80820 206 int ret = 0;
35abb21a 207 int t;
85799a37
HZ
208 struct netvsc_device *net_device;
209 struct nvsp_message *init_packet;
2ddd5e5f 210 struct net_device *ndev;
fceaf24a 211
5a71ae30 212 net_device = get_outbound_net_device(device);
2ddd5e5f 213 if (!net_device)
927bc33c 214 return -ENODEV;
2ddd5e5f 215 ndev = net_device->ndev;
fceaf24a 216
b679ef73 217 net_device->recv_buf = vzalloc(net_device->recv_buf_size);
53d21fdb 218 if (!net_device->recv_buf) {
d9871158 219 netdev_err(ndev, "unable to allocate receive "
c909ebbd 220 "buffer of size %d\n", net_device->recv_buf_size);
927bc33c 221 ret = -ENOMEM;
0c3b7b2f 222 goto cleanup;
fceaf24a 223 }
fceaf24a 224
454f18a9
BP
225 /*
226 * Establish the gpadl handle for this buffer on this
227 * channel. Note: This call uses the vmbus connection rather
228 * than the channel to establish the gpadl handle.
229 */
53d21fdb
HZ
230 ret = vmbus_establish_gpadl(device->channel, net_device->recv_buf,
231 net_device->recv_buf_size,
232 &net_device->recv_buf_gpadl_handle);
21a80820 233 if (ret != 0) {
d9871158 234 netdev_err(ndev,
c909ebbd 235 "unable to establish receive buffer's gpadl\n");
0c3b7b2f 236 goto cleanup;
fceaf24a
HJ
237 }
238
fceaf24a 239
454f18a9 240 /* Notify the NetVsp of the gpadl handle */
53d21fdb 241 init_packet = &net_device->channel_init_pkt;
fceaf24a 242
85799a37 243 memset(init_packet, 0, sizeof(struct nvsp_message));
fceaf24a 244
53d21fdb
HZ
245 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_RECV_BUF;
246 init_packet->msg.v1_msg.send_recv_buf.
247 gpadl_handle = net_device->recv_buf_gpadl_handle;
248 init_packet->msg.v1_msg.
249 send_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
fceaf24a 250
454f18a9 251 /* Send the gpadl notification request */
85799a37 252 ret = vmbus_sendpacket(device->channel, init_packet,
5a4df290 253 sizeof(struct nvsp_message),
85799a37 254 (unsigned long)init_packet,
415f2287 255 VM_PKT_DATA_INBAND,
5a4df290 256 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
21a80820 257 if (ret != 0) {
d9871158 258 netdev_err(ndev,
c909ebbd 259 "unable to send receive buffer's gpadl to netvsp\n");
0c3b7b2f 260 goto cleanup;
fceaf24a
HJ
261 }
262
5c5781b3 263 t = wait_for_completion_timeout(&net_device->channel_init_wait, 5*HZ);
35abb21a 264 BUG_ON(t == 0);
0c3b7b2f 265
fceaf24a 266
454f18a9 267 /* Check the response */
53d21fdb
HZ
268 if (init_packet->msg.v1_msg.
269 send_recv_buf_complete.status != NVSP_STAT_SUCCESS) {
d9871158 270 netdev_err(ndev, "Unable to complete receive buffer "
8bff33ab 271 "initialization with NetVsp - status %d\n",
53d21fdb
HZ
272 init_packet->msg.v1_msg.
273 send_recv_buf_complete.status);
927bc33c 274 ret = -EINVAL;
0c3b7b2f 275 goto cleanup;
fceaf24a
HJ
276 }
277
454f18a9 278 /* Parse the response */
fceaf24a 279
53d21fdb
HZ
280 net_device->recv_section_cnt = init_packet->msg.
281 v1_msg.send_recv_buf_complete.num_sections;
fceaf24a 282
c1813200
HZ
283 net_device->recv_section = kmemdup(
284 init_packet->msg.v1_msg.send_recv_buf_complete.sections,
285 net_device->recv_section_cnt *
286 sizeof(struct nvsp_1_receive_buffer_section),
287 GFP_KERNEL);
53d21fdb 288 if (net_device->recv_section == NULL) {
927bc33c 289 ret = -EINVAL;
0c3b7b2f 290 goto cleanup;
fceaf24a
HJ
291 }
292
21a80820
GKH
293 /*
294 * For 1st release, there should only be 1 section that represents the
295 * entire receive buffer
296 */
53d21fdb
HZ
297 if (net_device->recv_section_cnt != 1 ||
298 net_device->recv_section->offset != 0) {
927bc33c 299 ret = -EINVAL;
0c3b7b2f 300 goto cleanup;
fceaf24a
HJ
301 }
302
c25aaf81
KS
303 /* Now setup the send buffer.
304 */
06b47aac 305 net_device->send_buf = vzalloc(net_device->send_buf_size);
c25aaf81
KS
306 if (!net_device->send_buf) {
307 netdev_err(ndev, "unable to allocate send "
308 "buffer of size %d\n", net_device->send_buf_size);
309 ret = -ENOMEM;
310 goto cleanup;
311 }
312
313 /* Establish the gpadl handle for this buffer on this
314 * channel. Note: This call uses the vmbus connection rather
315 * than the channel to establish the gpadl handle.
316 */
317 ret = vmbus_establish_gpadl(device->channel, net_device->send_buf,
318 net_device->send_buf_size,
319 &net_device->send_buf_gpadl_handle);
320 if (ret != 0) {
321 netdev_err(ndev,
322 "unable to establish send buffer's gpadl\n");
323 goto cleanup;
324 }
325
326 /* Notify the NetVsp of the gpadl handle */
327 init_packet = &net_device->channel_init_pkt;
328 memset(init_packet, 0, sizeof(struct nvsp_message));
329 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_SEND_BUF;
330 init_packet->msg.v1_msg.send_recv_buf.gpadl_handle =
331 net_device->send_buf_gpadl_handle;
332 init_packet->msg.v1_msg.send_recv_buf.id = 0;
333
334 /* Send the gpadl notification request */
335 ret = vmbus_sendpacket(device->channel, init_packet,
336 sizeof(struct nvsp_message),
337 (unsigned long)init_packet,
338 VM_PKT_DATA_INBAND,
339 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
340 if (ret != 0) {
341 netdev_err(ndev,
342 "unable to send send buffer's gpadl to netvsp\n");
343 goto cleanup;
344 }
345
346 t = wait_for_completion_timeout(&net_device->channel_init_wait, 5*HZ);
347 BUG_ON(t == 0);
348
349 /* Check the response */
350 if (init_packet->msg.v1_msg.
351 send_send_buf_complete.status != NVSP_STAT_SUCCESS) {
352 netdev_err(ndev, "Unable to complete send buffer "
353 "initialization with NetVsp - status %d\n",
354 init_packet->msg.v1_msg.
355 send_recv_buf_complete.status);
356 ret = -EINVAL;
357 goto cleanup;
358 }
359
360 /* Parse the response */
361 net_device->send_section_size = init_packet->msg.
362 v1_msg.send_send_buf_complete.section_size;
363
364 /* Section count is simply the size divided by the section size.
365 */
366 net_device->send_section_cnt =
367 net_device->send_buf_size/net_device->send_section_size;
368
369 dev_info(&device->device, "Send section size: %d, Section count:%d\n",
370 net_device->send_section_size, net_device->send_section_cnt);
371
372 /* Setup state for managing the send buffer. */
373 net_device->map_words = DIV_ROUND_UP(net_device->send_section_cnt,
374 BITS_PER_LONG);
375
376 net_device->send_section_map =
377 kzalloc(net_device->map_words * sizeof(ulong), GFP_KERNEL);
dd1d3f8f
WY
378 if (net_device->send_section_map == NULL) {
379 ret = -ENOMEM;
c25aaf81 380 goto cleanup;
dd1d3f8f 381 }
c25aaf81 382
0c3b7b2f 383 goto exit;
fceaf24a 384
0c3b7b2f 385cleanup:
c25aaf81 386 netvsc_destroy_buf(net_device);
fceaf24a 387
0c3b7b2f 388exit:
fceaf24a
HJ
389 return ret;
390}
391
fceaf24a 392
f157e78d
HZ
393/* Negotiate NVSP protocol version */
394static int negotiate_nvsp_ver(struct hv_device *device,
395 struct netvsc_device *net_device,
396 struct nvsp_message *init_packet,
397 u32 nvsp_ver)
fceaf24a 398{
35abb21a 399 int ret, t;
fceaf24a 400
85799a37 401 memset(init_packet, 0, sizeof(struct nvsp_message));
53d21fdb 402 init_packet->hdr.msg_type = NVSP_MSG_TYPE_INIT;
f157e78d
HZ
403 init_packet->msg.init_msg.init.min_protocol_ver = nvsp_ver;
404 init_packet->msg.init_msg.init.max_protocol_ver = nvsp_ver;
fceaf24a 405
454f18a9 406 /* Send the init request */
85799a37 407 ret = vmbus_sendpacket(device->channel, init_packet,
5a4df290 408 sizeof(struct nvsp_message),
85799a37 409 (unsigned long)init_packet,
415f2287 410 VM_PKT_DATA_INBAND,
5a4df290 411 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
21a80820 412
b8a3d52b 413 if (ret != 0)
f157e78d 414 return ret;
fceaf24a 415
5c5781b3 416 t = wait_for_completion_timeout(&net_device->channel_init_wait, 5*HZ);
35abb21a 417
f157e78d
HZ
418 if (t == 0)
419 return -ETIMEDOUT;
fceaf24a 420
53d21fdb 421 if (init_packet->msg.init_msg.init_complete.status !=
f157e78d
HZ
422 NVSP_STAT_SUCCESS)
423 return -EINVAL;
fceaf24a 424
a1eabb01 425 if (nvsp_ver == NVSP_PROTOCOL_VERSION_1)
f157e78d
HZ
426 return 0;
427
428 /* NVSPv2 only: Send NDIS config */
429 memset(init_packet, 0, sizeof(struct nvsp_message));
430 init_packet->hdr.msg_type = NVSP_MSG2_TYPE_SEND_NDIS_CONFIG;
4d447c9a 431 init_packet->msg.v2_msg.send_ndis_config.mtu = net_device->ndev->mtu;
1f5f3a75 432 init_packet->msg.v2_msg.send_ndis_config.capability.ieee8021q = 1;
f157e78d
HZ
433
434 ret = vmbus_sendpacket(device->channel, init_packet,
435 sizeof(struct nvsp_message),
436 (unsigned long)init_packet,
437 VM_PKT_DATA_INBAND, 0);
438
439 return ret;
440}
441
442static int netvsc_connect_vsp(struct hv_device *device)
443{
444 int ret;
445 struct netvsc_device *net_device;
446 struct nvsp_message *init_packet;
447 int ndis_version;
448 struct net_device *ndev;
a1eabb01
HZ
449 u32 ver_list[] = { NVSP_PROTOCOL_VERSION_1, NVSP_PROTOCOL_VERSION_2,
450 NVSP_PROTOCOL_VERSION_4, NVSP_PROTOCOL_VERSION_5 };
451 int i, num_ver = 4; /* number of different NVSP versions */
f157e78d
HZ
452
453 net_device = get_outbound_net_device(device);
454 if (!net_device)
455 return -ENODEV;
456 ndev = net_device->ndev;
457
458 init_packet = &net_device->channel_init_pkt;
459
460 /* Negotiate the latest NVSP protocol supported */
a1eabb01
HZ
461 for (i = num_ver - 1; i >= 0; i--)
462 if (negotiate_nvsp_ver(device, net_device, init_packet,
463 ver_list[i]) == 0) {
464 net_device->nvsp_version = ver_list[i];
465 break;
466 }
467
468 if (i < 0) {
0f48c72c 469 ret = -EPROTO;
0c3b7b2f 470 goto cleanup;
fceaf24a 471 }
f157e78d
HZ
472
473 pr_debug("Negotiated NVSP version:%x\n", net_device->nvsp_version);
474
454f18a9 475 /* Send the ndis version */
85799a37 476 memset(init_packet, 0, sizeof(struct nvsp_message));
fceaf24a 477
a1eabb01 478 if (net_device->nvsp_version <= NVSP_PROTOCOL_VERSION_4)
1f73db49 479 ndis_version = 0x00060001;
a1eabb01
HZ
480 else
481 ndis_version = 0x0006001e;
fceaf24a 482
53d21fdb
HZ
483 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_NDIS_VER;
484 init_packet->msg.v1_msg.
485 send_ndis_ver.ndis_major_ver =
85799a37 486 (ndis_version & 0xFFFF0000) >> 16;
53d21fdb
HZ
487 init_packet->msg.v1_msg.
488 send_ndis_ver.ndis_minor_ver =
85799a37 489 ndis_version & 0xFFFF;
fceaf24a 490
454f18a9 491 /* Send the init request */
85799a37 492 ret = vmbus_sendpacket(device->channel, init_packet,
0c3b7b2f
S
493 sizeof(struct nvsp_message),
494 (unsigned long)init_packet,
495 VM_PKT_DATA_INBAND, 0);
0f48c72c 496 if (ret != 0)
0c3b7b2f 497 goto cleanup;
454f18a9
BP
498
499 /* Post the big receive buffer to NetVSP */
99d3016d
HZ
500 if (net_device->nvsp_version <= NVSP_PROTOCOL_VERSION_2)
501 net_device->recv_buf_size = NETVSC_RECEIVE_BUFFER_SIZE_LEGACY;
502 else
503 net_device->recv_buf_size = NETVSC_RECEIVE_BUFFER_SIZE;
c25aaf81 504 net_device->send_buf_size = NETVSC_SEND_BUFFER_SIZE;
99d3016d 505
c25aaf81 506 ret = netvsc_init_buf(device);
fceaf24a 507
0c3b7b2f 508cleanup:
fceaf24a
HJ
509 return ret;
510}
511
648dc598 512static void netvsc_disconnect_vsp(struct netvsc_device *net_device)
fceaf24a 513{
c25aaf81 514 netvsc_destroy_buf(net_device);
fceaf24a
HJ
515}
516
3e189519 517/*
5a71ae30 518 * netvsc_device_remove - Callback when the root bus device is removed
21a80820 519 */
905620d1 520int netvsc_device_remove(struct hv_device *device)
fceaf24a 521{
85799a37 522 struct netvsc_device *net_device;
c38b9c71 523 unsigned long flags;
fceaf24a 524
2ddd5e5f 525 net_device = hv_get_drvdata(device);
fceaf24a 526
648dc598 527 netvsc_disconnect_vsp(net_device);
fceaf24a 528
3852409b 529 /*
9d88f33a
S
530 * Since we have already drained, we don't need to busy wait
531 * as was done in final_release_stor_device()
532 * Note that we cannot set the ext pointer to NULL until
533 * we have drained - to drain the outgoing packets, we need to
534 * allow incoming packets.
3852409b 535 */
9d88f33a
S
536
537 spin_lock_irqsave(&device->channel->inbound_lock, flags);
2ddd5e5f 538 hv_set_drvdata(device, NULL);
9d88f33a 539 spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
3852409b 540
86c921af
S
541 /*
542 * At this point, no one should be accessing net_device
543 * except in here
544 */
c909ebbd 545 dev_notice(&device->device, "net device safe to remove\n");
fceaf24a 546
454f18a9 547 /* Now, we can close the channel safely */
85799a37 548 vmbus_close(device->channel);
fceaf24a 549
454f18a9 550 /* Release all resources */
5b54dac8
HZ
551 if (net_device->sub_cb_buf)
552 vfree(net_device->sub_cb_buf);
553
356c4657 554 kfree(net_device);
21a80820 555 return 0;
fceaf24a
HJ
556}
557
33be96e4
HZ
558
559#define RING_AVAIL_PERCENT_HIWATER 20
560#define RING_AVAIL_PERCENT_LOWATER 10
561
562/*
563 * Get the percentage of available bytes to write in the ring.
564 * The return value is in range from 0 to 100.
565 */
566static inline u32 hv_ringbuf_avail_percent(
567 struct hv_ring_buffer_info *ring_info)
568{
569 u32 avail_read, avail_write;
570
571 hv_get_ringbuffer_availbytes(ring_info, &avail_read, &avail_write);
572
573 return avail_write * 100 / ring_info->ring_datasize;
574}
575
c25aaf81
KS
576static inline void netvsc_free_send_slot(struct netvsc_device *net_device,
577 u32 index)
578{
579 sync_change_bit(index, net_device->send_section_map);
580}
581
97c1723a
KS
582static void netvsc_send_completion(struct netvsc_device *net_device,
583 struct hv_device *device,
85799a37 584 struct vmpacket_descriptor *packet)
fceaf24a 585{
85799a37
HZ
586 struct nvsp_message *nvsp_packet;
587 struct hv_netvsc_packet *nvsc_packet;
2ddd5e5f 588 struct net_device *ndev;
c25aaf81 589 u32 send_index;
fceaf24a 590
2ddd5e5f 591 ndev = net_device->ndev;
fceaf24a 592
85799a37 593 nvsp_packet = (struct nvsp_message *)((unsigned long)packet +
415f2287 594 (packet->offset8 << 3));
fceaf24a 595
53d21fdb
HZ
596 if ((nvsp_packet->hdr.msg_type == NVSP_MSG_TYPE_INIT_COMPLETE) ||
597 (nvsp_packet->hdr.msg_type ==
598 NVSP_MSG1_TYPE_SEND_RECV_BUF_COMPLETE) ||
599 (nvsp_packet->hdr.msg_type ==
5b54dac8
HZ
600 NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE) ||
601 (nvsp_packet->hdr.msg_type ==
602 NVSP_MSG5_TYPE_SUBCHANNEL)) {
454f18a9 603 /* Copy the response back */
53d21fdb 604 memcpy(&net_device->channel_init_pkt, nvsp_packet,
21a80820 605 sizeof(struct nvsp_message));
35abb21a 606 complete(&net_device->channel_init_wait);
53d21fdb
HZ
607 } else if (nvsp_packet->hdr.msg_type ==
608 NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE) {
33be96e4 609 int num_outstanding_sends;
5b54dac8
HZ
610 u16 q_idx = 0;
611 struct vmbus_channel *channel = device->channel;
612 int queue_sends;
33be96e4 613
454f18a9 614 /* Get the send context */
85799a37 615 nvsc_packet = (struct hv_netvsc_packet *)(unsigned long)
415f2287 616 packet->trans_id;
fceaf24a 617
454f18a9 618 /* Notify the layer above us */
5b54dac8 619 if (nvsc_packet) {
c25aaf81
KS
620 send_index = nvsc_packet->send_buf_index;
621 if (send_index != NETVSC_INVALID_INDEX)
622 netvsc_free_send_slot(net_device, send_index);
5b54dac8
HZ
623 q_idx = nvsc_packet->q_idx;
624 channel = nvsc_packet->channel;
893f6627
HZ
625 nvsc_packet->send_completion(nvsc_packet->
626 send_completion_ctx);
5b54dac8 627 }
fceaf24a 628
33be96e4
HZ
629 num_outstanding_sends =
630 atomic_dec_return(&net_device->num_outstanding_sends);
5b54dac8
HZ
631 queue_sends = atomic_dec_return(&net_device->
632 queue_sends[q_idx]);
1d06825b 633
dc5cd894
HZ
634 if (net_device->destroy && num_outstanding_sends == 0)
635 wake_up(&net_device->wait_drain);
636
5b54dac8
HZ
637 if (netif_tx_queue_stopped(netdev_get_tx_queue(ndev, q_idx)) &&
638 !net_device->start_remove &&
639 (hv_ringbuf_avail_percent(&channel->outbound) >
640 RING_AVAIL_PERCENT_HIWATER || queue_sends < 1))
641 netif_tx_wake_queue(netdev_get_tx_queue(
642 ndev, q_idx));
21a80820 643 } else {
d9871158 644 netdev_err(ndev, "Unknown send completion packet type- "
c909ebbd 645 "%d received!!\n", nvsp_packet->hdr.msg_type);
fceaf24a
HJ
646 }
647
fceaf24a
HJ
648}
649
c25aaf81
KS
650static u32 netvsc_get_next_send_section(struct netvsc_device *net_device)
651{
652 unsigned long index;
653 u32 max_words = net_device->map_words;
654 unsigned long *map_addr = (unsigned long *)net_device->send_section_map;
655 u32 section_cnt = net_device->send_section_cnt;
656 int ret_val = NETVSC_INVALID_INDEX;
657 int i;
658 int prev_val;
659
660 for (i = 0; i < max_words; i++) {
661 if (!~(map_addr[i]))
662 continue;
663 index = ffz(map_addr[i]);
664 prev_val = sync_test_and_set_bit(index, &map_addr[i]);
665 if (prev_val)
666 continue;
667 if ((index + (i * BITS_PER_LONG)) >= section_cnt)
668 break;
669 ret_val = (index + (i * BITS_PER_LONG));
670 break;
671 }
672 return ret_val;
673}
674
675u32 netvsc_copy_to_send_buf(struct netvsc_device *net_device,
676 unsigned int section_index,
677 struct hv_netvsc_packet *packet)
678{
679 char *start = net_device->send_buf;
680 char *dest = (start + (section_index * net_device->send_section_size));
681 int i;
682 u32 msg_size = 0;
683
684 for (i = 0; i < packet->page_buf_cnt; i++) {
685 char *src = phys_to_virt(packet->page_buf[i].pfn << PAGE_SHIFT);
686 u32 offset = packet->page_buf[i].offset;
687 u32 len = packet->page_buf[i].len;
688
689 memcpy(dest, (src + offset), len);
690 msg_size += len;
691 dest += len;
692 }
693 return msg_size;
694}
695
f9819f05 696int netvsc_send(struct hv_device *device,
85799a37 697 struct hv_netvsc_packet *packet)
fceaf24a 698{
85799a37 699 struct netvsc_device *net_device;
21a80820 700 int ret = 0;
223c1aa6 701 struct nvsp_message sendMessage;
2ddd5e5f 702 struct net_device *ndev;
5b54dac8 703 struct vmbus_channel *out_channel = NULL;
f1ea3cd7 704 u64 req_id;
c25aaf81
KS
705 unsigned int section_index = NETVSC_INVALID_INDEX;
706 u32 msg_size = 0;
707 struct sk_buff *skb;
708
fceaf24a 709
5a71ae30 710 net_device = get_outbound_net_device(device);
2ddd5e5f 711 if (!net_device)
ff2bd69a 712 return -ENODEV;
2ddd5e5f 713 ndev = net_device->ndev;
fceaf24a 714
53d21fdb 715 sendMessage.hdr.msg_type = NVSP_MSG1_TYPE_SEND_RNDIS_PKT;
72a2f5bd 716 if (packet->is_data_pkt) {
21a80820 717 /* 0 is RMC_DATA; */
53d21fdb 718 sendMessage.msg.v1_msg.send_rndis_pkt.channel_type = 0;
21a80820
GKH
719 } else {
720 /* 1 is RMC_CONTROL; */
53d21fdb 721 sendMessage.msg.v1_msg.send_rndis_pkt.channel_type = 1;
21a80820 722 }
fceaf24a 723
c25aaf81
KS
724 /* Attempt to send via sendbuf */
725 if (packet->total_data_buflen < net_device->send_section_size) {
726 section_index = netvsc_get_next_send_section(net_device);
727 if (section_index != NETVSC_INVALID_INDEX) {
728 msg_size = netvsc_copy_to_send_buf(net_device,
729 section_index,
730 packet);
731 skb = (struct sk_buff *)
732 (unsigned long)packet->send_completion_tid;
733 if (skb)
734 dev_kfree_skb_any(skb);
735 packet->page_buf_cnt = 0;
736 }
737 }
738 packet->send_buf_index = section_index;
739
740
53d21fdb 741 sendMessage.msg.v1_msg.send_rndis_pkt.send_buf_section_index =
c25aaf81
KS
742 section_index;
743 sendMessage.msg.v1_msg.send_rndis_pkt.send_buf_section_size = msg_size;
21a80820 744
893f6627 745 if (packet->send_completion)
00ca8f0c 746 req_id = (ulong)packet;
f1ea3cd7
HZ
747 else
748 req_id = 0;
749
5b54dac8
HZ
750 out_channel = net_device->chn_table[packet->q_idx];
751 if (out_channel == NULL)
752 out_channel = device->channel;
753 packet->channel = out_channel;
754
72a2f5bd 755 if (packet->page_buf_cnt) {
5b54dac8 756 ret = vmbus_sendpacket_pagebuffer(out_channel,
72a2f5bd
HZ
757 packet->page_buf,
758 packet->page_buf_cnt,
ff3f8eec
GKH
759 &sendMessage,
760 sizeof(struct nvsp_message),
f1ea3cd7 761 req_id);
21a80820 762 } else {
5b54dac8 763 ret = vmbus_sendpacket(out_channel, &sendMessage,
e4d59ac5 764 sizeof(struct nvsp_message),
f1ea3cd7 765 req_id,
e4d59ac5
HZ
766 VM_PKT_DATA_INBAND,
767 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
fceaf24a
HJ
768 }
769
1d06825b
HZ
770 if (ret == 0) {
771 atomic_inc(&net_device->num_outstanding_sends);
5b54dac8
HZ
772 atomic_inc(&net_device->queue_sends[packet->q_idx]);
773
774 if (hv_ringbuf_avail_percent(&out_channel->outbound) <
33be96e4 775 RING_AVAIL_PERCENT_LOWATER) {
5b54dac8
HZ
776 netif_tx_stop_queue(netdev_get_tx_queue(
777 ndev, packet->q_idx));
778
33be96e4 779 if (atomic_read(&net_device->
5b54dac8
HZ
780 queue_sends[packet->q_idx]) < 1)
781 netif_tx_wake_queue(netdev_get_tx_queue(
782 ndev, packet->q_idx));
33be96e4 783 }
1d06825b 784 } else if (ret == -EAGAIN) {
5b54dac8
HZ
785 netif_tx_stop_queue(netdev_get_tx_queue(
786 ndev, packet->q_idx));
787 if (atomic_read(&net_device->queue_sends[packet->q_idx]) < 1) {
788 netif_tx_wake_queue(netdev_get_tx_queue(
789 ndev, packet->q_idx));
33be96e4
HZ
790 ret = -ENOSPC;
791 }
1d06825b 792 } else {
d9871158 793 netdev_err(ndev, "Unable to send packet %p ret %d\n",
85799a37 794 packet, ret);
1d06825b 795 }
fceaf24a 796
fceaf24a
HJ
797 return ret;
798}
799
5fa9d3c5 800static void netvsc_send_recv_completion(struct hv_device *device,
5b54dac8 801 struct vmbus_channel *channel,
97c1723a 802 struct netvsc_device *net_device,
63f6921d 803 u64 transaction_id, u32 status)
5fa9d3c5
HZ
804{
805 struct nvsp_message recvcompMessage;
806 int retries = 0;
807 int ret;
2ddd5e5f 808 struct net_device *ndev;
2ddd5e5f
S
809
810 ndev = net_device->ndev;
5fa9d3c5
HZ
811
812 recvcompMessage.hdr.msg_type =
813 NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE;
814
63f6921d 815 recvcompMessage.msg.v1_msg.send_rndis_pkt_complete.status = status;
5fa9d3c5
HZ
816
817retry_send_cmplt:
818 /* Send the completion */
5b54dac8 819 ret = vmbus_sendpacket(channel, &recvcompMessage,
5fa9d3c5
HZ
820 sizeof(struct nvsp_message), transaction_id,
821 VM_PKT_COMP, 0);
822 if (ret == 0) {
823 /* success */
824 /* no-op */
d2598f01 825 } else if (ret == -EAGAIN) {
5fa9d3c5
HZ
826 /* no more room...wait a bit and attempt to retry 3 times */
827 retries++;
d9871158 828 netdev_err(ndev, "unable to send receive completion pkt"
c909ebbd 829 " (tid %llx)...retrying %d\n", transaction_id, retries);
5fa9d3c5
HZ
830
831 if (retries < 4) {
832 udelay(100);
833 goto retry_send_cmplt;
834 } else {
d9871158 835 netdev_err(ndev, "unable to send receive "
c909ebbd 836 "completion pkt (tid %llx)...give up retrying\n",
5fa9d3c5
HZ
837 transaction_id);
838 }
839 } else {
d9871158 840 netdev_err(ndev, "unable to send receive "
c909ebbd 841 "completion pkt - %llx\n", transaction_id);
5fa9d3c5
HZ
842 }
843}
844
97c1723a 845static void netvsc_receive(struct netvsc_device *net_device,
5b54dac8 846 struct vmbus_channel *channel,
97c1723a
KS
847 struct hv_device *device,
848 struct vmpacket_descriptor *packet)
fceaf24a 849{
85799a37
HZ
850 struct vmtransfer_page_packet_header *vmxferpage_packet;
851 struct nvsp_message *nvsp_packet;
4baab261
HZ
852 struct hv_netvsc_packet nv_pkt;
853 struct hv_netvsc_packet *netvsc_packet = &nv_pkt;
854 u32 status = NVSP_STAT_SUCCESS;
45326342
HZ
855 int i;
856 int count = 0;
2ddd5e5f 857 struct net_device *ndev;
779b4d17 858
2ddd5e5f 859 ndev = net_device->ndev;
fceaf24a 860
21a80820
GKH
861 /*
862 * All inbound packets other than send completion should be xfer page
863 * packet
864 */
415f2287 865 if (packet->type != VM_PKT_DATA_USING_XFER_PAGES) {
d9871158 866 netdev_err(ndev, "Unknown packet type received - %d\n",
415f2287 867 packet->type);
fceaf24a
HJ
868 return;
869 }
870
85799a37 871 nvsp_packet = (struct nvsp_message *)((unsigned long)packet +
415f2287 872 (packet->offset8 << 3));
fceaf24a 873
454f18a9 874 /* Make sure this is a valid nvsp packet */
53d21fdb
HZ
875 if (nvsp_packet->hdr.msg_type !=
876 NVSP_MSG1_TYPE_SEND_RNDIS_PKT) {
d9871158 877 netdev_err(ndev, "Unknown nvsp packet type received-"
c909ebbd 878 " %d\n", nvsp_packet->hdr.msg_type);
fceaf24a
HJ
879 return;
880 }
881
85799a37 882 vmxferpage_packet = (struct vmtransfer_page_packet_header *)packet;
fceaf24a 883
415f2287 884 if (vmxferpage_packet->xfer_pageset_id != NETVSC_RECEIVE_BUFFER_ID) {
d9871158 885 netdev_err(ndev, "Invalid xfer page set id - "
c909ebbd 886 "expecting %x got %x\n", NETVSC_RECEIVE_BUFFER_ID,
415f2287 887 vmxferpage_packet->xfer_pageset_id);
fceaf24a
HJ
888 return;
889 }
890
4baab261
HZ
891 count = vmxferpage_packet->range_cnt;
892 netvsc_packet->device = device;
893 netvsc_packet->channel = channel;
fceaf24a 894
454f18a9 895 /* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */
4baab261 896 for (i = 0; i < count; i++) {
454f18a9 897 /* Initialize the netvsc packet */
63f6921d 898 netvsc_packet->status = NVSP_STAT_SUCCESS;
45326342
HZ
899 netvsc_packet->data = (void *)((unsigned long)net_device->
900 recv_buf + vmxferpage_packet->ranges[i].byte_offset);
72a2f5bd 901 netvsc_packet->total_data_buflen =
415f2287 902 vmxferpage_packet->ranges[i].byte_count;
fceaf24a 903
454f18a9 904 /* Pass it to the upper layer */
ac6f7859 905 rndis_filter_receive(device, netvsc_packet);
fceaf24a 906
4baab261
HZ
907 if (netvsc_packet->status != NVSP_STAT_SUCCESS)
908 status = NVSP_STAT_FAIL;
fceaf24a
HJ
909 }
910
4baab261
HZ
911 netvsc_send_recv_completion(device, channel, net_device,
912 vmxferpage_packet->d.trans_id, status);
fceaf24a
HJ
913}
914
5b54dac8
HZ
915
916static void netvsc_send_table(struct hv_device *hdev,
917 struct vmpacket_descriptor *vmpkt)
918{
919 struct netvsc_device *nvscdev;
920 struct net_device *ndev;
921 struct nvsp_message *nvmsg;
922 int i;
923 u32 count, *tab;
924
925 nvscdev = get_outbound_net_device(hdev);
926 if (!nvscdev)
927 return;
928 ndev = nvscdev->ndev;
929
930 nvmsg = (struct nvsp_message *)((unsigned long)vmpkt +
931 (vmpkt->offset8 << 3));
932
933 if (nvmsg->hdr.msg_type != NVSP_MSG5_TYPE_SEND_INDIRECTION_TABLE)
934 return;
935
936 count = nvmsg->msg.v5_msg.send_table.count;
937 if (count != VRSS_SEND_TAB_SIZE) {
938 netdev_err(ndev, "Received wrong send-table size:%u\n", count);
939 return;
940 }
941
942 tab = (u32 *)((unsigned long)&nvmsg->msg.v5_msg.send_table +
943 nvmsg->msg.v5_msg.send_table.offset);
944
945 for (i = 0; i < count; i++)
946 nvscdev->send_table[i] = tab[i];
947}
948
949void netvsc_channel_cb(void *context)
fceaf24a 950{
21a80820 951 int ret;
5b54dac8
HZ
952 struct vmbus_channel *channel = (struct vmbus_channel *)context;
953 struct hv_device *device;
85799a37
HZ
954 struct netvsc_device *net_device;
955 u32 bytes_recvd;
956 u64 request_id;
8dc0a06a 957 struct vmpacket_descriptor *desc;
c6fcf0ba
BP
958 unsigned char *buffer;
959 int bufferlen = NETVSC_PACKET_SIZE;
2ddd5e5f 960 struct net_device *ndev;
fceaf24a 961
5b54dac8
HZ
962 if (channel->primary_channel != NULL)
963 device = channel->primary_channel->device_obj;
964 else
965 device = channel->device_obj;
966
5a71ae30 967 net_device = get_inbound_net_device(device);
2ddd5e5f 968 if (!net_device)
ee0c4c39 969 return;
2ddd5e5f 970 ndev = net_device->ndev;
5b54dac8 971 buffer = get_per_channel_state(channel);
fceaf24a 972
21a80820 973 do {
5b54dac8 974 ret = vmbus_recvpacket_raw(channel, buffer, bufferlen,
85799a37 975 &bytes_recvd, &request_id);
21a80820 976 if (ret == 0) {
85799a37 977 if (bytes_recvd > 0) {
21a80820 978 desc = (struct vmpacket_descriptor *)buffer;
415f2287
HZ
979 switch (desc->type) {
980 case VM_PKT_COMP:
97c1723a
KS
981 netvsc_send_completion(net_device,
982 device, desc);
21a80820
GKH
983 break;
984
415f2287 985 case VM_PKT_DATA_USING_XFER_PAGES:
5b54dac8
HZ
986 netvsc_receive(net_device, channel,
987 device, desc);
988 break;
989
990 case VM_PKT_DATA_INBAND:
991 netvsc_send_table(device, desc);
21a80820
GKH
992 break;
993
994 default:
d9871158 995 netdev_err(ndev,
21a80820
GKH
996 "unhandled packet type %d, "
997 "tid %llx len %d\n",
415f2287 998 desc->type, request_id,
85799a37 999 bytes_recvd);
21a80820 1000 break;
fceaf24a
HJ
1001 }
1002
21a80820 1003 } else {
ee0c4c39
KS
1004 /*
1005 * We are done for this pass.
1006 */
fceaf24a
HJ
1007 break;
1008 }
ee0c4c39 1009
3d5cad97 1010 } else if (ret == -ENOBUFS) {
ee0c4c39
KS
1011 if (bufferlen > NETVSC_PACKET_SIZE)
1012 kfree(buffer);
21a80820 1013 /* Handle large packet */
85799a37 1014 buffer = kmalloc(bytes_recvd, GFP_ATOMIC);
21a80820 1015 if (buffer == NULL) {
454f18a9 1016 /* Try again next time around */
d9871158 1017 netdev_err(ndev,
21a80820 1018 "unable to allocate buffer of size "
c909ebbd 1019 "(%d)!!\n", bytes_recvd);
fceaf24a
HJ
1020 break;
1021 }
1022
85799a37 1023 bufferlen = bytes_recvd;
fceaf24a
HJ
1024 }
1025 } while (1);
1026
ee0c4c39
KS
1027 if (bufferlen > NETVSC_PACKET_SIZE)
1028 kfree(buffer);
fceaf24a
HJ
1029 return;
1030}
af24ce42 1031
b637e023
HZ
1032/*
1033 * netvsc_device_add - Callback when the device belonging to this
1034 * driver is added
1035 */
7bd23a4d 1036int netvsc_device_add(struct hv_device *device, void *additional_info)
b637e023
HZ
1037{
1038 int ret = 0;
aae23986
S
1039 int ring_size =
1040 ((struct netvsc_device_info *)additional_info)->ring_size;
b637e023 1041 struct netvsc_device *net_device;
2ddd5e5f 1042 struct net_device *ndev;
b637e023
HZ
1043
1044 net_device = alloc_net_device(device);
1045 if (!net_device) {
ace163a8 1046 ret = -ENOMEM;
b637e023
HZ
1047 goto cleanup;
1048 }
1049
5b54dac8
HZ
1050 net_device->ring_size = ring_size;
1051
2ddd5e5f
S
1052 /*
1053 * Coming into this function, struct net_device * is
1054 * registered as the driver private data.
1055 * In alloc_net_device(), we register struct netvsc_device *
1056 * as the driver private data and stash away struct net_device *
1057 * in struct netvsc_device *.
1058 */
1059 ndev = net_device->ndev;
1060
b637e023 1061 /* Initialize the NetVSC channel extension */
35abb21a 1062 init_completion(&net_device->channel_init_wait);
b637e023 1063
5b54dac8
HZ
1064 set_per_channel_state(device->channel, net_device->cb_buffer);
1065
b637e023 1066 /* Open the channel */
aae23986
S
1067 ret = vmbus_open(device->channel, ring_size * PAGE_SIZE,
1068 ring_size * PAGE_SIZE, NULL, 0,
5b54dac8 1069 netvsc_channel_cb, device->channel);
b637e023
HZ
1070
1071 if (ret != 0) {
d9871158 1072 netdev_err(ndev, "unable to open channel: %d\n", ret);
b637e023
HZ
1073 goto cleanup;
1074 }
1075
1076 /* Channel is opened */
c909ebbd 1077 pr_info("hv_netvsc channel opened successfully\n");
b637e023 1078
5b54dac8
HZ
1079 net_device->chn_table[0] = device->channel;
1080
b637e023
HZ
1081 /* Connect with the NetVsp */
1082 ret = netvsc_connect_vsp(device);
1083 if (ret != 0) {
d9871158 1084 netdev_err(ndev,
c909ebbd 1085 "unable to connect to NetVSP - %d\n", ret);
b637e023
HZ
1086 goto close;
1087 }
1088
1089 return ret;
1090
1091close:
1092 /* Now, we can close the channel safely */
1093 vmbus_close(device->channel);
1094
1095cleanup:
bd4578bc 1096 kfree(net_device);
b637e023
HZ
1097
1098 return ret;
1099}