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