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/kernel.h>
10 #include <linux/sched.h>
11 #include <linux/wait.h>
12 #include <linux/highmem.h>
13 #include <linux/slab.h>
15 #include <linux/if_ether.h>
16 #include <linux/netdevice.h>
17 #include <linux/if_vlan.h>
18 #include <linux/nls.h>
19 #include <linux/vmalloc.h>
20 #include <linux/rtnetlink.h>
21 #include <linux/ucs2_string.h>
23 #include "hyperv_net.h"
24 #include "netvsc_trace.h"
26 static void rndis_set_multicast(struct work_struct *w);
28 #define RNDIS_EXT_LEN PAGE_SIZE
29 struct rndis_request {
30 struct list_head list_ent;
31 struct completion wait_event;
33 struct rndis_message response_msg;
35 * The buffer for extended info after the RNDIS response message. It's
36 * referenced based on the data offset in the RNDIS message. Its size
37 * is enough for current needs, and should be sufficient for the near
40 u8 response_ext[RNDIS_EXT_LEN];
42 /* Simplify allocation by having a netvsc packet inline */
43 struct hv_netvsc_packet pkt;
45 struct rndis_message request_msg;
47 * The buffer for the extended info after the RNDIS request message.
48 * It is referenced and sized in a similar way as response_ext.
50 u8 request_ext[RNDIS_EXT_LEN];
53 static const u8 netvsc_hash_key[NETVSC_HASH_KEYLEN] = {
54 0x6d, 0x5a, 0x56, 0xda, 0x25, 0x5b, 0x0e, 0xc2,
55 0x41, 0x67, 0x25, 0x3d, 0x43, 0xa3, 0x8f, 0xb0,
56 0xd0, 0xca, 0x2b, 0xcb, 0xae, 0x7b, 0x30, 0xb4,
57 0x77, 0xcb, 0x2d, 0xa3, 0x80, 0x30, 0xf2, 0x0c,
58 0x6a, 0x42, 0xb7, 0x3b, 0xbe, 0xac, 0x01, 0xfa
61 static struct rndis_device *get_rndis_device(void)
63 struct rndis_device *device;
65 device = kzalloc(sizeof(struct rndis_device), GFP_KERNEL);
69 spin_lock_init(&device->request_lock);
71 INIT_LIST_HEAD(&device->req_list);
72 INIT_WORK(&device->mcast_work, rndis_set_multicast);
74 device->state = RNDIS_DEV_UNINITIALIZED;
79 static struct rndis_request *get_rndis_request(struct rndis_device *dev,
83 struct rndis_request *request;
84 struct rndis_message *rndis_msg;
85 struct rndis_set_request *set;
88 request = kzalloc(sizeof(struct rndis_request), GFP_KERNEL);
92 init_completion(&request->wait_event);
94 rndis_msg = &request->request_msg;
95 rndis_msg->ndis_msg_type = msg_type;
96 rndis_msg->msg_len = msg_len;
98 request->pkt.q_idx = 0;
101 * Set the request id. This field is always after the rndis header for
102 * request/response packet types so we just used the SetRequest as a
105 set = &rndis_msg->msg.set_req;
106 set->req_id = atomic_inc_return(&dev->new_req_id);
108 /* Add to the request list */
109 spin_lock_irqsave(&dev->request_lock, flags);
110 list_add_tail(&request->list_ent, &dev->req_list);
111 spin_unlock_irqrestore(&dev->request_lock, flags);
116 static void put_rndis_request(struct rndis_device *dev,
117 struct rndis_request *req)
121 spin_lock_irqsave(&dev->request_lock, flags);
122 list_del(&req->list_ent);
123 spin_unlock_irqrestore(&dev->request_lock, flags);
128 static void dump_rndis_message(struct net_device *netdev,
129 const struct rndis_message *rndis_msg)
131 switch (rndis_msg->ndis_msg_type) {
132 case RNDIS_MSG_PACKET:
133 netdev_dbg(netdev, "RNDIS_MSG_PACKET (len %u, "
134 "data offset %u data len %u, # oob %u, "
135 "oob offset %u, oob len %u, pkt offset %u, "
138 rndis_msg->msg.pkt.data_offset,
139 rndis_msg->msg.pkt.data_len,
140 rndis_msg->msg.pkt.num_oob_data_elements,
141 rndis_msg->msg.pkt.oob_data_offset,
142 rndis_msg->msg.pkt.oob_data_len,
143 rndis_msg->msg.pkt.per_pkt_info_offset,
144 rndis_msg->msg.pkt.per_pkt_info_len);
147 case RNDIS_MSG_INIT_C:
148 netdev_dbg(netdev, "RNDIS_MSG_INIT_C "
149 "(len %u, id 0x%x, status 0x%x, major %d, minor %d, "
150 "device flags %d, max xfer size 0x%x, max pkts %u, "
153 rndis_msg->msg.init_complete.req_id,
154 rndis_msg->msg.init_complete.status,
155 rndis_msg->msg.init_complete.major_ver,
156 rndis_msg->msg.init_complete.minor_ver,
157 rndis_msg->msg.init_complete.dev_flags,
158 rndis_msg->msg.init_complete.max_xfer_size,
159 rndis_msg->msg.init_complete.
161 rndis_msg->msg.init_complete.
162 pkt_alignment_factor);
165 case RNDIS_MSG_QUERY_C:
166 netdev_dbg(netdev, "RNDIS_MSG_QUERY_C "
167 "(len %u, id 0x%x, status 0x%x, buf len %u, "
170 rndis_msg->msg.query_complete.req_id,
171 rndis_msg->msg.query_complete.status,
172 rndis_msg->msg.query_complete.
174 rndis_msg->msg.query_complete.
178 case RNDIS_MSG_SET_C:
180 "RNDIS_MSG_SET_C (len %u, id 0x%x, status 0x%x)\n",
182 rndis_msg->msg.set_complete.req_id,
183 rndis_msg->msg.set_complete.status);
186 case RNDIS_MSG_INDICATE:
187 netdev_dbg(netdev, "RNDIS_MSG_INDICATE "
188 "(len %u, status 0x%x, buf len %u, buf offset %u)\n",
190 rndis_msg->msg.indicate_status.status,
191 rndis_msg->msg.indicate_status.status_buflen,
192 rndis_msg->msg.indicate_status.status_buf_offset);
196 netdev_dbg(netdev, "0x%x (len %u)\n",
197 rndis_msg->ndis_msg_type,
203 static int rndis_filter_send_request(struct rndis_device *dev,
204 struct rndis_request *req)
206 struct hv_netvsc_packet *packet;
207 struct hv_page_buffer page_buf[2];
208 struct hv_page_buffer *pb = page_buf;
211 /* Setup the packet to send it */
214 packet->total_data_buflen = req->request_msg.msg_len;
215 packet->page_buf_cnt = 1;
217 pb[0].pfn = virt_to_phys(&req->request_msg) >>
219 pb[0].len = req->request_msg.msg_len;
221 (unsigned long)&req->request_msg & (PAGE_SIZE - 1);
223 /* Add one page_buf when request_msg crossing page boundary */
224 if (pb[0].offset + pb[0].len > PAGE_SIZE) {
225 packet->page_buf_cnt++;
226 pb[0].len = PAGE_SIZE -
228 pb[1].pfn = virt_to_phys((void *)&req->request_msg
229 + pb[0].len) >> 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 spin_lock_irqsave(&dev->request_lock, flags);
279 list_for_each_entry(request, &dev->req_list, list_ent) {
281 * All request/response message contains RequestId as the 1st
284 if (request->request_msg.msg.init_req.req_id
285 == resp->msg.init_complete.req_id) {
290 spin_unlock_irqrestore(&dev->request_lock, flags);
294 sizeof(struct rndis_message) + RNDIS_EXT_LEN) {
295 memcpy(&request->response_msg, resp,
297 if (request->request_msg.ndis_msg_type ==
298 RNDIS_MSG_QUERY && request->request_msg.msg.
299 query_req.oid == RNDIS_OID_GEN_MEDIA_CONNECT_STATUS)
300 rndis_set_link_state(dev, request);
303 "rndis response buffer overflow "
304 "detected (size %u max %zu)\n",
306 sizeof(struct rndis_message));
308 if (resp->ndis_msg_type ==
310 /* does not have a request id field */
311 request->response_msg.msg.reset_complete.
312 status = RNDIS_STATUS_BUFFER_OVERFLOW;
314 request->response_msg.msg.
315 init_complete.status =
316 RNDIS_STATUS_BUFFER_OVERFLOW;
320 complete(&request->wait_event);
323 "no rndis request found for this response "
324 "(id 0x%x res type 0x%x)\n",
325 resp->msg.init_complete.req_id,
326 resp->ndis_msg_type);
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,
335 u32 type, u8 internal)
337 struct rndis_per_packet_info *ppi;
340 if (rpkt->per_pkt_info_offset == 0)
343 ppi = (struct rndis_per_packet_info *)((ulong)rpkt +
344 rpkt->per_pkt_info_offset);
345 len = rpkt->per_pkt_info_len;
348 if (ppi->type == type && ppi->internal == internal)
349 return (void *)((ulong)ppi + ppi->ppi_offset);
351 ppi = (struct rndis_per_packet_info *)((ulong)ppi + ppi->size);
358 void rsc_add_data(struct netvsc_channel *nvchan,
359 const struct ndis_pkt_8021q_info *vlan,
360 const struct ndis_tcp_ip_checksum_info *csum_info,
361 const u32 *hash_info,
364 u32 cnt = nvchan->rsc.cnt;
367 nvchan->rsc.pktlen += len;
369 nvchan->rsc.vlan = vlan;
370 nvchan->rsc.csum_info = csum_info;
371 nvchan->rsc.pktlen = len;
372 nvchan->rsc.hash_info = hash_info;
375 nvchan->rsc.data[cnt] = data;
376 nvchan->rsc.len[cnt] = len;
380 static int rndis_filter_receive_data(struct net_device *ndev,
381 struct netvsc_device *nvdev,
382 struct netvsc_channel *nvchan,
383 struct rndis_message *msg,
386 struct rndis_packet *rndis_pkt = &msg->msg.pkt;
387 const struct ndis_tcp_ip_checksum_info *csum_info;
388 const struct ndis_pkt_8021q_info *vlan;
389 const struct rndis_pktinfo_id *pktinfo_id;
390 const u32 *hash_info;
393 bool rsc_more = false;
396 /* Remove the rndis header and pass it back up the stack */
397 data_offset = RNDIS_HEADER_SIZE + rndis_pkt->data_offset;
399 data_buflen -= data_offset;
402 * Make sure we got a valid RNDIS message, now total_data_buflen
403 * should be the data packet size plus the trailer padding size
405 if (unlikely(data_buflen < rndis_pkt->data_len)) {
406 netdev_err(ndev, "rndis message buffer "
407 "overflow detected (got %u, min %u)"
408 "...dropping this message!\n",
409 data_buflen, rndis_pkt->data_len);
410 return NVSP_STAT_FAIL;
413 vlan = rndis_get_ppi(rndis_pkt, IEEE_8021Q_INFO, 0);
415 csum_info = rndis_get_ppi(rndis_pkt, TCPIP_CHKSUM_PKTINFO, 0);
417 hash_info = rndis_get_ppi(rndis_pkt, NBL_HASH_VALUE, 0);
419 pktinfo_id = rndis_get_ppi(rndis_pkt, RNDIS_PKTINFO_ID, 1);
421 data = (void *)msg + data_offset;
423 /* Identify RSC frags, drop erroneous packets */
424 if (pktinfo_id && (pktinfo_id->flag & RNDIS_PKTINFO_SUBALLOC)) {
425 if (pktinfo_id->flag & RNDIS_PKTINFO_1ST_FRAG)
427 else if (nvchan->rsc.cnt == 0)
432 if (pktinfo_id->flag & RNDIS_PKTINFO_LAST_FRAG)
435 if (rsc_more && nvchan->rsc.is_last)
441 if (unlikely(nvchan->rsc.cnt >= NVSP_RSC_MAX))
444 /* Put data into per channel structure.
445 * Also, remove the rndis trailer padding from rndis packet message
446 * rndis_pkt->data_len tell us the real data length, we only copy
447 * the data packet to the stack, without the rndis trailer padding
449 rsc_add_data(nvchan, vlan, csum_info, hash_info,
450 data, rndis_pkt->data_len);
453 return NVSP_STAT_SUCCESS;
455 ret = netvsc_recv_callback(ndev, nvdev, nvchan);
461 /* Drop incomplete packet */
463 return NVSP_STAT_FAIL;
466 int rndis_filter_receive(struct net_device *ndev,
467 struct netvsc_device *net_dev,
468 struct netvsc_channel *nvchan,
469 void *data, u32 buflen)
471 struct net_device_context *net_device_ctx = netdev_priv(ndev);
472 struct rndis_message *rndis_msg = data;
474 if (netif_msg_rx_status(net_device_ctx))
475 dump_rndis_message(ndev, rndis_msg);
477 switch (rndis_msg->ndis_msg_type) {
478 case RNDIS_MSG_PACKET:
479 return rndis_filter_receive_data(ndev, net_dev, nvchan,
481 case RNDIS_MSG_INIT_C:
482 case RNDIS_MSG_QUERY_C:
483 case RNDIS_MSG_SET_C:
484 /* completion msgs */
485 rndis_filter_receive_response(ndev, net_dev, rndis_msg);
488 case RNDIS_MSG_INDICATE:
489 /* notification msgs */
490 netvsc_linkstatus_callback(ndev, rndis_msg);
494 "unhandled rndis message (type %u len %u)\n",
495 rndis_msg->ndis_msg_type,
497 return NVSP_STAT_FAIL;
500 return NVSP_STAT_SUCCESS;
503 static int rndis_filter_query_device(struct rndis_device *dev,
504 struct netvsc_device *nvdev,
505 u32 oid, void *result, u32 *result_size)
507 struct rndis_request *request;
508 u32 inresult_size = *result_size;
509 struct rndis_query_request *query;
510 struct rndis_query_complete *query_complete;
517 request = get_rndis_request(dev, RNDIS_MSG_QUERY,
518 RNDIS_MESSAGE_SIZE(struct rndis_query_request));
524 /* Setup the rndis query */
525 query = &request->request_msg.msg.query_req;
527 query->info_buf_offset = sizeof(struct rndis_query_request);
528 query->info_buflen = 0;
529 query->dev_vc_handle = 0;
531 if (oid == OID_TCP_OFFLOAD_HARDWARE_CAPABILITIES) {
532 struct ndis_offload *hwcaps;
533 u32 nvsp_version = nvdev->nvsp_version;
537 if (nvsp_version >= NVSP_PROTOCOL_VERSION_5) {
538 ndis_rev = NDIS_OFFLOAD_PARAMETERS_REVISION_3;
539 size = NDIS_OFFLOAD_SIZE;
540 } else if (nvsp_version >= NVSP_PROTOCOL_VERSION_4) {
541 ndis_rev = NDIS_OFFLOAD_PARAMETERS_REVISION_2;
542 size = NDIS_OFFLOAD_SIZE_6_1;
544 ndis_rev = NDIS_OFFLOAD_PARAMETERS_REVISION_1;
545 size = NDIS_OFFLOAD_SIZE_6_0;
548 request->request_msg.msg_len += size;
549 query->info_buflen = size;
550 hwcaps = (struct ndis_offload *)
551 ((unsigned long)query + query->info_buf_offset);
553 hwcaps->header.type = NDIS_OBJECT_TYPE_OFFLOAD;
554 hwcaps->header.revision = ndis_rev;
555 hwcaps->header.size = size;
557 } else if (oid == OID_GEN_RECEIVE_SCALE_CAPABILITIES) {
558 struct ndis_recv_scale_cap *cap;
560 request->request_msg.msg_len +=
561 sizeof(struct ndis_recv_scale_cap);
562 query->info_buflen = sizeof(struct ndis_recv_scale_cap);
563 cap = (struct ndis_recv_scale_cap *)((unsigned long)query +
564 query->info_buf_offset);
565 cap->hdr.type = NDIS_OBJECT_TYPE_RSS_CAPABILITIES;
566 cap->hdr.rev = NDIS_RECEIVE_SCALE_CAPABILITIES_REVISION_2;
567 cap->hdr.size = sizeof(struct ndis_recv_scale_cap);
570 ret = rndis_filter_send_request(dev, request);
574 wait_for_completion(&request->wait_event);
576 /* Copy the response back */
577 query_complete = &request->response_msg.msg.query_complete;
579 if (query_complete->info_buflen > inresult_size) {
585 (void *)((unsigned long)query_complete +
586 query_complete->info_buf_offset),
587 query_complete->info_buflen);
589 *result_size = query_complete->info_buflen;
593 put_rndis_request(dev, request);
598 /* Get the hardware offload capabilities */
600 rndis_query_hwcaps(struct rndis_device *dev, struct netvsc_device *net_device,
601 struct ndis_offload *caps)
603 u32 caps_len = sizeof(*caps);
606 memset(caps, 0, sizeof(*caps));
608 ret = rndis_filter_query_device(dev, net_device,
609 OID_TCP_OFFLOAD_HARDWARE_CAPABILITIES,
614 if (caps->header.type != NDIS_OBJECT_TYPE_OFFLOAD) {
615 netdev_warn(dev->ndev, "invalid NDIS objtype %#x\n",
620 if (caps->header.revision < NDIS_OFFLOAD_PARAMETERS_REVISION_1) {
621 netdev_warn(dev->ndev, "invalid NDIS objrev %x\n",
622 caps->header.revision);
626 if (caps->header.size > caps_len ||
627 caps->header.size < NDIS_OFFLOAD_SIZE_6_0) {
628 netdev_warn(dev->ndev,
629 "invalid NDIS objsize %u, data size %u\n",
630 caps->header.size, caps_len);
637 static int rndis_filter_query_device_mac(struct rndis_device *dev,
638 struct netvsc_device *net_device)
642 return rndis_filter_query_device(dev, net_device,
643 RNDIS_OID_802_3_PERMANENT_ADDRESS,
644 dev->hw_mac_adr, &size);
647 #define NWADR_STR "NetworkAddress"
648 #define NWADR_STRLEN 14
650 int rndis_filter_set_device_mac(struct netvsc_device *nvdev,
653 struct rndis_device *rdev = nvdev->extension;
654 struct rndis_request *request;
655 struct rndis_set_request *set;
656 struct rndis_config_parameter_info *cpi;
657 wchar_t *cfg_nwadr, *cfg_mac;
658 struct rndis_set_complete *set_complete;
659 char macstr[2*ETH_ALEN+1];
660 u32 extlen = sizeof(struct rndis_config_parameter_info) +
661 2*NWADR_STRLEN + 4*ETH_ALEN;
664 request = get_rndis_request(rdev, RNDIS_MSG_SET,
665 RNDIS_MESSAGE_SIZE(struct rndis_set_request) + extlen);
669 set = &request->request_msg.msg.set_req;
670 set->oid = RNDIS_OID_GEN_RNDIS_CONFIG_PARAMETER;
671 set->info_buflen = extlen;
672 set->info_buf_offset = sizeof(struct rndis_set_request);
673 set->dev_vc_handle = 0;
675 cpi = (struct rndis_config_parameter_info *)((ulong)set +
676 set->info_buf_offset);
677 cpi->parameter_name_offset =
678 sizeof(struct rndis_config_parameter_info);
679 /* Multiply by 2 because host needs 2 bytes (utf16) for each char */
680 cpi->parameter_name_length = 2*NWADR_STRLEN;
681 cpi->parameter_type = RNDIS_CONFIG_PARAM_TYPE_STRING;
682 cpi->parameter_value_offset =
683 cpi->parameter_name_offset + cpi->parameter_name_length;
684 /* Multiply by 4 because each MAC byte displayed as 2 utf16 chars */
685 cpi->parameter_value_length = 4*ETH_ALEN;
687 cfg_nwadr = (wchar_t *)((ulong)cpi + cpi->parameter_name_offset);
688 cfg_mac = (wchar_t *)((ulong)cpi + cpi->parameter_value_offset);
689 ret = utf8s_to_utf16s(NWADR_STR, NWADR_STRLEN, UTF16_HOST_ENDIAN,
690 cfg_nwadr, NWADR_STRLEN);
693 snprintf(macstr, 2*ETH_ALEN+1, "%pm", mac);
694 ret = utf8s_to_utf16s(macstr, 2*ETH_ALEN, UTF16_HOST_ENDIAN,
695 cfg_mac, 2*ETH_ALEN);
699 ret = rndis_filter_send_request(rdev, request);
703 wait_for_completion(&request->wait_event);
705 set_complete = &request->response_msg.msg.set_complete;
706 if (set_complete->status != RNDIS_STATUS_SUCCESS)
710 put_rndis_request(rdev, request);
715 rndis_filter_set_offload_params(struct net_device *ndev,
716 struct netvsc_device *nvdev,
717 struct ndis_offload_params *req_offloads)
719 struct rndis_device *rdev = nvdev->extension;
720 struct rndis_request *request;
721 struct rndis_set_request *set;
722 struct ndis_offload_params *offload_params;
723 struct rndis_set_complete *set_complete;
724 u32 extlen = sizeof(struct ndis_offload_params);
726 u32 vsp_version = nvdev->nvsp_version;
728 if (vsp_version <= NVSP_PROTOCOL_VERSION_4) {
729 extlen = VERSION_4_OFFLOAD_SIZE;
730 /* On NVSP_PROTOCOL_VERSION_4 and below, we do not support
731 * UDP checksum offload.
733 req_offloads->udp_ip_v4_csum = 0;
734 req_offloads->udp_ip_v6_csum = 0;
737 request = get_rndis_request(rdev, RNDIS_MSG_SET,
738 RNDIS_MESSAGE_SIZE(struct rndis_set_request) + extlen);
742 set = &request->request_msg.msg.set_req;
743 set->oid = OID_TCP_OFFLOAD_PARAMETERS;
744 set->info_buflen = extlen;
745 set->info_buf_offset = sizeof(struct rndis_set_request);
746 set->dev_vc_handle = 0;
748 offload_params = (struct ndis_offload_params *)((ulong)set +
749 set->info_buf_offset);
750 *offload_params = *req_offloads;
751 offload_params->header.type = NDIS_OBJECT_TYPE_DEFAULT;
752 offload_params->header.revision = NDIS_OFFLOAD_PARAMETERS_REVISION_3;
753 offload_params->header.size = extlen;
755 ret = rndis_filter_send_request(rdev, request);
759 wait_for_completion(&request->wait_event);
760 set_complete = &request->response_msg.msg.set_complete;
761 if (set_complete->status != RNDIS_STATUS_SUCCESS) {
762 netdev_err(ndev, "Fail to set offload on host side:0x%x\n",
763 set_complete->status);
768 put_rndis_request(rdev, request);
772 static int rndis_set_rss_param_msg(struct rndis_device *rdev,
773 const u8 *rss_key, u16 flag)
775 struct net_device *ndev = rdev->ndev;
776 struct net_device_context *ndc = netdev_priv(ndev);
777 struct rndis_request *request;
778 struct rndis_set_request *set;
779 struct rndis_set_complete *set_complete;
780 u32 extlen = sizeof(struct ndis_recv_scale_param) +
781 4 * ITAB_NUM + NETVSC_HASH_KEYLEN;
782 struct ndis_recv_scale_param *rssp;
787 request = get_rndis_request(
789 RNDIS_MESSAGE_SIZE(struct rndis_set_request) + extlen);
793 set = &request->request_msg.msg.set_req;
794 set->oid = OID_GEN_RECEIVE_SCALE_PARAMETERS;
795 set->info_buflen = extlen;
796 set->info_buf_offset = sizeof(struct rndis_set_request);
797 set->dev_vc_handle = 0;
799 rssp = (struct ndis_recv_scale_param *)(set + 1);
800 rssp->hdr.type = NDIS_OBJECT_TYPE_RSS_PARAMETERS;
801 rssp->hdr.rev = NDIS_RECEIVE_SCALE_PARAMETERS_REVISION_2;
802 rssp->hdr.size = sizeof(struct ndis_recv_scale_param);
804 rssp->hashinfo = NDIS_HASH_FUNC_TOEPLITZ | NDIS_HASH_IPV4 |
805 NDIS_HASH_TCP_IPV4 | NDIS_HASH_IPV6 |
807 rssp->indirect_tabsize = 4*ITAB_NUM;
808 rssp->indirect_taboffset = sizeof(struct ndis_recv_scale_param);
809 rssp->hashkey_size = NETVSC_HASH_KEYLEN;
810 rssp->hashkey_offset = rssp->indirect_taboffset +
811 rssp->indirect_tabsize;
813 /* Set indirection table entries */
814 itab = (u32 *)(rssp + 1);
815 for (i = 0; i < ITAB_NUM; i++)
816 itab[i] = ndc->rx_table[i];
818 /* Set hask key values */
819 keyp = (u8 *)((unsigned long)rssp + rssp->hashkey_offset);
820 memcpy(keyp, rss_key, NETVSC_HASH_KEYLEN);
822 ret = rndis_filter_send_request(rdev, request);
826 wait_for_completion(&request->wait_event);
827 set_complete = &request->response_msg.msg.set_complete;
828 if (set_complete->status == RNDIS_STATUS_SUCCESS) {
829 if (!(flag & NDIS_RSS_PARAM_FLAG_DISABLE_RSS) &&
830 !(flag & NDIS_RSS_PARAM_FLAG_HASH_KEY_UNCHANGED))
831 memcpy(rdev->rss_key, rss_key, NETVSC_HASH_KEYLEN);
834 netdev_err(ndev, "Fail to set RSS parameters:0x%x\n",
835 set_complete->status);
840 put_rndis_request(rdev, request);
844 int rndis_filter_set_rss_param(struct rndis_device *rdev,
847 /* Disable RSS before change */
848 rndis_set_rss_param_msg(rdev, rss_key,
849 NDIS_RSS_PARAM_FLAG_DISABLE_RSS);
851 return rndis_set_rss_param_msg(rdev, rss_key, 0);
854 static int rndis_filter_query_device_link_status(struct rndis_device *dev,
855 struct netvsc_device *net_device)
857 u32 size = sizeof(u32);
860 return rndis_filter_query_device(dev, net_device,
861 RNDIS_OID_GEN_MEDIA_CONNECT_STATUS,
862 &link_status, &size);
865 static int rndis_filter_query_link_speed(struct rndis_device *dev,
866 struct netvsc_device *net_device)
868 u32 size = sizeof(u32);
870 struct net_device_context *ndc;
873 ret = rndis_filter_query_device(dev, net_device,
874 RNDIS_OID_GEN_LINK_SPEED,
878 ndc = netdev_priv(dev->ndev);
880 /* The link speed reported from host is in 100bps unit, so
881 * we convert it to Mbps here.
883 ndc->speed = link_speed / 10000;
889 static int rndis_filter_set_packet_filter(struct rndis_device *dev,
892 struct rndis_request *request;
893 struct rndis_set_request *set;
896 if (dev->filter == new_filter)
899 request = get_rndis_request(dev, RNDIS_MSG_SET,
900 RNDIS_MESSAGE_SIZE(struct rndis_set_request) +
905 /* Setup the rndis set */
906 set = &request->request_msg.msg.set_req;
907 set->oid = RNDIS_OID_GEN_CURRENT_PACKET_FILTER;
908 set->info_buflen = sizeof(u32);
909 set->info_buf_offset = sizeof(struct rndis_set_request);
911 memcpy((void *)(unsigned long)set + sizeof(struct rndis_set_request),
912 &new_filter, sizeof(u32));
914 ret = rndis_filter_send_request(dev, request);
916 wait_for_completion(&request->wait_event);
917 dev->filter = new_filter;
920 put_rndis_request(dev, request);
925 static void rndis_set_multicast(struct work_struct *w)
927 struct rndis_device *rdev
928 = container_of(w, struct rndis_device, mcast_work);
929 u32 filter = NDIS_PACKET_TYPE_DIRECTED;
930 unsigned int flags = rdev->ndev->flags;
932 if (flags & IFF_PROMISC) {
933 filter = NDIS_PACKET_TYPE_PROMISCUOUS;
935 if (!netdev_mc_empty(rdev->ndev) || (flags & IFF_ALLMULTI))
936 filter |= NDIS_PACKET_TYPE_ALL_MULTICAST;
937 if (flags & IFF_BROADCAST)
938 filter |= NDIS_PACKET_TYPE_BROADCAST;
941 rndis_filter_set_packet_filter(rdev, filter);
944 void rndis_filter_update(struct netvsc_device *nvdev)
946 struct rndis_device *rdev = nvdev->extension;
948 schedule_work(&rdev->mcast_work);
951 static int rndis_filter_init_device(struct rndis_device *dev,
952 struct netvsc_device *nvdev)
954 struct rndis_request *request;
955 struct rndis_initialize_request *init;
956 struct rndis_initialize_complete *init_complete;
960 request = get_rndis_request(dev, RNDIS_MSG_INIT,
961 RNDIS_MESSAGE_SIZE(struct rndis_initialize_request));
967 /* Setup the rndis set */
968 init = &request->request_msg.msg.init_req;
969 init->major_ver = RNDIS_MAJOR_VERSION;
970 init->minor_ver = RNDIS_MINOR_VERSION;
971 init->max_xfer_size = 0x4000;
973 dev->state = RNDIS_DEV_INITIALIZING;
975 ret = rndis_filter_send_request(dev, request);
977 dev->state = RNDIS_DEV_UNINITIALIZED;
981 wait_for_completion(&request->wait_event);
983 init_complete = &request->response_msg.msg.init_complete;
984 status = init_complete->status;
985 if (status == RNDIS_STATUS_SUCCESS) {
986 dev->state = RNDIS_DEV_INITIALIZED;
987 nvdev->max_pkt = init_complete->max_pkt_per_msg;
988 nvdev->pkt_align = 1 << init_complete->pkt_alignment_factor;
991 dev->state = RNDIS_DEV_UNINITIALIZED;
997 put_rndis_request(dev, request);
1002 static bool netvsc_device_idle(const struct netvsc_device *nvdev)
1006 for (i = 0; i < nvdev->num_chn; i++) {
1007 const struct netvsc_channel *nvchan = &nvdev->chan_table[i];
1009 if (nvchan->mrc.first != nvchan->mrc.next)
1012 if (atomic_read(&nvchan->queue_sends) > 0)
1019 static void rndis_filter_halt_device(struct netvsc_device *nvdev,
1020 struct rndis_device *dev)
1022 struct rndis_request *request;
1023 struct rndis_halt_request *halt;
1025 /* Attempt to do a rndis device halt */
1026 request = get_rndis_request(dev, RNDIS_MSG_HALT,
1027 RNDIS_MESSAGE_SIZE(struct rndis_halt_request));
1031 /* Setup the rndis set */
1032 halt = &request->request_msg.msg.halt_req;
1033 halt->req_id = atomic_inc_return(&dev->new_req_id);
1035 /* Ignore return since this msg is optional. */
1036 rndis_filter_send_request(dev, request);
1038 dev->state = RNDIS_DEV_UNINITIALIZED;
1041 nvdev->destroy = true;
1043 /* Force flag to be ordered before waiting */
1046 /* Wait for all send completions */
1047 wait_event(nvdev->wait_drain, netvsc_device_idle(nvdev));
1050 put_rndis_request(dev, request);
1053 static int rndis_filter_open_device(struct rndis_device *dev)
1057 if (dev->state != RNDIS_DEV_INITIALIZED)
1060 ret = rndis_filter_set_packet_filter(dev,
1061 NDIS_PACKET_TYPE_BROADCAST |
1062 NDIS_PACKET_TYPE_ALL_MULTICAST |
1063 NDIS_PACKET_TYPE_DIRECTED);
1065 dev->state = RNDIS_DEV_DATAINITIALIZED;
1070 static int rndis_filter_close_device(struct rndis_device *dev)
1074 if (dev->state != RNDIS_DEV_DATAINITIALIZED)
1077 /* Make sure rndis_set_multicast doesn't re-enable filter! */
1078 cancel_work_sync(&dev->mcast_work);
1080 ret = rndis_filter_set_packet_filter(dev, 0);
1085 dev->state = RNDIS_DEV_INITIALIZED;
1090 static void netvsc_sc_open(struct vmbus_channel *new_sc)
1092 struct net_device *ndev =
1093 hv_get_drvdata(new_sc->primary_channel->device_obj);
1094 struct net_device_context *ndev_ctx = netdev_priv(ndev);
1095 struct netvsc_device *nvscdev;
1096 u16 chn_index = new_sc->offermsg.offer.sub_channel_index;
1097 struct netvsc_channel *nvchan;
1100 /* This is safe because this callback only happens when
1101 * new device is being setup and waiting on the channel_init_wait.
1103 nvscdev = rcu_dereference_raw(ndev_ctx->nvdev);
1104 if (!nvscdev || chn_index >= nvscdev->num_chn)
1107 nvchan = nvscdev->chan_table + chn_index;
1109 /* Because the device uses NAPI, all the interrupt batching and
1110 * control is done via Net softirq, not the channel handling
1112 set_channel_read_mode(new_sc, HV_CALL_ISR);
1114 /* Set the channel before opening.*/
1115 nvchan->channel = new_sc;
1117 ret = vmbus_open(new_sc, netvsc_ring_bytes,
1118 netvsc_ring_bytes, NULL, 0,
1119 netvsc_channel_cb, nvchan);
1121 napi_enable(&nvchan->napi);
1123 netdev_notice(ndev, "sub channel open failed: %d\n", ret);
1125 if (atomic_inc_return(&nvscdev->open_chn) == nvscdev->num_chn)
1126 wake_up(&nvscdev->subchan_open);
1129 /* Open sub-channels after completing the handling of the device probe.
1130 * This breaks overlap of processing the host message for the
1131 * new primary channel with the initialization of sub-channels.
1133 int rndis_set_subchannel(struct net_device *ndev,
1134 struct netvsc_device *nvdev,
1135 struct netvsc_device_info *dev_info)
1137 struct nvsp_message *init_packet = &nvdev->channel_init_pkt;
1138 struct net_device_context *ndev_ctx = netdev_priv(ndev);
1139 struct hv_device *hv_dev = ndev_ctx->device_ctx;
1140 struct rndis_device *rdev = nvdev->extension;
1145 memset(init_packet, 0, sizeof(struct nvsp_message));
1146 init_packet->hdr.msg_type = NVSP_MSG5_TYPE_SUBCHANNEL;
1147 init_packet->msg.v5_msg.subchn_req.op = NVSP_SUBCHANNEL_ALLOCATE;
1148 init_packet->msg.v5_msg.subchn_req.num_subchannels =
1150 trace_nvsp_send(ndev, init_packet);
1152 ret = vmbus_sendpacket(hv_dev->channel, init_packet,
1153 sizeof(struct nvsp_message),
1154 (unsigned long)init_packet,
1156 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1158 netdev_err(ndev, "sub channel allocate send failed: %d\n", ret);
1162 wait_for_completion(&nvdev->channel_init_wait);
1163 if (init_packet->msg.v5_msg.subchn_comp.status != NVSP_STAT_SUCCESS) {
1164 netdev_err(ndev, "sub channel request failed\n");
1168 nvdev->num_chn = 1 +
1169 init_packet->msg.v5_msg.subchn_comp.num_subchannels;
1171 /* wait for all sub channels to open */
1172 wait_event(nvdev->subchan_open,
1173 atomic_read(&nvdev->open_chn) == nvdev->num_chn);
1175 for (i = 0; i < VRSS_SEND_TAB_SIZE; i++)
1176 ndev_ctx->tx_table[i] = i % nvdev->num_chn;
1178 /* ignore failures from setting rss parameters, still have channels */
1180 rndis_filter_set_rss_param(rdev, dev_info->rss_key);
1182 rndis_filter_set_rss_param(rdev, netvsc_hash_key);
1184 netif_set_real_num_tx_queues(ndev, nvdev->num_chn);
1185 netif_set_real_num_rx_queues(ndev, nvdev->num_chn);
1190 static int rndis_netdev_set_hwcaps(struct rndis_device *rndis_device,
1191 struct netvsc_device *nvdev)
1193 struct net_device *net = rndis_device->ndev;
1194 struct net_device_context *net_device_ctx = netdev_priv(net);
1195 struct ndis_offload hwcaps;
1196 struct ndis_offload_params offloads;
1197 unsigned int gso_max_size = GSO_MAX_SIZE;
1200 /* Find HW offload capabilities */
1201 ret = rndis_query_hwcaps(rndis_device, nvdev, &hwcaps);
1205 /* A value of zero means "no change"; now turn on what we want. */
1206 memset(&offloads, 0, sizeof(struct ndis_offload_params));
1208 /* Linux does not care about IP checksum, always does in kernel */
1209 offloads.ip_v4_csum = NDIS_OFFLOAD_PARAMETERS_TX_RX_DISABLED;
1211 /* Reset previously set hw_features flags */
1212 net->hw_features &= ~NETVSC_SUPPORTED_HW_FEATURES;
1213 net_device_ctx->tx_checksum_mask = 0;
1215 /* Compute tx offload settings based on hw capabilities */
1216 net->hw_features |= NETIF_F_RXCSUM;
1217 net->hw_features |= NETIF_F_SG;
1218 net->hw_features |= NETIF_F_RXHASH;
1220 if ((hwcaps.csum.ip4_txcsum & NDIS_TXCSUM_ALL_TCP4) == NDIS_TXCSUM_ALL_TCP4) {
1221 /* Can checksum TCP */
1222 net->hw_features |= NETIF_F_IP_CSUM;
1223 net_device_ctx->tx_checksum_mask |= TRANSPORT_INFO_IPV4_TCP;
1225 offloads.tcp_ip_v4_csum = NDIS_OFFLOAD_PARAMETERS_TX_RX_ENABLED;
1227 if (hwcaps.lsov2.ip4_encap & NDIS_OFFLOAD_ENCAP_8023) {
1228 offloads.lso_v2_ipv4 = NDIS_OFFLOAD_PARAMETERS_LSOV2_ENABLED;
1229 net->hw_features |= NETIF_F_TSO;
1231 if (hwcaps.lsov2.ip4_maxsz < gso_max_size)
1232 gso_max_size = hwcaps.lsov2.ip4_maxsz;
1235 if (hwcaps.csum.ip4_txcsum & NDIS_TXCSUM_CAP_UDP4) {
1236 offloads.udp_ip_v4_csum = NDIS_OFFLOAD_PARAMETERS_TX_RX_ENABLED;
1237 net_device_ctx->tx_checksum_mask |= TRANSPORT_INFO_IPV4_UDP;
1241 if ((hwcaps.csum.ip6_txcsum & NDIS_TXCSUM_ALL_TCP6) == NDIS_TXCSUM_ALL_TCP6) {
1242 net->hw_features |= NETIF_F_IPV6_CSUM;
1244 offloads.tcp_ip_v6_csum = NDIS_OFFLOAD_PARAMETERS_TX_RX_ENABLED;
1245 net_device_ctx->tx_checksum_mask |= TRANSPORT_INFO_IPV6_TCP;
1247 if ((hwcaps.lsov2.ip6_encap & NDIS_OFFLOAD_ENCAP_8023) &&
1248 (hwcaps.lsov2.ip6_opts & NDIS_LSOV2_CAP_IP6) == NDIS_LSOV2_CAP_IP6) {
1249 offloads.lso_v2_ipv6 = NDIS_OFFLOAD_PARAMETERS_LSOV2_ENABLED;
1250 net->hw_features |= NETIF_F_TSO6;
1252 if (hwcaps.lsov2.ip6_maxsz < gso_max_size)
1253 gso_max_size = hwcaps.lsov2.ip6_maxsz;
1256 if (hwcaps.csum.ip6_txcsum & NDIS_TXCSUM_CAP_UDP6) {
1257 offloads.udp_ip_v6_csum = NDIS_OFFLOAD_PARAMETERS_TX_RX_ENABLED;
1258 net_device_ctx->tx_checksum_mask |= TRANSPORT_INFO_IPV6_UDP;
1262 if (hwcaps.rsc.ip4 && hwcaps.rsc.ip6) {
1263 net->hw_features |= NETIF_F_LRO;
1265 if (net->features & NETIF_F_LRO) {
1266 offloads.rsc_ip_v4 = NDIS_OFFLOAD_PARAMETERS_RSC_ENABLED;
1267 offloads.rsc_ip_v6 = NDIS_OFFLOAD_PARAMETERS_RSC_ENABLED;
1269 offloads.rsc_ip_v4 = NDIS_OFFLOAD_PARAMETERS_RSC_DISABLED;
1270 offloads.rsc_ip_v6 = NDIS_OFFLOAD_PARAMETERS_RSC_DISABLED;
1274 /* In case some hw_features disappeared we need to remove them from
1275 * net->features list as they're no longer supported.
1277 net->features &= ~NETVSC_SUPPORTED_HW_FEATURES | net->hw_features;
1279 netif_set_gso_max_size(net, gso_max_size);
1281 ret = rndis_filter_set_offload_params(net, nvdev, &offloads);
1286 static void rndis_get_friendly_name(struct net_device *net,
1287 struct rndis_device *rndis_device,
1288 struct netvsc_device *net_device)
1290 ucs2_char_t wname[256];
1295 size = sizeof(wname);
1296 if (rndis_filter_query_device(rndis_device, net_device,
1297 RNDIS_OID_GEN_FRIENDLY_NAME,
1299 return; /* ignore if host does not support */
1302 return; /* name not set */
1304 /* Convert Windows Unicode string to UTF-8 */
1305 len = ucs2_as_utf8(ifalias, wname, sizeof(ifalias));
1307 /* ignore the default value from host */
1308 if (strcmp(ifalias, "Network Adapter") != 0)
1309 dev_set_alias(net, ifalias, len);
1312 struct netvsc_device *rndis_filter_device_add(struct hv_device *dev,
1313 struct netvsc_device_info *device_info)
1315 struct net_device *net = hv_get_drvdata(dev);
1316 struct net_device_context *ndc = netdev_priv(net);
1317 struct netvsc_device *net_device;
1318 struct rndis_device *rndis_device;
1319 struct ndis_recv_scale_cap rsscap;
1320 u32 rsscap_size = sizeof(struct ndis_recv_scale_cap);
1322 u32 num_possible_rss_qs;
1325 rndis_device = get_rndis_device();
1327 return ERR_PTR(-ENODEV);
1329 /* Let the inner driver handle this first to create the netvsc channel
1330 * NOTE! Once the channel is created, we may get a receive callback
1331 * (RndisFilterOnReceive()) before this call is completed
1333 net_device = netvsc_device_add(dev, device_info);
1334 if (IS_ERR(net_device)) {
1335 kfree(rndis_device);
1339 /* Initialize the rndis device */
1340 net_device->max_chn = 1;
1341 net_device->num_chn = 1;
1343 net_device->extension = rndis_device;
1344 rndis_device->ndev = net;
1346 /* Send the rndis initialization message */
1347 ret = rndis_filter_init_device(rndis_device, net_device);
1351 /* Get the MTU from the host */
1353 ret = rndis_filter_query_device(rndis_device, net_device,
1354 RNDIS_OID_GEN_MAXIMUM_FRAME_SIZE,
1356 if (ret == 0 && size == sizeof(u32) && mtu < net->mtu)
1359 /* Get the mac address */
1360 ret = rndis_filter_query_device_mac(rndis_device, net_device);
1364 memcpy(device_info->mac_adr, rndis_device->hw_mac_adr, ETH_ALEN);
1366 /* Get friendly name as ifalias*/
1368 rndis_get_friendly_name(net, rndis_device, net_device);
1370 /* Query and set hardware capabilities */
1371 ret = rndis_netdev_set_hwcaps(rndis_device, net_device);
1375 rndis_filter_query_device_link_status(rndis_device, net_device);
1377 netdev_dbg(net, "Device MAC %pM link state %s\n",
1378 rndis_device->hw_mac_adr,
1379 rndis_device->link_state ? "down" : "up");
1381 if (net_device->nvsp_version < NVSP_PROTOCOL_VERSION_5)
1384 rndis_filter_query_link_speed(rndis_device, net_device);
1387 memset(&rsscap, 0, rsscap_size);
1388 ret = rndis_filter_query_device(rndis_device, net_device,
1389 OID_GEN_RECEIVE_SCALE_CAPABILITIES,
1390 &rsscap, &rsscap_size);
1391 if (ret || rsscap.num_recv_que < 2)
1394 /* This guarantees that num_possible_rss_qs <= num_online_cpus */
1395 num_possible_rss_qs = min_t(u32, num_online_cpus(),
1396 rsscap.num_recv_que);
1398 net_device->max_chn = min_t(u32, VRSS_CHANNEL_MAX, num_possible_rss_qs);
1400 /* We will use the given number of channels if available. */
1401 net_device->num_chn = min(net_device->max_chn, device_info->num_chn);
1403 if (!netif_is_rxfh_configured(net)) {
1404 for (i = 0; i < ITAB_NUM; i++)
1405 ndc->rx_table[i] = ethtool_rxfh_indir_default(
1406 i, net_device->num_chn);
1409 atomic_set(&net_device->open_chn, 1);
1410 vmbus_set_sc_create_callback(dev->channel, netvsc_sc_open);
1412 for (i = 1; i < net_device->num_chn; i++) {
1413 ret = netvsc_alloc_recv_comp_ring(net_device, i);
1416 vfree(net_device->chan_table[i].mrc.slots);
1421 for (i = 1; i < net_device->num_chn; i++)
1422 netif_napi_add(net, &net_device->chan_table[i].napi,
1423 netvsc_poll, NAPI_POLL_WEIGHT);
1428 /* setting up multiple channels failed */
1429 net_device->max_chn = 1;
1430 net_device->num_chn = 1;
1434 rndis_filter_device_remove(dev, net_device);
1435 return ERR_PTR(ret);
1438 void rndis_filter_device_remove(struct hv_device *dev,
1439 struct netvsc_device *net_dev)
1441 struct rndis_device *rndis_dev = net_dev->extension;
1443 /* Halt and release the rndis device */
1444 rndis_filter_halt_device(net_dev, rndis_dev);
1446 netvsc_device_remove(dev);
1449 int rndis_filter_open(struct netvsc_device *nvdev)
1454 return rndis_filter_open_device(nvdev->extension);
1457 int rndis_filter_close(struct netvsc_device *nvdev)
1462 return rndis_filter_close_device(nvdev->extension);