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