1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2009, Microsoft Corporation.
6 * Haiyang Zhang <haiyangz@microsoft.com>
7 * Hank Janssen <hjanssen@microsoft.com>
9 #include <linux/ethtool.h>
10 #include <linux/kernel.h>
11 #include <linux/sched.h>
12 #include <linux/wait.h>
13 #include <linux/highmem.h>
14 #include <linux/slab.h>
16 #include <linux/if_ether.h>
17 #include <linux/netdevice.h>
18 #include <linux/if_vlan.h>
19 #include <linux/nls.h>
20 #include <linux/vmalloc.h>
21 #include <linux/rtnetlink.h>
22 #include <linux/ucs2_string.h>
24 #include "hyperv_net.h"
25 #include "netvsc_trace.h"
27 static void rndis_set_multicast(struct work_struct
*w
);
29 #define RNDIS_EXT_LEN HV_HYP_PAGE_SIZE
30 struct rndis_request
{
31 struct list_head list_ent
;
32 struct completion wait_event
;
34 struct rndis_message response_msg
;
36 * The buffer for extended info after the RNDIS response message. It's
37 * referenced based on the data offset in the RNDIS message. Its size
38 * is enough for current needs, and should be sufficient for the near
41 u8 response_ext
[RNDIS_EXT_LEN
];
43 /* Simplify allocation by having a netvsc packet inline */
44 struct hv_netvsc_packet pkt
;
46 struct rndis_message request_msg
;
48 * The buffer for the extended info after the RNDIS request message.
49 * It is referenced and sized in a similar way as response_ext.
51 u8 request_ext
[RNDIS_EXT_LEN
];
54 static const u8 netvsc_hash_key
[NETVSC_HASH_KEYLEN
] = {
55 0x6d, 0x5a, 0x56, 0xda, 0x25, 0x5b, 0x0e, 0xc2,
56 0x41, 0x67, 0x25, 0x3d, 0x43, 0xa3, 0x8f, 0xb0,
57 0xd0, 0xca, 0x2b, 0xcb, 0xae, 0x7b, 0x30, 0xb4,
58 0x77, 0xcb, 0x2d, 0xa3, 0x80, 0x30, 0xf2, 0x0c,
59 0x6a, 0x42, 0xb7, 0x3b, 0xbe, 0xac, 0x01, 0xfa
62 static struct rndis_device
*get_rndis_device(void)
64 struct rndis_device
*device
;
66 device
= kzalloc(sizeof(struct rndis_device
), GFP_KERNEL
);
70 spin_lock_init(&device
->request_lock
);
72 INIT_LIST_HEAD(&device
->req_list
);
73 INIT_WORK(&device
->mcast_work
, rndis_set_multicast
);
75 device
->state
= RNDIS_DEV_UNINITIALIZED
;
80 static struct rndis_request
*get_rndis_request(struct rndis_device
*dev
,
84 struct rndis_request
*request
;
85 struct rndis_message
*rndis_msg
;
86 struct rndis_set_request
*set
;
89 request
= kzalloc(sizeof(struct rndis_request
), GFP_KERNEL
);
93 init_completion(&request
->wait_event
);
95 rndis_msg
= &request
->request_msg
;
96 rndis_msg
->ndis_msg_type
= msg_type
;
97 rndis_msg
->msg_len
= msg_len
;
99 request
->pkt
.q_idx
= 0;
102 * Set the request id. This field is always after the rndis header for
103 * request/response packet types so we just used the SetRequest as a
106 set
= &rndis_msg
->msg
.set_req
;
107 set
->req_id
= atomic_inc_return(&dev
->new_req_id
);
109 /* Add to the request list */
110 spin_lock_irqsave(&dev
->request_lock
, flags
);
111 list_add_tail(&request
->list_ent
, &dev
->req_list
);
112 spin_unlock_irqrestore(&dev
->request_lock
, flags
);
117 static void put_rndis_request(struct rndis_device
*dev
,
118 struct rndis_request
*req
)
122 spin_lock_irqsave(&dev
->request_lock
, flags
);
123 list_del(&req
->list_ent
);
124 spin_unlock_irqrestore(&dev
->request_lock
, flags
);
129 static void dump_rndis_message(struct net_device
*netdev
,
130 const struct rndis_message
*rndis_msg
)
132 switch (rndis_msg
->ndis_msg_type
) {
133 case RNDIS_MSG_PACKET
:
134 netdev_dbg(netdev
, "RNDIS_MSG_PACKET (len %u, "
135 "data offset %u data len %u, # oob %u, "
136 "oob offset %u, oob len %u, pkt offset %u, "
139 rndis_msg
->msg
.pkt
.data_offset
,
140 rndis_msg
->msg
.pkt
.data_len
,
141 rndis_msg
->msg
.pkt
.num_oob_data_elements
,
142 rndis_msg
->msg
.pkt
.oob_data_offset
,
143 rndis_msg
->msg
.pkt
.oob_data_len
,
144 rndis_msg
->msg
.pkt
.per_pkt_info_offset
,
145 rndis_msg
->msg
.pkt
.per_pkt_info_len
);
148 case RNDIS_MSG_INIT_C
:
149 netdev_dbg(netdev
, "RNDIS_MSG_INIT_C "
150 "(len %u, id 0x%x, status 0x%x, major %d, minor %d, "
151 "device flags %d, max xfer size 0x%x, max pkts %u, "
154 rndis_msg
->msg
.init_complete
.req_id
,
155 rndis_msg
->msg
.init_complete
.status
,
156 rndis_msg
->msg
.init_complete
.major_ver
,
157 rndis_msg
->msg
.init_complete
.minor_ver
,
158 rndis_msg
->msg
.init_complete
.dev_flags
,
159 rndis_msg
->msg
.init_complete
.max_xfer_size
,
160 rndis_msg
->msg
.init_complete
.
162 rndis_msg
->msg
.init_complete
.
163 pkt_alignment_factor
);
166 case RNDIS_MSG_QUERY_C
:
167 netdev_dbg(netdev
, "RNDIS_MSG_QUERY_C "
168 "(len %u, id 0x%x, status 0x%x, buf len %u, "
171 rndis_msg
->msg
.query_complete
.req_id
,
172 rndis_msg
->msg
.query_complete
.status
,
173 rndis_msg
->msg
.query_complete
.
175 rndis_msg
->msg
.query_complete
.
179 case RNDIS_MSG_SET_C
:
181 "RNDIS_MSG_SET_C (len %u, id 0x%x, status 0x%x)\n",
183 rndis_msg
->msg
.set_complete
.req_id
,
184 rndis_msg
->msg
.set_complete
.status
);
187 case RNDIS_MSG_INDICATE
:
188 netdev_dbg(netdev
, "RNDIS_MSG_INDICATE "
189 "(len %u, status 0x%x, buf len %u, buf offset %u)\n",
191 rndis_msg
->msg
.indicate_status
.status
,
192 rndis_msg
->msg
.indicate_status
.status_buflen
,
193 rndis_msg
->msg
.indicate_status
.status_buf_offset
);
197 netdev_dbg(netdev
, "0x%x (len %u)\n",
198 rndis_msg
->ndis_msg_type
,
204 static int rndis_filter_send_request(struct rndis_device
*dev
,
205 struct rndis_request
*req
)
207 struct hv_netvsc_packet
*packet
;
208 struct hv_page_buffer page_buf
[2];
209 struct hv_page_buffer
*pb
= page_buf
;
212 /* Setup the packet to send it */
215 packet
->total_data_buflen
= req
->request_msg
.msg_len
;
216 packet
->page_buf_cnt
= 1;
218 pb
[0].pfn
= virt_to_phys(&req
->request_msg
) >>
220 pb
[0].len
= req
->request_msg
.msg_len
;
221 pb
[0].offset
= offset_in_hvpage(&req
->request_msg
);
223 /* Add one page_buf when request_msg crossing page boundary */
224 if (pb
[0].offset
+ pb
[0].len
> HV_HYP_PAGE_SIZE
) {
225 packet
->page_buf_cnt
++;
226 pb
[0].len
= HV_HYP_PAGE_SIZE
-
228 pb
[1].pfn
= virt_to_phys((void *)&req
->request_msg
229 + pb
[0].len
) >> HV_HYP_PAGE_SHIFT
;
231 pb
[1].len
= req
->request_msg
.msg_len
-
235 trace_rndis_send(dev
->ndev
, 0, &req
->request_msg
);
238 ret
= netvsc_send(dev
->ndev
, packet
, NULL
, pb
, NULL
, false);
239 rcu_read_unlock_bh();
244 static void rndis_set_link_state(struct rndis_device
*rdev
,
245 struct rndis_request
*request
)
248 struct rndis_query_complete
*query_complete
;
250 query_complete
= &request
->response_msg
.msg
.query_complete
;
252 if (query_complete
->status
== RNDIS_STATUS_SUCCESS
&&
253 query_complete
->info_buflen
== sizeof(u32
)) {
254 memcpy(&link_status
, (void *)((unsigned long)query_complete
+
255 query_complete
->info_buf_offset
), sizeof(u32
));
256 rdev
->link_state
= link_status
!= 0;
260 static void rndis_filter_receive_response(struct net_device
*ndev
,
261 struct netvsc_device
*nvdev
,
262 const struct rndis_message
*resp
)
264 struct rndis_device
*dev
= nvdev
->extension
;
265 struct rndis_request
*request
= NULL
;
269 /* This should never happen, it means control message
270 * response received after device removed.
272 if (dev
->state
== RNDIS_DEV_UNINITIALIZED
) {
274 "got rndis message uninitialized\n");
278 /* Ensure the packet is big enough to read req_id. Req_id is the 1st
279 * field in any request/response message, so the payload should have at
280 * least sizeof(u32) bytes
282 if (resp
->msg_len
- RNDIS_HEADER_SIZE
< sizeof(u32
)) {
283 netdev_err(ndev
, "rndis msg_len too small: %u\n",
288 spin_lock_irqsave(&dev
->request_lock
, flags
);
289 list_for_each_entry(request
, &dev
->req_list
, list_ent
) {
291 * All request/response message contains RequestId as the 1st
294 if (request
->request_msg
.msg
.init_req
.req_id
295 == resp
->msg
.init_complete
.req_id
) {
300 spin_unlock_irqrestore(&dev
->request_lock
, flags
);
304 sizeof(struct rndis_message
) + RNDIS_EXT_LEN
) {
305 memcpy(&request
->response_msg
, resp
,
307 if (request
->request_msg
.ndis_msg_type
==
308 RNDIS_MSG_QUERY
&& request
->request_msg
.msg
.
309 query_req
.oid
== RNDIS_OID_GEN_MEDIA_CONNECT_STATUS
)
310 rndis_set_link_state(dev
, request
);
313 "rndis response buffer overflow "
314 "detected (size %u max %zu)\n",
316 sizeof(struct rndis_message
));
318 if (resp
->ndis_msg_type
==
320 /* does not have a request id field */
321 request
->response_msg
.msg
.reset_complete
.
322 status
= RNDIS_STATUS_BUFFER_OVERFLOW
;
324 request
->response_msg
.msg
.
325 init_complete
.status
=
326 RNDIS_STATUS_BUFFER_OVERFLOW
;
330 complete(&request
->wait_event
);
333 "no rndis request found for this response "
334 "(id 0x%x res type 0x%x)\n",
335 resp
->msg
.init_complete
.req_id
,
336 resp
->ndis_msg_type
);
341 * Get the Per-Packet-Info with the specified type
342 * return NULL if not found.
344 static inline void *rndis_get_ppi(struct net_device
*ndev
,
345 struct rndis_packet
*rpkt
,
346 u32 rpkt_len
, u32 type
, u8 internal
)
348 struct rndis_per_packet_info
*ppi
;
351 if (rpkt
->per_pkt_info_offset
== 0)
354 /* Validate info_offset and info_len */
355 if (rpkt
->per_pkt_info_offset
< sizeof(struct rndis_packet
) ||
356 rpkt
->per_pkt_info_offset
> rpkt_len
) {
357 netdev_err(ndev
, "Invalid per_pkt_info_offset: %u\n",
358 rpkt
->per_pkt_info_offset
);
362 if (rpkt
->per_pkt_info_len
> rpkt_len
- rpkt
->per_pkt_info_offset
) {
363 netdev_err(ndev
, "Invalid per_pkt_info_len: %u\n",
364 rpkt
->per_pkt_info_len
);
368 ppi
= (struct rndis_per_packet_info
*)((ulong
)rpkt
+
369 rpkt
->per_pkt_info_offset
);
370 len
= rpkt
->per_pkt_info_len
;
373 /* Validate ppi_offset and ppi_size */
374 if (ppi
->size
> len
) {
375 netdev_err(ndev
, "Invalid ppi size: %u\n", ppi
->size
);
379 if (ppi
->ppi_offset
>= ppi
->size
) {
380 netdev_err(ndev
, "Invalid ppi_offset: %u\n", ppi
->ppi_offset
);
384 if (ppi
->type
== type
&& ppi
->internal
== internal
)
385 return (void *)((ulong
)ppi
+ ppi
->ppi_offset
);
387 ppi
= (struct rndis_per_packet_info
*)((ulong
)ppi
+ ppi
->size
);
394 void rsc_add_data(struct netvsc_channel
*nvchan
,
395 const struct ndis_pkt_8021q_info
*vlan
,
396 const struct ndis_tcp_ip_checksum_info
*csum_info
,
397 const u32
*hash_info
,
400 u32 cnt
= nvchan
->rsc
.cnt
;
403 nvchan
->rsc
.pktlen
+= len
;
405 nvchan
->rsc
.vlan
= vlan
;
406 nvchan
->rsc
.csum_info
= csum_info
;
407 nvchan
->rsc
.pktlen
= len
;
408 nvchan
->rsc
.hash_info
= hash_info
;
411 nvchan
->rsc
.data
[cnt
] = data
;
412 nvchan
->rsc
.len
[cnt
] = len
;
416 static int rndis_filter_receive_data(struct net_device
*ndev
,
417 struct netvsc_device
*nvdev
,
418 struct netvsc_channel
*nvchan
,
419 struct rndis_message
*msg
,
422 struct rndis_packet
*rndis_pkt
= &msg
->msg
.pkt
;
423 const struct ndis_tcp_ip_checksum_info
*csum_info
;
424 const struct ndis_pkt_8021q_info
*vlan
;
425 const struct rndis_pktinfo_id
*pktinfo_id
;
426 const u32
*hash_info
;
427 u32 data_offset
, rpkt_len
;
429 bool rsc_more
= false;
432 /* Ensure data_buflen is big enough to read header fields */
433 if (data_buflen
< RNDIS_HEADER_SIZE
+ sizeof(struct rndis_packet
)) {
434 netdev_err(ndev
, "invalid rndis pkt, data_buflen too small: %u\n",
436 return NVSP_STAT_FAIL
;
439 /* Validate rndis_pkt offset */
440 if (rndis_pkt
->data_offset
>= data_buflen
- RNDIS_HEADER_SIZE
) {
441 netdev_err(ndev
, "invalid rndis packet offset: %u\n",
442 rndis_pkt
->data_offset
);
443 return NVSP_STAT_FAIL
;
446 /* Remove the rndis header and pass it back up the stack */
447 data_offset
= RNDIS_HEADER_SIZE
+ rndis_pkt
->data_offset
;
449 rpkt_len
= data_buflen
- RNDIS_HEADER_SIZE
;
450 data_buflen
-= data_offset
;
453 * Make sure we got a valid RNDIS message, now total_data_buflen
454 * should be the data packet size plus the trailer padding size
456 if (unlikely(data_buflen
< rndis_pkt
->data_len
)) {
457 netdev_err(ndev
, "rndis message buffer "
458 "overflow detected (got %u, min %u)"
459 "...dropping this message!\n",
460 data_buflen
, rndis_pkt
->data_len
);
461 return NVSP_STAT_FAIL
;
464 vlan
= rndis_get_ppi(ndev
, rndis_pkt
, rpkt_len
, IEEE_8021Q_INFO
, 0);
466 csum_info
= rndis_get_ppi(ndev
, rndis_pkt
, rpkt_len
, TCPIP_CHKSUM_PKTINFO
, 0);
468 hash_info
= rndis_get_ppi(ndev
, rndis_pkt
, rpkt_len
, NBL_HASH_VALUE
, 0);
470 pktinfo_id
= rndis_get_ppi(ndev
, rndis_pkt
, rpkt_len
, RNDIS_PKTINFO_ID
, 1);
472 data
= (void *)msg
+ data_offset
;
474 /* Identify RSC frags, drop erroneous packets */
475 if (pktinfo_id
&& (pktinfo_id
->flag
& RNDIS_PKTINFO_SUBALLOC
)) {
476 if (pktinfo_id
->flag
& RNDIS_PKTINFO_1ST_FRAG
)
478 else if (nvchan
->rsc
.cnt
== 0)
483 if (pktinfo_id
->flag
& RNDIS_PKTINFO_LAST_FRAG
)
486 if (rsc_more
&& nvchan
->rsc
.is_last
)
492 if (unlikely(nvchan
->rsc
.cnt
>= NVSP_RSC_MAX
))
495 /* Put data into per channel structure.
496 * Also, remove the rndis trailer padding from rndis packet message
497 * rndis_pkt->data_len tell us the real data length, we only copy
498 * the data packet to the stack, without the rndis trailer padding
500 rsc_add_data(nvchan
, vlan
, csum_info
, hash_info
,
501 data
, rndis_pkt
->data_len
);
504 return NVSP_STAT_SUCCESS
;
506 ret
= netvsc_recv_callback(ndev
, nvdev
, nvchan
);
512 return NVSP_STAT_FAIL
;
515 int rndis_filter_receive(struct net_device
*ndev
,
516 struct netvsc_device
*net_dev
,
517 struct netvsc_channel
*nvchan
,
518 void *data
, u32 buflen
)
520 struct net_device_context
*net_device_ctx
= netdev_priv(ndev
);
521 struct rndis_message
*rndis_msg
= data
;
523 if (netif_msg_rx_status(net_device_ctx
))
524 dump_rndis_message(ndev
, rndis_msg
);
526 /* Validate incoming rndis_message packet */
527 if (buflen
< RNDIS_HEADER_SIZE
|| rndis_msg
->msg_len
< RNDIS_HEADER_SIZE
||
528 buflen
< rndis_msg
->msg_len
) {
529 netdev_err(ndev
, "Invalid rndis_msg (buflen: %u, msg_len: %u)\n",
530 buflen
, rndis_msg
->msg_len
);
531 return NVSP_STAT_FAIL
;
534 switch (rndis_msg
->ndis_msg_type
) {
535 case RNDIS_MSG_PACKET
:
536 return rndis_filter_receive_data(ndev
, net_dev
, nvchan
,
538 case RNDIS_MSG_INIT_C
:
539 case RNDIS_MSG_QUERY_C
:
540 case RNDIS_MSG_SET_C
:
541 /* completion msgs */
542 rndis_filter_receive_response(ndev
, net_dev
, rndis_msg
);
545 case RNDIS_MSG_INDICATE
:
546 /* notification msgs */
547 netvsc_linkstatus_callback(ndev
, rndis_msg
);
551 "unhandled rndis message (type %u len %u)\n",
552 rndis_msg
->ndis_msg_type
,
554 return NVSP_STAT_FAIL
;
557 return NVSP_STAT_SUCCESS
;
560 static int rndis_filter_query_device(struct rndis_device
*dev
,
561 struct netvsc_device
*nvdev
,
562 u32 oid
, void *result
, u32
*result_size
)
564 struct rndis_request
*request
;
565 u32 inresult_size
= *result_size
;
566 struct rndis_query_request
*query
;
567 struct rndis_query_complete
*query_complete
;
574 request
= get_rndis_request(dev
, RNDIS_MSG_QUERY
,
575 RNDIS_MESSAGE_SIZE(struct rndis_query_request
));
581 /* Setup the rndis query */
582 query
= &request
->request_msg
.msg
.query_req
;
584 query
->info_buf_offset
= sizeof(struct rndis_query_request
);
585 query
->info_buflen
= 0;
586 query
->dev_vc_handle
= 0;
588 if (oid
== OID_TCP_OFFLOAD_HARDWARE_CAPABILITIES
) {
589 struct ndis_offload
*hwcaps
;
590 u32 nvsp_version
= nvdev
->nvsp_version
;
594 if (nvsp_version
>= NVSP_PROTOCOL_VERSION_5
) {
595 ndis_rev
= NDIS_OFFLOAD_PARAMETERS_REVISION_3
;
596 size
= NDIS_OFFLOAD_SIZE
;
597 } else if (nvsp_version
>= NVSP_PROTOCOL_VERSION_4
) {
598 ndis_rev
= NDIS_OFFLOAD_PARAMETERS_REVISION_2
;
599 size
= NDIS_OFFLOAD_SIZE_6_1
;
601 ndis_rev
= NDIS_OFFLOAD_PARAMETERS_REVISION_1
;
602 size
= NDIS_OFFLOAD_SIZE_6_0
;
605 request
->request_msg
.msg_len
+= size
;
606 query
->info_buflen
= size
;
607 hwcaps
= (struct ndis_offload
*)
608 ((unsigned long)query
+ query
->info_buf_offset
);
610 hwcaps
->header
.type
= NDIS_OBJECT_TYPE_OFFLOAD
;
611 hwcaps
->header
.revision
= ndis_rev
;
612 hwcaps
->header
.size
= size
;
614 } else if (oid
== OID_GEN_RECEIVE_SCALE_CAPABILITIES
) {
615 struct ndis_recv_scale_cap
*cap
;
617 request
->request_msg
.msg_len
+=
618 sizeof(struct ndis_recv_scale_cap
);
619 query
->info_buflen
= sizeof(struct ndis_recv_scale_cap
);
620 cap
= (struct ndis_recv_scale_cap
*)((unsigned long)query
+
621 query
->info_buf_offset
);
622 cap
->hdr
.type
= NDIS_OBJECT_TYPE_RSS_CAPABILITIES
;
623 cap
->hdr
.rev
= NDIS_RECEIVE_SCALE_CAPABILITIES_REVISION_2
;
624 cap
->hdr
.size
= sizeof(struct ndis_recv_scale_cap
);
627 ret
= rndis_filter_send_request(dev
, request
);
631 wait_for_completion(&request
->wait_event
);
633 /* Copy the response back */
634 query_complete
= &request
->response_msg
.msg
.query_complete
;
636 if (query_complete
->info_buflen
> inresult_size
) {
642 (void *)((unsigned long)query_complete
+
643 query_complete
->info_buf_offset
),
644 query_complete
->info_buflen
);
646 *result_size
= query_complete
->info_buflen
;
650 put_rndis_request(dev
, request
);
655 /* Get the hardware offload capabilities */
657 rndis_query_hwcaps(struct rndis_device
*dev
, struct netvsc_device
*net_device
,
658 struct ndis_offload
*caps
)
660 u32 caps_len
= sizeof(*caps
);
663 memset(caps
, 0, sizeof(*caps
));
665 ret
= rndis_filter_query_device(dev
, net_device
,
666 OID_TCP_OFFLOAD_HARDWARE_CAPABILITIES
,
671 if (caps
->header
.type
!= NDIS_OBJECT_TYPE_OFFLOAD
) {
672 netdev_warn(dev
->ndev
, "invalid NDIS objtype %#x\n",
677 if (caps
->header
.revision
< NDIS_OFFLOAD_PARAMETERS_REVISION_1
) {
678 netdev_warn(dev
->ndev
, "invalid NDIS objrev %x\n",
679 caps
->header
.revision
);
683 if (caps
->header
.size
> caps_len
||
684 caps
->header
.size
< NDIS_OFFLOAD_SIZE_6_0
) {
685 netdev_warn(dev
->ndev
,
686 "invalid NDIS objsize %u, data size %u\n",
687 caps
->header
.size
, caps_len
);
694 static int rndis_filter_query_device_mac(struct rndis_device
*dev
,
695 struct netvsc_device
*net_device
)
699 return rndis_filter_query_device(dev
, net_device
,
700 RNDIS_OID_802_3_PERMANENT_ADDRESS
,
701 dev
->hw_mac_adr
, &size
);
704 #define NWADR_STR "NetworkAddress"
705 #define NWADR_STRLEN 14
707 int rndis_filter_set_device_mac(struct netvsc_device
*nvdev
,
710 struct rndis_device
*rdev
= nvdev
->extension
;
711 struct rndis_request
*request
;
712 struct rndis_set_request
*set
;
713 struct rndis_config_parameter_info
*cpi
;
714 wchar_t *cfg_nwadr
, *cfg_mac
;
715 struct rndis_set_complete
*set_complete
;
716 char macstr
[2*ETH_ALEN
+1];
717 u32 extlen
= sizeof(struct rndis_config_parameter_info
) +
718 2*NWADR_STRLEN
+ 4*ETH_ALEN
;
721 request
= get_rndis_request(rdev
, RNDIS_MSG_SET
,
722 RNDIS_MESSAGE_SIZE(struct rndis_set_request
) + extlen
);
726 set
= &request
->request_msg
.msg
.set_req
;
727 set
->oid
= RNDIS_OID_GEN_RNDIS_CONFIG_PARAMETER
;
728 set
->info_buflen
= extlen
;
729 set
->info_buf_offset
= sizeof(struct rndis_set_request
);
730 set
->dev_vc_handle
= 0;
732 cpi
= (struct rndis_config_parameter_info
*)((ulong
)set
+
733 set
->info_buf_offset
);
734 cpi
->parameter_name_offset
=
735 sizeof(struct rndis_config_parameter_info
);
736 /* Multiply by 2 because host needs 2 bytes (utf16) for each char */
737 cpi
->parameter_name_length
= 2*NWADR_STRLEN
;
738 cpi
->parameter_type
= RNDIS_CONFIG_PARAM_TYPE_STRING
;
739 cpi
->parameter_value_offset
=
740 cpi
->parameter_name_offset
+ cpi
->parameter_name_length
;
741 /* Multiply by 4 because each MAC byte displayed as 2 utf16 chars */
742 cpi
->parameter_value_length
= 4*ETH_ALEN
;
744 cfg_nwadr
= (wchar_t *)((ulong
)cpi
+ cpi
->parameter_name_offset
);
745 cfg_mac
= (wchar_t *)((ulong
)cpi
+ cpi
->parameter_value_offset
);
746 ret
= utf8s_to_utf16s(NWADR_STR
, NWADR_STRLEN
, UTF16_HOST_ENDIAN
,
747 cfg_nwadr
, NWADR_STRLEN
);
750 snprintf(macstr
, 2*ETH_ALEN
+1, "%pm", mac
);
751 ret
= utf8s_to_utf16s(macstr
, 2*ETH_ALEN
, UTF16_HOST_ENDIAN
,
752 cfg_mac
, 2*ETH_ALEN
);
756 ret
= rndis_filter_send_request(rdev
, request
);
760 wait_for_completion(&request
->wait_event
);
762 set_complete
= &request
->response_msg
.msg
.set_complete
;
763 if (set_complete
->status
!= RNDIS_STATUS_SUCCESS
)
767 put_rndis_request(rdev
, request
);
772 rndis_filter_set_offload_params(struct net_device
*ndev
,
773 struct netvsc_device
*nvdev
,
774 struct ndis_offload_params
*req_offloads
)
776 struct rndis_device
*rdev
= nvdev
->extension
;
777 struct rndis_request
*request
;
778 struct rndis_set_request
*set
;
779 struct ndis_offload_params
*offload_params
;
780 struct rndis_set_complete
*set_complete
;
781 u32 extlen
= sizeof(struct ndis_offload_params
);
783 u32 vsp_version
= nvdev
->nvsp_version
;
785 if (vsp_version
<= NVSP_PROTOCOL_VERSION_4
) {
786 extlen
= VERSION_4_OFFLOAD_SIZE
;
787 /* On NVSP_PROTOCOL_VERSION_4 and below, we do not support
788 * UDP checksum offload.
790 req_offloads
->udp_ip_v4_csum
= 0;
791 req_offloads
->udp_ip_v6_csum
= 0;
794 request
= get_rndis_request(rdev
, RNDIS_MSG_SET
,
795 RNDIS_MESSAGE_SIZE(struct rndis_set_request
) + extlen
);
799 set
= &request
->request_msg
.msg
.set_req
;
800 set
->oid
= OID_TCP_OFFLOAD_PARAMETERS
;
801 set
->info_buflen
= extlen
;
802 set
->info_buf_offset
= sizeof(struct rndis_set_request
);
803 set
->dev_vc_handle
= 0;
805 offload_params
= (struct ndis_offload_params
*)((ulong
)set
+
806 set
->info_buf_offset
);
807 *offload_params
= *req_offloads
;
808 offload_params
->header
.type
= NDIS_OBJECT_TYPE_DEFAULT
;
809 offload_params
->header
.revision
= NDIS_OFFLOAD_PARAMETERS_REVISION_3
;
810 offload_params
->header
.size
= extlen
;
812 ret
= rndis_filter_send_request(rdev
, request
);
816 wait_for_completion(&request
->wait_event
);
817 set_complete
= &request
->response_msg
.msg
.set_complete
;
818 if (set_complete
->status
!= RNDIS_STATUS_SUCCESS
) {
819 netdev_err(ndev
, "Fail to set offload on host side:0x%x\n",
820 set_complete
->status
);
825 put_rndis_request(rdev
, request
);
829 static int rndis_set_rss_param_msg(struct rndis_device
*rdev
,
830 const u8
*rss_key
, u16 flag
)
832 struct net_device
*ndev
= rdev
->ndev
;
833 struct net_device_context
*ndc
= netdev_priv(ndev
);
834 struct rndis_request
*request
;
835 struct rndis_set_request
*set
;
836 struct rndis_set_complete
*set_complete
;
837 u32 extlen
= sizeof(struct ndis_recv_scale_param
) +
838 4 * ITAB_NUM
+ NETVSC_HASH_KEYLEN
;
839 struct ndis_recv_scale_param
*rssp
;
844 request
= get_rndis_request(
846 RNDIS_MESSAGE_SIZE(struct rndis_set_request
) + extlen
);
850 set
= &request
->request_msg
.msg
.set_req
;
851 set
->oid
= OID_GEN_RECEIVE_SCALE_PARAMETERS
;
852 set
->info_buflen
= extlen
;
853 set
->info_buf_offset
= sizeof(struct rndis_set_request
);
854 set
->dev_vc_handle
= 0;
856 rssp
= (struct ndis_recv_scale_param
*)(set
+ 1);
857 rssp
->hdr
.type
= NDIS_OBJECT_TYPE_RSS_PARAMETERS
;
858 rssp
->hdr
.rev
= NDIS_RECEIVE_SCALE_PARAMETERS_REVISION_2
;
859 rssp
->hdr
.size
= sizeof(struct ndis_recv_scale_param
);
861 rssp
->hashinfo
= NDIS_HASH_FUNC_TOEPLITZ
| NDIS_HASH_IPV4
|
862 NDIS_HASH_TCP_IPV4
| NDIS_HASH_IPV6
|
864 rssp
->indirect_tabsize
= 4*ITAB_NUM
;
865 rssp
->indirect_taboffset
= sizeof(struct ndis_recv_scale_param
);
866 rssp
->hashkey_size
= NETVSC_HASH_KEYLEN
;
867 rssp
->hashkey_offset
= rssp
->indirect_taboffset
+
868 rssp
->indirect_tabsize
;
870 /* Set indirection table entries */
871 itab
= (u32
*)(rssp
+ 1);
872 for (i
= 0; i
< ITAB_NUM
; i
++)
873 itab
[i
] = ndc
->rx_table
[i
];
875 /* Set hask key values */
876 keyp
= (u8
*)((unsigned long)rssp
+ rssp
->hashkey_offset
);
877 memcpy(keyp
, rss_key
, NETVSC_HASH_KEYLEN
);
879 ret
= rndis_filter_send_request(rdev
, request
);
883 wait_for_completion(&request
->wait_event
);
884 set_complete
= &request
->response_msg
.msg
.set_complete
;
885 if (set_complete
->status
== RNDIS_STATUS_SUCCESS
) {
886 if (!(flag
& NDIS_RSS_PARAM_FLAG_DISABLE_RSS
) &&
887 !(flag
& NDIS_RSS_PARAM_FLAG_HASH_KEY_UNCHANGED
))
888 memcpy(rdev
->rss_key
, rss_key
, NETVSC_HASH_KEYLEN
);
891 netdev_err(ndev
, "Fail to set RSS parameters:0x%x\n",
892 set_complete
->status
);
897 put_rndis_request(rdev
, request
);
901 int rndis_filter_set_rss_param(struct rndis_device
*rdev
,
904 /* Disable RSS before change */
905 rndis_set_rss_param_msg(rdev
, rss_key
,
906 NDIS_RSS_PARAM_FLAG_DISABLE_RSS
);
908 return rndis_set_rss_param_msg(rdev
, rss_key
, 0);
911 static int rndis_filter_query_device_link_status(struct rndis_device
*dev
,
912 struct netvsc_device
*net_device
)
914 u32 size
= sizeof(u32
);
917 return rndis_filter_query_device(dev
, net_device
,
918 RNDIS_OID_GEN_MEDIA_CONNECT_STATUS
,
919 &link_status
, &size
);
922 static int rndis_filter_query_link_speed(struct rndis_device
*dev
,
923 struct netvsc_device
*net_device
)
925 u32 size
= sizeof(u32
);
927 struct net_device_context
*ndc
;
930 ret
= rndis_filter_query_device(dev
, net_device
,
931 RNDIS_OID_GEN_LINK_SPEED
,
935 ndc
= netdev_priv(dev
->ndev
);
937 /* The link speed reported from host is in 100bps unit, so
938 * we convert it to Mbps here.
940 ndc
->speed
= link_speed
/ 10000;
946 static int rndis_filter_set_packet_filter(struct rndis_device
*dev
,
949 struct rndis_request
*request
;
950 struct rndis_set_request
*set
;
953 if (dev
->filter
== new_filter
)
956 request
= get_rndis_request(dev
, RNDIS_MSG_SET
,
957 RNDIS_MESSAGE_SIZE(struct rndis_set_request
) +
962 /* Setup the rndis set */
963 set
= &request
->request_msg
.msg
.set_req
;
964 set
->oid
= RNDIS_OID_GEN_CURRENT_PACKET_FILTER
;
965 set
->info_buflen
= sizeof(u32
);
966 set
->info_buf_offset
= sizeof(struct rndis_set_request
);
968 memcpy((void *)(unsigned long)set
+ sizeof(struct rndis_set_request
),
969 &new_filter
, sizeof(u32
));
971 ret
= rndis_filter_send_request(dev
, request
);
973 wait_for_completion(&request
->wait_event
);
974 dev
->filter
= new_filter
;
977 put_rndis_request(dev
, request
);
982 static void rndis_set_multicast(struct work_struct
*w
)
984 struct rndis_device
*rdev
985 = container_of(w
, struct rndis_device
, mcast_work
);
986 u32 filter
= NDIS_PACKET_TYPE_DIRECTED
;
987 unsigned int flags
= rdev
->ndev
->flags
;
989 if (flags
& IFF_PROMISC
) {
990 filter
= NDIS_PACKET_TYPE_PROMISCUOUS
;
992 if (!netdev_mc_empty(rdev
->ndev
) || (flags
& IFF_ALLMULTI
))
993 filter
|= NDIS_PACKET_TYPE_ALL_MULTICAST
;
994 if (flags
& IFF_BROADCAST
)
995 filter
|= NDIS_PACKET_TYPE_BROADCAST
;
998 rndis_filter_set_packet_filter(rdev
, filter
);
1001 void rndis_filter_update(struct netvsc_device
*nvdev
)
1003 struct rndis_device
*rdev
= nvdev
->extension
;
1005 schedule_work(&rdev
->mcast_work
);
1008 static int rndis_filter_init_device(struct rndis_device
*dev
,
1009 struct netvsc_device
*nvdev
)
1011 struct rndis_request
*request
;
1012 struct rndis_initialize_request
*init
;
1013 struct rndis_initialize_complete
*init_complete
;
1017 request
= get_rndis_request(dev
, RNDIS_MSG_INIT
,
1018 RNDIS_MESSAGE_SIZE(struct rndis_initialize_request
));
1024 /* Setup the rndis set */
1025 init
= &request
->request_msg
.msg
.init_req
;
1026 init
->major_ver
= RNDIS_MAJOR_VERSION
;
1027 init
->minor_ver
= RNDIS_MINOR_VERSION
;
1028 init
->max_xfer_size
= 0x4000;
1030 dev
->state
= RNDIS_DEV_INITIALIZING
;
1032 ret
= rndis_filter_send_request(dev
, request
);
1034 dev
->state
= RNDIS_DEV_UNINITIALIZED
;
1038 wait_for_completion(&request
->wait_event
);
1040 init_complete
= &request
->response_msg
.msg
.init_complete
;
1041 status
= init_complete
->status
;
1042 if (status
== RNDIS_STATUS_SUCCESS
) {
1043 dev
->state
= RNDIS_DEV_INITIALIZED
;
1044 nvdev
->max_pkt
= init_complete
->max_pkt_per_msg
;
1045 nvdev
->pkt_align
= 1 << init_complete
->pkt_alignment_factor
;
1048 dev
->state
= RNDIS_DEV_UNINITIALIZED
;
1054 put_rndis_request(dev
, request
);
1059 static bool netvsc_device_idle(const struct netvsc_device
*nvdev
)
1063 for (i
= 0; i
< nvdev
->num_chn
; i
++) {
1064 const struct netvsc_channel
*nvchan
= &nvdev
->chan_table
[i
];
1066 if (nvchan
->mrc
.first
!= nvchan
->mrc
.next
)
1069 if (atomic_read(&nvchan
->queue_sends
) > 0)
1076 static void rndis_filter_halt_device(struct netvsc_device
*nvdev
,
1077 struct rndis_device
*dev
)
1079 struct rndis_request
*request
;
1080 struct rndis_halt_request
*halt
;
1082 /* Attempt to do a rndis device halt */
1083 request
= get_rndis_request(dev
, RNDIS_MSG_HALT
,
1084 RNDIS_MESSAGE_SIZE(struct rndis_halt_request
));
1088 /* Setup the rndis set */
1089 halt
= &request
->request_msg
.msg
.halt_req
;
1090 halt
->req_id
= atomic_inc_return(&dev
->new_req_id
);
1092 /* Ignore return since this msg is optional. */
1093 rndis_filter_send_request(dev
, request
);
1095 dev
->state
= RNDIS_DEV_UNINITIALIZED
;
1098 nvdev
->destroy
= true;
1100 /* Force flag to be ordered before waiting */
1103 /* Wait for all send completions */
1104 wait_event(nvdev
->wait_drain
, netvsc_device_idle(nvdev
));
1107 put_rndis_request(dev
, request
);
1110 static int rndis_filter_open_device(struct rndis_device
*dev
)
1114 if (dev
->state
!= RNDIS_DEV_INITIALIZED
)
1117 ret
= rndis_filter_set_packet_filter(dev
,
1118 NDIS_PACKET_TYPE_BROADCAST
|
1119 NDIS_PACKET_TYPE_ALL_MULTICAST
|
1120 NDIS_PACKET_TYPE_DIRECTED
);
1122 dev
->state
= RNDIS_DEV_DATAINITIALIZED
;
1127 static int rndis_filter_close_device(struct rndis_device
*dev
)
1131 if (dev
->state
!= RNDIS_DEV_DATAINITIALIZED
)
1134 /* Make sure rndis_set_multicast doesn't re-enable filter! */
1135 cancel_work_sync(&dev
->mcast_work
);
1137 ret
= rndis_filter_set_packet_filter(dev
, 0);
1142 dev
->state
= RNDIS_DEV_INITIALIZED
;
1147 static void netvsc_sc_open(struct vmbus_channel
*new_sc
)
1149 struct net_device
*ndev
=
1150 hv_get_drvdata(new_sc
->primary_channel
->device_obj
);
1151 struct net_device_context
*ndev_ctx
= netdev_priv(ndev
);
1152 struct netvsc_device
*nvscdev
;
1153 u16 chn_index
= new_sc
->offermsg
.offer
.sub_channel_index
;
1154 struct netvsc_channel
*nvchan
;
1157 /* This is safe because this callback only happens when
1158 * new device is being setup and waiting on the channel_init_wait.
1160 nvscdev
= rcu_dereference_raw(ndev_ctx
->nvdev
);
1161 if (!nvscdev
|| chn_index
>= nvscdev
->num_chn
)
1164 nvchan
= nvscdev
->chan_table
+ chn_index
;
1166 /* Because the device uses NAPI, all the interrupt batching and
1167 * control is done via Net softirq, not the channel handling
1169 set_channel_read_mode(new_sc
, HV_CALL_ISR
);
1171 /* Set the channel before opening.*/
1172 nvchan
->channel
= new_sc
;
1174 new_sc
->rqstor_size
= netvsc_rqstor_size(netvsc_ring_bytes
);
1175 ret
= vmbus_open(new_sc
, netvsc_ring_bytes
,
1176 netvsc_ring_bytes
, NULL
, 0,
1177 netvsc_channel_cb
, nvchan
);
1179 napi_enable(&nvchan
->napi
);
1181 netdev_notice(ndev
, "sub channel open failed: %d\n", ret
);
1183 if (atomic_inc_return(&nvscdev
->open_chn
) == nvscdev
->num_chn
)
1184 wake_up(&nvscdev
->subchan_open
);
1187 /* Open sub-channels after completing the handling of the device probe.
1188 * This breaks overlap of processing the host message for the
1189 * new primary channel with the initialization of sub-channels.
1191 int rndis_set_subchannel(struct net_device
*ndev
,
1192 struct netvsc_device
*nvdev
,
1193 struct netvsc_device_info
*dev_info
)
1195 struct nvsp_message
*init_packet
= &nvdev
->channel_init_pkt
;
1196 struct net_device_context
*ndev_ctx
= netdev_priv(ndev
);
1197 struct hv_device
*hv_dev
= ndev_ctx
->device_ctx
;
1198 struct rndis_device
*rdev
= nvdev
->extension
;
1203 memset(init_packet
, 0, sizeof(struct nvsp_message
));
1204 init_packet
->hdr
.msg_type
= NVSP_MSG5_TYPE_SUBCHANNEL
;
1205 init_packet
->msg
.v5_msg
.subchn_req
.op
= NVSP_SUBCHANNEL_ALLOCATE
;
1206 init_packet
->msg
.v5_msg
.subchn_req
.num_subchannels
=
1208 trace_nvsp_send(ndev
, init_packet
);
1210 ret
= vmbus_sendpacket(hv_dev
->channel
, init_packet
,
1211 sizeof(struct nvsp_message
),
1212 (unsigned long)init_packet
,
1214 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED
);
1216 netdev_err(ndev
, "sub channel allocate send failed: %d\n", ret
);
1220 wait_for_completion(&nvdev
->channel_init_wait
);
1221 if (init_packet
->msg
.v5_msg
.subchn_comp
.status
!= NVSP_STAT_SUCCESS
) {
1222 netdev_err(ndev
, "sub channel request failed\n");
1226 /* Check that number of allocated sub channel is within the expected range */
1227 if (init_packet
->msg
.v5_msg
.subchn_comp
.num_subchannels
> nvdev
->num_chn
- 1) {
1228 netdev_err(ndev
, "invalid number of allocated sub channel\n");
1231 nvdev
->num_chn
= 1 +
1232 init_packet
->msg
.v5_msg
.subchn_comp
.num_subchannels
;
1234 /* wait for all sub channels to open */
1235 wait_event(nvdev
->subchan_open
,
1236 atomic_read(&nvdev
->open_chn
) == nvdev
->num_chn
);
1238 for (i
= 0; i
< VRSS_SEND_TAB_SIZE
; i
++)
1239 ndev_ctx
->tx_table
[i
] = i
% nvdev
->num_chn
;
1241 /* ignore failures from setting rss parameters, still have channels */
1243 rndis_filter_set_rss_param(rdev
, dev_info
->rss_key
);
1245 rndis_filter_set_rss_param(rdev
, netvsc_hash_key
);
1247 netif_set_real_num_tx_queues(ndev
, nvdev
->num_chn
);
1248 netif_set_real_num_rx_queues(ndev
, nvdev
->num_chn
);
1253 static int rndis_netdev_set_hwcaps(struct rndis_device
*rndis_device
,
1254 struct netvsc_device
*nvdev
)
1256 struct net_device
*net
= rndis_device
->ndev
;
1257 struct net_device_context
*net_device_ctx
= netdev_priv(net
);
1258 struct ndis_offload hwcaps
;
1259 struct ndis_offload_params offloads
;
1260 unsigned int gso_max_size
= GSO_MAX_SIZE
;
1263 /* Find HW offload capabilities */
1264 ret
= rndis_query_hwcaps(rndis_device
, nvdev
, &hwcaps
);
1268 /* A value of zero means "no change"; now turn on what we want. */
1269 memset(&offloads
, 0, sizeof(struct ndis_offload_params
));
1271 /* Linux does not care about IP checksum, always does in kernel */
1272 offloads
.ip_v4_csum
= NDIS_OFFLOAD_PARAMETERS_TX_RX_DISABLED
;
1274 /* Reset previously set hw_features flags */
1275 net
->hw_features
&= ~NETVSC_SUPPORTED_HW_FEATURES
;
1276 net_device_ctx
->tx_checksum_mask
= 0;
1278 /* Compute tx offload settings based on hw capabilities */
1279 net
->hw_features
|= NETIF_F_RXCSUM
;
1280 net
->hw_features
|= NETIF_F_SG
;
1281 net
->hw_features
|= NETIF_F_RXHASH
;
1283 if ((hwcaps
.csum
.ip4_txcsum
& NDIS_TXCSUM_ALL_TCP4
) == NDIS_TXCSUM_ALL_TCP4
) {
1284 /* Can checksum TCP */
1285 net
->hw_features
|= NETIF_F_IP_CSUM
;
1286 net_device_ctx
->tx_checksum_mask
|= TRANSPORT_INFO_IPV4_TCP
;
1288 offloads
.tcp_ip_v4_csum
= NDIS_OFFLOAD_PARAMETERS_TX_RX_ENABLED
;
1290 if (hwcaps
.lsov2
.ip4_encap
& NDIS_OFFLOAD_ENCAP_8023
) {
1291 offloads
.lso_v2_ipv4
= NDIS_OFFLOAD_PARAMETERS_LSOV2_ENABLED
;
1292 net
->hw_features
|= NETIF_F_TSO
;
1294 if (hwcaps
.lsov2
.ip4_maxsz
< gso_max_size
)
1295 gso_max_size
= hwcaps
.lsov2
.ip4_maxsz
;
1298 if (hwcaps
.csum
.ip4_txcsum
& NDIS_TXCSUM_CAP_UDP4
) {
1299 offloads
.udp_ip_v4_csum
= NDIS_OFFLOAD_PARAMETERS_TX_RX_ENABLED
;
1300 net_device_ctx
->tx_checksum_mask
|= TRANSPORT_INFO_IPV4_UDP
;
1304 if ((hwcaps
.csum
.ip6_txcsum
& NDIS_TXCSUM_ALL_TCP6
) == NDIS_TXCSUM_ALL_TCP6
) {
1305 net
->hw_features
|= NETIF_F_IPV6_CSUM
;
1307 offloads
.tcp_ip_v6_csum
= NDIS_OFFLOAD_PARAMETERS_TX_RX_ENABLED
;
1308 net_device_ctx
->tx_checksum_mask
|= TRANSPORT_INFO_IPV6_TCP
;
1310 if ((hwcaps
.lsov2
.ip6_encap
& NDIS_OFFLOAD_ENCAP_8023
) &&
1311 (hwcaps
.lsov2
.ip6_opts
& NDIS_LSOV2_CAP_IP6
) == NDIS_LSOV2_CAP_IP6
) {
1312 offloads
.lso_v2_ipv6
= NDIS_OFFLOAD_PARAMETERS_LSOV2_ENABLED
;
1313 net
->hw_features
|= NETIF_F_TSO6
;
1315 if (hwcaps
.lsov2
.ip6_maxsz
< gso_max_size
)
1316 gso_max_size
= hwcaps
.lsov2
.ip6_maxsz
;
1319 if (hwcaps
.csum
.ip6_txcsum
& NDIS_TXCSUM_CAP_UDP6
) {
1320 offloads
.udp_ip_v6_csum
= NDIS_OFFLOAD_PARAMETERS_TX_RX_ENABLED
;
1321 net_device_ctx
->tx_checksum_mask
|= TRANSPORT_INFO_IPV6_UDP
;
1325 if (hwcaps
.rsc
.ip4
&& hwcaps
.rsc
.ip6
) {
1326 net
->hw_features
|= NETIF_F_LRO
;
1328 if (net
->features
& NETIF_F_LRO
) {
1329 offloads
.rsc_ip_v4
= NDIS_OFFLOAD_PARAMETERS_RSC_ENABLED
;
1330 offloads
.rsc_ip_v6
= NDIS_OFFLOAD_PARAMETERS_RSC_ENABLED
;
1332 offloads
.rsc_ip_v4
= NDIS_OFFLOAD_PARAMETERS_RSC_DISABLED
;
1333 offloads
.rsc_ip_v6
= NDIS_OFFLOAD_PARAMETERS_RSC_DISABLED
;
1337 /* In case some hw_features disappeared we need to remove them from
1338 * net->features list as they're no longer supported.
1340 net
->features
&= ~NETVSC_SUPPORTED_HW_FEATURES
| net
->hw_features
;
1342 netif_set_gso_max_size(net
, gso_max_size
);
1344 ret
= rndis_filter_set_offload_params(net
, nvdev
, &offloads
);
1349 static void rndis_get_friendly_name(struct net_device
*net
,
1350 struct rndis_device
*rndis_device
,
1351 struct netvsc_device
*net_device
)
1353 ucs2_char_t wname
[256];
1358 size
= sizeof(wname
);
1359 if (rndis_filter_query_device(rndis_device
, net_device
,
1360 RNDIS_OID_GEN_FRIENDLY_NAME
,
1362 return; /* ignore if host does not support */
1365 return; /* name not set */
1367 /* Convert Windows Unicode string to UTF-8 */
1368 len
= ucs2_as_utf8(ifalias
, wname
, sizeof(ifalias
));
1370 /* ignore the default value from host */
1371 if (strcmp(ifalias
, "Network Adapter") != 0)
1372 dev_set_alias(net
, ifalias
, len
);
1375 struct netvsc_device
*rndis_filter_device_add(struct hv_device
*dev
,
1376 struct netvsc_device_info
*device_info
)
1378 struct net_device
*net
= hv_get_drvdata(dev
);
1379 struct net_device_context
*ndc
= netdev_priv(net
);
1380 struct netvsc_device
*net_device
;
1381 struct rndis_device
*rndis_device
;
1382 struct ndis_recv_scale_cap rsscap
;
1383 u32 rsscap_size
= sizeof(struct ndis_recv_scale_cap
);
1385 u32 num_possible_rss_qs
;
1388 rndis_device
= get_rndis_device();
1390 return ERR_PTR(-ENODEV
);
1392 /* Let the inner driver handle this first to create the netvsc channel
1393 * NOTE! Once the channel is created, we may get a receive callback
1394 * (RndisFilterOnReceive()) before this call is completed
1396 net_device
= netvsc_device_add(dev
, device_info
);
1397 if (IS_ERR(net_device
)) {
1398 kfree(rndis_device
);
1402 /* Initialize the rndis device */
1403 net_device
->max_chn
= 1;
1404 net_device
->num_chn
= 1;
1406 net_device
->extension
= rndis_device
;
1407 rndis_device
->ndev
= net
;
1409 /* Send the rndis initialization message */
1410 ret
= rndis_filter_init_device(rndis_device
, net_device
);
1414 /* Get the MTU from the host */
1416 ret
= rndis_filter_query_device(rndis_device
, net_device
,
1417 RNDIS_OID_GEN_MAXIMUM_FRAME_SIZE
,
1419 if (ret
== 0 && size
== sizeof(u32
) && mtu
< net
->mtu
)
1422 /* Get the mac address */
1423 ret
= rndis_filter_query_device_mac(rndis_device
, net_device
);
1427 memcpy(device_info
->mac_adr
, rndis_device
->hw_mac_adr
, ETH_ALEN
);
1429 /* Get friendly name as ifalias*/
1431 rndis_get_friendly_name(net
, rndis_device
, net_device
);
1433 /* Query and set hardware capabilities */
1434 ret
= rndis_netdev_set_hwcaps(rndis_device
, net_device
);
1438 rndis_filter_query_device_link_status(rndis_device
, net_device
);
1440 netdev_dbg(net
, "Device MAC %pM link state %s\n",
1441 rndis_device
->hw_mac_adr
,
1442 rndis_device
->link_state
? "down" : "up");
1444 if (net_device
->nvsp_version
< NVSP_PROTOCOL_VERSION_5
)
1447 rndis_filter_query_link_speed(rndis_device
, net_device
);
1450 memset(&rsscap
, 0, rsscap_size
);
1451 ret
= rndis_filter_query_device(rndis_device
, net_device
,
1452 OID_GEN_RECEIVE_SCALE_CAPABILITIES
,
1453 &rsscap
, &rsscap_size
);
1454 if (ret
|| rsscap
.num_recv_que
< 2)
1457 /* This guarantees that num_possible_rss_qs <= num_online_cpus */
1458 num_possible_rss_qs
= min_t(u32
, num_online_cpus(),
1459 rsscap
.num_recv_que
);
1461 net_device
->max_chn
= min_t(u32
, VRSS_CHANNEL_MAX
, num_possible_rss_qs
);
1463 /* We will use the given number of channels if available. */
1464 net_device
->num_chn
= min(net_device
->max_chn
, device_info
->num_chn
);
1466 if (!netif_is_rxfh_configured(net
)) {
1467 for (i
= 0; i
< ITAB_NUM
; i
++)
1468 ndc
->rx_table
[i
] = ethtool_rxfh_indir_default(
1469 i
, net_device
->num_chn
);
1472 atomic_set(&net_device
->open_chn
, 1);
1473 vmbus_set_sc_create_callback(dev
->channel
, netvsc_sc_open
);
1475 for (i
= 1; i
< net_device
->num_chn
; i
++) {
1476 ret
= netvsc_alloc_recv_comp_ring(net_device
, i
);
1479 vfree(net_device
->chan_table
[i
].mrc
.slots
);
1484 for (i
= 1; i
< net_device
->num_chn
; i
++)
1485 netif_napi_add(net
, &net_device
->chan_table
[i
].napi
,
1486 netvsc_poll
, NAPI_POLL_WEIGHT
);
1491 /* setting up multiple channels failed */
1492 net_device
->max_chn
= 1;
1493 net_device
->num_chn
= 1;
1497 rndis_filter_device_remove(dev
, net_device
);
1498 return ERR_PTR(ret
);
1501 void rndis_filter_device_remove(struct hv_device
*dev
,
1502 struct netvsc_device
*net_dev
)
1504 struct rndis_device
*rndis_dev
= net_dev
->extension
;
1506 /* Halt and release the rndis device */
1507 rndis_filter_halt_device(net_dev
, rndis_dev
);
1509 netvsc_device_remove(dev
);
1512 int rndis_filter_open(struct netvsc_device
*nvdev
)
1517 return rndis_filter_open_device(nvdev
->extension
);
1520 int rndis_filter_close(struct netvsc_device
*nvdev
)
1525 return rndis_filter_close_device(nvdev
->extension
);