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