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"
35 static struct netvsc_device
*alloc_net_device(struct hv_device
*device
)
37 struct netvsc_device
*net_device
;
39 net_device
= kzalloc(sizeof(struct netvsc_device
), GFP_KERNEL
);
43 /* Set to 2 to allow both inbound and outbound traffic */
44 atomic_set(&net_device
->refcnt
, 2);
46 net_device
->dev
= device
;
47 device
->ext
= net_device
;
52 /* Get the net device object iff exists and its refcount > 1 */
53 static struct netvsc_device
*get_outbound_net_device(struct hv_device
*device
)
55 struct netvsc_device
*net_device
;
57 net_device
= device
->ext
;
58 if (net_device
&& atomic_read(&net_device
->refcnt
) > 1)
59 atomic_inc(&net_device
->refcnt
);
66 /* Get the net device object iff exists and its refcount > 0 */
67 static struct netvsc_device
*get_inbound_net_device(struct hv_device
*device
)
69 struct netvsc_device
*net_device
;
71 net_device
= device
->ext
;
72 if (net_device
&& atomic_read(&net_device
->refcnt
))
73 atomic_inc(&net_device
->refcnt
);
80 static void put_net_device(struct hv_device
*device
)
82 struct netvsc_device
*net_device
;
84 net_device
= device
->ext
;
86 atomic_dec(&net_device
->refcnt
);
89 static struct netvsc_device
*release_outbound_net_device(
90 struct hv_device
*device
)
92 struct netvsc_device
*net_device
;
94 net_device
= device
->ext
;
95 if (net_device
== NULL
)
98 /* Busy wait until the ref drop to 2, then set it to 1 */
99 while (atomic_cmpxchg(&net_device
->refcnt
, 2, 1) != 2)
105 static struct netvsc_device
*release_inbound_net_device(
106 struct hv_device
*device
)
108 struct netvsc_device
*net_device
;
110 net_device
= device
->ext
;
111 if (net_device
== NULL
)
114 /* Busy wait until the ref drop to 1, then set it to 0 */
115 while (atomic_cmpxchg(&net_device
->refcnt
, 1, 0) != 1)
122 static int netvsc_destroy_recv_buf(struct netvsc_device
*net_device
)
124 struct nvsp_message
*revoke_packet
;
128 * If we got a section count, it means we received a
129 * SendReceiveBufferComplete msg (ie sent
130 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
131 * to send a revoke msg here
133 if (net_device
->recv_section_cnt
) {
134 /* Send the revoke receive buffer */
135 revoke_packet
= &net_device
->revoke_packet
;
136 memset(revoke_packet
, 0, sizeof(struct nvsp_message
));
138 revoke_packet
->hdr
.msg_type
=
139 NVSP_MSG1_TYPE_REVOKE_RECV_BUF
;
140 revoke_packet
->msg
.v1_msg
.
141 revoke_recv_buf
.id
= NETVSC_RECEIVE_BUFFER_ID
;
143 ret
= vmbus_sendpacket(net_device
->dev
->channel
,
145 sizeof(struct nvsp_message
),
146 (unsigned long)revoke_packet
,
147 VM_PKT_DATA_INBAND
, 0);
149 * If we failed here, we might as well return and
150 * have a leak rather than continue and a bugchk
153 dev_err(&net_device
->dev
->device
, "unable to send "
154 "revoke receive buffer to netvsp");
159 /* Teardown the gpadl on the vsp end */
160 if (net_device
->recv_buf_gpadl_handle
) {
161 ret
= vmbus_teardown_gpadl(net_device
->dev
->channel
,
162 net_device
->recv_buf_gpadl_handle
);
164 /* If we failed here, we might as well return and have a leak
165 * rather than continue and a bugchk
168 dev_err(&net_device
->dev
->device
,
169 "unable to teardown receive buffer's gpadl");
172 net_device
->recv_buf_gpadl_handle
= 0;
175 if (net_device
->recv_buf
) {
176 /* Free up the receive buffer */
177 free_pages((unsigned long)net_device
->recv_buf
,
178 get_order(net_device
->recv_buf_size
));
179 net_device
->recv_buf
= NULL
;
182 if (net_device
->recv_section
) {
183 net_device
->recv_section_cnt
= 0;
184 kfree(net_device
->recv_section
);
185 net_device
->recv_section
= NULL
;
191 static int netvsc_init_recv_buf(struct hv_device
*device
)
195 struct netvsc_device
*net_device
;
196 struct nvsp_message
*init_packet
;
198 net_device
= get_outbound_net_device(device
);
200 dev_err(&device
->device
, "unable to get net device..."
201 "device being destroyed?");
205 net_device
->recv_buf
=
206 (void *)__get_free_pages(GFP_KERNEL
|__GFP_ZERO
,
207 get_order(net_device
->recv_buf_size
));
208 if (!net_device
->recv_buf
) {
209 dev_err(&device
->device
, "unable to allocate receive "
210 "buffer of size %d", net_device
->recv_buf_size
);
216 * Establish the gpadl handle for this buffer on this
217 * channel. Note: This call uses the vmbus connection rather
218 * than the channel to establish the gpadl handle.
220 ret
= vmbus_establish_gpadl(device
->channel
, net_device
->recv_buf
,
221 net_device
->recv_buf_size
,
222 &net_device
->recv_buf_gpadl_handle
);
224 dev_err(&device
->device
,
225 "unable to establish receive buffer's gpadl");
230 /* Notify the NetVsp of the gpadl handle */
231 init_packet
= &net_device
->channel_init_pkt
;
233 memset(init_packet
, 0, sizeof(struct nvsp_message
));
235 init_packet
->hdr
.msg_type
= NVSP_MSG1_TYPE_SEND_RECV_BUF
;
236 init_packet
->msg
.v1_msg
.send_recv_buf
.
237 gpadl_handle
= net_device
->recv_buf_gpadl_handle
;
238 init_packet
->msg
.v1_msg
.
239 send_recv_buf
.id
= NETVSC_RECEIVE_BUFFER_ID
;
241 /* Send the gpadl notification request */
242 ret
= vmbus_sendpacket(device
->channel
, init_packet
,
243 sizeof(struct nvsp_message
),
244 (unsigned long)init_packet
,
246 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
248 dev_err(&device
->device
,
249 "unable to send receive buffer's gpadl to netvsp");
253 t
= wait_for_completion_timeout(&net_device
->channel_init_wait
, 5*HZ
);
257 /* Check the response */
258 if (init_packet
->msg
.v1_msg
.
259 send_recv_buf_complete
.status
!= NVSP_STAT_SUCCESS
) {
260 dev_err(&device
->device
, "Unable to complete receive buffer "
261 "initialzation with NetVsp - status %d",
262 init_packet
->msg
.v1_msg
.
263 send_recv_buf_complete
.status
);
268 /* Parse the response */
270 net_device
->recv_section_cnt
= init_packet
->msg
.
271 v1_msg
.send_recv_buf_complete
.num_sections
;
273 net_device
->recv_section
= kmalloc(net_device
->recv_section_cnt
274 * sizeof(struct nvsp_1_receive_buffer_section
), GFP_KERNEL
);
275 if (net_device
->recv_section
== NULL
) {
280 memcpy(net_device
->recv_section
,
281 init_packet
->msg
.v1_msg
.
282 send_recv_buf_complete
.sections
,
283 net_device
->recv_section_cnt
*
284 sizeof(struct nvsp_1_receive_buffer_section
));
287 * For 1st release, there should only be 1 section that represents the
288 * entire receive buffer
290 if (net_device
->recv_section_cnt
!= 1 ||
291 net_device
->recv_section
->offset
!= 0) {
299 netvsc_destroy_recv_buf(net_device
);
302 put_net_device(device
);
307 static int netvsc_connect_vsp(struct hv_device
*device
)
310 struct netvsc_device
*net_device
;
311 struct nvsp_message
*init_packet
;
314 net_device
= get_outbound_net_device(device
);
316 dev_err(&device
->device
, "unable to get net device..."
317 "device being destroyed?");
321 init_packet
= &net_device
->channel_init_pkt
;
323 memset(init_packet
, 0, sizeof(struct nvsp_message
));
324 init_packet
->hdr
.msg_type
= NVSP_MSG_TYPE_INIT
;
325 init_packet
->msg
.init_msg
.init
.min_protocol_ver
=
326 NVSP_MIN_PROTOCOL_VERSION
;
327 init_packet
->msg
.init_msg
.init
.max_protocol_ver
=
328 NVSP_MAX_PROTOCOL_VERSION
;
330 /* Send the init request */
331 ret
= vmbus_sendpacket(device
->channel
, init_packet
,
332 sizeof(struct nvsp_message
),
333 (unsigned long)init_packet
,
335 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
340 t
= wait_for_completion_timeout(&net_device
->channel_init_wait
, 5*HZ
);
347 if (init_packet
->msg
.init_msg
.init_complete
.status
!=
353 if (init_packet
->msg
.init_msg
.init_complete
.
354 negotiated_protocol_ver
!= NVSP_PROTOCOL_VERSION_1
) {
358 /* Send the ndis version */
359 memset(init_packet
, 0, sizeof(struct nvsp_message
));
361 ndis_version
= 0x00050000;
363 init_packet
->hdr
.msg_type
= NVSP_MSG1_TYPE_SEND_NDIS_VER
;
364 init_packet
->msg
.v1_msg
.
365 send_ndis_ver
.ndis_major_ver
=
366 (ndis_version
& 0xFFFF0000) >> 16;
367 init_packet
->msg
.v1_msg
.
368 send_ndis_ver
.ndis_minor_ver
=
369 ndis_version
& 0xFFFF;
371 /* Send the init request */
372 ret
= vmbus_sendpacket(device
->channel
, init_packet
,
373 sizeof(struct nvsp_message
),
374 (unsigned long)init_packet
,
375 VM_PKT_DATA_INBAND
, 0);
379 /* Post the big receive buffer to NetVSP */
380 ret
= netvsc_init_recv_buf(device
);
383 put_net_device(device
);
387 static void netvsc_disconnect_vsp(struct netvsc_device
*net_device
)
389 netvsc_destroy_recv_buf(net_device
);
393 * netvsc_device_remove - Callback when the root bus device is removed
395 int netvsc_device_remove(struct hv_device
*device
)
397 struct netvsc_device
*net_device
;
398 struct hv_netvsc_packet
*netvsc_packet
, *pos
;
400 /* Stop outbound traffic ie sends and receives completions */
401 net_device
= release_outbound_net_device(device
);
403 dev_err(&device
->device
, "No net device present!!");
407 /* Wait for all send completions */
408 while (atomic_read(&net_device
->num_outstanding_sends
)) {
409 dev_err(&device
->device
,
410 "waiting for %d requests to complete...",
411 atomic_read(&net_device
->num_outstanding_sends
));
415 netvsc_disconnect_vsp(net_device
);
417 /* Stop inbound traffic ie receives and sends completions */
418 net_device
= release_inbound_net_device(device
);
420 /* At this point, no one should be accessing netDevice except in here */
421 dev_notice(&device
->device
, "net device safe to remove");
423 /* Now, we can close the channel safely */
424 vmbus_close(device
->channel
);
426 /* Release all resources */
427 list_for_each_entry_safe(netvsc_packet
, pos
,
428 &net_device
->recv_pkt_list
, list_ent
) {
429 list_del(&netvsc_packet
->list_ent
);
430 kfree(netvsc_packet
);
437 static void netvsc_send_completion(struct hv_device
*device
,
438 struct vmpacket_descriptor
*packet
)
440 struct netvsc_device
*net_device
;
441 struct nvsp_message
*nvsp_packet
;
442 struct hv_netvsc_packet
*nvsc_packet
;
444 net_device
= get_inbound_net_device(device
);
446 dev_err(&device
->device
, "unable to get net device..."
447 "device being destroyed?");
451 nvsp_packet
= (struct nvsp_message
*)((unsigned long)packet
+
452 (packet
->offset8
<< 3));
454 if ((nvsp_packet
->hdr
.msg_type
== NVSP_MSG_TYPE_INIT_COMPLETE
) ||
455 (nvsp_packet
->hdr
.msg_type
==
456 NVSP_MSG1_TYPE_SEND_RECV_BUF_COMPLETE
) ||
457 (nvsp_packet
->hdr
.msg_type
==
458 NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE
)) {
459 /* Copy the response back */
460 memcpy(&net_device
->channel_init_pkt
, nvsp_packet
,
461 sizeof(struct nvsp_message
));
462 complete(&net_device
->channel_init_wait
);
463 } else if (nvsp_packet
->hdr
.msg_type
==
464 NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE
) {
465 /* Get the send context */
466 nvsc_packet
= (struct hv_netvsc_packet
*)(unsigned long)
469 /* Notify the layer above us */
470 nvsc_packet
->completion
.send
.send_completion(
471 nvsc_packet
->completion
.send
.send_completion_ctx
);
473 atomic_dec(&net_device
->num_outstanding_sends
);
475 dev_err(&device
->device
, "Unknown send completion packet type- "
476 "%d received!!", nvsp_packet
->hdr
.msg_type
);
479 put_net_device(device
);
482 int netvsc_send(struct hv_device
*device
,
483 struct hv_netvsc_packet
*packet
)
485 struct netvsc_device
*net_device
;
488 struct nvsp_message sendMessage
;
490 net_device
= get_outbound_net_device(device
);
492 dev_err(&device
->device
, "net device (%p) shutting down..."
493 "ignoring outbound packets", net_device
);
497 sendMessage
.hdr
.msg_type
= NVSP_MSG1_TYPE_SEND_RNDIS_PKT
;
498 if (packet
->is_data_pkt
) {
500 sendMessage
.msg
.v1_msg
.send_rndis_pkt
.channel_type
= 0;
502 /* 1 is RMC_CONTROL; */
503 sendMessage
.msg
.v1_msg
.send_rndis_pkt
.channel_type
= 1;
506 /* Not using send buffer section */
507 sendMessage
.msg
.v1_msg
.send_rndis_pkt
.send_buf_section_index
=
509 sendMessage
.msg
.v1_msg
.send_rndis_pkt
.send_buf_section_size
= 0;
511 if (packet
->page_buf_cnt
) {
512 ret
= vmbus_sendpacket_pagebuffer(device
->channel
,
514 packet
->page_buf_cnt
,
516 sizeof(struct nvsp_message
),
517 (unsigned long)packet
);
519 ret
= vmbus_sendpacket(device
->channel
, &sendMessage
,
520 sizeof(struct nvsp_message
),
521 (unsigned long)packet
,
523 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
528 dev_err(&device
->device
, "Unable to send packet %p ret %d",
531 atomic_inc(&net_device
->num_outstanding_sends
);
532 put_net_device(device
);
536 static void netvsc_send_recv_completion(struct hv_device
*device
,
539 struct nvsp_message recvcompMessage
;
543 recvcompMessage
.hdr
.msg_type
=
544 NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE
;
546 /* FIXME: Pass in the status */
547 recvcompMessage
.msg
.v1_msg
.send_rndis_pkt_complete
.status
=
551 /* Send the completion */
552 ret
= vmbus_sendpacket(device
->channel
, &recvcompMessage
,
553 sizeof(struct nvsp_message
), transaction_id
,
558 } else if (ret
== -EAGAIN
) {
559 /* no more room...wait a bit and attempt to retry 3 times */
561 dev_err(&device
->device
, "unable to send receive completion pkt"
562 " (tid %llx)...retrying %d", transaction_id
, retries
);
566 goto retry_send_cmplt
;
568 dev_err(&device
->device
, "unable to send receive "
569 "completion pkt (tid %llx)...give up retrying",
573 dev_err(&device
->device
, "unable to send receive "
574 "completion pkt - %llx", transaction_id
);
578 /* Send a receive completion packet to RNDIS device (ie NetVsp) */
579 static void netvsc_receive_completion(void *context
)
581 struct hv_netvsc_packet
*packet
= context
;
582 struct hv_device
*device
= (struct hv_device
*)packet
->device
;
583 struct netvsc_device
*net_device
;
584 u64 transaction_id
= 0;
585 bool fsend_receive_comp
= false;
589 * Even though it seems logical to do a GetOutboundNetDevice() here to
590 * send out receive completion, we are using GetInboundNetDevice()
591 * since we may have disable outbound traffic already.
593 net_device
= get_inbound_net_device(device
);
595 dev_err(&device
->device
, "unable to get net device..."
596 "device being destroyed?");
600 /* Overloading use of the lock. */
601 spin_lock_irqsave(&net_device
->recv_pkt_list_lock
, flags
);
603 packet
->xfer_page_pkt
->count
--;
606 * Last one in the line that represent 1 xfer page packet.
607 * Return the xfer page packet itself to the freelist
609 if (packet
->xfer_page_pkt
->count
== 0) {
610 fsend_receive_comp
= true;
611 transaction_id
= packet
->completion
.recv
.recv_completion_tid
;
612 list_add_tail(&packet
->xfer_page_pkt
->list_ent
,
613 &net_device
->recv_pkt_list
);
617 /* Put the packet back */
618 list_add_tail(&packet
->list_ent
, &net_device
->recv_pkt_list
);
619 spin_unlock_irqrestore(&net_device
->recv_pkt_list_lock
, flags
);
621 /* Send a receive completion for the xfer page packet */
622 if (fsend_receive_comp
)
623 netvsc_send_recv_completion(device
, transaction_id
);
625 put_net_device(device
);
628 static void netvsc_receive(struct hv_device
*device
,
629 struct vmpacket_descriptor
*packet
)
631 struct netvsc_device
*net_device
;
632 struct vmtransfer_page_packet_header
*vmxferpage_packet
;
633 struct nvsp_message
*nvsp_packet
;
634 struct hv_netvsc_packet
*netvsc_packet
= NULL
;
636 unsigned long end
, end_virtual
;
637 /* struct netvsc_driver *netvscDriver; */
638 struct xferpage_packet
*xferpage_packet
= NULL
;
640 int count
= 0, bytes_remain
= 0;
645 net_device
= get_inbound_net_device(device
);
647 dev_err(&device
->device
, "unable to get net device..."
648 "device being destroyed?");
653 * All inbound packets other than send completion should be xfer page
656 if (packet
->type
!= VM_PKT_DATA_USING_XFER_PAGES
) {
657 dev_err(&device
->device
, "Unknown packet type received - %d",
659 put_net_device(device
);
663 nvsp_packet
= (struct nvsp_message
*)((unsigned long)packet
+
664 (packet
->offset8
<< 3));
666 /* Make sure this is a valid nvsp packet */
667 if (nvsp_packet
->hdr
.msg_type
!=
668 NVSP_MSG1_TYPE_SEND_RNDIS_PKT
) {
669 dev_err(&device
->device
, "Unknown nvsp packet type received-"
670 " %d", nvsp_packet
->hdr
.msg_type
);
671 put_net_device(device
);
675 vmxferpage_packet
= (struct vmtransfer_page_packet_header
*)packet
;
677 if (vmxferpage_packet
->xfer_pageset_id
!= NETVSC_RECEIVE_BUFFER_ID
) {
678 dev_err(&device
->device
, "Invalid xfer page set id - "
679 "expecting %x got %x", NETVSC_RECEIVE_BUFFER_ID
,
680 vmxferpage_packet
->xfer_pageset_id
);
681 put_net_device(device
);
686 * Grab free packets (range count + 1) to represent this xfer
687 * page packet. +1 to represent the xfer page packet itself.
688 * We grab it here so that we know exactly how many we can
691 spin_lock_irqsave(&net_device
->recv_pkt_list_lock
, flags
);
692 while (!list_empty(&net_device
->recv_pkt_list
)) {
693 list_move_tail(net_device
->recv_pkt_list
.next
, &listHead
);
694 if (++count
== vmxferpage_packet
->range_cnt
+ 1)
697 spin_unlock_irqrestore(&net_device
->recv_pkt_list_lock
, flags
);
700 * We need at least 2 netvsc pkts (1 to represent the xfer
701 * page and at least 1 for the range) i.e. we can handled
702 * some of the xfer page packet ranges...
705 dev_err(&device
->device
, "Got only %d netvsc pkt...needed "
706 "%d pkts. Dropping this xfer page packet completely!",
707 count
, vmxferpage_packet
->range_cnt
+ 1);
709 /* Return it to the freelist */
710 spin_lock_irqsave(&net_device
->recv_pkt_list_lock
, flags
);
711 for (i
= count
; i
!= 0; i
--) {
712 list_move_tail(listHead
.next
,
713 &net_device
->recv_pkt_list
);
715 spin_unlock_irqrestore(&net_device
->recv_pkt_list_lock
,
718 netvsc_send_recv_completion(device
,
719 vmxferpage_packet
->d
.trans_id
);
721 put_net_device(device
);
725 /* Remove the 1st packet to represent the xfer page packet itself */
726 xferpage_packet
= (struct xferpage_packet
*)listHead
.next
;
727 list_del(&xferpage_packet
->list_ent
);
729 /* This is how much we can satisfy */
730 xferpage_packet
->count
= count
- 1;
732 if (xferpage_packet
->count
!= vmxferpage_packet
->range_cnt
) {
733 dev_err(&device
->device
, "Needed %d netvsc pkts to satisy "
734 "this xfer page...got %d",
735 vmxferpage_packet
->range_cnt
, xferpage_packet
->count
);
738 /* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */
739 for (i
= 0; i
< (count
- 1); i
++) {
740 netvsc_packet
= (struct hv_netvsc_packet
*)listHead
.next
;
741 list_del(&netvsc_packet
->list_ent
);
743 /* Initialize the netvsc packet */
744 netvsc_packet
->xfer_page_pkt
= xferpage_packet
;
745 netvsc_packet
->completion
.recv
.recv_completion
=
746 netvsc_receive_completion
;
747 netvsc_packet
->completion
.recv
.recv_completion_ctx
=
749 netvsc_packet
->device
= device
;
750 /* Save this so that we can send it back */
751 netvsc_packet
->completion
.recv
.recv_completion_tid
=
752 vmxferpage_packet
->d
.trans_id
;
754 netvsc_packet
->total_data_buflen
=
755 vmxferpage_packet
->ranges
[i
].byte_count
;
756 netvsc_packet
->page_buf_cnt
= 1;
758 netvsc_packet
->page_buf
[0].len
=
759 vmxferpage_packet
->ranges
[i
].byte_count
;
761 start
= virt_to_phys((void *)((unsigned long)net_device
->
762 recv_buf
+ vmxferpage_packet
->ranges
[i
].byte_offset
));
764 netvsc_packet
->page_buf
[0].pfn
= start
>> PAGE_SHIFT
;
765 end_virtual
= (unsigned long)net_device
->recv_buf
766 + vmxferpage_packet
->ranges
[i
].byte_offset
767 + vmxferpage_packet
->ranges
[i
].byte_count
- 1;
768 end
= virt_to_phys((void *)end_virtual
);
770 /* Calculate the page relative offset */
771 netvsc_packet
->page_buf
[0].offset
=
772 vmxferpage_packet
->ranges
[i
].byte_offset
&
774 if ((end
>> PAGE_SHIFT
) != (start
>> PAGE_SHIFT
)) {
775 /* Handle frame across multiple pages: */
776 netvsc_packet
->page_buf
[0].len
=
777 (netvsc_packet
->page_buf
[0].pfn
<<
780 bytes_remain
= netvsc_packet
->total_data_buflen
-
781 netvsc_packet
->page_buf
[0].len
;
782 for (j
= 1; j
< NETVSC_PACKET_MAXPAGE
; j
++) {
783 netvsc_packet
->page_buf
[j
].offset
= 0;
784 if (bytes_remain
<= PAGE_SIZE
) {
785 netvsc_packet
->page_buf
[j
].len
=
789 netvsc_packet
->page_buf
[j
].len
=
791 bytes_remain
-= PAGE_SIZE
;
793 netvsc_packet
->page_buf
[j
].pfn
=
794 virt_to_phys((void *)(end_virtual
-
795 bytes_remain
)) >> PAGE_SHIFT
;
796 netvsc_packet
->page_buf_cnt
++;
797 if (bytes_remain
== 0)
802 /* Pass it to the upper layer */
803 rndis_filter_receive(device
, netvsc_packet
);
805 netvsc_receive_completion(netvsc_packet
->
806 completion
.recv
.recv_completion_ctx
);
809 put_net_device(device
);
812 static void netvsc_channel_cb(void *context
)
815 struct hv_device
*device
= context
;
816 struct netvsc_device
*net_device
;
819 unsigned char *packet
;
820 struct vmpacket_descriptor
*desc
;
821 unsigned char *buffer
;
822 int bufferlen
= NETVSC_PACKET_SIZE
;
824 packet
= kzalloc(NETVSC_PACKET_SIZE
* sizeof(unsigned char),
830 net_device
= get_inbound_net_device(device
);
832 dev_err(&device
->device
, "net device (%p) shutting down..."
833 "ignoring inbound packets", net_device
);
838 ret
= vmbus_recvpacket_raw(device
->channel
, buffer
, bufferlen
,
839 &bytes_recvd
, &request_id
);
841 if (bytes_recvd
> 0) {
842 desc
= (struct vmpacket_descriptor
*)buffer
;
843 switch (desc
->type
) {
845 netvsc_send_completion(device
, desc
);
848 case VM_PKT_DATA_USING_XFER_PAGES
:
849 netvsc_receive(device
, desc
);
853 dev_err(&device
->device
,
854 "unhandled packet type %d, "
856 desc
->type
, request_id
,
862 if (bufferlen
> NETVSC_PACKET_SIZE
) {
865 bufferlen
= NETVSC_PACKET_SIZE
;
869 if (bufferlen
> NETVSC_PACKET_SIZE
) {
872 bufferlen
= NETVSC_PACKET_SIZE
;
877 } else if (ret
== -ENOBUFS
) {
878 /* Handle large packet */
879 buffer
= kmalloc(bytes_recvd
, GFP_ATOMIC
);
880 if (buffer
== NULL
) {
881 /* Try again next time around */
882 dev_err(&device
->device
,
883 "unable to allocate buffer of size "
884 "(%d)!!", bytes_recvd
);
888 bufferlen
= bytes_recvd
;
892 put_net_device(device
);
899 * netvsc_device_add - Callback when the device belonging to this
902 int netvsc_device_add(struct hv_device
*device
, void *additional_info
)
907 ((struct netvsc_device_info
*)additional_info
)->ring_size
;
908 struct netvsc_device
*net_device
;
909 struct hv_netvsc_packet
*packet
, *pos
;
911 net_device
= alloc_net_device(device
);
917 /* Initialize the NetVSC channel extension */
918 net_device
->recv_buf_size
= NETVSC_RECEIVE_BUFFER_SIZE
;
919 spin_lock_init(&net_device
->recv_pkt_list_lock
);
921 INIT_LIST_HEAD(&net_device
->recv_pkt_list
);
923 for (i
= 0; i
< NETVSC_RECEIVE_PACKETLIST_COUNT
; i
++) {
924 packet
= kzalloc(sizeof(struct hv_netvsc_packet
) +
925 (NETVSC_RECEIVE_SG_COUNT
*
926 sizeof(struct hv_page_buffer
)), GFP_KERNEL
);
930 list_add_tail(&packet
->list_ent
,
931 &net_device
->recv_pkt_list
);
933 init_completion(&net_device
->channel_init_wait
);
935 /* Open the channel */
936 ret
= vmbus_open(device
->channel
, ring_size
* PAGE_SIZE
,
937 ring_size
* PAGE_SIZE
, NULL
, 0,
938 netvsc_channel_cb
, device
);
941 dev_err(&device
->device
, "unable to open channel: %d", ret
);
945 /* Channel is opened */
946 pr_info("hv_netvsc channel opened successfully");
948 /* Connect with the NetVsp */
949 ret
= netvsc_connect_vsp(device
);
951 dev_err(&device
->device
,
952 "unable to connect to NetVSP - %d", ret
);
959 /* Now, we can close the channel safely */
960 vmbus_close(device
->channel
);
965 list_for_each_entry_safe(packet
, pos
,
966 &net_device
->recv_pkt_list
,
968 list_del(&packet
->list_ent
);
972 release_outbound_net_device(device
);
973 release_inbound_net_device(device
);