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/>.
17 * Haiyang Zhang <haiyangz@microsoft.com>
18 * Hank Janssen <hjanssen@microsoft.com>
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>
31 #include "hyperv_net.h"
34 #define RNDIS_EXT_LEN 100
35 struct rndis_request {
36 struct list_head list_ent;
37 struct completion wait_event;
39 struct rndis_message response_msg;
41 * The buffer for extended info after the RNDIS response message. It's
42 * referenced based on the data offset in the RNDIS message. Its size
43 * is enough for current needs, and should be sufficient for the near
46 u8 response_ext[RNDIS_EXT_LEN];
48 /* Simplify allocation by having a netvsc packet inline */
49 struct hv_netvsc_packet pkt;
50 /* Set 2 pages for rndis requests crossing page boundary */
51 struct hv_page_buffer buf[2];
53 struct rndis_message request_msg;
55 * The buffer for the extended info after the RNDIS request message.
56 * It is referenced and sized in a similar way as response_ext.
58 u8 request_ext[RNDIS_EXT_LEN];
61 static void rndis_filter_send_completion(void *ctx);
64 static struct rndis_device *get_rndis_device(void)
66 struct rndis_device *device;
68 device = kzalloc(sizeof(struct rndis_device), GFP_KERNEL);
72 spin_lock_init(&device->request_lock);
74 INIT_LIST_HEAD(&device->req_list);
76 device->state = RNDIS_DEV_UNINITIALIZED;
81 static struct rndis_request *get_rndis_request(struct rndis_device *dev,
85 struct rndis_request *request;
86 struct rndis_message *rndis_msg;
87 struct rndis_set_request *set;
90 request = kzalloc(sizeof(struct rndis_request), GFP_KERNEL);
94 init_completion(&request->wait_event);
96 rndis_msg = &request->request_msg;
97 rndis_msg->ndis_msg_type = msg_type;
98 rndis_msg->msg_len = msg_len;
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 hv_device *hv_dev,
129 struct rndis_message *rndis_msg)
131 struct net_device *netdev;
132 struct netvsc_device *net_device;
134 net_device = hv_get_drvdata(hv_dev);
135 netdev = net_device->ndev;
137 switch (rndis_msg->ndis_msg_type) {
138 case RNDIS_MSG_PACKET:
139 netdev_dbg(netdev, "RNDIS_MSG_PACKET (len %u, "
140 "data offset %u data len %u, # oob %u, "
141 "oob offset %u, oob len %u, pkt offset %u, "
144 rndis_msg->msg.pkt.data_offset,
145 rndis_msg->msg.pkt.data_len,
146 rndis_msg->msg.pkt.num_oob_data_elements,
147 rndis_msg->msg.pkt.oob_data_offset,
148 rndis_msg->msg.pkt.oob_data_len,
149 rndis_msg->msg.pkt.per_pkt_info_offset,
150 rndis_msg->msg.pkt.per_pkt_info_len);
153 case RNDIS_MSG_INIT_C:
154 netdev_dbg(netdev, "RNDIS_MSG_INIT_C "
155 "(len %u, id 0x%x, status 0x%x, major %d, minor %d, "
156 "device flags %d, max xfer size 0x%x, max pkts %u, "
159 rndis_msg->msg.init_complete.req_id,
160 rndis_msg->msg.init_complete.status,
161 rndis_msg->msg.init_complete.major_ver,
162 rndis_msg->msg.init_complete.minor_ver,
163 rndis_msg->msg.init_complete.dev_flags,
164 rndis_msg->msg.init_complete.max_xfer_size,
165 rndis_msg->msg.init_complete.
167 rndis_msg->msg.init_complete.
168 pkt_alignment_factor);
171 case RNDIS_MSG_QUERY_C:
172 netdev_dbg(netdev, "RNDIS_MSG_QUERY_C "
173 "(len %u, id 0x%x, status 0x%x, buf len %u, "
176 rndis_msg->msg.query_complete.req_id,
177 rndis_msg->msg.query_complete.status,
178 rndis_msg->msg.query_complete.
180 rndis_msg->msg.query_complete.
184 case RNDIS_MSG_SET_C:
186 "RNDIS_MSG_SET_C (len %u, id 0x%x, status 0x%x)\n",
188 rndis_msg->msg.set_complete.req_id,
189 rndis_msg->msg.set_complete.status);
192 case RNDIS_MSG_INDICATE:
193 netdev_dbg(netdev, "RNDIS_MSG_INDICATE "
194 "(len %u, status 0x%x, buf len %u, buf offset %u)\n",
196 rndis_msg->msg.indicate_status.status,
197 rndis_msg->msg.indicate_status.status_buflen,
198 rndis_msg->msg.indicate_status.status_buf_offset);
202 netdev_dbg(netdev, "0x%x (len %u)\n",
203 rndis_msg->ndis_msg_type,
209 static int rndis_filter_send_request(struct rndis_device *dev,
210 struct rndis_request *req)
213 struct hv_netvsc_packet *packet;
215 /* Setup the packet to send it */
218 packet->is_data_pkt = false;
219 packet->total_data_buflen = req->request_msg.msg_len;
220 packet->page_buf_cnt = 1;
222 packet->page_buf[0].pfn = virt_to_phys(&req->request_msg) >>
224 packet->page_buf[0].len = req->request_msg.msg_len;
225 packet->page_buf[0].offset =
226 (unsigned long)&req->request_msg & (PAGE_SIZE - 1);
228 /* Add one page_buf when request_msg crossing page boundary */
229 if (packet->page_buf[0].offset + packet->page_buf[0].len > PAGE_SIZE) {
230 packet->page_buf_cnt++;
231 packet->page_buf[0].len = PAGE_SIZE -
232 packet->page_buf[0].offset;
233 packet->page_buf[1].pfn = virt_to_phys((void *)&req->request_msg
234 + packet->page_buf[0].len) >> PAGE_SHIFT;
235 packet->page_buf[1].offset = 0;
236 packet->page_buf[1].len = req->request_msg.msg_len -
237 packet->page_buf[0].len;
240 packet->completion.send.send_completion = NULL;
242 ret = netvsc_send(dev->net_dev->dev, packet);
246 static void rndis_set_link_state(struct rndis_device *rdev,
247 struct rndis_request *request)
250 struct rndis_query_complete *query_complete;
252 query_complete = &request->response_msg.msg.query_complete;
254 if (query_complete->status == RNDIS_STATUS_SUCCESS &&
255 query_complete->info_buflen == sizeof(u32)) {
256 memcpy(&link_status, (void *)((unsigned long)query_complete +
257 query_complete->info_buf_offset), sizeof(u32));
258 rdev->link_state = link_status != 0;
262 static void rndis_filter_receive_response(struct rndis_device *dev,
263 struct rndis_message *resp)
265 struct rndis_request *request = NULL;
268 struct net_device *ndev;
270 ndev = dev->net_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_filter_packet));
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);
324 static void rndis_filter_receive_indicate_status(struct rndis_device *dev,
325 struct rndis_message *resp)
327 struct rndis_indicate_status *indicate =
328 &resp->msg.indicate_status;
330 if (indicate->status == RNDIS_STATUS_MEDIA_CONNECT) {
331 netvsc_linkstatus_callback(
332 dev->net_dev->dev, 1);
333 } else if (indicate->status == RNDIS_STATUS_MEDIA_DISCONNECT) {
334 netvsc_linkstatus_callback(
335 dev->net_dev->dev, 0);
344 * Get the Per-Packet-Info with the specified type
345 * return NULL if not found.
347 static inline void *rndis_get_ppi(struct rndis_packet *rpkt, u32 type)
349 struct rndis_per_packet_info *ppi;
352 if (rpkt->per_pkt_info_offset == 0)
355 ppi = (struct rndis_per_packet_info *)((ulong)rpkt +
356 rpkt->per_pkt_info_offset);
357 len = rpkt->per_pkt_info_len;
360 if (ppi->type == type)
361 return (void *)((ulong)ppi + ppi->ppi_offset);
363 ppi = (struct rndis_per_packet_info *)((ulong)ppi + ppi->size);
369 static void rndis_filter_receive_data(struct rndis_device *dev,
370 struct rndis_message *msg,
371 struct hv_netvsc_packet *pkt)
373 struct rndis_packet *rndis_pkt;
375 struct ndis_pkt_8021q_info *vlan;
377 rndis_pkt = &msg->msg.pkt;
379 /* Remove the rndis header and pass it back up the stack */
380 data_offset = RNDIS_HEADER_SIZE + rndis_pkt->data_offset;
382 pkt->total_data_buflen -= data_offset;
385 * Make sure we got a valid RNDIS message, now total_data_buflen
386 * should be the data packet size plus the trailer padding size
388 if (pkt->total_data_buflen < rndis_pkt->data_len) {
389 netdev_err(dev->net_dev->ndev, "rndis message buffer "
390 "overflow detected (got %u, min %u)"
391 "...dropping this message!\n",
392 pkt->total_data_buflen, rndis_pkt->data_len);
397 * Remove the rndis trailer padding from rndis packet message
398 * rndis_pkt->data_len tell us the real data length, we only copy
399 * the data packet to the stack, without the rndis trailer padding
401 pkt->total_data_buflen = rndis_pkt->data_len;
402 pkt->data = (void *)((unsigned long)pkt->data + data_offset);
404 pkt->is_data_pkt = true;
406 vlan = rndis_get_ppi(rndis_pkt, IEEE_8021Q_INFO);
408 pkt->vlan_tci = VLAN_TAG_PRESENT | vlan->vlanid |
409 (vlan->pri << VLAN_PRIO_SHIFT);
414 netvsc_recv_callback(dev->net_dev->dev, pkt);
417 int rndis_filter_receive(struct hv_device *dev,
418 struct hv_netvsc_packet *pkt)
420 struct netvsc_device *net_dev = hv_get_drvdata(dev);
421 struct rndis_device *rndis_dev;
422 struct rndis_message *rndis_msg;
423 struct net_device *ndev;
431 ndev = net_dev->ndev;
433 /* Make sure the rndis device state is initialized */
434 if (!net_dev->extension) {
435 netdev_err(ndev, "got rndis message but no rndis device - "
436 "dropping this message!\n");
441 rndis_dev = (struct rndis_device *)net_dev->extension;
442 if (rndis_dev->state == RNDIS_DEV_UNINITIALIZED) {
443 netdev_err(ndev, "got rndis message but rndis device "
444 "uninitialized...dropping this message!\n");
449 rndis_msg = pkt->data;
451 dump_rndis_message(dev, rndis_msg);
453 switch (rndis_msg->ndis_msg_type) {
454 case RNDIS_MSG_PACKET:
456 rndis_filter_receive_data(rndis_dev, rndis_msg, pkt);
459 case RNDIS_MSG_INIT_C:
460 case RNDIS_MSG_QUERY_C:
461 case RNDIS_MSG_SET_C:
462 /* completion msgs */
463 rndis_filter_receive_response(rndis_dev, rndis_msg);
466 case RNDIS_MSG_INDICATE:
467 /* notification msgs */
468 rndis_filter_receive_indicate_status(rndis_dev, rndis_msg);
472 "unhandled rndis message (type %u len %u)\n",
473 rndis_msg->ndis_msg_type,
480 pkt->status = NVSP_STAT_FAIL;
485 static int rndis_filter_query_device(struct rndis_device *dev, u32 oid,
486 void *result, u32 *result_size)
488 struct rndis_request *request;
489 u32 inresult_size = *result_size;
490 struct rndis_query_request *query;
491 struct rndis_query_complete *query_complete;
499 request = get_rndis_request(dev, RNDIS_MSG_QUERY,
500 RNDIS_MESSAGE_SIZE(struct rndis_query_request));
506 /* Setup the rndis query */
507 query = &request->request_msg.msg.query_req;
509 query->info_buf_offset = sizeof(struct rndis_query_request);
510 query->info_buflen = 0;
511 query->dev_vc_handle = 0;
513 ret = rndis_filter_send_request(dev, request);
517 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
523 /* Copy the response back */
524 query_complete = &request->response_msg.msg.query_complete;
526 if (query_complete->info_buflen > inresult_size) {
532 (void *)((unsigned long)query_complete +
533 query_complete->info_buf_offset),
534 query_complete->info_buflen);
536 *result_size = query_complete->info_buflen;
540 put_rndis_request(dev, request);
545 static int rndis_filter_query_device_mac(struct rndis_device *dev)
549 return rndis_filter_query_device(dev,
550 RNDIS_OID_802_3_PERMANENT_ADDRESS,
551 dev->hw_mac_adr, &size);
554 #define NWADR_STR "NetworkAddress"
555 #define NWADR_STRLEN 14
557 int rndis_filter_set_device_mac(struct hv_device *hdev, char *mac)
559 struct netvsc_device *nvdev = hv_get_drvdata(hdev);
560 struct rndis_device *rdev = nvdev->extension;
561 struct net_device *ndev = nvdev->ndev;
562 struct rndis_request *request;
563 struct rndis_set_request *set;
564 struct rndis_config_parameter_info *cpi;
565 wchar_t *cfg_nwadr, *cfg_mac;
566 struct rndis_set_complete *set_complete;
567 char macstr[2*ETH_ALEN+1];
568 u32 extlen = sizeof(struct rndis_config_parameter_info) +
569 2*NWADR_STRLEN + 4*ETH_ALEN;
572 request = get_rndis_request(rdev, RNDIS_MSG_SET,
573 RNDIS_MESSAGE_SIZE(struct rndis_set_request) + extlen);
577 set = &request->request_msg.msg.set_req;
578 set->oid = RNDIS_OID_GEN_RNDIS_CONFIG_PARAMETER;
579 set->info_buflen = extlen;
580 set->info_buf_offset = sizeof(struct rndis_set_request);
581 set->dev_vc_handle = 0;
583 cpi = (struct rndis_config_parameter_info *)((ulong)set +
584 set->info_buf_offset);
585 cpi->parameter_name_offset =
586 sizeof(struct rndis_config_parameter_info);
587 /* Multiply by 2 because host needs 2 bytes (utf16) for each char */
588 cpi->parameter_name_length = 2*NWADR_STRLEN;
589 cpi->parameter_type = RNDIS_CONFIG_PARAM_TYPE_STRING;
590 cpi->parameter_value_offset =
591 cpi->parameter_name_offset + cpi->parameter_name_length;
592 /* Multiply by 4 because each MAC byte displayed as 2 utf16 chars */
593 cpi->parameter_value_length = 4*ETH_ALEN;
595 cfg_nwadr = (wchar_t *)((ulong)cpi + cpi->parameter_name_offset);
596 cfg_mac = (wchar_t *)((ulong)cpi + cpi->parameter_value_offset);
597 ret = utf8s_to_utf16s(NWADR_STR, NWADR_STRLEN, UTF16_HOST_ENDIAN,
598 cfg_nwadr, NWADR_STRLEN);
601 snprintf(macstr, 2*ETH_ALEN+1, "%pm", mac);
602 ret = utf8s_to_utf16s(macstr, 2*ETH_ALEN, UTF16_HOST_ENDIAN,
603 cfg_mac, 2*ETH_ALEN);
607 ret = rndis_filter_send_request(rdev, request);
611 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
613 netdev_err(ndev, "timeout before we got a set response...\n");
615 * can't put_rndis_request, since we may still receive a
620 set_complete = &request->response_msg.msg.set_complete;
621 if (set_complete->status != RNDIS_STATUS_SUCCESS) {
622 netdev_err(ndev, "Fail to set MAC on host side:0x%x\n",
623 set_complete->status);
629 put_rndis_request(rdev, request);
634 static int rndis_filter_query_device_link_status(struct rndis_device *dev)
636 u32 size = sizeof(u32);
640 ret = rndis_filter_query_device(dev,
641 RNDIS_OID_GEN_MEDIA_CONNECT_STATUS,
642 &link_status, &size);
647 int rndis_filter_set_packet_filter(struct rndis_device *dev, u32 new_filter)
649 struct rndis_request *request;
650 struct rndis_set_request *set;
651 struct rndis_set_complete *set_complete;
654 struct net_device *ndev;
656 ndev = dev->net_dev->ndev;
658 request = get_rndis_request(dev, RNDIS_MSG_SET,
659 RNDIS_MESSAGE_SIZE(struct rndis_set_request) +
666 /* Setup the rndis set */
667 set = &request->request_msg.msg.set_req;
668 set->oid = RNDIS_OID_GEN_CURRENT_PACKET_FILTER;
669 set->info_buflen = sizeof(u32);
670 set->info_buf_offset = sizeof(struct rndis_set_request);
672 memcpy((void *)(unsigned long)set + sizeof(struct rndis_set_request),
673 &new_filter, sizeof(u32));
675 ret = rndis_filter_send_request(dev, request);
679 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
683 "timeout before we got a set response...\n");
686 * We can't deallocate the request since we may still receive a
687 * send completion for it.
691 set_complete = &request->response_msg.msg.set_complete;
692 status = set_complete->status;
697 put_rndis_request(dev, request);
703 static int rndis_filter_init_device(struct rndis_device *dev)
705 struct rndis_request *request;
706 struct rndis_initialize_request *init;
707 struct rndis_initialize_complete *init_complete;
711 request = get_rndis_request(dev, RNDIS_MSG_INIT,
712 RNDIS_MESSAGE_SIZE(struct rndis_initialize_request));
718 /* Setup the rndis set */
719 init = &request->request_msg.msg.init_req;
720 init->major_ver = RNDIS_MAJOR_VERSION;
721 init->minor_ver = RNDIS_MINOR_VERSION;
722 init->max_xfer_size = 0x4000;
724 dev->state = RNDIS_DEV_INITIALIZING;
726 ret = rndis_filter_send_request(dev, request);
728 dev->state = RNDIS_DEV_UNINITIALIZED;
733 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
740 init_complete = &request->response_msg.msg.init_complete;
741 status = init_complete->status;
742 if (status == RNDIS_STATUS_SUCCESS) {
743 dev->state = RNDIS_DEV_INITIALIZED;
746 dev->state = RNDIS_DEV_UNINITIALIZED;
752 put_rndis_request(dev, request);
757 static void rndis_filter_halt_device(struct rndis_device *dev)
759 struct rndis_request *request;
760 struct rndis_halt_request *halt;
761 struct netvsc_device *nvdev = dev->net_dev;
762 struct hv_device *hdev = nvdev->dev;
765 /* Attempt to do a rndis device halt */
766 request = get_rndis_request(dev, RNDIS_MSG_HALT,
767 RNDIS_MESSAGE_SIZE(struct rndis_halt_request));
771 /* Setup the rndis set */
772 halt = &request->request_msg.msg.halt_req;
773 halt->req_id = atomic_inc_return(&dev->new_req_id);
775 /* Ignore return since this msg is optional. */
776 rndis_filter_send_request(dev, request);
778 dev->state = RNDIS_DEV_UNINITIALIZED;
781 spin_lock_irqsave(&hdev->channel->inbound_lock, flags);
782 nvdev->destroy = true;
783 spin_unlock_irqrestore(&hdev->channel->inbound_lock, flags);
785 /* Wait for all send completions */
786 wait_event(nvdev->wait_drain,
787 atomic_read(&nvdev->num_outstanding_sends) == 0);
790 put_rndis_request(dev, request);
794 static int rndis_filter_open_device(struct rndis_device *dev)
798 if (dev->state != RNDIS_DEV_INITIALIZED)
801 ret = rndis_filter_set_packet_filter(dev,
802 NDIS_PACKET_TYPE_BROADCAST |
803 NDIS_PACKET_TYPE_ALL_MULTICAST |
804 NDIS_PACKET_TYPE_DIRECTED);
806 dev->state = RNDIS_DEV_DATAINITIALIZED;
811 static int rndis_filter_close_device(struct rndis_device *dev)
815 if (dev->state != RNDIS_DEV_DATAINITIALIZED)
818 ret = rndis_filter_set_packet_filter(dev, 0);
820 dev->state = RNDIS_DEV_INITIALIZED;
825 int rndis_filter_device_add(struct hv_device *dev,
826 void *additional_info)
829 struct netvsc_device *net_device;
830 struct rndis_device *rndis_device;
831 struct netvsc_device_info *device_info = additional_info;
833 rndis_device = get_rndis_device();
838 * Let the inner driver handle this first to create the netvsc channel
839 * NOTE! Once the channel is created, we may get a receive callback
840 * (RndisFilterOnReceive()) before this call is completed
842 ret = netvsc_device_add(dev, additional_info);
849 /* Initialize the rndis device */
850 net_device = hv_get_drvdata(dev);
852 net_device->extension = rndis_device;
853 rndis_device->net_dev = net_device;
855 /* Send the rndis initialization message */
856 ret = rndis_filter_init_device(rndis_device);
858 rndis_filter_device_remove(dev);
862 /* Get the mac address */
863 ret = rndis_filter_query_device_mac(rndis_device);
865 rndis_filter_device_remove(dev);
869 memcpy(device_info->mac_adr, rndis_device->hw_mac_adr, ETH_ALEN);
871 rndis_filter_query_device_link_status(rndis_device);
873 device_info->link_state = rndis_device->link_state;
875 dev_info(&dev->device, "Device MAC %pM link state %s\n",
876 rndis_device->hw_mac_adr,
877 device_info->link_state ? "down" : "up");
882 void rndis_filter_device_remove(struct hv_device *dev)
884 struct netvsc_device *net_dev = hv_get_drvdata(dev);
885 struct rndis_device *rndis_dev = net_dev->extension;
887 /* Halt and release the rndis device */
888 rndis_filter_halt_device(rndis_dev);
891 net_dev->extension = NULL;
893 netvsc_device_remove(dev);
897 int rndis_filter_open(struct hv_device *dev)
899 struct netvsc_device *net_device = hv_get_drvdata(dev);
904 return rndis_filter_open_device(net_device->extension);
907 int rndis_filter_close(struct hv_device *dev)
909 struct netvsc_device *nvdev = hv_get_drvdata(dev);
914 return rndis_filter_close_device(nvdev->extension);
917 int rndis_filter_send(struct hv_device *dev,
918 struct hv_netvsc_packet *pkt)
921 struct rndis_filter_packet *filter_pkt;
922 struct rndis_message *rndis_msg;
923 struct rndis_packet *rndis_pkt;
925 bool isvlan = pkt->vlan_tci & VLAN_TAG_PRESENT;
927 /* Add the rndis header */
928 filter_pkt = (struct rndis_filter_packet *)pkt->extension;
930 rndis_msg = &filter_pkt->msg;
931 rndis_msg_size = RNDIS_MESSAGE_SIZE(struct rndis_packet);
933 rndis_msg_size += NDIS_VLAN_PPI_SIZE;
935 rndis_msg->ndis_msg_type = RNDIS_MSG_PACKET;
936 rndis_msg->msg_len = pkt->total_data_buflen +
939 rndis_pkt = &rndis_msg->msg.pkt;
940 rndis_pkt->data_offset = sizeof(struct rndis_packet);
942 rndis_pkt->data_offset += NDIS_VLAN_PPI_SIZE;
943 rndis_pkt->data_len = pkt->total_data_buflen;
946 struct rndis_per_packet_info *ppi;
947 struct ndis_pkt_8021q_info *vlan;
949 rndis_pkt->per_pkt_info_offset = sizeof(struct rndis_packet);
950 rndis_pkt->per_pkt_info_len = NDIS_VLAN_PPI_SIZE;
952 ppi = (struct rndis_per_packet_info *)((ulong)rndis_pkt +
953 rndis_pkt->per_pkt_info_offset);
954 ppi->size = NDIS_VLAN_PPI_SIZE;
955 ppi->type = IEEE_8021Q_INFO;
956 ppi->ppi_offset = sizeof(struct rndis_per_packet_info);
958 vlan = (struct ndis_pkt_8021q_info *)((ulong)ppi +
960 vlan->vlanid = pkt->vlan_tci & VLAN_VID_MASK;
961 vlan->pri = (pkt->vlan_tci & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
964 pkt->is_data_pkt = true;
965 pkt->page_buf[0].pfn = virt_to_phys(rndis_msg) >> PAGE_SHIFT;
966 pkt->page_buf[0].offset =
967 (unsigned long)rndis_msg & (PAGE_SIZE-1);
968 pkt->page_buf[0].len = rndis_msg_size;
970 /* Add one page_buf if the rndis msg goes beyond page boundary */
971 if (pkt->page_buf[0].offset + rndis_msg_size > PAGE_SIZE) {
973 for (i = pkt->page_buf_cnt; i > 1; i--)
974 pkt->page_buf[i] = pkt->page_buf[i-1];
976 pkt->page_buf[0].len = PAGE_SIZE - pkt->page_buf[0].offset;
977 pkt->page_buf[1].pfn = virt_to_phys((void *)((ulong)
978 rndis_msg + pkt->page_buf[0].len)) >> PAGE_SHIFT;
979 pkt->page_buf[1].offset = 0;
980 pkt->page_buf[1].len = rndis_msg_size - pkt->page_buf[0].len;
983 /* Save the packet send completion and context */
984 filter_pkt->completion = pkt->completion.send.send_completion;
985 filter_pkt->completion_ctx =
986 pkt->completion.send.send_completion_ctx;
989 pkt->completion.send.send_completion = rndis_filter_send_completion;
990 pkt->completion.send.send_completion_ctx = filter_pkt;
992 ret = netvsc_send(dev, pkt);
995 * Reset the completion to originals to allow retries from
998 pkt->completion.send.send_completion =
999 filter_pkt->completion;
1000 pkt->completion.send.send_completion_ctx =
1001 filter_pkt->completion_ctx;
1007 static void rndis_filter_send_completion(void *ctx)
1009 struct rndis_filter_packet *filter_pkt = ctx;
1011 /* Pass it back to the original handler */
1012 filter_pkt->completion(filter_pkt->completion_ctx);