]> Git Repo - linux.git/blob - drivers/net/hyperv/rndis_filter.c
Merge ath-next from git://git.kernel.org/pub/scm/linux/kernel/git/kvalo/ath.git
[linux.git] / drivers / net / hyperv / rndis_filter.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2009, Microsoft Corporation.
4  *
5  * Authors:
6  *   Haiyang Zhang <[email protected]>
7  *   Hank Janssen  <[email protected]>
8  */
9 #include <linux/kernel.h>
10 #include <linux/sched.h>
11 #include <linux/wait.h>
12 #include <linux/highmem.h>
13 #include <linux/slab.h>
14 #include <linux/io.h>
15 #include <linux/if_ether.h>
16 #include <linux/netdevice.h>
17 #include <linux/if_vlan.h>
18 #include <linux/nls.h>
19 #include <linux/vmalloc.h>
20 #include <linux/rtnetlink.h>
21 #include <linux/ucs2_string.h>
22
23 #include "hyperv_net.h"
24 #include "netvsc_trace.h"
25
26 static void rndis_set_multicast(struct work_struct *w);
27
28 #define RNDIS_EXT_LEN PAGE_SIZE
29 struct rndis_request {
30         struct list_head list_ent;
31         struct completion  wait_event;
32
33         struct rndis_message response_msg;
34         /*
35          * The buffer for extended info after the RNDIS response message. It's
36          * referenced based on the data offset in the RNDIS message. Its size
37          * is enough for current needs, and should be sufficient for the near
38          * future.
39          */
40         u8 response_ext[RNDIS_EXT_LEN];
41
42         /* Simplify allocation by having a netvsc packet inline */
43         struct hv_netvsc_packet pkt;
44
45         struct rndis_message request_msg;
46         /*
47          * The buffer for the extended info after the RNDIS request message.
48          * It is referenced and sized in a similar way as response_ext.
49          */
50         u8 request_ext[RNDIS_EXT_LEN];
51 };
52
53 static const u8 netvsc_hash_key[NETVSC_HASH_KEYLEN] = {
54         0x6d, 0x5a, 0x56, 0xda, 0x25, 0x5b, 0x0e, 0xc2,
55         0x41, 0x67, 0x25, 0x3d, 0x43, 0xa3, 0x8f, 0xb0,
56         0xd0, 0xca, 0x2b, 0xcb, 0xae, 0x7b, 0x30, 0xb4,
57         0x77, 0xcb, 0x2d, 0xa3, 0x80, 0x30, 0xf2, 0x0c,
58         0x6a, 0x42, 0xb7, 0x3b, 0xbe, 0xac, 0x01, 0xfa
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         INIT_WORK(&device->mcast_work, rndis_set_multicast);
73
74         device->state = RNDIS_DEV_UNINITIALIZED;
75
76         return device;
77 }
78
79 static struct rndis_request *get_rndis_request(struct rndis_device *dev,
80                                              u32 msg_type,
81                                              u32 msg_len)
82 {
83         struct rndis_request *request;
84         struct rndis_message *rndis_msg;
85         struct rndis_set_request *set;
86         unsigned long flags;
87
88         request = kzalloc(sizeof(struct rndis_request), GFP_KERNEL);
89         if (!request)
90                 return NULL;
91
92         init_completion(&request->wait_event);
93
94         rndis_msg = &request->request_msg;
95         rndis_msg->ndis_msg_type = msg_type;
96         rndis_msg->msg_len = msg_len;
97
98         request->pkt.q_idx = 0;
99
100         /*
101          * Set the request id. This field is always after the rndis header for
102          * request/response packet types so we just used the SetRequest as a
103          * template
104          */
105         set = &rndis_msg->msg.set_req;
106         set->req_id = atomic_inc_return(&dev->new_req_id);
107
108         /* Add to the request list */
109         spin_lock_irqsave(&dev->request_lock, flags);
110         list_add_tail(&request->list_ent, &dev->req_list);
111         spin_unlock_irqrestore(&dev->request_lock, flags);
112
113         return request;
114 }
115
116 static void put_rndis_request(struct rndis_device *dev,
117                             struct rndis_request *req)
118 {
119         unsigned long flags;
120
121         spin_lock_irqsave(&dev->request_lock, flags);
122         list_del(&req->list_ent);
123         spin_unlock_irqrestore(&dev->request_lock, flags);
124
125         kfree(req);
126 }
127
128 static void dump_rndis_message(struct net_device *netdev,
129                                const struct rndis_message *rndis_msg)
130 {
131         switch (rndis_msg->ndis_msg_type) {
132         case RNDIS_MSG_PACKET:
133                 netdev_dbg(netdev, "RNDIS_MSG_PACKET (len %u, "
134                            "data offset %u data len %u, # oob %u, "
135                            "oob offset %u, oob len %u, pkt offset %u, "
136                            "pkt len %u\n",
137                            rndis_msg->msg_len,
138                            rndis_msg->msg.pkt.data_offset,
139                            rndis_msg->msg.pkt.data_len,
140                            rndis_msg->msg.pkt.num_oob_data_elements,
141                            rndis_msg->msg.pkt.oob_data_offset,
142                            rndis_msg->msg.pkt.oob_data_len,
143                            rndis_msg->msg.pkt.per_pkt_info_offset,
144                            rndis_msg->msg.pkt.per_pkt_info_len);
145                 break;
146
147         case RNDIS_MSG_INIT_C:
148                 netdev_dbg(netdev, "RNDIS_MSG_INIT_C "
149                         "(len %u, id 0x%x, status 0x%x, major %d, minor %d, "
150                         "device flags %d, max xfer size 0x%x, max pkts %u, "
151                         "pkt aligned %u)\n",
152                         rndis_msg->msg_len,
153                         rndis_msg->msg.init_complete.req_id,
154                         rndis_msg->msg.init_complete.status,
155                         rndis_msg->msg.init_complete.major_ver,
156                         rndis_msg->msg.init_complete.minor_ver,
157                         rndis_msg->msg.init_complete.dev_flags,
158                         rndis_msg->msg.init_complete.max_xfer_size,
159                         rndis_msg->msg.init_complete.
160                            max_pkt_per_msg,
161                         rndis_msg->msg.init_complete.
162                            pkt_alignment_factor);
163                 break;
164
165         case RNDIS_MSG_QUERY_C:
166                 netdev_dbg(netdev, "RNDIS_MSG_QUERY_C "
167                         "(len %u, id 0x%x, status 0x%x, buf len %u, "
168                         "buf offset %u)\n",
169                         rndis_msg->msg_len,
170                         rndis_msg->msg.query_complete.req_id,
171                         rndis_msg->msg.query_complete.status,
172                         rndis_msg->msg.query_complete.
173                            info_buflen,
174                         rndis_msg->msg.query_complete.
175                            info_buf_offset);
176                 break;
177
178         case RNDIS_MSG_SET_C:
179                 netdev_dbg(netdev,
180                         "RNDIS_MSG_SET_C (len %u, id 0x%x, status 0x%x)\n",
181                         rndis_msg->msg_len,
182                         rndis_msg->msg.set_complete.req_id,
183                         rndis_msg->msg.set_complete.status);
184                 break;
185
186         case RNDIS_MSG_INDICATE:
187                 netdev_dbg(netdev, "RNDIS_MSG_INDICATE "
188                         "(len %u, status 0x%x, buf len %u, buf offset %u)\n",
189                         rndis_msg->msg_len,
190                         rndis_msg->msg.indicate_status.status,
191                         rndis_msg->msg.indicate_status.status_buflen,
192                         rndis_msg->msg.indicate_status.status_buf_offset);
193                 break;
194
195         default:
196                 netdev_dbg(netdev, "0x%x (len %u)\n",
197                         rndis_msg->ndis_msg_type,
198                         rndis_msg->msg_len);
199                 break;
200         }
201 }
202
203 static int rndis_filter_send_request(struct rndis_device *dev,
204                                   struct rndis_request *req)
205 {
206         struct hv_netvsc_packet *packet;
207         struct hv_page_buffer page_buf[2];
208         struct hv_page_buffer *pb = page_buf;
209         int ret;
210
211         /* Setup the packet to send it */
212         packet = &req->pkt;
213
214         packet->total_data_buflen = req->request_msg.msg_len;
215         packet->page_buf_cnt = 1;
216
217         pb[0].pfn = virt_to_phys(&req->request_msg) >>
218                                         PAGE_SHIFT;
219         pb[0].len = req->request_msg.msg_len;
220         pb[0].offset =
221                 (unsigned long)&req->request_msg & (PAGE_SIZE - 1);
222
223         /* Add one page_buf when request_msg crossing page boundary */
224         if (pb[0].offset + pb[0].len > PAGE_SIZE) {
225                 packet->page_buf_cnt++;
226                 pb[0].len = PAGE_SIZE -
227                         pb[0].offset;
228                 pb[1].pfn = virt_to_phys((void *)&req->request_msg
229                         + pb[0].len) >> PAGE_SHIFT;
230                 pb[1].offset = 0;
231                 pb[1].len = req->request_msg.msg_len -
232                         pb[0].len;
233         }
234
235         trace_rndis_send(dev->ndev, 0, &req->request_msg);
236
237         rcu_read_lock_bh();
238         ret = netvsc_send(dev->ndev, packet, NULL, pb, NULL);
239         rcu_read_unlock_bh();
240
241         return ret;
242 }
243
244 static void rndis_set_link_state(struct rndis_device *rdev,
245                                  struct rndis_request *request)
246 {
247         u32 link_status;
248         struct rndis_query_complete *query_complete;
249
250         query_complete = &request->response_msg.msg.query_complete;
251
252         if (query_complete->status == RNDIS_STATUS_SUCCESS &&
253             query_complete->info_buflen == sizeof(u32)) {
254                 memcpy(&link_status, (void *)((unsigned long)query_complete +
255                        query_complete->info_buf_offset), sizeof(u32));
256                 rdev->link_state = link_status != 0;
257         }
258 }
259
260 static void rndis_filter_receive_response(struct net_device *ndev,
261                                           struct netvsc_device *nvdev,
262                                           const struct rndis_message *resp)
263 {
264         struct rndis_device *dev = nvdev->extension;
265         struct rndis_request *request = NULL;
266         bool found = false;
267         unsigned long flags;
268
269         /* This should never happen, it means control message
270          * response received after device removed.
271          */
272         if (dev->state == RNDIS_DEV_UNINITIALIZED) {
273                 netdev_err(ndev,
274                            "got rndis message uninitialized\n");
275                 return;
276         }
277
278         spin_lock_irqsave(&dev->request_lock, flags);
279         list_for_each_entry(request, &dev->req_list, list_ent) {
280                 /*
281                  * All request/response message contains RequestId as the 1st
282                  * field
283                  */
284                 if (request->request_msg.msg.init_req.req_id
285                     == resp->msg.init_complete.req_id) {
286                         found = true;
287                         break;
288                 }
289         }
290         spin_unlock_irqrestore(&dev->request_lock, flags);
291
292         if (found) {
293                 if (resp->msg_len <=
294                     sizeof(struct rndis_message) + RNDIS_EXT_LEN) {
295                         memcpy(&request->response_msg, resp,
296                                resp->msg_len);
297                         if (request->request_msg.ndis_msg_type ==
298                             RNDIS_MSG_QUERY && request->request_msg.msg.
299                             query_req.oid == RNDIS_OID_GEN_MEDIA_CONNECT_STATUS)
300                                 rndis_set_link_state(dev, request);
301                 } else {
302                         netdev_err(ndev,
303                                 "rndis response buffer overflow "
304                                 "detected (size %u max %zu)\n",
305                                 resp->msg_len,
306                                 sizeof(struct rndis_message));
307
308                         if (resp->ndis_msg_type ==
309                             RNDIS_MSG_RESET_C) {
310                                 /* does not have a request id field */
311                                 request->response_msg.msg.reset_complete.
312                                         status = RNDIS_STATUS_BUFFER_OVERFLOW;
313                         } else {
314                                 request->response_msg.msg.
315                                 init_complete.status =
316                                         RNDIS_STATUS_BUFFER_OVERFLOW;
317                         }
318                 }
319
320                 complete(&request->wait_event);
321         } else {
322                 netdev_err(ndev,
323                         "no rndis request found for this response "
324                         "(id 0x%x res type 0x%x)\n",
325                         resp->msg.init_complete.req_id,
326                         resp->ndis_msg_type);
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,
335                                   u32 type, u8 internal)
336 {
337         struct rndis_per_packet_info *ppi;
338         int len;
339
340         if (rpkt->per_pkt_info_offset == 0)
341                 return NULL;
342
343         ppi = (struct rndis_per_packet_info *)((ulong)rpkt +
344                 rpkt->per_pkt_info_offset);
345         len = rpkt->per_pkt_info_len;
346
347         while (len > 0) {
348                 if (ppi->type == type && ppi->internal == internal)
349                         return (void *)((ulong)ppi + ppi->ppi_offset);
350                 len -= ppi->size;
351                 ppi = (struct rndis_per_packet_info *)((ulong)ppi + ppi->size);
352         }
353
354         return NULL;
355 }
356
357 static inline
358 void rsc_add_data(struct netvsc_channel *nvchan,
359                   const struct ndis_pkt_8021q_info *vlan,
360                   const struct ndis_tcp_ip_checksum_info *csum_info,
361                   const u32 *hash_info,
362                   void *data, u32 len)
363 {
364         u32 cnt = nvchan->rsc.cnt;
365
366         if (cnt) {
367                 nvchan->rsc.pktlen += len;
368         } else {
369                 nvchan->rsc.vlan = vlan;
370                 nvchan->rsc.csum_info = csum_info;
371                 nvchan->rsc.pktlen = len;
372                 nvchan->rsc.hash_info = hash_info;
373         }
374
375         nvchan->rsc.data[cnt] = data;
376         nvchan->rsc.len[cnt] = len;
377         nvchan->rsc.cnt++;
378 }
379
380 static int rndis_filter_receive_data(struct net_device *ndev,
381                                      struct netvsc_device *nvdev,
382                                      struct netvsc_channel *nvchan,
383                                      struct rndis_message *msg,
384                                      u32 data_buflen)
385 {
386         struct rndis_packet *rndis_pkt = &msg->msg.pkt;
387         const struct ndis_tcp_ip_checksum_info *csum_info;
388         const struct ndis_pkt_8021q_info *vlan;
389         const struct rndis_pktinfo_id *pktinfo_id;
390         const u32 *hash_info;
391         u32 data_offset;
392         void *data;
393         bool rsc_more = false;
394         int ret;
395
396         /* Remove the rndis header and pass it back up the stack */
397         data_offset = RNDIS_HEADER_SIZE + rndis_pkt->data_offset;
398
399         data_buflen -= data_offset;
400
401         /*
402          * Make sure we got a valid RNDIS message, now total_data_buflen
403          * should be the data packet size plus the trailer padding size
404          */
405         if (unlikely(data_buflen < rndis_pkt->data_len)) {
406                 netdev_err(ndev, "rndis message buffer "
407                            "overflow detected (got %u, min %u)"
408                            "...dropping this message!\n",
409                            data_buflen, rndis_pkt->data_len);
410                 return NVSP_STAT_FAIL;
411         }
412
413         vlan = rndis_get_ppi(rndis_pkt, IEEE_8021Q_INFO, 0);
414
415         csum_info = rndis_get_ppi(rndis_pkt, TCPIP_CHKSUM_PKTINFO, 0);
416
417         hash_info = rndis_get_ppi(rndis_pkt, NBL_HASH_VALUE, 0);
418
419         pktinfo_id = rndis_get_ppi(rndis_pkt, RNDIS_PKTINFO_ID, 1);
420
421         data = (void *)msg + data_offset;
422
423         /* Identify RSC frags, drop erroneous packets */
424         if (pktinfo_id && (pktinfo_id->flag & RNDIS_PKTINFO_SUBALLOC)) {
425                 if (pktinfo_id->flag & RNDIS_PKTINFO_1ST_FRAG)
426                         nvchan->rsc.cnt = 0;
427                 else if (nvchan->rsc.cnt == 0)
428                         goto drop;
429
430                 rsc_more = true;
431
432                 if (pktinfo_id->flag & RNDIS_PKTINFO_LAST_FRAG)
433                         rsc_more = false;
434
435                 if (rsc_more && nvchan->rsc.is_last)
436                         goto drop;
437         } else {
438                 nvchan->rsc.cnt = 0;
439         }
440
441         if (unlikely(nvchan->rsc.cnt >= NVSP_RSC_MAX))
442                 goto drop;
443
444         /* Put data into per channel structure.
445          * Also, remove the rndis trailer padding from rndis packet message
446          * rndis_pkt->data_len tell us the real data length, we only copy
447          * the data packet to the stack, without the rndis trailer padding
448          */
449         rsc_add_data(nvchan, vlan, csum_info, hash_info,
450                      data, rndis_pkt->data_len);
451
452         if (rsc_more)
453                 return NVSP_STAT_SUCCESS;
454
455         ret = netvsc_recv_callback(ndev, nvdev, nvchan);
456         nvchan->rsc.cnt = 0;
457
458         return ret;
459
460 drop:
461         /* Drop incomplete packet */
462         nvchan->rsc.cnt = 0;
463         return NVSP_STAT_FAIL;
464 }
465
466 int rndis_filter_receive(struct net_device *ndev,
467                          struct netvsc_device *net_dev,
468                          struct netvsc_channel *nvchan,
469                          void *data, u32 buflen)
470 {
471         struct net_device_context *net_device_ctx = netdev_priv(ndev);
472         struct rndis_message *rndis_msg = data;
473
474         if (netif_msg_rx_status(net_device_ctx))
475                 dump_rndis_message(ndev, rndis_msg);
476
477         switch (rndis_msg->ndis_msg_type) {
478         case RNDIS_MSG_PACKET:
479                 return rndis_filter_receive_data(ndev, net_dev, nvchan,
480                                                  rndis_msg, buflen);
481         case RNDIS_MSG_INIT_C:
482         case RNDIS_MSG_QUERY_C:
483         case RNDIS_MSG_SET_C:
484                 /* completion msgs */
485                 rndis_filter_receive_response(ndev, net_dev, rndis_msg);
486                 break;
487
488         case RNDIS_MSG_INDICATE:
489                 /* notification msgs */
490                 netvsc_linkstatus_callback(ndev, rndis_msg);
491                 break;
492         default:
493                 netdev_err(ndev,
494                         "unhandled rndis message (type %u len %u)\n",
495                            rndis_msg->ndis_msg_type,
496                            rndis_msg->msg_len);
497                 return NVSP_STAT_FAIL;
498         }
499
500         return NVSP_STAT_SUCCESS;
501 }
502
503 static int rndis_filter_query_device(struct rndis_device *dev,
504                                      struct netvsc_device *nvdev,
505                                      u32 oid, void *result, u32 *result_size)
506 {
507         struct rndis_request *request;
508         u32 inresult_size = *result_size;
509         struct rndis_query_request *query;
510         struct rndis_query_complete *query_complete;
511         int ret = 0;
512
513         if (!result)
514                 return -EINVAL;
515
516         *result_size = 0;
517         request = get_rndis_request(dev, RNDIS_MSG_QUERY,
518                         RNDIS_MESSAGE_SIZE(struct rndis_query_request));
519         if (!request) {
520                 ret = -ENOMEM;
521                 goto cleanup;
522         }
523
524         /* Setup the rndis query */
525         query = &request->request_msg.msg.query_req;
526         query->oid = oid;
527         query->info_buf_offset = sizeof(struct rndis_query_request);
528         query->info_buflen = 0;
529         query->dev_vc_handle = 0;
530
531         if (oid == OID_TCP_OFFLOAD_HARDWARE_CAPABILITIES) {
532                 struct ndis_offload *hwcaps;
533                 u32 nvsp_version = nvdev->nvsp_version;
534                 u8 ndis_rev;
535                 size_t size;
536
537                 if (nvsp_version >= NVSP_PROTOCOL_VERSION_5) {
538                         ndis_rev = NDIS_OFFLOAD_PARAMETERS_REVISION_3;
539                         size = NDIS_OFFLOAD_SIZE;
540                 } else if (nvsp_version >= NVSP_PROTOCOL_VERSION_4) {
541                         ndis_rev = NDIS_OFFLOAD_PARAMETERS_REVISION_2;
542                         size = NDIS_OFFLOAD_SIZE_6_1;
543                 } else {
544                         ndis_rev = NDIS_OFFLOAD_PARAMETERS_REVISION_1;
545                         size = NDIS_OFFLOAD_SIZE_6_0;
546                 }
547
548                 request->request_msg.msg_len += size;
549                 query->info_buflen = size;
550                 hwcaps = (struct ndis_offload *)
551                         ((unsigned long)query + query->info_buf_offset);
552
553                 hwcaps->header.type = NDIS_OBJECT_TYPE_OFFLOAD;
554                 hwcaps->header.revision = ndis_rev;
555                 hwcaps->header.size = size;
556
557         } else if (oid == OID_GEN_RECEIVE_SCALE_CAPABILITIES) {
558                 struct ndis_recv_scale_cap *cap;
559
560                 request->request_msg.msg_len +=
561                         sizeof(struct ndis_recv_scale_cap);
562                 query->info_buflen = sizeof(struct ndis_recv_scale_cap);
563                 cap = (struct ndis_recv_scale_cap *)((unsigned long)query +
564                                                      query->info_buf_offset);
565                 cap->hdr.type = NDIS_OBJECT_TYPE_RSS_CAPABILITIES;
566                 cap->hdr.rev = NDIS_RECEIVE_SCALE_CAPABILITIES_REVISION_2;
567                 cap->hdr.size = sizeof(struct ndis_recv_scale_cap);
568         }
569
570         ret = rndis_filter_send_request(dev, request);
571         if (ret != 0)
572                 goto cleanup;
573
574         wait_for_completion(&request->wait_event);
575
576         /* Copy the response back */
577         query_complete = &request->response_msg.msg.query_complete;
578
579         if (query_complete->info_buflen > inresult_size) {
580                 ret = -1;
581                 goto cleanup;
582         }
583
584         memcpy(result,
585                (void *)((unsigned long)query_complete +
586                          query_complete->info_buf_offset),
587                query_complete->info_buflen);
588
589         *result_size = query_complete->info_buflen;
590
591 cleanup:
592         if (request)
593                 put_rndis_request(dev, request);
594
595         return ret;
596 }
597
598 /* Get the hardware offload capabilities */
599 static int
600 rndis_query_hwcaps(struct rndis_device *dev, struct netvsc_device *net_device,
601                    struct ndis_offload *caps)
602 {
603         u32 caps_len = sizeof(*caps);
604         int ret;
605
606         memset(caps, 0, sizeof(*caps));
607
608         ret = rndis_filter_query_device(dev, net_device,
609                                         OID_TCP_OFFLOAD_HARDWARE_CAPABILITIES,
610                                         caps, &caps_len);
611         if (ret)
612                 return ret;
613
614         if (caps->header.type != NDIS_OBJECT_TYPE_OFFLOAD) {
615                 netdev_warn(dev->ndev, "invalid NDIS objtype %#x\n",
616                             caps->header.type);
617                 return -EINVAL;
618         }
619
620         if (caps->header.revision < NDIS_OFFLOAD_PARAMETERS_REVISION_1) {
621                 netdev_warn(dev->ndev, "invalid NDIS objrev %x\n",
622                             caps->header.revision);
623                 return -EINVAL;
624         }
625
626         if (caps->header.size > caps_len ||
627             caps->header.size < NDIS_OFFLOAD_SIZE_6_0) {
628                 netdev_warn(dev->ndev,
629                             "invalid NDIS objsize %u, data size %u\n",
630                             caps->header.size, caps_len);
631                 return -EINVAL;
632         }
633
634         return 0;
635 }
636
637 static int rndis_filter_query_device_mac(struct rndis_device *dev,
638                                          struct netvsc_device *net_device)
639 {
640         u32 size = ETH_ALEN;
641
642         return rndis_filter_query_device(dev, net_device,
643                                       RNDIS_OID_802_3_PERMANENT_ADDRESS,
644                                       dev->hw_mac_adr, &size);
645 }
646
647 #define NWADR_STR "NetworkAddress"
648 #define NWADR_STRLEN 14
649
650 int rndis_filter_set_device_mac(struct netvsc_device *nvdev,
651                                 const char *mac)
652 {
653         struct rndis_device *rdev = nvdev->extension;
654         struct rndis_request *request;
655         struct rndis_set_request *set;
656         struct rndis_config_parameter_info *cpi;
657         wchar_t *cfg_nwadr, *cfg_mac;
658         struct rndis_set_complete *set_complete;
659         char macstr[2*ETH_ALEN+1];
660         u32 extlen = sizeof(struct rndis_config_parameter_info) +
661                 2*NWADR_STRLEN + 4*ETH_ALEN;
662         int ret;
663
664         request = get_rndis_request(rdev, RNDIS_MSG_SET,
665                 RNDIS_MESSAGE_SIZE(struct rndis_set_request) + extlen);
666         if (!request)
667                 return -ENOMEM;
668
669         set = &request->request_msg.msg.set_req;
670         set->oid = RNDIS_OID_GEN_RNDIS_CONFIG_PARAMETER;
671         set->info_buflen = extlen;
672         set->info_buf_offset = sizeof(struct rndis_set_request);
673         set->dev_vc_handle = 0;
674
675         cpi = (struct rndis_config_parameter_info *)((ulong)set +
676                 set->info_buf_offset);
677         cpi->parameter_name_offset =
678                 sizeof(struct rndis_config_parameter_info);
679         /* Multiply by 2 because host needs 2 bytes (utf16) for each char */
680         cpi->parameter_name_length = 2*NWADR_STRLEN;
681         cpi->parameter_type = RNDIS_CONFIG_PARAM_TYPE_STRING;
682         cpi->parameter_value_offset =
683                 cpi->parameter_name_offset + cpi->parameter_name_length;
684         /* Multiply by 4 because each MAC byte displayed as 2 utf16 chars */
685         cpi->parameter_value_length = 4*ETH_ALEN;
686
687         cfg_nwadr = (wchar_t *)((ulong)cpi + cpi->parameter_name_offset);
688         cfg_mac = (wchar_t *)((ulong)cpi + cpi->parameter_value_offset);
689         ret = utf8s_to_utf16s(NWADR_STR, NWADR_STRLEN, UTF16_HOST_ENDIAN,
690                               cfg_nwadr, NWADR_STRLEN);
691         if (ret < 0)
692                 goto cleanup;
693         snprintf(macstr, 2*ETH_ALEN+1, "%pm", mac);
694         ret = utf8s_to_utf16s(macstr, 2*ETH_ALEN, UTF16_HOST_ENDIAN,
695                               cfg_mac, 2*ETH_ALEN);
696         if (ret < 0)
697                 goto cleanup;
698
699         ret = rndis_filter_send_request(rdev, request);
700         if (ret != 0)
701                 goto cleanup;
702
703         wait_for_completion(&request->wait_event);
704
705         set_complete = &request->response_msg.msg.set_complete;
706         if (set_complete->status != RNDIS_STATUS_SUCCESS)
707                 ret = -EIO;
708
709 cleanup:
710         put_rndis_request(rdev, request);
711         return ret;
712 }
713
714 int
715 rndis_filter_set_offload_params(struct net_device *ndev,
716                                 struct netvsc_device *nvdev,
717                                 struct ndis_offload_params *req_offloads)
718 {
719         struct rndis_device *rdev = nvdev->extension;
720         struct rndis_request *request;
721         struct rndis_set_request *set;
722         struct ndis_offload_params *offload_params;
723         struct rndis_set_complete *set_complete;
724         u32 extlen = sizeof(struct ndis_offload_params);
725         int ret;
726         u32 vsp_version = nvdev->nvsp_version;
727
728         if (vsp_version <= NVSP_PROTOCOL_VERSION_4) {
729                 extlen = VERSION_4_OFFLOAD_SIZE;
730                 /* On NVSP_PROTOCOL_VERSION_4 and below, we do not support
731                  * UDP checksum offload.
732                  */
733                 req_offloads->udp_ip_v4_csum = 0;
734                 req_offloads->udp_ip_v6_csum = 0;
735         }
736
737         request = get_rndis_request(rdev, RNDIS_MSG_SET,
738                 RNDIS_MESSAGE_SIZE(struct rndis_set_request) + extlen);
739         if (!request)
740                 return -ENOMEM;
741
742         set = &request->request_msg.msg.set_req;
743         set->oid = OID_TCP_OFFLOAD_PARAMETERS;
744         set->info_buflen = extlen;
745         set->info_buf_offset = sizeof(struct rndis_set_request);
746         set->dev_vc_handle = 0;
747
748         offload_params = (struct ndis_offload_params *)((ulong)set +
749                                 set->info_buf_offset);
750         *offload_params = *req_offloads;
751         offload_params->header.type = NDIS_OBJECT_TYPE_DEFAULT;
752         offload_params->header.revision = NDIS_OFFLOAD_PARAMETERS_REVISION_3;
753         offload_params->header.size = extlen;
754
755         ret = rndis_filter_send_request(rdev, request);
756         if (ret != 0)
757                 goto cleanup;
758
759         wait_for_completion(&request->wait_event);
760         set_complete = &request->response_msg.msg.set_complete;
761         if (set_complete->status != RNDIS_STATUS_SUCCESS) {
762                 netdev_err(ndev, "Fail to set offload on host side:0x%x\n",
763                            set_complete->status);
764                 ret = -EINVAL;
765         }
766
767 cleanup:
768         put_rndis_request(rdev, request);
769         return ret;
770 }
771
772 static int rndis_set_rss_param_msg(struct rndis_device *rdev,
773                                    const u8 *rss_key, u16 flag)
774 {
775         struct net_device *ndev = rdev->ndev;
776         struct rndis_request *request;
777         struct rndis_set_request *set;
778         struct rndis_set_complete *set_complete;
779         u32 extlen = sizeof(struct ndis_recv_scale_param) +
780                      4 * ITAB_NUM + NETVSC_HASH_KEYLEN;
781         struct ndis_recv_scale_param *rssp;
782         u32 *itab;
783         u8 *keyp;
784         int i, ret;
785
786         request = get_rndis_request(
787                         rdev, RNDIS_MSG_SET,
788                         RNDIS_MESSAGE_SIZE(struct rndis_set_request) + extlen);
789         if (!request)
790                 return -ENOMEM;
791
792         set = &request->request_msg.msg.set_req;
793         set->oid = OID_GEN_RECEIVE_SCALE_PARAMETERS;
794         set->info_buflen = extlen;
795         set->info_buf_offset = sizeof(struct rndis_set_request);
796         set->dev_vc_handle = 0;
797
798         rssp = (struct ndis_recv_scale_param *)(set + 1);
799         rssp->hdr.type = NDIS_OBJECT_TYPE_RSS_PARAMETERS;
800         rssp->hdr.rev = NDIS_RECEIVE_SCALE_PARAMETERS_REVISION_2;
801         rssp->hdr.size = sizeof(struct ndis_recv_scale_param);
802         rssp->flag = flag;
803         rssp->hashinfo = NDIS_HASH_FUNC_TOEPLITZ | NDIS_HASH_IPV4 |
804                          NDIS_HASH_TCP_IPV4 | NDIS_HASH_IPV6 |
805                          NDIS_HASH_TCP_IPV6;
806         rssp->indirect_tabsize = 4*ITAB_NUM;
807         rssp->indirect_taboffset = sizeof(struct ndis_recv_scale_param);
808         rssp->hashkey_size = NETVSC_HASH_KEYLEN;
809         rssp->hashkey_offset = rssp->indirect_taboffset +
810                                rssp->indirect_tabsize;
811
812         /* Set indirection table entries */
813         itab = (u32 *)(rssp + 1);
814         for (i = 0; i < ITAB_NUM; i++)
815                 itab[i] = rdev->rx_table[i];
816
817         /* Set hask key values */
818         keyp = (u8 *)((unsigned long)rssp + rssp->hashkey_offset);
819         memcpy(keyp, rss_key, NETVSC_HASH_KEYLEN);
820
821         ret = rndis_filter_send_request(rdev, request);
822         if (ret != 0)
823                 goto cleanup;
824
825         wait_for_completion(&request->wait_event);
826         set_complete = &request->response_msg.msg.set_complete;
827         if (set_complete->status == RNDIS_STATUS_SUCCESS) {
828                 if (!(flag & NDIS_RSS_PARAM_FLAG_DISABLE_RSS) &&
829                     !(flag & NDIS_RSS_PARAM_FLAG_HASH_KEY_UNCHANGED))
830                         memcpy(rdev->rss_key, rss_key, NETVSC_HASH_KEYLEN);
831
832         } else {
833                 netdev_err(ndev, "Fail to set RSS parameters:0x%x\n",
834                            set_complete->status);
835                 ret = -EINVAL;
836         }
837
838 cleanup:
839         put_rndis_request(rdev, request);
840         return ret;
841 }
842
843 int rndis_filter_set_rss_param(struct rndis_device *rdev,
844                                const u8 *rss_key)
845 {
846         /* Disable RSS before change */
847         rndis_set_rss_param_msg(rdev, rss_key,
848                                 NDIS_RSS_PARAM_FLAG_DISABLE_RSS);
849
850         return rndis_set_rss_param_msg(rdev, rss_key, 0);
851 }
852
853 static int rndis_filter_query_device_link_status(struct rndis_device *dev,
854                                                  struct netvsc_device *net_device)
855 {
856         u32 size = sizeof(u32);
857         u32 link_status;
858
859         return rndis_filter_query_device(dev, net_device,
860                                          RNDIS_OID_GEN_MEDIA_CONNECT_STATUS,
861                                          &link_status, &size);
862 }
863
864 static int rndis_filter_query_link_speed(struct rndis_device *dev,
865                                          struct netvsc_device *net_device)
866 {
867         u32 size = sizeof(u32);
868         u32 link_speed;
869         struct net_device_context *ndc;
870         int ret;
871
872         ret = rndis_filter_query_device(dev, net_device,
873                                         RNDIS_OID_GEN_LINK_SPEED,
874                                         &link_speed, &size);
875
876         if (!ret) {
877                 ndc = netdev_priv(dev->ndev);
878
879                 /* The link speed reported from host is in 100bps unit, so
880                  * we convert it to Mbps here.
881                  */
882                 ndc->speed = link_speed / 10000;
883         }
884
885         return ret;
886 }
887
888 static int rndis_filter_set_packet_filter(struct rndis_device *dev,
889                                           u32 new_filter)
890 {
891         struct rndis_request *request;
892         struct rndis_set_request *set;
893         int ret;
894
895         if (dev->filter == new_filter)
896                 return 0;
897
898         request = get_rndis_request(dev, RNDIS_MSG_SET,
899                         RNDIS_MESSAGE_SIZE(struct rndis_set_request) +
900                         sizeof(u32));
901         if (!request)
902                 return -ENOMEM;
903
904         /* Setup the rndis set */
905         set = &request->request_msg.msg.set_req;
906         set->oid = RNDIS_OID_GEN_CURRENT_PACKET_FILTER;
907         set->info_buflen = sizeof(u32);
908         set->info_buf_offset = sizeof(struct rndis_set_request);
909
910         memcpy((void *)(unsigned long)set + sizeof(struct rndis_set_request),
911                &new_filter, sizeof(u32));
912
913         ret = rndis_filter_send_request(dev, request);
914         if (ret == 0) {
915                 wait_for_completion(&request->wait_event);
916                 dev->filter = new_filter;
917         }
918
919         put_rndis_request(dev, request);
920
921         return ret;
922 }
923
924 static void rndis_set_multicast(struct work_struct *w)
925 {
926         struct rndis_device *rdev
927                 = container_of(w, struct rndis_device, mcast_work);
928         u32 filter = NDIS_PACKET_TYPE_DIRECTED;
929         unsigned int flags = rdev->ndev->flags;
930
931         if (flags & IFF_PROMISC) {
932                 filter = NDIS_PACKET_TYPE_PROMISCUOUS;
933         } else {
934                 if (!netdev_mc_empty(rdev->ndev) || (flags & IFF_ALLMULTI))
935                         filter |= NDIS_PACKET_TYPE_ALL_MULTICAST;
936                 if (flags & IFF_BROADCAST)
937                         filter |= NDIS_PACKET_TYPE_BROADCAST;
938         }
939
940         rndis_filter_set_packet_filter(rdev, filter);
941 }
942
943 void rndis_filter_update(struct netvsc_device *nvdev)
944 {
945         struct rndis_device *rdev = nvdev->extension;
946
947         schedule_work(&rdev->mcast_work);
948 }
949
950 static int rndis_filter_init_device(struct rndis_device *dev,
951                                     struct netvsc_device *nvdev)
952 {
953         struct rndis_request *request;
954         struct rndis_initialize_request *init;
955         struct rndis_initialize_complete *init_complete;
956         u32 status;
957         int ret;
958
959         request = get_rndis_request(dev, RNDIS_MSG_INIT,
960                         RNDIS_MESSAGE_SIZE(struct rndis_initialize_request));
961         if (!request) {
962                 ret = -ENOMEM;
963                 goto cleanup;
964         }
965
966         /* Setup the rndis set */
967         init = &request->request_msg.msg.init_req;
968         init->major_ver = RNDIS_MAJOR_VERSION;
969         init->minor_ver = RNDIS_MINOR_VERSION;
970         init->max_xfer_size = 0x4000;
971
972         dev->state = RNDIS_DEV_INITIALIZING;
973
974         ret = rndis_filter_send_request(dev, request);
975         if (ret != 0) {
976                 dev->state = RNDIS_DEV_UNINITIALIZED;
977                 goto cleanup;
978         }
979
980         wait_for_completion(&request->wait_event);
981
982         init_complete = &request->response_msg.msg.init_complete;
983         status = init_complete->status;
984         if (status == RNDIS_STATUS_SUCCESS) {
985                 dev->state = RNDIS_DEV_INITIALIZED;
986                 nvdev->max_pkt = init_complete->max_pkt_per_msg;
987                 nvdev->pkt_align = 1 << init_complete->pkt_alignment_factor;
988                 ret = 0;
989         } else {
990                 dev->state = RNDIS_DEV_UNINITIALIZED;
991                 ret = -EINVAL;
992         }
993
994 cleanup:
995         if (request)
996                 put_rndis_request(dev, request);
997
998         return ret;
999 }
1000
1001 static bool netvsc_device_idle(const struct netvsc_device *nvdev)
1002 {
1003         int i;
1004
1005         for (i = 0; i < nvdev->num_chn; i++) {
1006                 const struct netvsc_channel *nvchan = &nvdev->chan_table[i];
1007
1008                 if (nvchan->mrc.first != nvchan->mrc.next)
1009                         return false;
1010
1011                 if (atomic_read(&nvchan->queue_sends) > 0)
1012                         return false;
1013         }
1014
1015         return true;
1016 }
1017
1018 static void rndis_filter_halt_device(struct netvsc_device *nvdev,
1019                                      struct rndis_device *dev)
1020 {
1021         struct rndis_request *request;
1022         struct rndis_halt_request *halt;
1023
1024         /* Attempt to do a rndis device halt */
1025         request = get_rndis_request(dev, RNDIS_MSG_HALT,
1026                                 RNDIS_MESSAGE_SIZE(struct rndis_halt_request));
1027         if (!request)
1028                 goto cleanup;
1029
1030         /* Setup the rndis set */
1031         halt = &request->request_msg.msg.halt_req;
1032         halt->req_id = atomic_inc_return(&dev->new_req_id);
1033
1034         /* Ignore return since this msg is optional. */
1035         rndis_filter_send_request(dev, request);
1036
1037         dev->state = RNDIS_DEV_UNINITIALIZED;
1038
1039 cleanup:
1040         nvdev->destroy = true;
1041
1042         /* Force flag to be ordered before waiting */
1043         wmb();
1044
1045         /* Wait for all send completions */
1046         wait_event(nvdev->wait_drain, netvsc_device_idle(nvdev));
1047
1048         if (request)
1049                 put_rndis_request(dev, request);
1050 }
1051
1052 static int rndis_filter_open_device(struct rndis_device *dev)
1053 {
1054         int ret;
1055
1056         if (dev->state != RNDIS_DEV_INITIALIZED)
1057                 return 0;
1058
1059         ret = rndis_filter_set_packet_filter(dev,
1060                                          NDIS_PACKET_TYPE_BROADCAST |
1061                                          NDIS_PACKET_TYPE_ALL_MULTICAST |
1062                                          NDIS_PACKET_TYPE_DIRECTED);
1063         if (ret == 0)
1064                 dev->state = RNDIS_DEV_DATAINITIALIZED;
1065
1066         return ret;
1067 }
1068
1069 static int rndis_filter_close_device(struct rndis_device *dev)
1070 {
1071         int ret;
1072
1073         if (dev->state != RNDIS_DEV_DATAINITIALIZED)
1074                 return 0;
1075
1076         /* Make sure rndis_set_multicast doesn't re-enable filter! */
1077         cancel_work_sync(&dev->mcast_work);
1078
1079         ret = rndis_filter_set_packet_filter(dev, 0);
1080         if (ret == -ENODEV)
1081                 ret = 0;
1082
1083         if (ret == 0)
1084                 dev->state = RNDIS_DEV_INITIALIZED;
1085
1086         return ret;
1087 }
1088
1089 static void netvsc_sc_open(struct vmbus_channel *new_sc)
1090 {
1091         struct net_device *ndev =
1092                 hv_get_drvdata(new_sc->primary_channel->device_obj);
1093         struct net_device_context *ndev_ctx = netdev_priv(ndev);
1094         struct netvsc_device *nvscdev;
1095         u16 chn_index = new_sc->offermsg.offer.sub_channel_index;
1096         struct netvsc_channel *nvchan;
1097         int ret;
1098
1099         /* This is safe because this callback only happens when
1100          * new device is being setup and waiting on the channel_init_wait.
1101          */
1102         nvscdev = rcu_dereference_raw(ndev_ctx->nvdev);
1103         if (!nvscdev || chn_index >= nvscdev->num_chn)
1104                 return;
1105
1106         nvchan = nvscdev->chan_table + chn_index;
1107
1108         /* Because the device uses NAPI, all the interrupt batching and
1109          * control is done via Net softirq, not the channel handling
1110          */
1111         set_channel_read_mode(new_sc, HV_CALL_ISR);
1112
1113         /* Set the channel before opening.*/
1114         nvchan->channel = new_sc;
1115
1116         ret = vmbus_open(new_sc, netvsc_ring_bytes,
1117                          netvsc_ring_bytes, NULL, 0,
1118                          netvsc_channel_cb, nvchan);
1119         if (ret == 0)
1120                 napi_enable(&nvchan->napi);
1121         else
1122                 netdev_notice(ndev, "sub channel open failed: %d\n", ret);
1123
1124         if (atomic_inc_return(&nvscdev->open_chn) == nvscdev->num_chn)
1125                 wake_up(&nvscdev->subchan_open);
1126 }
1127
1128 /* Open sub-channels after completing the handling of the device probe.
1129  * This breaks overlap of processing the host message for the
1130  * new primary channel with the initialization of sub-channels.
1131  */
1132 int rndis_set_subchannel(struct net_device *ndev,
1133                          struct netvsc_device *nvdev,
1134                          struct netvsc_device_info *dev_info)
1135 {
1136         struct nvsp_message *init_packet = &nvdev->channel_init_pkt;
1137         struct net_device_context *ndev_ctx = netdev_priv(ndev);
1138         struct hv_device *hv_dev = ndev_ctx->device_ctx;
1139         struct rndis_device *rdev = nvdev->extension;
1140         int i, ret;
1141
1142         ASSERT_RTNL();
1143
1144         memset(init_packet, 0, sizeof(struct nvsp_message));
1145         init_packet->hdr.msg_type = NVSP_MSG5_TYPE_SUBCHANNEL;
1146         init_packet->msg.v5_msg.subchn_req.op = NVSP_SUBCHANNEL_ALLOCATE;
1147         init_packet->msg.v5_msg.subchn_req.num_subchannels =
1148                                                 nvdev->num_chn - 1;
1149         trace_nvsp_send(ndev, init_packet);
1150
1151         ret = vmbus_sendpacket(hv_dev->channel, init_packet,
1152                                sizeof(struct nvsp_message),
1153                                (unsigned long)init_packet,
1154                                VM_PKT_DATA_INBAND,
1155                                VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1156         if (ret) {
1157                 netdev_err(ndev, "sub channel allocate send failed: %d\n", ret);
1158                 return ret;
1159         }
1160
1161         wait_for_completion(&nvdev->channel_init_wait);
1162         if (init_packet->msg.v5_msg.subchn_comp.status != NVSP_STAT_SUCCESS) {
1163                 netdev_err(ndev, "sub channel request failed\n");
1164                 return -EIO;
1165         }
1166
1167         nvdev->num_chn = 1 +
1168                 init_packet->msg.v5_msg.subchn_comp.num_subchannels;
1169
1170         /* wait for all sub channels to open */
1171         wait_event(nvdev->subchan_open,
1172                    atomic_read(&nvdev->open_chn) == nvdev->num_chn);
1173
1174         /* ignore failures from setting rss parameters, still have channels */
1175         if (dev_info)
1176                 rndis_filter_set_rss_param(rdev, dev_info->rss_key);
1177         else
1178                 rndis_filter_set_rss_param(rdev, netvsc_hash_key);
1179
1180         netif_set_real_num_tx_queues(ndev, nvdev->num_chn);
1181         netif_set_real_num_rx_queues(ndev, nvdev->num_chn);
1182
1183         for (i = 0; i < VRSS_SEND_TAB_SIZE; i++)
1184                 ndev_ctx->tx_table[i] = i % nvdev->num_chn;
1185
1186         return 0;
1187 }
1188
1189 static int rndis_netdev_set_hwcaps(struct rndis_device *rndis_device,
1190                                    struct netvsc_device *nvdev)
1191 {
1192         struct net_device *net = rndis_device->ndev;
1193         struct net_device_context *net_device_ctx = netdev_priv(net);
1194         struct ndis_offload hwcaps;
1195         struct ndis_offload_params offloads;
1196         unsigned int gso_max_size = GSO_MAX_SIZE;
1197         int ret;
1198
1199         /* Find HW offload capabilities */
1200         ret = rndis_query_hwcaps(rndis_device, nvdev, &hwcaps);
1201         if (ret != 0)
1202                 return ret;
1203
1204         /* A value of zero means "no change"; now turn on what we want. */
1205         memset(&offloads, 0, sizeof(struct ndis_offload_params));
1206
1207         /* Linux does not care about IP checksum, always does in kernel */
1208         offloads.ip_v4_csum = NDIS_OFFLOAD_PARAMETERS_TX_RX_DISABLED;
1209
1210         /* Reset previously set hw_features flags */
1211         net->hw_features &= ~NETVSC_SUPPORTED_HW_FEATURES;
1212         net_device_ctx->tx_checksum_mask = 0;
1213
1214         /* Compute tx offload settings based on hw capabilities */
1215         net->hw_features |= NETIF_F_RXCSUM;
1216         net->hw_features |= NETIF_F_SG;
1217         net->hw_features |= NETIF_F_RXHASH;
1218
1219         if ((hwcaps.csum.ip4_txcsum & NDIS_TXCSUM_ALL_TCP4) == NDIS_TXCSUM_ALL_TCP4) {
1220                 /* Can checksum TCP */
1221                 net->hw_features |= NETIF_F_IP_CSUM;
1222                 net_device_ctx->tx_checksum_mask |= TRANSPORT_INFO_IPV4_TCP;
1223
1224                 offloads.tcp_ip_v4_csum = NDIS_OFFLOAD_PARAMETERS_TX_RX_ENABLED;
1225
1226                 if (hwcaps.lsov2.ip4_encap & NDIS_OFFLOAD_ENCAP_8023) {
1227                         offloads.lso_v2_ipv4 = NDIS_OFFLOAD_PARAMETERS_LSOV2_ENABLED;
1228                         net->hw_features |= NETIF_F_TSO;
1229
1230                         if (hwcaps.lsov2.ip4_maxsz < gso_max_size)
1231                                 gso_max_size = hwcaps.lsov2.ip4_maxsz;
1232                 }
1233
1234                 if (hwcaps.csum.ip4_txcsum & NDIS_TXCSUM_CAP_UDP4) {
1235                         offloads.udp_ip_v4_csum = NDIS_OFFLOAD_PARAMETERS_TX_RX_ENABLED;
1236                         net_device_ctx->tx_checksum_mask |= TRANSPORT_INFO_IPV4_UDP;
1237                 }
1238         }
1239
1240         if ((hwcaps.csum.ip6_txcsum & NDIS_TXCSUM_ALL_TCP6) == NDIS_TXCSUM_ALL_TCP6) {
1241                 net->hw_features |= NETIF_F_IPV6_CSUM;
1242
1243                 offloads.tcp_ip_v6_csum = NDIS_OFFLOAD_PARAMETERS_TX_RX_ENABLED;
1244                 net_device_ctx->tx_checksum_mask |= TRANSPORT_INFO_IPV6_TCP;
1245
1246                 if ((hwcaps.lsov2.ip6_encap & NDIS_OFFLOAD_ENCAP_8023) &&
1247                     (hwcaps.lsov2.ip6_opts & NDIS_LSOV2_CAP_IP6) == NDIS_LSOV2_CAP_IP6) {
1248                         offloads.lso_v2_ipv6 = NDIS_OFFLOAD_PARAMETERS_LSOV2_ENABLED;
1249                         net->hw_features |= NETIF_F_TSO6;
1250
1251                         if (hwcaps.lsov2.ip6_maxsz < gso_max_size)
1252                                 gso_max_size = hwcaps.lsov2.ip6_maxsz;
1253                 }
1254
1255                 if (hwcaps.csum.ip6_txcsum & NDIS_TXCSUM_CAP_UDP6) {
1256                         offloads.udp_ip_v6_csum = NDIS_OFFLOAD_PARAMETERS_TX_RX_ENABLED;
1257                         net_device_ctx->tx_checksum_mask |= TRANSPORT_INFO_IPV6_UDP;
1258                 }
1259         }
1260
1261         if (hwcaps.rsc.ip4 && hwcaps.rsc.ip6) {
1262                 net->hw_features |= NETIF_F_LRO;
1263
1264                 if (net->features & NETIF_F_LRO) {
1265                         offloads.rsc_ip_v4 = NDIS_OFFLOAD_PARAMETERS_RSC_ENABLED;
1266                         offloads.rsc_ip_v6 = NDIS_OFFLOAD_PARAMETERS_RSC_ENABLED;
1267                 } else {
1268                         offloads.rsc_ip_v4 = NDIS_OFFLOAD_PARAMETERS_RSC_DISABLED;
1269                         offloads.rsc_ip_v6 = NDIS_OFFLOAD_PARAMETERS_RSC_DISABLED;
1270                 }
1271         }
1272
1273         /* In case some hw_features disappeared we need to remove them from
1274          * net->features list as they're no longer supported.
1275          */
1276         net->features &= ~NETVSC_SUPPORTED_HW_FEATURES | net->hw_features;
1277
1278         netif_set_gso_max_size(net, gso_max_size);
1279
1280         ret = rndis_filter_set_offload_params(net, nvdev, &offloads);
1281
1282         return ret;
1283 }
1284
1285 static void rndis_get_friendly_name(struct net_device *net,
1286                                     struct rndis_device *rndis_device,
1287                                     struct netvsc_device *net_device)
1288 {
1289         ucs2_char_t wname[256];
1290         unsigned long len;
1291         u8 ifalias[256];
1292         u32 size;
1293
1294         size = sizeof(wname);
1295         if (rndis_filter_query_device(rndis_device, net_device,
1296                                       RNDIS_OID_GEN_FRIENDLY_NAME,
1297                                       wname, &size) != 0)
1298                 return; /* ignore if host does not support */
1299
1300         if (size == 0)
1301                 return; /* name not set */
1302
1303         /* Convert Windows Unicode string to UTF-8 */
1304         len = ucs2_as_utf8(ifalias, wname, sizeof(ifalias));
1305
1306         /* ignore the default value from host */
1307         if (strcmp(ifalias, "Network Adapter") != 0)
1308                 dev_set_alias(net, ifalias, len);
1309 }
1310
1311 struct netvsc_device *rndis_filter_device_add(struct hv_device *dev,
1312                                       struct netvsc_device_info *device_info)
1313 {
1314         struct net_device *net = hv_get_drvdata(dev);
1315         struct netvsc_device *net_device;
1316         struct rndis_device *rndis_device;
1317         struct ndis_recv_scale_cap rsscap;
1318         u32 rsscap_size = sizeof(struct ndis_recv_scale_cap);
1319         u32 mtu, size;
1320         u32 num_possible_rss_qs;
1321         int i, ret;
1322
1323         rndis_device = get_rndis_device();
1324         if (!rndis_device)
1325                 return ERR_PTR(-ENODEV);
1326
1327         /* Let the inner driver handle this first to create the netvsc channel
1328          * NOTE! Once the channel is created, we may get a receive callback
1329          * (RndisFilterOnReceive()) before this call is completed
1330          */
1331         net_device = netvsc_device_add(dev, device_info);
1332         if (IS_ERR(net_device)) {
1333                 kfree(rndis_device);
1334                 return net_device;
1335         }
1336
1337         /* Initialize the rndis device */
1338         net_device->max_chn = 1;
1339         net_device->num_chn = 1;
1340
1341         net_device->extension = rndis_device;
1342         rndis_device->ndev = net;
1343
1344         /* Send the rndis initialization message */
1345         ret = rndis_filter_init_device(rndis_device, net_device);
1346         if (ret != 0)
1347                 goto err_dev_remv;
1348
1349         /* Get the MTU from the host */
1350         size = sizeof(u32);
1351         ret = rndis_filter_query_device(rndis_device, net_device,
1352                                         RNDIS_OID_GEN_MAXIMUM_FRAME_SIZE,
1353                                         &mtu, &size);
1354         if (ret == 0 && size == sizeof(u32) && mtu < net->mtu)
1355                 net->mtu = mtu;
1356
1357         /* Get the mac address */
1358         ret = rndis_filter_query_device_mac(rndis_device, net_device);
1359         if (ret != 0)
1360                 goto err_dev_remv;
1361
1362         memcpy(device_info->mac_adr, rndis_device->hw_mac_adr, ETH_ALEN);
1363
1364         /* Get friendly name as ifalias*/
1365         if (!net->ifalias)
1366                 rndis_get_friendly_name(net, rndis_device, net_device);
1367
1368         /* Query and set hardware capabilities */
1369         ret = rndis_netdev_set_hwcaps(rndis_device, net_device);
1370         if (ret != 0)
1371                 goto err_dev_remv;
1372
1373         rndis_filter_query_device_link_status(rndis_device, net_device);
1374
1375         netdev_dbg(net, "Device MAC %pM link state %s\n",
1376                    rndis_device->hw_mac_adr,
1377                    rndis_device->link_state ? "down" : "up");
1378
1379         if (net_device->nvsp_version < NVSP_PROTOCOL_VERSION_5)
1380                 goto out;
1381
1382         rndis_filter_query_link_speed(rndis_device, net_device);
1383
1384         /* vRSS setup */
1385         memset(&rsscap, 0, rsscap_size);
1386         ret = rndis_filter_query_device(rndis_device, net_device,
1387                                         OID_GEN_RECEIVE_SCALE_CAPABILITIES,
1388                                         &rsscap, &rsscap_size);
1389         if (ret || rsscap.num_recv_que < 2)
1390                 goto out;
1391
1392         /* This guarantees that num_possible_rss_qs <= num_online_cpus */
1393         num_possible_rss_qs = min_t(u32, num_online_cpus(),
1394                                     rsscap.num_recv_que);
1395
1396         net_device->max_chn = min_t(u32, VRSS_CHANNEL_MAX, num_possible_rss_qs);
1397
1398         /* We will use the given number of channels if available. */
1399         net_device->num_chn = min(net_device->max_chn, device_info->num_chn);
1400
1401         for (i = 0; i < ITAB_NUM; i++)
1402                 rndis_device->rx_table[i] = ethtool_rxfh_indir_default(
1403                                                 i, net_device->num_chn);
1404
1405         atomic_set(&net_device->open_chn, 1);
1406         vmbus_set_sc_create_callback(dev->channel, netvsc_sc_open);
1407
1408         for (i = 1; i < net_device->num_chn; i++) {
1409                 ret = netvsc_alloc_recv_comp_ring(net_device, i);
1410                 if (ret) {
1411                         while (--i != 0)
1412                                 vfree(net_device->chan_table[i].mrc.slots);
1413                         goto out;
1414                 }
1415         }
1416
1417         for (i = 1; i < net_device->num_chn; i++)
1418                 netif_napi_add(net, &net_device->chan_table[i].napi,
1419                                netvsc_poll, NAPI_POLL_WEIGHT);
1420
1421         return net_device;
1422
1423 out:
1424         /* setting up multiple channels failed */
1425         net_device->max_chn = 1;
1426         net_device->num_chn = 1;
1427         return net_device;
1428
1429 err_dev_remv:
1430         rndis_filter_device_remove(dev, net_device);
1431         return ERR_PTR(ret);
1432 }
1433
1434 void rndis_filter_device_remove(struct hv_device *dev,
1435                                 struct netvsc_device *net_dev)
1436 {
1437         struct rndis_device *rndis_dev = net_dev->extension;
1438
1439         /* Halt and release the rndis device */
1440         rndis_filter_halt_device(net_dev, rndis_dev);
1441
1442         net_dev->extension = NULL;
1443
1444         netvsc_device_remove(dev);
1445 }
1446
1447 int rndis_filter_open(struct netvsc_device *nvdev)
1448 {
1449         if (!nvdev)
1450                 return -EINVAL;
1451
1452         return rndis_filter_open_device(nvdev->extension);
1453 }
1454
1455 int rndis_filter_close(struct netvsc_device *nvdev)
1456 {
1457         if (!nvdev)
1458                 return -EINVAL;
1459
1460         return rndis_filter_close_device(nvdev->extension);
1461 }
This page took 0.122816 seconds and 4 git commands to generate.