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