hyperv: Remove unnecessary comments in rndis_filter_receive_data()
[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, 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                         ret = -EINVAL;
610         }
611
612 cleanup:
613         put_rndis_request(rdev, request);
614         return ret;
615 }
616
617
618 static int rndis_filter_query_device_link_status(struct rndis_device *dev)
619 {
620         u32 size = sizeof(u32);
621         u32 link_status;
622         int ret;
623
624         ret = rndis_filter_query_device(dev,
625                                       RNDIS_OID_GEN_MEDIA_CONNECT_STATUS,
626                                       &link_status, &size);
627         dev->link_state = (link_status != 0) ? true : false;
628
629         return ret;
630 }
631
632 int rndis_filter_set_packet_filter(struct rndis_device *dev, u32 new_filter)
633 {
634         struct rndis_request *request;
635         struct rndis_set_request *set;
636         struct rndis_set_complete *set_complete;
637         u32 status;
638         int ret, t;
639         struct net_device *ndev;
640
641         ndev = dev->net_dev->ndev;
642
643         request = get_rndis_request(dev, RNDIS_MSG_SET,
644                         RNDIS_MESSAGE_SIZE(struct rndis_set_request) +
645                         sizeof(u32));
646         if (!request) {
647                 ret = -ENOMEM;
648                 goto cleanup;
649         }
650
651         /* Setup the rndis set */
652         set = &request->request_msg.msg.set_req;
653         set->oid = RNDIS_OID_GEN_CURRENT_PACKET_FILTER;
654         set->info_buflen = sizeof(u32);
655         set->info_buf_offset = sizeof(struct rndis_set_request);
656
657         memcpy((void *)(unsigned long)set + sizeof(struct rndis_set_request),
658                &new_filter, sizeof(u32));
659
660         ret = rndis_filter_send_request(dev, request);
661         if (ret != 0)
662                 goto cleanup;
663
664         t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
665
666         if (t == 0) {
667                 netdev_err(ndev,
668                         "timeout before we got a set response...\n");
669                 ret = -ETIMEDOUT;
670                 /*
671                  * We can't deallocate the request since we may still receive a
672                  * send completion for it.
673                  */
674                 goto exit;
675         } else {
676                 set_complete = &request->response_msg.msg.set_complete;
677                 status = set_complete->status;
678         }
679
680 cleanup:
681         if (request)
682                 put_rndis_request(dev, request);
683 exit:
684         return ret;
685 }
686
687
688 static int rndis_filter_init_device(struct rndis_device *dev)
689 {
690         struct rndis_request *request;
691         struct rndis_initialize_request *init;
692         struct rndis_initialize_complete *init_complete;
693         u32 status;
694         int ret, t;
695
696         request = get_rndis_request(dev, RNDIS_MSG_INIT,
697                         RNDIS_MESSAGE_SIZE(struct rndis_initialize_request));
698         if (!request) {
699                 ret = -ENOMEM;
700                 goto cleanup;
701         }
702
703         /* Setup the rndis set */
704         init = &request->request_msg.msg.init_req;
705         init->major_ver = RNDIS_MAJOR_VERSION;
706         init->minor_ver = RNDIS_MINOR_VERSION;
707         init->max_xfer_size = 0x4000;
708
709         dev->state = RNDIS_DEV_INITIALIZING;
710
711         ret = rndis_filter_send_request(dev, request);
712         if (ret != 0) {
713                 dev->state = RNDIS_DEV_UNINITIALIZED;
714                 goto cleanup;
715         }
716
717
718         t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
719
720         if (t == 0) {
721                 ret = -ETIMEDOUT;
722                 goto cleanup;
723         }
724
725         init_complete = &request->response_msg.msg.init_complete;
726         status = init_complete->status;
727         if (status == RNDIS_STATUS_SUCCESS) {
728                 dev->state = RNDIS_DEV_INITIALIZED;
729                 ret = 0;
730         } else {
731                 dev->state = RNDIS_DEV_UNINITIALIZED;
732                 ret = -EINVAL;
733         }
734
735 cleanup:
736         if (request)
737                 put_rndis_request(dev, request);
738
739         return ret;
740 }
741
742 static void rndis_filter_halt_device(struct rndis_device *dev)
743 {
744         struct rndis_request *request;
745         struct rndis_halt_request *halt;
746         struct netvsc_device *nvdev = dev->net_dev;
747         struct hv_device *hdev = nvdev->dev;
748         ulong flags;
749
750         /* Attempt to do a rndis device halt */
751         request = get_rndis_request(dev, RNDIS_MSG_HALT,
752                                 RNDIS_MESSAGE_SIZE(struct rndis_halt_request));
753         if (!request)
754                 goto cleanup;
755
756         /* Setup the rndis set */
757         halt = &request->request_msg.msg.halt_req;
758         halt->req_id = atomic_inc_return(&dev->new_req_id);
759
760         /* Ignore return since this msg is optional. */
761         rndis_filter_send_request(dev, request);
762
763         dev->state = RNDIS_DEV_UNINITIALIZED;
764
765 cleanup:
766         spin_lock_irqsave(&hdev->channel->inbound_lock, flags);
767         nvdev->destroy = true;
768         spin_unlock_irqrestore(&hdev->channel->inbound_lock, flags);
769
770         /* Wait for all send completions */
771         wait_event(nvdev->wait_drain,
772                 atomic_read(&nvdev->num_outstanding_sends) == 0);
773
774         if (request)
775                 put_rndis_request(dev, request);
776         return;
777 }
778
779 static int rndis_filter_open_device(struct rndis_device *dev)
780 {
781         int ret;
782
783         if (dev->state != RNDIS_DEV_INITIALIZED)
784                 return 0;
785
786         ret = rndis_filter_set_packet_filter(dev,
787                                          NDIS_PACKET_TYPE_BROADCAST |
788                                          NDIS_PACKET_TYPE_ALL_MULTICAST |
789                                          NDIS_PACKET_TYPE_DIRECTED);
790         if (ret == 0)
791                 dev->state = RNDIS_DEV_DATAINITIALIZED;
792
793         return ret;
794 }
795
796 static int rndis_filter_close_device(struct rndis_device *dev)
797 {
798         int ret;
799
800         if (dev->state != RNDIS_DEV_DATAINITIALIZED)
801                 return 0;
802
803         ret = rndis_filter_set_packet_filter(dev, 0);
804         if (ret == 0)
805                 dev->state = RNDIS_DEV_INITIALIZED;
806
807         return ret;
808 }
809
810 int rndis_filter_device_add(struct hv_device *dev,
811                                   void *additional_info)
812 {
813         int ret;
814         struct netvsc_device *net_device;
815         struct rndis_device *rndis_device;
816         struct netvsc_device_info *device_info = additional_info;
817
818         rndis_device = get_rndis_device();
819         if (!rndis_device)
820                 return -ENODEV;
821
822         /*
823          * Let the inner driver handle this first to create the netvsc channel
824          * NOTE! Once the channel is created, we may get a receive callback
825          * (RndisFilterOnReceive()) before this call is completed
826          */
827         ret = netvsc_device_add(dev, additional_info);
828         if (ret != 0) {
829                 kfree(rndis_device);
830                 return ret;
831         }
832
833
834         /* Initialize the rndis device */
835         net_device = hv_get_drvdata(dev);
836
837         net_device->extension = rndis_device;
838         rndis_device->net_dev = net_device;
839
840         /* Send the rndis initialization message */
841         ret = rndis_filter_init_device(rndis_device);
842         if (ret != 0) {
843                 rndis_filter_device_remove(dev);
844                 return ret;
845         }
846
847         /* Get the mac address */
848         ret = rndis_filter_query_device_mac(rndis_device);
849         if (ret != 0) {
850                 rndis_filter_device_remove(dev);
851                 return ret;
852         }
853
854         memcpy(device_info->mac_adr, rndis_device->hw_mac_adr, ETH_ALEN);
855
856         rndis_filter_query_device_link_status(rndis_device);
857
858         device_info->link_state = rndis_device->link_state;
859
860         dev_info(&dev->device, "Device MAC %pM link state %s\n",
861                  rndis_device->hw_mac_adr,
862                  device_info->link_state ? "down" : "up");
863
864         return ret;
865 }
866
867 void rndis_filter_device_remove(struct hv_device *dev)
868 {
869         struct netvsc_device *net_dev = hv_get_drvdata(dev);
870         struct rndis_device *rndis_dev = net_dev->extension;
871
872         /* Halt and release the rndis device */
873         rndis_filter_halt_device(rndis_dev);
874
875         kfree(rndis_dev);
876         net_dev->extension = NULL;
877
878         netvsc_device_remove(dev);
879 }
880
881
882 int rndis_filter_open(struct hv_device *dev)
883 {
884         struct netvsc_device *net_device = hv_get_drvdata(dev);
885
886         if (!net_device)
887                 return -EINVAL;
888
889         return rndis_filter_open_device(net_device->extension);
890 }
891
892 int rndis_filter_close(struct hv_device *dev)
893 {
894         struct netvsc_device *nvdev = hv_get_drvdata(dev);
895
896         if (!nvdev)
897                 return -EINVAL;
898
899         return rndis_filter_close_device(nvdev->extension);
900 }
901
902 int rndis_filter_send(struct hv_device *dev,
903                              struct hv_netvsc_packet *pkt)
904 {
905         int ret;
906         struct rndis_filter_packet *filter_pkt;
907         struct rndis_message *rndis_msg;
908         struct rndis_packet *rndis_pkt;
909         u32 rndis_msg_size;
910         bool isvlan = pkt->vlan_tci & VLAN_TAG_PRESENT;
911
912         /* Add the rndis header */
913         filter_pkt = (struct rndis_filter_packet *)pkt->extension;
914
915         rndis_msg = &filter_pkt->msg;
916         rndis_msg_size = RNDIS_MESSAGE_SIZE(struct rndis_packet);
917         if (isvlan)
918                 rndis_msg_size += NDIS_VLAN_PPI_SIZE;
919
920         rndis_msg->ndis_msg_type = RNDIS_MSG_PACKET;
921         rndis_msg->msg_len = pkt->total_data_buflen +
922                                       rndis_msg_size;
923
924         rndis_pkt = &rndis_msg->msg.pkt;
925         rndis_pkt->data_offset = sizeof(struct rndis_packet);
926         if (isvlan)
927                 rndis_pkt->data_offset += NDIS_VLAN_PPI_SIZE;
928         rndis_pkt->data_len = pkt->total_data_buflen;
929
930         if (isvlan) {
931                 struct rndis_per_packet_info *ppi;
932                 struct ndis_pkt_8021q_info *vlan;
933
934                 rndis_pkt->per_pkt_info_offset = sizeof(struct rndis_packet);
935                 rndis_pkt->per_pkt_info_len = NDIS_VLAN_PPI_SIZE;
936
937                 ppi = (struct rndis_per_packet_info *)((ulong)rndis_pkt +
938                         rndis_pkt->per_pkt_info_offset);
939                 ppi->size = NDIS_VLAN_PPI_SIZE;
940                 ppi->type = IEEE_8021Q_INFO;
941                 ppi->ppi_offset = sizeof(struct rndis_per_packet_info);
942
943                 vlan = (struct ndis_pkt_8021q_info *)((ulong)ppi +
944                         ppi->ppi_offset);
945                 vlan->vlanid = pkt->vlan_tci & VLAN_VID_MASK;
946                 vlan->pri = (pkt->vlan_tci & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
947         }
948
949         pkt->is_data_pkt = true;
950         pkt->page_buf[0].pfn = virt_to_phys(rndis_msg) >> PAGE_SHIFT;
951         pkt->page_buf[0].offset =
952                         (unsigned long)rndis_msg & (PAGE_SIZE-1);
953         pkt->page_buf[0].len = rndis_msg_size;
954
955         /* Add one page_buf if the rndis msg goes beyond page boundary */
956         if (pkt->page_buf[0].offset + rndis_msg_size > PAGE_SIZE) {
957                 int i;
958                 for (i = pkt->page_buf_cnt; i > 1; i--)
959                         pkt->page_buf[i] = pkt->page_buf[i-1];
960                 pkt->page_buf_cnt++;
961                 pkt->page_buf[0].len = PAGE_SIZE - pkt->page_buf[0].offset;
962                 pkt->page_buf[1].pfn = virt_to_phys((void *)((ulong)
963                         rndis_msg + pkt->page_buf[0].len)) >> PAGE_SHIFT;
964                 pkt->page_buf[1].offset = 0;
965                 pkt->page_buf[1].len = rndis_msg_size - pkt->page_buf[0].len;
966         }
967
968         /* Save the packet send completion and context */
969         filter_pkt->completion = pkt->completion.send.send_completion;
970         filter_pkt->completion_ctx =
971                                 pkt->completion.send.send_completion_ctx;
972
973         /* Use ours */
974         pkt->completion.send.send_completion = rndis_filter_send_completion;
975         pkt->completion.send.send_completion_ctx = filter_pkt;
976
977         ret = netvsc_send(dev, pkt);
978         if (ret != 0) {
979                 /*
980                  * Reset the completion to originals to allow retries from
981                  * above
982                  */
983                 pkt->completion.send.send_completion =
984                                 filter_pkt->completion;
985                 pkt->completion.send.send_completion_ctx =
986                                 filter_pkt->completion_ctx;
987         }
988
989         return ret;
990 }
991
992 static void rndis_filter_send_completion(void *ctx)
993 {
994         struct rndis_filter_packet *filter_pkt = ctx;
995
996         /* Pass it back to the original handler */
997         filter_pkt->completion(filter_pkt->completion_ctx);
998 }
999
1000
1001 static void rndis_filter_send_request_completion(void *ctx)
1002 {
1003         /* Noop */
1004 }