linux/drivers/staging/hv/netvsc_drv.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, write to the Free Software Foundation, Inc., 59 Temple
  15 * Place - Suite 330, Boston, MA 02111-1307 USA.
  16 *
  17 * Authors:
  18 *   Haiyang Zhang <haiyangz@microsoft.com>
  19 *   Hank Janssen  <hjanssen@microsoft.com>
  20 */
  21#include <linux/init.h>
  22#include <linux/module.h>
  23#include <linux/highmem.h>
  24#include <linux/device.h>
  25#include <linux/io.h>
  26#include <linux/delay.h>
  27#include <linux/netdevice.h>
  28#include <linux/inetdevice.h>
  29#include <linux/etherdevice.h>
  30#include <linux/skbuff.h>
  31#include <linux/in.h>
  32#include <linux/slab.h>
  33#include <linux/dmi.h>
  34#include <linux/pci.h>
  35#include <net/arp.h>
  36#include <net/route.h>
  37#include <net/sock.h>
  38#include <net/pkt_sched.h>
  39#include "osd.h"
  40#include "logging.h"
  41#include "version_info.h"
  42#include "vmbus.h"
  43#include "netvsc_api.h"
  44
  45struct net_device_context {
  46        /* point back to our device context */
  47        struct vm_device *device_ctx;
  48        unsigned long avail;
  49};
  50
  51struct netvsc_driver_context {
  52        /* !! These must be the first 2 fields !! */
  53        /* Which is a bug FIXME! */
  54        struct driver_context drv_ctx;
  55        struct netvsc_driver drv_obj;
  56};
  57
  58#define PACKET_PAGES_LOWATER  8
  59/* Need this many pages to handle worst case fragmented packet */
  60#define PACKET_PAGES_HIWATER  (MAX_SKB_FRAGS + 2)
  61
  62static int ring_size = 128;
  63module_param(ring_size, int, S_IRUGO);
  64MODULE_PARM_DESC(ring_size, "Ring buffer size (# of pages)");
  65
  66/* The one and only one */
  67static struct netvsc_driver_context g_netvsc_drv;
  68
  69/* no-op so the netdev core doesn't return -EINVAL when modifying the the
  70 * multicast address list in SIOCADDMULTI. hv is setup to get all multicast
  71 * when it calls RndisFilterOnOpen() */
  72static void netvsc_set_multicast_list(struct net_device *net)
  73{
  74}
  75
  76static int netvsc_open(struct net_device *net)
  77{
  78        struct net_device_context *net_device_ctx = netdev_priv(net);
  79        struct hv_device *device_obj = &net_device_ctx->device_ctx->device_obj;
  80        int ret = 0;
  81
  82        if (netif_carrier_ok(net)) {
  83                /* Open up the device */
  84                ret = rndis_filter_open(device_obj);
  85                if (ret != 0) {
  86                        DPRINT_ERR(NETVSC_DRV,
  87                                   "unable to open device (ret %d).", ret);
  88                        return ret;
  89                }
  90
  91                netif_start_queue(net);
  92        } else {
  93                DPRINT_ERR(NETVSC_DRV, "unable to open device...link is down.");
  94        }
  95
  96        return ret;
  97}
  98
  99static int netvsc_close(struct net_device *net)
 100{
 101        struct net_device_context *net_device_ctx = netdev_priv(net);
 102        struct hv_device *device_obj = &net_device_ctx->device_ctx->device_obj;
 103        int ret;
 104
 105        netif_stop_queue(net);
 106
 107        ret = rndis_filter_close(device_obj);
 108        if (ret != 0)
 109                DPRINT_ERR(NETVSC_DRV, "unable to close device (ret %d).", ret);
 110
 111        return ret;
 112}
 113
 114static void netvsc_xmit_completion(void *context)
 115{
 116        struct hv_netvsc_packet *packet = (struct hv_netvsc_packet *)context;
 117        struct sk_buff *skb = (struct sk_buff *)
 118                (unsigned long)packet->completion.send.send_completion_tid;
 119
 120        kfree(packet);
 121
 122        if (skb) {
 123                struct net_device *net = skb->dev;
 124                struct net_device_context *net_device_ctx = netdev_priv(net);
 125                unsigned int num_pages = skb_shinfo(skb)->nr_frags + 2;
 126
 127                dev_kfree_skb_any(skb);
 128
 129                if ((net_device_ctx->avail += num_pages) >= PACKET_PAGES_HIWATER)
 130                        netif_wake_queue(net);
 131        }
 132}
 133
 134static int netvsc_start_xmit(struct sk_buff *skb, struct net_device *net)
 135{
 136        struct net_device_context *net_device_ctx = netdev_priv(net);
 137        struct driver_context *driver_ctx =
 138            driver_to_driver_context(net_device_ctx->device_ctx->device.driver);
 139        struct netvsc_driver_context *net_drv_ctx =
 140                (struct netvsc_driver_context *)driver_ctx;
 141        struct netvsc_driver *net_drv_obj = &net_drv_ctx->drv_obj;
 142        struct hv_netvsc_packet *packet;
 143        int ret;
 144        unsigned int i, num_pages;
 145
 146        DPRINT_DBG(NETVSC_DRV, "xmit packet - len %d data_len %d",
 147                   skb->len, skb->data_len);
 148
 149        /* Add 1 for skb->data and additional one for RNDIS */
 150        num_pages = skb_shinfo(skb)->nr_frags + 1 + 1;
 151        if (num_pages > net_device_ctx->avail)
 152                return NETDEV_TX_BUSY;
 153
 154        /* Allocate a netvsc packet based on # of frags. */
 155        packet = kzalloc(sizeof(struct hv_netvsc_packet) +
 156                         (num_pages * sizeof(struct hv_page_buffer)) +
 157                         net_drv_obj->req_ext_size, GFP_ATOMIC);
 158        if (!packet) {
 159                /* out of memory, silently drop packet */
 160                DPRINT_ERR(NETVSC_DRV, "unable to allocate hv_netvsc_packet");
 161
 162                dev_kfree_skb(skb);
 163                net->stats.tx_dropped++;
 164                return NETDEV_TX_OK;
 165        }
 166
 167        packet->extension = (void *)(unsigned long)packet +
 168                                sizeof(struct hv_netvsc_packet) +
 169                                    (num_pages * sizeof(struct hv_page_buffer));
 170
 171        /* Setup the rndis header */
 172        packet->page_buf_cnt = num_pages;
 173
 174        /* TODO: Flush all write buffers/ memory fence ??? */
 175        /* wmb(); */
 176
 177        /* Initialize it from the skb */
 178        packet->total_data_buflen       = skb->len;
 179
 180        /* Start filling in the page buffers starting after RNDIS buffer. */
 181        packet->page_buf[1].Pfn = virt_to_phys(skb->data) >> PAGE_SHIFT;
 182        packet->page_buf[1].Offset
 183                = (unsigned long)skb->data & (PAGE_SIZE - 1);
 184        packet->page_buf[1].Length = skb_headlen(skb);
 185
 186        /* Additional fragments are after SKB data */
 187        for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
 188                skb_frag_t *f = &skb_shinfo(skb)->frags[i];
 189
 190                packet->page_buf[i+2].Pfn = page_to_pfn(f->page);
 191                packet->page_buf[i+2].Offset = f->page_offset;
 192                packet->page_buf[i+2].Length = f->size;
 193        }
 194
 195        /* Set the completion routine */
 196        packet->completion.send.send_completion = netvsc_xmit_completion;
 197        packet->completion.send.send_completion_ctx = packet;
 198        packet->completion.send.send_completion_tid = (unsigned long)skb;
 199
 200        ret = net_drv_obj->send(&net_device_ctx->device_ctx->device_obj,
 201                                  packet);
 202        if (ret == 0) {
 203                net->stats.tx_bytes += skb->len;
 204                net->stats.tx_packets++;
 205
 206                DPRINT_DBG(NETVSC_DRV, "# of xmits %lu total size %lu",
 207                           net->stats.tx_packets,
 208                           net->stats.tx_bytes);
 209
 210                if ((net_device_ctx->avail -= num_pages) < PACKET_PAGES_LOWATER)
 211                        netif_stop_queue(net);
 212        } else {
 213                /* we are shutting down or bus overloaded, just drop packet */
 214                net->stats.tx_dropped++;
 215                netvsc_xmit_completion(packet);
 216        }
 217
 218        return NETDEV_TX_OK;
 219}
 220
 221/*
 222 * netvsc_linkstatus_callback - Link up/down notification
 223 */
 224static void netvsc_linkstatus_callback(struct hv_device *device_obj,
 225                                       unsigned int status)
 226{
 227        struct vm_device *device_ctx = to_vm_device(device_obj);
 228        struct net_device *net = dev_get_drvdata(&device_ctx->device);
 229
 230        if (!net) {
 231                DPRINT_ERR(NETVSC_DRV, "got link status but net device "
 232                                "not initialized yet");
 233                return;
 234        }
 235
 236        if (status == 1) {
 237                netif_carrier_on(net);
 238                netif_wake_queue(net);
 239                netif_notify_peers(net);
 240        } else {
 241                netif_carrier_off(net);
 242                netif_stop_queue(net);
 243        }
 244}
 245
 246/*
 247 * netvsc_recv_callback -  Callback when we receive a packet from the
 248 * "wire" on the specified device.
 249 */
 250static int netvsc_recv_callback(struct hv_device *device_obj,
 251                                struct hv_netvsc_packet *packet)
 252{
 253        struct vm_device *device_ctx = to_vm_device(device_obj);
 254        struct net_device *net = dev_get_drvdata(&device_ctx->device);
 255        struct sk_buff *skb;
 256        void *data;
 257        int i;
 258        unsigned long flags;
 259
 260        if (!net) {
 261                DPRINT_ERR(NETVSC_DRV, "got receive callback but net device "
 262                                "not initialized yet");
 263                return 0;
 264        }
 265
 266        /* Allocate a skb - TODO direct I/O to pages? */
 267        skb = netdev_alloc_skb_ip_align(net, packet->total_data_buflen);
 268        if (unlikely(!skb)) {
 269                ++net->stats.rx_dropped;
 270                return 0;
 271        }
 272
 273        /* for kmap_atomic */
 274        local_irq_save(flags);
 275
 276        /*
 277         * Copy to skb. This copy is needed here since the memory pointed by
 278         * hv_netvsc_packet cannot be deallocated
 279         */
 280        for (i = 0; i < packet->page_buf_cnt; i++) {
 281                data = kmap_atomic(pfn_to_page(packet->page_buf[i].Pfn),
 282                                               KM_IRQ1);
 283                data = (void *)(unsigned long)data +
 284                                packet->page_buf[i].Offset;
 285
 286                memcpy(skb_put(skb, packet->page_buf[i].Length), data,
 287                       packet->page_buf[i].Length);
 288
 289                kunmap_atomic((void *)((unsigned long)data -
 290                                       packet->page_buf[i].Offset), KM_IRQ1);
 291        }
 292
 293        local_irq_restore(flags);
 294
 295        skb->protocol = eth_type_trans(skb, net);
 296        skb->ip_summed = CHECKSUM_NONE;
 297
 298        net->stats.rx_packets++;
 299        net->stats.rx_bytes += skb->len;
 300
 301        /*
 302         * Pass the skb back up. Network stack will deallocate the skb when it
 303         * is done.
 304         * TODO - use NAPI?
 305         */
 306        netif_rx(skb);
 307
 308        DPRINT_DBG(NETVSC_DRV, "# of recvs %lu total size %lu",
 309                   net->stats.rx_packets, net->stats.rx_bytes);
 310
 311        return 0;
 312}
 313
 314static void netvsc_get_drvinfo(struct net_device *net,
 315                               struct ethtool_drvinfo *info)
 316{
 317        strcpy(info->driver, "hv_netvsc");
 318        strcpy(info->version, HV_DRV_VERSION);
 319        strcpy(info->fw_version, "N/A");
 320}
 321
 322static const struct ethtool_ops ethtool_ops = {
 323        .get_drvinfo    = netvsc_get_drvinfo,
 324        .get_sg         = ethtool_op_get_sg,
 325        .set_sg         = ethtool_op_set_sg,
 326        .get_link       = ethtool_op_get_link,
 327};
 328
 329static const struct net_device_ops device_ops = {
 330        .ndo_open =                     netvsc_open,
 331        .ndo_stop =                     netvsc_close,
 332        .ndo_start_xmit =               netvsc_start_xmit,
 333        .ndo_set_multicast_list =       netvsc_set_multicast_list,
 334        .ndo_change_mtu =               eth_change_mtu,
 335        .ndo_validate_addr =            eth_validate_addr,
 336        .ndo_set_mac_address =          eth_mac_addr,
 337};
 338
 339static int netvsc_probe(struct device *device)
 340{
 341        struct driver_context *driver_ctx =
 342                driver_to_driver_context(device->driver);
 343        struct netvsc_driver_context *net_drv_ctx =
 344                (struct netvsc_driver_context *)driver_ctx;
 345        struct netvsc_driver *net_drv_obj = &net_drv_ctx->drv_obj;
 346        struct vm_device *device_ctx = device_to_vm_device(device);
 347        struct hv_device *device_obj = &device_ctx->device_obj;
 348        struct net_device *net = NULL;
 349        struct net_device_context *net_device_ctx;
 350        struct netvsc_device_info device_info;
 351        int ret;
 352
 353        if (!net_drv_obj->base.OnDeviceAdd)
 354                return -1;
 355
 356        net = alloc_etherdev(sizeof(struct net_device_context));
 357        if (!net)
 358                return -1;
 359
 360        /* Set initial state */
 361        netif_carrier_off(net);
 362
 363        net_device_ctx = netdev_priv(net);
 364        net_device_ctx->device_ctx = device_ctx;
 365        net_device_ctx->avail = ring_size;
 366        dev_set_drvdata(device, net);
 367
 368        /* Notify the netvsc driver of the new device */
 369        ret = net_drv_obj->base.OnDeviceAdd(device_obj, &device_info);
 370        if (ret != 0) {
 371                free_netdev(net);
 372                dev_set_drvdata(device, NULL);
 373
 374                DPRINT_ERR(NETVSC_DRV, "unable to add netvsc device (ret %d)",
 375                           ret);
 376                return ret;
 377        }
 378
 379        /*
 380         * If carrier is still off ie we did not get a link status callback,
 381         * update it if necessary
 382         */
 383        /*
 384         * FIXME: We should use a atomic or test/set instead to avoid getting
 385         * out of sync with the device's link status
 386         */
 387        if (!netif_carrier_ok(net))
 388                if (!device_info.link_state)
 389                        netif_carrier_on(net);
 390
 391        memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN);
 392
 393        net->netdev_ops = &device_ops;
 394
 395        /* TODO: Add GSO and Checksum offload */
 396        net->features = NETIF_F_SG;
 397
 398        SET_ETHTOOL_OPS(net, &ethtool_ops);
 399        SET_NETDEV_DEV(net, device);
 400
 401        ret = register_netdev(net);
 402        if (ret != 0) {
 403                /* Remove the device and release the resource */
 404                net_drv_obj->base.OnDeviceRemove(device_obj);
 405                free_netdev(net);
 406        }
 407
 408        return ret;
 409}
 410
 411static int netvsc_remove(struct device *device)
 412{
 413        struct driver_context *driver_ctx =
 414                driver_to_driver_context(device->driver);
 415        struct netvsc_driver_context *net_drv_ctx =
 416                (struct netvsc_driver_context *)driver_ctx;
 417        struct netvsc_driver *net_drv_obj = &net_drv_ctx->drv_obj;
 418        struct vm_device *device_ctx = device_to_vm_device(device);
 419        struct net_device *net = dev_get_drvdata(&device_ctx->device);
 420        struct hv_device *device_obj = &device_ctx->device_obj;
 421        int ret;
 422
 423        if (net == NULL) {
 424                DPRINT_INFO(NETVSC, "no net device to remove");
 425                return 0;
 426        }
 427
 428        if (!net_drv_obj->base.OnDeviceRemove)
 429                return -1;
 430
 431        /* Stop outbound asap */
 432        netif_stop_queue(net);
 433        /* netif_carrier_off(net); */
 434
 435        unregister_netdev(net);
 436
 437        /*
 438         * Call to the vsc driver to let it know that the device is being
 439         * removed
 440         */
 441        ret = net_drv_obj->base.OnDeviceRemove(device_obj);
 442        if (ret != 0) {
 443                /* TODO: */
 444                DPRINT_ERR(NETVSC, "unable to remove vsc device (ret %d)", ret);
 445        }
 446
 447        free_netdev(net);
 448        return ret;
 449}
 450
 451static int netvsc_drv_exit_cb(struct device *dev, void *data)
 452{
 453        struct device **curr = (struct device **)data;
 454
 455        *curr = dev;
 456        /* stop iterating */
 457        return 1;
 458}
 459
 460static void netvsc_drv_exit(void)
 461{
 462        struct netvsc_driver *netvsc_drv_obj = &g_netvsc_drv.drv_obj;
 463        struct driver_context *drv_ctx = &g_netvsc_drv.drv_ctx;
 464        struct device *current_dev;
 465        int ret;
 466
 467        while (1) {
 468                current_dev = NULL;
 469
 470                /* Get the device */
 471                ret = driver_for_each_device(&drv_ctx->driver, NULL,
 472                                             &current_dev, netvsc_drv_exit_cb);
 473                if (ret)
 474                        DPRINT_WARN(NETVSC_DRV,
 475                                    "driver_for_each_device returned %d", ret);
 476
 477                if (current_dev == NULL)
 478                        break;
 479
 480                /* Initiate removal from the top-down */
 481                DPRINT_INFO(NETVSC_DRV, "unregistering device (%p)...",
 482                            current_dev);
 483
 484                device_unregister(current_dev);
 485        }
 486
 487        if (netvsc_drv_obj->base.OnCleanup)
 488                netvsc_drv_obj->base.OnCleanup(&netvsc_drv_obj->base);
 489
 490        vmbus_child_driver_unregister(drv_ctx);
 491
 492        return;
 493}
 494
 495static int netvsc_drv_init(int (*drv_init)(struct hv_driver *drv))
 496{
 497        struct netvsc_driver *net_drv_obj = &g_netvsc_drv.drv_obj;
 498        struct driver_context *drv_ctx = &g_netvsc_drv.drv_ctx;
 499        int ret;
 500
 501        net_drv_obj->ring_buf_size = ring_size * PAGE_SIZE;
 502        net_drv_obj->recv_cb = netvsc_recv_callback;
 503        net_drv_obj->link_status_change = netvsc_linkstatus_callback;
 504
 505        /* Callback to client driver to complete the initialization */
 506        drv_init(&net_drv_obj->base);
 507
 508        drv_ctx->driver.name = net_drv_obj->base.name;
 509        memcpy(&drv_ctx->class_id, &net_drv_obj->base.deviceType,
 510               sizeof(struct hv_guid));
 511
 512        drv_ctx->probe = netvsc_probe;
 513        drv_ctx->remove = netvsc_remove;
 514
 515        /* The driver belongs to vmbus */
 516        ret = vmbus_child_driver_register(drv_ctx);
 517
 518        return ret;
 519}
 520
 521static const struct dmi_system_id __initconst
 522hv_netvsc_dmi_table[] __maybe_unused  = {
 523        {
 524                .ident = "Hyper-V",
 525                .matches = {
 526                        DMI_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
 527                        DMI_MATCH(DMI_PRODUCT_NAME, "Virtual Machine"),
 528                        DMI_MATCH(DMI_BOARD_NAME, "Virtual Machine"),
 529                },
 530        },
 531        { },
 532};
 533MODULE_DEVICE_TABLE(dmi, hv_netvsc_dmi_table);
 534
 535static int __init netvsc_init(void)
 536{
 537        DPRINT_INFO(NETVSC_DRV, "Netvsc initializing....");
 538
 539        if (!dmi_check_system(hv_netvsc_dmi_table))
 540                return -ENODEV;
 541
 542        return netvsc_drv_init(netvsc_initialize);
 543}
 544
 545static void __exit netvsc_exit(void)
 546{
 547        netvsc_drv_exit();
 548}
 549
 550static const struct pci_device_id __initconst
 551hv_netvsc_pci_table[] __maybe_unused = {
 552        { PCI_DEVICE(0x1414, 0x5353) }, /* VGA compatible controller */
 553        { 0 }
 554};
 555MODULE_DEVICE_TABLE(pci, hv_netvsc_pci_table);
 556
 557MODULE_LICENSE("GPL");
 558MODULE_VERSION(HV_DRV_VERSION);
 559MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
 560
 561module_init(netvsc_init);
 562module_exit(netvsc_exit);
 563