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