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