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