1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2007 - 2018 Intel Corporation. */
4 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
6 #include <linux/module.h>
7 #include <linux/types.h>
8 #include <linux/init.h>
9 #include <linux/bitops.h>
10 #include <linux/vmalloc.h>
11 #include <linux/pagemap.h>
12 #include <linux/netdevice.h>
13 #include <linux/ipv6.h>
14 #include <linux/slab.h>
15 #include <net/checksum.h>
16 #include <net/ip6_checksum.h>
17 #include <net/pkt_sched.h>
18 #include <net/pkt_cls.h>
19 #include <linux/net_tstamp.h>
20 #include <linux/mii.h>
21 #include <linux/ethtool.h>
23 #include <linux/if_vlan.h>
24 #include <linux/pci.h>
25 #include <linux/delay.h>
26 #include <linux/interrupt.h>
28 #include <linux/tcp.h>
29 #include <linux/sctp.h>
30 #include <linux/if_ether.h>
31 #include <linux/aer.h>
32 #include <linux/prefetch.h>
33 #include <linux/bpf.h>
34 #include <linux/bpf_trace.h>
35 #include <linux/pm_runtime.h>
36 #include <linux/etherdevice.h>
38 #include <linux/dca.h>
40 #include <linux/i2c.h>
44 QUEUE_MODE_STRICT_PRIORITY,
45 QUEUE_MODE_STREAM_RESERVATION,
53 char igb_driver_name[] = "igb";
54 static const char igb_driver_string[] =
55 "Intel(R) Gigabit Ethernet Network Driver";
56 static const char igb_copyright[] =
57 "Copyright (c) 2007-2014 Intel Corporation.";
59 static const struct e1000_info *igb_info_tbl[] = {
60 [board_82575] = &e1000_82575_info,
63 static const struct pci_device_id igb_pci_tbl[] = {
64 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I354_BACKPLANE_1GBPS) },
65 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I354_SGMII) },
66 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I354_BACKPLANE_2_5GBPS) },
67 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I211_COPPER), board_82575 },
68 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I210_COPPER), board_82575 },
69 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I210_FIBER), board_82575 },
70 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I210_SERDES), board_82575 },
71 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I210_SGMII), board_82575 },
72 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I210_COPPER_FLASHLESS), board_82575 },
73 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I210_SERDES_FLASHLESS), board_82575 },
74 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I350_COPPER), board_82575 },
75 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I350_FIBER), board_82575 },
76 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I350_SERDES), board_82575 },
77 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I350_SGMII), board_82575 },
78 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_COPPER), board_82575 },
79 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_FIBER), board_82575 },
80 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_QUAD_FIBER), board_82575 },
81 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_SERDES), board_82575 },
82 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_SGMII), board_82575 },
83 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_COPPER_DUAL), board_82575 },
84 { PCI_VDEVICE(INTEL, E1000_DEV_ID_DH89XXCC_SGMII), board_82575 },
85 { PCI_VDEVICE(INTEL, E1000_DEV_ID_DH89XXCC_SERDES), board_82575 },
86 { PCI_VDEVICE(INTEL, E1000_DEV_ID_DH89XXCC_BACKPLANE), board_82575 },
87 { PCI_VDEVICE(INTEL, E1000_DEV_ID_DH89XXCC_SFP), board_82575 },
88 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576), board_82575 },
89 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_NS), board_82575 },
90 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_NS_SERDES), board_82575 },
91 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_FIBER), board_82575 },
92 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_SERDES), board_82575 },
93 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_SERDES_QUAD), board_82575 },
94 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_QUAD_COPPER_ET2), board_82575 },
95 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_QUAD_COPPER), board_82575 },
96 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82575EB_COPPER), board_82575 },
97 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82575EB_FIBER_SERDES), board_82575 },
98 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82575GB_QUAD_COPPER), board_82575 },
99 /* required last entry */
103 MODULE_DEVICE_TABLE(pci, igb_pci_tbl);
105 static int igb_setup_all_tx_resources(struct igb_adapter *);
106 static int igb_setup_all_rx_resources(struct igb_adapter *);
107 static void igb_free_all_tx_resources(struct igb_adapter *);
108 static void igb_free_all_rx_resources(struct igb_adapter *);
109 static void igb_setup_mrqc(struct igb_adapter *);
110 static int igb_probe(struct pci_dev *, const struct pci_device_id *);
111 static void igb_remove(struct pci_dev *pdev);
112 static int igb_sw_init(struct igb_adapter *);
113 int igb_open(struct net_device *);
114 int igb_close(struct net_device *);
115 static void igb_configure(struct igb_adapter *);
116 static void igb_configure_tx(struct igb_adapter *);
117 static void igb_configure_rx(struct igb_adapter *);
118 static void igb_clean_all_tx_rings(struct igb_adapter *);
119 static void igb_clean_all_rx_rings(struct igb_adapter *);
120 static void igb_clean_tx_ring(struct igb_ring *);
121 static void igb_clean_rx_ring(struct igb_ring *);
122 static void igb_set_rx_mode(struct net_device *);
123 static void igb_update_phy_info(struct timer_list *);
124 static void igb_watchdog(struct timer_list *);
125 static void igb_watchdog_task(struct work_struct *);
126 static netdev_tx_t igb_xmit_frame(struct sk_buff *skb, struct net_device *);
127 static void igb_get_stats64(struct net_device *dev,
128 struct rtnl_link_stats64 *stats);
129 static int igb_change_mtu(struct net_device *, int);
130 static int igb_set_mac(struct net_device *, void *);
131 static void igb_set_uta(struct igb_adapter *adapter, bool set);
132 static irqreturn_t igb_intr(int irq, void *);
133 static irqreturn_t igb_intr_msi(int irq, void *);
134 static irqreturn_t igb_msix_other(int irq, void *);
135 static irqreturn_t igb_msix_ring(int irq, void *);
136 #ifdef CONFIG_IGB_DCA
137 static void igb_update_dca(struct igb_q_vector *);
138 static void igb_setup_dca(struct igb_adapter *);
139 #endif /* CONFIG_IGB_DCA */
140 static int igb_poll(struct napi_struct *, int);
141 static bool igb_clean_tx_irq(struct igb_q_vector *, int);
142 static int igb_clean_rx_irq(struct igb_q_vector *, int);
143 static int igb_ioctl(struct net_device *, struct ifreq *, int cmd);
144 static void igb_tx_timeout(struct net_device *, unsigned int txqueue);
145 static void igb_reset_task(struct work_struct *);
146 static void igb_vlan_mode(struct net_device *netdev,
147 netdev_features_t features);
148 static int igb_vlan_rx_add_vid(struct net_device *, __be16, u16);
149 static int igb_vlan_rx_kill_vid(struct net_device *, __be16, u16);
150 static void igb_restore_vlan(struct igb_adapter *);
151 static void igb_rar_set_index(struct igb_adapter *, u32);
152 static void igb_ping_all_vfs(struct igb_adapter *);
153 static void igb_msg_task(struct igb_adapter *);
154 static void igb_vmm_control(struct igb_adapter *);
155 static int igb_set_vf_mac(struct igb_adapter *, int, unsigned char *);
156 static void igb_flush_mac_table(struct igb_adapter *);
157 static int igb_available_rars(struct igb_adapter *, u8);
158 static void igb_set_default_mac_filter(struct igb_adapter *);
159 static int igb_uc_sync(struct net_device *, const unsigned char *);
160 static int igb_uc_unsync(struct net_device *, const unsigned char *);
161 static void igb_restore_vf_multicasts(struct igb_adapter *adapter);
162 static int igb_ndo_set_vf_mac(struct net_device *netdev, int vf, u8 *mac);
163 static int igb_ndo_set_vf_vlan(struct net_device *netdev,
164 int vf, u16 vlan, u8 qos, __be16 vlan_proto);
165 static int igb_ndo_set_vf_bw(struct net_device *, int, int, int);
166 static int igb_ndo_set_vf_spoofchk(struct net_device *netdev, int vf,
168 static int igb_ndo_set_vf_trust(struct net_device *netdev, int vf,
170 static int igb_ndo_get_vf_config(struct net_device *netdev, int vf,
171 struct ifla_vf_info *ivi);
172 static void igb_check_vf_rate_limit(struct igb_adapter *);
173 static void igb_nfc_filter_exit(struct igb_adapter *adapter);
174 static void igb_nfc_filter_restore(struct igb_adapter *adapter);
176 #ifdef CONFIG_PCI_IOV
177 static int igb_vf_configure(struct igb_adapter *adapter, int vf);
178 static int igb_pci_enable_sriov(struct pci_dev *dev, int num_vfs);
179 static int igb_disable_sriov(struct pci_dev *dev);
180 static int igb_pci_disable_sriov(struct pci_dev *dev);
183 static int igb_suspend(struct device *);
184 static int igb_resume(struct device *);
185 static int igb_runtime_suspend(struct device *dev);
186 static int igb_runtime_resume(struct device *dev);
187 static int igb_runtime_idle(struct device *dev);
188 static const struct dev_pm_ops igb_pm_ops = {
189 SET_SYSTEM_SLEEP_PM_OPS(igb_suspend, igb_resume)
190 SET_RUNTIME_PM_OPS(igb_runtime_suspend, igb_runtime_resume,
193 static void igb_shutdown(struct pci_dev *);
194 static int igb_pci_sriov_configure(struct pci_dev *dev, int num_vfs);
195 #ifdef CONFIG_IGB_DCA
196 static int igb_notify_dca(struct notifier_block *, unsigned long, void *);
197 static struct notifier_block dca_notifier = {
198 .notifier_call = igb_notify_dca,
203 #ifdef CONFIG_PCI_IOV
204 static unsigned int max_vfs;
205 module_param(max_vfs, uint, 0);
206 MODULE_PARM_DESC(max_vfs, "Maximum number of virtual functions to allocate per physical function");
207 #endif /* CONFIG_PCI_IOV */
209 static pci_ers_result_t igb_io_error_detected(struct pci_dev *,
210 pci_channel_state_t);
211 static pci_ers_result_t igb_io_slot_reset(struct pci_dev *);
212 static void igb_io_resume(struct pci_dev *);
214 static const struct pci_error_handlers igb_err_handler = {
215 .error_detected = igb_io_error_detected,
216 .slot_reset = igb_io_slot_reset,
217 .resume = igb_io_resume,
220 static void igb_init_dmac(struct igb_adapter *adapter, u32 pba);
222 static struct pci_driver igb_driver = {
223 .name = igb_driver_name,
224 .id_table = igb_pci_tbl,
226 .remove = igb_remove,
228 .driver.pm = &igb_pm_ops,
230 .shutdown = igb_shutdown,
231 .sriov_configure = igb_pci_sriov_configure,
232 .err_handler = &igb_err_handler
236 MODULE_DESCRIPTION("Intel(R) Gigabit Ethernet Network Driver");
237 MODULE_LICENSE("GPL v2");
239 #define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK)
240 static int debug = -1;
241 module_param(debug, int, 0);
242 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
244 struct igb_reg_info {
249 static const struct igb_reg_info igb_reg_info_tbl[] = {
251 /* General Registers */
252 {E1000_CTRL, "CTRL"},
253 {E1000_STATUS, "STATUS"},
254 {E1000_CTRL_EXT, "CTRL_EXT"},
256 /* Interrupt Registers */
260 {E1000_RCTL, "RCTL"},
261 {E1000_RDLEN(0), "RDLEN"},
262 {E1000_RDH(0), "RDH"},
263 {E1000_RDT(0), "RDT"},
264 {E1000_RXDCTL(0), "RXDCTL"},
265 {E1000_RDBAL(0), "RDBAL"},
266 {E1000_RDBAH(0), "RDBAH"},
269 {E1000_TCTL, "TCTL"},
270 {E1000_TDBAL(0), "TDBAL"},
271 {E1000_TDBAH(0), "TDBAH"},
272 {E1000_TDLEN(0), "TDLEN"},
273 {E1000_TDH(0), "TDH"},
274 {E1000_TDT(0), "TDT"},
275 {E1000_TXDCTL(0), "TXDCTL"},
276 {E1000_TDFH, "TDFH"},
277 {E1000_TDFT, "TDFT"},
278 {E1000_TDFHS, "TDFHS"},
279 {E1000_TDFPC, "TDFPC"},
281 /* List Terminator */
285 /* igb_regdump - register printout routine */
286 static void igb_regdump(struct e1000_hw *hw, struct igb_reg_info *reginfo)
292 switch (reginfo->ofs) {
294 for (n = 0; n < 4; n++)
295 regs[n] = rd32(E1000_RDLEN(n));
298 for (n = 0; n < 4; n++)
299 regs[n] = rd32(E1000_RDH(n));
302 for (n = 0; n < 4; n++)
303 regs[n] = rd32(E1000_RDT(n));
305 case E1000_RXDCTL(0):
306 for (n = 0; n < 4; n++)
307 regs[n] = rd32(E1000_RXDCTL(n));
310 for (n = 0; n < 4; n++)
311 regs[n] = rd32(E1000_RDBAL(n));
314 for (n = 0; n < 4; n++)
315 regs[n] = rd32(E1000_RDBAH(n));
318 for (n = 0; n < 4; n++)
319 regs[n] = rd32(E1000_TDBAL(n));
322 for (n = 0; n < 4; n++)
323 regs[n] = rd32(E1000_TDBAH(n));
326 for (n = 0; n < 4; n++)
327 regs[n] = rd32(E1000_TDLEN(n));
330 for (n = 0; n < 4; n++)
331 regs[n] = rd32(E1000_TDH(n));
334 for (n = 0; n < 4; n++)
335 regs[n] = rd32(E1000_TDT(n));
337 case E1000_TXDCTL(0):
338 for (n = 0; n < 4; n++)
339 regs[n] = rd32(E1000_TXDCTL(n));
342 pr_info("%-15s %08x\n", reginfo->name, rd32(reginfo->ofs));
346 snprintf(rname, 16, "%s%s", reginfo->name, "[0-3]");
347 pr_info("%-15s %08x %08x %08x %08x\n", rname, regs[0], regs[1],
351 /* igb_dump - Print registers, Tx-rings and Rx-rings */
352 static void igb_dump(struct igb_adapter *adapter)
354 struct net_device *netdev = adapter->netdev;
355 struct e1000_hw *hw = &adapter->hw;
356 struct igb_reg_info *reginfo;
357 struct igb_ring *tx_ring;
358 union e1000_adv_tx_desc *tx_desc;
359 struct my_u0 { __le64 a; __le64 b; } *u0;
360 struct igb_ring *rx_ring;
361 union e1000_adv_rx_desc *rx_desc;
365 if (!netif_msg_hw(adapter))
368 /* Print netdevice Info */
370 dev_info(&adapter->pdev->dev, "Net device Info\n");
371 pr_info("Device Name state trans_start\n");
372 pr_info("%-15s %016lX %016lX\n", netdev->name,
373 netdev->state, dev_trans_start(netdev));
376 /* Print Registers */
377 dev_info(&adapter->pdev->dev, "Register Dump\n");
378 pr_info(" Register Name Value\n");
379 for (reginfo = (struct igb_reg_info *)igb_reg_info_tbl;
380 reginfo->name; reginfo++) {
381 igb_regdump(hw, reginfo);
384 /* Print TX Ring Summary */
385 if (!netdev || !netif_running(netdev))
388 dev_info(&adapter->pdev->dev, "TX Rings Summary\n");
389 pr_info("Queue [NTU] [NTC] [bi(ntc)->dma ] leng ntw timestamp\n");
390 for (n = 0; n < adapter->num_tx_queues; n++) {
391 struct igb_tx_buffer *buffer_info;
392 tx_ring = adapter->tx_ring[n];
393 buffer_info = &tx_ring->tx_buffer_info[tx_ring->next_to_clean];
394 pr_info(" %5d %5X %5X %016llX %04X %p %016llX\n",
395 n, tx_ring->next_to_use, tx_ring->next_to_clean,
396 (u64)dma_unmap_addr(buffer_info, dma),
397 dma_unmap_len(buffer_info, len),
398 buffer_info->next_to_watch,
399 (u64)buffer_info->time_stamp);
403 if (!netif_msg_tx_done(adapter))
404 goto rx_ring_summary;
406 dev_info(&adapter->pdev->dev, "TX Rings Dump\n");
408 /* Transmit Descriptor Formats
410 * Advanced Transmit Descriptor
411 * +--------------------------------------------------------------+
412 * 0 | Buffer Address [63:0] |
413 * +--------------------------------------------------------------+
414 * 8 | PAYLEN | PORTS |CC|IDX | STA | DCMD |DTYP|MAC|RSV| DTALEN |
415 * +--------------------------------------------------------------+
416 * 63 46 45 40 39 38 36 35 32 31 24 15 0
419 for (n = 0; n < adapter->num_tx_queues; n++) {
420 tx_ring = adapter->tx_ring[n];
421 pr_info("------------------------------------\n");
422 pr_info("TX QUEUE INDEX = %d\n", tx_ring->queue_index);
423 pr_info("------------------------------------\n");
424 pr_info("T [desc] [address 63:0 ] [PlPOCIStDDM Ln] [bi->dma ] leng ntw timestamp bi->skb\n");
426 for (i = 0; tx_ring->desc && (i < tx_ring->count); i++) {
427 const char *next_desc;
428 struct igb_tx_buffer *buffer_info;
429 tx_desc = IGB_TX_DESC(tx_ring, i);
430 buffer_info = &tx_ring->tx_buffer_info[i];
431 u0 = (struct my_u0 *)tx_desc;
432 if (i == tx_ring->next_to_use &&
433 i == tx_ring->next_to_clean)
434 next_desc = " NTC/U";
435 else if (i == tx_ring->next_to_use)
437 else if (i == tx_ring->next_to_clean)
442 pr_info("T [0x%03X] %016llX %016llX %016llX %04X %p %016llX %p%s\n",
443 i, le64_to_cpu(u0->a),
445 (u64)dma_unmap_addr(buffer_info, dma),
446 dma_unmap_len(buffer_info, len),
447 buffer_info->next_to_watch,
448 (u64)buffer_info->time_stamp,
449 buffer_info->skb, next_desc);
451 if (netif_msg_pktdata(adapter) && buffer_info->skb)
452 print_hex_dump(KERN_INFO, "",
454 16, 1, buffer_info->skb->data,
455 dma_unmap_len(buffer_info, len),
460 /* Print RX Rings Summary */
462 dev_info(&adapter->pdev->dev, "RX Rings Summary\n");
463 pr_info("Queue [NTU] [NTC]\n");
464 for (n = 0; n < adapter->num_rx_queues; n++) {
465 rx_ring = adapter->rx_ring[n];
466 pr_info(" %5d %5X %5X\n",
467 n, rx_ring->next_to_use, rx_ring->next_to_clean);
471 if (!netif_msg_rx_status(adapter))
474 dev_info(&adapter->pdev->dev, "RX Rings Dump\n");
476 /* Advanced Receive Descriptor (Read) Format
478 * +-----------------------------------------------------+
479 * 0 | Packet Buffer Address [63:1] |A0/NSE|
480 * +----------------------------------------------+------+
481 * 8 | Header Buffer Address [63:1] | DD |
482 * +-----------------------------------------------------+
485 * Advanced Receive Descriptor (Write-Back) Format
487 * 63 48 47 32 31 30 21 20 17 16 4 3 0
488 * +------------------------------------------------------+
489 * 0 | Packet IP |SPH| HDR_LEN | RSV|Packet| RSS |
490 * | Checksum Ident | | | | Type | Type |
491 * +------------------------------------------------------+
492 * 8 | VLAN Tag | Length | Extended Error | Extended Status |
493 * +------------------------------------------------------+
494 * 63 48 47 32 31 20 19 0
497 for (n = 0; n < adapter->num_rx_queues; n++) {
498 rx_ring = adapter->rx_ring[n];
499 pr_info("------------------------------------\n");
500 pr_info("RX QUEUE INDEX = %d\n", rx_ring->queue_index);
501 pr_info("------------------------------------\n");
502 pr_info("R [desc] [ PktBuf A0] [ HeadBuf DD] [bi->dma ] [bi->skb] <-- Adv Rx Read format\n");
503 pr_info("RWB[desc] [PcsmIpSHl PtRs] [vl er S cks ln] ---------------- [bi->skb] <-- Adv Rx Write-Back format\n");
505 for (i = 0; i < rx_ring->count; i++) {
506 const char *next_desc;
507 struct igb_rx_buffer *buffer_info;
508 buffer_info = &rx_ring->rx_buffer_info[i];
509 rx_desc = IGB_RX_DESC(rx_ring, i);
510 u0 = (struct my_u0 *)rx_desc;
511 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
513 if (i == rx_ring->next_to_use)
515 else if (i == rx_ring->next_to_clean)
520 if (staterr & E1000_RXD_STAT_DD) {
521 /* Descriptor Done */
522 pr_info("%s[0x%03X] %016llX %016llX ---------------- %s\n",
528 pr_info("%s[0x%03X] %016llX %016llX %016llX %s\n",
532 (u64)buffer_info->dma,
535 if (netif_msg_pktdata(adapter) &&
536 buffer_info->dma && buffer_info->page) {
537 print_hex_dump(KERN_INFO, "",
540 page_address(buffer_info->page) +
541 buffer_info->page_offset,
542 igb_rx_bufsz(rx_ring), true);
553 * igb_get_i2c_data - Reads the I2C SDA data bit
554 * @data: opaque pointer to adapter struct
556 * Returns the I2C data bit value
558 static int igb_get_i2c_data(void *data)
560 struct igb_adapter *adapter = (struct igb_adapter *)data;
561 struct e1000_hw *hw = &adapter->hw;
562 s32 i2cctl = rd32(E1000_I2CPARAMS);
564 return !!(i2cctl & E1000_I2C_DATA_IN);
568 * igb_set_i2c_data - Sets the I2C data bit
569 * @data: pointer to hardware structure
570 * @state: I2C data value (0 or 1) to set
572 * Sets the I2C data bit
574 static void igb_set_i2c_data(void *data, int state)
576 struct igb_adapter *adapter = (struct igb_adapter *)data;
577 struct e1000_hw *hw = &adapter->hw;
578 s32 i2cctl = rd32(E1000_I2CPARAMS);
581 i2cctl |= E1000_I2C_DATA_OUT;
583 i2cctl &= ~E1000_I2C_DATA_OUT;
585 i2cctl &= ~E1000_I2C_DATA_OE_N;
586 i2cctl |= E1000_I2C_CLK_OE_N;
587 wr32(E1000_I2CPARAMS, i2cctl);
593 * igb_set_i2c_clk - Sets the I2C SCL clock
594 * @data: pointer to hardware structure
595 * @state: state to set clock
597 * Sets the I2C clock line to state
599 static void igb_set_i2c_clk(void *data, int state)
601 struct igb_adapter *adapter = (struct igb_adapter *)data;
602 struct e1000_hw *hw = &adapter->hw;
603 s32 i2cctl = rd32(E1000_I2CPARAMS);
606 i2cctl |= E1000_I2C_CLK_OUT;
607 i2cctl &= ~E1000_I2C_CLK_OE_N;
609 i2cctl &= ~E1000_I2C_CLK_OUT;
610 i2cctl &= ~E1000_I2C_CLK_OE_N;
612 wr32(E1000_I2CPARAMS, i2cctl);
617 * igb_get_i2c_clk - Gets the I2C SCL clock state
618 * @data: pointer to hardware structure
620 * Gets the I2C clock state
622 static int igb_get_i2c_clk(void *data)
624 struct igb_adapter *adapter = (struct igb_adapter *)data;
625 struct e1000_hw *hw = &adapter->hw;
626 s32 i2cctl = rd32(E1000_I2CPARAMS);
628 return !!(i2cctl & E1000_I2C_CLK_IN);
631 static const struct i2c_algo_bit_data igb_i2c_algo = {
632 .setsda = igb_set_i2c_data,
633 .setscl = igb_set_i2c_clk,
634 .getsda = igb_get_i2c_data,
635 .getscl = igb_get_i2c_clk,
641 * igb_get_hw_dev - return device
642 * @hw: pointer to hardware structure
644 * used by hardware layer to print debugging information
646 struct net_device *igb_get_hw_dev(struct e1000_hw *hw)
648 struct igb_adapter *adapter = hw->back;
649 return adapter->netdev;
653 * igb_init_module - Driver Registration Routine
655 * igb_init_module is the first routine called when the driver is
656 * loaded. All it does is register with the PCI subsystem.
658 static int __init igb_init_module(void)
662 pr_info("%s\n", igb_driver_string);
663 pr_info("%s\n", igb_copyright);
665 #ifdef CONFIG_IGB_DCA
666 dca_register_notify(&dca_notifier);
668 ret = pci_register_driver(&igb_driver);
672 module_init(igb_init_module);
675 * igb_exit_module - Driver Exit Cleanup Routine
677 * igb_exit_module is called just before the driver is removed
680 static void __exit igb_exit_module(void)
682 #ifdef CONFIG_IGB_DCA
683 dca_unregister_notify(&dca_notifier);
685 pci_unregister_driver(&igb_driver);
688 module_exit(igb_exit_module);
690 #define Q_IDX_82576(i) (((i & 0x1) << 3) + (i >> 1))
692 * igb_cache_ring_register - Descriptor ring to register mapping
693 * @adapter: board private structure to initialize
695 * Once we know the feature-set enabled for the device, we'll cache
696 * the register offset the descriptor ring is assigned to.
698 static void igb_cache_ring_register(struct igb_adapter *adapter)
701 u32 rbase_offset = adapter->vfs_allocated_count;
703 switch (adapter->hw.mac.type) {
705 /* The queues are allocated for virtualization such that VF 0
706 * is allocated queues 0 and 8, VF 1 queues 1 and 9, etc.
707 * In order to avoid collision we start at the first free queue
708 * and continue consuming queues in the same sequence
710 if (adapter->vfs_allocated_count) {
711 for (; i < adapter->rss_queues; i++)
712 adapter->rx_ring[i]->reg_idx = rbase_offset +
723 for (; i < adapter->num_rx_queues; i++)
724 adapter->rx_ring[i]->reg_idx = rbase_offset + i;
725 for (; j < adapter->num_tx_queues; j++)
726 adapter->tx_ring[j]->reg_idx = rbase_offset + j;
731 u32 igb_rd32(struct e1000_hw *hw, u32 reg)
733 struct igb_adapter *igb = container_of(hw, struct igb_adapter, hw);
734 u8 __iomem *hw_addr = READ_ONCE(hw->hw_addr);
737 if (E1000_REMOVED(hw_addr))
740 value = readl(&hw_addr[reg]);
742 /* reads should not return all F's */
743 if (!(~value) && (!reg || !(~readl(hw_addr)))) {
744 struct net_device *netdev = igb->netdev;
746 netdev_err(netdev, "PCIe link lost\n");
747 WARN(pci_device_is_present(igb->pdev),
748 "igb: Failed to read reg 0x%x!\n", reg);
755 * igb_write_ivar - configure ivar for given MSI-X vector
756 * @hw: pointer to the HW structure
757 * @msix_vector: vector number we are allocating to a given ring
758 * @index: row index of IVAR register to write within IVAR table
759 * @offset: column offset of in IVAR, should be multiple of 8
761 * This function is intended to handle the writing of the IVAR register
762 * for adapters 82576 and newer. The IVAR table consists of 2 columns,
763 * each containing an cause allocation for an Rx and Tx ring, and a
764 * variable number of rows depending on the number of queues supported.
766 static void igb_write_ivar(struct e1000_hw *hw, int msix_vector,
767 int index, int offset)
769 u32 ivar = array_rd32(E1000_IVAR0, index);
771 /* clear any bits that are currently set */
772 ivar &= ~((u32)0xFF << offset);
774 /* write vector and valid bit */
775 ivar |= (msix_vector | E1000_IVAR_VALID) << offset;
777 array_wr32(E1000_IVAR0, index, ivar);
780 #define IGB_N0_QUEUE -1
781 static void igb_assign_vector(struct igb_q_vector *q_vector, int msix_vector)
783 struct igb_adapter *adapter = q_vector->adapter;
784 struct e1000_hw *hw = &adapter->hw;
785 int rx_queue = IGB_N0_QUEUE;
786 int tx_queue = IGB_N0_QUEUE;
789 if (q_vector->rx.ring)
790 rx_queue = q_vector->rx.ring->reg_idx;
791 if (q_vector->tx.ring)
792 tx_queue = q_vector->tx.ring->reg_idx;
794 switch (hw->mac.type) {
796 /* The 82575 assigns vectors using a bitmask, which matches the
797 * bitmask for the EICR/EIMS/EIMC registers. To assign one
798 * or more queues to a vector, we write the appropriate bits
799 * into the MSIXBM register for that vector.
801 if (rx_queue > IGB_N0_QUEUE)
802 msixbm = E1000_EICR_RX_QUEUE0 << rx_queue;
803 if (tx_queue > IGB_N0_QUEUE)
804 msixbm |= E1000_EICR_TX_QUEUE0 << tx_queue;
805 if (!(adapter->flags & IGB_FLAG_HAS_MSIX) && msix_vector == 0)
806 msixbm |= E1000_EIMS_OTHER;
807 array_wr32(E1000_MSIXBM(0), msix_vector, msixbm);
808 q_vector->eims_value = msixbm;
811 /* 82576 uses a table that essentially consists of 2 columns
812 * with 8 rows. The ordering is column-major so we use the
813 * lower 3 bits as the row index, and the 4th bit as the
816 if (rx_queue > IGB_N0_QUEUE)
817 igb_write_ivar(hw, msix_vector,
819 (rx_queue & 0x8) << 1);
820 if (tx_queue > IGB_N0_QUEUE)
821 igb_write_ivar(hw, msix_vector,
823 ((tx_queue & 0x8) << 1) + 8);
824 q_vector->eims_value = BIT(msix_vector);
831 /* On 82580 and newer adapters the scheme is similar to 82576
832 * however instead of ordering column-major we have things
833 * ordered row-major. So we traverse the table by using
834 * bit 0 as the column offset, and the remaining bits as the
837 if (rx_queue > IGB_N0_QUEUE)
838 igb_write_ivar(hw, msix_vector,
840 (rx_queue & 0x1) << 4);
841 if (tx_queue > IGB_N0_QUEUE)
842 igb_write_ivar(hw, msix_vector,
844 ((tx_queue & 0x1) << 4) + 8);
845 q_vector->eims_value = BIT(msix_vector);
852 /* add q_vector eims value to global eims_enable_mask */
853 adapter->eims_enable_mask |= q_vector->eims_value;
855 /* configure q_vector to set itr on first interrupt */
856 q_vector->set_itr = 1;
860 * igb_configure_msix - Configure MSI-X hardware
861 * @adapter: board private structure to initialize
863 * igb_configure_msix sets up the hardware to properly
864 * generate MSI-X interrupts.
866 static void igb_configure_msix(struct igb_adapter *adapter)
870 struct e1000_hw *hw = &adapter->hw;
872 adapter->eims_enable_mask = 0;
874 /* set vector for other causes, i.e. link changes */
875 switch (hw->mac.type) {
877 tmp = rd32(E1000_CTRL_EXT);
878 /* enable MSI-X PBA support*/
879 tmp |= E1000_CTRL_EXT_PBA_CLR;
881 /* Auto-Mask interrupts upon ICR read. */
882 tmp |= E1000_CTRL_EXT_EIAME;
883 tmp |= E1000_CTRL_EXT_IRCA;
885 wr32(E1000_CTRL_EXT, tmp);
887 /* enable msix_other interrupt */
888 array_wr32(E1000_MSIXBM(0), vector++, E1000_EIMS_OTHER);
889 adapter->eims_other = E1000_EIMS_OTHER;
899 /* Turn on MSI-X capability first, or our settings
900 * won't stick. And it will take days to debug.
902 wr32(E1000_GPIE, E1000_GPIE_MSIX_MODE |
903 E1000_GPIE_PBA | E1000_GPIE_EIAME |
906 /* enable msix_other interrupt */
907 adapter->eims_other = BIT(vector);
908 tmp = (vector++ | E1000_IVAR_VALID) << 8;
910 wr32(E1000_IVAR_MISC, tmp);
913 /* do nothing, since nothing else supports MSI-X */
915 } /* switch (hw->mac.type) */
917 adapter->eims_enable_mask |= adapter->eims_other;
919 for (i = 0; i < adapter->num_q_vectors; i++)
920 igb_assign_vector(adapter->q_vector[i], vector++);
926 * igb_request_msix - Initialize MSI-X interrupts
927 * @adapter: board private structure to initialize
929 * igb_request_msix allocates MSI-X vectors and requests interrupts from the
932 static int igb_request_msix(struct igb_adapter *adapter)
934 struct net_device *netdev = adapter->netdev;
935 int i, err = 0, vector = 0, free_vector = 0;
937 err = request_irq(adapter->msix_entries[vector].vector,
938 igb_msix_other, 0, netdev->name, adapter);
942 for (i = 0; i < adapter->num_q_vectors; i++) {
943 struct igb_q_vector *q_vector = adapter->q_vector[i];
947 q_vector->itr_register = adapter->io_addr + E1000_EITR(vector);
949 if (q_vector->rx.ring && q_vector->tx.ring)
950 sprintf(q_vector->name, "%s-TxRx-%u", netdev->name,
951 q_vector->rx.ring->queue_index);
952 else if (q_vector->tx.ring)
953 sprintf(q_vector->name, "%s-tx-%u", netdev->name,
954 q_vector->tx.ring->queue_index);
955 else if (q_vector->rx.ring)
956 sprintf(q_vector->name, "%s-rx-%u", netdev->name,
957 q_vector->rx.ring->queue_index);
959 sprintf(q_vector->name, "%s-unused", netdev->name);
961 err = request_irq(adapter->msix_entries[vector].vector,
962 igb_msix_ring, 0, q_vector->name,
968 igb_configure_msix(adapter);
972 /* free already assigned IRQs */
973 free_irq(adapter->msix_entries[free_vector++].vector, adapter);
976 for (i = 0; i < vector; i++) {
977 free_irq(adapter->msix_entries[free_vector++].vector,
978 adapter->q_vector[i]);
985 * igb_free_q_vector - Free memory allocated for specific interrupt vector
986 * @adapter: board private structure to initialize
987 * @v_idx: Index of vector to be freed
989 * This function frees the memory allocated to the q_vector.
991 static void igb_free_q_vector(struct igb_adapter *adapter, int v_idx)
993 struct igb_q_vector *q_vector = adapter->q_vector[v_idx];
995 adapter->q_vector[v_idx] = NULL;
997 /* igb_get_stats64() might access the rings on this vector,
998 * we must wait a grace period before freeing it.
1001 kfree_rcu(q_vector, rcu);
1005 * igb_reset_q_vector - Reset config for interrupt vector
1006 * @adapter: board private structure to initialize
1007 * @v_idx: Index of vector to be reset
1009 * If NAPI is enabled it will delete any references to the
1010 * NAPI struct. This is preparation for igb_free_q_vector.
1012 static void igb_reset_q_vector(struct igb_adapter *adapter, int v_idx)
1014 struct igb_q_vector *q_vector = adapter->q_vector[v_idx];
1016 /* Coming from igb_set_interrupt_capability, the vectors are not yet
1017 * allocated. So, q_vector is NULL so we should stop here.
1022 if (q_vector->tx.ring)
1023 adapter->tx_ring[q_vector->tx.ring->queue_index] = NULL;
1025 if (q_vector->rx.ring)
1026 adapter->rx_ring[q_vector->rx.ring->queue_index] = NULL;
1028 netif_napi_del(&q_vector->napi);
1032 static void igb_reset_interrupt_capability(struct igb_adapter *adapter)
1034 int v_idx = adapter->num_q_vectors;
1036 if (adapter->flags & IGB_FLAG_HAS_MSIX)
1037 pci_disable_msix(adapter->pdev);
1038 else if (adapter->flags & IGB_FLAG_HAS_MSI)
1039 pci_disable_msi(adapter->pdev);
1042 igb_reset_q_vector(adapter, v_idx);
1046 * igb_free_q_vectors - Free memory allocated for interrupt vectors
1047 * @adapter: board private structure to initialize
1049 * This function frees the memory allocated to the q_vectors. In addition if
1050 * NAPI is enabled it will delete any references to the NAPI struct prior
1051 * to freeing the q_vector.
1053 static void igb_free_q_vectors(struct igb_adapter *adapter)
1055 int v_idx = adapter->num_q_vectors;
1057 adapter->num_tx_queues = 0;
1058 adapter->num_rx_queues = 0;
1059 adapter->num_q_vectors = 0;
1062 igb_reset_q_vector(adapter, v_idx);
1063 igb_free_q_vector(adapter, v_idx);
1068 * igb_clear_interrupt_scheme - reset the device to a state of no interrupts
1069 * @adapter: board private structure to initialize
1071 * This function resets the device so that it has 0 Rx queues, Tx queues, and
1072 * MSI-X interrupts allocated.
1074 static void igb_clear_interrupt_scheme(struct igb_adapter *adapter)
1076 igb_free_q_vectors(adapter);
1077 igb_reset_interrupt_capability(adapter);
1081 * igb_set_interrupt_capability - set MSI or MSI-X if supported
1082 * @adapter: board private structure to initialize
1083 * @msix: boolean value of MSIX capability
1085 * Attempt to configure interrupts using the best available
1086 * capabilities of the hardware and kernel.
1088 static void igb_set_interrupt_capability(struct igb_adapter *adapter, bool msix)
1095 adapter->flags |= IGB_FLAG_HAS_MSIX;
1097 /* Number of supported queues. */
1098 adapter->num_rx_queues = adapter->rss_queues;
1099 if (adapter->vfs_allocated_count)
1100 adapter->num_tx_queues = 1;
1102 adapter->num_tx_queues = adapter->rss_queues;
1104 /* start with one vector for every Rx queue */
1105 numvecs = adapter->num_rx_queues;
1107 /* if Tx handler is separate add 1 for every Tx queue */
1108 if (!(adapter->flags & IGB_FLAG_QUEUE_PAIRS))
1109 numvecs += adapter->num_tx_queues;
1111 /* store the number of vectors reserved for queues */
1112 adapter->num_q_vectors = numvecs;
1114 /* add 1 vector for link status interrupts */
1116 for (i = 0; i < numvecs; i++)
1117 adapter->msix_entries[i].entry = i;
1119 err = pci_enable_msix_range(adapter->pdev,
1120 adapter->msix_entries,
1126 igb_reset_interrupt_capability(adapter);
1128 /* If we can't do MSI-X, try MSI */
1130 adapter->flags &= ~IGB_FLAG_HAS_MSIX;
1131 #ifdef CONFIG_PCI_IOV
1132 /* disable SR-IOV for non MSI-X configurations */
1133 if (adapter->vf_data) {
1134 struct e1000_hw *hw = &adapter->hw;
1135 /* disable iov and allow time for transactions to clear */
1136 pci_disable_sriov(adapter->pdev);
1139 kfree(adapter->vf_mac_list);
1140 adapter->vf_mac_list = NULL;
1141 kfree(adapter->vf_data);
1142 adapter->vf_data = NULL;
1143 wr32(E1000_IOVCTL, E1000_IOVCTL_REUSE_VFQ);
1146 dev_info(&adapter->pdev->dev, "IOV Disabled\n");
1149 adapter->vfs_allocated_count = 0;
1150 adapter->rss_queues = 1;
1151 adapter->flags |= IGB_FLAG_QUEUE_PAIRS;
1152 adapter->num_rx_queues = 1;
1153 adapter->num_tx_queues = 1;
1154 adapter->num_q_vectors = 1;
1155 if (!pci_enable_msi(adapter->pdev))
1156 adapter->flags |= IGB_FLAG_HAS_MSI;
1159 static void igb_add_ring(struct igb_ring *ring,
1160 struct igb_ring_container *head)
1167 * igb_alloc_q_vector - Allocate memory for a single interrupt vector
1168 * @adapter: board private structure to initialize
1169 * @v_count: q_vectors allocated on adapter, used for ring interleaving
1170 * @v_idx: index of vector in adapter struct
1171 * @txr_count: total number of Tx rings to allocate
1172 * @txr_idx: index of first Tx ring to allocate
1173 * @rxr_count: total number of Rx rings to allocate
1174 * @rxr_idx: index of first Rx ring to allocate
1176 * We allocate one q_vector. If allocation fails we return -ENOMEM.
1178 static int igb_alloc_q_vector(struct igb_adapter *adapter,
1179 int v_count, int v_idx,
1180 int txr_count, int txr_idx,
1181 int rxr_count, int rxr_idx)
1183 struct igb_q_vector *q_vector;
1184 struct igb_ring *ring;
1188 /* igb only supports 1 Tx and/or 1 Rx queue per vector */
1189 if (txr_count > 1 || rxr_count > 1)
1192 ring_count = txr_count + rxr_count;
1193 size = struct_size(q_vector, ring, ring_count);
1195 /* allocate q_vector and rings */
1196 q_vector = adapter->q_vector[v_idx];
1198 q_vector = kzalloc(size, GFP_KERNEL);
1199 } else if (size > ksize(q_vector)) {
1200 kfree_rcu(q_vector, rcu);
1201 q_vector = kzalloc(size, GFP_KERNEL);
1203 memset(q_vector, 0, size);
1208 /* initialize NAPI */
1209 netif_napi_add(adapter->netdev, &q_vector->napi,
1212 /* tie q_vector and adapter together */
1213 adapter->q_vector[v_idx] = q_vector;
1214 q_vector->adapter = adapter;
1216 /* initialize work limits */
1217 q_vector->tx.work_limit = adapter->tx_work_limit;
1219 /* initialize ITR configuration */
1220 q_vector->itr_register = adapter->io_addr + E1000_EITR(0);
1221 q_vector->itr_val = IGB_START_ITR;
1223 /* initialize pointer to rings */
1224 ring = q_vector->ring;
1228 /* rx or rx/tx vector */
1229 if (!adapter->rx_itr_setting || adapter->rx_itr_setting > 3)
1230 q_vector->itr_val = adapter->rx_itr_setting;
1232 /* tx only vector */
1233 if (!adapter->tx_itr_setting || adapter->tx_itr_setting > 3)
1234 q_vector->itr_val = adapter->tx_itr_setting;
1238 /* assign generic ring traits */
1239 ring->dev = &adapter->pdev->dev;
1240 ring->netdev = adapter->netdev;
1242 /* configure backlink on ring */
1243 ring->q_vector = q_vector;
1245 /* update q_vector Tx values */
1246 igb_add_ring(ring, &q_vector->tx);
1248 /* For 82575, context index must be unique per ring. */
1249 if (adapter->hw.mac.type == e1000_82575)
1250 set_bit(IGB_RING_FLAG_TX_CTX_IDX, &ring->flags);
1252 /* apply Tx specific ring traits */
1253 ring->count = adapter->tx_ring_count;
1254 ring->queue_index = txr_idx;
1256 ring->cbs_enable = false;
1257 ring->idleslope = 0;
1258 ring->sendslope = 0;
1262 u64_stats_init(&ring->tx_syncp);
1263 u64_stats_init(&ring->tx_syncp2);
1265 /* assign ring to adapter */
1266 adapter->tx_ring[txr_idx] = ring;
1268 /* push pointer to next ring */
1273 /* assign generic ring traits */
1274 ring->dev = &adapter->pdev->dev;
1275 ring->netdev = adapter->netdev;
1277 /* configure backlink on ring */
1278 ring->q_vector = q_vector;
1280 /* update q_vector Rx values */
1281 igb_add_ring(ring, &q_vector->rx);
1283 /* set flag indicating ring supports SCTP checksum offload */
1284 if (adapter->hw.mac.type >= e1000_82576)
1285 set_bit(IGB_RING_FLAG_RX_SCTP_CSUM, &ring->flags);
1287 /* On i350, i354, i210, and i211, loopback VLAN packets
1288 * have the tag byte-swapped.
1290 if (adapter->hw.mac.type >= e1000_i350)
1291 set_bit(IGB_RING_FLAG_RX_LB_VLAN_BSWAP, &ring->flags);
1293 /* apply Rx specific ring traits */
1294 ring->count = adapter->rx_ring_count;
1295 ring->queue_index = rxr_idx;
1297 u64_stats_init(&ring->rx_syncp);
1299 /* assign ring to adapter */
1300 adapter->rx_ring[rxr_idx] = ring;
1308 * igb_alloc_q_vectors - Allocate memory for interrupt vectors
1309 * @adapter: board private structure to initialize
1311 * We allocate one q_vector per queue interrupt. If allocation fails we
1314 static int igb_alloc_q_vectors(struct igb_adapter *adapter)
1316 int q_vectors = adapter->num_q_vectors;
1317 int rxr_remaining = adapter->num_rx_queues;
1318 int txr_remaining = adapter->num_tx_queues;
1319 int rxr_idx = 0, txr_idx = 0, v_idx = 0;
1322 if (q_vectors >= (rxr_remaining + txr_remaining)) {
1323 for (; rxr_remaining; v_idx++) {
1324 err = igb_alloc_q_vector(adapter, q_vectors, v_idx,
1330 /* update counts and index */
1336 for (; v_idx < q_vectors; v_idx++) {
1337 int rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - v_idx);
1338 int tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - v_idx);
1340 err = igb_alloc_q_vector(adapter, q_vectors, v_idx,
1341 tqpv, txr_idx, rqpv, rxr_idx);
1346 /* update counts and index */
1347 rxr_remaining -= rqpv;
1348 txr_remaining -= tqpv;
1356 adapter->num_tx_queues = 0;
1357 adapter->num_rx_queues = 0;
1358 adapter->num_q_vectors = 0;
1361 igb_free_q_vector(adapter, v_idx);
1367 * igb_init_interrupt_scheme - initialize interrupts, allocate queues/vectors
1368 * @adapter: board private structure to initialize
1369 * @msix: boolean value of MSIX capability
1371 * This function initializes the interrupts and allocates all of the queues.
1373 static int igb_init_interrupt_scheme(struct igb_adapter *adapter, bool msix)
1375 struct pci_dev *pdev = adapter->pdev;
1378 igb_set_interrupt_capability(adapter, msix);
1380 err = igb_alloc_q_vectors(adapter);
1382 dev_err(&pdev->dev, "Unable to allocate memory for vectors\n");
1383 goto err_alloc_q_vectors;
1386 igb_cache_ring_register(adapter);
1390 err_alloc_q_vectors:
1391 igb_reset_interrupt_capability(adapter);
1396 * igb_request_irq - initialize interrupts
1397 * @adapter: board private structure to initialize
1399 * Attempts to configure interrupts using the best available
1400 * capabilities of the hardware and kernel.
1402 static int igb_request_irq(struct igb_adapter *adapter)
1404 struct net_device *netdev = adapter->netdev;
1405 struct pci_dev *pdev = adapter->pdev;
1408 if (adapter->flags & IGB_FLAG_HAS_MSIX) {
1409 err = igb_request_msix(adapter);
1412 /* fall back to MSI */
1413 igb_free_all_tx_resources(adapter);
1414 igb_free_all_rx_resources(adapter);
1416 igb_clear_interrupt_scheme(adapter);
1417 err = igb_init_interrupt_scheme(adapter, false);
1421 igb_setup_all_tx_resources(adapter);
1422 igb_setup_all_rx_resources(adapter);
1423 igb_configure(adapter);
1426 igb_assign_vector(adapter->q_vector[0], 0);
1428 if (adapter->flags & IGB_FLAG_HAS_MSI) {
1429 err = request_irq(pdev->irq, igb_intr_msi, 0,
1430 netdev->name, adapter);
1434 /* fall back to legacy interrupts */
1435 igb_reset_interrupt_capability(adapter);
1436 adapter->flags &= ~IGB_FLAG_HAS_MSI;
1439 err = request_irq(pdev->irq, igb_intr, IRQF_SHARED,
1440 netdev->name, adapter);
1443 dev_err(&pdev->dev, "Error %d getting interrupt\n",
1450 static void igb_free_irq(struct igb_adapter *adapter)
1452 if (adapter->flags & IGB_FLAG_HAS_MSIX) {
1455 free_irq(adapter->msix_entries[vector++].vector, adapter);
1457 for (i = 0; i < adapter->num_q_vectors; i++)
1458 free_irq(adapter->msix_entries[vector++].vector,
1459 adapter->q_vector[i]);
1461 free_irq(adapter->pdev->irq, adapter);
1466 * igb_irq_disable - Mask off interrupt generation on the NIC
1467 * @adapter: board private structure
1469 static void igb_irq_disable(struct igb_adapter *adapter)
1471 struct e1000_hw *hw = &adapter->hw;
1473 /* we need to be careful when disabling interrupts. The VFs are also
1474 * mapped into these registers and so clearing the bits can cause
1475 * issues on the VF drivers so we only need to clear what we set
1477 if (adapter->flags & IGB_FLAG_HAS_MSIX) {
1478 u32 regval = rd32(E1000_EIAM);
1480 wr32(E1000_EIAM, regval & ~adapter->eims_enable_mask);
1481 wr32(E1000_EIMC, adapter->eims_enable_mask);
1482 regval = rd32(E1000_EIAC);
1483 wr32(E1000_EIAC, regval & ~adapter->eims_enable_mask);
1487 wr32(E1000_IMC, ~0);
1489 if (adapter->flags & IGB_FLAG_HAS_MSIX) {
1492 for (i = 0; i < adapter->num_q_vectors; i++)
1493 synchronize_irq(adapter->msix_entries[i].vector);
1495 synchronize_irq(adapter->pdev->irq);
1500 * igb_irq_enable - Enable default interrupt generation settings
1501 * @adapter: board private structure
1503 static void igb_irq_enable(struct igb_adapter *adapter)
1505 struct e1000_hw *hw = &adapter->hw;
1507 if (adapter->flags & IGB_FLAG_HAS_MSIX) {
1508 u32 ims = E1000_IMS_LSC | E1000_IMS_DOUTSYNC | E1000_IMS_DRSTA;
1509 u32 regval = rd32(E1000_EIAC);
1511 wr32(E1000_EIAC, regval | adapter->eims_enable_mask);
1512 regval = rd32(E1000_EIAM);
1513 wr32(E1000_EIAM, regval | adapter->eims_enable_mask);
1514 wr32(E1000_EIMS, adapter->eims_enable_mask);
1515 if (adapter->vfs_allocated_count) {
1516 wr32(E1000_MBVFIMR, 0xFF);
1517 ims |= E1000_IMS_VMMB;
1519 wr32(E1000_IMS, ims);
1521 wr32(E1000_IMS, IMS_ENABLE_MASK |
1523 wr32(E1000_IAM, IMS_ENABLE_MASK |
1528 static void igb_update_mng_vlan(struct igb_adapter *adapter)
1530 struct e1000_hw *hw = &adapter->hw;
1531 u16 pf_id = adapter->vfs_allocated_count;
1532 u16 vid = adapter->hw.mng_cookie.vlan_id;
1533 u16 old_vid = adapter->mng_vlan_id;
1535 if (hw->mng_cookie.status & E1000_MNG_DHCP_COOKIE_STATUS_VLAN) {
1536 /* add VID to filter table */
1537 igb_vfta_set(hw, vid, pf_id, true, true);
1538 adapter->mng_vlan_id = vid;
1540 adapter->mng_vlan_id = IGB_MNG_VLAN_NONE;
1543 if ((old_vid != (u16)IGB_MNG_VLAN_NONE) &&
1545 !test_bit(old_vid, adapter->active_vlans)) {
1546 /* remove VID from filter table */
1547 igb_vfta_set(hw, vid, pf_id, false, true);
1552 * igb_release_hw_control - release control of the h/w to f/w
1553 * @adapter: address of board private structure
1555 * igb_release_hw_control resets CTRL_EXT:DRV_LOAD bit.
1556 * For ASF and Pass Through versions of f/w this means that the
1557 * driver is no longer loaded.
1559 static void igb_release_hw_control(struct igb_adapter *adapter)
1561 struct e1000_hw *hw = &adapter->hw;
1564 /* Let firmware take over control of h/w */
1565 ctrl_ext = rd32(E1000_CTRL_EXT);
1566 wr32(E1000_CTRL_EXT,
1567 ctrl_ext & ~E1000_CTRL_EXT_DRV_LOAD);
1571 * igb_get_hw_control - get control of the h/w from f/w
1572 * @adapter: address of board private structure
1574 * igb_get_hw_control sets CTRL_EXT:DRV_LOAD bit.
1575 * For ASF and Pass Through versions of f/w this means that
1576 * the driver is loaded.
1578 static void igb_get_hw_control(struct igb_adapter *adapter)
1580 struct e1000_hw *hw = &adapter->hw;
1583 /* Let firmware know the driver has taken over */
1584 ctrl_ext = rd32(E1000_CTRL_EXT);
1585 wr32(E1000_CTRL_EXT,
1586 ctrl_ext | E1000_CTRL_EXT_DRV_LOAD);
1589 static void enable_fqtss(struct igb_adapter *adapter, bool enable)
1591 struct net_device *netdev = adapter->netdev;
1592 struct e1000_hw *hw = &adapter->hw;
1594 WARN_ON(hw->mac.type != e1000_i210);
1597 adapter->flags |= IGB_FLAG_FQTSS;
1599 adapter->flags &= ~IGB_FLAG_FQTSS;
1601 if (netif_running(netdev))
1602 schedule_work(&adapter->reset_task);
1605 static bool is_fqtss_enabled(struct igb_adapter *adapter)
1607 return (adapter->flags & IGB_FLAG_FQTSS) ? true : false;
1610 static void set_tx_desc_fetch_prio(struct e1000_hw *hw, int queue,
1611 enum tx_queue_prio prio)
1615 WARN_ON(hw->mac.type != e1000_i210);
1616 WARN_ON(queue < 0 || queue > 4);
1618 val = rd32(E1000_I210_TXDCTL(queue));
1620 if (prio == TX_QUEUE_PRIO_HIGH)
1621 val |= E1000_TXDCTL_PRIORITY;
1623 val &= ~E1000_TXDCTL_PRIORITY;
1625 wr32(E1000_I210_TXDCTL(queue), val);
1628 static void set_queue_mode(struct e1000_hw *hw, int queue, enum queue_mode mode)
1632 WARN_ON(hw->mac.type != e1000_i210);
1633 WARN_ON(queue < 0 || queue > 1);
1635 val = rd32(E1000_I210_TQAVCC(queue));
1637 if (mode == QUEUE_MODE_STREAM_RESERVATION)
1638 val |= E1000_TQAVCC_QUEUEMODE;
1640 val &= ~E1000_TQAVCC_QUEUEMODE;
1642 wr32(E1000_I210_TQAVCC(queue), val);
1645 static bool is_any_cbs_enabled(struct igb_adapter *adapter)
1649 for (i = 0; i < adapter->num_tx_queues; i++) {
1650 if (adapter->tx_ring[i]->cbs_enable)
1657 static bool is_any_txtime_enabled(struct igb_adapter *adapter)
1661 for (i = 0; i < adapter->num_tx_queues; i++) {
1662 if (adapter->tx_ring[i]->launchtime_enable)
1670 * igb_config_tx_modes - Configure "Qav Tx mode" features on igb
1671 * @adapter: pointer to adapter struct
1672 * @queue: queue number
1674 * Configure CBS and Launchtime for a given hardware queue.
1675 * Parameters are retrieved from the correct Tx ring, so
1676 * igb_save_cbs_params() and igb_save_txtime_params() should be used
1677 * for setting those correctly prior to this function being called.
1679 static void igb_config_tx_modes(struct igb_adapter *adapter, int queue)
1681 struct igb_ring *ring = adapter->tx_ring[queue];
1682 struct net_device *netdev = adapter->netdev;
1683 struct e1000_hw *hw = &adapter->hw;
1684 u32 tqavcc, tqavctrl;
1687 WARN_ON(hw->mac.type != e1000_i210);
1688 WARN_ON(queue < 0 || queue > 1);
1690 /* If any of the Qav features is enabled, configure queues as SR and
1691 * with HIGH PRIO. If none is, then configure them with LOW PRIO and
1694 if (ring->cbs_enable || ring->launchtime_enable) {
1695 set_tx_desc_fetch_prio(hw, queue, TX_QUEUE_PRIO_HIGH);
1696 set_queue_mode(hw, queue, QUEUE_MODE_STREAM_RESERVATION);
1698 set_tx_desc_fetch_prio(hw, queue, TX_QUEUE_PRIO_LOW);
1699 set_queue_mode(hw, queue, QUEUE_MODE_STRICT_PRIORITY);
1702 /* If CBS is enabled, set DataTranARB and config its parameters. */
1703 if (ring->cbs_enable || queue == 0) {
1704 /* i210 does not allow the queue 0 to be in the Strict
1705 * Priority mode while the Qav mode is enabled, so,
1706 * instead of disabling strict priority mode, we give
1707 * queue 0 the maximum of credits possible.
1709 * See section 8.12.19 of the i210 datasheet, "Note:
1710 * Queue0 QueueMode must be set to 1b when
1711 * TransmitMode is set to Qav."
1713 if (queue == 0 && !ring->cbs_enable) {
1714 /* max "linkspeed" idleslope in kbps */
1715 ring->idleslope = 1000000;
1716 ring->hicredit = ETH_FRAME_LEN;
1719 /* Always set data transfer arbitration to credit-based
1720 * shaper algorithm on TQAVCTRL if CBS is enabled for any of
1723 tqavctrl = rd32(E1000_I210_TQAVCTRL);
1724 tqavctrl |= E1000_TQAVCTRL_DATATRANARB;
1725 wr32(E1000_I210_TQAVCTRL, tqavctrl);
1727 /* According to i210 datasheet section 7.2.7.7, we should set
1728 * the 'idleSlope' field from TQAVCC register following the
1731 * For 100 Mbps link speed:
1733 * value = BW * 0x7735 * 0.2 (E1)
1735 * For 1000Mbps link speed:
1737 * value = BW * 0x7735 * 2 (E2)
1739 * E1 and E2 can be merged into one equation as shown below.
1740 * Note that 'link-speed' is in Mbps.
1742 * value = BW * 0x7735 * 2 * link-speed
1743 * -------------- (E3)
1746 * 'BW' is the percentage bandwidth out of full link speed
1747 * which can be found with the following equation. Note that
1748 * idleSlope here is the parameter from this function which
1752 * ----------------- (E4)
1755 * That said, we can come up with a generic equation to
1756 * calculate the value we should set it TQAVCC register by
1757 * replacing 'BW' in E3 by E4. The resulting equation is:
1759 * value = idleSlope * 0x7735 * 2 * link-speed
1760 * ----------------- -------------- (E5)
1761 * link-speed * 1000 1000
1763 * 'link-speed' is present in both sides of the fraction so
1764 * it is canceled out. The final equation is the following:
1766 * value = idleSlope * 61034
1767 * ----------------- (E6)
1770 * NOTE: For i210, given the above, we can see that idleslope
1771 * is represented in 16.38431 kbps units by the value at
1772 * the TQAVCC register (1Gbps / 61034), which reduces
1773 * the granularity for idleslope increments.
1774 * For instance, if you want to configure a 2576kbps
1775 * idleslope, the value to be written on the register
1776 * would have to be 157.23. If rounded down, you end
1777 * up with less bandwidth available than originally
1778 * required (~2572 kbps). If rounded up, you end up
1779 * with a higher bandwidth (~2589 kbps). Below the
1780 * approach we take is to always round up the
1781 * calculated value, so the resulting bandwidth might
1782 * be slightly higher for some configurations.
1784 value = DIV_ROUND_UP_ULL(ring->idleslope * 61034ULL, 1000000);
1786 tqavcc = rd32(E1000_I210_TQAVCC(queue));
1787 tqavcc &= ~E1000_TQAVCC_IDLESLOPE_MASK;
1789 wr32(E1000_I210_TQAVCC(queue), tqavcc);
1791 wr32(E1000_I210_TQAVHC(queue),
1792 0x80000000 + ring->hicredit * 0x7735);
1795 /* Set idleSlope to zero. */
1796 tqavcc = rd32(E1000_I210_TQAVCC(queue));
1797 tqavcc &= ~E1000_TQAVCC_IDLESLOPE_MASK;
1798 wr32(E1000_I210_TQAVCC(queue), tqavcc);
1800 /* Set hiCredit to zero. */
1801 wr32(E1000_I210_TQAVHC(queue), 0);
1803 /* If CBS is not enabled for any queues anymore, then return to
1804 * the default state of Data Transmission Arbitration on
1807 if (!is_any_cbs_enabled(adapter)) {
1808 tqavctrl = rd32(E1000_I210_TQAVCTRL);
1809 tqavctrl &= ~E1000_TQAVCTRL_DATATRANARB;
1810 wr32(E1000_I210_TQAVCTRL, tqavctrl);
1814 /* If LaunchTime is enabled, set DataTranTIM. */
1815 if (ring->launchtime_enable) {
1816 /* Always set DataTranTIM on TQAVCTRL if LaunchTime is enabled
1817 * for any of the SR queues, and configure fetchtime delta.
1819 * - LaunchTime will be enabled for all SR queues.
1820 * - A fixed offset can be added relative to the launch
1821 * time of all packets if configured at reg LAUNCH_OS0.
1822 * We are keeping it as 0 for now (default value).
1824 tqavctrl = rd32(E1000_I210_TQAVCTRL);
1825 tqavctrl |= E1000_TQAVCTRL_DATATRANTIM |
1826 E1000_TQAVCTRL_FETCHTIME_DELTA;
1827 wr32(E1000_I210_TQAVCTRL, tqavctrl);
1829 /* If Launchtime is not enabled for any SR queues anymore,
1830 * then clear DataTranTIM on TQAVCTRL and clear fetchtime delta,
1831 * effectively disabling Launchtime.
1833 if (!is_any_txtime_enabled(adapter)) {
1834 tqavctrl = rd32(E1000_I210_TQAVCTRL);
1835 tqavctrl &= ~E1000_TQAVCTRL_DATATRANTIM;
1836 tqavctrl &= ~E1000_TQAVCTRL_FETCHTIME_DELTA;
1837 wr32(E1000_I210_TQAVCTRL, tqavctrl);
1841 /* XXX: In i210 controller the sendSlope and loCredit parameters from
1842 * CBS are not configurable by software so we don't do any 'controller
1843 * configuration' in respect to these parameters.
1846 netdev_dbg(netdev, "Qav Tx mode: cbs %s, launchtime %s, queue %d idleslope %d sendslope %d hiCredit %d locredit %d\n",
1847 ring->cbs_enable ? "enabled" : "disabled",
1848 ring->launchtime_enable ? "enabled" : "disabled",
1850 ring->idleslope, ring->sendslope,
1851 ring->hicredit, ring->locredit);
1854 static int igb_save_txtime_params(struct igb_adapter *adapter, int queue,
1857 struct igb_ring *ring;
1859 if (queue < 0 || queue > adapter->num_tx_queues)
1862 ring = adapter->tx_ring[queue];
1863 ring->launchtime_enable = enable;
1868 static int igb_save_cbs_params(struct igb_adapter *adapter, int queue,
1869 bool enable, int idleslope, int sendslope,
1870 int hicredit, int locredit)
1872 struct igb_ring *ring;
1874 if (queue < 0 || queue > adapter->num_tx_queues)
1877 ring = adapter->tx_ring[queue];
1879 ring->cbs_enable = enable;
1880 ring->idleslope = idleslope;
1881 ring->sendslope = sendslope;
1882 ring->hicredit = hicredit;
1883 ring->locredit = locredit;
1889 * igb_setup_tx_mode - Switch to/from Qav Tx mode when applicable
1890 * @adapter: pointer to adapter struct
1892 * Configure TQAVCTRL register switching the controller's Tx mode
1893 * if FQTSS mode is enabled or disabled. Additionally, will issue
1894 * a call to igb_config_tx_modes() per queue so any previously saved
1895 * Tx parameters are applied.
1897 static void igb_setup_tx_mode(struct igb_adapter *adapter)
1899 struct net_device *netdev = adapter->netdev;
1900 struct e1000_hw *hw = &adapter->hw;
1903 /* Only i210 controller supports changing the transmission mode. */
1904 if (hw->mac.type != e1000_i210)
1907 if (is_fqtss_enabled(adapter)) {
1910 /* Configure TQAVCTRL register: set transmit mode to 'Qav',
1911 * set data fetch arbitration to 'round robin', set SP_WAIT_SR
1912 * so SP queues wait for SR ones.
1914 val = rd32(E1000_I210_TQAVCTRL);
1915 val |= E1000_TQAVCTRL_XMIT_MODE | E1000_TQAVCTRL_SP_WAIT_SR;
1916 val &= ~E1000_TQAVCTRL_DATAFETCHARB;
1917 wr32(E1000_I210_TQAVCTRL, val);
1919 /* Configure Tx and Rx packet buffers sizes as described in
1920 * i210 datasheet section 7.2.7.7.
1922 val = rd32(E1000_TXPBS);
1923 val &= ~I210_TXPBSIZE_MASK;
1924 val |= I210_TXPBSIZE_PB0_6KB | I210_TXPBSIZE_PB1_6KB |
1925 I210_TXPBSIZE_PB2_6KB | I210_TXPBSIZE_PB3_6KB;
1926 wr32(E1000_TXPBS, val);
1928 val = rd32(E1000_RXPBS);
1929 val &= ~I210_RXPBSIZE_MASK;
1930 val |= I210_RXPBSIZE_PB_30KB;
1931 wr32(E1000_RXPBS, val);
1933 /* Section 8.12.9 states that MAX_TPKT_SIZE from DTXMXPKTSZ
1934 * register should not exceed the buffer size programmed in
1935 * TXPBS. The smallest buffer size programmed in TXPBS is 4kB
1936 * so according to the datasheet we should set MAX_TPKT_SIZE to
1939 * However, when we do so, no frame from queue 2 and 3 are
1940 * transmitted. It seems the MAX_TPKT_SIZE should not be great
1941 * or _equal_ to the buffer size programmed in TXPBS. For this
1942 * reason, we set set MAX_ TPKT_SIZE to (4kB - 1) / 64.
1944 val = (4096 - 1) / 64;
1945 wr32(E1000_I210_DTXMXPKTSZ, val);
1947 /* Since FQTSS mode is enabled, apply any CBS configuration
1948 * previously set. If no previous CBS configuration has been
1949 * done, then the initial configuration is applied, which means
1952 max_queue = (adapter->num_tx_queues < I210_SR_QUEUES_NUM) ?
1953 adapter->num_tx_queues : I210_SR_QUEUES_NUM;
1955 for (i = 0; i < max_queue; i++) {
1956 igb_config_tx_modes(adapter, i);
1959 wr32(E1000_RXPBS, I210_RXPBSIZE_DEFAULT);
1960 wr32(E1000_TXPBS, I210_TXPBSIZE_DEFAULT);
1961 wr32(E1000_I210_DTXMXPKTSZ, I210_DTXMXPKTSZ_DEFAULT);
1963 val = rd32(E1000_I210_TQAVCTRL);
1964 /* According to Section 8.12.21, the other flags we've set when
1965 * enabling FQTSS are not relevant when disabling FQTSS so we
1966 * don't set they here.
1968 val &= ~E1000_TQAVCTRL_XMIT_MODE;
1969 wr32(E1000_I210_TQAVCTRL, val);
1972 netdev_dbg(netdev, "FQTSS %s\n", (is_fqtss_enabled(adapter)) ?
1973 "enabled" : "disabled");
1977 * igb_configure - configure the hardware for RX and TX
1978 * @adapter: private board structure
1980 static void igb_configure(struct igb_adapter *adapter)
1982 struct net_device *netdev = adapter->netdev;
1985 igb_get_hw_control(adapter);
1986 igb_set_rx_mode(netdev);
1987 igb_setup_tx_mode(adapter);
1989 igb_restore_vlan(adapter);
1991 igb_setup_tctl(adapter);
1992 igb_setup_mrqc(adapter);
1993 igb_setup_rctl(adapter);
1995 igb_nfc_filter_restore(adapter);
1996 igb_configure_tx(adapter);
1997 igb_configure_rx(adapter);
1999 igb_rx_fifo_flush_82575(&adapter->hw);
2001 /* call igb_desc_unused which always leaves
2002 * at least 1 descriptor unused to make sure
2003 * next_to_use != next_to_clean
2005 for (i = 0; i < adapter->num_rx_queues; i++) {
2006 struct igb_ring *ring = adapter->rx_ring[i];
2007 igb_alloc_rx_buffers(ring, igb_desc_unused(ring));
2012 * igb_power_up_link - Power up the phy/serdes link
2013 * @adapter: address of board private structure
2015 void igb_power_up_link(struct igb_adapter *adapter)
2017 igb_reset_phy(&adapter->hw);
2019 if (adapter->hw.phy.media_type == e1000_media_type_copper)
2020 igb_power_up_phy_copper(&adapter->hw);
2022 igb_power_up_serdes_link_82575(&adapter->hw);
2024 igb_setup_link(&adapter->hw);
2028 * igb_power_down_link - Power down the phy/serdes link
2029 * @adapter: address of board private structure
2031 static void igb_power_down_link(struct igb_adapter *adapter)
2033 if (adapter->hw.phy.media_type == e1000_media_type_copper)
2034 igb_power_down_phy_copper_82575(&adapter->hw);
2036 igb_shutdown_serdes_link_82575(&adapter->hw);
2040 * igb_check_swap_media - Detect and switch function for Media Auto Sense
2041 * @adapter: address of the board private structure
2043 static void igb_check_swap_media(struct igb_adapter *adapter)
2045 struct e1000_hw *hw = &adapter->hw;
2046 u32 ctrl_ext, connsw;
2047 bool swap_now = false;
2049 ctrl_ext = rd32(E1000_CTRL_EXT);
2050 connsw = rd32(E1000_CONNSW);
2052 /* need to live swap if current media is copper and we have fiber/serdes
2056 if ((hw->phy.media_type == e1000_media_type_copper) &&
2057 (!(connsw & E1000_CONNSW_AUTOSENSE_EN))) {
2059 } else if ((hw->phy.media_type != e1000_media_type_copper) &&
2060 !(connsw & E1000_CONNSW_SERDESD)) {
2061 /* copper signal takes time to appear */
2062 if (adapter->copper_tries < 4) {
2063 adapter->copper_tries++;
2064 connsw |= E1000_CONNSW_AUTOSENSE_CONF;
2065 wr32(E1000_CONNSW, connsw);
2068 adapter->copper_tries = 0;
2069 if ((connsw & E1000_CONNSW_PHYSD) &&
2070 (!(connsw & E1000_CONNSW_PHY_PDN))) {
2072 connsw &= ~E1000_CONNSW_AUTOSENSE_CONF;
2073 wr32(E1000_CONNSW, connsw);
2081 switch (hw->phy.media_type) {
2082 case e1000_media_type_copper:
2083 netdev_info(adapter->netdev,
2084 "MAS: changing media to fiber/serdes\n");
2086 E1000_CTRL_EXT_LINK_MODE_PCIE_SERDES;
2087 adapter->flags |= IGB_FLAG_MEDIA_RESET;
2088 adapter->copper_tries = 0;
2090 case e1000_media_type_internal_serdes:
2091 case e1000_media_type_fiber:
2092 netdev_info(adapter->netdev,
2093 "MAS: changing media to copper\n");
2095 ~E1000_CTRL_EXT_LINK_MODE_PCIE_SERDES;
2096 adapter->flags |= IGB_FLAG_MEDIA_RESET;
2099 /* shouldn't get here during regular operation */
2100 netdev_err(adapter->netdev,
2101 "AMS: Invalid media type found, returning\n");
2104 wr32(E1000_CTRL_EXT, ctrl_ext);
2108 * igb_up - Open the interface and prepare it to handle traffic
2109 * @adapter: board private structure
2111 int igb_up(struct igb_adapter *adapter)
2113 struct e1000_hw *hw = &adapter->hw;
2116 /* hardware has been reset, we need to reload some things */
2117 igb_configure(adapter);
2119 clear_bit(__IGB_DOWN, &adapter->state);
2121 for (i = 0; i < adapter->num_q_vectors; i++)
2122 napi_enable(&(adapter->q_vector[i]->napi));
2124 if (adapter->flags & IGB_FLAG_HAS_MSIX)
2125 igb_configure_msix(adapter);
2127 igb_assign_vector(adapter->q_vector[0], 0);
2129 /* Clear any pending interrupts. */
2132 igb_irq_enable(adapter);
2134 /* notify VFs that reset has been completed */
2135 if (adapter->vfs_allocated_count) {
2136 u32 reg_data = rd32(E1000_CTRL_EXT);
2138 reg_data |= E1000_CTRL_EXT_PFRSTD;
2139 wr32(E1000_CTRL_EXT, reg_data);
2142 netif_tx_start_all_queues(adapter->netdev);
2144 /* start the watchdog. */
2145 hw->mac.get_link_status = 1;
2146 schedule_work(&adapter->watchdog_task);
2148 if ((adapter->flags & IGB_FLAG_EEE) &&
2149 (!hw->dev_spec._82575.eee_disable))
2150 adapter->eee_advert = MDIO_EEE_100TX | MDIO_EEE_1000T;
2155 void igb_down(struct igb_adapter *adapter)
2157 struct net_device *netdev = adapter->netdev;
2158 struct e1000_hw *hw = &adapter->hw;
2162 /* signal that we're down so the interrupt handler does not
2163 * reschedule our watchdog timer
2165 set_bit(__IGB_DOWN, &adapter->state);
2167 /* disable receives in the hardware */
2168 rctl = rd32(E1000_RCTL);
2169 wr32(E1000_RCTL, rctl & ~E1000_RCTL_EN);
2170 /* flush and sleep below */
2172 igb_nfc_filter_exit(adapter);
2174 netif_carrier_off(netdev);
2175 netif_tx_stop_all_queues(netdev);
2177 /* disable transmits in the hardware */
2178 tctl = rd32(E1000_TCTL);
2179 tctl &= ~E1000_TCTL_EN;
2180 wr32(E1000_TCTL, tctl);
2181 /* flush both disables and wait for them to finish */
2183 usleep_range(10000, 11000);
2185 igb_irq_disable(adapter);
2187 adapter->flags &= ~IGB_FLAG_NEED_LINK_UPDATE;
2189 for (i = 0; i < adapter->num_q_vectors; i++) {
2190 if (adapter->q_vector[i]) {
2191 napi_synchronize(&adapter->q_vector[i]->napi);
2192 napi_disable(&adapter->q_vector[i]->napi);
2196 del_timer_sync(&adapter->watchdog_timer);
2197 del_timer_sync(&adapter->phy_info_timer);
2199 /* record the stats before reset*/
2200 spin_lock(&adapter->stats64_lock);
2201 igb_update_stats(adapter);
2202 spin_unlock(&adapter->stats64_lock);
2204 adapter->link_speed = 0;
2205 adapter->link_duplex = 0;
2207 if (!pci_channel_offline(adapter->pdev))
2210 /* clear VLAN promisc flag so VFTA will be updated if necessary */
2211 adapter->flags &= ~IGB_FLAG_VLAN_PROMISC;
2213 igb_clean_all_tx_rings(adapter);
2214 igb_clean_all_rx_rings(adapter);
2215 #ifdef CONFIG_IGB_DCA
2217 /* since we reset the hardware DCA settings were cleared */
2218 igb_setup_dca(adapter);
2222 void igb_reinit_locked(struct igb_adapter *adapter)
2224 while (test_and_set_bit(__IGB_RESETTING, &adapter->state))
2225 usleep_range(1000, 2000);
2228 clear_bit(__IGB_RESETTING, &adapter->state);
2231 /** igb_enable_mas - Media Autosense re-enable after swap
2233 * @adapter: adapter struct
2235 static void igb_enable_mas(struct igb_adapter *adapter)
2237 struct e1000_hw *hw = &adapter->hw;
2238 u32 connsw = rd32(E1000_CONNSW);
2240 /* configure for SerDes media detect */
2241 if ((hw->phy.media_type == e1000_media_type_copper) &&
2242 (!(connsw & E1000_CONNSW_SERDESD))) {
2243 connsw |= E1000_CONNSW_ENRGSRC;
2244 connsw |= E1000_CONNSW_AUTOSENSE_EN;
2245 wr32(E1000_CONNSW, connsw);
2250 void igb_reset(struct igb_adapter *adapter)
2252 struct pci_dev *pdev = adapter->pdev;
2253 struct e1000_hw *hw = &adapter->hw;
2254 struct e1000_mac_info *mac = &hw->mac;
2255 struct e1000_fc_info *fc = &hw->fc;
2258 /* Repartition Pba for greater than 9k mtu
2259 * To take effect CTRL.RST is required.
2261 switch (mac->type) {
2265 pba = rd32(E1000_RXPBS);
2266 pba = igb_rxpbs_adjust_82580(pba);
2269 pba = rd32(E1000_RXPBS);
2270 pba &= E1000_RXPBS_SIZE_MASK_82576;
2276 pba = E1000_PBA_34K;
2280 if (mac->type == e1000_82575) {
2281 u32 min_rx_space, min_tx_space, needed_tx_space;
2283 /* write Rx PBA so that hardware can report correct Tx PBA */
2284 wr32(E1000_PBA, pba);
2286 /* To maintain wire speed transmits, the Tx FIFO should be
2287 * large enough to accommodate two full transmit packets,
2288 * rounded up to the next 1KB and expressed in KB. Likewise,
2289 * the Rx FIFO should be large enough to accommodate at least
2290 * one full receive packet and is similarly rounded up and
2293 min_rx_space = DIV_ROUND_UP(MAX_JUMBO_FRAME_SIZE, 1024);
2295 /* The Tx FIFO also stores 16 bytes of information about the Tx
2296 * but don't include Ethernet FCS because hardware appends it.
2297 * We only need to round down to the nearest 512 byte block
2298 * count since the value we care about is 2 frames, not 1.
2300 min_tx_space = adapter->max_frame_size;
2301 min_tx_space += sizeof(union e1000_adv_tx_desc) - ETH_FCS_LEN;
2302 min_tx_space = DIV_ROUND_UP(min_tx_space, 512);
2304 /* upper 16 bits has Tx packet buffer allocation size in KB */
2305 needed_tx_space = min_tx_space - (rd32(E1000_PBA) >> 16);
2307 /* If current Tx allocation is less than the min Tx FIFO size,
2308 * and the min Tx FIFO size is less than the current Rx FIFO
2309 * allocation, take space away from current Rx allocation.
2311 if (needed_tx_space < pba) {
2312 pba -= needed_tx_space;
2314 /* if short on Rx space, Rx wins and must trump Tx
2317 if (pba < min_rx_space)
2321 /* adjust PBA for jumbo frames */
2322 wr32(E1000_PBA, pba);
2325 /* flow control settings
2326 * The high water mark must be low enough to fit one full frame
2327 * after transmitting the pause frame. As such we must have enough
2328 * space to allow for us to complete our current transmit and then
2329 * receive the frame that is in progress from the link partner.
2331 * - the full Rx FIFO size minus one full Tx plus one full Rx frame
2333 hwm = (pba << 10) - (adapter->max_frame_size + MAX_JUMBO_FRAME_SIZE);
2335 fc->high_water = hwm & 0xFFFFFFF0; /* 16-byte granularity */
2336 fc->low_water = fc->high_water - 16;
2337 fc->pause_time = 0xFFFF;
2339 fc->current_mode = fc->requested_mode;
2341 /* disable receive for all VFs and wait one second */
2342 if (adapter->vfs_allocated_count) {
2345 for (i = 0 ; i < adapter->vfs_allocated_count; i++)
2346 adapter->vf_data[i].flags &= IGB_VF_FLAG_PF_SET_MAC;
2348 /* ping all the active vfs to let them know we are going down */
2349 igb_ping_all_vfs(adapter);
2351 /* disable transmits and receives */
2352 wr32(E1000_VFRE, 0);
2353 wr32(E1000_VFTE, 0);
2356 /* Allow time for pending master requests to run */
2357 hw->mac.ops.reset_hw(hw);
2360 if (adapter->flags & IGB_FLAG_MEDIA_RESET) {
2361 /* need to resetup here after media swap */
2362 adapter->ei.get_invariants(hw);
2363 adapter->flags &= ~IGB_FLAG_MEDIA_RESET;
2365 if ((mac->type == e1000_82575 || mac->type == e1000_i350) &&
2366 (adapter->flags & IGB_FLAG_MAS_ENABLE)) {
2367 igb_enable_mas(adapter);
2369 if (hw->mac.ops.init_hw(hw))
2370 dev_err(&pdev->dev, "Hardware Error\n");
2372 /* RAR registers were cleared during init_hw, clear mac table */
2373 igb_flush_mac_table(adapter);
2374 __dev_uc_unsync(adapter->netdev, NULL);
2376 /* Recover default RAR entry */
2377 igb_set_default_mac_filter(adapter);
2379 /* Flow control settings reset on hardware reset, so guarantee flow
2380 * control is off when forcing speed.
2382 if (!hw->mac.autoneg)
2383 igb_force_mac_fc(hw);
2385 igb_init_dmac(adapter, pba);
2386 #ifdef CONFIG_IGB_HWMON
2387 /* Re-initialize the thermal sensor on i350 devices. */
2388 if (!test_bit(__IGB_DOWN, &adapter->state)) {
2389 if (mac->type == e1000_i350 && hw->bus.func == 0) {
2390 /* If present, re-initialize the external thermal sensor
2394 mac->ops.init_thermal_sensor_thresh(hw);
2398 /* Re-establish EEE setting */
2399 if (hw->phy.media_type == e1000_media_type_copper) {
2400 switch (mac->type) {
2404 igb_set_eee_i350(hw, true, true);
2407 igb_set_eee_i354(hw, true, true);
2413 if (!netif_running(adapter->netdev))
2414 igb_power_down_link(adapter);
2416 igb_update_mng_vlan(adapter);
2418 /* Enable h/w to recognize an 802.1Q VLAN Ethernet packet */
2419 wr32(E1000_VET, ETHERNET_IEEE_VLAN_TYPE);
2421 /* Re-enable PTP, where applicable. */
2422 if (adapter->ptp_flags & IGB_PTP_ENABLED)
2423 igb_ptp_reset(adapter);
2425 igb_get_phy_info(hw);
2428 static netdev_features_t igb_fix_features(struct net_device *netdev,
2429 netdev_features_t features)
2431 /* Since there is no support for separate Rx/Tx vlan accel
2432 * enable/disable make sure Tx flag is always in same state as Rx.
2434 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2435 features |= NETIF_F_HW_VLAN_CTAG_TX;
2437 features &= ~NETIF_F_HW_VLAN_CTAG_TX;
2442 static int igb_set_features(struct net_device *netdev,
2443 netdev_features_t features)
2445 netdev_features_t changed = netdev->features ^ features;
2446 struct igb_adapter *adapter = netdev_priv(netdev);
2448 if (changed & NETIF_F_HW_VLAN_CTAG_RX)
2449 igb_vlan_mode(netdev, features);
2451 if (!(changed & (NETIF_F_RXALL | NETIF_F_NTUPLE)))
2454 if (!(features & NETIF_F_NTUPLE)) {
2455 struct hlist_node *node2;
2456 struct igb_nfc_filter *rule;
2458 spin_lock(&adapter->nfc_lock);
2459 hlist_for_each_entry_safe(rule, node2,
2460 &adapter->nfc_filter_list, nfc_node) {
2461 igb_erase_filter(adapter, rule);
2462 hlist_del(&rule->nfc_node);
2465 spin_unlock(&adapter->nfc_lock);
2466 adapter->nfc_filter_count = 0;
2469 netdev->features = features;
2471 if (netif_running(netdev))
2472 igb_reinit_locked(adapter);
2479 static int igb_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
2480 struct net_device *dev,
2481 const unsigned char *addr, u16 vid,
2483 struct netlink_ext_ack *extack)
2485 /* guarantee we can provide a unique filter for the unicast address */
2486 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) {
2487 struct igb_adapter *adapter = netdev_priv(dev);
2488 int vfn = adapter->vfs_allocated_count;
2490 if (netdev_uc_count(dev) >= igb_available_rars(adapter, vfn))
2494 return ndo_dflt_fdb_add(ndm, tb, dev, addr, vid, flags);
2497 #define IGB_MAX_MAC_HDR_LEN 127
2498 #define IGB_MAX_NETWORK_HDR_LEN 511
2500 static netdev_features_t
2501 igb_features_check(struct sk_buff *skb, struct net_device *dev,
2502 netdev_features_t features)
2504 unsigned int network_hdr_len, mac_hdr_len;
2506 /* Make certain the headers can be described by a context descriptor */
2507 mac_hdr_len = skb_network_header(skb) - skb->data;
2508 if (unlikely(mac_hdr_len > IGB_MAX_MAC_HDR_LEN))
2509 return features & ~(NETIF_F_HW_CSUM |
2511 NETIF_F_GSO_UDP_L4 |
2512 NETIF_F_HW_VLAN_CTAG_TX |
2516 network_hdr_len = skb_checksum_start(skb) - skb_network_header(skb);
2517 if (unlikely(network_hdr_len > IGB_MAX_NETWORK_HDR_LEN))
2518 return features & ~(NETIF_F_HW_CSUM |
2520 NETIF_F_GSO_UDP_L4 |
2524 /* We can only support IPV4 TSO in tunnels if we can mangle the
2525 * inner IP ID field, so strip TSO if MANGLEID is not supported.
2527 if (skb->encapsulation && !(features & NETIF_F_TSO_MANGLEID))
2528 features &= ~NETIF_F_TSO;
2533 static void igb_offload_apply(struct igb_adapter *adapter, s32 queue)
2535 if (!is_fqtss_enabled(adapter)) {
2536 enable_fqtss(adapter, true);
2540 igb_config_tx_modes(adapter, queue);
2542 if (!is_any_cbs_enabled(adapter) && !is_any_txtime_enabled(adapter))
2543 enable_fqtss(adapter, false);
2546 static int igb_offload_cbs(struct igb_adapter *adapter,
2547 struct tc_cbs_qopt_offload *qopt)
2549 struct e1000_hw *hw = &adapter->hw;
2552 /* CBS offloading is only supported by i210 controller. */
2553 if (hw->mac.type != e1000_i210)
2556 /* CBS offloading is only supported by queue 0 and queue 1. */
2557 if (qopt->queue < 0 || qopt->queue > 1)
2560 err = igb_save_cbs_params(adapter, qopt->queue, qopt->enable,
2561 qopt->idleslope, qopt->sendslope,
2562 qopt->hicredit, qopt->locredit);
2566 igb_offload_apply(adapter, qopt->queue);
2571 #define ETHER_TYPE_FULL_MASK ((__force __be16)~0)
2572 #define VLAN_PRIO_FULL_MASK (0x07)
2574 static int igb_parse_cls_flower(struct igb_adapter *adapter,
2575 struct flow_cls_offload *f,
2577 struct igb_nfc_filter *input)
2579 struct flow_rule *rule = flow_cls_offload_flow_rule(f);
2580 struct flow_dissector *dissector = rule->match.dissector;
2581 struct netlink_ext_ack *extack = f->common.extack;
2583 if (dissector->used_keys &
2584 ~(BIT(FLOW_DISSECTOR_KEY_BASIC) |
2585 BIT(FLOW_DISSECTOR_KEY_CONTROL) |
2586 BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
2587 BIT(FLOW_DISSECTOR_KEY_VLAN))) {
2588 NL_SET_ERR_MSG_MOD(extack,
2589 "Unsupported key used, only BASIC, CONTROL, ETH_ADDRS and VLAN are supported");
2593 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
2594 struct flow_match_eth_addrs match;
2596 flow_rule_match_eth_addrs(rule, &match);
2597 if (!is_zero_ether_addr(match.mask->dst)) {
2598 if (!is_broadcast_ether_addr(match.mask->dst)) {
2599 NL_SET_ERR_MSG_MOD(extack, "Only full masks are supported for destination MAC address");
2603 input->filter.match_flags |=
2604 IGB_FILTER_FLAG_DST_MAC_ADDR;
2605 ether_addr_copy(input->filter.dst_addr, match.key->dst);
2608 if (!is_zero_ether_addr(match.mask->src)) {
2609 if (!is_broadcast_ether_addr(match.mask->src)) {
2610 NL_SET_ERR_MSG_MOD(extack, "Only full masks are supported for source MAC address");
2614 input->filter.match_flags |=
2615 IGB_FILTER_FLAG_SRC_MAC_ADDR;
2616 ether_addr_copy(input->filter.src_addr, match.key->src);
2620 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
2621 struct flow_match_basic match;
2623 flow_rule_match_basic(rule, &match);
2624 if (match.mask->n_proto) {
2625 if (match.mask->n_proto != ETHER_TYPE_FULL_MASK) {
2626 NL_SET_ERR_MSG_MOD(extack, "Only full mask is supported for EtherType filter");
2630 input->filter.match_flags |= IGB_FILTER_FLAG_ETHER_TYPE;
2631 input->filter.etype = match.key->n_proto;
2635 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
2636 struct flow_match_vlan match;
2638 flow_rule_match_vlan(rule, &match);
2639 if (match.mask->vlan_priority) {
2640 if (match.mask->vlan_priority != VLAN_PRIO_FULL_MASK) {
2641 NL_SET_ERR_MSG_MOD(extack, "Only full mask is supported for VLAN priority");
2645 input->filter.match_flags |= IGB_FILTER_FLAG_VLAN_TCI;
2646 input->filter.vlan_tci =
2647 (__force __be16)match.key->vlan_priority;
2651 input->action = traffic_class;
2652 input->cookie = f->cookie;
2657 static int igb_configure_clsflower(struct igb_adapter *adapter,
2658 struct flow_cls_offload *cls_flower)
2660 struct netlink_ext_ack *extack = cls_flower->common.extack;
2661 struct igb_nfc_filter *filter, *f;
2664 tc = tc_classid_to_hwtc(adapter->netdev, cls_flower->classid);
2666 NL_SET_ERR_MSG_MOD(extack, "Invalid traffic class");
2670 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
2674 err = igb_parse_cls_flower(adapter, cls_flower, tc, filter);
2678 spin_lock(&adapter->nfc_lock);
2680 hlist_for_each_entry(f, &adapter->nfc_filter_list, nfc_node) {
2681 if (!memcmp(&f->filter, &filter->filter, sizeof(f->filter))) {
2683 NL_SET_ERR_MSG_MOD(extack,
2684 "This filter is already set in ethtool");
2689 hlist_for_each_entry(f, &adapter->cls_flower_list, nfc_node) {
2690 if (!memcmp(&f->filter, &filter->filter, sizeof(f->filter))) {
2692 NL_SET_ERR_MSG_MOD(extack,
2693 "This filter is already set in cls_flower");
2698 err = igb_add_filter(adapter, filter);
2700 NL_SET_ERR_MSG_MOD(extack, "Could not add filter to the adapter");
2704 hlist_add_head(&filter->nfc_node, &adapter->cls_flower_list);
2706 spin_unlock(&adapter->nfc_lock);
2711 spin_unlock(&adapter->nfc_lock);
2719 static int igb_delete_clsflower(struct igb_adapter *adapter,
2720 struct flow_cls_offload *cls_flower)
2722 struct igb_nfc_filter *filter;
2725 spin_lock(&adapter->nfc_lock);
2727 hlist_for_each_entry(filter, &adapter->cls_flower_list, nfc_node)
2728 if (filter->cookie == cls_flower->cookie)
2736 err = igb_erase_filter(adapter, filter);
2740 hlist_del(&filter->nfc_node);
2744 spin_unlock(&adapter->nfc_lock);
2749 static int igb_setup_tc_cls_flower(struct igb_adapter *adapter,
2750 struct flow_cls_offload *cls_flower)
2752 switch (cls_flower->command) {
2753 case FLOW_CLS_REPLACE:
2754 return igb_configure_clsflower(adapter, cls_flower);
2755 case FLOW_CLS_DESTROY:
2756 return igb_delete_clsflower(adapter, cls_flower);
2757 case FLOW_CLS_STATS:
2764 static int igb_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
2767 struct igb_adapter *adapter = cb_priv;
2769 if (!tc_cls_can_offload_and_chain0(adapter->netdev, type_data))
2773 case TC_SETUP_CLSFLOWER:
2774 return igb_setup_tc_cls_flower(adapter, type_data);
2781 static int igb_offload_txtime(struct igb_adapter *adapter,
2782 struct tc_etf_qopt_offload *qopt)
2784 struct e1000_hw *hw = &adapter->hw;
2787 /* Launchtime offloading is only supported by i210 controller. */
2788 if (hw->mac.type != e1000_i210)
2791 /* Launchtime offloading is only supported by queues 0 and 1. */
2792 if (qopt->queue < 0 || qopt->queue > 1)
2795 err = igb_save_txtime_params(adapter, qopt->queue, qopt->enable);
2799 igb_offload_apply(adapter, qopt->queue);
2804 static LIST_HEAD(igb_block_cb_list);
2806 static int igb_setup_tc(struct net_device *dev, enum tc_setup_type type,
2809 struct igb_adapter *adapter = netdev_priv(dev);
2812 case TC_SETUP_QDISC_CBS:
2813 return igb_offload_cbs(adapter, type_data);
2814 case TC_SETUP_BLOCK:
2815 return flow_block_cb_setup_simple(type_data,
2817 igb_setup_tc_block_cb,
2818 adapter, adapter, true);
2820 case TC_SETUP_QDISC_ETF:
2821 return igb_offload_txtime(adapter, type_data);
2828 static int igb_xdp_setup(struct net_device *dev, struct netdev_bpf *bpf)
2830 int i, frame_size = dev->mtu + IGB_ETH_PKT_HDR_PAD;
2831 struct igb_adapter *adapter = netdev_priv(dev);
2832 struct bpf_prog *prog = bpf->prog, *old_prog;
2833 bool running = netif_running(dev);
2836 /* verify igb ring attributes are sufficient for XDP */
2837 for (i = 0; i < adapter->num_rx_queues; i++) {
2838 struct igb_ring *ring = adapter->rx_ring[i];
2840 if (frame_size > igb_rx_bufsz(ring)) {
2841 NL_SET_ERR_MSG_MOD(bpf->extack,
2842 "The RX buffer size is too small for the frame size");
2843 netdev_warn(dev, "XDP RX buffer size %d is too small for the frame size %d\n",
2844 igb_rx_bufsz(ring), frame_size);
2849 old_prog = xchg(&adapter->xdp_prog, prog);
2850 need_reset = (!!prog != !!old_prog);
2852 /* device is up and bpf is added/removed, must setup the RX queues */
2853 if (need_reset && running) {
2856 for (i = 0; i < adapter->num_rx_queues; i++)
2857 (void)xchg(&adapter->rx_ring[i]->xdp_prog,
2862 bpf_prog_put(old_prog);
2864 /* bpf is just replaced, RXQ and MTU are already setup */
2874 static int igb_xdp(struct net_device *dev, struct netdev_bpf *xdp)
2876 switch (xdp->command) {
2877 case XDP_SETUP_PROG:
2878 return igb_xdp_setup(dev, xdp);
2884 static void igb_xdp_ring_update_tail(struct igb_ring *ring)
2886 /* Force memory writes to complete before letting h/w know there
2887 * are new descriptors to fetch.
2890 writel(ring->next_to_use, ring->tail);
2893 static struct igb_ring *igb_xdp_tx_queue_mapping(struct igb_adapter *adapter)
2895 unsigned int r_idx = smp_processor_id();
2897 if (r_idx >= adapter->num_tx_queues)
2898 r_idx = r_idx % adapter->num_tx_queues;
2900 return adapter->tx_ring[r_idx];
2903 static int igb_xdp_xmit_back(struct igb_adapter *adapter, struct xdp_buff *xdp)
2905 struct xdp_frame *xdpf = xdp_convert_buff_to_frame(xdp);
2906 int cpu = smp_processor_id();
2907 struct igb_ring *tx_ring;
2908 struct netdev_queue *nq;
2911 if (unlikely(!xdpf))
2912 return IGB_XDP_CONSUMED;
2914 /* During program transitions its possible adapter->xdp_prog is assigned
2915 * but ring has not been configured yet. In this case simply abort xmit.
2917 tx_ring = adapter->xdp_prog ? igb_xdp_tx_queue_mapping(adapter) : NULL;
2918 if (unlikely(!tx_ring))
2919 return IGB_XDP_CONSUMED;
2921 nq = txring_txq(tx_ring);
2922 __netif_tx_lock(nq, cpu);
2923 /* Avoid transmit queue timeout since we share it with the slow path */
2924 nq->trans_start = jiffies;
2925 ret = igb_xmit_xdp_ring(adapter, tx_ring, xdpf);
2926 __netif_tx_unlock(nq);
2931 static int igb_xdp_xmit(struct net_device *dev, int n,
2932 struct xdp_frame **frames, u32 flags)
2934 struct igb_adapter *adapter = netdev_priv(dev);
2935 int cpu = smp_processor_id();
2936 struct igb_ring *tx_ring;
2937 struct netdev_queue *nq;
2941 if (unlikely(test_bit(__IGB_DOWN, &adapter->state)))
2944 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
2947 /* During program transitions its possible adapter->xdp_prog is assigned
2948 * but ring has not been configured yet. In this case simply abort xmit.
2950 tx_ring = adapter->xdp_prog ? igb_xdp_tx_queue_mapping(adapter) : NULL;
2951 if (unlikely(!tx_ring))
2954 nq = txring_txq(tx_ring);
2955 __netif_tx_lock(nq, cpu);
2957 /* Avoid transmit queue timeout since we share it with the slow path */
2958 nq->trans_start = jiffies;
2960 for (i = 0; i < n; i++) {
2961 struct xdp_frame *xdpf = frames[i];
2964 err = igb_xmit_xdp_ring(adapter, tx_ring, xdpf);
2965 if (err != IGB_XDP_TX)
2970 __netif_tx_unlock(nq);
2972 if (unlikely(flags & XDP_XMIT_FLUSH))
2973 igb_xdp_ring_update_tail(tx_ring);
2978 static const struct net_device_ops igb_netdev_ops = {
2979 .ndo_open = igb_open,
2980 .ndo_stop = igb_close,
2981 .ndo_start_xmit = igb_xmit_frame,
2982 .ndo_get_stats64 = igb_get_stats64,
2983 .ndo_set_rx_mode = igb_set_rx_mode,
2984 .ndo_set_mac_address = igb_set_mac,
2985 .ndo_change_mtu = igb_change_mtu,
2986 .ndo_do_ioctl = igb_ioctl,
2987 .ndo_tx_timeout = igb_tx_timeout,
2988 .ndo_validate_addr = eth_validate_addr,
2989 .ndo_vlan_rx_add_vid = igb_vlan_rx_add_vid,
2990 .ndo_vlan_rx_kill_vid = igb_vlan_rx_kill_vid,
2991 .ndo_set_vf_mac = igb_ndo_set_vf_mac,
2992 .ndo_set_vf_vlan = igb_ndo_set_vf_vlan,
2993 .ndo_set_vf_rate = igb_ndo_set_vf_bw,
2994 .ndo_set_vf_spoofchk = igb_ndo_set_vf_spoofchk,
2995 .ndo_set_vf_trust = igb_ndo_set_vf_trust,
2996 .ndo_get_vf_config = igb_ndo_get_vf_config,
2997 .ndo_fix_features = igb_fix_features,
2998 .ndo_set_features = igb_set_features,
2999 .ndo_fdb_add = igb_ndo_fdb_add,
3000 .ndo_features_check = igb_features_check,
3001 .ndo_setup_tc = igb_setup_tc,
3003 .ndo_xdp_xmit = igb_xdp_xmit,
3007 * igb_set_fw_version - Configure version string for ethtool
3008 * @adapter: adapter struct
3010 void igb_set_fw_version(struct igb_adapter *adapter)
3012 struct e1000_hw *hw = &adapter->hw;
3013 struct e1000_fw_version fw;
3015 igb_get_fw_version(hw, &fw);
3017 switch (hw->mac.type) {
3020 if (!(igb_get_flash_presence_i210(hw))) {
3021 snprintf(adapter->fw_version,
3022 sizeof(adapter->fw_version),
3024 fw.invm_major, fw.invm_minor,
3030 /* if option is rom valid, display its version too */
3032 snprintf(adapter->fw_version,
3033 sizeof(adapter->fw_version),
3034 "%d.%d, 0x%08x, %d.%d.%d",
3035 fw.eep_major, fw.eep_minor, fw.etrack_id,
3036 fw.or_major, fw.or_build, fw.or_patch);
3038 } else if (fw.etrack_id != 0X0000) {
3039 snprintf(adapter->fw_version,
3040 sizeof(adapter->fw_version),
3042 fw.eep_major, fw.eep_minor, fw.etrack_id);
3044 snprintf(adapter->fw_version,
3045 sizeof(adapter->fw_version),
3047 fw.eep_major, fw.eep_minor, fw.eep_build);
3054 * igb_init_mas - init Media Autosense feature if enabled in the NVM
3056 * @adapter: adapter struct
3058 static void igb_init_mas(struct igb_adapter *adapter)
3060 struct e1000_hw *hw = &adapter->hw;
3063 hw->nvm.ops.read(hw, NVM_COMPAT, 1, &eeprom_data);
3064 switch (hw->bus.func) {
3066 if (eeprom_data & IGB_MAS_ENABLE_0) {
3067 adapter->flags |= IGB_FLAG_MAS_ENABLE;
3068 netdev_info(adapter->netdev,
3069 "MAS: Enabling Media Autosense for port %d\n",
3074 if (eeprom_data & IGB_MAS_ENABLE_1) {
3075 adapter->flags |= IGB_FLAG_MAS_ENABLE;
3076 netdev_info(adapter->netdev,
3077 "MAS: Enabling Media Autosense for port %d\n",
3082 if (eeprom_data & IGB_MAS_ENABLE_2) {
3083 adapter->flags |= IGB_FLAG_MAS_ENABLE;
3084 netdev_info(adapter->netdev,
3085 "MAS: Enabling Media Autosense for port %d\n",
3090 if (eeprom_data & IGB_MAS_ENABLE_3) {
3091 adapter->flags |= IGB_FLAG_MAS_ENABLE;
3092 netdev_info(adapter->netdev,
3093 "MAS: Enabling Media Autosense for port %d\n",
3098 /* Shouldn't get here */
3099 netdev_err(adapter->netdev,
3100 "MAS: Invalid port configuration, returning\n");
3106 * igb_init_i2c - Init I2C interface
3107 * @adapter: pointer to adapter structure
3109 static s32 igb_init_i2c(struct igb_adapter *adapter)
3113 /* I2C interface supported on i350 devices */
3114 if (adapter->hw.mac.type != e1000_i350)
3117 /* Initialize the i2c bus which is controlled by the registers.
3118 * This bus will use the i2c_algo_bit structure that implements
3119 * the protocol through toggling of the 4 bits in the register.
3121 adapter->i2c_adap.owner = THIS_MODULE;
3122 adapter->i2c_algo = igb_i2c_algo;
3123 adapter->i2c_algo.data = adapter;
3124 adapter->i2c_adap.algo_data = &adapter->i2c_algo;
3125 adapter->i2c_adap.dev.parent = &adapter->pdev->dev;
3126 strlcpy(adapter->i2c_adap.name, "igb BB",
3127 sizeof(adapter->i2c_adap.name));
3128 status = i2c_bit_add_bus(&adapter->i2c_adap);
3133 * igb_probe - Device Initialization Routine
3134 * @pdev: PCI device information struct
3135 * @ent: entry in igb_pci_tbl
3137 * Returns 0 on success, negative on failure
3139 * igb_probe initializes an adapter identified by a pci_dev structure.
3140 * The OS initialization, configuring of the adapter private structure,
3141 * and a hardware reset occur.
3143 static int igb_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
3145 struct net_device *netdev;
3146 struct igb_adapter *adapter;
3147 struct e1000_hw *hw;
3148 u16 eeprom_data = 0;
3150 static int global_quad_port_a; /* global quad port a indication */
3151 const struct e1000_info *ei = igb_info_tbl[ent->driver_data];
3152 int err, pci_using_dac;
3153 u8 part_str[E1000_PBANUM_LENGTH];
3155 /* Catch broken hardware that put the wrong VF device ID in
3156 * the PCIe SR-IOV capability.
3158 if (pdev->is_virtfn) {
3159 WARN(1, KERN_ERR "%s (%x:%x) should not be a VF!\n",
3160 pci_name(pdev), pdev->vendor, pdev->device);
3164 err = pci_enable_device_mem(pdev);
3169 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
3173 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
3176 "No usable DMA configuration, aborting\n");
3181 err = pci_request_mem_regions(pdev, igb_driver_name);
3185 pci_enable_pcie_error_reporting(pdev);
3187 pci_set_master(pdev);
3188 pci_save_state(pdev);
3191 netdev = alloc_etherdev_mq(sizeof(struct igb_adapter),
3194 goto err_alloc_etherdev;
3196 SET_NETDEV_DEV(netdev, &pdev->dev);
3198 pci_set_drvdata(pdev, netdev);
3199 adapter = netdev_priv(netdev);
3200 adapter->netdev = netdev;
3201 adapter->pdev = pdev;
3204 adapter->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
3207 adapter->io_addr = pci_iomap(pdev, 0, 0);
3208 if (!adapter->io_addr)
3210 /* hw->hw_addr can be altered, we'll use adapter->io_addr for unmap */
3211 hw->hw_addr = adapter->io_addr;
3213 netdev->netdev_ops = &igb_netdev_ops;
3214 igb_set_ethtool_ops(netdev);
3215 netdev->watchdog_timeo = 5 * HZ;
3217 strncpy(netdev->name, pci_name(pdev), sizeof(netdev->name) - 1);
3219 netdev->mem_start = pci_resource_start(pdev, 0);
3220 netdev->mem_end = pci_resource_end(pdev, 0);
3222 /* PCI config space info */
3223 hw->vendor_id = pdev->vendor;
3224 hw->device_id = pdev->device;
3225 hw->revision_id = pdev->revision;
3226 hw->subsystem_vendor_id = pdev->subsystem_vendor;
3227 hw->subsystem_device_id = pdev->subsystem_device;
3229 /* Copy the default MAC, PHY and NVM function pointers */
3230 memcpy(&hw->mac.ops, ei->mac_ops, sizeof(hw->mac.ops));
3231 memcpy(&hw->phy.ops, ei->phy_ops, sizeof(hw->phy.ops));
3232 memcpy(&hw->nvm.ops, ei->nvm_ops, sizeof(hw->nvm.ops));
3233 /* Initialize skew-specific constants */
3234 err = ei->get_invariants(hw);
3238 /* setup the private structure */
3239 err = igb_sw_init(adapter);
3243 igb_get_bus_info_pcie(hw);
3245 hw->phy.autoneg_wait_to_complete = false;
3247 /* Copper options */
3248 if (hw->phy.media_type == e1000_media_type_copper) {
3249 hw->phy.mdix = AUTO_ALL_MODES;
3250 hw->phy.disable_polarity_correction = false;
3251 hw->phy.ms_type = e1000_ms_hw_default;
3254 if (igb_check_reset_block(hw))
3255 dev_info(&pdev->dev,
3256 "PHY reset is blocked due to SOL/IDER session.\n");
3258 /* features is initialized to 0 in allocation, it might have bits
3259 * set by igb_sw_init so we should use an or instead of an
3262 netdev->features |= NETIF_F_SG |
3269 if (hw->mac.type >= e1000_82576)
3270 netdev->features |= NETIF_F_SCTP_CRC | NETIF_F_GSO_UDP_L4;
3272 if (hw->mac.type >= e1000_i350)
3273 netdev->features |= NETIF_F_HW_TC;
3275 #define IGB_GSO_PARTIAL_FEATURES (NETIF_F_GSO_GRE | \
3276 NETIF_F_GSO_GRE_CSUM | \
3277 NETIF_F_GSO_IPXIP4 | \
3278 NETIF_F_GSO_IPXIP6 | \
3279 NETIF_F_GSO_UDP_TUNNEL | \
3280 NETIF_F_GSO_UDP_TUNNEL_CSUM)
3282 netdev->gso_partial_features = IGB_GSO_PARTIAL_FEATURES;
3283 netdev->features |= NETIF_F_GSO_PARTIAL | IGB_GSO_PARTIAL_FEATURES;
3285 /* copy netdev features into list of user selectable features */
3286 netdev->hw_features |= netdev->features |
3287 NETIF_F_HW_VLAN_CTAG_RX |
3288 NETIF_F_HW_VLAN_CTAG_TX |
3291 if (hw->mac.type >= e1000_i350)
3292 netdev->hw_features |= NETIF_F_NTUPLE;
3295 netdev->features |= NETIF_F_HIGHDMA;
3297 netdev->vlan_features |= netdev->features | NETIF_F_TSO_MANGLEID;
3298 netdev->mpls_features |= NETIF_F_HW_CSUM;
3299 netdev->hw_enc_features |= netdev->vlan_features;
3301 /* set this bit last since it cannot be part of vlan_features */
3302 netdev->features |= NETIF_F_HW_VLAN_CTAG_FILTER |
3303 NETIF_F_HW_VLAN_CTAG_RX |
3304 NETIF_F_HW_VLAN_CTAG_TX;
3306 netdev->priv_flags |= IFF_SUPP_NOFCS;
3308 netdev->priv_flags |= IFF_UNICAST_FLT;
3310 /* MTU range: 68 - 9216 */
3311 netdev->min_mtu = ETH_MIN_MTU;
3312 netdev->max_mtu = MAX_STD_JUMBO_FRAME_SIZE;
3314 adapter->en_mng_pt = igb_enable_mng_pass_thru(hw);
3316 /* before reading the NVM, reset the controller to put the device in a
3317 * known good starting state
3319 hw->mac.ops.reset_hw(hw);
3321 /* make sure the NVM is good , i211/i210 parts can have special NVM
3322 * that doesn't contain a checksum
3324 switch (hw->mac.type) {
3327 if (igb_get_flash_presence_i210(hw)) {
3328 if (hw->nvm.ops.validate(hw) < 0) {
3330 "The NVM Checksum Is Not Valid\n");
3337 if (hw->nvm.ops.validate(hw) < 0) {
3338 dev_err(&pdev->dev, "The NVM Checksum Is Not Valid\n");
3345 if (eth_platform_get_mac_address(&pdev->dev, hw->mac.addr)) {
3346 /* copy the MAC address out of the NVM */
3347 if (hw->mac.ops.read_mac_addr(hw))
3348 dev_err(&pdev->dev, "NVM Read Error\n");
3351 memcpy(netdev->dev_addr, hw->mac.addr, netdev->addr_len);
3353 if (!is_valid_ether_addr(netdev->dev_addr)) {
3354 dev_err(&pdev->dev, "Invalid MAC Address\n");
3359 igb_set_default_mac_filter(adapter);
3361 /* get firmware version for ethtool -i */
3362 igb_set_fw_version(adapter);
3364 /* configure RXPBSIZE and TXPBSIZE */
3365 if (hw->mac.type == e1000_i210) {
3366 wr32(E1000_RXPBS, I210_RXPBSIZE_DEFAULT);
3367 wr32(E1000_TXPBS, I210_TXPBSIZE_DEFAULT);
3370 timer_setup(&adapter->watchdog_timer, igb_watchdog, 0);
3371 timer_setup(&adapter->phy_info_timer, igb_update_phy_info, 0);
3373 INIT_WORK(&adapter->reset_task, igb_reset_task);
3374 INIT_WORK(&adapter->watchdog_task, igb_watchdog_task);
3376 /* Initialize link properties that are user-changeable */
3377 adapter->fc_autoneg = true;
3378 hw->mac.autoneg = true;
3379 hw->phy.autoneg_advertised = 0x2f;
3381 hw->fc.requested_mode = e1000_fc_default;
3382 hw->fc.current_mode = e1000_fc_default;
3384 igb_validate_mdi_setting(hw);
3386 /* By default, support wake on port A */
3387 if (hw->bus.func == 0)
3388 adapter->flags |= IGB_FLAG_WOL_SUPPORTED;
3390 /* Check the NVM for wake support on non-port A ports */
3391 if (hw->mac.type >= e1000_82580)
3392 hw->nvm.ops.read(hw, NVM_INIT_CONTROL3_PORT_A +
3393 NVM_82580_LAN_FUNC_OFFSET(hw->bus.func), 1,
3395 else if (hw->bus.func == 1)
3396 hw->nvm.ops.read(hw, NVM_INIT_CONTROL3_PORT_B, 1, &eeprom_data);
3398 if (eeprom_data & IGB_EEPROM_APME)
3399 adapter->flags |= IGB_FLAG_WOL_SUPPORTED;
3401 /* now that we have the eeprom settings, apply the special cases where
3402 * the eeprom may be wrong or the board simply won't support wake on
3403 * lan on a particular port
3405 switch (pdev->device) {
3406 case E1000_DEV_ID_82575GB_QUAD_COPPER:
3407 adapter->flags &= ~IGB_FLAG_WOL_SUPPORTED;
3409 case E1000_DEV_ID_82575EB_FIBER_SERDES:
3410 case E1000_DEV_ID_82576_FIBER:
3411 case E1000_DEV_ID_82576_SERDES:
3412 /* Wake events only supported on port A for dual fiber
3413 * regardless of eeprom setting
3415 if (rd32(E1000_STATUS) & E1000_STATUS_FUNC_1)
3416 adapter->flags &= ~IGB_FLAG_WOL_SUPPORTED;
3418 case E1000_DEV_ID_82576_QUAD_COPPER:
3419 case E1000_DEV_ID_82576_QUAD_COPPER_ET2:
3420 /* if quad port adapter, disable WoL on all but port A */
3421 if (global_quad_port_a != 0)
3422 adapter->flags &= ~IGB_FLAG_WOL_SUPPORTED;
3424 adapter->flags |= IGB_FLAG_QUAD_PORT_A;
3425 /* Reset for multiple quad port adapters */
3426 if (++global_quad_port_a == 4)
3427 global_quad_port_a = 0;
3430 /* If the device can't wake, don't set software support */
3431 if (!device_can_wakeup(&adapter->pdev->dev))
3432 adapter->flags &= ~IGB_FLAG_WOL_SUPPORTED;
3435 /* initialize the wol settings based on the eeprom settings */
3436 if (adapter->flags & IGB_FLAG_WOL_SUPPORTED)
3437 adapter->wol |= E1000_WUFC_MAG;
3439 /* Some vendors want WoL disabled by default, but still supported */
3440 if ((hw->mac.type == e1000_i350) &&
3441 (pdev->subsystem_vendor == PCI_VENDOR_ID_HP)) {
3442 adapter->flags |= IGB_FLAG_WOL_SUPPORTED;
3446 /* Some vendors want the ability to Use the EEPROM setting as
3447 * enable/disable only, and not for capability
3449 if (((hw->mac.type == e1000_i350) ||
3450 (hw->mac.type == e1000_i354)) &&
3451 (pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)) {
3452 adapter->flags |= IGB_FLAG_WOL_SUPPORTED;
3455 if (hw->mac.type == e1000_i350) {
3456 if (((pdev->subsystem_device == 0x5001) ||
3457 (pdev->subsystem_device == 0x5002)) &&
3458 (hw->bus.func == 0)) {
3459 adapter->flags |= IGB_FLAG_WOL_SUPPORTED;
3462 if (pdev->subsystem_device == 0x1F52)
3463 adapter->flags |= IGB_FLAG_WOL_SUPPORTED;
3466 device_set_wakeup_enable(&adapter->pdev->dev,
3467 adapter->flags & IGB_FLAG_WOL_SUPPORTED);
3469 /* reset the hardware with the new settings */
3472 /* Init the I2C interface */
3473 err = igb_init_i2c(adapter);
3475 dev_err(&pdev->dev, "failed to init i2c interface\n");
3479 /* let the f/w know that the h/w is now under the control of the
3482 igb_get_hw_control(adapter);
3484 strcpy(netdev->name, "eth%d");
3485 err = register_netdev(netdev);
3489 /* carrier off reporting is important to ethtool even BEFORE open */
3490 netif_carrier_off(netdev);
3492 #ifdef CONFIG_IGB_DCA
3493 if (dca_add_requester(&pdev->dev) == 0) {
3494 adapter->flags |= IGB_FLAG_DCA_ENABLED;
3495 dev_info(&pdev->dev, "DCA enabled\n");
3496 igb_setup_dca(adapter);
3500 #ifdef CONFIG_IGB_HWMON
3501 /* Initialize the thermal sensor on i350 devices. */
3502 if (hw->mac.type == e1000_i350 && hw->bus.func == 0) {
3505 /* Read the NVM to determine if this i350 device supports an
3506 * external thermal sensor.
3508 hw->nvm.ops.read(hw, NVM_ETS_CFG, 1, &ets_word);
3509 if (ets_word != 0x0000 && ets_word != 0xFFFF)
3510 adapter->ets = true;
3512 adapter->ets = false;
3513 if (igb_sysfs_init(adapter))
3515 "failed to allocate sysfs resources\n");
3517 adapter->ets = false;
3520 /* Check if Media Autosense is enabled */
3522 if (hw->dev_spec._82575.mas_capable)
3523 igb_init_mas(adapter);
3525 /* do hw tstamp init after resetting */
3526 igb_ptp_init(adapter);
3528 dev_info(&pdev->dev, "Intel(R) Gigabit Ethernet Network Connection\n");
3529 /* print bus type/speed/width info, not applicable to i354 */
3530 if (hw->mac.type != e1000_i354) {
3531 dev_info(&pdev->dev, "%s: (PCIe:%s:%s) %pM\n",
3533 ((hw->bus.speed == e1000_bus_speed_2500) ? "2.5Gb/s" :
3534 (hw->bus.speed == e1000_bus_speed_5000) ? "5.0Gb/s" :
3536 ((hw->bus.width == e1000_bus_width_pcie_x4) ?
3538 (hw->bus.width == e1000_bus_width_pcie_x2) ?
3540 (hw->bus.width == e1000_bus_width_pcie_x1) ?
3541 "Width x1" : "unknown"), netdev->dev_addr);
3544 if ((hw->mac.type == e1000_82576 &&
3545 rd32(E1000_EECD) & E1000_EECD_PRES) ||
3546 (hw->mac.type >= e1000_i210 ||
3547 igb_get_flash_presence_i210(hw))) {
3548 ret_val = igb_read_part_string(hw, part_str,
3549 E1000_PBANUM_LENGTH);
3551 ret_val = -E1000_ERR_INVM_VALUE_NOT_FOUND;
3555 strcpy(part_str, "Unknown");
3556 dev_info(&pdev->dev, "%s: PBA No: %s\n", netdev->name, part_str);
3557 dev_info(&pdev->dev,
3558 "Using %s interrupts. %d rx queue(s), %d tx queue(s)\n",
3559 (adapter->flags & IGB_FLAG_HAS_MSIX) ? "MSI-X" :
3560 (adapter->flags & IGB_FLAG_HAS_MSI) ? "MSI" : "legacy",
3561 adapter->num_rx_queues, adapter->num_tx_queues);
3562 if (hw->phy.media_type == e1000_media_type_copper) {
3563 switch (hw->mac.type) {
3567 /* Enable EEE for internal copper PHY devices */
3568 err = igb_set_eee_i350(hw, true, true);
3570 (!hw->dev_spec._82575.eee_disable)) {
3571 adapter->eee_advert =
3572 MDIO_EEE_100TX | MDIO_EEE_1000T;
3573 adapter->flags |= IGB_FLAG_EEE;
3577 if ((rd32(E1000_CTRL_EXT) &
3578 E1000_CTRL_EXT_LINK_MODE_SGMII)) {
3579 err = igb_set_eee_i354(hw, true, true);
3581 (!hw->dev_spec._82575.eee_disable)) {
3582 adapter->eee_advert =
3583 MDIO_EEE_100TX | MDIO_EEE_1000T;
3584 adapter->flags |= IGB_FLAG_EEE;
3593 dev_pm_set_driver_flags(&pdev->dev, DPM_FLAG_NO_DIRECT_COMPLETE);
3595 pm_runtime_put_noidle(&pdev->dev);
3599 igb_release_hw_control(adapter);
3600 memset(&adapter->i2c_adap, 0, sizeof(adapter->i2c_adap));
3602 if (!igb_check_reset_block(hw))
3605 if (hw->flash_address)
3606 iounmap(hw->flash_address);
3608 kfree(adapter->mac_table);
3609 kfree(adapter->shadow_vfta);
3610 igb_clear_interrupt_scheme(adapter);
3611 #ifdef CONFIG_PCI_IOV
3612 igb_disable_sriov(pdev);
3614 pci_iounmap(pdev, adapter->io_addr);
3616 free_netdev(netdev);
3618 pci_release_mem_regions(pdev);
3621 pci_disable_device(pdev);
3625 #ifdef CONFIG_PCI_IOV
3626 static int igb_disable_sriov(struct pci_dev *pdev)
3628 struct net_device *netdev = pci_get_drvdata(pdev);
3629 struct igb_adapter *adapter = netdev_priv(netdev);
3630 struct e1000_hw *hw = &adapter->hw;
3632 /* reclaim resources allocated to VFs */
3633 if (adapter->vf_data) {
3634 /* disable iov and allow time for transactions to clear */
3635 if (pci_vfs_assigned(pdev)) {
3636 dev_warn(&pdev->dev,
3637 "Cannot deallocate SR-IOV virtual functions while they are assigned - VFs will not be deallocated\n");
3640 pci_disable_sriov(pdev);
3644 kfree(adapter->vf_mac_list);
3645 adapter->vf_mac_list = NULL;
3646 kfree(adapter->vf_data);
3647 adapter->vf_data = NULL;
3648 adapter->vfs_allocated_count = 0;
3649 wr32(E1000_IOVCTL, E1000_IOVCTL_REUSE_VFQ);
3652 dev_info(&pdev->dev, "IOV Disabled\n");
3654 /* Re-enable DMA Coalescing flag since IOV is turned off */
3655 adapter->flags |= IGB_FLAG_DMAC;
3661 static int igb_enable_sriov(struct pci_dev *pdev, int num_vfs)
3663 struct net_device *netdev = pci_get_drvdata(pdev);
3664 struct igb_adapter *adapter = netdev_priv(netdev);
3665 int old_vfs = pci_num_vf(pdev);
3666 struct vf_mac_filter *mac_list;
3668 int num_vf_mac_filters, i;
3670 if (!(adapter->flags & IGB_FLAG_HAS_MSIX) || num_vfs > 7) {
3678 dev_info(&pdev->dev, "%d pre-allocated VFs found - override max_vfs setting of %d\n",
3680 adapter->vfs_allocated_count = old_vfs;
3682 adapter->vfs_allocated_count = num_vfs;
3684 adapter->vf_data = kcalloc(adapter->vfs_allocated_count,
3685 sizeof(struct vf_data_storage), GFP_KERNEL);
3687 /* if allocation failed then we do not support SR-IOV */
3688 if (!adapter->vf_data) {
3689 adapter->vfs_allocated_count = 0;
3694 /* Due to the limited number of RAR entries calculate potential
3695 * number of MAC filters available for the VFs. Reserve entries
3696 * for PF default MAC, PF MAC filters and at least one RAR entry
3697 * for each VF for VF MAC.
3699 num_vf_mac_filters = adapter->hw.mac.rar_entry_count -
3700 (1 + IGB_PF_MAC_FILTERS_RESERVED +
3701 adapter->vfs_allocated_count);
3703 adapter->vf_mac_list = kcalloc(num_vf_mac_filters,
3704 sizeof(struct vf_mac_filter),
3707 mac_list = adapter->vf_mac_list;
3708 INIT_LIST_HEAD(&adapter->vf_macs.l);
3710 if (adapter->vf_mac_list) {
3711 /* Initialize list of VF MAC filters */
3712 for (i = 0; i < num_vf_mac_filters; i++) {
3714 mac_list->free = true;
3715 list_add(&mac_list->l, &adapter->vf_macs.l);
3719 /* If we could not allocate memory for the VF MAC filters
3720 * we can continue without this feature but warn user.
3723 "Unable to allocate memory for VF MAC filter list\n");
3726 /* only call pci_enable_sriov() if no VFs are allocated already */
3728 err = pci_enable_sriov(pdev, adapter->vfs_allocated_count);
3732 dev_info(&pdev->dev, "%d VFs allocated\n",
3733 adapter->vfs_allocated_count);
3734 for (i = 0; i < adapter->vfs_allocated_count; i++)
3735 igb_vf_configure(adapter, i);
3737 /* DMA Coalescing is not supported in IOV mode. */
3738 adapter->flags &= ~IGB_FLAG_DMAC;
3742 kfree(adapter->vf_mac_list);
3743 adapter->vf_mac_list = NULL;
3744 kfree(adapter->vf_data);
3745 adapter->vf_data = NULL;
3746 adapter->vfs_allocated_count = 0;
3753 * igb_remove_i2c - Cleanup I2C interface
3754 * @adapter: pointer to adapter structure
3756 static void igb_remove_i2c(struct igb_adapter *adapter)
3758 /* free the adapter bus structure */
3759 i2c_del_adapter(&adapter->i2c_adap);
3763 * igb_remove - Device Removal Routine
3764 * @pdev: PCI device information struct
3766 * igb_remove is called by the PCI subsystem to alert the driver
3767 * that it should release a PCI device. The could be caused by a
3768 * Hot-Plug event, or because the driver is going to be removed from
3771 static void igb_remove(struct pci_dev *pdev)
3773 struct net_device *netdev = pci_get_drvdata(pdev);
3774 struct igb_adapter *adapter = netdev_priv(netdev);
3775 struct e1000_hw *hw = &adapter->hw;
3777 pm_runtime_get_noresume(&pdev->dev);
3778 #ifdef CONFIG_IGB_HWMON
3779 igb_sysfs_exit(adapter);
3781 igb_remove_i2c(adapter);
3782 igb_ptp_stop(adapter);
3783 /* The watchdog timer may be rescheduled, so explicitly
3784 * disable watchdog from being rescheduled.
3786 set_bit(__IGB_DOWN, &adapter->state);
3787 del_timer_sync(&adapter->watchdog_timer);
3788 del_timer_sync(&adapter->phy_info_timer);
3790 cancel_work_sync(&adapter->reset_task);
3791 cancel_work_sync(&adapter->watchdog_task);
3793 #ifdef CONFIG_IGB_DCA
3794 if (adapter->flags & IGB_FLAG_DCA_ENABLED) {
3795 dev_info(&pdev->dev, "DCA disabled\n");
3796 dca_remove_requester(&pdev->dev);
3797 adapter->flags &= ~IGB_FLAG_DCA_ENABLED;
3798 wr32(E1000_DCA_CTRL, E1000_DCA_CTRL_DCA_MODE_DISABLE);
3802 /* Release control of h/w to f/w. If f/w is AMT enabled, this
3803 * would have already happened in close and is redundant.
3805 igb_release_hw_control(adapter);
3807 #ifdef CONFIG_PCI_IOV
3808 igb_disable_sriov(pdev);
3811 unregister_netdev(netdev);
3813 igb_clear_interrupt_scheme(adapter);
3815 pci_iounmap(pdev, adapter->io_addr);
3816 if (hw->flash_address)
3817 iounmap(hw->flash_address);
3818 pci_release_mem_regions(pdev);
3820 kfree(adapter->mac_table);
3821 kfree(adapter->shadow_vfta);
3822 free_netdev(netdev);
3824 pci_disable_pcie_error_reporting(pdev);
3826 pci_disable_device(pdev);
3830 * igb_probe_vfs - Initialize vf data storage and add VFs to pci config space
3831 * @adapter: board private structure to initialize
3833 * This function initializes the vf specific data storage and then attempts to
3834 * allocate the VFs. The reason for ordering it this way is because it is much
3835 * mor expensive time wise to disable SR-IOV than it is to allocate and free
3836 * the memory for the VFs.
3838 static void igb_probe_vfs(struct igb_adapter *adapter)
3840 #ifdef CONFIG_PCI_IOV
3841 struct pci_dev *pdev = adapter->pdev;
3842 struct e1000_hw *hw = &adapter->hw;
3844 /* Virtualization features not supported on i210 family. */
3845 if ((hw->mac.type == e1000_i210) || (hw->mac.type == e1000_i211))
3848 /* Of the below we really only want the effect of getting
3849 * IGB_FLAG_HAS_MSIX set (if available), without which
3850 * igb_enable_sriov() has no effect.
3852 igb_set_interrupt_capability(adapter, true);
3853 igb_reset_interrupt_capability(adapter);
3855 pci_sriov_set_totalvfs(pdev, 7);
3856 igb_enable_sriov(pdev, max_vfs);
3858 #endif /* CONFIG_PCI_IOV */
3861 unsigned int igb_get_max_rss_queues(struct igb_adapter *adapter)
3863 struct e1000_hw *hw = &adapter->hw;
3864 unsigned int max_rss_queues;
3866 /* Determine the maximum number of RSS queues supported. */
3867 switch (hw->mac.type) {
3869 max_rss_queues = IGB_MAX_RX_QUEUES_I211;
3873 max_rss_queues = IGB_MAX_RX_QUEUES_82575;
3876 /* I350 cannot do RSS and SR-IOV at the same time */
3877 if (!!adapter->vfs_allocated_count) {
3883 if (!!adapter->vfs_allocated_count) {
3891 max_rss_queues = IGB_MAX_RX_QUEUES;
3895 return max_rss_queues;
3898 static void igb_init_queue_configuration(struct igb_adapter *adapter)
3902 max_rss_queues = igb_get_max_rss_queues(adapter);
3903 adapter->rss_queues = min_t(u32, max_rss_queues, num_online_cpus());
3905 igb_set_flag_queue_pairs(adapter, max_rss_queues);
3908 void igb_set_flag_queue_pairs(struct igb_adapter *adapter,
3909 const u32 max_rss_queues)
3911 struct e1000_hw *hw = &adapter->hw;
3913 /* Determine if we need to pair queues. */
3914 switch (hw->mac.type) {
3917 /* Device supports enough interrupts without queue pairing. */
3925 /* If rss_queues > half of max_rss_queues, pair the queues in
3926 * order to conserve interrupts due to limited supply.
3928 if (adapter->rss_queues > (max_rss_queues / 2))
3929 adapter->flags |= IGB_FLAG_QUEUE_PAIRS;
3931 adapter->flags &= ~IGB_FLAG_QUEUE_PAIRS;
3937 * igb_sw_init - Initialize general software structures (struct igb_adapter)
3938 * @adapter: board private structure to initialize
3940 * igb_sw_init initializes the Adapter private data structure.
3941 * Fields are initialized based on PCI device information and
3942 * OS network device settings (MTU size).
3944 static int igb_sw_init(struct igb_adapter *adapter)
3946 struct e1000_hw *hw = &adapter->hw;
3947 struct net_device *netdev = adapter->netdev;
3948 struct pci_dev *pdev = adapter->pdev;
3950 pci_read_config_word(pdev, PCI_COMMAND, &hw->bus.pci_cmd_word);
3952 /* set default ring sizes */
3953 adapter->tx_ring_count = IGB_DEFAULT_TXD;
3954 adapter->rx_ring_count = IGB_DEFAULT_RXD;
3956 /* set default ITR values */
3957 adapter->rx_itr_setting = IGB_DEFAULT_ITR;
3958 adapter->tx_itr_setting = IGB_DEFAULT_ITR;
3960 /* set default work limits */
3961 adapter->tx_work_limit = IGB_DEFAULT_TX_WORK;
3963 adapter->max_frame_size = netdev->mtu + IGB_ETH_PKT_HDR_PAD;
3964 adapter->min_frame_size = ETH_ZLEN + ETH_FCS_LEN;
3966 spin_lock_init(&adapter->nfc_lock);
3967 spin_lock_init(&adapter->stats64_lock);
3968 #ifdef CONFIG_PCI_IOV
3969 switch (hw->mac.type) {
3973 dev_warn(&pdev->dev,
3974 "Maximum of 7 VFs per PF, using max\n");
3975 max_vfs = adapter->vfs_allocated_count = 7;
3977 adapter->vfs_allocated_count = max_vfs;
3978 if (adapter->vfs_allocated_count)
3979 dev_warn(&pdev->dev,
3980 "Enabling SR-IOV VFs using the module parameter is deprecated - please use the pci sysfs interface.\n");
3985 #endif /* CONFIG_PCI_IOV */
3987 /* Assume MSI-X interrupts, will be checked during IRQ allocation */
3988 adapter->flags |= IGB_FLAG_HAS_MSIX;
3990 adapter->mac_table = kcalloc(hw->mac.rar_entry_count,
3991 sizeof(struct igb_mac_addr),
3993 if (!adapter->mac_table)
3996 igb_probe_vfs(adapter);
3998 igb_init_queue_configuration(adapter);
4000 /* Setup and initialize a copy of the hw vlan table array */
4001 adapter->shadow_vfta = kcalloc(E1000_VLAN_FILTER_TBL_SIZE, sizeof(u32),
4003 if (!adapter->shadow_vfta)
4006 /* This call may decrease the number of queues */
4007 if (igb_init_interrupt_scheme(adapter, true)) {
4008 dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
4012 /* Explicitly disable IRQ since the NIC can be in any state. */
4013 igb_irq_disable(adapter);
4015 if (hw->mac.type >= e1000_i350)
4016 adapter->flags &= ~IGB_FLAG_DMAC;
4018 set_bit(__IGB_DOWN, &adapter->state);
4023 * __igb_open - Called when a network interface is made active
4024 * @netdev: network interface device structure
4025 * @resuming: indicates whether we are in a resume call
4027 * Returns 0 on success, negative value on failure
4029 * The open entry point is called when a network interface is made
4030 * active by the system (IFF_UP). At this point all resources needed
4031 * for transmit and receive operations are allocated, the interrupt
4032 * handler is registered with the OS, the watchdog timer is started,
4033 * and the stack is notified that the interface is ready.
4035 static int __igb_open(struct net_device *netdev, bool resuming)
4037 struct igb_adapter *adapter = netdev_priv(netdev);
4038 struct e1000_hw *hw = &adapter->hw;
4039 struct pci_dev *pdev = adapter->pdev;
4043 /* disallow open during test */
4044 if (test_bit(__IGB_TESTING, &adapter->state)) {
4050 pm_runtime_get_sync(&pdev->dev);
4052 netif_carrier_off(netdev);
4054 /* allocate transmit descriptors */
4055 err = igb_setup_all_tx_resources(adapter);
4059 /* allocate receive descriptors */
4060 err = igb_setup_all_rx_resources(adapter);
4064 igb_power_up_link(adapter);
4066 /* before we allocate an interrupt, we must be ready to handle it.
4067 * Setting DEBUG_SHIRQ in the kernel makes it fire an interrupt
4068 * as soon as we call pci_request_irq, so we have to setup our
4069 * clean_rx handler before we do so.
4071 igb_configure(adapter);
4073 err = igb_request_irq(adapter);
4077 /* Notify the stack of the actual queue counts. */
4078 err = netif_set_real_num_tx_queues(adapter->netdev,
4079 adapter->num_tx_queues);
4081 goto err_set_queues;
4083 err = netif_set_real_num_rx_queues(adapter->netdev,
4084 adapter->num_rx_queues);
4086 goto err_set_queues;
4088 /* From here on the code is the same as igb_up() */
4089 clear_bit(__IGB_DOWN, &adapter->state);
4091 for (i = 0; i < adapter->num_q_vectors; i++)
4092 napi_enable(&(adapter->q_vector[i]->napi));
4094 /* Clear any pending interrupts. */
4098 igb_irq_enable(adapter);
4100 /* notify VFs that reset has been completed */
4101 if (adapter->vfs_allocated_count) {
4102 u32 reg_data = rd32(E1000_CTRL_EXT);
4104 reg_data |= E1000_CTRL_EXT_PFRSTD;
4105 wr32(E1000_CTRL_EXT, reg_data);
4108 netif_tx_start_all_queues(netdev);
4111 pm_runtime_put(&pdev->dev);
4113 /* start the watchdog. */
4114 hw->mac.get_link_status = 1;
4115 schedule_work(&adapter->watchdog_task);
4120 igb_free_irq(adapter);
4122 igb_release_hw_control(adapter);
4123 igb_power_down_link(adapter);
4124 igb_free_all_rx_resources(adapter);
4126 igb_free_all_tx_resources(adapter);
4130 pm_runtime_put(&pdev->dev);
4135 int igb_open(struct net_device *netdev)
4137 return __igb_open(netdev, false);
4141 * __igb_close - Disables a network interface
4142 * @netdev: network interface device structure
4143 * @suspending: indicates we are in a suspend call
4145 * Returns 0, this is not allowed to fail
4147 * The close entry point is called when an interface is de-activated
4148 * by the OS. The hardware is still under the driver's control, but
4149 * needs to be disabled. A global MAC reset is issued to stop the
4150 * hardware, and all transmit and receive resources are freed.
4152 static int __igb_close(struct net_device *netdev, bool suspending)
4154 struct igb_adapter *adapter = netdev_priv(netdev);
4155 struct pci_dev *pdev = adapter->pdev;
4157 WARN_ON(test_bit(__IGB_RESETTING, &adapter->state));
4160 pm_runtime_get_sync(&pdev->dev);
4163 igb_free_irq(adapter);
4165 igb_free_all_tx_resources(adapter);
4166 igb_free_all_rx_resources(adapter);
4169 pm_runtime_put_sync(&pdev->dev);
4173 int igb_close(struct net_device *netdev)
4175 if (netif_device_present(netdev) || netdev->dismantle)
4176 return __igb_close(netdev, false);
4181 * igb_setup_tx_resources - allocate Tx resources (Descriptors)
4182 * @tx_ring: tx descriptor ring (for a specific queue) to setup
4184 * Return 0 on success, negative on failure
4186 int igb_setup_tx_resources(struct igb_ring *tx_ring)
4188 struct device *dev = tx_ring->dev;
4191 size = sizeof(struct igb_tx_buffer) * tx_ring->count;
4193 tx_ring->tx_buffer_info = vmalloc(size);
4194 if (!tx_ring->tx_buffer_info)
4197 /* round up to nearest 4K */
4198 tx_ring->size = tx_ring->count * sizeof(union e1000_adv_tx_desc);
4199 tx_ring->size = ALIGN(tx_ring->size, 4096);
4201 tx_ring->desc = dma_alloc_coherent(dev, tx_ring->size,
4202 &tx_ring->dma, GFP_KERNEL);
4206 tx_ring->next_to_use = 0;
4207 tx_ring->next_to_clean = 0;
4212 vfree(tx_ring->tx_buffer_info);
4213 tx_ring->tx_buffer_info = NULL;
4214 dev_err(dev, "Unable to allocate memory for the Tx descriptor ring\n");
4219 * igb_setup_all_tx_resources - wrapper to allocate Tx resources
4220 * (Descriptors) for all queues
4221 * @adapter: board private structure
4223 * Return 0 on success, negative on failure
4225 static int igb_setup_all_tx_resources(struct igb_adapter *adapter)
4227 struct pci_dev *pdev = adapter->pdev;
4230 for (i = 0; i < adapter->num_tx_queues; i++) {
4231 err = igb_setup_tx_resources(adapter->tx_ring[i]);
4234 "Allocation for Tx Queue %u failed\n", i);
4235 for (i--; i >= 0; i--)
4236 igb_free_tx_resources(adapter->tx_ring[i]);
4245 * igb_setup_tctl - configure the transmit control registers
4246 * @adapter: Board private structure
4248 void igb_setup_tctl(struct igb_adapter *adapter)
4250 struct e1000_hw *hw = &adapter->hw;
4253 /* disable queue 0 which is enabled by default on 82575 and 82576 */
4254 wr32(E1000_TXDCTL(0), 0);
4256 /* Program the Transmit Control Register */
4257 tctl = rd32(E1000_TCTL);
4258 tctl &= ~E1000_TCTL_CT;
4259 tctl |= E1000_TCTL_PSP | E1000_TCTL_RTLC |
4260 (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
4262 igb_config_collision_dist(hw);
4264 /* Enable transmits */
4265 tctl |= E1000_TCTL_EN;
4267 wr32(E1000_TCTL, tctl);
4271 * igb_configure_tx_ring - Configure transmit ring after Reset
4272 * @adapter: board private structure
4273 * @ring: tx ring to configure
4275 * Configure a transmit ring after a reset.
4277 void igb_configure_tx_ring(struct igb_adapter *adapter,
4278 struct igb_ring *ring)
4280 struct e1000_hw *hw = &adapter->hw;
4282 u64 tdba = ring->dma;
4283 int reg_idx = ring->reg_idx;
4285 wr32(E1000_TDLEN(reg_idx),
4286 ring->count * sizeof(union e1000_adv_tx_desc));
4287 wr32(E1000_TDBAL(reg_idx),
4288 tdba & 0x00000000ffffffffULL);
4289 wr32(E1000_TDBAH(reg_idx), tdba >> 32);
4291 ring->tail = adapter->io_addr + E1000_TDT(reg_idx);
4292 wr32(E1000_TDH(reg_idx), 0);
4293 writel(0, ring->tail);
4295 txdctl |= IGB_TX_PTHRESH;
4296 txdctl |= IGB_TX_HTHRESH << 8;
4297 txdctl |= IGB_TX_WTHRESH << 16;
4299 /* reinitialize tx_buffer_info */
4300 memset(ring->tx_buffer_info, 0,
4301 sizeof(struct igb_tx_buffer) * ring->count);
4303 txdctl |= E1000_TXDCTL_QUEUE_ENABLE;
4304 wr32(E1000_TXDCTL(reg_idx), txdctl);
4308 * igb_configure_tx - Configure transmit Unit after Reset
4309 * @adapter: board private structure
4311 * Configure the Tx unit of the MAC after a reset.
4313 static void igb_configure_tx(struct igb_adapter *adapter)
4315 struct e1000_hw *hw = &adapter->hw;
4318 /* disable the queues */
4319 for (i = 0; i < adapter->num_tx_queues; i++)
4320 wr32(E1000_TXDCTL(adapter->tx_ring[i]->reg_idx), 0);
4323 usleep_range(10000, 20000);
4325 for (i = 0; i < adapter->num_tx_queues; i++)
4326 igb_configure_tx_ring(adapter, adapter->tx_ring[i]);
4330 * igb_setup_rx_resources - allocate Rx resources (Descriptors)
4331 * @rx_ring: Rx descriptor ring (for a specific queue) to setup
4333 * Returns 0 on success, negative on failure
4335 int igb_setup_rx_resources(struct igb_ring *rx_ring)
4337 struct igb_adapter *adapter = netdev_priv(rx_ring->netdev);
4338 struct device *dev = rx_ring->dev;
4341 size = sizeof(struct igb_rx_buffer) * rx_ring->count;
4343 rx_ring->rx_buffer_info = vmalloc(size);
4344 if (!rx_ring->rx_buffer_info)
4347 /* Round up to nearest 4K */
4348 rx_ring->size = rx_ring->count * sizeof(union e1000_adv_rx_desc);
4349 rx_ring->size = ALIGN(rx_ring->size, 4096);
4351 rx_ring->desc = dma_alloc_coherent(dev, rx_ring->size,
4352 &rx_ring->dma, GFP_KERNEL);
4356 rx_ring->next_to_alloc = 0;
4357 rx_ring->next_to_clean = 0;
4358 rx_ring->next_to_use = 0;
4360 rx_ring->xdp_prog = adapter->xdp_prog;
4362 /* XDP RX-queue info */
4363 if (xdp_rxq_info_reg(&rx_ring->xdp_rxq, rx_ring->netdev,
4364 rx_ring->queue_index, 0) < 0)
4370 vfree(rx_ring->rx_buffer_info);
4371 rx_ring->rx_buffer_info = NULL;
4372 dev_err(dev, "Unable to allocate memory for the Rx descriptor ring\n");
4377 * igb_setup_all_rx_resources - wrapper to allocate Rx resources
4378 * (Descriptors) for all queues
4379 * @adapter: board private structure
4381 * Return 0 on success, negative on failure
4383 static int igb_setup_all_rx_resources(struct igb_adapter *adapter)
4385 struct pci_dev *pdev = adapter->pdev;
4388 for (i = 0; i < adapter->num_rx_queues; i++) {
4389 err = igb_setup_rx_resources(adapter->rx_ring[i]);
4392 "Allocation for Rx Queue %u failed\n", i);
4393 for (i--; i >= 0; i--)
4394 igb_free_rx_resources(adapter->rx_ring[i]);
4403 * igb_setup_mrqc - configure the multiple receive queue control registers
4404 * @adapter: Board private structure
4406 static void igb_setup_mrqc(struct igb_adapter *adapter)
4408 struct e1000_hw *hw = &adapter->hw;
4410 u32 j, num_rx_queues;
4413 netdev_rss_key_fill(rss_key, sizeof(rss_key));
4414 for (j = 0; j < 10; j++)
4415 wr32(E1000_RSSRK(j), rss_key[j]);
4417 num_rx_queues = adapter->rss_queues;
4419 switch (hw->mac.type) {
4421 /* 82576 supports 2 RSS queues for SR-IOV */
4422 if (adapter->vfs_allocated_count)
4429 if (adapter->rss_indir_tbl_init != num_rx_queues) {
4430 for (j = 0; j < IGB_RETA_SIZE; j++)
4431 adapter->rss_indir_tbl[j] =
4432 (j * num_rx_queues) / IGB_RETA_SIZE;
4433 adapter->rss_indir_tbl_init = num_rx_queues;
4435 igb_write_rss_indir_tbl(adapter);
4437 /* Disable raw packet checksumming so that RSS hash is placed in
4438 * descriptor on writeback. No need to enable TCP/UDP/IP checksum
4439 * offloads as they are enabled by default
4441 rxcsum = rd32(E1000_RXCSUM);
4442 rxcsum |= E1000_RXCSUM_PCSD;
4444 if (adapter->hw.mac.type >= e1000_82576)
4445 /* Enable Receive Checksum Offload for SCTP */
4446 rxcsum |= E1000_RXCSUM_CRCOFL;
4448 /* Don't need to set TUOFL or IPOFL, they default to 1 */
4449 wr32(E1000_RXCSUM, rxcsum);
4451 /* Generate RSS hash based on packet types, TCP/UDP
4452 * port numbers and/or IPv4/v6 src and dst addresses
4454 mrqc = E1000_MRQC_RSS_FIELD_IPV4 |
4455 E1000_MRQC_RSS_FIELD_IPV4_TCP |
4456 E1000_MRQC_RSS_FIELD_IPV6 |
4457 E1000_MRQC_RSS_FIELD_IPV6_TCP |
4458 E1000_MRQC_RSS_FIELD_IPV6_TCP_EX;
4460 if (adapter->flags & IGB_FLAG_RSS_FIELD_IPV4_UDP)
4461 mrqc |= E1000_MRQC_RSS_FIELD_IPV4_UDP;
4462 if (adapter->flags & IGB_FLAG_RSS_FIELD_IPV6_UDP)
4463 mrqc |= E1000_MRQC_RSS_FIELD_IPV6_UDP;
4465 /* If VMDq is enabled then we set the appropriate mode for that, else
4466 * we default to RSS so that an RSS hash is calculated per packet even
4467 * if we are only using one queue
4469 if (adapter->vfs_allocated_count) {
4470 if (hw->mac.type > e1000_82575) {
4471 /* Set the default pool for the PF's first queue */
4472 u32 vtctl = rd32(E1000_VT_CTL);
4474 vtctl &= ~(E1000_VT_CTL_DEFAULT_POOL_MASK |
4475 E1000_VT_CTL_DISABLE_DEF_POOL);
4476 vtctl |= adapter->vfs_allocated_count <<
4477 E1000_VT_CTL_DEFAULT_POOL_SHIFT;
4478 wr32(E1000_VT_CTL, vtctl);
4480 if (adapter->rss_queues > 1)
4481 mrqc |= E1000_MRQC_ENABLE_VMDQ_RSS_MQ;
4483 mrqc |= E1000_MRQC_ENABLE_VMDQ;
4485 mrqc |= E1000_MRQC_ENABLE_RSS_MQ;
4487 igb_vmm_control(adapter);
4489 wr32(E1000_MRQC, mrqc);
4493 * igb_setup_rctl - configure the receive control registers
4494 * @adapter: Board private structure
4496 void igb_setup_rctl(struct igb_adapter *adapter)
4498 struct e1000_hw *hw = &adapter->hw;
4501 rctl = rd32(E1000_RCTL);
4503 rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
4504 rctl &= ~(E1000_RCTL_LBM_TCVR | E1000_RCTL_LBM_MAC);
4506 rctl |= E1000_RCTL_EN | E1000_RCTL_BAM | E1000_RCTL_RDMTS_HALF |
4507 (hw->mac.mc_filter_type << E1000_RCTL_MO_SHIFT);
4509 /* enable stripping of CRC. It's unlikely this will break BMC
4510 * redirection as it did with e1000. Newer features require
4511 * that the HW strips the CRC.
4513 rctl |= E1000_RCTL_SECRC;
4515 /* disable store bad packets and clear size bits. */
4516 rctl &= ~(E1000_RCTL_SBP | E1000_RCTL_SZ_256);
4518 /* enable LPE to allow for reception of jumbo frames */
4519 rctl |= E1000_RCTL_LPE;
4521 /* disable queue 0 to prevent tail write w/o re-config */
4522 wr32(E1000_RXDCTL(0), 0);
4524 /* Attention!!! For SR-IOV PF driver operations you must enable
4525 * queue drop for all VF and PF queues to prevent head of line blocking
4526 * if an un-trusted VF does not provide descriptors to hardware.
4528 if (adapter->vfs_allocated_count) {
4529 /* set all queue drop enable bits */
4530 wr32(E1000_QDE, ALL_QUEUES);
4533 /* This is useful for sniffing bad packets. */
4534 if (adapter->netdev->features & NETIF_F_RXALL) {
4535 /* UPE and MPE will be handled by normal PROMISC logic
4536 * in e1000e_set_rx_mode
4538 rctl |= (E1000_RCTL_SBP | /* Receive bad packets */
4539 E1000_RCTL_BAM | /* RX All Bcast Pkts */
4540 E1000_RCTL_PMCF); /* RX All MAC Ctrl Pkts */
4542 rctl &= ~(E1000_RCTL_DPF | /* Allow filtered pause */
4543 E1000_RCTL_CFIEN); /* Dis VLAN CFIEN Filter */
4544 /* Do not mess with E1000_CTRL_VME, it affects transmit as well,
4545 * and that breaks VLANs.
4549 wr32(E1000_RCTL, rctl);
4552 static inline int igb_set_vf_rlpml(struct igb_adapter *adapter, int size,
4555 struct e1000_hw *hw = &adapter->hw;
4558 if (size > MAX_JUMBO_FRAME_SIZE)
4559 size = MAX_JUMBO_FRAME_SIZE;
4561 vmolr = rd32(E1000_VMOLR(vfn));
4562 vmolr &= ~E1000_VMOLR_RLPML_MASK;
4563 vmolr |= size | E1000_VMOLR_LPE;
4564 wr32(E1000_VMOLR(vfn), vmolr);
4569 static inline void igb_set_vf_vlan_strip(struct igb_adapter *adapter,
4570 int vfn, bool enable)
4572 struct e1000_hw *hw = &adapter->hw;
4575 if (hw->mac.type < e1000_82576)
4578 if (hw->mac.type == e1000_i350)
4579 reg = E1000_DVMOLR(vfn);
4581 reg = E1000_VMOLR(vfn);
4585 val |= E1000_VMOLR_STRVLAN;
4587 val &= ~(E1000_VMOLR_STRVLAN);
4591 static inline void igb_set_vmolr(struct igb_adapter *adapter,
4594 struct e1000_hw *hw = &adapter->hw;
4597 /* This register exists only on 82576 and newer so if we are older then
4598 * we should exit and do nothing
4600 if (hw->mac.type < e1000_82576)
4603 vmolr = rd32(E1000_VMOLR(vfn));
4605 vmolr |= E1000_VMOLR_AUPE; /* Accept untagged packets */
4607 vmolr &= ~(E1000_VMOLR_AUPE); /* Tagged packets ONLY */
4609 /* clear all bits that might not be set */
4610 vmolr &= ~(E1000_VMOLR_BAM | E1000_VMOLR_RSSE);
4612 if (adapter->rss_queues > 1 && vfn == adapter->vfs_allocated_count)
4613 vmolr |= E1000_VMOLR_RSSE; /* enable RSS */
4614 /* for VMDq only allow the VFs and pool 0 to accept broadcast and
4617 if (vfn <= adapter->vfs_allocated_count)
4618 vmolr |= E1000_VMOLR_BAM; /* Accept broadcast */
4620 wr32(E1000_VMOLR(vfn), vmolr);
4624 * igb_setup_srrctl - configure the split and replication receive control
4626 * @adapter: Board private structure
4627 * @ring: receive ring to be configured
4629 void igb_setup_srrctl(struct igb_adapter *adapter, struct igb_ring *ring)
4631 struct e1000_hw *hw = &adapter->hw;
4632 int reg_idx = ring->reg_idx;
4635 srrctl = IGB_RX_HDR_LEN << E1000_SRRCTL_BSIZEHDRSIZE_SHIFT;
4636 if (ring_uses_large_buffer(ring))
4637 srrctl |= IGB_RXBUFFER_3072 >> E1000_SRRCTL_BSIZEPKT_SHIFT;
4639 srrctl |= IGB_RXBUFFER_2048 >> E1000_SRRCTL_BSIZEPKT_SHIFT;
4640 srrctl |= E1000_SRRCTL_DESCTYPE_ADV_ONEBUF;
4641 if (hw->mac.type >= e1000_82580)
4642 srrctl |= E1000_SRRCTL_TIMESTAMP;
4643 /* Only set Drop Enable if VFs allocated, or we are supporting multiple
4644 * queues and rx flow control is disabled
4646 if (adapter->vfs_allocated_count ||
4647 (!(hw->fc.current_mode & e1000_fc_rx_pause) &&
4648 adapter->num_rx_queues > 1))
4649 srrctl |= E1000_SRRCTL_DROP_EN;
4651 wr32(E1000_SRRCTL(reg_idx), srrctl);
4655 * igb_configure_rx_ring - Configure a receive ring after Reset
4656 * @adapter: board private structure
4657 * @ring: receive ring to be configured
4659 * Configure the Rx unit of the MAC after a reset.
4661 void igb_configure_rx_ring(struct igb_adapter *adapter,
4662 struct igb_ring *ring)
4664 struct e1000_hw *hw = &adapter->hw;
4665 union e1000_adv_rx_desc *rx_desc;
4666 u64 rdba = ring->dma;
4667 int reg_idx = ring->reg_idx;
4670 xdp_rxq_info_unreg_mem_model(&ring->xdp_rxq);
4671 WARN_ON(xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
4672 MEM_TYPE_PAGE_SHARED, NULL));
4674 /* disable the queue */
4675 wr32(E1000_RXDCTL(reg_idx), 0);
4677 /* Set DMA base address registers */
4678 wr32(E1000_RDBAL(reg_idx),
4679 rdba & 0x00000000ffffffffULL);
4680 wr32(E1000_RDBAH(reg_idx), rdba >> 32);
4681 wr32(E1000_RDLEN(reg_idx),
4682 ring->count * sizeof(union e1000_adv_rx_desc));
4684 /* initialize head and tail */
4685 ring->tail = adapter->io_addr + E1000_RDT(reg_idx);
4686 wr32(E1000_RDH(reg_idx), 0);
4687 writel(0, ring->tail);
4689 /* set descriptor configuration */
4690 igb_setup_srrctl(adapter, ring);
4692 /* set filtering for VMDQ pools */
4693 igb_set_vmolr(adapter, reg_idx & 0x7, true);
4695 rxdctl |= IGB_RX_PTHRESH;
4696 rxdctl |= IGB_RX_HTHRESH << 8;
4697 rxdctl |= IGB_RX_WTHRESH << 16;
4699 /* initialize rx_buffer_info */
4700 memset(ring->rx_buffer_info, 0,
4701 sizeof(struct igb_rx_buffer) * ring->count);
4703 /* initialize Rx descriptor 0 */
4704 rx_desc = IGB_RX_DESC(ring, 0);
4705 rx_desc->wb.upper.length = 0;
4707 /* enable receive descriptor fetching */
4708 rxdctl |= E1000_RXDCTL_QUEUE_ENABLE;
4709 wr32(E1000_RXDCTL(reg_idx), rxdctl);
4712 static void igb_set_rx_buffer_len(struct igb_adapter *adapter,
4713 struct igb_ring *rx_ring)
4715 /* set build_skb and buffer size flags */
4716 clear_ring_build_skb_enabled(rx_ring);
4717 clear_ring_uses_large_buffer(rx_ring);
4719 if (adapter->flags & IGB_FLAG_RX_LEGACY)
4722 set_ring_build_skb_enabled(rx_ring);
4724 #if (PAGE_SIZE < 8192)
4725 if (adapter->max_frame_size <= IGB_MAX_FRAME_BUILD_SKB)
4728 set_ring_uses_large_buffer(rx_ring);
4733 * igb_configure_rx - Configure receive Unit after Reset
4734 * @adapter: board private structure
4736 * Configure the Rx unit of the MAC after a reset.
4738 static void igb_configure_rx(struct igb_adapter *adapter)
4742 /* set the correct pool for the PF default MAC address in entry 0 */
4743 igb_set_default_mac_filter(adapter);
4745 /* Setup the HW Rx Head and Tail Descriptor Pointers and
4746 * the Base and Length of the Rx Descriptor Ring
4748 for (i = 0; i < adapter->num_rx_queues; i++) {
4749 struct igb_ring *rx_ring = adapter->rx_ring[i];
4751 igb_set_rx_buffer_len(adapter, rx_ring);
4752 igb_configure_rx_ring(adapter, rx_ring);
4757 * igb_free_tx_resources - Free Tx Resources per Queue
4758 * @tx_ring: Tx descriptor ring for a specific queue
4760 * Free all transmit software resources
4762 void igb_free_tx_resources(struct igb_ring *tx_ring)
4764 igb_clean_tx_ring(tx_ring);
4766 vfree(tx_ring->tx_buffer_info);
4767 tx_ring->tx_buffer_info = NULL;
4769 /* if not set, then don't free */
4773 dma_free_coherent(tx_ring->dev, tx_ring->size,
4774 tx_ring->desc, tx_ring->dma);
4776 tx_ring->desc = NULL;
4780 * igb_free_all_tx_resources - Free Tx Resources for All Queues
4781 * @adapter: board private structure
4783 * Free all transmit software resources
4785 static void igb_free_all_tx_resources(struct igb_adapter *adapter)
4789 for (i = 0; i < adapter->num_tx_queues; i++)
4790 if (adapter->tx_ring[i])
4791 igb_free_tx_resources(adapter->tx_ring[i]);
4795 * igb_clean_tx_ring - Free Tx Buffers
4796 * @tx_ring: ring to be cleaned
4798 static void igb_clean_tx_ring(struct igb_ring *tx_ring)
4800 u16 i = tx_ring->next_to_clean;
4801 struct igb_tx_buffer *tx_buffer = &tx_ring->tx_buffer_info[i];
4803 while (i != tx_ring->next_to_use) {
4804 union e1000_adv_tx_desc *eop_desc, *tx_desc;
4806 /* Free all the Tx ring sk_buffs */
4807 dev_kfree_skb_any(tx_buffer->skb);
4809 /* unmap skb header data */
4810 dma_unmap_single(tx_ring->dev,
4811 dma_unmap_addr(tx_buffer, dma),
4812 dma_unmap_len(tx_buffer, len),
4815 /* check for eop_desc to determine the end of the packet */
4816 eop_desc = tx_buffer->next_to_watch;
4817 tx_desc = IGB_TX_DESC(tx_ring, i);
4819 /* unmap remaining buffers */
4820 while (tx_desc != eop_desc) {
4824 if (unlikely(i == tx_ring->count)) {
4826 tx_buffer = tx_ring->tx_buffer_info;
4827 tx_desc = IGB_TX_DESC(tx_ring, 0);
4830 /* unmap any remaining paged data */
4831 if (dma_unmap_len(tx_buffer, len))
4832 dma_unmap_page(tx_ring->dev,
4833 dma_unmap_addr(tx_buffer, dma),
4834 dma_unmap_len(tx_buffer, len),
4838 /* move us one more past the eop_desc for start of next pkt */
4841 if (unlikely(i == tx_ring->count)) {
4843 tx_buffer = tx_ring->tx_buffer_info;
4847 /* reset BQL for queue */
4848 netdev_tx_reset_queue(txring_txq(tx_ring));
4850 /* reset next_to_use and next_to_clean */
4851 tx_ring->next_to_use = 0;
4852 tx_ring->next_to_clean = 0;
4856 * igb_clean_all_tx_rings - Free Tx Buffers for all queues
4857 * @adapter: board private structure
4859 static void igb_clean_all_tx_rings(struct igb_adapter *adapter)
4863 for (i = 0; i < adapter->num_tx_queues; i++)
4864 if (adapter->tx_ring[i])
4865 igb_clean_tx_ring(adapter->tx_ring[i]);
4869 * igb_free_rx_resources - Free Rx Resources
4870 * @rx_ring: ring to clean the resources from
4872 * Free all receive software resources
4874 void igb_free_rx_resources(struct igb_ring *rx_ring)
4876 igb_clean_rx_ring(rx_ring);
4878 rx_ring->xdp_prog = NULL;
4879 xdp_rxq_info_unreg(&rx_ring->xdp_rxq);
4880 vfree(rx_ring->rx_buffer_info);
4881 rx_ring->rx_buffer_info = NULL;
4883 /* if not set, then don't free */
4887 dma_free_coherent(rx_ring->dev, rx_ring->size,
4888 rx_ring->desc, rx_ring->dma);
4890 rx_ring->desc = NULL;
4894 * igb_free_all_rx_resources - Free Rx Resources for All Queues
4895 * @adapter: board private structure
4897 * Free all receive software resources
4899 static void igb_free_all_rx_resources(struct igb_adapter *adapter)
4903 for (i = 0; i < adapter->num_rx_queues; i++)
4904 if (adapter->rx_ring[i])
4905 igb_free_rx_resources(adapter->rx_ring[i]);
4909 * igb_clean_rx_ring - Free Rx Buffers per Queue
4910 * @rx_ring: ring to free buffers from
4912 static void igb_clean_rx_ring(struct igb_ring *rx_ring)
4914 u16 i = rx_ring->next_to_clean;
4916 dev_kfree_skb(rx_ring->skb);
4917 rx_ring->skb = NULL;
4919 /* Free all the Rx ring sk_buffs */
4920 while (i != rx_ring->next_to_alloc) {
4921 struct igb_rx_buffer *buffer_info = &rx_ring->rx_buffer_info[i];
4923 /* Invalidate cache lines that may have been written to by
4924 * device so that we avoid corrupting memory.
4926 dma_sync_single_range_for_cpu(rx_ring->dev,
4928 buffer_info->page_offset,
4929 igb_rx_bufsz(rx_ring),
4932 /* free resources associated with mapping */
4933 dma_unmap_page_attrs(rx_ring->dev,
4935 igb_rx_pg_size(rx_ring),
4938 __page_frag_cache_drain(buffer_info->page,
4939 buffer_info->pagecnt_bias);
4942 if (i == rx_ring->count)
4946 rx_ring->next_to_alloc = 0;
4947 rx_ring->next_to_clean = 0;
4948 rx_ring->next_to_use = 0;
4952 * igb_clean_all_rx_rings - Free Rx Buffers for all queues
4953 * @adapter: board private structure
4955 static void igb_clean_all_rx_rings(struct igb_adapter *adapter)
4959 for (i = 0; i < adapter->num_rx_queues; i++)
4960 if (adapter->rx_ring[i])
4961 igb_clean_rx_ring(adapter->rx_ring[i]);
4965 * igb_set_mac - Change the Ethernet Address of the NIC
4966 * @netdev: network interface device structure
4967 * @p: pointer to an address structure
4969 * Returns 0 on success, negative on failure
4971 static int igb_set_mac(struct net_device *netdev, void *p)
4973 struct igb_adapter *adapter = netdev_priv(netdev);
4974 struct e1000_hw *hw = &adapter->hw;
4975 struct sockaddr *addr = p;
4977 if (!is_valid_ether_addr(addr->sa_data))
4978 return -EADDRNOTAVAIL;
4980 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
4981 memcpy(hw->mac.addr, addr->sa_data, netdev->addr_len);
4983 /* set the correct pool for the new PF MAC address in entry 0 */
4984 igb_set_default_mac_filter(adapter);
4990 * igb_write_mc_addr_list - write multicast addresses to MTA
4991 * @netdev: network interface device structure
4993 * Writes multicast address list to the MTA hash table.
4994 * Returns: -ENOMEM on failure
4995 * 0 on no addresses written
4996 * X on writing X addresses to MTA
4998 static int igb_write_mc_addr_list(struct net_device *netdev)
5000 struct igb_adapter *adapter = netdev_priv(netdev);
5001 struct e1000_hw *hw = &adapter->hw;
5002 struct netdev_hw_addr *ha;
5006 if (netdev_mc_empty(netdev)) {
5007 /* nothing to program, so clear mc list */
5008 igb_update_mc_addr_list(hw, NULL, 0);
5009 igb_restore_vf_multicasts(adapter);
5013 mta_list = kcalloc(netdev_mc_count(netdev), 6, GFP_ATOMIC);
5017 /* The shared function expects a packed array of only addresses. */
5019 netdev_for_each_mc_addr(ha, netdev)
5020 memcpy(mta_list + (i++ * ETH_ALEN), ha->addr, ETH_ALEN);
5022 igb_update_mc_addr_list(hw, mta_list, i);
5025 return netdev_mc_count(netdev);
5028 static int igb_vlan_promisc_enable(struct igb_adapter *adapter)
5030 struct e1000_hw *hw = &adapter->hw;
5033 switch (hw->mac.type) {
5037 /* VLAN filtering needed for VLAN prio filter */
5038 if (adapter->netdev->features & NETIF_F_NTUPLE)
5044 /* VLAN filtering needed for pool filtering */
5045 if (adapter->vfs_allocated_count)
5052 /* We are already in VLAN promisc, nothing to do */
5053 if (adapter->flags & IGB_FLAG_VLAN_PROMISC)
5056 if (!adapter->vfs_allocated_count)
5059 /* Add PF to all active pools */
5060 pf_id = adapter->vfs_allocated_count + E1000_VLVF_POOLSEL_SHIFT;
5062 for (i = E1000_VLVF_ARRAY_SIZE; --i;) {
5063 u32 vlvf = rd32(E1000_VLVF(i));
5066 wr32(E1000_VLVF(i), vlvf);
5070 /* Set all bits in the VLAN filter table array */
5071 for (i = E1000_VLAN_FILTER_TBL_SIZE; i--;)
5072 hw->mac.ops.write_vfta(hw, i, ~0U);
5074 /* Set flag so we don't redo unnecessary work */
5075 adapter->flags |= IGB_FLAG_VLAN_PROMISC;
5080 #define VFTA_BLOCK_SIZE 8
5081 static void igb_scrub_vfta(struct igb_adapter *adapter, u32 vfta_offset)
5083 struct e1000_hw *hw = &adapter->hw;
5084 u32 vfta[VFTA_BLOCK_SIZE] = { 0 };
5085 u32 vid_start = vfta_offset * 32;
5086 u32 vid_end = vid_start + (VFTA_BLOCK_SIZE * 32);
5087 u32 i, vid, word, bits, pf_id;
5089 /* guarantee that we don't scrub out management VLAN */
5090 vid = adapter->mng_vlan_id;
5091 if (vid >= vid_start && vid < vid_end)
5092 vfta[(vid - vid_start) / 32] |= BIT(vid % 32);
5094 if (!adapter->vfs_allocated_count)
5097 pf_id = adapter->vfs_allocated_count + E1000_VLVF_POOLSEL_SHIFT;
5099 for (i = E1000_VLVF_ARRAY_SIZE; --i;) {
5100 u32 vlvf = rd32(E1000_VLVF(i));
5102 /* pull VLAN ID from VLVF */
5103 vid = vlvf & VLAN_VID_MASK;
5105 /* only concern ourselves with a certain range */
5106 if (vid < vid_start || vid >= vid_end)
5109 if (vlvf & E1000_VLVF_VLANID_ENABLE) {
5110 /* record VLAN ID in VFTA */
5111 vfta[(vid - vid_start) / 32] |= BIT(vid % 32);
5113 /* if PF is part of this then continue */
5114 if (test_bit(vid, adapter->active_vlans))
5118 /* remove PF from the pool */
5120 bits &= rd32(E1000_VLVF(i));
5121 wr32(E1000_VLVF(i), bits);
5125 /* extract values from active_vlans and write back to VFTA */
5126 for (i = VFTA_BLOCK_SIZE; i--;) {
5127 vid = (vfta_offset + i) * 32;
5128 word = vid / BITS_PER_LONG;
5129 bits = vid % BITS_PER_LONG;
5131 vfta[i] |= adapter->active_vlans[word] >> bits;
5133 hw->mac.ops.write_vfta(hw, vfta_offset + i, vfta[i]);
5137 static void igb_vlan_promisc_disable(struct igb_adapter *adapter)
5141 /* We are not in VLAN promisc, nothing to do */
5142 if (!(adapter->flags & IGB_FLAG_VLAN_PROMISC))
5145 /* Set flag so we don't redo unnecessary work */
5146 adapter->flags &= ~IGB_FLAG_VLAN_PROMISC;
5148 for (i = 0; i < E1000_VLAN_FILTER_TBL_SIZE; i += VFTA_BLOCK_SIZE)
5149 igb_scrub_vfta(adapter, i);
5153 * igb_set_rx_mode - Secondary Unicast, Multicast and Promiscuous mode set
5154 * @netdev: network interface device structure
5156 * The set_rx_mode entry point is called whenever the unicast or multicast
5157 * address lists or the network interface flags are updated. This routine is
5158 * responsible for configuring the hardware for proper unicast, multicast,
5159 * promiscuous mode, and all-multi behavior.
5161 static void igb_set_rx_mode(struct net_device *netdev)
5163 struct igb_adapter *adapter = netdev_priv(netdev);
5164 struct e1000_hw *hw = &adapter->hw;
5165 unsigned int vfn = adapter->vfs_allocated_count;
5166 u32 rctl = 0, vmolr = 0, rlpml = MAX_JUMBO_FRAME_SIZE;
5169 /* Check for Promiscuous and All Multicast modes */
5170 if (netdev->flags & IFF_PROMISC) {
5171 rctl |= E1000_RCTL_UPE | E1000_RCTL_MPE;
5172 vmolr |= E1000_VMOLR_MPME;
5174 /* enable use of UTA filter to force packets to default pool */
5175 if (hw->mac.type == e1000_82576)
5176 vmolr |= E1000_VMOLR_ROPE;
5178 if (netdev->flags & IFF_ALLMULTI) {
5179 rctl |= E1000_RCTL_MPE;
5180 vmolr |= E1000_VMOLR_MPME;
5182 /* Write addresses to the MTA, if the attempt fails
5183 * then we should just turn on promiscuous mode so
5184 * that we can at least receive multicast traffic
5186 count = igb_write_mc_addr_list(netdev);
5188 rctl |= E1000_RCTL_MPE;
5189 vmolr |= E1000_VMOLR_MPME;
5191 vmolr |= E1000_VMOLR_ROMPE;
5196 /* Write addresses to available RAR registers, if there is not
5197 * sufficient space to store all the addresses then enable
5198 * unicast promiscuous mode
5200 if (__dev_uc_sync(netdev, igb_uc_sync, igb_uc_unsync)) {
5201 rctl |= E1000_RCTL_UPE;
5202 vmolr |= E1000_VMOLR_ROPE;
5205 /* enable VLAN filtering by default */
5206 rctl |= E1000_RCTL_VFE;
5208 /* disable VLAN filtering for modes that require it */
5209 if ((netdev->flags & IFF_PROMISC) ||
5210 (netdev->features & NETIF_F_RXALL)) {
5211 /* if we fail to set all rules then just clear VFE */
5212 if (igb_vlan_promisc_enable(adapter))
5213 rctl &= ~E1000_RCTL_VFE;
5215 igb_vlan_promisc_disable(adapter);
5218 /* update state of unicast, multicast, and VLAN filtering modes */
5219 rctl |= rd32(E1000_RCTL) & ~(E1000_RCTL_UPE | E1000_RCTL_MPE |
5221 wr32(E1000_RCTL, rctl);
5223 #if (PAGE_SIZE < 8192)
5224 if (!adapter->vfs_allocated_count) {
5225 if (adapter->max_frame_size <= IGB_MAX_FRAME_BUILD_SKB)
5226 rlpml = IGB_MAX_FRAME_BUILD_SKB;
5229 wr32(E1000_RLPML, rlpml);
5231 /* In order to support SR-IOV and eventually VMDq it is necessary to set
5232 * the VMOLR to enable the appropriate modes. Without this workaround
5233 * we will have issues with VLAN tag stripping not being done for frames
5234 * that are only arriving because we are the default pool
5236 if ((hw->mac.type < e1000_82576) || (hw->mac.type > e1000_i350))
5239 /* set UTA to appropriate mode */
5240 igb_set_uta(adapter, !!(vmolr & E1000_VMOLR_ROPE));
5242 vmolr |= rd32(E1000_VMOLR(vfn)) &
5243 ~(E1000_VMOLR_ROPE | E1000_VMOLR_MPME | E1000_VMOLR_ROMPE);
5245 /* enable Rx jumbo frames, restrict as needed to support build_skb */
5246 vmolr &= ~E1000_VMOLR_RLPML_MASK;
5247 #if (PAGE_SIZE < 8192)
5248 if (adapter->max_frame_size <= IGB_MAX_FRAME_BUILD_SKB)
5249 vmolr |= IGB_MAX_FRAME_BUILD_SKB;
5252 vmolr |= MAX_JUMBO_FRAME_SIZE;
5253 vmolr |= E1000_VMOLR_LPE;
5255 wr32(E1000_VMOLR(vfn), vmolr);
5257 igb_restore_vf_multicasts(adapter);
5260 static void igb_check_wvbr(struct igb_adapter *adapter)
5262 struct e1000_hw *hw = &adapter->hw;
5265 switch (hw->mac.type) {
5268 wvbr = rd32(E1000_WVBR);
5276 adapter->wvbr |= wvbr;
5279 #define IGB_STAGGERED_QUEUE_OFFSET 8
5281 static void igb_spoof_check(struct igb_adapter *adapter)
5288 for (j = 0; j < adapter->vfs_allocated_count; j++) {
5289 if (adapter->wvbr & BIT(j) ||
5290 adapter->wvbr & BIT(j + IGB_STAGGERED_QUEUE_OFFSET)) {
5291 dev_warn(&adapter->pdev->dev,
5292 "Spoof event(s) detected on VF %d\n", j);
5295 BIT(j + IGB_STAGGERED_QUEUE_OFFSET));
5300 /* Need to wait a few seconds after link up to get diagnostic information from
5303 static void igb_update_phy_info(struct timer_list *t)
5305 struct igb_adapter *adapter = from_timer(adapter, t, phy_info_timer);
5306 igb_get_phy_info(&adapter->hw);
5310 * igb_has_link - check shared code for link and determine up/down
5311 * @adapter: pointer to driver private info
5313 bool igb_has_link(struct igb_adapter *adapter)
5315 struct e1000_hw *hw = &adapter->hw;
5316 bool link_active = false;
5318 /* get_link_status is set on LSC (link status) interrupt or
5319 * rx sequence error interrupt. get_link_status will stay
5320 * false until the e1000_check_for_link establishes link
5321 * for copper adapters ONLY
5323 switch (hw->phy.media_type) {
5324 case e1000_media_type_copper:
5325 if (!hw->mac.get_link_status)
5328 case e1000_media_type_internal_serdes:
5329 hw->mac.ops.check_for_link(hw);
5330 link_active = !hw->mac.get_link_status;
5333 case e1000_media_type_unknown:
5337 if (((hw->mac.type == e1000_i210) ||
5338 (hw->mac.type == e1000_i211)) &&
5339 (hw->phy.id == I210_I_PHY_ID)) {
5340 if (!netif_carrier_ok(adapter->netdev)) {
5341 adapter->flags &= ~IGB_FLAG_NEED_LINK_UPDATE;
5342 } else if (!(adapter->flags & IGB_FLAG_NEED_LINK_UPDATE)) {
5343 adapter->flags |= IGB_FLAG_NEED_LINK_UPDATE;
5344 adapter->link_check_timeout = jiffies;
5351 static bool igb_thermal_sensor_event(struct e1000_hw *hw, u32 event)
5354 u32 ctrl_ext, thstat;
5356 /* check for thermal sensor event on i350 copper only */
5357 if (hw->mac.type == e1000_i350) {
5358 thstat = rd32(E1000_THSTAT);
5359 ctrl_ext = rd32(E1000_CTRL_EXT);
5361 if ((hw->phy.media_type == e1000_media_type_copper) &&
5362 !(ctrl_ext & E1000_CTRL_EXT_LINK_MODE_SGMII))
5363 ret = !!(thstat & event);
5370 * igb_check_lvmmc - check for malformed packets received
5371 * and indicated in LVMMC register
5372 * @adapter: pointer to adapter
5374 static void igb_check_lvmmc(struct igb_adapter *adapter)
5376 struct e1000_hw *hw = &adapter->hw;
5379 lvmmc = rd32(E1000_LVMMC);
5381 if (unlikely(net_ratelimit())) {
5382 netdev_warn(adapter->netdev,
5383 "malformed Tx packet detected and dropped, LVMMC:0x%08x\n",
5390 * igb_watchdog - Timer Call-back
5391 * @t: pointer to timer_list containing our private info pointer
5393 static void igb_watchdog(struct timer_list *t)
5395 struct igb_adapter *adapter = from_timer(adapter, t, watchdog_timer);
5396 /* Do the rest outside of interrupt context */
5397 schedule_work(&adapter->watchdog_task);
5400 static void igb_watchdog_task(struct work_struct *work)
5402 struct igb_adapter *adapter = container_of(work,
5405 struct e1000_hw *hw = &adapter->hw;
5406 struct e1000_phy_info *phy = &hw->phy;
5407 struct net_device *netdev = adapter->netdev;
5411 u16 phy_data, retry_count = 20;
5413 link = igb_has_link(adapter);
5415 if (adapter->flags & IGB_FLAG_NEED_LINK_UPDATE) {
5416 if (time_after(jiffies, (adapter->link_check_timeout + HZ)))
5417 adapter->flags &= ~IGB_FLAG_NEED_LINK_UPDATE;
5422 /* Force link down if we have fiber to swap to */
5423 if (adapter->flags & IGB_FLAG_MAS_ENABLE) {
5424 if (hw->phy.media_type == e1000_media_type_copper) {
5425 connsw = rd32(E1000_CONNSW);
5426 if (!(connsw & E1000_CONNSW_AUTOSENSE_EN))
5431 /* Perform a reset if the media type changed. */
5432 if (hw->dev_spec._82575.media_changed) {
5433 hw->dev_spec._82575.media_changed = false;
5434 adapter->flags |= IGB_FLAG_MEDIA_RESET;
5437 /* Cancel scheduled suspend requests. */
5438 pm_runtime_resume(netdev->dev.parent);
5440 if (!netif_carrier_ok(netdev)) {
5443 hw->mac.ops.get_speed_and_duplex(hw,
5444 &adapter->link_speed,
5445 &adapter->link_duplex);
5447 ctrl = rd32(E1000_CTRL);
5448 /* Links status message must follow this format */
5450 "igb: %s NIC Link is Up %d Mbps %s Duplex, Flow Control: %s\n",
5452 adapter->link_speed,
5453 adapter->link_duplex == FULL_DUPLEX ?
5455 (ctrl & E1000_CTRL_TFCE) &&
5456 (ctrl & E1000_CTRL_RFCE) ? "RX/TX" :
5457 (ctrl & E1000_CTRL_RFCE) ? "RX" :
5458 (ctrl & E1000_CTRL_TFCE) ? "TX" : "None");
5460 /* disable EEE if enabled */
5461 if ((adapter->flags & IGB_FLAG_EEE) &&
5462 (adapter->link_duplex == HALF_DUPLEX)) {
5463 dev_info(&adapter->pdev->dev,
5464 "EEE Disabled: unsupported at half duplex. Re-enable using ethtool when at full duplex.\n");
5465 adapter->hw.dev_spec._82575.eee_disable = true;
5466 adapter->flags &= ~IGB_FLAG_EEE;
5469 /* check if SmartSpeed worked */
5470 igb_check_downshift(hw);
5471 if (phy->speed_downgraded)
5472 netdev_warn(netdev, "Link Speed was downgraded by SmartSpeed\n");
5474 /* check for thermal sensor event */
5475 if (igb_thermal_sensor_event(hw,
5476 E1000_THSTAT_LINK_THROTTLE))
5477 netdev_info(netdev, "The network adapter link speed was downshifted because it overheated\n");
5479 /* adjust timeout factor according to speed/duplex */
5480 adapter->tx_timeout_factor = 1;
5481 switch (adapter->link_speed) {
5483 adapter->tx_timeout_factor = 14;
5486 /* maybe add some timeout factor ? */
5490 if (adapter->link_speed != SPEED_1000)
5493 /* wait for Remote receiver status OK */
5495 if (!igb_read_phy_reg(hw, PHY_1000T_STATUS,
5497 if (!(phy_data & SR_1000T_REMOTE_RX_STATUS) &&
5501 goto retry_read_status;
5502 } else if (!retry_count) {
5503 dev_err(&adapter->pdev->dev, "exceed max 2 second\n");
5506 dev_err(&adapter->pdev->dev, "read 1000Base-T Status Reg\n");
5509 netif_carrier_on(netdev);
5511 igb_ping_all_vfs(adapter);
5512 igb_check_vf_rate_limit(adapter);
5514 /* link state has changed, schedule phy info update */
5515 if (!test_bit(__IGB_DOWN, &adapter->state))
5516 mod_timer(&adapter->phy_info_timer,
5517 round_jiffies(jiffies + 2 * HZ));
5520 if (netif_carrier_ok(netdev)) {
5521 adapter->link_speed = 0;
5522 adapter->link_duplex = 0;
5524 /* check for thermal sensor event */
5525 if (igb_thermal_sensor_event(hw,
5526 E1000_THSTAT_PWR_DOWN)) {
5527 netdev_err(netdev, "The network adapter was stopped because it overheated\n");
5530 /* Links status message must follow this format */
5531 netdev_info(netdev, "igb: %s NIC Link is Down\n",
5533 netif_carrier_off(netdev);
5535 igb_ping_all_vfs(adapter);
5537 /* link state has changed, schedule phy info update */
5538 if (!test_bit(__IGB_DOWN, &adapter->state))
5539 mod_timer(&adapter->phy_info_timer,
5540 round_jiffies(jiffies + 2 * HZ));
5542 /* link is down, time to check for alternate media */
5543 if (adapter->flags & IGB_FLAG_MAS_ENABLE) {
5544 igb_check_swap_media(adapter);
5545 if (adapter->flags & IGB_FLAG_MEDIA_RESET) {
5546 schedule_work(&adapter->reset_task);
5547 /* return immediately */
5551 pm_schedule_suspend(netdev->dev.parent,
5554 /* also check for alternate media here */
5555 } else if (!netif_carrier_ok(netdev) &&
5556 (adapter->flags & IGB_FLAG_MAS_ENABLE)) {
5557 igb_check_swap_media(adapter);
5558 if (adapter->flags & IGB_FLAG_MEDIA_RESET) {
5559 schedule_work(&adapter->reset_task);
5560 /* return immediately */
5566 spin_lock(&adapter->stats64_lock);
5567 igb_update_stats(adapter);
5568 spin_unlock(&adapter->stats64_lock);
5570 for (i = 0; i < adapter->num_tx_queues; i++) {
5571 struct igb_ring *tx_ring = adapter->tx_ring[i];
5572 if (!netif_carrier_ok(netdev)) {
5573 /* We've lost link, so the controller stops DMA,
5574 * but we've got queued Tx work that's never going
5575 * to get done, so reset controller to flush Tx.
5576 * (Do the reset outside of interrupt context).
5578 if (igb_desc_unused(tx_ring) + 1 < tx_ring->count) {
5579 adapter->tx_timeout_count++;
5580 schedule_work(&adapter->reset_task);
5581 /* return immediately since reset is imminent */
5586 /* Force detection of hung controller every watchdog period */
5587 set_bit(IGB_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags);
5590 /* Cause software interrupt to ensure Rx ring is cleaned */
5591 if (adapter->flags & IGB_FLAG_HAS_MSIX) {
5594 for (i = 0; i < adapter->num_q_vectors; i++)
5595 eics |= adapter->q_vector[i]->eims_value;
5596 wr32(E1000_EICS, eics);
5598 wr32(E1000_ICS, E1000_ICS_RXDMT0);
5601 igb_spoof_check(adapter);
5602 igb_ptp_rx_hang(adapter);
5603 igb_ptp_tx_hang(adapter);
5605 /* Check LVMMC register on i350/i354 only */
5606 if ((adapter->hw.mac.type == e1000_i350) ||
5607 (adapter->hw.mac.type == e1000_i354))
5608 igb_check_lvmmc(adapter);
5610 /* Reset the timer */
5611 if (!test_bit(__IGB_DOWN, &adapter->state)) {
5612 if (adapter->flags & IGB_FLAG_NEED_LINK_UPDATE)
5613 mod_timer(&adapter->watchdog_timer,
5614 round_jiffies(jiffies + HZ));
5616 mod_timer(&adapter->watchdog_timer,
5617 round_jiffies(jiffies + 2 * HZ));
5621 enum latency_range {
5625 latency_invalid = 255
5629 * igb_update_ring_itr - update the dynamic ITR value based on packet size
5630 * @q_vector: pointer to q_vector
5632 * Stores a new ITR value based on strictly on packet size. This
5633 * algorithm is less sophisticated than that used in igb_update_itr,
5634 * due to the difficulty of synchronizing statistics across multiple
5635 * receive rings. The divisors and thresholds used by this function
5636 * were determined based on theoretical maximum wire speed and testing
5637 * data, in order to minimize response time while increasing bulk
5639 * This functionality is controlled by ethtool's coalescing settings.
5640 * NOTE: This function is called only when operating in a multiqueue
5641 * receive environment.
5643 static void igb_update_ring_itr(struct igb_q_vector *q_vector)
5645 int new_val = q_vector->itr_val;
5646 int avg_wire_size = 0;
5647 struct igb_adapter *adapter = q_vector->adapter;
5648 unsigned int packets;
5650 /* For non-gigabit speeds, just fix the interrupt rate at 4000
5651 * ints/sec - ITR timer value of 120 ticks.
5653 if (adapter->link_speed != SPEED_1000) {
5654 new_val = IGB_4K_ITR;
5658 packets = q_vector->rx.total_packets;
5660 avg_wire_size = q_vector->rx.total_bytes / packets;
5662 packets = q_vector->tx.total_packets;
5664 avg_wire_size = max_t(u32, avg_wire_size,
5665 q_vector->tx.total_bytes / packets);
5667 /* if avg_wire_size isn't set no work was done */
5671 /* Add 24 bytes to size to account for CRC, preamble, and gap */
5672 avg_wire_size += 24;
5674 /* Don't starve jumbo frames */
5675 avg_wire_size = min(avg_wire_size, 3000);
5677 /* Give a little boost to mid-size frames */
5678 if ((avg_wire_size > 300) && (avg_wire_size < 1200))
5679 new_val = avg_wire_size / 3;
5681 new_val = avg_wire_size / 2;
5683 /* conservative mode (itr 3) eliminates the lowest_latency setting */
5684 if (new_val < IGB_20K_ITR &&
5685 ((q_vector->rx.ring && adapter->rx_itr_setting == 3) ||
5686 (!q_vector->rx.ring && adapter->tx_itr_setting == 3)))
5687 new_val = IGB_20K_ITR;
5690 if (new_val != q_vector->itr_val) {
5691 q_vector->itr_val = new_val;
5692 q_vector->set_itr = 1;
5695 q_vector->rx.total_bytes = 0;
5696 q_vector->rx.total_packets = 0;
5697 q_vector->tx.total_bytes = 0;
5698 q_vector->tx.total_packets = 0;
5702 * igb_update_itr - update the dynamic ITR value based on statistics
5703 * @q_vector: pointer to q_vector
5704 * @ring_container: ring info to update the itr for
5706 * Stores a new ITR value based on packets and byte
5707 * counts during the last interrupt. The advantage of per interrupt
5708 * computation is faster updates and more accurate ITR for the current
5709 * traffic pattern. Constants in this function were computed
5710 * based on theoretical maximum wire speed and thresholds were set based
5711 * on testing data as well as attempting to minimize response time
5712 * while increasing bulk throughput.
5713 * This functionality is controlled by ethtool's coalescing settings.
5714 * NOTE: These calculations are only valid when operating in a single-
5715 * queue environment.
5717 static void igb_update_itr(struct igb_q_vector *q_vector,
5718 struct igb_ring_container *ring_container)
5720 unsigned int packets = ring_container->total_packets;
5721 unsigned int bytes = ring_container->total_bytes;
5722 u8 itrval = ring_container->itr;
5724 /* no packets, exit with status unchanged */
5729 case lowest_latency:
5730 /* handle TSO and jumbo frames */
5731 if (bytes/packets > 8000)
5732 itrval = bulk_latency;
5733 else if ((packets < 5) && (bytes > 512))
5734 itrval = low_latency;
5736 case low_latency: /* 50 usec aka 20000 ints/s */
5737 if (bytes > 10000) {
5738 /* this if handles the TSO accounting */
5739 if (bytes/packets > 8000)
5740 itrval = bulk_latency;
5741 else if ((packets < 10) || ((bytes/packets) > 1200))
5742 itrval = bulk_latency;
5743 else if ((packets > 35))
5744 itrval = lowest_latency;
5745 } else if (bytes/packets > 2000) {
5746 itrval = bulk_latency;
5747 } else if (packets <= 2 && bytes < 512) {
5748 itrval = lowest_latency;
5751 case bulk_latency: /* 250 usec aka 4000 ints/s */
5752 if (bytes > 25000) {
5754 itrval = low_latency;
5755 } else if (bytes < 1500) {
5756 itrval = low_latency;
5761 /* clear work counters since we have the values we need */
5762 ring_container->total_bytes = 0;
5763 ring_container->total_packets = 0;
5765 /* write updated itr to ring container */
5766 ring_container->itr = itrval;
5769 static void igb_set_itr(struct igb_q_vector *q_vector)
5771 struct igb_adapter *adapter = q_vector->adapter;
5772 u32 new_itr = q_vector->itr_val;
5775 /* for non-gigabit speeds, just fix the interrupt rate at 4000 */
5776 if (adapter->link_speed != SPEED_1000) {
5778 new_itr = IGB_4K_ITR;
5782 igb_update_itr(q_vector, &q_vector->tx);
5783 igb_update_itr(q_vector, &q_vector->rx);
5785 current_itr = max(q_vector->rx.itr, q_vector->tx.itr);
5787 /* conservative mode (itr 3) eliminates the lowest_latency setting */
5788 if (current_itr == lowest_latency &&
5789 ((q_vector->rx.ring && adapter->rx_itr_setting == 3) ||
5790 (!q_vector->rx.ring && adapter->tx_itr_setting == 3)))
5791 current_itr = low_latency;
5793 switch (current_itr) {
5794 /* counts and packets in update_itr are dependent on these numbers */
5795 case lowest_latency:
5796 new_itr = IGB_70K_ITR; /* 70,000 ints/sec */
5799 new_itr = IGB_20K_ITR; /* 20,000 ints/sec */
5802 new_itr = IGB_4K_ITR; /* 4,000 ints/sec */
5809 if (new_itr != q_vector->itr_val) {
5810 /* this attempts to bias the interrupt rate towards Bulk
5811 * by adding intermediate steps when interrupt rate is
5814 new_itr = new_itr > q_vector->itr_val ?
5815 max((new_itr * q_vector->itr_val) /
5816 (new_itr + (q_vector->itr_val >> 2)),
5818 /* Don't write the value here; it resets the adapter's
5819 * internal timer, and causes us to delay far longer than
5820 * we should between interrupts. Instead, we write the ITR
5821 * value at the beginning of the next interrupt so the timing
5822 * ends up being correct.
5824 q_vector->itr_val = new_itr;
5825 q_vector->set_itr = 1;
5829 static void igb_tx_ctxtdesc(struct igb_ring *tx_ring,
5830 struct igb_tx_buffer *first,
5831 u32 vlan_macip_lens, u32 type_tucmd,
5834 struct e1000_adv_tx_context_desc *context_desc;
5835 u16 i = tx_ring->next_to_use;
5836 struct timespec64 ts;
5838 context_desc = IGB_TX_CTXTDESC(tx_ring, i);
5841 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
5843 /* set bits to identify this as an advanced context descriptor */
5844 type_tucmd |= E1000_TXD_CMD_DEXT | E1000_ADVTXD_DTYP_CTXT;
5846 /* For 82575, context index must be unique per ring. */
5847 if (test_bit(IGB_RING_FLAG_TX_CTX_IDX, &tx_ring->flags))
5848 mss_l4len_idx |= tx_ring->reg_idx << 4;
5850 context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens);
5851 context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd);
5852 context_desc->mss_l4len_idx = cpu_to_le32(mss_l4len_idx);
5854 /* We assume there is always a valid tx time available. Invalid times
5855 * should have been handled by the upper layers.
5857 if (tx_ring->launchtime_enable) {
5858 ts = ktime_to_timespec64(first->skb->tstamp);
5859 skb_txtime_consumed(first->skb);
5860 context_desc->seqnum_seed = cpu_to_le32(ts.tv_nsec / 32);
5862 context_desc->seqnum_seed = 0;
5866 static int igb_tso(struct igb_ring *tx_ring,
5867 struct igb_tx_buffer *first,
5870 u32 vlan_macip_lens, type_tucmd, mss_l4len_idx;
5871 struct sk_buff *skb = first->skb;
5882 u32 paylen, l4_offset;
5885 if (skb->ip_summed != CHECKSUM_PARTIAL)
5888 if (!skb_is_gso(skb))
5891 err = skb_cow_head(skb, 0);
5895 ip.hdr = skb_network_header(skb);
5896 l4.hdr = skb_checksum_start(skb);
5898 /* ADV DTYP TUCMD MKRLOC/ISCSIHEDLEN */
5899 type_tucmd = (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) ?
5900 E1000_ADVTXD_TUCMD_L4T_UDP : E1000_ADVTXD_TUCMD_L4T_TCP;
5902 /* initialize outer IP header fields */
5903 if (ip.v4->version == 4) {
5904 unsigned char *csum_start = skb_checksum_start(skb);
5905 unsigned char *trans_start = ip.hdr + (ip.v4->ihl * 4);
5907 /* IP header will have to cancel out any data that
5908 * is not a part of the outer IP header
5910 ip.v4->check = csum_fold(csum_partial(trans_start,
5911 csum_start - trans_start,
5913 type_tucmd |= E1000_ADVTXD_TUCMD_IPV4;
5916 first->tx_flags |= IGB_TX_FLAGS_TSO |
5920 ip.v6->payload_len = 0;
5921 first->tx_flags |= IGB_TX_FLAGS_TSO |
5925 /* determine offset of inner transport header */
5926 l4_offset = l4.hdr - skb->data;
5928 /* remove payload length from inner checksum */
5929 paylen = skb->len - l4_offset;
5930 if (type_tucmd & E1000_ADVTXD_TUCMD_L4T_TCP) {
5931 /* compute length of segmentation header */
5932 *hdr_len = (l4.tcp->doff * 4) + l4_offset;
5933 csum_replace_by_diff(&l4.tcp->check,
5934 (__force __wsum)htonl(paylen));
5936 /* compute length of segmentation header */
5937 *hdr_len = sizeof(*l4.udp) + l4_offset;
5938 csum_replace_by_diff(&l4.udp->check,
5939 (__force __wsum)htonl(paylen));
5942 /* update gso size and bytecount with header size */
5943 first->gso_segs = skb_shinfo(skb)->gso_segs;
5944 first->bytecount += (first->gso_segs - 1) * *hdr_len;
5947 mss_l4len_idx = (*hdr_len - l4_offset) << E1000_ADVTXD_L4LEN_SHIFT;
5948 mss_l4len_idx |= skb_shinfo(skb)->gso_size << E1000_ADVTXD_MSS_SHIFT;
5950 /* VLAN MACLEN IPLEN */
5951 vlan_macip_lens = l4.hdr - ip.hdr;
5952 vlan_macip_lens |= (ip.hdr - skb->data) << E1000_ADVTXD_MACLEN_SHIFT;
5953 vlan_macip_lens |= first->tx_flags & IGB_TX_FLAGS_VLAN_MASK;
5955 igb_tx_ctxtdesc(tx_ring, first, vlan_macip_lens,
5956 type_tucmd, mss_l4len_idx);
5961 static void igb_tx_csum(struct igb_ring *tx_ring, struct igb_tx_buffer *first)
5963 struct sk_buff *skb = first->skb;
5964 u32 vlan_macip_lens = 0;
5967 if (skb->ip_summed != CHECKSUM_PARTIAL) {
5969 if (!(first->tx_flags & IGB_TX_FLAGS_VLAN) &&
5970 !tx_ring->launchtime_enable)
5975 switch (skb->csum_offset) {
5976 case offsetof(struct tcphdr, check):
5977 type_tucmd = E1000_ADVTXD_TUCMD_L4T_TCP;
5979 case offsetof(struct udphdr, check):
5981 case offsetof(struct sctphdr, checksum):
5982 /* validate that this is actually an SCTP request */
5983 if (skb_csum_is_sctp(skb)) {
5984 type_tucmd = E1000_ADVTXD_TUCMD_L4T_SCTP;
5989 skb_checksum_help(skb);
5993 /* update TX checksum flag */
5994 first->tx_flags |= IGB_TX_FLAGS_CSUM;
5995 vlan_macip_lens = skb_checksum_start_offset(skb) -
5996 skb_network_offset(skb);
5998 vlan_macip_lens |= skb_network_offset(skb) << E1000_ADVTXD_MACLEN_SHIFT;
5999 vlan_macip_lens |= first->tx_flags & IGB_TX_FLAGS_VLAN_MASK;
6001 igb_tx_ctxtdesc(tx_ring, first, vlan_macip_lens, type_tucmd, 0);
6004 #define IGB_SET_FLAG(_input, _flag, _result) \
6005 ((_flag <= _result) ? \
6006 ((u32)(_input & _flag) * (_result / _flag)) : \
6007 ((u32)(_input & _flag) / (_flag / _result)))
6009 static u32 igb_tx_cmd_type(struct sk_buff *skb, u32 tx_flags)
6011 /* set type for advanced descriptor with frame checksum insertion */
6012 u32 cmd_type = E1000_ADVTXD_DTYP_DATA |
6013 E1000_ADVTXD_DCMD_DEXT |
6014 E1000_ADVTXD_DCMD_IFCS;
6016 /* set HW vlan bit if vlan is present */
6017 cmd_type |= IGB_SET_FLAG(tx_flags, IGB_TX_FLAGS_VLAN,
6018 (E1000_ADVTXD_DCMD_VLE));
6020 /* set segmentation bits for TSO */
6021 cmd_type |= IGB_SET_FLAG(tx_flags, IGB_TX_FLAGS_TSO,
6022 (E1000_ADVTXD_DCMD_TSE));
6024 /* set timestamp bit if present */
6025 cmd_type |= IGB_SET_FLAG(tx_flags, IGB_TX_FLAGS_TSTAMP,
6026 (E1000_ADVTXD_MAC_TSTAMP));
6028 /* insert frame checksum */
6029 cmd_type ^= IGB_SET_FLAG(skb->no_fcs, 1, E1000_ADVTXD_DCMD_IFCS);
6034 static void igb_tx_olinfo_status(struct igb_ring *tx_ring,
6035 union e1000_adv_tx_desc *tx_desc,
6036 u32 tx_flags, unsigned int paylen)
6038 u32 olinfo_status = paylen << E1000_ADVTXD_PAYLEN_SHIFT;
6040 /* 82575 requires a unique index per ring */
6041 if (test_bit(IGB_RING_FLAG_TX_CTX_IDX, &tx_ring->flags))
6042 olinfo_status |= tx_ring->reg_idx << 4;
6044 /* insert L4 checksum */
6045 olinfo_status |= IGB_SET_FLAG(tx_flags,
6047 (E1000_TXD_POPTS_TXSM << 8));
6049 /* insert IPv4 checksum */
6050 olinfo_status |= IGB_SET_FLAG(tx_flags,
6052 (E1000_TXD_POPTS_IXSM << 8));
6054 tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status);
6057 static int __igb_maybe_stop_tx(struct igb_ring *tx_ring, const u16 size)
6059 struct net_device *netdev = tx_ring->netdev;
6061 netif_stop_subqueue(netdev, tx_ring->queue_index);
6063 /* Herbert's original patch had:
6064 * smp_mb__after_netif_stop_queue();
6065 * but since that doesn't exist yet, just open code it.
6069 /* We need to check again in a case another CPU has just
6070 * made room available.
6072 if (igb_desc_unused(tx_ring) < size)
6076 netif_wake_subqueue(netdev, tx_ring->queue_index);
6078 u64_stats_update_begin(&tx_ring->tx_syncp2);
6079 tx_ring->tx_stats.restart_queue2++;
6080 u64_stats_update_end(&tx_ring->tx_syncp2);
6085 static inline int igb_maybe_stop_tx(struct igb_ring *tx_ring, const u16 size)
6087 if (igb_desc_unused(tx_ring) >= size)
6089 return __igb_maybe_stop_tx(tx_ring, size);
6092 static int igb_tx_map(struct igb_ring *tx_ring,
6093 struct igb_tx_buffer *first,
6096 struct sk_buff *skb = first->skb;
6097 struct igb_tx_buffer *tx_buffer;
6098 union e1000_adv_tx_desc *tx_desc;
6101 unsigned int data_len, size;
6102 u32 tx_flags = first->tx_flags;
6103 u32 cmd_type = igb_tx_cmd_type(skb, tx_flags);
6104 u16 i = tx_ring->next_to_use;
6106 tx_desc = IGB_TX_DESC(tx_ring, i);
6108 igb_tx_olinfo_status(tx_ring, tx_desc, tx_flags, skb->len - hdr_len);
6110 size = skb_headlen(skb);
6111 data_len = skb->data_len;
6113 dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
6117 for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
6118 if (dma_mapping_error(tx_ring->dev, dma))
6121 /* record length, and DMA address */
6122 dma_unmap_len_set(tx_buffer, len, size);
6123 dma_unmap_addr_set(tx_buffer, dma, dma);
6125 tx_desc->read.buffer_addr = cpu_to_le64(dma);
6127 while (unlikely(size > IGB_MAX_DATA_PER_TXD)) {
6128 tx_desc->read.cmd_type_len =
6129 cpu_to_le32(cmd_type ^ IGB_MAX_DATA_PER_TXD);
6133 if (i == tx_ring->count) {
6134 tx_desc = IGB_TX_DESC(tx_ring, 0);
6137 tx_desc->read.olinfo_status = 0;
6139 dma += IGB_MAX_DATA_PER_TXD;
6140 size -= IGB_MAX_DATA_PER_TXD;
6142 tx_desc->read.buffer_addr = cpu_to_le64(dma);
6145 if (likely(!data_len))
6148 tx_desc->read.cmd_type_len = cpu_to_le32(cmd_type ^ size);
6152 if (i == tx_ring->count) {
6153 tx_desc = IGB_TX_DESC(tx_ring, 0);
6156 tx_desc->read.olinfo_status = 0;
6158 size = skb_frag_size(frag);
6161 dma = skb_frag_dma_map(tx_ring->dev, frag, 0,
6162 size, DMA_TO_DEVICE);
6164 tx_buffer = &tx_ring->tx_buffer_info[i];
6167 /* write last descriptor with RS and EOP bits */
6168 cmd_type |= size | IGB_TXD_DCMD;
6169 tx_desc->read.cmd_type_len = cpu_to_le32(cmd_type);
6171 netdev_tx_sent_queue(txring_txq(tx_ring), first->bytecount);
6173 /* set the timestamp */
6174 first->time_stamp = jiffies;
6176 skb_tx_timestamp(skb);
6178 /* Force memory writes to complete before letting h/w know there
6179 * are new descriptors to fetch. (Only applicable for weak-ordered
6180 * memory model archs, such as IA-64).
6182 * We also need this memory barrier to make certain all of the
6183 * status bits have been updated before next_to_watch is written.
6187 /* set next_to_watch value indicating a packet is present */
6188 first->next_to_watch = tx_desc;
6191 if (i == tx_ring->count)
6194 tx_ring->next_to_use = i;
6196 /* Make sure there is space in the ring for the next send. */
6197 igb_maybe_stop_tx(tx_ring, DESC_NEEDED);
6199 if (netif_xmit_stopped(txring_txq(tx_ring)) || !netdev_xmit_more()) {
6200 writel(i, tx_ring->tail);
6205 dev_err(tx_ring->dev, "TX DMA map failed\n");
6206 tx_buffer = &tx_ring->tx_buffer_info[i];
6208 /* clear dma mappings for failed tx_buffer_info map */
6209 while (tx_buffer != first) {
6210 if (dma_unmap_len(tx_buffer, len))
6211 dma_unmap_page(tx_ring->dev,
6212 dma_unmap_addr(tx_buffer, dma),
6213 dma_unmap_len(tx_buffer, len),
6215 dma_unmap_len_set(tx_buffer, len, 0);
6218 i += tx_ring->count;
6219 tx_buffer = &tx_ring->tx_buffer_info[i];
6222 if (dma_unmap_len(tx_buffer, len))
6223 dma_unmap_single(tx_ring->dev,
6224 dma_unmap_addr(tx_buffer, dma),
6225 dma_unmap_len(tx_buffer, len),
6227 dma_unmap_len_set(tx_buffer, len, 0);
6229 dev_kfree_skb_any(tx_buffer->skb);
6230 tx_buffer->skb = NULL;
6232 tx_ring->next_to_use = i;
6237 int igb_xmit_xdp_ring(struct igb_adapter *adapter,
6238 struct igb_ring *tx_ring,
6239 struct xdp_frame *xdpf)
6241 union e1000_adv_tx_desc *tx_desc;
6242 u32 len, cmd_type, olinfo_status;
6243 struct igb_tx_buffer *tx_buffer;
6249 if (unlikely(!igb_desc_unused(tx_ring)))
6250 return IGB_XDP_CONSUMED;
6252 dma = dma_map_single(tx_ring->dev, xdpf->data, len, DMA_TO_DEVICE);
6253 if (dma_mapping_error(tx_ring->dev, dma))
6254 return IGB_XDP_CONSUMED;
6256 /* record the location of the first descriptor for this packet */
6257 tx_buffer = &tx_ring->tx_buffer_info[tx_ring->next_to_use];
6258 tx_buffer->bytecount = len;
6259 tx_buffer->gso_segs = 1;
6260 tx_buffer->protocol = 0;
6262 i = tx_ring->next_to_use;
6263 tx_desc = IGB_TX_DESC(tx_ring, i);
6265 dma_unmap_len_set(tx_buffer, len, len);
6266 dma_unmap_addr_set(tx_buffer, dma, dma);
6267 tx_buffer->type = IGB_TYPE_XDP;
6268 tx_buffer->xdpf = xdpf;
6270 tx_desc->read.buffer_addr = cpu_to_le64(dma);
6272 /* put descriptor type bits */
6273 cmd_type = E1000_ADVTXD_DTYP_DATA |
6274 E1000_ADVTXD_DCMD_DEXT |
6275 E1000_ADVTXD_DCMD_IFCS;
6276 cmd_type |= len | IGB_TXD_DCMD;
6277 tx_desc->read.cmd_type_len = cpu_to_le32(cmd_type);
6279 olinfo_status = len << E1000_ADVTXD_PAYLEN_SHIFT;
6280 /* 82575 requires a unique index per ring */
6281 if (test_bit(IGB_RING_FLAG_TX_CTX_IDX, &tx_ring->flags))
6282 olinfo_status |= tx_ring->reg_idx << 4;
6284 tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status);
6286 netdev_tx_sent_queue(txring_txq(tx_ring), tx_buffer->bytecount);
6288 /* set the timestamp */
6289 tx_buffer->time_stamp = jiffies;
6291 /* Avoid any potential race with xdp_xmit and cleanup */
6294 /* set next_to_watch value indicating a packet is present */
6296 if (i == tx_ring->count)
6299 tx_buffer->next_to_watch = tx_desc;
6300 tx_ring->next_to_use = i;
6302 /* Make sure there is space in the ring for the next send. */
6303 igb_maybe_stop_tx(tx_ring, DESC_NEEDED);
6305 if (netif_xmit_stopped(txring_txq(tx_ring)) || !netdev_xmit_more())
6306 writel(i, tx_ring->tail);
6311 netdev_tx_t igb_xmit_frame_ring(struct sk_buff *skb,
6312 struct igb_ring *tx_ring)
6314 struct igb_tx_buffer *first;
6318 u16 count = TXD_USE_COUNT(skb_headlen(skb));
6319 __be16 protocol = vlan_get_protocol(skb);
6322 /* need: 1 descriptor per page * PAGE_SIZE/IGB_MAX_DATA_PER_TXD,
6323 * + 1 desc for skb_headlen/IGB_MAX_DATA_PER_TXD,
6324 * + 2 desc gap to keep tail from touching head,
6325 * + 1 desc for context descriptor,
6326 * otherwise try next time
6328 for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
6329 count += TXD_USE_COUNT(skb_frag_size(
6330 &skb_shinfo(skb)->frags[f]));
6332 if (igb_maybe_stop_tx(tx_ring, count + 3)) {
6333 /* this is a hard error */
6334 return NETDEV_TX_BUSY;
6337 /* record the location of the first descriptor for this packet */
6338 first = &tx_ring->tx_buffer_info[tx_ring->next_to_use];
6339 first->type = IGB_TYPE_SKB;
6341 first->bytecount = skb->len;
6342 first->gso_segs = 1;
6344 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
6345 struct igb_adapter *adapter = netdev_priv(tx_ring->netdev);
6347 if (adapter->tstamp_config.tx_type == HWTSTAMP_TX_ON &&
6348 !test_and_set_bit_lock(__IGB_PTP_TX_IN_PROGRESS,
6350 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
6351 tx_flags |= IGB_TX_FLAGS_TSTAMP;
6353 adapter->ptp_tx_skb = skb_get(skb);
6354 adapter->ptp_tx_start = jiffies;
6355 if (adapter->hw.mac.type == e1000_82576)
6356 schedule_work(&adapter->ptp_tx_work);
6358 adapter->tx_hwtstamp_skipped++;
6362 if (skb_vlan_tag_present(skb)) {
6363 tx_flags |= IGB_TX_FLAGS_VLAN;
6364 tx_flags |= (skb_vlan_tag_get(skb) << IGB_TX_FLAGS_VLAN_SHIFT);
6367 /* record initial flags and protocol */
6368 first->tx_flags = tx_flags;
6369 first->protocol = protocol;
6371 tso = igb_tso(tx_ring, first, &hdr_len);
6375 igb_tx_csum(tx_ring, first);
6377 if (igb_tx_map(tx_ring, first, hdr_len))
6378 goto cleanup_tx_tstamp;
6380 return NETDEV_TX_OK;
6383 dev_kfree_skb_any(first->skb);
6386 if (unlikely(tx_flags & IGB_TX_FLAGS_TSTAMP)) {
6387 struct igb_adapter *adapter = netdev_priv(tx_ring->netdev);
6389 dev_kfree_skb_any(adapter->ptp_tx_skb);
6390 adapter->ptp_tx_skb = NULL;
6391 if (adapter->hw.mac.type == e1000_82576)
6392 cancel_work_sync(&adapter->ptp_tx_work);
6393 clear_bit_unlock(__IGB_PTP_TX_IN_PROGRESS, &adapter->state);
6396 return NETDEV_TX_OK;
6399 static inline struct igb_ring *igb_tx_queue_mapping(struct igb_adapter *adapter,
6400 struct sk_buff *skb)
6402 unsigned int r_idx = skb->queue_mapping;
6404 if (r_idx >= adapter->num_tx_queues)
6405 r_idx = r_idx % adapter->num_tx_queues;
6407 return adapter->tx_ring[r_idx];
6410 static netdev_tx_t igb_xmit_frame(struct sk_buff *skb,
6411 struct net_device *netdev)
6413 struct igb_adapter *adapter = netdev_priv(netdev);
6415 /* The minimum packet size with TCTL.PSP set is 17 so pad the skb
6416 * in order to meet this minimum size requirement.
6418 if (skb_put_padto(skb, 17))
6419 return NETDEV_TX_OK;
6421 return igb_xmit_frame_ring(skb, igb_tx_queue_mapping(adapter, skb));
6425 * igb_tx_timeout - Respond to a Tx Hang
6426 * @netdev: network interface device structure
6427 * @txqueue: number of the Tx queue that hung (unused)
6429 static void igb_tx_timeout(struct net_device *netdev, unsigned int __always_unused txqueue)
6431 struct igb_adapter *adapter = netdev_priv(netdev);
6432 struct e1000_hw *hw = &adapter->hw;
6434 /* Do the reset outside of interrupt context */
6435 adapter->tx_timeout_count++;
6437 if (hw->mac.type >= e1000_82580)
6438 hw->dev_spec._82575.global_device_reset = true;
6440 schedule_work(&adapter->reset_task);
6442 (adapter->eims_enable_mask & ~adapter->eims_other));
6445 static void igb_reset_task(struct work_struct *work)
6447 struct igb_adapter *adapter;
6448 adapter = container_of(work, struct igb_adapter, reset_task);
6451 /* If we're already down or resetting, just bail */
6452 if (test_bit(__IGB_DOWN, &adapter->state) ||
6453 test_bit(__IGB_RESETTING, &adapter->state)) {
6459 netdev_err(adapter->netdev, "Reset adapter\n");
6460 igb_reinit_locked(adapter);
6465 * igb_get_stats64 - Get System Network Statistics
6466 * @netdev: network interface device structure
6467 * @stats: rtnl_link_stats64 pointer
6469 static void igb_get_stats64(struct net_device *netdev,
6470 struct rtnl_link_stats64 *stats)
6472 struct igb_adapter *adapter = netdev_priv(netdev);
6474 spin_lock(&adapter->stats64_lock);
6475 igb_update_stats(adapter);
6476 memcpy(stats, &adapter->stats64, sizeof(*stats));
6477 spin_unlock(&adapter->stats64_lock);
6481 * igb_change_mtu - Change the Maximum Transfer Unit
6482 * @netdev: network interface device structure
6483 * @new_mtu: new value for maximum frame size
6485 * Returns 0 on success, negative on failure
6487 static int igb_change_mtu(struct net_device *netdev, int new_mtu)
6489 struct igb_adapter *adapter = netdev_priv(netdev);
6490 int max_frame = new_mtu + IGB_ETH_PKT_HDR_PAD;
6492 if (adapter->xdp_prog) {
6495 for (i = 0; i < adapter->num_rx_queues; i++) {
6496 struct igb_ring *ring = adapter->rx_ring[i];
6498 if (max_frame > igb_rx_bufsz(ring)) {
6499 netdev_warn(adapter->netdev,
6500 "Requested MTU size is not supported with XDP. Max frame size is %d\n",
6507 /* adjust max frame to be at least the size of a standard frame */
6508 if (max_frame < (ETH_FRAME_LEN + ETH_FCS_LEN))
6509 max_frame = ETH_FRAME_LEN + ETH_FCS_LEN;
6511 while (test_and_set_bit(__IGB_RESETTING, &adapter->state))
6512 usleep_range(1000, 2000);
6514 /* igb_down has a dependency on max_frame_size */
6515 adapter->max_frame_size = max_frame;
6517 if (netif_running(netdev))
6520 netdev_dbg(netdev, "changing MTU from %d to %d\n",
6521 netdev->mtu, new_mtu);
6522 netdev->mtu = new_mtu;
6524 if (netif_running(netdev))
6529 clear_bit(__IGB_RESETTING, &adapter->state);
6535 * igb_update_stats - Update the board statistics counters
6536 * @adapter: board private structure
6538 void igb_update_stats(struct igb_adapter *adapter)
6540 struct rtnl_link_stats64 *net_stats = &adapter->stats64;
6541 struct e1000_hw *hw = &adapter->hw;
6542 struct pci_dev *pdev = adapter->pdev;
6547 u64 _bytes, _packets;
6549 /* Prevent stats update while adapter is being reset, or if the pci
6550 * connection is down.
6552 if (adapter->link_speed == 0)
6554 if (pci_channel_offline(pdev))
6561 for (i = 0; i < adapter->num_rx_queues; i++) {
6562 struct igb_ring *ring = adapter->rx_ring[i];
6563 u32 rqdpc = rd32(E1000_RQDPC(i));
6564 if (hw->mac.type >= e1000_i210)
6565 wr32(E1000_RQDPC(i), 0);
6568 ring->rx_stats.drops += rqdpc;
6569 net_stats->rx_fifo_errors += rqdpc;
6573 start = u64_stats_fetch_begin_irq(&ring->rx_syncp);
6574 _bytes = ring->rx_stats.bytes;
6575 _packets = ring->rx_stats.packets;
6576 } while (u64_stats_fetch_retry_irq(&ring->rx_syncp, start));
6578 packets += _packets;
6581 net_stats->rx_bytes = bytes;
6582 net_stats->rx_packets = packets;
6586 for (i = 0; i < adapter->num_tx_queues; i++) {
6587 struct igb_ring *ring = adapter->tx_ring[i];
6589 start = u64_stats_fetch_begin_irq(&ring->tx_syncp);
6590 _bytes = ring->tx_stats.bytes;
6591 _packets = ring->tx_stats.packets;
6592 } while (u64_stats_fetch_retry_irq(&ring->tx_syncp, start));
6594 packets += _packets;
6596 net_stats->tx_bytes = bytes;
6597 net_stats->tx_packets = packets;
6600 /* read stats registers */
6601 adapter->stats.crcerrs += rd32(E1000_CRCERRS);
6602 adapter->stats.gprc += rd32(E1000_GPRC);
6603 adapter->stats.gorc += rd32(E1000_GORCL);
6604 rd32(E1000_GORCH); /* clear GORCL */
6605 adapter->stats.bprc += rd32(E1000_BPRC);
6606 adapter->stats.mprc += rd32(E1000_MPRC);
6607 adapter->stats.roc += rd32(E1000_ROC);
6609 adapter->stats.prc64 += rd32(E1000_PRC64);
6610 adapter->stats.prc127 += rd32(E1000_PRC127);
6611 adapter->stats.prc255 += rd32(E1000_PRC255);
6612 adapter->stats.prc511 += rd32(E1000_PRC511);
6613 adapter->stats.prc1023 += rd32(E1000_PRC1023);
6614 adapter->stats.prc1522 += rd32(E1000_PRC1522);
6615 adapter->stats.symerrs += rd32(E1000_SYMERRS);
6616 adapter->stats.sec += rd32(E1000_SEC);
6618 mpc = rd32(E1000_MPC);
6619 adapter->stats.mpc += mpc;
6620 net_stats->rx_fifo_errors += mpc;
6621 adapter->stats.scc += rd32(E1000_SCC);
6622 adapter->stats.ecol += rd32(E1000_ECOL);
6623 adapter->stats.mcc += rd32(E1000_MCC);
6624 adapter->stats.latecol += rd32(E1000_LATECOL);
6625 adapter->stats.dc += rd32(E1000_DC);
6626 adapter->stats.rlec += rd32(E1000_RLEC);
6627 adapter->stats.xonrxc += rd32(E1000_XONRXC);
6628 adapter->stats.xontxc += rd32(E1000_XONTXC);
6629 adapter->stats.xoffrxc += rd32(E1000_XOFFRXC);
6630 adapter->stats.xofftxc += rd32(E1000_XOFFTXC);
6631 adapter->stats.fcruc += rd32(E1000_FCRUC);
6632 adapter->stats.gptc += rd32(E1000_GPTC);
6633 adapter->stats.gotc += rd32(E1000_GOTCL);
6634 rd32(E1000_GOTCH); /* clear GOTCL */
6635 adapter->stats.rnbc += rd32(E1000_RNBC);
6636 adapter->stats.ruc += rd32(E1000_RUC);
6637 adapter->stats.rfc += rd32(E1000_RFC);
6638 adapter->stats.rjc += rd32(E1000_RJC);
6639 adapter->stats.tor += rd32(E1000_TORH);
6640 adapter->stats.tot += rd32(E1000_TOTH);
6641 adapter->stats.tpr += rd32(E1000_TPR);
6643 adapter->stats.ptc64 += rd32(E1000_PTC64);
6644 adapter->stats.ptc127 += rd32(E1000_PTC127);
6645 adapter->stats.ptc255 += rd32(E1000_PTC255);
6646 adapter->stats.ptc511 += rd32(E1000_PTC511);
6647 adapter->stats.ptc1023 += rd32(E1000_PTC1023);
6648 adapter->stats.ptc1522 += rd32(E1000_PTC1522);
6650 adapter->stats.mptc += rd32(E1000_MPTC);
6651 adapter->stats.bptc += rd32(E1000_BPTC);
6653 adapter->stats.tpt += rd32(E1000_TPT);
6654 adapter->stats.colc += rd32(E1000_COLC);
6656 adapter->stats.algnerrc += rd32(E1000_ALGNERRC);
6657 /* read internal phy specific stats */
6658 reg = rd32(E1000_CTRL_EXT);
6659 if (!(reg & E1000_CTRL_EXT_LINK_MODE_MASK)) {
6660 adapter->stats.rxerrc += rd32(E1000_RXERRC);
6662 /* this stat has invalid values on i210/i211 */
6663 if ((hw->mac.type != e1000_i210) &&
6664 (hw->mac.type != e1000_i211))
6665 adapter->stats.tncrs += rd32(E1000_TNCRS);
6668 adapter->stats.tsctc += rd32(E1000_TSCTC);
6669 adapter->stats.tsctfc += rd32(E1000_TSCTFC);
6671 adapter->stats.iac += rd32(E1000_IAC);
6672 adapter->stats.icrxoc += rd32(E1000_ICRXOC);
6673 adapter->stats.icrxptc += rd32(E1000_ICRXPTC);
6674 adapter->stats.icrxatc += rd32(E1000_ICRXATC);
6675 adapter->stats.ictxptc += rd32(E1000_ICTXPTC);
6676 adapter->stats.ictxatc += rd32(E1000_ICTXATC);
6677 adapter->stats.ictxqec += rd32(E1000_ICTXQEC);
6678 adapter->stats.ictxqmtc += rd32(E1000_ICTXQMTC);
6679 adapter->stats.icrxdmtc += rd32(E1000_ICRXDMTC);
6681 /* Fill out the OS statistics structure */
6682 net_stats->multicast = adapter->stats.mprc;
6683 net_stats->collisions = adapter->stats.colc;
6687 /* RLEC on some newer hardware can be incorrect so build
6688 * our own version based on RUC and ROC
6690 net_stats->rx_errors = adapter->stats.rxerrc +
6691 adapter->stats.crcerrs + adapter->stats.algnerrc +
6692 adapter->stats.ruc + adapter->stats.roc +
6693 adapter->stats.cexterr;
6694 net_stats->rx_length_errors = adapter->stats.ruc +
6696 net_stats->rx_crc_errors = adapter->stats.crcerrs;
6697 net_stats->rx_frame_errors = adapter->stats.algnerrc;
6698 net_stats->rx_missed_errors = adapter->stats.mpc;
6701 net_stats->tx_errors = adapter->stats.ecol +
6702 adapter->stats.latecol;
6703 net_stats->tx_aborted_errors = adapter->stats.ecol;
6704 net_stats->tx_window_errors = adapter->stats.latecol;
6705 net_stats->tx_carrier_errors = adapter->stats.tncrs;
6707 /* Tx Dropped needs to be maintained elsewhere */
6709 /* Management Stats */
6710 adapter->stats.mgptc += rd32(E1000_MGTPTC);
6711 adapter->stats.mgprc += rd32(E1000_MGTPRC);
6712 adapter->stats.mgpdc += rd32(E1000_MGTPDC);
6715 reg = rd32(E1000_MANC);
6716 if (reg & E1000_MANC_EN_BMC2OS) {
6717 adapter->stats.o2bgptc += rd32(E1000_O2BGPTC);
6718 adapter->stats.o2bspc += rd32(E1000_O2BSPC);
6719 adapter->stats.b2ospc += rd32(E1000_B2OSPC);
6720 adapter->stats.b2ogprc += rd32(E1000_B2OGPRC);
6724 static void igb_tsync_interrupt(struct igb_adapter *adapter)
6726 struct e1000_hw *hw = &adapter->hw;
6727 struct ptp_clock_event event;
6728 struct timespec64 ts;
6729 u32 ack = 0, tsauxc, sec, nsec, tsicr = rd32(E1000_TSICR);
6731 if (tsicr & TSINTR_SYS_WRAP) {
6732 event.type = PTP_CLOCK_PPS;
6733 if (adapter->ptp_caps.pps)
6734 ptp_clock_event(adapter->ptp_clock, &event);
6735 ack |= TSINTR_SYS_WRAP;
6738 if (tsicr & E1000_TSICR_TXTS) {
6739 /* retrieve hardware timestamp */
6740 schedule_work(&adapter->ptp_tx_work);
6741 ack |= E1000_TSICR_TXTS;
6744 if (tsicr & TSINTR_TT0) {
6745 spin_lock(&adapter->tmreg_lock);
6746 ts = timespec64_add(adapter->perout[0].start,
6747 adapter->perout[0].period);
6748 /* u32 conversion of tv_sec is safe until y2106 */
6749 wr32(E1000_TRGTTIML0, ts.tv_nsec);
6750 wr32(E1000_TRGTTIMH0, (u32)ts.tv_sec);
6751 tsauxc = rd32(E1000_TSAUXC);
6752 tsauxc |= TSAUXC_EN_TT0;
6753 wr32(E1000_TSAUXC, tsauxc);
6754 adapter->perout[0].start = ts;
6755 spin_unlock(&adapter->tmreg_lock);
6759 if (tsicr & TSINTR_TT1) {
6760 spin_lock(&adapter->tmreg_lock);
6761 ts = timespec64_add(adapter->perout[1].start,
6762 adapter->perout[1].period);
6763 wr32(E1000_TRGTTIML1, ts.tv_nsec);
6764 wr32(E1000_TRGTTIMH1, (u32)ts.tv_sec);
6765 tsauxc = rd32(E1000_TSAUXC);
6766 tsauxc |= TSAUXC_EN_TT1;
6767 wr32(E1000_TSAUXC, tsauxc);
6768 adapter->perout[1].start = ts;
6769 spin_unlock(&adapter->tmreg_lock);
6773 if (tsicr & TSINTR_AUTT0) {
6774 nsec = rd32(E1000_AUXSTMPL0);
6775 sec = rd32(E1000_AUXSTMPH0);
6776 event.type = PTP_CLOCK_EXTTS;
6778 event.timestamp = sec * 1000000000ULL + nsec;
6779 ptp_clock_event(adapter->ptp_clock, &event);
6780 ack |= TSINTR_AUTT0;
6783 if (tsicr & TSINTR_AUTT1) {
6784 nsec = rd32(E1000_AUXSTMPL1);
6785 sec = rd32(E1000_AUXSTMPH1);
6786 event.type = PTP_CLOCK_EXTTS;
6788 event.timestamp = sec * 1000000000ULL + nsec;
6789 ptp_clock_event(adapter->ptp_clock, &event);
6790 ack |= TSINTR_AUTT1;
6793 /* acknowledge the interrupts */
6794 wr32(E1000_TSICR, ack);
6797 static irqreturn_t igb_msix_other(int irq, void *data)
6799 struct igb_adapter *adapter = data;
6800 struct e1000_hw *hw = &adapter->hw;
6801 u32 icr = rd32(E1000_ICR);
6802 /* reading ICR causes bit 31 of EICR to be cleared */
6804 if (icr & E1000_ICR_DRSTA)
6805 schedule_work(&adapter->reset_task);
6807 if (icr & E1000_ICR_DOUTSYNC) {
6808 /* HW is reporting DMA is out of sync */
6809 adapter->stats.doosync++;
6810 /* The DMA Out of Sync is also indication of a spoof event
6811 * in IOV mode. Check the Wrong VM Behavior register to
6812 * see if it is really a spoof event.
6814 igb_check_wvbr(adapter);
6817 /* Check for a mailbox event */
6818 if (icr & E1000_ICR_VMMB)
6819 igb_msg_task(adapter);
6821 if (icr & E1000_ICR_LSC) {
6822 hw->mac.get_link_status = 1;
6823 /* guard against interrupt when we're going down */
6824 if (!test_bit(__IGB_DOWN, &adapter->state))
6825 mod_timer(&adapter->watchdog_timer, jiffies + 1);
6828 if (icr & E1000_ICR_TS)
6829 igb_tsync_interrupt(adapter);
6831 wr32(E1000_EIMS, adapter->eims_other);
6836 static void igb_write_itr(struct igb_q_vector *q_vector)
6838 struct igb_adapter *adapter = q_vector->adapter;
6839 u32 itr_val = q_vector->itr_val & 0x7FFC;
6841 if (!q_vector->set_itr)
6847 if (adapter->hw.mac.type == e1000_82575)
6848 itr_val |= itr_val << 16;
6850 itr_val |= E1000_EITR_CNT_IGNR;
6852 writel(itr_val, q_vector->itr_register);
6853 q_vector->set_itr = 0;
6856 static irqreturn_t igb_msix_ring(int irq, void *data)
6858 struct igb_q_vector *q_vector = data;
6860 /* Write the ITR value calculated from the previous interrupt. */
6861 igb_write_itr(q_vector);
6863 napi_schedule(&q_vector->napi);
6868 #ifdef CONFIG_IGB_DCA
6869 static void igb_update_tx_dca(struct igb_adapter *adapter,
6870 struct igb_ring *tx_ring,
6873 struct e1000_hw *hw = &adapter->hw;
6874 u32 txctrl = dca3_get_tag(tx_ring->dev, cpu);
6876 if (hw->mac.type != e1000_82575)
6877 txctrl <<= E1000_DCA_TXCTRL_CPUID_SHIFT;
6879 /* We can enable relaxed ordering for reads, but not writes when
6880 * DCA is enabled. This is due to a known issue in some chipsets
6881 * which will cause the DCA tag to be cleared.
6883 txctrl |= E1000_DCA_TXCTRL_DESC_RRO_EN |
6884 E1000_DCA_TXCTRL_DATA_RRO_EN |
6885 E1000_DCA_TXCTRL_DESC_DCA_EN;
6887 wr32(E1000_DCA_TXCTRL(tx_ring->reg_idx), txctrl);
6890 static void igb_update_rx_dca(struct igb_adapter *adapter,
6891 struct igb_ring *rx_ring,
6894 struct e1000_hw *hw = &adapter->hw;
6895 u32 rxctrl = dca3_get_tag(&adapter->pdev->dev, cpu);
6897 if (hw->mac.type != e1000_82575)
6898 rxctrl <<= E1000_DCA_RXCTRL_CPUID_SHIFT;
6900 /* We can enable relaxed ordering for reads, but not writes when
6901 * DCA is enabled. This is due to a known issue in some chipsets
6902 * which will cause the DCA tag to be cleared.
6904 rxctrl |= E1000_DCA_RXCTRL_DESC_RRO_EN |
6905 E1000_DCA_RXCTRL_DESC_DCA_EN;
6907 wr32(E1000_DCA_RXCTRL(rx_ring->reg_idx), rxctrl);
6910 static void igb_update_dca(struct igb_q_vector *q_vector)
6912 struct igb_adapter *adapter = q_vector->adapter;
6913 int cpu = get_cpu();
6915 if (q_vector->cpu == cpu)
6918 if (q_vector->tx.ring)
6919 igb_update_tx_dca(adapter, q_vector->tx.ring, cpu);
6921 if (q_vector->rx.ring)
6922 igb_update_rx_dca(adapter, q_vector->rx.ring, cpu);
6924 q_vector->cpu = cpu;
6929 static void igb_setup_dca(struct igb_adapter *adapter)
6931 struct e1000_hw *hw = &adapter->hw;
6934 if (!(adapter->flags & IGB_FLAG_DCA_ENABLED))
6937 /* Always use CB2 mode, difference is masked in the CB driver. */
6938 wr32(E1000_DCA_CTRL, E1000_DCA_CTRL_DCA_MODE_CB2);
6940 for (i = 0; i < adapter->num_q_vectors; i++) {
6941 adapter->q_vector[i]->cpu = -1;
6942 igb_update_dca(adapter->q_vector[i]);
6946 static int __igb_notify_dca(struct device *dev, void *data)
6948 struct net_device *netdev = dev_get_drvdata(dev);
6949 struct igb_adapter *adapter = netdev_priv(netdev);
6950 struct pci_dev *pdev = adapter->pdev;
6951 struct e1000_hw *hw = &adapter->hw;
6952 unsigned long event = *(unsigned long *)data;
6955 case DCA_PROVIDER_ADD:
6956 /* if already enabled, don't do it again */
6957 if (adapter->flags & IGB_FLAG_DCA_ENABLED)
6959 if (dca_add_requester(dev) == 0) {
6960 adapter->flags |= IGB_FLAG_DCA_ENABLED;
6961 dev_info(&pdev->dev, "DCA enabled\n");
6962 igb_setup_dca(adapter);
6965 fallthrough; /* since DCA is disabled. */
6966 case DCA_PROVIDER_REMOVE:
6967 if (adapter->flags & IGB_FLAG_DCA_ENABLED) {
6968 /* without this a class_device is left
6969 * hanging around in the sysfs model
6971 dca_remove_requester(dev);
6972 dev_info(&pdev->dev, "DCA disabled\n");
6973 adapter->flags &= ~IGB_FLAG_DCA_ENABLED;
6974 wr32(E1000_DCA_CTRL, E1000_DCA_CTRL_DCA_MODE_DISABLE);
6982 static int igb_notify_dca(struct notifier_block *nb, unsigned long event,
6987 ret_val = driver_for_each_device(&igb_driver.driver, NULL, &event,
6990 return ret_val ? NOTIFY_BAD : NOTIFY_DONE;
6992 #endif /* CONFIG_IGB_DCA */
6994 #ifdef CONFIG_PCI_IOV
6995 static int igb_vf_configure(struct igb_adapter *adapter, int vf)
6997 unsigned char mac_addr[ETH_ALEN];
6999 eth_zero_addr(mac_addr);
7000 igb_set_vf_mac(adapter, vf, mac_addr);
7002 /* By default spoof check is enabled for all VFs */
7003 adapter->vf_data[vf].spoofchk_enabled = true;
7005 /* By default VFs are not trusted */
7006 adapter->vf_data[vf].trusted = false;
7012 static void igb_ping_all_vfs(struct igb_adapter *adapter)
7014 struct e1000_hw *hw = &adapter->hw;
7018 for (i = 0 ; i < adapter->vfs_allocated_count; i++) {
7019 ping = E1000_PF_CONTROL_MSG;
7020 if (adapter->vf_data[i].flags & IGB_VF_FLAG_CTS)
7021 ping |= E1000_VT_MSGTYPE_CTS;
7022 igb_write_mbx(hw, &ping, 1, i);
7026 static int igb_set_vf_promisc(struct igb_adapter *adapter, u32 *msgbuf, u32 vf)
7028 struct e1000_hw *hw = &adapter->hw;
7029 u32 vmolr = rd32(E1000_VMOLR(vf));
7030 struct vf_data_storage *vf_data = &adapter->vf_data[vf];
7032 vf_data->flags &= ~(IGB_VF_FLAG_UNI_PROMISC |
7033 IGB_VF_FLAG_MULTI_PROMISC);
7034 vmolr &= ~(E1000_VMOLR_ROPE | E1000_VMOLR_ROMPE | E1000_VMOLR_MPME);
7036 if (*msgbuf & E1000_VF_SET_PROMISC_MULTICAST) {
7037 vmolr |= E1000_VMOLR_MPME;
7038 vf_data->flags |= IGB_VF_FLAG_MULTI_PROMISC;
7039 *msgbuf &= ~E1000_VF_SET_PROMISC_MULTICAST;
7041 /* if we have hashes and we are clearing a multicast promisc
7042 * flag we need to write the hashes to the MTA as this step
7043 * was previously skipped
7045 if (vf_data->num_vf_mc_hashes > 30) {
7046 vmolr |= E1000_VMOLR_MPME;
7047 } else if (vf_data->num_vf_mc_hashes) {
7050 vmolr |= E1000_VMOLR_ROMPE;
7051 for (j = 0; j < vf_data->num_vf_mc_hashes; j++)
7052 igb_mta_set(hw, vf_data->vf_mc_hashes[j]);
7056 wr32(E1000_VMOLR(vf), vmolr);
7058 /* there are flags left unprocessed, likely not supported */
7059 if (*msgbuf & E1000_VT_MSGINFO_MASK)
7065 static int igb_set_vf_multicasts(struct igb_adapter *adapter,
7066 u32 *msgbuf, u32 vf)
7068 int n = (msgbuf[0] & E1000_VT_MSGINFO_MASK) >> E1000_VT_MSGINFO_SHIFT;
7069 u16 *hash_list = (u16 *)&msgbuf[1];
7070 struct vf_data_storage *vf_data = &adapter->vf_data[vf];
7073 /* salt away the number of multicast addresses assigned
7074 * to this VF for later use to restore when the PF multi cast
7077 vf_data->num_vf_mc_hashes = n;
7079 /* only up to 30 hash values supported */
7083 /* store the hashes for later use */
7084 for (i = 0; i < n; i++)
7085 vf_data->vf_mc_hashes[i] = hash_list[i];
7087 /* Flush and reset the mta with the new values */
7088 igb_set_rx_mode(adapter->netdev);
7093 static void igb_restore_vf_multicasts(struct igb_adapter *adapter)
7095 struct e1000_hw *hw = &adapter->hw;
7096 struct vf_data_storage *vf_data;
7099 for (i = 0; i < adapter->vfs_allocated_count; i++) {
7100 u32 vmolr = rd32(E1000_VMOLR(i));
7102 vmolr &= ~(E1000_VMOLR_ROMPE | E1000_VMOLR_MPME);
7104 vf_data = &adapter->vf_data[i];
7106 if ((vf_data->num_vf_mc_hashes > 30) ||
7107 (vf_data->flags & IGB_VF_FLAG_MULTI_PROMISC)) {
7108 vmolr |= E1000_VMOLR_MPME;
7109 } else if (vf_data->num_vf_mc_hashes) {
7110 vmolr |= E1000_VMOLR_ROMPE;
7111 for (j = 0; j < vf_data->num_vf_mc_hashes; j++)
7112 igb_mta_set(hw, vf_data->vf_mc_hashes[j]);
7114 wr32(E1000_VMOLR(i), vmolr);
7118 static void igb_clear_vf_vfta(struct igb_adapter *adapter, u32 vf)
7120 struct e1000_hw *hw = &adapter->hw;
7121 u32 pool_mask, vlvf_mask, i;
7123 /* create mask for VF and other pools */
7124 pool_mask = E1000_VLVF_POOLSEL_MASK;
7125 vlvf_mask = BIT(E1000_VLVF_POOLSEL_SHIFT + vf);
7127 /* drop PF from pool bits */
7128 pool_mask &= ~BIT(E1000_VLVF_POOLSEL_SHIFT +
7129 adapter->vfs_allocated_count);
7131 /* Find the vlan filter for this id */
7132 for (i = E1000_VLVF_ARRAY_SIZE; i--;) {
7133 u32 vlvf = rd32(E1000_VLVF(i));
7134 u32 vfta_mask, vid, vfta;
7136 /* remove the vf from the pool */
7137 if (!(vlvf & vlvf_mask))
7140 /* clear out bit from VLVF */
7143 /* if other pools are present, just remove ourselves */
7144 if (vlvf & pool_mask)
7147 /* if PF is present, leave VFTA */
7148 if (vlvf & E1000_VLVF_POOLSEL_MASK)
7151 vid = vlvf & E1000_VLVF_VLANID_MASK;
7152 vfta_mask = BIT(vid % 32);
7154 /* clear bit from VFTA */
7155 vfta = adapter->shadow_vfta[vid / 32];
7156 if (vfta & vfta_mask)
7157 hw->mac.ops.write_vfta(hw, vid / 32, vfta ^ vfta_mask);
7159 /* clear pool selection enable */
7160 if (adapter->flags & IGB_FLAG_VLAN_PROMISC)
7161 vlvf &= E1000_VLVF_POOLSEL_MASK;
7165 /* clear pool bits */
7166 wr32(E1000_VLVF(i), vlvf);
7170 static int igb_find_vlvf_entry(struct e1000_hw *hw, u32 vlan)
7175 /* short cut the special case */
7179 /* Search for the VLAN id in the VLVF entries */
7180 for (idx = E1000_VLVF_ARRAY_SIZE; --idx;) {
7181 vlvf = rd32(E1000_VLVF(idx));
7182 if ((vlvf & VLAN_VID_MASK) == vlan)
7189 static void igb_update_pf_vlvf(struct igb_adapter *adapter, u32 vid)
7191 struct e1000_hw *hw = &adapter->hw;
7195 idx = igb_find_vlvf_entry(hw, vid);
7199 /* See if any other pools are set for this VLAN filter
7200 * entry other than the PF.
7202 pf_id = adapter->vfs_allocated_count + E1000_VLVF_POOLSEL_SHIFT;
7203 bits = ~BIT(pf_id) & E1000_VLVF_POOLSEL_MASK;
7204 bits &= rd32(E1000_VLVF(idx));
7206 /* Disable the filter so this falls into the default pool. */
7208 if (adapter->flags & IGB_FLAG_VLAN_PROMISC)
7209 wr32(E1000_VLVF(idx), BIT(pf_id));
7211 wr32(E1000_VLVF(idx), 0);
7215 static s32 igb_set_vf_vlan(struct igb_adapter *adapter, u32 vid,
7218 int pf_id = adapter->vfs_allocated_count;
7219 struct e1000_hw *hw = &adapter->hw;
7222 /* If VLAN overlaps with one the PF is currently monitoring make
7223 * sure that we are able to allocate a VLVF entry. This may be
7224 * redundant but it guarantees PF will maintain visibility to
7227 if (add && test_bit(vid, adapter->active_vlans)) {
7228 err = igb_vfta_set(hw, vid, pf_id, true, false);
7233 err = igb_vfta_set(hw, vid, vf, add, false);
7238 /* If we failed to add the VF VLAN or we are removing the VF VLAN
7239 * we may need to drop the PF pool bit in order to allow us to free
7240 * up the VLVF resources.
7242 if (test_bit(vid, adapter->active_vlans) ||
7243 (adapter->flags & IGB_FLAG_VLAN_PROMISC))
7244 igb_update_pf_vlvf(adapter, vid);
7249 static void igb_set_vmvir(struct igb_adapter *adapter, u32 vid, u32 vf)
7251 struct e1000_hw *hw = &adapter->hw;
7254 wr32(E1000_VMVIR(vf), (vid | E1000_VMVIR_VLANA_DEFAULT));
7256 wr32(E1000_VMVIR(vf), 0);
7259 static int igb_enable_port_vlan(struct igb_adapter *adapter, int vf,
7264 err = igb_set_vf_vlan(adapter, vlan, true, vf);
7268 igb_set_vmvir(adapter, vlan | (qos << VLAN_PRIO_SHIFT), vf);
7269 igb_set_vmolr(adapter, vf, !vlan);
7271 /* revoke access to previous VLAN */
7272 if (vlan != adapter->vf_data[vf].pf_vlan)
7273 igb_set_vf_vlan(adapter, adapter->vf_data[vf].pf_vlan,
7276 adapter->vf_data[vf].pf_vlan = vlan;
7277 adapter->vf_data[vf].pf_qos = qos;
7278 igb_set_vf_vlan_strip(adapter, vf, true);
7279 dev_info(&adapter->pdev->dev,
7280 "Setting VLAN %d, QOS 0x%x on VF %d\n", vlan, qos, vf);
7281 if (test_bit(__IGB_DOWN, &adapter->state)) {
7282 dev_warn(&adapter->pdev->dev,
7283 "The VF VLAN has been set, but the PF device is not up.\n");
7284 dev_warn(&adapter->pdev->dev,
7285 "Bring the PF device up before attempting to use the VF device.\n");
7291 static int igb_disable_port_vlan(struct igb_adapter *adapter, int vf)
7293 /* Restore tagless access via VLAN 0 */
7294 igb_set_vf_vlan(adapter, 0, true, vf);
7296 igb_set_vmvir(adapter, 0, vf);
7297 igb_set_vmolr(adapter, vf, true);
7299 /* Remove any PF assigned VLAN */
7300 if (adapter->vf_data[vf].pf_vlan)
7301 igb_set_vf_vlan(adapter, adapter->vf_data[vf].pf_vlan,
7304 adapter->vf_data[vf].pf_vlan = 0;
7305 adapter->vf_data[vf].pf_qos = 0;
7306 igb_set_vf_vlan_strip(adapter, vf, false);
7311 static int igb_ndo_set_vf_vlan(struct net_device *netdev, int vf,
7312 u16 vlan, u8 qos, __be16 vlan_proto)
7314 struct igb_adapter *adapter = netdev_priv(netdev);
7316 if ((vf >= adapter->vfs_allocated_count) || (vlan > 4095) || (qos > 7))
7319 if (vlan_proto != htons(ETH_P_8021Q))
7320 return -EPROTONOSUPPORT;
7322 return (vlan || qos) ? igb_enable_port_vlan(adapter, vf, vlan, qos) :
7323 igb_disable_port_vlan(adapter, vf);
7326 static int igb_set_vf_vlan_msg(struct igb_adapter *adapter, u32 *msgbuf, u32 vf)
7328 int add = (msgbuf[0] & E1000_VT_MSGINFO_MASK) >> E1000_VT_MSGINFO_SHIFT;
7329 int vid = (msgbuf[1] & E1000_VLVF_VLANID_MASK);
7332 if (adapter->vf_data[vf].pf_vlan)
7335 /* VLAN 0 is a special case, don't allow it to be removed */
7339 ret = igb_set_vf_vlan(adapter, vid, !!add, vf);
7341 igb_set_vf_vlan_strip(adapter, vf, !!vid);
7345 static inline void igb_vf_reset(struct igb_adapter *adapter, u32 vf)
7347 struct vf_data_storage *vf_data = &adapter->vf_data[vf];
7349 /* clear flags - except flag that indicates PF has set the MAC */
7350 vf_data->flags &= IGB_VF_FLAG_PF_SET_MAC;
7351 vf_data->last_nack = jiffies;
7353 /* reset vlans for device */
7354 igb_clear_vf_vfta(adapter, vf);
7355 igb_set_vf_vlan(adapter, vf_data->pf_vlan, true, vf);
7356 igb_set_vmvir(adapter, vf_data->pf_vlan |
7357 (vf_data->pf_qos << VLAN_PRIO_SHIFT), vf);
7358 igb_set_vmolr(adapter, vf, !vf_data->pf_vlan);
7359 igb_set_vf_vlan_strip(adapter, vf, !!(vf_data->pf_vlan));
7361 /* reset multicast table array for vf */
7362 adapter->vf_data[vf].num_vf_mc_hashes = 0;
7364 /* Flush and reset the mta with the new values */
7365 igb_set_rx_mode(adapter->netdev);
7368 static void igb_vf_reset_event(struct igb_adapter *adapter, u32 vf)
7370 unsigned char *vf_mac = adapter->vf_data[vf].vf_mac_addresses;
7372 /* clear mac address as we were hotplug removed/added */
7373 if (!(adapter->vf_data[vf].flags & IGB_VF_FLAG_PF_SET_MAC))
7374 eth_zero_addr(vf_mac);
7376 /* process remaining reset events */
7377 igb_vf_reset(adapter, vf);
7380 static void igb_vf_reset_msg(struct igb_adapter *adapter, u32 vf)
7382 struct e1000_hw *hw = &adapter->hw;
7383 unsigned char *vf_mac = adapter->vf_data[vf].vf_mac_addresses;
7385 u8 *addr = (u8 *)(&msgbuf[1]);
7387 /* process all the same items cleared in a function level reset */
7388 igb_vf_reset(adapter, vf);
7390 /* set vf mac address */
7391 igb_set_vf_mac(adapter, vf, vf_mac);
7393 /* enable transmit and receive for vf */
7394 reg = rd32(E1000_VFTE);
7395 wr32(E1000_VFTE, reg | BIT(vf));
7396 reg = rd32(E1000_VFRE);
7397 wr32(E1000_VFRE, reg | BIT(vf));
7399 adapter->vf_data[vf].flags |= IGB_VF_FLAG_CTS;
7401 /* reply to reset with ack and vf mac address */
7402 if (!is_zero_ether_addr(vf_mac)) {
7403 msgbuf[0] = E1000_VF_RESET | E1000_VT_MSGTYPE_ACK;
7404 memcpy(addr, vf_mac, ETH_ALEN);
7406 msgbuf[0] = E1000_VF_RESET | E1000_VT_MSGTYPE_NACK;
7408 igb_write_mbx(hw, msgbuf, 3, vf);
7411 static void igb_flush_mac_table(struct igb_adapter *adapter)
7413 struct e1000_hw *hw = &adapter->hw;
7416 for (i = 0; i < hw->mac.rar_entry_count; i++) {
7417 adapter->mac_table[i].state &= ~IGB_MAC_STATE_IN_USE;
7418 eth_zero_addr(adapter->mac_table[i].addr);
7419 adapter->mac_table[i].queue = 0;
7420 igb_rar_set_index(adapter, i);
7424 static int igb_available_rars(struct igb_adapter *adapter, u8 queue)
7426 struct e1000_hw *hw = &adapter->hw;
7427 /* do not count rar entries reserved for VFs MAC addresses */
7428 int rar_entries = hw->mac.rar_entry_count -
7429 adapter->vfs_allocated_count;
7432 for (i = 0; i < rar_entries; i++) {
7433 /* do not count default entries */
7434 if (adapter->mac_table[i].state & IGB_MAC_STATE_DEFAULT)
7437 /* do not count "in use" entries for different queues */
7438 if ((adapter->mac_table[i].state & IGB_MAC_STATE_IN_USE) &&
7439 (adapter->mac_table[i].queue != queue))
7448 /* Set default MAC address for the PF in the first RAR entry */
7449 static void igb_set_default_mac_filter(struct igb_adapter *adapter)
7451 struct igb_mac_addr *mac_table = &adapter->mac_table[0];
7453 ether_addr_copy(mac_table->addr, adapter->hw.mac.addr);
7454 mac_table->queue = adapter->vfs_allocated_count;
7455 mac_table->state = IGB_MAC_STATE_DEFAULT | IGB_MAC_STATE_IN_USE;
7457 igb_rar_set_index(adapter, 0);
7460 /* If the filter to be added and an already existing filter express
7461 * the same address and address type, it should be possible to only
7462 * override the other configurations, for example the queue to steer
7465 static bool igb_mac_entry_can_be_used(const struct igb_mac_addr *entry,
7466 const u8 *addr, const u8 flags)
7468 if (!(entry->state & IGB_MAC_STATE_IN_USE))
7471 if ((entry->state & IGB_MAC_STATE_SRC_ADDR) !=
7472 (flags & IGB_MAC_STATE_SRC_ADDR))
7475 if (!ether_addr_equal(addr, entry->addr))
7481 /* Add a MAC filter for 'addr' directing matching traffic to 'queue',
7482 * 'flags' is used to indicate what kind of match is made, match is by
7483 * default for the destination address, if matching by source address
7484 * is desired the flag IGB_MAC_STATE_SRC_ADDR can be used.
7486 static int igb_add_mac_filter_flags(struct igb_adapter *adapter,
7487 const u8 *addr, const u8 queue,
7490 struct e1000_hw *hw = &adapter->hw;
7491 int rar_entries = hw->mac.rar_entry_count -
7492 adapter->vfs_allocated_count;
7495 if (is_zero_ether_addr(addr))
7498 /* Search for the first empty entry in the MAC table.
7499 * Do not touch entries at the end of the table reserved for the VF MAC
7502 for (i = 0; i < rar_entries; i++) {
7503 if (!igb_mac_entry_can_be_used(&adapter->mac_table[i],
7507 ether_addr_copy(adapter->mac_table[i].addr, addr);
7508 adapter->mac_table[i].queue = queue;
7509 adapter->mac_table[i].state |= IGB_MAC_STATE_IN_USE | flags;
7511 igb_rar_set_index(adapter, i);
7518 static int igb_add_mac_filter(struct igb_adapter *adapter, const u8 *addr,
7521 return igb_add_mac_filter_flags(adapter, addr, queue, 0);
7524 /* Remove a MAC filter for 'addr' directing matching traffic to
7525 * 'queue', 'flags' is used to indicate what kind of match need to be
7526 * removed, match is by default for the destination address, if
7527 * matching by source address is to be removed the flag
7528 * IGB_MAC_STATE_SRC_ADDR can be used.
7530 static int igb_del_mac_filter_flags(struct igb_adapter *adapter,
7531 const u8 *addr, const u8 queue,
7534 struct e1000_hw *hw = &adapter->hw;
7535 int rar_entries = hw->mac.rar_entry_count -
7536 adapter->vfs_allocated_count;
7539 if (is_zero_ether_addr(addr))
7542 /* Search for matching entry in the MAC table based on given address
7543 * and queue. Do not touch entries at the end of the table reserved
7544 * for the VF MAC addresses.
7546 for (i = 0; i < rar_entries; i++) {
7547 if (!(adapter->mac_table[i].state & IGB_MAC_STATE_IN_USE))
7549 if ((adapter->mac_table[i].state & flags) != flags)
7551 if (adapter->mac_table[i].queue != queue)
7553 if (!ether_addr_equal(adapter->mac_table[i].addr, addr))
7556 /* When a filter for the default address is "deleted",
7557 * we return it to its initial configuration
7559 if (adapter->mac_table[i].state & IGB_MAC_STATE_DEFAULT) {
7560 adapter->mac_table[i].state =
7561 IGB_MAC_STATE_DEFAULT | IGB_MAC_STATE_IN_USE;
7562 adapter->mac_table[i].queue =
7563 adapter->vfs_allocated_count;
7565 adapter->mac_table[i].state = 0;
7566 adapter->mac_table[i].queue = 0;
7567 eth_zero_addr(adapter->mac_table[i].addr);
7570 igb_rar_set_index(adapter, i);
7577 static int igb_del_mac_filter(struct igb_adapter *adapter, const u8 *addr,
7580 return igb_del_mac_filter_flags(adapter, addr, queue, 0);
7583 int igb_add_mac_steering_filter(struct igb_adapter *adapter,
7584 const u8 *addr, u8 queue, u8 flags)
7586 struct e1000_hw *hw = &adapter->hw;
7588 /* In theory, this should be supported on 82575 as well, but
7589 * that part wasn't easily accessible during development.
7591 if (hw->mac.type != e1000_i210)
7594 return igb_add_mac_filter_flags(adapter, addr, queue,
7595 IGB_MAC_STATE_QUEUE_STEERING | flags);
7598 int igb_del_mac_steering_filter(struct igb_adapter *adapter,
7599 const u8 *addr, u8 queue, u8 flags)
7601 return igb_del_mac_filter_flags(adapter, addr, queue,
7602 IGB_MAC_STATE_QUEUE_STEERING | flags);
7605 static int igb_uc_sync(struct net_device *netdev, const unsigned char *addr)
7607 struct igb_adapter *adapter = netdev_priv(netdev);
7610 ret = igb_add_mac_filter(adapter, addr, adapter->vfs_allocated_count);
7612 return min_t(int, ret, 0);
7615 static int igb_uc_unsync(struct net_device *netdev, const unsigned char *addr)
7617 struct igb_adapter *adapter = netdev_priv(netdev);
7619 igb_del_mac_filter(adapter, addr, adapter->vfs_allocated_count);
7624 static int igb_set_vf_mac_filter(struct igb_adapter *adapter, const int vf,
7625 const u32 info, const u8 *addr)
7627 struct pci_dev *pdev = adapter->pdev;
7628 struct vf_data_storage *vf_data = &adapter->vf_data[vf];
7629 struct list_head *pos;
7630 struct vf_mac_filter *entry = NULL;
7634 case E1000_VF_MAC_FILTER_CLR:
7635 /* remove all unicast MAC filters related to the current VF */
7636 list_for_each(pos, &adapter->vf_macs.l) {
7637 entry = list_entry(pos, struct vf_mac_filter, l);
7638 if (entry->vf == vf) {
7641 igb_del_mac_filter(adapter, entry->vf_mac, vf);
7645 case E1000_VF_MAC_FILTER_ADD:
7646 if ((vf_data->flags & IGB_VF_FLAG_PF_SET_MAC) &&
7647 !vf_data->trusted) {
7648 dev_warn(&pdev->dev,
7649 "VF %d requested MAC filter but is administratively denied\n",
7653 if (!is_valid_ether_addr(addr)) {
7654 dev_warn(&pdev->dev,
7655 "VF %d attempted to set invalid MAC filter\n",
7660 /* try to find empty slot in the list */
7661 list_for_each(pos, &adapter->vf_macs.l) {
7662 entry = list_entry(pos, struct vf_mac_filter, l);
7667 if (entry && entry->free) {
7668 entry->free = false;
7670 ether_addr_copy(entry->vf_mac, addr);
7672 ret = igb_add_mac_filter(adapter, addr, vf);
7673 ret = min_t(int, ret, 0);
7679 dev_warn(&pdev->dev,
7680 "VF %d has requested MAC filter but there is no space for it\n",
7691 static int igb_set_vf_mac_addr(struct igb_adapter *adapter, u32 *msg, int vf)
7693 struct pci_dev *pdev = adapter->pdev;
7694 struct vf_data_storage *vf_data = &adapter->vf_data[vf];
7695 u32 info = msg[0] & E1000_VT_MSGINFO_MASK;
7697 /* The VF MAC Address is stored in a packed array of bytes
7698 * starting at the second 32 bit word of the msg array
7700 unsigned char *addr = (unsigned char *)&msg[1];
7704 if ((vf_data->flags & IGB_VF_FLAG_PF_SET_MAC) &&
7705 !vf_data->trusted) {
7706 dev_warn(&pdev->dev,
7707 "VF %d attempted to override administratively set MAC address\nReload the VF driver to resume operations\n",
7712 if (!is_valid_ether_addr(addr)) {
7713 dev_warn(&pdev->dev,
7714 "VF %d attempted to set invalid MAC\n",
7719 ret = igb_set_vf_mac(adapter, vf, addr);
7721 ret = igb_set_vf_mac_filter(adapter, vf, info, addr);
7727 static void igb_rcv_ack_from_vf(struct igb_adapter *adapter, u32 vf)
7729 struct e1000_hw *hw = &adapter->hw;
7730 struct vf_data_storage *vf_data = &adapter->vf_data[vf];
7731 u32 msg = E1000_VT_MSGTYPE_NACK;
7733 /* if device isn't clear to send it shouldn't be reading either */
7734 if (!(vf_data->flags & IGB_VF_FLAG_CTS) &&
7735 time_after(jiffies, vf_data->last_nack + (2 * HZ))) {
7736 igb_write_mbx(hw, &msg, 1, vf);
7737 vf_data->last_nack = jiffies;
7741 static void igb_rcv_msg_from_vf(struct igb_adapter *adapter, u32 vf)
7743 struct pci_dev *pdev = adapter->pdev;
7744 u32 msgbuf[E1000_VFMAILBOX_SIZE];
7745 struct e1000_hw *hw = &adapter->hw;
7746 struct vf_data_storage *vf_data = &adapter->vf_data[vf];
7749 retval = igb_read_mbx(hw, msgbuf, E1000_VFMAILBOX_SIZE, vf, false);
7752 /* if receive failed revoke VF CTS stats and restart init */
7753 dev_err(&pdev->dev, "Error receiving message from VF\n");
7754 vf_data->flags &= ~IGB_VF_FLAG_CTS;
7755 if (!time_after(jiffies, vf_data->last_nack + (2 * HZ)))
7760 /* this is a message we already processed, do nothing */
7761 if (msgbuf[0] & (E1000_VT_MSGTYPE_ACK | E1000_VT_MSGTYPE_NACK))
7764 /* until the vf completes a reset it should not be
7765 * allowed to start any configuration.
7767 if (msgbuf[0] == E1000_VF_RESET) {
7768 /* unlocks mailbox */
7769 igb_vf_reset_msg(adapter, vf);
7773 if (!(vf_data->flags & IGB_VF_FLAG_CTS)) {
7774 if (!time_after(jiffies, vf_data->last_nack + (2 * HZ)))
7780 switch ((msgbuf[0] & 0xFFFF)) {
7781 case E1000_VF_SET_MAC_ADDR:
7782 retval = igb_set_vf_mac_addr(adapter, msgbuf, vf);
7784 case E1000_VF_SET_PROMISC:
7785 retval = igb_set_vf_promisc(adapter, msgbuf, vf);
7787 case E1000_VF_SET_MULTICAST:
7788 retval = igb_set_vf_multicasts(adapter, msgbuf, vf);
7790 case E1000_VF_SET_LPE:
7791 retval = igb_set_vf_rlpml(adapter, msgbuf[1], vf);
7793 case E1000_VF_SET_VLAN:
7795 if (vf_data->pf_vlan)
7796 dev_warn(&pdev->dev,
7797 "VF %d attempted to override administratively set VLAN tag\nReload the VF driver to resume operations\n",
7800 retval = igb_set_vf_vlan_msg(adapter, msgbuf, vf);
7803 dev_err(&pdev->dev, "Unhandled Msg %08x\n", msgbuf[0]);
7808 msgbuf[0] |= E1000_VT_MSGTYPE_CTS;
7810 /* notify the VF of the results of what it sent us */
7812 msgbuf[0] |= E1000_VT_MSGTYPE_NACK;
7814 msgbuf[0] |= E1000_VT_MSGTYPE_ACK;
7816 /* unlocks mailbox */
7817 igb_write_mbx(hw, msgbuf, 1, vf);
7821 igb_unlock_mbx(hw, vf);
7824 static void igb_msg_task(struct igb_adapter *adapter)
7826 struct e1000_hw *hw = &adapter->hw;
7829 for (vf = 0; vf < adapter->vfs_allocated_count; vf++) {
7830 /* process any reset requests */
7831 if (!igb_check_for_rst(hw, vf))
7832 igb_vf_reset_event(adapter, vf);
7834 /* process any messages pending */
7835 if (!igb_check_for_msg(hw, vf))
7836 igb_rcv_msg_from_vf(adapter, vf);
7838 /* process any acks */
7839 if (!igb_check_for_ack(hw, vf))
7840 igb_rcv_ack_from_vf(adapter, vf);
7845 * igb_set_uta - Set unicast filter table address
7846 * @adapter: board private structure
7847 * @set: boolean indicating if we are setting or clearing bits
7849 * The unicast table address is a register array of 32-bit registers.
7850 * The table is meant to be used in a way similar to how the MTA is used
7851 * however due to certain limitations in the hardware it is necessary to
7852 * set all the hash bits to 1 and use the VMOLR ROPE bit as a promiscuous
7853 * enable bit to allow vlan tag stripping when promiscuous mode is enabled
7855 static void igb_set_uta(struct igb_adapter *adapter, bool set)
7857 struct e1000_hw *hw = &adapter->hw;
7858 u32 uta = set ? ~0 : 0;
7861 /* we only need to do this if VMDq is enabled */
7862 if (!adapter->vfs_allocated_count)
7865 for (i = hw->mac.uta_reg_count; i--;)
7866 array_wr32(E1000_UTA, i, uta);
7870 * igb_intr_msi - Interrupt Handler
7871 * @irq: interrupt number
7872 * @data: pointer to a network interface device structure
7874 static irqreturn_t igb_intr_msi(int irq, void *data)
7876 struct igb_adapter *adapter = data;
7877 struct igb_q_vector *q_vector = adapter->q_vector[0];
7878 struct e1000_hw *hw = &adapter->hw;
7879 /* read ICR disables interrupts using IAM */
7880 u32 icr = rd32(E1000_ICR);
7882 igb_write_itr(q_vector);
7884 if (icr & E1000_ICR_DRSTA)
7885 schedule_work(&adapter->reset_task);
7887 if (icr & E1000_ICR_DOUTSYNC) {
7888 /* HW is reporting DMA is out of sync */
7889 adapter->stats.doosync++;
7892 if (icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) {
7893 hw->mac.get_link_status = 1;
7894 if (!test_bit(__IGB_DOWN, &adapter->state))
7895 mod_timer(&adapter->watchdog_timer, jiffies + 1);
7898 if (icr & E1000_ICR_TS)
7899 igb_tsync_interrupt(adapter);
7901 napi_schedule(&q_vector->napi);
7907 * igb_intr - Legacy Interrupt Handler
7908 * @irq: interrupt number
7909 * @data: pointer to a network interface device structure
7911 static irqreturn_t igb_intr(int irq, void *data)
7913 struct igb_adapter *adapter = data;
7914 struct igb_q_vector *q_vector = adapter->q_vector[0];
7915 struct e1000_hw *hw = &adapter->hw;
7916 /* Interrupt Auto-Mask...upon reading ICR, interrupts are masked. No
7917 * need for the IMC write
7919 u32 icr = rd32(E1000_ICR);
7921 /* IMS will not auto-mask if INT_ASSERTED is not set, and if it is
7922 * not set, then the adapter didn't send an interrupt
7924 if (!(icr & E1000_ICR_INT_ASSERTED))
7927 igb_write_itr(q_vector);
7929 if (icr & E1000_ICR_DRSTA)
7930 schedule_work(&adapter->reset_task);
7932 if (icr & E1000_ICR_DOUTSYNC) {
7933 /* HW is reporting DMA is out of sync */
7934 adapter->stats.doosync++;
7937 if (icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) {
7938 hw->mac.get_link_status = 1;
7939 /* guard against interrupt when we're going down */
7940 if (!test_bit(__IGB_DOWN, &adapter->state))
7941 mod_timer(&adapter->watchdog_timer, jiffies + 1);
7944 if (icr & E1000_ICR_TS)
7945 igb_tsync_interrupt(adapter);
7947 napi_schedule(&q_vector->napi);
7952 static void igb_ring_irq_enable(struct igb_q_vector *q_vector)
7954 struct igb_adapter *adapter = q_vector->adapter;
7955 struct e1000_hw *hw = &adapter->hw;
7957 if ((q_vector->rx.ring && (adapter->rx_itr_setting & 3)) ||
7958 (!q_vector->rx.ring && (adapter->tx_itr_setting & 3))) {
7959 if ((adapter->num_q_vectors == 1) && !adapter->vf_data)
7960 igb_set_itr(q_vector);
7962 igb_update_ring_itr(q_vector);
7965 if (!test_bit(__IGB_DOWN, &adapter->state)) {
7966 if (adapter->flags & IGB_FLAG_HAS_MSIX)
7967 wr32(E1000_EIMS, q_vector->eims_value);
7969 igb_irq_enable(adapter);
7974 * igb_poll - NAPI Rx polling callback
7975 * @napi: napi polling structure
7976 * @budget: count of how many packets we should handle
7978 static int igb_poll(struct napi_struct *napi, int budget)
7980 struct igb_q_vector *q_vector = container_of(napi,
7981 struct igb_q_vector,
7983 bool clean_complete = true;
7986 #ifdef CONFIG_IGB_DCA
7987 if (q_vector->adapter->flags & IGB_FLAG_DCA_ENABLED)
7988 igb_update_dca(q_vector);
7990 if (q_vector->tx.ring)
7991 clean_complete = igb_clean_tx_irq(q_vector, budget);
7993 if (q_vector->rx.ring) {
7994 int cleaned = igb_clean_rx_irq(q_vector, budget);
7996 work_done += cleaned;
7997 if (cleaned >= budget)
7998 clean_complete = false;
8001 /* If all work not completed, return budget and keep polling */
8002 if (!clean_complete)
8005 /* Exit the polling mode, but don't re-enable interrupts if stack might
8006 * poll us due to busy-polling
8008 if (likely(napi_complete_done(napi, work_done)))
8009 igb_ring_irq_enable(q_vector);
8011 return min(work_done, budget - 1);
8015 * igb_clean_tx_irq - Reclaim resources after transmit completes
8016 * @q_vector: pointer to q_vector containing needed info
8017 * @napi_budget: Used to determine if we are in netpoll
8019 * returns true if ring is completely cleaned
8021 static bool igb_clean_tx_irq(struct igb_q_vector *q_vector, int napi_budget)
8023 struct igb_adapter *adapter = q_vector->adapter;
8024 struct igb_ring *tx_ring = q_vector->tx.ring;
8025 struct igb_tx_buffer *tx_buffer;
8026 union e1000_adv_tx_desc *tx_desc;
8027 unsigned int total_bytes = 0, total_packets = 0;
8028 unsigned int budget = q_vector->tx.work_limit;
8029 unsigned int i = tx_ring->next_to_clean;
8031 if (test_bit(__IGB_DOWN, &adapter->state))
8034 tx_buffer = &tx_ring->tx_buffer_info[i];
8035 tx_desc = IGB_TX_DESC(tx_ring, i);
8036 i -= tx_ring->count;
8039 union e1000_adv_tx_desc *eop_desc = tx_buffer->next_to_watch;
8041 /* if next_to_watch is not set then there is no work pending */
8045 /* prevent any other reads prior to eop_desc */
8048 /* if DD is not set pending work has not been completed */
8049 if (!(eop_desc->wb.status & cpu_to_le32(E1000_TXD_STAT_DD)))
8052 /* clear next_to_watch to prevent false hangs */
8053 tx_buffer->next_to_watch = NULL;
8055 /* update the statistics for this packet */
8056 total_bytes += tx_buffer->bytecount;
8057 total_packets += tx_buffer->gso_segs;
8060 if (tx_buffer->type == IGB_TYPE_SKB)
8061 napi_consume_skb(tx_buffer->skb, napi_budget);
8063 xdp_return_frame(tx_buffer->xdpf);
8065 /* unmap skb header data */
8066 dma_unmap_single(tx_ring->dev,
8067 dma_unmap_addr(tx_buffer, dma),
8068 dma_unmap_len(tx_buffer, len),
8071 /* clear tx_buffer data */
8072 dma_unmap_len_set(tx_buffer, len, 0);
8074 /* clear last DMA location and unmap remaining buffers */
8075 while (tx_desc != eop_desc) {
8080 i -= tx_ring->count;
8081 tx_buffer = tx_ring->tx_buffer_info;
8082 tx_desc = IGB_TX_DESC(tx_ring, 0);
8085 /* unmap any remaining paged data */
8086 if (dma_unmap_len(tx_buffer, len)) {
8087 dma_unmap_page(tx_ring->dev,
8088 dma_unmap_addr(tx_buffer, dma),
8089 dma_unmap_len(tx_buffer, len),
8091 dma_unmap_len_set(tx_buffer, len, 0);
8095 /* move us one more past the eop_desc for start of next pkt */
8100 i -= tx_ring->count;
8101 tx_buffer = tx_ring->tx_buffer_info;
8102 tx_desc = IGB_TX_DESC(tx_ring, 0);
8105 /* issue prefetch for next Tx descriptor */
8108 /* update budget accounting */
8110 } while (likely(budget));
8112 netdev_tx_completed_queue(txring_txq(tx_ring),
8113 total_packets, total_bytes);
8114 i += tx_ring->count;
8115 tx_ring->next_to_clean = i;
8116 u64_stats_update_begin(&tx_ring->tx_syncp);
8117 tx_ring->tx_stats.bytes += total_bytes;
8118 tx_ring->tx_stats.packets += total_packets;
8119 u64_stats_update_end(&tx_ring->tx_syncp);
8120 q_vector->tx.total_bytes += total_bytes;
8121 q_vector->tx.total_packets += total_packets;
8123 if (test_bit(IGB_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags)) {
8124 struct e1000_hw *hw = &adapter->hw;
8126 /* Detect a transmit hang in hardware, this serializes the
8127 * check with the clearing of time_stamp and movement of i
8129 clear_bit(IGB_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags);
8130 if (tx_buffer->next_to_watch &&
8131 time_after(jiffies, tx_buffer->time_stamp +
8132 (adapter->tx_timeout_factor * HZ)) &&
8133 !(rd32(E1000_STATUS) & E1000_STATUS_TXOFF)) {
8135 /* detected Tx unit hang */
8136 dev_err(tx_ring->dev,
8137 "Detected Tx Unit Hang\n"
8141 " next_to_use <%x>\n"
8142 " next_to_clean <%x>\n"
8143 "buffer_info[next_to_clean]\n"
8144 " time_stamp <%lx>\n"
8145 " next_to_watch <%p>\n"
8147 " desc.status <%x>\n",
8148 tx_ring->queue_index,
8149 rd32(E1000_TDH(tx_ring->reg_idx)),
8150 readl(tx_ring->tail),
8151 tx_ring->next_to_use,
8152 tx_ring->next_to_clean,
8153 tx_buffer->time_stamp,
8154 tx_buffer->next_to_watch,
8156 tx_buffer->next_to_watch->wb.status);
8157 netif_stop_subqueue(tx_ring->netdev,
8158 tx_ring->queue_index);
8160 /* we are about to reset, no point in enabling stuff */
8165 #define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
8166 if (unlikely(total_packets &&
8167 netif_carrier_ok(tx_ring->netdev) &&
8168 igb_desc_unused(tx_ring) >= TX_WAKE_THRESHOLD)) {
8169 /* Make sure that anybody stopping the queue after this
8170 * sees the new next_to_clean.
8173 if (__netif_subqueue_stopped(tx_ring->netdev,
8174 tx_ring->queue_index) &&
8175 !(test_bit(__IGB_DOWN, &adapter->state))) {
8176 netif_wake_subqueue(tx_ring->netdev,
8177 tx_ring->queue_index);
8179 u64_stats_update_begin(&tx_ring->tx_syncp);
8180 tx_ring->tx_stats.restart_queue++;
8181 u64_stats_update_end(&tx_ring->tx_syncp);
8189 * igb_reuse_rx_page - page flip buffer and store it back on the ring
8190 * @rx_ring: rx descriptor ring to store buffers on
8191 * @old_buff: donor buffer to have page reused
8193 * Synchronizes page for reuse by the adapter
8195 static void igb_reuse_rx_page(struct igb_ring *rx_ring,
8196 struct igb_rx_buffer *old_buff)
8198 struct igb_rx_buffer *new_buff;
8199 u16 nta = rx_ring->next_to_alloc;
8201 new_buff = &rx_ring->rx_buffer_info[nta];
8203 /* update, and store next to alloc */
8205 rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0;
8207 /* Transfer page from old buffer to new buffer.
8208 * Move each member individually to avoid possible store
8209 * forwarding stalls.
8211 new_buff->dma = old_buff->dma;
8212 new_buff->page = old_buff->page;
8213 new_buff->page_offset = old_buff->page_offset;
8214 new_buff->pagecnt_bias = old_buff->pagecnt_bias;
8217 static bool igb_can_reuse_rx_page(struct igb_rx_buffer *rx_buffer,
8220 unsigned int pagecnt_bias = rx_buffer->pagecnt_bias;
8221 struct page *page = rx_buffer->page;
8223 /* avoid re-using remote and pfmemalloc pages */
8224 if (!dev_page_is_reusable(page))
8227 #if (PAGE_SIZE < 8192)
8228 /* if we are only owner of page we can reuse it */
8229 if (unlikely((rx_buf_pgcnt - pagecnt_bias) > 1))
8232 #define IGB_LAST_OFFSET \
8233 (SKB_WITH_OVERHEAD(PAGE_SIZE) - IGB_RXBUFFER_2048)
8235 if (rx_buffer->page_offset > IGB_LAST_OFFSET)
8239 /* If we have drained the page fragment pool we need to update
8240 * the pagecnt_bias and page count so that we fully restock the
8241 * number of references the driver holds.
8243 if (unlikely(pagecnt_bias == 1)) {
8244 page_ref_add(page, USHRT_MAX - 1);
8245 rx_buffer->pagecnt_bias = USHRT_MAX;
8252 * igb_add_rx_frag - Add contents of Rx buffer to sk_buff
8253 * @rx_ring: rx descriptor ring to transact packets on
8254 * @rx_buffer: buffer containing page to add
8255 * @skb: sk_buff to place the data into
8256 * @size: size of buffer to be added
8258 * This function will add the data contained in rx_buffer->page to the skb.
8260 static void igb_add_rx_frag(struct igb_ring *rx_ring,
8261 struct igb_rx_buffer *rx_buffer,
8262 struct sk_buff *skb,
8265 #if (PAGE_SIZE < 8192)
8266 unsigned int truesize = igb_rx_pg_size(rx_ring) / 2;
8268 unsigned int truesize = ring_uses_build_skb(rx_ring) ?
8269 SKB_DATA_ALIGN(IGB_SKB_PAD + size) :
8270 SKB_DATA_ALIGN(size);
8272 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, rx_buffer->page,
8273 rx_buffer->page_offset, size, truesize);
8274 #if (PAGE_SIZE < 8192)
8275 rx_buffer->page_offset ^= truesize;
8277 rx_buffer->page_offset += truesize;
8281 static struct sk_buff *igb_construct_skb(struct igb_ring *rx_ring,
8282 struct igb_rx_buffer *rx_buffer,
8283 struct xdp_buff *xdp,
8286 #if (PAGE_SIZE < 8192)
8287 unsigned int truesize = igb_rx_pg_size(rx_ring) / 2;
8289 unsigned int truesize = SKB_DATA_ALIGN(xdp->data_end -
8290 xdp->data_hard_start);
8292 unsigned int size = xdp->data_end - xdp->data;
8293 unsigned int headlen;
8294 struct sk_buff *skb;
8296 /* prefetch first cache line of first page */
8297 net_prefetch(xdp->data);
8299 /* allocate a skb to store the frags */
8300 skb = napi_alloc_skb(&rx_ring->q_vector->napi, IGB_RX_HDR_LEN);
8305 skb_hwtstamps(skb)->hwtstamp = timestamp;
8307 /* Determine available headroom for copy */
8309 if (headlen > IGB_RX_HDR_LEN)
8310 headlen = eth_get_headlen(skb->dev, xdp->data, IGB_RX_HDR_LEN);
8312 /* align pull length to size of long to optimize memcpy performance */
8313 memcpy(__skb_put(skb, headlen), xdp->data, ALIGN(headlen, sizeof(long)));
8315 /* update all of the pointers */
8318 skb_add_rx_frag(skb, 0, rx_buffer->page,
8319 (xdp->data + headlen) - page_address(rx_buffer->page),
8321 #if (PAGE_SIZE < 8192)
8322 rx_buffer->page_offset ^= truesize;
8324 rx_buffer->page_offset += truesize;
8327 rx_buffer->pagecnt_bias++;
8333 static struct sk_buff *igb_build_skb(struct igb_ring *rx_ring,
8334 struct igb_rx_buffer *rx_buffer,
8335 struct xdp_buff *xdp,
8338 #if (PAGE_SIZE < 8192)
8339 unsigned int truesize = igb_rx_pg_size(rx_ring) / 2;
8341 unsigned int truesize = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) +
8342 SKB_DATA_ALIGN(xdp->data_end -
8343 xdp->data_hard_start);
8345 unsigned int metasize = xdp->data - xdp->data_meta;
8346 struct sk_buff *skb;
8348 /* prefetch first cache line of first page */
8349 net_prefetch(xdp->data_meta);
8351 /* build an skb around the page buffer */
8352 skb = build_skb(xdp->data_hard_start, truesize);
8356 /* update pointers within the skb to store the data */
8357 skb_reserve(skb, xdp->data - xdp->data_hard_start);
8358 __skb_put(skb, xdp->data_end - xdp->data);
8361 skb_metadata_set(skb, metasize);
8364 skb_hwtstamps(skb)->hwtstamp = timestamp;
8366 /* update buffer offset */
8367 #if (PAGE_SIZE < 8192)
8368 rx_buffer->page_offset ^= truesize;
8370 rx_buffer->page_offset += truesize;
8376 static struct sk_buff *igb_run_xdp(struct igb_adapter *adapter,
8377 struct igb_ring *rx_ring,
8378 struct xdp_buff *xdp)
8380 int err, result = IGB_XDP_PASS;
8381 struct bpf_prog *xdp_prog;
8384 xdp_prog = READ_ONCE(rx_ring->xdp_prog);
8389 prefetchw(xdp->data_hard_start); /* xdp_frame write */
8391 act = bpf_prog_run_xdp(xdp_prog, xdp);
8396 result = igb_xdp_xmit_back(adapter, xdp);
8397 if (result == IGB_XDP_CONSUMED)
8401 err = xdp_do_redirect(adapter->netdev, xdp, xdp_prog);
8404 result = IGB_XDP_REDIR;
8407 bpf_warn_invalid_xdp_action(act);
8411 trace_xdp_exception(rx_ring->netdev, xdp_prog, act);
8414 result = IGB_XDP_CONSUMED;
8418 return ERR_PTR(-result);
8421 static unsigned int igb_rx_frame_truesize(struct igb_ring *rx_ring,
8424 unsigned int truesize;
8426 #if (PAGE_SIZE < 8192)
8427 truesize = igb_rx_pg_size(rx_ring) / 2; /* Must be power-of-2 */
8429 truesize = ring_uses_build_skb(rx_ring) ?
8430 SKB_DATA_ALIGN(IGB_SKB_PAD + size) +
8431 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) :
8432 SKB_DATA_ALIGN(size);
8437 static void igb_rx_buffer_flip(struct igb_ring *rx_ring,
8438 struct igb_rx_buffer *rx_buffer,
8441 unsigned int truesize = igb_rx_frame_truesize(rx_ring, size);
8442 #if (PAGE_SIZE < 8192)
8443 rx_buffer->page_offset ^= truesize;
8445 rx_buffer->page_offset += truesize;
8449 static inline void igb_rx_checksum(struct igb_ring *ring,
8450 union e1000_adv_rx_desc *rx_desc,
8451 struct sk_buff *skb)
8453 skb_checksum_none_assert(skb);
8455 /* Ignore Checksum bit is set */
8456 if (igb_test_staterr(rx_desc, E1000_RXD_STAT_IXSM))
8459 /* Rx checksum disabled via ethtool */
8460 if (!(ring->netdev->features & NETIF_F_RXCSUM))
8463 /* TCP/UDP checksum error bit is set */
8464 if (igb_test_staterr(rx_desc,
8465 E1000_RXDEXT_STATERR_TCPE |
8466 E1000_RXDEXT_STATERR_IPE)) {
8467 /* work around errata with sctp packets where the TCPE aka
8468 * L4E bit is set incorrectly on 64 byte (60 byte w/o crc)
8469 * packets, (aka let the stack check the crc32c)
8471 if (!((skb->len == 60) &&
8472 test_bit(IGB_RING_FLAG_RX_SCTP_CSUM, &ring->flags))) {
8473 u64_stats_update_begin(&ring->rx_syncp);
8474 ring->rx_stats.csum_err++;
8475 u64_stats_update_end(&ring->rx_syncp);
8477 /* let the stack verify checksum errors */
8480 /* It must be a TCP or UDP packet with a valid checksum */
8481 if (igb_test_staterr(rx_desc, E1000_RXD_STAT_TCPCS |
8482 E1000_RXD_STAT_UDPCS))
8483 skb->ip_summed = CHECKSUM_UNNECESSARY;
8485 dev_dbg(ring->dev, "cksum success: bits %08X\n",
8486 le32_to_cpu(rx_desc->wb.upper.status_error));
8489 static inline void igb_rx_hash(struct igb_ring *ring,
8490 union e1000_adv_rx_desc *rx_desc,
8491 struct sk_buff *skb)
8493 if (ring->netdev->features & NETIF_F_RXHASH)
8495 le32_to_cpu(rx_desc->wb.lower.hi_dword.rss),
8500 * igb_is_non_eop - process handling of non-EOP buffers
8501 * @rx_ring: Rx ring being processed
8502 * @rx_desc: Rx descriptor for current buffer
8504 * This function updates next to clean. If the buffer is an EOP buffer
8505 * this function exits returning false, otherwise it will place the
8506 * sk_buff in the next buffer to be chained and return true indicating
8507 * that this is in fact a non-EOP buffer.
8509 static bool igb_is_non_eop(struct igb_ring *rx_ring,
8510 union e1000_adv_rx_desc *rx_desc)
8512 u32 ntc = rx_ring->next_to_clean + 1;
8514 /* fetch, update, and store next to clean */
8515 ntc = (ntc < rx_ring->count) ? ntc : 0;
8516 rx_ring->next_to_clean = ntc;
8518 prefetch(IGB_RX_DESC(rx_ring, ntc));
8520 if (likely(igb_test_staterr(rx_desc, E1000_RXD_STAT_EOP)))
8527 * igb_cleanup_headers - Correct corrupted or empty headers
8528 * @rx_ring: rx descriptor ring packet is being transacted on
8529 * @rx_desc: pointer to the EOP Rx descriptor
8530 * @skb: pointer to current skb being fixed
8532 * Address the case where we are pulling data in on pages only
8533 * and as such no data is present in the skb header.
8535 * In addition if skb is not at least 60 bytes we need to pad it so that
8536 * it is large enough to qualify as a valid Ethernet frame.
8538 * Returns true if an error was encountered and skb was freed.
8540 static bool igb_cleanup_headers(struct igb_ring *rx_ring,
8541 union e1000_adv_rx_desc *rx_desc,
8542 struct sk_buff *skb)
8544 /* XDP packets use error pointer so abort at this point */
8548 if (unlikely((igb_test_staterr(rx_desc,
8549 E1000_RXDEXT_ERR_FRAME_ERR_MASK)))) {
8550 struct net_device *netdev = rx_ring->netdev;
8551 if (!(netdev->features & NETIF_F_RXALL)) {
8552 dev_kfree_skb_any(skb);
8557 /* if eth_skb_pad returns an error the skb was freed */
8558 if (eth_skb_pad(skb))
8565 * igb_process_skb_fields - Populate skb header fields from Rx descriptor
8566 * @rx_ring: rx descriptor ring packet is being transacted on
8567 * @rx_desc: pointer to the EOP Rx descriptor
8568 * @skb: pointer to current skb being populated
8570 * This function checks the ring, descriptor, and packet information in
8571 * order to populate the hash, checksum, VLAN, timestamp, protocol, and
8572 * other fields within the skb.
8574 static void igb_process_skb_fields(struct igb_ring *rx_ring,
8575 union e1000_adv_rx_desc *rx_desc,
8576 struct sk_buff *skb)
8578 struct net_device *dev = rx_ring->netdev;
8580 igb_rx_hash(rx_ring, rx_desc, skb);
8582 igb_rx_checksum(rx_ring, rx_desc, skb);
8584 if (igb_test_staterr(rx_desc, E1000_RXDADV_STAT_TS) &&
8585 !igb_test_staterr(rx_desc, E1000_RXDADV_STAT_TSIP))
8586 igb_ptp_rx_rgtstamp(rx_ring->q_vector, skb);
8588 if ((dev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
8589 igb_test_staterr(rx_desc, E1000_RXD_STAT_VP)) {
8592 if (igb_test_staterr(rx_desc, E1000_RXDEXT_STATERR_LB) &&
8593 test_bit(IGB_RING_FLAG_RX_LB_VLAN_BSWAP, &rx_ring->flags))
8594 vid = be16_to_cpu((__force __be16)rx_desc->wb.upper.vlan);
8596 vid = le16_to_cpu(rx_desc->wb.upper.vlan);
8598 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vid);
8601 skb_record_rx_queue(skb, rx_ring->queue_index);
8603 skb->protocol = eth_type_trans(skb, rx_ring->netdev);
8606 static unsigned int igb_rx_offset(struct igb_ring *rx_ring)
8608 return ring_uses_build_skb(rx_ring) ? IGB_SKB_PAD : 0;
8611 static struct igb_rx_buffer *igb_get_rx_buffer(struct igb_ring *rx_ring,
8612 const unsigned int size, int *rx_buf_pgcnt)
8614 struct igb_rx_buffer *rx_buffer;
8616 rx_buffer = &rx_ring->rx_buffer_info[rx_ring->next_to_clean];
8618 #if (PAGE_SIZE < 8192)
8619 page_count(rx_buffer->page);
8623 prefetchw(rx_buffer->page);
8625 /* we are reusing so sync this buffer for CPU use */
8626 dma_sync_single_range_for_cpu(rx_ring->dev,
8628 rx_buffer->page_offset,
8632 rx_buffer->pagecnt_bias--;
8637 static void igb_put_rx_buffer(struct igb_ring *rx_ring,
8638 struct igb_rx_buffer *rx_buffer, int rx_buf_pgcnt)
8640 if (igb_can_reuse_rx_page(rx_buffer, rx_buf_pgcnt)) {
8641 /* hand second half of page back to the ring */
8642 igb_reuse_rx_page(rx_ring, rx_buffer);
8644 /* We are not reusing the buffer so unmap it and free
8645 * any references we are holding to it
8647 dma_unmap_page_attrs(rx_ring->dev, rx_buffer->dma,
8648 igb_rx_pg_size(rx_ring), DMA_FROM_DEVICE,
8650 __page_frag_cache_drain(rx_buffer->page,
8651 rx_buffer->pagecnt_bias);
8654 /* clear contents of rx_buffer */
8655 rx_buffer->page = NULL;
8658 static int igb_clean_rx_irq(struct igb_q_vector *q_vector, const int budget)
8660 struct igb_adapter *adapter = q_vector->adapter;
8661 struct igb_ring *rx_ring = q_vector->rx.ring;
8662 struct sk_buff *skb = rx_ring->skb;
8663 unsigned int total_bytes = 0, total_packets = 0;
8664 u16 cleaned_count = igb_desc_unused(rx_ring);
8665 unsigned int xdp_xmit = 0;
8666 struct xdp_buff xdp;
8670 /* Frame size depend on rx_ring setup when PAGE_SIZE=4K */
8671 #if (PAGE_SIZE < 8192)
8672 frame_sz = igb_rx_frame_truesize(rx_ring, 0);
8674 xdp_init_buff(&xdp, frame_sz, &rx_ring->xdp_rxq);
8676 while (likely(total_packets < budget)) {
8677 union e1000_adv_rx_desc *rx_desc;
8678 struct igb_rx_buffer *rx_buffer;
8679 ktime_t timestamp = 0;
8684 /* return some buffers to hardware, one at a time is too slow */
8685 if (cleaned_count >= IGB_RX_BUFFER_WRITE) {
8686 igb_alloc_rx_buffers(rx_ring, cleaned_count);
8690 rx_desc = IGB_RX_DESC(rx_ring, rx_ring->next_to_clean);
8691 size = le16_to_cpu(rx_desc->wb.upper.length);
8695 /* This memory barrier is needed to keep us from reading
8696 * any other fields out of the rx_desc until we know the
8697 * descriptor has been written back
8701 rx_buffer = igb_get_rx_buffer(rx_ring, size, &rx_buf_pgcnt);
8702 pktbuf = page_address(rx_buffer->page) + rx_buffer->page_offset;
8704 /* pull rx packet timestamp if available and valid */
8705 if (igb_test_staterr(rx_desc, E1000_RXDADV_STAT_TSIP)) {
8708 ts_hdr_len = igb_ptp_rx_pktstamp(rx_ring->q_vector,
8709 pktbuf, ×tamp);
8711 pkt_offset += ts_hdr_len;
8715 /* retrieve a buffer from the ring */
8717 unsigned char *hard_start = pktbuf - igb_rx_offset(rx_ring);
8718 unsigned int offset = pkt_offset + igb_rx_offset(rx_ring);
8720 xdp_prepare_buff(&xdp, hard_start, offset, size, true);
8721 #if (PAGE_SIZE > 4096)
8722 /* At larger PAGE_SIZE, frame_sz depend on len size */
8723 xdp.frame_sz = igb_rx_frame_truesize(rx_ring, size);
8725 skb = igb_run_xdp(adapter, rx_ring, &xdp);
8729 unsigned int xdp_res = -PTR_ERR(skb);
8731 if (xdp_res & (IGB_XDP_TX | IGB_XDP_REDIR)) {
8732 xdp_xmit |= xdp_res;
8733 igb_rx_buffer_flip(rx_ring, rx_buffer, size);
8735 rx_buffer->pagecnt_bias++;
8738 total_bytes += size;
8740 igb_add_rx_frag(rx_ring, rx_buffer, skb, size);
8741 else if (ring_uses_build_skb(rx_ring))
8742 skb = igb_build_skb(rx_ring, rx_buffer, &xdp,
8745 skb = igb_construct_skb(rx_ring, rx_buffer,
8748 /* exit if we failed to retrieve a buffer */
8750 rx_ring->rx_stats.alloc_failed++;
8751 rx_buffer->pagecnt_bias++;
8755 igb_put_rx_buffer(rx_ring, rx_buffer, rx_buf_pgcnt);
8758 /* fetch next buffer in frame if non-eop */
8759 if (igb_is_non_eop(rx_ring, rx_desc))
8762 /* verify the packet layout is correct */
8763 if (igb_cleanup_headers(rx_ring, rx_desc, skb)) {
8768 /* probably a little skewed due to removing CRC */
8769 total_bytes += skb->len;
8771 /* populate checksum, timestamp, VLAN, and protocol */
8772 igb_process_skb_fields(rx_ring, rx_desc, skb);
8774 napi_gro_receive(&q_vector->napi, skb);
8776 /* reset skb pointer */
8779 /* update budget accounting */
8783 /* place incomplete frames back on ring for completion */
8786 if (xdp_xmit & IGB_XDP_REDIR)
8789 if (xdp_xmit & IGB_XDP_TX) {
8790 struct igb_ring *tx_ring = igb_xdp_tx_queue_mapping(adapter);
8792 igb_xdp_ring_update_tail(tx_ring);
8795 u64_stats_update_begin(&rx_ring->rx_syncp);
8796 rx_ring->rx_stats.packets += total_packets;
8797 rx_ring->rx_stats.bytes += total_bytes;
8798 u64_stats_update_end(&rx_ring->rx_syncp);
8799 q_vector->rx.total_packets += total_packets;
8800 q_vector->rx.total_bytes += total_bytes;
8803 igb_alloc_rx_buffers(rx_ring, cleaned_count);
8805 return total_packets;
8808 static bool igb_alloc_mapped_page(struct igb_ring *rx_ring,
8809 struct igb_rx_buffer *bi)
8811 struct page *page = bi->page;
8814 /* since we are recycling buffers we should seldom need to alloc */
8818 /* alloc new page for storage */
8819 page = dev_alloc_pages(igb_rx_pg_order(rx_ring));
8820 if (unlikely(!page)) {
8821 rx_ring->rx_stats.alloc_failed++;
8825 /* map page for use */
8826 dma = dma_map_page_attrs(rx_ring->dev, page, 0,
8827 igb_rx_pg_size(rx_ring),
8831 /* if mapping failed free memory back to system since
8832 * there isn't much point in holding memory we can't use
8834 if (dma_mapping_error(rx_ring->dev, dma)) {
8835 __free_pages(page, igb_rx_pg_order(rx_ring));
8837 rx_ring->rx_stats.alloc_failed++;
8843 bi->page_offset = igb_rx_offset(rx_ring);
8844 page_ref_add(page, USHRT_MAX - 1);
8845 bi->pagecnt_bias = USHRT_MAX;
8851 * igb_alloc_rx_buffers - Replace used receive buffers
8852 * @rx_ring: rx descriptor ring to allocate new receive buffers
8853 * @cleaned_count: count of buffers to allocate
8855 void igb_alloc_rx_buffers(struct igb_ring *rx_ring, u16 cleaned_count)
8857 union e1000_adv_rx_desc *rx_desc;
8858 struct igb_rx_buffer *bi;
8859 u16 i = rx_ring->next_to_use;
8866 rx_desc = IGB_RX_DESC(rx_ring, i);
8867 bi = &rx_ring->rx_buffer_info[i];
8868 i -= rx_ring->count;
8870 bufsz = igb_rx_bufsz(rx_ring);
8873 if (!igb_alloc_mapped_page(rx_ring, bi))
8876 /* sync the buffer for use by the device */
8877 dma_sync_single_range_for_device(rx_ring->dev, bi->dma,
8878 bi->page_offset, bufsz,
8881 /* Refresh the desc even if buffer_addrs didn't change
8882 * because each write-back erases this info.
8884 rx_desc->read.pkt_addr = cpu_to_le64(bi->dma + bi->page_offset);
8890 rx_desc = IGB_RX_DESC(rx_ring, 0);
8891 bi = rx_ring->rx_buffer_info;
8892 i -= rx_ring->count;
8895 /* clear the length for the next_to_use descriptor */
8896 rx_desc->wb.upper.length = 0;
8899 } while (cleaned_count);
8901 i += rx_ring->count;
8903 if (rx_ring->next_to_use != i) {
8904 /* record the next descriptor to use */
8905 rx_ring->next_to_use = i;
8907 /* update next to alloc since we have filled the ring */
8908 rx_ring->next_to_alloc = i;
8910 /* Force memory writes to complete before letting h/w
8911 * know there are new descriptors to fetch. (Only
8912 * applicable for weak-ordered memory model archs,
8916 writel(i, rx_ring->tail);
8922 * @netdev: pointer to netdev struct
8923 * @ifr: interface structure
8924 * @cmd: ioctl command to execute
8926 static int igb_mii_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
8928 struct igb_adapter *adapter = netdev_priv(netdev);
8929 struct mii_ioctl_data *data = if_mii(ifr);
8931 if (adapter->hw.phy.media_type != e1000_media_type_copper)
8936 data->phy_id = adapter->hw.phy.addr;
8939 if (igb_read_phy_reg(&adapter->hw, data->reg_num & 0x1F,
8952 * @netdev: pointer to netdev struct
8953 * @ifr: interface structure
8954 * @cmd: ioctl command to execute
8956 static int igb_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
8962 return igb_mii_ioctl(netdev, ifr, cmd);
8964 return igb_ptp_get_ts_config(netdev, ifr);
8966 return igb_ptp_set_ts_config(netdev, ifr);
8972 void igb_read_pci_cfg(struct e1000_hw *hw, u32 reg, u16 *value)
8974 struct igb_adapter *adapter = hw->back;
8976 pci_read_config_word(adapter->pdev, reg, value);
8979 void igb_write_pci_cfg(struct e1000_hw *hw, u32 reg, u16 *value)
8981 struct igb_adapter *adapter = hw->back;
8983 pci_write_config_word(adapter->pdev, reg, *value);
8986 s32 igb_read_pcie_cap_reg(struct e1000_hw *hw, u32 reg, u16 *value)
8988 struct igb_adapter *adapter = hw->back;
8990 if (pcie_capability_read_word(adapter->pdev, reg, value))
8991 return -E1000_ERR_CONFIG;
8996 s32 igb_write_pcie_cap_reg(struct e1000_hw *hw, u32 reg, u16 *value)
8998 struct igb_adapter *adapter = hw->back;
9000 if (pcie_capability_write_word(adapter->pdev, reg, *value))
9001 return -E1000_ERR_CONFIG;
9006 static void igb_vlan_mode(struct net_device *netdev, netdev_features_t features)
9008 struct igb_adapter *adapter = netdev_priv(netdev);
9009 struct e1000_hw *hw = &adapter->hw;
9011 bool enable = !!(features & NETIF_F_HW_VLAN_CTAG_RX);
9014 /* enable VLAN tag insert/strip */
9015 ctrl = rd32(E1000_CTRL);
9016 ctrl |= E1000_CTRL_VME;
9017 wr32(E1000_CTRL, ctrl);
9019 /* Disable CFI check */
9020 rctl = rd32(E1000_RCTL);
9021 rctl &= ~E1000_RCTL_CFIEN;
9022 wr32(E1000_RCTL, rctl);
9024 /* disable VLAN tag insert/strip */
9025 ctrl = rd32(E1000_CTRL);
9026 ctrl &= ~E1000_CTRL_VME;
9027 wr32(E1000_CTRL, ctrl);
9030 igb_set_vf_vlan_strip(adapter, adapter->vfs_allocated_count, enable);
9033 static int igb_vlan_rx_add_vid(struct net_device *netdev,
9034 __be16 proto, u16 vid)
9036 struct igb_adapter *adapter = netdev_priv(netdev);
9037 struct e1000_hw *hw = &adapter->hw;
9038 int pf_id = adapter->vfs_allocated_count;
9040 /* add the filter since PF can receive vlans w/o entry in vlvf */
9041 if (!vid || !(adapter->flags & IGB_FLAG_VLAN_PROMISC))
9042 igb_vfta_set(hw, vid, pf_id, true, !!vid);
9044 set_bit(vid, adapter->active_vlans);
9049 static int igb_vlan_rx_kill_vid(struct net_device *netdev,
9050 __be16 proto, u16 vid)
9052 struct igb_adapter *adapter = netdev_priv(netdev);
9053 int pf_id = adapter->vfs_allocated_count;
9054 struct e1000_hw *hw = &adapter->hw;
9056 /* remove VID from filter table */
9057 if (vid && !(adapter->flags & IGB_FLAG_VLAN_PROMISC))
9058 igb_vfta_set(hw, vid, pf_id, false, true);
9060 clear_bit(vid, adapter->active_vlans);
9065 static void igb_restore_vlan(struct igb_adapter *adapter)
9069 igb_vlan_mode(adapter->netdev, adapter->netdev->features);
9070 igb_vlan_rx_add_vid(adapter->netdev, htons(ETH_P_8021Q), 0);
9072 for_each_set_bit_from(vid, adapter->active_vlans, VLAN_N_VID)
9073 igb_vlan_rx_add_vid(adapter->netdev, htons(ETH_P_8021Q), vid);
9076 int igb_set_spd_dplx(struct igb_adapter *adapter, u32 spd, u8 dplx)
9078 struct pci_dev *pdev = adapter->pdev;
9079 struct e1000_mac_info *mac = &adapter->hw.mac;
9083 /* Make sure dplx is at most 1 bit and lsb of speed is not set
9084 * for the switch() below to work
9086 if ((spd & 1) || (dplx & ~1))
9089 /* Fiber NIC's only allow 1000 gbps Full duplex
9090 * and 100Mbps Full duplex for 100baseFx sfp
9092 if (adapter->hw.phy.media_type == e1000_media_type_internal_serdes) {
9093 switch (spd + dplx) {
9094 case SPEED_10 + DUPLEX_HALF:
9095 case SPEED_10 + DUPLEX_FULL:
9096 case SPEED_100 + DUPLEX_HALF:
9103 switch (spd + dplx) {
9104 case SPEED_10 + DUPLEX_HALF:
9105 mac->forced_speed_duplex = ADVERTISE_10_HALF;
9107 case SPEED_10 + DUPLEX_FULL:
9108 mac->forced_speed_duplex = ADVERTISE_10_FULL;
9110 case SPEED_100 + DUPLEX_HALF:
9111 mac->forced_speed_duplex = ADVERTISE_100_HALF;
9113 case SPEED_100 + DUPLEX_FULL:
9114 mac->forced_speed_duplex = ADVERTISE_100_FULL;
9116 case SPEED_1000 + DUPLEX_FULL:
9118 adapter->hw.phy.autoneg_advertised = ADVERTISE_1000_FULL;
9120 case SPEED_1000 + DUPLEX_HALF: /* not supported */
9125 /* clear MDI, MDI(-X) override is only allowed when autoneg enabled */
9126 adapter->hw.phy.mdix = AUTO_ALL_MODES;
9131 dev_err(&pdev->dev, "Unsupported Speed/Duplex configuration\n");
9135 static int __igb_shutdown(struct pci_dev *pdev, bool *enable_wake,
9138 struct net_device *netdev = pci_get_drvdata(pdev);
9139 struct igb_adapter *adapter = netdev_priv(netdev);
9140 struct e1000_hw *hw = &adapter->hw;
9141 u32 ctrl, rctl, status;
9142 u32 wufc = runtime ? E1000_WUFC_LNKC : adapter->wol;
9146 netif_device_detach(netdev);
9148 if (netif_running(netdev))
9149 __igb_close(netdev, true);
9151 igb_ptp_suspend(adapter);
9153 igb_clear_interrupt_scheme(adapter);
9156 status = rd32(E1000_STATUS);
9157 if (status & E1000_STATUS_LU)
9158 wufc &= ~E1000_WUFC_LNKC;
9161 igb_setup_rctl(adapter);
9162 igb_set_rx_mode(netdev);
9164 /* turn on all-multi mode if wake on multicast is enabled */
9165 if (wufc & E1000_WUFC_MC) {
9166 rctl = rd32(E1000_RCTL);
9167 rctl |= E1000_RCTL_MPE;
9168 wr32(E1000_RCTL, rctl);
9171 ctrl = rd32(E1000_CTRL);
9172 ctrl |= E1000_CTRL_ADVD3WUC;
9173 wr32(E1000_CTRL, ctrl);
9175 /* Allow time for pending master requests to run */
9176 igb_disable_pcie_master(hw);
9178 wr32(E1000_WUC, E1000_WUC_PME_EN);
9179 wr32(E1000_WUFC, wufc);
9182 wr32(E1000_WUFC, 0);
9185 wake = wufc || adapter->en_mng_pt;
9187 igb_power_down_link(adapter);
9189 igb_power_up_link(adapter);
9192 *enable_wake = wake;
9194 /* Release control of h/w to f/w. If f/w is AMT enabled, this
9195 * would have already happened in close and is redundant.
9197 igb_release_hw_control(adapter);
9199 pci_disable_device(pdev);
9204 static void igb_deliver_wake_packet(struct net_device *netdev)
9206 struct igb_adapter *adapter = netdev_priv(netdev);
9207 struct e1000_hw *hw = &adapter->hw;
9208 struct sk_buff *skb;
9211 wupl = rd32(E1000_WUPL) & E1000_WUPL_MASK;
9213 /* WUPM stores only the first 128 bytes of the wake packet.
9214 * Read the packet only if we have the whole thing.
9216 if ((wupl == 0) || (wupl > E1000_WUPM_BYTES))
9219 skb = netdev_alloc_skb_ip_align(netdev, E1000_WUPM_BYTES);
9225 /* Ensure reads are 32-bit aligned */
9226 wupl = roundup(wupl, 4);
9228 memcpy_fromio(skb->data, hw->hw_addr + E1000_WUPM_REG(0), wupl);
9230 skb->protocol = eth_type_trans(skb, netdev);
9234 static int __maybe_unused igb_suspend(struct device *dev)
9236 return __igb_shutdown(to_pci_dev(dev), NULL, 0);
9239 static int __maybe_unused igb_resume(struct device *dev)
9241 struct pci_dev *pdev = to_pci_dev(dev);
9242 struct net_device *netdev = pci_get_drvdata(pdev);
9243 struct igb_adapter *adapter = netdev_priv(netdev);
9244 struct e1000_hw *hw = &adapter->hw;
9247 pci_set_power_state(pdev, PCI_D0);
9248 pci_restore_state(pdev);
9249 pci_save_state(pdev);
9251 if (!pci_device_is_present(pdev))
9253 err = pci_enable_device_mem(pdev);
9256 "igb: Cannot enable PCI device from suspend\n");
9259 pci_set_master(pdev);
9261 pci_enable_wake(pdev, PCI_D3hot, 0);
9262 pci_enable_wake(pdev, PCI_D3cold, 0);
9264 if (igb_init_interrupt_scheme(adapter, true)) {
9265 dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
9271 /* let the f/w know that the h/w is now under the control of the
9274 igb_get_hw_control(adapter);
9276 val = rd32(E1000_WUS);
9277 if (val & WAKE_PKT_WUS)
9278 igb_deliver_wake_packet(netdev);
9280 wr32(E1000_WUS, ~0);
9283 if (!err && netif_running(netdev))
9284 err = __igb_open(netdev, true);
9287 netif_device_attach(netdev);
9293 static int __maybe_unused igb_runtime_idle(struct device *dev)
9295 struct net_device *netdev = dev_get_drvdata(dev);
9296 struct igb_adapter *adapter = netdev_priv(netdev);
9298 if (!igb_has_link(adapter))
9299 pm_schedule_suspend(dev, MSEC_PER_SEC * 5);
9304 static int __maybe_unused igb_runtime_suspend(struct device *dev)
9306 return __igb_shutdown(to_pci_dev(dev), NULL, 1);
9309 static int __maybe_unused igb_runtime_resume(struct device *dev)
9311 return igb_resume(dev);
9314 static void igb_shutdown(struct pci_dev *pdev)
9318 __igb_shutdown(pdev, &wake, 0);
9320 if (system_state == SYSTEM_POWER_OFF) {
9321 pci_wake_from_d3(pdev, wake);
9322 pci_set_power_state(pdev, PCI_D3hot);
9326 #ifdef CONFIG_PCI_IOV
9327 static int igb_sriov_reinit(struct pci_dev *dev)
9329 struct net_device *netdev = pci_get_drvdata(dev);
9330 struct igb_adapter *adapter = netdev_priv(netdev);
9331 struct pci_dev *pdev = adapter->pdev;
9335 if (netif_running(netdev))
9340 igb_clear_interrupt_scheme(adapter);
9342 igb_init_queue_configuration(adapter);
9344 if (igb_init_interrupt_scheme(adapter, true)) {
9346 dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
9350 if (netif_running(netdev))
9358 static int igb_pci_disable_sriov(struct pci_dev *dev)
9360 int err = igb_disable_sriov(dev);
9363 err = igb_sriov_reinit(dev);
9368 static int igb_pci_enable_sriov(struct pci_dev *dev, int num_vfs)
9370 int err = igb_enable_sriov(dev, num_vfs);
9375 err = igb_sriov_reinit(dev);
9384 static int igb_pci_sriov_configure(struct pci_dev *dev, int num_vfs)
9386 #ifdef CONFIG_PCI_IOV
9388 return igb_pci_disable_sriov(dev);
9390 return igb_pci_enable_sriov(dev, num_vfs);
9396 * igb_io_error_detected - called when PCI error is detected
9397 * @pdev: Pointer to PCI device
9398 * @state: The current pci connection state
9400 * This function is called after a PCI bus error affecting
9401 * this device has been detected.
9403 static pci_ers_result_t igb_io_error_detected(struct pci_dev *pdev,
9404 pci_channel_state_t state)
9406 struct net_device *netdev = pci_get_drvdata(pdev);
9407 struct igb_adapter *adapter = netdev_priv(netdev);
9409 netif_device_detach(netdev);
9411 if (state == pci_channel_io_perm_failure)
9412 return PCI_ERS_RESULT_DISCONNECT;
9414 if (netif_running(netdev))
9416 pci_disable_device(pdev);
9418 /* Request a slot slot reset. */
9419 return PCI_ERS_RESULT_NEED_RESET;
9423 * igb_io_slot_reset - called after the pci bus has been reset.
9424 * @pdev: Pointer to PCI device
9426 * Restart the card from scratch, as if from a cold-boot. Implementation
9427 * resembles the first-half of the igb_resume routine.
9429 static pci_ers_result_t igb_io_slot_reset(struct pci_dev *pdev)
9431 struct net_device *netdev = pci_get_drvdata(pdev);
9432 struct igb_adapter *adapter = netdev_priv(netdev);
9433 struct e1000_hw *hw = &adapter->hw;
9434 pci_ers_result_t result;
9436 if (pci_enable_device_mem(pdev)) {
9438 "Cannot re-enable PCI device after reset.\n");
9439 result = PCI_ERS_RESULT_DISCONNECT;
9441 pci_set_master(pdev);
9442 pci_restore_state(pdev);
9443 pci_save_state(pdev);
9445 pci_enable_wake(pdev, PCI_D3hot, 0);
9446 pci_enable_wake(pdev, PCI_D3cold, 0);
9448 /* In case of PCI error, adapter lose its HW address
9449 * so we should re-assign it here.
9451 hw->hw_addr = adapter->io_addr;
9454 wr32(E1000_WUS, ~0);
9455 result = PCI_ERS_RESULT_RECOVERED;
9462 * igb_io_resume - called when traffic can start flowing again.
9463 * @pdev: Pointer to PCI device
9465 * This callback is called when the error recovery driver tells us that
9466 * its OK to resume normal operation. Implementation resembles the
9467 * second-half of the igb_resume routine.
9469 static void igb_io_resume(struct pci_dev *pdev)
9471 struct net_device *netdev = pci_get_drvdata(pdev);
9472 struct igb_adapter *adapter = netdev_priv(netdev);
9474 if (netif_running(netdev)) {
9475 if (igb_up(adapter)) {
9476 dev_err(&pdev->dev, "igb_up failed after reset\n");
9481 netif_device_attach(netdev);
9483 /* let the f/w know that the h/w is now under the control of the
9486 igb_get_hw_control(adapter);
9490 * igb_rar_set_index - Sync RAL[index] and RAH[index] registers with MAC table
9491 * @adapter: Pointer to adapter structure
9492 * @index: Index of the RAR entry which need to be synced with MAC table
9494 static void igb_rar_set_index(struct igb_adapter *adapter, u32 index)
9496 struct e1000_hw *hw = &adapter->hw;
9497 u32 rar_low, rar_high;
9498 u8 *addr = adapter->mac_table[index].addr;
9500 /* HW expects these to be in network order when they are plugged
9501 * into the registers which are little endian. In order to guarantee
9502 * that ordering we need to do an leXX_to_cpup here in order to be
9503 * ready for the byteswap that occurs with writel
9505 rar_low = le32_to_cpup((__le32 *)(addr));
9506 rar_high = le16_to_cpup((__le16 *)(addr + 4));
9508 /* Indicate to hardware the Address is Valid. */
9509 if (adapter->mac_table[index].state & IGB_MAC_STATE_IN_USE) {
9510 if (is_valid_ether_addr(addr))
9511 rar_high |= E1000_RAH_AV;
9513 if (adapter->mac_table[index].state & IGB_MAC_STATE_SRC_ADDR)
9514 rar_high |= E1000_RAH_ASEL_SRC_ADDR;
9516 switch (hw->mac.type) {
9519 if (adapter->mac_table[index].state &
9520 IGB_MAC_STATE_QUEUE_STEERING)
9521 rar_high |= E1000_RAH_QSEL_ENABLE;
9523 rar_high |= E1000_RAH_POOL_1 *
9524 adapter->mac_table[index].queue;
9527 rar_high |= E1000_RAH_POOL_1 <<
9528 adapter->mac_table[index].queue;
9533 wr32(E1000_RAL(index), rar_low);
9535 wr32(E1000_RAH(index), rar_high);
9539 static int igb_set_vf_mac(struct igb_adapter *adapter,
9540 int vf, unsigned char *mac_addr)
9542 struct e1000_hw *hw = &adapter->hw;
9543 /* VF MAC addresses start at end of receive addresses and moves
9544 * towards the first, as a result a collision should not be possible
9546 int rar_entry = hw->mac.rar_entry_count - (vf + 1);
9547 unsigned char *vf_mac_addr = adapter->vf_data[vf].vf_mac_addresses;
9549 ether_addr_copy(vf_mac_addr, mac_addr);
9550 ether_addr_copy(adapter->mac_table[rar_entry].addr, mac_addr);
9551 adapter->mac_table[rar_entry].queue = vf;
9552 adapter->mac_table[rar_entry].state |= IGB_MAC_STATE_IN_USE;
9553 igb_rar_set_index(adapter, rar_entry);
9558 static int igb_ndo_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
9560 struct igb_adapter *adapter = netdev_priv(netdev);
9562 if (vf >= adapter->vfs_allocated_count)
9565 /* Setting the VF MAC to 0 reverts the IGB_VF_FLAG_PF_SET_MAC
9566 * flag and allows to overwrite the MAC via VF netdev. This
9567 * is necessary to allow libvirt a way to restore the original
9568 * MAC after unbinding vfio-pci and reloading igbvf after shutting
9571 if (is_zero_ether_addr(mac)) {
9572 adapter->vf_data[vf].flags &= ~IGB_VF_FLAG_PF_SET_MAC;
9573 dev_info(&adapter->pdev->dev,
9574 "remove administratively set MAC on VF %d\n",
9576 } else if (is_valid_ether_addr(mac)) {
9577 adapter->vf_data[vf].flags |= IGB_VF_FLAG_PF_SET_MAC;
9578 dev_info(&adapter->pdev->dev, "setting MAC %pM on VF %d\n",
9580 dev_info(&adapter->pdev->dev,
9581 "Reload the VF driver to make this change effective.");
9582 /* Generate additional warning if PF is down */
9583 if (test_bit(__IGB_DOWN, &adapter->state)) {
9584 dev_warn(&adapter->pdev->dev,
9585 "The VF MAC address has been set, but the PF device is not up.\n");
9586 dev_warn(&adapter->pdev->dev,
9587 "Bring the PF device up before attempting to use the VF device.\n");
9592 return igb_set_vf_mac(adapter, vf, mac);
9595 static int igb_link_mbps(int internal_link_speed)
9597 switch (internal_link_speed) {
9607 static void igb_set_vf_rate_limit(struct e1000_hw *hw, int vf, int tx_rate,
9614 /* Calculate the rate factor values to set */
9615 rf_int = link_speed / tx_rate;
9616 rf_dec = (link_speed - (rf_int * tx_rate));
9617 rf_dec = (rf_dec * BIT(E1000_RTTBCNRC_RF_INT_SHIFT)) /
9620 bcnrc_val = E1000_RTTBCNRC_RS_ENA;
9621 bcnrc_val |= ((rf_int << E1000_RTTBCNRC_RF_INT_SHIFT) &
9622 E1000_RTTBCNRC_RF_INT_MASK);
9623 bcnrc_val |= (rf_dec & E1000_RTTBCNRC_RF_DEC_MASK);
9628 wr32(E1000_RTTDQSEL, vf); /* vf X uses queue X */
9629 /* Set global transmit compensation time to the MMW_SIZE in RTTBCNRM
9630 * register. MMW_SIZE=0x014 if 9728-byte jumbo is supported.
9632 wr32(E1000_RTTBCNRM, 0x14);
9633 wr32(E1000_RTTBCNRC, bcnrc_val);
9636 static void igb_check_vf_rate_limit(struct igb_adapter *adapter)
9638 int actual_link_speed, i;
9639 bool reset_rate = false;
9641 /* VF TX rate limit was not set or not supported */
9642 if ((adapter->vf_rate_link_speed == 0) ||
9643 (adapter->hw.mac.type != e1000_82576))
9646 actual_link_speed = igb_link_mbps(adapter->link_speed);
9647 if (actual_link_speed != adapter->vf_rate_link_speed) {
9649 adapter->vf_rate_link_speed = 0;
9650 dev_info(&adapter->pdev->dev,
9651 "Link speed has been changed. VF Transmit rate is disabled\n");
9654 for (i = 0; i < adapter->vfs_allocated_count; i++) {
9656 adapter->vf_data[i].tx_rate = 0;
9658 igb_set_vf_rate_limit(&adapter->hw, i,
9659 adapter->vf_data[i].tx_rate,
9664 static int igb_ndo_set_vf_bw(struct net_device *netdev, int vf,
9665 int min_tx_rate, int max_tx_rate)
9667 struct igb_adapter *adapter = netdev_priv(netdev);
9668 struct e1000_hw *hw = &adapter->hw;
9669 int actual_link_speed;
9671 if (hw->mac.type != e1000_82576)
9677 actual_link_speed = igb_link_mbps(adapter->link_speed);
9678 if ((vf >= adapter->vfs_allocated_count) ||
9679 (!(rd32(E1000_STATUS) & E1000_STATUS_LU)) ||
9680 (max_tx_rate < 0) ||
9681 (max_tx_rate > actual_link_speed))
9684 adapter->vf_rate_link_speed = actual_link_speed;
9685 adapter->vf_data[vf].tx_rate = (u16)max_tx_rate;
9686 igb_set_vf_rate_limit(hw, vf, max_tx_rate, actual_link_speed);
9691 static int igb_ndo_set_vf_spoofchk(struct net_device *netdev, int vf,
9694 struct igb_adapter *adapter = netdev_priv(netdev);
9695 struct e1000_hw *hw = &adapter->hw;
9696 u32 reg_val, reg_offset;
9698 if (!adapter->vfs_allocated_count)
9701 if (vf >= adapter->vfs_allocated_count)
9704 reg_offset = (hw->mac.type == e1000_82576) ? E1000_DTXSWC : E1000_TXSWC;
9705 reg_val = rd32(reg_offset);
9707 reg_val |= (BIT(vf) |
9708 BIT(vf + E1000_DTXSWC_VLAN_SPOOF_SHIFT));
9710 reg_val &= ~(BIT(vf) |
9711 BIT(vf + E1000_DTXSWC_VLAN_SPOOF_SHIFT));
9712 wr32(reg_offset, reg_val);
9714 adapter->vf_data[vf].spoofchk_enabled = setting;
9718 static int igb_ndo_set_vf_trust(struct net_device *netdev, int vf, bool setting)
9720 struct igb_adapter *adapter = netdev_priv(netdev);
9722 if (vf >= adapter->vfs_allocated_count)
9724 if (adapter->vf_data[vf].trusted == setting)
9727 adapter->vf_data[vf].trusted = setting;
9729 dev_info(&adapter->pdev->dev, "VF %u is %strusted\n",
9730 vf, setting ? "" : "not ");
9734 static int igb_ndo_get_vf_config(struct net_device *netdev,
9735 int vf, struct ifla_vf_info *ivi)
9737 struct igb_adapter *adapter = netdev_priv(netdev);
9738 if (vf >= adapter->vfs_allocated_count)
9741 memcpy(&ivi->mac, adapter->vf_data[vf].vf_mac_addresses, ETH_ALEN);
9742 ivi->max_tx_rate = adapter->vf_data[vf].tx_rate;
9743 ivi->min_tx_rate = 0;
9744 ivi->vlan = adapter->vf_data[vf].pf_vlan;
9745 ivi->qos = adapter->vf_data[vf].pf_qos;
9746 ivi->spoofchk = adapter->vf_data[vf].spoofchk_enabled;
9747 ivi->trusted = adapter->vf_data[vf].trusted;
9751 static void igb_vmm_control(struct igb_adapter *adapter)
9753 struct e1000_hw *hw = &adapter->hw;
9756 switch (hw->mac.type) {
9762 /* replication is not supported for 82575 */
9765 /* notify HW that the MAC is adding vlan tags */
9766 reg = rd32(E1000_DTXCTL);
9767 reg |= E1000_DTXCTL_VLAN_ADDED;
9768 wr32(E1000_DTXCTL, reg);
9771 /* enable replication vlan tag stripping */
9772 reg = rd32(E1000_RPLOLR);
9773 reg |= E1000_RPLOLR_STRVLAN;
9774 wr32(E1000_RPLOLR, reg);
9777 /* none of the above registers are supported by i350 */
9781 if (adapter->vfs_allocated_count) {
9782 igb_vmdq_set_loopback_pf(hw, true);
9783 igb_vmdq_set_replication_pf(hw, true);
9784 igb_vmdq_set_anti_spoofing_pf(hw, true,
9785 adapter->vfs_allocated_count);
9787 igb_vmdq_set_loopback_pf(hw, false);
9788 igb_vmdq_set_replication_pf(hw, false);
9792 static void igb_init_dmac(struct igb_adapter *adapter, u32 pba)
9794 struct e1000_hw *hw = &adapter->hw;
9798 if (hw->mac.type > e1000_82580) {
9799 if (adapter->flags & IGB_FLAG_DMAC) {
9802 /* force threshold to 0. */
9803 wr32(E1000_DMCTXTH, 0);
9805 /* DMA Coalescing high water mark needs to be greater
9806 * than the Rx threshold. Set hwm to PBA - max frame
9807 * size in 16B units, capping it at PBA - 6KB.
9809 hwm = 64 * (pba - 6);
9810 reg = rd32(E1000_FCRTC);
9811 reg &= ~E1000_FCRTC_RTH_COAL_MASK;
9812 reg |= ((hwm << E1000_FCRTC_RTH_COAL_SHIFT)
9813 & E1000_FCRTC_RTH_COAL_MASK);
9814 wr32(E1000_FCRTC, reg);
9816 /* Set the DMA Coalescing Rx threshold to PBA - 2 * max
9817 * frame size, capping it at PBA - 10KB.
9819 dmac_thr = pba - 10;
9820 reg = rd32(E1000_DMACR);
9821 reg &= ~E1000_DMACR_DMACTHR_MASK;
9822 reg |= ((dmac_thr << E1000_DMACR_DMACTHR_SHIFT)
9823 & E1000_DMACR_DMACTHR_MASK);
9825 /* transition to L0x or L1 if available..*/
9826 reg |= (E1000_DMACR_DMAC_EN | E1000_DMACR_DMAC_LX_MASK);
9828 /* watchdog timer= +-1000 usec in 32usec intervals */
9831 /* Disable BMC-to-OS Watchdog Enable */
9832 if (hw->mac.type != e1000_i354)
9833 reg &= ~E1000_DMACR_DC_BMC2OSW_EN;
9835 wr32(E1000_DMACR, reg);
9837 /* no lower threshold to disable
9838 * coalescing(smart fifb)-UTRESH=0
9840 wr32(E1000_DMCRTRH, 0);
9842 reg = (IGB_DMCTLX_DCFLUSH_DIS | 0x4);
9844 wr32(E1000_DMCTLX, reg);
9846 /* free space in tx packet buffer to wake from
9849 wr32(E1000_DMCTXTH, (IGB_MIN_TXPBSIZE -
9850 (IGB_TX_BUF_4096 + adapter->max_frame_size)) >> 6);
9852 /* make low power state decision controlled
9855 reg = rd32(E1000_PCIEMISC);
9856 reg &= ~E1000_PCIEMISC_LX_DECISION;
9857 wr32(E1000_PCIEMISC, reg);
9858 } /* endif adapter->dmac is not disabled */
9859 } else if (hw->mac.type == e1000_82580) {
9860 u32 reg = rd32(E1000_PCIEMISC);
9862 wr32(E1000_PCIEMISC, reg & ~E1000_PCIEMISC_LX_DECISION);
9863 wr32(E1000_DMACR, 0);
9868 * igb_read_i2c_byte - Reads 8 bit word over I2C
9869 * @hw: pointer to hardware structure
9870 * @byte_offset: byte offset to read
9871 * @dev_addr: device address
9874 * Performs byte read operation over I2C interface at
9875 * a specified device address.
9877 s32 igb_read_i2c_byte(struct e1000_hw *hw, u8 byte_offset,
9878 u8 dev_addr, u8 *data)
9880 struct igb_adapter *adapter = container_of(hw, struct igb_adapter, hw);
9881 struct i2c_client *this_client = adapter->i2c_client;
9886 return E1000_ERR_I2C;
9888 swfw_mask = E1000_SWFW_PHY0_SM;
9890 if (hw->mac.ops.acquire_swfw_sync(hw, swfw_mask))
9891 return E1000_ERR_SWFW_SYNC;
9893 status = i2c_smbus_read_byte_data(this_client, byte_offset);
9894 hw->mac.ops.release_swfw_sync(hw, swfw_mask);
9897 return E1000_ERR_I2C;
9905 * igb_write_i2c_byte - Writes 8 bit word over I2C
9906 * @hw: pointer to hardware structure
9907 * @byte_offset: byte offset to write
9908 * @dev_addr: device address
9909 * @data: value to write
9911 * Performs byte write operation over I2C interface at
9912 * a specified device address.
9914 s32 igb_write_i2c_byte(struct e1000_hw *hw, u8 byte_offset,
9915 u8 dev_addr, u8 data)
9917 struct igb_adapter *adapter = container_of(hw, struct igb_adapter, hw);
9918 struct i2c_client *this_client = adapter->i2c_client;
9920 u16 swfw_mask = E1000_SWFW_PHY0_SM;
9923 return E1000_ERR_I2C;
9925 if (hw->mac.ops.acquire_swfw_sync(hw, swfw_mask))
9926 return E1000_ERR_SWFW_SYNC;
9927 status = i2c_smbus_write_byte_data(this_client, byte_offset, data);
9928 hw->mac.ops.release_swfw_sync(hw, swfw_mask);
9931 return E1000_ERR_I2C;
9937 int igb_reinit_queues(struct igb_adapter *adapter)
9939 struct net_device *netdev = adapter->netdev;
9940 struct pci_dev *pdev = adapter->pdev;
9943 if (netif_running(netdev))
9946 igb_reset_interrupt_capability(adapter);
9948 if (igb_init_interrupt_scheme(adapter, true)) {
9949 dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
9953 if (netif_running(netdev))
9954 err = igb_open(netdev);
9959 static void igb_nfc_filter_exit(struct igb_adapter *adapter)
9961 struct igb_nfc_filter *rule;
9963 spin_lock(&adapter->nfc_lock);
9965 hlist_for_each_entry(rule, &adapter->nfc_filter_list, nfc_node)
9966 igb_erase_filter(adapter, rule);
9968 hlist_for_each_entry(rule, &adapter->cls_flower_list, nfc_node)
9969 igb_erase_filter(adapter, rule);
9971 spin_unlock(&adapter->nfc_lock);
9974 static void igb_nfc_filter_restore(struct igb_adapter *adapter)
9976 struct igb_nfc_filter *rule;
9978 spin_lock(&adapter->nfc_lock);
9980 hlist_for_each_entry(rule, &adapter->nfc_filter_list, nfc_node)
9981 igb_add_filter(adapter, rule);
9983 spin_unlock(&adapter->nfc_lock);