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, see <http://www.gnu.org/licenses/>.
20 #include <linux/kernel.h>
21 #include <linux/sched.h>
22 #include <linux/wait.h>
23 #include <linux/highmem.h>
24 #include <linux/slab.h>
26 #include <linux/if_ether.h>
27 #include <linux/netdevice.h>
28 #include <linux/if_vlan.h>
29 #include <linux/nls.h>
30 #include <linux/vmalloc.h>
32 #include "hyperv_net.h"
35 #define RNDIS_EXT_LEN PAGE_SIZE
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;
52 struct rndis_message request_msg;
54 * The buffer for the extended info after the RNDIS request message.
55 * It is referenced and sized in a similar way as response_ext.
57 u8 request_ext[RNDIS_EXT_LEN];
60 static const u8 netvsc_hash_key[NETVSC_HASH_KEYLEN] = {
61 0x6d, 0x5a, 0x56, 0xda, 0x25, 0x5b, 0x0e, 0xc2,
62 0x41, 0x67, 0x25, 0x3d, 0x43, 0xa3, 0x8f, 0xb0,
63 0xd0, 0xca, 0x2b, 0xcb, 0xae, 0x7b, 0x30, 0xb4,
64 0x77, 0xcb, 0x2d, 0xa3, 0x80, 0x30, 0xf2, 0x0c,
65 0x6a, 0x42, 0xb7, 0x3b, 0xbe, 0xac, 0x01, 0xfa
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;
104 request->pkt.q_idx = 0;
107 * Set the request id. This field is always after the rndis header for
108 * request/response packet types so we just used the SetRequest as a
111 set = &rndis_msg->msg.set_req;
112 set->req_id = atomic_inc_return(&dev->new_req_id);
114 /* Add to the request list */
115 spin_lock_irqsave(&dev->request_lock, flags);
116 list_add_tail(&request->list_ent, &dev->req_list);
117 spin_unlock_irqrestore(&dev->request_lock, flags);
122 static void put_rndis_request(struct rndis_device *dev,
123 struct rndis_request *req)
127 spin_lock_irqsave(&dev->request_lock, flags);
128 list_del(&req->list_ent);
129 spin_unlock_irqrestore(&dev->request_lock, flags);
134 static void dump_rndis_message(struct hv_device *hv_dev,
135 const struct rndis_message *rndis_msg)
137 struct net_device *netdev = hv_get_drvdata(hv_dev);
139 switch (rndis_msg->ndis_msg_type) {
140 case RNDIS_MSG_PACKET:
141 netdev_dbg(netdev, "RNDIS_MSG_PACKET (len %u, "
142 "data offset %u data len %u, # oob %u, "
143 "oob offset %u, oob len %u, pkt offset %u, "
146 rndis_msg->msg.pkt.data_offset,
147 rndis_msg->msg.pkt.data_len,
148 rndis_msg->msg.pkt.num_oob_data_elements,
149 rndis_msg->msg.pkt.oob_data_offset,
150 rndis_msg->msg.pkt.oob_data_len,
151 rndis_msg->msg.pkt.per_pkt_info_offset,
152 rndis_msg->msg.pkt.per_pkt_info_len);
155 case RNDIS_MSG_INIT_C:
156 netdev_dbg(netdev, "RNDIS_MSG_INIT_C "
157 "(len %u, id 0x%x, status 0x%x, major %d, minor %d, "
158 "device flags %d, max xfer size 0x%x, max pkts %u, "
161 rndis_msg->msg.init_complete.req_id,
162 rndis_msg->msg.init_complete.status,
163 rndis_msg->msg.init_complete.major_ver,
164 rndis_msg->msg.init_complete.minor_ver,
165 rndis_msg->msg.init_complete.dev_flags,
166 rndis_msg->msg.init_complete.max_xfer_size,
167 rndis_msg->msg.init_complete.
169 rndis_msg->msg.init_complete.
170 pkt_alignment_factor);
173 case RNDIS_MSG_QUERY_C:
174 netdev_dbg(netdev, "RNDIS_MSG_QUERY_C "
175 "(len %u, id 0x%x, status 0x%x, buf len %u, "
178 rndis_msg->msg.query_complete.req_id,
179 rndis_msg->msg.query_complete.status,
180 rndis_msg->msg.query_complete.
182 rndis_msg->msg.query_complete.
186 case RNDIS_MSG_SET_C:
188 "RNDIS_MSG_SET_C (len %u, id 0x%x, status 0x%x)\n",
190 rndis_msg->msg.set_complete.req_id,
191 rndis_msg->msg.set_complete.status);
194 case RNDIS_MSG_INDICATE:
195 netdev_dbg(netdev, "RNDIS_MSG_INDICATE "
196 "(len %u, status 0x%x, buf len %u, buf offset %u)\n",
198 rndis_msg->msg.indicate_status.status,
199 rndis_msg->msg.indicate_status.status_buflen,
200 rndis_msg->msg.indicate_status.status_buf_offset);
204 netdev_dbg(netdev, "0x%x (len %u)\n",
205 rndis_msg->ndis_msg_type,
211 static int rndis_filter_send_request(struct rndis_device *dev,
212 struct rndis_request *req)
215 struct hv_netvsc_packet *packet;
216 struct hv_page_buffer page_buf[2];
217 struct hv_page_buffer *pb = page_buf;
218 struct net_device_context *net_device_ctx = netdev_priv(dev->ndev);
220 /* Setup the packet to send it */
223 packet->total_data_buflen = req->request_msg.msg_len;
224 packet->page_buf_cnt = 1;
226 pb[0].pfn = virt_to_phys(&req->request_msg) >>
228 pb[0].len = req->request_msg.msg_len;
230 (unsigned long)&req->request_msg & (PAGE_SIZE - 1);
232 /* Add one page_buf when request_msg crossing page boundary */
233 if (pb[0].offset + pb[0].len > PAGE_SIZE) {
234 packet->page_buf_cnt++;
235 pb[0].len = PAGE_SIZE -
237 pb[1].pfn = virt_to_phys((void *)&req->request_msg
238 + pb[0].len) >> PAGE_SHIFT;
240 pb[1].len = req->request_msg.msg_len -
244 ret = netvsc_send(net_device_ctx->device_ctx, packet, NULL, &pb, NULL);
248 static void rndis_set_link_state(struct rndis_device *rdev,
249 struct rndis_request *request)
252 struct rndis_query_complete *query_complete;
254 query_complete = &request->response_msg.msg.query_complete;
256 if (query_complete->status == RNDIS_STATUS_SUCCESS &&
257 query_complete->info_buflen == sizeof(u32)) {
258 memcpy(&link_status, (void *)((unsigned long)query_complete +
259 query_complete->info_buf_offset), sizeof(u32));
260 rdev->link_state = link_status != 0;
264 static void rndis_filter_receive_response(struct rndis_device *dev,
265 struct rndis_message *resp)
267 struct rndis_request *request = NULL;
270 struct net_device *ndev = dev->ndev;
272 spin_lock_irqsave(&dev->request_lock, flags);
273 list_for_each_entry(request, &dev->req_list, list_ent) {
275 * All request/response message contains RequestId as the 1st
278 if (request->request_msg.msg.init_req.req_id
279 == resp->msg.init_complete.req_id) {
284 spin_unlock_irqrestore(&dev->request_lock, flags);
288 sizeof(struct rndis_message) + RNDIS_EXT_LEN) {
289 memcpy(&request->response_msg, resp,
291 if (request->request_msg.ndis_msg_type ==
292 RNDIS_MSG_QUERY && request->request_msg.msg.
293 query_req.oid == RNDIS_OID_GEN_MEDIA_CONNECT_STATUS)
294 rndis_set_link_state(dev, request);
297 "rndis response buffer overflow "
298 "detected (size %u max %zu)\n",
300 sizeof(struct rndis_message));
302 if (resp->ndis_msg_type ==
304 /* does not have a request id field */
305 request->response_msg.msg.reset_complete.
306 status = RNDIS_STATUS_BUFFER_OVERFLOW;
308 request->response_msg.msg.
309 init_complete.status =
310 RNDIS_STATUS_BUFFER_OVERFLOW;
314 complete(&request->wait_event);
317 "no rndis request found for this response "
318 "(id 0x%x res type 0x%x)\n",
319 resp->msg.init_complete.req_id,
320 resp->ndis_msg_type);
325 * Get the Per-Packet-Info with the specified type
326 * return NULL if not found.
328 static inline void *rndis_get_ppi(struct rndis_packet *rpkt, u32 type)
330 struct rndis_per_packet_info *ppi;
333 if (rpkt->per_pkt_info_offset == 0)
336 ppi = (struct rndis_per_packet_info *)((ulong)rpkt +
337 rpkt->per_pkt_info_offset);
338 len = rpkt->per_pkt_info_len;
341 if (ppi->type == type)
342 return (void *)((ulong)ppi + ppi->ppi_offset);
344 ppi = (struct rndis_per_packet_info *)((ulong)ppi + ppi->size);
350 static int rndis_filter_receive_data(struct net_device *ndev,
351 struct rndis_device *dev,
352 struct rndis_message *msg,
353 struct vmbus_channel *channel,
354 void *data, u32 data_buflen)
356 struct rndis_packet *rndis_pkt = &msg->msg.pkt;
357 const struct ndis_tcp_ip_checksum_info *csum_info;
358 const struct ndis_pkt_8021q_info *vlan;
361 /* Remove the rndis header and pass it back up the stack */
362 data_offset = RNDIS_HEADER_SIZE + rndis_pkt->data_offset;
364 data_buflen -= data_offset;
367 * Make sure we got a valid RNDIS message, now total_data_buflen
368 * should be the data packet size plus the trailer padding size
370 if (unlikely(data_buflen < rndis_pkt->data_len)) {
371 netdev_err(dev->ndev, "rndis message buffer "
372 "overflow detected (got %u, min %u)"
373 "...dropping this message!\n",
374 data_buflen, rndis_pkt->data_len);
375 return NVSP_STAT_FAIL;
378 vlan = rndis_get_ppi(rndis_pkt, IEEE_8021Q_INFO);
381 * Remove the rndis trailer padding from rndis packet message
382 * rndis_pkt->data_len tell us the real data length, we only copy
383 * the data packet to the stack, without the rndis trailer padding
385 data = (void *)((unsigned long)data + data_offset);
386 csum_info = rndis_get_ppi(rndis_pkt, TCPIP_CHKSUM_PKTINFO);
387 return netvsc_recv_callback(ndev, channel,
388 data, rndis_pkt->data_len,
392 int rndis_filter_receive(struct net_device *ndev,
393 struct netvsc_device *net_dev,
394 struct hv_device *dev,
395 struct vmbus_channel *channel,
396 void *data, u32 buflen)
398 struct net_device_context *net_device_ctx = netdev_priv(ndev);
399 struct rndis_device *rndis_dev = net_dev->extension;
400 struct rndis_message *rndis_msg = data;
402 /* Make sure the rndis device state is initialized */
403 if (unlikely(!rndis_dev)) {
404 netif_err(net_device_ctx, rx_err, ndev,
405 "got rndis message but no rndis device!\n");
406 return NVSP_STAT_FAIL;
409 if (unlikely(rndis_dev->state == RNDIS_DEV_UNINITIALIZED)) {
410 netif_err(net_device_ctx, rx_err, ndev,
411 "got rndis message uninitialized\n");
412 return NVSP_STAT_FAIL;
415 if (netif_msg_rx_status(net_device_ctx))
416 dump_rndis_message(dev, rndis_msg);
418 switch (rndis_msg->ndis_msg_type) {
419 case RNDIS_MSG_PACKET:
420 return rndis_filter_receive_data(ndev, rndis_dev, rndis_msg,
421 channel, data, buflen);
422 case RNDIS_MSG_INIT_C:
423 case RNDIS_MSG_QUERY_C:
424 case RNDIS_MSG_SET_C:
425 /* completion msgs */
426 rndis_filter_receive_response(rndis_dev, rndis_msg);
429 case RNDIS_MSG_INDICATE:
430 /* notification msgs */
431 netvsc_linkstatus_callback(dev, rndis_msg);
435 "unhandled rndis message (type %u len %u)\n",
436 rndis_msg->ndis_msg_type,
444 static int rndis_filter_query_device(struct rndis_device *dev, u32 oid,
445 void *result, u32 *result_size)
447 struct rndis_request *request;
448 u32 inresult_size = *result_size;
449 struct rndis_query_request *query;
450 struct rndis_query_complete *query_complete;
457 request = get_rndis_request(dev, RNDIS_MSG_QUERY,
458 RNDIS_MESSAGE_SIZE(struct rndis_query_request));
464 /* Setup the rndis query */
465 query = &request->request_msg.msg.query_req;
467 query->info_buf_offset = sizeof(struct rndis_query_request);
468 query->info_buflen = 0;
469 query->dev_vc_handle = 0;
471 if (oid == OID_TCP_OFFLOAD_HARDWARE_CAPABILITIES) {
472 struct net_device_context *ndevctx = netdev_priv(dev->ndev);
473 struct netvsc_device *nvdev = ndevctx->nvdev;
474 struct ndis_offload *hwcaps;
475 u32 nvsp_version = nvdev->nvsp_version;
479 if (nvsp_version >= NVSP_PROTOCOL_VERSION_5) {
480 ndis_rev = NDIS_OFFLOAD_PARAMETERS_REVISION_3;
481 size = NDIS_OFFLOAD_SIZE;
482 } else if (nvsp_version >= NVSP_PROTOCOL_VERSION_4) {
483 ndis_rev = NDIS_OFFLOAD_PARAMETERS_REVISION_2;
484 size = NDIS_OFFLOAD_SIZE_6_1;
486 ndis_rev = NDIS_OFFLOAD_PARAMETERS_REVISION_1;
487 size = NDIS_OFFLOAD_SIZE_6_0;
490 request->request_msg.msg_len += size;
491 query->info_buflen = size;
492 hwcaps = (struct ndis_offload *)
493 ((unsigned long)query + query->info_buf_offset);
495 hwcaps->header.type = NDIS_OBJECT_TYPE_OFFLOAD;
496 hwcaps->header.revision = ndis_rev;
497 hwcaps->header.size = size;
499 } else if (oid == OID_GEN_RECEIVE_SCALE_CAPABILITIES) {
500 struct ndis_recv_scale_cap *cap;
502 request->request_msg.msg_len +=
503 sizeof(struct ndis_recv_scale_cap);
504 query->info_buflen = sizeof(struct ndis_recv_scale_cap);
505 cap = (struct ndis_recv_scale_cap *)((unsigned long)query +
506 query->info_buf_offset);
507 cap->hdr.type = NDIS_OBJECT_TYPE_RSS_CAPABILITIES;
508 cap->hdr.rev = NDIS_RECEIVE_SCALE_CAPABILITIES_REVISION_2;
509 cap->hdr.size = sizeof(struct ndis_recv_scale_cap);
512 ret = rndis_filter_send_request(dev, request);
516 wait_for_completion(&request->wait_event);
518 /* Copy the response back */
519 query_complete = &request->response_msg.msg.query_complete;
521 if (query_complete->info_buflen > inresult_size) {
527 (void *)((unsigned long)query_complete +
528 query_complete->info_buf_offset),
529 query_complete->info_buflen);
531 *result_size = query_complete->info_buflen;
535 put_rndis_request(dev, request);
540 /* Get the hardware offload capabilities */
542 rndis_query_hwcaps(struct rndis_device *dev, struct ndis_offload *caps)
544 u32 caps_len = sizeof(*caps);
547 memset(caps, 0, sizeof(*caps));
549 ret = rndis_filter_query_device(dev,
550 OID_TCP_OFFLOAD_HARDWARE_CAPABILITIES,
555 if (caps->header.type != NDIS_OBJECT_TYPE_OFFLOAD) {
556 netdev_warn(dev->ndev, "invalid NDIS objtype %#x\n",
561 if (caps->header.revision < NDIS_OFFLOAD_PARAMETERS_REVISION_1) {
562 netdev_warn(dev->ndev, "invalid NDIS objrev %x\n",
563 caps->header.revision);
567 if (caps->header.size > caps_len ||
568 caps->header.size < NDIS_OFFLOAD_SIZE_6_0) {
569 netdev_warn(dev->ndev,
570 "invalid NDIS objsize %u, data size %u\n",
571 caps->header.size, caps_len);
578 static int rndis_filter_query_device_mac(struct rndis_device *dev)
582 return rndis_filter_query_device(dev,
583 RNDIS_OID_802_3_PERMANENT_ADDRESS,
584 dev->hw_mac_adr, &size);
587 #define NWADR_STR "NetworkAddress"
588 #define NWADR_STRLEN 14
590 int rndis_filter_set_device_mac(struct net_device *ndev, char *mac)
592 struct netvsc_device *nvdev = net_device_to_netvsc_device(ndev);
593 struct rndis_device *rdev = nvdev->extension;
594 struct rndis_request *request;
595 struct rndis_set_request *set;
596 struct rndis_config_parameter_info *cpi;
597 wchar_t *cfg_nwadr, *cfg_mac;
598 struct rndis_set_complete *set_complete;
599 char macstr[2*ETH_ALEN+1];
600 u32 extlen = sizeof(struct rndis_config_parameter_info) +
601 2*NWADR_STRLEN + 4*ETH_ALEN;
604 request = get_rndis_request(rdev, RNDIS_MSG_SET,
605 RNDIS_MESSAGE_SIZE(struct rndis_set_request) + extlen);
609 set = &request->request_msg.msg.set_req;
610 set->oid = RNDIS_OID_GEN_RNDIS_CONFIG_PARAMETER;
611 set->info_buflen = extlen;
612 set->info_buf_offset = sizeof(struct rndis_set_request);
613 set->dev_vc_handle = 0;
615 cpi = (struct rndis_config_parameter_info *)((ulong)set +
616 set->info_buf_offset);
617 cpi->parameter_name_offset =
618 sizeof(struct rndis_config_parameter_info);
619 /* Multiply by 2 because host needs 2 bytes (utf16) for each char */
620 cpi->parameter_name_length = 2*NWADR_STRLEN;
621 cpi->parameter_type = RNDIS_CONFIG_PARAM_TYPE_STRING;
622 cpi->parameter_value_offset =
623 cpi->parameter_name_offset + cpi->parameter_name_length;
624 /* Multiply by 4 because each MAC byte displayed as 2 utf16 chars */
625 cpi->parameter_value_length = 4*ETH_ALEN;
627 cfg_nwadr = (wchar_t *)((ulong)cpi + cpi->parameter_name_offset);
628 cfg_mac = (wchar_t *)((ulong)cpi + cpi->parameter_value_offset);
629 ret = utf8s_to_utf16s(NWADR_STR, NWADR_STRLEN, UTF16_HOST_ENDIAN,
630 cfg_nwadr, NWADR_STRLEN);
633 snprintf(macstr, 2*ETH_ALEN+1, "%pm", mac);
634 ret = utf8s_to_utf16s(macstr, 2*ETH_ALEN, UTF16_HOST_ENDIAN,
635 cfg_mac, 2*ETH_ALEN);
639 ret = rndis_filter_send_request(rdev, request);
643 wait_for_completion(&request->wait_event);
645 set_complete = &request->response_msg.msg.set_complete;
646 if (set_complete->status != RNDIS_STATUS_SUCCESS) {
647 netdev_err(ndev, "Fail to set MAC on host side:0x%x\n",
648 set_complete->status);
653 put_rndis_request(rdev, request);
658 rndis_filter_set_offload_params(struct net_device *ndev,
659 struct ndis_offload_params *req_offloads)
661 struct netvsc_device *nvdev = net_device_to_netvsc_device(ndev);
662 struct rndis_device *rdev = nvdev->extension;
663 struct rndis_request *request;
664 struct rndis_set_request *set;
665 struct ndis_offload_params *offload_params;
666 struct rndis_set_complete *set_complete;
667 u32 extlen = sizeof(struct ndis_offload_params);
669 u32 vsp_version = nvdev->nvsp_version;
671 if (vsp_version <= NVSP_PROTOCOL_VERSION_4) {
672 extlen = VERSION_4_OFFLOAD_SIZE;
673 /* On NVSP_PROTOCOL_VERSION_4 and below, we do not support
674 * UDP checksum offload.
676 req_offloads->udp_ip_v4_csum = 0;
677 req_offloads->udp_ip_v6_csum = 0;
680 request = get_rndis_request(rdev, RNDIS_MSG_SET,
681 RNDIS_MESSAGE_SIZE(struct rndis_set_request) + extlen);
685 set = &request->request_msg.msg.set_req;
686 set->oid = OID_TCP_OFFLOAD_PARAMETERS;
687 set->info_buflen = extlen;
688 set->info_buf_offset = sizeof(struct rndis_set_request);
689 set->dev_vc_handle = 0;
691 offload_params = (struct ndis_offload_params *)((ulong)set +
692 set->info_buf_offset);
693 *offload_params = *req_offloads;
694 offload_params->header.type = NDIS_OBJECT_TYPE_DEFAULT;
695 offload_params->header.revision = NDIS_OFFLOAD_PARAMETERS_REVISION_3;
696 offload_params->header.size = extlen;
698 ret = rndis_filter_send_request(rdev, request);
702 wait_for_completion(&request->wait_event);
703 set_complete = &request->response_msg.msg.set_complete;
704 if (set_complete->status != RNDIS_STATUS_SUCCESS) {
705 netdev_err(ndev, "Fail to set offload on host side:0x%x\n",
706 set_complete->status);
711 put_rndis_request(rdev, request);
715 int rndis_filter_set_rss_param(struct rndis_device *rdev,
716 const u8 *rss_key, int num_queue)
718 struct net_device *ndev = rdev->ndev;
719 struct rndis_request *request;
720 struct rndis_set_request *set;
721 struct rndis_set_complete *set_complete;
722 u32 extlen = sizeof(struct ndis_recv_scale_param) +
723 4 * ITAB_NUM + NETVSC_HASH_KEYLEN;
724 struct ndis_recv_scale_param *rssp;
729 request = get_rndis_request(
731 RNDIS_MESSAGE_SIZE(struct rndis_set_request) + extlen);
735 set = &request->request_msg.msg.set_req;
736 set->oid = OID_GEN_RECEIVE_SCALE_PARAMETERS;
737 set->info_buflen = extlen;
738 set->info_buf_offset = sizeof(struct rndis_set_request);
739 set->dev_vc_handle = 0;
741 rssp = (struct ndis_recv_scale_param *)(set + 1);
742 rssp->hdr.type = NDIS_OBJECT_TYPE_RSS_PARAMETERS;
743 rssp->hdr.rev = NDIS_RECEIVE_SCALE_PARAMETERS_REVISION_2;
744 rssp->hdr.size = sizeof(struct ndis_recv_scale_param);
746 rssp->hashinfo = NDIS_HASH_FUNC_TOEPLITZ | NDIS_HASH_IPV4 |
747 NDIS_HASH_TCP_IPV4 | NDIS_HASH_IPV6 |
749 rssp->indirect_tabsize = 4*ITAB_NUM;
750 rssp->indirect_taboffset = sizeof(struct ndis_recv_scale_param);
751 rssp->hashkey_size = NETVSC_HASH_KEYLEN;
752 rssp->kashkey_offset = rssp->indirect_taboffset +
753 rssp->indirect_tabsize;
755 /* Set indirection table entries */
756 itab = (u32 *)(rssp + 1);
757 for (i = 0; i < ITAB_NUM; i++)
758 itab[i] = rdev->ind_table[i];
760 /* Set hask key values */
761 keyp = (u8 *)((unsigned long)rssp + rssp->kashkey_offset);
762 memcpy(keyp, rss_key, NETVSC_HASH_KEYLEN);
764 ret = rndis_filter_send_request(rdev, request);
768 wait_for_completion(&request->wait_event);
769 set_complete = &request->response_msg.msg.set_complete;
770 if (set_complete->status == RNDIS_STATUS_SUCCESS)
771 memcpy(rdev->rss_key, rss_key, NETVSC_HASH_KEYLEN);
773 netdev_err(ndev, "Fail to set RSS parameters:0x%x\n",
774 set_complete->status);
779 put_rndis_request(rdev, request);
783 static int rndis_filter_query_device_link_status(struct rndis_device *dev)
785 u32 size = sizeof(u32);
789 ret = rndis_filter_query_device(dev,
790 RNDIS_OID_GEN_MEDIA_CONNECT_STATUS,
791 &link_status, &size);
796 static int rndis_filter_query_link_speed(struct rndis_device *dev)
798 u32 size = sizeof(u32);
800 struct net_device_context *ndc;
803 ret = rndis_filter_query_device(dev, RNDIS_OID_GEN_LINK_SPEED,
807 ndc = netdev_priv(dev->ndev);
809 /* The link speed reported from host is in 100bps unit, so
810 * we convert it to Mbps here.
812 ndc->speed = link_speed / 10000;
818 int rndis_filter_set_packet_filter(struct rndis_device *dev, u32 new_filter)
820 struct rndis_request *request;
821 struct rndis_set_request *set;
824 request = get_rndis_request(dev, RNDIS_MSG_SET,
825 RNDIS_MESSAGE_SIZE(struct rndis_set_request) +
831 /* Setup the rndis set */
832 set = &request->request_msg.msg.set_req;
833 set->oid = RNDIS_OID_GEN_CURRENT_PACKET_FILTER;
834 set->info_buflen = sizeof(u32);
835 set->info_buf_offset = sizeof(struct rndis_set_request);
837 memcpy((void *)(unsigned long)set + sizeof(struct rndis_set_request),
838 &new_filter, sizeof(u32));
840 ret = rndis_filter_send_request(dev, request);
842 wait_for_completion(&request->wait_event);
844 put_rndis_request(dev, request);
849 static int rndis_filter_init_device(struct rndis_device *dev)
851 struct rndis_request *request;
852 struct rndis_initialize_request *init;
853 struct rndis_initialize_complete *init_complete;
856 struct netvsc_device *nvdev = net_device_to_netvsc_device(dev->ndev);
858 request = get_rndis_request(dev, RNDIS_MSG_INIT,
859 RNDIS_MESSAGE_SIZE(struct rndis_initialize_request));
865 /* Setup the rndis set */
866 init = &request->request_msg.msg.init_req;
867 init->major_ver = RNDIS_MAJOR_VERSION;
868 init->minor_ver = RNDIS_MINOR_VERSION;
869 init->max_xfer_size = 0x4000;
871 dev->state = RNDIS_DEV_INITIALIZING;
873 ret = rndis_filter_send_request(dev, request);
875 dev->state = RNDIS_DEV_UNINITIALIZED;
879 wait_for_completion(&request->wait_event);
881 init_complete = &request->response_msg.msg.init_complete;
882 status = init_complete->status;
883 if (status == RNDIS_STATUS_SUCCESS) {
884 dev->state = RNDIS_DEV_INITIALIZED;
885 nvdev->max_pkt = init_complete->max_pkt_per_msg;
886 nvdev->pkt_align = 1 << init_complete->pkt_alignment_factor;
889 dev->state = RNDIS_DEV_UNINITIALIZED;
895 put_rndis_request(dev, request);
900 static bool netvsc_device_idle(const struct netvsc_device *nvdev)
904 if (atomic_read(&nvdev->num_outstanding_recvs) > 0)
907 for (i = 0; i < nvdev->num_chn; i++) {
908 const struct netvsc_channel *nvchan = &nvdev->chan_table[i];
910 if (atomic_read(&nvchan->queue_sends) > 0)
917 static void rndis_filter_halt_device(struct rndis_device *dev)
919 struct rndis_request *request;
920 struct rndis_halt_request *halt;
921 struct net_device_context *net_device_ctx = netdev_priv(dev->ndev);
922 struct netvsc_device *nvdev = net_device_ctx->nvdev;
924 /* Attempt to do a rndis device halt */
925 request = get_rndis_request(dev, RNDIS_MSG_HALT,
926 RNDIS_MESSAGE_SIZE(struct rndis_halt_request));
930 /* Setup the rndis set */
931 halt = &request->request_msg.msg.halt_req;
932 halt->req_id = atomic_inc_return(&dev->new_req_id);
934 /* Ignore return since this msg is optional. */
935 rndis_filter_send_request(dev, request);
937 dev->state = RNDIS_DEV_UNINITIALIZED;
940 nvdev->destroy = true;
942 /* Force flag to be ordered before waiting */
945 /* Wait for all send completions */
946 wait_event(nvdev->wait_drain, netvsc_device_idle(nvdev));
949 put_rndis_request(dev, request);
952 static int rndis_filter_open_device(struct rndis_device *dev)
956 if (dev->state != RNDIS_DEV_INITIALIZED)
959 ret = rndis_filter_set_packet_filter(dev,
960 NDIS_PACKET_TYPE_BROADCAST |
961 NDIS_PACKET_TYPE_ALL_MULTICAST |
962 NDIS_PACKET_TYPE_DIRECTED);
964 dev->state = RNDIS_DEV_DATAINITIALIZED;
969 static int rndis_filter_close_device(struct rndis_device *dev)
973 if (dev->state != RNDIS_DEV_DATAINITIALIZED)
976 ret = rndis_filter_set_packet_filter(dev, 0);
981 dev->state = RNDIS_DEV_INITIALIZED;
986 static void netvsc_sc_open(struct vmbus_channel *new_sc)
988 struct net_device *ndev =
989 hv_get_drvdata(new_sc->primary_channel->device_obj);
990 struct netvsc_device *nvscdev = net_device_to_netvsc_device(ndev);
991 u16 chn_index = new_sc->offermsg.offer.sub_channel_index;
992 struct netvsc_channel *nvchan;
995 if (chn_index >= nvscdev->num_chn)
998 nvchan = nvscdev->chan_table + chn_index;
1000 = vzalloc(NETVSC_RECVSLOT_MAX * sizeof(struct recv_comp_data));
1002 if (!nvchan->mrc.buf)
1005 /* Because the device uses NAPI, all the interrupt batching and
1006 * control is done via Net softirq, not the channel handling
1008 set_channel_read_mode(new_sc, HV_CALL_ISR);
1010 /* Set the channel before opening.*/
1011 nvchan->channel = new_sc;
1012 netif_napi_add(ndev, &nvchan->napi,
1013 netvsc_poll, NAPI_POLL_WEIGHT);
1015 ret = vmbus_open(new_sc, nvscdev->ring_size * PAGE_SIZE,
1016 nvscdev->ring_size * PAGE_SIZE, NULL, 0,
1017 netvsc_channel_cb, nvchan);
1019 napi_enable(&nvchan->napi);
1021 netdev_err(ndev, "sub channel open failed (%d)\n", ret);
1023 if (refcount_dec_and_test(&nvscdev->sc_offered))
1024 complete(&nvscdev->channel_init_wait);
1027 int rndis_filter_device_add(struct hv_device *dev,
1028 struct netvsc_device_info *device_info)
1030 struct net_device *net = hv_get_drvdata(dev);
1031 struct net_device_context *net_device_ctx = netdev_priv(net);
1032 struct netvsc_device *net_device;
1033 struct rndis_device *rndis_device;
1034 struct ndis_offload hwcaps;
1035 struct ndis_offload_params offloads;
1036 struct nvsp_message *init_packet;
1037 struct ndis_recv_scale_cap rsscap;
1038 u32 rsscap_size = sizeof(struct ndis_recv_scale_cap);
1039 unsigned int gso_max_size = GSO_MAX_SIZE;
1040 u32 mtu, size, num_rss_qs;
1041 const struct cpumask *node_cpu_mask;
1042 u32 num_possible_rss_qs;
1045 rndis_device = get_rndis_device();
1050 * Let the inner driver handle this first to create the netvsc channel
1051 * NOTE! Once the channel is created, we may get a receive callback
1052 * (RndisFilterOnReceive()) before this call is completed
1054 ret = netvsc_device_add(dev, device_info);
1056 kfree(rndis_device);
1060 /* Initialize the rndis device */
1061 net_device = net_device_ctx->nvdev;
1062 net_device->max_chn = 1;
1063 net_device->num_chn = 1;
1065 refcount_set(&net_device->sc_offered, 0);
1067 net_device->extension = rndis_device;
1068 rndis_device->ndev = net;
1070 /* Send the rndis initialization message */
1071 ret = rndis_filter_init_device(rndis_device);
1073 rndis_filter_device_remove(dev, net_device);
1077 /* Get the MTU from the host */
1079 ret = rndis_filter_query_device(rndis_device,
1080 RNDIS_OID_GEN_MAXIMUM_FRAME_SIZE,
1082 if (ret == 0 && size == sizeof(u32) && mtu < net->mtu)
1085 /* Get the mac address */
1086 ret = rndis_filter_query_device_mac(rndis_device);
1088 rndis_filter_device_remove(dev, net_device);
1092 memcpy(device_info->mac_adr, rndis_device->hw_mac_adr, ETH_ALEN);
1094 /* Find HW offload capabilities */
1095 ret = rndis_query_hwcaps(rndis_device, &hwcaps);
1097 rndis_filter_device_remove(dev, net_device);
1101 /* A value of zero means "no change"; now turn on what we want. */
1102 memset(&offloads, 0, sizeof(struct ndis_offload_params));
1104 /* Linux does not care about IP checksum, always does in kernel */
1105 offloads.ip_v4_csum = NDIS_OFFLOAD_PARAMETERS_TX_RX_DISABLED;
1107 /* Compute tx offload settings based on hw capabilities */
1108 net->hw_features = NETIF_F_RXCSUM;
1110 if ((hwcaps.csum.ip4_txcsum & NDIS_TXCSUM_ALL_TCP4) == NDIS_TXCSUM_ALL_TCP4) {
1111 /* Can checksum TCP */
1112 net->hw_features |= NETIF_F_IP_CSUM;
1113 net_device_ctx->tx_checksum_mask |= TRANSPORT_INFO_IPV4_TCP;
1115 offloads.tcp_ip_v4_csum = NDIS_OFFLOAD_PARAMETERS_TX_RX_ENABLED;
1117 if (hwcaps.lsov2.ip4_encap & NDIS_OFFLOAD_ENCAP_8023) {
1118 offloads.lso_v2_ipv4 = NDIS_OFFLOAD_PARAMETERS_LSOV2_ENABLED;
1119 net->hw_features |= NETIF_F_TSO;
1121 if (hwcaps.lsov2.ip4_maxsz < gso_max_size)
1122 gso_max_size = hwcaps.lsov2.ip4_maxsz;
1125 if (hwcaps.csum.ip4_txcsum & NDIS_TXCSUM_CAP_UDP4) {
1126 offloads.udp_ip_v4_csum = NDIS_OFFLOAD_PARAMETERS_TX_RX_ENABLED;
1127 net_device_ctx->tx_checksum_mask |= TRANSPORT_INFO_IPV4_UDP;
1131 if ((hwcaps.csum.ip6_txcsum & NDIS_TXCSUM_ALL_TCP6) == NDIS_TXCSUM_ALL_TCP6) {
1132 net->hw_features |= NETIF_F_IPV6_CSUM;
1134 offloads.tcp_ip_v6_csum = NDIS_OFFLOAD_PARAMETERS_TX_RX_ENABLED;
1135 net_device_ctx->tx_checksum_mask |= TRANSPORT_INFO_IPV6_TCP;
1137 if ((hwcaps.lsov2.ip6_encap & NDIS_OFFLOAD_ENCAP_8023) &&
1138 (hwcaps.lsov2.ip6_opts & NDIS_LSOV2_CAP_IP6) == NDIS_LSOV2_CAP_IP6) {
1139 offloads.lso_v2_ipv6 = NDIS_OFFLOAD_PARAMETERS_LSOV2_ENABLED;
1140 net->hw_features |= NETIF_F_TSO6;
1142 if (hwcaps.lsov2.ip6_maxsz < gso_max_size)
1143 gso_max_size = hwcaps.lsov2.ip6_maxsz;
1146 if (hwcaps.csum.ip6_txcsum & NDIS_TXCSUM_CAP_UDP6) {
1147 offloads.udp_ip_v6_csum = NDIS_OFFLOAD_PARAMETERS_TX_RX_ENABLED;
1148 net_device_ctx->tx_checksum_mask |= TRANSPORT_INFO_IPV6_UDP;
1152 netif_set_gso_max_size(net, gso_max_size);
1154 ret = rndis_filter_set_offload_params(net, &offloads);
1158 rndis_filter_query_device_link_status(rndis_device);
1160 device_info->link_state = rndis_device->link_state;
1162 netdev_dbg(net, "Device MAC %pM link state %s\n",
1163 rndis_device->hw_mac_adr,
1164 device_info->link_state ? "down" : "up");
1166 if (net_device->nvsp_version < NVSP_PROTOCOL_VERSION_5)
1169 rndis_filter_query_link_speed(rndis_device);
1172 memset(&rsscap, 0, rsscap_size);
1173 ret = rndis_filter_query_device(rndis_device,
1174 OID_GEN_RECEIVE_SCALE_CAPABILITIES,
1175 &rsscap, &rsscap_size);
1176 if (ret || rsscap.num_recv_que < 2)
1180 * We will limit the VRSS channels to the number CPUs in the NUMA node
1181 * the primary channel is currently bound to.
1183 * This also guarantees that num_possible_rss_qs <= num_online_cpus
1185 node_cpu_mask = cpumask_of_node(cpu_to_node(dev->channel->target_cpu));
1186 num_possible_rss_qs = min_t(u32, cpumask_weight(node_cpu_mask),
1187 rsscap.num_recv_que);
1189 net_device->max_chn = min_t(u32, VRSS_CHANNEL_MAX, num_possible_rss_qs);
1191 /* We will use the given number of channels if available. */
1192 net_device->num_chn = min(net_device->max_chn, device_info->num_chn);
1194 for (i = 0; i < ITAB_NUM; i++)
1195 rndis_device->ind_table[i] = ethtool_rxfh_indir_default(i,
1196 net_device->num_chn);
1198 num_rss_qs = net_device->num_chn - 1;
1199 if (num_rss_qs == 0)
1202 refcount_set(&net_device->sc_offered, num_rss_qs);
1203 vmbus_set_sc_create_callback(dev->channel, netvsc_sc_open);
1205 init_packet = &net_device->channel_init_pkt;
1206 memset(init_packet, 0, sizeof(struct nvsp_message));
1207 init_packet->hdr.msg_type = NVSP_MSG5_TYPE_SUBCHANNEL;
1208 init_packet->msg.v5_msg.subchn_req.op = NVSP_SUBCHANNEL_ALLOCATE;
1209 init_packet->msg.v5_msg.subchn_req.num_subchannels =
1210 net_device->num_chn - 1;
1211 ret = vmbus_sendpacket(dev->channel, init_packet,
1212 sizeof(struct nvsp_message),
1213 (unsigned long)init_packet,
1215 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1219 if (init_packet->msg.v5_msg.subchn_comp.status != NVSP_STAT_SUCCESS) {
1223 wait_for_completion(&net_device->channel_init_wait);
1225 net_device->num_chn = 1 +
1226 init_packet->msg.v5_msg.subchn_comp.num_subchannels;
1228 /* ignore failues from setting rss parameters, still have channels */
1229 rndis_filter_set_rss_param(rndis_device, netvsc_hash_key,
1230 net_device->num_chn);
1233 net_device->max_chn = 1;
1234 net_device->num_chn = 1;
1237 return 0; /* return 0 because primary channel can be used alone */
1240 rndis_filter_device_remove(dev, net_device);
1244 void rndis_filter_device_remove(struct hv_device *dev,
1245 struct netvsc_device *net_dev)
1247 struct rndis_device *rndis_dev = net_dev->extension;
1249 /* Halt and release the rndis device */
1250 rndis_filter_halt_device(rndis_dev);
1253 net_dev->extension = NULL;
1255 netvsc_device_remove(dev);
1258 int rndis_filter_open(struct netvsc_device *nvdev)
1263 if (atomic_inc_return(&nvdev->open_cnt) != 1)
1266 return rndis_filter_open_device(nvdev->extension);
1269 int rndis_filter_close(struct netvsc_device *nvdev)
1274 if (atomic_dec_return(&nvdev->open_cnt) != 0)
1277 return rndis_filter_close_device(nvdev->extension);