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