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