2 * Copyright (c) 2009, Microsoft Corporation.
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.
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
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 * Place - Suite 330, Boston, MA 02111-1307 USA.
18 * Haiyang Zhang <haiyangz@microsoft.com>
19 * Hank Janssen <hjanssen@microsoft.com>
21 #include <linux/kernel.h>
22 #include <linux/sched.h>
23 #include <linux/wait.h>
25 #include <linux/delay.h>
27 #include <linux/slab.h>
31 #include "rndis_filter.h"
36 static const char *driver_name
= "netvsc";
38 /* {F8615163-DF3E-46c5-913F-F2D2F965ED0E} */
39 static const struct hv_guid netvsc_device_type
= {
41 0x63, 0x51, 0x61, 0xF8, 0x3E, 0xDF, 0xc5, 0x46,
42 0x91, 0x3F, 0xF2, 0xD2, 0xF9, 0x65, 0xED, 0x0E
46 static int netvsc_device_add(struct hv_device
*device
, void *additional_info
);
48 static int netvsc_device_remove(struct hv_device
*device
);
50 static void netvsc_cleanup(struct hv_driver
*driver
);
52 static void netvsc_channel_cb(void *context
);
54 static int netvsc_init_send_buf(struct hv_device
*device
);
56 static int netvsc_init_recv_buf(struct hv_device
*device
);
58 static int netvsc_destroy_send_buf(struct netvsc_device
*net_device
);
60 static int netvsc_destroy_recv_buf(struct netvsc_device
*net_device
);
62 static int netvsc_connect_vsp(struct hv_device
*device
);
64 static void netvsc_send_completion(struct hv_device
*device
,
65 struct vmpacket_descriptor
*packet
);
67 static int netvsc_send(struct hv_device
*device
,
68 struct hv_netvsc_packet
*packet
);
70 static void netvsc_receive(struct hv_device
*device
,
71 struct vmpacket_descriptor
*packet
);
73 static void netvsc_receive_completion(void *context
);
75 static void netvsc_send_recv_completion(struct hv_device
*device
,
79 static struct netvsc_device
*alloc_net_device(struct hv_device
*device
)
81 struct netvsc_device
*net_device
;
83 net_device
= kzalloc(sizeof(struct netvsc_device
), GFP_KERNEL
);
87 /* Set to 2 to allow both inbound and outbound traffic */
88 atomic_cmpxchg(&net_device
->refcnt
, 0, 2);
90 net_device
->dev
= device
;
91 device
->ext
= net_device
;
96 static void free_net_device(struct netvsc_device
*device
)
98 WARN_ON(atomic_read(&device
->refcnt
) != 0);
99 device
->dev
->ext
= NULL
;
104 /* Get the net device object iff exists and its refcount > 1 */
105 static struct netvsc_device
*get_outbound_net_device(struct hv_device
*device
)
107 struct netvsc_device
*net_device
;
109 net_device
= device
->ext
;
110 if (net_device
&& atomic_read(&net_device
->refcnt
) > 1)
111 atomic_inc(&net_device
->refcnt
);
118 /* Get the net device object iff exists and its refcount > 0 */
119 static struct netvsc_device
*get_inbound_net_device(struct hv_device
*device
)
121 struct netvsc_device
*net_device
;
123 net_device
= device
->ext
;
124 if (net_device
&& atomic_read(&net_device
->refcnt
))
125 atomic_inc(&net_device
->refcnt
);
132 static void put_net_device(struct hv_device
*device
)
134 struct netvsc_device
*net_device
;
136 net_device
= device
->ext
;
138 atomic_dec(&net_device
->refcnt
);
141 static struct netvsc_device
*release_outbound_net_device(
142 struct hv_device
*device
)
144 struct netvsc_device
*net_device
;
146 net_device
= device
->ext
;
147 if (net_device
== NULL
)
150 /* Busy wait until the ref drop to 2, then set it to 1 */
151 while (atomic_cmpxchg(&net_device
->refcnt
, 2, 1) != 2)
157 static struct netvsc_device
*release_inbound_net_device(
158 struct hv_device
*device
)
160 struct netvsc_device
*net_device
;
162 net_device
= device
->ext
;
163 if (net_device
== NULL
)
166 /* Busy wait until the ref drop to 1, then set it to 0 */
167 while (atomic_cmpxchg(&net_device
->refcnt
, 1, 0) != 1)
175 * netvsc_initialize - Main entry point
177 int netvsc_initialize(struct hv_driver
*drv
)
179 struct netvsc_driver
*driver
= (struct netvsc_driver
*)drv
;
181 DPRINT_DBG(NETVSC
, "sizeof(struct hv_netvsc_packet)=%zd, "
182 "sizeof(struct nvsp_message)=%zd, "
183 "sizeof(struct vmtransfer_page_packet_header)=%zd",
184 sizeof(struct hv_netvsc_packet
),
185 sizeof(struct nvsp_message
),
186 sizeof(struct vmtransfer_page_packet_header
));
188 drv
->name
= driver_name
;
189 memcpy(&drv
->dev_type
, &netvsc_device_type
, sizeof(struct hv_guid
));
191 /* Setup the dispatch table */
192 driver
->base
.dev_add
= netvsc_device_add
;
193 driver
->base
.dev_rm
= netvsc_device_remove
;
194 driver
->base
.cleanup
= netvsc_cleanup
;
196 driver
->send
= netvsc_send
;
198 rndis_filter_init(driver
);
202 static int netvsc_init_recv_buf(struct hv_device
*device
)
205 struct netvsc_device
*net_device
;
206 struct nvsp_message
*init_packet
;
208 net_device
= get_outbound_net_device(device
);
210 DPRINT_ERR(NETVSC
, "unable to get net device..."
211 "device being destroyed?");
215 net_device
->recv_buf
=
216 (void *)__get_free_pages(GFP_KERNEL
|__GFP_ZERO
,
217 get_order(net_device
->recv_buf_size
));
218 if (!net_device
->recv_buf
) {
220 "unable to allocate receive buffer of size %d",
221 net_device
->recv_buf_size
);
226 DPRINT_INFO(NETVSC
, "Establishing receive buffer's GPADL...");
229 * Establish the gpadl handle for this buffer on this
230 * channel. Note: This call uses the vmbus connection rather
231 * than the channel to establish the gpadl handle.
233 ret
= vmbus_establish_gpadl(device
->channel
, net_device
->recv_buf
,
234 net_device
->recv_buf_size
,
235 &net_device
->recv_buf_gpadl_handle
);
238 "unable to establish receive buffer's gpadl");
243 /* Notify the NetVsp of the gpadl handle */
244 DPRINT_INFO(NETVSC
, "Sending NvspMessage1TypeSendReceiveBuffer...");
246 init_packet
= &net_device
->channel_init_pkt
;
248 memset(init_packet
, 0, sizeof(struct nvsp_message
));
250 init_packet
->hdr
.msg_type
= NVSP_MSG1_TYPE_SEND_RECV_BUF
;
251 init_packet
->msg
.v1_msg
.send_recv_buf
.
252 gpadl_handle
= net_device
->recv_buf_gpadl_handle
;
253 init_packet
->msg
.v1_msg
.
254 send_recv_buf
.id
= NETVSC_RECEIVE_BUFFER_ID
;
256 /* Send the gpadl notification request */
257 net_device
->wait_condition
= 0;
258 ret
= vmbus_sendpacket(device
->channel
, init_packet
,
259 sizeof(struct nvsp_message
),
260 (unsigned long)init_packet
,
262 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
265 "unable to send receive buffer's gpadl to netvsp");
269 wait_event_timeout(net_device
->channel_init_wait
,
270 net_device
->wait_condition
,
271 msecs_to_jiffies(1000));
272 BUG_ON(net_device
->wait_condition
== 0);
275 /* Check the response */
276 if (init_packet
->msg
.v1_msg
.
277 send_recv_buf_complete
.status
!= NVSP_STAT_SUCCESS
) {
278 DPRINT_ERR(NETVSC
, "Unable to complete receive buffer "
279 "initialzation with NetVsp - status %d",
280 init_packet
->msg
.v1_msg
.
281 send_recv_buf_complete
.status
);
286 /* Parse the response */
288 net_device
->recv_section_cnt
= init_packet
->msg
.
289 v1_msg
.send_recv_buf_complete
.num_sections
;
291 net_device
->recv_section
= kmalloc(net_device
->recv_section_cnt
292 * sizeof(struct nvsp_1_receive_buffer_section
), GFP_KERNEL
);
293 if (net_device
->recv_section
== NULL
) {
298 memcpy(net_device
->recv_section
,
299 init_packet
->msg
.v1_msg
.
300 send_recv_buf_complete
.sections
,
301 net_device
->recv_section_cnt
*
302 sizeof(struct nvsp_1_receive_buffer_section
));
304 DPRINT_INFO(NETVSC
, "Receive sections info (count %d, offset %d, "
305 "endoffset %d, suballoc size %d, num suballocs %d)",
306 net_device
->recv_section_cnt
,
307 net_device
->recv_section
[0].offset
,
308 net_device
->recv_section
[0].end_offset
,
309 net_device
->recv_section
[0].sub_alloc_size
,
310 net_device
->recv_section
[0].num_sub_allocs
);
313 * For 1st release, there should only be 1 section that represents the
314 * entire receive buffer
316 if (net_device
->recv_section_cnt
!= 1 ||
317 net_device
->recv_section
->offset
!= 0) {
325 netvsc_destroy_recv_buf(net_device
);
328 put_net_device(device
);
332 static int netvsc_init_send_buf(struct hv_device
*device
)
335 struct netvsc_device
*net_device
;
336 struct nvsp_message
*init_packet
;
338 net_device
= get_outbound_net_device(device
);
340 DPRINT_ERR(NETVSC
, "unable to get net device..."
341 "device being destroyed?");
344 if (net_device
->send_buf_size
<= 0) {
349 net_device
->send_buf
=
350 (void *)__get_free_pages(GFP_KERNEL
|__GFP_ZERO
,
351 get_order(net_device
->send_buf_size
));
352 if (!net_device
->send_buf
) {
353 DPRINT_ERR(NETVSC
, "unable to allocate send buffer of size %d",
354 net_device
->send_buf_size
);
359 DPRINT_INFO(NETVSC
, "Establishing send buffer's GPADL...");
362 * Establish the gpadl handle for this buffer on this
363 * channel. Note: This call uses the vmbus connection rather
364 * than the channel to establish the gpadl handle.
366 ret
= vmbus_establish_gpadl(device
->channel
, net_device
->send_buf
,
367 net_device
->send_buf_size
,
368 &net_device
->send_buf_gpadl_handle
);
370 DPRINT_ERR(NETVSC
, "unable to establish send buffer's gpadl");
374 /* Notify the NetVsp of the gpadl handle */
375 DPRINT_INFO(NETVSC
, "Sending NvspMessage1TypeSendSendBuffer...");
377 init_packet
= &net_device
->channel_init_pkt
;
379 memset(init_packet
, 0, sizeof(struct nvsp_message
));
381 init_packet
->hdr
.msg_type
= NVSP_MSG1_TYPE_SEND_SEND_BUF
;
382 init_packet
->msg
.v1_msg
.send_recv_buf
.
383 gpadl_handle
= net_device
->send_buf_gpadl_handle
;
384 init_packet
->msg
.v1_msg
.send_recv_buf
.id
=
385 NETVSC_SEND_BUFFER_ID
;
387 /* Send the gpadl notification request */
388 net_device
->wait_condition
= 0;
389 ret
= vmbus_sendpacket(device
->channel
, init_packet
,
390 sizeof(struct nvsp_message
),
391 (unsigned long)init_packet
,
393 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
396 "unable to send receive buffer's gpadl to netvsp");
400 wait_event_timeout(net_device
->channel_init_wait
,
401 net_device
->wait_condition
,
402 msecs_to_jiffies(1000));
403 BUG_ON(net_device
->wait_condition
== 0);
405 /* Check the response */
406 if (init_packet
->msg
.v1_msg
.
407 send_send_buf_complete
.status
!= NVSP_STAT_SUCCESS
) {
408 DPRINT_ERR(NETVSC
, "Unable to complete send buffer "
409 "initialzation with NetVsp - status %d",
410 init_packet
->msg
.v1_msg
.
411 send_send_buf_complete
.status
);
416 net_device
->send_section_size
= init_packet
->
417 msg
.v1_msg
.send_send_buf_complete
.section_size
;
422 netvsc_destroy_send_buf(net_device
);
425 put_net_device(device
);
429 static int netvsc_destroy_recv_buf(struct netvsc_device
*net_device
)
431 struct nvsp_message
*revoke_packet
;
435 * If we got a section count, it means we received a
436 * SendReceiveBufferComplete msg (ie sent
437 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
438 * to send a revoke msg here
440 if (net_device
->recv_section_cnt
) {
442 "Sending NvspMessage1TypeRevokeReceiveBuffer...");
444 /* Send the revoke receive buffer */
445 revoke_packet
= &net_device
->revoke_packet
;
446 memset(revoke_packet
, 0, sizeof(struct nvsp_message
));
448 revoke_packet
->hdr
.msg_type
=
449 NVSP_MSG1_TYPE_REVOKE_RECV_BUF
;
450 revoke_packet
->msg
.v1_msg
.
451 revoke_recv_buf
.id
= NETVSC_RECEIVE_BUFFER_ID
;
453 ret
= vmbus_sendpacket(net_device
->dev
->channel
,
455 sizeof(struct nvsp_message
),
456 (unsigned long)revoke_packet
,
457 VM_PKT_DATA_INBAND
, 0);
459 * If we failed here, we might as well return and
460 * have a leak rather than continue and a bugchk
463 DPRINT_ERR(NETVSC
, "unable to send revoke receive "
469 /* Teardown the gpadl on the vsp end */
470 if (net_device
->recv_buf_gpadl_handle
) {
471 DPRINT_INFO(NETVSC
, "Tearing down receive buffer's GPADL...");
473 ret
= vmbus_teardown_gpadl(net_device
->dev
->channel
,
474 net_device
->recv_buf_gpadl_handle
);
476 /* If we failed here, we might as well return and have a leak rather than continue and a bugchk */
479 "unable to teardown receive buffer's gpadl");
482 net_device
->recv_buf_gpadl_handle
= 0;
485 if (net_device
->recv_buf
) {
486 DPRINT_INFO(NETVSC
, "Freeing up receive buffer...");
488 /* Free up the receive buffer */
489 free_pages((unsigned long)net_device
->recv_buf
,
490 get_order(net_device
->recv_buf_size
));
491 net_device
->recv_buf
= NULL
;
494 if (net_device
->recv_section
) {
495 net_device
->recv_section_cnt
= 0;
496 kfree(net_device
->recv_section
);
497 net_device
->recv_section
= NULL
;
503 static int netvsc_destroy_send_buf(struct netvsc_device
*net_device
)
505 struct nvsp_message
*revoke_packet
;
509 * If we got a section count, it means we received a
510 * SendReceiveBufferComplete msg (ie sent
511 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
512 * to send a revoke msg here
514 if (net_device
->send_section_size
) {
516 "Sending NvspMessage1TypeRevokeSendBuffer...");
518 /* Send the revoke send buffer */
519 revoke_packet
= &net_device
->revoke_packet
;
520 memset(revoke_packet
, 0, sizeof(struct nvsp_message
));
522 revoke_packet
->hdr
.msg_type
=
523 NVSP_MSG1_TYPE_REVOKE_SEND_BUF
;
524 revoke_packet
->msg
.v1_msg
.
525 revoke_send_buf
.id
= NETVSC_SEND_BUFFER_ID
;
527 ret
= vmbus_sendpacket(net_device
->dev
->channel
,
529 sizeof(struct nvsp_message
),
530 (unsigned long)revoke_packet
,
531 VM_PKT_DATA_INBAND
, 0);
533 * If we failed here, we might as well return and have a leak
534 * rather than continue and a bugchk
537 DPRINT_ERR(NETVSC
, "unable to send revoke send buffer "
543 /* Teardown the gpadl on the vsp end */
544 if (net_device
->send_buf_gpadl_handle
) {
545 DPRINT_INFO(NETVSC
, "Tearing down send buffer's GPADL...");
546 ret
= vmbus_teardown_gpadl(net_device
->dev
->channel
,
547 net_device
->send_buf_gpadl_handle
);
550 * If we failed here, we might as well return and have a leak
551 * rather than continue and a bugchk
554 DPRINT_ERR(NETVSC
, "unable to teardown send buffer's "
558 net_device
->send_buf_gpadl_handle
= 0;
561 if (net_device
->send_buf
) {
562 DPRINT_INFO(NETVSC
, "Freeing up send buffer...");
564 /* Free up the receive buffer */
565 free_pages((unsigned long)net_device
->send_buf
,
566 get_order(net_device
->send_buf_size
));
567 net_device
->send_buf
= NULL
;
574 static int netvsc_connect_vsp(struct hv_device
*device
)
577 struct netvsc_device
*net_device
;
578 struct nvsp_message
*init_packet
;
581 net_device
= get_outbound_net_device(device
);
583 DPRINT_ERR(NETVSC
, "unable to get net device..."
584 "device being destroyed?");
588 init_packet
= &net_device
->channel_init_pkt
;
590 memset(init_packet
, 0, sizeof(struct nvsp_message
));
591 init_packet
->hdr
.msg_type
= NVSP_MSG_TYPE_INIT
;
592 init_packet
->msg
.init_msg
.init
.min_protocol_ver
=
593 NVSP_MIN_PROTOCOL_VERSION
;
594 init_packet
->msg
.init_msg
.init
.max_protocol_ver
=
595 NVSP_MAX_PROTOCOL_VERSION
;
597 DPRINT_INFO(NETVSC
, "Sending NvspMessageTypeInit...");
599 /* Send the init request */
600 net_device
->wait_condition
= 0;
601 ret
= vmbus_sendpacket(device
->channel
, init_packet
,
602 sizeof(struct nvsp_message
),
603 (unsigned long)init_packet
,
605 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
608 DPRINT_ERR(NETVSC
, "unable to send NvspMessageTypeInit");
612 wait_event_timeout(net_device
->channel_init_wait
,
613 net_device
->wait_condition
,
614 msecs_to_jiffies(1000));
615 if (net_device
->wait_condition
== 0) {
620 DPRINT_INFO(NETVSC
, "NvspMessageTypeInit status(%d) max mdl chain (%d)",
621 init_packet
->msg
.init_msg
.init_complete
.status
,
622 init_packet
->msg
.init_msg
.
623 init_complete
.max_mdl_chain_len
);
625 if (init_packet
->msg
.init_msg
.init_complete
.status
!=
628 "unable to initialize with netvsp (status 0x%x)",
629 init_packet
->msg
.init_msg
.init_complete
.status
);
634 if (init_packet
->msg
.init_msg
.init_complete
.
635 negotiated_protocol_ver
!= NVSP_PROTOCOL_VERSION_1
) {
636 DPRINT_ERR(NETVSC
, "unable to initialize with netvsp "
637 "(version expected 1 got %d)",
638 init_packet
->msg
.init_msg
.
639 init_complete
.negotiated_protocol_ver
);
643 DPRINT_INFO(NETVSC
, "Sending NvspMessage1TypeSendNdisVersion...");
645 /* Send the ndis version */
646 memset(init_packet
, 0, sizeof(struct nvsp_message
));
648 ndis_version
= 0x00050000;
650 init_packet
->hdr
.msg_type
= NVSP_MSG1_TYPE_SEND_NDIS_VER
;
651 init_packet
->msg
.v1_msg
.
652 send_ndis_ver
.ndis_major_ver
=
653 (ndis_version
& 0xFFFF0000) >> 16;
654 init_packet
->msg
.v1_msg
.
655 send_ndis_ver
.ndis_minor_ver
=
656 ndis_version
& 0xFFFF;
658 /* Send the init request */
659 ret
= vmbus_sendpacket(device
->channel
, init_packet
,
660 sizeof(struct nvsp_message
),
661 (unsigned long)init_packet
,
662 VM_PKT_DATA_INBAND
, 0);
665 "unable to send NvspMessage1TypeSendNdisVersion");
670 /* Post the big receive buffer to NetVSP */
671 ret
= netvsc_init_recv_buf(device
);
673 ret
= netvsc_init_send_buf(device
);
676 put_net_device(device
);
680 static void NetVscDisconnectFromVsp(struct netvsc_device
*net_device
)
682 netvsc_destroy_recv_buf(net_device
);
683 netvsc_destroy_send_buf(net_device
);
687 * netvsc_device_add - Callback when the device belonging to this
690 static int netvsc_device_add(struct hv_device
*device
, void *additional_info
)
694 struct netvsc_device
*net_device
;
695 struct hv_netvsc_packet
*packet
, *pos
;
696 struct netvsc_driver
*net_driver
=
697 (struct netvsc_driver
*)device
->drv
;
699 net_device
= alloc_net_device(device
);
705 DPRINT_DBG(NETVSC
, "netvsc channel object allocated - %p", net_device
);
707 /* Initialize the NetVSC channel extension */
708 net_device
->recv_buf_size
= NETVSC_RECEIVE_BUFFER_SIZE
;
709 spin_lock_init(&net_device
->recv_pkt_list_lock
);
711 net_device
->send_buf_size
= NETVSC_SEND_BUFFER_SIZE
;
713 INIT_LIST_HEAD(&net_device
->recv_pkt_list
);
715 for (i
= 0; i
< NETVSC_RECEIVE_PACKETLIST_COUNT
; i
++) {
716 packet
= kzalloc(sizeof(struct hv_netvsc_packet
) +
717 (NETVSC_RECEIVE_SG_COUNT
*
718 sizeof(struct hv_page_buffer
)), GFP_KERNEL
);
720 DPRINT_DBG(NETVSC
, "unable to allocate netvsc pkts "
721 "for receive pool (wanted %d got %d)",
722 NETVSC_RECEIVE_PACKETLIST_COUNT
, i
);
725 list_add_tail(&packet
->list_ent
,
726 &net_device
->recv_pkt_list
);
728 init_waitqueue_head(&net_device
->channel_init_wait
);
730 /* Open the channel */
731 ret
= vmbus_open(device
->channel
, net_driver
->ring_buf_size
,
732 net_driver
->ring_buf_size
, NULL
, 0,
733 netvsc_channel_cb
, device
);
736 DPRINT_ERR(NETVSC
, "unable to open channel: %d", ret
);
741 /* Channel is opened */
742 DPRINT_INFO(NETVSC
, "*** NetVSC channel opened successfully! ***");
744 /* Connect with the NetVsp */
745 ret
= netvsc_connect_vsp(device
);
747 DPRINT_ERR(NETVSC
, "unable to connect to NetVSP - %d", ret
);
752 DPRINT_INFO(NETVSC
, "*** NetVSC channel handshake result - %d ***",
758 /* Now, we can close the channel safely */
759 vmbus_close(device
->channel
);
764 list_for_each_entry_safe(packet
, pos
,
765 &net_device
->recv_pkt_list
,
767 list_del(&packet
->list_ent
);
771 release_outbound_net_device(device
);
772 release_inbound_net_device(device
);
774 free_net_device(net_device
);
781 * netvsc_device_remove - Callback when the root bus device is removed
783 static int netvsc_device_remove(struct hv_device
*device
)
785 struct netvsc_device
*net_device
;
786 struct hv_netvsc_packet
*netvsc_packet
, *pos
;
788 DPRINT_INFO(NETVSC
, "Disabling outbound traffic on net device (%p)...",
791 /* Stop outbound traffic ie sends and receives completions */
792 net_device
= release_outbound_net_device(device
);
794 DPRINT_ERR(NETVSC
, "No net device present!!");
798 /* Wait for all send completions */
799 while (atomic_read(&net_device
->num_outstanding_sends
)) {
800 DPRINT_INFO(NETVSC
, "waiting for %d requests to complete...",
801 atomic_read(&net_device
->num_outstanding_sends
));
805 DPRINT_INFO(NETVSC
, "Disconnecting from netvsp...");
807 NetVscDisconnectFromVsp(net_device
);
809 DPRINT_INFO(NETVSC
, "Disabling inbound traffic on net device (%p)...",
812 /* Stop inbound traffic ie receives and sends completions */
813 net_device
= release_inbound_net_device(device
);
815 /* At this point, no one should be accessing netDevice except in here */
816 DPRINT_INFO(NETVSC
, "net device (%p) safe to remove", net_device
);
818 /* Now, we can close the channel safely */
819 vmbus_close(device
->channel
);
821 /* Release all resources */
822 list_for_each_entry_safe(netvsc_packet
, pos
,
823 &net_device
->recv_pkt_list
, list_ent
) {
824 list_del(&netvsc_packet
->list_ent
);
825 kfree(netvsc_packet
);
828 free_net_device(net_device
);
833 * netvsc_cleanup - Perform any cleanup when the driver is removed
835 static void netvsc_cleanup(struct hv_driver
*drv
)
839 static void netvsc_send_completion(struct hv_device
*device
,
840 struct vmpacket_descriptor
*packet
)
842 struct netvsc_device
*net_device
;
843 struct nvsp_message
*nvsp_packet
;
844 struct hv_netvsc_packet
*nvsc_packet
;
846 net_device
= get_inbound_net_device(device
);
848 DPRINT_ERR(NETVSC
, "unable to get net device..."
849 "device being destroyed?");
853 nvsp_packet
= (struct nvsp_message
*)((unsigned long)packet
+
854 (packet
->offset8
<< 3));
856 DPRINT_DBG(NETVSC
, "send completion packet - type %d",
857 nvsp_packet
->hdr
.msg_type
);
859 if ((nvsp_packet
->hdr
.msg_type
== NVSP_MSG_TYPE_INIT_COMPLETE
) ||
860 (nvsp_packet
->hdr
.msg_type
==
861 NVSP_MSG1_TYPE_SEND_RECV_BUF_COMPLETE
) ||
862 (nvsp_packet
->hdr
.msg_type
==
863 NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE
)) {
864 /* Copy the response back */
865 memcpy(&net_device
->channel_init_pkt
, nvsp_packet
,
866 sizeof(struct nvsp_message
));
867 net_device
->wait_condition
= 1;
868 wake_up(&net_device
->channel_init_wait
);
869 } else if (nvsp_packet
->hdr
.msg_type
==
870 NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE
) {
871 /* Get the send context */
872 nvsc_packet
= (struct hv_netvsc_packet
*)(unsigned long)
875 /* Notify the layer above us */
876 nvsc_packet
->completion
.send
.send_completion(
877 nvsc_packet
->completion
.send
.send_completion_ctx
);
879 atomic_dec(&net_device
->num_outstanding_sends
);
881 DPRINT_ERR(NETVSC
, "Unknown send completion packet type - "
882 "%d received!!", nvsp_packet
->hdr
.msg_type
);
885 put_net_device(device
);
888 static int netvsc_send(struct hv_device
*device
,
889 struct hv_netvsc_packet
*packet
)
891 struct netvsc_device
*net_device
;
894 struct nvsp_message sendMessage
;
896 net_device
= get_outbound_net_device(device
);
898 DPRINT_ERR(NETVSC
, "net device (%p) shutting down..."
899 "ignoring outbound packets", net_device
);
903 sendMessage
.hdr
.msg_type
= NVSP_MSG1_TYPE_SEND_RNDIS_PKT
;
904 if (packet
->is_data_pkt
) {
906 sendMessage
.msg
.v1_msg
.send_rndis_pkt
.channel_type
= 0;
908 /* 1 is RMC_CONTROL; */
909 sendMessage
.msg
.v1_msg
.send_rndis_pkt
.channel_type
= 1;
912 /* Not using send buffer section */
913 sendMessage
.msg
.v1_msg
.send_rndis_pkt
.send_buf_section_index
=
915 sendMessage
.msg
.v1_msg
.send_rndis_pkt
.send_buf_section_size
= 0;
917 if (packet
->page_buf_cnt
) {
918 ret
= vmbus_sendpacket_pagebuffer(device
->channel
,
920 packet
->page_buf_cnt
,
922 sizeof(struct nvsp_message
),
923 (unsigned long)packet
);
925 ret
= vmbus_sendpacket(device
->channel
, &sendMessage
,
926 sizeof(struct nvsp_message
),
927 (unsigned long)packet
,
929 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
934 DPRINT_ERR(NETVSC
, "Unable to send packet %p ret %d",
937 atomic_inc(&net_device
->num_outstanding_sends
);
938 put_net_device(device
);
942 static void netvsc_receive(struct hv_device
*device
,
943 struct vmpacket_descriptor
*packet
)
945 struct netvsc_device
*net_device
;
946 struct vmtransfer_page_packet_header
*vmxferpage_packet
;
947 struct nvsp_message
*nvsp_packet
;
948 struct hv_netvsc_packet
*netvsc_packet
= NULL
;
950 unsigned long end
, end_virtual
;
951 /* struct netvsc_driver *netvscDriver; */
952 struct xferpage_packet
*xferpage_packet
= NULL
;
954 int count
= 0, bytes_remain
= 0;
958 net_device
= get_inbound_net_device(device
);
960 DPRINT_ERR(NETVSC
, "unable to get net device..."
961 "device being destroyed?");
966 * All inbound packets other than send completion should be xfer page
969 if (packet
->type
!= VM_PKT_DATA_USING_XFER_PAGES
) {
970 DPRINT_ERR(NETVSC
, "Unknown packet type received - %d",
972 put_net_device(device
);
976 nvsp_packet
= (struct nvsp_message
*)((unsigned long)packet
+
977 (packet
->offset8
<< 3));
979 /* Make sure this is a valid nvsp packet */
980 if (nvsp_packet
->hdr
.msg_type
!=
981 NVSP_MSG1_TYPE_SEND_RNDIS_PKT
) {
982 DPRINT_ERR(NETVSC
, "Unknown nvsp packet type received - %d",
983 nvsp_packet
->hdr
.msg_type
);
984 put_net_device(device
);
988 DPRINT_DBG(NETVSC
, "NVSP packet received - type %d",
989 nvsp_packet
->hdr
.msg_type
);
991 vmxferpage_packet
= (struct vmtransfer_page_packet_header
*)packet
;
993 if (vmxferpage_packet
->xfer_pageset_id
!= NETVSC_RECEIVE_BUFFER_ID
) {
994 DPRINT_ERR(NETVSC
, "Invalid xfer page set id - "
995 "expecting %x got %x", NETVSC_RECEIVE_BUFFER_ID
,
996 vmxferpage_packet
->xfer_pageset_id
);
997 put_net_device(device
);
1001 DPRINT_DBG(NETVSC
, "xfer page - range count %d",
1002 vmxferpage_packet
->range_cnt
);
1005 * Grab free packets (range count + 1) to represent this xfer
1006 * page packet. +1 to represent the xfer page packet itself.
1007 * We grab it here so that we know exactly how many we can
1010 spin_lock_irqsave(&net_device
->recv_pkt_list_lock
, flags
);
1011 while (!list_empty(&net_device
->recv_pkt_list
)) {
1012 list_move_tail(net_device
->recv_pkt_list
.next
, &listHead
);
1013 if (++count
== vmxferpage_packet
->range_cnt
+ 1)
1016 spin_unlock_irqrestore(&net_device
->recv_pkt_list_lock
, flags
);
1019 * We need at least 2 netvsc pkts (1 to represent the xfer
1020 * page and at least 1 for the range) i.e. we can handled
1021 * some of the xfer page packet ranges...
1024 DPRINT_ERR(NETVSC
, "Got only %d netvsc pkt...needed %d pkts. "
1025 "Dropping this xfer page packet completely!",
1026 count
, vmxferpage_packet
->range_cnt
+ 1);
1028 /* Return it to the freelist */
1029 spin_lock_irqsave(&net_device
->recv_pkt_list_lock
, flags
);
1030 for (i
= count
; i
!= 0; i
--) {
1031 list_move_tail(listHead
.next
,
1032 &net_device
->recv_pkt_list
);
1034 spin_unlock_irqrestore(&net_device
->recv_pkt_list_lock
,
1037 netvsc_send_recv_completion(device
,
1038 vmxferpage_packet
->d
.trans_id
);
1040 put_net_device(device
);
1044 /* Remove the 1st packet to represent the xfer page packet itself */
1045 xferpage_packet
= (struct xferpage_packet
*)listHead
.next
;
1046 list_del(&xferpage_packet
->list_ent
);
1048 /* This is how much we can satisfy */
1049 xferpage_packet
->count
= count
- 1;
1051 if (xferpage_packet
->count
!= vmxferpage_packet
->range_cnt
) {
1052 DPRINT_INFO(NETVSC
, "Needed %d netvsc pkts to satisy this xfer "
1053 "page...got %d", vmxferpage_packet
->range_cnt
,
1054 xferpage_packet
->count
);
1057 /* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */
1058 for (i
= 0; i
< (count
- 1); i
++) {
1059 netvsc_packet
= (struct hv_netvsc_packet
*)listHead
.next
;
1060 list_del(&netvsc_packet
->list_ent
);
1062 /* Initialize the netvsc packet */
1063 netvsc_packet
->xfer_page_pkt
= xferpage_packet
;
1064 netvsc_packet
->completion
.recv
.recv_completion
=
1065 netvsc_receive_completion
;
1066 netvsc_packet
->completion
.recv
.recv_completion_ctx
=
1068 netvsc_packet
->device
= device
;
1069 /* Save this so that we can send it back */
1070 netvsc_packet
->completion
.recv
.recv_completion_tid
=
1071 vmxferpage_packet
->d
.trans_id
;
1073 netvsc_packet
->total_data_buflen
=
1074 vmxferpage_packet
->ranges
[i
].byte_count
;
1075 netvsc_packet
->page_buf_cnt
= 1;
1077 netvsc_packet
->page_buf
[0].len
=
1078 vmxferpage_packet
->ranges
[i
].byte_count
;
1080 start
= virt_to_phys((void *)((unsigned long)net_device
->
1081 recv_buf
+ vmxferpage_packet
->ranges
[i
].byte_offset
));
1083 netvsc_packet
->page_buf
[0].pfn
= start
>> PAGE_SHIFT
;
1084 end_virtual
= (unsigned long)net_device
->recv_buf
1085 + vmxferpage_packet
->ranges
[i
].byte_offset
1086 + vmxferpage_packet
->ranges
[i
].byte_count
- 1;
1087 end
= virt_to_phys((void *)end_virtual
);
1089 /* Calculate the page relative offset */
1090 netvsc_packet
->page_buf
[0].offset
=
1091 vmxferpage_packet
->ranges
[i
].byte_offset
&
1093 if ((end
>> PAGE_SHIFT
) != (start
>> PAGE_SHIFT
)) {
1094 /* Handle frame across multiple pages: */
1095 netvsc_packet
->page_buf
[0].len
=
1096 (netvsc_packet
->page_buf
[0].pfn
<<
1098 + PAGE_SIZE
- start
;
1099 bytes_remain
= netvsc_packet
->total_data_buflen
-
1100 netvsc_packet
->page_buf
[0].len
;
1101 for (j
= 1; j
< NETVSC_PACKET_MAXPAGE
; j
++) {
1102 netvsc_packet
->page_buf
[j
].offset
= 0;
1103 if (bytes_remain
<= PAGE_SIZE
) {
1104 netvsc_packet
->page_buf
[j
].len
=
1108 netvsc_packet
->page_buf
[j
].len
=
1110 bytes_remain
-= PAGE_SIZE
;
1112 netvsc_packet
->page_buf
[j
].pfn
=
1113 virt_to_phys((void *)(end_virtual
-
1114 bytes_remain
)) >> PAGE_SHIFT
;
1115 netvsc_packet
->page_buf_cnt
++;
1116 if (bytes_remain
== 0)
1120 DPRINT_DBG(NETVSC
, "[%d] - (abs offset %u len %u) => "
1121 "(pfn %llx, offset %u, len %u)", i
,
1122 vmxferpage_packet
->ranges
[i
].byte_offset
,
1123 vmxferpage_packet
->ranges
[i
].byte_count
,
1124 netvsc_packet
->page_buf
[0].pfn
,
1125 netvsc_packet
->page_buf
[0].offset
,
1126 netvsc_packet
->page_buf
[0].len
);
1128 /* Pass it to the upper layer */
1129 ((struct netvsc_driver
*)device
->drv
)->
1130 recv_cb(device
, netvsc_packet
);
1132 netvsc_receive_completion(netvsc_packet
->
1133 completion
.recv
.recv_completion_ctx
);
1136 put_net_device(device
);
1139 static void netvsc_send_recv_completion(struct hv_device
*device
,
1142 struct nvsp_message recvcompMessage
;
1146 DPRINT_DBG(NETVSC
, "Sending receive completion pkt - %llx",
1149 recvcompMessage
.hdr
.msg_type
=
1150 NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE
;
1152 /* FIXME: Pass in the status */
1153 recvcompMessage
.msg
.v1_msg
.send_rndis_pkt_complete
.status
=
1157 /* Send the completion */
1158 ret
= vmbus_sendpacket(device
->channel
, &recvcompMessage
,
1159 sizeof(struct nvsp_message
), transaction_id
,
1164 } else if (ret
== -1) {
1165 /* no more room...wait a bit and attempt to retry 3 times */
1167 DPRINT_ERR(NETVSC
, "unable to send receive completion pkt "
1168 "(tid %llx)...retrying %d", transaction_id
, retries
);
1172 goto retry_send_cmplt
;
1174 DPRINT_ERR(NETVSC
, "unable to send receive completion "
1175 "pkt (tid %llx)...give up retrying",
1179 DPRINT_ERR(NETVSC
, "unable to send receive completion pkt - "
1180 "%llx", transaction_id
);
1184 /* Send a receive completion packet to RNDIS device (ie NetVsp) */
1185 static void netvsc_receive_completion(void *context
)
1187 struct hv_netvsc_packet
*packet
= context
;
1188 struct hv_device
*device
= (struct hv_device
*)packet
->device
;
1189 struct netvsc_device
*net_device
;
1190 u64 transaction_id
= 0;
1191 bool fsend_receive_comp
= false;
1192 unsigned long flags
;
1195 * Even though it seems logical to do a GetOutboundNetDevice() here to
1196 * send out receive completion, we are using GetInboundNetDevice()
1197 * since we may have disable outbound traffic already.
1199 net_device
= get_inbound_net_device(device
);
1201 DPRINT_ERR(NETVSC
, "unable to get net device..."
1202 "device being destroyed?");
1206 /* Overloading use of the lock. */
1207 spin_lock_irqsave(&net_device
->recv_pkt_list_lock
, flags
);
1209 packet
->xfer_page_pkt
->count
--;
1212 * Last one in the line that represent 1 xfer page packet.
1213 * Return the xfer page packet itself to the freelist
1215 if (packet
->xfer_page_pkt
->count
== 0) {
1216 fsend_receive_comp
= true;
1217 transaction_id
= packet
->completion
.recv
.recv_completion_tid
;
1218 list_add_tail(&packet
->xfer_page_pkt
->list_ent
,
1219 &net_device
->recv_pkt_list
);
1223 /* Put the packet back */
1224 list_add_tail(&packet
->list_ent
, &net_device
->recv_pkt_list
);
1225 spin_unlock_irqrestore(&net_device
->recv_pkt_list_lock
, flags
);
1227 /* Send a receive completion for the xfer page packet */
1228 if (fsend_receive_comp
)
1229 netvsc_send_recv_completion(device
, transaction_id
);
1231 put_net_device(device
);
1234 static void netvsc_channel_cb(void *context
)
1237 struct hv_device
*device
= context
;
1238 struct netvsc_device
*net_device
;
1241 unsigned char *packet
;
1242 struct vmpacket_descriptor
*desc
;
1243 unsigned char *buffer
;
1244 int bufferlen
= NETVSC_PACKET_SIZE
;
1246 packet
= kzalloc(NETVSC_PACKET_SIZE
* sizeof(unsigned char),
1252 net_device
= get_inbound_net_device(device
);
1254 DPRINT_ERR(NETVSC
, "net device (%p) shutting down..."
1255 "ignoring inbound packets", net_device
);
1260 ret
= vmbus_recvpacket_raw(device
->channel
, buffer
, bufferlen
,
1261 &bytes_recvd
, &request_id
);
1263 if (bytes_recvd
> 0) {
1264 DPRINT_DBG(NETVSC
, "receive %d bytes, tid %llx",
1265 bytes_recvd
, request_id
);
1267 desc
= (struct vmpacket_descriptor
*)buffer
;
1268 switch (desc
->type
) {
1270 netvsc_send_completion(device
, desc
);
1273 case VM_PKT_DATA_USING_XFER_PAGES
:
1274 netvsc_receive(device
, desc
);
1279 "unhandled packet type %d, "
1280 "tid %llx len %d\n",
1281 desc
->type
, request_id
,
1287 if (bufferlen
> NETVSC_PACKET_SIZE
) {
1290 bufferlen
= NETVSC_PACKET_SIZE
;
1294 if (bufferlen
> NETVSC_PACKET_SIZE
) {
1297 bufferlen
= NETVSC_PACKET_SIZE
;
1302 } else if (ret
== -2) {
1303 /* Handle large packet */
1304 buffer
= kmalloc(bytes_recvd
, GFP_ATOMIC
);
1305 if (buffer
== NULL
) {
1306 /* Try again next time around */
1308 "unable to allocate buffer of size "
1309 "(%d)!!", bytes_recvd
);
1313 bufferlen
= bytes_recvd
;
1317 put_net_device(device
);