1 /*******************************************************************************
3 Intel(R) Gigabit Ethernet Linux driver
4 Copyright(c) 2007-2012 Intel Corporation.
6 This program is free software; you can redistribute it and/or modify it
7 under the terms and conditions of the GNU General Public License,
8 version 2, as published by the Free Software Foundation.
10 This program is distributed in the hope it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 You should have received a copy of the GNU General Public License along with
16 this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
19 The full GNU General Public License is included in this distribution in
20 the file called "COPYING".
23 e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
24 Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
26 *******************************************************************************/
28 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30 #include <linux/module.h>
31 #include <linux/types.h>
32 #include <linux/init.h>
33 #include <linux/bitops.h>
34 #include <linux/vmalloc.h>
35 #include <linux/pagemap.h>
36 #include <linux/netdevice.h>
37 #include <linux/ipv6.h>
38 #include <linux/slab.h>
39 #include <net/checksum.h>
40 #include <net/ip6_checksum.h>
41 #include <linux/net_tstamp.h>
42 #include <linux/mii.h>
43 #include <linux/ethtool.h>
45 #include <linux/if_vlan.h>
46 #include <linux/pci.h>
47 #include <linux/pci-aspm.h>
48 #include <linux/delay.h>
49 #include <linux/interrupt.h>
51 #include <linux/tcp.h>
52 #include <linux/sctp.h>
53 #include <linux/if_ether.h>
54 #include <linux/aer.h>
55 #include <linux/prefetch.h>
56 #include <linux/pm_runtime.h>
58 #include <linux/dca.h>
65 #define DRV_VERSION __stringify(MAJ) "." __stringify(MIN) "." \
66 __stringify(BUILD) "-k"
67 char igb_driver_name[] = "igb";
68 char igb_driver_version[] = DRV_VERSION;
69 static const char igb_driver_string[] =
70 "Intel(R) Gigabit Ethernet Network Driver";
71 static const char igb_copyright[] = "Copyright (c) 2007-2012 Intel Corporation.";
73 static const struct e1000_info *igb_info_tbl[] = {
74 [board_82575] = &e1000_82575_info,
77 static DEFINE_PCI_DEVICE_TABLE(igb_pci_tbl) = {
78 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I211_COPPER), board_82575 },
79 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I210_COPPER), board_82575 },
80 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I210_FIBER), board_82575 },
81 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I210_SERDES), board_82575 },
82 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I210_SGMII), board_82575 },
83 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I350_COPPER), board_82575 },
84 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I350_FIBER), board_82575 },
85 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I350_SERDES), board_82575 },
86 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I350_SGMII), board_82575 },
87 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_COPPER), board_82575 },
88 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_FIBER), board_82575 },
89 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_QUAD_FIBER), board_82575 },
90 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_SERDES), board_82575 },
91 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_SGMII), board_82575 },
92 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_COPPER_DUAL), board_82575 },
93 { PCI_VDEVICE(INTEL, E1000_DEV_ID_DH89XXCC_SGMII), board_82575 },
94 { PCI_VDEVICE(INTEL, E1000_DEV_ID_DH89XXCC_SERDES), board_82575 },
95 { PCI_VDEVICE(INTEL, E1000_DEV_ID_DH89XXCC_BACKPLANE), board_82575 },
96 { PCI_VDEVICE(INTEL, E1000_DEV_ID_DH89XXCC_SFP), board_82575 },
97 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576), board_82575 },
98 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_NS), board_82575 },
99 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_NS_SERDES), board_82575 },
100 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_FIBER), board_82575 },
101 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_SERDES), board_82575 },
102 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_SERDES_QUAD), board_82575 },
103 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_QUAD_COPPER_ET2), board_82575 },
104 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_QUAD_COPPER), board_82575 },
105 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82575EB_COPPER), board_82575 },
106 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82575EB_FIBER_SERDES), board_82575 },
107 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82575GB_QUAD_COPPER), board_82575 },
108 /* required last entry */
112 MODULE_DEVICE_TABLE(pci, igb_pci_tbl);
114 void igb_reset(struct igb_adapter *);
115 static int igb_setup_all_tx_resources(struct igb_adapter *);
116 static int igb_setup_all_rx_resources(struct igb_adapter *);
117 static void igb_free_all_tx_resources(struct igb_adapter *);
118 static void igb_free_all_rx_resources(struct igb_adapter *);
119 static void igb_setup_mrqc(struct igb_adapter *);
120 static int igb_probe(struct pci_dev *, const struct pci_device_id *);
121 static void __devexit igb_remove(struct pci_dev *pdev);
122 static int igb_sw_init(struct igb_adapter *);
123 static int igb_open(struct net_device *);
124 static int igb_close(struct net_device *);
125 static void igb_configure_tx(struct igb_adapter *);
126 static void igb_configure_rx(struct igb_adapter *);
127 static void igb_clean_all_tx_rings(struct igb_adapter *);
128 static void igb_clean_all_rx_rings(struct igb_adapter *);
129 static void igb_clean_tx_ring(struct igb_ring *);
130 static void igb_clean_rx_ring(struct igb_ring *);
131 static void igb_set_rx_mode(struct net_device *);
132 static void igb_update_phy_info(unsigned long);
133 static void igb_watchdog(unsigned long);
134 static void igb_watchdog_task(struct work_struct *);
135 static netdev_tx_t igb_xmit_frame(struct sk_buff *skb, struct net_device *);
136 static struct rtnl_link_stats64 *igb_get_stats64(struct net_device *dev,
137 struct rtnl_link_stats64 *stats);
138 static int igb_change_mtu(struct net_device *, int);
139 static int igb_set_mac(struct net_device *, void *);
140 static void igb_set_uta(struct igb_adapter *adapter);
141 static irqreturn_t igb_intr(int irq, void *);
142 static irqreturn_t igb_intr_msi(int irq, void *);
143 static irqreturn_t igb_msix_other(int irq, void *);
144 static irqreturn_t igb_msix_ring(int irq, void *);
145 #ifdef CONFIG_IGB_DCA
146 static void igb_update_dca(struct igb_q_vector *);
147 static void igb_setup_dca(struct igb_adapter *);
148 #endif /* CONFIG_IGB_DCA */
149 static int igb_poll(struct napi_struct *, int);
150 static bool igb_clean_tx_irq(struct igb_q_vector *);
151 static bool igb_clean_rx_irq(struct igb_q_vector *, int);
152 static int igb_ioctl(struct net_device *, struct ifreq *, int cmd);
153 static void igb_tx_timeout(struct net_device *);
154 static void igb_reset_task(struct work_struct *);
155 static void igb_vlan_mode(struct net_device *netdev, netdev_features_t features);
156 static int igb_vlan_rx_add_vid(struct net_device *, u16);
157 static int igb_vlan_rx_kill_vid(struct net_device *, u16);
158 static void igb_restore_vlan(struct igb_adapter *);
159 static void igb_rar_set_qsel(struct igb_adapter *, u8 *, u32 , u8);
160 static void igb_ping_all_vfs(struct igb_adapter *);
161 static void igb_msg_task(struct igb_adapter *);
162 static void igb_vmm_control(struct igb_adapter *);
163 static int igb_set_vf_mac(struct igb_adapter *, int, unsigned char *);
164 static void igb_restore_vf_multicasts(struct igb_adapter *adapter);
165 static int igb_ndo_set_vf_mac(struct net_device *netdev, int vf, u8 *mac);
166 static int igb_ndo_set_vf_vlan(struct net_device *netdev,
167 int vf, u16 vlan, u8 qos);
168 static int igb_ndo_set_vf_bw(struct net_device *netdev, int vf, int tx_rate);
169 static int igb_ndo_get_vf_config(struct net_device *netdev, int vf,
170 struct ifla_vf_info *ivi);
171 static void igb_check_vf_rate_limit(struct igb_adapter *);
173 #ifdef CONFIG_PCI_IOV
174 static int igb_vf_configure(struct igb_adapter *adapter, int vf);
175 static bool igb_vfs_are_assigned(struct igb_adapter *adapter);
179 #ifdef CONFIG_PM_SLEEP
180 static int igb_suspend(struct device *);
182 static int igb_resume(struct device *);
183 #ifdef CONFIG_PM_RUNTIME
184 static int igb_runtime_suspend(struct device *dev);
185 static int igb_runtime_resume(struct device *dev);
186 static int igb_runtime_idle(struct device *dev);
188 static const struct dev_pm_ops igb_pm_ops = {
189 SET_SYSTEM_SLEEP_PM_OPS(igb_suspend, igb_resume)
190 SET_RUNTIME_PM_OPS(igb_runtime_suspend, igb_runtime_resume,
194 static void igb_shutdown(struct pci_dev *);
195 #ifdef CONFIG_IGB_DCA
196 static int igb_notify_dca(struct notifier_block *, unsigned long, void *);
197 static struct notifier_block dca_notifier = {
198 .notifier_call = igb_notify_dca,
203 #ifdef CONFIG_NET_POLL_CONTROLLER
204 /* for netdump / net console */
205 static void igb_netpoll(struct net_device *);
207 #ifdef CONFIG_PCI_IOV
208 static unsigned int max_vfs = 0;
209 module_param(max_vfs, uint, 0);
210 MODULE_PARM_DESC(max_vfs, "Maximum number of virtual functions to allocate "
211 "per physical function");
212 #endif /* CONFIG_PCI_IOV */
214 static pci_ers_result_t igb_io_error_detected(struct pci_dev *,
215 pci_channel_state_t);
216 static pci_ers_result_t igb_io_slot_reset(struct pci_dev *);
217 static void igb_io_resume(struct pci_dev *);
219 static const struct pci_error_handlers igb_err_handler = {
220 .error_detected = igb_io_error_detected,
221 .slot_reset = igb_io_slot_reset,
222 .resume = igb_io_resume,
225 static void igb_init_dmac(struct igb_adapter *adapter, u32 pba);
227 static struct pci_driver igb_driver = {
228 .name = igb_driver_name,
229 .id_table = igb_pci_tbl,
231 .remove = __devexit_p(igb_remove),
233 .driver.pm = &igb_pm_ops,
235 .shutdown = igb_shutdown,
236 .err_handler = &igb_err_handler
239 MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
240 MODULE_DESCRIPTION("Intel(R) Gigabit Ethernet Network Driver");
241 MODULE_LICENSE("GPL");
242 MODULE_VERSION(DRV_VERSION);
244 #define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK)
245 static int debug = -1;
246 module_param(debug, int, 0);
247 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
249 struct igb_reg_info {
254 static const struct igb_reg_info igb_reg_info_tbl[] = {
256 /* General Registers */
257 {E1000_CTRL, "CTRL"},
258 {E1000_STATUS, "STATUS"},
259 {E1000_CTRL_EXT, "CTRL_EXT"},
261 /* Interrupt Registers */
265 {E1000_RCTL, "RCTL"},
266 {E1000_RDLEN(0), "RDLEN"},
267 {E1000_RDH(0), "RDH"},
268 {E1000_RDT(0), "RDT"},
269 {E1000_RXDCTL(0), "RXDCTL"},
270 {E1000_RDBAL(0), "RDBAL"},
271 {E1000_RDBAH(0), "RDBAH"},
274 {E1000_TCTL, "TCTL"},
275 {E1000_TDBAL(0), "TDBAL"},
276 {E1000_TDBAH(0), "TDBAH"},
277 {E1000_TDLEN(0), "TDLEN"},
278 {E1000_TDH(0), "TDH"},
279 {E1000_TDT(0), "TDT"},
280 {E1000_TXDCTL(0), "TXDCTL"},
281 {E1000_TDFH, "TDFH"},
282 {E1000_TDFT, "TDFT"},
283 {E1000_TDFHS, "TDFHS"},
284 {E1000_TDFPC, "TDFPC"},
286 /* List Terminator */
291 * igb_regdump - register printout routine
293 static void igb_regdump(struct e1000_hw *hw, struct igb_reg_info *reginfo)
299 switch (reginfo->ofs) {
301 for (n = 0; n < 4; n++)
302 regs[n] = rd32(E1000_RDLEN(n));
305 for (n = 0; n < 4; n++)
306 regs[n] = rd32(E1000_RDH(n));
309 for (n = 0; n < 4; n++)
310 regs[n] = rd32(E1000_RDT(n));
312 case E1000_RXDCTL(0):
313 for (n = 0; n < 4; n++)
314 regs[n] = rd32(E1000_RXDCTL(n));
317 for (n = 0; n < 4; n++)
318 regs[n] = rd32(E1000_RDBAL(n));
321 for (n = 0; n < 4; n++)
322 regs[n] = rd32(E1000_RDBAH(n));
325 for (n = 0; n < 4; n++)
326 regs[n] = rd32(E1000_RDBAL(n));
329 for (n = 0; n < 4; n++)
330 regs[n] = rd32(E1000_TDBAH(n));
333 for (n = 0; n < 4; n++)
334 regs[n] = rd32(E1000_TDLEN(n));
337 for (n = 0; n < 4; n++)
338 regs[n] = rd32(E1000_TDH(n));
341 for (n = 0; n < 4; n++)
342 regs[n] = rd32(E1000_TDT(n));
344 case E1000_TXDCTL(0):
345 for (n = 0; n < 4; n++)
346 regs[n] = rd32(E1000_TXDCTL(n));
349 pr_info("%-15s %08x\n", reginfo->name, rd32(reginfo->ofs));
353 snprintf(rname, 16, "%s%s", reginfo->name, "[0-3]");
354 pr_info("%-15s %08x %08x %08x %08x\n", rname, regs[0], regs[1],
359 * igb_dump - Print registers, tx-rings and rx-rings
361 static void igb_dump(struct igb_adapter *adapter)
363 struct net_device *netdev = adapter->netdev;
364 struct e1000_hw *hw = &adapter->hw;
365 struct igb_reg_info *reginfo;
366 struct igb_ring *tx_ring;
367 union e1000_adv_tx_desc *tx_desc;
368 struct my_u0 { u64 a; u64 b; } *u0;
369 struct igb_ring *rx_ring;
370 union e1000_adv_rx_desc *rx_desc;
374 if (!netif_msg_hw(adapter))
377 /* Print netdevice Info */
379 dev_info(&adapter->pdev->dev, "Net device Info\n");
380 pr_info("Device Name state trans_start "
382 pr_info("%-15s %016lX %016lX %016lX\n", netdev->name,
383 netdev->state, netdev->trans_start, netdev->last_rx);
386 /* Print Registers */
387 dev_info(&adapter->pdev->dev, "Register Dump\n");
388 pr_info(" Register Name Value\n");
389 for (reginfo = (struct igb_reg_info *)igb_reg_info_tbl;
390 reginfo->name; reginfo++) {
391 igb_regdump(hw, reginfo);
394 /* Print TX Ring Summary */
395 if (!netdev || !netif_running(netdev))
398 dev_info(&adapter->pdev->dev, "TX Rings Summary\n");
399 pr_info("Queue [NTU] [NTC] [bi(ntc)->dma ] leng ntw timestamp\n");
400 for (n = 0; n < adapter->num_tx_queues; n++) {
401 struct igb_tx_buffer *buffer_info;
402 tx_ring = adapter->tx_ring[n];
403 buffer_info = &tx_ring->tx_buffer_info[tx_ring->next_to_clean];
404 pr_info(" %5d %5X %5X %016llX %04X %p %016llX\n",
405 n, tx_ring->next_to_use, tx_ring->next_to_clean,
406 (u64)dma_unmap_addr(buffer_info, dma),
407 dma_unmap_len(buffer_info, len),
408 buffer_info->next_to_watch,
409 (u64)buffer_info->time_stamp);
413 if (!netif_msg_tx_done(adapter))
414 goto rx_ring_summary;
416 dev_info(&adapter->pdev->dev, "TX Rings Dump\n");
418 /* Transmit Descriptor Formats
420 * Advanced Transmit Descriptor
421 * +--------------------------------------------------------------+
422 * 0 | Buffer Address [63:0] |
423 * +--------------------------------------------------------------+
424 * 8 | PAYLEN | PORTS |CC|IDX | STA | DCMD |DTYP|MAC|RSV| DTALEN |
425 * +--------------------------------------------------------------+
426 * 63 46 45 40 39 38 36 35 32 31 24 15 0
429 for (n = 0; n < adapter->num_tx_queues; n++) {
430 tx_ring = adapter->tx_ring[n];
431 pr_info("------------------------------------\n");
432 pr_info("TX QUEUE INDEX = %d\n", tx_ring->queue_index);
433 pr_info("------------------------------------\n");
434 pr_info("T [desc] [address 63:0 ] [PlPOCIStDDM Ln] "
435 "[bi->dma ] leng ntw timestamp "
438 for (i = 0; tx_ring->desc && (i < tx_ring->count); i++) {
439 const char *next_desc;
440 struct igb_tx_buffer *buffer_info;
441 tx_desc = IGB_TX_DESC(tx_ring, i);
442 buffer_info = &tx_ring->tx_buffer_info[i];
443 u0 = (struct my_u0 *)tx_desc;
444 if (i == tx_ring->next_to_use &&
445 i == tx_ring->next_to_clean)
446 next_desc = " NTC/U";
447 else if (i == tx_ring->next_to_use)
449 else if (i == tx_ring->next_to_clean)
454 pr_info("T [0x%03X] %016llX %016llX %016llX"
455 " %04X %p %016llX %p%s\n", i,
458 (u64)dma_unmap_addr(buffer_info, dma),
459 dma_unmap_len(buffer_info, len),
460 buffer_info->next_to_watch,
461 (u64)buffer_info->time_stamp,
462 buffer_info->skb, next_desc);
464 if (netif_msg_pktdata(adapter) && buffer_info->skb)
465 print_hex_dump(KERN_INFO, "",
467 16, 1, buffer_info->skb->data,
468 dma_unmap_len(buffer_info, len),
473 /* Print RX Rings Summary */
475 dev_info(&adapter->pdev->dev, "RX Rings Summary\n");
476 pr_info("Queue [NTU] [NTC]\n");
477 for (n = 0; n < adapter->num_rx_queues; n++) {
478 rx_ring = adapter->rx_ring[n];
479 pr_info(" %5d %5X %5X\n",
480 n, rx_ring->next_to_use, rx_ring->next_to_clean);
484 if (!netif_msg_rx_status(adapter))
487 dev_info(&adapter->pdev->dev, "RX Rings Dump\n");
489 /* Advanced Receive Descriptor (Read) Format
491 * +-----------------------------------------------------+
492 * 0 | Packet Buffer Address [63:1] |A0/NSE|
493 * +----------------------------------------------+------+
494 * 8 | Header Buffer Address [63:1] | DD |
495 * +-----------------------------------------------------+
498 * Advanced Receive Descriptor (Write-Back) Format
500 * 63 48 47 32 31 30 21 20 17 16 4 3 0
501 * +------------------------------------------------------+
502 * 0 | Packet IP |SPH| HDR_LEN | RSV|Packet| RSS |
503 * | Checksum Ident | | | | Type | Type |
504 * +------------------------------------------------------+
505 * 8 | VLAN Tag | Length | Extended Error | Extended Status |
506 * +------------------------------------------------------+
507 * 63 48 47 32 31 20 19 0
510 for (n = 0; n < adapter->num_rx_queues; n++) {
511 rx_ring = adapter->rx_ring[n];
512 pr_info("------------------------------------\n");
513 pr_info("RX QUEUE INDEX = %d\n", rx_ring->queue_index);
514 pr_info("------------------------------------\n");
515 pr_info("R [desc] [ PktBuf A0] [ HeadBuf DD] "
516 "[bi->dma ] [bi->skb] <-- Adv Rx Read format\n");
517 pr_info("RWB[desc] [PcsmIpSHl PtRs] [vl er S cks ln] -----"
518 "----------- [bi->skb] <-- Adv Rx Write-Back format\n");
520 for (i = 0; i < rx_ring->count; i++) {
521 const char *next_desc;
522 struct igb_rx_buffer *buffer_info;
523 buffer_info = &rx_ring->rx_buffer_info[i];
524 rx_desc = IGB_RX_DESC(rx_ring, i);
525 u0 = (struct my_u0 *)rx_desc;
526 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
528 if (i == rx_ring->next_to_use)
530 else if (i == rx_ring->next_to_clean)
535 if (staterr & E1000_RXD_STAT_DD) {
536 /* Descriptor Done */
537 pr_info("%s[0x%03X] %016llX %016llX ---------------- %s\n",
543 pr_info("%s[0x%03X] %016llX %016llX %016llX %s\n",
547 (u64)buffer_info->dma,
550 if (netif_msg_pktdata(adapter) &&
551 buffer_info->dma && buffer_info->page) {
552 print_hex_dump(KERN_INFO, "",
555 page_address(buffer_info->page) +
556 buffer_info->page_offset,
568 * igb_get_hw_dev - return device
569 * used by hardware layer to print debugging information
571 struct net_device *igb_get_hw_dev(struct e1000_hw *hw)
573 struct igb_adapter *adapter = hw->back;
574 return adapter->netdev;
578 * igb_init_module - Driver Registration Routine
580 * igb_init_module is the first routine called when the driver is
581 * loaded. All it does is register with the PCI subsystem.
583 static int __init igb_init_module(void)
586 pr_info("%s - version %s\n",
587 igb_driver_string, igb_driver_version);
589 pr_info("%s\n", igb_copyright);
591 #ifdef CONFIG_IGB_DCA
592 dca_register_notify(&dca_notifier);
594 ret = pci_register_driver(&igb_driver);
598 module_init(igb_init_module);
601 * igb_exit_module - Driver Exit Cleanup Routine
603 * igb_exit_module is called just before the driver is removed
606 static void __exit igb_exit_module(void)
608 #ifdef CONFIG_IGB_DCA
609 dca_unregister_notify(&dca_notifier);
611 pci_unregister_driver(&igb_driver);
614 module_exit(igb_exit_module);
616 #define Q_IDX_82576(i) (((i & 0x1) << 3) + (i >> 1))
618 * igb_cache_ring_register - Descriptor ring to register mapping
619 * @adapter: board private structure to initialize
621 * Once we know the feature-set enabled for the device, we'll cache
622 * the register offset the descriptor ring is assigned to.
624 static void igb_cache_ring_register(struct igb_adapter *adapter)
627 u32 rbase_offset = adapter->vfs_allocated_count;
629 switch (adapter->hw.mac.type) {
631 /* The queues are allocated for virtualization such that VF 0
632 * is allocated queues 0 and 8, VF 1 queues 1 and 9, etc.
633 * In order to avoid collision we start at the first free queue
634 * and continue consuming queues in the same sequence
636 if (adapter->vfs_allocated_count) {
637 for (; i < adapter->rss_queues; i++)
638 adapter->rx_ring[i]->reg_idx = rbase_offset +
647 for (; i < adapter->num_rx_queues; i++)
648 adapter->rx_ring[i]->reg_idx = rbase_offset + i;
649 for (; j < adapter->num_tx_queues; j++)
650 adapter->tx_ring[j]->reg_idx = rbase_offset + j;
656 * igb_write_ivar - configure ivar for given MSI-X vector
657 * @hw: pointer to the HW structure
658 * @msix_vector: vector number we are allocating to a given ring
659 * @index: row index of IVAR register to write within IVAR table
660 * @offset: column offset of in IVAR, should be multiple of 8
662 * This function is intended to handle the writing of the IVAR register
663 * for adapters 82576 and newer. The IVAR table consists of 2 columns,
664 * each containing an cause allocation for an Rx and Tx ring, and a
665 * variable number of rows depending on the number of queues supported.
667 static void igb_write_ivar(struct e1000_hw *hw, int msix_vector,
668 int index, int offset)
670 u32 ivar = array_rd32(E1000_IVAR0, index);
672 /* clear any bits that are currently set */
673 ivar &= ~((u32)0xFF << offset);
675 /* write vector and valid bit */
676 ivar |= (msix_vector | E1000_IVAR_VALID) << offset;
678 array_wr32(E1000_IVAR0, index, ivar);
681 #define IGB_N0_QUEUE -1
682 static void igb_assign_vector(struct igb_q_vector *q_vector, int msix_vector)
684 struct igb_adapter *adapter = q_vector->adapter;
685 struct e1000_hw *hw = &adapter->hw;
686 int rx_queue = IGB_N0_QUEUE;
687 int tx_queue = IGB_N0_QUEUE;
690 if (q_vector->rx.ring)
691 rx_queue = q_vector->rx.ring->reg_idx;
692 if (q_vector->tx.ring)
693 tx_queue = q_vector->tx.ring->reg_idx;
695 switch (hw->mac.type) {
697 /* The 82575 assigns vectors using a bitmask, which matches the
698 bitmask for the EICR/EIMS/EIMC registers. To assign one
699 or more queues to a vector, we write the appropriate bits
700 into the MSIXBM register for that vector. */
701 if (rx_queue > IGB_N0_QUEUE)
702 msixbm = E1000_EICR_RX_QUEUE0 << rx_queue;
703 if (tx_queue > IGB_N0_QUEUE)
704 msixbm |= E1000_EICR_TX_QUEUE0 << tx_queue;
705 if (!adapter->msix_entries && msix_vector == 0)
706 msixbm |= E1000_EIMS_OTHER;
707 array_wr32(E1000_MSIXBM(0), msix_vector, msixbm);
708 q_vector->eims_value = msixbm;
712 * 82576 uses a table that essentially consists of 2 columns
713 * with 8 rows. The ordering is column-major so we use the
714 * lower 3 bits as the row index, and the 4th bit as the
717 if (rx_queue > IGB_N0_QUEUE)
718 igb_write_ivar(hw, msix_vector,
720 (rx_queue & 0x8) << 1);
721 if (tx_queue > IGB_N0_QUEUE)
722 igb_write_ivar(hw, msix_vector,
724 ((tx_queue & 0x8) << 1) + 8);
725 q_vector->eims_value = 1 << msix_vector;
732 * On 82580 and newer adapters the scheme is similar to 82576
733 * however instead of ordering column-major we have things
734 * ordered row-major. So we traverse the table by using
735 * bit 0 as the column offset, and the remaining bits as the
738 if (rx_queue > IGB_N0_QUEUE)
739 igb_write_ivar(hw, msix_vector,
741 (rx_queue & 0x1) << 4);
742 if (tx_queue > IGB_N0_QUEUE)
743 igb_write_ivar(hw, msix_vector,
745 ((tx_queue & 0x1) << 4) + 8);
746 q_vector->eims_value = 1 << msix_vector;
753 /* add q_vector eims value to global eims_enable_mask */
754 adapter->eims_enable_mask |= q_vector->eims_value;
756 /* configure q_vector to set itr on first interrupt */
757 q_vector->set_itr = 1;
761 * igb_configure_msix - Configure MSI-X hardware
763 * igb_configure_msix sets up the hardware to properly
764 * generate MSI-X interrupts.
766 static void igb_configure_msix(struct igb_adapter *adapter)
770 struct e1000_hw *hw = &adapter->hw;
772 adapter->eims_enable_mask = 0;
774 /* set vector for other causes, i.e. link changes */
775 switch (hw->mac.type) {
777 tmp = rd32(E1000_CTRL_EXT);
778 /* enable MSI-X PBA support*/
779 tmp |= E1000_CTRL_EXT_PBA_CLR;
781 /* Auto-Mask interrupts upon ICR read. */
782 tmp |= E1000_CTRL_EXT_EIAME;
783 tmp |= E1000_CTRL_EXT_IRCA;
785 wr32(E1000_CTRL_EXT, tmp);
787 /* enable msix_other interrupt */
788 array_wr32(E1000_MSIXBM(0), vector++,
790 adapter->eims_other = E1000_EIMS_OTHER;
799 /* Turn on MSI-X capability first, or our settings
800 * won't stick. And it will take days to debug. */
801 wr32(E1000_GPIE, E1000_GPIE_MSIX_MODE |
802 E1000_GPIE_PBA | E1000_GPIE_EIAME |
805 /* enable msix_other interrupt */
806 adapter->eims_other = 1 << vector;
807 tmp = (vector++ | E1000_IVAR_VALID) << 8;
809 wr32(E1000_IVAR_MISC, tmp);
812 /* do nothing, since nothing else supports MSI-X */
814 } /* switch (hw->mac.type) */
816 adapter->eims_enable_mask |= adapter->eims_other;
818 for (i = 0; i < adapter->num_q_vectors; i++)
819 igb_assign_vector(adapter->q_vector[i], vector++);
825 * igb_request_msix - Initialize MSI-X interrupts
827 * igb_request_msix allocates MSI-X vectors and requests interrupts from the
830 static int igb_request_msix(struct igb_adapter *adapter)
832 struct net_device *netdev = adapter->netdev;
833 struct e1000_hw *hw = &adapter->hw;
834 int i, err = 0, vector = 0;
836 err = request_irq(adapter->msix_entries[vector].vector,
837 igb_msix_other, 0, netdev->name, adapter);
842 for (i = 0; i < adapter->num_q_vectors; i++) {
843 struct igb_q_vector *q_vector = adapter->q_vector[i];
845 q_vector->itr_register = hw->hw_addr + E1000_EITR(vector);
847 if (q_vector->rx.ring && q_vector->tx.ring)
848 sprintf(q_vector->name, "%s-TxRx-%u", netdev->name,
849 q_vector->rx.ring->queue_index);
850 else if (q_vector->tx.ring)
851 sprintf(q_vector->name, "%s-tx-%u", netdev->name,
852 q_vector->tx.ring->queue_index);
853 else if (q_vector->rx.ring)
854 sprintf(q_vector->name, "%s-rx-%u", netdev->name,
855 q_vector->rx.ring->queue_index);
857 sprintf(q_vector->name, "%s-unused", netdev->name);
859 err = request_irq(adapter->msix_entries[vector].vector,
860 igb_msix_ring, 0, q_vector->name,
867 igb_configure_msix(adapter);
873 static void igb_reset_interrupt_capability(struct igb_adapter *adapter)
875 if (adapter->msix_entries) {
876 pci_disable_msix(adapter->pdev);
877 kfree(adapter->msix_entries);
878 adapter->msix_entries = NULL;
879 } else if (adapter->flags & IGB_FLAG_HAS_MSI) {
880 pci_disable_msi(adapter->pdev);
885 * igb_free_q_vector - Free memory allocated for specific interrupt vector
886 * @adapter: board private structure to initialize
887 * @v_idx: Index of vector to be freed
889 * This function frees the memory allocated to the q_vector. In addition if
890 * NAPI is enabled it will delete any references to the NAPI struct prior
891 * to freeing the q_vector.
893 static void igb_free_q_vector(struct igb_adapter *adapter, int v_idx)
895 struct igb_q_vector *q_vector = adapter->q_vector[v_idx];
897 if (q_vector->tx.ring)
898 adapter->tx_ring[q_vector->tx.ring->queue_index] = NULL;
900 if (q_vector->rx.ring)
901 adapter->tx_ring[q_vector->rx.ring->queue_index] = NULL;
903 adapter->q_vector[v_idx] = NULL;
904 netif_napi_del(&q_vector->napi);
907 * ixgbe_get_stats64() might access the rings on this vector,
908 * we must wait a grace period before freeing it.
910 kfree_rcu(q_vector, rcu);
914 * igb_free_q_vectors - Free memory allocated for interrupt vectors
915 * @adapter: board private structure to initialize
917 * This function frees the memory allocated to the q_vectors. In addition if
918 * NAPI is enabled it will delete any references to the NAPI struct prior
919 * to freeing the q_vector.
921 static void igb_free_q_vectors(struct igb_adapter *adapter)
923 int v_idx = adapter->num_q_vectors;
925 adapter->num_tx_queues = 0;
926 adapter->num_rx_queues = 0;
927 adapter->num_q_vectors = 0;
930 igb_free_q_vector(adapter, v_idx);
934 * igb_clear_interrupt_scheme - reset the device to a state of no interrupts
936 * This function resets the device so that it has 0 rx queues, tx queues, and
937 * MSI-X interrupts allocated.
939 static void igb_clear_interrupt_scheme(struct igb_adapter *adapter)
941 igb_free_q_vectors(adapter);
942 igb_reset_interrupt_capability(adapter);
946 * igb_set_interrupt_capability - set MSI or MSI-X if supported
948 * Attempt to configure interrupts using the best available
949 * capabilities of the hardware and kernel.
951 static void igb_set_interrupt_capability(struct igb_adapter *adapter)
956 /* Number of supported queues. */
957 adapter->num_rx_queues = adapter->rss_queues;
958 if (adapter->vfs_allocated_count)
959 adapter->num_tx_queues = 1;
961 adapter->num_tx_queues = adapter->rss_queues;
963 /* start with one vector for every rx queue */
964 numvecs = adapter->num_rx_queues;
966 /* if tx handler is separate add 1 for every tx queue */
967 if (!(adapter->flags & IGB_FLAG_QUEUE_PAIRS))
968 numvecs += adapter->num_tx_queues;
970 /* store the number of vectors reserved for queues */
971 adapter->num_q_vectors = numvecs;
973 /* add 1 vector for link status interrupts */
975 adapter->msix_entries = kcalloc(numvecs, sizeof(struct msix_entry),
978 if (!adapter->msix_entries)
981 for (i = 0; i < numvecs; i++)
982 adapter->msix_entries[i].entry = i;
984 err = pci_enable_msix(adapter->pdev,
985 adapter->msix_entries,
990 igb_reset_interrupt_capability(adapter);
992 /* If we can't do MSI-X, try MSI */
994 #ifdef CONFIG_PCI_IOV
995 /* disable SR-IOV for non MSI-X configurations */
996 if (adapter->vf_data) {
997 struct e1000_hw *hw = &adapter->hw;
998 /* disable iov and allow time for transactions to clear */
999 pci_disable_sriov(adapter->pdev);
1002 kfree(adapter->vf_data);
1003 adapter->vf_data = NULL;
1004 wr32(E1000_IOVCTL, E1000_IOVCTL_REUSE_VFQ);
1007 dev_info(&adapter->pdev->dev, "IOV Disabled\n");
1010 adapter->vfs_allocated_count = 0;
1011 adapter->rss_queues = 1;
1012 adapter->flags |= IGB_FLAG_QUEUE_PAIRS;
1013 adapter->num_rx_queues = 1;
1014 adapter->num_tx_queues = 1;
1015 adapter->num_q_vectors = 1;
1016 if (!pci_enable_msi(adapter->pdev))
1017 adapter->flags |= IGB_FLAG_HAS_MSI;
1020 static void igb_add_ring(struct igb_ring *ring,
1021 struct igb_ring_container *head)
1028 * igb_alloc_q_vector - Allocate memory for a single interrupt vector
1029 * @adapter: board private structure to initialize
1030 * @v_count: q_vectors allocated on adapter, used for ring interleaving
1031 * @v_idx: index of vector in adapter struct
1032 * @txr_count: total number of Tx rings to allocate
1033 * @txr_idx: index of first Tx ring to allocate
1034 * @rxr_count: total number of Rx rings to allocate
1035 * @rxr_idx: index of first Rx ring to allocate
1037 * We allocate one q_vector. If allocation fails we return -ENOMEM.
1039 static int igb_alloc_q_vector(struct igb_adapter *adapter,
1040 int v_count, int v_idx,
1041 int txr_count, int txr_idx,
1042 int rxr_count, int rxr_idx)
1044 struct igb_q_vector *q_vector;
1045 struct igb_ring *ring;
1046 int ring_count, size;
1048 /* igb only supports 1 Tx and/or 1 Rx queue per vector */
1049 if (txr_count > 1 || rxr_count > 1)
1052 ring_count = txr_count + rxr_count;
1053 size = sizeof(struct igb_q_vector) +
1054 (sizeof(struct igb_ring) * ring_count);
1056 /* allocate q_vector and rings */
1057 q_vector = kzalloc(size, GFP_KERNEL);
1061 /* initialize NAPI */
1062 netif_napi_add(adapter->netdev, &q_vector->napi,
1065 /* tie q_vector and adapter together */
1066 adapter->q_vector[v_idx] = q_vector;
1067 q_vector->adapter = adapter;
1069 /* initialize work limits */
1070 q_vector->tx.work_limit = adapter->tx_work_limit;
1072 /* initialize ITR configuration */
1073 q_vector->itr_register = adapter->hw.hw_addr + E1000_EITR(0);
1074 q_vector->itr_val = IGB_START_ITR;
1076 /* initialize pointer to rings */
1077 ring = q_vector->ring;
1080 /* assign generic ring traits */
1081 ring->dev = &adapter->pdev->dev;
1082 ring->netdev = adapter->netdev;
1084 /* configure backlink on ring */
1085 ring->q_vector = q_vector;
1087 /* update q_vector Tx values */
1088 igb_add_ring(ring, &q_vector->tx);
1090 /* For 82575, context index must be unique per ring. */
1091 if (adapter->hw.mac.type == e1000_82575)
1092 set_bit(IGB_RING_FLAG_TX_CTX_IDX, &ring->flags);
1094 /* apply Tx specific ring traits */
1095 ring->count = adapter->tx_ring_count;
1096 ring->queue_index = txr_idx;
1098 /* assign ring to adapter */
1099 adapter->tx_ring[txr_idx] = ring;
1101 /* push pointer to next ring */
1106 /* assign generic ring traits */
1107 ring->dev = &adapter->pdev->dev;
1108 ring->netdev = adapter->netdev;
1110 /* configure backlink on ring */
1111 ring->q_vector = q_vector;
1113 /* update q_vector Rx values */
1114 igb_add_ring(ring, &q_vector->rx);
1116 /* set flag indicating ring supports SCTP checksum offload */
1117 if (adapter->hw.mac.type >= e1000_82576)
1118 set_bit(IGB_RING_FLAG_RX_SCTP_CSUM, &ring->flags);
1121 * On i350, i210, and i211, loopback VLAN packets
1122 * have the tag byte-swapped.
1124 if (adapter->hw.mac.type >= e1000_i350)
1125 set_bit(IGB_RING_FLAG_RX_LB_VLAN_BSWAP, &ring->flags);
1127 /* apply Rx specific ring traits */
1128 ring->count = adapter->rx_ring_count;
1129 ring->queue_index = rxr_idx;
1131 /* assign ring to adapter */
1132 adapter->rx_ring[rxr_idx] = ring;
1140 * igb_alloc_q_vectors - Allocate memory for interrupt vectors
1141 * @adapter: board private structure to initialize
1143 * We allocate one q_vector per queue interrupt. If allocation fails we
1146 static int igb_alloc_q_vectors(struct igb_adapter *adapter)
1148 int q_vectors = adapter->num_q_vectors;
1149 int rxr_remaining = adapter->num_rx_queues;
1150 int txr_remaining = adapter->num_tx_queues;
1151 int rxr_idx = 0, txr_idx = 0, v_idx = 0;
1154 if (q_vectors >= (rxr_remaining + txr_remaining)) {
1155 for (; rxr_remaining; v_idx++) {
1156 err = igb_alloc_q_vector(adapter, q_vectors, v_idx,
1162 /* update counts and index */
1168 for (; v_idx < q_vectors; v_idx++) {
1169 int rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - v_idx);
1170 int tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - v_idx);
1171 err = igb_alloc_q_vector(adapter, q_vectors, v_idx,
1172 tqpv, txr_idx, rqpv, rxr_idx);
1177 /* update counts and index */
1178 rxr_remaining -= rqpv;
1179 txr_remaining -= tqpv;
1187 adapter->num_tx_queues = 0;
1188 adapter->num_rx_queues = 0;
1189 adapter->num_q_vectors = 0;
1192 igb_free_q_vector(adapter, v_idx);
1198 * igb_init_interrupt_scheme - initialize interrupts, allocate queues/vectors
1200 * This function initializes the interrupts and allocates all of the queues.
1202 static int igb_init_interrupt_scheme(struct igb_adapter *adapter)
1204 struct pci_dev *pdev = adapter->pdev;
1207 igb_set_interrupt_capability(adapter);
1209 err = igb_alloc_q_vectors(adapter);
1211 dev_err(&pdev->dev, "Unable to allocate memory for vectors\n");
1212 goto err_alloc_q_vectors;
1215 igb_cache_ring_register(adapter);
1219 err_alloc_q_vectors:
1220 igb_reset_interrupt_capability(adapter);
1225 * igb_request_irq - initialize interrupts
1227 * Attempts to configure interrupts using the best available
1228 * capabilities of the hardware and kernel.
1230 static int igb_request_irq(struct igb_adapter *adapter)
1232 struct net_device *netdev = adapter->netdev;
1233 struct pci_dev *pdev = adapter->pdev;
1236 if (adapter->msix_entries) {
1237 err = igb_request_msix(adapter);
1240 /* fall back to MSI */
1241 igb_free_all_tx_resources(adapter);
1242 igb_free_all_rx_resources(adapter);
1243 igb_clear_interrupt_scheme(adapter);
1244 if (!pci_enable_msi(pdev))
1245 adapter->flags |= IGB_FLAG_HAS_MSI;
1246 adapter->num_tx_queues = 1;
1247 adapter->num_rx_queues = 1;
1248 adapter->num_q_vectors = 1;
1249 err = igb_alloc_q_vectors(adapter);
1252 "Unable to allocate memory for vectors\n");
1255 igb_setup_all_tx_resources(adapter);
1256 igb_setup_all_rx_resources(adapter);
1259 igb_assign_vector(adapter->q_vector[0], 0);
1261 if (adapter->flags & IGB_FLAG_HAS_MSI) {
1262 err = request_irq(pdev->irq, igb_intr_msi, 0,
1263 netdev->name, adapter);
1267 /* fall back to legacy interrupts */
1268 igb_reset_interrupt_capability(adapter);
1269 adapter->flags &= ~IGB_FLAG_HAS_MSI;
1272 err = request_irq(pdev->irq, igb_intr, IRQF_SHARED,
1273 netdev->name, adapter);
1276 dev_err(&pdev->dev, "Error %d getting interrupt\n",
1283 static void igb_free_irq(struct igb_adapter *adapter)
1285 if (adapter->msix_entries) {
1288 free_irq(adapter->msix_entries[vector++].vector, adapter);
1290 for (i = 0; i < adapter->num_q_vectors; i++)
1291 free_irq(adapter->msix_entries[vector++].vector,
1292 adapter->q_vector[i]);
1294 free_irq(adapter->pdev->irq, adapter);
1299 * igb_irq_disable - Mask off interrupt generation on the NIC
1300 * @adapter: board private structure
1302 static void igb_irq_disable(struct igb_adapter *adapter)
1304 struct e1000_hw *hw = &adapter->hw;
1307 * we need to be careful when disabling interrupts. The VFs are also
1308 * mapped into these registers and so clearing the bits can cause
1309 * issues on the VF drivers so we only need to clear what we set
1311 if (adapter->msix_entries) {
1312 u32 regval = rd32(E1000_EIAM);
1313 wr32(E1000_EIAM, regval & ~adapter->eims_enable_mask);
1314 wr32(E1000_EIMC, adapter->eims_enable_mask);
1315 regval = rd32(E1000_EIAC);
1316 wr32(E1000_EIAC, regval & ~adapter->eims_enable_mask);
1320 wr32(E1000_IMC, ~0);
1322 if (adapter->msix_entries) {
1324 for (i = 0; i < adapter->num_q_vectors; i++)
1325 synchronize_irq(adapter->msix_entries[i].vector);
1327 synchronize_irq(adapter->pdev->irq);
1332 * igb_irq_enable - Enable default interrupt generation settings
1333 * @adapter: board private structure
1335 static void igb_irq_enable(struct igb_adapter *adapter)
1337 struct e1000_hw *hw = &adapter->hw;
1339 if (adapter->msix_entries) {
1340 u32 ims = E1000_IMS_LSC | E1000_IMS_DOUTSYNC | E1000_IMS_DRSTA;
1341 u32 regval = rd32(E1000_EIAC);
1342 wr32(E1000_EIAC, regval | adapter->eims_enable_mask);
1343 regval = rd32(E1000_EIAM);
1344 wr32(E1000_EIAM, regval | adapter->eims_enable_mask);
1345 wr32(E1000_EIMS, adapter->eims_enable_mask);
1346 if (adapter->vfs_allocated_count) {
1347 wr32(E1000_MBVFIMR, 0xFF);
1348 ims |= E1000_IMS_VMMB;
1350 wr32(E1000_IMS, ims);
1352 wr32(E1000_IMS, IMS_ENABLE_MASK |
1354 wr32(E1000_IAM, IMS_ENABLE_MASK |
1359 static void igb_update_mng_vlan(struct igb_adapter *adapter)
1361 struct e1000_hw *hw = &adapter->hw;
1362 u16 vid = adapter->hw.mng_cookie.vlan_id;
1363 u16 old_vid = adapter->mng_vlan_id;
1365 if (hw->mng_cookie.status & E1000_MNG_DHCP_COOKIE_STATUS_VLAN) {
1366 /* add VID to filter table */
1367 igb_vfta_set(hw, vid, true);
1368 adapter->mng_vlan_id = vid;
1370 adapter->mng_vlan_id = IGB_MNG_VLAN_NONE;
1373 if ((old_vid != (u16)IGB_MNG_VLAN_NONE) &&
1375 !test_bit(old_vid, adapter->active_vlans)) {
1376 /* remove VID from filter table */
1377 igb_vfta_set(hw, old_vid, false);
1382 * igb_release_hw_control - release control of the h/w to f/w
1383 * @adapter: address of board private structure
1385 * igb_release_hw_control resets CTRL_EXT:DRV_LOAD bit.
1386 * For ASF and Pass Through versions of f/w this means that the
1387 * driver is no longer loaded.
1390 static void igb_release_hw_control(struct igb_adapter *adapter)
1392 struct e1000_hw *hw = &adapter->hw;
1395 /* Let firmware take over control of h/w */
1396 ctrl_ext = rd32(E1000_CTRL_EXT);
1397 wr32(E1000_CTRL_EXT,
1398 ctrl_ext & ~E1000_CTRL_EXT_DRV_LOAD);
1402 * igb_get_hw_control - get control of the h/w from f/w
1403 * @adapter: address of board private structure
1405 * igb_get_hw_control sets CTRL_EXT:DRV_LOAD bit.
1406 * For ASF and Pass Through versions of f/w this means that
1407 * the driver is loaded.
1410 static void igb_get_hw_control(struct igb_adapter *adapter)
1412 struct e1000_hw *hw = &adapter->hw;
1415 /* Let firmware know the driver has taken over */
1416 ctrl_ext = rd32(E1000_CTRL_EXT);
1417 wr32(E1000_CTRL_EXT,
1418 ctrl_ext | E1000_CTRL_EXT_DRV_LOAD);
1422 * igb_configure - configure the hardware for RX and TX
1423 * @adapter: private board structure
1425 static void igb_configure(struct igb_adapter *adapter)
1427 struct net_device *netdev = adapter->netdev;
1430 igb_get_hw_control(adapter);
1431 igb_set_rx_mode(netdev);
1433 igb_restore_vlan(adapter);
1435 igb_setup_tctl(adapter);
1436 igb_setup_mrqc(adapter);
1437 igb_setup_rctl(adapter);
1439 igb_configure_tx(adapter);
1440 igb_configure_rx(adapter);
1442 igb_rx_fifo_flush_82575(&adapter->hw);
1444 /* call igb_desc_unused which always leaves
1445 * at least 1 descriptor unused to make sure
1446 * next_to_use != next_to_clean */
1447 for (i = 0; i < adapter->num_rx_queues; i++) {
1448 struct igb_ring *ring = adapter->rx_ring[i];
1449 igb_alloc_rx_buffers(ring, igb_desc_unused(ring));
1454 * igb_power_up_link - Power up the phy/serdes link
1455 * @adapter: address of board private structure
1457 void igb_power_up_link(struct igb_adapter *adapter)
1459 igb_reset_phy(&adapter->hw);
1461 if (adapter->hw.phy.media_type == e1000_media_type_copper)
1462 igb_power_up_phy_copper(&adapter->hw);
1464 igb_power_up_serdes_link_82575(&adapter->hw);
1468 * igb_power_down_link - Power down the phy/serdes link
1469 * @adapter: address of board private structure
1471 static void igb_power_down_link(struct igb_adapter *adapter)
1473 if (adapter->hw.phy.media_type == e1000_media_type_copper)
1474 igb_power_down_phy_copper_82575(&adapter->hw);
1476 igb_shutdown_serdes_link_82575(&adapter->hw);
1480 * igb_up - Open the interface and prepare it to handle traffic
1481 * @adapter: board private structure
1483 int igb_up(struct igb_adapter *adapter)
1485 struct e1000_hw *hw = &adapter->hw;
1488 /* hardware has been reset, we need to reload some things */
1489 igb_configure(adapter);
1491 clear_bit(__IGB_DOWN, &adapter->state);
1493 for (i = 0; i < adapter->num_q_vectors; i++)
1494 napi_enable(&(adapter->q_vector[i]->napi));
1496 if (adapter->msix_entries)
1497 igb_configure_msix(adapter);
1499 igb_assign_vector(adapter->q_vector[0], 0);
1501 /* Clear any pending interrupts. */
1503 igb_irq_enable(adapter);
1505 /* notify VFs that reset has been completed */
1506 if (adapter->vfs_allocated_count) {
1507 u32 reg_data = rd32(E1000_CTRL_EXT);
1508 reg_data |= E1000_CTRL_EXT_PFRSTD;
1509 wr32(E1000_CTRL_EXT, reg_data);
1512 netif_tx_start_all_queues(adapter->netdev);
1514 /* start the watchdog. */
1515 hw->mac.get_link_status = 1;
1516 schedule_work(&adapter->watchdog_task);
1521 void igb_down(struct igb_adapter *adapter)
1523 struct net_device *netdev = adapter->netdev;
1524 struct e1000_hw *hw = &adapter->hw;
1528 /* signal that we're down so the interrupt handler does not
1529 * reschedule our watchdog timer */
1530 set_bit(__IGB_DOWN, &adapter->state);
1532 /* disable receives in the hardware */
1533 rctl = rd32(E1000_RCTL);
1534 wr32(E1000_RCTL, rctl & ~E1000_RCTL_EN);
1535 /* flush and sleep below */
1537 netif_tx_stop_all_queues(netdev);
1539 /* disable transmits in the hardware */
1540 tctl = rd32(E1000_TCTL);
1541 tctl &= ~E1000_TCTL_EN;
1542 wr32(E1000_TCTL, tctl);
1543 /* flush both disables and wait for them to finish */
1547 for (i = 0; i < adapter->num_q_vectors; i++)
1548 napi_disable(&(adapter->q_vector[i]->napi));
1550 igb_irq_disable(adapter);
1552 del_timer_sync(&adapter->watchdog_timer);
1553 del_timer_sync(&adapter->phy_info_timer);
1555 netif_carrier_off(netdev);
1557 /* record the stats before reset*/
1558 spin_lock(&adapter->stats64_lock);
1559 igb_update_stats(adapter, &adapter->stats64);
1560 spin_unlock(&adapter->stats64_lock);
1562 adapter->link_speed = 0;
1563 adapter->link_duplex = 0;
1565 if (!pci_channel_offline(adapter->pdev))
1567 igb_clean_all_tx_rings(adapter);
1568 igb_clean_all_rx_rings(adapter);
1569 #ifdef CONFIG_IGB_DCA
1571 /* since we reset the hardware DCA settings were cleared */
1572 igb_setup_dca(adapter);
1576 void igb_reinit_locked(struct igb_adapter *adapter)
1578 WARN_ON(in_interrupt());
1579 while (test_and_set_bit(__IGB_RESETTING, &adapter->state))
1583 clear_bit(__IGB_RESETTING, &adapter->state);
1586 void igb_reset(struct igb_adapter *adapter)
1588 struct pci_dev *pdev = adapter->pdev;
1589 struct e1000_hw *hw = &adapter->hw;
1590 struct e1000_mac_info *mac = &hw->mac;
1591 struct e1000_fc_info *fc = &hw->fc;
1592 u32 pba = 0, tx_space, min_tx_space, min_rx_space;
1595 /* Repartition Pba for greater than 9k mtu
1596 * To take effect CTRL.RST is required.
1598 switch (mac->type) {
1601 pba = rd32(E1000_RXPBS);
1602 pba = igb_rxpbs_adjust_82580(pba);
1605 pba = rd32(E1000_RXPBS);
1606 pba &= E1000_RXPBS_SIZE_MASK_82576;
1612 pba = E1000_PBA_34K;
1616 if ((adapter->max_frame_size > ETH_FRAME_LEN + ETH_FCS_LEN) &&
1617 (mac->type < e1000_82576)) {
1618 /* adjust PBA for jumbo frames */
1619 wr32(E1000_PBA, pba);
1621 /* To maintain wire speed transmits, the Tx FIFO should be
1622 * large enough to accommodate two full transmit packets,
1623 * rounded up to the next 1KB and expressed in KB. Likewise,
1624 * the Rx FIFO should be large enough to accommodate at least
1625 * one full receive packet and is similarly rounded up and
1626 * expressed in KB. */
1627 pba = rd32(E1000_PBA);
1628 /* upper 16 bits has Tx packet buffer allocation size in KB */
1629 tx_space = pba >> 16;
1630 /* lower 16 bits has Rx packet buffer allocation size in KB */
1632 /* the tx fifo also stores 16 bytes of information about the tx
1633 * but don't include ethernet FCS because hardware appends it */
1634 min_tx_space = (adapter->max_frame_size +
1635 sizeof(union e1000_adv_tx_desc) -
1637 min_tx_space = ALIGN(min_tx_space, 1024);
1638 min_tx_space >>= 10;
1639 /* software strips receive CRC, so leave room for it */
1640 min_rx_space = adapter->max_frame_size;
1641 min_rx_space = ALIGN(min_rx_space, 1024);
1642 min_rx_space >>= 10;
1644 /* If current Tx allocation is less than the min Tx FIFO size,
1645 * and the min Tx FIFO size is less than the current Rx FIFO
1646 * allocation, take space away from current Rx allocation */
1647 if (tx_space < min_tx_space &&
1648 ((min_tx_space - tx_space) < pba)) {
1649 pba = pba - (min_tx_space - tx_space);
1651 /* if short on rx space, rx wins and must trump tx
1653 if (pba < min_rx_space)
1656 wr32(E1000_PBA, pba);
1659 /* flow control settings */
1660 /* The high water mark must be low enough to fit one full frame
1661 * (or the size used for early receive) above it in the Rx FIFO.
1662 * Set it to the lower of:
1663 * - 90% of the Rx FIFO size, or
1664 * - the full Rx FIFO size minus one full frame */
1665 hwm = min(((pba << 10) * 9 / 10),
1666 ((pba << 10) - 2 * adapter->max_frame_size));
1668 fc->high_water = hwm & 0xFFF0; /* 16-byte granularity */
1669 fc->low_water = fc->high_water - 16;
1670 fc->pause_time = 0xFFFF;
1672 fc->current_mode = fc->requested_mode;
1674 /* disable receive for all VFs and wait one second */
1675 if (adapter->vfs_allocated_count) {
1677 for (i = 0 ; i < adapter->vfs_allocated_count; i++)
1678 adapter->vf_data[i].flags &= IGB_VF_FLAG_PF_SET_MAC;
1680 /* ping all the active vfs to let them know we are going down */
1681 igb_ping_all_vfs(adapter);
1683 /* disable transmits and receives */
1684 wr32(E1000_VFRE, 0);
1685 wr32(E1000_VFTE, 0);
1688 /* Allow time for pending master requests to run */
1689 hw->mac.ops.reset_hw(hw);
1692 if (hw->mac.ops.init_hw(hw))
1693 dev_err(&pdev->dev, "Hardware Error\n");
1696 * Flow control settings reset on hardware reset, so guarantee flow
1697 * control is off when forcing speed.
1699 if (!hw->mac.autoneg)
1700 igb_force_mac_fc(hw);
1702 igb_init_dmac(adapter, pba);
1703 if (!netif_running(adapter->netdev))
1704 igb_power_down_link(adapter);
1706 igb_update_mng_vlan(adapter);
1708 /* Enable h/w to recognize an 802.1Q VLAN Ethernet packet */
1709 wr32(E1000_VET, ETHERNET_IEEE_VLAN_TYPE);
1711 /* Re-enable PTP, where applicable. */
1712 igb_ptp_reset(adapter);
1714 igb_get_phy_info(hw);
1717 static netdev_features_t igb_fix_features(struct net_device *netdev,
1718 netdev_features_t features)
1721 * Since there is no support for separate rx/tx vlan accel
1722 * enable/disable make sure tx flag is always in same state as rx.
1724 if (features & NETIF_F_HW_VLAN_RX)
1725 features |= NETIF_F_HW_VLAN_TX;
1727 features &= ~NETIF_F_HW_VLAN_TX;
1732 static int igb_set_features(struct net_device *netdev,
1733 netdev_features_t features)
1735 netdev_features_t changed = netdev->features ^ features;
1736 struct igb_adapter *adapter = netdev_priv(netdev);
1738 if (changed & NETIF_F_HW_VLAN_RX)
1739 igb_vlan_mode(netdev, features);
1741 if (!(changed & NETIF_F_RXALL))
1744 netdev->features = features;
1746 if (netif_running(netdev))
1747 igb_reinit_locked(adapter);
1754 static const struct net_device_ops igb_netdev_ops = {
1755 .ndo_open = igb_open,
1756 .ndo_stop = igb_close,
1757 .ndo_start_xmit = igb_xmit_frame,
1758 .ndo_get_stats64 = igb_get_stats64,
1759 .ndo_set_rx_mode = igb_set_rx_mode,
1760 .ndo_set_mac_address = igb_set_mac,
1761 .ndo_change_mtu = igb_change_mtu,
1762 .ndo_do_ioctl = igb_ioctl,
1763 .ndo_tx_timeout = igb_tx_timeout,
1764 .ndo_validate_addr = eth_validate_addr,
1765 .ndo_vlan_rx_add_vid = igb_vlan_rx_add_vid,
1766 .ndo_vlan_rx_kill_vid = igb_vlan_rx_kill_vid,
1767 .ndo_set_vf_mac = igb_ndo_set_vf_mac,
1768 .ndo_set_vf_vlan = igb_ndo_set_vf_vlan,
1769 .ndo_set_vf_tx_rate = igb_ndo_set_vf_bw,
1770 .ndo_get_vf_config = igb_ndo_get_vf_config,
1771 #ifdef CONFIG_NET_POLL_CONTROLLER
1772 .ndo_poll_controller = igb_netpoll,
1774 .ndo_fix_features = igb_fix_features,
1775 .ndo_set_features = igb_set_features,
1779 * igb_set_fw_version - Configure version string for ethtool
1780 * @adapter: adapter struct
1783 void igb_set_fw_version(struct igb_adapter *adapter)
1785 struct e1000_hw *hw = &adapter->hw;
1786 struct e1000_fw_version fw;
1788 igb_get_fw_version(hw, &fw);
1790 switch (hw->mac.type) {
1792 snprintf(adapter->fw_version, sizeof(adapter->fw_version),
1794 fw.invm_major, fw.invm_minor, fw.invm_img_type);
1798 /* if option is rom valid, display its version too */
1800 snprintf(adapter->fw_version,
1801 sizeof(adapter->fw_version),
1802 "%d.%d, 0x%08x, %d.%d.%d",
1803 fw.eep_major, fw.eep_minor, fw.etrack_id,
1804 fw.or_major, fw.or_build, fw.or_patch);
1807 snprintf(adapter->fw_version,
1808 sizeof(adapter->fw_version),
1810 fw.eep_major, fw.eep_minor, fw.etrack_id);
1818 * igb_probe - Device Initialization Routine
1819 * @pdev: PCI device information struct
1820 * @ent: entry in igb_pci_tbl
1822 * Returns 0 on success, negative on failure
1824 * igb_probe initializes an adapter identified by a pci_dev structure.
1825 * The OS initialization, configuring of the adapter private structure,
1826 * and a hardware reset occur.
1828 static int __devinit igb_probe(struct pci_dev *pdev,
1829 const struct pci_device_id *ent)
1831 struct net_device *netdev;
1832 struct igb_adapter *adapter;
1833 struct e1000_hw *hw;
1834 u16 eeprom_data = 0;
1836 static int global_quad_port_a; /* global quad port a indication */
1837 const struct e1000_info *ei = igb_info_tbl[ent->driver_data];
1838 unsigned long mmio_start, mmio_len;
1839 int err, pci_using_dac;
1840 u16 eeprom_apme_mask = IGB_EEPROM_APME;
1841 u8 part_str[E1000_PBANUM_LENGTH];
1843 /* Catch broken hardware that put the wrong VF device ID in
1844 * the PCIe SR-IOV capability.
1846 if (pdev->is_virtfn) {
1847 WARN(1, KERN_ERR "%s (%hx:%hx) should not be a VF!\n",
1848 pci_name(pdev), pdev->vendor, pdev->device);
1852 err = pci_enable_device_mem(pdev);
1857 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(64));
1859 err = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64));
1863 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
1865 err = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
1867 dev_err(&pdev->dev, "No usable DMA "
1868 "configuration, aborting\n");
1874 err = pci_request_selected_regions(pdev, pci_select_bars(pdev,
1880 pci_enable_pcie_error_reporting(pdev);
1882 pci_set_master(pdev);
1883 pci_save_state(pdev);
1886 netdev = alloc_etherdev_mq(sizeof(struct igb_adapter),
1889 goto err_alloc_etherdev;
1891 SET_NETDEV_DEV(netdev, &pdev->dev);
1893 pci_set_drvdata(pdev, netdev);
1894 adapter = netdev_priv(netdev);
1895 adapter->netdev = netdev;
1896 adapter->pdev = pdev;
1899 adapter->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
1901 mmio_start = pci_resource_start(pdev, 0);
1902 mmio_len = pci_resource_len(pdev, 0);
1905 hw->hw_addr = ioremap(mmio_start, mmio_len);
1909 netdev->netdev_ops = &igb_netdev_ops;
1910 igb_set_ethtool_ops(netdev);
1911 netdev->watchdog_timeo = 5 * HZ;
1913 strncpy(netdev->name, pci_name(pdev), sizeof(netdev->name) - 1);
1915 netdev->mem_start = mmio_start;
1916 netdev->mem_end = mmio_start + mmio_len;
1918 /* PCI config space info */
1919 hw->vendor_id = pdev->vendor;
1920 hw->device_id = pdev->device;
1921 hw->revision_id = pdev->revision;
1922 hw->subsystem_vendor_id = pdev->subsystem_vendor;
1923 hw->subsystem_device_id = pdev->subsystem_device;
1925 /* Copy the default MAC, PHY and NVM function pointers */
1926 memcpy(&hw->mac.ops, ei->mac_ops, sizeof(hw->mac.ops));
1927 memcpy(&hw->phy.ops, ei->phy_ops, sizeof(hw->phy.ops));
1928 memcpy(&hw->nvm.ops, ei->nvm_ops, sizeof(hw->nvm.ops));
1929 /* Initialize skew-specific constants */
1930 err = ei->get_invariants(hw);
1934 /* setup the private structure */
1935 err = igb_sw_init(adapter);
1939 igb_get_bus_info_pcie(hw);
1941 hw->phy.autoneg_wait_to_complete = false;
1943 /* Copper options */
1944 if (hw->phy.media_type == e1000_media_type_copper) {
1945 hw->phy.mdix = AUTO_ALL_MODES;
1946 hw->phy.disable_polarity_correction = false;
1947 hw->phy.ms_type = e1000_ms_hw_default;
1950 if (igb_check_reset_block(hw))
1951 dev_info(&pdev->dev,
1952 "PHY reset is blocked due to SOL/IDER session.\n");
1955 * features is initialized to 0 in allocation, it might have bits
1956 * set by igb_sw_init so we should use an or instead of an
1959 netdev->features |= NETIF_F_SG |
1966 NETIF_F_HW_VLAN_RX |
1969 /* copy netdev features into list of user selectable features */
1970 netdev->hw_features |= netdev->features;
1971 netdev->hw_features |= NETIF_F_RXALL;
1973 /* set this bit last since it cannot be part of hw_features */
1974 netdev->features |= NETIF_F_HW_VLAN_FILTER;
1976 netdev->vlan_features |= NETIF_F_TSO |
1982 netdev->priv_flags |= IFF_SUPP_NOFCS;
1984 if (pci_using_dac) {
1985 netdev->features |= NETIF_F_HIGHDMA;
1986 netdev->vlan_features |= NETIF_F_HIGHDMA;
1989 if (hw->mac.type >= e1000_82576) {
1990 netdev->hw_features |= NETIF_F_SCTP_CSUM;
1991 netdev->features |= NETIF_F_SCTP_CSUM;
1994 netdev->priv_flags |= IFF_UNICAST_FLT;
1996 adapter->en_mng_pt = igb_enable_mng_pass_thru(hw);
1998 /* before reading the NVM, reset the controller to put the device in a
1999 * known good starting state */
2000 hw->mac.ops.reset_hw(hw);
2003 * make sure the NVM is good , i211 parts have special NVM that
2004 * doesn't contain a checksum
2006 if (hw->mac.type != e1000_i211) {
2007 if (hw->nvm.ops.validate(hw) < 0) {
2008 dev_err(&pdev->dev, "The NVM Checksum Is Not Valid\n");
2014 /* copy the MAC address out of the NVM */
2015 if (hw->mac.ops.read_mac_addr(hw))
2016 dev_err(&pdev->dev, "NVM Read Error\n");
2018 memcpy(netdev->dev_addr, hw->mac.addr, netdev->addr_len);
2019 memcpy(netdev->perm_addr, hw->mac.addr, netdev->addr_len);
2021 if (!is_valid_ether_addr(netdev->perm_addr)) {
2022 dev_err(&pdev->dev, "Invalid MAC Address\n");
2027 /* get firmware version for ethtool -i */
2028 igb_set_fw_version(adapter);
2030 setup_timer(&adapter->watchdog_timer, igb_watchdog,
2031 (unsigned long) adapter);
2032 setup_timer(&adapter->phy_info_timer, igb_update_phy_info,
2033 (unsigned long) adapter);
2035 INIT_WORK(&adapter->reset_task, igb_reset_task);
2036 INIT_WORK(&adapter->watchdog_task, igb_watchdog_task);
2038 /* Initialize link properties that are user-changeable */
2039 adapter->fc_autoneg = true;
2040 hw->mac.autoneg = true;
2041 hw->phy.autoneg_advertised = 0x2f;
2043 hw->fc.requested_mode = e1000_fc_default;
2044 hw->fc.current_mode = e1000_fc_default;
2046 igb_validate_mdi_setting(hw);
2048 /* Initial Wake on LAN setting If APM wake is enabled in the EEPROM,
2049 * enable the ACPI Magic Packet filter
2052 if (hw->bus.func == 0)
2053 hw->nvm.ops.read(hw, NVM_INIT_CONTROL3_PORT_A, 1, &eeprom_data);
2054 else if (hw->mac.type >= e1000_82580)
2055 hw->nvm.ops.read(hw, NVM_INIT_CONTROL3_PORT_A +
2056 NVM_82580_LAN_FUNC_OFFSET(hw->bus.func), 1,
2058 else if (hw->bus.func == 1)
2059 hw->nvm.ops.read(hw, NVM_INIT_CONTROL3_PORT_B, 1, &eeprom_data);
2061 if (eeprom_data & eeprom_apme_mask)
2062 adapter->eeprom_wol |= E1000_WUFC_MAG;
2064 /* now that we have the eeprom settings, apply the special cases where
2065 * the eeprom may be wrong or the board simply won't support wake on
2066 * lan on a particular port */
2067 switch (pdev->device) {
2068 case E1000_DEV_ID_82575GB_QUAD_COPPER:
2069 adapter->eeprom_wol = 0;
2071 case E1000_DEV_ID_82575EB_FIBER_SERDES:
2072 case E1000_DEV_ID_82576_FIBER:
2073 case E1000_DEV_ID_82576_SERDES:
2074 /* Wake events only supported on port A for dual fiber
2075 * regardless of eeprom setting */
2076 if (rd32(E1000_STATUS) & E1000_STATUS_FUNC_1)
2077 adapter->eeprom_wol = 0;
2079 case E1000_DEV_ID_82576_QUAD_COPPER:
2080 case E1000_DEV_ID_82576_QUAD_COPPER_ET2:
2081 /* if quad port adapter, disable WoL on all but port A */
2082 if (global_quad_port_a != 0)
2083 adapter->eeprom_wol = 0;
2085 adapter->flags |= IGB_FLAG_QUAD_PORT_A;
2086 /* Reset for multiple quad port adapters */
2087 if (++global_quad_port_a == 4)
2088 global_quad_port_a = 0;
2092 /* initialize the wol settings based on the eeprom settings */
2093 adapter->wol = adapter->eeprom_wol;
2094 device_set_wakeup_enable(&adapter->pdev->dev, adapter->wol);
2096 /* reset the hardware with the new settings */
2099 /* let the f/w know that the h/w is now under the control of the
2101 igb_get_hw_control(adapter);
2103 strcpy(netdev->name, "eth%d");
2104 err = register_netdev(netdev);
2108 /* carrier off reporting is important to ethtool even BEFORE open */
2109 netif_carrier_off(netdev);
2111 #ifdef CONFIG_IGB_DCA
2112 if (dca_add_requester(&pdev->dev) == 0) {
2113 adapter->flags |= IGB_FLAG_DCA_ENABLED;
2114 dev_info(&pdev->dev, "DCA enabled\n");
2115 igb_setup_dca(adapter);
2120 /* do hw tstamp init after resetting */
2121 igb_ptp_init(adapter);
2123 dev_info(&pdev->dev, "Intel(R) Gigabit Ethernet Network Connection\n");
2124 /* print bus type/speed/width info */
2125 dev_info(&pdev->dev, "%s: (PCIe:%s:%s) %pM\n",
2127 ((hw->bus.speed == e1000_bus_speed_2500) ? "2.5Gb/s" :
2128 (hw->bus.speed == e1000_bus_speed_5000) ? "5.0Gb/s" :
2130 ((hw->bus.width == e1000_bus_width_pcie_x4) ? "Width x4" :
2131 (hw->bus.width == e1000_bus_width_pcie_x2) ? "Width x2" :
2132 (hw->bus.width == e1000_bus_width_pcie_x1) ? "Width x1" :
2136 ret_val = igb_read_part_string(hw, part_str, E1000_PBANUM_LENGTH);
2138 strcpy(part_str, "Unknown");
2139 dev_info(&pdev->dev, "%s: PBA No: %s\n", netdev->name, part_str);
2140 dev_info(&pdev->dev,
2141 "Using %s interrupts. %d rx queue(s), %d tx queue(s)\n",
2142 adapter->msix_entries ? "MSI-X" :
2143 (adapter->flags & IGB_FLAG_HAS_MSI) ? "MSI" : "legacy",
2144 adapter->num_rx_queues, adapter->num_tx_queues);
2145 switch (hw->mac.type) {
2149 igb_set_eee_i350(hw);
2155 pm_runtime_put_noidle(&pdev->dev);
2159 igb_release_hw_control(adapter);
2161 if (!igb_check_reset_block(hw))
2164 if (hw->flash_address)
2165 iounmap(hw->flash_address);
2167 igb_clear_interrupt_scheme(adapter);
2168 iounmap(hw->hw_addr);
2170 free_netdev(netdev);
2172 pci_release_selected_regions(pdev,
2173 pci_select_bars(pdev, IORESOURCE_MEM));
2176 pci_disable_device(pdev);
2181 * igb_remove - Device Removal Routine
2182 * @pdev: PCI device information struct
2184 * igb_remove is called by the PCI subsystem to alert the driver
2185 * that it should release a PCI device. The could be caused by a
2186 * Hot-Plug event, or because the driver is going to be removed from
2189 static void __devexit igb_remove(struct pci_dev *pdev)
2191 struct net_device *netdev = pci_get_drvdata(pdev);
2192 struct igb_adapter *adapter = netdev_priv(netdev);
2193 struct e1000_hw *hw = &adapter->hw;
2195 pm_runtime_get_noresume(&pdev->dev);
2196 igb_ptp_stop(adapter);
2199 * The watchdog timer may be rescheduled, so explicitly
2200 * disable watchdog from being rescheduled.
2202 set_bit(__IGB_DOWN, &adapter->state);
2203 del_timer_sync(&adapter->watchdog_timer);
2204 del_timer_sync(&adapter->phy_info_timer);
2206 cancel_work_sync(&adapter->reset_task);
2207 cancel_work_sync(&adapter->watchdog_task);
2209 #ifdef CONFIG_IGB_DCA
2210 if (adapter->flags & IGB_FLAG_DCA_ENABLED) {
2211 dev_info(&pdev->dev, "DCA disabled\n");
2212 dca_remove_requester(&pdev->dev);
2213 adapter->flags &= ~IGB_FLAG_DCA_ENABLED;
2214 wr32(E1000_DCA_CTRL, E1000_DCA_CTRL_DCA_MODE_DISABLE);
2218 /* Release control of h/w to f/w. If f/w is AMT enabled, this
2219 * would have already happened in close and is redundant. */
2220 igb_release_hw_control(adapter);
2222 unregister_netdev(netdev);
2224 igb_clear_interrupt_scheme(adapter);
2226 #ifdef CONFIG_PCI_IOV
2227 /* reclaim resources allocated to VFs */
2228 if (adapter->vf_data) {
2229 /* disable iov and allow time for transactions to clear */
2230 if (igb_vfs_are_assigned(adapter)) {
2231 dev_info(&pdev->dev, "Unloading driver while VFs are assigned - VFs will not be deallocated\n");
2233 pci_disable_sriov(pdev);
2237 kfree(adapter->vf_data);
2238 adapter->vf_data = NULL;
2239 wr32(E1000_IOVCTL, E1000_IOVCTL_REUSE_VFQ);
2242 dev_info(&pdev->dev, "IOV Disabled\n");
2246 iounmap(hw->hw_addr);
2247 if (hw->flash_address)
2248 iounmap(hw->flash_address);
2249 pci_release_selected_regions(pdev,
2250 pci_select_bars(pdev, IORESOURCE_MEM));
2252 kfree(adapter->shadow_vfta);
2253 free_netdev(netdev);
2255 pci_disable_pcie_error_reporting(pdev);
2257 pci_disable_device(pdev);
2261 * igb_probe_vfs - Initialize vf data storage and add VFs to pci config space
2262 * @adapter: board private structure to initialize
2264 * This function initializes the vf specific data storage and then attempts to
2265 * allocate the VFs. The reason for ordering it this way is because it is much
2266 * mor expensive time wise to disable SR-IOV than it is to allocate and free
2267 * the memory for the VFs.
2269 static void __devinit igb_probe_vfs(struct igb_adapter * adapter)
2271 #ifdef CONFIG_PCI_IOV
2272 struct pci_dev *pdev = adapter->pdev;
2273 struct e1000_hw *hw = &adapter->hw;
2274 int old_vfs = pci_num_vf(adapter->pdev);
2277 /* Virtualization features not supported on i210 family. */
2278 if ((hw->mac.type == e1000_i210) || (hw->mac.type == e1000_i211))
2282 dev_info(&pdev->dev, "%d pre-allocated VFs found - override "
2283 "max_vfs setting of %d\n", old_vfs, max_vfs);
2284 adapter->vfs_allocated_count = old_vfs;
2287 if (!adapter->vfs_allocated_count)
2290 adapter->vf_data = kcalloc(adapter->vfs_allocated_count,
2291 sizeof(struct vf_data_storage), GFP_KERNEL);
2293 /* if allocation failed then we do not support SR-IOV */
2294 if (!adapter->vf_data) {
2295 adapter->vfs_allocated_count = 0;
2296 dev_err(&pdev->dev, "Unable to allocate memory for VF "
2302 if (pci_enable_sriov(pdev, adapter->vfs_allocated_count))
2305 dev_info(&pdev->dev, "%d VFs allocated\n",
2306 adapter->vfs_allocated_count);
2307 for (i = 0; i < adapter->vfs_allocated_count; i++)
2308 igb_vf_configure(adapter, i);
2310 /* DMA Coalescing is not supported in IOV mode. */
2311 adapter->flags &= ~IGB_FLAG_DMAC;
2314 kfree(adapter->vf_data);
2315 adapter->vf_data = NULL;
2316 adapter->vfs_allocated_count = 0;
2319 #endif /* CONFIG_PCI_IOV */
2323 * igb_sw_init - Initialize general software structures (struct igb_adapter)
2324 * @adapter: board private structure to initialize
2326 * igb_sw_init initializes the Adapter private data structure.
2327 * Fields are initialized based on PCI device information and
2328 * OS network device settings (MTU size).
2330 static int __devinit igb_sw_init(struct igb_adapter *adapter)
2332 struct e1000_hw *hw = &adapter->hw;
2333 struct net_device *netdev = adapter->netdev;
2334 struct pci_dev *pdev = adapter->pdev;
2337 pci_read_config_word(pdev, PCI_COMMAND, &hw->bus.pci_cmd_word);
2339 /* set default ring sizes */
2340 adapter->tx_ring_count = IGB_DEFAULT_TXD;
2341 adapter->rx_ring_count = IGB_DEFAULT_RXD;
2343 /* set default ITR values */
2344 adapter->rx_itr_setting = IGB_DEFAULT_ITR;
2345 adapter->tx_itr_setting = IGB_DEFAULT_ITR;
2347 /* set default work limits */
2348 adapter->tx_work_limit = IGB_DEFAULT_TX_WORK;
2350 adapter->max_frame_size = netdev->mtu + ETH_HLEN + ETH_FCS_LEN +
2352 adapter->min_frame_size = ETH_ZLEN + ETH_FCS_LEN;
2354 spin_lock_init(&adapter->stats64_lock);
2355 #ifdef CONFIG_PCI_IOV
2356 switch (hw->mac.type) {
2360 dev_warn(&pdev->dev,
2361 "Maximum of 7 VFs per PF, using max\n");
2362 adapter->vfs_allocated_count = 7;
2364 adapter->vfs_allocated_count = max_vfs;
2369 #endif /* CONFIG_PCI_IOV */
2371 /* Determine the maximum number of RSS queues supported. */
2372 switch (hw->mac.type) {
2374 max_rss_queues = IGB_MAX_RX_QUEUES_I211;
2378 max_rss_queues = IGB_MAX_RX_QUEUES_82575;
2381 /* I350 cannot do RSS and SR-IOV at the same time */
2382 if (!!adapter->vfs_allocated_count) {
2388 if (!!adapter->vfs_allocated_count) {
2395 max_rss_queues = IGB_MAX_RX_QUEUES;
2399 adapter->rss_queues = min_t(u32, max_rss_queues, num_online_cpus());
2401 /* Determine if we need to pair queues. */
2402 switch (hw->mac.type) {
2405 /* Device supports enough interrupts without queue pairing. */
2409 * If VFs are going to be allocated with RSS queues then we
2410 * should pair the queues in order to conserve interrupts due
2411 * to limited supply.
2413 if ((adapter->rss_queues > 1) &&
2414 (adapter->vfs_allocated_count > 6))
2415 adapter->flags |= IGB_FLAG_QUEUE_PAIRS;
2422 * If rss_queues > half of max_rss_queues, pair the queues in
2423 * order to conserve interrupts due to limited supply.
2425 if (adapter->rss_queues > (max_rss_queues / 2))
2426 adapter->flags |= IGB_FLAG_QUEUE_PAIRS;
2430 /* Setup and initialize a copy of the hw vlan table array */
2431 adapter->shadow_vfta = kzalloc(sizeof(u32) *
2432 E1000_VLAN_FILTER_TBL_SIZE,
2435 /* This call may decrease the number of queues */
2436 if (igb_init_interrupt_scheme(adapter)) {
2437 dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
2441 igb_probe_vfs(adapter);
2443 /* Explicitly disable IRQ since the NIC can be in any state. */
2444 igb_irq_disable(adapter);
2446 if (hw->mac.type >= e1000_i350)
2447 adapter->flags &= ~IGB_FLAG_DMAC;
2449 set_bit(__IGB_DOWN, &adapter->state);
2454 * igb_open - Called when a network interface is made active
2455 * @netdev: network interface device structure
2457 * Returns 0 on success, negative value on failure
2459 * The open entry point is called when a network interface is made
2460 * active by the system (IFF_UP). At this point all resources needed
2461 * for transmit and receive operations are allocated, the interrupt
2462 * handler is registered with the OS, the watchdog timer is started,
2463 * and the stack is notified that the interface is ready.
2465 static int __igb_open(struct net_device *netdev, bool resuming)
2467 struct igb_adapter *adapter = netdev_priv(netdev);
2468 struct e1000_hw *hw = &adapter->hw;
2469 struct pci_dev *pdev = adapter->pdev;
2473 /* disallow open during test */
2474 if (test_bit(__IGB_TESTING, &adapter->state)) {
2480 pm_runtime_get_sync(&pdev->dev);
2482 netif_carrier_off(netdev);
2484 /* allocate transmit descriptors */
2485 err = igb_setup_all_tx_resources(adapter);
2489 /* allocate receive descriptors */
2490 err = igb_setup_all_rx_resources(adapter);
2494 igb_power_up_link(adapter);
2496 /* before we allocate an interrupt, we must be ready to handle it.
2497 * Setting DEBUG_SHIRQ in the kernel makes it fire an interrupt
2498 * as soon as we call pci_request_irq, so we have to setup our
2499 * clean_rx handler before we do so. */
2500 igb_configure(adapter);
2502 err = igb_request_irq(adapter);
2506 /* Notify the stack of the actual queue counts. */
2507 err = netif_set_real_num_tx_queues(adapter->netdev,
2508 adapter->num_tx_queues);
2510 goto err_set_queues;
2512 err = netif_set_real_num_rx_queues(adapter->netdev,
2513 adapter->num_rx_queues);
2515 goto err_set_queues;
2517 /* From here on the code is the same as igb_up() */
2518 clear_bit(__IGB_DOWN, &adapter->state);
2520 for (i = 0; i < adapter->num_q_vectors; i++)
2521 napi_enable(&(adapter->q_vector[i]->napi));
2523 /* Clear any pending interrupts. */
2526 igb_irq_enable(adapter);
2528 /* notify VFs that reset has been completed */
2529 if (adapter->vfs_allocated_count) {
2530 u32 reg_data = rd32(E1000_CTRL_EXT);
2531 reg_data |= E1000_CTRL_EXT_PFRSTD;
2532 wr32(E1000_CTRL_EXT, reg_data);
2535 netif_tx_start_all_queues(netdev);
2538 pm_runtime_put(&pdev->dev);
2540 /* start the watchdog. */
2541 hw->mac.get_link_status = 1;
2542 schedule_work(&adapter->watchdog_task);
2547 igb_free_irq(adapter);
2549 igb_release_hw_control(adapter);
2550 igb_power_down_link(adapter);
2551 igb_free_all_rx_resources(adapter);
2553 igb_free_all_tx_resources(adapter);
2557 pm_runtime_put(&pdev->dev);
2562 static int igb_open(struct net_device *netdev)
2564 return __igb_open(netdev, false);
2568 * igb_close - Disables a network interface
2569 * @netdev: network interface device structure
2571 * Returns 0, this is not allowed to fail
2573 * The close entry point is called when an interface is de-activated
2574 * by the OS. The hardware is still under the driver's control, but
2575 * needs to be disabled. A global MAC reset is issued to stop the
2576 * hardware, and all transmit and receive resources are freed.
2578 static int __igb_close(struct net_device *netdev, bool suspending)
2580 struct igb_adapter *adapter = netdev_priv(netdev);
2581 struct pci_dev *pdev = adapter->pdev;
2583 WARN_ON(test_bit(__IGB_RESETTING, &adapter->state));
2586 pm_runtime_get_sync(&pdev->dev);
2589 igb_free_irq(adapter);
2591 igb_free_all_tx_resources(adapter);
2592 igb_free_all_rx_resources(adapter);
2595 pm_runtime_put_sync(&pdev->dev);
2599 static int igb_close(struct net_device *netdev)
2601 return __igb_close(netdev, false);
2605 * igb_setup_tx_resources - allocate Tx resources (Descriptors)
2606 * @tx_ring: tx descriptor ring (for a specific queue) to setup
2608 * Return 0 on success, negative on failure
2610 int igb_setup_tx_resources(struct igb_ring *tx_ring)
2612 struct device *dev = tx_ring->dev;
2615 size = sizeof(struct igb_tx_buffer) * tx_ring->count;
2617 tx_ring->tx_buffer_info = vzalloc(size);
2618 if (!tx_ring->tx_buffer_info)
2621 /* round up to nearest 4K */
2622 tx_ring->size = tx_ring->count * sizeof(union e1000_adv_tx_desc);
2623 tx_ring->size = ALIGN(tx_ring->size, 4096);
2625 tx_ring->desc = dma_alloc_coherent(dev, tx_ring->size,
2626 &tx_ring->dma, GFP_KERNEL);
2630 tx_ring->next_to_use = 0;
2631 tx_ring->next_to_clean = 0;
2636 vfree(tx_ring->tx_buffer_info);
2637 tx_ring->tx_buffer_info = NULL;
2638 dev_err(dev, "Unable to allocate memory for the Tx descriptor ring\n");
2643 * igb_setup_all_tx_resources - wrapper to allocate Tx resources
2644 * (Descriptors) for all queues
2645 * @adapter: board private structure
2647 * Return 0 on success, negative on failure
2649 static int igb_setup_all_tx_resources(struct igb_adapter *adapter)
2651 struct pci_dev *pdev = adapter->pdev;
2654 for (i = 0; i < adapter->num_tx_queues; i++) {
2655 err = igb_setup_tx_resources(adapter->tx_ring[i]);
2658 "Allocation for Tx Queue %u failed\n", i);
2659 for (i--; i >= 0; i--)
2660 igb_free_tx_resources(adapter->tx_ring[i]);
2669 * igb_setup_tctl - configure the transmit control registers
2670 * @adapter: Board private structure
2672 void igb_setup_tctl(struct igb_adapter *adapter)
2674 struct e1000_hw *hw = &adapter->hw;
2677 /* disable queue 0 which is enabled by default on 82575 and 82576 */
2678 wr32(E1000_TXDCTL(0), 0);
2680 /* Program the Transmit Control Register */
2681 tctl = rd32(E1000_TCTL);
2682 tctl &= ~E1000_TCTL_CT;
2683 tctl |= E1000_TCTL_PSP | E1000_TCTL_RTLC |
2684 (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
2686 igb_config_collision_dist(hw);
2688 /* Enable transmits */
2689 tctl |= E1000_TCTL_EN;
2691 wr32(E1000_TCTL, tctl);
2695 * igb_configure_tx_ring - Configure transmit ring after Reset
2696 * @adapter: board private structure
2697 * @ring: tx ring to configure
2699 * Configure a transmit ring after a reset.
2701 void igb_configure_tx_ring(struct igb_adapter *adapter,
2702 struct igb_ring *ring)
2704 struct e1000_hw *hw = &adapter->hw;
2706 u64 tdba = ring->dma;
2707 int reg_idx = ring->reg_idx;
2709 /* disable the queue */
2710 wr32(E1000_TXDCTL(reg_idx), 0);
2714 wr32(E1000_TDLEN(reg_idx),
2715 ring->count * sizeof(union e1000_adv_tx_desc));
2716 wr32(E1000_TDBAL(reg_idx),
2717 tdba & 0x00000000ffffffffULL);
2718 wr32(E1000_TDBAH(reg_idx), tdba >> 32);
2720 ring->tail = hw->hw_addr + E1000_TDT(reg_idx);
2721 wr32(E1000_TDH(reg_idx), 0);
2722 writel(0, ring->tail);
2724 txdctl |= IGB_TX_PTHRESH;
2725 txdctl |= IGB_TX_HTHRESH << 8;
2726 txdctl |= IGB_TX_WTHRESH << 16;
2728 txdctl |= E1000_TXDCTL_QUEUE_ENABLE;
2729 wr32(E1000_TXDCTL(reg_idx), txdctl);
2733 * igb_configure_tx - Configure transmit Unit after Reset
2734 * @adapter: board private structure
2736 * Configure the Tx unit of the MAC after a reset.
2738 static void igb_configure_tx(struct igb_adapter *adapter)
2742 for (i = 0; i < adapter->num_tx_queues; i++)
2743 igb_configure_tx_ring(adapter, adapter->tx_ring[i]);
2747 * igb_setup_rx_resources - allocate Rx resources (Descriptors)
2748 * @rx_ring: rx descriptor ring (for a specific queue) to setup
2750 * Returns 0 on success, negative on failure
2752 int igb_setup_rx_resources(struct igb_ring *rx_ring)
2754 struct device *dev = rx_ring->dev;
2757 size = sizeof(struct igb_rx_buffer) * rx_ring->count;
2759 rx_ring->rx_buffer_info = vzalloc(size);
2760 if (!rx_ring->rx_buffer_info)
2763 /* Round up to nearest 4K */
2764 rx_ring->size = rx_ring->count * sizeof(union e1000_adv_rx_desc);
2765 rx_ring->size = ALIGN(rx_ring->size, 4096);
2767 rx_ring->desc = dma_alloc_coherent(dev, rx_ring->size,
2768 &rx_ring->dma, GFP_KERNEL);
2772 rx_ring->next_to_alloc = 0;
2773 rx_ring->next_to_clean = 0;
2774 rx_ring->next_to_use = 0;
2779 vfree(rx_ring->rx_buffer_info);
2780 rx_ring->rx_buffer_info = NULL;
2781 dev_err(dev, "Unable to allocate memory for the Rx descriptor ring\n");
2786 * igb_setup_all_rx_resources - wrapper to allocate Rx resources
2787 * (Descriptors) for all queues
2788 * @adapter: board private structure
2790 * Return 0 on success, negative on failure
2792 static int igb_setup_all_rx_resources(struct igb_adapter *adapter)
2794 struct pci_dev *pdev = adapter->pdev;
2797 for (i = 0; i < adapter->num_rx_queues; i++) {
2798 err = igb_setup_rx_resources(adapter->rx_ring[i]);
2801 "Allocation for Rx Queue %u failed\n", i);
2802 for (i--; i >= 0; i--)
2803 igb_free_rx_resources(adapter->rx_ring[i]);
2812 * igb_setup_mrqc - configure the multiple receive queue control registers
2813 * @adapter: Board private structure
2815 static void igb_setup_mrqc(struct igb_adapter *adapter)
2817 struct e1000_hw *hw = &adapter->hw;
2819 u32 j, num_rx_queues, shift = 0;
2820 static const u32 rsskey[10] = { 0xDA565A6D, 0xC20E5B25, 0x3D256741,
2821 0xB08FA343, 0xCB2BCAD0, 0xB4307BAE,
2822 0xA32DCB77, 0x0CF23080, 0x3BB7426A,
2825 /* Fill out hash function seeds */
2826 for (j = 0; j < 10; j++)
2827 wr32(E1000_RSSRK(j), rsskey[j]);
2829 num_rx_queues = adapter->rss_queues;
2831 switch (hw->mac.type) {
2836 /* 82576 supports 2 RSS queues for SR-IOV */
2837 if (adapter->vfs_allocated_count) {
2847 * Populate the indirection table 4 entries at a time. To do this
2848 * we are generating the results for n and n+2 and then interleaving
2849 * those with the results with n+1 and n+3.
2851 for (j = 0; j < 32; j++) {
2852 /* first pass generates n and n+2 */
2853 u32 base = ((j * 0x00040004) + 0x00020000) * num_rx_queues;
2854 u32 reta = (base & 0x07800780) >> (7 - shift);
2856 /* second pass generates n+1 and n+3 */
2857 base += 0x00010001 * num_rx_queues;
2858 reta |= (base & 0x07800780) << (1 + shift);
2860 wr32(E1000_RETA(j), reta);
2864 * Disable raw packet checksumming so that RSS hash is placed in
2865 * descriptor on writeback. No need to enable TCP/UDP/IP checksum
2866 * offloads as they are enabled by default
2868 rxcsum = rd32(E1000_RXCSUM);
2869 rxcsum |= E1000_RXCSUM_PCSD;
2871 if (adapter->hw.mac.type >= e1000_82576)
2872 /* Enable Receive Checksum Offload for SCTP */
2873 rxcsum |= E1000_RXCSUM_CRCOFL;
2875 /* Don't need to set TUOFL or IPOFL, they default to 1 */
2876 wr32(E1000_RXCSUM, rxcsum);
2878 /* Generate RSS hash based on packet types, TCP/UDP
2879 * port numbers and/or IPv4/v6 src and dst addresses
2881 mrqc = E1000_MRQC_RSS_FIELD_IPV4 |
2882 E1000_MRQC_RSS_FIELD_IPV4_TCP |
2883 E1000_MRQC_RSS_FIELD_IPV6 |
2884 E1000_MRQC_RSS_FIELD_IPV6_TCP |
2885 E1000_MRQC_RSS_FIELD_IPV6_TCP_EX;
2887 if (adapter->flags & IGB_FLAG_RSS_FIELD_IPV4_UDP)
2888 mrqc |= E1000_MRQC_RSS_FIELD_IPV4_UDP;
2889 if (adapter->flags & IGB_FLAG_RSS_FIELD_IPV6_UDP)
2890 mrqc |= E1000_MRQC_RSS_FIELD_IPV6_UDP;
2892 /* If VMDq is enabled then we set the appropriate mode for that, else
2893 * we default to RSS so that an RSS hash is calculated per packet even
2894 * if we are only using one queue */
2895 if (adapter->vfs_allocated_count) {
2896 if (hw->mac.type > e1000_82575) {
2897 /* Set the default pool for the PF's first queue */
2898 u32 vtctl = rd32(E1000_VT_CTL);
2899 vtctl &= ~(E1000_VT_CTL_DEFAULT_POOL_MASK |
2900 E1000_VT_CTL_DISABLE_DEF_POOL);
2901 vtctl |= adapter->vfs_allocated_count <<
2902 E1000_VT_CTL_DEFAULT_POOL_SHIFT;
2903 wr32(E1000_VT_CTL, vtctl);
2905 if (adapter->rss_queues > 1)
2906 mrqc |= E1000_MRQC_ENABLE_VMDQ_RSS_2Q;
2908 mrqc |= E1000_MRQC_ENABLE_VMDQ;
2910 if (hw->mac.type != e1000_i211)
2911 mrqc |= E1000_MRQC_ENABLE_RSS_4Q;
2913 igb_vmm_control(adapter);
2915 wr32(E1000_MRQC, mrqc);
2919 * igb_setup_rctl - configure the receive control registers
2920 * @adapter: Board private structure
2922 void igb_setup_rctl(struct igb_adapter *adapter)
2924 struct e1000_hw *hw = &adapter->hw;
2927 rctl = rd32(E1000_RCTL);
2929 rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
2930 rctl &= ~(E1000_RCTL_LBM_TCVR | E1000_RCTL_LBM_MAC);
2932 rctl |= E1000_RCTL_EN | E1000_RCTL_BAM | E1000_RCTL_RDMTS_HALF |
2933 (hw->mac.mc_filter_type << E1000_RCTL_MO_SHIFT);
2936 * enable stripping of CRC. It's unlikely this will break BMC
2937 * redirection as it did with e1000. Newer features require
2938 * that the HW strips the CRC.
2940 rctl |= E1000_RCTL_SECRC;
2942 /* disable store bad packets and clear size bits. */
2943 rctl &= ~(E1000_RCTL_SBP | E1000_RCTL_SZ_256);
2945 /* enable LPE to prevent packets larger than max_frame_size */
2946 rctl |= E1000_RCTL_LPE;
2948 /* disable queue 0 to prevent tail write w/o re-config */
2949 wr32(E1000_RXDCTL(0), 0);
2951 /* Attention!!! For SR-IOV PF driver operations you must enable
2952 * queue drop for all VF and PF queues to prevent head of line blocking
2953 * if an un-trusted VF does not provide descriptors to hardware.
2955 if (adapter->vfs_allocated_count) {
2956 /* set all queue drop enable bits */
2957 wr32(E1000_QDE, ALL_QUEUES);
2960 /* This is useful for sniffing bad packets. */
2961 if (adapter->netdev->features & NETIF_F_RXALL) {
2962 /* UPE and MPE will be handled by normal PROMISC logic
2963 * in e1000e_set_rx_mode */
2964 rctl |= (E1000_RCTL_SBP | /* Receive bad packets */
2965 E1000_RCTL_BAM | /* RX All Bcast Pkts */
2966 E1000_RCTL_PMCF); /* RX All MAC Ctrl Pkts */
2968 rctl &= ~(E1000_RCTL_VFE | /* Disable VLAN filter */
2969 E1000_RCTL_DPF | /* Allow filtered pause */
2970 E1000_RCTL_CFIEN); /* Dis VLAN CFIEN Filter */
2971 /* Do not mess with E1000_CTRL_VME, it affects transmit as well,
2972 * and that breaks VLANs.
2976 wr32(E1000_RCTL, rctl);
2979 static inline int igb_set_vf_rlpml(struct igb_adapter *adapter, int size,
2982 struct e1000_hw *hw = &adapter->hw;
2985 /* if it isn't the PF check to see if VFs are enabled and
2986 * increase the size to support vlan tags */
2987 if (vfn < adapter->vfs_allocated_count &&
2988 adapter->vf_data[vfn].vlans_enabled)
2989 size += VLAN_TAG_SIZE;
2991 vmolr = rd32(E1000_VMOLR(vfn));
2992 vmolr &= ~E1000_VMOLR_RLPML_MASK;
2993 vmolr |= size | E1000_VMOLR_LPE;
2994 wr32(E1000_VMOLR(vfn), vmolr);
3000 * igb_rlpml_set - set maximum receive packet size
3001 * @adapter: board private structure
3003 * Configure maximum receivable packet size.
3005 static void igb_rlpml_set(struct igb_adapter *adapter)
3007 u32 max_frame_size = adapter->max_frame_size;
3008 struct e1000_hw *hw = &adapter->hw;
3009 u16 pf_id = adapter->vfs_allocated_count;
3012 igb_set_vf_rlpml(adapter, max_frame_size, pf_id);
3014 * If we're in VMDQ or SR-IOV mode, then set global RLPML
3015 * to our max jumbo frame size, in case we need to enable
3016 * jumbo frames on one of the rings later.
3017 * This will not pass over-length frames into the default
3018 * queue because it's gated by the VMOLR.RLPML.
3020 max_frame_size = MAX_JUMBO_FRAME_SIZE;
3023 wr32(E1000_RLPML, max_frame_size);
3026 static inline void igb_set_vmolr(struct igb_adapter *adapter,
3029 struct e1000_hw *hw = &adapter->hw;
3033 * This register exists only on 82576 and newer so if we are older then
3034 * we should exit and do nothing
3036 if (hw->mac.type < e1000_82576)
3039 vmolr = rd32(E1000_VMOLR(vfn));
3040 vmolr |= E1000_VMOLR_STRVLAN; /* Strip vlan tags */
3042 vmolr |= E1000_VMOLR_AUPE; /* Accept untagged packets */
3044 vmolr &= ~(E1000_VMOLR_AUPE); /* Tagged packets ONLY */
3046 /* clear all bits that might not be set */
3047 vmolr &= ~(E1000_VMOLR_BAM | E1000_VMOLR_RSSE);
3049 if (adapter->rss_queues > 1 && vfn == adapter->vfs_allocated_count)
3050 vmolr |= E1000_VMOLR_RSSE; /* enable RSS */
3052 * for VMDq only allow the VFs and pool 0 to accept broadcast and
3055 if (vfn <= adapter->vfs_allocated_count)
3056 vmolr |= E1000_VMOLR_BAM; /* Accept broadcast */
3058 wr32(E1000_VMOLR(vfn), vmolr);
3062 * igb_configure_rx_ring - Configure a receive ring after Reset
3063 * @adapter: board private structure
3064 * @ring: receive ring to be configured
3066 * Configure the Rx unit of the MAC after a reset.
3068 void igb_configure_rx_ring(struct igb_adapter *adapter,
3069 struct igb_ring *ring)
3071 struct e1000_hw *hw = &adapter->hw;
3072 u64 rdba = ring->dma;
3073 int reg_idx = ring->reg_idx;
3074 u32 srrctl = 0, rxdctl = 0;
3076 /* disable the queue */
3077 wr32(E1000_RXDCTL(reg_idx), 0);
3079 /* Set DMA base address registers */
3080 wr32(E1000_RDBAL(reg_idx),
3081 rdba & 0x00000000ffffffffULL);
3082 wr32(E1000_RDBAH(reg_idx), rdba >> 32);
3083 wr32(E1000_RDLEN(reg_idx),
3084 ring->count * sizeof(union e1000_adv_rx_desc));
3086 /* initialize head and tail */
3087 ring->tail = hw->hw_addr + E1000_RDT(reg_idx);
3088 wr32(E1000_RDH(reg_idx), 0);
3089 writel(0, ring->tail);
3091 /* set descriptor configuration */
3092 srrctl = IGB_RX_HDR_LEN << E1000_SRRCTL_BSIZEHDRSIZE_SHIFT;
3093 srrctl |= IGB_RX_BUFSZ >> E1000_SRRCTL_BSIZEPKT_SHIFT;
3094 srrctl |= E1000_SRRCTL_DESCTYPE_ADV_ONEBUF;
3095 if (hw->mac.type >= e1000_82580)
3096 srrctl |= E1000_SRRCTL_TIMESTAMP;
3097 /* Only set Drop Enable if we are supporting multiple queues */
3098 if (adapter->vfs_allocated_count || adapter->num_rx_queues > 1)
3099 srrctl |= E1000_SRRCTL_DROP_EN;
3101 wr32(E1000_SRRCTL(reg_idx), srrctl);
3103 /* set filtering for VMDQ pools */
3104 igb_set_vmolr(adapter, reg_idx & 0x7, true);
3106 rxdctl |= IGB_RX_PTHRESH;
3107 rxdctl |= IGB_RX_HTHRESH << 8;
3108 rxdctl |= IGB_RX_WTHRESH << 16;
3110 /* enable receive descriptor fetching */
3111 rxdctl |= E1000_RXDCTL_QUEUE_ENABLE;
3112 wr32(E1000_RXDCTL(reg_idx), rxdctl);
3116 * igb_configure_rx - Configure receive Unit after Reset
3117 * @adapter: board private structure
3119 * Configure the Rx unit of the MAC after a reset.
3121 static void igb_configure_rx(struct igb_adapter *adapter)
3125 /* set UTA to appropriate mode */
3126 igb_set_uta(adapter);
3128 /* set the correct pool for the PF default MAC address in entry 0 */
3129 igb_rar_set_qsel(adapter, adapter->hw.mac.addr, 0,
3130 adapter->vfs_allocated_count);
3132 /* Setup the HW Rx Head and Tail Descriptor Pointers and
3133 * the Base and Length of the Rx Descriptor Ring */
3134 for (i = 0; i < adapter->num_rx_queues; i++)
3135 igb_configure_rx_ring(adapter, adapter->rx_ring[i]);
3139 * igb_free_tx_resources - Free Tx Resources per Queue
3140 * @tx_ring: Tx descriptor ring for a specific queue
3142 * Free all transmit software resources
3144 void igb_free_tx_resources(struct igb_ring *tx_ring)
3146 igb_clean_tx_ring(tx_ring);
3148 vfree(tx_ring->tx_buffer_info);
3149 tx_ring->tx_buffer_info = NULL;
3151 /* if not set, then don't free */
3155 dma_free_coherent(tx_ring->dev, tx_ring->size,
3156 tx_ring->desc, tx_ring->dma);
3158 tx_ring->desc = NULL;
3162 * igb_free_all_tx_resources - Free Tx Resources for All Queues
3163 * @adapter: board private structure
3165 * Free all transmit software resources
3167 static void igb_free_all_tx_resources(struct igb_adapter *adapter)
3171 for (i = 0; i < adapter->num_tx_queues; i++)
3172 igb_free_tx_resources(adapter->tx_ring[i]);
3175 void igb_unmap_and_free_tx_resource(struct igb_ring *ring,
3176 struct igb_tx_buffer *tx_buffer)
3178 if (tx_buffer->skb) {
3179 dev_kfree_skb_any(tx_buffer->skb);
3180 if (dma_unmap_len(tx_buffer, len))
3181 dma_unmap_single(ring->dev,
3182 dma_unmap_addr(tx_buffer, dma),
3183 dma_unmap_len(tx_buffer, len),
3185 } else if (dma_unmap_len(tx_buffer, len)) {
3186 dma_unmap_page(ring->dev,
3187 dma_unmap_addr(tx_buffer, dma),
3188 dma_unmap_len(tx_buffer, len),
3191 tx_buffer->next_to_watch = NULL;
3192 tx_buffer->skb = NULL;
3193 dma_unmap_len_set(tx_buffer, len, 0);
3194 /* buffer_info must be completely set up in the transmit path */
3198 * igb_clean_tx_ring - Free Tx Buffers
3199 * @tx_ring: ring to be cleaned
3201 static void igb_clean_tx_ring(struct igb_ring *tx_ring)
3203 struct igb_tx_buffer *buffer_info;
3207 if (!tx_ring->tx_buffer_info)
3209 /* Free all the Tx ring sk_buffs */
3211 for (i = 0; i < tx_ring->count; i++) {
3212 buffer_info = &tx_ring->tx_buffer_info[i];
3213 igb_unmap_and_free_tx_resource(tx_ring, buffer_info);
3216 netdev_tx_reset_queue(txring_txq(tx_ring));
3218 size = sizeof(struct igb_tx_buffer) * tx_ring->count;
3219 memset(tx_ring->tx_buffer_info, 0, size);
3221 /* Zero out the descriptor ring */
3222 memset(tx_ring->desc, 0, tx_ring->size);
3224 tx_ring->next_to_use = 0;
3225 tx_ring->next_to_clean = 0;
3229 * igb_clean_all_tx_rings - Free Tx Buffers for all queues
3230 * @adapter: board private structure
3232 static void igb_clean_all_tx_rings(struct igb_adapter *adapter)
3236 for (i = 0; i < adapter->num_tx_queues; i++)
3237 igb_clean_tx_ring(adapter->tx_ring[i]);
3241 * igb_free_rx_resources - Free Rx Resources
3242 * @rx_ring: ring to clean the resources from
3244 * Free all receive software resources
3246 void igb_free_rx_resources(struct igb_ring *rx_ring)
3248 igb_clean_rx_ring(rx_ring);
3250 vfree(rx_ring->rx_buffer_info);
3251 rx_ring->rx_buffer_info = NULL;
3253 /* if not set, then don't free */
3257 dma_free_coherent(rx_ring->dev, rx_ring->size,
3258 rx_ring->desc, rx_ring->dma);
3260 rx_ring->desc = NULL;
3264 * igb_free_all_rx_resources - Free Rx Resources for All Queues
3265 * @adapter: board private structure
3267 * Free all receive software resources
3269 static void igb_free_all_rx_resources(struct igb_adapter *adapter)
3273 for (i = 0; i < adapter->num_rx_queues; i++)
3274 igb_free_rx_resources(adapter->rx_ring[i]);
3278 * igb_clean_rx_ring - Free Rx Buffers per Queue
3279 * @rx_ring: ring to free buffers from
3281 static void igb_clean_rx_ring(struct igb_ring *rx_ring)
3287 dev_kfree_skb(rx_ring->skb);
3288 rx_ring->skb = NULL;
3290 if (!rx_ring->rx_buffer_info)
3293 /* Free all the Rx ring sk_buffs */
3294 for (i = 0; i < rx_ring->count; i++) {
3295 struct igb_rx_buffer *buffer_info = &rx_ring->rx_buffer_info[i];
3297 if (!buffer_info->page)
3300 dma_unmap_page(rx_ring->dev,
3304 __free_page(buffer_info->page);
3306 buffer_info->page = NULL;
3309 size = sizeof(struct igb_rx_buffer) * rx_ring->count;
3310 memset(rx_ring->rx_buffer_info, 0, size);
3312 /* Zero out the descriptor ring */
3313 memset(rx_ring->desc, 0, rx_ring->size);
3315 rx_ring->next_to_alloc = 0;
3316 rx_ring->next_to_clean = 0;
3317 rx_ring->next_to_use = 0;
3321 * igb_clean_all_rx_rings - Free Rx Buffers for all queues
3322 * @adapter: board private structure
3324 static void igb_clean_all_rx_rings(struct igb_adapter *adapter)
3328 for (i = 0; i < adapter->num_rx_queues; i++)
3329 igb_clean_rx_ring(adapter->rx_ring[i]);
3333 * igb_set_mac - Change the Ethernet Address of the NIC
3334 * @netdev: network interface device structure
3335 * @p: pointer to an address structure
3337 * Returns 0 on success, negative on failure
3339 static int igb_set_mac(struct net_device *netdev, void *p)
3341 struct igb_adapter *adapter = netdev_priv(netdev);
3342 struct e1000_hw *hw = &adapter->hw;
3343 struct sockaddr *addr = p;
3345 if (!is_valid_ether_addr(addr->sa_data))
3346 return -EADDRNOTAVAIL;
3348 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
3349 memcpy(hw->mac.addr, addr->sa_data, netdev->addr_len);
3351 /* set the correct pool for the new PF MAC address in entry 0 */
3352 igb_rar_set_qsel(adapter, hw->mac.addr, 0,
3353 adapter->vfs_allocated_count);
3359 * igb_write_mc_addr_list - write multicast addresses to MTA
3360 * @netdev: network interface device structure
3362 * Writes multicast address list to the MTA hash table.
3363 * Returns: -ENOMEM on failure
3364 * 0 on no addresses written
3365 * X on writing X addresses to MTA
3367 static int igb_write_mc_addr_list(struct net_device *netdev)
3369 struct igb_adapter *adapter = netdev_priv(netdev);
3370 struct e1000_hw *hw = &adapter->hw;
3371 struct netdev_hw_addr *ha;
3375 if (netdev_mc_empty(netdev)) {
3376 /* nothing to program, so clear mc list */
3377 igb_update_mc_addr_list(hw, NULL, 0);
3378 igb_restore_vf_multicasts(adapter);
3382 mta_list = kzalloc(netdev_mc_count(netdev) * 6, GFP_ATOMIC);
3386 /* The shared function expects a packed array of only addresses. */
3388 netdev_for_each_mc_addr(ha, netdev)
3389 memcpy(mta_list + (i++ * ETH_ALEN), ha->addr, ETH_ALEN);
3391 igb_update_mc_addr_list(hw, mta_list, i);
3394 return netdev_mc_count(netdev);
3398 * igb_write_uc_addr_list - write unicast addresses to RAR table
3399 * @netdev: network interface device structure
3401 * Writes unicast address list to the RAR table.
3402 * Returns: -ENOMEM on failure/insufficient address space
3403 * 0 on no addresses written
3404 * X on writing X addresses to the RAR table
3406 static int igb_write_uc_addr_list(struct net_device *netdev)
3408 struct igb_adapter *adapter = netdev_priv(netdev);
3409 struct e1000_hw *hw = &adapter->hw;
3410 unsigned int vfn = adapter->vfs_allocated_count;
3411 unsigned int rar_entries = hw->mac.rar_entry_count - (vfn + 1);
3414 /* return ENOMEM indicating insufficient memory for addresses */
3415 if (netdev_uc_count(netdev) > rar_entries)
3418 if (!netdev_uc_empty(netdev) && rar_entries) {
3419 struct netdev_hw_addr *ha;
3421 netdev_for_each_uc_addr(ha, netdev) {
3424 igb_rar_set_qsel(adapter, ha->addr,
3430 /* write the addresses in reverse order to avoid write combining */
3431 for (; rar_entries > 0 ; rar_entries--) {
3432 wr32(E1000_RAH(rar_entries), 0);
3433 wr32(E1000_RAL(rar_entries), 0);
3441 * igb_set_rx_mode - Secondary Unicast, Multicast and Promiscuous mode set
3442 * @netdev: network interface device structure
3444 * The set_rx_mode entry point is called whenever the unicast or multicast
3445 * address lists or the network interface flags are updated. This routine is
3446 * responsible for configuring the hardware for proper unicast, multicast,
3447 * promiscuous mode, and all-multi behavior.
3449 static void igb_set_rx_mode(struct net_device *netdev)
3451 struct igb_adapter *adapter = netdev_priv(netdev);
3452 struct e1000_hw *hw = &adapter->hw;
3453 unsigned int vfn = adapter->vfs_allocated_count;
3454 u32 rctl, vmolr = 0;
3457 /* Check for Promiscuous and All Multicast modes */
3458 rctl = rd32(E1000_RCTL);
3460 /* clear the effected bits */
3461 rctl &= ~(E1000_RCTL_UPE | E1000_RCTL_MPE | E1000_RCTL_VFE);
3463 if (netdev->flags & IFF_PROMISC) {
3464 rctl |= (E1000_RCTL_UPE | E1000_RCTL_MPE);
3465 vmolr |= (E1000_VMOLR_ROPE | E1000_VMOLR_MPME);
3467 if (netdev->flags & IFF_ALLMULTI) {
3468 rctl |= E1000_RCTL_MPE;
3469 vmolr |= E1000_VMOLR_MPME;
3472 * Write addresses to the MTA, if the attempt fails
3473 * then we should just turn on promiscuous mode so
3474 * that we can at least receive multicast traffic
3476 count = igb_write_mc_addr_list(netdev);
3478 rctl |= E1000_RCTL_MPE;
3479 vmolr |= E1000_VMOLR_MPME;
3481 vmolr |= E1000_VMOLR_ROMPE;
3485 * Write addresses to available RAR registers, if there is not
3486 * sufficient space to store all the addresses then enable
3487 * unicast promiscuous mode
3489 count = igb_write_uc_addr_list(netdev);
3491 rctl |= E1000_RCTL_UPE;
3492 vmolr |= E1000_VMOLR_ROPE;
3494 rctl |= E1000_RCTL_VFE;
3496 wr32(E1000_RCTL, rctl);
3499 * In order to support SR-IOV and eventually VMDq it is necessary to set
3500 * the VMOLR to enable the appropriate modes. Without this workaround
3501 * we will have issues with VLAN tag stripping not being done for frames
3502 * that are only arriving because we are the default pool
3504 if ((hw->mac.type < e1000_82576) || (hw->mac.type > e1000_i350))
3507 vmolr |= rd32(E1000_VMOLR(vfn)) &
3508 ~(E1000_VMOLR_ROPE | E1000_VMOLR_MPME | E1000_VMOLR_ROMPE);
3509 wr32(E1000_VMOLR(vfn), vmolr);
3510 igb_restore_vf_multicasts(adapter);
3513 static void igb_check_wvbr(struct igb_adapter *adapter)
3515 struct e1000_hw *hw = &adapter->hw;
3518 switch (hw->mac.type) {
3521 if (!(wvbr = rd32(E1000_WVBR)))
3528 adapter->wvbr |= wvbr;
3531 #define IGB_STAGGERED_QUEUE_OFFSET 8
3533 static void igb_spoof_check(struct igb_adapter *adapter)
3540 for(j = 0; j < adapter->vfs_allocated_count; j++) {
3541 if (adapter->wvbr & (1 << j) ||
3542 adapter->wvbr & (1 << (j + IGB_STAGGERED_QUEUE_OFFSET))) {
3543 dev_warn(&adapter->pdev->dev,
3544 "Spoof event(s) detected on VF %d\n", j);
3547 (1 << (j + IGB_STAGGERED_QUEUE_OFFSET)));
3552 /* Need to wait a few seconds after link up to get diagnostic information from
3554 static void igb_update_phy_info(unsigned long data)
3556 struct igb_adapter *adapter = (struct igb_adapter *) data;
3557 igb_get_phy_info(&adapter->hw);
3561 * igb_has_link - check shared code for link and determine up/down
3562 * @adapter: pointer to driver private info
3564 bool igb_has_link(struct igb_adapter *adapter)
3566 struct e1000_hw *hw = &adapter->hw;
3567 bool link_active = false;
3570 /* get_link_status is set on LSC (link status) interrupt or
3571 * rx sequence error interrupt. get_link_status will stay
3572 * false until the e1000_check_for_link establishes link
3573 * for copper adapters ONLY
3575 switch (hw->phy.media_type) {
3576 case e1000_media_type_copper:
3577 if (hw->mac.get_link_status) {
3578 ret_val = hw->mac.ops.check_for_link(hw);
3579 link_active = !hw->mac.get_link_status;
3584 case e1000_media_type_internal_serdes:
3585 ret_val = hw->mac.ops.check_for_link(hw);
3586 link_active = hw->mac.serdes_has_link;
3589 case e1000_media_type_unknown:
3596 static bool igb_thermal_sensor_event(struct e1000_hw *hw, u32 event)
3599 u32 ctrl_ext, thstat;
3601 /* check for thermal sensor event on i350 copper only */
3602 if (hw->mac.type == e1000_i350) {
3603 thstat = rd32(E1000_THSTAT);
3604 ctrl_ext = rd32(E1000_CTRL_EXT);
3606 if ((hw->phy.media_type == e1000_media_type_copper) &&
3607 !(ctrl_ext & E1000_CTRL_EXT_LINK_MODE_SGMII)) {
3608 ret = !!(thstat & event);
3616 * igb_watchdog - Timer Call-back
3617 * @data: pointer to adapter cast into an unsigned long
3619 static void igb_watchdog(unsigned long data)
3621 struct igb_adapter *adapter = (struct igb_adapter *)data;
3622 /* Do the rest outside of interrupt context */
3623 schedule_work(&adapter->watchdog_task);
3626 static void igb_watchdog_task(struct work_struct *work)
3628 struct igb_adapter *adapter = container_of(work,
3631 struct e1000_hw *hw = &adapter->hw;
3632 struct net_device *netdev = adapter->netdev;
3636 link = igb_has_link(adapter);
3638 /* Cancel scheduled suspend requests. */
3639 pm_runtime_resume(netdev->dev.parent);
3641 if (!netif_carrier_ok(netdev)) {
3643 hw->mac.ops.get_speed_and_duplex(hw,
3644 &adapter->link_speed,
3645 &adapter->link_duplex);
3647 ctrl = rd32(E1000_CTRL);
3648 /* Links status message must follow this format */
3649 printk(KERN_INFO "igb: %s NIC Link is Up %d Mbps %s "
3650 "Duplex, Flow Control: %s\n",
3652 adapter->link_speed,
3653 adapter->link_duplex == FULL_DUPLEX ?
3655 (ctrl & E1000_CTRL_TFCE) &&
3656 (ctrl & E1000_CTRL_RFCE) ? "RX/TX" :
3657 (ctrl & E1000_CTRL_RFCE) ? "RX" :
3658 (ctrl & E1000_CTRL_TFCE) ? "TX" : "None");
3660 /* check for thermal sensor event */
3661 if (igb_thermal_sensor_event(hw,
3662 E1000_THSTAT_LINK_THROTTLE)) {
3663 netdev_info(netdev, "The network adapter link "
3664 "speed was downshifted because it "
3668 /* adjust timeout factor according to speed/duplex */
3669 adapter->tx_timeout_factor = 1;
3670 switch (adapter->link_speed) {
3672 adapter->tx_timeout_factor = 14;
3675 /* maybe add some timeout factor ? */
3679 netif_carrier_on(netdev);
3681 igb_ping_all_vfs(adapter);
3682 igb_check_vf_rate_limit(adapter);
3684 /* link state has changed, schedule phy info update */
3685 if (!test_bit(__IGB_DOWN, &adapter->state))
3686 mod_timer(&adapter->phy_info_timer,
3687 round_jiffies(jiffies + 2 * HZ));
3690 if (netif_carrier_ok(netdev)) {
3691 adapter->link_speed = 0;
3692 adapter->link_duplex = 0;
3694 /* check for thermal sensor event */
3695 if (igb_thermal_sensor_event(hw,
3696 E1000_THSTAT_PWR_DOWN)) {
3697 netdev_err(netdev, "The network adapter was "
3698 "stopped because it overheated\n");
3701 /* Links status message must follow this format */
3702 printk(KERN_INFO "igb: %s NIC Link is Down\n",
3704 netif_carrier_off(netdev);
3706 igb_ping_all_vfs(adapter);
3708 /* link state has changed, schedule phy info update */
3709 if (!test_bit(__IGB_DOWN, &adapter->state))
3710 mod_timer(&adapter->phy_info_timer,
3711 round_jiffies(jiffies + 2 * HZ));
3713 pm_schedule_suspend(netdev->dev.parent,
3718 spin_lock(&adapter->stats64_lock);
3719 igb_update_stats(adapter, &adapter->stats64);
3720 spin_unlock(&adapter->stats64_lock);
3722 for (i = 0; i < adapter->num_tx_queues; i++) {
3723 struct igb_ring *tx_ring = adapter->tx_ring[i];
3724 if (!netif_carrier_ok(netdev)) {
3725 /* We've lost link, so the controller stops DMA,
3726 * but we've got queued Tx work that's never going
3727 * to get done, so reset controller to flush Tx.
3728 * (Do the reset outside of interrupt context). */
3729 if (igb_desc_unused(tx_ring) + 1 < tx_ring->count) {
3730 adapter->tx_timeout_count++;
3731 schedule_work(&adapter->reset_task);
3732 /* return immediately since reset is imminent */
3737 /* Force detection of hung controller every watchdog period */
3738 set_bit(IGB_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags);
3741 /* Cause software interrupt to ensure rx ring is cleaned */
3742 if (adapter->msix_entries) {
3744 for (i = 0; i < adapter->num_q_vectors; i++)
3745 eics |= adapter->q_vector[i]->eims_value;
3746 wr32(E1000_EICS, eics);
3748 wr32(E1000_ICS, E1000_ICS_RXDMT0);
3751 igb_spoof_check(adapter);
3753 /* Reset the timer */
3754 if (!test_bit(__IGB_DOWN, &adapter->state))
3755 mod_timer(&adapter->watchdog_timer,
3756 round_jiffies(jiffies + 2 * HZ));
3759 enum latency_range {
3763 latency_invalid = 255
3767 * igb_update_ring_itr - update the dynamic ITR value based on packet size
3769 * Stores a new ITR value based on strictly on packet size. This
3770 * algorithm is less sophisticated than that used in igb_update_itr,
3771 * due to the difficulty of synchronizing statistics across multiple
3772 * receive rings. The divisors and thresholds used by this function
3773 * were determined based on theoretical maximum wire speed and testing
3774 * data, in order to minimize response time while increasing bulk
3776 * This functionality is controlled by the InterruptThrottleRate module
3777 * parameter (see igb_param.c)
3778 * NOTE: This function is called only when operating in a multiqueue
3779 * receive environment.
3780 * @q_vector: pointer to q_vector
3782 static void igb_update_ring_itr(struct igb_q_vector *q_vector)
3784 int new_val = q_vector->itr_val;
3785 int avg_wire_size = 0;
3786 struct igb_adapter *adapter = q_vector->adapter;
3787 unsigned int packets;
3789 /* For non-gigabit speeds, just fix the interrupt rate at 4000
3790 * ints/sec - ITR timer value of 120 ticks.
3792 if (adapter->link_speed != SPEED_1000) {
3793 new_val = IGB_4K_ITR;
3797 packets = q_vector->rx.total_packets;
3799 avg_wire_size = q_vector->rx.total_bytes / packets;
3801 packets = q_vector->tx.total_packets;
3803 avg_wire_size = max_t(u32, avg_wire_size,
3804 q_vector->tx.total_bytes / packets);
3806 /* if avg_wire_size isn't set no work was done */
3810 /* Add 24 bytes to size to account for CRC, preamble, and gap */
3811 avg_wire_size += 24;
3813 /* Don't starve jumbo frames */
3814 avg_wire_size = min(avg_wire_size, 3000);
3816 /* Give a little boost to mid-size frames */
3817 if ((avg_wire_size > 300) && (avg_wire_size < 1200))
3818 new_val = avg_wire_size / 3;
3820 new_val = avg_wire_size / 2;
3822 /* conservative mode (itr 3) eliminates the lowest_latency setting */
3823 if (new_val < IGB_20K_ITR &&
3824 ((q_vector->rx.ring && adapter->rx_itr_setting == 3) ||
3825 (!q_vector->rx.ring && adapter->tx_itr_setting == 3)))
3826 new_val = IGB_20K_ITR;
3829 if (new_val != q_vector->itr_val) {
3830 q_vector->itr_val = new_val;
3831 q_vector->set_itr = 1;
3834 q_vector->rx.total_bytes = 0;
3835 q_vector->rx.total_packets = 0;
3836 q_vector->tx.total_bytes = 0;
3837 q_vector->tx.total_packets = 0;
3841 * igb_update_itr - update the dynamic ITR value based on statistics
3842 * Stores a new ITR value based on packets and byte
3843 * counts during the last interrupt. The advantage of per interrupt
3844 * computation is faster updates and more accurate ITR for the current
3845 * traffic pattern. Constants in this function were computed
3846 * based on theoretical maximum wire speed and thresholds were set based
3847 * on testing data as well as attempting to minimize response time
3848 * while increasing bulk throughput.
3849 * this functionality is controlled by the InterruptThrottleRate module
3850 * parameter (see igb_param.c)
3851 * NOTE: These calculations are only valid when operating in a single-
3852 * queue environment.
3853 * @q_vector: pointer to q_vector
3854 * @ring_container: ring info to update the itr for
3856 static void igb_update_itr(struct igb_q_vector *q_vector,
3857 struct igb_ring_container *ring_container)
3859 unsigned int packets = ring_container->total_packets;
3860 unsigned int bytes = ring_container->total_bytes;
3861 u8 itrval = ring_container->itr;
3863 /* no packets, exit with status unchanged */
3868 case lowest_latency:
3869 /* handle TSO and jumbo frames */
3870 if (bytes/packets > 8000)
3871 itrval = bulk_latency;
3872 else if ((packets < 5) && (bytes > 512))
3873 itrval = low_latency;
3875 case low_latency: /* 50 usec aka 20000 ints/s */
3876 if (bytes > 10000) {
3877 /* this if handles the TSO accounting */
3878 if (bytes/packets > 8000) {
3879 itrval = bulk_latency;
3880 } else if ((packets < 10) || ((bytes/packets) > 1200)) {
3881 itrval = bulk_latency;
3882 } else if ((packets > 35)) {
3883 itrval = lowest_latency;
3885 } else if (bytes/packets > 2000) {
3886 itrval = bulk_latency;
3887 } else if (packets <= 2 && bytes < 512) {
3888 itrval = lowest_latency;
3891 case bulk_latency: /* 250 usec aka 4000 ints/s */
3892 if (bytes > 25000) {
3894 itrval = low_latency;
3895 } else if (bytes < 1500) {
3896 itrval = low_latency;
3901 /* clear work counters since we have the values we need */
3902 ring_container->total_bytes = 0;
3903 ring_container->total_packets = 0;
3905 /* write updated itr to ring container */
3906 ring_container->itr = itrval;
3909 static void igb_set_itr(struct igb_q_vector *q_vector)
3911 struct igb_adapter *adapter = q_vector->adapter;
3912 u32 new_itr = q_vector->itr_val;
3915 /* for non-gigabit speeds, just fix the interrupt rate at 4000 */
3916 if (adapter->link_speed != SPEED_1000) {
3918 new_itr = IGB_4K_ITR;
3922 igb_update_itr(q_vector, &q_vector->tx);
3923 igb_update_itr(q_vector, &q_vector->rx);
3925 current_itr = max(q_vector->rx.itr, q_vector->tx.itr);
3927 /* conservative mode (itr 3) eliminates the lowest_latency setting */
3928 if (current_itr == lowest_latency &&
3929 ((q_vector->rx.ring && adapter->rx_itr_setting == 3) ||
3930 (!q_vector->rx.ring && adapter->tx_itr_setting == 3)))
3931 current_itr = low_latency;
3933 switch (current_itr) {
3934 /* counts and packets in update_itr are dependent on these numbers */
3935 case lowest_latency:
3936 new_itr = IGB_70K_ITR; /* 70,000 ints/sec */
3939 new_itr = IGB_20K_ITR; /* 20,000 ints/sec */
3942 new_itr = IGB_4K_ITR; /* 4,000 ints/sec */
3949 if (new_itr != q_vector->itr_val) {
3950 /* this attempts to bias the interrupt rate towards Bulk
3951 * by adding intermediate steps when interrupt rate is
3953 new_itr = new_itr > q_vector->itr_val ?
3954 max((new_itr * q_vector->itr_val) /
3955 (new_itr + (q_vector->itr_val >> 2)),
3958 /* Don't write the value here; it resets the adapter's
3959 * internal timer, and causes us to delay far longer than
3960 * we should between interrupts. Instead, we write the ITR
3961 * value at the beginning of the next interrupt so the timing
3962 * ends up being correct.
3964 q_vector->itr_val = new_itr;
3965 q_vector->set_itr = 1;
3969 static void igb_tx_ctxtdesc(struct igb_ring *tx_ring, u32 vlan_macip_lens,
3970 u32 type_tucmd, u32 mss_l4len_idx)
3972 struct e1000_adv_tx_context_desc *context_desc;
3973 u16 i = tx_ring->next_to_use;
3975 context_desc = IGB_TX_CTXTDESC(tx_ring, i);
3978 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
3980 /* set bits to identify this as an advanced context descriptor */
3981 type_tucmd |= E1000_TXD_CMD_DEXT | E1000_ADVTXD_DTYP_CTXT;
3983 /* For 82575, context index must be unique per ring. */
3984 if (test_bit(IGB_RING_FLAG_TX_CTX_IDX, &tx_ring->flags))
3985 mss_l4len_idx |= tx_ring->reg_idx << 4;
3987 context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens);
3988 context_desc->seqnum_seed = 0;
3989 context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd);
3990 context_desc->mss_l4len_idx = cpu_to_le32(mss_l4len_idx);
3993 static int igb_tso(struct igb_ring *tx_ring,
3994 struct igb_tx_buffer *first,
3997 struct sk_buff *skb = first->skb;
3998 u32 vlan_macip_lens, type_tucmd;
3999 u32 mss_l4len_idx, l4len;
4001 if (skb->ip_summed != CHECKSUM_PARTIAL)
4004 if (!skb_is_gso(skb))
4007 if (skb_header_cloned(skb)) {
4008 int err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
4013 /* ADV DTYP TUCMD MKRLOC/ISCSIHEDLEN */
4014 type_tucmd = E1000_ADVTXD_TUCMD_L4T_TCP;
4016 if (first->protocol == __constant_htons(ETH_P_IP)) {
4017 struct iphdr *iph = ip_hdr(skb);
4020 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
4024 type_tucmd |= E1000_ADVTXD_TUCMD_IPV4;
4025 first->tx_flags |= IGB_TX_FLAGS_TSO |
4028 } else if (skb_is_gso_v6(skb)) {
4029 ipv6_hdr(skb)->payload_len = 0;
4030 tcp_hdr(skb)->check = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
4031 &ipv6_hdr(skb)->daddr,
4033 first->tx_flags |= IGB_TX_FLAGS_TSO |
4037 /* compute header lengths */
4038 l4len = tcp_hdrlen(skb);
4039 *hdr_len = skb_transport_offset(skb) + l4len;
4041 /* update gso size and bytecount with header size */
4042 first->gso_segs = skb_shinfo(skb)->gso_segs;
4043 first->bytecount += (first->gso_segs - 1) * *hdr_len;
4046 mss_l4len_idx = l4len << E1000_ADVTXD_L4LEN_SHIFT;
4047 mss_l4len_idx |= skb_shinfo(skb)->gso_size << E1000_ADVTXD_MSS_SHIFT;
4049 /* VLAN MACLEN IPLEN */
4050 vlan_macip_lens = skb_network_header_len(skb);
4051 vlan_macip_lens |= skb_network_offset(skb) << E1000_ADVTXD_MACLEN_SHIFT;
4052 vlan_macip_lens |= first->tx_flags & IGB_TX_FLAGS_VLAN_MASK;
4054 igb_tx_ctxtdesc(tx_ring, vlan_macip_lens, type_tucmd, mss_l4len_idx);
4059 static void igb_tx_csum(struct igb_ring *tx_ring, struct igb_tx_buffer *first)
4061 struct sk_buff *skb = first->skb;
4062 u32 vlan_macip_lens = 0;
4063 u32 mss_l4len_idx = 0;
4066 if (skb->ip_summed != CHECKSUM_PARTIAL) {
4067 if (!(first->tx_flags & IGB_TX_FLAGS_VLAN))
4071 switch (first->protocol) {
4072 case __constant_htons(ETH_P_IP):
4073 vlan_macip_lens |= skb_network_header_len(skb);
4074 type_tucmd |= E1000_ADVTXD_TUCMD_IPV4;
4075 l4_hdr = ip_hdr(skb)->protocol;
4077 case __constant_htons(ETH_P_IPV6):
4078 vlan_macip_lens |= skb_network_header_len(skb);
4079 l4_hdr = ipv6_hdr(skb)->nexthdr;
4082 if (unlikely(net_ratelimit())) {
4083 dev_warn(tx_ring->dev,
4084 "partial checksum but proto=%x!\n",
4092 type_tucmd |= E1000_ADVTXD_TUCMD_L4T_TCP;
4093 mss_l4len_idx = tcp_hdrlen(skb) <<
4094 E1000_ADVTXD_L4LEN_SHIFT;
4097 type_tucmd |= E1000_ADVTXD_TUCMD_L4T_SCTP;
4098 mss_l4len_idx = sizeof(struct sctphdr) <<
4099 E1000_ADVTXD_L4LEN_SHIFT;
4102 mss_l4len_idx = sizeof(struct udphdr) <<
4103 E1000_ADVTXD_L4LEN_SHIFT;
4106 if (unlikely(net_ratelimit())) {
4107 dev_warn(tx_ring->dev,
4108 "partial checksum but l4 proto=%x!\n",
4114 /* update TX checksum flag */
4115 first->tx_flags |= IGB_TX_FLAGS_CSUM;
4118 vlan_macip_lens |= skb_network_offset(skb) << E1000_ADVTXD_MACLEN_SHIFT;
4119 vlan_macip_lens |= first->tx_flags & IGB_TX_FLAGS_VLAN_MASK;
4121 igb_tx_ctxtdesc(tx_ring, vlan_macip_lens, type_tucmd, mss_l4len_idx);
4124 #define IGB_SET_FLAG(_input, _flag, _result) \
4125 ((_flag <= _result) ? \
4126 ((u32)(_input & _flag) * (_result / _flag)) : \
4127 ((u32)(_input & _flag) / (_flag / _result)))
4129 static u32 igb_tx_cmd_type(struct sk_buff *skb, u32 tx_flags)
4131 /* set type for advanced descriptor with frame checksum insertion */
4132 u32 cmd_type = E1000_ADVTXD_DTYP_DATA |
4133 E1000_ADVTXD_DCMD_DEXT |
4134 E1000_ADVTXD_DCMD_IFCS;
4136 /* set HW vlan bit if vlan is present */
4137 cmd_type |= IGB_SET_FLAG(tx_flags, IGB_TX_FLAGS_VLAN,
4138 (E1000_ADVTXD_DCMD_VLE));
4140 /* set segmentation bits for TSO */
4141 cmd_type |= IGB_SET_FLAG(tx_flags, IGB_TX_FLAGS_TSO,
4142 (E1000_ADVTXD_DCMD_TSE));
4144 /* set timestamp bit if present */
4145 cmd_type |= IGB_SET_FLAG(tx_flags, IGB_TX_FLAGS_TSTAMP,
4146 (E1000_ADVTXD_MAC_TSTAMP));
4148 /* insert frame checksum */
4149 cmd_type ^= IGB_SET_FLAG(skb->no_fcs, 1, E1000_ADVTXD_DCMD_IFCS);
4154 static void igb_tx_olinfo_status(struct igb_ring *tx_ring,
4155 union e1000_adv_tx_desc *tx_desc,
4156 u32 tx_flags, unsigned int paylen)
4158 u32 olinfo_status = paylen << E1000_ADVTXD_PAYLEN_SHIFT;
4160 /* 82575 requires a unique index per ring */
4161 if (test_bit(IGB_RING_FLAG_TX_CTX_IDX, &tx_ring->flags))
4162 olinfo_status |= tx_ring->reg_idx << 4;
4164 /* insert L4 checksum */
4165 olinfo_status |= IGB_SET_FLAG(tx_flags,
4167 (E1000_TXD_POPTS_TXSM << 8));
4169 /* insert IPv4 checksum */
4170 olinfo_status |= IGB_SET_FLAG(tx_flags,
4172 (E1000_TXD_POPTS_IXSM << 8));
4174 tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status);
4178 * The largest size we can write to the descriptor is 65535. In order to
4179 * maintain a power of two alignment we have to limit ourselves to 32K.
4181 #define IGB_MAX_TXD_PWR 15
4182 #define IGB_MAX_DATA_PER_TXD (1<<IGB_MAX_TXD_PWR)
4184 static void igb_tx_map(struct igb_ring *tx_ring,
4185 struct igb_tx_buffer *first,
4188 struct sk_buff *skb = first->skb;
4189 struct igb_tx_buffer *tx_buffer;
4190 union e1000_adv_tx_desc *tx_desc;
4192 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[0];
4193 unsigned int data_len = skb->data_len;
4194 unsigned int size = skb_headlen(skb);
4195 unsigned int paylen = skb->len - hdr_len;
4196 u32 tx_flags = first->tx_flags;
4197 u32 cmd_type = igb_tx_cmd_type(skb, tx_flags);
4198 u16 i = tx_ring->next_to_use;
4200 tx_desc = IGB_TX_DESC(tx_ring, i);
4202 igb_tx_olinfo_status(tx_ring, tx_desc, tx_flags, paylen);
4204 dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
4205 if (dma_mapping_error(tx_ring->dev, dma))
4208 /* record length, and DMA address */
4209 dma_unmap_len_set(first, len, size);
4210 dma_unmap_addr_set(first, dma, dma);
4211 tx_desc->read.buffer_addr = cpu_to_le64(dma);
4214 while (unlikely(size > IGB_MAX_DATA_PER_TXD)) {
4215 tx_desc->read.cmd_type_len =
4216 cpu_to_le32(cmd_type ^ IGB_MAX_DATA_PER_TXD);
4220 if (i == tx_ring->count) {
4221 tx_desc = IGB_TX_DESC(tx_ring, 0);
4225 dma += IGB_MAX_DATA_PER_TXD;
4226 size -= IGB_MAX_DATA_PER_TXD;
4228 tx_desc->read.olinfo_status = 0;
4229 tx_desc->read.buffer_addr = cpu_to_le64(dma);
4232 if (likely(!data_len))
4235 tx_desc->read.cmd_type_len = cpu_to_le32(cmd_type ^ size);
4239 if (i == tx_ring->count) {
4240 tx_desc = IGB_TX_DESC(tx_ring, 0);
4244 size = skb_frag_size(frag);
4247 dma = skb_frag_dma_map(tx_ring->dev, frag, 0,
4248 size, DMA_TO_DEVICE);
4249 if (dma_mapping_error(tx_ring->dev, dma))
4252 tx_buffer = &tx_ring->tx_buffer_info[i];
4253 dma_unmap_len_set(tx_buffer, len, size);
4254 dma_unmap_addr_set(tx_buffer, dma, dma);
4256 tx_desc->read.olinfo_status = 0;
4257 tx_desc->read.buffer_addr = cpu_to_le64(dma);
4262 netdev_tx_sent_queue(txring_txq(tx_ring), first->bytecount);
4264 /* write last descriptor with RS and EOP bits */
4265 cmd_type |= size | IGB_TXD_DCMD;
4266 tx_desc->read.cmd_type_len = cpu_to_le32(cmd_type);
4268 /* set the timestamp */
4269 first->time_stamp = jiffies;
4272 * Force memory writes to complete before letting h/w know there
4273 * are new descriptors to fetch. (Only applicable for weak-ordered
4274 * memory model archs, such as IA-64).
4276 * We also need this memory barrier to make certain all of the
4277 * status bits have been updated before next_to_watch is written.
4281 /* set next_to_watch value indicating a packet is present */
4282 first->next_to_watch = tx_desc;
4285 if (i == tx_ring->count)
4288 tx_ring->next_to_use = i;
4290 writel(i, tx_ring->tail);
4292 /* we need this if more than one processor can write to our tail
4293 * at a time, it syncronizes IO on IA64/Altix systems */
4299 dev_err(tx_ring->dev, "TX DMA map failed\n");
4301 /* clear dma mappings for failed tx_buffer_info map */
4303 tx_buffer = &tx_ring->tx_buffer_info[i];
4304 igb_unmap_and_free_tx_resource(tx_ring, tx_buffer);
4305 if (tx_buffer == first)
4312 tx_ring->next_to_use = i;
4315 static int __igb_maybe_stop_tx(struct igb_ring *tx_ring, const u16 size)
4317 struct net_device *netdev = tx_ring->netdev;
4319 netif_stop_subqueue(netdev, tx_ring->queue_index);
4321 /* Herbert's original patch had:
4322 * smp_mb__after_netif_stop_queue();
4323 * but since that doesn't exist yet, just open code it. */
4326 /* We need to check again in a case another CPU has just
4327 * made room available. */
4328 if (igb_desc_unused(tx_ring) < size)
4332 netif_wake_subqueue(netdev, tx_ring->queue_index);
4334 u64_stats_update_begin(&tx_ring->tx_syncp2);
4335 tx_ring->tx_stats.restart_queue2++;
4336 u64_stats_update_end(&tx_ring->tx_syncp2);
4341 static inline int igb_maybe_stop_tx(struct igb_ring *tx_ring, const u16 size)
4343 if (igb_desc_unused(tx_ring) >= size)
4345 return __igb_maybe_stop_tx(tx_ring, size);
4348 netdev_tx_t igb_xmit_frame_ring(struct sk_buff *skb,
4349 struct igb_ring *tx_ring)
4351 struct igb_adapter *adapter = netdev_priv(tx_ring->netdev);
4352 struct igb_tx_buffer *first;
4355 __be16 protocol = vlan_get_protocol(skb);
4358 /* need: 1 descriptor per page,
4359 * + 2 desc gap to keep tail from touching head,
4360 * + 1 desc for skb->data,
4361 * + 1 desc for context descriptor,
4362 * otherwise try next time */
4363 if (igb_maybe_stop_tx(tx_ring, skb_shinfo(skb)->nr_frags + 4)) {
4364 /* this is a hard error */
4365 return NETDEV_TX_BUSY;
4368 /* record the location of the first descriptor for this packet */
4369 first = &tx_ring->tx_buffer_info[tx_ring->next_to_use];
4371 first->bytecount = skb->len;
4372 first->gso_segs = 1;
4374 if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
4375 !(adapter->ptp_tx_skb))) {
4376 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
4377 tx_flags |= IGB_TX_FLAGS_TSTAMP;
4379 adapter->ptp_tx_skb = skb_get(skb);
4380 if (adapter->hw.mac.type == e1000_82576)
4381 schedule_work(&adapter->ptp_tx_work);
4384 if (vlan_tx_tag_present(skb)) {
4385 tx_flags |= IGB_TX_FLAGS_VLAN;
4386 tx_flags |= (vlan_tx_tag_get(skb) << IGB_TX_FLAGS_VLAN_SHIFT);
4389 /* record initial flags and protocol */
4390 first->tx_flags = tx_flags;
4391 first->protocol = protocol;
4393 tso = igb_tso(tx_ring, first, &hdr_len);
4397 igb_tx_csum(tx_ring, first);
4399 igb_tx_map(tx_ring, first, hdr_len);
4401 /* Make sure there is space in the ring for the next send. */
4402 igb_maybe_stop_tx(tx_ring, MAX_SKB_FRAGS + 4);
4404 return NETDEV_TX_OK;
4407 igb_unmap_and_free_tx_resource(tx_ring, first);
4409 return NETDEV_TX_OK;
4412 static inline struct igb_ring *igb_tx_queue_mapping(struct igb_adapter *adapter,
4413 struct sk_buff *skb)
4415 unsigned int r_idx = skb->queue_mapping;
4417 if (r_idx >= adapter->num_tx_queues)
4418 r_idx = r_idx % adapter->num_tx_queues;
4420 return adapter->tx_ring[r_idx];
4423 static netdev_tx_t igb_xmit_frame(struct sk_buff *skb,
4424 struct net_device *netdev)
4426 struct igb_adapter *adapter = netdev_priv(netdev);
4428 if (test_bit(__IGB_DOWN, &adapter->state)) {
4429 dev_kfree_skb_any(skb);
4430 return NETDEV_TX_OK;
4433 if (skb->len <= 0) {
4434 dev_kfree_skb_any(skb);
4435 return NETDEV_TX_OK;
4439 * The minimum packet size with TCTL.PSP set is 17 so pad the skb
4440 * in order to meet this minimum size requirement.
4442 if (unlikely(skb->len < 17)) {
4443 if (skb_pad(skb, 17 - skb->len))
4444 return NETDEV_TX_OK;
4446 skb_set_tail_pointer(skb, 17);
4449 return igb_xmit_frame_ring(skb, igb_tx_queue_mapping(adapter, skb));
4453 * igb_tx_timeout - Respond to a Tx Hang
4454 * @netdev: network interface device structure
4456 static void igb_tx_timeout(struct net_device *netdev)
4458 struct igb_adapter *adapter = netdev_priv(netdev);
4459 struct e1000_hw *hw = &adapter->hw;
4461 /* Do the reset outside of interrupt context */
4462 adapter->tx_timeout_count++;
4464 if (hw->mac.type >= e1000_82580)
4465 hw->dev_spec._82575.global_device_reset = true;
4467 schedule_work(&adapter->reset_task);
4469 (adapter->eims_enable_mask & ~adapter->eims_other));
4472 static void igb_reset_task(struct work_struct *work)
4474 struct igb_adapter *adapter;
4475 adapter = container_of(work, struct igb_adapter, reset_task);
4478 netdev_err(adapter->netdev, "Reset adapter\n");
4479 igb_reinit_locked(adapter);
4483 * igb_get_stats64 - Get System Network Statistics
4484 * @netdev: network interface device structure
4485 * @stats: rtnl_link_stats64 pointer
4488 static struct rtnl_link_stats64 *igb_get_stats64(struct net_device *netdev,
4489 struct rtnl_link_stats64 *stats)
4491 struct igb_adapter *adapter = netdev_priv(netdev);
4493 spin_lock(&adapter->stats64_lock);
4494 igb_update_stats(adapter, &adapter->stats64);
4495 memcpy(stats, &adapter->stats64, sizeof(*stats));
4496 spin_unlock(&adapter->stats64_lock);
4502 * igb_change_mtu - Change the Maximum Transfer Unit
4503 * @netdev: network interface device structure
4504 * @new_mtu: new value for maximum frame size
4506 * Returns 0 on success, negative on failure
4508 static int igb_change_mtu(struct net_device *netdev, int new_mtu)
4510 struct igb_adapter *adapter = netdev_priv(netdev);
4511 struct pci_dev *pdev = adapter->pdev;
4512 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
4514 if ((new_mtu < 68) || (max_frame > MAX_JUMBO_FRAME_SIZE)) {
4515 dev_err(&pdev->dev, "Invalid MTU setting\n");
4519 #define MAX_STD_JUMBO_FRAME_SIZE 9238
4520 if (max_frame > MAX_STD_JUMBO_FRAME_SIZE) {
4521 dev_err(&pdev->dev, "MTU > 9216 not supported.\n");
4525 while (test_and_set_bit(__IGB_RESETTING, &adapter->state))
4528 /* igb_down has a dependency on max_frame_size */
4529 adapter->max_frame_size = max_frame;
4531 if (netif_running(netdev))
4534 dev_info(&pdev->dev, "changing MTU from %d to %d\n",
4535 netdev->mtu, new_mtu);
4536 netdev->mtu = new_mtu;
4538 if (netif_running(netdev))
4543 clear_bit(__IGB_RESETTING, &adapter->state);
4549 * igb_update_stats - Update the board statistics counters
4550 * @adapter: board private structure
4553 void igb_update_stats(struct igb_adapter *adapter,
4554 struct rtnl_link_stats64 *net_stats)
4556 struct e1000_hw *hw = &adapter->hw;
4557 struct pci_dev *pdev = adapter->pdev;
4563 u64 _bytes, _packets;
4565 #define PHY_IDLE_ERROR_COUNT_MASK 0x00FF
4568 * Prevent stats update while adapter is being reset, or if the pci
4569 * connection is down.
4571 if (adapter->link_speed == 0)
4573 if (pci_channel_offline(pdev))
4578 for (i = 0; i < adapter->num_rx_queues; i++) {
4579 u32 rqdpc = rd32(E1000_RQDPC(i));
4580 struct igb_ring *ring = adapter->rx_ring[i];
4583 ring->rx_stats.drops += rqdpc;
4584 net_stats->rx_fifo_errors += rqdpc;
4588 start = u64_stats_fetch_begin_bh(&ring->rx_syncp);
4589 _bytes = ring->rx_stats.bytes;
4590 _packets = ring->rx_stats.packets;
4591 } while (u64_stats_fetch_retry_bh(&ring->rx_syncp, start));
4593 packets += _packets;
4596 net_stats->rx_bytes = bytes;
4597 net_stats->rx_packets = packets;
4601 for (i = 0; i < adapter->num_tx_queues; i++) {
4602 struct igb_ring *ring = adapter->tx_ring[i];
4604 start = u64_stats_fetch_begin_bh(&ring->tx_syncp);
4605 _bytes = ring->tx_stats.bytes;
4606 _packets = ring->tx_stats.packets;
4607 } while (u64_stats_fetch_retry_bh(&ring->tx_syncp, start));
4609 packets += _packets;
4611 net_stats->tx_bytes = bytes;
4612 net_stats->tx_packets = packets;
4614 /* read stats registers */
4615 adapter->stats.crcerrs += rd32(E1000_CRCERRS);
4616 adapter->stats.gprc += rd32(E1000_GPRC);
4617 adapter->stats.gorc += rd32(E1000_GORCL);
4618 rd32(E1000_GORCH); /* clear GORCL */
4619 adapter->stats.bprc += rd32(E1000_BPRC);
4620 adapter->stats.mprc += rd32(E1000_MPRC);
4621 adapter->stats.roc += rd32(E1000_ROC);
4623 adapter->stats.prc64 += rd32(E1000_PRC64);
4624 adapter->stats.prc127 += rd32(E1000_PRC127);
4625 adapter->stats.prc255 += rd32(E1000_PRC255);
4626 adapter->stats.prc511 += rd32(E1000_PRC511);
4627 adapter->stats.prc1023 += rd32(E1000_PRC1023);
4628 adapter->stats.prc1522 += rd32(E1000_PRC1522);
4629 adapter->stats.symerrs += rd32(E1000_SYMERRS);
4630 adapter->stats.sec += rd32(E1000_SEC);
4632 mpc = rd32(E1000_MPC);
4633 adapter->stats.mpc += mpc;
4634 net_stats->rx_fifo_errors += mpc;
4635 adapter->stats.scc += rd32(E1000_SCC);
4636 adapter->stats.ecol += rd32(E1000_ECOL);
4637 adapter->stats.mcc += rd32(E1000_MCC);
4638 adapter->stats.latecol += rd32(E1000_LATECOL);
4639 adapter->stats.dc += rd32(E1000_DC);
4640 adapter->stats.rlec += rd32(E1000_RLEC);
4641 adapter->stats.xonrxc += rd32(E1000_XONRXC);
4642 adapter->stats.xontxc += rd32(E1000_XONTXC);
4643 adapter->stats.xoffrxc += rd32(E1000_XOFFRXC);
4644 adapter->stats.xofftxc += rd32(E1000_XOFFTXC);
4645 adapter->stats.fcruc += rd32(E1000_FCRUC);
4646 adapter->stats.gptc += rd32(E1000_GPTC);
4647 adapter->stats.gotc += rd32(E1000_GOTCL);
4648 rd32(E1000_GOTCH); /* clear GOTCL */
4649 adapter->stats.rnbc += rd32(E1000_RNBC);
4650 adapter->stats.ruc += rd32(E1000_RUC);
4651 adapter->stats.rfc += rd32(E1000_RFC);
4652 adapter->stats.rjc += rd32(E1000_RJC);
4653 adapter->stats.tor += rd32(E1000_TORH);
4654 adapter->stats.tot += rd32(E1000_TOTH);
4655 adapter->stats.tpr += rd32(E1000_TPR);
4657 adapter->stats.ptc64 += rd32(E1000_PTC64);
4658 adapter->stats.ptc127 += rd32(E1000_PTC127);
4659 adapter->stats.ptc255 += rd32(E1000_PTC255);
4660 adapter->stats.ptc511 += rd32(E1000_PTC511);
4661 adapter->stats.ptc1023 += rd32(E1000_PTC1023);
4662 adapter->stats.ptc1522 += rd32(E1000_PTC1522);
4664 adapter->stats.mptc += rd32(E1000_MPTC);
4665 adapter->stats.bptc += rd32(E1000_BPTC);
4667 adapter->stats.tpt += rd32(E1000_TPT);
4668 adapter->stats.colc += rd32(E1000_COLC);
4670 adapter->stats.algnerrc += rd32(E1000_ALGNERRC);
4671 /* read internal phy specific stats */
4672 reg = rd32(E1000_CTRL_EXT);
4673 if (!(reg & E1000_CTRL_EXT_LINK_MODE_MASK)) {
4674 adapter->stats.rxerrc += rd32(E1000_RXERRC);
4676 /* this stat has invalid values on i210/i211 */
4677 if ((hw->mac.type != e1000_i210) &&
4678 (hw->mac.type != e1000_i211))
4679 adapter->stats.tncrs += rd32(E1000_TNCRS);
4682 adapter->stats.tsctc += rd32(E1000_TSCTC);
4683 adapter->stats.tsctfc += rd32(E1000_TSCTFC);
4685 adapter->stats.iac += rd32(E1000_IAC);
4686 adapter->stats.icrxoc += rd32(E1000_ICRXOC);
4687 adapter->stats.icrxptc += rd32(E1000_ICRXPTC);
4688 adapter->stats.icrxatc += rd32(E1000_ICRXATC);
4689 adapter->stats.ictxptc += rd32(E1000_ICTXPTC);
4690 adapter->stats.ictxatc += rd32(E1000_ICTXATC);
4691 adapter->stats.ictxqec += rd32(E1000_ICTXQEC);
4692 adapter->stats.ictxqmtc += rd32(E1000_ICTXQMTC);
4693 adapter->stats.icrxdmtc += rd32(E1000_ICRXDMTC);
4695 /* Fill out the OS statistics structure */
4696 net_stats->multicast = adapter->stats.mprc;
4697 net_stats->collisions = adapter->stats.colc;
4701 /* RLEC on some newer hardware can be incorrect so build
4702 * our own version based on RUC and ROC */
4703 net_stats->rx_errors = adapter->stats.rxerrc +
4704 adapter->stats.crcerrs + adapter->stats.algnerrc +
4705 adapter->stats.ruc + adapter->stats.roc +
4706 adapter->stats.cexterr;
4707 net_stats->rx_length_errors = adapter->stats.ruc +
4709 net_stats->rx_crc_errors = adapter->stats.crcerrs;
4710 net_stats->rx_frame_errors = adapter->stats.algnerrc;
4711 net_stats->rx_missed_errors = adapter->stats.mpc;
4714 net_stats->tx_errors = adapter->stats.ecol +
4715 adapter->stats.latecol;
4716 net_stats->tx_aborted_errors = adapter->stats.ecol;
4717 net_stats->tx_window_errors = adapter->stats.latecol;
4718 net_stats->tx_carrier_errors = adapter->stats.tncrs;
4720 /* Tx Dropped needs to be maintained elsewhere */
4723 if (hw->phy.media_type == e1000_media_type_copper) {
4724 if ((adapter->link_speed == SPEED_1000) &&
4725 (!igb_read_phy_reg(hw, PHY_1000T_STATUS, &phy_tmp))) {
4726 phy_tmp &= PHY_IDLE_ERROR_COUNT_MASK;
4727 adapter->phy_stats.idle_errors += phy_tmp;
4731 /* Management Stats */
4732 adapter->stats.mgptc += rd32(E1000_MGTPTC);
4733 adapter->stats.mgprc += rd32(E1000_MGTPRC);
4734 adapter->stats.mgpdc += rd32(E1000_MGTPDC);
4737 reg = rd32(E1000_MANC);
4738 if (reg & E1000_MANC_EN_BMC2OS) {
4739 adapter->stats.o2bgptc += rd32(E1000_O2BGPTC);
4740 adapter->stats.o2bspc += rd32(E1000_O2BSPC);
4741 adapter->stats.b2ospc += rd32(E1000_B2OSPC);
4742 adapter->stats.b2ogprc += rd32(E1000_B2OGPRC);
4746 static irqreturn_t igb_msix_other(int irq, void *data)
4748 struct igb_adapter *adapter = data;
4749 struct e1000_hw *hw = &adapter->hw;
4750 u32 icr = rd32(E1000_ICR);
4751 /* reading ICR causes bit 31 of EICR to be cleared */
4753 if (icr & E1000_ICR_DRSTA)
4754 schedule_work(&adapter->reset_task);
4756 if (icr & E1000_ICR_DOUTSYNC) {
4757 /* HW is reporting DMA is out of sync */
4758 adapter->stats.doosync++;
4759 /* The DMA Out of Sync is also indication of a spoof event
4760 * in IOV mode. Check the Wrong VM Behavior register to
4761 * see if it is really a spoof event. */
4762 igb_check_wvbr(adapter);
4765 /* Check for a mailbox event */
4766 if (icr & E1000_ICR_VMMB)
4767 igb_msg_task(adapter);
4769 if (icr & E1000_ICR_LSC) {
4770 hw->mac.get_link_status = 1;
4771 /* guard against interrupt when we're going down */
4772 if (!test_bit(__IGB_DOWN, &adapter->state))
4773 mod_timer(&adapter->watchdog_timer, jiffies + 1);
4776 if (icr & E1000_ICR_TS) {
4777 u32 tsicr = rd32(E1000_TSICR);
4779 if (tsicr & E1000_TSICR_TXTS) {
4780 /* acknowledge the interrupt */
4781 wr32(E1000_TSICR, E1000_TSICR_TXTS);
4782 /* retrieve hardware timestamp */
4783 schedule_work(&adapter->ptp_tx_work);
4787 wr32(E1000_EIMS, adapter->eims_other);
4792 static void igb_write_itr(struct igb_q_vector *q_vector)
4794 struct igb_adapter *adapter = q_vector->adapter;
4795 u32 itr_val = q_vector->itr_val & 0x7FFC;
4797 if (!q_vector->set_itr)
4803 if (adapter->hw.mac.type == e1000_82575)
4804 itr_val |= itr_val << 16;
4806 itr_val |= E1000_EITR_CNT_IGNR;
4808 writel(itr_val, q_vector->itr_register);
4809 q_vector->set_itr = 0;
4812 static irqreturn_t igb_msix_ring(int irq, void *data)
4814 struct igb_q_vector *q_vector = data;
4816 /* Write the ITR value calculated from the previous interrupt. */
4817 igb_write_itr(q_vector);
4819 napi_schedule(&q_vector->napi);
4824 #ifdef CONFIG_IGB_DCA
4825 static void igb_update_tx_dca(struct igb_adapter *adapter,
4826 struct igb_ring *tx_ring,
4829 struct e1000_hw *hw = &adapter->hw;
4830 u32 txctrl = dca3_get_tag(tx_ring->dev, cpu);
4832 if (hw->mac.type != e1000_82575)
4833 txctrl <<= E1000_DCA_TXCTRL_CPUID_SHIFT;
4836 * We can enable relaxed ordering for reads, but not writes when
4837 * DCA is enabled. This is due to a known issue in some chipsets
4838 * which will cause the DCA tag to be cleared.
4840 txctrl |= E1000_DCA_TXCTRL_DESC_RRO_EN |
4841 E1000_DCA_TXCTRL_DATA_RRO_EN |
4842 E1000_DCA_TXCTRL_DESC_DCA_EN;
4844 wr32(E1000_DCA_TXCTRL(tx_ring->reg_idx), txctrl);
4847 static void igb_update_rx_dca(struct igb_adapter *adapter,
4848 struct igb_ring *rx_ring,
4851 struct e1000_hw *hw = &adapter->hw;
4852 u32 rxctrl = dca3_get_tag(&adapter->pdev->dev, cpu);
4854 if (hw->mac.type != e1000_82575)
4855 rxctrl <<= E1000_DCA_RXCTRL_CPUID_SHIFT;
4858 * We can enable relaxed ordering for reads, but not writes when
4859 * DCA is enabled. This is due to a known issue in some chipsets
4860 * which will cause the DCA tag to be cleared.
4862 rxctrl |= E1000_DCA_RXCTRL_DESC_RRO_EN |
4863 E1000_DCA_RXCTRL_DESC_DCA_EN;
4865 wr32(E1000_DCA_RXCTRL(rx_ring->reg_idx), rxctrl);
4868 static void igb_update_dca(struct igb_q_vector *q_vector)
4870 struct igb_adapter *adapter = q_vector->adapter;
4871 int cpu = get_cpu();
4873 if (q_vector->cpu == cpu)
4876 if (q_vector->tx.ring)
4877 igb_update_tx_dca(adapter, q_vector->tx.ring, cpu);
4879 if (q_vector->rx.ring)
4880 igb_update_rx_dca(adapter, q_vector->rx.ring, cpu);
4882 q_vector->cpu = cpu;
4887 static void igb_setup_dca(struct igb_adapter *adapter)
4889 struct e1000_hw *hw = &adapter->hw;
4892 if (!(adapter->flags & IGB_FLAG_DCA_ENABLED))
4895 /* Always use CB2 mode, difference is masked in the CB driver. */
4896 wr32(E1000_DCA_CTRL, E1000_DCA_CTRL_DCA_MODE_CB2);
4898 for (i = 0; i < adapter->num_q_vectors; i++) {
4899 adapter->q_vector[i]->cpu = -1;
4900 igb_update_dca(adapter->q_vector[i]);
4904 static int __igb_notify_dca(struct device *dev, void *data)
4906 struct net_device *netdev = dev_get_drvdata(dev);
4907 struct igb_adapter *adapter = netdev_priv(netdev);
4908 struct pci_dev *pdev = adapter->pdev;
4909 struct e1000_hw *hw = &adapter->hw;
4910 unsigned long event = *(unsigned long *)data;
4913 case DCA_PROVIDER_ADD:
4914 /* if already enabled, don't do it again */
4915 if (adapter->flags & IGB_FLAG_DCA_ENABLED)
4917 if (dca_add_requester(dev) == 0) {
4918 adapter->flags |= IGB_FLAG_DCA_ENABLED;
4919 dev_info(&pdev->dev, "DCA enabled\n");
4920 igb_setup_dca(adapter);
4923 /* Fall Through since DCA is disabled. */
4924 case DCA_PROVIDER_REMOVE:
4925 if (adapter->flags & IGB_FLAG_DCA_ENABLED) {
4926 /* without this a class_device is left
4927 * hanging around in the sysfs model */
4928 dca_remove_requester(dev);
4929 dev_info(&pdev->dev, "DCA disabled\n");
4930 adapter->flags &= ~IGB_FLAG_DCA_ENABLED;
4931 wr32(E1000_DCA_CTRL, E1000_DCA_CTRL_DCA_MODE_DISABLE);
4939 static int igb_notify_dca(struct notifier_block *nb, unsigned long event,
4944 ret_val = driver_for_each_device(&igb_driver.driver, NULL, &event,
4947 return ret_val ? NOTIFY_BAD : NOTIFY_DONE;
4949 #endif /* CONFIG_IGB_DCA */
4951 #ifdef CONFIG_PCI_IOV
4952 static int igb_vf_configure(struct igb_adapter *adapter, int vf)
4954 unsigned char mac_addr[ETH_ALEN];
4956 eth_random_addr(mac_addr);
4957 igb_set_vf_mac(adapter, vf, mac_addr);
4962 static bool igb_vfs_are_assigned(struct igb_adapter *adapter)
4964 struct pci_dev *pdev = adapter->pdev;
4965 struct pci_dev *vfdev;
4968 switch (adapter->hw.mac.type) {
4970 dev_id = IGB_82576_VF_DEV_ID;
4973 dev_id = IGB_I350_VF_DEV_ID;
4979 /* loop through all the VFs to see if we own any that are assigned */
4980 vfdev = pci_get_device(PCI_VENDOR_ID_INTEL, dev_id, NULL);
4982 /* if we don't own it we don't care */
4983 if (vfdev->is_virtfn && vfdev->physfn == pdev) {
4984 /* if it is assigned we cannot release it */
4985 if (vfdev->dev_flags & PCI_DEV_FLAGS_ASSIGNED)
4989 vfdev = pci_get_device(PCI_VENDOR_ID_INTEL, dev_id, vfdev);
4996 static void igb_ping_all_vfs(struct igb_adapter *adapter)
4998 struct e1000_hw *hw = &adapter->hw;
5002 for (i = 0 ; i < adapter->vfs_allocated_count; i++) {
5003 ping = E1000_PF_CONTROL_MSG;
5004 if (adapter->vf_data[i].flags & IGB_VF_FLAG_CTS)
5005 ping |= E1000_VT_MSGTYPE_CTS;
5006 igb_write_mbx(hw, &ping, 1, i);
5010 static int igb_set_vf_promisc(struct igb_adapter *adapter, u32 *msgbuf, u32 vf)
5012 struct e1000_hw *hw = &adapter->hw;
5013 u32 vmolr = rd32(E1000_VMOLR(vf));
5014 struct vf_data_storage *vf_data = &adapter->vf_data[vf];
5016 vf_data->flags &= ~(IGB_VF_FLAG_UNI_PROMISC |
5017 IGB_VF_FLAG_MULTI_PROMISC);
5018 vmolr &= ~(E1000_VMOLR_ROPE | E1000_VMOLR_ROMPE | E1000_VMOLR_MPME);
5020 if (*msgbuf & E1000_VF_SET_PROMISC_MULTICAST) {
5021 vmolr |= E1000_VMOLR_MPME;
5022 vf_data->flags |= IGB_VF_FLAG_MULTI_PROMISC;
5023 *msgbuf &= ~E1000_VF_SET_PROMISC_MULTICAST;
5026 * if we have hashes and we are clearing a multicast promisc
5027 * flag we need to write the hashes to the MTA as this step
5028 * was previously skipped
5030 if (vf_data->num_vf_mc_hashes > 30) {
5031 vmolr |= E1000_VMOLR_MPME;
5032 } else if (vf_data->num_vf_mc_hashes) {
5034 vmolr |= E1000_VMOLR_ROMPE;
5035 for (j = 0; j < vf_data->num_vf_mc_hashes; j++)
5036 igb_mta_set(hw, vf_data->vf_mc_hashes[j]);
5040 wr32(E1000_VMOLR(vf), vmolr);
5042 /* there are flags left unprocessed, likely not supported */
5043 if (*msgbuf & E1000_VT_MSGINFO_MASK)
5050 static int igb_set_vf_multicasts(struct igb_adapter *adapter,
5051 u32 *msgbuf, u32 vf)
5053 int n = (msgbuf[0] & E1000_VT_MSGINFO_MASK) >> E1000_VT_MSGINFO_SHIFT;
5054 u16 *hash_list = (u16 *)&msgbuf[1];
5055 struct vf_data_storage *vf_data = &adapter->vf_data[vf];
5058 /* salt away the number of multicast addresses assigned
5059 * to this VF for later use to restore when the PF multi cast
5062 vf_data->num_vf_mc_hashes = n;
5064 /* only up to 30 hash values supported */
5068 /* store the hashes for later use */
5069 for (i = 0; i < n; i++)
5070 vf_data->vf_mc_hashes[i] = hash_list[i];
5072 /* Flush and reset the mta with the new values */
5073 igb_set_rx_mode(adapter->netdev);
5078 static void igb_restore_vf_multicasts(struct igb_adapter *adapter)
5080 struct e1000_hw *hw = &adapter->hw;
5081 struct vf_data_storage *vf_data;
5084 for (i = 0; i < adapter->vfs_allocated_count; i++) {
5085 u32 vmolr = rd32(E1000_VMOLR(i));
5086 vmolr &= ~(E1000_VMOLR_ROMPE | E1000_VMOLR_MPME);
5088 vf_data = &adapter->vf_data[i];
5090 if ((vf_data->num_vf_mc_hashes > 30) ||
5091 (vf_data->flags & IGB_VF_FLAG_MULTI_PROMISC)) {
5092 vmolr |= E1000_VMOLR_MPME;
5093 } else if (vf_data->num_vf_mc_hashes) {
5094 vmolr |= E1000_VMOLR_ROMPE;
5095 for (j = 0; j < vf_data->num_vf_mc_hashes; j++)
5096 igb_mta_set(hw, vf_data->vf_mc_hashes[j]);
5098 wr32(E1000_VMOLR(i), vmolr);
5102 static void igb_clear_vf_vfta(struct igb_adapter *adapter, u32 vf)
5104 struct e1000_hw *hw = &adapter->hw;
5105 u32 pool_mask, reg, vid;
5108 pool_mask = 1 << (E1000_VLVF_POOLSEL_SHIFT + vf);
5110 /* Find the vlan filter for this id */
5111 for (i = 0; i < E1000_VLVF_ARRAY_SIZE; i++) {
5112 reg = rd32(E1000_VLVF(i));
5114 /* remove the vf from the pool */
5117 /* if pool is empty then remove entry from vfta */
5118 if (!(reg & E1000_VLVF_POOLSEL_MASK) &&
5119 (reg & E1000_VLVF_VLANID_ENABLE)) {
5121 vid = reg & E1000_VLVF_VLANID_MASK;
5122 igb_vfta_set(hw, vid, false);
5125 wr32(E1000_VLVF(i), reg);
5128 adapter->vf_data[vf].vlans_enabled = 0;
5131 static s32 igb_vlvf_set(struct igb_adapter *adapter, u32 vid, bool add, u32 vf)
5133 struct e1000_hw *hw = &adapter->hw;
5136 /* The vlvf table only exists on 82576 hardware and newer */
5137 if (hw->mac.type < e1000_82576)
5140 /* we only need to do this if VMDq is enabled */
5141 if (!adapter->vfs_allocated_count)
5144 /* Find the vlan filter for this id */
5145 for (i = 0; i < E1000_VLVF_ARRAY_SIZE; i++) {
5146 reg = rd32(E1000_VLVF(i));
5147 if ((reg & E1000_VLVF_VLANID_ENABLE) &&
5148 vid == (reg & E1000_VLVF_VLANID_MASK))
5153 if (i == E1000_VLVF_ARRAY_SIZE) {
5154 /* Did not find a matching VLAN ID entry that was
5155 * enabled. Search for a free filter entry, i.e.
5156 * one without the enable bit set
5158 for (i = 0; i < E1000_VLVF_ARRAY_SIZE; i++) {
5159 reg = rd32(E1000_VLVF(i));
5160 if (!(reg & E1000_VLVF_VLANID_ENABLE))
5164 if (i < E1000_VLVF_ARRAY_SIZE) {
5165 /* Found an enabled/available entry */
5166 reg |= 1 << (E1000_VLVF_POOLSEL_SHIFT + vf);
5168 /* if !enabled we need to set this up in vfta */
5169 if (!(reg & E1000_VLVF_VLANID_ENABLE)) {
5170 /* add VID to filter table */
5171 igb_vfta_set(hw, vid, true);
5172 reg |= E1000_VLVF_VLANID_ENABLE;
5174 reg &= ~E1000_VLVF_VLANID_MASK;
5176 wr32(E1000_VLVF(i), reg);
5178 /* do not modify RLPML for PF devices */
5179 if (vf >= adapter->vfs_allocated_count)
5182 if (!adapter->vf_data[vf].vlans_enabled) {
5184 reg = rd32(E1000_VMOLR(vf));
5185 size = reg & E1000_VMOLR_RLPML_MASK;
5187 reg &= ~E1000_VMOLR_RLPML_MASK;
5189 wr32(E1000_VMOLR(vf), reg);
5192 adapter->vf_data[vf].vlans_enabled++;
5195 if (i < E1000_VLVF_ARRAY_SIZE) {
5196 /* remove vf from the pool */
5197 reg &= ~(1 << (E1000_VLVF_POOLSEL_SHIFT + vf));
5198 /* if pool is empty then remove entry from vfta */
5199 if (!(reg & E1000_VLVF_POOLSEL_MASK)) {
5201 igb_vfta_set(hw, vid, false);
5203 wr32(E1000_VLVF(i), reg);
5205 /* do not modify RLPML for PF devices */
5206 if (vf >= adapter->vfs_allocated_count)
5209 adapter->vf_data[vf].vlans_enabled--;
5210 if (!adapter->vf_data[vf].vlans_enabled) {
5212 reg = rd32(E1000_VMOLR(vf));
5213 size = reg & E1000_VMOLR_RLPML_MASK;
5215 reg &= ~E1000_VMOLR_RLPML_MASK;
5217 wr32(E1000_VMOLR(vf), reg);
5224 static void igb_set_vmvir(struct igb_adapter *adapter, u32 vid, u32 vf)
5226 struct e1000_hw *hw = &adapter->hw;
5229 wr32(E1000_VMVIR(vf), (vid | E1000_VMVIR_VLANA_DEFAULT));
5231 wr32(E1000_VMVIR(vf), 0);
5234 static int igb_ndo_set_vf_vlan(struct net_device *netdev,
5235 int vf, u16 vlan, u8 qos)
5238 struct igb_adapter *adapter = netdev_priv(netdev);
5240 if ((vf >= adapter->vfs_allocated_count) || (vlan > 4095) || (qos > 7))
5243 err = igb_vlvf_set(adapter, vlan, !!vlan, vf);
5246 igb_set_vmvir(adapter, vlan | (qos << VLAN_PRIO_SHIFT), vf);
5247 igb_set_vmolr(adapter, vf, !vlan);
5248 adapter->vf_data[vf].pf_vlan = vlan;
5249 adapter->vf_data[vf].pf_qos = qos;
5250 dev_info(&adapter->pdev->dev,
5251 "Setting VLAN %d, QOS 0x%x on VF %d\n", vlan, qos, vf);
5252 if (test_bit(__IGB_DOWN, &adapter->state)) {
5253 dev_warn(&adapter->pdev->dev,
5254 "The VF VLAN has been set,"
5255 " but the PF device is not up.\n");
5256 dev_warn(&adapter->pdev->dev,
5257 "Bring the PF device up before"
5258 " attempting to use the VF device.\n");
5261 igb_vlvf_set(adapter, adapter->vf_data[vf].pf_vlan,
5263 igb_set_vmvir(adapter, vlan, vf);
5264 igb_set_vmolr(adapter, vf, true);
5265 adapter->vf_data[vf].pf_vlan = 0;
5266 adapter->vf_data[vf].pf_qos = 0;
5272 static int igb_set_vf_vlan(struct igb_adapter *adapter, u32 *msgbuf, u32 vf)
5274 int add = (msgbuf[0] & E1000_VT_MSGINFO_MASK) >> E1000_VT_MSGINFO_SHIFT;
5275 int vid = (msgbuf[1] & E1000_VLVF_VLANID_MASK);
5277 return igb_vlvf_set(adapter, vid, add, vf);
5280 static inline void igb_vf_reset(struct igb_adapter *adapter, u32 vf)
5282 /* clear flags - except flag that indicates PF has set the MAC */
5283 adapter->vf_data[vf].flags &= IGB_VF_FLAG_PF_SET_MAC;
5284 adapter->vf_data[vf].last_nack = jiffies;
5286 /* reset offloads to defaults */
5287 igb_set_vmolr(adapter, vf, true);
5289 /* reset vlans for device */
5290 igb_clear_vf_vfta(adapter, vf);
5291 if (adapter->vf_data[vf].pf_vlan)
5292 igb_ndo_set_vf_vlan(adapter->netdev, vf,
5293 adapter->vf_data[vf].pf_vlan,
5294 adapter->vf_data[vf].pf_qos);
5296 igb_clear_vf_vfta(adapter, vf);
5298 /* reset multicast table array for vf */
5299 adapter->vf_data[vf].num_vf_mc_hashes = 0;
5301 /* Flush and reset the mta with the new values */
5302 igb_set_rx_mode(adapter->netdev);
5305 static void igb_vf_reset_event(struct igb_adapter *adapter, u32 vf)
5307 unsigned char *vf_mac = adapter->vf_data[vf].vf_mac_addresses;
5309 /* generate a new mac address as we were hotplug removed/added */
5310 if (!(adapter->vf_data[vf].flags & IGB_VF_FLAG_PF_SET_MAC))
5311 eth_random_addr(vf_mac);
5313 /* process remaining reset events */
5314 igb_vf_reset(adapter, vf);
5317 static void igb_vf_reset_msg(struct igb_adapter *adapter, u32 vf)
5319 struct e1000_hw *hw = &adapter->hw;
5320 unsigned char *vf_mac = adapter->vf_data[vf].vf_mac_addresses;
5321 int rar_entry = hw->mac.rar_entry_count - (vf + 1);
5323 u8 *addr = (u8 *)(&msgbuf[1]);
5325 /* process all the same items cleared in a function level reset */
5326 igb_vf_reset(adapter, vf);
5328 /* set vf mac address */
5329 igb_rar_set_qsel(adapter, vf_mac, rar_entry, vf);
5331 /* enable transmit and receive for vf */
5332 reg = rd32(E1000_VFTE);
5333 wr32(E1000_VFTE, reg | (1 << vf));
5334 reg = rd32(E1000_VFRE);
5335 wr32(E1000_VFRE, reg | (1 << vf));
5337 adapter->vf_data[vf].flags |= IGB_VF_FLAG_CTS;
5339 /* reply to reset with ack and vf mac address */
5340 msgbuf[0] = E1000_VF_RESET | E1000_VT_MSGTYPE_ACK;
5341 memcpy(addr, vf_mac, 6);
5342 igb_write_mbx(hw, msgbuf, 3, vf);
5345 static int igb_set_vf_mac_addr(struct igb_adapter *adapter, u32 *msg, int vf)
5348 * The VF MAC Address is stored in a packed array of bytes
5349 * starting at the second 32 bit word of the msg array
5351 unsigned char *addr = (char *)&msg[1];
5354 if (is_valid_ether_addr(addr))
5355 err = igb_set_vf_mac(adapter, vf, addr);
5360 static void igb_rcv_ack_from_vf(struct igb_adapter *adapter, u32 vf)
5362 struct e1000_hw *hw = &adapter->hw;
5363 struct vf_data_storage *vf_data = &adapter->vf_data[vf];
5364 u32 msg = E1000_VT_MSGTYPE_NACK;
5366 /* if device isn't clear to send it shouldn't be reading either */
5367 if (!(vf_data->flags & IGB_VF_FLAG_CTS) &&
5368 time_after(jiffies, vf_data->last_nack + (2 * HZ))) {
5369 igb_write_mbx(hw, &msg, 1, vf);
5370 vf_data->last_nack = jiffies;
5374 static void igb_rcv_msg_from_vf(struct igb_adapter *adapter, u32 vf)
5376 struct pci_dev *pdev = adapter->pdev;
5377 u32 msgbuf[E1000_VFMAILBOX_SIZE];
5378 struct e1000_hw *hw = &adapter->hw;
5379 struct vf_data_storage *vf_data = &adapter->vf_data[vf];
5382 retval = igb_read_mbx(hw, msgbuf, E1000_VFMAILBOX_SIZE, vf);
5385 /* if receive failed revoke VF CTS stats and restart init */
5386 dev_err(&pdev->dev, "Error receiving message from VF\n");
5387 vf_data->flags &= ~IGB_VF_FLAG_CTS;
5388 if (!time_after(jiffies, vf_data->last_nack + (2 * HZ)))
5393 /* this is a message we already processed, do nothing */
5394 if (msgbuf[0] & (E1000_VT_MSGTYPE_ACK | E1000_VT_MSGTYPE_NACK))
5398 * until the vf completes a reset it should not be
5399 * allowed to start any configuration.
5402 if (msgbuf[0] == E1000_VF_RESET) {
5403 igb_vf_reset_msg(adapter, vf);
5407 if (!(vf_data->flags & IGB_VF_FLAG_CTS)) {
5408 if (!time_after(jiffies, vf_data->last_nack + (2 * HZ)))
5414 switch ((msgbuf[0] & 0xFFFF)) {
5415 case E1000_VF_SET_MAC_ADDR:
5417 if (!(vf_data->flags & IGB_VF_FLAG_PF_SET_MAC))
5418 retval = igb_set_vf_mac_addr(adapter, msgbuf, vf);
5420 dev_warn(&pdev->dev,
5421 "VF %d attempted to override administratively "
5422 "set MAC address\nReload the VF driver to "
5423 "resume operations\n", vf);
5425 case E1000_VF_SET_PROMISC:
5426 retval = igb_set_vf_promisc(adapter, msgbuf, vf);
5428 case E1000_VF_SET_MULTICAST:
5429 retval = igb_set_vf_multicasts(adapter, msgbuf, vf);
5431 case E1000_VF_SET_LPE:
5432 retval = igb_set_vf_rlpml(adapter, msgbuf[1], vf);
5434 case E1000_VF_SET_VLAN:
5436 if (vf_data->pf_vlan)
5437 dev_warn(&pdev->dev,
5438 "VF %d attempted to override administratively "
5439 "set VLAN tag\nReload the VF driver to "
5440 "resume operations\n", vf);
5442 retval = igb_set_vf_vlan(adapter, msgbuf, vf);
5445 dev_err(&pdev->dev, "Unhandled Msg %08x\n", msgbuf[0]);
5450 msgbuf[0] |= E1000_VT_MSGTYPE_CTS;
5452 /* notify the VF of the results of what it sent us */
5454 msgbuf[0] |= E1000_VT_MSGTYPE_NACK;
5456 msgbuf[0] |= E1000_VT_MSGTYPE_ACK;
5458 igb_write_mbx(hw, msgbuf, 1, vf);
5461 static void igb_msg_task(struct igb_adapter *adapter)
5463 struct e1000_hw *hw = &adapter->hw;
5466 for (vf = 0; vf < adapter->vfs_allocated_count; vf++) {
5467 /* process any reset requests */
5468 if (!igb_check_for_rst(hw, vf))
5469 igb_vf_reset_event(adapter, vf);
5471 /* process any messages pending */
5472 if (!igb_check_for_msg(hw, vf))
5473 igb_rcv_msg_from_vf(adapter, vf);
5475 /* process any acks */
5476 if (!igb_check_for_ack(hw, vf))
5477 igb_rcv_ack_from_vf(adapter, vf);
5482 * igb_set_uta - Set unicast filter table address
5483 * @adapter: board private structure
5485 * The unicast table address is a register array of 32-bit registers.
5486 * The table is meant to be used in a way similar to how the MTA is used
5487 * however due to certain limitations in the hardware it is necessary to
5488 * set all the hash bits to 1 and use the VMOLR ROPE bit as a promiscuous
5489 * enable bit to allow vlan tag stripping when promiscuous mode is enabled
5491 static void igb_set_uta(struct igb_adapter *adapter)
5493 struct e1000_hw *hw = &adapter->hw;
5496 /* The UTA table only exists on 82576 hardware and newer */
5497 if (hw->mac.type < e1000_82576)
5500 /* we only need to do this if VMDq is enabled */
5501 if (!adapter->vfs_allocated_count)
5504 for (i = 0; i < hw->mac.uta_reg_count; i++)
5505 array_wr32(E1000_UTA, i, ~0);
5509 * igb_intr_msi - Interrupt Handler
5510 * @irq: interrupt number
5511 * @data: pointer to a network interface device structure
5513 static irqreturn_t igb_intr_msi(int irq, void *data)
5515 struct igb_adapter *adapter = data;
5516 struct igb_q_vector *q_vector = adapter->q_vector[0];
5517 struct e1000_hw *hw = &adapter->hw;
5518 /* read ICR disables interrupts using IAM */
5519 u32 icr = rd32(E1000_ICR);
5521 igb_write_itr(q_vector);
5523 if (icr & E1000_ICR_DRSTA)
5524 schedule_work(&adapter->reset_task);
5526 if (icr & E1000_ICR_DOUTSYNC) {
5527 /* HW is reporting DMA is out of sync */
5528 adapter->stats.doosync++;
5531 if (icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) {
5532 hw->mac.get_link_status = 1;
5533 if (!test_bit(__IGB_DOWN, &adapter->state))
5534 mod_timer(&adapter->watchdog_timer, jiffies + 1);
5537 if (icr & E1000_ICR_TS) {
5538 u32 tsicr = rd32(E1000_TSICR);
5540 if (tsicr & E1000_TSICR_TXTS) {
5541 /* acknowledge the interrupt */
5542 wr32(E1000_TSICR, E1000_TSICR_TXTS);
5543 /* retrieve hardware timestamp */
5544 schedule_work(&adapter->ptp_tx_work);
5548 napi_schedule(&q_vector->napi);
5554 * igb_intr - Legacy Interrupt Handler
5555 * @irq: interrupt number
5556 * @data: pointer to a network interface device structure
5558 static irqreturn_t igb_intr(int irq, void *data)
5560 struct igb_adapter *adapter = data;
5561 struct igb_q_vector *q_vector = adapter->q_vector[0];
5562 struct e1000_hw *hw = &adapter->hw;
5563 /* Interrupt Auto-Mask...upon reading ICR, interrupts are masked. No
5564 * need for the IMC write */
5565 u32 icr = rd32(E1000_ICR);
5567 /* IMS will not auto-mask if INT_ASSERTED is not set, and if it is
5568 * not set, then the adapter didn't send an interrupt */
5569 if (!(icr & E1000_ICR_INT_ASSERTED))
5572 igb_write_itr(q_vector);
5574 if (icr & E1000_ICR_DRSTA)
5575 schedule_work(&adapter->reset_task);
5577 if (icr & E1000_ICR_DOUTSYNC) {
5578 /* HW is reporting DMA is out of sync */
5579 adapter->stats.doosync++;
5582 if (icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) {
5583 hw->mac.get_link_status = 1;
5584 /* guard against interrupt when we're going down */
5585 if (!test_bit(__IGB_DOWN, &adapter->state))
5586 mod_timer(&adapter->watchdog_timer, jiffies + 1);
5589 if (icr & E1000_ICR_TS) {
5590 u32 tsicr = rd32(E1000_TSICR);
5592 if (tsicr & E1000_TSICR_TXTS) {
5593 /* acknowledge the interrupt */
5594 wr32(E1000_TSICR, E1000_TSICR_TXTS);
5595 /* retrieve hardware timestamp */
5596 schedule_work(&adapter->ptp_tx_work);
5600 napi_schedule(&q_vector->napi);
5605 static void igb_ring_irq_enable(struct igb_q_vector *q_vector)
5607 struct igb_adapter *adapter = q_vector->adapter;
5608 struct e1000_hw *hw = &adapter->hw;
5610 if ((q_vector->rx.ring && (adapter->rx_itr_setting & 3)) ||
5611 (!q_vector->rx.ring && (adapter->tx_itr_setting & 3))) {
5612 if ((adapter->num_q_vectors == 1) && !adapter->vf_data)
5613 igb_set_itr(q_vector);
5615 igb_update_ring_itr(q_vector);
5618 if (!test_bit(__IGB_DOWN, &adapter->state)) {
5619 if (adapter->msix_entries)
5620 wr32(E1000_EIMS, q_vector->eims_value);
5622 igb_irq_enable(adapter);
5627 * igb_poll - NAPI Rx polling callback
5628 * @napi: napi polling structure
5629 * @budget: count of how many packets we should handle
5631 static int igb_poll(struct napi_struct *napi, int budget)
5633 struct igb_q_vector *q_vector = container_of(napi,
5634 struct igb_q_vector,
5636 bool clean_complete = true;
5638 #ifdef CONFIG_IGB_DCA
5639 if (q_vector->adapter->flags & IGB_FLAG_DCA_ENABLED)
5640 igb_update_dca(q_vector);
5642 if (q_vector->tx.ring)
5643 clean_complete = igb_clean_tx_irq(q_vector);
5645 if (q_vector->rx.ring)
5646 clean_complete &= igb_clean_rx_irq(q_vector, budget);
5648 /* If all work not completed, return budget and keep polling */
5649 if (!clean_complete)
5652 /* If not enough Rx work done, exit the polling mode */
5653 napi_complete(napi);
5654 igb_ring_irq_enable(q_vector);
5660 * igb_clean_tx_irq - Reclaim resources after transmit completes
5661 * @q_vector: pointer to q_vector containing needed info
5663 * returns true if ring is completely cleaned
5665 static bool igb_clean_tx_irq(struct igb_q_vector *q_vector)
5667 struct igb_adapter *adapter = q_vector->adapter;
5668 struct igb_ring *tx_ring = q_vector->tx.ring;
5669 struct igb_tx_buffer *tx_buffer;
5670 union e1000_adv_tx_desc *tx_desc;
5671 unsigned int total_bytes = 0, total_packets = 0;
5672 unsigned int budget = q_vector->tx.work_limit;
5673 unsigned int i = tx_ring->next_to_clean;
5675 if (test_bit(__IGB_DOWN, &adapter->state))
5678 tx_buffer = &tx_ring->tx_buffer_info[i];
5679 tx_desc = IGB_TX_DESC(tx_ring, i);
5680 i -= tx_ring->count;
5683 union e1000_adv_tx_desc *eop_desc = tx_buffer->next_to_watch;
5685 /* if next_to_watch is not set then there is no work pending */
5689 /* prevent any other reads prior to eop_desc */
5692 /* if DD is not set pending work has not been completed */
5693 if (!(eop_desc->wb.status & cpu_to_le32(E1000_TXD_STAT_DD)))
5696 /* clear next_to_watch to prevent false hangs */
5697 tx_buffer->next_to_watch = NULL;
5699 /* update the statistics for this packet */
5700 total_bytes += tx_buffer->bytecount;
5701 total_packets += tx_buffer->gso_segs;
5704 dev_kfree_skb_any(tx_buffer->skb);
5706 /* unmap skb header data */
5707 dma_unmap_single(tx_ring->dev,
5708 dma_unmap_addr(tx_buffer, dma),
5709 dma_unmap_len(tx_buffer, len),
5712 /* clear tx_buffer data */
5713 tx_buffer->skb = NULL;
5714 dma_unmap_len_set(tx_buffer, len, 0);
5716 /* clear last DMA location and unmap remaining buffers */
5717 while (tx_desc != eop_desc) {
5722 i -= tx_ring->count;
5723 tx_buffer = tx_ring->tx_buffer_info;
5724 tx_desc = IGB_TX_DESC(tx_ring, 0);
5727 /* unmap any remaining paged data */
5728 if (dma_unmap_len(tx_buffer, len)) {
5729 dma_unmap_page(tx_ring->dev,
5730 dma_unmap_addr(tx_buffer, dma),
5731 dma_unmap_len(tx_buffer, len),
5733 dma_unmap_len_set(tx_buffer, len, 0);
5737 /* move us one more past the eop_desc for start of next pkt */
5742 i -= tx_ring->count;
5743 tx_buffer = tx_ring->tx_buffer_info;
5744 tx_desc = IGB_TX_DESC(tx_ring, 0);
5747 /* issue prefetch for next Tx descriptor */
5750 /* update budget accounting */
5752 } while (likely(budget));
5754 netdev_tx_completed_queue(txring_txq(tx_ring),
5755 total_packets, total_bytes);
5756 i += tx_ring->count;
5757 tx_ring->next_to_clean = i;
5758 u64_stats_update_begin(&tx_ring->tx_syncp);
5759 tx_ring->tx_stats.bytes += total_bytes;
5760 tx_ring->tx_stats.packets += total_packets;
5761 u64_stats_update_end(&tx_ring->tx_syncp);
5762 q_vector->tx.total_bytes += total_bytes;
5763 q_vector->tx.total_packets += total_packets;
5765 if (test_bit(IGB_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags)) {
5766 struct e1000_hw *hw = &adapter->hw;
5768 /* Detect a transmit hang in hardware, this serializes the
5769 * check with the clearing of time_stamp and movement of i */
5770 clear_bit(IGB_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags);
5771 if (tx_buffer->next_to_watch &&
5772 time_after(jiffies, tx_buffer->time_stamp +
5773 (adapter->tx_timeout_factor * HZ)) &&
5774 !(rd32(E1000_STATUS) & E1000_STATUS_TXOFF)) {
5776 /* detected Tx unit hang */
5777 dev_err(tx_ring->dev,
5778 "Detected Tx Unit Hang\n"
5782 " next_to_use <%x>\n"
5783 " next_to_clean <%x>\n"
5784 "buffer_info[next_to_clean]\n"
5785 " time_stamp <%lx>\n"
5786 " next_to_watch <%p>\n"
5788 " desc.status <%x>\n",
5789 tx_ring->queue_index,
5790 rd32(E1000_TDH(tx_ring->reg_idx)),
5791 readl(tx_ring->tail),
5792 tx_ring->next_to_use,
5793 tx_ring->next_to_clean,
5794 tx_buffer->time_stamp,
5795 tx_buffer->next_to_watch,
5797 tx_buffer->next_to_watch->wb.status);
5798 netif_stop_subqueue(tx_ring->netdev,
5799 tx_ring->queue_index);
5801 /* we are about to reset, no point in enabling stuff */
5806 if (unlikely(total_packets &&
5807 netif_carrier_ok(tx_ring->netdev) &&
5808 igb_desc_unused(tx_ring) >= IGB_TX_QUEUE_WAKE)) {
5809 /* Make sure that anybody stopping the queue after this
5810 * sees the new next_to_clean.
5813 if (__netif_subqueue_stopped(tx_ring->netdev,
5814 tx_ring->queue_index) &&
5815 !(test_bit(__IGB_DOWN, &adapter->state))) {
5816 netif_wake_subqueue(tx_ring->netdev,
5817 tx_ring->queue_index);
5819 u64_stats_update_begin(&tx_ring->tx_syncp);
5820 tx_ring->tx_stats.restart_queue++;
5821 u64_stats_update_end(&tx_ring->tx_syncp);
5829 * igb_reuse_rx_page - page flip buffer and store it back on the ring
5830 * @rx_ring: rx descriptor ring to store buffers on
5831 * @old_buff: donor buffer to have page reused
5833 * Synchronizes page for reuse by the adapter
5835 static void igb_reuse_rx_page(struct igb_ring *rx_ring,
5836 struct igb_rx_buffer *old_buff)
5838 struct igb_rx_buffer *new_buff;
5839 u16 nta = rx_ring->next_to_alloc;
5841 new_buff = &rx_ring->rx_buffer_info[nta];
5843 /* update, and store next to alloc */
5845 rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0;
5847 /* transfer page from old buffer to new buffer */
5848 memcpy(new_buff, old_buff, sizeof(struct igb_rx_buffer));
5850 /* sync the buffer for use by the device */
5851 dma_sync_single_range_for_device(rx_ring->dev, old_buff->dma,
5852 old_buff->page_offset,
5858 * igb_add_rx_frag - Add contents of Rx buffer to sk_buff
5859 * @rx_ring: rx descriptor ring to transact packets on
5860 * @rx_buffer: buffer containing page to add
5861 * @rx_desc: descriptor containing length of buffer written by hardware
5862 * @skb: sk_buff to place the data into
5864 * This function will add the data contained in rx_buffer->page to the skb.
5865 * This is done either through a direct copy if the data in the buffer is
5866 * less than the skb header size, otherwise it will just attach the page as
5867 * a frag to the skb.
5869 * The function will then update the page offset if necessary and return
5870 * true if the buffer can be reused by the adapter.
5872 static bool igb_add_rx_frag(struct igb_ring *rx_ring,
5873 struct igb_rx_buffer *rx_buffer,
5874 union e1000_adv_rx_desc *rx_desc,
5875 struct sk_buff *skb)
5877 struct page *page = rx_buffer->page;
5878 unsigned int size = le16_to_cpu(rx_desc->wb.upper.length);
5880 if ((size <= IGB_RX_HDR_LEN) && !skb_is_nonlinear(skb)) {
5881 unsigned char *va = page_address(page) + rx_buffer->page_offset;
5883 if (igb_test_staterr(rx_desc, E1000_RXDADV_STAT_TSIP)) {
5884 igb_ptp_rx_pktstamp(rx_ring->q_vector, va, skb);
5885 va += IGB_TS_HDR_LEN;
5886 size -= IGB_TS_HDR_LEN;
5889 memcpy(__skb_put(skb, size), va, ALIGN(size, sizeof(long)));
5891 /* we can reuse buffer as-is, just make sure it is local */
5892 if (likely(page_to_nid(page) == numa_node_id()))
5895 /* this page cannot be reused so discard it */
5900 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page,
5901 rx_buffer->page_offset, size, IGB_RX_BUFSZ);
5903 /* avoid re-using remote pages */
5904 if (unlikely(page_to_nid(page) != numa_node_id()))
5907 #if (PAGE_SIZE < 8192)
5908 /* if we are only owner of page we can reuse it */
5909 if (unlikely(page_count(page) != 1))
5912 /* flip page offset to other buffer */
5913 rx_buffer->page_offset ^= IGB_RX_BUFSZ;
5916 * since we are the only owner of the page and we need to
5917 * increment it, just set the value to 2 in order to avoid
5918 * an unnecessary locked operation
5920 atomic_set(&page->_count, 2);
5922 /* move offset up to the next cache line */
5923 rx_buffer->page_offset += SKB_DATA_ALIGN(size);
5925 if (rx_buffer->page_offset > (PAGE_SIZE - IGB_RX_BUFSZ))
5928 /* bump ref count on page before it is given to the stack */
5935 static struct sk_buff *igb_fetch_rx_buffer(struct igb_ring *rx_ring,
5936 union e1000_adv_rx_desc *rx_desc,
5937 struct sk_buff *skb)
5939 struct igb_rx_buffer *rx_buffer;
5942 rx_buffer = &rx_ring->rx_buffer_info[rx_ring->next_to_clean];
5945 * This memory barrier is needed to keep us from reading
5946 * any other fields out of the rx_desc until we know the
5947 * RXD_STAT_DD bit is set
5951 page = rx_buffer->page;
5955 void *page_addr = page_address(page) +
5956 rx_buffer->page_offset;
5958 /* prefetch first cache line of first page */
5959 prefetch(page_addr);
5960 #if L1_CACHE_BYTES < 128
5961 prefetch(page_addr + L1_CACHE_BYTES);
5964 /* allocate a skb to store the frags */
5965 skb = netdev_alloc_skb_ip_align(rx_ring->netdev,
5967 if (unlikely(!skb)) {
5968 rx_ring->rx_stats.alloc_failed++;
5973 * we will be copying header into skb->data in
5974 * pskb_may_pull so it is in our interest to prefetch
5975 * it now to avoid a possible cache miss
5977 prefetchw(skb->data);
5980 /* we are reusing so sync this buffer for CPU use */
5981 dma_sync_single_range_for_cpu(rx_ring->dev,
5983 rx_buffer->page_offset,
5987 /* pull page into skb */
5988 if (igb_add_rx_frag(rx_ring, rx_buffer, rx_desc, skb)) {
5989 /* hand second half of page back to the ring */
5990 igb_reuse_rx_page(rx_ring, rx_buffer);
5992 /* we are not reusing the buffer so unmap it */
5993 dma_unmap_page(rx_ring->dev, rx_buffer->dma,
5994 PAGE_SIZE, DMA_FROM_DEVICE);
5997 /* clear contents of rx_buffer */
5998 rx_buffer->page = NULL;
6003 static inline void igb_rx_checksum(struct igb_ring *ring,
6004 union e1000_adv_rx_desc *rx_desc,
6005 struct sk_buff *skb)
6007 skb_checksum_none_assert(skb);
6009 /* Ignore Checksum bit is set */
6010 if (igb_test_staterr(rx_desc, E1000_RXD_STAT_IXSM))
6013 /* Rx checksum disabled via ethtool */
6014 if (!(ring->netdev->features & NETIF_F_RXCSUM))
6017 /* TCP/UDP checksum error bit is set */
6018 if (igb_test_staterr(rx_desc,
6019 E1000_RXDEXT_STATERR_TCPE |
6020 E1000_RXDEXT_STATERR_IPE)) {
6022 * work around errata with sctp packets where the TCPE aka
6023 * L4E bit is set incorrectly on 64 byte (60 byte w/o crc)
6024 * packets, (aka let the stack check the crc32c)
6026 if (!((skb->len == 60) &&
6027 test_bit(IGB_RING_FLAG_RX_SCTP_CSUM, &ring->flags))) {
6028 u64_stats_update_begin(&ring->rx_syncp);
6029 ring->rx_stats.csum_err++;
6030 u64_stats_update_end(&ring->rx_syncp);
6032 /* let the stack verify checksum errors */
6035 /* It must be a TCP or UDP packet with a valid checksum */
6036 if (igb_test_staterr(rx_desc, E1000_RXD_STAT_TCPCS |
6037 E1000_RXD_STAT_UDPCS))
6038 skb->ip_summed = CHECKSUM_UNNECESSARY;
6040 dev_dbg(ring->dev, "cksum success: bits %08X\n",
6041 le32_to_cpu(rx_desc->wb.upper.status_error));
6044 static inline void igb_rx_hash(struct igb_ring *ring,
6045 union e1000_adv_rx_desc *rx_desc,
6046 struct sk_buff *skb)
6048 if (ring->netdev->features & NETIF_F_RXHASH)
6049 skb->rxhash = le32_to_cpu(rx_desc->wb.lower.hi_dword.rss);
6053 * igb_is_non_eop - process handling of non-EOP buffers
6054 * @rx_ring: Rx ring being processed
6055 * @rx_desc: Rx descriptor for current buffer
6056 * @skb: current socket buffer containing buffer in progress
6058 * This function updates next to clean. If the buffer is an EOP buffer
6059 * this function exits returning false, otherwise it will place the
6060 * sk_buff in the next buffer to be chained and return true indicating
6061 * that this is in fact a non-EOP buffer.
6063 static bool igb_is_non_eop(struct igb_ring *rx_ring,
6064 union e1000_adv_rx_desc *rx_desc)
6066 u32 ntc = rx_ring->next_to_clean + 1;
6068 /* fetch, update, and store next to clean */
6069 ntc = (ntc < rx_ring->count) ? ntc : 0;
6070 rx_ring->next_to_clean = ntc;
6072 prefetch(IGB_RX_DESC(rx_ring, ntc));
6074 if (likely(igb_test_staterr(rx_desc, E1000_RXD_STAT_EOP)))
6081 * igb_get_headlen - determine size of header for LRO/GRO
6082 * @data: pointer to the start of the headers
6083 * @max_len: total length of section to find headers in
6085 * This function is meant to determine the length of headers that will
6086 * be recognized by hardware for LRO, and GRO offloads. The main
6087 * motivation of doing this is to only perform one pull for IPv4 TCP
6088 * packets so that we can do basic things like calculating the gso_size
6089 * based on the average data per packet.
6091 static unsigned int igb_get_headlen(unsigned char *data,
6092 unsigned int max_len)
6095 unsigned char *network;
6098 struct vlan_hdr *vlan;
6101 struct ipv6hdr *ipv6;
6104 u8 nexthdr = 0; /* default to not TCP */
6107 /* this should never happen, but better safe than sorry */
6108 if (max_len < ETH_HLEN)
6111 /* initialize network frame pointer */
6114 /* set first protocol and move network header forward */
6115 protocol = hdr.eth->h_proto;
6116 hdr.network += ETH_HLEN;
6118 /* handle any vlan tag if present */
6119 if (protocol == __constant_htons(ETH_P_8021Q)) {
6120 if ((hdr.network - data) > (max_len - VLAN_HLEN))
6123 protocol = hdr.vlan->h_vlan_encapsulated_proto;
6124 hdr.network += VLAN_HLEN;
6127 /* handle L3 protocols */
6128 if (protocol == __constant_htons(ETH_P_IP)) {
6129 if ((hdr.network - data) > (max_len - sizeof(struct iphdr)))
6132 /* access ihl as a u8 to avoid unaligned access on ia64 */
6133 hlen = (hdr.network[0] & 0x0F) << 2;
6135 /* verify hlen meets minimum size requirements */
6136 if (hlen < sizeof(struct iphdr))
6137 return hdr.network - data;
6139 /* record next protocol */
6140 nexthdr = hdr.ipv4->protocol;
6141 hdr.network += hlen;
6142 } else if (protocol == __constant_htons(ETH_P_IPV6)) {
6143 if ((hdr.network - data) > (max_len - sizeof(struct ipv6hdr)))
6146 /* record next protocol */
6147 nexthdr = hdr.ipv6->nexthdr;
6148 hdr.network += sizeof(struct ipv6hdr);
6150 return hdr.network - data;
6153 /* finally sort out TCP */
6154 if (nexthdr == IPPROTO_TCP) {
6155 if ((hdr.network - data) > (max_len - sizeof(struct tcphdr)))
6158 /* access doff as a u8 to avoid unaligned access on ia64 */
6159 hlen = (hdr.network[12] & 0xF0) >> 2;
6161 /* verify hlen meets minimum size requirements */
6162 if (hlen < sizeof(struct tcphdr))
6163 return hdr.network - data;
6165 hdr.network += hlen;
6166 } else if (nexthdr == IPPROTO_UDP) {
6167 if ((hdr.network - data) > (max_len - sizeof(struct udphdr)))
6170 hdr.network += sizeof(struct udphdr);
6174 * If everything has gone correctly hdr.network should be the
6175 * data section of the packet and will be the end of the header.
6176 * If not then it probably represents the end of the last recognized
6179 if ((hdr.network - data) < max_len)
6180 return hdr.network - data;
6186 * igb_pull_tail - igb specific version of skb_pull_tail
6187 * @rx_ring: rx descriptor ring packet is being transacted on
6188 * @rx_desc: pointer to the EOP Rx descriptor
6189 * @skb: pointer to current skb being adjusted
6191 * This function is an igb specific version of __pskb_pull_tail. The
6192 * main difference between this version and the original function is that
6193 * this function can make several assumptions about the state of things
6194 * that allow for significant optimizations versus the standard function.
6195 * As a result we can do things like drop a frag and maintain an accurate
6196 * truesize for the skb.
6198 static void igb_pull_tail(struct igb_ring *rx_ring,
6199 union e1000_adv_rx_desc *rx_desc,
6200 struct sk_buff *skb)
6202 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[0];
6204 unsigned int pull_len;
6207 * it is valid to use page_address instead of kmap since we are
6208 * working with pages allocated out of the lomem pool per
6209 * alloc_page(GFP_ATOMIC)
6211 va = skb_frag_address(frag);
6213 if (igb_test_staterr(rx_desc, E1000_RXDADV_STAT_TSIP)) {
6214 /* retrieve timestamp from buffer */
6215 igb_ptp_rx_pktstamp(rx_ring->q_vector, va, skb);
6217 /* update pointers to remove timestamp header */
6218 skb_frag_size_sub(frag, IGB_TS_HDR_LEN);
6219 frag->page_offset += IGB_TS_HDR_LEN;
6220 skb->data_len -= IGB_TS_HDR_LEN;
6221 skb->len -= IGB_TS_HDR_LEN;
6223 /* move va to start of packet data */
6224 va += IGB_TS_HDR_LEN;
6228 * we need the header to contain the greater of either ETH_HLEN or
6229 * 60 bytes if the skb->len is less than 60 for skb_pad.
6231 pull_len = igb_get_headlen(va, IGB_RX_HDR_LEN);
6233 /* align pull length to size of long to optimize memcpy performance */
6234 skb_copy_to_linear_data(skb, va, ALIGN(pull_len, sizeof(long)));
6236 /* update all of the pointers */
6237 skb_frag_size_sub(frag, pull_len);
6238 frag->page_offset += pull_len;
6239 skb->data_len -= pull_len;
6240 skb->tail += pull_len;
6244 * igb_cleanup_headers - Correct corrupted or empty headers
6245 * @rx_ring: rx descriptor ring packet is being transacted on
6246 * @rx_desc: pointer to the EOP Rx descriptor
6247 * @skb: pointer to current skb being fixed
6249 * Address the case where we are pulling data in on pages only
6250 * and as such no data is present in the skb header.
6252 * In addition if skb is not at least 60 bytes we need to pad it so that
6253 * it is large enough to qualify as a valid Ethernet frame.
6255 * Returns true if an error was encountered and skb was freed.
6257 static bool igb_cleanup_headers(struct igb_ring *rx_ring,
6258 union e1000_adv_rx_desc *rx_desc,
6259 struct sk_buff *skb)
6262 if (unlikely((igb_test_staterr(rx_desc,
6263 E1000_RXDEXT_ERR_FRAME_ERR_MASK)))) {
6264 struct net_device *netdev = rx_ring->netdev;
6265 if (!(netdev->features & NETIF_F_RXALL)) {
6266 dev_kfree_skb_any(skb);
6271 /* place header in linear portion of buffer */
6272 if (skb_is_nonlinear(skb))
6273 igb_pull_tail(rx_ring, rx_desc, skb);
6275 /* if skb_pad returns an error the skb was freed */
6276 if (unlikely(skb->len < 60)) {
6277 int pad_len = 60 - skb->len;
6279 if (skb_pad(skb, pad_len))
6281 __skb_put(skb, pad_len);
6288 * igb_process_skb_fields - Populate skb header fields from Rx descriptor
6289 * @rx_ring: rx descriptor ring packet is being transacted on
6290 * @rx_desc: pointer to the EOP Rx descriptor
6291 * @skb: pointer to current skb being populated
6293 * This function checks the ring, descriptor, and packet information in
6294 * order to populate the hash, checksum, VLAN, timestamp, protocol, and
6295 * other fields within the skb.
6297 static void igb_process_skb_fields(struct igb_ring *rx_ring,
6298 union e1000_adv_rx_desc *rx_desc,
6299 struct sk_buff *skb)
6301 struct net_device *dev = rx_ring->netdev;
6303 igb_rx_hash(rx_ring, rx_desc, skb);
6305 igb_rx_checksum(rx_ring, rx_desc, skb);
6307 igb_ptp_rx_hwtstamp(rx_ring->q_vector, rx_desc, skb);
6309 if ((dev->features & NETIF_F_HW_VLAN_RX) &&
6310 igb_test_staterr(rx_desc, E1000_RXD_STAT_VP)) {
6312 if (igb_test_staterr(rx_desc, E1000_RXDEXT_STATERR_LB) &&
6313 test_bit(IGB_RING_FLAG_RX_LB_VLAN_BSWAP, &rx_ring->flags))
6314 vid = be16_to_cpu(rx_desc->wb.upper.vlan);
6316 vid = le16_to_cpu(rx_desc->wb.upper.vlan);
6318 __vlan_hwaccel_put_tag(skb, vid);
6321 skb_record_rx_queue(skb, rx_ring->queue_index);
6323 skb->protocol = eth_type_trans(skb, rx_ring->netdev);
6326 static bool igb_clean_rx_irq(struct igb_q_vector *q_vector, const int budget)
6328 struct igb_ring *rx_ring = q_vector->rx.ring;
6329 struct sk_buff *skb = rx_ring->skb;
6330 unsigned int total_bytes = 0, total_packets = 0;
6331 u16 cleaned_count = igb_desc_unused(rx_ring);
6334 union e1000_adv_rx_desc *rx_desc;
6336 /* return some buffers to hardware, one at a time is too slow */
6337 if (cleaned_count >= IGB_RX_BUFFER_WRITE) {
6338 igb_alloc_rx_buffers(rx_ring, cleaned_count);
6342 rx_desc = IGB_RX_DESC(rx_ring, rx_ring->next_to_clean);
6344 if (!igb_test_staterr(rx_desc, E1000_RXD_STAT_DD))
6347 /* retrieve a buffer from the ring */
6348 skb = igb_fetch_rx_buffer(rx_ring, rx_desc, skb);
6350 /* exit if we failed to retrieve a buffer */
6356 /* fetch next buffer in frame if non-eop */
6357 if (igb_is_non_eop(rx_ring, rx_desc))
6360 /* verify the packet layout is correct */
6361 if (igb_cleanup_headers(rx_ring, rx_desc, skb)) {
6366 /* probably a little skewed due to removing CRC */
6367 total_bytes += skb->len;
6369 /* populate checksum, timestamp, VLAN, and protocol */
6370 igb_process_skb_fields(rx_ring, rx_desc, skb);
6372 napi_gro_receive(&q_vector->napi, skb);
6374 /* reset skb pointer */
6377 /* update budget accounting */
6379 } while (likely(total_packets < budget));
6381 /* place incomplete frames back on ring for completion */
6384 u64_stats_update_begin(&rx_ring->rx_syncp);
6385 rx_ring->rx_stats.packets += total_packets;
6386 rx_ring->rx_stats.bytes += total_bytes;
6387 u64_stats_update_end(&rx_ring->rx_syncp);
6388 q_vector->rx.total_packets += total_packets;
6389 q_vector->rx.total_bytes += total_bytes;
6392 igb_alloc_rx_buffers(rx_ring, cleaned_count);
6394 return (total_packets < budget);
6397 static bool igb_alloc_mapped_page(struct igb_ring *rx_ring,
6398 struct igb_rx_buffer *bi)
6400 struct page *page = bi->page;
6403 /* since we are recycling buffers we should seldom need to alloc */
6407 /* alloc new page for storage */
6408 page = __skb_alloc_page(GFP_ATOMIC | __GFP_COLD, NULL);
6409 if (unlikely(!page)) {
6410 rx_ring->rx_stats.alloc_failed++;
6414 /* map page for use */
6415 dma = dma_map_page(rx_ring->dev, page, 0, PAGE_SIZE, DMA_FROM_DEVICE);
6418 * if mapping failed free memory back to system since
6419 * there isn't much point in holding memory we can't use
6421 if (dma_mapping_error(rx_ring->dev, dma)) {
6424 rx_ring->rx_stats.alloc_failed++;
6430 bi->page_offset = 0;
6436 * igb_alloc_rx_buffers - Replace used receive buffers; packet split
6437 * @adapter: address of board private structure
6439 void igb_alloc_rx_buffers(struct igb_ring *rx_ring, u16 cleaned_count)
6441 union e1000_adv_rx_desc *rx_desc;
6442 struct igb_rx_buffer *bi;
6443 u16 i = rx_ring->next_to_use;
6449 rx_desc = IGB_RX_DESC(rx_ring, i);
6450 bi = &rx_ring->rx_buffer_info[i];
6451 i -= rx_ring->count;
6454 if (!igb_alloc_mapped_page(rx_ring, bi))
6458 * Refresh the desc even if buffer_addrs didn't change
6459 * because each write-back erases this info.
6461 rx_desc->read.pkt_addr = cpu_to_le64(bi->dma + bi->page_offset);
6467 rx_desc = IGB_RX_DESC(rx_ring, 0);
6468 bi = rx_ring->rx_buffer_info;
6469 i -= rx_ring->count;
6472 /* clear the hdr_addr for the next_to_use descriptor */
6473 rx_desc->read.hdr_addr = 0;
6476 } while (cleaned_count);
6478 i += rx_ring->count;
6480 if (rx_ring->next_to_use != i) {
6481 /* record the next descriptor to use */
6482 rx_ring->next_to_use = i;
6484 /* update next to alloc since we have filled the ring */
6485 rx_ring->next_to_alloc = i;
6488 * Force memory writes to complete before letting h/w
6489 * know there are new descriptors to fetch. (Only
6490 * applicable for weak-ordered memory model archs,
6494 writel(i, rx_ring->tail);
6504 static int igb_mii_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
6506 struct igb_adapter *adapter = netdev_priv(netdev);
6507 struct mii_ioctl_data *data = if_mii(ifr);
6509 if (adapter->hw.phy.media_type != e1000_media_type_copper)
6514 data->phy_id = adapter->hw.phy.addr;
6517 if (igb_read_phy_reg(&adapter->hw, data->reg_num & 0x1F,
6534 static int igb_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
6540 return igb_mii_ioctl(netdev, ifr, cmd);
6542 return igb_ptp_hwtstamp_ioctl(netdev, ifr, cmd);
6548 s32 igb_read_pcie_cap_reg(struct e1000_hw *hw, u32 reg, u16 *value)
6550 struct igb_adapter *adapter = hw->back;
6552 if (pcie_capability_read_word(adapter->pdev, reg, value))
6553 return -E1000_ERR_CONFIG;
6558 s32 igb_write_pcie_cap_reg(struct e1000_hw *hw, u32 reg, u16 *value)
6560 struct igb_adapter *adapter = hw->back;
6562 if (pcie_capability_write_word(adapter->pdev, reg, *value))
6563 return -E1000_ERR_CONFIG;
6568 static void igb_vlan_mode(struct net_device *netdev, netdev_features_t features)
6570 struct igb_adapter *adapter = netdev_priv(netdev);
6571 struct e1000_hw *hw = &adapter->hw;
6573 bool enable = !!(features & NETIF_F_HW_VLAN_RX);
6576 /* enable VLAN tag insert/strip */
6577 ctrl = rd32(E1000_CTRL);
6578 ctrl |= E1000_CTRL_VME;
6579 wr32(E1000_CTRL, ctrl);
6581 /* Disable CFI check */
6582 rctl = rd32(E1000_RCTL);
6583 rctl &= ~E1000_RCTL_CFIEN;
6584 wr32(E1000_RCTL, rctl);
6586 /* disable VLAN tag insert/strip */
6587 ctrl = rd32(E1000_CTRL);
6588 ctrl &= ~E1000_CTRL_VME;
6589 wr32(E1000_CTRL, ctrl);
6592 igb_rlpml_set(adapter);
6595 static int igb_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
6597 struct igb_adapter *adapter = netdev_priv(netdev);
6598 struct e1000_hw *hw = &adapter->hw;
6599 int pf_id = adapter->vfs_allocated_count;
6601 /* attempt to add filter to vlvf array */
6602 igb_vlvf_set(adapter, vid, true, pf_id);
6604 /* add the filter since PF can receive vlans w/o entry in vlvf */
6605 igb_vfta_set(hw, vid, true);
6607 set_bit(vid, adapter->active_vlans);
6612 static int igb_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
6614 struct igb_adapter *adapter = netdev_priv(netdev);
6615 struct e1000_hw *hw = &adapter->hw;
6616 int pf_id = adapter->vfs_allocated_count;
6619 /* remove vlan from VLVF table array */
6620 err = igb_vlvf_set(adapter, vid, false, pf_id);
6622 /* if vid was not present in VLVF just remove it from table */
6624 igb_vfta_set(hw, vid, false);
6626 clear_bit(vid, adapter->active_vlans);
6631 static void igb_restore_vlan(struct igb_adapter *adapter)
6635 igb_vlan_mode(adapter->netdev, adapter->netdev->features);
6637 for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
6638 igb_vlan_rx_add_vid(adapter->netdev, vid);
6641 int igb_set_spd_dplx(struct igb_adapter *adapter, u32 spd, u8 dplx)
6643 struct pci_dev *pdev = adapter->pdev;
6644 struct e1000_mac_info *mac = &adapter->hw.mac;
6648 /* Make sure dplx is at most 1 bit and lsb of speed is not set
6649 * for the switch() below to work */
6650 if ((spd & 1) || (dplx & ~1))
6653 /* Fiber NIC's only allow 1000 Gbps Full duplex */
6654 if ((adapter->hw.phy.media_type == e1000_media_type_internal_serdes) &&
6655 spd != SPEED_1000 &&
6656 dplx != DUPLEX_FULL)
6659 switch (spd + dplx) {
6660 case SPEED_10 + DUPLEX_HALF:
6661 mac->forced_speed_duplex = ADVERTISE_10_HALF;
6663 case SPEED_10 + DUPLEX_FULL:
6664 mac->forced_speed_duplex = ADVERTISE_10_FULL;
6666 case SPEED_100 + DUPLEX_HALF:
6667 mac->forced_speed_duplex = ADVERTISE_100_HALF;
6669 case SPEED_100 + DUPLEX_FULL:
6670 mac->forced_speed_duplex = ADVERTISE_100_FULL;
6672 case SPEED_1000 + DUPLEX_FULL:
6674 adapter->hw.phy.autoneg_advertised = ADVERTISE_1000_FULL;
6676 case SPEED_1000 + DUPLEX_HALF: /* not supported */
6681 /* clear MDI, MDI(-X) override is only allowed when autoneg enabled */
6682 adapter->hw.phy.mdix = AUTO_ALL_MODES;
6687 dev_err(&pdev->dev, "Unsupported Speed/Duplex configuration\n");
6691 static int __igb_shutdown(struct pci_dev *pdev, bool *enable_wake,
6694 struct net_device *netdev = pci_get_drvdata(pdev);
6695 struct igb_adapter *adapter = netdev_priv(netdev);
6696 struct e1000_hw *hw = &adapter->hw;
6697 u32 ctrl, rctl, status;
6698 u32 wufc = runtime ? E1000_WUFC_LNKC : adapter->wol;
6703 netif_device_detach(netdev);
6705 if (netif_running(netdev))
6706 __igb_close(netdev, true);
6708 igb_clear_interrupt_scheme(adapter);
6711 retval = pci_save_state(pdev);
6716 status = rd32(E1000_STATUS);
6717 if (status & E1000_STATUS_LU)
6718 wufc &= ~E1000_WUFC_LNKC;
6721 igb_setup_rctl(adapter);
6722 igb_set_rx_mode(netdev);
6724 /* turn on all-multi mode if wake on multicast is enabled */
6725 if (wufc & E1000_WUFC_MC) {
6726 rctl = rd32(E1000_RCTL);
6727 rctl |= E1000_RCTL_MPE;
6728 wr32(E1000_RCTL, rctl);
6731 ctrl = rd32(E1000_CTRL);
6732 /* advertise wake from D3Cold */
6733 #define E1000_CTRL_ADVD3WUC 0x00100000
6734 /* phy power management enable */
6735 #define E1000_CTRL_EN_PHY_PWR_MGMT 0x00200000
6736 ctrl |= E1000_CTRL_ADVD3WUC;
6737 wr32(E1000_CTRL, ctrl);
6739 /* Allow time for pending master requests to run */
6740 igb_disable_pcie_master(hw);
6742 wr32(E1000_WUC, E1000_WUC_PME_EN);
6743 wr32(E1000_WUFC, wufc);
6746 wr32(E1000_WUFC, 0);
6749 *enable_wake = wufc || adapter->en_mng_pt;
6751 igb_power_down_link(adapter);
6753 igb_power_up_link(adapter);
6755 /* Release control of h/w to f/w. If f/w is AMT enabled, this
6756 * would have already happened in close and is redundant. */
6757 igb_release_hw_control(adapter);
6759 pci_disable_device(pdev);
6765 #ifdef CONFIG_PM_SLEEP
6766 static int igb_suspend(struct device *dev)
6770 struct pci_dev *pdev = to_pci_dev(dev);
6772 retval = __igb_shutdown(pdev, &wake, 0);
6777 pci_prepare_to_sleep(pdev);
6779 pci_wake_from_d3(pdev, false);
6780 pci_set_power_state(pdev, PCI_D3hot);
6785 #endif /* CONFIG_PM_SLEEP */
6787 static int igb_resume(struct device *dev)
6789 struct pci_dev *pdev = to_pci_dev(dev);
6790 struct net_device *netdev = pci_get_drvdata(pdev);
6791 struct igb_adapter *adapter = netdev_priv(netdev);
6792 struct e1000_hw *hw = &adapter->hw;
6795 pci_set_power_state(pdev, PCI_D0);
6796 pci_restore_state(pdev);
6797 pci_save_state(pdev);
6799 err = pci_enable_device_mem(pdev);
6802 "igb: Cannot enable PCI device from suspend\n");
6805 pci_set_master(pdev);
6807 pci_enable_wake(pdev, PCI_D3hot, 0);
6808 pci_enable_wake(pdev, PCI_D3cold, 0);
6810 if (igb_init_interrupt_scheme(adapter)) {
6811 dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
6817 /* let the f/w know that the h/w is now under the control of the
6819 igb_get_hw_control(adapter);
6821 wr32(E1000_WUS, ~0);
6823 if (netdev->flags & IFF_UP) {
6825 err = __igb_open(netdev, true);
6831 netif_device_attach(netdev);
6835 #ifdef CONFIG_PM_RUNTIME
6836 static int igb_runtime_idle(struct device *dev)
6838 struct pci_dev *pdev = to_pci_dev(dev);
6839 struct net_device *netdev = pci_get_drvdata(pdev);
6840 struct igb_adapter *adapter = netdev_priv(netdev);
6842 if (!igb_has_link(adapter))
6843 pm_schedule_suspend(dev, MSEC_PER_SEC * 5);
6848 static int igb_runtime_suspend(struct device *dev)
6850 struct pci_dev *pdev = to_pci_dev(dev);
6854 retval = __igb_shutdown(pdev, &wake, 1);
6859 pci_prepare_to_sleep(pdev);
6861 pci_wake_from_d3(pdev, false);
6862 pci_set_power_state(pdev, PCI_D3hot);
6868 static int igb_runtime_resume(struct device *dev)
6870 return igb_resume(dev);
6872 #endif /* CONFIG_PM_RUNTIME */
6875 static void igb_shutdown(struct pci_dev *pdev)
6879 __igb_shutdown(pdev, &wake, 0);
6881 if (system_state == SYSTEM_POWER_OFF) {
6882 pci_wake_from_d3(pdev, wake);
6883 pci_set_power_state(pdev, PCI_D3hot);
6887 #ifdef CONFIG_NET_POLL_CONTROLLER
6889 * Polling 'interrupt' - used by things like netconsole to send skbs
6890 * without having to re-enable interrupts. It's not called while
6891 * the interrupt routine is executing.
6893 static void igb_netpoll(struct net_device *netdev)
6895 struct igb_adapter *adapter = netdev_priv(netdev);
6896 struct e1000_hw *hw = &adapter->hw;
6897 struct igb_q_vector *q_vector;
6900 for (i = 0; i < adapter->num_q_vectors; i++) {
6901 q_vector = adapter->q_vector[i];
6902 if (adapter->msix_entries)
6903 wr32(E1000_EIMC, q_vector->eims_value);
6905 igb_irq_disable(adapter);
6906 napi_schedule(&q_vector->napi);
6909 #endif /* CONFIG_NET_POLL_CONTROLLER */
6912 * igb_io_error_detected - called when PCI error is detected
6913 * @pdev: Pointer to PCI device
6914 * @state: The current pci connection state
6916 * This function is called after a PCI bus error affecting
6917 * this device has been detected.
6919 static pci_ers_result_t igb_io_error_detected(struct pci_dev *pdev,
6920 pci_channel_state_t state)
6922 struct net_device *netdev = pci_get_drvdata(pdev);
6923 struct igb_adapter *adapter = netdev_priv(netdev);
6925 netif_device_detach(netdev);
6927 if (state == pci_channel_io_perm_failure)
6928 return PCI_ERS_RESULT_DISCONNECT;
6930 if (netif_running(netdev))
6932 pci_disable_device(pdev);
6934 /* Request a slot slot reset. */
6935 return PCI_ERS_RESULT_NEED_RESET;
6939 * igb_io_slot_reset - called after the pci bus has been reset.
6940 * @pdev: Pointer to PCI device
6942 * Restart the card from scratch, as if from a cold-boot. Implementation
6943 * resembles the first-half of the igb_resume routine.
6945 static pci_ers_result_t igb_io_slot_reset(struct pci_dev *pdev)
6947 struct net_device *netdev = pci_get_drvdata(pdev);
6948 struct igb_adapter *adapter = netdev_priv(netdev);
6949 struct e1000_hw *hw = &adapter->hw;
6950 pci_ers_result_t result;
6953 if (pci_enable_device_mem(pdev)) {
6955 "Cannot re-enable PCI device after reset.\n");
6956 result = PCI_ERS_RESULT_DISCONNECT;
6958 pci_set_master(pdev);
6959 pci_restore_state(pdev);
6960 pci_save_state(pdev);
6962 pci_enable_wake(pdev, PCI_D3hot, 0);
6963 pci_enable_wake(pdev, PCI_D3cold, 0);
6966 wr32(E1000_WUS, ~0);
6967 result = PCI_ERS_RESULT_RECOVERED;
6970 err = pci_cleanup_aer_uncorrect_error_status(pdev);
6972 dev_err(&pdev->dev, "pci_cleanup_aer_uncorrect_error_status "
6973 "failed 0x%0x\n", err);
6974 /* non-fatal, continue */
6981 * igb_io_resume - called when traffic can start flowing again.
6982 * @pdev: Pointer to PCI device
6984 * This callback is called when the error recovery driver tells us that
6985 * its OK to resume normal operation. Implementation resembles the
6986 * second-half of the igb_resume routine.
6988 static void igb_io_resume(struct pci_dev *pdev)
6990 struct net_device *netdev = pci_get_drvdata(pdev);
6991 struct igb_adapter *adapter = netdev_priv(netdev);
6993 if (netif_running(netdev)) {
6994 if (igb_up(adapter)) {
6995 dev_err(&pdev->dev, "igb_up failed after reset\n");
7000 netif_device_attach(netdev);
7002 /* let the f/w know that the h/w is now under the control of the
7004 igb_get_hw_control(adapter);
7007 static void igb_rar_set_qsel(struct igb_adapter *adapter, u8 *addr, u32 index,
7010 u32 rar_low, rar_high;
7011 struct e1000_hw *hw = &adapter->hw;
7013 /* HW expects these in little endian so we reverse the byte order
7014 * from network order (big endian) to little endian
7016 rar_low = ((u32) addr[0] | ((u32) addr[1] << 8) |
7017 ((u32) addr[2] << 16) | ((u32) addr[3] << 24));
7018 rar_high = ((u32) addr[4] | ((u32) addr[5] << 8));
7020 /* Indicate to hardware the Address is Valid. */
7021 rar_high |= E1000_RAH_AV;
7023 if (hw->mac.type == e1000_82575)
7024 rar_high |= E1000_RAH_POOL_1 * qsel;
7026 rar_high |= E1000_RAH_POOL_1 << qsel;
7028 wr32(E1000_RAL(index), rar_low);
7030 wr32(E1000_RAH(index), rar_high);
7034 static int igb_set_vf_mac(struct igb_adapter *adapter,
7035 int vf, unsigned char *mac_addr)
7037 struct e1000_hw *hw = &adapter->hw;
7038 /* VF MAC addresses start at end of receive addresses and moves
7039 * torwards the first, as a result a collision should not be possible */
7040 int rar_entry = hw->mac.rar_entry_count - (vf + 1);
7042 memcpy(adapter->vf_data[vf].vf_mac_addresses, mac_addr, ETH_ALEN);
7044 igb_rar_set_qsel(adapter, mac_addr, rar_entry, vf);
7049 static int igb_ndo_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
7051 struct igb_adapter *adapter = netdev_priv(netdev);
7052 if (!is_valid_ether_addr(mac) || (vf >= adapter->vfs_allocated_count))
7054 adapter->vf_data[vf].flags |= IGB_VF_FLAG_PF_SET_MAC;
7055 dev_info(&adapter->pdev->dev, "setting MAC %pM on VF %d\n", mac, vf);
7056 dev_info(&adapter->pdev->dev, "Reload the VF driver to make this"
7057 " change effective.");
7058 if (test_bit(__IGB_DOWN, &adapter->state)) {
7059 dev_warn(&adapter->pdev->dev, "The VF MAC address has been set,"
7060 " but the PF device is not up.\n");
7061 dev_warn(&adapter->pdev->dev, "Bring the PF device up before"
7062 " attempting to use the VF device.\n");
7064 return igb_set_vf_mac(adapter, vf, mac);
7067 static int igb_link_mbps(int internal_link_speed)
7069 switch (internal_link_speed) {
7079 static void igb_set_vf_rate_limit(struct e1000_hw *hw, int vf, int tx_rate,
7086 /* Calculate the rate factor values to set */
7087 rf_int = link_speed / tx_rate;
7088 rf_dec = (link_speed - (rf_int * tx_rate));
7089 rf_dec = (rf_dec * (1<<E1000_RTTBCNRC_RF_INT_SHIFT)) / tx_rate;
7091 bcnrc_val = E1000_RTTBCNRC_RS_ENA;
7092 bcnrc_val |= ((rf_int<<E1000_RTTBCNRC_RF_INT_SHIFT) &
7093 E1000_RTTBCNRC_RF_INT_MASK);
7094 bcnrc_val |= (rf_dec & E1000_RTTBCNRC_RF_DEC_MASK);
7099 wr32(E1000_RTTDQSEL, vf); /* vf X uses queue X */
7101 * Set global transmit compensation time to the MMW_SIZE in RTTBCNRM
7102 * register. MMW_SIZE=0x014 if 9728-byte jumbo is supported.
7104 wr32(E1000_RTTBCNRM, 0x14);
7105 wr32(E1000_RTTBCNRC, bcnrc_val);
7108 static void igb_check_vf_rate_limit(struct igb_adapter *adapter)
7110 int actual_link_speed, i;
7111 bool reset_rate = false;
7113 /* VF TX rate limit was not set or not supported */
7114 if ((adapter->vf_rate_link_speed == 0) ||
7115 (adapter->hw.mac.type != e1000_82576))
7118 actual_link_speed = igb_link_mbps(adapter->link_speed);
7119 if (actual_link_speed != adapter->vf_rate_link_speed) {
7121 adapter->vf_rate_link_speed = 0;
7122 dev_info(&adapter->pdev->dev,
7123 "Link speed has been changed. VF Transmit "
7124 "rate is disabled\n");
7127 for (i = 0; i < adapter->vfs_allocated_count; i++) {
7129 adapter->vf_data[i].tx_rate = 0;
7131 igb_set_vf_rate_limit(&adapter->hw, i,
7132 adapter->vf_data[i].tx_rate,
7137 static int igb_ndo_set_vf_bw(struct net_device *netdev, int vf, int tx_rate)
7139 struct igb_adapter *adapter = netdev_priv(netdev);
7140 struct e1000_hw *hw = &adapter->hw;
7141 int actual_link_speed;
7143 if (hw->mac.type != e1000_82576)
7146 actual_link_speed = igb_link_mbps(adapter->link_speed);
7147 if ((vf >= adapter->vfs_allocated_count) ||
7148 (!(rd32(E1000_STATUS) & E1000_STATUS_LU)) ||
7149 (tx_rate < 0) || (tx_rate > actual_link_speed))
7152 adapter->vf_rate_link_speed = actual_link_speed;
7153 adapter->vf_data[vf].tx_rate = (u16)tx_rate;
7154 igb_set_vf_rate_limit(hw, vf, tx_rate, actual_link_speed);
7159 static int igb_ndo_get_vf_config(struct net_device *netdev,
7160 int vf, struct ifla_vf_info *ivi)
7162 struct igb_adapter *adapter = netdev_priv(netdev);
7163 if (vf >= adapter->vfs_allocated_count)
7166 memcpy(&ivi->mac, adapter->vf_data[vf].vf_mac_addresses, ETH_ALEN);
7167 ivi->tx_rate = adapter->vf_data[vf].tx_rate;
7168 ivi->vlan = adapter->vf_data[vf].pf_vlan;
7169 ivi->qos = adapter->vf_data[vf].pf_qos;
7173 static void igb_vmm_control(struct igb_adapter *adapter)
7175 struct e1000_hw *hw = &adapter->hw;
7178 switch (hw->mac.type) {
7183 /* replication is not supported for 82575 */
7186 /* notify HW that the MAC is adding vlan tags */
7187 reg = rd32(E1000_DTXCTL);
7188 reg |= E1000_DTXCTL_VLAN_ADDED;
7189 wr32(E1000_DTXCTL, reg);
7191 /* enable replication vlan tag stripping */
7192 reg = rd32(E1000_RPLOLR);
7193 reg |= E1000_RPLOLR_STRVLAN;
7194 wr32(E1000_RPLOLR, reg);
7196 /* none of the above registers are supported by i350 */
7200 if (adapter->vfs_allocated_count) {
7201 igb_vmdq_set_loopback_pf(hw, true);
7202 igb_vmdq_set_replication_pf(hw, true);
7203 igb_vmdq_set_anti_spoofing_pf(hw, true,
7204 adapter->vfs_allocated_count);
7206 igb_vmdq_set_loopback_pf(hw, false);
7207 igb_vmdq_set_replication_pf(hw, false);
7211 static void igb_init_dmac(struct igb_adapter *adapter, u32 pba)
7213 struct e1000_hw *hw = &adapter->hw;
7217 if (hw->mac.type > e1000_82580) {
7218 if (adapter->flags & IGB_FLAG_DMAC) {
7221 /* force threshold to 0. */
7222 wr32(E1000_DMCTXTH, 0);
7225 * DMA Coalescing high water mark needs to be greater
7226 * than the Rx threshold. Set hwm to PBA - max frame
7227 * size in 16B units, capping it at PBA - 6KB.
7229 hwm = 64 * pba - adapter->max_frame_size / 16;
7230 if (hwm < 64 * (pba - 6))
7231 hwm = 64 * (pba - 6);
7232 reg = rd32(E1000_FCRTC);
7233 reg &= ~E1000_FCRTC_RTH_COAL_MASK;
7234 reg |= ((hwm << E1000_FCRTC_RTH_COAL_SHIFT)
7235 & E1000_FCRTC_RTH_COAL_MASK);
7236 wr32(E1000_FCRTC, reg);
7239 * Set the DMA Coalescing Rx threshold to PBA - 2 * max
7240 * frame size, capping it at PBA - 10KB.
7242 dmac_thr = pba - adapter->max_frame_size / 512;
7243 if (dmac_thr < pba - 10)
7244 dmac_thr = pba - 10;
7245 reg = rd32(E1000_DMACR);
7246 reg &= ~E1000_DMACR_DMACTHR_MASK;
7247 reg |= ((dmac_thr << E1000_DMACR_DMACTHR_SHIFT)
7248 & E1000_DMACR_DMACTHR_MASK);
7250 /* transition to L0x or L1 if available..*/
7251 reg |= (E1000_DMACR_DMAC_EN | E1000_DMACR_DMAC_LX_MASK);
7253 /* watchdog timer= +-1000 usec in 32usec intervals */
7256 /* Disable BMC-to-OS Watchdog Enable */
7257 reg &= ~E1000_DMACR_DC_BMC2OSW_EN;
7258 wr32(E1000_DMACR, reg);
7261 * no lower threshold to disable
7262 * coalescing(smart fifb)-UTRESH=0
7264 wr32(E1000_DMCRTRH, 0);
7266 reg = (IGB_DMCTLX_DCFLUSH_DIS | 0x4);
7268 wr32(E1000_DMCTLX, reg);
7271 * free space in tx packet buffer to wake from
7274 wr32(E1000_DMCTXTH, (IGB_MIN_TXPBSIZE -
7275 (IGB_TX_BUF_4096 + adapter->max_frame_size)) >> 6);
7278 * make low power state decision controlled
7281 reg = rd32(E1000_PCIEMISC);
7282 reg &= ~E1000_PCIEMISC_LX_DECISION;
7283 wr32(E1000_PCIEMISC, reg);
7284 } /* endif adapter->dmac is not disabled */
7285 } else if (hw->mac.type == e1000_82580) {
7286 u32 reg = rd32(E1000_PCIEMISC);
7287 wr32(E1000_PCIEMISC, reg & ~E1000_PCIEMISC_LX_DECISION);
7288 wr32(E1000_DMACR, 0);