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