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