1 /******************************************************************************* 2 3 Intel 10 Gigabit PCI Express Linux driver 4 Copyright(c) 1999 - 2015 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 Linux NICS <linux.nics@intel.com> 24 e1000-devel Mailing List <e1000-devel@lists.sourceforge.net> 25 Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497 26 27 *******************************************************************************/ 28 29 #include <linux/types.h> 30 #include <linux/module.h> 31 #include <linux/pci.h> 32 #include <linux/netdevice.h> 33 #include <linux/vmalloc.h> 34 #include <linux/string.h> 35 #include <linux/in.h> 36 #include <linux/ip.h> 37 #include <linux/tcp.h> 38 #include <linux/ipv6.h> 39 #include <linux/if_bridge.h> 40 #ifdef NETIF_F_HW_VLAN_CTAG_TX 41 #include <linux/if_vlan.h> 42 #endif 43 44 #include "ixgbe.h" 45 #include "ixgbe_type.h" 46 #include "ixgbe_sriov.h" 47 48 #ifdef CONFIG_PCI_IOV 49 static inline void ixgbe_alloc_vf_macvlans(struct ixgbe_adapter *adapter, 50 unsigned int num_vfs) 51 { 52 struct ixgbe_hw *hw = &adapter->hw; 53 struct vf_macvlans *mv_list; 54 int num_vf_macvlans, i; 55 56 num_vf_macvlans = hw->mac.num_rar_entries - 57 (IXGBE_MAX_PF_MACVLANS + 1 + num_vfs); 58 if (!num_vf_macvlans) 59 return; 60 61 mv_list = kcalloc(num_vf_macvlans, sizeof(struct vf_macvlans), 62 GFP_KERNEL); 63 if (mv_list) { 64 /* Initialize list of VF macvlans */ 65 INIT_LIST_HEAD(&adapter->vf_mvs.l); 66 for (i = 0; i < num_vf_macvlans; i++) { 67 mv_list[i].vf = -1; 68 mv_list[i].free = true; 69 list_add(&mv_list[i].l, &adapter->vf_mvs.l); 70 } 71 adapter->mv_list = mv_list; 72 } 73 } 74 75 static int __ixgbe_enable_sriov(struct ixgbe_adapter *adapter, 76 unsigned int num_vfs) 77 { 78 struct ixgbe_hw *hw = &adapter->hw; 79 int i; 80 81 adapter->flags |= IXGBE_FLAG_SRIOV_ENABLED; 82 83 /* Enable VMDq flag so device will be set in VM mode */ 84 adapter->flags |= IXGBE_FLAG_VMDQ_ENABLED; 85 if (!adapter->ring_feature[RING_F_VMDQ].limit) 86 adapter->ring_feature[RING_F_VMDQ].limit = 1; 87 88 /* Allocate memory for per VF control structures */ 89 adapter->vfinfo = kcalloc(num_vfs, sizeof(struct vf_data_storage), 90 GFP_KERNEL); 91 if (!adapter->vfinfo) 92 return -ENOMEM; 93 94 adapter->num_vfs = num_vfs; 95 96 ixgbe_alloc_vf_macvlans(adapter, num_vfs); 97 adapter->ring_feature[RING_F_VMDQ].offset = num_vfs; 98 99 /* Initialize default switching mode VEB */ 100 IXGBE_WRITE_REG(hw, IXGBE_PFDTXGSWC, IXGBE_PFDTXGSWC_VT_LBEN); 101 adapter->bridge_mode = BRIDGE_MODE_VEB; 102 103 /* limit trafffic classes based on VFs enabled */ 104 if ((adapter->hw.mac.type == ixgbe_mac_82599EB) && (num_vfs < 16)) { 105 adapter->dcb_cfg.num_tcs.pg_tcs = MAX_TRAFFIC_CLASS; 106 adapter->dcb_cfg.num_tcs.pfc_tcs = MAX_TRAFFIC_CLASS; 107 } else if (num_vfs < 32) { 108 adapter->dcb_cfg.num_tcs.pg_tcs = 4; 109 adapter->dcb_cfg.num_tcs.pfc_tcs = 4; 110 } else { 111 adapter->dcb_cfg.num_tcs.pg_tcs = 1; 112 adapter->dcb_cfg.num_tcs.pfc_tcs = 1; 113 } 114 115 /* Disable RSC when in SR-IOV mode */ 116 adapter->flags2 &= ~(IXGBE_FLAG2_RSC_CAPABLE | 117 IXGBE_FLAG2_RSC_ENABLED); 118 119 for (i = 0; i < num_vfs; i++) { 120 /* enable spoof checking for all VFs */ 121 adapter->vfinfo[i].spoofchk_enabled = true; 122 123 /* We support VF RSS querying only for 82599 and x540 124 * devices at the moment. These devices share RSS 125 * indirection table and RSS hash key with PF therefore 126 * we want to disable the querying by default. 127 */ 128 adapter->vfinfo[i].rss_query_enabled = 0; 129 130 /* Untrust all VFs */ 131 adapter->vfinfo[i].trusted = false; 132 133 /* set the default xcast mode */ 134 adapter->vfinfo[i].xcast_mode = IXGBEVF_XCAST_MODE_NONE; 135 } 136 137 e_info(probe, "SR-IOV enabled with %d VFs\n", num_vfs); 138 return 0; 139 } 140 141 /** 142 * ixgbe_get_vfs - Find and take references to all vf devices 143 * @adapter: Pointer to adapter struct 144 */ 145 static void ixgbe_get_vfs(struct ixgbe_adapter *adapter) 146 { 147 struct pci_dev *pdev = adapter->pdev; 148 u16 vendor = pdev->vendor; 149 struct pci_dev *vfdev; 150 int vf = 0; 151 u16 vf_id; 152 int pos; 153 154 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV); 155 if (!pos) 156 return; 157 pci_read_config_word(pdev, pos + PCI_SRIOV_VF_DID, &vf_id); 158 159 vfdev = pci_get_device(vendor, vf_id, NULL); 160 for (; vfdev; vfdev = pci_get_device(vendor, vf_id, vfdev)) { 161 if (!vfdev->is_virtfn) 162 continue; 163 if (vfdev->physfn != pdev) 164 continue; 165 if (vf >= adapter->num_vfs) 166 continue; 167 pci_dev_get(vfdev); 168 adapter->vfinfo[vf].vfdev = vfdev; 169 ++vf; 170 } 171 } 172 173 /* Note this function is called when the user wants to enable SR-IOV 174 * VFs using the now deprecated module parameter 175 */ 176 void ixgbe_enable_sriov(struct ixgbe_adapter *adapter, unsigned int max_vfs) 177 { 178 int pre_existing_vfs = 0; 179 unsigned int num_vfs; 180 181 pre_existing_vfs = pci_num_vf(adapter->pdev); 182 if (!pre_existing_vfs && !max_vfs) 183 return; 184 185 /* If there are pre-existing VFs then we have to force 186 * use of that many - over ride any module parameter value. 187 * This may result from the user unloading the PF driver 188 * while VFs were assigned to guest VMs or because the VFs 189 * have been created via the new PCI SR-IOV sysfs interface. 190 */ 191 if (pre_existing_vfs) { 192 num_vfs = pre_existing_vfs; 193 dev_warn(&adapter->pdev->dev, 194 "Virtual Functions already enabled for this device - Please reload all VF drivers to avoid spoofed packet errors\n"); 195 } else { 196 int err; 197 /* 198 * The 82599 supports up to 64 VFs per physical function 199 * but this implementation limits allocation to 63 so that 200 * basic networking resources are still available to the 201 * physical function. If the user requests greater than 202 * 63 VFs then it is an error - reset to default of zero. 203 */ 204 num_vfs = min_t(unsigned int, max_vfs, IXGBE_MAX_VFS_DRV_LIMIT); 205 206 err = pci_enable_sriov(adapter->pdev, num_vfs); 207 if (err) { 208 e_err(probe, "Failed to enable PCI sriov: %d\n", err); 209 return; 210 } 211 } 212 213 if (!__ixgbe_enable_sriov(adapter, num_vfs)) { 214 ixgbe_get_vfs(adapter); 215 return; 216 } 217 218 /* If we have gotten to this point then there is no memory available 219 * to manage the VF devices - print message and bail. 220 */ 221 e_err(probe, "Unable to allocate memory for VF Data Storage - " 222 "SRIOV disabled\n"); 223 ixgbe_disable_sriov(adapter); 224 } 225 226 #endif /* #ifdef CONFIG_PCI_IOV */ 227 int ixgbe_disable_sriov(struct ixgbe_adapter *adapter) 228 { 229 unsigned int num_vfs = adapter->num_vfs, vf; 230 struct ixgbe_hw *hw = &adapter->hw; 231 u32 gpie; 232 u32 vmdctl; 233 int rss; 234 235 /* set num VFs to 0 to prevent access to vfinfo */ 236 adapter->num_vfs = 0; 237 238 /* put the reference to all of the vf devices */ 239 for (vf = 0; vf < num_vfs; ++vf) { 240 struct pci_dev *vfdev = adapter->vfinfo[vf].vfdev; 241 242 if (!vfdev) 243 continue; 244 adapter->vfinfo[vf].vfdev = NULL; 245 pci_dev_put(vfdev); 246 } 247 248 /* free VF control structures */ 249 kfree(adapter->vfinfo); 250 adapter->vfinfo = NULL; 251 252 /* free macvlan list */ 253 kfree(adapter->mv_list); 254 adapter->mv_list = NULL; 255 256 /* if SR-IOV is already disabled then there is nothing to do */ 257 if (!(adapter->flags & IXGBE_FLAG_SRIOV_ENABLED)) 258 return 0; 259 260 #ifdef CONFIG_PCI_IOV 261 /* 262 * If our VFs are assigned we cannot shut down SR-IOV 263 * without causing issues, so just leave the hardware 264 * available but disabled 265 */ 266 if (pci_vfs_assigned(adapter->pdev)) { 267 e_dev_warn("Unloading driver while VFs are assigned - VFs will not be deallocated\n"); 268 return -EPERM; 269 } 270 /* disable iov and allow time for transactions to clear */ 271 pci_disable_sriov(adapter->pdev); 272 #endif 273 274 /* turn off device IOV mode */ 275 IXGBE_WRITE_REG(hw, IXGBE_GCR_EXT, 0); 276 gpie = IXGBE_READ_REG(hw, IXGBE_GPIE); 277 gpie &= ~IXGBE_GPIE_VTMODE_MASK; 278 IXGBE_WRITE_REG(hw, IXGBE_GPIE, gpie); 279 280 /* set default pool back to 0 */ 281 vmdctl = IXGBE_READ_REG(hw, IXGBE_VT_CTL); 282 vmdctl &= ~IXGBE_VT_CTL_POOL_MASK; 283 IXGBE_WRITE_REG(hw, IXGBE_VT_CTL, vmdctl); 284 IXGBE_WRITE_FLUSH(hw); 285 286 /* Disable VMDq flag so device will be set in VM mode */ 287 if (adapter->ring_feature[RING_F_VMDQ].limit == 1) { 288 adapter->flags &= ~IXGBE_FLAG_VMDQ_ENABLED; 289 adapter->flags &= ~IXGBE_FLAG_SRIOV_ENABLED; 290 rss = min_t(int, ixgbe_max_rss_indices(adapter), 291 num_online_cpus()); 292 } else { 293 rss = min_t(int, IXGBE_MAX_L2A_QUEUES, num_online_cpus()); 294 } 295 296 adapter->ring_feature[RING_F_VMDQ].offset = 0; 297 adapter->ring_feature[RING_F_RSS].limit = rss; 298 299 /* take a breather then clean up driver data */ 300 msleep(100); 301 return 0; 302 } 303 304 static int ixgbe_pci_sriov_enable(struct pci_dev *dev, int num_vfs) 305 { 306 #ifdef CONFIG_PCI_IOV 307 struct ixgbe_adapter *adapter = pci_get_drvdata(dev); 308 int err = 0; 309 u8 num_tc; 310 int i; 311 int pre_existing_vfs = pci_num_vf(dev); 312 313 if (pre_existing_vfs && pre_existing_vfs != num_vfs) 314 err = ixgbe_disable_sriov(adapter); 315 else if (pre_existing_vfs && pre_existing_vfs == num_vfs) 316 return num_vfs; 317 318 if (err) 319 return err; 320 321 /* While the SR-IOV capability structure reports total VFs to be 64, 322 * we limit the actual number allocated as below based on two factors. 323 * Num_TCs MAX_VFs 324 * 1 63 325 * <=4 31 326 * >4 15 327 * First, we reserve some transmit/receive resources for the PF. 328 * Second, VMDQ also uses the same pools that SR-IOV does. We need to 329 * account for this, so that we don't accidentally allocate more VFs 330 * than we have available pools. The PCI bus driver already checks for 331 * other values out of range. 332 */ 333 num_tc = netdev_get_num_tc(adapter->netdev); 334 335 if (num_tc > 4) { 336 if ((num_vfs + adapter->num_rx_pools) > IXGBE_MAX_VFS_8TC) { 337 e_dev_err("Currently the device is configured with %d TCs, Creating more than %d VFs is not allowed\n", num_tc, IXGBE_MAX_VFS_8TC); 338 return -EPERM; 339 } 340 } else if ((num_tc > 1) && (num_tc <= 4)) { 341 if ((num_vfs + adapter->num_rx_pools) > IXGBE_MAX_VFS_4TC) { 342 e_dev_err("Currently the device is configured with %d TCs, Creating more than %d VFs is not allowed\n", num_tc, IXGBE_MAX_VFS_4TC); 343 return -EPERM; 344 } 345 } else { 346 if ((num_vfs + adapter->num_rx_pools) > IXGBE_MAX_VFS_1TC) { 347 e_dev_err("Currently the device is configured with %d TCs, Creating more than %d VFs is not allowed\n", num_tc, IXGBE_MAX_VFS_1TC); 348 return -EPERM; 349 } 350 } 351 352 err = __ixgbe_enable_sriov(adapter, num_vfs); 353 if (err) 354 return err; 355 356 for (i = 0; i < num_vfs; i++) 357 ixgbe_vf_configuration(dev, (i | 0x10000000)); 358 359 /* reset before enabling SRIOV to avoid mailbox issues */ 360 ixgbe_sriov_reinit(adapter); 361 362 err = pci_enable_sriov(dev, num_vfs); 363 if (err) { 364 e_dev_warn("Failed to enable PCI sriov: %d\n", err); 365 return err; 366 } 367 ixgbe_get_vfs(adapter); 368 369 return num_vfs; 370 #else 371 return 0; 372 #endif 373 } 374 375 static int ixgbe_pci_sriov_disable(struct pci_dev *dev) 376 { 377 struct ixgbe_adapter *adapter = pci_get_drvdata(dev); 378 int err; 379 #ifdef CONFIG_PCI_IOV 380 u32 current_flags = adapter->flags; 381 #endif 382 383 err = ixgbe_disable_sriov(adapter); 384 385 /* Only reinit if no error and state changed */ 386 #ifdef CONFIG_PCI_IOV 387 if (!err && current_flags != adapter->flags) 388 ixgbe_sriov_reinit(adapter); 389 #endif 390 391 return err; 392 } 393 394 int ixgbe_pci_sriov_configure(struct pci_dev *dev, int num_vfs) 395 { 396 if (num_vfs == 0) 397 return ixgbe_pci_sriov_disable(dev); 398 else 399 return ixgbe_pci_sriov_enable(dev, num_vfs); 400 } 401 402 static int ixgbe_set_vf_multicasts(struct ixgbe_adapter *adapter, 403 u32 *msgbuf, u32 vf) 404 { 405 int entries = (msgbuf[0] & IXGBE_VT_MSGINFO_MASK) 406 >> IXGBE_VT_MSGINFO_SHIFT; 407 u16 *hash_list = (u16 *)&msgbuf[1]; 408 struct vf_data_storage *vfinfo = &adapter->vfinfo[vf]; 409 struct ixgbe_hw *hw = &adapter->hw; 410 int i; 411 u32 vector_bit; 412 u32 vector_reg; 413 u32 mta_reg; 414 u32 vmolr = IXGBE_READ_REG(hw, IXGBE_VMOLR(vf)); 415 416 /* only so many hash values supported */ 417 entries = min(entries, IXGBE_MAX_VF_MC_ENTRIES); 418 419 /* 420 * salt away the number of multi cast addresses assigned 421 * to this VF for later use to restore when the PF multi cast 422 * list changes 423 */ 424 vfinfo->num_vf_mc_hashes = entries; 425 426 /* 427 * VFs are limited to using the MTA hash table for their multicast 428 * addresses 429 */ 430 for (i = 0; i < entries; i++) { 431 vfinfo->vf_mc_hashes[i] = hash_list[i]; 432 } 433 434 for (i = 0; i < vfinfo->num_vf_mc_hashes; i++) { 435 vector_reg = (vfinfo->vf_mc_hashes[i] >> 5) & 0x7F; 436 vector_bit = vfinfo->vf_mc_hashes[i] & 0x1F; 437 mta_reg = IXGBE_READ_REG(hw, IXGBE_MTA(vector_reg)); 438 mta_reg |= BIT(vector_bit); 439 IXGBE_WRITE_REG(hw, IXGBE_MTA(vector_reg), mta_reg); 440 } 441 vmolr |= IXGBE_VMOLR_ROMPE; 442 IXGBE_WRITE_REG(hw, IXGBE_VMOLR(vf), vmolr); 443 444 return 0; 445 } 446 447 #ifdef CONFIG_PCI_IOV 448 void ixgbe_restore_vf_multicasts(struct ixgbe_adapter *adapter) 449 { 450 struct ixgbe_hw *hw = &adapter->hw; 451 struct vf_data_storage *vfinfo; 452 int i, j; 453 u32 vector_bit; 454 u32 vector_reg; 455 u32 mta_reg; 456 457 for (i = 0; i < adapter->num_vfs; i++) { 458 u32 vmolr = IXGBE_READ_REG(hw, IXGBE_VMOLR(i)); 459 vfinfo = &adapter->vfinfo[i]; 460 for (j = 0; j < vfinfo->num_vf_mc_hashes; j++) { 461 hw->addr_ctrl.mta_in_use++; 462 vector_reg = (vfinfo->vf_mc_hashes[j] >> 5) & 0x7F; 463 vector_bit = vfinfo->vf_mc_hashes[j] & 0x1F; 464 mta_reg = IXGBE_READ_REG(hw, IXGBE_MTA(vector_reg)); 465 mta_reg |= BIT(vector_bit); 466 IXGBE_WRITE_REG(hw, IXGBE_MTA(vector_reg), mta_reg); 467 } 468 469 if (vfinfo->num_vf_mc_hashes) 470 vmolr |= IXGBE_VMOLR_ROMPE; 471 else 472 vmolr &= ~IXGBE_VMOLR_ROMPE; 473 IXGBE_WRITE_REG(hw, IXGBE_VMOLR(i), vmolr); 474 } 475 476 /* Restore any VF macvlans */ 477 ixgbe_full_sync_mac_table(adapter); 478 } 479 #endif 480 481 static int ixgbe_set_vf_vlan(struct ixgbe_adapter *adapter, int add, int vid, 482 u32 vf) 483 { 484 struct ixgbe_hw *hw = &adapter->hw; 485 int err; 486 487 /* If VLAN overlaps with one the PF is currently monitoring make 488 * sure that we are able to allocate a VLVF entry. This may be 489 * redundant but it guarantees PF will maintain visibility to 490 * the VLAN. 491 */ 492 if (add && test_bit(vid, adapter->active_vlans)) { 493 err = hw->mac.ops.set_vfta(hw, vid, VMDQ_P(0), true, false); 494 if (err) 495 return err; 496 } 497 498 err = hw->mac.ops.set_vfta(hw, vid, vf, !!add, false); 499 500 if (add && !err) 501 return err; 502 503 /* If we failed to add the VF VLAN or we are removing the VF VLAN 504 * we may need to drop the PF pool bit in order to allow us to free 505 * up the VLVF resources. 506 */ 507 if (test_bit(vid, adapter->active_vlans) || 508 (adapter->flags2 & IXGBE_FLAG2_VLAN_PROMISC)) 509 ixgbe_update_pf_promisc_vlvf(adapter, vid); 510 511 return err; 512 } 513 514 static s32 ixgbe_set_vf_lpe(struct ixgbe_adapter *adapter, u32 *msgbuf, u32 vf) 515 { 516 struct ixgbe_hw *hw = &adapter->hw; 517 int max_frame = msgbuf[1]; 518 u32 max_frs; 519 520 /* 521 * For 82599EB we have to keep all PFs and VFs operating with 522 * the same max_frame value in order to avoid sending an oversize 523 * frame to a VF. In order to guarantee this is handled correctly 524 * for all cases we have several special exceptions to take into 525 * account before we can enable the VF for receive 526 */ 527 if (adapter->hw.mac.type == ixgbe_mac_82599EB) { 528 struct net_device *dev = adapter->netdev; 529 int pf_max_frame = dev->mtu + ETH_HLEN; 530 u32 reg_offset, vf_shift, vfre; 531 s32 err = 0; 532 533 #ifdef CONFIG_FCOE 534 if (dev->features & NETIF_F_FCOE_MTU) 535 pf_max_frame = max_t(int, pf_max_frame, 536 IXGBE_FCOE_JUMBO_FRAME_SIZE); 537 538 #endif /* CONFIG_FCOE */ 539 switch (adapter->vfinfo[vf].vf_api) { 540 case ixgbe_mbox_api_11: 541 case ixgbe_mbox_api_12: 542 case ixgbe_mbox_api_13: 543 /* Version 1.1 supports jumbo frames on VFs if PF has 544 * jumbo frames enabled which means legacy VFs are 545 * disabled 546 */ 547 if (pf_max_frame > ETH_FRAME_LEN) 548 break; 549 /* fall through */ 550 default: 551 /* If the PF or VF are running w/ jumbo frames enabled 552 * we need to shut down the VF Rx path as we cannot 553 * support jumbo frames on legacy VFs 554 */ 555 if ((pf_max_frame > ETH_FRAME_LEN) || 556 (max_frame > (ETH_FRAME_LEN + ETH_FCS_LEN))) 557 err = -EINVAL; 558 break; 559 } 560 561 /* determine VF receive enable location */ 562 vf_shift = vf % 32; 563 reg_offset = vf / 32; 564 565 /* enable or disable receive depending on error */ 566 vfre = IXGBE_READ_REG(hw, IXGBE_VFRE(reg_offset)); 567 if (err) 568 vfre &= ~BIT(vf_shift); 569 else 570 vfre |= BIT(vf_shift); 571 IXGBE_WRITE_REG(hw, IXGBE_VFRE(reg_offset), vfre); 572 573 if (err) { 574 e_err(drv, "VF max_frame %d out of range\n", max_frame); 575 return err; 576 } 577 } 578 579 /* MTU < 68 is an error and causes problems on some kernels */ 580 if (max_frame > IXGBE_MAX_JUMBO_FRAME_SIZE) { 581 e_err(drv, "VF max_frame %d out of range\n", max_frame); 582 return -EINVAL; 583 } 584 585 /* pull current max frame size from hardware */ 586 max_frs = IXGBE_READ_REG(hw, IXGBE_MAXFRS); 587 max_frs &= IXGBE_MHADD_MFS_MASK; 588 max_frs >>= IXGBE_MHADD_MFS_SHIFT; 589 590 if (max_frs < max_frame) { 591 max_frs = max_frame << IXGBE_MHADD_MFS_SHIFT; 592 IXGBE_WRITE_REG(hw, IXGBE_MAXFRS, max_frs); 593 } 594 595 e_info(hw, "VF requests change max MTU to %d\n", max_frame); 596 597 return 0; 598 } 599 600 static void ixgbe_set_vmolr(struct ixgbe_hw *hw, u32 vf, bool aupe) 601 { 602 u32 vmolr = IXGBE_READ_REG(hw, IXGBE_VMOLR(vf)); 603 vmolr |= IXGBE_VMOLR_BAM; 604 if (aupe) 605 vmolr |= IXGBE_VMOLR_AUPE; 606 else 607 vmolr &= ~IXGBE_VMOLR_AUPE; 608 IXGBE_WRITE_REG(hw, IXGBE_VMOLR(vf), vmolr); 609 } 610 611 static void ixgbe_clear_vmvir(struct ixgbe_adapter *adapter, u32 vf) 612 { 613 struct ixgbe_hw *hw = &adapter->hw; 614 615 IXGBE_WRITE_REG(hw, IXGBE_VMVIR(vf), 0); 616 } 617 618 static void ixgbe_clear_vf_vlans(struct ixgbe_adapter *adapter, u32 vf) 619 { 620 struct ixgbe_hw *hw = &adapter->hw; 621 u32 vlvfb_mask, pool_mask, i; 622 623 /* create mask for VF and other pools */ 624 pool_mask = ~BIT(VMDQ_P(0) % 32); 625 vlvfb_mask = BIT(vf % 32); 626 627 /* post increment loop, covers VLVF_ENTRIES - 1 to 0 */ 628 for (i = IXGBE_VLVF_ENTRIES; i--;) { 629 u32 bits[2], vlvfb, vid, vfta, vlvf; 630 u32 word = i * 2 + vf / 32; 631 u32 mask; 632 633 vlvfb = IXGBE_READ_REG(hw, IXGBE_VLVFB(word)); 634 635 /* if our bit isn't set we can skip it */ 636 if (!(vlvfb & vlvfb_mask)) 637 continue; 638 639 /* clear our bit from vlvfb */ 640 vlvfb ^= vlvfb_mask; 641 642 /* create 64b mask to chedk to see if we should clear VLVF */ 643 bits[word % 2] = vlvfb; 644 bits[~word % 2] = IXGBE_READ_REG(hw, IXGBE_VLVFB(word ^ 1)); 645 646 /* if other pools are present, just remove ourselves */ 647 if (bits[(VMDQ_P(0) / 32) ^ 1] || 648 (bits[VMDQ_P(0) / 32] & pool_mask)) 649 goto update_vlvfb; 650 651 /* if PF is present, leave VFTA */ 652 if (bits[0] || bits[1]) 653 goto update_vlvf; 654 655 /* if we cannot determine VLAN just remove ourselves */ 656 vlvf = IXGBE_READ_REG(hw, IXGBE_VLVF(i)); 657 if (!vlvf) 658 goto update_vlvfb; 659 660 vid = vlvf & VLAN_VID_MASK; 661 mask = BIT(vid % 32); 662 663 /* clear bit from VFTA */ 664 vfta = IXGBE_READ_REG(hw, IXGBE_VFTA(vid / 32)); 665 if (vfta & mask) 666 IXGBE_WRITE_REG(hw, IXGBE_VFTA(vid / 32), vfta ^ mask); 667 update_vlvf: 668 /* clear POOL selection enable */ 669 IXGBE_WRITE_REG(hw, IXGBE_VLVF(i), 0); 670 671 if (!(adapter->flags2 & IXGBE_FLAG2_VLAN_PROMISC)) 672 vlvfb = 0; 673 update_vlvfb: 674 /* clear pool bits */ 675 IXGBE_WRITE_REG(hw, IXGBE_VLVFB(word), vlvfb); 676 } 677 } 678 679 static int ixgbe_set_vf_macvlan(struct ixgbe_adapter *adapter, 680 int vf, int index, unsigned char *mac_addr) 681 { 682 struct list_head *pos; 683 struct vf_macvlans *entry; 684 685 if (index <= 1) { 686 list_for_each(pos, &adapter->vf_mvs.l) { 687 entry = list_entry(pos, struct vf_macvlans, l); 688 if (entry->vf == vf) { 689 entry->vf = -1; 690 entry->free = true; 691 entry->is_macvlan = false; 692 ixgbe_del_mac_filter(adapter, 693 entry->vf_macvlan, vf); 694 } 695 } 696 } 697 698 /* 699 * If index was zero then we were asked to clear the uc list 700 * for the VF. We're done. 701 */ 702 if (!index) 703 return 0; 704 705 entry = NULL; 706 707 list_for_each(pos, &adapter->vf_mvs.l) { 708 entry = list_entry(pos, struct vf_macvlans, l); 709 if (entry->free) 710 break; 711 } 712 713 /* 714 * If we traversed the entire list and didn't find a free entry 715 * then we're out of space on the RAR table. Also entry may 716 * be NULL because the original memory allocation for the list 717 * failed, which is not fatal but does mean we can't support 718 * VF requests for MACVLAN because we couldn't allocate 719 * memory for the list management required. 720 */ 721 if (!entry || !entry->free) 722 return -ENOSPC; 723 724 entry->free = false; 725 entry->is_macvlan = true; 726 entry->vf = vf; 727 memcpy(entry->vf_macvlan, mac_addr, ETH_ALEN); 728 729 ixgbe_add_mac_filter(adapter, mac_addr, vf); 730 731 return 0; 732 } 733 734 static inline void ixgbe_vf_reset_event(struct ixgbe_adapter *adapter, u32 vf) 735 { 736 struct ixgbe_hw *hw = &adapter->hw; 737 struct vf_data_storage *vfinfo = &adapter->vfinfo[vf]; 738 u8 num_tcs = netdev_get_num_tc(adapter->netdev); 739 740 /* remove VLAN filters beloning to this VF */ 741 ixgbe_clear_vf_vlans(adapter, vf); 742 743 /* add back PF assigned VLAN or VLAN 0 */ 744 ixgbe_set_vf_vlan(adapter, true, vfinfo->pf_vlan, vf); 745 746 /* reset offloads to defaults */ 747 ixgbe_set_vmolr(hw, vf, !vfinfo->pf_vlan); 748 749 /* set outgoing tags for VFs */ 750 if (!vfinfo->pf_vlan && !vfinfo->pf_qos && !num_tcs) { 751 ixgbe_clear_vmvir(adapter, vf); 752 } else { 753 if (vfinfo->pf_qos || !num_tcs) 754 ixgbe_set_vmvir(adapter, vfinfo->pf_vlan, 755 vfinfo->pf_qos, vf); 756 else 757 ixgbe_set_vmvir(adapter, vfinfo->pf_vlan, 758 adapter->default_up, vf); 759 760 if (vfinfo->spoofchk_enabled) 761 hw->mac.ops.set_vlan_anti_spoofing(hw, true, vf); 762 } 763 764 /* reset multicast table array for vf */ 765 adapter->vfinfo[vf].num_vf_mc_hashes = 0; 766 767 /* Flush and reset the mta with the new values */ 768 ixgbe_set_rx_mode(adapter->netdev); 769 770 ixgbe_del_mac_filter(adapter, adapter->vfinfo[vf].vf_mac_addresses, vf); 771 ixgbe_set_vf_macvlan(adapter, vf, 0, NULL); 772 773 /* reset VF api back to unknown */ 774 adapter->vfinfo[vf].vf_api = ixgbe_mbox_api_10; 775 } 776 777 static int ixgbe_set_vf_mac(struct ixgbe_adapter *adapter, 778 int vf, unsigned char *mac_addr) 779 { 780 s32 retval; 781 782 ixgbe_del_mac_filter(adapter, adapter->vfinfo[vf].vf_mac_addresses, vf); 783 retval = ixgbe_add_mac_filter(adapter, mac_addr, vf); 784 if (retval >= 0) 785 memcpy(adapter->vfinfo[vf].vf_mac_addresses, mac_addr, 786 ETH_ALEN); 787 else 788 memset(adapter->vfinfo[vf].vf_mac_addresses, 0, ETH_ALEN); 789 790 return retval; 791 } 792 793 int ixgbe_vf_configuration(struct pci_dev *pdev, unsigned int event_mask) 794 { 795 struct ixgbe_adapter *adapter = pci_get_drvdata(pdev); 796 unsigned int vfn = (event_mask & 0x3f); 797 798 bool enable = ((event_mask & 0x10000000U) != 0); 799 800 if (enable) 801 eth_zero_addr(adapter->vfinfo[vfn].vf_mac_addresses); 802 803 return 0; 804 } 805 806 static inline void ixgbe_write_qde(struct ixgbe_adapter *adapter, u32 vf, 807 u32 qde) 808 { 809 struct ixgbe_hw *hw = &adapter->hw; 810 struct ixgbe_ring_feature *vmdq = &adapter->ring_feature[RING_F_VMDQ]; 811 u32 q_per_pool = __ALIGN_MASK(1, ~vmdq->mask); 812 int i; 813 814 for (i = vf * q_per_pool; i < ((vf + 1) * q_per_pool); i++) { 815 u32 reg; 816 817 /* flush previous write */ 818 IXGBE_WRITE_FLUSH(hw); 819 820 /* indicate to hardware that we want to set drop enable */ 821 reg = IXGBE_QDE_WRITE | qde; 822 reg |= i << IXGBE_QDE_IDX_SHIFT; 823 IXGBE_WRITE_REG(hw, IXGBE_QDE, reg); 824 } 825 } 826 827 static int ixgbe_vf_reset_msg(struct ixgbe_adapter *adapter, u32 vf) 828 { 829 struct ixgbe_ring_feature *vmdq = &adapter->ring_feature[RING_F_VMDQ]; 830 struct ixgbe_hw *hw = &adapter->hw; 831 unsigned char *vf_mac = adapter->vfinfo[vf].vf_mac_addresses; 832 u32 reg, reg_offset, vf_shift; 833 u32 msgbuf[4] = {0, 0, 0, 0}; 834 u8 *addr = (u8 *)(&msgbuf[1]); 835 u32 q_per_pool = __ALIGN_MASK(1, ~vmdq->mask); 836 int i; 837 838 e_info(probe, "VF Reset msg received from vf %d\n", vf); 839 840 /* reset the filters for the device */ 841 ixgbe_vf_reset_event(adapter, vf); 842 843 /* set vf mac address */ 844 if (!is_zero_ether_addr(vf_mac)) 845 ixgbe_set_vf_mac(adapter, vf, vf_mac); 846 847 vf_shift = vf % 32; 848 reg_offset = vf / 32; 849 850 /* enable transmit for vf */ 851 reg = IXGBE_READ_REG(hw, IXGBE_VFTE(reg_offset)); 852 reg |= BIT(vf_shift); 853 IXGBE_WRITE_REG(hw, IXGBE_VFTE(reg_offset), reg); 854 855 /* force drop enable for all VF Rx queues */ 856 ixgbe_write_qde(adapter, vf, IXGBE_QDE_ENABLE); 857 858 /* enable receive for vf */ 859 reg = IXGBE_READ_REG(hw, IXGBE_VFRE(reg_offset)); 860 reg |= BIT(vf_shift); 861 /* 862 * The 82599 cannot support a mix of jumbo and non-jumbo PF/VFs. 863 * For more info take a look at ixgbe_set_vf_lpe 864 */ 865 if (adapter->hw.mac.type == ixgbe_mac_82599EB) { 866 struct net_device *dev = adapter->netdev; 867 int pf_max_frame = dev->mtu + ETH_HLEN; 868 869 #ifdef CONFIG_FCOE 870 if (dev->features & NETIF_F_FCOE_MTU) 871 pf_max_frame = max_t(int, pf_max_frame, 872 IXGBE_FCOE_JUMBO_FRAME_SIZE); 873 874 #endif /* CONFIG_FCOE */ 875 if (pf_max_frame > ETH_FRAME_LEN) 876 reg &= ~BIT(vf_shift); 877 } 878 IXGBE_WRITE_REG(hw, IXGBE_VFRE(reg_offset), reg); 879 880 /* enable VF mailbox for further messages */ 881 adapter->vfinfo[vf].clear_to_send = true; 882 883 /* Enable counting of spoofed packets in the SSVPC register */ 884 reg = IXGBE_READ_REG(hw, IXGBE_VMECM(reg_offset)); 885 reg |= BIT(vf_shift); 886 IXGBE_WRITE_REG(hw, IXGBE_VMECM(reg_offset), reg); 887 888 /* 889 * Reset the VFs TDWBAL and TDWBAH registers 890 * which are not cleared by an FLR 891 */ 892 for (i = 0; i < q_per_pool; i++) { 893 IXGBE_WRITE_REG(hw, IXGBE_PVFTDWBAHn(q_per_pool, vf, i), 0); 894 IXGBE_WRITE_REG(hw, IXGBE_PVFTDWBALn(q_per_pool, vf, i), 0); 895 } 896 897 /* reply to reset with ack and vf mac address */ 898 msgbuf[0] = IXGBE_VF_RESET; 899 if (!is_zero_ether_addr(vf_mac)) { 900 msgbuf[0] |= IXGBE_VT_MSGTYPE_ACK; 901 memcpy(addr, vf_mac, ETH_ALEN); 902 } else { 903 msgbuf[0] |= IXGBE_VT_MSGTYPE_NACK; 904 dev_warn(&adapter->pdev->dev, 905 "VF %d has no MAC address assigned, you may have to assign one manually\n", 906 vf); 907 } 908 909 /* 910 * Piggyback the multicast filter type so VF can compute the 911 * correct vectors 912 */ 913 msgbuf[3] = hw->mac.mc_filter_type; 914 ixgbe_write_mbx(hw, msgbuf, IXGBE_VF_PERMADDR_MSG_LEN, vf); 915 916 return 0; 917 } 918 919 static int ixgbe_set_vf_mac_addr(struct ixgbe_adapter *adapter, 920 u32 *msgbuf, u32 vf) 921 { 922 u8 *new_mac = ((u8 *)(&msgbuf[1])); 923 924 if (!is_valid_ether_addr(new_mac)) { 925 e_warn(drv, "VF %d attempted to set invalid mac\n", vf); 926 return -1; 927 } 928 929 if (adapter->vfinfo[vf].pf_set_mac && !adapter->vfinfo[vf].trusted && 930 !ether_addr_equal(adapter->vfinfo[vf].vf_mac_addresses, new_mac)) { 931 e_warn(drv, 932 "VF %d attempted to override administratively set MAC address\n" 933 "Reload the VF driver to resume operations\n", 934 vf); 935 return -1; 936 } 937 938 return ixgbe_set_vf_mac(adapter, vf, new_mac) < 0; 939 } 940 941 static int ixgbe_set_vf_vlan_msg(struct ixgbe_adapter *adapter, 942 u32 *msgbuf, u32 vf) 943 { 944 u32 add = (msgbuf[0] & IXGBE_VT_MSGINFO_MASK) >> IXGBE_VT_MSGINFO_SHIFT; 945 u32 vid = (msgbuf[1] & IXGBE_VLVF_VLANID_MASK); 946 u8 tcs = netdev_get_num_tc(adapter->netdev); 947 948 if (adapter->vfinfo[vf].pf_vlan || tcs) { 949 e_warn(drv, 950 "VF %d attempted to override administratively set VLAN configuration\n" 951 "Reload the VF driver to resume operations\n", 952 vf); 953 return -1; 954 } 955 956 /* VLAN 0 is a special case, don't allow it to be removed */ 957 if (!vid && !add) 958 return 0; 959 960 return ixgbe_set_vf_vlan(adapter, add, vid, vf); 961 } 962 963 static int ixgbe_set_vf_macvlan_msg(struct ixgbe_adapter *adapter, 964 u32 *msgbuf, u32 vf) 965 { 966 u8 *new_mac = ((u8 *)(&msgbuf[1])); 967 int index = (msgbuf[0] & IXGBE_VT_MSGINFO_MASK) >> 968 IXGBE_VT_MSGINFO_SHIFT; 969 int err; 970 971 if (adapter->vfinfo[vf].pf_set_mac && !adapter->vfinfo[vf].trusted && 972 index > 0) { 973 e_warn(drv, 974 "VF %d requested MACVLAN filter but is administratively denied\n", 975 vf); 976 return -1; 977 } 978 979 /* An non-zero index indicates the VF is setting a filter */ 980 if (index) { 981 if (!is_valid_ether_addr(new_mac)) { 982 e_warn(drv, "VF %d attempted to set invalid mac\n", vf); 983 return -1; 984 } 985 986 /* 987 * If the VF is allowed to set MAC filters then turn off 988 * anti-spoofing to avoid false positives. 989 */ 990 if (adapter->vfinfo[vf].spoofchk_enabled) { 991 struct ixgbe_hw *hw = &adapter->hw; 992 993 hw->mac.ops.set_mac_anti_spoofing(hw, false, vf); 994 hw->mac.ops.set_vlan_anti_spoofing(hw, false, vf); 995 } 996 } 997 998 err = ixgbe_set_vf_macvlan(adapter, vf, index, new_mac); 999 if (err == -ENOSPC) 1000 e_warn(drv, 1001 "VF %d has requested a MACVLAN filter but there is no space for it\n", 1002 vf); 1003 1004 return err < 0; 1005 } 1006 1007 static int ixgbe_negotiate_vf_api(struct ixgbe_adapter *adapter, 1008 u32 *msgbuf, u32 vf) 1009 { 1010 int api = msgbuf[1]; 1011 1012 switch (api) { 1013 case ixgbe_mbox_api_10: 1014 case ixgbe_mbox_api_11: 1015 case ixgbe_mbox_api_12: 1016 case ixgbe_mbox_api_13: 1017 adapter->vfinfo[vf].vf_api = api; 1018 return 0; 1019 default: 1020 break; 1021 } 1022 1023 e_info(drv, "VF %d requested invalid api version %u\n", vf, api); 1024 1025 return -1; 1026 } 1027 1028 static int ixgbe_get_vf_queues(struct ixgbe_adapter *adapter, 1029 u32 *msgbuf, u32 vf) 1030 { 1031 struct net_device *dev = adapter->netdev; 1032 struct ixgbe_ring_feature *vmdq = &adapter->ring_feature[RING_F_VMDQ]; 1033 unsigned int default_tc = 0; 1034 u8 num_tcs = netdev_get_num_tc(dev); 1035 1036 /* verify the PF is supporting the correct APIs */ 1037 switch (adapter->vfinfo[vf].vf_api) { 1038 case ixgbe_mbox_api_20: 1039 case ixgbe_mbox_api_11: 1040 case ixgbe_mbox_api_12: 1041 case ixgbe_mbox_api_13: 1042 break; 1043 default: 1044 return -1; 1045 } 1046 1047 /* only allow 1 Tx queue for bandwidth limiting */ 1048 msgbuf[IXGBE_VF_TX_QUEUES] = __ALIGN_MASK(1, ~vmdq->mask); 1049 msgbuf[IXGBE_VF_RX_QUEUES] = __ALIGN_MASK(1, ~vmdq->mask); 1050 1051 /* if TCs > 1 determine which TC belongs to default user priority */ 1052 if (num_tcs > 1) 1053 default_tc = netdev_get_prio_tc_map(dev, adapter->default_up); 1054 1055 /* notify VF of need for VLAN tag stripping, and correct queue */ 1056 if (num_tcs) 1057 msgbuf[IXGBE_VF_TRANS_VLAN] = num_tcs; 1058 else if (adapter->vfinfo[vf].pf_vlan || adapter->vfinfo[vf].pf_qos) 1059 msgbuf[IXGBE_VF_TRANS_VLAN] = 1; 1060 else 1061 msgbuf[IXGBE_VF_TRANS_VLAN] = 0; 1062 1063 /* notify VF of default queue */ 1064 msgbuf[IXGBE_VF_DEF_QUEUE] = default_tc; 1065 1066 return 0; 1067 } 1068 1069 static int ixgbe_get_vf_reta(struct ixgbe_adapter *adapter, u32 *msgbuf, u32 vf) 1070 { 1071 u32 i, j; 1072 u32 *out_buf = &msgbuf[1]; 1073 const u8 *reta = adapter->rss_indir_tbl; 1074 u32 reta_size = ixgbe_rss_indir_tbl_entries(adapter); 1075 1076 /* Check if operation is permitted */ 1077 if (!adapter->vfinfo[vf].rss_query_enabled) 1078 return -EPERM; 1079 1080 /* verify the PF is supporting the correct API */ 1081 switch (adapter->vfinfo[vf].vf_api) { 1082 case ixgbe_mbox_api_13: 1083 case ixgbe_mbox_api_12: 1084 break; 1085 default: 1086 return -EOPNOTSUPP; 1087 } 1088 1089 /* This mailbox command is supported (required) only for 82599 and x540 1090 * VFs which support up to 4 RSS queues. Therefore we will compress the 1091 * RETA by saving only 2 bits from each entry. This way we will be able 1092 * to transfer the whole RETA in a single mailbox operation. 1093 */ 1094 for (i = 0; i < reta_size / 16; i++) { 1095 out_buf[i] = 0; 1096 for (j = 0; j < 16; j++) 1097 out_buf[i] |= (u32)(reta[16 * i + j] & 0x3) << (2 * j); 1098 } 1099 1100 return 0; 1101 } 1102 1103 static int ixgbe_get_vf_rss_key(struct ixgbe_adapter *adapter, 1104 u32 *msgbuf, u32 vf) 1105 { 1106 u32 *rss_key = &msgbuf[1]; 1107 1108 /* Check if the operation is permitted */ 1109 if (!adapter->vfinfo[vf].rss_query_enabled) 1110 return -EPERM; 1111 1112 /* verify the PF is supporting the correct API */ 1113 switch (adapter->vfinfo[vf].vf_api) { 1114 case ixgbe_mbox_api_13: 1115 case ixgbe_mbox_api_12: 1116 break; 1117 default: 1118 return -EOPNOTSUPP; 1119 } 1120 1121 memcpy(rss_key, adapter->rss_key, IXGBE_RSS_KEY_SIZE); 1122 1123 return 0; 1124 } 1125 1126 static int ixgbe_update_vf_xcast_mode(struct ixgbe_adapter *adapter, 1127 u32 *msgbuf, u32 vf) 1128 { 1129 struct ixgbe_hw *hw = &adapter->hw; 1130 int xcast_mode = msgbuf[1]; 1131 u32 vmolr, fctrl, disable, enable; 1132 1133 /* verify the PF is supporting the correct APIs */ 1134 switch (adapter->vfinfo[vf].vf_api) { 1135 case ixgbe_mbox_api_12: 1136 /* promisc introduced in 1.3 version */ 1137 if (xcast_mode == IXGBEVF_XCAST_MODE_PROMISC) 1138 return -EOPNOTSUPP; 1139 /* Fall threw */ 1140 case ixgbe_mbox_api_13: 1141 break; 1142 default: 1143 return -EOPNOTSUPP; 1144 } 1145 1146 if (xcast_mode > IXGBEVF_XCAST_MODE_MULTI && 1147 !adapter->vfinfo[vf].trusted) { 1148 xcast_mode = IXGBEVF_XCAST_MODE_MULTI; 1149 } 1150 1151 if (adapter->vfinfo[vf].xcast_mode == xcast_mode) 1152 goto out; 1153 1154 switch (xcast_mode) { 1155 case IXGBEVF_XCAST_MODE_NONE: 1156 disable = IXGBE_VMOLR_BAM | IXGBE_VMOLR_ROMPE | 1157 IXGBE_VMOLR_MPE | IXGBE_VMOLR_UPE | IXGBE_VMOLR_VPE; 1158 enable = 0; 1159 break; 1160 case IXGBEVF_XCAST_MODE_MULTI: 1161 disable = IXGBE_VMOLR_MPE | IXGBE_VMOLR_UPE | IXGBE_VMOLR_VPE; 1162 enable = IXGBE_VMOLR_BAM | IXGBE_VMOLR_ROMPE; 1163 break; 1164 case IXGBEVF_XCAST_MODE_ALLMULTI: 1165 disable = IXGBE_VMOLR_UPE | IXGBE_VMOLR_VPE; 1166 enable = IXGBE_VMOLR_BAM | IXGBE_VMOLR_ROMPE | IXGBE_VMOLR_MPE; 1167 break; 1168 case IXGBEVF_XCAST_MODE_PROMISC: 1169 if (hw->mac.type <= ixgbe_mac_82599EB) 1170 return -EOPNOTSUPP; 1171 1172 fctrl = IXGBE_READ_REG(hw, IXGBE_FCTRL); 1173 if (!(fctrl & IXGBE_FCTRL_UPE)) { 1174 /* VF promisc requires PF in promisc */ 1175 e_warn(drv, 1176 "Enabling VF promisc requires PF in promisc\n"); 1177 return -EPERM; 1178 } 1179 1180 disable = 0; 1181 enable = IXGBE_VMOLR_BAM | IXGBE_VMOLR_ROMPE | 1182 IXGBE_VMOLR_MPE | IXGBE_VMOLR_UPE | IXGBE_VMOLR_VPE; 1183 break; 1184 default: 1185 return -EOPNOTSUPP; 1186 } 1187 1188 vmolr = IXGBE_READ_REG(hw, IXGBE_VMOLR(vf)); 1189 vmolr &= ~disable; 1190 vmolr |= enable; 1191 IXGBE_WRITE_REG(hw, IXGBE_VMOLR(vf), vmolr); 1192 1193 adapter->vfinfo[vf].xcast_mode = xcast_mode; 1194 1195 out: 1196 msgbuf[1] = xcast_mode; 1197 1198 return 0; 1199 } 1200 1201 static int ixgbe_rcv_msg_from_vf(struct ixgbe_adapter *adapter, u32 vf) 1202 { 1203 u32 mbx_size = IXGBE_VFMAILBOX_SIZE; 1204 u32 msgbuf[IXGBE_VFMAILBOX_SIZE]; 1205 struct ixgbe_hw *hw = &adapter->hw; 1206 s32 retval; 1207 1208 retval = ixgbe_read_mbx(hw, msgbuf, mbx_size, vf); 1209 1210 if (retval) { 1211 pr_err("Error receiving message from VF\n"); 1212 return retval; 1213 } 1214 1215 /* this is a message we already processed, do nothing */ 1216 if (msgbuf[0] & (IXGBE_VT_MSGTYPE_ACK | IXGBE_VT_MSGTYPE_NACK)) 1217 return 0; 1218 1219 /* flush the ack before we write any messages back */ 1220 IXGBE_WRITE_FLUSH(hw); 1221 1222 if (msgbuf[0] == IXGBE_VF_RESET) 1223 return ixgbe_vf_reset_msg(adapter, vf); 1224 1225 /* 1226 * until the vf completes a virtual function reset it should not be 1227 * allowed to start any configuration. 1228 */ 1229 if (!adapter->vfinfo[vf].clear_to_send) { 1230 msgbuf[0] |= IXGBE_VT_MSGTYPE_NACK; 1231 ixgbe_write_mbx(hw, msgbuf, 1, vf); 1232 return 0; 1233 } 1234 1235 switch ((msgbuf[0] & 0xFFFF)) { 1236 case IXGBE_VF_SET_MAC_ADDR: 1237 retval = ixgbe_set_vf_mac_addr(adapter, msgbuf, vf); 1238 break; 1239 case IXGBE_VF_SET_MULTICAST: 1240 retval = ixgbe_set_vf_multicasts(adapter, msgbuf, vf); 1241 break; 1242 case IXGBE_VF_SET_VLAN: 1243 retval = ixgbe_set_vf_vlan_msg(adapter, msgbuf, vf); 1244 break; 1245 case IXGBE_VF_SET_LPE: 1246 retval = ixgbe_set_vf_lpe(adapter, msgbuf, vf); 1247 break; 1248 case IXGBE_VF_SET_MACVLAN: 1249 retval = ixgbe_set_vf_macvlan_msg(adapter, msgbuf, vf); 1250 break; 1251 case IXGBE_VF_API_NEGOTIATE: 1252 retval = ixgbe_negotiate_vf_api(adapter, msgbuf, vf); 1253 break; 1254 case IXGBE_VF_GET_QUEUES: 1255 retval = ixgbe_get_vf_queues(adapter, msgbuf, vf); 1256 break; 1257 case IXGBE_VF_GET_RETA: 1258 retval = ixgbe_get_vf_reta(adapter, msgbuf, vf); 1259 break; 1260 case IXGBE_VF_GET_RSS_KEY: 1261 retval = ixgbe_get_vf_rss_key(adapter, msgbuf, vf); 1262 break; 1263 case IXGBE_VF_UPDATE_XCAST_MODE: 1264 retval = ixgbe_update_vf_xcast_mode(adapter, msgbuf, vf); 1265 break; 1266 default: 1267 e_err(drv, "Unhandled Msg %8.8x\n", msgbuf[0]); 1268 retval = IXGBE_ERR_MBX; 1269 break; 1270 } 1271 1272 /* notify the VF of the results of what it sent us */ 1273 if (retval) 1274 msgbuf[0] |= IXGBE_VT_MSGTYPE_NACK; 1275 else 1276 msgbuf[0] |= IXGBE_VT_MSGTYPE_ACK; 1277 1278 msgbuf[0] |= IXGBE_VT_MSGTYPE_CTS; 1279 1280 ixgbe_write_mbx(hw, msgbuf, mbx_size, vf); 1281 1282 return retval; 1283 } 1284 1285 static void ixgbe_rcv_ack_from_vf(struct ixgbe_adapter *adapter, u32 vf) 1286 { 1287 struct ixgbe_hw *hw = &adapter->hw; 1288 u32 msg = IXGBE_VT_MSGTYPE_NACK; 1289 1290 /* if device isn't clear to send it shouldn't be reading either */ 1291 if (!adapter->vfinfo[vf].clear_to_send) 1292 ixgbe_write_mbx(hw, &msg, 1, vf); 1293 } 1294 1295 void ixgbe_msg_task(struct ixgbe_adapter *adapter) 1296 { 1297 struct ixgbe_hw *hw = &adapter->hw; 1298 u32 vf; 1299 1300 for (vf = 0; vf < adapter->num_vfs; vf++) { 1301 /* process any reset requests */ 1302 if (!ixgbe_check_for_rst(hw, vf)) 1303 ixgbe_vf_reset_event(adapter, vf); 1304 1305 /* process any messages pending */ 1306 if (!ixgbe_check_for_msg(hw, vf)) 1307 ixgbe_rcv_msg_from_vf(adapter, vf); 1308 1309 /* process any acks */ 1310 if (!ixgbe_check_for_ack(hw, vf)) 1311 ixgbe_rcv_ack_from_vf(adapter, vf); 1312 } 1313 } 1314 1315 void ixgbe_disable_tx_rx(struct ixgbe_adapter *adapter) 1316 { 1317 struct ixgbe_hw *hw = &adapter->hw; 1318 1319 /* disable transmit and receive for all vfs */ 1320 IXGBE_WRITE_REG(hw, IXGBE_VFTE(0), 0); 1321 IXGBE_WRITE_REG(hw, IXGBE_VFTE(1), 0); 1322 1323 IXGBE_WRITE_REG(hw, IXGBE_VFRE(0), 0); 1324 IXGBE_WRITE_REG(hw, IXGBE_VFRE(1), 0); 1325 } 1326 1327 static inline void ixgbe_ping_vf(struct ixgbe_adapter *adapter, int vf) 1328 { 1329 struct ixgbe_hw *hw = &adapter->hw; 1330 u32 ping; 1331 1332 ping = IXGBE_PF_CONTROL_MSG; 1333 if (adapter->vfinfo[vf].clear_to_send) 1334 ping |= IXGBE_VT_MSGTYPE_CTS; 1335 ixgbe_write_mbx(hw, &ping, 1, vf); 1336 } 1337 1338 void ixgbe_ping_all_vfs(struct ixgbe_adapter *adapter) 1339 { 1340 struct ixgbe_hw *hw = &adapter->hw; 1341 u32 ping; 1342 int i; 1343 1344 for (i = 0 ; i < adapter->num_vfs; i++) { 1345 ping = IXGBE_PF_CONTROL_MSG; 1346 if (adapter->vfinfo[i].clear_to_send) 1347 ping |= IXGBE_VT_MSGTYPE_CTS; 1348 ixgbe_write_mbx(hw, &ping, 1, i); 1349 } 1350 } 1351 1352 int ixgbe_ndo_set_vf_mac(struct net_device *netdev, int vf, u8 *mac) 1353 { 1354 struct ixgbe_adapter *adapter = netdev_priv(netdev); 1355 s32 retval; 1356 1357 if (vf >= adapter->num_vfs) 1358 return -EINVAL; 1359 1360 if (is_valid_ether_addr(mac)) { 1361 dev_info(&adapter->pdev->dev, "setting MAC %pM on VF %d\n", 1362 mac, vf); 1363 dev_info(&adapter->pdev->dev, "Reload the VF driver to make this change effective."); 1364 1365 retval = ixgbe_set_vf_mac(adapter, vf, mac); 1366 if (retval >= 0) { 1367 adapter->vfinfo[vf].pf_set_mac = true; 1368 1369 if (test_bit(__IXGBE_DOWN, &adapter->state)) { 1370 dev_warn(&adapter->pdev->dev, "The VF MAC address has been set, but the PF device is not up.\n"); 1371 dev_warn(&adapter->pdev->dev, "Bring the PF device up before attempting to use the VF device.\n"); 1372 } 1373 } else { 1374 dev_warn(&adapter->pdev->dev, "The VF MAC address was NOT set due to invalid or duplicate MAC address.\n"); 1375 } 1376 } else if (is_zero_ether_addr(mac)) { 1377 unsigned char *vf_mac_addr = 1378 adapter->vfinfo[vf].vf_mac_addresses; 1379 1380 /* nothing to do */ 1381 if (is_zero_ether_addr(vf_mac_addr)) 1382 return 0; 1383 1384 dev_info(&adapter->pdev->dev, "removing MAC on VF %d\n", vf); 1385 1386 retval = ixgbe_del_mac_filter(adapter, vf_mac_addr, vf); 1387 if (retval >= 0) { 1388 adapter->vfinfo[vf].pf_set_mac = false; 1389 memcpy(vf_mac_addr, mac, ETH_ALEN); 1390 } else { 1391 dev_warn(&adapter->pdev->dev, "Could NOT remove the VF MAC address.\n"); 1392 } 1393 } else { 1394 retval = -EINVAL; 1395 } 1396 1397 return retval; 1398 } 1399 1400 static int ixgbe_enable_port_vlan(struct ixgbe_adapter *adapter, int vf, 1401 u16 vlan, u8 qos) 1402 { 1403 struct ixgbe_hw *hw = &adapter->hw; 1404 int err; 1405 1406 err = ixgbe_set_vf_vlan(adapter, true, vlan, vf); 1407 if (err) 1408 goto out; 1409 1410 /* Revoke tagless access via VLAN 0 */ 1411 ixgbe_set_vf_vlan(adapter, false, 0, vf); 1412 1413 ixgbe_set_vmvir(adapter, vlan, qos, vf); 1414 ixgbe_set_vmolr(hw, vf, false); 1415 1416 /* enable hide vlan on X550 */ 1417 if (hw->mac.type >= ixgbe_mac_X550) 1418 ixgbe_write_qde(adapter, vf, IXGBE_QDE_ENABLE | 1419 IXGBE_QDE_HIDE_VLAN); 1420 1421 adapter->vfinfo[vf].pf_vlan = vlan; 1422 adapter->vfinfo[vf].pf_qos = qos; 1423 dev_info(&adapter->pdev->dev, 1424 "Setting VLAN %d, QOS 0x%x on VF %d\n", vlan, qos, vf); 1425 if (test_bit(__IXGBE_DOWN, &adapter->state)) { 1426 dev_warn(&adapter->pdev->dev, 1427 "The VF VLAN has been set, but the PF device is not up.\n"); 1428 dev_warn(&adapter->pdev->dev, 1429 "Bring the PF device up before attempting to use the VF device.\n"); 1430 } 1431 1432 out: 1433 return err; 1434 } 1435 1436 static int ixgbe_disable_port_vlan(struct ixgbe_adapter *adapter, int vf) 1437 { 1438 struct ixgbe_hw *hw = &adapter->hw; 1439 int err; 1440 1441 err = ixgbe_set_vf_vlan(adapter, false, 1442 adapter->vfinfo[vf].pf_vlan, vf); 1443 /* Restore tagless access via VLAN 0 */ 1444 ixgbe_set_vf_vlan(adapter, true, 0, vf); 1445 ixgbe_clear_vmvir(adapter, vf); 1446 ixgbe_set_vmolr(hw, vf, true); 1447 1448 /* disable hide VLAN on X550 */ 1449 if (hw->mac.type >= ixgbe_mac_X550) 1450 ixgbe_write_qde(adapter, vf, IXGBE_QDE_ENABLE); 1451 1452 adapter->vfinfo[vf].pf_vlan = 0; 1453 adapter->vfinfo[vf].pf_qos = 0; 1454 1455 return err; 1456 } 1457 1458 int ixgbe_ndo_set_vf_vlan(struct net_device *netdev, int vf, u16 vlan, 1459 u8 qos, __be16 vlan_proto) 1460 { 1461 int err = 0; 1462 struct ixgbe_adapter *adapter = netdev_priv(netdev); 1463 1464 if ((vf >= adapter->num_vfs) || (vlan > 4095) || (qos > 7)) 1465 return -EINVAL; 1466 if (vlan_proto != htons(ETH_P_8021Q)) 1467 return -EPROTONOSUPPORT; 1468 if (vlan || qos) { 1469 /* Check if there is already a port VLAN set, if so 1470 * we have to delete the old one first before we 1471 * can set the new one. The usage model had 1472 * previously assumed the user would delete the 1473 * old port VLAN before setting a new one but this 1474 * is not necessarily the case. 1475 */ 1476 if (adapter->vfinfo[vf].pf_vlan) 1477 err = ixgbe_disable_port_vlan(adapter, vf); 1478 if (err) 1479 goto out; 1480 err = ixgbe_enable_port_vlan(adapter, vf, vlan, qos); 1481 } else { 1482 err = ixgbe_disable_port_vlan(adapter, vf); 1483 } 1484 1485 out: 1486 return err; 1487 } 1488 1489 int ixgbe_link_mbps(struct ixgbe_adapter *adapter) 1490 { 1491 switch (adapter->link_speed) { 1492 case IXGBE_LINK_SPEED_100_FULL: 1493 return 100; 1494 case IXGBE_LINK_SPEED_1GB_FULL: 1495 return 1000; 1496 case IXGBE_LINK_SPEED_10GB_FULL: 1497 return 10000; 1498 default: 1499 return 0; 1500 } 1501 } 1502 1503 static void ixgbe_set_vf_rate_limit(struct ixgbe_adapter *adapter, int vf) 1504 { 1505 struct ixgbe_ring_feature *vmdq = &adapter->ring_feature[RING_F_VMDQ]; 1506 struct ixgbe_hw *hw = &adapter->hw; 1507 u32 bcnrc_val = 0; 1508 u16 queue, queues_per_pool; 1509 u16 tx_rate = adapter->vfinfo[vf].tx_rate; 1510 1511 if (tx_rate) { 1512 /* start with base link speed value */ 1513 bcnrc_val = adapter->vf_rate_link_speed; 1514 1515 /* Calculate the rate factor values to set */ 1516 bcnrc_val <<= IXGBE_RTTBCNRC_RF_INT_SHIFT; 1517 bcnrc_val /= tx_rate; 1518 1519 /* clear everything but the rate factor */ 1520 bcnrc_val &= IXGBE_RTTBCNRC_RF_INT_MASK | 1521 IXGBE_RTTBCNRC_RF_DEC_MASK; 1522 1523 /* enable the rate scheduler */ 1524 bcnrc_val |= IXGBE_RTTBCNRC_RS_ENA; 1525 } 1526 1527 /* 1528 * Set global transmit compensation time to the MMW_SIZE in RTTBCNRM 1529 * register. Typically MMW_SIZE=0x014 if 9728-byte jumbo is supported 1530 * and 0x004 otherwise. 1531 */ 1532 switch (hw->mac.type) { 1533 case ixgbe_mac_82599EB: 1534 IXGBE_WRITE_REG(hw, IXGBE_RTTBCNRM, 0x4); 1535 break; 1536 case ixgbe_mac_X540: 1537 IXGBE_WRITE_REG(hw, IXGBE_RTTBCNRM, 0x14); 1538 break; 1539 default: 1540 break; 1541 } 1542 1543 /* determine how many queues per pool based on VMDq mask */ 1544 queues_per_pool = __ALIGN_MASK(1, ~vmdq->mask); 1545 1546 /* write value for all Tx queues belonging to VF */ 1547 for (queue = 0; queue < queues_per_pool; queue++) { 1548 unsigned int reg_idx = (vf * queues_per_pool) + queue; 1549 1550 IXGBE_WRITE_REG(hw, IXGBE_RTTDQSEL, reg_idx); 1551 IXGBE_WRITE_REG(hw, IXGBE_RTTBCNRC, bcnrc_val); 1552 } 1553 } 1554 1555 void ixgbe_check_vf_rate_limit(struct ixgbe_adapter *adapter) 1556 { 1557 int i; 1558 1559 /* VF Tx rate limit was not set */ 1560 if (!adapter->vf_rate_link_speed) 1561 return; 1562 1563 if (ixgbe_link_mbps(adapter) != adapter->vf_rate_link_speed) { 1564 adapter->vf_rate_link_speed = 0; 1565 dev_info(&adapter->pdev->dev, 1566 "Link speed has been changed. VF Transmit rate is disabled\n"); 1567 } 1568 1569 for (i = 0; i < adapter->num_vfs; i++) { 1570 if (!adapter->vf_rate_link_speed) 1571 adapter->vfinfo[i].tx_rate = 0; 1572 1573 ixgbe_set_vf_rate_limit(adapter, i); 1574 } 1575 } 1576 1577 int ixgbe_ndo_set_vf_bw(struct net_device *netdev, int vf, int min_tx_rate, 1578 int max_tx_rate) 1579 { 1580 struct ixgbe_adapter *adapter = netdev_priv(netdev); 1581 int link_speed; 1582 1583 /* verify VF is active */ 1584 if (vf >= adapter->num_vfs) 1585 return -EINVAL; 1586 1587 /* verify link is up */ 1588 if (!adapter->link_up) 1589 return -EINVAL; 1590 1591 /* verify we are linked at 10Gbps */ 1592 link_speed = ixgbe_link_mbps(adapter); 1593 if (link_speed != 10000) 1594 return -EINVAL; 1595 1596 if (min_tx_rate) 1597 return -EINVAL; 1598 1599 /* rate limit cannot be less than 10Mbs or greater than link speed */ 1600 if (max_tx_rate && ((max_tx_rate <= 10) || (max_tx_rate > link_speed))) 1601 return -EINVAL; 1602 1603 /* store values */ 1604 adapter->vf_rate_link_speed = link_speed; 1605 adapter->vfinfo[vf].tx_rate = max_tx_rate; 1606 1607 /* update hardware configuration */ 1608 ixgbe_set_vf_rate_limit(adapter, vf); 1609 1610 return 0; 1611 } 1612 1613 int ixgbe_ndo_set_vf_spoofchk(struct net_device *netdev, int vf, bool setting) 1614 { 1615 struct ixgbe_adapter *adapter = netdev_priv(netdev); 1616 struct ixgbe_hw *hw = &adapter->hw; 1617 1618 if (vf >= adapter->num_vfs) 1619 return -EINVAL; 1620 1621 adapter->vfinfo[vf].spoofchk_enabled = setting; 1622 1623 /* configure MAC spoofing */ 1624 hw->mac.ops.set_mac_anti_spoofing(hw, setting, vf); 1625 1626 /* configure VLAN spoofing */ 1627 hw->mac.ops.set_vlan_anti_spoofing(hw, setting, vf); 1628 1629 /* Ensure LLDP and FC is set for Ethertype Antispoofing if we will be 1630 * calling set_ethertype_anti_spoofing for each VF in loop below 1631 */ 1632 if (hw->mac.ops.set_ethertype_anti_spoofing) { 1633 IXGBE_WRITE_REG(hw, IXGBE_ETQF(IXGBE_ETQF_FILTER_LLDP), 1634 (IXGBE_ETQF_FILTER_EN | 1635 IXGBE_ETQF_TX_ANTISPOOF | 1636 IXGBE_ETH_P_LLDP)); 1637 1638 IXGBE_WRITE_REG(hw, IXGBE_ETQF(IXGBE_ETQF_FILTER_FC), 1639 (IXGBE_ETQF_FILTER_EN | 1640 IXGBE_ETQF_TX_ANTISPOOF | 1641 ETH_P_PAUSE)); 1642 1643 hw->mac.ops.set_ethertype_anti_spoofing(hw, setting, vf); 1644 } 1645 1646 return 0; 1647 } 1648 1649 int ixgbe_ndo_set_vf_rss_query_en(struct net_device *netdev, int vf, 1650 bool setting) 1651 { 1652 struct ixgbe_adapter *adapter = netdev_priv(netdev); 1653 1654 /* This operation is currently supported only for 82599 and x540 1655 * devices. 1656 */ 1657 if (adapter->hw.mac.type < ixgbe_mac_82599EB || 1658 adapter->hw.mac.type >= ixgbe_mac_X550) 1659 return -EOPNOTSUPP; 1660 1661 if (vf >= adapter->num_vfs) 1662 return -EINVAL; 1663 1664 adapter->vfinfo[vf].rss_query_enabled = setting; 1665 1666 return 0; 1667 } 1668 1669 int ixgbe_ndo_set_vf_trust(struct net_device *netdev, int vf, bool setting) 1670 { 1671 struct ixgbe_adapter *adapter = netdev_priv(netdev); 1672 1673 if (vf >= adapter->num_vfs) 1674 return -EINVAL; 1675 1676 /* nothing to do */ 1677 if (adapter->vfinfo[vf].trusted == setting) 1678 return 0; 1679 1680 adapter->vfinfo[vf].trusted = setting; 1681 1682 /* reset VF to reconfigure features */ 1683 adapter->vfinfo[vf].clear_to_send = false; 1684 ixgbe_ping_vf(adapter, vf); 1685 1686 e_info(drv, "VF %u is %strusted\n", vf, setting ? "" : "not "); 1687 1688 return 0; 1689 } 1690 1691 int ixgbe_ndo_get_vf_config(struct net_device *netdev, 1692 int vf, struct ifla_vf_info *ivi) 1693 { 1694 struct ixgbe_adapter *adapter = netdev_priv(netdev); 1695 if (vf >= adapter->num_vfs) 1696 return -EINVAL; 1697 ivi->vf = vf; 1698 memcpy(&ivi->mac, adapter->vfinfo[vf].vf_mac_addresses, ETH_ALEN); 1699 ivi->max_tx_rate = adapter->vfinfo[vf].tx_rate; 1700 ivi->min_tx_rate = 0; 1701 ivi->vlan = adapter->vfinfo[vf].pf_vlan; 1702 ivi->qos = adapter->vfinfo[vf].pf_qos; 1703 ivi->spoofchk = adapter->vfinfo[vf].spoofchk_enabled; 1704 ivi->rss_query_en = adapter->vfinfo[vf].rss_query_enabled; 1705 ivi->trusted = adapter->vfinfo[vf].trusted; 1706 return 0; 1707 } 1708