1 /******************************************************************************* 2 * 3 * Intel Ethernet Controller XL710 Family Linux Driver 4 * Copyright(c) 2013 - 2017 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 16 * with this program. If not, see <http://www.gnu.org/licenses/>. 17 * 18 * The full GNU General Public License is included in this distribution in 19 * the file called "COPYING". 20 * 21 * Contact Information: 22 * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net> 23 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497 24 * 25 ******************************************************************************/ 26 27 #include <linux/etherdevice.h> 28 #include <linux/of_net.h> 29 #include <linux/pci.h> 30 #include <linux/bpf.h> 31 32 /* Local includes */ 33 #include "i40e.h" 34 #include "i40e_diag.h" 35 #include <net/udp_tunnel.h> 36 /* All i40e tracepoints are defined by the include below, which 37 * must be included exactly once across the whole kernel with 38 * CREATE_TRACE_POINTS defined 39 */ 40 #define CREATE_TRACE_POINTS 41 #include "i40e_trace.h" 42 43 const char i40e_driver_name[] = "i40e"; 44 static const char i40e_driver_string[] = 45 "Intel(R) Ethernet Connection XL710 Network Driver"; 46 47 #define DRV_KERN "-k" 48 49 #define DRV_VERSION_MAJOR 2 50 #define DRV_VERSION_MINOR 3 51 #define DRV_VERSION_BUILD 2 52 #define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \ 53 __stringify(DRV_VERSION_MINOR) "." \ 54 __stringify(DRV_VERSION_BUILD) DRV_KERN 55 const char i40e_driver_version_str[] = DRV_VERSION; 56 static const char i40e_copyright[] = "Copyright (c) 2013 - 2014 Intel Corporation."; 57 58 /* a bit of forward declarations */ 59 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi); 60 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired); 61 static int i40e_add_vsi(struct i40e_vsi *vsi); 62 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi); 63 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit); 64 static int i40e_setup_misc_vector(struct i40e_pf *pf); 65 static void i40e_determine_queue_usage(struct i40e_pf *pf); 66 static int i40e_setup_pf_filter_control(struct i40e_pf *pf); 67 static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired); 68 static int i40e_reset(struct i40e_pf *pf); 69 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired); 70 static void i40e_fdir_sb_setup(struct i40e_pf *pf); 71 static int i40e_veb_get_bw_info(struct i40e_veb *veb); 72 static int i40e_add_del_cloud_filter(struct i40e_vsi *vsi, 73 struct i40e_cloud_filter *filter, 74 bool add); 75 static int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi, 76 struct i40e_cloud_filter *filter, 77 bool add); 78 static int i40e_get_capabilities(struct i40e_pf *pf, 79 enum i40e_admin_queue_opc list_type); 80 81 82 /* i40e_pci_tbl - PCI Device ID Table 83 * 84 * Last entry must be all 0s 85 * 86 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID, 87 * Class, Class Mask, private data (not used) } 88 */ 89 static const struct pci_device_id i40e_pci_tbl[] = { 90 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0}, 91 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0}, 92 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0}, 93 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0}, 94 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0}, 95 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0}, 96 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0}, 97 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0}, 98 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0}, 99 {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0}, 100 {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0}, 101 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0}, 102 {PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0}, 103 {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0}, 104 {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0}, 105 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0}, 106 {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0}, 107 {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0}, 108 {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0}, 109 /* required last entry */ 110 {0, } 111 }; 112 MODULE_DEVICE_TABLE(pci, i40e_pci_tbl); 113 114 #define I40E_MAX_VF_COUNT 128 115 static int debug = -1; 116 module_param(debug, uint, 0); 117 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)"); 118 119 MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>"); 120 MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver"); 121 MODULE_LICENSE("GPL"); 122 MODULE_VERSION(DRV_VERSION); 123 124 static struct workqueue_struct *i40e_wq; 125 126 /** 127 * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code 128 * @hw: pointer to the HW structure 129 * @mem: ptr to mem struct to fill out 130 * @size: size of memory requested 131 * @alignment: what to align the allocation to 132 **/ 133 int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem, 134 u64 size, u32 alignment) 135 { 136 struct i40e_pf *pf = (struct i40e_pf *)hw->back; 137 138 mem->size = ALIGN(size, alignment); 139 mem->va = dma_zalloc_coherent(&pf->pdev->dev, mem->size, 140 &mem->pa, GFP_KERNEL); 141 if (!mem->va) 142 return -ENOMEM; 143 144 return 0; 145 } 146 147 /** 148 * i40e_free_dma_mem_d - OS specific memory free for shared code 149 * @hw: pointer to the HW structure 150 * @mem: ptr to mem struct to free 151 **/ 152 int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem) 153 { 154 struct i40e_pf *pf = (struct i40e_pf *)hw->back; 155 156 dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa); 157 mem->va = NULL; 158 mem->pa = 0; 159 mem->size = 0; 160 161 return 0; 162 } 163 164 /** 165 * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code 166 * @hw: pointer to the HW structure 167 * @mem: ptr to mem struct to fill out 168 * @size: size of memory requested 169 **/ 170 int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem, 171 u32 size) 172 { 173 mem->size = size; 174 mem->va = kzalloc(size, GFP_KERNEL); 175 176 if (!mem->va) 177 return -ENOMEM; 178 179 return 0; 180 } 181 182 /** 183 * i40e_free_virt_mem_d - OS specific memory free for shared code 184 * @hw: pointer to the HW structure 185 * @mem: ptr to mem struct to free 186 **/ 187 int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem) 188 { 189 /* it's ok to kfree a NULL pointer */ 190 kfree(mem->va); 191 mem->va = NULL; 192 mem->size = 0; 193 194 return 0; 195 } 196 197 /** 198 * i40e_get_lump - find a lump of free generic resource 199 * @pf: board private structure 200 * @pile: the pile of resource to search 201 * @needed: the number of items needed 202 * @id: an owner id to stick on the items assigned 203 * 204 * Returns the base item index of the lump, or negative for error 205 * 206 * The search_hint trick and lack of advanced fit-finding only work 207 * because we're highly likely to have all the same size lump requests. 208 * Linear search time and any fragmentation should be minimal. 209 **/ 210 static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile, 211 u16 needed, u16 id) 212 { 213 int ret = -ENOMEM; 214 int i, j; 215 216 if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) { 217 dev_info(&pf->pdev->dev, 218 "param err: pile=%s needed=%d id=0x%04x\n", 219 pile ? "<valid>" : "<null>", needed, id); 220 return -EINVAL; 221 } 222 223 /* start the linear search with an imperfect hint */ 224 i = pile->search_hint; 225 while (i < pile->num_entries) { 226 /* skip already allocated entries */ 227 if (pile->list[i] & I40E_PILE_VALID_BIT) { 228 i++; 229 continue; 230 } 231 232 /* do we have enough in this lump? */ 233 for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) { 234 if (pile->list[i+j] & I40E_PILE_VALID_BIT) 235 break; 236 } 237 238 if (j == needed) { 239 /* there was enough, so assign it to the requestor */ 240 for (j = 0; j < needed; j++) 241 pile->list[i+j] = id | I40E_PILE_VALID_BIT; 242 ret = i; 243 pile->search_hint = i + j; 244 break; 245 } 246 247 /* not enough, so skip over it and continue looking */ 248 i += j; 249 } 250 251 return ret; 252 } 253 254 /** 255 * i40e_put_lump - return a lump of generic resource 256 * @pile: the pile of resource to search 257 * @index: the base item index 258 * @id: the owner id of the items assigned 259 * 260 * Returns the count of items in the lump 261 **/ 262 static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id) 263 { 264 int valid_id = (id | I40E_PILE_VALID_BIT); 265 int count = 0; 266 int i; 267 268 if (!pile || index >= pile->num_entries) 269 return -EINVAL; 270 271 for (i = index; 272 i < pile->num_entries && pile->list[i] == valid_id; 273 i++) { 274 pile->list[i] = 0; 275 count++; 276 } 277 278 if (count && index < pile->search_hint) 279 pile->search_hint = index; 280 281 return count; 282 } 283 284 /** 285 * i40e_find_vsi_from_id - searches for the vsi with the given id 286 * @pf - the pf structure to search for the vsi 287 * @id - id of the vsi it is searching for 288 **/ 289 struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id) 290 { 291 int i; 292 293 for (i = 0; i < pf->num_alloc_vsi; i++) 294 if (pf->vsi[i] && (pf->vsi[i]->id == id)) 295 return pf->vsi[i]; 296 297 return NULL; 298 } 299 300 /** 301 * i40e_service_event_schedule - Schedule the service task to wake up 302 * @pf: board private structure 303 * 304 * If not already scheduled, this puts the task into the work queue 305 **/ 306 void i40e_service_event_schedule(struct i40e_pf *pf) 307 { 308 if (!test_bit(__I40E_DOWN, pf->state) && 309 !test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) 310 queue_work(i40e_wq, &pf->service_task); 311 } 312 313 /** 314 * i40e_tx_timeout - Respond to a Tx Hang 315 * @netdev: network interface device structure 316 * 317 * If any port has noticed a Tx timeout, it is likely that the whole 318 * device is munged, not just the one netdev port, so go for the full 319 * reset. 320 **/ 321 static void i40e_tx_timeout(struct net_device *netdev) 322 { 323 struct i40e_netdev_priv *np = netdev_priv(netdev); 324 struct i40e_vsi *vsi = np->vsi; 325 struct i40e_pf *pf = vsi->back; 326 struct i40e_ring *tx_ring = NULL; 327 unsigned int i, hung_queue = 0; 328 u32 head, val; 329 330 pf->tx_timeout_count++; 331 332 /* find the stopped queue the same way the stack does */ 333 for (i = 0; i < netdev->num_tx_queues; i++) { 334 struct netdev_queue *q; 335 unsigned long trans_start; 336 337 q = netdev_get_tx_queue(netdev, i); 338 trans_start = q->trans_start; 339 if (netif_xmit_stopped(q) && 340 time_after(jiffies, 341 (trans_start + netdev->watchdog_timeo))) { 342 hung_queue = i; 343 break; 344 } 345 } 346 347 if (i == netdev->num_tx_queues) { 348 netdev_info(netdev, "tx_timeout: no netdev hung queue found\n"); 349 } else { 350 /* now that we have an index, find the tx_ring struct */ 351 for (i = 0; i < vsi->num_queue_pairs; i++) { 352 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) { 353 if (hung_queue == 354 vsi->tx_rings[i]->queue_index) { 355 tx_ring = vsi->tx_rings[i]; 356 break; 357 } 358 } 359 } 360 } 361 362 if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20))) 363 pf->tx_timeout_recovery_level = 1; /* reset after some time */ 364 else if (time_before(jiffies, 365 (pf->tx_timeout_last_recovery + netdev->watchdog_timeo))) 366 return; /* don't do any new action before the next timeout */ 367 368 if (tx_ring) { 369 head = i40e_get_head(tx_ring); 370 /* Read interrupt register */ 371 if (pf->flags & I40E_FLAG_MSIX_ENABLED) 372 val = rd32(&pf->hw, 373 I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx + 374 tx_ring->vsi->base_vector - 1)); 375 else 376 val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0); 377 378 netdev_info(netdev, "tx_timeout: VSI_seid: %d, Q %d, NTC: 0x%x, HWB: 0x%x, NTU: 0x%x, TAIL: 0x%x, INT: 0x%x\n", 379 vsi->seid, hung_queue, tx_ring->next_to_clean, 380 head, tx_ring->next_to_use, 381 readl(tx_ring->tail), val); 382 } 383 384 pf->tx_timeout_last_recovery = jiffies; 385 netdev_info(netdev, "tx_timeout recovery level %d, hung_queue %d\n", 386 pf->tx_timeout_recovery_level, hung_queue); 387 388 switch (pf->tx_timeout_recovery_level) { 389 case 1: 390 set_bit(__I40E_PF_RESET_REQUESTED, pf->state); 391 break; 392 case 2: 393 set_bit(__I40E_CORE_RESET_REQUESTED, pf->state); 394 break; 395 case 3: 396 set_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state); 397 break; 398 default: 399 netdev_err(netdev, "tx_timeout recovery unsuccessful\n"); 400 break; 401 } 402 403 i40e_service_event_schedule(pf); 404 pf->tx_timeout_recovery_level++; 405 } 406 407 /** 408 * i40e_get_vsi_stats_struct - Get System Network Statistics 409 * @vsi: the VSI we care about 410 * 411 * Returns the address of the device statistics structure. 412 * The statistics are actually updated from the service task. 413 **/ 414 struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi) 415 { 416 return &vsi->net_stats; 417 } 418 419 /** 420 * i40e_get_netdev_stats_struct_tx - populate stats from a Tx ring 421 * @ring: Tx ring to get statistics from 422 * @stats: statistics entry to be updated 423 **/ 424 static void i40e_get_netdev_stats_struct_tx(struct i40e_ring *ring, 425 struct rtnl_link_stats64 *stats) 426 { 427 u64 bytes, packets; 428 unsigned int start; 429 430 do { 431 start = u64_stats_fetch_begin_irq(&ring->syncp); 432 packets = ring->stats.packets; 433 bytes = ring->stats.bytes; 434 } while (u64_stats_fetch_retry_irq(&ring->syncp, start)); 435 436 stats->tx_packets += packets; 437 stats->tx_bytes += bytes; 438 } 439 440 /** 441 * i40e_get_netdev_stats_struct - Get statistics for netdev interface 442 * @netdev: network interface device structure 443 * 444 * Returns the address of the device statistics structure. 445 * The statistics are actually updated from the service task. 446 **/ 447 static void i40e_get_netdev_stats_struct(struct net_device *netdev, 448 struct rtnl_link_stats64 *stats) 449 { 450 struct i40e_netdev_priv *np = netdev_priv(netdev); 451 struct i40e_ring *tx_ring, *rx_ring; 452 struct i40e_vsi *vsi = np->vsi; 453 struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi); 454 int i; 455 456 if (test_bit(__I40E_VSI_DOWN, vsi->state)) 457 return; 458 459 if (!vsi->tx_rings) 460 return; 461 462 rcu_read_lock(); 463 for (i = 0; i < vsi->num_queue_pairs; i++) { 464 u64 bytes, packets; 465 unsigned int start; 466 467 tx_ring = READ_ONCE(vsi->tx_rings[i]); 468 if (!tx_ring) 469 continue; 470 i40e_get_netdev_stats_struct_tx(tx_ring, stats); 471 472 rx_ring = &tx_ring[1]; 473 474 do { 475 start = u64_stats_fetch_begin_irq(&rx_ring->syncp); 476 packets = rx_ring->stats.packets; 477 bytes = rx_ring->stats.bytes; 478 } while (u64_stats_fetch_retry_irq(&rx_ring->syncp, start)); 479 480 stats->rx_packets += packets; 481 stats->rx_bytes += bytes; 482 483 if (i40e_enabled_xdp_vsi(vsi)) 484 i40e_get_netdev_stats_struct_tx(&rx_ring[1], stats); 485 } 486 rcu_read_unlock(); 487 488 /* following stats updated by i40e_watchdog_subtask() */ 489 stats->multicast = vsi_stats->multicast; 490 stats->tx_errors = vsi_stats->tx_errors; 491 stats->tx_dropped = vsi_stats->tx_dropped; 492 stats->rx_errors = vsi_stats->rx_errors; 493 stats->rx_dropped = vsi_stats->rx_dropped; 494 stats->rx_crc_errors = vsi_stats->rx_crc_errors; 495 stats->rx_length_errors = vsi_stats->rx_length_errors; 496 } 497 498 /** 499 * i40e_vsi_reset_stats - Resets all stats of the given vsi 500 * @vsi: the VSI to have its stats reset 501 **/ 502 void i40e_vsi_reset_stats(struct i40e_vsi *vsi) 503 { 504 struct rtnl_link_stats64 *ns; 505 int i; 506 507 if (!vsi) 508 return; 509 510 ns = i40e_get_vsi_stats_struct(vsi); 511 memset(ns, 0, sizeof(*ns)); 512 memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets)); 513 memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats)); 514 memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets)); 515 if (vsi->rx_rings && vsi->rx_rings[0]) { 516 for (i = 0; i < vsi->num_queue_pairs; i++) { 517 memset(&vsi->rx_rings[i]->stats, 0, 518 sizeof(vsi->rx_rings[i]->stats)); 519 memset(&vsi->rx_rings[i]->rx_stats, 0, 520 sizeof(vsi->rx_rings[i]->rx_stats)); 521 memset(&vsi->tx_rings[i]->stats, 0, 522 sizeof(vsi->tx_rings[i]->stats)); 523 memset(&vsi->tx_rings[i]->tx_stats, 0, 524 sizeof(vsi->tx_rings[i]->tx_stats)); 525 } 526 } 527 vsi->stat_offsets_loaded = false; 528 } 529 530 /** 531 * i40e_pf_reset_stats - Reset all of the stats for the given PF 532 * @pf: the PF to be reset 533 **/ 534 void i40e_pf_reset_stats(struct i40e_pf *pf) 535 { 536 int i; 537 538 memset(&pf->stats, 0, sizeof(pf->stats)); 539 memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets)); 540 pf->stat_offsets_loaded = false; 541 542 for (i = 0; i < I40E_MAX_VEB; i++) { 543 if (pf->veb[i]) { 544 memset(&pf->veb[i]->stats, 0, 545 sizeof(pf->veb[i]->stats)); 546 memset(&pf->veb[i]->stats_offsets, 0, 547 sizeof(pf->veb[i]->stats_offsets)); 548 pf->veb[i]->stat_offsets_loaded = false; 549 } 550 } 551 pf->hw_csum_rx_error = 0; 552 } 553 554 /** 555 * i40e_stat_update48 - read and update a 48 bit stat from the chip 556 * @hw: ptr to the hardware info 557 * @hireg: the high 32 bit reg to read 558 * @loreg: the low 32 bit reg to read 559 * @offset_loaded: has the initial offset been loaded yet 560 * @offset: ptr to current offset value 561 * @stat: ptr to the stat 562 * 563 * Since the device stats are not reset at PFReset, they likely will not 564 * be zeroed when the driver starts. We'll save the first values read 565 * and use them as offsets to be subtracted from the raw values in order 566 * to report stats that count from zero. In the process, we also manage 567 * the potential roll-over. 568 **/ 569 static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg, 570 bool offset_loaded, u64 *offset, u64 *stat) 571 { 572 u64 new_data; 573 574 if (hw->device_id == I40E_DEV_ID_QEMU) { 575 new_data = rd32(hw, loreg); 576 new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32; 577 } else { 578 new_data = rd64(hw, loreg); 579 } 580 if (!offset_loaded) 581 *offset = new_data; 582 if (likely(new_data >= *offset)) 583 *stat = new_data - *offset; 584 else 585 *stat = (new_data + BIT_ULL(48)) - *offset; 586 *stat &= 0xFFFFFFFFFFFFULL; 587 } 588 589 /** 590 * i40e_stat_update32 - read and update a 32 bit stat from the chip 591 * @hw: ptr to the hardware info 592 * @reg: the hw reg to read 593 * @offset_loaded: has the initial offset been loaded yet 594 * @offset: ptr to current offset value 595 * @stat: ptr to the stat 596 **/ 597 static void i40e_stat_update32(struct i40e_hw *hw, u32 reg, 598 bool offset_loaded, u64 *offset, u64 *stat) 599 { 600 u32 new_data; 601 602 new_data = rd32(hw, reg); 603 if (!offset_loaded) 604 *offset = new_data; 605 if (likely(new_data >= *offset)) 606 *stat = (u32)(new_data - *offset); 607 else 608 *stat = (u32)((new_data + BIT_ULL(32)) - *offset); 609 } 610 611 /** 612 * i40e_stat_update_and_clear32 - read and clear hw reg, update a 32 bit stat 613 * @hw: ptr to the hardware info 614 * @reg: the hw reg to read and clear 615 * @stat: ptr to the stat 616 **/ 617 static void i40e_stat_update_and_clear32(struct i40e_hw *hw, u32 reg, u64 *stat) 618 { 619 u32 new_data = rd32(hw, reg); 620 621 wr32(hw, reg, 1); /* must write a nonzero value to clear register */ 622 *stat += new_data; 623 } 624 625 /** 626 * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters. 627 * @vsi: the VSI to be updated 628 **/ 629 void i40e_update_eth_stats(struct i40e_vsi *vsi) 630 { 631 int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx); 632 struct i40e_pf *pf = vsi->back; 633 struct i40e_hw *hw = &pf->hw; 634 struct i40e_eth_stats *oes; 635 struct i40e_eth_stats *es; /* device's eth stats */ 636 637 es = &vsi->eth_stats; 638 oes = &vsi->eth_stats_offsets; 639 640 /* Gather up the stats that the hw collects */ 641 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx), 642 vsi->stat_offsets_loaded, 643 &oes->tx_errors, &es->tx_errors); 644 i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx), 645 vsi->stat_offsets_loaded, 646 &oes->rx_discards, &es->rx_discards); 647 i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx), 648 vsi->stat_offsets_loaded, 649 &oes->rx_unknown_protocol, &es->rx_unknown_protocol); 650 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx), 651 vsi->stat_offsets_loaded, 652 &oes->tx_errors, &es->tx_errors); 653 654 i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx), 655 I40E_GLV_GORCL(stat_idx), 656 vsi->stat_offsets_loaded, 657 &oes->rx_bytes, &es->rx_bytes); 658 i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx), 659 I40E_GLV_UPRCL(stat_idx), 660 vsi->stat_offsets_loaded, 661 &oes->rx_unicast, &es->rx_unicast); 662 i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx), 663 I40E_GLV_MPRCL(stat_idx), 664 vsi->stat_offsets_loaded, 665 &oes->rx_multicast, &es->rx_multicast); 666 i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx), 667 I40E_GLV_BPRCL(stat_idx), 668 vsi->stat_offsets_loaded, 669 &oes->rx_broadcast, &es->rx_broadcast); 670 671 i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx), 672 I40E_GLV_GOTCL(stat_idx), 673 vsi->stat_offsets_loaded, 674 &oes->tx_bytes, &es->tx_bytes); 675 i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx), 676 I40E_GLV_UPTCL(stat_idx), 677 vsi->stat_offsets_loaded, 678 &oes->tx_unicast, &es->tx_unicast); 679 i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx), 680 I40E_GLV_MPTCL(stat_idx), 681 vsi->stat_offsets_loaded, 682 &oes->tx_multicast, &es->tx_multicast); 683 i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx), 684 I40E_GLV_BPTCL(stat_idx), 685 vsi->stat_offsets_loaded, 686 &oes->tx_broadcast, &es->tx_broadcast); 687 vsi->stat_offsets_loaded = true; 688 } 689 690 /** 691 * i40e_update_veb_stats - Update Switch component statistics 692 * @veb: the VEB being updated 693 **/ 694 static void i40e_update_veb_stats(struct i40e_veb *veb) 695 { 696 struct i40e_pf *pf = veb->pf; 697 struct i40e_hw *hw = &pf->hw; 698 struct i40e_eth_stats *oes; 699 struct i40e_eth_stats *es; /* device's eth stats */ 700 struct i40e_veb_tc_stats *veb_oes; 701 struct i40e_veb_tc_stats *veb_es; 702 int i, idx = 0; 703 704 idx = veb->stats_idx; 705 es = &veb->stats; 706 oes = &veb->stats_offsets; 707 veb_es = &veb->tc_stats; 708 veb_oes = &veb->tc_stats_offsets; 709 710 /* Gather up the stats that the hw collects */ 711 i40e_stat_update32(hw, I40E_GLSW_TDPC(idx), 712 veb->stat_offsets_loaded, 713 &oes->tx_discards, &es->tx_discards); 714 if (hw->revision_id > 0) 715 i40e_stat_update32(hw, I40E_GLSW_RUPP(idx), 716 veb->stat_offsets_loaded, 717 &oes->rx_unknown_protocol, 718 &es->rx_unknown_protocol); 719 i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx), 720 veb->stat_offsets_loaded, 721 &oes->rx_bytes, &es->rx_bytes); 722 i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx), 723 veb->stat_offsets_loaded, 724 &oes->rx_unicast, &es->rx_unicast); 725 i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx), 726 veb->stat_offsets_loaded, 727 &oes->rx_multicast, &es->rx_multicast); 728 i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx), 729 veb->stat_offsets_loaded, 730 &oes->rx_broadcast, &es->rx_broadcast); 731 732 i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx), 733 veb->stat_offsets_loaded, 734 &oes->tx_bytes, &es->tx_bytes); 735 i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx), 736 veb->stat_offsets_loaded, 737 &oes->tx_unicast, &es->tx_unicast); 738 i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx), 739 veb->stat_offsets_loaded, 740 &oes->tx_multicast, &es->tx_multicast); 741 i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx), 742 veb->stat_offsets_loaded, 743 &oes->tx_broadcast, &es->tx_broadcast); 744 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) { 745 i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx), 746 I40E_GLVEBTC_RPCL(i, idx), 747 veb->stat_offsets_loaded, 748 &veb_oes->tc_rx_packets[i], 749 &veb_es->tc_rx_packets[i]); 750 i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx), 751 I40E_GLVEBTC_RBCL(i, idx), 752 veb->stat_offsets_loaded, 753 &veb_oes->tc_rx_bytes[i], 754 &veb_es->tc_rx_bytes[i]); 755 i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx), 756 I40E_GLVEBTC_TPCL(i, idx), 757 veb->stat_offsets_loaded, 758 &veb_oes->tc_tx_packets[i], 759 &veb_es->tc_tx_packets[i]); 760 i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx), 761 I40E_GLVEBTC_TBCL(i, idx), 762 veb->stat_offsets_loaded, 763 &veb_oes->tc_tx_bytes[i], 764 &veb_es->tc_tx_bytes[i]); 765 } 766 veb->stat_offsets_loaded = true; 767 } 768 769 /** 770 * i40e_update_vsi_stats - Update the vsi statistics counters. 771 * @vsi: the VSI to be updated 772 * 773 * There are a few instances where we store the same stat in a 774 * couple of different structs. This is partly because we have 775 * the netdev stats that need to be filled out, which is slightly 776 * different from the "eth_stats" defined by the chip and used in 777 * VF communications. We sort it out here. 778 **/ 779 static void i40e_update_vsi_stats(struct i40e_vsi *vsi) 780 { 781 struct i40e_pf *pf = vsi->back; 782 struct rtnl_link_stats64 *ons; 783 struct rtnl_link_stats64 *ns; /* netdev stats */ 784 struct i40e_eth_stats *oes; 785 struct i40e_eth_stats *es; /* device's eth stats */ 786 u32 tx_restart, tx_busy; 787 struct i40e_ring *p; 788 u32 rx_page, rx_buf; 789 u64 bytes, packets; 790 unsigned int start; 791 u64 tx_linearize; 792 u64 tx_force_wb; 793 u64 rx_p, rx_b; 794 u64 tx_p, tx_b; 795 u16 q; 796 797 if (test_bit(__I40E_VSI_DOWN, vsi->state) || 798 test_bit(__I40E_CONFIG_BUSY, pf->state)) 799 return; 800 801 ns = i40e_get_vsi_stats_struct(vsi); 802 ons = &vsi->net_stats_offsets; 803 es = &vsi->eth_stats; 804 oes = &vsi->eth_stats_offsets; 805 806 /* Gather up the netdev and vsi stats that the driver collects 807 * on the fly during packet processing 808 */ 809 rx_b = rx_p = 0; 810 tx_b = tx_p = 0; 811 tx_restart = tx_busy = tx_linearize = tx_force_wb = 0; 812 rx_page = 0; 813 rx_buf = 0; 814 rcu_read_lock(); 815 for (q = 0; q < vsi->num_queue_pairs; q++) { 816 /* locate Tx ring */ 817 p = READ_ONCE(vsi->tx_rings[q]); 818 819 do { 820 start = u64_stats_fetch_begin_irq(&p->syncp); 821 packets = p->stats.packets; 822 bytes = p->stats.bytes; 823 } while (u64_stats_fetch_retry_irq(&p->syncp, start)); 824 tx_b += bytes; 825 tx_p += packets; 826 tx_restart += p->tx_stats.restart_queue; 827 tx_busy += p->tx_stats.tx_busy; 828 tx_linearize += p->tx_stats.tx_linearize; 829 tx_force_wb += p->tx_stats.tx_force_wb; 830 831 /* Rx queue is part of the same block as Tx queue */ 832 p = &p[1]; 833 do { 834 start = u64_stats_fetch_begin_irq(&p->syncp); 835 packets = p->stats.packets; 836 bytes = p->stats.bytes; 837 } while (u64_stats_fetch_retry_irq(&p->syncp, start)); 838 rx_b += bytes; 839 rx_p += packets; 840 rx_buf += p->rx_stats.alloc_buff_failed; 841 rx_page += p->rx_stats.alloc_page_failed; 842 } 843 rcu_read_unlock(); 844 vsi->tx_restart = tx_restart; 845 vsi->tx_busy = tx_busy; 846 vsi->tx_linearize = tx_linearize; 847 vsi->tx_force_wb = tx_force_wb; 848 vsi->rx_page_failed = rx_page; 849 vsi->rx_buf_failed = rx_buf; 850 851 ns->rx_packets = rx_p; 852 ns->rx_bytes = rx_b; 853 ns->tx_packets = tx_p; 854 ns->tx_bytes = tx_b; 855 856 /* update netdev stats from eth stats */ 857 i40e_update_eth_stats(vsi); 858 ons->tx_errors = oes->tx_errors; 859 ns->tx_errors = es->tx_errors; 860 ons->multicast = oes->rx_multicast; 861 ns->multicast = es->rx_multicast; 862 ons->rx_dropped = oes->rx_discards; 863 ns->rx_dropped = es->rx_discards; 864 ons->tx_dropped = oes->tx_discards; 865 ns->tx_dropped = es->tx_discards; 866 867 /* pull in a couple PF stats if this is the main vsi */ 868 if (vsi == pf->vsi[pf->lan_vsi]) { 869 ns->rx_crc_errors = pf->stats.crc_errors; 870 ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes; 871 ns->rx_length_errors = pf->stats.rx_length_errors; 872 } 873 } 874 875 /** 876 * i40e_update_pf_stats - Update the PF statistics counters. 877 * @pf: the PF to be updated 878 **/ 879 static void i40e_update_pf_stats(struct i40e_pf *pf) 880 { 881 struct i40e_hw_port_stats *osd = &pf->stats_offsets; 882 struct i40e_hw_port_stats *nsd = &pf->stats; 883 struct i40e_hw *hw = &pf->hw; 884 u32 val; 885 int i; 886 887 i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port), 888 I40E_GLPRT_GORCL(hw->port), 889 pf->stat_offsets_loaded, 890 &osd->eth.rx_bytes, &nsd->eth.rx_bytes); 891 i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port), 892 I40E_GLPRT_GOTCL(hw->port), 893 pf->stat_offsets_loaded, 894 &osd->eth.tx_bytes, &nsd->eth.tx_bytes); 895 i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port), 896 pf->stat_offsets_loaded, 897 &osd->eth.rx_discards, 898 &nsd->eth.rx_discards); 899 i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port), 900 I40E_GLPRT_UPRCL(hw->port), 901 pf->stat_offsets_loaded, 902 &osd->eth.rx_unicast, 903 &nsd->eth.rx_unicast); 904 i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port), 905 I40E_GLPRT_MPRCL(hw->port), 906 pf->stat_offsets_loaded, 907 &osd->eth.rx_multicast, 908 &nsd->eth.rx_multicast); 909 i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port), 910 I40E_GLPRT_BPRCL(hw->port), 911 pf->stat_offsets_loaded, 912 &osd->eth.rx_broadcast, 913 &nsd->eth.rx_broadcast); 914 i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port), 915 I40E_GLPRT_UPTCL(hw->port), 916 pf->stat_offsets_loaded, 917 &osd->eth.tx_unicast, 918 &nsd->eth.tx_unicast); 919 i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port), 920 I40E_GLPRT_MPTCL(hw->port), 921 pf->stat_offsets_loaded, 922 &osd->eth.tx_multicast, 923 &nsd->eth.tx_multicast); 924 i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port), 925 I40E_GLPRT_BPTCL(hw->port), 926 pf->stat_offsets_loaded, 927 &osd->eth.tx_broadcast, 928 &nsd->eth.tx_broadcast); 929 930 i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port), 931 pf->stat_offsets_loaded, 932 &osd->tx_dropped_link_down, 933 &nsd->tx_dropped_link_down); 934 935 i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port), 936 pf->stat_offsets_loaded, 937 &osd->crc_errors, &nsd->crc_errors); 938 939 i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port), 940 pf->stat_offsets_loaded, 941 &osd->illegal_bytes, &nsd->illegal_bytes); 942 943 i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port), 944 pf->stat_offsets_loaded, 945 &osd->mac_local_faults, 946 &nsd->mac_local_faults); 947 i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port), 948 pf->stat_offsets_loaded, 949 &osd->mac_remote_faults, 950 &nsd->mac_remote_faults); 951 952 i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port), 953 pf->stat_offsets_loaded, 954 &osd->rx_length_errors, 955 &nsd->rx_length_errors); 956 957 i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port), 958 pf->stat_offsets_loaded, 959 &osd->link_xon_rx, &nsd->link_xon_rx); 960 i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port), 961 pf->stat_offsets_loaded, 962 &osd->link_xon_tx, &nsd->link_xon_tx); 963 i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port), 964 pf->stat_offsets_loaded, 965 &osd->link_xoff_rx, &nsd->link_xoff_rx); 966 i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port), 967 pf->stat_offsets_loaded, 968 &osd->link_xoff_tx, &nsd->link_xoff_tx); 969 970 for (i = 0; i < 8; i++) { 971 i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i), 972 pf->stat_offsets_loaded, 973 &osd->priority_xoff_rx[i], 974 &nsd->priority_xoff_rx[i]); 975 i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i), 976 pf->stat_offsets_loaded, 977 &osd->priority_xon_rx[i], 978 &nsd->priority_xon_rx[i]); 979 i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i), 980 pf->stat_offsets_loaded, 981 &osd->priority_xon_tx[i], 982 &nsd->priority_xon_tx[i]); 983 i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i), 984 pf->stat_offsets_loaded, 985 &osd->priority_xoff_tx[i], 986 &nsd->priority_xoff_tx[i]); 987 i40e_stat_update32(hw, 988 I40E_GLPRT_RXON2OFFCNT(hw->port, i), 989 pf->stat_offsets_loaded, 990 &osd->priority_xon_2_xoff[i], 991 &nsd->priority_xon_2_xoff[i]); 992 } 993 994 i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port), 995 I40E_GLPRT_PRC64L(hw->port), 996 pf->stat_offsets_loaded, 997 &osd->rx_size_64, &nsd->rx_size_64); 998 i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port), 999 I40E_GLPRT_PRC127L(hw->port), 1000 pf->stat_offsets_loaded, 1001 &osd->rx_size_127, &nsd->rx_size_127); 1002 i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port), 1003 I40E_GLPRT_PRC255L(hw->port), 1004 pf->stat_offsets_loaded, 1005 &osd->rx_size_255, &nsd->rx_size_255); 1006 i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port), 1007 I40E_GLPRT_PRC511L(hw->port), 1008 pf->stat_offsets_loaded, 1009 &osd->rx_size_511, &nsd->rx_size_511); 1010 i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port), 1011 I40E_GLPRT_PRC1023L(hw->port), 1012 pf->stat_offsets_loaded, 1013 &osd->rx_size_1023, &nsd->rx_size_1023); 1014 i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port), 1015 I40E_GLPRT_PRC1522L(hw->port), 1016 pf->stat_offsets_loaded, 1017 &osd->rx_size_1522, &nsd->rx_size_1522); 1018 i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port), 1019 I40E_GLPRT_PRC9522L(hw->port), 1020 pf->stat_offsets_loaded, 1021 &osd->rx_size_big, &nsd->rx_size_big); 1022 1023 i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port), 1024 I40E_GLPRT_PTC64L(hw->port), 1025 pf->stat_offsets_loaded, 1026 &osd->tx_size_64, &nsd->tx_size_64); 1027 i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port), 1028 I40E_GLPRT_PTC127L(hw->port), 1029 pf->stat_offsets_loaded, 1030 &osd->tx_size_127, &nsd->tx_size_127); 1031 i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port), 1032 I40E_GLPRT_PTC255L(hw->port), 1033 pf->stat_offsets_loaded, 1034 &osd->tx_size_255, &nsd->tx_size_255); 1035 i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port), 1036 I40E_GLPRT_PTC511L(hw->port), 1037 pf->stat_offsets_loaded, 1038 &osd->tx_size_511, &nsd->tx_size_511); 1039 i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port), 1040 I40E_GLPRT_PTC1023L(hw->port), 1041 pf->stat_offsets_loaded, 1042 &osd->tx_size_1023, &nsd->tx_size_1023); 1043 i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port), 1044 I40E_GLPRT_PTC1522L(hw->port), 1045 pf->stat_offsets_loaded, 1046 &osd->tx_size_1522, &nsd->tx_size_1522); 1047 i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port), 1048 I40E_GLPRT_PTC9522L(hw->port), 1049 pf->stat_offsets_loaded, 1050 &osd->tx_size_big, &nsd->tx_size_big); 1051 1052 i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port), 1053 pf->stat_offsets_loaded, 1054 &osd->rx_undersize, &nsd->rx_undersize); 1055 i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port), 1056 pf->stat_offsets_loaded, 1057 &osd->rx_fragments, &nsd->rx_fragments); 1058 i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port), 1059 pf->stat_offsets_loaded, 1060 &osd->rx_oversize, &nsd->rx_oversize); 1061 i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port), 1062 pf->stat_offsets_loaded, 1063 &osd->rx_jabber, &nsd->rx_jabber); 1064 1065 /* FDIR stats */ 1066 i40e_stat_update_and_clear32(hw, 1067 I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(hw->pf_id)), 1068 &nsd->fd_atr_match); 1069 i40e_stat_update_and_clear32(hw, 1070 I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(hw->pf_id)), 1071 &nsd->fd_sb_match); 1072 i40e_stat_update_and_clear32(hw, 1073 I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(hw->pf_id)), 1074 &nsd->fd_atr_tunnel_match); 1075 1076 val = rd32(hw, I40E_PRTPM_EEE_STAT); 1077 nsd->tx_lpi_status = 1078 (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >> 1079 I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT; 1080 nsd->rx_lpi_status = 1081 (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >> 1082 I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT; 1083 i40e_stat_update32(hw, I40E_PRTPM_TLPIC, 1084 pf->stat_offsets_loaded, 1085 &osd->tx_lpi_count, &nsd->tx_lpi_count); 1086 i40e_stat_update32(hw, I40E_PRTPM_RLPIC, 1087 pf->stat_offsets_loaded, 1088 &osd->rx_lpi_count, &nsd->rx_lpi_count); 1089 1090 if (pf->flags & I40E_FLAG_FD_SB_ENABLED && 1091 !(pf->flags & I40E_FLAG_FD_SB_AUTO_DISABLED)) 1092 nsd->fd_sb_status = true; 1093 else 1094 nsd->fd_sb_status = false; 1095 1096 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED && 1097 !(pf->flags & I40E_FLAG_FD_ATR_AUTO_DISABLED)) 1098 nsd->fd_atr_status = true; 1099 else 1100 nsd->fd_atr_status = false; 1101 1102 pf->stat_offsets_loaded = true; 1103 } 1104 1105 /** 1106 * i40e_update_stats - Update the various statistics counters. 1107 * @vsi: the VSI to be updated 1108 * 1109 * Update the various stats for this VSI and its related entities. 1110 **/ 1111 void i40e_update_stats(struct i40e_vsi *vsi) 1112 { 1113 struct i40e_pf *pf = vsi->back; 1114 1115 if (vsi == pf->vsi[pf->lan_vsi]) 1116 i40e_update_pf_stats(pf); 1117 1118 i40e_update_vsi_stats(vsi); 1119 } 1120 1121 /** 1122 * i40e_find_filter - Search VSI filter list for specific mac/vlan filter 1123 * @vsi: the VSI to be searched 1124 * @macaddr: the MAC address 1125 * @vlan: the vlan 1126 * 1127 * Returns ptr to the filter object or NULL 1128 **/ 1129 static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi, 1130 const u8 *macaddr, s16 vlan) 1131 { 1132 struct i40e_mac_filter *f; 1133 u64 key; 1134 1135 if (!vsi || !macaddr) 1136 return NULL; 1137 1138 key = i40e_addr_to_hkey(macaddr); 1139 hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) { 1140 if ((ether_addr_equal(macaddr, f->macaddr)) && 1141 (vlan == f->vlan)) 1142 return f; 1143 } 1144 return NULL; 1145 } 1146 1147 /** 1148 * i40e_find_mac - Find a mac addr in the macvlan filters list 1149 * @vsi: the VSI to be searched 1150 * @macaddr: the MAC address we are searching for 1151 * 1152 * Returns the first filter with the provided MAC address or NULL if 1153 * MAC address was not found 1154 **/ 1155 struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr) 1156 { 1157 struct i40e_mac_filter *f; 1158 u64 key; 1159 1160 if (!vsi || !macaddr) 1161 return NULL; 1162 1163 key = i40e_addr_to_hkey(macaddr); 1164 hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) { 1165 if ((ether_addr_equal(macaddr, f->macaddr))) 1166 return f; 1167 } 1168 return NULL; 1169 } 1170 1171 /** 1172 * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode 1173 * @vsi: the VSI to be searched 1174 * 1175 * Returns true if VSI is in vlan mode or false otherwise 1176 **/ 1177 bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi) 1178 { 1179 /* If we have a PVID, always operate in VLAN mode */ 1180 if (vsi->info.pvid) 1181 return true; 1182 1183 /* We need to operate in VLAN mode whenever we have any filters with 1184 * a VLAN other than I40E_VLAN_ALL. We could check the table each 1185 * time, incurring search cost repeatedly. However, we can notice two 1186 * things: 1187 * 1188 * 1) the only place where we can gain a VLAN filter is in 1189 * i40e_add_filter. 1190 * 1191 * 2) the only place where filters are actually removed is in 1192 * i40e_sync_filters_subtask. 1193 * 1194 * Thus, we can simply use a boolean value, has_vlan_filters which we 1195 * will set to true when we add a VLAN filter in i40e_add_filter. Then 1196 * we have to perform the full search after deleting filters in 1197 * i40e_sync_filters_subtask, but we already have to search 1198 * filters here and can perform the check at the same time. This 1199 * results in avoiding embedding a loop for VLAN mode inside another 1200 * loop over all the filters, and should maintain correctness as noted 1201 * above. 1202 */ 1203 return vsi->has_vlan_filter; 1204 } 1205 1206 /** 1207 * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary 1208 * @vsi: the VSI to configure 1209 * @tmp_add_list: list of filters ready to be added 1210 * @tmp_del_list: list of filters ready to be deleted 1211 * @vlan_filters: the number of active VLAN filters 1212 * 1213 * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they 1214 * behave as expected. If we have any active VLAN filters remaining or about 1215 * to be added then we need to update non-VLAN filters to be marked as VLAN=0 1216 * so that they only match against untagged traffic. If we no longer have any 1217 * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1 1218 * so that they match against both tagged and untagged traffic. In this way, 1219 * we ensure that we correctly receive the desired traffic. This ensures that 1220 * when we have an active VLAN we will receive only untagged traffic and 1221 * traffic matching active VLANs. If we have no active VLANs then we will 1222 * operate in non-VLAN mode and receive all traffic, tagged or untagged. 1223 * 1224 * Finally, in a similar fashion, this function also corrects filters when 1225 * there is an active PVID assigned to this VSI. 1226 * 1227 * In case of memory allocation failure return -ENOMEM. Otherwise, return 0. 1228 * 1229 * This function is only expected to be called from within 1230 * i40e_sync_vsi_filters. 1231 * 1232 * NOTE: This function expects to be called while under the 1233 * mac_filter_hash_lock 1234 */ 1235 static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi, 1236 struct hlist_head *tmp_add_list, 1237 struct hlist_head *tmp_del_list, 1238 int vlan_filters) 1239 { 1240 s16 pvid = le16_to_cpu(vsi->info.pvid); 1241 struct i40e_mac_filter *f, *add_head; 1242 struct i40e_new_mac_filter *new; 1243 struct hlist_node *h; 1244 int bkt, new_vlan; 1245 1246 /* To determine if a particular filter needs to be replaced we 1247 * have the three following conditions: 1248 * 1249 * a) if we have a PVID assigned, then all filters which are 1250 * not marked as VLAN=PVID must be replaced with filters that 1251 * are. 1252 * b) otherwise, if we have any active VLANS, all filters 1253 * which are marked as VLAN=-1 must be replaced with 1254 * filters marked as VLAN=0 1255 * c) finally, if we do not have any active VLANS, all filters 1256 * which are marked as VLAN=0 must be replaced with filters 1257 * marked as VLAN=-1 1258 */ 1259 1260 /* Update the filters about to be added in place */ 1261 hlist_for_each_entry(new, tmp_add_list, hlist) { 1262 if (pvid && new->f->vlan != pvid) 1263 new->f->vlan = pvid; 1264 else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY) 1265 new->f->vlan = 0; 1266 else if (!vlan_filters && new->f->vlan == 0) 1267 new->f->vlan = I40E_VLAN_ANY; 1268 } 1269 1270 /* Update the remaining active filters */ 1271 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) { 1272 /* Combine the checks for whether a filter needs to be changed 1273 * and then determine the new VLAN inside the if block, in 1274 * order to avoid duplicating code for adding the new filter 1275 * then deleting the old filter. 1276 */ 1277 if ((pvid && f->vlan != pvid) || 1278 (vlan_filters && f->vlan == I40E_VLAN_ANY) || 1279 (!vlan_filters && f->vlan == 0)) { 1280 /* Determine the new vlan we will be adding */ 1281 if (pvid) 1282 new_vlan = pvid; 1283 else if (vlan_filters) 1284 new_vlan = 0; 1285 else 1286 new_vlan = I40E_VLAN_ANY; 1287 1288 /* Create the new filter */ 1289 add_head = i40e_add_filter(vsi, f->macaddr, new_vlan); 1290 if (!add_head) 1291 return -ENOMEM; 1292 1293 /* Create a temporary i40e_new_mac_filter */ 1294 new = kzalloc(sizeof(*new), GFP_ATOMIC); 1295 if (!new) 1296 return -ENOMEM; 1297 1298 new->f = add_head; 1299 new->state = add_head->state; 1300 1301 /* Add the new filter to the tmp list */ 1302 hlist_add_head(&new->hlist, tmp_add_list); 1303 1304 /* Put the original filter into the delete list */ 1305 f->state = I40E_FILTER_REMOVE; 1306 hash_del(&f->hlist); 1307 hlist_add_head(&f->hlist, tmp_del_list); 1308 } 1309 } 1310 1311 vsi->has_vlan_filter = !!vlan_filters; 1312 1313 return 0; 1314 } 1315 1316 /** 1317 * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM 1318 * @vsi: the PF Main VSI - inappropriate for any other VSI 1319 * @macaddr: the MAC address 1320 * 1321 * Remove whatever filter the firmware set up so the driver can manage 1322 * its own filtering intelligently. 1323 **/ 1324 static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr) 1325 { 1326 struct i40e_aqc_remove_macvlan_element_data element; 1327 struct i40e_pf *pf = vsi->back; 1328 1329 /* Only appropriate for the PF main VSI */ 1330 if (vsi->type != I40E_VSI_MAIN) 1331 return; 1332 1333 memset(&element, 0, sizeof(element)); 1334 ether_addr_copy(element.mac_addr, macaddr); 1335 element.vlan_tag = 0; 1336 /* Ignore error returns, some firmware does it this way... */ 1337 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH; 1338 i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL); 1339 1340 memset(&element, 0, sizeof(element)); 1341 ether_addr_copy(element.mac_addr, macaddr); 1342 element.vlan_tag = 0; 1343 /* ...and some firmware does it this way. */ 1344 element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH | 1345 I40E_AQC_MACVLAN_DEL_IGNORE_VLAN; 1346 i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL); 1347 } 1348 1349 /** 1350 * i40e_add_filter - Add a mac/vlan filter to the VSI 1351 * @vsi: the VSI to be searched 1352 * @macaddr: the MAC address 1353 * @vlan: the vlan 1354 * 1355 * Returns ptr to the filter object or NULL when no memory available. 1356 * 1357 * NOTE: This function is expected to be called with mac_filter_hash_lock 1358 * being held. 1359 **/ 1360 struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi, 1361 const u8 *macaddr, s16 vlan) 1362 { 1363 struct i40e_mac_filter *f; 1364 u64 key; 1365 1366 if (!vsi || !macaddr) 1367 return NULL; 1368 1369 f = i40e_find_filter(vsi, macaddr, vlan); 1370 if (!f) { 1371 f = kzalloc(sizeof(*f), GFP_ATOMIC); 1372 if (!f) 1373 return NULL; 1374 1375 /* Update the boolean indicating if we need to function in 1376 * VLAN mode. 1377 */ 1378 if (vlan >= 0) 1379 vsi->has_vlan_filter = true; 1380 1381 ether_addr_copy(f->macaddr, macaddr); 1382 f->vlan = vlan; 1383 f->state = I40E_FILTER_NEW; 1384 INIT_HLIST_NODE(&f->hlist); 1385 1386 key = i40e_addr_to_hkey(macaddr); 1387 hash_add(vsi->mac_filter_hash, &f->hlist, key); 1388 1389 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED; 1390 vsi->back->flags |= I40E_FLAG_FILTER_SYNC; 1391 } 1392 1393 /* If we're asked to add a filter that has been marked for removal, it 1394 * is safe to simply restore it to active state. __i40e_del_filter 1395 * will have simply deleted any filters which were previously marked 1396 * NEW or FAILED, so if it is currently marked REMOVE it must have 1397 * previously been ACTIVE. Since we haven't yet run the sync filters 1398 * task, just restore this filter to the ACTIVE state so that the 1399 * sync task leaves it in place 1400 */ 1401 if (f->state == I40E_FILTER_REMOVE) 1402 f->state = I40E_FILTER_ACTIVE; 1403 1404 return f; 1405 } 1406 1407 /** 1408 * __i40e_del_filter - Remove a specific filter from the VSI 1409 * @vsi: VSI to remove from 1410 * @f: the filter to remove from the list 1411 * 1412 * This function should be called instead of i40e_del_filter only if you know 1413 * the exact filter you will remove already, such as via i40e_find_filter or 1414 * i40e_find_mac. 1415 * 1416 * NOTE: This function is expected to be called with mac_filter_hash_lock 1417 * being held. 1418 * ANOTHER NOTE: This function MUST be called from within the context of 1419 * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe() 1420 * instead of list_for_each_entry(). 1421 **/ 1422 void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f) 1423 { 1424 if (!f) 1425 return; 1426 1427 /* If the filter was never added to firmware then we can just delete it 1428 * directly and we don't want to set the status to remove or else an 1429 * admin queue command will unnecessarily fire. 1430 */ 1431 if ((f->state == I40E_FILTER_FAILED) || 1432 (f->state == I40E_FILTER_NEW)) { 1433 hash_del(&f->hlist); 1434 kfree(f); 1435 } else { 1436 f->state = I40E_FILTER_REMOVE; 1437 } 1438 1439 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED; 1440 vsi->back->flags |= I40E_FLAG_FILTER_SYNC; 1441 } 1442 1443 /** 1444 * i40e_del_filter - Remove a MAC/VLAN filter from the VSI 1445 * @vsi: the VSI to be searched 1446 * @macaddr: the MAC address 1447 * @vlan: the VLAN 1448 * 1449 * NOTE: This function is expected to be called with mac_filter_hash_lock 1450 * being held. 1451 * ANOTHER NOTE: This function MUST be called from within the context of 1452 * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe() 1453 * instead of list_for_each_entry(). 1454 **/ 1455 void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan) 1456 { 1457 struct i40e_mac_filter *f; 1458 1459 if (!vsi || !macaddr) 1460 return; 1461 1462 f = i40e_find_filter(vsi, macaddr, vlan); 1463 __i40e_del_filter(vsi, f); 1464 } 1465 1466 /** 1467 * i40e_add_mac_filter - Add a MAC filter for all active VLANs 1468 * @vsi: the VSI to be searched 1469 * @macaddr: the mac address to be filtered 1470 * 1471 * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise, 1472 * go through all the macvlan filters and add a macvlan filter for each 1473 * unique vlan that already exists. If a PVID has been assigned, instead only 1474 * add the macaddr to that VLAN. 1475 * 1476 * Returns last filter added on success, else NULL 1477 **/ 1478 struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi, 1479 const u8 *macaddr) 1480 { 1481 struct i40e_mac_filter *f, *add = NULL; 1482 struct hlist_node *h; 1483 int bkt; 1484 1485 if (vsi->info.pvid) 1486 return i40e_add_filter(vsi, macaddr, 1487 le16_to_cpu(vsi->info.pvid)); 1488 1489 if (!i40e_is_vsi_in_vlan(vsi)) 1490 return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY); 1491 1492 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) { 1493 if (f->state == I40E_FILTER_REMOVE) 1494 continue; 1495 add = i40e_add_filter(vsi, macaddr, f->vlan); 1496 if (!add) 1497 return NULL; 1498 } 1499 1500 return add; 1501 } 1502 1503 /** 1504 * i40e_del_mac_filter - Remove a MAC filter from all VLANs 1505 * @vsi: the VSI to be searched 1506 * @macaddr: the mac address to be removed 1507 * 1508 * Removes a given MAC address from a VSI regardless of what VLAN it has been 1509 * associated with. 1510 * 1511 * Returns 0 for success, or error 1512 **/ 1513 int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr) 1514 { 1515 struct i40e_mac_filter *f; 1516 struct hlist_node *h; 1517 bool found = false; 1518 int bkt; 1519 1520 WARN(!spin_is_locked(&vsi->mac_filter_hash_lock), 1521 "Missing mac_filter_hash_lock\n"); 1522 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) { 1523 if (ether_addr_equal(macaddr, f->macaddr)) { 1524 __i40e_del_filter(vsi, f); 1525 found = true; 1526 } 1527 } 1528 1529 if (found) 1530 return 0; 1531 else 1532 return -ENOENT; 1533 } 1534 1535 /** 1536 * i40e_set_mac - NDO callback to set mac address 1537 * @netdev: network interface device structure 1538 * @p: pointer to an address structure 1539 * 1540 * Returns 0 on success, negative on failure 1541 **/ 1542 static int i40e_set_mac(struct net_device *netdev, void *p) 1543 { 1544 struct i40e_netdev_priv *np = netdev_priv(netdev); 1545 struct i40e_vsi *vsi = np->vsi; 1546 struct i40e_pf *pf = vsi->back; 1547 struct i40e_hw *hw = &pf->hw; 1548 struct sockaddr *addr = p; 1549 1550 if (!is_valid_ether_addr(addr->sa_data)) 1551 return -EADDRNOTAVAIL; 1552 1553 if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) { 1554 netdev_info(netdev, "already using mac address %pM\n", 1555 addr->sa_data); 1556 return 0; 1557 } 1558 1559 if (test_bit(__I40E_VSI_DOWN, vsi->back->state) || 1560 test_bit(__I40E_RESET_RECOVERY_PENDING, vsi->back->state)) 1561 return -EADDRNOTAVAIL; 1562 1563 if (ether_addr_equal(hw->mac.addr, addr->sa_data)) 1564 netdev_info(netdev, "returning to hw mac address %pM\n", 1565 hw->mac.addr); 1566 else 1567 netdev_info(netdev, "set new mac address %pM\n", addr->sa_data); 1568 1569 /* Copy the address first, so that we avoid a possible race with 1570 * .set_rx_mode(). If we copy after changing the address in the filter 1571 * list, we might open ourselves to a narrow race window where 1572 * .set_rx_mode could delete our dev_addr filter and prevent traffic 1573 * from passing. 1574 */ 1575 ether_addr_copy(netdev->dev_addr, addr->sa_data); 1576 1577 spin_lock_bh(&vsi->mac_filter_hash_lock); 1578 i40e_del_mac_filter(vsi, netdev->dev_addr); 1579 i40e_add_mac_filter(vsi, addr->sa_data); 1580 spin_unlock_bh(&vsi->mac_filter_hash_lock); 1581 if (vsi->type == I40E_VSI_MAIN) { 1582 i40e_status ret; 1583 1584 ret = i40e_aq_mac_address_write(&vsi->back->hw, 1585 I40E_AQC_WRITE_TYPE_LAA_WOL, 1586 addr->sa_data, NULL); 1587 if (ret) 1588 netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n", 1589 i40e_stat_str(hw, ret), 1590 i40e_aq_str(hw, hw->aq.asq_last_status)); 1591 } 1592 1593 /* schedule our worker thread which will take care of 1594 * applying the new filter changes 1595 */ 1596 i40e_service_event_schedule(vsi->back); 1597 return 0; 1598 } 1599 1600 /** 1601 * i40e_config_rss_aq - Prepare for RSS using AQ commands 1602 * @vsi: vsi structure 1603 * @seed: RSS hash seed 1604 **/ 1605 static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed, 1606 u8 *lut, u16 lut_size) 1607 { 1608 struct i40e_pf *pf = vsi->back; 1609 struct i40e_hw *hw = &pf->hw; 1610 int ret = 0; 1611 1612 if (seed) { 1613 struct i40e_aqc_get_set_rss_key_data *seed_dw = 1614 (struct i40e_aqc_get_set_rss_key_data *)seed; 1615 ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw); 1616 if (ret) { 1617 dev_info(&pf->pdev->dev, 1618 "Cannot set RSS key, err %s aq_err %s\n", 1619 i40e_stat_str(hw, ret), 1620 i40e_aq_str(hw, hw->aq.asq_last_status)); 1621 return ret; 1622 } 1623 } 1624 if (lut) { 1625 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false; 1626 1627 ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size); 1628 if (ret) { 1629 dev_info(&pf->pdev->dev, 1630 "Cannot set RSS lut, err %s aq_err %s\n", 1631 i40e_stat_str(hw, ret), 1632 i40e_aq_str(hw, hw->aq.asq_last_status)); 1633 return ret; 1634 } 1635 } 1636 return ret; 1637 } 1638 1639 /** 1640 * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used 1641 * @vsi: VSI structure 1642 **/ 1643 static int i40e_vsi_config_rss(struct i40e_vsi *vsi) 1644 { 1645 struct i40e_pf *pf = vsi->back; 1646 u8 seed[I40E_HKEY_ARRAY_SIZE]; 1647 u8 *lut; 1648 int ret; 1649 1650 if (!(pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)) 1651 return 0; 1652 if (!vsi->rss_size) 1653 vsi->rss_size = min_t(int, pf->alloc_rss_size, 1654 vsi->num_queue_pairs); 1655 if (!vsi->rss_size) 1656 return -EINVAL; 1657 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL); 1658 if (!lut) 1659 return -ENOMEM; 1660 1661 /* Use the user configured hash keys and lookup table if there is one, 1662 * otherwise use default 1663 */ 1664 if (vsi->rss_lut_user) 1665 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size); 1666 else 1667 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size); 1668 if (vsi->rss_hkey_user) 1669 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE); 1670 else 1671 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE); 1672 ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size); 1673 kfree(lut); 1674 return ret; 1675 } 1676 1677 /** 1678 * i40e_vsi_setup_queue_map_mqprio - Prepares mqprio based tc_config 1679 * @vsi: the VSI being configured, 1680 * @ctxt: VSI context structure 1681 * @enabled_tc: number of traffic classes to enable 1682 * 1683 * Prepares VSI tc_config to have queue configurations based on MQPRIO options. 1684 **/ 1685 static int i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi *vsi, 1686 struct i40e_vsi_context *ctxt, 1687 u8 enabled_tc) 1688 { 1689 u16 qcount = 0, max_qcount, qmap, sections = 0; 1690 int i, override_q, pow, num_qps, ret; 1691 u8 netdev_tc = 0, offset = 0; 1692 1693 if (vsi->type != I40E_VSI_MAIN) 1694 return -EINVAL; 1695 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID; 1696 sections |= I40E_AQ_VSI_PROP_SCHED_VALID; 1697 vsi->tc_config.numtc = vsi->mqprio_qopt.qopt.num_tc; 1698 vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1; 1699 num_qps = vsi->mqprio_qopt.qopt.count[0]; 1700 1701 /* find the next higher power-of-2 of num queue pairs */ 1702 pow = ilog2(num_qps); 1703 if (!is_power_of_2(num_qps)) 1704 pow++; 1705 qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) | 1706 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT); 1707 1708 /* Setup queue offset/count for all TCs for given VSI */ 1709 max_qcount = vsi->mqprio_qopt.qopt.count[0]; 1710 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) { 1711 /* See if the given TC is enabled for the given VSI */ 1712 if (vsi->tc_config.enabled_tc & BIT(i)) { 1713 offset = vsi->mqprio_qopt.qopt.offset[i]; 1714 qcount = vsi->mqprio_qopt.qopt.count[i]; 1715 if (qcount > max_qcount) 1716 max_qcount = qcount; 1717 vsi->tc_config.tc_info[i].qoffset = offset; 1718 vsi->tc_config.tc_info[i].qcount = qcount; 1719 vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++; 1720 } else { 1721 /* TC is not enabled so set the offset to 1722 * default queue and allocate one queue 1723 * for the given TC. 1724 */ 1725 vsi->tc_config.tc_info[i].qoffset = 0; 1726 vsi->tc_config.tc_info[i].qcount = 1; 1727 vsi->tc_config.tc_info[i].netdev_tc = 0; 1728 } 1729 } 1730 1731 /* Set actual Tx/Rx queue pairs */ 1732 vsi->num_queue_pairs = offset + qcount; 1733 1734 /* Setup queue TC[0].qmap for given VSI context */ 1735 ctxt->info.tc_mapping[0] = cpu_to_le16(qmap); 1736 ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG); 1737 ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue); 1738 ctxt->info.valid_sections |= cpu_to_le16(sections); 1739 1740 /* Reconfigure RSS for main VSI with max queue count */ 1741 vsi->rss_size = max_qcount; 1742 ret = i40e_vsi_config_rss(vsi); 1743 if (ret) { 1744 dev_info(&vsi->back->pdev->dev, 1745 "Failed to reconfig rss for num_queues (%u)\n", 1746 max_qcount); 1747 return ret; 1748 } 1749 vsi->reconfig_rss = true; 1750 dev_dbg(&vsi->back->pdev->dev, 1751 "Reconfigured rss with num_queues (%u)\n", max_qcount); 1752 1753 /* Find queue count available for channel VSIs and starting offset 1754 * for channel VSIs 1755 */ 1756 override_q = vsi->mqprio_qopt.qopt.count[0]; 1757 if (override_q && override_q < vsi->num_queue_pairs) { 1758 vsi->cnt_q_avail = vsi->num_queue_pairs - override_q; 1759 vsi->next_base_queue = override_q; 1760 } 1761 return 0; 1762 } 1763 1764 /** 1765 * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc 1766 * @vsi: the VSI being setup 1767 * @ctxt: VSI context structure 1768 * @enabled_tc: Enabled TCs bitmap 1769 * @is_add: True if called before Add VSI 1770 * 1771 * Setup VSI queue mapping for enabled traffic classes. 1772 **/ 1773 static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi, 1774 struct i40e_vsi_context *ctxt, 1775 u8 enabled_tc, 1776 bool is_add) 1777 { 1778 struct i40e_pf *pf = vsi->back; 1779 u16 sections = 0; 1780 u8 netdev_tc = 0; 1781 u16 numtc = 1; 1782 u16 qcount; 1783 u8 offset; 1784 u16 qmap; 1785 int i; 1786 u16 num_tc_qps = 0; 1787 1788 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID; 1789 offset = 0; 1790 1791 /* Number of queues per enabled TC */ 1792 num_tc_qps = vsi->alloc_queue_pairs; 1793 if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) { 1794 /* Find numtc from enabled TC bitmap */ 1795 for (i = 0, numtc = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) { 1796 if (enabled_tc & BIT(i)) /* TC is enabled */ 1797 numtc++; 1798 } 1799 if (!numtc) { 1800 dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n"); 1801 numtc = 1; 1802 } 1803 num_tc_qps = num_tc_qps / numtc; 1804 num_tc_qps = min_t(int, num_tc_qps, 1805 i40e_pf_get_max_q_per_tc(pf)); 1806 } 1807 1808 vsi->tc_config.numtc = numtc; 1809 vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1; 1810 1811 /* Do not allow use more TC queue pairs than MSI-X vectors exist */ 1812 if (pf->flags & I40E_FLAG_MSIX_ENABLED) 1813 num_tc_qps = min_t(int, num_tc_qps, pf->num_lan_msix); 1814 1815 /* Setup queue offset/count for all TCs for given VSI */ 1816 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) { 1817 /* See if the given TC is enabled for the given VSI */ 1818 if (vsi->tc_config.enabled_tc & BIT(i)) { 1819 /* TC is enabled */ 1820 int pow, num_qps; 1821 1822 switch (vsi->type) { 1823 case I40E_VSI_MAIN: 1824 if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED | 1825 I40E_FLAG_FD_ATR_ENABLED)) || 1826 vsi->tc_config.enabled_tc != 1) { 1827 qcount = min_t(int, pf->alloc_rss_size, 1828 num_tc_qps); 1829 break; 1830 } 1831 case I40E_VSI_FDIR: 1832 case I40E_VSI_SRIOV: 1833 case I40E_VSI_VMDQ2: 1834 default: 1835 qcount = num_tc_qps; 1836 WARN_ON(i != 0); 1837 break; 1838 } 1839 vsi->tc_config.tc_info[i].qoffset = offset; 1840 vsi->tc_config.tc_info[i].qcount = qcount; 1841 1842 /* find the next higher power-of-2 of num queue pairs */ 1843 num_qps = qcount; 1844 pow = 0; 1845 while (num_qps && (BIT_ULL(pow) < qcount)) { 1846 pow++; 1847 num_qps >>= 1; 1848 } 1849 1850 vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++; 1851 qmap = 1852 (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) | 1853 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT); 1854 1855 offset += qcount; 1856 } else { 1857 /* TC is not enabled so set the offset to 1858 * default queue and allocate one queue 1859 * for the given TC. 1860 */ 1861 vsi->tc_config.tc_info[i].qoffset = 0; 1862 vsi->tc_config.tc_info[i].qcount = 1; 1863 vsi->tc_config.tc_info[i].netdev_tc = 0; 1864 1865 qmap = 0; 1866 } 1867 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap); 1868 } 1869 1870 /* Set actual Tx/Rx queue pairs */ 1871 vsi->num_queue_pairs = offset; 1872 if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) { 1873 if (vsi->req_queue_pairs > 0) 1874 vsi->num_queue_pairs = vsi->req_queue_pairs; 1875 else if (pf->flags & I40E_FLAG_MSIX_ENABLED) 1876 vsi->num_queue_pairs = pf->num_lan_msix; 1877 } 1878 1879 /* Scheduler section valid can only be set for ADD VSI */ 1880 if (is_add) { 1881 sections |= I40E_AQ_VSI_PROP_SCHED_VALID; 1882 1883 ctxt->info.up_enable_bits = enabled_tc; 1884 } 1885 if (vsi->type == I40E_VSI_SRIOV) { 1886 ctxt->info.mapping_flags |= 1887 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG); 1888 for (i = 0; i < vsi->num_queue_pairs; i++) 1889 ctxt->info.queue_mapping[i] = 1890 cpu_to_le16(vsi->base_queue + i); 1891 } else { 1892 ctxt->info.mapping_flags |= 1893 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG); 1894 ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue); 1895 } 1896 ctxt->info.valid_sections |= cpu_to_le16(sections); 1897 } 1898 1899 /** 1900 * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address 1901 * @netdev: the netdevice 1902 * @addr: address to add 1903 * 1904 * Called by __dev_(mc|uc)_sync when an address needs to be added. We call 1905 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock. 1906 */ 1907 static int i40e_addr_sync(struct net_device *netdev, const u8 *addr) 1908 { 1909 struct i40e_netdev_priv *np = netdev_priv(netdev); 1910 struct i40e_vsi *vsi = np->vsi; 1911 1912 if (i40e_add_mac_filter(vsi, addr)) 1913 return 0; 1914 else 1915 return -ENOMEM; 1916 } 1917 1918 /** 1919 * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address 1920 * @netdev: the netdevice 1921 * @addr: address to add 1922 * 1923 * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call 1924 * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock. 1925 */ 1926 static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr) 1927 { 1928 struct i40e_netdev_priv *np = netdev_priv(netdev); 1929 struct i40e_vsi *vsi = np->vsi; 1930 1931 /* Under some circumstances, we might receive a request to delete 1932 * our own device address from our uc list. Because we store the 1933 * device address in the VSI's MAC/VLAN filter list, we need to ignore 1934 * such requests and not delete our device address from this list. 1935 */ 1936 if (ether_addr_equal(addr, netdev->dev_addr)) 1937 return 0; 1938 1939 i40e_del_mac_filter(vsi, addr); 1940 1941 return 0; 1942 } 1943 1944 /** 1945 * i40e_set_rx_mode - NDO callback to set the netdev filters 1946 * @netdev: network interface device structure 1947 **/ 1948 static void i40e_set_rx_mode(struct net_device *netdev) 1949 { 1950 struct i40e_netdev_priv *np = netdev_priv(netdev); 1951 struct i40e_vsi *vsi = np->vsi; 1952 1953 spin_lock_bh(&vsi->mac_filter_hash_lock); 1954 1955 __dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync); 1956 __dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync); 1957 1958 spin_unlock_bh(&vsi->mac_filter_hash_lock); 1959 1960 /* check for other flag changes */ 1961 if (vsi->current_netdev_flags != vsi->netdev->flags) { 1962 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED; 1963 vsi->back->flags |= I40E_FLAG_FILTER_SYNC; 1964 } 1965 } 1966 1967 /** 1968 * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries 1969 * @vsi: Pointer to VSI struct 1970 * @from: Pointer to list which contains MAC filter entries - changes to 1971 * those entries needs to be undone. 1972 * 1973 * MAC filter entries from this list were slated for deletion. 1974 **/ 1975 static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi, 1976 struct hlist_head *from) 1977 { 1978 struct i40e_mac_filter *f; 1979 struct hlist_node *h; 1980 1981 hlist_for_each_entry_safe(f, h, from, hlist) { 1982 u64 key = i40e_addr_to_hkey(f->macaddr); 1983 1984 /* Move the element back into MAC filter list*/ 1985 hlist_del(&f->hlist); 1986 hash_add(vsi->mac_filter_hash, &f->hlist, key); 1987 } 1988 } 1989 1990 /** 1991 * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries 1992 * @vsi: Pointer to vsi struct 1993 * @from: Pointer to list which contains MAC filter entries - changes to 1994 * those entries needs to be undone. 1995 * 1996 * MAC filter entries from this list were slated for addition. 1997 **/ 1998 static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi, 1999 struct hlist_head *from) 2000 { 2001 struct i40e_new_mac_filter *new; 2002 struct hlist_node *h; 2003 2004 hlist_for_each_entry_safe(new, h, from, hlist) { 2005 /* We can simply free the wrapper structure */ 2006 hlist_del(&new->hlist); 2007 kfree(new); 2008 } 2009 } 2010 2011 /** 2012 * i40e_next_entry - Get the next non-broadcast filter from a list 2013 * @next: pointer to filter in list 2014 * 2015 * Returns the next non-broadcast filter in the list. Required so that we 2016 * ignore broadcast filters within the list, since these are not handled via 2017 * the normal firmware update path. 2018 */ 2019 static 2020 struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next) 2021 { 2022 hlist_for_each_entry_continue(next, hlist) { 2023 if (!is_broadcast_ether_addr(next->f->macaddr)) 2024 return next; 2025 } 2026 2027 return NULL; 2028 } 2029 2030 /** 2031 * i40e_update_filter_state - Update filter state based on return data 2032 * from firmware 2033 * @count: Number of filters added 2034 * @add_list: return data from fw 2035 * @head: pointer to first filter in current batch 2036 * 2037 * MAC filter entries from list were slated to be added to device. Returns 2038 * number of successful filters. Note that 0 does NOT mean success! 2039 **/ 2040 static int 2041 i40e_update_filter_state(int count, 2042 struct i40e_aqc_add_macvlan_element_data *add_list, 2043 struct i40e_new_mac_filter *add_head) 2044 { 2045 int retval = 0; 2046 int i; 2047 2048 for (i = 0; i < count; i++) { 2049 /* Always check status of each filter. We don't need to check 2050 * the firmware return status because we pre-set the filter 2051 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter 2052 * request to the adminq. Thus, if it no longer matches then 2053 * we know the filter is active. 2054 */ 2055 if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) { 2056 add_head->state = I40E_FILTER_FAILED; 2057 } else { 2058 add_head->state = I40E_FILTER_ACTIVE; 2059 retval++; 2060 } 2061 2062 add_head = i40e_next_filter(add_head); 2063 if (!add_head) 2064 break; 2065 } 2066 2067 return retval; 2068 } 2069 2070 /** 2071 * i40e_aqc_del_filters - Request firmware to delete a set of filters 2072 * @vsi: ptr to the VSI 2073 * @vsi_name: name to display in messages 2074 * @list: the list of filters to send to firmware 2075 * @num_del: the number of filters to delete 2076 * @retval: Set to -EIO on failure to delete 2077 * 2078 * Send a request to firmware via AdminQ to delete a set of filters. Uses 2079 * *retval instead of a return value so that success does not force ret_val to 2080 * be set to 0. This ensures that a sequence of calls to this function 2081 * preserve the previous value of *retval on successful delete. 2082 */ 2083 static 2084 void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name, 2085 struct i40e_aqc_remove_macvlan_element_data *list, 2086 int num_del, int *retval) 2087 { 2088 struct i40e_hw *hw = &vsi->back->hw; 2089 i40e_status aq_ret; 2090 int aq_err; 2091 2092 aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, list, num_del, NULL); 2093 aq_err = hw->aq.asq_last_status; 2094 2095 /* Explicitly ignore and do not report when firmware returns ENOENT */ 2096 if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) { 2097 *retval = -EIO; 2098 dev_info(&vsi->back->pdev->dev, 2099 "ignoring delete macvlan error on %s, err %s, aq_err %s\n", 2100 vsi_name, i40e_stat_str(hw, aq_ret), 2101 i40e_aq_str(hw, aq_err)); 2102 } 2103 } 2104 2105 /** 2106 * i40e_aqc_add_filters - Request firmware to add a set of filters 2107 * @vsi: ptr to the VSI 2108 * @vsi_name: name to display in messages 2109 * @list: the list of filters to send to firmware 2110 * @add_head: Position in the add hlist 2111 * @num_add: the number of filters to add 2112 * 2113 * Send a request to firmware via AdminQ to add a chunk of filters. Will set 2114 * __I40E_VSI_OVERFLOW_PROMISC bit in vsi->state if the firmware has run out of 2115 * space for more filters. 2116 */ 2117 static 2118 void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name, 2119 struct i40e_aqc_add_macvlan_element_data *list, 2120 struct i40e_new_mac_filter *add_head, 2121 int num_add) 2122 { 2123 struct i40e_hw *hw = &vsi->back->hw; 2124 int aq_err, fcnt; 2125 2126 i40e_aq_add_macvlan(hw, vsi->seid, list, num_add, NULL); 2127 aq_err = hw->aq.asq_last_status; 2128 fcnt = i40e_update_filter_state(num_add, list, add_head); 2129 2130 if (fcnt != num_add) { 2131 set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state); 2132 dev_warn(&vsi->back->pdev->dev, 2133 "Error %s adding RX filters on %s, promiscuous mode forced on\n", 2134 i40e_aq_str(hw, aq_err), 2135 vsi_name); 2136 } 2137 } 2138 2139 /** 2140 * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags 2141 * @vsi: pointer to the VSI 2142 * @f: filter data 2143 * 2144 * This function sets or clears the promiscuous broadcast flags for VLAN 2145 * filters in order to properly receive broadcast frames. Assumes that only 2146 * broadcast filters are passed. 2147 * 2148 * Returns status indicating success or failure; 2149 **/ 2150 static i40e_status 2151 i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name, 2152 struct i40e_mac_filter *f) 2153 { 2154 bool enable = f->state == I40E_FILTER_NEW; 2155 struct i40e_hw *hw = &vsi->back->hw; 2156 i40e_status aq_ret; 2157 2158 if (f->vlan == I40E_VLAN_ANY) { 2159 aq_ret = i40e_aq_set_vsi_broadcast(hw, 2160 vsi->seid, 2161 enable, 2162 NULL); 2163 } else { 2164 aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw, 2165 vsi->seid, 2166 enable, 2167 f->vlan, 2168 NULL); 2169 } 2170 2171 if (aq_ret) { 2172 set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state); 2173 dev_warn(&vsi->back->pdev->dev, 2174 "Error %s, forcing overflow promiscuous on %s\n", 2175 i40e_aq_str(hw, hw->aq.asq_last_status), 2176 vsi_name); 2177 } 2178 2179 return aq_ret; 2180 } 2181 2182 /** 2183 * i40e_set_promiscuous - set promiscuous mode 2184 * @pf: board private structure 2185 * @promisc: promisc on or off 2186 * 2187 * There are different ways of setting promiscuous mode on a PF depending on 2188 * what state/environment we're in. This identifies and sets it appropriately. 2189 * Returns 0 on success. 2190 **/ 2191 static int i40e_set_promiscuous(struct i40e_pf *pf, bool promisc) 2192 { 2193 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi]; 2194 struct i40e_hw *hw = &pf->hw; 2195 i40e_status aq_ret; 2196 2197 if (vsi->type == I40E_VSI_MAIN && 2198 pf->lan_veb != I40E_NO_VEB && 2199 !(pf->flags & I40E_FLAG_MFP_ENABLED)) { 2200 /* set defport ON for Main VSI instead of true promisc 2201 * this way we will get all unicast/multicast and VLAN 2202 * promisc behavior but will not get VF or VMDq traffic 2203 * replicated on the Main VSI. 2204 */ 2205 if (promisc) 2206 aq_ret = i40e_aq_set_default_vsi(hw, 2207 vsi->seid, 2208 NULL); 2209 else 2210 aq_ret = i40e_aq_clear_default_vsi(hw, 2211 vsi->seid, 2212 NULL); 2213 if (aq_ret) { 2214 dev_info(&pf->pdev->dev, 2215 "Set default VSI failed, err %s, aq_err %s\n", 2216 i40e_stat_str(hw, aq_ret), 2217 i40e_aq_str(hw, hw->aq.asq_last_status)); 2218 } 2219 } else { 2220 aq_ret = i40e_aq_set_vsi_unicast_promiscuous( 2221 hw, 2222 vsi->seid, 2223 promisc, NULL, 2224 true); 2225 if (aq_ret) { 2226 dev_info(&pf->pdev->dev, 2227 "set unicast promisc failed, err %s, aq_err %s\n", 2228 i40e_stat_str(hw, aq_ret), 2229 i40e_aq_str(hw, hw->aq.asq_last_status)); 2230 } 2231 aq_ret = i40e_aq_set_vsi_multicast_promiscuous( 2232 hw, 2233 vsi->seid, 2234 promisc, NULL); 2235 if (aq_ret) { 2236 dev_info(&pf->pdev->dev, 2237 "set multicast promisc failed, err %s, aq_err %s\n", 2238 i40e_stat_str(hw, aq_ret), 2239 i40e_aq_str(hw, hw->aq.asq_last_status)); 2240 } 2241 } 2242 2243 if (!aq_ret) 2244 pf->cur_promisc = promisc; 2245 2246 return aq_ret; 2247 } 2248 2249 /** 2250 * i40e_sync_vsi_filters - Update the VSI filter list to the HW 2251 * @vsi: ptr to the VSI 2252 * 2253 * Push any outstanding VSI filter changes through the AdminQ. 2254 * 2255 * Returns 0 or error value 2256 **/ 2257 int i40e_sync_vsi_filters(struct i40e_vsi *vsi) 2258 { 2259 struct hlist_head tmp_add_list, tmp_del_list; 2260 struct i40e_mac_filter *f; 2261 struct i40e_new_mac_filter *new, *add_head = NULL; 2262 struct i40e_hw *hw = &vsi->back->hw; 2263 bool old_overflow, new_overflow; 2264 unsigned int failed_filters = 0; 2265 unsigned int vlan_filters = 0; 2266 char vsi_name[16] = "PF"; 2267 int filter_list_len = 0; 2268 i40e_status aq_ret = 0; 2269 u32 changed_flags = 0; 2270 struct hlist_node *h; 2271 struct i40e_pf *pf; 2272 int num_add = 0; 2273 int num_del = 0; 2274 int retval = 0; 2275 u16 cmd_flags; 2276 int list_size; 2277 int bkt; 2278 2279 /* empty array typed pointers, kcalloc later */ 2280 struct i40e_aqc_add_macvlan_element_data *add_list; 2281 struct i40e_aqc_remove_macvlan_element_data *del_list; 2282 2283 while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state)) 2284 usleep_range(1000, 2000); 2285 pf = vsi->back; 2286 2287 old_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state); 2288 2289 if (vsi->netdev) { 2290 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags; 2291 vsi->current_netdev_flags = vsi->netdev->flags; 2292 } 2293 2294 INIT_HLIST_HEAD(&tmp_add_list); 2295 INIT_HLIST_HEAD(&tmp_del_list); 2296 2297 if (vsi->type == I40E_VSI_SRIOV) 2298 snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id); 2299 else if (vsi->type != I40E_VSI_MAIN) 2300 snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid); 2301 2302 if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) { 2303 vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED; 2304 2305 spin_lock_bh(&vsi->mac_filter_hash_lock); 2306 /* Create a list of filters to delete. */ 2307 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) { 2308 if (f->state == I40E_FILTER_REMOVE) { 2309 /* Move the element into temporary del_list */ 2310 hash_del(&f->hlist); 2311 hlist_add_head(&f->hlist, &tmp_del_list); 2312 2313 /* Avoid counting removed filters */ 2314 continue; 2315 } 2316 if (f->state == I40E_FILTER_NEW) { 2317 /* Create a temporary i40e_new_mac_filter */ 2318 new = kzalloc(sizeof(*new), GFP_ATOMIC); 2319 if (!new) 2320 goto err_no_memory_locked; 2321 2322 /* Store pointer to the real filter */ 2323 new->f = f; 2324 new->state = f->state; 2325 2326 /* Add it to the hash list */ 2327 hlist_add_head(&new->hlist, &tmp_add_list); 2328 } 2329 2330 /* Count the number of active (current and new) VLAN 2331 * filters we have now. Does not count filters which 2332 * are marked for deletion. 2333 */ 2334 if (f->vlan > 0) 2335 vlan_filters++; 2336 } 2337 2338 retval = i40e_correct_mac_vlan_filters(vsi, 2339 &tmp_add_list, 2340 &tmp_del_list, 2341 vlan_filters); 2342 if (retval) 2343 goto err_no_memory_locked; 2344 2345 spin_unlock_bh(&vsi->mac_filter_hash_lock); 2346 } 2347 2348 /* Now process 'del_list' outside the lock */ 2349 if (!hlist_empty(&tmp_del_list)) { 2350 filter_list_len = hw->aq.asq_buf_size / 2351 sizeof(struct i40e_aqc_remove_macvlan_element_data); 2352 list_size = filter_list_len * 2353 sizeof(struct i40e_aqc_remove_macvlan_element_data); 2354 del_list = kzalloc(list_size, GFP_ATOMIC); 2355 if (!del_list) 2356 goto err_no_memory; 2357 2358 hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) { 2359 cmd_flags = 0; 2360 2361 /* handle broadcast filters by updating the broadcast 2362 * promiscuous flag and release filter list. 2363 */ 2364 if (is_broadcast_ether_addr(f->macaddr)) { 2365 i40e_aqc_broadcast_filter(vsi, vsi_name, f); 2366 2367 hlist_del(&f->hlist); 2368 kfree(f); 2369 continue; 2370 } 2371 2372 /* add to delete list */ 2373 ether_addr_copy(del_list[num_del].mac_addr, f->macaddr); 2374 if (f->vlan == I40E_VLAN_ANY) { 2375 del_list[num_del].vlan_tag = 0; 2376 cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN; 2377 } else { 2378 del_list[num_del].vlan_tag = 2379 cpu_to_le16((u16)(f->vlan)); 2380 } 2381 2382 cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH; 2383 del_list[num_del].flags = cmd_flags; 2384 num_del++; 2385 2386 /* flush a full buffer */ 2387 if (num_del == filter_list_len) { 2388 i40e_aqc_del_filters(vsi, vsi_name, del_list, 2389 num_del, &retval); 2390 memset(del_list, 0, list_size); 2391 num_del = 0; 2392 } 2393 /* Release memory for MAC filter entries which were 2394 * synced up with HW. 2395 */ 2396 hlist_del(&f->hlist); 2397 kfree(f); 2398 } 2399 2400 if (num_del) { 2401 i40e_aqc_del_filters(vsi, vsi_name, del_list, 2402 num_del, &retval); 2403 } 2404 2405 kfree(del_list); 2406 del_list = NULL; 2407 } 2408 2409 if (!hlist_empty(&tmp_add_list)) { 2410 /* Do all the adds now. */ 2411 filter_list_len = hw->aq.asq_buf_size / 2412 sizeof(struct i40e_aqc_add_macvlan_element_data); 2413 list_size = filter_list_len * 2414 sizeof(struct i40e_aqc_add_macvlan_element_data); 2415 add_list = kzalloc(list_size, GFP_ATOMIC); 2416 if (!add_list) 2417 goto err_no_memory; 2418 2419 num_add = 0; 2420 hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) { 2421 /* handle broadcast filters by updating the broadcast 2422 * promiscuous flag instead of adding a MAC filter. 2423 */ 2424 if (is_broadcast_ether_addr(new->f->macaddr)) { 2425 if (i40e_aqc_broadcast_filter(vsi, vsi_name, 2426 new->f)) 2427 new->state = I40E_FILTER_FAILED; 2428 else 2429 new->state = I40E_FILTER_ACTIVE; 2430 continue; 2431 } 2432 2433 /* add to add array */ 2434 if (num_add == 0) 2435 add_head = new; 2436 cmd_flags = 0; 2437 ether_addr_copy(add_list[num_add].mac_addr, 2438 new->f->macaddr); 2439 if (new->f->vlan == I40E_VLAN_ANY) { 2440 add_list[num_add].vlan_tag = 0; 2441 cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN; 2442 } else { 2443 add_list[num_add].vlan_tag = 2444 cpu_to_le16((u16)(new->f->vlan)); 2445 } 2446 add_list[num_add].queue_number = 0; 2447 /* set invalid match method for later detection */ 2448 add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES; 2449 cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH; 2450 add_list[num_add].flags = cpu_to_le16(cmd_flags); 2451 num_add++; 2452 2453 /* flush a full buffer */ 2454 if (num_add == filter_list_len) { 2455 i40e_aqc_add_filters(vsi, vsi_name, add_list, 2456 add_head, num_add); 2457 memset(add_list, 0, list_size); 2458 num_add = 0; 2459 } 2460 } 2461 if (num_add) { 2462 i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head, 2463 num_add); 2464 } 2465 /* Now move all of the filters from the temp add list back to 2466 * the VSI's list. 2467 */ 2468 spin_lock_bh(&vsi->mac_filter_hash_lock); 2469 hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) { 2470 /* Only update the state if we're still NEW */ 2471 if (new->f->state == I40E_FILTER_NEW) 2472 new->f->state = new->state; 2473 hlist_del(&new->hlist); 2474 kfree(new); 2475 } 2476 spin_unlock_bh(&vsi->mac_filter_hash_lock); 2477 kfree(add_list); 2478 add_list = NULL; 2479 } 2480 2481 /* Determine the number of active and failed filters. */ 2482 spin_lock_bh(&vsi->mac_filter_hash_lock); 2483 vsi->active_filters = 0; 2484 hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) { 2485 if (f->state == I40E_FILTER_ACTIVE) 2486 vsi->active_filters++; 2487 else if (f->state == I40E_FILTER_FAILED) 2488 failed_filters++; 2489 } 2490 spin_unlock_bh(&vsi->mac_filter_hash_lock); 2491 2492 /* Check if we are able to exit overflow promiscuous mode. We can 2493 * safely exit if we didn't just enter, we no longer have any failed 2494 * filters, and we have reduced filters below the threshold value. 2495 */ 2496 if (old_overflow && !failed_filters && 2497 vsi->active_filters < vsi->promisc_threshold) { 2498 dev_info(&pf->pdev->dev, 2499 "filter logjam cleared on %s, leaving overflow promiscuous mode\n", 2500 vsi_name); 2501 clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state); 2502 vsi->promisc_threshold = 0; 2503 } 2504 2505 /* if the VF is not trusted do not do promisc */ 2506 if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) { 2507 clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state); 2508 goto out; 2509 } 2510 2511 new_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state); 2512 2513 /* If we are entering overflow promiscuous, we need to calculate a new 2514 * threshold for when we are safe to exit 2515 */ 2516 if (!old_overflow && new_overflow) 2517 vsi->promisc_threshold = (vsi->active_filters * 3) / 4; 2518 2519 /* check for changes in promiscuous modes */ 2520 if (changed_flags & IFF_ALLMULTI) { 2521 bool cur_multipromisc; 2522 2523 cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI); 2524 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw, 2525 vsi->seid, 2526 cur_multipromisc, 2527 NULL); 2528 if (aq_ret) { 2529 retval = i40e_aq_rc_to_posix(aq_ret, 2530 hw->aq.asq_last_status); 2531 dev_info(&pf->pdev->dev, 2532 "set multi promisc failed on %s, err %s aq_err %s\n", 2533 vsi_name, 2534 i40e_stat_str(hw, aq_ret), 2535 i40e_aq_str(hw, hw->aq.asq_last_status)); 2536 } 2537 } 2538 2539 if ((changed_flags & IFF_PROMISC) || old_overflow != new_overflow) { 2540 bool cur_promisc; 2541 2542 cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) || 2543 new_overflow); 2544 aq_ret = i40e_set_promiscuous(pf, cur_promisc); 2545 if (aq_ret) { 2546 retval = i40e_aq_rc_to_posix(aq_ret, 2547 hw->aq.asq_last_status); 2548 dev_info(&pf->pdev->dev, 2549 "Setting promiscuous %s failed on %s, err %s aq_err %s\n", 2550 cur_promisc ? "on" : "off", 2551 vsi_name, 2552 i40e_stat_str(hw, aq_ret), 2553 i40e_aq_str(hw, hw->aq.asq_last_status)); 2554 } 2555 } 2556 out: 2557 /* if something went wrong then set the changed flag so we try again */ 2558 if (retval) 2559 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED; 2560 2561 clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state); 2562 return retval; 2563 2564 err_no_memory: 2565 /* Restore elements on the temporary add and delete lists */ 2566 spin_lock_bh(&vsi->mac_filter_hash_lock); 2567 err_no_memory_locked: 2568 i40e_undo_del_filter_entries(vsi, &tmp_del_list); 2569 i40e_undo_add_filter_entries(vsi, &tmp_add_list); 2570 spin_unlock_bh(&vsi->mac_filter_hash_lock); 2571 2572 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED; 2573 clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state); 2574 return -ENOMEM; 2575 } 2576 2577 /** 2578 * i40e_sync_filters_subtask - Sync the VSI filter list with HW 2579 * @pf: board private structure 2580 **/ 2581 static void i40e_sync_filters_subtask(struct i40e_pf *pf) 2582 { 2583 int v; 2584 2585 if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC)) 2586 return; 2587 pf->flags &= ~I40E_FLAG_FILTER_SYNC; 2588 2589 for (v = 0; v < pf->num_alloc_vsi; v++) { 2590 if (pf->vsi[v] && 2591 (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) { 2592 int ret = i40e_sync_vsi_filters(pf->vsi[v]); 2593 2594 if (ret) { 2595 /* come back and try again later */ 2596 pf->flags |= I40E_FLAG_FILTER_SYNC; 2597 break; 2598 } 2599 } 2600 } 2601 } 2602 2603 /** 2604 * i40e_max_xdp_frame_size - returns the maximum allowed frame size for XDP 2605 * @vsi: the vsi 2606 **/ 2607 static int i40e_max_xdp_frame_size(struct i40e_vsi *vsi) 2608 { 2609 if (PAGE_SIZE >= 8192 || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) 2610 return I40E_RXBUFFER_2048; 2611 else 2612 return I40E_RXBUFFER_3072; 2613 } 2614 2615 /** 2616 * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit 2617 * @netdev: network interface device structure 2618 * @new_mtu: new value for maximum frame size 2619 * 2620 * Returns 0 on success, negative on failure 2621 **/ 2622 static int i40e_change_mtu(struct net_device *netdev, int new_mtu) 2623 { 2624 struct i40e_netdev_priv *np = netdev_priv(netdev); 2625 struct i40e_vsi *vsi = np->vsi; 2626 struct i40e_pf *pf = vsi->back; 2627 2628 if (i40e_enabled_xdp_vsi(vsi)) { 2629 int frame_size = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN; 2630 2631 if (frame_size > i40e_max_xdp_frame_size(vsi)) 2632 return -EINVAL; 2633 } 2634 2635 netdev_info(netdev, "changing MTU from %d to %d\n", 2636 netdev->mtu, new_mtu); 2637 netdev->mtu = new_mtu; 2638 if (netif_running(netdev)) 2639 i40e_vsi_reinit_locked(vsi); 2640 pf->flags |= (I40E_FLAG_SERVICE_CLIENT_REQUESTED | 2641 I40E_FLAG_CLIENT_L2_CHANGE); 2642 return 0; 2643 } 2644 2645 /** 2646 * i40e_ioctl - Access the hwtstamp interface 2647 * @netdev: network interface device structure 2648 * @ifr: interface request data 2649 * @cmd: ioctl command 2650 **/ 2651 int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd) 2652 { 2653 struct i40e_netdev_priv *np = netdev_priv(netdev); 2654 struct i40e_pf *pf = np->vsi->back; 2655 2656 switch (cmd) { 2657 case SIOCGHWTSTAMP: 2658 return i40e_ptp_get_ts_config(pf, ifr); 2659 case SIOCSHWTSTAMP: 2660 return i40e_ptp_set_ts_config(pf, ifr); 2661 default: 2662 return -EOPNOTSUPP; 2663 } 2664 } 2665 2666 /** 2667 * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI 2668 * @vsi: the vsi being adjusted 2669 **/ 2670 void i40e_vlan_stripping_enable(struct i40e_vsi *vsi) 2671 { 2672 struct i40e_vsi_context ctxt; 2673 i40e_status ret; 2674 2675 if ((vsi->info.valid_sections & 2676 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) && 2677 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0)) 2678 return; /* already enabled */ 2679 2680 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID); 2681 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL | 2682 I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH; 2683 2684 ctxt.seid = vsi->seid; 2685 ctxt.info = vsi->info; 2686 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL); 2687 if (ret) { 2688 dev_info(&vsi->back->pdev->dev, 2689 "update vlan stripping failed, err %s aq_err %s\n", 2690 i40e_stat_str(&vsi->back->hw, ret), 2691 i40e_aq_str(&vsi->back->hw, 2692 vsi->back->hw.aq.asq_last_status)); 2693 } 2694 } 2695 2696 /** 2697 * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI 2698 * @vsi: the vsi being adjusted 2699 **/ 2700 void i40e_vlan_stripping_disable(struct i40e_vsi *vsi) 2701 { 2702 struct i40e_vsi_context ctxt; 2703 i40e_status ret; 2704 2705 if ((vsi->info.valid_sections & 2706 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) && 2707 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) == 2708 I40E_AQ_VSI_PVLAN_EMOD_MASK)) 2709 return; /* already disabled */ 2710 2711 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID); 2712 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL | 2713 I40E_AQ_VSI_PVLAN_EMOD_NOTHING; 2714 2715 ctxt.seid = vsi->seid; 2716 ctxt.info = vsi->info; 2717 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL); 2718 if (ret) { 2719 dev_info(&vsi->back->pdev->dev, 2720 "update vlan stripping failed, err %s aq_err %s\n", 2721 i40e_stat_str(&vsi->back->hw, ret), 2722 i40e_aq_str(&vsi->back->hw, 2723 vsi->back->hw.aq.asq_last_status)); 2724 } 2725 } 2726 2727 /** 2728 * i40e_vlan_rx_register - Setup or shutdown vlan offload 2729 * @netdev: network interface to be adjusted 2730 * @features: netdev features to test if VLAN offload is enabled or not 2731 **/ 2732 static void i40e_vlan_rx_register(struct net_device *netdev, u32 features) 2733 { 2734 struct i40e_netdev_priv *np = netdev_priv(netdev); 2735 struct i40e_vsi *vsi = np->vsi; 2736 2737 if (features & NETIF_F_HW_VLAN_CTAG_RX) 2738 i40e_vlan_stripping_enable(vsi); 2739 else 2740 i40e_vlan_stripping_disable(vsi); 2741 } 2742 2743 /** 2744 * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address 2745 * @vsi: the vsi being configured 2746 * @vid: vlan id to be added (0 = untagged only , -1 = any) 2747 * 2748 * This is a helper function for adding a new MAC/VLAN filter with the 2749 * specified VLAN for each existing MAC address already in the hash table. 2750 * This function does *not* perform any accounting to update filters based on 2751 * VLAN mode. 2752 * 2753 * NOTE: this function expects to be called while under the 2754 * mac_filter_hash_lock 2755 **/ 2756 int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid) 2757 { 2758 struct i40e_mac_filter *f, *add_f; 2759 struct hlist_node *h; 2760 int bkt; 2761 2762 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) { 2763 if (f->state == I40E_FILTER_REMOVE) 2764 continue; 2765 add_f = i40e_add_filter(vsi, f->macaddr, vid); 2766 if (!add_f) { 2767 dev_info(&vsi->back->pdev->dev, 2768 "Could not add vlan filter %d for %pM\n", 2769 vid, f->macaddr); 2770 return -ENOMEM; 2771 } 2772 } 2773 2774 return 0; 2775 } 2776 2777 /** 2778 * i40e_vsi_add_vlan - Add VSI membership for given VLAN 2779 * @vsi: the VSI being configured 2780 * @vid: VLAN id to be added 2781 **/ 2782 int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid) 2783 { 2784 int err; 2785 2786 if (vsi->info.pvid) 2787 return -EINVAL; 2788 2789 /* The network stack will attempt to add VID=0, with the intention to 2790 * receive priority tagged packets with a VLAN of 0. Our HW receives 2791 * these packets by default when configured to receive untagged 2792 * packets, so we don't need to add a filter for this case. 2793 * Additionally, HW interprets adding a VID=0 filter as meaning to 2794 * receive *only* tagged traffic and stops receiving untagged traffic. 2795 * Thus, we do not want to actually add a filter for VID=0 2796 */ 2797 if (!vid) 2798 return 0; 2799 2800 /* Locked once because all functions invoked below iterates list*/ 2801 spin_lock_bh(&vsi->mac_filter_hash_lock); 2802 err = i40e_add_vlan_all_mac(vsi, vid); 2803 spin_unlock_bh(&vsi->mac_filter_hash_lock); 2804 if (err) 2805 return err; 2806 2807 /* schedule our worker thread which will take care of 2808 * applying the new filter changes 2809 */ 2810 i40e_service_event_schedule(vsi->back); 2811 return 0; 2812 } 2813 2814 /** 2815 * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN 2816 * @vsi: the vsi being configured 2817 * @vid: vlan id to be removed (0 = untagged only , -1 = any) 2818 * 2819 * This function should be used to remove all VLAN filters which match the 2820 * given VID. It does not schedule the service event and does not take the 2821 * mac_filter_hash_lock so it may be combined with other operations under 2822 * a single invocation of the mac_filter_hash_lock. 2823 * 2824 * NOTE: this function expects to be called while under the 2825 * mac_filter_hash_lock 2826 */ 2827 void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid) 2828 { 2829 struct i40e_mac_filter *f; 2830 struct hlist_node *h; 2831 int bkt; 2832 2833 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) { 2834 if (f->vlan == vid) 2835 __i40e_del_filter(vsi, f); 2836 } 2837 } 2838 2839 /** 2840 * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN 2841 * @vsi: the VSI being configured 2842 * @vid: VLAN id to be removed 2843 **/ 2844 void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid) 2845 { 2846 if (!vid || vsi->info.pvid) 2847 return; 2848 2849 spin_lock_bh(&vsi->mac_filter_hash_lock); 2850 i40e_rm_vlan_all_mac(vsi, vid); 2851 spin_unlock_bh(&vsi->mac_filter_hash_lock); 2852 2853 /* schedule our worker thread which will take care of 2854 * applying the new filter changes 2855 */ 2856 i40e_service_event_schedule(vsi->back); 2857 } 2858 2859 /** 2860 * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload 2861 * @netdev: network interface to be adjusted 2862 * @vid: vlan id to be added 2863 * 2864 * net_device_ops implementation for adding vlan ids 2865 **/ 2866 static int i40e_vlan_rx_add_vid(struct net_device *netdev, 2867 __always_unused __be16 proto, u16 vid) 2868 { 2869 struct i40e_netdev_priv *np = netdev_priv(netdev); 2870 struct i40e_vsi *vsi = np->vsi; 2871 int ret = 0; 2872 2873 if (vid >= VLAN_N_VID) 2874 return -EINVAL; 2875 2876 ret = i40e_vsi_add_vlan(vsi, vid); 2877 if (!ret) 2878 set_bit(vid, vsi->active_vlans); 2879 2880 return ret; 2881 } 2882 2883 /** 2884 * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload 2885 * @netdev: network interface to be adjusted 2886 * @vid: vlan id to be removed 2887 * 2888 * net_device_ops implementation for removing vlan ids 2889 **/ 2890 static int i40e_vlan_rx_kill_vid(struct net_device *netdev, 2891 __always_unused __be16 proto, u16 vid) 2892 { 2893 struct i40e_netdev_priv *np = netdev_priv(netdev); 2894 struct i40e_vsi *vsi = np->vsi; 2895 2896 /* return code is ignored as there is nothing a user 2897 * can do about failure to remove and a log message was 2898 * already printed from the other function 2899 */ 2900 i40e_vsi_kill_vlan(vsi, vid); 2901 2902 clear_bit(vid, vsi->active_vlans); 2903 2904 return 0; 2905 } 2906 2907 /** 2908 * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up 2909 * @vsi: the vsi being brought back up 2910 **/ 2911 static void i40e_restore_vlan(struct i40e_vsi *vsi) 2912 { 2913 u16 vid; 2914 2915 if (!vsi->netdev) 2916 return; 2917 2918 i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features); 2919 2920 for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID) 2921 i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q), 2922 vid); 2923 } 2924 2925 /** 2926 * i40e_vsi_add_pvid - Add pvid for the VSI 2927 * @vsi: the vsi being adjusted 2928 * @vid: the vlan id to set as a PVID 2929 **/ 2930 int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid) 2931 { 2932 struct i40e_vsi_context ctxt; 2933 i40e_status ret; 2934 2935 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID); 2936 vsi->info.pvid = cpu_to_le16(vid); 2937 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED | 2938 I40E_AQ_VSI_PVLAN_INSERT_PVID | 2939 I40E_AQ_VSI_PVLAN_EMOD_STR; 2940 2941 ctxt.seid = vsi->seid; 2942 ctxt.info = vsi->info; 2943 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL); 2944 if (ret) { 2945 dev_info(&vsi->back->pdev->dev, 2946 "add pvid failed, err %s aq_err %s\n", 2947 i40e_stat_str(&vsi->back->hw, ret), 2948 i40e_aq_str(&vsi->back->hw, 2949 vsi->back->hw.aq.asq_last_status)); 2950 return -ENOENT; 2951 } 2952 2953 return 0; 2954 } 2955 2956 /** 2957 * i40e_vsi_remove_pvid - Remove the pvid from the VSI 2958 * @vsi: the vsi being adjusted 2959 * 2960 * Just use the vlan_rx_register() service to put it back to normal 2961 **/ 2962 void i40e_vsi_remove_pvid(struct i40e_vsi *vsi) 2963 { 2964 i40e_vlan_stripping_disable(vsi); 2965 2966 vsi->info.pvid = 0; 2967 } 2968 2969 /** 2970 * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources 2971 * @vsi: ptr to the VSI 2972 * 2973 * If this function returns with an error, then it's possible one or 2974 * more of the rings is populated (while the rest are not). It is the 2975 * callers duty to clean those orphaned rings. 2976 * 2977 * Return 0 on success, negative on failure 2978 **/ 2979 static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi) 2980 { 2981 int i, err = 0; 2982 2983 for (i = 0; i < vsi->num_queue_pairs && !err; i++) 2984 err = i40e_setup_tx_descriptors(vsi->tx_rings[i]); 2985 2986 if (!i40e_enabled_xdp_vsi(vsi)) 2987 return err; 2988 2989 for (i = 0; i < vsi->num_queue_pairs && !err; i++) 2990 err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]); 2991 2992 return err; 2993 } 2994 2995 /** 2996 * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues 2997 * @vsi: ptr to the VSI 2998 * 2999 * Free VSI's transmit software resources 3000 **/ 3001 static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi) 3002 { 3003 int i; 3004 3005 if (vsi->tx_rings) { 3006 for (i = 0; i < vsi->num_queue_pairs; i++) 3007 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) 3008 i40e_free_tx_resources(vsi->tx_rings[i]); 3009 } 3010 3011 if (vsi->xdp_rings) { 3012 for (i = 0; i < vsi->num_queue_pairs; i++) 3013 if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc) 3014 i40e_free_tx_resources(vsi->xdp_rings[i]); 3015 } 3016 } 3017 3018 /** 3019 * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources 3020 * @vsi: ptr to the VSI 3021 * 3022 * If this function returns with an error, then it's possible one or 3023 * more of the rings is populated (while the rest are not). It is the 3024 * callers duty to clean those orphaned rings. 3025 * 3026 * Return 0 on success, negative on failure 3027 **/ 3028 static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi) 3029 { 3030 int i, err = 0; 3031 3032 for (i = 0; i < vsi->num_queue_pairs && !err; i++) 3033 err = i40e_setup_rx_descriptors(vsi->rx_rings[i]); 3034 return err; 3035 } 3036 3037 /** 3038 * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues 3039 * @vsi: ptr to the VSI 3040 * 3041 * Free all receive software resources 3042 **/ 3043 static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi) 3044 { 3045 int i; 3046 3047 if (!vsi->rx_rings) 3048 return; 3049 3050 for (i = 0; i < vsi->num_queue_pairs; i++) 3051 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc) 3052 i40e_free_rx_resources(vsi->rx_rings[i]); 3053 } 3054 3055 /** 3056 * i40e_config_xps_tx_ring - Configure XPS for a Tx ring 3057 * @ring: The Tx ring to configure 3058 * 3059 * This enables/disables XPS for a given Tx descriptor ring 3060 * based on the TCs enabled for the VSI that ring belongs to. 3061 **/ 3062 static void i40e_config_xps_tx_ring(struct i40e_ring *ring) 3063 { 3064 int cpu; 3065 3066 if (!ring->q_vector || !ring->netdev || ring->ch) 3067 return; 3068 3069 /* We only initialize XPS once, so as not to overwrite user settings */ 3070 if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state)) 3071 return; 3072 3073 cpu = cpumask_local_spread(ring->q_vector->v_idx, -1); 3074 netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu), 3075 ring->queue_index); 3076 } 3077 3078 /** 3079 * i40e_configure_tx_ring - Configure a transmit ring context and rest 3080 * @ring: The Tx ring to configure 3081 * 3082 * Configure the Tx descriptor ring in the HMC context. 3083 **/ 3084 static int i40e_configure_tx_ring(struct i40e_ring *ring) 3085 { 3086 struct i40e_vsi *vsi = ring->vsi; 3087 u16 pf_q = vsi->base_queue + ring->queue_index; 3088 struct i40e_hw *hw = &vsi->back->hw; 3089 struct i40e_hmc_obj_txq tx_ctx; 3090 i40e_status err = 0; 3091 u32 qtx_ctl = 0; 3092 3093 /* some ATR related tx ring init */ 3094 if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) { 3095 ring->atr_sample_rate = vsi->back->atr_sample_rate; 3096 ring->atr_count = 0; 3097 } else { 3098 ring->atr_sample_rate = 0; 3099 } 3100 3101 /* configure XPS */ 3102 i40e_config_xps_tx_ring(ring); 3103 3104 /* clear the context structure first */ 3105 memset(&tx_ctx, 0, sizeof(tx_ctx)); 3106 3107 tx_ctx.new_context = 1; 3108 tx_ctx.base = (ring->dma / 128); 3109 tx_ctx.qlen = ring->count; 3110 tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED | 3111 I40E_FLAG_FD_ATR_ENABLED)); 3112 tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP); 3113 /* FDIR VSI tx ring can still use RS bit and writebacks */ 3114 if (vsi->type != I40E_VSI_FDIR) 3115 tx_ctx.head_wb_ena = 1; 3116 tx_ctx.head_wb_addr = ring->dma + 3117 (ring->count * sizeof(struct i40e_tx_desc)); 3118 3119 /* As part of VSI creation/update, FW allocates certain 3120 * Tx arbitration queue sets for each TC enabled for 3121 * the VSI. The FW returns the handles to these queue 3122 * sets as part of the response buffer to Add VSI, 3123 * Update VSI, etc. AQ commands. It is expected that 3124 * these queue set handles be associated with the Tx 3125 * queues by the driver as part of the TX queue context 3126 * initialization. This has to be done regardless of 3127 * DCB as by default everything is mapped to TC0. 3128 */ 3129 3130 if (ring->ch) 3131 tx_ctx.rdylist = 3132 le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]); 3133 3134 else 3135 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]); 3136 3137 tx_ctx.rdylist_act = 0; 3138 3139 /* clear the context in the HMC */ 3140 err = i40e_clear_lan_tx_queue_context(hw, pf_q); 3141 if (err) { 3142 dev_info(&vsi->back->pdev->dev, 3143 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n", 3144 ring->queue_index, pf_q, err); 3145 return -ENOMEM; 3146 } 3147 3148 /* set the context in the HMC */ 3149 err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx); 3150 if (err) { 3151 dev_info(&vsi->back->pdev->dev, 3152 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n", 3153 ring->queue_index, pf_q, err); 3154 return -ENOMEM; 3155 } 3156 3157 /* Now associate this queue with this PCI function */ 3158 if (ring->ch) { 3159 if (ring->ch->type == I40E_VSI_VMDQ2) 3160 qtx_ctl = I40E_QTX_CTL_VM_QUEUE; 3161 else 3162 return -EINVAL; 3163 3164 qtx_ctl |= (ring->ch->vsi_number << 3165 I40E_QTX_CTL_VFVM_INDX_SHIFT) & 3166 I40E_QTX_CTL_VFVM_INDX_MASK; 3167 } else { 3168 if (vsi->type == I40E_VSI_VMDQ2) { 3169 qtx_ctl = I40E_QTX_CTL_VM_QUEUE; 3170 qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) & 3171 I40E_QTX_CTL_VFVM_INDX_MASK; 3172 } else { 3173 qtx_ctl = I40E_QTX_CTL_PF_QUEUE; 3174 } 3175 } 3176 3177 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) & 3178 I40E_QTX_CTL_PF_INDX_MASK); 3179 wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl); 3180 i40e_flush(hw); 3181 3182 /* cache tail off for easier writes later */ 3183 ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q); 3184 3185 return 0; 3186 } 3187 3188 /** 3189 * i40e_configure_rx_ring - Configure a receive ring context 3190 * @ring: The Rx ring to configure 3191 * 3192 * Configure the Rx descriptor ring in the HMC context. 3193 **/ 3194 static int i40e_configure_rx_ring(struct i40e_ring *ring) 3195 { 3196 struct i40e_vsi *vsi = ring->vsi; 3197 u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len; 3198 u16 pf_q = vsi->base_queue + ring->queue_index; 3199 struct i40e_hw *hw = &vsi->back->hw; 3200 struct i40e_hmc_obj_rxq rx_ctx; 3201 i40e_status err = 0; 3202 3203 bitmap_zero(ring->state, __I40E_RING_STATE_NBITS); 3204 3205 /* clear the context structure first */ 3206 memset(&rx_ctx, 0, sizeof(rx_ctx)); 3207 3208 ring->rx_buf_len = vsi->rx_buf_len; 3209 3210 rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len, 3211 BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT)); 3212 3213 rx_ctx.base = (ring->dma / 128); 3214 rx_ctx.qlen = ring->count; 3215 3216 /* use 32 byte descriptors */ 3217 rx_ctx.dsize = 1; 3218 3219 /* descriptor type is always zero 3220 * rx_ctx.dtype = 0; 3221 */ 3222 rx_ctx.hsplit_0 = 0; 3223 3224 rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len); 3225 if (hw->revision_id == 0) 3226 rx_ctx.lrxqthresh = 0; 3227 else 3228 rx_ctx.lrxqthresh = 1; 3229 rx_ctx.crcstrip = 1; 3230 rx_ctx.l2tsel = 1; 3231 /* this controls whether VLAN is stripped from inner headers */ 3232 rx_ctx.showiv = 0; 3233 /* set the prefena field to 1 because the manual says to */ 3234 rx_ctx.prefena = 1; 3235 3236 /* clear the context in the HMC */ 3237 err = i40e_clear_lan_rx_queue_context(hw, pf_q); 3238 if (err) { 3239 dev_info(&vsi->back->pdev->dev, 3240 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n", 3241 ring->queue_index, pf_q, err); 3242 return -ENOMEM; 3243 } 3244 3245 /* set the context in the HMC */ 3246 err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx); 3247 if (err) { 3248 dev_info(&vsi->back->pdev->dev, 3249 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n", 3250 ring->queue_index, pf_q, err); 3251 return -ENOMEM; 3252 } 3253 3254 /* configure Rx buffer alignment */ 3255 if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) 3256 clear_ring_build_skb_enabled(ring); 3257 else 3258 set_ring_build_skb_enabled(ring); 3259 3260 /* cache tail for quicker writes, and clear the reg before use */ 3261 ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q); 3262 writel(0, ring->tail); 3263 3264 i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring)); 3265 3266 return 0; 3267 } 3268 3269 /** 3270 * i40e_vsi_configure_tx - Configure the VSI for Tx 3271 * @vsi: VSI structure describing this set of rings and resources 3272 * 3273 * Configure the Tx VSI for operation. 3274 **/ 3275 static int i40e_vsi_configure_tx(struct i40e_vsi *vsi) 3276 { 3277 int err = 0; 3278 u16 i; 3279 3280 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++) 3281 err = i40e_configure_tx_ring(vsi->tx_rings[i]); 3282 3283 if (!i40e_enabled_xdp_vsi(vsi)) 3284 return err; 3285 3286 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++) 3287 err = i40e_configure_tx_ring(vsi->xdp_rings[i]); 3288 3289 return err; 3290 } 3291 3292 /** 3293 * i40e_vsi_configure_rx - Configure the VSI for Rx 3294 * @vsi: the VSI being configured 3295 * 3296 * Configure the Rx VSI for operation. 3297 **/ 3298 static int i40e_vsi_configure_rx(struct i40e_vsi *vsi) 3299 { 3300 int err = 0; 3301 u16 i; 3302 3303 if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) { 3304 vsi->max_frame = I40E_MAX_RXBUFFER; 3305 vsi->rx_buf_len = I40E_RXBUFFER_2048; 3306 #if (PAGE_SIZE < 8192) 3307 } else if (!I40E_2K_TOO_SMALL_WITH_PADDING && 3308 (vsi->netdev->mtu <= ETH_DATA_LEN)) { 3309 vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN; 3310 vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN; 3311 #endif 3312 } else { 3313 vsi->max_frame = I40E_MAX_RXBUFFER; 3314 vsi->rx_buf_len = (PAGE_SIZE < 8192) ? I40E_RXBUFFER_3072 : 3315 I40E_RXBUFFER_2048; 3316 } 3317 3318 /* set up individual rings */ 3319 for (i = 0; i < vsi->num_queue_pairs && !err; i++) 3320 err = i40e_configure_rx_ring(vsi->rx_rings[i]); 3321 3322 return err; 3323 } 3324 3325 /** 3326 * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC 3327 * @vsi: ptr to the VSI 3328 **/ 3329 static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi) 3330 { 3331 struct i40e_ring *tx_ring, *rx_ring; 3332 u16 qoffset, qcount; 3333 int i, n; 3334 3335 if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) { 3336 /* Reset the TC information */ 3337 for (i = 0; i < vsi->num_queue_pairs; i++) { 3338 rx_ring = vsi->rx_rings[i]; 3339 tx_ring = vsi->tx_rings[i]; 3340 rx_ring->dcb_tc = 0; 3341 tx_ring->dcb_tc = 0; 3342 } 3343 return; 3344 } 3345 3346 for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) { 3347 if (!(vsi->tc_config.enabled_tc & BIT_ULL(n))) 3348 continue; 3349 3350 qoffset = vsi->tc_config.tc_info[n].qoffset; 3351 qcount = vsi->tc_config.tc_info[n].qcount; 3352 for (i = qoffset; i < (qoffset + qcount); i++) { 3353 rx_ring = vsi->rx_rings[i]; 3354 tx_ring = vsi->tx_rings[i]; 3355 rx_ring->dcb_tc = n; 3356 tx_ring->dcb_tc = n; 3357 } 3358 } 3359 } 3360 3361 /** 3362 * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI 3363 * @vsi: ptr to the VSI 3364 **/ 3365 static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi) 3366 { 3367 if (vsi->netdev) 3368 i40e_set_rx_mode(vsi->netdev); 3369 } 3370 3371 /** 3372 * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters 3373 * @vsi: Pointer to the targeted VSI 3374 * 3375 * This function replays the hlist on the hw where all the SB Flow Director 3376 * filters were saved. 3377 **/ 3378 static void i40e_fdir_filter_restore(struct i40e_vsi *vsi) 3379 { 3380 struct i40e_fdir_filter *filter; 3381 struct i40e_pf *pf = vsi->back; 3382 struct hlist_node *node; 3383 3384 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED)) 3385 return; 3386 3387 /* Reset FDir counters as we're replaying all existing filters */ 3388 pf->fd_tcp4_filter_cnt = 0; 3389 pf->fd_udp4_filter_cnt = 0; 3390 pf->fd_sctp4_filter_cnt = 0; 3391 pf->fd_ip4_filter_cnt = 0; 3392 3393 hlist_for_each_entry_safe(filter, node, 3394 &pf->fdir_filter_list, fdir_node) { 3395 i40e_add_del_fdir(vsi, filter, true); 3396 } 3397 } 3398 3399 /** 3400 * i40e_vsi_configure - Set up the VSI for action 3401 * @vsi: the VSI being configured 3402 **/ 3403 static int i40e_vsi_configure(struct i40e_vsi *vsi) 3404 { 3405 int err; 3406 3407 i40e_set_vsi_rx_mode(vsi); 3408 i40e_restore_vlan(vsi); 3409 i40e_vsi_config_dcb_rings(vsi); 3410 err = i40e_vsi_configure_tx(vsi); 3411 if (!err) 3412 err = i40e_vsi_configure_rx(vsi); 3413 3414 return err; 3415 } 3416 3417 /** 3418 * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW 3419 * @vsi: the VSI being configured 3420 **/ 3421 static void i40e_vsi_configure_msix(struct i40e_vsi *vsi) 3422 { 3423 bool has_xdp = i40e_enabled_xdp_vsi(vsi); 3424 struct i40e_pf *pf = vsi->back; 3425 struct i40e_hw *hw = &pf->hw; 3426 u16 vector; 3427 int i, q; 3428 u32 qp; 3429 3430 /* The interrupt indexing is offset by 1 in the PFINT_ITRn 3431 * and PFINT_LNKLSTn registers, e.g.: 3432 * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts) 3433 */ 3434 qp = vsi->base_queue; 3435 vector = vsi->base_vector; 3436 for (i = 0; i < vsi->num_q_vectors; i++, vector++) { 3437 struct i40e_q_vector *q_vector = vsi->q_vectors[i]; 3438 3439 q_vector->rx.next_update = jiffies + 1; 3440 q_vector->rx.target_itr = 3441 ITR_TO_REG(vsi->rx_rings[i]->itr_setting); 3442 wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1), 3443 q_vector->rx.target_itr); 3444 q_vector->rx.current_itr = q_vector->rx.target_itr; 3445 3446 q_vector->tx.next_update = jiffies + 1; 3447 q_vector->tx.target_itr = 3448 ITR_TO_REG(vsi->tx_rings[i]->itr_setting); 3449 wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1), 3450 q_vector->tx.target_itr); 3451 q_vector->tx.current_itr = q_vector->tx.target_itr; 3452 3453 wr32(hw, I40E_PFINT_RATEN(vector - 1), 3454 i40e_intrl_usec_to_reg(vsi->int_rate_limit)); 3455 3456 /* Linked list for the queuepairs assigned to this vector */ 3457 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp); 3458 for (q = 0; q < q_vector->num_ringpairs; q++) { 3459 u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp; 3460 u32 val; 3461 3462 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK | 3463 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) | 3464 (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) | 3465 (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) | 3466 (I40E_QUEUE_TYPE_TX << 3467 I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT); 3468 3469 wr32(hw, I40E_QINT_RQCTL(qp), val); 3470 3471 if (has_xdp) { 3472 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK | 3473 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) | 3474 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) | 3475 (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) | 3476 (I40E_QUEUE_TYPE_TX << 3477 I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT); 3478 3479 wr32(hw, I40E_QINT_TQCTL(nextqp), val); 3480 } 3481 3482 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK | 3483 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) | 3484 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) | 3485 ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) | 3486 (I40E_QUEUE_TYPE_RX << 3487 I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT); 3488 3489 /* Terminate the linked list */ 3490 if (q == (q_vector->num_ringpairs - 1)) 3491 val |= (I40E_QUEUE_END_OF_LIST << 3492 I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT); 3493 3494 wr32(hw, I40E_QINT_TQCTL(qp), val); 3495 qp++; 3496 } 3497 } 3498 3499 i40e_flush(hw); 3500 } 3501 3502 /** 3503 * i40e_enable_misc_int_causes - enable the non-queue interrupts 3504 * @hw: ptr to the hardware info 3505 **/ 3506 static void i40e_enable_misc_int_causes(struct i40e_pf *pf) 3507 { 3508 struct i40e_hw *hw = &pf->hw; 3509 u32 val; 3510 3511 /* clear things first */ 3512 wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */ 3513 rd32(hw, I40E_PFINT_ICR0); /* read to clear */ 3514 3515 val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK | 3516 I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK | 3517 I40E_PFINT_ICR0_ENA_GRST_MASK | 3518 I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK | 3519 I40E_PFINT_ICR0_ENA_GPIO_MASK | 3520 I40E_PFINT_ICR0_ENA_HMC_ERR_MASK | 3521 I40E_PFINT_ICR0_ENA_VFLR_MASK | 3522 I40E_PFINT_ICR0_ENA_ADMINQ_MASK; 3523 3524 if (pf->flags & I40E_FLAG_IWARP_ENABLED) 3525 val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK; 3526 3527 if (pf->flags & I40E_FLAG_PTP) 3528 val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK; 3529 3530 wr32(hw, I40E_PFINT_ICR0_ENA, val); 3531 3532 /* SW_ITR_IDX = 0, but don't change INTENA */ 3533 wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK | 3534 I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK); 3535 3536 /* OTHER_ITR_IDX = 0 */ 3537 wr32(hw, I40E_PFINT_STAT_CTL0, 0); 3538 } 3539 3540 /** 3541 * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW 3542 * @vsi: the VSI being configured 3543 **/ 3544 static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi) 3545 { 3546 u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0; 3547 struct i40e_q_vector *q_vector = vsi->q_vectors[0]; 3548 struct i40e_pf *pf = vsi->back; 3549 struct i40e_hw *hw = &pf->hw; 3550 u32 val; 3551 3552 /* set the ITR configuration */ 3553 q_vector->rx.next_update = jiffies + 1; 3554 q_vector->rx.target_itr = ITR_TO_REG(vsi->rx_rings[0]->itr_setting); 3555 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.target_itr); 3556 q_vector->rx.current_itr = q_vector->rx.target_itr; 3557 q_vector->tx.next_update = jiffies + 1; 3558 q_vector->tx.target_itr = ITR_TO_REG(vsi->tx_rings[0]->itr_setting); 3559 wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.target_itr); 3560 q_vector->tx.current_itr = q_vector->tx.target_itr; 3561 3562 i40e_enable_misc_int_causes(pf); 3563 3564 /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */ 3565 wr32(hw, I40E_PFINT_LNKLST0, 0); 3566 3567 /* Associate the queue pair to the vector and enable the queue int */ 3568 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK | 3569 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) | 3570 (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)| 3571 (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT); 3572 3573 wr32(hw, I40E_QINT_RQCTL(0), val); 3574 3575 if (i40e_enabled_xdp_vsi(vsi)) { 3576 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK | 3577 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT)| 3578 (I40E_QUEUE_TYPE_TX 3579 << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT); 3580 3581 wr32(hw, I40E_QINT_TQCTL(nextqp), val); 3582 } 3583 3584 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK | 3585 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) | 3586 (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT); 3587 3588 wr32(hw, I40E_QINT_TQCTL(0), val); 3589 i40e_flush(hw); 3590 } 3591 3592 /** 3593 * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0 3594 * @pf: board private structure 3595 **/ 3596 void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf) 3597 { 3598 struct i40e_hw *hw = &pf->hw; 3599 3600 wr32(hw, I40E_PFINT_DYN_CTL0, 3601 I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT); 3602 i40e_flush(hw); 3603 } 3604 3605 /** 3606 * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0 3607 * @pf: board private structure 3608 **/ 3609 void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf) 3610 { 3611 struct i40e_hw *hw = &pf->hw; 3612 u32 val; 3613 3614 val = I40E_PFINT_DYN_CTL0_INTENA_MASK | 3615 I40E_PFINT_DYN_CTL0_CLEARPBA_MASK | 3616 (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT); 3617 3618 wr32(hw, I40E_PFINT_DYN_CTL0, val); 3619 i40e_flush(hw); 3620 } 3621 3622 /** 3623 * i40e_msix_clean_rings - MSIX mode Interrupt Handler 3624 * @irq: interrupt number 3625 * @data: pointer to a q_vector 3626 **/ 3627 static irqreturn_t i40e_msix_clean_rings(int irq, void *data) 3628 { 3629 struct i40e_q_vector *q_vector = data; 3630 3631 if (!q_vector->tx.ring && !q_vector->rx.ring) 3632 return IRQ_HANDLED; 3633 3634 napi_schedule_irqoff(&q_vector->napi); 3635 3636 return IRQ_HANDLED; 3637 } 3638 3639 /** 3640 * i40e_irq_affinity_notify - Callback for affinity changes 3641 * @notify: context as to what irq was changed 3642 * @mask: the new affinity mask 3643 * 3644 * This is a callback function used by the irq_set_affinity_notifier function 3645 * so that we may register to receive changes to the irq affinity masks. 3646 **/ 3647 static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify, 3648 const cpumask_t *mask) 3649 { 3650 struct i40e_q_vector *q_vector = 3651 container_of(notify, struct i40e_q_vector, affinity_notify); 3652 3653 cpumask_copy(&q_vector->affinity_mask, mask); 3654 } 3655 3656 /** 3657 * i40e_irq_affinity_release - Callback for affinity notifier release 3658 * @ref: internal core kernel usage 3659 * 3660 * This is a callback function used by the irq_set_affinity_notifier function 3661 * to inform the current notification subscriber that they will no longer 3662 * receive notifications. 3663 **/ 3664 static void i40e_irq_affinity_release(struct kref *ref) {} 3665 3666 /** 3667 * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts 3668 * @vsi: the VSI being configured 3669 * @basename: name for the vector 3670 * 3671 * Allocates MSI-X vectors and requests interrupts from the kernel. 3672 **/ 3673 static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename) 3674 { 3675 int q_vectors = vsi->num_q_vectors; 3676 struct i40e_pf *pf = vsi->back; 3677 int base = vsi->base_vector; 3678 int rx_int_idx = 0; 3679 int tx_int_idx = 0; 3680 int vector, err; 3681 int irq_num; 3682 int cpu; 3683 3684 for (vector = 0; vector < q_vectors; vector++) { 3685 struct i40e_q_vector *q_vector = vsi->q_vectors[vector]; 3686 3687 irq_num = pf->msix_entries[base + vector].vector; 3688 3689 if (q_vector->tx.ring && q_vector->rx.ring) { 3690 snprintf(q_vector->name, sizeof(q_vector->name) - 1, 3691 "%s-%s-%d", basename, "TxRx", rx_int_idx++); 3692 tx_int_idx++; 3693 } else if (q_vector->rx.ring) { 3694 snprintf(q_vector->name, sizeof(q_vector->name) - 1, 3695 "%s-%s-%d", basename, "rx", rx_int_idx++); 3696 } else if (q_vector->tx.ring) { 3697 snprintf(q_vector->name, sizeof(q_vector->name) - 1, 3698 "%s-%s-%d", basename, "tx", tx_int_idx++); 3699 } else { 3700 /* skip this unused q_vector */ 3701 continue; 3702 } 3703 err = request_irq(irq_num, 3704 vsi->irq_handler, 3705 0, 3706 q_vector->name, 3707 q_vector); 3708 if (err) { 3709 dev_info(&pf->pdev->dev, 3710 "MSIX request_irq failed, error: %d\n", err); 3711 goto free_queue_irqs; 3712 } 3713 3714 /* register for affinity change notifications */ 3715 q_vector->affinity_notify.notify = i40e_irq_affinity_notify; 3716 q_vector->affinity_notify.release = i40e_irq_affinity_release; 3717 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify); 3718 /* Spread affinity hints out across online CPUs. 3719 * 3720 * get_cpu_mask returns a static constant mask with 3721 * a permanent lifetime so it's ok to pass to 3722 * irq_set_affinity_hint without making a copy. 3723 */ 3724 cpu = cpumask_local_spread(q_vector->v_idx, -1); 3725 irq_set_affinity_hint(irq_num, get_cpu_mask(cpu)); 3726 } 3727 3728 vsi->irqs_ready = true; 3729 return 0; 3730 3731 free_queue_irqs: 3732 while (vector) { 3733 vector--; 3734 irq_num = pf->msix_entries[base + vector].vector; 3735 irq_set_affinity_notifier(irq_num, NULL); 3736 irq_set_affinity_hint(irq_num, NULL); 3737 free_irq(irq_num, &vsi->q_vectors[vector]); 3738 } 3739 return err; 3740 } 3741 3742 /** 3743 * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI 3744 * @vsi: the VSI being un-configured 3745 **/ 3746 static void i40e_vsi_disable_irq(struct i40e_vsi *vsi) 3747 { 3748 struct i40e_pf *pf = vsi->back; 3749 struct i40e_hw *hw = &pf->hw; 3750 int base = vsi->base_vector; 3751 int i; 3752 3753 /* disable interrupt causation from each queue */ 3754 for (i = 0; i < vsi->num_queue_pairs; i++) { 3755 u32 val; 3756 3757 val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx)); 3758 val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK; 3759 wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val); 3760 3761 val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx)); 3762 val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK; 3763 wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val); 3764 3765 if (!i40e_enabled_xdp_vsi(vsi)) 3766 continue; 3767 wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0); 3768 } 3769 3770 /* disable each interrupt */ 3771 if (pf->flags & I40E_FLAG_MSIX_ENABLED) { 3772 for (i = vsi->base_vector; 3773 i < (vsi->num_q_vectors + vsi->base_vector); i++) 3774 wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0); 3775 3776 i40e_flush(hw); 3777 for (i = 0; i < vsi->num_q_vectors; i++) 3778 synchronize_irq(pf->msix_entries[i + base].vector); 3779 } else { 3780 /* Legacy and MSI mode - this stops all interrupt handling */ 3781 wr32(hw, I40E_PFINT_ICR0_ENA, 0); 3782 wr32(hw, I40E_PFINT_DYN_CTL0, 0); 3783 i40e_flush(hw); 3784 synchronize_irq(pf->pdev->irq); 3785 } 3786 } 3787 3788 /** 3789 * i40e_vsi_enable_irq - Enable IRQ for the given VSI 3790 * @vsi: the VSI being configured 3791 **/ 3792 static int i40e_vsi_enable_irq(struct i40e_vsi *vsi) 3793 { 3794 struct i40e_pf *pf = vsi->back; 3795 int i; 3796 3797 if (pf->flags & I40E_FLAG_MSIX_ENABLED) { 3798 for (i = 0; i < vsi->num_q_vectors; i++) 3799 i40e_irq_dynamic_enable(vsi, i); 3800 } else { 3801 i40e_irq_dynamic_enable_icr0(pf); 3802 } 3803 3804 i40e_flush(&pf->hw); 3805 return 0; 3806 } 3807 3808 /** 3809 * i40e_free_misc_vector - Free the vector that handles non-queue events 3810 * @pf: board private structure 3811 **/ 3812 static void i40e_free_misc_vector(struct i40e_pf *pf) 3813 { 3814 /* Disable ICR 0 */ 3815 wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0); 3816 i40e_flush(&pf->hw); 3817 3818 if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) { 3819 synchronize_irq(pf->msix_entries[0].vector); 3820 free_irq(pf->msix_entries[0].vector, pf); 3821 clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state); 3822 } 3823 } 3824 3825 /** 3826 * i40e_intr - MSI/Legacy and non-queue interrupt handler 3827 * @irq: interrupt number 3828 * @data: pointer to a q_vector 3829 * 3830 * This is the handler used for all MSI/Legacy interrupts, and deals 3831 * with both queue and non-queue interrupts. This is also used in 3832 * MSIX mode to handle the non-queue interrupts. 3833 **/ 3834 static irqreturn_t i40e_intr(int irq, void *data) 3835 { 3836 struct i40e_pf *pf = (struct i40e_pf *)data; 3837 struct i40e_hw *hw = &pf->hw; 3838 irqreturn_t ret = IRQ_NONE; 3839 u32 icr0, icr0_remaining; 3840 u32 val, ena_mask; 3841 3842 icr0 = rd32(hw, I40E_PFINT_ICR0); 3843 ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA); 3844 3845 /* if sharing a legacy IRQ, we might get called w/o an intr pending */ 3846 if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0) 3847 goto enable_intr; 3848 3849 /* if interrupt but no bits showing, must be SWINT */ 3850 if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) || 3851 (icr0 & I40E_PFINT_ICR0_SWINT_MASK)) 3852 pf->sw_int_count++; 3853 3854 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) && 3855 (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) { 3856 ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK; 3857 dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n"); 3858 set_bit(__I40E_CORE_RESET_REQUESTED, pf->state); 3859 } 3860 3861 /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */ 3862 if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) { 3863 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi]; 3864 struct i40e_q_vector *q_vector = vsi->q_vectors[0]; 3865 3866 /* We do not have a way to disarm Queue causes while leaving 3867 * interrupt enabled for all other causes, ideally 3868 * interrupt should be disabled while we are in NAPI but 3869 * this is not a performance path and napi_schedule() 3870 * can deal with rescheduling. 3871 */ 3872 if (!test_bit(__I40E_DOWN, pf->state)) 3873 napi_schedule_irqoff(&q_vector->napi); 3874 } 3875 3876 if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) { 3877 ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK; 3878 set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state); 3879 i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n"); 3880 } 3881 3882 if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) { 3883 ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK; 3884 set_bit(__I40E_MDD_EVENT_PENDING, pf->state); 3885 } 3886 3887 if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) { 3888 ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK; 3889 set_bit(__I40E_VFLR_EVENT_PENDING, pf->state); 3890 } 3891 3892 if (icr0 & I40E_PFINT_ICR0_GRST_MASK) { 3893 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) 3894 set_bit(__I40E_RESET_INTR_RECEIVED, pf->state); 3895 ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK; 3896 val = rd32(hw, I40E_GLGEN_RSTAT); 3897 val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK) 3898 >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT; 3899 if (val == I40E_RESET_CORER) { 3900 pf->corer_count++; 3901 } else if (val == I40E_RESET_GLOBR) { 3902 pf->globr_count++; 3903 } else if (val == I40E_RESET_EMPR) { 3904 pf->empr_count++; 3905 set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state); 3906 } 3907 } 3908 3909 if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) { 3910 icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK; 3911 dev_info(&pf->pdev->dev, "HMC error interrupt\n"); 3912 dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n", 3913 rd32(hw, I40E_PFHMC_ERRORINFO), 3914 rd32(hw, I40E_PFHMC_ERRORDATA)); 3915 } 3916 3917 if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) { 3918 u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0); 3919 3920 if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) { 3921 icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK; 3922 i40e_ptp_tx_hwtstamp(pf); 3923 } 3924 } 3925 3926 /* If a critical error is pending we have no choice but to reset the 3927 * device. 3928 * Report and mask out any remaining unexpected interrupts. 3929 */ 3930 icr0_remaining = icr0 & ena_mask; 3931 if (icr0_remaining) { 3932 dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n", 3933 icr0_remaining); 3934 if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) || 3935 (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) || 3936 (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) { 3937 dev_info(&pf->pdev->dev, "device will be reset\n"); 3938 set_bit(__I40E_PF_RESET_REQUESTED, pf->state); 3939 i40e_service_event_schedule(pf); 3940 } 3941 ena_mask &= ~icr0_remaining; 3942 } 3943 ret = IRQ_HANDLED; 3944 3945 enable_intr: 3946 /* re-enable interrupt causes */ 3947 wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask); 3948 if (!test_bit(__I40E_DOWN, pf->state)) { 3949 i40e_service_event_schedule(pf); 3950 i40e_irq_dynamic_enable_icr0(pf); 3951 } 3952 3953 return ret; 3954 } 3955 3956 /** 3957 * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes 3958 * @tx_ring: tx ring to clean 3959 * @budget: how many cleans we're allowed 3960 * 3961 * Returns true if there's any budget left (e.g. the clean is finished) 3962 **/ 3963 static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget) 3964 { 3965 struct i40e_vsi *vsi = tx_ring->vsi; 3966 u16 i = tx_ring->next_to_clean; 3967 struct i40e_tx_buffer *tx_buf; 3968 struct i40e_tx_desc *tx_desc; 3969 3970 tx_buf = &tx_ring->tx_bi[i]; 3971 tx_desc = I40E_TX_DESC(tx_ring, i); 3972 i -= tx_ring->count; 3973 3974 do { 3975 struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch; 3976 3977 /* if next_to_watch is not set then there is no work pending */ 3978 if (!eop_desc) 3979 break; 3980 3981 /* prevent any other reads prior to eop_desc */ 3982 smp_rmb(); 3983 3984 /* if the descriptor isn't done, no work yet to do */ 3985 if (!(eop_desc->cmd_type_offset_bsz & 3986 cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE))) 3987 break; 3988 3989 /* clear next_to_watch to prevent false hangs */ 3990 tx_buf->next_to_watch = NULL; 3991 3992 tx_desc->buffer_addr = 0; 3993 tx_desc->cmd_type_offset_bsz = 0; 3994 /* move past filter desc */ 3995 tx_buf++; 3996 tx_desc++; 3997 i++; 3998 if (unlikely(!i)) { 3999 i -= tx_ring->count; 4000 tx_buf = tx_ring->tx_bi; 4001 tx_desc = I40E_TX_DESC(tx_ring, 0); 4002 } 4003 /* unmap skb header data */ 4004 dma_unmap_single(tx_ring->dev, 4005 dma_unmap_addr(tx_buf, dma), 4006 dma_unmap_len(tx_buf, len), 4007 DMA_TO_DEVICE); 4008 if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB) 4009 kfree(tx_buf->raw_buf); 4010 4011 tx_buf->raw_buf = NULL; 4012 tx_buf->tx_flags = 0; 4013 tx_buf->next_to_watch = NULL; 4014 dma_unmap_len_set(tx_buf, len, 0); 4015 tx_desc->buffer_addr = 0; 4016 tx_desc->cmd_type_offset_bsz = 0; 4017 4018 /* move us past the eop_desc for start of next FD desc */ 4019 tx_buf++; 4020 tx_desc++; 4021 i++; 4022 if (unlikely(!i)) { 4023 i -= tx_ring->count; 4024 tx_buf = tx_ring->tx_bi; 4025 tx_desc = I40E_TX_DESC(tx_ring, 0); 4026 } 4027 4028 /* update budget accounting */ 4029 budget--; 4030 } while (likely(budget)); 4031 4032 i += tx_ring->count; 4033 tx_ring->next_to_clean = i; 4034 4035 if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED) 4036 i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx); 4037 4038 return budget > 0; 4039 } 4040 4041 /** 4042 * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring 4043 * @irq: interrupt number 4044 * @data: pointer to a q_vector 4045 **/ 4046 static irqreturn_t i40e_fdir_clean_ring(int irq, void *data) 4047 { 4048 struct i40e_q_vector *q_vector = data; 4049 struct i40e_vsi *vsi; 4050 4051 if (!q_vector->tx.ring) 4052 return IRQ_HANDLED; 4053 4054 vsi = q_vector->tx.ring->vsi; 4055 i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit); 4056 4057 return IRQ_HANDLED; 4058 } 4059 4060 /** 4061 * i40e_map_vector_to_qp - Assigns the queue pair to the vector 4062 * @vsi: the VSI being configured 4063 * @v_idx: vector index 4064 * @qp_idx: queue pair index 4065 **/ 4066 static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx) 4067 { 4068 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx]; 4069 struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx]; 4070 struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx]; 4071 4072 tx_ring->q_vector = q_vector; 4073 tx_ring->next = q_vector->tx.ring; 4074 q_vector->tx.ring = tx_ring; 4075 q_vector->tx.count++; 4076 4077 /* Place XDP Tx ring in the same q_vector ring list as regular Tx */ 4078 if (i40e_enabled_xdp_vsi(vsi)) { 4079 struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx]; 4080 4081 xdp_ring->q_vector = q_vector; 4082 xdp_ring->next = q_vector->tx.ring; 4083 q_vector->tx.ring = xdp_ring; 4084 q_vector->tx.count++; 4085 } 4086 4087 rx_ring->q_vector = q_vector; 4088 rx_ring->next = q_vector->rx.ring; 4089 q_vector->rx.ring = rx_ring; 4090 q_vector->rx.count++; 4091 } 4092 4093 /** 4094 * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors 4095 * @vsi: the VSI being configured 4096 * 4097 * This function maps descriptor rings to the queue-specific vectors 4098 * we were allotted through the MSI-X enabling code. Ideally, we'd have 4099 * one vector per queue pair, but on a constrained vector budget, we 4100 * group the queue pairs as "efficiently" as possible. 4101 **/ 4102 static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi) 4103 { 4104 int qp_remaining = vsi->num_queue_pairs; 4105 int q_vectors = vsi->num_q_vectors; 4106 int num_ringpairs; 4107 int v_start = 0; 4108 int qp_idx = 0; 4109 4110 /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to 4111 * group them so there are multiple queues per vector. 4112 * It is also important to go through all the vectors available to be 4113 * sure that if we don't use all the vectors, that the remaining vectors 4114 * are cleared. This is especially important when decreasing the 4115 * number of queues in use. 4116 */ 4117 for (; v_start < q_vectors; v_start++) { 4118 struct i40e_q_vector *q_vector = vsi->q_vectors[v_start]; 4119 4120 num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start); 4121 4122 q_vector->num_ringpairs = num_ringpairs; 4123 q_vector->reg_idx = q_vector->v_idx + vsi->base_vector - 1; 4124 4125 q_vector->rx.count = 0; 4126 q_vector->tx.count = 0; 4127 q_vector->rx.ring = NULL; 4128 q_vector->tx.ring = NULL; 4129 4130 while (num_ringpairs--) { 4131 i40e_map_vector_to_qp(vsi, v_start, qp_idx); 4132 qp_idx++; 4133 qp_remaining--; 4134 } 4135 } 4136 } 4137 4138 /** 4139 * i40e_vsi_request_irq - Request IRQ from the OS 4140 * @vsi: the VSI being configured 4141 * @basename: name for the vector 4142 **/ 4143 static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename) 4144 { 4145 struct i40e_pf *pf = vsi->back; 4146 int err; 4147 4148 if (pf->flags & I40E_FLAG_MSIX_ENABLED) 4149 err = i40e_vsi_request_irq_msix(vsi, basename); 4150 else if (pf->flags & I40E_FLAG_MSI_ENABLED) 4151 err = request_irq(pf->pdev->irq, i40e_intr, 0, 4152 pf->int_name, pf); 4153 else 4154 err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED, 4155 pf->int_name, pf); 4156 4157 if (err) 4158 dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err); 4159 4160 return err; 4161 } 4162 4163 #ifdef CONFIG_NET_POLL_CONTROLLER 4164 /** 4165 * i40e_netpoll - A Polling 'interrupt' handler 4166 * @netdev: network interface device structure 4167 * 4168 * This is used by netconsole to send skbs without having to re-enable 4169 * interrupts. It's not called while the normal interrupt routine is executing. 4170 **/ 4171 static void i40e_netpoll(struct net_device *netdev) 4172 { 4173 struct i40e_netdev_priv *np = netdev_priv(netdev); 4174 struct i40e_vsi *vsi = np->vsi; 4175 struct i40e_pf *pf = vsi->back; 4176 int i; 4177 4178 /* if interface is down do nothing */ 4179 if (test_bit(__I40E_VSI_DOWN, vsi->state)) 4180 return; 4181 4182 if (pf->flags & I40E_FLAG_MSIX_ENABLED) { 4183 for (i = 0; i < vsi->num_q_vectors; i++) 4184 i40e_msix_clean_rings(0, vsi->q_vectors[i]); 4185 } else { 4186 i40e_intr(pf->pdev->irq, netdev); 4187 } 4188 } 4189 #endif 4190 4191 #define I40E_QTX_ENA_WAIT_COUNT 50 4192 4193 /** 4194 * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled 4195 * @pf: the PF being configured 4196 * @pf_q: the PF queue 4197 * @enable: enable or disable state of the queue 4198 * 4199 * This routine will wait for the given Tx queue of the PF to reach the 4200 * enabled or disabled state. 4201 * Returns -ETIMEDOUT in case of failing to reach the requested state after 4202 * multiple retries; else will return 0 in case of success. 4203 **/ 4204 static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable) 4205 { 4206 int i; 4207 u32 tx_reg; 4208 4209 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) { 4210 tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q)); 4211 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK)) 4212 break; 4213 4214 usleep_range(10, 20); 4215 } 4216 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT) 4217 return -ETIMEDOUT; 4218 4219 return 0; 4220 } 4221 4222 /** 4223 * i40e_control_tx_q - Start or stop a particular Tx queue 4224 * @pf: the PF structure 4225 * @pf_q: the PF queue to configure 4226 * @enable: start or stop the queue 4227 * 4228 * This function enables or disables a single queue. Note that any delay 4229 * required after the operation is expected to be handled by the caller of 4230 * this function. 4231 **/ 4232 static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable) 4233 { 4234 struct i40e_hw *hw = &pf->hw; 4235 u32 tx_reg; 4236 int i; 4237 4238 /* warn the TX unit of coming changes */ 4239 i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable); 4240 if (!enable) 4241 usleep_range(10, 20); 4242 4243 for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) { 4244 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q)); 4245 if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) == 4246 ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1)) 4247 break; 4248 usleep_range(1000, 2000); 4249 } 4250 4251 /* Skip if the queue is already in the requested state */ 4252 if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK)) 4253 return; 4254 4255 /* turn on/off the queue */ 4256 if (enable) { 4257 wr32(hw, I40E_QTX_HEAD(pf_q), 0); 4258 tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK; 4259 } else { 4260 tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK; 4261 } 4262 4263 wr32(hw, I40E_QTX_ENA(pf_q), tx_reg); 4264 } 4265 4266 /** 4267 * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion 4268 * @seid: VSI SEID 4269 * @pf: the PF structure 4270 * @pf_q: the PF queue to configure 4271 * @is_xdp: true if the queue is used for XDP 4272 * @enable: start or stop the queue 4273 **/ 4274 static int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q, 4275 bool is_xdp, bool enable) 4276 { 4277 int ret; 4278 4279 i40e_control_tx_q(pf, pf_q, enable); 4280 4281 /* wait for the change to finish */ 4282 ret = i40e_pf_txq_wait(pf, pf_q, enable); 4283 if (ret) { 4284 dev_info(&pf->pdev->dev, 4285 "VSI seid %d %sTx ring %d %sable timeout\n", 4286 seid, (is_xdp ? "XDP " : ""), pf_q, 4287 (enable ? "en" : "dis")); 4288 } 4289 4290 return ret; 4291 } 4292 4293 /** 4294 * i40e_vsi_control_tx - Start or stop a VSI's rings 4295 * @vsi: the VSI being configured 4296 * @enable: start or stop the rings 4297 **/ 4298 static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable) 4299 { 4300 struct i40e_pf *pf = vsi->back; 4301 int i, pf_q, ret = 0; 4302 4303 pf_q = vsi->base_queue; 4304 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) { 4305 ret = i40e_control_wait_tx_q(vsi->seid, pf, 4306 pf_q, 4307 false /*is xdp*/, enable); 4308 if (ret) 4309 break; 4310 4311 if (!i40e_enabled_xdp_vsi(vsi)) 4312 continue; 4313 4314 ret = i40e_control_wait_tx_q(vsi->seid, pf, 4315 pf_q + vsi->alloc_queue_pairs, 4316 true /*is xdp*/, enable); 4317 if (ret) 4318 break; 4319 } 4320 4321 return ret; 4322 } 4323 4324 /** 4325 * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled 4326 * @pf: the PF being configured 4327 * @pf_q: the PF queue 4328 * @enable: enable or disable state of the queue 4329 * 4330 * This routine will wait for the given Rx queue of the PF to reach the 4331 * enabled or disabled state. 4332 * Returns -ETIMEDOUT in case of failing to reach the requested state after 4333 * multiple retries; else will return 0 in case of success. 4334 **/ 4335 static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable) 4336 { 4337 int i; 4338 u32 rx_reg; 4339 4340 for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) { 4341 rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q)); 4342 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK)) 4343 break; 4344 4345 usleep_range(10, 20); 4346 } 4347 if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT) 4348 return -ETIMEDOUT; 4349 4350 return 0; 4351 } 4352 4353 /** 4354 * i40e_control_rx_q - Start or stop a particular Rx queue 4355 * @pf: the PF structure 4356 * @pf_q: the PF queue to configure 4357 * @enable: start or stop the queue 4358 * 4359 * This function enables or disables a single queue. Note that any delay 4360 * required after the operation is expected to be handled by the caller of 4361 * this function. 4362 **/ 4363 static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable) 4364 { 4365 struct i40e_hw *hw = &pf->hw; 4366 u32 rx_reg; 4367 int i; 4368 4369 for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) { 4370 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q)); 4371 if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) == 4372 ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1)) 4373 break; 4374 usleep_range(1000, 2000); 4375 } 4376 4377 /* Skip if the queue is already in the requested state */ 4378 if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK)) 4379 return; 4380 4381 /* turn on/off the queue */ 4382 if (enable) 4383 rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK; 4384 else 4385 rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK; 4386 4387 wr32(hw, I40E_QRX_ENA(pf_q), rx_reg); 4388 } 4389 4390 /** 4391 * i40e_vsi_control_rx - Start or stop a VSI's rings 4392 * @vsi: the VSI being configured 4393 * @enable: start or stop the rings 4394 **/ 4395 static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable) 4396 { 4397 struct i40e_pf *pf = vsi->back; 4398 int i, pf_q, ret = 0; 4399 4400 pf_q = vsi->base_queue; 4401 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) { 4402 i40e_control_rx_q(pf, pf_q, enable); 4403 4404 /* wait for the change to finish */ 4405 ret = i40e_pf_rxq_wait(pf, pf_q, enable); 4406 if (ret) { 4407 dev_info(&pf->pdev->dev, 4408 "VSI seid %d Rx ring %d %sable timeout\n", 4409 vsi->seid, pf_q, (enable ? "en" : "dis")); 4410 break; 4411 } 4412 } 4413 4414 /* Due to HW errata, on Rx disable only, the register can indicate done 4415 * before it really is. Needs 50ms to be sure 4416 */ 4417 if (!enable) 4418 mdelay(50); 4419 4420 return ret; 4421 } 4422 4423 /** 4424 * i40e_vsi_start_rings - Start a VSI's rings 4425 * @vsi: the VSI being configured 4426 **/ 4427 int i40e_vsi_start_rings(struct i40e_vsi *vsi) 4428 { 4429 int ret = 0; 4430 4431 /* do rx first for enable and last for disable */ 4432 ret = i40e_vsi_control_rx(vsi, true); 4433 if (ret) 4434 return ret; 4435 ret = i40e_vsi_control_tx(vsi, true); 4436 4437 return ret; 4438 } 4439 4440 /** 4441 * i40e_vsi_stop_rings - Stop a VSI's rings 4442 * @vsi: the VSI being configured 4443 **/ 4444 void i40e_vsi_stop_rings(struct i40e_vsi *vsi) 4445 { 4446 /* When port TX is suspended, don't wait */ 4447 if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state)) 4448 return i40e_vsi_stop_rings_no_wait(vsi); 4449 4450 /* do rx first for enable and last for disable 4451 * Ignore return value, we need to shutdown whatever we can 4452 */ 4453 i40e_vsi_control_tx(vsi, false); 4454 i40e_vsi_control_rx(vsi, false); 4455 } 4456 4457 /** 4458 * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay 4459 * @vsi: the VSI being shutdown 4460 * 4461 * This function stops all the rings for a VSI but does not delay to verify 4462 * that rings have been disabled. It is expected that the caller is shutting 4463 * down multiple VSIs at once and will delay together for all the VSIs after 4464 * initiating the shutdown. This is particularly useful for shutting down lots 4465 * of VFs together. Otherwise, a large delay can be incurred while configuring 4466 * each VSI in serial. 4467 **/ 4468 void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi) 4469 { 4470 struct i40e_pf *pf = vsi->back; 4471 int i, pf_q; 4472 4473 pf_q = vsi->base_queue; 4474 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) { 4475 i40e_control_tx_q(pf, pf_q, false); 4476 i40e_control_rx_q(pf, pf_q, false); 4477 } 4478 } 4479 4480 /** 4481 * i40e_vsi_free_irq - Free the irq association with the OS 4482 * @vsi: the VSI being configured 4483 **/ 4484 static void i40e_vsi_free_irq(struct i40e_vsi *vsi) 4485 { 4486 struct i40e_pf *pf = vsi->back; 4487 struct i40e_hw *hw = &pf->hw; 4488 int base = vsi->base_vector; 4489 u32 val, qp; 4490 int i; 4491 4492 if (pf->flags & I40E_FLAG_MSIX_ENABLED) { 4493 if (!vsi->q_vectors) 4494 return; 4495 4496 if (!vsi->irqs_ready) 4497 return; 4498 4499 vsi->irqs_ready = false; 4500 for (i = 0; i < vsi->num_q_vectors; i++) { 4501 int irq_num; 4502 u16 vector; 4503 4504 vector = i + base; 4505 irq_num = pf->msix_entries[vector].vector; 4506 4507 /* free only the irqs that were actually requested */ 4508 if (!vsi->q_vectors[i] || 4509 !vsi->q_vectors[i]->num_ringpairs) 4510 continue; 4511 4512 /* clear the affinity notifier in the IRQ descriptor */ 4513 irq_set_affinity_notifier(irq_num, NULL); 4514 /* remove our suggested affinity mask for this IRQ */ 4515 irq_set_affinity_hint(irq_num, NULL); 4516 synchronize_irq(irq_num); 4517 free_irq(irq_num, vsi->q_vectors[i]); 4518 4519 /* Tear down the interrupt queue link list 4520 * 4521 * We know that they come in pairs and always 4522 * the Rx first, then the Tx. To clear the 4523 * link list, stick the EOL value into the 4524 * next_q field of the registers. 4525 */ 4526 val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1)); 4527 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK) 4528 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT; 4529 val |= I40E_QUEUE_END_OF_LIST 4530 << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT; 4531 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val); 4532 4533 while (qp != I40E_QUEUE_END_OF_LIST) { 4534 u32 next; 4535 4536 val = rd32(hw, I40E_QINT_RQCTL(qp)); 4537 4538 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK | 4539 I40E_QINT_RQCTL_MSIX0_INDX_MASK | 4540 I40E_QINT_RQCTL_CAUSE_ENA_MASK | 4541 I40E_QINT_RQCTL_INTEVENT_MASK); 4542 4543 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK | 4544 I40E_QINT_RQCTL_NEXTQ_INDX_MASK); 4545 4546 wr32(hw, I40E_QINT_RQCTL(qp), val); 4547 4548 val = rd32(hw, I40E_QINT_TQCTL(qp)); 4549 4550 next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK) 4551 >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT; 4552 4553 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK | 4554 I40E_QINT_TQCTL_MSIX0_INDX_MASK | 4555 I40E_QINT_TQCTL_CAUSE_ENA_MASK | 4556 I40E_QINT_TQCTL_INTEVENT_MASK); 4557 4558 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK | 4559 I40E_QINT_TQCTL_NEXTQ_INDX_MASK); 4560 4561 wr32(hw, I40E_QINT_TQCTL(qp), val); 4562 qp = next; 4563 } 4564 } 4565 } else { 4566 free_irq(pf->pdev->irq, pf); 4567 4568 val = rd32(hw, I40E_PFINT_LNKLST0); 4569 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK) 4570 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT; 4571 val |= I40E_QUEUE_END_OF_LIST 4572 << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT; 4573 wr32(hw, I40E_PFINT_LNKLST0, val); 4574 4575 val = rd32(hw, I40E_QINT_RQCTL(qp)); 4576 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK | 4577 I40E_QINT_RQCTL_MSIX0_INDX_MASK | 4578 I40E_QINT_RQCTL_CAUSE_ENA_MASK | 4579 I40E_QINT_RQCTL_INTEVENT_MASK); 4580 4581 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK | 4582 I40E_QINT_RQCTL_NEXTQ_INDX_MASK); 4583 4584 wr32(hw, I40E_QINT_RQCTL(qp), val); 4585 4586 val = rd32(hw, I40E_QINT_TQCTL(qp)); 4587 4588 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK | 4589 I40E_QINT_TQCTL_MSIX0_INDX_MASK | 4590 I40E_QINT_TQCTL_CAUSE_ENA_MASK | 4591 I40E_QINT_TQCTL_INTEVENT_MASK); 4592 4593 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK | 4594 I40E_QINT_TQCTL_NEXTQ_INDX_MASK); 4595 4596 wr32(hw, I40E_QINT_TQCTL(qp), val); 4597 } 4598 } 4599 4600 /** 4601 * i40e_free_q_vector - Free memory allocated for specific interrupt vector 4602 * @vsi: the VSI being configured 4603 * @v_idx: Index of vector to be freed 4604 * 4605 * This function frees the memory allocated to the q_vector. In addition if 4606 * NAPI is enabled it will delete any references to the NAPI struct prior 4607 * to freeing the q_vector. 4608 **/ 4609 static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx) 4610 { 4611 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx]; 4612 struct i40e_ring *ring; 4613 4614 if (!q_vector) 4615 return; 4616 4617 /* disassociate q_vector from rings */ 4618 i40e_for_each_ring(ring, q_vector->tx) 4619 ring->q_vector = NULL; 4620 4621 i40e_for_each_ring(ring, q_vector->rx) 4622 ring->q_vector = NULL; 4623 4624 /* only VSI w/ an associated netdev is set up w/ NAPI */ 4625 if (vsi->netdev) 4626 netif_napi_del(&q_vector->napi); 4627 4628 vsi->q_vectors[v_idx] = NULL; 4629 4630 kfree_rcu(q_vector, rcu); 4631 } 4632 4633 /** 4634 * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors 4635 * @vsi: the VSI being un-configured 4636 * 4637 * This frees the memory allocated to the q_vectors and 4638 * deletes references to the NAPI struct. 4639 **/ 4640 static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi) 4641 { 4642 int v_idx; 4643 4644 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++) 4645 i40e_free_q_vector(vsi, v_idx); 4646 } 4647 4648 /** 4649 * i40e_reset_interrupt_capability - Disable interrupt setup in OS 4650 * @pf: board private structure 4651 **/ 4652 static void i40e_reset_interrupt_capability(struct i40e_pf *pf) 4653 { 4654 /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */ 4655 if (pf->flags & I40E_FLAG_MSIX_ENABLED) { 4656 pci_disable_msix(pf->pdev); 4657 kfree(pf->msix_entries); 4658 pf->msix_entries = NULL; 4659 kfree(pf->irq_pile); 4660 pf->irq_pile = NULL; 4661 } else if (pf->flags & I40E_FLAG_MSI_ENABLED) { 4662 pci_disable_msi(pf->pdev); 4663 } 4664 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED); 4665 } 4666 4667 /** 4668 * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings 4669 * @pf: board private structure 4670 * 4671 * We go through and clear interrupt specific resources and reset the structure 4672 * to pre-load conditions 4673 **/ 4674 static void i40e_clear_interrupt_scheme(struct i40e_pf *pf) 4675 { 4676 int i; 4677 4678 i40e_free_misc_vector(pf); 4679 4680 i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector, 4681 I40E_IWARP_IRQ_PILE_ID); 4682 4683 i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1); 4684 for (i = 0; i < pf->num_alloc_vsi; i++) 4685 if (pf->vsi[i]) 4686 i40e_vsi_free_q_vectors(pf->vsi[i]); 4687 i40e_reset_interrupt_capability(pf); 4688 } 4689 4690 /** 4691 * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI 4692 * @vsi: the VSI being configured 4693 **/ 4694 static void i40e_napi_enable_all(struct i40e_vsi *vsi) 4695 { 4696 int q_idx; 4697 4698 if (!vsi->netdev) 4699 return; 4700 4701 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) { 4702 struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx]; 4703 4704 if (q_vector->rx.ring || q_vector->tx.ring) 4705 napi_enable(&q_vector->napi); 4706 } 4707 } 4708 4709 /** 4710 * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI 4711 * @vsi: the VSI being configured 4712 **/ 4713 static void i40e_napi_disable_all(struct i40e_vsi *vsi) 4714 { 4715 int q_idx; 4716 4717 if (!vsi->netdev) 4718 return; 4719 4720 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) { 4721 struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx]; 4722 4723 if (q_vector->rx.ring || q_vector->tx.ring) 4724 napi_disable(&q_vector->napi); 4725 } 4726 } 4727 4728 /** 4729 * i40e_vsi_close - Shut down a VSI 4730 * @vsi: the vsi to be quelled 4731 **/ 4732 static void i40e_vsi_close(struct i40e_vsi *vsi) 4733 { 4734 struct i40e_pf *pf = vsi->back; 4735 if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state)) 4736 i40e_down(vsi); 4737 i40e_vsi_free_irq(vsi); 4738 i40e_vsi_free_tx_resources(vsi); 4739 i40e_vsi_free_rx_resources(vsi); 4740 vsi->current_netdev_flags = 0; 4741 pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED; 4742 if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) 4743 pf->flags |= I40E_FLAG_CLIENT_RESET; 4744 } 4745 4746 /** 4747 * i40e_quiesce_vsi - Pause a given VSI 4748 * @vsi: the VSI being paused 4749 **/ 4750 static void i40e_quiesce_vsi(struct i40e_vsi *vsi) 4751 { 4752 if (test_bit(__I40E_VSI_DOWN, vsi->state)) 4753 return; 4754 4755 set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state); 4756 if (vsi->netdev && netif_running(vsi->netdev)) 4757 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev); 4758 else 4759 i40e_vsi_close(vsi); 4760 } 4761 4762 /** 4763 * i40e_unquiesce_vsi - Resume a given VSI 4764 * @vsi: the VSI being resumed 4765 **/ 4766 static void i40e_unquiesce_vsi(struct i40e_vsi *vsi) 4767 { 4768 if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state)) 4769 return; 4770 4771 if (vsi->netdev && netif_running(vsi->netdev)) 4772 vsi->netdev->netdev_ops->ndo_open(vsi->netdev); 4773 else 4774 i40e_vsi_open(vsi); /* this clears the DOWN bit */ 4775 } 4776 4777 /** 4778 * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF 4779 * @pf: the PF 4780 **/ 4781 static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf) 4782 { 4783 int v; 4784 4785 for (v = 0; v < pf->num_alloc_vsi; v++) { 4786 if (pf->vsi[v]) 4787 i40e_quiesce_vsi(pf->vsi[v]); 4788 } 4789 } 4790 4791 /** 4792 * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF 4793 * @pf: the PF 4794 **/ 4795 static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf) 4796 { 4797 int v; 4798 4799 for (v = 0; v < pf->num_alloc_vsi; v++) { 4800 if (pf->vsi[v]) 4801 i40e_unquiesce_vsi(pf->vsi[v]); 4802 } 4803 } 4804 4805 /** 4806 * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled 4807 * @vsi: the VSI being configured 4808 * 4809 * Wait until all queues on a given VSI have been disabled. 4810 **/ 4811 int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi) 4812 { 4813 struct i40e_pf *pf = vsi->back; 4814 int i, pf_q, ret; 4815 4816 pf_q = vsi->base_queue; 4817 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) { 4818 /* Check and wait for the Tx queue */ 4819 ret = i40e_pf_txq_wait(pf, pf_q, false); 4820 if (ret) { 4821 dev_info(&pf->pdev->dev, 4822 "VSI seid %d Tx ring %d disable timeout\n", 4823 vsi->seid, pf_q); 4824 return ret; 4825 } 4826 4827 if (!i40e_enabled_xdp_vsi(vsi)) 4828 goto wait_rx; 4829 4830 /* Check and wait for the XDP Tx queue */ 4831 ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs, 4832 false); 4833 if (ret) { 4834 dev_info(&pf->pdev->dev, 4835 "VSI seid %d XDP Tx ring %d disable timeout\n", 4836 vsi->seid, pf_q); 4837 return ret; 4838 } 4839 wait_rx: 4840 /* Check and wait for the Rx queue */ 4841 ret = i40e_pf_rxq_wait(pf, pf_q, false); 4842 if (ret) { 4843 dev_info(&pf->pdev->dev, 4844 "VSI seid %d Rx ring %d disable timeout\n", 4845 vsi->seid, pf_q); 4846 return ret; 4847 } 4848 } 4849 4850 return 0; 4851 } 4852 4853 #ifdef CONFIG_I40E_DCB 4854 /** 4855 * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled 4856 * @pf: the PF 4857 * 4858 * This function waits for the queues to be in disabled state for all the 4859 * VSIs that are managed by this PF. 4860 **/ 4861 static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf) 4862 { 4863 int v, ret = 0; 4864 4865 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) { 4866 if (pf->vsi[v]) { 4867 ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]); 4868 if (ret) 4869 break; 4870 } 4871 } 4872 4873 return ret; 4874 } 4875 4876 #endif 4877 4878 /** 4879 * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP 4880 * @pf: pointer to PF 4881 * 4882 * Get TC map for ISCSI PF type that will include iSCSI TC 4883 * and LAN TC. 4884 **/ 4885 static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf) 4886 { 4887 struct i40e_dcb_app_priority_table app; 4888 struct i40e_hw *hw = &pf->hw; 4889 u8 enabled_tc = 1; /* TC0 is always enabled */ 4890 u8 tc, i; 4891 /* Get the iSCSI APP TLV */ 4892 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config; 4893 4894 for (i = 0; i < dcbcfg->numapps; i++) { 4895 app = dcbcfg->app[i]; 4896 if (app.selector == I40E_APP_SEL_TCPIP && 4897 app.protocolid == I40E_APP_PROTOID_ISCSI) { 4898 tc = dcbcfg->etscfg.prioritytable[app.priority]; 4899 enabled_tc |= BIT(tc); 4900 break; 4901 } 4902 } 4903 4904 return enabled_tc; 4905 } 4906 4907 /** 4908 * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config 4909 * @dcbcfg: the corresponding DCBx configuration structure 4910 * 4911 * Return the number of TCs from given DCBx configuration 4912 **/ 4913 static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg) 4914 { 4915 int i, tc_unused = 0; 4916 u8 num_tc = 0; 4917 u8 ret = 0; 4918 4919 /* Scan the ETS Config Priority Table to find 4920 * traffic class enabled for a given priority 4921 * and create a bitmask of enabled TCs 4922 */ 4923 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) 4924 num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]); 4925 4926 /* Now scan the bitmask to check for 4927 * contiguous TCs starting with TC0 4928 */ 4929 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) { 4930 if (num_tc & BIT(i)) { 4931 if (!tc_unused) { 4932 ret++; 4933 } else { 4934 pr_err("Non-contiguous TC - Disabling DCB\n"); 4935 return 1; 4936 } 4937 } else { 4938 tc_unused = 1; 4939 } 4940 } 4941 4942 /* There is always at least TC0 */ 4943 if (!ret) 4944 ret = 1; 4945 4946 return ret; 4947 } 4948 4949 /** 4950 * i40e_dcb_get_enabled_tc - Get enabled traffic classes 4951 * @dcbcfg: the corresponding DCBx configuration structure 4952 * 4953 * Query the current DCB configuration and return the number of 4954 * traffic classes enabled from the given DCBX config 4955 **/ 4956 static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg) 4957 { 4958 u8 num_tc = i40e_dcb_get_num_tc(dcbcfg); 4959 u8 enabled_tc = 1; 4960 u8 i; 4961 4962 for (i = 0; i < num_tc; i++) 4963 enabled_tc |= BIT(i); 4964 4965 return enabled_tc; 4966 } 4967 4968 /** 4969 * i40e_mqprio_get_enabled_tc - Get enabled traffic classes 4970 * @pf: PF being queried 4971 * 4972 * Query the current MQPRIO configuration and return the number of 4973 * traffic classes enabled. 4974 **/ 4975 static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf) 4976 { 4977 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi]; 4978 u8 num_tc = vsi->mqprio_qopt.qopt.num_tc; 4979 u8 enabled_tc = 1, i; 4980 4981 for (i = 1; i < num_tc; i++) 4982 enabled_tc |= BIT(i); 4983 return enabled_tc; 4984 } 4985 4986 /** 4987 * i40e_pf_get_num_tc - Get enabled traffic classes for PF 4988 * @pf: PF being queried 4989 * 4990 * Return number of traffic classes enabled for the given PF 4991 **/ 4992 static u8 i40e_pf_get_num_tc(struct i40e_pf *pf) 4993 { 4994 struct i40e_hw *hw = &pf->hw; 4995 u8 i, enabled_tc = 1; 4996 u8 num_tc = 0; 4997 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config; 4998 4999 if (pf->flags & I40E_FLAG_TC_MQPRIO) 5000 return pf->vsi[pf->lan_vsi]->mqprio_qopt.qopt.num_tc; 5001 5002 /* If neither MQPRIO nor DCB is enabled, then always use single TC */ 5003 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) 5004 return 1; 5005 5006 /* SFP mode will be enabled for all TCs on port */ 5007 if (!(pf->flags & I40E_FLAG_MFP_ENABLED)) 5008 return i40e_dcb_get_num_tc(dcbcfg); 5009 5010 /* MFP mode return count of enabled TCs for this PF */ 5011 if (pf->hw.func_caps.iscsi) 5012 enabled_tc = i40e_get_iscsi_tc_map(pf); 5013 else 5014 return 1; /* Only TC0 */ 5015 5016 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) { 5017 if (enabled_tc & BIT(i)) 5018 num_tc++; 5019 } 5020 return num_tc; 5021 } 5022 5023 /** 5024 * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes 5025 * @pf: PF being queried 5026 * 5027 * Return a bitmap for enabled traffic classes for this PF. 5028 **/ 5029 static u8 i40e_pf_get_tc_map(struct i40e_pf *pf) 5030 { 5031 if (pf->flags & I40E_FLAG_TC_MQPRIO) 5032 return i40e_mqprio_get_enabled_tc(pf); 5033 5034 /* If neither MQPRIO nor DCB is enabled for this PF then just return 5035 * default TC 5036 */ 5037 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) 5038 return I40E_DEFAULT_TRAFFIC_CLASS; 5039 5040 /* SFP mode we want PF to be enabled for all TCs */ 5041 if (!(pf->flags & I40E_FLAG_MFP_ENABLED)) 5042 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config); 5043 5044 /* MFP enabled and iSCSI PF type */ 5045 if (pf->hw.func_caps.iscsi) 5046 return i40e_get_iscsi_tc_map(pf); 5047 else 5048 return I40E_DEFAULT_TRAFFIC_CLASS; 5049 } 5050 5051 /** 5052 * i40e_vsi_get_bw_info - Query VSI BW Information 5053 * @vsi: the VSI being queried 5054 * 5055 * Returns 0 on success, negative value on failure 5056 **/ 5057 static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi) 5058 { 5059 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0}; 5060 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0}; 5061 struct i40e_pf *pf = vsi->back; 5062 struct i40e_hw *hw = &pf->hw; 5063 i40e_status ret; 5064 u32 tc_bw_max; 5065 int i; 5066 5067 /* Get the VSI level BW configuration */ 5068 ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL); 5069 if (ret) { 5070 dev_info(&pf->pdev->dev, 5071 "couldn't get PF vsi bw config, err %s aq_err %s\n", 5072 i40e_stat_str(&pf->hw, ret), 5073 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status)); 5074 return -EINVAL; 5075 } 5076 5077 /* Get the VSI level BW configuration per TC */ 5078 ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config, 5079 NULL); 5080 if (ret) { 5081 dev_info(&pf->pdev->dev, 5082 "couldn't get PF vsi ets bw config, err %s aq_err %s\n", 5083 i40e_stat_str(&pf->hw, ret), 5084 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status)); 5085 return -EINVAL; 5086 } 5087 5088 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) { 5089 dev_info(&pf->pdev->dev, 5090 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n", 5091 bw_config.tc_valid_bits, 5092 bw_ets_config.tc_valid_bits); 5093 /* Still continuing */ 5094 } 5095 5096 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit); 5097 vsi->bw_max_quanta = bw_config.max_bw; 5098 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) | 5099 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16); 5100 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) { 5101 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i]; 5102 vsi->bw_ets_limit_credits[i] = 5103 le16_to_cpu(bw_ets_config.credits[i]); 5104 /* 3 bits out of 4 for each TC */ 5105 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7); 5106 } 5107 5108 return 0; 5109 } 5110 5111 /** 5112 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC 5113 * @vsi: the VSI being configured 5114 * @enabled_tc: TC bitmap 5115 * @bw_credits: BW shared credits per TC 5116 * 5117 * Returns 0 on success, negative value on failure 5118 **/ 5119 static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc, 5120 u8 *bw_share) 5121 { 5122 struct i40e_aqc_configure_vsi_tc_bw_data bw_data; 5123 i40e_status ret; 5124 int i; 5125 5126 if (vsi->back->flags & I40E_FLAG_TC_MQPRIO) 5127 return 0; 5128 if (!vsi->mqprio_qopt.qopt.hw) { 5129 ret = i40e_set_bw_limit(vsi, vsi->seid, 0); 5130 if (ret) 5131 dev_info(&vsi->back->pdev->dev, 5132 "Failed to reset tx rate for vsi->seid %u\n", 5133 vsi->seid); 5134 return ret; 5135 } 5136 bw_data.tc_valid_bits = enabled_tc; 5137 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) 5138 bw_data.tc_bw_credits[i] = bw_share[i]; 5139 5140 ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data, 5141 NULL); 5142 if (ret) { 5143 dev_info(&vsi->back->pdev->dev, 5144 "AQ command Config VSI BW allocation per TC failed = %d\n", 5145 vsi->back->hw.aq.asq_last_status); 5146 return -EINVAL; 5147 } 5148 5149 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) 5150 vsi->info.qs_handle[i] = bw_data.qs_handles[i]; 5151 5152 return 0; 5153 } 5154 5155 /** 5156 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration 5157 * @vsi: the VSI being configured 5158 * @enabled_tc: TC map to be enabled 5159 * 5160 **/ 5161 static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc) 5162 { 5163 struct net_device *netdev = vsi->netdev; 5164 struct i40e_pf *pf = vsi->back; 5165 struct i40e_hw *hw = &pf->hw; 5166 u8 netdev_tc = 0; 5167 int i; 5168 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config; 5169 5170 if (!netdev) 5171 return; 5172 5173 if (!enabled_tc) { 5174 netdev_reset_tc(netdev); 5175 return; 5176 } 5177 5178 /* Set up actual enabled TCs on the VSI */ 5179 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc)) 5180 return; 5181 5182 /* set per TC queues for the VSI */ 5183 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) { 5184 /* Only set TC queues for enabled tcs 5185 * 5186 * e.g. For a VSI that has TC0 and TC3 enabled the 5187 * enabled_tc bitmap would be 0x00001001; the driver 5188 * will set the numtc for netdev as 2 that will be 5189 * referenced by the netdev layer as TC 0 and 1. 5190 */ 5191 if (vsi->tc_config.enabled_tc & BIT(i)) 5192 netdev_set_tc_queue(netdev, 5193 vsi->tc_config.tc_info[i].netdev_tc, 5194 vsi->tc_config.tc_info[i].qcount, 5195 vsi->tc_config.tc_info[i].qoffset); 5196 } 5197 5198 if (pf->flags & I40E_FLAG_TC_MQPRIO) 5199 return; 5200 5201 /* Assign UP2TC map for the VSI */ 5202 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) { 5203 /* Get the actual TC# for the UP */ 5204 u8 ets_tc = dcbcfg->etscfg.prioritytable[i]; 5205 /* Get the mapped netdev TC# for the UP */ 5206 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc; 5207 netdev_set_prio_tc_map(netdev, i, netdev_tc); 5208 } 5209 } 5210 5211 /** 5212 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map 5213 * @vsi: the VSI being configured 5214 * @ctxt: the ctxt buffer returned from AQ VSI update param command 5215 **/ 5216 static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi, 5217 struct i40e_vsi_context *ctxt) 5218 { 5219 /* copy just the sections touched not the entire info 5220 * since not all sections are valid as returned by 5221 * update vsi params 5222 */ 5223 vsi->info.mapping_flags = ctxt->info.mapping_flags; 5224 memcpy(&vsi->info.queue_mapping, 5225 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping)); 5226 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping, 5227 sizeof(vsi->info.tc_mapping)); 5228 } 5229 5230 /** 5231 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map 5232 * @vsi: VSI to be configured 5233 * @enabled_tc: TC bitmap 5234 * 5235 * This configures a particular VSI for TCs that are mapped to the 5236 * given TC bitmap. It uses default bandwidth share for TCs across 5237 * VSIs to configure TC for a particular VSI. 5238 * 5239 * NOTE: 5240 * It is expected that the VSI queues have been quisced before calling 5241 * this function. 5242 **/ 5243 static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc) 5244 { 5245 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0}; 5246 struct i40e_pf *pf = vsi->back; 5247 struct i40e_hw *hw = &pf->hw; 5248 struct i40e_vsi_context ctxt; 5249 int ret = 0; 5250 int i; 5251 5252 /* Check if enabled_tc is same as existing or new TCs */ 5253 if (vsi->tc_config.enabled_tc == enabled_tc && 5254 vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL) 5255 return ret; 5256 5257 /* Enable ETS TCs with equal BW Share for now across all VSIs */ 5258 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) { 5259 if (enabled_tc & BIT(i)) 5260 bw_share[i] = 1; 5261 } 5262 5263 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share); 5264 if (ret) { 5265 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0}; 5266 5267 dev_info(&pf->pdev->dev, 5268 "Failed configuring TC map %d for VSI %d\n", 5269 enabled_tc, vsi->seid); 5270 ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, 5271 &bw_config, NULL); 5272 if (ret) { 5273 dev_info(&pf->pdev->dev, 5274 "Failed querying vsi bw info, err %s aq_err %s\n", 5275 i40e_stat_str(hw, ret), 5276 i40e_aq_str(hw, hw->aq.asq_last_status)); 5277 goto out; 5278 } 5279 if ((bw_config.tc_valid_bits & enabled_tc) != enabled_tc) { 5280 u8 valid_tc = bw_config.tc_valid_bits & enabled_tc; 5281 5282 if (!valid_tc) 5283 valid_tc = bw_config.tc_valid_bits; 5284 /* Always enable TC0, no matter what */ 5285 valid_tc |= 1; 5286 dev_info(&pf->pdev->dev, 5287 "Requested tc 0x%x, but FW reports 0x%x as valid. Attempting to use 0x%x.\n", 5288 enabled_tc, bw_config.tc_valid_bits, valid_tc); 5289 enabled_tc = valid_tc; 5290 } 5291 5292 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share); 5293 if (ret) { 5294 dev_err(&pf->pdev->dev, 5295 "Unable to configure TC map %d for VSI %d\n", 5296 enabled_tc, vsi->seid); 5297 goto out; 5298 } 5299 } 5300 5301 /* Update Queue Pairs Mapping for currently enabled UPs */ 5302 ctxt.seid = vsi->seid; 5303 ctxt.pf_num = vsi->back->hw.pf_id; 5304 ctxt.vf_num = 0; 5305 ctxt.uplink_seid = vsi->uplink_seid; 5306 ctxt.info = vsi->info; 5307 if (vsi->back->flags & I40E_FLAG_TC_MQPRIO) { 5308 ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc); 5309 if (ret) 5310 goto out; 5311 } else { 5312 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false); 5313 } 5314 5315 /* On destroying the qdisc, reset vsi->rss_size, as number of enabled 5316 * queues changed. 5317 */ 5318 if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) { 5319 vsi->rss_size = min_t(int, vsi->back->alloc_rss_size, 5320 vsi->num_queue_pairs); 5321 ret = i40e_vsi_config_rss(vsi); 5322 if (ret) { 5323 dev_info(&vsi->back->pdev->dev, 5324 "Failed to reconfig rss for num_queues\n"); 5325 return ret; 5326 } 5327 vsi->reconfig_rss = false; 5328 } 5329 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) { 5330 ctxt.info.valid_sections |= 5331 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID); 5332 ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA; 5333 } 5334 5335 /* Update the VSI after updating the VSI queue-mapping 5336 * information 5337 */ 5338 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL); 5339 if (ret) { 5340 dev_info(&pf->pdev->dev, 5341 "Update vsi tc config failed, err %s aq_err %s\n", 5342 i40e_stat_str(hw, ret), 5343 i40e_aq_str(hw, hw->aq.asq_last_status)); 5344 goto out; 5345 } 5346 /* update the local VSI info with updated queue map */ 5347 i40e_vsi_update_queue_map(vsi, &ctxt); 5348 vsi->info.valid_sections = 0; 5349 5350 /* Update current VSI BW information */ 5351 ret = i40e_vsi_get_bw_info(vsi); 5352 if (ret) { 5353 dev_info(&pf->pdev->dev, 5354 "Failed updating vsi bw info, err %s aq_err %s\n", 5355 i40e_stat_str(hw, ret), 5356 i40e_aq_str(hw, hw->aq.asq_last_status)); 5357 goto out; 5358 } 5359 5360 /* Update the netdev TC setup */ 5361 i40e_vsi_config_netdev_tc(vsi, enabled_tc); 5362 out: 5363 return ret; 5364 } 5365 5366 /** 5367 * i40e_get_link_speed - Returns link speed for the interface 5368 * @vsi: VSI to be configured 5369 * 5370 **/ 5371 static int i40e_get_link_speed(struct i40e_vsi *vsi) 5372 { 5373 struct i40e_pf *pf = vsi->back; 5374 5375 switch (pf->hw.phy.link_info.link_speed) { 5376 case I40E_LINK_SPEED_40GB: 5377 return 40000; 5378 case I40E_LINK_SPEED_25GB: 5379 return 25000; 5380 case I40E_LINK_SPEED_20GB: 5381 return 20000; 5382 case I40E_LINK_SPEED_10GB: 5383 return 10000; 5384 case I40E_LINK_SPEED_1GB: 5385 return 1000; 5386 default: 5387 return -EINVAL; 5388 } 5389 } 5390 5391 /** 5392 * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate 5393 * @vsi: VSI to be configured 5394 * @seid: seid of the channel/VSI 5395 * @max_tx_rate: max TX rate to be configured as BW limit 5396 * 5397 * Helper function to set BW limit for a given VSI 5398 **/ 5399 int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate) 5400 { 5401 struct i40e_pf *pf = vsi->back; 5402 u64 credits = 0; 5403 int speed = 0; 5404 int ret = 0; 5405 5406 speed = i40e_get_link_speed(vsi); 5407 if (max_tx_rate > speed) { 5408 dev_err(&pf->pdev->dev, 5409 "Invalid max tx rate %llu specified for VSI seid %d.", 5410 max_tx_rate, seid); 5411 return -EINVAL; 5412 } 5413 if (max_tx_rate && max_tx_rate < 50) { 5414 dev_warn(&pf->pdev->dev, 5415 "Setting max tx rate to minimum usable value of 50Mbps.\n"); 5416 max_tx_rate = 50; 5417 } 5418 5419 /* Tx rate credits are in values of 50Mbps, 0 is disabled */ 5420 credits = max_tx_rate; 5421 do_div(credits, I40E_BW_CREDIT_DIVISOR); 5422 ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits, 5423 I40E_MAX_BW_INACTIVE_ACCUM, NULL); 5424 if (ret) 5425 dev_err(&pf->pdev->dev, 5426 "Failed set tx rate (%llu Mbps) for vsi->seid %u, err %s aq_err %s\n", 5427 max_tx_rate, seid, i40e_stat_str(&pf->hw, ret), 5428 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status)); 5429 return ret; 5430 } 5431 5432 /** 5433 * i40e_remove_queue_channels - Remove queue channels for the TCs 5434 * @vsi: VSI to be configured 5435 * 5436 * Remove queue channels for the TCs 5437 **/ 5438 static void i40e_remove_queue_channels(struct i40e_vsi *vsi) 5439 { 5440 enum i40e_admin_queue_err last_aq_status; 5441 struct i40e_cloud_filter *cfilter; 5442 struct i40e_channel *ch, *ch_tmp; 5443 struct i40e_pf *pf = vsi->back; 5444 struct hlist_node *node; 5445 int ret, i; 5446 5447 /* Reset rss size that was stored when reconfiguring rss for 5448 * channel VSIs with non-power-of-2 queue count. 5449 */ 5450 vsi->current_rss_size = 0; 5451 5452 /* perform cleanup for channels if they exist */ 5453 if (list_empty(&vsi->ch_list)) 5454 return; 5455 5456 list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) { 5457 struct i40e_vsi *p_vsi; 5458 5459 list_del(&ch->list); 5460 p_vsi = ch->parent_vsi; 5461 if (!p_vsi || !ch->initialized) { 5462 kfree(ch); 5463 continue; 5464 } 5465 /* Reset queue contexts */ 5466 for (i = 0; i < ch->num_queue_pairs; i++) { 5467 struct i40e_ring *tx_ring, *rx_ring; 5468 u16 pf_q; 5469 5470 pf_q = ch->base_queue + i; 5471 tx_ring = vsi->tx_rings[pf_q]; 5472 tx_ring->ch = NULL; 5473 5474 rx_ring = vsi->rx_rings[pf_q]; 5475 rx_ring->ch = NULL; 5476 } 5477 5478 /* Reset BW configured for this VSI via mqprio */ 5479 ret = i40e_set_bw_limit(vsi, ch->seid, 0); 5480 if (ret) 5481 dev_info(&vsi->back->pdev->dev, 5482 "Failed to reset tx rate for ch->seid %u\n", 5483 ch->seid); 5484 5485 /* delete cloud filters associated with this channel */ 5486 hlist_for_each_entry_safe(cfilter, node, 5487 &pf->cloud_filter_list, cloud_node) { 5488 if (cfilter->seid != ch->seid) 5489 continue; 5490 5491 hash_del(&cfilter->cloud_node); 5492 if (cfilter->dst_port) 5493 ret = i40e_add_del_cloud_filter_big_buf(vsi, 5494 cfilter, 5495 false); 5496 else 5497 ret = i40e_add_del_cloud_filter(vsi, cfilter, 5498 false); 5499 last_aq_status = pf->hw.aq.asq_last_status; 5500 if (ret) 5501 dev_info(&pf->pdev->dev, 5502 "Failed to delete cloud filter, err %s aq_err %s\n", 5503 i40e_stat_str(&pf->hw, ret), 5504 i40e_aq_str(&pf->hw, last_aq_status)); 5505 kfree(cfilter); 5506 } 5507 5508 /* delete VSI from FW */ 5509 ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid, 5510 NULL); 5511 if (ret) 5512 dev_err(&vsi->back->pdev->dev, 5513 "unable to remove channel (%d) for parent VSI(%d)\n", 5514 ch->seid, p_vsi->seid); 5515 kfree(ch); 5516 } 5517 INIT_LIST_HEAD(&vsi->ch_list); 5518 } 5519 5520 /** 5521 * i40e_is_any_channel - channel exist or not 5522 * @vsi: ptr to VSI to which channels are associated with 5523 * 5524 * Returns true or false if channel(s) exist for associated VSI or not 5525 **/ 5526 static bool i40e_is_any_channel(struct i40e_vsi *vsi) 5527 { 5528 struct i40e_channel *ch, *ch_tmp; 5529 5530 list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) { 5531 if (ch->initialized) 5532 return true; 5533 } 5534 5535 return false; 5536 } 5537 5538 /** 5539 * i40e_get_max_queues_for_channel 5540 * @vsi: ptr to VSI to which channels are associated with 5541 * 5542 * Helper function which returns max value among the queue counts set on the 5543 * channels/TCs created. 5544 **/ 5545 static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi) 5546 { 5547 struct i40e_channel *ch, *ch_tmp; 5548 int max = 0; 5549 5550 list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) { 5551 if (!ch->initialized) 5552 continue; 5553 if (ch->num_queue_pairs > max) 5554 max = ch->num_queue_pairs; 5555 } 5556 5557 return max; 5558 } 5559 5560 /** 5561 * i40e_validate_num_queues - validate num_queues w.r.t channel 5562 * @pf: ptr to PF device 5563 * @num_queues: number of queues 5564 * @vsi: the parent VSI 5565 * @reconfig_rss: indicates should the RSS be reconfigured or not 5566 * 5567 * This function validates number of queues in the context of new channel 5568 * which is being established and determines if RSS should be reconfigured 5569 * or not for parent VSI. 5570 **/ 5571 static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues, 5572 struct i40e_vsi *vsi, bool *reconfig_rss) 5573 { 5574 int max_ch_queues; 5575 5576 if (!reconfig_rss) 5577 return -EINVAL; 5578 5579 *reconfig_rss = false; 5580 if (vsi->current_rss_size) { 5581 if (num_queues > vsi->current_rss_size) { 5582 dev_dbg(&pf->pdev->dev, 5583 "Error: num_queues (%d) > vsi's current_size(%d)\n", 5584 num_queues, vsi->current_rss_size); 5585 return -EINVAL; 5586 } else if ((num_queues < vsi->current_rss_size) && 5587 (!is_power_of_2(num_queues))) { 5588 dev_dbg(&pf->pdev->dev, 5589 "Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n", 5590 num_queues, vsi->current_rss_size); 5591 return -EINVAL; 5592 } 5593 } 5594 5595 if (!is_power_of_2(num_queues)) { 5596 /* Find the max num_queues configured for channel if channel 5597 * exist. 5598 * if channel exist, then enforce 'num_queues' to be more than 5599 * max ever queues configured for channel. 5600 */ 5601 max_ch_queues = i40e_get_max_queues_for_channel(vsi); 5602 if (num_queues < max_ch_queues) { 5603 dev_dbg(&pf->pdev->dev, 5604 "Error: num_queues (%d) < max queues configured for channel(%d)\n", 5605 num_queues, max_ch_queues); 5606 return -EINVAL; 5607 } 5608 *reconfig_rss = true; 5609 } 5610 5611 return 0; 5612 } 5613 5614 /** 5615 * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size 5616 * @vsi: the VSI being setup 5617 * @rss_size: size of RSS, accordingly LUT gets reprogrammed 5618 * 5619 * This function reconfigures RSS by reprogramming LUTs using 'rss_size' 5620 **/ 5621 static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size) 5622 { 5623 struct i40e_pf *pf = vsi->back; 5624 u8 seed[I40E_HKEY_ARRAY_SIZE]; 5625 struct i40e_hw *hw = &pf->hw; 5626 int local_rss_size; 5627 u8 *lut; 5628 int ret; 5629 5630 if (!vsi->rss_size) 5631 return -EINVAL; 5632 5633 if (rss_size > vsi->rss_size) 5634 return -EINVAL; 5635 5636 local_rss_size = min_t(int, vsi->rss_size, rss_size); 5637 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL); 5638 if (!lut) 5639 return -ENOMEM; 5640 5641 /* Ignoring user configured lut if there is one */ 5642 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size); 5643 5644 /* Use user configured hash key if there is one, otherwise 5645 * use default. 5646 */ 5647 if (vsi->rss_hkey_user) 5648 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE); 5649 else 5650 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE); 5651 5652 ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size); 5653 if (ret) { 5654 dev_info(&pf->pdev->dev, 5655 "Cannot set RSS lut, err %s aq_err %s\n", 5656 i40e_stat_str(hw, ret), 5657 i40e_aq_str(hw, hw->aq.asq_last_status)); 5658 kfree(lut); 5659 return ret; 5660 } 5661 kfree(lut); 5662 5663 /* Do the update w.r.t. storing rss_size */ 5664 if (!vsi->orig_rss_size) 5665 vsi->orig_rss_size = vsi->rss_size; 5666 vsi->current_rss_size = local_rss_size; 5667 5668 return ret; 5669 } 5670 5671 /** 5672 * i40e_channel_setup_queue_map - Setup a channel queue map 5673 * @pf: ptr to PF device 5674 * @vsi: the VSI being setup 5675 * @ctxt: VSI context structure 5676 * @ch: ptr to channel structure 5677 * 5678 * Setup queue map for a specific channel 5679 **/ 5680 static void i40e_channel_setup_queue_map(struct i40e_pf *pf, 5681 struct i40e_vsi_context *ctxt, 5682 struct i40e_channel *ch) 5683 { 5684 u16 qcount, qmap, sections = 0; 5685 u8 offset = 0; 5686 int pow; 5687 5688 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID; 5689 sections |= I40E_AQ_VSI_PROP_SCHED_VALID; 5690 5691 qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix); 5692 ch->num_queue_pairs = qcount; 5693 5694 /* find the next higher power-of-2 of num queue pairs */ 5695 pow = ilog2(qcount); 5696 if (!is_power_of_2(qcount)) 5697 pow++; 5698 5699 qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) | 5700 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT); 5701 5702 /* Setup queue TC[0].qmap for given VSI context */ 5703 ctxt->info.tc_mapping[0] = cpu_to_le16(qmap); 5704 5705 ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */ 5706 ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG); 5707 ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue); 5708 ctxt->info.valid_sections |= cpu_to_le16(sections); 5709 } 5710 5711 /** 5712 * i40e_add_channel - add a channel by adding VSI 5713 * @pf: ptr to PF device 5714 * @uplink_seid: underlying HW switching element (VEB) ID 5715 * @ch: ptr to channel structure 5716 * 5717 * Add a channel (VSI) using add_vsi and queue_map 5718 **/ 5719 static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid, 5720 struct i40e_channel *ch) 5721 { 5722 struct i40e_hw *hw = &pf->hw; 5723 struct i40e_vsi_context ctxt; 5724 u8 enabled_tc = 0x1; /* TC0 enabled */ 5725 int ret; 5726 5727 if (ch->type != I40E_VSI_VMDQ2) { 5728 dev_info(&pf->pdev->dev, 5729 "add new vsi failed, ch->type %d\n", ch->type); 5730 return -EINVAL; 5731 } 5732 5733 memset(&ctxt, 0, sizeof(ctxt)); 5734 ctxt.pf_num = hw->pf_id; 5735 ctxt.vf_num = 0; 5736 ctxt.uplink_seid = uplink_seid; 5737 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL; 5738 if (ch->type == I40E_VSI_VMDQ2) 5739 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2; 5740 5741 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED) { 5742 ctxt.info.valid_sections |= 5743 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID); 5744 ctxt.info.switch_id = 5745 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB); 5746 } 5747 5748 /* Set queue map for a given VSI context */ 5749 i40e_channel_setup_queue_map(pf, &ctxt, ch); 5750 5751 /* Now time to create VSI */ 5752 ret = i40e_aq_add_vsi(hw, &ctxt, NULL); 5753 if (ret) { 5754 dev_info(&pf->pdev->dev, 5755 "add new vsi failed, err %s aq_err %s\n", 5756 i40e_stat_str(&pf->hw, ret), 5757 i40e_aq_str(&pf->hw, 5758 pf->hw.aq.asq_last_status)); 5759 return -ENOENT; 5760 } 5761 5762 /* Success, update channel */ 5763 ch->enabled_tc = enabled_tc; 5764 ch->seid = ctxt.seid; 5765 ch->vsi_number = ctxt.vsi_number; 5766 ch->stat_counter_idx = cpu_to_le16(ctxt.info.stat_counter_idx); 5767 5768 /* copy just the sections touched not the entire info 5769 * since not all sections are valid as returned by 5770 * update vsi params 5771 */ 5772 ch->info.mapping_flags = ctxt.info.mapping_flags; 5773 memcpy(&ch->info.queue_mapping, 5774 &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping)); 5775 memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping, 5776 sizeof(ctxt.info.tc_mapping)); 5777 5778 return 0; 5779 } 5780 5781 static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch, 5782 u8 *bw_share) 5783 { 5784 struct i40e_aqc_configure_vsi_tc_bw_data bw_data; 5785 i40e_status ret; 5786 int i; 5787 5788 bw_data.tc_valid_bits = ch->enabled_tc; 5789 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) 5790 bw_data.tc_bw_credits[i] = bw_share[i]; 5791 5792 ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid, 5793 &bw_data, NULL); 5794 if (ret) { 5795 dev_info(&vsi->back->pdev->dev, 5796 "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n", 5797 vsi->back->hw.aq.asq_last_status, ch->seid); 5798 return -EINVAL; 5799 } 5800 5801 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) 5802 ch->info.qs_handle[i] = bw_data.qs_handles[i]; 5803 5804 return 0; 5805 } 5806 5807 /** 5808 * i40e_channel_config_tx_ring - config TX ring associated with new channel 5809 * @pf: ptr to PF device 5810 * @vsi: the VSI being setup 5811 * @ch: ptr to channel structure 5812 * 5813 * Configure TX rings associated with channel (VSI) since queues are being 5814 * from parent VSI. 5815 **/ 5816 static int i40e_channel_config_tx_ring(struct i40e_pf *pf, 5817 struct i40e_vsi *vsi, 5818 struct i40e_channel *ch) 5819 { 5820 i40e_status ret; 5821 int i; 5822 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0}; 5823 5824 /* Enable ETS TCs with equal BW Share for now across all VSIs */ 5825 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) { 5826 if (ch->enabled_tc & BIT(i)) 5827 bw_share[i] = 1; 5828 } 5829 5830 /* configure BW for new VSI */ 5831 ret = i40e_channel_config_bw(vsi, ch, bw_share); 5832 if (ret) { 5833 dev_info(&vsi->back->pdev->dev, 5834 "Failed configuring TC map %d for channel (seid %u)\n", 5835 ch->enabled_tc, ch->seid); 5836 return ret; 5837 } 5838 5839 for (i = 0; i < ch->num_queue_pairs; i++) { 5840 struct i40e_ring *tx_ring, *rx_ring; 5841 u16 pf_q; 5842 5843 pf_q = ch->base_queue + i; 5844 5845 /* Get to TX ring ptr of main VSI, for re-setup TX queue 5846 * context 5847 */ 5848 tx_ring = vsi->tx_rings[pf_q]; 5849 tx_ring->ch = ch; 5850 5851 /* Get the RX ring ptr */ 5852 rx_ring = vsi->rx_rings[pf_q]; 5853 rx_ring->ch = ch; 5854 } 5855 5856 return 0; 5857 } 5858 5859 /** 5860 * i40e_setup_hw_channel - setup new channel 5861 * @pf: ptr to PF device 5862 * @vsi: the VSI being setup 5863 * @ch: ptr to channel structure 5864 * @uplink_seid: underlying HW switching element (VEB) ID 5865 * @type: type of channel to be created (VMDq2/VF) 5866 * 5867 * Setup new channel (VSI) based on specified type (VMDq2/VF) 5868 * and configures TX rings accordingly 5869 **/ 5870 static inline int i40e_setup_hw_channel(struct i40e_pf *pf, 5871 struct i40e_vsi *vsi, 5872 struct i40e_channel *ch, 5873 u16 uplink_seid, u8 type) 5874 { 5875 int ret; 5876 5877 ch->initialized = false; 5878 ch->base_queue = vsi->next_base_queue; 5879 ch->type = type; 5880 5881 /* Proceed with creation of channel (VMDq2) VSI */ 5882 ret = i40e_add_channel(pf, uplink_seid, ch); 5883 if (ret) { 5884 dev_info(&pf->pdev->dev, 5885 "failed to add_channel using uplink_seid %u\n", 5886 uplink_seid); 5887 return ret; 5888 } 5889 5890 /* Mark the successful creation of channel */ 5891 ch->initialized = true; 5892 5893 /* Reconfigure TX queues using QTX_CTL register */ 5894 ret = i40e_channel_config_tx_ring(pf, vsi, ch); 5895 if (ret) { 5896 dev_info(&pf->pdev->dev, 5897 "failed to configure TX rings for channel %u\n", 5898 ch->seid); 5899 return ret; 5900 } 5901 5902 /* update 'next_base_queue' */ 5903 vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs; 5904 dev_dbg(&pf->pdev->dev, 5905 "Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n", 5906 ch->seid, ch->vsi_number, ch->stat_counter_idx, 5907 ch->num_queue_pairs, 5908 vsi->next_base_queue); 5909 return ret; 5910 } 5911 5912 /** 5913 * i40e_setup_channel - setup new channel using uplink element 5914 * @pf: ptr to PF device 5915 * @type: type of channel to be created (VMDq2/VF) 5916 * @uplink_seid: underlying HW switching element (VEB) ID 5917 * @ch: ptr to channel structure 5918 * 5919 * Setup new channel (VSI) based on specified type (VMDq2/VF) 5920 * and uplink switching element (uplink_seid) 5921 **/ 5922 static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi, 5923 struct i40e_channel *ch) 5924 { 5925 u8 vsi_type; 5926 u16 seid; 5927 int ret; 5928 5929 if (vsi->type == I40E_VSI_MAIN) { 5930 vsi_type = I40E_VSI_VMDQ2; 5931 } else { 5932 dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n", 5933 vsi->type); 5934 return false; 5935 } 5936 5937 /* underlying switching element */ 5938 seid = pf->vsi[pf->lan_vsi]->uplink_seid; 5939 5940 /* create channel (VSI), configure TX rings */ 5941 ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type); 5942 if (ret) { 5943 dev_err(&pf->pdev->dev, "failed to setup hw_channel\n"); 5944 return false; 5945 } 5946 5947 return ch->initialized ? true : false; 5948 } 5949 5950 /** 5951 * i40e_validate_and_set_switch_mode - sets up switch mode correctly 5952 * @vsi: ptr to VSI which has PF backing 5953 * 5954 * Sets up switch mode correctly if it needs to be changed and perform 5955 * what are allowed modes. 5956 **/ 5957 static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi) 5958 { 5959 u8 mode; 5960 struct i40e_pf *pf = vsi->back; 5961 struct i40e_hw *hw = &pf->hw; 5962 int ret; 5963 5964 ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities); 5965 if (ret) 5966 return -EINVAL; 5967 5968 if (hw->dev_caps.switch_mode) { 5969 /* if switch mode is set, support mode2 (non-tunneled for 5970 * cloud filter) for now 5971 */ 5972 u32 switch_mode = hw->dev_caps.switch_mode & 5973 I40E_SWITCH_MODE_MASK; 5974 if (switch_mode >= I40E_CLOUD_FILTER_MODE1) { 5975 if (switch_mode == I40E_CLOUD_FILTER_MODE2) 5976 return 0; 5977 dev_err(&pf->pdev->dev, 5978 "Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n", 5979 hw->dev_caps.switch_mode); 5980 return -EINVAL; 5981 } 5982 } 5983 5984 /* Set Bit 7 to be valid */ 5985 mode = I40E_AQ_SET_SWITCH_BIT7_VALID; 5986 5987 /* Set L4type for TCP support */ 5988 mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP; 5989 5990 /* Set cloud filter mode */ 5991 mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL; 5992 5993 /* Prep mode field for set_switch_config */ 5994 ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags, 5995 pf->last_sw_conf_valid_flags, 5996 mode, NULL); 5997 if (ret && hw->aq.asq_last_status != I40E_AQ_RC_ESRCH) 5998 dev_err(&pf->pdev->dev, 5999 "couldn't set switch config bits, err %s aq_err %s\n", 6000 i40e_stat_str(hw, ret), 6001 i40e_aq_str(hw, 6002 hw->aq.asq_last_status)); 6003 6004 return ret; 6005 } 6006 6007 /** 6008 * i40e_create_queue_channel - function to create channel 6009 * @vsi: VSI to be configured 6010 * @ch: ptr to channel (it contains channel specific params) 6011 * 6012 * This function creates channel (VSI) using num_queues specified by user, 6013 * reconfigs RSS if needed. 6014 **/ 6015 int i40e_create_queue_channel(struct i40e_vsi *vsi, 6016 struct i40e_channel *ch) 6017 { 6018 struct i40e_pf *pf = vsi->back; 6019 bool reconfig_rss; 6020 int err; 6021 6022 if (!ch) 6023 return -EINVAL; 6024 6025 if (!ch->num_queue_pairs) { 6026 dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n", 6027 ch->num_queue_pairs); 6028 return -EINVAL; 6029 } 6030 6031 /* validate user requested num_queues for channel */ 6032 err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi, 6033 &reconfig_rss); 6034 if (err) { 6035 dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n", 6036 ch->num_queue_pairs); 6037 return -EINVAL; 6038 } 6039 6040 /* By default we are in VEPA mode, if this is the first VF/VMDq 6041 * VSI to be added switch to VEB mode. 6042 */ 6043 if ((!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) || 6044 (!i40e_is_any_channel(vsi))) { 6045 if (!is_power_of_2(vsi->tc_config.tc_info[0].qcount)) { 6046 dev_dbg(&pf->pdev->dev, 6047 "Failed to create channel. Override queues (%u) not power of 2\n", 6048 vsi->tc_config.tc_info[0].qcount); 6049 return -EINVAL; 6050 } 6051 6052 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) { 6053 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED; 6054 6055 if (vsi->type == I40E_VSI_MAIN) { 6056 if (pf->flags & I40E_FLAG_TC_MQPRIO) 6057 i40e_do_reset(pf, I40E_PF_RESET_FLAG, 6058 true); 6059 else 6060 i40e_do_reset_safe(pf, 6061 I40E_PF_RESET_FLAG); 6062 } 6063 } 6064 /* now onwards for main VSI, number of queues will be value 6065 * of TC0's queue count 6066 */ 6067 } 6068 6069 /* By this time, vsi->cnt_q_avail shall be set to non-zero and 6070 * it should be more than num_queues 6071 */ 6072 if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) { 6073 dev_dbg(&pf->pdev->dev, 6074 "Error: cnt_q_avail (%u) less than num_queues %d\n", 6075 vsi->cnt_q_avail, ch->num_queue_pairs); 6076 return -EINVAL; 6077 } 6078 6079 /* reconfig_rss only if vsi type is MAIN_VSI */ 6080 if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) { 6081 err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs); 6082 if (err) { 6083 dev_info(&pf->pdev->dev, 6084 "Error: unable to reconfig rss for num_queues (%u)\n", 6085 ch->num_queue_pairs); 6086 return -EINVAL; 6087 } 6088 } 6089 6090 if (!i40e_setup_channel(pf, vsi, ch)) { 6091 dev_info(&pf->pdev->dev, "Failed to setup channel\n"); 6092 return -EINVAL; 6093 } 6094 6095 dev_info(&pf->pdev->dev, 6096 "Setup channel (id:%u) utilizing num_queues %d\n", 6097 ch->seid, ch->num_queue_pairs); 6098 6099 /* configure VSI for BW limit */ 6100 if (ch->max_tx_rate) { 6101 u64 credits = ch->max_tx_rate; 6102 6103 if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate)) 6104 return -EINVAL; 6105 6106 do_div(credits, I40E_BW_CREDIT_DIVISOR); 6107 dev_dbg(&pf->pdev->dev, 6108 "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n", 6109 ch->max_tx_rate, 6110 credits, 6111 ch->seid); 6112 } 6113 6114 /* in case of VF, this will be main SRIOV VSI */ 6115 ch->parent_vsi = vsi; 6116 6117 /* and update main_vsi's count for queue_available to use */ 6118 vsi->cnt_q_avail -= ch->num_queue_pairs; 6119 6120 return 0; 6121 } 6122 6123 /** 6124 * i40e_configure_queue_channels - Add queue channel for the given TCs 6125 * @vsi: VSI to be configured 6126 * 6127 * Configures queue channel mapping to the given TCs 6128 **/ 6129 static int i40e_configure_queue_channels(struct i40e_vsi *vsi) 6130 { 6131 struct i40e_channel *ch; 6132 u64 max_rate = 0; 6133 int ret = 0, i; 6134 6135 /* Create app vsi with the TCs. Main VSI with TC0 is already set up */ 6136 vsi->tc_seid_map[0] = vsi->seid; 6137 for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) { 6138 if (vsi->tc_config.enabled_tc & BIT(i)) { 6139 ch = kzalloc(sizeof(*ch), GFP_KERNEL); 6140 if (!ch) { 6141 ret = -ENOMEM; 6142 goto err_free; 6143 } 6144 6145 INIT_LIST_HEAD(&ch->list); 6146 ch->num_queue_pairs = 6147 vsi->tc_config.tc_info[i].qcount; 6148 ch->base_queue = 6149 vsi->tc_config.tc_info[i].qoffset; 6150 6151 /* Bandwidth limit through tc interface is in bytes/s, 6152 * change to Mbit/s 6153 */ 6154 max_rate = vsi->mqprio_qopt.max_rate[i]; 6155 do_div(max_rate, I40E_BW_MBPS_DIVISOR); 6156 ch->max_tx_rate = max_rate; 6157 6158 list_add_tail(&ch->list, &vsi->ch_list); 6159 6160 ret = i40e_create_queue_channel(vsi, ch); 6161 if (ret) { 6162 dev_err(&vsi->back->pdev->dev, 6163 "Failed creating queue channel with TC%d: queues %d\n", 6164 i, ch->num_queue_pairs); 6165 goto err_free; 6166 } 6167 vsi->tc_seid_map[i] = ch->seid; 6168 } 6169 } 6170 return ret; 6171 6172 err_free: 6173 i40e_remove_queue_channels(vsi); 6174 return ret; 6175 } 6176 6177 /** 6178 * i40e_veb_config_tc - Configure TCs for given VEB 6179 * @veb: given VEB 6180 * @enabled_tc: TC bitmap 6181 * 6182 * Configures given TC bitmap for VEB (switching) element 6183 **/ 6184 int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc) 6185 { 6186 struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0}; 6187 struct i40e_pf *pf = veb->pf; 6188 int ret = 0; 6189 int i; 6190 6191 /* No TCs or already enabled TCs just return */ 6192 if (!enabled_tc || veb->enabled_tc == enabled_tc) 6193 return ret; 6194 6195 bw_data.tc_valid_bits = enabled_tc; 6196 /* bw_data.absolute_credits is not set (relative) */ 6197 6198 /* Enable ETS TCs with equal BW Share for now */ 6199 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) { 6200 if (enabled_tc & BIT(i)) 6201 bw_data.tc_bw_share_credits[i] = 1; 6202 } 6203 6204 ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid, 6205 &bw_data, NULL); 6206 if (ret) { 6207 dev_info(&pf->pdev->dev, 6208 "VEB bw config failed, err %s aq_err %s\n", 6209 i40e_stat_str(&pf->hw, ret), 6210 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status)); 6211 goto out; 6212 } 6213 6214 /* Update the BW information */ 6215 ret = i40e_veb_get_bw_info(veb); 6216 if (ret) { 6217 dev_info(&pf->pdev->dev, 6218 "Failed getting veb bw config, err %s aq_err %s\n", 6219 i40e_stat_str(&pf->hw, ret), 6220 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status)); 6221 } 6222 6223 out: 6224 return ret; 6225 } 6226 6227 #ifdef CONFIG_I40E_DCB 6228 /** 6229 * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs 6230 * @pf: PF struct 6231 * 6232 * Reconfigure VEB/VSIs on a given PF; it is assumed that 6233 * the caller would've quiesce all the VSIs before calling 6234 * this function 6235 **/ 6236 static void i40e_dcb_reconfigure(struct i40e_pf *pf) 6237 { 6238 u8 tc_map = 0; 6239 int ret; 6240 u8 v; 6241 6242 /* Enable the TCs available on PF to all VEBs */ 6243 tc_map = i40e_pf_get_tc_map(pf); 6244 for (v = 0; v < I40E_MAX_VEB; v++) { 6245 if (!pf->veb[v]) 6246 continue; 6247 ret = i40e_veb_config_tc(pf->veb[v], tc_map); 6248 if (ret) { 6249 dev_info(&pf->pdev->dev, 6250 "Failed configuring TC for VEB seid=%d\n", 6251 pf->veb[v]->seid); 6252 /* Will try to configure as many components */ 6253 } 6254 } 6255 6256 /* Update each VSI */ 6257 for (v = 0; v < pf->num_alloc_vsi; v++) { 6258 if (!pf->vsi[v]) 6259 continue; 6260 6261 /* - Enable all TCs for the LAN VSI 6262 * - For all others keep them at TC0 for now 6263 */ 6264 if (v == pf->lan_vsi) 6265 tc_map = i40e_pf_get_tc_map(pf); 6266 else 6267 tc_map = I40E_DEFAULT_TRAFFIC_CLASS; 6268 6269 ret = i40e_vsi_config_tc(pf->vsi[v], tc_map); 6270 if (ret) { 6271 dev_info(&pf->pdev->dev, 6272 "Failed configuring TC for VSI seid=%d\n", 6273 pf->vsi[v]->seid); 6274 /* Will try to configure as many components */ 6275 } else { 6276 /* Re-configure VSI vectors based on updated TC map */ 6277 i40e_vsi_map_rings_to_vectors(pf->vsi[v]); 6278 if (pf->vsi[v]->netdev) 6279 i40e_dcbnl_set_all(pf->vsi[v]); 6280 } 6281 } 6282 } 6283 6284 /** 6285 * i40e_resume_port_tx - Resume port Tx 6286 * @pf: PF struct 6287 * 6288 * Resume a port's Tx and issue a PF reset in case of failure to 6289 * resume. 6290 **/ 6291 static int i40e_resume_port_tx(struct i40e_pf *pf) 6292 { 6293 struct i40e_hw *hw = &pf->hw; 6294 int ret; 6295 6296 ret = i40e_aq_resume_port_tx(hw, NULL); 6297 if (ret) { 6298 dev_info(&pf->pdev->dev, 6299 "Resume Port Tx failed, err %s aq_err %s\n", 6300 i40e_stat_str(&pf->hw, ret), 6301 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status)); 6302 /* Schedule PF reset to recover */ 6303 set_bit(__I40E_PF_RESET_REQUESTED, pf->state); 6304 i40e_service_event_schedule(pf); 6305 } 6306 6307 return ret; 6308 } 6309 6310 /** 6311 * i40e_init_pf_dcb - Initialize DCB configuration 6312 * @pf: PF being configured 6313 * 6314 * Query the current DCB configuration and cache it 6315 * in the hardware structure 6316 **/ 6317 static int i40e_init_pf_dcb(struct i40e_pf *pf) 6318 { 6319 struct i40e_hw *hw = &pf->hw; 6320 int err = 0; 6321 6322 /* Do not enable DCB for SW1 and SW2 images even if the FW is capable 6323 * Also do not enable DCBx if FW LLDP agent is disabled 6324 */ 6325 if ((pf->hw_features & I40E_HW_NO_DCB_SUPPORT) || 6326 (pf->flags & I40E_FLAG_DISABLE_FW_LLDP)) 6327 goto out; 6328 6329 /* Get the initial DCB configuration */ 6330 err = i40e_init_dcb(hw); 6331 if (!err) { 6332 /* Device/Function is not DCBX capable */ 6333 if ((!hw->func_caps.dcb) || 6334 (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) { 6335 dev_info(&pf->pdev->dev, 6336 "DCBX offload is not supported or is disabled for this PF.\n"); 6337 } else { 6338 /* When status is not DISABLED then DCBX in FW */ 6339 pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED | 6340 DCB_CAP_DCBX_VER_IEEE; 6341 6342 pf->flags |= I40E_FLAG_DCB_CAPABLE; 6343 /* Enable DCB tagging only when more than one TC 6344 * or explicitly disable if only one TC 6345 */ 6346 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1) 6347 pf->flags |= I40E_FLAG_DCB_ENABLED; 6348 else 6349 pf->flags &= ~I40E_FLAG_DCB_ENABLED; 6350 dev_dbg(&pf->pdev->dev, 6351 "DCBX offload is supported for this PF.\n"); 6352 } 6353 } else if (pf->hw.aq.asq_last_status == I40E_AQ_RC_EPERM) { 6354 dev_info(&pf->pdev->dev, "FW LLDP disabled for this PF.\n"); 6355 pf->flags |= I40E_FLAG_DISABLE_FW_LLDP; 6356 } else { 6357 dev_info(&pf->pdev->dev, 6358 "Query for DCB configuration failed, err %s aq_err %s\n", 6359 i40e_stat_str(&pf->hw, err), 6360 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status)); 6361 } 6362 6363 out: 6364 return err; 6365 } 6366 #endif /* CONFIG_I40E_DCB */ 6367 #define SPEED_SIZE 14 6368 #define FC_SIZE 8 6369 /** 6370 * i40e_print_link_message - print link up or down 6371 * @vsi: the VSI for which link needs a message 6372 */ 6373 void i40e_print_link_message(struct i40e_vsi *vsi, bool isup) 6374 { 6375 enum i40e_aq_link_speed new_speed; 6376 struct i40e_pf *pf = vsi->back; 6377 char *speed = "Unknown"; 6378 char *fc = "Unknown"; 6379 char *fec = ""; 6380 char *req_fec = ""; 6381 char *an = ""; 6382 6383 new_speed = pf->hw.phy.link_info.link_speed; 6384 6385 if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed)) 6386 return; 6387 vsi->current_isup = isup; 6388 vsi->current_speed = new_speed; 6389 if (!isup) { 6390 netdev_info(vsi->netdev, "NIC Link is Down\n"); 6391 return; 6392 } 6393 6394 /* Warn user if link speed on NPAR enabled partition is not at 6395 * least 10GB 6396 */ 6397 if (pf->hw.func_caps.npar_enable && 6398 (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB || 6399 pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB)) 6400 netdev_warn(vsi->netdev, 6401 "The partition detected link speed that is less than 10Gbps\n"); 6402 6403 switch (pf->hw.phy.link_info.link_speed) { 6404 case I40E_LINK_SPEED_40GB: 6405 speed = "40 G"; 6406 break; 6407 case I40E_LINK_SPEED_20GB: 6408 speed = "20 G"; 6409 break; 6410 case I40E_LINK_SPEED_25GB: 6411 speed = "25 G"; 6412 break; 6413 case I40E_LINK_SPEED_10GB: 6414 speed = "10 G"; 6415 break; 6416 case I40E_LINK_SPEED_1GB: 6417 speed = "1000 M"; 6418 break; 6419 case I40E_LINK_SPEED_100MB: 6420 speed = "100 M"; 6421 break; 6422 default: 6423 break; 6424 } 6425 6426 switch (pf->hw.fc.current_mode) { 6427 case I40E_FC_FULL: 6428 fc = "RX/TX"; 6429 break; 6430 case I40E_FC_TX_PAUSE: 6431 fc = "TX"; 6432 break; 6433 case I40E_FC_RX_PAUSE: 6434 fc = "RX"; 6435 break; 6436 default: 6437 fc = "None"; 6438 break; 6439 } 6440 6441 if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) { 6442 req_fec = ", Requested FEC: None"; 6443 fec = ", FEC: None"; 6444 an = ", Autoneg: False"; 6445 6446 if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED) 6447 an = ", Autoneg: True"; 6448 6449 if (pf->hw.phy.link_info.fec_info & 6450 I40E_AQ_CONFIG_FEC_KR_ENA) 6451 fec = ", FEC: CL74 FC-FEC/BASE-R"; 6452 else if (pf->hw.phy.link_info.fec_info & 6453 I40E_AQ_CONFIG_FEC_RS_ENA) 6454 fec = ", FEC: CL108 RS-FEC"; 6455 6456 /* 'CL108 RS-FEC' should be displayed when RS is requested, or 6457 * both RS and FC are requested 6458 */ 6459 if (vsi->back->hw.phy.link_info.req_fec_info & 6460 (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) { 6461 if (vsi->back->hw.phy.link_info.req_fec_info & 6462 I40E_AQ_REQUEST_FEC_RS) 6463 req_fec = ", Requested FEC: CL108 RS-FEC"; 6464 else 6465 req_fec = ", Requested FEC: CL74 FC-FEC/BASE-R"; 6466 } 6467 } 6468 6469 netdev_info(vsi->netdev, "NIC Link is Up, %sbps Full Duplex%s%s%s, Flow Control: %s\n", 6470 speed, req_fec, fec, an, fc); 6471 } 6472 6473 /** 6474 * i40e_up_complete - Finish the last steps of bringing up a connection 6475 * @vsi: the VSI being configured 6476 **/ 6477 static int i40e_up_complete(struct i40e_vsi *vsi) 6478 { 6479 struct i40e_pf *pf = vsi->back; 6480 int err; 6481 6482 if (pf->flags & I40E_FLAG_MSIX_ENABLED) 6483 i40e_vsi_configure_msix(vsi); 6484 else 6485 i40e_configure_msi_and_legacy(vsi); 6486 6487 /* start rings */ 6488 err = i40e_vsi_start_rings(vsi); 6489 if (err) 6490 return err; 6491 6492 clear_bit(__I40E_VSI_DOWN, vsi->state); 6493 i40e_napi_enable_all(vsi); 6494 i40e_vsi_enable_irq(vsi); 6495 6496 if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) && 6497 (vsi->netdev)) { 6498 i40e_print_link_message(vsi, true); 6499 netif_tx_start_all_queues(vsi->netdev); 6500 netif_carrier_on(vsi->netdev); 6501 } 6502 6503 /* replay FDIR SB filters */ 6504 if (vsi->type == I40E_VSI_FDIR) { 6505 /* reset fd counters */ 6506 pf->fd_add_err = 0; 6507 pf->fd_atr_cnt = 0; 6508 i40e_fdir_filter_restore(vsi); 6509 } 6510 6511 /* On the next run of the service_task, notify any clients of the new 6512 * opened netdev 6513 */ 6514 pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED; 6515 i40e_service_event_schedule(pf); 6516 6517 return 0; 6518 } 6519 6520 /** 6521 * i40e_vsi_reinit_locked - Reset the VSI 6522 * @vsi: the VSI being configured 6523 * 6524 * Rebuild the ring structs after some configuration 6525 * has changed, e.g. MTU size. 6526 **/ 6527 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi) 6528 { 6529 struct i40e_pf *pf = vsi->back; 6530 6531 WARN_ON(in_interrupt()); 6532 while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) 6533 usleep_range(1000, 2000); 6534 i40e_down(vsi); 6535 6536 i40e_up(vsi); 6537 clear_bit(__I40E_CONFIG_BUSY, pf->state); 6538 } 6539 6540 /** 6541 * i40e_up - Bring the connection back up after being down 6542 * @vsi: the VSI being configured 6543 **/ 6544 int i40e_up(struct i40e_vsi *vsi) 6545 { 6546 int err; 6547 6548 err = i40e_vsi_configure(vsi); 6549 if (!err) 6550 err = i40e_up_complete(vsi); 6551 6552 return err; 6553 } 6554 6555 /** 6556 * i40e_down - Shutdown the connection processing 6557 * @vsi: the VSI being stopped 6558 **/ 6559 void i40e_down(struct i40e_vsi *vsi) 6560 { 6561 int i; 6562 6563 /* It is assumed that the caller of this function 6564 * sets the vsi->state __I40E_VSI_DOWN bit. 6565 */ 6566 if (vsi->netdev) { 6567 netif_carrier_off(vsi->netdev); 6568 netif_tx_disable(vsi->netdev); 6569 } 6570 i40e_vsi_disable_irq(vsi); 6571 i40e_vsi_stop_rings(vsi); 6572 i40e_napi_disable_all(vsi); 6573 6574 for (i = 0; i < vsi->num_queue_pairs; i++) { 6575 i40e_clean_tx_ring(vsi->tx_rings[i]); 6576 if (i40e_enabled_xdp_vsi(vsi)) 6577 i40e_clean_tx_ring(vsi->xdp_rings[i]); 6578 i40e_clean_rx_ring(vsi->rx_rings[i]); 6579 } 6580 6581 } 6582 6583 /** 6584 * i40e_validate_mqprio_qopt- validate queue mapping info 6585 * @vsi: the VSI being configured 6586 * @mqprio_qopt: queue parametrs 6587 **/ 6588 static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi, 6589 struct tc_mqprio_qopt_offload *mqprio_qopt) 6590 { 6591 u64 sum_max_rate = 0; 6592 u64 max_rate = 0; 6593 int i; 6594 6595 if (mqprio_qopt->qopt.offset[0] != 0 || 6596 mqprio_qopt->qopt.num_tc < 1 || 6597 mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS) 6598 return -EINVAL; 6599 for (i = 0; ; i++) { 6600 if (!mqprio_qopt->qopt.count[i]) 6601 return -EINVAL; 6602 if (mqprio_qopt->min_rate[i]) { 6603 dev_err(&vsi->back->pdev->dev, 6604 "Invalid min tx rate (greater than 0) specified\n"); 6605 return -EINVAL; 6606 } 6607 max_rate = mqprio_qopt->max_rate[i]; 6608 do_div(max_rate, I40E_BW_MBPS_DIVISOR); 6609 sum_max_rate += max_rate; 6610 6611 if (i >= mqprio_qopt->qopt.num_tc - 1) 6612 break; 6613 if (mqprio_qopt->qopt.offset[i + 1] != 6614 (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) 6615 return -EINVAL; 6616 } 6617 if (vsi->num_queue_pairs < 6618 (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) { 6619 return -EINVAL; 6620 } 6621 if (sum_max_rate > i40e_get_link_speed(vsi)) { 6622 dev_err(&vsi->back->pdev->dev, 6623 "Invalid max tx rate specified\n"); 6624 return -EINVAL; 6625 } 6626 return 0; 6627 } 6628 6629 /** 6630 * i40e_vsi_set_default_tc_config - set default values for tc configuration 6631 * @vsi: the VSI being configured 6632 **/ 6633 static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi) 6634 { 6635 u16 qcount; 6636 int i; 6637 6638 /* Only TC0 is enabled */ 6639 vsi->tc_config.numtc = 1; 6640 vsi->tc_config.enabled_tc = 1; 6641 qcount = min_t(int, vsi->alloc_queue_pairs, 6642 i40e_pf_get_max_q_per_tc(vsi->back)); 6643 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) { 6644 /* For the TC that is not enabled set the offset to to default 6645 * queue and allocate one queue for the given TC. 6646 */ 6647 vsi->tc_config.tc_info[i].qoffset = 0; 6648 if (i == 0) 6649 vsi->tc_config.tc_info[i].qcount = qcount; 6650 else 6651 vsi->tc_config.tc_info[i].qcount = 1; 6652 vsi->tc_config.tc_info[i].netdev_tc = 0; 6653 } 6654 } 6655 6656 /** 6657 * i40e_setup_tc - configure multiple traffic classes 6658 * @netdev: net device to configure 6659 * @type_data: tc offload data 6660 **/ 6661 static int i40e_setup_tc(struct net_device *netdev, void *type_data) 6662 { 6663 struct tc_mqprio_qopt_offload *mqprio_qopt = type_data; 6664 struct i40e_netdev_priv *np = netdev_priv(netdev); 6665 struct i40e_vsi *vsi = np->vsi; 6666 struct i40e_pf *pf = vsi->back; 6667 u8 enabled_tc = 0, num_tc, hw; 6668 bool need_reset = false; 6669 int ret = -EINVAL; 6670 u16 mode; 6671 int i; 6672 6673 num_tc = mqprio_qopt->qopt.num_tc; 6674 hw = mqprio_qopt->qopt.hw; 6675 mode = mqprio_qopt->mode; 6676 if (!hw) { 6677 pf->flags &= ~I40E_FLAG_TC_MQPRIO; 6678 memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt)); 6679 goto config_tc; 6680 } 6681 6682 /* Check if MFP enabled */ 6683 if (pf->flags & I40E_FLAG_MFP_ENABLED) { 6684 netdev_info(netdev, 6685 "Configuring TC not supported in MFP mode\n"); 6686 return ret; 6687 } 6688 switch (mode) { 6689 case TC_MQPRIO_MODE_DCB: 6690 pf->flags &= ~I40E_FLAG_TC_MQPRIO; 6691 6692 /* Check if DCB enabled to continue */ 6693 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) { 6694 netdev_info(netdev, 6695 "DCB is not enabled for adapter\n"); 6696 return ret; 6697 } 6698 6699 /* Check whether tc count is within enabled limit */ 6700 if (num_tc > i40e_pf_get_num_tc(pf)) { 6701 netdev_info(netdev, 6702 "TC count greater than enabled on link for adapter\n"); 6703 return ret; 6704 } 6705 break; 6706 case TC_MQPRIO_MODE_CHANNEL: 6707 if (pf->flags & I40E_FLAG_DCB_ENABLED) { 6708 netdev_info(netdev, 6709 "Full offload of TC Mqprio options is not supported when DCB is enabled\n"); 6710 return ret; 6711 } 6712 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED)) 6713 return ret; 6714 ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt); 6715 if (ret) 6716 return ret; 6717 memcpy(&vsi->mqprio_qopt, mqprio_qopt, 6718 sizeof(*mqprio_qopt)); 6719 pf->flags |= I40E_FLAG_TC_MQPRIO; 6720 pf->flags &= ~I40E_FLAG_DCB_ENABLED; 6721 break; 6722 default: 6723 return -EINVAL; 6724 } 6725 6726 config_tc: 6727 /* Generate TC map for number of tc requested */ 6728 for (i = 0; i < num_tc; i++) 6729 enabled_tc |= BIT(i); 6730 6731 /* Requesting same TC configuration as already enabled */ 6732 if (enabled_tc == vsi->tc_config.enabled_tc && 6733 mode != TC_MQPRIO_MODE_CHANNEL) 6734 return 0; 6735 6736 /* Quiesce VSI queues */ 6737 i40e_quiesce_vsi(vsi); 6738 6739 if (!hw && !(pf->flags & I40E_FLAG_TC_MQPRIO)) 6740 i40e_remove_queue_channels(vsi); 6741 6742 /* Configure VSI for enabled TCs */ 6743 ret = i40e_vsi_config_tc(vsi, enabled_tc); 6744 if (ret) { 6745 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n", 6746 vsi->seid); 6747 need_reset = true; 6748 goto exit; 6749 } 6750 6751 if (pf->flags & I40E_FLAG_TC_MQPRIO) { 6752 if (vsi->mqprio_qopt.max_rate[0]) { 6753 u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0]; 6754 6755 do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR); 6756 ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate); 6757 if (!ret) { 6758 u64 credits = max_tx_rate; 6759 6760 do_div(credits, I40E_BW_CREDIT_DIVISOR); 6761 dev_dbg(&vsi->back->pdev->dev, 6762 "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n", 6763 max_tx_rate, 6764 credits, 6765 vsi->seid); 6766 } else { 6767 need_reset = true; 6768 goto exit; 6769 } 6770 } 6771 ret = i40e_configure_queue_channels(vsi); 6772 if (ret) { 6773 netdev_info(netdev, 6774 "Failed configuring queue channels\n"); 6775 need_reset = true; 6776 goto exit; 6777 } 6778 } 6779 6780 exit: 6781 /* Reset the configuration data to defaults, only TC0 is enabled */ 6782 if (need_reset) { 6783 i40e_vsi_set_default_tc_config(vsi); 6784 need_reset = false; 6785 } 6786 6787 /* Unquiesce VSI */ 6788 i40e_unquiesce_vsi(vsi); 6789 return ret; 6790 } 6791 6792 /** 6793 * i40e_set_cld_element - sets cloud filter element data 6794 * @filter: cloud filter rule 6795 * @cld: ptr to cloud filter element data 6796 * 6797 * This is helper function to copy data into cloud filter element 6798 **/ 6799 static inline void 6800 i40e_set_cld_element(struct i40e_cloud_filter *filter, 6801 struct i40e_aqc_cloud_filters_element_data *cld) 6802 { 6803 int i, j; 6804 u32 ipa; 6805 6806 memset(cld, 0, sizeof(*cld)); 6807 ether_addr_copy(cld->outer_mac, filter->dst_mac); 6808 ether_addr_copy(cld->inner_mac, filter->src_mac); 6809 6810 if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6) 6811 return; 6812 6813 if (filter->n_proto == ETH_P_IPV6) { 6814 #define IPV6_MAX_INDEX (ARRAY_SIZE(filter->dst_ipv6) - 1) 6815 for (i = 0, j = 0; i < ARRAY_SIZE(filter->dst_ipv6); 6816 i++, j += 2) { 6817 ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]); 6818 ipa = cpu_to_le32(ipa); 6819 memcpy(&cld->ipaddr.raw_v6.data[j], &ipa, sizeof(ipa)); 6820 } 6821 } else { 6822 ipa = be32_to_cpu(filter->dst_ipv4); 6823 memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa)); 6824 } 6825 6826 cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id)); 6827 6828 /* tenant_id is not supported by FW now, once the support is enabled 6829 * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id) 6830 */ 6831 if (filter->tenant_id) 6832 return; 6833 } 6834 6835 /** 6836 * i40e_add_del_cloud_filter - Add/del cloud filter 6837 * @vsi: pointer to VSI 6838 * @filter: cloud filter rule 6839 * @add: if true, add, if false, delete 6840 * 6841 * Add or delete a cloud filter for a specific flow spec. 6842 * Returns 0 if the filter were successfully added. 6843 **/ 6844 static int i40e_add_del_cloud_filter(struct i40e_vsi *vsi, 6845 struct i40e_cloud_filter *filter, bool add) 6846 { 6847 struct i40e_aqc_cloud_filters_element_data cld_filter; 6848 struct i40e_pf *pf = vsi->back; 6849 int ret; 6850 static const u16 flag_table[128] = { 6851 [I40E_CLOUD_FILTER_FLAGS_OMAC] = 6852 I40E_AQC_ADD_CLOUD_FILTER_OMAC, 6853 [I40E_CLOUD_FILTER_FLAGS_IMAC] = 6854 I40E_AQC_ADD_CLOUD_FILTER_IMAC, 6855 [I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN] = 6856 I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN, 6857 [I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] = 6858 I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID, 6859 [I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] = 6860 I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC, 6861 [I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] = 6862 I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID, 6863 [I40E_CLOUD_FILTER_FLAGS_IIP] = 6864 I40E_AQC_ADD_CLOUD_FILTER_IIP, 6865 }; 6866 6867 if (filter->flags >= ARRAY_SIZE(flag_table)) 6868 return I40E_ERR_CONFIG; 6869 6870 /* copy element needed to add cloud filter from filter */ 6871 i40e_set_cld_element(filter, &cld_filter); 6872 6873 if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE) 6874 cld_filter.flags = cpu_to_le16(filter->tunnel_type << 6875 I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT); 6876 6877 if (filter->n_proto == ETH_P_IPV6) 6878 cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] | 6879 I40E_AQC_ADD_CLOUD_FLAGS_IPV6); 6880 else 6881 cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] | 6882 I40E_AQC_ADD_CLOUD_FLAGS_IPV4); 6883 6884 if (add) 6885 ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid, 6886 &cld_filter, 1); 6887 else 6888 ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid, 6889 &cld_filter, 1); 6890 if (ret) 6891 dev_dbg(&pf->pdev->dev, 6892 "Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n", 6893 add ? "add" : "delete", filter->dst_port, ret, 6894 pf->hw.aq.asq_last_status); 6895 else 6896 dev_info(&pf->pdev->dev, 6897 "%s cloud filter for VSI: %d\n", 6898 add ? "Added" : "Deleted", filter->seid); 6899 return ret; 6900 } 6901 6902 /** 6903 * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf 6904 * @vsi: pointer to VSI 6905 * @filter: cloud filter rule 6906 * @add: if true, add, if false, delete 6907 * 6908 * Add or delete a cloud filter for a specific flow spec using big buffer. 6909 * Returns 0 if the filter were successfully added. 6910 **/ 6911 static int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi, 6912 struct i40e_cloud_filter *filter, 6913 bool add) 6914 { 6915 struct i40e_aqc_cloud_filters_element_bb cld_filter; 6916 struct i40e_pf *pf = vsi->back; 6917 int ret; 6918 6919 /* Both (src/dst) valid mac_addr are not supported */ 6920 if ((is_valid_ether_addr(filter->dst_mac) && 6921 is_valid_ether_addr(filter->src_mac)) || 6922 (is_multicast_ether_addr(filter->dst_mac) && 6923 is_multicast_ether_addr(filter->src_mac))) 6924 return -EOPNOTSUPP; 6925 6926 /* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP 6927 * ports are not supported via big buffer now. 6928 */ 6929 if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP) 6930 return -EOPNOTSUPP; 6931 6932 /* adding filter using src_port/src_ip is not supported at this stage */ 6933 if (filter->src_port || filter->src_ipv4 || 6934 !ipv6_addr_any(&filter->ip.v6.src_ip6)) 6935 return -EOPNOTSUPP; 6936 6937 /* copy element needed to add cloud filter from filter */ 6938 i40e_set_cld_element(filter, &cld_filter.element); 6939 6940 if (is_valid_ether_addr(filter->dst_mac) || 6941 is_valid_ether_addr(filter->src_mac) || 6942 is_multicast_ether_addr(filter->dst_mac) || 6943 is_multicast_ether_addr(filter->src_mac)) { 6944 /* MAC + IP : unsupported mode */ 6945 if (filter->dst_ipv4) 6946 return -EOPNOTSUPP; 6947 6948 /* since we validated that L4 port must be valid before 6949 * we get here, start with respective "flags" value 6950 * and update if vlan is present or not 6951 */ 6952 cld_filter.element.flags = 6953 cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT); 6954 6955 if (filter->vlan_id) { 6956 cld_filter.element.flags = 6957 cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT); 6958 } 6959 6960 } else if (filter->dst_ipv4 || 6961 !ipv6_addr_any(&filter->ip.v6.dst_ip6)) { 6962 cld_filter.element.flags = 6963 cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT); 6964 if (filter->n_proto == ETH_P_IPV6) 6965 cld_filter.element.flags |= 6966 cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6); 6967 else 6968 cld_filter.element.flags |= 6969 cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4); 6970 } else { 6971 dev_err(&pf->pdev->dev, 6972 "either mac or ip has to be valid for cloud filter\n"); 6973 return -EINVAL; 6974 } 6975 6976 /* Now copy L4 port in Byte 6..7 in general fields */ 6977 cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] = 6978 be16_to_cpu(filter->dst_port); 6979 6980 if (add) { 6981 /* Validate current device switch mode, change if necessary */ 6982 ret = i40e_validate_and_set_switch_mode(vsi); 6983 if (ret) { 6984 dev_err(&pf->pdev->dev, 6985 "failed to set switch mode, ret %d\n", 6986 ret); 6987 return ret; 6988 } 6989 6990 ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid, 6991 &cld_filter, 1); 6992 } else { 6993 ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid, 6994 &cld_filter, 1); 6995 } 6996 6997 if (ret) 6998 dev_dbg(&pf->pdev->dev, 6999 "Failed to %s cloud filter(big buffer) err %d aq_err %d\n", 7000 add ? "add" : "delete", ret, pf->hw.aq.asq_last_status); 7001 else 7002 dev_info(&pf->pdev->dev, 7003 "%s cloud filter for VSI: %d, L4 port: %d\n", 7004 add ? "add" : "delete", filter->seid, 7005 ntohs(filter->dst_port)); 7006 return ret; 7007 } 7008 7009 /** 7010 * i40e_parse_cls_flower - Parse tc flower filters provided by kernel 7011 * @vsi: Pointer to VSI 7012 * @cls_flower: Pointer to struct tc_cls_flower_offload 7013 * @filter: Pointer to cloud filter structure 7014 * 7015 **/ 7016 static int i40e_parse_cls_flower(struct i40e_vsi *vsi, 7017 struct tc_cls_flower_offload *f, 7018 struct i40e_cloud_filter *filter) 7019 { 7020 u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0; 7021 struct i40e_pf *pf = vsi->back; 7022 u8 field_flags = 0; 7023 7024 if (f->dissector->used_keys & 7025 ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) | 7026 BIT(FLOW_DISSECTOR_KEY_BASIC) | 7027 BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) | 7028 BIT(FLOW_DISSECTOR_KEY_VLAN) | 7029 BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) | 7030 BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) | 7031 BIT(FLOW_DISSECTOR_KEY_PORTS) | 7032 BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) { 7033 dev_err(&pf->pdev->dev, "Unsupported key used: 0x%x\n", 7034 f->dissector->used_keys); 7035 return -EOPNOTSUPP; 7036 } 7037 7038 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ENC_KEYID)) { 7039 struct flow_dissector_key_keyid *key = 7040 skb_flow_dissector_target(f->dissector, 7041 FLOW_DISSECTOR_KEY_ENC_KEYID, 7042 f->key); 7043 7044 struct flow_dissector_key_keyid *mask = 7045 skb_flow_dissector_target(f->dissector, 7046 FLOW_DISSECTOR_KEY_ENC_KEYID, 7047 f->mask); 7048 7049 if (mask->keyid != 0) 7050 field_flags |= I40E_CLOUD_FIELD_TEN_ID; 7051 7052 filter->tenant_id = be32_to_cpu(key->keyid); 7053 } 7054 7055 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_BASIC)) { 7056 struct flow_dissector_key_basic *key = 7057 skb_flow_dissector_target(f->dissector, 7058 FLOW_DISSECTOR_KEY_BASIC, 7059 f->key); 7060 7061 struct flow_dissector_key_basic *mask = 7062 skb_flow_dissector_target(f->dissector, 7063 FLOW_DISSECTOR_KEY_BASIC, 7064 f->mask); 7065 7066 n_proto_key = ntohs(key->n_proto); 7067 n_proto_mask = ntohs(mask->n_proto); 7068 7069 if (n_proto_key == ETH_P_ALL) { 7070 n_proto_key = 0; 7071 n_proto_mask = 0; 7072 } 7073 filter->n_proto = n_proto_key & n_proto_mask; 7074 filter->ip_proto = key->ip_proto; 7075 } 7076 7077 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ETH_ADDRS)) { 7078 struct flow_dissector_key_eth_addrs *key = 7079 skb_flow_dissector_target(f->dissector, 7080 FLOW_DISSECTOR_KEY_ETH_ADDRS, 7081 f->key); 7082 7083 struct flow_dissector_key_eth_addrs *mask = 7084 skb_flow_dissector_target(f->dissector, 7085 FLOW_DISSECTOR_KEY_ETH_ADDRS, 7086 f->mask); 7087 7088 /* use is_broadcast and is_zero to check for all 0xf or 0 */ 7089 if (!is_zero_ether_addr(mask->dst)) { 7090 if (is_broadcast_ether_addr(mask->dst)) { 7091 field_flags |= I40E_CLOUD_FIELD_OMAC; 7092 } else { 7093 dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n", 7094 mask->dst); 7095 return I40E_ERR_CONFIG; 7096 } 7097 } 7098 7099 if (!is_zero_ether_addr(mask->src)) { 7100 if (is_broadcast_ether_addr(mask->src)) { 7101 field_flags |= I40E_CLOUD_FIELD_IMAC; 7102 } else { 7103 dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n", 7104 mask->src); 7105 return I40E_ERR_CONFIG; 7106 } 7107 } 7108 ether_addr_copy(filter->dst_mac, key->dst); 7109 ether_addr_copy(filter->src_mac, key->src); 7110 } 7111 7112 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_VLAN)) { 7113 struct flow_dissector_key_vlan *key = 7114 skb_flow_dissector_target(f->dissector, 7115 FLOW_DISSECTOR_KEY_VLAN, 7116 f->key); 7117 struct flow_dissector_key_vlan *mask = 7118 skb_flow_dissector_target(f->dissector, 7119 FLOW_DISSECTOR_KEY_VLAN, 7120 f->mask); 7121 7122 if (mask->vlan_id) { 7123 if (mask->vlan_id == VLAN_VID_MASK) { 7124 field_flags |= I40E_CLOUD_FIELD_IVLAN; 7125 7126 } else { 7127 dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n", 7128 mask->vlan_id); 7129 return I40E_ERR_CONFIG; 7130 } 7131 } 7132 7133 filter->vlan_id = cpu_to_be16(key->vlan_id); 7134 } 7135 7136 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_CONTROL)) { 7137 struct flow_dissector_key_control *key = 7138 skb_flow_dissector_target(f->dissector, 7139 FLOW_DISSECTOR_KEY_CONTROL, 7140 f->key); 7141 7142 addr_type = key->addr_type; 7143 } 7144 7145 if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) { 7146 struct flow_dissector_key_ipv4_addrs *key = 7147 skb_flow_dissector_target(f->dissector, 7148 FLOW_DISSECTOR_KEY_IPV4_ADDRS, 7149 f->key); 7150 struct flow_dissector_key_ipv4_addrs *mask = 7151 skb_flow_dissector_target(f->dissector, 7152 FLOW_DISSECTOR_KEY_IPV4_ADDRS, 7153 f->mask); 7154 7155 if (mask->dst) { 7156 if (mask->dst == cpu_to_be32(0xffffffff)) { 7157 field_flags |= I40E_CLOUD_FIELD_IIP; 7158 } else { 7159 mask->dst = be32_to_cpu(mask->dst); 7160 dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4\n", 7161 &mask->dst); 7162 return I40E_ERR_CONFIG; 7163 } 7164 } 7165 7166 if (mask->src) { 7167 if (mask->src == cpu_to_be32(0xffffffff)) { 7168 field_flags |= I40E_CLOUD_FIELD_IIP; 7169 } else { 7170 mask->src = be32_to_cpu(mask->src); 7171 dev_err(&pf->pdev->dev, "Bad ip src mask %pI4\n", 7172 &mask->src); 7173 return I40E_ERR_CONFIG; 7174 } 7175 } 7176 7177 if (field_flags & I40E_CLOUD_FIELD_TEN_ID) { 7178 dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n"); 7179 return I40E_ERR_CONFIG; 7180 } 7181 filter->dst_ipv4 = key->dst; 7182 filter->src_ipv4 = key->src; 7183 } 7184 7185 if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) { 7186 struct flow_dissector_key_ipv6_addrs *key = 7187 skb_flow_dissector_target(f->dissector, 7188 FLOW_DISSECTOR_KEY_IPV6_ADDRS, 7189 f->key); 7190 struct flow_dissector_key_ipv6_addrs *mask = 7191 skb_flow_dissector_target(f->dissector, 7192 FLOW_DISSECTOR_KEY_IPV6_ADDRS, 7193 f->mask); 7194 7195 /* src and dest IPV6 address should not be LOOPBACK 7196 * (0:0:0:0:0:0:0:1), which can be represented as ::1 7197 */ 7198 if (ipv6_addr_loopback(&key->dst) || 7199 ipv6_addr_loopback(&key->src)) { 7200 dev_err(&pf->pdev->dev, 7201 "Bad ipv6, addr is LOOPBACK\n"); 7202 return I40E_ERR_CONFIG; 7203 } 7204 if (!ipv6_addr_any(&mask->dst) || !ipv6_addr_any(&mask->src)) 7205 field_flags |= I40E_CLOUD_FIELD_IIP; 7206 7207 memcpy(&filter->src_ipv6, &key->src.s6_addr32, 7208 sizeof(filter->src_ipv6)); 7209 memcpy(&filter->dst_ipv6, &key->dst.s6_addr32, 7210 sizeof(filter->dst_ipv6)); 7211 } 7212 7213 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_PORTS)) { 7214 struct flow_dissector_key_ports *key = 7215 skb_flow_dissector_target(f->dissector, 7216 FLOW_DISSECTOR_KEY_PORTS, 7217 f->key); 7218 struct flow_dissector_key_ports *mask = 7219 skb_flow_dissector_target(f->dissector, 7220 FLOW_DISSECTOR_KEY_PORTS, 7221 f->mask); 7222 7223 if (mask->src) { 7224 if (mask->src == cpu_to_be16(0xffff)) { 7225 field_flags |= I40E_CLOUD_FIELD_IIP; 7226 } else { 7227 dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n", 7228 be16_to_cpu(mask->src)); 7229 return I40E_ERR_CONFIG; 7230 } 7231 } 7232 7233 if (mask->dst) { 7234 if (mask->dst == cpu_to_be16(0xffff)) { 7235 field_flags |= I40E_CLOUD_FIELD_IIP; 7236 } else { 7237 dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n", 7238 be16_to_cpu(mask->dst)); 7239 return I40E_ERR_CONFIG; 7240 } 7241 } 7242 7243 filter->dst_port = key->dst; 7244 filter->src_port = key->src; 7245 7246 switch (filter->ip_proto) { 7247 case IPPROTO_TCP: 7248 case IPPROTO_UDP: 7249 break; 7250 default: 7251 dev_err(&pf->pdev->dev, 7252 "Only UDP and TCP transport are supported\n"); 7253 return -EINVAL; 7254 } 7255 } 7256 filter->flags = field_flags; 7257 return 0; 7258 } 7259 7260 /** 7261 * i40e_handle_tclass: Forward to a traffic class on the device 7262 * @vsi: Pointer to VSI 7263 * @tc: traffic class index on the device 7264 * @filter: Pointer to cloud filter structure 7265 * 7266 **/ 7267 static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc, 7268 struct i40e_cloud_filter *filter) 7269 { 7270 struct i40e_channel *ch, *ch_tmp; 7271 7272 /* direct to a traffic class on the same device */ 7273 if (tc == 0) { 7274 filter->seid = vsi->seid; 7275 return 0; 7276 } else if (vsi->tc_config.enabled_tc & BIT(tc)) { 7277 if (!filter->dst_port) { 7278 dev_err(&vsi->back->pdev->dev, 7279 "Specify destination port to direct to traffic class that is not default\n"); 7280 return -EINVAL; 7281 } 7282 if (list_empty(&vsi->ch_list)) 7283 return -EINVAL; 7284 list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, 7285 list) { 7286 if (ch->seid == vsi->tc_seid_map[tc]) 7287 filter->seid = ch->seid; 7288 } 7289 return 0; 7290 } 7291 dev_err(&vsi->back->pdev->dev, "TC is not enabled\n"); 7292 return -EINVAL; 7293 } 7294 7295 /** 7296 * i40e_configure_clsflower - Configure tc flower filters 7297 * @vsi: Pointer to VSI 7298 * @cls_flower: Pointer to struct tc_cls_flower_offload 7299 * 7300 **/ 7301 static int i40e_configure_clsflower(struct i40e_vsi *vsi, 7302 struct tc_cls_flower_offload *cls_flower) 7303 { 7304 int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid); 7305 struct i40e_cloud_filter *filter = NULL; 7306 struct i40e_pf *pf = vsi->back; 7307 int err = 0; 7308 7309 if (tc < 0) { 7310 dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n"); 7311 return -EOPNOTSUPP; 7312 } 7313 7314 if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) || 7315 test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) 7316 return -EBUSY; 7317 7318 if (pf->fdir_pf_active_filters || 7319 (!hlist_empty(&pf->fdir_filter_list))) { 7320 dev_err(&vsi->back->pdev->dev, 7321 "Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n"); 7322 return -EINVAL; 7323 } 7324 7325 if (vsi->back->flags & I40E_FLAG_FD_SB_ENABLED) { 7326 dev_err(&vsi->back->pdev->dev, 7327 "Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n"); 7328 vsi->back->flags &= ~I40E_FLAG_FD_SB_ENABLED; 7329 vsi->back->flags |= I40E_FLAG_FD_SB_TO_CLOUD_FILTER; 7330 } 7331 7332 filter = kzalloc(sizeof(*filter), GFP_KERNEL); 7333 if (!filter) 7334 return -ENOMEM; 7335 7336 filter->cookie = cls_flower->cookie; 7337 7338 err = i40e_parse_cls_flower(vsi, cls_flower, filter); 7339 if (err < 0) 7340 goto err; 7341 7342 err = i40e_handle_tclass(vsi, tc, filter); 7343 if (err < 0) 7344 goto err; 7345 7346 /* Add cloud filter */ 7347 if (filter->dst_port) 7348 err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true); 7349 else 7350 err = i40e_add_del_cloud_filter(vsi, filter, true); 7351 7352 if (err) { 7353 dev_err(&pf->pdev->dev, 7354 "Failed to add cloud filter, err %s\n", 7355 i40e_stat_str(&pf->hw, err)); 7356 goto err; 7357 } 7358 7359 /* add filter to the ordered list */ 7360 INIT_HLIST_NODE(&filter->cloud_node); 7361 7362 hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list); 7363 7364 pf->num_cloud_filters++; 7365 7366 return err; 7367 err: 7368 kfree(filter); 7369 return err; 7370 } 7371 7372 /** 7373 * i40e_find_cloud_filter - Find the could filter in the list 7374 * @vsi: Pointer to VSI 7375 * @cookie: filter specific cookie 7376 * 7377 **/ 7378 static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi, 7379 unsigned long *cookie) 7380 { 7381 struct i40e_cloud_filter *filter = NULL; 7382 struct hlist_node *node2; 7383 7384 hlist_for_each_entry_safe(filter, node2, 7385 &vsi->back->cloud_filter_list, cloud_node) 7386 if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie))) 7387 return filter; 7388 return NULL; 7389 } 7390 7391 /** 7392 * i40e_delete_clsflower - Remove tc flower filters 7393 * @vsi: Pointer to VSI 7394 * @cls_flower: Pointer to struct tc_cls_flower_offload 7395 * 7396 **/ 7397 static int i40e_delete_clsflower(struct i40e_vsi *vsi, 7398 struct tc_cls_flower_offload *cls_flower) 7399 { 7400 struct i40e_cloud_filter *filter = NULL; 7401 struct i40e_pf *pf = vsi->back; 7402 int err = 0; 7403 7404 filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie); 7405 7406 if (!filter) 7407 return -EINVAL; 7408 7409 hash_del(&filter->cloud_node); 7410 7411 if (filter->dst_port) 7412 err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false); 7413 else 7414 err = i40e_add_del_cloud_filter(vsi, filter, false); 7415 7416 kfree(filter); 7417 if (err) { 7418 dev_err(&pf->pdev->dev, 7419 "Failed to delete cloud filter, err %s\n", 7420 i40e_stat_str(&pf->hw, err)); 7421 return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status); 7422 } 7423 7424 pf->num_cloud_filters--; 7425 if (!pf->num_cloud_filters) 7426 if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) && 7427 !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) { 7428 pf->flags |= I40E_FLAG_FD_SB_ENABLED; 7429 pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER; 7430 pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE; 7431 } 7432 return 0; 7433 } 7434 7435 /** 7436 * i40e_setup_tc_cls_flower - flower classifier offloads 7437 * @netdev: net device to configure 7438 * @type_data: offload data 7439 **/ 7440 static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np, 7441 struct tc_cls_flower_offload *cls_flower) 7442 { 7443 struct i40e_vsi *vsi = np->vsi; 7444 7445 switch (cls_flower->command) { 7446 case TC_CLSFLOWER_REPLACE: 7447 return i40e_configure_clsflower(vsi, cls_flower); 7448 case TC_CLSFLOWER_DESTROY: 7449 return i40e_delete_clsflower(vsi, cls_flower); 7450 case TC_CLSFLOWER_STATS: 7451 return -EOPNOTSUPP; 7452 default: 7453 return -EINVAL; 7454 } 7455 } 7456 7457 static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data, 7458 void *cb_priv) 7459 { 7460 struct i40e_netdev_priv *np = cb_priv; 7461 7462 if (!tc_cls_can_offload_and_chain0(np->vsi->netdev, type_data)) 7463 return -EOPNOTSUPP; 7464 7465 switch (type) { 7466 case TC_SETUP_CLSFLOWER: 7467 return i40e_setup_tc_cls_flower(np, type_data); 7468 7469 default: 7470 return -EOPNOTSUPP; 7471 } 7472 } 7473 7474 static int i40e_setup_tc_block(struct net_device *dev, 7475 struct tc_block_offload *f) 7476 { 7477 struct i40e_netdev_priv *np = netdev_priv(dev); 7478 7479 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS) 7480 return -EOPNOTSUPP; 7481 7482 switch (f->command) { 7483 case TC_BLOCK_BIND: 7484 return tcf_block_cb_register(f->block, i40e_setup_tc_block_cb, 7485 np, np); 7486 case TC_BLOCK_UNBIND: 7487 tcf_block_cb_unregister(f->block, i40e_setup_tc_block_cb, np); 7488 return 0; 7489 default: 7490 return -EOPNOTSUPP; 7491 } 7492 } 7493 7494 static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type, 7495 void *type_data) 7496 { 7497 switch (type) { 7498 case TC_SETUP_QDISC_MQPRIO: 7499 return i40e_setup_tc(netdev, type_data); 7500 case TC_SETUP_BLOCK: 7501 return i40e_setup_tc_block(netdev, type_data); 7502 default: 7503 return -EOPNOTSUPP; 7504 } 7505 } 7506 7507 /** 7508 * i40e_open - Called when a network interface is made active 7509 * @netdev: network interface device structure 7510 * 7511 * The open entry point is called when a network interface is made 7512 * active by the system (IFF_UP). At this point all resources needed 7513 * for transmit and receive operations are allocated, the interrupt 7514 * handler is registered with the OS, the netdev watchdog subtask is 7515 * enabled, and the stack is notified that the interface is ready. 7516 * 7517 * Returns 0 on success, negative value on failure 7518 **/ 7519 int i40e_open(struct net_device *netdev) 7520 { 7521 struct i40e_netdev_priv *np = netdev_priv(netdev); 7522 struct i40e_vsi *vsi = np->vsi; 7523 struct i40e_pf *pf = vsi->back; 7524 int err; 7525 7526 /* disallow open during test or if eeprom is broken */ 7527 if (test_bit(__I40E_TESTING, pf->state) || 7528 test_bit(__I40E_BAD_EEPROM, pf->state)) 7529 return -EBUSY; 7530 7531 netif_carrier_off(netdev); 7532 7533 err = i40e_vsi_open(vsi); 7534 if (err) 7535 return err; 7536 7537 /* configure global TSO hardware offload settings */ 7538 wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH | 7539 TCP_FLAG_FIN) >> 16); 7540 wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH | 7541 TCP_FLAG_FIN | 7542 TCP_FLAG_CWR) >> 16); 7543 wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16); 7544 7545 udp_tunnel_get_rx_info(netdev); 7546 7547 return 0; 7548 } 7549 7550 /** 7551 * i40e_vsi_open - 7552 * @vsi: the VSI to open 7553 * 7554 * Finish initialization of the VSI. 7555 * 7556 * Returns 0 on success, negative value on failure 7557 * 7558 * Note: expects to be called while under rtnl_lock() 7559 **/ 7560 int i40e_vsi_open(struct i40e_vsi *vsi) 7561 { 7562 struct i40e_pf *pf = vsi->back; 7563 char int_name[I40E_INT_NAME_STR_LEN]; 7564 int err; 7565 7566 /* allocate descriptors */ 7567 err = i40e_vsi_setup_tx_resources(vsi); 7568 if (err) 7569 goto err_setup_tx; 7570 err = i40e_vsi_setup_rx_resources(vsi); 7571 if (err) 7572 goto err_setup_rx; 7573 7574 err = i40e_vsi_configure(vsi); 7575 if (err) 7576 goto err_setup_rx; 7577 7578 if (vsi->netdev) { 7579 snprintf(int_name, sizeof(int_name) - 1, "%s-%s", 7580 dev_driver_string(&pf->pdev->dev), vsi->netdev->name); 7581 err = i40e_vsi_request_irq(vsi, int_name); 7582 if (err) 7583 goto err_setup_rx; 7584 7585 /* Notify the stack of the actual queue counts. */ 7586 err = netif_set_real_num_tx_queues(vsi->netdev, 7587 vsi->num_queue_pairs); 7588 if (err) 7589 goto err_set_queues; 7590 7591 err = netif_set_real_num_rx_queues(vsi->netdev, 7592 vsi->num_queue_pairs); 7593 if (err) 7594 goto err_set_queues; 7595 7596 } else if (vsi->type == I40E_VSI_FDIR) { 7597 snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir", 7598 dev_driver_string(&pf->pdev->dev), 7599 dev_name(&pf->pdev->dev)); 7600 err = i40e_vsi_request_irq(vsi, int_name); 7601 7602 } else { 7603 err = -EINVAL; 7604 goto err_setup_rx; 7605 } 7606 7607 err = i40e_up_complete(vsi); 7608 if (err) 7609 goto err_up_complete; 7610 7611 return 0; 7612 7613 err_up_complete: 7614 i40e_down(vsi); 7615 err_set_queues: 7616 i40e_vsi_free_irq(vsi); 7617 err_setup_rx: 7618 i40e_vsi_free_rx_resources(vsi); 7619 err_setup_tx: 7620 i40e_vsi_free_tx_resources(vsi); 7621 if (vsi == pf->vsi[pf->lan_vsi]) 7622 i40e_do_reset(pf, I40E_PF_RESET_FLAG, true); 7623 7624 return err; 7625 } 7626 7627 /** 7628 * i40e_fdir_filter_exit - Cleans up the Flow Director accounting 7629 * @pf: Pointer to PF 7630 * 7631 * This function destroys the hlist where all the Flow Director 7632 * filters were saved. 7633 **/ 7634 static void i40e_fdir_filter_exit(struct i40e_pf *pf) 7635 { 7636 struct i40e_fdir_filter *filter; 7637 struct i40e_flex_pit *pit_entry, *tmp; 7638 struct hlist_node *node2; 7639 7640 hlist_for_each_entry_safe(filter, node2, 7641 &pf->fdir_filter_list, fdir_node) { 7642 hlist_del(&filter->fdir_node); 7643 kfree(filter); 7644 } 7645 7646 list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) { 7647 list_del(&pit_entry->list); 7648 kfree(pit_entry); 7649 } 7650 INIT_LIST_HEAD(&pf->l3_flex_pit_list); 7651 7652 list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) { 7653 list_del(&pit_entry->list); 7654 kfree(pit_entry); 7655 } 7656 INIT_LIST_HEAD(&pf->l4_flex_pit_list); 7657 7658 pf->fdir_pf_active_filters = 0; 7659 pf->fd_tcp4_filter_cnt = 0; 7660 pf->fd_udp4_filter_cnt = 0; 7661 pf->fd_sctp4_filter_cnt = 0; 7662 pf->fd_ip4_filter_cnt = 0; 7663 7664 /* Reprogram the default input set for TCP/IPv4 */ 7665 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP, 7666 I40E_L3_SRC_MASK | I40E_L3_DST_MASK | 7667 I40E_L4_SRC_MASK | I40E_L4_DST_MASK); 7668 7669 /* Reprogram the default input set for UDP/IPv4 */ 7670 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP, 7671 I40E_L3_SRC_MASK | I40E_L3_DST_MASK | 7672 I40E_L4_SRC_MASK | I40E_L4_DST_MASK); 7673 7674 /* Reprogram the default input set for SCTP/IPv4 */ 7675 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP, 7676 I40E_L3_SRC_MASK | I40E_L3_DST_MASK | 7677 I40E_L4_SRC_MASK | I40E_L4_DST_MASK); 7678 7679 /* Reprogram the default input set for Other/IPv4 */ 7680 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER, 7681 I40E_L3_SRC_MASK | I40E_L3_DST_MASK); 7682 7683 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4, 7684 I40E_L3_SRC_MASK | I40E_L3_DST_MASK); 7685 } 7686 7687 /** 7688 * i40e_cloud_filter_exit - Cleans up the cloud filters 7689 * @pf: Pointer to PF 7690 * 7691 * This function destroys the hlist where all the cloud filters 7692 * were saved. 7693 **/ 7694 static void i40e_cloud_filter_exit(struct i40e_pf *pf) 7695 { 7696 struct i40e_cloud_filter *cfilter; 7697 struct hlist_node *node; 7698 7699 hlist_for_each_entry_safe(cfilter, node, 7700 &pf->cloud_filter_list, cloud_node) { 7701 hlist_del(&cfilter->cloud_node); 7702 kfree(cfilter); 7703 } 7704 pf->num_cloud_filters = 0; 7705 7706 if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) && 7707 !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) { 7708 pf->flags |= I40E_FLAG_FD_SB_ENABLED; 7709 pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER; 7710 pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE; 7711 } 7712 } 7713 7714 /** 7715 * i40e_close - Disables a network interface 7716 * @netdev: network interface device structure 7717 * 7718 * The close entry point is called when an interface is de-activated 7719 * by the OS. The hardware is still under the driver's control, but 7720 * this netdev interface is disabled. 7721 * 7722 * Returns 0, this is not allowed to fail 7723 **/ 7724 int i40e_close(struct net_device *netdev) 7725 { 7726 struct i40e_netdev_priv *np = netdev_priv(netdev); 7727 struct i40e_vsi *vsi = np->vsi; 7728 7729 i40e_vsi_close(vsi); 7730 7731 return 0; 7732 } 7733 7734 /** 7735 * i40e_do_reset - Start a PF or Core Reset sequence 7736 * @pf: board private structure 7737 * @reset_flags: which reset is requested 7738 * @lock_acquired: indicates whether or not the lock has been acquired 7739 * before this function was called. 7740 * 7741 * The essential difference in resets is that the PF Reset 7742 * doesn't clear the packet buffers, doesn't reset the PE 7743 * firmware, and doesn't bother the other PFs on the chip. 7744 **/ 7745 void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired) 7746 { 7747 u32 val; 7748 7749 WARN_ON(in_interrupt()); 7750 7751 7752 /* do the biggest reset indicated */ 7753 if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) { 7754 7755 /* Request a Global Reset 7756 * 7757 * This will start the chip's countdown to the actual full 7758 * chip reset event, and a warning interrupt to be sent 7759 * to all PFs, including the requestor. Our handler 7760 * for the warning interrupt will deal with the shutdown 7761 * and recovery of the switch setup. 7762 */ 7763 dev_dbg(&pf->pdev->dev, "GlobalR requested\n"); 7764 val = rd32(&pf->hw, I40E_GLGEN_RTRIG); 7765 val |= I40E_GLGEN_RTRIG_GLOBR_MASK; 7766 wr32(&pf->hw, I40E_GLGEN_RTRIG, val); 7767 7768 } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) { 7769 7770 /* Request a Core Reset 7771 * 7772 * Same as Global Reset, except does *not* include the MAC/PHY 7773 */ 7774 dev_dbg(&pf->pdev->dev, "CoreR requested\n"); 7775 val = rd32(&pf->hw, I40E_GLGEN_RTRIG); 7776 val |= I40E_GLGEN_RTRIG_CORER_MASK; 7777 wr32(&pf->hw, I40E_GLGEN_RTRIG, val); 7778 i40e_flush(&pf->hw); 7779 7780 } else if (reset_flags & I40E_PF_RESET_FLAG) { 7781 7782 /* Request a PF Reset 7783 * 7784 * Resets only the PF-specific registers 7785 * 7786 * This goes directly to the tear-down and rebuild of 7787 * the switch, since we need to do all the recovery as 7788 * for the Core Reset. 7789 */ 7790 dev_dbg(&pf->pdev->dev, "PFR requested\n"); 7791 i40e_handle_reset_warning(pf, lock_acquired); 7792 7793 } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) { 7794 int v; 7795 7796 /* Find the VSI(s) that requested a re-init */ 7797 dev_info(&pf->pdev->dev, 7798 "VSI reinit requested\n"); 7799 for (v = 0; v < pf->num_alloc_vsi; v++) { 7800 struct i40e_vsi *vsi = pf->vsi[v]; 7801 7802 if (vsi != NULL && 7803 test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED, 7804 vsi->state)) 7805 i40e_vsi_reinit_locked(pf->vsi[v]); 7806 } 7807 } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) { 7808 int v; 7809 7810 /* Find the VSI(s) that needs to be brought down */ 7811 dev_info(&pf->pdev->dev, "VSI down requested\n"); 7812 for (v = 0; v < pf->num_alloc_vsi; v++) { 7813 struct i40e_vsi *vsi = pf->vsi[v]; 7814 7815 if (vsi != NULL && 7816 test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED, 7817 vsi->state)) { 7818 set_bit(__I40E_VSI_DOWN, vsi->state); 7819 i40e_down(vsi); 7820 } 7821 } 7822 } else { 7823 dev_info(&pf->pdev->dev, 7824 "bad reset request 0x%08x\n", reset_flags); 7825 } 7826 } 7827 7828 #ifdef CONFIG_I40E_DCB 7829 /** 7830 * i40e_dcb_need_reconfig - Check if DCB needs reconfig 7831 * @pf: board private structure 7832 * @old_cfg: current DCB config 7833 * @new_cfg: new DCB config 7834 **/ 7835 bool i40e_dcb_need_reconfig(struct i40e_pf *pf, 7836 struct i40e_dcbx_config *old_cfg, 7837 struct i40e_dcbx_config *new_cfg) 7838 { 7839 bool need_reconfig = false; 7840 7841 /* Check if ETS configuration has changed */ 7842 if (memcmp(&new_cfg->etscfg, 7843 &old_cfg->etscfg, 7844 sizeof(new_cfg->etscfg))) { 7845 /* If Priority Table has changed reconfig is needed */ 7846 if (memcmp(&new_cfg->etscfg.prioritytable, 7847 &old_cfg->etscfg.prioritytable, 7848 sizeof(new_cfg->etscfg.prioritytable))) { 7849 need_reconfig = true; 7850 dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n"); 7851 } 7852 7853 if (memcmp(&new_cfg->etscfg.tcbwtable, 7854 &old_cfg->etscfg.tcbwtable, 7855 sizeof(new_cfg->etscfg.tcbwtable))) 7856 dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n"); 7857 7858 if (memcmp(&new_cfg->etscfg.tsatable, 7859 &old_cfg->etscfg.tsatable, 7860 sizeof(new_cfg->etscfg.tsatable))) 7861 dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n"); 7862 } 7863 7864 /* Check if PFC configuration has changed */ 7865 if (memcmp(&new_cfg->pfc, 7866 &old_cfg->pfc, 7867 sizeof(new_cfg->pfc))) { 7868 need_reconfig = true; 7869 dev_dbg(&pf->pdev->dev, "PFC config change detected.\n"); 7870 } 7871 7872 /* Check if APP Table has changed */ 7873 if (memcmp(&new_cfg->app, 7874 &old_cfg->app, 7875 sizeof(new_cfg->app))) { 7876 need_reconfig = true; 7877 dev_dbg(&pf->pdev->dev, "APP Table change detected.\n"); 7878 } 7879 7880 dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig); 7881 return need_reconfig; 7882 } 7883 7884 /** 7885 * i40e_handle_lldp_event - Handle LLDP Change MIB event 7886 * @pf: board private structure 7887 * @e: event info posted on ARQ 7888 **/ 7889 static int i40e_handle_lldp_event(struct i40e_pf *pf, 7890 struct i40e_arq_event_info *e) 7891 { 7892 struct i40e_aqc_lldp_get_mib *mib = 7893 (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw; 7894 struct i40e_hw *hw = &pf->hw; 7895 struct i40e_dcbx_config tmp_dcbx_cfg; 7896 bool need_reconfig = false; 7897 int ret = 0; 7898 u8 type; 7899 7900 /* Not DCB capable or capability disabled */ 7901 if (!(pf->flags & I40E_FLAG_DCB_CAPABLE)) 7902 return ret; 7903 7904 /* Ignore if event is not for Nearest Bridge */ 7905 type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT) 7906 & I40E_AQ_LLDP_BRIDGE_TYPE_MASK); 7907 dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type); 7908 if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE) 7909 return ret; 7910 7911 /* Check MIB Type and return if event for Remote MIB update */ 7912 type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK; 7913 dev_dbg(&pf->pdev->dev, 7914 "LLDP event mib type %s\n", type ? "remote" : "local"); 7915 if (type == I40E_AQ_LLDP_MIB_REMOTE) { 7916 /* Update the remote cached instance and return */ 7917 ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE, 7918 I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE, 7919 &hw->remote_dcbx_config); 7920 goto exit; 7921 } 7922 7923 /* Store the old configuration */ 7924 tmp_dcbx_cfg = hw->local_dcbx_config; 7925 7926 /* Reset the old DCBx configuration data */ 7927 memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config)); 7928 /* Get updated DCBX data from firmware */ 7929 ret = i40e_get_dcb_config(&pf->hw); 7930 if (ret) { 7931 dev_info(&pf->pdev->dev, 7932 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n", 7933 i40e_stat_str(&pf->hw, ret), 7934 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status)); 7935 goto exit; 7936 } 7937 7938 /* No change detected in DCBX configs */ 7939 if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config, 7940 sizeof(tmp_dcbx_cfg))) { 7941 dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n"); 7942 goto exit; 7943 } 7944 7945 need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg, 7946 &hw->local_dcbx_config); 7947 7948 i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config); 7949 7950 if (!need_reconfig) 7951 goto exit; 7952 7953 /* Enable DCB tagging only when more than one TC */ 7954 if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1) 7955 pf->flags |= I40E_FLAG_DCB_ENABLED; 7956 else 7957 pf->flags &= ~I40E_FLAG_DCB_ENABLED; 7958 7959 set_bit(__I40E_PORT_SUSPENDED, pf->state); 7960 /* Reconfiguration needed quiesce all VSIs */ 7961 i40e_pf_quiesce_all_vsi(pf); 7962 7963 /* Changes in configuration update VEB/VSI */ 7964 i40e_dcb_reconfigure(pf); 7965 7966 ret = i40e_resume_port_tx(pf); 7967 7968 clear_bit(__I40E_PORT_SUSPENDED, pf->state); 7969 /* In case of error no point in resuming VSIs */ 7970 if (ret) 7971 goto exit; 7972 7973 /* Wait for the PF's queues to be disabled */ 7974 ret = i40e_pf_wait_queues_disabled(pf); 7975 if (ret) { 7976 /* Schedule PF reset to recover */ 7977 set_bit(__I40E_PF_RESET_REQUESTED, pf->state); 7978 i40e_service_event_schedule(pf); 7979 } else { 7980 i40e_pf_unquiesce_all_vsi(pf); 7981 pf->flags |= (I40E_FLAG_SERVICE_CLIENT_REQUESTED | 7982 I40E_FLAG_CLIENT_L2_CHANGE); 7983 } 7984 7985 exit: 7986 return ret; 7987 } 7988 #endif /* CONFIG_I40E_DCB */ 7989 7990 /** 7991 * i40e_do_reset_safe - Protected reset path for userland calls. 7992 * @pf: board private structure 7993 * @reset_flags: which reset is requested 7994 * 7995 **/ 7996 void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags) 7997 { 7998 rtnl_lock(); 7999 i40e_do_reset(pf, reset_flags, true); 8000 rtnl_unlock(); 8001 } 8002 8003 /** 8004 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event 8005 * @pf: board private structure 8006 * @e: event info posted on ARQ 8007 * 8008 * Handler for LAN Queue Overflow Event generated by the firmware for PF 8009 * and VF queues 8010 **/ 8011 static void i40e_handle_lan_overflow_event(struct i40e_pf *pf, 8012 struct i40e_arq_event_info *e) 8013 { 8014 struct i40e_aqc_lan_overflow *data = 8015 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw; 8016 u32 queue = le32_to_cpu(data->prtdcb_rupto); 8017 u32 qtx_ctl = le32_to_cpu(data->otx_ctl); 8018 struct i40e_hw *hw = &pf->hw; 8019 struct i40e_vf *vf; 8020 u16 vf_id; 8021 8022 dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n", 8023 queue, qtx_ctl); 8024 8025 /* Queue belongs to VF, find the VF and issue VF reset */ 8026 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK) 8027 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) { 8028 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK) 8029 >> I40E_QTX_CTL_VFVM_INDX_SHIFT); 8030 vf_id -= hw->func_caps.vf_base_id; 8031 vf = &pf->vf[vf_id]; 8032 i40e_vc_notify_vf_reset(vf); 8033 /* Allow VF to process pending reset notification */ 8034 msleep(20); 8035 i40e_reset_vf(vf, false); 8036 } 8037 } 8038 8039 /** 8040 * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters 8041 * @pf: board private structure 8042 **/ 8043 u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf) 8044 { 8045 u32 val, fcnt_prog; 8046 8047 val = rd32(&pf->hw, I40E_PFQF_FDSTAT); 8048 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK); 8049 return fcnt_prog; 8050 } 8051 8052 /** 8053 * i40e_get_current_fd_count - Get total FD filters programmed for this PF 8054 * @pf: board private structure 8055 **/ 8056 u32 i40e_get_current_fd_count(struct i40e_pf *pf) 8057 { 8058 u32 val, fcnt_prog; 8059 8060 val = rd32(&pf->hw, I40E_PFQF_FDSTAT); 8061 fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) + 8062 ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >> 8063 I40E_PFQF_FDSTAT_BEST_CNT_SHIFT); 8064 return fcnt_prog; 8065 } 8066 8067 /** 8068 * i40e_get_global_fd_count - Get total FD filters programmed on device 8069 * @pf: board private structure 8070 **/ 8071 u32 i40e_get_global_fd_count(struct i40e_pf *pf) 8072 { 8073 u32 val, fcnt_prog; 8074 8075 val = rd32(&pf->hw, I40E_GLQF_FDCNT_0); 8076 fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) + 8077 ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >> 8078 I40E_GLQF_FDCNT_0_BESTCNT_SHIFT); 8079 return fcnt_prog; 8080 } 8081 8082 /** 8083 * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled 8084 * @pf: board private structure 8085 **/ 8086 void i40e_fdir_check_and_reenable(struct i40e_pf *pf) 8087 { 8088 struct i40e_fdir_filter *filter; 8089 u32 fcnt_prog, fcnt_avail; 8090 struct hlist_node *node; 8091 8092 if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state)) 8093 return; 8094 8095 /* Check if we have enough room to re-enable FDir SB capability. */ 8096 fcnt_prog = i40e_get_global_fd_count(pf); 8097 fcnt_avail = pf->fdir_pf_filter_count; 8098 if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) || 8099 (pf->fd_add_err == 0) || 8100 (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt)) { 8101 if (pf->flags & I40E_FLAG_FD_SB_AUTO_DISABLED) { 8102 pf->flags &= ~I40E_FLAG_FD_SB_AUTO_DISABLED; 8103 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) && 8104 (I40E_DEBUG_FD & pf->hw.debug_mask)) 8105 dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n"); 8106 } 8107 } 8108 8109 /* We should wait for even more space before re-enabling ATR. 8110 * Additionally, we cannot enable ATR as long as we still have TCP SB 8111 * rules active. 8112 */ 8113 if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) && 8114 (pf->fd_tcp4_filter_cnt == 0)) { 8115 if (pf->flags & I40E_FLAG_FD_ATR_AUTO_DISABLED) { 8116 pf->flags &= ~I40E_FLAG_FD_ATR_AUTO_DISABLED; 8117 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) && 8118 (I40E_DEBUG_FD & pf->hw.debug_mask)) 8119 dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n"); 8120 } 8121 } 8122 8123 /* if hw had a problem adding a filter, delete it */ 8124 if (pf->fd_inv > 0) { 8125 hlist_for_each_entry_safe(filter, node, 8126 &pf->fdir_filter_list, fdir_node) { 8127 if (filter->fd_id == pf->fd_inv) { 8128 hlist_del(&filter->fdir_node); 8129 kfree(filter); 8130 pf->fdir_pf_active_filters--; 8131 pf->fd_inv = 0; 8132 } 8133 } 8134 } 8135 } 8136 8137 #define I40E_MIN_FD_FLUSH_INTERVAL 10 8138 #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30 8139 /** 8140 * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB 8141 * @pf: board private structure 8142 **/ 8143 static void i40e_fdir_flush_and_replay(struct i40e_pf *pf) 8144 { 8145 unsigned long min_flush_time; 8146 int flush_wait_retry = 50; 8147 bool disable_atr = false; 8148 int fd_room; 8149 int reg; 8150 8151 if (!time_after(jiffies, pf->fd_flush_timestamp + 8152 (I40E_MIN_FD_FLUSH_INTERVAL * HZ))) 8153 return; 8154 8155 /* If the flush is happening too quick and we have mostly SB rules we 8156 * should not re-enable ATR for some time. 8157 */ 8158 min_flush_time = pf->fd_flush_timestamp + 8159 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ); 8160 fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters; 8161 8162 if (!(time_after(jiffies, min_flush_time)) && 8163 (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) { 8164 if (I40E_DEBUG_FD & pf->hw.debug_mask) 8165 dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n"); 8166 disable_atr = true; 8167 } 8168 8169 pf->fd_flush_timestamp = jiffies; 8170 pf->flags |= I40E_FLAG_FD_ATR_AUTO_DISABLED; 8171 /* flush all filters */ 8172 wr32(&pf->hw, I40E_PFQF_CTL_1, 8173 I40E_PFQF_CTL_1_CLEARFDTABLE_MASK); 8174 i40e_flush(&pf->hw); 8175 pf->fd_flush_cnt++; 8176 pf->fd_add_err = 0; 8177 do { 8178 /* Check FD flush status every 5-6msec */ 8179 usleep_range(5000, 6000); 8180 reg = rd32(&pf->hw, I40E_PFQF_CTL_1); 8181 if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK)) 8182 break; 8183 } while (flush_wait_retry--); 8184 if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) { 8185 dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n"); 8186 } else { 8187 /* replay sideband filters */ 8188 i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]); 8189 if (!disable_atr && !pf->fd_tcp4_filter_cnt) 8190 pf->flags &= ~I40E_FLAG_FD_ATR_AUTO_DISABLED; 8191 clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state); 8192 if (I40E_DEBUG_FD & pf->hw.debug_mask) 8193 dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n"); 8194 } 8195 } 8196 8197 /** 8198 * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed 8199 * @pf: board private structure 8200 **/ 8201 u32 i40e_get_current_atr_cnt(struct i40e_pf *pf) 8202 { 8203 return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters; 8204 } 8205 8206 /* We can see up to 256 filter programming desc in transit if the filters are 8207 * being applied really fast; before we see the first 8208 * filter miss error on Rx queue 0. Accumulating enough error messages before 8209 * reacting will make sure we don't cause flush too often. 8210 */ 8211 #define I40E_MAX_FD_PROGRAM_ERROR 256 8212 8213 /** 8214 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table 8215 * @pf: board private structure 8216 **/ 8217 static void i40e_fdir_reinit_subtask(struct i40e_pf *pf) 8218 { 8219 8220 /* if interface is down do nothing */ 8221 if (test_bit(__I40E_DOWN, pf->state)) 8222 return; 8223 8224 if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state)) 8225 i40e_fdir_flush_and_replay(pf); 8226 8227 i40e_fdir_check_and_reenable(pf); 8228 8229 } 8230 8231 /** 8232 * i40e_vsi_link_event - notify VSI of a link event 8233 * @vsi: vsi to be notified 8234 * @link_up: link up or down 8235 **/ 8236 static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up) 8237 { 8238 if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state)) 8239 return; 8240 8241 switch (vsi->type) { 8242 case I40E_VSI_MAIN: 8243 if (!vsi->netdev || !vsi->netdev_registered) 8244 break; 8245 8246 if (link_up) { 8247 netif_carrier_on(vsi->netdev); 8248 netif_tx_wake_all_queues(vsi->netdev); 8249 } else { 8250 netif_carrier_off(vsi->netdev); 8251 netif_tx_stop_all_queues(vsi->netdev); 8252 } 8253 break; 8254 8255 case I40E_VSI_SRIOV: 8256 case I40E_VSI_VMDQ2: 8257 case I40E_VSI_CTRL: 8258 case I40E_VSI_IWARP: 8259 case I40E_VSI_MIRROR: 8260 default: 8261 /* there is no notification for other VSIs */ 8262 break; 8263 } 8264 } 8265 8266 /** 8267 * i40e_veb_link_event - notify elements on the veb of a link event 8268 * @veb: veb to be notified 8269 * @link_up: link up or down 8270 **/ 8271 static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up) 8272 { 8273 struct i40e_pf *pf; 8274 int i; 8275 8276 if (!veb || !veb->pf) 8277 return; 8278 pf = veb->pf; 8279 8280 /* depth first... */ 8281 for (i = 0; i < I40E_MAX_VEB; i++) 8282 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid)) 8283 i40e_veb_link_event(pf->veb[i], link_up); 8284 8285 /* ... now the local VSIs */ 8286 for (i = 0; i < pf->num_alloc_vsi; i++) 8287 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid)) 8288 i40e_vsi_link_event(pf->vsi[i], link_up); 8289 } 8290 8291 /** 8292 * i40e_link_event - Update netif_carrier status 8293 * @pf: board private structure 8294 **/ 8295 static void i40e_link_event(struct i40e_pf *pf) 8296 { 8297 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi]; 8298 u8 new_link_speed, old_link_speed; 8299 i40e_status status; 8300 bool new_link, old_link; 8301 8302 /* save off old link status information */ 8303 pf->hw.phy.link_info_old = pf->hw.phy.link_info; 8304 8305 /* set this to force the get_link_status call to refresh state */ 8306 pf->hw.phy.get_link_info = true; 8307 8308 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP); 8309 8310 status = i40e_get_link_status(&pf->hw, &new_link); 8311 8312 /* On success, disable temp link polling */ 8313 if (status == I40E_SUCCESS) { 8314 if (pf->flags & I40E_FLAG_TEMP_LINK_POLLING) 8315 pf->flags &= ~I40E_FLAG_TEMP_LINK_POLLING; 8316 } else { 8317 /* Enable link polling temporarily until i40e_get_link_status 8318 * returns I40E_SUCCESS 8319 */ 8320 pf->flags |= I40E_FLAG_TEMP_LINK_POLLING; 8321 dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n", 8322 status); 8323 return; 8324 } 8325 8326 old_link_speed = pf->hw.phy.link_info_old.link_speed; 8327 new_link_speed = pf->hw.phy.link_info.link_speed; 8328 8329 if (new_link == old_link && 8330 new_link_speed == old_link_speed && 8331 (test_bit(__I40E_VSI_DOWN, vsi->state) || 8332 new_link == netif_carrier_ok(vsi->netdev))) 8333 return; 8334 8335 i40e_print_link_message(vsi, new_link); 8336 8337 /* Notify the base of the switch tree connected to 8338 * the link. Floating VEBs are not notified. 8339 */ 8340 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb]) 8341 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link); 8342 else 8343 i40e_vsi_link_event(vsi, new_link); 8344 8345 if (pf->vf) 8346 i40e_vc_notify_link_state(pf); 8347 8348 if (pf->flags & I40E_FLAG_PTP) 8349 i40e_ptp_set_increment(pf); 8350 } 8351 8352 /** 8353 * i40e_watchdog_subtask - periodic checks not using event driven response 8354 * @pf: board private structure 8355 **/ 8356 static void i40e_watchdog_subtask(struct i40e_pf *pf) 8357 { 8358 int i; 8359 8360 /* if interface is down do nothing */ 8361 if (test_bit(__I40E_DOWN, pf->state) || 8362 test_bit(__I40E_CONFIG_BUSY, pf->state)) 8363 return; 8364 8365 /* make sure we don't do these things too often */ 8366 if (time_before(jiffies, (pf->service_timer_previous + 8367 pf->service_timer_period))) 8368 return; 8369 pf->service_timer_previous = jiffies; 8370 8371 if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) || 8372 (pf->flags & I40E_FLAG_TEMP_LINK_POLLING)) 8373 i40e_link_event(pf); 8374 8375 /* Update the stats for active netdevs so the network stack 8376 * can look at updated numbers whenever it cares to 8377 */ 8378 for (i = 0; i < pf->num_alloc_vsi; i++) 8379 if (pf->vsi[i] && pf->vsi[i]->netdev) 8380 i40e_update_stats(pf->vsi[i]); 8381 8382 if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) { 8383 /* Update the stats for the active switching components */ 8384 for (i = 0; i < I40E_MAX_VEB; i++) 8385 if (pf->veb[i]) 8386 i40e_update_veb_stats(pf->veb[i]); 8387 } 8388 8389 i40e_ptp_rx_hang(pf); 8390 i40e_ptp_tx_hang(pf); 8391 } 8392 8393 /** 8394 * i40e_reset_subtask - Set up for resetting the device and driver 8395 * @pf: board private structure 8396 **/ 8397 static void i40e_reset_subtask(struct i40e_pf *pf) 8398 { 8399 u32 reset_flags = 0; 8400 8401 if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) { 8402 reset_flags |= BIT(__I40E_REINIT_REQUESTED); 8403 clear_bit(__I40E_REINIT_REQUESTED, pf->state); 8404 } 8405 if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) { 8406 reset_flags |= BIT(__I40E_PF_RESET_REQUESTED); 8407 clear_bit(__I40E_PF_RESET_REQUESTED, pf->state); 8408 } 8409 if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) { 8410 reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED); 8411 clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state); 8412 } 8413 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) { 8414 reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED); 8415 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state); 8416 } 8417 if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) { 8418 reset_flags |= BIT(__I40E_DOWN_REQUESTED); 8419 clear_bit(__I40E_DOWN_REQUESTED, pf->state); 8420 } 8421 8422 /* If there's a recovery already waiting, it takes 8423 * precedence before starting a new reset sequence. 8424 */ 8425 if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) { 8426 i40e_prep_for_reset(pf, false); 8427 i40e_reset(pf); 8428 i40e_rebuild(pf, false, false); 8429 } 8430 8431 /* If we're already down or resetting, just bail */ 8432 if (reset_flags && 8433 !test_bit(__I40E_DOWN, pf->state) && 8434 !test_bit(__I40E_CONFIG_BUSY, pf->state)) { 8435 i40e_do_reset(pf, reset_flags, false); 8436 } 8437 } 8438 8439 /** 8440 * i40e_handle_link_event - Handle link event 8441 * @pf: board private structure 8442 * @e: event info posted on ARQ 8443 **/ 8444 static void i40e_handle_link_event(struct i40e_pf *pf, 8445 struct i40e_arq_event_info *e) 8446 { 8447 struct i40e_aqc_get_link_status *status = 8448 (struct i40e_aqc_get_link_status *)&e->desc.params.raw; 8449 8450 /* Do a new status request to re-enable LSE reporting 8451 * and load new status information into the hw struct 8452 * This completely ignores any state information 8453 * in the ARQ event info, instead choosing to always 8454 * issue the AQ update link status command. 8455 */ 8456 i40e_link_event(pf); 8457 8458 /* Check if module meets thermal requirements */ 8459 if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) { 8460 dev_err(&pf->pdev->dev, 8461 "Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n"); 8462 dev_err(&pf->pdev->dev, 8463 "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n"); 8464 } else { 8465 /* check for unqualified module, if link is down, suppress 8466 * the message if link was forced to be down. 8467 */ 8468 if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) && 8469 (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) && 8470 (!(status->link_info & I40E_AQ_LINK_UP)) && 8471 (!(pf->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED))) { 8472 dev_err(&pf->pdev->dev, 8473 "Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n"); 8474 dev_err(&pf->pdev->dev, 8475 "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n"); 8476 } 8477 } 8478 } 8479 8480 /** 8481 * i40e_clean_adminq_subtask - Clean the AdminQ rings 8482 * @pf: board private structure 8483 **/ 8484 static void i40e_clean_adminq_subtask(struct i40e_pf *pf) 8485 { 8486 struct i40e_arq_event_info event; 8487 struct i40e_hw *hw = &pf->hw; 8488 u16 pending, i = 0; 8489 i40e_status ret; 8490 u16 opcode; 8491 u32 oldval; 8492 u32 val; 8493 8494 /* Do not run clean AQ when PF reset fails */ 8495 if (test_bit(__I40E_RESET_FAILED, pf->state)) 8496 return; 8497 8498 /* check for error indications */ 8499 val = rd32(&pf->hw, pf->hw.aq.arq.len); 8500 oldval = val; 8501 if (val & I40E_PF_ARQLEN_ARQVFE_MASK) { 8502 if (hw->debug_mask & I40E_DEBUG_AQ) 8503 dev_info(&pf->pdev->dev, "ARQ VF Error detected\n"); 8504 val &= ~I40E_PF_ARQLEN_ARQVFE_MASK; 8505 } 8506 if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) { 8507 if (hw->debug_mask & I40E_DEBUG_AQ) 8508 dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n"); 8509 val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK; 8510 pf->arq_overflows++; 8511 } 8512 if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) { 8513 if (hw->debug_mask & I40E_DEBUG_AQ) 8514 dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n"); 8515 val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK; 8516 } 8517 if (oldval != val) 8518 wr32(&pf->hw, pf->hw.aq.arq.len, val); 8519 8520 val = rd32(&pf->hw, pf->hw.aq.asq.len); 8521 oldval = val; 8522 if (val & I40E_PF_ATQLEN_ATQVFE_MASK) { 8523 if (pf->hw.debug_mask & I40E_DEBUG_AQ) 8524 dev_info(&pf->pdev->dev, "ASQ VF Error detected\n"); 8525 val &= ~I40E_PF_ATQLEN_ATQVFE_MASK; 8526 } 8527 if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) { 8528 if (pf->hw.debug_mask & I40E_DEBUG_AQ) 8529 dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n"); 8530 val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK; 8531 } 8532 if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) { 8533 if (pf->hw.debug_mask & I40E_DEBUG_AQ) 8534 dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n"); 8535 val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK; 8536 } 8537 if (oldval != val) 8538 wr32(&pf->hw, pf->hw.aq.asq.len, val); 8539 8540 event.buf_len = I40E_MAX_AQ_BUF_SIZE; 8541 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL); 8542 if (!event.msg_buf) 8543 return; 8544 8545 do { 8546 ret = i40e_clean_arq_element(hw, &event, &pending); 8547 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK) 8548 break; 8549 else if (ret) { 8550 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret); 8551 break; 8552 } 8553 8554 opcode = le16_to_cpu(event.desc.opcode); 8555 switch (opcode) { 8556 8557 case i40e_aqc_opc_get_link_status: 8558 i40e_handle_link_event(pf, &event); 8559 break; 8560 case i40e_aqc_opc_send_msg_to_pf: 8561 ret = i40e_vc_process_vf_msg(pf, 8562 le16_to_cpu(event.desc.retval), 8563 le32_to_cpu(event.desc.cookie_high), 8564 le32_to_cpu(event.desc.cookie_low), 8565 event.msg_buf, 8566 event.msg_len); 8567 break; 8568 case i40e_aqc_opc_lldp_update_mib: 8569 dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n"); 8570 #ifdef CONFIG_I40E_DCB 8571 rtnl_lock(); 8572 ret = i40e_handle_lldp_event(pf, &event); 8573 rtnl_unlock(); 8574 #endif /* CONFIG_I40E_DCB */ 8575 break; 8576 case i40e_aqc_opc_event_lan_overflow: 8577 dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n"); 8578 i40e_handle_lan_overflow_event(pf, &event); 8579 break; 8580 case i40e_aqc_opc_send_msg_to_peer: 8581 dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n"); 8582 break; 8583 case i40e_aqc_opc_nvm_erase: 8584 case i40e_aqc_opc_nvm_update: 8585 case i40e_aqc_opc_oem_post_update: 8586 i40e_debug(&pf->hw, I40E_DEBUG_NVM, 8587 "ARQ NVM operation 0x%04x completed\n", 8588 opcode); 8589 break; 8590 default: 8591 dev_info(&pf->pdev->dev, 8592 "ARQ: Unknown event 0x%04x ignored\n", 8593 opcode); 8594 break; 8595 } 8596 } while (i++ < pf->adminq_work_limit); 8597 8598 if (i < pf->adminq_work_limit) 8599 clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state); 8600 8601 /* re-enable Admin queue interrupt cause */ 8602 val = rd32(hw, I40E_PFINT_ICR0_ENA); 8603 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK; 8604 wr32(hw, I40E_PFINT_ICR0_ENA, val); 8605 i40e_flush(hw); 8606 8607 kfree(event.msg_buf); 8608 } 8609 8610 /** 8611 * i40e_verify_eeprom - make sure eeprom is good to use 8612 * @pf: board private structure 8613 **/ 8614 static void i40e_verify_eeprom(struct i40e_pf *pf) 8615 { 8616 int err; 8617 8618 err = i40e_diag_eeprom_test(&pf->hw); 8619 if (err) { 8620 /* retry in case of garbage read */ 8621 err = i40e_diag_eeprom_test(&pf->hw); 8622 if (err) { 8623 dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n", 8624 err); 8625 set_bit(__I40E_BAD_EEPROM, pf->state); 8626 } 8627 } 8628 8629 if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) { 8630 dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n"); 8631 clear_bit(__I40E_BAD_EEPROM, pf->state); 8632 } 8633 } 8634 8635 /** 8636 * i40e_enable_pf_switch_lb 8637 * @pf: pointer to the PF structure 8638 * 8639 * enable switch loop back or die - no point in a return value 8640 **/ 8641 static void i40e_enable_pf_switch_lb(struct i40e_pf *pf) 8642 { 8643 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi]; 8644 struct i40e_vsi_context ctxt; 8645 int ret; 8646 8647 ctxt.seid = pf->main_vsi_seid; 8648 ctxt.pf_num = pf->hw.pf_id; 8649 ctxt.vf_num = 0; 8650 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL); 8651 if (ret) { 8652 dev_info(&pf->pdev->dev, 8653 "couldn't get PF vsi config, err %s aq_err %s\n", 8654 i40e_stat_str(&pf->hw, ret), 8655 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status)); 8656 return; 8657 } 8658 ctxt.flags = I40E_AQ_VSI_TYPE_PF; 8659 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID); 8660 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB); 8661 8662 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL); 8663 if (ret) { 8664 dev_info(&pf->pdev->dev, 8665 "update vsi switch failed, err %s aq_err %s\n", 8666 i40e_stat_str(&pf->hw, ret), 8667 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status)); 8668 } 8669 } 8670 8671 /** 8672 * i40e_disable_pf_switch_lb 8673 * @pf: pointer to the PF structure 8674 * 8675 * disable switch loop back or die - no point in a return value 8676 **/ 8677 static void i40e_disable_pf_switch_lb(struct i40e_pf *pf) 8678 { 8679 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi]; 8680 struct i40e_vsi_context ctxt; 8681 int ret; 8682 8683 ctxt.seid = pf->main_vsi_seid; 8684 ctxt.pf_num = pf->hw.pf_id; 8685 ctxt.vf_num = 0; 8686 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL); 8687 if (ret) { 8688 dev_info(&pf->pdev->dev, 8689 "couldn't get PF vsi config, err %s aq_err %s\n", 8690 i40e_stat_str(&pf->hw, ret), 8691 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status)); 8692 return; 8693 } 8694 ctxt.flags = I40E_AQ_VSI_TYPE_PF; 8695 ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID); 8696 ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB); 8697 8698 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL); 8699 if (ret) { 8700 dev_info(&pf->pdev->dev, 8701 "update vsi switch failed, err %s aq_err %s\n", 8702 i40e_stat_str(&pf->hw, ret), 8703 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status)); 8704 } 8705 } 8706 8707 /** 8708 * i40e_config_bridge_mode - Configure the HW bridge mode 8709 * @veb: pointer to the bridge instance 8710 * 8711 * Configure the loop back mode for the LAN VSI that is downlink to the 8712 * specified HW bridge instance. It is expected this function is called 8713 * when a new HW bridge is instantiated. 8714 **/ 8715 static void i40e_config_bridge_mode(struct i40e_veb *veb) 8716 { 8717 struct i40e_pf *pf = veb->pf; 8718 8719 if (pf->hw.debug_mask & I40E_DEBUG_LAN) 8720 dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n", 8721 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB"); 8722 if (veb->bridge_mode & BRIDGE_MODE_VEPA) 8723 i40e_disable_pf_switch_lb(pf); 8724 else 8725 i40e_enable_pf_switch_lb(pf); 8726 } 8727 8728 /** 8729 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it 8730 * @veb: pointer to the VEB instance 8731 * 8732 * This is a recursive function that first builds the attached VSIs then 8733 * recurses in to build the next layer of VEB. We track the connections 8734 * through our own index numbers because the seid's from the HW could 8735 * change across the reset. 8736 **/ 8737 static int i40e_reconstitute_veb(struct i40e_veb *veb) 8738 { 8739 struct i40e_vsi *ctl_vsi = NULL; 8740 struct i40e_pf *pf = veb->pf; 8741 int v, veb_idx; 8742 int ret; 8743 8744 /* build VSI that owns this VEB, temporarily attached to base VEB */ 8745 for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) { 8746 if (pf->vsi[v] && 8747 pf->vsi[v]->veb_idx == veb->idx && 8748 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) { 8749 ctl_vsi = pf->vsi[v]; 8750 break; 8751 } 8752 } 8753 if (!ctl_vsi) { 8754 dev_info(&pf->pdev->dev, 8755 "missing owner VSI for veb_idx %d\n", veb->idx); 8756 ret = -ENOENT; 8757 goto end_reconstitute; 8758 } 8759 if (ctl_vsi != pf->vsi[pf->lan_vsi]) 8760 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid; 8761 ret = i40e_add_vsi(ctl_vsi); 8762 if (ret) { 8763 dev_info(&pf->pdev->dev, 8764 "rebuild of veb_idx %d owner VSI failed: %d\n", 8765 veb->idx, ret); 8766 goto end_reconstitute; 8767 } 8768 i40e_vsi_reset_stats(ctl_vsi); 8769 8770 /* create the VEB in the switch and move the VSI onto the VEB */ 8771 ret = i40e_add_veb(veb, ctl_vsi); 8772 if (ret) 8773 goto end_reconstitute; 8774 8775 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED) 8776 veb->bridge_mode = BRIDGE_MODE_VEB; 8777 else 8778 veb->bridge_mode = BRIDGE_MODE_VEPA; 8779 i40e_config_bridge_mode(veb); 8780 8781 /* create the remaining VSIs attached to this VEB */ 8782 for (v = 0; v < pf->num_alloc_vsi; v++) { 8783 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi) 8784 continue; 8785 8786 if (pf->vsi[v]->veb_idx == veb->idx) { 8787 struct i40e_vsi *vsi = pf->vsi[v]; 8788 8789 vsi->uplink_seid = veb->seid; 8790 ret = i40e_add_vsi(vsi); 8791 if (ret) { 8792 dev_info(&pf->pdev->dev, 8793 "rebuild of vsi_idx %d failed: %d\n", 8794 v, ret); 8795 goto end_reconstitute; 8796 } 8797 i40e_vsi_reset_stats(vsi); 8798 } 8799 } 8800 8801 /* create any VEBs attached to this VEB - RECURSION */ 8802 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) { 8803 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) { 8804 pf->veb[veb_idx]->uplink_seid = veb->seid; 8805 ret = i40e_reconstitute_veb(pf->veb[veb_idx]); 8806 if (ret) 8807 break; 8808 } 8809 } 8810 8811 end_reconstitute: 8812 return ret; 8813 } 8814 8815 /** 8816 * i40e_get_capabilities - get info about the HW 8817 * @pf: the PF struct 8818 **/ 8819 static int i40e_get_capabilities(struct i40e_pf *pf, 8820 enum i40e_admin_queue_opc list_type) 8821 { 8822 struct i40e_aqc_list_capabilities_element_resp *cap_buf; 8823 u16 data_size; 8824 int buf_len; 8825 int err; 8826 8827 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp); 8828 do { 8829 cap_buf = kzalloc(buf_len, GFP_KERNEL); 8830 if (!cap_buf) 8831 return -ENOMEM; 8832 8833 /* this loads the data into the hw struct for us */ 8834 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len, 8835 &data_size, list_type, 8836 NULL); 8837 /* data loaded, buffer no longer needed */ 8838 kfree(cap_buf); 8839 8840 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) { 8841 /* retry with a larger buffer */ 8842 buf_len = data_size; 8843 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) { 8844 dev_info(&pf->pdev->dev, 8845 "capability discovery failed, err %s aq_err %s\n", 8846 i40e_stat_str(&pf->hw, err), 8847 i40e_aq_str(&pf->hw, 8848 pf->hw.aq.asq_last_status)); 8849 return -ENODEV; 8850 } 8851 } while (err); 8852 8853 if (pf->hw.debug_mask & I40E_DEBUG_USER) { 8854 if (list_type == i40e_aqc_opc_list_func_capabilities) { 8855 dev_info(&pf->pdev->dev, 8856 "pf=%d, num_vfs=%d, msix_pf=%d, msix_vf=%d, fd_g=%d, fd_b=%d, pf_max_q=%d num_vsi=%d\n", 8857 pf->hw.pf_id, pf->hw.func_caps.num_vfs, 8858 pf->hw.func_caps.num_msix_vectors, 8859 pf->hw.func_caps.num_msix_vectors_vf, 8860 pf->hw.func_caps.fd_filters_guaranteed, 8861 pf->hw.func_caps.fd_filters_best_effort, 8862 pf->hw.func_caps.num_tx_qp, 8863 pf->hw.func_caps.num_vsis); 8864 } else if (list_type == i40e_aqc_opc_list_dev_capabilities) { 8865 dev_info(&pf->pdev->dev, 8866 "switch_mode=0x%04x, function_valid=0x%08x\n", 8867 pf->hw.dev_caps.switch_mode, 8868 pf->hw.dev_caps.valid_functions); 8869 dev_info(&pf->pdev->dev, 8870 "SR-IOV=%d, num_vfs for all function=%u\n", 8871 pf->hw.dev_caps.sr_iov_1_1, 8872 pf->hw.dev_caps.num_vfs); 8873 dev_info(&pf->pdev->dev, 8874 "num_vsis=%u, num_rx:%u, num_tx=%u\n", 8875 pf->hw.dev_caps.num_vsis, 8876 pf->hw.dev_caps.num_rx_qp, 8877 pf->hw.dev_caps.num_tx_qp); 8878 } 8879 } 8880 if (list_type == i40e_aqc_opc_list_func_capabilities) { 8881 #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \ 8882 + pf->hw.func_caps.num_vfs) 8883 if (pf->hw.revision_id == 0 && 8884 pf->hw.func_caps.num_vsis < DEF_NUM_VSI) { 8885 dev_info(&pf->pdev->dev, 8886 "got num_vsis %d, setting num_vsis to %d\n", 8887 pf->hw.func_caps.num_vsis, DEF_NUM_VSI); 8888 pf->hw.func_caps.num_vsis = DEF_NUM_VSI; 8889 } 8890 } 8891 return 0; 8892 } 8893 8894 static int i40e_vsi_clear(struct i40e_vsi *vsi); 8895 8896 /** 8897 * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband 8898 * @pf: board private structure 8899 **/ 8900 static void i40e_fdir_sb_setup(struct i40e_pf *pf) 8901 { 8902 struct i40e_vsi *vsi; 8903 8904 /* quick workaround for an NVM issue that leaves a critical register 8905 * uninitialized 8906 */ 8907 if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) { 8908 static const u32 hkey[] = { 8909 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36, 8910 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb, 8911 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21, 8912 0x95b3a76d}; 8913 int i; 8914 8915 for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++) 8916 wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]); 8917 } 8918 8919 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED)) 8920 return; 8921 8922 /* find existing VSI and see if it needs configuring */ 8923 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR); 8924 8925 /* create a new VSI if none exists */ 8926 if (!vsi) { 8927 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR, 8928 pf->vsi[pf->lan_vsi]->seid, 0); 8929 if (!vsi) { 8930 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n"); 8931 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED; 8932 pf->flags |= I40E_FLAG_FD_SB_INACTIVE; 8933 return; 8934 } 8935 } 8936 8937 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring); 8938 } 8939 8940 /** 8941 * i40e_fdir_teardown - release the Flow Director resources 8942 * @pf: board private structure 8943 **/ 8944 static void i40e_fdir_teardown(struct i40e_pf *pf) 8945 { 8946 struct i40e_vsi *vsi; 8947 8948 i40e_fdir_filter_exit(pf); 8949 vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR); 8950 if (vsi) 8951 i40e_vsi_release(vsi); 8952 } 8953 8954 /** 8955 * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs 8956 * @vsi: PF main vsi 8957 * @seid: seid of main or channel VSIs 8958 * 8959 * Rebuilds cloud filters associated with main VSI and channel VSIs if they 8960 * existed before reset 8961 **/ 8962 static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid) 8963 { 8964 struct i40e_cloud_filter *cfilter; 8965 struct i40e_pf *pf = vsi->back; 8966 struct hlist_node *node; 8967 i40e_status ret; 8968 8969 /* Add cloud filters back if they exist */ 8970 hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list, 8971 cloud_node) { 8972 if (cfilter->seid != seid) 8973 continue; 8974 8975 if (cfilter->dst_port) 8976 ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter, 8977 true); 8978 else 8979 ret = i40e_add_del_cloud_filter(vsi, cfilter, true); 8980 8981 if (ret) { 8982 dev_dbg(&pf->pdev->dev, 8983 "Failed to rebuild cloud filter, err %s aq_err %s\n", 8984 i40e_stat_str(&pf->hw, ret), 8985 i40e_aq_str(&pf->hw, 8986 pf->hw.aq.asq_last_status)); 8987 return ret; 8988 } 8989 } 8990 return 0; 8991 } 8992 8993 /** 8994 * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset 8995 * @vsi: PF main vsi 8996 * 8997 * Rebuilds channel VSIs if they existed before reset 8998 **/ 8999 static int i40e_rebuild_channels(struct i40e_vsi *vsi) 9000 { 9001 struct i40e_channel *ch, *ch_tmp; 9002 i40e_status ret; 9003 9004 if (list_empty(&vsi->ch_list)) 9005 return 0; 9006 9007 list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) { 9008 if (!ch->initialized) 9009 break; 9010 /* Proceed with creation of channel (VMDq2) VSI */ 9011 ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch); 9012 if (ret) { 9013 dev_info(&vsi->back->pdev->dev, 9014 "failed to rebuild channels using uplink_seid %u\n", 9015 vsi->uplink_seid); 9016 return ret; 9017 } 9018 /* Reconfigure TX queues using QTX_CTL register */ 9019 ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch); 9020 if (ret) { 9021 dev_info(&vsi->back->pdev->dev, 9022 "failed to configure TX rings for channel %u\n", 9023 ch->seid); 9024 return ret; 9025 } 9026 /* update 'next_base_queue' */ 9027 vsi->next_base_queue = vsi->next_base_queue + 9028 ch->num_queue_pairs; 9029 if (ch->max_tx_rate) { 9030 u64 credits = ch->max_tx_rate; 9031 9032 if (i40e_set_bw_limit(vsi, ch->seid, 9033 ch->max_tx_rate)) 9034 return -EINVAL; 9035 9036 do_div(credits, I40E_BW_CREDIT_DIVISOR); 9037 dev_dbg(&vsi->back->pdev->dev, 9038 "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n", 9039 ch->max_tx_rate, 9040 credits, 9041 ch->seid); 9042 } 9043 ret = i40e_rebuild_cloud_filters(vsi, ch->seid); 9044 if (ret) { 9045 dev_dbg(&vsi->back->pdev->dev, 9046 "Failed to rebuild cloud filters for channel VSI %u\n", 9047 ch->seid); 9048 return ret; 9049 } 9050 } 9051 return 0; 9052 } 9053 9054 /** 9055 * i40e_prep_for_reset - prep for the core to reset 9056 * @pf: board private structure 9057 * @lock_acquired: indicates whether or not the lock has been acquired 9058 * before this function was called. 9059 * 9060 * Close up the VFs and other things in prep for PF Reset. 9061 **/ 9062 static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired) 9063 { 9064 struct i40e_hw *hw = &pf->hw; 9065 i40e_status ret = 0; 9066 u32 v; 9067 9068 clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state); 9069 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) 9070 return; 9071 if (i40e_check_asq_alive(&pf->hw)) 9072 i40e_vc_notify_reset(pf); 9073 9074 dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n"); 9075 9076 /* quiesce the VSIs and their queues that are not already DOWN */ 9077 /* pf_quiesce_all_vsi modifies netdev structures -rtnl_lock needed */ 9078 if (!lock_acquired) 9079 rtnl_lock(); 9080 i40e_pf_quiesce_all_vsi(pf); 9081 if (!lock_acquired) 9082 rtnl_unlock(); 9083 9084 for (v = 0; v < pf->num_alloc_vsi; v++) { 9085 if (pf->vsi[v]) 9086 pf->vsi[v]->seid = 0; 9087 } 9088 9089 i40e_shutdown_adminq(&pf->hw); 9090 9091 /* call shutdown HMC */ 9092 if (hw->hmc.hmc_obj) { 9093 ret = i40e_shutdown_lan_hmc(hw); 9094 if (ret) 9095 dev_warn(&pf->pdev->dev, 9096 "shutdown_lan_hmc failed: %d\n", ret); 9097 } 9098 } 9099 9100 /** 9101 * i40e_send_version - update firmware with driver version 9102 * @pf: PF struct 9103 */ 9104 static void i40e_send_version(struct i40e_pf *pf) 9105 { 9106 struct i40e_driver_version dv; 9107 9108 dv.major_version = DRV_VERSION_MAJOR; 9109 dv.minor_version = DRV_VERSION_MINOR; 9110 dv.build_version = DRV_VERSION_BUILD; 9111 dv.subbuild_version = 0; 9112 strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string)); 9113 i40e_aq_send_driver_version(&pf->hw, &dv, NULL); 9114 } 9115 9116 /** 9117 * i40e_get_oem_version - get OEM specific version information 9118 * @hw: pointer to the hardware structure 9119 **/ 9120 static void i40e_get_oem_version(struct i40e_hw *hw) 9121 { 9122 u16 block_offset = 0xffff; 9123 u16 block_length = 0; 9124 u16 capabilities = 0; 9125 u16 gen_snap = 0; 9126 u16 release = 0; 9127 9128 #define I40E_SR_NVM_OEM_VERSION_PTR 0x1B 9129 #define I40E_NVM_OEM_LENGTH_OFFSET 0x00 9130 #define I40E_NVM_OEM_CAPABILITIES_OFFSET 0x01 9131 #define I40E_NVM_OEM_GEN_OFFSET 0x02 9132 #define I40E_NVM_OEM_RELEASE_OFFSET 0x03 9133 #define I40E_NVM_OEM_CAPABILITIES_MASK 0x000F 9134 #define I40E_NVM_OEM_LENGTH 3 9135 9136 /* Check if pointer to OEM version block is valid. */ 9137 i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset); 9138 if (block_offset == 0xffff) 9139 return; 9140 9141 /* Check if OEM version block has correct length. */ 9142 i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET, 9143 &block_length); 9144 if (block_length < I40E_NVM_OEM_LENGTH) 9145 return; 9146 9147 /* Check if OEM version format is as expected. */ 9148 i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET, 9149 &capabilities); 9150 if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0) 9151 return; 9152 9153 i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET, 9154 &gen_snap); 9155 i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET, 9156 &release); 9157 hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release; 9158 hw->nvm.eetrack = I40E_OEM_EETRACK_ID; 9159 } 9160 9161 /** 9162 * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen 9163 * @pf: board private structure 9164 **/ 9165 static int i40e_reset(struct i40e_pf *pf) 9166 { 9167 struct i40e_hw *hw = &pf->hw; 9168 i40e_status ret; 9169 9170 ret = i40e_pf_reset(hw); 9171 if (ret) { 9172 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret); 9173 set_bit(__I40E_RESET_FAILED, pf->state); 9174 clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state); 9175 } else { 9176 pf->pfr_count++; 9177 } 9178 return ret; 9179 } 9180 9181 /** 9182 * i40e_rebuild - rebuild using a saved config 9183 * @pf: board private structure 9184 * @reinit: if the Main VSI needs to re-initialized. 9185 * @lock_acquired: indicates whether or not the lock has been acquired 9186 * before this function was called. 9187 **/ 9188 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired) 9189 { 9190 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi]; 9191 struct i40e_hw *hw = &pf->hw; 9192 u8 set_fc_aq_fail = 0; 9193 i40e_status ret; 9194 u32 val; 9195 int v; 9196 9197 if (test_bit(__I40E_DOWN, pf->state)) 9198 goto clear_recovery; 9199 dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n"); 9200 9201 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */ 9202 ret = i40e_init_adminq(&pf->hw); 9203 if (ret) { 9204 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n", 9205 i40e_stat_str(&pf->hw, ret), 9206 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status)); 9207 goto clear_recovery; 9208 } 9209 i40e_get_oem_version(&pf->hw); 9210 9211 if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) && 9212 ((hw->aq.fw_maj_ver == 4 && hw->aq.fw_min_ver <= 33) || 9213 hw->aq.fw_maj_ver < 4) && hw->mac.type == I40E_MAC_XL710) { 9214 /* The following delay is necessary for 4.33 firmware and older 9215 * to recover after EMP reset. 200 ms should suffice but we 9216 * put here 300 ms to be sure that FW is ready to operate 9217 * after reset. 9218 */ 9219 mdelay(300); 9220 } 9221 9222 /* re-verify the eeprom if we just had an EMP reset */ 9223 if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state)) 9224 i40e_verify_eeprom(pf); 9225 9226 i40e_clear_pxe_mode(hw); 9227 ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities); 9228 if (ret) 9229 goto end_core_reset; 9230 9231 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp, 9232 hw->func_caps.num_rx_qp, 0, 0); 9233 if (ret) { 9234 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret); 9235 goto end_core_reset; 9236 } 9237 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY); 9238 if (ret) { 9239 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret); 9240 goto end_core_reset; 9241 } 9242 9243 /* Enable FW to write a default DCB config on link-up */ 9244 i40e_aq_set_dcb_parameters(hw, true, NULL); 9245 9246 #ifdef CONFIG_I40E_DCB 9247 ret = i40e_init_pf_dcb(pf); 9248 if (ret) { 9249 dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret); 9250 pf->flags &= ~I40E_FLAG_DCB_CAPABLE; 9251 /* Continue without DCB enabled */ 9252 } 9253 #endif /* CONFIG_I40E_DCB */ 9254 /* do basic switch setup */ 9255 if (!lock_acquired) 9256 rtnl_lock(); 9257 ret = i40e_setup_pf_switch(pf, reinit); 9258 if (ret) 9259 goto end_unlock; 9260 9261 /* The driver only wants link up/down and module qualification 9262 * reports from firmware. Note the negative logic. 9263 */ 9264 ret = i40e_aq_set_phy_int_mask(&pf->hw, 9265 ~(I40E_AQ_EVENT_LINK_UPDOWN | 9266 I40E_AQ_EVENT_MEDIA_NA | 9267 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL); 9268 if (ret) 9269 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n", 9270 i40e_stat_str(&pf->hw, ret), 9271 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status)); 9272 9273 /* make sure our flow control settings are restored */ 9274 ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true); 9275 if (ret) 9276 dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n", 9277 i40e_stat_str(&pf->hw, ret), 9278 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status)); 9279 9280 /* Rebuild the VSIs and VEBs that existed before reset. 9281 * They are still in our local switch element arrays, so only 9282 * need to rebuild the switch model in the HW. 9283 * 9284 * If there were VEBs but the reconstitution failed, we'll try 9285 * try to recover minimal use by getting the basic PF VSI working. 9286 */ 9287 if (vsi->uplink_seid != pf->mac_seid) { 9288 dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n"); 9289 /* find the one VEB connected to the MAC, and find orphans */ 9290 for (v = 0; v < I40E_MAX_VEB; v++) { 9291 if (!pf->veb[v]) 9292 continue; 9293 9294 if (pf->veb[v]->uplink_seid == pf->mac_seid || 9295 pf->veb[v]->uplink_seid == 0) { 9296 ret = i40e_reconstitute_veb(pf->veb[v]); 9297 9298 if (!ret) 9299 continue; 9300 9301 /* If Main VEB failed, we're in deep doodoo, 9302 * so give up rebuilding the switch and set up 9303 * for minimal rebuild of PF VSI. 9304 * If orphan failed, we'll report the error 9305 * but try to keep going. 9306 */ 9307 if (pf->veb[v]->uplink_seid == pf->mac_seid) { 9308 dev_info(&pf->pdev->dev, 9309 "rebuild of switch failed: %d, will try to set up simple PF connection\n", 9310 ret); 9311 vsi->uplink_seid = pf->mac_seid; 9312 break; 9313 } else if (pf->veb[v]->uplink_seid == 0) { 9314 dev_info(&pf->pdev->dev, 9315 "rebuild of orphan VEB failed: %d\n", 9316 ret); 9317 } 9318 } 9319 } 9320 } 9321 9322 if (vsi->uplink_seid == pf->mac_seid) { 9323 dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n"); 9324 /* no VEB, so rebuild only the Main VSI */ 9325 ret = i40e_add_vsi(vsi); 9326 if (ret) { 9327 dev_info(&pf->pdev->dev, 9328 "rebuild of Main VSI failed: %d\n", ret); 9329 goto end_unlock; 9330 } 9331 } 9332 9333 if (vsi->mqprio_qopt.max_rate[0]) { 9334 u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0]; 9335 u64 credits = 0; 9336 9337 do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR); 9338 ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate); 9339 if (ret) 9340 goto end_unlock; 9341 9342 credits = max_tx_rate; 9343 do_div(credits, I40E_BW_CREDIT_DIVISOR); 9344 dev_dbg(&vsi->back->pdev->dev, 9345 "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n", 9346 max_tx_rate, 9347 credits, 9348 vsi->seid); 9349 } 9350 9351 ret = i40e_rebuild_cloud_filters(vsi, vsi->seid); 9352 if (ret) 9353 goto end_unlock; 9354 9355 /* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs 9356 * for this main VSI if they exist 9357 */ 9358 ret = i40e_rebuild_channels(vsi); 9359 if (ret) 9360 goto end_unlock; 9361 9362 /* Reconfigure hardware for allowing smaller MSS in the case 9363 * of TSO, so that we avoid the MDD being fired and causing 9364 * a reset in the case of small MSS+TSO. 9365 */ 9366 #define I40E_REG_MSS 0x000E64DC 9367 #define I40E_REG_MSS_MIN_MASK 0x3FF0000 9368 #define I40E_64BYTE_MSS 0x400000 9369 val = rd32(hw, I40E_REG_MSS); 9370 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) { 9371 val &= ~I40E_REG_MSS_MIN_MASK; 9372 val |= I40E_64BYTE_MSS; 9373 wr32(hw, I40E_REG_MSS, val); 9374 } 9375 9376 if (pf->hw_features & I40E_HW_RESTART_AUTONEG) { 9377 msleep(75); 9378 ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL); 9379 if (ret) 9380 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n", 9381 i40e_stat_str(&pf->hw, ret), 9382 i40e_aq_str(&pf->hw, 9383 pf->hw.aq.asq_last_status)); 9384 } 9385 /* reinit the misc interrupt */ 9386 if (pf->flags & I40E_FLAG_MSIX_ENABLED) 9387 ret = i40e_setup_misc_vector(pf); 9388 9389 /* Add a filter to drop all Flow control frames from any VSI from being 9390 * transmitted. By doing so we stop a malicious VF from sending out 9391 * PAUSE or PFC frames and potentially controlling traffic for other 9392 * PF/VF VSIs. 9393 * The FW can still send Flow control frames if enabled. 9394 */ 9395 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw, 9396 pf->main_vsi_seid); 9397 9398 /* restart the VSIs that were rebuilt and running before the reset */ 9399 i40e_pf_unquiesce_all_vsi(pf); 9400 9401 /* Release the RTNL lock before we start resetting VFs */ 9402 if (!lock_acquired) 9403 rtnl_unlock(); 9404 9405 /* Restore promiscuous settings */ 9406 ret = i40e_set_promiscuous(pf, pf->cur_promisc); 9407 if (ret) 9408 dev_warn(&pf->pdev->dev, 9409 "Failed to restore promiscuous setting: %s, err %s aq_err %s\n", 9410 pf->cur_promisc ? "on" : "off", 9411 i40e_stat_str(&pf->hw, ret), 9412 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status)); 9413 9414 i40e_reset_all_vfs(pf, true); 9415 9416 /* tell the firmware that we're starting */ 9417 i40e_send_version(pf); 9418 9419 /* We've already released the lock, so don't do it again */ 9420 goto end_core_reset; 9421 9422 end_unlock: 9423 if (!lock_acquired) 9424 rtnl_unlock(); 9425 end_core_reset: 9426 clear_bit(__I40E_RESET_FAILED, pf->state); 9427 clear_recovery: 9428 clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state); 9429 } 9430 9431 /** 9432 * i40e_reset_and_rebuild - reset and rebuild using a saved config 9433 * @pf: board private structure 9434 * @reinit: if the Main VSI needs to re-initialized. 9435 * @lock_acquired: indicates whether or not the lock has been acquired 9436 * before this function was called. 9437 **/ 9438 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit, 9439 bool lock_acquired) 9440 { 9441 int ret; 9442 /* Now we wait for GRST to settle out. 9443 * We don't have to delete the VEBs or VSIs from the hw switch 9444 * because the reset will make them disappear. 9445 */ 9446 ret = i40e_reset(pf); 9447 if (!ret) 9448 i40e_rebuild(pf, reinit, lock_acquired); 9449 } 9450 9451 /** 9452 * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild 9453 * @pf: board private structure 9454 * 9455 * Close up the VFs and other things in prep for a Core Reset, 9456 * then get ready to rebuild the world. 9457 * @lock_acquired: indicates whether or not the lock has been acquired 9458 * before this function was called. 9459 **/ 9460 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired) 9461 { 9462 i40e_prep_for_reset(pf, lock_acquired); 9463 i40e_reset_and_rebuild(pf, false, lock_acquired); 9464 } 9465 9466 /** 9467 * i40e_handle_mdd_event 9468 * @pf: pointer to the PF structure 9469 * 9470 * Called from the MDD irq handler to identify possibly malicious vfs 9471 **/ 9472 static void i40e_handle_mdd_event(struct i40e_pf *pf) 9473 { 9474 struct i40e_hw *hw = &pf->hw; 9475 bool mdd_detected = false; 9476 bool pf_mdd_detected = false; 9477 struct i40e_vf *vf; 9478 u32 reg; 9479 int i; 9480 9481 if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state)) 9482 return; 9483 9484 /* find what triggered the MDD event */ 9485 reg = rd32(hw, I40E_GL_MDET_TX); 9486 if (reg & I40E_GL_MDET_TX_VALID_MASK) { 9487 u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >> 9488 I40E_GL_MDET_TX_PF_NUM_SHIFT; 9489 u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >> 9490 I40E_GL_MDET_TX_VF_NUM_SHIFT; 9491 u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >> 9492 I40E_GL_MDET_TX_EVENT_SHIFT; 9493 u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >> 9494 I40E_GL_MDET_TX_QUEUE_SHIFT) - 9495 pf->hw.func_caps.base_queue; 9496 if (netif_msg_tx_err(pf)) 9497 dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n", 9498 event, queue, pf_num, vf_num); 9499 wr32(hw, I40E_GL_MDET_TX, 0xffffffff); 9500 mdd_detected = true; 9501 } 9502 reg = rd32(hw, I40E_GL_MDET_RX); 9503 if (reg & I40E_GL_MDET_RX_VALID_MASK) { 9504 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >> 9505 I40E_GL_MDET_RX_FUNCTION_SHIFT; 9506 u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >> 9507 I40E_GL_MDET_RX_EVENT_SHIFT; 9508 u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >> 9509 I40E_GL_MDET_RX_QUEUE_SHIFT) - 9510 pf->hw.func_caps.base_queue; 9511 if (netif_msg_rx_err(pf)) 9512 dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n", 9513 event, queue, func); 9514 wr32(hw, I40E_GL_MDET_RX, 0xffffffff); 9515 mdd_detected = true; 9516 } 9517 9518 if (mdd_detected) { 9519 reg = rd32(hw, I40E_PF_MDET_TX); 9520 if (reg & I40E_PF_MDET_TX_VALID_MASK) { 9521 wr32(hw, I40E_PF_MDET_TX, 0xFFFF); 9522 dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n"); 9523 pf_mdd_detected = true; 9524 } 9525 reg = rd32(hw, I40E_PF_MDET_RX); 9526 if (reg & I40E_PF_MDET_RX_VALID_MASK) { 9527 wr32(hw, I40E_PF_MDET_RX, 0xFFFF); 9528 dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n"); 9529 pf_mdd_detected = true; 9530 } 9531 /* Queue belongs to the PF, initiate a reset */ 9532 if (pf_mdd_detected) { 9533 set_bit(__I40E_PF_RESET_REQUESTED, pf->state); 9534 i40e_service_event_schedule(pf); 9535 } 9536 } 9537 9538 /* see if one of the VFs needs its hand slapped */ 9539 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) { 9540 vf = &(pf->vf[i]); 9541 reg = rd32(hw, I40E_VP_MDET_TX(i)); 9542 if (reg & I40E_VP_MDET_TX_VALID_MASK) { 9543 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF); 9544 vf->num_mdd_events++; 9545 dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n", 9546 i); 9547 } 9548 9549 reg = rd32(hw, I40E_VP_MDET_RX(i)); 9550 if (reg & I40E_VP_MDET_RX_VALID_MASK) { 9551 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF); 9552 vf->num_mdd_events++; 9553 dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n", 9554 i); 9555 } 9556 9557 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) { 9558 dev_info(&pf->pdev->dev, 9559 "Too many MDD events on VF %d, disabled\n", i); 9560 dev_info(&pf->pdev->dev, 9561 "Use PF Control I/F to re-enable the VF\n"); 9562 set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states); 9563 } 9564 } 9565 9566 /* re-enable mdd interrupt cause */ 9567 clear_bit(__I40E_MDD_EVENT_PENDING, pf->state); 9568 reg = rd32(hw, I40E_PFINT_ICR0_ENA); 9569 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK; 9570 wr32(hw, I40E_PFINT_ICR0_ENA, reg); 9571 i40e_flush(hw); 9572 } 9573 9574 static const char *i40e_tunnel_name(struct i40e_udp_port_config *port) 9575 { 9576 switch (port->type) { 9577 case UDP_TUNNEL_TYPE_VXLAN: 9578 return "vxlan"; 9579 case UDP_TUNNEL_TYPE_GENEVE: 9580 return "geneve"; 9581 default: 9582 return "unknown"; 9583 } 9584 } 9585 9586 /** 9587 * i40e_sync_udp_filters - Trigger a sync event for existing UDP filters 9588 * @pf: board private structure 9589 **/ 9590 static void i40e_sync_udp_filters(struct i40e_pf *pf) 9591 { 9592 int i; 9593 9594 /* loop through and set pending bit for all active UDP filters */ 9595 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) { 9596 if (pf->udp_ports[i].port) 9597 pf->pending_udp_bitmap |= BIT_ULL(i); 9598 } 9599 9600 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC; 9601 } 9602 9603 /** 9604 * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW 9605 * @pf: board private structure 9606 **/ 9607 static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf) 9608 { 9609 struct i40e_hw *hw = &pf->hw; 9610 i40e_status ret; 9611 u16 port; 9612 int i; 9613 9614 if (!(pf->flags & I40E_FLAG_UDP_FILTER_SYNC)) 9615 return; 9616 9617 pf->flags &= ~I40E_FLAG_UDP_FILTER_SYNC; 9618 9619 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) { 9620 if (pf->pending_udp_bitmap & BIT_ULL(i)) { 9621 pf->pending_udp_bitmap &= ~BIT_ULL(i); 9622 port = pf->udp_ports[i].port; 9623 if (port) 9624 ret = i40e_aq_add_udp_tunnel(hw, port, 9625 pf->udp_ports[i].type, 9626 NULL, NULL); 9627 else 9628 ret = i40e_aq_del_udp_tunnel(hw, i, NULL); 9629 9630 if (ret) { 9631 dev_info(&pf->pdev->dev, 9632 "%s %s port %d, index %d failed, err %s aq_err %s\n", 9633 i40e_tunnel_name(&pf->udp_ports[i]), 9634 port ? "add" : "delete", 9635 port, i, 9636 i40e_stat_str(&pf->hw, ret), 9637 i40e_aq_str(&pf->hw, 9638 pf->hw.aq.asq_last_status)); 9639 pf->udp_ports[i].port = 0; 9640 } 9641 } 9642 } 9643 } 9644 9645 /** 9646 * i40e_service_task - Run the driver's async subtasks 9647 * @work: pointer to work_struct containing our data 9648 **/ 9649 static void i40e_service_task(struct work_struct *work) 9650 { 9651 struct i40e_pf *pf = container_of(work, 9652 struct i40e_pf, 9653 service_task); 9654 unsigned long start_time = jiffies; 9655 9656 /* don't bother with service tasks if a reset is in progress */ 9657 if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) 9658 return; 9659 9660 if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state)) 9661 return; 9662 9663 i40e_detect_recover_hung(pf->vsi[pf->lan_vsi]); 9664 i40e_sync_filters_subtask(pf); 9665 i40e_reset_subtask(pf); 9666 i40e_handle_mdd_event(pf); 9667 i40e_vc_process_vflr_event(pf); 9668 i40e_watchdog_subtask(pf); 9669 i40e_fdir_reinit_subtask(pf); 9670 if (pf->flags & I40E_FLAG_CLIENT_RESET) { 9671 /* Client subtask will reopen next time through. */ 9672 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], true); 9673 pf->flags &= ~I40E_FLAG_CLIENT_RESET; 9674 } else { 9675 i40e_client_subtask(pf); 9676 if (pf->flags & I40E_FLAG_CLIENT_L2_CHANGE) { 9677 i40e_notify_client_of_l2_param_changes( 9678 pf->vsi[pf->lan_vsi]); 9679 pf->flags &= ~I40E_FLAG_CLIENT_L2_CHANGE; 9680 } 9681 } 9682 i40e_sync_filters_subtask(pf); 9683 i40e_sync_udp_filters_subtask(pf); 9684 i40e_clean_adminq_subtask(pf); 9685 9686 /* flush memory to make sure state is correct before next watchdog */ 9687 smp_mb__before_atomic(); 9688 clear_bit(__I40E_SERVICE_SCHED, pf->state); 9689 9690 /* If the tasks have taken longer than one timer cycle or there 9691 * is more work to be done, reschedule the service task now 9692 * rather than wait for the timer to tick again. 9693 */ 9694 if (time_after(jiffies, (start_time + pf->service_timer_period)) || 9695 test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state) || 9696 test_bit(__I40E_MDD_EVENT_PENDING, pf->state) || 9697 test_bit(__I40E_VFLR_EVENT_PENDING, pf->state)) 9698 i40e_service_event_schedule(pf); 9699 } 9700 9701 /** 9702 * i40e_service_timer - timer callback 9703 * @data: pointer to PF struct 9704 **/ 9705 static void i40e_service_timer(struct timer_list *t) 9706 { 9707 struct i40e_pf *pf = from_timer(pf, t, service_timer); 9708 9709 mod_timer(&pf->service_timer, 9710 round_jiffies(jiffies + pf->service_timer_period)); 9711 i40e_service_event_schedule(pf); 9712 } 9713 9714 /** 9715 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI 9716 * @vsi: the VSI being configured 9717 **/ 9718 static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi) 9719 { 9720 struct i40e_pf *pf = vsi->back; 9721 9722 switch (vsi->type) { 9723 case I40E_VSI_MAIN: 9724 vsi->alloc_queue_pairs = pf->num_lan_qps; 9725 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS, 9726 I40E_REQ_DESCRIPTOR_MULTIPLE); 9727 if (pf->flags & I40E_FLAG_MSIX_ENABLED) 9728 vsi->num_q_vectors = pf->num_lan_msix; 9729 else 9730 vsi->num_q_vectors = 1; 9731 9732 break; 9733 9734 case I40E_VSI_FDIR: 9735 vsi->alloc_queue_pairs = 1; 9736 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT, 9737 I40E_REQ_DESCRIPTOR_MULTIPLE); 9738 vsi->num_q_vectors = pf->num_fdsb_msix; 9739 break; 9740 9741 case I40E_VSI_VMDQ2: 9742 vsi->alloc_queue_pairs = pf->num_vmdq_qps; 9743 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS, 9744 I40E_REQ_DESCRIPTOR_MULTIPLE); 9745 vsi->num_q_vectors = pf->num_vmdq_msix; 9746 break; 9747 9748 case I40E_VSI_SRIOV: 9749 vsi->alloc_queue_pairs = pf->num_vf_qps; 9750 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS, 9751 I40E_REQ_DESCRIPTOR_MULTIPLE); 9752 break; 9753 9754 default: 9755 WARN_ON(1); 9756 return -ENODATA; 9757 } 9758 9759 return 0; 9760 } 9761 9762 /** 9763 * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi 9764 * @vsi: VSI pointer 9765 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated. 9766 * 9767 * On error: returns error code (negative) 9768 * On success: returns 0 9769 **/ 9770 static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors) 9771 { 9772 struct i40e_ring **next_rings; 9773 int size; 9774 int ret = 0; 9775 9776 /* allocate memory for both Tx, XDP Tx and Rx ring pointers */ 9777 size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs * 9778 (i40e_enabled_xdp_vsi(vsi) ? 3 : 2); 9779 vsi->tx_rings = kzalloc(size, GFP_KERNEL); 9780 if (!vsi->tx_rings) 9781 return -ENOMEM; 9782 next_rings = vsi->tx_rings + vsi->alloc_queue_pairs; 9783 if (i40e_enabled_xdp_vsi(vsi)) { 9784 vsi->xdp_rings = next_rings; 9785 next_rings += vsi->alloc_queue_pairs; 9786 } 9787 vsi->rx_rings = next_rings; 9788 9789 if (alloc_qvectors) { 9790 /* allocate memory for q_vector pointers */ 9791 size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors; 9792 vsi->q_vectors = kzalloc(size, GFP_KERNEL); 9793 if (!vsi->q_vectors) { 9794 ret = -ENOMEM; 9795 goto err_vectors; 9796 } 9797 } 9798 return ret; 9799 9800 err_vectors: 9801 kfree(vsi->tx_rings); 9802 return ret; 9803 } 9804 9805 /** 9806 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF 9807 * @pf: board private structure 9808 * @type: type of VSI 9809 * 9810 * On error: returns error code (negative) 9811 * On success: returns vsi index in PF (positive) 9812 **/ 9813 static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type) 9814 { 9815 int ret = -ENODEV; 9816 struct i40e_vsi *vsi; 9817 int vsi_idx; 9818 int i; 9819 9820 /* Need to protect the allocation of the VSIs at the PF level */ 9821 mutex_lock(&pf->switch_mutex); 9822 9823 /* VSI list may be fragmented if VSI creation/destruction has 9824 * been happening. We can afford to do a quick scan to look 9825 * for any free VSIs in the list. 9826 * 9827 * find next empty vsi slot, looping back around if necessary 9828 */ 9829 i = pf->next_vsi; 9830 while (i < pf->num_alloc_vsi && pf->vsi[i]) 9831 i++; 9832 if (i >= pf->num_alloc_vsi) { 9833 i = 0; 9834 while (i < pf->next_vsi && pf->vsi[i]) 9835 i++; 9836 } 9837 9838 if (i < pf->num_alloc_vsi && !pf->vsi[i]) { 9839 vsi_idx = i; /* Found one! */ 9840 } else { 9841 ret = -ENODEV; 9842 goto unlock_pf; /* out of VSI slots! */ 9843 } 9844 pf->next_vsi = ++i; 9845 9846 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL); 9847 if (!vsi) { 9848 ret = -ENOMEM; 9849 goto unlock_pf; 9850 } 9851 vsi->type = type; 9852 vsi->back = pf; 9853 set_bit(__I40E_VSI_DOWN, vsi->state); 9854 vsi->flags = 0; 9855 vsi->idx = vsi_idx; 9856 vsi->int_rate_limit = 0; 9857 vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ? 9858 pf->rss_table_size : 64; 9859 vsi->netdev_registered = false; 9860 vsi->work_limit = I40E_DEFAULT_IRQ_WORK; 9861 hash_init(vsi->mac_filter_hash); 9862 vsi->irqs_ready = false; 9863 9864 ret = i40e_set_num_rings_in_vsi(vsi); 9865 if (ret) 9866 goto err_rings; 9867 9868 ret = i40e_vsi_alloc_arrays(vsi, true); 9869 if (ret) 9870 goto err_rings; 9871 9872 /* Setup default MSIX irq handler for VSI */ 9873 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings); 9874 9875 /* Initialize VSI lock */ 9876 spin_lock_init(&vsi->mac_filter_hash_lock); 9877 pf->vsi[vsi_idx] = vsi; 9878 ret = vsi_idx; 9879 goto unlock_pf; 9880 9881 err_rings: 9882 pf->next_vsi = i - 1; 9883 kfree(vsi); 9884 unlock_pf: 9885 mutex_unlock(&pf->switch_mutex); 9886 return ret; 9887 } 9888 9889 /** 9890 * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI 9891 * @type: VSI pointer 9892 * @free_qvectors: a bool to specify if q_vectors need to be freed. 9893 * 9894 * On error: returns error code (negative) 9895 * On success: returns 0 9896 **/ 9897 static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors) 9898 { 9899 /* free the ring and vector containers */ 9900 if (free_qvectors) { 9901 kfree(vsi->q_vectors); 9902 vsi->q_vectors = NULL; 9903 } 9904 kfree(vsi->tx_rings); 9905 vsi->tx_rings = NULL; 9906 vsi->rx_rings = NULL; 9907 vsi->xdp_rings = NULL; 9908 } 9909 9910 /** 9911 * i40e_clear_rss_config_user - clear the user configured RSS hash keys 9912 * and lookup table 9913 * @vsi: Pointer to VSI structure 9914 */ 9915 static void i40e_clear_rss_config_user(struct i40e_vsi *vsi) 9916 { 9917 if (!vsi) 9918 return; 9919 9920 kfree(vsi->rss_hkey_user); 9921 vsi->rss_hkey_user = NULL; 9922 9923 kfree(vsi->rss_lut_user); 9924 vsi->rss_lut_user = NULL; 9925 } 9926 9927 /** 9928 * i40e_vsi_clear - Deallocate the VSI provided 9929 * @vsi: the VSI being un-configured 9930 **/ 9931 static int i40e_vsi_clear(struct i40e_vsi *vsi) 9932 { 9933 struct i40e_pf *pf; 9934 9935 if (!vsi) 9936 return 0; 9937 9938 if (!vsi->back) 9939 goto free_vsi; 9940 pf = vsi->back; 9941 9942 mutex_lock(&pf->switch_mutex); 9943 if (!pf->vsi[vsi->idx]) { 9944 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n", 9945 vsi->idx, vsi->idx, vsi->type); 9946 goto unlock_vsi; 9947 } 9948 9949 if (pf->vsi[vsi->idx] != vsi) { 9950 dev_err(&pf->pdev->dev, 9951 "pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n", 9952 pf->vsi[vsi->idx]->idx, 9953 pf->vsi[vsi->idx]->type, 9954 vsi->idx, vsi->type); 9955 goto unlock_vsi; 9956 } 9957 9958 /* updates the PF for this cleared vsi */ 9959 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx); 9960 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx); 9961 9962 i40e_vsi_free_arrays(vsi, true); 9963 i40e_clear_rss_config_user(vsi); 9964 9965 pf->vsi[vsi->idx] = NULL; 9966 if (vsi->idx < pf->next_vsi) 9967 pf->next_vsi = vsi->idx; 9968 9969 unlock_vsi: 9970 mutex_unlock(&pf->switch_mutex); 9971 free_vsi: 9972 kfree(vsi); 9973 9974 return 0; 9975 } 9976 9977 /** 9978 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI 9979 * @vsi: the VSI being cleaned 9980 **/ 9981 static void i40e_vsi_clear_rings(struct i40e_vsi *vsi) 9982 { 9983 int i; 9984 9985 if (vsi->tx_rings && vsi->tx_rings[0]) { 9986 for (i = 0; i < vsi->alloc_queue_pairs; i++) { 9987 kfree_rcu(vsi->tx_rings[i], rcu); 9988 vsi->tx_rings[i] = NULL; 9989 vsi->rx_rings[i] = NULL; 9990 if (vsi->xdp_rings) 9991 vsi->xdp_rings[i] = NULL; 9992 } 9993 } 9994 } 9995 9996 /** 9997 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI 9998 * @vsi: the VSI being configured 9999 **/ 10000 static int i40e_alloc_rings(struct i40e_vsi *vsi) 10001 { 10002 int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2; 10003 struct i40e_pf *pf = vsi->back; 10004 struct i40e_ring *ring; 10005 10006 /* Set basic values in the rings to be used later during open() */ 10007 for (i = 0; i < vsi->alloc_queue_pairs; i++) { 10008 /* allocate space for both Tx and Rx in one shot */ 10009 ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL); 10010 if (!ring) 10011 goto err_out; 10012 10013 ring->queue_index = i; 10014 ring->reg_idx = vsi->base_queue + i; 10015 ring->ring_active = false; 10016 ring->vsi = vsi; 10017 ring->netdev = vsi->netdev; 10018 ring->dev = &pf->pdev->dev; 10019 ring->count = vsi->num_desc; 10020 ring->size = 0; 10021 ring->dcb_tc = 0; 10022 if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE) 10023 ring->flags = I40E_TXR_FLAGS_WB_ON_ITR; 10024 ring->itr_setting = pf->tx_itr_default; 10025 vsi->tx_rings[i] = ring++; 10026 10027 if (!i40e_enabled_xdp_vsi(vsi)) 10028 goto setup_rx; 10029 10030 ring->queue_index = vsi->alloc_queue_pairs + i; 10031 ring->reg_idx = vsi->base_queue + ring->queue_index; 10032 ring->ring_active = false; 10033 ring->vsi = vsi; 10034 ring->netdev = NULL; 10035 ring->dev = &pf->pdev->dev; 10036 ring->count = vsi->num_desc; 10037 ring->size = 0; 10038 ring->dcb_tc = 0; 10039 if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE) 10040 ring->flags = I40E_TXR_FLAGS_WB_ON_ITR; 10041 set_ring_xdp(ring); 10042 ring->itr_setting = pf->tx_itr_default; 10043 vsi->xdp_rings[i] = ring++; 10044 10045 setup_rx: 10046 ring->queue_index = i; 10047 ring->reg_idx = vsi->base_queue + i; 10048 ring->ring_active = false; 10049 ring->vsi = vsi; 10050 ring->netdev = vsi->netdev; 10051 ring->dev = &pf->pdev->dev; 10052 ring->count = vsi->num_desc; 10053 ring->size = 0; 10054 ring->dcb_tc = 0; 10055 ring->itr_setting = pf->rx_itr_default; 10056 vsi->rx_rings[i] = ring; 10057 } 10058 10059 return 0; 10060 10061 err_out: 10062 i40e_vsi_clear_rings(vsi); 10063 return -ENOMEM; 10064 } 10065 10066 /** 10067 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel 10068 * @pf: board private structure 10069 * @vectors: the number of MSI-X vectors to request 10070 * 10071 * Returns the number of vectors reserved, or error 10072 **/ 10073 static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors) 10074 { 10075 vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries, 10076 I40E_MIN_MSIX, vectors); 10077 if (vectors < 0) { 10078 dev_info(&pf->pdev->dev, 10079 "MSI-X vector reservation failed: %d\n", vectors); 10080 vectors = 0; 10081 } 10082 10083 return vectors; 10084 } 10085 10086 /** 10087 * i40e_init_msix - Setup the MSIX capability 10088 * @pf: board private structure 10089 * 10090 * Work with the OS to set up the MSIX vectors needed. 10091 * 10092 * Returns the number of vectors reserved or negative on failure 10093 **/ 10094 static int i40e_init_msix(struct i40e_pf *pf) 10095 { 10096 struct i40e_hw *hw = &pf->hw; 10097 int cpus, extra_vectors; 10098 int vectors_left; 10099 int v_budget, i; 10100 int v_actual; 10101 int iwarp_requested = 0; 10102 10103 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED)) 10104 return -ENODEV; 10105 10106 /* The number of vectors we'll request will be comprised of: 10107 * - Add 1 for "other" cause for Admin Queue events, etc. 10108 * - The number of LAN queue pairs 10109 * - Queues being used for RSS. 10110 * We don't need as many as max_rss_size vectors. 10111 * use rss_size instead in the calculation since that 10112 * is governed by number of cpus in the system. 10113 * - assumes symmetric Tx/Rx pairing 10114 * - The number of VMDq pairs 10115 * - The CPU count within the NUMA node if iWARP is enabled 10116 * Once we count this up, try the request. 10117 * 10118 * If we can't get what we want, we'll simplify to nearly nothing 10119 * and try again. If that still fails, we punt. 10120 */ 10121 vectors_left = hw->func_caps.num_msix_vectors; 10122 v_budget = 0; 10123 10124 /* reserve one vector for miscellaneous handler */ 10125 if (vectors_left) { 10126 v_budget++; 10127 vectors_left--; 10128 } 10129 10130 /* reserve some vectors for the main PF traffic queues. Initially we 10131 * only reserve at most 50% of the available vectors, in the case that 10132 * the number of online CPUs is large. This ensures that we can enable 10133 * extra features as well. Once we've enabled the other features, we 10134 * will use any remaining vectors to reach as close as we can to the 10135 * number of online CPUs. 10136 */ 10137 cpus = num_online_cpus(); 10138 pf->num_lan_msix = min_t(int, cpus, vectors_left / 2); 10139 vectors_left -= pf->num_lan_msix; 10140 10141 /* reserve one vector for sideband flow director */ 10142 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) { 10143 if (vectors_left) { 10144 pf->num_fdsb_msix = 1; 10145 v_budget++; 10146 vectors_left--; 10147 } else { 10148 pf->num_fdsb_msix = 0; 10149 } 10150 } 10151 10152 /* can we reserve enough for iWARP? */ 10153 if (pf->flags & I40E_FLAG_IWARP_ENABLED) { 10154 iwarp_requested = pf->num_iwarp_msix; 10155 10156 if (!vectors_left) 10157 pf->num_iwarp_msix = 0; 10158 else if (vectors_left < pf->num_iwarp_msix) 10159 pf->num_iwarp_msix = 1; 10160 v_budget += pf->num_iwarp_msix; 10161 vectors_left -= pf->num_iwarp_msix; 10162 } 10163 10164 /* any vectors left over go for VMDq support */ 10165 if (pf->flags & I40E_FLAG_VMDQ_ENABLED) { 10166 int vmdq_vecs_wanted = pf->num_vmdq_vsis * pf->num_vmdq_qps; 10167 int vmdq_vecs = min_t(int, vectors_left, vmdq_vecs_wanted); 10168 10169 if (!vectors_left) { 10170 pf->num_vmdq_msix = 0; 10171 pf->num_vmdq_qps = 0; 10172 } else { 10173 /* if we're short on vectors for what's desired, we limit 10174 * the queues per vmdq. If this is still more than are 10175 * available, the user will need to change the number of 10176 * queues/vectors used by the PF later with the ethtool 10177 * channels command 10178 */ 10179 if (vmdq_vecs < vmdq_vecs_wanted) 10180 pf->num_vmdq_qps = 1; 10181 pf->num_vmdq_msix = pf->num_vmdq_qps; 10182 10183 v_budget += vmdq_vecs; 10184 vectors_left -= vmdq_vecs; 10185 } 10186 } 10187 10188 /* On systems with a large number of SMP cores, we previously limited 10189 * the number of vectors for num_lan_msix to be at most 50% of the 10190 * available vectors, to allow for other features. Now, we add back 10191 * the remaining vectors. However, we ensure that the total 10192 * num_lan_msix will not exceed num_online_cpus(). To do this, we 10193 * calculate the number of vectors we can add without going over the 10194 * cap of CPUs. For systems with a small number of CPUs this will be 10195 * zero. 10196 */ 10197 extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left); 10198 pf->num_lan_msix += extra_vectors; 10199 vectors_left -= extra_vectors; 10200 10201 WARN(vectors_left < 0, 10202 "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n"); 10203 10204 v_budget += pf->num_lan_msix; 10205 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry), 10206 GFP_KERNEL); 10207 if (!pf->msix_entries) 10208 return -ENOMEM; 10209 10210 for (i = 0; i < v_budget; i++) 10211 pf->msix_entries[i].entry = i; 10212 v_actual = i40e_reserve_msix_vectors(pf, v_budget); 10213 10214 if (v_actual < I40E_MIN_MSIX) { 10215 pf->flags &= ~I40E_FLAG_MSIX_ENABLED; 10216 kfree(pf->msix_entries); 10217 pf->msix_entries = NULL; 10218 pci_disable_msix(pf->pdev); 10219 return -ENODEV; 10220 10221 } else if (v_actual == I40E_MIN_MSIX) { 10222 /* Adjust for minimal MSIX use */ 10223 pf->num_vmdq_vsis = 0; 10224 pf->num_vmdq_qps = 0; 10225 pf->num_lan_qps = 1; 10226 pf->num_lan_msix = 1; 10227 10228 } else if (v_actual != v_budget) { 10229 /* If we have limited resources, we will start with no vectors 10230 * for the special features and then allocate vectors to some 10231 * of these features based on the policy and at the end disable 10232 * the features that did not get any vectors. 10233 */ 10234 int vec; 10235 10236 dev_info(&pf->pdev->dev, 10237 "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n", 10238 v_actual, v_budget); 10239 /* reserve the misc vector */ 10240 vec = v_actual - 1; 10241 10242 /* Scale vector usage down */ 10243 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */ 10244 pf->num_vmdq_vsis = 1; 10245 pf->num_vmdq_qps = 1; 10246 10247 /* partition out the remaining vectors */ 10248 switch (vec) { 10249 case 2: 10250 pf->num_lan_msix = 1; 10251 break; 10252 case 3: 10253 if (pf->flags & I40E_FLAG_IWARP_ENABLED) { 10254 pf->num_lan_msix = 1; 10255 pf->num_iwarp_msix = 1; 10256 } else { 10257 pf->num_lan_msix = 2; 10258 } 10259 break; 10260 default: 10261 if (pf->flags & I40E_FLAG_IWARP_ENABLED) { 10262 pf->num_iwarp_msix = min_t(int, (vec / 3), 10263 iwarp_requested); 10264 pf->num_vmdq_vsis = min_t(int, (vec / 3), 10265 I40E_DEFAULT_NUM_VMDQ_VSI); 10266 } else { 10267 pf->num_vmdq_vsis = min_t(int, (vec / 2), 10268 I40E_DEFAULT_NUM_VMDQ_VSI); 10269 } 10270 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) { 10271 pf->num_fdsb_msix = 1; 10272 vec--; 10273 } 10274 pf->num_lan_msix = min_t(int, 10275 (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)), 10276 pf->num_lan_msix); 10277 pf->num_lan_qps = pf->num_lan_msix; 10278 break; 10279 } 10280 } 10281 10282 if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) && 10283 (pf->num_fdsb_msix == 0)) { 10284 dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n"); 10285 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED; 10286 pf->flags |= I40E_FLAG_FD_SB_INACTIVE; 10287 } 10288 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) && 10289 (pf->num_vmdq_msix == 0)) { 10290 dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n"); 10291 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED; 10292 } 10293 10294 if ((pf->flags & I40E_FLAG_IWARP_ENABLED) && 10295 (pf->num_iwarp_msix == 0)) { 10296 dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n"); 10297 pf->flags &= ~I40E_FLAG_IWARP_ENABLED; 10298 } 10299 i40e_debug(&pf->hw, I40E_DEBUG_INIT, 10300 "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n", 10301 pf->num_lan_msix, 10302 pf->num_vmdq_msix * pf->num_vmdq_vsis, 10303 pf->num_fdsb_msix, 10304 pf->num_iwarp_msix); 10305 10306 return v_actual; 10307 } 10308 10309 /** 10310 * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector 10311 * @vsi: the VSI being configured 10312 * @v_idx: index of the vector in the vsi struct 10313 * @cpu: cpu to be used on affinity_mask 10314 * 10315 * We allocate one q_vector. If allocation fails we return -ENOMEM. 10316 **/ 10317 static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx, int cpu) 10318 { 10319 struct i40e_q_vector *q_vector; 10320 10321 /* allocate q_vector */ 10322 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL); 10323 if (!q_vector) 10324 return -ENOMEM; 10325 10326 q_vector->vsi = vsi; 10327 q_vector->v_idx = v_idx; 10328 cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask); 10329 10330 if (vsi->netdev) 10331 netif_napi_add(vsi->netdev, &q_vector->napi, 10332 i40e_napi_poll, NAPI_POLL_WEIGHT); 10333 10334 /* tie q_vector and vsi together */ 10335 vsi->q_vectors[v_idx] = q_vector; 10336 10337 return 0; 10338 } 10339 10340 /** 10341 * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors 10342 * @vsi: the VSI being configured 10343 * 10344 * We allocate one q_vector per queue interrupt. If allocation fails we 10345 * return -ENOMEM. 10346 **/ 10347 static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi) 10348 { 10349 struct i40e_pf *pf = vsi->back; 10350 int err, v_idx, num_q_vectors, current_cpu; 10351 10352 /* if not MSIX, give the one vector only to the LAN VSI */ 10353 if (pf->flags & I40E_FLAG_MSIX_ENABLED) 10354 num_q_vectors = vsi->num_q_vectors; 10355 else if (vsi == pf->vsi[pf->lan_vsi]) 10356 num_q_vectors = 1; 10357 else 10358 return -EINVAL; 10359 10360 current_cpu = cpumask_first(cpu_online_mask); 10361 10362 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) { 10363 err = i40e_vsi_alloc_q_vector(vsi, v_idx, current_cpu); 10364 if (err) 10365 goto err_out; 10366 current_cpu = cpumask_next(current_cpu, cpu_online_mask); 10367 if (unlikely(current_cpu >= nr_cpu_ids)) 10368 current_cpu = cpumask_first(cpu_online_mask); 10369 } 10370 10371 return 0; 10372 10373 err_out: 10374 while (v_idx--) 10375 i40e_free_q_vector(vsi, v_idx); 10376 10377 return err; 10378 } 10379 10380 /** 10381 * i40e_init_interrupt_scheme - Determine proper interrupt scheme 10382 * @pf: board private structure to initialize 10383 **/ 10384 static int i40e_init_interrupt_scheme(struct i40e_pf *pf) 10385 { 10386 int vectors = 0; 10387 ssize_t size; 10388 10389 if (pf->flags & I40E_FLAG_MSIX_ENABLED) { 10390 vectors = i40e_init_msix(pf); 10391 if (vectors < 0) { 10392 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | 10393 I40E_FLAG_IWARP_ENABLED | 10394 I40E_FLAG_RSS_ENABLED | 10395 I40E_FLAG_DCB_CAPABLE | 10396 I40E_FLAG_DCB_ENABLED | 10397 I40E_FLAG_SRIOV_ENABLED | 10398 I40E_FLAG_FD_SB_ENABLED | 10399 I40E_FLAG_FD_ATR_ENABLED | 10400 I40E_FLAG_VMDQ_ENABLED); 10401 pf->flags |= I40E_FLAG_FD_SB_INACTIVE; 10402 10403 /* rework the queue expectations without MSIX */ 10404 i40e_determine_queue_usage(pf); 10405 } 10406 } 10407 10408 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) && 10409 (pf->flags & I40E_FLAG_MSI_ENABLED)) { 10410 dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n"); 10411 vectors = pci_enable_msi(pf->pdev); 10412 if (vectors < 0) { 10413 dev_info(&pf->pdev->dev, "MSI init failed - %d\n", 10414 vectors); 10415 pf->flags &= ~I40E_FLAG_MSI_ENABLED; 10416 } 10417 vectors = 1; /* one MSI or Legacy vector */ 10418 } 10419 10420 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED))) 10421 dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n"); 10422 10423 /* set up vector assignment tracking */ 10424 size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors); 10425 pf->irq_pile = kzalloc(size, GFP_KERNEL); 10426 if (!pf->irq_pile) 10427 return -ENOMEM; 10428 10429 pf->irq_pile->num_entries = vectors; 10430 pf->irq_pile->search_hint = 0; 10431 10432 /* track first vector for misc interrupts, ignore return */ 10433 (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1); 10434 10435 return 0; 10436 } 10437 10438 /** 10439 * i40e_restore_interrupt_scheme - Restore the interrupt scheme 10440 * @pf: private board data structure 10441 * 10442 * Restore the interrupt scheme that was cleared when we suspended the 10443 * device. This should be called during resume to re-allocate the q_vectors 10444 * and reacquire IRQs. 10445 */ 10446 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf) 10447 { 10448 int err, i; 10449 10450 /* We cleared the MSI and MSI-X flags when disabling the old interrupt 10451 * scheme. We need to re-enabled them here in order to attempt to 10452 * re-acquire the MSI or MSI-X vectors 10453 */ 10454 pf->flags |= (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED); 10455 10456 err = i40e_init_interrupt_scheme(pf); 10457 if (err) 10458 return err; 10459 10460 /* Now that we've re-acquired IRQs, we need to remap the vectors and 10461 * rings together again. 10462 */ 10463 for (i = 0; i < pf->num_alloc_vsi; i++) { 10464 if (pf->vsi[i]) { 10465 err = i40e_vsi_alloc_q_vectors(pf->vsi[i]); 10466 if (err) 10467 goto err_unwind; 10468 i40e_vsi_map_rings_to_vectors(pf->vsi[i]); 10469 } 10470 } 10471 10472 err = i40e_setup_misc_vector(pf); 10473 if (err) 10474 goto err_unwind; 10475 10476 return 0; 10477 10478 err_unwind: 10479 while (i--) { 10480 if (pf->vsi[i]) 10481 i40e_vsi_free_q_vectors(pf->vsi[i]); 10482 } 10483 10484 return err; 10485 } 10486 10487 /** 10488 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events 10489 * @pf: board private structure 10490 * 10491 * This sets up the handler for MSIX 0, which is used to manage the 10492 * non-queue interrupts, e.g. AdminQ and errors. This is not used 10493 * when in MSI or Legacy interrupt mode. 10494 **/ 10495 static int i40e_setup_misc_vector(struct i40e_pf *pf) 10496 { 10497 struct i40e_hw *hw = &pf->hw; 10498 int err = 0; 10499 10500 /* Only request the IRQ once, the first time through. */ 10501 if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) { 10502 err = request_irq(pf->msix_entries[0].vector, 10503 i40e_intr, 0, pf->int_name, pf); 10504 if (err) { 10505 clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state); 10506 dev_info(&pf->pdev->dev, 10507 "request_irq for %s failed: %d\n", 10508 pf->int_name, err); 10509 return -EFAULT; 10510 } 10511 } 10512 10513 i40e_enable_misc_int_causes(pf); 10514 10515 /* associate no queues to the misc vector */ 10516 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST); 10517 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K); 10518 10519 i40e_flush(hw); 10520 10521 i40e_irq_dynamic_enable_icr0(pf); 10522 10523 return err; 10524 } 10525 10526 /** 10527 * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands 10528 * @vsi: Pointer to vsi structure 10529 * @seed: Buffter to store the hash keys 10530 * @lut: Buffer to store the lookup table entries 10531 * @lut_size: Size of buffer to store the lookup table entries 10532 * 10533 * Return 0 on success, negative on failure 10534 */ 10535 static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed, 10536 u8 *lut, u16 lut_size) 10537 { 10538 struct i40e_pf *pf = vsi->back; 10539 struct i40e_hw *hw = &pf->hw; 10540 int ret = 0; 10541 10542 if (seed) { 10543 ret = i40e_aq_get_rss_key(hw, vsi->id, 10544 (struct i40e_aqc_get_set_rss_key_data *)seed); 10545 if (ret) { 10546 dev_info(&pf->pdev->dev, 10547 "Cannot get RSS key, err %s aq_err %s\n", 10548 i40e_stat_str(&pf->hw, ret), 10549 i40e_aq_str(&pf->hw, 10550 pf->hw.aq.asq_last_status)); 10551 return ret; 10552 } 10553 } 10554 10555 if (lut) { 10556 bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false; 10557 10558 ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size); 10559 if (ret) { 10560 dev_info(&pf->pdev->dev, 10561 "Cannot get RSS lut, err %s aq_err %s\n", 10562 i40e_stat_str(&pf->hw, ret), 10563 i40e_aq_str(&pf->hw, 10564 pf->hw.aq.asq_last_status)); 10565 return ret; 10566 } 10567 } 10568 10569 return ret; 10570 } 10571 10572 /** 10573 * i40e_config_rss_reg - Configure RSS keys and lut by writing registers 10574 * @vsi: Pointer to vsi structure 10575 * @seed: RSS hash seed 10576 * @lut: Lookup table 10577 * @lut_size: Lookup table size 10578 * 10579 * Returns 0 on success, negative on failure 10580 **/ 10581 static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed, 10582 const u8 *lut, u16 lut_size) 10583 { 10584 struct i40e_pf *pf = vsi->back; 10585 struct i40e_hw *hw = &pf->hw; 10586 u16 vf_id = vsi->vf_id; 10587 u8 i; 10588 10589 /* Fill out hash function seed */ 10590 if (seed) { 10591 u32 *seed_dw = (u32 *)seed; 10592 10593 if (vsi->type == I40E_VSI_MAIN) { 10594 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++) 10595 wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]); 10596 } else if (vsi->type == I40E_VSI_SRIOV) { 10597 for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++) 10598 wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]); 10599 } else { 10600 dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n"); 10601 } 10602 } 10603 10604 if (lut) { 10605 u32 *lut_dw = (u32 *)lut; 10606 10607 if (vsi->type == I40E_VSI_MAIN) { 10608 if (lut_size != I40E_HLUT_ARRAY_SIZE) 10609 return -EINVAL; 10610 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++) 10611 wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]); 10612 } else if (vsi->type == I40E_VSI_SRIOV) { 10613 if (lut_size != I40E_VF_HLUT_ARRAY_SIZE) 10614 return -EINVAL; 10615 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++) 10616 wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]); 10617 } else { 10618 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n"); 10619 } 10620 } 10621 i40e_flush(hw); 10622 10623 return 0; 10624 } 10625 10626 /** 10627 * i40e_get_rss_reg - Get the RSS keys and lut by reading registers 10628 * @vsi: Pointer to VSI structure 10629 * @seed: Buffer to store the keys 10630 * @lut: Buffer to store the lookup table entries 10631 * @lut_size: Size of buffer to store the lookup table entries 10632 * 10633 * Returns 0 on success, negative on failure 10634 */ 10635 static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed, 10636 u8 *lut, u16 lut_size) 10637 { 10638 struct i40e_pf *pf = vsi->back; 10639 struct i40e_hw *hw = &pf->hw; 10640 u16 i; 10641 10642 if (seed) { 10643 u32 *seed_dw = (u32 *)seed; 10644 10645 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++) 10646 seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i)); 10647 } 10648 if (lut) { 10649 u32 *lut_dw = (u32 *)lut; 10650 10651 if (lut_size != I40E_HLUT_ARRAY_SIZE) 10652 return -EINVAL; 10653 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++) 10654 lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i)); 10655 } 10656 10657 return 0; 10658 } 10659 10660 /** 10661 * i40e_config_rss - Configure RSS keys and lut 10662 * @vsi: Pointer to VSI structure 10663 * @seed: RSS hash seed 10664 * @lut: Lookup table 10665 * @lut_size: Lookup table size 10666 * 10667 * Returns 0 on success, negative on failure 10668 */ 10669 int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size) 10670 { 10671 struct i40e_pf *pf = vsi->back; 10672 10673 if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) 10674 return i40e_config_rss_aq(vsi, seed, lut, lut_size); 10675 else 10676 return i40e_config_rss_reg(vsi, seed, lut, lut_size); 10677 } 10678 10679 /** 10680 * i40e_get_rss - Get RSS keys and lut 10681 * @vsi: Pointer to VSI structure 10682 * @seed: Buffer to store the keys 10683 * @lut: Buffer to store the lookup table entries 10684 * lut_size: Size of buffer to store the lookup table entries 10685 * 10686 * Returns 0 on success, negative on failure 10687 */ 10688 int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size) 10689 { 10690 struct i40e_pf *pf = vsi->back; 10691 10692 if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) 10693 return i40e_get_rss_aq(vsi, seed, lut, lut_size); 10694 else 10695 return i40e_get_rss_reg(vsi, seed, lut, lut_size); 10696 } 10697 10698 /** 10699 * i40e_fill_rss_lut - Fill the RSS lookup table with default values 10700 * @pf: Pointer to board private structure 10701 * @lut: Lookup table 10702 * @rss_table_size: Lookup table size 10703 * @rss_size: Range of queue number for hashing 10704 */ 10705 void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut, 10706 u16 rss_table_size, u16 rss_size) 10707 { 10708 u16 i; 10709 10710 for (i = 0; i < rss_table_size; i++) 10711 lut[i] = i % rss_size; 10712 } 10713 10714 /** 10715 * i40e_pf_config_rss - Prepare for RSS if used 10716 * @pf: board private structure 10717 **/ 10718 static int i40e_pf_config_rss(struct i40e_pf *pf) 10719 { 10720 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi]; 10721 u8 seed[I40E_HKEY_ARRAY_SIZE]; 10722 u8 *lut; 10723 struct i40e_hw *hw = &pf->hw; 10724 u32 reg_val; 10725 u64 hena; 10726 int ret; 10727 10728 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */ 10729 hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) | 10730 ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32); 10731 hena |= i40e_pf_get_default_rss_hena(pf); 10732 10733 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena); 10734 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32)); 10735 10736 /* Determine the RSS table size based on the hardware capabilities */ 10737 reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0); 10738 reg_val = (pf->rss_table_size == 512) ? 10739 (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) : 10740 (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512); 10741 i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val); 10742 10743 /* Determine the RSS size of the VSI */ 10744 if (!vsi->rss_size) { 10745 u16 qcount; 10746 /* If the firmware does something weird during VSI init, we 10747 * could end up with zero TCs. Check for that to avoid 10748 * divide-by-zero. It probably won't pass traffic, but it also 10749 * won't panic. 10750 */ 10751 qcount = vsi->num_queue_pairs / 10752 (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1); 10753 vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount); 10754 } 10755 if (!vsi->rss_size) 10756 return -EINVAL; 10757 10758 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL); 10759 if (!lut) 10760 return -ENOMEM; 10761 10762 /* Use user configured lut if there is one, otherwise use default */ 10763 if (vsi->rss_lut_user) 10764 memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size); 10765 else 10766 i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size); 10767 10768 /* Use user configured hash key if there is one, otherwise 10769 * use default. 10770 */ 10771 if (vsi->rss_hkey_user) 10772 memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE); 10773 else 10774 netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE); 10775 ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size); 10776 kfree(lut); 10777 10778 return ret; 10779 } 10780 10781 /** 10782 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild 10783 * @pf: board private structure 10784 * @queue_count: the requested queue count for rss. 10785 * 10786 * returns 0 if rss is not enabled, if enabled returns the final rss queue 10787 * count which may be different from the requested queue count. 10788 * Note: expects to be called while under rtnl_lock() 10789 **/ 10790 int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count) 10791 { 10792 struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi]; 10793 int new_rss_size; 10794 10795 if (!(pf->flags & I40E_FLAG_RSS_ENABLED)) 10796 return 0; 10797 10798 new_rss_size = min_t(int, queue_count, pf->rss_size_max); 10799 10800 if (queue_count != vsi->num_queue_pairs) { 10801 u16 qcount; 10802 10803 vsi->req_queue_pairs = queue_count; 10804 i40e_prep_for_reset(pf, true); 10805 10806 pf->alloc_rss_size = new_rss_size; 10807 10808 i40e_reset_and_rebuild(pf, true, true); 10809 10810 /* Discard the user configured hash keys and lut, if less 10811 * queues are enabled. 10812 */ 10813 if (queue_count < vsi->rss_size) { 10814 i40e_clear_rss_config_user(vsi); 10815 dev_dbg(&pf->pdev->dev, 10816 "discard user configured hash keys and lut\n"); 10817 } 10818 10819 /* Reset vsi->rss_size, as number of enabled queues changed */ 10820 qcount = vsi->num_queue_pairs / vsi->tc_config.numtc; 10821 vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount); 10822 10823 i40e_pf_config_rss(pf); 10824 } 10825 dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count: %d/%d\n", 10826 vsi->req_queue_pairs, pf->rss_size_max); 10827 return pf->alloc_rss_size; 10828 } 10829 10830 /** 10831 * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition 10832 * @pf: board private structure 10833 **/ 10834 i40e_status i40e_get_partition_bw_setting(struct i40e_pf *pf) 10835 { 10836 i40e_status status; 10837 bool min_valid, max_valid; 10838 u32 max_bw, min_bw; 10839 10840 status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw, 10841 &min_valid, &max_valid); 10842 10843 if (!status) { 10844 if (min_valid) 10845 pf->min_bw = min_bw; 10846 if (max_valid) 10847 pf->max_bw = max_bw; 10848 } 10849 10850 return status; 10851 } 10852 10853 /** 10854 * i40e_set_partition_bw_setting - Set BW settings for this PF partition 10855 * @pf: board private structure 10856 **/ 10857 i40e_status i40e_set_partition_bw_setting(struct i40e_pf *pf) 10858 { 10859 struct i40e_aqc_configure_partition_bw_data bw_data; 10860 i40e_status status; 10861 10862 /* Set the valid bit for this PF */ 10863 bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id)); 10864 bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK; 10865 bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK; 10866 10867 /* Set the new bandwidths */ 10868 status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL); 10869 10870 return status; 10871 } 10872 10873 /** 10874 * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition 10875 * @pf: board private structure 10876 **/ 10877 i40e_status i40e_commit_partition_bw_setting(struct i40e_pf *pf) 10878 { 10879 /* Commit temporary BW setting to permanent NVM image */ 10880 enum i40e_admin_queue_err last_aq_status; 10881 i40e_status ret; 10882 u16 nvm_word; 10883 10884 if (pf->hw.partition_id != 1) { 10885 dev_info(&pf->pdev->dev, 10886 "Commit BW only works on partition 1! This is partition %d", 10887 pf->hw.partition_id); 10888 ret = I40E_NOT_SUPPORTED; 10889 goto bw_commit_out; 10890 } 10891 10892 /* Acquire NVM for read access */ 10893 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ); 10894 last_aq_status = pf->hw.aq.asq_last_status; 10895 if (ret) { 10896 dev_info(&pf->pdev->dev, 10897 "Cannot acquire NVM for read access, err %s aq_err %s\n", 10898 i40e_stat_str(&pf->hw, ret), 10899 i40e_aq_str(&pf->hw, last_aq_status)); 10900 goto bw_commit_out; 10901 } 10902 10903 /* Read word 0x10 of NVM - SW compatibility word 1 */ 10904 ret = i40e_aq_read_nvm(&pf->hw, 10905 I40E_SR_NVM_CONTROL_WORD, 10906 0x10, sizeof(nvm_word), &nvm_word, 10907 false, NULL); 10908 /* Save off last admin queue command status before releasing 10909 * the NVM 10910 */ 10911 last_aq_status = pf->hw.aq.asq_last_status; 10912 i40e_release_nvm(&pf->hw); 10913 if (ret) { 10914 dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n", 10915 i40e_stat_str(&pf->hw, ret), 10916 i40e_aq_str(&pf->hw, last_aq_status)); 10917 goto bw_commit_out; 10918 } 10919 10920 /* Wait a bit for NVM release to complete */ 10921 msleep(50); 10922 10923 /* Acquire NVM for write access */ 10924 ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE); 10925 last_aq_status = pf->hw.aq.asq_last_status; 10926 if (ret) { 10927 dev_info(&pf->pdev->dev, 10928 "Cannot acquire NVM for write access, err %s aq_err %s\n", 10929 i40e_stat_str(&pf->hw, ret), 10930 i40e_aq_str(&pf->hw, last_aq_status)); 10931 goto bw_commit_out; 10932 } 10933 /* Write it back out unchanged to initiate update NVM, 10934 * which will force a write of the shadow (alt) RAM to 10935 * the NVM - thus storing the bandwidth values permanently. 10936 */ 10937 ret = i40e_aq_update_nvm(&pf->hw, 10938 I40E_SR_NVM_CONTROL_WORD, 10939 0x10, sizeof(nvm_word), 10940 &nvm_word, true, 0, NULL); 10941 /* Save off last admin queue command status before releasing 10942 * the NVM 10943 */ 10944 last_aq_status = pf->hw.aq.asq_last_status; 10945 i40e_release_nvm(&pf->hw); 10946 if (ret) 10947 dev_info(&pf->pdev->dev, 10948 "BW settings NOT SAVED, err %s aq_err %s\n", 10949 i40e_stat_str(&pf->hw, ret), 10950 i40e_aq_str(&pf->hw, last_aq_status)); 10951 bw_commit_out: 10952 10953 return ret; 10954 } 10955 10956 /** 10957 * i40e_sw_init - Initialize general software structures (struct i40e_pf) 10958 * @pf: board private structure to initialize 10959 * 10960 * i40e_sw_init initializes the Adapter private data structure. 10961 * Fields are initialized based on PCI device information and 10962 * OS network device settings (MTU size). 10963 **/ 10964 static int i40e_sw_init(struct i40e_pf *pf) 10965 { 10966 int err = 0; 10967 int size; 10968 10969 /* Set default capability flags */ 10970 pf->flags = I40E_FLAG_RX_CSUM_ENABLED | 10971 I40E_FLAG_MSI_ENABLED | 10972 I40E_FLAG_MSIX_ENABLED; 10973 10974 /* Set default ITR */ 10975 pf->rx_itr_default = I40E_ITR_RX_DEF; 10976 pf->tx_itr_default = I40E_ITR_TX_DEF; 10977 10978 /* Depending on PF configurations, it is possible that the RSS 10979 * maximum might end up larger than the available queues 10980 */ 10981 pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width); 10982 pf->alloc_rss_size = 1; 10983 pf->rss_table_size = pf->hw.func_caps.rss_table_size; 10984 pf->rss_size_max = min_t(int, pf->rss_size_max, 10985 pf->hw.func_caps.num_tx_qp); 10986 if (pf->hw.func_caps.rss) { 10987 pf->flags |= I40E_FLAG_RSS_ENABLED; 10988 pf->alloc_rss_size = min_t(int, pf->rss_size_max, 10989 num_online_cpus()); 10990 } 10991 10992 /* MFP mode enabled */ 10993 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) { 10994 pf->flags |= I40E_FLAG_MFP_ENABLED; 10995 dev_info(&pf->pdev->dev, "MFP mode Enabled\n"); 10996 if (i40e_get_partition_bw_setting(pf)) { 10997 dev_warn(&pf->pdev->dev, 10998 "Could not get partition bw settings\n"); 10999 } else { 11000 dev_info(&pf->pdev->dev, 11001 "Partition BW Min = %8.8x, Max = %8.8x\n", 11002 pf->min_bw, pf->max_bw); 11003 11004 /* nudge the Tx scheduler */ 11005 i40e_set_partition_bw_setting(pf); 11006 } 11007 } 11008 11009 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) || 11010 (pf->hw.func_caps.fd_filters_best_effort > 0)) { 11011 pf->flags |= I40E_FLAG_FD_ATR_ENABLED; 11012 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE; 11013 if (pf->flags & I40E_FLAG_MFP_ENABLED && 11014 pf->hw.num_partitions > 1) 11015 dev_info(&pf->pdev->dev, 11016 "Flow Director Sideband mode Disabled in MFP mode\n"); 11017 else 11018 pf->flags |= I40E_FLAG_FD_SB_ENABLED; 11019 pf->fdir_pf_filter_count = 11020 pf->hw.func_caps.fd_filters_guaranteed; 11021 pf->hw.fdir_shared_filter_count = 11022 pf->hw.func_caps.fd_filters_best_effort; 11023 } 11024 11025 if (pf->hw.mac.type == I40E_MAC_X722) { 11026 pf->hw_features |= (I40E_HW_RSS_AQ_CAPABLE | 11027 I40E_HW_128_QP_RSS_CAPABLE | 11028 I40E_HW_ATR_EVICT_CAPABLE | 11029 I40E_HW_WB_ON_ITR_CAPABLE | 11030 I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE | 11031 I40E_HW_NO_PCI_LINK_CHECK | 11032 I40E_HW_USE_SET_LLDP_MIB | 11033 I40E_HW_GENEVE_OFFLOAD_CAPABLE | 11034 I40E_HW_PTP_L4_CAPABLE | 11035 I40E_HW_WOL_MC_MAGIC_PKT_WAKE | 11036 I40E_HW_OUTER_UDP_CSUM_CAPABLE); 11037 11038 #define I40E_FDEVICT_PCTYPE_DEFAULT 0xc03 11039 if (rd32(&pf->hw, I40E_GLQF_FDEVICTENA(1)) != 11040 I40E_FDEVICT_PCTYPE_DEFAULT) { 11041 dev_warn(&pf->pdev->dev, 11042 "FD EVICT PCTYPES are not right, disable FD HW EVICT\n"); 11043 pf->hw_features &= ~I40E_HW_ATR_EVICT_CAPABLE; 11044 } 11045 } else if ((pf->hw.aq.api_maj_ver > 1) || 11046 ((pf->hw.aq.api_maj_ver == 1) && 11047 (pf->hw.aq.api_min_ver > 4))) { 11048 /* Supported in FW API version higher than 1.4 */ 11049 pf->hw_features |= I40E_HW_GENEVE_OFFLOAD_CAPABLE; 11050 } 11051 11052 /* Enable HW ATR eviction if possible */ 11053 if (pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE) 11054 pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED; 11055 11056 if ((pf->hw.mac.type == I40E_MAC_XL710) && 11057 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) || 11058 (pf->hw.aq.fw_maj_ver < 4))) { 11059 pf->hw_features |= I40E_HW_RESTART_AUTONEG; 11060 /* No DCB support for FW < v4.33 */ 11061 pf->hw_features |= I40E_HW_NO_DCB_SUPPORT; 11062 } 11063 11064 /* Disable FW LLDP if FW < v4.3 */ 11065 if ((pf->hw.mac.type == I40E_MAC_XL710) && 11066 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) || 11067 (pf->hw.aq.fw_maj_ver < 4))) 11068 pf->hw_features |= I40E_HW_STOP_FW_LLDP; 11069 11070 /* Use the FW Set LLDP MIB API if FW > v4.40 */ 11071 if ((pf->hw.mac.type == I40E_MAC_XL710) && 11072 (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) || 11073 (pf->hw.aq.fw_maj_ver >= 5))) 11074 pf->hw_features |= I40E_HW_USE_SET_LLDP_MIB; 11075 11076 /* Enable PTP L4 if FW > v6.0 */ 11077 if (pf->hw.mac.type == I40E_MAC_XL710 && 11078 pf->hw.aq.fw_maj_ver >= 6) 11079 pf->hw_features |= I40E_HW_PTP_L4_CAPABLE; 11080 11081 if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) { 11082 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI; 11083 pf->flags |= I40E_FLAG_VMDQ_ENABLED; 11084 pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf); 11085 } 11086 11087 if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) { 11088 pf->flags |= I40E_FLAG_IWARP_ENABLED; 11089 /* IWARP needs one extra vector for CQP just like MISC.*/ 11090 pf->num_iwarp_msix = (int)num_online_cpus() + 1; 11091 } 11092 /* Stopping the FW LLDP engine is only supported on the 11093 * XL710 with a FW ver >= 1.7. Also, stopping FW LLDP 11094 * engine is not supported if NPAR is functioning on this 11095 * part 11096 */ 11097 if (pf->hw.mac.type == I40E_MAC_XL710 && 11098 !pf->hw.func_caps.npar_enable && 11099 (pf->hw.aq.api_maj_ver > 1 || 11100 (pf->hw.aq.api_maj_ver == 1 && pf->hw.aq.api_min_ver > 6))) 11101 pf->hw_features |= I40E_HW_STOPPABLE_FW_LLDP; 11102 11103 #ifdef CONFIG_PCI_IOV 11104 if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) { 11105 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF; 11106 pf->flags |= I40E_FLAG_SRIOV_ENABLED; 11107 pf->num_req_vfs = min_t(int, 11108 pf->hw.func_caps.num_vfs, 11109 I40E_MAX_VF_COUNT); 11110 } 11111 #endif /* CONFIG_PCI_IOV */ 11112 pf->eeprom_version = 0xDEAD; 11113 pf->lan_veb = I40E_NO_VEB; 11114 pf->lan_vsi = I40E_NO_VSI; 11115 11116 /* By default FW has this off for performance reasons */ 11117 pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED; 11118 11119 /* set up queue assignment tracking */ 11120 size = sizeof(struct i40e_lump_tracking) 11121 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp); 11122 pf->qp_pile = kzalloc(size, GFP_KERNEL); 11123 if (!pf->qp_pile) { 11124 err = -ENOMEM; 11125 goto sw_init_done; 11126 } 11127 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp; 11128 pf->qp_pile->search_hint = 0; 11129 11130 pf->tx_timeout_recovery_level = 1; 11131 11132 mutex_init(&pf->switch_mutex); 11133 11134 sw_init_done: 11135 return err; 11136 } 11137 11138 /** 11139 * i40e_set_ntuple - set the ntuple feature flag and take action 11140 * @pf: board private structure to initialize 11141 * @features: the feature set that the stack is suggesting 11142 * 11143 * returns a bool to indicate if reset needs to happen 11144 **/ 11145 bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features) 11146 { 11147 bool need_reset = false; 11148 11149 /* Check if Flow Director n-tuple support was enabled or disabled. If 11150 * the state changed, we need to reset. 11151 */ 11152 if (features & NETIF_F_NTUPLE) { 11153 /* Enable filters and mark for reset */ 11154 if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED)) 11155 need_reset = true; 11156 /* enable FD_SB only if there is MSI-X vector and no cloud 11157 * filters exist 11158 */ 11159 if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) { 11160 pf->flags |= I40E_FLAG_FD_SB_ENABLED; 11161 pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE; 11162 } 11163 } else { 11164 /* turn off filters, mark for reset and clear SW filter list */ 11165 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) { 11166 need_reset = true; 11167 i40e_fdir_filter_exit(pf); 11168 } 11169 pf->flags &= ~(I40E_FLAG_FD_SB_ENABLED | 11170 I40E_FLAG_FD_SB_AUTO_DISABLED); 11171 pf->flags |= I40E_FLAG_FD_SB_INACTIVE; 11172 11173 /* reset fd counters */ 11174 pf->fd_add_err = 0; 11175 pf->fd_atr_cnt = 0; 11176 /* if ATR was auto disabled it can be re-enabled. */ 11177 if (pf->flags & I40E_FLAG_FD_ATR_AUTO_DISABLED) { 11178 pf->flags &= ~I40E_FLAG_FD_ATR_AUTO_DISABLED; 11179 if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) && 11180 (I40E_DEBUG_FD & pf->hw.debug_mask)) 11181 dev_info(&pf->pdev->dev, "ATR re-enabled.\n"); 11182 } 11183 } 11184 return need_reset; 11185 } 11186 11187 /** 11188 * i40e_clear_rss_lut - clear the rx hash lookup table 11189 * @vsi: the VSI being configured 11190 **/ 11191 static void i40e_clear_rss_lut(struct i40e_vsi *vsi) 11192 { 11193 struct i40e_pf *pf = vsi->back; 11194 struct i40e_hw *hw = &pf->hw; 11195 u16 vf_id = vsi->vf_id; 11196 u8 i; 11197 11198 if (vsi->type == I40E_VSI_MAIN) { 11199 for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++) 11200 wr32(hw, I40E_PFQF_HLUT(i), 0); 11201 } else if (vsi->type == I40E_VSI_SRIOV) { 11202 for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++) 11203 i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0); 11204 } else { 11205 dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n"); 11206 } 11207 } 11208 11209 /** 11210 * i40e_set_features - set the netdev feature flags 11211 * @netdev: ptr to the netdev being adjusted 11212 * @features: the feature set that the stack is suggesting 11213 * Note: expects to be called while under rtnl_lock() 11214 **/ 11215 static int i40e_set_features(struct net_device *netdev, 11216 netdev_features_t features) 11217 { 11218 struct i40e_netdev_priv *np = netdev_priv(netdev); 11219 struct i40e_vsi *vsi = np->vsi; 11220 struct i40e_pf *pf = vsi->back; 11221 bool need_reset; 11222 11223 if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH)) 11224 i40e_pf_config_rss(pf); 11225 else if (!(features & NETIF_F_RXHASH) && 11226 netdev->features & NETIF_F_RXHASH) 11227 i40e_clear_rss_lut(vsi); 11228 11229 if (features & NETIF_F_HW_VLAN_CTAG_RX) 11230 i40e_vlan_stripping_enable(vsi); 11231 else 11232 i40e_vlan_stripping_disable(vsi); 11233 11234 if (!(features & NETIF_F_HW_TC) && pf->num_cloud_filters) { 11235 dev_err(&pf->pdev->dev, 11236 "Offloaded tc filters active, can't turn hw_tc_offload off"); 11237 return -EINVAL; 11238 } 11239 11240 need_reset = i40e_set_ntuple(pf, features); 11241 11242 if (need_reset) 11243 i40e_do_reset(pf, I40E_PF_RESET_FLAG, true); 11244 11245 return 0; 11246 } 11247 11248 /** 11249 * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port 11250 * @pf: board private structure 11251 * @port: The UDP port to look up 11252 * 11253 * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found 11254 **/ 11255 static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, u16 port) 11256 { 11257 u8 i; 11258 11259 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) { 11260 if (pf->udp_ports[i].port == port) 11261 return i; 11262 } 11263 11264 return i; 11265 } 11266 11267 /** 11268 * i40e_udp_tunnel_add - Get notifications about UDP tunnel ports that come up 11269 * @netdev: This physical port's netdev 11270 * @ti: Tunnel endpoint information 11271 **/ 11272 static void i40e_udp_tunnel_add(struct net_device *netdev, 11273 struct udp_tunnel_info *ti) 11274 { 11275 struct i40e_netdev_priv *np = netdev_priv(netdev); 11276 struct i40e_vsi *vsi = np->vsi; 11277 struct i40e_pf *pf = vsi->back; 11278 u16 port = ntohs(ti->port); 11279 u8 next_idx; 11280 u8 idx; 11281 11282 idx = i40e_get_udp_port_idx(pf, port); 11283 11284 /* Check if port already exists */ 11285 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) { 11286 netdev_info(netdev, "port %d already offloaded\n", port); 11287 return; 11288 } 11289 11290 /* Now check if there is space to add the new port */ 11291 next_idx = i40e_get_udp_port_idx(pf, 0); 11292 11293 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) { 11294 netdev_info(netdev, "maximum number of offloaded UDP ports reached, not adding port %d\n", 11295 port); 11296 return; 11297 } 11298 11299 switch (ti->type) { 11300 case UDP_TUNNEL_TYPE_VXLAN: 11301 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN; 11302 break; 11303 case UDP_TUNNEL_TYPE_GENEVE: 11304 if (!(pf->hw_features & I40E_HW_GENEVE_OFFLOAD_CAPABLE)) 11305 return; 11306 pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE; 11307 break; 11308 default: 11309 return; 11310 } 11311 11312 /* New port: add it and mark its index in the bitmap */ 11313 pf->udp_ports[next_idx].port = port; 11314 pf->pending_udp_bitmap |= BIT_ULL(next_idx); 11315 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC; 11316 } 11317 11318 /** 11319 * i40e_udp_tunnel_del - Get notifications about UDP tunnel ports that go away 11320 * @netdev: This physical port's netdev 11321 * @ti: Tunnel endpoint information 11322 **/ 11323 static void i40e_udp_tunnel_del(struct net_device *netdev, 11324 struct udp_tunnel_info *ti) 11325 { 11326 struct i40e_netdev_priv *np = netdev_priv(netdev); 11327 struct i40e_vsi *vsi = np->vsi; 11328 struct i40e_pf *pf = vsi->back; 11329 u16 port = ntohs(ti->port); 11330 u8 idx; 11331 11332 idx = i40e_get_udp_port_idx(pf, port); 11333 11334 /* Check if port already exists */ 11335 if (idx >= I40E_MAX_PF_UDP_OFFLOAD_PORTS) 11336 goto not_found; 11337 11338 switch (ti->type) { 11339 case UDP_TUNNEL_TYPE_VXLAN: 11340 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_VXLAN) 11341 goto not_found; 11342 break; 11343 case UDP_TUNNEL_TYPE_GENEVE: 11344 if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_NGE) 11345 goto not_found; 11346 break; 11347 default: 11348 goto not_found; 11349 } 11350 11351 /* if port exists, set it to 0 (mark for deletion) 11352 * and make it pending 11353 */ 11354 pf->udp_ports[idx].port = 0; 11355 pf->pending_udp_bitmap |= BIT_ULL(idx); 11356 pf->flags |= I40E_FLAG_UDP_FILTER_SYNC; 11357 11358 return; 11359 not_found: 11360 netdev_warn(netdev, "UDP port %d was not found, not deleting\n", 11361 port); 11362 } 11363 11364 static int i40e_get_phys_port_id(struct net_device *netdev, 11365 struct netdev_phys_item_id *ppid) 11366 { 11367 struct i40e_netdev_priv *np = netdev_priv(netdev); 11368 struct i40e_pf *pf = np->vsi->back; 11369 struct i40e_hw *hw = &pf->hw; 11370 11371 if (!(pf->hw_features & I40E_HW_PORT_ID_VALID)) 11372 return -EOPNOTSUPP; 11373 11374 ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id)); 11375 memcpy(ppid->id, hw->mac.port_addr, ppid->id_len); 11376 11377 return 0; 11378 } 11379 11380 /** 11381 * i40e_ndo_fdb_add - add an entry to the hardware database 11382 * @ndm: the input from the stack 11383 * @tb: pointer to array of nladdr (unused) 11384 * @dev: the net device pointer 11385 * @addr: the MAC address entry being added 11386 * @flags: instructions from stack about fdb operation 11387 */ 11388 static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[], 11389 struct net_device *dev, 11390 const unsigned char *addr, u16 vid, 11391 u16 flags) 11392 { 11393 struct i40e_netdev_priv *np = netdev_priv(dev); 11394 struct i40e_pf *pf = np->vsi->back; 11395 int err = 0; 11396 11397 if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED)) 11398 return -EOPNOTSUPP; 11399 11400 if (vid) { 11401 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name); 11402 return -EINVAL; 11403 } 11404 11405 /* Hardware does not support aging addresses so if a 11406 * ndm_state is given only allow permanent addresses 11407 */ 11408 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) { 11409 netdev_info(dev, "FDB only supports static addresses\n"); 11410 return -EINVAL; 11411 } 11412 11413 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) 11414 err = dev_uc_add_excl(dev, addr); 11415 else if (is_multicast_ether_addr(addr)) 11416 err = dev_mc_add_excl(dev, addr); 11417 else 11418 err = -EINVAL; 11419 11420 /* Only return duplicate errors if NLM_F_EXCL is set */ 11421 if (err == -EEXIST && !(flags & NLM_F_EXCL)) 11422 err = 0; 11423 11424 return err; 11425 } 11426 11427 /** 11428 * i40e_ndo_bridge_setlink - Set the hardware bridge mode 11429 * @dev: the netdev being configured 11430 * @nlh: RTNL message 11431 * 11432 * Inserts a new hardware bridge if not already created and 11433 * enables the bridging mode requested (VEB or VEPA). If the 11434 * hardware bridge has already been inserted and the request 11435 * is to change the mode then that requires a PF reset to 11436 * allow rebuild of the components with required hardware 11437 * bridge mode enabled. 11438 * 11439 * Note: expects to be called while under rtnl_lock() 11440 **/ 11441 static int i40e_ndo_bridge_setlink(struct net_device *dev, 11442 struct nlmsghdr *nlh, 11443 u16 flags) 11444 { 11445 struct i40e_netdev_priv *np = netdev_priv(dev); 11446 struct i40e_vsi *vsi = np->vsi; 11447 struct i40e_pf *pf = vsi->back; 11448 struct i40e_veb *veb = NULL; 11449 struct nlattr *attr, *br_spec; 11450 int i, rem; 11451 11452 /* Only for PF VSI for now */ 11453 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) 11454 return -EOPNOTSUPP; 11455 11456 /* Find the HW bridge for PF VSI */ 11457 for (i = 0; i < I40E_MAX_VEB && !veb; i++) { 11458 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid) 11459 veb = pf->veb[i]; 11460 } 11461 11462 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 11463 11464 nla_for_each_nested(attr, br_spec, rem) { 11465 __u16 mode; 11466 11467 if (nla_type(attr) != IFLA_BRIDGE_MODE) 11468 continue; 11469 11470 mode = nla_get_u16(attr); 11471 if ((mode != BRIDGE_MODE_VEPA) && 11472 (mode != BRIDGE_MODE_VEB)) 11473 return -EINVAL; 11474 11475 /* Insert a new HW bridge */ 11476 if (!veb) { 11477 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid, 11478 vsi->tc_config.enabled_tc); 11479 if (veb) { 11480 veb->bridge_mode = mode; 11481 i40e_config_bridge_mode(veb); 11482 } else { 11483 /* No Bridge HW offload available */ 11484 return -ENOENT; 11485 } 11486 break; 11487 } else if (mode != veb->bridge_mode) { 11488 /* Existing HW bridge but different mode needs reset */ 11489 veb->bridge_mode = mode; 11490 /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */ 11491 if (mode == BRIDGE_MODE_VEB) 11492 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED; 11493 else 11494 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED; 11495 i40e_do_reset(pf, I40E_PF_RESET_FLAG, true); 11496 break; 11497 } 11498 } 11499 11500 return 0; 11501 } 11502 11503 /** 11504 * i40e_ndo_bridge_getlink - Get the hardware bridge mode 11505 * @skb: skb buff 11506 * @pid: process id 11507 * @seq: RTNL message seq # 11508 * @dev: the netdev being configured 11509 * @filter_mask: unused 11510 * @nlflags: netlink flags passed in 11511 * 11512 * Return the mode in which the hardware bridge is operating in 11513 * i.e VEB or VEPA. 11514 **/ 11515 static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq, 11516 struct net_device *dev, 11517 u32 __always_unused filter_mask, 11518 int nlflags) 11519 { 11520 struct i40e_netdev_priv *np = netdev_priv(dev); 11521 struct i40e_vsi *vsi = np->vsi; 11522 struct i40e_pf *pf = vsi->back; 11523 struct i40e_veb *veb = NULL; 11524 int i; 11525 11526 /* Only for PF VSI for now */ 11527 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) 11528 return -EOPNOTSUPP; 11529 11530 /* Find the HW bridge for the PF VSI */ 11531 for (i = 0; i < I40E_MAX_VEB && !veb; i++) { 11532 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid) 11533 veb = pf->veb[i]; 11534 } 11535 11536 if (!veb) 11537 return 0; 11538 11539 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode, 11540 0, 0, nlflags, filter_mask, NULL); 11541 } 11542 11543 /** 11544 * i40e_features_check - Validate encapsulated packet conforms to limits 11545 * @skb: skb buff 11546 * @dev: This physical port's netdev 11547 * @features: Offload features that the stack believes apply 11548 **/ 11549 static netdev_features_t i40e_features_check(struct sk_buff *skb, 11550 struct net_device *dev, 11551 netdev_features_t features) 11552 { 11553 size_t len; 11554 11555 /* No point in doing any of this if neither checksum nor GSO are 11556 * being requested for this frame. We can rule out both by just 11557 * checking for CHECKSUM_PARTIAL 11558 */ 11559 if (skb->ip_summed != CHECKSUM_PARTIAL) 11560 return features; 11561 11562 /* We cannot support GSO if the MSS is going to be less than 11563 * 64 bytes. If it is then we need to drop support for GSO. 11564 */ 11565 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64)) 11566 features &= ~NETIF_F_GSO_MASK; 11567 11568 /* MACLEN can support at most 63 words */ 11569 len = skb_network_header(skb) - skb->data; 11570 if (len & ~(63 * 2)) 11571 goto out_err; 11572 11573 /* IPLEN and EIPLEN can support at most 127 dwords */ 11574 len = skb_transport_header(skb) - skb_network_header(skb); 11575 if (len & ~(127 * 4)) 11576 goto out_err; 11577 11578 if (skb->encapsulation) { 11579 /* L4TUNLEN can support 127 words */ 11580 len = skb_inner_network_header(skb) - skb_transport_header(skb); 11581 if (len & ~(127 * 2)) 11582 goto out_err; 11583 11584 /* IPLEN can support at most 127 dwords */ 11585 len = skb_inner_transport_header(skb) - 11586 skb_inner_network_header(skb); 11587 if (len & ~(127 * 4)) 11588 goto out_err; 11589 } 11590 11591 /* No need to validate L4LEN as TCP is the only protocol with a 11592 * a flexible value and we support all possible values supported 11593 * by TCP, which is at most 15 dwords 11594 */ 11595 11596 return features; 11597 out_err: 11598 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK); 11599 } 11600 11601 /** 11602 * i40e_xdp_setup - add/remove an XDP program 11603 * @vsi: VSI to changed 11604 * @prog: XDP program 11605 **/ 11606 static int i40e_xdp_setup(struct i40e_vsi *vsi, 11607 struct bpf_prog *prog) 11608 { 11609 int frame_size = vsi->netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN; 11610 struct i40e_pf *pf = vsi->back; 11611 struct bpf_prog *old_prog; 11612 bool need_reset; 11613 int i; 11614 11615 /* Don't allow frames that span over multiple buffers */ 11616 if (frame_size > vsi->rx_buf_len) 11617 return -EINVAL; 11618 11619 if (!i40e_enabled_xdp_vsi(vsi) && !prog) 11620 return 0; 11621 11622 /* When turning XDP on->off/off->on we reset and rebuild the rings. */ 11623 need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog); 11624 11625 if (need_reset) 11626 i40e_prep_for_reset(pf, true); 11627 11628 old_prog = xchg(&vsi->xdp_prog, prog); 11629 11630 if (need_reset) 11631 i40e_reset_and_rebuild(pf, true, true); 11632 11633 for (i = 0; i < vsi->num_queue_pairs; i++) 11634 WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog); 11635 11636 if (old_prog) 11637 bpf_prog_put(old_prog); 11638 11639 return 0; 11640 } 11641 11642 /** 11643 * i40e_xdp - implements ndo_bpf for i40e 11644 * @dev: netdevice 11645 * @xdp: XDP command 11646 **/ 11647 static int i40e_xdp(struct net_device *dev, 11648 struct netdev_bpf *xdp) 11649 { 11650 struct i40e_netdev_priv *np = netdev_priv(dev); 11651 struct i40e_vsi *vsi = np->vsi; 11652 11653 if (vsi->type != I40E_VSI_MAIN) 11654 return -EINVAL; 11655 11656 switch (xdp->command) { 11657 case XDP_SETUP_PROG: 11658 return i40e_xdp_setup(vsi, xdp->prog); 11659 case XDP_QUERY_PROG: 11660 xdp->prog_attached = i40e_enabled_xdp_vsi(vsi); 11661 xdp->prog_id = vsi->xdp_prog ? vsi->xdp_prog->aux->id : 0; 11662 return 0; 11663 default: 11664 return -EINVAL; 11665 } 11666 } 11667 11668 static const struct net_device_ops i40e_netdev_ops = { 11669 .ndo_open = i40e_open, 11670 .ndo_stop = i40e_close, 11671 .ndo_start_xmit = i40e_lan_xmit_frame, 11672 .ndo_get_stats64 = i40e_get_netdev_stats_struct, 11673 .ndo_set_rx_mode = i40e_set_rx_mode, 11674 .ndo_validate_addr = eth_validate_addr, 11675 .ndo_set_mac_address = i40e_set_mac, 11676 .ndo_change_mtu = i40e_change_mtu, 11677 .ndo_do_ioctl = i40e_ioctl, 11678 .ndo_tx_timeout = i40e_tx_timeout, 11679 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid, 11680 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid, 11681 #ifdef CONFIG_NET_POLL_CONTROLLER 11682 .ndo_poll_controller = i40e_netpoll, 11683 #endif 11684 .ndo_setup_tc = __i40e_setup_tc, 11685 .ndo_set_features = i40e_set_features, 11686 .ndo_set_vf_mac = i40e_ndo_set_vf_mac, 11687 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan, 11688 .ndo_set_vf_rate = i40e_ndo_set_vf_bw, 11689 .ndo_get_vf_config = i40e_ndo_get_vf_config, 11690 .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state, 11691 .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk, 11692 .ndo_set_vf_trust = i40e_ndo_set_vf_trust, 11693 .ndo_udp_tunnel_add = i40e_udp_tunnel_add, 11694 .ndo_udp_tunnel_del = i40e_udp_tunnel_del, 11695 .ndo_get_phys_port_id = i40e_get_phys_port_id, 11696 .ndo_fdb_add = i40e_ndo_fdb_add, 11697 .ndo_features_check = i40e_features_check, 11698 .ndo_bridge_getlink = i40e_ndo_bridge_getlink, 11699 .ndo_bridge_setlink = i40e_ndo_bridge_setlink, 11700 .ndo_bpf = i40e_xdp, 11701 }; 11702 11703 /** 11704 * i40e_config_netdev - Setup the netdev flags 11705 * @vsi: the VSI being configured 11706 * 11707 * Returns 0 on success, negative value on failure 11708 **/ 11709 static int i40e_config_netdev(struct i40e_vsi *vsi) 11710 { 11711 struct i40e_pf *pf = vsi->back; 11712 struct i40e_hw *hw = &pf->hw; 11713 struct i40e_netdev_priv *np; 11714 struct net_device *netdev; 11715 u8 broadcast[ETH_ALEN]; 11716 u8 mac_addr[ETH_ALEN]; 11717 int etherdev_size; 11718 netdev_features_t hw_enc_features; 11719 netdev_features_t hw_features; 11720 11721 etherdev_size = sizeof(struct i40e_netdev_priv); 11722 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs); 11723 if (!netdev) 11724 return -ENOMEM; 11725 11726 vsi->netdev = netdev; 11727 np = netdev_priv(netdev); 11728 np->vsi = vsi; 11729 11730 hw_enc_features = NETIF_F_SG | 11731 NETIF_F_IP_CSUM | 11732 NETIF_F_IPV6_CSUM | 11733 NETIF_F_HIGHDMA | 11734 NETIF_F_SOFT_FEATURES | 11735 NETIF_F_TSO | 11736 NETIF_F_TSO_ECN | 11737 NETIF_F_TSO6 | 11738 NETIF_F_GSO_GRE | 11739 NETIF_F_GSO_GRE_CSUM | 11740 NETIF_F_GSO_PARTIAL | 11741 NETIF_F_GSO_UDP_TUNNEL | 11742 NETIF_F_GSO_UDP_TUNNEL_CSUM | 11743 NETIF_F_SCTP_CRC | 11744 NETIF_F_RXHASH | 11745 NETIF_F_RXCSUM | 11746 0; 11747 11748 if (!(pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE)) 11749 netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM; 11750 11751 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM; 11752 11753 netdev->hw_enc_features |= hw_enc_features; 11754 11755 /* record features VLANs can make use of */ 11756 netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID; 11757 11758 if (!(pf->flags & I40E_FLAG_MFP_ENABLED)) 11759 netdev->hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC; 11760 11761 hw_features = hw_enc_features | 11762 NETIF_F_HW_VLAN_CTAG_TX | 11763 NETIF_F_HW_VLAN_CTAG_RX; 11764 11765 netdev->hw_features |= hw_features; 11766 11767 netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER; 11768 netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID; 11769 11770 if (vsi->type == I40E_VSI_MAIN) { 11771 SET_NETDEV_DEV(netdev, &pf->pdev->dev); 11772 ether_addr_copy(mac_addr, hw->mac.perm_addr); 11773 /* The following steps are necessary for two reasons. First, 11774 * some older NVM configurations load a default MAC-VLAN 11775 * filter that will accept any tagged packet, and we want to 11776 * replace this with a normal filter. Additionally, it is 11777 * possible our MAC address was provided by the platform using 11778 * Open Firmware or similar. 11779 * 11780 * Thus, we need to remove the default filter and install one 11781 * specific to the MAC address. 11782 */ 11783 i40e_rm_default_mac_filter(vsi, mac_addr); 11784 spin_lock_bh(&vsi->mac_filter_hash_lock); 11785 i40e_add_mac_filter(vsi, mac_addr); 11786 spin_unlock_bh(&vsi->mac_filter_hash_lock); 11787 } else { 11788 /* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we 11789 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to 11790 * the end, which is 4 bytes long, so force truncation of the 11791 * original name by IFNAMSIZ - 4 11792 */ 11793 snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d", 11794 IFNAMSIZ - 4, 11795 pf->vsi[pf->lan_vsi]->netdev->name); 11796 random_ether_addr(mac_addr); 11797 11798 spin_lock_bh(&vsi->mac_filter_hash_lock); 11799 i40e_add_mac_filter(vsi, mac_addr); 11800 spin_unlock_bh(&vsi->mac_filter_hash_lock); 11801 } 11802 11803 /* Add the broadcast filter so that we initially will receive 11804 * broadcast packets. Note that when a new VLAN is first added the 11805 * driver will convert all filters marked I40E_VLAN_ANY into VLAN 11806 * specific filters as part of transitioning into "vlan" operation. 11807 * When more VLANs are added, the driver will copy each existing MAC 11808 * filter and add it for the new VLAN. 11809 * 11810 * Broadcast filters are handled specially by 11811 * i40e_sync_filters_subtask, as the driver must to set the broadcast 11812 * promiscuous bit instead of adding this directly as a MAC/VLAN 11813 * filter. The subtask will update the correct broadcast promiscuous 11814 * bits as VLANs become active or inactive. 11815 */ 11816 eth_broadcast_addr(broadcast); 11817 spin_lock_bh(&vsi->mac_filter_hash_lock); 11818 i40e_add_mac_filter(vsi, broadcast); 11819 spin_unlock_bh(&vsi->mac_filter_hash_lock); 11820 11821 ether_addr_copy(netdev->dev_addr, mac_addr); 11822 ether_addr_copy(netdev->perm_addr, mac_addr); 11823 11824 netdev->priv_flags |= IFF_UNICAST_FLT; 11825 netdev->priv_flags |= IFF_SUPP_NOFCS; 11826 /* Setup netdev TC information */ 11827 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc); 11828 11829 netdev->netdev_ops = &i40e_netdev_ops; 11830 netdev->watchdog_timeo = 5 * HZ; 11831 i40e_set_ethtool_ops(netdev); 11832 11833 /* MTU range: 68 - 9706 */ 11834 netdev->min_mtu = ETH_MIN_MTU; 11835 netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD; 11836 11837 return 0; 11838 } 11839 11840 /** 11841 * i40e_vsi_delete - Delete a VSI from the switch 11842 * @vsi: the VSI being removed 11843 * 11844 * Returns 0 on success, negative value on failure 11845 **/ 11846 static void i40e_vsi_delete(struct i40e_vsi *vsi) 11847 { 11848 /* remove default VSI is not allowed */ 11849 if (vsi == vsi->back->vsi[vsi->back->lan_vsi]) 11850 return; 11851 11852 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL); 11853 } 11854 11855 /** 11856 * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB 11857 * @vsi: the VSI being queried 11858 * 11859 * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode 11860 **/ 11861 int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi) 11862 { 11863 struct i40e_veb *veb; 11864 struct i40e_pf *pf = vsi->back; 11865 11866 /* Uplink is not a bridge so default to VEB */ 11867 if (vsi->veb_idx == I40E_NO_VEB) 11868 return 1; 11869 11870 veb = pf->veb[vsi->veb_idx]; 11871 if (!veb) { 11872 dev_info(&pf->pdev->dev, 11873 "There is no veb associated with the bridge\n"); 11874 return -ENOENT; 11875 } 11876 11877 /* Uplink is a bridge in VEPA mode */ 11878 if (veb->bridge_mode & BRIDGE_MODE_VEPA) { 11879 return 0; 11880 } else { 11881 /* Uplink is a bridge in VEB mode */ 11882 return 1; 11883 } 11884 11885 /* VEPA is now default bridge, so return 0 */ 11886 return 0; 11887 } 11888 11889 /** 11890 * i40e_add_vsi - Add a VSI to the switch 11891 * @vsi: the VSI being configured 11892 * 11893 * This initializes a VSI context depending on the VSI type to be added and 11894 * passes it down to the add_vsi aq command. 11895 **/ 11896 static int i40e_add_vsi(struct i40e_vsi *vsi) 11897 { 11898 int ret = -ENODEV; 11899 struct i40e_pf *pf = vsi->back; 11900 struct i40e_hw *hw = &pf->hw; 11901 struct i40e_vsi_context ctxt; 11902 struct i40e_mac_filter *f; 11903 struct hlist_node *h; 11904 int bkt; 11905 11906 u8 enabled_tc = 0x1; /* TC0 enabled */ 11907 int f_count = 0; 11908 11909 memset(&ctxt, 0, sizeof(ctxt)); 11910 switch (vsi->type) { 11911 case I40E_VSI_MAIN: 11912 /* The PF's main VSI is already setup as part of the 11913 * device initialization, so we'll not bother with 11914 * the add_vsi call, but we will retrieve the current 11915 * VSI context. 11916 */ 11917 ctxt.seid = pf->main_vsi_seid; 11918 ctxt.pf_num = pf->hw.pf_id; 11919 ctxt.vf_num = 0; 11920 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL); 11921 ctxt.flags = I40E_AQ_VSI_TYPE_PF; 11922 if (ret) { 11923 dev_info(&pf->pdev->dev, 11924 "couldn't get PF vsi config, err %s aq_err %s\n", 11925 i40e_stat_str(&pf->hw, ret), 11926 i40e_aq_str(&pf->hw, 11927 pf->hw.aq.asq_last_status)); 11928 return -ENOENT; 11929 } 11930 vsi->info = ctxt.info; 11931 vsi->info.valid_sections = 0; 11932 11933 vsi->seid = ctxt.seid; 11934 vsi->id = ctxt.vsi_number; 11935 11936 enabled_tc = i40e_pf_get_tc_map(pf); 11937 11938 /* Source pruning is enabled by default, so the flag is 11939 * negative logic - if it's set, we need to fiddle with 11940 * the VSI to disable source pruning. 11941 */ 11942 if (pf->flags & I40E_FLAG_SOURCE_PRUNING_DISABLED) { 11943 memset(&ctxt, 0, sizeof(ctxt)); 11944 ctxt.seid = pf->main_vsi_seid; 11945 ctxt.pf_num = pf->hw.pf_id; 11946 ctxt.vf_num = 0; 11947 ctxt.info.valid_sections |= 11948 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID); 11949 ctxt.info.switch_id = 11950 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB); 11951 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL); 11952 if (ret) { 11953 dev_info(&pf->pdev->dev, 11954 "update vsi failed, err %s aq_err %s\n", 11955 i40e_stat_str(&pf->hw, ret), 11956 i40e_aq_str(&pf->hw, 11957 pf->hw.aq.asq_last_status)); 11958 ret = -ENOENT; 11959 goto err; 11960 } 11961 } 11962 11963 /* MFP mode setup queue map and update VSI */ 11964 if ((pf->flags & I40E_FLAG_MFP_ENABLED) && 11965 !(pf->hw.func_caps.iscsi)) { /* NIC type PF */ 11966 memset(&ctxt, 0, sizeof(ctxt)); 11967 ctxt.seid = pf->main_vsi_seid; 11968 ctxt.pf_num = pf->hw.pf_id; 11969 ctxt.vf_num = 0; 11970 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false); 11971 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL); 11972 if (ret) { 11973 dev_info(&pf->pdev->dev, 11974 "update vsi failed, err %s aq_err %s\n", 11975 i40e_stat_str(&pf->hw, ret), 11976 i40e_aq_str(&pf->hw, 11977 pf->hw.aq.asq_last_status)); 11978 ret = -ENOENT; 11979 goto err; 11980 } 11981 /* update the local VSI info queue map */ 11982 i40e_vsi_update_queue_map(vsi, &ctxt); 11983 vsi->info.valid_sections = 0; 11984 } else { 11985 /* Default/Main VSI is only enabled for TC0 11986 * reconfigure it to enable all TCs that are 11987 * available on the port in SFP mode. 11988 * For MFP case the iSCSI PF would use this 11989 * flow to enable LAN+iSCSI TC. 11990 */ 11991 ret = i40e_vsi_config_tc(vsi, enabled_tc); 11992 if (ret) { 11993 /* Single TC condition is not fatal, 11994 * message and continue 11995 */ 11996 dev_info(&pf->pdev->dev, 11997 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n", 11998 enabled_tc, 11999 i40e_stat_str(&pf->hw, ret), 12000 i40e_aq_str(&pf->hw, 12001 pf->hw.aq.asq_last_status)); 12002 } 12003 } 12004 break; 12005 12006 case I40E_VSI_FDIR: 12007 ctxt.pf_num = hw->pf_id; 12008 ctxt.vf_num = 0; 12009 ctxt.uplink_seid = vsi->uplink_seid; 12010 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL; 12011 ctxt.flags = I40E_AQ_VSI_TYPE_PF; 12012 if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) && 12013 (i40e_is_vsi_uplink_mode_veb(vsi))) { 12014 ctxt.info.valid_sections |= 12015 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID); 12016 ctxt.info.switch_id = 12017 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB); 12018 } 12019 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true); 12020 break; 12021 12022 case I40E_VSI_VMDQ2: 12023 ctxt.pf_num = hw->pf_id; 12024 ctxt.vf_num = 0; 12025 ctxt.uplink_seid = vsi->uplink_seid; 12026 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL; 12027 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2; 12028 12029 /* This VSI is connected to VEB so the switch_id 12030 * should be set to zero by default. 12031 */ 12032 if (i40e_is_vsi_uplink_mode_veb(vsi)) { 12033 ctxt.info.valid_sections |= 12034 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID); 12035 ctxt.info.switch_id = 12036 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB); 12037 } 12038 12039 /* Setup the VSI tx/rx queue map for TC0 only for now */ 12040 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true); 12041 break; 12042 12043 case I40E_VSI_SRIOV: 12044 ctxt.pf_num = hw->pf_id; 12045 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id; 12046 ctxt.uplink_seid = vsi->uplink_seid; 12047 ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL; 12048 ctxt.flags = I40E_AQ_VSI_TYPE_VF; 12049 12050 /* This VSI is connected to VEB so the switch_id 12051 * should be set to zero by default. 12052 */ 12053 if (i40e_is_vsi_uplink_mode_veb(vsi)) { 12054 ctxt.info.valid_sections |= 12055 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID); 12056 ctxt.info.switch_id = 12057 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB); 12058 } 12059 12060 if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) { 12061 ctxt.info.valid_sections |= 12062 cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID); 12063 ctxt.info.queueing_opt_flags |= 12064 (I40E_AQ_VSI_QUE_OPT_TCP_ENA | 12065 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI); 12066 } 12067 12068 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID); 12069 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL; 12070 if (pf->vf[vsi->vf_id].spoofchk) { 12071 ctxt.info.valid_sections |= 12072 cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID); 12073 ctxt.info.sec_flags |= 12074 (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK | 12075 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK); 12076 } 12077 /* Setup the VSI tx/rx queue map for TC0 only for now */ 12078 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true); 12079 break; 12080 12081 case I40E_VSI_IWARP: 12082 /* send down message to iWARP */ 12083 break; 12084 12085 default: 12086 return -ENODEV; 12087 } 12088 12089 if (vsi->type != I40E_VSI_MAIN) { 12090 ret = i40e_aq_add_vsi(hw, &ctxt, NULL); 12091 if (ret) { 12092 dev_info(&vsi->back->pdev->dev, 12093 "add vsi failed, err %s aq_err %s\n", 12094 i40e_stat_str(&pf->hw, ret), 12095 i40e_aq_str(&pf->hw, 12096 pf->hw.aq.asq_last_status)); 12097 ret = -ENOENT; 12098 goto err; 12099 } 12100 vsi->info = ctxt.info; 12101 vsi->info.valid_sections = 0; 12102 vsi->seid = ctxt.seid; 12103 vsi->id = ctxt.vsi_number; 12104 } 12105 12106 vsi->active_filters = 0; 12107 clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state); 12108 spin_lock_bh(&vsi->mac_filter_hash_lock); 12109 /* If macvlan filters already exist, force them to get loaded */ 12110 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) { 12111 f->state = I40E_FILTER_NEW; 12112 f_count++; 12113 } 12114 spin_unlock_bh(&vsi->mac_filter_hash_lock); 12115 12116 if (f_count) { 12117 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED; 12118 pf->flags |= I40E_FLAG_FILTER_SYNC; 12119 } 12120 12121 /* Update VSI BW information */ 12122 ret = i40e_vsi_get_bw_info(vsi); 12123 if (ret) { 12124 dev_info(&pf->pdev->dev, 12125 "couldn't get vsi bw info, err %s aq_err %s\n", 12126 i40e_stat_str(&pf->hw, ret), 12127 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status)); 12128 /* VSI is already added so not tearing that up */ 12129 ret = 0; 12130 } 12131 12132 err: 12133 return ret; 12134 } 12135 12136 /** 12137 * i40e_vsi_release - Delete a VSI and free its resources 12138 * @vsi: the VSI being removed 12139 * 12140 * Returns 0 on success or < 0 on error 12141 **/ 12142 int i40e_vsi_release(struct i40e_vsi *vsi) 12143 { 12144 struct i40e_mac_filter *f; 12145 struct hlist_node *h; 12146 struct i40e_veb *veb = NULL; 12147 struct i40e_pf *pf; 12148 u16 uplink_seid; 12149 int i, n, bkt; 12150 12151 pf = vsi->back; 12152 12153 /* release of a VEB-owner or last VSI is not allowed */ 12154 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) { 12155 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n", 12156 vsi->seid, vsi->uplink_seid); 12157 return -ENODEV; 12158 } 12159 if (vsi == pf->vsi[pf->lan_vsi] && 12160 !test_bit(__I40E_DOWN, pf->state)) { 12161 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n"); 12162 return -ENODEV; 12163 } 12164 12165 uplink_seid = vsi->uplink_seid; 12166 if (vsi->type != I40E_VSI_SRIOV) { 12167 if (vsi->netdev_registered) { 12168 vsi->netdev_registered = false; 12169 if (vsi->netdev) { 12170 /* results in a call to i40e_close() */ 12171 unregister_netdev(vsi->netdev); 12172 } 12173 } else { 12174 i40e_vsi_close(vsi); 12175 } 12176 i40e_vsi_disable_irq(vsi); 12177 } 12178 12179 spin_lock_bh(&vsi->mac_filter_hash_lock); 12180 12181 /* clear the sync flag on all filters */ 12182 if (vsi->netdev) { 12183 __dev_uc_unsync(vsi->netdev, NULL); 12184 __dev_mc_unsync(vsi->netdev, NULL); 12185 } 12186 12187 /* make sure any remaining filters are marked for deletion */ 12188 hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) 12189 __i40e_del_filter(vsi, f); 12190 12191 spin_unlock_bh(&vsi->mac_filter_hash_lock); 12192 12193 i40e_sync_vsi_filters(vsi); 12194 12195 i40e_vsi_delete(vsi); 12196 i40e_vsi_free_q_vectors(vsi); 12197 if (vsi->netdev) { 12198 free_netdev(vsi->netdev); 12199 vsi->netdev = NULL; 12200 } 12201 i40e_vsi_clear_rings(vsi); 12202 i40e_vsi_clear(vsi); 12203 12204 /* If this was the last thing on the VEB, except for the 12205 * controlling VSI, remove the VEB, which puts the controlling 12206 * VSI onto the next level down in the switch. 12207 * 12208 * Well, okay, there's one more exception here: don't remove 12209 * the orphan VEBs yet. We'll wait for an explicit remove request 12210 * from up the network stack. 12211 */ 12212 for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) { 12213 if (pf->vsi[i] && 12214 pf->vsi[i]->uplink_seid == uplink_seid && 12215 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) { 12216 n++; /* count the VSIs */ 12217 } 12218 } 12219 for (i = 0; i < I40E_MAX_VEB; i++) { 12220 if (!pf->veb[i]) 12221 continue; 12222 if (pf->veb[i]->uplink_seid == uplink_seid) 12223 n++; /* count the VEBs */ 12224 if (pf->veb[i]->seid == uplink_seid) 12225 veb = pf->veb[i]; 12226 } 12227 if (n == 0 && veb && veb->uplink_seid != 0) 12228 i40e_veb_release(veb); 12229 12230 return 0; 12231 } 12232 12233 /** 12234 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI 12235 * @vsi: ptr to the VSI 12236 * 12237 * This should only be called after i40e_vsi_mem_alloc() which allocates the 12238 * corresponding SW VSI structure and initializes num_queue_pairs for the 12239 * newly allocated VSI. 12240 * 12241 * Returns 0 on success or negative on failure 12242 **/ 12243 static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi) 12244 { 12245 int ret = -ENOENT; 12246 struct i40e_pf *pf = vsi->back; 12247 12248 if (vsi->q_vectors[0]) { 12249 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n", 12250 vsi->seid); 12251 return -EEXIST; 12252 } 12253 12254 if (vsi->base_vector) { 12255 dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n", 12256 vsi->seid, vsi->base_vector); 12257 return -EEXIST; 12258 } 12259 12260 ret = i40e_vsi_alloc_q_vectors(vsi); 12261 if (ret) { 12262 dev_info(&pf->pdev->dev, 12263 "failed to allocate %d q_vector for VSI %d, ret=%d\n", 12264 vsi->num_q_vectors, vsi->seid, ret); 12265 vsi->num_q_vectors = 0; 12266 goto vector_setup_out; 12267 } 12268 12269 /* In Legacy mode, we do not have to get any other vector since we 12270 * piggyback on the misc/ICR0 for queue interrupts. 12271 */ 12272 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED)) 12273 return ret; 12274 if (vsi->num_q_vectors) 12275 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile, 12276 vsi->num_q_vectors, vsi->idx); 12277 if (vsi->base_vector < 0) { 12278 dev_info(&pf->pdev->dev, 12279 "failed to get tracking for %d vectors for VSI %d, err=%d\n", 12280 vsi->num_q_vectors, vsi->seid, vsi->base_vector); 12281 i40e_vsi_free_q_vectors(vsi); 12282 ret = -ENOENT; 12283 goto vector_setup_out; 12284 } 12285 12286 vector_setup_out: 12287 return ret; 12288 } 12289 12290 /** 12291 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI 12292 * @vsi: pointer to the vsi. 12293 * 12294 * This re-allocates a vsi's queue resources. 12295 * 12296 * Returns pointer to the successfully allocated and configured VSI sw struct 12297 * on success, otherwise returns NULL on failure. 12298 **/ 12299 static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi) 12300 { 12301 u16 alloc_queue_pairs; 12302 struct i40e_pf *pf; 12303 u8 enabled_tc; 12304 int ret; 12305 12306 if (!vsi) 12307 return NULL; 12308 12309 pf = vsi->back; 12310 12311 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx); 12312 i40e_vsi_clear_rings(vsi); 12313 12314 i40e_vsi_free_arrays(vsi, false); 12315 i40e_set_num_rings_in_vsi(vsi); 12316 ret = i40e_vsi_alloc_arrays(vsi, false); 12317 if (ret) 12318 goto err_vsi; 12319 12320 alloc_queue_pairs = vsi->alloc_queue_pairs * 12321 (i40e_enabled_xdp_vsi(vsi) ? 2 : 1); 12322 12323 ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx); 12324 if (ret < 0) { 12325 dev_info(&pf->pdev->dev, 12326 "failed to get tracking for %d queues for VSI %d err %d\n", 12327 alloc_queue_pairs, vsi->seid, ret); 12328 goto err_vsi; 12329 } 12330 vsi->base_queue = ret; 12331 12332 /* Update the FW view of the VSI. Force a reset of TC and queue 12333 * layout configurations. 12334 */ 12335 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc; 12336 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0; 12337 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid; 12338 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc); 12339 if (vsi->type == I40E_VSI_MAIN) 12340 i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr); 12341 12342 /* assign it some queues */ 12343 ret = i40e_alloc_rings(vsi); 12344 if (ret) 12345 goto err_rings; 12346 12347 /* map all of the rings to the q_vectors */ 12348 i40e_vsi_map_rings_to_vectors(vsi); 12349 return vsi; 12350 12351 err_rings: 12352 i40e_vsi_free_q_vectors(vsi); 12353 if (vsi->netdev_registered) { 12354 vsi->netdev_registered = false; 12355 unregister_netdev(vsi->netdev); 12356 free_netdev(vsi->netdev); 12357 vsi->netdev = NULL; 12358 } 12359 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL); 12360 err_vsi: 12361 i40e_vsi_clear(vsi); 12362 return NULL; 12363 } 12364 12365 /** 12366 * i40e_vsi_setup - Set up a VSI by a given type 12367 * @pf: board private structure 12368 * @type: VSI type 12369 * @uplink_seid: the switch element to link to 12370 * @param1: usage depends upon VSI type. For VF types, indicates VF id 12371 * 12372 * This allocates the sw VSI structure and its queue resources, then add a VSI 12373 * to the identified VEB. 12374 * 12375 * Returns pointer to the successfully allocated and configure VSI sw struct on 12376 * success, otherwise returns NULL on failure. 12377 **/ 12378 struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type, 12379 u16 uplink_seid, u32 param1) 12380 { 12381 struct i40e_vsi *vsi = NULL; 12382 struct i40e_veb *veb = NULL; 12383 u16 alloc_queue_pairs; 12384 int ret, i; 12385 int v_idx; 12386 12387 /* The requested uplink_seid must be either 12388 * - the PF's port seid 12389 * no VEB is needed because this is the PF 12390 * or this is a Flow Director special case VSI 12391 * - seid of an existing VEB 12392 * - seid of a VSI that owns an existing VEB 12393 * - seid of a VSI that doesn't own a VEB 12394 * a new VEB is created and the VSI becomes the owner 12395 * - seid of the PF VSI, which is what creates the first VEB 12396 * this is a special case of the previous 12397 * 12398 * Find which uplink_seid we were given and create a new VEB if needed 12399 */ 12400 for (i = 0; i < I40E_MAX_VEB; i++) { 12401 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) { 12402 veb = pf->veb[i]; 12403 break; 12404 } 12405 } 12406 12407 if (!veb && uplink_seid != pf->mac_seid) { 12408 12409 for (i = 0; i < pf->num_alloc_vsi; i++) { 12410 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) { 12411 vsi = pf->vsi[i]; 12412 break; 12413 } 12414 } 12415 if (!vsi) { 12416 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n", 12417 uplink_seid); 12418 return NULL; 12419 } 12420 12421 if (vsi->uplink_seid == pf->mac_seid) 12422 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid, 12423 vsi->tc_config.enabled_tc); 12424 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) 12425 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid, 12426 vsi->tc_config.enabled_tc); 12427 if (veb) { 12428 if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) { 12429 dev_info(&vsi->back->pdev->dev, 12430 "New VSI creation error, uplink seid of LAN VSI expected.\n"); 12431 return NULL; 12432 } 12433 /* We come up by default in VEPA mode if SRIOV is not 12434 * already enabled, in which case we can't force VEPA 12435 * mode. 12436 */ 12437 if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) { 12438 veb->bridge_mode = BRIDGE_MODE_VEPA; 12439 pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED; 12440 } 12441 i40e_config_bridge_mode(veb); 12442 } 12443 for (i = 0; i < I40E_MAX_VEB && !veb; i++) { 12444 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid) 12445 veb = pf->veb[i]; 12446 } 12447 if (!veb) { 12448 dev_info(&pf->pdev->dev, "couldn't add VEB\n"); 12449 return NULL; 12450 } 12451 12452 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER; 12453 uplink_seid = veb->seid; 12454 } 12455 12456 /* get vsi sw struct */ 12457 v_idx = i40e_vsi_mem_alloc(pf, type); 12458 if (v_idx < 0) 12459 goto err_alloc; 12460 vsi = pf->vsi[v_idx]; 12461 if (!vsi) 12462 goto err_alloc; 12463 vsi->type = type; 12464 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB); 12465 12466 if (type == I40E_VSI_MAIN) 12467 pf->lan_vsi = v_idx; 12468 else if (type == I40E_VSI_SRIOV) 12469 vsi->vf_id = param1; 12470 /* assign it some queues */ 12471 alloc_queue_pairs = vsi->alloc_queue_pairs * 12472 (i40e_enabled_xdp_vsi(vsi) ? 2 : 1); 12473 12474 ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx); 12475 if (ret < 0) { 12476 dev_info(&pf->pdev->dev, 12477 "failed to get tracking for %d queues for VSI %d err=%d\n", 12478 alloc_queue_pairs, vsi->seid, ret); 12479 goto err_vsi; 12480 } 12481 vsi->base_queue = ret; 12482 12483 /* get a VSI from the hardware */ 12484 vsi->uplink_seid = uplink_seid; 12485 ret = i40e_add_vsi(vsi); 12486 if (ret) 12487 goto err_vsi; 12488 12489 switch (vsi->type) { 12490 /* setup the netdev if needed */ 12491 case I40E_VSI_MAIN: 12492 case I40E_VSI_VMDQ2: 12493 ret = i40e_config_netdev(vsi); 12494 if (ret) 12495 goto err_netdev; 12496 ret = register_netdev(vsi->netdev); 12497 if (ret) 12498 goto err_netdev; 12499 vsi->netdev_registered = true; 12500 netif_carrier_off(vsi->netdev); 12501 #ifdef CONFIG_I40E_DCB 12502 /* Setup DCB netlink interface */ 12503 i40e_dcbnl_setup(vsi); 12504 #endif /* CONFIG_I40E_DCB */ 12505 /* fall through */ 12506 12507 case I40E_VSI_FDIR: 12508 /* set up vectors and rings if needed */ 12509 ret = i40e_vsi_setup_vectors(vsi); 12510 if (ret) 12511 goto err_msix; 12512 12513 ret = i40e_alloc_rings(vsi); 12514 if (ret) 12515 goto err_rings; 12516 12517 /* map all of the rings to the q_vectors */ 12518 i40e_vsi_map_rings_to_vectors(vsi); 12519 12520 i40e_vsi_reset_stats(vsi); 12521 break; 12522 12523 default: 12524 /* no netdev or rings for the other VSI types */ 12525 break; 12526 } 12527 12528 if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) && 12529 (vsi->type == I40E_VSI_VMDQ2)) { 12530 ret = i40e_vsi_config_rss(vsi); 12531 } 12532 return vsi; 12533 12534 err_rings: 12535 i40e_vsi_free_q_vectors(vsi); 12536 err_msix: 12537 if (vsi->netdev_registered) { 12538 vsi->netdev_registered = false; 12539 unregister_netdev(vsi->netdev); 12540 free_netdev(vsi->netdev); 12541 vsi->netdev = NULL; 12542 } 12543 err_netdev: 12544 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL); 12545 err_vsi: 12546 i40e_vsi_clear(vsi); 12547 err_alloc: 12548 return NULL; 12549 } 12550 12551 /** 12552 * i40e_veb_get_bw_info - Query VEB BW information 12553 * @veb: the veb to query 12554 * 12555 * Query the Tx scheduler BW configuration data for given VEB 12556 **/ 12557 static int i40e_veb_get_bw_info(struct i40e_veb *veb) 12558 { 12559 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data; 12560 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data; 12561 struct i40e_pf *pf = veb->pf; 12562 struct i40e_hw *hw = &pf->hw; 12563 u32 tc_bw_max; 12564 int ret = 0; 12565 int i; 12566 12567 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid, 12568 &bw_data, NULL); 12569 if (ret) { 12570 dev_info(&pf->pdev->dev, 12571 "query veb bw config failed, err %s aq_err %s\n", 12572 i40e_stat_str(&pf->hw, ret), 12573 i40e_aq_str(&pf->hw, hw->aq.asq_last_status)); 12574 goto out; 12575 } 12576 12577 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid, 12578 &ets_data, NULL); 12579 if (ret) { 12580 dev_info(&pf->pdev->dev, 12581 "query veb bw ets config failed, err %s aq_err %s\n", 12582 i40e_stat_str(&pf->hw, ret), 12583 i40e_aq_str(&pf->hw, hw->aq.asq_last_status)); 12584 goto out; 12585 } 12586 12587 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit); 12588 veb->bw_max_quanta = ets_data.tc_bw_max; 12589 veb->is_abs_credits = bw_data.absolute_credits_enable; 12590 veb->enabled_tc = ets_data.tc_valid_bits; 12591 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) | 12592 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16); 12593 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) { 12594 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i]; 12595 veb->bw_tc_limit_credits[i] = 12596 le16_to_cpu(bw_data.tc_bw_limits[i]); 12597 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7); 12598 } 12599 12600 out: 12601 return ret; 12602 } 12603 12604 /** 12605 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF 12606 * @pf: board private structure 12607 * 12608 * On error: returns error code (negative) 12609 * On success: returns vsi index in PF (positive) 12610 **/ 12611 static int i40e_veb_mem_alloc(struct i40e_pf *pf) 12612 { 12613 int ret = -ENOENT; 12614 struct i40e_veb *veb; 12615 int i; 12616 12617 /* Need to protect the allocation of switch elements at the PF level */ 12618 mutex_lock(&pf->switch_mutex); 12619 12620 /* VEB list may be fragmented if VEB creation/destruction has 12621 * been happening. We can afford to do a quick scan to look 12622 * for any free slots in the list. 12623 * 12624 * find next empty veb slot, looping back around if necessary 12625 */ 12626 i = 0; 12627 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL)) 12628 i++; 12629 if (i >= I40E_MAX_VEB) { 12630 ret = -ENOMEM; 12631 goto err_alloc_veb; /* out of VEB slots! */ 12632 } 12633 12634 veb = kzalloc(sizeof(*veb), GFP_KERNEL); 12635 if (!veb) { 12636 ret = -ENOMEM; 12637 goto err_alloc_veb; 12638 } 12639 veb->pf = pf; 12640 veb->idx = i; 12641 veb->enabled_tc = 1; 12642 12643 pf->veb[i] = veb; 12644 ret = i; 12645 err_alloc_veb: 12646 mutex_unlock(&pf->switch_mutex); 12647 return ret; 12648 } 12649 12650 /** 12651 * i40e_switch_branch_release - Delete a branch of the switch tree 12652 * @branch: where to start deleting 12653 * 12654 * This uses recursion to find the tips of the branch to be 12655 * removed, deleting until we get back to and can delete this VEB. 12656 **/ 12657 static void i40e_switch_branch_release(struct i40e_veb *branch) 12658 { 12659 struct i40e_pf *pf = branch->pf; 12660 u16 branch_seid = branch->seid; 12661 u16 veb_idx = branch->idx; 12662 int i; 12663 12664 /* release any VEBs on this VEB - RECURSION */ 12665 for (i = 0; i < I40E_MAX_VEB; i++) { 12666 if (!pf->veb[i]) 12667 continue; 12668 if (pf->veb[i]->uplink_seid == branch->seid) 12669 i40e_switch_branch_release(pf->veb[i]); 12670 } 12671 12672 /* Release the VSIs on this VEB, but not the owner VSI. 12673 * 12674 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing 12675 * the VEB itself, so don't use (*branch) after this loop. 12676 */ 12677 for (i = 0; i < pf->num_alloc_vsi; i++) { 12678 if (!pf->vsi[i]) 12679 continue; 12680 if (pf->vsi[i]->uplink_seid == branch_seid && 12681 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) { 12682 i40e_vsi_release(pf->vsi[i]); 12683 } 12684 } 12685 12686 /* There's one corner case where the VEB might not have been 12687 * removed, so double check it here and remove it if needed. 12688 * This case happens if the veb was created from the debugfs 12689 * commands and no VSIs were added to it. 12690 */ 12691 if (pf->veb[veb_idx]) 12692 i40e_veb_release(pf->veb[veb_idx]); 12693 } 12694 12695 /** 12696 * i40e_veb_clear - remove veb struct 12697 * @veb: the veb to remove 12698 **/ 12699 static void i40e_veb_clear(struct i40e_veb *veb) 12700 { 12701 if (!veb) 12702 return; 12703 12704 if (veb->pf) { 12705 struct i40e_pf *pf = veb->pf; 12706 12707 mutex_lock(&pf->switch_mutex); 12708 if (pf->veb[veb->idx] == veb) 12709 pf->veb[veb->idx] = NULL; 12710 mutex_unlock(&pf->switch_mutex); 12711 } 12712 12713 kfree(veb); 12714 } 12715 12716 /** 12717 * i40e_veb_release - Delete a VEB and free its resources 12718 * @veb: the VEB being removed 12719 **/ 12720 void i40e_veb_release(struct i40e_veb *veb) 12721 { 12722 struct i40e_vsi *vsi = NULL; 12723 struct i40e_pf *pf; 12724 int i, n = 0; 12725 12726 pf = veb->pf; 12727 12728 /* find the remaining VSI and check for extras */ 12729 for (i = 0; i < pf->num_alloc_vsi; i++) { 12730 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) { 12731 n++; 12732 vsi = pf->vsi[i]; 12733 } 12734 } 12735 if (n != 1) { 12736 dev_info(&pf->pdev->dev, 12737 "can't remove VEB %d with %d VSIs left\n", 12738 veb->seid, n); 12739 return; 12740 } 12741 12742 /* move the remaining VSI to uplink veb */ 12743 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER; 12744 if (veb->uplink_seid) { 12745 vsi->uplink_seid = veb->uplink_seid; 12746 if (veb->uplink_seid == pf->mac_seid) 12747 vsi->veb_idx = I40E_NO_VEB; 12748 else 12749 vsi->veb_idx = veb->veb_idx; 12750 } else { 12751 /* floating VEB */ 12752 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid; 12753 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx; 12754 } 12755 12756 i40e_aq_delete_element(&pf->hw, veb->seid, NULL); 12757 i40e_veb_clear(veb); 12758 } 12759 12760 /** 12761 * i40e_add_veb - create the VEB in the switch 12762 * @veb: the VEB to be instantiated 12763 * @vsi: the controlling VSI 12764 **/ 12765 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi) 12766 { 12767 struct i40e_pf *pf = veb->pf; 12768 bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED); 12769 int ret; 12770 12771 ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid, 12772 veb->enabled_tc, false, 12773 &veb->seid, enable_stats, NULL); 12774 12775 /* get a VEB from the hardware */ 12776 if (ret) { 12777 dev_info(&pf->pdev->dev, 12778 "couldn't add VEB, err %s aq_err %s\n", 12779 i40e_stat_str(&pf->hw, ret), 12780 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status)); 12781 return -EPERM; 12782 } 12783 12784 /* get statistics counter */ 12785 ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL, 12786 &veb->stats_idx, NULL, NULL, NULL); 12787 if (ret) { 12788 dev_info(&pf->pdev->dev, 12789 "couldn't get VEB statistics idx, err %s aq_err %s\n", 12790 i40e_stat_str(&pf->hw, ret), 12791 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status)); 12792 return -EPERM; 12793 } 12794 ret = i40e_veb_get_bw_info(veb); 12795 if (ret) { 12796 dev_info(&pf->pdev->dev, 12797 "couldn't get VEB bw info, err %s aq_err %s\n", 12798 i40e_stat_str(&pf->hw, ret), 12799 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status)); 12800 i40e_aq_delete_element(&pf->hw, veb->seid, NULL); 12801 return -ENOENT; 12802 } 12803 12804 vsi->uplink_seid = veb->seid; 12805 vsi->veb_idx = veb->idx; 12806 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER; 12807 12808 return 0; 12809 } 12810 12811 /** 12812 * i40e_veb_setup - Set up a VEB 12813 * @pf: board private structure 12814 * @flags: VEB setup flags 12815 * @uplink_seid: the switch element to link to 12816 * @vsi_seid: the initial VSI seid 12817 * @enabled_tc: Enabled TC bit-map 12818 * 12819 * This allocates the sw VEB structure and links it into the switch 12820 * It is possible and legal for this to be a duplicate of an already 12821 * existing VEB. It is also possible for both uplink and vsi seids 12822 * to be zero, in order to create a floating VEB. 12823 * 12824 * Returns pointer to the successfully allocated VEB sw struct on 12825 * success, otherwise returns NULL on failure. 12826 **/ 12827 struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags, 12828 u16 uplink_seid, u16 vsi_seid, 12829 u8 enabled_tc) 12830 { 12831 struct i40e_veb *veb, *uplink_veb = NULL; 12832 int vsi_idx, veb_idx; 12833 int ret; 12834 12835 /* if one seid is 0, the other must be 0 to create a floating relay */ 12836 if ((uplink_seid == 0 || vsi_seid == 0) && 12837 (uplink_seid + vsi_seid != 0)) { 12838 dev_info(&pf->pdev->dev, 12839 "one, not both seid's are 0: uplink=%d vsi=%d\n", 12840 uplink_seid, vsi_seid); 12841 return NULL; 12842 } 12843 12844 /* make sure there is such a vsi and uplink */ 12845 for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++) 12846 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid) 12847 break; 12848 if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) { 12849 dev_info(&pf->pdev->dev, "vsi seid %d not found\n", 12850 vsi_seid); 12851 return NULL; 12852 } 12853 12854 if (uplink_seid && uplink_seid != pf->mac_seid) { 12855 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) { 12856 if (pf->veb[veb_idx] && 12857 pf->veb[veb_idx]->seid == uplink_seid) { 12858 uplink_veb = pf->veb[veb_idx]; 12859 break; 12860 } 12861 } 12862 if (!uplink_veb) { 12863 dev_info(&pf->pdev->dev, 12864 "uplink seid %d not found\n", uplink_seid); 12865 return NULL; 12866 } 12867 } 12868 12869 /* get veb sw struct */ 12870 veb_idx = i40e_veb_mem_alloc(pf); 12871 if (veb_idx < 0) 12872 goto err_alloc; 12873 veb = pf->veb[veb_idx]; 12874 veb->flags = flags; 12875 veb->uplink_seid = uplink_seid; 12876 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB); 12877 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1); 12878 12879 /* create the VEB in the switch */ 12880 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]); 12881 if (ret) 12882 goto err_veb; 12883 if (vsi_idx == pf->lan_vsi) 12884 pf->lan_veb = veb->idx; 12885 12886 return veb; 12887 12888 err_veb: 12889 i40e_veb_clear(veb); 12890 err_alloc: 12891 return NULL; 12892 } 12893 12894 /** 12895 * i40e_setup_pf_switch_element - set PF vars based on switch type 12896 * @pf: board private structure 12897 * @ele: element we are building info from 12898 * @num_reported: total number of elements 12899 * @printconfig: should we print the contents 12900 * 12901 * helper function to assist in extracting a few useful SEID values. 12902 **/ 12903 static void i40e_setup_pf_switch_element(struct i40e_pf *pf, 12904 struct i40e_aqc_switch_config_element_resp *ele, 12905 u16 num_reported, bool printconfig) 12906 { 12907 u16 downlink_seid = le16_to_cpu(ele->downlink_seid); 12908 u16 uplink_seid = le16_to_cpu(ele->uplink_seid); 12909 u8 element_type = ele->element_type; 12910 u16 seid = le16_to_cpu(ele->seid); 12911 12912 if (printconfig) 12913 dev_info(&pf->pdev->dev, 12914 "type=%d seid=%d uplink=%d downlink=%d\n", 12915 element_type, seid, uplink_seid, downlink_seid); 12916 12917 switch (element_type) { 12918 case I40E_SWITCH_ELEMENT_TYPE_MAC: 12919 pf->mac_seid = seid; 12920 break; 12921 case I40E_SWITCH_ELEMENT_TYPE_VEB: 12922 /* Main VEB? */ 12923 if (uplink_seid != pf->mac_seid) 12924 break; 12925 if (pf->lan_veb == I40E_NO_VEB) { 12926 int v; 12927 12928 /* find existing or else empty VEB */ 12929 for (v = 0; v < I40E_MAX_VEB; v++) { 12930 if (pf->veb[v] && (pf->veb[v]->seid == seid)) { 12931 pf->lan_veb = v; 12932 break; 12933 } 12934 } 12935 if (pf->lan_veb == I40E_NO_VEB) { 12936 v = i40e_veb_mem_alloc(pf); 12937 if (v < 0) 12938 break; 12939 pf->lan_veb = v; 12940 } 12941 } 12942 12943 pf->veb[pf->lan_veb]->seid = seid; 12944 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid; 12945 pf->veb[pf->lan_veb]->pf = pf; 12946 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB; 12947 break; 12948 case I40E_SWITCH_ELEMENT_TYPE_VSI: 12949 if (num_reported != 1) 12950 break; 12951 /* This is immediately after a reset so we can assume this is 12952 * the PF's VSI 12953 */ 12954 pf->mac_seid = uplink_seid; 12955 pf->pf_seid = downlink_seid; 12956 pf->main_vsi_seid = seid; 12957 if (printconfig) 12958 dev_info(&pf->pdev->dev, 12959 "pf_seid=%d main_vsi_seid=%d\n", 12960 pf->pf_seid, pf->main_vsi_seid); 12961 break; 12962 case I40E_SWITCH_ELEMENT_TYPE_PF: 12963 case I40E_SWITCH_ELEMENT_TYPE_VF: 12964 case I40E_SWITCH_ELEMENT_TYPE_EMP: 12965 case I40E_SWITCH_ELEMENT_TYPE_BMC: 12966 case I40E_SWITCH_ELEMENT_TYPE_PE: 12967 case I40E_SWITCH_ELEMENT_TYPE_PA: 12968 /* ignore these for now */ 12969 break; 12970 default: 12971 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n", 12972 element_type, seid); 12973 break; 12974 } 12975 } 12976 12977 /** 12978 * i40e_fetch_switch_configuration - Get switch config from firmware 12979 * @pf: board private structure 12980 * @printconfig: should we print the contents 12981 * 12982 * Get the current switch configuration from the device and 12983 * extract a few useful SEID values. 12984 **/ 12985 int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig) 12986 { 12987 struct i40e_aqc_get_switch_config_resp *sw_config; 12988 u16 next_seid = 0; 12989 int ret = 0; 12990 u8 *aq_buf; 12991 int i; 12992 12993 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL); 12994 if (!aq_buf) 12995 return -ENOMEM; 12996 12997 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf; 12998 do { 12999 u16 num_reported, num_total; 13000 13001 ret = i40e_aq_get_switch_config(&pf->hw, sw_config, 13002 I40E_AQ_LARGE_BUF, 13003 &next_seid, NULL); 13004 if (ret) { 13005 dev_info(&pf->pdev->dev, 13006 "get switch config failed err %s aq_err %s\n", 13007 i40e_stat_str(&pf->hw, ret), 13008 i40e_aq_str(&pf->hw, 13009 pf->hw.aq.asq_last_status)); 13010 kfree(aq_buf); 13011 return -ENOENT; 13012 } 13013 13014 num_reported = le16_to_cpu(sw_config->header.num_reported); 13015 num_total = le16_to_cpu(sw_config->header.num_total); 13016 13017 if (printconfig) 13018 dev_info(&pf->pdev->dev, 13019 "header: %d reported %d total\n", 13020 num_reported, num_total); 13021 13022 for (i = 0; i < num_reported; i++) { 13023 struct i40e_aqc_switch_config_element_resp *ele = 13024 &sw_config->element[i]; 13025 13026 i40e_setup_pf_switch_element(pf, ele, num_reported, 13027 printconfig); 13028 } 13029 } while (next_seid != 0); 13030 13031 kfree(aq_buf); 13032 return ret; 13033 } 13034 13035 /** 13036 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset 13037 * @pf: board private structure 13038 * @reinit: if the Main VSI needs to re-initialized. 13039 * 13040 * Returns 0 on success, negative value on failure 13041 **/ 13042 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit) 13043 { 13044 u16 flags = 0; 13045 int ret; 13046 13047 /* find out what's out there already */ 13048 ret = i40e_fetch_switch_configuration(pf, false); 13049 if (ret) { 13050 dev_info(&pf->pdev->dev, 13051 "couldn't fetch switch config, err %s aq_err %s\n", 13052 i40e_stat_str(&pf->hw, ret), 13053 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status)); 13054 return ret; 13055 } 13056 i40e_pf_reset_stats(pf); 13057 13058 /* set the switch config bit for the whole device to 13059 * support limited promisc or true promisc 13060 * when user requests promisc. The default is limited 13061 * promisc. 13062 */ 13063 13064 if ((pf->hw.pf_id == 0) && 13065 !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT)) { 13066 flags = I40E_AQ_SET_SWITCH_CFG_PROMISC; 13067 pf->last_sw_conf_flags = flags; 13068 } 13069 13070 if (pf->hw.pf_id == 0) { 13071 u16 valid_flags; 13072 13073 valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC; 13074 ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0, 13075 NULL); 13076 if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) { 13077 dev_info(&pf->pdev->dev, 13078 "couldn't set switch config bits, err %s aq_err %s\n", 13079 i40e_stat_str(&pf->hw, ret), 13080 i40e_aq_str(&pf->hw, 13081 pf->hw.aq.asq_last_status)); 13082 /* not a fatal problem, just keep going */ 13083 } 13084 pf->last_sw_conf_valid_flags = valid_flags; 13085 } 13086 13087 /* first time setup */ 13088 if (pf->lan_vsi == I40E_NO_VSI || reinit) { 13089 struct i40e_vsi *vsi = NULL; 13090 u16 uplink_seid; 13091 13092 /* Set up the PF VSI associated with the PF's main VSI 13093 * that is already in the HW switch 13094 */ 13095 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb]) 13096 uplink_seid = pf->veb[pf->lan_veb]->seid; 13097 else 13098 uplink_seid = pf->mac_seid; 13099 if (pf->lan_vsi == I40E_NO_VSI) 13100 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0); 13101 else if (reinit) 13102 vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]); 13103 if (!vsi) { 13104 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n"); 13105 i40e_cloud_filter_exit(pf); 13106 i40e_fdir_teardown(pf); 13107 return -EAGAIN; 13108 } 13109 } else { 13110 /* force a reset of TC and queue layout configurations */ 13111 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc; 13112 13113 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0; 13114 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid; 13115 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc); 13116 } 13117 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]); 13118 13119 i40e_fdir_sb_setup(pf); 13120 13121 /* Setup static PF queue filter control settings */ 13122 ret = i40e_setup_pf_filter_control(pf); 13123 if (ret) { 13124 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n", 13125 ret); 13126 /* Failure here should not stop continuing other steps */ 13127 } 13128 13129 /* enable RSS in the HW, even for only one queue, as the stack can use 13130 * the hash 13131 */ 13132 if ((pf->flags & I40E_FLAG_RSS_ENABLED)) 13133 i40e_pf_config_rss(pf); 13134 13135 /* fill in link information and enable LSE reporting */ 13136 i40e_link_event(pf); 13137 13138 /* Initialize user-specific link properties */ 13139 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info & 13140 I40E_AQ_AN_COMPLETED) ? true : false); 13141 13142 i40e_ptp_init(pf); 13143 13144 /* repopulate tunnel port filters */ 13145 i40e_sync_udp_filters(pf); 13146 13147 return ret; 13148 } 13149 13150 /** 13151 * i40e_determine_queue_usage - Work out queue distribution 13152 * @pf: board private structure 13153 **/ 13154 static void i40e_determine_queue_usage(struct i40e_pf *pf) 13155 { 13156 int queues_left; 13157 int q_max; 13158 13159 pf->num_lan_qps = 0; 13160 13161 /* Find the max queues to be put into basic use. We'll always be 13162 * using TC0, whether or not DCB is running, and TC0 will get the 13163 * big RSS set. 13164 */ 13165 queues_left = pf->hw.func_caps.num_tx_qp; 13166 13167 if ((queues_left == 1) || 13168 !(pf->flags & I40E_FLAG_MSIX_ENABLED)) { 13169 /* one qp for PF, no queues for anything else */ 13170 queues_left = 0; 13171 pf->alloc_rss_size = pf->num_lan_qps = 1; 13172 13173 /* make sure all the fancies are disabled */ 13174 pf->flags &= ~(I40E_FLAG_RSS_ENABLED | 13175 I40E_FLAG_IWARP_ENABLED | 13176 I40E_FLAG_FD_SB_ENABLED | 13177 I40E_FLAG_FD_ATR_ENABLED | 13178 I40E_FLAG_DCB_CAPABLE | 13179 I40E_FLAG_DCB_ENABLED | 13180 I40E_FLAG_SRIOV_ENABLED | 13181 I40E_FLAG_VMDQ_ENABLED); 13182 pf->flags |= I40E_FLAG_FD_SB_INACTIVE; 13183 } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED | 13184 I40E_FLAG_FD_SB_ENABLED | 13185 I40E_FLAG_FD_ATR_ENABLED | 13186 I40E_FLAG_DCB_CAPABLE))) { 13187 /* one qp for PF */ 13188 pf->alloc_rss_size = pf->num_lan_qps = 1; 13189 queues_left -= pf->num_lan_qps; 13190 13191 pf->flags &= ~(I40E_FLAG_RSS_ENABLED | 13192 I40E_FLAG_IWARP_ENABLED | 13193 I40E_FLAG_FD_SB_ENABLED | 13194 I40E_FLAG_FD_ATR_ENABLED | 13195 I40E_FLAG_DCB_ENABLED | 13196 I40E_FLAG_VMDQ_ENABLED); 13197 pf->flags |= I40E_FLAG_FD_SB_INACTIVE; 13198 } else { 13199 /* Not enough queues for all TCs */ 13200 if ((pf->flags & I40E_FLAG_DCB_CAPABLE) && 13201 (queues_left < I40E_MAX_TRAFFIC_CLASS)) { 13202 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | 13203 I40E_FLAG_DCB_ENABLED); 13204 dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n"); 13205 } 13206 13207 /* limit lan qps to the smaller of qps, cpus or msix */ 13208 q_max = max_t(int, pf->rss_size_max, num_online_cpus()); 13209 q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp); 13210 q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors); 13211 pf->num_lan_qps = q_max; 13212 13213 queues_left -= pf->num_lan_qps; 13214 } 13215 13216 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) { 13217 if (queues_left > 1) { 13218 queues_left -= 1; /* save 1 queue for FD */ 13219 } else { 13220 pf->flags &= ~I40E_FLAG_FD_SB_ENABLED; 13221 pf->flags |= I40E_FLAG_FD_SB_INACTIVE; 13222 dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n"); 13223 } 13224 } 13225 13226 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) && 13227 pf->num_vf_qps && pf->num_req_vfs && queues_left) { 13228 pf->num_req_vfs = min_t(int, pf->num_req_vfs, 13229 (queues_left / pf->num_vf_qps)); 13230 queues_left -= (pf->num_req_vfs * pf->num_vf_qps); 13231 } 13232 13233 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) && 13234 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) { 13235 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis, 13236 (queues_left / pf->num_vmdq_qps)); 13237 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps); 13238 } 13239 13240 pf->queues_left = queues_left; 13241 dev_dbg(&pf->pdev->dev, 13242 "qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n", 13243 pf->hw.func_caps.num_tx_qp, 13244 !!(pf->flags & I40E_FLAG_FD_SB_ENABLED), 13245 pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs, 13246 pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps, 13247 queues_left); 13248 } 13249 13250 /** 13251 * i40e_setup_pf_filter_control - Setup PF static filter control 13252 * @pf: PF to be setup 13253 * 13254 * i40e_setup_pf_filter_control sets up a PF's initial filter control 13255 * settings. If PE/FCoE are enabled then it will also set the per PF 13256 * based filter sizes required for them. It also enables Flow director, 13257 * ethertype and macvlan type filter settings for the pf. 13258 * 13259 * Returns 0 on success, negative on failure 13260 **/ 13261 static int i40e_setup_pf_filter_control(struct i40e_pf *pf) 13262 { 13263 struct i40e_filter_control_settings *settings = &pf->filter_settings; 13264 13265 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128; 13266 13267 /* Flow Director is enabled */ 13268 if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED)) 13269 settings->enable_fdir = true; 13270 13271 /* Ethtype and MACVLAN filters enabled for PF */ 13272 settings->enable_ethtype = true; 13273 settings->enable_macvlan = true; 13274 13275 if (i40e_set_filter_control(&pf->hw, settings)) 13276 return -ENOENT; 13277 13278 return 0; 13279 } 13280 13281 #define INFO_STRING_LEN 255 13282 #define REMAIN(__x) (INFO_STRING_LEN - (__x)) 13283 static void i40e_print_features(struct i40e_pf *pf) 13284 { 13285 struct i40e_hw *hw = &pf->hw; 13286 char *buf; 13287 int i; 13288 13289 buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL); 13290 if (!buf) 13291 return; 13292 13293 i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id); 13294 #ifdef CONFIG_PCI_IOV 13295 i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs); 13296 #endif 13297 i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d", 13298 pf->hw.func_caps.num_vsis, 13299 pf->vsi[pf->lan_vsi]->num_queue_pairs); 13300 if (pf->flags & I40E_FLAG_RSS_ENABLED) 13301 i += snprintf(&buf[i], REMAIN(i), " RSS"); 13302 if (pf->flags & I40E_FLAG_FD_ATR_ENABLED) 13303 i += snprintf(&buf[i], REMAIN(i), " FD_ATR"); 13304 if (pf->flags & I40E_FLAG_FD_SB_ENABLED) { 13305 i += snprintf(&buf[i], REMAIN(i), " FD_SB"); 13306 i += snprintf(&buf[i], REMAIN(i), " NTUPLE"); 13307 } 13308 if (pf->flags & I40E_FLAG_DCB_CAPABLE) 13309 i += snprintf(&buf[i], REMAIN(i), " DCB"); 13310 i += snprintf(&buf[i], REMAIN(i), " VxLAN"); 13311 i += snprintf(&buf[i], REMAIN(i), " Geneve"); 13312 if (pf->flags & I40E_FLAG_PTP) 13313 i += snprintf(&buf[i], REMAIN(i), " PTP"); 13314 if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED) 13315 i += snprintf(&buf[i], REMAIN(i), " VEB"); 13316 else 13317 i += snprintf(&buf[i], REMAIN(i), " VEPA"); 13318 13319 dev_info(&pf->pdev->dev, "%s\n", buf); 13320 kfree(buf); 13321 WARN_ON(i > INFO_STRING_LEN); 13322 } 13323 13324 /** 13325 * i40e_get_platform_mac_addr - get platform-specific MAC address 13326 * @pdev: PCI device information struct 13327 * @pf: board private structure 13328 * 13329 * Look up the MAC address for the device. First we'll try 13330 * eth_platform_get_mac_address, which will check Open Firmware, or arch 13331 * specific fallback. Otherwise, we'll default to the stored value in 13332 * firmware. 13333 **/ 13334 static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf) 13335 { 13336 if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr)) 13337 i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr); 13338 } 13339 13340 /** 13341 * i40e_probe - Device initialization routine 13342 * @pdev: PCI device information struct 13343 * @ent: entry in i40e_pci_tbl 13344 * 13345 * i40e_probe initializes a PF identified by a pci_dev structure. 13346 * The OS initialization, configuring of the PF private structure, 13347 * and a hardware reset occur. 13348 * 13349 * Returns 0 on success, negative on failure 13350 **/ 13351 static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent) 13352 { 13353 struct i40e_aq_get_phy_abilities_resp abilities; 13354 struct i40e_pf *pf; 13355 struct i40e_hw *hw; 13356 static u16 pfs_found; 13357 u16 wol_nvm_bits; 13358 u16 link_status; 13359 int err; 13360 u32 val; 13361 u32 i; 13362 u8 set_fc_aq_fail; 13363 13364 err = pci_enable_device_mem(pdev); 13365 if (err) 13366 return err; 13367 13368 /* set up for high or low dma */ 13369 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)); 13370 if (err) { 13371 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)); 13372 if (err) { 13373 dev_err(&pdev->dev, 13374 "DMA configuration failed: 0x%x\n", err); 13375 goto err_dma; 13376 } 13377 } 13378 13379 /* set up pci connections */ 13380 err = pci_request_mem_regions(pdev, i40e_driver_name); 13381 if (err) { 13382 dev_info(&pdev->dev, 13383 "pci_request_selected_regions failed %d\n", err); 13384 goto err_pci_reg; 13385 } 13386 13387 pci_enable_pcie_error_reporting(pdev); 13388 pci_set_master(pdev); 13389 13390 /* Now that we have a PCI connection, we need to do the 13391 * low level device setup. This is primarily setting up 13392 * the Admin Queue structures and then querying for the 13393 * device's current profile information. 13394 */ 13395 pf = kzalloc(sizeof(*pf), GFP_KERNEL); 13396 if (!pf) { 13397 err = -ENOMEM; 13398 goto err_pf_alloc; 13399 } 13400 pf->next_vsi = 0; 13401 pf->pdev = pdev; 13402 set_bit(__I40E_DOWN, pf->state); 13403 13404 hw = &pf->hw; 13405 hw->back = pf; 13406 13407 pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0), 13408 I40E_MAX_CSR_SPACE); 13409 13410 hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len); 13411 if (!hw->hw_addr) { 13412 err = -EIO; 13413 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n", 13414 (unsigned int)pci_resource_start(pdev, 0), 13415 pf->ioremap_len, err); 13416 goto err_ioremap; 13417 } 13418 hw->vendor_id = pdev->vendor; 13419 hw->device_id = pdev->device; 13420 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id); 13421 hw->subsystem_vendor_id = pdev->subsystem_vendor; 13422 hw->subsystem_device_id = pdev->subsystem_device; 13423 hw->bus.device = PCI_SLOT(pdev->devfn); 13424 hw->bus.func = PCI_FUNC(pdev->devfn); 13425 hw->bus.bus_id = pdev->bus->number; 13426 pf->instance = pfs_found; 13427 13428 /* Select something other than the 802.1ad ethertype for the 13429 * switch to use internally and drop on ingress. 13430 */ 13431 hw->switch_tag = 0xffff; 13432 hw->first_tag = ETH_P_8021AD; 13433 hw->second_tag = ETH_P_8021Q; 13434 13435 INIT_LIST_HEAD(&pf->l3_flex_pit_list); 13436 INIT_LIST_HEAD(&pf->l4_flex_pit_list); 13437 13438 /* set up the locks for the AQ, do this only once in probe 13439 * and destroy them only once in remove 13440 */ 13441 mutex_init(&hw->aq.asq_mutex); 13442 mutex_init(&hw->aq.arq_mutex); 13443 13444 pf->msg_enable = netif_msg_init(debug, 13445 NETIF_MSG_DRV | 13446 NETIF_MSG_PROBE | 13447 NETIF_MSG_LINK); 13448 if (debug < -1) 13449 pf->hw.debug_mask = debug; 13450 13451 /* do a special CORER for clearing PXE mode once at init */ 13452 if (hw->revision_id == 0 && 13453 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) { 13454 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK); 13455 i40e_flush(hw); 13456 msleep(200); 13457 pf->corer_count++; 13458 13459 i40e_clear_pxe_mode(hw); 13460 } 13461 13462 /* Reset here to make sure all is clean and to define PF 'n' */ 13463 i40e_clear_hw(hw); 13464 err = i40e_pf_reset(hw); 13465 if (err) { 13466 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err); 13467 goto err_pf_reset; 13468 } 13469 pf->pfr_count++; 13470 13471 hw->aq.num_arq_entries = I40E_AQ_LEN; 13472 hw->aq.num_asq_entries = I40E_AQ_LEN; 13473 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE; 13474 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE; 13475 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT; 13476 13477 snprintf(pf->int_name, sizeof(pf->int_name) - 1, 13478 "%s-%s:misc", 13479 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev)); 13480 13481 err = i40e_init_shared_code(hw); 13482 if (err) { 13483 dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n", 13484 err); 13485 goto err_pf_reset; 13486 } 13487 13488 /* set up a default setting for link flow control */ 13489 pf->hw.fc.requested_mode = I40E_FC_NONE; 13490 13491 err = i40e_init_adminq(hw); 13492 if (err) { 13493 if (err == I40E_ERR_FIRMWARE_API_VERSION) 13494 dev_info(&pdev->dev, 13495 "The driver for the device stopped because the NVM image is newer than expected. You must install the most recent version of the network driver.\n"); 13496 else 13497 dev_info(&pdev->dev, 13498 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n"); 13499 13500 goto err_pf_reset; 13501 } 13502 i40e_get_oem_version(hw); 13503 13504 /* provide nvm, fw, api versions */ 13505 dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s\n", 13506 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build, 13507 hw->aq.api_maj_ver, hw->aq.api_min_ver, 13508 i40e_nvm_version_str(hw)); 13509 13510 if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR && 13511 hw->aq.api_min_ver > I40E_FW_MINOR_VERSION(hw)) 13512 dev_info(&pdev->dev, 13513 "The driver for the device detected a newer version of the NVM image than expected. Please install the most recent version of the network driver.\n"); 13514 else if (hw->aq.api_maj_ver == 1 && hw->aq.api_min_ver < 4) 13515 dev_info(&pdev->dev, 13516 "The driver for the device detected an older version of the NVM image than expected. Please update the NVM image.\n"); 13517 13518 i40e_verify_eeprom(pf); 13519 13520 /* Rev 0 hardware was never productized */ 13521 if (hw->revision_id < 1) 13522 dev_warn(&pdev->dev, "This device is a pre-production adapter/LOM. Please be aware there may be issues with your hardware. If you are experiencing problems please contact your Intel or hardware representative who provided you with this hardware.\n"); 13523 13524 i40e_clear_pxe_mode(hw); 13525 err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities); 13526 if (err) 13527 goto err_adminq_setup; 13528 13529 err = i40e_sw_init(pf); 13530 if (err) { 13531 dev_info(&pdev->dev, "sw_init failed: %d\n", err); 13532 goto err_sw_init; 13533 } 13534 13535 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp, 13536 hw->func_caps.num_rx_qp, 0, 0); 13537 if (err) { 13538 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err); 13539 goto err_init_lan_hmc; 13540 } 13541 13542 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY); 13543 if (err) { 13544 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err); 13545 err = -ENOENT; 13546 goto err_configure_lan_hmc; 13547 } 13548 13549 /* Disable LLDP for NICs that have firmware versions lower than v4.3. 13550 * Ignore error return codes because if it was already disabled via 13551 * hardware settings this will fail 13552 */ 13553 if (pf->hw_features & I40E_HW_STOP_FW_LLDP) { 13554 dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n"); 13555 i40e_aq_stop_lldp(hw, true, NULL); 13556 } 13557 13558 /* allow a platform config to override the HW addr */ 13559 i40e_get_platform_mac_addr(pdev, pf); 13560 13561 if (!is_valid_ether_addr(hw->mac.addr)) { 13562 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr); 13563 err = -EIO; 13564 goto err_mac_addr; 13565 } 13566 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr); 13567 ether_addr_copy(hw->mac.perm_addr, hw->mac.addr); 13568 i40e_get_port_mac_addr(hw, hw->mac.port_addr); 13569 if (is_valid_ether_addr(hw->mac.port_addr)) 13570 pf->hw_features |= I40E_HW_PORT_ID_VALID; 13571 13572 pci_set_drvdata(pdev, pf); 13573 pci_save_state(pdev); 13574 13575 /* Enable FW to write default DCB config on link-up */ 13576 i40e_aq_set_dcb_parameters(hw, true, NULL); 13577 13578 #ifdef CONFIG_I40E_DCB 13579 err = i40e_init_pf_dcb(pf); 13580 if (err) { 13581 dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err); 13582 pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED); 13583 /* Continue without DCB enabled */ 13584 } 13585 #endif /* CONFIG_I40E_DCB */ 13586 13587 /* set up periodic task facility */ 13588 timer_setup(&pf->service_timer, i40e_service_timer, 0); 13589 pf->service_timer_period = HZ; 13590 13591 INIT_WORK(&pf->service_task, i40e_service_task); 13592 clear_bit(__I40E_SERVICE_SCHED, pf->state); 13593 13594 /* NVM bit on means WoL disabled for the port */ 13595 i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits); 13596 if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1) 13597 pf->wol_en = false; 13598 else 13599 pf->wol_en = true; 13600 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en); 13601 13602 /* set up the main switch operations */ 13603 i40e_determine_queue_usage(pf); 13604 err = i40e_init_interrupt_scheme(pf); 13605 if (err) 13606 goto err_switch_setup; 13607 13608 /* The number of VSIs reported by the FW is the minimum guaranteed 13609 * to us; HW supports far more and we share the remaining pool with 13610 * the other PFs. We allocate space for more than the guarantee with 13611 * the understanding that we might not get them all later. 13612 */ 13613 if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC) 13614 pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC; 13615 else 13616 pf->num_alloc_vsi = pf->hw.func_caps.num_vsis; 13617 13618 /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */ 13619 pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *), 13620 GFP_KERNEL); 13621 if (!pf->vsi) { 13622 err = -ENOMEM; 13623 goto err_switch_setup; 13624 } 13625 13626 #ifdef CONFIG_PCI_IOV 13627 /* prep for VF support */ 13628 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) && 13629 (pf->flags & I40E_FLAG_MSIX_ENABLED) && 13630 !test_bit(__I40E_BAD_EEPROM, pf->state)) { 13631 if (pci_num_vf(pdev)) 13632 pf->flags |= I40E_FLAG_VEB_MODE_ENABLED; 13633 } 13634 #endif 13635 err = i40e_setup_pf_switch(pf, false); 13636 if (err) { 13637 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err); 13638 goto err_vsis; 13639 } 13640 INIT_LIST_HEAD(&pf->vsi[pf->lan_vsi]->ch_list); 13641 13642 /* Make sure flow control is set according to current settings */ 13643 err = i40e_set_fc(hw, &set_fc_aq_fail, true); 13644 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET) 13645 dev_dbg(&pf->pdev->dev, 13646 "Set fc with err %s aq_err %s on get_phy_cap\n", 13647 i40e_stat_str(hw, err), 13648 i40e_aq_str(hw, hw->aq.asq_last_status)); 13649 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET) 13650 dev_dbg(&pf->pdev->dev, 13651 "Set fc with err %s aq_err %s on set_phy_config\n", 13652 i40e_stat_str(hw, err), 13653 i40e_aq_str(hw, hw->aq.asq_last_status)); 13654 if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE) 13655 dev_dbg(&pf->pdev->dev, 13656 "Set fc with err %s aq_err %s on get_link_info\n", 13657 i40e_stat_str(hw, err), 13658 i40e_aq_str(hw, hw->aq.asq_last_status)); 13659 13660 /* if FDIR VSI was set up, start it now */ 13661 for (i = 0; i < pf->num_alloc_vsi; i++) { 13662 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) { 13663 i40e_vsi_open(pf->vsi[i]); 13664 break; 13665 } 13666 } 13667 13668 /* The driver only wants link up/down and module qualification 13669 * reports from firmware. Note the negative logic. 13670 */ 13671 err = i40e_aq_set_phy_int_mask(&pf->hw, 13672 ~(I40E_AQ_EVENT_LINK_UPDOWN | 13673 I40E_AQ_EVENT_MEDIA_NA | 13674 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL); 13675 if (err) 13676 dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n", 13677 i40e_stat_str(&pf->hw, err), 13678 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status)); 13679 13680 /* Reconfigure hardware for allowing smaller MSS in the case 13681 * of TSO, so that we avoid the MDD being fired and causing 13682 * a reset in the case of small MSS+TSO. 13683 */ 13684 val = rd32(hw, I40E_REG_MSS); 13685 if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) { 13686 val &= ~I40E_REG_MSS_MIN_MASK; 13687 val |= I40E_64BYTE_MSS; 13688 wr32(hw, I40E_REG_MSS, val); 13689 } 13690 13691 if (pf->hw_features & I40E_HW_RESTART_AUTONEG) { 13692 msleep(75); 13693 err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL); 13694 if (err) 13695 dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n", 13696 i40e_stat_str(&pf->hw, err), 13697 i40e_aq_str(&pf->hw, 13698 pf->hw.aq.asq_last_status)); 13699 } 13700 /* The main driver is (mostly) up and happy. We need to set this state 13701 * before setting up the misc vector or we get a race and the vector 13702 * ends up disabled forever. 13703 */ 13704 clear_bit(__I40E_DOWN, pf->state); 13705 13706 /* In case of MSIX we are going to setup the misc vector right here 13707 * to handle admin queue events etc. In case of legacy and MSI 13708 * the misc functionality and queue processing is combined in 13709 * the same vector and that gets setup at open. 13710 */ 13711 if (pf->flags & I40E_FLAG_MSIX_ENABLED) { 13712 err = i40e_setup_misc_vector(pf); 13713 if (err) { 13714 dev_info(&pdev->dev, 13715 "setup of misc vector failed: %d\n", err); 13716 goto err_vsis; 13717 } 13718 } 13719 13720 #ifdef CONFIG_PCI_IOV 13721 /* prep for VF support */ 13722 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) && 13723 (pf->flags & I40E_FLAG_MSIX_ENABLED) && 13724 !test_bit(__I40E_BAD_EEPROM, pf->state)) { 13725 /* disable link interrupts for VFs */ 13726 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM); 13727 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK; 13728 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val); 13729 i40e_flush(hw); 13730 13731 if (pci_num_vf(pdev)) { 13732 dev_info(&pdev->dev, 13733 "Active VFs found, allocating resources.\n"); 13734 err = i40e_alloc_vfs(pf, pci_num_vf(pdev)); 13735 if (err) 13736 dev_info(&pdev->dev, 13737 "Error %d allocating resources for existing VFs\n", 13738 err); 13739 } 13740 } 13741 #endif /* CONFIG_PCI_IOV */ 13742 13743 if (pf->flags & I40E_FLAG_IWARP_ENABLED) { 13744 pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile, 13745 pf->num_iwarp_msix, 13746 I40E_IWARP_IRQ_PILE_ID); 13747 if (pf->iwarp_base_vector < 0) { 13748 dev_info(&pdev->dev, 13749 "failed to get tracking for %d vectors for IWARP err=%d\n", 13750 pf->num_iwarp_msix, pf->iwarp_base_vector); 13751 pf->flags &= ~I40E_FLAG_IWARP_ENABLED; 13752 } 13753 } 13754 13755 i40e_dbg_pf_init(pf); 13756 13757 /* tell the firmware that we're starting */ 13758 i40e_send_version(pf); 13759 13760 /* since everything's happy, start the service_task timer */ 13761 mod_timer(&pf->service_timer, 13762 round_jiffies(jiffies + pf->service_timer_period)); 13763 13764 /* add this PF to client device list and launch a client service task */ 13765 if (pf->flags & I40E_FLAG_IWARP_ENABLED) { 13766 err = i40e_lan_add_device(pf); 13767 if (err) 13768 dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n", 13769 err); 13770 } 13771 13772 #define PCI_SPEED_SIZE 8 13773 #define PCI_WIDTH_SIZE 8 13774 /* Devices on the IOSF bus do not have this information 13775 * and will report PCI Gen 1 x 1 by default so don't bother 13776 * checking them. 13777 */ 13778 if (!(pf->hw_features & I40E_HW_NO_PCI_LINK_CHECK)) { 13779 char speed[PCI_SPEED_SIZE] = "Unknown"; 13780 char width[PCI_WIDTH_SIZE] = "Unknown"; 13781 13782 /* Get the negotiated link width and speed from PCI config 13783 * space 13784 */ 13785 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA, 13786 &link_status); 13787 13788 i40e_set_pci_config_data(hw, link_status); 13789 13790 switch (hw->bus.speed) { 13791 case i40e_bus_speed_8000: 13792 strncpy(speed, "8.0", PCI_SPEED_SIZE); break; 13793 case i40e_bus_speed_5000: 13794 strncpy(speed, "5.0", PCI_SPEED_SIZE); break; 13795 case i40e_bus_speed_2500: 13796 strncpy(speed, "2.5", PCI_SPEED_SIZE); break; 13797 default: 13798 break; 13799 } 13800 switch (hw->bus.width) { 13801 case i40e_bus_width_pcie_x8: 13802 strncpy(width, "8", PCI_WIDTH_SIZE); break; 13803 case i40e_bus_width_pcie_x4: 13804 strncpy(width, "4", PCI_WIDTH_SIZE); break; 13805 case i40e_bus_width_pcie_x2: 13806 strncpy(width, "2", PCI_WIDTH_SIZE); break; 13807 case i40e_bus_width_pcie_x1: 13808 strncpy(width, "1", PCI_WIDTH_SIZE); break; 13809 default: 13810 break; 13811 } 13812 13813 dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n", 13814 speed, width); 13815 13816 if (hw->bus.width < i40e_bus_width_pcie_x8 || 13817 hw->bus.speed < i40e_bus_speed_8000) { 13818 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n"); 13819 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n"); 13820 } 13821 } 13822 13823 /* get the requested speeds from the fw */ 13824 err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL); 13825 if (err) 13826 dev_dbg(&pf->pdev->dev, "get requested speeds ret = %s last_status = %s\n", 13827 i40e_stat_str(&pf->hw, err), 13828 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status)); 13829 pf->hw.phy.link_info.requested_speeds = abilities.link_speed; 13830 13831 /* get the supported phy types from the fw */ 13832 err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL); 13833 if (err) 13834 dev_dbg(&pf->pdev->dev, "get supported phy types ret = %s last_status = %s\n", 13835 i40e_stat_str(&pf->hw, err), 13836 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status)); 13837 13838 /* Add a filter to drop all Flow control frames from any VSI from being 13839 * transmitted. By doing so we stop a malicious VF from sending out 13840 * PAUSE or PFC frames and potentially controlling traffic for other 13841 * PF/VF VSIs. 13842 * The FW can still send Flow control frames if enabled. 13843 */ 13844 i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw, 13845 pf->main_vsi_seid); 13846 13847 if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) || 13848 (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4)) 13849 pf->hw_features |= I40E_HW_PHY_CONTROLS_LEDS; 13850 if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722) 13851 pf->hw_features |= I40E_HW_HAVE_CRT_RETIMER; 13852 /* print a string summarizing features */ 13853 i40e_print_features(pf); 13854 13855 return 0; 13856 13857 /* Unwind what we've done if something failed in the setup */ 13858 err_vsis: 13859 set_bit(__I40E_DOWN, pf->state); 13860 i40e_clear_interrupt_scheme(pf); 13861 kfree(pf->vsi); 13862 err_switch_setup: 13863 i40e_reset_interrupt_capability(pf); 13864 del_timer_sync(&pf->service_timer); 13865 err_mac_addr: 13866 err_configure_lan_hmc: 13867 (void)i40e_shutdown_lan_hmc(hw); 13868 err_init_lan_hmc: 13869 kfree(pf->qp_pile); 13870 err_sw_init: 13871 err_adminq_setup: 13872 err_pf_reset: 13873 iounmap(hw->hw_addr); 13874 err_ioremap: 13875 kfree(pf); 13876 err_pf_alloc: 13877 pci_disable_pcie_error_reporting(pdev); 13878 pci_release_mem_regions(pdev); 13879 err_pci_reg: 13880 err_dma: 13881 pci_disable_device(pdev); 13882 return err; 13883 } 13884 13885 /** 13886 * i40e_remove - Device removal routine 13887 * @pdev: PCI device information struct 13888 * 13889 * i40e_remove is called by the PCI subsystem to alert the driver 13890 * that is should release a PCI device. This could be caused by a 13891 * Hot-Plug event, or because the driver is going to be removed from 13892 * memory. 13893 **/ 13894 static void i40e_remove(struct pci_dev *pdev) 13895 { 13896 struct i40e_pf *pf = pci_get_drvdata(pdev); 13897 struct i40e_hw *hw = &pf->hw; 13898 i40e_status ret_code; 13899 int i; 13900 13901 i40e_dbg_pf_exit(pf); 13902 13903 i40e_ptp_stop(pf); 13904 13905 /* Disable RSS in hw */ 13906 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0); 13907 i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0); 13908 13909 /* no more scheduling of any task */ 13910 set_bit(__I40E_SUSPENDED, pf->state); 13911 set_bit(__I40E_DOWN, pf->state); 13912 if (pf->service_timer.function) 13913 del_timer_sync(&pf->service_timer); 13914 if (pf->service_task.func) 13915 cancel_work_sync(&pf->service_task); 13916 13917 /* Client close must be called explicitly here because the timer 13918 * has been stopped. 13919 */ 13920 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false); 13921 13922 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) { 13923 i40e_free_vfs(pf); 13924 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED; 13925 } 13926 13927 i40e_fdir_teardown(pf); 13928 13929 /* If there is a switch structure or any orphans, remove them. 13930 * This will leave only the PF's VSI remaining. 13931 */ 13932 for (i = 0; i < I40E_MAX_VEB; i++) { 13933 if (!pf->veb[i]) 13934 continue; 13935 13936 if (pf->veb[i]->uplink_seid == pf->mac_seid || 13937 pf->veb[i]->uplink_seid == 0) 13938 i40e_switch_branch_release(pf->veb[i]); 13939 } 13940 13941 /* Now we can shutdown the PF's VSI, just before we kill 13942 * adminq and hmc. 13943 */ 13944 if (pf->vsi[pf->lan_vsi]) 13945 i40e_vsi_release(pf->vsi[pf->lan_vsi]); 13946 13947 i40e_cloud_filter_exit(pf); 13948 13949 /* remove attached clients */ 13950 if (pf->flags & I40E_FLAG_IWARP_ENABLED) { 13951 ret_code = i40e_lan_del_device(pf); 13952 if (ret_code) 13953 dev_warn(&pdev->dev, "Failed to delete client device: %d\n", 13954 ret_code); 13955 } 13956 13957 /* shutdown and destroy the HMC */ 13958 if (hw->hmc.hmc_obj) { 13959 ret_code = i40e_shutdown_lan_hmc(hw); 13960 if (ret_code) 13961 dev_warn(&pdev->dev, 13962 "Failed to destroy the HMC resources: %d\n", 13963 ret_code); 13964 } 13965 13966 /* shutdown the adminq */ 13967 i40e_shutdown_adminq(hw); 13968 13969 /* destroy the locks only once, here */ 13970 mutex_destroy(&hw->aq.arq_mutex); 13971 mutex_destroy(&hw->aq.asq_mutex); 13972 13973 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */ 13974 i40e_clear_interrupt_scheme(pf); 13975 for (i = 0; i < pf->num_alloc_vsi; i++) { 13976 if (pf->vsi[i]) { 13977 i40e_vsi_clear_rings(pf->vsi[i]); 13978 i40e_vsi_clear(pf->vsi[i]); 13979 pf->vsi[i] = NULL; 13980 } 13981 } 13982 13983 for (i = 0; i < I40E_MAX_VEB; i++) { 13984 kfree(pf->veb[i]); 13985 pf->veb[i] = NULL; 13986 } 13987 13988 kfree(pf->qp_pile); 13989 kfree(pf->vsi); 13990 13991 iounmap(hw->hw_addr); 13992 kfree(pf); 13993 pci_release_mem_regions(pdev); 13994 13995 pci_disable_pcie_error_reporting(pdev); 13996 pci_disable_device(pdev); 13997 } 13998 13999 /** 14000 * i40e_pci_error_detected - warning that something funky happened in PCI land 14001 * @pdev: PCI device information struct 14002 * 14003 * Called to warn that something happened and the error handling steps 14004 * are in progress. Allows the driver to quiesce things, be ready for 14005 * remediation. 14006 **/ 14007 static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev, 14008 enum pci_channel_state error) 14009 { 14010 struct i40e_pf *pf = pci_get_drvdata(pdev); 14011 14012 dev_info(&pdev->dev, "%s: error %d\n", __func__, error); 14013 14014 if (!pf) { 14015 dev_info(&pdev->dev, 14016 "Cannot recover - error happened during device probe\n"); 14017 return PCI_ERS_RESULT_DISCONNECT; 14018 } 14019 14020 /* shutdown all operations */ 14021 if (!test_bit(__I40E_SUSPENDED, pf->state)) 14022 i40e_prep_for_reset(pf, false); 14023 14024 /* Request a slot reset */ 14025 return PCI_ERS_RESULT_NEED_RESET; 14026 } 14027 14028 /** 14029 * i40e_pci_error_slot_reset - a PCI slot reset just happened 14030 * @pdev: PCI device information struct 14031 * 14032 * Called to find if the driver can work with the device now that 14033 * the pci slot has been reset. If a basic connection seems good 14034 * (registers are readable and have sane content) then return a 14035 * happy little PCI_ERS_RESULT_xxx. 14036 **/ 14037 static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev) 14038 { 14039 struct i40e_pf *pf = pci_get_drvdata(pdev); 14040 pci_ers_result_t result; 14041 int err; 14042 u32 reg; 14043 14044 dev_dbg(&pdev->dev, "%s\n", __func__); 14045 if (pci_enable_device_mem(pdev)) { 14046 dev_info(&pdev->dev, 14047 "Cannot re-enable PCI device after reset.\n"); 14048 result = PCI_ERS_RESULT_DISCONNECT; 14049 } else { 14050 pci_set_master(pdev); 14051 pci_restore_state(pdev); 14052 pci_save_state(pdev); 14053 pci_wake_from_d3(pdev, false); 14054 14055 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG); 14056 if (reg == 0) 14057 result = PCI_ERS_RESULT_RECOVERED; 14058 else 14059 result = PCI_ERS_RESULT_DISCONNECT; 14060 } 14061 14062 err = pci_cleanup_aer_uncorrect_error_status(pdev); 14063 if (err) { 14064 dev_info(&pdev->dev, 14065 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n", 14066 err); 14067 /* non-fatal, continue */ 14068 } 14069 14070 return result; 14071 } 14072 14073 /** 14074 * i40e_pci_error_reset_prepare - prepare device driver for pci reset 14075 * @pdev: PCI device information struct 14076 */ 14077 static void i40e_pci_error_reset_prepare(struct pci_dev *pdev) 14078 { 14079 struct i40e_pf *pf = pci_get_drvdata(pdev); 14080 14081 i40e_prep_for_reset(pf, false); 14082 } 14083 14084 /** 14085 * i40e_pci_error_reset_done - pci reset done, device driver reset can begin 14086 * @pdev: PCI device information struct 14087 */ 14088 static void i40e_pci_error_reset_done(struct pci_dev *pdev) 14089 { 14090 struct i40e_pf *pf = pci_get_drvdata(pdev); 14091 14092 i40e_reset_and_rebuild(pf, false, false); 14093 } 14094 14095 /** 14096 * i40e_pci_error_resume - restart operations after PCI error recovery 14097 * @pdev: PCI device information struct 14098 * 14099 * Called to allow the driver to bring things back up after PCI error 14100 * and/or reset recovery has finished. 14101 **/ 14102 static void i40e_pci_error_resume(struct pci_dev *pdev) 14103 { 14104 struct i40e_pf *pf = pci_get_drvdata(pdev); 14105 14106 dev_dbg(&pdev->dev, "%s\n", __func__); 14107 if (test_bit(__I40E_SUSPENDED, pf->state)) 14108 return; 14109 14110 i40e_handle_reset_warning(pf, false); 14111 } 14112 14113 /** 14114 * i40e_enable_mc_magic_wake - enable multicast magic packet wake up 14115 * using the mac_address_write admin q function 14116 * @pf: pointer to i40e_pf struct 14117 **/ 14118 static void i40e_enable_mc_magic_wake(struct i40e_pf *pf) 14119 { 14120 struct i40e_hw *hw = &pf->hw; 14121 i40e_status ret; 14122 u8 mac_addr[6]; 14123 u16 flags = 0; 14124 14125 /* Get current MAC address in case it's an LAA */ 14126 if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) { 14127 ether_addr_copy(mac_addr, 14128 pf->vsi[pf->lan_vsi]->netdev->dev_addr); 14129 } else { 14130 dev_err(&pf->pdev->dev, 14131 "Failed to retrieve MAC address; using default\n"); 14132 ether_addr_copy(mac_addr, hw->mac.addr); 14133 } 14134 14135 /* The FW expects the mac address write cmd to first be called with 14136 * one of these flags before calling it again with the multicast 14137 * enable flags. 14138 */ 14139 flags = I40E_AQC_WRITE_TYPE_LAA_WOL; 14140 14141 if (hw->func_caps.flex10_enable && hw->partition_id != 1) 14142 flags = I40E_AQC_WRITE_TYPE_LAA_ONLY; 14143 14144 ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL); 14145 if (ret) { 14146 dev_err(&pf->pdev->dev, 14147 "Failed to update MAC address registers; cannot enable Multicast Magic packet wake up"); 14148 return; 14149 } 14150 14151 flags = I40E_AQC_MC_MAG_EN 14152 | I40E_AQC_WOL_PRESERVE_ON_PFR 14153 | I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG; 14154 ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL); 14155 if (ret) 14156 dev_err(&pf->pdev->dev, 14157 "Failed to enable Multicast Magic Packet wake up\n"); 14158 } 14159 14160 /** 14161 * i40e_shutdown - PCI callback for shutting down 14162 * @pdev: PCI device information struct 14163 **/ 14164 static void i40e_shutdown(struct pci_dev *pdev) 14165 { 14166 struct i40e_pf *pf = pci_get_drvdata(pdev); 14167 struct i40e_hw *hw = &pf->hw; 14168 14169 set_bit(__I40E_SUSPENDED, pf->state); 14170 set_bit(__I40E_DOWN, pf->state); 14171 rtnl_lock(); 14172 i40e_prep_for_reset(pf, true); 14173 rtnl_unlock(); 14174 14175 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0)); 14176 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0)); 14177 14178 del_timer_sync(&pf->service_timer); 14179 cancel_work_sync(&pf->service_task); 14180 i40e_cloud_filter_exit(pf); 14181 i40e_fdir_teardown(pf); 14182 14183 /* Client close must be called explicitly here because the timer 14184 * has been stopped. 14185 */ 14186 i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false); 14187 14188 if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE)) 14189 i40e_enable_mc_magic_wake(pf); 14190 14191 i40e_prep_for_reset(pf, false); 14192 14193 wr32(hw, I40E_PFPM_APM, 14194 (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0)); 14195 wr32(hw, I40E_PFPM_WUFC, 14196 (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0)); 14197 14198 i40e_clear_interrupt_scheme(pf); 14199 14200 if (system_state == SYSTEM_POWER_OFF) { 14201 pci_wake_from_d3(pdev, pf->wol_en); 14202 pci_set_power_state(pdev, PCI_D3hot); 14203 } 14204 } 14205 14206 /** 14207 * i40e_suspend - PM callback for moving to D3 14208 * @dev: generic device information structure 14209 **/ 14210 static int __maybe_unused i40e_suspend(struct device *dev) 14211 { 14212 struct pci_dev *pdev = to_pci_dev(dev); 14213 struct i40e_pf *pf = pci_get_drvdata(pdev); 14214 struct i40e_hw *hw = &pf->hw; 14215 14216 /* If we're already suspended, then there is nothing to do */ 14217 if (test_and_set_bit(__I40E_SUSPENDED, pf->state)) 14218 return 0; 14219 14220 set_bit(__I40E_DOWN, pf->state); 14221 14222 /* Ensure service task will not be running */ 14223 del_timer_sync(&pf->service_timer); 14224 cancel_work_sync(&pf->service_task); 14225 14226 if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE)) 14227 i40e_enable_mc_magic_wake(pf); 14228 14229 i40e_prep_for_reset(pf, false); 14230 14231 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0)); 14232 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0)); 14233 14234 /* Clear the interrupt scheme and release our IRQs so that the system 14235 * can safely hibernate even when there are a large number of CPUs. 14236 * Otherwise hibernation might fail when mapping all the vectors back 14237 * to CPU0. 14238 */ 14239 i40e_clear_interrupt_scheme(pf); 14240 14241 return 0; 14242 } 14243 14244 /** 14245 * i40e_resume - PM callback for waking up from D3 14246 * @dev: generic device information structure 14247 **/ 14248 static int __maybe_unused i40e_resume(struct device *dev) 14249 { 14250 struct pci_dev *pdev = to_pci_dev(dev); 14251 struct i40e_pf *pf = pci_get_drvdata(pdev); 14252 int err; 14253 14254 /* If we're not suspended, then there is nothing to do */ 14255 if (!test_bit(__I40E_SUSPENDED, pf->state)) 14256 return 0; 14257 14258 /* We cleared the interrupt scheme when we suspended, so we need to 14259 * restore it now to resume device functionality. 14260 */ 14261 err = i40e_restore_interrupt_scheme(pf); 14262 if (err) { 14263 dev_err(&pdev->dev, "Cannot restore interrupt scheme: %d\n", 14264 err); 14265 } 14266 14267 clear_bit(__I40E_DOWN, pf->state); 14268 i40e_reset_and_rebuild(pf, false, false); 14269 14270 /* Clear suspended state last after everything is recovered */ 14271 clear_bit(__I40E_SUSPENDED, pf->state); 14272 14273 /* Restart the service task */ 14274 mod_timer(&pf->service_timer, 14275 round_jiffies(jiffies + pf->service_timer_period)); 14276 14277 return 0; 14278 } 14279 14280 static const struct pci_error_handlers i40e_err_handler = { 14281 .error_detected = i40e_pci_error_detected, 14282 .slot_reset = i40e_pci_error_slot_reset, 14283 .reset_prepare = i40e_pci_error_reset_prepare, 14284 .reset_done = i40e_pci_error_reset_done, 14285 .resume = i40e_pci_error_resume, 14286 }; 14287 14288 static SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume); 14289 14290 static struct pci_driver i40e_driver = { 14291 .name = i40e_driver_name, 14292 .id_table = i40e_pci_tbl, 14293 .probe = i40e_probe, 14294 .remove = i40e_remove, 14295 .driver = { 14296 .pm = &i40e_pm_ops, 14297 }, 14298 .shutdown = i40e_shutdown, 14299 .err_handler = &i40e_err_handler, 14300 .sriov_configure = i40e_pci_sriov_configure, 14301 }; 14302 14303 /** 14304 * i40e_init_module - Driver registration routine 14305 * 14306 * i40e_init_module is the first routine called when the driver is 14307 * loaded. All it does is register with the PCI subsystem. 14308 **/ 14309 static int __init i40e_init_module(void) 14310 { 14311 pr_info("%s: %s - version %s\n", i40e_driver_name, 14312 i40e_driver_string, i40e_driver_version_str); 14313 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright); 14314 14315 /* There is no need to throttle the number of active tasks because 14316 * each device limits its own task using a state bit for scheduling 14317 * the service task, and the device tasks do not interfere with each 14318 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM 14319 * since we need to be able to guarantee forward progress even under 14320 * memory pressure. 14321 */ 14322 i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name); 14323 if (!i40e_wq) { 14324 pr_err("%s: Failed to create workqueue\n", i40e_driver_name); 14325 return -ENOMEM; 14326 } 14327 14328 i40e_dbg_init(); 14329 return pci_register_driver(&i40e_driver); 14330 } 14331 module_init(i40e_init_module); 14332 14333 /** 14334 * i40e_exit_module - Driver exit cleanup routine 14335 * 14336 * i40e_exit_module is called just before the driver is removed 14337 * from memory. 14338 **/ 14339 static void __exit i40e_exit_module(void) 14340 { 14341 pci_unregister_driver(&i40e_driver); 14342 destroy_workqueue(i40e_wq); 14343 i40e_dbg_exit(); 14344 } 14345 module_exit(i40e_exit_module); 14346