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