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