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_cmpxchg(&net_device
->refcnt
, 0, 2);
46 net_device
->dev
= device
;
47 device
->ext
= net_device
;
52 static void free_net_device(struct netvsc_device
*device
)
54 WARN_ON(atomic_read(&device
->refcnt
) != 0);
55 device
->dev
->ext
= NULL
;
60 /* Get the net device object iff exists and its refcount > 1 */
61 static struct netvsc_device
*get_outbound_net_device(struct hv_device
*device
)
63 struct netvsc_device
*net_device
;
65 net_device
= device
->ext
;
66 if (net_device
&& atomic_read(&net_device
->refcnt
) > 1)
67 atomic_inc(&net_device
->refcnt
);
74 /* Get the net device object iff exists and its refcount > 0 */
75 static struct netvsc_device
*get_inbound_net_device(struct hv_device
*device
)
77 struct netvsc_device
*net_device
;
79 net_device
= device
->ext
;
80 if (net_device
&& atomic_read(&net_device
->refcnt
))
81 atomic_inc(&net_device
->refcnt
);
88 static void put_net_device(struct hv_device
*device
)
90 struct netvsc_device
*net_device
;
92 net_device
= device
->ext
;
94 atomic_dec(&net_device
->refcnt
);
97 static struct netvsc_device
*release_outbound_net_device(
98 struct hv_device
*device
)
100 struct netvsc_device
*net_device
;
102 net_device
= device
->ext
;
103 if (net_device
== NULL
)
106 /* Busy wait until the ref drop to 2, then set it to 1 */
107 while (atomic_cmpxchg(&net_device
->refcnt
, 2, 1) != 2)
113 static struct netvsc_device
*release_inbound_net_device(
114 struct hv_device
*device
)
116 struct netvsc_device
*net_device
;
118 net_device
= device
->ext
;
119 if (net_device
== NULL
)
122 /* Busy wait until the ref drop to 1, then set it to 0 */
123 while (atomic_cmpxchg(&net_device
->refcnt
, 1, 0) != 1)
130 static int netvsc_destroy_recv_buf(struct netvsc_device
*net_device
)
132 struct nvsp_message
*revoke_packet
;
136 * If we got a section count, it means we received a
137 * SendReceiveBufferComplete msg (ie sent
138 * NvspMessage1TypeSendReceiveBuffer msg) therefore, we need
139 * to send a revoke msg here
141 if (net_device
->recv_section_cnt
) {
142 /* Send the revoke receive buffer */
143 revoke_packet
= &net_device
->revoke_packet
;
144 memset(revoke_packet
, 0, sizeof(struct nvsp_message
));
146 revoke_packet
->hdr
.msg_type
=
147 NVSP_MSG1_TYPE_REVOKE_RECV_BUF
;
148 revoke_packet
->msg
.v1_msg
.
149 revoke_recv_buf
.id
= NETVSC_RECEIVE_BUFFER_ID
;
151 ret
= vmbus_sendpacket(net_device
->dev
->channel
,
153 sizeof(struct nvsp_message
),
154 (unsigned long)revoke_packet
,
155 VM_PKT_DATA_INBAND
, 0);
157 * If we failed here, we might as well return and
158 * have a leak rather than continue and a bugchk
161 dev_err(&net_device
->dev
->device
, "unable to send "
162 "revoke receive buffer to netvsp");
167 /* Teardown the gpadl on the vsp end */
168 if (net_device
->recv_buf_gpadl_handle
) {
169 ret
= vmbus_teardown_gpadl(net_device
->dev
->channel
,
170 net_device
->recv_buf_gpadl_handle
);
172 /* If we failed here, we might as well return and have a leak
173 * rather than continue and a bugchk
176 dev_err(&net_device
->dev
->device
,
177 "unable to teardown receive buffer's gpadl");
180 net_device
->recv_buf_gpadl_handle
= 0;
183 if (net_device
->recv_buf
) {
184 /* Free up the receive buffer */
185 free_pages((unsigned long)net_device
->recv_buf
,
186 get_order(net_device
->recv_buf_size
));
187 net_device
->recv_buf
= NULL
;
190 if (net_device
->recv_section
) {
191 net_device
->recv_section_cnt
= 0;
192 kfree(net_device
->recv_section
);
193 net_device
->recv_section
= NULL
;
199 static int netvsc_init_recv_buf(struct hv_device
*device
)
203 struct netvsc_device
*net_device
;
204 struct nvsp_message
*init_packet
;
206 net_device
= get_outbound_net_device(device
);
208 dev_err(&device
->device
, "unable to get net device..."
209 "device being destroyed?");
213 net_device
->recv_buf
=
214 (void *)__get_free_pages(GFP_KERNEL
|__GFP_ZERO
,
215 get_order(net_device
->recv_buf_size
));
216 if (!net_device
->recv_buf
) {
217 dev_err(&device
->device
, "unable to allocate receive "
218 "buffer of size %d", net_device
->recv_buf_size
);
224 * Establish the gpadl handle for this buffer on this
225 * channel. Note: This call uses the vmbus connection rather
226 * than the channel to establish the gpadl handle.
228 ret
= vmbus_establish_gpadl(device
->channel
, net_device
->recv_buf
,
229 net_device
->recv_buf_size
,
230 &net_device
->recv_buf_gpadl_handle
);
232 dev_err(&device
->device
,
233 "unable to establish receive buffer's gpadl");
238 /* Notify the NetVsp of the gpadl handle */
239 init_packet
= &net_device
->channel_init_pkt
;
241 memset(init_packet
, 0, sizeof(struct nvsp_message
));
243 init_packet
->hdr
.msg_type
= NVSP_MSG1_TYPE_SEND_RECV_BUF
;
244 init_packet
->msg
.v1_msg
.send_recv_buf
.
245 gpadl_handle
= net_device
->recv_buf_gpadl_handle
;
246 init_packet
->msg
.v1_msg
.
247 send_recv_buf
.id
= NETVSC_RECEIVE_BUFFER_ID
;
249 /* Send the gpadl notification request */
250 ret
= vmbus_sendpacket(device
->channel
, init_packet
,
251 sizeof(struct nvsp_message
),
252 (unsigned long)init_packet
,
254 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
256 dev_err(&device
->device
,
257 "unable to send receive buffer's gpadl to netvsp");
261 t
= wait_for_completion_timeout(&net_device
->channel_init_wait
, 5*HZ
);
265 /* Check the response */
266 if (init_packet
->msg
.v1_msg
.
267 send_recv_buf_complete
.status
!= NVSP_STAT_SUCCESS
) {
268 dev_err(&device
->device
, "Unable to complete receive buffer "
269 "initialzation with NetVsp - status %d",
270 init_packet
->msg
.v1_msg
.
271 send_recv_buf_complete
.status
);
276 /* Parse the response */
278 net_device
->recv_section_cnt
= init_packet
->msg
.
279 v1_msg
.send_recv_buf_complete
.num_sections
;
281 net_device
->recv_section
= kmalloc(net_device
->recv_section_cnt
282 * sizeof(struct nvsp_1_receive_buffer_section
), GFP_KERNEL
);
283 if (net_device
->recv_section
== NULL
) {
288 memcpy(net_device
->recv_section
,
289 init_packet
->msg
.v1_msg
.
290 send_recv_buf_complete
.sections
,
291 net_device
->recv_section_cnt
*
292 sizeof(struct nvsp_1_receive_buffer_section
));
295 * For 1st release, there should only be 1 section that represents the
296 * entire receive buffer
298 if (net_device
->recv_section_cnt
!= 1 ||
299 net_device
->recv_section
->offset
!= 0) {
307 netvsc_destroy_recv_buf(net_device
);
310 put_net_device(device
);
315 static int netvsc_connect_vsp(struct hv_device
*device
)
318 struct netvsc_device
*net_device
;
319 struct nvsp_message
*init_packet
;
322 net_device
= get_outbound_net_device(device
);
324 dev_err(&device
->device
, "unable to get net device..."
325 "device being destroyed?");
329 init_packet
= &net_device
->channel_init_pkt
;
331 memset(init_packet
, 0, sizeof(struct nvsp_message
));
332 init_packet
->hdr
.msg_type
= NVSP_MSG_TYPE_INIT
;
333 init_packet
->msg
.init_msg
.init
.min_protocol_ver
=
334 NVSP_MIN_PROTOCOL_VERSION
;
335 init_packet
->msg
.init_msg
.init
.max_protocol_ver
=
336 NVSP_MAX_PROTOCOL_VERSION
;
338 /* Send the init request */
339 ret
= vmbus_sendpacket(device
->channel
, init_packet
,
340 sizeof(struct nvsp_message
),
341 (unsigned long)init_packet
,
343 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
348 t
= wait_for_completion_timeout(&net_device
->channel_init_wait
, 5*HZ
);
355 if (init_packet
->msg
.init_msg
.init_complete
.status
!=
361 if (init_packet
->msg
.init_msg
.init_complete
.
362 negotiated_protocol_ver
!= NVSP_PROTOCOL_VERSION_1
) {
366 /* Send the ndis version */
367 memset(init_packet
, 0, sizeof(struct nvsp_message
));
369 ndis_version
= 0x00050000;
371 init_packet
->hdr
.msg_type
= NVSP_MSG1_TYPE_SEND_NDIS_VER
;
372 init_packet
->msg
.v1_msg
.
373 send_ndis_ver
.ndis_major_ver
=
374 (ndis_version
& 0xFFFF0000) >> 16;
375 init_packet
->msg
.v1_msg
.
376 send_ndis_ver
.ndis_minor_ver
=
377 ndis_version
& 0xFFFF;
379 /* Send the init request */
380 ret
= vmbus_sendpacket(device
->channel
, init_packet
,
381 sizeof(struct nvsp_message
),
382 (unsigned long)init_packet
,
383 VM_PKT_DATA_INBAND
, 0);
389 /* Post the big receive buffer to NetVSP */
390 ret
= netvsc_init_recv_buf(device
);
393 put_net_device(device
);
397 static void netvsc_disconnect_vsp(struct netvsc_device
*net_device
)
399 netvsc_destroy_recv_buf(net_device
);
403 * netvsc_device_remove - Callback when the root bus device is removed
405 int netvsc_device_remove(struct hv_device
*device
)
407 struct netvsc_device
*net_device
;
408 struct hv_netvsc_packet
*netvsc_packet
, *pos
;
410 /* Stop outbound traffic ie sends and receives completions */
411 net_device
= release_outbound_net_device(device
);
413 dev_err(&device
->device
, "No net device present!!");
417 /* Wait for all send completions */
418 while (atomic_read(&net_device
->num_outstanding_sends
)) {
419 dev_err(&device
->device
,
420 "waiting for %d requests to complete...",
421 atomic_read(&net_device
->num_outstanding_sends
));
425 netvsc_disconnect_vsp(net_device
);
427 /* Stop inbound traffic ie receives and sends completions */
428 net_device
= release_inbound_net_device(device
);
430 /* At this point, no one should be accessing netDevice except in here */
431 dev_notice(&device
->device
, "net device safe to remove");
433 /* Now, we can close the channel safely */
434 vmbus_close(device
->channel
);
436 /* Release all resources */
437 list_for_each_entry_safe(netvsc_packet
, pos
,
438 &net_device
->recv_pkt_list
, list_ent
) {
439 list_del(&netvsc_packet
->list_ent
);
440 kfree(netvsc_packet
);
443 free_net_device(net_device
);
447 static void netvsc_send_completion(struct hv_device
*device
,
448 struct vmpacket_descriptor
*packet
)
450 struct netvsc_device
*net_device
;
451 struct nvsp_message
*nvsp_packet
;
452 struct hv_netvsc_packet
*nvsc_packet
;
454 net_device
= get_inbound_net_device(device
);
456 dev_err(&device
->device
, "unable to get net device..."
457 "device being destroyed?");
461 nvsp_packet
= (struct nvsp_message
*)((unsigned long)packet
+
462 (packet
->offset8
<< 3));
464 if ((nvsp_packet
->hdr
.msg_type
== NVSP_MSG_TYPE_INIT_COMPLETE
) ||
465 (nvsp_packet
->hdr
.msg_type
==
466 NVSP_MSG1_TYPE_SEND_RECV_BUF_COMPLETE
) ||
467 (nvsp_packet
->hdr
.msg_type
==
468 NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE
)) {
469 /* Copy the response back */
470 memcpy(&net_device
->channel_init_pkt
, nvsp_packet
,
471 sizeof(struct nvsp_message
));
472 complete(&net_device
->channel_init_wait
);
473 } else if (nvsp_packet
->hdr
.msg_type
==
474 NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE
) {
475 /* Get the send context */
476 nvsc_packet
= (struct hv_netvsc_packet
*)(unsigned long)
479 /* Notify the layer above us */
480 nvsc_packet
->completion
.send
.send_completion(
481 nvsc_packet
->completion
.send
.send_completion_ctx
);
483 atomic_dec(&net_device
->num_outstanding_sends
);
485 dev_err(&device
->device
, "Unknown send completion packet type- "
486 "%d received!!", nvsp_packet
->hdr
.msg_type
);
489 put_net_device(device
);
492 int netvsc_send(struct hv_device
*device
,
493 struct hv_netvsc_packet
*packet
)
495 struct netvsc_device
*net_device
;
498 struct nvsp_message sendMessage
;
500 net_device
= get_outbound_net_device(device
);
502 dev_err(&device
->device
, "net device (%p) shutting down..."
503 "ignoring outbound packets", net_device
);
507 sendMessage
.hdr
.msg_type
= NVSP_MSG1_TYPE_SEND_RNDIS_PKT
;
508 if (packet
->is_data_pkt
) {
510 sendMessage
.msg
.v1_msg
.send_rndis_pkt
.channel_type
= 0;
512 /* 1 is RMC_CONTROL; */
513 sendMessage
.msg
.v1_msg
.send_rndis_pkt
.channel_type
= 1;
516 /* Not using send buffer section */
517 sendMessage
.msg
.v1_msg
.send_rndis_pkt
.send_buf_section_index
=
519 sendMessage
.msg
.v1_msg
.send_rndis_pkt
.send_buf_section_size
= 0;
521 if (packet
->page_buf_cnt
) {
522 ret
= vmbus_sendpacket_pagebuffer(device
->channel
,
524 packet
->page_buf_cnt
,
526 sizeof(struct nvsp_message
),
527 (unsigned long)packet
);
529 ret
= vmbus_sendpacket(device
->channel
, &sendMessage
,
530 sizeof(struct nvsp_message
),
531 (unsigned long)packet
,
533 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
538 dev_err(&device
->device
, "Unable to send packet %p ret %d",
541 atomic_inc(&net_device
->num_outstanding_sends
);
542 put_net_device(device
);
546 static void netvsc_send_recv_completion(struct hv_device
*device
,
549 struct nvsp_message recvcompMessage
;
553 recvcompMessage
.hdr
.msg_type
=
554 NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE
;
556 /* FIXME: Pass in the status */
557 recvcompMessage
.msg
.v1_msg
.send_rndis_pkt_complete
.status
=
561 /* Send the completion */
562 ret
= vmbus_sendpacket(device
->channel
, &recvcompMessage
,
563 sizeof(struct nvsp_message
), transaction_id
,
568 } else if (ret
== -EAGAIN
) {
569 /* no more room...wait a bit and attempt to retry 3 times */
571 dev_err(&device
->device
, "unable to send receive completion pkt"
572 " (tid %llx)...retrying %d", transaction_id
, retries
);
576 goto retry_send_cmplt
;
578 dev_err(&device
->device
, "unable to send receive "
579 "completion pkt (tid %llx)...give up retrying",
583 dev_err(&device
->device
, "unable to send receive "
584 "completion pkt - %llx", transaction_id
);
588 /* Send a receive completion packet to RNDIS device (ie NetVsp) */
589 static void netvsc_receive_completion(void *context
)
591 struct hv_netvsc_packet
*packet
= context
;
592 struct hv_device
*device
= (struct hv_device
*)packet
->device
;
593 struct netvsc_device
*net_device
;
594 u64 transaction_id
= 0;
595 bool fsend_receive_comp
= false;
599 * Even though it seems logical to do a GetOutboundNetDevice() here to
600 * send out receive completion, we are using GetInboundNetDevice()
601 * since we may have disable outbound traffic already.
603 net_device
= get_inbound_net_device(device
);
605 dev_err(&device
->device
, "unable to get net device..."
606 "device being destroyed?");
610 /* Overloading use of the lock. */
611 spin_lock_irqsave(&net_device
->recv_pkt_list_lock
, flags
);
613 packet
->xfer_page_pkt
->count
--;
616 * Last one in the line that represent 1 xfer page packet.
617 * Return the xfer page packet itself to the freelist
619 if (packet
->xfer_page_pkt
->count
== 0) {
620 fsend_receive_comp
= true;
621 transaction_id
= packet
->completion
.recv
.recv_completion_tid
;
622 list_add_tail(&packet
->xfer_page_pkt
->list_ent
,
623 &net_device
->recv_pkt_list
);
627 /* Put the packet back */
628 list_add_tail(&packet
->list_ent
, &net_device
->recv_pkt_list
);
629 spin_unlock_irqrestore(&net_device
->recv_pkt_list_lock
, flags
);
631 /* Send a receive completion for the xfer page packet */
632 if (fsend_receive_comp
)
633 netvsc_send_recv_completion(device
, transaction_id
);
635 put_net_device(device
);
638 static void netvsc_receive(struct hv_device
*device
,
639 struct vmpacket_descriptor
*packet
)
641 struct netvsc_device
*net_device
;
642 struct vmtransfer_page_packet_header
*vmxferpage_packet
;
643 struct nvsp_message
*nvsp_packet
;
644 struct hv_netvsc_packet
*netvsc_packet
= NULL
;
646 unsigned long end
, end_virtual
;
647 /* struct netvsc_driver *netvscDriver; */
648 struct xferpage_packet
*xferpage_packet
= NULL
;
650 int count
= 0, bytes_remain
= 0;
655 net_device
= get_inbound_net_device(device
);
657 dev_err(&device
->device
, "unable to get net device..."
658 "device being destroyed?");
663 * All inbound packets other than send completion should be xfer page
666 if (packet
->type
!= VM_PKT_DATA_USING_XFER_PAGES
) {
667 dev_err(&device
->device
, "Unknown packet type received - %d",
669 put_net_device(device
);
673 nvsp_packet
= (struct nvsp_message
*)((unsigned long)packet
+
674 (packet
->offset8
<< 3));
676 /* Make sure this is a valid nvsp packet */
677 if (nvsp_packet
->hdr
.msg_type
!=
678 NVSP_MSG1_TYPE_SEND_RNDIS_PKT
) {
679 dev_err(&device
->device
, "Unknown nvsp packet type received-"
680 " %d", nvsp_packet
->hdr
.msg_type
);
681 put_net_device(device
);
685 vmxferpage_packet
= (struct vmtransfer_page_packet_header
*)packet
;
687 if (vmxferpage_packet
->xfer_pageset_id
!= NETVSC_RECEIVE_BUFFER_ID
) {
688 dev_err(&device
->device
, "Invalid xfer page set id - "
689 "expecting %x got %x", NETVSC_RECEIVE_BUFFER_ID
,
690 vmxferpage_packet
->xfer_pageset_id
);
691 put_net_device(device
);
696 * Grab free packets (range count + 1) to represent this xfer
697 * page packet. +1 to represent the xfer page packet itself.
698 * We grab it here so that we know exactly how many we can
701 spin_lock_irqsave(&net_device
->recv_pkt_list_lock
, flags
);
702 while (!list_empty(&net_device
->recv_pkt_list
)) {
703 list_move_tail(net_device
->recv_pkt_list
.next
, &listHead
);
704 if (++count
== vmxferpage_packet
->range_cnt
+ 1)
707 spin_unlock_irqrestore(&net_device
->recv_pkt_list_lock
, flags
);
710 * We need at least 2 netvsc pkts (1 to represent the xfer
711 * page and at least 1 for the range) i.e. we can handled
712 * some of the xfer page packet ranges...
715 dev_err(&device
->device
, "Got only %d netvsc pkt...needed "
716 "%d pkts. Dropping this xfer page packet completely!",
717 count
, vmxferpage_packet
->range_cnt
+ 1);
719 /* Return it to the freelist */
720 spin_lock_irqsave(&net_device
->recv_pkt_list_lock
, flags
);
721 for (i
= count
; i
!= 0; i
--) {
722 list_move_tail(listHead
.next
,
723 &net_device
->recv_pkt_list
);
725 spin_unlock_irqrestore(&net_device
->recv_pkt_list_lock
,
728 netvsc_send_recv_completion(device
,
729 vmxferpage_packet
->d
.trans_id
);
731 put_net_device(device
);
735 /* Remove the 1st packet to represent the xfer page packet itself */
736 xferpage_packet
= (struct xferpage_packet
*)listHead
.next
;
737 list_del(&xferpage_packet
->list_ent
);
739 /* This is how much we can satisfy */
740 xferpage_packet
->count
= count
- 1;
742 if (xferpage_packet
->count
!= vmxferpage_packet
->range_cnt
) {
743 dev_err(&device
->device
, "Needed %d netvsc pkts to satisy "
744 "this xfer page...got %d",
745 vmxferpage_packet
->range_cnt
, xferpage_packet
->count
);
748 /* Each range represents 1 RNDIS pkt that contains 1 ethernet frame */
749 for (i
= 0; i
< (count
- 1); i
++) {
750 netvsc_packet
= (struct hv_netvsc_packet
*)listHead
.next
;
751 list_del(&netvsc_packet
->list_ent
);
753 /* Initialize the netvsc packet */
754 netvsc_packet
->xfer_page_pkt
= xferpage_packet
;
755 netvsc_packet
->completion
.recv
.recv_completion
=
756 netvsc_receive_completion
;
757 netvsc_packet
->completion
.recv
.recv_completion_ctx
=
759 netvsc_packet
->device
= device
;
760 /* Save this so that we can send it back */
761 netvsc_packet
->completion
.recv
.recv_completion_tid
=
762 vmxferpage_packet
->d
.trans_id
;
764 netvsc_packet
->total_data_buflen
=
765 vmxferpage_packet
->ranges
[i
].byte_count
;
766 netvsc_packet
->page_buf_cnt
= 1;
768 netvsc_packet
->page_buf
[0].len
=
769 vmxferpage_packet
->ranges
[i
].byte_count
;
771 start
= virt_to_phys((void *)((unsigned long)net_device
->
772 recv_buf
+ vmxferpage_packet
->ranges
[i
].byte_offset
));
774 netvsc_packet
->page_buf
[0].pfn
= start
>> PAGE_SHIFT
;
775 end_virtual
= (unsigned long)net_device
->recv_buf
776 + vmxferpage_packet
->ranges
[i
].byte_offset
777 + vmxferpage_packet
->ranges
[i
].byte_count
- 1;
778 end
= virt_to_phys((void *)end_virtual
);
780 /* Calculate the page relative offset */
781 netvsc_packet
->page_buf
[0].offset
=
782 vmxferpage_packet
->ranges
[i
].byte_offset
&
784 if ((end
>> PAGE_SHIFT
) != (start
>> PAGE_SHIFT
)) {
785 /* Handle frame across multiple pages: */
786 netvsc_packet
->page_buf
[0].len
=
787 (netvsc_packet
->page_buf
[0].pfn
<<
790 bytes_remain
= netvsc_packet
->total_data_buflen
-
791 netvsc_packet
->page_buf
[0].len
;
792 for (j
= 1; j
< NETVSC_PACKET_MAXPAGE
; j
++) {
793 netvsc_packet
->page_buf
[j
].offset
= 0;
794 if (bytes_remain
<= PAGE_SIZE
) {
795 netvsc_packet
->page_buf
[j
].len
=
799 netvsc_packet
->page_buf
[j
].len
=
801 bytes_remain
-= PAGE_SIZE
;
803 netvsc_packet
->page_buf
[j
].pfn
=
804 virt_to_phys((void *)(end_virtual
-
805 bytes_remain
)) >> PAGE_SHIFT
;
806 netvsc_packet
->page_buf_cnt
++;
807 if (bytes_remain
== 0)
812 /* Pass it to the upper layer */
813 rndis_filter_receive(device
, netvsc_packet
);
815 netvsc_receive_completion(netvsc_packet
->
816 completion
.recv
.recv_completion_ctx
);
819 put_net_device(device
);
822 static void netvsc_channel_cb(void *context
)
825 struct hv_device
*device
= context
;
826 struct netvsc_device
*net_device
;
829 unsigned char *packet
;
830 struct vmpacket_descriptor
*desc
;
831 unsigned char *buffer
;
832 int bufferlen
= NETVSC_PACKET_SIZE
;
834 packet
= kzalloc(NETVSC_PACKET_SIZE
* sizeof(unsigned char),
840 net_device
= get_inbound_net_device(device
);
842 dev_err(&device
->device
, "net device (%p) shutting down..."
843 "ignoring inbound packets", net_device
);
848 ret
= vmbus_recvpacket_raw(device
->channel
, buffer
, bufferlen
,
849 &bytes_recvd
, &request_id
);
851 if (bytes_recvd
> 0) {
852 desc
= (struct vmpacket_descriptor
*)buffer
;
853 switch (desc
->type
) {
855 netvsc_send_completion(device
, desc
);
858 case VM_PKT_DATA_USING_XFER_PAGES
:
859 netvsc_receive(device
, desc
);
863 dev_err(&device
->device
,
864 "unhandled packet type %d, "
866 desc
->type
, request_id
,
872 if (bufferlen
> NETVSC_PACKET_SIZE
) {
875 bufferlen
= NETVSC_PACKET_SIZE
;
879 if (bufferlen
> NETVSC_PACKET_SIZE
) {
882 bufferlen
= NETVSC_PACKET_SIZE
;
887 } else if (ret
== -ENOBUFS
) {
888 /* Handle large packet */
889 buffer
= kmalloc(bytes_recvd
, GFP_ATOMIC
);
890 if (buffer
== NULL
) {
891 /* Try again next time around */
892 dev_err(&device
->device
,
893 "unable to allocate buffer of size "
894 "(%d)!!", bytes_recvd
);
898 bufferlen
= bytes_recvd
;
902 put_net_device(device
);
909 * netvsc_device_add - Callback when the device belonging to this
912 int netvsc_device_add(struct hv_device
*device
, void *additional_info
)
917 ((struct netvsc_device_info
*)additional_info
)->ring_size
;
918 struct netvsc_device
*net_device
;
919 struct hv_netvsc_packet
*packet
, *pos
;
921 net_device
= alloc_net_device(device
);
927 /* Initialize the NetVSC channel extension */
928 net_device
->recv_buf_size
= NETVSC_RECEIVE_BUFFER_SIZE
;
929 spin_lock_init(&net_device
->recv_pkt_list_lock
);
931 INIT_LIST_HEAD(&net_device
->recv_pkt_list
);
933 for (i
= 0; i
< NETVSC_RECEIVE_PACKETLIST_COUNT
; i
++) {
934 packet
= kzalloc(sizeof(struct hv_netvsc_packet
) +
935 (NETVSC_RECEIVE_SG_COUNT
*
936 sizeof(struct hv_page_buffer
)), GFP_KERNEL
);
940 list_add_tail(&packet
->list_ent
,
941 &net_device
->recv_pkt_list
);
943 init_completion(&net_device
->channel_init_wait
);
945 /* Open the channel */
946 ret
= vmbus_open(device
->channel
, ring_size
* PAGE_SIZE
,
947 ring_size
* PAGE_SIZE
, NULL
, 0,
948 netvsc_channel_cb
, device
);
951 dev_err(&device
->device
, "unable to open channel: %d", ret
);
956 /* Channel is opened */
957 pr_info("hv_netvsc channel opened successfully");
959 /* Connect with the NetVsp */
960 ret
= netvsc_connect_vsp(device
);
962 dev_err(&device
->device
,
963 "unable to connect to NetVSP - %d", ret
);
971 /* Now, we can close the channel safely */
972 vmbus_close(device
->channel
);
977 list_for_each_entry_safe(packet
, pos
,
978 &net_device
->recv_pkt_list
,
980 list_del(&packet
->list_ent
);
984 release_outbound_net_device(device
);
985 release_inbound_net_device(device
);
987 free_net_device(net_device
);