Staging: hv: netvsc: Rename netDevice as net_device
[pandora-kernel.git] / drivers / staging / hv / rndis_filter.c
1 /*
2  * Copyright (c) 2009, Microsoft Corporation.
3  *
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.
7  *
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.
12  *
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.
16  *
17  * Authors:
18  *   Haiyang Zhang <haiyangz@microsoft.com>
19  *   Hank Janssen  <hjanssen@microsoft.com>
20  */
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
30 #include "hyperv_net.h"
31
32
33 enum rndis_device_state {
34         RNDIS_DEV_UNINITIALIZED = 0,
35         RNDIS_DEV_INITIALIZING,
36         RNDIS_DEV_INITIALIZED,
37         RNDIS_DEV_DATAINITIALIZED,
38 };
39
40 struct rndis_device {
41         struct netvsc_device *net_dev;
42
43         enum rndis_device_state state;
44         u32 link_stat;
45         atomic_t new_req_id;
46
47         spinlock_t request_lock;
48         struct list_head req_list;
49
50         unsigned char hw_mac_adr[ETH_ALEN];
51 };
52
53 struct rndis_request {
54         struct list_head list_ent;
55         struct completion  wait_event;
56
57         /*
58          * FIXME: We assumed a fixed size response here. If we do ever need to
59          * handle a bigger response, we can either define a max response
60          * message or add a response buffer variable above this field
61          */
62         struct rndis_message response_msg;
63
64         /* Simplify allocation by having a netvsc packet inline */
65         struct hv_netvsc_packet pkt;
66         struct hv_page_buffer buf;
67         /* FIXME: We assumed a fixed size request here. */
68         struct rndis_message request_msg;
69 };
70
71 static void rndis_filter_send_completion(void *ctx);
72
73 static void rndis_filter_send_request_completion(void *ctx);
74
75
76
77 static struct rndis_device *get_rndis_device(void)
78 {
79         struct rndis_device *device;
80
81         device = kzalloc(sizeof(struct rndis_device), GFP_KERNEL);
82         if (!device)
83                 return NULL;
84
85         spin_lock_init(&device->request_lock);
86
87         INIT_LIST_HEAD(&device->req_list);
88
89         device->state = RNDIS_DEV_UNINITIALIZED;
90
91         return device;
92 }
93
94 static struct rndis_request *get_rndis_request(struct rndis_device *dev,
95                                              u32 msg_type,
96                                              u32 msg_len)
97 {
98         struct rndis_request *request;
99         struct rndis_message *rndis_msg;
100         struct rndis_set_request *set;
101         unsigned long flags;
102
103         request = kzalloc(sizeof(struct rndis_request), GFP_KERNEL);
104         if (!request)
105                 return NULL;
106
107         init_completion(&request->wait_event);
108
109         rndis_msg = &request->request_msg;
110         rndis_msg->ndis_msg_type = msg_type;
111         rndis_msg->msg_len = msg_len;
112
113         /*
114          * Set the request id. This field is always after the rndis header for
115          * request/response packet types so we just used the SetRequest as a
116          * template
117          */
118         set = &rndis_msg->msg.set_req;
119         set->req_id = atomic_inc_return(&dev->new_req_id);
120
121         /* Add to the request list */
122         spin_lock_irqsave(&dev->request_lock, flags);
123         list_add_tail(&request->list_ent, &dev->req_list);
124         spin_unlock_irqrestore(&dev->request_lock, flags);
125
126         return request;
127 }
128
129 static void put_rndis_request(struct rndis_device *dev,
130                             struct rndis_request *req)
131 {
132         unsigned long flags;
133
134         spin_lock_irqsave(&dev->request_lock, flags);
135         list_del(&req->list_ent);
136         spin_unlock_irqrestore(&dev->request_lock, flags);
137
138         kfree(req);
139 }
140
141 static void dump_rndis_message(struct hv_device *hv_dev,
142                         struct rndis_message *rndis_msg)
143 {
144         struct net_device *netdev;
145         struct netvsc_device *net_device;
146
147         net_device = hv_get_drvdata(hv_dev);
148         netdev = net_device->ndev;
149
150         switch (rndis_msg->ndis_msg_type) {
151         case REMOTE_NDIS_PACKET_MSG:
152                 netdev_dbg(netdev, "REMOTE_NDIS_PACKET_MSG (len %u, "
153                            "data offset %u data len %u, # oob %u, "
154                            "oob offset %u, oob len %u, pkt offset %u, "
155                            "pkt len %u\n",
156                            rndis_msg->msg_len,
157                            rndis_msg->msg.pkt.data_offset,
158                            rndis_msg->msg.pkt.data_len,
159                            rndis_msg->msg.pkt.num_oob_data_elements,
160                            rndis_msg->msg.pkt.oob_data_offset,
161                            rndis_msg->msg.pkt.oob_data_len,
162                            rndis_msg->msg.pkt.per_pkt_info_offset,
163                            rndis_msg->msg.pkt.per_pkt_info_len);
164                 break;
165
166         case REMOTE_NDIS_INITIALIZE_CMPLT:
167                 netdev_dbg(netdev, "REMOTE_NDIS_INITIALIZE_CMPLT "
168                         "(len %u, id 0x%x, status 0x%x, major %d, minor %d, "
169                         "device flags %d, max xfer size 0x%x, max pkts %u, "
170                         "pkt aligned %u)\n",
171                         rndis_msg->msg_len,
172                         rndis_msg->msg.init_complete.req_id,
173                         rndis_msg->msg.init_complete.status,
174                         rndis_msg->msg.init_complete.major_ver,
175                         rndis_msg->msg.init_complete.minor_ver,
176                         rndis_msg->msg.init_complete.dev_flags,
177                         rndis_msg->msg.init_complete.max_xfer_size,
178                         rndis_msg->msg.init_complete.
179                            max_pkt_per_msg,
180                         rndis_msg->msg.init_complete.
181                            pkt_alignment_factor);
182                 break;
183
184         case REMOTE_NDIS_QUERY_CMPLT:
185                 netdev_dbg(netdev, "REMOTE_NDIS_QUERY_CMPLT "
186                         "(len %u, id 0x%x, status 0x%x, buf len %u, "
187                         "buf offset %u)\n",
188                         rndis_msg->msg_len,
189                         rndis_msg->msg.query_complete.req_id,
190                         rndis_msg->msg.query_complete.status,
191                         rndis_msg->msg.query_complete.
192                            info_buflen,
193                         rndis_msg->msg.query_complete.
194                            info_buf_offset);
195                 break;
196
197         case REMOTE_NDIS_SET_CMPLT:
198                 netdev_dbg(netdev,
199                         "REMOTE_NDIS_SET_CMPLT (len %u, id 0x%x, status 0x%x)\n",
200                         rndis_msg->msg_len,
201                         rndis_msg->msg.set_complete.req_id,
202                         rndis_msg->msg.set_complete.status);
203                 break;
204
205         case REMOTE_NDIS_INDICATE_STATUS_MSG:
206                 netdev_dbg(netdev, "REMOTE_NDIS_INDICATE_STATUS_MSG "
207                         "(len %u, status 0x%x, buf len %u, buf offset %u)\n",
208                         rndis_msg->msg_len,
209                         rndis_msg->msg.indicate_status.status,
210                         rndis_msg->msg.indicate_status.status_buflen,
211                         rndis_msg->msg.indicate_status.status_buf_offset);
212                 break;
213
214         default:
215                 netdev_dbg(netdev, "0x%x (len %u)\n",
216                         rndis_msg->ndis_msg_type,
217                         rndis_msg->msg_len);
218                 break;
219         }
220 }
221
222 static int rndis_filter_send_request(struct rndis_device *dev,
223                                   struct rndis_request *req)
224 {
225         int ret;
226         struct hv_netvsc_packet *packet;
227
228         /* Setup the packet to send it */
229         packet = &req->pkt;
230
231         packet->is_data_pkt = false;
232         packet->total_data_buflen = req->request_msg.msg_len;
233         packet->page_buf_cnt = 1;
234
235         packet->page_buf[0].pfn = virt_to_phys(&req->request_msg) >>
236                                         PAGE_SHIFT;
237         packet->page_buf[0].len = req->request_msg.msg_len;
238         packet->page_buf[0].offset =
239                 (unsigned long)&req->request_msg & (PAGE_SIZE - 1);
240
241         packet->completion.send.send_completion_ctx = req;/* packet; */
242         packet->completion.send.send_completion =
243                 rndis_filter_send_request_completion;
244         packet->completion.send.send_completion_tid = (unsigned long)dev;
245
246         ret = netvsc_send(dev->net_dev->dev, packet);
247         return ret;
248 }
249
250 static void rndis_filter_receive_response(struct rndis_device *dev,
251                                        struct rndis_message *resp)
252 {
253         struct rndis_request *request = NULL;
254         bool found = false;
255         unsigned long flags;
256         struct net_device *ndev;
257
258         ndev = dev->net_dev->ndev;
259
260         spin_lock_irqsave(&dev->request_lock, flags);
261         list_for_each_entry(request, &dev->req_list, list_ent) {
262                 /*
263                  * All request/response message contains RequestId as the 1st
264                  * field
265                  */
266                 if (request->request_msg.msg.init_req.req_id
267                     == resp->msg.init_complete.req_id) {
268                         found = true;
269                         break;
270                 }
271         }
272         spin_unlock_irqrestore(&dev->request_lock, flags);
273
274         if (found) {
275                 if (resp->msg_len <= sizeof(struct rndis_message)) {
276                         memcpy(&request->response_msg, resp,
277                                resp->msg_len);
278                 } else {
279                         netdev_err(ndev,
280                                 "rndis response buffer overflow "
281                                 "detected (size %u max %zu)\n",
282                                 resp->msg_len,
283                                 sizeof(struct rndis_filter_packet));
284
285                         if (resp->ndis_msg_type ==
286                             REMOTE_NDIS_RESET_CMPLT) {
287                                 /* does not have a request id field */
288                                 request->response_msg.msg.reset_complete.
289                                         status = STATUS_BUFFER_OVERFLOW;
290                         } else {
291                                 request->response_msg.msg.
292                                 init_complete.status =
293                                         STATUS_BUFFER_OVERFLOW;
294                         }
295                 }
296
297                 complete(&request->wait_event);
298         } else {
299                 netdev_err(ndev,
300                         "no rndis request found for this response "
301                         "(id 0x%x res type 0x%x)\n",
302                         resp->msg.init_complete.req_id,
303                         resp->ndis_msg_type);
304         }
305 }
306
307 static void rndis_filter_receive_indicate_status(struct rndis_device *dev,
308                                              struct rndis_message *resp)
309 {
310         struct rndis_indicate_status *indicate =
311                         &resp->msg.indicate_status;
312
313         if (indicate->status == RNDIS_STATUS_MEDIA_CONNECT) {
314                 netvsc_linkstatus_callback(
315                         dev->net_dev->dev, 1);
316         } else if (indicate->status == RNDIS_STATUS_MEDIA_DISCONNECT) {
317                 netvsc_linkstatus_callback(
318                         dev->net_dev->dev, 0);
319         } else {
320                 /*
321                  * TODO:
322                  */
323         }
324 }
325
326 static void rndis_filter_receive_data(struct rndis_device *dev,
327                                    struct rndis_message *msg,
328                                    struct hv_netvsc_packet *pkt)
329 {
330         struct rndis_packet *rndis_pkt;
331         u32 data_offset;
332         int i;
333
334         rndis_pkt = &msg->msg.pkt;
335
336         /*
337          * FIXME: Handle multiple rndis pkt msgs that maybe enclosed in this
338          * netvsc packet (ie TotalDataBufferLength != MessageLength)
339          */
340
341         /* Remove the rndis header and pass it back up the stack */
342         data_offset = RNDIS_HEADER_SIZE + rndis_pkt->data_offset;
343
344         pkt->total_data_buflen -= data_offset;
345         pkt->page_buf[0].offset += data_offset;
346         pkt->page_buf[0].len -= data_offset;
347
348         /* Drop the 0th page, if rndis data go beyond page boundary */
349         if (pkt->page_buf[0].offset >= PAGE_SIZE) {
350                 pkt->page_buf[1].offset = pkt->page_buf[0].offset - PAGE_SIZE;
351                 pkt->page_buf[1].len -= pkt->page_buf[1].offset;
352                 pkt->page_buf_cnt--;
353                 for (i = 0; i < pkt->page_buf_cnt; i++)
354                         pkt->page_buf[i] = pkt->page_buf[i+1];
355         }
356
357         pkt->is_data_pkt = true;
358
359         netvsc_recv_callback(dev->net_dev->dev, pkt);
360 }
361
362 int rndis_filter_receive(struct hv_device *dev,
363                                 struct hv_netvsc_packet *pkt)
364 {
365         struct netvsc_device *net_dev = hv_get_drvdata(dev);
366         struct rndis_device *rndis_dev;
367         struct rndis_message rndis_msg;
368         struct rndis_message *rndis_hdr;
369         struct net_device *ndev;
370
371         ndev = net_dev->ndev;
372
373         if (!net_dev)
374                 return -EINVAL;
375
376         /* Make sure the rndis device state is initialized */
377         if (!net_dev->extension) {
378                 netdev_err(ndev, "got rndis message but no rndis device - "
379                           "dropping this message!\n");
380                 return -ENODEV;
381         }
382
383         rndis_dev = (struct rndis_device *)net_dev->extension;
384         if (rndis_dev->state == RNDIS_DEV_UNINITIALIZED) {
385                 netdev_err(ndev, "got rndis message but rndis device "
386                            "uninitialized...dropping this message!\n");
387                 return -ENODEV;
388         }
389
390         rndis_hdr = (struct rndis_message *)kmap_atomic(
391                         pfn_to_page(pkt->page_buf[0].pfn), KM_IRQ0);
392
393         rndis_hdr = (void *)((unsigned long)rndis_hdr +
394                         pkt->page_buf[0].offset);
395
396         /* Make sure we got a valid rndis message */
397         if ((rndis_hdr->ndis_msg_type != REMOTE_NDIS_PACKET_MSG) &&
398             (rndis_hdr->msg_len > sizeof(struct rndis_message))) {
399                 netdev_err(ndev, "incoming rndis message buffer overflow "
400                            "detected (got %u, max %zu)..marking it an error!\n",
401                            rndis_hdr->msg_len,
402                            sizeof(struct rndis_message));
403         }
404
405         memcpy(&rndis_msg, rndis_hdr,
406                 (rndis_hdr->msg_len > sizeof(struct rndis_message)) ?
407                         sizeof(struct rndis_message) :
408                         rndis_hdr->msg_len);
409
410         kunmap_atomic(rndis_hdr - pkt->page_buf[0].offset, KM_IRQ0);
411
412         dump_rndis_message(dev, &rndis_msg);
413
414         switch (rndis_msg.ndis_msg_type) {
415         case REMOTE_NDIS_PACKET_MSG:
416                 /* data msg */
417                 rndis_filter_receive_data(rndis_dev, &rndis_msg, pkt);
418                 break;
419
420         case REMOTE_NDIS_INITIALIZE_CMPLT:
421         case REMOTE_NDIS_QUERY_CMPLT:
422         case REMOTE_NDIS_SET_CMPLT:
423                 /* completion msgs */
424                 rndis_filter_receive_response(rndis_dev, &rndis_msg);
425                 break;
426
427         case REMOTE_NDIS_INDICATE_STATUS_MSG:
428                 /* notification msgs */
429                 rndis_filter_receive_indicate_status(rndis_dev, &rndis_msg);
430                 break;
431         default:
432                 netdev_err(ndev,
433                         "unhandled rndis message (type %u len %u)\n",
434                            rndis_msg.ndis_msg_type,
435                            rndis_msg.msg_len);
436                 break;
437         }
438
439         return 0;
440 }
441
442 static int rndis_filter_query_device(struct rndis_device *dev, u32 oid,
443                                   void *result, u32 *result_size)
444 {
445         struct rndis_request *request;
446         u32 inresult_size = *result_size;
447         struct rndis_query_request *query;
448         struct rndis_query_complete *query_complete;
449         int ret = 0;
450         int t;
451
452         if (!result)
453                 return -EINVAL;
454
455         *result_size = 0;
456         request = get_rndis_request(dev, REMOTE_NDIS_QUERY_MSG,
457                         RNDIS_MESSAGE_SIZE(struct rndis_query_request));
458         if (!request) {
459                 ret = -ENOMEM;
460                 goto cleanup;
461         }
462
463         /* Setup the rndis query */
464         query = &request->request_msg.msg.query_req;
465         query->oid = oid;
466         query->info_buf_offset = sizeof(struct rndis_query_request);
467         query->info_buflen = 0;
468         query->dev_vc_handle = 0;
469
470         ret = rndis_filter_send_request(dev, request);
471         if (ret != 0)
472                 goto cleanup;
473
474         t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
475         if (t == 0) {
476                 ret = -ETIMEDOUT;
477                 goto cleanup;
478         }
479
480         /* Copy the response back */
481         query_complete = &request->response_msg.msg.query_complete;
482
483         if (query_complete->info_buflen > inresult_size) {
484                 ret = -1;
485                 goto cleanup;
486         }
487
488         memcpy(result,
489                (void *)((unsigned long)query_complete +
490                          query_complete->info_buf_offset),
491                query_complete->info_buflen);
492
493         *result_size = query_complete->info_buflen;
494
495 cleanup:
496         if (request)
497                 put_rndis_request(dev, request);
498
499         return ret;
500 }
501
502 static int rndis_filter_query_device_mac(struct rndis_device *dev)
503 {
504         u32 size = ETH_ALEN;
505
506         return rndis_filter_query_device(dev,
507                                       RNDIS_OID_802_3_PERMANENT_ADDRESS,
508                                       dev->hw_mac_adr, &size);
509 }
510
511 static int rndis_filter_query_device_link_status(struct rndis_device *dev)
512 {
513         u32 size = sizeof(u32);
514
515         return rndis_filter_query_device(dev,
516                                       RNDIS_OID_GEN_MEDIA_CONNECT_STATUS,
517                                       &dev->link_stat, &size);
518 }
519
520 static int rndis_filter_set_packet_filter(struct rndis_device *dev,
521                                       u32 new_filter)
522 {
523         struct rndis_request *request;
524         struct rndis_set_request *set;
525         struct rndis_set_complete *set_complete;
526         u32 status;
527         int ret, t;
528         struct net_device *ndev;
529
530         ndev = dev->net_dev->ndev;
531
532         request = get_rndis_request(dev, REMOTE_NDIS_SET_MSG,
533                         RNDIS_MESSAGE_SIZE(struct rndis_set_request) +
534                         sizeof(u32));
535         if (!request) {
536                 ret = -ENOMEM;
537                 goto cleanup;
538         }
539
540         /* Setup the rndis set */
541         set = &request->request_msg.msg.set_req;
542         set->oid = RNDIS_OID_GEN_CURRENT_PACKET_FILTER;
543         set->info_buflen = sizeof(u32);
544         set->info_buf_offset = sizeof(struct rndis_set_request);
545
546         memcpy((void *)(unsigned long)set + sizeof(struct rndis_set_request),
547                &new_filter, sizeof(u32));
548
549         ret = rndis_filter_send_request(dev, request);
550         if (ret != 0)
551                 goto cleanup;
552
553         t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
554
555         if (t == 0) {
556                 netdev_err(ndev,
557                         "timeout before we got a set response...\n");
558                 /*
559                  * We can't deallocate the request since we may still receive a
560                  * send completion for it.
561                  */
562                 goto exit;
563         } else {
564                 set_complete = &request->response_msg.msg.set_complete;
565                 status = set_complete->status;
566         }
567
568 cleanup:
569         if (request)
570                 put_rndis_request(dev, request);
571 exit:
572         return ret;
573 }
574
575
576 static int rndis_filter_init_device(struct rndis_device *dev)
577 {
578         struct rndis_request *request;
579         struct rndis_initialize_request *init;
580         struct rndis_initialize_complete *init_complete;
581         u32 status;
582         int ret, t;
583
584         request = get_rndis_request(dev, REMOTE_NDIS_INITIALIZE_MSG,
585                         RNDIS_MESSAGE_SIZE(struct rndis_initialize_request));
586         if (!request) {
587                 ret = -ENOMEM;
588                 goto cleanup;
589         }
590
591         /* Setup the rndis set */
592         init = &request->request_msg.msg.init_req;
593         init->major_ver = RNDIS_MAJOR_VERSION;
594         init->minor_ver = RNDIS_MINOR_VERSION;
595         /* FIXME: Use 1536 - rounded ethernet frame size */
596         init->max_xfer_size = 2048;
597
598         dev->state = RNDIS_DEV_INITIALIZING;
599
600         ret = rndis_filter_send_request(dev, request);
601         if (ret != 0) {
602                 dev->state = RNDIS_DEV_UNINITIALIZED;
603                 goto cleanup;
604         }
605
606
607         t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
608
609         if (t == 0) {
610                 ret = -ETIMEDOUT;
611                 goto cleanup;
612         }
613
614         init_complete = &request->response_msg.msg.init_complete;
615         status = init_complete->status;
616         if (status == RNDIS_STATUS_SUCCESS) {
617                 dev->state = RNDIS_DEV_INITIALIZED;
618                 ret = 0;
619         } else {
620                 dev->state = RNDIS_DEV_UNINITIALIZED;
621                 ret = -EINVAL;
622         }
623
624 cleanup:
625         if (request)
626                 put_rndis_request(dev, request);
627
628         return ret;
629 }
630
631 static void rndis_filter_halt_device(struct rndis_device *dev)
632 {
633         struct rndis_request *request;
634         struct rndis_halt_request *halt;
635
636         /* Attempt to do a rndis device halt */
637         request = get_rndis_request(dev, REMOTE_NDIS_HALT_MSG,
638                                 RNDIS_MESSAGE_SIZE(struct rndis_halt_request));
639         if (!request)
640                 goto cleanup;
641
642         /* Setup the rndis set */
643         halt = &request->request_msg.msg.halt_req;
644         halt->req_id = atomic_inc_return(&dev->new_req_id);
645
646         /* Ignore return since this msg is optional. */
647         rndis_filter_send_request(dev, request);
648
649         dev->state = RNDIS_DEV_UNINITIALIZED;
650
651 cleanup:
652         if (request)
653                 put_rndis_request(dev, request);
654         return;
655 }
656
657 static int rndis_filter_open_device(struct rndis_device *dev)
658 {
659         int ret;
660
661         if (dev->state != RNDIS_DEV_INITIALIZED)
662                 return 0;
663
664         ret = rndis_filter_set_packet_filter(dev,
665                                          NDIS_PACKET_TYPE_BROADCAST |
666                                          NDIS_PACKET_TYPE_ALL_MULTICAST |
667                                          NDIS_PACKET_TYPE_DIRECTED);
668         if (ret == 0)
669                 dev->state = RNDIS_DEV_DATAINITIALIZED;
670
671         return ret;
672 }
673
674 static int rndis_filter_close_device(struct rndis_device *dev)
675 {
676         int ret;
677
678         if (dev->state != RNDIS_DEV_DATAINITIALIZED)
679                 return 0;
680
681         ret = rndis_filter_set_packet_filter(dev, 0);
682         if (ret == 0)
683                 dev->state = RNDIS_DEV_INITIALIZED;
684
685         return ret;
686 }
687
688 int rndis_filter_device_add(struct hv_device *dev,
689                                   void *additional_info)
690 {
691         int ret;
692         struct netvsc_device *net_device;
693         struct rndis_device *rndisDevice;
694         struct netvsc_device_info *deviceInfo = additional_info;
695
696         rndisDevice = get_rndis_device();
697         if (!rndisDevice)
698                 return -ENODEV;
699
700         /*
701          * Let the inner driver handle this first to create the netvsc channel
702          * NOTE! Once the channel is created, we may get a receive callback
703          * (RndisFilterOnReceive()) before this call is completed
704          */
705         ret = netvsc_device_add(dev, additional_info);
706         if (ret != 0) {
707                 kfree(rndisDevice);
708                 return ret;
709         }
710
711
712         /* Initialize the rndis device */
713         net_device = hv_get_drvdata(dev);
714
715         net_device->extension = rndisDevice;
716         rndisDevice->net_dev = net_device;
717
718         /* Send the rndis initialization message */
719         ret = rndis_filter_init_device(rndisDevice);
720         if (ret != 0) {
721                 /*
722                  * TODO: If rndis init failed, we will need to shut down the
723                  * channel
724                  */
725         }
726
727         /* Get the mac address */
728         ret = rndis_filter_query_device_mac(rndisDevice);
729         if (ret != 0) {
730                 /*
731                  * TODO: shutdown rndis device and the channel
732                  */
733         }
734
735         memcpy(deviceInfo->mac_adr, rndisDevice->hw_mac_adr, ETH_ALEN);
736
737         rndis_filter_query_device_link_status(rndisDevice);
738
739         deviceInfo->link_state = rndisDevice->link_stat;
740
741         dev_info(&dev->device, "Device MAC %pM link state %s",
742                  rndisDevice->hw_mac_adr,
743                  ((deviceInfo->link_state) ? ("down\n") : ("up\n")));
744
745         return ret;
746 }
747
748 void rndis_filter_device_remove(struct hv_device *dev)
749 {
750         struct netvsc_device *net_dev = hv_get_drvdata(dev);
751         struct rndis_device *rndis_dev = net_dev->extension;
752
753         /* Halt and release the rndis device */
754         rndis_filter_halt_device(rndis_dev);
755
756         kfree(rndis_dev);
757         net_dev->extension = NULL;
758
759         netvsc_device_remove(dev);
760 }
761
762
763 int rndis_filter_open(struct hv_device *dev)
764 {
765         struct netvsc_device *net_device = hv_get_drvdata(dev);
766
767         if (!net_device)
768                 return -EINVAL;
769
770         return rndis_filter_open_device(net_device->extension);
771 }
772
773 int rndis_filter_close(struct hv_device *dev)
774 {
775         struct netvsc_device *netDevice = hv_get_drvdata(dev);
776
777         if (!netDevice)
778                 return -EINVAL;
779
780         return rndis_filter_close_device(netDevice->extension);
781 }
782
783 int rndis_filter_send(struct hv_device *dev,
784                              struct hv_netvsc_packet *pkt)
785 {
786         int ret;
787         struct rndis_filter_packet *filterPacket;
788         struct rndis_message *rndisMessage;
789         struct rndis_packet *rndisPacket;
790         u32 rndisMessageSize;
791
792         /* Add the rndis header */
793         filterPacket = (struct rndis_filter_packet *)pkt->extension;
794
795         memset(filterPacket, 0, sizeof(struct rndis_filter_packet));
796
797         rndisMessage = &filterPacket->msg;
798         rndisMessageSize = RNDIS_MESSAGE_SIZE(struct rndis_packet);
799
800         rndisMessage->ndis_msg_type = REMOTE_NDIS_PACKET_MSG;
801         rndisMessage->msg_len = pkt->total_data_buflen +
802                                       rndisMessageSize;
803
804         rndisPacket = &rndisMessage->msg.pkt;
805         rndisPacket->data_offset = sizeof(struct rndis_packet);
806         rndisPacket->data_len = pkt->total_data_buflen;
807
808         pkt->is_data_pkt = true;
809         pkt->page_buf[0].pfn = virt_to_phys(rndisMessage) >> PAGE_SHIFT;
810         pkt->page_buf[0].offset =
811                         (unsigned long)rndisMessage & (PAGE_SIZE-1);
812         pkt->page_buf[0].len = rndisMessageSize;
813
814         /* Save the packet send completion and context */
815         filterPacket->completion = pkt->completion.send.send_completion;
816         filterPacket->completion_ctx =
817                                 pkt->completion.send.send_completion_ctx;
818
819         /* Use ours */
820         pkt->completion.send.send_completion = rndis_filter_send_completion;
821         pkt->completion.send.send_completion_ctx = filterPacket;
822
823         ret = netvsc_send(dev, pkt);
824         if (ret != 0) {
825                 /*
826                  * Reset the completion to originals to allow retries from
827                  * above
828                  */
829                 pkt->completion.send.send_completion =
830                                 filterPacket->completion;
831                 pkt->completion.send.send_completion_ctx =
832                                 filterPacket->completion_ctx;
833         }
834
835         return ret;
836 }
837
838 static void rndis_filter_send_completion(void *ctx)
839 {
840         struct rndis_filter_packet *filterPacket = ctx;
841
842         /* Pass it back to the original handler */
843         filterPacket->completion(filterPacket->completion_ctx);
844 }
845
846
847 static void rndis_filter_send_request_completion(void *ctx)
848 {
849         /* Noop */
850 }