root/drivers/net/hyperv/rndis_filter.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. get_rndis_device
  2. get_rndis_request
  3. put_rndis_request
  4. dump_rndis_message
  5. rndis_filter_send_request
  6. rndis_set_link_state
  7. rndis_filter_receive_response
  8. rndis_get_ppi
  9. rsc_add_data
  10. rndis_filter_receive_data
  11. rndis_filter_receive
  12. rndis_filter_query_device
  13. rndis_query_hwcaps
  14. rndis_filter_query_device_mac
  15. rndis_filter_set_device_mac
  16. rndis_filter_set_offload_params
  17. rndis_set_rss_param_msg
  18. rndis_filter_set_rss_param
  19. rndis_filter_query_device_link_status
  20. rndis_filter_query_link_speed
  21. rndis_filter_set_packet_filter
  22. rndis_set_multicast
  23. rndis_filter_update
  24. rndis_filter_init_device
  25. netvsc_device_idle
  26. rndis_filter_halt_device
  27. rndis_filter_open_device
  28. rndis_filter_close_device
  29. netvsc_sc_open
  30. rndis_set_subchannel
  31. rndis_netdev_set_hwcaps
  32. rndis_get_friendly_name
  33. rndis_filter_device_add
  34. rndis_filter_device_remove
  35. rndis_filter_open
  36. rndis_filter_close

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

/* [<][>][^][v][top][bottom][index][help] */