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>
32 #include "hyperv_net.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
47 static struct netvsc_device
*alloc_net_device(struct hv_device
*device
)
49 struct netvsc_device
*net_device
;
51 net_device
= kzalloc(sizeof(struct netvsc_device
), GFP_KERNEL
);
55 /* Set to 2 to allow both inbound and outbound traffic */
56 atomic_cmpxchg(&net_device
->refcnt
, 0, 2);
58 net_device
->dev
= device
;
59 device
->ext
= net_device
;
64 static void free_net_device(struct netvsc_device
*device
)
66 WARN_ON(atomic_read(&device
->refcnt
) != 0);
67 device
->dev
->ext
= NULL
;
72 /* Get the net device object iff exists and its refcount > 1 */
73 static struct netvsc_device
*get_outbound_net_device(struct hv_device
*device
)
75 struct netvsc_device
*net_device
;
77 net_device
= device
->ext
;
78 if (net_device
&& atomic_read(&net_device
->refcnt
) > 1)
79 atomic_inc(&net_device
->refcnt
);
86 /* Get the net device object iff exists and its refcount > 0 */
87 static struct netvsc_device
*get_inbound_net_device(struct hv_device
*device
)
89 struct netvsc_device
*net_device
;
91 net_device
= device
->ext
;
92 if (net_device
&& atomic_read(&net_device
->refcnt
))
93 atomic_inc(&net_device
->refcnt
);
100 static void put_net_device(struct hv_device
*device
)
102 struct netvsc_device
*net_device
;
104 net_device
= device
->ext
;
106 atomic_dec(&net_device
->refcnt
);
109 static struct netvsc_device
*release_outbound_net_device(
110 struct hv_device
*device
)
112 struct netvsc_device
*net_device
;
114 net_device
= device
->ext
;
115 if (net_device
== NULL
)
118 /* Busy wait until the ref drop to 2, then set it to 1 */
119 while (atomic_cmpxchg(&net_device
->refcnt
, 2, 1) != 2)
125 static struct netvsc_device
*release_inbound_net_device(
126 struct hv_device
*device
)
128 struct netvsc_device
*net_device
;
130 net_device
= device
->ext
;
131 if (net_device
== NULL
)
134 /* Busy wait until the ref drop to 1, then set it to 0 */
135 while (atomic_cmpxchg(&net_device
->refcnt
, 1, 0) != 1)
142 static int netvsc_destroy_recv_buf(struct netvsc_device
*net_device
)
144 struct nvsp_message
*revoke_packet
;
148 * If we got a section count, it means we received a
149 * SendReceiveBufferComplete msg (ie sent
150 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
151 * to send a revoke msg here
153 if (net_device
->recv_section_cnt
) {
154 /* Send the revoke receive buffer */
155 revoke_packet
= &net_device
->revoke_packet
;
156 memset(revoke_packet
, 0, sizeof(struct nvsp_message
));
158 revoke_packet
->hdr
.msg_type
=
159 NVSP_MSG1_TYPE_REVOKE_RECV_BUF
;
160 revoke_packet
->msg
.v1_msg
.
161 revoke_recv_buf
.id
= NETVSC_RECEIVE_BUFFER_ID
;
163 ret
= vmbus_sendpacket(net_device
->dev
->channel
,
165 sizeof(struct nvsp_message
),
166 (unsigned long)revoke_packet
,
167 VM_PKT_DATA_INBAND
, 0);
169 * If we failed here, we might as well return and
170 * have a leak rather than continue and a bugchk
173 dev_err(&net_device
->dev
->device
, "unable to send "
174 "revoke receive buffer to netvsp");
179 /* Teardown the gpadl on the vsp end */
180 if (net_device
->recv_buf_gpadl_handle
) {
181 ret
= vmbus_teardown_gpadl(net_device
->dev
->channel
,
182 net_device
->recv_buf_gpadl_handle
);
184 /* If we failed here, we might as well return and have a leak
185 * rather than continue and a bugchk
188 dev_err(&net_device
->dev
->device
,
189 "unable to teardown receive buffer's gpadl");
192 net_device
->recv_buf_gpadl_handle
= 0;
195 if (net_device
->recv_buf
) {
196 /* Free up the receive buffer */
197 free_pages((unsigned long)net_device
->recv_buf
,
198 get_order(net_device
->recv_buf_size
));
199 net_device
->recv_buf
= NULL
;
202 if (net_device
->recv_section
) {
203 net_device
->recv_section_cnt
= 0;
204 kfree(net_device
->recv_section
);
205 net_device
->recv_section
= NULL
;
211 static int netvsc_init_recv_buf(struct hv_device
*device
)
215 struct netvsc_device
*net_device
;
216 struct nvsp_message
*init_packet
;
218 net_device
= get_outbound_net_device(device
);
220 dev_err(&device
->device
, "unable to get net device..."
221 "device being destroyed?");
225 net_device
->recv_buf
=
226 (void *)__get_free_pages(GFP_KERNEL
|__GFP_ZERO
,
227 get_order(net_device
->recv_buf_size
));
228 if (!net_device
->recv_buf
) {
229 dev_err(&device
->device
, "unable to allocate receive "
230 "buffer of size %d", net_device
->recv_buf_size
);
236 * Establish the gpadl handle for this buffer on this
237 * channel. Note: This call uses the vmbus connection rather
238 * than the channel to establish the gpadl handle.
240 ret
= vmbus_establish_gpadl(device
->channel
, net_device
->recv_buf
,
241 net_device
->recv_buf_size
,
242 &net_device
->recv_buf_gpadl_handle
);
244 dev_err(&device
->device
,
245 "unable to establish receive buffer's gpadl");
250 /* Notify the NetVsp of the gpadl handle */
251 init_packet
= &net_device
->channel_init_pkt
;
253 memset(init_packet
, 0, sizeof(struct nvsp_message
));
255 init_packet
->hdr
.msg_type
= NVSP_MSG1_TYPE_SEND_RECV_BUF
;
256 init_packet
->msg
.v1_msg
.send_recv_buf
.
257 gpadl_handle
= net_device
->recv_buf_gpadl_handle
;
258 init_packet
->msg
.v1_msg
.
259 send_recv_buf
.id
= NETVSC_RECEIVE_BUFFER_ID
;
261 /* Send the gpadl notification request */
262 ret
= vmbus_sendpacket(device
->channel
, init_packet
,
263 sizeof(struct nvsp_message
),
264 (unsigned long)init_packet
,
266 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
268 dev_err(&device
->device
,
269 "unable to send receive buffer's gpadl to netvsp");
273 t
= wait_for_completion_timeout(&net_device
->channel_init_wait
, 5*HZ
);
277 /* Check the response */
278 if (init_packet
->msg
.v1_msg
.
279 send_recv_buf_complete
.status
!= NVSP_STAT_SUCCESS
) {
280 dev_err(&device
->device
, "Unable to complete receive buffer "
281 "initialzation with NetVsp - status %d",
282 init_packet
->msg
.v1_msg
.
283 send_recv_buf_complete
.status
);
288 /* Parse the response */
290 net_device
->recv_section_cnt
= init_packet
->msg
.
291 v1_msg
.send_recv_buf_complete
.num_sections
;
293 net_device
->recv_section
= kmalloc(net_device
->recv_section_cnt
294 * sizeof(struct nvsp_1_receive_buffer_section
), GFP_KERNEL
);
295 if (net_device
->recv_section
== NULL
) {
300 memcpy(net_device
->recv_section
,
301 init_packet
->msg
.v1_msg
.
302 send_recv_buf_complete
.sections
,
303 net_device
->recv_section_cnt
*
304 sizeof(struct nvsp_1_receive_buffer_section
));
307 * For 1st release, there should only be 1 section that represents the
308 * entire receive buffer
310 if (net_device
->recv_section_cnt
!= 1 ||
311 net_device
->recv_section
->offset
!= 0) {
319 netvsc_destroy_recv_buf(net_device
);
322 put_net_device(device
);
327 static int netvsc_connect_vsp(struct hv_device
*device
)
330 struct netvsc_device
*net_device
;
331 struct nvsp_message
*init_packet
;
334 net_device
= get_outbound_net_device(device
);
336 dev_err(&device
->device
, "unable to get net device..."
337 "device being destroyed?");
341 init_packet
= &net_device
->channel_init_pkt
;
343 memset(init_packet
, 0, sizeof(struct nvsp_message
));
344 init_packet
->hdr
.msg_type
= NVSP_MSG_TYPE_INIT
;
345 init_packet
->msg
.init_msg
.init
.min_protocol_ver
=
346 NVSP_MIN_PROTOCOL_VERSION
;
347 init_packet
->msg
.init_msg
.init
.max_protocol_ver
=
348 NVSP_MAX_PROTOCOL_VERSION
;
350 /* Send the init request */
351 ret
= vmbus_sendpacket(device
->channel
, init_packet
,
352 sizeof(struct nvsp_message
),
353 (unsigned long)init_packet
,
355 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
360 t
= wait_for_completion_timeout(&net_device
->channel_init_wait
, 5*HZ
);
367 if (init_packet
->msg
.init_msg
.init_complete
.status
!=
373 if (init_packet
->msg
.init_msg
.init_complete
.
374 negotiated_protocol_ver
!= NVSP_PROTOCOL_VERSION_1
) {
378 /* Send the ndis version */
379 memset(init_packet
, 0, sizeof(struct nvsp_message
));
381 ndis_version
= 0x00050000;
383 init_packet
->hdr
.msg_type
= NVSP_MSG1_TYPE_SEND_NDIS_VER
;
384 init_packet
->msg
.v1_msg
.
385 send_ndis_ver
.ndis_major_ver
=
386 (ndis_version
& 0xFFFF0000) >> 16;
387 init_packet
->msg
.v1_msg
.
388 send_ndis_ver
.ndis_minor_ver
=
389 ndis_version
& 0xFFFF;
391 /* Send the init request */
392 ret
= vmbus_sendpacket(device
->channel
, init_packet
,
393 sizeof(struct nvsp_message
),
394 (unsigned long)init_packet
,
395 VM_PKT_DATA_INBAND
, 0);
401 /* Post the big receive buffer to NetVSP */
402 ret
= netvsc_init_recv_buf(device
);
405 put_net_device(device
);
409 static void netvsc_disconnect_vsp(struct netvsc_device
*net_device
)
411 netvsc_destroy_recv_buf(net_device
);
415 * netvsc_device_remove - Callback when the root bus device is removed
417 int netvsc_device_remove(struct hv_device
*device
)
419 struct netvsc_device
*net_device
;
420 struct hv_netvsc_packet
*netvsc_packet
, *pos
;
422 /* Stop outbound traffic ie sends and receives completions */
423 net_device
= release_outbound_net_device(device
);
425 dev_err(&device
->device
, "No net device present!!");
429 /* Wait for all send completions */
430 while (atomic_read(&net_device
->num_outstanding_sends
)) {
431 dev_err(&device
->device
,
432 "waiting for %d requests to complete...",
433 atomic_read(&net_device
->num_outstanding_sends
));
437 netvsc_disconnect_vsp(net_device
);
439 /* Stop inbound traffic ie receives and sends completions */
440 net_device
= release_inbound_net_device(device
);
442 /* At this point, no one should be accessing netDevice except in here */
443 dev_notice(&device
->device
, "net device safe to remove");
445 /* Now, we can close the channel safely */
446 vmbus_close(device
->channel
);
448 /* Release all resources */
449 list_for_each_entry_safe(netvsc_packet
, pos
,
450 &net_device
->recv_pkt_list
, list_ent
) {
451 list_del(&netvsc_packet
->list_ent
);
452 kfree(netvsc_packet
);
455 free_net_device(net_device
);
459 static void netvsc_send_completion(struct hv_device
*device
,
460 struct vmpacket_descriptor
*packet
)
462 struct netvsc_device
*net_device
;
463 struct nvsp_message
*nvsp_packet
;
464 struct hv_netvsc_packet
*nvsc_packet
;
466 net_device
= get_inbound_net_device(device
);
468 dev_err(&device
->device
, "unable to get net device..."
469 "device being destroyed?");
473 nvsp_packet
= (struct nvsp_message
*)((unsigned long)packet
+
474 (packet
->offset8
<< 3));
476 if ((nvsp_packet
->hdr
.msg_type
== NVSP_MSG_TYPE_INIT_COMPLETE
) ||
477 (nvsp_packet
->hdr
.msg_type
==
478 NVSP_MSG1_TYPE_SEND_RECV_BUF_COMPLETE
) ||
479 (nvsp_packet
->hdr
.msg_type
==
480 NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE
)) {
481 /* Copy the response back */
482 memcpy(&net_device
->channel_init_pkt
, nvsp_packet
,
483 sizeof(struct nvsp_message
));
484 complete(&net_device
->channel_init_wait
);
485 } else if (nvsp_packet
->hdr
.msg_type
==
486 NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE
) {
487 /* Get the send context */
488 nvsc_packet
= (struct hv_netvsc_packet
*)(unsigned long)
491 /* Notify the layer above us */
492 nvsc_packet
->completion
.send
.send_completion(
493 nvsc_packet
->completion
.send
.send_completion_ctx
);
495 atomic_dec(&net_device
->num_outstanding_sends
);
497 dev_err(&device
->device
, "Unknown send completion packet type- "
498 "%d received!!", nvsp_packet
->hdr
.msg_type
);
501 put_net_device(device
);
504 int netvsc_send(struct hv_device
*device
,
505 struct hv_netvsc_packet
*packet
)
507 struct netvsc_device
*net_device
;
510 struct nvsp_message sendMessage
;
512 net_device
= get_outbound_net_device(device
);
514 dev_err(&device
->device
, "net device (%p) shutting down..."
515 "ignoring outbound packets", net_device
);
519 sendMessage
.hdr
.msg_type
= NVSP_MSG1_TYPE_SEND_RNDIS_PKT
;
520 if (packet
->is_data_pkt
) {
522 sendMessage
.msg
.v1_msg
.send_rndis_pkt
.channel_type
= 0;
524 /* 1 is RMC_CONTROL; */
525 sendMessage
.msg
.v1_msg
.send_rndis_pkt
.channel_type
= 1;
528 /* Not using send buffer section */
529 sendMessage
.msg
.v1_msg
.send_rndis_pkt
.send_buf_section_index
=
531 sendMessage
.msg
.v1_msg
.send_rndis_pkt
.send_buf_section_size
= 0;
533 if (packet
->page_buf_cnt
) {
534 ret
= vmbus_sendpacket_pagebuffer(device
->channel
,
536 packet
->page_buf_cnt
,
538 sizeof(struct nvsp_message
),
539 (unsigned long)packet
);
541 ret
= vmbus_sendpacket(device
->channel
, &sendMessage
,
542 sizeof(struct nvsp_message
),
543 (unsigned long)packet
,
545 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
550 dev_err(&device
->device
, "Unable to send packet %p ret %d",
553 atomic_inc(&net_device
->num_outstanding_sends
);
554 put_net_device(device
);
558 static void netvsc_send_recv_completion(struct hv_device
*device
,
561 struct nvsp_message recvcompMessage
;
565 recvcompMessage
.hdr
.msg_type
=
566 NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE
;
568 /* FIXME: Pass in the status */
569 recvcompMessage
.msg
.v1_msg
.send_rndis_pkt_complete
.status
=
573 /* Send the completion */
574 ret
= vmbus_sendpacket(device
->channel
, &recvcompMessage
,
575 sizeof(struct nvsp_message
), transaction_id
,
580 } else if (ret
== -1) {
581 /* no more room...wait a bit and attempt to retry 3 times */
583 dev_err(&device
->device
, "unable to send receive completion pkt"
584 " (tid %llx)...retrying %d", transaction_id
, retries
);
588 goto retry_send_cmplt
;
590 dev_err(&device
->device
, "unable to send receive "
591 "completion pkt (tid %llx)...give up retrying",
595 dev_err(&device
->device
, "unable to send receive "
596 "completion pkt - %llx", transaction_id
);
600 /* Send a receive completion packet to RNDIS device (ie NetVsp) */
601 static void netvsc_receive_completion(void *context
)
603 struct hv_netvsc_packet
*packet
= context
;
604 struct hv_device
*device
= (struct hv_device
*)packet
->device
;
605 struct netvsc_device
*net_device
;
606 u64 transaction_id
= 0;
607 bool fsend_receive_comp
= false;
611 * Even though it seems logical to do a GetOutboundNetDevice() here to
612 * send out receive completion, we are using GetInboundNetDevice()
613 * since we may have disable outbound traffic already.
615 net_device
= get_inbound_net_device(device
);
617 dev_err(&device
->device
, "unable to get net device..."
618 "device being destroyed?");
622 /* Overloading use of the lock. */
623 spin_lock_irqsave(&net_device
->recv_pkt_list_lock
, flags
);
625 packet
->xfer_page_pkt
->count
--;
628 * Last one in the line that represent 1 xfer page packet.
629 * Return the xfer page packet itself to the freelist
631 if (packet
->xfer_page_pkt
->count
== 0) {
632 fsend_receive_comp
= true;
633 transaction_id
= packet
->completion
.recv
.recv_completion_tid
;
634 list_add_tail(&packet
->xfer_page_pkt
->list_ent
,
635 &net_device
->recv_pkt_list
);
639 /* Put the packet back */
640 list_add_tail(&packet
->list_ent
, &net_device
->recv_pkt_list
);
641 spin_unlock_irqrestore(&net_device
->recv_pkt_list_lock
, flags
);
643 /* Send a receive completion for the xfer page packet */
644 if (fsend_receive_comp
)
645 netvsc_send_recv_completion(device
, transaction_id
);
647 put_net_device(device
);
650 static void netvsc_receive(struct hv_device
*device
,
651 struct vmpacket_descriptor
*packet
)
653 struct netvsc_device
*net_device
;
654 struct vmtransfer_page_packet_header
*vmxferpage_packet
;
655 struct nvsp_message
*nvsp_packet
;
656 struct hv_netvsc_packet
*netvsc_packet
= NULL
;
658 unsigned long end
, end_virtual
;
659 /* struct netvsc_driver *netvscDriver; */
660 struct xferpage_packet
*xferpage_packet
= NULL
;
662 int count
= 0, bytes_remain
= 0;
667 net_device
= get_inbound_net_device(device
);
669 dev_err(&device
->device
, "unable to get net device..."
670 "device being destroyed?");
675 * All inbound packets other than send completion should be xfer page
678 if (packet
->type
!= VM_PKT_DATA_USING_XFER_PAGES
) {
679 dev_err(&device
->device
, "Unknown packet type received - %d",
681 put_net_device(device
);
685 nvsp_packet
= (struct nvsp_message
*)((unsigned long)packet
+
686 (packet
->offset8
<< 3));
688 /* Make sure this is a valid nvsp packet */
689 if (nvsp_packet
->hdr
.msg_type
!=
690 NVSP_MSG1_TYPE_SEND_RNDIS_PKT
) {
691 dev_err(&device
->device
, "Unknown nvsp packet type received-"
692 " %d", nvsp_packet
->hdr
.msg_type
);
693 put_net_device(device
);
697 vmxferpage_packet
= (struct vmtransfer_page_packet_header
*)packet
;
699 if (vmxferpage_packet
->xfer_pageset_id
!= NETVSC_RECEIVE_BUFFER_ID
) {
700 dev_err(&device
->device
, "Invalid xfer page set id - "
701 "expecting %x got %x", NETVSC_RECEIVE_BUFFER_ID
,
702 vmxferpage_packet
->xfer_pageset_id
);
703 put_net_device(device
);
708 * Grab free packets (range count + 1) to represent this xfer
709 * page packet. +1 to represent the xfer page packet itself.
710 * We grab it here so that we know exactly how many we can
713 spin_lock_irqsave(&net_device
->recv_pkt_list_lock
, flags
);
714 while (!list_empty(&net_device
->recv_pkt_list
)) {
715 list_move_tail(net_device
->recv_pkt_list
.next
, &listHead
);
716 if (++count
== vmxferpage_packet
->range_cnt
+ 1)
719 spin_unlock_irqrestore(&net_device
->recv_pkt_list_lock
, flags
);
722 * We need at least 2 netvsc pkts (1 to represent the xfer
723 * page and at least 1 for the range) i.e. we can handled
724 * some of the xfer page packet ranges...
727 dev_err(&device
->device
, "Got only %d netvsc pkt...needed "
728 "%d pkts. Dropping this xfer page packet completely!",
729 count
, vmxferpage_packet
->range_cnt
+ 1);
731 /* Return it to the freelist */
732 spin_lock_irqsave(&net_device
->recv_pkt_list_lock
, flags
);
733 for (i
= count
; i
!= 0; i
--) {
734 list_move_tail(listHead
.next
,
735 &net_device
->recv_pkt_list
);
737 spin_unlock_irqrestore(&net_device
->recv_pkt_list_lock
,
740 netvsc_send_recv_completion(device
,
741 vmxferpage_packet
->d
.trans_id
);
743 put_net_device(device
);
747 /* Remove the 1st packet to represent the xfer page packet itself */
748 xferpage_packet
= (struct xferpage_packet
*)listHead
.next
;
749 list_del(&xferpage_packet
->list_ent
);
751 /* This is how much we can satisfy */
752 xferpage_packet
->count
= count
- 1;
754 if (xferpage_packet
->count
!= vmxferpage_packet
->range_cnt
) {
755 dev_err(&device
->device
, "Needed %d netvsc pkts to satisy "
756 "this xfer page...got %d",
757 vmxferpage_packet
->range_cnt
, xferpage_packet
->count
);
760 /* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */
761 for (i
= 0; i
< (count
- 1); i
++) {
762 netvsc_packet
= (struct hv_netvsc_packet
*)listHead
.next
;
763 list_del(&netvsc_packet
->list_ent
);
765 /* Initialize the netvsc packet */
766 netvsc_packet
->xfer_page_pkt
= xferpage_packet
;
767 netvsc_packet
->completion
.recv
.recv_completion
=
768 netvsc_receive_completion
;
769 netvsc_packet
->completion
.recv
.recv_completion_ctx
=
771 netvsc_packet
->device
= device
;
772 /* Save this so that we can send it back */
773 netvsc_packet
->completion
.recv
.recv_completion_tid
=
774 vmxferpage_packet
->d
.trans_id
;
776 netvsc_packet
->total_data_buflen
=
777 vmxferpage_packet
->ranges
[i
].byte_count
;
778 netvsc_packet
->page_buf_cnt
= 1;
780 netvsc_packet
->page_buf
[0].len
=
781 vmxferpage_packet
->ranges
[i
].byte_count
;
783 start
= virt_to_phys((void *)((unsigned long)net_device
->
784 recv_buf
+ vmxferpage_packet
->ranges
[i
].byte_offset
));
786 netvsc_packet
->page_buf
[0].pfn
= start
>> PAGE_SHIFT
;
787 end_virtual
= (unsigned long)net_device
->recv_buf
788 + vmxferpage_packet
->ranges
[i
].byte_offset
789 + vmxferpage_packet
->ranges
[i
].byte_count
- 1;
790 end
= virt_to_phys((void *)end_virtual
);
792 /* Calculate the page relative offset */
793 netvsc_packet
->page_buf
[0].offset
=
794 vmxferpage_packet
->ranges
[i
].byte_offset
&
796 if ((end
>> PAGE_SHIFT
) != (start
>> PAGE_SHIFT
)) {
797 /* Handle frame across multiple pages: */
798 netvsc_packet
->page_buf
[0].len
=
799 (netvsc_packet
->page_buf
[0].pfn
<<
802 bytes_remain
= netvsc_packet
->total_data_buflen
-
803 netvsc_packet
->page_buf
[0].len
;
804 for (j
= 1; j
< NETVSC_PACKET_MAXPAGE
; j
++) {
805 netvsc_packet
->page_buf
[j
].offset
= 0;
806 if (bytes_remain
<= PAGE_SIZE
) {
807 netvsc_packet
->page_buf
[j
].len
=
811 netvsc_packet
->page_buf
[j
].len
=
813 bytes_remain
-= PAGE_SIZE
;
815 netvsc_packet
->page_buf
[j
].pfn
=
816 virt_to_phys((void *)(end_virtual
-
817 bytes_remain
)) >> PAGE_SHIFT
;
818 netvsc_packet
->page_buf_cnt
++;
819 if (bytes_remain
== 0)
824 /* Pass it to the upper layer */
825 rndis_filter_receive(device
, netvsc_packet
);
827 netvsc_receive_completion(netvsc_packet
->
828 completion
.recv
.recv_completion_ctx
);
831 put_net_device(device
);
834 static void netvsc_channel_cb(void *context
)
837 struct hv_device
*device
= context
;
838 struct netvsc_device
*net_device
;
841 unsigned char *packet
;
842 struct vmpacket_descriptor
*desc
;
843 unsigned char *buffer
;
844 int bufferlen
= NETVSC_PACKET_SIZE
;
846 packet
= kzalloc(NETVSC_PACKET_SIZE
* sizeof(unsigned char),
852 net_device
= get_inbound_net_device(device
);
854 dev_err(&device
->device
, "net device (%p) shutting down..."
855 "ignoring inbound packets", net_device
);
860 ret
= vmbus_recvpacket_raw(device
->channel
, buffer
, bufferlen
,
861 &bytes_recvd
, &request_id
);
863 if (bytes_recvd
> 0) {
864 desc
= (struct vmpacket_descriptor
*)buffer
;
865 switch (desc
->type
) {
867 netvsc_send_completion(device
, desc
);
870 case VM_PKT_DATA_USING_XFER_PAGES
:
871 netvsc_receive(device
, desc
);
875 dev_err(&device
->device
,
876 "unhandled packet type %d, "
878 desc
->type
, request_id
,
884 if (bufferlen
> NETVSC_PACKET_SIZE
) {
887 bufferlen
= NETVSC_PACKET_SIZE
;
891 if (bufferlen
> NETVSC_PACKET_SIZE
) {
894 bufferlen
= NETVSC_PACKET_SIZE
;
899 } else if (ret
== -2) {
900 /* Handle large packet */
901 buffer
= kmalloc(bytes_recvd
, GFP_ATOMIC
);
902 if (buffer
== NULL
) {
903 /* Try again next time around */
904 dev_err(&device
->device
,
905 "unable to allocate buffer of size "
906 "(%d)!!", bytes_recvd
);
910 bufferlen
= bytes_recvd
;
914 put_net_device(device
);
921 * netvsc_device_add - Callback when the device belonging to this
924 int netvsc_device_add(struct hv_device
*device
, void *additional_info
)
929 ((struct netvsc_device_info
*)additional_info
)->ring_size
;
930 struct netvsc_device
*net_device
;
931 struct hv_netvsc_packet
*packet
, *pos
;
933 net_device
= alloc_net_device(device
);
939 /* Initialize the NetVSC channel extension */
940 net_device
->recv_buf_size
= NETVSC_RECEIVE_BUFFER_SIZE
;
941 spin_lock_init(&net_device
->recv_pkt_list_lock
);
943 INIT_LIST_HEAD(&net_device
->recv_pkt_list
);
945 for (i
= 0; i
< NETVSC_RECEIVE_PACKETLIST_COUNT
; i
++) {
946 packet
= kzalloc(sizeof(struct hv_netvsc_packet
) +
947 (NETVSC_RECEIVE_SG_COUNT
*
948 sizeof(struct hv_page_buffer
)), GFP_KERNEL
);
952 list_add_tail(&packet
->list_ent
,
953 &net_device
->recv_pkt_list
);
955 init_completion(&net_device
->channel_init_wait
);
957 /* Open the channel */
958 ret
= vmbus_open(device
->channel
, ring_size
* PAGE_SIZE
,
959 ring_size
* PAGE_SIZE
, NULL
, 0,
960 netvsc_channel_cb
, device
);
963 dev_err(&device
->device
, "unable to open channel: %d", ret
);
968 /* Channel is opened */
969 pr_info("hv_netvsc channel opened successfully");
971 /* Connect with the NetVsp */
972 ret
= netvsc_connect_vsp(device
);
974 dev_err(&device
->device
,
975 "unable to connect to NetVSP - %d", ret
);
983 /* Now, we can close the channel safely */
984 vmbus_close(device
->channel
);
989 list_for_each_entry_safe(packet
, pos
,
990 &net_device
->recv_pkt_list
,
992 list_del(&packet
->list_ent
);
996 release_outbound_net_device(device
);
997 release_inbound_net_device(device
);
999 free_net_device(net_device
);
1006 * netvsc_initialize - Main entry point
1008 int netvsc_initialize(struct hv_driver
*drv
)
1011 drv
->name
= driver_name
;
1012 memcpy(&drv
->dev_type
, &netvsc_device_type
, sizeof(struct hv_guid
));