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