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