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);
65 static struct rndis_device *get_rndis_device(void)
67 struct rndis_device *device;
69 device = kzalloc(sizeof(struct rndis_device), GFP_KERNEL);
73 spin_lock_init(&device->request_lock);
75 INIT_LIST_HEAD(&device->req_list);
77 device->state = RNDIS_DEV_UNINITIALIZED;
82 static struct rndis_request *get_rndis_request(struct rndis_device *dev,
86 struct rndis_request *request;
87 struct rndis_message *rndis_msg;
88 struct rndis_set_request *set;
91 request = kzalloc(sizeof(struct rndis_request), GFP_KERNEL);
95 init_completion(&request->wait_event);
97 rndis_msg = &request->request_msg;
98 rndis_msg->ndis_msg_type = msg_type;
99 rndis_msg->msg_len = msg_len;
102 * Set the request id. This field is always after the rndis header for
103 * request/response packet types so we just used the SetRequest as a
106 set = &rndis_msg->msg.set_req;
107 set->req_id = atomic_inc_return(&dev->new_req_id);
109 /* Add to the request list */
110 spin_lock_irqsave(&dev->request_lock, flags);
111 list_add_tail(&request->list_ent, &dev->req_list);
112 spin_unlock_irqrestore(&dev->request_lock, flags);
117 static void put_rndis_request(struct rndis_device *dev,
118 struct rndis_request *req)
122 spin_lock_irqsave(&dev->request_lock, flags);
123 list_del(&req->list_ent);
124 spin_unlock_irqrestore(&dev->request_lock, flags);
129 static void dump_rndis_message(struct hv_device *hv_dev,
130 struct rndis_message *rndis_msg)
132 struct net_device *netdev;
133 struct netvsc_device *net_device;
135 net_device = hv_get_drvdata(hv_dev);
136 netdev = net_device->ndev;
138 switch (rndis_msg->ndis_msg_type) {
139 case RNDIS_MSG_PACKET:
140 netdev_dbg(netdev, "RNDIS_MSG_PACKET (len %u, "
141 "data offset %u data len %u, # oob %u, "
142 "oob offset %u, oob len %u, pkt offset %u, "
145 rndis_msg->msg.pkt.data_offset,
146 rndis_msg->msg.pkt.data_len,
147 rndis_msg->msg.pkt.num_oob_data_elements,
148 rndis_msg->msg.pkt.oob_data_offset,
149 rndis_msg->msg.pkt.oob_data_len,
150 rndis_msg->msg.pkt.per_pkt_info_offset,
151 rndis_msg->msg.pkt.per_pkt_info_len);
154 case RNDIS_MSG_INIT_C:
155 netdev_dbg(netdev, "RNDIS_MSG_INIT_C "
156 "(len %u, id 0x%x, status 0x%x, major %d, minor %d, "
157 "device flags %d, max xfer size 0x%x, max pkts %u, "
160 rndis_msg->msg.init_complete.req_id,
161 rndis_msg->msg.init_complete.status,
162 rndis_msg->msg.init_complete.major_ver,
163 rndis_msg->msg.init_complete.minor_ver,
164 rndis_msg->msg.init_complete.dev_flags,
165 rndis_msg->msg.init_complete.max_xfer_size,
166 rndis_msg->msg.init_complete.
168 rndis_msg->msg.init_complete.
169 pkt_alignment_factor);
172 case RNDIS_MSG_QUERY_C:
173 netdev_dbg(netdev, "RNDIS_MSG_QUERY_C "
174 "(len %u, id 0x%x, status 0x%x, buf len %u, "
177 rndis_msg->msg.query_complete.req_id,
178 rndis_msg->msg.query_complete.status,
179 rndis_msg->msg.query_complete.
181 rndis_msg->msg.query_complete.
185 case RNDIS_MSG_SET_C:
187 "RNDIS_MSG_SET_C (len %u, id 0x%x, status 0x%x)\n",
189 rndis_msg->msg.set_complete.req_id,
190 rndis_msg->msg.set_complete.status);
193 case RNDIS_MSG_INDICATE:
194 netdev_dbg(netdev, "RNDIS_MSG_INDICATE "
195 "(len %u, status 0x%x, buf len %u, buf offset %u)\n",
197 rndis_msg->msg.indicate_status.status,
198 rndis_msg->msg.indicate_status.status_buflen,
199 rndis_msg->msg.indicate_status.status_buf_offset);
203 netdev_dbg(netdev, "0x%x (len %u)\n",
204 rndis_msg->ndis_msg_type,
210 static int rndis_filter_send_request(struct rndis_device *dev,
211 struct rndis_request *req)
214 struct hv_netvsc_packet *packet;
216 /* Setup the packet to send it */
219 packet->is_data_pkt = false;
220 packet->total_data_buflen = req->request_msg.msg_len;
221 packet->page_buf_cnt = 1;
223 packet->page_buf[0].pfn = virt_to_phys(&req->request_msg) >>
225 packet->page_buf[0].len = req->request_msg.msg_len;
226 packet->page_buf[0].offset =
227 (unsigned long)&req->request_msg & (PAGE_SIZE - 1);
229 /* Add one page_buf when request_msg crossing page boundary */
230 if (packet->page_buf[0].offset + packet->page_buf[0].len > PAGE_SIZE) {
231 packet->page_buf_cnt++;
232 packet->page_buf[0].len = PAGE_SIZE -
233 packet->page_buf[0].offset;
234 packet->page_buf[1].pfn = virt_to_phys((void *)&req->request_msg
235 + packet->page_buf[0].len) >> PAGE_SHIFT;
236 packet->page_buf[1].offset = 0;
237 packet->page_buf[1].len = req->request_msg.msg_len -
238 packet->page_buf[0].len;
241 packet->completion.send.send_completion = NULL;
243 ret = netvsc_send(dev->net_dev->dev, packet);
247 static void rndis_filter_receive_response(struct rndis_device *dev,
248 struct rndis_message *resp)
250 struct rndis_request *request = NULL;
253 struct net_device *ndev;
255 ndev = dev->net_dev->ndev;
257 spin_lock_irqsave(&dev->request_lock, flags);
258 list_for_each_entry(request, &dev->req_list, list_ent) {
260 * All request/response message contains RequestId as the 1st
263 if (request->request_msg.msg.init_req.req_id
264 == resp->msg.init_complete.req_id) {
269 spin_unlock_irqrestore(&dev->request_lock, flags);
273 sizeof(struct rndis_message) + RNDIS_EXT_LEN) {
274 memcpy(&request->response_msg, resp,
278 "rndis response buffer overflow "
279 "detected (size %u max %zu)\n",
281 sizeof(struct rndis_filter_packet));
283 if (resp->ndis_msg_type ==
285 /* does not have a request id field */
286 request->response_msg.msg.reset_complete.
287 status = RNDIS_STATUS_BUFFER_OVERFLOW;
289 request->response_msg.msg.
290 init_complete.status =
291 RNDIS_STATUS_BUFFER_OVERFLOW;
295 complete(&request->wait_event);
298 "no rndis request found for this response "
299 "(id 0x%x res type 0x%x)\n",
300 resp->msg.init_complete.req_id,
301 resp->ndis_msg_type);
305 static void rndis_filter_receive_indicate_status(struct rndis_device *dev,
306 struct rndis_message *resp)
308 struct rndis_indicate_status *indicate =
309 &resp->msg.indicate_status;
311 if (indicate->status == RNDIS_STATUS_MEDIA_CONNECT) {
312 netvsc_linkstatus_callback(
313 dev->net_dev->dev, 1);
314 } else if (indicate->status == RNDIS_STATUS_MEDIA_DISCONNECT) {
315 netvsc_linkstatus_callback(
316 dev->net_dev->dev, 0);
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 void rndis_filter_receive_data(struct rndis_device *dev,
351 struct rndis_message *msg,
352 struct hv_netvsc_packet *pkt)
354 struct rndis_packet *rndis_pkt;
356 struct ndis_pkt_8021q_info *vlan;
358 rndis_pkt = &msg->msg.pkt;
360 /* Remove the rndis header and pass it back up the stack */
361 data_offset = RNDIS_HEADER_SIZE + rndis_pkt->data_offset;
363 pkt->total_data_buflen -= data_offset;
366 * Make sure we got a valid RNDIS message, now total_data_buflen
367 * should be the data packet size plus the trailer padding size
369 if (pkt->total_data_buflen < rndis_pkt->data_len) {
370 netdev_err(dev->net_dev->ndev, "rndis message buffer "
371 "overflow detected (got %u, min %u)"
372 "...dropping this message!\n",
373 pkt->total_data_buflen, rndis_pkt->data_len);
378 * Remove the rndis trailer padding from rndis packet message
379 * rndis_pkt->data_len tell us the real data length, we only copy
380 * the data packet to the stack, without the rndis trailer padding
382 pkt->total_data_buflen = rndis_pkt->data_len;
383 pkt->data = (void *)((unsigned long)pkt->data + data_offset);
385 pkt->is_data_pkt = true;
387 vlan = rndis_get_ppi(rndis_pkt, IEEE_8021Q_INFO);
389 pkt->vlan_tci = VLAN_TAG_PRESENT | vlan->vlanid |
390 (vlan->pri << VLAN_PRIO_SHIFT);
395 netvsc_recv_callback(dev->net_dev->dev, pkt);
398 int rndis_filter_receive(struct hv_device *dev,
399 struct hv_netvsc_packet *pkt)
401 struct netvsc_device *net_dev = hv_get_drvdata(dev);
402 struct rndis_device *rndis_dev;
403 struct rndis_message *rndis_msg;
404 struct net_device *ndev;
412 ndev = net_dev->ndev;
414 /* Make sure the rndis device state is initialized */
415 if (!net_dev->extension) {
416 netdev_err(ndev, "got rndis message but no rndis device - "
417 "dropping this message!\n");
422 rndis_dev = (struct rndis_device *)net_dev->extension;
423 if (rndis_dev->state == RNDIS_DEV_UNINITIALIZED) {
424 netdev_err(ndev, "got rndis message but rndis device "
425 "uninitialized...dropping this message!\n");
430 rndis_msg = pkt->data;
432 dump_rndis_message(dev, rndis_msg);
434 switch (rndis_msg->ndis_msg_type) {
435 case RNDIS_MSG_PACKET:
437 rndis_filter_receive_data(rndis_dev, rndis_msg, pkt);
440 case RNDIS_MSG_INIT_C:
441 case RNDIS_MSG_QUERY_C:
442 case RNDIS_MSG_SET_C:
443 /* completion msgs */
444 rndis_filter_receive_response(rndis_dev, rndis_msg);
447 case RNDIS_MSG_INDICATE:
448 /* notification msgs */
449 rndis_filter_receive_indicate_status(rndis_dev, rndis_msg);
453 "unhandled rndis message (type %u len %u)\n",
454 rndis_msg->ndis_msg_type,
461 pkt->status = NVSP_STAT_FAIL;
466 static int rndis_filter_query_device(struct rndis_device *dev, u32 oid,
467 void *result, u32 *result_size)
469 struct rndis_request *request;
470 u32 inresult_size = *result_size;
471 struct rndis_query_request *query;
472 struct rndis_query_complete *query_complete;
480 request = get_rndis_request(dev, RNDIS_MSG_QUERY,
481 RNDIS_MESSAGE_SIZE(struct rndis_query_request));
487 /* Setup the rndis query */
488 query = &request->request_msg.msg.query_req;
490 query->info_buf_offset = sizeof(struct rndis_query_request);
491 query->info_buflen = 0;
492 query->dev_vc_handle = 0;
494 ret = rndis_filter_send_request(dev, request);
498 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
504 /* Copy the response back */
505 query_complete = &request->response_msg.msg.query_complete;
507 if (query_complete->info_buflen > inresult_size) {
513 (void *)((unsigned long)query_complete +
514 query_complete->info_buf_offset),
515 query_complete->info_buflen);
517 *result_size = query_complete->info_buflen;
521 put_rndis_request(dev, request);
526 static int rndis_filter_query_device_mac(struct rndis_device *dev)
530 return rndis_filter_query_device(dev,
531 RNDIS_OID_802_3_PERMANENT_ADDRESS,
532 dev->hw_mac_adr, &size);
535 #define NWADR_STR "NetworkAddress"
536 #define NWADR_STRLEN 14
538 int rndis_filter_set_device_mac(struct hv_device *hdev, char *mac)
540 struct netvsc_device *nvdev = hv_get_drvdata(hdev);
541 struct rndis_device *rdev = nvdev->extension;
542 struct net_device *ndev = nvdev->ndev;
543 struct rndis_request *request;
544 struct rndis_set_request *set;
545 struct rndis_config_parameter_info *cpi;
546 wchar_t *cfg_nwadr, *cfg_mac;
547 struct rndis_set_complete *set_complete;
548 char macstr[2*ETH_ALEN+1];
549 u32 extlen = sizeof(struct rndis_config_parameter_info) +
550 2*NWADR_STRLEN + 4*ETH_ALEN;
553 request = get_rndis_request(rdev, RNDIS_MSG_SET,
554 RNDIS_MESSAGE_SIZE(struct rndis_set_request) + extlen);
558 set = &request->request_msg.msg.set_req;
559 set->oid = RNDIS_OID_GEN_RNDIS_CONFIG_PARAMETER;
560 set->info_buflen = extlen;
561 set->info_buf_offset = sizeof(struct rndis_set_request);
562 set->dev_vc_handle = 0;
564 cpi = (struct rndis_config_parameter_info *)((ulong)set +
565 set->info_buf_offset);
566 cpi->parameter_name_offset =
567 sizeof(struct rndis_config_parameter_info);
568 /* Multiply by 2 because host needs 2 bytes (utf16) for each char */
569 cpi->parameter_name_length = 2*NWADR_STRLEN;
570 cpi->parameter_type = RNDIS_CONFIG_PARAM_TYPE_STRING;
571 cpi->parameter_value_offset =
572 cpi->parameter_name_offset + cpi->parameter_name_length;
573 /* Multiply by 4 because each MAC byte displayed as 2 utf16 chars */
574 cpi->parameter_value_length = 4*ETH_ALEN;
576 cfg_nwadr = (wchar_t *)((ulong)cpi + cpi->parameter_name_offset);
577 cfg_mac = (wchar_t *)((ulong)cpi + cpi->parameter_value_offset);
578 ret = utf8s_to_utf16s(NWADR_STR, NWADR_STRLEN, UTF16_HOST_ENDIAN,
579 cfg_nwadr, NWADR_STRLEN);
582 snprintf(macstr, 2*ETH_ALEN+1, "%pm", mac);
583 ret = utf8s_to_utf16s(macstr, 2*ETH_ALEN, UTF16_HOST_ENDIAN,
584 cfg_mac, 2*ETH_ALEN);
588 ret = rndis_filter_send_request(rdev, request);
592 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
594 netdev_err(ndev, "timeout before we got a set response...\n");
596 * can't put_rndis_request, since we may still receive a
601 set_complete = &request->response_msg.msg.set_complete;
602 if (set_complete->status != RNDIS_STATUS_SUCCESS) {
603 netdev_err(ndev, "Fail to set MAC on host side:0x%x\n",
604 set_complete->status);
610 put_rndis_request(rdev, request);
615 static int rndis_filter_query_device_link_status(struct rndis_device *dev)
617 u32 size = sizeof(u32);
621 ret = rndis_filter_query_device(dev,
622 RNDIS_OID_GEN_MEDIA_CONNECT_STATUS,
623 &link_status, &size);
624 dev->link_state = (link_status != 0) ? true : false;
629 int rndis_filter_set_packet_filter(struct rndis_device *dev, u32 new_filter)
631 struct rndis_request *request;
632 struct rndis_set_request *set;
633 struct rndis_set_complete *set_complete;
636 struct net_device *ndev;
638 ndev = dev->net_dev->ndev;
640 request = get_rndis_request(dev, RNDIS_MSG_SET,
641 RNDIS_MESSAGE_SIZE(struct rndis_set_request) +
648 /* Setup the rndis set */
649 set = &request->request_msg.msg.set_req;
650 set->oid = RNDIS_OID_GEN_CURRENT_PACKET_FILTER;
651 set->info_buflen = sizeof(u32);
652 set->info_buf_offset = sizeof(struct rndis_set_request);
654 memcpy((void *)(unsigned long)set + sizeof(struct rndis_set_request),
655 &new_filter, sizeof(u32));
657 ret = rndis_filter_send_request(dev, request);
661 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
665 "timeout before we got a set response...\n");
668 * We can't deallocate the request since we may still receive a
669 * send completion for it.
673 set_complete = &request->response_msg.msg.set_complete;
674 status = set_complete->status;
679 put_rndis_request(dev, request);
685 static int rndis_filter_init_device(struct rndis_device *dev)
687 struct rndis_request *request;
688 struct rndis_initialize_request *init;
689 struct rndis_initialize_complete *init_complete;
693 request = get_rndis_request(dev, RNDIS_MSG_INIT,
694 RNDIS_MESSAGE_SIZE(struct rndis_initialize_request));
700 /* Setup the rndis set */
701 init = &request->request_msg.msg.init_req;
702 init->major_ver = RNDIS_MAJOR_VERSION;
703 init->minor_ver = RNDIS_MINOR_VERSION;
704 init->max_xfer_size = 0x4000;
706 dev->state = RNDIS_DEV_INITIALIZING;
708 ret = rndis_filter_send_request(dev, request);
710 dev->state = RNDIS_DEV_UNINITIALIZED;
715 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
722 init_complete = &request->response_msg.msg.init_complete;
723 status = init_complete->status;
724 if (status == RNDIS_STATUS_SUCCESS) {
725 dev->state = RNDIS_DEV_INITIALIZED;
728 dev->state = RNDIS_DEV_UNINITIALIZED;
734 put_rndis_request(dev, request);
739 static void rndis_filter_halt_device(struct rndis_device *dev)
741 struct rndis_request *request;
742 struct rndis_halt_request *halt;
743 struct netvsc_device *nvdev = dev->net_dev;
744 struct hv_device *hdev = nvdev->dev;
747 /* Attempt to do a rndis device halt */
748 request = get_rndis_request(dev, RNDIS_MSG_HALT,
749 RNDIS_MESSAGE_SIZE(struct rndis_halt_request));
753 /* Setup the rndis set */
754 halt = &request->request_msg.msg.halt_req;
755 halt->req_id = atomic_inc_return(&dev->new_req_id);
757 /* Ignore return since this msg is optional. */
758 rndis_filter_send_request(dev, request);
760 dev->state = RNDIS_DEV_UNINITIALIZED;
763 spin_lock_irqsave(&hdev->channel->inbound_lock, flags);
764 nvdev->destroy = true;
765 spin_unlock_irqrestore(&hdev->channel->inbound_lock, flags);
767 /* Wait for all send completions */
768 wait_event(nvdev->wait_drain,
769 atomic_read(&nvdev->num_outstanding_sends) == 0);
772 put_rndis_request(dev, request);
776 static int rndis_filter_open_device(struct rndis_device *dev)
780 if (dev->state != RNDIS_DEV_INITIALIZED)
783 ret = rndis_filter_set_packet_filter(dev,
784 NDIS_PACKET_TYPE_BROADCAST |
785 NDIS_PACKET_TYPE_ALL_MULTICAST |
786 NDIS_PACKET_TYPE_DIRECTED);
788 dev->state = RNDIS_DEV_DATAINITIALIZED;
793 static int rndis_filter_close_device(struct rndis_device *dev)
797 if (dev->state != RNDIS_DEV_DATAINITIALIZED)
800 ret = rndis_filter_set_packet_filter(dev, 0);
802 dev->state = RNDIS_DEV_INITIALIZED;
807 int rndis_filter_device_add(struct hv_device *dev,
808 void *additional_info)
811 struct netvsc_device *net_device;
812 struct rndis_device *rndis_device;
813 struct netvsc_device_info *device_info = additional_info;
815 rndis_device = get_rndis_device();
820 * Let the inner driver handle this first to create the netvsc channel
821 * NOTE! Once the channel is created, we may get a receive callback
822 * (RndisFilterOnReceive()) before this call is completed
824 ret = netvsc_device_add(dev, additional_info);
831 /* Initialize the rndis device */
832 net_device = hv_get_drvdata(dev);
834 net_device->extension = rndis_device;
835 rndis_device->net_dev = net_device;
837 /* Send the rndis initialization message */
838 ret = rndis_filter_init_device(rndis_device);
840 rndis_filter_device_remove(dev);
844 /* Get the mac address */
845 ret = rndis_filter_query_device_mac(rndis_device);
847 rndis_filter_device_remove(dev);
851 memcpy(device_info->mac_adr, rndis_device->hw_mac_adr, ETH_ALEN);
853 rndis_filter_query_device_link_status(rndis_device);
855 device_info->link_state = rndis_device->link_state;
857 dev_info(&dev->device, "Device MAC %pM link state %s\n",
858 rndis_device->hw_mac_adr,
859 device_info->link_state ? "down" : "up");
864 void rndis_filter_device_remove(struct hv_device *dev)
866 struct netvsc_device *net_dev = hv_get_drvdata(dev);
867 struct rndis_device *rndis_dev = net_dev->extension;
869 /* Halt and release the rndis device */
870 rndis_filter_halt_device(rndis_dev);
873 net_dev->extension = NULL;
875 netvsc_device_remove(dev);
879 int rndis_filter_open(struct hv_device *dev)
881 struct netvsc_device *net_device = hv_get_drvdata(dev);
886 return rndis_filter_open_device(net_device->extension);
889 int rndis_filter_close(struct hv_device *dev)
891 struct netvsc_device *nvdev = hv_get_drvdata(dev);
896 return rndis_filter_close_device(nvdev->extension);
899 int rndis_filter_send(struct hv_device *dev,
900 struct hv_netvsc_packet *pkt)
903 struct rndis_filter_packet *filter_pkt;
904 struct rndis_message *rndis_msg;
905 struct rndis_packet *rndis_pkt;
907 bool isvlan = pkt->vlan_tci & VLAN_TAG_PRESENT;
909 /* Add the rndis header */
910 filter_pkt = (struct rndis_filter_packet *)pkt->extension;
912 rndis_msg = &filter_pkt->msg;
913 rndis_msg_size = RNDIS_MESSAGE_SIZE(struct rndis_packet);
915 rndis_msg_size += NDIS_VLAN_PPI_SIZE;
917 rndis_msg->ndis_msg_type = RNDIS_MSG_PACKET;
918 rndis_msg->msg_len = pkt->total_data_buflen +
921 rndis_pkt = &rndis_msg->msg.pkt;
922 rndis_pkt->data_offset = sizeof(struct rndis_packet);
924 rndis_pkt->data_offset += NDIS_VLAN_PPI_SIZE;
925 rndis_pkt->data_len = pkt->total_data_buflen;
928 struct rndis_per_packet_info *ppi;
929 struct ndis_pkt_8021q_info *vlan;
931 rndis_pkt->per_pkt_info_offset = sizeof(struct rndis_packet);
932 rndis_pkt->per_pkt_info_len = NDIS_VLAN_PPI_SIZE;
934 ppi = (struct rndis_per_packet_info *)((ulong)rndis_pkt +
935 rndis_pkt->per_pkt_info_offset);
936 ppi->size = NDIS_VLAN_PPI_SIZE;
937 ppi->type = IEEE_8021Q_INFO;
938 ppi->ppi_offset = sizeof(struct rndis_per_packet_info);
940 vlan = (struct ndis_pkt_8021q_info *)((ulong)ppi +
942 vlan->vlanid = pkt->vlan_tci & VLAN_VID_MASK;
943 vlan->pri = (pkt->vlan_tci & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
946 pkt->is_data_pkt = true;
947 pkt->page_buf[0].pfn = virt_to_phys(rndis_msg) >> PAGE_SHIFT;
948 pkt->page_buf[0].offset =
949 (unsigned long)rndis_msg & (PAGE_SIZE-1);
950 pkt->page_buf[0].len = rndis_msg_size;
952 /* Add one page_buf if the rndis msg goes beyond page boundary */
953 if (pkt->page_buf[0].offset + rndis_msg_size > PAGE_SIZE) {
955 for (i = pkt->page_buf_cnt; i > 1; i--)
956 pkt->page_buf[i] = pkt->page_buf[i-1];
958 pkt->page_buf[0].len = PAGE_SIZE - pkt->page_buf[0].offset;
959 pkt->page_buf[1].pfn = virt_to_phys((void *)((ulong)
960 rndis_msg + pkt->page_buf[0].len)) >> PAGE_SHIFT;
961 pkt->page_buf[1].offset = 0;
962 pkt->page_buf[1].len = rndis_msg_size - pkt->page_buf[0].len;
965 /* Save the packet send completion and context */
966 filter_pkt->completion = pkt->completion.send.send_completion;
967 filter_pkt->completion_ctx =
968 pkt->completion.send.send_completion_ctx;
971 pkt->completion.send.send_completion = rndis_filter_send_completion;
972 pkt->completion.send.send_completion_ctx = filter_pkt;
974 ret = netvsc_send(dev, pkt);
977 * Reset the completion to originals to allow retries from
980 pkt->completion.send.send_completion =
981 filter_pkt->completion;
982 pkt->completion.send.send_completion_ctx =
983 filter_pkt->completion_ctx;
989 static void rndis_filter_send_completion(void *ctx)
991 struct rndis_filter_packet *filter_pkt = ctx;
993 /* Pass it back to the original handler */
994 filter_pkt->completion(filter_pkt->completion_ctx);