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>
23 #include <linux/delay.h>
25 #include <linux/slab.h>
29 #include "rndis_filter.h"
34 static const char *driver_name
= "netvsc";
36 /* {F8615163-DF3E-46c5-913F-F2D2F965ED0E} */
37 static const struct hv_guid netvsc_device_type
= {
39 0x63, 0x51, 0x61, 0xF8, 0x3E, 0xDF, 0xc5, 0x46,
40 0x91, 0x3F, 0xF2, 0xD2, 0xF9, 0x65, 0xED, 0x0E
44 static int netvsc_device_add(struct hv_device
*device
, void *additional_info
);
46 static int netvsc_device_remove(struct hv_device
*device
);
48 static void netvsc_cleanup(struct hv_driver
*driver
);
50 static void netvsc_channel_cb(void *context
);
52 static int netvsc_init_send_buf(struct hv_device
*device
);
54 static int netvsc_init_recv_buf(struct hv_device
*device
);
56 static int netvsc_destroy_send_buf(struct netvsc_device
*net_device
);
58 static int netvsc_destroy_recv_buf(struct netvsc_device
*net_device
);
60 static int netvsc_connect_vsp(struct hv_device
*device
);
62 static void netvsc_send_completion(struct hv_device
*device
,
63 struct vmpacket_descriptor
*packet
);
65 static int netvsc_send(struct hv_device
*device
,
66 struct hv_netvsc_packet
*packet
);
68 static void netvsc_receive(struct hv_device
*device
,
69 struct vmpacket_descriptor
*packet
);
71 static void netvsc_receive_completion(void *context
);
73 static void netvsc_send_recv_completion(struct hv_device
*device
,
77 static struct netvsc_device
*alloc_net_device(struct hv_device
*device
)
79 struct netvsc_device
*net_device
;
81 net_device
= kzalloc(sizeof(struct netvsc_device
), GFP_KERNEL
);
85 /* Set to 2 to allow both inbound and outbound traffic */
86 atomic_cmpxchg(&net_device
->refcnt
, 0, 2);
88 net_device
->dev
= device
;
89 device
->ext
= net_device
;
94 static void free_net_device(struct netvsc_device
*device
)
96 WARN_ON(atomic_read(&device
->refcnt
) == 0);
97 device
->dev
->ext
= NULL
;
102 /* Get the net device object iff exists and its refcount > 1 */
103 static struct netvsc_device
*get_outbound_net_device(struct hv_device
*device
)
105 struct netvsc_device
*net_device
;
107 net_device
= device
->ext
;
108 if (net_device
&& atomic_read(&net_device
->refcnt
) > 1)
109 atomic_inc(&net_device
->refcnt
);
116 /* Get the net device object iff exists and its refcount > 0 */
117 static struct netvsc_device
*get_inbound_net_device(struct hv_device
*device
)
119 struct netvsc_device
*net_device
;
121 net_device
= device
->ext
;
122 if (net_device
&& atomic_read(&net_device
->refcnt
))
123 atomic_inc(&net_device
->refcnt
);
130 static void put_net_device(struct hv_device
*device
)
132 struct netvsc_device
*net_device
;
134 net_device
= device
->ext
;
135 /* ASSERT(netDevice); */
137 atomic_dec(&net_device
->refcnt
);
140 static struct netvsc_device
*release_outbound_net_device(
141 struct hv_device
*device
)
143 struct netvsc_device
*net_device
;
145 net_device
= device
->ext
;
146 if (net_device
== NULL
)
149 /* Busy wait until the ref drop to 2, then set it to 1 */
150 while (atomic_cmpxchg(&net_device
->refcnt
, 2, 1) != 2)
156 static struct netvsc_device
*release_inbound_net_device(
157 struct hv_device
*device
)
159 struct netvsc_device
*net_device
;
161 net_device
= device
->ext
;
162 if (net_device
== NULL
)
165 /* Busy wait until the ref drop to 1, then set it to 0 */
166 while (atomic_cmpxchg(&net_device
->refcnt
, 1, 0) != 1)
174 * netvsc_initialize - Main entry point
176 int netvsc_initialize(struct hv_driver
*drv
)
178 struct netvsc_driver
*driver
= (struct netvsc_driver
*)drv
;
180 DPRINT_DBG(NETVSC
, "sizeof(struct hv_netvsc_packet)=%zd, "
181 "sizeof(struct nvsp_message)=%zd, "
182 "sizeof(struct vmtransfer_page_packet_header)=%zd",
183 sizeof(struct hv_netvsc_packet
),
184 sizeof(struct nvsp_message
),
185 sizeof(struct vmtransfer_page_packet_header
));
187 /* Make sure we are at least 2 pages since 1 page is used for control */
188 /* ASSERT(driver->RingBufferSize >= (PAGE_SIZE << 1)); */
190 drv
->name
= driver_name
;
191 memcpy(&drv
->dev_type
, &netvsc_device_type
, sizeof(struct hv_guid
));
193 /* Make sure it is set by the caller */
194 /* FIXME: These probably should still be tested in some way */
195 /* ASSERT(driver->OnReceiveCallback); */
196 /* ASSERT(driver->OnLinkStatusChanged); */
198 /* Setup the dispatch table */
199 driver
->base
.dev_add
= netvsc_device_add
;
200 driver
->base
.dev_rm
= netvsc_device_remove
;
201 driver
->base
.cleanup
= netvsc_cleanup
;
203 driver
->send
= netvsc_send
;
205 rndis_filter_init(driver
);
209 static int netvsc_init_recv_buf(struct hv_device
*device
)
212 struct netvsc_device
*net_device
;
213 struct nvsp_message
*init_packet
;
215 net_device
= get_outbound_net_device(device
);
217 DPRINT_ERR(NETVSC
, "unable to get net device..."
218 "device being destroyed?");
221 /* ASSERT(netDevice->ReceiveBufferSize > 0); */
222 /* page-size grandularity */
223 /* ASSERT((netDevice->ReceiveBufferSize & (PAGE_SIZE - 1)) == 0); */
225 net_device
->recv_buf
=
226 osd_page_alloc(net_device
->recv_buf_size
>> PAGE_SHIFT
);
227 if (!net_device
->recv_buf
) {
229 "unable to allocate receive buffer of size %d",
230 net_device
->recv_buf_size
);
234 /* page-aligned buffer */
235 /* ASSERT(((unsigned long)netDevice->ReceiveBuffer & (PAGE_SIZE - 1)) == */
238 DPRINT_INFO(NETVSC
, "Establishing receive buffer's GPADL...");
241 * Establish the gpadl handle for this buffer on this
242 * channel. Note: This call uses the vmbus connection rather
243 * than the channel to establish the gpadl handle.
245 ret
= vmbus_establish_gpadl(device
->channel
, net_device
->recv_buf
,
246 net_device
->recv_buf_size
,
247 &net_device
->recv_buf_gpadl_handle
);
250 "unable to establish receive buffer's gpadl");
254 /* osd_waitevent_wait(ext->ChannelInitEvent); */
256 /* Notify the NetVsp of the gpadl handle */
257 DPRINT_INFO(NETVSC
, "Sending NvspMessage1TypeSendReceiveBuffer...");
259 init_packet
= &net_device
->channel_init_pkt
;
261 memset(init_packet
, 0, sizeof(struct nvsp_message
));
263 init_packet
->hdr
.msg_type
= NVSP_MSG1_TYPE_SEND_RECV_BUF
;
264 init_packet
->msg
.v1_msg
.send_recv_buf
.
265 gpadl_handle
= net_device
->recv_buf_gpadl_handle
;
266 init_packet
->msg
.v1_msg
.
267 send_recv_buf
.id
= NETVSC_RECEIVE_BUFFER_ID
;
269 /* Send the gpadl notification request */
270 ret
= vmbus_sendpacket(device
->channel
, init_packet
,
271 sizeof(struct nvsp_message
),
272 (unsigned long)init_packet
,
274 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
277 "unable to send receive buffer's gpadl to netvsp");
281 osd_waitevent_wait(net_device
->channel_init_event
);
283 /* Check the response */
284 if (init_packet
->msg
.v1_msg
.
285 send_recv_buf_complete
.status
!= NVSP_STAT_SUCCESS
) {
286 DPRINT_ERR(NETVSC
, "Unable to complete receive buffer "
287 "initialzation with NetVsp - status %d",
288 init_packet
->msg
.v1_msg
.
289 send_recv_buf_complete
.status
);
294 /* Parse the response */
295 /* ASSERT(netDevice->ReceiveSectionCount == 0); */
296 /* ASSERT(netDevice->ReceiveSections == NULL); */
298 net_device
->recv_section_cnt
= init_packet
->msg
.
299 v1_msg
.send_recv_buf_complete
.num_sections
;
301 net_device
->recv_section
= kmalloc(net_device
->recv_section_cnt
302 * sizeof(struct nvsp_1_receive_buffer_section
), GFP_KERNEL
);
303 if (net_device
->recv_section
== NULL
) {
308 memcpy(net_device
->recv_section
,
309 init_packet
->msg
.v1_msg
.
310 send_recv_buf_complete
.sections
,
311 net_device
->recv_section_cnt
*
312 sizeof(struct nvsp_1_receive_buffer_section
));
314 DPRINT_INFO(NETVSC
, "Receive sections info (count %d, offset %d, "
315 "endoffset %d, suballoc size %d, num suballocs %d)",
316 net_device
->recv_section_cnt
,
317 net_device
->recv_section
[0].offset
,
318 net_device
->recv_section
[0].end_offset
,
319 net_device
->recv_section
[0].sub_alloc_size
,
320 net_device
->recv_section
[0].num_sub_allocs
);
323 * For 1st release, there should only be 1 section that represents the
324 * entire receive buffer
326 if (net_device
->recv_section_cnt
!= 1 ||
327 net_device
->recv_section
->offset
!= 0) {
335 netvsc_destroy_recv_buf(net_device
);
338 put_net_device(device
);
342 static int netvsc_init_send_buf(struct hv_device
*device
)
345 struct netvsc_device
*net_device
;
346 struct nvsp_message
*init_packet
;
348 net_device
= get_outbound_net_device(device
);
350 DPRINT_ERR(NETVSC
, "unable to get net device..."
351 "device being destroyed?");
354 if (net_device
->send_buf_size
<= 0) {
359 /* page-size grandularity */
360 /* ASSERT((netDevice->SendBufferSize & (PAGE_SIZE - 1)) == 0); */
362 net_device
->send_buf
=
363 osd_page_alloc(net_device
->send_buf_size
>> PAGE_SHIFT
);
364 if (!net_device
->send_buf
) {
365 DPRINT_ERR(NETVSC
, "unable to allocate send buffer of size %d",
366 net_device
->send_buf_size
);
370 /* page-aligned buffer */
371 /* ASSERT(((unsigned long)netDevice->SendBuffer & (PAGE_SIZE - 1)) == 0); */
373 DPRINT_INFO(NETVSC
, "Establishing send buffer's GPADL...");
376 * Establish the gpadl handle for this buffer on this
377 * channel. Note: This call uses the vmbus connection rather
378 * than the channel to establish the gpadl handle.
380 ret
= vmbus_establish_gpadl(device
->channel
, net_device
->send_buf
,
381 net_device
->send_buf_size
,
382 &net_device
->send_buf_gpadl_handle
);
384 DPRINT_ERR(NETVSC
, "unable to establish send buffer's gpadl");
388 /* osd_waitevent_wait(ext->ChannelInitEvent); */
390 /* Notify the NetVsp of the gpadl handle */
391 DPRINT_INFO(NETVSC
, "Sending NvspMessage1TypeSendSendBuffer...");
393 init_packet
= &net_device
->channel_init_pkt
;
395 memset(init_packet
, 0, sizeof(struct nvsp_message
));
397 init_packet
->hdr
.msg_type
= NVSP_MSG1_TYPE_SEND_SEND_BUF
;
398 init_packet
->msg
.v1_msg
.send_recv_buf
.
399 gpadl_handle
= net_device
->send_buf_gpadl_handle
;
400 init_packet
->msg
.v1_msg
.send_recv_buf
.id
=
401 NETVSC_SEND_BUFFER_ID
;
403 /* Send the gpadl notification request */
404 ret
= vmbus_sendpacket(device
->channel
, init_packet
,
405 sizeof(struct nvsp_message
),
406 (unsigned long)init_packet
,
408 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
411 "unable to send receive buffer's gpadl to netvsp");
415 osd_waitevent_wait(net_device
->channel_init_event
);
417 /* Check the response */
418 if (init_packet
->msg
.v1_msg
.
419 send_send_buf_complete
.status
!= NVSP_STAT_SUCCESS
) {
420 DPRINT_ERR(NETVSC
, "Unable to complete send buffer "
421 "initialzation with NetVsp - status %d",
422 init_packet
->msg
.v1_msg
.
423 send_send_buf_complete
.status
);
428 net_device
->send_section_size
= init_packet
->
429 msg
.v1_msg
.send_send_buf_complete
.section_size
;
434 netvsc_destroy_send_buf(net_device
);
437 put_net_device(device
);
441 static int netvsc_destroy_recv_buf(struct netvsc_device
*net_device
)
443 struct nvsp_message
*revoke_packet
;
447 * If we got a section count, it means we received a
448 * SendReceiveBufferComplete msg (ie sent
449 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
450 * to send a revoke msg here
452 if (net_device
->recv_section_cnt
) {
454 "Sending NvspMessage1TypeRevokeReceiveBuffer...");
456 /* Send the revoke receive buffer */
457 revoke_packet
= &net_device
->revoke_packet
;
458 memset(revoke_packet
, 0, sizeof(struct nvsp_message
));
460 revoke_packet
->hdr
.msg_type
=
461 NVSP_MSG1_TYPE_REVOKE_RECV_BUF
;
462 revoke_packet
->msg
.v1_msg
.
463 revoke_recv_buf
.id
= NETVSC_RECEIVE_BUFFER_ID
;
465 ret
= vmbus_sendpacket(net_device
->dev
->channel
,
467 sizeof(struct nvsp_message
),
468 (unsigned long)revoke_packet
,
469 VM_PKT_DATA_INBAND
, 0);
471 * If we failed here, we might as well return and
472 * have a leak rather than continue and a bugchk
475 DPRINT_ERR(NETVSC
, "unable to send revoke receive "
481 /* Teardown the gpadl on the vsp end */
482 if (net_device
->recv_buf_gpadl_handle
) {
483 DPRINT_INFO(NETVSC
, "Tearing down receive buffer's GPADL...");
485 ret
= vmbus_teardown_gpadl(net_device
->dev
->channel
,
486 net_device
->recv_buf_gpadl_handle
);
488 /* If we failed here, we might as well return and have a leak rather than continue and a bugchk */
491 "unable to teardown receive buffer's gpadl");
494 net_device
->recv_buf_gpadl_handle
= 0;
497 if (net_device
->recv_buf
) {
498 DPRINT_INFO(NETVSC
, "Freeing up receive buffer...");
500 /* Free up the receive buffer */
501 osd_page_free(net_device
->recv_buf
,
502 net_device
->recv_buf_size
>> PAGE_SHIFT
);
503 net_device
->recv_buf
= NULL
;
506 if (net_device
->recv_section
) {
507 net_device
->recv_section_cnt
= 0;
508 kfree(net_device
->recv_section
);
509 net_device
->recv_section
= NULL
;
515 static int netvsc_destroy_send_buf(struct netvsc_device
*net_device
)
517 struct nvsp_message
*revoke_packet
;
521 * If we got a section count, it means we received a
522 * SendReceiveBufferComplete msg (ie sent
523 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
524 * to send a revoke msg here
526 if (net_device
->send_section_size
) {
528 "Sending NvspMessage1TypeRevokeSendBuffer...");
530 /* Send the revoke send buffer */
531 revoke_packet
= &net_device
->revoke_packet
;
532 memset(revoke_packet
, 0, sizeof(struct nvsp_message
));
534 revoke_packet
->hdr
.msg_type
=
535 NVSP_MSG1_TYPE_REVOKE_SEND_BUF
;
536 revoke_packet
->msg
.v1_msg
.
537 revoke_send_buf
.id
= NETVSC_SEND_BUFFER_ID
;
539 ret
= vmbus_sendpacket(net_device
->dev
->channel
,
541 sizeof(struct nvsp_message
),
542 (unsigned long)revoke_packet
,
543 VM_PKT_DATA_INBAND
, 0);
545 * If we failed here, we might as well return and have a leak
546 * rather than continue and a bugchk
549 DPRINT_ERR(NETVSC
, "unable to send revoke send buffer "
555 /* Teardown the gpadl on the vsp end */
556 if (net_device
->send_buf_gpadl_handle
) {
557 DPRINT_INFO(NETVSC
, "Tearing down send buffer's GPADL...");
558 ret
= vmbus_teardown_gpadl(net_device
->dev
->channel
,
559 net_device
->send_buf_gpadl_handle
);
562 * If we failed here, we might as well return and have a leak
563 * rather than continue and a bugchk
566 DPRINT_ERR(NETVSC
, "unable to teardown send buffer's "
570 net_device
->send_buf_gpadl_handle
= 0;
573 if (net_device
->send_buf
) {
574 DPRINT_INFO(NETVSC
, "Freeing up send buffer...");
576 /* Free up the receive buffer */
577 osd_page_free(net_device
->send_buf
,
578 net_device
->send_buf_size
>> PAGE_SHIFT
);
579 net_device
->send_buf
= NULL
;
586 static int netvsc_connect_vsp(struct hv_device
*device
)
589 struct netvsc_device
*net_device
;
590 struct nvsp_message
*init_packet
;
593 net_device
= get_outbound_net_device(device
);
595 DPRINT_ERR(NETVSC
, "unable to get net device..."
596 "device being destroyed?");
600 init_packet
= &net_device
->channel_init_pkt
;
602 memset(init_packet
, 0, sizeof(struct nvsp_message
));
603 init_packet
->hdr
.msg_type
= NVSP_MSG_TYPE_INIT
;
604 init_packet
->msg
.init_msg
.init
.min_protocol_ver
=
605 NVSP_MIN_PROTOCOL_VERSION
;
606 init_packet
->msg
.init_msg
.init
.max_protocol_ver
=
607 NVSP_MAX_PROTOCOL_VERSION
;
609 DPRINT_INFO(NETVSC
, "Sending NvspMessageTypeInit...");
611 /* Send the init request */
612 ret
= vmbus_sendpacket(device
->channel
, init_packet
,
613 sizeof(struct nvsp_message
),
614 (unsigned long)init_packet
,
616 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
619 DPRINT_ERR(NETVSC
, "unable to send NvspMessageTypeInit");
623 osd_waitevent_wait(net_device
->channel_init_event
);
625 /* Now, check the response */
626 /* ASSERT(initPacket->Messages.InitMessages.InitComplete.MaximumMdlChainLength <= MAX_MULTIPAGE_BUFFER_COUNT); */
627 DPRINT_INFO(NETVSC
, "NvspMessageTypeInit status(%d) max mdl chain (%d)",
628 init_packet
->msg
.init_msg
.init_complete
.status
,
629 init_packet
->msg
.init_msg
.
630 init_complete
.max_mdl_chain_len
);
632 if (init_packet
->msg
.init_msg
.init_complete
.status
!=
635 "unable to initialize with netvsp (status 0x%x)",
636 init_packet
->msg
.init_msg
.init_complete
.status
);
641 if (init_packet
->msg
.init_msg
.init_complete
.
642 negotiated_protocol_ver
!= NVSP_PROTOCOL_VERSION_1
) {
643 DPRINT_ERR(NETVSC
, "unable to initialize with netvsp "
644 "(version expected 1 got %d)",
645 init_packet
->msg
.init_msg
.
646 init_complete
.negotiated_protocol_ver
);
650 DPRINT_INFO(NETVSC
, "Sending NvspMessage1TypeSendNdisVersion...");
652 /* Send the ndis version */
653 memset(init_packet
, 0, sizeof(struct nvsp_message
));
655 ndis_version
= 0x00050000;
657 init_packet
->hdr
.msg_type
= NVSP_MSG1_TYPE_SEND_NDIS_VER
;
658 init_packet
->msg
.v1_msg
.
659 send_ndis_ver
.ndis_major_ver
=
660 (ndis_version
& 0xFFFF0000) >> 16;
661 init_packet
->msg
.v1_msg
.
662 send_ndis_ver
.ndis_minor_ver
=
663 ndis_version
& 0xFFFF;
665 /* Send the init request */
666 ret
= vmbus_sendpacket(device
->channel
, init_packet
,
667 sizeof(struct nvsp_message
),
668 (unsigned long)init_packet
,
669 VM_PKT_DATA_INBAND
, 0);
672 "unable to send NvspMessage1TypeSendNdisVersion");
677 * BUGBUG - We have to wait for the above msg since the
678 * netvsp uses KMCL which acknowledges packet (completion
679 * packet) since our Vmbus always set the
680 * VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED flag
682 /* osd_waitevent_wait(NetVscChannel->ChannelInitEvent); */
684 /* Post the big receive buffer to NetVSP */
685 ret
= netvsc_init_recv_buf(device
);
687 ret
= netvsc_init_send_buf(device
);
690 put_net_device(device
);
694 static void NetVscDisconnectFromVsp(struct netvsc_device
*net_device
)
696 netvsc_destroy_recv_buf(net_device
);
697 netvsc_destroy_send_buf(net_device
);
701 * netvsc_device_add - Callback when the device belonging to this
704 static int netvsc_device_add(struct hv_device
*device
, void *additional_info
)
708 struct netvsc_device
*net_device
;
709 struct hv_netvsc_packet
*packet
, *pos
;
710 struct netvsc_driver
*net_driver
=
711 (struct netvsc_driver
*)device
->drv
;
713 net_device
= alloc_net_device(device
);
719 DPRINT_DBG(NETVSC
, "netvsc channel object allocated - %p", net_device
);
721 /* Initialize the NetVSC channel extension */
722 net_device
->recv_buf_size
= NETVSC_RECEIVE_BUFFER_SIZE
;
723 spin_lock_init(&net_device
->recv_pkt_list_lock
);
725 net_device
->send_buf_size
= NETVSC_SEND_BUFFER_SIZE
;
727 INIT_LIST_HEAD(&net_device
->recv_pkt_list
);
729 for (i
= 0; i
< NETVSC_RECEIVE_PACKETLIST_COUNT
; i
++) {
730 packet
= kzalloc(sizeof(struct hv_netvsc_packet
) +
731 (NETVSC_RECEIVE_SG_COUNT
*
732 sizeof(struct hv_page_buffer
)), GFP_KERNEL
);
734 DPRINT_DBG(NETVSC
, "unable to allocate netvsc pkts "
735 "for receive pool (wanted %d got %d)",
736 NETVSC_RECEIVE_PACKETLIST_COUNT
, i
);
739 list_add_tail(&packet
->list_ent
,
740 &net_device
->recv_pkt_list
);
742 net_device
->channel_init_event
= osd_waitevent_create();
743 if (!net_device
->channel_init_event
) {
748 /* Open the channel */
749 ret
= vmbus_open(device
->channel
, net_driver
->ring_buf_size
,
750 net_driver
->ring_buf_size
, NULL
, 0,
751 netvsc_channel_cb
, device
);
754 DPRINT_ERR(NETVSC
, "unable to open channel: %d", ret
);
759 /* Channel is opened */
760 DPRINT_INFO(NETVSC
, "*** NetVSC channel opened successfully! ***");
762 /* Connect with the NetVsp */
763 ret
= netvsc_connect_vsp(device
);
765 DPRINT_ERR(NETVSC
, "unable to connect to NetVSP - %d", ret
);
770 DPRINT_INFO(NETVSC
, "*** NetVSC channel handshake result - %d ***",
776 /* Now, we can close the channel safely */
777 vmbus_close(device
->channel
);
782 kfree(net_device
->channel_init_event
);
784 list_for_each_entry_safe(packet
, pos
,
785 &net_device
->recv_pkt_list
,
787 list_del(&packet
->list_ent
);
791 release_outbound_net_device(device
);
792 release_inbound_net_device(device
);
794 free_net_device(net_device
);
801 * netvsc_device_remove - Callback when the root bus device is removed
803 static int netvsc_device_remove(struct hv_device
*device
)
805 struct netvsc_device
*net_device
;
806 struct hv_netvsc_packet
*netvsc_packet
, *pos
;
808 DPRINT_INFO(NETVSC
, "Disabling outbound traffic on net device (%p)...",
811 /* Stop outbound traffic ie sends and receives completions */
812 net_device
= release_outbound_net_device(device
);
814 DPRINT_ERR(NETVSC
, "No net device present!!");
818 /* Wait for all send completions */
819 while (atomic_read(&net_device
->num_outstanding_sends
)) {
820 DPRINT_INFO(NETVSC
, "waiting for %d requests to complete...",
821 atomic_read(&net_device
->num_outstanding_sends
));
825 DPRINT_INFO(NETVSC
, "Disconnecting from netvsp...");
827 NetVscDisconnectFromVsp(net_device
);
829 DPRINT_INFO(NETVSC
, "Disabling inbound traffic on net device (%p)...",
832 /* Stop inbound traffic ie receives and sends completions */
833 net_device
= release_inbound_net_device(device
);
835 /* At this point, no one should be accessing netDevice except in here */
836 DPRINT_INFO(NETVSC
, "net device (%p) safe to remove", net_device
);
838 /* Now, we can close the channel safely */
839 vmbus_close(device
->channel
);
841 /* Release all resources */
842 list_for_each_entry_safe(netvsc_packet
, pos
,
843 &net_device
->recv_pkt_list
, list_ent
) {
844 list_del(&netvsc_packet
->list_ent
);
845 kfree(netvsc_packet
);
848 kfree(net_device
->channel_init_event
);
849 free_net_device(net_device
);
854 * netvsc_cleanup - Perform any cleanup when the driver is removed
856 static void netvsc_cleanup(struct hv_driver
*drv
)
860 static void netvsc_send_completion(struct hv_device
*device
,
861 struct vmpacket_descriptor
*packet
)
863 struct netvsc_device
*net_device
;
864 struct nvsp_message
*nvsp_packet
;
865 struct hv_netvsc_packet
*nvsc_packet
;
867 net_device
= get_inbound_net_device(device
);
869 DPRINT_ERR(NETVSC
, "unable to get net device..."
870 "device being destroyed?");
874 nvsp_packet
= (struct nvsp_message
*)((unsigned long)packet
+
875 (packet
->offset8
<< 3));
877 DPRINT_DBG(NETVSC
, "send completion packet - type %d",
878 nvsp_packet
->hdr
.msg_type
);
880 if ((nvsp_packet
->hdr
.msg_type
== NVSP_MSG_TYPE_INIT_COMPLETE
) ||
881 (nvsp_packet
->hdr
.msg_type
==
882 NVSP_MSG1_TYPE_SEND_RECV_BUF_COMPLETE
) ||
883 (nvsp_packet
->hdr
.msg_type
==
884 NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE
)) {
885 /* Copy the response back */
886 memcpy(&net_device
->channel_init_pkt
, nvsp_packet
,
887 sizeof(struct nvsp_message
));
888 osd_waitevent_set(net_device
->channel_init_event
);
889 } else if (nvsp_packet
->hdr
.msg_type
==
890 NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE
) {
891 /* Get the send context */
892 nvsc_packet
= (struct hv_netvsc_packet
*)(unsigned long)
894 /* ASSERT(nvscPacket); */
896 /* Notify the layer above us */
897 nvsc_packet
->completion
.send
.send_completion(
898 nvsc_packet
->completion
.send
.send_completion_ctx
);
900 atomic_dec(&net_device
->num_outstanding_sends
);
902 DPRINT_ERR(NETVSC
, "Unknown send completion packet type - "
903 "%d received!!", nvsp_packet
->hdr
.msg_type
);
906 put_net_device(device
);
909 static int netvsc_send(struct hv_device
*device
,
910 struct hv_netvsc_packet
*packet
)
912 struct netvsc_device
*net_device
;
915 struct nvsp_message sendMessage
;
917 net_device
= get_outbound_net_device(device
);
919 DPRINT_ERR(NETVSC
, "net device (%p) shutting down..."
920 "ignoring outbound packets", net_device
);
924 sendMessage
.hdr
.msg_type
= NVSP_MSG1_TYPE_SEND_RNDIS_PKT
;
925 if (packet
->is_data_pkt
) {
927 sendMessage
.msg
.v1_msg
.send_rndis_pkt
.channel_type
= 0;
929 /* 1 is RMC_CONTROL; */
930 sendMessage
.msg
.v1_msg
.send_rndis_pkt
.channel_type
= 1;
933 /* Not using send buffer section */
934 sendMessage
.msg
.v1_msg
.send_rndis_pkt
.send_buf_section_index
=
936 sendMessage
.msg
.v1_msg
.send_rndis_pkt
.send_buf_section_size
= 0;
938 if (packet
->page_buf_cnt
) {
939 ret
= vmbus_sendpacket_pagebuffer(device
->channel
,
941 packet
->page_buf_cnt
,
943 sizeof(struct nvsp_message
),
944 (unsigned long)packet
);
946 ret
= vmbus_sendpacket(device
->channel
, &sendMessage
,
947 sizeof(struct nvsp_message
),
948 (unsigned long)packet
,
950 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
955 DPRINT_ERR(NETVSC
, "Unable to send packet %p ret %d",
958 atomic_inc(&net_device
->num_outstanding_sends
);
959 put_net_device(device
);
963 static void netvsc_receive(struct hv_device
*device
,
964 struct vmpacket_descriptor
*packet
)
966 struct netvsc_device
*net_device
;
967 struct vmtransfer_page_packet_header
*vmxferpage_packet
;
968 struct nvsp_message
*nvsp_packet
;
969 struct hv_netvsc_packet
*netvsc_packet
= NULL
;
971 unsigned long end
, end_virtual
;
972 /* struct netvsc_driver *netvscDriver; */
973 struct xferpage_packet
*xferpage_packet
= NULL
;
975 int count
= 0, bytes_remain
= 0;
979 net_device
= get_inbound_net_device(device
);
981 DPRINT_ERR(NETVSC
, "unable to get net device..."
982 "device being destroyed?");
987 * All inbound packets other than send completion should be xfer page
990 if (packet
->type
!= VM_PKT_DATA_USING_XFER_PAGES
) {
991 DPRINT_ERR(NETVSC
, "Unknown packet type received - %d",
993 put_net_device(device
);
997 nvsp_packet
= (struct nvsp_message
*)((unsigned long)packet
+
998 (packet
->offset8
<< 3));
1000 /* Make sure this is a valid nvsp packet */
1001 if (nvsp_packet
->hdr
.msg_type
!=
1002 NVSP_MSG1_TYPE_SEND_RNDIS_PKT
) {
1003 DPRINT_ERR(NETVSC
, "Unknown nvsp packet type received - %d",
1004 nvsp_packet
->hdr
.msg_type
);
1005 put_net_device(device
);
1009 DPRINT_DBG(NETVSC
, "NVSP packet received - type %d",
1010 nvsp_packet
->hdr
.msg_type
);
1012 vmxferpage_packet
= (struct vmtransfer_page_packet_header
*)packet
;
1014 if (vmxferpage_packet
->xfer_pageset_id
!= NETVSC_RECEIVE_BUFFER_ID
) {
1015 DPRINT_ERR(NETVSC
, "Invalid xfer page set id - "
1016 "expecting %x got %x", NETVSC_RECEIVE_BUFFER_ID
,
1017 vmxferpage_packet
->xfer_pageset_id
);
1018 put_net_device(device
);
1022 DPRINT_DBG(NETVSC
, "xfer page - range count %d",
1023 vmxferpage_packet
->range_cnt
);
1026 * Grab free packets (range count + 1) to represent this xfer
1027 * page packet. +1 to represent the xfer page packet itself.
1028 * We grab it here so that we know exactly how many we can
1031 spin_lock_irqsave(&net_device
->recv_pkt_list_lock
, flags
);
1032 while (!list_empty(&net_device
->recv_pkt_list
)) {
1033 list_move_tail(net_device
->recv_pkt_list
.next
, &listHead
);
1034 if (++count
== vmxferpage_packet
->range_cnt
+ 1)
1037 spin_unlock_irqrestore(&net_device
->recv_pkt_list_lock
, flags
);
1040 * We need at least 2 netvsc pkts (1 to represent the xfer
1041 * page and at least 1 for the range) i.e. we can handled
1042 * some of the xfer page packet ranges...
1045 DPRINT_ERR(NETVSC
, "Got only %d netvsc pkt...needed %d pkts. "
1046 "Dropping this xfer page packet completely!",
1047 count
, vmxferpage_packet
->range_cnt
+ 1);
1049 /* Return it to the freelist */
1050 spin_lock_irqsave(&net_device
->recv_pkt_list_lock
, flags
);
1051 for (i
= count
; i
!= 0; i
--) {
1052 list_move_tail(listHead
.next
,
1053 &net_device
->recv_pkt_list
);
1055 spin_unlock_irqrestore(&net_device
->recv_pkt_list_lock
,
1058 netvsc_send_recv_completion(device
,
1059 vmxferpage_packet
->d
.trans_id
);
1061 put_net_device(device
);
1065 /* Remove the 1st packet to represent the xfer page packet itself */
1066 xferpage_packet
= (struct xferpage_packet
*)listHead
.next
;
1067 list_del(&xferpage_packet
->list_ent
);
1069 /* This is how much we can satisfy */
1070 xferpage_packet
->count
= count
- 1;
1071 /* ASSERT(xferpagePacket->Count > 0 && xferpagePacket->Count <= */
1072 /* vmxferpagePacket->RangeCount); */
1074 if (xferpage_packet
->count
!= vmxferpage_packet
->range_cnt
) {
1075 DPRINT_INFO(NETVSC
, "Needed %d netvsc pkts to satisy this xfer "
1076 "page...got %d", vmxferpage_packet
->range_cnt
,
1077 xferpage_packet
->count
);
1080 /* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */
1081 for (i
= 0; i
< (count
- 1); i
++) {
1082 netvsc_packet
= (struct hv_netvsc_packet
*)listHead
.next
;
1083 list_del(&netvsc_packet
->list_ent
);
1085 /* Initialize the netvsc packet */
1086 netvsc_packet
->xfer_page_pkt
= xferpage_packet
;
1087 netvsc_packet
->completion
.recv
.recv_completion
=
1088 netvsc_receive_completion
;
1089 netvsc_packet
->completion
.recv
.recv_completion_ctx
=
1091 netvsc_packet
->device
= device
;
1092 /* Save this so that we can send it back */
1093 netvsc_packet
->completion
.recv
.recv_completion_tid
=
1094 vmxferpage_packet
->d
.trans_id
;
1096 netvsc_packet
->total_data_buflen
=
1097 vmxferpage_packet
->ranges
[i
].byte_count
;
1098 netvsc_packet
->page_buf_cnt
= 1;
1100 /* ASSERT(vmxferpagePacket->Ranges[i].ByteOffset + */
1101 /* vmxferpagePacket->Ranges[i].ByteCount < */
1102 /* netDevice->ReceiveBufferSize); */
1104 netvsc_packet
->page_buf
[0].len
=
1105 vmxferpage_packet
->ranges
[i
].byte_count
;
1107 start
= virt_to_phys((void *)((unsigned long)net_device
->
1108 recv_buf
+ vmxferpage_packet
->ranges
[i
].byte_offset
));
1110 netvsc_packet
->page_buf
[0].pfn
= start
>> PAGE_SHIFT
;
1111 end_virtual
= (unsigned long)net_device
->recv_buf
1112 + vmxferpage_packet
->ranges
[i
].byte_offset
1113 + vmxferpage_packet
->ranges
[i
].byte_count
- 1;
1114 end
= virt_to_phys((void *)end_virtual
);
1116 /* Calculate the page relative offset */
1117 netvsc_packet
->page_buf
[0].offset
=
1118 vmxferpage_packet
->ranges
[i
].byte_offset
&
1120 if ((end
>> PAGE_SHIFT
) != (start
>> PAGE_SHIFT
)) {
1121 /* Handle frame across multiple pages: */
1122 netvsc_packet
->page_buf
[0].len
=
1123 (netvsc_packet
->page_buf
[0].pfn
<<
1125 + PAGE_SIZE
- start
;
1126 bytes_remain
= netvsc_packet
->total_data_buflen
-
1127 netvsc_packet
->page_buf
[0].len
;
1128 for (j
= 1; j
< NETVSC_PACKET_MAXPAGE
; j
++) {
1129 netvsc_packet
->page_buf
[j
].offset
= 0;
1130 if (bytes_remain
<= PAGE_SIZE
) {
1131 netvsc_packet
->page_buf
[j
].len
=
1135 netvsc_packet
->page_buf
[j
].len
=
1137 bytes_remain
-= PAGE_SIZE
;
1139 netvsc_packet
->page_buf
[j
].pfn
=
1140 virt_to_phys((void *)(end_virtual
-
1141 bytes_remain
)) >> PAGE_SHIFT
;
1142 netvsc_packet
->page_buf_cnt
++;
1143 if (bytes_remain
== 0)
1146 /* ASSERT(bytesRemain == 0); */
1148 DPRINT_DBG(NETVSC
, "[%d] - (abs offset %u len %u) => "
1149 "(pfn %llx, offset %u, len %u)", i
,
1150 vmxferpage_packet
->ranges
[i
].byte_offset
,
1151 vmxferpage_packet
->ranges
[i
].byte_count
,
1152 netvsc_packet
->page_buf
[0].pfn
,
1153 netvsc_packet
->page_buf
[0].offset
,
1154 netvsc_packet
->page_buf
[0].len
);
1156 /* Pass it to the upper layer */
1157 ((struct netvsc_driver
*)device
->drv
)->
1158 recv_cb(device
, netvsc_packet
);
1160 netvsc_receive_completion(netvsc_packet
->
1161 completion
.recv
.recv_completion_ctx
);
1164 /* ASSERT(list_empty(&listHead)); */
1166 put_net_device(device
);
1169 static void netvsc_send_recv_completion(struct hv_device
*device
,
1172 struct nvsp_message recvcompMessage
;
1176 DPRINT_DBG(NETVSC
, "Sending receive completion pkt - %llx",
1179 recvcompMessage
.hdr
.msg_type
=
1180 NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE
;
1182 /* FIXME: Pass in the status */
1183 recvcompMessage
.msg
.v1_msg
.send_rndis_pkt_complete
.status
=
1187 /* Send the completion */
1188 ret
= vmbus_sendpacket(device
->channel
, &recvcompMessage
,
1189 sizeof(struct nvsp_message
), transaction_id
,
1194 } else if (ret
== -1) {
1195 /* no more room...wait a bit and attempt to retry 3 times */
1197 DPRINT_ERR(NETVSC
, "unable to send receive completion pkt "
1198 "(tid %llx)...retrying %d", transaction_id
, retries
);
1202 goto retry_send_cmplt
;
1204 DPRINT_ERR(NETVSC
, "unable to send receive completion "
1205 "pkt (tid %llx)...give up retrying",
1209 DPRINT_ERR(NETVSC
, "unable to send receive completion pkt - "
1210 "%llx", transaction_id
);
1214 /* Send a receive completion packet to RNDIS device (ie NetVsp) */
1215 static void netvsc_receive_completion(void *context
)
1217 struct hv_netvsc_packet
*packet
= context
;
1218 struct hv_device
*device
= (struct hv_device
*)packet
->device
;
1219 struct netvsc_device
*net_device
;
1220 u64 transaction_id
= 0;
1221 bool fsend_receive_comp
= false;
1222 unsigned long flags
;
1224 /* ASSERT(packet->XferPagePacket); */
1227 * Even though it seems logical to do a GetOutboundNetDevice() here to
1228 * send out receive completion, we are using GetInboundNetDevice()
1229 * since we may have disable outbound traffic already.
1231 net_device
= get_inbound_net_device(device
);
1233 DPRINT_ERR(NETVSC
, "unable to get net device..."
1234 "device being destroyed?");
1238 /* Overloading use of the lock. */
1239 spin_lock_irqsave(&net_device
->recv_pkt_list_lock
, flags
);
1241 /* ASSERT(packet->XferPagePacket->Count > 0); */
1242 packet
->xfer_page_pkt
->count
--;
1245 * Last one in the line that represent 1 xfer page packet.
1246 * Return the xfer page packet itself to the freelist
1248 if (packet
->xfer_page_pkt
->count
== 0) {
1249 fsend_receive_comp
= true;
1250 transaction_id
= packet
->completion
.recv
.recv_completion_tid
;
1251 list_add_tail(&packet
->xfer_page_pkt
->list_ent
,
1252 &net_device
->recv_pkt_list
);
1256 /* Put the packet back */
1257 list_add_tail(&packet
->list_ent
, &net_device
->recv_pkt_list
);
1258 spin_unlock_irqrestore(&net_device
->recv_pkt_list_lock
, flags
);
1260 /* Send a receive completion for the xfer page packet */
1261 if (fsend_receive_comp
)
1262 netvsc_send_recv_completion(device
, transaction_id
);
1264 put_net_device(device
);
1267 static void netvsc_channel_cb(void *context
)
1270 struct hv_device
*device
= context
;
1271 struct netvsc_device
*net_device
;
1274 unsigned char *packet
;
1275 struct vmpacket_descriptor
*desc
;
1276 unsigned char *buffer
;
1277 int bufferlen
= NETVSC_PACKET_SIZE
;
1279 /* ASSERT(device); */
1281 packet
= kzalloc(NETVSC_PACKET_SIZE
* sizeof(unsigned char),
1287 net_device
= get_inbound_net_device(device
);
1289 DPRINT_ERR(NETVSC
, "net device (%p) shutting down..."
1290 "ignoring inbound packets", net_device
);
1295 ret
= vmbus_recvpacket_raw(device
->channel
, buffer
, bufferlen
,
1296 &bytes_recvd
, &request_id
);
1298 if (bytes_recvd
> 0) {
1299 DPRINT_DBG(NETVSC
, "receive %d bytes, tid %llx",
1300 bytes_recvd
, request_id
);
1302 desc
= (struct vmpacket_descriptor
*)buffer
;
1303 switch (desc
->type
) {
1305 netvsc_send_completion(device
, desc
);
1308 case VM_PKT_DATA_USING_XFER_PAGES
:
1309 netvsc_receive(device
, desc
);
1314 "unhandled packet type %d, "
1315 "tid %llx len %d\n",
1316 desc
->type
, request_id
,
1322 if (bufferlen
> NETVSC_PACKET_SIZE
) {
1325 bufferlen
= NETVSC_PACKET_SIZE
;
1329 if (bufferlen
> NETVSC_PACKET_SIZE
) {
1332 bufferlen
= NETVSC_PACKET_SIZE
;
1337 } else if (ret
== -2) {
1338 /* Handle large packet */
1339 buffer
= kmalloc(bytes_recvd
, GFP_ATOMIC
);
1340 if (buffer
== NULL
) {
1341 /* Try again next time around */
1343 "unable to allocate buffer of size "
1344 "(%d)!!", bytes_recvd
);
1348 bufferlen
= bytes_recvd
;
1352 put_net_device(device
);