linux/drivers/net/ethernet/intel/ixgbe/ixgbe_sriov.c
<<
>>
Prefs
   1/*******************************************************************************
   2
   3  Intel 10 Gigabit PCI Express Linux driver
   4  Copyright(c) 1999 - 2011 Intel Corporation.
   5
   6  This program is free software; you can redistribute it and/or modify it
   7  under the terms and conditions of the GNU General Public License,
   8  version 2, as published by the Free Software Foundation.
   9
  10  This program is distributed in the hope it will be useful, but WITHOUT
  11  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  13  more details.
  14
  15  You should have received a copy of the GNU General Public License along with
  16  this program; if not, write to the Free Software Foundation, Inc.,
  17  51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  18
  19  The full GNU General Public License is included in this distribution in
  20  the file called "COPYING".
  21
  22  Contact Information:
  23  e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  24  Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  25
  26*******************************************************************************/
  27
  28#include <linux/types.h>
  29#include <linux/module.h>
  30#include <linux/pci.h>
  31#include <linux/netdevice.h>
  32#include <linux/vmalloc.h>
  33#include <linux/string.h>
  34#include <linux/in.h>
  35#include <linux/ip.h>
  36#include <linux/tcp.h>
  37#include <linux/ipv6.h>
  38#ifdef NETIF_F_HW_VLAN_TX
  39#include <linux/if_vlan.h>
  40#endif
  41
  42#include "ixgbe.h"
  43#include "ixgbe_type.h"
  44#include "ixgbe_sriov.h"
  45
  46#ifdef CONFIG_PCI_IOV
  47static int ixgbe_find_enabled_vfs(struct ixgbe_adapter *adapter)
  48{
  49        struct pci_dev *pdev = adapter->pdev;
  50        struct pci_dev *pvfdev;
  51        u16 vf_devfn = 0;
  52        int device_id;
  53        int vfs_found = 0;
  54
  55        switch (adapter->hw.mac.type) {
  56        case ixgbe_mac_82599EB:
  57                device_id = IXGBE_DEV_ID_82599_VF;
  58                break;
  59        case ixgbe_mac_X540:
  60                device_id = IXGBE_DEV_ID_X540_VF;
  61                break;
  62        default:
  63                device_id = 0;
  64                break;
  65        }
  66
  67        vf_devfn = pdev->devfn + 0x80;
  68        pvfdev = pci_get_device(IXGBE_INTEL_VENDOR_ID, device_id, NULL);
  69        while (pvfdev) {
  70                if (pvfdev->devfn == vf_devfn &&
  71                    (pvfdev->bus->number >= pdev->bus->number))
  72                        vfs_found++;
  73                vf_devfn += 2;
  74                pvfdev = pci_get_device(IXGBE_INTEL_VENDOR_ID,
  75                                        device_id, pvfdev);
  76        }
  77
  78        return vfs_found;
  79}
  80
  81void ixgbe_enable_sriov(struct ixgbe_adapter *adapter,
  82                         const struct ixgbe_info *ii)
  83{
  84        struct ixgbe_hw *hw = &adapter->hw;
  85        int err = 0;
  86        int num_vf_macvlans, i;
  87        struct vf_macvlans *mv_list;
  88        int pre_existing_vfs = 0;
  89
  90        pre_existing_vfs = ixgbe_find_enabled_vfs(adapter);
  91        if (!pre_existing_vfs && !adapter->num_vfs)
  92                return;
  93
  94        /* If there are pre-existing VFs then we have to force
  95         * use of that many because they were not deleted the last
  96         * time someone removed the PF driver.  That would have
  97         * been because they were allocated to guest VMs and can't
  98         * be removed.  Go ahead and just re-enable the old amount.
  99         * If the user wants to change the number of VFs they can
 100         * use ethtool while making sure no VFs are allocated to
 101         * guest VMs... i.e. the right way.
 102         */
 103        if (pre_existing_vfs) {
 104                adapter->num_vfs = pre_existing_vfs;
 105                dev_warn(&adapter->pdev->dev, "Virtual Functions already "
 106                         "enabled for this device - Please reload all "
 107                         "VF drivers to avoid spoofed packet errors\n");
 108        } else {
 109                err = pci_enable_sriov(adapter->pdev, adapter->num_vfs);
 110        }
 111        if (err) {
 112                e_err(probe, "Failed to enable PCI sriov: %d\n", err);
 113                goto err_novfs;
 114        }
 115        adapter->flags |= IXGBE_FLAG_SRIOV_ENABLED;
 116
 117        e_info(probe, "SR-IOV enabled with %d VFs\n", adapter->num_vfs);
 118
 119        num_vf_macvlans = hw->mac.num_rar_entries -
 120        (IXGBE_MAX_PF_MACVLANS + 1 + adapter->num_vfs);
 121
 122        adapter->mv_list = mv_list = kcalloc(num_vf_macvlans,
 123                                             sizeof(struct vf_macvlans),
 124                                             GFP_KERNEL);
 125        if (mv_list) {
 126                /* Initialize list of VF macvlans */
 127                INIT_LIST_HEAD(&adapter->vf_mvs.l);
 128                for (i = 0; i < num_vf_macvlans; i++) {
 129                        mv_list->vf = -1;
 130                        mv_list->free = true;
 131                        mv_list->rar_entry = hw->mac.num_rar_entries -
 132                                (i + adapter->num_vfs + 1);
 133                        list_add(&mv_list->l, &adapter->vf_mvs.l);
 134                        mv_list++;
 135                }
 136        }
 137
 138        /* If call to enable VFs succeeded then allocate memory
 139         * for per VF control structures.
 140         */
 141        adapter->vfinfo =
 142                kcalloc(adapter->num_vfs,
 143                        sizeof(struct vf_data_storage), GFP_KERNEL);
 144        if (adapter->vfinfo) {
 145                /* Now that we're sure SR-IOV is enabled
 146                 * and memory allocated set up the mailbox parameters
 147                 */
 148                ixgbe_init_mbx_params_pf(hw);
 149                memcpy(&hw->mbx.ops, ii->mbx_ops,
 150                       sizeof(hw->mbx.ops));
 151
 152                /* Disable RSC when in SR-IOV mode */
 153                adapter->flags2 &= ~(IXGBE_FLAG2_RSC_CAPABLE |
 154                                     IXGBE_FLAG2_RSC_ENABLED);
 155                for (i = 0; i < adapter->num_vfs; i++)
 156                        adapter->vfinfo[i].spoofchk_enabled = true;
 157                return;
 158        }
 159
 160        /* Oh oh */
 161        e_err(probe, "Unable to allocate memory for VF Data Storage - "
 162              "SRIOV disabled\n");
 163        pci_disable_sriov(adapter->pdev);
 164
 165err_novfs:
 166        adapter->flags &= ~IXGBE_FLAG_SRIOV_ENABLED;
 167        adapter->num_vfs = 0;
 168}
 169#endif /* #ifdef CONFIG_PCI_IOV */
 170
 171void ixgbe_disable_sriov(struct ixgbe_adapter *adapter)
 172{
 173        struct ixgbe_hw *hw = &adapter->hw;
 174        u32 gcr;
 175        u32 gpie;
 176        u32 vmdctl;
 177        int i;
 178
 179#ifdef CONFIG_PCI_IOV
 180        /* disable iov and allow time for transactions to clear */
 181        pci_disable_sriov(adapter->pdev);
 182#endif
 183
 184        /* turn off device IOV mode */
 185        gcr = IXGBE_READ_REG(hw, IXGBE_GCR_EXT);
 186        gcr &= ~(IXGBE_GCR_EXT_SRIOV);
 187        IXGBE_WRITE_REG(hw, IXGBE_GCR_EXT, gcr);
 188        gpie = IXGBE_READ_REG(hw, IXGBE_GPIE);
 189        gpie &= ~IXGBE_GPIE_VTMODE_MASK;
 190        IXGBE_WRITE_REG(hw, IXGBE_GPIE, gpie);
 191
 192        /* set default pool back to 0 */
 193        vmdctl = IXGBE_READ_REG(hw, IXGBE_VT_CTL);
 194        vmdctl &= ~IXGBE_VT_CTL_POOL_MASK;
 195        IXGBE_WRITE_REG(hw, IXGBE_VT_CTL, vmdctl);
 196        IXGBE_WRITE_FLUSH(hw);
 197
 198        /* take a breather then clean up driver data */
 199        msleep(100);
 200
 201        /* Release reference to VF devices */
 202        for (i = 0; i < adapter->num_vfs; i++) {
 203                if (adapter->vfinfo[i].vfdev)
 204                        pci_dev_put(adapter->vfinfo[i].vfdev);
 205        }
 206        kfree(adapter->vfinfo);
 207        kfree(adapter->mv_list);
 208        adapter->vfinfo = NULL;
 209
 210        adapter->num_vfs = 0;
 211        adapter->flags &= ~IXGBE_FLAG_SRIOV_ENABLED;
 212}
 213
 214static int ixgbe_set_vf_multicasts(struct ixgbe_adapter *adapter,
 215                                   int entries, u16 *hash_list, u32 vf)
 216{
 217        struct vf_data_storage *vfinfo = &adapter->vfinfo[vf];
 218        struct ixgbe_hw *hw = &adapter->hw;
 219        int i;
 220        u32 vector_bit;
 221        u32 vector_reg;
 222        u32 mta_reg;
 223
 224        /* only so many hash values supported */
 225        entries = min(entries, IXGBE_MAX_VF_MC_ENTRIES);
 226
 227        /*
 228         * salt away the number of multi cast addresses assigned
 229         * to this VF for later use to restore when the PF multi cast
 230         * list changes
 231         */
 232        vfinfo->num_vf_mc_hashes = entries;
 233
 234        /*
 235         * VFs are limited to using the MTA hash table for their multicast
 236         * addresses
 237         */
 238        for (i = 0; i < entries; i++) {
 239                vfinfo->vf_mc_hashes[i] = hash_list[i];
 240        }
 241
 242        for (i = 0; i < vfinfo->num_vf_mc_hashes; i++) {
 243                vector_reg = (vfinfo->vf_mc_hashes[i] >> 5) & 0x7F;
 244                vector_bit = vfinfo->vf_mc_hashes[i] & 0x1F;
 245                mta_reg = IXGBE_READ_REG(hw, IXGBE_MTA(vector_reg));
 246                mta_reg |= (1 << vector_bit);
 247                IXGBE_WRITE_REG(hw, IXGBE_MTA(vector_reg), mta_reg);
 248        }
 249
 250        return 0;
 251}
 252
 253static void ixgbe_restore_vf_macvlans(struct ixgbe_adapter *adapter)
 254{
 255        struct ixgbe_hw *hw = &adapter->hw;
 256        struct list_head *pos;
 257        struct vf_macvlans *entry;
 258
 259        list_for_each(pos, &adapter->vf_mvs.l) {
 260                entry = list_entry(pos, struct vf_macvlans, l);
 261                if (entry->free == false)
 262                        hw->mac.ops.set_rar(hw, entry->rar_entry,
 263                                            entry->vf_macvlan,
 264                                            entry->vf, IXGBE_RAH_AV);
 265        }
 266}
 267
 268void ixgbe_restore_vf_multicasts(struct ixgbe_adapter *adapter)
 269{
 270        struct ixgbe_hw *hw = &adapter->hw;
 271        struct vf_data_storage *vfinfo;
 272        int i, j;
 273        u32 vector_bit;
 274        u32 vector_reg;
 275        u32 mta_reg;
 276
 277        for (i = 0; i < adapter->num_vfs; i++) {
 278                vfinfo = &adapter->vfinfo[i];
 279                for (j = 0; j < vfinfo->num_vf_mc_hashes; j++) {
 280                        hw->addr_ctrl.mta_in_use++;
 281                        vector_reg = (vfinfo->vf_mc_hashes[j] >> 5) & 0x7F;
 282                        vector_bit = vfinfo->vf_mc_hashes[j] & 0x1F;
 283                        mta_reg = IXGBE_READ_REG(hw, IXGBE_MTA(vector_reg));
 284                        mta_reg |= (1 << vector_bit);
 285                        IXGBE_WRITE_REG(hw, IXGBE_MTA(vector_reg), mta_reg);
 286                }
 287        }
 288
 289        /* Restore any VF macvlans */
 290        ixgbe_restore_vf_macvlans(adapter);
 291}
 292
 293static int ixgbe_set_vf_vlan(struct ixgbe_adapter *adapter, int add, int vid,
 294                             u32 vf)
 295{
 296        return adapter->hw.mac.ops.set_vfta(&adapter->hw, vid, vf, (bool)add);
 297}
 298
 299static void ixgbe_set_vf_lpe(struct ixgbe_adapter *adapter, u32 *msgbuf)
 300{
 301        struct ixgbe_hw *hw = &adapter->hw;
 302        int new_mtu = msgbuf[1];
 303        u32 max_frs;
 304        int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
 305
 306        /* Only X540 supports jumbo frames in IOV mode */
 307        if (adapter->hw.mac.type != ixgbe_mac_X540)
 308                return;
 309
 310        /* MTU < 68 is an error and causes problems on some kernels */
 311        if ((new_mtu < 68) || (max_frame > IXGBE_MAX_JUMBO_FRAME_SIZE)) {
 312                e_err(drv, "VF mtu %d out of range\n", new_mtu);
 313                return;
 314        }
 315
 316        max_frs = (IXGBE_READ_REG(hw, IXGBE_MAXFRS) &
 317                   IXGBE_MHADD_MFS_MASK) >> IXGBE_MHADD_MFS_SHIFT;
 318        if (max_frs < new_mtu) {
 319                max_frs = new_mtu << IXGBE_MHADD_MFS_SHIFT;
 320                IXGBE_WRITE_REG(hw, IXGBE_MAXFRS, max_frs);
 321        }
 322
 323        e_info(hw, "VF requests change max MTU to %d\n", new_mtu);
 324}
 325
 326static void ixgbe_set_vmolr(struct ixgbe_hw *hw, u32 vf, bool aupe)
 327{
 328        u32 vmolr = IXGBE_READ_REG(hw, IXGBE_VMOLR(vf));
 329        vmolr |= (IXGBE_VMOLR_ROMPE |
 330                  IXGBE_VMOLR_BAM);
 331        if (aupe)
 332                vmolr |= IXGBE_VMOLR_AUPE;
 333        else
 334                vmolr &= ~IXGBE_VMOLR_AUPE;
 335        IXGBE_WRITE_REG(hw, IXGBE_VMOLR(vf), vmolr);
 336}
 337
 338static void ixgbe_set_vmvir(struct ixgbe_adapter *adapter, u32 vid, u32 vf)
 339{
 340        struct ixgbe_hw *hw = &adapter->hw;
 341
 342        if (vid)
 343                IXGBE_WRITE_REG(hw, IXGBE_VMVIR(vf),
 344                                (vid | IXGBE_VMVIR_VLANA_DEFAULT));
 345        else
 346                IXGBE_WRITE_REG(hw, IXGBE_VMVIR(vf), 0);
 347}
 348
 349static inline void ixgbe_vf_reset_event(struct ixgbe_adapter *adapter, u32 vf)
 350{
 351        struct ixgbe_hw *hw = &adapter->hw;
 352        int rar_entry = hw->mac.num_rar_entries - (vf + 1);
 353
 354        /* reset offloads to defaults */
 355        if (adapter->vfinfo[vf].pf_vlan) {
 356                ixgbe_set_vf_vlan(adapter, true,
 357                                  adapter->vfinfo[vf].pf_vlan, vf);
 358                ixgbe_set_vmvir(adapter,
 359                                (adapter->vfinfo[vf].pf_vlan |
 360                                 (adapter->vfinfo[vf].pf_qos <<
 361                                  VLAN_PRIO_SHIFT)), vf);
 362                ixgbe_set_vmolr(hw, vf, false);
 363        } else {
 364                ixgbe_set_vmvir(adapter, 0, vf);
 365                ixgbe_set_vmolr(hw, vf, true);
 366        }
 367
 368        /* reset multicast table array for vf */
 369        adapter->vfinfo[vf].num_vf_mc_hashes = 0;
 370
 371        /* Flush and reset the mta with the new values */
 372        ixgbe_set_rx_mode(adapter->netdev);
 373
 374        hw->mac.ops.clear_rar(hw, rar_entry);
 375}
 376
 377static int ixgbe_set_vf_mac(struct ixgbe_adapter *adapter,
 378                            int vf, unsigned char *mac_addr)
 379{
 380        struct ixgbe_hw *hw = &adapter->hw;
 381        int rar_entry = hw->mac.num_rar_entries - (vf + 1);
 382
 383        memcpy(adapter->vfinfo[vf].vf_mac_addresses, mac_addr, 6);
 384        hw->mac.ops.set_rar(hw, rar_entry, mac_addr, vf, IXGBE_RAH_AV);
 385
 386        return 0;
 387}
 388
 389static int ixgbe_set_vf_macvlan(struct ixgbe_adapter *adapter,
 390                                int vf, int index, unsigned char *mac_addr)
 391{
 392        struct ixgbe_hw *hw = &adapter->hw;
 393        struct list_head *pos;
 394        struct vf_macvlans *entry;
 395
 396        if (index <= 1) {
 397                list_for_each(pos, &adapter->vf_mvs.l) {
 398                        entry = list_entry(pos, struct vf_macvlans, l);
 399                        if (entry->vf == vf) {
 400                                entry->vf = -1;
 401                                entry->free = true;
 402                                entry->is_macvlan = false;
 403                                hw->mac.ops.clear_rar(hw, entry->rar_entry);
 404                        }
 405                }
 406        }
 407
 408        /*
 409         * If index was zero then we were asked to clear the uc list
 410         * for the VF.  We're done.
 411         */
 412        if (!index)
 413                return 0;
 414
 415        entry = NULL;
 416
 417        list_for_each(pos, &adapter->vf_mvs.l) {
 418                entry = list_entry(pos, struct vf_macvlans, l);
 419                if (entry->free)
 420                        break;
 421        }
 422
 423        /*
 424         * If we traversed the entire list and didn't find a free entry
 425         * then we're out of space on the RAR table.  Also entry may
 426         * be NULL because the original memory allocation for the list
 427         * failed, which is not fatal but does mean we can't support
 428         * VF requests for MACVLAN because we couldn't allocate
 429         * memory for the list management required.
 430         */
 431        if (!entry || !entry->free)
 432                return -ENOSPC;
 433
 434        entry->free = false;
 435        entry->is_macvlan = true;
 436        entry->vf = vf;
 437        memcpy(entry->vf_macvlan, mac_addr, ETH_ALEN);
 438
 439        hw->mac.ops.set_rar(hw, entry->rar_entry, mac_addr, vf, IXGBE_RAH_AV);
 440
 441        return 0;
 442}
 443
 444int ixgbe_check_vf_assignment(struct ixgbe_adapter *adapter)
 445{
 446#ifdef CONFIG_PCI_IOV
 447        int i;
 448        for (i = 0; i < adapter->num_vfs; i++) {
 449                if (adapter->vfinfo[i].vfdev->dev_flags &
 450                                PCI_DEV_FLAGS_ASSIGNED)
 451                        return true;
 452        }
 453#endif
 454        return false;
 455}
 456
 457int ixgbe_vf_configuration(struct pci_dev *pdev, unsigned int event_mask)
 458{
 459        unsigned char vf_mac_addr[6];
 460        struct ixgbe_adapter *adapter = pci_get_drvdata(pdev);
 461        unsigned int vfn = (event_mask & 0x3f);
 462        struct pci_dev *pvfdev;
 463        unsigned int device_id;
 464        u16 thisvf_devfn = (pdev->devfn + 0x80 + (vfn << 1)) |
 465                                (pdev->devfn & 1);
 466
 467        bool enable = ((event_mask & 0x10000000U) != 0);
 468
 469        if (enable) {
 470                random_ether_addr(vf_mac_addr);
 471                e_info(probe, "IOV: VF %d is enabled MAC %pM\n",
 472                       vfn, vf_mac_addr);
 473                /*
 474                 * Store away the VF "permananet" MAC address, it will ask
 475                 * for it later.
 476                 */
 477                memcpy(adapter->vfinfo[vfn].vf_mac_addresses, vf_mac_addr, 6);
 478
 479                switch (adapter->hw.mac.type) {
 480                case ixgbe_mac_82599EB:
 481                        device_id = IXGBE_DEV_ID_82599_VF;
 482                        break;
 483                case ixgbe_mac_X540:
 484                        device_id = IXGBE_DEV_ID_X540_VF;
 485                        break;
 486                default:
 487                        device_id = 0;
 488                        break;
 489                }
 490
 491                pvfdev = pci_get_device(IXGBE_INTEL_VENDOR_ID, device_id, NULL);
 492                while (pvfdev) {
 493                        if (pvfdev->devfn == thisvf_devfn)
 494                                break;
 495                        pvfdev = pci_get_device(IXGBE_INTEL_VENDOR_ID,
 496                                                device_id, pvfdev);
 497                }
 498                if (pvfdev)
 499                        adapter->vfinfo[vfn].vfdev = pvfdev;
 500                else
 501                        e_err(drv, "Couldn't find pci dev ptr for VF %4.4x\n",
 502                              thisvf_devfn);
 503        }
 504
 505        return 0;
 506}
 507
 508static inline void ixgbe_vf_reset_msg(struct ixgbe_adapter *adapter, u32 vf)
 509{
 510        struct ixgbe_hw *hw = &adapter->hw;
 511        u32 reg;
 512        u32 reg_offset, vf_shift;
 513
 514        vf_shift = vf % 32;
 515        reg_offset = vf / 32;
 516
 517        /* enable transmit and receive for vf */
 518        reg = IXGBE_READ_REG(hw, IXGBE_VFTE(reg_offset));
 519        reg |= (reg | (1 << vf_shift));
 520        IXGBE_WRITE_REG(hw, IXGBE_VFTE(reg_offset), reg);
 521
 522        reg = IXGBE_READ_REG(hw, IXGBE_VFRE(reg_offset));
 523        reg |= (reg | (1 << vf_shift));
 524        IXGBE_WRITE_REG(hw, IXGBE_VFRE(reg_offset), reg);
 525
 526        /* Enable counting of spoofed packets in the SSVPC register */
 527        reg = IXGBE_READ_REG(hw, IXGBE_VMECM(reg_offset));
 528        reg |= (1 << vf_shift);
 529        IXGBE_WRITE_REG(hw, IXGBE_VMECM(reg_offset), reg);
 530
 531        ixgbe_vf_reset_event(adapter, vf);
 532}
 533
 534static int ixgbe_rcv_msg_from_vf(struct ixgbe_adapter *adapter, u32 vf)
 535{
 536        u32 mbx_size = IXGBE_VFMAILBOX_SIZE;
 537        u32 msgbuf[IXGBE_VFMAILBOX_SIZE];
 538        struct ixgbe_hw *hw = &adapter->hw;
 539        s32 retval;
 540        int entries;
 541        u16 *hash_list;
 542        int add, vid, index;
 543        u8 *new_mac;
 544
 545        retval = ixgbe_read_mbx(hw, msgbuf, mbx_size, vf);
 546
 547        if (retval)
 548                pr_err("Error receiving message from VF\n");
 549
 550        /* this is a message we already processed, do nothing */
 551        if (msgbuf[0] & (IXGBE_VT_MSGTYPE_ACK | IXGBE_VT_MSGTYPE_NACK))
 552                return retval;
 553
 554        /*
 555         * until the vf completes a virtual function reset it should not be
 556         * allowed to start any configuration.
 557         */
 558
 559        if (msgbuf[0] == IXGBE_VF_RESET) {
 560                unsigned char *vf_mac = adapter->vfinfo[vf].vf_mac_addresses;
 561                new_mac = (u8 *)(&msgbuf[1]);
 562                e_info(probe, "VF Reset msg received from vf %d\n", vf);
 563                adapter->vfinfo[vf].clear_to_send = false;
 564                ixgbe_vf_reset_msg(adapter, vf);
 565                adapter->vfinfo[vf].clear_to_send = true;
 566
 567                if (is_valid_ether_addr(new_mac) &&
 568                    !adapter->vfinfo[vf].pf_set_mac)
 569                        ixgbe_set_vf_mac(adapter, vf, vf_mac);
 570                else
 571                        ixgbe_set_vf_mac(adapter,
 572                                 vf, adapter->vfinfo[vf].vf_mac_addresses);
 573
 574                /* reply to reset with ack and vf mac address */
 575                msgbuf[0] = IXGBE_VF_RESET | IXGBE_VT_MSGTYPE_ACK;
 576                memcpy(new_mac, vf_mac, IXGBE_ETH_LENGTH_OF_ADDRESS);
 577                /*
 578                 * Piggyback the multicast filter type so VF can compute the
 579                 * correct vectors
 580                 */
 581                msgbuf[3] = hw->mac.mc_filter_type;
 582                ixgbe_write_mbx(hw, msgbuf, IXGBE_VF_PERMADDR_MSG_LEN, vf);
 583
 584                return retval;
 585        }
 586
 587        if (!adapter->vfinfo[vf].clear_to_send) {
 588                msgbuf[0] |= IXGBE_VT_MSGTYPE_NACK;
 589                ixgbe_write_mbx(hw, msgbuf, 1, vf);
 590                return retval;
 591        }
 592
 593        switch ((msgbuf[0] & 0xFFFF)) {
 594        case IXGBE_VF_SET_MAC_ADDR:
 595                new_mac = ((u8 *)(&msgbuf[1]));
 596                if (is_valid_ether_addr(new_mac) &&
 597                    !adapter->vfinfo[vf].pf_set_mac) {
 598                        ixgbe_set_vf_mac(adapter, vf, new_mac);
 599                } else if (memcmp(adapter->vfinfo[vf].vf_mac_addresses,
 600                                  new_mac, ETH_ALEN)) {
 601                        e_warn(drv, "VF %d attempted to override "
 602                               "administratively set MAC address\nReload "
 603                               "the VF driver to resume operations\n", vf);
 604                        retval = -1;
 605                }
 606                break;
 607        case IXGBE_VF_SET_MULTICAST:
 608                entries = (msgbuf[0] & IXGBE_VT_MSGINFO_MASK)
 609                          >> IXGBE_VT_MSGINFO_SHIFT;
 610                hash_list = (u16 *)&msgbuf[1];
 611                retval = ixgbe_set_vf_multicasts(adapter, entries,
 612                                                 hash_list, vf);
 613                break;
 614        case IXGBE_VF_SET_LPE:
 615                ixgbe_set_vf_lpe(adapter, msgbuf);
 616                break;
 617        case IXGBE_VF_SET_VLAN:
 618                add = (msgbuf[0] & IXGBE_VT_MSGINFO_MASK)
 619                      >> IXGBE_VT_MSGINFO_SHIFT;
 620                vid = (msgbuf[1] & IXGBE_VLVF_VLANID_MASK);
 621                if (adapter->vfinfo[vf].pf_vlan) {
 622                        e_warn(drv, "VF %d attempted to override "
 623                               "administratively set VLAN configuration\n"
 624                               "Reload the VF driver to resume operations\n",
 625                               vf);
 626                        retval = -1;
 627                } else {
 628                        if (add)
 629                                adapter->vfinfo[vf].vlan_count++;
 630                        else if (adapter->vfinfo[vf].vlan_count)
 631                                adapter->vfinfo[vf].vlan_count--;
 632                        retval = ixgbe_set_vf_vlan(adapter, add, vid, vf);
 633                        if (!retval && adapter->vfinfo[vf].spoofchk_enabled)
 634                                hw->mac.ops.set_vlan_anti_spoofing(hw, true, vf);
 635                }
 636                break;
 637        case IXGBE_VF_SET_MACVLAN:
 638                index = (msgbuf[0] & IXGBE_VT_MSGINFO_MASK) >>
 639                        IXGBE_VT_MSGINFO_SHIFT;
 640                /*
 641                 * If the VF is allowed to set MAC filters then turn off
 642                 * anti-spoofing to avoid false positives.  An index
 643                 * greater than 0 will indicate the VF is setting a
 644                 * macvlan MAC filter.
 645                 */
 646                if (index > 0 && adapter->vfinfo[vf].spoofchk_enabled)
 647                        ixgbe_ndo_set_vf_spoofchk(adapter->netdev, vf, false);
 648                retval = ixgbe_set_vf_macvlan(adapter, vf, index,
 649                                              (unsigned char *)(&msgbuf[1]));
 650                break;
 651        default:
 652                e_err(drv, "Unhandled Msg %8.8x\n", msgbuf[0]);
 653                retval = IXGBE_ERR_MBX;
 654                break;
 655        }
 656
 657        /* notify the VF of the results of what it sent us */
 658        if (retval)
 659                msgbuf[0] |= IXGBE_VT_MSGTYPE_NACK;
 660        else
 661                msgbuf[0] |= IXGBE_VT_MSGTYPE_ACK;
 662
 663        msgbuf[0] |= IXGBE_VT_MSGTYPE_CTS;
 664
 665        ixgbe_write_mbx(hw, msgbuf, 1, vf);
 666
 667        return retval;
 668}
 669
 670static void ixgbe_rcv_ack_from_vf(struct ixgbe_adapter *adapter, u32 vf)
 671{
 672        struct ixgbe_hw *hw = &adapter->hw;
 673        u32 msg = IXGBE_VT_MSGTYPE_NACK;
 674
 675        /* if device isn't clear to send it shouldn't be reading either */
 676        if (!adapter->vfinfo[vf].clear_to_send)
 677                ixgbe_write_mbx(hw, &msg, 1, vf);
 678}
 679
 680void ixgbe_msg_task(struct ixgbe_adapter *adapter)
 681{
 682        struct ixgbe_hw *hw = &adapter->hw;
 683        u32 vf;
 684
 685        for (vf = 0; vf < adapter->num_vfs; vf++) {
 686                /* process any reset requests */
 687                if (!ixgbe_check_for_rst(hw, vf))
 688                        ixgbe_vf_reset_event(adapter, vf);
 689
 690                /* process any messages pending */
 691                if (!ixgbe_check_for_msg(hw, vf))
 692                        ixgbe_rcv_msg_from_vf(adapter, vf);
 693
 694                /* process any acks */
 695                if (!ixgbe_check_for_ack(hw, vf))
 696                        ixgbe_rcv_ack_from_vf(adapter, vf);
 697        }
 698}
 699
 700void ixgbe_disable_tx_rx(struct ixgbe_adapter *adapter)
 701{
 702        struct ixgbe_hw *hw = &adapter->hw;
 703
 704        /* disable transmit and receive for all vfs */
 705        IXGBE_WRITE_REG(hw, IXGBE_VFTE(0), 0);
 706        IXGBE_WRITE_REG(hw, IXGBE_VFTE(1), 0);
 707
 708        IXGBE_WRITE_REG(hw, IXGBE_VFRE(0), 0);
 709        IXGBE_WRITE_REG(hw, IXGBE_VFRE(1), 0);
 710}
 711
 712void ixgbe_ping_all_vfs(struct ixgbe_adapter *adapter)
 713{
 714        struct ixgbe_hw *hw = &adapter->hw;
 715        u32 ping;
 716        int i;
 717
 718        for (i = 0 ; i < adapter->num_vfs; i++) {
 719                ping = IXGBE_PF_CONTROL_MSG;
 720                if (adapter->vfinfo[i].clear_to_send)
 721                        ping |= IXGBE_VT_MSGTYPE_CTS;
 722                ixgbe_write_mbx(hw, &ping, 1, i);
 723        }
 724}
 725
 726int ixgbe_ndo_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
 727{
 728        struct ixgbe_adapter *adapter = netdev_priv(netdev);
 729        if (!is_valid_ether_addr(mac) || (vf >= adapter->num_vfs))
 730                return -EINVAL;
 731        adapter->vfinfo[vf].pf_set_mac = true;
 732        dev_info(&adapter->pdev->dev, "setting MAC %pM on VF %d\n", mac, vf);
 733        dev_info(&adapter->pdev->dev, "Reload the VF driver to make this"
 734                                      " change effective.");
 735        if (test_bit(__IXGBE_DOWN, &adapter->state)) {
 736                dev_warn(&adapter->pdev->dev, "The VF MAC address has been set,"
 737                         " but the PF device is not up.\n");
 738                dev_warn(&adapter->pdev->dev, "Bring the PF device up before"
 739                         " attempting to use the VF device.\n");
 740        }
 741        return ixgbe_set_vf_mac(adapter, vf, mac);
 742}
 743
 744int ixgbe_ndo_set_vf_vlan(struct net_device *netdev, int vf, u16 vlan, u8 qos)
 745{
 746        int err = 0;
 747        struct ixgbe_adapter *adapter = netdev_priv(netdev);
 748        struct ixgbe_hw *hw = &adapter->hw;
 749
 750        if ((vf >= adapter->num_vfs) || (vlan > 4095) || (qos > 7))
 751                return -EINVAL;
 752        if (vlan || qos) {
 753                err = ixgbe_set_vf_vlan(adapter, true, vlan, vf);
 754                if (err)
 755                        goto out;
 756                ixgbe_set_vmvir(adapter, vlan | (qos << VLAN_PRIO_SHIFT), vf);
 757                ixgbe_set_vmolr(hw, vf, false);
 758                if (adapter->vfinfo[vf].spoofchk_enabled)
 759                        hw->mac.ops.set_vlan_anti_spoofing(hw, true, vf);
 760                adapter->vfinfo[vf].vlan_count++;
 761                adapter->vfinfo[vf].pf_vlan = vlan;
 762                adapter->vfinfo[vf].pf_qos = qos;
 763                dev_info(&adapter->pdev->dev,
 764                         "Setting VLAN %d, QOS 0x%x on VF %d\n", vlan, qos, vf);
 765                if (test_bit(__IXGBE_DOWN, &adapter->state)) {
 766                        dev_warn(&adapter->pdev->dev,
 767                                 "The VF VLAN has been set,"
 768                                 " but the PF device is not up.\n");
 769                        dev_warn(&adapter->pdev->dev,
 770                                 "Bring the PF device up before"
 771                                 " attempting to use the VF device.\n");
 772                }
 773        } else {
 774                err = ixgbe_set_vf_vlan(adapter, false,
 775                                        adapter->vfinfo[vf].pf_vlan, vf);
 776                ixgbe_set_vmvir(adapter, vlan, vf);
 777                ixgbe_set_vmolr(hw, vf, true);
 778                hw->mac.ops.set_vlan_anti_spoofing(hw, false, vf);
 779                if (adapter->vfinfo[vf].vlan_count)
 780                        adapter->vfinfo[vf].vlan_count--;
 781                adapter->vfinfo[vf].pf_vlan = 0;
 782                adapter->vfinfo[vf].pf_qos = 0;
 783       }
 784out:
 785       return err;
 786}
 787
 788static int ixgbe_link_mbps(int internal_link_speed)
 789{
 790        switch (internal_link_speed) {
 791        case IXGBE_LINK_SPEED_100_FULL:
 792                return 100;
 793        case IXGBE_LINK_SPEED_1GB_FULL:
 794                return 1000;
 795        case IXGBE_LINK_SPEED_10GB_FULL:
 796                return 10000;
 797        default:
 798                return 0;
 799        }
 800}
 801
 802static void ixgbe_set_vf_rate_limit(struct ixgbe_hw *hw, int vf, int tx_rate,
 803                                    int link_speed)
 804{
 805        int rf_dec, rf_int;
 806        u32 bcnrc_val;
 807
 808        if (tx_rate != 0) {
 809                /* Calculate the rate factor values to set */
 810                rf_int = link_speed / tx_rate;
 811                rf_dec = (link_speed - (rf_int * tx_rate));
 812                rf_dec = (rf_dec * (1<<IXGBE_RTTBCNRC_RF_INT_SHIFT)) / tx_rate;
 813
 814                bcnrc_val = IXGBE_RTTBCNRC_RS_ENA;
 815                bcnrc_val |= ((rf_int<<IXGBE_RTTBCNRC_RF_INT_SHIFT) &
 816                               IXGBE_RTTBCNRC_RF_INT_MASK);
 817                bcnrc_val |= (rf_dec & IXGBE_RTTBCNRC_RF_DEC_MASK);
 818        } else {
 819                bcnrc_val = 0;
 820        }
 821
 822        IXGBE_WRITE_REG(hw, IXGBE_RTTDQSEL, 2*vf); /* vf Y uses queue 2*Y */
 823        /*
 824         * Set global transmit compensation time to the MMW_SIZE in RTTBCNRM
 825         * register. Typically MMW_SIZE=0x014 if 9728-byte jumbo is supported
 826         * and 0x004 otherwise.
 827         */
 828        switch (hw->mac.type) {
 829        case ixgbe_mac_82599EB:
 830                IXGBE_WRITE_REG(hw, IXGBE_RTTBCNRM, 0x4);
 831                break;
 832        case ixgbe_mac_X540:
 833                IXGBE_WRITE_REG(hw, IXGBE_RTTBCNRM, 0x14);
 834                break;
 835        default:
 836                break;
 837        }
 838
 839        IXGBE_WRITE_REG(hw, IXGBE_RTTBCNRC, bcnrc_val);
 840}
 841
 842void ixgbe_check_vf_rate_limit(struct ixgbe_adapter *adapter)
 843{
 844        int actual_link_speed, i;
 845        bool reset_rate = false;
 846
 847        /* VF Tx rate limit was not set */
 848        if (adapter->vf_rate_link_speed == 0)
 849                return;
 850
 851        actual_link_speed = ixgbe_link_mbps(adapter->link_speed);
 852        if (actual_link_speed != adapter->vf_rate_link_speed) {
 853                reset_rate = true;
 854                adapter->vf_rate_link_speed = 0;
 855                dev_info(&adapter->pdev->dev,
 856                         "Link speed has been changed. VF Transmit rate "
 857                         "is disabled\n");
 858        }
 859
 860        for (i = 0; i < adapter->num_vfs; i++) {
 861                if (reset_rate)
 862                        adapter->vfinfo[i].tx_rate = 0;
 863
 864                ixgbe_set_vf_rate_limit(&adapter->hw, i,
 865                                        adapter->vfinfo[i].tx_rate,
 866                                        actual_link_speed);
 867        }
 868}
 869
 870int ixgbe_ndo_set_vf_bw(struct net_device *netdev, int vf, int tx_rate)
 871{
 872        struct ixgbe_adapter *adapter = netdev_priv(netdev);
 873        struct ixgbe_hw *hw = &adapter->hw;
 874        int actual_link_speed;
 875
 876        actual_link_speed = ixgbe_link_mbps(adapter->link_speed);
 877        if ((vf >= adapter->num_vfs) || (!adapter->link_up) ||
 878            (tx_rate > actual_link_speed) || (actual_link_speed != 10000) ||
 879            ((tx_rate != 0) && (tx_rate <= 10)))
 880            /* rate limit cannot be set to 10Mb or less in 10Gb adapters */
 881                return -EINVAL;
 882
 883        adapter->vf_rate_link_speed = actual_link_speed;
 884        adapter->vfinfo[vf].tx_rate = (u16)tx_rate;
 885        ixgbe_set_vf_rate_limit(hw, vf, tx_rate, actual_link_speed);
 886
 887        return 0;
 888}
 889
 890int ixgbe_ndo_set_vf_spoofchk(struct net_device *netdev, int vf, bool setting)
 891{
 892        struct ixgbe_adapter *adapter = netdev_priv(netdev);
 893        int vf_target_reg = vf >> 3;
 894        int vf_target_shift = vf % 8;
 895        struct ixgbe_hw *hw = &adapter->hw;
 896        u32 regval;
 897
 898        adapter->vfinfo[vf].spoofchk_enabled = setting;
 899
 900        regval = IXGBE_READ_REG(hw, IXGBE_PFVFSPOOF(vf_target_reg));
 901        regval &= ~(1 << vf_target_shift);
 902        regval |= (setting << vf_target_shift);
 903        IXGBE_WRITE_REG(hw, IXGBE_PFVFSPOOF(vf_target_reg), regval);
 904
 905        if (adapter->vfinfo[vf].vlan_count) {
 906                vf_target_shift += IXGBE_SPOOF_VLANAS_SHIFT;
 907                regval = IXGBE_READ_REG(hw, IXGBE_PFVFSPOOF(vf_target_reg));
 908                regval &= ~(1 << vf_target_shift);
 909                regval |= (setting << vf_target_shift);
 910                IXGBE_WRITE_REG(hw, IXGBE_PFVFSPOOF(vf_target_reg), regval);
 911        }
 912
 913        return 0;
 914}
 915
 916int ixgbe_ndo_get_vf_config(struct net_device *netdev,
 917                            int vf, struct ifla_vf_info *ivi)
 918{
 919        struct ixgbe_adapter *adapter = netdev_priv(netdev);
 920        if (vf >= adapter->num_vfs)
 921                return -EINVAL;
 922        ivi->vf = vf;
 923        memcpy(&ivi->mac, adapter->vfinfo[vf].vf_mac_addresses, ETH_ALEN);
 924        ivi->tx_rate = adapter->vfinfo[vf].tx_rate;
 925        ivi->vlan = adapter->vfinfo[vf].pf_vlan;
 926        ivi->qos = adapter->vfinfo[vf].pf_qos;
 927        ivi->spoofchk = adapter->vfinfo[vf].spoofchk_enabled;
 928        return 0;
 929}
 930
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.