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