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