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_filter_receive_response(struct rndis_device *dev,
247 struct rndis_message *resp)
249 struct rndis_request *request = NULL;
252 struct net_device *ndev;
254 ndev = dev->net_dev->ndev;
256 spin_lock_irqsave(&dev->request_lock, flags);
257 list_for_each_entry(request, &dev->req_list, list_ent) {
259 * All request/response message contains RequestId as the 1st
262 if (request->request_msg.msg.init_req.req_id
263 == resp->msg.init_complete.req_id) {
268 spin_unlock_irqrestore(&dev->request_lock, flags);
272 sizeof(struct rndis_message) + RNDIS_EXT_LEN) {
273 memcpy(&request->response_msg, resp,
277 "rndis response buffer overflow "
278 "detected (size %u max %zu)\n",
280 sizeof(struct rndis_filter_packet));
282 if (resp->ndis_msg_type ==
284 /* does not have a request id field */
285 request->response_msg.msg.reset_complete.
286 status = RNDIS_STATUS_BUFFER_OVERFLOW;
288 request->response_msg.msg.
289 init_complete.status =
290 RNDIS_STATUS_BUFFER_OVERFLOW;
294 complete(&request->wait_event);
297 "no rndis request found for this response "
298 "(id 0x%x res type 0x%x)\n",
299 resp->msg.init_complete.req_id,
300 resp->ndis_msg_type);
304 static void rndis_filter_receive_indicate_status(struct rndis_device *dev,
305 struct rndis_message *resp)
307 struct rndis_indicate_status *indicate =
308 &resp->msg.indicate_status;
310 if (indicate->status == RNDIS_STATUS_MEDIA_CONNECT) {
311 netvsc_linkstatus_callback(
312 dev->net_dev->dev, 1);
313 } else if (indicate->status == RNDIS_STATUS_MEDIA_DISCONNECT) {
314 netvsc_linkstatus_callback(
315 dev->net_dev->dev, 0);
324 * Get the Per-Packet-Info with the specified type
325 * return NULL if not found.
327 static inline void *rndis_get_ppi(struct rndis_packet *rpkt, u32 type)
329 struct rndis_per_packet_info *ppi;
332 if (rpkt->per_pkt_info_offset == 0)
335 ppi = (struct rndis_per_packet_info *)((ulong)rpkt +
336 rpkt->per_pkt_info_offset);
337 len = rpkt->per_pkt_info_len;
340 if (ppi->type == type)
341 return (void *)((ulong)ppi + ppi->ppi_offset);
343 ppi = (struct rndis_per_packet_info *)((ulong)ppi + ppi->size);
349 static void rndis_filter_receive_data(struct rndis_device *dev,
350 struct rndis_message *msg,
351 struct hv_netvsc_packet *pkt)
353 struct rndis_packet *rndis_pkt;
355 struct ndis_pkt_8021q_info *vlan;
357 rndis_pkt = &msg->msg.pkt;
359 /* Remove the rndis header and pass it back up the stack */
360 data_offset = RNDIS_HEADER_SIZE + rndis_pkt->data_offset;
362 pkt->total_data_buflen -= data_offset;
365 * Make sure we got a valid RNDIS message, now total_data_buflen
366 * should be the data packet size plus the trailer padding size
368 if (pkt->total_data_buflen < rndis_pkt->data_len) {
369 netdev_err(dev->net_dev->ndev, "rndis message buffer "
370 "overflow detected (got %u, min %u)"
371 "...dropping this message!\n",
372 pkt->total_data_buflen, rndis_pkt->data_len);
377 * Remove the rndis trailer padding from rndis packet message
378 * rndis_pkt->data_len tell us the real data length, we only copy
379 * the data packet to the stack, without the rndis trailer padding
381 pkt->total_data_buflen = rndis_pkt->data_len;
382 pkt->data = (void *)((unsigned long)pkt->data + data_offset);
384 pkt->is_data_pkt = true;
386 vlan = rndis_get_ppi(rndis_pkt, IEEE_8021Q_INFO);
388 pkt->vlan_tci = VLAN_TAG_PRESENT | vlan->vlanid |
389 (vlan->pri << VLAN_PRIO_SHIFT);
394 netvsc_recv_callback(dev->net_dev->dev, pkt);
397 int rndis_filter_receive(struct hv_device *dev,
398 struct hv_netvsc_packet *pkt)
400 struct netvsc_device *net_dev = hv_get_drvdata(dev);
401 struct rndis_device *rndis_dev;
402 struct rndis_message *rndis_msg;
403 struct net_device *ndev;
411 ndev = net_dev->ndev;
413 /* Make sure the rndis device state is initialized */
414 if (!net_dev->extension) {
415 netdev_err(ndev, "got rndis message but no rndis device - "
416 "dropping this message!\n");
421 rndis_dev = (struct rndis_device *)net_dev->extension;
422 if (rndis_dev->state == RNDIS_DEV_UNINITIALIZED) {
423 netdev_err(ndev, "got rndis message but rndis device "
424 "uninitialized...dropping this message!\n");
429 rndis_msg = pkt->data;
431 dump_rndis_message(dev, rndis_msg);
433 switch (rndis_msg->ndis_msg_type) {
434 case RNDIS_MSG_PACKET:
436 rndis_filter_receive_data(rndis_dev, rndis_msg, pkt);
439 case RNDIS_MSG_INIT_C:
440 case RNDIS_MSG_QUERY_C:
441 case RNDIS_MSG_SET_C:
442 /* completion msgs */
443 rndis_filter_receive_response(rndis_dev, rndis_msg);
446 case RNDIS_MSG_INDICATE:
447 /* notification msgs */
448 rndis_filter_receive_indicate_status(rndis_dev, rndis_msg);
452 "unhandled rndis message (type %u len %u)\n",
453 rndis_msg->ndis_msg_type,
460 pkt->status = NVSP_STAT_FAIL;
465 static int rndis_filter_query_device(struct rndis_device *dev, u32 oid,
466 void *result, u32 *result_size)
468 struct rndis_request *request;
469 u32 inresult_size = *result_size;
470 struct rndis_query_request *query;
471 struct rndis_query_complete *query_complete;
479 request = get_rndis_request(dev, RNDIS_MSG_QUERY,
480 RNDIS_MESSAGE_SIZE(struct rndis_query_request));
486 /* Setup the rndis query */
487 query = &request->request_msg.msg.query_req;
489 query->info_buf_offset = sizeof(struct rndis_query_request);
490 query->info_buflen = 0;
491 query->dev_vc_handle = 0;
493 ret = rndis_filter_send_request(dev, request);
497 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
503 /* Copy the response back */
504 query_complete = &request->response_msg.msg.query_complete;
506 if (query_complete->info_buflen > inresult_size) {
512 (void *)((unsigned long)query_complete +
513 query_complete->info_buf_offset),
514 query_complete->info_buflen);
516 *result_size = query_complete->info_buflen;
520 put_rndis_request(dev, request);
525 static int rndis_filter_query_device_mac(struct rndis_device *dev)
529 return rndis_filter_query_device(dev,
530 RNDIS_OID_802_3_PERMANENT_ADDRESS,
531 dev->hw_mac_adr, &size);
534 #define NWADR_STR "NetworkAddress"
535 #define NWADR_STRLEN 14
537 int rndis_filter_set_device_mac(struct hv_device *hdev, char *mac)
539 struct netvsc_device *nvdev = hv_get_drvdata(hdev);
540 struct rndis_device *rdev = nvdev->extension;
541 struct net_device *ndev = nvdev->ndev;
542 struct rndis_request *request;
543 struct rndis_set_request *set;
544 struct rndis_config_parameter_info *cpi;
545 wchar_t *cfg_nwadr, *cfg_mac;
546 struct rndis_set_complete *set_complete;
547 char macstr[2*ETH_ALEN+1];
548 u32 extlen = sizeof(struct rndis_config_parameter_info) +
549 2*NWADR_STRLEN + 4*ETH_ALEN;
552 request = get_rndis_request(rdev, RNDIS_MSG_SET,
553 RNDIS_MESSAGE_SIZE(struct rndis_set_request) + extlen);
557 set = &request->request_msg.msg.set_req;
558 set->oid = RNDIS_OID_GEN_RNDIS_CONFIG_PARAMETER;
559 set->info_buflen = extlen;
560 set->info_buf_offset = sizeof(struct rndis_set_request);
561 set->dev_vc_handle = 0;
563 cpi = (struct rndis_config_parameter_info *)((ulong)set +
564 set->info_buf_offset);
565 cpi->parameter_name_offset =
566 sizeof(struct rndis_config_parameter_info);
567 /* Multiply by 2 because host needs 2 bytes (utf16) for each char */
568 cpi->parameter_name_length = 2*NWADR_STRLEN;
569 cpi->parameter_type = RNDIS_CONFIG_PARAM_TYPE_STRING;
570 cpi->parameter_value_offset =
571 cpi->parameter_name_offset + cpi->parameter_name_length;
572 /* Multiply by 4 because each MAC byte displayed as 2 utf16 chars */
573 cpi->parameter_value_length = 4*ETH_ALEN;
575 cfg_nwadr = (wchar_t *)((ulong)cpi + cpi->parameter_name_offset);
576 cfg_mac = (wchar_t *)((ulong)cpi + cpi->parameter_value_offset);
577 ret = utf8s_to_utf16s(NWADR_STR, NWADR_STRLEN, UTF16_HOST_ENDIAN,
578 cfg_nwadr, NWADR_STRLEN);
581 snprintf(macstr, 2*ETH_ALEN+1, "%pm", mac);
582 ret = utf8s_to_utf16s(macstr, 2*ETH_ALEN, UTF16_HOST_ENDIAN,
583 cfg_mac, 2*ETH_ALEN);
587 ret = rndis_filter_send_request(rdev, request);
591 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
593 netdev_err(ndev, "timeout before we got a set response...\n");
595 * can't put_rndis_request, since we may still receive a
600 set_complete = &request->response_msg.msg.set_complete;
601 if (set_complete->status != RNDIS_STATUS_SUCCESS) {
602 netdev_err(ndev, "Fail to set MAC on host side:0x%x\n",
603 set_complete->status);
609 put_rndis_request(rdev, request);
614 static int rndis_filter_query_device_link_status(struct rndis_device *dev)
616 u32 size = sizeof(u32);
620 ret = rndis_filter_query_device(dev,
621 RNDIS_OID_GEN_MEDIA_CONNECT_STATUS,
622 &link_status, &size);
623 dev->link_state = (link_status != 0) ? true : false;
628 int rndis_filter_set_packet_filter(struct rndis_device *dev, u32 new_filter)
630 struct rndis_request *request;
631 struct rndis_set_request *set;
632 struct rndis_set_complete *set_complete;
635 struct net_device *ndev;
637 ndev = dev->net_dev->ndev;
639 request = get_rndis_request(dev, RNDIS_MSG_SET,
640 RNDIS_MESSAGE_SIZE(struct rndis_set_request) +
647 /* Setup the rndis set */
648 set = &request->request_msg.msg.set_req;
649 set->oid = RNDIS_OID_GEN_CURRENT_PACKET_FILTER;
650 set->info_buflen = sizeof(u32);
651 set->info_buf_offset = sizeof(struct rndis_set_request);
653 memcpy((void *)(unsigned long)set + sizeof(struct rndis_set_request),
654 &new_filter, sizeof(u32));
656 ret = rndis_filter_send_request(dev, request);
660 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
664 "timeout before we got a set response...\n");
667 * We can't deallocate the request since we may still receive a
668 * send completion for it.
672 set_complete = &request->response_msg.msg.set_complete;
673 status = set_complete->status;
678 put_rndis_request(dev, request);
684 static int rndis_filter_init_device(struct rndis_device *dev)
686 struct rndis_request *request;
687 struct rndis_initialize_request *init;
688 struct rndis_initialize_complete *init_complete;
692 request = get_rndis_request(dev, RNDIS_MSG_INIT,
693 RNDIS_MESSAGE_SIZE(struct rndis_initialize_request));
699 /* Setup the rndis set */
700 init = &request->request_msg.msg.init_req;
701 init->major_ver = RNDIS_MAJOR_VERSION;
702 init->minor_ver = RNDIS_MINOR_VERSION;
703 init->max_xfer_size = 0x4000;
705 dev->state = RNDIS_DEV_INITIALIZING;
707 ret = rndis_filter_send_request(dev, request);
709 dev->state = RNDIS_DEV_UNINITIALIZED;
714 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
721 init_complete = &request->response_msg.msg.init_complete;
722 status = init_complete->status;
723 if (status == RNDIS_STATUS_SUCCESS) {
724 dev->state = RNDIS_DEV_INITIALIZED;
727 dev->state = RNDIS_DEV_UNINITIALIZED;
733 put_rndis_request(dev, request);
738 static void rndis_filter_halt_device(struct rndis_device *dev)
740 struct rndis_request *request;
741 struct rndis_halt_request *halt;
742 struct netvsc_device *nvdev = dev->net_dev;
743 struct hv_device *hdev = nvdev->dev;
746 /* Attempt to do a rndis device halt */
747 request = get_rndis_request(dev, RNDIS_MSG_HALT,
748 RNDIS_MESSAGE_SIZE(struct rndis_halt_request));
752 /* Setup the rndis set */
753 halt = &request->request_msg.msg.halt_req;
754 halt->req_id = atomic_inc_return(&dev->new_req_id);
756 /* Ignore return since this msg is optional. */
757 rndis_filter_send_request(dev, request);
759 dev->state = RNDIS_DEV_UNINITIALIZED;
762 spin_lock_irqsave(&hdev->channel->inbound_lock, flags);
763 nvdev->destroy = true;
764 spin_unlock_irqrestore(&hdev->channel->inbound_lock, flags);
766 /* Wait for all send completions */
767 wait_event(nvdev->wait_drain,
768 atomic_read(&nvdev->num_outstanding_sends) == 0);
771 put_rndis_request(dev, request);
775 static int rndis_filter_open_device(struct rndis_device *dev)
779 if (dev->state != RNDIS_DEV_INITIALIZED)
782 ret = rndis_filter_set_packet_filter(dev,
783 NDIS_PACKET_TYPE_BROADCAST |
784 NDIS_PACKET_TYPE_ALL_MULTICAST |
785 NDIS_PACKET_TYPE_DIRECTED);
787 dev->state = RNDIS_DEV_DATAINITIALIZED;
792 static int rndis_filter_close_device(struct rndis_device *dev)
796 if (dev->state != RNDIS_DEV_DATAINITIALIZED)
799 ret = rndis_filter_set_packet_filter(dev, 0);
801 dev->state = RNDIS_DEV_INITIALIZED;
806 int rndis_filter_device_add(struct hv_device *dev,
807 void *additional_info)
810 struct netvsc_device *net_device;
811 struct rndis_device *rndis_device;
812 struct netvsc_device_info *device_info = additional_info;
814 rndis_device = get_rndis_device();
819 * Let the inner driver handle this first to create the netvsc channel
820 * NOTE! Once the channel is created, we may get a receive callback
821 * (RndisFilterOnReceive()) before this call is completed
823 ret = netvsc_device_add(dev, additional_info);
830 /* Initialize the rndis device */
831 net_device = hv_get_drvdata(dev);
833 net_device->extension = rndis_device;
834 rndis_device->net_dev = net_device;
836 /* Send the rndis initialization message */
837 ret = rndis_filter_init_device(rndis_device);
839 rndis_filter_device_remove(dev);
843 /* Get the mac address */
844 ret = rndis_filter_query_device_mac(rndis_device);
846 rndis_filter_device_remove(dev);
850 memcpy(device_info->mac_adr, rndis_device->hw_mac_adr, ETH_ALEN);
852 rndis_filter_query_device_link_status(rndis_device);
854 device_info->link_state = rndis_device->link_state;
856 dev_info(&dev->device, "Device MAC %pM link state %s\n",
857 rndis_device->hw_mac_adr,
858 device_info->link_state ? "down" : "up");
863 void rndis_filter_device_remove(struct hv_device *dev)
865 struct netvsc_device *net_dev = hv_get_drvdata(dev);
866 struct rndis_device *rndis_dev = net_dev->extension;
868 /* Halt and release the rndis device */
869 rndis_filter_halt_device(rndis_dev);
872 net_dev->extension = NULL;
874 netvsc_device_remove(dev);
878 int rndis_filter_open(struct hv_device *dev)
880 struct netvsc_device *net_device = hv_get_drvdata(dev);
885 return rndis_filter_open_device(net_device->extension);
888 int rndis_filter_close(struct hv_device *dev)
890 struct netvsc_device *nvdev = hv_get_drvdata(dev);
895 return rndis_filter_close_device(nvdev->extension);
898 int rndis_filter_send(struct hv_device *dev,
899 struct hv_netvsc_packet *pkt)
902 struct rndis_filter_packet *filter_pkt;
903 struct rndis_message *rndis_msg;
904 struct rndis_packet *rndis_pkt;
906 bool isvlan = pkt->vlan_tci & VLAN_TAG_PRESENT;
908 /* Add the rndis header */
909 filter_pkt = (struct rndis_filter_packet *)pkt->extension;
911 rndis_msg = &filter_pkt->msg;
912 rndis_msg_size = RNDIS_MESSAGE_SIZE(struct rndis_packet);
914 rndis_msg_size += NDIS_VLAN_PPI_SIZE;
916 rndis_msg->ndis_msg_type = RNDIS_MSG_PACKET;
917 rndis_msg->msg_len = pkt->total_data_buflen +
920 rndis_pkt = &rndis_msg->msg.pkt;
921 rndis_pkt->data_offset = sizeof(struct rndis_packet);
923 rndis_pkt->data_offset += NDIS_VLAN_PPI_SIZE;
924 rndis_pkt->data_len = pkt->total_data_buflen;
927 struct rndis_per_packet_info *ppi;
928 struct ndis_pkt_8021q_info *vlan;
930 rndis_pkt->per_pkt_info_offset = sizeof(struct rndis_packet);
931 rndis_pkt->per_pkt_info_len = NDIS_VLAN_PPI_SIZE;
933 ppi = (struct rndis_per_packet_info *)((ulong)rndis_pkt +
934 rndis_pkt->per_pkt_info_offset);
935 ppi->size = NDIS_VLAN_PPI_SIZE;
936 ppi->type = IEEE_8021Q_INFO;
937 ppi->ppi_offset = sizeof(struct rndis_per_packet_info);
939 vlan = (struct ndis_pkt_8021q_info *)((ulong)ppi +
941 vlan->vlanid = pkt->vlan_tci & VLAN_VID_MASK;
942 vlan->pri = (pkt->vlan_tci & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
945 pkt->is_data_pkt = true;
946 pkt->page_buf[0].pfn = virt_to_phys(rndis_msg) >> PAGE_SHIFT;
947 pkt->page_buf[0].offset =
948 (unsigned long)rndis_msg & (PAGE_SIZE-1);
949 pkt->page_buf[0].len = rndis_msg_size;
951 /* Add one page_buf if the rndis msg goes beyond page boundary */
952 if (pkt->page_buf[0].offset + rndis_msg_size > PAGE_SIZE) {
954 for (i = pkt->page_buf_cnt; i > 1; i--)
955 pkt->page_buf[i] = pkt->page_buf[i-1];
957 pkt->page_buf[0].len = PAGE_SIZE - pkt->page_buf[0].offset;
958 pkt->page_buf[1].pfn = virt_to_phys((void *)((ulong)
959 rndis_msg + pkt->page_buf[0].len)) >> PAGE_SHIFT;
960 pkt->page_buf[1].offset = 0;
961 pkt->page_buf[1].len = rndis_msg_size - pkt->page_buf[0].len;
964 /* Save the packet send completion and context */
965 filter_pkt->completion = pkt->completion.send.send_completion;
966 filter_pkt->completion_ctx =
967 pkt->completion.send.send_completion_ctx;
970 pkt->completion.send.send_completion = rndis_filter_send_completion;
971 pkt->completion.send.send_completion_ctx = filter_pkt;
973 ret = netvsc_send(dev, pkt);
976 * Reset the completion to originals to allow retries from
979 pkt->completion.send.send_completion =
980 filter_pkt->completion;
981 pkt->completion.send.send_completion_ctx =
982 filter_pkt->completion_ctx;
988 static void rndis_filter_send_completion(void *ctx)
990 struct rndis_filter_packet *filter_pkt = ctx;
992 /* Pass it back to the original handler */
993 filter_pkt->completion(filter_pkt->completion_ctx);