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