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