1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright(c) 2007 - 2018 Intel Corporation. */ 3 4 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 5 6 #include <linux/module.h> 7 #include <linux/types.h> 8 #include <linux/init.h> 9 #include <linux/bitops.h> 10 #include <linux/vmalloc.h> 11 #include <linux/pagemap.h> 12 #include <linux/netdevice.h> 13 #include <linux/ipv6.h> 14 #include <linux/slab.h> 15 #include <net/checksum.h> 16 #include <net/ip6_checksum.h> 17 #include <net/pkt_sched.h> 18 #include <net/pkt_cls.h> 19 #include <linux/net_tstamp.h> 20 #include <linux/mii.h> 21 #include <linux/ethtool.h> 22 #include <linux/if.h> 23 #include <linux/if_vlan.h> 24 #include <linux/pci.h> 25 #include <linux/delay.h> 26 #include <linux/interrupt.h> 27 #include <linux/ip.h> 28 #include <linux/tcp.h> 29 #include <linux/sctp.h> 30 #include <linux/if_ether.h> 31 #include <linux/prefetch.h> 32 #include <linux/bpf.h> 33 #include <linux/bpf_trace.h> 34 #include <linux/pm_runtime.h> 35 #include <linux/etherdevice.h> 36 #ifdef CONFIG_IGB_DCA 37 #include <linux/dca.h> 38 #endif 39 #include <linux/i2c.h> 40 #include "igb.h" 41 42 enum queue_mode { 43 QUEUE_MODE_STRICT_PRIORITY, 44 QUEUE_MODE_STREAM_RESERVATION, 45 }; 46 47 enum tx_queue_prio { 48 TX_QUEUE_PRIO_HIGH, 49 TX_QUEUE_PRIO_LOW, 50 }; 51 52 char igb_driver_name[] = "igb"; 53 static const char igb_driver_string[] = 54 "Intel(R) Gigabit Ethernet Network Driver"; 55 static const char igb_copyright[] = 56 "Copyright (c) 2007-2014 Intel Corporation."; 57 58 static const struct e1000_info *igb_info_tbl[] = { 59 [board_82575] = &e1000_82575_info, 60 }; 61 62 static const struct pci_device_id igb_pci_tbl[] = { 63 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I354_BACKPLANE_1GBPS) }, 64 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I354_SGMII) }, 65 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I354_BACKPLANE_2_5GBPS) }, 66 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I211_COPPER), board_82575 }, 67 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I210_COPPER), board_82575 }, 68 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I210_FIBER), board_82575 }, 69 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I210_SERDES), board_82575 }, 70 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I210_SGMII), board_82575 }, 71 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I210_COPPER_FLASHLESS), board_82575 }, 72 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I210_SERDES_FLASHLESS), board_82575 }, 73 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I350_COPPER), board_82575 }, 74 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I350_FIBER), board_82575 }, 75 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I350_SERDES), board_82575 }, 76 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I350_SGMII), board_82575 }, 77 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_COPPER), board_82575 }, 78 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_FIBER), board_82575 }, 79 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_QUAD_FIBER), board_82575 }, 80 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_SERDES), board_82575 }, 81 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_SGMII), board_82575 }, 82 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_COPPER_DUAL), board_82575 }, 83 { PCI_VDEVICE(INTEL, E1000_DEV_ID_DH89XXCC_SGMII), board_82575 }, 84 { PCI_VDEVICE(INTEL, E1000_DEV_ID_DH89XXCC_SERDES), board_82575 }, 85 { PCI_VDEVICE(INTEL, E1000_DEV_ID_DH89XXCC_BACKPLANE), board_82575 }, 86 { PCI_VDEVICE(INTEL, E1000_DEV_ID_DH89XXCC_SFP), board_82575 }, 87 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576), board_82575 }, 88 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_NS), board_82575 }, 89 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_NS_SERDES), board_82575 }, 90 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_FIBER), board_82575 }, 91 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_SERDES), board_82575 }, 92 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_SERDES_QUAD), board_82575 }, 93 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_QUAD_COPPER_ET2), board_82575 }, 94 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_QUAD_COPPER), board_82575 }, 95 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82575EB_COPPER), board_82575 }, 96 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82575EB_FIBER_SERDES), board_82575 }, 97 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82575GB_QUAD_COPPER), board_82575 }, 98 /* required last entry */ 99 {0, } 100 }; 101 102 MODULE_DEVICE_TABLE(pci, igb_pci_tbl); 103 104 static int igb_setup_all_tx_resources(struct igb_adapter *); 105 static int igb_setup_all_rx_resources(struct igb_adapter *); 106 static void igb_free_all_tx_resources(struct igb_adapter *); 107 static void igb_free_all_rx_resources(struct igb_adapter *); 108 static void igb_setup_mrqc(struct igb_adapter *); 109 static void igb_init_queue_configuration(struct igb_adapter *adapter); 110 static int igb_sw_init(struct igb_adapter *); 111 int igb_open(struct net_device *); 112 int igb_close(struct net_device *); 113 static void igb_configure(struct igb_adapter *); 114 static void igb_configure_tx(struct igb_adapter *); 115 static void igb_configure_rx(struct igb_adapter *); 116 static void igb_clean_all_tx_rings(struct igb_adapter *); 117 static void igb_clean_all_rx_rings(struct igb_adapter *); 118 static void igb_clean_tx_ring(struct igb_ring *); 119 static void igb_clean_rx_ring(struct igb_ring *); 120 static void igb_set_rx_mode(struct net_device *); 121 static void igb_update_phy_info(struct timer_list *); 122 static void igb_watchdog(struct timer_list *); 123 static void igb_watchdog_task(struct work_struct *); 124 static netdev_tx_t igb_xmit_frame(struct sk_buff *skb, struct net_device *); 125 static void igb_get_stats64(struct net_device *dev, 126 struct rtnl_link_stats64 *stats); 127 static int igb_change_mtu(struct net_device *, int); 128 static int igb_set_mac(struct net_device *, void *); 129 static void igb_set_uta(struct igb_adapter *adapter, bool set); 130 static irqreturn_t igb_intr(int irq, void *); 131 static irqreturn_t igb_intr_msi(int irq, void *); 132 static irqreturn_t igb_msix_other(int irq, void *); 133 static irqreturn_t igb_msix_ring(int irq, void *); 134 #ifdef CONFIG_IGB_DCA 135 static void igb_update_dca(struct igb_q_vector *); 136 static void igb_setup_dca(struct igb_adapter *); 137 #endif /* CONFIG_IGB_DCA */ 138 static int igb_poll(struct napi_struct *, int); 139 static bool igb_clean_tx_irq(struct igb_q_vector *, int); 140 static int igb_clean_rx_irq(struct igb_q_vector *, int); 141 static int igb_ioctl(struct net_device *, struct ifreq *, int cmd); 142 static void igb_tx_timeout(struct net_device *, unsigned int txqueue); 143 static void igb_reset_task(struct work_struct *); 144 static void igb_vlan_mode(struct net_device *netdev, 145 netdev_features_t features); 146 static int igb_vlan_rx_add_vid(struct net_device *, __be16, u16); 147 static int igb_vlan_rx_kill_vid(struct net_device *, __be16, u16); 148 static void igb_restore_vlan(struct igb_adapter *); 149 static void igb_rar_set_index(struct igb_adapter *, u32); 150 static void igb_ping_all_vfs(struct igb_adapter *); 151 static void igb_msg_task(struct igb_adapter *); 152 static void igb_vmm_control(struct igb_adapter *); 153 static int igb_set_vf_mac(struct igb_adapter *, int, unsigned char *); 154 static void igb_flush_mac_table(struct igb_adapter *); 155 static int igb_available_rars(struct igb_adapter *, u8); 156 static void igb_set_default_mac_filter(struct igb_adapter *); 157 static int igb_uc_sync(struct net_device *, const unsigned char *); 158 static int igb_uc_unsync(struct net_device *, const unsigned char *); 159 static void igb_restore_vf_multicasts(struct igb_adapter *adapter); 160 static int igb_ndo_set_vf_mac(struct net_device *netdev, int vf, u8 *mac); 161 static int igb_ndo_set_vf_vlan(struct net_device *netdev, 162 int vf, u16 vlan, u8 qos, __be16 vlan_proto); 163 static int igb_ndo_set_vf_bw(struct net_device *, int, int, int); 164 static int igb_ndo_set_vf_spoofchk(struct net_device *netdev, int vf, 165 bool setting); 166 static int igb_ndo_set_vf_trust(struct net_device *netdev, int vf, 167 bool setting); 168 static int igb_ndo_get_vf_config(struct net_device *netdev, int vf, 169 struct ifla_vf_info *ivi); 170 static void igb_check_vf_rate_limit(struct igb_adapter *); 171 static void igb_nfc_filter_exit(struct igb_adapter *adapter); 172 static void igb_nfc_filter_restore(struct igb_adapter *adapter); 173 174 #ifdef CONFIG_PCI_IOV 175 static int igb_vf_configure(struct igb_adapter *adapter, int vf); 176 static int igb_disable_sriov(struct pci_dev *dev, bool reinit); 177 #endif 178 179 #ifdef CONFIG_IGB_DCA 180 static int igb_notify_dca(struct notifier_block *, unsigned long, void *); 181 static struct notifier_block dca_notifier = { 182 .notifier_call = igb_notify_dca, 183 .next = NULL, 184 .priority = 0 185 }; 186 #endif 187 #ifdef CONFIG_PCI_IOV 188 static unsigned int max_vfs; 189 module_param(max_vfs, uint, 0444); 190 MODULE_PARM_DESC(max_vfs, "Maximum number of virtual functions to allocate per physical function"); 191 #endif /* CONFIG_PCI_IOV */ 192 193 static pci_ers_result_t igb_io_error_detected(struct pci_dev *, 194 pci_channel_state_t); 195 static pci_ers_result_t igb_io_slot_reset(struct pci_dev *); 196 static void igb_io_resume(struct pci_dev *); 197 198 static const struct pci_error_handlers igb_err_handler = { 199 .error_detected = igb_io_error_detected, 200 .slot_reset = igb_io_slot_reset, 201 .resume = igb_io_resume, 202 }; 203 204 static void igb_init_dmac(struct igb_adapter *adapter, u32 pba); 205 206 MODULE_DESCRIPTION("Intel(R) Gigabit Ethernet Network Driver"); 207 MODULE_LICENSE("GPL v2"); 208 209 #define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK) 210 static int debug = -1; 211 module_param(debug, int, 0); 212 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)"); 213 214 struct igb_reg_info { 215 u32 ofs; 216 char *name; 217 }; 218 219 static const struct igb_reg_info igb_reg_info_tbl[] = { 220 221 /* General Registers */ 222 {E1000_CTRL, "CTRL"}, 223 {E1000_STATUS, "STATUS"}, 224 {E1000_CTRL_EXT, "CTRL_EXT"}, 225 226 /* Interrupt Registers */ 227 {E1000_ICR, "ICR"}, 228 229 /* RX Registers */ 230 {E1000_RCTL, "RCTL"}, 231 {E1000_RDLEN(0), "RDLEN"}, 232 {E1000_RDH(0), "RDH"}, 233 {E1000_RDT(0), "RDT"}, 234 {E1000_RXDCTL(0), "RXDCTL"}, 235 {E1000_RDBAL(0), "RDBAL"}, 236 {E1000_RDBAH(0), "RDBAH"}, 237 238 /* TX Registers */ 239 {E1000_TCTL, "TCTL"}, 240 {E1000_TDBAL(0), "TDBAL"}, 241 {E1000_TDBAH(0), "TDBAH"}, 242 {E1000_TDLEN(0), "TDLEN"}, 243 {E1000_TDH(0), "TDH"}, 244 {E1000_TDT(0), "TDT"}, 245 {E1000_TXDCTL(0), "TXDCTL"}, 246 {E1000_TDFH, "TDFH"}, 247 {E1000_TDFT, "TDFT"}, 248 {E1000_TDFHS, "TDFHS"}, 249 {E1000_TDFPC, "TDFPC"}, 250 251 /* List Terminator */ 252 {} 253 }; 254 255 /* igb_regdump - register printout routine */ 256 static void igb_regdump(struct e1000_hw *hw, struct igb_reg_info *reginfo) 257 { 258 int n = 0; 259 char rname[16]; 260 u32 regs[8]; 261 262 switch (reginfo->ofs) { 263 case E1000_RDLEN(0): 264 for (n = 0; n < 4; n++) 265 regs[n] = rd32(E1000_RDLEN(n)); 266 break; 267 case E1000_RDH(0): 268 for (n = 0; n < 4; n++) 269 regs[n] = rd32(E1000_RDH(n)); 270 break; 271 case E1000_RDT(0): 272 for (n = 0; n < 4; n++) 273 regs[n] = rd32(E1000_RDT(n)); 274 break; 275 case E1000_RXDCTL(0): 276 for (n = 0; n < 4; n++) 277 regs[n] = rd32(E1000_RXDCTL(n)); 278 break; 279 case E1000_RDBAL(0): 280 for (n = 0; n < 4; n++) 281 regs[n] = rd32(E1000_RDBAL(n)); 282 break; 283 case E1000_RDBAH(0): 284 for (n = 0; n < 4; n++) 285 regs[n] = rd32(E1000_RDBAH(n)); 286 break; 287 case E1000_TDBAL(0): 288 for (n = 0; n < 4; n++) 289 regs[n] = rd32(E1000_TDBAL(n)); 290 break; 291 case E1000_TDBAH(0): 292 for (n = 0; n < 4; n++) 293 regs[n] = rd32(E1000_TDBAH(n)); 294 break; 295 case E1000_TDLEN(0): 296 for (n = 0; n < 4; n++) 297 regs[n] = rd32(E1000_TDLEN(n)); 298 break; 299 case E1000_TDH(0): 300 for (n = 0; n < 4; n++) 301 regs[n] = rd32(E1000_TDH(n)); 302 break; 303 case E1000_TDT(0): 304 for (n = 0; n < 4; n++) 305 regs[n] = rd32(E1000_TDT(n)); 306 break; 307 case E1000_TXDCTL(0): 308 for (n = 0; n < 4; n++) 309 regs[n] = rd32(E1000_TXDCTL(n)); 310 break; 311 default: 312 pr_info("%-15s %08x\n", reginfo->name, rd32(reginfo->ofs)); 313 return; 314 } 315 316 snprintf(rname, 16, "%s%s", reginfo->name, "[0-3]"); 317 pr_info("%-15s %08x %08x %08x %08x\n", rname, regs[0], regs[1], 318 regs[2], regs[3]); 319 } 320 321 /* igb_dump - Print registers, Tx-rings and Rx-rings */ 322 static void igb_dump(struct igb_adapter *adapter) 323 { 324 struct net_device *netdev = adapter->netdev; 325 struct e1000_hw *hw = &adapter->hw; 326 struct igb_reg_info *reginfo; 327 struct igb_ring *tx_ring; 328 union e1000_adv_tx_desc *tx_desc; 329 struct my_u0 { __le64 a; __le64 b; } *u0; 330 struct igb_ring *rx_ring; 331 union e1000_adv_rx_desc *rx_desc; 332 u32 staterr; 333 u16 i, n; 334 335 if (!netif_msg_hw(adapter)) 336 return; 337 338 /* Print netdevice Info */ 339 if (netdev) { 340 dev_info(&adapter->pdev->dev, "Net device Info\n"); 341 pr_info("Device Name state trans_start\n"); 342 pr_info("%-15s %016lX %016lX\n", netdev->name, 343 netdev->state, dev_trans_start(netdev)); 344 } 345 346 /* Print Registers */ 347 dev_info(&adapter->pdev->dev, "Register Dump\n"); 348 pr_info(" Register Name Value\n"); 349 for (reginfo = (struct igb_reg_info *)igb_reg_info_tbl; 350 reginfo->name; reginfo++) { 351 igb_regdump(hw, reginfo); 352 } 353 354 /* Print TX Ring Summary */ 355 if (!netdev || !netif_running(netdev)) 356 goto exit; 357 358 dev_info(&adapter->pdev->dev, "TX Rings Summary\n"); 359 pr_info("Queue [NTU] [NTC] [bi(ntc)->dma ] leng ntw timestamp\n"); 360 for (n = 0; n < adapter->num_tx_queues; n++) { 361 struct igb_tx_buffer *buffer_info; 362 tx_ring = adapter->tx_ring[n]; 363 buffer_info = &tx_ring->tx_buffer_info[tx_ring->next_to_clean]; 364 pr_info(" %5d %5X %5X %016llX %04X %p %016llX\n", 365 n, tx_ring->next_to_use, tx_ring->next_to_clean, 366 (u64)dma_unmap_addr(buffer_info, dma), 367 dma_unmap_len(buffer_info, len), 368 buffer_info->next_to_watch, 369 (u64)buffer_info->time_stamp); 370 } 371 372 /* Print TX Rings */ 373 if (!netif_msg_tx_done(adapter)) 374 goto rx_ring_summary; 375 376 dev_info(&adapter->pdev->dev, "TX Rings Dump\n"); 377 378 /* Transmit Descriptor Formats 379 * 380 * Advanced Transmit Descriptor 381 * +--------------------------------------------------------------+ 382 * 0 | Buffer Address [63:0] | 383 * +--------------------------------------------------------------+ 384 * 8 | PAYLEN | PORTS |CC|IDX | STA | DCMD |DTYP|MAC|RSV| DTALEN | 385 * +--------------------------------------------------------------+ 386 * 63 46 45 40 39 38 36 35 32 31 24 15 0 387 */ 388 389 for (n = 0; n < adapter->num_tx_queues; n++) { 390 tx_ring = adapter->tx_ring[n]; 391 pr_info("------------------------------------\n"); 392 pr_info("TX QUEUE INDEX = %d\n", tx_ring->queue_index); 393 pr_info("------------------------------------\n"); 394 pr_info("T [desc] [address 63:0 ] [PlPOCIStDDM Ln] [bi->dma ] leng ntw timestamp bi->skb\n"); 395 396 for (i = 0; tx_ring->desc && (i < tx_ring->count); i++) { 397 const char *next_desc; 398 struct igb_tx_buffer *buffer_info; 399 tx_desc = IGB_TX_DESC(tx_ring, i); 400 buffer_info = &tx_ring->tx_buffer_info[i]; 401 u0 = (struct my_u0 *)tx_desc; 402 if (i == tx_ring->next_to_use && 403 i == tx_ring->next_to_clean) 404 next_desc = " NTC/U"; 405 else if (i == tx_ring->next_to_use) 406 next_desc = " NTU"; 407 else if (i == tx_ring->next_to_clean) 408 next_desc = " NTC"; 409 else 410 next_desc = ""; 411 412 pr_info("T [0x%03X] %016llX %016llX %016llX %04X %p %016llX %p%s\n", 413 i, le64_to_cpu(u0->a), 414 le64_to_cpu(u0->b), 415 (u64)dma_unmap_addr(buffer_info, dma), 416 dma_unmap_len(buffer_info, len), 417 buffer_info->next_to_watch, 418 (u64)buffer_info->time_stamp, 419 buffer_info->skb, next_desc); 420 421 if (netif_msg_pktdata(adapter) && buffer_info->skb) 422 print_hex_dump(KERN_INFO, "", 423 DUMP_PREFIX_ADDRESS, 424 16, 1, buffer_info->skb->data, 425 dma_unmap_len(buffer_info, len), 426 true); 427 } 428 } 429 430 /* Print RX Rings Summary */ 431 rx_ring_summary: 432 dev_info(&adapter->pdev->dev, "RX Rings Summary\n"); 433 pr_info("Queue [NTU] [NTC]\n"); 434 for (n = 0; n < adapter->num_rx_queues; n++) { 435 rx_ring = adapter->rx_ring[n]; 436 pr_info(" %5d %5X %5X\n", 437 n, rx_ring->next_to_use, rx_ring->next_to_clean); 438 } 439 440 /* Print RX Rings */ 441 if (!netif_msg_rx_status(adapter)) 442 goto exit; 443 444 dev_info(&adapter->pdev->dev, "RX Rings Dump\n"); 445 446 /* Advanced Receive Descriptor (Read) Format 447 * 63 1 0 448 * +-----------------------------------------------------+ 449 * 0 | Packet Buffer Address [63:1] |A0/NSE| 450 * +----------------------------------------------+------+ 451 * 8 | Header Buffer Address [63:1] | DD | 452 * +-----------------------------------------------------+ 453 * 454 * 455 * Advanced Receive Descriptor (Write-Back) Format 456 * 457 * 63 48 47 32 31 30 21 20 17 16 4 3 0 458 * +------------------------------------------------------+ 459 * 0 | Packet IP |SPH| HDR_LEN | RSV|Packet| RSS | 460 * | Checksum Ident | | | | Type | Type | 461 * +------------------------------------------------------+ 462 * 8 | VLAN Tag | Length | Extended Error | Extended Status | 463 * +------------------------------------------------------+ 464 * 63 48 47 32 31 20 19 0 465 */ 466 467 for (n = 0; n < adapter->num_rx_queues; n++) { 468 rx_ring = adapter->rx_ring[n]; 469 pr_info("------------------------------------\n"); 470 pr_info("RX QUEUE INDEX = %d\n", rx_ring->queue_index); 471 pr_info("------------------------------------\n"); 472 pr_info("R [desc] [ PktBuf A0] [ HeadBuf DD] [bi->dma ] [bi->skb] <-- Adv Rx Read format\n"); 473 pr_info("RWB[desc] [PcsmIpSHl PtRs] [vl er S cks ln] ---------------- [bi->skb] <-- Adv Rx Write-Back format\n"); 474 475 for (i = 0; i < rx_ring->count; i++) { 476 const char *next_desc; 477 struct igb_rx_buffer *buffer_info; 478 buffer_info = &rx_ring->rx_buffer_info[i]; 479 rx_desc = IGB_RX_DESC(rx_ring, i); 480 u0 = (struct my_u0 *)rx_desc; 481 staterr = le32_to_cpu(rx_desc->wb.upper.status_error); 482 483 if (i == rx_ring->next_to_use) 484 next_desc = " NTU"; 485 else if (i == rx_ring->next_to_clean) 486 next_desc = " NTC"; 487 else 488 next_desc = ""; 489 490 if (staterr & E1000_RXD_STAT_DD) { 491 /* Descriptor Done */ 492 pr_info("%s[0x%03X] %016llX %016llX ---------------- %s\n", 493 "RWB", i, 494 le64_to_cpu(u0->a), 495 le64_to_cpu(u0->b), 496 next_desc); 497 } else { 498 pr_info("%s[0x%03X] %016llX %016llX %016llX %s\n", 499 "R ", i, 500 le64_to_cpu(u0->a), 501 le64_to_cpu(u0->b), 502 (u64)buffer_info->dma, 503 next_desc); 504 505 if (netif_msg_pktdata(adapter) && 506 buffer_info->dma && buffer_info->page) { 507 print_hex_dump(KERN_INFO, "", 508 DUMP_PREFIX_ADDRESS, 509 16, 1, 510 page_address(buffer_info->page) + 511 buffer_info->page_offset, 512 igb_rx_bufsz(rx_ring), true); 513 } 514 } 515 } 516 } 517 518 exit: 519 return; 520 } 521 522 /** 523 * igb_get_i2c_data - Reads the I2C SDA data bit 524 * @data: opaque pointer to adapter struct 525 * 526 * Returns the I2C data bit value 527 **/ 528 static int igb_get_i2c_data(void *data) 529 { 530 struct igb_adapter *adapter = (struct igb_adapter *)data; 531 struct e1000_hw *hw = &adapter->hw; 532 s32 i2cctl = rd32(E1000_I2CPARAMS); 533 534 return !!(i2cctl & E1000_I2C_DATA_IN); 535 } 536 537 /** 538 * igb_set_i2c_data - Sets the I2C data bit 539 * @data: pointer to hardware structure 540 * @state: I2C data value (0 or 1) to set 541 * 542 * Sets the I2C data bit 543 **/ 544 static void igb_set_i2c_data(void *data, int state) 545 { 546 struct igb_adapter *adapter = (struct igb_adapter *)data; 547 struct e1000_hw *hw = &adapter->hw; 548 s32 i2cctl = rd32(E1000_I2CPARAMS); 549 550 if (state) { 551 i2cctl |= E1000_I2C_DATA_OUT | E1000_I2C_DATA_OE_N; 552 } else { 553 i2cctl &= ~E1000_I2C_DATA_OE_N; 554 i2cctl &= ~E1000_I2C_DATA_OUT; 555 } 556 557 wr32(E1000_I2CPARAMS, i2cctl); 558 wrfl(); 559 } 560 561 /** 562 * igb_set_i2c_clk - Sets the I2C SCL clock 563 * @data: pointer to hardware structure 564 * @state: state to set clock 565 * 566 * Sets the I2C clock line to state 567 **/ 568 static void igb_set_i2c_clk(void *data, int state) 569 { 570 struct igb_adapter *adapter = (struct igb_adapter *)data; 571 struct e1000_hw *hw = &adapter->hw; 572 s32 i2cctl = rd32(E1000_I2CPARAMS); 573 574 if (state) { 575 i2cctl |= E1000_I2C_CLK_OUT | E1000_I2C_CLK_OE_N; 576 } else { 577 i2cctl &= ~E1000_I2C_CLK_OUT; 578 i2cctl &= ~E1000_I2C_CLK_OE_N; 579 } 580 wr32(E1000_I2CPARAMS, i2cctl); 581 wrfl(); 582 } 583 584 /** 585 * igb_get_i2c_clk - Gets the I2C SCL clock state 586 * @data: pointer to hardware structure 587 * 588 * Gets the I2C clock state 589 **/ 590 static int igb_get_i2c_clk(void *data) 591 { 592 struct igb_adapter *adapter = (struct igb_adapter *)data; 593 struct e1000_hw *hw = &adapter->hw; 594 s32 i2cctl = rd32(E1000_I2CPARAMS); 595 596 return !!(i2cctl & E1000_I2C_CLK_IN); 597 } 598 599 static const struct i2c_algo_bit_data igb_i2c_algo = { 600 .setsda = igb_set_i2c_data, 601 .setscl = igb_set_i2c_clk, 602 .getsda = igb_get_i2c_data, 603 .getscl = igb_get_i2c_clk, 604 .udelay = 5, 605 .timeout = 20, 606 }; 607 608 /** 609 * igb_get_hw_dev - return device 610 * @hw: pointer to hardware structure 611 * 612 * used by hardware layer to print debugging information 613 **/ 614 struct net_device *igb_get_hw_dev(struct e1000_hw *hw) 615 { 616 struct igb_adapter *adapter = hw->back; 617 return adapter->netdev; 618 } 619 620 static struct pci_driver igb_driver; 621 622 /** 623 * igb_init_module - Driver Registration Routine 624 * 625 * igb_init_module is the first routine called when the driver is 626 * loaded. All it does is register with the PCI subsystem. 627 **/ 628 static int __init igb_init_module(void) 629 { 630 int ret; 631 632 pr_info("%s\n", igb_driver_string); 633 pr_info("%s\n", igb_copyright); 634 635 #ifdef CONFIG_IGB_DCA 636 dca_register_notify(&dca_notifier); 637 #endif 638 ret = pci_register_driver(&igb_driver); 639 return ret; 640 } 641 642 module_init(igb_init_module); 643 644 /** 645 * igb_exit_module - Driver Exit Cleanup Routine 646 * 647 * igb_exit_module is called just before the driver is removed 648 * from memory. 649 **/ 650 static void __exit igb_exit_module(void) 651 { 652 #ifdef CONFIG_IGB_DCA 653 dca_unregister_notify(&dca_notifier); 654 #endif 655 pci_unregister_driver(&igb_driver); 656 } 657 658 module_exit(igb_exit_module); 659 660 #define Q_IDX_82576(i) (((i & 0x1) << 3) + (i >> 1)) 661 /** 662 * igb_cache_ring_register - Descriptor ring to register mapping 663 * @adapter: board private structure to initialize 664 * 665 * Once we know the feature-set enabled for the device, we'll cache 666 * the register offset the descriptor ring is assigned to. 667 **/ 668 static void igb_cache_ring_register(struct igb_adapter *adapter) 669 { 670 int i = 0, j = 0; 671 u32 rbase_offset = adapter->vfs_allocated_count; 672 673 switch (adapter->hw.mac.type) { 674 case e1000_82576: 675 /* The queues are allocated for virtualization such that VF 0 676 * is allocated queues 0 and 8, VF 1 queues 1 and 9, etc. 677 * In order to avoid collision we start at the first free queue 678 * and continue consuming queues in the same sequence 679 */ 680 if (adapter->vfs_allocated_count) { 681 for (; i < adapter->rss_queues; i++) 682 adapter->rx_ring[i]->reg_idx = rbase_offset + 683 Q_IDX_82576(i); 684 } 685 fallthrough; 686 case e1000_82575: 687 case e1000_82580: 688 case e1000_i350: 689 case e1000_i354: 690 case e1000_i210: 691 case e1000_i211: 692 default: 693 for (; i < adapter->num_rx_queues; i++) 694 adapter->rx_ring[i]->reg_idx = rbase_offset + i; 695 for (; j < adapter->num_tx_queues; j++) 696 adapter->tx_ring[j]->reg_idx = rbase_offset + j; 697 break; 698 } 699 } 700 701 u32 igb_rd32(struct e1000_hw *hw, u32 reg) 702 { 703 struct igb_adapter *igb = container_of(hw, struct igb_adapter, hw); 704 u8 __iomem *hw_addr = READ_ONCE(hw->hw_addr); 705 u32 value = 0; 706 707 if (E1000_REMOVED(hw_addr)) 708 return ~value; 709 710 value = readl(&hw_addr[reg]); 711 712 /* reads should not return all F's */ 713 if (!(~value) && (!reg || !(~readl(hw_addr)))) { 714 struct net_device *netdev = igb->netdev; 715 hw->hw_addr = NULL; 716 netdev_err(netdev, "PCIe link lost\n"); 717 WARN(pci_device_is_present(igb->pdev), 718 "igb: Failed to read reg 0x%x!\n", reg); 719 } 720 721 return value; 722 } 723 724 /** 725 * igb_write_ivar - configure ivar for given MSI-X vector 726 * @hw: pointer to the HW structure 727 * @msix_vector: vector number we are allocating to a given ring 728 * @index: row index of IVAR register to write within IVAR table 729 * @offset: column offset of in IVAR, should be multiple of 8 730 * 731 * This function is intended to handle the writing of the IVAR register 732 * for adapters 82576 and newer. The IVAR table consists of 2 columns, 733 * each containing an cause allocation for an Rx and Tx ring, and a 734 * variable number of rows depending on the number of queues supported. 735 **/ 736 static void igb_write_ivar(struct e1000_hw *hw, int msix_vector, 737 int index, int offset) 738 { 739 u32 ivar = array_rd32(E1000_IVAR0, index); 740 741 /* clear any bits that are currently set */ 742 ivar &= ~((u32)0xFF << offset); 743 744 /* write vector and valid bit */ 745 ivar |= (msix_vector | E1000_IVAR_VALID) << offset; 746 747 array_wr32(E1000_IVAR0, index, ivar); 748 } 749 750 #define IGB_N0_QUEUE -1 751 static void igb_assign_vector(struct igb_q_vector *q_vector, int msix_vector) 752 { 753 struct igb_adapter *adapter = q_vector->adapter; 754 struct e1000_hw *hw = &adapter->hw; 755 int rx_queue = IGB_N0_QUEUE; 756 int tx_queue = IGB_N0_QUEUE; 757 u32 msixbm = 0; 758 759 if (q_vector->rx.ring) 760 rx_queue = q_vector->rx.ring->reg_idx; 761 if (q_vector->tx.ring) 762 tx_queue = q_vector->tx.ring->reg_idx; 763 764 switch (hw->mac.type) { 765 case e1000_82575: 766 /* The 82575 assigns vectors using a bitmask, which matches the 767 * bitmask for the EICR/EIMS/EIMC registers. To assign one 768 * or more queues to a vector, we write the appropriate bits 769 * into the MSIXBM register for that vector. 770 */ 771 if (rx_queue > IGB_N0_QUEUE) 772 msixbm = E1000_EICR_RX_QUEUE0 << rx_queue; 773 if (tx_queue > IGB_N0_QUEUE) 774 msixbm |= E1000_EICR_TX_QUEUE0 << tx_queue; 775 if (!(adapter->flags & IGB_FLAG_HAS_MSIX) && msix_vector == 0) 776 msixbm |= E1000_EIMS_OTHER; 777 array_wr32(E1000_MSIXBM(0), msix_vector, msixbm); 778 q_vector->eims_value = msixbm; 779 break; 780 case e1000_82576: 781 /* 82576 uses a table that essentially consists of 2 columns 782 * with 8 rows. The ordering is column-major so we use the 783 * lower 3 bits as the row index, and the 4th bit as the 784 * column offset. 785 */ 786 if (rx_queue > IGB_N0_QUEUE) 787 igb_write_ivar(hw, msix_vector, 788 rx_queue & 0x7, 789 (rx_queue & 0x8) << 1); 790 if (tx_queue > IGB_N0_QUEUE) 791 igb_write_ivar(hw, msix_vector, 792 tx_queue & 0x7, 793 ((tx_queue & 0x8) << 1) + 8); 794 q_vector->eims_value = BIT(msix_vector); 795 break; 796 case e1000_82580: 797 case e1000_i350: 798 case e1000_i354: 799 case e1000_i210: 800 case e1000_i211: 801 /* On 82580 and newer adapters the scheme is similar to 82576 802 * however instead of ordering column-major we have things 803 * ordered row-major. So we traverse the table by using 804 * bit 0 as the column offset, and the remaining bits as the 805 * row index. 806 */ 807 if (rx_queue > IGB_N0_QUEUE) 808 igb_write_ivar(hw, msix_vector, 809 rx_queue >> 1, 810 (rx_queue & 0x1) << 4); 811 if (tx_queue > IGB_N0_QUEUE) 812 igb_write_ivar(hw, msix_vector, 813 tx_queue >> 1, 814 ((tx_queue & 0x1) << 4) + 8); 815 q_vector->eims_value = BIT(msix_vector); 816 break; 817 default: 818 BUG(); 819 break; 820 } 821 822 /* add q_vector eims value to global eims_enable_mask */ 823 adapter->eims_enable_mask |= q_vector->eims_value; 824 825 /* configure q_vector to set itr on first interrupt */ 826 q_vector->set_itr = 1; 827 } 828 829 /** 830 * igb_configure_msix - Configure MSI-X hardware 831 * @adapter: board private structure to initialize 832 * 833 * igb_configure_msix sets up the hardware to properly 834 * generate MSI-X interrupts. 835 **/ 836 static void igb_configure_msix(struct igb_adapter *adapter) 837 { 838 u32 tmp; 839 int i, vector = 0; 840 struct e1000_hw *hw = &adapter->hw; 841 842 adapter->eims_enable_mask = 0; 843 844 /* set vector for other causes, i.e. link changes */ 845 switch (hw->mac.type) { 846 case e1000_82575: 847 tmp = rd32(E1000_CTRL_EXT); 848 /* enable MSI-X PBA support*/ 849 tmp |= E1000_CTRL_EXT_PBA_CLR; 850 851 /* Auto-Mask interrupts upon ICR read. */ 852 tmp |= E1000_CTRL_EXT_EIAME; 853 tmp |= E1000_CTRL_EXT_IRCA; 854 855 wr32(E1000_CTRL_EXT, tmp); 856 857 /* enable msix_other interrupt */ 858 array_wr32(E1000_MSIXBM(0), vector++, E1000_EIMS_OTHER); 859 adapter->eims_other = E1000_EIMS_OTHER; 860 861 break; 862 863 case e1000_82576: 864 case e1000_82580: 865 case e1000_i350: 866 case e1000_i354: 867 case e1000_i210: 868 case e1000_i211: 869 /* Turn on MSI-X capability first, or our settings 870 * won't stick. And it will take days to debug. 871 */ 872 wr32(E1000_GPIE, E1000_GPIE_MSIX_MODE | 873 E1000_GPIE_PBA | E1000_GPIE_EIAME | 874 E1000_GPIE_NSICR); 875 876 /* enable msix_other interrupt */ 877 adapter->eims_other = BIT(vector); 878 tmp = (vector++ | E1000_IVAR_VALID) << 8; 879 880 wr32(E1000_IVAR_MISC, tmp); 881 break; 882 default: 883 /* do nothing, since nothing else supports MSI-X */ 884 break; 885 } /* switch (hw->mac.type) */ 886 887 adapter->eims_enable_mask |= adapter->eims_other; 888 889 for (i = 0; i < adapter->num_q_vectors; i++) 890 igb_assign_vector(adapter->q_vector[i], vector++); 891 892 wrfl(); 893 } 894 895 /** 896 * igb_request_msix - Initialize MSI-X interrupts 897 * @adapter: board private structure to initialize 898 * 899 * igb_request_msix allocates MSI-X vectors and requests interrupts from the 900 * kernel. 901 **/ 902 static int igb_request_msix(struct igb_adapter *adapter) 903 { 904 unsigned int num_q_vectors = adapter->num_q_vectors; 905 struct net_device *netdev = adapter->netdev; 906 int i, err = 0, vector = 0, free_vector = 0; 907 908 err = request_irq(adapter->msix_entries[vector].vector, 909 igb_msix_other, 0, netdev->name, adapter); 910 if (err) 911 goto err_out; 912 913 if (num_q_vectors > MAX_Q_VECTORS) { 914 num_q_vectors = MAX_Q_VECTORS; 915 dev_warn(&adapter->pdev->dev, 916 "The number of queue vectors (%d) is higher than max allowed (%d)\n", 917 adapter->num_q_vectors, MAX_Q_VECTORS); 918 } 919 for (i = 0; i < num_q_vectors; i++) { 920 struct igb_q_vector *q_vector = adapter->q_vector[i]; 921 922 vector++; 923 924 q_vector->itr_register = adapter->io_addr + E1000_EITR(vector); 925 926 if (q_vector->rx.ring && q_vector->tx.ring) 927 sprintf(q_vector->name, "%s-TxRx-%u", netdev->name, 928 q_vector->rx.ring->queue_index); 929 else if (q_vector->tx.ring) 930 sprintf(q_vector->name, "%s-tx-%u", netdev->name, 931 q_vector->tx.ring->queue_index); 932 else if (q_vector->rx.ring) 933 sprintf(q_vector->name, "%s-rx-%u", netdev->name, 934 q_vector->rx.ring->queue_index); 935 else 936 sprintf(q_vector->name, "%s-unused", netdev->name); 937 938 err = request_irq(adapter->msix_entries[vector].vector, 939 igb_msix_ring, 0, q_vector->name, 940 q_vector); 941 if (err) 942 goto err_free; 943 } 944 945 igb_configure_msix(adapter); 946 return 0; 947 948 err_free: 949 /* free already assigned IRQs */ 950 free_irq(adapter->msix_entries[free_vector++].vector, adapter); 951 952 vector--; 953 for (i = 0; i < vector; i++) { 954 free_irq(adapter->msix_entries[free_vector++].vector, 955 adapter->q_vector[i]); 956 } 957 err_out: 958 return err; 959 } 960 961 /** 962 * igb_free_q_vector - Free memory allocated for specific interrupt vector 963 * @adapter: board private structure to initialize 964 * @v_idx: Index of vector to be freed 965 * 966 * This function frees the memory allocated to the q_vector. 967 **/ 968 static void igb_free_q_vector(struct igb_adapter *adapter, int v_idx) 969 { 970 struct igb_q_vector *q_vector = adapter->q_vector[v_idx]; 971 972 adapter->q_vector[v_idx] = NULL; 973 974 /* igb_get_stats64() might access the rings on this vector, 975 * we must wait a grace period before freeing it. 976 */ 977 if (q_vector) 978 kfree_rcu(q_vector, rcu); 979 } 980 981 /** 982 * igb_reset_q_vector - Reset config for interrupt vector 983 * @adapter: board private structure to initialize 984 * @v_idx: Index of vector to be reset 985 * 986 * If NAPI is enabled it will delete any references to the 987 * NAPI struct. This is preparation for igb_free_q_vector. 988 **/ 989 static void igb_reset_q_vector(struct igb_adapter *adapter, int v_idx) 990 { 991 struct igb_q_vector *q_vector = adapter->q_vector[v_idx]; 992 993 /* Coming from igb_set_interrupt_capability, the vectors are not yet 994 * allocated. So, q_vector is NULL so we should stop here. 995 */ 996 if (!q_vector) 997 return; 998 999 if (q_vector->tx.ring) 1000 adapter->tx_ring[q_vector->tx.ring->queue_index] = NULL; 1001 1002 if (q_vector->rx.ring) 1003 adapter->rx_ring[q_vector->rx.ring->queue_index] = NULL; 1004 1005 netif_napi_del(&q_vector->napi); 1006 1007 } 1008 1009 static void igb_reset_interrupt_capability(struct igb_adapter *adapter) 1010 { 1011 int v_idx = adapter->num_q_vectors; 1012 1013 if (adapter->flags & IGB_FLAG_HAS_MSIX) 1014 pci_disable_msix(adapter->pdev); 1015 else if (adapter->flags & IGB_FLAG_HAS_MSI) 1016 pci_disable_msi(adapter->pdev); 1017 1018 while (v_idx--) 1019 igb_reset_q_vector(adapter, v_idx); 1020 } 1021 1022 /** 1023 * igb_free_q_vectors - Free memory allocated for interrupt vectors 1024 * @adapter: board private structure to initialize 1025 * 1026 * This function frees the memory allocated to the q_vectors. In addition if 1027 * NAPI is enabled it will delete any references to the NAPI struct prior 1028 * to freeing the q_vector. 1029 **/ 1030 static void igb_free_q_vectors(struct igb_adapter *adapter) 1031 { 1032 int v_idx = adapter->num_q_vectors; 1033 1034 adapter->num_tx_queues = 0; 1035 adapter->num_rx_queues = 0; 1036 adapter->num_q_vectors = 0; 1037 1038 while (v_idx--) { 1039 igb_reset_q_vector(adapter, v_idx); 1040 igb_free_q_vector(adapter, v_idx); 1041 } 1042 } 1043 1044 /** 1045 * igb_clear_interrupt_scheme - reset the device to a state of no interrupts 1046 * @adapter: board private structure to initialize 1047 * 1048 * This function resets the device so that it has 0 Rx queues, Tx queues, and 1049 * MSI-X interrupts allocated. 1050 */ 1051 static void igb_clear_interrupt_scheme(struct igb_adapter *adapter) 1052 { 1053 igb_free_q_vectors(adapter); 1054 igb_reset_interrupt_capability(adapter); 1055 } 1056 1057 /** 1058 * igb_set_interrupt_capability - set MSI or MSI-X if supported 1059 * @adapter: board private structure to initialize 1060 * @msix: boolean value of MSIX capability 1061 * 1062 * Attempt to configure interrupts using the best available 1063 * capabilities of the hardware and kernel. 1064 **/ 1065 static void igb_set_interrupt_capability(struct igb_adapter *adapter, bool msix) 1066 { 1067 int err; 1068 int numvecs, i; 1069 1070 if (!msix) 1071 goto msi_only; 1072 adapter->flags |= IGB_FLAG_HAS_MSIX; 1073 1074 /* Number of supported queues. */ 1075 adapter->num_rx_queues = adapter->rss_queues; 1076 if (adapter->vfs_allocated_count) 1077 adapter->num_tx_queues = 1; 1078 else 1079 adapter->num_tx_queues = adapter->rss_queues; 1080 1081 /* start with one vector for every Rx queue */ 1082 numvecs = adapter->num_rx_queues; 1083 1084 /* if Tx handler is separate add 1 for every Tx queue */ 1085 if (!(adapter->flags & IGB_FLAG_QUEUE_PAIRS)) 1086 numvecs += adapter->num_tx_queues; 1087 1088 /* store the number of vectors reserved for queues */ 1089 adapter->num_q_vectors = numvecs; 1090 1091 /* add 1 vector for link status interrupts */ 1092 numvecs++; 1093 for (i = 0; i < numvecs; i++) 1094 adapter->msix_entries[i].entry = i; 1095 1096 err = pci_enable_msix_range(adapter->pdev, 1097 adapter->msix_entries, 1098 numvecs, 1099 numvecs); 1100 if (err > 0) 1101 return; 1102 1103 igb_reset_interrupt_capability(adapter); 1104 1105 /* If we can't do MSI-X, try MSI */ 1106 msi_only: 1107 adapter->flags &= ~IGB_FLAG_HAS_MSIX; 1108 #ifdef CONFIG_PCI_IOV 1109 /* disable SR-IOV for non MSI-X configurations */ 1110 if (adapter->vf_data) { 1111 struct e1000_hw *hw = &adapter->hw; 1112 /* disable iov and allow time for transactions to clear */ 1113 pci_disable_sriov(adapter->pdev); 1114 msleep(500); 1115 1116 kfree(adapter->vf_mac_list); 1117 adapter->vf_mac_list = NULL; 1118 kfree(adapter->vf_data); 1119 adapter->vf_data = NULL; 1120 wr32(E1000_IOVCTL, E1000_IOVCTL_REUSE_VFQ); 1121 wrfl(); 1122 msleep(100); 1123 dev_info(&adapter->pdev->dev, "IOV Disabled\n"); 1124 } 1125 #endif 1126 adapter->vfs_allocated_count = 0; 1127 adapter->rss_queues = 1; 1128 adapter->flags |= IGB_FLAG_QUEUE_PAIRS; 1129 adapter->num_rx_queues = 1; 1130 adapter->num_tx_queues = 1; 1131 adapter->num_q_vectors = 1; 1132 if (!pci_enable_msi(adapter->pdev)) 1133 adapter->flags |= IGB_FLAG_HAS_MSI; 1134 } 1135 1136 static void igb_add_ring(struct igb_ring *ring, 1137 struct igb_ring_container *head) 1138 { 1139 head->ring = ring; 1140 head->count++; 1141 } 1142 1143 /** 1144 * igb_alloc_q_vector - Allocate memory for a single interrupt vector 1145 * @adapter: board private structure to initialize 1146 * @v_count: q_vectors allocated on adapter, used for ring interleaving 1147 * @v_idx: index of vector in adapter struct 1148 * @txr_count: total number of Tx rings to allocate 1149 * @txr_idx: index of first Tx ring to allocate 1150 * @rxr_count: total number of Rx rings to allocate 1151 * @rxr_idx: index of first Rx ring to allocate 1152 * 1153 * We allocate one q_vector. If allocation fails we return -ENOMEM. 1154 **/ 1155 static int igb_alloc_q_vector(struct igb_adapter *adapter, 1156 int v_count, int v_idx, 1157 int txr_count, int txr_idx, 1158 int rxr_count, int rxr_idx) 1159 { 1160 struct igb_q_vector *q_vector; 1161 struct igb_ring *ring; 1162 int ring_count; 1163 size_t size; 1164 1165 /* igb only supports 1 Tx and/or 1 Rx queue per vector */ 1166 if (txr_count > 1 || rxr_count > 1) 1167 return -ENOMEM; 1168 1169 ring_count = txr_count + rxr_count; 1170 size = kmalloc_size_roundup(struct_size(q_vector, ring, ring_count)); 1171 1172 /* allocate q_vector and rings */ 1173 q_vector = adapter->q_vector[v_idx]; 1174 if (!q_vector) { 1175 q_vector = kzalloc(size, GFP_KERNEL); 1176 } else if (size > ksize(q_vector)) { 1177 struct igb_q_vector *new_q_vector; 1178 1179 new_q_vector = kzalloc(size, GFP_KERNEL); 1180 if (new_q_vector) 1181 kfree_rcu(q_vector, rcu); 1182 q_vector = new_q_vector; 1183 } else { 1184 memset(q_vector, 0, size); 1185 } 1186 if (!q_vector) 1187 return -ENOMEM; 1188 1189 /* initialize NAPI */ 1190 netif_napi_add(adapter->netdev, &q_vector->napi, igb_poll); 1191 1192 /* tie q_vector and adapter together */ 1193 adapter->q_vector[v_idx] = q_vector; 1194 q_vector->adapter = adapter; 1195 1196 /* initialize work limits */ 1197 q_vector->tx.work_limit = adapter->tx_work_limit; 1198 1199 /* initialize ITR configuration */ 1200 q_vector->itr_register = adapter->io_addr + E1000_EITR(0); 1201 q_vector->itr_val = IGB_START_ITR; 1202 1203 /* initialize pointer to rings */ 1204 ring = q_vector->ring; 1205 1206 /* intialize ITR */ 1207 if (rxr_count) { 1208 /* rx or rx/tx vector */ 1209 if (!adapter->rx_itr_setting || adapter->rx_itr_setting > 3) 1210 q_vector->itr_val = adapter->rx_itr_setting; 1211 } else { 1212 /* tx only vector */ 1213 if (!adapter->tx_itr_setting || adapter->tx_itr_setting > 3) 1214 q_vector->itr_val = adapter->tx_itr_setting; 1215 } 1216 1217 if (txr_count) { 1218 /* assign generic ring traits */ 1219 ring->dev = &adapter->pdev->dev; 1220 ring->netdev = adapter->netdev; 1221 1222 /* configure backlink on ring */ 1223 ring->q_vector = q_vector; 1224 1225 /* update q_vector Tx values */ 1226 igb_add_ring(ring, &q_vector->tx); 1227 1228 /* For 82575, context index must be unique per ring. */ 1229 if (adapter->hw.mac.type == e1000_82575) 1230 set_bit(IGB_RING_FLAG_TX_CTX_IDX, &ring->flags); 1231 1232 /* apply Tx specific ring traits */ 1233 ring->count = adapter->tx_ring_count; 1234 ring->queue_index = txr_idx; 1235 1236 ring->cbs_enable = false; 1237 ring->idleslope = 0; 1238 ring->sendslope = 0; 1239 ring->hicredit = 0; 1240 ring->locredit = 0; 1241 1242 u64_stats_init(&ring->tx_syncp); 1243 u64_stats_init(&ring->tx_syncp2); 1244 1245 /* assign ring to adapter */ 1246 adapter->tx_ring[txr_idx] = ring; 1247 1248 /* push pointer to next ring */ 1249 ring++; 1250 } 1251 1252 if (rxr_count) { 1253 /* assign generic ring traits */ 1254 ring->dev = &adapter->pdev->dev; 1255 ring->netdev = adapter->netdev; 1256 1257 /* configure backlink on ring */ 1258 ring->q_vector = q_vector; 1259 1260 /* update q_vector Rx values */ 1261 igb_add_ring(ring, &q_vector->rx); 1262 1263 /* set flag indicating ring supports SCTP checksum offload */ 1264 if (adapter->hw.mac.type >= e1000_82576) 1265 set_bit(IGB_RING_FLAG_RX_SCTP_CSUM, &ring->flags); 1266 1267 /* On i350, i354, i210, and i211, loopback VLAN packets 1268 * have the tag byte-swapped. 1269 */ 1270 if (adapter->hw.mac.type >= e1000_i350) 1271 set_bit(IGB_RING_FLAG_RX_LB_VLAN_BSWAP, &ring->flags); 1272 1273 /* apply Rx specific ring traits */ 1274 ring->count = adapter->rx_ring_count; 1275 ring->queue_index = rxr_idx; 1276 1277 u64_stats_init(&ring->rx_syncp); 1278 1279 /* assign ring to adapter */ 1280 adapter->rx_ring[rxr_idx] = ring; 1281 } 1282 1283 return 0; 1284 } 1285 1286 1287 /** 1288 * igb_alloc_q_vectors - Allocate memory for interrupt vectors 1289 * @adapter: board private structure to initialize 1290 * 1291 * We allocate one q_vector per queue interrupt. If allocation fails we 1292 * return -ENOMEM. 1293 **/ 1294 static int igb_alloc_q_vectors(struct igb_adapter *adapter) 1295 { 1296 int q_vectors = adapter->num_q_vectors; 1297 int rxr_remaining = adapter->num_rx_queues; 1298 int txr_remaining = adapter->num_tx_queues; 1299 int rxr_idx = 0, txr_idx = 0, v_idx = 0; 1300 int err; 1301 1302 if (q_vectors >= (rxr_remaining + txr_remaining)) { 1303 for (; rxr_remaining; v_idx++) { 1304 err = igb_alloc_q_vector(adapter, q_vectors, v_idx, 1305 0, 0, 1, rxr_idx); 1306 1307 if (err) 1308 goto err_out; 1309 1310 /* update counts and index */ 1311 rxr_remaining--; 1312 rxr_idx++; 1313 } 1314 } 1315 1316 for (; v_idx < q_vectors; v_idx++) { 1317 int rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - v_idx); 1318 int tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - v_idx); 1319 1320 err = igb_alloc_q_vector(adapter, q_vectors, v_idx, 1321 tqpv, txr_idx, rqpv, rxr_idx); 1322 1323 if (err) 1324 goto err_out; 1325 1326 /* update counts and index */ 1327 rxr_remaining -= rqpv; 1328 txr_remaining -= tqpv; 1329 rxr_idx++; 1330 txr_idx++; 1331 } 1332 1333 return 0; 1334 1335 err_out: 1336 adapter->num_tx_queues = 0; 1337 adapter->num_rx_queues = 0; 1338 adapter->num_q_vectors = 0; 1339 1340 while (v_idx--) 1341 igb_free_q_vector(adapter, v_idx); 1342 1343 return -ENOMEM; 1344 } 1345 1346 /** 1347 * igb_init_interrupt_scheme - initialize interrupts, allocate queues/vectors 1348 * @adapter: board private structure to initialize 1349 * @msix: boolean value of MSIX capability 1350 * 1351 * This function initializes the interrupts and allocates all of the queues. 1352 **/ 1353 static int igb_init_interrupt_scheme(struct igb_adapter *adapter, bool msix) 1354 { 1355 struct pci_dev *pdev = adapter->pdev; 1356 int err; 1357 1358 igb_set_interrupt_capability(adapter, msix); 1359 1360 err = igb_alloc_q_vectors(adapter); 1361 if (err) { 1362 dev_err(&pdev->dev, "Unable to allocate memory for vectors\n"); 1363 goto err_alloc_q_vectors; 1364 } 1365 1366 igb_cache_ring_register(adapter); 1367 1368 return 0; 1369 1370 err_alloc_q_vectors: 1371 igb_reset_interrupt_capability(adapter); 1372 return err; 1373 } 1374 1375 /** 1376 * igb_request_irq - initialize interrupts 1377 * @adapter: board private structure to initialize 1378 * 1379 * Attempts to configure interrupts using the best available 1380 * capabilities of the hardware and kernel. 1381 **/ 1382 static int igb_request_irq(struct igb_adapter *adapter) 1383 { 1384 struct net_device *netdev = adapter->netdev; 1385 struct pci_dev *pdev = adapter->pdev; 1386 int err = 0; 1387 1388 if (adapter->flags & IGB_FLAG_HAS_MSIX) { 1389 err = igb_request_msix(adapter); 1390 if (!err) 1391 goto request_done; 1392 /* fall back to MSI */ 1393 igb_free_all_tx_resources(adapter); 1394 igb_free_all_rx_resources(adapter); 1395 1396 igb_clear_interrupt_scheme(adapter); 1397 err = igb_init_interrupt_scheme(adapter, false); 1398 if (err) 1399 goto request_done; 1400 1401 igb_setup_all_tx_resources(adapter); 1402 igb_setup_all_rx_resources(adapter); 1403 igb_configure(adapter); 1404 } 1405 1406 igb_assign_vector(adapter->q_vector[0], 0); 1407 1408 if (adapter->flags & IGB_FLAG_HAS_MSI) { 1409 err = request_irq(pdev->irq, igb_intr_msi, 0, 1410 netdev->name, adapter); 1411 if (!err) 1412 goto request_done; 1413 1414 /* fall back to legacy interrupts */ 1415 igb_reset_interrupt_capability(adapter); 1416 adapter->flags &= ~IGB_FLAG_HAS_MSI; 1417 } 1418 1419 err = request_irq(pdev->irq, igb_intr, IRQF_SHARED, 1420 netdev->name, adapter); 1421 1422 if (err) 1423 dev_err(&pdev->dev, "Error %d getting interrupt\n", 1424 err); 1425 1426 request_done: 1427 return err; 1428 } 1429 1430 static void igb_free_irq(struct igb_adapter *adapter) 1431 { 1432 if (adapter->flags & IGB_FLAG_HAS_MSIX) { 1433 int vector = 0, i; 1434 1435 free_irq(adapter->msix_entries[vector++].vector, adapter); 1436 1437 for (i = 0; i < adapter->num_q_vectors; i++) 1438 free_irq(adapter->msix_entries[vector++].vector, 1439 adapter->q_vector[i]); 1440 } else { 1441 free_irq(adapter->pdev->irq, adapter); 1442 } 1443 } 1444 1445 /** 1446 * igb_irq_disable - Mask off interrupt generation on the NIC 1447 * @adapter: board private structure 1448 **/ 1449 static void igb_irq_disable(struct igb_adapter *adapter) 1450 { 1451 struct e1000_hw *hw = &adapter->hw; 1452 1453 /* we need to be careful when disabling interrupts. The VFs are also 1454 * mapped into these registers and so clearing the bits can cause 1455 * issues on the VF drivers so we only need to clear what we set 1456 */ 1457 if (adapter->flags & IGB_FLAG_HAS_MSIX) { 1458 u32 regval = rd32(E1000_EIAM); 1459 1460 wr32(E1000_EIAM, regval & ~adapter->eims_enable_mask); 1461 wr32(E1000_EIMC, adapter->eims_enable_mask); 1462 regval = rd32(E1000_EIAC); 1463 wr32(E1000_EIAC, regval & ~adapter->eims_enable_mask); 1464 } 1465 1466 wr32(E1000_IAM, 0); 1467 wr32(E1000_IMC, ~0); 1468 wrfl(); 1469 if (adapter->flags & IGB_FLAG_HAS_MSIX) { 1470 int i; 1471 1472 for (i = 0; i < adapter->num_q_vectors; i++) 1473 synchronize_irq(adapter->msix_entries[i].vector); 1474 } else { 1475 synchronize_irq(adapter->pdev->irq); 1476 } 1477 } 1478 1479 /** 1480 * igb_irq_enable - Enable default interrupt generation settings 1481 * @adapter: board private structure 1482 **/ 1483 static void igb_irq_enable(struct igb_adapter *adapter) 1484 { 1485 struct e1000_hw *hw = &adapter->hw; 1486 1487 if (adapter->flags & IGB_FLAG_HAS_MSIX) { 1488 u32 ims = E1000_IMS_LSC | E1000_IMS_DOUTSYNC | E1000_IMS_DRSTA; 1489 u32 regval = rd32(E1000_EIAC); 1490 1491 wr32(E1000_EIAC, regval | adapter->eims_enable_mask); 1492 regval = rd32(E1000_EIAM); 1493 wr32(E1000_EIAM, regval | adapter->eims_enable_mask); 1494 wr32(E1000_EIMS, adapter->eims_enable_mask); 1495 if (adapter->vfs_allocated_count) { 1496 wr32(E1000_MBVFIMR, 0xFF); 1497 ims |= E1000_IMS_VMMB; 1498 } 1499 wr32(E1000_IMS, ims); 1500 } else { 1501 wr32(E1000_IMS, IMS_ENABLE_MASK | 1502 E1000_IMS_DRSTA); 1503 wr32(E1000_IAM, IMS_ENABLE_MASK | 1504 E1000_IMS_DRSTA); 1505 } 1506 } 1507 1508 static void igb_update_mng_vlan(struct igb_adapter *adapter) 1509 { 1510 struct e1000_hw *hw = &adapter->hw; 1511 u16 pf_id = adapter->vfs_allocated_count; 1512 u16 vid = adapter->hw.mng_cookie.vlan_id; 1513 u16 old_vid = adapter->mng_vlan_id; 1514 1515 if (hw->mng_cookie.status & E1000_MNG_DHCP_COOKIE_STATUS_VLAN) { 1516 /* add VID to filter table */ 1517 igb_vfta_set(hw, vid, pf_id, true, true); 1518 adapter->mng_vlan_id = vid; 1519 } else { 1520 adapter->mng_vlan_id = IGB_MNG_VLAN_NONE; 1521 } 1522 1523 if ((old_vid != (u16)IGB_MNG_VLAN_NONE) && 1524 (vid != old_vid) && 1525 !test_bit(old_vid, adapter->active_vlans)) { 1526 /* remove VID from filter table */ 1527 igb_vfta_set(hw, vid, pf_id, false, true); 1528 } 1529 } 1530 1531 /** 1532 * igb_release_hw_control - release control of the h/w to f/w 1533 * @adapter: address of board private structure 1534 * 1535 * igb_release_hw_control resets CTRL_EXT:DRV_LOAD bit. 1536 * For ASF and Pass Through versions of f/w this means that the 1537 * driver is no longer loaded. 1538 **/ 1539 static void igb_release_hw_control(struct igb_adapter *adapter) 1540 { 1541 struct e1000_hw *hw = &adapter->hw; 1542 u32 ctrl_ext; 1543 1544 /* Let firmware take over control of h/w */ 1545 ctrl_ext = rd32(E1000_CTRL_EXT); 1546 wr32(E1000_CTRL_EXT, 1547 ctrl_ext & ~E1000_CTRL_EXT_DRV_LOAD); 1548 } 1549 1550 /** 1551 * igb_get_hw_control - get control of the h/w from f/w 1552 * @adapter: address of board private structure 1553 * 1554 * igb_get_hw_control sets CTRL_EXT:DRV_LOAD bit. 1555 * For ASF and Pass Through versions of f/w this means that 1556 * the driver is loaded. 1557 **/ 1558 static void igb_get_hw_control(struct igb_adapter *adapter) 1559 { 1560 struct e1000_hw *hw = &adapter->hw; 1561 u32 ctrl_ext; 1562 1563 /* Let firmware know the driver has taken over */ 1564 ctrl_ext = rd32(E1000_CTRL_EXT); 1565 wr32(E1000_CTRL_EXT, 1566 ctrl_ext | E1000_CTRL_EXT_DRV_LOAD); 1567 } 1568 1569 static void enable_fqtss(struct igb_adapter *adapter, bool enable) 1570 { 1571 struct net_device *netdev = adapter->netdev; 1572 struct e1000_hw *hw = &adapter->hw; 1573 1574 WARN_ON(hw->mac.type != e1000_i210); 1575 1576 if (enable) 1577 adapter->flags |= IGB_FLAG_FQTSS; 1578 else 1579 adapter->flags &= ~IGB_FLAG_FQTSS; 1580 1581 if (netif_running(netdev)) 1582 schedule_work(&adapter->reset_task); 1583 } 1584 1585 static bool is_fqtss_enabled(struct igb_adapter *adapter) 1586 { 1587 return (adapter->flags & IGB_FLAG_FQTSS) ? true : false; 1588 } 1589 1590 static void set_tx_desc_fetch_prio(struct e1000_hw *hw, int queue, 1591 enum tx_queue_prio prio) 1592 { 1593 u32 val; 1594 1595 WARN_ON(hw->mac.type != e1000_i210); 1596 WARN_ON(queue < 0 || queue > 4); 1597 1598 val = rd32(E1000_I210_TXDCTL(queue)); 1599 1600 if (prio == TX_QUEUE_PRIO_HIGH) 1601 val |= E1000_TXDCTL_PRIORITY; 1602 else 1603 val &= ~E1000_TXDCTL_PRIORITY; 1604 1605 wr32(E1000_I210_TXDCTL(queue), val); 1606 } 1607 1608 static void set_queue_mode(struct e1000_hw *hw, int queue, enum queue_mode mode) 1609 { 1610 u32 val; 1611 1612 WARN_ON(hw->mac.type != e1000_i210); 1613 WARN_ON(queue < 0 || queue > 1); 1614 1615 val = rd32(E1000_I210_TQAVCC(queue)); 1616 1617 if (mode == QUEUE_MODE_STREAM_RESERVATION) 1618 val |= E1000_TQAVCC_QUEUEMODE; 1619 else 1620 val &= ~E1000_TQAVCC_QUEUEMODE; 1621 1622 wr32(E1000_I210_TQAVCC(queue), val); 1623 } 1624 1625 static bool is_any_cbs_enabled(struct igb_adapter *adapter) 1626 { 1627 int i; 1628 1629 for (i = 0; i < adapter->num_tx_queues; i++) { 1630 if (adapter->tx_ring[i]->cbs_enable) 1631 return true; 1632 } 1633 1634 return false; 1635 } 1636 1637 static bool is_any_txtime_enabled(struct igb_adapter *adapter) 1638 { 1639 int i; 1640 1641 for (i = 0; i < adapter->num_tx_queues; i++) { 1642 if (adapter->tx_ring[i]->launchtime_enable) 1643 return true; 1644 } 1645 1646 return false; 1647 } 1648 1649 /** 1650 * igb_config_tx_modes - Configure "Qav Tx mode" features on igb 1651 * @adapter: pointer to adapter struct 1652 * @queue: queue number 1653 * 1654 * Configure CBS and Launchtime for a given hardware queue. 1655 * Parameters are retrieved from the correct Tx ring, so 1656 * igb_save_cbs_params() and igb_save_txtime_params() should be used 1657 * for setting those correctly prior to this function being called. 1658 **/ 1659 static void igb_config_tx_modes(struct igb_adapter *adapter, int queue) 1660 { 1661 struct net_device *netdev = adapter->netdev; 1662 struct e1000_hw *hw = &adapter->hw; 1663 struct igb_ring *ring; 1664 u32 tqavcc, tqavctrl; 1665 u16 value; 1666 1667 WARN_ON(hw->mac.type != e1000_i210); 1668 WARN_ON(queue < 0 || queue > 1); 1669 ring = adapter->tx_ring[queue]; 1670 1671 /* If any of the Qav features is enabled, configure queues as SR and 1672 * with HIGH PRIO. If none is, then configure them with LOW PRIO and 1673 * as SP. 1674 */ 1675 if (ring->cbs_enable || ring->launchtime_enable) { 1676 set_tx_desc_fetch_prio(hw, queue, TX_QUEUE_PRIO_HIGH); 1677 set_queue_mode(hw, queue, QUEUE_MODE_STREAM_RESERVATION); 1678 } else { 1679 set_tx_desc_fetch_prio(hw, queue, TX_QUEUE_PRIO_LOW); 1680 set_queue_mode(hw, queue, QUEUE_MODE_STRICT_PRIORITY); 1681 } 1682 1683 /* If CBS is enabled, set DataTranARB and config its parameters. */ 1684 if (ring->cbs_enable || queue == 0) { 1685 /* i210 does not allow the queue 0 to be in the Strict 1686 * Priority mode while the Qav mode is enabled, so, 1687 * instead of disabling strict priority mode, we give 1688 * queue 0 the maximum of credits possible. 1689 * 1690 * See section 8.12.19 of the i210 datasheet, "Note: 1691 * Queue0 QueueMode must be set to 1b when 1692 * TransmitMode is set to Qav." 1693 */ 1694 if (queue == 0 && !ring->cbs_enable) { 1695 /* max "linkspeed" idleslope in kbps */ 1696 ring->idleslope = 1000000; 1697 ring->hicredit = ETH_FRAME_LEN; 1698 } 1699 1700 /* Always set data transfer arbitration to credit-based 1701 * shaper algorithm on TQAVCTRL if CBS is enabled for any of 1702 * the queues. 1703 */ 1704 tqavctrl = rd32(E1000_I210_TQAVCTRL); 1705 tqavctrl |= E1000_TQAVCTRL_DATATRANARB; 1706 wr32(E1000_I210_TQAVCTRL, tqavctrl); 1707 1708 /* According to i210 datasheet section 7.2.7.7, we should set 1709 * the 'idleSlope' field from TQAVCC register following the 1710 * equation: 1711 * 1712 * For 100 Mbps link speed: 1713 * 1714 * value = BW * 0x7735 * 0.2 (E1) 1715 * 1716 * For 1000Mbps link speed: 1717 * 1718 * value = BW * 0x7735 * 2 (E2) 1719 * 1720 * E1 and E2 can be merged into one equation as shown below. 1721 * Note that 'link-speed' is in Mbps. 1722 * 1723 * value = BW * 0x7735 * 2 * link-speed 1724 * -------------- (E3) 1725 * 1000 1726 * 1727 * 'BW' is the percentage bandwidth out of full link speed 1728 * which can be found with the following equation. Note that 1729 * idleSlope here is the parameter from this function which 1730 * is in kbps. 1731 * 1732 * BW = idleSlope 1733 * ----------------- (E4) 1734 * link-speed * 1000 1735 * 1736 * That said, we can come up with a generic equation to 1737 * calculate the value we should set it TQAVCC register by 1738 * replacing 'BW' in E3 by E4. The resulting equation is: 1739 * 1740 * value = idleSlope * 0x7735 * 2 * link-speed 1741 * ----------------- -------------- (E5) 1742 * link-speed * 1000 1000 1743 * 1744 * 'link-speed' is present in both sides of the fraction so 1745 * it is canceled out. The final equation is the following: 1746 * 1747 * value = idleSlope * 61034 1748 * ----------------- (E6) 1749 * 1000000 1750 * 1751 * NOTE: For i210, given the above, we can see that idleslope 1752 * is represented in 16.38431 kbps units by the value at 1753 * the TQAVCC register (1Gbps / 61034), which reduces 1754 * the granularity for idleslope increments. 1755 * For instance, if you want to configure a 2576kbps 1756 * idleslope, the value to be written on the register 1757 * would have to be 157.23. If rounded down, you end 1758 * up with less bandwidth available than originally 1759 * required (~2572 kbps). If rounded up, you end up 1760 * with a higher bandwidth (~2589 kbps). Below the 1761 * approach we take is to always round up the 1762 * calculated value, so the resulting bandwidth might 1763 * be slightly higher for some configurations. 1764 */ 1765 value = DIV_ROUND_UP_ULL(ring->idleslope * 61034ULL, 1000000); 1766 1767 tqavcc = rd32(E1000_I210_TQAVCC(queue)); 1768 tqavcc &= ~E1000_TQAVCC_IDLESLOPE_MASK; 1769 tqavcc |= value; 1770 wr32(E1000_I210_TQAVCC(queue), tqavcc); 1771 1772 wr32(E1000_I210_TQAVHC(queue), 1773 0x80000000 + ring->hicredit * 0x7735); 1774 } else { 1775 1776 /* Set idleSlope to zero. */ 1777 tqavcc = rd32(E1000_I210_TQAVCC(queue)); 1778 tqavcc &= ~E1000_TQAVCC_IDLESLOPE_MASK; 1779 wr32(E1000_I210_TQAVCC(queue), tqavcc); 1780 1781 /* Set hiCredit to zero. */ 1782 wr32(E1000_I210_TQAVHC(queue), 0); 1783 1784 /* If CBS is not enabled for any queues anymore, then return to 1785 * the default state of Data Transmission Arbitration on 1786 * TQAVCTRL. 1787 */ 1788 if (!is_any_cbs_enabled(adapter)) { 1789 tqavctrl = rd32(E1000_I210_TQAVCTRL); 1790 tqavctrl &= ~E1000_TQAVCTRL_DATATRANARB; 1791 wr32(E1000_I210_TQAVCTRL, tqavctrl); 1792 } 1793 } 1794 1795 /* If LaunchTime is enabled, set DataTranTIM. */ 1796 if (ring->launchtime_enable) { 1797 /* Always set DataTranTIM on TQAVCTRL if LaunchTime is enabled 1798 * for any of the SR queues, and configure fetchtime delta. 1799 * XXX NOTE: 1800 * - LaunchTime will be enabled for all SR queues. 1801 * - A fixed offset can be added relative to the launch 1802 * time of all packets if configured at reg LAUNCH_OS0. 1803 * We are keeping it as 0 for now (default value). 1804 */ 1805 tqavctrl = rd32(E1000_I210_TQAVCTRL); 1806 tqavctrl |= E1000_TQAVCTRL_DATATRANTIM | 1807 E1000_TQAVCTRL_FETCHTIME_DELTA; 1808 wr32(E1000_I210_TQAVCTRL, tqavctrl); 1809 } else { 1810 /* If Launchtime is not enabled for any SR queues anymore, 1811 * then clear DataTranTIM on TQAVCTRL and clear fetchtime delta, 1812 * effectively disabling Launchtime. 1813 */ 1814 if (!is_any_txtime_enabled(adapter)) { 1815 tqavctrl = rd32(E1000_I210_TQAVCTRL); 1816 tqavctrl &= ~E1000_TQAVCTRL_DATATRANTIM; 1817 tqavctrl &= ~E1000_TQAVCTRL_FETCHTIME_DELTA; 1818 wr32(E1000_I210_TQAVCTRL, tqavctrl); 1819 } 1820 } 1821 1822 /* XXX: In i210 controller the sendSlope and loCredit parameters from 1823 * CBS are not configurable by software so we don't do any 'controller 1824 * configuration' in respect to these parameters. 1825 */ 1826 1827 netdev_dbg(netdev, "Qav Tx mode: cbs %s, launchtime %s, queue %d idleslope %d sendslope %d hiCredit %d locredit %d\n", 1828 ring->cbs_enable ? "enabled" : "disabled", 1829 ring->launchtime_enable ? "enabled" : "disabled", 1830 queue, 1831 ring->idleslope, ring->sendslope, 1832 ring->hicredit, ring->locredit); 1833 } 1834 1835 static int igb_save_txtime_params(struct igb_adapter *adapter, int queue, 1836 bool enable) 1837 { 1838 struct igb_ring *ring; 1839 1840 if (queue < 0 || queue > adapter->num_tx_queues) 1841 return -EINVAL; 1842 1843 ring = adapter->tx_ring[queue]; 1844 ring->launchtime_enable = enable; 1845 1846 return 0; 1847 } 1848 1849 static int igb_save_cbs_params(struct igb_adapter *adapter, int queue, 1850 bool enable, int idleslope, int sendslope, 1851 int hicredit, int locredit) 1852 { 1853 struct igb_ring *ring; 1854 1855 if (queue < 0 || queue > adapter->num_tx_queues) 1856 return -EINVAL; 1857 1858 ring = adapter->tx_ring[queue]; 1859 1860 ring->cbs_enable = enable; 1861 ring->idleslope = idleslope; 1862 ring->sendslope = sendslope; 1863 ring->hicredit = hicredit; 1864 ring->locredit = locredit; 1865 1866 return 0; 1867 } 1868 1869 /** 1870 * igb_setup_tx_mode - Switch to/from Qav Tx mode when applicable 1871 * @adapter: pointer to adapter struct 1872 * 1873 * Configure TQAVCTRL register switching the controller's Tx mode 1874 * if FQTSS mode is enabled or disabled. Additionally, will issue 1875 * a call to igb_config_tx_modes() per queue so any previously saved 1876 * Tx parameters are applied. 1877 **/ 1878 static void igb_setup_tx_mode(struct igb_adapter *adapter) 1879 { 1880 struct net_device *netdev = adapter->netdev; 1881 struct e1000_hw *hw = &adapter->hw; 1882 u32 val; 1883 1884 /* Only i210 controller supports changing the transmission mode. */ 1885 if (hw->mac.type != e1000_i210) 1886 return; 1887 1888 if (is_fqtss_enabled(adapter)) { 1889 int i, max_queue; 1890 1891 /* Configure TQAVCTRL register: set transmit mode to 'Qav', 1892 * set data fetch arbitration to 'round robin', set SP_WAIT_SR 1893 * so SP queues wait for SR ones. 1894 */ 1895 val = rd32(E1000_I210_TQAVCTRL); 1896 val |= E1000_TQAVCTRL_XMIT_MODE | E1000_TQAVCTRL_SP_WAIT_SR; 1897 val &= ~E1000_TQAVCTRL_DATAFETCHARB; 1898 wr32(E1000_I210_TQAVCTRL, val); 1899 1900 /* Configure Tx and Rx packet buffers sizes as described in 1901 * i210 datasheet section 7.2.7.7. 1902 */ 1903 val = rd32(E1000_TXPBS); 1904 val &= ~I210_TXPBSIZE_MASK; 1905 val |= I210_TXPBSIZE_PB0_6KB | I210_TXPBSIZE_PB1_6KB | 1906 I210_TXPBSIZE_PB2_6KB | I210_TXPBSIZE_PB3_6KB; 1907 wr32(E1000_TXPBS, val); 1908 1909 val = rd32(E1000_RXPBS); 1910 val &= ~I210_RXPBSIZE_MASK; 1911 val |= I210_RXPBSIZE_PB_30KB; 1912 wr32(E1000_RXPBS, val); 1913 1914 /* Section 8.12.9 states that MAX_TPKT_SIZE from DTXMXPKTSZ 1915 * register should not exceed the buffer size programmed in 1916 * TXPBS. The smallest buffer size programmed in TXPBS is 4kB 1917 * so according to the datasheet we should set MAX_TPKT_SIZE to 1918 * 4kB / 64. 1919 * 1920 * However, when we do so, no frame from queue 2 and 3 are 1921 * transmitted. It seems the MAX_TPKT_SIZE should not be great 1922 * or _equal_ to the buffer size programmed in TXPBS. For this 1923 * reason, we set MAX_ TPKT_SIZE to (4kB - 1) / 64. 1924 */ 1925 val = (4096 - 1) / 64; 1926 wr32(E1000_I210_DTXMXPKTSZ, val); 1927 1928 /* Since FQTSS mode is enabled, apply any CBS configuration 1929 * previously set. If no previous CBS configuration has been 1930 * done, then the initial configuration is applied, which means 1931 * CBS is disabled. 1932 */ 1933 max_queue = (adapter->num_tx_queues < I210_SR_QUEUES_NUM) ? 1934 adapter->num_tx_queues : I210_SR_QUEUES_NUM; 1935 1936 for (i = 0; i < max_queue; i++) { 1937 igb_config_tx_modes(adapter, i); 1938 } 1939 } else { 1940 wr32(E1000_RXPBS, I210_RXPBSIZE_DEFAULT); 1941 wr32(E1000_TXPBS, I210_TXPBSIZE_DEFAULT); 1942 wr32(E1000_I210_DTXMXPKTSZ, I210_DTXMXPKTSZ_DEFAULT); 1943 1944 val = rd32(E1000_I210_TQAVCTRL); 1945 /* According to Section 8.12.21, the other flags we've set when 1946 * enabling FQTSS are not relevant when disabling FQTSS so we 1947 * don't set they here. 1948 */ 1949 val &= ~E1000_TQAVCTRL_XMIT_MODE; 1950 wr32(E1000_I210_TQAVCTRL, val); 1951 } 1952 1953 netdev_dbg(netdev, "FQTSS %s\n", (is_fqtss_enabled(adapter)) ? 1954 "enabled" : "disabled"); 1955 } 1956 1957 /** 1958 * igb_configure - configure the hardware for RX and TX 1959 * @adapter: private board structure 1960 **/ 1961 static void igb_configure(struct igb_adapter *adapter) 1962 { 1963 struct net_device *netdev = adapter->netdev; 1964 int i; 1965 1966 igb_get_hw_control(adapter); 1967 igb_set_rx_mode(netdev); 1968 igb_setup_tx_mode(adapter); 1969 1970 igb_restore_vlan(adapter); 1971 1972 igb_setup_tctl(adapter); 1973 igb_setup_mrqc(adapter); 1974 igb_setup_rctl(adapter); 1975 1976 igb_nfc_filter_restore(adapter); 1977 igb_configure_tx(adapter); 1978 igb_configure_rx(adapter); 1979 1980 igb_rx_fifo_flush_82575(&adapter->hw); 1981 1982 /* call igb_desc_unused which always leaves 1983 * at least 1 descriptor unused to make sure 1984 * next_to_use != next_to_clean 1985 */ 1986 for (i = 0; i < adapter->num_rx_queues; i++) { 1987 struct igb_ring *ring = adapter->rx_ring[i]; 1988 igb_alloc_rx_buffers(ring, igb_desc_unused(ring)); 1989 } 1990 } 1991 1992 /** 1993 * igb_power_up_link - Power up the phy/serdes link 1994 * @adapter: address of board private structure 1995 **/ 1996 void igb_power_up_link(struct igb_adapter *adapter) 1997 { 1998 igb_reset_phy(&adapter->hw); 1999 2000 if (adapter->hw.phy.media_type == e1000_media_type_copper) 2001 igb_power_up_phy_copper(&adapter->hw); 2002 else 2003 igb_power_up_serdes_link_82575(&adapter->hw); 2004 2005 igb_setup_link(&adapter->hw); 2006 } 2007 2008 /** 2009 * igb_power_down_link - Power down the phy/serdes link 2010 * @adapter: address of board private structure 2011 */ 2012 static void igb_power_down_link(struct igb_adapter *adapter) 2013 { 2014 if (adapter->hw.phy.media_type == e1000_media_type_copper) 2015 igb_power_down_phy_copper_82575(&adapter->hw); 2016 else 2017 igb_shutdown_serdes_link_82575(&adapter->hw); 2018 } 2019 2020 /** 2021 * igb_check_swap_media - Detect and switch function for Media Auto Sense 2022 * @adapter: address of the board private structure 2023 **/ 2024 static void igb_check_swap_media(struct igb_adapter *adapter) 2025 { 2026 struct e1000_hw *hw = &adapter->hw; 2027 u32 ctrl_ext, connsw; 2028 bool swap_now = false; 2029 2030 ctrl_ext = rd32(E1000_CTRL_EXT); 2031 connsw = rd32(E1000_CONNSW); 2032 2033 /* need to live swap if current media is copper and we have fiber/serdes 2034 * to go to. 2035 */ 2036 2037 if ((hw->phy.media_type == e1000_media_type_copper) && 2038 (!(connsw & E1000_CONNSW_AUTOSENSE_EN))) { 2039 swap_now = true; 2040 } else if ((hw->phy.media_type != e1000_media_type_copper) && 2041 !(connsw & E1000_CONNSW_SERDESD)) { 2042 /* copper signal takes time to appear */ 2043 if (adapter->copper_tries < 4) { 2044 adapter->copper_tries++; 2045 connsw |= E1000_CONNSW_AUTOSENSE_CONF; 2046 wr32(E1000_CONNSW, connsw); 2047 return; 2048 } else { 2049 adapter->copper_tries = 0; 2050 if ((connsw & E1000_CONNSW_PHYSD) && 2051 (!(connsw & E1000_CONNSW_PHY_PDN))) { 2052 swap_now = true; 2053 connsw &= ~E1000_CONNSW_AUTOSENSE_CONF; 2054 wr32(E1000_CONNSW, connsw); 2055 } 2056 } 2057 } 2058 2059 if (!swap_now) 2060 return; 2061 2062 switch (hw->phy.media_type) { 2063 case e1000_media_type_copper: 2064 netdev_info(adapter->netdev, 2065 "MAS: changing media to fiber/serdes\n"); 2066 ctrl_ext |= 2067 E1000_CTRL_EXT_LINK_MODE_PCIE_SERDES; 2068 adapter->flags |= IGB_FLAG_MEDIA_RESET; 2069 adapter->copper_tries = 0; 2070 break; 2071 case e1000_media_type_internal_serdes: 2072 case e1000_media_type_fiber: 2073 netdev_info(adapter->netdev, 2074 "MAS: changing media to copper\n"); 2075 ctrl_ext &= 2076 ~E1000_CTRL_EXT_LINK_MODE_PCIE_SERDES; 2077 adapter->flags |= IGB_FLAG_MEDIA_RESET; 2078 break; 2079 default: 2080 /* shouldn't get here during regular operation */ 2081 netdev_err(adapter->netdev, 2082 "AMS: Invalid media type found, returning\n"); 2083 break; 2084 } 2085 wr32(E1000_CTRL_EXT, ctrl_ext); 2086 } 2087 2088 /** 2089 * igb_up - Open the interface and prepare it to handle traffic 2090 * @adapter: board private structure 2091 **/ 2092 int igb_up(struct igb_adapter *adapter) 2093 { 2094 struct e1000_hw *hw = &adapter->hw; 2095 int i; 2096 2097 /* hardware has been reset, we need to reload some things */ 2098 igb_configure(adapter); 2099 2100 clear_bit(__IGB_DOWN, &adapter->state); 2101 2102 for (i = 0; i < adapter->num_q_vectors; i++) 2103 napi_enable(&(adapter->q_vector[i]->napi)); 2104 2105 if (adapter->flags & IGB_FLAG_HAS_MSIX) 2106 igb_configure_msix(adapter); 2107 else 2108 igb_assign_vector(adapter->q_vector[0], 0); 2109 2110 /* Clear any pending interrupts. */ 2111 rd32(E1000_TSICR); 2112 rd32(E1000_ICR); 2113 igb_irq_enable(adapter); 2114 2115 /* notify VFs that reset has been completed */ 2116 if (adapter->vfs_allocated_count) { 2117 u32 reg_data = rd32(E1000_CTRL_EXT); 2118 2119 reg_data |= E1000_CTRL_EXT_PFRSTD; 2120 wr32(E1000_CTRL_EXT, reg_data); 2121 } 2122 2123 netif_tx_start_all_queues(adapter->netdev); 2124 2125 /* start the watchdog. */ 2126 hw->mac.get_link_status = 1; 2127 schedule_work(&adapter->watchdog_task); 2128 2129 if ((adapter->flags & IGB_FLAG_EEE) && 2130 (!hw->dev_spec._82575.eee_disable)) 2131 adapter->eee_advert = MDIO_EEE_100TX | MDIO_EEE_1000T; 2132 2133 return 0; 2134 } 2135 2136 void igb_down(struct igb_adapter *adapter) 2137 { 2138 struct net_device *netdev = adapter->netdev; 2139 struct e1000_hw *hw = &adapter->hw; 2140 u32 tctl, rctl; 2141 int i; 2142 2143 /* signal that we're down so the interrupt handler does not 2144 * reschedule our watchdog timer 2145 */ 2146 set_bit(__IGB_DOWN, &adapter->state); 2147 2148 /* disable receives in the hardware */ 2149 rctl = rd32(E1000_RCTL); 2150 wr32(E1000_RCTL, rctl & ~E1000_RCTL_EN); 2151 /* flush and sleep below */ 2152 2153 igb_nfc_filter_exit(adapter); 2154 2155 netif_carrier_off(netdev); 2156 netif_tx_stop_all_queues(netdev); 2157 2158 /* disable transmits in the hardware */ 2159 tctl = rd32(E1000_TCTL); 2160 tctl &= ~E1000_TCTL_EN; 2161 wr32(E1000_TCTL, tctl); 2162 /* flush both disables and wait for them to finish */ 2163 wrfl(); 2164 usleep_range(10000, 11000); 2165 2166 igb_irq_disable(adapter); 2167 2168 adapter->flags &= ~IGB_FLAG_NEED_LINK_UPDATE; 2169 2170 for (i = 0; i < adapter->num_q_vectors; i++) { 2171 if (adapter->q_vector[i]) { 2172 napi_synchronize(&adapter->q_vector[i]->napi); 2173 napi_disable(&adapter->q_vector[i]->napi); 2174 } 2175 } 2176 2177 del_timer_sync(&adapter->watchdog_timer); 2178 del_timer_sync(&adapter->phy_info_timer); 2179 2180 /* record the stats before reset*/ 2181 spin_lock(&adapter->stats64_lock); 2182 igb_update_stats(adapter); 2183 spin_unlock(&adapter->stats64_lock); 2184 2185 adapter->link_speed = 0; 2186 adapter->link_duplex = 0; 2187 2188 if (!pci_channel_offline(adapter->pdev)) 2189 igb_reset(adapter); 2190 2191 /* clear VLAN promisc flag so VFTA will be updated if necessary */ 2192 adapter->flags &= ~IGB_FLAG_VLAN_PROMISC; 2193 2194 igb_clean_all_tx_rings(adapter); 2195 igb_clean_all_rx_rings(adapter); 2196 #ifdef CONFIG_IGB_DCA 2197 2198 /* since we reset the hardware DCA settings were cleared */ 2199 igb_setup_dca(adapter); 2200 #endif 2201 } 2202 2203 void igb_reinit_locked(struct igb_adapter *adapter) 2204 { 2205 while (test_and_set_bit(__IGB_RESETTING, &adapter->state)) 2206 usleep_range(1000, 2000); 2207 igb_down(adapter); 2208 igb_up(adapter); 2209 clear_bit(__IGB_RESETTING, &adapter->state); 2210 } 2211 2212 /** igb_enable_mas - Media Autosense re-enable after swap 2213 * 2214 * @adapter: adapter struct 2215 **/ 2216 static void igb_enable_mas(struct igb_adapter *adapter) 2217 { 2218 struct e1000_hw *hw = &adapter->hw; 2219 u32 connsw = rd32(E1000_CONNSW); 2220 2221 /* configure for SerDes media detect */ 2222 if ((hw->phy.media_type == e1000_media_type_copper) && 2223 (!(connsw & E1000_CONNSW_SERDESD))) { 2224 connsw |= E1000_CONNSW_ENRGSRC; 2225 connsw |= E1000_CONNSW_AUTOSENSE_EN; 2226 wr32(E1000_CONNSW, connsw); 2227 wrfl(); 2228 } 2229 } 2230 2231 #ifdef CONFIG_IGB_HWMON 2232 /** 2233 * igb_set_i2c_bb - Init I2C interface 2234 * @hw: pointer to hardware structure 2235 **/ 2236 static void igb_set_i2c_bb(struct e1000_hw *hw) 2237 { 2238 u32 ctrl_ext; 2239 s32 i2cctl; 2240 2241 ctrl_ext = rd32(E1000_CTRL_EXT); 2242 ctrl_ext |= E1000_CTRL_I2C_ENA; 2243 wr32(E1000_CTRL_EXT, ctrl_ext); 2244 wrfl(); 2245 2246 i2cctl = rd32(E1000_I2CPARAMS); 2247 i2cctl |= E1000_I2CBB_EN 2248 | E1000_I2C_CLK_OE_N 2249 | E1000_I2C_DATA_OE_N; 2250 wr32(E1000_I2CPARAMS, i2cctl); 2251 wrfl(); 2252 } 2253 #endif 2254 2255 void igb_reset(struct igb_adapter *adapter) 2256 { 2257 struct pci_dev *pdev = adapter->pdev; 2258 struct e1000_hw *hw = &adapter->hw; 2259 struct e1000_mac_info *mac = &hw->mac; 2260 struct e1000_fc_info *fc = &hw->fc; 2261 u32 pba, hwm; 2262 2263 /* Repartition Pba for greater than 9k mtu 2264 * To take effect CTRL.RST is required. 2265 */ 2266 switch (mac->type) { 2267 case e1000_i350: 2268 case e1000_i354: 2269 case e1000_82580: 2270 pba = rd32(E1000_RXPBS); 2271 pba = igb_rxpbs_adjust_82580(pba); 2272 break; 2273 case e1000_82576: 2274 pba = rd32(E1000_RXPBS); 2275 pba &= E1000_RXPBS_SIZE_MASK_82576; 2276 break; 2277 case e1000_82575: 2278 case e1000_i210: 2279 case e1000_i211: 2280 default: 2281 pba = E1000_PBA_34K; 2282 break; 2283 } 2284 2285 if (mac->type == e1000_82575) { 2286 u32 min_rx_space, min_tx_space, needed_tx_space; 2287 2288 /* write Rx PBA so that hardware can report correct Tx PBA */ 2289 wr32(E1000_PBA, pba); 2290 2291 /* To maintain wire speed transmits, the Tx FIFO should be 2292 * large enough to accommodate two full transmit packets, 2293 * rounded up to the next 1KB and expressed in KB. Likewise, 2294 * the Rx FIFO should be large enough to accommodate at least 2295 * one full receive packet and is similarly rounded up and 2296 * expressed in KB. 2297 */ 2298 min_rx_space = DIV_ROUND_UP(MAX_JUMBO_FRAME_SIZE, 1024); 2299 2300 /* The Tx FIFO also stores 16 bytes of information about the Tx 2301 * but don't include Ethernet FCS because hardware appends it. 2302 * We only need to round down to the nearest 512 byte block 2303 * count since the value we care about is 2 frames, not 1. 2304 */ 2305 min_tx_space = adapter->max_frame_size; 2306 min_tx_space += sizeof(union e1000_adv_tx_desc) - ETH_FCS_LEN; 2307 min_tx_space = DIV_ROUND_UP(min_tx_space, 512); 2308 2309 /* upper 16 bits has Tx packet buffer allocation size in KB */ 2310 needed_tx_space = min_tx_space - (rd32(E1000_PBA) >> 16); 2311 2312 /* If current Tx allocation is less than the min Tx FIFO size, 2313 * and the min Tx FIFO size is less than the current Rx FIFO 2314 * allocation, take space away from current Rx allocation. 2315 */ 2316 if (needed_tx_space < pba) { 2317 pba -= needed_tx_space; 2318 2319 /* if short on Rx space, Rx wins and must trump Tx 2320 * adjustment 2321 */ 2322 if (pba < min_rx_space) 2323 pba = min_rx_space; 2324 } 2325 2326 /* adjust PBA for jumbo frames */ 2327 wr32(E1000_PBA, pba); 2328 } 2329 2330 /* flow control settings 2331 * The high water mark must be low enough to fit one full frame 2332 * after transmitting the pause frame. As such we must have enough 2333 * space to allow for us to complete our current transmit and then 2334 * receive the frame that is in progress from the link partner. 2335 * Set it to: 2336 * - the full Rx FIFO size minus one full Tx plus one full Rx frame 2337 */ 2338 hwm = (pba << 10) - (adapter->max_frame_size + MAX_JUMBO_FRAME_SIZE); 2339 2340 fc->high_water = hwm & 0xFFFFFFF0; /* 16-byte granularity */ 2341 fc->low_water = fc->high_water - 16; 2342 fc->pause_time = 0xFFFF; 2343 fc->send_xon = 1; 2344 fc->current_mode = fc->requested_mode; 2345 2346 /* disable receive for all VFs and wait one second */ 2347 if (adapter->vfs_allocated_count) { 2348 int i; 2349 2350 for (i = 0 ; i < adapter->vfs_allocated_count; i++) 2351 adapter->vf_data[i].flags &= IGB_VF_FLAG_PF_SET_MAC; 2352 2353 /* ping all the active vfs to let them know we are going down */ 2354 igb_ping_all_vfs(adapter); 2355 2356 /* disable transmits and receives */ 2357 wr32(E1000_VFRE, 0); 2358 wr32(E1000_VFTE, 0); 2359 } 2360 2361 /* Allow time for pending master requests to run */ 2362 hw->mac.ops.reset_hw(hw); 2363 wr32(E1000_WUC, 0); 2364 2365 if (adapter->flags & IGB_FLAG_MEDIA_RESET) { 2366 /* need to resetup here after media swap */ 2367 adapter->ei.get_invariants(hw); 2368 adapter->flags &= ~IGB_FLAG_MEDIA_RESET; 2369 } 2370 if ((mac->type == e1000_82575 || mac->type == e1000_i350) && 2371 (adapter->flags & IGB_FLAG_MAS_ENABLE)) { 2372 igb_enable_mas(adapter); 2373 } 2374 if (hw->mac.ops.init_hw(hw)) 2375 dev_err(&pdev->dev, "Hardware Error\n"); 2376 2377 /* RAR registers were cleared during init_hw, clear mac table */ 2378 igb_flush_mac_table(adapter); 2379 __dev_uc_unsync(adapter->netdev, NULL); 2380 2381 /* Recover default RAR entry */ 2382 igb_set_default_mac_filter(adapter); 2383 2384 /* Flow control settings reset on hardware reset, so guarantee flow 2385 * control is off when forcing speed. 2386 */ 2387 if (!hw->mac.autoneg) 2388 igb_force_mac_fc(hw); 2389 2390 igb_init_dmac(adapter, pba); 2391 #ifdef CONFIG_IGB_HWMON 2392 /* Re-initialize the thermal sensor on i350 devices. */ 2393 if (!test_bit(__IGB_DOWN, &adapter->state)) { 2394 if (mac->type == e1000_i350 && hw->bus.func == 0) { 2395 /* If present, re-initialize the external thermal sensor 2396 * interface. 2397 */ 2398 if (adapter->ets) 2399 igb_set_i2c_bb(hw); 2400 mac->ops.init_thermal_sensor_thresh(hw); 2401 } 2402 } 2403 #endif 2404 /* Re-establish EEE setting */ 2405 if (hw->phy.media_type == e1000_media_type_copper) { 2406 switch (mac->type) { 2407 case e1000_i350: 2408 case e1000_i210: 2409 case e1000_i211: 2410 igb_set_eee_i350(hw, true, true); 2411 break; 2412 case e1000_i354: 2413 igb_set_eee_i354(hw, true, true); 2414 break; 2415 default: 2416 break; 2417 } 2418 } 2419 if (!netif_running(adapter->netdev)) 2420 igb_power_down_link(adapter); 2421 2422 igb_update_mng_vlan(adapter); 2423 2424 /* Enable h/w to recognize an 802.1Q VLAN Ethernet packet */ 2425 wr32(E1000_VET, ETHERNET_IEEE_VLAN_TYPE); 2426 2427 /* Re-enable PTP, where applicable. */ 2428 if (adapter->ptp_flags & IGB_PTP_ENABLED) 2429 igb_ptp_reset(adapter); 2430 2431 igb_get_phy_info(hw); 2432 } 2433 2434 static netdev_features_t igb_fix_features(struct net_device *netdev, 2435 netdev_features_t features) 2436 { 2437 /* Since there is no support for separate Rx/Tx vlan accel 2438 * enable/disable make sure Tx flag is always in same state as Rx. 2439 */ 2440 if (features & NETIF_F_HW_VLAN_CTAG_RX) 2441 features |= NETIF_F_HW_VLAN_CTAG_TX; 2442 else 2443 features &= ~NETIF_F_HW_VLAN_CTAG_TX; 2444 2445 return features; 2446 } 2447 2448 static int igb_set_features(struct net_device *netdev, 2449 netdev_features_t features) 2450 { 2451 netdev_features_t changed = netdev->features ^ features; 2452 struct igb_adapter *adapter = netdev_priv(netdev); 2453 2454 if (changed & NETIF_F_HW_VLAN_CTAG_RX) 2455 igb_vlan_mode(netdev, features); 2456 2457 if (!(changed & (NETIF_F_RXALL | NETIF_F_NTUPLE))) 2458 return 0; 2459 2460 if (!(features & NETIF_F_NTUPLE)) { 2461 struct hlist_node *node2; 2462 struct igb_nfc_filter *rule; 2463 2464 spin_lock(&adapter->nfc_lock); 2465 hlist_for_each_entry_safe(rule, node2, 2466 &adapter->nfc_filter_list, nfc_node) { 2467 igb_erase_filter(adapter, rule); 2468 hlist_del(&rule->nfc_node); 2469 kfree(rule); 2470 } 2471 spin_unlock(&adapter->nfc_lock); 2472 adapter->nfc_filter_count = 0; 2473 } 2474 2475 netdev->features = features; 2476 2477 if (netif_running(netdev)) 2478 igb_reinit_locked(adapter); 2479 else 2480 igb_reset(adapter); 2481 2482 return 1; 2483 } 2484 2485 static int igb_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[], 2486 struct net_device *dev, 2487 const unsigned char *addr, u16 vid, 2488 u16 flags, 2489 struct netlink_ext_ack *extack) 2490 { 2491 /* guarantee we can provide a unique filter for the unicast address */ 2492 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) { 2493 struct igb_adapter *adapter = netdev_priv(dev); 2494 int vfn = adapter->vfs_allocated_count; 2495 2496 if (netdev_uc_count(dev) >= igb_available_rars(adapter, vfn)) 2497 return -ENOMEM; 2498 } 2499 2500 return ndo_dflt_fdb_add(ndm, tb, dev, addr, vid, flags); 2501 } 2502 2503 #define IGB_MAX_MAC_HDR_LEN 127 2504 #define IGB_MAX_NETWORK_HDR_LEN 511 2505 2506 static netdev_features_t 2507 igb_features_check(struct sk_buff *skb, struct net_device *dev, 2508 netdev_features_t features) 2509 { 2510 unsigned int network_hdr_len, mac_hdr_len; 2511 2512 /* Make certain the headers can be described by a context descriptor */ 2513 mac_hdr_len = skb_network_offset(skb); 2514 if (unlikely(mac_hdr_len > IGB_MAX_MAC_HDR_LEN)) 2515 return features & ~(NETIF_F_HW_CSUM | 2516 NETIF_F_SCTP_CRC | 2517 NETIF_F_GSO_UDP_L4 | 2518 NETIF_F_HW_VLAN_CTAG_TX | 2519 NETIF_F_TSO | 2520 NETIF_F_TSO6); 2521 2522 network_hdr_len = skb_checksum_start(skb) - skb_network_header(skb); 2523 if (unlikely(network_hdr_len > IGB_MAX_NETWORK_HDR_LEN)) 2524 return features & ~(NETIF_F_HW_CSUM | 2525 NETIF_F_SCTP_CRC | 2526 NETIF_F_GSO_UDP_L4 | 2527 NETIF_F_TSO | 2528 NETIF_F_TSO6); 2529 2530 /* We can only support IPV4 TSO in tunnels if we can mangle the 2531 * inner IP ID field, so strip TSO if MANGLEID is not supported. 2532 */ 2533 if (skb->encapsulation && !(features & NETIF_F_TSO_MANGLEID)) 2534 features &= ~NETIF_F_TSO; 2535 2536 return features; 2537 } 2538 2539 static void igb_offload_apply(struct igb_adapter *adapter, s32 queue) 2540 { 2541 if (!is_fqtss_enabled(adapter)) { 2542 enable_fqtss(adapter, true); 2543 return; 2544 } 2545 2546 igb_config_tx_modes(adapter, queue); 2547 2548 if (!is_any_cbs_enabled(adapter) && !is_any_txtime_enabled(adapter)) 2549 enable_fqtss(adapter, false); 2550 } 2551 2552 static int igb_offload_cbs(struct igb_adapter *adapter, 2553 struct tc_cbs_qopt_offload *qopt) 2554 { 2555 struct e1000_hw *hw = &adapter->hw; 2556 int err; 2557 2558 /* CBS offloading is only supported by i210 controller. */ 2559 if (hw->mac.type != e1000_i210) 2560 return -EOPNOTSUPP; 2561 2562 /* CBS offloading is only supported by queue 0 and queue 1. */ 2563 if (qopt->queue < 0 || qopt->queue > 1) 2564 return -EINVAL; 2565 2566 err = igb_save_cbs_params(adapter, qopt->queue, qopt->enable, 2567 qopt->idleslope, qopt->sendslope, 2568 qopt->hicredit, qopt->locredit); 2569 if (err) 2570 return err; 2571 2572 igb_offload_apply(adapter, qopt->queue); 2573 2574 return 0; 2575 } 2576 2577 #define ETHER_TYPE_FULL_MASK ((__force __be16)~0) 2578 #define VLAN_PRIO_FULL_MASK (0x07) 2579 2580 static int igb_parse_cls_flower(struct igb_adapter *adapter, 2581 struct flow_cls_offload *f, 2582 int traffic_class, 2583 struct igb_nfc_filter *input) 2584 { 2585 struct flow_rule *rule = flow_cls_offload_flow_rule(f); 2586 struct flow_dissector *dissector = rule->match.dissector; 2587 struct netlink_ext_ack *extack = f->common.extack; 2588 2589 if (dissector->used_keys & 2590 ~(BIT_ULL(FLOW_DISSECTOR_KEY_BASIC) | 2591 BIT_ULL(FLOW_DISSECTOR_KEY_CONTROL) | 2592 BIT_ULL(FLOW_DISSECTOR_KEY_ETH_ADDRS) | 2593 BIT_ULL(FLOW_DISSECTOR_KEY_VLAN))) { 2594 NL_SET_ERR_MSG_MOD(extack, 2595 "Unsupported key used, only BASIC, CONTROL, ETH_ADDRS and VLAN are supported"); 2596 return -EOPNOTSUPP; 2597 } 2598 2599 if (flow_rule_match_has_control_flags(rule, extack)) 2600 return -EOPNOTSUPP; 2601 2602 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) { 2603 struct flow_match_eth_addrs match; 2604 2605 flow_rule_match_eth_addrs(rule, &match); 2606 if (!is_zero_ether_addr(match.mask->dst)) { 2607 if (!is_broadcast_ether_addr(match.mask->dst)) { 2608 NL_SET_ERR_MSG_MOD(extack, "Only full masks are supported for destination MAC address"); 2609 return -EINVAL; 2610 } 2611 2612 input->filter.match_flags |= 2613 IGB_FILTER_FLAG_DST_MAC_ADDR; 2614 ether_addr_copy(input->filter.dst_addr, match.key->dst); 2615 } 2616 2617 if (!is_zero_ether_addr(match.mask->src)) { 2618 if (!is_broadcast_ether_addr(match.mask->src)) { 2619 NL_SET_ERR_MSG_MOD(extack, "Only full masks are supported for source MAC address"); 2620 return -EINVAL; 2621 } 2622 2623 input->filter.match_flags |= 2624 IGB_FILTER_FLAG_SRC_MAC_ADDR; 2625 ether_addr_copy(input->filter.src_addr, match.key->src); 2626 } 2627 } 2628 2629 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) { 2630 struct flow_match_basic match; 2631 2632 flow_rule_match_basic(rule, &match); 2633 if (match.mask->n_proto) { 2634 if (match.mask->n_proto != ETHER_TYPE_FULL_MASK) { 2635 NL_SET_ERR_MSG_MOD(extack, "Only full mask is supported for EtherType filter"); 2636 return -EINVAL; 2637 } 2638 2639 input->filter.match_flags |= IGB_FILTER_FLAG_ETHER_TYPE; 2640 input->filter.etype = match.key->n_proto; 2641 } 2642 } 2643 2644 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) { 2645 struct flow_match_vlan match; 2646 2647 flow_rule_match_vlan(rule, &match); 2648 if (match.mask->vlan_priority) { 2649 if (match.mask->vlan_priority != VLAN_PRIO_FULL_MASK) { 2650 NL_SET_ERR_MSG_MOD(extack, "Only full mask is supported for VLAN priority"); 2651 return -EINVAL; 2652 } 2653 2654 input->filter.match_flags |= IGB_FILTER_FLAG_VLAN_TCI; 2655 input->filter.vlan_tci = 2656 (__force __be16)match.key->vlan_priority; 2657 } 2658 } 2659 2660 input->action = traffic_class; 2661 input->cookie = f->cookie; 2662 2663 return 0; 2664 } 2665 2666 static int igb_configure_clsflower(struct igb_adapter *adapter, 2667 struct flow_cls_offload *cls_flower) 2668 { 2669 struct netlink_ext_ack *extack = cls_flower->common.extack; 2670 struct igb_nfc_filter *filter, *f; 2671 int err, tc; 2672 2673 tc = tc_classid_to_hwtc(adapter->netdev, cls_flower->classid); 2674 if (tc < 0) { 2675 NL_SET_ERR_MSG_MOD(extack, "Invalid traffic class"); 2676 return -EINVAL; 2677 } 2678 2679 filter = kzalloc(sizeof(*filter), GFP_KERNEL); 2680 if (!filter) 2681 return -ENOMEM; 2682 2683 err = igb_parse_cls_flower(adapter, cls_flower, tc, filter); 2684 if (err < 0) 2685 goto err_parse; 2686 2687 spin_lock(&adapter->nfc_lock); 2688 2689 hlist_for_each_entry(f, &adapter->nfc_filter_list, nfc_node) { 2690 if (!memcmp(&f->filter, &filter->filter, sizeof(f->filter))) { 2691 err = -EEXIST; 2692 NL_SET_ERR_MSG_MOD(extack, 2693 "This filter is already set in ethtool"); 2694 goto err_locked; 2695 } 2696 } 2697 2698 hlist_for_each_entry(f, &adapter->cls_flower_list, nfc_node) { 2699 if (!memcmp(&f->filter, &filter->filter, sizeof(f->filter))) { 2700 err = -EEXIST; 2701 NL_SET_ERR_MSG_MOD(extack, 2702 "This filter is already set in cls_flower"); 2703 goto err_locked; 2704 } 2705 } 2706 2707 err = igb_add_filter(adapter, filter); 2708 if (err < 0) { 2709 NL_SET_ERR_MSG_MOD(extack, "Could not add filter to the adapter"); 2710 goto err_locked; 2711 } 2712 2713 hlist_add_head(&filter->nfc_node, &adapter->cls_flower_list); 2714 2715 spin_unlock(&adapter->nfc_lock); 2716 2717 return 0; 2718 2719 err_locked: 2720 spin_unlock(&adapter->nfc_lock); 2721 2722 err_parse: 2723 kfree(filter); 2724 2725 return err; 2726 } 2727 2728 static int igb_delete_clsflower(struct igb_adapter *adapter, 2729 struct flow_cls_offload *cls_flower) 2730 { 2731 struct igb_nfc_filter *filter; 2732 int err; 2733 2734 spin_lock(&adapter->nfc_lock); 2735 2736 hlist_for_each_entry(filter, &adapter->cls_flower_list, nfc_node) 2737 if (filter->cookie == cls_flower->cookie) 2738 break; 2739 2740 if (!filter) { 2741 err = -ENOENT; 2742 goto out; 2743 } 2744 2745 err = igb_erase_filter(adapter, filter); 2746 if (err < 0) 2747 goto out; 2748 2749 hlist_del(&filter->nfc_node); 2750 kfree(filter); 2751 2752 out: 2753 spin_unlock(&adapter->nfc_lock); 2754 2755 return err; 2756 } 2757 2758 static int igb_setup_tc_cls_flower(struct igb_adapter *adapter, 2759 struct flow_cls_offload *cls_flower) 2760 { 2761 switch (cls_flower->command) { 2762 case FLOW_CLS_REPLACE: 2763 return igb_configure_clsflower(adapter, cls_flower); 2764 case FLOW_CLS_DESTROY: 2765 return igb_delete_clsflower(adapter, cls_flower); 2766 case FLOW_CLS_STATS: 2767 return -EOPNOTSUPP; 2768 default: 2769 return -EOPNOTSUPP; 2770 } 2771 } 2772 2773 static int igb_setup_tc_block_cb(enum tc_setup_type type, void *type_data, 2774 void *cb_priv) 2775 { 2776 struct igb_adapter *adapter = cb_priv; 2777 2778 if (!tc_cls_can_offload_and_chain0(adapter->netdev, type_data)) 2779 return -EOPNOTSUPP; 2780 2781 switch (type) { 2782 case TC_SETUP_CLSFLOWER: 2783 return igb_setup_tc_cls_flower(adapter, type_data); 2784 2785 default: 2786 return -EOPNOTSUPP; 2787 } 2788 } 2789 2790 static int igb_offload_txtime(struct igb_adapter *adapter, 2791 struct tc_etf_qopt_offload *qopt) 2792 { 2793 struct e1000_hw *hw = &adapter->hw; 2794 int err; 2795 2796 /* Launchtime offloading is only supported by i210 controller. */ 2797 if (hw->mac.type != e1000_i210) 2798 return -EOPNOTSUPP; 2799 2800 /* Launchtime offloading is only supported by queues 0 and 1. */ 2801 if (qopt->queue < 0 || qopt->queue > 1) 2802 return -EINVAL; 2803 2804 err = igb_save_txtime_params(adapter, qopt->queue, qopt->enable); 2805 if (err) 2806 return err; 2807 2808 igb_offload_apply(adapter, qopt->queue); 2809 2810 return 0; 2811 } 2812 2813 static int igb_tc_query_caps(struct igb_adapter *adapter, 2814 struct tc_query_caps_base *base) 2815 { 2816 switch (base->type) { 2817 case TC_SETUP_QDISC_TAPRIO: { 2818 struct tc_taprio_caps *caps = base->caps; 2819 2820 caps->broken_mqprio = true; 2821 2822 return 0; 2823 } 2824 default: 2825 return -EOPNOTSUPP; 2826 } 2827 } 2828 2829 static LIST_HEAD(igb_block_cb_list); 2830 2831 static int igb_setup_tc(struct net_device *dev, enum tc_setup_type type, 2832 void *type_data) 2833 { 2834 struct igb_adapter *adapter = netdev_priv(dev); 2835 2836 switch (type) { 2837 case TC_QUERY_CAPS: 2838 return igb_tc_query_caps(adapter, type_data); 2839 case TC_SETUP_QDISC_CBS: 2840 return igb_offload_cbs(adapter, type_data); 2841 case TC_SETUP_BLOCK: 2842 return flow_block_cb_setup_simple(type_data, 2843 &igb_block_cb_list, 2844 igb_setup_tc_block_cb, 2845 adapter, adapter, true); 2846 2847 case TC_SETUP_QDISC_ETF: 2848 return igb_offload_txtime(adapter, type_data); 2849 2850 default: 2851 return -EOPNOTSUPP; 2852 } 2853 } 2854 2855 static int igb_xdp_setup(struct net_device *dev, struct netdev_bpf *bpf) 2856 { 2857 int i, frame_size = dev->mtu + IGB_ETH_PKT_HDR_PAD; 2858 struct igb_adapter *adapter = netdev_priv(dev); 2859 struct bpf_prog *prog = bpf->prog, *old_prog; 2860 bool running = netif_running(dev); 2861 bool need_reset; 2862 2863 /* verify igb ring attributes are sufficient for XDP */ 2864 for (i = 0; i < adapter->num_rx_queues; i++) { 2865 struct igb_ring *ring = adapter->rx_ring[i]; 2866 2867 if (frame_size > igb_rx_bufsz(ring)) { 2868 NL_SET_ERR_MSG_MOD(bpf->extack, 2869 "The RX buffer size is too small for the frame size"); 2870 netdev_warn(dev, "XDP RX buffer size %d is too small for the frame size %d\n", 2871 igb_rx_bufsz(ring), frame_size); 2872 return -EINVAL; 2873 } 2874 } 2875 2876 old_prog = xchg(&adapter->xdp_prog, prog); 2877 need_reset = (!!prog != !!old_prog); 2878 2879 /* device is up and bpf is added/removed, must setup the RX queues */ 2880 if (need_reset && running) { 2881 igb_close(dev); 2882 } else { 2883 for (i = 0; i < adapter->num_rx_queues; i++) 2884 (void)xchg(&adapter->rx_ring[i]->xdp_prog, 2885 adapter->xdp_prog); 2886 } 2887 2888 if (old_prog) 2889 bpf_prog_put(old_prog); 2890 2891 /* bpf is just replaced, RXQ and MTU are already setup */ 2892 if (!need_reset) { 2893 return 0; 2894 } else { 2895 if (prog) 2896 xdp_features_set_redirect_target(dev, true); 2897 else 2898 xdp_features_clear_redirect_target(dev); 2899 } 2900 2901 if (running) 2902 igb_open(dev); 2903 2904 return 0; 2905 } 2906 2907 static int igb_xdp(struct net_device *dev, struct netdev_bpf *xdp) 2908 { 2909 switch (xdp->command) { 2910 case XDP_SETUP_PROG: 2911 return igb_xdp_setup(dev, xdp); 2912 default: 2913 return -EINVAL; 2914 } 2915 } 2916 2917 static void igb_xdp_ring_update_tail(struct igb_ring *ring) 2918 { 2919 /* Force memory writes to complete before letting h/w know there 2920 * are new descriptors to fetch. 2921 */ 2922 wmb(); 2923 writel(ring->next_to_use, ring->tail); 2924 } 2925 2926 static struct igb_ring *igb_xdp_tx_queue_mapping(struct igb_adapter *adapter) 2927 { 2928 unsigned int r_idx = smp_processor_id(); 2929 2930 if (r_idx >= adapter->num_tx_queues) 2931 r_idx = r_idx % adapter->num_tx_queues; 2932 2933 return adapter->tx_ring[r_idx]; 2934 } 2935 2936 static int igb_xdp_xmit_back(struct igb_adapter *adapter, struct xdp_buff *xdp) 2937 { 2938 struct xdp_frame *xdpf = xdp_convert_buff_to_frame(xdp); 2939 int cpu = smp_processor_id(); 2940 struct igb_ring *tx_ring; 2941 struct netdev_queue *nq; 2942 u32 ret; 2943 2944 if (unlikely(!xdpf)) 2945 return IGB_XDP_CONSUMED; 2946 2947 /* During program transitions its possible adapter->xdp_prog is assigned 2948 * but ring has not been configured yet. In this case simply abort xmit. 2949 */ 2950 tx_ring = adapter->xdp_prog ? igb_xdp_tx_queue_mapping(adapter) : NULL; 2951 if (unlikely(!tx_ring)) 2952 return IGB_XDP_CONSUMED; 2953 2954 nq = txring_txq(tx_ring); 2955 __netif_tx_lock(nq, cpu); 2956 /* Avoid transmit queue timeout since we share it with the slow path */ 2957 txq_trans_cond_update(nq); 2958 ret = igb_xmit_xdp_ring(adapter, tx_ring, xdpf); 2959 __netif_tx_unlock(nq); 2960 2961 return ret; 2962 } 2963 2964 static int igb_xdp_xmit(struct net_device *dev, int n, 2965 struct xdp_frame **frames, u32 flags) 2966 { 2967 struct igb_adapter *adapter = netdev_priv(dev); 2968 int cpu = smp_processor_id(); 2969 struct igb_ring *tx_ring; 2970 struct netdev_queue *nq; 2971 int nxmit = 0; 2972 int i; 2973 2974 if (unlikely(test_bit(__IGB_DOWN, &adapter->state))) 2975 return -ENETDOWN; 2976 2977 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) 2978 return -EINVAL; 2979 2980 /* During program transitions its possible adapter->xdp_prog is assigned 2981 * but ring has not been configured yet. In this case simply abort xmit. 2982 */ 2983 tx_ring = adapter->xdp_prog ? igb_xdp_tx_queue_mapping(adapter) : NULL; 2984 if (unlikely(!tx_ring)) 2985 return -ENXIO; 2986 2987 nq = txring_txq(tx_ring); 2988 __netif_tx_lock(nq, cpu); 2989 2990 /* Avoid transmit queue timeout since we share it with the slow path */ 2991 txq_trans_cond_update(nq); 2992 2993 for (i = 0; i < n; i++) { 2994 struct xdp_frame *xdpf = frames[i]; 2995 int err; 2996 2997 err = igb_xmit_xdp_ring(adapter, tx_ring, xdpf); 2998 if (err != IGB_XDP_TX) 2999 break; 3000 nxmit++; 3001 } 3002 3003 __netif_tx_unlock(nq); 3004 3005 if (unlikely(flags & XDP_XMIT_FLUSH)) 3006 igb_xdp_ring_update_tail(tx_ring); 3007 3008 return nxmit; 3009 } 3010 3011 static const struct net_device_ops igb_netdev_ops = { 3012 .ndo_open = igb_open, 3013 .ndo_stop = igb_close, 3014 .ndo_start_xmit = igb_xmit_frame, 3015 .ndo_get_stats64 = igb_get_stats64, 3016 .ndo_set_rx_mode = igb_set_rx_mode, 3017 .ndo_set_mac_address = igb_set_mac, 3018 .ndo_change_mtu = igb_change_mtu, 3019 .ndo_eth_ioctl = igb_ioctl, 3020 .ndo_tx_timeout = igb_tx_timeout, 3021 .ndo_validate_addr = eth_validate_addr, 3022 .ndo_vlan_rx_add_vid = igb_vlan_rx_add_vid, 3023 .ndo_vlan_rx_kill_vid = igb_vlan_rx_kill_vid, 3024 .ndo_set_vf_mac = igb_ndo_set_vf_mac, 3025 .ndo_set_vf_vlan = igb_ndo_set_vf_vlan, 3026 .ndo_set_vf_rate = igb_ndo_set_vf_bw, 3027 .ndo_set_vf_spoofchk = igb_ndo_set_vf_spoofchk, 3028 .ndo_set_vf_trust = igb_ndo_set_vf_trust, 3029 .ndo_get_vf_config = igb_ndo_get_vf_config, 3030 .ndo_fix_features = igb_fix_features, 3031 .ndo_set_features = igb_set_features, 3032 .ndo_fdb_add = igb_ndo_fdb_add, 3033 .ndo_features_check = igb_features_check, 3034 .ndo_setup_tc = igb_setup_tc, 3035 .ndo_bpf = igb_xdp, 3036 .ndo_xdp_xmit = igb_xdp_xmit, 3037 }; 3038 3039 /** 3040 * igb_set_fw_version - Configure version string for ethtool 3041 * @adapter: adapter struct 3042 **/ 3043 void igb_set_fw_version(struct igb_adapter *adapter) 3044 { 3045 struct e1000_hw *hw = &adapter->hw; 3046 struct e1000_fw_version fw; 3047 3048 igb_get_fw_version(hw, &fw); 3049 3050 switch (hw->mac.type) { 3051 case e1000_i210: 3052 case e1000_i211: 3053 if (!(igb_get_flash_presence_i210(hw))) { 3054 snprintf(adapter->fw_version, 3055 sizeof(adapter->fw_version), 3056 "%2d.%2d-%d", 3057 fw.invm_major, fw.invm_minor, 3058 fw.invm_img_type); 3059 break; 3060 } 3061 fallthrough; 3062 default: 3063 /* if option rom is valid, display its version too */ 3064 if (fw.or_valid) { 3065 snprintf(adapter->fw_version, 3066 sizeof(adapter->fw_version), 3067 "%d.%d, 0x%08x, %d.%d.%d", 3068 fw.eep_major, fw.eep_minor, fw.etrack_id, 3069 fw.or_major, fw.or_build, fw.or_patch); 3070 /* no option rom */ 3071 } else if (fw.etrack_id != 0X0000) { 3072 snprintf(adapter->fw_version, 3073 sizeof(adapter->fw_version), 3074 "%d.%d, 0x%08x", 3075 fw.eep_major, fw.eep_minor, fw.etrack_id); 3076 } else { 3077 snprintf(adapter->fw_version, 3078 sizeof(adapter->fw_version), 3079 "%d.%d.%d", 3080 fw.eep_major, fw.eep_minor, fw.eep_build); 3081 } 3082 break; 3083 } 3084 } 3085 3086 /** 3087 * igb_init_mas - init Media Autosense feature if enabled in the NVM 3088 * 3089 * @adapter: adapter struct 3090 **/ 3091 static void igb_init_mas(struct igb_adapter *adapter) 3092 { 3093 struct e1000_hw *hw = &adapter->hw; 3094 u16 eeprom_data; 3095 3096 hw->nvm.ops.read(hw, NVM_COMPAT, 1, &eeprom_data); 3097 switch (hw->bus.func) { 3098 case E1000_FUNC_0: 3099 if (eeprom_data & IGB_MAS_ENABLE_0) { 3100 adapter->flags |= IGB_FLAG_MAS_ENABLE; 3101 netdev_info(adapter->netdev, 3102 "MAS: Enabling Media Autosense for port %d\n", 3103 hw->bus.func); 3104 } 3105 break; 3106 case E1000_FUNC_1: 3107 if (eeprom_data & IGB_MAS_ENABLE_1) { 3108 adapter->flags |= IGB_FLAG_MAS_ENABLE; 3109 netdev_info(adapter->netdev, 3110 "MAS: Enabling Media Autosense for port %d\n", 3111 hw->bus.func); 3112 } 3113 break; 3114 case E1000_FUNC_2: 3115 if (eeprom_data & IGB_MAS_ENABLE_2) { 3116 adapter->flags |= IGB_FLAG_MAS_ENABLE; 3117 netdev_info(adapter->netdev, 3118 "MAS: Enabling Media Autosense for port %d\n", 3119 hw->bus.func); 3120 } 3121 break; 3122 case E1000_FUNC_3: 3123 if (eeprom_data & IGB_MAS_ENABLE_3) { 3124 adapter->flags |= IGB_FLAG_MAS_ENABLE; 3125 netdev_info(adapter->netdev, 3126 "MAS: Enabling Media Autosense for port %d\n", 3127 hw->bus.func); 3128 } 3129 break; 3130 default: 3131 /* Shouldn't get here */ 3132 netdev_err(adapter->netdev, 3133 "MAS: Invalid port configuration, returning\n"); 3134 break; 3135 } 3136 } 3137 3138 /** 3139 * igb_init_i2c - Init I2C interface 3140 * @adapter: pointer to adapter structure 3141 **/ 3142 static s32 igb_init_i2c(struct igb_adapter *adapter) 3143 { 3144 s32 status = 0; 3145 3146 /* I2C interface supported on i350 devices */ 3147 if (adapter->hw.mac.type != e1000_i350) 3148 return 0; 3149 3150 /* Initialize the i2c bus which is controlled by the registers. 3151 * This bus will use the i2c_algo_bit structure that implements 3152 * the protocol through toggling of the 4 bits in the register. 3153 */ 3154 adapter->i2c_adap.owner = THIS_MODULE; 3155 adapter->i2c_algo = igb_i2c_algo; 3156 adapter->i2c_algo.data = adapter; 3157 adapter->i2c_adap.algo_data = &adapter->i2c_algo; 3158 adapter->i2c_adap.dev.parent = &adapter->pdev->dev; 3159 strscpy(adapter->i2c_adap.name, "igb BB", 3160 sizeof(adapter->i2c_adap.name)); 3161 status = i2c_bit_add_bus(&adapter->i2c_adap); 3162 return status; 3163 } 3164 3165 /** 3166 * igb_probe - Device Initialization Routine 3167 * @pdev: PCI device information struct 3168 * @ent: entry in igb_pci_tbl 3169 * 3170 * Returns 0 on success, negative on failure 3171 * 3172 * igb_probe initializes an adapter identified by a pci_dev structure. 3173 * The OS initialization, configuring of the adapter private structure, 3174 * and a hardware reset occur. 3175 **/ 3176 static int igb_probe(struct pci_dev *pdev, const struct pci_device_id *ent) 3177 { 3178 struct net_device *netdev; 3179 struct igb_adapter *adapter; 3180 struct e1000_hw *hw; 3181 u16 eeprom_data = 0; 3182 s32 ret_val; 3183 static int global_quad_port_a; /* global quad port a indication */ 3184 const struct e1000_info *ei = igb_info_tbl[ent->driver_data]; 3185 u8 part_str[E1000_PBANUM_LENGTH]; 3186 int err; 3187 3188 /* Catch broken hardware that put the wrong VF device ID in 3189 * the PCIe SR-IOV capability. 3190 */ 3191 if (pdev->is_virtfn) { 3192 WARN(1, KERN_ERR "%s (%x:%x) should not be a VF!\n", 3193 pci_name(pdev), pdev->vendor, pdev->device); 3194 return -EINVAL; 3195 } 3196 3197 err = pci_enable_device_mem(pdev); 3198 if (err) 3199 return err; 3200 3201 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)); 3202 if (err) { 3203 dev_err(&pdev->dev, 3204 "No usable DMA configuration, aborting\n"); 3205 goto err_dma; 3206 } 3207 3208 err = pci_request_mem_regions(pdev, igb_driver_name); 3209 if (err) 3210 goto err_pci_reg; 3211 3212 pci_set_master(pdev); 3213 pci_save_state(pdev); 3214 3215 err = -ENOMEM; 3216 netdev = alloc_etherdev_mq(sizeof(struct igb_adapter), 3217 IGB_MAX_TX_QUEUES); 3218 if (!netdev) 3219 goto err_alloc_etherdev; 3220 3221 SET_NETDEV_DEV(netdev, &pdev->dev); 3222 3223 pci_set_drvdata(pdev, netdev); 3224 adapter = netdev_priv(netdev); 3225 adapter->netdev = netdev; 3226 adapter->pdev = pdev; 3227 hw = &adapter->hw; 3228 hw->back = adapter; 3229 adapter->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE); 3230 3231 err = -EIO; 3232 adapter->io_addr = pci_iomap(pdev, 0, 0); 3233 if (!adapter->io_addr) 3234 goto err_ioremap; 3235 /* hw->hw_addr can be altered, we'll use adapter->io_addr for unmap */ 3236 hw->hw_addr = adapter->io_addr; 3237 3238 netdev->netdev_ops = &igb_netdev_ops; 3239 igb_set_ethtool_ops(netdev); 3240 netdev->watchdog_timeo = 5 * HZ; 3241 3242 strscpy(netdev->name, pci_name(pdev), sizeof(netdev->name)); 3243 3244 netdev->mem_start = pci_resource_start(pdev, 0); 3245 netdev->mem_end = pci_resource_end(pdev, 0); 3246 3247 /* PCI config space info */ 3248 hw->vendor_id = pdev->vendor; 3249 hw->device_id = pdev->device; 3250 hw->revision_id = pdev->revision; 3251 hw->subsystem_vendor_id = pdev->subsystem_vendor; 3252 hw->subsystem_device_id = pdev->subsystem_device; 3253 3254 /* Copy the default MAC, PHY and NVM function pointers */ 3255 memcpy(&hw->mac.ops, ei->mac_ops, sizeof(hw->mac.ops)); 3256 memcpy(&hw->phy.ops, ei->phy_ops, sizeof(hw->phy.ops)); 3257 memcpy(&hw->nvm.ops, ei->nvm_ops, sizeof(hw->nvm.ops)); 3258 /* Initialize skew-specific constants */ 3259 err = ei->get_invariants(hw); 3260 if (err) 3261 goto err_sw_init; 3262 3263 /* setup the private structure */ 3264 err = igb_sw_init(adapter); 3265 if (err) 3266 goto err_sw_init; 3267 3268 igb_get_bus_info_pcie(hw); 3269 3270 hw->phy.autoneg_wait_to_complete = false; 3271 3272 /* Copper options */ 3273 if (hw->phy.media_type == e1000_media_type_copper) { 3274 hw->phy.mdix = AUTO_ALL_MODES; 3275 hw->phy.disable_polarity_correction = false; 3276 hw->phy.ms_type = e1000_ms_hw_default; 3277 } 3278 3279 if (igb_check_reset_block(hw)) 3280 dev_info(&pdev->dev, 3281 "PHY reset is blocked due to SOL/IDER session.\n"); 3282 3283 /* features is initialized to 0 in allocation, it might have bits 3284 * set by igb_sw_init so we should use an or instead of an 3285 * assignment. 3286 */ 3287 netdev->features |= NETIF_F_SG | 3288 NETIF_F_TSO | 3289 NETIF_F_TSO6 | 3290 NETIF_F_RXHASH | 3291 NETIF_F_RXCSUM | 3292 NETIF_F_HW_CSUM; 3293 3294 if (hw->mac.type >= e1000_82576) 3295 netdev->features |= NETIF_F_SCTP_CRC | NETIF_F_GSO_UDP_L4; 3296 3297 if (hw->mac.type >= e1000_i350) 3298 netdev->features |= NETIF_F_HW_TC; 3299 3300 #define IGB_GSO_PARTIAL_FEATURES (NETIF_F_GSO_GRE | \ 3301 NETIF_F_GSO_GRE_CSUM | \ 3302 NETIF_F_GSO_IPXIP4 | \ 3303 NETIF_F_GSO_IPXIP6 | \ 3304 NETIF_F_GSO_UDP_TUNNEL | \ 3305 NETIF_F_GSO_UDP_TUNNEL_CSUM) 3306 3307 netdev->gso_partial_features = IGB_GSO_PARTIAL_FEATURES; 3308 netdev->features |= NETIF_F_GSO_PARTIAL | IGB_GSO_PARTIAL_FEATURES; 3309 3310 /* copy netdev features into list of user selectable features */ 3311 netdev->hw_features |= netdev->features | 3312 NETIF_F_HW_VLAN_CTAG_RX | 3313 NETIF_F_HW_VLAN_CTAG_TX | 3314 NETIF_F_RXALL; 3315 3316 if (hw->mac.type >= e1000_i350) 3317 netdev->hw_features |= NETIF_F_NTUPLE; 3318 3319 netdev->features |= NETIF_F_HIGHDMA; 3320 3321 netdev->vlan_features |= netdev->features | NETIF_F_TSO_MANGLEID; 3322 netdev->mpls_features |= NETIF_F_HW_CSUM; 3323 netdev->hw_enc_features |= netdev->vlan_features; 3324 3325 /* set this bit last since it cannot be part of vlan_features */ 3326 netdev->features |= NETIF_F_HW_VLAN_CTAG_FILTER | 3327 NETIF_F_HW_VLAN_CTAG_RX | 3328 NETIF_F_HW_VLAN_CTAG_TX; 3329 3330 netdev->priv_flags |= IFF_SUPP_NOFCS; 3331 3332 netdev->priv_flags |= IFF_UNICAST_FLT; 3333 netdev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT; 3334 3335 /* MTU range: 68 - 9216 */ 3336 netdev->min_mtu = ETH_MIN_MTU; 3337 netdev->max_mtu = MAX_STD_JUMBO_FRAME_SIZE; 3338 3339 adapter->en_mng_pt = igb_enable_mng_pass_thru(hw); 3340 3341 /* before reading the NVM, reset the controller to put the device in a 3342 * known good starting state 3343 */ 3344 hw->mac.ops.reset_hw(hw); 3345 3346 /* make sure the NVM is good , i211/i210 parts can have special NVM 3347 * that doesn't contain a checksum 3348 */ 3349 switch (hw->mac.type) { 3350 case e1000_i210: 3351 case e1000_i211: 3352 if (igb_get_flash_presence_i210(hw)) { 3353 if (hw->nvm.ops.validate(hw) < 0) { 3354 dev_err(&pdev->dev, 3355 "The NVM Checksum Is Not Valid\n"); 3356 err = -EIO; 3357 goto err_eeprom; 3358 } 3359 } 3360 break; 3361 default: 3362 if (hw->nvm.ops.validate(hw) < 0) { 3363 dev_err(&pdev->dev, "The NVM Checksum Is Not Valid\n"); 3364 err = -EIO; 3365 goto err_eeprom; 3366 } 3367 break; 3368 } 3369 3370 if (eth_platform_get_mac_address(&pdev->dev, hw->mac.addr)) { 3371 /* copy the MAC address out of the NVM */ 3372 if (hw->mac.ops.read_mac_addr(hw)) 3373 dev_err(&pdev->dev, "NVM Read Error\n"); 3374 } 3375 3376 eth_hw_addr_set(netdev, hw->mac.addr); 3377 3378 if (!is_valid_ether_addr(netdev->dev_addr)) { 3379 dev_err(&pdev->dev, "Invalid MAC Address\n"); 3380 err = -EIO; 3381 goto err_eeprom; 3382 } 3383 3384 igb_set_default_mac_filter(adapter); 3385 3386 /* get firmware version for ethtool -i */ 3387 igb_set_fw_version(adapter); 3388 3389 /* configure RXPBSIZE and TXPBSIZE */ 3390 if (hw->mac.type == e1000_i210) { 3391 wr32(E1000_RXPBS, I210_RXPBSIZE_DEFAULT); 3392 wr32(E1000_TXPBS, I210_TXPBSIZE_DEFAULT); 3393 } 3394 3395 timer_setup(&adapter->watchdog_timer, igb_watchdog, 0); 3396 timer_setup(&adapter->phy_info_timer, igb_update_phy_info, 0); 3397 3398 INIT_WORK(&adapter->reset_task, igb_reset_task); 3399 INIT_WORK(&adapter->watchdog_task, igb_watchdog_task); 3400 3401 /* Initialize link properties that are user-changeable */ 3402 adapter->fc_autoneg = true; 3403 hw->mac.autoneg = true; 3404 hw->phy.autoneg_advertised = 0x2f; 3405 3406 hw->fc.requested_mode = e1000_fc_default; 3407 hw->fc.current_mode = e1000_fc_default; 3408 3409 igb_validate_mdi_setting(hw); 3410 3411 /* By default, support wake on port A */ 3412 if (hw->bus.func == 0) 3413 adapter->flags |= IGB_FLAG_WOL_SUPPORTED; 3414 3415 /* Check the NVM for wake support on non-port A ports */ 3416 if (hw->mac.type >= e1000_82580) 3417 hw->nvm.ops.read(hw, NVM_INIT_CONTROL3_PORT_A + 3418 NVM_82580_LAN_FUNC_OFFSET(hw->bus.func), 1, 3419 &eeprom_data); 3420 else if (hw->bus.func == 1) 3421 hw->nvm.ops.read(hw, NVM_INIT_CONTROL3_PORT_B, 1, &eeprom_data); 3422 3423 if (eeprom_data & IGB_EEPROM_APME) 3424 adapter->flags |= IGB_FLAG_WOL_SUPPORTED; 3425 3426 /* now that we have the eeprom settings, apply the special cases where 3427 * the eeprom may be wrong or the board simply won't support wake on 3428 * lan on a particular port 3429 */ 3430 switch (pdev->device) { 3431 case E1000_DEV_ID_82575GB_QUAD_COPPER: 3432 adapter->flags &= ~IGB_FLAG_WOL_SUPPORTED; 3433 break; 3434 case E1000_DEV_ID_82575EB_FIBER_SERDES: 3435 case E1000_DEV_ID_82576_FIBER: 3436 case E1000_DEV_ID_82576_SERDES: 3437 /* Wake events only supported on port A for dual fiber 3438 * regardless of eeprom setting 3439 */ 3440 if (rd32(E1000_STATUS) & E1000_STATUS_FUNC_1) 3441 adapter->flags &= ~IGB_FLAG_WOL_SUPPORTED; 3442 break; 3443 case E1000_DEV_ID_82576_QUAD_COPPER: 3444 case E1000_DEV_ID_82576_QUAD_COPPER_ET2: 3445 /* if quad port adapter, disable WoL on all but port A */ 3446 if (global_quad_port_a != 0) 3447 adapter->flags &= ~IGB_FLAG_WOL_SUPPORTED; 3448 else 3449 adapter->flags |= IGB_FLAG_QUAD_PORT_A; 3450 /* Reset for multiple quad port adapters */ 3451 if (++global_quad_port_a == 4) 3452 global_quad_port_a = 0; 3453 break; 3454 default: 3455 /* If the device can't wake, don't set software support */ 3456 if (!device_can_wakeup(&adapter->pdev->dev)) 3457 adapter->flags &= ~IGB_FLAG_WOL_SUPPORTED; 3458 } 3459 3460 /* initialize the wol settings based on the eeprom settings */ 3461 if (adapter->flags & IGB_FLAG_WOL_SUPPORTED) 3462 adapter->wol |= E1000_WUFC_MAG; 3463 3464 /* Some vendors want WoL disabled by default, but still supported */ 3465 if ((hw->mac.type == e1000_i350) && 3466 (pdev->subsystem_vendor == PCI_VENDOR_ID_HP)) { 3467 adapter->flags |= IGB_FLAG_WOL_SUPPORTED; 3468 adapter->wol = 0; 3469 } 3470 3471 /* Some vendors want the ability to Use the EEPROM setting as 3472 * enable/disable only, and not for capability 3473 */ 3474 if (((hw->mac.type == e1000_i350) || 3475 (hw->mac.type == e1000_i354)) && 3476 (pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)) { 3477 adapter->flags |= IGB_FLAG_WOL_SUPPORTED; 3478 adapter->wol = 0; 3479 } 3480 if (hw->mac.type == e1000_i350) { 3481 if (((pdev->subsystem_device == 0x5001) || 3482 (pdev->subsystem_device == 0x5002)) && 3483 (hw->bus.func == 0)) { 3484 adapter->flags |= IGB_FLAG_WOL_SUPPORTED; 3485 adapter->wol = 0; 3486 } 3487 if (pdev->subsystem_device == 0x1F52) 3488 adapter->flags |= IGB_FLAG_WOL_SUPPORTED; 3489 } 3490 3491 device_set_wakeup_enable(&adapter->pdev->dev, 3492 adapter->flags & IGB_FLAG_WOL_SUPPORTED); 3493 3494 /* reset the hardware with the new settings */ 3495 igb_reset(adapter); 3496 3497 /* Init the I2C interface */ 3498 err = igb_init_i2c(adapter); 3499 if (err) { 3500 dev_err(&pdev->dev, "failed to init i2c interface\n"); 3501 goto err_eeprom; 3502 } 3503 3504 /* let the f/w know that the h/w is now under the control of the 3505 * driver. 3506 */ 3507 igb_get_hw_control(adapter); 3508 3509 strcpy(netdev->name, "eth%d"); 3510 err = register_netdev(netdev); 3511 if (err) 3512 goto err_register; 3513 3514 /* carrier off reporting is important to ethtool even BEFORE open */ 3515 netif_carrier_off(netdev); 3516 3517 #ifdef CONFIG_IGB_DCA 3518 if (dca_add_requester(&pdev->dev) == 0) { 3519 adapter->flags |= IGB_FLAG_DCA_ENABLED; 3520 dev_info(&pdev->dev, "DCA enabled\n"); 3521 igb_setup_dca(adapter); 3522 } 3523 3524 #endif 3525 #ifdef CONFIG_IGB_HWMON 3526 /* Initialize the thermal sensor on i350 devices. */ 3527 if (hw->mac.type == e1000_i350 && hw->bus.func == 0) { 3528 u16 ets_word; 3529 3530 /* Read the NVM to determine if this i350 device supports an 3531 * external thermal sensor. 3532 */ 3533 hw->nvm.ops.read(hw, NVM_ETS_CFG, 1, &ets_word); 3534 if (ets_word != 0x0000 && ets_word != 0xFFFF) 3535 adapter->ets = true; 3536 else 3537 adapter->ets = false; 3538 /* Only enable I2C bit banging if an external thermal 3539 * sensor is supported. 3540 */ 3541 if (adapter->ets) 3542 igb_set_i2c_bb(hw); 3543 hw->mac.ops.init_thermal_sensor_thresh(hw); 3544 if (igb_sysfs_init(adapter)) 3545 dev_err(&pdev->dev, 3546 "failed to allocate sysfs resources\n"); 3547 } else { 3548 adapter->ets = false; 3549 } 3550 #endif 3551 /* Check if Media Autosense is enabled */ 3552 adapter->ei = *ei; 3553 if (hw->dev_spec._82575.mas_capable) 3554 igb_init_mas(adapter); 3555 3556 /* do hw tstamp init after resetting */ 3557 igb_ptp_init(adapter); 3558 3559 dev_info(&pdev->dev, "Intel(R) Gigabit Ethernet Network Connection\n"); 3560 /* print bus type/speed/width info, not applicable to i354 */ 3561 if (hw->mac.type != e1000_i354) { 3562 dev_info(&pdev->dev, "%s: (PCIe:%s:%s) %pM\n", 3563 netdev->name, 3564 ((hw->bus.speed == e1000_bus_speed_2500) ? "2.5Gb/s" : 3565 (hw->bus.speed == e1000_bus_speed_5000) ? "5.0Gb/s" : 3566 "unknown"), 3567 ((hw->bus.width == e1000_bus_width_pcie_x4) ? 3568 "Width x4" : 3569 (hw->bus.width == e1000_bus_width_pcie_x2) ? 3570 "Width x2" : 3571 (hw->bus.width == e1000_bus_width_pcie_x1) ? 3572 "Width x1" : "unknown"), netdev->dev_addr); 3573 } 3574 3575 if ((hw->mac.type == e1000_82576 && 3576 rd32(E1000_EECD) & E1000_EECD_PRES) || 3577 (hw->mac.type >= e1000_i210 || 3578 igb_get_flash_presence_i210(hw))) { 3579 ret_val = igb_read_part_string(hw, part_str, 3580 E1000_PBANUM_LENGTH); 3581 } else { 3582 ret_val = -E1000_ERR_INVM_VALUE_NOT_FOUND; 3583 } 3584 3585 if (ret_val) 3586 strcpy(part_str, "Unknown"); 3587 dev_info(&pdev->dev, "%s: PBA No: %s\n", netdev->name, part_str); 3588 dev_info(&pdev->dev, 3589 "Using %s interrupts. %d rx queue(s), %d tx queue(s)\n", 3590 (adapter->flags & IGB_FLAG_HAS_MSIX) ? "MSI-X" : 3591 (adapter->flags & IGB_FLAG_HAS_MSI) ? "MSI" : "legacy", 3592 adapter->num_rx_queues, adapter->num_tx_queues); 3593 if (hw->phy.media_type == e1000_media_type_copper) { 3594 switch (hw->mac.type) { 3595 case e1000_i350: 3596 case e1000_i210: 3597 case e1000_i211: 3598 /* Enable EEE for internal copper PHY devices */ 3599 err = igb_set_eee_i350(hw, true, true); 3600 if ((!err) && 3601 (!hw->dev_spec._82575.eee_disable)) { 3602 adapter->eee_advert = 3603 MDIO_EEE_100TX | MDIO_EEE_1000T; 3604 adapter->flags |= IGB_FLAG_EEE; 3605 } 3606 break; 3607 case e1000_i354: 3608 if ((rd32(E1000_CTRL_EXT) & 3609 E1000_CTRL_EXT_LINK_MODE_SGMII)) { 3610 err = igb_set_eee_i354(hw, true, true); 3611 if ((!err) && 3612 (!hw->dev_spec._82575.eee_disable)) { 3613 adapter->eee_advert = 3614 MDIO_EEE_100TX | MDIO_EEE_1000T; 3615 adapter->flags |= IGB_FLAG_EEE; 3616 } 3617 } 3618 break; 3619 default: 3620 break; 3621 } 3622 } 3623 3624 dev_pm_set_driver_flags(&pdev->dev, DPM_FLAG_NO_DIRECT_COMPLETE); 3625 3626 pm_runtime_put_noidle(&pdev->dev); 3627 return 0; 3628 3629 err_register: 3630 igb_release_hw_control(adapter); 3631 memset(&adapter->i2c_adap, 0, sizeof(adapter->i2c_adap)); 3632 err_eeprom: 3633 if (!igb_check_reset_block(hw)) 3634 igb_reset_phy(hw); 3635 3636 if (hw->flash_address) 3637 iounmap(hw->flash_address); 3638 err_sw_init: 3639 kfree(adapter->mac_table); 3640 kfree(adapter->shadow_vfta); 3641 igb_clear_interrupt_scheme(adapter); 3642 #ifdef CONFIG_PCI_IOV 3643 igb_disable_sriov(pdev, false); 3644 #endif 3645 pci_iounmap(pdev, adapter->io_addr); 3646 err_ioremap: 3647 free_netdev(netdev); 3648 err_alloc_etherdev: 3649 pci_release_mem_regions(pdev); 3650 err_pci_reg: 3651 err_dma: 3652 pci_disable_device(pdev); 3653 return err; 3654 } 3655 3656 #ifdef CONFIG_PCI_IOV 3657 static int igb_sriov_reinit(struct pci_dev *dev) 3658 { 3659 struct net_device *netdev = pci_get_drvdata(dev); 3660 struct igb_adapter *adapter = netdev_priv(netdev); 3661 struct pci_dev *pdev = adapter->pdev; 3662 3663 rtnl_lock(); 3664 3665 if (netif_running(netdev)) 3666 igb_close(netdev); 3667 else 3668 igb_reset(adapter); 3669 3670 igb_clear_interrupt_scheme(adapter); 3671 3672 igb_init_queue_configuration(adapter); 3673 3674 if (igb_init_interrupt_scheme(adapter, true)) { 3675 rtnl_unlock(); 3676 dev_err(&pdev->dev, "Unable to allocate memory for queues\n"); 3677 return -ENOMEM; 3678 } 3679 3680 if (netif_running(netdev)) 3681 igb_open(netdev); 3682 3683 rtnl_unlock(); 3684 3685 return 0; 3686 } 3687 3688 static int igb_disable_sriov(struct pci_dev *pdev, bool reinit) 3689 { 3690 struct net_device *netdev = pci_get_drvdata(pdev); 3691 struct igb_adapter *adapter = netdev_priv(netdev); 3692 struct e1000_hw *hw = &adapter->hw; 3693 unsigned long flags; 3694 3695 /* reclaim resources allocated to VFs */ 3696 if (adapter->vf_data) { 3697 /* disable iov and allow time for transactions to clear */ 3698 if (pci_vfs_assigned(pdev)) { 3699 dev_warn(&pdev->dev, 3700 "Cannot deallocate SR-IOV virtual functions while they are assigned - VFs will not be deallocated\n"); 3701 return -EPERM; 3702 } else { 3703 pci_disable_sriov(pdev); 3704 msleep(500); 3705 } 3706 spin_lock_irqsave(&adapter->vfs_lock, flags); 3707 kfree(adapter->vf_mac_list); 3708 adapter->vf_mac_list = NULL; 3709 kfree(adapter->vf_data); 3710 adapter->vf_data = NULL; 3711 adapter->vfs_allocated_count = 0; 3712 spin_unlock_irqrestore(&adapter->vfs_lock, flags); 3713 wr32(E1000_IOVCTL, E1000_IOVCTL_REUSE_VFQ); 3714 wrfl(); 3715 msleep(100); 3716 dev_info(&pdev->dev, "IOV Disabled\n"); 3717 3718 /* Re-enable DMA Coalescing flag since IOV is turned off */ 3719 adapter->flags |= IGB_FLAG_DMAC; 3720 } 3721 3722 return reinit ? igb_sriov_reinit(pdev) : 0; 3723 } 3724 3725 static int igb_enable_sriov(struct pci_dev *pdev, int num_vfs, bool reinit) 3726 { 3727 struct net_device *netdev = pci_get_drvdata(pdev); 3728 struct igb_adapter *adapter = netdev_priv(netdev); 3729 int old_vfs = pci_num_vf(pdev); 3730 struct vf_mac_filter *mac_list; 3731 int err = 0; 3732 int num_vf_mac_filters, i; 3733 3734 if (!(adapter->flags & IGB_FLAG_HAS_MSIX) || num_vfs > 7) { 3735 err = -EPERM; 3736 goto out; 3737 } 3738 if (!num_vfs) 3739 goto out; 3740 3741 if (old_vfs) { 3742 dev_info(&pdev->dev, "%d pre-allocated VFs found - override max_vfs setting of %d\n", 3743 old_vfs, max_vfs); 3744 adapter->vfs_allocated_count = old_vfs; 3745 } else 3746 adapter->vfs_allocated_count = num_vfs; 3747 3748 adapter->vf_data = kcalloc(adapter->vfs_allocated_count, 3749 sizeof(struct vf_data_storage), GFP_KERNEL); 3750 3751 /* if allocation failed then we do not support SR-IOV */ 3752 if (!adapter->vf_data) { 3753 adapter->vfs_allocated_count = 0; 3754 err = -ENOMEM; 3755 goto out; 3756 } 3757 3758 /* Due to the limited number of RAR entries calculate potential 3759 * number of MAC filters available for the VFs. Reserve entries 3760 * for PF default MAC, PF MAC filters and at least one RAR entry 3761 * for each VF for VF MAC. 3762 */ 3763 num_vf_mac_filters = adapter->hw.mac.rar_entry_count - 3764 (1 + IGB_PF_MAC_FILTERS_RESERVED + 3765 adapter->vfs_allocated_count); 3766 3767 adapter->vf_mac_list = kcalloc(num_vf_mac_filters, 3768 sizeof(struct vf_mac_filter), 3769 GFP_KERNEL); 3770 3771 mac_list = adapter->vf_mac_list; 3772 INIT_LIST_HEAD(&adapter->vf_macs.l); 3773 3774 if (adapter->vf_mac_list) { 3775 /* Initialize list of VF MAC filters */ 3776 for (i = 0; i < num_vf_mac_filters; i++) { 3777 mac_list->vf = -1; 3778 mac_list->free = true; 3779 list_add(&mac_list->l, &adapter->vf_macs.l); 3780 mac_list++; 3781 } 3782 } else { 3783 /* If we could not allocate memory for the VF MAC filters 3784 * we can continue without this feature but warn user. 3785 */ 3786 dev_err(&pdev->dev, 3787 "Unable to allocate memory for VF MAC filter list\n"); 3788 } 3789 3790 dev_info(&pdev->dev, "%d VFs allocated\n", 3791 adapter->vfs_allocated_count); 3792 for (i = 0; i < adapter->vfs_allocated_count; i++) 3793 igb_vf_configure(adapter, i); 3794 3795 /* DMA Coalescing is not supported in IOV mode. */ 3796 adapter->flags &= ~IGB_FLAG_DMAC; 3797 3798 if (reinit) { 3799 err = igb_sriov_reinit(pdev); 3800 if (err) 3801 goto err_out; 3802 } 3803 3804 /* only call pci_enable_sriov() if no VFs are allocated already */ 3805 if (!old_vfs) { 3806 err = pci_enable_sriov(pdev, adapter->vfs_allocated_count); 3807 if (err) 3808 goto err_out; 3809 } 3810 3811 goto out; 3812 3813 err_out: 3814 kfree(adapter->vf_mac_list); 3815 adapter->vf_mac_list = NULL; 3816 kfree(adapter->vf_data); 3817 adapter->vf_data = NULL; 3818 adapter->vfs_allocated_count = 0; 3819 out: 3820 return err; 3821 } 3822 3823 #endif 3824 /** 3825 * igb_remove_i2c - Cleanup I2C interface 3826 * @adapter: pointer to adapter structure 3827 **/ 3828 static void igb_remove_i2c(struct igb_adapter *adapter) 3829 { 3830 /* free the adapter bus structure */ 3831 i2c_del_adapter(&adapter->i2c_adap); 3832 } 3833 3834 /** 3835 * igb_remove - Device Removal Routine 3836 * @pdev: PCI device information struct 3837 * 3838 * igb_remove is called by the PCI subsystem to alert the driver 3839 * that it should release a PCI device. The could be caused by a 3840 * Hot-Plug event, or because the driver is going to be removed from 3841 * memory. 3842 **/ 3843 static void igb_remove(struct pci_dev *pdev) 3844 { 3845 struct net_device *netdev = pci_get_drvdata(pdev); 3846 struct igb_adapter *adapter = netdev_priv(netdev); 3847 struct e1000_hw *hw = &adapter->hw; 3848 3849 pm_runtime_get_noresume(&pdev->dev); 3850 #ifdef CONFIG_IGB_HWMON 3851 igb_sysfs_exit(adapter); 3852 #endif 3853 igb_remove_i2c(adapter); 3854 igb_ptp_stop(adapter); 3855 /* The watchdog timer may be rescheduled, so explicitly 3856 * disable watchdog from being rescheduled. 3857 */ 3858 set_bit(__IGB_DOWN, &adapter->state); 3859 del_timer_sync(&adapter->watchdog_timer); 3860 del_timer_sync(&adapter->phy_info_timer); 3861 3862 cancel_work_sync(&adapter->reset_task); 3863 cancel_work_sync(&adapter->watchdog_task); 3864 3865 #ifdef CONFIG_IGB_DCA 3866 if (adapter->flags & IGB_FLAG_DCA_ENABLED) { 3867 dev_info(&pdev->dev, "DCA disabled\n"); 3868 dca_remove_requester(&pdev->dev); 3869 adapter->flags &= ~IGB_FLAG_DCA_ENABLED; 3870 wr32(E1000_DCA_CTRL, E1000_DCA_CTRL_DCA_MODE_DISABLE); 3871 } 3872 #endif 3873 3874 /* Release control of h/w to f/w. If f/w is AMT enabled, this 3875 * would have already happened in close and is redundant. 3876 */ 3877 igb_release_hw_control(adapter); 3878 3879 #ifdef CONFIG_PCI_IOV 3880 igb_disable_sriov(pdev, false); 3881 #endif 3882 3883 unregister_netdev(netdev); 3884 3885 igb_clear_interrupt_scheme(adapter); 3886 3887 pci_iounmap(pdev, adapter->io_addr); 3888 if (hw->flash_address) 3889 iounmap(hw->flash_address); 3890 pci_release_mem_regions(pdev); 3891 3892 kfree(adapter->mac_table); 3893 kfree(adapter->shadow_vfta); 3894 free_netdev(netdev); 3895 3896 pci_disable_device(pdev); 3897 } 3898 3899 /** 3900 * igb_probe_vfs - Initialize vf data storage and add VFs to pci config space 3901 * @adapter: board private structure to initialize 3902 * 3903 * This function initializes the vf specific data storage and then attempts to 3904 * allocate the VFs. The reason for ordering it this way is because it is much 3905 * mor expensive time wise to disable SR-IOV than it is to allocate and free 3906 * the memory for the VFs. 3907 **/ 3908 static void igb_probe_vfs(struct igb_adapter *adapter) 3909 { 3910 #ifdef CONFIG_PCI_IOV 3911 struct pci_dev *pdev = adapter->pdev; 3912 struct e1000_hw *hw = &adapter->hw; 3913 3914 /* Virtualization features not supported on i210 and 82580 family. */ 3915 if ((hw->mac.type == e1000_i210) || (hw->mac.type == e1000_i211) || 3916 (hw->mac.type == e1000_82580)) 3917 return; 3918 3919 /* Of the below we really only want the effect of getting 3920 * IGB_FLAG_HAS_MSIX set (if available), without which 3921 * igb_enable_sriov() has no effect. 3922 */ 3923 igb_set_interrupt_capability(adapter, true); 3924 igb_reset_interrupt_capability(adapter); 3925 3926 pci_sriov_set_totalvfs(pdev, 7); 3927 igb_enable_sriov(pdev, max_vfs, false); 3928 3929 #endif /* CONFIG_PCI_IOV */ 3930 } 3931 3932 unsigned int igb_get_max_rss_queues(struct igb_adapter *adapter) 3933 { 3934 struct e1000_hw *hw = &adapter->hw; 3935 unsigned int max_rss_queues; 3936 3937 /* Determine the maximum number of RSS queues supported. */ 3938 switch (hw->mac.type) { 3939 case e1000_i211: 3940 max_rss_queues = IGB_MAX_RX_QUEUES_I211; 3941 break; 3942 case e1000_82575: 3943 case e1000_i210: 3944 max_rss_queues = IGB_MAX_RX_QUEUES_82575; 3945 break; 3946 case e1000_i350: 3947 /* I350 cannot do RSS and SR-IOV at the same time */ 3948 if (!!adapter->vfs_allocated_count) { 3949 max_rss_queues = 1; 3950 break; 3951 } 3952 fallthrough; 3953 case e1000_82576: 3954 if (!!adapter->vfs_allocated_count) { 3955 max_rss_queues = 2; 3956 break; 3957 } 3958 fallthrough; 3959 case e1000_82580: 3960 case e1000_i354: 3961 default: 3962 max_rss_queues = IGB_MAX_RX_QUEUES; 3963 break; 3964 } 3965 3966 return max_rss_queues; 3967 } 3968 3969 static void igb_init_queue_configuration(struct igb_adapter *adapter) 3970 { 3971 u32 max_rss_queues; 3972 3973 max_rss_queues = igb_get_max_rss_queues(adapter); 3974 adapter->rss_queues = min_t(u32, max_rss_queues, num_online_cpus()); 3975 3976 igb_set_flag_queue_pairs(adapter, max_rss_queues); 3977 } 3978 3979 void igb_set_flag_queue_pairs(struct igb_adapter *adapter, 3980 const u32 max_rss_queues) 3981 { 3982 struct e1000_hw *hw = &adapter->hw; 3983 3984 /* Determine if we need to pair queues. */ 3985 switch (hw->mac.type) { 3986 case e1000_82575: 3987 case e1000_i211: 3988 /* Device supports enough interrupts without queue pairing. */ 3989 break; 3990 case e1000_82576: 3991 case e1000_82580: 3992 case e1000_i350: 3993 case e1000_i354: 3994 case e1000_i210: 3995 default: 3996 /* If rss_queues > half of max_rss_queues, pair the queues in 3997 * order to conserve interrupts due to limited supply. 3998 */ 3999 if (adapter->rss_queues > (max_rss_queues / 2)) 4000 adapter->flags |= IGB_FLAG_QUEUE_PAIRS; 4001 else 4002 adapter->flags &= ~IGB_FLAG_QUEUE_PAIRS; 4003 break; 4004 } 4005 } 4006 4007 /** 4008 * igb_sw_init - Initialize general software structures (struct igb_adapter) 4009 * @adapter: board private structure to initialize 4010 * 4011 * igb_sw_init initializes the Adapter private data structure. 4012 * Fields are initialized based on PCI device information and 4013 * OS network device settings (MTU size). 4014 **/ 4015 static int igb_sw_init(struct igb_adapter *adapter) 4016 { 4017 struct e1000_hw *hw = &adapter->hw; 4018 struct net_device *netdev = adapter->netdev; 4019 struct pci_dev *pdev = adapter->pdev; 4020 4021 pci_read_config_word(pdev, PCI_COMMAND, &hw->bus.pci_cmd_word); 4022 4023 /* set default ring sizes */ 4024 adapter->tx_ring_count = IGB_DEFAULT_TXD; 4025 adapter->rx_ring_count = IGB_DEFAULT_RXD; 4026 4027 /* set default ITR values */ 4028 adapter->rx_itr_setting = IGB_DEFAULT_ITR; 4029 adapter->tx_itr_setting = IGB_DEFAULT_ITR; 4030 4031 /* set default work limits */ 4032 adapter->tx_work_limit = IGB_DEFAULT_TX_WORK; 4033 4034 adapter->max_frame_size = netdev->mtu + IGB_ETH_PKT_HDR_PAD; 4035 adapter->min_frame_size = ETH_ZLEN + ETH_FCS_LEN; 4036 4037 spin_lock_init(&adapter->nfc_lock); 4038 spin_lock_init(&adapter->stats64_lock); 4039 4040 /* init spinlock to avoid concurrency of VF resources */ 4041 spin_lock_init(&adapter->vfs_lock); 4042 #ifdef CONFIG_PCI_IOV 4043 switch (hw->mac.type) { 4044 case e1000_82576: 4045 case e1000_i350: 4046 if (max_vfs > 7) { 4047 dev_warn(&pdev->dev, 4048 "Maximum of 7 VFs per PF, using max\n"); 4049 max_vfs = adapter->vfs_allocated_count = 7; 4050 } else 4051 adapter->vfs_allocated_count = max_vfs; 4052 if (adapter->vfs_allocated_count) 4053 dev_warn(&pdev->dev, 4054 "Enabling SR-IOV VFs using the module parameter is deprecated - please use the pci sysfs interface.\n"); 4055 break; 4056 default: 4057 break; 4058 } 4059 #endif /* CONFIG_PCI_IOV */ 4060 4061 /* Assume MSI-X interrupts, will be checked during IRQ allocation */ 4062 adapter->flags |= IGB_FLAG_HAS_MSIX; 4063 4064 adapter->mac_table = kcalloc(hw->mac.rar_entry_count, 4065 sizeof(struct igb_mac_addr), 4066 GFP_KERNEL); 4067 if (!adapter->mac_table) 4068 return -ENOMEM; 4069 4070 igb_probe_vfs(adapter); 4071 4072 igb_init_queue_configuration(adapter); 4073 4074 /* Setup and initialize a copy of the hw vlan table array */ 4075 adapter->shadow_vfta = kcalloc(E1000_VLAN_FILTER_TBL_SIZE, sizeof(u32), 4076 GFP_KERNEL); 4077 if (!adapter->shadow_vfta) 4078 return -ENOMEM; 4079 4080 /* This call may decrease the number of queues */ 4081 if (igb_init_interrupt_scheme(adapter, true)) { 4082 dev_err(&pdev->dev, "Unable to allocate memory for queues\n"); 4083 return -ENOMEM; 4084 } 4085 4086 /* Explicitly disable IRQ since the NIC can be in any state. */ 4087 igb_irq_disable(adapter); 4088 4089 if (hw->mac.type >= e1000_i350) 4090 adapter->flags &= ~IGB_FLAG_DMAC; 4091 4092 set_bit(__IGB_DOWN, &adapter->state); 4093 return 0; 4094 } 4095 4096 /** 4097 * __igb_open - Called when a network interface is made active 4098 * @netdev: network interface device structure 4099 * @resuming: indicates whether we are in a resume call 4100 * 4101 * Returns 0 on success, negative value on failure 4102 * 4103 * The open entry point is called when a network interface is made 4104 * active by the system (IFF_UP). At this point all resources needed 4105 * for transmit and receive operations are allocated, the interrupt 4106 * handler is registered with the OS, the watchdog timer is started, 4107 * and the stack is notified that the interface is ready. 4108 **/ 4109 static int __igb_open(struct net_device *netdev, bool resuming) 4110 { 4111 struct igb_adapter *adapter = netdev_priv(netdev); 4112 struct e1000_hw *hw = &adapter->hw; 4113 struct pci_dev *pdev = adapter->pdev; 4114 int err; 4115 int i; 4116 4117 /* disallow open during test */ 4118 if (test_bit(__IGB_TESTING, &adapter->state)) { 4119 WARN_ON(resuming); 4120 return -EBUSY; 4121 } 4122 4123 if (!resuming) 4124 pm_runtime_get_sync(&pdev->dev); 4125 4126 netif_carrier_off(netdev); 4127 4128 /* allocate transmit descriptors */ 4129 err = igb_setup_all_tx_resources(adapter); 4130 if (err) 4131 goto err_setup_tx; 4132 4133 /* allocate receive descriptors */ 4134 err = igb_setup_all_rx_resources(adapter); 4135 if (err) 4136 goto err_setup_rx; 4137 4138 igb_power_up_link(adapter); 4139 4140 /* before we allocate an interrupt, we must be ready to handle it. 4141 * Setting DEBUG_SHIRQ in the kernel makes it fire an interrupt 4142 * as soon as we call pci_request_irq, so we have to setup our 4143 * clean_rx handler before we do so. 4144 */ 4145 igb_configure(adapter); 4146 4147 err = igb_request_irq(adapter); 4148 if (err) 4149 goto err_req_irq; 4150 4151 /* Notify the stack of the actual queue counts. */ 4152 err = netif_set_real_num_tx_queues(adapter->netdev, 4153 adapter->num_tx_queues); 4154 if (err) 4155 goto err_set_queues; 4156 4157 err = netif_set_real_num_rx_queues(adapter->netdev, 4158 adapter->num_rx_queues); 4159 if (err) 4160 goto err_set_queues; 4161 4162 /* From here on the code is the same as igb_up() */ 4163 clear_bit(__IGB_DOWN, &adapter->state); 4164 4165 for (i = 0; i < adapter->num_q_vectors; i++) 4166 napi_enable(&(adapter->q_vector[i]->napi)); 4167 4168 /* Clear any pending interrupts. */ 4169 rd32(E1000_TSICR); 4170 rd32(E1000_ICR); 4171 4172 igb_irq_enable(adapter); 4173 4174 /* notify VFs that reset has been completed */ 4175 if (adapter->vfs_allocated_count) { 4176 u32 reg_data = rd32(E1000_CTRL_EXT); 4177 4178 reg_data |= E1000_CTRL_EXT_PFRSTD; 4179 wr32(E1000_CTRL_EXT, reg_data); 4180 } 4181 4182 netif_tx_start_all_queues(netdev); 4183 4184 if (!resuming) 4185 pm_runtime_put(&pdev->dev); 4186 4187 /* start the watchdog. */ 4188 hw->mac.get_link_status = 1; 4189 schedule_work(&adapter->watchdog_task); 4190 4191 return 0; 4192 4193 err_set_queues: 4194 igb_free_irq(adapter); 4195 err_req_irq: 4196 igb_release_hw_control(adapter); 4197 igb_power_down_link(adapter); 4198 igb_free_all_rx_resources(adapter); 4199 err_setup_rx: 4200 igb_free_all_tx_resources(adapter); 4201 err_setup_tx: 4202 igb_reset(adapter); 4203 if (!resuming) 4204 pm_runtime_put(&pdev->dev); 4205 4206 return err; 4207 } 4208 4209 int igb_open(struct net_device *netdev) 4210 { 4211 return __igb_open(netdev, false); 4212 } 4213 4214 /** 4215 * __igb_close - Disables a network interface 4216 * @netdev: network interface device structure 4217 * @suspending: indicates we are in a suspend call 4218 * 4219 * Returns 0, this is not allowed to fail 4220 * 4221 * The close entry point is called when an interface is de-activated 4222 * by the OS. The hardware is still under the driver's control, but 4223 * needs to be disabled. A global MAC reset is issued to stop the 4224 * hardware, and all transmit and receive resources are freed. 4225 **/ 4226 static int __igb_close(struct net_device *netdev, bool suspending) 4227 { 4228 struct igb_adapter *adapter = netdev_priv(netdev); 4229 struct pci_dev *pdev = adapter->pdev; 4230 4231 WARN_ON(test_bit(__IGB_RESETTING, &adapter->state)); 4232 4233 if (!suspending) 4234 pm_runtime_get_sync(&pdev->dev); 4235 4236 igb_down(adapter); 4237 igb_free_irq(adapter); 4238 4239 igb_free_all_tx_resources(adapter); 4240 igb_free_all_rx_resources(adapter); 4241 4242 if (!suspending) 4243 pm_runtime_put_sync(&pdev->dev); 4244 return 0; 4245 } 4246 4247 int igb_close(struct net_device *netdev) 4248 { 4249 if (netif_device_present(netdev) || netdev->dismantle) 4250 return __igb_close(netdev, false); 4251 return 0; 4252 } 4253 4254 /** 4255 * igb_setup_tx_resources - allocate Tx resources (Descriptors) 4256 * @tx_ring: tx descriptor ring (for a specific queue) to setup 4257 * 4258 * Return 0 on success, negative on failure 4259 **/ 4260 int igb_setup_tx_resources(struct igb_ring *tx_ring) 4261 { 4262 struct device *dev = tx_ring->dev; 4263 int size; 4264 4265 size = sizeof(struct igb_tx_buffer) * tx_ring->count; 4266 4267 tx_ring->tx_buffer_info = vmalloc(size); 4268 if (!tx_ring->tx_buffer_info) 4269 goto err; 4270 4271 /* round up to nearest 4K */ 4272 tx_ring->size = tx_ring->count * sizeof(union e1000_adv_tx_desc); 4273 tx_ring->size = ALIGN(tx_ring->size, 4096); 4274 4275 tx_ring->desc = dma_alloc_coherent(dev, tx_ring->size, 4276 &tx_ring->dma, GFP_KERNEL); 4277 if (!tx_ring->desc) 4278 goto err; 4279 4280 tx_ring->next_to_use = 0; 4281 tx_ring->next_to_clean = 0; 4282 4283 return 0; 4284 4285 err: 4286 vfree(tx_ring->tx_buffer_info); 4287 tx_ring->tx_buffer_info = NULL; 4288 dev_err(dev, "Unable to allocate memory for the Tx descriptor ring\n"); 4289 return -ENOMEM; 4290 } 4291 4292 /** 4293 * igb_setup_all_tx_resources - wrapper to allocate Tx resources 4294 * (Descriptors) for all queues 4295 * @adapter: board private structure 4296 * 4297 * Return 0 on success, negative on failure 4298 **/ 4299 static int igb_setup_all_tx_resources(struct igb_adapter *adapter) 4300 { 4301 struct pci_dev *pdev = adapter->pdev; 4302 int i, err = 0; 4303 4304 for (i = 0; i < adapter->num_tx_queues; i++) { 4305 err = igb_setup_tx_resources(adapter->tx_ring[i]); 4306 if (err) { 4307 dev_err(&pdev->dev, 4308 "Allocation for Tx Queue %u failed\n", i); 4309 for (i--; i >= 0; i--) 4310 igb_free_tx_resources(adapter->tx_ring[i]); 4311 break; 4312 } 4313 } 4314 4315 return err; 4316 } 4317 4318 /** 4319 * igb_setup_tctl - configure the transmit control registers 4320 * @adapter: Board private structure 4321 **/ 4322 void igb_setup_tctl(struct igb_adapter *adapter) 4323 { 4324 struct e1000_hw *hw = &adapter->hw; 4325 u32 tctl; 4326 4327 /* disable queue 0 which is enabled by default on 82575 and 82576 */ 4328 wr32(E1000_TXDCTL(0), 0); 4329 4330 /* Program the Transmit Control Register */ 4331 tctl = rd32(E1000_TCTL); 4332 tctl &= ~E1000_TCTL_CT; 4333 tctl |= E1000_TCTL_PSP | E1000_TCTL_RTLC | 4334 (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT); 4335 4336 igb_config_collision_dist(hw); 4337 4338 /* Enable transmits */ 4339 tctl |= E1000_TCTL_EN; 4340 4341 wr32(E1000_TCTL, tctl); 4342 } 4343 4344 /** 4345 * igb_configure_tx_ring - Configure transmit ring after Reset 4346 * @adapter: board private structure 4347 * @ring: tx ring to configure 4348 * 4349 * Configure a transmit ring after a reset. 4350 **/ 4351 void igb_configure_tx_ring(struct igb_adapter *adapter, 4352 struct igb_ring *ring) 4353 { 4354 struct e1000_hw *hw = &adapter->hw; 4355 u32 txdctl = 0; 4356 u64 tdba = ring->dma; 4357 int reg_idx = ring->reg_idx; 4358 4359 wr32(E1000_TDLEN(reg_idx), 4360 ring->count * sizeof(union e1000_adv_tx_desc)); 4361 wr32(E1000_TDBAL(reg_idx), 4362 tdba & 0x00000000ffffffffULL); 4363 wr32(E1000_TDBAH(reg_idx), tdba >> 32); 4364 4365 ring->tail = adapter->io_addr + E1000_TDT(reg_idx); 4366 wr32(E1000_TDH(reg_idx), 0); 4367 writel(0, ring->tail); 4368 4369 txdctl |= IGB_TX_PTHRESH; 4370 txdctl |= IGB_TX_HTHRESH << 8; 4371 txdctl |= IGB_TX_WTHRESH << 16; 4372 4373 /* reinitialize tx_buffer_info */ 4374 memset(ring->tx_buffer_info, 0, 4375 sizeof(struct igb_tx_buffer) * ring->count); 4376 4377 txdctl |= E1000_TXDCTL_QUEUE_ENABLE; 4378 wr32(E1000_TXDCTL(reg_idx), txdctl); 4379 } 4380 4381 /** 4382 * igb_configure_tx - Configure transmit Unit after Reset 4383 * @adapter: board private structure 4384 * 4385 * Configure the Tx unit of the MAC after a reset. 4386 **/ 4387 static void igb_configure_tx(struct igb_adapter *adapter) 4388 { 4389 struct e1000_hw *hw = &adapter->hw; 4390 int i; 4391 4392 /* disable the queues */ 4393 for (i = 0; i < adapter->num_tx_queues; i++) 4394 wr32(E1000_TXDCTL(adapter->tx_ring[i]->reg_idx), 0); 4395 4396 wrfl(); 4397 usleep_range(10000, 20000); 4398 4399 for (i = 0; i < adapter->num_tx_queues; i++) 4400 igb_configure_tx_ring(adapter, adapter->tx_ring[i]); 4401 } 4402 4403 /** 4404 * igb_setup_rx_resources - allocate Rx resources (Descriptors) 4405 * @rx_ring: Rx descriptor ring (for a specific queue) to setup 4406 * 4407 * Returns 0 on success, negative on failure 4408 **/ 4409 int igb_setup_rx_resources(struct igb_ring *rx_ring) 4410 { 4411 struct igb_adapter *adapter = netdev_priv(rx_ring->netdev); 4412 struct device *dev = rx_ring->dev; 4413 int size, res; 4414 4415 /* XDP RX-queue info */ 4416 if (xdp_rxq_info_is_reg(&rx_ring->xdp_rxq)) 4417 xdp_rxq_info_unreg(&rx_ring->xdp_rxq); 4418 res = xdp_rxq_info_reg(&rx_ring->xdp_rxq, rx_ring->netdev, 4419 rx_ring->queue_index, 0); 4420 if (res < 0) { 4421 dev_err(dev, "Failed to register xdp_rxq index %u\n", 4422 rx_ring->queue_index); 4423 return res; 4424 } 4425 4426 size = sizeof(struct igb_rx_buffer) * rx_ring->count; 4427 4428 rx_ring->rx_buffer_info = vmalloc(size); 4429 if (!rx_ring->rx_buffer_info) 4430 goto err; 4431 4432 /* Round up to nearest 4K */ 4433 rx_ring->size = rx_ring->count * sizeof(union e1000_adv_rx_desc); 4434 rx_ring->size = ALIGN(rx_ring->size, 4096); 4435 4436 rx_ring->desc = dma_alloc_coherent(dev, rx_ring->size, 4437 &rx_ring->dma, GFP_KERNEL); 4438 if (!rx_ring->desc) 4439 goto err; 4440 4441 rx_ring->next_to_alloc = 0; 4442 rx_ring->next_to_clean = 0; 4443 rx_ring->next_to_use = 0; 4444 4445 rx_ring->xdp_prog = adapter->xdp_prog; 4446 4447 return 0; 4448 4449 err: 4450 xdp_rxq_info_unreg(&rx_ring->xdp_rxq); 4451 vfree(rx_ring->rx_buffer_info); 4452 rx_ring->rx_buffer_info = NULL; 4453 dev_err(dev, "Unable to allocate memory for the Rx descriptor ring\n"); 4454 return -ENOMEM; 4455 } 4456 4457 /** 4458 * igb_setup_all_rx_resources - wrapper to allocate Rx resources 4459 * (Descriptors) for all queues 4460 * @adapter: board private structure 4461 * 4462 * Return 0 on success, negative on failure 4463 **/ 4464 static int igb_setup_all_rx_resources(struct igb_adapter *adapter) 4465 { 4466 struct pci_dev *pdev = adapter->pdev; 4467 int i, err = 0; 4468 4469 for (i = 0; i < adapter->num_rx_queues; i++) { 4470 err = igb_setup_rx_resources(adapter->rx_ring[i]); 4471 if (err) { 4472 dev_err(&pdev->dev, 4473 "Allocation for Rx Queue %u failed\n", i); 4474 for (i--; i >= 0; i--) 4475 igb_free_rx_resources(adapter->rx_ring[i]); 4476 break; 4477 } 4478 } 4479 4480 return err; 4481 } 4482 4483 /** 4484 * igb_setup_mrqc - configure the multiple receive queue control registers 4485 * @adapter: Board private structure 4486 **/ 4487 static void igb_setup_mrqc(struct igb_adapter *adapter) 4488 { 4489 struct e1000_hw *hw = &adapter->hw; 4490 u32 mrqc, rxcsum; 4491 u32 j, num_rx_queues; 4492 u32 rss_key[10]; 4493 4494 netdev_rss_key_fill(rss_key, sizeof(rss_key)); 4495 for (j = 0; j < 10; j++) 4496 wr32(E1000_RSSRK(j), rss_key[j]); 4497 4498 num_rx_queues = adapter->rss_queues; 4499 4500 switch (hw->mac.type) { 4501 case e1000_82576: 4502 /* 82576 supports 2 RSS queues for SR-IOV */ 4503 if (adapter->vfs_allocated_count) 4504 num_rx_queues = 2; 4505 break; 4506 default: 4507 break; 4508 } 4509 4510 if (adapter->rss_indir_tbl_init != num_rx_queues) { 4511 for (j = 0; j < IGB_RETA_SIZE; j++) 4512 adapter->rss_indir_tbl[j] = 4513 (j * num_rx_queues) / IGB_RETA_SIZE; 4514 adapter->rss_indir_tbl_init = num_rx_queues; 4515 } 4516 igb_write_rss_indir_tbl(adapter); 4517 4518 /* Disable raw packet checksumming so that RSS hash is placed in 4519 * descriptor on writeback. No need to enable TCP/UDP/IP checksum 4520 * offloads as they are enabled by default 4521 */ 4522 rxcsum = rd32(E1000_RXCSUM); 4523 rxcsum |= E1000_RXCSUM_PCSD; 4524 4525 if (adapter->hw.mac.type >= e1000_82576) 4526 /* Enable Receive Checksum Offload for SCTP */ 4527 rxcsum |= E1000_RXCSUM_CRCOFL; 4528 4529 /* Don't need to set TUOFL or IPOFL, they default to 1 */ 4530 wr32(E1000_RXCSUM, rxcsum); 4531 4532 /* Generate RSS hash based on packet types, TCP/UDP 4533 * port numbers and/or IPv4/v6 src and dst addresses 4534 */ 4535 mrqc = E1000_MRQC_RSS_FIELD_IPV4 | 4536 E1000_MRQC_RSS_FIELD_IPV4_TCP | 4537 E1000_MRQC_RSS_FIELD_IPV6 | 4538 E1000_MRQC_RSS_FIELD_IPV6_TCP | 4539 E1000_MRQC_RSS_FIELD_IPV6_TCP_EX; 4540 4541 if (adapter->flags & IGB_FLAG_RSS_FIELD_IPV4_UDP) 4542 mrqc |= E1000_MRQC_RSS_FIELD_IPV4_UDP; 4543 if (adapter->flags & IGB_FLAG_RSS_FIELD_IPV6_UDP) 4544 mrqc |= E1000_MRQC_RSS_FIELD_IPV6_UDP; 4545 4546 /* If VMDq is enabled then we set the appropriate mode for that, else 4547 * we default to RSS so that an RSS hash is calculated per packet even 4548 * if we are only using one queue 4549 */ 4550 if (adapter->vfs_allocated_count) { 4551 if (hw->mac.type > e1000_82575) { 4552 /* Set the default pool for the PF's first queue */ 4553 u32 vtctl = rd32(E1000_VT_CTL); 4554 4555 vtctl &= ~(E1000_VT_CTL_DEFAULT_POOL_MASK | 4556 E1000_VT_CTL_DISABLE_DEF_POOL); 4557 vtctl |= adapter->vfs_allocated_count << 4558 E1000_VT_CTL_DEFAULT_POOL_SHIFT; 4559 wr32(E1000_VT_CTL, vtctl); 4560 } 4561 if (adapter->rss_queues > 1) 4562 mrqc |= E1000_MRQC_ENABLE_VMDQ_RSS_MQ; 4563 else 4564 mrqc |= E1000_MRQC_ENABLE_VMDQ; 4565 } else { 4566 mrqc |= E1000_MRQC_ENABLE_RSS_MQ; 4567 } 4568 igb_vmm_control(adapter); 4569 4570 wr32(E1000_MRQC, mrqc); 4571 } 4572 4573 /** 4574 * igb_setup_rctl - configure the receive control registers 4575 * @adapter: Board private structure 4576 **/ 4577 void igb_setup_rctl(struct igb_adapter *adapter) 4578 { 4579 struct e1000_hw *hw = &adapter->hw; 4580 u32 rctl; 4581 4582 rctl = rd32(E1000_RCTL); 4583 4584 rctl &= ~(3 << E1000_RCTL_MO_SHIFT); 4585 rctl &= ~(E1000_RCTL_LBM_TCVR | E1000_RCTL_LBM_MAC); 4586 4587 rctl |= E1000_RCTL_EN | E1000_RCTL_BAM | E1000_RCTL_RDMTS_HALF | 4588 (hw->mac.mc_filter_type << E1000_RCTL_MO_SHIFT); 4589 4590 /* enable stripping of CRC. It's unlikely this will break BMC 4591 * redirection as it did with e1000. Newer features require 4592 * that the HW strips the CRC. 4593 */ 4594 rctl |= E1000_RCTL_SECRC; 4595 4596 /* disable store bad packets and clear size bits. */ 4597 rctl &= ~(E1000_RCTL_SBP | E1000_RCTL_SZ_256); 4598 4599 /* enable LPE to allow for reception of jumbo frames */ 4600 rctl |= E1000_RCTL_LPE; 4601 4602 /* disable queue 0 to prevent tail write w/o re-config */ 4603 wr32(E1000_RXDCTL(0), 0); 4604 4605 /* Attention!!! For SR-IOV PF driver operations you must enable 4606 * queue drop for all VF and PF queues to prevent head of line blocking 4607 * if an un-trusted VF does not provide descriptors to hardware. 4608 */ 4609 if (adapter->vfs_allocated_count) { 4610 /* set all queue drop enable bits */ 4611 wr32(E1000_QDE, ALL_QUEUES); 4612 } 4613 4614 /* This is useful for sniffing bad packets. */ 4615 if (adapter->netdev->features & NETIF_F_RXALL) { 4616 /* UPE and MPE will be handled by normal PROMISC logic 4617 * in e1000e_set_rx_mode 4618 */ 4619 rctl |= (E1000_RCTL_SBP | /* Receive bad packets */ 4620 E1000_RCTL_BAM | /* RX All Bcast Pkts */ 4621 E1000_RCTL_PMCF); /* RX All MAC Ctrl Pkts */ 4622 4623 rctl &= ~(E1000_RCTL_DPF | /* Allow filtered pause */ 4624 E1000_RCTL_CFIEN); /* Dis VLAN CFIEN Filter */ 4625 /* Do not mess with E1000_CTRL_VME, it affects transmit as well, 4626 * and that breaks VLANs. 4627 */ 4628 } 4629 4630 wr32(E1000_RCTL, rctl); 4631 } 4632 4633 static inline int igb_set_vf_rlpml(struct igb_adapter *adapter, int size, 4634 int vfn) 4635 { 4636 struct e1000_hw *hw = &adapter->hw; 4637 u32 vmolr; 4638 4639 if (size > MAX_JUMBO_FRAME_SIZE) 4640 size = MAX_JUMBO_FRAME_SIZE; 4641 4642 vmolr = rd32(E1000_VMOLR(vfn)); 4643 vmolr &= ~E1000_VMOLR_RLPML_MASK; 4644 vmolr |= size | E1000_VMOLR_LPE; 4645 wr32(E1000_VMOLR(vfn), vmolr); 4646 4647 return 0; 4648 } 4649 4650 static inline void igb_set_vf_vlan_strip(struct igb_adapter *adapter, 4651 int vfn, bool enable) 4652 { 4653 struct e1000_hw *hw = &adapter->hw; 4654 u32 val, reg; 4655 4656 if (hw->mac.type < e1000_82576) 4657 return; 4658 4659 if (hw->mac.type == e1000_i350) 4660 reg = E1000_DVMOLR(vfn); 4661 else 4662 reg = E1000_VMOLR(vfn); 4663 4664 val = rd32(reg); 4665 if (enable) 4666 val |= E1000_VMOLR_STRVLAN; 4667 else 4668 val &= ~(E1000_VMOLR_STRVLAN); 4669 wr32(reg, val); 4670 } 4671 4672 static inline void igb_set_vmolr(struct igb_adapter *adapter, 4673 int vfn, bool aupe) 4674 { 4675 struct e1000_hw *hw = &adapter->hw; 4676 u32 vmolr; 4677 4678 /* This register exists only on 82576 and newer so if we are older then 4679 * we should exit and do nothing 4680 */ 4681 if (hw->mac.type < e1000_82576) 4682 return; 4683 4684 vmolr = rd32(E1000_VMOLR(vfn)); 4685 if (aupe) 4686 vmolr |= E1000_VMOLR_AUPE; /* Accept untagged packets */ 4687 else 4688 vmolr &= ~(E1000_VMOLR_AUPE); /* Tagged packets ONLY */ 4689 4690 /* clear all bits that might not be set */ 4691 vmolr &= ~(E1000_VMOLR_BAM | E1000_VMOLR_RSSE); 4692 4693 if (adapter->rss_queues > 1 && vfn == adapter->vfs_allocated_count) 4694 vmolr |= E1000_VMOLR_RSSE; /* enable RSS */ 4695 /* for VMDq only allow the VFs and pool 0 to accept broadcast and 4696 * multicast packets 4697 */ 4698 if (vfn <= adapter->vfs_allocated_count) 4699 vmolr |= E1000_VMOLR_BAM; /* Accept broadcast */ 4700 4701 wr32(E1000_VMOLR(vfn), vmolr); 4702 } 4703 4704 /** 4705 * igb_setup_srrctl - configure the split and replication receive control 4706 * registers 4707 * @adapter: Board private structure 4708 * @ring: receive ring to be configured 4709 **/ 4710 void igb_setup_srrctl(struct igb_adapter *adapter, struct igb_ring *ring) 4711 { 4712 struct e1000_hw *hw = &adapter->hw; 4713 int reg_idx = ring->reg_idx; 4714 u32 srrctl = 0; 4715 4716 srrctl = IGB_RX_HDR_LEN << E1000_SRRCTL_BSIZEHDRSIZE_SHIFT; 4717 if (ring_uses_large_buffer(ring)) 4718 srrctl |= IGB_RXBUFFER_3072 >> E1000_SRRCTL_BSIZEPKT_SHIFT; 4719 else 4720 srrctl |= IGB_RXBUFFER_2048 >> E1000_SRRCTL_BSIZEPKT_SHIFT; 4721 srrctl |= E1000_SRRCTL_DESCTYPE_ADV_ONEBUF; 4722 if (hw->mac.type >= e1000_82580) 4723 srrctl |= E1000_SRRCTL_TIMESTAMP; 4724 /* Only set Drop Enable if VFs allocated, or we are supporting multiple 4725 * queues and rx flow control is disabled 4726 */ 4727 if (adapter->vfs_allocated_count || 4728 (!(hw->fc.current_mode & e1000_fc_rx_pause) && 4729 adapter->num_rx_queues > 1)) 4730 srrctl |= E1000_SRRCTL_DROP_EN; 4731 4732 wr32(E1000_SRRCTL(reg_idx), srrctl); 4733 } 4734 4735 /** 4736 * igb_configure_rx_ring - Configure a receive ring after Reset 4737 * @adapter: board private structure 4738 * @ring: receive ring to be configured 4739 * 4740 * Configure the Rx unit of the MAC after a reset. 4741 **/ 4742 void igb_configure_rx_ring(struct igb_adapter *adapter, 4743 struct igb_ring *ring) 4744 { 4745 struct e1000_hw *hw = &adapter->hw; 4746 union e1000_adv_rx_desc *rx_desc; 4747 u64 rdba = ring->dma; 4748 int reg_idx = ring->reg_idx; 4749 u32 rxdctl = 0; 4750 4751 xdp_rxq_info_unreg_mem_model(&ring->xdp_rxq); 4752 WARN_ON(xdp_rxq_info_reg_mem_model(&ring->xdp_rxq, 4753 MEM_TYPE_PAGE_SHARED, NULL)); 4754 4755 /* disable the queue */ 4756 wr32(E1000_RXDCTL(reg_idx), 0); 4757 4758 /* Set DMA base address registers */ 4759 wr32(E1000_RDBAL(reg_idx), 4760 rdba & 0x00000000ffffffffULL); 4761 wr32(E1000_RDBAH(reg_idx), rdba >> 32); 4762 wr32(E1000_RDLEN(reg_idx), 4763 ring->count * sizeof(union e1000_adv_rx_desc)); 4764 4765 /* initialize head and tail */ 4766 ring->tail = adapter->io_addr + E1000_RDT(reg_idx); 4767 wr32(E1000_RDH(reg_idx), 0); 4768 writel(0, ring->tail); 4769 4770 /* set descriptor configuration */ 4771 igb_setup_srrctl(adapter, ring); 4772 4773 /* set filtering for VMDQ pools */ 4774 igb_set_vmolr(adapter, reg_idx & 0x7, true); 4775 4776 rxdctl |= IGB_RX_PTHRESH; 4777 rxdctl |= IGB_RX_HTHRESH << 8; 4778 rxdctl |= IGB_RX_WTHRESH << 16; 4779 4780 /* initialize rx_buffer_info */ 4781 memset(ring->rx_buffer_info, 0, 4782 sizeof(struct igb_rx_buffer) * ring->count); 4783 4784 /* initialize Rx descriptor 0 */ 4785 rx_desc = IGB_RX_DESC(ring, 0); 4786 rx_desc->wb.upper.length = 0; 4787 4788 /* enable receive descriptor fetching */ 4789 rxdctl |= E1000_RXDCTL_QUEUE_ENABLE; 4790 wr32(E1000_RXDCTL(reg_idx), rxdctl); 4791 } 4792 4793 static void igb_set_rx_buffer_len(struct igb_adapter *adapter, 4794 struct igb_ring *rx_ring) 4795 { 4796 #if (PAGE_SIZE < 8192) 4797 struct e1000_hw *hw = &adapter->hw; 4798 #endif 4799 4800 /* set build_skb and buffer size flags */ 4801 clear_ring_build_skb_enabled(rx_ring); 4802 clear_ring_uses_large_buffer(rx_ring); 4803 4804 if (adapter->flags & IGB_FLAG_RX_LEGACY) 4805 return; 4806 4807 set_ring_build_skb_enabled(rx_ring); 4808 4809 #if (PAGE_SIZE < 8192) 4810 if (adapter->max_frame_size > IGB_MAX_FRAME_BUILD_SKB || 4811 rd32(E1000_RCTL) & E1000_RCTL_SBP) 4812 set_ring_uses_large_buffer(rx_ring); 4813 #endif 4814 } 4815 4816 /** 4817 * igb_configure_rx - Configure receive Unit after Reset 4818 * @adapter: board private structure 4819 * 4820 * Configure the Rx unit of the MAC after a reset. 4821 **/ 4822 static void igb_configure_rx(struct igb_adapter *adapter) 4823 { 4824 int i; 4825 4826 /* set the correct pool for the PF default MAC address in entry 0 */ 4827 igb_set_default_mac_filter(adapter); 4828 4829 /* Setup the HW Rx Head and Tail Descriptor Pointers and 4830 * the Base and Length of the Rx Descriptor Ring 4831 */ 4832 for (i = 0; i < adapter->num_rx_queues; i++) { 4833 struct igb_ring *rx_ring = adapter->rx_ring[i]; 4834 4835 igb_set_rx_buffer_len(adapter, rx_ring); 4836 igb_configure_rx_ring(adapter, rx_ring); 4837 } 4838 } 4839 4840 /** 4841 * igb_free_tx_resources - Free Tx Resources per Queue 4842 * @tx_ring: Tx descriptor ring for a specific queue 4843 * 4844 * Free all transmit software resources 4845 **/ 4846 void igb_free_tx_resources(struct igb_ring *tx_ring) 4847 { 4848 igb_clean_tx_ring(tx_ring); 4849 4850 vfree(tx_ring->tx_buffer_info); 4851 tx_ring->tx_buffer_info = NULL; 4852 4853 /* if not set, then don't free */ 4854 if (!tx_ring->desc) 4855 return; 4856 4857 dma_free_coherent(tx_ring->dev, tx_ring->size, 4858 tx_ring->desc, tx_ring->dma); 4859 4860 tx_ring->desc = NULL; 4861 } 4862 4863 /** 4864 * igb_free_all_tx_resources - Free Tx Resources for All Queues 4865 * @adapter: board private structure 4866 * 4867 * Free all transmit software resources 4868 **/ 4869 static void igb_free_all_tx_resources(struct igb_adapter *adapter) 4870 { 4871 int i; 4872 4873 for (i = 0; i < adapter->num_tx_queues; i++) 4874 if (adapter->tx_ring[i]) 4875 igb_free_tx_resources(adapter->tx_ring[i]); 4876 } 4877 4878 /** 4879 * igb_clean_tx_ring - Free Tx Buffers 4880 * @tx_ring: ring to be cleaned 4881 **/ 4882 static void igb_clean_tx_ring(struct igb_ring *tx_ring) 4883 { 4884 u16 i = tx_ring->next_to_clean; 4885 struct igb_tx_buffer *tx_buffer = &tx_ring->tx_buffer_info[i]; 4886 4887 while (i != tx_ring->next_to_use) { 4888 union e1000_adv_tx_desc *eop_desc, *tx_desc; 4889 4890 /* Free all the Tx ring sk_buffs or xdp frames */ 4891 if (tx_buffer->type == IGB_TYPE_SKB) 4892 dev_kfree_skb_any(tx_buffer->skb); 4893 else 4894 xdp_return_frame(tx_buffer->xdpf); 4895 4896 /* unmap skb header data */ 4897 dma_unmap_single(tx_ring->dev, 4898 dma_unmap_addr(tx_buffer, dma), 4899 dma_unmap_len(tx_buffer, len), 4900 DMA_TO_DEVICE); 4901 4902 /* check for eop_desc to determine the end of the packet */ 4903 eop_desc = tx_buffer->next_to_watch; 4904 tx_desc = IGB_TX_DESC(tx_ring, i); 4905 4906 /* unmap remaining buffers */ 4907 while (tx_desc != eop_desc) { 4908 tx_buffer++; 4909 tx_desc++; 4910 i++; 4911 if (unlikely(i == tx_ring->count)) { 4912 i = 0; 4913 tx_buffer = tx_ring->tx_buffer_info; 4914 tx_desc = IGB_TX_DESC(tx_ring, 0); 4915 } 4916 4917 /* unmap any remaining paged data */ 4918 if (dma_unmap_len(tx_buffer, len)) 4919 dma_unmap_page(tx_ring->dev, 4920 dma_unmap_addr(tx_buffer, dma), 4921 dma_unmap_len(tx_buffer, len), 4922 DMA_TO_DEVICE); 4923 } 4924 4925 tx_buffer->next_to_watch = NULL; 4926 4927 /* move us one more past the eop_desc for start of next pkt */ 4928 tx_buffer++; 4929 i++; 4930 if (unlikely(i == tx_ring->count)) { 4931 i = 0; 4932 tx_buffer = tx_ring->tx_buffer_info; 4933 } 4934 } 4935 4936 /* reset BQL for queue */ 4937 netdev_tx_reset_queue(txring_txq(tx_ring)); 4938 4939 /* reset next_to_use and next_to_clean */ 4940 tx_ring->next_to_use = 0; 4941 tx_ring->next_to_clean = 0; 4942 } 4943 4944 /** 4945 * igb_clean_all_tx_rings - Free Tx Buffers for all queues 4946 * @adapter: board private structure 4947 **/ 4948 static void igb_clean_all_tx_rings(struct igb_adapter *adapter) 4949 { 4950 int i; 4951 4952 for (i = 0; i < adapter->num_tx_queues; i++) 4953 if (adapter->tx_ring[i]) 4954 igb_clean_tx_ring(adapter->tx_ring[i]); 4955 } 4956 4957 /** 4958 * igb_free_rx_resources - Free Rx Resources 4959 * @rx_ring: ring to clean the resources from 4960 * 4961 * Free all receive software resources 4962 **/ 4963 void igb_free_rx_resources(struct igb_ring *rx_ring) 4964 { 4965 igb_clean_rx_ring(rx_ring); 4966 4967 rx_ring->xdp_prog = NULL; 4968 xdp_rxq_info_unreg(&rx_ring->xdp_rxq); 4969 vfree(rx_ring->rx_buffer_info); 4970 rx_ring->rx_buffer_info = NULL; 4971 4972 /* if not set, then don't free */ 4973 if (!rx_ring->desc) 4974 return; 4975 4976 dma_free_coherent(rx_ring->dev, rx_ring->size, 4977 rx_ring->desc, rx_ring->dma); 4978 4979 rx_ring->desc = NULL; 4980 } 4981 4982 /** 4983 * igb_free_all_rx_resources - Free Rx Resources for All Queues 4984 * @adapter: board private structure 4985 * 4986 * Free all receive software resources 4987 **/ 4988 static void igb_free_all_rx_resources(struct igb_adapter *adapter) 4989 { 4990 int i; 4991 4992 for (i = 0; i < adapter->num_rx_queues; i++) 4993 if (adapter->rx_ring[i]) 4994 igb_free_rx_resources(adapter->rx_ring[i]); 4995 } 4996 4997 /** 4998 * igb_clean_rx_ring - Free Rx Buffers per Queue 4999 * @rx_ring: ring to free buffers from 5000 **/ 5001 static void igb_clean_rx_ring(struct igb_ring *rx_ring) 5002 { 5003 u16 i = rx_ring->next_to_clean; 5004 5005 dev_kfree_skb(rx_ring->skb); 5006 rx_ring->skb = NULL; 5007 5008 /* Free all the Rx ring sk_buffs */ 5009 while (i != rx_ring->next_to_alloc) { 5010 struct igb_rx_buffer *buffer_info = &rx_ring->rx_buffer_info[i]; 5011 5012 /* Invalidate cache lines that may have been written to by 5013 * device so that we avoid corrupting memory. 5014 */ 5015 dma_sync_single_range_for_cpu(rx_ring->dev, 5016 buffer_info->dma, 5017 buffer_info->page_offset, 5018 igb_rx_bufsz(rx_ring), 5019 DMA_FROM_DEVICE); 5020 5021 /* free resources associated with mapping */ 5022 dma_unmap_page_attrs(rx_ring->dev, 5023 buffer_info->dma, 5024 igb_rx_pg_size(rx_ring), 5025 DMA_FROM_DEVICE, 5026 IGB_RX_DMA_ATTR); 5027 __page_frag_cache_drain(buffer_info->page, 5028 buffer_info->pagecnt_bias); 5029 5030 i++; 5031 if (i == rx_ring->count) 5032 i = 0; 5033 } 5034 5035 rx_ring->next_to_alloc = 0; 5036 rx_ring->next_to_clean = 0; 5037 rx_ring->next_to_use = 0; 5038 } 5039 5040 /** 5041 * igb_clean_all_rx_rings - Free Rx Buffers for all queues 5042 * @adapter: board private structure 5043 **/ 5044 static void igb_clean_all_rx_rings(struct igb_adapter *adapter) 5045 { 5046 int i; 5047 5048 for (i = 0; i < adapter->num_rx_queues; i++) 5049 if (adapter->rx_ring[i]) 5050 igb_clean_rx_ring(adapter->rx_ring[i]); 5051 } 5052 5053 /** 5054 * igb_set_mac - Change the Ethernet Address of the NIC 5055 * @netdev: network interface device structure 5056 * @p: pointer to an address structure 5057 * 5058 * Returns 0 on success, negative on failure 5059 **/ 5060 static int igb_set_mac(struct net_device *netdev, void *p) 5061 { 5062 struct igb_adapter *adapter = netdev_priv(netdev); 5063 struct e1000_hw *hw = &adapter->hw; 5064 struct sockaddr *addr = p; 5065 5066 if (!is_valid_ether_addr(addr->sa_data)) 5067 return -EADDRNOTAVAIL; 5068 5069 eth_hw_addr_set(netdev, addr->sa_data); 5070 memcpy(hw->mac.addr, addr->sa_data, netdev->addr_len); 5071 5072 /* set the correct pool for the new PF MAC address in entry 0 */ 5073 igb_set_default_mac_filter(adapter); 5074 5075 return 0; 5076 } 5077 5078 /** 5079 * igb_write_mc_addr_list - write multicast addresses to MTA 5080 * @netdev: network interface device structure 5081 * 5082 * Writes multicast address list to the MTA hash table. 5083 * Returns: -ENOMEM on failure 5084 * 0 on no addresses written 5085 * X on writing X addresses to MTA 5086 **/ 5087 static int igb_write_mc_addr_list(struct net_device *netdev) 5088 { 5089 struct igb_adapter *adapter = netdev_priv(netdev); 5090 struct e1000_hw *hw = &adapter->hw; 5091 struct netdev_hw_addr *ha; 5092 u8 *mta_list; 5093 int i; 5094 5095 if (netdev_mc_empty(netdev)) { 5096 /* nothing to program, so clear mc list */ 5097 igb_update_mc_addr_list(hw, NULL, 0); 5098 igb_restore_vf_multicasts(adapter); 5099 return 0; 5100 } 5101 5102 mta_list = kcalloc(netdev_mc_count(netdev), 6, GFP_ATOMIC); 5103 if (!mta_list) 5104 return -ENOMEM; 5105 5106 /* The shared function expects a packed array of only addresses. */ 5107 i = 0; 5108 netdev_for_each_mc_addr(ha, netdev) 5109 memcpy(mta_list + (i++ * ETH_ALEN), ha->addr, ETH_ALEN); 5110 5111 igb_update_mc_addr_list(hw, mta_list, i); 5112 kfree(mta_list); 5113 5114 return netdev_mc_count(netdev); 5115 } 5116 5117 static int igb_vlan_promisc_enable(struct igb_adapter *adapter) 5118 { 5119 struct e1000_hw *hw = &adapter->hw; 5120 u32 i, pf_id; 5121 5122 switch (hw->mac.type) { 5123 case e1000_i210: 5124 case e1000_i211: 5125 case e1000_i350: 5126 /* VLAN filtering needed for VLAN prio filter */ 5127 if (adapter->netdev->features & NETIF_F_NTUPLE) 5128 break; 5129 fallthrough; 5130 case e1000_82576: 5131 case e1000_82580: 5132 case e1000_i354: 5133 /* VLAN filtering needed for pool filtering */ 5134 if (adapter->vfs_allocated_count) 5135 break; 5136 fallthrough; 5137 default: 5138 return 1; 5139 } 5140 5141 /* We are already in VLAN promisc, nothing to do */ 5142 if (adapter->flags & IGB_FLAG_VLAN_PROMISC) 5143 return 0; 5144 5145 if (!adapter->vfs_allocated_count) 5146 goto set_vfta; 5147 5148 /* Add PF to all active pools */ 5149 pf_id = adapter->vfs_allocated_count + E1000_VLVF_POOLSEL_SHIFT; 5150 5151 for (i = E1000_VLVF_ARRAY_SIZE; --i;) { 5152 u32 vlvf = rd32(E1000_VLVF(i)); 5153 5154 vlvf |= BIT(pf_id); 5155 wr32(E1000_VLVF(i), vlvf); 5156 } 5157 5158 set_vfta: 5159 /* Set all bits in the VLAN filter table array */ 5160 for (i = E1000_VLAN_FILTER_TBL_SIZE; i--;) 5161 hw->mac.ops.write_vfta(hw, i, ~0U); 5162 5163 /* Set flag so we don't redo unnecessary work */ 5164 adapter->flags |= IGB_FLAG_VLAN_PROMISC; 5165 5166 return 0; 5167 } 5168 5169 #define VFTA_BLOCK_SIZE 8 5170 static void igb_scrub_vfta(struct igb_adapter *adapter, u32 vfta_offset) 5171 { 5172 struct e1000_hw *hw = &adapter->hw; 5173 u32 vfta[VFTA_BLOCK_SIZE] = { 0 }; 5174 u32 vid_start = vfta_offset * 32; 5175 u32 vid_end = vid_start + (VFTA_BLOCK_SIZE * 32); 5176 u32 i, vid, word, bits, pf_id; 5177 5178 /* guarantee that we don't scrub out management VLAN */ 5179 vid = adapter->mng_vlan_id; 5180 if (vid >= vid_start && vid < vid_end) 5181 vfta[(vid - vid_start) / 32] |= BIT(vid % 32); 5182 5183 if (!adapter->vfs_allocated_count) 5184 goto set_vfta; 5185 5186 pf_id = adapter->vfs_allocated_count + E1000_VLVF_POOLSEL_SHIFT; 5187 5188 for (i = E1000_VLVF_ARRAY_SIZE; --i;) { 5189 u32 vlvf = rd32(E1000_VLVF(i)); 5190 5191 /* pull VLAN ID from VLVF */ 5192 vid = vlvf & VLAN_VID_MASK; 5193 5194 /* only concern ourselves with a certain range */ 5195 if (vid < vid_start || vid >= vid_end) 5196 continue; 5197 5198 if (vlvf & E1000_VLVF_VLANID_ENABLE) { 5199 /* record VLAN ID in VFTA */ 5200 vfta[(vid - vid_start) / 32] |= BIT(vid % 32); 5201 5202 /* if PF is part of this then continue */ 5203 if (test_bit(vid, adapter->active_vlans)) 5204 continue; 5205 } 5206 5207 /* remove PF from the pool */ 5208 bits = ~BIT(pf_id); 5209 bits &= rd32(E1000_VLVF(i)); 5210 wr32(E1000_VLVF(i), bits); 5211 } 5212 5213 set_vfta: 5214 /* extract values from active_vlans and write back to VFTA */ 5215 for (i = VFTA_BLOCK_SIZE; i--;) { 5216 vid = (vfta_offset + i) * 32; 5217 word = vid / BITS_PER_LONG; 5218 bits = vid % BITS_PER_LONG; 5219 5220 vfta[i] |= adapter->active_vlans[word] >> bits; 5221 5222 hw->mac.ops.write_vfta(hw, vfta_offset + i, vfta[i]); 5223 } 5224 } 5225 5226 static void igb_vlan_promisc_disable(struct igb_adapter *adapter) 5227 { 5228 u32 i; 5229 5230 /* We are not in VLAN promisc, nothing to do */ 5231 if (!(adapter->flags & IGB_FLAG_VLAN_PROMISC)) 5232 return; 5233 5234 /* Set flag so we don't redo unnecessary work */ 5235 adapter->flags &= ~IGB_FLAG_VLAN_PROMISC; 5236 5237 for (i = 0; i < E1000_VLAN_FILTER_TBL_SIZE; i += VFTA_BLOCK_SIZE) 5238 igb_scrub_vfta(adapter, i); 5239 } 5240 5241 /** 5242 * igb_set_rx_mode - Secondary Unicast, Multicast and Promiscuous mode set 5243 * @netdev: network interface device structure 5244 * 5245 * The set_rx_mode entry point is called whenever the unicast or multicast 5246 * address lists or the network interface flags are updated. This routine is 5247 * responsible for configuring the hardware for proper unicast, multicast, 5248 * promiscuous mode, and all-multi behavior. 5249 **/ 5250 static void igb_set_rx_mode(struct net_device *netdev) 5251 { 5252 struct igb_adapter *adapter = netdev_priv(netdev); 5253 struct e1000_hw *hw = &adapter->hw; 5254 unsigned int vfn = adapter->vfs_allocated_count; 5255 u32 rctl = 0, vmolr = 0, rlpml = MAX_JUMBO_FRAME_SIZE; 5256 int count; 5257 5258 /* Check for Promiscuous and All Multicast modes */ 5259 if (netdev->flags & IFF_PROMISC) { 5260 rctl |= E1000_RCTL_UPE | E1000_RCTL_MPE; 5261 vmolr |= E1000_VMOLR_MPME; 5262 5263 /* enable use of UTA filter to force packets to default pool */ 5264 if (hw->mac.type == e1000_82576) 5265 vmolr |= E1000_VMOLR_ROPE; 5266 } else { 5267 if (netdev->flags & IFF_ALLMULTI) { 5268 rctl |= E1000_RCTL_MPE; 5269 vmolr |= E1000_VMOLR_MPME; 5270 } else { 5271 /* Write addresses to the MTA, if the attempt fails 5272 * then we should just turn on promiscuous mode so 5273 * that we can at least receive multicast traffic 5274 */ 5275 count = igb_write_mc_addr_list(netdev); 5276 if (count < 0) { 5277 rctl |= E1000_RCTL_MPE; 5278 vmolr |= E1000_VMOLR_MPME; 5279 } else if (count) { 5280 vmolr |= E1000_VMOLR_ROMPE; 5281 } 5282 } 5283 } 5284 5285 /* Write addresses to available RAR registers, if there is not 5286 * sufficient space to store all the addresses then enable 5287 * unicast promiscuous mode 5288 */ 5289 if (__dev_uc_sync(netdev, igb_uc_sync, igb_uc_unsync)) { 5290 rctl |= E1000_RCTL_UPE; 5291 vmolr |= E1000_VMOLR_ROPE; 5292 } 5293 5294 /* enable VLAN filtering by default */ 5295 rctl |= E1000_RCTL_VFE; 5296 5297 /* disable VLAN filtering for modes that require it */ 5298 if ((netdev->flags & IFF_PROMISC) || 5299 (netdev->features & NETIF_F_RXALL)) { 5300 /* if we fail to set all rules then just clear VFE */ 5301 if (igb_vlan_promisc_enable(adapter)) 5302 rctl &= ~E1000_RCTL_VFE; 5303 } else { 5304 igb_vlan_promisc_disable(adapter); 5305 } 5306 5307 /* update state of unicast, multicast, and VLAN filtering modes */ 5308 rctl |= rd32(E1000_RCTL) & ~(E1000_RCTL_UPE | E1000_RCTL_MPE | 5309 E1000_RCTL_VFE); 5310 wr32(E1000_RCTL, rctl); 5311 5312 #if (PAGE_SIZE < 8192) 5313 if (!adapter->vfs_allocated_count) { 5314 if (adapter->max_frame_size <= IGB_MAX_FRAME_BUILD_SKB) 5315 rlpml = IGB_MAX_FRAME_BUILD_SKB; 5316 } 5317 #endif 5318 wr32(E1000_RLPML, rlpml); 5319 5320 /* In order to support SR-IOV and eventually VMDq it is necessary to set 5321 * the VMOLR to enable the appropriate modes. Without this workaround 5322 * we will have issues with VLAN tag stripping not being done for frames 5323 * that are only arriving because we are the default pool 5324 */ 5325 if ((hw->mac.type < e1000_82576) || (hw->mac.type > e1000_i350)) 5326 return; 5327 5328 /* set UTA to appropriate mode */ 5329 igb_set_uta(adapter, !!(vmolr & E1000_VMOLR_ROPE)); 5330 5331 vmolr |= rd32(E1000_VMOLR(vfn)) & 5332 ~(E1000_VMOLR_ROPE | E1000_VMOLR_MPME | E1000_VMOLR_ROMPE); 5333 5334 /* enable Rx jumbo frames, restrict as needed to support build_skb */ 5335 vmolr &= ~E1000_VMOLR_RLPML_MASK; 5336 #if (PAGE_SIZE < 8192) 5337 if (adapter->max_frame_size <= IGB_MAX_FRAME_BUILD_SKB) 5338 vmolr |= IGB_MAX_FRAME_BUILD_SKB; 5339 else 5340 #endif 5341 vmolr |= MAX_JUMBO_FRAME_SIZE; 5342 vmolr |= E1000_VMOLR_LPE; 5343 5344 wr32(E1000_VMOLR(vfn), vmolr); 5345 5346 igb_restore_vf_multicasts(adapter); 5347 } 5348 5349 static void igb_check_wvbr(struct igb_adapter *adapter) 5350 { 5351 struct e1000_hw *hw = &adapter->hw; 5352 u32 wvbr = 0; 5353 5354 switch (hw->mac.type) { 5355 case e1000_82576: 5356 case e1000_i350: 5357 wvbr = rd32(E1000_WVBR); 5358 if (!wvbr) 5359 return; 5360 break; 5361 default: 5362 break; 5363 } 5364 5365 adapter->wvbr |= wvbr; 5366 } 5367 5368 #define IGB_STAGGERED_QUEUE_OFFSET 8 5369 5370 static void igb_spoof_check(struct igb_adapter *adapter) 5371 { 5372 int j; 5373 5374 if (!adapter->wvbr) 5375 return; 5376 5377 for (j = 0; j < adapter->vfs_allocated_count; j++) { 5378 if (adapter->wvbr & BIT(j) || 5379 adapter->wvbr & BIT(j + IGB_STAGGERED_QUEUE_OFFSET)) { 5380 dev_warn(&adapter->pdev->dev, 5381 "Spoof event(s) detected on VF %d\n", j); 5382 adapter->wvbr &= 5383 ~(BIT(j) | 5384 BIT(j + IGB_STAGGERED_QUEUE_OFFSET)); 5385 } 5386 } 5387 } 5388 5389 /* Need to wait a few seconds after link up to get diagnostic information from 5390 * the phy 5391 */ 5392 static void igb_update_phy_info(struct timer_list *t) 5393 { 5394 struct igb_adapter *adapter = from_timer(adapter, t, phy_info_timer); 5395 igb_get_phy_info(&adapter->hw); 5396 } 5397 5398 /** 5399 * igb_has_link - check shared code for link and determine up/down 5400 * @adapter: pointer to driver private info 5401 **/ 5402 bool igb_has_link(struct igb_adapter *adapter) 5403 { 5404 struct e1000_hw *hw = &adapter->hw; 5405 bool link_active = false; 5406 5407 /* get_link_status is set on LSC (link status) interrupt or 5408 * rx sequence error interrupt. get_link_status will stay 5409 * false until the e1000_check_for_link establishes link 5410 * for copper adapters ONLY 5411 */ 5412 switch (hw->phy.media_type) { 5413 case e1000_media_type_copper: 5414 if (!hw->mac.get_link_status) 5415 return true; 5416 fallthrough; 5417 case e1000_media_type_internal_serdes: 5418 hw->mac.ops.check_for_link(hw); 5419 link_active = !hw->mac.get_link_status; 5420 break; 5421 default: 5422 case e1000_media_type_unknown: 5423 break; 5424 } 5425 5426 if (((hw->mac.type == e1000_i210) || 5427 (hw->mac.type == e1000_i211)) && 5428 (hw->phy.id == I210_I_PHY_ID)) { 5429 if (!netif_carrier_ok(adapter->netdev)) { 5430 adapter->flags &= ~IGB_FLAG_NEED_LINK_UPDATE; 5431 } else if (!(adapter->flags & IGB_FLAG_NEED_LINK_UPDATE)) { 5432 adapter->flags |= IGB_FLAG_NEED_LINK_UPDATE; 5433 adapter->link_check_timeout = jiffies; 5434 } 5435 } 5436 5437 return link_active; 5438 } 5439 5440 static bool igb_thermal_sensor_event(struct e1000_hw *hw, u32 event) 5441 { 5442 bool ret = false; 5443 u32 ctrl_ext, thstat; 5444 5445 /* check for thermal sensor event on i350 copper only */ 5446 if (hw->mac.type == e1000_i350) { 5447 thstat = rd32(E1000_THSTAT); 5448 ctrl_ext = rd32(E1000_CTRL_EXT); 5449 5450 if ((hw->phy.media_type == e1000_media_type_copper) && 5451 !(ctrl_ext & E1000_CTRL_EXT_LINK_MODE_SGMII)) 5452 ret = !!(thstat & event); 5453 } 5454 5455 return ret; 5456 } 5457 5458 /** 5459 * igb_check_lvmmc - check for malformed packets received 5460 * and indicated in LVMMC register 5461 * @adapter: pointer to adapter 5462 **/ 5463 static void igb_check_lvmmc(struct igb_adapter *adapter) 5464 { 5465 struct e1000_hw *hw = &adapter->hw; 5466 u32 lvmmc; 5467 5468 lvmmc = rd32(E1000_LVMMC); 5469 if (lvmmc) { 5470 if (unlikely(net_ratelimit())) { 5471 netdev_warn(adapter->netdev, 5472 "malformed Tx packet detected and dropped, LVMMC:0x%08x\n", 5473 lvmmc); 5474 } 5475 } 5476 } 5477 5478 /** 5479 * igb_watchdog - Timer Call-back 5480 * @t: pointer to timer_list containing our private info pointer 5481 **/ 5482 static void igb_watchdog(struct timer_list *t) 5483 { 5484 struct igb_adapter *adapter = from_timer(adapter, t, watchdog_timer); 5485 /* Do the rest outside of interrupt context */ 5486 schedule_work(&adapter->watchdog_task); 5487 } 5488 5489 static void igb_watchdog_task(struct work_struct *work) 5490 { 5491 struct igb_adapter *adapter = container_of(work, 5492 struct igb_adapter, 5493 watchdog_task); 5494 struct e1000_hw *hw = &adapter->hw; 5495 struct e1000_phy_info *phy = &hw->phy; 5496 struct net_device *netdev = adapter->netdev; 5497 u32 link; 5498 int i; 5499 u32 connsw; 5500 u16 phy_data, retry_count = 20; 5501 5502 link = igb_has_link(adapter); 5503 5504 if (adapter->flags & IGB_FLAG_NEED_LINK_UPDATE) { 5505 if (time_after(jiffies, (adapter->link_check_timeout + HZ))) 5506 adapter->flags &= ~IGB_FLAG_NEED_LINK_UPDATE; 5507 else 5508 link = false; 5509 } 5510 5511 /* Force link down if we have fiber to swap to */ 5512 if (adapter->flags & IGB_FLAG_MAS_ENABLE) { 5513 if (hw->phy.media_type == e1000_media_type_copper) { 5514 connsw = rd32(E1000_CONNSW); 5515 if (!(connsw & E1000_CONNSW_AUTOSENSE_EN)) 5516 link = 0; 5517 } 5518 } 5519 if (link) { 5520 /* Perform a reset if the media type changed. */ 5521 if (hw->dev_spec._82575.media_changed) { 5522 hw->dev_spec._82575.media_changed = false; 5523 adapter->flags |= IGB_FLAG_MEDIA_RESET; 5524 igb_reset(adapter); 5525 } 5526 /* Cancel scheduled suspend requests. */ 5527 pm_runtime_resume(netdev->dev.parent); 5528 5529 if (!netif_carrier_ok(netdev)) { 5530 u32 ctrl; 5531 5532 hw->mac.ops.get_speed_and_duplex(hw, 5533 &adapter->link_speed, 5534 &adapter->link_duplex); 5535 5536 ctrl = rd32(E1000_CTRL); 5537 /* Links status message must follow this format */ 5538 netdev_info(netdev, 5539 "igb: %s NIC Link is Up %d Mbps %s Duplex, Flow Control: %s\n", 5540 netdev->name, 5541 adapter->link_speed, 5542 adapter->link_duplex == FULL_DUPLEX ? 5543 "Full" : "Half", 5544 (ctrl & E1000_CTRL_TFCE) && 5545 (ctrl & E1000_CTRL_RFCE) ? "RX/TX" : 5546 (ctrl & E1000_CTRL_RFCE) ? "RX" : 5547 (ctrl & E1000_CTRL_TFCE) ? "TX" : "None"); 5548 5549 /* disable EEE if enabled */ 5550 if ((adapter->flags & IGB_FLAG_EEE) && 5551 (adapter->link_duplex == HALF_DUPLEX)) { 5552 dev_info(&adapter->pdev->dev, 5553 "EEE Disabled: unsupported at half duplex. Re-enable using ethtool when at full duplex.\n"); 5554 adapter->hw.dev_spec._82575.eee_disable = true; 5555 adapter->flags &= ~IGB_FLAG_EEE; 5556 } 5557 5558 /* check if SmartSpeed worked */ 5559 igb_check_downshift(hw); 5560 if (phy->speed_downgraded) 5561 netdev_warn(netdev, "Link Speed was downgraded by SmartSpeed\n"); 5562 5563 /* check for thermal sensor event */ 5564 if (igb_thermal_sensor_event(hw, 5565 E1000_THSTAT_LINK_THROTTLE)) 5566 netdev_info(netdev, "The network adapter link speed was downshifted because it overheated\n"); 5567 5568 /* adjust timeout factor according to speed/duplex */ 5569 adapter->tx_timeout_factor = 1; 5570 switch (adapter->link_speed) { 5571 case SPEED_10: 5572 adapter->tx_timeout_factor = 14; 5573 break; 5574 case SPEED_100: 5575 /* maybe add some timeout factor ? */ 5576 break; 5577 } 5578 5579 if (adapter->link_speed != SPEED_1000 || 5580 !hw->phy.ops.read_reg) 5581 goto no_wait; 5582 5583 /* wait for Remote receiver status OK */ 5584 retry_read_status: 5585 if (!igb_read_phy_reg(hw, PHY_1000T_STATUS, 5586 &phy_data)) { 5587 if (!(phy_data & SR_1000T_REMOTE_RX_STATUS) && 5588 retry_count) { 5589 msleep(100); 5590 retry_count--; 5591 goto retry_read_status; 5592 } else if (!retry_count) { 5593 dev_err(&adapter->pdev->dev, "exceed max 2 second\n"); 5594 } 5595 } else { 5596 dev_err(&adapter->pdev->dev, "read 1000Base-T Status Reg\n"); 5597 } 5598 no_wait: 5599 netif_carrier_on(netdev); 5600 5601 igb_ping_all_vfs(adapter); 5602 igb_check_vf_rate_limit(adapter); 5603 5604 /* link state has changed, schedule phy info update */ 5605 if (!test_bit(__IGB_DOWN, &adapter->state)) 5606 mod_timer(&adapter->phy_info_timer, 5607 round_jiffies(jiffies + 2 * HZ)); 5608 } 5609 } else { 5610 if (netif_carrier_ok(netdev)) { 5611 adapter->link_speed = 0; 5612 adapter->link_duplex = 0; 5613 5614 /* check for thermal sensor event */ 5615 if (igb_thermal_sensor_event(hw, 5616 E1000_THSTAT_PWR_DOWN)) { 5617 netdev_err(netdev, "The network adapter was stopped because it overheated\n"); 5618 } 5619 5620 /* Links status message must follow this format */ 5621 netdev_info(netdev, "igb: %s NIC Link is Down\n", 5622 netdev->name); 5623 netif_carrier_off(netdev); 5624 5625 igb_ping_all_vfs(adapter); 5626 5627 /* link state has changed, schedule phy info update */ 5628 if (!test_bit(__IGB_DOWN, &adapter->state)) 5629 mod_timer(&adapter->phy_info_timer, 5630 round_jiffies(jiffies + 2 * HZ)); 5631 5632 /* link is down, time to check for alternate media */ 5633 if (adapter->flags & IGB_FLAG_MAS_ENABLE) { 5634 igb_check_swap_media(adapter); 5635 if (adapter->flags & IGB_FLAG_MEDIA_RESET) { 5636 schedule_work(&adapter->reset_task); 5637 /* return immediately */ 5638 return; 5639 } 5640 } 5641 pm_schedule_suspend(netdev->dev.parent, 5642 MSEC_PER_SEC * 5); 5643 5644 /* also check for alternate media here */ 5645 } else if (!netif_carrier_ok(netdev) && 5646 (adapter->flags & IGB_FLAG_MAS_ENABLE)) { 5647 igb_check_swap_media(adapter); 5648 if (adapter->flags & IGB_FLAG_MEDIA_RESET) { 5649 schedule_work(&adapter->reset_task); 5650 /* return immediately */ 5651 return; 5652 } 5653 } 5654 } 5655 5656 spin_lock(&adapter->stats64_lock); 5657 igb_update_stats(adapter); 5658 spin_unlock(&adapter->stats64_lock); 5659 5660 for (i = 0; i < adapter->num_tx_queues; i++) { 5661 struct igb_ring *tx_ring = adapter->tx_ring[i]; 5662 if (!netif_carrier_ok(netdev)) { 5663 /* We've lost link, so the controller stops DMA, 5664 * but we've got queued Tx work that's never going 5665 * to get done, so reset controller to flush Tx. 5666 * (Do the reset outside of interrupt context). 5667 */ 5668 if (igb_desc_unused(tx_ring) + 1 < tx_ring->count) { 5669 adapter->tx_timeout_count++; 5670 schedule_work(&adapter->reset_task); 5671 /* return immediately since reset is imminent */ 5672 return; 5673 } 5674 } 5675 5676 /* Force detection of hung controller every watchdog period */ 5677 set_bit(IGB_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags); 5678 } 5679 5680 /* Cause software interrupt to ensure Rx ring is cleaned */ 5681 if (adapter->flags & IGB_FLAG_HAS_MSIX) { 5682 u32 eics = 0; 5683 5684 for (i = 0; i < adapter->num_q_vectors; i++) 5685 eics |= adapter->q_vector[i]->eims_value; 5686 wr32(E1000_EICS, eics); 5687 } else { 5688 wr32(E1000_ICS, E1000_ICS_RXDMT0); 5689 } 5690 5691 igb_spoof_check(adapter); 5692 igb_ptp_rx_hang(adapter); 5693 igb_ptp_tx_hang(adapter); 5694 5695 /* Check LVMMC register on i350/i354 only */ 5696 if ((adapter->hw.mac.type == e1000_i350) || 5697 (adapter->hw.mac.type == e1000_i354)) 5698 igb_check_lvmmc(adapter); 5699 5700 /* Reset the timer */ 5701 if (!test_bit(__IGB_DOWN, &adapter->state)) { 5702 if (adapter->flags & IGB_FLAG_NEED_LINK_UPDATE) 5703 mod_timer(&adapter->watchdog_timer, 5704 round_jiffies(jiffies + HZ)); 5705 else 5706 mod_timer(&adapter->watchdog_timer, 5707 round_jiffies(jiffies + 2 * HZ)); 5708 } 5709 } 5710 5711 enum latency_range { 5712 lowest_latency = 0, 5713 low_latency = 1, 5714 bulk_latency = 2, 5715 latency_invalid = 255 5716 }; 5717 5718 /** 5719 * igb_update_ring_itr - update the dynamic ITR value based on packet size 5720 * @q_vector: pointer to q_vector 5721 * 5722 * Stores a new ITR value based on strictly on packet size. This 5723 * algorithm is less sophisticated than that used in igb_update_itr, 5724 * due to the difficulty of synchronizing statistics across multiple 5725 * receive rings. The divisors and thresholds used by this function 5726 * were determined based on theoretical maximum wire speed and testing 5727 * data, in order to minimize response time while increasing bulk 5728 * throughput. 5729 * This functionality is controlled by ethtool's coalescing settings. 5730 * NOTE: This function is called only when operating in a multiqueue 5731 * receive environment. 5732 **/ 5733 static void igb_update_ring_itr(struct igb_q_vector *q_vector) 5734 { 5735 int new_val = q_vector->itr_val; 5736 int avg_wire_size = 0; 5737 struct igb_adapter *adapter = q_vector->adapter; 5738 unsigned int packets; 5739 5740 /* For non-gigabit speeds, just fix the interrupt rate at 4000 5741 * ints/sec - ITR timer value of 120 ticks. 5742 */ 5743 if (adapter->link_speed != SPEED_1000) { 5744 new_val = IGB_4K_ITR; 5745 goto set_itr_val; 5746 } 5747 5748 packets = q_vector->rx.total_packets; 5749 if (packets) 5750 avg_wire_size = q_vector->rx.total_bytes / packets; 5751 5752 packets = q_vector->tx.total_packets; 5753 if (packets) 5754 avg_wire_size = max_t(u32, avg_wire_size, 5755 q_vector->tx.total_bytes / packets); 5756 5757 /* if avg_wire_size isn't set no work was done */ 5758 if (!avg_wire_size) 5759 goto clear_counts; 5760 5761 /* Add 24 bytes to size to account for CRC, preamble, and gap */ 5762 avg_wire_size += 24; 5763 5764 /* Don't starve jumbo frames */ 5765 avg_wire_size = min(avg_wire_size, 3000); 5766 5767 /* Give a little boost to mid-size frames */ 5768 if ((avg_wire_size > 300) && (avg_wire_size < 1200)) 5769 new_val = avg_wire_size / 3; 5770 else 5771 new_val = avg_wire_size / 2; 5772 5773 /* conservative mode (itr 3) eliminates the lowest_latency setting */ 5774 if (new_val < IGB_20K_ITR && 5775 ((q_vector->rx.ring && adapter->rx_itr_setting == 3) || 5776 (!q_vector->rx.ring && adapter->tx_itr_setting == 3))) 5777 new_val = IGB_20K_ITR; 5778 5779 set_itr_val: 5780 if (new_val != q_vector->itr_val) { 5781 q_vector->itr_val = new_val; 5782 q_vector->set_itr = 1; 5783 } 5784 clear_counts: 5785 q_vector->rx.total_bytes = 0; 5786 q_vector->rx.total_packets = 0; 5787 q_vector->tx.total_bytes = 0; 5788 q_vector->tx.total_packets = 0; 5789 } 5790 5791 /** 5792 * igb_update_itr - update the dynamic ITR value based on statistics 5793 * @q_vector: pointer to q_vector 5794 * @ring_container: ring info to update the itr for 5795 * 5796 * Stores a new ITR value based on packets and byte 5797 * counts during the last interrupt. The advantage of per interrupt 5798 * computation is faster updates and more accurate ITR for the current 5799 * traffic pattern. Constants in this function were computed 5800 * based on theoretical maximum wire speed and thresholds were set based 5801 * on testing data as well as attempting to minimize response time 5802 * while increasing bulk throughput. 5803 * This functionality is controlled by ethtool's coalescing settings. 5804 * NOTE: These calculations are only valid when operating in a single- 5805 * queue environment. 5806 **/ 5807 static void igb_update_itr(struct igb_q_vector *q_vector, 5808 struct igb_ring_container *ring_container) 5809 { 5810 unsigned int packets = ring_container->total_packets; 5811 unsigned int bytes = ring_container->total_bytes; 5812 u8 itrval = ring_container->itr; 5813 5814 /* no packets, exit with status unchanged */ 5815 if (packets == 0) 5816 return; 5817 5818 switch (itrval) { 5819 case lowest_latency: 5820 /* handle TSO and jumbo frames */ 5821 if (bytes/packets > 8000) 5822 itrval = bulk_latency; 5823 else if ((packets < 5) && (bytes > 512)) 5824 itrval = low_latency; 5825 break; 5826 case low_latency: /* 50 usec aka 20000 ints/s */ 5827 if (bytes > 10000) { 5828 /* this if handles the TSO accounting */ 5829 if (bytes/packets > 8000) 5830 itrval = bulk_latency; 5831 else if ((packets < 10) || ((bytes/packets) > 1200)) 5832 itrval = bulk_latency; 5833 else if ((packets > 35)) 5834 itrval = lowest_latency; 5835 } else if (bytes/packets > 2000) { 5836 itrval = bulk_latency; 5837 } else if (packets <= 2 && bytes < 512) { 5838 itrval = lowest_latency; 5839 } 5840 break; 5841 case bulk_latency: /* 250 usec aka 4000 ints/s */ 5842 if (bytes > 25000) { 5843 if (packets > 35) 5844 itrval = low_latency; 5845 } else if (bytes < 1500) { 5846 itrval = low_latency; 5847 } 5848 break; 5849 } 5850 5851 /* clear work counters since we have the values we need */ 5852 ring_container->total_bytes = 0; 5853 ring_container->total_packets = 0; 5854 5855 /* write updated itr to ring container */ 5856 ring_container->itr = itrval; 5857 } 5858 5859 static void igb_set_itr(struct igb_q_vector *q_vector) 5860 { 5861 struct igb_adapter *adapter = q_vector->adapter; 5862 u32 new_itr = q_vector->itr_val; 5863 u8 current_itr = 0; 5864 5865 /* for non-gigabit speeds, just fix the interrupt rate at 4000 */ 5866 if (adapter->link_speed != SPEED_1000) { 5867 current_itr = 0; 5868 new_itr = IGB_4K_ITR; 5869 goto set_itr_now; 5870 } 5871 5872 igb_update_itr(q_vector, &q_vector->tx); 5873 igb_update_itr(q_vector, &q_vector->rx); 5874 5875 current_itr = max(q_vector->rx.itr, q_vector->tx.itr); 5876 5877 /* conservative mode (itr 3) eliminates the lowest_latency setting */ 5878 if (current_itr == lowest_latency && 5879 ((q_vector->rx.ring && adapter->rx_itr_setting == 3) || 5880 (!q_vector->rx.ring && adapter->tx_itr_setting == 3))) 5881 current_itr = low_latency; 5882 5883 switch (current_itr) { 5884 /* counts and packets in update_itr are dependent on these numbers */ 5885 case lowest_latency: 5886 new_itr = IGB_70K_ITR; /* 70,000 ints/sec */ 5887 break; 5888 case low_latency: 5889 new_itr = IGB_20K_ITR; /* 20,000 ints/sec */ 5890 break; 5891 case bulk_latency: 5892 new_itr = IGB_4K_ITR; /* 4,000 ints/sec */ 5893 break; 5894 default: 5895 break; 5896 } 5897 5898 set_itr_now: 5899 if (new_itr != q_vector->itr_val) { 5900 /* this attempts to bias the interrupt rate towards Bulk 5901 * by adding intermediate steps when interrupt rate is 5902 * increasing 5903 */ 5904 new_itr = new_itr > q_vector->itr_val ? 5905 max((new_itr * q_vector->itr_val) / 5906 (new_itr + (q_vector->itr_val >> 2)), 5907 new_itr) : new_itr; 5908 /* Don't write the value here; it resets the adapter's 5909 * internal timer, and causes us to delay far longer than 5910 * we should between interrupts. Instead, we write the ITR 5911 * value at the beginning of the next interrupt so the timing 5912 * ends up being correct. 5913 */ 5914 q_vector->itr_val = new_itr; 5915 q_vector->set_itr = 1; 5916 } 5917 } 5918 5919 static void igb_tx_ctxtdesc(struct igb_ring *tx_ring, 5920 struct igb_tx_buffer *first, 5921 u32 vlan_macip_lens, u32 type_tucmd, 5922 u32 mss_l4len_idx) 5923 { 5924 struct e1000_adv_tx_context_desc *context_desc; 5925 u16 i = tx_ring->next_to_use; 5926 struct timespec64 ts; 5927 5928 context_desc = IGB_TX_CTXTDESC(tx_ring, i); 5929 5930 i++; 5931 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0; 5932 5933 /* set bits to identify this as an advanced context descriptor */ 5934 type_tucmd |= E1000_TXD_CMD_DEXT | E1000_ADVTXD_DTYP_CTXT; 5935 5936 /* For 82575, context index must be unique per ring. */ 5937 if (test_bit(IGB_RING_FLAG_TX_CTX_IDX, &tx_ring->flags)) 5938 mss_l4len_idx |= tx_ring->reg_idx << 4; 5939 5940 context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens); 5941 context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd); 5942 context_desc->mss_l4len_idx = cpu_to_le32(mss_l4len_idx); 5943 5944 /* We assume there is always a valid tx time available. Invalid times 5945 * should have been handled by the upper layers. 5946 */ 5947 if (tx_ring->launchtime_enable) { 5948 ts = ktime_to_timespec64(first->skb->tstamp); 5949 skb_txtime_consumed(first->skb); 5950 context_desc->seqnum_seed = cpu_to_le32(ts.tv_nsec / 32); 5951 } else { 5952 context_desc->seqnum_seed = 0; 5953 } 5954 } 5955 5956 static int igb_tso(struct igb_ring *tx_ring, 5957 struct igb_tx_buffer *first, 5958 u8 *hdr_len) 5959 { 5960 u32 vlan_macip_lens, type_tucmd, mss_l4len_idx; 5961 struct sk_buff *skb = first->skb; 5962 union { 5963 struct iphdr *v4; 5964 struct ipv6hdr *v6; 5965 unsigned char *hdr; 5966 } ip; 5967 union { 5968 struct tcphdr *tcp; 5969 struct udphdr *udp; 5970 unsigned char *hdr; 5971 } l4; 5972 u32 paylen, l4_offset; 5973 int err; 5974 5975 if (skb->ip_summed != CHECKSUM_PARTIAL) 5976 return 0; 5977 5978 if (!skb_is_gso(skb)) 5979 return 0; 5980 5981 err = skb_cow_head(skb, 0); 5982 if (err < 0) 5983 return err; 5984 5985 ip.hdr = skb_network_header(skb); 5986 l4.hdr = skb_checksum_start(skb); 5987 5988 /* ADV DTYP TUCMD MKRLOC/ISCSIHEDLEN */ 5989 type_tucmd = (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) ? 5990 E1000_ADVTXD_TUCMD_L4T_UDP : E1000_ADVTXD_TUCMD_L4T_TCP; 5991 5992 /* initialize outer IP header fields */ 5993 if (ip.v4->version == 4) { 5994 unsigned char *csum_start = skb_checksum_start(skb); 5995 unsigned char *trans_start = ip.hdr + (ip.v4->ihl * 4); 5996 5997 /* IP header will have to cancel out any data that 5998 * is not a part of the outer IP header 5999 */ 6000 ip.v4->check = csum_fold(csum_partial(trans_start, 6001 csum_start - trans_start, 6002 0)); 6003 type_tucmd |= E1000_ADVTXD_TUCMD_IPV4; 6004 6005 ip.v4->tot_len = 0; 6006 first->tx_flags |= IGB_TX_FLAGS_TSO | 6007 IGB_TX_FLAGS_CSUM | 6008 IGB_TX_FLAGS_IPV4; 6009 } else { 6010 ip.v6->payload_len = 0; 6011 first->tx_flags |= IGB_TX_FLAGS_TSO | 6012 IGB_TX_FLAGS_CSUM; 6013 } 6014 6015 /* determine offset of inner transport header */ 6016 l4_offset = l4.hdr - skb->data; 6017 6018 /* remove payload length from inner checksum */ 6019 paylen = skb->len - l4_offset; 6020 if (type_tucmd & E1000_ADVTXD_TUCMD_L4T_TCP) { 6021 /* compute length of segmentation header */ 6022 *hdr_len = (l4.tcp->doff * 4) + l4_offset; 6023 csum_replace_by_diff(&l4.tcp->check, 6024 (__force __wsum)htonl(paylen)); 6025 } else { 6026 /* compute length of segmentation header */ 6027 *hdr_len = sizeof(*l4.udp) + l4_offset; 6028 csum_replace_by_diff(&l4.udp->check, 6029 (__force __wsum)htonl(paylen)); 6030 } 6031 6032 /* update gso size and bytecount with header size */ 6033 first->gso_segs = skb_shinfo(skb)->gso_segs; 6034 first->bytecount += (first->gso_segs - 1) * *hdr_len; 6035 6036 /* MSS L4LEN IDX */ 6037 mss_l4len_idx = (*hdr_len - l4_offset) << E1000_ADVTXD_L4LEN_SHIFT; 6038 mss_l4len_idx |= skb_shinfo(skb)->gso_size << E1000_ADVTXD_MSS_SHIFT; 6039 6040 /* VLAN MACLEN IPLEN */ 6041 vlan_macip_lens = l4.hdr - ip.hdr; 6042 vlan_macip_lens |= (ip.hdr - skb->data) << E1000_ADVTXD_MACLEN_SHIFT; 6043 vlan_macip_lens |= first->tx_flags & IGB_TX_FLAGS_VLAN_MASK; 6044 6045 igb_tx_ctxtdesc(tx_ring, first, vlan_macip_lens, 6046 type_tucmd, mss_l4len_idx); 6047 6048 return 1; 6049 } 6050 6051 static void igb_tx_csum(struct igb_ring *tx_ring, struct igb_tx_buffer *first) 6052 { 6053 struct sk_buff *skb = first->skb; 6054 u32 vlan_macip_lens = 0; 6055 u32 type_tucmd = 0; 6056 6057 if (skb->ip_summed != CHECKSUM_PARTIAL) { 6058 csum_failed: 6059 if (!(first->tx_flags & IGB_TX_FLAGS_VLAN) && 6060 !tx_ring->launchtime_enable) 6061 return; 6062 goto no_csum; 6063 } 6064 6065 switch (skb->csum_offset) { 6066 case offsetof(struct tcphdr, check): 6067 type_tucmd = E1000_ADVTXD_TUCMD_L4T_TCP; 6068 fallthrough; 6069 case offsetof(struct udphdr, check): 6070 break; 6071 case offsetof(struct sctphdr, checksum): 6072 /* validate that this is actually an SCTP request */ 6073 if (skb_csum_is_sctp(skb)) { 6074 type_tucmd = E1000_ADVTXD_TUCMD_L4T_SCTP; 6075 break; 6076 } 6077 fallthrough; 6078 default: 6079 skb_checksum_help(skb); 6080 goto csum_failed; 6081 } 6082 6083 /* update TX checksum flag */ 6084 first->tx_flags |= IGB_TX_FLAGS_CSUM; 6085 vlan_macip_lens = skb_checksum_start_offset(skb) - 6086 skb_network_offset(skb); 6087 no_csum: 6088 vlan_macip_lens |= skb_network_offset(skb) << E1000_ADVTXD_MACLEN_SHIFT; 6089 vlan_macip_lens |= first->tx_flags & IGB_TX_FLAGS_VLAN_MASK; 6090 6091 igb_tx_ctxtdesc(tx_ring, first, vlan_macip_lens, type_tucmd, 0); 6092 } 6093 6094 #define IGB_SET_FLAG(_input, _flag, _result) \ 6095 ((_flag <= _result) ? \ 6096 ((u32)(_input & _flag) * (_result / _flag)) : \ 6097 ((u32)(_input & _flag) / (_flag / _result))) 6098 6099 static u32 igb_tx_cmd_type(struct sk_buff *skb, u32 tx_flags) 6100 { 6101 /* set type for advanced descriptor with frame checksum insertion */ 6102 u32 cmd_type = E1000_ADVTXD_DTYP_DATA | 6103 E1000_ADVTXD_DCMD_DEXT | 6104 E1000_ADVTXD_DCMD_IFCS; 6105 6106 /* set HW vlan bit if vlan is present */ 6107 cmd_type |= IGB_SET_FLAG(tx_flags, IGB_TX_FLAGS_VLAN, 6108 (E1000_ADVTXD_DCMD_VLE)); 6109 6110 /* set segmentation bits for TSO */ 6111 cmd_type |= IGB_SET_FLAG(tx_flags, IGB_TX_FLAGS_TSO, 6112 (E1000_ADVTXD_DCMD_TSE)); 6113 6114 /* set timestamp bit if present */ 6115 cmd_type |= IGB_SET_FLAG(tx_flags, IGB_TX_FLAGS_TSTAMP, 6116 (E1000_ADVTXD_MAC_TSTAMP)); 6117 6118 /* insert frame checksum */ 6119 cmd_type ^= IGB_SET_FLAG(skb->no_fcs, 1, E1000_ADVTXD_DCMD_IFCS); 6120 6121 return cmd_type; 6122 } 6123 6124 static void igb_tx_olinfo_status(struct igb_ring *tx_ring, 6125 union e1000_adv_tx_desc *tx_desc, 6126 u32 tx_flags, unsigned int paylen) 6127 { 6128 u32 olinfo_status = paylen << E1000_ADVTXD_PAYLEN_SHIFT; 6129 6130 /* 82575 requires a unique index per ring */ 6131 if (test_bit(IGB_RING_FLAG_TX_CTX_IDX, &tx_ring->flags)) 6132 olinfo_status |= tx_ring->reg_idx << 4; 6133 6134 /* insert L4 checksum */ 6135 olinfo_status |= IGB_SET_FLAG(tx_flags, 6136 IGB_TX_FLAGS_CSUM, 6137 (E1000_TXD_POPTS_TXSM << 8)); 6138 6139 /* insert IPv4 checksum */ 6140 olinfo_status |= IGB_SET_FLAG(tx_flags, 6141 IGB_TX_FLAGS_IPV4, 6142 (E1000_TXD_POPTS_IXSM << 8)); 6143 6144 tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status); 6145 } 6146 6147 static int __igb_maybe_stop_tx(struct igb_ring *tx_ring, const u16 size) 6148 { 6149 struct net_device *netdev = tx_ring->netdev; 6150 6151 netif_stop_subqueue(netdev, tx_ring->queue_index); 6152 6153 /* Herbert's original patch had: 6154 * smp_mb__after_netif_stop_queue(); 6155 * but since that doesn't exist yet, just open code it. 6156 */ 6157 smp_mb(); 6158 6159 /* We need to check again in a case another CPU has just 6160 * made room available. 6161 */ 6162 if (igb_desc_unused(tx_ring) < size) 6163 return -EBUSY; 6164 6165 /* A reprieve! */ 6166 netif_wake_subqueue(netdev, tx_ring->queue_index); 6167 6168 u64_stats_update_begin(&tx_ring->tx_syncp2); 6169 tx_ring->tx_stats.restart_queue2++; 6170 u64_stats_update_end(&tx_ring->tx_syncp2); 6171 6172 return 0; 6173 } 6174 6175 static inline int igb_maybe_stop_tx(struct igb_ring *tx_ring, const u16 size) 6176 { 6177 if (igb_desc_unused(tx_ring) >= size) 6178 return 0; 6179 return __igb_maybe_stop_tx(tx_ring, size); 6180 } 6181 6182 static int igb_tx_map(struct igb_ring *tx_ring, 6183 struct igb_tx_buffer *first, 6184 const u8 hdr_len) 6185 { 6186 struct sk_buff *skb = first->skb; 6187 struct igb_tx_buffer *tx_buffer; 6188 union e1000_adv_tx_desc *tx_desc; 6189 skb_frag_t *frag; 6190 dma_addr_t dma; 6191 unsigned int data_len, size; 6192 u32 tx_flags = first->tx_flags; 6193 u32 cmd_type = igb_tx_cmd_type(skb, tx_flags); 6194 u16 i = tx_ring->next_to_use; 6195 6196 tx_desc = IGB_TX_DESC(tx_ring, i); 6197 6198 igb_tx_olinfo_status(tx_ring, tx_desc, tx_flags, skb->len - hdr_len); 6199 6200 size = skb_headlen(skb); 6201 data_len = skb->data_len; 6202 6203 dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE); 6204 6205 tx_buffer = first; 6206 6207 for (frag = &skb_shinfo(skb)->frags[0];; frag++) { 6208 if (dma_mapping_error(tx_ring->dev, dma)) 6209 goto dma_error; 6210 6211 /* record length, and DMA address */ 6212 dma_unmap_len_set(tx_buffer, len, size); 6213 dma_unmap_addr_set(tx_buffer, dma, dma); 6214 6215 tx_desc->read.buffer_addr = cpu_to_le64(dma); 6216 6217 while (unlikely(size > IGB_MAX_DATA_PER_TXD)) { 6218 tx_desc->read.cmd_type_len = 6219 cpu_to_le32(cmd_type ^ IGB_MAX_DATA_PER_TXD); 6220 6221 i++; 6222 tx_desc++; 6223 if (i == tx_ring->count) { 6224 tx_desc = IGB_TX_DESC(tx_ring, 0); 6225 i = 0; 6226 } 6227 tx_desc->read.olinfo_status = 0; 6228 6229 dma += IGB_MAX_DATA_PER_TXD; 6230 size -= IGB_MAX_DATA_PER_TXD; 6231 6232 tx_desc->read.buffer_addr = cpu_to_le64(dma); 6233 } 6234 6235 if (likely(!data_len)) 6236 break; 6237 6238 tx_desc->read.cmd_type_len = cpu_to_le32(cmd_type ^ size); 6239 6240 i++; 6241 tx_desc++; 6242 if (i == tx_ring->count) { 6243 tx_desc = IGB_TX_DESC(tx_ring, 0); 6244 i = 0; 6245 } 6246 tx_desc->read.olinfo_status = 0; 6247 6248 size = skb_frag_size(frag); 6249 data_len -= size; 6250 6251 dma = skb_frag_dma_map(tx_ring->dev, frag, 0, 6252 size, DMA_TO_DEVICE); 6253 6254 tx_buffer = &tx_ring->tx_buffer_info[i]; 6255 } 6256 6257 /* write last descriptor with RS and EOP bits */ 6258 cmd_type |= size | IGB_TXD_DCMD; 6259 tx_desc->read.cmd_type_len = cpu_to_le32(cmd_type); 6260 6261 netdev_tx_sent_queue(txring_txq(tx_ring), first->bytecount); 6262 6263 /* set the timestamp */ 6264 first->time_stamp = jiffies; 6265 6266 skb_tx_timestamp(skb); 6267 6268 /* Force memory writes to complete before letting h/w know there 6269 * are new descriptors to fetch. (Only applicable for weak-ordered 6270 * memory model archs, such as IA-64). 6271 * 6272 * We also need this memory barrier to make certain all of the 6273 * status bits have been updated before next_to_watch is written. 6274 */ 6275 dma_wmb(); 6276 6277 /* set next_to_watch value indicating a packet is present */ 6278 first->next_to_watch = tx_desc; 6279 6280 i++; 6281 if (i == tx_ring->count) 6282 i = 0; 6283 6284 tx_ring->next_to_use = i; 6285 6286 /* Make sure there is space in the ring for the next send. */ 6287 igb_maybe_stop_tx(tx_ring, DESC_NEEDED); 6288 6289 if (netif_xmit_stopped(txring_txq(tx_ring)) || !netdev_xmit_more()) { 6290 writel(i, tx_ring->tail); 6291 } 6292 return 0; 6293 6294 dma_error: 6295 dev_err(tx_ring->dev, "TX DMA map failed\n"); 6296 tx_buffer = &tx_ring->tx_buffer_info[i]; 6297 6298 /* clear dma mappings for failed tx_buffer_info map */ 6299 while (tx_buffer != first) { 6300 if (dma_unmap_len(tx_buffer, len)) 6301 dma_unmap_page(tx_ring->dev, 6302 dma_unmap_addr(tx_buffer, dma), 6303 dma_unmap_len(tx_buffer, len), 6304 DMA_TO_DEVICE); 6305 dma_unmap_len_set(tx_buffer, len, 0); 6306 6307 if (i-- == 0) 6308 i += tx_ring->count; 6309 tx_buffer = &tx_ring->tx_buffer_info[i]; 6310 } 6311 6312 if (dma_unmap_len(tx_buffer, len)) 6313 dma_unmap_single(tx_ring->dev, 6314 dma_unmap_addr(tx_buffer, dma), 6315 dma_unmap_len(tx_buffer, len), 6316 DMA_TO_DEVICE); 6317 dma_unmap_len_set(tx_buffer, len, 0); 6318 6319 dev_kfree_skb_any(tx_buffer->skb); 6320 tx_buffer->skb = NULL; 6321 6322 tx_ring->next_to_use = i; 6323 6324 return -1; 6325 } 6326 6327 int igb_xmit_xdp_ring(struct igb_adapter *adapter, 6328 struct igb_ring *tx_ring, 6329 struct xdp_frame *xdpf) 6330 { 6331 struct skb_shared_info *sinfo = xdp_get_shared_info_from_frame(xdpf); 6332 u8 nr_frags = unlikely(xdp_frame_has_frags(xdpf)) ? sinfo->nr_frags : 0; 6333 u16 count, i, index = tx_ring->next_to_use; 6334 struct igb_tx_buffer *tx_head = &tx_ring->tx_buffer_info[index]; 6335 struct igb_tx_buffer *tx_buffer = tx_head; 6336 union e1000_adv_tx_desc *tx_desc = IGB_TX_DESC(tx_ring, index); 6337 u32 len = xdpf->len, cmd_type, olinfo_status; 6338 void *data = xdpf->data; 6339 6340 count = TXD_USE_COUNT(len); 6341 for (i = 0; i < nr_frags; i++) 6342 count += TXD_USE_COUNT(skb_frag_size(&sinfo->frags[i])); 6343 6344 if (igb_maybe_stop_tx(tx_ring, count + 3)) 6345 return IGB_XDP_CONSUMED; 6346 6347 i = 0; 6348 /* record the location of the first descriptor for this packet */ 6349 tx_head->bytecount = xdp_get_frame_len(xdpf); 6350 tx_head->type = IGB_TYPE_XDP; 6351 tx_head->gso_segs = 1; 6352 tx_head->xdpf = xdpf; 6353 6354 olinfo_status = tx_head->bytecount << E1000_ADVTXD_PAYLEN_SHIFT; 6355 /* 82575 requires a unique index per ring */ 6356 if (test_bit(IGB_RING_FLAG_TX_CTX_IDX, &tx_ring->flags)) 6357 olinfo_status |= tx_ring->reg_idx << 4; 6358 tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status); 6359 6360 for (;;) { 6361 dma_addr_t dma; 6362 6363 dma = dma_map_single(tx_ring->dev, data, len, DMA_TO_DEVICE); 6364 if (dma_mapping_error(tx_ring->dev, dma)) 6365 goto unmap; 6366 6367 /* record length, and DMA address */ 6368 dma_unmap_len_set(tx_buffer, len, len); 6369 dma_unmap_addr_set(tx_buffer, dma, dma); 6370 6371 /* put descriptor type bits */ 6372 cmd_type = E1000_ADVTXD_DTYP_DATA | E1000_ADVTXD_DCMD_DEXT | 6373 E1000_ADVTXD_DCMD_IFCS | len; 6374 6375 tx_desc->read.cmd_type_len = cpu_to_le32(cmd_type); 6376 tx_desc->read.buffer_addr = cpu_to_le64(dma); 6377 6378 tx_buffer->protocol = 0; 6379 6380 if (++index == tx_ring->count) 6381 index = 0; 6382 6383 if (i == nr_frags) 6384 break; 6385 6386 tx_buffer = &tx_ring->tx_buffer_info[index]; 6387 tx_desc = IGB_TX_DESC(tx_ring, index); 6388 tx_desc->read.olinfo_status = 0; 6389 6390 data = skb_frag_address(&sinfo->frags[i]); 6391 len = skb_frag_size(&sinfo->frags[i]); 6392 i++; 6393 } 6394 tx_desc->read.cmd_type_len |= cpu_to_le32(IGB_TXD_DCMD); 6395 6396 netdev_tx_sent_queue(txring_txq(tx_ring), tx_head->bytecount); 6397 /* set the timestamp */ 6398 tx_head->time_stamp = jiffies; 6399 6400 /* Avoid any potential race with xdp_xmit and cleanup */ 6401 smp_wmb(); 6402 6403 /* set next_to_watch value indicating a packet is present */ 6404 tx_head->next_to_watch = tx_desc; 6405 tx_ring->next_to_use = index; 6406 6407 /* Make sure there is space in the ring for the next send. */ 6408 igb_maybe_stop_tx(tx_ring, DESC_NEEDED); 6409 6410 if (netif_xmit_stopped(txring_txq(tx_ring)) || !netdev_xmit_more()) 6411 writel(index, tx_ring->tail); 6412 6413 return IGB_XDP_TX; 6414 6415 unmap: 6416 for (;;) { 6417 tx_buffer = &tx_ring->tx_buffer_info[index]; 6418 if (dma_unmap_len(tx_buffer, len)) 6419 dma_unmap_page(tx_ring->dev, 6420 dma_unmap_addr(tx_buffer, dma), 6421 dma_unmap_len(tx_buffer, len), 6422 DMA_TO_DEVICE); 6423 dma_unmap_len_set(tx_buffer, len, 0); 6424 if (tx_buffer == tx_head) 6425 break; 6426 6427 if (!index) 6428 index += tx_ring->count; 6429 index--; 6430 } 6431 6432 return IGB_XDP_CONSUMED; 6433 } 6434 6435 netdev_tx_t igb_xmit_frame_ring(struct sk_buff *skb, 6436 struct igb_ring *tx_ring) 6437 { 6438 struct igb_tx_buffer *first; 6439 int tso; 6440 u32 tx_flags = 0; 6441 unsigned short f; 6442 u16 count = TXD_USE_COUNT(skb_headlen(skb)); 6443 __be16 protocol = vlan_get_protocol(skb); 6444 u8 hdr_len = 0; 6445 6446 /* need: 1 descriptor per page * PAGE_SIZE/IGB_MAX_DATA_PER_TXD, 6447 * + 1 desc for skb_headlen/IGB_MAX_DATA_PER_TXD, 6448 * + 2 desc gap to keep tail from touching head, 6449 * + 1 desc for context descriptor, 6450 * otherwise try next time 6451 */ 6452 for (f = 0; f < skb_shinfo(skb)->nr_frags; f++) 6453 count += TXD_USE_COUNT(skb_frag_size( 6454 &skb_shinfo(skb)->frags[f])); 6455 6456 if (igb_maybe_stop_tx(tx_ring, count + 3)) { 6457 /* this is a hard error */ 6458 return NETDEV_TX_BUSY; 6459 } 6460 6461 /* record the location of the first descriptor for this packet */ 6462 first = &tx_ring->tx_buffer_info[tx_ring->next_to_use]; 6463 first->type = IGB_TYPE_SKB; 6464 first->skb = skb; 6465 first->bytecount = skb->len; 6466 first->gso_segs = 1; 6467 6468 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) { 6469 struct igb_adapter *adapter = netdev_priv(tx_ring->netdev); 6470 6471 if (adapter->tstamp_config.tx_type == HWTSTAMP_TX_ON && 6472 !test_and_set_bit_lock(__IGB_PTP_TX_IN_PROGRESS, 6473 &adapter->state)) { 6474 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 6475 tx_flags |= IGB_TX_FLAGS_TSTAMP; 6476 6477 adapter->ptp_tx_skb = skb_get(skb); 6478 adapter->ptp_tx_start = jiffies; 6479 if (adapter->hw.mac.type == e1000_82576) 6480 schedule_work(&adapter->ptp_tx_work); 6481 } else { 6482 adapter->tx_hwtstamp_skipped++; 6483 } 6484 } 6485 6486 if (skb_vlan_tag_present(skb)) { 6487 tx_flags |= IGB_TX_FLAGS_VLAN; 6488 tx_flags |= (skb_vlan_tag_get(skb) << IGB_TX_FLAGS_VLAN_SHIFT); 6489 } 6490 6491 /* record initial flags and protocol */ 6492 first->tx_flags = tx_flags; 6493 first->protocol = protocol; 6494 6495 tso = igb_tso(tx_ring, first, &hdr_len); 6496 if (tso < 0) 6497 goto out_drop; 6498 else if (!tso) 6499 igb_tx_csum(tx_ring, first); 6500 6501 if (igb_tx_map(tx_ring, first, hdr_len)) 6502 goto cleanup_tx_tstamp; 6503 6504 return NETDEV_TX_OK; 6505 6506 out_drop: 6507 dev_kfree_skb_any(first->skb); 6508 first->skb = NULL; 6509 cleanup_tx_tstamp: 6510 if (unlikely(tx_flags & IGB_TX_FLAGS_TSTAMP)) { 6511 struct igb_adapter *adapter = netdev_priv(tx_ring->netdev); 6512 6513 dev_kfree_skb_any(adapter->ptp_tx_skb); 6514 adapter->ptp_tx_skb = NULL; 6515 if (adapter->hw.mac.type == e1000_82576) 6516 cancel_work_sync(&adapter->ptp_tx_work); 6517 clear_bit_unlock(__IGB_PTP_TX_IN_PROGRESS, &adapter->state); 6518 } 6519 6520 return NETDEV_TX_OK; 6521 } 6522 6523 static inline struct igb_ring *igb_tx_queue_mapping(struct igb_adapter *adapter, 6524 struct sk_buff *skb) 6525 { 6526 unsigned int r_idx = skb->queue_mapping; 6527 6528 if (r_idx >= adapter->num_tx_queues) 6529 r_idx = r_idx % adapter->num_tx_queues; 6530 6531 return adapter->tx_ring[r_idx]; 6532 } 6533 6534 static netdev_tx_t igb_xmit_frame(struct sk_buff *skb, 6535 struct net_device *netdev) 6536 { 6537 struct igb_adapter *adapter = netdev_priv(netdev); 6538 6539 /* The minimum packet size with TCTL.PSP set is 17 so pad the skb 6540 * in order to meet this minimum size requirement. 6541 */ 6542 if (skb_put_padto(skb, 17)) 6543 return NETDEV_TX_OK; 6544 6545 return igb_xmit_frame_ring(skb, igb_tx_queue_mapping(adapter, skb)); 6546 } 6547 6548 /** 6549 * igb_tx_timeout - Respond to a Tx Hang 6550 * @netdev: network interface device structure 6551 * @txqueue: number of the Tx queue that hung (unused) 6552 **/ 6553 static void igb_tx_timeout(struct net_device *netdev, unsigned int __always_unused txqueue) 6554 { 6555 struct igb_adapter *adapter = netdev_priv(netdev); 6556 struct e1000_hw *hw = &adapter->hw; 6557 6558 /* Do the reset outside of interrupt context */ 6559 adapter->tx_timeout_count++; 6560 6561 if (hw->mac.type >= e1000_82580) 6562 hw->dev_spec._82575.global_device_reset = true; 6563 6564 schedule_work(&adapter->reset_task); 6565 wr32(E1000_EICS, 6566 (adapter->eims_enable_mask & ~adapter->eims_other)); 6567 } 6568 6569 static void igb_reset_task(struct work_struct *work) 6570 { 6571 struct igb_adapter *adapter; 6572 adapter = container_of(work, struct igb_adapter, reset_task); 6573 6574 rtnl_lock(); 6575 /* If we're already down or resetting, just bail */ 6576 if (test_bit(__IGB_DOWN, &adapter->state) || 6577 test_bit(__IGB_RESETTING, &adapter->state)) { 6578 rtnl_unlock(); 6579 return; 6580 } 6581 6582 igb_dump(adapter); 6583 netdev_err(adapter->netdev, "Reset adapter\n"); 6584 igb_reinit_locked(adapter); 6585 rtnl_unlock(); 6586 } 6587 6588 /** 6589 * igb_get_stats64 - Get System Network Statistics 6590 * @netdev: network interface device structure 6591 * @stats: rtnl_link_stats64 pointer 6592 **/ 6593 static void igb_get_stats64(struct net_device *netdev, 6594 struct rtnl_link_stats64 *stats) 6595 { 6596 struct igb_adapter *adapter = netdev_priv(netdev); 6597 6598 spin_lock(&adapter->stats64_lock); 6599 igb_update_stats(adapter); 6600 memcpy(stats, &adapter->stats64, sizeof(*stats)); 6601 spin_unlock(&adapter->stats64_lock); 6602 } 6603 6604 /** 6605 * igb_change_mtu - Change the Maximum Transfer Unit 6606 * @netdev: network interface device structure 6607 * @new_mtu: new value for maximum frame size 6608 * 6609 * Returns 0 on success, negative on failure 6610 **/ 6611 static int igb_change_mtu(struct net_device *netdev, int new_mtu) 6612 { 6613 struct igb_adapter *adapter = netdev_priv(netdev); 6614 int max_frame = new_mtu + IGB_ETH_PKT_HDR_PAD; 6615 6616 if (adapter->xdp_prog) { 6617 int i; 6618 6619 for (i = 0; i < adapter->num_rx_queues; i++) { 6620 struct igb_ring *ring = adapter->rx_ring[i]; 6621 6622 if (max_frame > igb_rx_bufsz(ring)) { 6623 netdev_warn(adapter->netdev, 6624 "Requested MTU size is not supported with XDP. Max frame size is %d\n", 6625 max_frame); 6626 return -EINVAL; 6627 } 6628 } 6629 } 6630 6631 /* adjust max frame to be at least the size of a standard frame */ 6632 if (max_frame < (ETH_FRAME_LEN + ETH_FCS_LEN)) 6633 max_frame = ETH_FRAME_LEN + ETH_FCS_LEN; 6634 6635 while (test_and_set_bit(__IGB_RESETTING, &adapter->state)) 6636 usleep_range(1000, 2000); 6637 6638 /* igb_down has a dependency on max_frame_size */ 6639 adapter->max_frame_size = max_frame; 6640 6641 if (netif_running(netdev)) 6642 igb_down(adapter); 6643 6644 netdev_dbg(netdev, "changing MTU from %d to %d\n", 6645 netdev->mtu, new_mtu); 6646 WRITE_ONCE(netdev->mtu, new_mtu); 6647 6648 if (netif_running(netdev)) 6649 igb_up(adapter); 6650 else 6651 igb_reset(adapter); 6652 6653 clear_bit(__IGB_RESETTING, &adapter->state); 6654 6655 return 0; 6656 } 6657 6658 /** 6659 * igb_update_stats - Update the board statistics counters 6660 * @adapter: board private structure 6661 **/ 6662 void igb_update_stats(struct igb_adapter *adapter) 6663 { 6664 struct rtnl_link_stats64 *net_stats = &adapter->stats64; 6665 struct e1000_hw *hw = &adapter->hw; 6666 struct pci_dev *pdev = adapter->pdev; 6667 u32 reg, mpc; 6668 int i; 6669 u64 bytes, packets; 6670 unsigned int start; 6671 u64 _bytes, _packets; 6672 6673 /* Prevent stats update while adapter is being reset, or if the pci 6674 * connection is down. 6675 */ 6676 if (adapter->link_speed == 0) 6677 return; 6678 if (pci_channel_offline(pdev)) 6679 return; 6680 6681 bytes = 0; 6682 packets = 0; 6683 6684 rcu_read_lock(); 6685 for (i = 0; i < adapter->num_rx_queues; i++) { 6686 struct igb_ring *ring = adapter->rx_ring[i]; 6687 u32 rqdpc = rd32(E1000_RQDPC(i)); 6688 if (hw->mac.type >= e1000_i210) 6689 wr32(E1000_RQDPC(i), 0); 6690 6691 if (rqdpc) { 6692 ring->rx_stats.drops += rqdpc; 6693 net_stats->rx_fifo_errors += rqdpc; 6694 } 6695 6696 do { 6697 start = u64_stats_fetch_begin(&ring->rx_syncp); 6698 _bytes = ring->rx_stats.bytes; 6699 _packets = ring->rx_stats.packets; 6700 } while (u64_stats_fetch_retry(&ring->rx_syncp, start)); 6701 bytes += _bytes; 6702 packets += _packets; 6703 } 6704 6705 net_stats->rx_bytes = bytes; 6706 net_stats->rx_packets = packets; 6707 6708 bytes = 0; 6709 packets = 0; 6710 for (i = 0; i < adapter->num_tx_queues; i++) { 6711 struct igb_ring *ring = adapter->tx_ring[i]; 6712 do { 6713 start = u64_stats_fetch_begin(&ring->tx_syncp); 6714 _bytes = ring->tx_stats.bytes; 6715 _packets = ring->tx_stats.packets; 6716 } while (u64_stats_fetch_retry(&ring->tx_syncp, start)); 6717 bytes += _bytes; 6718 packets += _packets; 6719 } 6720 net_stats->tx_bytes = bytes; 6721 net_stats->tx_packets = packets; 6722 rcu_read_unlock(); 6723 6724 /* read stats registers */ 6725 adapter->stats.crcerrs += rd32(E1000_CRCERRS); 6726 adapter->stats.gprc += rd32(E1000_GPRC); 6727 adapter->stats.gorc += rd32(E1000_GORCL); 6728 rd32(E1000_GORCH); /* clear GORCL */ 6729 adapter->stats.bprc += rd32(E1000_BPRC); 6730 adapter->stats.mprc += rd32(E1000_MPRC); 6731 adapter->stats.roc += rd32(E1000_ROC); 6732 6733 adapter->stats.prc64 += rd32(E1000_PRC64); 6734 adapter->stats.prc127 += rd32(E1000_PRC127); 6735 adapter->stats.prc255 += rd32(E1000_PRC255); 6736 adapter->stats.prc511 += rd32(E1000_PRC511); 6737 adapter->stats.prc1023 += rd32(E1000_PRC1023); 6738 adapter->stats.prc1522 += rd32(E1000_PRC1522); 6739 adapter->stats.symerrs += rd32(E1000_SYMERRS); 6740 adapter->stats.sec += rd32(E1000_SEC); 6741 6742 mpc = rd32(E1000_MPC); 6743 adapter->stats.mpc += mpc; 6744 net_stats->rx_fifo_errors += mpc; 6745 adapter->stats.scc += rd32(E1000_SCC); 6746 adapter->stats.ecol += rd32(E1000_ECOL); 6747 adapter->stats.mcc += rd32(E1000_MCC); 6748 adapter->stats.latecol += rd32(E1000_LATECOL); 6749 adapter->stats.dc += rd32(E1000_DC); 6750 adapter->stats.rlec += rd32(E1000_RLEC); 6751 adapter->stats.xonrxc += rd32(E1000_XONRXC); 6752 adapter->stats.xontxc += rd32(E1000_XONTXC); 6753 adapter->stats.xoffrxc += rd32(E1000_XOFFRXC); 6754 adapter->stats.xofftxc += rd32(E1000_XOFFTXC); 6755 adapter->stats.fcruc += rd32(E1000_FCRUC); 6756 adapter->stats.gptc += rd32(E1000_GPTC); 6757 adapter->stats.gotc += rd32(E1000_GOTCL); 6758 rd32(E1000_GOTCH); /* clear GOTCL */ 6759 adapter->stats.rnbc += rd32(E1000_RNBC); 6760 adapter->stats.ruc += rd32(E1000_RUC); 6761 adapter->stats.rfc += rd32(E1000_RFC); 6762 adapter->stats.rjc += rd32(E1000_RJC); 6763 adapter->stats.tor += rd32(E1000_TORH); 6764 adapter->stats.tot += rd32(E1000_TOTH); 6765 adapter->stats.tpr += rd32(E1000_TPR); 6766 6767 adapter->stats.ptc64 += rd32(E1000_PTC64); 6768 adapter->stats.ptc127 += rd32(E1000_PTC127); 6769 adapter->stats.ptc255 += rd32(E1000_PTC255); 6770 adapter->stats.ptc511 += rd32(E1000_PTC511); 6771 adapter->stats.ptc1023 += rd32(E1000_PTC1023); 6772 adapter->stats.ptc1522 += rd32(E1000_PTC1522); 6773 6774 adapter->stats.mptc += rd32(E1000_MPTC); 6775 adapter->stats.bptc += rd32(E1000_BPTC); 6776 6777 adapter->stats.tpt += rd32(E1000_TPT); 6778 adapter->stats.colc += rd32(E1000_COLC); 6779 6780 adapter->stats.algnerrc += rd32(E1000_ALGNERRC); 6781 /* read internal phy specific stats */ 6782 reg = rd32(E1000_CTRL_EXT); 6783 if (!(reg & E1000_CTRL_EXT_LINK_MODE_MASK)) { 6784 adapter->stats.rxerrc += rd32(E1000_RXERRC); 6785 6786 /* this stat has invalid values on i210/i211 */ 6787 if ((hw->mac.type != e1000_i210) && 6788 (hw->mac.type != e1000_i211)) 6789 adapter->stats.tncrs += rd32(E1000_TNCRS); 6790 } 6791 6792 adapter->stats.tsctc += rd32(E1000_TSCTC); 6793 adapter->stats.tsctfc += rd32(E1000_TSCTFC); 6794 6795 adapter->stats.iac += rd32(E1000_IAC); 6796 adapter->stats.icrxoc += rd32(E1000_ICRXOC); 6797 adapter->stats.icrxptc += rd32(E1000_ICRXPTC); 6798 adapter->stats.icrxatc += rd32(E1000_ICRXATC); 6799 adapter->stats.ictxptc += rd32(E1000_ICTXPTC); 6800 adapter->stats.ictxatc += rd32(E1000_ICTXATC); 6801 adapter->stats.ictxqec += rd32(E1000_ICTXQEC); 6802 adapter->stats.ictxqmtc += rd32(E1000_ICTXQMTC); 6803 adapter->stats.icrxdmtc += rd32(E1000_ICRXDMTC); 6804 6805 /* Fill out the OS statistics structure */ 6806 net_stats->multicast = adapter->stats.mprc; 6807 net_stats->collisions = adapter->stats.colc; 6808 6809 /* Rx Errors */ 6810 6811 /* RLEC on some newer hardware can be incorrect so build 6812 * our own version based on RUC and ROC 6813 */ 6814 net_stats->rx_errors = adapter->stats.rxerrc + 6815 adapter->stats.crcerrs + adapter->stats.algnerrc + 6816 adapter->stats.ruc + adapter->stats.roc + 6817 adapter->stats.cexterr; 6818 net_stats->rx_length_errors = adapter->stats.ruc + 6819 adapter->stats.roc; 6820 net_stats->rx_crc_errors = adapter->stats.crcerrs; 6821 net_stats->rx_frame_errors = adapter->stats.algnerrc; 6822 net_stats->rx_missed_errors = adapter->stats.mpc; 6823 6824 /* Tx Errors */ 6825 net_stats->tx_errors = adapter->stats.ecol + 6826 adapter->stats.latecol; 6827 net_stats->tx_aborted_errors = adapter->stats.ecol; 6828 net_stats->tx_window_errors = adapter->stats.latecol; 6829 net_stats->tx_carrier_errors = adapter->stats.tncrs; 6830 6831 /* Tx Dropped needs to be maintained elsewhere */ 6832 6833 /* Management Stats */ 6834 adapter->stats.mgptc += rd32(E1000_MGTPTC); 6835 adapter->stats.mgprc += rd32(E1000_MGTPRC); 6836 adapter->stats.mgpdc += rd32(E1000_MGTPDC); 6837 6838 /* OS2BMC Stats */ 6839 reg = rd32(E1000_MANC); 6840 if (reg & E1000_MANC_EN_BMC2OS) { 6841 adapter->stats.o2bgptc += rd32(E1000_O2BGPTC); 6842 adapter->stats.o2bspc += rd32(E1000_O2BSPC); 6843 adapter->stats.b2ospc += rd32(E1000_B2OSPC); 6844 adapter->stats.b2ogprc += rd32(E1000_B2OGPRC); 6845 } 6846 } 6847 6848 static void igb_perout(struct igb_adapter *adapter, int tsintr_tt) 6849 { 6850 int pin = ptp_find_pin(adapter->ptp_clock, PTP_PF_PEROUT, tsintr_tt); 6851 struct e1000_hw *hw = &adapter->hw; 6852 struct timespec64 ts; 6853 u32 tsauxc; 6854 6855 if (pin < 0 || pin >= IGB_N_SDP) 6856 return; 6857 6858 spin_lock(&adapter->tmreg_lock); 6859 6860 if (hw->mac.type == e1000_82580 || 6861 hw->mac.type == e1000_i354 || 6862 hw->mac.type == e1000_i350) { 6863 s64 ns = timespec64_to_ns(&adapter->perout[tsintr_tt].period); 6864 u32 systiml, systimh, level_mask, level, rem; 6865 u64 systim, now; 6866 6867 /* read systim registers in sequence */ 6868 rd32(E1000_SYSTIMR); 6869 systiml = rd32(E1000_SYSTIML); 6870 systimh = rd32(E1000_SYSTIMH); 6871 systim = (((u64)(systimh & 0xFF)) << 32) | ((u64)systiml); 6872 now = timecounter_cyc2time(&adapter->tc, systim); 6873 6874 if (pin < 2) { 6875 level_mask = (tsintr_tt == 1) ? 0x80000 : 0x40000; 6876 level = (rd32(E1000_CTRL) & level_mask) ? 1 : 0; 6877 } else { 6878 level_mask = (tsintr_tt == 1) ? 0x80 : 0x40; 6879 level = (rd32(E1000_CTRL_EXT) & level_mask) ? 1 : 0; 6880 } 6881 6882 div_u64_rem(now, ns, &rem); 6883 systim = systim + (ns - rem); 6884 6885 /* synchronize pin level with rising/falling edges */ 6886 div_u64_rem(now, ns << 1, &rem); 6887 if (rem < ns) { 6888 /* first half of period */ 6889 if (level == 0) { 6890 /* output is already low, skip this period */ 6891 systim += ns; 6892 pr_notice("igb: periodic output on %s missed falling edge\n", 6893 adapter->sdp_config[pin].name); 6894 } 6895 } else { 6896 /* second half of period */ 6897 if (level == 1) { 6898 /* output is already high, skip this period */ 6899 systim += ns; 6900 pr_notice("igb: periodic output on %s missed rising edge\n", 6901 adapter->sdp_config[pin].name); 6902 } 6903 } 6904 6905 /* for this chip family tv_sec is the upper part of the binary value, 6906 * so not seconds 6907 */ 6908 ts.tv_nsec = (u32)systim; 6909 ts.tv_sec = ((u32)(systim >> 32)) & 0xFF; 6910 } else { 6911 ts = timespec64_add(adapter->perout[tsintr_tt].start, 6912 adapter->perout[tsintr_tt].period); 6913 } 6914 6915 /* u32 conversion of tv_sec is safe until y2106 */ 6916 wr32((tsintr_tt == 1) ? E1000_TRGTTIML1 : E1000_TRGTTIML0, ts.tv_nsec); 6917 wr32((tsintr_tt == 1) ? E1000_TRGTTIMH1 : E1000_TRGTTIMH0, (u32)ts.tv_sec); 6918 tsauxc = rd32(E1000_TSAUXC); 6919 tsauxc |= TSAUXC_EN_TT0; 6920 wr32(E1000_TSAUXC, tsauxc); 6921 adapter->perout[tsintr_tt].start = ts; 6922 6923 spin_unlock(&adapter->tmreg_lock); 6924 } 6925 6926 static void igb_extts(struct igb_adapter *adapter, int tsintr_tt) 6927 { 6928 int pin = ptp_find_pin(adapter->ptp_clock, PTP_PF_EXTTS, tsintr_tt); 6929 int auxstmpl = (tsintr_tt == 1) ? E1000_AUXSTMPL1 : E1000_AUXSTMPL0; 6930 int auxstmph = (tsintr_tt == 1) ? E1000_AUXSTMPH1 : E1000_AUXSTMPH0; 6931 struct e1000_hw *hw = &adapter->hw; 6932 struct ptp_clock_event event; 6933 struct timespec64 ts; 6934 unsigned long flags; 6935 6936 if (pin < 0 || pin >= IGB_N_SDP) 6937 return; 6938 6939 if (hw->mac.type == e1000_82580 || 6940 hw->mac.type == e1000_i354 || 6941 hw->mac.type == e1000_i350) { 6942 u64 ns = rd32(auxstmpl); 6943 6944 ns += ((u64)(rd32(auxstmph) & 0xFF)) << 32; 6945 spin_lock_irqsave(&adapter->tmreg_lock, flags); 6946 ns = timecounter_cyc2time(&adapter->tc, ns); 6947 spin_unlock_irqrestore(&adapter->tmreg_lock, flags); 6948 ts = ns_to_timespec64(ns); 6949 } else { 6950 ts.tv_nsec = rd32(auxstmpl); 6951 ts.tv_sec = rd32(auxstmph); 6952 } 6953 6954 event.type = PTP_CLOCK_EXTTS; 6955 event.index = tsintr_tt; 6956 event.timestamp = ts.tv_sec * 1000000000ULL + ts.tv_nsec; 6957 ptp_clock_event(adapter->ptp_clock, &event); 6958 } 6959 6960 static void igb_tsync_interrupt(struct igb_adapter *adapter) 6961 { 6962 struct e1000_hw *hw = &adapter->hw; 6963 u32 tsicr = rd32(E1000_TSICR); 6964 struct ptp_clock_event event; 6965 6966 if (tsicr & TSINTR_SYS_WRAP) { 6967 event.type = PTP_CLOCK_PPS; 6968 if (adapter->ptp_caps.pps) 6969 ptp_clock_event(adapter->ptp_clock, &event); 6970 } 6971 6972 if (tsicr & E1000_TSICR_TXTS) { 6973 /* retrieve hardware timestamp */ 6974 schedule_work(&adapter->ptp_tx_work); 6975 } 6976 6977 if (tsicr & TSINTR_TT0) 6978 igb_perout(adapter, 0); 6979 6980 if (tsicr & TSINTR_TT1) 6981 igb_perout(adapter, 1); 6982 6983 if (tsicr & TSINTR_AUTT0) 6984 igb_extts(adapter, 0); 6985 6986 if (tsicr & TSINTR_AUTT1) 6987 igb_extts(adapter, 1); 6988 } 6989 6990 static irqreturn_t igb_msix_other(int irq, void *data) 6991 { 6992 struct igb_adapter *adapter = data; 6993 struct e1000_hw *hw = &adapter->hw; 6994 u32 icr = rd32(E1000_ICR); 6995 /* reading ICR causes bit 31 of EICR to be cleared */ 6996 6997 if (icr & E1000_ICR_DRSTA) 6998 schedule_work(&adapter->reset_task); 6999 7000 if (icr & E1000_ICR_DOUTSYNC) { 7001 /* HW is reporting DMA is out of sync */ 7002 adapter->stats.doosync++; 7003 /* The DMA Out of Sync is also indication of a spoof event 7004 * in IOV mode. Check the Wrong VM Behavior register to 7005 * see if it is really a spoof event. 7006 */ 7007 igb_check_wvbr(adapter); 7008 } 7009 7010 /* Check for a mailbox event */ 7011 if (icr & E1000_ICR_VMMB) 7012 igb_msg_task(adapter); 7013 7014 if (icr & E1000_ICR_LSC) { 7015 hw->mac.get_link_status = 1; 7016 /* guard against interrupt when we're going down */ 7017 if (!test_bit(__IGB_DOWN, &adapter->state)) 7018 mod_timer(&adapter->watchdog_timer, jiffies + 1); 7019 } 7020 7021 if (icr & E1000_ICR_TS) 7022 igb_tsync_interrupt(adapter); 7023 7024 wr32(E1000_EIMS, adapter->eims_other); 7025 7026 return IRQ_HANDLED; 7027 } 7028 7029 static void igb_write_itr(struct igb_q_vector *q_vector) 7030 { 7031 struct igb_adapter *adapter = q_vector->adapter; 7032 u32 itr_val = q_vector->itr_val & 0x7FFC; 7033 7034 if (!q_vector->set_itr) 7035 return; 7036 7037 if (!itr_val) 7038 itr_val = 0x4; 7039 7040 if (adapter->hw.mac.type == e1000_82575) 7041 itr_val |= itr_val << 16; 7042 else 7043 itr_val |= E1000_EITR_CNT_IGNR; 7044 7045 writel(itr_val, q_vector->itr_register); 7046 q_vector->set_itr = 0; 7047 } 7048 7049 static irqreturn_t igb_msix_ring(int irq, void *data) 7050 { 7051 struct igb_q_vector *q_vector = data; 7052 7053 /* Write the ITR value calculated from the previous interrupt. */ 7054 igb_write_itr(q_vector); 7055 7056 napi_schedule(&q_vector->napi); 7057 7058 return IRQ_HANDLED; 7059 } 7060 7061 #ifdef CONFIG_IGB_DCA 7062 static void igb_update_tx_dca(struct igb_adapter *adapter, 7063 struct igb_ring *tx_ring, 7064 int cpu) 7065 { 7066 struct e1000_hw *hw = &adapter->hw; 7067 u32 txctrl = dca3_get_tag(tx_ring->dev, cpu); 7068 7069 if (hw->mac.type != e1000_82575) 7070 txctrl <<= E1000_DCA_TXCTRL_CPUID_SHIFT; 7071 7072 /* We can enable relaxed ordering for reads, but not writes when 7073 * DCA is enabled. This is due to a known issue in some chipsets 7074 * which will cause the DCA tag to be cleared. 7075 */ 7076 txctrl |= E1000_DCA_TXCTRL_DESC_RRO_EN | 7077 E1000_DCA_TXCTRL_DATA_RRO_EN | 7078 E1000_DCA_TXCTRL_DESC_DCA_EN; 7079 7080 wr32(E1000_DCA_TXCTRL(tx_ring->reg_idx), txctrl); 7081 } 7082 7083 static void igb_update_rx_dca(struct igb_adapter *adapter, 7084 struct igb_ring *rx_ring, 7085 int cpu) 7086 { 7087 struct e1000_hw *hw = &adapter->hw; 7088 u32 rxctrl = dca3_get_tag(&adapter->pdev->dev, cpu); 7089 7090 if (hw->mac.type != e1000_82575) 7091 rxctrl <<= E1000_DCA_RXCTRL_CPUID_SHIFT; 7092 7093 /* We can enable relaxed ordering for reads, but not writes when 7094 * DCA is enabled. This is due to a known issue in some chipsets 7095 * which will cause the DCA tag to be cleared. 7096 */ 7097 rxctrl |= E1000_DCA_RXCTRL_DESC_RRO_EN | 7098 E1000_DCA_RXCTRL_DESC_DCA_EN; 7099 7100 wr32(E1000_DCA_RXCTRL(rx_ring->reg_idx), rxctrl); 7101 } 7102 7103 static void igb_update_dca(struct igb_q_vector *q_vector) 7104 { 7105 struct igb_adapter *adapter = q_vector->adapter; 7106 int cpu = get_cpu(); 7107 7108 if (q_vector->cpu == cpu) 7109 goto out_no_update; 7110 7111 if (q_vector->tx.ring) 7112 igb_update_tx_dca(adapter, q_vector->tx.ring, cpu); 7113 7114 if (q_vector->rx.ring) 7115 igb_update_rx_dca(adapter, q_vector->rx.ring, cpu); 7116 7117 q_vector->cpu = cpu; 7118 out_no_update: 7119 put_cpu(); 7120 } 7121 7122 static void igb_setup_dca(struct igb_adapter *adapter) 7123 { 7124 struct e1000_hw *hw = &adapter->hw; 7125 int i; 7126 7127 if (!(adapter->flags & IGB_FLAG_DCA_ENABLED)) 7128 return; 7129 7130 /* Always use CB2 mode, difference is masked in the CB driver. */ 7131 wr32(E1000_DCA_CTRL, E1000_DCA_CTRL_DCA_MODE_CB2); 7132 7133 for (i = 0; i < adapter->num_q_vectors; i++) { 7134 adapter->q_vector[i]->cpu = -1; 7135 igb_update_dca(adapter->q_vector[i]); 7136 } 7137 } 7138 7139 static int __igb_notify_dca(struct device *dev, void *data) 7140 { 7141 struct net_device *netdev = dev_get_drvdata(dev); 7142 struct igb_adapter *adapter = netdev_priv(netdev); 7143 struct pci_dev *pdev = adapter->pdev; 7144 struct e1000_hw *hw = &adapter->hw; 7145 unsigned long event = *(unsigned long *)data; 7146 7147 switch (event) { 7148 case DCA_PROVIDER_ADD: 7149 /* if already enabled, don't do it again */ 7150 if (adapter->flags & IGB_FLAG_DCA_ENABLED) 7151 break; 7152 if (dca_add_requester(dev) == 0) { 7153 adapter->flags |= IGB_FLAG_DCA_ENABLED; 7154 dev_info(&pdev->dev, "DCA enabled\n"); 7155 igb_setup_dca(adapter); 7156 break; 7157 } 7158 fallthrough; /* since DCA is disabled. */ 7159 case DCA_PROVIDER_REMOVE: 7160 if (adapter->flags & IGB_FLAG_DCA_ENABLED) { 7161 /* without this a class_device is left 7162 * hanging around in the sysfs model 7163 */ 7164 dca_remove_requester(dev); 7165 dev_info(&pdev->dev, "DCA disabled\n"); 7166 adapter->flags &= ~IGB_FLAG_DCA_ENABLED; 7167 wr32(E1000_DCA_CTRL, E1000_DCA_CTRL_DCA_MODE_DISABLE); 7168 } 7169 break; 7170 } 7171 7172 return 0; 7173 } 7174 7175 static int igb_notify_dca(struct notifier_block *nb, unsigned long event, 7176 void *p) 7177 { 7178 int ret_val; 7179 7180 ret_val = driver_for_each_device(&igb_driver.driver, NULL, &event, 7181 __igb_notify_dca); 7182 7183 return ret_val ? NOTIFY_BAD : NOTIFY_DONE; 7184 } 7185 #endif /* CONFIG_IGB_DCA */ 7186 7187 #ifdef CONFIG_PCI_IOV 7188 static int igb_vf_configure(struct igb_adapter *adapter, int vf) 7189 { 7190 unsigned char mac_addr[ETH_ALEN]; 7191 7192 eth_zero_addr(mac_addr); 7193 igb_set_vf_mac(adapter, vf, mac_addr); 7194 7195 /* By default spoof check is enabled for all VFs */ 7196 adapter->vf_data[vf].spoofchk_enabled = true; 7197 7198 /* By default VFs are not trusted */ 7199 adapter->vf_data[vf].trusted = false; 7200 7201 return 0; 7202 } 7203 7204 #endif 7205 static void igb_ping_all_vfs(struct igb_adapter *adapter) 7206 { 7207 struct e1000_hw *hw = &adapter->hw; 7208 u32 ping; 7209 int i; 7210 7211 for (i = 0 ; i < adapter->vfs_allocated_count; i++) { 7212 ping = E1000_PF_CONTROL_MSG; 7213 if (adapter->vf_data[i].flags & IGB_VF_FLAG_CTS) 7214 ping |= E1000_VT_MSGTYPE_CTS; 7215 igb_write_mbx(hw, &ping, 1, i); 7216 } 7217 } 7218 7219 static int igb_set_vf_promisc(struct igb_adapter *adapter, u32 *msgbuf, u32 vf) 7220 { 7221 struct e1000_hw *hw = &adapter->hw; 7222 u32 vmolr = rd32(E1000_VMOLR(vf)); 7223 struct vf_data_storage *vf_data = &adapter->vf_data[vf]; 7224 7225 vf_data->flags &= ~(IGB_VF_FLAG_UNI_PROMISC | 7226 IGB_VF_FLAG_MULTI_PROMISC); 7227 vmolr &= ~(E1000_VMOLR_ROPE | E1000_VMOLR_ROMPE | E1000_VMOLR_MPME); 7228 7229 if (*msgbuf & E1000_VF_SET_PROMISC_MULTICAST) { 7230 vmolr |= E1000_VMOLR_MPME; 7231 vf_data->flags |= IGB_VF_FLAG_MULTI_PROMISC; 7232 *msgbuf &= ~E1000_VF_SET_PROMISC_MULTICAST; 7233 } else { 7234 /* if we have hashes and we are clearing a multicast promisc 7235 * flag we need to write the hashes to the MTA as this step 7236 * was previously skipped 7237 */ 7238 if (vf_data->num_vf_mc_hashes > 30) { 7239 vmolr |= E1000_VMOLR_MPME; 7240 } else if (vf_data->num_vf_mc_hashes) { 7241 int j; 7242 7243 vmolr |= E1000_VMOLR_ROMPE; 7244 for (j = 0; j < vf_data->num_vf_mc_hashes; j++) 7245 igb_mta_set(hw, vf_data->vf_mc_hashes[j]); 7246 } 7247 } 7248 7249 wr32(E1000_VMOLR(vf), vmolr); 7250 7251 /* there are flags left unprocessed, likely not supported */ 7252 if (*msgbuf & E1000_VT_MSGINFO_MASK) 7253 return -EINVAL; 7254 7255 return 0; 7256 } 7257 7258 static int igb_set_vf_multicasts(struct igb_adapter *adapter, 7259 u32 *msgbuf, u32 vf) 7260 { 7261 int n = FIELD_GET(E1000_VT_MSGINFO_MASK, msgbuf[0]); 7262 u16 *hash_list = (u16 *)&msgbuf[1]; 7263 struct vf_data_storage *vf_data = &adapter->vf_data[vf]; 7264 int i; 7265 7266 /* salt away the number of multicast addresses assigned 7267 * to this VF for later use to restore when the PF multi cast 7268 * list changes 7269 */ 7270 vf_data->num_vf_mc_hashes = n; 7271 7272 /* only up to 30 hash values supported */ 7273 if (n > 30) 7274 n = 30; 7275 7276 /* store the hashes for later use */ 7277 for (i = 0; i < n; i++) 7278 vf_data->vf_mc_hashes[i] = hash_list[i]; 7279 7280 /* Flush and reset the mta with the new values */ 7281 igb_set_rx_mode(adapter->netdev); 7282 7283 return 0; 7284 } 7285 7286 static void igb_restore_vf_multicasts(struct igb_adapter *adapter) 7287 { 7288 struct e1000_hw *hw = &adapter->hw; 7289 struct vf_data_storage *vf_data; 7290 int i, j; 7291 7292 for (i = 0; i < adapter->vfs_allocated_count; i++) { 7293 u32 vmolr = rd32(E1000_VMOLR(i)); 7294 7295 vmolr &= ~(E1000_VMOLR_ROMPE | E1000_VMOLR_MPME); 7296 7297 vf_data = &adapter->vf_data[i]; 7298 7299 if ((vf_data->num_vf_mc_hashes > 30) || 7300 (vf_data->flags & IGB_VF_FLAG_MULTI_PROMISC)) { 7301 vmolr |= E1000_VMOLR_MPME; 7302 } else if (vf_data->num_vf_mc_hashes) { 7303 vmolr |= E1000_VMOLR_ROMPE; 7304 for (j = 0; j < vf_data->num_vf_mc_hashes; j++) 7305 igb_mta_set(hw, vf_data->vf_mc_hashes[j]); 7306 } 7307 wr32(E1000_VMOLR(i), vmolr); 7308 } 7309 } 7310 7311 static void igb_clear_vf_vfta(struct igb_adapter *adapter, u32 vf) 7312 { 7313 struct e1000_hw *hw = &adapter->hw; 7314 u32 pool_mask, vlvf_mask, i; 7315 7316 /* create mask for VF and other pools */ 7317 pool_mask = E1000_VLVF_POOLSEL_MASK; 7318 vlvf_mask = BIT(E1000_VLVF_POOLSEL_SHIFT + vf); 7319 7320 /* drop PF from pool bits */ 7321 pool_mask &= ~BIT(E1000_VLVF_POOLSEL_SHIFT + 7322 adapter->vfs_allocated_count); 7323 7324 /* Find the vlan filter for this id */ 7325 for (i = E1000_VLVF_ARRAY_SIZE; i--;) { 7326 u32 vlvf = rd32(E1000_VLVF(i)); 7327 u32 vfta_mask, vid, vfta; 7328 7329 /* remove the vf from the pool */ 7330 if (!(vlvf & vlvf_mask)) 7331 continue; 7332 7333 /* clear out bit from VLVF */ 7334 vlvf ^= vlvf_mask; 7335 7336 /* if other pools are present, just remove ourselves */ 7337 if (vlvf & pool_mask) 7338 goto update_vlvfb; 7339 7340 /* if PF is present, leave VFTA */ 7341 if (vlvf & E1000_VLVF_POOLSEL_MASK) 7342 goto update_vlvf; 7343 7344 vid = vlvf & E1000_VLVF_VLANID_MASK; 7345 vfta_mask = BIT(vid % 32); 7346 7347 /* clear bit from VFTA */ 7348 vfta = adapter->shadow_vfta[vid / 32]; 7349 if (vfta & vfta_mask) 7350 hw->mac.ops.write_vfta(hw, vid / 32, vfta ^ vfta_mask); 7351 update_vlvf: 7352 /* clear pool selection enable */ 7353 if (adapter->flags & IGB_FLAG_VLAN_PROMISC) 7354 vlvf &= E1000_VLVF_POOLSEL_MASK; 7355 else 7356 vlvf = 0; 7357 update_vlvfb: 7358 /* clear pool bits */ 7359 wr32(E1000_VLVF(i), vlvf); 7360 } 7361 } 7362 7363 static int igb_find_vlvf_entry(struct e1000_hw *hw, u32 vlan) 7364 { 7365 u32 vlvf; 7366 int idx; 7367 7368 /* short cut the special case */ 7369 if (vlan == 0) 7370 return 0; 7371 7372 /* Search for the VLAN id in the VLVF entries */ 7373 for (idx = E1000_VLVF_ARRAY_SIZE; --idx;) { 7374 vlvf = rd32(E1000_VLVF(idx)); 7375 if ((vlvf & VLAN_VID_MASK) == vlan) 7376 break; 7377 } 7378 7379 return idx; 7380 } 7381 7382 static void igb_update_pf_vlvf(struct igb_adapter *adapter, u32 vid) 7383 { 7384 struct e1000_hw *hw = &adapter->hw; 7385 u32 bits, pf_id; 7386 int idx; 7387 7388 idx = igb_find_vlvf_entry(hw, vid); 7389 if (!idx) 7390 return; 7391 7392 /* See if any other pools are set for this VLAN filter 7393 * entry other than the PF. 7394 */ 7395 pf_id = adapter->vfs_allocated_count + E1000_VLVF_POOLSEL_SHIFT; 7396 bits = ~BIT(pf_id) & E1000_VLVF_POOLSEL_MASK; 7397 bits &= rd32(E1000_VLVF(idx)); 7398 7399 /* Disable the filter so this falls into the default pool. */ 7400 if (!bits) { 7401 if (adapter->flags & IGB_FLAG_VLAN_PROMISC) 7402 wr32(E1000_VLVF(idx), BIT(pf_id)); 7403 else 7404 wr32(E1000_VLVF(idx), 0); 7405 } 7406 } 7407 7408 static s32 igb_set_vf_vlan(struct igb_adapter *adapter, u32 vid, 7409 bool add, u32 vf) 7410 { 7411 int pf_id = adapter->vfs_allocated_count; 7412 struct e1000_hw *hw = &adapter->hw; 7413 int err; 7414 7415 /* If VLAN overlaps with one the PF is currently monitoring make 7416 * sure that we are able to allocate a VLVF entry. This may be 7417 * redundant but it guarantees PF will maintain visibility to 7418 * the VLAN. 7419 */ 7420 if (add && test_bit(vid, adapter->active_vlans)) { 7421 err = igb_vfta_set(hw, vid, pf_id, true, false); 7422 if (err) 7423 return err; 7424 } 7425 7426 err = igb_vfta_set(hw, vid, vf, add, false); 7427 7428 if (add && !err) 7429 return err; 7430 7431 /* If we failed to add the VF VLAN or we are removing the VF VLAN 7432 * we may need to drop the PF pool bit in order to allow us to free 7433 * up the VLVF resources. 7434 */ 7435 if (test_bit(vid, adapter->active_vlans) || 7436 (adapter->flags & IGB_FLAG_VLAN_PROMISC)) 7437 igb_update_pf_vlvf(adapter, vid); 7438 7439 return err; 7440 } 7441 7442 static void igb_set_vmvir(struct igb_adapter *adapter, u32 vid, u32 vf) 7443 { 7444 struct e1000_hw *hw = &adapter->hw; 7445 7446 if (vid) 7447 wr32(E1000_VMVIR(vf), (vid | E1000_VMVIR_VLANA_DEFAULT)); 7448 else 7449 wr32(E1000_VMVIR(vf), 0); 7450 } 7451 7452 static int igb_enable_port_vlan(struct igb_adapter *adapter, int vf, 7453 u16 vlan, u8 qos) 7454 { 7455 int err; 7456 7457 err = igb_set_vf_vlan(adapter, vlan, true, vf); 7458 if (err) 7459 return err; 7460 7461 igb_set_vmvir(adapter, vlan | (qos << VLAN_PRIO_SHIFT), vf); 7462 igb_set_vmolr(adapter, vf, !vlan); 7463 7464 /* revoke access to previous VLAN */ 7465 if (vlan != adapter->vf_data[vf].pf_vlan) 7466 igb_set_vf_vlan(adapter, adapter->vf_data[vf].pf_vlan, 7467 false, vf); 7468 7469 adapter->vf_data[vf].pf_vlan = vlan; 7470 adapter->vf_data[vf].pf_qos = qos; 7471 igb_set_vf_vlan_strip(adapter, vf, true); 7472 dev_info(&adapter->pdev->dev, 7473 "Setting VLAN %d, QOS 0x%x on VF %d\n", vlan, qos, vf); 7474 if (test_bit(__IGB_DOWN, &adapter->state)) { 7475 dev_warn(&adapter->pdev->dev, 7476 "The VF VLAN has been set, but the PF device is not up.\n"); 7477 dev_warn(&adapter->pdev->dev, 7478 "Bring the PF device up before attempting to use the VF device.\n"); 7479 } 7480 7481 return err; 7482 } 7483 7484 static int igb_disable_port_vlan(struct igb_adapter *adapter, int vf) 7485 { 7486 /* Restore tagless access via VLAN 0 */ 7487 igb_set_vf_vlan(adapter, 0, true, vf); 7488 7489 igb_set_vmvir(adapter, 0, vf); 7490 igb_set_vmolr(adapter, vf, true); 7491 7492 /* Remove any PF assigned VLAN */ 7493 if (adapter->vf_data[vf].pf_vlan) 7494 igb_set_vf_vlan(adapter, adapter->vf_data[vf].pf_vlan, 7495 false, vf); 7496 7497 adapter->vf_data[vf].pf_vlan = 0; 7498 adapter->vf_data[vf].pf_qos = 0; 7499 igb_set_vf_vlan_strip(adapter, vf, false); 7500 7501 return 0; 7502 } 7503 7504 static int igb_ndo_set_vf_vlan(struct net_device *netdev, int vf, 7505 u16 vlan, u8 qos, __be16 vlan_proto) 7506 { 7507 struct igb_adapter *adapter = netdev_priv(netdev); 7508 7509 if ((vf >= adapter->vfs_allocated_count) || (vlan > 4095) || (qos > 7)) 7510 return -EINVAL; 7511 7512 if (vlan_proto != htons(ETH_P_8021Q)) 7513 return -EPROTONOSUPPORT; 7514 7515 return (vlan || qos) ? igb_enable_port_vlan(adapter, vf, vlan, qos) : 7516 igb_disable_port_vlan(adapter, vf); 7517 } 7518 7519 static int igb_set_vf_vlan_msg(struct igb_adapter *adapter, u32 *msgbuf, u32 vf) 7520 { 7521 int add = FIELD_GET(E1000_VT_MSGINFO_MASK, msgbuf[0]); 7522 int vid = (msgbuf[1] & E1000_VLVF_VLANID_MASK); 7523 int ret; 7524 7525 if (adapter->vf_data[vf].pf_vlan) 7526 return -1; 7527 7528 /* VLAN 0 is a special case, don't allow it to be removed */ 7529 if (!vid && !add) 7530 return 0; 7531 7532 ret = igb_set_vf_vlan(adapter, vid, !!add, vf); 7533 if (!ret) 7534 igb_set_vf_vlan_strip(adapter, vf, !!vid); 7535 return ret; 7536 } 7537 7538 static inline void igb_vf_reset(struct igb_adapter *adapter, u32 vf) 7539 { 7540 struct vf_data_storage *vf_data = &adapter->vf_data[vf]; 7541 7542 /* clear flags - except flag that indicates PF has set the MAC */ 7543 vf_data->flags &= IGB_VF_FLAG_PF_SET_MAC; 7544 vf_data->last_nack = jiffies; 7545 7546 /* reset vlans for device */ 7547 igb_clear_vf_vfta(adapter, vf); 7548 igb_set_vf_vlan(adapter, vf_data->pf_vlan, true, vf); 7549 igb_set_vmvir(adapter, vf_data->pf_vlan | 7550 (vf_data->pf_qos << VLAN_PRIO_SHIFT), vf); 7551 igb_set_vmolr(adapter, vf, !vf_data->pf_vlan); 7552 igb_set_vf_vlan_strip(adapter, vf, !!(vf_data->pf_vlan)); 7553 7554 /* reset multicast table array for vf */ 7555 adapter->vf_data[vf].num_vf_mc_hashes = 0; 7556 7557 /* Flush and reset the mta with the new values */ 7558 igb_set_rx_mode(adapter->netdev); 7559 } 7560 7561 static void igb_vf_reset_event(struct igb_adapter *adapter, u32 vf) 7562 { 7563 unsigned char *vf_mac = adapter->vf_data[vf].vf_mac_addresses; 7564 7565 /* clear mac address as we were hotplug removed/added */ 7566 if (!(adapter->vf_data[vf].flags & IGB_VF_FLAG_PF_SET_MAC)) 7567 eth_zero_addr(vf_mac); 7568 7569 /* process remaining reset events */ 7570 igb_vf_reset(adapter, vf); 7571 } 7572 7573 static void igb_vf_reset_msg(struct igb_adapter *adapter, u32 vf) 7574 { 7575 struct e1000_hw *hw = &adapter->hw; 7576 unsigned char *vf_mac = adapter->vf_data[vf].vf_mac_addresses; 7577 u32 reg, msgbuf[3] = {}; 7578 u8 *addr = (u8 *)(&msgbuf[1]); 7579 7580 /* process all the same items cleared in a function level reset */ 7581 igb_vf_reset(adapter, vf); 7582 7583 /* set vf mac address */ 7584 igb_set_vf_mac(adapter, vf, vf_mac); 7585 7586 /* enable transmit and receive for vf */ 7587 reg = rd32(E1000_VFTE); 7588 wr32(E1000_VFTE, reg | BIT(vf)); 7589 reg = rd32(E1000_VFRE); 7590 wr32(E1000_VFRE, reg | BIT(vf)); 7591 7592 adapter->vf_data[vf].flags |= IGB_VF_FLAG_CTS; 7593 7594 /* reply to reset with ack and vf mac address */ 7595 if (!is_zero_ether_addr(vf_mac)) { 7596 msgbuf[0] = E1000_VF_RESET | E1000_VT_MSGTYPE_ACK; 7597 memcpy(addr, vf_mac, ETH_ALEN); 7598 } else { 7599 msgbuf[0] = E1000_VF_RESET | E1000_VT_MSGTYPE_NACK; 7600 } 7601 igb_write_mbx(hw, msgbuf, 3, vf); 7602 } 7603 7604 static void igb_flush_mac_table(struct igb_adapter *adapter) 7605 { 7606 struct e1000_hw *hw = &adapter->hw; 7607 int i; 7608 7609 for (i = 0; i < hw->mac.rar_entry_count; i++) { 7610 adapter->mac_table[i].state &= ~IGB_MAC_STATE_IN_USE; 7611 eth_zero_addr(adapter->mac_table[i].addr); 7612 adapter->mac_table[i].queue = 0; 7613 igb_rar_set_index(adapter, i); 7614 } 7615 } 7616 7617 static int igb_available_rars(struct igb_adapter *adapter, u8 queue) 7618 { 7619 struct e1000_hw *hw = &adapter->hw; 7620 /* do not count rar entries reserved for VFs MAC addresses */ 7621 int rar_entries = hw->mac.rar_entry_count - 7622 adapter->vfs_allocated_count; 7623 int i, count = 0; 7624 7625 for (i = 0; i < rar_entries; i++) { 7626 /* do not count default entries */ 7627 if (adapter->mac_table[i].state & IGB_MAC_STATE_DEFAULT) 7628 continue; 7629 7630 /* do not count "in use" entries for different queues */ 7631 if ((adapter->mac_table[i].state & IGB_MAC_STATE_IN_USE) && 7632 (adapter->mac_table[i].queue != queue)) 7633 continue; 7634 7635 count++; 7636 } 7637 7638 return count; 7639 } 7640 7641 /* Set default MAC address for the PF in the first RAR entry */ 7642 static void igb_set_default_mac_filter(struct igb_adapter *adapter) 7643 { 7644 struct igb_mac_addr *mac_table = &adapter->mac_table[0]; 7645 7646 ether_addr_copy(mac_table->addr, adapter->hw.mac.addr); 7647 mac_table->queue = adapter->vfs_allocated_count; 7648 mac_table->state = IGB_MAC_STATE_DEFAULT | IGB_MAC_STATE_IN_USE; 7649 7650 igb_rar_set_index(adapter, 0); 7651 } 7652 7653 /* If the filter to be added and an already existing filter express 7654 * the same address and address type, it should be possible to only 7655 * override the other configurations, for example the queue to steer 7656 * traffic. 7657 */ 7658 static bool igb_mac_entry_can_be_used(const struct igb_mac_addr *entry, 7659 const u8 *addr, const u8 flags) 7660 { 7661 if (!(entry->state & IGB_MAC_STATE_IN_USE)) 7662 return true; 7663 7664 if ((entry->state & IGB_MAC_STATE_SRC_ADDR) != 7665 (flags & IGB_MAC_STATE_SRC_ADDR)) 7666 return false; 7667 7668 if (!ether_addr_equal(addr, entry->addr)) 7669 return false; 7670 7671 return true; 7672 } 7673 7674 /* Add a MAC filter for 'addr' directing matching traffic to 'queue', 7675 * 'flags' is used to indicate what kind of match is made, match is by 7676 * default for the destination address, if matching by source address 7677 * is desired the flag IGB_MAC_STATE_SRC_ADDR can be used. 7678 */ 7679 static int igb_add_mac_filter_flags(struct igb_adapter *adapter, 7680 const u8 *addr, const u8 queue, 7681 const u8 flags) 7682 { 7683 struct e1000_hw *hw = &adapter->hw; 7684 int rar_entries = hw->mac.rar_entry_count - 7685 adapter->vfs_allocated_count; 7686 int i; 7687 7688 if (is_zero_ether_addr(addr)) 7689 return -EINVAL; 7690 7691 /* Search for the first empty entry in the MAC table. 7692 * Do not touch entries at the end of the table reserved for the VF MAC 7693 * addresses. 7694 */ 7695 for (i = 0; i < rar_entries; i++) { 7696 if (!igb_mac_entry_can_be_used(&adapter->mac_table[i], 7697 addr, flags)) 7698 continue; 7699 7700 ether_addr_copy(adapter->mac_table[i].addr, addr); 7701 adapter->mac_table[i].queue = queue; 7702 adapter->mac_table[i].state |= IGB_MAC_STATE_IN_USE | flags; 7703 7704 igb_rar_set_index(adapter, i); 7705 return i; 7706 } 7707 7708 return -ENOSPC; 7709 } 7710 7711 static int igb_add_mac_filter(struct igb_adapter *adapter, const u8 *addr, 7712 const u8 queue) 7713 { 7714 return igb_add_mac_filter_flags(adapter, addr, queue, 0); 7715 } 7716 7717 /* Remove a MAC filter for 'addr' directing matching traffic to 7718 * 'queue', 'flags' is used to indicate what kind of match need to be 7719 * removed, match is by default for the destination address, if 7720 * matching by source address is to be removed the flag 7721 * IGB_MAC_STATE_SRC_ADDR can be used. 7722 */ 7723 static int igb_del_mac_filter_flags(struct igb_adapter *adapter, 7724 const u8 *addr, const u8 queue, 7725 const u8 flags) 7726 { 7727 struct e1000_hw *hw = &adapter->hw; 7728 int rar_entries = hw->mac.rar_entry_count - 7729 adapter->vfs_allocated_count; 7730 int i; 7731 7732 if (is_zero_ether_addr(addr)) 7733 return -EINVAL; 7734 7735 /* Search for matching entry in the MAC table based on given address 7736 * and queue. Do not touch entries at the end of the table reserved 7737 * for the VF MAC addresses. 7738 */ 7739 for (i = 0; i < rar_entries; i++) { 7740 if (!(adapter->mac_table[i].state & IGB_MAC_STATE_IN_USE)) 7741 continue; 7742 if ((adapter->mac_table[i].state & flags) != flags) 7743 continue; 7744 if (adapter->mac_table[i].queue != queue) 7745 continue; 7746 if (!ether_addr_equal(adapter->mac_table[i].addr, addr)) 7747 continue; 7748 7749 /* When a filter for the default address is "deleted", 7750 * we return it to its initial configuration 7751 */ 7752 if (adapter->mac_table[i].state & IGB_MAC_STATE_DEFAULT) { 7753 adapter->mac_table[i].state = 7754 IGB_MAC_STATE_DEFAULT | IGB_MAC_STATE_IN_USE; 7755 adapter->mac_table[i].queue = 7756 adapter->vfs_allocated_count; 7757 } else { 7758 adapter->mac_table[i].state = 0; 7759 adapter->mac_table[i].queue = 0; 7760 eth_zero_addr(adapter->mac_table[i].addr); 7761 } 7762 7763 igb_rar_set_index(adapter, i); 7764 return 0; 7765 } 7766 7767 return -ENOENT; 7768 } 7769 7770 static int igb_del_mac_filter(struct igb_adapter *adapter, const u8 *addr, 7771 const u8 queue) 7772 { 7773 return igb_del_mac_filter_flags(adapter, addr, queue, 0); 7774 } 7775 7776 int igb_add_mac_steering_filter(struct igb_adapter *adapter, 7777 const u8 *addr, u8 queue, u8 flags) 7778 { 7779 struct e1000_hw *hw = &adapter->hw; 7780 7781 /* In theory, this should be supported on 82575 as well, but 7782 * that part wasn't easily accessible during development. 7783 */ 7784 if (hw->mac.type != e1000_i210) 7785 return -EOPNOTSUPP; 7786 7787 return igb_add_mac_filter_flags(adapter, addr, queue, 7788 IGB_MAC_STATE_QUEUE_STEERING | flags); 7789 } 7790 7791 int igb_del_mac_steering_filter(struct igb_adapter *adapter, 7792 const u8 *addr, u8 queue, u8 flags) 7793 { 7794 return igb_del_mac_filter_flags(adapter, addr, queue, 7795 IGB_MAC_STATE_QUEUE_STEERING | flags); 7796 } 7797 7798 static int igb_uc_sync(struct net_device *netdev, const unsigned char *addr) 7799 { 7800 struct igb_adapter *adapter = netdev_priv(netdev); 7801 int ret; 7802 7803 ret = igb_add_mac_filter(adapter, addr, adapter->vfs_allocated_count); 7804 7805 return min_t(int, ret, 0); 7806 } 7807 7808 static int igb_uc_unsync(struct net_device *netdev, const unsigned char *addr) 7809 { 7810 struct igb_adapter *adapter = netdev_priv(netdev); 7811 7812 igb_del_mac_filter(adapter, addr, adapter->vfs_allocated_count); 7813 7814 return 0; 7815 } 7816 7817 static int igb_set_vf_mac_filter(struct igb_adapter *adapter, const int vf, 7818 const u32 info, const u8 *addr) 7819 { 7820 struct pci_dev *pdev = adapter->pdev; 7821 struct vf_data_storage *vf_data = &adapter->vf_data[vf]; 7822 struct vf_mac_filter *entry; 7823 bool found = false; 7824 int ret = 0; 7825 7826 if ((vf_data->flags & IGB_VF_FLAG_PF_SET_MAC) && 7827 !vf_data->trusted) { 7828 dev_warn(&pdev->dev, 7829 "VF %d requested MAC filter but is administratively denied\n", 7830 vf); 7831 return -EINVAL; 7832 } 7833 if (!is_valid_ether_addr(addr)) { 7834 dev_warn(&pdev->dev, 7835 "VF %d attempted to set invalid MAC filter\n", 7836 vf); 7837 return -EINVAL; 7838 } 7839 7840 switch (info) { 7841 case E1000_VF_MAC_FILTER_CLR: 7842 /* remove all unicast MAC filters related to the current VF */ 7843 list_for_each_entry(entry, &adapter->vf_macs.l, l) { 7844 if (entry->vf == vf) { 7845 entry->vf = -1; 7846 entry->free = true; 7847 igb_del_mac_filter(adapter, entry->vf_mac, vf); 7848 } 7849 } 7850 break; 7851 case E1000_VF_MAC_FILTER_ADD: 7852 /* try to find empty slot in the list */ 7853 list_for_each_entry(entry, &adapter->vf_macs.l, l) { 7854 if (entry->free) { 7855 found = true; 7856 break; 7857 } 7858 } 7859 7860 if (found) { 7861 entry->free = false; 7862 entry->vf = vf; 7863 ether_addr_copy(entry->vf_mac, addr); 7864 7865 ret = igb_add_mac_filter(adapter, addr, vf); 7866 ret = min_t(int, ret, 0); 7867 } else { 7868 ret = -ENOSPC; 7869 } 7870 7871 if (ret == -ENOSPC) 7872 dev_warn(&pdev->dev, 7873 "VF %d has requested MAC filter but there is no space for it\n", 7874 vf); 7875 break; 7876 default: 7877 ret = -EINVAL; 7878 break; 7879 } 7880 7881 return ret; 7882 } 7883 7884 static int igb_set_vf_mac_addr(struct igb_adapter *adapter, u32 *msg, int vf) 7885 { 7886 struct pci_dev *pdev = adapter->pdev; 7887 struct vf_data_storage *vf_data = &adapter->vf_data[vf]; 7888 u32 info = msg[0] & E1000_VT_MSGINFO_MASK; 7889 7890 /* The VF MAC Address is stored in a packed array of bytes 7891 * starting at the second 32 bit word of the msg array 7892 */ 7893 unsigned char *addr = (unsigned char *)&msg[1]; 7894 int ret = 0; 7895 7896 if (!info) { 7897 if ((vf_data->flags & IGB_VF_FLAG_PF_SET_MAC) && 7898 !vf_data->trusted) { 7899 dev_warn(&pdev->dev, 7900 "VF %d attempted to override administratively set MAC address\nReload the VF driver to resume operations\n", 7901 vf); 7902 return -EINVAL; 7903 } 7904 7905 if (!is_valid_ether_addr(addr)) { 7906 dev_warn(&pdev->dev, 7907 "VF %d attempted to set invalid MAC\n", 7908 vf); 7909 return -EINVAL; 7910 } 7911 7912 ret = igb_set_vf_mac(adapter, vf, addr); 7913 } else { 7914 ret = igb_set_vf_mac_filter(adapter, vf, info, addr); 7915 } 7916 7917 return ret; 7918 } 7919 7920 static void igb_rcv_ack_from_vf(struct igb_adapter *adapter, u32 vf) 7921 { 7922 struct e1000_hw *hw = &adapter->hw; 7923 struct vf_data_storage *vf_data = &adapter->vf_data[vf]; 7924 u32 msg = E1000_VT_MSGTYPE_NACK; 7925 7926 /* if device isn't clear to send it shouldn't be reading either */ 7927 if (!(vf_data->flags & IGB_VF_FLAG_CTS) && 7928 time_after(jiffies, vf_data->last_nack + (2 * HZ))) { 7929 igb_write_mbx(hw, &msg, 1, vf); 7930 vf_data->last_nack = jiffies; 7931 } 7932 } 7933 7934 static void igb_rcv_msg_from_vf(struct igb_adapter *adapter, u32 vf) 7935 { 7936 struct pci_dev *pdev = adapter->pdev; 7937 u32 msgbuf[E1000_VFMAILBOX_SIZE]; 7938 struct e1000_hw *hw = &adapter->hw; 7939 struct vf_data_storage *vf_data = &adapter->vf_data[vf]; 7940 s32 retval; 7941 7942 retval = igb_read_mbx(hw, msgbuf, E1000_VFMAILBOX_SIZE, vf, false); 7943 7944 if (retval) { 7945 /* if receive failed revoke VF CTS stats and restart init */ 7946 dev_err(&pdev->dev, "Error receiving message from VF\n"); 7947 vf_data->flags &= ~IGB_VF_FLAG_CTS; 7948 if (!time_after(jiffies, vf_data->last_nack + (2 * HZ))) 7949 goto unlock; 7950 goto out; 7951 } 7952 7953 /* this is a message we already processed, do nothing */ 7954 if (msgbuf[0] & (E1000_VT_MSGTYPE_ACK | E1000_VT_MSGTYPE_NACK)) 7955 goto unlock; 7956 7957 /* until the vf completes a reset it should not be 7958 * allowed to start any configuration. 7959 */ 7960 if (msgbuf[0] == E1000_VF_RESET) { 7961 /* unlocks mailbox */ 7962 igb_vf_reset_msg(adapter, vf); 7963 return; 7964 } 7965 7966 if (!(vf_data->flags & IGB_VF_FLAG_CTS)) { 7967 if (!time_after(jiffies, vf_data->last_nack + (2 * HZ))) 7968 goto unlock; 7969 retval = -1; 7970 goto out; 7971 } 7972 7973 switch ((msgbuf[0] & 0xFFFF)) { 7974 case E1000_VF_SET_MAC_ADDR: 7975 retval = igb_set_vf_mac_addr(adapter, msgbuf, vf); 7976 break; 7977 case E1000_VF_SET_PROMISC: 7978 retval = igb_set_vf_promisc(adapter, msgbuf, vf); 7979 break; 7980 case E1000_VF_SET_MULTICAST: 7981 retval = igb_set_vf_multicasts(adapter, msgbuf, vf); 7982 break; 7983 case E1000_VF_SET_LPE: 7984 retval = igb_set_vf_rlpml(adapter, msgbuf[1], vf); 7985 break; 7986 case E1000_VF_SET_VLAN: 7987 retval = -1; 7988 if (vf_data->pf_vlan) 7989 dev_warn(&pdev->dev, 7990 "VF %d attempted to override administratively set VLAN tag\nReload the VF driver to resume operations\n", 7991 vf); 7992 else 7993 retval = igb_set_vf_vlan_msg(adapter, msgbuf, vf); 7994 break; 7995 default: 7996 dev_err(&pdev->dev, "Unhandled Msg %08x\n", msgbuf[0]); 7997 retval = -1; 7998 break; 7999 } 8000 8001 msgbuf[0] |= E1000_VT_MSGTYPE_CTS; 8002 out: 8003 /* notify the VF of the results of what it sent us */ 8004 if (retval) 8005 msgbuf[0] |= E1000_VT_MSGTYPE_NACK; 8006 else 8007 msgbuf[0] |= E1000_VT_MSGTYPE_ACK; 8008 8009 /* unlocks mailbox */ 8010 igb_write_mbx(hw, msgbuf, 1, vf); 8011 return; 8012 8013 unlock: 8014 igb_unlock_mbx(hw, vf); 8015 } 8016 8017 static void igb_msg_task(struct igb_adapter *adapter) 8018 { 8019 struct e1000_hw *hw = &adapter->hw; 8020 unsigned long flags; 8021 u32 vf; 8022 8023 spin_lock_irqsave(&adapter->vfs_lock, flags); 8024 for (vf = 0; vf < adapter->vfs_allocated_count; vf++) { 8025 /* process any reset requests */ 8026 if (!igb_check_for_rst(hw, vf)) 8027 igb_vf_reset_event(adapter, vf); 8028 8029 /* process any messages pending */ 8030 if (!igb_check_for_msg(hw, vf)) 8031 igb_rcv_msg_from_vf(adapter, vf); 8032 8033 /* process any acks */ 8034 if (!igb_check_for_ack(hw, vf)) 8035 igb_rcv_ack_from_vf(adapter, vf); 8036 } 8037 spin_unlock_irqrestore(&adapter->vfs_lock, flags); 8038 } 8039 8040 /** 8041 * igb_set_uta - Set unicast filter table address 8042 * @adapter: board private structure 8043 * @set: boolean indicating if we are setting or clearing bits 8044 * 8045 * The unicast table address is a register array of 32-bit registers. 8046 * The table is meant to be used in a way similar to how the MTA is used 8047 * however due to certain limitations in the hardware it is necessary to 8048 * set all the hash bits to 1 and use the VMOLR ROPE bit as a promiscuous 8049 * enable bit to allow vlan tag stripping when promiscuous mode is enabled 8050 **/ 8051 static void igb_set_uta(struct igb_adapter *adapter, bool set) 8052 { 8053 struct e1000_hw *hw = &adapter->hw; 8054 u32 uta = set ? ~0 : 0; 8055 int i; 8056 8057 /* we only need to do this if VMDq is enabled */ 8058 if (!adapter->vfs_allocated_count) 8059 return; 8060 8061 for (i = hw->mac.uta_reg_count; i--;) 8062 array_wr32(E1000_UTA, i, uta); 8063 } 8064 8065 /** 8066 * igb_intr_msi - Interrupt Handler 8067 * @irq: interrupt number 8068 * @data: pointer to a network interface device structure 8069 **/ 8070 static irqreturn_t igb_intr_msi(int irq, void *data) 8071 { 8072 struct igb_adapter *adapter = data; 8073 struct igb_q_vector *q_vector = adapter->q_vector[0]; 8074 struct e1000_hw *hw = &adapter->hw; 8075 /* read ICR disables interrupts using IAM */ 8076 u32 icr = rd32(E1000_ICR); 8077 8078 igb_write_itr(q_vector); 8079 8080 if (icr & E1000_ICR_DRSTA) 8081 schedule_work(&adapter->reset_task); 8082 8083 if (icr & E1000_ICR_DOUTSYNC) { 8084 /* HW is reporting DMA is out of sync */ 8085 adapter->stats.doosync++; 8086 } 8087 8088 if (icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) { 8089 hw->mac.get_link_status = 1; 8090 if (!test_bit(__IGB_DOWN, &adapter->state)) 8091 mod_timer(&adapter->watchdog_timer, jiffies + 1); 8092 } 8093 8094 if (icr & E1000_ICR_TS) 8095 igb_tsync_interrupt(adapter); 8096 8097 napi_schedule(&q_vector->napi); 8098 8099 return IRQ_HANDLED; 8100 } 8101 8102 /** 8103 * igb_intr - Legacy Interrupt Handler 8104 * @irq: interrupt number 8105 * @data: pointer to a network interface device structure 8106 **/ 8107 static irqreturn_t igb_intr(int irq, void *data) 8108 { 8109 struct igb_adapter *adapter = data; 8110 struct igb_q_vector *q_vector = adapter->q_vector[0]; 8111 struct e1000_hw *hw = &adapter->hw; 8112 /* Interrupt Auto-Mask...upon reading ICR, interrupts are masked. No 8113 * need for the IMC write 8114 */ 8115 u32 icr = rd32(E1000_ICR); 8116 8117 /* IMS will not auto-mask if INT_ASSERTED is not set, and if it is 8118 * not set, then the adapter didn't send an interrupt 8119 */ 8120 if (!(icr & E1000_ICR_INT_ASSERTED)) 8121 return IRQ_NONE; 8122 8123 igb_write_itr(q_vector); 8124 8125 if (icr & E1000_ICR_DRSTA) 8126 schedule_work(&adapter->reset_task); 8127 8128 if (icr & E1000_ICR_DOUTSYNC) { 8129 /* HW is reporting DMA is out of sync */ 8130 adapter->stats.doosync++; 8131 } 8132 8133 if (icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) { 8134 hw->mac.get_link_status = 1; 8135 /* guard against interrupt when we're going down */ 8136 if (!test_bit(__IGB_DOWN, &adapter->state)) 8137 mod_timer(&adapter->watchdog_timer, jiffies + 1); 8138 } 8139 8140 if (icr & E1000_ICR_TS) 8141 igb_tsync_interrupt(adapter); 8142 8143 napi_schedule(&q_vector->napi); 8144 8145 return IRQ_HANDLED; 8146 } 8147 8148 static void igb_ring_irq_enable(struct igb_q_vector *q_vector) 8149 { 8150 struct igb_adapter *adapter = q_vector->adapter; 8151 struct e1000_hw *hw = &adapter->hw; 8152 8153 if ((q_vector->rx.ring && (adapter->rx_itr_setting & 3)) || 8154 (!q_vector->rx.ring && (adapter->tx_itr_setting & 3))) { 8155 if ((adapter->num_q_vectors == 1) && !adapter->vf_data) 8156 igb_set_itr(q_vector); 8157 else 8158 igb_update_ring_itr(q_vector); 8159 } 8160 8161 if (!test_bit(__IGB_DOWN, &adapter->state)) { 8162 if (adapter->flags & IGB_FLAG_HAS_MSIX) 8163 wr32(E1000_EIMS, q_vector->eims_value); 8164 else 8165 igb_irq_enable(adapter); 8166 } 8167 } 8168 8169 /** 8170 * igb_poll - NAPI Rx polling callback 8171 * @napi: napi polling structure 8172 * @budget: count of how many packets we should handle 8173 **/ 8174 static int igb_poll(struct napi_struct *napi, int budget) 8175 { 8176 struct igb_q_vector *q_vector = container_of(napi, 8177 struct igb_q_vector, 8178 napi); 8179 bool clean_complete = true; 8180 int work_done = 0; 8181 8182 #ifdef CONFIG_IGB_DCA 8183 if (q_vector->adapter->flags & IGB_FLAG_DCA_ENABLED) 8184 igb_update_dca(q_vector); 8185 #endif 8186 if (q_vector->tx.ring) 8187 clean_complete = igb_clean_tx_irq(q_vector, budget); 8188 8189 if (q_vector->rx.ring) { 8190 int cleaned = igb_clean_rx_irq(q_vector, budget); 8191 8192 work_done += cleaned; 8193 if (cleaned >= budget) 8194 clean_complete = false; 8195 } 8196 8197 /* If all work not completed, return budget and keep polling */ 8198 if (!clean_complete) 8199 return budget; 8200 8201 /* Exit the polling mode, but don't re-enable interrupts if stack might 8202 * poll us due to busy-polling 8203 */ 8204 if (likely(napi_complete_done(napi, work_done))) 8205 igb_ring_irq_enable(q_vector); 8206 8207 return work_done; 8208 } 8209 8210 /** 8211 * igb_clean_tx_irq - Reclaim resources after transmit completes 8212 * @q_vector: pointer to q_vector containing needed info 8213 * @napi_budget: Used to determine if we are in netpoll 8214 * 8215 * returns true if ring is completely cleaned 8216 **/ 8217 static bool igb_clean_tx_irq(struct igb_q_vector *q_vector, int napi_budget) 8218 { 8219 struct igb_adapter *adapter = q_vector->adapter; 8220 struct igb_ring *tx_ring = q_vector->tx.ring; 8221 struct igb_tx_buffer *tx_buffer; 8222 union e1000_adv_tx_desc *tx_desc; 8223 unsigned int total_bytes = 0, total_packets = 0; 8224 unsigned int budget = q_vector->tx.work_limit; 8225 unsigned int i = tx_ring->next_to_clean; 8226 8227 if (test_bit(__IGB_DOWN, &adapter->state)) 8228 return true; 8229 8230 tx_buffer = &tx_ring->tx_buffer_info[i]; 8231 tx_desc = IGB_TX_DESC(tx_ring, i); 8232 i -= tx_ring->count; 8233 8234 do { 8235 union e1000_adv_tx_desc *eop_desc = tx_buffer->next_to_watch; 8236 8237 /* if next_to_watch is not set then there is no work pending */ 8238 if (!eop_desc) 8239 break; 8240 8241 /* prevent any other reads prior to eop_desc */ 8242 smp_rmb(); 8243 8244 /* if DD is not set pending work has not been completed */ 8245 if (!(eop_desc->wb.status & cpu_to_le32(E1000_TXD_STAT_DD))) 8246 break; 8247 8248 /* clear next_to_watch to prevent false hangs */ 8249 tx_buffer->next_to_watch = NULL; 8250 8251 /* update the statistics for this packet */ 8252 total_bytes += tx_buffer->bytecount; 8253 total_packets += tx_buffer->gso_segs; 8254 8255 /* free the skb */ 8256 if (tx_buffer->type == IGB_TYPE_SKB) 8257 napi_consume_skb(tx_buffer->skb, napi_budget); 8258 else 8259 xdp_return_frame(tx_buffer->xdpf); 8260 8261 /* unmap skb header data */ 8262 dma_unmap_single(tx_ring->dev, 8263 dma_unmap_addr(tx_buffer, dma), 8264 dma_unmap_len(tx_buffer, len), 8265 DMA_TO_DEVICE); 8266 8267 /* clear tx_buffer data */ 8268 dma_unmap_len_set(tx_buffer, len, 0); 8269 8270 /* clear last DMA location and unmap remaining buffers */ 8271 while (tx_desc != eop_desc) { 8272 tx_buffer++; 8273 tx_desc++; 8274 i++; 8275 if (unlikely(!i)) { 8276 i -= tx_ring->count; 8277 tx_buffer = tx_ring->tx_buffer_info; 8278 tx_desc = IGB_TX_DESC(tx_ring, 0); 8279 } 8280 8281 /* unmap any remaining paged data */ 8282 if (dma_unmap_len(tx_buffer, len)) { 8283 dma_unmap_page(tx_ring->dev, 8284 dma_unmap_addr(tx_buffer, dma), 8285 dma_unmap_len(tx_buffer, len), 8286 DMA_TO_DEVICE); 8287 dma_unmap_len_set(tx_buffer, len, 0); 8288 } 8289 } 8290 8291 /* move us one more past the eop_desc for start of next pkt */ 8292 tx_buffer++; 8293 tx_desc++; 8294 i++; 8295 if (unlikely(!i)) { 8296 i -= tx_ring->count; 8297 tx_buffer = tx_ring->tx_buffer_info; 8298 tx_desc = IGB_TX_DESC(tx_ring, 0); 8299 } 8300 8301 /* issue prefetch for next Tx descriptor */ 8302 prefetch(tx_desc); 8303 8304 /* update budget accounting */ 8305 budget--; 8306 } while (likely(budget)); 8307 8308 netdev_tx_completed_queue(txring_txq(tx_ring), 8309 total_packets, total_bytes); 8310 i += tx_ring->count; 8311 tx_ring->next_to_clean = i; 8312 u64_stats_update_begin(&tx_ring->tx_syncp); 8313 tx_ring->tx_stats.bytes += total_bytes; 8314 tx_ring->tx_stats.packets += total_packets; 8315 u64_stats_update_end(&tx_ring->tx_syncp); 8316 q_vector->tx.total_bytes += total_bytes; 8317 q_vector->tx.total_packets += total_packets; 8318 8319 if (test_bit(IGB_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags)) { 8320 struct e1000_hw *hw = &adapter->hw; 8321 8322 /* Detect a transmit hang in hardware, this serializes the 8323 * check with the clearing of time_stamp and movement of i 8324 */ 8325 clear_bit(IGB_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags); 8326 if (tx_buffer->next_to_watch && 8327 time_after(jiffies, tx_buffer->time_stamp + 8328 (adapter->tx_timeout_factor * HZ)) && 8329 !(rd32(E1000_STATUS) & E1000_STATUS_TXOFF)) { 8330 8331 /* detected Tx unit hang */ 8332 dev_err(tx_ring->dev, 8333 "Detected Tx Unit Hang\n" 8334 " Tx Queue <%d>\n" 8335 " TDH <%x>\n" 8336 " TDT <%x>\n" 8337 " next_to_use <%x>\n" 8338 " next_to_clean <%x>\n" 8339 "buffer_info[next_to_clean]\n" 8340 " time_stamp <%lx>\n" 8341 " next_to_watch <%p>\n" 8342 " jiffies <%lx>\n" 8343 " desc.status <%x>\n", 8344 tx_ring->queue_index, 8345 rd32(E1000_TDH(tx_ring->reg_idx)), 8346 readl(tx_ring->tail), 8347 tx_ring->next_to_use, 8348 tx_ring->next_to_clean, 8349 tx_buffer->time_stamp, 8350 tx_buffer->next_to_watch, 8351 jiffies, 8352 tx_buffer->next_to_watch->wb.status); 8353 netif_stop_subqueue(tx_ring->netdev, 8354 tx_ring->queue_index); 8355 8356 /* we are about to reset, no point in enabling stuff */ 8357 return true; 8358 } 8359 } 8360 8361 #define TX_WAKE_THRESHOLD (DESC_NEEDED * 2) 8362 if (unlikely(total_packets && 8363 netif_carrier_ok(tx_ring->netdev) && 8364 igb_desc_unused(tx_ring) >= TX_WAKE_THRESHOLD)) { 8365 /* Make sure that anybody stopping the queue after this 8366 * sees the new next_to_clean. 8367 */ 8368 smp_mb(); 8369 if (__netif_subqueue_stopped(tx_ring->netdev, 8370 tx_ring->queue_index) && 8371 !(test_bit(__IGB_DOWN, &adapter->state))) { 8372 netif_wake_subqueue(tx_ring->netdev, 8373 tx_ring->queue_index); 8374 8375 u64_stats_update_begin(&tx_ring->tx_syncp); 8376 tx_ring->tx_stats.restart_queue++; 8377 u64_stats_update_end(&tx_ring->tx_syncp); 8378 } 8379 } 8380 8381 return !!budget; 8382 } 8383 8384 /** 8385 * igb_reuse_rx_page - page flip buffer and store it back on the ring 8386 * @rx_ring: rx descriptor ring to store buffers on 8387 * @old_buff: donor buffer to have page reused 8388 * 8389 * Synchronizes page for reuse by the adapter 8390 **/ 8391 static void igb_reuse_rx_page(struct igb_ring *rx_ring, 8392 struct igb_rx_buffer *old_buff) 8393 { 8394 struct igb_rx_buffer *new_buff; 8395 u16 nta = rx_ring->next_to_alloc; 8396 8397 new_buff = &rx_ring->rx_buffer_info[nta]; 8398 8399 /* update, and store next to alloc */ 8400 nta++; 8401 rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0; 8402 8403 /* Transfer page from old buffer to new buffer. 8404 * Move each member individually to avoid possible store 8405 * forwarding stalls. 8406 */ 8407 new_buff->dma = old_buff->dma; 8408 new_buff->page = old_buff->page; 8409 new_buff->page_offset = old_buff->page_offset; 8410 new_buff->pagecnt_bias = old_buff->pagecnt_bias; 8411 } 8412 8413 static bool igb_can_reuse_rx_page(struct igb_rx_buffer *rx_buffer, 8414 int rx_buf_pgcnt) 8415 { 8416 unsigned int pagecnt_bias = rx_buffer->pagecnt_bias; 8417 struct page *page = rx_buffer->page; 8418 8419 /* avoid re-using remote and pfmemalloc pages */ 8420 if (!dev_page_is_reusable(page)) 8421 return false; 8422 8423 #if (PAGE_SIZE < 8192) 8424 /* if we are only owner of page we can reuse it */ 8425 if (unlikely((rx_buf_pgcnt - pagecnt_bias) > 1)) 8426 return false; 8427 #else 8428 #define IGB_LAST_OFFSET \ 8429 (SKB_WITH_OVERHEAD(PAGE_SIZE) - IGB_RXBUFFER_2048) 8430 8431 if (rx_buffer->page_offset > IGB_LAST_OFFSET) 8432 return false; 8433 #endif 8434 8435 /* If we have drained the page fragment pool we need to update 8436 * the pagecnt_bias and page count so that we fully restock the 8437 * number of references the driver holds. 8438 */ 8439 if (unlikely(pagecnt_bias == 1)) { 8440 page_ref_add(page, USHRT_MAX - 1); 8441 rx_buffer->pagecnt_bias = USHRT_MAX; 8442 } 8443 8444 return true; 8445 } 8446 8447 /** 8448 * igb_add_rx_frag - Add contents of Rx buffer to sk_buff 8449 * @rx_ring: rx descriptor ring to transact packets on 8450 * @rx_buffer: buffer containing page to add 8451 * @skb: sk_buff to place the data into 8452 * @size: size of buffer to be added 8453 * 8454 * This function will add the data contained in rx_buffer->page to the skb. 8455 **/ 8456 static void igb_add_rx_frag(struct igb_ring *rx_ring, 8457 struct igb_rx_buffer *rx_buffer, 8458 struct sk_buff *skb, 8459 unsigned int size) 8460 { 8461 #if (PAGE_SIZE < 8192) 8462 unsigned int truesize = igb_rx_pg_size(rx_ring) / 2; 8463 #else 8464 unsigned int truesize = ring_uses_build_skb(rx_ring) ? 8465 SKB_DATA_ALIGN(IGB_SKB_PAD + size) : 8466 SKB_DATA_ALIGN(size); 8467 #endif 8468 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, rx_buffer->page, 8469 rx_buffer->page_offset, size, truesize); 8470 #if (PAGE_SIZE < 8192) 8471 rx_buffer->page_offset ^= truesize; 8472 #else 8473 rx_buffer->page_offset += truesize; 8474 #endif 8475 } 8476 8477 static struct sk_buff *igb_construct_skb(struct igb_ring *rx_ring, 8478 struct igb_rx_buffer *rx_buffer, 8479 struct xdp_buff *xdp, 8480 ktime_t timestamp) 8481 { 8482 #if (PAGE_SIZE < 8192) 8483 unsigned int truesize = igb_rx_pg_size(rx_ring) / 2; 8484 #else 8485 unsigned int truesize = SKB_DATA_ALIGN(xdp->data_end - 8486 xdp->data_hard_start); 8487 #endif 8488 unsigned int size = xdp->data_end - xdp->data; 8489 unsigned int headlen; 8490 struct sk_buff *skb; 8491 8492 /* prefetch first cache line of first page */ 8493 net_prefetch(xdp->data); 8494 8495 /* allocate a skb to store the frags */ 8496 skb = napi_alloc_skb(&rx_ring->q_vector->napi, IGB_RX_HDR_LEN); 8497 if (unlikely(!skb)) 8498 return NULL; 8499 8500 if (timestamp) 8501 skb_hwtstamps(skb)->hwtstamp = timestamp; 8502 8503 /* Determine available headroom for copy */ 8504 headlen = size; 8505 if (headlen > IGB_RX_HDR_LEN) 8506 headlen = eth_get_headlen(skb->dev, xdp->data, IGB_RX_HDR_LEN); 8507 8508 /* align pull length to size of long to optimize memcpy performance */ 8509 memcpy(__skb_put(skb, headlen), xdp->data, ALIGN(headlen, sizeof(long))); 8510 8511 /* update all of the pointers */ 8512 size -= headlen; 8513 if (size) { 8514 skb_add_rx_frag(skb, 0, rx_buffer->page, 8515 (xdp->data + headlen) - page_address(rx_buffer->page), 8516 size, truesize); 8517 #if (PAGE_SIZE < 8192) 8518 rx_buffer->page_offset ^= truesize; 8519 #else 8520 rx_buffer->page_offset += truesize; 8521 #endif 8522 } else { 8523 rx_buffer->pagecnt_bias++; 8524 } 8525 8526 return skb; 8527 } 8528 8529 static struct sk_buff *igb_build_skb(struct igb_ring *rx_ring, 8530 struct igb_rx_buffer *rx_buffer, 8531 struct xdp_buff *xdp, 8532 ktime_t timestamp) 8533 { 8534 #if (PAGE_SIZE < 8192) 8535 unsigned int truesize = igb_rx_pg_size(rx_ring) / 2; 8536 #else 8537 unsigned int truesize = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) + 8538 SKB_DATA_ALIGN(xdp->data_end - 8539 xdp->data_hard_start); 8540 #endif 8541 unsigned int metasize = xdp->data - xdp->data_meta; 8542 struct sk_buff *skb; 8543 8544 /* prefetch first cache line of first page */ 8545 net_prefetch(xdp->data_meta); 8546 8547 /* build an skb around the page buffer */ 8548 skb = napi_build_skb(xdp->data_hard_start, truesize); 8549 if (unlikely(!skb)) 8550 return NULL; 8551 8552 /* update pointers within the skb to store the data */ 8553 skb_reserve(skb, xdp->data - xdp->data_hard_start); 8554 __skb_put(skb, xdp->data_end - xdp->data); 8555 8556 if (metasize) 8557 skb_metadata_set(skb, metasize); 8558 8559 if (timestamp) 8560 skb_hwtstamps(skb)->hwtstamp = timestamp; 8561 8562 /* update buffer offset */ 8563 #if (PAGE_SIZE < 8192) 8564 rx_buffer->page_offset ^= truesize; 8565 #else 8566 rx_buffer->page_offset += truesize; 8567 #endif 8568 8569 return skb; 8570 } 8571 8572 static struct sk_buff *igb_run_xdp(struct igb_adapter *adapter, 8573 struct igb_ring *rx_ring, 8574 struct xdp_buff *xdp) 8575 { 8576 int err, result = IGB_XDP_PASS; 8577 struct bpf_prog *xdp_prog; 8578 u32 act; 8579 8580 xdp_prog = READ_ONCE(rx_ring->xdp_prog); 8581 8582 if (!xdp_prog) 8583 goto xdp_out; 8584 8585 prefetchw(xdp->data_hard_start); /* xdp_frame write */ 8586 8587 act = bpf_prog_run_xdp(xdp_prog, xdp); 8588 switch (act) { 8589 case XDP_PASS: 8590 break; 8591 case XDP_TX: 8592 result = igb_xdp_xmit_back(adapter, xdp); 8593 if (result == IGB_XDP_CONSUMED) 8594 goto out_failure; 8595 break; 8596 case XDP_REDIRECT: 8597 err = xdp_do_redirect(adapter->netdev, xdp, xdp_prog); 8598 if (err) 8599 goto out_failure; 8600 result = IGB_XDP_REDIR; 8601 break; 8602 default: 8603 bpf_warn_invalid_xdp_action(adapter->netdev, xdp_prog, act); 8604 fallthrough; 8605 case XDP_ABORTED: 8606 out_failure: 8607 trace_xdp_exception(rx_ring->netdev, xdp_prog, act); 8608 fallthrough; 8609 case XDP_DROP: 8610 result = IGB_XDP_CONSUMED; 8611 break; 8612 } 8613 xdp_out: 8614 return ERR_PTR(-result); 8615 } 8616 8617 static unsigned int igb_rx_frame_truesize(struct igb_ring *rx_ring, 8618 unsigned int size) 8619 { 8620 unsigned int truesize; 8621 8622 #if (PAGE_SIZE < 8192) 8623 truesize = igb_rx_pg_size(rx_ring) / 2; /* Must be power-of-2 */ 8624 #else 8625 truesize = ring_uses_build_skb(rx_ring) ? 8626 SKB_DATA_ALIGN(IGB_SKB_PAD + size) + 8627 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) : 8628 SKB_DATA_ALIGN(size); 8629 #endif 8630 return truesize; 8631 } 8632 8633 static void igb_rx_buffer_flip(struct igb_ring *rx_ring, 8634 struct igb_rx_buffer *rx_buffer, 8635 unsigned int size) 8636 { 8637 unsigned int truesize = igb_rx_frame_truesize(rx_ring, size); 8638 #if (PAGE_SIZE < 8192) 8639 rx_buffer->page_offset ^= truesize; 8640 #else 8641 rx_buffer->page_offset += truesize; 8642 #endif 8643 } 8644 8645 static inline void igb_rx_checksum(struct igb_ring *ring, 8646 union e1000_adv_rx_desc *rx_desc, 8647 struct sk_buff *skb) 8648 { 8649 skb_checksum_none_assert(skb); 8650 8651 /* Ignore Checksum bit is set */ 8652 if (igb_test_staterr(rx_desc, E1000_RXD_STAT_IXSM)) 8653 return; 8654 8655 /* Rx checksum disabled via ethtool */ 8656 if (!(ring->netdev->features & NETIF_F_RXCSUM)) 8657 return; 8658 8659 /* TCP/UDP checksum error bit is set */ 8660 if (igb_test_staterr(rx_desc, 8661 E1000_RXDEXT_STATERR_TCPE | 8662 E1000_RXDEXT_STATERR_IPE)) { 8663 /* work around errata with sctp packets where the TCPE aka 8664 * L4E bit is set incorrectly on 64 byte (60 byte w/o crc) 8665 * packets, (aka let the stack check the crc32c) 8666 */ 8667 if (!((skb->len == 60) && 8668 test_bit(IGB_RING_FLAG_RX_SCTP_CSUM, &ring->flags))) { 8669 u64_stats_update_begin(&ring->rx_syncp); 8670 ring->rx_stats.csum_err++; 8671 u64_stats_update_end(&ring->rx_syncp); 8672 } 8673 /* let the stack verify checksum errors */ 8674 return; 8675 } 8676 /* It must be a TCP or UDP packet with a valid checksum */ 8677 if (igb_test_staterr(rx_desc, E1000_RXD_STAT_TCPCS | 8678 E1000_RXD_STAT_UDPCS)) 8679 skb->ip_summed = CHECKSUM_UNNECESSARY; 8680 8681 dev_dbg(ring->dev, "cksum success: bits %08X\n", 8682 le32_to_cpu(rx_desc->wb.upper.status_error)); 8683 } 8684 8685 static inline void igb_rx_hash(struct igb_ring *ring, 8686 union e1000_adv_rx_desc *rx_desc, 8687 struct sk_buff *skb) 8688 { 8689 if (ring->netdev->features & NETIF_F_RXHASH) 8690 skb_set_hash(skb, 8691 le32_to_cpu(rx_desc->wb.lower.hi_dword.rss), 8692 PKT_HASH_TYPE_L3); 8693 } 8694 8695 /** 8696 * igb_is_non_eop - process handling of non-EOP buffers 8697 * @rx_ring: Rx ring being processed 8698 * @rx_desc: Rx descriptor for current buffer 8699 * 8700 * This function updates next to clean. If the buffer is an EOP buffer 8701 * this function exits returning false, otherwise it will place the 8702 * sk_buff in the next buffer to be chained and return true indicating 8703 * that this is in fact a non-EOP buffer. 8704 **/ 8705 static bool igb_is_non_eop(struct igb_ring *rx_ring, 8706 union e1000_adv_rx_desc *rx_desc) 8707 { 8708 u32 ntc = rx_ring->next_to_clean + 1; 8709 8710 /* fetch, update, and store next to clean */ 8711 ntc = (ntc < rx_ring->count) ? ntc : 0; 8712 rx_ring->next_to_clean = ntc; 8713 8714 prefetch(IGB_RX_DESC(rx_ring, ntc)); 8715 8716 if (likely(igb_test_staterr(rx_desc, E1000_RXD_STAT_EOP))) 8717 return false; 8718 8719 return true; 8720 } 8721 8722 /** 8723 * igb_cleanup_headers - Correct corrupted or empty headers 8724 * @rx_ring: rx descriptor ring packet is being transacted on 8725 * @rx_desc: pointer to the EOP Rx descriptor 8726 * @skb: pointer to current skb being fixed 8727 * 8728 * Address the case where we are pulling data in on pages only 8729 * and as such no data is present in the skb header. 8730 * 8731 * In addition if skb is not at least 60 bytes we need to pad it so that 8732 * it is large enough to qualify as a valid Ethernet frame. 8733 * 8734 * Returns true if an error was encountered and skb was freed. 8735 **/ 8736 static bool igb_cleanup_headers(struct igb_ring *rx_ring, 8737 union e1000_adv_rx_desc *rx_desc, 8738 struct sk_buff *skb) 8739 { 8740 /* XDP packets use error pointer so abort at this point */ 8741 if (IS_ERR(skb)) 8742 return true; 8743 8744 if (unlikely((igb_test_staterr(rx_desc, 8745 E1000_RXDEXT_ERR_FRAME_ERR_MASK)))) { 8746 struct net_device *netdev = rx_ring->netdev; 8747 if (!(netdev->features & NETIF_F_RXALL)) { 8748 dev_kfree_skb_any(skb); 8749 return true; 8750 } 8751 } 8752 8753 /* if eth_skb_pad returns an error the skb was freed */ 8754 if (eth_skb_pad(skb)) 8755 return true; 8756 8757 return false; 8758 } 8759 8760 /** 8761 * igb_process_skb_fields - Populate skb header fields from Rx descriptor 8762 * @rx_ring: rx descriptor ring packet is being transacted on 8763 * @rx_desc: pointer to the EOP Rx descriptor 8764 * @skb: pointer to current skb being populated 8765 * 8766 * This function checks the ring, descriptor, and packet information in 8767 * order to populate the hash, checksum, VLAN, timestamp, protocol, and 8768 * other fields within the skb. 8769 **/ 8770 static void igb_process_skb_fields(struct igb_ring *rx_ring, 8771 union e1000_adv_rx_desc *rx_desc, 8772 struct sk_buff *skb) 8773 { 8774 struct net_device *dev = rx_ring->netdev; 8775 8776 igb_rx_hash(rx_ring, rx_desc, skb); 8777 8778 igb_rx_checksum(rx_ring, rx_desc, skb); 8779 8780 if (igb_test_staterr(rx_desc, E1000_RXDADV_STAT_TS) && 8781 !igb_test_staterr(rx_desc, E1000_RXDADV_STAT_TSIP)) 8782 igb_ptp_rx_rgtstamp(rx_ring->q_vector, skb); 8783 8784 if ((dev->features & NETIF_F_HW_VLAN_CTAG_RX) && 8785 igb_test_staterr(rx_desc, E1000_RXD_STAT_VP)) { 8786 u16 vid; 8787 8788 if (igb_test_staterr(rx_desc, E1000_RXDEXT_STATERR_LB) && 8789 test_bit(IGB_RING_FLAG_RX_LB_VLAN_BSWAP, &rx_ring->flags)) 8790 vid = be16_to_cpu((__force __be16)rx_desc->wb.upper.vlan); 8791 else 8792 vid = le16_to_cpu(rx_desc->wb.upper.vlan); 8793 8794 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vid); 8795 } 8796 8797 skb_record_rx_queue(skb, rx_ring->queue_index); 8798 8799 skb->protocol = eth_type_trans(skb, rx_ring->netdev); 8800 } 8801 8802 static unsigned int igb_rx_offset(struct igb_ring *rx_ring) 8803 { 8804 return ring_uses_build_skb(rx_ring) ? IGB_SKB_PAD : 0; 8805 } 8806 8807 static struct igb_rx_buffer *igb_get_rx_buffer(struct igb_ring *rx_ring, 8808 const unsigned int size, int *rx_buf_pgcnt) 8809 { 8810 struct igb_rx_buffer *rx_buffer; 8811 8812 rx_buffer = &rx_ring->rx_buffer_info[rx_ring->next_to_clean]; 8813 *rx_buf_pgcnt = 8814 #if (PAGE_SIZE < 8192) 8815 page_count(rx_buffer->page); 8816 #else 8817 0; 8818 #endif 8819 prefetchw(rx_buffer->page); 8820 8821 /* we are reusing so sync this buffer for CPU use */ 8822 dma_sync_single_range_for_cpu(rx_ring->dev, 8823 rx_buffer->dma, 8824 rx_buffer->page_offset, 8825 size, 8826 DMA_FROM_DEVICE); 8827 8828 rx_buffer->pagecnt_bias--; 8829 8830 return rx_buffer; 8831 } 8832 8833 static void igb_put_rx_buffer(struct igb_ring *rx_ring, 8834 struct igb_rx_buffer *rx_buffer, int rx_buf_pgcnt) 8835 { 8836 if (igb_can_reuse_rx_page(rx_buffer, rx_buf_pgcnt)) { 8837 /* hand second half of page back to the ring */ 8838 igb_reuse_rx_page(rx_ring, rx_buffer); 8839 } else { 8840 /* We are not reusing the buffer so unmap it and free 8841 * any references we are holding to it 8842 */ 8843 dma_unmap_page_attrs(rx_ring->dev, rx_buffer->dma, 8844 igb_rx_pg_size(rx_ring), DMA_FROM_DEVICE, 8845 IGB_RX_DMA_ATTR); 8846 __page_frag_cache_drain(rx_buffer->page, 8847 rx_buffer->pagecnt_bias); 8848 } 8849 8850 /* clear contents of rx_buffer */ 8851 rx_buffer->page = NULL; 8852 } 8853 8854 static int igb_clean_rx_irq(struct igb_q_vector *q_vector, const int budget) 8855 { 8856 struct igb_adapter *adapter = q_vector->adapter; 8857 struct igb_ring *rx_ring = q_vector->rx.ring; 8858 struct sk_buff *skb = rx_ring->skb; 8859 unsigned int total_bytes = 0, total_packets = 0; 8860 u16 cleaned_count = igb_desc_unused(rx_ring); 8861 unsigned int xdp_xmit = 0; 8862 struct xdp_buff xdp; 8863 u32 frame_sz = 0; 8864 int rx_buf_pgcnt; 8865 8866 /* Frame size depend on rx_ring setup when PAGE_SIZE=4K */ 8867 #if (PAGE_SIZE < 8192) 8868 frame_sz = igb_rx_frame_truesize(rx_ring, 0); 8869 #endif 8870 xdp_init_buff(&xdp, frame_sz, &rx_ring->xdp_rxq); 8871 8872 while (likely(total_packets < budget)) { 8873 union e1000_adv_rx_desc *rx_desc; 8874 struct igb_rx_buffer *rx_buffer; 8875 ktime_t timestamp = 0; 8876 int pkt_offset = 0; 8877 unsigned int size; 8878 void *pktbuf; 8879 8880 /* return some buffers to hardware, one at a time is too slow */ 8881 if (cleaned_count >= IGB_RX_BUFFER_WRITE) { 8882 igb_alloc_rx_buffers(rx_ring, cleaned_count); 8883 cleaned_count = 0; 8884 } 8885 8886 rx_desc = IGB_RX_DESC(rx_ring, rx_ring->next_to_clean); 8887 size = le16_to_cpu(rx_desc->wb.upper.length); 8888 if (!size) 8889 break; 8890 8891 /* This memory barrier is needed to keep us from reading 8892 * any other fields out of the rx_desc until we know the 8893 * descriptor has been written back 8894 */ 8895 dma_rmb(); 8896 8897 rx_buffer = igb_get_rx_buffer(rx_ring, size, &rx_buf_pgcnt); 8898 pktbuf = page_address(rx_buffer->page) + rx_buffer->page_offset; 8899 8900 /* pull rx packet timestamp if available and valid */ 8901 if (igb_test_staterr(rx_desc, E1000_RXDADV_STAT_TSIP)) { 8902 int ts_hdr_len; 8903 8904 ts_hdr_len = igb_ptp_rx_pktstamp(rx_ring->q_vector, 8905 pktbuf, ×tamp); 8906 8907 pkt_offset += ts_hdr_len; 8908 size -= ts_hdr_len; 8909 } 8910 8911 /* retrieve a buffer from the ring */ 8912 if (!skb) { 8913 unsigned char *hard_start = pktbuf - igb_rx_offset(rx_ring); 8914 unsigned int offset = pkt_offset + igb_rx_offset(rx_ring); 8915 8916 xdp_prepare_buff(&xdp, hard_start, offset, size, true); 8917 xdp_buff_clear_frags_flag(&xdp); 8918 #if (PAGE_SIZE > 4096) 8919 /* At larger PAGE_SIZE, frame_sz depend on len size */ 8920 xdp.frame_sz = igb_rx_frame_truesize(rx_ring, size); 8921 #endif 8922 skb = igb_run_xdp(adapter, rx_ring, &xdp); 8923 } 8924 8925 if (IS_ERR(skb)) { 8926 unsigned int xdp_res = -PTR_ERR(skb); 8927 8928 if (xdp_res & (IGB_XDP_TX | IGB_XDP_REDIR)) { 8929 xdp_xmit |= xdp_res; 8930 igb_rx_buffer_flip(rx_ring, rx_buffer, size); 8931 } else { 8932 rx_buffer->pagecnt_bias++; 8933 } 8934 total_packets++; 8935 total_bytes += size; 8936 } else if (skb) 8937 igb_add_rx_frag(rx_ring, rx_buffer, skb, size); 8938 else if (ring_uses_build_skb(rx_ring)) 8939 skb = igb_build_skb(rx_ring, rx_buffer, &xdp, 8940 timestamp); 8941 else 8942 skb = igb_construct_skb(rx_ring, rx_buffer, 8943 &xdp, timestamp); 8944 8945 /* exit if we failed to retrieve a buffer */ 8946 if (!skb) { 8947 rx_ring->rx_stats.alloc_failed++; 8948 rx_buffer->pagecnt_bias++; 8949 break; 8950 } 8951 8952 igb_put_rx_buffer(rx_ring, rx_buffer, rx_buf_pgcnt); 8953 cleaned_count++; 8954 8955 /* fetch next buffer in frame if non-eop */ 8956 if (igb_is_non_eop(rx_ring, rx_desc)) 8957 continue; 8958 8959 /* verify the packet layout is correct */ 8960 if (igb_cleanup_headers(rx_ring, rx_desc, skb)) { 8961 skb = NULL; 8962 continue; 8963 } 8964 8965 /* probably a little skewed due to removing CRC */ 8966 total_bytes += skb->len; 8967 8968 /* populate checksum, timestamp, VLAN, and protocol */ 8969 igb_process_skb_fields(rx_ring, rx_desc, skb); 8970 8971 napi_gro_receive(&q_vector->napi, skb); 8972 8973 /* reset skb pointer */ 8974 skb = NULL; 8975 8976 /* update budget accounting */ 8977 total_packets++; 8978 } 8979 8980 /* place incomplete frames back on ring for completion */ 8981 rx_ring->skb = skb; 8982 8983 if (xdp_xmit & IGB_XDP_REDIR) 8984 xdp_do_flush(); 8985 8986 if (xdp_xmit & IGB_XDP_TX) { 8987 struct igb_ring *tx_ring = igb_xdp_tx_queue_mapping(adapter); 8988 8989 igb_xdp_ring_update_tail(tx_ring); 8990 } 8991 8992 u64_stats_update_begin(&rx_ring->rx_syncp); 8993 rx_ring->rx_stats.packets += total_packets; 8994 rx_ring->rx_stats.bytes += total_bytes; 8995 u64_stats_update_end(&rx_ring->rx_syncp); 8996 q_vector->rx.total_packets += total_packets; 8997 q_vector->rx.total_bytes += total_bytes; 8998 8999 if (cleaned_count) 9000 igb_alloc_rx_buffers(rx_ring, cleaned_count); 9001 9002 return total_packets; 9003 } 9004 9005 static bool igb_alloc_mapped_page(struct igb_ring *rx_ring, 9006 struct igb_rx_buffer *bi) 9007 { 9008 struct page *page = bi->page; 9009 dma_addr_t dma; 9010 9011 /* since we are recycling buffers we should seldom need to alloc */ 9012 if (likely(page)) 9013 return true; 9014 9015 /* alloc new page for storage */ 9016 page = dev_alloc_pages(igb_rx_pg_order(rx_ring)); 9017 if (unlikely(!page)) { 9018 rx_ring->rx_stats.alloc_failed++; 9019 return false; 9020 } 9021 9022 /* map page for use */ 9023 dma = dma_map_page_attrs(rx_ring->dev, page, 0, 9024 igb_rx_pg_size(rx_ring), 9025 DMA_FROM_DEVICE, 9026 IGB_RX_DMA_ATTR); 9027 9028 /* if mapping failed free memory back to system since 9029 * there isn't much point in holding memory we can't use 9030 */ 9031 if (dma_mapping_error(rx_ring->dev, dma)) { 9032 __free_pages(page, igb_rx_pg_order(rx_ring)); 9033 9034 rx_ring->rx_stats.alloc_failed++; 9035 return false; 9036 } 9037 9038 bi->dma = dma; 9039 bi->page = page; 9040 bi->page_offset = igb_rx_offset(rx_ring); 9041 page_ref_add(page, USHRT_MAX - 1); 9042 bi->pagecnt_bias = USHRT_MAX; 9043 9044 return true; 9045 } 9046 9047 /** 9048 * igb_alloc_rx_buffers - Replace used receive buffers 9049 * @rx_ring: rx descriptor ring to allocate new receive buffers 9050 * @cleaned_count: count of buffers to allocate 9051 **/ 9052 void igb_alloc_rx_buffers(struct igb_ring *rx_ring, u16 cleaned_count) 9053 { 9054 union e1000_adv_rx_desc *rx_desc; 9055 struct igb_rx_buffer *bi; 9056 u16 i = rx_ring->next_to_use; 9057 u16 bufsz; 9058 9059 /* nothing to do */ 9060 if (!cleaned_count) 9061 return; 9062 9063 rx_desc = IGB_RX_DESC(rx_ring, i); 9064 bi = &rx_ring->rx_buffer_info[i]; 9065 i -= rx_ring->count; 9066 9067 bufsz = igb_rx_bufsz(rx_ring); 9068 9069 do { 9070 if (!igb_alloc_mapped_page(rx_ring, bi)) 9071 break; 9072 9073 /* sync the buffer for use by the device */ 9074 dma_sync_single_range_for_device(rx_ring->dev, bi->dma, 9075 bi->page_offset, bufsz, 9076 DMA_FROM_DEVICE); 9077 9078 /* Refresh the desc even if buffer_addrs didn't change 9079 * because each write-back erases this info. 9080 */ 9081 rx_desc->read.pkt_addr = cpu_to_le64(bi->dma + bi->page_offset); 9082 9083 rx_desc++; 9084 bi++; 9085 i++; 9086 if (unlikely(!i)) { 9087 rx_desc = IGB_RX_DESC(rx_ring, 0); 9088 bi = rx_ring->rx_buffer_info; 9089 i -= rx_ring->count; 9090 } 9091 9092 /* clear the length for the next_to_use descriptor */ 9093 rx_desc->wb.upper.length = 0; 9094 9095 cleaned_count--; 9096 } while (cleaned_count); 9097 9098 i += rx_ring->count; 9099 9100 if (rx_ring->next_to_use != i) { 9101 /* record the next descriptor to use */ 9102 rx_ring->next_to_use = i; 9103 9104 /* update next to alloc since we have filled the ring */ 9105 rx_ring->next_to_alloc = i; 9106 9107 /* Force memory writes to complete before letting h/w 9108 * know there are new descriptors to fetch. (Only 9109 * applicable for weak-ordered memory model archs, 9110 * such as IA-64). 9111 */ 9112 dma_wmb(); 9113 writel(i, rx_ring->tail); 9114 } 9115 } 9116 9117 /** 9118 * igb_mii_ioctl - 9119 * @netdev: pointer to netdev struct 9120 * @ifr: interface structure 9121 * @cmd: ioctl command to execute 9122 **/ 9123 static int igb_mii_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd) 9124 { 9125 struct igb_adapter *adapter = netdev_priv(netdev); 9126 struct mii_ioctl_data *data = if_mii(ifr); 9127 9128 if (adapter->hw.phy.media_type != e1000_media_type_copper) 9129 return -EOPNOTSUPP; 9130 9131 switch (cmd) { 9132 case SIOCGMIIPHY: 9133 data->phy_id = adapter->hw.phy.addr; 9134 break; 9135 case SIOCGMIIREG: 9136 if (igb_read_phy_reg(&adapter->hw, data->reg_num & 0x1F, 9137 &data->val_out)) 9138 return -EIO; 9139 break; 9140 case SIOCSMIIREG: 9141 if (igb_write_phy_reg(&adapter->hw, data->reg_num & 0x1F, 9142 data->val_in)) 9143 return -EIO; 9144 break; 9145 default: 9146 return -EOPNOTSUPP; 9147 } 9148 return 0; 9149 } 9150 9151 /** 9152 * igb_ioctl - 9153 * @netdev: pointer to netdev struct 9154 * @ifr: interface structure 9155 * @cmd: ioctl command to execute 9156 **/ 9157 static int igb_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd) 9158 { 9159 switch (cmd) { 9160 case SIOCGMIIPHY: 9161 case SIOCGMIIREG: 9162 case SIOCSMIIREG: 9163 return igb_mii_ioctl(netdev, ifr, cmd); 9164 case SIOCGHWTSTAMP: 9165 return igb_ptp_get_ts_config(netdev, ifr); 9166 case SIOCSHWTSTAMP: 9167 return igb_ptp_set_ts_config(netdev, ifr); 9168 default: 9169 return -EOPNOTSUPP; 9170 } 9171 } 9172 9173 void igb_read_pci_cfg(struct e1000_hw *hw, u32 reg, u16 *value) 9174 { 9175 struct igb_adapter *adapter = hw->back; 9176 9177 pci_read_config_word(adapter->pdev, reg, value); 9178 } 9179 9180 void igb_write_pci_cfg(struct e1000_hw *hw, u32 reg, u16 *value) 9181 { 9182 struct igb_adapter *adapter = hw->back; 9183 9184 pci_write_config_word(adapter->pdev, reg, *value); 9185 } 9186 9187 s32 igb_read_pcie_cap_reg(struct e1000_hw *hw, u32 reg, u16 *value) 9188 { 9189 struct igb_adapter *adapter = hw->back; 9190 9191 if (pcie_capability_read_word(adapter->pdev, reg, value)) 9192 return -E1000_ERR_CONFIG; 9193 9194 return 0; 9195 } 9196 9197 s32 igb_write_pcie_cap_reg(struct e1000_hw *hw, u32 reg, u16 *value) 9198 { 9199 struct igb_adapter *adapter = hw->back; 9200 9201 if (pcie_capability_write_word(adapter->pdev, reg, *value)) 9202 return -E1000_ERR_CONFIG; 9203 9204 return 0; 9205 } 9206 9207 static void igb_vlan_mode(struct net_device *netdev, netdev_features_t features) 9208 { 9209 struct igb_adapter *adapter = netdev_priv(netdev); 9210 struct e1000_hw *hw = &adapter->hw; 9211 u32 ctrl, rctl; 9212 bool enable = !!(features & NETIF_F_HW_VLAN_CTAG_RX); 9213 9214 if (enable) { 9215 /* enable VLAN tag insert/strip */ 9216 ctrl = rd32(E1000_CTRL); 9217 ctrl |= E1000_CTRL_VME; 9218 wr32(E1000_CTRL, ctrl); 9219 9220 /* Disable CFI check */ 9221 rctl = rd32(E1000_RCTL); 9222 rctl &= ~E1000_RCTL_CFIEN; 9223 wr32(E1000_RCTL, rctl); 9224 } else { 9225 /* disable VLAN tag insert/strip */ 9226 ctrl = rd32(E1000_CTRL); 9227 ctrl &= ~E1000_CTRL_VME; 9228 wr32(E1000_CTRL, ctrl); 9229 } 9230 9231 igb_set_vf_vlan_strip(adapter, adapter->vfs_allocated_count, enable); 9232 } 9233 9234 static int igb_vlan_rx_add_vid(struct net_device *netdev, 9235 __be16 proto, u16 vid) 9236 { 9237 struct igb_adapter *adapter = netdev_priv(netdev); 9238 struct e1000_hw *hw = &adapter->hw; 9239 int pf_id = adapter->vfs_allocated_count; 9240 9241 /* add the filter since PF can receive vlans w/o entry in vlvf */ 9242 if (!vid || !(adapter->flags & IGB_FLAG_VLAN_PROMISC)) 9243 igb_vfta_set(hw, vid, pf_id, true, !!vid); 9244 9245 set_bit(vid, adapter->active_vlans); 9246 9247 return 0; 9248 } 9249 9250 static int igb_vlan_rx_kill_vid(struct net_device *netdev, 9251 __be16 proto, u16 vid) 9252 { 9253 struct igb_adapter *adapter = netdev_priv(netdev); 9254 int pf_id = adapter->vfs_allocated_count; 9255 struct e1000_hw *hw = &adapter->hw; 9256 9257 /* remove VID from filter table */ 9258 if (vid && !(adapter->flags & IGB_FLAG_VLAN_PROMISC)) 9259 igb_vfta_set(hw, vid, pf_id, false, true); 9260 9261 clear_bit(vid, adapter->active_vlans); 9262 9263 return 0; 9264 } 9265 9266 static void igb_restore_vlan(struct igb_adapter *adapter) 9267 { 9268 u16 vid = 1; 9269 9270 igb_vlan_mode(adapter->netdev, adapter->netdev->features); 9271 igb_vlan_rx_add_vid(adapter->netdev, htons(ETH_P_8021Q), 0); 9272 9273 for_each_set_bit_from(vid, adapter->active_vlans, VLAN_N_VID) 9274 igb_vlan_rx_add_vid(adapter->netdev, htons(ETH_P_8021Q), vid); 9275 } 9276 9277 int igb_set_spd_dplx(struct igb_adapter *adapter, u32 spd, u8 dplx) 9278 { 9279 struct pci_dev *pdev = adapter->pdev; 9280 struct e1000_mac_info *mac = &adapter->hw.mac; 9281 9282 mac->autoneg = 0; 9283 9284 /* Make sure dplx is at most 1 bit and lsb of speed is not set 9285 * for the switch() below to work 9286 */ 9287 if ((spd & 1) || (dplx & ~1)) 9288 goto err_inval; 9289 9290 /* Fiber NIC's only allow 1000 gbps Full duplex 9291 * and 100Mbps Full duplex for 100baseFx sfp 9292 */ 9293 if (adapter->hw.phy.media_type == e1000_media_type_internal_serdes) { 9294 switch (spd + dplx) { 9295 case SPEED_10 + DUPLEX_HALF: 9296 case SPEED_10 + DUPLEX_FULL: 9297 case SPEED_100 + DUPLEX_HALF: 9298 goto err_inval; 9299 default: 9300 break; 9301 } 9302 } 9303 9304 switch (spd + dplx) { 9305 case SPEED_10 + DUPLEX_HALF: 9306 mac->forced_speed_duplex = ADVERTISE_10_HALF; 9307 break; 9308 case SPEED_10 + DUPLEX_FULL: 9309 mac->forced_speed_duplex = ADVERTISE_10_FULL; 9310 break; 9311 case SPEED_100 + DUPLEX_HALF: 9312 mac->forced_speed_duplex = ADVERTISE_100_HALF; 9313 break; 9314 case SPEED_100 + DUPLEX_FULL: 9315 mac->forced_speed_duplex = ADVERTISE_100_FULL; 9316 break; 9317 case SPEED_1000 + DUPLEX_FULL: 9318 mac->autoneg = 1; 9319 adapter->hw.phy.autoneg_advertised = ADVERTISE_1000_FULL; 9320 break; 9321 case SPEED_1000 + DUPLEX_HALF: /* not supported */ 9322 default: 9323 goto err_inval; 9324 } 9325 9326 /* clear MDI, MDI(-X) override is only allowed when autoneg enabled */ 9327 adapter->hw.phy.mdix = AUTO_ALL_MODES; 9328 9329 return 0; 9330 9331 err_inval: 9332 dev_err(&pdev->dev, "Unsupported Speed/Duplex configuration\n"); 9333 return -EINVAL; 9334 } 9335 9336 static int __igb_shutdown(struct pci_dev *pdev, bool *enable_wake, 9337 bool runtime) 9338 { 9339 struct net_device *netdev = pci_get_drvdata(pdev); 9340 struct igb_adapter *adapter = netdev_priv(netdev); 9341 struct e1000_hw *hw = &adapter->hw; 9342 u32 ctrl, rctl, status; 9343 u32 wufc = runtime ? E1000_WUFC_LNKC : adapter->wol; 9344 bool wake; 9345 9346 rtnl_lock(); 9347 netif_device_detach(netdev); 9348 9349 if (netif_running(netdev)) 9350 __igb_close(netdev, true); 9351 9352 igb_ptp_suspend(adapter); 9353 9354 igb_clear_interrupt_scheme(adapter); 9355 rtnl_unlock(); 9356 9357 status = rd32(E1000_STATUS); 9358 if (status & E1000_STATUS_LU) 9359 wufc &= ~E1000_WUFC_LNKC; 9360 9361 if (wufc) { 9362 igb_setup_rctl(adapter); 9363 igb_set_rx_mode(netdev); 9364 9365 /* turn on all-multi mode if wake on multicast is enabled */ 9366 if (wufc & E1000_WUFC_MC) { 9367 rctl = rd32(E1000_RCTL); 9368 rctl |= E1000_RCTL_MPE; 9369 wr32(E1000_RCTL, rctl); 9370 } 9371 9372 ctrl = rd32(E1000_CTRL); 9373 ctrl |= E1000_CTRL_ADVD3WUC; 9374 wr32(E1000_CTRL, ctrl); 9375 9376 /* Allow time for pending master requests to run */ 9377 igb_disable_pcie_master(hw); 9378 9379 wr32(E1000_WUC, E1000_WUC_PME_EN); 9380 wr32(E1000_WUFC, wufc); 9381 } else { 9382 wr32(E1000_WUC, 0); 9383 wr32(E1000_WUFC, 0); 9384 } 9385 9386 wake = wufc || adapter->en_mng_pt; 9387 if (!wake) 9388 igb_power_down_link(adapter); 9389 else 9390 igb_power_up_link(adapter); 9391 9392 if (enable_wake) 9393 *enable_wake = wake; 9394 9395 /* Release control of h/w to f/w. If f/w is AMT enabled, this 9396 * would have already happened in close and is redundant. 9397 */ 9398 igb_release_hw_control(adapter); 9399 9400 pci_disable_device(pdev); 9401 9402 return 0; 9403 } 9404 9405 static void igb_deliver_wake_packet(struct net_device *netdev) 9406 { 9407 struct igb_adapter *adapter = netdev_priv(netdev); 9408 struct e1000_hw *hw = &adapter->hw; 9409 struct sk_buff *skb; 9410 u32 wupl; 9411 9412 wupl = rd32(E1000_WUPL) & E1000_WUPL_MASK; 9413 9414 /* WUPM stores only the first 128 bytes of the wake packet. 9415 * Read the packet only if we have the whole thing. 9416 */ 9417 if ((wupl == 0) || (wupl > E1000_WUPM_BYTES)) 9418 return; 9419 9420 skb = netdev_alloc_skb_ip_align(netdev, E1000_WUPM_BYTES); 9421 if (!skb) 9422 return; 9423 9424 skb_put(skb, wupl); 9425 9426 /* Ensure reads are 32-bit aligned */ 9427 wupl = roundup(wupl, 4); 9428 9429 memcpy_fromio(skb->data, hw->hw_addr + E1000_WUPM_REG(0), wupl); 9430 9431 skb->protocol = eth_type_trans(skb, netdev); 9432 netif_rx(skb); 9433 } 9434 9435 static int igb_suspend(struct device *dev) 9436 { 9437 return __igb_shutdown(to_pci_dev(dev), NULL, 0); 9438 } 9439 9440 static int __igb_resume(struct device *dev, bool rpm) 9441 { 9442 struct pci_dev *pdev = to_pci_dev(dev); 9443 struct net_device *netdev = pci_get_drvdata(pdev); 9444 struct igb_adapter *adapter = netdev_priv(netdev); 9445 struct e1000_hw *hw = &adapter->hw; 9446 u32 err, val; 9447 9448 pci_set_power_state(pdev, PCI_D0); 9449 pci_restore_state(pdev); 9450 pci_save_state(pdev); 9451 9452 if (!pci_device_is_present(pdev)) 9453 return -ENODEV; 9454 err = pci_enable_device_mem(pdev); 9455 if (err) { 9456 dev_err(&pdev->dev, 9457 "igb: Cannot enable PCI device from suspend\n"); 9458 return err; 9459 } 9460 pci_set_master(pdev); 9461 9462 pci_enable_wake(pdev, PCI_D3hot, 0); 9463 pci_enable_wake(pdev, PCI_D3cold, 0); 9464 9465 if (igb_init_interrupt_scheme(adapter, true)) { 9466 dev_err(&pdev->dev, "Unable to allocate memory for queues\n"); 9467 return -ENOMEM; 9468 } 9469 9470 igb_reset(adapter); 9471 9472 /* let the f/w know that the h/w is now under the control of the 9473 * driver. 9474 */ 9475 igb_get_hw_control(adapter); 9476 9477 val = rd32(E1000_WUS); 9478 if (val & WAKE_PKT_WUS) 9479 igb_deliver_wake_packet(netdev); 9480 9481 wr32(E1000_WUS, ~0); 9482 9483 if (!rpm) 9484 rtnl_lock(); 9485 if (!err && netif_running(netdev)) 9486 err = __igb_open(netdev, true); 9487 9488 if (!err) 9489 netif_device_attach(netdev); 9490 if (!rpm) 9491 rtnl_unlock(); 9492 9493 return err; 9494 } 9495 9496 static int igb_resume(struct device *dev) 9497 { 9498 return __igb_resume(dev, false); 9499 } 9500 9501 static int igb_runtime_idle(struct device *dev) 9502 { 9503 struct net_device *netdev = dev_get_drvdata(dev); 9504 struct igb_adapter *adapter = netdev_priv(netdev); 9505 9506 if (!igb_has_link(adapter)) 9507 pm_schedule_suspend(dev, MSEC_PER_SEC * 5); 9508 9509 return -EBUSY; 9510 } 9511 9512 static int igb_runtime_suspend(struct device *dev) 9513 { 9514 return __igb_shutdown(to_pci_dev(dev), NULL, 1); 9515 } 9516 9517 static int igb_runtime_resume(struct device *dev) 9518 { 9519 return __igb_resume(dev, true); 9520 } 9521 9522 static void igb_shutdown(struct pci_dev *pdev) 9523 { 9524 bool wake; 9525 9526 __igb_shutdown(pdev, &wake, 0); 9527 9528 if (system_state == SYSTEM_POWER_OFF) { 9529 pci_wake_from_d3(pdev, wake); 9530 pci_set_power_state(pdev, PCI_D3hot); 9531 } 9532 } 9533 9534 static int igb_pci_sriov_configure(struct pci_dev *dev, int num_vfs) 9535 { 9536 #ifdef CONFIG_PCI_IOV 9537 int err; 9538 9539 if (num_vfs == 0) { 9540 return igb_disable_sriov(dev, true); 9541 } else { 9542 err = igb_enable_sriov(dev, num_vfs, true); 9543 return err ? err : num_vfs; 9544 } 9545 #endif 9546 return 0; 9547 } 9548 9549 /** 9550 * igb_io_error_detected - called when PCI error is detected 9551 * @pdev: Pointer to PCI device 9552 * @state: The current pci connection state 9553 * 9554 * This function is called after a PCI bus error affecting 9555 * this device has been detected. 9556 **/ 9557 static pci_ers_result_t igb_io_error_detected(struct pci_dev *pdev, 9558 pci_channel_state_t state) 9559 { 9560 struct net_device *netdev = pci_get_drvdata(pdev); 9561 struct igb_adapter *adapter = netdev_priv(netdev); 9562 9563 if (state == pci_channel_io_normal) { 9564 dev_warn(&pdev->dev, "Non-correctable non-fatal error reported.\n"); 9565 return PCI_ERS_RESULT_CAN_RECOVER; 9566 } 9567 9568 netif_device_detach(netdev); 9569 9570 if (state == pci_channel_io_perm_failure) 9571 return PCI_ERS_RESULT_DISCONNECT; 9572 9573 if (netif_running(netdev)) 9574 igb_down(adapter); 9575 pci_disable_device(pdev); 9576 9577 /* Request a slot reset. */ 9578 return PCI_ERS_RESULT_NEED_RESET; 9579 } 9580 9581 /** 9582 * igb_io_slot_reset - called after the pci bus has been reset. 9583 * @pdev: Pointer to PCI device 9584 * 9585 * Restart the card from scratch, as if from a cold-boot. Implementation 9586 * resembles the first-half of the __igb_resume routine. 9587 **/ 9588 static pci_ers_result_t igb_io_slot_reset(struct pci_dev *pdev) 9589 { 9590 struct net_device *netdev = pci_get_drvdata(pdev); 9591 struct igb_adapter *adapter = netdev_priv(netdev); 9592 struct e1000_hw *hw = &adapter->hw; 9593 pci_ers_result_t result; 9594 9595 if (pci_enable_device_mem(pdev)) { 9596 dev_err(&pdev->dev, 9597 "Cannot re-enable PCI device after reset.\n"); 9598 result = PCI_ERS_RESULT_DISCONNECT; 9599 } else { 9600 pci_set_master(pdev); 9601 pci_restore_state(pdev); 9602 pci_save_state(pdev); 9603 9604 pci_enable_wake(pdev, PCI_D3hot, 0); 9605 pci_enable_wake(pdev, PCI_D3cold, 0); 9606 9607 /* In case of PCI error, adapter lose its HW address 9608 * so we should re-assign it here. 9609 */ 9610 hw->hw_addr = adapter->io_addr; 9611 9612 igb_reset(adapter); 9613 wr32(E1000_WUS, ~0); 9614 result = PCI_ERS_RESULT_RECOVERED; 9615 } 9616 9617 return result; 9618 } 9619 9620 /** 9621 * igb_io_resume - called when traffic can start flowing again. 9622 * @pdev: Pointer to PCI device 9623 * 9624 * This callback is called when the error recovery driver tells us that 9625 * its OK to resume normal operation. Implementation resembles the 9626 * second-half of the __igb_resume routine. 9627 */ 9628 static void igb_io_resume(struct pci_dev *pdev) 9629 { 9630 struct net_device *netdev = pci_get_drvdata(pdev); 9631 struct igb_adapter *adapter = netdev_priv(netdev); 9632 9633 if (netif_running(netdev)) { 9634 if (igb_up(adapter)) { 9635 dev_err(&pdev->dev, "igb_up failed after reset\n"); 9636 return; 9637 } 9638 } 9639 9640 netif_device_attach(netdev); 9641 9642 /* let the f/w know that the h/w is now under the control of the 9643 * driver. 9644 */ 9645 igb_get_hw_control(adapter); 9646 } 9647 9648 /** 9649 * igb_rar_set_index - Sync RAL[index] and RAH[index] registers with MAC table 9650 * @adapter: Pointer to adapter structure 9651 * @index: Index of the RAR entry which need to be synced with MAC table 9652 **/ 9653 static void igb_rar_set_index(struct igb_adapter *adapter, u32 index) 9654 { 9655 struct e1000_hw *hw = &adapter->hw; 9656 u32 rar_low, rar_high; 9657 u8 *addr = adapter->mac_table[index].addr; 9658 9659 /* HW expects these to be in network order when they are plugged 9660 * into the registers which are little endian. In order to guarantee 9661 * that ordering we need to do an leXX_to_cpup here in order to be 9662 * ready for the byteswap that occurs with writel 9663 */ 9664 rar_low = le32_to_cpup((__le32 *)(addr)); 9665 rar_high = le16_to_cpup((__le16 *)(addr + 4)); 9666 9667 /* Indicate to hardware the Address is Valid. */ 9668 if (adapter->mac_table[index].state & IGB_MAC_STATE_IN_USE) { 9669 if (is_valid_ether_addr(addr)) 9670 rar_high |= E1000_RAH_AV; 9671 9672 if (adapter->mac_table[index].state & IGB_MAC_STATE_SRC_ADDR) 9673 rar_high |= E1000_RAH_ASEL_SRC_ADDR; 9674 9675 switch (hw->mac.type) { 9676 case e1000_82575: 9677 case e1000_i210: 9678 if (adapter->mac_table[index].state & 9679 IGB_MAC_STATE_QUEUE_STEERING) 9680 rar_high |= E1000_RAH_QSEL_ENABLE; 9681 9682 rar_high |= E1000_RAH_POOL_1 * 9683 adapter->mac_table[index].queue; 9684 break; 9685 default: 9686 rar_high |= E1000_RAH_POOL_1 << 9687 adapter->mac_table[index].queue; 9688 break; 9689 } 9690 } 9691 9692 wr32(E1000_RAL(index), rar_low); 9693 wrfl(); 9694 wr32(E1000_RAH(index), rar_high); 9695 wrfl(); 9696 } 9697 9698 static int igb_set_vf_mac(struct igb_adapter *adapter, 9699 int vf, unsigned char *mac_addr) 9700 { 9701 struct e1000_hw *hw = &adapter->hw; 9702 /* VF MAC addresses start at end of receive addresses and moves 9703 * towards the first, as a result a collision should not be possible 9704 */ 9705 int rar_entry = hw->mac.rar_entry_count - (vf + 1); 9706 unsigned char *vf_mac_addr = adapter->vf_data[vf].vf_mac_addresses; 9707 9708 ether_addr_copy(vf_mac_addr, mac_addr); 9709 ether_addr_copy(adapter->mac_table[rar_entry].addr, mac_addr); 9710 adapter->mac_table[rar_entry].queue = vf; 9711 adapter->mac_table[rar_entry].state |= IGB_MAC_STATE_IN_USE; 9712 igb_rar_set_index(adapter, rar_entry); 9713 9714 return 0; 9715 } 9716 9717 static int igb_ndo_set_vf_mac(struct net_device *netdev, int vf, u8 *mac) 9718 { 9719 struct igb_adapter *adapter = netdev_priv(netdev); 9720 9721 if (vf >= adapter->vfs_allocated_count) 9722 return -EINVAL; 9723 9724 /* Setting the VF MAC to 0 reverts the IGB_VF_FLAG_PF_SET_MAC 9725 * flag and allows to overwrite the MAC via VF netdev. This 9726 * is necessary to allow libvirt a way to restore the original 9727 * MAC after unbinding vfio-pci and reloading igbvf after shutting 9728 * down a VM. 9729 */ 9730 if (is_zero_ether_addr(mac)) { 9731 adapter->vf_data[vf].flags &= ~IGB_VF_FLAG_PF_SET_MAC; 9732 dev_info(&adapter->pdev->dev, 9733 "remove administratively set MAC on VF %d\n", 9734 vf); 9735 } else if (is_valid_ether_addr(mac)) { 9736 adapter->vf_data[vf].flags |= IGB_VF_FLAG_PF_SET_MAC; 9737 dev_info(&adapter->pdev->dev, "setting MAC %pM on VF %d\n", 9738 mac, vf); 9739 dev_info(&adapter->pdev->dev, 9740 "Reload the VF driver to make this change effective."); 9741 /* Generate additional warning if PF is down */ 9742 if (test_bit(__IGB_DOWN, &adapter->state)) { 9743 dev_warn(&adapter->pdev->dev, 9744 "The VF MAC address has been set, but the PF device is not up.\n"); 9745 dev_warn(&adapter->pdev->dev, 9746 "Bring the PF device up before attempting to use the VF device.\n"); 9747 } 9748 } else { 9749 return -EINVAL; 9750 } 9751 return igb_set_vf_mac(adapter, vf, mac); 9752 } 9753 9754 static int igb_link_mbps(int internal_link_speed) 9755 { 9756 switch (internal_link_speed) { 9757 case SPEED_100: 9758 return 100; 9759 case SPEED_1000: 9760 return 1000; 9761 default: 9762 return 0; 9763 } 9764 } 9765 9766 static void igb_set_vf_rate_limit(struct e1000_hw *hw, int vf, int tx_rate, 9767 int link_speed) 9768 { 9769 int rf_dec, rf_int; 9770 u32 bcnrc_val; 9771 9772 if (tx_rate != 0) { 9773 /* Calculate the rate factor values to set */ 9774 rf_int = link_speed / tx_rate; 9775 rf_dec = (link_speed - (rf_int * tx_rate)); 9776 rf_dec = (rf_dec * BIT(E1000_RTTBCNRC_RF_INT_SHIFT)) / 9777 tx_rate; 9778 9779 bcnrc_val = E1000_RTTBCNRC_RS_ENA; 9780 bcnrc_val |= FIELD_PREP(E1000_RTTBCNRC_RF_INT_MASK, rf_int); 9781 bcnrc_val |= (rf_dec & E1000_RTTBCNRC_RF_DEC_MASK); 9782 } else { 9783 bcnrc_val = 0; 9784 } 9785 9786 wr32(E1000_RTTDQSEL, vf); /* vf X uses queue X */ 9787 /* Set global transmit compensation time to the MMW_SIZE in RTTBCNRM 9788 * register. MMW_SIZE=0x014 if 9728-byte jumbo is supported. 9789 */ 9790 wr32(E1000_RTTBCNRM, 0x14); 9791 wr32(E1000_RTTBCNRC, bcnrc_val); 9792 } 9793 9794 static void igb_check_vf_rate_limit(struct igb_adapter *adapter) 9795 { 9796 int actual_link_speed, i; 9797 bool reset_rate = false; 9798 9799 /* VF TX rate limit was not set or not supported */ 9800 if ((adapter->vf_rate_link_speed == 0) || 9801 (adapter->hw.mac.type != e1000_82576)) 9802 return; 9803 9804 actual_link_speed = igb_link_mbps(adapter->link_speed); 9805 if (actual_link_speed != adapter->vf_rate_link_speed) { 9806 reset_rate = true; 9807 adapter->vf_rate_link_speed = 0; 9808 dev_info(&adapter->pdev->dev, 9809 "Link speed has been changed. VF Transmit rate is disabled\n"); 9810 } 9811 9812 for (i = 0; i < adapter->vfs_allocated_count; i++) { 9813 if (reset_rate) 9814 adapter->vf_data[i].tx_rate = 0; 9815 9816 igb_set_vf_rate_limit(&adapter->hw, i, 9817 adapter->vf_data[i].tx_rate, 9818 actual_link_speed); 9819 } 9820 } 9821 9822 static int igb_ndo_set_vf_bw(struct net_device *netdev, int vf, 9823 int min_tx_rate, int max_tx_rate) 9824 { 9825 struct igb_adapter *adapter = netdev_priv(netdev); 9826 struct e1000_hw *hw = &adapter->hw; 9827 int actual_link_speed; 9828 9829 if (hw->mac.type != e1000_82576) 9830 return -EOPNOTSUPP; 9831 9832 if (min_tx_rate) 9833 return -EINVAL; 9834 9835 actual_link_speed = igb_link_mbps(adapter->link_speed); 9836 if ((vf >= adapter->vfs_allocated_count) || 9837 (!(rd32(E1000_STATUS) & E1000_STATUS_LU)) || 9838 (max_tx_rate < 0) || 9839 (max_tx_rate > actual_link_speed)) 9840 return -EINVAL; 9841 9842 adapter->vf_rate_link_speed = actual_link_speed; 9843 adapter->vf_data[vf].tx_rate = (u16)max_tx_rate; 9844 igb_set_vf_rate_limit(hw, vf, max_tx_rate, actual_link_speed); 9845 9846 return 0; 9847 } 9848 9849 static int igb_ndo_set_vf_spoofchk(struct net_device *netdev, int vf, 9850 bool setting) 9851 { 9852 struct igb_adapter *adapter = netdev_priv(netdev); 9853 struct e1000_hw *hw = &adapter->hw; 9854 u32 reg_val, reg_offset; 9855 9856 if (!adapter->vfs_allocated_count) 9857 return -EOPNOTSUPP; 9858 9859 if (vf >= adapter->vfs_allocated_count) 9860 return -EINVAL; 9861 9862 reg_offset = (hw->mac.type == e1000_82576) ? E1000_DTXSWC : E1000_TXSWC; 9863 reg_val = rd32(reg_offset); 9864 if (setting) 9865 reg_val |= (BIT(vf) | 9866 BIT(vf + E1000_DTXSWC_VLAN_SPOOF_SHIFT)); 9867 else 9868 reg_val &= ~(BIT(vf) | 9869 BIT(vf + E1000_DTXSWC_VLAN_SPOOF_SHIFT)); 9870 wr32(reg_offset, reg_val); 9871 9872 adapter->vf_data[vf].spoofchk_enabled = setting; 9873 return 0; 9874 } 9875 9876 static int igb_ndo_set_vf_trust(struct net_device *netdev, int vf, bool setting) 9877 { 9878 struct igb_adapter *adapter = netdev_priv(netdev); 9879 9880 if (vf >= adapter->vfs_allocated_count) 9881 return -EINVAL; 9882 if (adapter->vf_data[vf].trusted == setting) 9883 return 0; 9884 9885 adapter->vf_data[vf].trusted = setting; 9886 9887 dev_info(&adapter->pdev->dev, "VF %u is %strusted\n", 9888 vf, setting ? "" : "not "); 9889 return 0; 9890 } 9891 9892 static int igb_ndo_get_vf_config(struct net_device *netdev, 9893 int vf, struct ifla_vf_info *ivi) 9894 { 9895 struct igb_adapter *adapter = netdev_priv(netdev); 9896 if (vf >= adapter->vfs_allocated_count) 9897 return -EINVAL; 9898 ivi->vf = vf; 9899 memcpy(&ivi->mac, adapter->vf_data[vf].vf_mac_addresses, ETH_ALEN); 9900 ivi->max_tx_rate = adapter->vf_data[vf].tx_rate; 9901 ivi->min_tx_rate = 0; 9902 ivi->vlan = adapter->vf_data[vf].pf_vlan; 9903 ivi->qos = adapter->vf_data[vf].pf_qos; 9904 ivi->spoofchk = adapter->vf_data[vf].spoofchk_enabled; 9905 ivi->trusted = adapter->vf_data[vf].trusted; 9906 return 0; 9907 } 9908 9909 static void igb_vmm_control(struct igb_adapter *adapter) 9910 { 9911 struct e1000_hw *hw = &adapter->hw; 9912 u32 reg; 9913 9914 switch (hw->mac.type) { 9915 case e1000_82575: 9916 case e1000_i210: 9917 case e1000_i211: 9918 case e1000_i354: 9919 default: 9920 /* replication is not supported for 82575 */ 9921 return; 9922 case e1000_82576: 9923 /* notify HW that the MAC is adding vlan tags */ 9924 reg = rd32(E1000_DTXCTL); 9925 reg |= E1000_DTXCTL_VLAN_ADDED; 9926 wr32(E1000_DTXCTL, reg); 9927 fallthrough; 9928 case e1000_82580: 9929 /* enable replication vlan tag stripping */ 9930 reg = rd32(E1000_RPLOLR); 9931 reg |= E1000_RPLOLR_STRVLAN; 9932 wr32(E1000_RPLOLR, reg); 9933 fallthrough; 9934 case e1000_i350: 9935 /* none of the above registers are supported by i350 */ 9936 break; 9937 } 9938 9939 if (adapter->vfs_allocated_count) { 9940 igb_vmdq_set_loopback_pf(hw, true); 9941 igb_vmdq_set_replication_pf(hw, true); 9942 igb_vmdq_set_anti_spoofing_pf(hw, true, 9943 adapter->vfs_allocated_count); 9944 } else { 9945 igb_vmdq_set_loopback_pf(hw, false); 9946 igb_vmdq_set_replication_pf(hw, false); 9947 } 9948 } 9949 9950 static void igb_init_dmac(struct igb_adapter *adapter, u32 pba) 9951 { 9952 struct e1000_hw *hw = &adapter->hw; 9953 u32 dmac_thr; 9954 u16 hwm; 9955 u32 reg; 9956 9957 if (hw->mac.type > e1000_82580) { 9958 if (adapter->flags & IGB_FLAG_DMAC) { 9959 /* force threshold to 0. */ 9960 wr32(E1000_DMCTXTH, 0); 9961 9962 /* DMA Coalescing high water mark needs to be greater 9963 * than the Rx threshold. Set hwm to PBA - max frame 9964 * size in 16B units, capping it at PBA - 6KB. 9965 */ 9966 hwm = 64 * (pba - 6); 9967 reg = rd32(E1000_FCRTC); 9968 reg &= ~E1000_FCRTC_RTH_COAL_MASK; 9969 reg |= FIELD_PREP(E1000_FCRTC_RTH_COAL_MASK, hwm); 9970 wr32(E1000_FCRTC, reg); 9971 9972 /* Set the DMA Coalescing Rx threshold to PBA - 2 * max 9973 * frame size, capping it at PBA - 10KB. 9974 */ 9975 dmac_thr = pba - 10; 9976 reg = rd32(E1000_DMACR); 9977 reg &= ~E1000_DMACR_DMACTHR_MASK; 9978 reg |= FIELD_PREP(E1000_DMACR_DMACTHR_MASK, dmac_thr); 9979 9980 /* transition to L0x or L1 if available..*/ 9981 reg |= (E1000_DMACR_DMAC_EN | E1000_DMACR_DMAC_LX_MASK); 9982 9983 /* watchdog timer= +-1000 usec in 32usec intervals */ 9984 reg |= (1000 >> 5); 9985 9986 /* Disable BMC-to-OS Watchdog Enable */ 9987 if (hw->mac.type != e1000_i354) 9988 reg &= ~E1000_DMACR_DC_BMC2OSW_EN; 9989 wr32(E1000_DMACR, reg); 9990 9991 /* no lower threshold to disable 9992 * coalescing(smart fifb)-UTRESH=0 9993 */ 9994 wr32(E1000_DMCRTRH, 0); 9995 9996 reg = (IGB_DMCTLX_DCFLUSH_DIS | 0x4); 9997 9998 wr32(E1000_DMCTLX, reg); 9999 10000 /* free space in tx packet buffer to wake from 10001 * DMA coal 10002 */ 10003 wr32(E1000_DMCTXTH, (IGB_MIN_TXPBSIZE - 10004 (IGB_TX_BUF_4096 + adapter->max_frame_size)) >> 6); 10005 } 10006 10007 if (hw->mac.type >= e1000_i210 || 10008 (adapter->flags & IGB_FLAG_DMAC)) { 10009 reg = rd32(E1000_PCIEMISC); 10010 reg |= E1000_PCIEMISC_LX_DECISION; 10011 wr32(E1000_PCIEMISC, reg); 10012 } /* endif adapter->dmac is not disabled */ 10013 } else if (hw->mac.type == e1000_82580) { 10014 u32 reg = rd32(E1000_PCIEMISC); 10015 10016 wr32(E1000_PCIEMISC, reg & ~E1000_PCIEMISC_LX_DECISION); 10017 wr32(E1000_DMACR, 0); 10018 } 10019 } 10020 10021 /** 10022 * igb_read_i2c_byte - Reads 8 bit word over I2C 10023 * @hw: pointer to hardware structure 10024 * @byte_offset: byte offset to read 10025 * @dev_addr: device address 10026 * @data: value read 10027 * 10028 * Performs byte read operation over I2C interface at 10029 * a specified device address. 10030 **/ 10031 s32 igb_read_i2c_byte(struct e1000_hw *hw, u8 byte_offset, 10032 u8 dev_addr, u8 *data) 10033 { 10034 struct igb_adapter *adapter = container_of(hw, struct igb_adapter, hw); 10035 struct i2c_client *this_client = adapter->i2c_client; 10036 s32 status; 10037 u16 swfw_mask = 0; 10038 10039 if (!this_client) 10040 return E1000_ERR_I2C; 10041 10042 swfw_mask = E1000_SWFW_PHY0_SM; 10043 10044 if (hw->mac.ops.acquire_swfw_sync(hw, swfw_mask)) 10045 return E1000_ERR_SWFW_SYNC; 10046 10047 status = i2c_smbus_read_byte_data(this_client, byte_offset); 10048 hw->mac.ops.release_swfw_sync(hw, swfw_mask); 10049 10050 if (status < 0) 10051 return E1000_ERR_I2C; 10052 else { 10053 *data = status; 10054 return 0; 10055 } 10056 } 10057 10058 /** 10059 * igb_write_i2c_byte - Writes 8 bit word over I2C 10060 * @hw: pointer to hardware structure 10061 * @byte_offset: byte offset to write 10062 * @dev_addr: device address 10063 * @data: value to write 10064 * 10065 * Performs byte write operation over I2C interface at 10066 * a specified device address. 10067 **/ 10068 s32 igb_write_i2c_byte(struct e1000_hw *hw, u8 byte_offset, 10069 u8 dev_addr, u8 data) 10070 { 10071 struct igb_adapter *adapter = container_of(hw, struct igb_adapter, hw); 10072 struct i2c_client *this_client = adapter->i2c_client; 10073 s32 status; 10074 u16 swfw_mask = E1000_SWFW_PHY0_SM; 10075 10076 if (!this_client) 10077 return E1000_ERR_I2C; 10078 10079 if (hw->mac.ops.acquire_swfw_sync(hw, swfw_mask)) 10080 return E1000_ERR_SWFW_SYNC; 10081 status = i2c_smbus_write_byte_data(this_client, byte_offset, data); 10082 hw->mac.ops.release_swfw_sync(hw, swfw_mask); 10083 10084 if (status) 10085 return E1000_ERR_I2C; 10086 else 10087 return 0; 10088 10089 } 10090 10091 int igb_reinit_queues(struct igb_adapter *adapter) 10092 { 10093 struct net_device *netdev = adapter->netdev; 10094 struct pci_dev *pdev = adapter->pdev; 10095 int err = 0; 10096 10097 if (netif_running(netdev)) 10098 igb_close(netdev); 10099 10100 igb_reset_interrupt_capability(adapter); 10101 10102 if (igb_init_interrupt_scheme(adapter, true)) { 10103 dev_err(&pdev->dev, "Unable to allocate memory for queues\n"); 10104 return -ENOMEM; 10105 } 10106 10107 if (netif_running(netdev)) 10108 err = igb_open(netdev); 10109 10110 return err; 10111 } 10112 10113 static void igb_nfc_filter_exit(struct igb_adapter *adapter) 10114 { 10115 struct igb_nfc_filter *rule; 10116 10117 spin_lock(&adapter->nfc_lock); 10118 10119 hlist_for_each_entry(rule, &adapter->nfc_filter_list, nfc_node) 10120 igb_erase_filter(adapter, rule); 10121 10122 hlist_for_each_entry(rule, &adapter->cls_flower_list, nfc_node) 10123 igb_erase_filter(adapter, rule); 10124 10125 spin_unlock(&adapter->nfc_lock); 10126 } 10127 10128 static void igb_nfc_filter_restore(struct igb_adapter *adapter) 10129 { 10130 struct igb_nfc_filter *rule; 10131 10132 spin_lock(&adapter->nfc_lock); 10133 10134 hlist_for_each_entry(rule, &adapter->nfc_filter_list, nfc_node) 10135 igb_add_filter(adapter, rule); 10136 10137 spin_unlock(&adapter->nfc_lock); 10138 } 10139 10140 static _DEFINE_DEV_PM_OPS(igb_pm_ops, igb_suspend, igb_resume, 10141 igb_runtime_suspend, igb_runtime_resume, 10142 igb_runtime_idle); 10143 10144 static struct pci_driver igb_driver = { 10145 .name = igb_driver_name, 10146 .id_table = igb_pci_tbl, 10147 .probe = igb_probe, 10148 .remove = igb_remove, 10149 .driver.pm = pm_ptr(&igb_pm_ops), 10150 .shutdown = igb_shutdown, 10151 .sriov_configure = igb_pci_sriov_configure, 10152 .err_handler = &igb_err_handler 10153 }; 10154 10155 /* igb_main.c */ 10156