1 /*******************************************************************************
3 Intel(R) Gigabit Ethernet Linux driver
4 Copyright(c) 2007-2013 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>
60 #include <linux/i2c.h>
66 #define DRV_VERSION __stringify(MAJ) "." __stringify(MIN) "." \
67 __stringify(BUILD) "-k"
68 char igb_driver_name[] = "igb";
69 char igb_driver_version[] = DRV_VERSION;
70 static const char igb_driver_string[] =
71 "Intel(R) Gigabit Ethernet Network Driver";
72 static const char igb_copyright[] =
73 "Copyright (c) 2007-2013 Intel Corporation.";
75 static const struct e1000_info *igb_info_tbl[] = {
76 [board_82575] = &e1000_82575_info,
79 static DEFINE_PCI_DEVICE_TABLE(igb_pci_tbl) = {
80 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I354_BACKPLANE_1GBPS) },
81 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I354_SGMII) },
82 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I354_BACKPLANE_2_5GBPS) },
83 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I211_COPPER), board_82575 },
84 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I210_COPPER), board_82575 },
85 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I210_FIBER), board_82575 },
86 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I210_SERDES), board_82575 },
87 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I210_SGMII), board_82575 },
88 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I210_COPPER_FLASHLESS), board_82575 },
89 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I210_SERDES_FLASHLESS), board_82575 },
90 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I350_COPPER), board_82575 },
91 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I350_FIBER), board_82575 },
92 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I350_SERDES), board_82575 },
93 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I350_SGMII), board_82575 },
94 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_COPPER), board_82575 },
95 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_FIBER), board_82575 },
96 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_QUAD_FIBER), board_82575 },
97 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_SERDES), board_82575 },
98 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_SGMII), board_82575 },
99 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_COPPER_DUAL), board_82575 },
100 { PCI_VDEVICE(INTEL, E1000_DEV_ID_DH89XXCC_SGMII), board_82575 },
101 { PCI_VDEVICE(INTEL, E1000_DEV_ID_DH89XXCC_SERDES), board_82575 },
102 { PCI_VDEVICE(INTEL, E1000_DEV_ID_DH89XXCC_BACKPLANE), board_82575 },
103 { PCI_VDEVICE(INTEL, E1000_DEV_ID_DH89XXCC_SFP), board_82575 },
104 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576), board_82575 },
105 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_NS), board_82575 },
106 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_NS_SERDES), board_82575 },
107 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_FIBER), board_82575 },
108 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_SERDES), board_82575 },
109 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_SERDES_QUAD), board_82575 },
110 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_QUAD_COPPER_ET2), board_82575 },
111 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_QUAD_COPPER), board_82575 },
112 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82575EB_COPPER), board_82575 },
113 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82575EB_FIBER_SERDES), board_82575 },
114 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82575GB_QUAD_COPPER), board_82575 },
115 /* required last entry */
119 MODULE_DEVICE_TABLE(pci, igb_pci_tbl);
121 void igb_reset(struct igb_adapter *);
122 static int igb_setup_all_tx_resources(struct igb_adapter *);
123 static int igb_setup_all_rx_resources(struct igb_adapter *);
124 static void igb_free_all_tx_resources(struct igb_adapter *);
125 static void igb_free_all_rx_resources(struct igb_adapter *);
126 static void igb_setup_mrqc(struct igb_adapter *);
127 static int igb_probe(struct pci_dev *, const struct pci_device_id *);
128 static void igb_remove(struct pci_dev *pdev);
129 static int igb_sw_init(struct igb_adapter *);
130 static int igb_open(struct net_device *);
131 static int igb_close(struct net_device *);
132 static void igb_configure(struct igb_adapter *);
133 static void igb_configure_tx(struct igb_adapter *);
134 static void igb_configure_rx(struct igb_adapter *);
135 static void igb_clean_all_tx_rings(struct igb_adapter *);
136 static void igb_clean_all_rx_rings(struct igb_adapter *);
137 static void igb_clean_tx_ring(struct igb_ring *);
138 static void igb_clean_rx_ring(struct igb_ring *);
139 static void igb_set_rx_mode(struct net_device *);
140 static void igb_update_phy_info(unsigned long);
141 static void igb_watchdog(unsigned long);
142 static void igb_watchdog_task(struct work_struct *);
143 static netdev_tx_t igb_xmit_frame(struct sk_buff *skb, struct net_device *);
144 static struct rtnl_link_stats64 *igb_get_stats64(struct net_device *dev,
145 struct rtnl_link_stats64 *stats);
146 static int igb_change_mtu(struct net_device *, int);
147 static int igb_set_mac(struct net_device *, void *);
148 static void igb_set_uta(struct igb_adapter *adapter);
149 static irqreturn_t igb_intr(int irq, void *);
150 static irqreturn_t igb_intr_msi(int irq, void *);
151 static irqreturn_t igb_msix_other(int irq, void *);
152 static irqreturn_t igb_msix_ring(int irq, void *);
153 #ifdef CONFIG_IGB_DCA
154 static void igb_update_dca(struct igb_q_vector *);
155 static void igb_setup_dca(struct igb_adapter *);
156 #endif /* CONFIG_IGB_DCA */
157 static int igb_poll(struct napi_struct *, int);
158 static bool igb_clean_tx_irq(struct igb_q_vector *);
159 static bool igb_clean_rx_irq(struct igb_q_vector *, int);
160 static int igb_ioctl(struct net_device *, struct ifreq *, int cmd);
161 static void igb_tx_timeout(struct net_device *);
162 static void igb_reset_task(struct work_struct *);
163 static void igb_vlan_mode(struct net_device *netdev, netdev_features_t features);
164 static int igb_vlan_rx_add_vid(struct net_device *, __be16, u16);
165 static int igb_vlan_rx_kill_vid(struct net_device *, __be16, u16);
166 static void igb_restore_vlan(struct igb_adapter *);
167 static void igb_rar_set_qsel(struct igb_adapter *, u8 *, u32 , u8);
168 static void igb_ping_all_vfs(struct igb_adapter *);
169 static void igb_msg_task(struct igb_adapter *);
170 static void igb_vmm_control(struct igb_adapter *);
171 static int igb_set_vf_mac(struct igb_adapter *, int, unsigned char *);
172 static void igb_restore_vf_multicasts(struct igb_adapter *adapter);
173 static int igb_ndo_set_vf_mac(struct net_device *netdev, int vf, u8 *mac);
174 static int igb_ndo_set_vf_vlan(struct net_device *netdev,
175 int vf, u16 vlan, u8 qos);
176 static int igb_ndo_set_vf_bw(struct net_device *netdev, int vf, int tx_rate);
177 static int igb_ndo_set_vf_spoofchk(struct net_device *netdev, int vf,
179 static int igb_ndo_get_vf_config(struct net_device *netdev, int vf,
180 struct ifla_vf_info *ivi);
181 static void igb_check_vf_rate_limit(struct igb_adapter *);
183 #ifdef CONFIG_PCI_IOV
184 static int igb_vf_configure(struct igb_adapter *adapter, int vf);
185 static int igb_pci_enable_sriov(struct pci_dev *dev, int num_vfs);
189 #ifdef CONFIG_PM_SLEEP
190 static int igb_suspend(struct device *);
192 static int igb_resume(struct device *);
193 #ifdef CONFIG_PM_RUNTIME
194 static int igb_runtime_suspend(struct device *dev);
195 static int igb_runtime_resume(struct device *dev);
196 static int igb_runtime_idle(struct device *dev);
198 static const struct dev_pm_ops igb_pm_ops = {
199 SET_SYSTEM_SLEEP_PM_OPS(igb_suspend, igb_resume)
200 SET_RUNTIME_PM_OPS(igb_runtime_suspend, igb_runtime_resume,
204 static void igb_shutdown(struct pci_dev *);
205 static int igb_pci_sriov_configure(struct pci_dev *dev, int num_vfs);
206 #ifdef CONFIG_IGB_DCA
207 static int igb_notify_dca(struct notifier_block *, unsigned long, void *);
208 static struct notifier_block dca_notifier = {
209 .notifier_call = igb_notify_dca,
214 #ifdef CONFIG_NET_POLL_CONTROLLER
215 /* for netdump / net console */
216 static void igb_netpoll(struct net_device *);
218 #ifdef CONFIG_PCI_IOV
219 static unsigned int max_vfs = 0;
220 module_param(max_vfs, uint, 0);
221 MODULE_PARM_DESC(max_vfs, "Maximum number of virtual functions to allocate "
222 "per physical function");
223 #endif /* CONFIG_PCI_IOV */
225 static pci_ers_result_t igb_io_error_detected(struct pci_dev *,
226 pci_channel_state_t);
227 static pci_ers_result_t igb_io_slot_reset(struct pci_dev *);
228 static void igb_io_resume(struct pci_dev *);
230 static const struct pci_error_handlers igb_err_handler = {
231 .error_detected = igb_io_error_detected,
232 .slot_reset = igb_io_slot_reset,
233 .resume = igb_io_resume,
236 static void igb_init_dmac(struct igb_adapter *adapter, u32 pba);
238 static struct pci_driver igb_driver = {
239 .name = igb_driver_name,
240 .id_table = igb_pci_tbl,
242 .remove = igb_remove,
244 .driver.pm = &igb_pm_ops,
246 .shutdown = igb_shutdown,
247 .sriov_configure = igb_pci_sriov_configure,
248 .err_handler = &igb_err_handler
251 MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
252 MODULE_DESCRIPTION("Intel(R) Gigabit Ethernet Network Driver");
253 MODULE_LICENSE("GPL");
254 MODULE_VERSION(DRV_VERSION);
256 #define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK)
257 static int debug = -1;
258 module_param(debug, int, 0);
259 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
261 struct igb_reg_info {
266 static const struct igb_reg_info igb_reg_info_tbl[] = {
268 /* General Registers */
269 {E1000_CTRL, "CTRL"},
270 {E1000_STATUS, "STATUS"},
271 {E1000_CTRL_EXT, "CTRL_EXT"},
273 /* Interrupt Registers */
277 {E1000_RCTL, "RCTL"},
278 {E1000_RDLEN(0), "RDLEN"},
279 {E1000_RDH(0), "RDH"},
280 {E1000_RDT(0), "RDT"},
281 {E1000_RXDCTL(0), "RXDCTL"},
282 {E1000_RDBAL(0), "RDBAL"},
283 {E1000_RDBAH(0), "RDBAH"},
286 {E1000_TCTL, "TCTL"},
287 {E1000_TDBAL(0), "TDBAL"},
288 {E1000_TDBAH(0), "TDBAH"},
289 {E1000_TDLEN(0), "TDLEN"},
290 {E1000_TDH(0), "TDH"},
291 {E1000_TDT(0), "TDT"},
292 {E1000_TXDCTL(0), "TXDCTL"},
293 {E1000_TDFH, "TDFH"},
294 {E1000_TDFT, "TDFT"},
295 {E1000_TDFHS, "TDFHS"},
296 {E1000_TDFPC, "TDFPC"},
298 /* List Terminator */
302 /* igb_regdump - register printout routine */
303 static void igb_regdump(struct e1000_hw *hw, struct igb_reg_info *reginfo)
309 switch (reginfo->ofs) {
311 for (n = 0; n < 4; n++)
312 regs[n] = rd32(E1000_RDLEN(n));
315 for (n = 0; n < 4; n++)
316 regs[n] = rd32(E1000_RDH(n));
319 for (n = 0; n < 4; n++)
320 regs[n] = rd32(E1000_RDT(n));
322 case E1000_RXDCTL(0):
323 for (n = 0; n < 4; n++)
324 regs[n] = rd32(E1000_RXDCTL(n));
327 for (n = 0; n < 4; n++)
328 regs[n] = rd32(E1000_RDBAL(n));
331 for (n = 0; n < 4; n++)
332 regs[n] = rd32(E1000_RDBAH(n));
335 for (n = 0; n < 4; n++)
336 regs[n] = rd32(E1000_RDBAL(n));
339 for (n = 0; n < 4; n++)
340 regs[n] = rd32(E1000_TDBAH(n));
343 for (n = 0; n < 4; n++)
344 regs[n] = rd32(E1000_TDLEN(n));
347 for (n = 0; n < 4; n++)
348 regs[n] = rd32(E1000_TDH(n));
351 for (n = 0; n < 4; n++)
352 regs[n] = rd32(E1000_TDT(n));
354 case E1000_TXDCTL(0):
355 for (n = 0; n < 4; n++)
356 regs[n] = rd32(E1000_TXDCTL(n));
359 pr_info("%-15s %08x\n", reginfo->name, rd32(reginfo->ofs));
363 snprintf(rname, 16, "%s%s", reginfo->name, "[0-3]");
364 pr_info("%-15s %08x %08x %08x %08x\n", rname, regs[0], regs[1],
368 /* igb_dump - Print registers, Tx-rings and Rx-rings */
369 static void igb_dump(struct igb_adapter *adapter)
371 struct net_device *netdev = adapter->netdev;
372 struct e1000_hw *hw = &adapter->hw;
373 struct igb_reg_info *reginfo;
374 struct igb_ring *tx_ring;
375 union e1000_adv_tx_desc *tx_desc;
376 struct my_u0 { u64 a; u64 b; } *u0;
377 struct igb_ring *rx_ring;
378 union e1000_adv_rx_desc *rx_desc;
382 if (!netif_msg_hw(adapter))
385 /* Print netdevice Info */
387 dev_info(&adapter->pdev->dev, "Net device Info\n");
388 pr_info("Device Name state trans_start "
390 pr_info("%-15s %016lX %016lX %016lX\n", netdev->name,
391 netdev->state, netdev->trans_start, netdev->last_rx);
394 /* Print Registers */
395 dev_info(&adapter->pdev->dev, "Register Dump\n");
396 pr_info(" Register Name Value\n");
397 for (reginfo = (struct igb_reg_info *)igb_reg_info_tbl;
398 reginfo->name; reginfo++) {
399 igb_regdump(hw, reginfo);
402 /* Print TX Ring Summary */
403 if (!netdev || !netif_running(netdev))
406 dev_info(&adapter->pdev->dev, "TX Rings Summary\n");
407 pr_info("Queue [NTU] [NTC] [bi(ntc)->dma ] leng ntw timestamp\n");
408 for (n = 0; n < adapter->num_tx_queues; n++) {
409 struct igb_tx_buffer *buffer_info;
410 tx_ring = adapter->tx_ring[n];
411 buffer_info = &tx_ring->tx_buffer_info[tx_ring->next_to_clean];
412 pr_info(" %5d %5X %5X %016llX %04X %p %016llX\n",
413 n, tx_ring->next_to_use, tx_ring->next_to_clean,
414 (u64)dma_unmap_addr(buffer_info, dma),
415 dma_unmap_len(buffer_info, len),
416 buffer_info->next_to_watch,
417 (u64)buffer_info->time_stamp);
421 if (!netif_msg_tx_done(adapter))
422 goto rx_ring_summary;
424 dev_info(&adapter->pdev->dev, "TX Rings Dump\n");
426 /* Transmit Descriptor Formats
428 * Advanced Transmit Descriptor
429 * +--------------------------------------------------------------+
430 * 0 | Buffer Address [63:0] |
431 * +--------------------------------------------------------------+
432 * 8 | PAYLEN | PORTS |CC|IDX | STA | DCMD |DTYP|MAC|RSV| DTALEN |
433 * +--------------------------------------------------------------+
434 * 63 46 45 40 39 38 36 35 32 31 24 15 0
437 for (n = 0; n < adapter->num_tx_queues; n++) {
438 tx_ring = adapter->tx_ring[n];
439 pr_info("------------------------------------\n");
440 pr_info("TX QUEUE INDEX = %d\n", tx_ring->queue_index);
441 pr_info("------------------------------------\n");
442 pr_info("T [desc] [address 63:0 ] [PlPOCIStDDM Ln] "
443 "[bi->dma ] leng ntw timestamp "
446 for (i = 0; tx_ring->desc && (i < tx_ring->count); i++) {
447 const char *next_desc;
448 struct igb_tx_buffer *buffer_info;
449 tx_desc = IGB_TX_DESC(tx_ring, i);
450 buffer_info = &tx_ring->tx_buffer_info[i];
451 u0 = (struct my_u0 *)tx_desc;
452 if (i == tx_ring->next_to_use &&
453 i == tx_ring->next_to_clean)
454 next_desc = " NTC/U";
455 else if (i == tx_ring->next_to_use)
457 else if (i == tx_ring->next_to_clean)
462 pr_info("T [0x%03X] %016llX %016llX %016llX"
463 " %04X %p %016llX %p%s\n", i,
466 (u64)dma_unmap_addr(buffer_info, dma),
467 dma_unmap_len(buffer_info, len),
468 buffer_info->next_to_watch,
469 (u64)buffer_info->time_stamp,
470 buffer_info->skb, next_desc);
472 if (netif_msg_pktdata(adapter) && buffer_info->skb)
473 print_hex_dump(KERN_INFO, "",
475 16, 1, buffer_info->skb->data,
476 dma_unmap_len(buffer_info, len),
481 /* Print RX Rings Summary */
483 dev_info(&adapter->pdev->dev, "RX Rings Summary\n");
484 pr_info("Queue [NTU] [NTC]\n");
485 for (n = 0; n < adapter->num_rx_queues; n++) {
486 rx_ring = adapter->rx_ring[n];
487 pr_info(" %5d %5X %5X\n",
488 n, rx_ring->next_to_use, rx_ring->next_to_clean);
492 if (!netif_msg_rx_status(adapter))
495 dev_info(&adapter->pdev->dev, "RX Rings Dump\n");
497 /* Advanced Receive Descriptor (Read) Format
499 * +-----------------------------------------------------+
500 * 0 | Packet Buffer Address [63:1] |A0/NSE|
501 * +----------------------------------------------+------+
502 * 8 | Header Buffer Address [63:1] | DD |
503 * +-----------------------------------------------------+
506 * Advanced Receive Descriptor (Write-Back) Format
508 * 63 48 47 32 31 30 21 20 17 16 4 3 0
509 * +------------------------------------------------------+
510 * 0 | Packet IP |SPH| HDR_LEN | RSV|Packet| RSS |
511 * | Checksum Ident | | | | Type | Type |
512 * +------------------------------------------------------+
513 * 8 | VLAN Tag | Length | Extended Error | Extended Status |
514 * +------------------------------------------------------+
515 * 63 48 47 32 31 20 19 0
518 for (n = 0; n < adapter->num_rx_queues; n++) {
519 rx_ring = adapter->rx_ring[n];
520 pr_info("------------------------------------\n");
521 pr_info("RX QUEUE INDEX = %d\n", rx_ring->queue_index);
522 pr_info("------------------------------------\n");
523 pr_info("R [desc] [ PktBuf A0] [ HeadBuf DD] "
524 "[bi->dma ] [bi->skb] <-- Adv Rx Read format\n");
525 pr_info("RWB[desc] [PcsmIpSHl PtRs] [vl er S cks ln] -----"
526 "----------- [bi->skb] <-- Adv Rx Write-Back format\n");
528 for (i = 0; i < rx_ring->count; i++) {
529 const char *next_desc;
530 struct igb_rx_buffer *buffer_info;
531 buffer_info = &rx_ring->rx_buffer_info[i];
532 rx_desc = IGB_RX_DESC(rx_ring, i);
533 u0 = (struct my_u0 *)rx_desc;
534 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
536 if (i == rx_ring->next_to_use)
538 else if (i == rx_ring->next_to_clean)
543 if (staterr & E1000_RXD_STAT_DD) {
544 /* Descriptor Done */
545 pr_info("%s[0x%03X] %016llX %016llX ---------------- %s\n",
551 pr_info("%s[0x%03X] %016llX %016llX %016llX %s\n",
555 (u64)buffer_info->dma,
558 if (netif_msg_pktdata(adapter) &&
559 buffer_info->dma && buffer_info->page) {
560 print_hex_dump(KERN_INFO, "",
563 page_address(buffer_info->page) +
564 buffer_info->page_offset,
576 * igb_get_i2c_data - Reads the I2C SDA data bit
577 * @hw: pointer to hardware structure
578 * @i2cctl: Current value of I2CCTL register
580 * Returns the I2C data bit value
582 static int igb_get_i2c_data(void *data)
584 struct igb_adapter *adapter = (struct igb_adapter *)data;
585 struct e1000_hw *hw = &adapter->hw;
586 s32 i2cctl = rd32(E1000_I2CPARAMS);
588 return ((i2cctl & E1000_I2C_DATA_IN) != 0);
592 * igb_set_i2c_data - Sets the I2C data bit
593 * @data: pointer to hardware structure
594 * @state: I2C data value (0 or 1) to set
596 * Sets the I2C data bit
598 static void igb_set_i2c_data(void *data, int state)
600 struct igb_adapter *adapter = (struct igb_adapter *)data;
601 struct e1000_hw *hw = &adapter->hw;
602 s32 i2cctl = rd32(E1000_I2CPARAMS);
605 i2cctl |= E1000_I2C_DATA_OUT;
607 i2cctl &= ~E1000_I2C_DATA_OUT;
609 i2cctl &= ~E1000_I2C_DATA_OE_N;
610 i2cctl |= E1000_I2C_CLK_OE_N;
611 wr32(E1000_I2CPARAMS, i2cctl);
617 * igb_set_i2c_clk - Sets the I2C SCL clock
618 * @data: pointer to hardware structure
619 * @state: state to set clock
621 * Sets the I2C clock line to state
623 static void igb_set_i2c_clk(void *data, int state)
625 struct igb_adapter *adapter = (struct igb_adapter *)data;
626 struct e1000_hw *hw = &adapter->hw;
627 s32 i2cctl = rd32(E1000_I2CPARAMS);
630 i2cctl |= E1000_I2C_CLK_OUT;
631 i2cctl &= ~E1000_I2C_CLK_OE_N;
633 i2cctl &= ~E1000_I2C_CLK_OUT;
634 i2cctl &= ~E1000_I2C_CLK_OE_N;
636 wr32(E1000_I2CPARAMS, i2cctl);
641 * igb_get_i2c_clk - Gets the I2C SCL clock state
642 * @data: pointer to hardware structure
644 * Gets the I2C clock state
646 static int igb_get_i2c_clk(void *data)
648 struct igb_adapter *adapter = (struct igb_adapter *)data;
649 struct e1000_hw *hw = &adapter->hw;
650 s32 i2cctl = rd32(E1000_I2CPARAMS);
652 return ((i2cctl & E1000_I2C_CLK_IN) != 0);
655 static const struct i2c_algo_bit_data igb_i2c_algo = {
656 .setsda = igb_set_i2c_data,
657 .setscl = igb_set_i2c_clk,
658 .getsda = igb_get_i2c_data,
659 .getscl = igb_get_i2c_clk,
665 * igb_get_hw_dev - return device
666 * @hw: pointer to hardware structure
668 * used by hardware layer to print debugging information
670 struct net_device *igb_get_hw_dev(struct e1000_hw *hw)
672 struct igb_adapter *adapter = hw->back;
673 return adapter->netdev;
677 * igb_init_module - Driver Registration Routine
679 * igb_init_module is the first routine called when the driver is
680 * loaded. All it does is register with the PCI subsystem.
682 static int __init igb_init_module(void)
685 pr_info("%s - version %s\n",
686 igb_driver_string, igb_driver_version);
688 pr_info("%s\n", igb_copyright);
690 #ifdef CONFIG_IGB_DCA
691 dca_register_notify(&dca_notifier);
693 ret = pci_register_driver(&igb_driver);
697 module_init(igb_init_module);
700 * igb_exit_module - Driver Exit Cleanup Routine
702 * igb_exit_module is called just before the driver is removed
705 static void __exit igb_exit_module(void)
707 #ifdef CONFIG_IGB_DCA
708 dca_unregister_notify(&dca_notifier);
710 pci_unregister_driver(&igb_driver);
713 module_exit(igb_exit_module);
715 #define Q_IDX_82576(i) (((i & 0x1) << 3) + (i >> 1))
717 * igb_cache_ring_register - Descriptor ring to register mapping
718 * @adapter: board private structure to initialize
720 * Once we know the feature-set enabled for the device, we'll cache
721 * the register offset the descriptor ring is assigned to.
723 static void igb_cache_ring_register(struct igb_adapter *adapter)
726 u32 rbase_offset = adapter->vfs_allocated_count;
728 switch (adapter->hw.mac.type) {
730 /* The queues are allocated for virtualization such that VF 0
731 * is allocated queues 0 and 8, VF 1 queues 1 and 9, etc.
732 * In order to avoid collision we start at the first free queue
733 * and continue consuming queues in the same sequence
735 if (adapter->vfs_allocated_count) {
736 for (; i < adapter->rss_queues; i++)
737 adapter->rx_ring[i]->reg_idx = rbase_offset +
747 for (; i < adapter->num_rx_queues; i++)
748 adapter->rx_ring[i]->reg_idx = rbase_offset + i;
749 for (; j < adapter->num_tx_queues; j++)
750 adapter->tx_ring[j]->reg_idx = rbase_offset + j;
756 * igb_write_ivar - configure ivar for given MSI-X vector
757 * @hw: pointer to the HW structure
758 * @msix_vector: vector number we are allocating to a given ring
759 * @index: row index of IVAR register to write within IVAR table
760 * @offset: column offset of in IVAR, should be multiple of 8
762 * This function is intended to handle the writing of the IVAR register
763 * for adapters 82576 and newer. The IVAR table consists of 2 columns,
764 * each containing an cause allocation for an Rx and Tx ring, and a
765 * variable number of rows depending on the number of queues supported.
767 static void igb_write_ivar(struct e1000_hw *hw, int msix_vector,
768 int index, int offset)
770 u32 ivar = array_rd32(E1000_IVAR0, index);
772 /* clear any bits that are currently set */
773 ivar &= ~((u32)0xFF << offset);
775 /* write vector and valid bit */
776 ivar |= (msix_vector | E1000_IVAR_VALID) << offset;
778 array_wr32(E1000_IVAR0, index, ivar);
781 #define IGB_N0_QUEUE -1
782 static void igb_assign_vector(struct igb_q_vector *q_vector, int msix_vector)
784 struct igb_adapter *adapter = q_vector->adapter;
785 struct e1000_hw *hw = &adapter->hw;
786 int rx_queue = IGB_N0_QUEUE;
787 int tx_queue = IGB_N0_QUEUE;
790 if (q_vector->rx.ring)
791 rx_queue = q_vector->rx.ring->reg_idx;
792 if (q_vector->tx.ring)
793 tx_queue = q_vector->tx.ring->reg_idx;
795 switch (hw->mac.type) {
797 /* The 82575 assigns vectors using a bitmask, which matches the
798 * bitmask for the EICR/EIMS/EIMC registers. To assign one
799 * or more queues to a vector, we write the appropriate bits
800 * into the MSIXBM register for that vector.
802 if (rx_queue > IGB_N0_QUEUE)
803 msixbm = E1000_EICR_RX_QUEUE0 << rx_queue;
804 if (tx_queue > IGB_N0_QUEUE)
805 msixbm |= E1000_EICR_TX_QUEUE0 << tx_queue;
806 if (!adapter->msix_entries && msix_vector == 0)
807 msixbm |= E1000_EIMS_OTHER;
808 array_wr32(E1000_MSIXBM(0), msix_vector, msixbm);
809 q_vector->eims_value = msixbm;
812 /* 82576 uses a table that essentially consists of 2 columns
813 * with 8 rows. The ordering is column-major so we use the
814 * lower 3 bits as the row index, and the 4th bit as the
817 if (rx_queue > IGB_N0_QUEUE)
818 igb_write_ivar(hw, msix_vector,
820 (rx_queue & 0x8) << 1);
821 if (tx_queue > IGB_N0_QUEUE)
822 igb_write_ivar(hw, msix_vector,
824 ((tx_queue & 0x8) << 1) + 8);
825 q_vector->eims_value = 1 << msix_vector;
832 /* On 82580 and newer adapters the scheme is similar to 82576
833 * however instead of ordering column-major we have things
834 * ordered row-major. So we traverse the table by using
835 * bit 0 as the column offset, and the remaining bits as the
838 if (rx_queue > IGB_N0_QUEUE)
839 igb_write_ivar(hw, msix_vector,
841 (rx_queue & 0x1) << 4);
842 if (tx_queue > IGB_N0_QUEUE)
843 igb_write_ivar(hw, msix_vector,
845 ((tx_queue & 0x1) << 4) + 8);
846 q_vector->eims_value = 1 << msix_vector;
853 /* add q_vector eims value to global eims_enable_mask */
854 adapter->eims_enable_mask |= q_vector->eims_value;
856 /* configure q_vector to set itr on first interrupt */
857 q_vector->set_itr = 1;
861 * igb_configure_msix - Configure MSI-X hardware
862 * @adapter: board private structure to initialize
864 * igb_configure_msix sets up the hardware to properly
865 * generate MSI-X interrupts.
867 static void igb_configure_msix(struct igb_adapter *adapter)
871 struct e1000_hw *hw = &adapter->hw;
873 adapter->eims_enable_mask = 0;
875 /* set vector for other causes, i.e. link changes */
876 switch (hw->mac.type) {
878 tmp = rd32(E1000_CTRL_EXT);
879 /* enable MSI-X PBA support*/
880 tmp |= E1000_CTRL_EXT_PBA_CLR;
882 /* Auto-Mask interrupts upon ICR read. */
883 tmp |= E1000_CTRL_EXT_EIAME;
884 tmp |= E1000_CTRL_EXT_IRCA;
886 wr32(E1000_CTRL_EXT, tmp);
888 /* enable msix_other interrupt */
889 array_wr32(E1000_MSIXBM(0), vector++, E1000_EIMS_OTHER);
890 adapter->eims_other = E1000_EIMS_OTHER;
900 /* Turn on MSI-X capability first, or our settings
901 * won't stick. And it will take days to debug.
903 wr32(E1000_GPIE, E1000_GPIE_MSIX_MODE |
904 E1000_GPIE_PBA | E1000_GPIE_EIAME |
907 /* enable msix_other interrupt */
908 adapter->eims_other = 1 << vector;
909 tmp = (vector++ | E1000_IVAR_VALID) << 8;
911 wr32(E1000_IVAR_MISC, tmp);
914 /* do nothing, since nothing else supports MSI-X */
916 } /* switch (hw->mac.type) */
918 adapter->eims_enable_mask |= adapter->eims_other;
920 for (i = 0; i < adapter->num_q_vectors; i++)
921 igb_assign_vector(adapter->q_vector[i], vector++);
927 * igb_request_msix - Initialize MSI-X interrupts
928 * @adapter: board private structure to initialize
930 * igb_request_msix allocates MSI-X vectors and requests interrupts from the
933 static int igb_request_msix(struct igb_adapter *adapter)
935 struct net_device *netdev = adapter->netdev;
936 struct e1000_hw *hw = &adapter->hw;
937 int i, err = 0, vector = 0, free_vector = 0;
939 err = request_irq(adapter->msix_entries[vector].vector,
940 igb_msix_other, 0, netdev->name, adapter);
944 for (i = 0; i < adapter->num_q_vectors; i++) {
945 struct igb_q_vector *q_vector = adapter->q_vector[i];
949 q_vector->itr_register = hw->hw_addr + E1000_EITR(vector);
951 if (q_vector->rx.ring && q_vector->tx.ring)
952 sprintf(q_vector->name, "%s-TxRx-%u", netdev->name,
953 q_vector->rx.ring->queue_index);
954 else if (q_vector->tx.ring)
955 sprintf(q_vector->name, "%s-tx-%u", netdev->name,
956 q_vector->tx.ring->queue_index);
957 else if (q_vector->rx.ring)
958 sprintf(q_vector->name, "%s-rx-%u", netdev->name,
959 q_vector->rx.ring->queue_index);
961 sprintf(q_vector->name, "%s-unused", netdev->name);
963 err = request_irq(adapter->msix_entries[vector].vector,
964 igb_msix_ring, 0, q_vector->name,
970 igb_configure_msix(adapter);
974 /* free already assigned IRQs */
975 free_irq(adapter->msix_entries[free_vector++].vector, adapter);
978 for (i = 0; i < vector; i++) {
979 free_irq(adapter->msix_entries[free_vector++].vector,
980 adapter->q_vector[i]);
986 static void igb_reset_interrupt_capability(struct igb_adapter *adapter)
988 if (adapter->msix_entries) {
989 pci_disable_msix(adapter->pdev);
990 kfree(adapter->msix_entries);
991 adapter->msix_entries = NULL;
992 } else if (adapter->flags & IGB_FLAG_HAS_MSI) {
993 pci_disable_msi(adapter->pdev);
998 * igb_free_q_vector - Free memory allocated for specific interrupt vector
999 * @adapter: board private structure to initialize
1000 * @v_idx: Index of vector to be freed
1002 * This function frees the memory allocated to the q_vector. In addition if
1003 * NAPI is enabled it will delete any references to the NAPI struct prior
1004 * to freeing the q_vector.
1006 static void igb_free_q_vector(struct igb_adapter *adapter, int v_idx)
1008 struct igb_q_vector *q_vector = adapter->q_vector[v_idx];
1010 if (q_vector->tx.ring)
1011 adapter->tx_ring[q_vector->tx.ring->queue_index] = NULL;
1013 if (q_vector->rx.ring)
1014 adapter->tx_ring[q_vector->rx.ring->queue_index] = NULL;
1016 adapter->q_vector[v_idx] = NULL;
1017 netif_napi_del(&q_vector->napi);
1019 /* igb_get_stats64() might access the rings on this vector,
1020 * we must wait a grace period before freeing it.
1022 kfree_rcu(q_vector, rcu);
1026 * igb_free_q_vectors - Free memory allocated for interrupt vectors
1027 * @adapter: board private structure to initialize
1029 * This function frees the memory allocated to the q_vectors. In addition if
1030 * NAPI is enabled it will delete any references to the NAPI struct prior
1031 * to freeing the q_vector.
1033 static void igb_free_q_vectors(struct igb_adapter *adapter)
1035 int v_idx = adapter->num_q_vectors;
1037 adapter->num_tx_queues = 0;
1038 adapter->num_rx_queues = 0;
1039 adapter->num_q_vectors = 0;
1042 igb_free_q_vector(adapter, v_idx);
1046 * igb_clear_interrupt_scheme - reset the device to a state of no interrupts
1047 * @adapter: board private structure to initialize
1049 * This function resets the device so that it has 0 Rx queues, Tx queues, and
1050 * MSI-X interrupts allocated.
1052 static void igb_clear_interrupt_scheme(struct igb_adapter *adapter)
1054 igb_free_q_vectors(adapter);
1055 igb_reset_interrupt_capability(adapter);
1059 * igb_set_interrupt_capability - set MSI or MSI-X if supported
1060 * @adapter: board private structure to initialize
1061 * @msix: boolean value of MSIX capability
1063 * Attempt to configure interrupts using the best available
1064 * capabilities of the hardware and kernel.
1066 static void igb_set_interrupt_capability(struct igb_adapter *adapter, bool msix)
1074 /* Number of supported queues. */
1075 adapter->num_rx_queues = adapter->rss_queues;
1076 if (adapter->vfs_allocated_count)
1077 adapter->num_tx_queues = 1;
1079 adapter->num_tx_queues = adapter->rss_queues;
1081 /* start with one vector for every Rx queue */
1082 numvecs = adapter->num_rx_queues;
1084 /* if Tx handler is separate add 1 for every Tx queue */
1085 if (!(adapter->flags & IGB_FLAG_QUEUE_PAIRS))
1086 numvecs += adapter->num_tx_queues;
1088 /* store the number of vectors reserved for queues */
1089 adapter->num_q_vectors = numvecs;
1091 /* add 1 vector for link status interrupts */
1093 adapter->msix_entries = kcalloc(numvecs, sizeof(struct msix_entry),
1096 if (!adapter->msix_entries)
1099 for (i = 0; i < numvecs; i++)
1100 adapter->msix_entries[i].entry = i;
1102 err = pci_enable_msix(adapter->pdev,
1103 adapter->msix_entries,
1108 igb_reset_interrupt_capability(adapter);
1110 /* If we can't do MSI-X, try MSI */
1112 #ifdef CONFIG_PCI_IOV
1113 /* disable SR-IOV for non MSI-X configurations */
1114 if (adapter->vf_data) {
1115 struct e1000_hw *hw = &adapter->hw;
1116 /* disable iov and allow time for transactions to clear */
1117 pci_disable_sriov(adapter->pdev);
1120 kfree(adapter->vf_data);
1121 adapter->vf_data = NULL;
1122 wr32(E1000_IOVCTL, E1000_IOVCTL_REUSE_VFQ);
1125 dev_info(&adapter->pdev->dev, "IOV Disabled\n");
1128 adapter->vfs_allocated_count = 0;
1129 adapter->rss_queues = 1;
1130 adapter->flags |= IGB_FLAG_QUEUE_PAIRS;
1131 adapter->num_rx_queues = 1;
1132 adapter->num_tx_queues = 1;
1133 adapter->num_q_vectors = 1;
1134 if (!pci_enable_msi(adapter->pdev))
1135 adapter->flags |= IGB_FLAG_HAS_MSI;
1138 static void igb_add_ring(struct igb_ring *ring,
1139 struct igb_ring_container *head)
1146 * igb_alloc_q_vector - Allocate memory for a single interrupt vector
1147 * @adapter: board private structure to initialize
1148 * @v_count: q_vectors allocated on adapter, used for ring interleaving
1149 * @v_idx: index of vector in adapter struct
1150 * @txr_count: total number of Tx rings to allocate
1151 * @txr_idx: index of first Tx ring to allocate
1152 * @rxr_count: total number of Rx rings to allocate
1153 * @rxr_idx: index of first Rx ring to allocate
1155 * We allocate one q_vector. If allocation fails we return -ENOMEM.
1157 static int igb_alloc_q_vector(struct igb_adapter *adapter,
1158 int v_count, int v_idx,
1159 int txr_count, int txr_idx,
1160 int rxr_count, int rxr_idx)
1162 struct igb_q_vector *q_vector;
1163 struct igb_ring *ring;
1164 int ring_count, size;
1166 /* igb only supports 1 Tx and/or 1 Rx queue per vector */
1167 if (txr_count > 1 || rxr_count > 1)
1170 ring_count = txr_count + rxr_count;
1171 size = sizeof(struct igb_q_vector) +
1172 (sizeof(struct igb_ring) * ring_count);
1174 /* allocate q_vector and rings */
1175 q_vector = kzalloc(size, GFP_KERNEL);
1179 /* initialize NAPI */
1180 netif_napi_add(adapter->netdev, &q_vector->napi,
1183 /* tie q_vector and adapter together */
1184 adapter->q_vector[v_idx] = q_vector;
1185 q_vector->adapter = adapter;
1187 /* initialize work limits */
1188 q_vector->tx.work_limit = adapter->tx_work_limit;
1190 /* initialize ITR configuration */
1191 q_vector->itr_register = adapter->hw.hw_addr + E1000_EITR(0);
1192 q_vector->itr_val = IGB_START_ITR;
1194 /* initialize pointer to rings */
1195 ring = q_vector->ring;
1199 /* rx or rx/tx vector */
1200 if (!adapter->rx_itr_setting || adapter->rx_itr_setting > 3)
1201 q_vector->itr_val = adapter->rx_itr_setting;
1203 /* tx only vector */
1204 if (!adapter->tx_itr_setting || adapter->tx_itr_setting > 3)
1205 q_vector->itr_val = adapter->tx_itr_setting;
1209 /* assign generic ring traits */
1210 ring->dev = &adapter->pdev->dev;
1211 ring->netdev = adapter->netdev;
1213 /* configure backlink on ring */
1214 ring->q_vector = q_vector;
1216 /* update q_vector Tx values */
1217 igb_add_ring(ring, &q_vector->tx);
1219 /* For 82575, context index must be unique per ring. */
1220 if (adapter->hw.mac.type == e1000_82575)
1221 set_bit(IGB_RING_FLAG_TX_CTX_IDX, &ring->flags);
1223 /* apply Tx specific ring traits */
1224 ring->count = adapter->tx_ring_count;
1225 ring->queue_index = txr_idx;
1227 /* assign ring to adapter */
1228 adapter->tx_ring[txr_idx] = ring;
1230 /* push pointer to next ring */
1235 /* assign generic ring traits */
1236 ring->dev = &adapter->pdev->dev;
1237 ring->netdev = adapter->netdev;
1239 /* configure backlink on ring */
1240 ring->q_vector = q_vector;
1242 /* update q_vector Rx values */
1243 igb_add_ring(ring, &q_vector->rx);
1245 /* set flag indicating ring supports SCTP checksum offload */
1246 if (adapter->hw.mac.type >= e1000_82576)
1247 set_bit(IGB_RING_FLAG_RX_SCTP_CSUM, &ring->flags);
1250 * On i350, i354, i210, and i211, loopback VLAN packets
1251 * have the tag byte-swapped.
1253 if (adapter->hw.mac.type >= e1000_i350)
1254 set_bit(IGB_RING_FLAG_RX_LB_VLAN_BSWAP, &ring->flags);
1256 /* apply Rx specific ring traits */
1257 ring->count = adapter->rx_ring_count;
1258 ring->queue_index = rxr_idx;
1260 /* assign ring to adapter */
1261 adapter->rx_ring[rxr_idx] = ring;
1269 * igb_alloc_q_vectors - Allocate memory for interrupt vectors
1270 * @adapter: board private structure to initialize
1272 * We allocate one q_vector per queue interrupt. If allocation fails we
1275 static int igb_alloc_q_vectors(struct igb_adapter *adapter)
1277 int q_vectors = adapter->num_q_vectors;
1278 int rxr_remaining = adapter->num_rx_queues;
1279 int txr_remaining = adapter->num_tx_queues;
1280 int rxr_idx = 0, txr_idx = 0, v_idx = 0;
1283 if (q_vectors >= (rxr_remaining + txr_remaining)) {
1284 for (; rxr_remaining; v_idx++) {
1285 err = igb_alloc_q_vector(adapter, q_vectors, v_idx,
1291 /* update counts and index */
1297 for (; v_idx < q_vectors; v_idx++) {
1298 int rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - v_idx);
1299 int tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - v_idx);
1300 err = igb_alloc_q_vector(adapter, q_vectors, v_idx,
1301 tqpv, txr_idx, rqpv, rxr_idx);
1306 /* update counts and index */
1307 rxr_remaining -= rqpv;
1308 txr_remaining -= tqpv;
1316 adapter->num_tx_queues = 0;
1317 adapter->num_rx_queues = 0;
1318 adapter->num_q_vectors = 0;
1321 igb_free_q_vector(adapter, v_idx);
1327 * igb_init_interrupt_scheme - initialize interrupts, allocate queues/vectors
1328 * @adapter: board private structure to initialize
1329 * @msix: boolean value of MSIX capability
1331 * This function initializes the interrupts and allocates all of the queues.
1333 static int igb_init_interrupt_scheme(struct igb_adapter *adapter, bool msix)
1335 struct pci_dev *pdev = adapter->pdev;
1338 igb_set_interrupt_capability(adapter, msix);
1340 err = igb_alloc_q_vectors(adapter);
1342 dev_err(&pdev->dev, "Unable to allocate memory for vectors\n");
1343 goto err_alloc_q_vectors;
1346 igb_cache_ring_register(adapter);
1350 err_alloc_q_vectors:
1351 igb_reset_interrupt_capability(adapter);
1356 * igb_request_irq - initialize interrupts
1357 * @adapter: board private structure to initialize
1359 * Attempts to configure interrupts using the best available
1360 * capabilities of the hardware and kernel.
1362 static int igb_request_irq(struct igb_adapter *adapter)
1364 struct net_device *netdev = adapter->netdev;
1365 struct pci_dev *pdev = adapter->pdev;
1368 if (adapter->msix_entries) {
1369 err = igb_request_msix(adapter);
1372 /* fall back to MSI */
1373 igb_free_all_tx_resources(adapter);
1374 igb_free_all_rx_resources(adapter);
1376 igb_clear_interrupt_scheme(adapter);
1377 err = igb_init_interrupt_scheme(adapter, false);
1381 igb_setup_all_tx_resources(adapter);
1382 igb_setup_all_rx_resources(adapter);
1383 igb_configure(adapter);
1386 igb_assign_vector(adapter->q_vector[0], 0);
1388 if (adapter->flags & IGB_FLAG_HAS_MSI) {
1389 err = request_irq(pdev->irq, igb_intr_msi, 0,
1390 netdev->name, adapter);
1394 /* fall back to legacy interrupts */
1395 igb_reset_interrupt_capability(adapter);
1396 adapter->flags &= ~IGB_FLAG_HAS_MSI;
1399 err = request_irq(pdev->irq, igb_intr, IRQF_SHARED,
1400 netdev->name, adapter);
1403 dev_err(&pdev->dev, "Error %d getting interrupt\n",
1410 static void igb_free_irq(struct igb_adapter *adapter)
1412 if (adapter->msix_entries) {
1415 free_irq(adapter->msix_entries[vector++].vector, adapter);
1417 for (i = 0; i < adapter->num_q_vectors; i++)
1418 free_irq(adapter->msix_entries[vector++].vector,
1419 adapter->q_vector[i]);
1421 free_irq(adapter->pdev->irq, adapter);
1426 * igb_irq_disable - Mask off interrupt generation on the NIC
1427 * @adapter: board private structure
1429 static void igb_irq_disable(struct igb_adapter *adapter)
1431 struct e1000_hw *hw = &adapter->hw;
1433 /* we need to be careful when disabling interrupts. The VFs are also
1434 * mapped into these registers and so clearing the bits can cause
1435 * issues on the VF drivers so we only need to clear what we set
1437 if (adapter->msix_entries) {
1438 u32 regval = rd32(E1000_EIAM);
1439 wr32(E1000_EIAM, regval & ~adapter->eims_enable_mask);
1440 wr32(E1000_EIMC, adapter->eims_enable_mask);
1441 regval = rd32(E1000_EIAC);
1442 wr32(E1000_EIAC, regval & ~adapter->eims_enable_mask);
1446 wr32(E1000_IMC, ~0);
1448 if (adapter->msix_entries) {
1450 for (i = 0; i < adapter->num_q_vectors; i++)
1451 synchronize_irq(adapter->msix_entries[i].vector);
1453 synchronize_irq(adapter->pdev->irq);
1458 * igb_irq_enable - Enable default interrupt generation settings
1459 * @adapter: board private structure
1461 static void igb_irq_enable(struct igb_adapter *adapter)
1463 struct e1000_hw *hw = &adapter->hw;
1465 if (adapter->msix_entries) {
1466 u32 ims = E1000_IMS_LSC | E1000_IMS_DOUTSYNC | E1000_IMS_DRSTA;
1467 u32 regval = rd32(E1000_EIAC);
1468 wr32(E1000_EIAC, regval | adapter->eims_enable_mask);
1469 regval = rd32(E1000_EIAM);
1470 wr32(E1000_EIAM, regval | adapter->eims_enable_mask);
1471 wr32(E1000_EIMS, adapter->eims_enable_mask);
1472 if (adapter->vfs_allocated_count) {
1473 wr32(E1000_MBVFIMR, 0xFF);
1474 ims |= E1000_IMS_VMMB;
1476 wr32(E1000_IMS, ims);
1478 wr32(E1000_IMS, IMS_ENABLE_MASK |
1480 wr32(E1000_IAM, IMS_ENABLE_MASK |
1485 static void igb_update_mng_vlan(struct igb_adapter *adapter)
1487 struct e1000_hw *hw = &adapter->hw;
1488 u16 vid = adapter->hw.mng_cookie.vlan_id;
1489 u16 old_vid = adapter->mng_vlan_id;
1491 if (hw->mng_cookie.status & E1000_MNG_DHCP_COOKIE_STATUS_VLAN) {
1492 /* add VID to filter table */
1493 igb_vfta_set(hw, vid, true);
1494 adapter->mng_vlan_id = vid;
1496 adapter->mng_vlan_id = IGB_MNG_VLAN_NONE;
1499 if ((old_vid != (u16)IGB_MNG_VLAN_NONE) &&
1501 !test_bit(old_vid, adapter->active_vlans)) {
1502 /* remove VID from filter table */
1503 igb_vfta_set(hw, old_vid, false);
1508 * igb_release_hw_control - release control of the h/w to f/w
1509 * @adapter: address of board private structure
1511 * igb_release_hw_control resets CTRL_EXT:DRV_LOAD bit.
1512 * For ASF and Pass Through versions of f/w this means that the
1513 * driver is no longer loaded.
1515 static void igb_release_hw_control(struct igb_adapter *adapter)
1517 struct e1000_hw *hw = &adapter->hw;
1520 /* Let firmware take over control of h/w */
1521 ctrl_ext = rd32(E1000_CTRL_EXT);
1522 wr32(E1000_CTRL_EXT,
1523 ctrl_ext & ~E1000_CTRL_EXT_DRV_LOAD);
1527 * igb_get_hw_control - get control of the h/w from f/w
1528 * @adapter: address of board private structure
1530 * igb_get_hw_control sets CTRL_EXT:DRV_LOAD bit.
1531 * For ASF and Pass Through versions of f/w this means that
1532 * the driver is loaded.
1534 static void igb_get_hw_control(struct igb_adapter *adapter)
1536 struct e1000_hw *hw = &adapter->hw;
1539 /* Let firmware know the driver has taken over */
1540 ctrl_ext = rd32(E1000_CTRL_EXT);
1541 wr32(E1000_CTRL_EXT,
1542 ctrl_ext | E1000_CTRL_EXT_DRV_LOAD);
1546 * igb_configure - configure the hardware for RX and TX
1547 * @adapter: private board structure
1549 static void igb_configure(struct igb_adapter *adapter)
1551 struct net_device *netdev = adapter->netdev;
1554 igb_get_hw_control(adapter);
1555 igb_set_rx_mode(netdev);
1557 igb_restore_vlan(adapter);
1559 igb_setup_tctl(adapter);
1560 igb_setup_mrqc(adapter);
1561 igb_setup_rctl(adapter);
1563 igb_configure_tx(adapter);
1564 igb_configure_rx(adapter);
1566 igb_rx_fifo_flush_82575(&adapter->hw);
1568 /* call igb_desc_unused which always leaves
1569 * at least 1 descriptor unused to make sure
1570 * next_to_use != next_to_clean
1572 for (i = 0; i < adapter->num_rx_queues; i++) {
1573 struct igb_ring *ring = adapter->rx_ring[i];
1574 igb_alloc_rx_buffers(ring, igb_desc_unused(ring));
1579 * igb_power_up_link - Power up the phy/serdes link
1580 * @adapter: address of board private structure
1582 void igb_power_up_link(struct igb_adapter *adapter)
1584 igb_reset_phy(&adapter->hw);
1586 if (adapter->hw.phy.media_type == e1000_media_type_copper)
1587 igb_power_up_phy_copper(&adapter->hw);
1589 igb_power_up_serdes_link_82575(&adapter->hw);
1593 * igb_power_down_link - Power down the phy/serdes link
1594 * @adapter: address of board private structure
1596 static void igb_power_down_link(struct igb_adapter *adapter)
1598 if (adapter->hw.phy.media_type == e1000_media_type_copper)
1599 igb_power_down_phy_copper_82575(&adapter->hw);
1601 igb_shutdown_serdes_link_82575(&adapter->hw);
1605 * igb_up - Open the interface and prepare it to handle traffic
1606 * @adapter: board private structure
1608 int igb_up(struct igb_adapter *adapter)
1610 struct e1000_hw *hw = &adapter->hw;
1613 /* hardware has been reset, we need to reload some things */
1614 igb_configure(adapter);
1616 clear_bit(__IGB_DOWN, &adapter->state);
1618 for (i = 0; i < adapter->num_q_vectors; i++)
1619 napi_enable(&(adapter->q_vector[i]->napi));
1621 if (adapter->msix_entries)
1622 igb_configure_msix(adapter);
1624 igb_assign_vector(adapter->q_vector[0], 0);
1626 /* Clear any pending interrupts. */
1628 igb_irq_enable(adapter);
1630 /* notify VFs that reset has been completed */
1631 if (adapter->vfs_allocated_count) {
1632 u32 reg_data = rd32(E1000_CTRL_EXT);
1633 reg_data |= E1000_CTRL_EXT_PFRSTD;
1634 wr32(E1000_CTRL_EXT, reg_data);
1637 netif_tx_start_all_queues(adapter->netdev);
1639 /* start the watchdog. */
1640 hw->mac.get_link_status = 1;
1641 schedule_work(&adapter->watchdog_task);
1646 void igb_down(struct igb_adapter *adapter)
1648 struct net_device *netdev = adapter->netdev;
1649 struct e1000_hw *hw = &adapter->hw;
1653 /* signal that we're down so the interrupt handler does not
1654 * reschedule our watchdog timer
1656 set_bit(__IGB_DOWN, &adapter->state);
1658 /* disable receives in the hardware */
1659 rctl = rd32(E1000_RCTL);
1660 wr32(E1000_RCTL, rctl & ~E1000_RCTL_EN);
1661 /* flush and sleep below */
1663 netif_tx_stop_all_queues(netdev);
1665 /* disable transmits in the hardware */
1666 tctl = rd32(E1000_TCTL);
1667 tctl &= ~E1000_TCTL_EN;
1668 wr32(E1000_TCTL, tctl);
1669 /* flush both disables and wait for them to finish */
1673 igb_irq_disable(adapter);
1675 adapter->flags &= ~IGB_FLAG_NEED_LINK_UPDATE;
1677 for (i = 0; i < adapter->num_q_vectors; i++) {
1678 napi_synchronize(&(adapter->q_vector[i]->napi));
1679 napi_disable(&(adapter->q_vector[i]->napi));
1683 del_timer_sync(&adapter->watchdog_timer);
1684 del_timer_sync(&adapter->phy_info_timer);
1686 netif_carrier_off(netdev);
1688 /* record the stats before reset*/
1689 spin_lock(&adapter->stats64_lock);
1690 igb_update_stats(adapter, &adapter->stats64);
1691 spin_unlock(&adapter->stats64_lock);
1693 adapter->link_speed = 0;
1694 adapter->link_duplex = 0;
1696 if (!pci_channel_offline(adapter->pdev))
1698 igb_clean_all_tx_rings(adapter);
1699 igb_clean_all_rx_rings(adapter);
1700 #ifdef CONFIG_IGB_DCA
1702 /* since we reset the hardware DCA settings were cleared */
1703 igb_setup_dca(adapter);
1707 void igb_reinit_locked(struct igb_adapter *adapter)
1709 WARN_ON(in_interrupt());
1710 while (test_and_set_bit(__IGB_RESETTING, &adapter->state))
1714 clear_bit(__IGB_RESETTING, &adapter->state);
1717 void igb_reset(struct igb_adapter *adapter)
1719 struct pci_dev *pdev = adapter->pdev;
1720 struct e1000_hw *hw = &adapter->hw;
1721 struct e1000_mac_info *mac = &hw->mac;
1722 struct e1000_fc_info *fc = &hw->fc;
1723 u32 pba = 0, tx_space, min_tx_space, min_rx_space, hwm;
1725 /* Repartition Pba for greater than 9k mtu
1726 * To take effect CTRL.RST is required.
1728 switch (mac->type) {
1732 pba = rd32(E1000_RXPBS);
1733 pba = igb_rxpbs_adjust_82580(pba);
1736 pba = rd32(E1000_RXPBS);
1737 pba &= E1000_RXPBS_SIZE_MASK_82576;
1743 pba = E1000_PBA_34K;
1747 if ((adapter->max_frame_size > ETH_FRAME_LEN + ETH_FCS_LEN) &&
1748 (mac->type < e1000_82576)) {
1749 /* adjust PBA for jumbo frames */
1750 wr32(E1000_PBA, pba);
1752 /* To maintain wire speed transmits, the Tx FIFO should be
1753 * large enough to accommodate two full transmit packets,
1754 * rounded up to the next 1KB and expressed in KB. Likewise,
1755 * the Rx FIFO should be large enough to accommodate at least
1756 * one full receive packet and is similarly rounded up and
1759 pba = rd32(E1000_PBA);
1760 /* upper 16 bits has Tx packet buffer allocation size in KB */
1761 tx_space = pba >> 16;
1762 /* lower 16 bits has Rx packet buffer allocation size in KB */
1764 /* the Tx fifo also stores 16 bytes of information about the Tx
1765 * but don't include ethernet FCS because hardware appends it
1767 min_tx_space = (adapter->max_frame_size +
1768 sizeof(union e1000_adv_tx_desc) -
1770 min_tx_space = ALIGN(min_tx_space, 1024);
1771 min_tx_space >>= 10;
1772 /* software strips receive CRC, so leave room for it */
1773 min_rx_space = adapter->max_frame_size;
1774 min_rx_space = ALIGN(min_rx_space, 1024);
1775 min_rx_space >>= 10;
1777 /* If current Tx allocation is less than the min Tx FIFO size,
1778 * and the min Tx FIFO size is less than the current Rx FIFO
1779 * allocation, take space away from current Rx allocation
1781 if (tx_space < min_tx_space &&
1782 ((min_tx_space - tx_space) < pba)) {
1783 pba = pba - (min_tx_space - tx_space);
1785 /* if short on Rx space, Rx wins and must trump Tx
1788 if (pba < min_rx_space)
1791 wr32(E1000_PBA, pba);
1794 /* flow control settings */
1795 /* The high water mark must be low enough to fit one full frame
1796 * (or the size used for early receive) above it in the Rx FIFO.
1797 * Set it to the lower of:
1798 * - 90% of the Rx FIFO size, or
1799 * - the full Rx FIFO size minus one full frame
1801 hwm = min(((pba << 10) * 9 / 10),
1802 ((pba << 10) - 2 * adapter->max_frame_size));
1804 fc->high_water = hwm & 0xFFFFFFF0; /* 16-byte granularity */
1805 fc->low_water = fc->high_water - 16;
1806 fc->pause_time = 0xFFFF;
1808 fc->current_mode = fc->requested_mode;
1810 /* disable receive for all VFs and wait one second */
1811 if (adapter->vfs_allocated_count) {
1813 for (i = 0 ; i < adapter->vfs_allocated_count; i++)
1814 adapter->vf_data[i].flags &= IGB_VF_FLAG_PF_SET_MAC;
1816 /* ping all the active vfs to let them know we are going down */
1817 igb_ping_all_vfs(adapter);
1819 /* disable transmits and receives */
1820 wr32(E1000_VFRE, 0);
1821 wr32(E1000_VFTE, 0);
1824 /* Allow time for pending master requests to run */
1825 hw->mac.ops.reset_hw(hw);
1828 if (hw->mac.ops.init_hw(hw))
1829 dev_err(&pdev->dev, "Hardware Error\n");
1831 /* Flow control settings reset on hardware reset, so guarantee flow
1832 * control is off when forcing speed.
1834 if (!hw->mac.autoneg)
1835 igb_force_mac_fc(hw);
1837 igb_init_dmac(adapter, pba);
1838 #ifdef CONFIG_IGB_HWMON
1839 /* Re-initialize the thermal sensor on i350 devices. */
1840 if (!test_bit(__IGB_DOWN, &adapter->state)) {
1841 if (mac->type == e1000_i350 && hw->bus.func == 0) {
1842 /* If present, re-initialize the external thermal sensor
1846 mac->ops.init_thermal_sensor_thresh(hw);
1850 if (!netif_running(adapter->netdev))
1851 igb_power_down_link(adapter);
1853 igb_update_mng_vlan(adapter);
1855 /* Enable h/w to recognize an 802.1Q VLAN Ethernet packet */
1856 wr32(E1000_VET, ETHERNET_IEEE_VLAN_TYPE);
1858 /* Re-enable PTP, where applicable. */
1859 igb_ptp_reset(adapter);
1861 igb_get_phy_info(hw);
1864 static netdev_features_t igb_fix_features(struct net_device *netdev,
1865 netdev_features_t features)
1867 /* Since there is no support for separate Rx/Tx vlan accel
1868 * enable/disable make sure Tx flag is always in same state as Rx.
1870 if (features & NETIF_F_HW_VLAN_CTAG_RX)
1871 features |= NETIF_F_HW_VLAN_CTAG_TX;
1873 features &= ~NETIF_F_HW_VLAN_CTAG_TX;
1878 static int igb_set_features(struct net_device *netdev,
1879 netdev_features_t features)
1881 netdev_features_t changed = netdev->features ^ features;
1882 struct igb_adapter *adapter = netdev_priv(netdev);
1884 if (changed & NETIF_F_HW_VLAN_CTAG_RX)
1885 igb_vlan_mode(netdev, features);
1887 if (!(changed & NETIF_F_RXALL))
1890 netdev->features = features;
1892 if (netif_running(netdev))
1893 igb_reinit_locked(adapter);
1900 static const struct net_device_ops igb_netdev_ops = {
1901 .ndo_open = igb_open,
1902 .ndo_stop = igb_close,
1903 .ndo_start_xmit = igb_xmit_frame,
1904 .ndo_get_stats64 = igb_get_stats64,
1905 .ndo_set_rx_mode = igb_set_rx_mode,
1906 .ndo_set_mac_address = igb_set_mac,
1907 .ndo_change_mtu = igb_change_mtu,
1908 .ndo_do_ioctl = igb_ioctl,
1909 .ndo_tx_timeout = igb_tx_timeout,
1910 .ndo_validate_addr = eth_validate_addr,
1911 .ndo_vlan_rx_add_vid = igb_vlan_rx_add_vid,
1912 .ndo_vlan_rx_kill_vid = igb_vlan_rx_kill_vid,
1913 .ndo_set_vf_mac = igb_ndo_set_vf_mac,
1914 .ndo_set_vf_vlan = igb_ndo_set_vf_vlan,
1915 .ndo_set_vf_tx_rate = igb_ndo_set_vf_bw,
1916 .ndo_set_vf_spoofchk = igb_ndo_set_vf_spoofchk,
1917 .ndo_get_vf_config = igb_ndo_get_vf_config,
1918 #ifdef CONFIG_NET_POLL_CONTROLLER
1919 .ndo_poll_controller = igb_netpoll,
1921 .ndo_fix_features = igb_fix_features,
1922 .ndo_set_features = igb_set_features,
1926 * igb_set_fw_version - Configure version string for ethtool
1927 * @adapter: adapter struct
1929 void igb_set_fw_version(struct igb_adapter *adapter)
1931 struct e1000_hw *hw = &adapter->hw;
1932 struct e1000_fw_version fw;
1934 igb_get_fw_version(hw, &fw);
1936 switch (hw->mac.type) {
1939 if (!(igb_get_flash_presence_i210(hw))) {
1940 snprintf(adapter->fw_version,
1941 sizeof(adapter->fw_version),
1943 fw.invm_major, fw.invm_minor,
1949 /* if option is rom valid, display its version too */
1951 snprintf(adapter->fw_version,
1952 sizeof(adapter->fw_version),
1953 "%d.%d, 0x%08x, %d.%d.%d",
1954 fw.eep_major, fw.eep_minor, fw.etrack_id,
1955 fw.or_major, fw.or_build, fw.or_patch);
1957 } else if (fw.etrack_id != 0X0000) {
1958 snprintf(adapter->fw_version,
1959 sizeof(adapter->fw_version),
1961 fw.eep_major, fw.eep_minor, fw.etrack_id);
1963 snprintf(adapter->fw_version,
1964 sizeof(adapter->fw_version),
1966 fw.eep_major, fw.eep_minor, fw.eep_build);
1974 * igb_init_i2c - Init I2C interface
1975 * @adapter: pointer to adapter structure
1977 static s32 igb_init_i2c(struct igb_adapter *adapter)
1979 s32 status = E1000_SUCCESS;
1981 /* I2C interface supported on i350 devices */
1982 if (adapter->hw.mac.type != e1000_i350)
1983 return E1000_SUCCESS;
1985 /* Initialize the i2c bus which is controlled by the registers.
1986 * This bus will use the i2c_algo_bit structue that implements
1987 * the protocol through toggling of the 4 bits in the register.
1989 adapter->i2c_adap.owner = THIS_MODULE;
1990 adapter->i2c_algo = igb_i2c_algo;
1991 adapter->i2c_algo.data = adapter;
1992 adapter->i2c_adap.algo_data = &adapter->i2c_algo;
1993 adapter->i2c_adap.dev.parent = &adapter->pdev->dev;
1994 strlcpy(adapter->i2c_adap.name, "igb BB",
1995 sizeof(adapter->i2c_adap.name));
1996 status = i2c_bit_add_bus(&adapter->i2c_adap);
2001 * igb_probe - Device Initialization Routine
2002 * @pdev: PCI device information struct
2003 * @ent: entry in igb_pci_tbl
2005 * Returns 0 on success, negative on failure
2007 * igb_probe initializes an adapter identified by a pci_dev structure.
2008 * The OS initialization, configuring of the adapter private structure,
2009 * and a hardware reset occur.
2011 static int igb_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2013 struct net_device *netdev;
2014 struct igb_adapter *adapter;
2015 struct e1000_hw *hw;
2016 u16 eeprom_data = 0;
2018 static int global_quad_port_a; /* global quad port a indication */
2019 const struct e1000_info *ei = igb_info_tbl[ent->driver_data];
2020 unsigned long mmio_start, mmio_len;
2021 int err, pci_using_dac;
2022 u8 part_str[E1000_PBANUM_LENGTH];
2024 /* Catch broken hardware that put the wrong VF device ID in
2025 * the PCIe SR-IOV capability.
2027 if (pdev->is_virtfn) {
2028 WARN(1, KERN_ERR "%s (%hx:%hx) should not be a VF!\n",
2029 pci_name(pdev), pdev->vendor, pdev->device);
2033 err = pci_enable_device_mem(pdev);
2038 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
2042 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
2045 "No usable DMA configuration, aborting\n");
2050 err = pci_request_selected_regions(pdev, pci_select_bars(pdev,
2056 pci_enable_pcie_error_reporting(pdev);
2058 pci_set_master(pdev);
2059 pci_save_state(pdev);
2062 netdev = alloc_etherdev_mq(sizeof(struct igb_adapter),
2065 goto err_alloc_etherdev;
2067 SET_NETDEV_DEV(netdev, &pdev->dev);
2069 pci_set_drvdata(pdev, netdev);
2070 adapter = netdev_priv(netdev);
2071 adapter->netdev = netdev;
2072 adapter->pdev = pdev;
2075 adapter->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
2077 mmio_start = pci_resource_start(pdev, 0);
2078 mmio_len = pci_resource_len(pdev, 0);
2081 hw->hw_addr = ioremap(mmio_start, mmio_len);
2085 netdev->netdev_ops = &igb_netdev_ops;
2086 igb_set_ethtool_ops(netdev);
2087 netdev->watchdog_timeo = 5 * HZ;
2089 strncpy(netdev->name, pci_name(pdev), sizeof(netdev->name) - 1);
2091 netdev->mem_start = mmio_start;
2092 netdev->mem_end = mmio_start + mmio_len;
2094 /* PCI config space info */
2095 hw->vendor_id = pdev->vendor;
2096 hw->device_id = pdev->device;
2097 hw->revision_id = pdev->revision;
2098 hw->subsystem_vendor_id = pdev->subsystem_vendor;
2099 hw->subsystem_device_id = pdev->subsystem_device;
2101 /* Copy the default MAC, PHY and NVM function pointers */
2102 memcpy(&hw->mac.ops, ei->mac_ops, sizeof(hw->mac.ops));
2103 memcpy(&hw->phy.ops, ei->phy_ops, sizeof(hw->phy.ops));
2104 memcpy(&hw->nvm.ops, ei->nvm_ops, sizeof(hw->nvm.ops));
2105 /* Initialize skew-specific constants */
2106 err = ei->get_invariants(hw);
2110 /* setup the private structure */
2111 err = igb_sw_init(adapter);
2115 igb_get_bus_info_pcie(hw);
2117 hw->phy.autoneg_wait_to_complete = false;
2119 /* Copper options */
2120 if (hw->phy.media_type == e1000_media_type_copper) {
2121 hw->phy.mdix = AUTO_ALL_MODES;
2122 hw->phy.disable_polarity_correction = false;
2123 hw->phy.ms_type = e1000_ms_hw_default;
2126 if (igb_check_reset_block(hw))
2127 dev_info(&pdev->dev,
2128 "PHY reset is blocked due to SOL/IDER session.\n");
2130 /* features is initialized to 0 in allocation, it might have bits
2131 * set by igb_sw_init so we should use an or instead of an
2134 netdev->features |= NETIF_F_SG |
2141 NETIF_F_HW_VLAN_CTAG_RX |
2142 NETIF_F_HW_VLAN_CTAG_TX;
2144 /* copy netdev features into list of user selectable features */
2145 netdev->hw_features |= netdev->features;
2146 netdev->hw_features |= NETIF_F_RXALL;
2148 /* set this bit last since it cannot be part of hw_features */
2149 netdev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
2151 netdev->vlan_features |= NETIF_F_TSO |
2157 netdev->priv_flags |= IFF_SUPP_NOFCS;
2159 if (pci_using_dac) {
2160 netdev->features |= NETIF_F_HIGHDMA;
2161 netdev->vlan_features |= NETIF_F_HIGHDMA;
2164 if (hw->mac.type >= e1000_82576) {
2165 netdev->hw_features |= NETIF_F_SCTP_CSUM;
2166 netdev->features |= NETIF_F_SCTP_CSUM;
2169 netdev->priv_flags |= IFF_UNICAST_FLT;
2171 adapter->en_mng_pt = igb_enable_mng_pass_thru(hw);
2173 /* before reading the NVM, reset the controller to put the device in a
2174 * known good starting state
2176 hw->mac.ops.reset_hw(hw);
2178 /* make sure the NVM is good , i211/i210 parts can have special NVM
2179 * that doesn't contain a checksum
2181 switch (hw->mac.type) {
2184 if (igb_get_flash_presence_i210(hw)) {
2185 if (hw->nvm.ops.validate(hw) < 0) {
2187 "The NVM Checksum Is Not Valid\n");
2194 if (hw->nvm.ops.validate(hw) < 0) {
2195 dev_err(&pdev->dev, "The NVM Checksum Is Not Valid\n");
2202 /* copy the MAC address out of the NVM */
2203 if (hw->mac.ops.read_mac_addr(hw))
2204 dev_err(&pdev->dev, "NVM Read Error\n");
2206 memcpy(netdev->dev_addr, hw->mac.addr, netdev->addr_len);
2208 if (!is_valid_ether_addr(netdev->dev_addr)) {
2209 dev_err(&pdev->dev, "Invalid MAC Address\n");
2214 /* get firmware version for ethtool -i */
2215 igb_set_fw_version(adapter);
2217 setup_timer(&adapter->watchdog_timer, igb_watchdog,
2218 (unsigned long) adapter);
2219 setup_timer(&adapter->phy_info_timer, igb_update_phy_info,
2220 (unsigned long) adapter);
2222 INIT_WORK(&adapter->reset_task, igb_reset_task);
2223 INIT_WORK(&adapter->watchdog_task, igb_watchdog_task);
2225 /* Initialize link properties that are user-changeable */
2226 adapter->fc_autoneg = true;
2227 hw->mac.autoneg = true;
2228 hw->phy.autoneg_advertised = 0x2f;
2230 hw->fc.requested_mode = e1000_fc_default;
2231 hw->fc.current_mode = e1000_fc_default;
2233 igb_validate_mdi_setting(hw);
2235 /* By default, support wake on port A */
2236 if (hw->bus.func == 0)
2237 adapter->flags |= IGB_FLAG_WOL_SUPPORTED;
2239 /* Check the NVM for wake support on non-port A ports */
2240 if (hw->mac.type >= e1000_82580)
2241 hw->nvm.ops.read(hw, NVM_INIT_CONTROL3_PORT_A +
2242 NVM_82580_LAN_FUNC_OFFSET(hw->bus.func), 1,
2244 else if (hw->bus.func == 1)
2245 hw->nvm.ops.read(hw, NVM_INIT_CONTROL3_PORT_B, 1, &eeprom_data);
2247 if (eeprom_data & IGB_EEPROM_APME)
2248 adapter->flags |= IGB_FLAG_WOL_SUPPORTED;
2250 /* now that we have the eeprom settings, apply the special cases where
2251 * the eeprom may be wrong or the board simply won't support wake on
2252 * lan on a particular port
2254 switch (pdev->device) {
2255 case E1000_DEV_ID_82575GB_QUAD_COPPER:
2256 adapter->flags &= ~IGB_FLAG_WOL_SUPPORTED;
2258 case E1000_DEV_ID_82575EB_FIBER_SERDES:
2259 case E1000_DEV_ID_82576_FIBER:
2260 case E1000_DEV_ID_82576_SERDES:
2261 /* Wake events only supported on port A for dual fiber
2262 * regardless of eeprom setting
2264 if (rd32(E1000_STATUS) & E1000_STATUS_FUNC_1)
2265 adapter->flags &= ~IGB_FLAG_WOL_SUPPORTED;
2267 case E1000_DEV_ID_82576_QUAD_COPPER:
2268 case E1000_DEV_ID_82576_QUAD_COPPER_ET2:
2269 /* if quad port adapter, disable WoL on all but port A */
2270 if (global_quad_port_a != 0)
2271 adapter->flags &= ~IGB_FLAG_WOL_SUPPORTED;
2273 adapter->flags |= IGB_FLAG_QUAD_PORT_A;
2274 /* Reset for multiple quad port adapters */
2275 if (++global_quad_port_a == 4)
2276 global_quad_port_a = 0;
2279 /* If the device can't wake, don't set software support */
2280 if (!device_can_wakeup(&adapter->pdev->dev))
2281 adapter->flags &= ~IGB_FLAG_WOL_SUPPORTED;
2284 /* initialize the wol settings based on the eeprom settings */
2285 if (adapter->flags & IGB_FLAG_WOL_SUPPORTED)
2286 adapter->wol |= E1000_WUFC_MAG;
2288 /* Some vendors want WoL disabled by default, but still supported */
2289 if ((hw->mac.type == e1000_i350) &&
2290 (pdev->subsystem_vendor == PCI_VENDOR_ID_HP)) {
2291 adapter->flags |= IGB_FLAG_WOL_SUPPORTED;
2295 device_set_wakeup_enable(&adapter->pdev->dev,
2296 adapter->flags & IGB_FLAG_WOL_SUPPORTED);
2298 /* reset the hardware with the new settings */
2301 /* Init the I2C interface */
2302 err = igb_init_i2c(adapter);
2304 dev_err(&pdev->dev, "failed to init i2c interface\n");
2308 /* let the f/w know that the h/w is now under the control of the
2310 igb_get_hw_control(adapter);
2312 strcpy(netdev->name, "eth%d");
2313 err = register_netdev(netdev);
2317 /* carrier off reporting is important to ethtool even BEFORE open */
2318 netif_carrier_off(netdev);
2320 #ifdef CONFIG_IGB_DCA
2321 if (dca_add_requester(&pdev->dev) == 0) {
2322 adapter->flags |= IGB_FLAG_DCA_ENABLED;
2323 dev_info(&pdev->dev, "DCA enabled\n");
2324 igb_setup_dca(adapter);
2328 #ifdef CONFIG_IGB_HWMON
2329 /* Initialize the thermal sensor on i350 devices. */
2330 if (hw->mac.type == e1000_i350 && hw->bus.func == 0) {
2333 /* Read the NVM to determine if this i350 device supports an
2334 * external thermal sensor.
2336 hw->nvm.ops.read(hw, NVM_ETS_CFG, 1, &ets_word);
2337 if (ets_word != 0x0000 && ets_word != 0xFFFF)
2338 adapter->ets = true;
2340 adapter->ets = false;
2341 if (igb_sysfs_init(adapter))
2343 "failed to allocate sysfs resources\n");
2345 adapter->ets = false;
2348 /* do hw tstamp init after resetting */
2349 igb_ptp_init(adapter);
2351 dev_info(&pdev->dev, "Intel(R) Gigabit Ethernet Network Connection\n");
2352 /* print bus type/speed/width info, not applicable to i354 */
2353 if (hw->mac.type != e1000_i354) {
2354 dev_info(&pdev->dev, "%s: (PCIe:%s:%s) %pM\n",
2356 ((hw->bus.speed == e1000_bus_speed_2500) ? "2.5Gb/s" :
2357 (hw->bus.speed == e1000_bus_speed_5000) ? "5.0Gb/s" :
2359 ((hw->bus.width == e1000_bus_width_pcie_x4) ?
2361 (hw->bus.width == e1000_bus_width_pcie_x2) ?
2363 (hw->bus.width == e1000_bus_width_pcie_x1) ?
2364 "Width x1" : "unknown"), netdev->dev_addr);
2367 if ((hw->mac.type >= e1000_i210 ||
2368 igb_get_flash_presence_i210(hw))) {
2369 ret_val = igb_read_part_string(hw, part_str,
2370 E1000_PBANUM_LENGTH);
2372 ret_val = -E1000_ERR_INVM_VALUE_NOT_FOUND;
2376 strcpy(part_str, "Unknown");
2377 dev_info(&pdev->dev, "%s: PBA No: %s\n", netdev->name, part_str);
2378 dev_info(&pdev->dev,
2379 "Using %s interrupts. %d rx queue(s), %d tx queue(s)\n",
2380 adapter->msix_entries ? "MSI-X" :
2381 (adapter->flags & IGB_FLAG_HAS_MSI) ? "MSI" : "legacy",
2382 adapter->num_rx_queues, adapter->num_tx_queues);
2383 switch (hw->mac.type) {
2387 igb_set_eee_i350(hw);
2390 if (hw->phy.media_type == e1000_media_type_copper) {
2391 if ((rd32(E1000_CTRL_EXT) &
2392 E1000_CTRL_EXT_LINK_MODE_SGMII))
2393 igb_set_eee_i354(hw);
2400 pm_runtime_put_noidle(&pdev->dev);
2404 igb_release_hw_control(adapter);
2405 memset(&adapter->i2c_adap, 0, sizeof(adapter->i2c_adap));
2407 if (!igb_check_reset_block(hw))
2410 if (hw->flash_address)
2411 iounmap(hw->flash_address);
2413 igb_clear_interrupt_scheme(adapter);
2414 iounmap(hw->hw_addr);
2416 free_netdev(netdev);
2418 pci_release_selected_regions(pdev,
2419 pci_select_bars(pdev, IORESOURCE_MEM));
2422 pci_disable_device(pdev);
2426 #ifdef CONFIG_PCI_IOV
2427 static int igb_disable_sriov(struct pci_dev *pdev)
2429 struct net_device *netdev = pci_get_drvdata(pdev);
2430 struct igb_adapter *adapter = netdev_priv(netdev);
2431 struct e1000_hw *hw = &adapter->hw;
2433 /* reclaim resources allocated to VFs */
2434 if (adapter->vf_data) {
2435 /* disable iov and allow time for transactions to clear */
2436 if (pci_vfs_assigned(pdev)) {
2437 dev_warn(&pdev->dev,
2438 "Cannot deallocate SR-IOV virtual functions while they are assigned - VFs will not be deallocated\n");
2441 pci_disable_sriov(pdev);
2445 kfree(adapter->vf_data);
2446 adapter->vf_data = NULL;
2447 adapter->vfs_allocated_count = 0;
2448 wr32(E1000_IOVCTL, E1000_IOVCTL_REUSE_VFQ);
2451 dev_info(&pdev->dev, "IOV Disabled\n");
2453 /* Re-enable DMA Coalescing flag since IOV is turned off */
2454 adapter->flags |= IGB_FLAG_DMAC;
2460 static int igb_enable_sriov(struct pci_dev *pdev, int num_vfs)
2462 struct net_device *netdev = pci_get_drvdata(pdev);
2463 struct igb_adapter *adapter = netdev_priv(netdev);
2464 int old_vfs = pci_num_vf(pdev);
2468 if (!adapter->msix_entries || num_vfs > 7) {
2476 dev_info(&pdev->dev, "%d pre-allocated VFs found - override max_vfs setting of %d\n",
2478 adapter->vfs_allocated_count = old_vfs;
2480 adapter->vfs_allocated_count = num_vfs;
2482 adapter->vf_data = kcalloc(adapter->vfs_allocated_count,
2483 sizeof(struct vf_data_storage), GFP_KERNEL);
2485 /* if allocation failed then we do not support SR-IOV */
2486 if (!adapter->vf_data) {
2487 adapter->vfs_allocated_count = 0;
2489 "Unable to allocate memory for VF Data Storage\n");
2494 /* only call pci_enable_sriov() if no VFs are allocated already */
2496 err = pci_enable_sriov(pdev, adapter->vfs_allocated_count);
2500 dev_info(&pdev->dev, "%d VFs allocated\n",
2501 adapter->vfs_allocated_count);
2502 for (i = 0; i < adapter->vfs_allocated_count; i++)
2503 igb_vf_configure(adapter, i);
2505 /* DMA Coalescing is not supported in IOV mode. */
2506 adapter->flags &= ~IGB_FLAG_DMAC;
2510 kfree(adapter->vf_data);
2511 adapter->vf_data = NULL;
2512 adapter->vfs_allocated_count = 0;
2519 * igb_remove_i2c - Cleanup I2C interface
2520 * @adapter: pointer to adapter structure
2522 static void igb_remove_i2c(struct igb_adapter *adapter)
2524 /* free the adapter bus structure */
2525 i2c_del_adapter(&adapter->i2c_adap);
2529 * igb_remove - Device Removal Routine
2530 * @pdev: PCI device information struct
2532 * igb_remove is called by the PCI subsystem to alert the driver
2533 * that it should release a PCI device. The could be caused by a
2534 * Hot-Plug event, or because the driver is going to be removed from
2537 static void igb_remove(struct pci_dev *pdev)
2539 struct net_device *netdev = pci_get_drvdata(pdev);
2540 struct igb_adapter *adapter = netdev_priv(netdev);
2541 struct e1000_hw *hw = &adapter->hw;
2543 pm_runtime_get_noresume(&pdev->dev);
2544 #ifdef CONFIG_IGB_HWMON
2545 igb_sysfs_exit(adapter);
2547 igb_remove_i2c(adapter);
2548 igb_ptp_stop(adapter);
2549 /* The watchdog timer may be rescheduled, so explicitly
2550 * disable watchdog from being rescheduled.
2552 set_bit(__IGB_DOWN, &adapter->state);
2553 del_timer_sync(&adapter->watchdog_timer);
2554 del_timer_sync(&adapter->phy_info_timer);
2556 cancel_work_sync(&adapter->reset_task);
2557 cancel_work_sync(&adapter->watchdog_task);
2559 #ifdef CONFIG_IGB_DCA
2560 if (adapter->flags & IGB_FLAG_DCA_ENABLED) {
2561 dev_info(&pdev->dev, "DCA disabled\n");
2562 dca_remove_requester(&pdev->dev);
2563 adapter->flags &= ~IGB_FLAG_DCA_ENABLED;
2564 wr32(E1000_DCA_CTRL, E1000_DCA_CTRL_DCA_MODE_DISABLE);
2568 /* Release control of h/w to f/w. If f/w is AMT enabled, this
2569 * would have already happened in close and is redundant.
2571 igb_release_hw_control(adapter);
2573 unregister_netdev(netdev);
2575 igb_clear_interrupt_scheme(adapter);
2577 #ifdef CONFIG_PCI_IOV
2578 igb_disable_sriov(pdev);
2581 iounmap(hw->hw_addr);
2582 if (hw->flash_address)
2583 iounmap(hw->flash_address);
2584 pci_release_selected_regions(pdev,
2585 pci_select_bars(pdev, IORESOURCE_MEM));
2587 kfree(adapter->shadow_vfta);
2588 free_netdev(netdev);
2590 pci_disable_pcie_error_reporting(pdev);
2592 pci_disable_device(pdev);
2596 * igb_probe_vfs - Initialize vf data storage and add VFs to pci config space
2597 * @adapter: board private structure to initialize
2599 * This function initializes the vf specific data storage and then attempts to
2600 * allocate the VFs. The reason for ordering it this way is because it is much
2601 * mor expensive time wise to disable SR-IOV than it is to allocate and free
2602 * the memory for the VFs.
2604 static void igb_probe_vfs(struct igb_adapter *adapter)
2606 #ifdef CONFIG_PCI_IOV
2607 struct pci_dev *pdev = adapter->pdev;
2608 struct e1000_hw *hw = &adapter->hw;
2610 /* Virtualization features not supported on i210 family. */
2611 if ((hw->mac.type == e1000_i210) || (hw->mac.type == e1000_i211))
2614 pci_sriov_set_totalvfs(pdev, 7);
2615 igb_pci_enable_sriov(pdev, max_vfs);
2617 #endif /* CONFIG_PCI_IOV */
2620 static void igb_init_queue_configuration(struct igb_adapter *adapter)
2622 struct e1000_hw *hw = &adapter->hw;
2625 /* Determine the maximum number of RSS queues supported. */
2626 switch (hw->mac.type) {
2628 max_rss_queues = IGB_MAX_RX_QUEUES_I211;
2632 max_rss_queues = IGB_MAX_RX_QUEUES_82575;
2635 /* I350 cannot do RSS and SR-IOV at the same time */
2636 if (!!adapter->vfs_allocated_count) {
2642 if (!!adapter->vfs_allocated_count) {
2650 max_rss_queues = IGB_MAX_RX_QUEUES;
2654 adapter->rss_queues = min_t(u32, max_rss_queues, num_online_cpus());
2656 /* Determine if we need to pair queues. */
2657 switch (hw->mac.type) {
2660 /* Device supports enough interrupts without queue pairing. */
2663 /* If VFs are going to be allocated with RSS queues then we
2664 * should pair the queues in order to conserve interrupts due
2665 * to limited supply.
2667 if ((adapter->rss_queues > 1) &&
2668 (adapter->vfs_allocated_count > 6))
2669 adapter->flags |= IGB_FLAG_QUEUE_PAIRS;
2676 /* If rss_queues > half of max_rss_queues, pair the queues in
2677 * order to conserve interrupts due to limited supply.
2679 if (adapter->rss_queues > (max_rss_queues / 2))
2680 adapter->flags |= IGB_FLAG_QUEUE_PAIRS;
2686 * igb_sw_init - Initialize general software structures (struct igb_adapter)
2687 * @adapter: board private structure to initialize
2689 * igb_sw_init initializes the Adapter private data structure.
2690 * Fields are initialized based on PCI device information and
2691 * OS network device settings (MTU size).
2693 static int igb_sw_init(struct igb_adapter *adapter)
2695 struct e1000_hw *hw = &adapter->hw;
2696 struct net_device *netdev = adapter->netdev;
2697 struct pci_dev *pdev = adapter->pdev;
2699 pci_read_config_word(pdev, PCI_COMMAND, &hw->bus.pci_cmd_word);
2701 /* set default ring sizes */
2702 adapter->tx_ring_count = IGB_DEFAULT_TXD;
2703 adapter->rx_ring_count = IGB_DEFAULT_RXD;
2705 /* set default ITR values */
2706 adapter->rx_itr_setting = IGB_DEFAULT_ITR;
2707 adapter->tx_itr_setting = IGB_DEFAULT_ITR;
2709 /* set default work limits */
2710 adapter->tx_work_limit = IGB_DEFAULT_TX_WORK;
2712 adapter->max_frame_size = netdev->mtu + ETH_HLEN + ETH_FCS_LEN +
2714 adapter->min_frame_size = ETH_ZLEN + ETH_FCS_LEN;
2716 spin_lock_init(&adapter->stats64_lock);
2717 #ifdef CONFIG_PCI_IOV
2718 switch (hw->mac.type) {
2722 dev_warn(&pdev->dev,
2723 "Maximum of 7 VFs per PF, using max\n");
2724 max_vfs = adapter->vfs_allocated_count = 7;
2726 adapter->vfs_allocated_count = max_vfs;
2727 if (adapter->vfs_allocated_count)
2728 dev_warn(&pdev->dev,
2729 "Enabling SR-IOV VFs using the module parameter is deprecated - please use the pci sysfs interface.\n");
2734 #endif /* CONFIG_PCI_IOV */
2736 igb_init_queue_configuration(adapter);
2738 /* Setup and initialize a copy of the hw vlan table array */
2739 adapter->shadow_vfta = kcalloc(E1000_VLAN_FILTER_TBL_SIZE, sizeof(u32),
2742 /* This call may decrease the number of queues */
2743 if (igb_init_interrupt_scheme(adapter, true)) {
2744 dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
2748 igb_probe_vfs(adapter);
2750 /* Explicitly disable IRQ since the NIC can be in any state. */
2751 igb_irq_disable(adapter);
2753 if (hw->mac.type >= e1000_i350)
2754 adapter->flags &= ~IGB_FLAG_DMAC;
2756 set_bit(__IGB_DOWN, &adapter->state);
2761 * igb_open - Called when a network interface is made active
2762 * @netdev: network interface device structure
2764 * Returns 0 on success, negative value on failure
2766 * The open entry point is called when a network interface is made
2767 * active by the system (IFF_UP). At this point all resources needed
2768 * for transmit and receive operations are allocated, the interrupt
2769 * handler is registered with the OS, the watchdog timer is started,
2770 * and the stack is notified that the interface is ready.
2772 static int __igb_open(struct net_device *netdev, bool resuming)
2774 struct igb_adapter *adapter = netdev_priv(netdev);
2775 struct e1000_hw *hw = &adapter->hw;
2776 struct pci_dev *pdev = adapter->pdev;
2780 /* disallow open during test */
2781 if (test_bit(__IGB_TESTING, &adapter->state)) {
2787 pm_runtime_get_sync(&pdev->dev);
2789 netif_carrier_off(netdev);
2791 /* allocate transmit descriptors */
2792 err = igb_setup_all_tx_resources(adapter);
2796 /* allocate receive descriptors */
2797 err = igb_setup_all_rx_resources(adapter);
2801 igb_power_up_link(adapter);
2803 /* before we allocate an interrupt, we must be ready to handle it.
2804 * Setting DEBUG_SHIRQ in the kernel makes it fire an interrupt
2805 * as soon as we call pci_request_irq, so we have to setup our
2806 * clean_rx handler before we do so.
2808 igb_configure(adapter);
2810 err = igb_request_irq(adapter);
2814 /* Notify the stack of the actual queue counts. */
2815 err = netif_set_real_num_tx_queues(adapter->netdev,
2816 adapter->num_tx_queues);
2818 goto err_set_queues;
2820 err = netif_set_real_num_rx_queues(adapter->netdev,
2821 adapter->num_rx_queues);
2823 goto err_set_queues;
2825 /* From here on the code is the same as igb_up() */
2826 clear_bit(__IGB_DOWN, &adapter->state);
2828 for (i = 0; i < adapter->num_q_vectors; i++)
2829 napi_enable(&(adapter->q_vector[i]->napi));
2831 /* Clear any pending interrupts. */
2834 igb_irq_enable(adapter);
2836 /* notify VFs that reset has been completed */
2837 if (adapter->vfs_allocated_count) {
2838 u32 reg_data = rd32(E1000_CTRL_EXT);
2839 reg_data |= E1000_CTRL_EXT_PFRSTD;
2840 wr32(E1000_CTRL_EXT, reg_data);
2843 netif_tx_start_all_queues(netdev);
2846 pm_runtime_put(&pdev->dev);
2848 /* start the watchdog. */
2849 hw->mac.get_link_status = 1;
2850 schedule_work(&adapter->watchdog_task);
2855 igb_free_irq(adapter);
2857 igb_release_hw_control(adapter);
2858 igb_power_down_link(adapter);
2859 igb_free_all_rx_resources(adapter);
2861 igb_free_all_tx_resources(adapter);
2865 pm_runtime_put(&pdev->dev);
2870 static int igb_open(struct net_device *netdev)
2872 return __igb_open(netdev, false);
2876 * igb_close - Disables a network interface
2877 * @netdev: network interface device structure
2879 * Returns 0, this is not allowed to fail
2881 * The close entry point is called when an interface is de-activated
2882 * by the OS. The hardware is still under the driver's control, but
2883 * needs to be disabled. A global MAC reset is issued to stop the
2884 * hardware, and all transmit and receive resources are freed.
2886 static int __igb_close(struct net_device *netdev, bool suspending)
2888 struct igb_adapter *adapter = netdev_priv(netdev);
2889 struct pci_dev *pdev = adapter->pdev;
2891 WARN_ON(test_bit(__IGB_RESETTING, &adapter->state));
2894 pm_runtime_get_sync(&pdev->dev);
2897 igb_free_irq(adapter);
2899 igb_free_all_tx_resources(adapter);
2900 igb_free_all_rx_resources(adapter);
2903 pm_runtime_put_sync(&pdev->dev);
2907 static int igb_close(struct net_device *netdev)
2909 return __igb_close(netdev, false);
2913 * igb_setup_tx_resources - allocate Tx resources (Descriptors)
2914 * @tx_ring: tx descriptor ring (for a specific queue) to setup
2916 * Return 0 on success, negative on failure
2918 int igb_setup_tx_resources(struct igb_ring *tx_ring)
2920 struct device *dev = tx_ring->dev;
2923 size = sizeof(struct igb_tx_buffer) * tx_ring->count;
2925 tx_ring->tx_buffer_info = vzalloc(size);
2926 if (!tx_ring->tx_buffer_info)
2929 /* round up to nearest 4K */
2930 tx_ring->size = tx_ring->count * sizeof(union e1000_adv_tx_desc);
2931 tx_ring->size = ALIGN(tx_ring->size, 4096);
2933 tx_ring->desc = dma_alloc_coherent(dev, tx_ring->size,
2934 &tx_ring->dma, GFP_KERNEL);
2938 tx_ring->next_to_use = 0;
2939 tx_ring->next_to_clean = 0;
2944 vfree(tx_ring->tx_buffer_info);
2945 tx_ring->tx_buffer_info = NULL;
2946 dev_err(dev, "Unable to allocate memory for the Tx descriptor ring\n");
2951 * igb_setup_all_tx_resources - wrapper to allocate Tx resources
2952 * (Descriptors) for all queues
2953 * @adapter: board private structure
2955 * Return 0 on success, negative on failure
2957 static int igb_setup_all_tx_resources(struct igb_adapter *adapter)
2959 struct pci_dev *pdev = adapter->pdev;
2962 for (i = 0; i < adapter->num_tx_queues; i++) {
2963 err = igb_setup_tx_resources(adapter->tx_ring[i]);
2966 "Allocation for Tx Queue %u failed\n", i);
2967 for (i--; i >= 0; i--)
2968 igb_free_tx_resources(adapter->tx_ring[i]);
2977 * igb_setup_tctl - configure the transmit control registers
2978 * @adapter: Board private structure
2980 void igb_setup_tctl(struct igb_adapter *adapter)
2982 struct e1000_hw *hw = &adapter->hw;
2985 /* disable queue 0 which is enabled by default on 82575 and 82576 */
2986 wr32(E1000_TXDCTL(0), 0);
2988 /* Program the Transmit Control Register */
2989 tctl = rd32(E1000_TCTL);
2990 tctl &= ~E1000_TCTL_CT;
2991 tctl |= E1000_TCTL_PSP | E1000_TCTL_RTLC |
2992 (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
2994 igb_config_collision_dist(hw);
2996 /* Enable transmits */
2997 tctl |= E1000_TCTL_EN;
2999 wr32(E1000_TCTL, tctl);
3003 * igb_configure_tx_ring - Configure transmit ring after Reset
3004 * @adapter: board private structure
3005 * @ring: tx ring to configure
3007 * Configure a transmit ring after a reset.
3009 void igb_configure_tx_ring(struct igb_adapter *adapter,
3010 struct igb_ring *ring)
3012 struct e1000_hw *hw = &adapter->hw;
3014 u64 tdba = ring->dma;
3015 int reg_idx = ring->reg_idx;
3017 /* disable the queue */
3018 wr32(E1000_TXDCTL(reg_idx), 0);
3022 wr32(E1000_TDLEN(reg_idx),
3023 ring->count * sizeof(union e1000_adv_tx_desc));
3024 wr32(E1000_TDBAL(reg_idx),
3025 tdba & 0x00000000ffffffffULL);
3026 wr32(E1000_TDBAH(reg_idx), tdba >> 32);
3028 ring->tail = hw->hw_addr + E1000_TDT(reg_idx);
3029 wr32(E1000_TDH(reg_idx), 0);
3030 writel(0, ring->tail);
3032 txdctl |= IGB_TX_PTHRESH;
3033 txdctl |= IGB_TX_HTHRESH << 8;
3034 txdctl |= IGB_TX_WTHRESH << 16;
3036 txdctl |= E1000_TXDCTL_QUEUE_ENABLE;
3037 wr32(E1000_TXDCTL(reg_idx), txdctl);
3041 * igb_configure_tx - Configure transmit Unit after Reset
3042 * @adapter: board private structure
3044 * Configure the Tx unit of the MAC after a reset.
3046 static void igb_configure_tx(struct igb_adapter *adapter)
3050 for (i = 0; i < adapter->num_tx_queues; i++)
3051 igb_configure_tx_ring(adapter, adapter->tx_ring[i]);
3055 * igb_setup_rx_resources - allocate Rx resources (Descriptors)
3056 * @rx_ring: Rx descriptor ring (for a specific queue) to setup
3058 * Returns 0 on success, negative on failure
3060 int igb_setup_rx_resources(struct igb_ring *rx_ring)
3062 struct device *dev = rx_ring->dev;
3065 size = sizeof(struct igb_rx_buffer) * rx_ring->count;
3067 rx_ring->rx_buffer_info = vzalloc(size);
3068 if (!rx_ring->rx_buffer_info)
3071 /* Round up to nearest 4K */
3072 rx_ring->size = rx_ring->count * sizeof(union e1000_adv_rx_desc);
3073 rx_ring->size = ALIGN(rx_ring->size, 4096);
3075 rx_ring->desc = dma_alloc_coherent(dev, rx_ring->size,
3076 &rx_ring->dma, GFP_KERNEL);
3080 rx_ring->next_to_alloc = 0;
3081 rx_ring->next_to_clean = 0;
3082 rx_ring->next_to_use = 0;
3087 vfree(rx_ring->rx_buffer_info);
3088 rx_ring->rx_buffer_info = NULL;
3089 dev_err(dev, "Unable to allocate memory for the Rx descriptor ring\n");
3094 * igb_setup_all_rx_resources - wrapper to allocate Rx resources
3095 * (Descriptors) for all queues
3096 * @adapter: board private structure
3098 * Return 0 on success, negative on failure
3100 static int igb_setup_all_rx_resources(struct igb_adapter *adapter)
3102 struct pci_dev *pdev = adapter->pdev;
3105 for (i = 0; i < adapter->num_rx_queues; i++) {
3106 err = igb_setup_rx_resources(adapter->rx_ring[i]);
3109 "Allocation for Rx Queue %u failed\n", i);
3110 for (i--; i >= 0; i--)
3111 igb_free_rx_resources(adapter->rx_ring[i]);
3120 * igb_setup_mrqc - configure the multiple receive queue control registers
3121 * @adapter: Board private structure
3123 static void igb_setup_mrqc(struct igb_adapter *adapter)
3125 struct e1000_hw *hw = &adapter->hw;
3127 u32 j, num_rx_queues;
3128 static const u32 rsskey[10] = { 0xDA565A6D, 0xC20E5B25, 0x3D256741,
3129 0xB08FA343, 0xCB2BCAD0, 0xB4307BAE,
3130 0xA32DCB77, 0x0CF23080, 0x3BB7426A,
3133 /* Fill out hash function seeds */
3134 for (j = 0; j < 10; j++)
3135 wr32(E1000_RSSRK(j), rsskey[j]);
3137 num_rx_queues = adapter->rss_queues;
3139 switch (hw->mac.type) {
3141 /* 82576 supports 2 RSS queues for SR-IOV */
3142 if (adapter->vfs_allocated_count)
3149 if (adapter->rss_indir_tbl_init != num_rx_queues) {
3150 for (j = 0; j < IGB_RETA_SIZE; j++)
3151 adapter->rss_indir_tbl[j] = (j * num_rx_queues) / IGB_RETA_SIZE;
3152 adapter->rss_indir_tbl_init = num_rx_queues;
3154 igb_write_rss_indir_tbl(adapter);
3156 /* Disable raw packet checksumming so that RSS hash is placed in
3157 * descriptor on writeback. No need to enable TCP/UDP/IP checksum
3158 * offloads as they are enabled by default
3160 rxcsum = rd32(E1000_RXCSUM);
3161 rxcsum |= E1000_RXCSUM_PCSD;
3163 if (adapter->hw.mac.type >= e1000_82576)
3164 /* Enable Receive Checksum Offload for SCTP */
3165 rxcsum |= E1000_RXCSUM_CRCOFL;
3167 /* Don't need to set TUOFL or IPOFL, they default to 1 */
3168 wr32(E1000_RXCSUM, rxcsum);
3170 /* Generate RSS hash based on packet types, TCP/UDP
3171 * port numbers and/or IPv4/v6 src and dst addresses
3173 mrqc = E1000_MRQC_RSS_FIELD_IPV4 |
3174 E1000_MRQC_RSS_FIELD_IPV4_TCP |
3175 E1000_MRQC_RSS_FIELD_IPV6 |
3176 E1000_MRQC_RSS_FIELD_IPV6_TCP |
3177 E1000_MRQC_RSS_FIELD_IPV6_TCP_EX;
3179 if (adapter->flags & IGB_FLAG_RSS_FIELD_IPV4_UDP)
3180 mrqc |= E1000_MRQC_RSS_FIELD_IPV4_UDP;
3181 if (adapter->flags & IGB_FLAG_RSS_FIELD_IPV6_UDP)
3182 mrqc |= E1000_MRQC_RSS_FIELD_IPV6_UDP;
3184 /* If VMDq is enabled then we set the appropriate mode for that, else
3185 * we default to RSS so that an RSS hash is calculated per packet even
3186 * if we are only using one queue
3188 if (adapter->vfs_allocated_count) {
3189 if (hw->mac.type > e1000_82575) {
3190 /* Set the default pool for the PF's first queue */
3191 u32 vtctl = rd32(E1000_VT_CTL);
3192 vtctl &= ~(E1000_VT_CTL_DEFAULT_POOL_MASK |
3193 E1000_VT_CTL_DISABLE_DEF_POOL);
3194 vtctl |= adapter->vfs_allocated_count <<
3195 E1000_VT_CTL_DEFAULT_POOL_SHIFT;
3196 wr32(E1000_VT_CTL, vtctl);
3198 if (adapter->rss_queues > 1)
3199 mrqc |= E1000_MRQC_ENABLE_VMDQ_RSS_2Q;
3201 mrqc |= E1000_MRQC_ENABLE_VMDQ;
3203 if (hw->mac.type != e1000_i211)
3204 mrqc |= E1000_MRQC_ENABLE_RSS_4Q;
3206 igb_vmm_control(adapter);
3208 wr32(E1000_MRQC, mrqc);
3212 * igb_setup_rctl - configure the receive control registers
3213 * @adapter: Board private structure
3215 void igb_setup_rctl(struct igb_adapter *adapter)
3217 struct e1000_hw *hw = &adapter->hw;
3220 rctl = rd32(E1000_RCTL);
3222 rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
3223 rctl &= ~(E1000_RCTL_LBM_TCVR | E1000_RCTL_LBM_MAC);
3225 rctl |= E1000_RCTL_EN | E1000_RCTL_BAM | E1000_RCTL_RDMTS_HALF |
3226 (hw->mac.mc_filter_type << E1000_RCTL_MO_SHIFT);
3228 /* enable stripping of CRC. It's unlikely this will break BMC
3229 * redirection as it did with e1000. Newer features require
3230 * that the HW strips the CRC.
3232 rctl |= E1000_RCTL_SECRC;
3234 /* disable store bad packets and clear size bits. */
3235 rctl &= ~(E1000_RCTL_SBP | E1000_RCTL_SZ_256);
3237 /* enable LPE to prevent packets larger than max_frame_size */
3238 rctl |= E1000_RCTL_LPE;
3240 /* disable queue 0 to prevent tail write w/o re-config */
3241 wr32(E1000_RXDCTL(0), 0);
3243 /* Attention!!! For SR-IOV PF driver operations you must enable
3244 * queue drop for all VF and PF queues to prevent head of line blocking
3245 * if an un-trusted VF does not provide descriptors to hardware.
3247 if (adapter->vfs_allocated_count) {
3248 /* set all queue drop enable bits */
3249 wr32(E1000_QDE, ALL_QUEUES);
3252 /* This is useful for sniffing bad packets. */
3253 if (adapter->netdev->features & NETIF_F_RXALL) {
3254 /* UPE and MPE will be handled by normal PROMISC logic
3255 * in e1000e_set_rx_mode
3257 rctl |= (E1000_RCTL_SBP | /* Receive bad packets */
3258 E1000_RCTL_BAM | /* RX All Bcast Pkts */
3259 E1000_RCTL_PMCF); /* RX All MAC Ctrl Pkts */
3261 rctl &= ~(E1000_RCTL_VFE | /* Disable VLAN filter */
3262 E1000_RCTL_DPF | /* Allow filtered pause */
3263 E1000_RCTL_CFIEN); /* Dis VLAN CFIEN Filter */
3264 /* Do not mess with E1000_CTRL_VME, it affects transmit as well,
3265 * and that breaks VLANs.
3269 wr32(E1000_RCTL, rctl);
3272 static inline int igb_set_vf_rlpml(struct igb_adapter *adapter, int size,
3275 struct e1000_hw *hw = &adapter->hw;
3278 /* if it isn't the PF check to see if VFs are enabled and
3279 * increase the size to support vlan tags
3281 if (vfn < adapter->vfs_allocated_count &&
3282 adapter->vf_data[vfn].vlans_enabled)
3283 size += VLAN_TAG_SIZE;
3285 vmolr = rd32(E1000_VMOLR(vfn));
3286 vmolr &= ~E1000_VMOLR_RLPML_MASK;
3287 vmolr |= size | E1000_VMOLR_LPE;
3288 wr32(E1000_VMOLR(vfn), vmolr);
3294 * igb_rlpml_set - set maximum receive packet size
3295 * @adapter: board private structure
3297 * Configure maximum receivable packet size.
3299 static void igb_rlpml_set(struct igb_adapter *adapter)
3301 u32 max_frame_size = adapter->max_frame_size;
3302 struct e1000_hw *hw = &adapter->hw;
3303 u16 pf_id = adapter->vfs_allocated_count;
3306 igb_set_vf_rlpml(adapter, max_frame_size, pf_id);
3307 /* If we're in VMDQ or SR-IOV mode, then set global RLPML
3308 * to our max jumbo frame size, in case we need to enable
3309 * jumbo frames on one of the rings later.
3310 * This will not pass over-length frames into the default
3311 * queue because it's gated by the VMOLR.RLPML.
3313 max_frame_size = MAX_JUMBO_FRAME_SIZE;
3316 wr32(E1000_RLPML, max_frame_size);
3319 static inline void igb_set_vmolr(struct igb_adapter *adapter,
3322 struct e1000_hw *hw = &adapter->hw;
3325 /* This register exists only on 82576 and newer so if we are older then
3326 * we should exit and do nothing
3328 if (hw->mac.type < e1000_82576)
3331 vmolr = rd32(E1000_VMOLR(vfn));
3332 vmolr |= E1000_VMOLR_STRVLAN; /* Strip vlan tags */
3334 vmolr |= E1000_VMOLR_AUPE; /* Accept untagged packets */
3336 vmolr &= ~(E1000_VMOLR_AUPE); /* Tagged packets ONLY */
3338 /* clear all bits that might not be set */
3339 vmolr &= ~(E1000_VMOLR_BAM | E1000_VMOLR_RSSE);
3341 if (adapter->rss_queues > 1 && vfn == adapter->vfs_allocated_count)
3342 vmolr |= E1000_VMOLR_RSSE; /* enable RSS */
3343 /* for VMDq only allow the VFs and pool 0 to accept broadcast and
3346 if (vfn <= adapter->vfs_allocated_count)
3347 vmolr |= E1000_VMOLR_BAM; /* Accept broadcast */
3349 wr32(E1000_VMOLR(vfn), vmolr);
3353 * igb_configure_rx_ring - Configure a receive ring after Reset
3354 * @adapter: board private structure
3355 * @ring: receive ring to be configured
3357 * Configure the Rx unit of the MAC after a reset.
3359 void igb_configure_rx_ring(struct igb_adapter *adapter,
3360 struct igb_ring *ring)
3362 struct e1000_hw *hw = &adapter->hw;
3363 u64 rdba = ring->dma;
3364 int reg_idx = ring->reg_idx;
3365 u32 srrctl = 0, rxdctl = 0;
3367 /* disable the queue */
3368 wr32(E1000_RXDCTL(reg_idx), 0);
3370 /* Set DMA base address registers */
3371 wr32(E1000_RDBAL(reg_idx),
3372 rdba & 0x00000000ffffffffULL);
3373 wr32(E1000_RDBAH(reg_idx), rdba >> 32);
3374 wr32(E1000_RDLEN(reg_idx),
3375 ring->count * sizeof(union e1000_adv_rx_desc));
3377 /* initialize head and tail */
3378 ring->tail = hw->hw_addr + E1000_RDT(reg_idx);
3379 wr32(E1000_RDH(reg_idx), 0);
3380 writel(0, ring->tail);
3382 /* set descriptor configuration */
3383 srrctl = IGB_RX_HDR_LEN << E1000_SRRCTL_BSIZEHDRSIZE_SHIFT;
3384 srrctl |= IGB_RX_BUFSZ >> E1000_SRRCTL_BSIZEPKT_SHIFT;
3385 srrctl |= E1000_SRRCTL_DESCTYPE_ADV_ONEBUF;
3386 if (hw->mac.type >= e1000_82580)
3387 srrctl |= E1000_SRRCTL_TIMESTAMP;
3388 /* Only set Drop Enable if we are supporting multiple queues */
3389 if (adapter->vfs_allocated_count || adapter->num_rx_queues > 1)
3390 srrctl |= E1000_SRRCTL_DROP_EN;
3392 wr32(E1000_SRRCTL(reg_idx), srrctl);
3394 /* set filtering for VMDQ pools */
3395 igb_set_vmolr(adapter, reg_idx & 0x7, true);
3397 rxdctl |= IGB_RX_PTHRESH;
3398 rxdctl |= IGB_RX_HTHRESH << 8;
3399 rxdctl |= IGB_RX_WTHRESH << 16;
3401 /* enable receive descriptor fetching */
3402 rxdctl |= E1000_RXDCTL_QUEUE_ENABLE;
3403 wr32(E1000_RXDCTL(reg_idx), rxdctl);
3407 * igb_configure_rx - Configure receive Unit after Reset
3408 * @adapter: board private structure
3410 * Configure the Rx unit of the MAC after a reset.
3412 static void igb_configure_rx(struct igb_adapter *adapter)
3416 /* set UTA to appropriate mode */
3417 igb_set_uta(adapter);
3419 /* set the correct pool for the PF default MAC address in entry 0 */
3420 igb_rar_set_qsel(adapter, adapter->hw.mac.addr, 0,
3421 adapter->vfs_allocated_count);
3423 /* Setup the HW Rx Head and Tail Descriptor Pointers and
3424 * the Base and Length of the Rx Descriptor Ring
3426 for (i = 0; i < adapter->num_rx_queues; i++)
3427 igb_configure_rx_ring(adapter, adapter->rx_ring[i]);
3431 * igb_free_tx_resources - Free Tx Resources per Queue
3432 * @tx_ring: Tx descriptor ring for a specific queue
3434 * Free all transmit software resources
3436 void igb_free_tx_resources(struct igb_ring *tx_ring)
3438 igb_clean_tx_ring(tx_ring);
3440 vfree(tx_ring->tx_buffer_info);
3441 tx_ring->tx_buffer_info = NULL;
3443 /* if not set, then don't free */
3447 dma_free_coherent(tx_ring->dev, tx_ring->size,
3448 tx_ring->desc, tx_ring->dma);
3450 tx_ring->desc = NULL;
3454 * igb_free_all_tx_resources - Free Tx Resources for All Queues
3455 * @adapter: board private structure
3457 * Free all transmit software resources
3459 static void igb_free_all_tx_resources(struct igb_adapter *adapter)
3463 for (i = 0; i < adapter->num_tx_queues; i++)
3464 igb_free_tx_resources(adapter->tx_ring[i]);
3467 void igb_unmap_and_free_tx_resource(struct igb_ring *ring,
3468 struct igb_tx_buffer *tx_buffer)
3470 if (tx_buffer->skb) {
3471 dev_kfree_skb_any(tx_buffer->skb);
3472 if (dma_unmap_len(tx_buffer, len))
3473 dma_unmap_single(ring->dev,
3474 dma_unmap_addr(tx_buffer, dma),
3475 dma_unmap_len(tx_buffer, len),
3477 } else if (dma_unmap_len(tx_buffer, len)) {
3478 dma_unmap_page(ring->dev,
3479 dma_unmap_addr(tx_buffer, dma),
3480 dma_unmap_len(tx_buffer, len),
3483 tx_buffer->next_to_watch = NULL;
3484 tx_buffer->skb = NULL;
3485 dma_unmap_len_set(tx_buffer, len, 0);
3486 /* buffer_info must be completely set up in the transmit path */
3490 * igb_clean_tx_ring - Free Tx Buffers
3491 * @tx_ring: ring to be cleaned
3493 static void igb_clean_tx_ring(struct igb_ring *tx_ring)
3495 struct igb_tx_buffer *buffer_info;
3499 if (!tx_ring->tx_buffer_info)
3501 /* Free all the Tx ring sk_buffs */
3503 for (i = 0; i < tx_ring->count; i++) {
3504 buffer_info = &tx_ring->tx_buffer_info[i];
3505 igb_unmap_and_free_tx_resource(tx_ring, buffer_info);
3508 netdev_tx_reset_queue(txring_txq(tx_ring));
3510 size = sizeof(struct igb_tx_buffer) * tx_ring->count;
3511 memset(tx_ring->tx_buffer_info, 0, size);
3513 /* Zero out the descriptor ring */
3514 memset(tx_ring->desc, 0, tx_ring->size);
3516 tx_ring->next_to_use = 0;
3517 tx_ring->next_to_clean = 0;
3521 * igb_clean_all_tx_rings - Free Tx Buffers for all queues
3522 * @adapter: board private structure
3524 static void igb_clean_all_tx_rings(struct igb_adapter *adapter)
3528 for (i = 0; i < adapter->num_tx_queues; i++)
3529 igb_clean_tx_ring(adapter->tx_ring[i]);
3533 * igb_free_rx_resources - Free Rx Resources
3534 * @rx_ring: ring to clean the resources from
3536 * Free all receive software resources
3538 void igb_free_rx_resources(struct igb_ring *rx_ring)
3540 igb_clean_rx_ring(rx_ring);
3542 vfree(rx_ring->rx_buffer_info);
3543 rx_ring->rx_buffer_info = NULL;
3545 /* if not set, then don't free */
3549 dma_free_coherent(rx_ring->dev, rx_ring->size,
3550 rx_ring->desc, rx_ring->dma);
3552 rx_ring->desc = NULL;
3556 * igb_free_all_rx_resources - Free Rx Resources for All Queues
3557 * @adapter: board private structure
3559 * Free all receive software resources
3561 static void igb_free_all_rx_resources(struct igb_adapter *adapter)
3565 for (i = 0; i < adapter->num_rx_queues; i++)
3566 igb_free_rx_resources(adapter->rx_ring[i]);
3570 * igb_clean_rx_ring - Free Rx Buffers per Queue
3571 * @rx_ring: ring to free buffers from
3573 static void igb_clean_rx_ring(struct igb_ring *rx_ring)
3579 dev_kfree_skb(rx_ring->skb);
3580 rx_ring->skb = NULL;
3582 if (!rx_ring->rx_buffer_info)
3585 /* Free all the Rx ring sk_buffs */
3586 for (i = 0; i < rx_ring->count; i++) {
3587 struct igb_rx_buffer *buffer_info = &rx_ring->rx_buffer_info[i];
3589 if (!buffer_info->page)
3592 dma_unmap_page(rx_ring->dev,
3596 __free_page(buffer_info->page);
3598 buffer_info->page = NULL;
3601 size = sizeof(struct igb_rx_buffer) * rx_ring->count;
3602 memset(rx_ring->rx_buffer_info, 0, size);
3604 /* Zero out the descriptor ring */
3605 memset(rx_ring->desc, 0, rx_ring->size);
3607 rx_ring->next_to_alloc = 0;
3608 rx_ring->next_to_clean = 0;
3609 rx_ring->next_to_use = 0;
3613 * igb_clean_all_rx_rings - Free Rx Buffers for all queues
3614 * @adapter: board private structure
3616 static void igb_clean_all_rx_rings(struct igb_adapter *adapter)
3620 for (i = 0; i < adapter->num_rx_queues; i++)
3621 igb_clean_rx_ring(adapter->rx_ring[i]);
3625 * igb_set_mac - Change the Ethernet Address of the NIC
3626 * @netdev: network interface device structure
3627 * @p: pointer to an address structure
3629 * Returns 0 on success, negative on failure
3631 static int igb_set_mac(struct net_device *netdev, void *p)
3633 struct igb_adapter *adapter = netdev_priv(netdev);
3634 struct e1000_hw *hw = &adapter->hw;
3635 struct sockaddr *addr = p;
3637 if (!is_valid_ether_addr(addr->sa_data))
3638 return -EADDRNOTAVAIL;
3640 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
3641 memcpy(hw->mac.addr, addr->sa_data, netdev->addr_len);
3643 /* set the correct pool for the new PF MAC address in entry 0 */
3644 igb_rar_set_qsel(adapter, hw->mac.addr, 0,
3645 adapter->vfs_allocated_count);
3651 * igb_write_mc_addr_list - write multicast addresses to MTA
3652 * @netdev: network interface device structure
3654 * Writes multicast address list to the MTA hash table.
3655 * Returns: -ENOMEM on failure
3656 * 0 on no addresses written
3657 * X on writing X addresses to MTA
3659 static int igb_write_mc_addr_list(struct net_device *netdev)
3661 struct igb_adapter *adapter = netdev_priv(netdev);
3662 struct e1000_hw *hw = &adapter->hw;
3663 struct netdev_hw_addr *ha;
3667 if (netdev_mc_empty(netdev)) {
3668 /* nothing to program, so clear mc list */
3669 igb_update_mc_addr_list(hw, NULL, 0);
3670 igb_restore_vf_multicasts(adapter);
3674 mta_list = kzalloc(netdev_mc_count(netdev) * 6, GFP_ATOMIC);
3678 /* The shared function expects a packed array of only addresses. */
3680 netdev_for_each_mc_addr(ha, netdev)
3681 memcpy(mta_list + (i++ * ETH_ALEN), ha->addr, ETH_ALEN);
3683 igb_update_mc_addr_list(hw, mta_list, i);
3686 return netdev_mc_count(netdev);
3690 * igb_write_uc_addr_list - write unicast addresses to RAR table
3691 * @netdev: network interface device structure
3693 * Writes unicast address list to the RAR table.
3694 * Returns: -ENOMEM on failure/insufficient address space
3695 * 0 on no addresses written
3696 * X on writing X addresses to the RAR table
3698 static int igb_write_uc_addr_list(struct net_device *netdev)
3700 struct igb_adapter *adapter = netdev_priv(netdev);
3701 struct e1000_hw *hw = &adapter->hw;
3702 unsigned int vfn = adapter->vfs_allocated_count;
3703 unsigned int rar_entries = hw->mac.rar_entry_count - (vfn + 1);
3706 /* return ENOMEM indicating insufficient memory for addresses */
3707 if (netdev_uc_count(netdev) > rar_entries)
3710 if (!netdev_uc_empty(netdev) && rar_entries) {
3711 struct netdev_hw_addr *ha;
3713 netdev_for_each_uc_addr(ha, netdev) {
3716 igb_rar_set_qsel(adapter, ha->addr,
3722 /* write the addresses in reverse order to avoid write combining */
3723 for (; rar_entries > 0 ; rar_entries--) {
3724 wr32(E1000_RAH(rar_entries), 0);
3725 wr32(E1000_RAL(rar_entries), 0);
3733 * igb_set_rx_mode - Secondary Unicast, Multicast and Promiscuous mode set
3734 * @netdev: network interface device structure
3736 * The set_rx_mode entry point is called whenever the unicast or multicast
3737 * address lists or the network interface flags are updated. This routine is
3738 * responsible for configuring the hardware for proper unicast, multicast,
3739 * promiscuous mode, and all-multi behavior.
3741 static void igb_set_rx_mode(struct net_device *netdev)
3743 struct igb_adapter *adapter = netdev_priv(netdev);
3744 struct e1000_hw *hw = &adapter->hw;
3745 unsigned int vfn = adapter->vfs_allocated_count;
3746 u32 rctl, vmolr = 0;
3749 /* Check for Promiscuous and All Multicast modes */
3750 rctl = rd32(E1000_RCTL);
3752 /* clear the effected bits */
3753 rctl &= ~(E1000_RCTL_UPE | E1000_RCTL_MPE | E1000_RCTL_VFE);
3755 if (netdev->flags & IFF_PROMISC) {
3756 /* retain VLAN HW filtering if in VT mode */
3757 if (adapter->vfs_allocated_count)
3758 rctl |= E1000_RCTL_VFE;
3759 rctl |= (E1000_RCTL_UPE | E1000_RCTL_MPE);
3760 vmolr |= (E1000_VMOLR_ROPE | E1000_VMOLR_MPME);
3762 if (netdev->flags & IFF_ALLMULTI) {
3763 rctl |= E1000_RCTL_MPE;
3764 vmolr |= E1000_VMOLR_MPME;
3766 /* Write addresses to the MTA, if the attempt fails
3767 * then we should just turn on promiscuous mode so
3768 * that we can at least receive multicast traffic
3770 count = igb_write_mc_addr_list(netdev);
3772 rctl |= E1000_RCTL_MPE;
3773 vmolr |= E1000_VMOLR_MPME;
3775 vmolr |= E1000_VMOLR_ROMPE;
3778 /* Write addresses to available RAR registers, if there is not
3779 * sufficient space to store all the addresses then enable
3780 * unicast promiscuous mode
3782 count = igb_write_uc_addr_list(netdev);
3784 rctl |= E1000_RCTL_UPE;
3785 vmolr |= E1000_VMOLR_ROPE;
3787 rctl |= E1000_RCTL_VFE;
3789 wr32(E1000_RCTL, rctl);
3791 /* In order to support SR-IOV and eventually VMDq it is necessary to set
3792 * the VMOLR to enable the appropriate modes. Without this workaround
3793 * we will have issues with VLAN tag stripping not being done for frames
3794 * that are only arriving because we are the default pool
3796 if ((hw->mac.type < e1000_82576) || (hw->mac.type > e1000_i350))
3799 vmolr |= rd32(E1000_VMOLR(vfn)) &
3800 ~(E1000_VMOLR_ROPE | E1000_VMOLR_MPME | E1000_VMOLR_ROMPE);
3801 wr32(E1000_VMOLR(vfn), vmolr);
3802 igb_restore_vf_multicasts(adapter);
3805 static void igb_check_wvbr(struct igb_adapter *adapter)
3807 struct e1000_hw *hw = &adapter->hw;
3810 switch (hw->mac.type) {
3813 if (!(wvbr = rd32(E1000_WVBR)))
3820 adapter->wvbr |= wvbr;
3823 #define IGB_STAGGERED_QUEUE_OFFSET 8
3825 static void igb_spoof_check(struct igb_adapter *adapter)
3832 for(j = 0; j < adapter->vfs_allocated_count; j++) {
3833 if (adapter->wvbr & (1 << j) ||
3834 adapter->wvbr & (1 << (j + IGB_STAGGERED_QUEUE_OFFSET))) {
3835 dev_warn(&adapter->pdev->dev,
3836 "Spoof event(s) detected on VF %d\n", j);
3839 (1 << (j + IGB_STAGGERED_QUEUE_OFFSET)));
3844 /* Need to wait a few seconds after link up to get diagnostic information from
3847 static void igb_update_phy_info(unsigned long data)
3849 struct igb_adapter *adapter = (struct igb_adapter *) data;
3850 igb_get_phy_info(&adapter->hw);
3854 * igb_has_link - check shared code for link and determine up/down
3855 * @adapter: pointer to driver private info
3857 bool igb_has_link(struct igb_adapter *adapter)
3859 struct e1000_hw *hw = &adapter->hw;
3860 bool link_active = false;
3862 /* get_link_status is set on LSC (link status) interrupt or
3863 * rx sequence error interrupt. get_link_status will stay
3864 * false until the e1000_check_for_link establishes link
3865 * for copper adapters ONLY
3867 switch (hw->phy.media_type) {
3868 case e1000_media_type_copper:
3869 if (!hw->mac.get_link_status)
3871 case e1000_media_type_internal_serdes:
3872 hw->mac.ops.check_for_link(hw);
3873 link_active = !hw->mac.get_link_status;
3876 case e1000_media_type_unknown:
3880 if (((hw->mac.type == e1000_i210) ||
3881 (hw->mac.type == e1000_i211)) &&
3882 (hw->phy.id == I210_I_PHY_ID)) {
3883 if (!netif_carrier_ok(adapter->netdev)) {
3884 adapter->flags &= ~IGB_FLAG_NEED_LINK_UPDATE;
3885 } else if (!(adapter->flags & IGB_FLAG_NEED_LINK_UPDATE)) {
3886 adapter->flags |= IGB_FLAG_NEED_LINK_UPDATE;
3887 adapter->link_check_timeout = jiffies;
3894 static bool igb_thermal_sensor_event(struct e1000_hw *hw, u32 event)
3897 u32 ctrl_ext, thstat;
3899 /* check for thermal sensor event on i350 copper only */
3900 if (hw->mac.type == e1000_i350) {
3901 thstat = rd32(E1000_THSTAT);
3902 ctrl_ext = rd32(E1000_CTRL_EXT);
3904 if ((hw->phy.media_type == e1000_media_type_copper) &&
3905 !(ctrl_ext & E1000_CTRL_EXT_LINK_MODE_SGMII))
3906 ret = !!(thstat & event);
3913 * igb_watchdog - Timer Call-back
3914 * @data: pointer to adapter cast into an unsigned long
3916 static void igb_watchdog(unsigned long data)
3918 struct igb_adapter *adapter = (struct igb_adapter *)data;
3919 /* Do the rest outside of interrupt context */
3920 schedule_work(&adapter->watchdog_task);
3923 static void igb_watchdog_task(struct work_struct *work)
3925 struct igb_adapter *adapter = container_of(work,
3928 struct e1000_hw *hw = &adapter->hw;
3929 struct e1000_phy_info *phy = &hw->phy;
3930 struct net_device *netdev = adapter->netdev;
3934 link = igb_has_link(adapter);
3936 if (adapter->flags & IGB_FLAG_NEED_LINK_UPDATE) {
3937 if (time_after(jiffies, (adapter->link_check_timeout + HZ)))
3938 adapter->flags &= ~IGB_FLAG_NEED_LINK_UPDATE;
3944 /* Cancel scheduled suspend requests. */
3945 pm_runtime_resume(netdev->dev.parent);
3947 if (!netif_carrier_ok(netdev)) {
3949 hw->mac.ops.get_speed_and_duplex(hw,
3950 &adapter->link_speed,
3951 &adapter->link_duplex);
3953 ctrl = rd32(E1000_CTRL);
3954 /* Links status message must follow this format */
3955 printk(KERN_INFO "igb: %s NIC Link is Up %d Mbps %s "
3956 "Duplex, Flow Control: %s\n",
3958 adapter->link_speed,
3959 adapter->link_duplex == FULL_DUPLEX ?
3961 (ctrl & E1000_CTRL_TFCE) &&
3962 (ctrl & E1000_CTRL_RFCE) ? "RX/TX" :
3963 (ctrl & E1000_CTRL_RFCE) ? "RX" :
3964 (ctrl & E1000_CTRL_TFCE) ? "TX" : "None");
3966 /* check if SmartSpeed worked */
3967 igb_check_downshift(hw);
3968 if (phy->speed_downgraded)
3969 netdev_warn(netdev, "Link Speed was downgraded by SmartSpeed\n");
3971 /* check for thermal sensor event */
3972 if (igb_thermal_sensor_event(hw,
3973 E1000_THSTAT_LINK_THROTTLE)) {
3974 netdev_info(netdev, "The network adapter link "
3975 "speed was downshifted because it "
3979 /* adjust timeout factor according to speed/duplex */
3980 adapter->tx_timeout_factor = 1;
3981 switch (adapter->link_speed) {
3983 adapter->tx_timeout_factor = 14;
3986 /* maybe add some timeout factor ? */
3990 netif_carrier_on(netdev);
3992 igb_ping_all_vfs(adapter);
3993 igb_check_vf_rate_limit(adapter);
3995 /* link state has changed, schedule phy info update */
3996 if (!test_bit(__IGB_DOWN, &adapter->state))
3997 mod_timer(&adapter->phy_info_timer,
3998 round_jiffies(jiffies + 2 * HZ));
4001 if (netif_carrier_ok(netdev)) {
4002 adapter->link_speed = 0;
4003 adapter->link_duplex = 0;
4005 /* check for thermal sensor event */
4006 if (igb_thermal_sensor_event(hw,
4007 E1000_THSTAT_PWR_DOWN)) {
4008 netdev_err(netdev, "The network adapter was "
4009 "stopped because it overheated\n");
4012 /* Links status message must follow this format */
4013 printk(KERN_INFO "igb: %s NIC Link is Down\n",
4015 netif_carrier_off(netdev);
4017 igb_ping_all_vfs(adapter);
4019 /* link state has changed, schedule phy info update */
4020 if (!test_bit(__IGB_DOWN, &adapter->state))
4021 mod_timer(&adapter->phy_info_timer,
4022 round_jiffies(jiffies + 2 * HZ));
4024 pm_schedule_suspend(netdev->dev.parent,
4029 spin_lock(&adapter->stats64_lock);
4030 igb_update_stats(adapter, &adapter->stats64);
4031 spin_unlock(&adapter->stats64_lock);
4033 for (i = 0; i < adapter->num_tx_queues; i++) {
4034 struct igb_ring *tx_ring = adapter->tx_ring[i];
4035 if (!netif_carrier_ok(netdev)) {
4036 /* We've lost link, so the controller stops DMA,
4037 * but we've got queued Tx work that's never going
4038 * to get done, so reset controller to flush Tx.
4039 * (Do the reset outside of interrupt context).
4041 if (igb_desc_unused(tx_ring) + 1 < tx_ring->count) {
4042 adapter->tx_timeout_count++;
4043 schedule_work(&adapter->reset_task);
4044 /* return immediately since reset is imminent */
4049 /* Force detection of hung controller every watchdog period */
4050 set_bit(IGB_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags);
4053 /* Cause software interrupt to ensure Rx ring is cleaned */
4054 if (adapter->msix_entries) {
4056 for (i = 0; i < adapter->num_q_vectors; i++)
4057 eics |= adapter->q_vector[i]->eims_value;
4058 wr32(E1000_EICS, eics);
4060 wr32(E1000_ICS, E1000_ICS_RXDMT0);
4063 igb_spoof_check(adapter);
4064 igb_ptp_rx_hang(adapter);
4066 /* Reset the timer */
4067 if (!test_bit(__IGB_DOWN, &adapter->state)) {
4068 if (adapter->flags & IGB_FLAG_NEED_LINK_UPDATE)
4069 mod_timer(&adapter->watchdog_timer,
4070 round_jiffies(jiffies + HZ));
4072 mod_timer(&adapter->watchdog_timer,
4073 round_jiffies(jiffies + 2 * HZ));
4077 enum latency_range {
4081 latency_invalid = 255
4085 * igb_update_ring_itr - update the dynamic ITR value based on packet size
4086 * @q_vector: pointer to q_vector
4088 * Stores a new ITR value based on strictly on packet size. This
4089 * algorithm is less sophisticated than that used in igb_update_itr,
4090 * due to the difficulty of synchronizing statistics across multiple
4091 * receive rings. The divisors and thresholds used by this function
4092 * were determined based on theoretical maximum wire speed and testing
4093 * data, in order to minimize response time while increasing bulk
4095 * This functionality is controlled by the InterruptThrottleRate module
4096 * parameter (see igb_param.c)
4097 * NOTE: This function is called only when operating in a multiqueue
4098 * receive environment.
4100 static void igb_update_ring_itr(struct igb_q_vector *q_vector)
4102 int new_val = q_vector->itr_val;
4103 int avg_wire_size = 0;
4104 struct igb_adapter *adapter = q_vector->adapter;
4105 unsigned int packets;
4107 /* For non-gigabit speeds, just fix the interrupt rate at 4000
4108 * ints/sec - ITR timer value of 120 ticks.
4110 if (adapter->link_speed != SPEED_1000) {
4111 new_val = IGB_4K_ITR;
4115 packets = q_vector->rx.total_packets;
4117 avg_wire_size = q_vector->rx.total_bytes / packets;
4119 packets = q_vector->tx.total_packets;
4121 avg_wire_size = max_t(u32, avg_wire_size,
4122 q_vector->tx.total_bytes / packets);
4124 /* if avg_wire_size isn't set no work was done */
4128 /* Add 24 bytes to size to account for CRC, preamble, and gap */
4129 avg_wire_size += 24;
4131 /* Don't starve jumbo frames */
4132 avg_wire_size = min(avg_wire_size, 3000);
4134 /* Give a little boost to mid-size frames */
4135 if ((avg_wire_size > 300) && (avg_wire_size < 1200))
4136 new_val = avg_wire_size / 3;
4138 new_val = avg_wire_size / 2;
4140 /* conservative mode (itr 3) eliminates the lowest_latency setting */
4141 if (new_val < IGB_20K_ITR &&
4142 ((q_vector->rx.ring && adapter->rx_itr_setting == 3) ||
4143 (!q_vector->rx.ring && adapter->tx_itr_setting == 3)))
4144 new_val = IGB_20K_ITR;
4147 if (new_val != q_vector->itr_val) {
4148 q_vector->itr_val = new_val;
4149 q_vector->set_itr = 1;
4152 q_vector->rx.total_bytes = 0;
4153 q_vector->rx.total_packets = 0;
4154 q_vector->tx.total_bytes = 0;
4155 q_vector->tx.total_packets = 0;
4159 * igb_update_itr - update the dynamic ITR value based on statistics
4160 * @q_vector: pointer to q_vector
4161 * @ring_container: ring info to update the itr for
4163 * Stores a new ITR value based on packets and byte
4164 * counts during the last interrupt. The advantage of per interrupt
4165 * computation is faster updates and more accurate ITR for the current
4166 * traffic pattern. Constants in this function were computed
4167 * based on theoretical maximum wire speed and thresholds were set based
4168 * on testing data as well as attempting to minimize response time
4169 * while increasing bulk throughput.
4170 * this functionality is controlled by the InterruptThrottleRate module
4171 * parameter (see igb_param.c)
4172 * NOTE: These calculations are only valid when operating in a single-
4173 * queue environment.
4175 static void igb_update_itr(struct igb_q_vector *q_vector,
4176 struct igb_ring_container *ring_container)
4178 unsigned int packets = ring_container->total_packets;
4179 unsigned int bytes = ring_container->total_bytes;
4180 u8 itrval = ring_container->itr;
4182 /* no packets, exit with status unchanged */
4187 case lowest_latency:
4188 /* handle TSO and jumbo frames */
4189 if (bytes/packets > 8000)
4190 itrval = bulk_latency;
4191 else if ((packets < 5) && (bytes > 512))
4192 itrval = low_latency;
4194 case low_latency: /* 50 usec aka 20000 ints/s */
4195 if (bytes > 10000) {
4196 /* this if handles the TSO accounting */
4197 if (bytes/packets > 8000) {
4198 itrval = bulk_latency;
4199 } else if ((packets < 10) || ((bytes/packets) > 1200)) {
4200 itrval = bulk_latency;
4201 } else if ((packets > 35)) {
4202 itrval = lowest_latency;
4204 } else if (bytes/packets > 2000) {
4205 itrval = bulk_latency;
4206 } else if (packets <= 2 && bytes < 512) {
4207 itrval = lowest_latency;
4210 case bulk_latency: /* 250 usec aka 4000 ints/s */
4211 if (bytes > 25000) {
4213 itrval = low_latency;
4214 } else if (bytes < 1500) {
4215 itrval = low_latency;
4220 /* clear work counters since we have the values we need */
4221 ring_container->total_bytes = 0;
4222 ring_container->total_packets = 0;
4224 /* write updated itr to ring container */
4225 ring_container->itr = itrval;
4228 static void igb_set_itr(struct igb_q_vector *q_vector)
4230 struct igb_adapter *adapter = q_vector->adapter;
4231 u32 new_itr = q_vector->itr_val;
4234 /* for non-gigabit speeds, just fix the interrupt rate at 4000 */
4235 if (adapter->link_speed != SPEED_1000) {
4237 new_itr = IGB_4K_ITR;
4241 igb_update_itr(q_vector, &q_vector->tx);
4242 igb_update_itr(q_vector, &q_vector->rx);
4244 current_itr = max(q_vector->rx.itr, q_vector->tx.itr);
4246 /* conservative mode (itr 3) eliminates the lowest_latency setting */
4247 if (current_itr == lowest_latency &&
4248 ((q_vector->rx.ring && adapter->rx_itr_setting == 3) ||
4249 (!q_vector->rx.ring && adapter->tx_itr_setting == 3)))
4250 current_itr = low_latency;
4252 switch (current_itr) {
4253 /* counts and packets in update_itr are dependent on these numbers */
4254 case lowest_latency:
4255 new_itr = IGB_70K_ITR; /* 70,000 ints/sec */
4258 new_itr = IGB_20K_ITR; /* 20,000 ints/sec */
4261 new_itr = IGB_4K_ITR; /* 4,000 ints/sec */
4268 if (new_itr != q_vector->itr_val) {
4269 /* this attempts to bias the interrupt rate towards Bulk
4270 * by adding intermediate steps when interrupt rate is
4273 new_itr = new_itr > q_vector->itr_val ?
4274 max((new_itr * q_vector->itr_val) /
4275 (new_itr + (q_vector->itr_val >> 2)),
4277 /* Don't write the value here; it resets the adapter's
4278 * internal timer, and causes us to delay far longer than
4279 * we should between interrupts. Instead, we write the ITR
4280 * value at the beginning of the next interrupt so the timing
4281 * ends up being correct.
4283 q_vector->itr_val = new_itr;
4284 q_vector->set_itr = 1;
4288 static void igb_tx_ctxtdesc(struct igb_ring *tx_ring, u32 vlan_macip_lens,
4289 u32 type_tucmd, u32 mss_l4len_idx)
4291 struct e1000_adv_tx_context_desc *context_desc;
4292 u16 i = tx_ring->next_to_use;
4294 context_desc = IGB_TX_CTXTDESC(tx_ring, i);
4297 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
4299 /* set bits to identify this as an advanced context descriptor */
4300 type_tucmd |= E1000_TXD_CMD_DEXT | E1000_ADVTXD_DTYP_CTXT;
4302 /* For 82575, context index must be unique per ring. */
4303 if (test_bit(IGB_RING_FLAG_TX_CTX_IDX, &tx_ring->flags))
4304 mss_l4len_idx |= tx_ring->reg_idx << 4;
4306 context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens);
4307 context_desc->seqnum_seed = 0;
4308 context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd);
4309 context_desc->mss_l4len_idx = cpu_to_le32(mss_l4len_idx);
4312 static int igb_tso(struct igb_ring *tx_ring,
4313 struct igb_tx_buffer *first,
4316 struct sk_buff *skb = first->skb;
4317 u32 vlan_macip_lens, type_tucmd;
4318 u32 mss_l4len_idx, l4len;
4320 if (skb->ip_summed != CHECKSUM_PARTIAL)
4323 if (!skb_is_gso(skb))
4326 if (skb_header_cloned(skb)) {
4327 int err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
4332 /* ADV DTYP TUCMD MKRLOC/ISCSIHEDLEN */
4333 type_tucmd = E1000_ADVTXD_TUCMD_L4T_TCP;
4335 if (first->protocol == __constant_htons(ETH_P_IP)) {
4336 struct iphdr *iph = ip_hdr(skb);
4339 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
4343 type_tucmd |= E1000_ADVTXD_TUCMD_IPV4;
4344 first->tx_flags |= IGB_TX_FLAGS_TSO |
4347 } else if (skb_is_gso_v6(skb)) {
4348 ipv6_hdr(skb)->payload_len = 0;
4349 tcp_hdr(skb)->check = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
4350 &ipv6_hdr(skb)->daddr,
4352 first->tx_flags |= IGB_TX_FLAGS_TSO |
4356 /* compute header lengths */
4357 l4len = tcp_hdrlen(skb);
4358 *hdr_len = skb_transport_offset(skb) + l4len;
4360 /* update gso size and bytecount with header size */
4361 first->gso_segs = skb_shinfo(skb)->gso_segs;
4362 first->bytecount += (first->gso_segs - 1) * *hdr_len;
4365 mss_l4len_idx = l4len << E1000_ADVTXD_L4LEN_SHIFT;
4366 mss_l4len_idx |= skb_shinfo(skb)->gso_size << E1000_ADVTXD_MSS_SHIFT;
4368 /* VLAN MACLEN IPLEN */
4369 vlan_macip_lens = skb_network_header_len(skb);
4370 vlan_macip_lens |= skb_network_offset(skb) << E1000_ADVTXD_MACLEN_SHIFT;
4371 vlan_macip_lens |= first->tx_flags & IGB_TX_FLAGS_VLAN_MASK;
4373 igb_tx_ctxtdesc(tx_ring, vlan_macip_lens, type_tucmd, mss_l4len_idx);
4378 static void igb_tx_csum(struct igb_ring *tx_ring, struct igb_tx_buffer *first)
4380 struct sk_buff *skb = first->skb;
4381 u32 vlan_macip_lens = 0;
4382 u32 mss_l4len_idx = 0;
4385 if (skb->ip_summed != CHECKSUM_PARTIAL) {
4386 if (!(first->tx_flags & IGB_TX_FLAGS_VLAN))
4390 switch (first->protocol) {
4391 case __constant_htons(ETH_P_IP):
4392 vlan_macip_lens |= skb_network_header_len(skb);
4393 type_tucmd |= E1000_ADVTXD_TUCMD_IPV4;
4394 l4_hdr = ip_hdr(skb)->protocol;
4396 case __constant_htons(ETH_P_IPV6):
4397 vlan_macip_lens |= skb_network_header_len(skb);
4398 l4_hdr = ipv6_hdr(skb)->nexthdr;
4401 if (unlikely(net_ratelimit())) {
4402 dev_warn(tx_ring->dev,
4403 "partial checksum but proto=%x!\n",
4411 type_tucmd |= E1000_ADVTXD_TUCMD_L4T_TCP;
4412 mss_l4len_idx = tcp_hdrlen(skb) <<
4413 E1000_ADVTXD_L4LEN_SHIFT;
4416 type_tucmd |= E1000_ADVTXD_TUCMD_L4T_SCTP;
4417 mss_l4len_idx = sizeof(struct sctphdr) <<
4418 E1000_ADVTXD_L4LEN_SHIFT;
4421 mss_l4len_idx = sizeof(struct udphdr) <<
4422 E1000_ADVTXD_L4LEN_SHIFT;
4425 if (unlikely(net_ratelimit())) {
4426 dev_warn(tx_ring->dev,
4427 "partial checksum but l4 proto=%x!\n",
4433 /* update TX checksum flag */
4434 first->tx_flags |= IGB_TX_FLAGS_CSUM;
4437 vlan_macip_lens |= skb_network_offset(skb) << E1000_ADVTXD_MACLEN_SHIFT;
4438 vlan_macip_lens |= first->tx_flags & IGB_TX_FLAGS_VLAN_MASK;
4440 igb_tx_ctxtdesc(tx_ring, vlan_macip_lens, type_tucmd, mss_l4len_idx);
4443 #define IGB_SET_FLAG(_input, _flag, _result) \
4444 ((_flag <= _result) ? \
4445 ((u32)(_input & _flag) * (_result / _flag)) : \
4446 ((u32)(_input & _flag) / (_flag / _result)))
4448 static u32 igb_tx_cmd_type(struct sk_buff *skb, u32 tx_flags)
4450 /* set type for advanced descriptor with frame checksum insertion */
4451 u32 cmd_type = E1000_ADVTXD_DTYP_DATA |
4452 E1000_ADVTXD_DCMD_DEXT |
4453 E1000_ADVTXD_DCMD_IFCS;
4455 /* set HW vlan bit if vlan is present */
4456 cmd_type |= IGB_SET_FLAG(tx_flags, IGB_TX_FLAGS_VLAN,
4457 (E1000_ADVTXD_DCMD_VLE));
4459 /* set segmentation bits for TSO */
4460 cmd_type |= IGB_SET_FLAG(tx_flags, IGB_TX_FLAGS_TSO,
4461 (E1000_ADVTXD_DCMD_TSE));
4463 /* set timestamp bit if present */
4464 cmd_type |= IGB_SET_FLAG(tx_flags, IGB_TX_FLAGS_TSTAMP,
4465 (E1000_ADVTXD_MAC_TSTAMP));
4467 /* insert frame checksum */
4468 cmd_type ^= IGB_SET_FLAG(skb->no_fcs, 1, E1000_ADVTXD_DCMD_IFCS);
4473 static void igb_tx_olinfo_status(struct igb_ring *tx_ring,
4474 union e1000_adv_tx_desc *tx_desc,
4475 u32 tx_flags, unsigned int paylen)
4477 u32 olinfo_status = paylen << E1000_ADVTXD_PAYLEN_SHIFT;
4479 /* 82575 requires a unique index per ring */
4480 if (test_bit(IGB_RING_FLAG_TX_CTX_IDX, &tx_ring->flags))
4481 olinfo_status |= tx_ring->reg_idx << 4;
4483 /* insert L4 checksum */
4484 olinfo_status |= IGB_SET_FLAG(tx_flags,
4486 (E1000_TXD_POPTS_TXSM << 8));
4488 /* insert IPv4 checksum */
4489 olinfo_status |= IGB_SET_FLAG(tx_flags,
4491 (E1000_TXD_POPTS_IXSM << 8));
4493 tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status);
4496 static void igb_tx_map(struct igb_ring *tx_ring,
4497 struct igb_tx_buffer *first,
4500 struct sk_buff *skb = first->skb;
4501 struct igb_tx_buffer *tx_buffer;
4502 union e1000_adv_tx_desc *tx_desc;
4503 struct skb_frag_struct *frag;
4505 unsigned int data_len, size;
4506 u32 tx_flags = first->tx_flags;
4507 u32 cmd_type = igb_tx_cmd_type(skb, tx_flags);
4508 u16 i = tx_ring->next_to_use;
4510 tx_desc = IGB_TX_DESC(tx_ring, i);
4512 igb_tx_olinfo_status(tx_ring, tx_desc, tx_flags, skb->len - hdr_len);
4514 size = skb_headlen(skb);
4515 data_len = skb->data_len;
4517 dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
4521 for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
4522 if (dma_mapping_error(tx_ring->dev, dma))
4525 /* record length, and DMA address */
4526 dma_unmap_len_set(tx_buffer, len, size);
4527 dma_unmap_addr_set(tx_buffer, dma, dma);
4529 tx_desc->read.buffer_addr = cpu_to_le64(dma);
4531 while (unlikely(size > IGB_MAX_DATA_PER_TXD)) {
4532 tx_desc->read.cmd_type_len =
4533 cpu_to_le32(cmd_type ^ IGB_MAX_DATA_PER_TXD);
4537 if (i == tx_ring->count) {
4538 tx_desc = IGB_TX_DESC(tx_ring, 0);
4541 tx_desc->read.olinfo_status = 0;
4543 dma += IGB_MAX_DATA_PER_TXD;
4544 size -= IGB_MAX_DATA_PER_TXD;
4546 tx_desc->read.buffer_addr = cpu_to_le64(dma);
4549 if (likely(!data_len))
4552 tx_desc->read.cmd_type_len = cpu_to_le32(cmd_type ^ size);
4556 if (i == tx_ring->count) {
4557 tx_desc = IGB_TX_DESC(tx_ring, 0);
4560 tx_desc->read.olinfo_status = 0;
4562 size = skb_frag_size(frag);
4565 dma = skb_frag_dma_map(tx_ring->dev, frag, 0,
4566 size, DMA_TO_DEVICE);
4568 tx_buffer = &tx_ring->tx_buffer_info[i];
4571 /* write last descriptor with RS and EOP bits */
4572 cmd_type |= size | IGB_TXD_DCMD;
4573 tx_desc->read.cmd_type_len = cpu_to_le32(cmd_type);
4575 netdev_tx_sent_queue(txring_txq(tx_ring), first->bytecount);
4577 /* set the timestamp */
4578 first->time_stamp = jiffies;
4580 /* Force memory writes to complete before letting h/w know there
4581 * are new descriptors to fetch. (Only applicable for weak-ordered
4582 * memory model archs, such as IA-64).
4584 * We also need this memory barrier to make certain all of the
4585 * status bits have been updated before next_to_watch is written.
4589 /* set next_to_watch value indicating a packet is present */
4590 first->next_to_watch = tx_desc;
4593 if (i == tx_ring->count)
4596 tx_ring->next_to_use = i;
4598 writel(i, tx_ring->tail);
4600 /* we need this if more than one processor can write to our tail
4601 * at a time, it synchronizes IO on IA64/Altix systems
4608 dev_err(tx_ring->dev, "TX DMA map failed\n");
4610 /* clear dma mappings for failed tx_buffer_info map */
4612 tx_buffer = &tx_ring->tx_buffer_info[i];
4613 igb_unmap_and_free_tx_resource(tx_ring, tx_buffer);
4614 if (tx_buffer == first)
4621 tx_ring->next_to_use = i;
4624 static int __igb_maybe_stop_tx(struct igb_ring *tx_ring, const u16 size)
4626 struct net_device *netdev = tx_ring->netdev;
4628 netif_stop_subqueue(netdev, tx_ring->queue_index);
4630 /* Herbert's original patch had:
4631 * smp_mb__after_netif_stop_queue();
4632 * but since that doesn't exist yet, just open code it.
4636 /* We need to check again in a case another CPU has just
4637 * made room available.
4639 if (igb_desc_unused(tx_ring) < size)
4643 netif_wake_subqueue(netdev, tx_ring->queue_index);
4645 u64_stats_update_begin(&tx_ring->tx_syncp2);
4646 tx_ring->tx_stats.restart_queue2++;
4647 u64_stats_update_end(&tx_ring->tx_syncp2);
4652 static inline int igb_maybe_stop_tx(struct igb_ring *tx_ring, const u16 size)
4654 if (igb_desc_unused(tx_ring) >= size)
4656 return __igb_maybe_stop_tx(tx_ring, size);
4659 netdev_tx_t igb_xmit_frame_ring(struct sk_buff *skb,
4660 struct igb_ring *tx_ring)
4662 struct igb_tx_buffer *first;
4665 u16 count = TXD_USE_COUNT(skb_headlen(skb));
4666 __be16 protocol = vlan_get_protocol(skb);
4669 /* need: 1 descriptor per page * PAGE_SIZE/IGB_MAX_DATA_PER_TXD,
4670 * + 1 desc for skb_headlen/IGB_MAX_DATA_PER_TXD,
4671 * + 2 desc gap to keep tail from touching head,
4672 * + 1 desc for context descriptor,
4673 * otherwise try next time
4675 if (NETDEV_FRAG_PAGE_MAX_SIZE > IGB_MAX_DATA_PER_TXD) {
4677 for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
4678 count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size);
4680 count += skb_shinfo(skb)->nr_frags;
4683 if (igb_maybe_stop_tx(tx_ring, count + 3)) {
4684 /* this is a hard error */
4685 return NETDEV_TX_BUSY;
4688 /* record the location of the first descriptor for this packet */
4689 first = &tx_ring->tx_buffer_info[tx_ring->next_to_use];
4691 first->bytecount = skb->len;
4692 first->gso_segs = 1;
4694 skb_tx_timestamp(skb);
4696 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
4697 struct igb_adapter *adapter = netdev_priv(tx_ring->netdev);
4699 if (!(adapter->ptp_tx_skb)) {
4700 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
4701 tx_flags |= IGB_TX_FLAGS_TSTAMP;
4703 adapter->ptp_tx_skb = skb_get(skb);
4704 adapter->ptp_tx_start = jiffies;
4705 if (adapter->hw.mac.type == e1000_82576)
4706 schedule_work(&adapter->ptp_tx_work);
4710 if (vlan_tx_tag_present(skb)) {
4711 tx_flags |= IGB_TX_FLAGS_VLAN;
4712 tx_flags |= (vlan_tx_tag_get(skb) << IGB_TX_FLAGS_VLAN_SHIFT);
4715 /* record initial flags and protocol */
4716 first->tx_flags = tx_flags;
4717 first->protocol = protocol;
4719 tso = igb_tso(tx_ring, first, &hdr_len);
4723 igb_tx_csum(tx_ring, first);
4725 igb_tx_map(tx_ring, first, hdr_len);
4727 /* Make sure there is space in the ring for the next send. */
4728 igb_maybe_stop_tx(tx_ring, DESC_NEEDED);
4730 return NETDEV_TX_OK;
4733 igb_unmap_and_free_tx_resource(tx_ring, first);
4735 return NETDEV_TX_OK;
4738 static inline struct igb_ring *igb_tx_queue_mapping(struct igb_adapter *adapter,
4739 struct sk_buff *skb)
4741 unsigned int r_idx = skb->queue_mapping;
4743 if (r_idx >= adapter->num_tx_queues)
4744 r_idx = r_idx % adapter->num_tx_queues;
4746 return adapter->tx_ring[r_idx];
4749 static netdev_tx_t igb_xmit_frame(struct sk_buff *skb,
4750 struct net_device *netdev)
4752 struct igb_adapter *adapter = netdev_priv(netdev);
4754 if (test_bit(__IGB_DOWN, &adapter->state)) {
4755 dev_kfree_skb_any(skb);
4756 return NETDEV_TX_OK;
4759 if (skb->len <= 0) {
4760 dev_kfree_skb_any(skb);
4761 return NETDEV_TX_OK;
4764 /* The minimum packet size with TCTL.PSP set is 17 so pad the skb
4765 * in order to meet this minimum size requirement.
4767 if (unlikely(skb->len < 17)) {
4768 if (skb_pad(skb, 17 - skb->len))
4769 return NETDEV_TX_OK;
4771 skb_set_tail_pointer(skb, 17);
4774 return igb_xmit_frame_ring(skb, igb_tx_queue_mapping(adapter, skb));
4778 * igb_tx_timeout - Respond to a Tx Hang
4779 * @netdev: network interface device structure
4781 static void igb_tx_timeout(struct net_device *netdev)
4783 struct igb_adapter *adapter = netdev_priv(netdev);
4784 struct e1000_hw *hw = &adapter->hw;
4786 /* Do the reset outside of interrupt context */
4787 adapter->tx_timeout_count++;
4789 if (hw->mac.type >= e1000_82580)
4790 hw->dev_spec._82575.global_device_reset = true;
4792 schedule_work(&adapter->reset_task);
4794 (adapter->eims_enable_mask & ~adapter->eims_other));
4797 static void igb_reset_task(struct work_struct *work)
4799 struct igb_adapter *adapter;
4800 adapter = container_of(work, struct igb_adapter, reset_task);
4803 netdev_err(adapter->netdev, "Reset adapter\n");
4804 igb_reinit_locked(adapter);
4808 * igb_get_stats64 - Get System Network Statistics
4809 * @netdev: network interface device structure
4810 * @stats: rtnl_link_stats64 pointer
4812 static struct rtnl_link_stats64 *igb_get_stats64(struct net_device *netdev,
4813 struct rtnl_link_stats64 *stats)
4815 struct igb_adapter *adapter = netdev_priv(netdev);
4817 spin_lock(&adapter->stats64_lock);
4818 igb_update_stats(adapter, &adapter->stats64);
4819 memcpy(stats, &adapter->stats64, sizeof(*stats));
4820 spin_unlock(&adapter->stats64_lock);
4826 * igb_change_mtu - Change the Maximum Transfer Unit
4827 * @netdev: network interface device structure
4828 * @new_mtu: new value for maximum frame size
4830 * Returns 0 on success, negative on failure
4832 static int igb_change_mtu(struct net_device *netdev, int new_mtu)
4834 struct igb_adapter *adapter = netdev_priv(netdev);
4835 struct pci_dev *pdev = adapter->pdev;
4836 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
4838 if ((new_mtu < 68) || (max_frame > MAX_JUMBO_FRAME_SIZE)) {
4839 dev_err(&pdev->dev, "Invalid MTU setting\n");
4843 #define MAX_STD_JUMBO_FRAME_SIZE 9238
4844 if (max_frame > MAX_STD_JUMBO_FRAME_SIZE) {
4845 dev_err(&pdev->dev, "MTU > 9216 not supported.\n");
4849 /* adjust max frame to be at least the size of a standard frame */
4850 if (max_frame < (ETH_FRAME_LEN + ETH_FCS_LEN))
4851 max_frame = ETH_FRAME_LEN + ETH_FCS_LEN;
4853 while (test_and_set_bit(__IGB_RESETTING, &adapter->state))
4856 /* igb_down has a dependency on max_frame_size */
4857 adapter->max_frame_size = max_frame;
4859 if (netif_running(netdev))
4862 dev_info(&pdev->dev, "changing MTU from %d to %d\n",
4863 netdev->mtu, new_mtu);
4864 netdev->mtu = new_mtu;
4866 if (netif_running(netdev))
4871 clear_bit(__IGB_RESETTING, &adapter->state);
4877 * igb_update_stats - Update the board statistics counters
4878 * @adapter: board private structure
4880 void igb_update_stats(struct igb_adapter *adapter,
4881 struct rtnl_link_stats64 *net_stats)
4883 struct e1000_hw *hw = &adapter->hw;
4884 struct pci_dev *pdev = adapter->pdev;
4890 u64 _bytes, _packets;
4892 #define PHY_IDLE_ERROR_COUNT_MASK 0x00FF
4894 /* Prevent stats update while adapter is being reset, or if the pci
4895 * connection is down.
4897 if (adapter->link_speed == 0)
4899 if (pci_channel_offline(pdev))
4906 for (i = 0; i < adapter->num_rx_queues; i++) {
4907 u32 rqdpc = rd32(E1000_RQDPC(i));
4908 struct igb_ring *ring = adapter->rx_ring[i];
4911 ring->rx_stats.drops += rqdpc;
4912 net_stats->rx_fifo_errors += rqdpc;
4916 start = u64_stats_fetch_begin_bh(&ring->rx_syncp);
4917 _bytes = ring->rx_stats.bytes;
4918 _packets = ring->rx_stats.packets;
4919 } while (u64_stats_fetch_retry_bh(&ring->rx_syncp, start));
4921 packets += _packets;
4924 net_stats->rx_bytes = bytes;
4925 net_stats->rx_packets = packets;
4929 for (i = 0; i < adapter->num_tx_queues; i++) {
4930 struct igb_ring *ring = adapter->tx_ring[i];
4932 start = u64_stats_fetch_begin_bh(&ring->tx_syncp);
4933 _bytes = ring->tx_stats.bytes;
4934 _packets = ring->tx_stats.packets;
4935 } while (u64_stats_fetch_retry_bh(&ring->tx_syncp, start));
4937 packets += _packets;
4939 net_stats->tx_bytes = bytes;
4940 net_stats->tx_packets = packets;
4943 /* read stats registers */
4944 adapter->stats.crcerrs += rd32(E1000_CRCERRS);
4945 adapter->stats.gprc += rd32(E1000_GPRC);
4946 adapter->stats.gorc += rd32(E1000_GORCL);
4947 rd32(E1000_GORCH); /* clear GORCL */
4948 adapter->stats.bprc += rd32(E1000_BPRC);
4949 adapter->stats.mprc += rd32(E1000_MPRC);
4950 adapter->stats.roc += rd32(E1000_ROC);
4952 adapter->stats.prc64 += rd32(E1000_PRC64);
4953 adapter->stats.prc127 += rd32(E1000_PRC127);
4954 adapter->stats.prc255 += rd32(E1000_PRC255);
4955 adapter->stats.prc511 += rd32(E1000_PRC511);
4956 adapter->stats.prc1023 += rd32(E1000_PRC1023);
4957 adapter->stats.prc1522 += rd32(E1000_PRC1522);
4958 adapter->stats.symerrs += rd32(E1000_SYMERRS);
4959 adapter->stats.sec += rd32(E1000_SEC);
4961 mpc = rd32(E1000_MPC);
4962 adapter->stats.mpc += mpc;
4963 net_stats->rx_fifo_errors += mpc;
4964 adapter->stats.scc += rd32(E1000_SCC);
4965 adapter->stats.ecol += rd32(E1000_ECOL);
4966 adapter->stats.mcc += rd32(E1000_MCC);
4967 adapter->stats.latecol += rd32(E1000_LATECOL);
4968 adapter->stats.dc += rd32(E1000_DC);
4969 adapter->stats.rlec += rd32(E1000_RLEC);
4970 adapter->stats.xonrxc += rd32(E1000_XONRXC);
4971 adapter->stats.xontxc += rd32(E1000_XONTXC);
4972 adapter->stats.xoffrxc += rd32(E1000_XOFFRXC);
4973 adapter->stats.xofftxc += rd32(E1000_XOFFTXC);
4974 adapter->stats.fcruc += rd32(E1000_FCRUC);
4975 adapter->stats.gptc += rd32(E1000_GPTC);
4976 adapter->stats.gotc += rd32(E1000_GOTCL);
4977 rd32(E1000_GOTCH); /* clear GOTCL */
4978 adapter->stats.rnbc += rd32(E1000_RNBC);
4979 adapter->stats.ruc += rd32(E1000_RUC);
4980 adapter->stats.rfc += rd32(E1000_RFC);
4981 adapter->stats.rjc += rd32(E1000_RJC);
4982 adapter->stats.tor += rd32(E1000_TORH);
4983 adapter->stats.tot += rd32(E1000_TOTH);
4984 adapter->stats.tpr += rd32(E1000_TPR);
4986 adapter->stats.ptc64 += rd32(E1000_PTC64);
4987 adapter->stats.ptc127 += rd32(E1000_PTC127);
4988 adapter->stats.ptc255 += rd32(E1000_PTC255);
4989 adapter->stats.ptc511 += rd32(E1000_PTC511);
4990 adapter->stats.ptc1023 += rd32(E1000_PTC1023);
4991 adapter->stats.ptc1522 += rd32(E1000_PTC1522);
4993 adapter->stats.mptc += rd32(E1000_MPTC);
4994 adapter->stats.bptc += rd32(E1000_BPTC);
4996 adapter->stats.tpt += rd32(E1000_TPT);
4997 adapter->stats.colc += rd32(E1000_COLC);
4999 adapter->stats.algnerrc += rd32(E1000_ALGNERRC);
5000 /* read internal phy specific stats */
5001 reg = rd32(E1000_CTRL_EXT);
5002 if (!(reg & E1000_CTRL_EXT_LINK_MODE_MASK)) {
5003 adapter->stats.rxerrc += rd32(E1000_RXERRC);
5005 /* this stat has invalid values on i210/i211 */
5006 if ((hw->mac.type != e1000_i210) &&
5007 (hw->mac.type != e1000_i211))
5008 adapter->stats.tncrs += rd32(E1000_TNCRS);
5011 adapter->stats.tsctc += rd32(E1000_TSCTC);
5012 adapter->stats.tsctfc += rd32(E1000_TSCTFC);
5014 adapter->stats.iac += rd32(E1000_IAC);
5015 adapter->stats.icrxoc += rd32(E1000_ICRXOC);
5016 adapter->stats.icrxptc += rd32(E1000_ICRXPTC);
5017 adapter->stats.icrxatc += rd32(E1000_ICRXATC);
5018 adapter->stats.ictxptc += rd32(E1000_ICTXPTC);
5019 adapter->stats.ictxatc += rd32(E1000_ICTXATC);
5020 adapter->stats.ictxqec += rd32(E1000_ICTXQEC);
5021 adapter->stats.ictxqmtc += rd32(E1000_ICTXQMTC);
5022 adapter->stats.icrxdmtc += rd32(E1000_ICRXDMTC);
5024 /* Fill out the OS statistics structure */
5025 net_stats->multicast = adapter->stats.mprc;
5026 net_stats->collisions = adapter->stats.colc;
5030 /* RLEC on some newer hardware can be incorrect so build
5031 * our own version based on RUC and ROC
5033 net_stats->rx_errors = adapter->stats.rxerrc +
5034 adapter->stats.crcerrs + adapter->stats.algnerrc +
5035 adapter->stats.ruc + adapter->stats.roc +
5036 adapter->stats.cexterr;
5037 net_stats->rx_length_errors = adapter->stats.ruc +
5039 net_stats->rx_crc_errors = adapter->stats.crcerrs;
5040 net_stats->rx_frame_errors = adapter->stats.algnerrc;
5041 net_stats->rx_missed_errors = adapter->stats.mpc;
5044 net_stats->tx_errors = adapter->stats.ecol +
5045 adapter->stats.latecol;
5046 net_stats->tx_aborted_errors = adapter->stats.ecol;
5047 net_stats->tx_window_errors = adapter->stats.latecol;
5048 net_stats->tx_carrier_errors = adapter->stats.tncrs;
5050 /* Tx Dropped needs to be maintained elsewhere */
5053 if (hw->phy.media_type == e1000_media_type_copper) {
5054 if ((adapter->link_speed == SPEED_1000) &&
5055 (!igb_read_phy_reg(hw, PHY_1000T_STATUS, &phy_tmp))) {
5056 phy_tmp &= PHY_IDLE_ERROR_COUNT_MASK;
5057 adapter->phy_stats.idle_errors += phy_tmp;
5061 /* Management Stats */
5062 adapter->stats.mgptc += rd32(E1000_MGTPTC);
5063 adapter->stats.mgprc += rd32(E1000_MGTPRC);
5064 adapter->stats.mgpdc += rd32(E1000_MGTPDC);
5067 reg = rd32(E1000_MANC);
5068 if (reg & E1000_MANC_EN_BMC2OS) {
5069 adapter->stats.o2bgptc += rd32(E1000_O2BGPTC);
5070 adapter->stats.o2bspc += rd32(E1000_O2BSPC);
5071 adapter->stats.b2ospc += rd32(E1000_B2OSPC);
5072 adapter->stats.b2ogprc += rd32(E1000_B2OGPRC);
5076 static irqreturn_t igb_msix_other(int irq, void *data)
5078 struct igb_adapter *adapter = data;
5079 struct e1000_hw *hw = &adapter->hw;
5080 u32 icr = rd32(E1000_ICR);
5081 /* reading ICR causes bit 31 of EICR to be cleared */
5083 if (icr & E1000_ICR_DRSTA)
5084 schedule_work(&adapter->reset_task);
5086 if (icr & E1000_ICR_DOUTSYNC) {
5087 /* HW is reporting DMA is out of sync */
5088 adapter->stats.doosync++;
5089 /* The DMA Out of Sync is also indication of a spoof event
5090 * in IOV mode. Check the Wrong VM Behavior register to
5091 * see if it is really a spoof event.
5093 igb_check_wvbr(adapter);
5096 /* Check for a mailbox event */
5097 if (icr & E1000_ICR_VMMB)
5098 igb_msg_task(adapter);
5100 if (icr & E1000_ICR_LSC) {
5101 hw->mac.get_link_status = 1;
5102 /* guard against interrupt when we're going down */
5103 if (!test_bit(__IGB_DOWN, &adapter->state))
5104 mod_timer(&adapter->watchdog_timer, jiffies + 1);
5107 if (icr & E1000_ICR_TS) {
5108 u32 tsicr = rd32(E1000_TSICR);
5110 if (tsicr & E1000_TSICR_TXTS) {
5111 /* acknowledge the interrupt */
5112 wr32(E1000_TSICR, E1000_TSICR_TXTS);
5113 /* retrieve hardware timestamp */
5114 schedule_work(&adapter->ptp_tx_work);
5118 wr32(E1000_EIMS, adapter->eims_other);
5123 static void igb_write_itr(struct igb_q_vector *q_vector)
5125 struct igb_adapter *adapter = q_vector->adapter;
5126 u32 itr_val = q_vector->itr_val & 0x7FFC;
5128 if (!q_vector->set_itr)
5134 if (adapter->hw.mac.type == e1000_82575)
5135 itr_val |= itr_val << 16;
5137 itr_val |= E1000_EITR_CNT_IGNR;
5139 writel(itr_val, q_vector->itr_register);
5140 q_vector->set_itr = 0;
5143 static irqreturn_t igb_msix_ring(int irq, void *data)
5145 struct igb_q_vector *q_vector = data;
5147 /* Write the ITR value calculated from the previous interrupt. */
5148 igb_write_itr(q_vector);
5150 napi_schedule(&q_vector->napi);
5155 #ifdef CONFIG_IGB_DCA
5156 static void igb_update_tx_dca(struct igb_adapter *adapter,
5157 struct igb_ring *tx_ring,
5160 struct e1000_hw *hw = &adapter->hw;
5161 u32 txctrl = dca3_get_tag(tx_ring->dev, cpu);
5163 if (hw->mac.type != e1000_82575)
5164 txctrl <<= E1000_DCA_TXCTRL_CPUID_SHIFT;
5166 /* We can enable relaxed ordering for reads, but not writes when
5167 * DCA is enabled. This is due to a known issue in some chipsets
5168 * which will cause the DCA tag to be cleared.
5170 txctrl |= E1000_DCA_TXCTRL_DESC_RRO_EN |
5171 E1000_DCA_TXCTRL_DATA_RRO_EN |
5172 E1000_DCA_TXCTRL_DESC_DCA_EN;
5174 wr32(E1000_DCA_TXCTRL(tx_ring->reg_idx), txctrl);
5177 static void igb_update_rx_dca(struct igb_adapter *adapter,
5178 struct igb_ring *rx_ring,
5181 struct e1000_hw *hw = &adapter->hw;
5182 u32 rxctrl = dca3_get_tag(&adapter->pdev->dev, cpu);
5184 if (hw->mac.type != e1000_82575)
5185 rxctrl <<= E1000_DCA_RXCTRL_CPUID_SHIFT;
5187 /* We can enable relaxed ordering for reads, but not writes when
5188 * DCA is enabled. This is due to a known issue in some chipsets
5189 * which will cause the DCA tag to be cleared.
5191 rxctrl |= E1000_DCA_RXCTRL_DESC_RRO_EN |
5192 E1000_DCA_RXCTRL_DESC_DCA_EN;
5194 wr32(E1000_DCA_RXCTRL(rx_ring->reg_idx), rxctrl);
5197 static void igb_update_dca(struct igb_q_vector *q_vector)
5199 struct igb_adapter *adapter = q_vector->adapter;
5200 int cpu = get_cpu();
5202 if (q_vector->cpu == cpu)
5205 if (q_vector->tx.ring)
5206 igb_update_tx_dca(adapter, q_vector->tx.ring, cpu);
5208 if (q_vector->rx.ring)
5209 igb_update_rx_dca(adapter, q_vector->rx.ring, cpu);
5211 q_vector->cpu = cpu;
5216 static void igb_setup_dca(struct igb_adapter *adapter)
5218 struct e1000_hw *hw = &adapter->hw;
5221 if (!(adapter->flags & IGB_FLAG_DCA_ENABLED))
5224 /* Always use CB2 mode, difference is masked in the CB driver. */
5225 wr32(E1000_DCA_CTRL, E1000_DCA_CTRL_DCA_MODE_CB2);
5227 for (i = 0; i < adapter->num_q_vectors; i++) {
5228 adapter->q_vector[i]->cpu = -1;
5229 igb_update_dca(adapter->q_vector[i]);
5233 static int __igb_notify_dca(struct device *dev, void *data)
5235 struct net_device *netdev = dev_get_drvdata(dev);
5236 struct igb_adapter *adapter = netdev_priv(netdev);
5237 struct pci_dev *pdev = adapter->pdev;
5238 struct e1000_hw *hw = &adapter->hw;
5239 unsigned long event = *(unsigned long *)data;
5242 case DCA_PROVIDER_ADD:
5243 /* if already enabled, don't do it again */
5244 if (adapter->flags & IGB_FLAG_DCA_ENABLED)
5246 if (dca_add_requester(dev) == 0) {
5247 adapter->flags |= IGB_FLAG_DCA_ENABLED;
5248 dev_info(&pdev->dev, "DCA enabled\n");
5249 igb_setup_dca(adapter);
5252 /* Fall Through since DCA is disabled. */
5253 case DCA_PROVIDER_REMOVE:
5254 if (adapter->flags & IGB_FLAG_DCA_ENABLED) {
5255 /* without this a class_device is left
5256 * hanging around in the sysfs model
5258 dca_remove_requester(dev);
5259 dev_info(&pdev->dev, "DCA disabled\n");
5260 adapter->flags &= ~IGB_FLAG_DCA_ENABLED;
5261 wr32(E1000_DCA_CTRL, E1000_DCA_CTRL_DCA_MODE_DISABLE);
5269 static int igb_notify_dca(struct notifier_block *nb, unsigned long event,
5274 ret_val = driver_for_each_device(&igb_driver.driver, NULL, &event,
5277 return ret_val ? NOTIFY_BAD : NOTIFY_DONE;
5279 #endif /* CONFIG_IGB_DCA */
5281 #ifdef CONFIG_PCI_IOV
5282 static int igb_vf_configure(struct igb_adapter *adapter, int vf)
5284 unsigned char mac_addr[ETH_ALEN];
5286 eth_zero_addr(mac_addr);
5287 igb_set_vf_mac(adapter, vf, mac_addr);
5289 /* By default spoof check is enabled for all VFs */
5290 adapter->vf_data[vf].spoofchk_enabled = true;
5296 static void igb_ping_all_vfs(struct igb_adapter *adapter)
5298 struct e1000_hw *hw = &adapter->hw;
5302 for (i = 0 ; i < adapter->vfs_allocated_count; i++) {
5303 ping = E1000_PF_CONTROL_MSG;
5304 if (adapter->vf_data[i].flags & IGB_VF_FLAG_CTS)
5305 ping |= E1000_VT_MSGTYPE_CTS;
5306 igb_write_mbx(hw, &ping, 1, i);
5310 static int igb_set_vf_promisc(struct igb_adapter *adapter, u32 *msgbuf, u32 vf)
5312 struct e1000_hw *hw = &adapter->hw;
5313 u32 vmolr = rd32(E1000_VMOLR(vf));
5314 struct vf_data_storage *vf_data = &adapter->vf_data[vf];
5316 vf_data->flags &= ~(IGB_VF_FLAG_UNI_PROMISC |
5317 IGB_VF_FLAG_MULTI_PROMISC);
5318 vmolr &= ~(E1000_VMOLR_ROPE | E1000_VMOLR_ROMPE | E1000_VMOLR_MPME);
5320 if (*msgbuf & E1000_VF_SET_PROMISC_MULTICAST) {
5321 vmolr |= E1000_VMOLR_MPME;
5322 vf_data->flags |= IGB_VF_FLAG_MULTI_PROMISC;
5323 *msgbuf &= ~E1000_VF_SET_PROMISC_MULTICAST;
5325 /* if we have hashes and we are clearing a multicast promisc
5326 * flag we need to write the hashes to the MTA as this step
5327 * was previously skipped
5329 if (vf_data->num_vf_mc_hashes > 30) {
5330 vmolr |= E1000_VMOLR_MPME;
5331 } else if (vf_data->num_vf_mc_hashes) {
5333 vmolr |= E1000_VMOLR_ROMPE;
5334 for (j = 0; j < vf_data->num_vf_mc_hashes; j++)
5335 igb_mta_set(hw, vf_data->vf_mc_hashes[j]);
5339 wr32(E1000_VMOLR(vf), vmolr);
5341 /* there are flags left unprocessed, likely not supported */
5342 if (*msgbuf & E1000_VT_MSGINFO_MASK)
5348 static int igb_set_vf_multicasts(struct igb_adapter *adapter,
5349 u32 *msgbuf, u32 vf)
5351 int n = (msgbuf[0] & E1000_VT_MSGINFO_MASK) >> E1000_VT_MSGINFO_SHIFT;
5352 u16 *hash_list = (u16 *)&msgbuf[1];
5353 struct vf_data_storage *vf_data = &adapter->vf_data[vf];
5356 /* salt away the number of multicast addresses assigned
5357 * to this VF for later use to restore when the PF multi cast
5360 vf_data->num_vf_mc_hashes = n;
5362 /* only up to 30 hash values supported */
5366 /* store the hashes for later use */
5367 for (i = 0; i < n; i++)
5368 vf_data->vf_mc_hashes[i] = hash_list[i];
5370 /* Flush and reset the mta with the new values */
5371 igb_set_rx_mode(adapter->netdev);
5376 static void igb_restore_vf_multicasts(struct igb_adapter *adapter)
5378 struct e1000_hw *hw = &adapter->hw;
5379 struct vf_data_storage *vf_data;
5382 for (i = 0; i < adapter->vfs_allocated_count; i++) {
5383 u32 vmolr = rd32(E1000_VMOLR(i));
5384 vmolr &= ~(E1000_VMOLR_ROMPE | E1000_VMOLR_MPME);
5386 vf_data = &adapter->vf_data[i];
5388 if ((vf_data->num_vf_mc_hashes > 30) ||
5389 (vf_data->flags & IGB_VF_FLAG_MULTI_PROMISC)) {
5390 vmolr |= E1000_VMOLR_MPME;
5391 } else if (vf_data->num_vf_mc_hashes) {
5392 vmolr |= E1000_VMOLR_ROMPE;
5393 for (j = 0; j < vf_data->num_vf_mc_hashes; j++)
5394 igb_mta_set(hw, vf_data->vf_mc_hashes[j]);
5396 wr32(E1000_VMOLR(i), vmolr);
5400 static void igb_clear_vf_vfta(struct igb_adapter *adapter, u32 vf)
5402 struct e1000_hw *hw = &adapter->hw;
5403 u32 pool_mask, reg, vid;
5406 pool_mask = 1 << (E1000_VLVF_POOLSEL_SHIFT + vf);
5408 /* Find the vlan filter for this id */
5409 for (i = 0; i < E1000_VLVF_ARRAY_SIZE; i++) {
5410 reg = rd32(E1000_VLVF(i));
5412 /* remove the vf from the pool */
5415 /* if pool is empty then remove entry from vfta */
5416 if (!(reg & E1000_VLVF_POOLSEL_MASK) &&
5417 (reg & E1000_VLVF_VLANID_ENABLE)) {
5419 vid = reg & E1000_VLVF_VLANID_MASK;
5420 igb_vfta_set(hw, vid, false);
5423 wr32(E1000_VLVF(i), reg);
5426 adapter->vf_data[vf].vlans_enabled = 0;
5429 static s32 igb_vlvf_set(struct igb_adapter *adapter, u32 vid, bool add, u32 vf)
5431 struct e1000_hw *hw = &adapter->hw;
5434 /* The vlvf table only exists on 82576 hardware and newer */
5435 if (hw->mac.type < e1000_82576)
5438 /* we only need to do this if VMDq is enabled */
5439 if (!adapter->vfs_allocated_count)
5442 /* Find the vlan filter for this id */
5443 for (i = 0; i < E1000_VLVF_ARRAY_SIZE; i++) {
5444 reg = rd32(E1000_VLVF(i));
5445 if ((reg & E1000_VLVF_VLANID_ENABLE) &&
5446 vid == (reg & E1000_VLVF_VLANID_MASK))
5451 if (i == E1000_VLVF_ARRAY_SIZE) {
5452 /* Did not find a matching VLAN ID entry that was
5453 * enabled. Search for a free filter entry, i.e.
5454 * one without the enable bit set
5456 for (i = 0; i < E1000_VLVF_ARRAY_SIZE; i++) {
5457 reg = rd32(E1000_VLVF(i));
5458 if (!(reg & E1000_VLVF_VLANID_ENABLE))
5462 if (i < E1000_VLVF_ARRAY_SIZE) {
5463 /* Found an enabled/available entry */
5464 reg |= 1 << (E1000_VLVF_POOLSEL_SHIFT + vf);
5466 /* if !enabled we need to set this up in vfta */
5467 if (!(reg & E1000_VLVF_VLANID_ENABLE)) {
5468 /* add VID to filter table */
5469 igb_vfta_set(hw, vid, true);
5470 reg |= E1000_VLVF_VLANID_ENABLE;
5472 reg &= ~E1000_VLVF_VLANID_MASK;
5474 wr32(E1000_VLVF(i), reg);
5476 /* do not modify RLPML for PF devices */
5477 if (vf >= adapter->vfs_allocated_count)
5480 if (!adapter->vf_data[vf].vlans_enabled) {
5482 reg = rd32(E1000_VMOLR(vf));
5483 size = reg & E1000_VMOLR_RLPML_MASK;
5485 reg &= ~E1000_VMOLR_RLPML_MASK;
5487 wr32(E1000_VMOLR(vf), reg);
5490 adapter->vf_data[vf].vlans_enabled++;
5493 if (i < E1000_VLVF_ARRAY_SIZE) {
5494 /* remove vf from the pool */
5495 reg &= ~(1 << (E1000_VLVF_POOLSEL_SHIFT + vf));
5496 /* if pool is empty then remove entry from vfta */
5497 if (!(reg & E1000_VLVF_POOLSEL_MASK)) {
5499 igb_vfta_set(hw, vid, false);
5501 wr32(E1000_VLVF(i), reg);
5503 /* do not modify RLPML for PF devices */
5504 if (vf >= adapter->vfs_allocated_count)
5507 adapter->vf_data[vf].vlans_enabled--;
5508 if (!adapter->vf_data[vf].vlans_enabled) {
5510 reg = rd32(E1000_VMOLR(vf));
5511 size = reg & E1000_VMOLR_RLPML_MASK;
5513 reg &= ~E1000_VMOLR_RLPML_MASK;
5515 wr32(E1000_VMOLR(vf), reg);
5522 static void igb_set_vmvir(struct igb_adapter *adapter, u32 vid, u32 vf)
5524 struct e1000_hw *hw = &adapter->hw;
5527 wr32(E1000_VMVIR(vf), (vid | E1000_VMVIR_VLANA_DEFAULT));
5529 wr32(E1000_VMVIR(vf), 0);
5532 static int igb_ndo_set_vf_vlan(struct net_device *netdev,
5533 int vf, u16 vlan, u8 qos)
5536 struct igb_adapter *adapter = netdev_priv(netdev);
5538 if ((vf >= adapter->vfs_allocated_count) || (vlan > 4095) || (qos > 7))
5541 err = igb_vlvf_set(adapter, vlan, !!vlan, vf);
5544 igb_set_vmvir(adapter, vlan | (qos << VLAN_PRIO_SHIFT), vf);
5545 igb_set_vmolr(adapter, vf, !vlan);
5546 adapter->vf_data[vf].pf_vlan = vlan;
5547 adapter->vf_data[vf].pf_qos = qos;
5548 dev_info(&adapter->pdev->dev,
5549 "Setting VLAN %d, QOS 0x%x on VF %d\n", vlan, qos, vf);
5550 if (test_bit(__IGB_DOWN, &adapter->state)) {
5551 dev_warn(&adapter->pdev->dev,
5552 "The VF VLAN has been set, but the PF device is not up.\n");
5553 dev_warn(&adapter->pdev->dev,
5554 "Bring the PF device up before attempting to use the VF device.\n");
5557 igb_vlvf_set(adapter, adapter->vf_data[vf].pf_vlan,
5559 igb_set_vmvir(adapter, vlan, vf);
5560 igb_set_vmolr(adapter, vf, true);
5561 adapter->vf_data[vf].pf_vlan = 0;
5562 adapter->vf_data[vf].pf_qos = 0;
5568 static int igb_find_vlvf_entry(struct igb_adapter *adapter, int vid)
5570 struct e1000_hw *hw = &adapter->hw;
5574 /* Find the vlan filter for this id */
5575 for (i = 0; i < E1000_VLVF_ARRAY_SIZE; i++) {
5576 reg = rd32(E1000_VLVF(i));
5577 if ((reg & E1000_VLVF_VLANID_ENABLE) &&
5578 vid == (reg & E1000_VLVF_VLANID_MASK))
5582 if (i >= E1000_VLVF_ARRAY_SIZE)
5588 static int igb_set_vf_vlan(struct igb_adapter *adapter, u32 *msgbuf, u32 vf)
5590 struct e1000_hw *hw = &adapter->hw;
5591 int add = (msgbuf[0] & E1000_VT_MSGINFO_MASK) >> E1000_VT_MSGINFO_SHIFT;
5592 int vid = (msgbuf[1] & E1000_VLVF_VLANID_MASK);
5595 /* If in promiscuous mode we need to make sure the PF also has
5596 * the VLAN filter set.
5598 if (add && (adapter->netdev->flags & IFF_PROMISC))
5599 err = igb_vlvf_set(adapter, vid, add,
5600 adapter->vfs_allocated_count);
5604 err = igb_vlvf_set(adapter, vid, add, vf);
5609 /* Go through all the checks to see if the VLAN filter should
5610 * be wiped completely.
5612 if (!add && (adapter->netdev->flags & IFF_PROMISC)) {
5615 int regndx = igb_find_vlvf_entry(adapter, vid);
5618 /* See if any other pools are set for this VLAN filter
5619 * entry other than the PF.
5621 vlvf = bits = rd32(E1000_VLVF(regndx));
5622 bits &= 1 << (E1000_VLVF_POOLSEL_SHIFT +
5623 adapter->vfs_allocated_count);
5624 /* If the filter was removed then ensure PF pool bit
5625 * is cleared if the PF only added itself to the pool
5626 * because the PF is in promiscuous mode.
5628 if ((vlvf & VLAN_VID_MASK) == vid &&
5629 !test_bit(vid, adapter->active_vlans) &&
5631 igb_vlvf_set(adapter, vid, add,
5632 adapter->vfs_allocated_count);
5639 static inline void igb_vf_reset(struct igb_adapter *adapter, u32 vf)
5641 /* clear flags - except flag that indicates PF has set the MAC */
5642 adapter->vf_data[vf].flags &= IGB_VF_FLAG_PF_SET_MAC;
5643 adapter->vf_data[vf].last_nack = jiffies;
5645 /* reset offloads to defaults */
5646 igb_set_vmolr(adapter, vf, true);
5648 /* reset vlans for device */
5649 igb_clear_vf_vfta(adapter, vf);
5650 if (adapter->vf_data[vf].pf_vlan)
5651 igb_ndo_set_vf_vlan(adapter->netdev, vf,
5652 adapter->vf_data[vf].pf_vlan,
5653 adapter->vf_data[vf].pf_qos);
5655 igb_clear_vf_vfta(adapter, vf);
5657 /* reset multicast table array for vf */
5658 adapter->vf_data[vf].num_vf_mc_hashes = 0;
5660 /* Flush and reset the mta with the new values */
5661 igb_set_rx_mode(adapter->netdev);
5664 static void igb_vf_reset_event(struct igb_adapter *adapter, u32 vf)
5666 unsigned char *vf_mac = adapter->vf_data[vf].vf_mac_addresses;
5668 /* clear mac address as we were hotplug removed/added */
5669 if (!(adapter->vf_data[vf].flags & IGB_VF_FLAG_PF_SET_MAC))
5670 eth_zero_addr(vf_mac);
5672 /* process remaining reset events */
5673 igb_vf_reset(adapter, vf);
5676 static void igb_vf_reset_msg(struct igb_adapter *adapter, u32 vf)
5678 struct e1000_hw *hw = &adapter->hw;
5679 unsigned char *vf_mac = adapter->vf_data[vf].vf_mac_addresses;
5680 int rar_entry = hw->mac.rar_entry_count - (vf + 1);
5682 u8 *addr = (u8 *)(&msgbuf[1]);
5684 /* process all the same items cleared in a function level reset */
5685 igb_vf_reset(adapter, vf);
5687 /* set vf mac address */
5688 igb_rar_set_qsel(adapter, vf_mac, rar_entry, vf);
5690 /* enable transmit and receive for vf */
5691 reg = rd32(E1000_VFTE);
5692 wr32(E1000_VFTE, reg | (1 << vf));
5693 reg = rd32(E1000_VFRE);
5694 wr32(E1000_VFRE, reg | (1 << vf));
5696 adapter->vf_data[vf].flags |= IGB_VF_FLAG_CTS;
5698 /* reply to reset with ack and vf mac address */
5699 msgbuf[0] = E1000_VF_RESET | E1000_VT_MSGTYPE_ACK;
5700 memcpy(addr, vf_mac, ETH_ALEN);
5701 igb_write_mbx(hw, msgbuf, 3, vf);
5704 static int igb_set_vf_mac_addr(struct igb_adapter *adapter, u32 *msg, int vf)
5706 /* The VF MAC Address is stored in a packed array of bytes
5707 * starting at the second 32 bit word of the msg array
5709 unsigned char *addr = (char *)&msg[1];
5712 if (is_valid_ether_addr(addr))
5713 err = igb_set_vf_mac(adapter, vf, addr);
5718 static void igb_rcv_ack_from_vf(struct igb_adapter *adapter, u32 vf)
5720 struct e1000_hw *hw = &adapter->hw;
5721 struct vf_data_storage *vf_data = &adapter->vf_data[vf];
5722 u32 msg = E1000_VT_MSGTYPE_NACK;
5724 /* if device isn't clear to send it shouldn't be reading either */
5725 if (!(vf_data->flags & IGB_VF_FLAG_CTS) &&
5726 time_after(jiffies, vf_data->last_nack + (2 * HZ))) {
5727 igb_write_mbx(hw, &msg, 1, vf);
5728 vf_data->last_nack = jiffies;
5732 static void igb_rcv_msg_from_vf(struct igb_adapter *adapter, u32 vf)
5734 struct pci_dev *pdev = adapter->pdev;
5735 u32 msgbuf[E1000_VFMAILBOX_SIZE];
5736 struct e1000_hw *hw = &adapter->hw;
5737 struct vf_data_storage *vf_data = &adapter->vf_data[vf];
5740 retval = igb_read_mbx(hw, msgbuf, E1000_VFMAILBOX_SIZE, vf);
5743 /* if receive failed revoke VF CTS stats and restart init */
5744 dev_err(&pdev->dev, "Error receiving message from VF\n");
5745 vf_data->flags &= ~IGB_VF_FLAG_CTS;
5746 if (!time_after(jiffies, vf_data->last_nack + (2 * HZ)))
5751 /* this is a message we already processed, do nothing */
5752 if (msgbuf[0] & (E1000_VT_MSGTYPE_ACK | E1000_VT_MSGTYPE_NACK))
5755 /* until the vf completes a reset it should not be
5756 * allowed to start any configuration.
5758 if (msgbuf[0] == E1000_VF_RESET) {
5759 igb_vf_reset_msg(adapter, vf);
5763 if (!(vf_data->flags & IGB_VF_FLAG_CTS)) {
5764 if (!time_after(jiffies, vf_data->last_nack + (2 * HZ)))
5770 switch ((msgbuf[0] & 0xFFFF)) {
5771 case E1000_VF_SET_MAC_ADDR:
5773 if (!(vf_data->flags & IGB_VF_FLAG_PF_SET_MAC))
5774 retval = igb_set_vf_mac_addr(adapter, msgbuf, vf);
5776 dev_warn(&pdev->dev,
5777 "VF %d attempted to override administratively set MAC address\nReload the VF driver to resume operations\n",
5780 case E1000_VF_SET_PROMISC:
5781 retval = igb_set_vf_promisc(adapter, msgbuf, vf);
5783 case E1000_VF_SET_MULTICAST:
5784 retval = igb_set_vf_multicasts(adapter, msgbuf, vf);
5786 case E1000_VF_SET_LPE:
5787 retval = igb_set_vf_rlpml(adapter, msgbuf[1], vf);
5789 case E1000_VF_SET_VLAN:
5791 if (vf_data->pf_vlan)
5792 dev_warn(&pdev->dev,
5793 "VF %d attempted to override administratively set VLAN tag\nReload the VF driver to resume operations\n",
5796 retval = igb_set_vf_vlan(adapter, msgbuf, vf);
5799 dev_err(&pdev->dev, "Unhandled Msg %08x\n", msgbuf[0]);
5804 msgbuf[0] |= E1000_VT_MSGTYPE_CTS;
5806 /* notify the VF of the results of what it sent us */
5808 msgbuf[0] |= E1000_VT_MSGTYPE_NACK;
5810 msgbuf[0] |= E1000_VT_MSGTYPE_ACK;
5812 igb_write_mbx(hw, msgbuf, 1, vf);
5815 static void igb_msg_task(struct igb_adapter *adapter)
5817 struct e1000_hw *hw = &adapter->hw;
5820 for (vf = 0; vf < adapter->vfs_allocated_count; vf++) {
5821 /* process any reset requests */
5822 if (!igb_check_for_rst(hw, vf))
5823 igb_vf_reset_event(adapter, vf);
5825 /* process any messages pending */
5826 if (!igb_check_for_msg(hw, vf))
5827 igb_rcv_msg_from_vf(adapter, vf);
5829 /* process any acks */
5830 if (!igb_check_for_ack(hw, vf))
5831 igb_rcv_ack_from_vf(adapter, vf);
5836 * igb_set_uta - Set unicast filter table address
5837 * @adapter: board private structure
5839 * The unicast table address is a register array of 32-bit registers.
5840 * The table is meant to be used in a way similar to how the MTA is used
5841 * however due to certain limitations in the hardware it is necessary to
5842 * set all the hash bits to 1 and use the VMOLR ROPE bit as a promiscuous
5843 * enable bit to allow vlan tag stripping when promiscuous mode is enabled
5845 static void igb_set_uta(struct igb_adapter *adapter)
5847 struct e1000_hw *hw = &adapter->hw;
5850 /* The UTA table only exists on 82576 hardware and newer */
5851 if (hw->mac.type < e1000_82576)
5854 /* we only need to do this if VMDq is enabled */
5855 if (!adapter->vfs_allocated_count)
5858 for (i = 0; i < hw->mac.uta_reg_count; i++)
5859 array_wr32(E1000_UTA, i, ~0);
5863 * igb_intr_msi - Interrupt Handler
5864 * @irq: interrupt number
5865 * @data: pointer to a network interface device structure
5867 static irqreturn_t igb_intr_msi(int irq, void *data)
5869 struct igb_adapter *adapter = data;
5870 struct igb_q_vector *q_vector = adapter->q_vector[0];
5871 struct e1000_hw *hw = &adapter->hw;
5872 /* read ICR disables interrupts using IAM */
5873 u32 icr = rd32(E1000_ICR);
5875 igb_write_itr(q_vector);
5877 if (icr & E1000_ICR_DRSTA)
5878 schedule_work(&adapter->reset_task);
5880 if (icr & E1000_ICR_DOUTSYNC) {
5881 /* HW is reporting DMA is out of sync */
5882 adapter->stats.doosync++;
5885 if (icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) {
5886 hw->mac.get_link_status = 1;
5887 if (!test_bit(__IGB_DOWN, &adapter->state))
5888 mod_timer(&adapter->watchdog_timer, jiffies + 1);
5891 if (icr & E1000_ICR_TS) {
5892 u32 tsicr = rd32(E1000_TSICR);
5894 if (tsicr & E1000_TSICR_TXTS) {
5895 /* acknowledge the interrupt */
5896 wr32(E1000_TSICR, E1000_TSICR_TXTS);
5897 /* retrieve hardware timestamp */
5898 schedule_work(&adapter->ptp_tx_work);
5902 napi_schedule(&q_vector->napi);
5908 * igb_intr - Legacy Interrupt Handler
5909 * @irq: interrupt number
5910 * @data: pointer to a network interface device structure
5912 static irqreturn_t igb_intr(int irq, void *data)
5914 struct igb_adapter *adapter = data;
5915 struct igb_q_vector *q_vector = adapter->q_vector[0];
5916 struct e1000_hw *hw = &adapter->hw;
5917 /* Interrupt Auto-Mask...upon reading ICR, interrupts are masked. No
5918 * need for the IMC write
5920 u32 icr = rd32(E1000_ICR);
5922 /* IMS will not auto-mask if INT_ASSERTED is not set, and if it is
5923 * not set, then the adapter didn't send an interrupt
5925 if (!(icr & E1000_ICR_INT_ASSERTED))
5928 igb_write_itr(q_vector);
5930 if (icr & E1000_ICR_DRSTA)
5931 schedule_work(&adapter->reset_task);
5933 if (icr & E1000_ICR_DOUTSYNC) {
5934 /* HW is reporting DMA is out of sync */
5935 adapter->stats.doosync++;
5938 if (icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) {
5939 hw->mac.get_link_status = 1;
5940 /* guard against interrupt when we're going down */
5941 if (!test_bit(__IGB_DOWN, &adapter->state))
5942 mod_timer(&adapter->watchdog_timer, jiffies + 1);
5945 if (icr & E1000_ICR_TS) {
5946 u32 tsicr = rd32(E1000_TSICR);
5948 if (tsicr & E1000_TSICR_TXTS) {
5949 /* acknowledge the interrupt */
5950 wr32(E1000_TSICR, E1000_TSICR_TXTS);
5951 /* retrieve hardware timestamp */
5952 schedule_work(&adapter->ptp_tx_work);
5956 napi_schedule(&q_vector->napi);
5961 static void igb_ring_irq_enable(struct igb_q_vector *q_vector)
5963 struct igb_adapter *adapter = q_vector->adapter;
5964 struct e1000_hw *hw = &adapter->hw;
5966 if ((q_vector->rx.ring && (adapter->rx_itr_setting & 3)) ||
5967 (!q_vector->rx.ring && (adapter->tx_itr_setting & 3))) {
5968 if ((adapter->num_q_vectors == 1) && !adapter->vf_data)
5969 igb_set_itr(q_vector);
5971 igb_update_ring_itr(q_vector);
5974 if (!test_bit(__IGB_DOWN, &adapter->state)) {
5975 if (adapter->msix_entries)
5976 wr32(E1000_EIMS, q_vector->eims_value);
5978 igb_irq_enable(adapter);
5983 * igb_poll - NAPI Rx polling callback
5984 * @napi: napi polling structure
5985 * @budget: count of how many packets we should handle
5987 static int igb_poll(struct napi_struct *napi, int budget)
5989 struct igb_q_vector *q_vector = container_of(napi,
5990 struct igb_q_vector,
5992 bool clean_complete = true;
5994 #ifdef CONFIG_IGB_DCA
5995 if (q_vector->adapter->flags & IGB_FLAG_DCA_ENABLED)
5996 igb_update_dca(q_vector);
5998 if (q_vector->tx.ring)
5999 clean_complete = igb_clean_tx_irq(q_vector);
6001 if (q_vector->rx.ring)
6002 clean_complete &= igb_clean_rx_irq(q_vector, budget);
6004 /* If all work not completed, return budget and keep polling */
6005 if (!clean_complete)
6008 /* If not enough Rx work done, exit the polling mode */
6009 napi_complete(napi);
6010 igb_ring_irq_enable(q_vector);
6016 * igb_clean_tx_irq - Reclaim resources after transmit completes
6017 * @q_vector: pointer to q_vector containing needed info
6019 * returns true if ring is completely cleaned
6021 static bool igb_clean_tx_irq(struct igb_q_vector *q_vector)
6023 struct igb_adapter *adapter = q_vector->adapter;
6024 struct igb_ring *tx_ring = q_vector->tx.ring;
6025 struct igb_tx_buffer *tx_buffer;
6026 union e1000_adv_tx_desc *tx_desc;
6027 unsigned int total_bytes = 0, total_packets = 0;
6028 unsigned int budget = q_vector->tx.work_limit;
6029 unsigned int i = tx_ring->next_to_clean;
6031 if (test_bit(__IGB_DOWN, &adapter->state))
6034 tx_buffer = &tx_ring->tx_buffer_info[i];
6035 tx_desc = IGB_TX_DESC(tx_ring, i);
6036 i -= tx_ring->count;
6039 union e1000_adv_tx_desc *eop_desc = tx_buffer->next_to_watch;
6041 /* if next_to_watch is not set then there is no work pending */
6045 /* prevent any other reads prior to eop_desc */
6046 read_barrier_depends();
6048 /* if DD is not set pending work has not been completed */
6049 if (!(eop_desc->wb.status & cpu_to_le32(E1000_TXD_STAT_DD)))
6052 /* clear next_to_watch to prevent false hangs */
6053 tx_buffer->next_to_watch = NULL;
6055 /* update the statistics for this packet */
6056 total_bytes += tx_buffer->bytecount;
6057 total_packets += tx_buffer->gso_segs;
6060 dev_kfree_skb_any(tx_buffer->skb);
6062 /* unmap skb header data */
6063 dma_unmap_single(tx_ring->dev,
6064 dma_unmap_addr(tx_buffer, dma),
6065 dma_unmap_len(tx_buffer, len),
6068 /* clear tx_buffer data */
6069 tx_buffer->skb = NULL;
6070 dma_unmap_len_set(tx_buffer, len, 0);
6072 /* clear last DMA location and unmap remaining buffers */
6073 while (tx_desc != eop_desc) {
6078 i -= tx_ring->count;
6079 tx_buffer = tx_ring->tx_buffer_info;
6080 tx_desc = IGB_TX_DESC(tx_ring, 0);
6083 /* unmap any remaining paged data */
6084 if (dma_unmap_len(tx_buffer, len)) {
6085 dma_unmap_page(tx_ring->dev,
6086 dma_unmap_addr(tx_buffer, dma),
6087 dma_unmap_len(tx_buffer, len),
6089 dma_unmap_len_set(tx_buffer, len, 0);
6093 /* move us one more past the eop_desc for start of next pkt */
6098 i -= tx_ring->count;
6099 tx_buffer = tx_ring->tx_buffer_info;
6100 tx_desc = IGB_TX_DESC(tx_ring, 0);
6103 /* issue prefetch for next Tx descriptor */
6106 /* update budget accounting */
6108 } while (likely(budget));
6110 netdev_tx_completed_queue(txring_txq(tx_ring),
6111 total_packets, total_bytes);
6112 i += tx_ring->count;
6113 tx_ring->next_to_clean = i;
6114 u64_stats_update_begin(&tx_ring->tx_syncp);
6115 tx_ring->tx_stats.bytes += total_bytes;
6116 tx_ring->tx_stats.packets += total_packets;
6117 u64_stats_update_end(&tx_ring->tx_syncp);
6118 q_vector->tx.total_bytes += total_bytes;
6119 q_vector->tx.total_packets += total_packets;
6121 if (test_bit(IGB_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags)) {
6122 struct e1000_hw *hw = &adapter->hw;
6124 /* Detect a transmit hang in hardware, this serializes the
6125 * check with the clearing of time_stamp and movement of i
6127 clear_bit(IGB_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags);
6128 if (tx_buffer->next_to_watch &&
6129 time_after(jiffies, tx_buffer->time_stamp +
6130 (adapter->tx_timeout_factor * HZ)) &&
6131 !(rd32(E1000_STATUS) & E1000_STATUS_TXOFF)) {
6133 /* detected Tx unit hang */
6134 dev_err(tx_ring->dev,
6135 "Detected Tx Unit Hang\n"
6139 " next_to_use <%x>\n"
6140 " next_to_clean <%x>\n"
6141 "buffer_info[next_to_clean]\n"
6142 " time_stamp <%lx>\n"
6143 " next_to_watch <%p>\n"
6145 " desc.status <%x>\n",
6146 tx_ring->queue_index,
6147 rd32(E1000_TDH(tx_ring->reg_idx)),
6148 readl(tx_ring->tail),
6149 tx_ring->next_to_use,
6150 tx_ring->next_to_clean,
6151 tx_buffer->time_stamp,
6152 tx_buffer->next_to_watch,
6154 tx_buffer->next_to_watch->wb.status);
6155 netif_stop_subqueue(tx_ring->netdev,
6156 tx_ring->queue_index);
6158 /* we are about to reset, no point in enabling stuff */
6163 #define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
6164 if (unlikely(total_packets &&
6165 netif_carrier_ok(tx_ring->netdev) &&
6166 igb_desc_unused(tx_ring) >= TX_WAKE_THRESHOLD)) {
6167 /* Make sure that anybody stopping the queue after this
6168 * sees the new next_to_clean.
6171 if (__netif_subqueue_stopped(tx_ring->netdev,
6172 tx_ring->queue_index) &&
6173 !(test_bit(__IGB_DOWN, &adapter->state))) {
6174 netif_wake_subqueue(tx_ring->netdev,
6175 tx_ring->queue_index);
6177 u64_stats_update_begin(&tx_ring->tx_syncp);
6178 tx_ring->tx_stats.restart_queue++;
6179 u64_stats_update_end(&tx_ring->tx_syncp);
6187 * igb_reuse_rx_page - page flip buffer and store it back on the ring
6188 * @rx_ring: rx descriptor ring to store buffers on
6189 * @old_buff: donor buffer to have page reused
6191 * Synchronizes page for reuse by the adapter
6193 static void igb_reuse_rx_page(struct igb_ring *rx_ring,
6194 struct igb_rx_buffer *old_buff)
6196 struct igb_rx_buffer *new_buff;
6197 u16 nta = rx_ring->next_to_alloc;
6199 new_buff = &rx_ring->rx_buffer_info[nta];
6201 /* update, and store next to alloc */
6203 rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0;
6205 /* transfer page from old buffer to new buffer */
6206 memcpy(new_buff, old_buff, sizeof(struct igb_rx_buffer));
6208 /* sync the buffer for use by the device */
6209 dma_sync_single_range_for_device(rx_ring->dev, old_buff->dma,
6210 old_buff->page_offset,
6215 static bool igb_can_reuse_rx_page(struct igb_rx_buffer *rx_buffer,
6217 unsigned int truesize)
6219 /* avoid re-using remote pages */
6220 if (unlikely(page_to_nid(page) != numa_node_id()))
6223 #if (PAGE_SIZE < 8192)
6224 /* if we are only owner of page we can reuse it */
6225 if (unlikely(page_count(page) != 1))
6228 /* flip page offset to other buffer */
6229 rx_buffer->page_offset ^= IGB_RX_BUFSZ;
6231 /* since we are the only owner of the page and we need to
6232 * increment it, just set the value to 2 in order to avoid
6233 * an unnecessary locked operation
6235 atomic_set(&page->_count, 2);
6237 /* move offset up to the next cache line */
6238 rx_buffer->page_offset += truesize;
6240 if (rx_buffer->page_offset > (PAGE_SIZE - IGB_RX_BUFSZ))
6243 /* bump ref count on page before it is given to the stack */
6251 * igb_add_rx_frag - Add contents of Rx buffer to sk_buff
6252 * @rx_ring: rx descriptor ring to transact packets on
6253 * @rx_buffer: buffer containing page to add
6254 * @rx_desc: descriptor containing length of buffer written by hardware
6255 * @skb: sk_buff to place the data into
6257 * This function will add the data contained in rx_buffer->page to the skb.
6258 * This is done either through a direct copy if the data in the buffer is
6259 * less than the skb header size, otherwise it will just attach the page as
6260 * a frag to the skb.
6262 * The function will then update the page offset if necessary and return
6263 * true if the buffer can be reused by the adapter.
6265 static bool igb_add_rx_frag(struct igb_ring *rx_ring,
6266 struct igb_rx_buffer *rx_buffer,
6267 union e1000_adv_rx_desc *rx_desc,
6268 struct sk_buff *skb)
6270 struct page *page = rx_buffer->page;
6271 unsigned int size = le16_to_cpu(rx_desc->wb.upper.length);
6272 #if (PAGE_SIZE < 8192)
6273 unsigned int truesize = IGB_RX_BUFSZ;
6275 unsigned int truesize = ALIGN(size, L1_CACHE_BYTES);
6278 if ((size <= IGB_RX_HDR_LEN) && !skb_is_nonlinear(skb)) {
6279 unsigned char *va = page_address(page) + rx_buffer->page_offset;
6281 if (igb_test_staterr(rx_desc, E1000_RXDADV_STAT_TSIP)) {
6282 igb_ptp_rx_pktstamp(rx_ring->q_vector, va, skb);
6283 va += IGB_TS_HDR_LEN;
6284 size -= IGB_TS_HDR_LEN;
6287 memcpy(__skb_put(skb, size), va, ALIGN(size, sizeof(long)));
6289 /* we can reuse buffer as-is, just make sure it is local */
6290 if (likely(page_to_nid(page) == numa_node_id()))
6293 /* this page cannot be reused so discard it */
6298 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page,
6299 rx_buffer->page_offset, size, truesize);
6301 return igb_can_reuse_rx_page(rx_buffer, page, truesize);
6304 static struct sk_buff *igb_fetch_rx_buffer(struct igb_ring *rx_ring,
6305 union e1000_adv_rx_desc *rx_desc,
6306 struct sk_buff *skb)
6308 struct igb_rx_buffer *rx_buffer;
6311 rx_buffer = &rx_ring->rx_buffer_info[rx_ring->next_to_clean];
6313 page = rx_buffer->page;
6317 void *page_addr = page_address(page) +
6318 rx_buffer->page_offset;
6320 /* prefetch first cache line of first page */
6321 prefetch(page_addr);
6322 #if L1_CACHE_BYTES < 128
6323 prefetch(page_addr + L1_CACHE_BYTES);
6326 /* allocate a skb to store the frags */
6327 skb = netdev_alloc_skb_ip_align(rx_ring->netdev,
6329 if (unlikely(!skb)) {
6330 rx_ring->rx_stats.alloc_failed++;
6334 /* we will be copying header into skb->data in
6335 * pskb_may_pull so it is in our interest to prefetch
6336 * it now to avoid a possible cache miss
6338 prefetchw(skb->data);
6341 /* we are reusing so sync this buffer for CPU use */
6342 dma_sync_single_range_for_cpu(rx_ring->dev,
6344 rx_buffer->page_offset,
6348 /* pull page into skb */
6349 if (igb_add_rx_frag(rx_ring, rx_buffer, rx_desc, skb)) {
6350 /* hand second half of page back to the ring */
6351 igb_reuse_rx_page(rx_ring, rx_buffer);
6353 /* we are not reusing the buffer so unmap it */
6354 dma_unmap_page(rx_ring->dev, rx_buffer->dma,
6355 PAGE_SIZE, DMA_FROM_DEVICE);
6358 /* clear contents of rx_buffer */
6359 rx_buffer->page = NULL;
6364 static inline void igb_rx_checksum(struct igb_ring *ring,
6365 union e1000_adv_rx_desc *rx_desc,
6366 struct sk_buff *skb)
6368 skb_checksum_none_assert(skb);
6370 /* Ignore Checksum bit is set */
6371 if (igb_test_staterr(rx_desc, E1000_RXD_STAT_IXSM))
6374 /* Rx checksum disabled via ethtool */
6375 if (!(ring->netdev->features & NETIF_F_RXCSUM))
6378 /* TCP/UDP checksum error bit is set */
6379 if (igb_test_staterr(rx_desc,
6380 E1000_RXDEXT_STATERR_TCPE |
6381 E1000_RXDEXT_STATERR_IPE)) {
6382 /* work around errata with sctp packets where the TCPE aka
6383 * L4E bit is set incorrectly on 64 byte (60 byte w/o crc)
6384 * packets, (aka let the stack check the crc32c)
6386 if (!((skb->len == 60) &&
6387 test_bit(IGB_RING_FLAG_RX_SCTP_CSUM, &ring->flags))) {
6388 u64_stats_update_begin(&ring->rx_syncp);
6389 ring->rx_stats.csum_err++;
6390 u64_stats_update_end(&ring->rx_syncp);
6392 /* let the stack verify checksum errors */
6395 /* It must be a TCP or UDP packet with a valid checksum */
6396 if (igb_test_staterr(rx_desc, E1000_RXD_STAT_TCPCS |
6397 E1000_RXD_STAT_UDPCS))
6398 skb->ip_summed = CHECKSUM_UNNECESSARY;
6400 dev_dbg(ring->dev, "cksum success: bits %08X\n",
6401 le32_to_cpu(rx_desc->wb.upper.status_error));
6404 static inline void igb_rx_hash(struct igb_ring *ring,
6405 union e1000_adv_rx_desc *rx_desc,
6406 struct sk_buff *skb)
6408 if (ring->netdev->features & NETIF_F_RXHASH)
6409 skb->rxhash = le32_to_cpu(rx_desc->wb.lower.hi_dword.rss);
6413 * igb_is_non_eop - process handling of non-EOP buffers
6414 * @rx_ring: Rx ring being processed
6415 * @rx_desc: Rx descriptor for current buffer
6416 * @skb: current socket buffer containing buffer in progress
6418 * This function updates next to clean. If the buffer is an EOP buffer
6419 * this function exits returning false, otherwise it will place the
6420 * sk_buff in the next buffer to be chained and return true indicating
6421 * that this is in fact a non-EOP buffer.
6423 static bool igb_is_non_eop(struct igb_ring *rx_ring,
6424 union e1000_adv_rx_desc *rx_desc)
6426 u32 ntc = rx_ring->next_to_clean + 1;
6428 /* fetch, update, and store next to clean */
6429 ntc = (ntc < rx_ring->count) ? ntc : 0;
6430 rx_ring->next_to_clean = ntc;
6432 prefetch(IGB_RX_DESC(rx_ring, ntc));
6434 if (likely(igb_test_staterr(rx_desc, E1000_RXD_STAT_EOP)))
6441 * igb_get_headlen - determine size of header for LRO/GRO
6442 * @data: pointer to the start of the headers
6443 * @max_len: total length of section to find headers in
6445 * This function is meant to determine the length of headers that will
6446 * be recognized by hardware for LRO, and GRO offloads. The main
6447 * motivation of doing this is to only perform one pull for IPv4 TCP
6448 * packets so that we can do basic things like calculating the gso_size
6449 * based on the average data per packet.
6451 static unsigned int igb_get_headlen(unsigned char *data,
6452 unsigned int max_len)
6455 unsigned char *network;
6458 struct vlan_hdr *vlan;
6461 struct ipv6hdr *ipv6;
6464 u8 nexthdr = 0; /* default to not TCP */
6467 /* this should never happen, but better safe than sorry */
6468 if (max_len < ETH_HLEN)
6471 /* initialize network frame pointer */
6474 /* set first protocol and move network header forward */
6475 protocol = hdr.eth->h_proto;
6476 hdr.network += ETH_HLEN;
6478 /* handle any vlan tag if present */
6479 if (protocol == __constant_htons(ETH_P_8021Q)) {
6480 if ((hdr.network - data) > (max_len - VLAN_HLEN))
6483 protocol = hdr.vlan->h_vlan_encapsulated_proto;
6484 hdr.network += VLAN_HLEN;
6487 /* handle L3 protocols */
6488 if (protocol == __constant_htons(ETH_P_IP)) {
6489 if ((hdr.network - data) > (max_len - sizeof(struct iphdr)))
6492 /* access ihl as a u8 to avoid unaligned access on ia64 */
6493 hlen = (hdr.network[0] & 0x0F) << 2;
6495 /* verify hlen meets minimum size requirements */
6496 if (hlen < sizeof(struct iphdr))
6497 return hdr.network - data;
6499 /* record next protocol if header is present */
6500 if (!(hdr.ipv4->frag_off & htons(IP_OFFSET)))
6501 nexthdr = hdr.ipv4->protocol;
6502 } else if (protocol == __constant_htons(ETH_P_IPV6)) {
6503 if ((hdr.network - data) > (max_len - sizeof(struct ipv6hdr)))
6506 /* record next protocol */
6507 nexthdr = hdr.ipv6->nexthdr;
6508 hlen = sizeof(struct ipv6hdr);
6510 return hdr.network - data;
6513 /* relocate pointer to start of L4 header */
6514 hdr.network += hlen;
6516 /* finally sort out TCP */
6517 if (nexthdr == IPPROTO_TCP) {
6518 if ((hdr.network - data) > (max_len - sizeof(struct tcphdr)))
6521 /* access doff as a u8 to avoid unaligned access on ia64 */
6522 hlen = (hdr.network[12] & 0xF0) >> 2;
6524 /* verify hlen meets minimum size requirements */
6525 if (hlen < sizeof(struct tcphdr))
6526 return hdr.network - data;
6528 hdr.network += hlen;
6529 } else if (nexthdr == IPPROTO_UDP) {
6530 if ((hdr.network - data) > (max_len - sizeof(struct udphdr)))
6533 hdr.network += sizeof(struct udphdr);
6536 /* If everything has gone correctly hdr.network should be the
6537 * data section of the packet and will be the end of the header.
6538 * If not then it probably represents the end of the last recognized
6541 if ((hdr.network - data) < max_len)
6542 return hdr.network - data;
6548 * igb_pull_tail - igb specific version of skb_pull_tail
6549 * @rx_ring: rx descriptor ring packet is being transacted on
6550 * @rx_desc: pointer to the EOP Rx descriptor
6551 * @skb: pointer to current skb being adjusted
6553 * This function is an igb specific version of __pskb_pull_tail. The
6554 * main difference between this version and the original function is that
6555 * this function can make several assumptions about the state of things
6556 * that allow for significant optimizations versus the standard function.
6557 * As a result we can do things like drop a frag and maintain an accurate
6558 * truesize for the skb.
6560 static void igb_pull_tail(struct igb_ring *rx_ring,
6561 union e1000_adv_rx_desc *rx_desc,
6562 struct sk_buff *skb)
6564 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[0];
6566 unsigned int pull_len;
6568 /* it is valid to use page_address instead of kmap since we are
6569 * working with pages allocated out of the lomem pool per
6570 * alloc_page(GFP_ATOMIC)
6572 va = skb_frag_address(frag);
6574 if (igb_test_staterr(rx_desc, E1000_RXDADV_STAT_TSIP)) {
6575 /* retrieve timestamp from buffer */
6576 igb_ptp_rx_pktstamp(rx_ring->q_vector, va, skb);
6578 /* update pointers to remove timestamp header */
6579 skb_frag_size_sub(frag, IGB_TS_HDR_LEN);
6580 frag->page_offset += IGB_TS_HDR_LEN;
6581 skb->data_len -= IGB_TS_HDR_LEN;
6582 skb->len -= IGB_TS_HDR_LEN;
6584 /* move va to start of packet data */
6585 va += IGB_TS_HDR_LEN;
6588 /* we need the header to contain the greater of either ETH_HLEN or
6589 * 60 bytes if the skb->len is less than 60 for skb_pad.
6591 pull_len = igb_get_headlen(va, IGB_RX_HDR_LEN);
6593 /* align pull length to size of long to optimize memcpy performance */
6594 skb_copy_to_linear_data(skb, va, ALIGN(pull_len, sizeof(long)));
6596 /* update all of the pointers */
6597 skb_frag_size_sub(frag, pull_len);
6598 frag->page_offset += pull_len;
6599 skb->data_len -= pull_len;
6600 skb->tail += pull_len;
6604 * igb_cleanup_headers - Correct corrupted or empty headers
6605 * @rx_ring: rx descriptor ring packet is being transacted on
6606 * @rx_desc: pointer to the EOP Rx descriptor
6607 * @skb: pointer to current skb being fixed
6609 * Address the case where we are pulling data in on pages only
6610 * and as such no data is present in the skb header.
6612 * In addition if skb is not at least 60 bytes we need to pad it so that
6613 * it is large enough to qualify as a valid Ethernet frame.
6615 * Returns true if an error was encountered and skb was freed.
6617 static bool igb_cleanup_headers(struct igb_ring *rx_ring,
6618 union e1000_adv_rx_desc *rx_desc,
6619 struct sk_buff *skb)
6621 if (unlikely((igb_test_staterr(rx_desc,
6622 E1000_RXDEXT_ERR_FRAME_ERR_MASK)))) {
6623 struct net_device *netdev = rx_ring->netdev;
6624 if (!(netdev->features & NETIF_F_RXALL)) {
6625 dev_kfree_skb_any(skb);
6630 /* place header in linear portion of buffer */
6631 if (skb_is_nonlinear(skb))
6632 igb_pull_tail(rx_ring, rx_desc, skb);
6634 /* if skb_pad returns an error the skb was freed */
6635 if (unlikely(skb->len < 60)) {
6636 int pad_len = 60 - skb->len;
6638 if (skb_pad(skb, pad_len))
6640 __skb_put(skb, pad_len);
6647 * igb_process_skb_fields - Populate skb header fields from Rx descriptor
6648 * @rx_ring: rx descriptor ring packet is being transacted on
6649 * @rx_desc: pointer to the EOP Rx descriptor
6650 * @skb: pointer to current skb being populated
6652 * This function checks the ring, descriptor, and packet information in
6653 * order to populate the hash, checksum, VLAN, timestamp, protocol, and
6654 * other fields within the skb.
6656 static void igb_process_skb_fields(struct igb_ring *rx_ring,
6657 union e1000_adv_rx_desc *rx_desc,
6658 struct sk_buff *skb)
6660 struct net_device *dev = rx_ring->netdev;
6662 igb_rx_hash(rx_ring, rx_desc, skb);
6664 igb_rx_checksum(rx_ring, rx_desc, skb);
6666 igb_ptp_rx_hwtstamp(rx_ring, rx_desc, skb);
6668 if ((dev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
6669 igb_test_staterr(rx_desc, E1000_RXD_STAT_VP)) {
6671 if (igb_test_staterr(rx_desc, E1000_RXDEXT_STATERR_LB) &&
6672 test_bit(IGB_RING_FLAG_RX_LB_VLAN_BSWAP, &rx_ring->flags))
6673 vid = be16_to_cpu(rx_desc->wb.upper.vlan);
6675 vid = le16_to_cpu(rx_desc->wb.upper.vlan);
6677 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vid);
6680 skb_record_rx_queue(skb, rx_ring->queue_index);
6682 skb->protocol = eth_type_trans(skb, rx_ring->netdev);
6685 static bool igb_clean_rx_irq(struct igb_q_vector *q_vector, const int budget)
6687 struct igb_ring *rx_ring = q_vector->rx.ring;
6688 struct sk_buff *skb = rx_ring->skb;
6689 unsigned int total_bytes = 0, total_packets = 0;
6690 u16 cleaned_count = igb_desc_unused(rx_ring);
6693 union e1000_adv_rx_desc *rx_desc;
6695 /* return some buffers to hardware, one at a time is too slow */
6696 if (cleaned_count >= IGB_RX_BUFFER_WRITE) {
6697 igb_alloc_rx_buffers(rx_ring, cleaned_count);
6701 rx_desc = IGB_RX_DESC(rx_ring, rx_ring->next_to_clean);
6703 if (!igb_test_staterr(rx_desc, E1000_RXD_STAT_DD))
6706 /* This memory barrier is needed to keep us from reading
6707 * any other fields out of the rx_desc until we know the
6708 * RXD_STAT_DD bit is set
6712 /* retrieve a buffer from the ring */
6713 skb = igb_fetch_rx_buffer(rx_ring, rx_desc, skb);
6715 /* exit if we failed to retrieve a buffer */
6721 /* fetch next buffer in frame if non-eop */
6722 if (igb_is_non_eop(rx_ring, rx_desc))
6725 /* verify the packet layout is correct */
6726 if (igb_cleanup_headers(rx_ring, rx_desc, skb)) {
6731 /* probably a little skewed due to removing CRC */
6732 total_bytes += skb->len;
6734 /* populate checksum, timestamp, VLAN, and protocol */
6735 igb_process_skb_fields(rx_ring, rx_desc, skb);
6737 napi_gro_receive(&q_vector->napi, skb);
6739 /* reset skb pointer */
6742 /* update budget accounting */
6744 } while (likely(total_packets < budget));
6746 /* place incomplete frames back on ring for completion */
6749 u64_stats_update_begin(&rx_ring->rx_syncp);
6750 rx_ring->rx_stats.packets += total_packets;
6751 rx_ring->rx_stats.bytes += total_bytes;
6752 u64_stats_update_end(&rx_ring->rx_syncp);
6753 q_vector->rx.total_packets += total_packets;
6754 q_vector->rx.total_bytes += total_bytes;
6757 igb_alloc_rx_buffers(rx_ring, cleaned_count);
6759 return (total_packets < budget);
6762 static bool igb_alloc_mapped_page(struct igb_ring *rx_ring,
6763 struct igb_rx_buffer *bi)
6765 struct page *page = bi->page;
6768 /* since we are recycling buffers we should seldom need to alloc */
6772 /* alloc new page for storage */
6773 page = __skb_alloc_page(GFP_ATOMIC | __GFP_COLD, NULL);
6774 if (unlikely(!page)) {
6775 rx_ring->rx_stats.alloc_failed++;
6779 /* map page for use */
6780 dma = dma_map_page(rx_ring->dev, page, 0, PAGE_SIZE, DMA_FROM_DEVICE);
6782 /* if mapping failed free memory back to system since
6783 * there isn't much point in holding memory we can't use
6785 if (dma_mapping_error(rx_ring->dev, dma)) {
6788 rx_ring->rx_stats.alloc_failed++;
6794 bi->page_offset = 0;
6800 * igb_alloc_rx_buffers - Replace used receive buffers; packet split
6801 * @adapter: address of board private structure
6803 void igb_alloc_rx_buffers(struct igb_ring *rx_ring, u16 cleaned_count)
6805 union e1000_adv_rx_desc *rx_desc;
6806 struct igb_rx_buffer *bi;
6807 u16 i = rx_ring->next_to_use;
6813 rx_desc = IGB_RX_DESC(rx_ring, i);
6814 bi = &rx_ring->rx_buffer_info[i];
6815 i -= rx_ring->count;
6818 if (!igb_alloc_mapped_page(rx_ring, bi))
6821 /* Refresh the desc even if buffer_addrs didn't change
6822 * because each write-back erases this info.
6824 rx_desc->read.pkt_addr = cpu_to_le64(bi->dma + bi->page_offset);
6830 rx_desc = IGB_RX_DESC(rx_ring, 0);
6831 bi = rx_ring->rx_buffer_info;
6832 i -= rx_ring->count;
6835 /* clear the hdr_addr for the next_to_use descriptor */
6836 rx_desc->read.hdr_addr = 0;
6839 } while (cleaned_count);
6841 i += rx_ring->count;
6843 if (rx_ring->next_to_use != i) {
6844 /* record the next descriptor to use */
6845 rx_ring->next_to_use = i;
6847 /* update next to alloc since we have filled the ring */
6848 rx_ring->next_to_alloc = i;
6850 /* Force memory writes to complete before letting h/w
6851 * know there are new descriptors to fetch. (Only
6852 * applicable for weak-ordered memory model archs,
6856 writel(i, rx_ring->tail);
6866 static int igb_mii_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
6868 struct igb_adapter *adapter = netdev_priv(netdev);
6869 struct mii_ioctl_data *data = if_mii(ifr);
6871 if (adapter->hw.phy.media_type != e1000_media_type_copper)
6876 data->phy_id = adapter->hw.phy.addr;
6879 if (igb_read_phy_reg(&adapter->hw, data->reg_num & 0x1F,
6896 static int igb_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
6902 return igb_mii_ioctl(netdev, ifr, cmd);
6904 return igb_ptp_hwtstamp_ioctl(netdev, ifr, cmd);
6910 s32 igb_read_pcie_cap_reg(struct e1000_hw *hw, u32 reg, u16 *value)
6912 struct igb_adapter *adapter = hw->back;
6914 if (pcie_capability_read_word(adapter->pdev, reg, value))
6915 return -E1000_ERR_CONFIG;
6920 s32 igb_write_pcie_cap_reg(struct e1000_hw *hw, u32 reg, u16 *value)
6922 struct igb_adapter *adapter = hw->back;
6924 if (pcie_capability_write_word(adapter->pdev, reg, *value))
6925 return -E1000_ERR_CONFIG;
6930 static void igb_vlan_mode(struct net_device *netdev, netdev_features_t features)
6932 struct igb_adapter *adapter = netdev_priv(netdev);
6933 struct e1000_hw *hw = &adapter->hw;
6935 bool enable = !!(features & NETIF_F_HW_VLAN_CTAG_RX);
6938 /* enable VLAN tag insert/strip */
6939 ctrl = rd32(E1000_CTRL);
6940 ctrl |= E1000_CTRL_VME;
6941 wr32(E1000_CTRL, ctrl);
6943 /* Disable CFI check */
6944 rctl = rd32(E1000_RCTL);
6945 rctl &= ~E1000_RCTL_CFIEN;
6946 wr32(E1000_RCTL, rctl);
6948 /* disable VLAN tag insert/strip */
6949 ctrl = rd32(E1000_CTRL);
6950 ctrl &= ~E1000_CTRL_VME;
6951 wr32(E1000_CTRL, ctrl);
6954 igb_rlpml_set(adapter);
6957 static int igb_vlan_rx_add_vid(struct net_device *netdev,
6958 __be16 proto, u16 vid)
6960 struct igb_adapter *adapter = netdev_priv(netdev);
6961 struct e1000_hw *hw = &adapter->hw;
6962 int pf_id = adapter->vfs_allocated_count;
6964 /* attempt to add filter to vlvf array */
6965 igb_vlvf_set(adapter, vid, true, pf_id);
6967 /* add the filter since PF can receive vlans w/o entry in vlvf */
6968 igb_vfta_set(hw, vid, true);
6970 set_bit(vid, adapter->active_vlans);
6975 static int igb_vlan_rx_kill_vid(struct net_device *netdev,
6976 __be16 proto, u16 vid)
6978 struct igb_adapter *adapter = netdev_priv(netdev);
6979 struct e1000_hw *hw = &adapter->hw;
6980 int pf_id = adapter->vfs_allocated_count;
6983 /* remove vlan from VLVF table array */
6984 err = igb_vlvf_set(adapter, vid, false, pf_id);
6986 /* if vid was not present in VLVF just remove it from table */
6988 igb_vfta_set(hw, vid, false);
6990 clear_bit(vid, adapter->active_vlans);
6995 static void igb_restore_vlan(struct igb_adapter *adapter)
6999 igb_vlan_mode(adapter->netdev, adapter->netdev->features);
7001 for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
7002 igb_vlan_rx_add_vid(adapter->netdev, htons(ETH_P_8021Q), vid);
7005 int igb_set_spd_dplx(struct igb_adapter *adapter, u32 spd, u8 dplx)
7007 struct pci_dev *pdev = adapter->pdev;
7008 struct e1000_mac_info *mac = &adapter->hw.mac;
7012 /* Make sure dplx is at most 1 bit and lsb of speed is not set
7013 * for the switch() below to work
7015 if ((spd & 1) || (dplx & ~1))
7018 /* Fiber NIC's only allow 1000 gbps Full duplex
7019 * and 100Mbps Full duplex for 100baseFx sfp
7021 if (adapter->hw.phy.media_type == e1000_media_type_internal_serdes) {
7022 switch (spd + dplx) {
7023 case SPEED_10 + DUPLEX_HALF:
7024 case SPEED_10 + DUPLEX_FULL:
7025 case SPEED_100 + DUPLEX_HALF:
7032 switch (spd + dplx) {
7033 case SPEED_10 + DUPLEX_HALF:
7034 mac->forced_speed_duplex = ADVERTISE_10_HALF;
7036 case SPEED_10 + DUPLEX_FULL:
7037 mac->forced_speed_duplex = ADVERTISE_10_FULL;
7039 case SPEED_100 + DUPLEX_HALF:
7040 mac->forced_speed_duplex = ADVERTISE_100_HALF;
7042 case SPEED_100 + DUPLEX_FULL:
7043 mac->forced_speed_duplex = ADVERTISE_100_FULL;
7045 case SPEED_1000 + DUPLEX_FULL:
7047 adapter->hw.phy.autoneg_advertised = ADVERTISE_1000_FULL;
7049 case SPEED_1000 + DUPLEX_HALF: /* not supported */
7054 /* clear MDI, MDI(-X) override is only allowed when autoneg enabled */
7055 adapter->hw.phy.mdix = AUTO_ALL_MODES;
7060 dev_err(&pdev->dev, "Unsupported Speed/Duplex configuration\n");
7064 static int __igb_shutdown(struct pci_dev *pdev, bool *enable_wake,
7067 struct net_device *netdev = pci_get_drvdata(pdev);
7068 struct igb_adapter *adapter = netdev_priv(netdev);
7069 struct e1000_hw *hw = &adapter->hw;
7070 u32 ctrl, rctl, status;
7071 u32 wufc = runtime ? E1000_WUFC_LNKC : adapter->wol;
7076 netif_device_detach(netdev);
7078 if (netif_running(netdev))
7079 __igb_close(netdev, true);
7081 igb_clear_interrupt_scheme(adapter);
7084 retval = pci_save_state(pdev);
7089 status = rd32(E1000_STATUS);
7090 if (status & E1000_STATUS_LU)
7091 wufc &= ~E1000_WUFC_LNKC;
7094 igb_setup_rctl(adapter);
7095 igb_set_rx_mode(netdev);
7097 /* turn on all-multi mode if wake on multicast is enabled */
7098 if (wufc & E1000_WUFC_MC) {
7099 rctl = rd32(E1000_RCTL);
7100 rctl |= E1000_RCTL_MPE;
7101 wr32(E1000_RCTL, rctl);
7104 ctrl = rd32(E1000_CTRL);
7105 /* advertise wake from D3Cold */
7106 #define E1000_CTRL_ADVD3WUC 0x00100000
7107 /* phy power management enable */
7108 #define E1000_CTRL_EN_PHY_PWR_MGMT 0x00200000
7109 ctrl |= E1000_CTRL_ADVD3WUC;
7110 wr32(E1000_CTRL, ctrl);
7112 /* Allow time for pending master requests to run */
7113 igb_disable_pcie_master(hw);
7115 wr32(E1000_WUC, E1000_WUC_PME_EN);
7116 wr32(E1000_WUFC, wufc);
7119 wr32(E1000_WUFC, 0);
7122 *enable_wake = wufc || adapter->en_mng_pt;
7124 igb_power_down_link(adapter);
7126 igb_power_up_link(adapter);
7128 /* Release control of h/w to f/w. If f/w is AMT enabled, this
7129 * would have already happened in close and is redundant.
7131 igb_release_hw_control(adapter);
7133 pci_disable_device(pdev);
7139 #ifdef CONFIG_PM_SLEEP
7140 static int igb_suspend(struct device *dev)
7144 struct pci_dev *pdev = to_pci_dev(dev);
7146 retval = __igb_shutdown(pdev, &wake, 0);
7151 pci_prepare_to_sleep(pdev);
7153 pci_wake_from_d3(pdev, false);
7154 pci_set_power_state(pdev, PCI_D3hot);
7159 #endif /* CONFIG_PM_SLEEP */
7161 static int igb_resume(struct device *dev)
7163 struct pci_dev *pdev = to_pci_dev(dev);
7164 struct net_device *netdev = pci_get_drvdata(pdev);
7165 struct igb_adapter *adapter = netdev_priv(netdev);
7166 struct e1000_hw *hw = &adapter->hw;
7169 pci_set_power_state(pdev, PCI_D0);
7170 pci_restore_state(pdev);
7171 pci_save_state(pdev);
7173 err = pci_enable_device_mem(pdev);
7176 "igb: Cannot enable PCI device from suspend\n");
7179 pci_set_master(pdev);
7181 pci_enable_wake(pdev, PCI_D3hot, 0);
7182 pci_enable_wake(pdev, PCI_D3cold, 0);
7184 if (igb_init_interrupt_scheme(adapter, true)) {
7185 dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
7191 /* let the f/w know that the h/w is now under the control of the
7194 igb_get_hw_control(adapter);
7196 wr32(E1000_WUS, ~0);
7198 if (netdev->flags & IFF_UP) {
7200 err = __igb_open(netdev, true);
7206 netif_device_attach(netdev);
7210 #ifdef CONFIG_PM_RUNTIME
7211 static int igb_runtime_idle(struct device *dev)
7213 struct pci_dev *pdev = to_pci_dev(dev);
7214 struct net_device *netdev = pci_get_drvdata(pdev);
7215 struct igb_adapter *adapter = netdev_priv(netdev);
7217 if (!igb_has_link(adapter))
7218 pm_schedule_suspend(dev, MSEC_PER_SEC * 5);
7223 static int igb_runtime_suspend(struct device *dev)
7225 struct pci_dev *pdev = to_pci_dev(dev);
7229 retval = __igb_shutdown(pdev, &wake, 1);
7234 pci_prepare_to_sleep(pdev);
7236 pci_wake_from_d3(pdev, false);
7237 pci_set_power_state(pdev, PCI_D3hot);
7243 static int igb_runtime_resume(struct device *dev)
7245 return igb_resume(dev);
7247 #endif /* CONFIG_PM_RUNTIME */
7250 static void igb_shutdown(struct pci_dev *pdev)
7254 __igb_shutdown(pdev, &wake, 0);
7256 if (system_state == SYSTEM_POWER_OFF) {
7257 pci_wake_from_d3(pdev, wake);
7258 pci_set_power_state(pdev, PCI_D3hot);
7262 #ifdef CONFIG_PCI_IOV
7263 static int igb_sriov_reinit(struct pci_dev *dev)
7265 struct net_device *netdev = pci_get_drvdata(dev);
7266 struct igb_adapter *adapter = netdev_priv(netdev);
7267 struct pci_dev *pdev = adapter->pdev;
7271 if (netif_running(netdev))
7274 igb_clear_interrupt_scheme(adapter);
7276 igb_init_queue_configuration(adapter);
7278 if (igb_init_interrupt_scheme(adapter, true)) {
7279 dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
7283 if (netif_running(netdev))
7291 static int igb_pci_disable_sriov(struct pci_dev *dev)
7293 int err = igb_disable_sriov(dev);
7296 err = igb_sriov_reinit(dev);
7301 static int igb_pci_enable_sriov(struct pci_dev *dev, int num_vfs)
7303 int err = igb_enable_sriov(dev, num_vfs);
7308 err = igb_sriov_reinit(dev);
7317 static int igb_pci_sriov_configure(struct pci_dev *dev, int num_vfs)
7319 #ifdef CONFIG_PCI_IOV
7321 return igb_pci_disable_sriov(dev);
7323 return igb_pci_enable_sriov(dev, num_vfs);
7328 #ifdef CONFIG_NET_POLL_CONTROLLER
7329 /* Polling 'interrupt' - used by things like netconsole to send skbs
7330 * without having to re-enable interrupts. It's not called while
7331 * the interrupt routine is executing.
7333 static void igb_netpoll(struct net_device *netdev)
7335 struct igb_adapter *adapter = netdev_priv(netdev);
7336 struct e1000_hw *hw = &adapter->hw;
7337 struct igb_q_vector *q_vector;
7340 for (i = 0; i < adapter->num_q_vectors; i++) {
7341 q_vector = adapter->q_vector[i];
7342 if (adapter->msix_entries)
7343 wr32(E1000_EIMC, q_vector->eims_value);
7345 igb_irq_disable(adapter);
7346 napi_schedule(&q_vector->napi);
7349 #endif /* CONFIG_NET_POLL_CONTROLLER */
7352 * igb_io_error_detected - called when PCI error is detected
7353 * @pdev: Pointer to PCI device
7354 * @state: The current pci connection state
7356 * This function is called after a PCI bus error affecting
7357 * this device has been detected.
7359 static pci_ers_result_t igb_io_error_detected(struct pci_dev *pdev,
7360 pci_channel_state_t state)
7362 struct net_device *netdev = pci_get_drvdata(pdev);
7363 struct igb_adapter *adapter = netdev_priv(netdev);
7365 netif_device_detach(netdev);
7367 if (state == pci_channel_io_perm_failure)
7368 return PCI_ERS_RESULT_DISCONNECT;
7370 if (netif_running(netdev))
7372 pci_disable_device(pdev);
7374 /* Request a slot slot reset. */
7375 return PCI_ERS_RESULT_NEED_RESET;
7379 * igb_io_slot_reset - called after the pci bus has been reset.
7380 * @pdev: Pointer to PCI device
7382 * Restart the card from scratch, as if from a cold-boot. Implementation
7383 * resembles the first-half of the igb_resume routine.
7385 static pci_ers_result_t igb_io_slot_reset(struct pci_dev *pdev)
7387 struct net_device *netdev = pci_get_drvdata(pdev);
7388 struct igb_adapter *adapter = netdev_priv(netdev);
7389 struct e1000_hw *hw = &adapter->hw;
7390 pci_ers_result_t result;
7393 if (pci_enable_device_mem(pdev)) {
7395 "Cannot re-enable PCI device after reset.\n");
7396 result = PCI_ERS_RESULT_DISCONNECT;
7398 pci_set_master(pdev);
7399 pci_restore_state(pdev);
7400 pci_save_state(pdev);
7402 pci_enable_wake(pdev, PCI_D3hot, 0);
7403 pci_enable_wake(pdev, PCI_D3cold, 0);
7406 wr32(E1000_WUS, ~0);
7407 result = PCI_ERS_RESULT_RECOVERED;
7410 err = pci_cleanup_aer_uncorrect_error_status(pdev);
7413 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
7415 /* non-fatal, continue */
7422 * igb_io_resume - called when traffic can start flowing again.
7423 * @pdev: Pointer to PCI device
7425 * This callback is called when the error recovery driver tells us that
7426 * its OK to resume normal operation. Implementation resembles the
7427 * second-half of the igb_resume routine.
7429 static void igb_io_resume(struct pci_dev *pdev)
7431 struct net_device *netdev = pci_get_drvdata(pdev);
7432 struct igb_adapter *adapter = netdev_priv(netdev);
7434 if (netif_running(netdev)) {
7435 if (igb_up(adapter)) {
7436 dev_err(&pdev->dev, "igb_up failed after reset\n");
7441 netif_device_attach(netdev);
7443 /* let the f/w know that the h/w is now under the control of the
7446 igb_get_hw_control(adapter);
7449 static void igb_rar_set_qsel(struct igb_adapter *adapter, u8 *addr, u32 index,
7452 u32 rar_low, rar_high;
7453 struct e1000_hw *hw = &adapter->hw;
7455 /* HW expects these in little endian so we reverse the byte order
7456 * from network order (big endian) to little endian
7458 rar_low = ((u32) addr[0] | ((u32) addr[1] << 8) |
7459 ((u32) addr[2] << 16) | ((u32) addr[3] << 24));
7460 rar_high = ((u32) addr[4] | ((u32) addr[5] << 8));
7462 /* Indicate to hardware the Address is Valid. */
7463 rar_high |= E1000_RAH_AV;
7465 if (hw->mac.type == e1000_82575)
7466 rar_high |= E1000_RAH_POOL_1 * qsel;
7468 rar_high |= E1000_RAH_POOL_1 << qsel;
7470 wr32(E1000_RAL(index), rar_low);
7472 wr32(E1000_RAH(index), rar_high);
7476 static int igb_set_vf_mac(struct igb_adapter *adapter,
7477 int vf, unsigned char *mac_addr)
7479 struct e1000_hw *hw = &adapter->hw;
7480 /* VF MAC addresses start at end of receive addresses and moves
7481 * towards the first, as a result a collision should not be possible
7483 int rar_entry = hw->mac.rar_entry_count - (vf + 1);
7485 memcpy(adapter->vf_data[vf].vf_mac_addresses, mac_addr, ETH_ALEN);
7487 igb_rar_set_qsel(adapter, mac_addr, rar_entry, vf);
7492 static int igb_ndo_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
7494 struct igb_adapter *adapter = netdev_priv(netdev);
7495 if (!is_valid_ether_addr(mac) || (vf >= adapter->vfs_allocated_count))
7497 adapter->vf_data[vf].flags |= IGB_VF_FLAG_PF_SET_MAC;
7498 dev_info(&adapter->pdev->dev, "setting MAC %pM on VF %d\n", mac, vf);
7499 dev_info(&adapter->pdev->dev,
7500 "Reload the VF driver to make this change effective.");
7501 if (test_bit(__IGB_DOWN, &adapter->state)) {
7502 dev_warn(&adapter->pdev->dev,
7503 "The VF MAC address has been set, but the PF device is not up.\n");
7504 dev_warn(&adapter->pdev->dev,
7505 "Bring the PF device up before attempting to use the VF device.\n");
7507 return igb_set_vf_mac(adapter, vf, mac);
7510 static int igb_link_mbps(int internal_link_speed)
7512 switch (internal_link_speed) {
7522 static void igb_set_vf_rate_limit(struct e1000_hw *hw, int vf, int tx_rate,
7529 /* Calculate the rate factor values to set */
7530 rf_int = link_speed / tx_rate;
7531 rf_dec = (link_speed - (rf_int * tx_rate));
7532 rf_dec = (rf_dec * (1 << E1000_RTTBCNRC_RF_INT_SHIFT)) /
7535 bcnrc_val = E1000_RTTBCNRC_RS_ENA;
7536 bcnrc_val |= ((rf_int << E1000_RTTBCNRC_RF_INT_SHIFT) &
7537 E1000_RTTBCNRC_RF_INT_MASK);
7538 bcnrc_val |= (rf_dec & E1000_RTTBCNRC_RF_DEC_MASK);
7543 wr32(E1000_RTTDQSEL, vf); /* vf X uses queue X */
7544 /* Set global transmit compensation time to the MMW_SIZE in RTTBCNRM
7545 * register. MMW_SIZE=0x014 if 9728-byte jumbo is supported.
7547 wr32(E1000_RTTBCNRM, 0x14);
7548 wr32(E1000_RTTBCNRC, bcnrc_val);
7551 static void igb_check_vf_rate_limit(struct igb_adapter *adapter)
7553 int actual_link_speed, i;
7554 bool reset_rate = false;
7556 /* VF TX rate limit was not set or not supported */
7557 if ((adapter->vf_rate_link_speed == 0) ||
7558 (adapter->hw.mac.type != e1000_82576))
7561 actual_link_speed = igb_link_mbps(adapter->link_speed);
7562 if (actual_link_speed != adapter->vf_rate_link_speed) {
7564 adapter->vf_rate_link_speed = 0;
7565 dev_info(&adapter->pdev->dev,
7566 "Link speed has been changed. VF Transmit rate is disabled\n");
7569 for (i = 0; i < adapter->vfs_allocated_count; i++) {
7571 adapter->vf_data[i].tx_rate = 0;
7573 igb_set_vf_rate_limit(&adapter->hw, i,
7574 adapter->vf_data[i].tx_rate,
7579 static int igb_ndo_set_vf_bw(struct net_device *netdev, int vf, int tx_rate)
7581 struct igb_adapter *adapter = netdev_priv(netdev);
7582 struct e1000_hw *hw = &adapter->hw;
7583 int actual_link_speed;
7585 if (hw->mac.type != e1000_82576)
7588 actual_link_speed = igb_link_mbps(adapter->link_speed);
7589 if ((vf >= adapter->vfs_allocated_count) ||
7590 (!(rd32(E1000_STATUS) & E1000_STATUS_LU)) ||
7591 (tx_rate < 0) || (tx_rate > actual_link_speed))
7594 adapter->vf_rate_link_speed = actual_link_speed;
7595 adapter->vf_data[vf].tx_rate = (u16)tx_rate;
7596 igb_set_vf_rate_limit(hw, vf, tx_rate, actual_link_speed);
7601 static int igb_ndo_set_vf_spoofchk(struct net_device *netdev, int vf,
7604 struct igb_adapter *adapter = netdev_priv(netdev);
7605 struct e1000_hw *hw = &adapter->hw;
7606 u32 reg_val, reg_offset;
7608 if (!adapter->vfs_allocated_count)
7611 if (vf >= adapter->vfs_allocated_count)
7614 reg_offset = (hw->mac.type == e1000_82576) ? E1000_DTXSWC : E1000_TXSWC;
7615 reg_val = rd32(reg_offset);
7617 reg_val |= ((1 << vf) |
7618 (1 << (vf + E1000_DTXSWC_VLAN_SPOOF_SHIFT)));
7620 reg_val &= ~((1 << vf) |
7621 (1 << (vf + E1000_DTXSWC_VLAN_SPOOF_SHIFT)));
7622 wr32(reg_offset, reg_val);
7624 adapter->vf_data[vf].spoofchk_enabled = setting;
7625 return E1000_SUCCESS;
7628 static int igb_ndo_get_vf_config(struct net_device *netdev,
7629 int vf, struct ifla_vf_info *ivi)
7631 struct igb_adapter *adapter = netdev_priv(netdev);
7632 if (vf >= adapter->vfs_allocated_count)
7635 memcpy(&ivi->mac, adapter->vf_data[vf].vf_mac_addresses, ETH_ALEN);
7636 ivi->tx_rate = adapter->vf_data[vf].tx_rate;
7637 ivi->vlan = adapter->vf_data[vf].pf_vlan;
7638 ivi->qos = adapter->vf_data[vf].pf_qos;
7639 ivi->spoofchk = adapter->vf_data[vf].spoofchk_enabled;
7643 static void igb_vmm_control(struct igb_adapter *adapter)
7645 struct e1000_hw *hw = &adapter->hw;
7648 switch (hw->mac.type) {
7654 /* replication is not supported for 82575 */
7657 /* notify HW that the MAC is adding vlan tags */
7658 reg = rd32(E1000_DTXCTL);
7659 reg |= E1000_DTXCTL_VLAN_ADDED;
7660 wr32(E1000_DTXCTL, reg);
7662 /* enable replication vlan tag stripping */
7663 reg = rd32(E1000_RPLOLR);
7664 reg |= E1000_RPLOLR_STRVLAN;
7665 wr32(E1000_RPLOLR, reg);
7667 /* none of the above registers are supported by i350 */
7671 if (adapter->vfs_allocated_count) {
7672 igb_vmdq_set_loopback_pf(hw, true);
7673 igb_vmdq_set_replication_pf(hw, true);
7674 igb_vmdq_set_anti_spoofing_pf(hw, true,
7675 adapter->vfs_allocated_count);
7677 igb_vmdq_set_loopback_pf(hw, false);
7678 igb_vmdq_set_replication_pf(hw, false);
7682 static void igb_init_dmac(struct igb_adapter *adapter, u32 pba)
7684 struct e1000_hw *hw = &adapter->hw;
7688 if (hw->mac.type > e1000_82580) {
7689 if (adapter->flags & IGB_FLAG_DMAC) {
7692 /* force threshold to 0. */
7693 wr32(E1000_DMCTXTH, 0);
7695 /* DMA Coalescing high water mark needs to be greater
7696 * than the Rx threshold. Set hwm to PBA - max frame
7697 * size in 16B units, capping it at PBA - 6KB.
7699 hwm = 64 * pba - adapter->max_frame_size / 16;
7700 if (hwm < 64 * (pba - 6))
7701 hwm = 64 * (pba - 6);
7702 reg = rd32(E1000_FCRTC);
7703 reg &= ~E1000_FCRTC_RTH_COAL_MASK;
7704 reg |= ((hwm << E1000_FCRTC_RTH_COAL_SHIFT)
7705 & E1000_FCRTC_RTH_COAL_MASK);
7706 wr32(E1000_FCRTC, reg);
7708 /* Set the DMA Coalescing Rx threshold to PBA - 2 * max
7709 * frame size, capping it at PBA - 10KB.
7711 dmac_thr = pba - adapter->max_frame_size / 512;
7712 if (dmac_thr < pba - 10)
7713 dmac_thr = pba - 10;
7714 reg = rd32(E1000_DMACR);
7715 reg &= ~E1000_DMACR_DMACTHR_MASK;
7716 reg |= ((dmac_thr << E1000_DMACR_DMACTHR_SHIFT)
7717 & E1000_DMACR_DMACTHR_MASK);
7719 /* transition to L0x or L1 if available..*/
7720 reg |= (E1000_DMACR_DMAC_EN | E1000_DMACR_DMAC_LX_MASK);
7722 /* watchdog timer= +-1000 usec in 32usec intervals */
7725 /* Disable BMC-to-OS Watchdog Enable */
7726 if (hw->mac.type != e1000_i354)
7727 reg &= ~E1000_DMACR_DC_BMC2OSW_EN;
7729 wr32(E1000_DMACR, reg);
7731 /* no lower threshold to disable
7732 * coalescing(smart fifb)-UTRESH=0
7734 wr32(E1000_DMCRTRH, 0);
7736 reg = (IGB_DMCTLX_DCFLUSH_DIS | 0x4);
7738 wr32(E1000_DMCTLX, reg);
7740 /* free space in tx packet buffer to wake from
7743 wr32(E1000_DMCTXTH, (IGB_MIN_TXPBSIZE -
7744 (IGB_TX_BUF_4096 + adapter->max_frame_size)) >> 6);
7746 /* make low power state decision controlled
7749 reg = rd32(E1000_PCIEMISC);
7750 reg &= ~E1000_PCIEMISC_LX_DECISION;
7751 wr32(E1000_PCIEMISC, reg);
7752 } /* endif adapter->dmac is not disabled */
7753 } else if (hw->mac.type == e1000_82580) {
7754 u32 reg = rd32(E1000_PCIEMISC);
7755 wr32(E1000_PCIEMISC, reg & ~E1000_PCIEMISC_LX_DECISION);
7756 wr32(E1000_DMACR, 0);
7761 * igb_read_i2c_byte - Reads 8 bit word over I2C
7762 * @hw: pointer to hardware structure
7763 * @byte_offset: byte offset to read
7764 * @dev_addr: device address
7767 * Performs byte read operation over I2C interface at
7768 * a specified device address.
7770 s32 igb_read_i2c_byte(struct e1000_hw *hw, u8 byte_offset,
7771 u8 dev_addr, u8 *data)
7773 struct igb_adapter *adapter = container_of(hw, struct igb_adapter, hw);
7774 struct i2c_client *this_client = adapter->i2c_client;
7779 return E1000_ERR_I2C;
7781 swfw_mask = E1000_SWFW_PHY0_SM;
7783 if (hw->mac.ops.acquire_swfw_sync(hw, swfw_mask)
7785 return E1000_ERR_SWFW_SYNC;
7787 status = i2c_smbus_read_byte_data(this_client, byte_offset);
7788 hw->mac.ops.release_swfw_sync(hw, swfw_mask);
7791 return E1000_ERR_I2C;
7794 return E1000_SUCCESS;
7799 * igb_write_i2c_byte - Writes 8 bit word over I2C
7800 * @hw: pointer to hardware structure
7801 * @byte_offset: byte offset to write
7802 * @dev_addr: device address
7803 * @data: value to write
7805 * Performs byte write operation over I2C interface at
7806 * a specified device address.
7808 s32 igb_write_i2c_byte(struct e1000_hw *hw, u8 byte_offset,
7809 u8 dev_addr, u8 data)
7811 struct igb_adapter *adapter = container_of(hw, struct igb_adapter, hw);
7812 struct i2c_client *this_client = adapter->i2c_client;
7814 u16 swfw_mask = E1000_SWFW_PHY0_SM;
7817 return E1000_ERR_I2C;
7819 if (hw->mac.ops.acquire_swfw_sync(hw, swfw_mask) != E1000_SUCCESS)
7820 return E1000_ERR_SWFW_SYNC;
7821 status = i2c_smbus_write_byte_data(this_client, byte_offset, data);
7822 hw->mac.ops.release_swfw_sync(hw, swfw_mask);
7825 return E1000_ERR_I2C;
7827 return E1000_SUCCESS;
7831 int igb_reinit_queues(struct igb_adapter *adapter)
7833 struct net_device *netdev = adapter->netdev;
7834 struct pci_dev *pdev = adapter->pdev;
7837 if (netif_running(netdev))
7840 igb_clear_interrupt_scheme(adapter);
7842 if (igb_init_interrupt_scheme(adapter, true)) {
7843 dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
7847 if (netif_running(netdev))
7848 err = igb_open(netdev);