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