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>
22 #include <linux/sched.h>
23 #include <linux/wait.h>
24 #include <linux/highmem.h>
25 #include <linux/slab.h>
27 #include <linux/if_ether.h>
28 #include <linux/netdevice.h>
29 #include <linux/if_vlan.h>
30 #include <linux/nls.h>
32 #include "hyperv_net.h"
35 struct rndis_request
{
36 struct list_head list_ent
;
37 struct completion wait_event
;
40 * FIXME: We assumed a fixed size response here. If we do ever need to
41 * handle a bigger response, we can either define a max response
42 * message or add a response buffer variable above this field
44 struct rndis_message response_msg
;
46 /* Simplify allocation by having a netvsc packet inline */
47 struct hv_netvsc_packet pkt
;
48 struct hv_page_buffer buf
;
49 /* FIXME: We assumed a fixed size request here. */
50 struct rndis_message request_msg
;
54 static void rndis_filter_send_completion(void *ctx
);
56 static void rndis_filter_send_request_completion(void *ctx
);
60 static struct rndis_device
*get_rndis_device(void)
62 struct rndis_device
*device
;
64 device
= kzalloc(sizeof(struct rndis_device
), GFP_KERNEL
);
68 spin_lock_init(&device
->request_lock
);
70 INIT_LIST_HEAD(&device
->req_list
);
72 device
->state
= RNDIS_DEV_UNINITIALIZED
;
77 static struct rndis_request
*get_rndis_request(struct rndis_device
*dev
,
81 struct rndis_request
*request
;
82 struct rndis_message
*rndis_msg
;
83 struct rndis_set_request
*set
;
86 request
= kzalloc(sizeof(struct rndis_request
), GFP_KERNEL
);
90 init_completion(&request
->wait_event
);
92 rndis_msg
= &request
->request_msg
;
93 rndis_msg
->ndis_msg_type
= msg_type
;
94 rndis_msg
->msg_len
= msg_len
;
97 * Set the request id. This field is always after the rndis header for
98 * request/response packet types so we just used the SetRequest as a
101 set
= &rndis_msg
->msg
.set_req
;
102 set
->req_id
= atomic_inc_return(&dev
->new_req_id
);
104 /* Add to the request list */
105 spin_lock_irqsave(&dev
->request_lock
, flags
);
106 list_add_tail(&request
->list_ent
, &dev
->req_list
);
107 spin_unlock_irqrestore(&dev
->request_lock
, flags
);
112 static void put_rndis_request(struct rndis_device
*dev
,
113 struct rndis_request
*req
)
117 spin_lock_irqsave(&dev
->request_lock
, flags
);
118 list_del(&req
->list_ent
);
119 spin_unlock_irqrestore(&dev
->request_lock
, flags
);
124 static void dump_rndis_message(struct hv_device
*hv_dev
,
125 struct rndis_message
*rndis_msg
)
127 struct net_device
*netdev
;
128 struct netvsc_device
*net_device
;
130 net_device
= hv_get_drvdata(hv_dev
);
131 netdev
= net_device
->ndev
;
133 switch (rndis_msg
->ndis_msg_type
) {
134 case RNDIS_MSG_PACKET
:
135 netdev_dbg(netdev
, "RNDIS_MSG_PACKET (len %u, "
136 "data offset %u data len %u, # oob %u, "
137 "oob offset %u, oob len %u, pkt offset %u, "
140 rndis_msg
->msg
.pkt
.data_offset
,
141 rndis_msg
->msg
.pkt
.data_len
,
142 rndis_msg
->msg
.pkt
.num_oob_data_elements
,
143 rndis_msg
->msg
.pkt
.oob_data_offset
,
144 rndis_msg
->msg
.pkt
.oob_data_len
,
145 rndis_msg
->msg
.pkt
.per_pkt_info_offset
,
146 rndis_msg
->msg
.pkt
.per_pkt_info_len
);
149 case RNDIS_MSG_INIT_C
:
150 netdev_dbg(netdev
, "RNDIS_MSG_INIT_C "
151 "(len %u, id 0x%x, status 0x%x, major %d, minor %d, "
152 "device flags %d, max xfer size 0x%x, max pkts %u, "
155 rndis_msg
->msg
.init_complete
.req_id
,
156 rndis_msg
->msg
.init_complete
.status
,
157 rndis_msg
->msg
.init_complete
.major_ver
,
158 rndis_msg
->msg
.init_complete
.minor_ver
,
159 rndis_msg
->msg
.init_complete
.dev_flags
,
160 rndis_msg
->msg
.init_complete
.max_xfer_size
,
161 rndis_msg
->msg
.init_complete
.
163 rndis_msg
->msg
.init_complete
.
164 pkt_alignment_factor
);
167 case RNDIS_MSG_QUERY_C
:
168 netdev_dbg(netdev
, "RNDIS_MSG_QUERY_C "
169 "(len %u, id 0x%x, status 0x%x, buf len %u, "
172 rndis_msg
->msg
.query_complete
.req_id
,
173 rndis_msg
->msg
.query_complete
.status
,
174 rndis_msg
->msg
.query_complete
.
176 rndis_msg
->msg
.query_complete
.
180 case RNDIS_MSG_SET_C
:
182 "RNDIS_MSG_SET_C (len %u, id 0x%x, status 0x%x)\n",
184 rndis_msg
->msg
.set_complete
.req_id
,
185 rndis_msg
->msg
.set_complete
.status
);
188 case RNDIS_MSG_INDICATE
:
189 netdev_dbg(netdev
, "RNDIS_MSG_INDICATE "
190 "(len %u, status 0x%x, buf len %u, buf offset %u)\n",
192 rndis_msg
->msg
.indicate_status
.status
,
193 rndis_msg
->msg
.indicate_status
.status_buflen
,
194 rndis_msg
->msg
.indicate_status
.status_buf_offset
);
198 netdev_dbg(netdev
, "0x%x (len %u)\n",
199 rndis_msg
->ndis_msg_type
,
205 static int rndis_filter_send_request(struct rndis_device
*dev
,
206 struct rndis_request
*req
)
209 struct hv_netvsc_packet
*packet
;
211 /* Setup the packet to send it */
214 packet
->is_data_pkt
= false;
215 packet
->total_data_buflen
= req
->request_msg
.msg_len
;
216 packet
->page_buf_cnt
= 1;
218 packet
->page_buf
[0].pfn
= virt_to_phys(&req
->request_msg
) >>
220 packet
->page_buf
[0].len
= req
->request_msg
.msg_len
;
221 packet
->page_buf
[0].offset
=
222 (unsigned long)&req
->request_msg
& (PAGE_SIZE
- 1);
224 packet
->completion
.send
.send_completion_ctx
= req
;/* packet; */
225 packet
->completion
.send
.send_completion
=
226 rndis_filter_send_request_completion
;
227 packet
->completion
.send
.send_completion_tid
= (unsigned long)dev
;
229 ret
= netvsc_send(dev
->net_dev
->dev
, packet
);
233 static void rndis_filter_receive_response(struct rndis_device
*dev
,
234 struct rndis_message
*resp
)
236 struct rndis_request
*request
= NULL
;
239 struct net_device
*ndev
;
241 ndev
= dev
->net_dev
->ndev
;
243 spin_lock_irqsave(&dev
->request_lock
, flags
);
244 list_for_each_entry(request
, &dev
->req_list
, list_ent
) {
246 * All request/response message contains RequestId as the 1st
249 if (request
->request_msg
.msg
.init_req
.req_id
250 == resp
->msg
.init_complete
.req_id
) {
255 spin_unlock_irqrestore(&dev
->request_lock
, flags
);
258 if (resp
->msg_len
<= sizeof(struct rndis_message
)) {
259 memcpy(&request
->response_msg
, resp
,
263 "rndis response buffer overflow "
264 "detected (size %u max %zu)\n",
266 sizeof(struct rndis_filter_packet
));
268 if (resp
->ndis_msg_type
==
270 /* does not have a request id field */
271 request
->response_msg
.msg
.reset_complete
.
272 status
= RNDIS_STATUS_BUFFER_OVERFLOW
;
274 request
->response_msg
.msg
.
275 init_complete
.status
=
276 RNDIS_STATUS_BUFFER_OVERFLOW
;
280 complete(&request
->wait_event
);
283 "no rndis request found for this response "
284 "(id 0x%x res type 0x%x)\n",
285 resp
->msg
.init_complete
.req_id
,
286 resp
->ndis_msg_type
);
290 static void rndis_filter_receive_indicate_status(struct rndis_device
*dev
,
291 struct rndis_message
*resp
)
293 struct rndis_indicate_status
*indicate
=
294 &resp
->msg
.indicate_status
;
296 if (indicate
->status
== RNDIS_STATUS_MEDIA_CONNECT
) {
297 netvsc_linkstatus_callback(
298 dev
->net_dev
->dev
, 1);
299 } else if (indicate
->status
== RNDIS_STATUS_MEDIA_DISCONNECT
) {
300 netvsc_linkstatus_callback(
301 dev
->net_dev
->dev
, 0);
310 * Get the Per-Packet-Info with the specified type
311 * return NULL if not found.
313 static inline void *rndis_get_ppi(struct rndis_packet
*rpkt
, u32 type
)
315 struct rndis_per_packet_info
*ppi
;
318 if (rpkt
->per_pkt_info_offset
== 0)
321 ppi
= (struct rndis_per_packet_info
*)((ulong
)rpkt
+
322 rpkt
->per_pkt_info_offset
);
323 len
= rpkt
->per_pkt_info_len
;
326 if (ppi
->type
== type
)
327 return (void *)((ulong
)ppi
+ ppi
->ppi_offset
);
329 ppi
= (struct rndis_per_packet_info
*)((ulong
)ppi
+ ppi
->size
);
335 static void rndis_filter_receive_data(struct rndis_device
*dev
,
336 struct rndis_message
*msg
,
337 struct hv_netvsc_packet
*pkt
)
339 struct rndis_packet
*rndis_pkt
;
341 struct ndis_pkt_8021q_info
*vlan
;
343 rndis_pkt
= &msg
->msg
.pkt
;
346 * FIXME: Handle multiple rndis pkt msgs that maybe enclosed in this
347 * netvsc packet (ie TotalDataBufferLength != MessageLength)
350 /* Remove the rndis header and pass it back up the stack */
351 data_offset
= RNDIS_HEADER_SIZE
+ rndis_pkt
->data_offset
;
353 pkt
->total_data_buflen
-= data_offset
;
356 * Make sure we got a valid RNDIS message, now total_data_buflen
357 * should be the data packet size plus the trailer padding size
359 if (pkt
->total_data_buflen
< rndis_pkt
->data_len
) {
360 netdev_err(dev
->net_dev
->ndev
, "rndis message buffer "
361 "overflow detected (got %u, min %u)"
362 "...dropping this message!\n",
363 pkt
->total_data_buflen
, rndis_pkt
->data_len
);
368 * Remove the rndis trailer padding from rndis packet message
369 * rndis_pkt->data_len tell us the real data length, we only copy
370 * the data packet to the stack, without the rndis trailer padding
372 pkt
->total_data_buflen
= rndis_pkt
->data_len
;
373 pkt
->data
= (void *)((unsigned long)pkt
->data
+ data_offset
);
375 pkt
->is_data_pkt
= true;
377 vlan
= rndis_get_ppi(rndis_pkt
, IEEE_8021Q_INFO
);
379 pkt
->vlan_tci
= VLAN_TAG_PRESENT
| vlan
->vlanid
|
380 (vlan
->pri
<< VLAN_PRIO_SHIFT
);
385 netvsc_recv_callback(dev
->net_dev
->dev
, pkt
);
388 int rndis_filter_receive(struct hv_device
*dev
,
389 struct hv_netvsc_packet
*pkt
)
391 struct netvsc_device
*net_dev
= hv_get_drvdata(dev
);
392 struct rndis_device
*rndis_dev
;
393 struct rndis_message
*rndis_msg
;
394 struct net_device
*ndev
;
399 ndev
= net_dev
->ndev
;
401 /* Make sure the rndis device state is initialized */
402 if (!net_dev
->extension
) {
403 netdev_err(ndev
, "got rndis message but no rndis device - "
404 "dropping this message!\n");
408 rndis_dev
= (struct rndis_device
*)net_dev
->extension
;
409 if (rndis_dev
->state
== RNDIS_DEV_UNINITIALIZED
) {
410 netdev_err(ndev
, "got rndis message but rndis device "
411 "uninitialized...dropping this message!\n");
415 rndis_msg
= pkt
->data
;
417 dump_rndis_message(dev
, rndis_msg
);
419 switch (rndis_msg
->ndis_msg_type
) {
420 case RNDIS_MSG_PACKET
:
422 rndis_filter_receive_data(rndis_dev
, rndis_msg
, pkt
);
425 case RNDIS_MSG_INIT_C
:
426 case RNDIS_MSG_QUERY_C
:
427 case RNDIS_MSG_SET_C
:
428 /* completion msgs */
429 rndis_filter_receive_response(rndis_dev
, rndis_msg
);
432 case RNDIS_MSG_INDICATE
:
433 /* notification msgs */
434 rndis_filter_receive_indicate_status(rndis_dev
, rndis_msg
);
438 "unhandled rndis message (type %u len %u)\n",
439 rndis_msg
->ndis_msg_type
,
447 static int rndis_filter_query_device(struct rndis_device
*dev
, u32 oid
,
448 void *result
, u32
*result_size
)
450 struct rndis_request
*request
;
451 u32 inresult_size
= *result_size
;
452 struct rndis_query_request
*query
;
453 struct rndis_query_complete
*query_complete
;
461 request
= get_rndis_request(dev
, RNDIS_MSG_QUERY
,
462 RNDIS_MESSAGE_SIZE(struct rndis_query_request
));
468 /* Setup the rndis query */
469 query
= &request
->request_msg
.msg
.query_req
;
471 query
->info_buf_offset
= sizeof(struct rndis_query_request
);
472 query
->info_buflen
= 0;
473 query
->dev_vc_handle
= 0;
475 ret
= rndis_filter_send_request(dev
, request
);
479 t
= wait_for_completion_timeout(&request
->wait_event
, 5*HZ
);
485 /* Copy the response back */
486 query_complete
= &request
->response_msg
.msg
.query_complete
;
488 if (query_complete
->info_buflen
> inresult_size
) {
494 (void *)((unsigned long)query_complete
+
495 query_complete
->info_buf_offset
),
496 query_complete
->info_buflen
);
498 *result_size
= query_complete
->info_buflen
;
502 put_rndis_request(dev
, request
);
507 static int rndis_filter_query_device_mac(struct rndis_device
*dev
)
511 return rndis_filter_query_device(dev
,
512 RNDIS_OID_802_3_PERMANENT_ADDRESS
,
513 dev
->hw_mac_adr
, &size
);
516 #define NWADR_STR "NetworkAddress"
517 #define NWADR_STRLEN 14
519 int rndis_filter_set_device_mac(struct hv_device
*hdev
, char *mac
)
521 struct netvsc_device
*nvdev
= hv_get_drvdata(hdev
);
522 struct rndis_device
*rdev
= nvdev
->extension
;
523 struct net_device
*ndev
= nvdev
->ndev
;
524 struct rndis_request
*request
;
525 struct rndis_set_request
*set
;
526 struct rndis_config_parameter_info
*cpi
;
527 wchar_t *cfg_nwadr
, *cfg_mac
;
528 struct rndis_set_complete
*set_complete
;
529 char macstr
[2*ETH_ALEN
+1];
530 u32 extlen
= sizeof(struct rndis_config_parameter_info
) +
531 2*NWADR_STRLEN
+ 4*ETH_ALEN
;
534 request
= get_rndis_request(rdev
, RNDIS_MSG_SET
,
535 RNDIS_MESSAGE_SIZE(struct rndis_set_request
) + extlen
);
539 set
= &request
->request_msg
.msg
.set_req
;
540 set
->oid
= RNDIS_OID_GEN_RNDIS_CONFIG_PARAMETER
;
541 set
->info_buflen
= extlen
;
542 set
->info_buf_offset
= sizeof(struct rndis_set_request
);
543 set
->dev_vc_handle
= 0;
545 cpi
= (struct rndis_config_parameter_info
*)((ulong
)set
+
546 set
->info_buf_offset
);
547 cpi
->parameter_name_offset
=
548 sizeof(struct rndis_config_parameter_info
);
549 /* Multiply by 2 because host needs 2 bytes (utf16) for each char */
550 cpi
->parameter_name_length
= 2*NWADR_STRLEN
;
551 cpi
->parameter_type
= RNDIS_CONFIG_PARAM_TYPE_STRING
;
552 cpi
->parameter_value_offset
=
553 cpi
->parameter_name_offset
+ cpi
->parameter_name_length
;
554 /* Multiply by 4 because each MAC byte displayed as 2 utf16 chars */
555 cpi
->parameter_value_length
= 4*ETH_ALEN
;
557 cfg_nwadr
= (wchar_t *)((ulong
)cpi
+ cpi
->parameter_name_offset
);
558 cfg_mac
= (wchar_t *)((ulong
)cpi
+ cpi
->parameter_value_offset
);
559 ret
= utf8s_to_utf16s(NWADR_STR
, NWADR_STRLEN
, UTF16_HOST_ENDIAN
,
560 cfg_nwadr
, NWADR_STRLEN
);
563 snprintf(macstr
, 2*ETH_ALEN
+1, "%pm", mac
);
564 ret
= utf8s_to_utf16s(macstr
, 2*ETH_ALEN
, UTF16_HOST_ENDIAN
,
565 cfg_mac
, 2*ETH_ALEN
);
569 ret
= rndis_filter_send_request(rdev
, request
);
573 t
= wait_for_completion_timeout(&request
->wait_event
, 5*HZ
);
575 netdev_err(ndev
, "timeout before we got a set response...\n");
577 * can't put_rndis_request, since we may still receive a
582 set_complete
= &request
->response_msg
.msg
.set_complete
;
583 if (set_complete
->status
!= RNDIS_STATUS_SUCCESS
)
588 put_rndis_request(rdev
, request
);
593 static int rndis_filter_query_device_link_status(struct rndis_device
*dev
)
595 u32 size
= sizeof(u32
);
599 ret
= rndis_filter_query_device(dev
,
600 RNDIS_OID_GEN_MEDIA_CONNECT_STATUS
,
601 &link_status
, &size
);
602 dev
->link_state
= (link_status
!= 0) ? true : false;
607 int rndis_filter_set_packet_filter(struct rndis_device
*dev
, u32 new_filter
)
609 struct rndis_request
*request
;
610 struct rndis_set_request
*set
;
611 struct rndis_set_complete
*set_complete
;
614 struct net_device
*ndev
;
616 ndev
= dev
->net_dev
->ndev
;
618 request
= get_rndis_request(dev
, RNDIS_MSG_SET
,
619 RNDIS_MESSAGE_SIZE(struct rndis_set_request
) +
626 /* Setup the rndis set */
627 set
= &request
->request_msg
.msg
.set_req
;
628 set
->oid
= RNDIS_OID_GEN_CURRENT_PACKET_FILTER
;
629 set
->info_buflen
= sizeof(u32
);
630 set
->info_buf_offset
= sizeof(struct rndis_set_request
);
632 memcpy((void *)(unsigned long)set
+ sizeof(struct rndis_set_request
),
633 &new_filter
, sizeof(u32
));
635 ret
= rndis_filter_send_request(dev
, request
);
639 t
= wait_for_completion_timeout(&request
->wait_event
, 5*HZ
);
643 "timeout before we got a set response...\n");
645 * We can't deallocate the request since we may still receive a
646 * send completion for it.
650 set_complete
= &request
->response_msg
.msg
.set_complete
;
651 status
= set_complete
->status
;
656 put_rndis_request(dev
, request
);
662 static int rndis_filter_init_device(struct rndis_device
*dev
)
664 struct rndis_request
*request
;
665 struct rndis_initialize_request
*init
;
666 struct rndis_initialize_complete
*init_complete
;
670 request
= get_rndis_request(dev
, RNDIS_MSG_INIT
,
671 RNDIS_MESSAGE_SIZE(struct rndis_initialize_request
));
677 /* Setup the rndis set */
678 init
= &request
->request_msg
.msg
.init_req
;
679 init
->major_ver
= RNDIS_MAJOR_VERSION
;
680 init
->minor_ver
= RNDIS_MINOR_VERSION
;
681 /* FIXME: Use 1536 - rounded ethernet frame size */
682 init
->max_xfer_size
= 2048;
684 dev
->state
= RNDIS_DEV_INITIALIZING
;
686 ret
= rndis_filter_send_request(dev
, request
);
688 dev
->state
= RNDIS_DEV_UNINITIALIZED
;
693 t
= wait_for_completion_timeout(&request
->wait_event
, 5*HZ
);
700 init_complete
= &request
->response_msg
.msg
.init_complete
;
701 status
= init_complete
->status
;
702 if (status
== RNDIS_STATUS_SUCCESS
) {
703 dev
->state
= RNDIS_DEV_INITIALIZED
;
706 dev
->state
= RNDIS_DEV_UNINITIALIZED
;
712 put_rndis_request(dev
, request
);
717 static void rndis_filter_halt_device(struct rndis_device
*dev
)
719 struct rndis_request
*request
;
720 struct rndis_halt_request
*halt
;
722 /* Attempt to do a rndis device halt */
723 request
= get_rndis_request(dev
, RNDIS_MSG_HALT
,
724 RNDIS_MESSAGE_SIZE(struct rndis_halt_request
));
728 /* Setup the rndis set */
729 halt
= &request
->request_msg
.msg
.halt_req
;
730 halt
->req_id
= atomic_inc_return(&dev
->new_req_id
);
732 /* Ignore return since this msg is optional. */
733 rndis_filter_send_request(dev
, request
);
735 dev
->state
= RNDIS_DEV_UNINITIALIZED
;
739 put_rndis_request(dev
, request
);
743 static int rndis_filter_open_device(struct rndis_device
*dev
)
747 if (dev
->state
!= RNDIS_DEV_INITIALIZED
)
750 ret
= rndis_filter_set_packet_filter(dev
,
751 NDIS_PACKET_TYPE_BROADCAST
|
752 NDIS_PACKET_TYPE_ALL_MULTICAST
|
753 NDIS_PACKET_TYPE_DIRECTED
);
755 dev
->state
= RNDIS_DEV_DATAINITIALIZED
;
760 static int rndis_filter_close_device(struct rndis_device
*dev
)
764 if (dev
->state
!= RNDIS_DEV_DATAINITIALIZED
)
767 ret
= rndis_filter_set_packet_filter(dev
, 0);
769 dev
->state
= RNDIS_DEV_INITIALIZED
;
774 int rndis_filter_device_add(struct hv_device
*dev
,
775 void *additional_info
)
778 struct netvsc_device
*net_device
;
779 struct rndis_device
*rndis_device
;
780 struct netvsc_device_info
*device_info
= additional_info
;
782 rndis_device
= get_rndis_device();
787 * Let the inner driver handle this first to create the netvsc channel
788 * NOTE! Once the channel is created, we may get a receive callback
789 * (RndisFilterOnReceive()) before this call is completed
791 ret
= netvsc_device_add(dev
, additional_info
);
798 /* Initialize the rndis device */
799 net_device
= hv_get_drvdata(dev
);
801 net_device
->extension
= rndis_device
;
802 rndis_device
->net_dev
= net_device
;
804 /* Send the rndis initialization message */
805 ret
= rndis_filter_init_device(rndis_device
);
807 rndis_filter_device_remove(dev
);
811 /* Get the mac address */
812 ret
= rndis_filter_query_device_mac(rndis_device
);
814 rndis_filter_device_remove(dev
);
818 memcpy(device_info
->mac_adr
, rndis_device
->hw_mac_adr
, ETH_ALEN
);
820 rndis_filter_query_device_link_status(rndis_device
);
822 device_info
->link_state
= rndis_device
->link_state
;
824 dev_info(&dev
->device
, "Device MAC %pM link state %s\n",
825 rndis_device
->hw_mac_adr
,
826 device_info
->link_state
? "down" : "up");
831 void rndis_filter_device_remove(struct hv_device
*dev
)
833 struct netvsc_device
*net_dev
= hv_get_drvdata(dev
);
834 struct rndis_device
*rndis_dev
= net_dev
->extension
;
836 /* Halt and release the rndis device */
837 rndis_filter_halt_device(rndis_dev
);
840 net_dev
->extension
= NULL
;
842 netvsc_device_remove(dev
);
846 int rndis_filter_open(struct hv_device
*dev
)
848 struct netvsc_device
*net_device
= hv_get_drvdata(dev
);
853 return rndis_filter_open_device(net_device
->extension
);
856 int rndis_filter_close(struct hv_device
*dev
)
858 struct netvsc_device
*nvdev
= hv_get_drvdata(dev
);
863 return rndis_filter_close_device(nvdev
->extension
);
866 int rndis_filter_send(struct hv_device
*dev
,
867 struct hv_netvsc_packet
*pkt
)
870 struct rndis_filter_packet
*filter_pkt
;
871 struct rndis_message
*rndis_msg
;
872 struct rndis_packet
*rndis_pkt
;
874 bool isvlan
= pkt
->vlan_tci
& VLAN_TAG_PRESENT
;
876 /* Add the rndis header */
877 filter_pkt
= (struct rndis_filter_packet
*)pkt
->extension
;
879 rndis_msg
= &filter_pkt
->msg
;
880 rndis_msg_size
= RNDIS_MESSAGE_SIZE(struct rndis_packet
);
882 rndis_msg_size
+= NDIS_VLAN_PPI_SIZE
;
884 rndis_msg
->ndis_msg_type
= RNDIS_MSG_PACKET
;
885 rndis_msg
->msg_len
= pkt
->total_data_buflen
+
888 rndis_pkt
= &rndis_msg
->msg
.pkt
;
889 rndis_pkt
->data_offset
= sizeof(struct rndis_packet
);
891 rndis_pkt
->data_offset
+= NDIS_VLAN_PPI_SIZE
;
892 rndis_pkt
->data_len
= pkt
->total_data_buflen
;
895 struct rndis_per_packet_info
*ppi
;
896 struct ndis_pkt_8021q_info
*vlan
;
898 rndis_pkt
->per_pkt_info_offset
= sizeof(struct rndis_packet
);
899 rndis_pkt
->per_pkt_info_len
= NDIS_VLAN_PPI_SIZE
;
901 ppi
= (struct rndis_per_packet_info
*)((ulong
)rndis_pkt
+
902 rndis_pkt
->per_pkt_info_offset
);
903 ppi
->size
= NDIS_VLAN_PPI_SIZE
;
904 ppi
->type
= IEEE_8021Q_INFO
;
905 ppi
->ppi_offset
= sizeof(struct rndis_per_packet_info
);
907 vlan
= (struct ndis_pkt_8021q_info
*)((ulong
)ppi
+
909 vlan
->vlanid
= pkt
->vlan_tci
& VLAN_VID_MASK
;
910 vlan
->pri
= (pkt
->vlan_tci
& VLAN_PRIO_MASK
) >> VLAN_PRIO_SHIFT
;
913 pkt
->is_data_pkt
= true;
914 pkt
->page_buf
[0].pfn
= virt_to_phys(rndis_msg
) >> PAGE_SHIFT
;
915 pkt
->page_buf
[0].offset
=
916 (unsigned long)rndis_msg
& (PAGE_SIZE
-1);
917 pkt
->page_buf
[0].len
= rndis_msg_size
;
919 /* Add one page_buf if the rndis msg goes beyond page boundary */
920 if (pkt
->page_buf
[0].offset
+ rndis_msg_size
> PAGE_SIZE
) {
922 for (i
= pkt
->page_buf_cnt
; i
> 1; i
--)
923 pkt
->page_buf
[i
] = pkt
->page_buf
[i
-1];
925 pkt
->page_buf
[0].len
= PAGE_SIZE
- pkt
->page_buf
[0].offset
;
926 pkt
->page_buf
[1].pfn
= virt_to_phys((void *)((ulong
)
927 rndis_msg
+ pkt
->page_buf
[0].len
)) >> PAGE_SHIFT
;
928 pkt
->page_buf
[1].offset
= 0;
929 pkt
->page_buf
[1].len
= rndis_msg_size
- pkt
->page_buf
[0].len
;
932 /* Save the packet send completion and context */
933 filter_pkt
->completion
= pkt
->completion
.send
.send_completion
;
934 filter_pkt
->completion_ctx
=
935 pkt
->completion
.send
.send_completion_ctx
;
938 pkt
->completion
.send
.send_completion
= rndis_filter_send_completion
;
939 pkt
->completion
.send
.send_completion_ctx
= filter_pkt
;
941 ret
= netvsc_send(dev
, pkt
);
944 * Reset the completion to originals to allow retries from
947 pkt
->completion
.send
.send_completion
=
948 filter_pkt
->completion
;
949 pkt
->completion
.send
.send_completion_ctx
=
950 filter_pkt
->completion_ctx
;
956 static void rndis_filter_send_completion(void *ctx
)
958 struct rndis_filter_packet
*filter_pkt
= ctx
;
960 /* Pass it back to the original handler */
961 filter_pkt
->completion(filter_pkt
->completion_ctx
);
965 static void rndis_filter_send_request_completion(void *ctx
)