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