]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/net/hyperv/netvsc.c
aquantia: Fix driver name reported by ethtool
[mirror_ubuntu-artful-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>
d6472302 31#include <linux/vmalloc.h>
c25aaf81 32#include <asm/sync_bitops.h>
3f335ea2 33
5ca7252a 34#include "hyperv_net.h"
fceaf24a 35
84bf9cef
KS
36/*
37 * Switch the data path from the synthetic interface to the VF
38 * interface.
39 */
0a1275ca 40void netvsc_switch_datapath(struct net_device *ndev, bool vf)
84bf9cef 41{
3d541ac5
VK
42 struct net_device_context *net_device_ctx = netdev_priv(ndev);
43 struct hv_device *dev = net_device_ctx->device_ctx;
0a1275ca
VK
44 struct netvsc_device *nv_dev = net_device_ctx->nvdev;
45 struct nvsp_message *init_pkt = &nv_dev->channel_init_pkt;
84bf9cef
KS
46
47 memset(init_pkt, 0, sizeof(struct nvsp_message));
48 init_pkt->hdr.msg_type = NVSP_MSG4_TYPE_SWITCH_DATA_PATH;
49 if (vf)
50 init_pkt->msg.v4_msg.active_dp.active_datapath =
51 NVSP_DATAPATH_VF;
52 else
53 init_pkt->msg.v4_msg.active_dp.active_datapath =
54 NVSP_DATAPATH_SYNTHETIC;
55
56 vmbus_sendpacket(dev->channel, init_pkt,
57 sizeof(struct nvsp_message),
58 (unsigned long)init_pkt,
59 VM_PKT_DATA_INBAND, 0);
60}
61
88098834 62static struct netvsc_device *alloc_net_device(void)
fceaf24a 63{
85799a37 64 struct netvsc_device *net_device;
fceaf24a 65
85799a37
HZ
66 net_device = kzalloc(sizeof(struct netvsc_device), GFP_KERNEL);
67 if (!net_device)
fceaf24a
HJ
68 return NULL;
69
b8b835a8 70 net_device->chan_table[0].mrc.buf
71 = vzalloc(NETVSC_RECVSLOT_MAX * sizeof(struct recv_comp_data));
c0b558e5 72
dc5cd894 73 init_waitqueue_head(&net_device->wait_drain);
c38b9c71 74 net_device->destroy = false;
84bf9cef 75 atomic_set(&net_device->open_cnt, 0);
7c3877f2
HZ
76 net_device->max_pkt = RNDIS_MAX_PKT_DEFAULT;
77 net_device->pkt_align = RNDIS_PKT_ALIGN_DEFAULT;
fd612602 78 init_completion(&net_device->channel_init_wait);
7c3877f2 79
85799a37 80 return net_device;
fceaf24a
HJ
81}
82
545a8e79 83static void free_netvsc_device(struct rcu_head *head)
f90251c8 84{
545a8e79 85 struct netvsc_device *nvdev
86 = container_of(head, struct netvsc_device, rcu);
c0b558e5
HZ
87 int i;
88
89 for (i = 0; i < VRSS_CHANNEL_MAX; i++)
b8b835a8 90 vfree(nvdev->chan_table[i].mrc.buf);
c0b558e5 91
f90251c8
HZ
92 kfree(nvdev);
93}
94
545a8e79 95static void free_netvsc_device_rcu(struct netvsc_device *nvdev)
96{
97 call_rcu(&nvdev->rcu, free_netvsc_device);
98}
fceaf24a 99
46b4f7f5 100static struct netvsc_device *get_outbound_net_device(struct hv_device *device)
fceaf24a 101{
2625466d 102 struct netvsc_device *net_device = hv_device_to_netvsc_device(device);
9d88f33a 103
46b4f7f5 104 if (net_device && net_device->destroy)
85799a37 105 net_device = NULL;
fceaf24a 106
85799a37 107 return net_device;
fceaf24a
HJ
108}
109
7a2a0a84 110static void netvsc_destroy_buf(struct hv_device *device)
ec91cd09
HZ
111{
112 struct nvsp_message *revoke_packet;
3d541ac5 113 struct net_device *ndev = hv_get_drvdata(device);
2625466d 114 struct netvsc_device *net_device = net_device_to_netvsc_device(ndev);
7a2a0a84 115 int ret;
ec91cd09
HZ
116
117 /*
118 * If we got a section count, it means we received a
119 * SendReceiveBufferComplete msg (ie sent
120 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
121 * to send a revoke msg here
122 */
123 if (net_device->recv_section_cnt) {
124 /* Send the revoke receive buffer */
125 revoke_packet = &net_device->revoke_packet;
126 memset(revoke_packet, 0, sizeof(struct nvsp_message));
127
128 revoke_packet->hdr.msg_type =
129 NVSP_MSG1_TYPE_REVOKE_RECV_BUF;
130 revoke_packet->msg.v1_msg.
131 revoke_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
132
3d541ac5 133 ret = vmbus_sendpacket(device->channel,
ec91cd09
HZ
134 revoke_packet,
135 sizeof(struct nvsp_message),
136 (unsigned long)revoke_packet,
137 VM_PKT_DATA_INBAND, 0);
73e64fa4
S
138 /* If the failure is because the channel is rescinded;
139 * ignore the failure since we cannot send on a rescinded
140 * channel. This would allow us to properly cleanup
141 * even when the channel is rescinded.
142 */
143 if (device->channel->rescind)
144 ret = 0;
ec91cd09
HZ
145 /*
146 * If we failed here, we might as well return and
147 * have a leak rather than continue and a bugchk
148 */
149 if (ret != 0) {
d9871158 150 netdev_err(ndev, "unable to send "
c909ebbd 151 "revoke receive buffer to netvsp\n");
7a2a0a84 152 return;
ec91cd09
HZ
153 }
154 }
155
156 /* Teardown the gpadl on the vsp end */
157 if (net_device->recv_buf_gpadl_handle) {
3d541ac5
VK
158 ret = vmbus_teardown_gpadl(device->channel,
159 net_device->recv_buf_gpadl_handle);
ec91cd09
HZ
160
161 /* If we failed here, we might as well return and have a leak
162 * rather than continue and a bugchk
163 */
164 if (ret != 0) {
d9871158 165 netdev_err(ndev,
c909ebbd 166 "unable to teardown receive buffer's gpadl\n");
7a2a0a84 167 return;
ec91cd09
HZ
168 }
169 net_device->recv_buf_gpadl_handle = 0;
170 }
171
172 if (net_device->recv_buf) {
173 /* Free up the receive buffer */
b679ef73 174 vfree(net_device->recv_buf);
ec91cd09
HZ
175 net_device->recv_buf = NULL;
176 }
177
178 if (net_device->recv_section) {
179 net_device->recv_section_cnt = 0;
180 kfree(net_device->recv_section);
181 net_device->recv_section = NULL;
182 }
183
c25aaf81
KS
184 /* Deal with the send buffer we may have setup.
185 * If we got a send section size, it means we received a
c51ed182
HZ
186 * NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE msg (ie sent
187 * NVSP_MSG1_TYPE_SEND_SEND_BUF msg) therefore, we need
c25aaf81
KS
188 * to send a revoke msg here
189 */
190 if (net_device->send_section_size) {
191 /* Send the revoke receive buffer */
192 revoke_packet = &net_device->revoke_packet;
193 memset(revoke_packet, 0, sizeof(struct nvsp_message));
194
195 revoke_packet->hdr.msg_type =
196 NVSP_MSG1_TYPE_REVOKE_SEND_BUF;
c51ed182
HZ
197 revoke_packet->msg.v1_msg.revoke_send_buf.id =
198 NETVSC_SEND_BUFFER_ID;
c25aaf81 199
3d541ac5 200 ret = vmbus_sendpacket(device->channel,
c25aaf81
KS
201 revoke_packet,
202 sizeof(struct nvsp_message),
203 (unsigned long)revoke_packet,
204 VM_PKT_DATA_INBAND, 0);
73e64fa4
S
205
206 /* If the failure is because the channel is rescinded;
207 * ignore the failure since we cannot send on a rescinded
208 * channel. This would allow us to properly cleanup
209 * even when the channel is rescinded.
210 */
211 if (device->channel->rescind)
212 ret = 0;
213
c25aaf81
KS
214 /* If we failed here, we might as well return and
215 * have a leak rather than continue and a bugchk
216 */
217 if (ret != 0) {
218 netdev_err(ndev, "unable to send "
219 "revoke send buffer to netvsp\n");
7a2a0a84 220 return;
c25aaf81
KS
221 }
222 }
223 /* Teardown the gpadl on the vsp end */
224 if (net_device->send_buf_gpadl_handle) {
3d541ac5 225 ret = vmbus_teardown_gpadl(device->channel,
c25aaf81
KS
226 net_device->send_buf_gpadl_handle);
227
228 /* If we failed here, we might as well return and have a leak
229 * rather than continue and a bugchk
230 */
231 if (ret != 0) {
232 netdev_err(ndev,
233 "unable to teardown send buffer's gpadl\n");
7a2a0a84 234 return;
c25aaf81 235 }
2f18423d 236 net_device->send_buf_gpadl_handle = 0;
c25aaf81
KS
237 }
238 if (net_device->send_buf) {
c51ed182 239 /* Free up the send buffer */
06b47aac 240 vfree(net_device->send_buf);
c25aaf81
KS
241 net_device->send_buf = NULL;
242 }
243 kfree(net_device->send_section_map);
ec91cd09
HZ
244}
245
c25aaf81 246static int netvsc_init_buf(struct hv_device *device)
fceaf24a 247{
21a80820 248 int ret = 0;
85799a37
HZ
249 struct netvsc_device *net_device;
250 struct nvsp_message *init_packet;
2ddd5e5f 251 struct net_device *ndev;
fdfb70d2 252 size_t map_words;
0a726c2b 253 int node;
fceaf24a 254
5a71ae30 255 net_device = get_outbound_net_device(device);
2ddd5e5f 256 if (!net_device)
927bc33c 257 return -ENODEV;
0a1275ca 258 ndev = hv_get_drvdata(device);
fceaf24a 259
0a726c2b
S
260 node = cpu_to_node(device->channel->target_cpu);
261 net_device->recv_buf = vzalloc_node(net_device->recv_buf_size, node);
262 if (!net_device->recv_buf)
263 net_device->recv_buf = vzalloc(net_device->recv_buf_size);
264
53d21fdb 265 if (!net_device->recv_buf) {
d9871158 266 netdev_err(ndev, "unable to allocate receive "
c909ebbd 267 "buffer of size %d\n", net_device->recv_buf_size);
927bc33c 268 ret = -ENOMEM;
0c3b7b2f 269 goto cleanup;
fceaf24a 270 }
fceaf24a 271
454f18a9
BP
272 /*
273 * Establish the gpadl handle for this buffer on this
274 * channel. Note: This call uses the vmbus connection rather
275 * than the channel to establish the gpadl handle.
276 */
53d21fdb
HZ
277 ret = vmbus_establish_gpadl(device->channel, net_device->recv_buf,
278 net_device->recv_buf_size,
279 &net_device->recv_buf_gpadl_handle);
21a80820 280 if (ret != 0) {
d9871158 281 netdev_err(ndev,
c909ebbd 282 "unable to establish receive buffer's gpadl\n");
0c3b7b2f 283 goto cleanup;
fceaf24a
HJ
284 }
285
454f18a9 286 /* Notify the NetVsp of the gpadl handle */
53d21fdb 287 init_packet = &net_device->channel_init_pkt;
fceaf24a 288
85799a37 289 memset(init_packet, 0, sizeof(struct nvsp_message));
fceaf24a 290
53d21fdb
HZ
291 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_RECV_BUF;
292 init_packet->msg.v1_msg.send_recv_buf.
293 gpadl_handle = net_device->recv_buf_gpadl_handle;
294 init_packet->msg.v1_msg.
295 send_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
fceaf24a 296
454f18a9 297 /* Send the gpadl notification request */
85799a37 298 ret = vmbus_sendpacket(device->channel, init_packet,
5a4df290 299 sizeof(struct nvsp_message),
85799a37 300 (unsigned long)init_packet,
415f2287 301 VM_PKT_DATA_INBAND,
5a4df290 302 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
21a80820 303 if (ret != 0) {
d9871158 304 netdev_err(ndev,
c909ebbd 305 "unable to send receive buffer's gpadl to netvsp\n");
0c3b7b2f 306 goto cleanup;
fceaf24a
HJ
307 }
308
5362855a 309 wait_for_completion(&net_device->channel_init_wait);
fceaf24a 310
454f18a9 311 /* Check the response */
53d21fdb
HZ
312 if (init_packet->msg.v1_msg.
313 send_recv_buf_complete.status != NVSP_STAT_SUCCESS) {
d9871158 314 netdev_err(ndev, "Unable to complete receive buffer "
8bff33ab 315 "initialization with NetVsp - status %d\n",
53d21fdb
HZ
316 init_packet->msg.v1_msg.
317 send_recv_buf_complete.status);
927bc33c 318 ret = -EINVAL;
0c3b7b2f 319 goto cleanup;
fceaf24a
HJ
320 }
321
454f18a9 322 /* Parse the response */
fceaf24a 323
53d21fdb
HZ
324 net_device->recv_section_cnt = init_packet->msg.
325 v1_msg.send_recv_buf_complete.num_sections;
fceaf24a 326
c1813200
HZ
327 net_device->recv_section = kmemdup(
328 init_packet->msg.v1_msg.send_recv_buf_complete.sections,
329 net_device->recv_section_cnt *
330 sizeof(struct nvsp_1_receive_buffer_section),
331 GFP_KERNEL);
53d21fdb 332 if (net_device->recv_section == NULL) {
927bc33c 333 ret = -EINVAL;
0c3b7b2f 334 goto cleanup;
fceaf24a
HJ
335 }
336
21a80820
GKH
337 /*
338 * For 1st release, there should only be 1 section that represents the
339 * entire receive buffer
340 */
53d21fdb
HZ
341 if (net_device->recv_section_cnt != 1 ||
342 net_device->recv_section->offset != 0) {
927bc33c 343 ret = -EINVAL;
0c3b7b2f 344 goto cleanup;
fceaf24a
HJ
345 }
346
c25aaf81
KS
347 /* Now setup the send buffer.
348 */
5defde59
S
349 net_device->send_buf = vzalloc_node(net_device->send_buf_size, node);
350 if (!net_device->send_buf)
351 net_device->send_buf = vzalloc(net_device->send_buf_size);
c25aaf81
KS
352 if (!net_device->send_buf) {
353 netdev_err(ndev, "unable to allocate send "
354 "buffer of size %d\n", net_device->send_buf_size);
355 ret = -ENOMEM;
356 goto cleanup;
357 }
358
359 /* Establish the gpadl handle for this buffer on this
360 * channel. Note: This call uses the vmbus connection rather
361 * than the channel to establish the gpadl handle.
362 */
363 ret = vmbus_establish_gpadl(device->channel, net_device->send_buf,
364 net_device->send_buf_size,
365 &net_device->send_buf_gpadl_handle);
366 if (ret != 0) {
367 netdev_err(ndev,
368 "unable to establish send buffer's gpadl\n");
369 goto cleanup;
370 }
371
372 /* Notify the NetVsp of the gpadl handle */
373 init_packet = &net_device->channel_init_pkt;
374 memset(init_packet, 0, sizeof(struct nvsp_message));
375 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_SEND_BUF;
c51ed182 376 init_packet->msg.v1_msg.send_send_buf.gpadl_handle =
c25aaf81 377 net_device->send_buf_gpadl_handle;
c51ed182 378 init_packet->msg.v1_msg.send_send_buf.id = NETVSC_SEND_BUFFER_ID;
c25aaf81
KS
379
380 /* Send the gpadl notification request */
381 ret = vmbus_sendpacket(device->channel, init_packet,
382 sizeof(struct nvsp_message),
383 (unsigned long)init_packet,
384 VM_PKT_DATA_INBAND,
385 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
386 if (ret != 0) {
387 netdev_err(ndev,
388 "unable to send send buffer's gpadl to netvsp\n");
389 goto cleanup;
390 }
391
5362855a 392 wait_for_completion(&net_device->channel_init_wait);
c25aaf81
KS
393
394 /* Check the response */
395 if (init_packet->msg.v1_msg.
396 send_send_buf_complete.status != NVSP_STAT_SUCCESS) {
397 netdev_err(ndev, "Unable to complete send buffer "
398 "initialization with NetVsp - status %d\n",
399 init_packet->msg.v1_msg.
c51ed182 400 send_send_buf_complete.status);
c25aaf81
KS
401 ret = -EINVAL;
402 goto cleanup;
403 }
404
405 /* Parse the response */
406 net_device->send_section_size = init_packet->msg.
407 v1_msg.send_send_buf_complete.section_size;
408
409 /* Section count is simply the size divided by the section size.
410 */
411 net_device->send_section_cnt =
796cc88c 412 net_device->send_buf_size / net_device->send_section_size;
c25aaf81 413
93ba2222
VK
414 netdev_dbg(ndev, "Send section size: %d, Section count:%d\n",
415 net_device->send_section_size, net_device->send_section_cnt);
c25aaf81
KS
416
417 /* Setup state for managing the send buffer. */
fdfb70d2 418 map_words = DIV_ROUND_UP(net_device->send_section_cnt, BITS_PER_LONG);
c25aaf81 419
fdfb70d2 420 net_device->send_section_map = kcalloc(map_words, sizeof(ulong), GFP_KERNEL);
dd1d3f8f
WY
421 if (net_device->send_section_map == NULL) {
422 ret = -ENOMEM;
c25aaf81 423 goto cleanup;
dd1d3f8f 424 }
c25aaf81 425
0c3b7b2f 426 goto exit;
fceaf24a 427
0c3b7b2f 428cleanup:
3d541ac5 429 netvsc_destroy_buf(device);
fceaf24a 430
0c3b7b2f 431exit:
fceaf24a
HJ
432 return ret;
433}
434
f157e78d
HZ
435/* Negotiate NVSP protocol version */
436static int negotiate_nvsp_ver(struct hv_device *device,
437 struct netvsc_device *net_device,
438 struct nvsp_message *init_packet,
439 u32 nvsp_ver)
fceaf24a 440{
0a1275ca 441 struct net_device *ndev = hv_get_drvdata(device);
7390fe9c 442 int ret;
fceaf24a 443
85799a37 444 memset(init_packet, 0, sizeof(struct nvsp_message));
53d21fdb 445 init_packet->hdr.msg_type = NVSP_MSG_TYPE_INIT;
f157e78d
HZ
446 init_packet->msg.init_msg.init.min_protocol_ver = nvsp_ver;
447 init_packet->msg.init_msg.init.max_protocol_ver = nvsp_ver;
fceaf24a 448
454f18a9 449 /* Send the init request */
85799a37 450 ret = vmbus_sendpacket(device->channel, init_packet,
5a4df290 451 sizeof(struct nvsp_message),
85799a37 452 (unsigned long)init_packet,
415f2287 453 VM_PKT_DATA_INBAND,
5a4df290 454 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
21a80820 455
b8a3d52b 456 if (ret != 0)
f157e78d 457 return ret;
fceaf24a 458
5362855a 459 wait_for_completion(&net_device->channel_init_wait);
fceaf24a 460
53d21fdb 461 if (init_packet->msg.init_msg.init_complete.status !=
f157e78d
HZ
462 NVSP_STAT_SUCCESS)
463 return -EINVAL;
fceaf24a 464
a1eabb01 465 if (nvsp_ver == NVSP_PROTOCOL_VERSION_1)
f157e78d
HZ
466 return 0;
467
71790a27 468 /* NVSPv2 or later: Send NDIS config */
f157e78d
HZ
469 memset(init_packet, 0, sizeof(struct nvsp_message));
470 init_packet->hdr.msg_type = NVSP_MSG2_TYPE_SEND_NDIS_CONFIG;
0a1275ca 471 init_packet->msg.v2_msg.send_ndis_config.mtu = ndev->mtu + ETH_HLEN;
1f5f3a75 472 init_packet->msg.v2_msg.send_ndis_config.capability.ieee8021q = 1;
f157e78d 473
7f5d5af0 474 if (nvsp_ver >= NVSP_PROTOCOL_VERSION_5) {
71790a27
HZ
475 init_packet->msg.v2_msg.send_ndis_config.capability.sriov = 1;
476
7f5d5af0
HZ
477 /* Teaming bit is needed to receive link speed updates */
478 init_packet->msg.v2_msg.send_ndis_config.capability.teaming = 1;
479 }
480
f157e78d
HZ
481 ret = vmbus_sendpacket(device->channel, init_packet,
482 sizeof(struct nvsp_message),
483 (unsigned long)init_packet,
484 VM_PKT_DATA_INBAND, 0);
485
486 return ret;
487}
488
489static int netvsc_connect_vsp(struct hv_device *device)
490{
491 int ret;
492 struct netvsc_device *net_device;
493 struct nvsp_message *init_packet;
494 int ndis_version;
e5a78fad
SH
495 const u32 ver_list[] = {
496 NVSP_PROTOCOL_VERSION_1, NVSP_PROTOCOL_VERSION_2,
a1eabb01 497 NVSP_PROTOCOL_VERSION_4, NVSP_PROTOCOL_VERSION_5 };
e5a78fad 498 int i;
f157e78d
HZ
499
500 net_device = get_outbound_net_device(device);
501 if (!net_device)
502 return -ENODEV;
f157e78d
HZ
503
504 init_packet = &net_device->channel_init_pkt;
505
506 /* Negotiate the latest NVSP protocol supported */
e5a78fad 507 for (i = ARRAY_SIZE(ver_list) - 1; i >= 0; i--)
a1eabb01
HZ
508 if (negotiate_nvsp_ver(device, net_device, init_packet,
509 ver_list[i]) == 0) {
510 net_device->nvsp_version = ver_list[i];
511 break;
512 }
513
514 if (i < 0) {
0f48c72c 515 ret = -EPROTO;
0c3b7b2f 516 goto cleanup;
fceaf24a 517 }
f157e78d
HZ
518
519 pr_debug("Negotiated NVSP version:%x\n", net_device->nvsp_version);
520
454f18a9 521 /* Send the ndis version */
85799a37 522 memset(init_packet, 0, sizeof(struct nvsp_message));
fceaf24a 523
a1eabb01 524 if (net_device->nvsp_version <= NVSP_PROTOCOL_VERSION_4)
1f73db49 525 ndis_version = 0x00060001;
a1eabb01
HZ
526 else
527 ndis_version = 0x0006001e;
fceaf24a 528
53d21fdb
HZ
529 init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_NDIS_VER;
530 init_packet->msg.v1_msg.
531 send_ndis_ver.ndis_major_ver =
85799a37 532 (ndis_version & 0xFFFF0000) >> 16;
53d21fdb
HZ
533 init_packet->msg.v1_msg.
534 send_ndis_ver.ndis_minor_ver =
85799a37 535 ndis_version & 0xFFFF;
fceaf24a 536
454f18a9 537 /* Send the init request */
85799a37 538 ret = vmbus_sendpacket(device->channel, init_packet,
0c3b7b2f
S
539 sizeof(struct nvsp_message),
540 (unsigned long)init_packet,
541 VM_PKT_DATA_INBAND, 0);
0f48c72c 542 if (ret != 0)
0c3b7b2f 543 goto cleanup;
454f18a9
BP
544
545 /* Post the big receive buffer to NetVSP */
99d3016d
HZ
546 if (net_device->nvsp_version <= NVSP_PROTOCOL_VERSION_2)
547 net_device->recv_buf_size = NETVSC_RECEIVE_BUFFER_SIZE_LEGACY;
548 else
549 net_device->recv_buf_size = NETVSC_RECEIVE_BUFFER_SIZE;
c25aaf81 550 net_device->send_buf_size = NETVSC_SEND_BUFFER_SIZE;
99d3016d 551
c25aaf81 552 ret = netvsc_init_buf(device);
fceaf24a 553
0c3b7b2f 554cleanup:
fceaf24a
HJ
555 return ret;
556}
557
3d541ac5 558static void netvsc_disconnect_vsp(struct hv_device *device)
fceaf24a 559{
3d541ac5 560 netvsc_destroy_buf(device);
fceaf24a
HJ
561}
562
3e189519 563/*
5a71ae30 564 * netvsc_device_remove - Callback when the root bus device is removed
21a80820 565 */
e08f3ea5 566void netvsc_device_remove(struct hv_device *device)
fceaf24a 567{
3d541ac5
VK
568 struct net_device *ndev = hv_get_drvdata(device);
569 struct net_device_context *net_device_ctx = netdev_priv(ndev);
570 struct netvsc_device *net_device = net_device_ctx->nvdev;
15a863bf 571 int i;
fceaf24a 572
3d541ac5 573 netvsc_disconnect_vsp(device);
9d88f33a 574
545a8e79 575 RCU_INIT_POINTER(net_device_ctx->nvdev, NULL);
3852409b 576
86c921af
S
577 /*
578 * At this point, no one should be accessing net_device
579 * except in here
580 */
93ba2222 581 netdev_dbg(ndev, "net device safe to remove\n");
fceaf24a 582
454f18a9 583 /* Now, we can close the channel safely */
85799a37 584 vmbus_close(device->channel);
fceaf24a 585
76bb5db5 586 /* And dissassociate NAPI context from device */
79cd874c 587 for (i = 0; i < net_device->num_chn; i++)
76bb5db5 588 netif_napi_del(&net_device->chan_table[i].napi);
15a863bf 589
454f18a9 590 /* Release all resources */
545a8e79 591 free_netvsc_device_rcu(net_device);
fceaf24a
HJ
592}
593
33be96e4
HZ
594#define RING_AVAIL_PERCENT_HIWATER 20
595#define RING_AVAIL_PERCENT_LOWATER 10
596
597/*
598 * Get the percentage of available bytes to write in the ring.
599 * The return value is in range from 0 to 100.
600 */
601static inline u32 hv_ringbuf_avail_percent(
602 struct hv_ring_buffer_info *ring_info)
603{
604 u32 avail_read, avail_write;
605
606 hv_get_ringbuffer_availbytes(ring_info, &avail_read, &avail_write);
607
608 return avail_write * 100 / ring_info->ring_datasize;
609}
610
c25aaf81
KS
611static inline void netvsc_free_send_slot(struct netvsc_device *net_device,
612 u32 index)
613{
614 sync_change_bit(index, net_device->send_section_map);
615}
616
bc304dd3
SH
617static void netvsc_send_tx_complete(struct netvsc_device *net_device,
618 struct vmbus_channel *incoming_channel,
619 struct hv_device *device,
f9645430 620 const struct vmpacket_descriptor *desc,
621 int budget)
bc304dd3 622{
50698d80 623 struct sk_buff *skb = (struct sk_buff *)(unsigned long)desc->trans_id;
bc304dd3 624 struct net_device *ndev = hv_get_drvdata(device);
bc304dd3 625 struct vmbus_channel *channel = device->channel;
bc304dd3
SH
626 u16 q_idx = 0;
627 int queue_sends;
628
629 /* Notify the layer above us */
630 if (likely(skb)) {
793e3955 631 const struct hv_netvsc_packet *packet
bc304dd3 632 = (struct hv_netvsc_packet *)skb->cb;
793e3955 633 u32 send_index = packet->send_buf_index;
634 struct netvsc_stats *tx_stats;
bc304dd3
SH
635
636 if (send_index != NETVSC_INVALID_INDEX)
637 netvsc_free_send_slot(net_device, send_index);
793e3955 638 q_idx = packet->q_idx;
bc304dd3
SH
639 channel = incoming_channel;
640
6c80f3fc 641 tx_stats = &net_device->chan_table[q_idx].tx_stats;
793e3955 642
643 u64_stats_update_begin(&tx_stats->syncp);
644 tx_stats->packets += packet->total_packets;
645 tx_stats->bytes += packet->total_bytes;
646 u64_stats_update_end(&tx_stats->syncp);
647
f9645430 648 napi_consume_skb(skb, budget);
bc304dd3
SH
649 }
650
b8b835a8 651 queue_sends =
652 atomic_dec_return(&net_device->chan_table[q_idx].queue_sends);
bc304dd3 653
46b4f7f5 654 if (net_device->destroy && queue_sends == 0)
bc304dd3
SH
655 wake_up(&net_device->wait_drain);
656
657 if (netif_tx_queue_stopped(netdev_get_tx_queue(ndev, q_idx)) &&
bc304dd3
SH
658 (hv_ringbuf_avail_percent(&channel->outbound) > RING_AVAIL_PERCENT_HIWATER ||
659 queue_sends < 1))
660 netif_tx_wake_queue(netdev_get_tx_queue(ndev, q_idx));
661}
662
97c1723a 663static void netvsc_send_completion(struct netvsc_device *net_device,
25b85ee8 664 struct vmbus_channel *incoming_channel,
97c1723a 665 struct hv_device *device,
f9645430 666 const struct vmpacket_descriptor *desc,
667 int budget)
fceaf24a 668{
f3dd3f47 669 struct nvsp_message *nvsp_packet = hv_pkt_data(desc);
3d541ac5 670 struct net_device *ndev = hv_get_drvdata(device);
fceaf24a 671
bc304dd3
SH
672 switch (nvsp_packet->hdr.msg_type) {
673 case NVSP_MSG_TYPE_INIT_COMPLETE:
674 case NVSP_MSG1_TYPE_SEND_RECV_BUF_COMPLETE:
675 case NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE:
676 case NVSP_MSG5_TYPE_SUBCHANNEL:
454f18a9 677 /* Copy the response back */
53d21fdb 678 memcpy(&net_device->channel_init_pkt, nvsp_packet,
21a80820 679 sizeof(struct nvsp_message));
35abb21a 680 complete(&net_device->channel_init_wait);
bc304dd3
SH
681 break;
682
683 case NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE:
684 netvsc_send_tx_complete(net_device, incoming_channel,
f9645430 685 device, desc, budget);
bc304dd3 686 break;
fceaf24a 687
bc304dd3
SH
688 default:
689 netdev_err(ndev,
690 "Unknown send completion type %d received!!\n",
691 nvsp_packet->hdr.msg_type);
fceaf24a 692 }
fceaf24a
HJ
693}
694
c25aaf81
KS
695static u32 netvsc_get_next_send_section(struct netvsc_device *net_device)
696{
b58a1858 697 unsigned long *map_addr = net_device->send_section_map;
698 unsigned int i;
699
fdfb70d2 700 for_each_clear_bit(i, map_addr, net_device->send_section_cnt) {
b58a1858 701 if (sync_test_and_set_bit(i, map_addr) == 0)
702 return i;
c25aaf81 703 }
b58a1858 704
705 return NETVSC_INVALID_INDEX;
c25aaf81
KS
706}
707
da19fcd0
LP
708static u32 netvsc_copy_to_send_buf(struct netvsc_device *net_device,
709 unsigned int section_index,
7c3877f2 710 u32 pend_size,
24476760 711 struct hv_netvsc_packet *packet,
a9f2e2d6 712 struct rndis_message *rndis_msg,
694a9fb0
KS
713 struct hv_page_buffer **pb,
714 struct sk_buff *skb)
c25aaf81
KS
715{
716 char *start = net_device->send_buf;
7c3877f2
HZ
717 char *dest = start + (section_index * net_device->send_section_size)
718 + pend_size;
c25aaf81
KS
719 int i;
720 u32 msg_size = 0;
7c3877f2
HZ
721 u32 padding = 0;
722 u32 remain = packet->total_data_buflen % net_device->pkt_align;
aa0a34be
HZ
723 u32 page_count = packet->cp_partial ? packet->rmsg_pgcnt :
724 packet->page_buf_cnt;
7c3877f2
HZ
725
726 /* Add padding */
ebc1dcf6 727 if (skb->xmit_more && remain && !packet->cp_partial) {
7c3877f2 728 padding = net_device->pkt_align - remain;
24476760 729 rndis_msg->msg_len += padding;
7c3877f2
HZ
730 packet->total_data_buflen += padding;
731 }
c25aaf81 732
aa0a34be 733 for (i = 0; i < page_count; i++) {
a9f2e2d6
KS
734 char *src = phys_to_virt((*pb)[i].pfn << PAGE_SHIFT);
735 u32 offset = (*pb)[i].offset;
736 u32 len = (*pb)[i].len;
c25aaf81
KS
737
738 memcpy(dest, (src + offset), len);
739 msg_size += len;
740 dest += len;
741 }
7c3877f2
HZ
742
743 if (padding) {
744 memset(dest, 0, padding);
745 msg_size += padding;
746 }
747
c25aaf81
KS
748 return msg_size;
749}
750
3a8963ac 751static inline int netvsc_send_pkt(
0a1275ca 752 struct hv_device *device,
7c3877f2 753 struct hv_netvsc_packet *packet,
a9f2e2d6 754 struct netvsc_device *net_device,
3a3d9a0a
KS
755 struct hv_page_buffer **pb,
756 struct sk_buff *skb)
fceaf24a 757{
7c3877f2 758 struct nvsp_message nvmsg;
b8b835a8 759 struct netvsc_channel *nvchan
760 = &net_device->chan_table[packet->q_idx];
761 struct vmbus_channel *out_channel = nvchan->channel;
0a1275ca 762 struct net_device *ndev = hv_get_drvdata(device);
b8b835a8 763 struct netdev_queue *txq = netdev_get_tx_queue(ndev, packet->q_idx);
7c3877f2
HZ
764 u64 req_id;
765 int ret;
aa0a34be 766 struct hv_page_buffer *pgbuf;
82fa3c77 767 u32 ring_avail = hv_ringbuf_avail_percent(&out_channel->outbound);
c25aaf81 768
7c3877f2 769 nvmsg.hdr.msg_type = NVSP_MSG1_TYPE_SEND_RNDIS_PKT;
694a9fb0 770 if (skb != NULL) {
21a80820 771 /* 0 is RMC_DATA; */
7c3877f2 772 nvmsg.msg.v1_msg.send_rndis_pkt.channel_type = 0;
21a80820
GKH
773 } else {
774 /* 1 is RMC_CONTROL; */
7c3877f2 775 nvmsg.msg.v1_msg.send_rndis_pkt.channel_type = 1;
21a80820 776 }
fceaf24a 777
7c3877f2
HZ
778 nvmsg.msg.v1_msg.send_rndis_pkt.send_buf_section_index =
779 packet->send_buf_index;
780 if (packet->send_buf_index == NETVSC_INVALID_INDEX)
781 nvmsg.msg.v1_msg.send_rndis_pkt.send_buf_section_size = 0;
782 else
783 nvmsg.msg.v1_msg.send_rndis_pkt.send_buf_section_size =
784 packet->total_data_buflen;
21a80820 785
3a3d9a0a 786 req_id = (ulong)skb;
f1ea3cd7 787
c3582a2c
HZ
788 if (out_channel->rescind)
789 return -ENODEV;
790
72a2f5bd 791 if (packet->page_buf_cnt) {
a9f2e2d6
KS
792 pgbuf = packet->cp_partial ? (*pb) +
793 packet->rmsg_pgcnt : (*pb);
82fa3c77
KS
794 ret = vmbus_sendpacket_pagebuffer_ctl(out_channel,
795 pgbuf,
796 packet->page_buf_cnt,
797 &nvmsg,
798 sizeof(struct nvsp_message),
799 req_id,
3454323c 800 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
21a80820 801 } else {
82fa3c77
KS
802 ret = vmbus_sendpacket_ctl(out_channel, &nvmsg,
803 sizeof(struct nvsp_message),
804 req_id,
805 VM_PKT_DATA_INBAND,
3454323c 806 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
fceaf24a
HJ
807 }
808
1d06825b 809 if (ret == 0) {
b8b835a8 810 atomic_inc_return(&nvchan->queue_sends);
5b54dac8 811
46b4f7f5 812 if (ring_avail < RING_AVAIL_PERCENT_LOWATER)
b8b835a8 813 netif_tx_stop_queue(txq);
1d06825b 814 } else if (ret == -EAGAIN) {
b8b835a8 815 netif_tx_stop_queue(txq);
816 if (atomic_read(&nvchan->queue_sends) < 1) {
817 netif_tx_wake_queue(txq);
33be96e4
HZ
818 ret = -ENOSPC;
819 }
1d06825b 820 } else {
d9871158 821 netdev_err(ndev, "Unable to send packet %p ret %d\n",
85799a37 822 packet, ret);
1d06825b 823 }
fceaf24a 824
7c3877f2
HZ
825 return ret;
826}
827
c85e4924
HZ
828/* Move packet out of multi send data (msd), and clear msd */
829static inline void move_pkt_msd(struct hv_netvsc_packet **msd_send,
830 struct sk_buff **msd_skb,
831 struct multi_send_data *msdp)
832{
833 *msd_skb = msdp->skb;
834 *msd_send = msdp->pkt;
835 msdp->skb = NULL;
836 msdp->pkt = NULL;
837 msdp->count = 0;
838}
839
7c3877f2 840int netvsc_send(struct hv_device *device,
24476760 841 struct hv_netvsc_packet *packet,
a9f2e2d6 842 struct rndis_message *rndis_msg,
3a3d9a0a
KS
843 struct hv_page_buffer **pb,
844 struct sk_buff *skb)
7c3877f2
HZ
845{
846 struct netvsc_device *net_device;
6c4c137e 847 int ret = 0;
b8b835a8 848 struct netvsc_channel *nvchan;
7c3877f2
HZ
849 u32 pktlen = packet->total_data_buflen, msd_len = 0;
850 unsigned int section_index = NETVSC_INVALID_INDEX;
7c3877f2
HZ
851 struct multi_send_data *msdp;
852 struct hv_netvsc_packet *msd_send = NULL, *cur_send = NULL;
c85e4924 853 struct sk_buff *msd_skb = NULL;
aa0a34be 854 bool try_batch;
bde79be5 855 bool xmit_more = (skb != NULL) ? skb->xmit_more : false;
7c3877f2
HZ
856
857 net_device = get_outbound_net_device(device);
858 if (!net_device)
859 return -ENODEV;
860
e8f0a89c
VK
861 /* We may race with netvsc_connect_vsp()/netvsc_init_buf() and get
862 * here before the negotiation with the host is finished and
863 * send_section_map may not be allocated yet.
864 */
865 if (!net_device->send_section_map)
866 return -EAGAIN;
867
b8b835a8 868 nvchan = &net_device->chan_table[packet->q_idx];
7c3877f2 869 packet->send_buf_index = NETVSC_INVALID_INDEX;
aa0a34be 870 packet->cp_partial = false;
7c3877f2 871
cf8190e4
HZ
872 /* Send control message directly without accessing msd (Multi-Send
873 * Data) field which may be changed during data packet processing.
874 */
875 if (!skb) {
876 cur_send = packet;
877 goto send_now;
878 }
879
7c3877f2 880 /* batch packets in send buffer if possible */
b8b835a8 881 msdp = &nvchan->msd;
7c3877f2
HZ
882 if (msdp->pkt)
883 msd_len = msdp->pkt->total_data_buflen;
884
ebc1dcf6 885 try_batch = msd_len > 0 && msdp->count < net_device->max_pkt;
aa0a34be 886 if (try_batch && msd_len + pktlen + net_device->pkt_align <
7c3877f2
HZ
887 net_device->send_section_size) {
888 section_index = msdp->pkt->send_buf_index;
889
aa0a34be
HZ
890 } else if (try_batch && msd_len + packet->rmsg_size <
891 net_device->send_section_size) {
892 section_index = msdp->pkt->send_buf_index;
893 packet->cp_partial = true;
894
ebc1dcf6 895 } else if (pktlen + net_device->pkt_align <
7c3877f2
HZ
896 net_device->send_section_size) {
897 section_index = netvsc_get_next_send_section(net_device);
898 if (section_index != NETVSC_INVALID_INDEX) {
c85e4924
HZ
899 move_pkt_msd(&msd_send, &msd_skb, msdp);
900 msd_len = 0;
7c3877f2
HZ
901 }
902 }
903
904 if (section_index != NETVSC_INVALID_INDEX) {
905 netvsc_copy_to_send_buf(net_device,
906 section_index, msd_len,
694a9fb0 907 packet, rndis_msg, pb, skb);
b08cc791 908
7c3877f2 909 packet->send_buf_index = section_index;
aa0a34be
HZ
910
911 if (packet->cp_partial) {
912 packet->page_buf_cnt -= packet->rmsg_pgcnt;
913 packet->total_data_buflen = msd_len + packet->rmsg_size;
914 } else {
915 packet->page_buf_cnt = 0;
916 packet->total_data_buflen += msd_len;
aa0a34be 917 }
7c3877f2 918
793e3955 919 if (msdp->pkt) {
920 packet->total_packets += msdp->pkt->total_packets;
921 packet->total_bytes += msdp->pkt->total_bytes;
922 }
923
c85e4924 924 if (msdp->skb)
17db4bce 925 dev_consume_skb_any(msdp->skb);
ee90b812 926
bde79be5 927 if (xmit_more && !packet->cp_partial) {
c85e4924 928 msdp->skb = skb;
7c3877f2
HZ
929 msdp->pkt = packet;
930 msdp->count++;
931 } else {
932 cur_send = packet;
c85e4924 933 msdp->skb = NULL;
7c3877f2
HZ
934 msdp->pkt = NULL;
935 msdp->count = 0;
936 }
937 } else {
c85e4924 938 move_pkt_msd(&msd_send, &msd_skb, msdp);
7c3877f2
HZ
939 cur_send = packet;
940 }
941
7c3877f2 942 if (msd_send) {
6c4c137e
SH
943 int m_ret = netvsc_send_pkt(device, msd_send, net_device,
944 NULL, msd_skb);
7c3877f2
HZ
945
946 if (m_ret != 0) {
947 netvsc_free_send_slot(net_device,
948 msd_send->send_buf_index);
c85e4924 949 dev_kfree_skb_any(msd_skb);
7c3877f2
HZ
950 }
951 }
952
cf8190e4 953send_now:
7c3877f2 954 if (cur_send)
0a1275ca 955 ret = netvsc_send_pkt(device, cur_send, net_device, pb, skb);
7c3877f2 956
7aab5159
JS
957 if (ret != 0 && section_index != NETVSC_INVALID_INDEX)
958 netvsc_free_send_slot(net_device, section_index);
d953ca4d 959
fceaf24a
HJ
960 return ret;
961}
962
c0b558e5
HZ
963static int netvsc_send_recv_completion(struct vmbus_channel *channel,
964 u64 transaction_id, u32 status)
5fa9d3c5
HZ
965{
966 struct nvsp_message recvcompMessage;
5fa9d3c5
HZ
967 int ret;
968
969 recvcompMessage.hdr.msg_type =
970 NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE;
971
63f6921d 972 recvcompMessage.msg.v1_msg.send_rndis_pkt_complete.status = status;
5fa9d3c5 973
5fa9d3c5 974 /* Send the completion */
5b54dac8 975 ret = vmbus_sendpacket(channel, &recvcompMessage,
c0b558e5
HZ
976 sizeof(struct nvsp_message_header) + sizeof(u32),
977 transaction_id, VM_PKT_COMP, 0);
978
979 return ret;
980}
981
982static inline void count_recv_comp_slot(struct netvsc_device *nvdev, u16 q_idx,
983 u32 *filled, u32 *avail)
984{
b8b835a8 985 struct multi_recv_comp *mrc = &nvdev->chan_table[q_idx].mrc;
986 u32 first = mrc->first;
987 u32 next = mrc->next;
c0b558e5
HZ
988
989 *filled = (first > next) ? NETVSC_RECVSLOT_MAX - first + next :
990 next - first;
991
992 *avail = NETVSC_RECVSLOT_MAX - *filled - 1;
993}
994
995/* Read the first filled slot, no change to index */
996static inline struct recv_comp_data *read_recv_comp_slot(struct netvsc_device
997 *nvdev, u16 q_idx)
998{
b8b835a8 999 struct multi_recv_comp *mrc = &nvdev->chan_table[q_idx].mrc;
c0b558e5
HZ
1000 u32 filled, avail;
1001
b8b835a8 1002 if (unlikely(!mrc->buf))
c0b558e5
HZ
1003 return NULL;
1004
1005 count_recv_comp_slot(nvdev, q_idx, &filled, &avail);
1006 if (!filled)
1007 return NULL;
1008
b8b835a8 1009 return mrc->buf + mrc->first * sizeof(struct recv_comp_data);
c0b558e5
HZ
1010}
1011
1012/* Put the first filled slot back to available pool */
1013static inline void put_recv_comp_slot(struct netvsc_device *nvdev, u16 q_idx)
1014{
b8b835a8 1015 struct multi_recv_comp *mrc = &nvdev->chan_table[q_idx].mrc;
c0b558e5
HZ
1016 int num_recv;
1017
b8b835a8 1018 mrc->first = (mrc->first + 1) % NETVSC_RECVSLOT_MAX;
c0b558e5
HZ
1019
1020 num_recv = atomic_dec_return(&nvdev->num_outstanding_recvs);
1021
1022 if (nvdev->destroy && num_recv == 0)
1023 wake_up(&nvdev->wait_drain);
1024}
1025
1026/* Check and send pending recv completions */
1027static void netvsc_chk_recv_comp(struct netvsc_device *nvdev,
1028 struct vmbus_channel *channel, u16 q_idx)
1029{
1030 struct recv_comp_data *rcd;
1031 int ret;
1032
1033 while (true) {
1034 rcd = read_recv_comp_slot(nvdev, q_idx);
1035 if (!rcd)
1036 break;
1037
1038 ret = netvsc_send_recv_completion(channel, rcd->tid,
1039 rcd->status);
1040 if (ret)
1041 break;
1042
1043 put_recv_comp_slot(nvdev, q_idx);
5fa9d3c5
HZ
1044 }
1045}
1046
c0b558e5
HZ
1047#define NETVSC_RCD_WATERMARK 80
1048
1049/* Get next available slot */
1050static inline struct recv_comp_data *get_recv_comp_slot(
1051 struct netvsc_device *nvdev, struct vmbus_channel *channel, u16 q_idx)
1052{
b8b835a8 1053 struct multi_recv_comp *mrc = &nvdev->chan_table[q_idx].mrc;
c0b558e5
HZ
1054 u32 filled, avail, next;
1055 struct recv_comp_data *rcd;
1056
b8b835a8 1057 if (unlikely(!nvdev->recv_section))
c0b558e5
HZ
1058 return NULL;
1059
b8b835a8 1060 if (unlikely(!mrc->buf))
c0b558e5
HZ
1061 return NULL;
1062
1063 if (atomic_read(&nvdev->num_outstanding_recvs) >
1064 nvdev->recv_section->num_sub_allocs * NETVSC_RCD_WATERMARK / 100)
1065 netvsc_chk_recv_comp(nvdev, channel, q_idx);
1066
1067 count_recv_comp_slot(nvdev, q_idx, &filled, &avail);
1068 if (!avail)
1069 return NULL;
1070
b8b835a8 1071 next = mrc->next;
1072 rcd = mrc->buf + next * sizeof(struct recv_comp_data);
1073 mrc->next = (next + 1) % NETVSC_RECVSLOT_MAX;
c0b558e5
HZ
1074
1075 atomic_inc(&nvdev->num_outstanding_recvs);
1076
1077 return rcd;
1078}
1079
15a863bf 1080static int netvsc_receive(struct net_device *ndev,
dc54a08c 1081 struct netvsc_device *net_device,
1082 struct net_device_context *net_device_ctx,
1083 struct hv_device *device,
1084 struct vmbus_channel *channel,
f3dd3f47 1085 const struct vmpacket_descriptor *desc,
dc54a08c 1086 struct nvsp_message *nvsp)
fceaf24a 1087{
f3dd3f47 1088 const struct vmtransfer_page_packet_header *vmxferpage_packet
1089 = container_of(desc, const struct vmtransfer_page_packet_header, d);
15a863bf 1090 u16 q_idx = channel->offermsg.offer.sub_channel_index;
dc54a08c 1091 char *recv_buf = net_device->recv_buf;
4baab261 1092 u32 status = NVSP_STAT_SUCCESS;
45326342
HZ
1093 int i;
1094 int count = 0;
c0b558e5 1095 int ret;
779b4d17 1096
454f18a9 1097 /* Make sure this is a valid nvsp packet */
dc54a08c 1098 if (unlikely(nvsp->hdr.msg_type != NVSP_MSG1_TYPE_SEND_RNDIS_PKT)) {
1099 netif_err(net_device_ctx, rx_err, ndev,
1100 "Unknown nvsp packet type received %u\n",
1101 nvsp->hdr.msg_type);
15a863bf 1102 return 0;
fceaf24a
HJ
1103 }
1104
dc54a08c 1105 if (unlikely(vmxferpage_packet->xfer_pageset_id != NETVSC_RECEIVE_BUFFER_ID)) {
1106 netif_err(net_device_ctx, rx_err, ndev,
1107 "Invalid xfer page set id - expecting %x got %x\n",
1108 NETVSC_RECEIVE_BUFFER_ID,
1109 vmxferpage_packet->xfer_pageset_id);
15a863bf 1110 return 0;
fceaf24a
HJ
1111 }
1112
4baab261 1113 count = vmxferpage_packet->range_cnt;
fceaf24a 1114
454f18a9 1115 /* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */
4baab261 1116 for (i = 0; i < count; i++) {
dc54a08c 1117 void *data = recv_buf
1118 + vmxferpage_packet->ranges[i].byte_offset;
1119 u32 buflen = vmxferpage_packet->ranges[i].byte_count;
fceaf24a 1120
454f18a9 1121 /* Pass it to the upper layer */
dc54a08c 1122 status = rndis_filter_receive(ndev, net_device, device,
1123 channel, data, buflen);
fceaf24a
HJ
1124 }
1125
15a863bf 1126 if (net_device->chan_table[q_idx].mrc.buf) {
1127 struct recv_comp_data *rcd;
1128
1129 rcd = get_recv_comp_slot(net_device, channel, q_idx);
1130 if (rcd) {
1131 rcd->tid = vmxferpage_packet->d.trans_id;
1132 rcd->status = status;
1133 } else {
1134 netdev_err(ndev, "Recv_comp full buf q:%hd, tid:%llx\n",
1135 q_idx, vmxferpage_packet->d.trans_id);
1136 }
1137 } else {
c0b558e5
HZ
1138 ret = netvsc_send_recv_completion(channel,
1139 vmxferpage_packet->d.trans_id,
1140 status);
1141 if (ret)
1142 netdev_err(ndev, "Recv_comp q:%hd, tid:%llx, err:%d\n",
1143 q_idx, vmxferpage_packet->d.trans_id, ret);
c0b558e5 1144 }
15a863bf 1145 return count;
fceaf24a
HJ
1146}
1147
5b54dac8 1148static void netvsc_send_table(struct hv_device *hdev,
71790a27 1149 struct nvsp_message *nvmsg)
5b54dac8 1150{
0a1275ca 1151 struct net_device *ndev = hv_get_drvdata(hdev);
7ce10124 1152 struct net_device_context *net_device_ctx = netdev_priv(ndev);
5b54dac8
HZ
1153 int i;
1154 u32 count, *tab;
1155
5b54dac8
HZ
1156 count = nvmsg->msg.v5_msg.send_table.count;
1157 if (count != VRSS_SEND_TAB_SIZE) {
1158 netdev_err(ndev, "Received wrong send-table size:%u\n", count);
1159 return;
1160 }
1161
1162 tab = (u32 *)((unsigned long)&nvmsg->msg.v5_msg.send_table +
1163 nvmsg->msg.v5_msg.send_table.offset);
1164
1165 for (i = 0; i < count; i++)
7ce10124 1166 net_device_ctx->tx_send_table[i] = tab[i];
5b54dac8
HZ
1167}
1168
f9a7da91 1169static void netvsc_send_vf(struct net_device_context *net_device_ctx,
71790a27
HZ
1170 struct nvsp_message *nvmsg)
1171{
f9a7da91
VK
1172 net_device_ctx->vf_alloc = nvmsg->msg.v4_msg.vf_assoc.allocated;
1173 net_device_ctx->vf_serial = nvmsg->msg.v4_msg.vf_assoc.serial;
71790a27
HZ
1174}
1175
1176static inline void netvsc_receive_inband(struct hv_device *hdev,
f9a7da91
VK
1177 struct net_device_context *net_device_ctx,
1178 struct nvsp_message *nvmsg)
71790a27
HZ
1179{
1180 switch (nvmsg->hdr.msg_type) {
1181 case NVSP_MSG5_TYPE_SEND_INDIRECTION_TABLE:
1182 netvsc_send_table(hdev, nvmsg);
1183 break;
1184
1185 case NVSP_MSG4_TYPE_SEND_VF_ASSOCIATION:
f9a7da91 1186 netvsc_send_vf(net_device_ctx, nvmsg);
71790a27
HZ
1187 break;
1188 }
1189}
1190
15a863bf 1191static int netvsc_process_raw_pkt(struct hv_device *device,
1192 struct vmbus_channel *channel,
1193 struct netvsc_device *net_device,
1194 struct net_device *ndev,
f9645430 1195 const struct vmpacket_descriptor *desc,
1196 int budget)
99a50bb1 1197{
f9a7da91 1198 struct net_device_context *net_device_ctx = netdev_priv(ndev);
f3dd3f47 1199 struct nvsp_message *nvmsg = hv_pkt_data(desc);
99a50bb1
S
1200
1201 switch (desc->type) {
1202 case VM_PKT_COMP:
f9645430 1203 netvsc_send_completion(net_device, channel, device,
1204 desc, budget);
99a50bb1
S
1205 break;
1206
1207 case VM_PKT_DATA_USING_XFER_PAGES:
15a863bf 1208 return netvsc_receive(ndev, net_device, net_device_ctx,
1209 device, channel, desc, nvmsg);
99a50bb1
S
1210 break;
1211
1212 case VM_PKT_DATA_INBAND:
f9a7da91 1213 netvsc_receive_inband(device, net_device_ctx, nvmsg);
99a50bb1
S
1214 break;
1215
1216 default:
1217 netdev_err(ndev, "unhandled packet type %d, tid %llx\n",
f4f1c23d 1218 desc->type, desc->trans_id);
99a50bb1
S
1219 break;
1220 }
15a863bf 1221
1222 return 0;
1223}
1224
1225static struct hv_device *netvsc_channel_to_device(struct vmbus_channel *channel)
1226{
1227 struct vmbus_channel *primary = channel->primary_channel;
1228
1229 return primary ? primary->device_obj : channel->device_obj;
1230}
1231
262b7f14 1232/* Network processing softirq
1233 * Process data in incoming ring buffer from host
1234 * Stops when ring is empty or budget is met or exceeded.
1235 */
15a863bf 1236int netvsc_poll(struct napi_struct *napi, int budget)
1237{
1238 struct netvsc_channel *nvchan
1239 = container_of(napi, struct netvsc_channel, napi);
1240 struct vmbus_channel *channel = nvchan->channel;
1241 struct hv_device *device = netvsc_channel_to_device(channel);
1242 u16 q_idx = channel->offermsg.offer.sub_channel_index;
1243 struct net_device *ndev = hv_get_drvdata(device);
1244 struct netvsc_device *net_device = net_device_to_netvsc_device(ndev);
15a863bf 1245 int work_done = 0;
1246
f4f1c23d 1247 /* If starting a new interval */
1248 if (!nvchan->desc)
1249 nvchan->desc = hv_pkt_iter_first(channel);
15a863bf 1250
f4f1c23d 1251 while (nvchan->desc && work_done < budget) {
1252 work_done += netvsc_process_raw_pkt(device, channel, net_device,
f9645430 1253 ndev, nvchan->desc, budget);
f4f1c23d 1254 nvchan->desc = hv_pkt_iter_next(channel, nvchan->desc);
15a863bf 1255 }
15a863bf 1256
f4f1c23d 1257 /* If receive ring was exhausted
1258 * and not doing busy poll
262b7f14 1259 * then re-enable host interrupts
1260 * and reschedule if ring is not empty.
1261 */
15a863bf 1262 if (work_done < budget &&
1263 napi_complete_done(napi, work_done) &&
1264 hv_end_read(&channel->inbound) != 0)
1265 napi_reschedule(napi);
1266
1267 netvsc_chk_recv_comp(net_device, channel, q_idx);
f4f1c23d 1268
1269 /* Driver may overshoot since multiple packets per descriptor */
1270 return min(work_done, budget);
99a50bb1
S
1271}
1272
262b7f14 1273/* Call back when data is available in host ring buffer.
1274 * Processing is deferred until network softirq (NAPI)
1275 */
5b54dac8 1276void netvsc_channel_cb(void *context)
fceaf24a 1277{
6de38af6 1278 struct netvsc_channel *nvchan = context;
0b307ebd 1279
f4f1c23d 1280 if (napi_schedule_prep(&nvchan->napi)) {
1281 /* disable interupts from host */
1282 hv_begin_read(&nvchan->channel->inbound);
0d6dd357 1283
f4f1c23d 1284 __napi_schedule(&nvchan->napi);
1285 }
fceaf24a 1286}
af24ce42 1287
b637e023
HZ
1288/*
1289 * netvsc_device_add - Callback when the device belonging to this
1290 * driver is added
1291 */
2c7f83ca 1292int netvsc_device_add(struct hv_device *device,
1293 const struct netvsc_device_info *device_info)
b637e023 1294{
88098834 1295 int i, ret = 0;
2c7f83ca 1296 int ring_size = device_info->ring_size;
b637e023 1297 struct netvsc_device *net_device;
88098834
VK
1298 struct net_device *ndev = hv_get_drvdata(device);
1299 struct net_device_context *net_device_ctx = netdev_priv(ndev);
b637e023 1300
88098834 1301 net_device = alloc_net_device();
b1c84927
DC
1302 if (!net_device)
1303 return -ENOMEM;
b637e023 1304
5b54dac8
HZ
1305 net_device->ring_size = ring_size;
1306
15a863bf 1307 /* Because the device uses NAPI, all the interrupt batching and
1308 * control is done via Net softirq, not the channel handling
1309 */
1310 set_channel_read_mode(device->channel, HV_CALL_ISR);
1311
bffb1842
S
1312 /* If we're reopening the device we may have multiple queues, fill the
1313 * chn_table with the default channel to use it before subchannels are
1314 * opened.
1315 * Initialize the channel state before we open;
1316 * we can be interrupted as soon as we open the channel.
1317 */
1318
1319 for (i = 0; i < VRSS_CHANNEL_MAX; i++) {
1320 struct netvsc_channel *nvchan = &net_device->chan_table[i];
1321
1322 nvchan->channel = device->channel;
bffb1842
S
1323 }
1324
b637e023 1325 /* Open the channel */
aae23986
S
1326 ret = vmbus_open(device->channel, ring_size * PAGE_SIZE,
1327 ring_size * PAGE_SIZE, NULL, 0,
6de38af6 1328 netvsc_channel_cb,
1329 net_device->chan_table);
b637e023
HZ
1330
1331 if (ret != 0) {
d9871158 1332 netdev_err(ndev, "unable to open channel: %d\n", ret);
b637e023
HZ
1333 goto cleanup;
1334 }
1335
1336 /* Channel is opened */
93ba2222 1337 netdev_dbg(ndev, "hv_netvsc channel opened successfully\n");
b637e023 1338
15a863bf 1339 /* Enable NAPI handler for init callbacks */
76bb5db5 1340 netif_napi_add(ndev, &net_device->chan_table[0].napi,
1341 netvsc_poll, NAPI_POLL_WEIGHT);
15a863bf 1342 napi_enable(&net_device->chan_table[0].napi);
88098834
VK
1343
1344 /* Writing nvdev pointer unlocks netvsc_send(), make sure chn_table is
1345 * populated.
1346 */
545a8e79 1347 rcu_assign_pointer(net_device_ctx->nvdev, net_device);
5b54dac8 1348
b637e023
HZ
1349 /* Connect with the NetVsp */
1350 ret = netvsc_connect_vsp(device);
1351 if (ret != 0) {
d9871158 1352 netdev_err(ndev,
c909ebbd 1353 "unable to connect to NetVSP - %d\n", ret);
b637e023
HZ
1354 goto close;
1355 }
1356
1357 return ret;
1358
1359close:
76bb5db5 1360 netif_napi_del(&net_device->chan_table[0].napi);
15a863bf 1361
b637e023
HZ
1362 /* Now, we can close the channel safely */
1363 vmbus_close(device->channel);
1364
1365cleanup:
545a8e79 1366 free_netvsc_device(&net_device->rcu);
b637e023
HZ
1367
1368 return ret;
1369}