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