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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23 #include <linux/kernel.h>
24 #include <linux/sched.h>
25 #include <linux/wait.h>
27 #include <linux/delay.h>
29 #include <linux/slab.h>
33 #include "rndis_filter.h"
38 static const char *driver_name
= "netvsc";
40 /* {F8615163-DF3E-46c5-913F-F2D2F965ED0E} */
41 static const struct hv_guid netvsc_device_type
= {
43 0x63, 0x51, 0x61, 0xF8, 0x3E, 0xDF, 0xc5, 0x46,
44 0x91, 0x3F, 0xF2, 0xD2, 0xF9, 0x65, 0xED, 0x0E
48 static void netvsc_channel_cb(void *context
);
50 static int netvsc_init_send_buf(struct hv_device
*device
);
52 static int netvsc_init_recv_buf(struct hv_device
*device
);
54 static int netvsc_destroy_send_buf(struct netvsc_device
*net_device
);
56 static int netvsc_connect_vsp(struct hv_device
*device
);
58 static void netvsc_send_completion(struct hv_device
*device
,
59 struct vmpacket_descriptor
*packet
);
61 static void netvsc_receive(struct hv_device
*device
,
62 struct vmpacket_descriptor
*packet
);
65 static struct netvsc_device
*alloc_net_device(struct hv_device
*device
)
67 struct netvsc_device
*net_device
;
69 net_device
= kzalloc(sizeof(struct netvsc_device
), GFP_KERNEL
);
73 /* Set to 2 to allow both inbound and outbound traffic */
74 atomic_cmpxchg(&net_device
->refcnt
, 0, 2);
76 net_device
->dev
= device
;
77 device
->ext
= net_device
;
82 static void free_net_device(struct netvsc_device
*device
)
84 WARN_ON(atomic_read(&device
->refcnt
) != 0);
85 device
->dev
->ext
= NULL
;
90 /* Get the net device object iff exists and its refcount > 1 */
91 static struct netvsc_device
*get_outbound_net_device(struct hv_device
*device
)
93 struct netvsc_device
*net_device
;
95 net_device
= device
->ext
;
96 if (net_device
&& atomic_read(&net_device
->refcnt
) > 1)
97 atomic_inc(&net_device
->refcnt
);
104 /* Get the net device object iff exists and its refcount > 0 */
105 static struct netvsc_device
*get_inbound_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
))
111 atomic_inc(&net_device
->refcnt
);
118 static void put_net_device(struct hv_device
*device
)
120 struct netvsc_device
*net_device
;
122 net_device
= device
->ext
;
124 atomic_dec(&net_device
->refcnt
);
127 static struct netvsc_device
*release_outbound_net_device(
128 struct hv_device
*device
)
130 struct netvsc_device
*net_device
;
132 net_device
= device
->ext
;
133 if (net_device
== NULL
)
136 /* Busy wait until the ref drop to 2, then set it to 1 */
137 while (atomic_cmpxchg(&net_device
->refcnt
, 2, 1) != 2)
143 static struct netvsc_device
*release_inbound_net_device(
144 struct hv_device
*device
)
146 struct netvsc_device
*net_device
;
148 net_device
= device
->ext
;
149 if (net_device
== NULL
)
152 /* Busy wait until the ref drop to 1, then set it to 0 */
153 while (atomic_cmpxchg(&net_device
->refcnt
, 1, 0) != 1)
160 static int netvsc_destroy_recv_buf(struct netvsc_device
*net_device
)
162 struct nvsp_message
*revoke_packet
;
166 * If we got a section count, it means we received a
167 * SendReceiveBufferComplete msg (ie sent
168 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
169 * to send a revoke msg here
171 if (net_device
->recv_section_cnt
) {
172 /* Send the revoke receive buffer */
173 revoke_packet
= &net_device
->revoke_packet
;
174 memset(revoke_packet
, 0, sizeof(struct nvsp_message
));
176 revoke_packet
->hdr
.msg_type
=
177 NVSP_MSG1_TYPE_REVOKE_RECV_BUF
;
178 revoke_packet
->msg
.v1_msg
.
179 revoke_recv_buf
.id
= NETVSC_RECEIVE_BUFFER_ID
;
181 ret
= vmbus_sendpacket(net_device
->dev
->channel
,
183 sizeof(struct nvsp_message
),
184 (unsigned long)revoke_packet
,
185 VM_PKT_DATA_INBAND
, 0);
187 * If we failed here, we might as well return and
188 * have a leak rather than continue and a bugchk
191 dev_err(&net_device
->dev
->device
, "unable to send "
192 "revoke receive buffer to netvsp");
197 /* Teardown the gpadl on the vsp end */
198 if (net_device
->recv_buf_gpadl_handle
) {
199 ret
= vmbus_teardown_gpadl(net_device
->dev
->channel
,
200 net_device
->recv_buf_gpadl_handle
);
202 /* If we failed here, we might as well return and have a leak
203 * rather than continue and a bugchk
206 dev_err(&net_device
->dev
->device
,
207 "unable to teardown receive buffer's gpadl");
210 net_device
->recv_buf_gpadl_handle
= 0;
213 if (net_device
->recv_buf
) {
214 /* Free up the receive buffer */
215 free_pages((unsigned long)net_device
->recv_buf
,
216 get_order(net_device
->recv_buf_size
));
217 net_device
->recv_buf
= NULL
;
220 if (net_device
->recv_section
) {
221 net_device
->recv_section_cnt
= 0;
222 kfree(net_device
->recv_section
);
223 net_device
->recv_section
= NULL
;
229 static int netvsc_init_recv_buf(struct hv_device
*device
)
232 struct netvsc_device
*net_device
;
233 struct nvsp_message
*init_packet
;
235 net_device
= get_outbound_net_device(device
);
237 dev_err(&device
->device
, "unable to get net device..."
238 "device being destroyed?");
242 net_device
->recv_buf
=
243 (void *)__get_free_pages(GFP_KERNEL
|__GFP_ZERO
,
244 get_order(net_device
->recv_buf_size
));
245 if (!net_device
->recv_buf
) {
246 dev_err(&device
->device
, "unable to allocate receive "
247 "buffer of size %d", net_device
->recv_buf_size
);
253 * Establish the gpadl handle for this buffer on this
254 * channel. Note: This call uses the vmbus connection rather
255 * than the channel to establish the gpadl handle.
257 ret
= vmbus_establish_gpadl(device
->channel
, net_device
->recv_buf
,
258 net_device
->recv_buf_size
,
259 &net_device
->recv_buf_gpadl_handle
);
261 dev_err(&device
->device
,
262 "unable to establish receive buffer's gpadl");
267 /* Notify the NetVsp of the gpadl handle */
268 init_packet
= &net_device
->channel_init_pkt
;
270 memset(init_packet
, 0, sizeof(struct nvsp_message
));
272 init_packet
->hdr
.msg_type
= NVSP_MSG1_TYPE_SEND_RECV_BUF
;
273 init_packet
->msg
.v1_msg
.send_recv_buf
.
274 gpadl_handle
= net_device
->recv_buf_gpadl_handle
;
275 init_packet
->msg
.v1_msg
.
276 send_recv_buf
.id
= NETVSC_RECEIVE_BUFFER_ID
;
278 /* Send the gpadl notification request */
279 net_device
->wait_condition
= 0;
280 ret
= vmbus_sendpacket(device
->channel
, init_packet
,
281 sizeof(struct nvsp_message
),
282 (unsigned long)init_packet
,
284 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
286 dev_err(&device
->device
,
287 "unable to send receive buffer's gpadl to netvsp");
291 wait_event_timeout(net_device
->channel_init_wait
,
292 net_device
->wait_condition
,
293 msecs_to_jiffies(1000));
294 BUG_ON(net_device
->wait_condition
== 0);
297 /* Check the response */
298 if (init_packet
->msg
.v1_msg
.
299 send_recv_buf_complete
.status
!= NVSP_STAT_SUCCESS
) {
300 dev_err(&device
->device
, "Unable to complete receive buffer "
301 "initialzation with NetVsp - status %d",
302 init_packet
->msg
.v1_msg
.
303 send_recv_buf_complete
.status
);
308 /* Parse the response */
310 net_device
->recv_section_cnt
= init_packet
->msg
.
311 v1_msg
.send_recv_buf_complete
.num_sections
;
313 net_device
->recv_section
= kmalloc(net_device
->recv_section_cnt
314 * sizeof(struct nvsp_1_receive_buffer_section
), GFP_KERNEL
);
315 if (net_device
->recv_section
== NULL
) {
320 memcpy(net_device
->recv_section
,
321 init_packet
->msg
.v1_msg
.
322 send_recv_buf_complete
.sections
,
323 net_device
->recv_section_cnt
*
324 sizeof(struct nvsp_1_receive_buffer_section
));
327 * For 1st release, there should only be 1 section that represents the
328 * entire receive buffer
330 if (net_device
->recv_section_cnt
!= 1 ||
331 net_device
->recv_section
->offset
!= 0) {
339 netvsc_destroy_recv_buf(net_device
);
342 put_net_device(device
);
346 static int netvsc_init_send_buf(struct hv_device
*device
)
349 struct netvsc_device
*net_device
;
350 struct nvsp_message
*init_packet
;
352 net_device
= get_outbound_net_device(device
);
354 dev_err(&device
->device
, "unable to get net device..."
355 "device being destroyed?");
358 if (net_device
->send_buf_size
<= 0) {
363 net_device
->send_buf
=
364 (void *)__get_free_pages(GFP_KERNEL
|__GFP_ZERO
,
365 get_order(net_device
->send_buf_size
));
366 if (!net_device
->send_buf
) {
367 dev_err(&device
->device
, "unable to allocate send "
368 "buffer of size %d", net_device
->send_buf_size
);
374 * Establish the gpadl handle for this buffer on this
375 * channel. Note: This call uses the vmbus connection rather
376 * than the channel to establish the gpadl handle.
378 ret
= vmbus_establish_gpadl(device
->channel
, net_device
->send_buf
,
379 net_device
->send_buf_size
,
380 &net_device
->send_buf_gpadl_handle
);
382 dev_err(&device
->device
, "unable to establish send buffer's gpadl");
386 /* Notify the NetVsp of the gpadl handle */
387 init_packet
= &net_device
->channel_init_pkt
;
389 memset(init_packet
, 0, sizeof(struct nvsp_message
));
391 init_packet
->hdr
.msg_type
= NVSP_MSG1_TYPE_SEND_SEND_BUF
;
392 init_packet
->msg
.v1_msg
.send_recv_buf
.
393 gpadl_handle
= net_device
->send_buf_gpadl_handle
;
394 init_packet
->msg
.v1_msg
.send_recv_buf
.id
=
395 NETVSC_SEND_BUFFER_ID
;
397 /* Send the gpadl notification request */
398 net_device
->wait_condition
= 0;
399 ret
= vmbus_sendpacket(device
->channel
, init_packet
,
400 sizeof(struct nvsp_message
),
401 (unsigned long)init_packet
,
403 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
405 dev_err(&device
->device
,
406 "unable to send receive buffer's gpadl to netvsp");
410 wait_event_timeout(net_device
->channel_init_wait
,
411 net_device
->wait_condition
,
412 msecs_to_jiffies(1000));
413 BUG_ON(net_device
->wait_condition
== 0);
415 /* Check the response */
416 if (init_packet
->msg
.v1_msg
.
417 send_send_buf_complete
.status
!= NVSP_STAT_SUCCESS
) {
418 dev_err(&device
->device
, "Unable to complete send buffer "
419 "initialzation with NetVsp - status %d",
420 init_packet
->msg
.v1_msg
.
421 send_send_buf_complete
.status
);
426 net_device
->send_section_size
= init_packet
->
427 msg
.v1_msg
.send_send_buf_complete
.section_size
;
432 netvsc_destroy_send_buf(net_device
);
435 put_net_device(device
);
439 static int netvsc_destroy_send_buf(struct netvsc_device
*net_device
)
441 struct nvsp_message
*revoke_packet
;
445 * If we got a section count, it means we received a
446 * SendReceiveBufferComplete msg (ie sent
447 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
448 * to send a revoke msg here
450 if (net_device
->send_section_size
) {
451 /* Send the revoke send buffer */
452 revoke_packet
= &net_device
->revoke_packet
;
453 memset(revoke_packet
, 0, sizeof(struct nvsp_message
));
455 revoke_packet
->hdr
.msg_type
=
456 NVSP_MSG1_TYPE_REVOKE_SEND_BUF
;
457 revoke_packet
->msg
.v1_msg
.
458 revoke_send_buf
.id
= NETVSC_SEND_BUFFER_ID
;
460 ret
= vmbus_sendpacket(net_device
->dev
->channel
,
462 sizeof(struct nvsp_message
),
463 (unsigned long)revoke_packet
,
464 VM_PKT_DATA_INBAND
, 0);
466 * If we failed here, we might as well return and have a leak
467 * rather than continue and a bugchk
470 dev_err(&net_device
->dev
->device
, "unable to send "
471 "revoke send buffer to netvsp");
476 /* Teardown the gpadl on the vsp end */
477 if (net_device
->send_buf_gpadl_handle
) {
478 ret
= vmbus_teardown_gpadl(net_device
->dev
->channel
,
479 net_device
->send_buf_gpadl_handle
);
482 * If we failed here, we might as well return and have a leak
483 * rather than continue and a bugchk
486 dev_err(&net_device
->dev
->device
,
487 "unable to teardown send buffer's gpadl");
490 net_device
->send_buf_gpadl_handle
= 0;
493 if (net_device
->send_buf
) {
494 /* Free up the receive buffer */
495 free_pages((unsigned long)net_device
->send_buf
,
496 get_order(net_device
->send_buf_size
));
497 net_device
->send_buf
= NULL
;
504 static int netvsc_connect_vsp(struct hv_device
*device
)
507 struct netvsc_device
*net_device
;
508 struct nvsp_message
*init_packet
;
511 net_device
= get_outbound_net_device(device
);
513 dev_err(&device
->device
, "unable to get net device..."
514 "device being destroyed?");
518 init_packet
= &net_device
->channel_init_pkt
;
520 memset(init_packet
, 0, sizeof(struct nvsp_message
));
521 init_packet
->hdr
.msg_type
= NVSP_MSG_TYPE_INIT
;
522 init_packet
->msg
.init_msg
.init
.min_protocol_ver
=
523 NVSP_MIN_PROTOCOL_VERSION
;
524 init_packet
->msg
.init_msg
.init
.max_protocol_ver
=
525 NVSP_MAX_PROTOCOL_VERSION
;
527 /* Send the init request */
528 net_device
->wait_condition
= 0;
529 ret
= vmbus_sendpacket(device
->channel
, init_packet
,
530 sizeof(struct nvsp_message
),
531 (unsigned long)init_packet
,
533 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
538 wait_event_timeout(net_device
->channel_init_wait
,
539 net_device
->wait_condition
,
540 msecs_to_jiffies(1000));
541 if (net_device
->wait_condition
== 0) {
546 if (init_packet
->msg
.init_msg
.init_complete
.status
!=
552 if (init_packet
->msg
.init_msg
.init_complete
.
553 negotiated_protocol_ver
!= NVSP_PROTOCOL_VERSION_1
) {
557 /* Send the ndis version */
558 memset(init_packet
, 0, sizeof(struct nvsp_message
));
560 ndis_version
= 0x00050000;
562 init_packet
->hdr
.msg_type
= NVSP_MSG1_TYPE_SEND_NDIS_VER
;
563 init_packet
->msg
.v1_msg
.
564 send_ndis_ver
.ndis_major_ver
=
565 (ndis_version
& 0xFFFF0000) >> 16;
566 init_packet
->msg
.v1_msg
.
567 send_ndis_ver
.ndis_minor_ver
=
568 ndis_version
& 0xFFFF;
570 /* Send the init request */
571 ret
= vmbus_sendpacket(device
->channel
, init_packet
,
572 sizeof(struct nvsp_message
),
573 (unsigned long)init_packet
,
574 VM_PKT_DATA_INBAND
, 0);
580 /* Post the big receive buffer to NetVSP */
581 ret
= netvsc_init_recv_buf(device
);
583 ret
= netvsc_init_send_buf(device
);
586 put_net_device(device
);
590 static void NetVscDisconnectFromVsp(struct netvsc_device
*net_device
)
592 netvsc_destroy_recv_buf(net_device
);
593 netvsc_destroy_send_buf(net_device
);
597 * netvsc_device_add - Callback when the device belonging to this
600 static int netvsc_device_add(struct hv_device
*device
, void *additional_info
)
604 struct netvsc_device
*net_device
;
605 struct hv_netvsc_packet
*packet
, *pos
;
606 struct netvsc_driver
*net_driver
=
607 (struct netvsc_driver
*)device
->drv
;
609 net_device
= alloc_net_device(device
);
615 /* Initialize the NetVSC channel extension */
616 net_device
->recv_buf_size
= NETVSC_RECEIVE_BUFFER_SIZE
;
617 spin_lock_init(&net_device
->recv_pkt_list_lock
);
619 net_device
->send_buf_size
= NETVSC_SEND_BUFFER_SIZE
;
621 INIT_LIST_HEAD(&net_device
->recv_pkt_list
);
623 for (i
= 0; i
< NETVSC_RECEIVE_PACKETLIST_COUNT
; i
++) {
624 packet
= kzalloc(sizeof(struct hv_netvsc_packet
) +
625 (NETVSC_RECEIVE_SG_COUNT
*
626 sizeof(struct hv_page_buffer
)), GFP_KERNEL
);
630 list_add_tail(&packet
->list_ent
,
631 &net_device
->recv_pkt_list
);
633 init_waitqueue_head(&net_device
->channel_init_wait
);
635 /* Open the channel */
636 ret
= vmbus_open(device
->channel
, net_driver
->ring_buf_size
,
637 net_driver
->ring_buf_size
, NULL
, 0,
638 netvsc_channel_cb
, device
);
641 dev_err(&device
->device
, "unable to open channel: %d", ret
);
646 /* Channel is opened */
647 pr_info("hv_netvsc channel opened successfully");
649 /* Connect with the NetVsp */
650 ret
= netvsc_connect_vsp(device
);
652 dev_err(&device
->device
,
653 "unable to connect to NetVSP - %d", ret
);
661 /* Now, we can close the channel safely */
662 vmbus_close(device
->channel
);
667 list_for_each_entry_safe(packet
, pos
,
668 &net_device
->recv_pkt_list
,
670 list_del(&packet
->list_ent
);
674 release_outbound_net_device(device
);
675 release_inbound_net_device(device
);
677 free_net_device(net_device
);
684 * netvsc_device_remove - Callback when the root bus device is removed
686 static int netvsc_device_remove(struct hv_device
*device
)
688 struct netvsc_device
*net_device
;
689 struct hv_netvsc_packet
*netvsc_packet
, *pos
;
691 /* Stop outbound traffic ie sends and receives completions */
692 net_device
= release_outbound_net_device(device
);
694 dev_err(&device
->device
, "No net device present!!");
698 /* Wait for all send completions */
699 while (atomic_read(&net_device
->num_outstanding_sends
)) {
700 dev_err(&device
->device
,
701 "waiting for %d requests to complete...",
702 atomic_read(&net_device
->num_outstanding_sends
));
706 NetVscDisconnectFromVsp(net_device
);
708 /* Stop inbound traffic ie receives and sends completions */
709 net_device
= release_inbound_net_device(device
);
711 /* At this point, no one should be accessing netDevice except in here */
712 dev_notice(&device
->device
, "net device safe to remove");
714 /* Now, we can close the channel safely */
715 vmbus_close(device
->channel
);
717 /* Release all resources */
718 list_for_each_entry_safe(netvsc_packet
, pos
,
719 &net_device
->recv_pkt_list
, list_ent
) {
720 list_del(&netvsc_packet
->list_ent
);
721 kfree(netvsc_packet
);
724 free_net_device(net_device
);
729 * netvsc_cleanup - Perform any cleanup when the driver is removed
731 static void netvsc_cleanup(struct hv_driver
*drv
)
735 static void netvsc_send_completion(struct hv_device
*device
,
736 struct vmpacket_descriptor
*packet
)
738 struct netvsc_device
*net_device
;
739 struct nvsp_message
*nvsp_packet
;
740 struct hv_netvsc_packet
*nvsc_packet
;
742 net_device
= get_inbound_net_device(device
);
744 dev_err(&device
->device
, "unable to get net device..."
745 "device being destroyed?");
749 nvsp_packet
= (struct nvsp_message
*)((unsigned long)packet
+
750 (packet
->offset8
<< 3));
752 if ((nvsp_packet
->hdr
.msg_type
== NVSP_MSG_TYPE_INIT_COMPLETE
) ||
753 (nvsp_packet
->hdr
.msg_type
==
754 NVSP_MSG1_TYPE_SEND_RECV_BUF_COMPLETE
) ||
755 (nvsp_packet
->hdr
.msg_type
==
756 NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE
)) {
757 /* Copy the response back */
758 memcpy(&net_device
->channel_init_pkt
, nvsp_packet
,
759 sizeof(struct nvsp_message
));
760 net_device
->wait_condition
= 1;
761 wake_up(&net_device
->channel_init_wait
);
762 } else if (nvsp_packet
->hdr
.msg_type
==
763 NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE
) {
764 /* Get the send context */
765 nvsc_packet
= (struct hv_netvsc_packet
*)(unsigned long)
768 /* Notify the layer above us */
769 nvsc_packet
->completion
.send
.send_completion(
770 nvsc_packet
->completion
.send
.send_completion_ctx
);
772 atomic_dec(&net_device
->num_outstanding_sends
);
774 dev_err(&device
->device
, "Unknown send completion packet type- "
775 "%d received!!", nvsp_packet
->hdr
.msg_type
);
778 put_net_device(device
);
781 static int netvsc_send(struct hv_device
*device
,
782 struct hv_netvsc_packet
*packet
)
784 struct netvsc_device
*net_device
;
787 struct nvsp_message sendMessage
;
789 net_device
= get_outbound_net_device(device
);
791 dev_err(&device
->device
, "net device (%p) shutting down..."
792 "ignoring outbound packets", net_device
);
796 sendMessage
.hdr
.msg_type
= NVSP_MSG1_TYPE_SEND_RNDIS_PKT
;
797 if (packet
->is_data_pkt
) {
799 sendMessage
.msg
.v1_msg
.send_rndis_pkt
.channel_type
= 0;
801 /* 1 is RMC_CONTROL; */
802 sendMessage
.msg
.v1_msg
.send_rndis_pkt
.channel_type
= 1;
805 /* Not using send buffer section */
806 sendMessage
.msg
.v1_msg
.send_rndis_pkt
.send_buf_section_index
=
808 sendMessage
.msg
.v1_msg
.send_rndis_pkt
.send_buf_section_size
= 0;
810 if (packet
->page_buf_cnt
) {
811 ret
= vmbus_sendpacket_pagebuffer(device
->channel
,
813 packet
->page_buf_cnt
,
815 sizeof(struct nvsp_message
),
816 (unsigned long)packet
);
818 ret
= vmbus_sendpacket(device
->channel
, &sendMessage
,
819 sizeof(struct nvsp_message
),
820 (unsigned long)packet
,
822 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
827 dev_err(&device
->device
, "Unable to send packet %p ret %d",
830 atomic_inc(&net_device
->num_outstanding_sends
);
831 put_net_device(device
);
835 static void netvsc_send_recv_completion(struct hv_device
*device
,
838 struct nvsp_message recvcompMessage
;
842 recvcompMessage
.hdr
.msg_type
=
843 NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE
;
845 /* FIXME: Pass in the status */
846 recvcompMessage
.msg
.v1_msg
.send_rndis_pkt_complete
.status
=
850 /* Send the completion */
851 ret
= vmbus_sendpacket(device
->channel
, &recvcompMessage
,
852 sizeof(struct nvsp_message
), transaction_id
,
857 } else if (ret
== -1) {
858 /* no more room...wait a bit and attempt to retry 3 times */
860 dev_err(&device
->device
, "unable to send receive completion pkt"
861 " (tid %llx)...retrying %d", transaction_id
, retries
);
865 goto retry_send_cmplt
;
867 dev_err(&device
->device
, "unable to send receive "
868 "completion pkt (tid %llx)...give up retrying",
872 dev_err(&device
->device
, "unable to send receive "
873 "completion pkt - %llx", transaction_id
);
877 /* Send a receive completion packet to RNDIS device (ie NetVsp) */
878 static void netvsc_receive_completion(void *context
)
880 struct hv_netvsc_packet
*packet
= context
;
881 struct hv_device
*device
= (struct hv_device
*)packet
->device
;
882 struct netvsc_device
*net_device
;
883 u64 transaction_id
= 0;
884 bool fsend_receive_comp
= false;
888 * Even though it seems logical to do a GetOutboundNetDevice() here to
889 * send out receive completion, we are using GetInboundNetDevice()
890 * since we may have disable outbound traffic already.
892 net_device
= get_inbound_net_device(device
);
894 dev_err(&device
->device
, "unable to get net device..."
895 "device being destroyed?");
899 /* Overloading use of the lock. */
900 spin_lock_irqsave(&net_device
->recv_pkt_list_lock
, flags
);
902 packet
->xfer_page_pkt
->count
--;
905 * Last one in the line that represent 1 xfer page packet.
906 * Return the xfer page packet itself to the freelist
908 if (packet
->xfer_page_pkt
->count
== 0) {
909 fsend_receive_comp
= true;
910 transaction_id
= packet
->completion
.recv
.recv_completion_tid
;
911 list_add_tail(&packet
->xfer_page_pkt
->list_ent
,
912 &net_device
->recv_pkt_list
);
916 /* Put the packet back */
917 list_add_tail(&packet
->list_ent
, &net_device
->recv_pkt_list
);
918 spin_unlock_irqrestore(&net_device
->recv_pkt_list_lock
, flags
);
920 /* Send a receive completion for the xfer page packet */
921 if (fsend_receive_comp
)
922 netvsc_send_recv_completion(device
, transaction_id
);
924 put_net_device(device
);
927 static void netvsc_receive(struct hv_device
*device
,
928 struct vmpacket_descriptor
*packet
)
930 struct netvsc_device
*net_device
;
931 struct vmtransfer_page_packet_header
*vmxferpage_packet
;
932 struct nvsp_message
*nvsp_packet
;
933 struct hv_netvsc_packet
*netvsc_packet
= NULL
;
935 unsigned long end
, end_virtual
;
936 /* struct netvsc_driver *netvscDriver; */
937 struct xferpage_packet
*xferpage_packet
= NULL
;
939 int count
= 0, bytes_remain
= 0;
943 net_device
= get_inbound_net_device(device
);
945 dev_err(&device
->device
, "unable to get net device..."
946 "device being destroyed?");
951 * All inbound packets other than send completion should be xfer page
954 if (packet
->type
!= VM_PKT_DATA_USING_XFER_PAGES
) {
955 dev_err(&device
->device
, "Unknown packet type received - %d",
957 put_net_device(device
);
961 nvsp_packet
= (struct nvsp_message
*)((unsigned long)packet
+
962 (packet
->offset8
<< 3));
964 /* Make sure this is a valid nvsp packet */
965 if (nvsp_packet
->hdr
.msg_type
!=
966 NVSP_MSG1_TYPE_SEND_RNDIS_PKT
) {
967 dev_err(&device
->device
, "Unknown nvsp packet type received-"
968 " %d", nvsp_packet
->hdr
.msg_type
);
969 put_net_device(device
);
973 vmxferpage_packet
= (struct vmtransfer_page_packet_header
*)packet
;
975 if (vmxferpage_packet
->xfer_pageset_id
!= NETVSC_RECEIVE_BUFFER_ID
) {
976 dev_err(&device
->device
, "Invalid xfer page set id - "
977 "expecting %x got %x", NETVSC_RECEIVE_BUFFER_ID
,
978 vmxferpage_packet
->xfer_pageset_id
);
979 put_net_device(device
);
984 * Grab free packets (range count + 1) to represent this xfer
985 * page packet. +1 to represent the xfer page packet itself.
986 * We grab it here so that we know exactly how many we can
989 spin_lock_irqsave(&net_device
->recv_pkt_list_lock
, flags
);
990 while (!list_empty(&net_device
->recv_pkt_list
)) {
991 list_move_tail(net_device
->recv_pkt_list
.next
, &listHead
);
992 if (++count
== vmxferpage_packet
->range_cnt
+ 1)
995 spin_unlock_irqrestore(&net_device
->recv_pkt_list_lock
, flags
);
998 * We need at least 2 netvsc pkts (1 to represent the xfer
999 * page and at least 1 for the range) i.e. we can handled
1000 * some of the xfer page packet ranges...
1003 dev_err(&device
->device
, "Got only %d netvsc pkt...needed "
1004 "%d pkts. Dropping this xfer page packet completely!",
1005 count
, vmxferpage_packet
->range_cnt
+ 1);
1007 /* Return it to the freelist */
1008 spin_lock_irqsave(&net_device
->recv_pkt_list_lock
, flags
);
1009 for (i
= count
; i
!= 0; i
--) {
1010 list_move_tail(listHead
.next
,
1011 &net_device
->recv_pkt_list
);
1013 spin_unlock_irqrestore(&net_device
->recv_pkt_list_lock
,
1016 netvsc_send_recv_completion(device
,
1017 vmxferpage_packet
->d
.trans_id
);
1019 put_net_device(device
);
1023 /* Remove the 1st packet to represent the xfer page packet itself */
1024 xferpage_packet
= (struct xferpage_packet
*)listHead
.next
;
1025 list_del(&xferpage_packet
->list_ent
);
1027 /* This is how much we can satisfy */
1028 xferpage_packet
->count
= count
- 1;
1030 if (xferpage_packet
->count
!= vmxferpage_packet
->range_cnt
) {
1031 dev_err(&device
->device
, "Needed %d netvsc pkts to satisy "
1032 "this xfer page...got %d",
1033 vmxferpage_packet
->range_cnt
, xferpage_packet
->count
);
1036 /* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */
1037 for (i
= 0; i
< (count
- 1); i
++) {
1038 netvsc_packet
= (struct hv_netvsc_packet
*)listHead
.next
;
1039 list_del(&netvsc_packet
->list_ent
);
1041 /* Initialize the netvsc packet */
1042 netvsc_packet
->xfer_page_pkt
= xferpage_packet
;
1043 netvsc_packet
->completion
.recv
.recv_completion
=
1044 netvsc_receive_completion
;
1045 netvsc_packet
->completion
.recv
.recv_completion_ctx
=
1047 netvsc_packet
->device
= device
;
1048 /* Save this so that we can send it back */
1049 netvsc_packet
->completion
.recv
.recv_completion_tid
=
1050 vmxferpage_packet
->d
.trans_id
;
1052 netvsc_packet
->total_data_buflen
=
1053 vmxferpage_packet
->ranges
[i
].byte_count
;
1054 netvsc_packet
->page_buf_cnt
= 1;
1056 netvsc_packet
->page_buf
[0].len
=
1057 vmxferpage_packet
->ranges
[i
].byte_count
;
1059 start
= virt_to_phys((void *)((unsigned long)net_device
->
1060 recv_buf
+ vmxferpage_packet
->ranges
[i
].byte_offset
));
1062 netvsc_packet
->page_buf
[0].pfn
= start
>> PAGE_SHIFT
;
1063 end_virtual
= (unsigned long)net_device
->recv_buf
1064 + vmxferpage_packet
->ranges
[i
].byte_offset
1065 + vmxferpage_packet
->ranges
[i
].byte_count
- 1;
1066 end
= virt_to_phys((void *)end_virtual
);
1068 /* Calculate the page relative offset */
1069 netvsc_packet
->page_buf
[0].offset
=
1070 vmxferpage_packet
->ranges
[i
].byte_offset
&
1072 if ((end
>> PAGE_SHIFT
) != (start
>> PAGE_SHIFT
)) {
1073 /* Handle frame across multiple pages: */
1074 netvsc_packet
->page_buf
[0].len
=
1075 (netvsc_packet
->page_buf
[0].pfn
<<
1077 + PAGE_SIZE
- start
;
1078 bytes_remain
= netvsc_packet
->total_data_buflen
-
1079 netvsc_packet
->page_buf
[0].len
;
1080 for (j
= 1; j
< NETVSC_PACKET_MAXPAGE
; j
++) {
1081 netvsc_packet
->page_buf
[j
].offset
= 0;
1082 if (bytes_remain
<= PAGE_SIZE
) {
1083 netvsc_packet
->page_buf
[j
].len
=
1087 netvsc_packet
->page_buf
[j
].len
=
1089 bytes_remain
-= PAGE_SIZE
;
1091 netvsc_packet
->page_buf
[j
].pfn
=
1092 virt_to_phys((void *)(end_virtual
-
1093 bytes_remain
)) >> PAGE_SHIFT
;
1094 netvsc_packet
->page_buf_cnt
++;
1095 if (bytes_remain
== 0)
1100 /* Pass it to the upper layer */
1101 ((struct netvsc_driver
*)device
->drv
)->
1102 recv_cb(device
, netvsc_packet
);
1104 netvsc_receive_completion(netvsc_packet
->
1105 completion
.recv
.recv_completion_ctx
);
1108 put_net_device(device
);
1111 static void netvsc_channel_cb(void *context
)
1114 struct hv_device
*device
= context
;
1115 struct netvsc_device
*net_device
;
1118 unsigned char *packet
;
1119 struct vmpacket_descriptor
*desc
;
1120 unsigned char *buffer
;
1121 int bufferlen
= NETVSC_PACKET_SIZE
;
1123 packet
= kzalloc(NETVSC_PACKET_SIZE
* sizeof(unsigned char),
1129 net_device
= get_inbound_net_device(device
);
1131 dev_err(&device
->device
, "net device (%p) shutting down..."
1132 "ignoring inbound packets", net_device
);
1137 ret
= vmbus_recvpacket_raw(device
->channel
, buffer
, bufferlen
,
1138 &bytes_recvd
, &request_id
);
1140 if (bytes_recvd
> 0) {
1141 desc
= (struct vmpacket_descriptor
*)buffer
;
1142 switch (desc
->type
) {
1144 netvsc_send_completion(device
, desc
);
1147 case VM_PKT_DATA_USING_XFER_PAGES
:
1148 netvsc_receive(device
, desc
);
1152 dev_err(&device
->device
,
1153 "unhandled packet type %d, "
1154 "tid %llx len %d\n",
1155 desc
->type
, request_id
,
1161 if (bufferlen
> NETVSC_PACKET_SIZE
) {
1164 bufferlen
= NETVSC_PACKET_SIZE
;
1168 if (bufferlen
> NETVSC_PACKET_SIZE
) {
1171 bufferlen
= NETVSC_PACKET_SIZE
;
1176 } else if (ret
== -2) {
1177 /* Handle large packet */
1178 buffer
= kmalloc(bytes_recvd
, GFP_ATOMIC
);
1179 if (buffer
== NULL
) {
1180 /* Try again next time around */
1181 dev_err(&device
->device
,
1182 "unable to allocate buffer of size "
1183 "(%d)!!", bytes_recvd
);
1187 bufferlen
= bytes_recvd
;
1191 put_net_device(device
);
1198 * netvsc_initialize - Main entry point
1200 int netvsc_initialize(struct hv_driver
*drv
)
1202 struct netvsc_driver
*driver
= (struct netvsc_driver
*)drv
;
1204 drv
->name
= driver_name
;
1205 memcpy(&drv
->dev_type
, &netvsc_device_type
, sizeof(struct hv_guid
));
1207 /* Setup the dispatch table */
1208 driver
->base
.dev_add
= netvsc_device_add
;
1209 driver
->base
.dev_rm
= netvsc_device_remove
;
1210 driver
->base
.cleanup
= netvsc_cleanup
;
1212 driver
->send
= netvsc_send
;
1214 rndis_filter_init(driver
);