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