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>
31 #include "hyperv_net.h"
34 struct rndis_request {
35 struct list_head list_ent;
36 struct completion wait_event;
39 * FIXME: We assumed a fixed size response here. If we do ever need to
40 * handle a bigger response, we can either define a max response
41 * message or add a response buffer variable above this field
43 struct rndis_message response_msg;
45 /* Simplify allocation by having a netvsc packet inline */
46 struct hv_netvsc_packet pkt;
47 struct hv_page_buffer buf;
48 /* FIXME: We assumed a fixed size request here. */
49 struct rndis_message request_msg;
52 static void rndis_filter_send_completion(void *ctx);
54 static void rndis_filter_send_request_completion(void *ctx);
58 static struct rndis_device *get_rndis_device(void)
60 struct rndis_device *device;
62 device = kzalloc(sizeof(struct rndis_device), GFP_KERNEL);
66 spin_lock_init(&device->request_lock);
68 INIT_LIST_HEAD(&device->req_list);
70 device->state = RNDIS_DEV_UNINITIALIZED;
75 static struct rndis_request *get_rndis_request(struct rndis_device *dev,
79 struct rndis_request *request;
80 struct rndis_message *rndis_msg;
81 struct rndis_set_request *set;
84 request = kzalloc(sizeof(struct rndis_request), GFP_KERNEL);
88 init_completion(&request->wait_event);
90 rndis_msg = &request->request_msg;
91 rndis_msg->ndis_msg_type = msg_type;
92 rndis_msg->msg_len = msg_len;
95 * Set the request id. This field is always after the rndis header for
96 * request/response packet types so we just used the SetRequest as a
99 set = &rndis_msg->msg.set_req;
100 set->req_id = atomic_inc_return(&dev->new_req_id);
102 /* Add to the request list */
103 spin_lock_irqsave(&dev->request_lock, flags);
104 list_add_tail(&request->list_ent, &dev->req_list);
105 spin_unlock_irqrestore(&dev->request_lock, flags);
110 static void put_rndis_request(struct rndis_device *dev,
111 struct rndis_request *req)
115 spin_lock_irqsave(&dev->request_lock, flags);
116 list_del(&req->list_ent);
117 spin_unlock_irqrestore(&dev->request_lock, flags);
122 static void dump_rndis_message(struct hv_device *hv_dev,
123 struct rndis_message *rndis_msg)
125 struct net_device *netdev;
126 struct netvsc_device *net_device;
128 net_device = hv_get_drvdata(hv_dev);
129 netdev = net_device->ndev;
131 switch (rndis_msg->ndis_msg_type) {
132 case RNDIS_MSG_PACKET:
133 netdev_dbg(netdev, "RNDIS_MSG_PACKET (len %u, "
134 "data offset %u data len %u, # oob %u, "
135 "oob offset %u, oob len %u, pkt offset %u, "
138 rndis_msg->msg.pkt.data_offset,
139 rndis_msg->msg.pkt.data_len,
140 rndis_msg->msg.pkt.num_oob_data_elements,
141 rndis_msg->msg.pkt.oob_data_offset,
142 rndis_msg->msg.pkt.oob_data_len,
143 rndis_msg->msg.pkt.per_pkt_info_offset,
144 rndis_msg->msg.pkt.per_pkt_info_len);
147 case RNDIS_MSG_INIT_C:
148 netdev_dbg(netdev, "RNDIS_MSG_INIT_C "
149 "(len %u, id 0x%x, status 0x%x, major %d, minor %d, "
150 "device flags %d, max xfer size 0x%x, max pkts %u, "
153 rndis_msg->msg.init_complete.req_id,
154 rndis_msg->msg.init_complete.status,
155 rndis_msg->msg.init_complete.major_ver,
156 rndis_msg->msg.init_complete.minor_ver,
157 rndis_msg->msg.init_complete.dev_flags,
158 rndis_msg->msg.init_complete.max_xfer_size,
159 rndis_msg->msg.init_complete.
161 rndis_msg->msg.init_complete.
162 pkt_alignment_factor);
165 case RNDIS_MSG_QUERY_C:
166 netdev_dbg(netdev, "RNDIS_MSG_QUERY_C "
167 "(len %u, id 0x%x, status 0x%x, buf len %u, "
170 rndis_msg->msg.query_complete.req_id,
171 rndis_msg->msg.query_complete.status,
172 rndis_msg->msg.query_complete.
174 rndis_msg->msg.query_complete.
178 case RNDIS_MSG_SET_C:
180 "RNDIS_MSG_SET_C (len %u, id 0x%x, status 0x%x)\n",
182 rndis_msg->msg.set_complete.req_id,
183 rndis_msg->msg.set_complete.status);
186 case RNDIS_MSG_INDICATE:
187 netdev_dbg(netdev, "RNDIS_MSG_INDICATE "
188 "(len %u, status 0x%x, buf len %u, buf offset %u)\n",
190 rndis_msg->msg.indicate_status.status,
191 rndis_msg->msg.indicate_status.status_buflen,
192 rndis_msg->msg.indicate_status.status_buf_offset);
196 netdev_dbg(netdev, "0x%x (len %u)\n",
197 rndis_msg->ndis_msg_type,
203 static int rndis_filter_send_request(struct rndis_device *dev,
204 struct rndis_request *req)
207 struct hv_netvsc_packet *packet;
209 /* Setup the packet to send it */
212 packet->is_data_pkt = false;
213 packet->total_data_buflen = req->request_msg.msg_len;
214 packet->page_buf_cnt = 1;
216 packet->page_buf[0].pfn = virt_to_phys(&req->request_msg) >>
218 packet->page_buf[0].len = req->request_msg.msg_len;
219 packet->page_buf[0].offset =
220 (unsigned long)&req->request_msg & (PAGE_SIZE - 1);
222 packet->completion.send.send_completion_ctx = req;/* packet; */
223 packet->completion.send.send_completion =
224 rndis_filter_send_request_completion;
225 packet->completion.send.send_completion_tid = (unsigned long)dev;
227 ret = netvsc_send(dev->net_dev->dev, packet);
231 static void rndis_filter_receive_response(struct rndis_device *dev,
232 struct rndis_message *resp)
234 struct rndis_request *request = NULL;
237 struct net_device *ndev;
239 ndev = dev->net_dev->ndev;
241 spin_lock_irqsave(&dev->request_lock, flags);
242 list_for_each_entry(request, &dev->req_list, list_ent) {
244 * All request/response message contains RequestId as the 1st
247 if (request->request_msg.msg.init_req.req_id
248 == resp->msg.init_complete.req_id) {
253 spin_unlock_irqrestore(&dev->request_lock, flags);
256 if (resp->msg_len <= sizeof(struct rndis_message)) {
257 memcpy(&request->response_msg, resp,
261 "rndis response buffer overflow "
262 "detected (size %u max %zu)\n",
264 sizeof(struct rndis_filter_packet));
266 if (resp->ndis_msg_type ==
268 /* does not have a request id field */
269 request->response_msg.msg.reset_complete.
270 status = RNDIS_STATUS_BUFFER_OVERFLOW;
272 request->response_msg.msg.
273 init_complete.status =
274 RNDIS_STATUS_BUFFER_OVERFLOW;
278 complete(&request->wait_event);
281 "no rndis request found for this response "
282 "(id 0x%x res type 0x%x)\n",
283 resp->msg.init_complete.req_id,
284 resp->ndis_msg_type);
288 static void rndis_filter_receive_indicate_status(struct rndis_device *dev,
289 struct rndis_message *resp)
291 struct rndis_indicate_status *indicate =
292 &resp->msg.indicate_status;
294 if (indicate->status == RNDIS_STATUS_MEDIA_CONNECT) {
295 netvsc_linkstatus_callback(
296 dev->net_dev->dev, 1);
297 } else if (indicate->status == RNDIS_STATUS_MEDIA_DISCONNECT) {
298 netvsc_linkstatus_callback(
299 dev->net_dev->dev, 0);
308 * Get the Per-Packet-Info with the specified type
309 * return NULL if not found.
311 static inline void *rndis_get_ppi(struct rndis_packet *rpkt, u32 type)
313 struct rndis_per_packet_info *ppi;
316 if (rpkt->per_pkt_info_offset == 0)
319 ppi = (struct rndis_per_packet_info *)((ulong)rpkt +
320 rpkt->per_pkt_info_offset);
321 len = rpkt->per_pkt_info_len;
324 if (ppi->type == type)
325 return (void *)((ulong)ppi + ppi->ppi_offset);
327 ppi = (struct rndis_per_packet_info *)((ulong)ppi + ppi->size);
333 static void rndis_filter_receive_data(struct rndis_device *dev,
334 struct rndis_message *msg,
335 struct hv_netvsc_packet *pkt)
337 struct rndis_packet *rndis_pkt;
339 struct ndis_pkt_8021q_info *vlan;
341 rndis_pkt = &msg->msg.pkt;
344 * FIXME: Handle multiple rndis pkt msgs that maybe enclosed in this
345 * netvsc packet (ie TotalDataBufferLength != MessageLength)
348 /* Remove the rndis header and pass it back up the stack */
349 data_offset = RNDIS_HEADER_SIZE + rndis_pkt->data_offset;
351 pkt->total_data_buflen -= data_offset;
354 * Make sure we got a valid RNDIS message, now total_data_buflen
355 * should be the data packet size plus the trailer padding size
357 if (pkt->total_data_buflen < rndis_pkt->data_len) {
358 netdev_err(dev->net_dev->ndev, "rndis message buffer "
359 "overflow detected (got %u, min %u)"
360 "...dropping this message!\n",
361 pkt->total_data_buflen, rndis_pkt->data_len);
366 * Remove the rndis trailer padding from rndis packet message
367 * rndis_pkt->data_len tell us the real data length, we only copy
368 * the data packet to the stack, without the rndis trailer padding
370 pkt->total_data_buflen = rndis_pkt->data_len;
371 pkt->data = (void *)((unsigned long)pkt->data + data_offset);
373 pkt->is_data_pkt = true;
375 vlan = rndis_get_ppi(rndis_pkt, IEEE_8021Q_INFO);
377 pkt->vlan_tci = VLAN_TAG_PRESENT | vlan->vlanid |
378 (vlan->pri << VLAN_PRIO_SHIFT);
383 netvsc_recv_callback(dev->net_dev->dev, pkt);
386 int rndis_filter_receive(struct hv_device *dev,
387 struct hv_netvsc_packet *pkt)
389 struct netvsc_device *net_dev = hv_get_drvdata(dev);
390 struct rndis_device *rndis_dev;
391 struct rndis_message *rndis_msg;
392 struct net_device *ndev;
397 ndev = net_dev->ndev;
399 /* Make sure the rndis device state is initialized */
400 if (!net_dev->extension) {
401 netdev_err(ndev, "got rndis message but no rndis device - "
402 "dropping this message!\n");
406 rndis_dev = (struct rndis_device *)net_dev->extension;
407 if (rndis_dev->state == RNDIS_DEV_UNINITIALIZED) {
408 netdev_err(ndev, "got rndis message but rndis device "
409 "uninitialized...dropping this message!\n");
413 rndis_msg = pkt->data;
415 dump_rndis_message(dev, rndis_msg);
417 switch (rndis_msg->ndis_msg_type) {
418 case RNDIS_MSG_PACKET:
420 rndis_filter_receive_data(rndis_dev, rndis_msg, pkt);
423 case RNDIS_MSG_INIT_C:
424 case RNDIS_MSG_QUERY_C:
425 case RNDIS_MSG_SET_C:
426 /* completion msgs */
427 rndis_filter_receive_response(rndis_dev, rndis_msg);
430 case RNDIS_MSG_INDICATE:
431 /* notification msgs */
432 rndis_filter_receive_indicate_status(rndis_dev, rndis_msg);
436 "unhandled rndis message (type %u len %u)\n",
437 rndis_msg->ndis_msg_type,
445 static int rndis_filter_query_device(struct rndis_device *dev, u32 oid,
446 void *result, u32 *result_size)
448 struct rndis_request *request;
449 u32 inresult_size = *result_size;
450 struct rndis_query_request *query;
451 struct rndis_query_complete *query_complete;
459 request = get_rndis_request(dev, RNDIS_MSG_QUERY,
460 RNDIS_MESSAGE_SIZE(struct rndis_query_request));
466 /* Setup the rndis query */
467 query = &request->request_msg.msg.query_req;
469 query->info_buf_offset = sizeof(struct rndis_query_request);
470 query->info_buflen = 0;
471 query->dev_vc_handle = 0;
473 ret = rndis_filter_send_request(dev, request);
477 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
483 /* Copy the response back */
484 query_complete = &request->response_msg.msg.query_complete;
486 if (query_complete->info_buflen > inresult_size) {
492 (void *)((unsigned long)query_complete +
493 query_complete->info_buf_offset),
494 query_complete->info_buflen);
496 *result_size = query_complete->info_buflen;
500 put_rndis_request(dev, request);
505 static int rndis_filter_query_device_mac(struct rndis_device *dev)
509 return rndis_filter_query_device(dev,
510 RNDIS_OID_802_3_PERMANENT_ADDRESS,
511 dev->hw_mac_adr, &size);
514 static int rndis_filter_query_device_link_status(struct rndis_device *dev)
516 u32 size = sizeof(u32);
520 ret = rndis_filter_query_device(dev,
521 RNDIS_OID_GEN_MEDIA_CONNECT_STATUS,
522 &link_status, &size);
523 dev->link_state = (link_status != 0) ? true : false;
528 int rndis_filter_set_packet_filter(struct rndis_device *dev, u32 new_filter)
530 struct rndis_request *request;
531 struct rndis_set_request *set;
532 struct rndis_set_complete *set_complete;
535 struct net_device *ndev;
537 ndev = dev->net_dev->ndev;
539 request = get_rndis_request(dev, RNDIS_MSG_SET,
540 RNDIS_MESSAGE_SIZE(struct rndis_set_request) +
547 /* Setup the rndis set */
548 set = &request->request_msg.msg.set_req;
549 set->oid = RNDIS_OID_GEN_CURRENT_PACKET_FILTER;
550 set->info_buflen = sizeof(u32);
551 set->info_buf_offset = sizeof(struct rndis_set_request);
553 memcpy((void *)(unsigned long)set + sizeof(struct rndis_set_request),
554 &new_filter, sizeof(u32));
556 ret = rndis_filter_send_request(dev, request);
560 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
564 "timeout before we got a set response...\n");
566 * We can't deallocate the request since we may still receive a
567 * send completion for it.
571 set_complete = &request->response_msg.msg.set_complete;
572 status = set_complete->status;
577 put_rndis_request(dev, request);
583 static int rndis_filter_init_device(struct rndis_device *dev)
585 struct rndis_request *request;
586 struct rndis_initialize_request *init;
587 struct rndis_initialize_complete *init_complete;
591 request = get_rndis_request(dev, RNDIS_MSG_INIT,
592 RNDIS_MESSAGE_SIZE(struct rndis_initialize_request));
598 /* Setup the rndis set */
599 init = &request->request_msg.msg.init_req;
600 init->major_ver = RNDIS_MAJOR_VERSION;
601 init->minor_ver = RNDIS_MINOR_VERSION;
602 /* FIXME: Use 1536 - rounded ethernet frame size */
603 init->max_xfer_size = 2048;
605 dev->state = RNDIS_DEV_INITIALIZING;
607 ret = rndis_filter_send_request(dev, request);
609 dev->state = RNDIS_DEV_UNINITIALIZED;
614 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
621 init_complete = &request->response_msg.msg.init_complete;
622 status = init_complete->status;
623 if (status == RNDIS_STATUS_SUCCESS) {
624 dev->state = RNDIS_DEV_INITIALIZED;
627 dev->state = RNDIS_DEV_UNINITIALIZED;
633 put_rndis_request(dev, request);
638 static void rndis_filter_halt_device(struct rndis_device *dev)
640 struct rndis_request *request;
641 struct rndis_halt_request *halt;
643 /* Attempt to do a rndis device halt */
644 request = get_rndis_request(dev, RNDIS_MSG_HALT,
645 RNDIS_MESSAGE_SIZE(struct rndis_halt_request));
649 /* Setup the rndis set */
650 halt = &request->request_msg.msg.halt_req;
651 halt->req_id = atomic_inc_return(&dev->new_req_id);
653 /* Ignore return since this msg is optional. */
654 rndis_filter_send_request(dev, request);
656 dev->state = RNDIS_DEV_UNINITIALIZED;
660 put_rndis_request(dev, request);
664 static int rndis_filter_open_device(struct rndis_device *dev)
668 if (dev->state != RNDIS_DEV_INITIALIZED)
671 ret = rndis_filter_set_packet_filter(dev,
672 NDIS_PACKET_TYPE_BROADCAST |
673 NDIS_PACKET_TYPE_ALL_MULTICAST |
674 NDIS_PACKET_TYPE_DIRECTED);
676 dev->state = RNDIS_DEV_DATAINITIALIZED;
681 static int rndis_filter_close_device(struct rndis_device *dev)
685 if (dev->state != RNDIS_DEV_DATAINITIALIZED)
688 ret = rndis_filter_set_packet_filter(dev, 0);
690 dev->state = RNDIS_DEV_INITIALIZED;
695 int rndis_filter_device_add(struct hv_device *dev,
696 void *additional_info)
699 struct netvsc_device *net_device;
700 struct rndis_device *rndis_device;
701 struct netvsc_device_info *device_info = additional_info;
703 rndis_device = get_rndis_device();
708 * Let the inner driver handle this first to create the netvsc channel
709 * NOTE! Once the channel is created, we may get a receive callback
710 * (RndisFilterOnReceive()) before this call is completed
712 ret = netvsc_device_add(dev, additional_info);
719 /* Initialize the rndis device */
720 net_device = hv_get_drvdata(dev);
722 net_device->extension = rndis_device;
723 rndis_device->net_dev = net_device;
725 /* Send the rndis initialization message */
726 ret = rndis_filter_init_device(rndis_device);
729 * TODO: If rndis init failed, we will need to shut down the
734 /* Get the mac address */
735 ret = rndis_filter_query_device_mac(rndis_device);
738 * TODO: shutdown rndis device and the channel
742 memcpy(device_info->mac_adr, rndis_device->hw_mac_adr, ETH_ALEN);
744 rndis_filter_query_device_link_status(rndis_device);
746 device_info->link_state = rndis_device->link_state;
748 dev_info(&dev->device, "Device MAC %pM link state %s\n",
749 rndis_device->hw_mac_adr,
750 device_info->link_state ? "down" : "up");
755 void rndis_filter_device_remove(struct hv_device *dev)
757 struct netvsc_device *net_dev = hv_get_drvdata(dev);
758 struct rndis_device *rndis_dev = net_dev->extension;
760 /* Halt and release the rndis device */
761 rndis_filter_halt_device(rndis_dev);
764 net_dev->extension = NULL;
766 netvsc_device_remove(dev);
770 int rndis_filter_open(struct hv_device *dev)
772 struct netvsc_device *net_device = hv_get_drvdata(dev);
777 return rndis_filter_open_device(net_device->extension);
780 int rndis_filter_close(struct hv_device *dev)
782 struct netvsc_device *nvdev = hv_get_drvdata(dev);
787 return rndis_filter_close_device(nvdev->extension);
790 int rndis_filter_send(struct hv_device *dev,
791 struct hv_netvsc_packet *pkt)
794 struct rndis_filter_packet *filter_pkt;
795 struct rndis_message *rndis_msg;
796 struct rndis_packet *rndis_pkt;
798 bool isvlan = pkt->vlan_tci & VLAN_TAG_PRESENT;
800 /* Add the rndis header */
801 filter_pkt = (struct rndis_filter_packet *)pkt->extension;
803 rndis_msg = &filter_pkt->msg;
804 rndis_msg_size = RNDIS_MESSAGE_SIZE(struct rndis_packet);
806 rndis_msg_size += NDIS_VLAN_PPI_SIZE;
808 rndis_msg->ndis_msg_type = RNDIS_MSG_PACKET;
809 rndis_msg->msg_len = pkt->total_data_buflen +
812 rndis_pkt = &rndis_msg->msg.pkt;
813 rndis_pkt->data_offset = sizeof(struct rndis_packet);
815 rndis_pkt->data_offset += NDIS_VLAN_PPI_SIZE;
816 rndis_pkt->data_len = pkt->total_data_buflen;
819 struct rndis_per_packet_info *ppi;
820 struct ndis_pkt_8021q_info *vlan;
822 rndis_pkt->per_pkt_info_offset = sizeof(struct rndis_packet);
823 rndis_pkt->per_pkt_info_len = NDIS_VLAN_PPI_SIZE;
825 ppi = (struct rndis_per_packet_info *)((ulong)rndis_pkt +
826 rndis_pkt->per_pkt_info_offset);
827 ppi->size = NDIS_VLAN_PPI_SIZE;
828 ppi->type = IEEE_8021Q_INFO;
829 ppi->ppi_offset = sizeof(struct rndis_per_packet_info);
831 vlan = (struct ndis_pkt_8021q_info *)((ulong)ppi +
833 vlan->vlanid = pkt->vlan_tci & VLAN_VID_MASK;
834 vlan->pri = (pkt->vlan_tci & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
837 pkt->is_data_pkt = true;
838 pkt->page_buf[0].pfn = virt_to_phys(rndis_msg) >> PAGE_SHIFT;
839 pkt->page_buf[0].offset =
840 (unsigned long)rndis_msg & (PAGE_SIZE-1);
841 pkt->page_buf[0].len = rndis_msg_size;
843 /* Add one page_buf if the rndis msg goes beyond page boundary */
844 if (pkt->page_buf[0].offset + rndis_msg_size > PAGE_SIZE) {
846 for (i = pkt->page_buf_cnt; i > 1; i--)
847 pkt->page_buf[i] = pkt->page_buf[i-1];
849 pkt->page_buf[0].len = PAGE_SIZE - pkt->page_buf[0].offset;
850 pkt->page_buf[1].pfn = virt_to_phys((void *)((ulong)
851 rndis_msg + pkt->page_buf[0].len)) >> PAGE_SHIFT;
852 pkt->page_buf[1].offset = 0;
853 pkt->page_buf[1].len = rndis_msg_size - pkt->page_buf[0].len;
856 /* Save the packet send completion and context */
857 filter_pkt->completion = pkt->completion.send.send_completion;
858 filter_pkt->completion_ctx =
859 pkt->completion.send.send_completion_ctx;
862 pkt->completion.send.send_completion = rndis_filter_send_completion;
863 pkt->completion.send.send_completion_ctx = filter_pkt;
865 ret = netvsc_send(dev, pkt);
868 * Reset the completion to originals to allow retries from
871 pkt->completion.send.send_completion =
872 filter_pkt->completion;
873 pkt->completion.send.send_completion_ctx =
874 filter_pkt->completion_ctx;
880 static void rndis_filter_send_completion(void *ctx)
882 struct rndis_filter_packet *filter_pkt = ctx;
884 /* Pass it back to the original handler */
885 filter_pkt->completion(filter_pkt->completion_ctx);
889 static void rndis_filter_send_request_completion(void *ctx)