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