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 #define RNDIS_EXT_LEN 100
36 struct rndis_request
{
37 struct list_head list_ent
;
38 struct completion wait_event
;
40 struct rndis_message response_msg
;
42 * The buffer for extended info after the RNDIS response message. It's
43 * referenced based on the data offset in the RNDIS message. Its size
44 * is enough for current needs, and should be sufficient for the near
47 u8 response_ext
[RNDIS_EXT_LEN
];
49 /* Simplify allocation by having a netvsc packet inline */
50 struct hv_netvsc_packet pkt
;
51 /* Set 2 pages for rndis requests crossing page boundary */
52 struct hv_page_buffer buf
[2];
54 struct rndis_message request_msg
;
56 * The buffer for the extended info after the RNDIS request message.
57 * It is referenced and sized in a similar way as response_ext.
59 u8 request_ext
[RNDIS_EXT_LEN
];
62 static void rndis_filter_send_completion(void *ctx
);
64 static void rndis_filter_send_request_completion(void *ctx
);
68 static struct rndis_device
*get_rndis_device(void)
70 struct rndis_device
*device
;
72 device
= kzalloc(sizeof(struct rndis_device
), GFP_KERNEL
);
76 spin_lock_init(&device
->request_lock
);
78 INIT_LIST_HEAD(&device
->req_list
);
80 device
->state
= RNDIS_DEV_UNINITIALIZED
;
85 static struct rndis_request
*get_rndis_request(struct rndis_device
*dev
,
89 struct rndis_request
*request
;
90 struct rndis_message
*rndis_msg
;
91 struct rndis_set_request
*set
;
94 request
= kzalloc(sizeof(struct rndis_request
), GFP_KERNEL
);
98 init_completion(&request
->wait_event
);
100 rndis_msg
= &request
->request_msg
;
101 rndis_msg
->ndis_msg_type
= msg_type
;
102 rndis_msg
->msg_len
= msg_len
;
105 * Set the request id. This field is always after the rndis header for
106 * request/response packet types so we just used the SetRequest as a
109 set
= &rndis_msg
->msg
.set_req
;
110 set
->req_id
= atomic_inc_return(&dev
->new_req_id
);
112 /* Add to the request list */
113 spin_lock_irqsave(&dev
->request_lock
, flags
);
114 list_add_tail(&request
->list_ent
, &dev
->req_list
);
115 spin_unlock_irqrestore(&dev
->request_lock
, flags
);
120 static void put_rndis_request(struct rndis_device
*dev
,
121 struct rndis_request
*req
)
125 spin_lock_irqsave(&dev
->request_lock
, flags
);
126 list_del(&req
->list_ent
);
127 spin_unlock_irqrestore(&dev
->request_lock
, flags
);
132 static void dump_rndis_message(struct hv_device
*hv_dev
,
133 struct rndis_message
*rndis_msg
)
135 struct net_device
*netdev
;
136 struct netvsc_device
*net_device
;
138 net_device
= hv_get_drvdata(hv_dev
);
139 netdev
= net_device
->ndev
;
141 switch (rndis_msg
->ndis_msg_type
) {
142 case RNDIS_MSG_PACKET
:
143 netdev_dbg(netdev
, "RNDIS_MSG_PACKET (len %u, "
144 "data offset %u data len %u, # oob %u, "
145 "oob offset %u, oob len %u, pkt offset %u, "
148 rndis_msg
->msg
.pkt
.data_offset
,
149 rndis_msg
->msg
.pkt
.data_len
,
150 rndis_msg
->msg
.pkt
.num_oob_data_elements
,
151 rndis_msg
->msg
.pkt
.oob_data_offset
,
152 rndis_msg
->msg
.pkt
.oob_data_len
,
153 rndis_msg
->msg
.pkt
.per_pkt_info_offset
,
154 rndis_msg
->msg
.pkt
.per_pkt_info_len
);
157 case RNDIS_MSG_INIT_C
:
158 netdev_dbg(netdev
, "RNDIS_MSG_INIT_C "
159 "(len %u, id 0x%x, status 0x%x, major %d, minor %d, "
160 "device flags %d, max xfer size 0x%x, max pkts %u, "
163 rndis_msg
->msg
.init_complete
.req_id
,
164 rndis_msg
->msg
.init_complete
.status
,
165 rndis_msg
->msg
.init_complete
.major_ver
,
166 rndis_msg
->msg
.init_complete
.minor_ver
,
167 rndis_msg
->msg
.init_complete
.dev_flags
,
168 rndis_msg
->msg
.init_complete
.max_xfer_size
,
169 rndis_msg
->msg
.init_complete
.
171 rndis_msg
->msg
.init_complete
.
172 pkt_alignment_factor
);
175 case RNDIS_MSG_QUERY_C
:
176 netdev_dbg(netdev
, "RNDIS_MSG_QUERY_C "
177 "(len %u, id 0x%x, status 0x%x, buf len %u, "
180 rndis_msg
->msg
.query_complete
.req_id
,
181 rndis_msg
->msg
.query_complete
.status
,
182 rndis_msg
->msg
.query_complete
.
184 rndis_msg
->msg
.query_complete
.
188 case RNDIS_MSG_SET_C
:
190 "RNDIS_MSG_SET_C (len %u, id 0x%x, status 0x%x)\n",
192 rndis_msg
->msg
.set_complete
.req_id
,
193 rndis_msg
->msg
.set_complete
.status
);
196 case RNDIS_MSG_INDICATE
:
197 netdev_dbg(netdev
, "RNDIS_MSG_INDICATE "
198 "(len %u, status 0x%x, buf len %u, buf offset %u)\n",
200 rndis_msg
->msg
.indicate_status
.status
,
201 rndis_msg
->msg
.indicate_status
.status_buflen
,
202 rndis_msg
->msg
.indicate_status
.status_buf_offset
);
206 netdev_dbg(netdev
, "0x%x (len %u)\n",
207 rndis_msg
->ndis_msg_type
,
213 static int rndis_filter_send_request(struct rndis_device
*dev
,
214 struct rndis_request
*req
)
217 struct hv_netvsc_packet
*packet
;
219 /* Setup the packet to send it */
222 packet
->is_data_pkt
= false;
223 packet
->total_data_buflen
= req
->request_msg
.msg_len
;
224 packet
->page_buf_cnt
= 1;
226 packet
->page_buf
[0].pfn
= virt_to_phys(&req
->request_msg
) >>
228 packet
->page_buf
[0].len
= req
->request_msg
.msg_len
;
229 packet
->page_buf
[0].offset
=
230 (unsigned long)&req
->request_msg
& (PAGE_SIZE
- 1);
232 /* Add one page_buf when request_msg crossing page boundary */
233 if (packet
->page_buf
[0].offset
+ packet
->page_buf
[0].len
> PAGE_SIZE
) {
234 packet
->page_buf_cnt
++;
235 packet
->page_buf
[0].len
= PAGE_SIZE
-
236 packet
->page_buf
[0].offset
;
237 packet
->page_buf
[1].pfn
= virt_to_phys((void *)&req
->request_msg
238 + packet
->page_buf
[0].len
) >> PAGE_SHIFT
;
239 packet
->page_buf
[1].offset
= 0;
240 packet
->page_buf
[1].len
= req
->request_msg
.msg_len
-
241 packet
->page_buf
[0].len
;
244 packet
->completion
.send
.send_completion_ctx
= req
;/* packet; */
245 packet
->completion
.send
.send_completion
=
246 rndis_filter_send_request_completion
;
247 packet
->completion
.send
.send_completion_tid
= (unsigned long)dev
;
249 ret
= netvsc_send(dev
->net_dev
->dev
, packet
);
253 static void rndis_filter_receive_response(struct rndis_device
*dev
,
254 struct rndis_message
*resp
)
256 struct rndis_request
*request
= NULL
;
259 struct net_device
*ndev
;
261 ndev
= dev
->net_dev
->ndev
;
263 spin_lock_irqsave(&dev
->request_lock
, flags
);
264 list_for_each_entry(request
, &dev
->req_list
, list_ent
) {
266 * All request/response message contains RequestId as the 1st
269 if (request
->request_msg
.msg
.init_req
.req_id
270 == resp
->msg
.init_complete
.req_id
) {
275 spin_unlock_irqrestore(&dev
->request_lock
, flags
);
279 sizeof(struct rndis_message
) + RNDIS_EXT_LEN
) {
280 memcpy(&request
->response_msg
, resp
,
284 "rndis response buffer overflow "
285 "detected (size %u max %zu)\n",
287 sizeof(struct rndis_filter_packet
));
289 if (resp
->ndis_msg_type
==
291 /* does not have a request id field */
292 request
->response_msg
.msg
.reset_complete
.
293 status
= RNDIS_STATUS_BUFFER_OVERFLOW
;
295 request
->response_msg
.msg
.
296 init_complete
.status
=
297 RNDIS_STATUS_BUFFER_OVERFLOW
;
301 complete(&request
->wait_event
);
304 "no rndis request found for this response "
305 "(id 0x%x res type 0x%x)\n",
306 resp
->msg
.init_complete
.req_id
,
307 resp
->ndis_msg_type
);
311 static void rndis_filter_receive_indicate_status(struct rndis_device
*dev
,
312 struct rndis_message
*resp
)
314 struct rndis_indicate_status
*indicate
=
315 &resp
->msg
.indicate_status
;
317 if (indicate
->status
== RNDIS_STATUS_MEDIA_CONNECT
) {
318 netvsc_linkstatus_callback(
319 dev
->net_dev
->dev
, 1);
320 } else if (indicate
->status
== RNDIS_STATUS_MEDIA_DISCONNECT
) {
321 netvsc_linkstatus_callback(
322 dev
->net_dev
->dev
, 0);
331 * Get the Per-Packet-Info with the specified type
332 * return NULL if not found.
334 static inline void *rndis_get_ppi(struct rndis_packet
*rpkt
, u32 type
)
336 struct rndis_per_packet_info
*ppi
;
339 if (rpkt
->per_pkt_info_offset
== 0)
342 ppi
= (struct rndis_per_packet_info
*)((ulong
)rpkt
+
343 rpkt
->per_pkt_info_offset
);
344 len
= rpkt
->per_pkt_info_len
;
347 if (ppi
->type
== type
)
348 return (void *)((ulong
)ppi
+ ppi
->ppi_offset
);
350 ppi
= (struct rndis_per_packet_info
*)((ulong
)ppi
+ ppi
->size
);
356 static void rndis_filter_receive_data(struct rndis_device
*dev
,
357 struct rndis_message
*msg
,
358 struct hv_netvsc_packet
*pkt
)
360 struct rndis_packet
*rndis_pkt
;
362 struct ndis_pkt_8021q_info
*vlan
;
364 rndis_pkt
= &msg
->msg
.pkt
;
366 /* Remove the rndis header and pass it back up the stack */
367 data_offset
= RNDIS_HEADER_SIZE
+ rndis_pkt
->data_offset
;
369 pkt
->total_data_buflen
-= data_offset
;
372 * Make sure we got a valid RNDIS message, now total_data_buflen
373 * should be the data packet size plus the trailer padding size
375 if (pkt
->total_data_buflen
< rndis_pkt
->data_len
) {
376 netdev_err(dev
->net_dev
->ndev
, "rndis message buffer "
377 "overflow detected (got %u, min %u)"
378 "...dropping this message!\n",
379 pkt
->total_data_buflen
, rndis_pkt
->data_len
);
384 * Remove the rndis trailer padding from rndis packet message
385 * rndis_pkt->data_len tell us the real data length, we only copy
386 * the data packet to the stack, without the rndis trailer padding
388 pkt
->total_data_buflen
= rndis_pkt
->data_len
;
389 pkt
->data
= (void *)((unsigned long)pkt
->data
+ data_offset
);
391 pkt
->is_data_pkt
= true;
393 vlan
= rndis_get_ppi(rndis_pkt
, IEEE_8021Q_INFO
);
395 pkt
->vlan_tci
= VLAN_TAG_PRESENT
| vlan
->vlanid
|
396 (vlan
->pri
<< VLAN_PRIO_SHIFT
);
401 netvsc_recv_callback(dev
->net_dev
->dev
, pkt
);
404 int rndis_filter_receive(struct hv_device
*dev
,
405 struct hv_netvsc_packet
*pkt
)
407 struct netvsc_device
*net_dev
= hv_get_drvdata(dev
);
408 struct rndis_device
*rndis_dev
;
409 struct rndis_message
*rndis_msg
;
410 struct net_device
*ndev
;
418 ndev
= net_dev
->ndev
;
420 /* Make sure the rndis device state is initialized */
421 if (!net_dev
->extension
) {
422 netdev_err(ndev
, "got rndis message but no rndis device - "
423 "dropping this message!\n");
428 rndis_dev
= (struct rndis_device
*)net_dev
->extension
;
429 if (rndis_dev
->state
== RNDIS_DEV_UNINITIALIZED
) {
430 netdev_err(ndev
, "got rndis message but rndis device "
431 "uninitialized...dropping this message!\n");
436 rndis_msg
= pkt
->data
;
438 dump_rndis_message(dev
, rndis_msg
);
440 switch (rndis_msg
->ndis_msg_type
) {
441 case RNDIS_MSG_PACKET
:
443 rndis_filter_receive_data(rndis_dev
, rndis_msg
, pkt
);
446 case RNDIS_MSG_INIT_C
:
447 case RNDIS_MSG_QUERY_C
:
448 case RNDIS_MSG_SET_C
:
449 /* completion msgs */
450 rndis_filter_receive_response(rndis_dev
, rndis_msg
);
453 case RNDIS_MSG_INDICATE
:
454 /* notification msgs */
455 rndis_filter_receive_indicate_status(rndis_dev
, rndis_msg
);
459 "unhandled rndis message (type %u len %u)\n",
460 rndis_msg
->ndis_msg_type
,
467 pkt
->status
= NVSP_STAT_FAIL
;
472 static int rndis_filter_query_device(struct rndis_device
*dev
, u32 oid
,
473 void *result
, u32
*result_size
)
475 struct rndis_request
*request
;
476 u32 inresult_size
= *result_size
;
477 struct rndis_query_request
*query
;
478 struct rndis_query_complete
*query_complete
;
486 request
= get_rndis_request(dev
, RNDIS_MSG_QUERY
,
487 RNDIS_MESSAGE_SIZE(struct rndis_query_request
));
493 /* Setup the rndis query */
494 query
= &request
->request_msg
.msg
.query_req
;
496 query
->info_buf_offset
= sizeof(struct rndis_query_request
);
497 query
->info_buflen
= 0;
498 query
->dev_vc_handle
= 0;
500 ret
= rndis_filter_send_request(dev
, request
);
504 t
= wait_for_completion_timeout(&request
->wait_event
, 5*HZ
);
510 /* Copy the response back */
511 query_complete
= &request
->response_msg
.msg
.query_complete
;
513 if (query_complete
->info_buflen
> inresult_size
) {
519 (void *)((unsigned long)query_complete
+
520 query_complete
->info_buf_offset
),
521 query_complete
->info_buflen
);
523 *result_size
= query_complete
->info_buflen
;
527 put_rndis_request(dev
, request
);
532 static int rndis_filter_query_device_mac(struct rndis_device
*dev
)
536 return rndis_filter_query_device(dev
,
537 RNDIS_OID_802_3_PERMANENT_ADDRESS
,
538 dev
->hw_mac_adr
, &size
);
541 #define NWADR_STR "NetworkAddress"
542 #define NWADR_STRLEN 14
544 int rndis_filter_set_device_mac(struct hv_device
*hdev
, char *mac
)
546 struct netvsc_device
*nvdev
= hv_get_drvdata(hdev
);
547 struct rndis_device
*rdev
= nvdev
->extension
;
548 struct net_device
*ndev
= nvdev
->ndev
;
549 struct rndis_request
*request
;
550 struct rndis_set_request
*set
;
551 struct rndis_config_parameter_info
*cpi
;
552 wchar_t *cfg_nwadr
, *cfg_mac
;
553 struct rndis_set_complete
*set_complete
;
554 char macstr
[2*ETH_ALEN
+1];
555 u32 extlen
= sizeof(struct rndis_config_parameter_info
) +
556 2*NWADR_STRLEN
+ 4*ETH_ALEN
;
559 request
= get_rndis_request(rdev
, RNDIS_MSG_SET
,
560 RNDIS_MESSAGE_SIZE(struct rndis_set_request
) + extlen
);
564 set
= &request
->request_msg
.msg
.set_req
;
565 set
->oid
= RNDIS_OID_GEN_RNDIS_CONFIG_PARAMETER
;
566 set
->info_buflen
= extlen
;
567 set
->info_buf_offset
= sizeof(struct rndis_set_request
);
568 set
->dev_vc_handle
= 0;
570 cpi
= (struct rndis_config_parameter_info
*)((ulong
)set
+
571 set
->info_buf_offset
);
572 cpi
->parameter_name_offset
=
573 sizeof(struct rndis_config_parameter_info
);
574 /* Multiply by 2 because host needs 2 bytes (utf16) for each char */
575 cpi
->parameter_name_length
= 2*NWADR_STRLEN
;
576 cpi
->parameter_type
= RNDIS_CONFIG_PARAM_TYPE_STRING
;
577 cpi
->parameter_value_offset
=
578 cpi
->parameter_name_offset
+ cpi
->parameter_name_length
;
579 /* Multiply by 4 because each MAC byte displayed as 2 utf16 chars */
580 cpi
->parameter_value_length
= 4*ETH_ALEN
;
582 cfg_nwadr
= (wchar_t *)((ulong
)cpi
+ cpi
->parameter_name_offset
);
583 cfg_mac
= (wchar_t *)((ulong
)cpi
+ cpi
->parameter_value_offset
);
584 ret
= utf8s_to_utf16s(NWADR_STR
, NWADR_STRLEN
, UTF16_HOST_ENDIAN
,
585 cfg_nwadr
, NWADR_STRLEN
);
588 snprintf(macstr
, 2*ETH_ALEN
+1, "%pm", mac
);
589 ret
= utf8s_to_utf16s(macstr
, 2*ETH_ALEN
, UTF16_HOST_ENDIAN
,
590 cfg_mac
, 2*ETH_ALEN
);
594 ret
= rndis_filter_send_request(rdev
, request
);
598 t
= wait_for_completion_timeout(&request
->wait_event
, 5*HZ
);
600 netdev_err(ndev
, "timeout before we got a set response...\n");
602 * can't put_rndis_request, since we may still receive a
607 set_complete
= &request
->response_msg
.msg
.set_complete
;
608 if (set_complete
->status
!= RNDIS_STATUS_SUCCESS
) {
609 netdev_err(ndev
, "Fail to set MAC on host side:0x%x\n",
610 set_complete
->status
);
616 put_rndis_request(rdev
, request
);
621 static int rndis_filter_query_device_link_status(struct rndis_device
*dev
)
623 u32 size
= sizeof(u32
);
627 ret
= rndis_filter_query_device(dev
,
628 RNDIS_OID_GEN_MEDIA_CONNECT_STATUS
,
629 &link_status
, &size
);
630 dev
->link_state
= (link_status
!= 0) ? true : false;
635 int rndis_filter_set_packet_filter(struct rndis_device
*dev
, u32 new_filter
)
637 struct rndis_request
*request
;
638 struct rndis_set_request
*set
;
639 struct rndis_set_complete
*set_complete
;
642 struct net_device
*ndev
;
644 ndev
= dev
->net_dev
->ndev
;
646 request
= get_rndis_request(dev
, RNDIS_MSG_SET
,
647 RNDIS_MESSAGE_SIZE(struct rndis_set_request
) +
654 /* Setup the rndis set */
655 set
= &request
->request_msg
.msg
.set_req
;
656 set
->oid
= RNDIS_OID_GEN_CURRENT_PACKET_FILTER
;
657 set
->info_buflen
= sizeof(u32
);
658 set
->info_buf_offset
= sizeof(struct rndis_set_request
);
660 memcpy((void *)(unsigned long)set
+ sizeof(struct rndis_set_request
),
661 &new_filter
, sizeof(u32
));
663 ret
= rndis_filter_send_request(dev
, request
);
667 t
= wait_for_completion_timeout(&request
->wait_event
, 5*HZ
);
671 "timeout before we got a set response...\n");
674 * We can't deallocate the request since we may still receive a
675 * send completion for it.
679 set_complete
= &request
->response_msg
.msg
.set_complete
;
680 status
= set_complete
->status
;
685 put_rndis_request(dev
, request
);
691 static int rndis_filter_init_device(struct rndis_device
*dev
)
693 struct rndis_request
*request
;
694 struct rndis_initialize_request
*init
;
695 struct rndis_initialize_complete
*init_complete
;
699 request
= get_rndis_request(dev
, RNDIS_MSG_INIT
,
700 RNDIS_MESSAGE_SIZE(struct rndis_initialize_request
));
706 /* Setup the rndis set */
707 init
= &request
->request_msg
.msg
.init_req
;
708 init
->major_ver
= RNDIS_MAJOR_VERSION
;
709 init
->minor_ver
= RNDIS_MINOR_VERSION
;
710 init
->max_xfer_size
= 0x4000;
712 dev
->state
= RNDIS_DEV_INITIALIZING
;
714 ret
= rndis_filter_send_request(dev
, request
);
716 dev
->state
= RNDIS_DEV_UNINITIALIZED
;
721 t
= wait_for_completion_timeout(&request
->wait_event
, 5*HZ
);
728 init_complete
= &request
->response_msg
.msg
.init_complete
;
729 status
= init_complete
->status
;
730 if (status
== RNDIS_STATUS_SUCCESS
) {
731 dev
->state
= RNDIS_DEV_INITIALIZED
;
734 dev
->state
= RNDIS_DEV_UNINITIALIZED
;
740 put_rndis_request(dev
, request
);
745 static void rndis_filter_halt_device(struct rndis_device
*dev
)
747 struct rndis_request
*request
;
748 struct rndis_halt_request
*halt
;
749 struct netvsc_device
*nvdev
= dev
->net_dev
;
750 struct hv_device
*hdev
= nvdev
->dev
;
753 /* Attempt to do a rndis device halt */
754 request
= get_rndis_request(dev
, RNDIS_MSG_HALT
,
755 RNDIS_MESSAGE_SIZE(struct rndis_halt_request
));
759 /* Setup the rndis set */
760 halt
= &request
->request_msg
.msg
.halt_req
;
761 halt
->req_id
= atomic_inc_return(&dev
->new_req_id
);
763 /* Ignore return since this msg is optional. */
764 rndis_filter_send_request(dev
, request
);
766 dev
->state
= RNDIS_DEV_UNINITIALIZED
;
769 spin_lock_irqsave(&hdev
->channel
->inbound_lock
, flags
);
770 nvdev
->destroy
= true;
771 spin_unlock_irqrestore(&hdev
->channel
->inbound_lock
, flags
);
773 /* Wait for all send completions */
774 wait_event(nvdev
->wait_drain
,
775 atomic_read(&nvdev
->num_outstanding_sends
) == 0);
778 put_rndis_request(dev
, request
);
782 static int rndis_filter_open_device(struct rndis_device
*dev
)
786 if (dev
->state
!= RNDIS_DEV_INITIALIZED
)
789 ret
= rndis_filter_set_packet_filter(dev
,
790 NDIS_PACKET_TYPE_BROADCAST
|
791 NDIS_PACKET_TYPE_ALL_MULTICAST
|
792 NDIS_PACKET_TYPE_DIRECTED
);
794 dev
->state
= RNDIS_DEV_DATAINITIALIZED
;
799 static int rndis_filter_close_device(struct rndis_device
*dev
)
803 if (dev
->state
!= RNDIS_DEV_DATAINITIALIZED
)
806 ret
= rndis_filter_set_packet_filter(dev
, 0);
808 dev
->state
= RNDIS_DEV_INITIALIZED
;
813 int rndis_filter_device_add(struct hv_device
*dev
,
814 void *additional_info
)
817 struct netvsc_device
*net_device
;
818 struct rndis_device
*rndis_device
;
819 struct netvsc_device_info
*device_info
= additional_info
;
821 rndis_device
= get_rndis_device();
826 * Let the inner driver handle this first to create the netvsc channel
827 * NOTE! Once the channel is created, we may get a receive callback
828 * (RndisFilterOnReceive()) before this call is completed
830 ret
= netvsc_device_add(dev
, additional_info
);
837 /* Initialize the rndis device */
838 net_device
= hv_get_drvdata(dev
);
840 net_device
->extension
= rndis_device
;
841 rndis_device
->net_dev
= net_device
;
843 /* Send the rndis initialization message */
844 ret
= rndis_filter_init_device(rndis_device
);
846 rndis_filter_device_remove(dev
);
850 /* Get the mac address */
851 ret
= rndis_filter_query_device_mac(rndis_device
);
853 rndis_filter_device_remove(dev
);
857 memcpy(device_info
->mac_adr
, rndis_device
->hw_mac_adr
, ETH_ALEN
);
859 rndis_filter_query_device_link_status(rndis_device
);
861 device_info
->link_state
= rndis_device
->link_state
;
863 dev_info(&dev
->device
, "Device MAC %pM link state %s\n",
864 rndis_device
->hw_mac_adr
,
865 device_info
->link_state
? "down" : "up");
870 void rndis_filter_device_remove(struct hv_device
*dev
)
872 struct netvsc_device
*net_dev
= hv_get_drvdata(dev
);
873 struct rndis_device
*rndis_dev
= net_dev
->extension
;
875 /* Halt and release the rndis device */
876 rndis_filter_halt_device(rndis_dev
);
879 net_dev
->extension
= NULL
;
881 netvsc_device_remove(dev
);
885 int rndis_filter_open(struct hv_device
*dev
)
887 struct netvsc_device
*net_device
= hv_get_drvdata(dev
);
892 return rndis_filter_open_device(net_device
->extension
);
895 int rndis_filter_close(struct hv_device
*dev
)
897 struct netvsc_device
*nvdev
= hv_get_drvdata(dev
);
902 return rndis_filter_close_device(nvdev
->extension
);
905 int rndis_filter_send(struct hv_device
*dev
,
906 struct hv_netvsc_packet
*pkt
)
909 struct rndis_filter_packet
*filter_pkt
;
910 struct rndis_message
*rndis_msg
;
911 struct rndis_packet
*rndis_pkt
;
913 bool isvlan
= pkt
->vlan_tci
& VLAN_TAG_PRESENT
;
915 /* Add the rndis header */
916 filter_pkt
= (struct rndis_filter_packet
*)pkt
->extension
;
918 rndis_msg
= &filter_pkt
->msg
;
919 rndis_msg_size
= RNDIS_MESSAGE_SIZE(struct rndis_packet
);
921 rndis_msg_size
+= NDIS_VLAN_PPI_SIZE
;
923 rndis_msg
->ndis_msg_type
= RNDIS_MSG_PACKET
;
924 rndis_msg
->msg_len
= pkt
->total_data_buflen
+
927 rndis_pkt
= &rndis_msg
->msg
.pkt
;
928 rndis_pkt
->data_offset
= sizeof(struct rndis_packet
);
930 rndis_pkt
->data_offset
+= NDIS_VLAN_PPI_SIZE
;
931 rndis_pkt
->data_len
= pkt
->total_data_buflen
;
934 struct rndis_per_packet_info
*ppi
;
935 struct ndis_pkt_8021q_info
*vlan
;
937 rndis_pkt
->per_pkt_info_offset
= sizeof(struct rndis_packet
);
938 rndis_pkt
->per_pkt_info_len
= NDIS_VLAN_PPI_SIZE
;
940 ppi
= (struct rndis_per_packet_info
*)((ulong
)rndis_pkt
+
941 rndis_pkt
->per_pkt_info_offset
);
942 ppi
->size
= NDIS_VLAN_PPI_SIZE
;
943 ppi
->type
= IEEE_8021Q_INFO
;
944 ppi
->ppi_offset
= sizeof(struct rndis_per_packet_info
);
946 vlan
= (struct ndis_pkt_8021q_info
*)((ulong
)ppi
+
948 vlan
->vlanid
= pkt
->vlan_tci
& VLAN_VID_MASK
;
949 vlan
->pri
= (pkt
->vlan_tci
& VLAN_PRIO_MASK
) >> VLAN_PRIO_SHIFT
;
952 pkt
->is_data_pkt
= true;
953 pkt
->page_buf
[0].pfn
= virt_to_phys(rndis_msg
) >> PAGE_SHIFT
;
954 pkt
->page_buf
[0].offset
=
955 (unsigned long)rndis_msg
& (PAGE_SIZE
-1);
956 pkt
->page_buf
[0].len
= rndis_msg_size
;
958 /* Add one page_buf if the rndis msg goes beyond page boundary */
959 if (pkt
->page_buf
[0].offset
+ rndis_msg_size
> PAGE_SIZE
) {
961 for (i
= pkt
->page_buf_cnt
; i
> 1; i
--)
962 pkt
->page_buf
[i
] = pkt
->page_buf
[i
-1];
964 pkt
->page_buf
[0].len
= PAGE_SIZE
- pkt
->page_buf
[0].offset
;
965 pkt
->page_buf
[1].pfn
= virt_to_phys((void *)((ulong
)
966 rndis_msg
+ pkt
->page_buf
[0].len
)) >> PAGE_SHIFT
;
967 pkt
->page_buf
[1].offset
= 0;
968 pkt
->page_buf
[1].len
= rndis_msg_size
- pkt
->page_buf
[0].len
;
971 /* Save the packet send completion and context */
972 filter_pkt
->completion
= pkt
->completion
.send
.send_completion
;
973 filter_pkt
->completion_ctx
=
974 pkt
->completion
.send
.send_completion_ctx
;
977 pkt
->completion
.send
.send_completion
= rndis_filter_send_completion
;
978 pkt
->completion
.send
.send_completion_ctx
= filter_pkt
;
980 ret
= netvsc_send(dev
, pkt
);
983 * Reset the completion to originals to allow retries from
986 pkt
->completion
.send
.send_completion
=
987 filter_pkt
->completion
;
988 pkt
->completion
.send
.send_completion_ctx
=
989 filter_pkt
->completion_ctx
;
995 static void rndis_filter_send_completion(void *ctx
)
997 struct rndis_filter_packet
*filter_pkt
= ctx
;
999 /* Pass it back to the original handler */
1000 filter_pkt
->completion(filter_pkt
->completion_ctx
);
1004 static void rndis_filter_send_request_completion(void *ctx
)