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