12ad471ac5e1fee70c044725f43092c3745a8f16
[linux-2.6-microblaze.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 <haiyangz@microsoft.com>
7  *   Hank Janssen  <hjanssen@microsoft.com>
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, false);
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         /* Ensure the packet is big enough to read req_id. Req_id is the 1st
279          * field in any request/response message, so the payload should have at
280          * least sizeof(u32) bytes
281          */
282         if (resp->msg_len - RNDIS_HEADER_SIZE < sizeof(u32)) {
283                 netdev_err(ndev, "rndis msg_len too small: %u\n",
284                            resp->msg_len);
285                 return;
286         }
287
288         spin_lock_irqsave(&dev->request_lock, flags);
289         list_for_each_entry(request, &dev->req_list, list_ent) {
290                 /*
291                  * All request/response message contains RequestId as the 1st
292                  * field
293                  */
294                 if (request->request_msg.msg.init_req.req_id
295                     == resp->msg.init_complete.req_id) {
296                         found = true;
297                         break;
298                 }
299         }
300         spin_unlock_irqrestore(&dev->request_lock, flags);
301
302         if (found) {
303                 if (resp->msg_len <=
304                     sizeof(struct rndis_message) + RNDIS_EXT_LEN) {
305                         memcpy(&request->response_msg, resp,
306                                resp->msg_len);
307                         if (request->request_msg.ndis_msg_type ==
308                             RNDIS_MSG_QUERY && request->request_msg.msg.
309                             query_req.oid == RNDIS_OID_GEN_MEDIA_CONNECT_STATUS)
310                                 rndis_set_link_state(dev, request);
311                 } else {
312                         netdev_err(ndev,
313                                 "rndis response buffer overflow "
314                                 "detected (size %u max %zu)\n",
315                                 resp->msg_len,
316                                 sizeof(struct rndis_message));
317
318                         if (resp->ndis_msg_type ==
319                             RNDIS_MSG_RESET_C) {
320                                 /* does not have a request id field */
321                                 request->response_msg.msg.reset_complete.
322                                         status = RNDIS_STATUS_BUFFER_OVERFLOW;
323                         } else {
324                                 request->response_msg.msg.
325                                 init_complete.status =
326                                         RNDIS_STATUS_BUFFER_OVERFLOW;
327                         }
328                 }
329
330                 complete(&request->wait_event);
331         } else {
332                 netdev_err(ndev,
333                         "no rndis request found for this response "
334                         "(id 0x%x res type 0x%x)\n",
335                         resp->msg.init_complete.req_id,
336                         resp->ndis_msg_type);
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 net_device *ndev,
345                                   struct rndis_packet *rpkt,
346                                   u32 rpkt_len, u32 type, u8 internal)
347 {
348         struct rndis_per_packet_info *ppi;
349         int len;
350
351         if (rpkt->per_pkt_info_offset == 0)
352                 return NULL;
353
354         /* Validate info_offset and info_len */
355         if (rpkt->per_pkt_info_offset < sizeof(struct rndis_packet) ||
356             rpkt->per_pkt_info_offset > rpkt_len) {
357                 netdev_err(ndev, "Invalid per_pkt_info_offset: %u\n",
358                            rpkt->per_pkt_info_offset);
359                 return NULL;
360         }
361
362         if (rpkt->per_pkt_info_len > rpkt_len - rpkt->per_pkt_info_offset) {
363                 netdev_err(ndev, "Invalid per_pkt_info_len: %u\n",
364                            rpkt->per_pkt_info_len);
365                 return NULL;
366         }
367
368         ppi = (struct rndis_per_packet_info *)((ulong)rpkt +
369                 rpkt->per_pkt_info_offset);
370         len = rpkt->per_pkt_info_len;
371
372         while (len > 0) {
373                 /* Validate ppi_offset and ppi_size */
374                 if (ppi->size > len) {
375                         netdev_err(ndev, "Invalid ppi size: %u\n", ppi->size);
376                         continue;
377                 }
378
379                 if (ppi->ppi_offset >= ppi->size) {
380                         netdev_err(ndev, "Invalid ppi_offset: %u\n", ppi->ppi_offset);
381                         continue;
382                 }
383
384                 if (ppi->type == type && ppi->internal == internal)
385                         return (void *)((ulong)ppi + ppi->ppi_offset);
386                 len -= ppi->size;
387                 ppi = (struct rndis_per_packet_info *)((ulong)ppi + ppi->size);
388         }
389
390         return NULL;
391 }
392
393 static inline
394 void rsc_add_data(struct netvsc_channel *nvchan,
395                   const struct ndis_pkt_8021q_info *vlan,
396                   const struct ndis_tcp_ip_checksum_info *csum_info,
397                   const u32 *hash_info,
398                   void *data, u32 len)
399 {
400         u32 cnt = nvchan->rsc.cnt;
401
402         if (cnt) {
403                 nvchan->rsc.pktlen += len;
404         } else {
405                 nvchan->rsc.vlan = vlan;
406                 nvchan->rsc.csum_info = csum_info;
407                 nvchan->rsc.pktlen = len;
408                 nvchan->rsc.hash_info = hash_info;
409         }
410
411         nvchan->rsc.data[cnt] = data;
412         nvchan->rsc.len[cnt] = len;
413         nvchan->rsc.cnt++;
414 }
415
416 static int rndis_filter_receive_data(struct net_device *ndev,
417                                      struct netvsc_device *nvdev,
418                                      struct netvsc_channel *nvchan,
419                                      struct rndis_message *msg,
420                                      u32 data_buflen)
421 {
422         struct rndis_packet *rndis_pkt = &msg->msg.pkt;
423         const struct ndis_tcp_ip_checksum_info *csum_info;
424         const struct ndis_pkt_8021q_info *vlan;
425         const struct rndis_pktinfo_id *pktinfo_id;
426         const u32 *hash_info;
427         u32 data_offset, rpkt_len;
428         void *data;
429         bool rsc_more = false;
430         int ret;
431
432         /* Ensure data_buflen is big enough to read header fields */
433         if (data_buflen < RNDIS_HEADER_SIZE + sizeof(struct rndis_packet)) {
434                 netdev_err(ndev, "invalid rndis pkt, data_buflen too small: %u\n",
435                            data_buflen);
436                 return NVSP_STAT_FAIL;
437         }
438
439         /* Validate rndis_pkt offset */
440         if (rndis_pkt->data_offset >= data_buflen - RNDIS_HEADER_SIZE) {
441                 netdev_err(ndev, "invalid rndis packet offset: %u\n",
442                            rndis_pkt->data_offset);
443                 return NVSP_STAT_FAIL;
444         }
445
446         /* Remove the rndis header and pass it back up the stack */
447         data_offset = RNDIS_HEADER_SIZE + rndis_pkt->data_offset;
448
449         rpkt_len = data_buflen - RNDIS_HEADER_SIZE;
450         data_buflen -= data_offset;
451
452         /*
453          * Make sure we got a valid RNDIS message, now total_data_buflen
454          * should be the data packet size plus the trailer padding size
455          */
456         if (unlikely(data_buflen < rndis_pkt->data_len)) {
457                 netdev_err(ndev, "rndis message buffer "
458                            "overflow detected (got %u, min %u)"
459                            "...dropping this message!\n",
460                            data_buflen, rndis_pkt->data_len);
461                 return NVSP_STAT_FAIL;
462         }
463
464         vlan = rndis_get_ppi(ndev, rndis_pkt, rpkt_len, IEEE_8021Q_INFO, 0);
465
466         csum_info = rndis_get_ppi(ndev, rndis_pkt, rpkt_len, TCPIP_CHKSUM_PKTINFO, 0);
467
468         hash_info = rndis_get_ppi(ndev, rndis_pkt, rpkt_len, NBL_HASH_VALUE, 0);
469
470         pktinfo_id = rndis_get_ppi(ndev, rndis_pkt, rpkt_len, RNDIS_PKTINFO_ID, 1);
471
472         data = (void *)msg + data_offset;
473
474         /* Identify RSC frags, drop erroneous packets */
475         if (pktinfo_id && (pktinfo_id->flag & RNDIS_PKTINFO_SUBALLOC)) {
476                 if (pktinfo_id->flag & RNDIS_PKTINFO_1ST_FRAG)
477                         nvchan->rsc.cnt = 0;
478                 else if (nvchan->rsc.cnt == 0)
479                         goto drop;
480
481                 rsc_more = true;
482
483                 if (pktinfo_id->flag & RNDIS_PKTINFO_LAST_FRAG)
484                         rsc_more = false;
485
486                 if (rsc_more && nvchan->rsc.is_last)
487                         goto drop;
488         } else {
489                 nvchan->rsc.cnt = 0;
490         }
491
492         if (unlikely(nvchan->rsc.cnt >= NVSP_RSC_MAX))
493                 goto drop;
494
495         /* Put data into per channel structure.
496          * Also, remove the rndis trailer padding from rndis packet message
497          * rndis_pkt->data_len tell us the real data length, we only copy
498          * the data packet to the stack, without the rndis trailer padding
499          */
500         rsc_add_data(nvchan, vlan, csum_info, hash_info,
501                      data, rndis_pkt->data_len);
502
503         if (rsc_more)
504                 return NVSP_STAT_SUCCESS;
505
506         ret = netvsc_recv_callback(ndev, nvdev, nvchan);
507         nvchan->rsc.cnt = 0;
508
509         return ret;
510
511 drop:
512         /* Drop incomplete packet */
513         nvchan->rsc.cnt = 0;
514         return NVSP_STAT_FAIL;
515 }
516
517 int rndis_filter_receive(struct net_device *ndev,
518                          struct netvsc_device *net_dev,
519                          struct netvsc_channel *nvchan,
520                          void *data, u32 buflen)
521 {
522         struct net_device_context *net_device_ctx = netdev_priv(ndev);
523         struct rndis_message *rndis_msg = data;
524
525         if (netif_msg_rx_status(net_device_ctx))
526                 dump_rndis_message(ndev, rndis_msg);
527
528         /* Validate incoming rndis_message packet */
529         if (buflen < RNDIS_HEADER_SIZE || rndis_msg->msg_len < RNDIS_HEADER_SIZE ||
530             buflen < rndis_msg->msg_len) {
531                 netdev_err(ndev, "Invalid rndis_msg (buflen: %u, msg_len: %u)\n",
532                            buflen, rndis_msg->msg_len);
533                 return NVSP_STAT_FAIL;
534         }
535
536         switch (rndis_msg->ndis_msg_type) {
537         case RNDIS_MSG_PACKET:
538                 return rndis_filter_receive_data(ndev, net_dev, nvchan,
539                                                  rndis_msg, buflen);
540         case RNDIS_MSG_INIT_C:
541         case RNDIS_MSG_QUERY_C:
542         case RNDIS_MSG_SET_C:
543                 /* completion msgs */
544                 rndis_filter_receive_response(ndev, net_dev, rndis_msg);
545                 break;
546
547         case RNDIS_MSG_INDICATE:
548                 /* notification msgs */
549                 netvsc_linkstatus_callback(ndev, rndis_msg);
550                 break;
551         default:
552                 netdev_err(ndev,
553                         "unhandled rndis message (type %u len %u)\n",
554                            rndis_msg->ndis_msg_type,
555                            rndis_msg->msg_len);
556                 return NVSP_STAT_FAIL;
557         }
558
559         return NVSP_STAT_SUCCESS;
560 }
561
562 static int rndis_filter_query_device(struct rndis_device *dev,
563                                      struct netvsc_device *nvdev,
564                                      u32 oid, void *result, u32 *result_size)
565 {
566         struct rndis_request *request;
567         u32 inresult_size = *result_size;
568         struct rndis_query_request *query;
569         struct rndis_query_complete *query_complete;
570         int ret = 0;
571
572         if (!result)
573                 return -EINVAL;
574
575         *result_size = 0;
576         request = get_rndis_request(dev, RNDIS_MSG_QUERY,
577                         RNDIS_MESSAGE_SIZE(struct rndis_query_request));
578         if (!request) {
579                 ret = -ENOMEM;
580                 goto cleanup;
581         }
582
583         /* Setup the rndis query */
584         query = &request->request_msg.msg.query_req;
585         query->oid = oid;
586         query->info_buf_offset = sizeof(struct rndis_query_request);
587         query->info_buflen = 0;
588         query->dev_vc_handle = 0;
589
590         if (oid == OID_TCP_OFFLOAD_HARDWARE_CAPABILITIES) {
591                 struct ndis_offload *hwcaps;
592                 u32 nvsp_version = nvdev->nvsp_version;
593                 u8 ndis_rev;
594                 size_t size;
595
596                 if (nvsp_version >= NVSP_PROTOCOL_VERSION_5) {
597                         ndis_rev = NDIS_OFFLOAD_PARAMETERS_REVISION_3;
598                         size = NDIS_OFFLOAD_SIZE;
599                 } else if (nvsp_version >= NVSP_PROTOCOL_VERSION_4) {
600                         ndis_rev = NDIS_OFFLOAD_PARAMETERS_REVISION_2;
601                         size = NDIS_OFFLOAD_SIZE_6_1;
602                 } else {
603                         ndis_rev = NDIS_OFFLOAD_PARAMETERS_REVISION_1;
604                         size = NDIS_OFFLOAD_SIZE_6_0;
605                 }
606
607                 request->request_msg.msg_len += size;
608                 query->info_buflen = size;
609                 hwcaps = (struct ndis_offload *)
610                         ((unsigned long)query + query->info_buf_offset);
611
612                 hwcaps->header.type = NDIS_OBJECT_TYPE_OFFLOAD;
613                 hwcaps->header.revision = ndis_rev;
614                 hwcaps->header.size = size;
615
616         } else if (oid == OID_GEN_RECEIVE_SCALE_CAPABILITIES) {
617                 struct ndis_recv_scale_cap *cap;
618
619                 request->request_msg.msg_len +=
620                         sizeof(struct ndis_recv_scale_cap);
621                 query->info_buflen = sizeof(struct ndis_recv_scale_cap);
622                 cap = (struct ndis_recv_scale_cap *)((unsigned long)query +
623                                                      query->info_buf_offset);
624                 cap->hdr.type = NDIS_OBJECT_TYPE_RSS_CAPABILITIES;
625                 cap->hdr.rev = NDIS_RECEIVE_SCALE_CAPABILITIES_REVISION_2;
626                 cap->hdr.size = sizeof(struct ndis_recv_scale_cap);
627         }
628
629         ret = rndis_filter_send_request(dev, request);
630         if (ret != 0)
631                 goto cleanup;
632
633         wait_for_completion(&request->wait_event);
634
635         /* Copy the response back */
636         query_complete = &request->response_msg.msg.query_complete;
637
638         if (query_complete->info_buflen > inresult_size) {
639                 ret = -1;
640                 goto cleanup;
641         }
642
643         memcpy(result,
644                (void *)((unsigned long)query_complete +
645                          query_complete->info_buf_offset),
646                query_complete->info_buflen);
647
648         *result_size = query_complete->info_buflen;
649
650 cleanup:
651         if (request)
652                 put_rndis_request(dev, request);
653
654         return ret;
655 }
656
657 /* Get the hardware offload capabilities */
658 static int
659 rndis_query_hwcaps(struct rndis_device *dev, struct netvsc_device *net_device,
660                    struct ndis_offload *caps)
661 {
662         u32 caps_len = sizeof(*caps);
663         int ret;
664
665         memset(caps, 0, sizeof(*caps));
666
667         ret = rndis_filter_query_device(dev, net_device,
668                                         OID_TCP_OFFLOAD_HARDWARE_CAPABILITIES,
669                                         caps, &caps_len);
670         if (ret)
671                 return ret;
672
673         if (caps->header.type != NDIS_OBJECT_TYPE_OFFLOAD) {
674                 netdev_warn(dev->ndev, "invalid NDIS objtype %#x\n",
675                             caps->header.type);
676                 return -EINVAL;
677         }
678
679         if (caps->header.revision < NDIS_OFFLOAD_PARAMETERS_REVISION_1) {
680                 netdev_warn(dev->ndev, "invalid NDIS objrev %x\n",
681                             caps->header.revision);
682                 return -EINVAL;
683         }
684
685         if (caps->header.size > caps_len ||
686             caps->header.size < NDIS_OFFLOAD_SIZE_6_0) {
687                 netdev_warn(dev->ndev,
688                             "invalid NDIS objsize %u, data size %u\n",
689                             caps->header.size, caps_len);
690                 return -EINVAL;
691         }
692
693         return 0;
694 }
695
696 static int rndis_filter_query_device_mac(struct rndis_device *dev,
697                                          struct netvsc_device *net_device)
698 {
699         u32 size = ETH_ALEN;
700
701         return rndis_filter_query_device(dev, net_device,
702                                       RNDIS_OID_802_3_PERMANENT_ADDRESS,
703                                       dev->hw_mac_adr, &size);
704 }
705
706 #define NWADR_STR "NetworkAddress"
707 #define NWADR_STRLEN 14
708
709 int rndis_filter_set_device_mac(struct netvsc_device *nvdev,
710                                 const char *mac)
711 {
712         struct rndis_device *rdev = nvdev->extension;
713         struct rndis_request *request;
714         struct rndis_set_request *set;
715         struct rndis_config_parameter_info *cpi;
716         wchar_t *cfg_nwadr, *cfg_mac;
717         struct rndis_set_complete *set_complete;
718         char macstr[2*ETH_ALEN+1];
719         u32 extlen = sizeof(struct rndis_config_parameter_info) +
720                 2*NWADR_STRLEN + 4*ETH_ALEN;
721         int ret;
722
723         request = get_rndis_request(rdev, RNDIS_MSG_SET,
724                 RNDIS_MESSAGE_SIZE(struct rndis_set_request) + extlen);
725         if (!request)
726                 return -ENOMEM;
727
728         set = &request->request_msg.msg.set_req;
729         set->oid = RNDIS_OID_GEN_RNDIS_CONFIG_PARAMETER;
730         set->info_buflen = extlen;
731         set->info_buf_offset = sizeof(struct rndis_set_request);
732         set->dev_vc_handle = 0;
733
734         cpi = (struct rndis_config_parameter_info *)((ulong)set +
735                 set->info_buf_offset);
736         cpi->parameter_name_offset =
737                 sizeof(struct rndis_config_parameter_info);
738         /* Multiply by 2 because host needs 2 bytes (utf16) for each char */
739         cpi->parameter_name_length = 2*NWADR_STRLEN;
740         cpi->parameter_type = RNDIS_CONFIG_PARAM_TYPE_STRING;
741         cpi->parameter_value_offset =
742                 cpi->parameter_name_offset + cpi->parameter_name_length;
743         /* Multiply by 4 because each MAC byte displayed as 2 utf16 chars */
744         cpi->parameter_value_length = 4*ETH_ALEN;
745
746         cfg_nwadr = (wchar_t *)((ulong)cpi + cpi->parameter_name_offset);
747         cfg_mac = (wchar_t *)((ulong)cpi + cpi->parameter_value_offset);
748         ret = utf8s_to_utf16s(NWADR_STR, NWADR_STRLEN, UTF16_HOST_ENDIAN,
749                               cfg_nwadr, NWADR_STRLEN);
750         if (ret < 0)
751                 goto cleanup;
752         snprintf(macstr, 2*ETH_ALEN+1, "%pm", mac);
753         ret = utf8s_to_utf16s(macstr, 2*ETH_ALEN, UTF16_HOST_ENDIAN,
754                               cfg_mac, 2*ETH_ALEN);
755         if (ret < 0)
756                 goto cleanup;
757
758         ret = rndis_filter_send_request(rdev, request);
759         if (ret != 0)
760                 goto cleanup;
761
762         wait_for_completion(&request->wait_event);
763
764         set_complete = &request->response_msg.msg.set_complete;
765         if (set_complete->status != RNDIS_STATUS_SUCCESS)
766                 ret = -EIO;
767
768 cleanup:
769         put_rndis_request(rdev, request);
770         return ret;
771 }
772
773 int
774 rndis_filter_set_offload_params(struct net_device *ndev,
775                                 struct netvsc_device *nvdev,
776                                 struct ndis_offload_params *req_offloads)
777 {
778         struct rndis_device *rdev = nvdev->extension;
779         struct rndis_request *request;
780         struct rndis_set_request *set;
781         struct ndis_offload_params *offload_params;
782         struct rndis_set_complete *set_complete;
783         u32 extlen = sizeof(struct ndis_offload_params);
784         int ret;
785         u32 vsp_version = nvdev->nvsp_version;
786
787         if (vsp_version <= NVSP_PROTOCOL_VERSION_4) {
788                 extlen = VERSION_4_OFFLOAD_SIZE;
789                 /* On NVSP_PROTOCOL_VERSION_4 and below, we do not support
790                  * UDP checksum offload.
791                  */
792                 req_offloads->udp_ip_v4_csum = 0;
793                 req_offloads->udp_ip_v6_csum = 0;
794         }
795
796         request = get_rndis_request(rdev, RNDIS_MSG_SET,
797                 RNDIS_MESSAGE_SIZE(struct rndis_set_request) + extlen);
798         if (!request)
799                 return -ENOMEM;
800
801         set = &request->request_msg.msg.set_req;
802         set->oid = OID_TCP_OFFLOAD_PARAMETERS;
803         set->info_buflen = extlen;
804         set->info_buf_offset = sizeof(struct rndis_set_request);
805         set->dev_vc_handle = 0;
806
807         offload_params = (struct ndis_offload_params *)((ulong)set +
808                                 set->info_buf_offset);
809         *offload_params = *req_offloads;
810         offload_params->header.type = NDIS_OBJECT_TYPE_DEFAULT;
811         offload_params->header.revision = NDIS_OFFLOAD_PARAMETERS_REVISION_3;
812         offload_params->header.size = extlen;
813
814         ret = rndis_filter_send_request(rdev, request);
815         if (ret != 0)
816                 goto cleanup;
817
818         wait_for_completion(&request->wait_event);
819         set_complete = &request->response_msg.msg.set_complete;
820         if (set_complete->status != RNDIS_STATUS_SUCCESS) {
821                 netdev_err(ndev, "Fail to set offload on host side:0x%x\n",
822                            set_complete->status);
823                 ret = -EINVAL;
824         }
825
826 cleanup:
827         put_rndis_request(rdev, request);
828         return ret;
829 }
830
831 static int rndis_set_rss_param_msg(struct rndis_device *rdev,
832                                    const u8 *rss_key, u16 flag)
833 {
834         struct net_device *ndev = rdev->ndev;
835         struct net_device_context *ndc = netdev_priv(ndev);
836         struct rndis_request *request;
837         struct rndis_set_request *set;
838         struct rndis_set_complete *set_complete;
839         u32 extlen = sizeof(struct ndis_recv_scale_param) +
840                      4 * ITAB_NUM + NETVSC_HASH_KEYLEN;
841         struct ndis_recv_scale_param *rssp;
842         u32 *itab;
843         u8 *keyp;
844         int i, ret;
845
846         request = get_rndis_request(
847                         rdev, RNDIS_MSG_SET,
848                         RNDIS_MESSAGE_SIZE(struct rndis_set_request) + extlen);
849         if (!request)
850                 return -ENOMEM;
851
852         set = &request->request_msg.msg.set_req;
853         set->oid = OID_GEN_RECEIVE_SCALE_PARAMETERS;
854         set->info_buflen = extlen;
855         set->info_buf_offset = sizeof(struct rndis_set_request);
856         set->dev_vc_handle = 0;
857
858         rssp = (struct ndis_recv_scale_param *)(set + 1);
859         rssp->hdr.type = NDIS_OBJECT_TYPE_RSS_PARAMETERS;
860         rssp->hdr.rev = NDIS_RECEIVE_SCALE_PARAMETERS_REVISION_2;
861         rssp->hdr.size = sizeof(struct ndis_recv_scale_param);
862         rssp->flag = flag;
863         rssp->hashinfo = NDIS_HASH_FUNC_TOEPLITZ | NDIS_HASH_IPV4 |
864                          NDIS_HASH_TCP_IPV4 | NDIS_HASH_IPV6 |
865                          NDIS_HASH_TCP_IPV6;
866         rssp->indirect_tabsize = 4*ITAB_NUM;
867         rssp->indirect_taboffset = sizeof(struct ndis_recv_scale_param);
868         rssp->hashkey_size = NETVSC_HASH_KEYLEN;
869         rssp->hashkey_offset = rssp->indirect_taboffset +
870                                rssp->indirect_tabsize;
871
872         /* Set indirection table entries */
873         itab = (u32 *)(rssp + 1);
874         for (i = 0; i < ITAB_NUM; i++)
875                 itab[i] = ndc->rx_table[i];
876
877         /* Set hask key values */
878         keyp = (u8 *)((unsigned long)rssp + rssp->hashkey_offset);
879         memcpy(keyp, rss_key, NETVSC_HASH_KEYLEN);
880
881         ret = rndis_filter_send_request(rdev, request);
882         if (ret != 0)
883                 goto cleanup;
884
885         wait_for_completion(&request->wait_event);
886         set_complete = &request->response_msg.msg.set_complete;
887         if (set_complete->status == RNDIS_STATUS_SUCCESS) {
888                 if (!(flag & NDIS_RSS_PARAM_FLAG_DISABLE_RSS) &&
889                     !(flag & NDIS_RSS_PARAM_FLAG_HASH_KEY_UNCHANGED))
890                         memcpy(rdev->rss_key, rss_key, NETVSC_HASH_KEYLEN);
891
892         } else {
893                 netdev_err(ndev, "Fail to set RSS parameters:0x%x\n",
894                            set_complete->status);
895                 ret = -EINVAL;
896         }
897
898 cleanup:
899         put_rndis_request(rdev, request);
900         return ret;
901 }
902
903 int rndis_filter_set_rss_param(struct rndis_device *rdev,
904                                const u8 *rss_key)
905 {
906         /* Disable RSS before change */
907         rndis_set_rss_param_msg(rdev, rss_key,
908                                 NDIS_RSS_PARAM_FLAG_DISABLE_RSS);
909
910         return rndis_set_rss_param_msg(rdev, rss_key, 0);
911 }
912
913 static int rndis_filter_query_device_link_status(struct rndis_device *dev,
914                                                  struct netvsc_device *net_device)
915 {
916         u32 size = sizeof(u32);
917         u32 link_status;
918
919         return rndis_filter_query_device(dev, net_device,
920                                          RNDIS_OID_GEN_MEDIA_CONNECT_STATUS,
921                                          &link_status, &size);
922 }
923
924 static int rndis_filter_query_link_speed(struct rndis_device *dev,
925                                          struct netvsc_device *net_device)
926 {
927         u32 size = sizeof(u32);
928         u32 link_speed;
929         struct net_device_context *ndc;
930         int ret;
931
932         ret = rndis_filter_query_device(dev, net_device,
933                                         RNDIS_OID_GEN_LINK_SPEED,
934                                         &link_speed, &size);
935
936         if (!ret) {
937                 ndc = netdev_priv(dev->ndev);
938
939                 /* The link speed reported from host is in 100bps unit, so
940                  * we convert it to Mbps here.
941                  */
942                 ndc->speed = link_speed / 10000;
943         }
944
945         return ret;
946 }
947
948 static int rndis_filter_set_packet_filter(struct rndis_device *dev,
949                                           u32 new_filter)
950 {
951         struct rndis_request *request;
952         struct rndis_set_request *set;
953         int ret;
954
955         if (dev->filter == new_filter)
956                 return 0;
957
958         request = get_rndis_request(dev, RNDIS_MSG_SET,
959                         RNDIS_MESSAGE_SIZE(struct rndis_set_request) +
960                         sizeof(u32));
961         if (!request)
962                 return -ENOMEM;
963
964         /* Setup the rndis set */
965         set = &request->request_msg.msg.set_req;
966         set->oid = RNDIS_OID_GEN_CURRENT_PACKET_FILTER;
967         set->info_buflen = sizeof(u32);
968         set->info_buf_offset = sizeof(struct rndis_set_request);
969
970         memcpy((void *)(unsigned long)set + sizeof(struct rndis_set_request),
971                &new_filter, sizeof(u32));
972
973         ret = rndis_filter_send_request(dev, request);
974         if (ret == 0) {
975                 wait_for_completion(&request->wait_event);
976                 dev->filter = new_filter;
977         }
978
979         put_rndis_request(dev, request);
980
981         return ret;
982 }
983
984 static void rndis_set_multicast(struct work_struct *w)
985 {
986         struct rndis_device *rdev
987                 = container_of(w, struct rndis_device, mcast_work);
988         u32 filter = NDIS_PACKET_TYPE_DIRECTED;
989         unsigned int flags = rdev->ndev->flags;
990
991         if (flags & IFF_PROMISC) {
992                 filter = NDIS_PACKET_TYPE_PROMISCUOUS;
993         } else {
994                 if (!netdev_mc_empty(rdev->ndev) || (flags & IFF_ALLMULTI))
995                         filter |= NDIS_PACKET_TYPE_ALL_MULTICAST;
996                 if (flags & IFF_BROADCAST)
997                         filter |= NDIS_PACKET_TYPE_BROADCAST;
998         }
999
1000         rndis_filter_set_packet_filter(rdev, filter);
1001 }
1002
1003 void rndis_filter_update(struct netvsc_device *nvdev)
1004 {
1005         struct rndis_device *rdev = nvdev->extension;
1006
1007         schedule_work(&rdev->mcast_work);
1008 }
1009
1010 static int rndis_filter_init_device(struct rndis_device *dev,
1011                                     struct netvsc_device *nvdev)
1012 {
1013         struct rndis_request *request;
1014         struct rndis_initialize_request *init;
1015         struct rndis_initialize_complete *init_complete;
1016         u32 status;
1017         int ret;
1018
1019         request = get_rndis_request(dev, RNDIS_MSG_INIT,
1020                         RNDIS_MESSAGE_SIZE(struct rndis_initialize_request));
1021         if (!request) {
1022                 ret = -ENOMEM;
1023                 goto cleanup;
1024         }
1025
1026         /* Setup the rndis set */
1027         init = &request->request_msg.msg.init_req;
1028         init->major_ver = RNDIS_MAJOR_VERSION;
1029         init->minor_ver = RNDIS_MINOR_VERSION;
1030         init->max_xfer_size = 0x4000;
1031
1032         dev->state = RNDIS_DEV_INITIALIZING;
1033
1034         ret = rndis_filter_send_request(dev, request);
1035         if (ret != 0) {
1036                 dev->state = RNDIS_DEV_UNINITIALIZED;
1037                 goto cleanup;
1038         }
1039
1040         wait_for_completion(&request->wait_event);
1041
1042         init_complete = &request->response_msg.msg.init_complete;
1043         status = init_complete->status;
1044         if (status == RNDIS_STATUS_SUCCESS) {
1045                 dev->state = RNDIS_DEV_INITIALIZED;
1046                 nvdev->max_pkt = init_complete->max_pkt_per_msg;
1047                 nvdev->pkt_align = 1 << init_complete->pkt_alignment_factor;
1048                 ret = 0;
1049         } else {
1050                 dev->state = RNDIS_DEV_UNINITIALIZED;
1051                 ret = -EINVAL;
1052         }
1053
1054 cleanup:
1055         if (request)
1056                 put_rndis_request(dev, request);
1057
1058         return ret;
1059 }
1060
1061 static bool netvsc_device_idle(const struct netvsc_device *nvdev)
1062 {
1063         int i;
1064
1065         for (i = 0; i < nvdev->num_chn; i++) {
1066                 const struct netvsc_channel *nvchan = &nvdev->chan_table[i];
1067
1068                 if (nvchan->mrc.first != nvchan->mrc.next)
1069                         return false;
1070
1071                 if (atomic_read(&nvchan->queue_sends) > 0)
1072                         return false;
1073         }
1074
1075         return true;
1076 }
1077
1078 static void rndis_filter_halt_device(struct netvsc_device *nvdev,
1079                                      struct rndis_device *dev)
1080 {
1081         struct rndis_request *request;
1082         struct rndis_halt_request *halt;
1083
1084         /* Attempt to do a rndis device halt */
1085         request = get_rndis_request(dev, RNDIS_MSG_HALT,
1086                                 RNDIS_MESSAGE_SIZE(struct rndis_halt_request));
1087         if (!request)
1088                 goto cleanup;
1089
1090         /* Setup the rndis set */
1091         halt = &request->request_msg.msg.halt_req;
1092         halt->req_id = atomic_inc_return(&dev->new_req_id);
1093
1094         /* Ignore return since this msg is optional. */
1095         rndis_filter_send_request(dev, request);
1096
1097         dev->state = RNDIS_DEV_UNINITIALIZED;
1098
1099 cleanup:
1100         nvdev->destroy = true;
1101
1102         /* Force flag to be ordered before waiting */
1103         wmb();
1104
1105         /* Wait for all send completions */
1106         wait_event(nvdev->wait_drain, netvsc_device_idle(nvdev));
1107
1108         if (request)
1109                 put_rndis_request(dev, request);
1110 }
1111
1112 static int rndis_filter_open_device(struct rndis_device *dev)
1113 {
1114         int ret;
1115
1116         if (dev->state != RNDIS_DEV_INITIALIZED)
1117                 return 0;
1118
1119         ret = rndis_filter_set_packet_filter(dev,
1120                                          NDIS_PACKET_TYPE_BROADCAST |
1121                                          NDIS_PACKET_TYPE_ALL_MULTICAST |
1122                                          NDIS_PACKET_TYPE_DIRECTED);
1123         if (ret == 0)
1124                 dev->state = RNDIS_DEV_DATAINITIALIZED;
1125
1126         return ret;
1127 }
1128
1129 static int rndis_filter_close_device(struct rndis_device *dev)
1130 {
1131         int ret;
1132
1133         if (dev->state != RNDIS_DEV_DATAINITIALIZED)
1134                 return 0;
1135
1136         /* Make sure rndis_set_multicast doesn't re-enable filter! */
1137         cancel_work_sync(&dev->mcast_work);
1138
1139         ret = rndis_filter_set_packet_filter(dev, 0);
1140         if (ret == -ENODEV)
1141                 ret = 0;
1142
1143         if (ret == 0)
1144                 dev->state = RNDIS_DEV_INITIALIZED;
1145
1146         return ret;
1147 }
1148
1149 static void netvsc_sc_open(struct vmbus_channel *new_sc)
1150 {
1151         struct net_device *ndev =
1152                 hv_get_drvdata(new_sc->primary_channel->device_obj);
1153         struct net_device_context *ndev_ctx = netdev_priv(ndev);
1154         struct netvsc_device *nvscdev;
1155         u16 chn_index = new_sc->offermsg.offer.sub_channel_index;
1156         struct netvsc_channel *nvchan;
1157         int ret;
1158
1159         /* This is safe because this callback only happens when
1160          * new device is being setup and waiting on the channel_init_wait.
1161          */
1162         nvscdev = rcu_dereference_raw(ndev_ctx->nvdev);
1163         if (!nvscdev || chn_index >= nvscdev->num_chn)
1164                 return;
1165
1166         nvchan = nvscdev->chan_table + chn_index;
1167
1168         /* Because the device uses NAPI, all the interrupt batching and
1169          * control is done via Net softirq, not the channel handling
1170          */
1171         set_channel_read_mode(new_sc, HV_CALL_ISR);
1172
1173         /* Set the channel before opening.*/
1174         nvchan->channel = new_sc;
1175
1176         ret = vmbus_open(new_sc, netvsc_ring_bytes,
1177                          netvsc_ring_bytes, NULL, 0,
1178                          netvsc_channel_cb, nvchan);
1179         if (ret == 0)
1180                 napi_enable(&nvchan->napi);
1181         else
1182                 netdev_notice(ndev, "sub channel open failed: %d\n", ret);
1183
1184         if (atomic_inc_return(&nvscdev->open_chn) == nvscdev->num_chn)
1185                 wake_up(&nvscdev->subchan_open);
1186 }
1187
1188 /* Open sub-channels after completing the handling of the device probe.
1189  * This breaks overlap of processing the host message for the
1190  * new primary channel with the initialization of sub-channels.
1191  */
1192 int rndis_set_subchannel(struct net_device *ndev,
1193                          struct netvsc_device *nvdev,
1194                          struct netvsc_device_info *dev_info)
1195 {
1196         struct nvsp_message *init_packet = &nvdev->channel_init_pkt;
1197         struct net_device_context *ndev_ctx = netdev_priv(ndev);
1198         struct hv_device *hv_dev = ndev_ctx->device_ctx;
1199         struct rndis_device *rdev = nvdev->extension;
1200         int i, ret;
1201
1202         ASSERT_RTNL();
1203
1204         memset(init_packet, 0, sizeof(struct nvsp_message));
1205         init_packet->hdr.msg_type = NVSP_MSG5_TYPE_SUBCHANNEL;
1206         init_packet->msg.v5_msg.subchn_req.op = NVSP_SUBCHANNEL_ALLOCATE;
1207         init_packet->msg.v5_msg.subchn_req.num_subchannels =
1208                                                 nvdev->num_chn - 1;
1209         trace_nvsp_send(ndev, init_packet);
1210
1211         ret = vmbus_sendpacket(hv_dev->channel, init_packet,
1212                                sizeof(struct nvsp_message),
1213                                (unsigned long)init_packet,
1214                                VM_PKT_DATA_INBAND,
1215                                VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1216         if (ret) {
1217                 netdev_err(ndev, "sub channel allocate send failed: %d\n", ret);
1218                 return ret;
1219         }
1220
1221         wait_for_completion(&nvdev->channel_init_wait);
1222         if (init_packet->msg.v5_msg.subchn_comp.status != NVSP_STAT_SUCCESS) {
1223                 netdev_err(ndev, "sub channel request failed\n");
1224                 return -EIO;
1225         }
1226
1227         nvdev->num_chn = 1 +
1228                 init_packet->msg.v5_msg.subchn_comp.num_subchannels;
1229
1230         /* wait for all sub channels to open */
1231         wait_event(nvdev->subchan_open,
1232                    atomic_read(&nvdev->open_chn) == nvdev->num_chn);
1233
1234         for (i = 0; i < VRSS_SEND_TAB_SIZE; i++)
1235                 ndev_ctx->tx_table[i] = i % nvdev->num_chn;
1236
1237         /* ignore failures from setting rss parameters, still have channels */
1238         if (dev_info)
1239                 rndis_filter_set_rss_param(rdev, dev_info->rss_key);
1240         else
1241                 rndis_filter_set_rss_param(rdev, netvsc_hash_key);
1242
1243         netif_set_real_num_tx_queues(ndev, nvdev->num_chn);
1244         netif_set_real_num_rx_queues(ndev, nvdev->num_chn);
1245
1246         return 0;
1247 }
1248
1249 static int rndis_netdev_set_hwcaps(struct rndis_device *rndis_device,
1250                                    struct netvsc_device *nvdev)
1251 {
1252         struct net_device *net = rndis_device->ndev;
1253         struct net_device_context *net_device_ctx = netdev_priv(net);
1254         struct ndis_offload hwcaps;
1255         struct ndis_offload_params offloads;
1256         unsigned int gso_max_size = GSO_MAX_SIZE;
1257         int ret;
1258
1259         /* Find HW offload capabilities */
1260         ret = rndis_query_hwcaps(rndis_device, nvdev, &hwcaps);
1261         if (ret != 0)
1262                 return ret;
1263
1264         /* A value of zero means "no change"; now turn on what we want. */
1265         memset(&offloads, 0, sizeof(struct ndis_offload_params));
1266
1267         /* Linux does not care about IP checksum, always does in kernel */
1268         offloads.ip_v4_csum = NDIS_OFFLOAD_PARAMETERS_TX_RX_DISABLED;
1269
1270         /* Reset previously set hw_features flags */
1271         net->hw_features &= ~NETVSC_SUPPORTED_HW_FEATURES;
1272         net_device_ctx->tx_checksum_mask = 0;
1273
1274         /* Compute tx offload settings based on hw capabilities */
1275         net->hw_features |= NETIF_F_RXCSUM;
1276         net->hw_features |= NETIF_F_SG;
1277         net->hw_features |= NETIF_F_RXHASH;
1278
1279         if ((hwcaps.csum.ip4_txcsum & NDIS_TXCSUM_ALL_TCP4) == NDIS_TXCSUM_ALL_TCP4) {
1280                 /* Can checksum TCP */
1281                 net->hw_features |= NETIF_F_IP_CSUM;
1282                 net_device_ctx->tx_checksum_mask |= TRANSPORT_INFO_IPV4_TCP;
1283
1284                 offloads.tcp_ip_v4_csum = NDIS_OFFLOAD_PARAMETERS_TX_RX_ENABLED;
1285
1286                 if (hwcaps.lsov2.ip4_encap & NDIS_OFFLOAD_ENCAP_8023) {
1287                         offloads.lso_v2_ipv4 = NDIS_OFFLOAD_PARAMETERS_LSOV2_ENABLED;
1288                         net->hw_features |= NETIF_F_TSO;
1289
1290                         if (hwcaps.lsov2.ip4_maxsz < gso_max_size)
1291                                 gso_max_size = hwcaps.lsov2.ip4_maxsz;
1292                 }
1293
1294                 if (hwcaps.csum.ip4_txcsum & NDIS_TXCSUM_CAP_UDP4) {
1295                         offloads.udp_ip_v4_csum = NDIS_OFFLOAD_PARAMETERS_TX_RX_ENABLED;
1296                         net_device_ctx->tx_checksum_mask |= TRANSPORT_INFO_IPV4_UDP;
1297                 }
1298         }
1299
1300         if ((hwcaps.csum.ip6_txcsum & NDIS_TXCSUM_ALL_TCP6) == NDIS_TXCSUM_ALL_TCP6) {
1301                 net->hw_features |= NETIF_F_IPV6_CSUM;
1302
1303                 offloads.tcp_ip_v6_csum = NDIS_OFFLOAD_PARAMETERS_TX_RX_ENABLED;
1304                 net_device_ctx->tx_checksum_mask |= TRANSPORT_INFO_IPV6_TCP;
1305
1306                 if ((hwcaps.lsov2.ip6_encap & NDIS_OFFLOAD_ENCAP_8023) &&
1307                     (hwcaps.lsov2.ip6_opts & NDIS_LSOV2_CAP_IP6) == NDIS_LSOV2_CAP_IP6) {
1308                         offloads.lso_v2_ipv6 = NDIS_OFFLOAD_PARAMETERS_LSOV2_ENABLED;
1309                         net->hw_features |= NETIF_F_TSO6;
1310
1311                         if (hwcaps.lsov2.ip6_maxsz < gso_max_size)
1312                                 gso_max_size = hwcaps.lsov2.ip6_maxsz;
1313                 }
1314
1315                 if (hwcaps.csum.ip6_txcsum & NDIS_TXCSUM_CAP_UDP6) {
1316                         offloads.udp_ip_v6_csum = NDIS_OFFLOAD_PARAMETERS_TX_RX_ENABLED;
1317                         net_device_ctx->tx_checksum_mask |= TRANSPORT_INFO_IPV6_UDP;
1318                 }
1319         }
1320
1321         if (hwcaps.rsc.ip4 && hwcaps.rsc.ip6) {
1322                 net->hw_features |= NETIF_F_LRO;
1323
1324                 if (net->features & NETIF_F_LRO) {
1325                         offloads.rsc_ip_v4 = NDIS_OFFLOAD_PARAMETERS_RSC_ENABLED;
1326                         offloads.rsc_ip_v6 = NDIS_OFFLOAD_PARAMETERS_RSC_ENABLED;
1327                 } else {
1328                         offloads.rsc_ip_v4 = NDIS_OFFLOAD_PARAMETERS_RSC_DISABLED;
1329                         offloads.rsc_ip_v6 = NDIS_OFFLOAD_PARAMETERS_RSC_DISABLED;
1330                 }
1331         }
1332
1333         /* In case some hw_features disappeared we need to remove them from
1334          * net->features list as they're no longer supported.
1335          */
1336         net->features &= ~NETVSC_SUPPORTED_HW_FEATURES | net->hw_features;
1337
1338         netif_set_gso_max_size(net, gso_max_size);
1339
1340         ret = rndis_filter_set_offload_params(net, nvdev, &offloads);
1341
1342         return ret;
1343 }
1344
1345 static void rndis_get_friendly_name(struct net_device *net,
1346                                     struct rndis_device *rndis_device,
1347                                     struct netvsc_device *net_device)
1348 {
1349         ucs2_char_t wname[256];
1350         unsigned long len;
1351         u8 ifalias[256];
1352         u32 size;
1353
1354         size = sizeof(wname);
1355         if (rndis_filter_query_device(rndis_device, net_device,
1356                                       RNDIS_OID_GEN_FRIENDLY_NAME,
1357                                       wname, &size) != 0)
1358                 return; /* ignore if host does not support */
1359
1360         if (size == 0)
1361                 return; /* name not set */
1362
1363         /* Convert Windows Unicode string to UTF-8 */
1364         len = ucs2_as_utf8(ifalias, wname, sizeof(ifalias));
1365
1366         /* ignore the default value from host */
1367         if (strcmp(ifalias, "Network Adapter") != 0)
1368                 dev_set_alias(net, ifalias, len);
1369 }
1370
1371 struct netvsc_device *rndis_filter_device_add(struct hv_device *dev,
1372                                       struct netvsc_device_info *device_info)
1373 {
1374         struct net_device *net = hv_get_drvdata(dev);
1375         struct net_device_context *ndc = netdev_priv(net);
1376         struct netvsc_device *net_device;
1377         struct rndis_device *rndis_device;
1378         struct ndis_recv_scale_cap rsscap;
1379         u32 rsscap_size = sizeof(struct ndis_recv_scale_cap);
1380         u32 mtu, size;
1381         u32 num_possible_rss_qs;
1382         int i, ret;
1383
1384         rndis_device = get_rndis_device();
1385         if (!rndis_device)
1386                 return ERR_PTR(-ENODEV);
1387
1388         /* Let the inner driver handle this first to create the netvsc channel
1389          * NOTE! Once the channel is created, we may get a receive callback
1390          * (RndisFilterOnReceive()) before this call is completed
1391          */
1392         net_device = netvsc_device_add(dev, device_info);
1393         if (IS_ERR(net_device)) {
1394                 kfree(rndis_device);
1395                 return net_device;
1396         }
1397
1398         /* Initialize the rndis device */
1399         net_device->max_chn = 1;
1400         net_device->num_chn = 1;
1401
1402         net_device->extension = rndis_device;
1403         rndis_device->ndev = net;
1404
1405         /* Send the rndis initialization message */
1406         ret = rndis_filter_init_device(rndis_device, net_device);
1407         if (ret != 0)
1408                 goto err_dev_remv;
1409
1410         /* Get the MTU from the host */
1411         size = sizeof(u32);
1412         ret = rndis_filter_query_device(rndis_device, net_device,
1413                                         RNDIS_OID_GEN_MAXIMUM_FRAME_SIZE,
1414                                         &mtu, &size);
1415         if (ret == 0 && size == sizeof(u32) && mtu < net->mtu)
1416                 net->mtu = mtu;
1417
1418         /* Get the mac address */
1419         ret = rndis_filter_query_device_mac(rndis_device, net_device);
1420         if (ret != 0)
1421                 goto err_dev_remv;
1422
1423         memcpy(device_info->mac_adr, rndis_device->hw_mac_adr, ETH_ALEN);
1424
1425         /* Get friendly name as ifalias*/
1426         if (!net->ifalias)
1427                 rndis_get_friendly_name(net, rndis_device, net_device);
1428
1429         /* Query and set hardware capabilities */
1430         ret = rndis_netdev_set_hwcaps(rndis_device, net_device);
1431         if (ret != 0)
1432                 goto err_dev_remv;
1433
1434         rndis_filter_query_device_link_status(rndis_device, net_device);
1435
1436         netdev_dbg(net, "Device MAC %pM link state %s\n",
1437                    rndis_device->hw_mac_adr,
1438                    rndis_device->link_state ? "down" : "up");
1439
1440         if (net_device->nvsp_version < NVSP_PROTOCOL_VERSION_5)
1441                 goto out;
1442
1443         rndis_filter_query_link_speed(rndis_device, net_device);
1444
1445         /* vRSS setup */
1446         memset(&rsscap, 0, rsscap_size);
1447         ret = rndis_filter_query_device(rndis_device, net_device,
1448                                         OID_GEN_RECEIVE_SCALE_CAPABILITIES,
1449                                         &rsscap, &rsscap_size);
1450         if (ret || rsscap.num_recv_que < 2)
1451                 goto out;
1452
1453         /* This guarantees that num_possible_rss_qs <= num_online_cpus */
1454         num_possible_rss_qs = min_t(u32, num_online_cpus(),
1455                                     rsscap.num_recv_que);
1456
1457         net_device->max_chn = min_t(u32, VRSS_CHANNEL_MAX, num_possible_rss_qs);
1458
1459         /* We will use the given number of channels if available. */
1460         net_device->num_chn = min(net_device->max_chn, device_info->num_chn);
1461
1462         if (!netif_is_rxfh_configured(net)) {
1463                 for (i = 0; i < ITAB_NUM; i++)
1464                         ndc->rx_table[i] = ethtool_rxfh_indir_default(
1465                                                 i, net_device->num_chn);
1466         }
1467
1468         atomic_set(&net_device->open_chn, 1);
1469         vmbus_set_sc_create_callback(dev->channel, netvsc_sc_open);
1470
1471         for (i = 1; i < net_device->num_chn; i++) {
1472                 ret = netvsc_alloc_recv_comp_ring(net_device, i);
1473                 if (ret) {
1474                         while (--i != 0)
1475                                 vfree(net_device->chan_table[i].mrc.slots);
1476                         goto out;
1477                 }
1478         }
1479
1480         for (i = 1; i < net_device->num_chn; i++)
1481                 netif_napi_add(net, &net_device->chan_table[i].napi,
1482                                netvsc_poll, NAPI_POLL_WEIGHT);
1483
1484         return net_device;
1485
1486 out:
1487         /* setting up multiple channels failed */
1488         net_device->max_chn = 1;
1489         net_device->num_chn = 1;
1490         return net_device;
1491
1492 err_dev_remv:
1493         rndis_filter_device_remove(dev, net_device);
1494         return ERR_PTR(ret);
1495 }
1496
1497 void rndis_filter_device_remove(struct hv_device *dev,
1498                                 struct netvsc_device *net_dev)
1499 {
1500         struct rndis_device *rndis_dev = net_dev->extension;
1501
1502         /* Halt and release the rndis device */
1503         rndis_filter_halt_device(net_dev, rndis_dev);
1504
1505         netvsc_device_remove(dev);
1506 }
1507
1508 int rndis_filter_open(struct netvsc_device *nvdev)
1509 {
1510         if (!nvdev)
1511                 return -EINVAL;
1512
1513         return rndis_filter_open_device(nvdev->extension);
1514 }
1515
1516 int rndis_filter_close(struct netvsc_device *nvdev)
1517 {
1518         if (!nvdev)
1519                 return -EINVAL;
1520
1521         return rndis_filter_close_device(nvdev->extension);
1522 }