2 * Copyright (c) 2009, Microsoft Corporation.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 * Place - Suite 330, Boston, MA 02111-1307 USA.
18 * Haiyang Zhang <haiyangz@microsoft.com>
19 * Hank Janssen <hjanssen@microsoft.com>
21 #include <linux/kernel.h>
22 #include <linux/sched.h>
23 #include <linux/wait.h>
24 #include <linux/highmem.h>
25 #include <linux/slab.h>
27 #include <linux/if_ether.h>
28 #include <linux/netdevice.h>
29 #include <linux/if_vlan.h>
30 #include <linux/nls.h>
32 #include "hyperv_net.h"
35 #define RNDIS_EXT_LEN 100
36 struct rndis_request {
37 struct list_head list_ent;
38 struct completion wait_event;
40 struct rndis_message response_msg;
42 * The buffer for extended info after the RNDIS response message. It's
43 * referenced based on the data offset in the RNDIS message. Its size
44 * is enough for current needs, and should be sufficient for the near
47 u8 response_ext[RNDIS_EXT_LEN];
49 /* Simplify allocation by having a netvsc packet inline */
50 struct hv_netvsc_packet pkt;
51 /* Set 2 pages for rndis requests crossing page boundary */
52 struct hv_page_buffer buf[2];
54 struct rndis_message request_msg;
56 * The buffer for the extended info after the RNDIS request message.
57 * It is referenced and sized in a similar way as response_ext.
59 u8 request_ext[RNDIS_EXT_LEN];
62 static void rndis_filter_send_completion(void *ctx);
64 static void rndis_filter_send_request_completion(void *ctx);
68 static struct rndis_device *get_rndis_device(void)
70 struct rndis_device *device;
72 device = kzalloc(sizeof(struct rndis_device), GFP_KERNEL);
76 spin_lock_init(&device->request_lock);
78 INIT_LIST_HEAD(&device->req_list);
80 device->state = RNDIS_DEV_UNINITIALIZED;
85 static struct rndis_request *get_rndis_request(struct rndis_device *dev,
89 struct rndis_request *request;
90 struct rndis_message *rndis_msg;
91 struct rndis_set_request *set;
94 request = kzalloc(sizeof(struct rndis_request), GFP_KERNEL);
98 init_completion(&request->wait_event);
100 rndis_msg = &request->request_msg;
101 rndis_msg->ndis_msg_type = msg_type;
102 rndis_msg->msg_len = msg_len;
105 * Set the request id. This field is always after the rndis header for
106 * request/response packet types so we just used the SetRequest as a
109 set = &rndis_msg->msg.set_req;
110 set->req_id = atomic_inc_return(&dev->new_req_id);
112 /* Add to the request list */
113 spin_lock_irqsave(&dev->request_lock, flags);
114 list_add_tail(&request->list_ent, &dev->req_list);
115 spin_unlock_irqrestore(&dev->request_lock, flags);
120 static void put_rndis_request(struct rndis_device *dev,
121 struct rndis_request *req)
125 spin_lock_irqsave(&dev->request_lock, flags);
126 list_del(&req->list_ent);
127 spin_unlock_irqrestore(&dev->request_lock, flags);
132 static void dump_rndis_message(struct hv_device *hv_dev,
133 struct rndis_message *rndis_msg)
135 struct net_device *netdev;
136 struct netvsc_device *net_device;
138 net_device = hv_get_drvdata(hv_dev);
139 netdev = net_device->ndev;
141 switch (rndis_msg->ndis_msg_type) {
142 case RNDIS_MSG_PACKET:
143 netdev_dbg(netdev, "RNDIS_MSG_PACKET (len %u, "
144 "data offset %u data len %u, # oob %u, "
145 "oob offset %u, oob len %u, pkt offset %u, "
148 rndis_msg->msg.pkt.data_offset,
149 rndis_msg->msg.pkt.data_len,
150 rndis_msg->msg.pkt.num_oob_data_elements,
151 rndis_msg->msg.pkt.oob_data_offset,
152 rndis_msg->msg.pkt.oob_data_len,
153 rndis_msg->msg.pkt.per_pkt_info_offset,
154 rndis_msg->msg.pkt.per_pkt_info_len);
157 case RNDIS_MSG_INIT_C:
158 netdev_dbg(netdev, "RNDIS_MSG_INIT_C "
159 "(len %u, id 0x%x, status 0x%x, major %d, minor %d, "
160 "device flags %d, max xfer size 0x%x, max pkts %u, "
163 rndis_msg->msg.init_complete.req_id,
164 rndis_msg->msg.init_complete.status,
165 rndis_msg->msg.init_complete.major_ver,
166 rndis_msg->msg.init_complete.minor_ver,
167 rndis_msg->msg.init_complete.dev_flags,
168 rndis_msg->msg.init_complete.max_xfer_size,
169 rndis_msg->msg.init_complete.
171 rndis_msg->msg.init_complete.
172 pkt_alignment_factor);
175 case RNDIS_MSG_QUERY_C:
176 netdev_dbg(netdev, "RNDIS_MSG_QUERY_C "
177 "(len %u, id 0x%x, status 0x%x, buf len %u, "
180 rndis_msg->msg.query_complete.req_id,
181 rndis_msg->msg.query_complete.status,
182 rndis_msg->msg.query_complete.
184 rndis_msg->msg.query_complete.
188 case RNDIS_MSG_SET_C:
190 "RNDIS_MSG_SET_C (len %u, id 0x%x, status 0x%x)\n",
192 rndis_msg->msg.set_complete.req_id,
193 rndis_msg->msg.set_complete.status);
196 case RNDIS_MSG_INDICATE:
197 netdev_dbg(netdev, "RNDIS_MSG_INDICATE "
198 "(len %u, status 0x%x, buf len %u, buf offset %u)\n",
200 rndis_msg->msg.indicate_status.status,
201 rndis_msg->msg.indicate_status.status_buflen,
202 rndis_msg->msg.indicate_status.status_buf_offset);
206 netdev_dbg(netdev, "0x%x (len %u)\n",
207 rndis_msg->ndis_msg_type,
213 static int rndis_filter_send_request(struct rndis_device *dev,
214 struct rndis_request *req)
217 struct hv_netvsc_packet *packet;
219 /* Setup the packet to send it */
222 packet->is_data_pkt = false;
223 packet->total_data_buflen = req->request_msg.msg_len;
224 packet->page_buf_cnt = 1;
226 packet->page_buf[0].pfn = virt_to_phys(&req->request_msg) >>
228 packet->page_buf[0].len = req->request_msg.msg_len;
229 packet->page_buf[0].offset =
230 (unsigned long)&req->request_msg & (PAGE_SIZE - 1);
232 /* Add one page_buf when request_msg crossing page boundary */
233 if (packet->page_buf[0].offset + packet->page_buf[0].len > PAGE_SIZE) {
234 packet->page_buf_cnt++;
235 packet->page_buf[0].len = PAGE_SIZE -
236 packet->page_buf[0].offset;
237 packet->page_buf[1].pfn = virt_to_phys((void *)&req->request_msg
238 + packet->page_buf[0].len) >> PAGE_SHIFT;
239 packet->page_buf[1].offset = 0;
240 packet->page_buf[1].len = req->request_msg.msg_len -
241 packet->page_buf[0].len;
244 packet->completion.send.send_completion_ctx = req;/* packet; */
245 packet->completion.send.send_completion =
246 rndis_filter_send_request_completion;
247 packet->completion.send.send_completion_tid = (unsigned long)dev;
249 ret = netvsc_send(dev->net_dev->dev, packet);
253 static void rndis_filter_receive_response(struct rndis_device *dev,
254 struct rndis_message *resp)
256 struct rndis_request *request = NULL;
259 struct net_device *ndev;
261 ndev = dev->net_dev->ndev;
263 spin_lock_irqsave(&dev->request_lock, flags);
264 list_for_each_entry(request, &dev->req_list, list_ent) {
266 * All request/response message contains RequestId as the 1st
269 if (request->request_msg.msg.init_req.req_id
270 == resp->msg.init_complete.req_id) {
275 spin_unlock_irqrestore(&dev->request_lock, flags);
279 sizeof(struct rndis_message) + RNDIS_EXT_LEN) {
280 memcpy(&request->response_msg, resp,
284 "rndis response buffer overflow "
285 "detected (size %u max %zu)\n",
287 sizeof(struct rndis_filter_packet));
289 if (resp->ndis_msg_type ==
291 /* does not have a request id field */
292 request->response_msg.msg.reset_complete.
293 status = RNDIS_STATUS_BUFFER_OVERFLOW;
295 request->response_msg.msg.
296 init_complete.status =
297 RNDIS_STATUS_BUFFER_OVERFLOW;
301 complete(&request->wait_event);
304 "no rndis request found for this response "
305 "(id 0x%x res type 0x%x)\n",
306 resp->msg.init_complete.req_id,
307 resp->ndis_msg_type);
311 static void rndis_filter_receive_indicate_status(struct rndis_device *dev,
312 struct rndis_message *resp)
314 struct rndis_indicate_status *indicate =
315 &resp->msg.indicate_status;
317 if (indicate->status == RNDIS_STATUS_MEDIA_CONNECT) {
318 netvsc_linkstatus_callback(
319 dev->net_dev->dev, 1);
320 } else if (indicate->status == RNDIS_STATUS_MEDIA_DISCONNECT) {
321 netvsc_linkstatus_callback(
322 dev->net_dev->dev, 0);
331 * Get the Per-Packet-Info with the specified type
332 * return NULL if not found.
334 static inline void *rndis_get_ppi(struct rndis_packet *rpkt, u32 type)
336 struct rndis_per_packet_info *ppi;
339 if (rpkt->per_pkt_info_offset == 0)
342 ppi = (struct rndis_per_packet_info *)((ulong)rpkt +
343 rpkt->per_pkt_info_offset);
344 len = rpkt->per_pkt_info_len;
347 if (ppi->type == type)
348 return (void *)((ulong)ppi + ppi->ppi_offset);
350 ppi = (struct rndis_per_packet_info *)((ulong)ppi + ppi->size);
356 static void rndis_filter_receive_data(struct rndis_device *dev,
357 struct rndis_message *msg,
358 struct hv_netvsc_packet *pkt)
360 struct rndis_packet *rndis_pkt;
362 struct ndis_pkt_8021q_info *vlan;
364 rndis_pkt = &msg->msg.pkt;
367 * FIXME: Handle multiple rndis pkt msgs that maybe enclosed in this
368 * netvsc packet (ie TotalDataBufferLength != MessageLength)
371 /* Remove the rndis header and pass it back up the stack */
372 data_offset = RNDIS_HEADER_SIZE + rndis_pkt->data_offset;
374 pkt->total_data_buflen -= data_offset;
377 * Make sure we got a valid RNDIS message, now total_data_buflen
378 * should be the data packet size plus the trailer padding size
380 if (pkt->total_data_buflen < rndis_pkt->data_len) {
381 netdev_err(dev->net_dev->ndev, "rndis message buffer "
382 "overflow detected (got %u, min %u)"
383 "...dropping this message!\n",
384 pkt->total_data_buflen, rndis_pkt->data_len);
389 * Remove the rndis trailer padding from rndis packet message
390 * rndis_pkt->data_len tell us the real data length, we only copy
391 * the data packet to the stack, without the rndis trailer padding
393 pkt->total_data_buflen = rndis_pkt->data_len;
394 pkt->data = (void *)((unsigned long)pkt->data + data_offset);
396 pkt->is_data_pkt = true;
398 vlan = rndis_get_ppi(rndis_pkt, IEEE_8021Q_INFO);
400 pkt->vlan_tci = VLAN_TAG_PRESENT | vlan->vlanid |
401 (vlan->pri << VLAN_PRIO_SHIFT);
406 netvsc_recv_callback(dev->net_dev->dev, pkt);
409 int rndis_filter_receive(struct hv_device *dev,
410 struct hv_netvsc_packet *pkt)
412 struct netvsc_device *net_dev = hv_get_drvdata(dev);
413 struct rndis_device *rndis_dev;
414 struct rndis_message *rndis_msg;
415 struct net_device *ndev;
423 ndev = net_dev->ndev;
425 /* Make sure the rndis device state is initialized */
426 if (!net_dev->extension) {
427 netdev_err(ndev, "got rndis message but no rndis device - "
428 "dropping this message!\n");
433 rndis_dev = (struct rndis_device *)net_dev->extension;
434 if (rndis_dev->state == RNDIS_DEV_UNINITIALIZED) {
435 netdev_err(ndev, "got rndis message but rndis device "
436 "uninitialized...dropping this message!\n");
441 rndis_msg = pkt->data;
443 dump_rndis_message(dev, rndis_msg);
445 switch (rndis_msg->ndis_msg_type) {
446 case RNDIS_MSG_PACKET:
448 rndis_filter_receive_data(rndis_dev, rndis_msg, pkt);
451 case RNDIS_MSG_INIT_C:
452 case RNDIS_MSG_QUERY_C:
453 case RNDIS_MSG_SET_C:
454 /* completion msgs */
455 rndis_filter_receive_response(rndis_dev, rndis_msg);
458 case RNDIS_MSG_INDICATE:
459 /* notification msgs */
460 rndis_filter_receive_indicate_status(rndis_dev, rndis_msg);
464 "unhandled rndis message (type %u len %u)\n",
465 rndis_msg->ndis_msg_type,
472 pkt->status = NVSP_STAT_FAIL;
477 static int rndis_filter_query_device(struct rndis_device *dev, u32 oid,
478 void *result, u32 *result_size)
480 struct rndis_request *request;
481 u32 inresult_size = *result_size;
482 struct rndis_query_request *query;
483 struct rndis_query_complete *query_complete;
491 request = get_rndis_request(dev, RNDIS_MSG_QUERY,
492 RNDIS_MESSAGE_SIZE(struct rndis_query_request));
498 /* Setup the rndis query */
499 query = &request->request_msg.msg.query_req;
501 query->info_buf_offset = sizeof(struct rndis_query_request);
502 query->info_buflen = 0;
503 query->dev_vc_handle = 0;
505 ret = rndis_filter_send_request(dev, request);
509 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
515 /* Copy the response back */
516 query_complete = &request->response_msg.msg.query_complete;
518 if (query_complete->info_buflen > inresult_size) {
524 (void *)((unsigned long)query_complete +
525 query_complete->info_buf_offset),
526 query_complete->info_buflen);
528 *result_size = query_complete->info_buflen;
532 put_rndis_request(dev, request);
537 static int rndis_filter_query_device_mac(struct rndis_device *dev)
541 return rndis_filter_query_device(dev,
542 RNDIS_OID_802_3_PERMANENT_ADDRESS,
543 dev->hw_mac_adr, &size);
546 #define NWADR_STR "NetworkAddress"
547 #define NWADR_STRLEN 14
549 int rndis_filter_set_device_mac(struct hv_device *hdev, char *mac)
551 struct netvsc_device *nvdev = hv_get_drvdata(hdev);
552 struct rndis_device *rdev = nvdev->extension;
553 struct net_device *ndev = nvdev->ndev;
554 struct rndis_request *request;
555 struct rndis_set_request *set;
556 struct rndis_config_parameter_info *cpi;
557 wchar_t *cfg_nwadr, *cfg_mac;
558 struct rndis_set_complete *set_complete;
559 char macstr[2*ETH_ALEN+1];
560 u32 extlen = sizeof(struct rndis_config_parameter_info) +
561 2*NWADR_STRLEN + 4*ETH_ALEN;
564 request = get_rndis_request(rdev, RNDIS_MSG_SET,
565 RNDIS_MESSAGE_SIZE(struct rndis_set_request) + extlen);
569 set = &request->request_msg.msg.set_req;
570 set->oid = RNDIS_OID_GEN_RNDIS_CONFIG_PARAMETER;
571 set->info_buflen = extlen;
572 set->info_buf_offset = sizeof(struct rndis_set_request);
573 set->dev_vc_handle = 0;
575 cpi = (struct rndis_config_parameter_info *)((ulong)set +
576 set->info_buf_offset);
577 cpi->parameter_name_offset =
578 sizeof(struct rndis_config_parameter_info);
579 /* Multiply by 2 because host needs 2 bytes (utf16) for each char */
580 cpi->parameter_name_length = 2*NWADR_STRLEN;
581 cpi->parameter_type = RNDIS_CONFIG_PARAM_TYPE_STRING;
582 cpi->parameter_value_offset =
583 cpi->parameter_name_offset + cpi->parameter_name_length;
584 /* Multiply by 4 because each MAC byte displayed as 2 utf16 chars */
585 cpi->parameter_value_length = 4*ETH_ALEN;
587 cfg_nwadr = (wchar_t *)((ulong)cpi + cpi->parameter_name_offset);
588 cfg_mac = (wchar_t *)((ulong)cpi + cpi->parameter_value_offset);
589 ret = utf8s_to_utf16s(NWADR_STR, NWADR_STRLEN, UTF16_HOST_ENDIAN,
590 cfg_nwadr, NWADR_STRLEN);
593 snprintf(macstr, 2*ETH_ALEN+1, "%pm", mac);
594 ret = utf8s_to_utf16s(macstr, 2*ETH_ALEN, UTF16_HOST_ENDIAN,
595 cfg_mac, 2*ETH_ALEN);
599 ret = rndis_filter_send_request(rdev, request);
603 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
605 netdev_err(ndev, "timeout before we got a set response...\n");
607 * can't put_rndis_request, since we may still receive a
612 set_complete = &request->response_msg.msg.set_complete;
613 if (set_complete->status != RNDIS_STATUS_SUCCESS)
618 put_rndis_request(rdev, request);
623 static int rndis_filter_query_device_link_status(struct rndis_device *dev)
625 u32 size = sizeof(u32);
629 ret = rndis_filter_query_device(dev,
630 RNDIS_OID_GEN_MEDIA_CONNECT_STATUS,
631 &link_status, &size);
632 dev->link_state = (link_status != 0) ? true : false;
637 int rndis_filter_set_packet_filter(struct rndis_device *dev, u32 new_filter)
639 struct rndis_request *request;
640 struct rndis_set_request *set;
641 struct rndis_set_complete *set_complete;
644 struct net_device *ndev;
646 ndev = dev->net_dev->ndev;
648 request = get_rndis_request(dev, RNDIS_MSG_SET,
649 RNDIS_MESSAGE_SIZE(struct rndis_set_request) +
656 /* Setup the rndis set */
657 set = &request->request_msg.msg.set_req;
658 set->oid = RNDIS_OID_GEN_CURRENT_PACKET_FILTER;
659 set->info_buflen = sizeof(u32);
660 set->info_buf_offset = sizeof(struct rndis_set_request);
662 memcpy((void *)(unsigned long)set + sizeof(struct rndis_set_request),
663 &new_filter, sizeof(u32));
665 ret = rndis_filter_send_request(dev, request);
669 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
673 "timeout before we got a set response...\n");
676 * We can't deallocate the request since we may still receive a
677 * send completion for it.
681 set_complete = &request->response_msg.msg.set_complete;
682 status = set_complete->status;
687 put_rndis_request(dev, request);
693 static int rndis_filter_init_device(struct rndis_device *dev)
695 struct rndis_request *request;
696 struct rndis_initialize_request *init;
697 struct rndis_initialize_complete *init_complete;
701 request = get_rndis_request(dev, RNDIS_MSG_INIT,
702 RNDIS_MESSAGE_SIZE(struct rndis_initialize_request));
708 /* Setup the rndis set */
709 init = &request->request_msg.msg.init_req;
710 init->major_ver = RNDIS_MAJOR_VERSION;
711 init->minor_ver = RNDIS_MINOR_VERSION;
712 init->max_xfer_size = 0x4000;
714 dev->state = RNDIS_DEV_INITIALIZING;
716 ret = rndis_filter_send_request(dev, request);
718 dev->state = RNDIS_DEV_UNINITIALIZED;
723 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
730 init_complete = &request->response_msg.msg.init_complete;
731 status = init_complete->status;
732 if (status == RNDIS_STATUS_SUCCESS) {
733 dev->state = RNDIS_DEV_INITIALIZED;
736 dev->state = RNDIS_DEV_UNINITIALIZED;
742 put_rndis_request(dev, request);
747 static void rndis_filter_halt_device(struct rndis_device *dev)
749 struct rndis_request *request;
750 struct rndis_halt_request *halt;
751 struct netvsc_device *nvdev = dev->net_dev;
752 struct hv_device *hdev = nvdev->dev;
755 /* Attempt to do a rndis device halt */
756 request = get_rndis_request(dev, RNDIS_MSG_HALT,
757 RNDIS_MESSAGE_SIZE(struct rndis_halt_request));
761 /* Setup the rndis set */
762 halt = &request->request_msg.msg.halt_req;
763 halt->req_id = atomic_inc_return(&dev->new_req_id);
765 /* Ignore return since this msg is optional. */
766 rndis_filter_send_request(dev, request);
768 dev->state = RNDIS_DEV_UNINITIALIZED;
771 spin_lock_irqsave(&hdev->channel->inbound_lock, flags);
772 nvdev->destroy = true;
773 spin_unlock_irqrestore(&hdev->channel->inbound_lock, flags);
775 /* Wait for all send completions */
776 wait_event(nvdev->wait_drain,
777 atomic_read(&nvdev->num_outstanding_sends) == 0);
780 put_rndis_request(dev, request);
784 static int rndis_filter_open_device(struct rndis_device *dev)
788 if (dev->state != RNDIS_DEV_INITIALIZED)
791 ret = rndis_filter_set_packet_filter(dev,
792 NDIS_PACKET_TYPE_BROADCAST |
793 NDIS_PACKET_TYPE_ALL_MULTICAST |
794 NDIS_PACKET_TYPE_DIRECTED);
796 dev->state = RNDIS_DEV_DATAINITIALIZED;
801 static int rndis_filter_close_device(struct rndis_device *dev)
805 if (dev->state != RNDIS_DEV_DATAINITIALIZED)
808 ret = rndis_filter_set_packet_filter(dev, 0);
810 dev->state = RNDIS_DEV_INITIALIZED;
815 int rndis_filter_device_add(struct hv_device *dev,
816 void *additional_info)
819 struct netvsc_device *net_device;
820 struct rndis_device *rndis_device;
821 struct netvsc_device_info *device_info = additional_info;
823 rndis_device = get_rndis_device();
828 * Let the inner driver handle this first to create the netvsc channel
829 * NOTE! Once the channel is created, we may get a receive callback
830 * (RndisFilterOnReceive()) before this call is completed
832 ret = netvsc_device_add(dev, additional_info);
839 /* Initialize the rndis device */
840 net_device = hv_get_drvdata(dev);
842 net_device->extension = rndis_device;
843 rndis_device->net_dev = net_device;
845 /* Send the rndis initialization message */
846 ret = rndis_filter_init_device(rndis_device);
848 rndis_filter_device_remove(dev);
852 /* Get the mac address */
853 ret = rndis_filter_query_device_mac(rndis_device);
855 rndis_filter_device_remove(dev);
859 memcpy(device_info->mac_adr, rndis_device->hw_mac_adr, ETH_ALEN);
861 rndis_filter_query_device_link_status(rndis_device);
863 device_info->link_state = rndis_device->link_state;
865 dev_info(&dev->device, "Device MAC %pM link state %s\n",
866 rndis_device->hw_mac_adr,
867 device_info->link_state ? "down" : "up");
872 void rndis_filter_device_remove(struct hv_device *dev)
874 struct netvsc_device *net_dev = hv_get_drvdata(dev);
875 struct rndis_device *rndis_dev = net_dev->extension;
877 /* Halt and release the rndis device */
878 rndis_filter_halt_device(rndis_dev);
881 net_dev->extension = NULL;
883 netvsc_device_remove(dev);
887 int rndis_filter_open(struct hv_device *dev)
889 struct netvsc_device *net_device = hv_get_drvdata(dev);
894 return rndis_filter_open_device(net_device->extension);
897 int rndis_filter_close(struct hv_device *dev)
899 struct netvsc_device *nvdev = hv_get_drvdata(dev);
904 return rndis_filter_close_device(nvdev->extension);
907 int rndis_filter_send(struct hv_device *dev,
908 struct hv_netvsc_packet *pkt)
911 struct rndis_filter_packet *filter_pkt;
912 struct rndis_message *rndis_msg;
913 struct rndis_packet *rndis_pkt;
915 bool isvlan = pkt->vlan_tci & VLAN_TAG_PRESENT;
917 /* Add the rndis header */
918 filter_pkt = (struct rndis_filter_packet *)pkt->extension;
920 rndis_msg = &filter_pkt->msg;
921 rndis_msg_size = RNDIS_MESSAGE_SIZE(struct rndis_packet);
923 rndis_msg_size += NDIS_VLAN_PPI_SIZE;
925 rndis_msg->ndis_msg_type = RNDIS_MSG_PACKET;
926 rndis_msg->msg_len = pkt->total_data_buflen +
929 rndis_pkt = &rndis_msg->msg.pkt;
930 rndis_pkt->data_offset = sizeof(struct rndis_packet);
932 rndis_pkt->data_offset += NDIS_VLAN_PPI_SIZE;
933 rndis_pkt->data_len = pkt->total_data_buflen;
936 struct rndis_per_packet_info *ppi;
937 struct ndis_pkt_8021q_info *vlan;
939 rndis_pkt->per_pkt_info_offset = sizeof(struct rndis_packet);
940 rndis_pkt->per_pkt_info_len = NDIS_VLAN_PPI_SIZE;
942 ppi = (struct rndis_per_packet_info *)((ulong)rndis_pkt +
943 rndis_pkt->per_pkt_info_offset);
944 ppi->size = NDIS_VLAN_PPI_SIZE;
945 ppi->type = IEEE_8021Q_INFO;
946 ppi->ppi_offset = sizeof(struct rndis_per_packet_info);
948 vlan = (struct ndis_pkt_8021q_info *)((ulong)ppi +
950 vlan->vlanid = pkt->vlan_tci & VLAN_VID_MASK;
951 vlan->pri = (pkt->vlan_tci & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
954 pkt->is_data_pkt = true;
955 pkt->page_buf[0].pfn = virt_to_phys(rndis_msg) >> PAGE_SHIFT;
956 pkt->page_buf[0].offset =
957 (unsigned long)rndis_msg & (PAGE_SIZE-1);
958 pkt->page_buf[0].len = rndis_msg_size;
960 /* Add one page_buf if the rndis msg goes beyond page boundary */
961 if (pkt->page_buf[0].offset + rndis_msg_size > PAGE_SIZE) {
963 for (i = pkt->page_buf_cnt; i > 1; i--)
964 pkt->page_buf[i] = pkt->page_buf[i-1];
966 pkt->page_buf[0].len = PAGE_SIZE - pkt->page_buf[0].offset;
967 pkt->page_buf[1].pfn = virt_to_phys((void *)((ulong)
968 rndis_msg + pkt->page_buf[0].len)) >> PAGE_SHIFT;
969 pkt->page_buf[1].offset = 0;
970 pkt->page_buf[1].len = rndis_msg_size - pkt->page_buf[0].len;
973 /* Save the packet send completion and context */
974 filter_pkt->completion = pkt->completion.send.send_completion;
975 filter_pkt->completion_ctx =
976 pkt->completion.send.send_completion_ctx;
979 pkt->completion.send.send_completion = rndis_filter_send_completion;
980 pkt->completion.send.send_completion_ctx = filter_pkt;
982 ret = netvsc_send(dev, pkt);
985 * Reset the completion to originals to allow retries from
988 pkt->completion.send.send_completion =
989 filter_pkt->completion;
990 pkt->completion.send.send_completion_ctx =
991 filter_pkt->completion_ctx;
997 static void rndis_filter_send_completion(void *ctx)
999 struct rndis_filter_packet *filter_pkt = ctx;
1001 /* Pass it back to the original handler */
1002 filter_pkt->completion(filter_pkt->completion_ctx);
1006 static void rndis_filter_send_request_completion(void *ctx)