Staging: merge 2.6.39-rc3 into staging-next
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / hv / rndis_filter.c
blob6305050f9e8d24216d8522f116bc557945e19926
1 /*
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
11 * more details.
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.
17 * Authors:
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>
26 #include <linux/io.h>
27 #include <linux/if_ether.h>
28 #include <linux/netdevice.h>
29 #include "logging.h"
30 #include "hv_api.h"
31 #include "netvsc_api.h"
32 #include "rndis_filter.h"
34 /* Data types */
35 struct rndis_filter_driver_object {
36 /* The original driver */
37 struct netvsc_driver inner_drv;
40 enum rndis_device_state {
41 RNDIS_DEV_UNINITIALIZED = 0,
42 RNDIS_DEV_INITIALIZING,
43 RNDIS_DEV_INITIALIZED,
44 RNDIS_DEV_DATAINITIALIZED,
47 struct rndis_device {
48 struct netvsc_device *net_dev;
50 enum rndis_device_state state;
51 u32 link_stat;
52 atomic_t new_req_id;
54 spinlock_t request_lock;
55 struct list_head req_list;
57 unsigned char hw_mac_adr[ETH_ALEN];
60 struct rndis_request {
61 struct list_head list_ent;
62 int wait_condition;
63 wait_queue_head_t wait_event;
66 * FIXME: We assumed a fixed size response here. If we do ever need to
67 * handle a bigger response, we can either define a max response
68 * message or add a response buffer variable above this field
70 struct rndis_message response_msg;
72 /* Simplify allocation by having a netvsc packet inline */
73 struct hv_netvsc_packet pkt;
74 struct hv_page_buffer buf;
75 /* FIXME: We assumed a fixed size request here. */
76 struct rndis_message request_msg;
80 struct rndis_filter_packet {
81 void *completion_ctx;
82 void (*completion)(void *context);
83 struct rndis_message msg;
87 static int rndis_filte_device_add(struct hv_device *dev,
88 void *additional_info);
90 static int rndis_filter_device_remove(struct hv_device *dev);
92 static void rndis_filter_cleanup(struct hv_driver *drv);
94 static int rndis_filter_send(struct hv_device *dev,
95 struct hv_netvsc_packet *pkt);
97 static void rndis_filter_send_completion(void *ctx);
99 static void rndis_filter_send_request_completion(void *ctx);
102 /* The one and only */
103 static struct rndis_filter_driver_object rndis_filter;
105 static struct rndis_device *get_rndis_device(void)
107 struct rndis_device *device;
109 device = kzalloc(sizeof(struct rndis_device), GFP_KERNEL);
110 if (!device)
111 return NULL;
113 spin_lock_init(&device->request_lock);
115 INIT_LIST_HEAD(&device->req_list);
117 device->state = RNDIS_DEV_UNINITIALIZED;
119 return device;
122 static struct rndis_request *get_rndis_request(struct rndis_device *dev,
123 u32 msg_type,
124 u32 msg_len)
126 struct rndis_request *request;
127 struct rndis_message *rndis_msg;
128 struct rndis_set_request *set;
129 unsigned long flags;
131 request = kzalloc(sizeof(struct rndis_request), GFP_KERNEL);
132 if (!request)
133 return NULL;
135 init_waitqueue_head(&request->wait_event);
137 rndis_msg = &request->request_msg;
138 rndis_msg->ndis_msg_type = msg_type;
139 rndis_msg->msg_len = msg_len;
142 * Set the request id. This field is always after the rndis header for
143 * request/response packet types so we just used the SetRequest as a
144 * template
146 set = &rndis_msg->msg.set_req;
147 set->req_id = atomic_inc_return(&dev->new_req_id);
149 /* Add to the request list */
150 spin_lock_irqsave(&dev->request_lock, flags);
151 list_add_tail(&request->list_ent, &dev->req_list);
152 spin_unlock_irqrestore(&dev->request_lock, flags);
154 return request;
157 static void put_rndis_request(struct rndis_device *dev,
158 struct rndis_request *req)
160 unsigned long flags;
162 spin_lock_irqsave(&dev->request_lock, flags);
163 list_del(&req->list_ent);
164 spin_unlock_irqrestore(&dev->request_lock, flags);
166 kfree(req);
169 static void dump_rndis_message(struct rndis_message *rndis_msg)
171 switch (rndis_msg->ndis_msg_type) {
172 case REMOTE_NDIS_PACKET_MSG:
173 DPRINT_DBG(NETVSC, "REMOTE_NDIS_PACKET_MSG (len %u, "
174 "data offset %u data len %u, # oob %u, "
175 "oob offset %u, oob len %u, pkt offset %u, "
176 "pkt len %u",
177 rndis_msg->msg_len,
178 rndis_msg->msg.pkt.data_offset,
179 rndis_msg->msg.pkt.data_len,
180 rndis_msg->msg.pkt.num_oob_data_elements,
181 rndis_msg->msg.pkt.oob_data_offset,
182 rndis_msg->msg.pkt.oob_data_len,
183 rndis_msg->msg.pkt.per_pkt_info_offset,
184 rndis_msg->msg.pkt.per_pkt_info_len);
185 break;
187 case REMOTE_NDIS_INITIALIZE_CMPLT:
188 DPRINT_DBG(NETVSC, "REMOTE_NDIS_INITIALIZE_CMPLT "
189 "(len %u, id 0x%x, status 0x%x, major %d, minor %d, "
190 "device flags %d, max xfer size 0x%x, max pkts %u, "
191 "pkt aligned %u)",
192 rndis_msg->msg_len,
193 rndis_msg->msg.init_complete.req_id,
194 rndis_msg->msg.init_complete.status,
195 rndis_msg->msg.init_complete.major_ver,
196 rndis_msg->msg.init_complete.minor_ver,
197 rndis_msg->msg.init_complete.dev_flags,
198 rndis_msg->msg.init_complete.max_xfer_size,
199 rndis_msg->msg.init_complete.
200 max_pkt_per_msg,
201 rndis_msg->msg.init_complete.
202 pkt_alignment_factor);
203 break;
205 case REMOTE_NDIS_QUERY_CMPLT:
206 DPRINT_DBG(NETVSC, "REMOTE_NDIS_QUERY_CMPLT "
207 "(len %u, id 0x%x, status 0x%x, buf len %u, "
208 "buf offset %u)",
209 rndis_msg->msg_len,
210 rndis_msg->msg.query_complete.req_id,
211 rndis_msg->msg.query_complete.status,
212 rndis_msg->msg.query_complete.
213 info_buflen,
214 rndis_msg->msg.query_complete.
215 info_buf_offset);
216 break;
218 case REMOTE_NDIS_SET_CMPLT:
219 DPRINT_DBG(NETVSC,
220 "REMOTE_NDIS_SET_CMPLT (len %u, id 0x%x, status 0x%x)",
221 rndis_msg->msg_len,
222 rndis_msg->msg.set_complete.req_id,
223 rndis_msg->msg.set_complete.status);
224 break;
226 case REMOTE_NDIS_INDICATE_STATUS_MSG:
227 DPRINT_DBG(NETVSC, "REMOTE_NDIS_INDICATE_STATUS_MSG "
228 "(len %u, status 0x%x, buf len %u, buf offset %u)",
229 rndis_msg->msg_len,
230 rndis_msg->msg.indicate_status.status,
231 rndis_msg->msg.indicate_status.status_buflen,
232 rndis_msg->msg.indicate_status.status_buf_offset);
233 break;
235 default:
236 DPRINT_DBG(NETVSC, "0x%x (len %u)",
237 rndis_msg->ndis_msg_type,
238 rndis_msg->msg_len);
239 break;
243 static int rndis_filter_send_request(struct rndis_device *dev,
244 struct rndis_request *req)
246 int ret;
247 struct hv_netvsc_packet *packet;
249 /* Setup the packet to send it */
250 packet = &req->pkt;
252 packet->is_data_pkt = false;
253 packet->total_data_buflen = req->request_msg.msg_len;
254 packet->page_buf_cnt = 1;
256 packet->page_buf[0].pfn = virt_to_phys(&req->request_msg) >>
257 PAGE_SHIFT;
258 packet->page_buf[0].len = req->request_msg.msg_len;
259 packet->page_buf[0].offset =
260 (unsigned long)&req->request_msg & (PAGE_SIZE - 1);
262 packet->completion.send.send_completion_ctx = req;/* packet; */
263 packet->completion.send.send_completion =
264 rndis_filter_send_request_completion;
265 packet->completion.send.send_completion_tid = (unsigned long)dev;
267 ret = rndis_filter.inner_drv.send(dev->net_dev->dev, packet);
268 return ret;
271 static void rndis_filter_receive_response(struct rndis_device *dev,
272 struct rndis_message *resp)
274 struct rndis_request *request = NULL;
275 bool found = false;
276 unsigned long flags;
278 spin_lock_irqsave(&dev->request_lock, flags);
279 list_for_each_entry(request, &dev->req_list, list_ent) {
281 * All request/response message contains RequestId as the 1st
282 * field
284 if (request->request_msg.msg.init_req.req_id
285 == resp->msg.init_complete.req_id) {
286 found = true;
287 break;
290 spin_unlock_irqrestore(&dev->request_lock, flags);
292 if (found) {
293 if (resp->msg_len <= sizeof(struct rndis_message)) {
294 memcpy(&request->response_msg, resp,
295 resp->msg_len);
296 } else {
297 dev_err(&dev->net_dev->dev->device,
298 "rndis response buffer overflow "
299 "detected (size %u max %zu)\n",
300 resp->msg_len,
301 sizeof(struct rndis_filter_packet));
303 if (resp->ndis_msg_type ==
304 REMOTE_NDIS_RESET_CMPLT) {
305 /* does not have a request id field */
306 request->response_msg.msg.reset_complete.
307 status = STATUS_BUFFER_OVERFLOW;
308 } else {
309 request->response_msg.msg.
310 init_complete.status =
311 STATUS_BUFFER_OVERFLOW;
315 request->wait_condition = 1;
316 wake_up(&request->wait_event);
317 } else {
318 dev_err(&dev->net_dev->dev->device,
319 "no rndis request found for this response "
320 "(id 0x%x res type 0x%x)\n",
321 resp->msg.init_complete.req_id,
322 resp->ndis_msg_type);
326 static void rndis_filter_receive_indicate_status(struct rndis_device *dev,
327 struct rndis_message *resp)
329 struct rndis_indicate_status *indicate =
330 &resp->msg.indicate_status;
332 if (indicate->status == RNDIS_STATUS_MEDIA_CONNECT) {
333 rndis_filter.inner_drv.link_status_change(
334 dev->net_dev->dev, 1);
335 } else if (indicate->status == RNDIS_STATUS_MEDIA_DISCONNECT) {
336 rndis_filter.inner_drv.link_status_change(
337 dev->net_dev->dev, 0);
338 } else {
340 * TODO:
345 static void rndis_filter_receive_data(struct rndis_device *dev,
346 struct rndis_message *msg,
347 struct hv_netvsc_packet *pkt)
349 struct rndis_packet *rndis_pkt;
350 u32 data_offset;
352 rndis_pkt = &msg->msg.pkt;
355 * FIXME: Handle multiple rndis pkt msgs that maybe enclosed in this
356 * netvsc packet (ie TotalDataBufferLength != MessageLength)
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;
363 pkt->page_buf[0].offset += data_offset;
364 pkt->page_buf[0].len -= data_offset;
366 pkt->is_data_pkt = true;
368 rndis_filter.inner_drv.recv_cb(dev->net_dev->dev,
369 pkt);
372 static int rndis_filter_receive(struct hv_device *dev,
373 struct hv_netvsc_packet *pkt)
375 struct netvsc_device *net_dev = dev->ext;
376 struct rndis_device *rndis_dev;
377 struct rndis_message rndis_msg;
378 struct rndis_message *rndis_hdr;
380 if (!net_dev)
381 return -EINVAL;
383 /* Make sure the rndis device state is initialized */
384 if (!net_dev->extension) {
385 dev_err(&dev->device, "got rndis message but no rndis device - "
386 "dropping this message!\n");
387 return -1;
390 rndis_dev = (struct rndis_device *)net_dev->extension;
391 if (rndis_dev->state == RNDIS_DEV_UNINITIALIZED) {
392 dev_err(&dev->device, "got rndis message but rndis device "
393 "uninitialized...dropping this message!\n");
394 return -1;
397 rndis_hdr = (struct rndis_message *)kmap_atomic(
398 pfn_to_page(pkt->page_buf[0].pfn), KM_IRQ0);
400 rndis_hdr = (void *)((unsigned long)rndis_hdr +
401 pkt->page_buf[0].offset);
403 /* Make sure we got a valid rndis message */
405 * FIXME: There seems to be a bug in set completion msg where its
406 * MessageLength is 16 bytes but the ByteCount field in the xfer page
407 * range shows 52 bytes
408 * */
409 #if 0
410 if (pkt->total_data_buflen != rndis_hdr->msg_len) {
411 kunmap_atomic(rndis_hdr - pkt->page_buf[0].offset,
412 KM_IRQ0);
414 dev_err(&dev->device, "invalid rndis message? (expected %u "
415 "bytes got %u)...dropping this message!\n",
416 rndis_hdr->msg_len,
417 pkt->total_data_buflen);
418 return -1;
420 #endif
422 if ((rndis_hdr->ndis_msg_type != REMOTE_NDIS_PACKET_MSG) &&
423 (rndis_hdr->msg_len > sizeof(struct rndis_message))) {
424 dev_err(&dev->device, "incoming rndis message buffer overflow "
425 "detected (got %u, max %zu)..marking it an error!\n",
426 rndis_hdr->msg_len,
427 sizeof(struct rndis_message));
430 memcpy(&rndis_msg, rndis_hdr,
431 (rndis_hdr->msg_len > sizeof(struct rndis_message)) ?
432 sizeof(struct rndis_message) :
433 rndis_hdr->msg_len);
435 kunmap_atomic(rndis_hdr - pkt->page_buf[0].offset, KM_IRQ0);
437 dump_rndis_message(&rndis_msg);
439 switch (rndis_msg.ndis_msg_type) {
440 case REMOTE_NDIS_PACKET_MSG:
441 /* data msg */
442 rndis_filter_receive_data(rndis_dev, &rndis_msg, pkt);
443 break;
445 case REMOTE_NDIS_INITIALIZE_CMPLT:
446 case REMOTE_NDIS_QUERY_CMPLT:
447 case REMOTE_NDIS_SET_CMPLT:
448 /* completion msgs */
449 rndis_filter_receive_response(rndis_dev, &rndis_msg);
450 break;
452 case REMOTE_NDIS_INDICATE_STATUS_MSG:
453 /* notification msgs */
454 rndis_filter_receive_indicate_status(rndis_dev, &rndis_msg);
455 break;
456 default:
457 dev_err(&dev->device,
458 "unhandled rndis message (type %u len %u)\n",
459 rndis_msg.ndis_msg_type,
460 rndis_msg.msg_len);
461 break;
464 return 0;
467 static int rndis_filter_query_device(struct rndis_device *dev, u32 oid,
468 void *result, u32 *result_size)
470 struct rndis_request *request;
471 u32 inresult_size = *result_size;
472 struct rndis_query_request *query;
473 struct rndis_query_complete *query_complete;
474 int ret = 0;
476 if (!result)
477 return -EINVAL;
479 *result_size = 0;
480 request = get_rndis_request(dev, REMOTE_NDIS_QUERY_MSG,
481 RNDIS_MESSAGE_SIZE(struct rndis_query_request));
482 if (!request) {
483 ret = -1;
484 goto Cleanup;
487 /* Setup the rndis query */
488 query = &request->request_msg.msg.query_req;
489 query->oid = oid;
490 query->info_buf_offset = sizeof(struct rndis_query_request);
491 query->info_buflen = 0;
492 query->dev_vc_handle = 0;
494 request->wait_condition = 0;
495 ret = rndis_filter_send_request(dev, request);
496 if (ret != 0)
497 goto Cleanup;
499 wait_event_timeout(request->wait_event, request->wait_condition,
500 msecs_to_jiffies(1000));
501 if (request->wait_condition == 0) {
502 ret = -ETIMEDOUT;
503 goto Cleanup;
506 /* Copy the response back */
507 query_complete = &request->response_msg.msg.query_complete;
509 if (query_complete->info_buflen > inresult_size) {
510 ret = -1;
511 goto Cleanup;
514 memcpy(result,
515 (void *)((unsigned long)query_complete +
516 query_complete->info_buf_offset),
517 query_complete->info_buflen);
519 *result_size = query_complete->info_buflen;
521 Cleanup:
522 if (request)
523 put_rndis_request(dev, request);
525 return ret;
528 static int rndis_filter_query_device_mac(struct rndis_device *dev)
530 u32 size = ETH_ALEN;
532 return rndis_filter_query_device(dev,
533 RNDIS_OID_802_3_PERMANENT_ADDRESS,
534 dev->hw_mac_adr, &size);
537 static int rndis_filter_query_device_link_status(struct rndis_device *dev)
539 u32 size = sizeof(u32);
541 return rndis_filter_query_device(dev,
542 RNDIS_OID_GEN_MEDIA_CONNECT_STATUS,
543 &dev->link_stat, &size);
546 static int rndis_filter_set_packet_filter(struct rndis_device *dev,
547 u32 new_filter)
549 struct rndis_request *request;
550 struct rndis_set_request *set;
551 struct rndis_set_complete *set_complete;
552 u32 status;
553 int ret;
555 request = get_rndis_request(dev, REMOTE_NDIS_SET_MSG,
556 RNDIS_MESSAGE_SIZE(struct rndis_set_request) +
557 sizeof(u32));
558 if (!request) {
559 ret = -1;
560 goto Cleanup;
563 /* Setup the rndis set */
564 set = &request->request_msg.msg.set_req;
565 set->oid = RNDIS_OID_GEN_CURRENT_PACKET_FILTER;
566 set->info_buflen = sizeof(u32);
567 set->info_buf_offset = sizeof(struct rndis_set_request);
569 memcpy((void *)(unsigned long)set + sizeof(struct rndis_set_request),
570 &new_filter, sizeof(u32));
572 request->wait_condition = 0;
573 ret = rndis_filter_send_request(dev, request);
574 if (ret != 0)
575 goto Cleanup;
577 wait_event_timeout(request->wait_event, request->wait_condition,
578 msecs_to_jiffies(2000));
579 if (request->wait_condition == 0) {
580 ret = -1;
581 dev_err(&dev->net_dev->dev->device,
582 "timeout before we got a set response...\n");
584 * We can't deallocate the request since we may still receive a
585 * send completion for it.
587 goto Exit;
588 } else {
589 if (ret > 0)
590 ret = 0;
591 set_complete = &request->response_msg.msg.set_complete;
592 status = set_complete->status;
595 Cleanup:
596 if (request)
597 put_rndis_request(dev, request);
598 Exit:
599 return ret;
602 int rndis_filter_init(struct netvsc_driver *drv)
604 drv->req_ext_size = sizeof(struct rndis_filter_packet);
606 /* Driver->Context = rndisDriver; */
608 memset(&rndis_filter, 0, sizeof(struct rndis_filter_driver_object));
610 /*rndisDriver->Driver = Driver;
612 ASSERT(Driver->OnLinkStatusChanged);
613 rndisDriver->OnLinkStatusChanged = Driver->OnLinkStatusChanged;*/
615 /* Save the original dispatch handlers before we override it */
616 rndis_filter.inner_drv.base.dev_add = drv->base.dev_add;
617 rndis_filter.inner_drv.base.dev_rm =
618 drv->base.dev_rm;
619 rndis_filter.inner_drv.base.cleanup = drv->base.cleanup;
621 rndis_filter.inner_drv.send = drv->send;
622 rndis_filter.inner_drv.recv_cb = drv->recv_cb;
623 rndis_filter.inner_drv.link_status_change =
624 drv->link_status_change;
626 /* Override */
627 drv->base.dev_add = rndis_filte_device_add;
628 drv->base.dev_rm = rndis_filter_device_remove;
629 drv->base.cleanup = rndis_filter_cleanup;
630 drv->send = rndis_filter_send;
631 drv->recv_cb = rndis_filter_receive;
633 return 0;
636 static int rndis_filter_init_device(struct rndis_device *dev)
638 struct rndis_request *request;
639 struct rndis_initialize_request *init;
640 struct rndis_initialize_complete *init_complete;
641 u32 status;
642 int ret;
644 request = get_rndis_request(dev, REMOTE_NDIS_INITIALIZE_MSG,
645 RNDIS_MESSAGE_SIZE(struct rndis_initialize_request));
646 if (!request) {
647 ret = -1;
648 goto Cleanup;
651 /* Setup the rndis set */
652 init = &request->request_msg.msg.init_req;
653 init->major_ver = RNDIS_MAJOR_VERSION;
654 init->minor_ver = RNDIS_MINOR_VERSION;
655 /* FIXME: Use 1536 - rounded ethernet frame size */
656 init->max_xfer_size = 2048;
658 dev->state = RNDIS_DEV_INITIALIZING;
660 request->wait_condition = 0;
661 ret = rndis_filter_send_request(dev, request);
662 if (ret != 0) {
663 dev->state = RNDIS_DEV_UNINITIALIZED;
664 goto Cleanup;
668 wait_event_timeout(request->wait_event, request->wait_condition,
669 msecs_to_jiffies(1000));
670 if (request->wait_condition == 0) {
671 ret = -ETIMEDOUT;
672 goto Cleanup;
675 init_complete = &request->response_msg.msg.init_complete;
676 status = init_complete->status;
677 if (status == RNDIS_STATUS_SUCCESS) {
678 dev->state = RNDIS_DEV_INITIALIZED;
679 ret = 0;
680 } else {
681 dev->state = RNDIS_DEV_UNINITIALIZED;
682 ret = -1;
685 Cleanup:
686 if (request)
687 put_rndis_request(dev, request);
689 return ret;
692 static void rndis_filter_halt_device(struct rndis_device *dev)
694 struct rndis_request *request;
695 struct rndis_halt_request *halt;
697 /* Attempt to do a rndis device halt */
698 request = get_rndis_request(dev, REMOTE_NDIS_HALT_MSG,
699 RNDIS_MESSAGE_SIZE(struct rndis_halt_request));
700 if (!request)
701 goto Cleanup;
703 /* Setup the rndis set */
704 halt = &request->request_msg.msg.halt_req;
705 halt->req_id = atomic_inc_return(&dev->new_req_id);
707 /* Ignore return since this msg is optional. */
708 rndis_filter_send_request(dev, request);
710 dev->state = RNDIS_DEV_UNINITIALIZED;
712 Cleanup:
713 if (request)
714 put_rndis_request(dev, request);
715 return;
718 static int rndis_filter_open_device(struct rndis_device *dev)
720 int ret;
722 if (dev->state != RNDIS_DEV_INITIALIZED)
723 return 0;
725 ret = rndis_filter_set_packet_filter(dev,
726 NDIS_PACKET_TYPE_BROADCAST |
727 NDIS_PACKET_TYPE_ALL_MULTICAST |
728 NDIS_PACKET_TYPE_DIRECTED);
729 if (ret == 0)
730 dev->state = RNDIS_DEV_DATAINITIALIZED;
732 return ret;
735 static int rndis_filter_close_device(struct rndis_device *dev)
737 int ret;
739 if (dev->state != RNDIS_DEV_DATAINITIALIZED)
740 return 0;
742 ret = rndis_filter_set_packet_filter(dev, 0);
743 if (ret == 0)
744 dev->state = RNDIS_DEV_INITIALIZED;
746 return ret;
749 static int rndis_filte_device_add(struct hv_device *dev,
750 void *additional_info)
752 int ret;
753 struct netvsc_device *netDevice;
754 struct rndis_device *rndisDevice;
755 struct netvsc_device_info *deviceInfo = additional_info;
757 rndisDevice = get_rndis_device();
758 if (!rndisDevice)
759 return -1;
762 * Let the inner driver handle this first to create the netvsc channel
763 * NOTE! Once the channel is created, we may get a receive callback
764 * (RndisFilterOnReceive()) before this call is completed
766 ret = rndis_filter.inner_drv.base.dev_add(dev, additional_info);
767 if (ret != 0) {
768 kfree(rndisDevice);
769 return ret;
773 /* Initialize the rndis device */
774 netDevice = dev->ext;
776 netDevice->extension = rndisDevice;
777 rndisDevice->net_dev = netDevice;
779 /* Send the rndis initialization message */
780 ret = rndis_filter_init_device(rndisDevice);
781 if (ret != 0) {
783 * TODO: If rndis init failed, we will need to shut down the
784 * channel
788 /* Get the mac address */
789 ret = rndis_filter_query_device_mac(rndisDevice);
790 if (ret != 0) {
792 * TODO: shutdown rndis device and the channel
796 memcpy(deviceInfo->mac_adr, rndisDevice->hw_mac_adr, ETH_ALEN);
798 rndis_filter_query_device_link_status(rndisDevice);
800 deviceInfo->link_state = rndisDevice->link_stat;
802 dev_info(&dev->device, "Device MAC %pM link state %s",
803 rndisDevice->hw_mac_adr,
804 ((deviceInfo->link_state) ? ("down\n") : ("up\n")));
806 return ret;
809 static int rndis_filter_device_remove(struct hv_device *dev)
811 struct netvsc_device *net_dev = dev->ext;
812 struct rndis_device *rndis_dev = net_dev->extension;
814 /* Halt and release the rndis device */
815 rndis_filter_halt_device(rndis_dev);
817 kfree(rndis_dev);
818 net_dev->extension = NULL;
820 /* Pass control to inner driver to remove the device */
821 rndis_filter.inner_drv.base.dev_rm(dev);
823 return 0;
826 static void rndis_filter_cleanup(struct hv_driver *drv)
830 int rndis_filter_open(struct hv_device *dev)
832 struct netvsc_device *netDevice = dev->ext;
834 if (!netDevice)
835 return -EINVAL;
837 return rndis_filter_open_device(netDevice->extension);
840 int rndis_filter_close(struct hv_device *dev)
842 struct netvsc_device *netDevice = dev->ext;
844 if (!netDevice)
845 return -EINVAL;
847 return rndis_filter_close_device(netDevice->extension);
850 static int rndis_filter_send(struct hv_device *dev,
851 struct hv_netvsc_packet *pkt)
853 int ret;
854 struct rndis_filter_packet *filterPacket;
855 struct rndis_message *rndisMessage;
856 struct rndis_packet *rndisPacket;
857 u32 rndisMessageSize;
859 /* Add the rndis header */
860 filterPacket = (struct rndis_filter_packet *)pkt->extension;
862 memset(filterPacket, 0, sizeof(struct rndis_filter_packet));
864 rndisMessage = &filterPacket->msg;
865 rndisMessageSize = RNDIS_MESSAGE_SIZE(struct rndis_packet);
867 rndisMessage->ndis_msg_type = REMOTE_NDIS_PACKET_MSG;
868 rndisMessage->msg_len = pkt->total_data_buflen +
869 rndisMessageSize;
871 rndisPacket = &rndisMessage->msg.pkt;
872 rndisPacket->data_offset = sizeof(struct rndis_packet);
873 rndisPacket->data_len = pkt->total_data_buflen;
875 pkt->is_data_pkt = true;
876 pkt->page_buf[0].pfn = virt_to_phys(rndisMessage) >> PAGE_SHIFT;
877 pkt->page_buf[0].offset =
878 (unsigned long)rndisMessage & (PAGE_SIZE-1);
879 pkt->page_buf[0].len = rndisMessageSize;
881 /* Save the packet send completion and context */
882 filterPacket->completion = pkt->completion.send.send_completion;
883 filterPacket->completion_ctx =
884 pkt->completion.send.send_completion_ctx;
886 /* Use ours */
887 pkt->completion.send.send_completion = rndis_filter_send_completion;
888 pkt->completion.send.send_completion_ctx = filterPacket;
890 ret = rndis_filter.inner_drv.send(dev, pkt);
891 if (ret != 0) {
893 * Reset the completion to originals to allow retries from
894 * above
896 pkt->completion.send.send_completion =
897 filterPacket->completion;
898 pkt->completion.send.send_completion_ctx =
899 filterPacket->completion_ctx;
902 return ret;
905 static void rndis_filter_send_completion(void *ctx)
907 struct rndis_filter_packet *filterPacket = ctx;
909 /* Pass it back to the original handler */
910 filterPacket->completion(filterPacket->completion_ctx);
914 static void rndis_filter_send_request_completion(void *ctx)
916 /* Noop */