blob: b88fae78536951cd76506a78cb19e044762fc943 [file] [log] [blame]
Carolyn Wybornye52c0f92014-04-11 01:46:06 +00001/* Intel(R) Gigabit Ethernet Linux driver
2 * Copyright(c) 2007-2014 Intel Corporation.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, see <http://www.gnu.org/licenses/>.
15 *
16 * The full GNU General Public License is included in this distribution in
17 * the file called "COPYING".
18 *
19 * Contact Information:
20 * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
21 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
22 */
Auke Kok9d5c8242008-01-24 02:22:38 -080023
Jeff Kirsher876d2d62011-10-21 20:01:34 +000024#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25
Auke Kok9d5c8242008-01-24 02:22:38 -080026#include <linux/module.h>
27#include <linux/types.h>
28#include <linux/init.h>
Jiri Pirkob2cb09b2011-07-21 03:27:27 +000029#include <linux/bitops.h>
Auke Kok9d5c8242008-01-24 02:22:38 -080030#include <linux/vmalloc.h>
31#include <linux/pagemap.h>
32#include <linux/netdevice.h>
Auke Kok9d5c8242008-01-24 02:22:38 -080033#include <linux/ipv6.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Auke Kok9d5c8242008-01-24 02:22:38 -080035#include <net/checksum.h>
36#include <net/ip6_checksum.h>
Andre Guedes05f9d3e2017-10-16 18:01:28 -070037#include <net/pkt_sched.h>
Patrick Ohlyc6cb0902009-02-12 05:03:42 +000038#include <linux/net_tstamp.h>
Auke Kok9d5c8242008-01-24 02:22:38 -080039#include <linux/mii.h>
40#include <linux/ethtool.h>
Jiri Pirko01789342011-08-16 06:29:00 +000041#include <linux/if.h>
Auke Kok9d5c8242008-01-24 02:22:38 -080042#include <linux/if_vlan.h>
43#include <linux/pci.h>
Alexander Duyckc54106b2008-10-16 21:26:57 -070044#include <linux/pci-aspm.h>
Auke Kok9d5c8242008-01-24 02:22:38 -080045#include <linux/delay.h>
46#include <linux/interrupt.h>
Alexander Duyck7d13a7d2011-08-26 07:44:32 +000047#include <linux/ip.h>
48#include <linux/tcp.h>
49#include <linux/sctp.h>
Auke Kok9d5c8242008-01-24 02:22:38 -080050#include <linux/if_ether.h>
Alexander Duyck40a914f2008-11-27 00:24:37 -080051#include <linux/aer.h>
Paul Gortmaker70c71602011-05-22 16:47:17 -040052#include <linux/prefetch.h>
Yan, Zheng749ab2c2012-01-04 20:23:37 +000053#include <linux/pm_runtime.h>
John Holland806ffb12016-02-18 12:10:52 +010054#include <linux/etherdevice.h>
Jeff Kirsher421e02f2008-10-17 11:08:31 -070055#ifdef CONFIG_IGB_DCA
Jeb Cramerfe4506b2008-07-08 15:07:55 -070056#include <linux/dca.h>
57#endif
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +000058#include <linux/i2c.h>
Auke Kok9d5c8242008-01-24 02:22:38 -080059#include "igb.h"
60
Carolyn Wyborny67b1b902013-04-17 16:44:53 +000061#define MAJ 5
Todd Fujinaka07423372016-08-24 08:40:23 -070062#define MIN 4
Todd Fujinaka6fb46902015-05-20 15:40:20 -070063#define BUILD 0
Carolyn Wyborny0d1fe822011-03-11 20:58:19 -080064#define DRV_VERSION __stringify(MAJ) "." __stringify(MIN) "." \
Carolyn Wyborny929dd042011-05-26 03:02:26 +000065__stringify(BUILD) "-k"
Andre Guedes05f9d3e2017-10-16 18:01:28 -070066
67enum queue_mode {
68 QUEUE_MODE_STRICT_PRIORITY,
69 QUEUE_MODE_STREAM_RESERVATION,
70};
71
72enum tx_queue_prio {
73 TX_QUEUE_PRIO_HIGH,
74 TX_QUEUE_PRIO_LOW,
75};
76
Auke Kok9d5c8242008-01-24 02:22:38 -080077char igb_driver_name[] = "igb";
78char igb_driver_version[] = DRV_VERSION;
79static const char igb_driver_string[] =
80 "Intel(R) Gigabit Ethernet Network Driver";
Akeem G. Abodunrin4b9ea462013-01-08 18:31:12 +000081static const char igb_copyright[] =
Carolyn Wyborny74cfb2e2014-02-25 17:58:57 -080082 "Copyright (c) 2007-2014 Intel Corporation.";
Auke Kok9d5c8242008-01-24 02:22:38 -080083
Auke Kok9d5c8242008-01-24 02:22:38 -080084static const struct e1000_info *igb_info_tbl[] = {
85 [board_82575] = &e1000_82575_info,
86};
87
Carolyn Wybornycd1631c2014-04-11 01:47:08 +000088static const struct pci_device_id igb_pci_tbl[] = {
Carolyn Wybornyceb5f132013-04-18 22:21:30 +000089 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I354_BACKPLANE_1GBPS) },
90 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I354_SGMII) },
91 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I354_BACKPLANE_2_5GBPS) },
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +000092 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I211_COPPER), board_82575 },
93 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I210_COPPER), board_82575 },
94 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I210_FIBER), board_82575 },
95 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I210_SERDES), board_82575 },
96 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I210_SGMII), board_82575 },
Carolyn Wyborny53b87ce2013-07-16 19:18:36 +000097 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I210_COPPER_FLASHLESS), board_82575 },
98 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I210_SERDES_FLASHLESS), board_82575 },
Alexander Duyckd2ba2ed2010-03-22 14:08:06 +000099 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I350_COPPER), board_82575 },
100 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I350_FIBER), board_82575 },
101 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I350_SERDES), board_82575 },
102 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I350_SGMII), board_82575 },
Alexander Duyck55cac242009-11-19 12:42:21 +0000103 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_COPPER), board_82575 },
104 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_FIBER), board_82575 },
Carolyn Wyborny6493d242011-01-14 05:33:46 +0000105 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_QUAD_FIBER), board_82575 },
Alexander Duyck55cac242009-11-19 12:42:21 +0000106 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_SERDES), board_82575 },
107 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_SGMII), board_82575 },
108 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_COPPER_DUAL), board_82575 },
Joseph Gasparakis308fb392010-09-22 17:56:44 +0000109 { PCI_VDEVICE(INTEL, E1000_DEV_ID_DH89XXCC_SGMII), board_82575 },
110 { PCI_VDEVICE(INTEL, E1000_DEV_ID_DH89XXCC_SERDES), board_82575 },
Gasparakis, Joseph1b5dda32010-12-09 01:41:01 +0000111 { PCI_VDEVICE(INTEL, E1000_DEV_ID_DH89XXCC_BACKPLANE), board_82575 },
112 { PCI_VDEVICE(INTEL, E1000_DEV_ID_DH89XXCC_SFP), board_82575 },
Alexander Duyck2d064c02008-07-08 15:10:12 -0700113 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576), board_82575 },
Alexander Duyck9eb23412009-03-13 20:42:15 +0000114 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_NS), board_82575 },
Alexander Duyck747d49b2009-10-05 06:33:27 +0000115 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_NS_SERDES), board_82575 },
Alexander Duyck2d064c02008-07-08 15:10:12 -0700116 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_FIBER), board_82575 },
117 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_SERDES), board_82575 },
Alexander Duyck4703bf72009-07-23 18:09:48 +0000118 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_SERDES_QUAD), board_82575 },
Carolyn Wybornyb894fa22010-03-19 06:07:48 +0000119 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_QUAD_COPPER_ET2), board_82575 },
Alexander Duyckc8ea5ea2009-03-13 20:42:35 +0000120 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_QUAD_COPPER), board_82575 },
Auke Kok9d5c8242008-01-24 02:22:38 -0800121 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82575EB_COPPER), board_82575 },
122 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82575EB_FIBER_SERDES), board_82575 },
123 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82575GB_QUAD_COPPER), board_82575 },
124 /* required last entry */
125 {0, }
126};
127
128MODULE_DEVICE_TABLE(pci, igb_pci_tbl);
129
Auke Kok9d5c8242008-01-24 02:22:38 -0800130static int igb_setup_all_tx_resources(struct igb_adapter *);
131static int igb_setup_all_rx_resources(struct igb_adapter *);
132static void igb_free_all_tx_resources(struct igb_adapter *);
133static void igb_free_all_rx_resources(struct igb_adapter *);
Alexander Duyck06cf2662009-10-27 15:53:25 +0000134static void igb_setup_mrqc(struct igb_adapter *);
Auke Kok9d5c8242008-01-24 02:22:38 -0800135static int igb_probe(struct pci_dev *, const struct pci_device_id *);
Bill Pemberton9f9a12f2012-12-03 09:24:25 -0500136static void igb_remove(struct pci_dev *pdev);
Auke Kok9d5c8242008-01-24 02:22:38 -0800137static int igb_sw_init(struct igb_adapter *);
Stefan Assmann46eafa52016-02-03 09:20:50 +0100138int igb_open(struct net_device *);
139int igb_close(struct net_device *);
Stefan Assmann53c7d062012-12-04 06:00:12 +0000140static void igb_configure(struct igb_adapter *);
Auke Kok9d5c8242008-01-24 02:22:38 -0800141static void igb_configure_tx(struct igb_adapter *);
142static void igb_configure_rx(struct igb_adapter *);
Auke Kok9d5c8242008-01-24 02:22:38 -0800143static void igb_clean_all_tx_rings(struct igb_adapter *);
144static void igb_clean_all_rx_rings(struct igb_adapter *);
Mitch Williams3b644cf2008-06-27 10:59:48 -0700145static void igb_clean_tx_ring(struct igb_ring *);
146static void igb_clean_rx_ring(struct igb_ring *);
Alexander Duyckff41f8d2009-09-03 14:48:56 +0000147static void igb_set_rx_mode(struct net_device *);
Kees Cook26566ea2017-10-16 17:29:35 -0700148static void igb_update_phy_info(struct timer_list *);
149static void igb_watchdog(struct timer_list *);
Auke Kok9d5c8242008-01-24 02:22:38 -0800150static void igb_watchdog_task(struct work_struct *);
Alexander Duyckcd392f52011-08-26 07:43:59 +0000151static netdev_tx_t igb_xmit_frame(struct sk_buff *skb, struct net_device *);
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800152static void igb_get_stats64(struct net_device *dev,
153 struct rtnl_link_stats64 *stats);
Auke Kok9d5c8242008-01-24 02:22:38 -0800154static int igb_change_mtu(struct net_device *, int);
155static int igb_set_mac(struct net_device *, void *);
Alexander Duyckbf456ab2016-01-06 23:11:43 -0800156static void igb_set_uta(struct igb_adapter *adapter, bool set);
Auke Kok9d5c8242008-01-24 02:22:38 -0800157static irqreturn_t igb_intr(int irq, void *);
158static irqreturn_t igb_intr_msi(int irq, void *);
159static irqreturn_t igb_msix_other(int irq, void *);
Alexander Duyck047e0032009-10-27 15:49:27 +0000160static irqreturn_t igb_msix_ring(int irq, void *);
Jeff Kirsher421e02f2008-10-17 11:08:31 -0700161#ifdef CONFIG_IGB_DCA
Alexander Duyck047e0032009-10-27 15:49:27 +0000162static void igb_update_dca(struct igb_q_vector *);
Jeb Cramerfe4506b2008-07-08 15:07:55 -0700163static void igb_setup_dca(struct igb_adapter *);
Jeff Kirsher421e02f2008-10-17 11:08:31 -0700164#endif /* CONFIG_IGB_DCA */
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -0700165static int igb_poll(struct napi_struct *, int);
Alexander Duyck7f0ba842016-03-07 09:30:21 -0800166static bool igb_clean_tx_irq(struct igb_q_vector *, int);
Jesse Brandeburg32b3e082015-09-24 16:35:47 -0700167static int igb_clean_rx_irq(struct igb_q_vector *, int);
Auke Kok9d5c8242008-01-24 02:22:38 -0800168static int igb_ioctl(struct net_device *, struct ifreq *, int cmd);
169static void igb_tx_timeout(struct net_device *);
170static void igb_reset_task(struct work_struct *);
Carolyn Wybornyc502ea22014-04-11 01:46:33 +0000171static void igb_vlan_mode(struct net_device *netdev,
172 netdev_features_t features);
Patrick McHardy80d5c362013-04-19 02:04:28 +0000173static int igb_vlan_rx_add_vid(struct net_device *, __be16, u16);
174static int igb_vlan_rx_kill_vid(struct net_device *, __be16, u16);
Auke Kok9d5c8242008-01-24 02:22:38 -0800175static void igb_restore_vlan(struct igb_adapter *);
Yury Kylulin83c21332017-03-07 11:20:25 +0300176static void igb_rar_set_index(struct igb_adapter *, u32);
Alexander Duyck4ae196d2009-02-19 20:40:07 -0800177static void igb_ping_all_vfs(struct igb_adapter *);
178static void igb_msg_task(struct igb_adapter *);
Alexander Duyck4ae196d2009-02-19 20:40:07 -0800179static void igb_vmm_control(struct igb_adapter *);
Alexander Duyckf2ca0db2009-10-27 23:46:57 +0000180static int igb_set_vf_mac(struct igb_adapter *, int, unsigned char *);
Yury Kylulin83c21332017-03-07 11:20:25 +0300181static void igb_flush_mac_table(struct igb_adapter *);
182static int igb_available_rars(struct igb_adapter *, u8);
183static void igb_set_default_mac_filter(struct igb_adapter *);
184static int igb_uc_sync(struct net_device *, const unsigned char *);
185static int igb_uc_unsync(struct net_device *, const unsigned char *);
Alexander Duyck4ae196d2009-02-19 20:40:07 -0800186static void igb_restore_vf_multicasts(struct igb_adapter *adapter);
Williams, Mitch A8151d292010-02-10 01:44:24 +0000187static int igb_ndo_set_vf_mac(struct net_device *netdev, int vf, u8 *mac);
188static int igb_ndo_set_vf_vlan(struct net_device *netdev,
Moshe Shemesh79aab092016-09-22 12:11:15 +0300189 int vf, u16 vlan, u8 qos, __be16 vlan_proto);
Sucheta Chakrabortyed616682014-05-22 09:59:05 -0400190static int igb_ndo_set_vf_bw(struct net_device *, int, int, int);
Lior Levy70ea4782013-03-03 20:27:48 +0000191static int igb_ndo_set_vf_spoofchk(struct net_device *netdev, int vf,
192 bool setting);
Williams, Mitch A8151d292010-02-10 01:44:24 +0000193static int igb_ndo_get_vf_config(struct net_device *netdev, int vf,
194 struct ifla_vf_info *ivi);
Lior Levy17dc5662011-02-08 02:28:46 +0000195static void igb_check_vf_rate_limit(struct igb_adapter *);
Gangfeng Huang0e71def2016-07-06 13:22:54 +0800196static void igb_nfc_filter_exit(struct igb_adapter *adapter);
197static void igb_nfc_filter_restore(struct igb_adapter *adapter);
RongQing Li46a01692011-10-18 22:52:35 +0000198
199#ifdef CONFIG_PCI_IOV
Greg Rose0224d662011-10-14 02:57:14 +0000200static int igb_vf_configure(struct igb_adapter *adapter, int vf);
Stefan Assmann781798a2013-09-24 05:18:39 +0000201static int igb_pci_enable_sriov(struct pci_dev *dev, int num_vfs);
Todd Fujinakaceee3452015-08-07 17:27:39 -0700202static int igb_disable_sriov(struct pci_dev *dev);
203static int igb_pci_disable_sriov(struct pci_dev *dev);
RongQing Li46a01692011-10-18 22:52:35 +0000204#endif
Auke Kok9d5c8242008-01-24 02:22:38 -0800205
Yan, Zheng749ab2c2012-01-04 20:23:37 +0000206static int igb_suspend(struct device *);
207static int igb_resume(struct device *);
Yan, Zheng749ab2c2012-01-04 20:23:37 +0000208static int igb_runtime_suspend(struct device *dev);
209static int igb_runtime_resume(struct device *dev);
210static int igb_runtime_idle(struct device *dev);
Yan, Zheng749ab2c2012-01-04 20:23:37 +0000211static const struct dev_pm_ops igb_pm_ops = {
212 SET_SYSTEM_SLEEP_PM_OPS(igb_suspend, igb_resume)
213 SET_RUNTIME_PM_OPS(igb_runtime_suspend, igb_runtime_resume,
214 igb_runtime_idle)
215};
Auke Kok9d5c8242008-01-24 02:22:38 -0800216static void igb_shutdown(struct pci_dev *);
Greg Rosefa44f2f2013-01-17 01:03:06 -0800217static int igb_pci_sriov_configure(struct pci_dev *dev, int num_vfs);
Jeff Kirsher421e02f2008-10-17 11:08:31 -0700218#ifdef CONFIG_IGB_DCA
Jeb Cramerfe4506b2008-07-08 15:07:55 -0700219static int igb_notify_dca(struct notifier_block *, unsigned long, void *);
220static struct notifier_block dca_notifier = {
221 .notifier_call = igb_notify_dca,
222 .next = NULL,
223 .priority = 0
224};
225#endif
Auke Kok9d5c8242008-01-24 02:22:38 -0800226#ifdef CONFIG_NET_POLL_CONTROLLER
227/* for netdump / net console */
228static void igb_netpoll(struct net_device *);
229#endif
Alexander Duyck37680112009-02-19 20:40:30 -0800230#ifdef CONFIG_PCI_IOV
Carolyn Wyborny6dd6d2b2014-04-11 01:46:48 +0000231static unsigned int max_vfs;
Alexander Duyck2a3abf62009-04-07 14:37:52 +0000232module_param(max_vfs, uint, 0);
Carolyn Wybornyc75c4ed2014-04-11 01:45:17 +0000233MODULE_PARM_DESC(max_vfs, "Maximum number of virtual functions to allocate per physical function");
Alexander Duyck2a3abf62009-04-07 14:37:52 +0000234#endif /* CONFIG_PCI_IOV */
235
Auke Kok9d5c8242008-01-24 02:22:38 -0800236static pci_ers_result_t igb_io_error_detected(struct pci_dev *,
237 pci_channel_state_t);
238static pci_ers_result_t igb_io_slot_reset(struct pci_dev *);
239static void igb_io_resume(struct pci_dev *);
240
Stephen Hemminger3646f0e2012-09-07 09:33:15 -0700241static const struct pci_error_handlers igb_err_handler = {
Auke Kok9d5c8242008-01-24 02:22:38 -0800242 .error_detected = igb_io_error_detected,
243 .slot_reset = igb_io_slot_reset,
244 .resume = igb_io_resume,
245};
246
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +0000247static void igb_init_dmac(struct igb_adapter *adapter, u32 pba);
Auke Kok9d5c8242008-01-24 02:22:38 -0800248
249static struct pci_driver igb_driver = {
250 .name = igb_driver_name,
251 .id_table = igb_pci_tbl,
252 .probe = igb_probe,
Bill Pemberton9f9a12f2012-12-03 09:24:25 -0500253 .remove = igb_remove,
Auke Kok9d5c8242008-01-24 02:22:38 -0800254#ifdef CONFIG_PM
Yan, Zheng749ab2c2012-01-04 20:23:37 +0000255 .driver.pm = &igb_pm_ops,
Auke Kok9d5c8242008-01-24 02:22:38 -0800256#endif
257 .shutdown = igb_shutdown,
Greg Rosefa44f2f2013-01-17 01:03:06 -0800258 .sriov_configure = igb_pci_sriov_configure,
Auke Kok9d5c8242008-01-24 02:22:38 -0800259 .err_handler = &igb_err_handler
260};
261
262MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
263MODULE_DESCRIPTION("Intel(R) Gigabit Ethernet Network Driver");
264MODULE_LICENSE("GPL");
265MODULE_VERSION(DRV_VERSION);
266
stephen hemmingerb3f4d592012-03-13 06:04:20 +0000267#define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK)
268static int debug = -1;
269module_param(debug, int, 0);
270MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
271
Taku Izumic97ec422010-04-27 14:39:30 +0000272struct igb_reg_info {
273 u32 ofs;
274 char *name;
275};
276
277static const struct igb_reg_info igb_reg_info_tbl[] = {
278
279 /* General Registers */
280 {E1000_CTRL, "CTRL"},
281 {E1000_STATUS, "STATUS"},
282 {E1000_CTRL_EXT, "CTRL_EXT"},
283
284 /* Interrupt Registers */
285 {E1000_ICR, "ICR"},
286
287 /* RX Registers */
288 {E1000_RCTL, "RCTL"},
289 {E1000_RDLEN(0), "RDLEN"},
290 {E1000_RDH(0), "RDH"},
291 {E1000_RDT(0), "RDT"},
292 {E1000_RXDCTL(0), "RXDCTL"},
293 {E1000_RDBAL(0), "RDBAL"},
294 {E1000_RDBAH(0), "RDBAH"},
295
296 /* TX Registers */
297 {E1000_TCTL, "TCTL"},
298 {E1000_TDBAL(0), "TDBAL"},
299 {E1000_TDBAH(0), "TDBAH"},
300 {E1000_TDLEN(0), "TDLEN"},
301 {E1000_TDH(0), "TDH"},
302 {E1000_TDT(0), "TDT"},
303 {E1000_TXDCTL(0), "TXDCTL"},
304 {E1000_TDFH, "TDFH"},
305 {E1000_TDFT, "TDFT"},
306 {E1000_TDFHS, "TDFHS"},
307 {E1000_TDFPC, "TDFPC"},
308
309 /* List Terminator */
310 {}
311};
312
Jeff Kirsherb980ac12013-02-23 07:29:56 +0000313/* igb_regdump - register printout routine */
Taku Izumic97ec422010-04-27 14:39:30 +0000314static void igb_regdump(struct e1000_hw *hw, struct igb_reg_info *reginfo)
315{
316 int n = 0;
317 char rname[16];
318 u32 regs[8];
319
320 switch (reginfo->ofs) {
321 case E1000_RDLEN(0):
322 for (n = 0; n < 4; n++)
323 regs[n] = rd32(E1000_RDLEN(n));
324 break;
325 case E1000_RDH(0):
326 for (n = 0; n < 4; n++)
327 regs[n] = rd32(E1000_RDH(n));
328 break;
329 case E1000_RDT(0):
330 for (n = 0; n < 4; n++)
331 regs[n] = rd32(E1000_RDT(n));
332 break;
333 case E1000_RXDCTL(0):
334 for (n = 0; n < 4; n++)
335 regs[n] = rd32(E1000_RXDCTL(n));
336 break;
337 case E1000_RDBAL(0):
338 for (n = 0; n < 4; n++)
339 regs[n] = rd32(E1000_RDBAL(n));
340 break;
341 case E1000_RDBAH(0):
342 for (n = 0; n < 4; n++)
343 regs[n] = rd32(E1000_RDBAH(n));
344 break;
345 case E1000_TDBAL(0):
346 for (n = 0; n < 4; n++)
347 regs[n] = rd32(E1000_RDBAL(n));
348 break;
349 case E1000_TDBAH(0):
350 for (n = 0; n < 4; n++)
351 regs[n] = rd32(E1000_TDBAH(n));
352 break;
353 case E1000_TDLEN(0):
354 for (n = 0; n < 4; n++)
355 regs[n] = rd32(E1000_TDLEN(n));
356 break;
357 case E1000_TDH(0):
358 for (n = 0; n < 4; n++)
359 regs[n] = rd32(E1000_TDH(n));
360 break;
361 case E1000_TDT(0):
362 for (n = 0; n < 4; n++)
363 regs[n] = rd32(E1000_TDT(n));
364 break;
365 case E1000_TXDCTL(0):
366 for (n = 0; n < 4; n++)
367 regs[n] = rd32(E1000_TXDCTL(n));
368 break;
369 default:
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000370 pr_info("%-15s %08x\n", reginfo->name, rd32(reginfo->ofs));
Taku Izumic97ec422010-04-27 14:39:30 +0000371 return;
372 }
373
374 snprintf(rname, 16, "%s%s", reginfo->name, "[0-3]");
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000375 pr_info("%-15s %08x %08x %08x %08x\n", rname, regs[0], regs[1],
376 regs[2], regs[3]);
Taku Izumic97ec422010-04-27 14:39:30 +0000377}
378
Jeff Kirsherb980ac12013-02-23 07:29:56 +0000379/* igb_dump - Print registers, Tx-rings and Rx-rings */
Taku Izumic97ec422010-04-27 14:39:30 +0000380static void igb_dump(struct igb_adapter *adapter)
381{
382 struct net_device *netdev = adapter->netdev;
383 struct e1000_hw *hw = &adapter->hw;
384 struct igb_reg_info *reginfo;
Taku Izumic97ec422010-04-27 14:39:30 +0000385 struct igb_ring *tx_ring;
386 union e1000_adv_tx_desc *tx_desc;
387 struct my_u0 { u64 a; u64 b; } *u0;
Taku Izumic97ec422010-04-27 14:39:30 +0000388 struct igb_ring *rx_ring;
389 union e1000_adv_rx_desc *rx_desc;
390 u32 staterr;
Alexander Duyck6ad4edf2011-08-26 07:45:26 +0000391 u16 i, n;
Taku Izumic97ec422010-04-27 14:39:30 +0000392
393 if (!netif_msg_hw(adapter))
394 return;
395
396 /* Print netdevice Info */
397 if (netdev) {
398 dev_info(&adapter->pdev->dev, "Net device Info\n");
Tobias Klauser4a7c9722017-01-18 17:45:01 +0100399 pr_info("Device Name state trans_start\n");
400 pr_info("%-15s %016lX %016lX\n", netdev->name,
401 netdev->state, dev_trans_start(netdev));
Taku Izumic97ec422010-04-27 14:39:30 +0000402 }
403
404 /* Print Registers */
405 dev_info(&adapter->pdev->dev, "Register Dump\n");
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000406 pr_info(" Register Name Value\n");
Taku Izumic97ec422010-04-27 14:39:30 +0000407 for (reginfo = (struct igb_reg_info *)igb_reg_info_tbl;
408 reginfo->name; reginfo++) {
409 igb_regdump(hw, reginfo);
410 }
411
412 /* Print TX Ring Summary */
413 if (!netdev || !netif_running(netdev))
414 goto exit;
415
416 dev_info(&adapter->pdev->dev, "TX Rings Summary\n");
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000417 pr_info("Queue [NTU] [NTC] [bi(ntc)->dma ] leng ntw timestamp\n");
Taku Izumic97ec422010-04-27 14:39:30 +0000418 for (n = 0; n < adapter->num_tx_queues; n++) {
Alexander Duyck06034642011-08-26 07:44:22 +0000419 struct igb_tx_buffer *buffer_info;
Taku Izumic97ec422010-04-27 14:39:30 +0000420 tx_ring = adapter->tx_ring[n];
Alexander Duyck06034642011-08-26 07:44:22 +0000421 buffer_info = &tx_ring->tx_buffer_info[tx_ring->next_to_clean];
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000422 pr_info(" %5d %5X %5X %016llX %04X %p %016llX\n",
423 n, tx_ring->next_to_use, tx_ring->next_to_clean,
Alexander Duyckc9f14bf32012-09-18 01:56:27 +0000424 (u64)dma_unmap_addr(buffer_info, dma),
425 dma_unmap_len(buffer_info, len),
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000426 buffer_info->next_to_watch,
427 (u64)buffer_info->time_stamp);
Taku Izumic97ec422010-04-27 14:39:30 +0000428 }
429
430 /* Print TX Rings */
431 if (!netif_msg_tx_done(adapter))
432 goto rx_ring_summary;
433
434 dev_info(&adapter->pdev->dev, "TX Rings Dump\n");
435
436 /* Transmit Descriptor Formats
437 *
438 * Advanced Transmit Descriptor
439 * +--------------------------------------------------------------+
440 * 0 | Buffer Address [63:0] |
441 * +--------------------------------------------------------------+
442 * 8 | PAYLEN | PORTS |CC|IDX | STA | DCMD |DTYP|MAC|RSV| DTALEN |
443 * +--------------------------------------------------------------+
444 * 63 46 45 40 39 38 36 35 32 31 24 15 0
445 */
446
447 for (n = 0; n < adapter->num_tx_queues; n++) {
448 tx_ring = adapter->tx_ring[n];
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000449 pr_info("------------------------------------\n");
450 pr_info("TX QUEUE INDEX = %d\n", tx_ring->queue_index);
451 pr_info("------------------------------------\n");
Carolyn Wybornyc75c4ed2014-04-11 01:45:17 +0000452 pr_info("T [desc] [address 63:0 ] [PlPOCIStDDM Ln] [bi->dma ] leng ntw timestamp bi->skb\n");
Taku Izumic97ec422010-04-27 14:39:30 +0000453
454 for (i = 0; tx_ring->desc && (i < tx_ring->count); i++) {
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000455 const char *next_desc;
Alexander Duyck06034642011-08-26 07:44:22 +0000456 struct igb_tx_buffer *buffer_info;
Alexander Duyck601369062011-08-26 07:44:05 +0000457 tx_desc = IGB_TX_DESC(tx_ring, i);
Alexander Duyck06034642011-08-26 07:44:22 +0000458 buffer_info = &tx_ring->tx_buffer_info[i];
Taku Izumic97ec422010-04-27 14:39:30 +0000459 u0 = (struct my_u0 *)tx_desc;
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000460 if (i == tx_ring->next_to_use &&
461 i == tx_ring->next_to_clean)
462 next_desc = " NTC/U";
463 else if (i == tx_ring->next_to_use)
464 next_desc = " NTU";
465 else if (i == tx_ring->next_to_clean)
466 next_desc = " NTC";
467 else
468 next_desc = "";
469
Carolyn Wybornyc75c4ed2014-04-11 01:45:17 +0000470 pr_info("T [0x%03X] %016llX %016llX %016llX %04X %p %016llX %p%s\n",
471 i, le64_to_cpu(u0->a),
Taku Izumic97ec422010-04-27 14:39:30 +0000472 le64_to_cpu(u0->b),
Alexander Duyckc9f14bf32012-09-18 01:56:27 +0000473 (u64)dma_unmap_addr(buffer_info, dma),
474 dma_unmap_len(buffer_info, len),
Taku Izumic97ec422010-04-27 14:39:30 +0000475 buffer_info->next_to_watch,
476 (u64)buffer_info->time_stamp,
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000477 buffer_info->skb, next_desc);
Taku Izumic97ec422010-04-27 14:39:30 +0000478
Emil Tantilovb6695882012-07-28 05:07:48 +0000479 if (netif_msg_pktdata(adapter) && buffer_info->skb)
Taku Izumic97ec422010-04-27 14:39:30 +0000480 print_hex_dump(KERN_INFO, "",
481 DUMP_PREFIX_ADDRESS,
Emil Tantilovb6695882012-07-28 05:07:48 +0000482 16, 1, buffer_info->skb->data,
Alexander Duyckc9f14bf32012-09-18 01:56:27 +0000483 dma_unmap_len(buffer_info, len),
484 true);
Taku Izumic97ec422010-04-27 14:39:30 +0000485 }
486 }
487
488 /* Print RX Rings Summary */
489rx_ring_summary:
490 dev_info(&adapter->pdev->dev, "RX Rings Summary\n");
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000491 pr_info("Queue [NTU] [NTC]\n");
Taku Izumic97ec422010-04-27 14:39:30 +0000492 for (n = 0; n < adapter->num_rx_queues; n++) {
493 rx_ring = adapter->rx_ring[n];
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000494 pr_info(" %5d %5X %5X\n",
495 n, rx_ring->next_to_use, rx_ring->next_to_clean);
Taku Izumic97ec422010-04-27 14:39:30 +0000496 }
497
498 /* Print RX Rings */
499 if (!netif_msg_rx_status(adapter))
500 goto exit;
501
502 dev_info(&adapter->pdev->dev, "RX Rings Dump\n");
503
504 /* Advanced Receive Descriptor (Read) Format
505 * 63 1 0
506 * +-----------------------------------------------------+
507 * 0 | Packet Buffer Address [63:1] |A0/NSE|
508 * +----------------------------------------------+------+
509 * 8 | Header Buffer Address [63:1] | DD |
510 * +-----------------------------------------------------+
511 *
512 *
513 * Advanced Receive Descriptor (Write-Back) Format
514 *
515 * 63 48 47 32 31 30 21 20 17 16 4 3 0
516 * +------------------------------------------------------+
517 * 0 | Packet IP |SPH| HDR_LEN | RSV|Packet| RSS |
518 * | Checksum Ident | | | | Type | Type |
519 * +------------------------------------------------------+
520 * 8 | VLAN Tag | Length | Extended Error | Extended Status |
521 * +------------------------------------------------------+
522 * 63 48 47 32 31 20 19 0
523 */
524
525 for (n = 0; n < adapter->num_rx_queues; n++) {
526 rx_ring = adapter->rx_ring[n];
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000527 pr_info("------------------------------------\n");
528 pr_info("RX QUEUE INDEX = %d\n", rx_ring->queue_index);
529 pr_info("------------------------------------\n");
Carolyn Wybornyc75c4ed2014-04-11 01:45:17 +0000530 pr_info("R [desc] [ PktBuf A0] [ HeadBuf DD] [bi->dma ] [bi->skb] <-- Adv Rx Read format\n");
531 pr_info("RWB[desc] [PcsmIpSHl PtRs] [vl er S cks ln] ---------------- [bi->skb] <-- Adv Rx Write-Back format\n");
Taku Izumic97ec422010-04-27 14:39:30 +0000532
533 for (i = 0; i < rx_ring->count; i++) {
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000534 const char *next_desc;
Alexander Duyck06034642011-08-26 07:44:22 +0000535 struct igb_rx_buffer *buffer_info;
536 buffer_info = &rx_ring->rx_buffer_info[i];
Alexander Duyck601369062011-08-26 07:44:05 +0000537 rx_desc = IGB_RX_DESC(rx_ring, i);
Taku Izumic97ec422010-04-27 14:39:30 +0000538 u0 = (struct my_u0 *)rx_desc;
539 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000540
541 if (i == rx_ring->next_to_use)
542 next_desc = " NTU";
543 else if (i == rx_ring->next_to_clean)
544 next_desc = " NTC";
545 else
546 next_desc = "";
547
Taku Izumic97ec422010-04-27 14:39:30 +0000548 if (staterr & E1000_RXD_STAT_DD) {
549 /* Descriptor Done */
Alexander Duyck1a1c2252012-09-25 00:30:52 +0000550 pr_info("%s[0x%03X] %016llX %016llX ---------------- %s\n",
551 "RWB", i,
Taku Izumic97ec422010-04-27 14:39:30 +0000552 le64_to_cpu(u0->a),
553 le64_to_cpu(u0->b),
Alexander Duyck1a1c2252012-09-25 00:30:52 +0000554 next_desc);
Taku Izumic97ec422010-04-27 14:39:30 +0000555 } else {
Alexander Duyck1a1c2252012-09-25 00:30:52 +0000556 pr_info("%s[0x%03X] %016llX %016llX %016llX %s\n",
557 "R ", i,
Taku Izumic97ec422010-04-27 14:39:30 +0000558 le64_to_cpu(u0->a),
559 le64_to_cpu(u0->b),
560 (u64)buffer_info->dma,
Alexander Duyck1a1c2252012-09-25 00:30:52 +0000561 next_desc);
Taku Izumic97ec422010-04-27 14:39:30 +0000562
Emil Tantilovb6695882012-07-28 05:07:48 +0000563 if (netif_msg_pktdata(adapter) &&
Alexander Duyck1a1c2252012-09-25 00:30:52 +0000564 buffer_info->dma && buffer_info->page) {
Alexander Duyck44390ca2011-08-26 07:43:38 +0000565 print_hex_dump(KERN_INFO, "",
566 DUMP_PREFIX_ADDRESS,
567 16, 1,
Emil Tantilovb6695882012-07-28 05:07:48 +0000568 page_address(buffer_info->page) +
569 buffer_info->page_offset,
Alexander Duyck8649aae2017-02-06 18:27:03 -0800570 igb_rx_bufsz(rx_ring), true);
Taku Izumic97ec422010-04-27 14:39:30 +0000571 }
572 }
Taku Izumic97ec422010-04-27 14:39:30 +0000573 }
574 }
575
576exit:
577 return;
578}
579
Jeff Kirsherb980ac12013-02-23 07:29:56 +0000580/**
581 * igb_get_i2c_data - Reads the I2C SDA data bit
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +0000582 * @hw: pointer to hardware structure
583 * @i2cctl: Current value of I2CCTL register
584 *
585 * Returns the I2C data bit value
Jeff Kirsherb980ac12013-02-23 07:29:56 +0000586 **/
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +0000587static int igb_get_i2c_data(void *data)
588{
589 struct igb_adapter *adapter = (struct igb_adapter *)data;
590 struct e1000_hw *hw = &adapter->hw;
591 s32 i2cctl = rd32(E1000_I2CPARAMS);
592
Carolyn Wybornyda1f1df2014-04-11 02:11:17 +0000593 return !!(i2cctl & E1000_I2C_DATA_IN);
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +0000594}
595
Jeff Kirsherb980ac12013-02-23 07:29:56 +0000596/**
597 * igb_set_i2c_data - Sets the I2C data bit
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +0000598 * @data: pointer to hardware structure
599 * @state: I2C data value (0 or 1) to set
600 *
601 * Sets the I2C data bit
Jeff Kirsherb980ac12013-02-23 07:29:56 +0000602 **/
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +0000603static void igb_set_i2c_data(void *data, int state)
604{
605 struct igb_adapter *adapter = (struct igb_adapter *)data;
606 struct e1000_hw *hw = &adapter->hw;
607 s32 i2cctl = rd32(E1000_I2CPARAMS);
608
609 if (state)
610 i2cctl |= E1000_I2C_DATA_OUT;
611 else
612 i2cctl &= ~E1000_I2C_DATA_OUT;
613
614 i2cctl &= ~E1000_I2C_DATA_OE_N;
615 i2cctl |= E1000_I2C_CLK_OE_N;
616 wr32(E1000_I2CPARAMS, i2cctl);
617 wrfl();
618
619}
620
Jeff Kirsherb980ac12013-02-23 07:29:56 +0000621/**
622 * igb_set_i2c_clk - Sets the I2C SCL clock
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +0000623 * @data: pointer to hardware structure
624 * @state: state to set clock
625 *
626 * Sets the I2C clock line to state
Jeff Kirsherb980ac12013-02-23 07:29:56 +0000627 **/
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +0000628static void igb_set_i2c_clk(void *data, int state)
629{
630 struct igb_adapter *adapter = (struct igb_adapter *)data;
631 struct e1000_hw *hw = &adapter->hw;
632 s32 i2cctl = rd32(E1000_I2CPARAMS);
633
634 if (state) {
635 i2cctl |= E1000_I2C_CLK_OUT;
636 i2cctl &= ~E1000_I2C_CLK_OE_N;
637 } else {
638 i2cctl &= ~E1000_I2C_CLK_OUT;
639 i2cctl &= ~E1000_I2C_CLK_OE_N;
640 }
641 wr32(E1000_I2CPARAMS, i2cctl);
642 wrfl();
643}
644
Jeff Kirsherb980ac12013-02-23 07:29:56 +0000645/**
646 * igb_get_i2c_clk - Gets the I2C SCL clock state
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +0000647 * @data: pointer to hardware structure
648 *
649 * Gets the I2C clock state
Jeff Kirsherb980ac12013-02-23 07:29:56 +0000650 **/
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +0000651static int igb_get_i2c_clk(void *data)
652{
653 struct igb_adapter *adapter = (struct igb_adapter *)data;
654 struct e1000_hw *hw = &adapter->hw;
655 s32 i2cctl = rd32(E1000_I2CPARAMS);
656
Carolyn Wybornyda1f1df2014-04-11 02:11:17 +0000657 return !!(i2cctl & E1000_I2C_CLK_IN);
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +0000658}
659
660static const struct i2c_algo_bit_data igb_i2c_algo = {
661 .setsda = igb_set_i2c_data,
662 .setscl = igb_set_i2c_clk,
663 .getsda = igb_get_i2c_data,
664 .getscl = igb_get_i2c_clk,
665 .udelay = 5,
666 .timeout = 20,
667};
668
Auke Kok9d5c8242008-01-24 02:22:38 -0800669/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +0000670 * igb_get_hw_dev - return device
671 * @hw: pointer to hardware structure
672 *
673 * used by hardware layer to print debugging information
Auke Kok9d5c8242008-01-24 02:22:38 -0800674 **/
Alexander Duyckc0410762010-03-25 13:10:08 +0000675struct net_device *igb_get_hw_dev(struct e1000_hw *hw)
Auke Kok9d5c8242008-01-24 02:22:38 -0800676{
677 struct igb_adapter *adapter = hw->back;
Alexander Duyckc0410762010-03-25 13:10:08 +0000678 return adapter->netdev;
Auke Kok9d5c8242008-01-24 02:22:38 -0800679}
Patrick Ohly38c845c2009-02-12 05:03:41 +0000680
681/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +0000682 * igb_init_module - Driver Registration Routine
Auke Kok9d5c8242008-01-24 02:22:38 -0800683 *
Jeff Kirsherb980ac12013-02-23 07:29:56 +0000684 * igb_init_module is the first routine called when the driver is
685 * loaded. All it does is register with the PCI subsystem.
Auke Kok9d5c8242008-01-24 02:22:38 -0800686 **/
687static int __init igb_init_module(void)
688{
689 int ret;
Carolyn Wyborny9005df32014-04-11 01:45:34 +0000690
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000691 pr_info("%s - version %s\n",
Auke Kok9d5c8242008-01-24 02:22:38 -0800692 igb_driver_string, igb_driver_version);
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000693 pr_info("%s\n", igb_copyright);
Auke Kok9d5c8242008-01-24 02:22:38 -0800694
Jeff Kirsher421e02f2008-10-17 11:08:31 -0700695#ifdef CONFIG_IGB_DCA
Jeb Cramerfe4506b2008-07-08 15:07:55 -0700696 dca_register_notify(&dca_notifier);
697#endif
Alexander Duyckbbd98fe2009-01-31 00:52:30 -0800698 ret = pci_register_driver(&igb_driver);
Auke Kok9d5c8242008-01-24 02:22:38 -0800699 return ret;
700}
701
702module_init(igb_init_module);
703
704/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +0000705 * igb_exit_module - Driver Exit Cleanup Routine
Auke Kok9d5c8242008-01-24 02:22:38 -0800706 *
Jeff Kirsherb980ac12013-02-23 07:29:56 +0000707 * igb_exit_module is called just before the driver is removed
708 * from memory.
Auke Kok9d5c8242008-01-24 02:22:38 -0800709 **/
710static void __exit igb_exit_module(void)
711{
Jeff Kirsher421e02f2008-10-17 11:08:31 -0700712#ifdef CONFIG_IGB_DCA
Jeb Cramerfe4506b2008-07-08 15:07:55 -0700713 dca_unregister_notify(&dca_notifier);
714#endif
Auke Kok9d5c8242008-01-24 02:22:38 -0800715 pci_unregister_driver(&igb_driver);
716}
717
718module_exit(igb_exit_module);
719
Alexander Duyck26bc19e2008-12-26 01:34:11 -0800720#define Q_IDX_82576(i) (((i & 0x1) << 3) + (i >> 1))
721/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +0000722 * igb_cache_ring_register - Descriptor ring to register mapping
723 * @adapter: board private structure to initialize
Alexander Duyck26bc19e2008-12-26 01:34:11 -0800724 *
Jeff Kirsherb980ac12013-02-23 07:29:56 +0000725 * Once we know the feature-set enabled for the device, we'll cache
726 * the register offset the descriptor ring is assigned to.
Alexander Duyck26bc19e2008-12-26 01:34:11 -0800727 **/
728static void igb_cache_ring_register(struct igb_adapter *adapter)
729{
Alexander Duyckee1b9f02009-10-27 23:49:40 +0000730 int i = 0, j = 0;
Alexander Duyck047e0032009-10-27 15:49:27 +0000731 u32 rbase_offset = adapter->vfs_allocated_count;
Alexander Duyck26bc19e2008-12-26 01:34:11 -0800732
733 switch (adapter->hw.mac.type) {
734 case e1000_82576:
735 /* The queues are allocated for virtualization such that VF 0
736 * is allocated queues 0 and 8, VF 1 queues 1 and 9, etc.
737 * In order to avoid collision we start at the first free queue
738 * and continue consuming queues in the same sequence
739 */
Alexander Duyckee1b9f02009-10-27 23:49:40 +0000740 if (adapter->vfs_allocated_count) {
Alexander Duycka99955f2009-11-12 18:37:19 +0000741 for (; i < adapter->rss_queues; i++)
Alexander Duyck3025a442010-02-17 01:02:39 +0000742 adapter->rx_ring[i]->reg_idx = rbase_offset +
Jeff Kirsherb980ac12013-02-23 07:29:56 +0000743 Q_IDX_82576(i);
Alexander Duyckee1b9f02009-10-27 23:49:40 +0000744 }
Carolyn Wybornyb26141d2014-04-17 04:10:13 +0000745 /* Fall through */
Alexander Duyck26bc19e2008-12-26 01:34:11 -0800746 case e1000_82575:
Alexander Duyck55cac242009-11-19 12:42:21 +0000747 case e1000_82580:
Alexander Duyckd2ba2ed2010-03-22 14:08:06 +0000748 case e1000_i350:
Carolyn Wybornyceb5f132013-04-18 22:21:30 +0000749 case e1000_i354:
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +0000750 case e1000_i210:
751 case e1000_i211:
Carolyn Wybornyb26141d2014-04-17 04:10:13 +0000752 /* Fall through */
Alexander Duyck26bc19e2008-12-26 01:34:11 -0800753 default:
Alexander Duyckee1b9f02009-10-27 23:49:40 +0000754 for (; i < adapter->num_rx_queues; i++)
Alexander Duyck3025a442010-02-17 01:02:39 +0000755 adapter->rx_ring[i]->reg_idx = rbase_offset + i;
Alexander Duyckee1b9f02009-10-27 23:49:40 +0000756 for (; j < adapter->num_tx_queues; j++)
Alexander Duyck3025a442010-02-17 01:02:39 +0000757 adapter->tx_ring[j]->reg_idx = rbase_offset + j;
Alexander Duyck26bc19e2008-12-26 01:34:11 -0800758 break;
759 }
760}
761
Fujinaka, Todd22a8b292014-03-13 04:29:01 +0000762u32 igb_rd32(struct e1000_hw *hw, u32 reg)
763{
764 struct igb_adapter *igb = container_of(hw, struct igb_adapter, hw);
Mark Rutland6aa7de02017-10-23 14:07:29 -0700765 u8 __iomem *hw_addr = READ_ONCE(hw->hw_addr);
Fujinaka, Todd22a8b292014-03-13 04:29:01 +0000766 u32 value = 0;
767
768 if (E1000_REMOVED(hw_addr))
769 return ~value;
770
771 value = readl(&hw_addr[reg]);
772
773 /* reads should not return all F's */
774 if (!(~value) && (!reg || !(~readl(hw_addr)))) {
775 struct net_device *netdev = igb->netdev;
776 hw->hw_addr = NULL;
777 netif_device_detach(netdev);
778 netdev_err(netdev, "PCIe link lost, device now detached\n");
779 }
780
781 return value;
782}
783
Alexander Duyck4be000c2011-08-26 07:45:52 +0000784/**
785 * igb_write_ivar - configure ivar for given MSI-X vector
786 * @hw: pointer to the HW structure
787 * @msix_vector: vector number we are allocating to a given ring
788 * @index: row index of IVAR register to write within IVAR table
789 * @offset: column offset of in IVAR, should be multiple of 8
790 *
791 * This function is intended to handle the writing of the IVAR register
792 * for adapters 82576 and newer. The IVAR table consists of 2 columns,
793 * each containing an cause allocation for an Rx and Tx ring, and a
794 * variable number of rows depending on the number of queues supported.
795 **/
796static void igb_write_ivar(struct e1000_hw *hw, int msix_vector,
797 int index, int offset)
798{
799 u32 ivar = array_rd32(E1000_IVAR0, index);
800
801 /* clear any bits that are currently set */
802 ivar &= ~((u32)0xFF << offset);
803
804 /* write vector and valid bit */
805 ivar |= (msix_vector | E1000_IVAR_VALID) << offset;
806
807 array_wr32(E1000_IVAR0, index, ivar);
808}
809
Auke Kok9d5c8242008-01-24 02:22:38 -0800810#define IGB_N0_QUEUE -1
Alexander Duyck047e0032009-10-27 15:49:27 +0000811static void igb_assign_vector(struct igb_q_vector *q_vector, int msix_vector)
Auke Kok9d5c8242008-01-24 02:22:38 -0800812{
Alexander Duyck047e0032009-10-27 15:49:27 +0000813 struct igb_adapter *adapter = q_vector->adapter;
Auke Kok9d5c8242008-01-24 02:22:38 -0800814 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck047e0032009-10-27 15:49:27 +0000815 int rx_queue = IGB_N0_QUEUE;
816 int tx_queue = IGB_N0_QUEUE;
Alexander Duyck4be000c2011-08-26 07:45:52 +0000817 u32 msixbm = 0;
Alexander Duyck047e0032009-10-27 15:49:27 +0000818
Alexander Duyck0ba82992011-08-26 07:45:47 +0000819 if (q_vector->rx.ring)
820 rx_queue = q_vector->rx.ring->reg_idx;
821 if (q_vector->tx.ring)
822 tx_queue = q_vector->tx.ring->reg_idx;
Alexander Duyck2d064c02008-07-08 15:10:12 -0700823
824 switch (hw->mac.type) {
825 case e1000_82575:
Auke Kok9d5c8242008-01-24 02:22:38 -0800826 /* The 82575 assigns vectors using a bitmask, which matches the
Jeff Kirsherb980ac12013-02-23 07:29:56 +0000827 * bitmask for the EICR/EIMS/EIMC registers. To assign one
828 * or more queues to a vector, we write the appropriate bits
829 * into the MSIXBM register for that vector.
830 */
Alexander Duyck047e0032009-10-27 15:49:27 +0000831 if (rx_queue > IGB_N0_QUEUE)
Auke Kok9d5c8242008-01-24 02:22:38 -0800832 msixbm = E1000_EICR_RX_QUEUE0 << rx_queue;
Alexander Duyck047e0032009-10-27 15:49:27 +0000833 if (tx_queue > IGB_N0_QUEUE)
Auke Kok9d5c8242008-01-24 02:22:38 -0800834 msixbm |= E1000_EICR_TX_QUEUE0 << tx_queue;
Carolyn Wybornycd14ef52013-12-10 07:58:34 +0000835 if (!(adapter->flags & IGB_FLAG_HAS_MSIX) && msix_vector == 0)
Alexander Duyckfeeb2722010-02-03 21:59:51 +0000836 msixbm |= E1000_EIMS_OTHER;
Auke Kok9d5c8242008-01-24 02:22:38 -0800837 array_wr32(E1000_MSIXBM(0), msix_vector, msixbm);
Alexander Duyck047e0032009-10-27 15:49:27 +0000838 q_vector->eims_value = msixbm;
Alexander Duyck2d064c02008-07-08 15:10:12 -0700839 break;
840 case e1000_82576:
Jeff Kirsherb980ac12013-02-23 07:29:56 +0000841 /* 82576 uses a table that essentially consists of 2 columns
Alexander Duyck4be000c2011-08-26 07:45:52 +0000842 * with 8 rows. The ordering is column-major so we use the
843 * lower 3 bits as the row index, and the 4th bit as the
844 * column offset.
845 */
846 if (rx_queue > IGB_N0_QUEUE)
847 igb_write_ivar(hw, msix_vector,
848 rx_queue & 0x7,
849 (rx_queue & 0x8) << 1);
850 if (tx_queue > IGB_N0_QUEUE)
851 igb_write_ivar(hw, msix_vector,
852 tx_queue & 0x7,
853 ((tx_queue & 0x8) << 1) + 8);
Jacob Kellera51d8c22016-04-13 16:08:28 -0700854 q_vector->eims_value = BIT(msix_vector);
Alexander Duyck2d064c02008-07-08 15:10:12 -0700855 break;
Alexander Duyck55cac242009-11-19 12:42:21 +0000856 case e1000_82580:
Alexander Duyckd2ba2ed2010-03-22 14:08:06 +0000857 case e1000_i350:
Carolyn Wybornyceb5f132013-04-18 22:21:30 +0000858 case e1000_i354:
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +0000859 case e1000_i210:
860 case e1000_i211:
Jeff Kirsherb980ac12013-02-23 07:29:56 +0000861 /* On 82580 and newer adapters the scheme is similar to 82576
Alexander Duyck4be000c2011-08-26 07:45:52 +0000862 * however instead of ordering column-major we have things
863 * ordered row-major. So we traverse the table by using
864 * bit 0 as the column offset, and the remaining bits as the
865 * row index.
866 */
867 if (rx_queue > IGB_N0_QUEUE)
868 igb_write_ivar(hw, msix_vector,
869 rx_queue >> 1,
870 (rx_queue & 0x1) << 4);
871 if (tx_queue > IGB_N0_QUEUE)
872 igb_write_ivar(hw, msix_vector,
873 tx_queue >> 1,
874 ((tx_queue & 0x1) << 4) + 8);
Jacob Kellera51d8c22016-04-13 16:08:28 -0700875 q_vector->eims_value = BIT(msix_vector);
Alexander Duyck55cac242009-11-19 12:42:21 +0000876 break;
Alexander Duyck2d064c02008-07-08 15:10:12 -0700877 default:
878 BUG();
879 break;
880 }
Alexander Duyck26b39272010-02-17 01:00:41 +0000881
882 /* add q_vector eims value to global eims_enable_mask */
883 adapter->eims_enable_mask |= q_vector->eims_value;
884
885 /* configure q_vector to set itr on first interrupt */
886 q_vector->set_itr = 1;
Auke Kok9d5c8242008-01-24 02:22:38 -0800887}
888
889/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +0000890 * igb_configure_msix - Configure MSI-X hardware
891 * @adapter: board private structure to initialize
Auke Kok9d5c8242008-01-24 02:22:38 -0800892 *
Jeff Kirsherb980ac12013-02-23 07:29:56 +0000893 * igb_configure_msix sets up the hardware to properly
894 * generate MSI-X interrupts.
Auke Kok9d5c8242008-01-24 02:22:38 -0800895 **/
896static void igb_configure_msix(struct igb_adapter *adapter)
897{
898 u32 tmp;
899 int i, vector = 0;
900 struct e1000_hw *hw = &adapter->hw;
901
902 adapter->eims_enable_mask = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -0800903
904 /* set vector for other causes, i.e. link changes */
Alexander Duyck2d064c02008-07-08 15:10:12 -0700905 switch (hw->mac.type) {
906 case e1000_82575:
Auke Kok9d5c8242008-01-24 02:22:38 -0800907 tmp = rd32(E1000_CTRL_EXT);
908 /* enable MSI-X PBA support*/
909 tmp |= E1000_CTRL_EXT_PBA_CLR;
910
911 /* Auto-Mask interrupts upon ICR read. */
912 tmp |= E1000_CTRL_EXT_EIAME;
913 tmp |= E1000_CTRL_EXT_IRCA;
914
915 wr32(E1000_CTRL_EXT, tmp);
Alexander Duyck047e0032009-10-27 15:49:27 +0000916
917 /* enable msix_other interrupt */
Jeff Kirsherb980ac12013-02-23 07:29:56 +0000918 array_wr32(E1000_MSIXBM(0), vector++, E1000_EIMS_OTHER);
PJ Waskiewicz844290e2008-06-27 11:00:39 -0700919 adapter->eims_other = E1000_EIMS_OTHER;
Auke Kok9d5c8242008-01-24 02:22:38 -0800920
Alexander Duyck2d064c02008-07-08 15:10:12 -0700921 break;
922
923 case e1000_82576:
Alexander Duyck55cac242009-11-19 12:42:21 +0000924 case e1000_82580:
Alexander Duyckd2ba2ed2010-03-22 14:08:06 +0000925 case e1000_i350:
Carolyn Wybornyceb5f132013-04-18 22:21:30 +0000926 case e1000_i354:
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +0000927 case e1000_i210:
928 case e1000_i211:
Alexander Duyck047e0032009-10-27 15:49:27 +0000929 /* Turn on MSI-X capability first, or our settings
Jeff Kirsherb980ac12013-02-23 07:29:56 +0000930 * won't stick. And it will take days to debug.
931 */
Alexander Duyck047e0032009-10-27 15:49:27 +0000932 wr32(E1000_GPIE, E1000_GPIE_MSIX_MODE |
Jeff Kirsherb980ac12013-02-23 07:29:56 +0000933 E1000_GPIE_PBA | E1000_GPIE_EIAME |
934 E1000_GPIE_NSICR);
Alexander Duyck2d064c02008-07-08 15:10:12 -0700935
Alexander Duyck047e0032009-10-27 15:49:27 +0000936 /* enable msix_other interrupt */
Jacob Kellera51d8c22016-04-13 16:08:28 -0700937 adapter->eims_other = BIT(vector);
Alexander Duyck047e0032009-10-27 15:49:27 +0000938 tmp = (vector++ | E1000_IVAR_VALID) << 8;
939
940 wr32(E1000_IVAR_MISC, tmp);
Alexander Duyck2d064c02008-07-08 15:10:12 -0700941 break;
942 default:
943 /* do nothing, since nothing else supports MSI-X */
944 break;
945 } /* switch (hw->mac.type) */
Alexander Duyck047e0032009-10-27 15:49:27 +0000946
947 adapter->eims_enable_mask |= adapter->eims_other;
948
Alexander Duyck26b39272010-02-17 01:00:41 +0000949 for (i = 0; i < adapter->num_q_vectors; i++)
950 igb_assign_vector(adapter->q_vector[i], vector++);
Alexander Duyck047e0032009-10-27 15:49:27 +0000951
Auke Kok9d5c8242008-01-24 02:22:38 -0800952 wrfl();
953}
954
955/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +0000956 * igb_request_msix - Initialize MSI-X interrupts
957 * @adapter: board private structure to initialize
Auke Kok9d5c8242008-01-24 02:22:38 -0800958 *
Jeff Kirsherb980ac12013-02-23 07:29:56 +0000959 * igb_request_msix allocates MSI-X vectors and requests interrupts from the
960 * kernel.
Auke Kok9d5c8242008-01-24 02:22:38 -0800961 **/
962static int igb_request_msix(struct igb_adapter *adapter)
963{
964 struct net_device *netdev = adapter->netdev;
Stefan Assmann52285b72012-12-04 06:00:17 +0000965 int i, err = 0, vector = 0, free_vector = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -0800966
Auke Kok9d5c8242008-01-24 02:22:38 -0800967 err = request_irq(adapter->msix_entries[vector].vector,
Jeff Kirsherb980ac12013-02-23 07:29:56 +0000968 igb_msix_other, 0, netdev->name, adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -0800969 if (err)
Stefan Assmann52285b72012-12-04 06:00:17 +0000970 goto err_out;
Alexander Duyck047e0032009-10-27 15:49:27 +0000971
972 for (i = 0; i < adapter->num_q_vectors; i++) {
973 struct igb_q_vector *q_vector = adapter->q_vector[i];
974
Stefan Assmann52285b72012-12-04 06:00:17 +0000975 vector++;
976
Jarod Wilson7b06a692015-10-19 11:52:04 -0400977 q_vector->itr_register = adapter->io_addr + E1000_EITR(vector);
Alexander Duyck047e0032009-10-27 15:49:27 +0000978
Alexander Duyck0ba82992011-08-26 07:45:47 +0000979 if (q_vector->rx.ring && q_vector->tx.ring)
Alexander Duyck047e0032009-10-27 15:49:27 +0000980 sprintf(q_vector->name, "%s-TxRx-%u", netdev->name,
Alexander Duyck0ba82992011-08-26 07:45:47 +0000981 q_vector->rx.ring->queue_index);
982 else if (q_vector->tx.ring)
Alexander Duyck047e0032009-10-27 15:49:27 +0000983 sprintf(q_vector->name, "%s-tx-%u", netdev->name,
Alexander Duyck0ba82992011-08-26 07:45:47 +0000984 q_vector->tx.ring->queue_index);
985 else if (q_vector->rx.ring)
Alexander Duyck047e0032009-10-27 15:49:27 +0000986 sprintf(q_vector->name, "%s-rx-%u", netdev->name,
Alexander Duyck0ba82992011-08-26 07:45:47 +0000987 q_vector->rx.ring->queue_index);
Alexander Duyck047e0032009-10-27 15:49:27 +0000988 else
989 sprintf(q_vector->name, "%s-unused", netdev->name);
990
991 err = request_irq(adapter->msix_entries[vector].vector,
Jeff Kirsherb980ac12013-02-23 07:29:56 +0000992 igb_msix_ring, 0, q_vector->name,
993 q_vector);
Alexander Duyck047e0032009-10-27 15:49:27 +0000994 if (err)
Stefan Assmann52285b72012-12-04 06:00:17 +0000995 goto err_free;
Alexander Duyck047e0032009-10-27 15:49:27 +0000996 }
Auke Kok9d5c8242008-01-24 02:22:38 -0800997
Auke Kok9d5c8242008-01-24 02:22:38 -0800998 igb_configure_msix(adapter);
999 return 0;
Stefan Assmann52285b72012-12-04 06:00:17 +00001000
1001err_free:
1002 /* free already assigned IRQs */
1003 free_irq(adapter->msix_entries[free_vector++].vector, adapter);
1004
1005 vector--;
1006 for (i = 0; i < vector; i++) {
1007 free_irq(adapter->msix_entries[free_vector++].vector,
1008 adapter->q_vector[i]);
1009 }
1010err_out:
Auke Kok9d5c8242008-01-24 02:22:38 -08001011 return err;
1012}
1013
Alexander Duyck047e0032009-10-27 15:49:27 +00001014/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00001015 * igb_free_q_vector - Free memory allocated for specific interrupt vector
1016 * @adapter: board private structure to initialize
1017 * @v_idx: Index of vector to be freed
Alexander Duyck5536d212012-09-25 00:31:17 +00001018 *
Carolyn Wyborny02ef6e12013-12-10 07:58:29 +00001019 * This function frees the memory allocated to the q_vector.
Alexander Duyck5536d212012-09-25 00:31:17 +00001020 **/
1021static void igb_free_q_vector(struct igb_adapter *adapter, int v_idx)
1022{
1023 struct igb_q_vector *q_vector = adapter->q_vector[v_idx];
1024
Carolyn Wyborny02ef6e12013-12-10 07:58:29 +00001025 adapter->q_vector[v_idx] = NULL;
1026
1027 /* igb_get_stats64() might access the rings on this vector,
1028 * we must wait a grace period before freeing it.
1029 */
Carolyn Wyborny17a402a2014-11-21 23:52:54 -08001030 if (q_vector)
1031 kfree_rcu(q_vector, rcu);
Carolyn Wyborny02ef6e12013-12-10 07:58:29 +00001032}
1033
1034/**
1035 * igb_reset_q_vector - Reset config for interrupt vector
1036 * @adapter: board private structure to initialize
1037 * @v_idx: Index of vector to be reset
1038 *
1039 * If NAPI is enabled it will delete any references to the
1040 * NAPI struct. This is preparation for igb_free_q_vector.
1041 **/
1042static void igb_reset_q_vector(struct igb_adapter *adapter, int v_idx)
1043{
1044 struct igb_q_vector *q_vector = adapter->q_vector[v_idx];
1045
Christoph Paaschcb06d102014-03-21 03:48:19 -07001046 /* Coming from igb_set_interrupt_capability, the vectors are not yet
1047 * allocated. So, q_vector is NULL so we should stop here.
1048 */
1049 if (!q_vector)
1050 return;
1051
Alexander Duyck5536d212012-09-25 00:31:17 +00001052 if (q_vector->tx.ring)
1053 adapter->tx_ring[q_vector->tx.ring->queue_index] = NULL;
1054
1055 if (q_vector->rx.ring)
Toshiaki Makita2439fc42015-04-13 18:15:11 +09001056 adapter->rx_ring[q_vector->rx.ring->queue_index] = NULL;
Alexander Duyck5536d212012-09-25 00:31:17 +00001057
Alexander Duyck5536d212012-09-25 00:31:17 +00001058 netif_napi_del(&q_vector->napi);
1059
Carolyn Wyborny02ef6e12013-12-10 07:58:29 +00001060}
1061
1062static void igb_reset_interrupt_capability(struct igb_adapter *adapter)
1063{
1064 int v_idx = adapter->num_q_vectors;
1065
Carolyn Wybornycd14ef52013-12-10 07:58:34 +00001066 if (adapter->flags & IGB_FLAG_HAS_MSIX)
Carolyn Wyborny02ef6e12013-12-10 07:58:29 +00001067 pci_disable_msix(adapter->pdev);
Carolyn Wybornycd14ef52013-12-10 07:58:34 +00001068 else if (adapter->flags & IGB_FLAG_HAS_MSI)
Carolyn Wyborny02ef6e12013-12-10 07:58:29 +00001069 pci_disable_msi(adapter->pdev);
Carolyn Wyborny02ef6e12013-12-10 07:58:29 +00001070
1071 while (v_idx--)
1072 igb_reset_q_vector(adapter, v_idx);
Alexander Duyck5536d212012-09-25 00:31:17 +00001073}
1074
1075/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00001076 * igb_free_q_vectors - Free memory allocated for interrupt vectors
1077 * @adapter: board private structure to initialize
Alexander Duyck047e0032009-10-27 15:49:27 +00001078 *
Jeff Kirsherb980ac12013-02-23 07:29:56 +00001079 * This function frees the memory allocated to the q_vectors. In addition if
1080 * NAPI is enabled it will delete any references to the NAPI struct prior
1081 * to freeing the q_vector.
Alexander Duyck047e0032009-10-27 15:49:27 +00001082 **/
1083static void igb_free_q_vectors(struct igb_adapter *adapter)
1084{
Alexander Duyck5536d212012-09-25 00:31:17 +00001085 int v_idx = adapter->num_q_vectors;
Alexander Duyck047e0032009-10-27 15:49:27 +00001086
Alexander Duyck5536d212012-09-25 00:31:17 +00001087 adapter->num_tx_queues = 0;
1088 adapter->num_rx_queues = 0;
Alexander Duyck047e0032009-10-27 15:49:27 +00001089 adapter->num_q_vectors = 0;
Alexander Duyck5536d212012-09-25 00:31:17 +00001090
Carolyn Wyborny02ef6e12013-12-10 07:58:29 +00001091 while (v_idx--) {
1092 igb_reset_q_vector(adapter, v_idx);
Alexander Duyck5536d212012-09-25 00:31:17 +00001093 igb_free_q_vector(adapter, v_idx);
Carolyn Wyborny02ef6e12013-12-10 07:58:29 +00001094 }
Alexander Duyck047e0032009-10-27 15:49:27 +00001095}
1096
1097/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00001098 * igb_clear_interrupt_scheme - reset the device to a state of no interrupts
1099 * @adapter: board private structure to initialize
Alexander Duyck047e0032009-10-27 15:49:27 +00001100 *
Jeff Kirsherb980ac12013-02-23 07:29:56 +00001101 * This function resets the device so that it has 0 Rx queues, Tx queues, and
1102 * MSI-X interrupts allocated.
Alexander Duyck047e0032009-10-27 15:49:27 +00001103 */
1104static void igb_clear_interrupt_scheme(struct igb_adapter *adapter)
1105{
Alexander Duyck047e0032009-10-27 15:49:27 +00001106 igb_free_q_vectors(adapter);
1107 igb_reset_interrupt_capability(adapter);
1108}
Auke Kok9d5c8242008-01-24 02:22:38 -08001109
1110/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00001111 * igb_set_interrupt_capability - set MSI or MSI-X if supported
1112 * @adapter: board private structure to initialize
1113 * @msix: boolean value of MSIX capability
Auke Kok9d5c8242008-01-24 02:22:38 -08001114 *
Jeff Kirsherb980ac12013-02-23 07:29:56 +00001115 * Attempt to configure interrupts using the best available
1116 * capabilities of the hardware and kernel.
Auke Kok9d5c8242008-01-24 02:22:38 -08001117 **/
Stefan Assmann53c7d062012-12-04 06:00:12 +00001118static void igb_set_interrupt_capability(struct igb_adapter *adapter, bool msix)
Auke Kok9d5c8242008-01-24 02:22:38 -08001119{
1120 int err;
1121 int numvecs, i;
1122
Stefan Assmann53c7d062012-12-04 06:00:12 +00001123 if (!msix)
1124 goto msi_only;
Carolyn Wybornycd14ef52013-12-10 07:58:34 +00001125 adapter->flags |= IGB_FLAG_HAS_MSIX;
Stefan Assmann53c7d062012-12-04 06:00:12 +00001126
Alexander Duyck83b71802009-02-06 23:15:45 +00001127 /* Number of supported queues. */
Alexander Duycka99955f2009-11-12 18:37:19 +00001128 adapter->num_rx_queues = adapter->rss_queues;
Greg Rose5fa85172010-07-01 13:38:16 +00001129 if (adapter->vfs_allocated_count)
1130 adapter->num_tx_queues = 1;
1131 else
1132 adapter->num_tx_queues = adapter->rss_queues;
Alexander Duyck83b71802009-02-06 23:15:45 +00001133
Jeff Kirsherb980ac12013-02-23 07:29:56 +00001134 /* start with one vector for every Rx queue */
Alexander Duyck047e0032009-10-27 15:49:27 +00001135 numvecs = adapter->num_rx_queues;
1136
Jeff Kirsherb980ac12013-02-23 07:29:56 +00001137 /* if Tx handler is separate add 1 for every Tx queue */
Alexander Duycka99955f2009-11-12 18:37:19 +00001138 if (!(adapter->flags & IGB_FLAG_QUEUE_PAIRS))
1139 numvecs += adapter->num_tx_queues;
Alexander Duyck047e0032009-10-27 15:49:27 +00001140
1141 /* store the number of vectors reserved for queues */
1142 adapter->num_q_vectors = numvecs;
1143
1144 /* add 1 vector for link status interrupts */
1145 numvecs++;
Auke Kok9d5c8242008-01-24 02:22:38 -08001146 for (i = 0; i < numvecs; i++)
1147 adapter->msix_entries[i].entry = i;
1148
Alexander Gordeev479d02d2014-02-18 11:11:43 +01001149 err = pci_enable_msix_range(adapter->pdev,
1150 adapter->msix_entries,
1151 numvecs,
1152 numvecs);
1153 if (err > 0)
Alexander Duyck0c2cc022012-09-25 00:31:22 +00001154 return;
Auke Kok9d5c8242008-01-24 02:22:38 -08001155
1156 igb_reset_interrupt_capability(adapter);
1157
1158 /* If we can't do MSI-X, try MSI */
1159msi_only:
Christoph Paaschb7093232014-03-21 04:02:09 -07001160 adapter->flags &= ~IGB_FLAG_HAS_MSIX;
Alexander Duyck2a3abf62009-04-07 14:37:52 +00001161#ifdef CONFIG_PCI_IOV
1162 /* disable SR-IOV for non MSI-X configurations */
1163 if (adapter->vf_data) {
1164 struct e1000_hw *hw = &adapter->hw;
1165 /* disable iov and allow time for transactions to clear */
1166 pci_disable_sriov(adapter->pdev);
1167 msleep(500);
1168
Yury Kylulin4827cc32017-03-07 11:20:26 +03001169 kfree(adapter->vf_mac_list);
1170 adapter->vf_mac_list = NULL;
Alexander Duyck2a3abf62009-04-07 14:37:52 +00001171 kfree(adapter->vf_data);
1172 adapter->vf_data = NULL;
1173 wr32(E1000_IOVCTL, E1000_IOVCTL_REUSE_VFQ);
Jesse Brandeburg945a5152011-07-20 00:56:21 +00001174 wrfl();
Alexander Duyck2a3abf62009-04-07 14:37:52 +00001175 msleep(100);
1176 dev_info(&adapter->pdev->dev, "IOV Disabled\n");
1177 }
1178#endif
Alexander Duyck4fc82ad2009-10-27 23:45:42 +00001179 adapter->vfs_allocated_count = 0;
Alexander Duycka99955f2009-11-12 18:37:19 +00001180 adapter->rss_queues = 1;
Alexander Duyck4fc82ad2009-10-27 23:45:42 +00001181 adapter->flags |= IGB_FLAG_QUEUE_PAIRS;
Auke Kok9d5c8242008-01-24 02:22:38 -08001182 adapter->num_rx_queues = 1;
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07001183 adapter->num_tx_queues = 1;
Alexander Duyck047e0032009-10-27 15:49:27 +00001184 adapter->num_q_vectors = 1;
Auke Kok9d5c8242008-01-24 02:22:38 -08001185 if (!pci_enable_msi(adapter->pdev))
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07001186 adapter->flags |= IGB_FLAG_HAS_MSI;
Auke Kok9d5c8242008-01-24 02:22:38 -08001187}
1188
Alexander Duyck5536d212012-09-25 00:31:17 +00001189static void igb_add_ring(struct igb_ring *ring,
1190 struct igb_ring_container *head)
1191{
1192 head->ring = ring;
1193 head->count++;
1194}
1195
1196/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00001197 * igb_alloc_q_vector - Allocate memory for a single interrupt vector
1198 * @adapter: board private structure to initialize
1199 * @v_count: q_vectors allocated on adapter, used for ring interleaving
1200 * @v_idx: index of vector in adapter struct
1201 * @txr_count: total number of Tx rings to allocate
1202 * @txr_idx: index of first Tx ring to allocate
1203 * @rxr_count: total number of Rx rings to allocate
1204 * @rxr_idx: index of first Rx ring to allocate
Alexander Duyck5536d212012-09-25 00:31:17 +00001205 *
Jeff Kirsherb980ac12013-02-23 07:29:56 +00001206 * We allocate one q_vector. If allocation fails we return -ENOMEM.
Alexander Duyck5536d212012-09-25 00:31:17 +00001207 **/
1208static int igb_alloc_q_vector(struct igb_adapter *adapter,
1209 int v_count, int v_idx,
1210 int txr_count, int txr_idx,
1211 int rxr_count, int rxr_idx)
1212{
1213 struct igb_q_vector *q_vector;
1214 struct igb_ring *ring;
1215 int ring_count, size;
1216
1217 /* igb only supports 1 Tx and/or 1 Rx queue per vector */
1218 if (txr_count > 1 || rxr_count > 1)
1219 return -ENOMEM;
1220
1221 ring_count = txr_count + rxr_count;
1222 size = sizeof(struct igb_q_vector) +
1223 (sizeof(struct igb_ring) * ring_count);
1224
1225 /* allocate q_vector and rings */
Carolyn Wyborny02ef6e12013-12-10 07:58:29 +00001226 q_vector = adapter->q_vector[v_idx];
Shota Suzuki72ddef02015-07-01 09:25:52 +09001227 if (!q_vector) {
Carolyn Wyborny02ef6e12013-12-10 07:58:29 +00001228 q_vector = kzalloc(size, GFP_KERNEL);
Shota Suzuki72ddef02015-07-01 09:25:52 +09001229 } else if (size > ksize(q_vector)) {
1230 kfree_rcu(q_vector, rcu);
1231 q_vector = kzalloc(size, GFP_KERNEL);
1232 } else {
Toshiaki Makitac0a06ee2015-04-13 18:15:10 +09001233 memset(q_vector, 0, size);
Shota Suzuki72ddef02015-07-01 09:25:52 +09001234 }
Alexander Duyck5536d212012-09-25 00:31:17 +00001235 if (!q_vector)
1236 return -ENOMEM;
1237
1238 /* initialize NAPI */
1239 netif_napi_add(adapter->netdev, &q_vector->napi,
1240 igb_poll, 64);
1241
1242 /* tie q_vector and adapter together */
1243 adapter->q_vector[v_idx] = q_vector;
1244 q_vector->adapter = adapter;
1245
1246 /* initialize work limits */
1247 q_vector->tx.work_limit = adapter->tx_work_limit;
1248
1249 /* initialize ITR configuration */
Jarod Wilson7b06a692015-10-19 11:52:04 -04001250 q_vector->itr_register = adapter->io_addr + E1000_EITR(0);
Alexander Duyck5536d212012-09-25 00:31:17 +00001251 q_vector->itr_val = IGB_START_ITR;
1252
1253 /* initialize pointer to rings */
1254 ring = q_vector->ring;
1255
Alexander Duyck4e2276672013-02-12 02:31:01 +00001256 /* intialize ITR */
1257 if (rxr_count) {
1258 /* rx or rx/tx vector */
1259 if (!adapter->rx_itr_setting || adapter->rx_itr_setting > 3)
1260 q_vector->itr_val = adapter->rx_itr_setting;
1261 } else {
1262 /* tx only vector */
1263 if (!adapter->tx_itr_setting || adapter->tx_itr_setting > 3)
1264 q_vector->itr_val = adapter->tx_itr_setting;
1265 }
1266
Alexander Duyck5536d212012-09-25 00:31:17 +00001267 if (txr_count) {
1268 /* assign generic ring traits */
1269 ring->dev = &adapter->pdev->dev;
1270 ring->netdev = adapter->netdev;
1271
1272 /* configure backlink on ring */
1273 ring->q_vector = q_vector;
1274
1275 /* update q_vector Tx values */
1276 igb_add_ring(ring, &q_vector->tx);
1277
1278 /* For 82575, context index must be unique per ring. */
1279 if (adapter->hw.mac.type == e1000_82575)
1280 set_bit(IGB_RING_FLAG_TX_CTX_IDX, &ring->flags);
1281
1282 /* apply Tx specific ring traits */
1283 ring->count = adapter->tx_ring_count;
1284 ring->queue_index = txr_idx;
1285
Andre Guedes05f9d3e2017-10-16 18:01:28 -07001286 ring->cbs_enable = false;
1287 ring->idleslope = 0;
1288 ring->sendslope = 0;
1289 ring->hicredit = 0;
1290 ring->locredit = 0;
1291
John Stultz827da442013-10-07 15:51:58 -07001292 u64_stats_init(&ring->tx_syncp);
1293 u64_stats_init(&ring->tx_syncp2);
1294
Alexander Duyck5536d212012-09-25 00:31:17 +00001295 /* assign ring to adapter */
1296 adapter->tx_ring[txr_idx] = ring;
1297
1298 /* push pointer to next ring */
1299 ring++;
1300 }
1301
1302 if (rxr_count) {
1303 /* assign generic ring traits */
1304 ring->dev = &adapter->pdev->dev;
1305 ring->netdev = adapter->netdev;
1306
1307 /* configure backlink on ring */
1308 ring->q_vector = q_vector;
1309
1310 /* update q_vector Rx values */
1311 igb_add_ring(ring, &q_vector->rx);
1312
1313 /* set flag indicating ring supports SCTP checksum offload */
1314 if (adapter->hw.mac.type >= e1000_82576)
1315 set_bit(IGB_RING_FLAG_RX_SCTP_CSUM, &ring->flags);
1316
Carolyn Wybornye52c0f92014-04-11 01:46:06 +00001317 /* On i350, i354, i210, and i211, loopback VLAN packets
Alexander Duyck5536d212012-09-25 00:31:17 +00001318 * have the tag byte-swapped.
Jeff Kirsherb980ac12013-02-23 07:29:56 +00001319 */
Alexander Duyck5536d212012-09-25 00:31:17 +00001320 if (adapter->hw.mac.type >= e1000_i350)
1321 set_bit(IGB_RING_FLAG_RX_LB_VLAN_BSWAP, &ring->flags);
1322
1323 /* apply Rx specific ring traits */
1324 ring->count = adapter->rx_ring_count;
1325 ring->queue_index = rxr_idx;
1326
John Stultz827da442013-10-07 15:51:58 -07001327 u64_stats_init(&ring->rx_syncp);
1328
Alexander Duyck5536d212012-09-25 00:31:17 +00001329 /* assign ring to adapter */
1330 adapter->rx_ring[rxr_idx] = ring;
1331 }
1332
1333 return 0;
1334}
1335
1336
Auke Kok9d5c8242008-01-24 02:22:38 -08001337/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00001338 * igb_alloc_q_vectors - Allocate memory for interrupt vectors
1339 * @adapter: board private structure to initialize
Alexander Duyck047e0032009-10-27 15:49:27 +00001340 *
Jeff Kirsherb980ac12013-02-23 07:29:56 +00001341 * We allocate one q_vector per queue interrupt. If allocation fails we
1342 * return -ENOMEM.
Alexander Duyck047e0032009-10-27 15:49:27 +00001343 **/
1344static int igb_alloc_q_vectors(struct igb_adapter *adapter)
1345{
Alexander Duyck5536d212012-09-25 00:31:17 +00001346 int q_vectors = adapter->num_q_vectors;
1347 int rxr_remaining = adapter->num_rx_queues;
1348 int txr_remaining = adapter->num_tx_queues;
1349 int rxr_idx = 0, txr_idx = 0, v_idx = 0;
1350 int err;
Alexander Duyck047e0032009-10-27 15:49:27 +00001351
Alexander Duyck5536d212012-09-25 00:31:17 +00001352 if (q_vectors >= (rxr_remaining + txr_remaining)) {
1353 for (; rxr_remaining; v_idx++) {
1354 err = igb_alloc_q_vector(adapter, q_vectors, v_idx,
1355 0, 0, 1, rxr_idx);
1356
1357 if (err)
1358 goto err_out;
1359
1360 /* update counts and index */
1361 rxr_remaining--;
1362 rxr_idx++;
1363 }
1364 }
1365
1366 for (; v_idx < q_vectors; v_idx++) {
1367 int rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - v_idx);
1368 int tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - v_idx);
Carolyn Wyborny9005df32014-04-11 01:45:34 +00001369
Alexander Duyck5536d212012-09-25 00:31:17 +00001370 err = igb_alloc_q_vector(adapter, q_vectors, v_idx,
1371 tqpv, txr_idx, rqpv, rxr_idx);
1372
1373 if (err)
Alexander Duyck047e0032009-10-27 15:49:27 +00001374 goto err_out;
Alexander Duyck5536d212012-09-25 00:31:17 +00001375
1376 /* update counts and index */
1377 rxr_remaining -= rqpv;
1378 txr_remaining -= tqpv;
1379 rxr_idx++;
1380 txr_idx++;
Alexander Duyck047e0032009-10-27 15:49:27 +00001381 }
Alexander Duyck81c2fc22011-08-26 07:45:20 +00001382
Alexander Duyck047e0032009-10-27 15:49:27 +00001383 return 0;
1384
1385err_out:
Alexander Duyck5536d212012-09-25 00:31:17 +00001386 adapter->num_tx_queues = 0;
1387 adapter->num_rx_queues = 0;
1388 adapter->num_q_vectors = 0;
1389
1390 while (v_idx--)
1391 igb_free_q_vector(adapter, v_idx);
1392
Alexander Duyck047e0032009-10-27 15:49:27 +00001393 return -ENOMEM;
1394}
1395
Alexander Duyck047e0032009-10-27 15:49:27 +00001396/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00001397 * igb_init_interrupt_scheme - initialize interrupts, allocate queues/vectors
1398 * @adapter: board private structure to initialize
1399 * @msix: boolean value of MSIX capability
Alexander Duyck047e0032009-10-27 15:49:27 +00001400 *
Jeff Kirsherb980ac12013-02-23 07:29:56 +00001401 * This function initializes the interrupts and allocates all of the queues.
Alexander Duyck047e0032009-10-27 15:49:27 +00001402 **/
Stefan Assmann53c7d062012-12-04 06:00:12 +00001403static int igb_init_interrupt_scheme(struct igb_adapter *adapter, bool msix)
Alexander Duyck047e0032009-10-27 15:49:27 +00001404{
1405 struct pci_dev *pdev = adapter->pdev;
1406 int err;
1407
Stefan Assmann53c7d062012-12-04 06:00:12 +00001408 igb_set_interrupt_capability(adapter, msix);
Alexander Duyck047e0032009-10-27 15:49:27 +00001409
1410 err = igb_alloc_q_vectors(adapter);
1411 if (err) {
1412 dev_err(&pdev->dev, "Unable to allocate memory for vectors\n");
1413 goto err_alloc_q_vectors;
1414 }
1415
Alexander Duyck5536d212012-09-25 00:31:17 +00001416 igb_cache_ring_register(adapter);
Alexander Duyck047e0032009-10-27 15:49:27 +00001417
1418 return 0;
Alexander Duyck5536d212012-09-25 00:31:17 +00001419
Alexander Duyck047e0032009-10-27 15:49:27 +00001420err_alloc_q_vectors:
1421 igb_reset_interrupt_capability(adapter);
1422 return err;
1423}
1424
1425/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00001426 * igb_request_irq - initialize interrupts
1427 * @adapter: board private structure to initialize
Auke Kok9d5c8242008-01-24 02:22:38 -08001428 *
Jeff Kirsherb980ac12013-02-23 07:29:56 +00001429 * Attempts to configure interrupts using the best available
1430 * capabilities of the hardware and kernel.
Auke Kok9d5c8242008-01-24 02:22:38 -08001431 **/
1432static int igb_request_irq(struct igb_adapter *adapter)
1433{
1434 struct net_device *netdev = adapter->netdev;
Alexander Duyck047e0032009-10-27 15:49:27 +00001435 struct pci_dev *pdev = adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08001436 int err = 0;
1437
Carolyn Wybornycd14ef52013-12-10 07:58:34 +00001438 if (adapter->flags & IGB_FLAG_HAS_MSIX) {
Auke Kok9d5c8242008-01-24 02:22:38 -08001439 err = igb_request_msix(adapter);
PJ Waskiewicz844290e2008-06-27 11:00:39 -07001440 if (!err)
Auke Kok9d5c8242008-01-24 02:22:38 -08001441 goto request_done;
Auke Kok9d5c8242008-01-24 02:22:38 -08001442 /* fall back to MSI */
Alexander Duyck5536d212012-09-25 00:31:17 +00001443 igb_free_all_tx_resources(adapter);
1444 igb_free_all_rx_resources(adapter);
Stefan Assmann53c7d062012-12-04 06:00:12 +00001445
Alexander Duyck047e0032009-10-27 15:49:27 +00001446 igb_clear_interrupt_scheme(adapter);
Stefan Assmann53c7d062012-12-04 06:00:12 +00001447 err = igb_init_interrupt_scheme(adapter, false);
1448 if (err)
Alexander Duyck047e0032009-10-27 15:49:27 +00001449 goto request_done;
Stefan Assmann53c7d062012-12-04 06:00:12 +00001450
Alexander Duyck047e0032009-10-27 15:49:27 +00001451 igb_setup_all_tx_resources(adapter);
1452 igb_setup_all_rx_resources(adapter);
Stefan Assmann53c7d062012-12-04 06:00:12 +00001453 igb_configure(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001454 }
PJ Waskiewicz844290e2008-06-27 11:00:39 -07001455
Alexander Duyckc74d5882011-08-26 07:46:45 +00001456 igb_assign_vector(adapter->q_vector[0], 0);
1457
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07001458 if (adapter->flags & IGB_FLAG_HAS_MSI) {
Alexander Duyckc74d5882011-08-26 07:46:45 +00001459 err = request_irq(pdev->irq, igb_intr_msi, 0,
Alexander Duyck047e0032009-10-27 15:49:27 +00001460 netdev->name, adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001461 if (!err)
1462 goto request_done;
Alexander Duyck047e0032009-10-27 15:49:27 +00001463
Auke Kok9d5c8242008-01-24 02:22:38 -08001464 /* fall back to legacy interrupts */
1465 igb_reset_interrupt_capability(adapter);
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07001466 adapter->flags &= ~IGB_FLAG_HAS_MSI;
Auke Kok9d5c8242008-01-24 02:22:38 -08001467 }
1468
Alexander Duyckc74d5882011-08-26 07:46:45 +00001469 err = request_irq(pdev->irq, igb_intr, IRQF_SHARED,
Alexander Duyck047e0032009-10-27 15:49:27 +00001470 netdev->name, adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001471
Andy Gospodarek6cb5e572008-02-15 14:05:25 -08001472 if (err)
Alexander Duyckc74d5882011-08-26 07:46:45 +00001473 dev_err(&pdev->dev, "Error %d getting interrupt\n",
Auke Kok9d5c8242008-01-24 02:22:38 -08001474 err);
Auke Kok9d5c8242008-01-24 02:22:38 -08001475
1476request_done:
1477 return err;
1478}
1479
1480static void igb_free_irq(struct igb_adapter *adapter)
1481{
Carolyn Wybornycd14ef52013-12-10 07:58:34 +00001482 if (adapter->flags & IGB_FLAG_HAS_MSIX) {
Auke Kok9d5c8242008-01-24 02:22:38 -08001483 int vector = 0, i;
1484
Alexander Duyck047e0032009-10-27 15:49:27 +00001485 free_irq(adapter->msix_entries[vector++].vector, adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001486
Alexander Duyck0d1ae7f2011-08-26 07:46:34 +00001487 for (i = 0; i < adapter->num_q_vectors; i++)
Alexander Duyck047e0032009-10-27 15:49:27 +00001488 free_irq(adapter->msix_entries[vector++].vector,
Alexander Duyck0d1ae7f2011-08-26 07:46:34 +00001489 adapter->q_vector[i]);
Alexander Duyck047e0032009-10-27 15:49:27 +00001490 } else {
1491 free_irq(adapter->pdev->irq, adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001492 }
Auke Kok9d5c8242008-01-24 02:22:38 -08001493}
1494
1495/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00001496 * igb_irq_disable - Mask off interrupt generation on the NIC
1497 * @adapter: board private structure
Auke Kok9d5c8242008-01-24 02:22:38 -08001498 **/
1499static void igb_irq_disable(struct igb_adapter *adapter)
1500{
1501 struct e1000_hw *hw = &adapter->hw;
1502
Jeff Kirsherb980ac12013-02-23 07:29:56 +00001503 /* we need to be careful when disabling interrupts. The VFs are also
Alexander Duyck25568a52009-10-27 23:49:59 +00001504 * mapped into these registers and so clearing the bits can cause
1505 * issues on the VF drivers so we only need to clear what we set
1506 */
Carolyn Wybornycd14ef52013-12-10 07:58:34 +00001507 if (adapter->flags & IGB_FLAG_HAS_MSIX) {
Alexander Duyck2dfd1212009-09-03 14:49:15 +00001508 u32 regval = rd32(E1000_EIAM);
Carolyn Wyborny9005df32014-04-11 01:45:34 +00001509
Alexander Duyck2dfd1212009-09-03 14:49:15 +00001510 wr32(E1000_EIAM, regval & ~adapter->eims_enable_mask);
1511 wr32(E1000_EIMC, adapter->eims_enable_mask);
1512 regval = rd32(E1000_EIAC);
1513 wr32(E1000_EIAC, regval & ~adapter->eims_enable_mask);
Auke Kok9d5c8242008-01-24 02:22:38 -08001514 }
PJ Waskiewicz844290e2008-06-27 11:00:39 -07001515
1516 wr32(E1000_IAM, 0);
Auke Kok9d5c8242008-01-24 02:22:38 -08001517 wr32(E1000_IMC, ~0);
1518 wrfl();
Carolyn Wybornycd14ef52013-12-10 07:58:34 +00001519 if (adapter->flags & IGB_FLAG_HAS_MSIX) {
Emil Tantilov81a61852010-08-02 14:40:52 +00001520 int i;
Carolyn Wyborny9005df32014-04-11 01:45:34 +00001521
Emil Tantilov81a61852010-08-02 14:40:52 +00001522 for (i = 0; i < adapter->num_q_vectors; i++)
1523 synchronize_irq(adapter->msix_entries[i].vector);
1524 } else {
1525 synchronize_irq(adapter->pdev->irq);
1526 }
Auke Kok9d5c8242008-01-24 02:22:38 -08001527}
1528
1529/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00001530 * igb_irq_enable - Enable default interrupt generation settings
1531 * @adapter: board private structure
Auke Kok9d5c8242008-01-24 02:22:38 -08001532 **/
1533static void igb_irq_enable(struct igb_adapter *adapter)
1534{
1535 struct e1000_hw *hw = &adapter->hw;
1536
Carolyn Wybornycd14ef52013-12-10 07:58:34 +00001537 if (adapter->flags & IGB_FLAG_HAS_MSIX) {
Alexander Duyck06218a82011-08-26 07:46:55 +00001538 u32 ims = E1000_IMS_LSC | E1000_IMS_DOUTSYNC | E1000_IMS_DRSTA;
Alexander Duyck2dfd1212009-09-03 14:49:15 +00001539 u32 regval = rd32(E1000_EIAC);
Carolyn Wyborny9005df32014-04-11 01:45:34 +00001540
Alexander Duyck2dfd1212009-09-03 14:49:15 +00001541 wr32(E1000_EIAC, regval | adapter->eims_enable_mask);
1542 regval = rd32(E1000_EIAM);
1543 wr32(E1000_EIAM, regval | adapter->eims_enable_mask);
PJ Waskiewicz844290e2008-06-27 11:00:39 -07001544 wr32(E1000_EIMS, adapter->eims_enable_mask);
Alexander Duyck25568a52009-10-27 23:49:59 +00001545 if (adapter->vfs_allocated_count) {
Alexander Duyck4ae196d2009-02-19 20:40:07 -08001546 wr32(E1000_MBVFIMR, 0xFF);
Alexander Duyck25568a52009-10-27 23:49:59 +00001547 ims |= E1000_IMS_VMMB;
1548 }
1549 wr32(E1000_IMS, ims);
PJ Waskiewicz844290e2008-06-27 11:00:39 -07001550 } else {
Alexander Duyck55cac242009-11-19 12:42:21 +00001551 wr32(E1000_IMS, IMS_ENABLE_MASK |
1552 E1000_IMS_DRSTA);
1553 wr32(E1000_IAM, IMS_ENABLE_MASK |
1554 E1000_IMS_DRSTA);
PJ Waskiewicz844290e2008-06-27 11:00:39 -07001555 }
Auke Kok9d5c8242008-01-24 02:22:38 -08001556}
1557
1558static void igb_update_mng_vlan(struct igb_adapter *adapter)
1559{
Alexander Duyck51466232009-10-27 23:47:35 +00001560 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck8b77c6b2016-01-06 23:11:04 -08001561 u16 pf_id = adapter->vfs_allocated_count;
Auke Kok9d5c8242008-01-24 02:22:38 -08001562 u16 vid = adapter->hw.mng_cookie.vlan_id;
1563 u16 old_vid = adapter->mng_vlan_id;
Auke Kok9d5c8242008-01-24 02:22:38 -08001564
Alexander Duyck51466232009-10-27 23:47:35 +00001565 if (hw->mng_cookie.status & E1000_MNG_DHCP_COOKIE_STATUS_VLAN) {
1566 /* add VID to filter table */
Alexander Duyck8b77c6b2016-01-06 23:11:04 -08001567 igb_vfta_set(hw, vid, pf_id, true, true);
Alexander Duyck51466232009-10-27 23:47:35 +00001568 adapter->mng_vlan_id = vid;
1569 } else {
1570 adapter->mng_vlan_id = IGB_MNG_VLAN_NONE;
1571 }
1572
1573 if ((old_vid != (u16)IGB_MNG_VLAN_NONE) &&
1574 (vid != old_vid) &&
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00001575 !test_bit(old_vid, adapter->active_vlans)) {
Alexander Duyck51466232009-10-27 23:47:35 +00001576 /* remove VID from filter table */
Alexander Duyck8b77c6b2016-01-06 23:11:04 -08001577 igb_vfta_set(hw, vid, pf_id, false, true);
Auke Kok9d5c8242008-01-24 02:22:38 -08001578 }
1579}
1580
1581/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00001582 * igb_release_hw_control - release control of the h/w to f/w
1583 * @adapter: address of board private structure
Auke Kok9d5c8242008-01-24 02:22:38 -08001584 *
Jeff Kirsherb980ac12013-02-23 07:29:56 +00001585 * igb_release_hw_control resets CTRL_EXT:DRV_LOAD bit.
1586 * For ASF and Pass Through versions of f/w this means that the
1587 * driver is no longer loaded.
Auke Kok9d5c8242008-01-24 02:22:38 -08001588 **/
1589static void igb_release_hw_control(struct igb_adapter *adapter)
1590{
1591 struct e1000_hw *hw = &adapter->hw;
1592 u32 ctrl_ext;
1593
1594 /* Let firmware take over control of h/w */
1595 ctrl_ext = rd32(E1000_CTRL_EXT);
1596 wr32(E1000_CTRL_EXT,
1597 ctrl_ext & ~E1000_CTRL_EXT_DRV_LOAD);
1598}
1599
Auke Kok9d5c8242008-01-24 02:22:38 -08001600/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00001601 * igb_get_hw_control - get control of the h/w from f/w
1602 * @adapter: address of board private structure
Auke Kok9d5c8242008-01-24 02:22:38 -08001603 *
Jeff Kirsherb980ac12013-02-23 07:29:56 +00001604 * igb_get_hw_control sets CTRL_EXT:DRV_LOAD bit.
1605 * For ASF and Pass Through versions of f/w this means that
1606 * the driver is loaded.
Auke Kok9d5c8242008-01-24 02:22:38 -08001607 **/
1608static void igb_get_hw_control(struct igb_adapter *adapter)
1609{
1610 struct e1000_hw *hw = &adapter->hw;
1611 u32 ctrl_ext;
1612
1613 /* Let firmware know the driver has taken over */
1614 ctrl_ext = rd32(E1000_CTRL_EXT);
1615 wr32(E1000_CTRL_EXT,
1616 ctrl_ext | E1000_CTRL_EXT_DRV_LOAD);
1617}
1618
Andre Guedes05f9d3e2017-10-16 18:01:28 -07001619static void enable_fqtss(struct igb_adapter *adapter, bool enable)
1620{
1621 struct net_device *netdev = adapter->netdev;
1622 struct e1000_hw *hw = &adapter->hw;
1623
1624 WARN_ON(hw->mac.type != e1000_i210);
1625
1626 if (enable)
1627 adapter->flags |= IGB_FLAG_FQTSS;
1628 else
1629 adapter->flags &= ~IGB_FLAG_FQTSS;
1630
1631 if (netif_running(netdev))
1632 schedule_work(&adapter->reset_task);
1633}
1634
1635static bool is_fqtss_enabled(struct igb_adapter *adapter)
1636{
1637 return (adapter->flags & IGB_FLAG_FQTSS) ? true : false;
1638}
1639
1640static void set_tx_desc_fetch_prio(struct e1000_hw *hw, int queue,
1641 enum tx_queue_prio prio)
1642{
1643 u32 val;
1644
1645 WARN_ON(hw->mac.type != e1000_i210);
1646 WARN_ON(queue < 0 || queue > 4);
1647
1648 val = rd32(E1000_I210_TXDCTL(queue));
1649
1650 if (prio == TX_QUEUE_PRIO_HIGH)
1651 val |= E1000_TXDCTL_PRIORITY;
1652 else
1653 val &= ~E1000_TXDCTL_PRIORITY;
1654
1655 wr32(E1000_I210_TXDCTL(queue), val);
1656}
1657
1658static void set_queue_mode(struct e1000_hw *hw, int queue, enum queue_mode mode)
1659{
1660 u32 val;
1661
1662 WARN_ON(hw->mac.type != e1000_i210);
1663 WARN_ON(queue < 0 || queue > 1);
1664
1665 val = rd32(E1000_I210_TQAVCC(queue));
1666
1667 if (mode == QUEUE_MODE_STREAM_RESERVATION)
1668 val |= E1000_TQAVCC_QUEUEMODE;
1669 else
1670 val &= ~E1000_TQAVCC_QUEUEMODE;
1671
1672 wr32(E1000_I210_TQAVCC(queue), val);
1673}
1674
1675/**
1676 * igb_configure_cbs - Configure Credit-Based Shaper (CBS)
1677 * @adapter: pointer to adapter struct
1678 * @queue: queue number
1679 * @enable: true = enable CBS, false = disable CBS
1680 * @idleslope: idleSlope in kbps
1681 * @sendslope: sendSlope in kbps
1682 * @hicredit: hiCredit in bytes
1683 * @locredit: loCredit in bytes
1684 *
1685 * Configure CBS for a given hardware queue. When disabling, idleslope,
1686 * sendslope, hicredit, locredit arguments are ignored. Returns 0 if
1687 * success. Negative otherwise.
1688 **/
1689static void igb_configure_cbs(struct igb_adapter *adapter, int queue,
1690 bool enable, int idleslope, int sendslope,
1691 int hicredit, int locredit)
1692{
1693 struct net_device *netdev = adapter->netdev;
1694 struct e1000_hw *hw = &adapter->hw;
1695 u32 tqavcc;
1696 u16 value;
1697
1698 WARN_ON(hw->mac.type != e1000_i210);
1699 WARN_ON(queue < 0 || queue > 1);
1700
1701 if (enable) {
1702 set_tx_desc_fetch_prio(hw, queue, TX_QUEUE_PRIO_HIGH);
1703 set_queue_mode(hw, queue, QUEUE_MODE_STREAM_RESERVATION);
1704
1705 /* According to i210 datasheet section 7.2.7.7, we should set
1706 * the 'idleSlope' field from TQAVCC register following the
1707 * equation:
1708 *
1709 * For 100 Mbps link speed:
1710 *
1711 * value = BW * 0x7735 * 0.2 (E1)
1712 *
1713 * For 1000Mbps link speed:
1714 *
1715 * value = BW * 0x7735 * 2 (E2)
1716 *
1717 * E1 and E2 can be merged into one equation as shown below.
1718 * Note that 'link-speed' is in Mbps.
1719 *
1720 * value = BW * 0x7735 * 2 * link-speed
1721 * -------------- (E3)
1722 * 1000
1723 *
1724 * 'BW' is the percentage bandwidth out of full link speed
1725 * which can be found with the following equation. Note that
1726 * idleSlope here is the parameter from this function which
1727 * is in kbps.
1728 *
1729 * BW = idleSlope
1730 * ----------------- (E4)
1731 * link-speed * 1000
1732 *
1733 * That said, we can come up with a generic equation to
1734 * calculate the value we should set it TQAVCC register by
1735 * replacing 'BW' in E3 by E4. The resulting equation is:
1736 *
1737 * value = idleSlope * 0x7735 * 2 * link-speed
1738 * ----------------- -------------- (E5)
1739 * link-speed * 1000 1000
1740 *
1741 * 'link-speed' is present in both sides of the fraction so
1742 * it is canceled out. The final equation is the following:
1743 *
1744 * value = idleSlope * 61034
1745 * ----------------- (E6)
1746 * 1000000
Jesus Sanchez-Palencia0da60902017-11-10 14:21:50 -08001747 *
1748 * NOTE: For i210, given the above, we can see that idleslope
1749 * is represented in 16.38431 kbps units by the value at
1750 * the TQAVCC register (1Gbps / 61034), which reduces
1751 * the granularity for idleslope increments.
1752 * For instance, if you want to configure a 2576kbps
1753 * idleslope, the value to be written on the register
1754 * would have to be 157.23. If rounded down, you end
1755 * up with less bandwidth available than originally
1756 * required (~2572 kbps). If rounded up, you end up
1757 * with a higher bandwidth (~2589 kbps). Below the
1758 * approach we take is to always round up the
1759 * calculated value, so the resulting bandwidth might
1760 * be slightly higher for some configurations.
Andre Guedes05f9d3e2017-10-16 18:01:28 -07001761 */
1762 value = DIV_ROUND_UP_ULL(idleslope * 61034ULL, 1000000);
1763
1764 tqavcc = rd32(E1000_I210_TQAVCC(queue));
1765 tqavcc &= ~E1000_TQAVCC_IDLESLOPE_MASK;
1766 tqavcc |= value;
1767 wr32(E1000_I210_TQAVCC(queue), tqavcc);
1768
1769 wr32(E1000_I210_TQAVHC(queue), 0x80000000 + hicredit * 0x7735);
1770 } else {
1771 set_tx_desc_fetch_prio(hw, queue, TX_QUEUE_PRIO_LOW);
1772 set_queue_mode(hw, queue, QUEUE_MODE_STRICT_PRIORITY);
1773
1774 /* Set idleSlope to zero. */
1775 tqavcc = rd32(E1000_I210_TQAVCC(queue));
1776 tqavcc &= ~E1000_TQAVCC_IDLESLOPE_MASK;
1777 wr32(E1000_I210_TQAVCC(queue), tqavcc);
1778
1779 /* Set hiCredit to zero. */
1780 wr32(E1000_I210_TQAVHC(queue), 0);
1781 }
1782
1783 /* XXX: In i210 controller the sendSlope and loCredit parameters from
1784 * CBS are not configurable by software so we don't do any 'controller
1785 * configuration' in respect to these parameters.
1786 */
1787
1788 netdev_dbg(netdev, "CBS %s: queue %d idleslope %d sendslope %d hiCredit %d locredit %d\n",
1789 (enable) ? "enabled" : "disabled", queue,
1790 idleslope, sendslope, hicredit, locredit);
1791}
1792
1793static int igb_save_cbs_params(struct igb_adapter *adapter, int queue,
1794 bool enable, int idleslope, int sendslope,
1795 int hicredit, int locredit)
1796{
1797 struct igb_ring *ring;
1798
1799 if (queue < 0 || queue > adapter->num_tx_queues)
1800 return -EINVAL;
1801
1802 ring = adapter->tx_ring[queue];
1803
1804 ring->cbs_enable = enable;
1805 ring->idleslope = idleslope;
1806 ring->sendslope = sendslope;
1807 ring->hicredit = hicredit;
1808 ring->locredit = locredit;
1809
1810 return 0;
1811}
1812
1813static bool is_any_cbs_enabled(struct igb_adapter *adapter)
1814{
1815 struct igb_ring *ring;
1816 int i;
1817
1818 for (i = 0; i < adapter->num_tx_queues; i++) {
1819 ring = adapter->tx_ring[i];
1820
1821 if (ring->cbs_enable)
1822 return true;
1823 }
1824
1825 return false;
1826}
1827
1828static void igb_setup_tx_mode(struct igb_adapter *adapter)
1829{
1830 struct net_device *netdev = adapter->netdev;
1831 struct e1000_hw *hw = &adapter->hw;
1832 u32 val;
1833
1834 /* Only i210 controller supports changing the transmission mode. */
1835 if (hw->mac.type != e1000_i210)
1836 return;
1837
1838 if (is_fqtss_enabled(adapter)) {
1839 int i, max_queue;
1840
1841 /* Configure TQAVCTRL register: set transmit mode to 'Qav',
1842 * set data fetch arbitration to 'round robin' and set data
1843 * transfer arbitration to 'credit shaper algorithm.
1844 */
1845 val = rd32(E1000_I210_TQAVCTRL);
1846 val |= E1000_TQAVCTRL_XMIT_MODE | E1000_TQAVCTRL_DATATRANARB;
1847 val &= ~E1000_TQAVCTRL_DATAFETCHARB;
1848 wr32(E1000_I210_TQAVCTRL, val);
1849
1850 /* Configure Tx and Rx packet buffers sizes as described in
1851 * i210 datasheet section 7.2.7.7.
1852 */
1853 val = rd32(E1000_TXPBS);
1854 val &= ~I210_TXPBSIZE_MASK;
1855 val |= I210_TXPBSIZE_PB0_8KB | I210_TXPBSIZE_PB1_8KB |
1856 I210_TXPBSIZE_PB2_4KB | I210_TXPBSIZE_PB3_4KB;
1857 wr32(E1000_TXPBS, val);
1858
1859 val = rd32(E1000_RXPBS);
1860 val &= ~I210_RXPBSIZE_MASK;
1861 val |= I210_RXPBSIZE_PB_32KB;
1862 wr32(E1000_RXPBS, val);
1863
1864 /* Section 8.12.9 states that MAX_TPKT_SIZE from DTXMXPKTSZ
1865 * register should not exceed the buffer size programmed in
1866 * TXPBS. The smallest buffer size programmed in TXPBS is 4kB
1867 * so according to the datasheet we should set MAX_TPKT_SIZE to
1868 * 4kB / 64.
1869 *
1870 * However, when we do so, no frame from queue 2 and 3 are
1871 * transmitted. It seems the MAX_TPKT_SIZE should not be great
1872 * or _equal_ to the buffer size programmed in TXPBS. For this
1873 * reason, we set set MAX_ TPKT_SIZE to (4kB - 1) / 64.
1874 */
1875 val = (4096 - 1) / 64;
1876 wr32(E1000_I210_DTXMXPKTSZ, val);
1877
1878 /* Since FQTSS mode is enabled, apply any CBS configuration
1879 * previously set. If no previous CBS configuration has been
1880 * done, then the initial configuration is applied, which means
1881 * CBS is disabled.
1882 */
1883 max_queue = (adapter->num_tx_queues < I210_SR_QUEUES_NUM) ?
1884 adapter->num_tx_queues : I210_SR_QUEUES_NUM;
1885
1886 for (i = 0; i < max_queue; i++) {
1887 struct igb_ring *ring = adapter->tx_ring[i];
1888
1889 igb_configure_cbs(adapter, i, ring->cbs_enable,
1890 ring->idleslope, ring->sendslope,
1891 ring->hicredit, ring->locredit);
1892 }
1893 } else {
1894 wr32(E1000_RXPBS, I210_RXPBSIZE_DEFAULT);
1895 wr32(E1000_TXPBS, I210_TXPBSIZE_DEFAULT);
1896 wr32(E1000_I210_DTXMXPKTSZ, I210_DTXMXPKTSZ_DEFAULT);
1897
1898 val = rd32(E1000_I210_TQAVCTRL);
1899 /* According to Section 8.12.21, the other flags we've set when
1900 * enabling FQTSS are not relevant when disabling FQTSS so we
1901 * don't set they here.
1902 */
1903 val &= ~E1000_TQAVCTRL_XMIT_MODE;
1904 wr32(E1000_I210_TQAVCTRL, val);
1905 }
1906
1907 netdev_dbg(netdev, "FQTSS %s\n", (is_fqtss_enabled(adapter)) ?
1908 "enabled" : "disabled");
1909}
1910
Auke Kok9d5c8242008-01-24 02:22:38 -08001911/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00001912 * igb_configure - configure the hardware for RX and TX
1913 * @adapter: private board structure
Auke Kok9d5c8242008-01-24 02:22:38 -08001914 **/
1915static void igb_configure(struct igb_adapter *adapter)
1916{
1917 struct net_device *netdev = adapter->netdev;
1918 int i;
1919
1920 igb_get_hw_control(adapter);
Alexander Duyckff41f8d2009-09-03 14:48:56 +00001921 igb_set_rx_mode(netdev);
Andre Guedes05f9d3e2017-10-16 18:01:28 -07001922 igb_setup_tx_mode(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001923
1924 igb_restore_vlan(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001925
Alexander Duyck85b430b2009-10-27 15:50:29 +00001926 igb_setup_tctl(adapter);
Alexander Duyck06cf2662009-10-27 15:53:25 +00001927 igb_setup_mrqc(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001928 igb_setup_rctl(adapter);
Alexander Duyck85b430b2009-10-27 15:50:29 +00001929
Gangfeng Huang0e71def2016-07-06 13:22:54 +08001930 igb_nfc_filter_restore(adapter);
Alexander Duyck85b430b2009-10-27 15:50:29 +00001931 igb_configure_tx(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001932 igb_configure_rx(adapter);
Alexander Duyck662d7202008-06-27 11:00:29 -07001933
1934 igb_rx_fifo_flush_82575(&adapter->hw);
1935
Alexander Duyckc493ea42009-03-20 00:16:50 +00001936 /* call igb_desc_unused which always leaves
Auke Kok9d5c8242008-01-24 02:22:38 -08001937 * at least 1 descriptor unused to make sure
Jeff Kirsherb980ac12013-02-23 07:29:56 +00001938 * next_to_use != next_to_clean
1939 */
Auke Kok9d5c8242008-01-24 02:22:38 -08001940 for (i = 0; i < adapter->num_rx_queues; i++) {
Alexander Duyck3025a442010-02-17 01:02:39 +00001941 struct igb_ring *ring = adapter->rx_ring[i];
Alexander Duyckcd392f52011-08-26 07:43:59 +00001942 igb_alloc_rx_buffers(ring, igb_desc_unused(ring));
Auke Kok9d5c8242008-01-24 02:22:38 -08001943 }
Auke Kok9d5c8242008-01-24 02:22:38 -08001944}
1945
Nick Nunley88a268c2010-02-17 01:01:59 +00001946/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00001947 * igb_power_up_link - Power up the phy/serdes link
1948 * @adapter: address of board private structure
Nick Nunley88a268c2010-02-17 01:01:59 +00001949 **/
1950void igb_power_up_link(struct igb_adapter *adapter)
1951{
Akeem G. Abodunrin76886592012-07-17 04:51:18 +00001952 igb_reset_phy(&adapter->hw);
1953
Nick Nunley88a268c2010-02-17 01:01:59 +00001954 if (adapter->hw.phy.media_type == e1000_media_type_copper)
1955 igb_power_up_phy_copper(&adapter->hw);
1956 else
1957 igb_power_up_serdes_link_82575(&adapter->hw);
Todd Fujinakaaec653c2014-06-17 06:58:11 +00001958
1959 igb_setup_link(&adapter->hw);
Nick Nunley88a268c2010-02-17 01:01:59 +00001960}
1961
1962/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00001963 * igb_power_down_link - Power down the phy/serdes link
1964 * @adapter: address of board private structure
Nick Nunley88a268c2010-02-17 01:01:59 +00001965 */
1966static void igb_power_down_link(struct igb_adapter *adapter)
1967{
1968 if (adapter->hw.phy.media_type == e1000_media_type_copper)
1969 igb_power_down_phy_copper_82575(&adapter->hw);
1970 else
1971 igb_shutdown_serdes_link_82575(&adapter->hw);
1972}
Auke Kok9d5c8242008-01-24 02:22:38 -08001973
1974/**
Carolyn Wyborny56cec242013-10-17 05:36:26 +00001975 * Detect and switch function for Media Auto Sense
1976 * @adapter: address of the board private structure
1977 **/
1978static void igb_check_swap_media(struct igb_adapter *adapter)
1979{
1980 struct e1000_hw *hw = &adapter->hw;
1981 u32 ctrl_ext, connsw;
1982 bool swap_now = false;
1983
1984 ctrl_ext = rd32(E1000_CTRL_EXT);
1985 connsw = rd32(E1000_CONNSW);
1986
1987 /* need to live swap if current media is copper and we have fiber/serdes
1988 * to go to.
1989 */
1990
1991 if ((hw->phy.media_type == e1000_media_type_copper) &&
1992 (!(connsw & E1000_CONNSW_AUTOSENSE_EN))) {
1993 swap_now = true;
1994 } else if (!(connsw & E1000_CONNSW_SERDESD)) {
1995 /* copper signal takes time to appear */
1996 if (adapter->copper_tries < 4) {
1997 adapter->copper_tries++;
1998 connsw |= E1000_CONNSW_AUTOSENSE_CONF;
1999 wr32(E1000_CONNSW, connsw);
2000 return;
2001 } else {
2002 adapter->copper_tries = 0;
2003 if ((connsw & E1000_CONNSW_PHYSD) &&
2004 (!(connsw & E1000_CONNSW_PHY_PDN))) {
2005 swap_now = true;
2006 connsw &= ~E1000_CONNSW_AUTOSENSE_CONF;
2007 wr32(E1000_CONNSW, connsw);
2008 }
2009 }
2010 }
2011
2012 if (!swap_now)
2013 return;
2014
2015 switch (hw->phy.media_type) {
2016 case e1000_media_type_copper:
2017 netdev_info(adapter->netdev,
2018 "MAS: changing media to fiber/serdes\n");
2019 ctrl_ext |=
2020 E1000_CTRL_EXT_LINK_MODE_PCIE_SERDES;
2021 adapter->flags |= IGB_FLAG_MEDIA_RESET;
2022 adapter->copper_tries = 0;
2023 break;
2024 case e1000_media_type_internal_serdes:
2025 case e1000_media_type_fiber:
2026 netdev_info(adapter->netdev,
2027 "MAS: changing media to copper\n");
2028 ctrl_ext &=
2029 ~E1000_CTRL_EXT_LINK_MODE_PCIE_SERDES;
2030 adapter->flags |= IGB_FLAG_MEDIA_RESET;
2031 break;
2032 default:
2033 /* shouldn't get here during regular operation */
2034 netdev_err(adapter->netdev,
2035 "AMS: Invalid media type found, returning\n");
2036 break;
2037 }
2038 wr32(E1000_CTRL_EXT, ctrl_ext);
2039}
2040
2041/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00002042 * igb_up - Open the interface and prepare it to handle traffic
2043 * @adapter: board private structure
Auke Kok9d5c8242008-01-24 02:22:38 -08002044 **/
Auke Kok9d5c8242008-01-24 02:22:38 -08002045int igb_up(struct igb_adapter *adapter)
2046{
2047 struct e1000_hw *hw = &adapter->hw;
2048 int i;
2049
2050 /* hardware has been reset, we need to reload some things */
2051 igb_configure(adapter);
2052
2053 clear_bit(__IGB_DOWN, &adapter->state);
2054
Alexander Duyck0d1ae7f2011-08-26 07:46:34 +00002055 for (i = 0; i < adapter->num_q_vectors; i++)
2056 napi_enable(&(adapter->q_vector[i]->napi));
2057
Carolyn Wybornycd14ef52013-12-10 07:58:34 +00002058 if (adapter->flags & IGB_FLAG_HAS_MSIX)
Auke Kok9d5c8242008-01-24 02:22:38 -08002059 igb_configure_msix(adapter);
Alexander Duyckfeeb2722010-02-03 21:59:51 +00002060 else
2061 igb_assign_vector(adapter->q_vector[0], 0);
Auke Kok9d5c8242008-01-24 02:22:38 -08002062
2063 /* Clear any pending interrupts. */
2064 rd32(E1000_ICR);
2065 igb_irq_enable(adapter);
2066
Alexander Duyckd4960302009-10-27 15:53:45 +00002067 /* notify VFs that reset has been completed */
2068 if (adapter->vfs_allocated_count) {
2069 u32 reg_data = rd32(E1000_CTRL_EXT);
Carolyn Wyborny9005df32014-04-11 01:45:34 +00002070
Alexander Duyckd4960302009-10-27 15:53:45 +00002071 reg_data |= E1000_CTRL_EXT_PFRSTD;
2072 wr32(E1000_CTRL_EXT, reg_data);
2073 }
2074
Jesse Brandeburg4cb9be72009-04-21 18:42:05 +00002075 netif_tx_start_all_queues(adapter->netdev);
2076
Alexander Duyck25568a52009-10-27 23:49:59 +00002077 /* start the watchdog. */
2078 hw->mac.get_link_status = 1;
2079 schedule_work(&adapter->watchdog_task);
2080
Carolyn Wybornyf4c01e92014-03-12 03:58:22 +00002081 if ((adapter->flags & IGB_FLAG_EEE) &&
2082 (!hw->dev_spec._82575.eee_disable))
2083 adapter->eee_advert = MDIO_EEE_100TX | MDIO_EEE_1000T;
2084
Auke Kok9d5c8242008-01-24 02:22:38 -08002085 return 0;
2086}
2087
2088void igb_down(struct igb_adapter *adapter)
2089{
Auke Kok9d5c8242008-01-24 02:22:38 -08002090 struct net_device *netdev = adapter->netdev;
Alexander Duyck330a6d62009-10-27 23:51:35 +00002091 struct e1000_hw *hw = &adapter->hw;
Auke Kok9d5c8242008-01-24 02:22:38 -08002092 u32 tctl, rctl;
2093 int i;
2094
2095 /* signal that we're down so the interrupt handler does not
Jeff Kirsherb980ac12013-02-23 07:29:56 +00002096 * reschedule our watchdog timer
2097 */
Auke Kok9d5c8242008-01-24 02:22:38 -08002098 set_bit(__IGB_DOWN, &adapter->state);
2099
2100 /* disable receives in the hardware */
2101 rctl = rd32(E1000_RCTL);
2102 wr32(E1000_RCTL, rctl & ~E1000_RCTL_EN);
2103 /* flush and sleep below */
2104
Gangfeng Huang94221ae752017-05-27 09:17:53 +08002105 igb_nfc_filter_exit(adapter);
2106
Todd Fujinakaf28ea082015-03-20 17:41:53 -07002107 netif_carrier_off(netdev);
David S. Millerfd2ea0a2008-07-17 01:56:23 -07002108 netif_tx_stop_all_queues(netdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08002109
2110 /* disable transmits in the hardware */
2111 tctl = rd32(E1000_TCTL);
2112 tctl &= ~E1000_TCTL_EN;
2113 wr32(E1000_TCTL, tctl);
2114 /* flush both disables and wait for them to finish */
2115 wrfl();
Carolyn Wyborny0d451e72014-04-11 01:46:40 +00002116 usleep_range(10000, 11000);
Auke Kok9d5c8242008-01-24 02:22:38 -08002117
Auke Kok9d5c8242008-01-24 02:22:38 -08002118 igb_irq_disable(adapter);
2119
Akeem G Abodunrinaa9b8cc2013-08-28 02:22:43 +00002120 adapter->flags &= ~IGB_FLAG_NEED_LINK_UPDATE;
2121
Carolyn Wyborny41f149a2013-04-30 00:21:32 +00002122 for (i = 0; i < adapter->num_q_vectors; i++) {
Carolyn Wyborny17a402a2014-11-21 23:52:54 -08002123 if (adapter->q_vector[i]) {
2124 napi_synchronize(&adapter->q_vector[i]->napi);
2125 napi_disable(&adapter->q_vector[i]->napi);
2126 }
Carolyn Wyborny41f149a2013-04-30 00:21:32 +00002127 }
2128
Auke Kok9d5c8242008-01-24 02:22:38 -08002129 del_timer_sync(&adapter->watchdog_timer);
2130 del_timer_sync(&adapter->phy_info_timer);
2131
Alexander Duyck04fe6352009-02-06 23:22:32 +00002132 /* record the stats before reset*/
Eric Dumazet12dcd862010-10-15 17:27:10 +00002133 spin_lock(&adapter->stats64_lock);
Benjamin Poirier81e3f642017-05-16 15:55:16 -07002134 igb_update_stats(adapter);
Eric Dumazet12dcd862010-10-15 17:27:10 +00002135 spin_unlock(&adapter->stats64_lock);
Alexander Duyck04fe6352009-02-06 23:22:32 +00002136
Auke Kok9d5c8242008-01-24 02:22:38 -08002137 adapter->link_speed = 0;
2138 adapter->link_duplex = 0;
2139
Jeff Kirsher30236822008-06-24 17:01:15 -07002140 if (!pci_channel_offline(adapter->pdev))
2141 igb_reset(adapter);
Alexander Duyck16903ca2016-01-06 23:11:18 -08002142
2143 /* clear VLAN promisc flag so VFTA will be updated if necessary */
2144 adapter->flags &= ~IGB_FLAG_VLAN_PROMISC;
2145
Auke Kok9d5c8242008-01-24 02:22:38 -08002146 igb_clean_all_tx_rings(adapter);
2147 igb_clean_all_rx_rings(adapter);
Alexander Duyck7e0e99e2009-05-21 13:06:56 +00002148#ifdef CONFIG_IGB_DCA
2149
2150 /* since we reset the hardware DCA settings were cleared */
2151 igb_setup_dca(adapter);
2152#endif
Auke Kok9d5c8242008-01-24 02:22:38 -08002153}
2154
2155void igb_reinit_locked(struct igb_adapter *adapter)
2156{
2157 WARN_ON(in_interrupt());
2158 while (test_and_set_bit(__IGB_RESETTING, &adapter->state))
Carolyn Wyborny0d451e72014-04-11 01:46:40 +00002159 usleep_range(1000, 2000);
Auke Kok9d5c8242008-01-24 02:22:38 -08002160 igb_down(adapter);
2161 igb_up(adapter);
2162 clear_bit(__IGB_RESETTING, &adapter->state);
2163}
2164
Carolyn Wyborny56cec242013-10-17 05:36:26 +00002165/** igb_enable_mas - Media Autosense re-enable after swap
2166 *
2167 * @adapter: adapter struct
2168 **/
Todd Fujinaka8cfb8792015-05-02 00:39:03 -07002169static void igb_enable_mas(struct igb_adapter *adapter)
Carolyn Wyborny56cec242013-10-17 05:36:26 +00002170{
2171 struct e1000_hw *hw = &adapter->hw;
Todd Fujinaka8cfb8792015-05-02 00:39:03 -07002172 u32 connsw = rd32(E1000_CONNSW);
Carolyn Wyborny56cec242013-10-17 05:36:26 +00002173
2174 /* configure for SerDes media detect */
Todd Fujinaka8cfb8792015-05-02 00:39:03 -07002175 if ((hw->phy.media_type == e1000_media_type_copper) &&
2176 (!(connsw & E1000_CONNSW_SERDESD))) {
Carolyn Wyborny56cec242013-10-17 05:36:26 +00002177 connsw |= E1000_CONNSW_ENRGSRC;
2178 connsw |= E1000_CONNSW_AUTOSENSE_EN;
2179 wr32(E1000_CONNSW, connsw);
2180 wrfl();
Carolyn Wyborny56cec242013-10-17 05:36:26 +00002181 }
Carolyn Wyborny56cec242013-10-17 05:36:26 +00002182}
2183
Auke Kok9d5c8242008-01-24 02:22:38 -08002184void igb_reset(struct igb_adapter *adapter)
2185{
Alexander Duyck090b1792009-10-27 23:51:55 +00002186 struct pci_dev *pdev = adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08002187 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck2d064c02008-07-08 15:10:12 -07002188 struct e1000_mac_info *mac = &hw->mac;
2189 struct e1000_fc_info *fc = &hw->fc;
Alexander Duyck45693bc2016-01-06 23:10:39 -08002190 u32 pba, hwm;
Auke Kok9d5c8242008-01-24 02:22:38 -08002191
2192 /* Repartition Pba for greater than 9k mtu
2193 * To take effect CTRL.RST is required.
2194 */
Alexander Duyckfa4dfae2009-02-06 23:21:31 +00002195 switch (mac->type) {
Alexander Duyckd2ba2ed2010-03-22 14:08:06 +00002196 case e1000_i350:
Carolyn Wybornyceb5f132013-04-18 22:21:30 +00002197 case e1000_i354:
Alexander Duyck55cac242009-11-19 12:42:21 +00002198 case e1000_82580:
2199 pba = rd32(E1000_RXPBS);
2200 pba = igb_rxpbs_adjust_82580(pba);
2201 break;
Alexander Duyckfa4dfae2009-02-06 23:21:31 +00002202 case e1000_82576:
Alexander Duyckd249be52009-10-27 23:46:38 +00002203 pba = rd32(E1000_RXPBS);
2204 pba &= E1000_RXPBS_SIZE_MASK_82576;
Alexander Duyckfa4dfae2009-02-06 23:21:31 +00002205 break;
2206 case e1000_82575:
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002207 case e1000_i210:
2208 case e1000_i211:
Alexander Duyckfa4dfae2009-02-06 23:21:31 +00002209 default:
2210 pba = E1000_PBA_34K;
2211 break;
Alexander Duyck2d064c02008-07-08 15:10:12 -07002212 }
Auke Kok9d5c8242008-01-24 02:22:38 -08002213
Alexander Duyck45693bc2016-01-06 23:10:39 -08002214 if (mac->type == e1000_82575) {
2215 u32 min_rx_space, min_tx_space, needed_tx_space;
2216
2217 /* write Rx PBA so that hardware can report correct Tx PBA */
Auke Kok9d5c8242008-01-24 02:22:38 -08002218 wr32(E1000_PBA, pba);
2219
2220 /* To maintain wire speed transmits, the Tx FIFO should be
2221 * large enough to accommodate two full transmit packets,
2222 * rounded up to the next 1KB and expressed in KB. Likewise,
2223 * the Rx FIFO should be large enough to accommodate at least
2224 * one full receive packet and is similarly rounded up and
Jeff Kirsherb980ac12013-02-23 07:29:56 +00002225 * expressed in KB.
2226 */
Alexander Duyck45693bc2016-01-06 23:10:39 -08002227 min_rx_space = DIV_ROUND_UP(MAX_JUMBO_FRAME_SIZE, 1024);
2228
2229 /* The Tx FIFO also stores 16 bytes of information about the Tx
2230 * but don't include Ethernet FCS because hardware appends it.
2231 * We only need to round down to the nearest 512 byte block
2232 * count since the value we care about is 2 frames, not 1.
Jeff Kirsherb980ac12013-02-23 07:29:56 +00002233 */
Alexander Duyck45693bc2016-01-06 23:10:39 -08002234 min_tx_space = adapter->max_frame_size;
2235 min_tx_space += sizeof(union e1000_adv_tx_desc) - ETH_FCS_LEN;
2236 min_tx_space = DIV_ROUND_UP(min_tx_space, 512);
2237
2238 /* upper 16 bits has Tx packet buffer allocation size in KB */
2239 needed_tx_space = min_tx_space - (rd32(E1000_PBA) >> 16);
Auke Kok9d5c8242008-01-24 02:22:38 -08002240
2241 /* If current Tx allocation is less than the min Tx FIFO size,
2242 * and the min Tx FIFO size is less than the current Rx FIFO
Alexander Duyck45693bc2016-01-06 23:10:39 -08002243 * allocation, take space away from current Rx allocation.
Jeff Kirsherb980ac12013-02-23 07:29:56 +00002244 */
Alexander Duyck45693bc2016-01-06 23:10:39 -08002245 if (needed_tx_space < pba) {
2246 pba -= needed_tx_space;
Auke Kok9d5c8242008-01-24 02:22:38 -08002247
Jeff Kirsherb980ac12013-02-23 07:29:56 +00002248 /* if short on Rx space, Rx wins and must trump Tx
2249 * adjustment
2250 */
Auke Kok9d5c8242008-01-24 02:22:38 -08002251 if (pba < min_rx_space)
2252 pba = min_rx_space;
2253 }
Alexander Duyck45693bc2016-01-06 23:10:39 -08002254
2255 /* adjust PBA for jumbo frames */
Alexander Duyck2d064c02008-07-08 15:10:12 -07002256 wr32(E1000_PBA, pba);
Auke Kok9d5c8242008-01-24 02:22:38 -08002257 }
Auke Kok9d5c8242008-01-24 02:22:38 -08002258
Alexander Duyck45693bc2016-01-06 23:10:39 -08002259 /* flow control settings
2260 * The high water mark must be low enough to fit one full frame
2261 * after transmitting the pause frame. As such we must have enough
2262 * space to allow for us to complete our current transmit and then
2263 * receive the frame that is in progress from the link partner.
2264 * Set it to:
2265 * - the full Rx FIFO size minus one full Tx plus one full Rx frame
Jeff Kirsherb980ac12013-02-23 07:29:56 +00002266 */
Alexander Duyck45693bc2016-01-06 23:10:39 -08002267 hwm = (pba << 10) - (adapter->max_frame_size + MAX_JUMBO_FRAME_SIZE);
Auke Kok9d5c8242008-01-24 02:22:38 -08002268
Matthew Vickd48507f2012-11-08 04:03:58 +00002269 fc->high_water = hwm & 0xFFFFFFF0; /* 16-byte granularity */
Alexander Duyckd405ea32009-12-23 13:21:27 +00002270 fc->low_water = fc->high_water - 16;
Auke Kok9d5c8242008-01-24 02:22:38 -08002271 fc->pause_time = 0xFFFF;
2272 fc->send_xon = 1;
Alexander Duyck0cce1192009-07-23 18:10:24 +00002273 fc->current_mode = fc->requested_mode;
Auke Kok9d5c8242008-01-24 02:22:38 -08002274
Alexander Duyck4ae196d2009-02-19 20:40:07 -08002275 /* disable receive for all VFs and wait one second */
2276 if (adapter->vfs_allocated_count) {
2277 int i;
Carolyn Wyborny9005df32014-04-11 01:45:34 +00002278
Alexander Duyck4ae196d2009-02-19 20:40:07 -08002279 for (i = 0 ; i < adapter->vfs_allocated_count; i++)
Greg Rose8fa7e0f2010-11-06 05:43:21 +00002280 adapter->vf_data[i].flags &= IGB_VF_FLAG_PF_SET_MAC;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08002281
2282 /* ping all the active vfs to let them know we are going down */
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00002283 igb_ping_all_vfs(adapter);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08002284
2285 /* disable transmits and receives */
2286 wr32(E1000_VFRE, 0);
2287 wr32(E1000_VFTE, 0);
2288 }
2289
Auke Kok9d5c8242008-01-24 02:22:38 -08002290 /* Allow time for pending master requests to run */
Alexander Duyck330a6d62009-10-27 23:51:35 +00002291 hw->mac.ops.reset_hw(hw);
Auke Kok9d5c8242008-01-24 02:22:38 -08002292 wr32(E1000_WUC, 0);
2293
Carolyn Wyborny56cec242013-10-17 05:36:26 +00002294 if (adapter->flags & IGB_FLAG_MEDIA_RESET) {
2295 /* need to resetup here after media swap */
2296 adapter->ei.get_invariants(hw);
2297 adapter->flags &= ~IGB_FLAG_MEDIA_RESET;
2298 }
Todd Fujinaka8cfb8792015-05-02 00:39:03 -07002299 if ((mac->type == e1000_82575) &&
2300 (adapter->flags & IGB_FLAG_MAS_ENABLE)) {
2301 igb_enable_mas(adapter);
Carolyn Wyborny56cec242013-10-17 05:36:26 +00002302 }
Alexander Duyck330a6d62009-10-27 23:51:35 +00002303 if (hw->mac.ops.init_hw(hw))
Alexander Duyck090b1792009-10-27 23:51:55 +00002304 dev_err(&pdev->dev, "Hardware Error\n");
Auke Kok9d5c8242008-01-24 02:22:38 -08002305
Yury Kylulin83c21332017-03-07 11:20:25 +03002306 /* RAR registers were cleared during init_hw, clear mac table */
2307 igb_flush_mac_table(adapter);
2308 __dev_uc_unsync(adapter->netdev, NULL);
2309
2310 /* Recover default RAR entry */
2311 igb_set_default_mac_filter(adapter);
2312
Jeff Kirsherb980ac12013-02-23 07:29:56 +00002313 /* Flow control settings reset on hardware reset, so guarantee flow
Matthew Vicka27416b2012-04-18 02:57:44 +00002314 * control is off when forcing speed.
2315 */
2316 if (!hw->mac.autoneg)
2317 igb_force_mac_fc(hw);
2318
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +00002319 igb_init_dmac(adapter, pba);
Carolyn Wybornye4288932012-12-07 03:01:42 +00002320#ifdef CONFIG_IGB_HWMON
2321 /* Re-initialize the thermal sensor on i350 devices. */
2322 if (!test_bit(__IGB_DOWN, &adapter->state)) {
2323 if (mac->type == e1000_i350 && hw->bus.func == 0) {
2324 /* If present, re-initialize the external thermal sensor
2325 * interface.
2326 */
2327 if (adapter->ets)
2328 mac->ops.init_thermal_sensor_thresh(hw);
2329 }
2330 }
2331#endif
Jeff Kirsherb9361362014-03-13 16:07:14 -07002332 /* Re-establish EEE setting */
Carolyn Wybornyf4c01e92014-03-12 03:58:22 +00002333 if (hw->phy.media_type == e1000_media_type_copper) {
2334 switch (mac->type) {
2335 case e1000_i350:
2336 case e1000_i210:
2337 case e1000_i211:
Todd Fujinakac4c112f2014-08-29 06:43:13 +00002338 igb_set_eee_i350(hw, true, true);
Carolyn Wybornyf4c01e92014-03-12 03:58:22 +00002339 break;
2340 case e1000_i354:
Todd Fujinakac4c112f2014-08-29 06:43:13 +00002341 igb_set_eee_i354(hw, true, true);
Carolyn Wybornyf4c01e92014-03-12 03:58:22 +00002342 break;
2343 default:
2344 break;
2345 }
2346 }
Nick Nunley88a268c2010-02-17 01:01:59 +00002347 if (!netif_running(adapter->netdev))
2348 igb_power_down_link(adapter);
2349
Auke Kok9d5c8242008-01-24 02:22:38 -08002350 igb_update_mng_vlan(adapter);
2351
2352 /* Enable h/w to recognize an 802.1Q VLAN Ethernet packet */
2353 wr32(E1000_VET, ETHERNET_IEEE_VLAN_TYPE);
2354
Matthew Vick1f6e8172012-08-18 07:26:33 +00002355 /* Re-enable PTP, where applicable. */
Jacob Keller4f3ce712016-05-24 13:56:29 -07002356 if (adapter->ptp_flags & IGB_PTP_ENABLED)
2357 igb_ptp_reset(adapter);
Matthew Vick1f6e8172012-08-18 07:26:33 +00002358
Alexander Duyck330a6d62009-10-27 23:51:35 +00002359 igb_get_phy_info(hw);
Auke Kok9d5c8242008-01-24 02:22:38 -08002360}
2361
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002362static netdev_features_t igb_fix_features(struct net_device *netdev,
2363 netdev_features_t features)
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00002364{
Jeff Kirsherb980ac12013-02-23 07:29:56 +00002365 /* Since there is no support for separate Rx/Tx vlan accel
2366 * enable/disable make sure Tx flag is always in same state as Rx.
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00002367 */
Patrick McHardyf6469682013-04-19 02:04:27 +00002368 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2369 features |= NETIF_F_HW_VLAN_CTAG_TX;
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00002370 else
Patrick McHardyf6469682013-04-19 02:04:27 +00002371 features &= ~NETIF_F_HW_VLAN_CTAG_TX;
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00002372
2373 return features;
2374}
2375
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002376static int igb_set_features(struct net_device *netdev,
2377 netdev_features_t features)
Michał Mirosławac52caa2011-06-08 08:38:01 +00002378{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002379 netdev_features_t changed = netdev->features ^ features;
Ben Greear89eaefb2012-03-06 09:41:58 +00002380 struct igb_adapter *adapter = netdev_priv(netdev);
Michał Mirosławac52caa2011-06-08 08:38:01 +00002381
Patrick McHardyf6469682013-04-19 02:04:27 +00002382 if (changed & NETIF_F_HW_VLAN_CTAG_RX)
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00002383 igb_vlan_mode(netdev, features);
2384
Alexander Duyck16903ca2016-01-06 23:11:18 -08002385 if (!(changed & (NETIF_F_RXALL | NETIF_F_NTUPLE)))
Ben Greear89eaefb2012-03-06 09:41:58 +00002386 return 0;
2387
Gangfeng Huang0e71def2016-07-06 13:22:54 +08002388 if (!(features & NETIF_F_NTUPLE)) {
2389 struct hlist_node *node2;
2390 struct igb_nfc_filter *rule;
2391
2392 spin_lock(&adapter->nfc_lock);
2393 hlist_for_each_entry_safe(rule, node2,
2394 &adapter->nfc_filter_list, nfc_node) {
2395 igb_erase_filter(adapter, rule);
2396 hlist_del(&rule->nfc_node);
2397 kfree(rule);
2398 }
2399 spin_unlock(&adapter->nfc_lock);
2400 adapter->nfc_filter_count = 0;
2401 }
2402
Ben Greear89eaefb2012-03-06 09:41:58 +00002403 netdev->features = features;
2404
2405 if (netif_running(netdev))
2406 igb_reinit_locked(adapter);
2407 else
2408 igb_reset(adapter);
2409
Michał Mirosławac52caa2011-06-08 08:38:01 +00002410 return 0;
2411}
2412
Alexander Duyck268f9d32016-01-06 23:11:34 -08002413static int igb_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
2414 struct net_device *dev,
2415 const unsigned char *addr, u16 vid,
2416 u16 flags)
2417{
2418 /* guarantee we can provide a unique filter for the unicast address */
2419 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) {
2420 struct igb_adapter *adapter = netdev_priv(dev);
Alexander Duyck268f9d32016-01-06 23:11:34 -08002421 int vfn = adapter->vfs_allocated_count;
Alexander Duyck268f9d32016-01-06 23:11:34 -08002422
Yury Kylulin83c21332017-03-07 11:20:25 +03002423 if (netdev_uc_count(dev) >= igb_available_rars(adapter, vfn))
Alexander Duyck268f9d32016-01-06 23:11:34 -08002424 return -ENOMEM;
2425 }
2426
2427 return ndo_dflt_fdb_add(ndm, tb, dev, addr, vid, flags);
2428}
2429
Alexander Duycke10715d2016-04-14 17:19:38 -04002430#define IGB_MAX_MAC_HDR_LEN 127
2431#define IGB_MAX_NETWORK_HDR_LEN 511
2432
2433static netdev_features_t
2434igb_features_check(struct sk_buff *skb, struct net_device *dev,
2435 netdev_features_t features)
2436{
2437 unsigned int network_hdr_len, mac_hdr_len;
2438
2439 /* Make certain the headers can be described by a context descriptor */
2440 mac_hdr_len = skb_network_header(skb) - skb->data;
2441 if (unlikely(mac_hdr_len > IGB_MAX_MAC_HDR_LEN))
2442 return features & ~(NETIF_F_HW_CSUM |
2443 NETIF_F_SCTP_CRC |
2444 NETIF_F_HW_VLAN_CTAG_TX |
2445 NETIF_F_TSO |
2446 NETIF_F_TSO6);
2447
2448 network_hdr_len = skb_checksum_start(skb) - skb_network_header(skb);
2449 if (unlikely(network_hdr_len > IGB_MAX_NETWORK_HDR_LEN))
2450 return features & ~(NETIF_F_HW_CSUM |
2451 NETIF_F_SCTP_CRC |
2452 NETIF_F_TSO |
2453 NETIF_F_TSO6);
2454
2455 /* We can only support IPV4 TSO in tunnels if we can mangle the
2456 * inner IP ID field, so strip TSO if MANGLEID is not supported.
2457 */
2458 if (skb->encapsulation && !(features & NETIF_F_TSO_MANGLEID))
2459 features &= ~NETIF_F_TSO;
2460
2461 return features;
2462}
2463
Andre Guedes05f9d3e2017-10-16 18:01:28 -07002464static int igb_offload_cbs(struct igb_adapter *adapter,
2465 struct tc_cbs_qopt_offload *qopt)
2466{
2467 struct e1000_hw *hw = &adapter->hw;
2468 int err;
2469
2470 /* CBS offloading is only supported by i210 controller. */
2471 if (hw->mac.type != e1000_i210)
2472 return -EOPNOTSUPP;
2473
2474 /* CBS offloading is only supported by queue 0 and queue 1. */
2475 if (qopt->queue < 0 || qopt->queue > 1)
2476 return -EINVAL;
2477
2478 err = igb_save_cbs_params(adapter, qopt->queue, qopt->enable,
2479 qopt->idleslope, qopt->sendslope,
2480 qopt->hicredit, qopt->locredit);
2481 if (err)
2482 return err;
2483
2484 if (is_fqtss_enabled(adapter)) {
2485 igb_configure_cbs(adapter, qopt->queue, qopt->enable,
2486 qopt->idleslope, qopt->sendslope,
2487 qopt->hicredit, qopt->locredit);
2488
2489 if (!is_any_cbs_enabled(adapter))
2490 enable_fqtss(adapter, false);
2491
2492 } else {
2493 enable_fqtss(adapter, true);
2494 }
2495
2496 return 0;
2497}
2498
2499static int igb_setup_tc(struct net_device *dev, enum tc_setup_type type,
2500 void *type_data)
2501{
2502 struct igb_adapter *adapter = netdev_priv(dev);
2503
2504 switch (type) {
Nogah Frankel8521db42017-11-06 07:23:43 +01002505 case TC_SETUP_QDISC_CBS:
Andre Guedes05f9d3e2017-10-16 18:01:28 -07002506 return igb_offload_cbs(adapter, type_data);
2507
2508 default:
2509 return -EOPNOTSUPP;
2510 }
2511}
2512
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08002513static const struct net_device_ops igb_netdev_ops = {
Alexander Duyck559e9c42009-10-27 23:52:50 +00002514 .ndo_open = igb_open,
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08002515 .ndo_stop = igb_close,
Alexander Duyckcd392f52011-08-26 07:43:59 +00002516 .ndo_start_xmit = igb_xmit_frame,
Eric Dumazet12dcd862010-10-15 17:27:10 +00002517 .ndo_get_stats64 = igb_get_stats64,
Alexander Duyckff41f8d2009-09-03 14:48:56 +00002518 .ndo_set_rx_mode = igb_set_rx_mode,
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08002519 .ndo_set_mac_address = igb_set_mac,
2520 .ndo_change_mtu = igb_change_mtu,
2521 .ndo_do_ioctl = igb_ioctl,
2522 .ndo_tx_timeout = igb_tx_timeout,
2523 .ndo_validate_addr = eth_validate_addr,
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08002524 .ndo_vlan_rx_add_vid = igb_vlan_rx_add_vid,
2525 .ndo_vlan_rx_kill_vid = igb_vlan_rx_kill_vid,
Williams, Mitch A8151d292010-02-10 01:44:24 +00002526 .ndo_set_vf_mac = igb_ndo_set_vf_mac,
2527 .ndo_set_vf_vlan = igb_ndo_set_vf_vlan,
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04002528 .ndo_set_vf_rate = igb_ndo_set_vf_bw,
Lior Levy70ea4782013-03-03 20:27:48 +00002529 .ndo_set_vf_spoofchk = igb_ndo_set_vf_spoofchk,
Williams, Mitch A8151d292010-02-10 01:44:24 +00002530 .ndo_get_vf_config = igb_ndo_get_vf_config,
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08002531#ifdef CONFIG_NET_POLL_CONTROLLER
2532 .ndo_poll_controller = igb_netpoll,
2533#endif
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00002534 .ndo_fix_features = igb_fix_features,
2535 .ndo_set_features = igb_set_features,
Alexander Duyck268f9d32016-01-06 23:11:34 -08002536 .ndo_fdb_add = igb_ndo_fdb_add,
Alexander Duycke10715d2016-04-14 17:19:38 -04002537 .ndo_features_check = igb_features_check,
Andre Guedes05f9d3e2017-10-16 18:01:28 -07002538 .ndo_setup_tc = igb_setup_tc,
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08002539};
2540
Taku Izumi42bfd33a2008-06-20 12:10:30 +09002541/**
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00002542 * igb_set_fw_version - Configure version string for ethtool
2543 * @adapter: adapter struct
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00002544 **/
2545void igb_set_fw_version(struct igb_adapter *adapter)
2546{
2547 struct e1000_hw *hw = &adapter->hw;
Carolyn Wyborny0b1a6f22012-10-18 07:16:19 +00002548 struct e1000_fw_version fw;
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00002549
Carolyn Wyborny0b1a6f22012-10-18 07:16:19 +00002550 igb_get_fw_version(hw, &fw);
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00002551
Carolyn Wyborny0b1a6f22012-10-18 07:16:19 +00002552 switch (hw->mac.type) {
Carolyn Wyborny7dc98a62013-07-16 19:25:33 +00002553 case e1000_i210:
Carolyn Wyborny0b1a6f22012-10-18 07:16:19 +00002554 case e1000_i211:
Carolyn Wyborny7dc98a62013-07-16 19:25:33 +00002555 if (!(igb_get_flash_presence_i210(hw))) {
2556 snprintf(adapter->fw_version,
2557 sizeof(adapter->fw_version),
2558 "%2d.%2d-%d",
2559 fw.invm_major, fw.invm_minor,
2560 fw.invm_img_type);
2561 break;
2562 }
2563 /* fall through */
Carolyn Wyborny0b1a6f22012-10-18 07:16:19 +00002564 default:
2565 /* if option is rom valid, display its version too */
2566 if (fw.or_valid) {
2567 snprintf(adapter->fw_version,
2568 sizeof(adapter->fw_version),
2569 "%d.%d, 0x%08x, %d.%d.%d",
2570 fw.eep_major, fw.eep_minor, fw.etrack_id,
2571 fw.or_major, fw.or_build, fw.or_patch);
2572 /* no option rom */
Carolyn Wyborny7dc98a62013-07-16 19:25:33 +00002573 } else if (fw.etrack_id != 0X0000) {
Carolyn Wyborny0b1a6f22012-10-18 07:16:19 +00002574 snprintf(adapter->fw_version,
Carolyn Wyborny7dc98a62013-07-16 19:25:33 +00002575 sizeof(adapter->fw_version),
2576 "%d.%d, 0x%08x",
2577 fw.eep_major, fw.eep_minor, fw.etrack_id);
2578 } else {
2579 snprintf(adapter->fw_version,
2580 sizeof(adapter->fw_version),
2581 "%d.%d.%d",
2582 fw.eep_major, fw.eep_minor, fw.eep_build);
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00002583 }
Carolyn Wyborny0b1a6f22012-10-18 07:16:19 +00002584 break;
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00002585 }
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00002586}
2587
Jeff Kirsherb980ac12013-02-23 07:29:56 +00002588/**
Carolyn Wyborny56cec242013-10-17 05:36:26 +00002589 * igb_init_mas - init Media Autosense feature if enabled in the NVM
2590 *
2591 * @adapter: adapter struct
2592 **/
2593static void igb_init_mas(struct igb_adapter *adapter)
2594{
2595 struct e1000_hw *hw = &adapter->hw;
2596 u16 eeprom_data;
2597
2598 hw->nvm.ops.read(hw, NVM_COMPAT, 1, &eeprom_data);
2599 switch (hw->bus.func) {
2600 case E1000_FUNC_0:
2601 if (eeprom_data & IGB_MAS_ENABLE_0) {
2602 adapter->flags |= IGB_FLAG_MAS_ENABLE;
2603 netdev_info(adapter->netdev,
2604 "MAS: Enabling Media Autosense for port %d\n",
2605 hw->bus.func);
2606 }
2607 break;
2608 case E1000_FUNC_1:
2609 if (eeprom_data & IGB_MAS_ENABLE_1) {
2610 adapter->flags |= IGB_FLAG_MAS_ENABLE;
2611 netdev_info(adapter->netdev,
2612 "MAS: Enabling Media Autosense for port %d\n",
2613 hw->bus.func);
2614 }
2615 break;
2616 case E1000_FUNC_2:
2617 if (eeprom_data & IGB_MAS_ENABLE_2) {
2618 adapter->flags |= IGB_FLAG_MAS_ENABLE;
2619 netdev_info(adapter->netdev,
2620 "MAS: Enabling Media Autosense for port %d\n",
2621 hw->bus.func);
2622 }
2623 break;
2624 case E1000_FUNC_3:
2625 if (eeprom_data & IGB_MAS_ENABLE_3) {
2626 adapter->flags |= IGB_FLAG_MAS_ENABLE;
2627 netdev_info(adapter->netdev,
2628 "MAS: Enabling Media Autosense for port %d\n",
2629 hw->bus.func);
2630 }
2631 break;
2632 default:
2633 /* Shouldn't get here */
2634 netdev_err(adapter->netdev,
2635 "MAS: Invalid port configuration, returning\n");
2636 break;
2637 }
2638}
2639
2640/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00002641 * igb_init_i2c - Init I2C interface
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +00002642 * @adapter: pointer to adapter structure
Jeff Kirsherb980ac12013-02-23 07:29:56 +00002643 **/
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +00002644static s32 igb_init_i2c(struct igb_adapter *adapter)
2645{
Todd Fujinaka23d87822014-06-04 07:12:15 +00002646 s32 status = 0;
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +00002647
2648 /* I2C interface supported on i350 devices */
2649 if (adapter->hw.mac.type != e1000_i350)
Todd Fujinaka23d87822014-06-04 07:12:15 +00002650 return 0;
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +00002651
2652 /* Initialize the i2c bus which is controlled by the registers.
2653 * This bus will use the i2c_algo_bit structue that implements
2654 * the protocol through toggling of the 4 bits in the register.
2655 */
2656 adapter->i2c_adap.owner = THIS_MODULE;
2657 adapter->i2c_algo = igb_i2c_algo;
2658 adapter->i2c_algo.data = adapter;
2659 adapter->i2c_adap.algo_data = &adapter->i2c_algo;
2660 adapter->i2c_adap.dev.parent = &adapter->pdev->dev;
2661 strlcpy(adapter->i2c_adap.name, "igb BB",
2662 sizeof(adapter->i2c_adap.name));
2663 status = i2c_bit_add_bus(&adapter->i2c_adap);
2664 return status;
2665}
2666
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00002667/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00002668 * igb_probe - Device Initialization Routine
2669 * @pdev: PCI device information struct
2670 * @ent: entry in igb_pci_tbl
Auke Kok9d5c8242008-01-24 02:22:38 -08002671 *
Jeff Kirsherb980ac12013-02-23 07:29:56 +00002672 * Returns 0 on success, negative on failure
Auke Kok9d5c8242008-01-24 02:22:38 -08002673 *
Jeff Kirsherb980ac12013-02-23 07:29:56 +00002674 * igb_probe initializes an adapter identified by a pci_dev structure.
2675 * The OS initialization, configuring of the adapter private structure,
2676 * and a hardware reset occur.
Auke Kok9d5c8242008-01-24 02:22:38 -08002677 **/
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00002678static int igb_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Auke Kok9d5c8242008-01-24 02:22:38 -08002679{
2680 struct net_device *netdev;
2681 struct igb_adapter *adapter;
2682 struct e1000_hw *hw;
Alexander Duyck4337e992009-10-27 23:48:31 +00002683 u16 eeprom_data = 0;
Carolyn Wyborny9835fd72010-11-22 17:17:21 +00002684 s32 ret_val;
Alexander Duyck4337e992009-10-27 23:48:31 +00002685 static int global_quad_port_a; /* global quad port a indication */
Auke Kok9d5c8242008-01-24 02:22:38 -08002686 const struct e1000_info *ei = igb_info_tbl[ent->driver_data];
David S. Miller2d6a5e92009-03-17 15:01:30 -07002687 int err, pci_using_dac;
Carolyn Wyborny9835fd72010-11-22 17:17:21 +00002688 u8 part_str[E1000_PBANUM_LENGTH];
Auke Kok9d5c8242008-01-24 02:22:38 -08002689
Andy Gospodarekbded64a2010-07-21 06:40:31 +00002690 /* Catch broken hardware that put the wrong VF device ID in
2691 * the PCIe SR-IOV capability.
2692 */
2693 if (pdev->is_virtfn) {
2694 WARN(1, KERN_ERR "%s (%hx:%hx) should not be a VF!\n",
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002695 pci_name(pdev), pdev->vendor, pdev->device);
Andy Gospodarekbded64a2010-07-21 06:40:31 +00002696 return -EINVAL;
2697 }
2698
Alexander Duyckaed5dec2009-02-06 23:16:04 +00002699 err = pci_enable_device_mem(pdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08002700 if (err)
2701 return err;
2702
2703 pci_using_dac = 0;
Russell Kingdc4ff9b2013-06-10 12:24:50 +01002704 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
Auke Kok9d5c8242008-01-24 02:22:38 -08002705 if (!err) {
Russell Kingdc4ff9b2013-06-10 12:24:50 +01002706 pci_using_dac = 1;
Auke Kok9d5c8242008-01-24 02:22:38 -08002707 } else {
Russell Kingdc4ff9b2013-06-10 12:24:50 +01002708 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
Auke Kok9d5c8242008-01-24 02:22:38 -08002709 if (err) {
Russell Kingdc4ff9b2013-06-10 12:24:50 +01002710 dev_err(&pdev->dev,
2711 "No usable DMA configuration, aborting\n");
2712 goto err_dma;
Auke Kok9d5c8242008-01-24 02:22:38 -08002713 }
2714 }
2715
Johannes Thumshirn56d766d2016-06-07 09:44:05 +02002716 err = pci_request_mem_regions(pdev, igb_driver_name);
Auke Kok9d5c8242008-01-24 02:22:38 -08002717 if (err)
2718 goto err_pci_reg;
2719
Frans Pop19d5afd2009-10-02 10:04:12 -07002720 pci_enable_pcie_error_reporting(pdev);
Alexander Duyck40a914f2008-11-27 00:24:37 -08002721
Auke Kok9d5c8242008-01-24 02:22:38 -08002722 pci_set_master(pdev);
Auke Kokc682fc22008-04-23 11:09:34 -07002723 pci_save_state(pdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08002724
2725 err = -ENOMEM;
Alexander Duyck1bfaf072009-02-19 20:39:23 -08002726 netdev = alloc_etherdev_mq(sizeof(struct igb_adapter),
Alexander Duyck1cc3bd82011-08-26 07:44:10 +00002727 IGB_MAX_TX_QUEUES);
Auke Kok9d5c8242008-01-24 02:22:38 -08002728 if (!netdev)
2729 goto err_alloc_etherdev;
2730
2731 SET_NETDEV_DEV(netdev, &pdev->dev);
2732
2733 pci_set_drvdata(pdev, netdev);
2734 adapter = netdev_priv(netdev);
2735 adapter->netdev = netdev;
2736 adapter->pdev = pdev;
2737 hw = &adapter->hw;
2738 hw->back = adapter;
stephen hemmingerb3f4d592012-03-13 06:04:20 +00002739 adapter->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
Auke Kok9d5c8242008-01-24 02:22:38 -08002740
Auke Kok9d5c8242008-01-24 02:22:38 -08002741 err = -EIO;
Jarod Wilson73bf8042015-09-10 15:37:50 -04002742 adapter->io_addr = pci_iomap(pdev, 0, 0);
2743 if (!adapter->io_addr)
Auke Kok9d5c8242008-01-24 02:22:38 -08002744 goto err_ioremap;
Jarod Wilson73bf8042015-09-10 15:37:50 -04002745 /* hw->hw_addr can be altered, we'll use adapter->io_addr for unmap */
2746 hw->hw_addr = adapter->io_addr;
Auke Kok9d5c8242008-01-24 02:22:38 -08002747
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08002748 netdev->netdev_ops = &igb_netdev_ops;
Auke Kok9d5c8242008-01-24 02:22:38 -08002749 igb_set_ethtool_ops(netdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08002750 netdev->watchdog_timeo = 5 * HZ;
Auke Kok9d5c8242008-01-24 02:22:38 -08002751
2752 strncpy(netdev->name, pci_name(pdev), sizeof(netdev->name) - 1);
2753
Aaron Sierra89dbefb2013-10-31 00:32:34 +00002754 netdev->mem_start = pci_resource_start(pdev, 0);
2755 netdev->mem_end = pci_resource_end(pdev, 0);
Auke Kok9d5c8242008-01-24 02:22:38 -08002756
Auke Kok9d5c8242008-01-24 02:22:38 -08002757 /* PCI config space info */
2758 hw->vendor_id = pdev->vendor;
2759 hw->device_id = pdev->device;
2760 hw->revision_id = pdev->revision;
2761 hw->subsystem_vendor_id = pdev->subsystem_vendor;
2762 hw->subsystem_device_id = pdev->subsystem_device;
2763
Auke Kok9d5c8242008-01-24 02:22:38 -08002764 /* Copy the default MAC, PHY and NVM function pointers */
2765 memcpy(&hw->mac.ops, ei->mac_ops, sizeof(hw->mac.ops));
2766 memcpy(&hw->phy.ops, ei->phy_ops, sizeof(hw->phy.ops));
2767 memcpy(&hw->nvm.ops, ei->nvm_ops, sizeof(hw->nvm.ops));
2768 /* Initialize skew-specific constants */
2769 err = ei->get_invariants(hw);
2770 if (err)
Alexander Duyck450c87c2009-02-06 23:22:11 +00002771 goto err_sw_init;
Auke Kok9d5c8242008-01-24 02:22:38 -08002772
Alexander Duyck450c87c2009-02-06 23:22:11 +00002773 /* setup the private structure */
Auke Kok9d5c8242008-01-24 02:22:38 -08002774 err = igb_sw_init(adapter);
2775 if (err)
2776 goto err_sw_init;
2777
2778 igb_get_bus_info_pcie(hw);
2779
2780 hw->phy.autoneg_wait_to_complete = false;
Auke Kok9d5c8242008-01-24 02:22:38 -08002781
2782 /* Copper options */
2783 if (hw->phy.media_type == e1000_media_type_copper) {
2784 hw->phy.mdix = AUTO_ALL_MODES;
2785 hw->phy.disable_polarity_correction = false;
2786 hw->phy.ms_type = e1000_ms_hw_default;
2787 }
2788
2789 if (igb_check_reset_block(hw))
2790 dev_info(&pdev->dev,
2791 "PHY reset is blocked due to SOL/IDER session.\n");
2792
Jeff Kirsherb980ac12013-02-23 07:29:56 +00002793 /* features is initialized to 0 in allocation, it might have bits
Alexander Duyck077887c2011-08-26 07:46:29 +00002794 * set by igb_sw_init so we should use an or instead of an
2795 * assignment.
2796 */
2797 netdev->features |= NETIF_F_SG |
Alexander Duyck077887c2011-08-26 07:46:29 +00002798 NETIF_F_TSO |
2799 NETIF_F_TSO6 |
2800 NETIF_F_RXHASH |
2801 NETIF_F_RXCSUM |
Alexander Duycke10715d2016-04-14 17:19:38 -04002802 NETIF_F_HW_CSUM;
Michał Mirosławac52caa2011-06-08 08:38:01 +00002803
Alexander Duyck6e033702016-01-13 07:31:23 -08002804 if (hw->mac.type >= e1000_82576)
2805 netdev->features |= NETIF_F_SCTP_CRC;
2806
Alexander Duycke10715d2016-04-14 17:19:38 -04002807#define IGB_GSO_PARTIAL_FEATURES (NETIF_F_GSO_GRE | \
2808 NETIF_F_GSO_GRE_CSUM | \
Tom Herbert7e133182016-05-18 09:06:10 -07002809 NETIF_F_GSO_IPXIP4 | \
Alexander Duyckbf2d1df2016-05-18 10:44:53 -07002810 NETIF_F_GSO_IPXIP6 | \
Alexander Duycke10715d2016-04-14 17:19:38 -04002811 NETIF_F_GSO_UDP_TUNNEL | \
2812 NETIF_F_GSO_UDP_TUNNEL_CSUM)
2813
2814 netdev->gso_partial_features = IGB_GSO_PARTIAL_FEATURES;
2815 netdev->features |= NETIF_F_GSO_PARTIAL | IGB_GSO_PARTIAL_FEATURES;
2816
Alexander Duyck077887c2011-08-26 07:46:29 +00002817 /* copy netdev features into list of user selectable features */
Alexander Duycke10715d2016-04-14 17:19:38 -04002818 netdev->hw_features |= netdev->features |
2819 NETIF_F_HW_VLAN_CTAG_RX |
2820 NETIF_F_HW_VLAN_CTAG_TX |
2821 NETIF_F_RXALL;
Auke Kok9d5c8242008-01-24 02:22:38 -08002822
Alexander Duyck6e033702016-01-13 07:31:23 -08002823 if (hw->mac.type >= e1000_i350)
2824 netdev->hw_features |= NETIF_F_NTUPLE;
2825
Alexander Duycke10715d2016-04-14 17:19:38 -04002826 if (pci_using_dac)
2827 netdev->features |= NETIF_F_HIGHDMA;
Alexander Duyck077887c2011-08-26 07:46:29 +00002828
Alexander Duycke10715d2016-04-14 17:19:38 -04002829 netdev->vlan_features |= netdev->features | NETIF_F_TSO_MANGLEID;
Alexander Duyck6e033702016-01-13 07:31:23 -08002830 netdev->mpls_features |= NETIF_F_HW_CSUM;
Alexander Duycke10715d2016-04-14 17:19:38 -04002831 netdev->hw_enc_features |= netdev->vlan_features;
2832
2833 /* set this bit last since it cannot be part of vlan_features */
2834 netdev->features |= NETIF_F_HW_VLAN_CTAG_FILTER |
2835 NETIF_F_HW_VLAN_CTAG_RX |
2836 NETIF_F_HW_VLAN_CTAG_TX;
Jeff Kirsher48f29ff2008-06-05 04:06:27 -07002837
Ben Greear6b8f0922012-03-06 09:41:53 +00002838 netdev->priv_flags |= IFF_SUPP_NOFCS;
2839
Jiri Pirko01789342011-08-16 06:29:00 +00002840 netdev->priv_flags |= IFF_UNICAST_FLT;
2841
Jarod Wilson91c527a2016-10-17 15:54:05 -04002842 /* MTU range: 68 - 9216 */
2843 netdev->min_mtu = ETH_MIN_MTU;
2844 netdev->max_mtu = MAX_STD_JUMBO_FRAME_SIZE;
2845
Alexander Duyck330a6d62009-10-27 23:51:35 +00002846 adapter->en_mng_pt = igb_enable_mng_pass_thru(hw);
Auke Kok9d5c8242008-01-24 02:22:38 -08002847
2848 /* before reading the NVM, reset the controller to put the device in a
Jeff Kirsherb980ac12013-02-23 07:29:56 +00002849 * known good starting state
2850 */
Auke Kok9d5c8242008-01-24 02:22:38 -08002851 hw->mac.ops.reset_hw(hw);
2852
Carolyn Wybornyef3a0092013-07-17 19:02:53 +00002853 /* make sure the NVM is good , i211/i210 parts can have special NVM
2854 * that doesn't contain a checksum
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002855 */
Carolyn Wybornyef3a0092013-07-17 19:02:53 +00002856 switch (hw->mac.type) {
2857 case e1000_i210:
2858 case e1000_i211:
2859 if (igb_get_flash_presence_i210(hw)) {
2860 if (hw->nvm.ops.validate(hw) < 0) {
2861 dev_err(&pdev->dev,
2862 "The NVM Checksum Is Not Valid\n");
2863 err = -EIO;
2864 goto err_eeprom;
2865 }
2866 }
2867 break;
2868 default:
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002869 if (hw->nvm.ops.validate(hw) < 0) {
2870 dev_err(&pdev->dev, "The NVM Checksum Is Not Valid\n");
2871 err = -EIO;
2872 goto err_eeprom;
2873 }
Carolyn Wybornyef3a0092013-07-17 19:02:53 +00002874 break;
Auke Kok9d5c8242008-01-24 02:22:38 -08002875 }
2876
John Holland806ffb12016-02-18 12:10:52 +01002877 if (eth_platform_get_mac_address(&pdev->dev, hw->mac.addr)) {
2878 /* copy the MAC address out of the NVM */
2879 if (hw->mac.ops.read_mac_addr(hw))
2880 dev_err(&pdev->dev, "NVM Read Error\n");
2881 }
Auke Kok9d5c8242008-01-24 02:22:38 -08002882
2883 memcpy(netdev->dev_addr, hw->mac.addr, netdev->addr_len);
Auke Kok9d5c8242008-01-24 02:22:38 -08002884
Jiri Pirkoaaeb6cd2013-01-08 01:38:26 +00002885 if (!is_valid_ether_addr(netdev->dev_addr)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08002886 dev_err(&pdev->dev, "Invalid MAC Address\n");
2887 err = -EIO;
2888 goto err_eeprom;
2889 }
2890
Yury Kylulin83c21332017-03-07 11:20:25 +03002891 igb_set_default_mac_filter(adapter);
2892
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00002893 /* get firmware version for ethtool -i */
2894 igb_set_fw_version(adapter);
2895
Todd Fujinaka27dff8b2014-05-29 05:47:26 +00002896 /* configure RXPBSIZE and TXPBSIZE */
2897 if (hw->mac.type == e1000_i210) {
2898 wr32(E1000_RXPBS, I210_RXPBSIZE_DEFAULT);
2899 wr32(E1000_TXPBS, I210_TXPBSIZE_DEFAULT);
2900 }
2901
Kees Cook26566ea2017-10-16 17:29:35 -07002902 timer_setup(&adapter->watchdog_timer, igb_watchdog, 0);
2903 timer_setup(&adapter->phy_info_timer, igb_update_phy_info, 0);
Auke Kok9d5c8242008-01-24 02:22:38 -08002904
2905 INIT_WORK(&adapter->reset_task, igb_reset_task);
2906 INIT_WORK(&adapter->watchdog_task, igb_watchdog_task);
2907
Alexander Duyck450c87c2009-02-06 23:22:11 +00002908 /* Initialize link properties that are user-changeable */
Auke Kok9d5c8242008-01-24 02:22:38 -08002909 adapter->fc_autoneg = true;
2910 hw->mac.autoneg = true;
2911 hw->phy.autoneg_advertised = 0x2f;
2912
Alexander Duyck0cce1192009-07-23 18:10:24 +00002913 hw->fc.requested_mode = e1000_fc_default;
2914 hw->fc.current_mode = e1000_fc_default;
Auke Kok9d5c8242008-01-24 02:22:38 -08002915
Auke Kok9d5c8242008-01-24 02:22:38 -08002916 igb_validate_mdi_setting(hw);
2917
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002918 /* By default, support wake on port A */
Alexander Duycka2cf8b62009-03-13 20:41:17 +00002919 if (hw->bus.func == 0)
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002920 adapter->flags |= IGB_FLAG_WOL_SUPPORTED;
2921
2922 /* Check the NVM for wake support on non-port A ports */
2923 if (hw->mac.type >= e1000_82580)
Alexander Duyck55cac242009-11-19 12:42:21 +00002924 hw->nvm.ops.read(hw, NVM_INIT_CONTROL3_PORT_A +
Jeff Kirsherb980ac12013-02-23 07:29:56 +00002925 NVM_82580_LAN_FUNC_OFFSET(hw->bus.func), 1,
2926 &eeprom_data);
Alexander Duycka2cf8b62009-03-13 20:41:17 +00002927 else if (hw->bus.func == 1)
2928 hw->nvm.ops.read(hw, NVM_INIT_CONTROL3_PORT_B, 1, &eeprom_data);
Auke Kok9d5c8242008-01-24 02:22:38 -08002929
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002930 if (eeprom_data & IGB_EEPROM_APME)
2931 adapter->flags |= IGB_FLAG_WOL_SUPPORTED;
Auke Kok9d5c8242008-01-24 02:22:38 -08002932
2933 /* now that we have the eeprom settings, apply the special cases where
2934 * the eeprom may be wrong or the board simply won't support wake on
Jeff Kirsherb980ac12013-02-23 07:29:56 +00002935 * lan on a particular port
2936 */
Auke Kok9d5c8242008-01-24 02:22:38 -08002937 switch (pdev->device) {
2938 case E1000_DEV_ID_82575GB_QUAD_COPPER:
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002939 adapter->flags &= ~IGB_FLAG_WOL_SUPPORTED;
Auke Kok9d5c8242008-01-24 02:22:38 -08002940 break;
2941 case E1000_DEV_ID_82575EB_FIBER_SERDES:
Alexander Duyck2d064c02008-07-08 15:10:12 -07002942 case E1000_DEV_ID_82576_FIBER:
2943 case E1000_DEV_ID_82576_SERDES:
Auke Kok9d5c8242008-01-24 02:22:38 -08002944 /* Wake events only supported on port A for dual fiber
Jeff Kirsherb980ac12013-02-23 07:29:56 +00002945 * regardless of eeprom setting
2946 */
Auke Kok9d5c8242008-01-24 02:22:38 -08002947 if (rd32(E1000_STATUS) & E1000_STATUS_FUNC_1)
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002948 adapter->flags &= ~IGB_FLAG_WOL_SUPPORTED;
Auke Kok9d5c8242008-01-24 02:22:38 -08002949 break;
Alexander Duyckc8ea5ea2009-03-13 20:42:35 +00002950 case E1000_DEV_ID_82576_QUAD_COPPER:
Stefan Assmannd5aa2252010-04-09 09:51:34 +00002951 case E1000_DEV_ID_82576_QUAD_COPPER_ET2:
Alexander Duyckc8ea5ea2009-03-13 20:42:35 +00002952 /* if quad port adapter, disable WoL on all but port A */
2953 if (global_quad_port_a != 0)
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002954 adapter->flags &= ~IGB_FLAG_WOL_SUPPORTED;
Alexander Duyckc8ea5ea2009-03-13 20:42:35 +00002955 else
2956 adapter->flags |= IGB_FLAG_QUAD_PORT_A;
2957 /* Reset for multiple quad port adapters */
2958 if (++global_quad_port_a == 4)
2959 global_quad_port_a = 0;
2960 break;
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002961 default:
2962 /* If the device can't wake, don't set software support */
2963 if (!device_can_wakeup(&adapter->pdev->dev))
2964 adapter->flags &= ~IGB_FLAG_WOL_SUPPORTED;
Auke Kok9d5c8242008-01-24 02:22:38 -08002965 }
2966
2967 /* initialize the wol settings based on the eeprom settings */
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002968 if (adapter->flags & IGB_FLAG_WOL_SUPPORTED)
2969 adapter->wol |= E1000_WUFC_MAG;
2970
2971 /* Some vendors want WoL disabled by default, but still supported */
2972 if ((hw->mac.type == e1000_i350) &&
2973 (pdev->subsystem_vendor == PCI_VENDOR_ID_HP)) {
2974 adapter->flags |= IGB_FLAG_WOL_SUPPORTED;
2975 adapter->wol = 0;
2976 }
2977
Todd Fujinaka5e350b92016-01-05 10:08:28 -08002978 /* Some vendors want the ability to Use the EEPROM setting as
2979 * enable/disable only, and not for capability
2980 */
2981 if (((hw->mac.type == e1000_i350) ||
2982 (hw->mac.type == e1000_i354)) &&
2983 (pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)) {
2984 adapter->flags |= IGB_FLAG_WOL_SUPPORTED;
2985 adapter->wol = 0;
2986 }
2987 if (hw->mac.type == e1000_i350) {
2988 if (((pdev->subsystem_device == 0x5001) ||
2989 (pdev->subsystem_device == 0x5002)) &&
2990 (hw->bus.func == 0)) {
2991 adapter->flags |= IGB_FLAG_WOL_SUPPORTED;
2992 adapter->wol = 0;
2993 }
2994 if (pdev->subsystem_device == 0x1F52)
2995 adapter->flags |= IGB_FLAG_WOL_SUPPORTED;
2996 }
2997
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002998 device_set_wakeup_enable(&adapter->pdev->dev,
2999 adapter->flags & IGB_FLAG_WOL_SUPPORTED);
Auke Kok9d5c8242008-01-24 02:22:38 -08003000
3001 /* reset the hardware with the new settings */
3002 igb_reset(adapter);
3003
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +00003004 /* Init the I2C interface */
3005 err = igb_init_i2c(adapter);
3006 if (err) {
3007 dev_err(&pdev->dev, "failed to init i2c interface\n");
3008 goto err_eeprom;
3009 }
3010
Auke Kok9d5c8242008-01-24 02:22:38 -08003011 /* let the f/w know that the h/w is now under the control of the
Carolyn Wybornye52c0f92014-04-11 01:46:06 +00003012 * driver.
3013 */
Auke Kok9d5c8242008-01-24 02:22:38 -08003014 igb_get_hw_control(adapter);
3015
Auke Kok9d5c8242008-01-24 02:22:38 -08003016 strcpy(netdev->name, "eth%d");
3017 err = register_netdev(netdev);
3018 if (err)
3019 goto err_register;
3020
Jesse Brandeburgb168dfc2009-04-17 20:44:32 +00003021 /* carrier off reporting is important to ethtool even BEFORE open */
3022 netif_carrier_off(netdev);
3023
Jeff Kirsher421e02f2008-10-17 11:08:31 -07003024#ifdef CONFIG_IGB_DCA
Alexander Duyckbbd98fe2009-01-31 00:52:30 -08003025 if (dca_add_requester(&pdev->dev) == 0) {
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07003026 adapter->flags |= IGB_FLAG_DCA_ENABLED;
Jeb Cramerfe4506b2008-07-08 15:07:55 -07003027 dev_info(&pdev->dev, "DCA enabled\n");
Jeb Cramerfe4506b2008-07-08 15:07:55 -07003028 igb_setup_dca(adapter);
3029 }
Alexander Duyckc5b9bd52009-10-27 23:46:01 +00003030
Jeb Cramerfe4506b2008-07-08 15:07:55 -07003031#endif
Carolyn Wybornye4288932012-12-07 03:01:42 +00003032#ifdef CONFIG_IGB_HWMON
3033 /* Initialize the thermal sensor on i350 devices. */
3034 if (hw->mac.type == e1000_i350 && hw->bus.func == 0) {
3035 u16 ets_word;
Matthew Vick3c89f6d2012-08-10 05:40:43 +00003036
Jeff Kirsherb980ac12013-02-23 07:29:56 +00003037 /* Read the NVM to determine if this i350 device supports an
Carolyn Wybornye4288932012-12-07 03:01:42 +00003038 * external thermal sensor.
3039 */
3040 hw->nvm.ops.read(hw, NVM_ETS_CFG, 1, &ets_word);
3041 if (ets_word != 0x0000 && ets_word != 0xFFFF)
3042 adapter->ets = true;
3043 else
3044 adapter->ets = false;
3045 if (igb_sysfs_init(adapter))
3046 dev_err(&pdev->dev,
3047 "failed to allocate sysfs resources\n");
3048 } else {
3049 adapter->ets = false;
3050 }
3051#endif
Carolyn Wyborny56cec242013-10-17 05:36:26 +00003052 /* Check if Media Autosense is enabled */
3053 adapter->ei = *ei;
3054 if (hw->dev_spec._82575.mas_capable)
3055 igb_init_mas(adapter);
3056
Anders Berggren673b8b72011-02-04 07:32:32 +00003057 /* do hw tstamp init after resetting */
Richard Cochran7ebae812012-03-16 10:55:37 +00003058 igb_ptp_init(adapter);
Anders Berggren673b8b72011-02-04 07:32:32 +00003059
Auke Kok9d5c8242008-01-24 02:22:38 -08003060 dev_info(&pdev->dev, "Intel(R) Gigabit Ethernet Network Connection\n");
Carolyn Wybornyceb5f132013-04-18 22:21:30 +00003061 /* print bus type/speed/width info, not applicable to i354 */
3062 if (hw->mac.type != e1000_i354) {
3063 dev_info(&pdev->dev, "%s: (PCIe:%s:%s) %pM\n",
3064 netdev->name,
3065 ((hw->bus.speed == e1000_bus_speed_2500) ? "2.5Gb/s" :
3066 (hw->bus.speed == e1000_bus_speed_5000) ? "5.0Gb/s" :
3067 "unknown"),
3068 ((hw->bus.width == e1000_bus_width_pcie_x4) ?
3069 "Width x4" :
3070 (hw->bus.width == e1000_bus_width_pcie_x2) ?
3071 "Width x2" :
3072 (hw->bus.width == e1000_bus_width_pcie_x1) ?
3073 "Width x1" : "unknown"), netdev->dev_addr);
3074 }
Auke Kok9d5c8242008-01-24 02:22:38 -08003075
Todd Fujinaka53ea6c72013-08-23 07:49:00 +00003076 if ((hw->mac.type >= e1000_i210 ||
3077 igb_get_flash_presence_i210(hw))) {
3078 ret_val = igb_read_part_string(hw, part_str,
3079 E1000_PBANUM_LENGTH);
3080 } else {
3081 ret_val = -E1000_ERR_INVM_VALUE_NOT_FOUND;
3082 }
3083
Carolyn Wyborny9835fd72010-11-22 17:17:21 +00003084 if (ret_val)
3085 strcpy(part_str, "Unknown");
3086 dev_info(&pdev->dev, "%s: PBA No: %s\n", netdev->name, part_str);
Auke Kok9d5c8242008-01-24 02:22:38 -08003087 dev_info(&pdev->dev,
3088 "Using %s interrupts. %d rx queue(s), %d tx queue(s)\n",
Carolyn Wybornycd14ef52013-12-10 07:58:34 +00003089 (adapter->flags & IGB_FLAG_HAS_MSIX) ? "MSI-X" :
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07003090 (adapter->flags & IGB_FLAG_HAS_MSI) ? "MSI" : "legacy",
Auke Kok9d5c8242008-01-24 02:22:38 -08003091 adapter->num_rx_queues, adapter->num_tx_queues);
Carolyn Wybornyf4c01e92014-03-12 03:58:22 +00003092 if (hw->phy.media_type == e1000_media_type_copper) {
3093 switch (hw->mac.type) {
3094 case e1000_i350:
3095 case e1000_i210:
3096 case e1000_i211:
3097 /* Enable EEE for internal copper PHY devices */
Todd Fujinakac4c112f2014-08-29 06:43:13 +00003098 err = igb_set_eee_i350(hw, true, true);
Carolyn Wybornyf4c01e92014-03-12 03:58:22 +00003099 if ((!err) &&
3100 (!hw->dev_spec._82575.eee_disable)) {
3101 adapter->eee_advert =
3102 MDIO_EEE_100TX | MDIO_EEE_1000T;
3103 adapter->flags |= IGB_FLAG_EEE;
3104 }
3105 break;
3106 case e1000_i354:
Carolyn Wybornyceb5f132013-04-18 22:21:30 +00003107 if ((rd32(E1000_CTRL_EXT) &
Carolyn Wybornyf4c01e92014-03-12 03:58:22 +00003108 E1000_CTRL_EXT_LINK_MODE_SGMII)) {
Todd Fujinakac4c112f2014-08-29 06:43:13 +00003109 err = igb_set_eee_i354(hw, true, true);
Carolyn Wybornyf4c01e92014-03-12 03:58:22 +00003110 if ((!err) &&
3111 (!hw->dev_spec._82575.eee_disable)) {
3112 adapter->eee_advert =
3113 MDIO_EEE_100TX | MDIO_EEE_1000T;
3114 adapter->flags |= IGB_FLAG_EEE;
3115 }
3116 }
3117 break;
3118 default:
3119 break;
Carolyn Wybornyceb5f132013-04-18 22:21:30 +00003120 }
Carolyn Wyborny09b068d2011-03-11 20:42:13 -08003121 }
Yan, Zheng749ab2c2012-01-04 20:23:37 +00003122 pm_runtime_put_noidle(&pdev->dev);
Auke Kok9d5c8242008-01-24 02:22:38 -08003123 return 0;
3124
3125err_register:
3126 igb_release_hw_control(adapter);
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +00003127 memset(&adapter->i2c_adap, 0, sizeof(adapter->i2c_adap));
Auke Kok9d5c8242008-01-24 02:22:38 -08003128err_eeprom:
3129 if (!igb_check_reset_block(hw))
Alexander Duyckf5f4cf02008-11-21 21:30:24 -08003130 igb_reset_phy(hw);
Auke Kok9d5c8242008-01-24 02:22:38 -08003131
3132 if (hw->flash_address)
3133 iounmap(hw->flash_address);
Auke Kok9d5c8242008-01-24 02:22:38 -08003134err_sw_init:
Yury Kylulin83c21332017-03-07 11:20:25 +03003135 kfree(adapter->mac_table);
Jia-Ju Bai42ad1a02015-08-05 22:05:16 +08003136 kfree(adapter->shadow_vfta);
Alexander Duyck047e0032009-10-27 15:49:27 +00003137 igb_clear_interrupt_scheme(adapter);
Todd Fujinakaceee3452015-08-07 17:27:39 -07003138#ifdef CONFIG_PCI_IOV
3139 igb_disable_sriov(pdev);
3140#endif
Jarod Wilson73bf8042015-09-10 15:37:50 -04003141 pci_iounmap(pdev, adapter->io_addr);
Auke Kok9d5c8242008-01-24 02:22:38 -08003142err_ioremap:
3143 free_netdev(netdev);
3144err_alloc_etherdev:
Johannes Thumshirn56d766d2016-06-07 09:44:05 +02003145 pci_release_mem_regions(pdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08003146err_pci_reg:
3147err_dma:
3148 pci_disable_device(pdev);
3149 return err;
3150}
3151
Greg Rosefa44f2f2013-01-17 01:03:06 -08003152#ifdef CONFIG_PCI_IOV
Stefan Assmann781798a2013-09-24 05:18:39 +00003153static int igb_disable_sriov(struct pci_dev *pdev)
Greg Rosefa44f2f2013-01-17 01:03:06 -08003154{
3155 struct net_device *netdev = pci_get_drvdata(pdev);
3156 struct igb_adapter *adapter = netdev_priv(netdev);
3157 struct e1000_hw *hw = &adapter->hw;
3158
3159 /* reclaim resources allocated to VFs */
3160 if (adapter->vf_data) {
3161 /* disable iov and allow time for transactions to clear */
Alexander Duyckb09186d2013-03-26 00:03:26 +00003162 if (pci_vfs_assigned(pdev)) {
Greg Rosefa44f2f2013-01-17 01:03:06 -08003163 dev_warn(&pdev->dev,
3164 "Cannot deallocate SR-IOV virtual functions while they are assigned - VFs will not be deallocated\n");
3165 return -EPERM;
3166 } else {
3167 pci_disable_sriov(pdev);
3168 msleep(500);
3169 }
3170
Yury Kylulin4827cc32017-03-07 11:20:26 +03003171 kfree(adapter->vf_mac_list);
3172 adapter->vf_mac_list = NULL;
Greg Rosefa44f2f2013-01-17 01:03:06 -08003173 kfree(adapter->vf_data);
3174 adapter->vf_data = NULL;
3175 adapter->vfs_allocated_count = 0;
3176 wr32(E1000_IOVCTL, E1000_IOVCTL_REUSE_VFQ);
3177 wrfl();
3178 msleep(100);
3179 dev_info(&pdev->dev, "IOV Disabled\n");
3180
3181 /* Re-enable DMA Coalescing flag since IOV is turned off */
3182 adapter->flags |= IGB_FLAG_DMAC;
3183 }
3184
3185 return 0;
3186}
3187
3188static int igb_enable_sriov(struct pci_dev *pdev, int num_vfs)
3189{
3190 struct net_device *netdev = pci_get_drvdata(pdev);
3191 struct igb_adapter *adapter = netdev_priv(netdev);
3192 int old_vfs = pci_num_vf(pdev);
Yury Kylulin4827cc32017-03-07 11:20:26 +03003193 struct vf_mac_filter *mac_list;
Greg Rosefa44f2f2013-01-17 01:03:06 -08003194 int err = 0;
Yury Kylulin4827cc32017-03-07 11:20:26 +03003195 int num_vf_mac_filters, i;
Greg Rosefa44f2f2013-01-17 01:03:06 -08003196
Carolyn Wybornycd14ef52013-12-10 07:58:34 +00003197 if (!(adapter->flags & IGB_FLAG_HAS_MSIX) || num_vfs > 7) {
Mitch A Williams50267192013-06-20 06:03:36 +00003198 err = -EPERM;
3199 goto out;
3200 }
Greg Rosefa44f2f2013-01-17 01:03:06 -08003201 if (!num_vfs)
3202 goto out;
Greg Rosefa44f2f2013-01-17 01:03:06 -08003203
Stefan Assmann781798a2013-09-24 05:18:39 +00003204 if (old_vfs) {
3205 dev_info(&pdev->dev, "%d pre-allocated VFs found - override max_vfs setting of %d\n",
3206 old_vfs, max_vfs);
3207 adapter->vfs_allocated_count = old_vfs;
3208 } else
3209 adapter->vfs_allocated_count = num_vfs;
Greg Rosefa44f2f2013-01-17 01:03:06 -08003210
3211 adapter->vf_data = kcalloc(adapter->vfs_allocated_count,
3212 sizeof(struct vf_data_storage), GFP_KERNEL);
3213
3214 /* if allocation failed then we do not support SR-IOV */
3215 if (!adapter->vf_data) {
3216 adapter->vfs_allocated_count = 0;
Greg Rosefa44f2f2013-01-17 01:03:06 -08003217 err = -ENOMEM;
3218 goto out;
3219 }
3220
Yury Kylulin4827cc32017-03-07 11:20:26 +03003221 /* Due to the limited number of RAR entries calculate potential
3222 * number of MAC filters available for the VFs. Reserve entries
3223 * for PF default MAC, PF MAC filters and at least one RAR entry
3224 * for each VF for VF MAC.
3225 */
3226 num_vf_mac_filters = adapter->hw.mac.rar_entry_count -
3227 (1 + IGB_PF_MAC_FILTERS_RESERVED +
3228 adapter->vfs_allocated_count);
3229
3230 adapter->vf_mac_list = kcalloc(num_vf_mac_filters,
3231 sizeof(struct vf_mac_filter),
3232 GFP_KERNEL);
3233
3234 mac_list = adapter->vf_mac_list;
3235 INIT_LIST_HEAD(&adapter->vf_macs.l);
3236
3237 if (adapter->vf_mac_list) {
3238 /* Initialize list of VF MAC filters */
3239 for (i = 0; i < num_vf_mac_filters; i++) {
3240 mac_list->vf = -1;
3241 mac_list->free = true;
3242 list_add(&mac_list->l, &adapter->vf_macs.l);
3243 mac_list++;
3244 }
3245 } else {
3246 /* If we could not allocate memory for the VF MAC filters
3247 * we can continue without this feature but warn user.
3248 */
3249 dev_err(&pdev->dev,
3250 "Unable to allocate memory for VF MAC filter list\n");
3251 }
3252
Stefan Assmann781798a2013-09-24 05:18:39 +00003253 /* only call pci_enable_sriov() if no VFs are allocated already */
3254 if (!old_vfs) {
3255 err = pci_enable_sriov(pdev, adapter->vfs_allocated_count);
3256 if (err)
3257 goto err_out;
3258 }
Greg Rosefa44f2f2013-01-17 01:03:06 -08003259 dev_info(&pdev->dev, "%d VFs allocated\n",
3260 adapter->vfs_allocated_count);
3261 for (i = 0; i < adapter->vfs_allocated_count; i++)
3262 igb_vf_configure(adapter, i);
3263
3264 /* DMA Coalescing is not supported in IOV mode. */
3265 adapter->flags &= ~IGB_FLAG_DMAC;
3266 goto out;
3267
3268err_out:
Yury Kylulin4827cc32017-03-07 11:20:26 +03003269 kfree(adapter->vf_mac_list);
3270 adapter->vf_mac_list = NULL;
Greg Rosefa44f2f2013-01-17 01:03:06 -08003271 kfree(adapter->vf_data);
3272 adapter->vf_data = NULL;
3273 adapter->vfs_allocated_count = 0;
3274out:
3275 return err;
3276}
3277
3278#endif
Jeff Kirsherb980ac12013-02-23 07:29:56 +00003279/**
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +00003280 * igb_remove_i2c - Cleanup I2C interface
3281 * @adapter: pointer to adapter structure
Jeff Kirsherb980ac12013-02-23 07:29:56 +00003282 **/
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +00003283static void igb_remove_i2c(struct igb_adapter *adapter)
3284{
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +00003285 /* free the adapter bus structure */
3286 i2c_del_adapter(&adapter->i2c_adap);
3287}
3288
Auke Kok9d5c8242008-01-24 02:22:38 -08003289/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00003290 * igb_remove - Device Removal Routine
3291 * @pdev: PCI device information struct
Auke Kok9d5c8242008-01-24 02:22:38 -08003292 *
Jeff Kirsherb980ac12013-02-23 07:29:56 +00003293 * igb_remove is called by the PCI subsystem to alert the driver
3294 * that it should release a PCI device. The could be caused by a
3295 * Hot-Plug event, or because the driver is going to be removed from
3296 * memory.
Auke Kok9d5c8242008-01-24 02:22:38 -08003297 **/
Bill Pemberton9f9a12f2012-12-03 09:24:25 -05003298static void igb_remove(struct pci_dev *pdev)
Auke Kok9d5c8242008-01-24 02:22:38 -08003299{
3300 struct net_device *netdev = pci_get_drvdata(pdev);
3301 struct igb_adapter *adapter = netdev_priv(netdev);
Jeb Cramerfe4506b2008-07-08 15:07:55 -07003302 struct e1000_hw *hw = &adapter->hw;
Auke Kok9d5c8242008-01-24 02:22:38 -08003303
Yan, Zheng749ab2c2012-01-04 20:23:37 +00003304 pm_runtime_get_noresume(&pdev->dev);
Carolyn Wybornye4288932012-12-07 03:01:42 +00003305#ifdef CONFIG_IGB_HWMON
3306 igb_sysfs_exit(adapter);
3307#endif
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +00003308 igb_remove_i2c(adapter);
Matthew Vicka79f4f82012-08-10 05:40:44 +00003309 igb_ptp_stop(adapter);
Jeff Kirsherb980ac12013-02-23 07:29:56 +00003310 /* The watchdog timer may be rescheduled, so explicitly
Tejun Heo760141a2010-12-12 16:45:14 +01003311 * disable watchdog from being rescheduled.
3312 */
Auke Kok9d5c8242008-01-24 02:22:38 -08003313 set_bit(__IGB_DOWN, &adapter->state);
3314 del_timer_sync(&adapter->watchdog_timer);
3315 del_timer_sync(&adapter->phy_info_timer);
3316
Tejun Heo760141a2010-12-12 16:45:14 +01003317 cancel_work_sync(&adapter->reset_task);
3318 cancel_work_sync(&adapter->watchdog_task);
Auke Kok9d5c8242008-01-24 02:22:38 -08003319
Jeff Kirsher421e02f2008-10-17 11:08:31 -07003320#ifdef CONFIG_IGB_DCA
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07003321 if (adapter->flags & IGB_FLAG_DCA_ENABLED) {
Jeb Cramerfe4506b2008-07-08 15:07:55 -07003322 dev_info(&pdev->dev, "DCA disabled\n");
3323 dca_remove_requester(&pdev->dev);
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07003324 adapter->flags &= ~IGB_FLAG_DCA_ENABLED;
Alexander Duyckcbd347a2009-02-15 23:59:44 -08003325 wr32(E1000_DCA_CTRL, E1000_DCA_CTRL_DCA_MODE_DISABLE);
Jeb Cramerfe4506b2008-07-08 15:07:55 -07003326 }
3327#endif
3328
Auke Kok9d5c8242008-01-24 02:22:38 -08003329 /* Release control of h/w to f/w. If f/w is AMT enabled, this
Jeff Kirsherb980ac12013-02-23 07:29:56 +00003330 * would have already happened in close and is redundant.
3331 */
Auke Kok9d5c8242008-01-24 02:22:38 -08003332 igb_release_hw_control(adapter);
3333
Alexander Duyck37680112009-02-19 20:40:30 -08003334#ifdef CONFIG_PCI_IOV
Greg Rosefa44f2f2013-01-17 01:03:06 -08003335 igb_disable_sriov(pdev);
Alexander Duyck37680112009-02-19 20:40:30 -08003336#endif
Alexander Duyck559e9c42009-10-27 23:52:50 +00003337
Alex Williamsonc23d92b2015-07-29 14:38:15 -06003338 unregister_netdev(netdev);
3339
3340 igb_clear_interrupt_scheme(adapter);
3341
Jarod Wilson73bf8042015-09-10 15:37:50 -04003342 pci_iounmap(pdev, adapter->io_addr);
Alexander Duyck28b07592009-02-06 23:20:31 +00003343 if (hw->flash_address)
3344 iounmap(hw->flash_address);
Johannes Thumshirn56d766d2016-06-07 09:44:05 +02003345 pci_release_mem_regions(pdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08003346
Yury Kylulin83c21332017-03-07 11:20:25 +03003347 kfree(adapter->mac_table);
Carolyn Wyborny1128c752011-10-14 00:13:49 +00003348 kfree(adapter->shadow_vfta);
Auke Kok9d5c8242008-01-24 02:22:38 -08003349 free_netdev(netdev);
3350
Frans Pop19d5afd2009-10-02 10:04:12 -07003351 pci_disable_pcie_error_reporting(pdev);
Alexander Duyck40a914f2008-11-27 00:24:37 -08003352
Auke Kok9d5c8242008-01-24 02:22:38 -08003353 pci_disable_device(pdev);
3354}
3355
3356/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00003357 * igb_probe_vfs - Initialize vf data storage and add VFs to pci config space
3358 * @adapter: board private structure to initialize
Alexander Duycka6b623e2009-10-27 23:47:53 +00003359 *
Jeff Kirsherb980ac12013-02-23 07:29:56 +00003360 * This function initializes the vf specific data storage and then attempts to
3361 * allocate the VFs. The reason for ordering it this way is because it is much
3362 * mor expensive time wise to disable SR-IOV than it is to allocate and free
3363 * the memory for the VFs.
Alexander Duycka6b623e2009-10-27 23:47:53 +00003364 **/
Bill Pemberton9f9a12f2012-12-03 09:24:25 -05003365static void igb_probe_vfs(struct igb_adapter *adapter)
Alexander Duycka6b623e2009-10-27 23:47:53 +00003366{
3367#ifdef CONFIG_PCI_IOV
3368 struct pci_dev *pdev = adapter->pdev;
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00003369 struct e1000_hw *hw = &adapter->hw;
Alexander Duycka6b623e2009-10-27 23:47:53 +00003370
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00003371 /* Virtualization features not supported on i210 family. */
3372 if ((hw->mac.type == e1000_i210) || (hw->mac.type == e1000_i211))
3373 return;
3374
Jan Beulichbe069982015-10-19 04:23:29 -06003375 /* Of the below we really only want the effect of getting
3376 * IGB_FLAG_HAS_MSIX set (if available), without which
3377 * igb_enable_sriov() has no effect.
3378 */
3379 igb_set_interrupt_capability(adapter, true);
3380 igb_reset_interrupt_capability(adapter);
3381
Greg Rosefa44f2f2013-01-17 01:03:06 -08003382 pci_sriov_set_totalvfs(pdev, 7);
Stefan Assmann6423fc32015-07-10 15:01:12 +02003383 igb_enable_sriov(pdev, max_vfs);
Alexander Duycka6b623e2009-10-27 23:47:53 +00003384
Alexander Duycka6b623e2009-10-27 23:47:53 +00003385#endif /* CONFIG_PCI_IOV */
3386}
3387
Zhang Shengju28cb2d12017-09-19 21:40:54 +08003388unsigned int igb_get_max_rss_queues(struct igb_adapter *adapter)
Auke Kok9d5c8242008-01-24 02:22:38 -08003389{
3390 struct e1000_hw *hw = &adapter->hw;
Zhang Shengju28cb2d12017-09-19 21:40:54 +08003391 unsigned int max_rss_queues;
Auke Kok9d5c8242008-01-24 02:22:38 -08003392
Matthew Vick374a5422012-05-18 04:54:58 +00003393 /* Determine the maximum number of RSS queues supported. */
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00003394 switch (hw->mac.type) {
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00003395 case e1000_i211:
Matthew Vick374a5422012-05-18 04:54:58 +00003396 max_rss_queues = IGB_MAX_RX_QUEUES_I211;
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00003397 break;
Matthew Vick374a5422012-05-18 04:54:58 +00003398 case e1000_82575:
3399 case e1000_i210:
3400 max_rss_queues = IGB_MAX_RX_QUEUES_82575;
3401 break;
3402 case e1000_i350:
3403 /* I350 cannot do RSS and SR-IOV at the same time */
3404 if (!!adapter->vfs_allocated_count) {
3405 max_rss_queues = 1;
3406 break;
3407 }
3408 /* fall through */
3409 case e1000_82576:
3410 if (!!adapter->vfs_allocated_count) {
3411 max_rss_queues = 2;
3412 break;
3413 }
3414 /* fall through */
3415 case e1000_82580:
Carolyn Wybornyceb5f132013-04-18 22:21:30 +00003416 case e1000_i354:
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00003417 default:
Matthew Vick374a5422012-05-18 04:54:58 +00003418 max_rss_queues = IGB_MAX_RX_QUEUES;
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00003419 break;
3420 }
Alexander Duycka99955f2009-11-12 18:37:19 +00003421
Zhang Shengju28cb2d12017-09-19 21:40:54 +08003422 return max_rss_queues;
3423}
3424
3425static void igb_init_queue_configuration(struct igb_adapter *adapter)
3426{
3427 u32 max_rss_queues;
3428
3429 max_rss_queues = igb_get_max_rss_queues(adapter);
Matthew Vick374a5422012-05-18 04:54:58 +00003430 adapter->rss_queues = min_t(u32, max_rss_queues, num_online_cpus());
3431
Shota Suzuki72ddef02015-07-01 09:25:52 +09003432 igb_set_flag_queue_pairs(adapter, max_rss_queues);
3433}
3434
3435void igb_set_flag_queue_pairs(struct igb_adapter *adapter,
3436 const u32 max_rss_queues)
3437{
3438 struct e1000_hw *hw = &adapter->hw;
3439
Matthew Vick374a5422012-05-18 04:54:58 +00003440 /* Determine if we need to pair queues. */
3441 switch (hw->mac.type) {
3442 case e1000_82575:
3443 case e1000_i211:
3444 /* Device supports enough interrupts without queue pairing. */
3445 break;
3446 case e1000_82576:
Matthew Vick374a5422012-05-18 04:54:58 +00003447 case e1000_82580:
3448 case e1000_i350:
Carolyn Wybornyceb5f132013-04-18 22:21:30 +00003449 case e1000_i354:
Matthew Vick374a5422012-05-18 04:54:58 +00003450 case e1000_i210:
3451 default:
Jeff Kirsherb980ac12013-02-23 07:29:56 +00003452 /* If rss_queues > half of max_rss_queues, pair the queues in
Matthew Vick374a5422012-05-18 04:54:58 +00003453 * order to conserve interrupts due to limited supply.
3454 */
3455 if (adapter->rss_queues > (max_rss_queues / 2))
3456 adapter->flags |= IGB_FLAG_QUEUE_PAIRS;
Shota Suzuki37a5d162015-12-11 18:44:00 +09003457 else
3458 adapter->flags &= ~IGB_FLAG_QUEUE_PAIRS;
Matthew Vick374a5422012-05-18 04:54:58 +00003459 break;
3460 }
Greg Rosefa44f2f2013-01-17 01:03:06 -08003461}
3462
3463/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00003464 * igb_sw_init - Initialize general software structures (struct igb_adapter)
3465 * @adapter: board private structure to initialize
Greg Rosefa44f2f2013-01-17 01:03:06 -08003466 *
Jeff Kirsherb980ac12013-02-23 07:29:56 +00003467 * igb_sw_init initializes the Adapter private data structure.
3468 * Fields are initialized based on PCI device information and
3469 * OS network device settings (MTU size).
Greg Rosefa44f2f2013-01-17 01:03:06 -08003470 **/
3471static int igb_sw_init(struct igb_adapter *adapter)
3472{
3473 struct e1000_hw *hw = &adapter->hw;
3474 struct net_device *netdev = adapter->netdev;
3475 struct pci_dev *pdev = adapter->pdev;
3476
3477 pci_read_config_word(pdev, PCI_COMMAND, &hw->bus.pci_cmd_word);
3478
3479 /* set default ring sizes */
3480 adapter->tx_ring_count = IGB_DEFAULT_TXD;
3481 adapter->rx_ring_count = IGB_DEFAULT_RXD;
3482
3483 /* set default ITR values */
3484 adapter->rx_itr_setting = IGB_DEFAULT_ITR;
3485 adapter->tx_itr_setting = IGB_DEFAULT_ITR;
3486
3487 /* set default work limits */
3488 adapter->tx_work_limit = IGB_DEFAULT_TX_WORK;
3489
3490 adapter->max_frame_size = netdev->mtu + ETH_HLEN + ETH_FCS_LEN +
3491 VLAN_HLEN;
3492 adapter->min_frame_size = ETH_ZLEN + ETH_FCS_LEN;
3493
Gangfeng Huang0e71def2016-07-06 13:22:54 +08003494 spin_lock_init(&adapter->nfc_lock);
Greg Rosefa44f2f2013-01-17 01:03:06 -08003495 spin_lock_init(&adapter->stats64_lock);
3496#ifdef CONFIG_PCI_IOV
3497 switch (hw->mac.type) {
3498 case e1000_82576:
3499 case e1000_i350:
3500 if (max_vfs > 7) {
3501 dev_warn(&pdev->dev,
3502 "Maximum of 7 VFs per PF, using max\n");
Alex Williamsond0f63ac2013-03-13 15:50:24 +00003503 max_vfs = adapter->vfs_allocated_count = 7;
Greg Rosefa44f2f2013-01-17 01:03:06 -08003504 } else
3505 adapter->vfs_allocated_count = max_vfs;
3506 if (adapter->vfs_allocated_count)
3507 dev_warn(&pdev->dev,
3508 "Enabling SR-IOV VFs using the module parameter is deprecated - please use the pci sysfs interface.\n");
3509 break;
3510 default:
3511 break;
3512 }
3513#endif /* CONFIG_PCI_IOV */
3514
Stefan Assmanncbfe3602015-09-17 14:46:10 +02003515 /* Assume MSI-X interrupts, will be checked during IRQ allocation */
3516 adapter->flags |= IGB_FLAG_HAS_MSIX;
3517
Yury Kylulin83c21332017-03-07 11:20:25 +03003518 adapter->mac_table = kzalloc(sizeof(struct igb_mac_addr) *
3519 hw->mac.rar_entry_count, GFP_ATOMIC);
3520 if (!adapter->mac_table)
3521 return -ENOMEM;
3522
Todd Fujinakaceee3452015-08-07 17:27:39 -07003523 igb_probe_vfs(adapter);
3524
Greg Rosefa44f2f2013-01-17 01:03:06 -08003525 igb_init_queue_configuration(adapter);
Alexander Duycka99955f2009-11-12 18:37:19 +00003526
Carolyn Wyborny1128c752011-10-14 00:13:49 +00003527 /* Setup and initialize a copy of the hw vlan table array */
Joe Perchesb2adaca2013-02-03 17:43:58 +00003528 adapter->shadow_vfta = kcalloc(E1000_VLAN_FILTER_TBL_SIZE, sizeof(u32),
3529 GFP_ATOMIC);
Christophe JAILLET18eb8632017-08-27 08:39:51 +02003530 if (!adapter->shadow_vfta)
3531 return -ENOMEM;
Carolyn Wyborny1128c752011-10-14 00:13:49 +00003532
Alexander Duycka6b623e2009-10-27 23:47:53 +00003533 /* This call may decrease the number of queues */
Stefan Assmann53c7d062012-12-04 06:00:12 +00003534 if (igb_init_interrupt_scheme(adapter, true)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08003535 dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
3536 return -ENOMEM;
3537 }
3538
3539 /* Explicitly disable IRQ since the NIC can be in any state. */
3540 igb_irq_disable(adapter);
3541
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00003542 if (hw->mac.type >= e1000_i350)
Carolyn Wyborny831ec0b2011-03-11 20:43:54 -08003543 adapter->flags &= ~IGB_FLAG_DMAC;
3544
Auke Kok9d5c8242008-01-24 02:22:38 -08003545 set_bit(__IGB_DOWN, &adapter->state);
3546 return 0;
3547}
3548
3549/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00003550 * igb_open - Called when a network interface is made active
3551 * @netdev: network interface device structure
Auke Kok9d5c8242008-01-24 02:22:38 -08003552 *
Jeff Kirsherb980ac12013-02-23 07:29:56 +00003553 * Returns 0 on success, negative value on failure
Auke Kok9d5c8242008-01-24 02:22:38 -08003554 *
Jeff Kirsherb980ac12013-02-23 07:29:56 +00003555 * The open entry point is called when a network interface is made
3556 * active by the system (IFF_UP). At this point all resources needed
3557 * for transmit and receive operations are allocated, the interrupt
3558 * handler is registered with the OS, the watchdog timer is started,
3559 * and the stack is notified that the interface is ready.
Auke Kok9d5c8242008-01-24 02:22:38 -08003560 **/
Yan, Zheng749ab2c2012-01-04 20:23:37 +00003561static int __igb_open(struct net_device *netdev, bool resuming)
Auke Kok9d5c8242008-01-24 02:22:38 -08003562{
3563 struct igb_adapter *adapter = netdev_priv(netdev);
3564 struct e1000_hw *hw = &adapter->hw;
Yan, Zheng749ab2c2012-01-04 20:23:37 +00003565 struct pci_dev *pdev = adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08003566 int err;
3567 int i;
3568
3569 /* disallow open during test */
Yan, Zheng749ab2c2012-01-04 20:23:37 +00003570 if (test_bit(__IGB_TESTING, &adapter->state)) {
3571 WARN_ON(resuming);
Auke Kok9d5c8242008-01-24 02:22:38 -08003572 return -EBUSY;
Yan, Zheng749ab2c2012-01-04 20:23:37 +00003573 }
3574
3575 if (!resuming)
3576 pm_runtime_get_sync(&pdev->dev);
Auke Kok9d5c8242008-01-24 02:22:38 -08003577
Jesse Brandeburgb168dfc2009-04-17 20:44:32 +00003578 netif_carrier_off(netdev);
3579
Auke Kok9d5c8242008-01-24 02:22:38 -08003580 /* allocate transmit descriptors */
3581 err = igb_setup_all_tx_resources(adapter);
3582 if (err)
3583 goto err_setup_tx;
3584
3585 /* allocate receive descriptors */
3586 err = igb_setup_all_rx_resources(adapter);
3587 if (err)
3588 goto err_setup_rx;
3589
Nick Nunley88a268c2010-02-17 01:01:59 +00003590 igb_power_up_link(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08003591
Auke Kok9d5c8242008-01-24 02:22:38 -08003592 /* before we allocate an interrupt, we must be ready to handle it.
3593 * Setting DEBUG_SHIRQ in the kernel makes it fire an interrupt
3594 * as soon as we call pci_request_irq, so we have to setup our
Jeff Kirsherb980ac12013-02-23 07:29:56 +00003595 * clean_rx handler before we do so.
3596 */
Auke Kok9d5c8242008-01-24 02:22:38 -08003597 igb_configure(adapter);
3598
3599 err = igb_request_irq(adapter);
3600 if (err)
3601 goto err_req_irq;
3602
Alexander Duyck0c2cc022012-09-25 00:31:22 +00003603 /* Notify the stack of the actual queue counts. */
3604 err = netif_set_real_num_tx_queues(adapter->netdev,
3605 adapter->num_tx_queues);
3606 if (err)
3607 goto err_set_queues;
3608
3609 err = netif_set_real_num_rx_queues(adapter->netdev,
3610 adapter->num_rx_queues);
3611 if (err)
3612 goto err_set_queues;
3613
Auke Kok9d5c8242008-01-24 02:22:38 -08003614 /* From here on the code is the same as igb_up() */
3615 clear_bit(__IGB_DOWN, &adapter->state);
3616
Alexander Duyck0d1ae7f2011-08-26 07:46:34 +00003617 for (i = 0; i < adapter->num_q_vectors; i++)
3618 napi_enable(&(adapter->q_vector[i]->napi));
Auke Kok9d5c8242008-01-24 02:22:38 -08003619
3620 /* Clear any pending interrupts. */
3621 rd32(E1000_ICR);
PJ Waskiewicz844290e2008-06-27 11:00:39 -07003622
3623 igb_irq_enable(adapter);
3624
Alexander Duyckd4960302009-10-27 15:53:45 +00003625 /* notify VFs that reset has been completed */
3626 if (adapter->vfs_allocated_count) {
3627 u32 reg_data = rd32(E1000_CTRL_EXT);
Carolyn Wyborny9005df32014-04-11 01:45:34 +00003628
Alexander Duyckd4960302009-10-27 15:53:45 +00003629 reg_data |= E1000_CTRL_EXT_PFRSTD;
3630 wr32(E1000_CTRL_EXT, reg_data);
3631 }
3632
Jeff Kirsherd55b53f2008-07-18 04:33:03 -07003633 netif_tx_start_all_queues(netdev);
3634
Yan, Zheng749ab2c2012-01-04 20:23:37 +00003635 if (!resuming)
3636 pm_runtime_put(&pdev->dev);
3637
Alexander Duyck25568a52009-10-27 23:49:59 +00003638 /* start the watchdog. */
3639 hw->mac.get_link_status = 1;
3640 schedule_work(&adapter->watchdog_task);
Auke Kok9d5c8242008-01-24 02:22:38 -08003641
3642 return 0;
3643
Alexander Duyck0c2cc022012-09-25 00:31:22 +00003644err_set_queues:
3645 igb_free_irq(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08003646err_req_irq:
3647 igb_release_hw_control(adapter);
Nick Nunley88a268c2010-02-17 01:01:59 +00003648 igb_power_down_link(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08003649 igb_free_all_rx_resources(adapter);
3650err_setup_rx:
3651 igb_free_all_tx_resources(adapter);
3652err_setup_tx:
3653 igb_reset(adapter);
Yan, Zheng749ab2c2012-01-04 20:23:37 +00003654 if (!resuming)
3655 pm_runtime_put(&pdev->dev);
Auke Kok9d5c8242008-01-24 02:22:38 -08003656
3657 return err;
3658}
3659
Stefan Assmann46eafa52016-02-03 09:20:50 +01003660int igb_open(struct net_device *netdev)
Yan, Zheng749ab2c2012-01-04 20:23:37 +00003661{
3662 return __igb_open(netdev, false);
3663}
3664
Auke Kok9d5c8242008-01-24 02:22:38 -08003665/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00003666 * igb_close - Disables a network interface
3667 * @netdev: network interface device structure
Auke Kok9d5c8242008-01-24 02:22:38 -08003668 *
Jeff Kirsherb980ac12013-02-23 07:29:56 +00003669 * Returns 0, this is not allowed to fail
Auke Kok9d5c8242008-01-24 02:22:38 -08003670 *
Jeff Kirsherb980ac12013-02-23 07:29:56 +00003671 * The close entry point is called when an interface is de-activated
3672 * by the OS. The hardware is still under the driver's control, but
3673 * needs to be disabled. A global MAC reset is issued to stop the
3674 * hardware, and all transmit and receive resources are freed.
Auke Kok9d5c8242008-01-24 02:22:38 -08003675 **/
Yan, Zheng749ab2c2012-01-04 20:23:37 +00003676static int __igb_close(struct net_device *netdev, bool suspending)
Auke Kok9d5c8242008-01-24 02:22:38 -08003677{
3678 struct igb_adapter *adapter = netdev_priv(netdev);
Yan, Zheng749ab2c2012-01-04 20:23:37 +00003679 struct pci_dev *pdev = adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08003680
3681 WARN_ON(test_bit(__IGB_RESETTING, &adapter->state));
Auke Kok9d5c8242008-01-24 02:22:38 -08003682
Yan, Zheng749ab2c2012-01-04 20:23:37 +00003683 if (!suspending)
3684 pm_runtime_get_sync(&pdev->dev);
3685
3686 igb_down(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08003687 igb_free_irq(adapter);
3688
3689 igb_free_all_tx_resources(adapter);
3690 igb_free_all_rx_resources(adapter);
3691
Yan, Zheng749ab2c2012-01-04 20:23:37 +00003692 if (!suspending)
3693 pm_runtime_put_sync(&pdev->dev);
Auke Kok9d5c8242008-01-24 02:22:38 -08003694 return 0;
3695}
3696
Stefan Assmann46eafa52016-02-03 09:20:50 +01003697int igb_close(struct net_device *netdev)
Yan, Zheng749ab2c2012-01-04 20:23:37 +00003698{
Lyude Paul888f2292017-12-12 14:31:30 -05003699 if (netif_device_present(netdev) || netdev->dismantle)
Todd Fujinaka94749332016-11-15 08:54:26 -08003700 return __igb_close(netdev, false);
3701 return 0;
Yan, Zheng749ab2c2012-01-04 20:23:37 +00003702}
3703
Auke Kok9d5c8242008-01-24 02:22:38 -08003704/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00003705 * igb_setup_tx_resources - allocate Tx resources (Descriptors)
3706 * @tx_ring: tx descriptor ring (for a specific queue) to setup
Auke Kok9d5c8242008-01-24 02:22:38 -08003707 *
Jeff Kirsherb980ac12013-02-23 07:29:56 +00003708 * Return 0 on success, negative on failure
Auke Kok9d5c8242008-01-24 02:22:38 -08003709 **/
Alexander Duyck80785292009-10-27 15:51:47 +00003710int igb_setup_tx_resources(struct igb_ring *tx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08003711{
Alexander Duyck59d71982010-04-27 13:09:25 +00003712 struct device *dev = tx_ring->dev;
Auke Kok9d5c8242008-01-24 02:22:38 -08003713 int size;
3714
Alexander Duyck06034642011-08-26 07:44:22 +00003715 size = sizeof(struct igb_tx_buffer) * tx_ring->count;
Alexander Duyckf33005a2012-09-13 06:27:55 +00003716
Alexander Duyck7cc6fd42017-02-06 18:26:02 -08003717 tx_ring->tx_buffer_info = vmalloc(size);
Alexander Duyck06034642011-08-26 07:44:22 +00003718 if (!tx_ring->tx_buffer_info)
Auke Kok9d5c8242008-01-24 02:22:38 -08003719 goto err;
Auke Kok9d5c8242008-01-24 02:22:38 -08003720
3721 /* round up to nearest 4K */
Alexander Duyck85e8d002009-02-16 00:00:20 -08003722 tx_ring->size = tx_ring->count * sizeof(union e1000_adv_tx_desc);
Auke Kok9d5c8242008-01-24 02:22:38 -08003723 tx_ring->size = ALIGN(tx_ring->size, 4096);
3724
Alexander Duyck5536d212012-09-25 00:31:17 +00003725 tx_ring->desc = dma_alloc_coherent(dev, tx_ring->size,
3726 &tx_ring->dma, GFP_KERNEL);
Auke Kok9d5c8242008-01-24 02:22:38 -08003727 if (!tx_ring->desc)
3728 goto err;
3729
Auke Kok9d5c8242008-01-24 02:22:38 -08003730 tx_ring->next_to_use = 0;
3731 tx_ring->next_to_clean = 0;
Alexander Duyck81c2fc22011-08-26 07:45:20 +00003732
Auke Kok9d5c8242008-01-24 02:22:38 -08003733 return 0;
3734
3735err:
Alexander Duyck06034642011-08-26 07:44:22 +00003736 vfree(tx_ring->tx_buffer_info);
Alexander Duyckf33005a2012-09-13 06:27:55 +00003737 tx_ring->tx_buffer_info = NULL;
3738 dev_err(dev, "Unable to allocate memory for the Tx descriptor ring\n");
Auke Kok9d5c8242008-01-24 02:22:38 -08003739 return -ENOMEM;
3740}
3741
3742/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00003743 * igb_setup_all_tx_resources - wrapper to allocate Tx resources
3744 * (Descriptors) for all queues
3745 * @adapter: board private structure
Auke Kok9d5c8242008-01-24 02:22:38 -08003746 *
Jeff Kirsherb980ac12013-02-23 07:29:56 +00003747 * Return 0 on success, negative on failure
Auke Kok9d5c8242008-01-24 02:22:38 -08003748 **/
3749static int igb_setup_all_tx_resources(struct igb_adapter *adapter)
3750{
Alexander Duyck439705e2009-10-27 23:49:20 +00003751 struct pci_dev *pdev = adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08003752 int i, err = 0;
3753
3754 for (i = 0; i < adapter->num_tx_queues; i++) {
Alexander Duyck3025a442010-02-17 01:02:39 +00003755 err = igb_setup_tx_resources(adapter->tx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08003756 if (err) {
Alexander Duyck439705e2009-10-27 23:49:20 +00003757 dev_err(&pdev->dev,
Auke Kok9d5c8242008-01-24 02:22:38 -08003758 "Allocation for Tx Queue %u failed\n", i);
3759 for (i--; i >= 0; i--)
Alexander Duyck3025a442010-02-17 01:02:39 +00003760 igb_free_tx_resources(adapter->tx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08003761 break;
3762 }
3763 }
3764
3765 return err;
3766}
3767
3768/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00003769 * igb_setup_tctl - configure the transmit control registers
3770 * @adapter: Board private structure
Auke Kok9d5c8242008-01-24 02:22:38 -08003771 **/
Alexander Duyckd7ee5b32009-10-27 15:54:23 +00003772void igb_setup_tctl(struct igb_adapter *adapter)
Auke Kok9d5c8242008-01-24 02:22:38 -08003773{
Auke Kok9d5c8242008-01-24 02:22:38 -08003774 struct e1000_hw *hw = &adapter->hw;
3775 u32 tctl;
Auke Kok9d5c8242008-01-24 02:22:38 -08003776
Alexander Duyck85b430b2009-10-27 15:50:29 +00003777 /* disable queue 0 which is enabled by default on 82575 and 82576 */
3778 wr32(E1000_TXDCTL(0), 0);
Auke Kok9d5c8242008-01-24 02:22:38 -08003779
3780 /* Program the Transmit Control Register */
Auke Kok9d5c8242008-01-24 02:22:38 -08003781 tctl = rd32(E1000_TCTL);
3782 tctl &= ~E1000_TCTL_CT;
3783 tctl |= E1000_TCTL_PSP | E1000_TCTL_RTLC |
3784 (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
3785
3786 igb_config_collision_dist(hw);
3787
Auke Kok9d5c8242008-01-24 02:22:38 -08003788 /* Enable transmits */
3789 tctl |= E1000_TCTL_EN;
3790
3791 wr32(E1000_TCTL, tctl);
3792}
3793
3794/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00003795 * igb_configure_tx_ring - Configure transmit ring after Reset
3796 * @adapter: board private structure
3797 * @ring: tx ring to configure
Alexander Duyck85b430b2009-10-27 15:50:29 +00003798 *
Jeff Kirsherb980ac12013-02-23 07:29:56 +00003799 * Configure a transmit ring after a reset.
Alexander Duyck85b430b2009-10-27 15:50:29 +00003800 **/
Alexander Duyckd7ee5b32009-10-27 15:54:23 +00003801void igb_configure_tx_ring(struct igb_adapter *adapter,
Carolyn Wyborny9005df32014-04-11 01:45:34 +00003802 struct igb_ring *ring)
Alexander Duyck85b430b2009-10-27 15:50:29 +00003803{
3804 struct e1000_hw *hw = &adapter->hw;
Alexander Duycka74420e2011-08-26 07:43:27 +00003805 u32 txdctl = 0;
Alexander Duyck85b430b2009-10-27 15:50:29 +00003806 u64 tdba = ring->dma;
3807 int reg_idx = ring->reg_idx;
3808
3809 /* disable the queue */
Alexander Duycka74420e2011-08-26 07:43:27 +00003810 wr32(E1000_TXDCTL(reg_idx), 0);
Alexander Duyck85b430b2009-10-27 15:50:29 +00003811 wrfl();
3812 mdelay(10);
3813
3814 wr32(E1000_TDLEN(reg_idx),
Jeff Kirsherb980ac12013-02-23 07:29:56 +00003815 ring->count * sizeof(union e1000_adv_tx_desc));
Alexander Duyck85b430b2009-10-27 15:50:29 +00003816 wr32(E1000_TDBAL(reg_idx),
Jeff Kirsherb980ac12013-02-23 07:29:56 +00003817 tdba & 0x00000000ffffffffULL);
Alexander Duyck85b430b2009-10-27 15:50:29 +00003818 wr32(E1000_TDBAH(reg_idx), tdba >> 32);
3819
Cao jin629823b2016-11-08 15:06:20 +08003820 ring->tail = adapter->io_addr + E1000_TDT(reg_idx);
Alexander Duycka74420e2011-08-26 07:43:27 +00003821 wr32(E1000_TDH(reg_idx), 0);
Alexander Duyckfce99e32009-10-27 15:51:27 +00003822 writel(0, ring->tail);
Alexander Duyck85b430b2009-10-27 15:50:29 +00003823
3824 txdctl |= IGB_TX_PTHRESH;
3825 txdctl |= IGB_TX_HTHRESH << 8;
3826 txdctl |= IGB_TX_WTHRESH << 16;
3827
Alexander Duyck7cc6fd42017-02-06 18:26:02 -08003828 /* reinitialize tx_buffer_info */
3829 memset(ring->tx_buffer_info, 0,
3830 sizeof(struct igb_tx_buffer) * ring->count);
3831
Alexander Duyck85b430b2009-10-27 15:50:29 +00003832 txdctl |= E1000_TXDCTL_QUEUE_ENABLE;
3833 wr32(E1000_TXDCTL(reg_idx), txdctl);
3834}
3835
3836/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00003837 * igb_configure_tx - Configure transmit Unit after Reset
3838 * @adapter: board private structure
Alexander Duyck85b430b2009-10-27 15:50:29 +00003839 *
Jeff Kirsherb980ac12013-02-23 07:29:56 +00003840 * Configure the Tx unit of the MAC after a reset.
Alexander Duyck85b430b2009-10-27 15:50:29 +00003841 **/
3842static void igb_configure_tx(struct igb_adapter *adapter)
3843{
3844 int i;
3845
3846 for (i = 0; i < adapter->num_tx_queues; i++)
Alexander Duyck3025a442010-02-17 01:02:39 +00003847 igb_configure_tx_ring(adapter, adapter->tx_ring[i]);
Alexander Duyck85b430b2009-10-27 15:50:29 +00003848}
3849
3850/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00003851 * igb_setup_rx_resources - allocate Rx resources (Descriptors)
3852 * @rx_ring: Rx descriptor ring (for a specific queue) to setup
Auke Kok9d5c8242008-01-24 02:22:38 -08003853 *
Jeff Kirsherb980ac12013-02-23 07:29:56 +00003854 * Returns 0 on success, negative on failure
Auke Kok9d5c8242008-01-24 02:22:38 -08003855 **/
Alexander Duyck80785292009-10-27 15:51:47 +00003856int igb_setup_rx_resources(struct igb_ring *rx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08003857{
Alexander Duyck59d71982010-04-27 13:09:25 +00003858 struct device *dev = rx_ring->dev;
Alexander Duyckf33005a2012-09-13 06:27:55 +00003859 int size;
Auke Kok9d5c8242008-01-24 02:22:38 -08003860
Alexander Duyck06034642011-08-26 07:44:22 +00003861 size = sizeof(struct igb_rx_buffer) * rx_ring->count;
Alexander Duyckf33005a2012-09-13 06:27:55 +00003862
Alexander Duyckd2bead52017-02-06 18:25:50 -08003863 rx_ring->rx_buffer_info = vmalloc(size);
Alexander Duyck06034642011-08-26 07:44:22 +00003864 if (!rx_ring->rx_buffer_info)
Auke Kok9d5c8242008-01-24 02:22:38 -08003865 goto err;
Auke Kok9d5c8242008-01-24 02:22:38 -08003866
Auke Kok9d5c8242008-01-24 02:22:38 -08003867 /* Round up to nearest 4K */
Alexander Duyckf33005a2012-09-13 06:27:55 +00003868 rx_ring->size = rx_ring->count * sizeof(union e1000_adv_rx_desc);
Auke Kok9d5c8242008-01-24 02:22:38 -08003869 rx_ring->size = ALIGN(rx_ring->size, 4096);
3870
Alexander Duyck5536d212012-09-25 00:31:17 +00003871 rx_ring->desc = dma_alloc_coherent(dev, rx_ring->size,
3872 &rx_ring->dma, GFP_KERNEL);
Auke Kok9d5c8242008-01-24 02:22:38 -08003873 if (!rx_ring->desc)
3874 goto err;
3875
Alexander Duyckcbc8e552012-09-25 00:31:02 +00003876 rx_ring->next_to_alloc = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08003877 rx_ring->next_to_clean = 0;
3878 rx_ring->next_to_use = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08003879
Auke Kok9d5c8242008-01-24 02:22:38 -08003880 return 0;
3881
3882err:
Alexander Duyck06034642011-08-26 07:44:22 +00003883 vfree(rx_ring->rx_buffer_info);
3884 rx_ring->rx_buffer_info = NULL;
Alexander Duyckf33005a2012-09-13 06:27:55 +00003885 dev_err(dev, "Unable to allocate memory for the Rx descriptor ring\n");
Auke Kok9d5c8242008-01-24 02:22:38 -08003886 return -ENOMEM;
3887}
3888
3889/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00003890 * igb_setup_all_rx_resources - wrapper to allocate Rx resources
3891 * (Descriptors) for all queues
3892 * @adapter: board private structure
Auke Kok9d5c8242008-01-24 02:22:38 -08003893 *
Jeff Kirsherb980ac12013-02-23 07:29:56 +00003894 * Return 0 on success, negative on failure
Auke Kok9d5c8242008-01-24 02:22:38 -08003895 **/
3896static int igb_setup_all_rx_resources(struct igb_adapter *adapter)
3897{
Alexander Duyck439705e2009-10-27 23:49:20 +00003898 struct pci_dev *pdev = adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08003899 int i, err = 0;
3900
3901 for (i = 0; i < adapter->num_rx_queues; i++) {
Alexander Duyck3025a442010-02-17 01:02:39 +00003902 err = igb_setup_rx_resources(adapter->rx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08003903 if (err) {
Alexander Duyck439705e2009-10-27 23:49:20 +00003904 dev_err(&pdev->dev,
Auke Kok9d5c8242008-01-24 02:22:38 -08003905 "Allocation for Rx Queue %u failed\n", i);
3906 for (i--; i >= 0; i--)
Alexander Duyck3025a442010-02-17 01:02:39 +00003907 igb_free_rx_resources(adapter->rx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08003908 break;
3909 }
3910 }
3911
3912 return err;
3913}
3914
3915/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00003916 * igb_setup_mrqc - configure the multiple receive queue control registers
3917 * @adapter: Board private structure
Alexander Duyck06cf2662009-10-27 15:53:25 +00003918 **/
3919static void igb_setup_mrqc(struct igb_adapter *adapter)
3920{
3921 struct e1000_hw *hw = &adapter->hw;
3922 u32 mrqc, rxcsum;
Laura Mihaela Vasilescued12cc92013-07-31 20:19:54 +00003923 u32 j, num_rx_queues;
Eric Dumazeteb31f842014-11-16 06:23:14 -08003924 u32 rss_key[10];
Alexander Duyck06cf2662009-10-27 15:53:25 +00003925
Eric Dumazeteb31f842014-11-16 06:23:14 -08003926 netdev_rss_key_fill(rss_key, sizeof(rss_key));
Alexander Duycka57fe232012-09-13 06:28:16 +00003927 for (j = 0; j < 10; j++)
Eric Dumazeteb31f842014-11-16 06:23:14 -08003928 wr32(E1000_RSSRK(j), rss_key[j]);
Alexander Duyck06cf2662009-10-27 15:53:25 +00003929
Alexander Duycka99955f2009-11-12 18:37:19 +00003930 num_rx_queues = adapter->rss_queues;
Alexander Duyck06cf2662009-10-27 15:53:25 +00003931
Alexander Duyck797fd4b2012-09-13 06:28:11 +00003932 switch (hw->mac.type) {
Alexander Duyck797fd4b2012-09-13 06:28:11 +00003933 case e1000_82576:
3934 /* 82576 supports 2 RSS queues for SR-IOV */
Laura Mihaela Vasilescued12cc92013-07-31 20:19:54 +00003935 if (adapter->vfs_allocated_count)
Alexander Duyck06cf2662009-10-27 15:53:25 +00003936 num_rx_queues = 2;
Alexander Duyck797fd4b2012-09-13 06:28:11 +00003937 break;
3938 default:
3939 break;
Alexander Duyck06cf2662009-10-27 15:53:25 +00003940 }
3941
Laura Mihaela Vasilescued12cc92013-07-31 20:19:54 +00003942 if (adapter->rss_indir_tbl_init != num_rx_queues) {
3943 for (j = 0; j < IGB_RETA_SIZE; j++)
Carolyn Wybornyc502ea22014-04-11 01:46:33 +00003944 adapter->rss_indir_tbl[j] =
3945 (j * num_rx_queues) / IGB_RETA_SIZE;
Laura Mihaela Vasilescued12cc92013-07-31 20:19:54 +00003946 adapter->rss_indir_tbl_init = num_rx_queues;
Alexander Duyck06cf2662009-10-27 15:53:25 +00003947 }
Laura Mihaela Vasilescued12cc92013-07-31 20:19:54 +00003948 igb_write_rss_indir_tbl(adapter);
Alexander Duyck06cf2662009-10-27 15:53:25 +00003949
Jeff Kirsherb980ac12013-02-23 07:29:56 +00003950 /* Disable raw packet checksumming so that RSS hash is placed in
Alexander Duyck06cf2662009-10-27 15:53:25 +00003951 * descriptor on writeback. No need to enable TCP/UDP/IP checksum
3952 * offloads as they are enabled by default
3953 */
3954 rxcsum = rd32(E1000_RXCSUM);
3955 rxcsum |= E1000_RXCSUM_PCSD;
3956
3957 if (adapter->hw.mac.type >= e1000_82576)
3958 /* Enable Receive Checksum Offload for SCTP */
3959 rxcsum |= E1000_RXCSUM_CRCOFL;
3960
3961 /* Don't need to set TUOFL or IPOFL, they default to 1 */
3962 wr32(E1000_RXCSUM, rxcsum);
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00003963
Akeem G. Abodunrin039454a2012-11-13 04:03:21 +00003964 /* Generate RSS hash based on packet types, TCP/UDP
3965 * port numbers and/or IPv4/v6 src and dst addresses
3966 */
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00003967 mrqc = E1000_MRQC_RSS_FIELD_IPV4 |
3968 E1000_MRQC_RSS_FIELD_IPV4_TCP |
3969 E1000_MRQC_RSS_FIELD_IPV6 |
3970 E1000_MRQC_RSS_FIELD_IPV6_TCP |
3971 E1000_MRQC_RSS_FIELD_IPV6_TCP_EX;
Alexander Duyck06cf2662009-10-27 15:53:25 +00003972
Akeem G. Abodunrin039454a2012-11-13 04:03:21 +00003973 if (adapter->flags & IGB_FLAG_RSS_FIELD_IPV4_UDP)
3974 mrqc |= E1000_MRQC_RSS_FIELD_IPV4_UDP;
3975 if (adapter->flags & IGB_FLAG_RSS_FIELD_IPV6_UDP)
3976 mrqc |= E1000_MRQC_RSS_FIELD_IPV6_UDP;
3977
Alexander Duyck06cf2662009-10-27 15:53:25 +00003978 /* If VMDq is enabled then we set the appropriate mode for that, else
3979 * we default to RSS so that an RSS hash is calculated per packet even
Jeff Kirsherb980ac12013-02-23 07:29:56 +00003980 * if we are only using one queue
3981 */
Alexander Duyck06cf2662009-10-27 15:53:25 +00003982 if (adapter->vfs_allocated_count) {
3983 if (hw->mac.type > e1000_82575) {
3984 /* Set the default pool for the PF's first queue */
3985 u32 vtctl = rd32(E1000_VT_CTL);
Carolyn Wyborny9005df32014-04-11 01:45:34 +00003986
Alexander Duyck06cf2662009-10-27 15:53:25 +00003987 vtctl &= ~(E1000_VT_CTL_DEFAULT_POOL_MASK |
3988 E1000_VT_CTL_DISABLE_DEF_POOL);
3989 vtctl |= adapter->vfs_allocated_count <<
3990 E1000_VT_CTL_DEFAULT_POOL_SHIFT;
3991 wr32(E1000_VT_CTL, vtctl);
3992 }
Alexander Duycka99955f2009-11-12 18:37:19 +00003993 if (adapter->rss_queues > 1)
Todd Fujinakac883de92016-01-11 09:34:50 -08003994 mrqc |= E1000_MRQC_ENABLE_VMDQ_RSS_MQ;
Alexander Duyck06cf2662009-10-27 15:53:25 +00003995 else
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00003996 mrqc |= E1000_MRQC_ENABLE_VMDQ;
Alexander Duyck06cf2662009-10-27 15:53:25 +00003997 } else {
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00003998 if (hw->mac.type != e1000_i211)
Todd Fujinakac883de92016-01-11 09:34:50 -08003999 mrqc |= E1000_MRQC_ENABLE_RSS_MQ;
Alexander Duyck06cf2662009-10-27 15:53:25 +00004000 }
4001 igb_vmm_control(adapter);
4002
Alexander Duyck06cf2662009-10-27 15:53:25 +00004003 wr32(E1000_MRQC, mrqc);
4004}
4005
4006/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00004007 * igb_setup_rctl - configure the receive control registers
4008 * @adapter: Board private structure
Auke Kok9d5c8242008-01-24 02:22:38 -08004009 **/
Alexander Duyckd7ee5b32009-10-27 15:54:23 +00004010void igb_setup_rctl(struct igb_adapter *adapter)
Auke Kok9d5c8242008-01-24 02:22:38 -08004011{
4012 struct e1000_hw *hw = &adapter->hw;
4013 u32 rctl;
Auke Kok9d5c8242008-01-24 02:22:38 -08004014
4015 rctl = rd32(E1000_RCTL);
4016
4017 rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
Alexander Duyck69d728b2008-11-25 01:04:03 -08004018 rctl &= ~(E1000_RCTL_LBM_TCVR | E1000_RCTL_LBM_MAC);
Auke Kok9d5c8242008-01-24 02:22:38 -08004019
Alexander Duyck69d728b2008-11-25 01:04:03 -08004020 rctl |= E1000_RCTL_EN | E1000_RCTL_BAM | E1000_RCTL_RDMTS_HALF |
Alexander Duyck28b07592009-02-06 23:20:31 +00004021 (hw->mac.mc_filter_type << E1000_RCTL_MO_SHIFT);
Auke Kok9d5c8242008-01-24 02:22:38 -08004022
Jeff Kirsherb980ac12013-02-23 07:29:56 +00004023 /* enable stripping of CRC. It's unlikely this will break BMC
Auke Kok87cb7e82008-07-08 15:08:29 -07004024 * redirection as it did with e1000. Newer features require
4025 * that the HW strips the CRC.
Alexander Duyck73cd78f2009-02-12 18:16:59 +00004026 */
Auke Kok87cb7e82008-07-08 15:08:29 -07004027 rctl |= E1000_RCTL_SECRC;
Auke Kok9d5c8242008-01-24 02:22:38 -08004028
Alexander Duyck559e9c42009-10-27 23:52:50 +00004029 /* disable store bad packets and clear size bits. */
Alexander Duyckec54d7d2009-01-31 00:52:57 -08004030 rctl &= ~(E1000_RCTL_SBP | E1000_RCTL_SZ_256);
Auke Kok9d5c8242008-01-24 02:22:38 -08004031
Alexander Duyck45693bc2016-01-06 23:10:39 -08004032 /* enable LPE to allow for reception of jumbo frames */
Alexander Duyck6ec43fe2009-10-27 15:50:48 +00004033 rctl |= E1000_RCTL_LPE;
Auke Kok9d5c8242008-01-24 02:22:38 -08004034
Alexander Duyck952f72a2009-10-27 15:51:07 +00004035 /* disable queue 0 to prevent tail write w/o re-config */
4036 wr32(E1000_RXDCTL(0), 0);
Auke Kok9d5c8242008-01-24 02:22:38 -08004037
Alexander Duycke1739522009-02-19 20:39:44 -08004038 /* Attention!!! For SR-IOV PF driver operations you must enable
4039 * queue drop for all VF and PF queues to prevent head of line blocking
4040 * if an un-trusted VF does not provide descriptors to hardware.
4041 */
4042 if (adapter->vfs_allocated_count) {
Alexander Duycke1739522009-02-19 20:39:44 -08004043 /* set all queue drop enable bits */
4044 wr32(E1000_QDE, ALL_QUEUES);
Alexander Duycke1739522009-02-19 20:39:44 -08004045 }
4046
Ben Greear89eaefb2012-03-06 09:41:58 +00004047 /* This is useful for sniffing bad packets. */
4048 if (adapter->netdev->features & NETIF_F_RXALL) {
4049 /* UPE and MPE will be handled by normal PROMISC logic
Jeff Kirsherb980ac12013-02-23 07:29:56 +00004050 * in e1000e_set_rx_mode
4051 */
Ben Greear89eaefb2012-03-06 09:41:58 +00004052 rctl |= (E1000_RCTL_SBP | /* Receive bad packets */
4053 E1000_RCTL_BAM | /* RX All Bcast Pkts */
4054 E1000_RCTL_PMCF); /* RX All MAC Ctrl Pkts */
4055
Alexander Duyck16903ca2016-01-06 23:11:18 -08004056 rctl &= ~(E1000_RCTL_DPF | /* Allow filtered pause */
Ben Greear89eaefb2012-03-06 09:41:58 +00004057 E1000_RCTL_CFIEN); /* Dis VLAN CFIEN Filter */
4058 /* Do not mess with E1000_CTRL_VME, it affects transmit as well,
4059 * and that breaks VLANs.
4060 */
4061 }
4062
Auke Kok9d5c8242008-01-24 02:22:38 -08004063 wr32(E1000_RCTL, rctl);
4064}
4065
Alexander Duyck7d5753f2009-10-27 23:47:16 +00004066static inline int igb_set_vf_rlpml(struct igb_adapter *adapter, int size,
Carolyn Wyborny9005df32014-04-11 01:45:34 +00004067 int vfn)
Alexander Duyck7d5753f2009-10-27 23:47:16 +00004068{
4069 struct e1000_hw *hw = &adapter->hw;
4070 u32 vmolr;
4071
Alexander Duyckd3836f82016-01-06 23:10:47 -08004072 if (size > MAX_JUMBO_FRAME_SIZE)
4073 size = MAX_JUMBO_FRAME_SIZE;
Alexander Duyck7d5753f2009-10-27 23:47:16 +00004074
4075 vmolr = rd32(E1000_VMOLR(vfn));
4076 vmolr &= ~E1000_VMOLR_RLPML_MASK;
4077 vmolr |= size | E1000_VMOLR_LPE;
4078 wr32(E1000_VMOLR(vfn), vmolr);
4079
4080 return 0;
4081}
4082
Corinna Vinschen030f9f52016-01-28 13:53:23 +01004083static inline void igb_set_vf_vlan_strip(struct igb_adapter *adapter,
4084 int vfn, bool enable)
Alexander Duycke1739522009-02-19 20:39:44 -08004085{
Alexander Duycke1739522009-02-19 20:39:44 -08004086 struct e1000_hw *hw = &adapter->hw;
Corinna Vinschen030f9f52016-01-28 13:53:23 +01004087 u32 val, reg;
Alexander Duycke1739522009-02-19 20:39:44 -08004088
Corinna Vinschen030f9f52016-01-28 13:53:23 +01004089 if (hw->mac.type < e1000_82576)
4090 return;
Alexander Duycke1739522009-02-19 20:39:44 -08004091
Corinna Vinschen030f9f52016-01-28 13:53:23 +01004092 if (hw->mac.type == e1000_i350)
4093 reg = E1000_DVMOLR(vfn);
4094 else
4095 reg = E1000_VMOLR(vfn);
4096
4097 val = rd32(reg);
4098 if (enable)
4099 val |= E1000_VMOLR_STRVLAN;
4100 else
4101 val &= ~(E1000_VMOLR_STRVLAN);
4102 wr32(reg, val);
Alexander Duycke1739522009-02-19 20:39:44 -08004103}
4104
Williams, Mitch A8151d292010-02-10 01:44:24 +00004105static inline void igb_set_vmolr(struct igb_adapter *adapter,
4106 int vfn, bool aupe)
Alexander Duyck7d5753f2009-10-27 23:47:16 +00004107{
4108 struct e1000_hw *hw = &adapter->hw;
4109 u32 vmolr;
4110
Jeff Kirsherb980ac12013-02-23 07:29:56 +00004111 /* This register exists only on 82576 and newer so if we are older then
Alexander Duyck7d5753f2009-10-27 23:47:16 +00004112 * we should exit and do nothing
4113 */
4114 if (hw->mac.type < e1000_82576)
4115 return;
4116
4117 vmolr = rd32(E1000_VMOLR(vfn));
Williams, Mitch A8151d292010-02-10 01:44:24 +00004118 if (aupe)
Jeff Kirsherb980ac12013-02-23 07:29:56 +00004119 vmolr |= E1000_VMOLR_AUPE; /* Accept untagged packets */
Williams, Mitch A8151d292010-02-10 01:44:24 +00004120 else
4121 vmolr &= ~(E1000_VMOLR_AUPE); /* Tagged packets ONLY */
Alexander Duyck7d5753f2009-10-27 23:47:16 +00004122
4123 /* clear all bits that might not be set */
4124 vmolr &= ~(E1000_VMOLR_BAM | E1000_VMOLR_RSSE);
4125
Alexander Duycka99955f2009-11-12 18:37:19 +00004126 if (adapter->rss_queues > 1 && vfn == adapter->vfs_allocated_count)
Alexander Duyck7d5753f2009-10-27 23:47:16 +00004127 vmolr |= E1000_VMOLR_RSSE; /* enable RSS */
Jeff Kirsherb980ac12013-02-23 07:29:56 +00004128 /* for VMDq only allow the VFs and pool 0 to accept broadcast and
Alexander Duyck7d5753f2009-10-27 23:47:16 +00004129 * multicast packets
4130 */
4131 if (vfn <= adapter->vfs_allocated_count)
Jeff Kirsherb980ac12013-02-23 07:29:56 +00004132 vmolr |= E1000_VMOLR_BAM; /* Accept broadcast */
Alexander Duyck7d5753f2009-10-27 23:47:16 +00004133
4134 wr32(E1000_VMOLR(vfn), vmolr);
4135}
4136
Alexander Duycke1739522009-02-19 20:39:44 -08004137/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00004138 * igb_configure_rx_ring - Configure a receive ring after Reset
4139 * @adapter: board private structure
4140 * @ring: receive ring to be configured
Alexander Duyck85b430b2009-10-27 15:50:29 +00004141 *
Jeff Kirsherb980ac12013-02-23 07:29:56 +00004142 * Configure the Rx unit of the MAC after a reset.
Alexander Duyck85b430b2009-10-27 15:50:29 +00004143 **/
Alexander Duyckd7ee5b32009-10-27 15:54:23 +00004144void igb_configure_rx_ring(struct igb_adapter *adapter,
Jeff Kirsherb980ac12013-02-23 07:29:56 +00004145 struct igb_ring *ring)
Alexander Duyck85b430b2009-10-27 15:50:29 +00004146{
4147 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck7ec01162017-02-06 18:25:41 -08004148 union e1000_adv_rx_desc *rx_desc;
Alexander Duyck85b430b2009-10-27 15:50:29 +00004149 u64 rdba = ring->dma;
4150 int reg_idx = ring->reg_idx;
Alexander Duycka74420e2011-08-26 07:43:27 +00004151 u32 srrctl = 0, rxdctl = 0;
Alexander Duyck85b430b2009-10-27 15:50:29 +00004152
4153 /* disable the queue */
Alexander Duycka74420e2011-08-26 07:43:27 +00004154 wr32(E1000_RXDCTL(reg_idx), 0);
Alexander Duyck85b430b2009-10-27 15:50:29 +00004155
4156 /* Set DMA base address registers */
4157 wr32(E1000_RDBAL(reg_idx),
4158 rdba & 0x00000000ffffffffULL);
4159 wr32(E1000_RDBAH(reg_idx), rdba >> 32);
4160 wr32(E1000_RDLEN(reg_idx),
Jeff Kirsherb980ac12013-02-23 07:29:56 +00004161 ring->count * sizeof(union e1000_adv_rx_desc));
Alexander Duyck85b430b2009-10-27 15:50:29 +00004162
4163 /* initialize head and tail */
Cao jin629823b2016-11-08 15:06:20 +08004164 ring->tail = adapter->io_addr + E1000_RDT(reg_idx);
Alexander Duycka74420e2011-08-26 07:43:27 +00004165 wr32(E1000_RDH(reg_idx), 0);
Alexander Duyckfce99e32009-10-27 15:51:27 +00004166 writel(0, ring->tail);
Alexander Duyck85b430b2009-10-27 15:50:29 +00004167
Alexander Duyck952f72a2009-10-27 15:51:07 +00004168 /* set descriptor configuration */
Alexander Duyck44390ca2011-08-26 07:43:38 +00004169 srrctl = IGB_RX_HDR_LEN << E1000_SRRCTL_BSIZEHDRSIZE_SHIFT;
Alexander Duyck8649aae2017-02-06 18:27:03 -08004170 if (ring_uses_large_buffer(ring))
4171 srrctl |= IGB_RXBUFFER_3072 >> E1000_SRRCTL_BSIZEPKT_SHIFT;
4172 else
4173 srrctl |= IGB_RXBUFFER_2048 >> E1000_SRRCTL_BSIZEPKT_SHIFT;
Alexander Duyck1a1c2252012-09-25 00:30:52 +00004174 srrctl |= E1000_SRRCTL_DESCTYPE_ADV_ONEBUF;
Alexander Duyck06218a82011-08-26 07:46:55 +00004175 if (hw->mac.type >= e1000_82580)
Nick Nunley757b77e2010-03-26 11:36:47 +00004176 srrctl |= E1000_SRRCTL_TIMESTAMP;
Nick Nunleye6bdb6f2010-02-17 01:03:38 +00004177 /* Only set Drop Enable if we are supporting multiple queues */
4178 if (adapter->vfs_allocated_count || adapter->num_rx_queues > 1)
4179 srrctl |= E1000_SRRCTL_DROP_EN;
Alexander Duyck952f72a2009-10-27 15:51:07 +00004180
4181 wr32(E1000_SRRCTL(reg_idx), srrctl);
4182
Alexander Duyck7d5753f2009-10-27 23:47:16 +00004183 /* set filtering for VMDQ pools */
Williams, Mitch A8151d292010-02-10 01:44:24 +00004184 igb_set_vmolr(adapter, reg_idx & 0x7, true);
Alexander Duyck7d5753f2009-10-27 23:47:16 +00004185
Alexander Duyck85b430b2009-10-27 15:50:29 +00004186 rxdctl |= IGB_RX_PTHRESH;
4187 rxdctl |= IGB_RX_HTHRESH << 8;
4188 rxdctl |= IGB_RX_WTHRESH << 16;
Alexander Duycka74420e2011-08-26 07:43:27 +00004189
Alexander Duyckd2bead52017-02-06 18:25:50 -08004190 /* initialize rx_buffer_info */
4191 memset(ring->rx_buffer_info, 0,
4192 sizeof(struct igb_rx_buffer) * ring->count);
4193
Alexander Duyck7ec01162017-02-06 18:25:41 -08004194 /* initialize Rx descriptor 0 */
4195 rx_desc = IGB_RX_DESC(ring, 0);
4196 rx_desc->wb.upper.length = 0;
4197
Alexander Duycka74420e2011-08-26 07:43:27 +00004198 /* enable receive descriptor fetching */
4199 rxdctl |= E1000_RXDCTL_QUEUE_ENABLE;
Alexander Duyck85b430b2009-10-27 15:50:29 +00004200 wr32(E1000_RXDCTL(reg_idx), rxdctl);
4201}
4202
Alexander Duyck8649aae2017-02-06 18:27:03 -08004203static void igb_set_rx_buffer_len(struct igb_adapter *adapter,
4204 struct igb_ring *rx_ring)
4205{
4206 /* set build_skb and buffer size flags */
Alexander Duycke3cdf682017-02-06 18:27:14 -08004207 clear_ring_build_skb_enabled(rx_ring);
Alexander Duyck8649aae2017-02-06 18:27:03 -08004208 clear_ring_uses_large_buffer(rx_ring);
4209
4210 if (adapter->flags & IGB_FLAG_RX_LEGACY)
4211 return;
4212
Alexander Duycke3cdf682017-02-06 18:27:14 -08004213 set_ring_build_skb_enabled(rx_ring);
4214
Alexander Duyck8649aae2017-02-06 18:27:03 -08004215#if (PAGE_SIZE < 8192)
4216 if (adapter->max_frame_size <= IGB_MAX_FRAME_BUILD_SKB)
4217 return;
4218
4219 set_ring_uses_large_buffer(rx_ring);
4220#endif
4221}
4222
Alexander Duyck85b430b2009-10-27 15:50:29 +00004223/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00004224 * igb_configure_rx - Configure receive Unit after Reset
4225 * @adapter: board private structure
Auke Kok9d5c8242008-01-24 02:22:38 -08004226 *
Jeff Kirsherb980ac12013-02-23 07:29:56 +00004227 * Configure the Rx unit of the MAC after a reset.
Auke Kok9d5c8242008-01-24 02:22:38 -08004228 **/
4229static void igb_configure_rx(struct igb_adapter *adapter)
4230{
Hannes Eder91075842009-02-18 19:36:04 -08004231 int i;
Auke Kok9d5c8242008-01-24 02:22:38 -08004232
Alexander Duyck26ad9172009-10-05 06:32:49 +00004233 /* set the correct pool for the PF default MAC address in entry 0 */
Yury Kylulin83c21332017-03-07 11:20:25 +03004234 igb_set_default_mac_filter(adapter);
Alexander Duyck26ad9172009-10-05 06:32:49 +00004235
Alexander Duyck06cf2662009-10-27 15:53:25 +00004236 /* Setup the HW Rx Head and Tail Descriptor Pointers and
Jeff Kirsherb980ac12013-02-23 07:29:56 +00004237 * the Base and Length of the Rx Descriptor Ring
4238 */
Alexander Duyck8649aae2017-02-06 18:27:03 -08004239 for (i = 0; i < adapter->num_rx_queues; i++) {
4240 struct igb_ring *rx_ring = adapter->rx_ring[i];
4241
4242 igb_set_rx_buffer_len(adapter, rx_ring);
4243 igb_configure_rx_ring(adapter, rx_ring);
4244 }
Auke Kok9d5c8242008-01-24 02:22:38 -08004245}
4246
4247/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00004248 * igb_free_tx_resources - Free Tx Resources per Queue
4249 * @tx_ring: Tx descriptor ring for a specific queue
Auke Kok9d5c8242008-01-24 02:22:38 -08004250 *
Jeff Kirsherb980ac12013-02-23 07:29:56 +00004251 * Free all transmit software resources
Auke Kok9d5c8242008-01-24 02:22:38 -08004252 **/
Alexander Duyck68fd9912008-11-20 00:48:10 -08004253void igb_free_tx_resources(struct igb_ring *tx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08004254{
Mitch Williams3b644cf2008-06-27 10:59:48 -07004255 igb_clean_tx_ring(tx_ring);
Auke Kok9d5c8242008-01-24 02:22:38 -08004256
Alexander Duyck06034642011-08-26 07:44:22 +00004257 vfree(tx_ring->tx_buffer_info);
4258 tx_ring->tx_buffer_info = NULL;
Auke Kok9d5c8242008-01-24 02:22:38 -08004259
Alexander Duyck439705e2009-10-27 23:49:20 +00004260 /* if not set, then don't free */
4261 if (!tx_ring->desc)
4262 return;
4263
Alexander Duyck59d71982010-04-27 13:09:25 +00004264 dma_free_coherent(tx_ring->dev, tx_ring->size,
4265 tx_ring->desc, tx_ring->dma);
Auke Kok9d5c8242008-01-24 02:22:38 -08004266
4267 tx_ring->desc = NULL;
4268}
4269
4270/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00004271 * igb_free_all_tx_resources - Free Tx Resources for All Queues
4272 * @adapter: board private structure
Auke Kok9d5c8242008-01-24 02:22:38 -08004273 *
Jeff Kirsherb980ac12013-02-23 07:29:56 +00004274 * Free all transmit software resources
Auke Kok9d5c8242008-01-24 02:22:38 -08004275 **/
4276static void igb_free_all_tx_resources(struct igb_adapter *adapter)
4277{
4278 int i;
4279
4280 for (i = 0; i < adapter->num_tx_queues; i++)
Carolyn Wyborny17a402a2014-11-21 23:52:54 -08004281 if (adapter->tx_ring[i])
4282 igb_free_tx_resources(adapter->tx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08004283}
4284
Auke Kok9d5c8242008-01-24 02:22:38 -08004285/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00004286 * igb_clean_tx_ring - Free Tx Buffers
4287 * @tx_ring: ring to be cleaned
Auke Kok9d5c8242008-01-24 02:22:38 -08004288 **/
Mitch Williams3b644cf2008-06-27 10:59:48 -07004289static void igb_clean_tx_ring(struct igb_ring *tx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08004290{
Alexander Duyck7cc6fd42017-02-06 18:26:02 -08004291 u16 i = tx_ring->next_to_clean;
4292 struct igb_tx_buffer *tx_buffer = &tx_ring->tx_buffer_info[i];
Auke Kok9d5c8242008-01-24 02:22:38 -08004293
Alexander Duyck7cc6fd42017-02-06 18:26:02 -08004294 while (i != tx_ring->next_to_use) {
4295 union e1000_adv_tx_desc *eop_desc, *tx_desc;
Auke Kok9d5c8242008-01-24 02:22:38 -08004296
Alexander Duyck7cc6fd42017-02-06 18:26:02 -08004297 /* Free all the Tx ring sk_buffs */
4298 dev_kfree_skb_any(tx_buffer->skb);
4299
4300 /* unmap skb header data */
4301 dma_unmap_single(tx_ring->dev,
4302 dma_unmap_addr(tx_buffer, dma),
4303 dma_unmap_len(tx_buffer, len),
4304 DMA_TO_DEVICE);
4305
4306 /* check for eop_desc to determine the end of the packet */
4307 eop_desc = tx_buffer->next_to_watch;
4308 tx_desc = IGB_TX_DESC(tx_ring, i);
4309
4310 /* unmap remaining buffers */
4311 while (tx_desc != eop_desc) {
4312 tx_buffer++;
4313 tx_desc++;
4314 i++;
4315 if (unlikely(i == tx_ring->count)) {
4316 i = 0;
4317 tx_buffer = tx_ring->tx_buffer_info;
4318 tx_desc = IGB_TX_DESC(tx_ring, 0);
4319 }
4320
4321 /* unmap any remaining paged data */
4322 if (dma_unmap_len(tx_buffer, len))
4323 dma_unmap_page(tx_ring->dev,
4324 dma_unmap_addr(tx_buffer, dma),
4325 dma_unmap_len(tx_buffer, len),
4326 DMA_TO_DEVICE);
4327 }
4328
4329 /* move us one more past the eop_desc for start of next pkt */
4330 tx_buffer++;
4331 i++;
4332 if (unlikely(i == tx_ring->count)) {
4333 i = 0;
4334 tx_buffer = tx_ring->tx_buffer_info;
4335 }
Auke Kok9d5c8242008-01-24 02:22:38 -08004336 }
4337
Alexander Duyck7cc6fd42017-02-06 18:26:02 -08004338 /* reset BQL for queue */
John Fastabenddad8a3b2012-04-23 12:22:39 +00004339 netdev_tx_reset_queue(txring_txq(tx_ring));
4340
Alexander Duyck7cc6fd42017-02-06 18:26:02 -08004341 /* reset next_to_use and next_to_clean */
Auke Kok9d5c8242008-01-24 02:22:38 -08004342 tx_ring->next_to_use = 0;
4343 tx_ring->next_to_clean = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08004344}
4345
4346/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00004347 * igb_clean_all_tx_rings - Free Tx Buffers for all queues
4348 * @adapter: board private structure
Auke Kok9d5c8242008-01-24 02:22:38 -08004349 **/
4350static void igb_clean_all_tx_rings(struct igb_adapter *adapter)
4351{
4352 int i;
4353
4354 for (i = 0; i < adapter->num_tx_queues; i++)
Carolyn Wyborny17a402a2014-11-21 23:52:54 -08004355 if (adapter->tx_ring[i])
4356 igb_clean_tx_ring(adapter->tx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08004357}
4358
4359/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00004360 * igb_free_rx_resources - Free Rx Resources
4361 * @rx_ring: ring to clean the resources from
Auke Kok9d5c8242008-01-24 02:22:38 -08004362 *
Jeff Kirsherb980ac12013-02-23 07:29:56 +00004363 * Free all receive software resources
Auke Kok9d5c8242008-01-24 02:22:38 -08004364 **/
Alexander Duyck68fd9912008-11-20 00:48:10 -08004365void igb_free_rx_resources(struct igb_ring *rx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08004366{
Mitch Williams3b644cf2008-06-27 10:59:48 -07004367 igb_clean_rx_ring(rx_ring);
Auke Kok9d5c8242008-01-24 02:22:38 -08004368
Alexander Duyck06034642011-08-26 07:44:22 +00004369 vfree(rx_ring->rx_buffer_info);
4370 rx_ring->rx_buffer_info = NULL;
Auke Kok9d5c8242008-01-24 02:22:38 -08004371
Alexander Duyck439705e2009-10-27 23:49:20 +00004372 /* if not set, then don't free */
4373 if (!rx_ring->desc)
4374 return;
4375
Alexander Duyck59d71982010-04-27 13:09:25 +00004376 dma_free_coherent(rx_ring->dev, rx_ring->size,
4377 rx_ring->desc, rx_ring->dma);
Auke Kok9d5c8242008-01-24 02:22:38 -08004378
4379 rx_ring->desc = NULL;
4380}
4381
4382/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00004383 * igb_free_all_rx_resources - Free Rx Resources for All Queues
4384 * @adapter: board private structure
Auke Kok9d5c8242008-01-24 02:22:38 -08004385 *
Jeff Kirsherb980ac12013-02-23 07:29:56 +00004386 * Free all receive software resources
Auke Kok9d5c8242008-01-24 02:22:38 -08004387 **/
4388static void igb_free_all_rx_resources(struct igb_adapter *adapter)
4389{
4390 int i;
4391
4392 for (i = 0; i < adapter->num_rx_queues; i++)
Carolyn Wyborny17a402a2014-11-21 23:52:54 -08004393 if (adapter->rx_ring[i])
4394 igb_free_rx_resources(adapter->rx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08004395}
4396
4397/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00004398 * igb_clean_rx_ring - Free Rx Buffers per Queue
4399 * @rx_ring: ring to free buffers from
Auke Kok9d5c8242008-01-24 02:22:38 -08004400 **/
Mitch Williams3b644cf2008-06-27 10:59:48 -07004401static void igb_clean_rx_ring(struct igb_ring *rx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08004402{
Alexander Duyckd2bead52017-02-06 18:25:50 -08004403 u16 i = rx_ring->next_to_clean;
Auke Kok9d5c8242008-01-24 02:22:38 -08004404
Alexander Duyck1a1c2252012-09-25 00:30:52 +00004405 if (rx_ring->skb)
4406 dev_kfree_skb(rx_ring->skb);
4407 rx_ring->skb = NULL;
4408
Auke Kok9d5c8242008-01-24 02:22:38 -08004409 /* Free all the Rx ring sk_buffs */
Alexander Duyckd2bead52017-02-06 18:25:50 -08004410 while (i != rx_ring->next_to_alloc) {
Alexander Duyck06034642011-08-26 07:44:22 +00004411 struct igb_rx_buffer *buffer_info = &rx_ring->rx_buffer_info[i];
Auke Kok9d5c8242008-01-24 02:22:38 -08004412
Alexander Duyck5be59552016-12-14 15:05:30 -08004413 /* Invalidate cache lines that may have been written to by
4414 * device so that we avoid corrupting memory.
4415 */
4416 dma_sync_single_range_for_cpu(rx_ring->dev,
4417 buffer_info->dma,
4418 buffer_info->page_offset,
Alexander Duyck8649aae2017-02-06 18:27:03 -08004419 igb_rx_bufsz(rx_ring),
Alexander Duyck5be59552016-12-14 15:05:30 -08004420 DMA_FROM_DEVICE);
4421
4422 /* free resources associated with mapping */
4423 dma_unmap_page_attrs(rx_ring->dev,
4424 buffer_info->dma,
Alexander Duyck8649aae2017-02-06 18:27:03 -08004425 igb_rx_pg_size(rx_ring),
Alexander Duyck5be59552016-12-14 15:05:30 -08004426 DMA_FROM_DEVICE,
Alexander Duyck7bd17592017-02-06 18:25:26 -08004427 IGB_RX_DMA_ATTR);
Alexander Duyck2976db82017-01-10 16:58:09 -08004428 __page_frag_cache_drain(buffer_info->page,
4429 buffer_info->pagecnt_bias);
Alexander Duyckcbc8e552012-09-25 00:31:02 +00004430
Alexander Duyckd2bead52017-02-06 18:25:50 -08004431 i++;
4432 if (i == rx_ring->count)
4433 i = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08004434 }
4435
Alexander Duyckcbc8e552012-09-25 00:31:02 +00004436 rx_ring->next_to_alloc = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08004437 rx_ring->next_to_clean = 0;
4438 rx_ring->next_to_use = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08004439}
4440
4441/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00004442 * igb_clean_all_rx_rings - Free Rx Buffers for all queues
4443 * @adapter: board private structure
Auke Kok9d5c8242008-01-24 02:22:38 -08004444 **/
4445static void igb_clean_all_rx_rings(struct igb_adapter *adapter)
4446{
4447 int i;
4448
4449 for (i = 0; i < adapter->num_rx_queues; i++)
Carolyn Wyborny17a402a2014-11-21 23:52:54 -08004450 if (adapter->rx_ring[i])
4451 igb_clean_rx_ring(adapter->rx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08004452}
4453
4454/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00004455 * igb_set_mac - Change the Ethernet Address of the NIC
4456 * @netdev: network interface device structure
4457 * @p: pointer to an address structure
Auke Kok9d5c8242008-01-24 02:22:38 -08004458 *
Jeff Kirsherb980ac12013-02-23 07:29:56 +00004459 * Returns 0 on success, negative on failure
Auke Kok9d5c8242008-01-24 02:22:38 -08004460 **/
4461static int igb_set_mac(struct net_device *netdev, void *p)
4462{
4463 struct igb_adapter *adapter = netdev_priv(netdev);
Alexander Duyck28b07592009-02-06 23:20:31 +00004464 struct e1000_hw *hw = &adapter->hw;
Auke Kok9d5c8242008-01-24 02:22:38 -08004465 struct sockaddr *addr = p;
4466
4467 if (!is_valid_ether_addr(addr->sa_data))
4468 return -EADDRNOTAVAIL;
4469
4470 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
Alexander Duyck28b07592009-02-06 23:20:31 +00004471 memcpy(hw->mac.addr, addr->sa_data, netdev->addr_len);
Auke Kok9d5c8242008-01-24 02:22:38 -08004472
Alexander Duyck26ad9172009-10-05 06:32:49 +00004473 /* set the correct pool for the new PF MAC address in entry 0 */
Yury Kylulin83c21332017-03-07 11:20:25 +03004474 igb_set_default_mac_filter(adapter);
Alexander Duycke1739522009-02-19 20:39:44 -08004475
Auke Kok9d5c8242008-01-24 02:22:38 -08004476 return 0;
4477}
4478
4479/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00004480 * igb_write_mc_addr_list - write multicast addresses to MTA
4481 * @netdev: network interface device structure
Alexander Duyck68d480c2009-10-05 06:33:08 +00004482 *
Jeff Kirsherb980ac12013-02-23 07:29:56 +00004483 * Writes multicast address list to the MTA hash table.
4484 * Returns: -ENOMEM on failure
4485 * 0 on no addresses written
4486 * X on writing X addresses to MTA
Alexander Duyck68d480c2009-10-05 06:33:08 +00004487 **/
4488static int igb_write_mc_addr_list(struct net_device *netdev)
4489{
4490 struct igb_adapter *adapter = netdev_priv(netdev);
4491 struct e1000_hw *hw = &adapter->hw;
Jiri Pirko22bedad32010-04-01 21:22:57 +00004492 struct netdev_hw_addr *ha;
Alexander Duyck68d480c2009-10-05 06:33:08 +00004493 u8 *mta_list;
Alexander Duyck68d480c2009-10-05 06:33:08 +00004494 int i;
4495
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00004496 if (netdev_mc_empty(netdev)) {
Alexander Duyck68d480c2009-10-05 06:33:08 +00004497 /* nothing to program, so clear mc list */
4498 igb_update_mc_addr_list(hw, NULL, 0);
4499 igb_restore_vf_multicasts(adapter);
4500 return 0;
4501 }
4502
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00004503 mta_list = kzalloc(netdev_mc_count(netdev) * 6, GFP_ATOMIC);
Alexander Duyck68d480c2009-10-05 06:33:08 +00004504 if (!mta_list)
4505 return -ENOMEM;
4506
Alexander Duyck68d480c2009-10-05 06:33:08 +00004507 /* The shared function expects a packed array of only addresses. */
Jiri Pirko48e2f182010-02-22 09:22:26 +00004508 i = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +00004509 netdev_for_each_mc_addr(ha, netdev)
4510 memcpy(mta_list + (i++ * ETH_ALEN), ha->addr, ETH_ALEN);
Alexander Duyck68d480c2009-10-05 06:33:08 +00004511
Alexander Duyck68d480c2009-10-05 06:33:08 +00004512 igb_update_mc_addr_list(hw, mta_list, i);
4513 kfree(mta_list);
4514
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00004515 return netdev_mc_count(netdev);
Alexander Duyck68d480c2009-10-05 06:33:08 +00004516}
4517
Alexander Duyck16903ca2016-01-06 23:11:18 -08004518static int igb_vlan_promisc_enable(struct igb_adapter *adapter)
4519{
4520 struct e1000_hw *hw = &adapter->hw;
4521 u32 i, pf_id;
4522
4523 switch (hw->mac.type) {
4524 case e1000_i210:
4525 case e1000_i211:
4526 case e1000_i350:
4527 /* VLAN filtering needed for VLAN prio filter */
4528 if (adapter->netdev->features & NETIF_F_NTUPLE)
4529 break;
4530 /* fall through */
4531 case e1000_82576:
4532 case e1000_82580:
4533 case e1000_i354:
4534 /* VLAN filtering needed for pool filtering */
4535 if (adapter->vfs_allocated_count)
4536 break;
4537 /* fall through */
4538 default:
4539 return 1;
4540 }
4541
4542 /* We are already in VLAN promisc, nothing to do */
4543 if (adapter->flags & IGB_FLAG_VLAN_PROMISC)
4544 return 0;
4545
4546 if (!adapter->vfs_allocated_count)
4547 goto set_vfta;
4548
4549 /* Add PF to all active pools */
4550 pf_id = adapter->vfs_allocated_count + E1000_VLVF_POOLSEL_SHIFT;
4551
4552 for (i = E1000_VLVF_ARRAY_SIZE; --i;) {
4553 u32 vlvf = rd32(E1000_VLVF(i));
4554
Jacob Kellera51d8c22016-04-13 16:08:28 -07004555 vlvf |= BIT(pf_id);
Alexander Duyck16903ca2016-01-06 23:11:18 -08004556 wr32(E1000_VLVF(i), vlvf);
4557 }
4558
4559set_vfta:
4560 /* Set all bits in the VLAN filter table array */
4561 for (i = E1000_VLAN_FILTER_TBL_SIZE; i--;)
4562 hw->mac.ops.write_vfta(hw, i, ~0U);
4563
4564 /* Set flag so we don't redo unnecessary work */
4565 adapter->flags |= IGB_FLAG_VLAN_PROMISC;
4566
4567 return 0;
4568}
4569
4570#define VFTA_BLOCK_SIZE 8
4571static void igb_scrub_vfta(struct igb_adapter *adapter, u32 vfta_offset)
4572{
4573 struct e1000_hw *hw = &adapter->hw;
4574 u32 vfta[VFTA_BLOCK_SIZE] = { 0 };
4575 u32 vid_start = vfta_offset * 32;
4576 u32 vid_end = vid_start + (VFTA_BLOCK_SIZE * 32);
4577 u32 i, vid, word, bits, pf_id;
4578
4579 /* guarantee that we don't scrub out management VLAN */
4580 vid = adapter->mng_vlan_id;
4581 if (vid >= vid_start && vid < vid_end)
Jacob Kellera51d8c22016-04-13 16:08:28 -07004582 vfta[(vid - vid_start) / 32] |= BIT(vid % 32);
Alexander Duyck16903ca2016-01-06 23:11:18 -08004583
4584 if (!adapter->vfs_allocated_count)
4585 goto set_vfta;
4586
4587 pf_id = adapter->vfs_allocated_count + E1000_VLVF_POOLSEL_SHIFT;
4588
4589 for (i = E1000_VLVF_ARRAY_SIZE; --i;) {
4590 u32 vlvf = rd32(E1000_VLVF(i));
4591
4592 /* pull VLAN ID from VLVF */
4593 vid = vlvf & VLAN_VID_MASK;
4594
4595 /* only concern ourselves with a certain range */
4596 if (vid < vid_start || vid >= vid_end)
4597 continue;
4598
4599 if (vlvf & E1000_VLVF_VLANID_ENABLE) {
4600 /* record VLAN ID in VFTA */
Jacob Kellera51d8c22016-04-13 16:08:28 -07004601 vfta[(vid - vid_start) / 32] |= BIT(vid % 32);
Alexander Duyck16903ca2016-01-06 23:11:18 -08004602
4603 /* if PF is part of this then continue */
4604 if (test_bit(vid, adapter->active_vlans))
4605 continue;
4606 }
4607
4608 /* remove PF from the pool */
Jacob Kellera51d8c22016-04-13 16:08:28 -07004609 bits = ~BIT(pf_id);
Alexander Duyck16903ca2016-01-06 23:11:18 -08004610 bits &= rd32(E1000_VLVF(i));
4611 wr32(E1000_VLVF(i), bits);
4612 }
4613
4614set_vfta:
4615 /* extract values from active_vlans and write back to VFTA */
4616 for (i = VFTA_BLOCK_SIZE; i--;) {
4617 vid = (vfta_offset + i) * 32;
4618 word = vid / BITS_PER_LONG;
4619 bits = vid % BITS_PER_LONG;
4620
4621 vfta[i] |= adapter->active_vlans[word] >> bits;
4622
4623 hw->mac.ops.write_vfta(hw, vfta_offset + i, vfta[i]);
4624 }
4625}
4626
4627static void igb_vlan_promisc_disable(struct igb_adapter *adapter)
4628{
4629 u32 i;
4630
4631 /* We are not in VLAN promisc, nothing to do */
4632 if (!(adapter->flags & IGB_FLAG_VLAN_PROMISC))
4633 return;
4634
4635 /* Set flag so we don't redo unnecessary work */
4636 adapter->flags &= ~IGB_FLAG_VLAN_PROMISC;
4637
4638 for (i = 0; i < E1000_VLAN_FILTER_TBL_SIZE; i += VFTA_BLOCK_SIZE)
4639 igb_scrub_vfta(adapter, i);
4640}
4641
Alexander Duyck68d480c2009-10-05 06:33:08 +00004642/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00004643 * igb_set_rx_mode - Secondary Unicast, Multicast and Promiscuous mode set
4644 * @netdev: network interface device structure
Auke Kok9d5c8242008-01-24 02:22:38 -08004645 *
Jeff Kirsherb980ac12013-02-23 07:29:56 +00004646 * The set_rx_mode entry point is called whenever the unicast or multicast
4647 * address lists or the network interface flags are updated. This routine is
4648 * responsible for configuring the hardware for proper unicast, multicast,
4649 * promiscuous mode, and all-multi behavior.
Auke Kok9d5c8242008-01-24 02:22:38 -08004650 **/
Alexander Duyckff41f8d2009-09-03 14:48:56 +00004651static void igb_set_rx_mode(struct net_device *netdev)
Auke Kok9d5c8242008-01-24 02:22:38 -08004652{
4653 struct igb_adapter *adapter = netdev_priv(netdev);
4654 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck68d480c2009-10-05 06:33:08 +00004655 unsigned int vfn = adapter->vfs_allocated_count;
Alexander Duyckcfbc8712017-02-06 18:26:15 -08004656 u32 rctl = 0, vmolr = 0, rlpml = MAX_JUMBO_FRAME_SIZE;
Alexander Duyck68d480c2009-10-05 06:33:08 +00004657 int count;
Auke Kok9d5c8242008-01-24 02:22:38 -08004658
4659 /* Check for Promiscuous and All Multicast modes */
Patrick McHardy746b9f02008-07-16 20:15:45 -07004660 if (netdev->flags & IFF_PROMISC) {
Alexander Duyck16903ca2016-01-06 23:11:18 -08004661 rctl |= E1000_RCTL_UPE | E1000_RCTL_MPE;
Alexander Duyckbf456ab2016-01-06 23:11:43 -08004662 vmolr |= E1000_VMOLR_MPME;
4663
4664 /* enable use of UTA filter to force packets to default pool */
4665 if (hw->mac.type == e1000_82576)
4666 vmolr |= E1000_VMOLR_ROPE;
Patrick McHardy746b9f02008-07-16 20:15:45 -07004667 } else {
Alexander Duyck68d480c2009-10-05 06:33:08 +00004668 if (netdev->flags & IFF_ALLMULTI) {
Patrick McHardy746b9f02008-07-16 20:15:45 -07004669 rctl |= E1000_RCTL_MPE;
Alexander Duyck68d480c2009-10-05 06:33:08 +00004670 vmolr |= E1000_VMOLR_MPME;
4671 } else {
Jeff Kirsherb980ac12013-02-23 07:29:56 +00004672 /* Write addresses to the MTA, if the attempt fails
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004673 * then we should just turn on promiscuous mode so
Alexander Duyck68d480c2009-10-05 06:33:08 +00004674 * that we can at least receive multicast traffic
4675 */
4676 count = igb_write_mc_addr_list(netdev);
4677 if (count < 0) {
4678 rctl |= E1000_RCTL_MPE;
4679 vmolr |= E1000_VMOLR_MPME;
4680 } else if (count) {
4681 vmolr |= E1000_VMOLR_ROMPE;
4682 }
4683 }
Patrick McHardy746b9f02008-07-16 20:15:45 -07004684 }
Alexander Duyck268f9d32016-01-06 23:11:34 -08004685
4686 /* Write addresses to available RAR registers, if there is not
4687 * sufficient space to store all the addresses then enable
4688 * unicast promiscuous mode
4689 */
Yury Kylulin83c21332017-03-07 11:20:25 +03004690 if (__dev_uc_sync(netdev, igb_uc_sync, igb_uc_unsync)) {
Alexander Duyck268f9d32016-01-06 23:11:34 -08004691 rctl |= E1000_RCTL_UPE;
4692 vmolr |= E1000_VMOLR_ROPE;
Auke Kok9d5c8242008-01-24 02:22:38 -08004693 }
Alexander Duyck16903ca2016-01-06 23:11:18 -08004694
4695 /* enable VLAN filtering by default */
4696 rctl |= E1000_RCTL_VFE;
4697
4698 /* disable VLAN filtering for modes that require it */
4699 if ((netdev->flags & IFF_PROMISC) ||
4700 (netdev->features & NETIF_F_RXALL)) {
4701 /* if we fail to set all rules then just clear VFE */
4702 if (igb_vlan_promisc_enable(adapter))
4703 rctl &= ~E1000_RCTL_VFE;
4704 } else {
4705 igb_vlan_promisc_disable(adapter);
4706 }
4707
4708 /* update state of unicast, multicast, and VLAN filtering modes */
4709 rctl |= rd32(E1000_RCTL) & ~(E1000_RCTL_UPE | E1000_RCTL_MPE |
4710 E1000_RCTL_VFE);
Auke Kok9d5c8242008-01-24 02:22:38 -08004711 wr32(E1000_RCTL, rctl);
4712
Alexander Duyckcfbc8712017-02-06 18:26:15 -08004713#if (PAGE_SIZE < 8192)
4714 if (!adapter->vfs_allocated_count) {
4715 if (adapter->max_frame_size <= IGB_MAX_FRAME_BUILD_SKB)
4716 rlpml = IGB_MAX_FRAME_BUILD_SKB;
4717 }
4718#endif
4719 wr32(E1000_RLPML, rlpml);
4720
Jeff Kirsherb980ac12013-02-23 07:29:56 +00004721 /* In order to support SR-IOV and eventually VMDq it is necessary to set
Alexander Duyck68d480c2009-10-05 06:33:08 +00004722 * the VMOLR to enable the appropriate modes. Without this workaround
4723 * we will have issues with VLAN tag stripping not being done for frames
4724 * that are only arriving because we are the default pool
4725 */
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00004726 if ((hw->mac.type < e1000_82576) || (hw->mac.type > e1000_i350))
Alexander Duyck28fc06f2009-07-23 18:08:54 +00004727 return;
Alexander Duyck28fc06f2009-07-23 18:08:54 +00004728
Alexander Duyckbf456ab2016-01-06 23:11:43 -08004729 /* set UTA to appropriate mode */
4730 igb_set_uta(adapter, !!(vmolr & E1000_VMOLR_ROPE));
4731
Alexander Duyck68d480c2009-10-05 06:33:08 +00004732 vmolr |= rd32(E1000_VMOLR(vfn)) &
Jeff Kirsherb980ac12013-02-23 07:29:56 +00004733 ~(E1000_VMOLR_ROPE | E1000_VMOLR_MPME | E1000_VMOLR_ROMPE);
Alexander Duyck45693bc2016-01-06 23:10:39 -08004734
Alexander Duyckcfbc8712017-02-06 18:26:15 -08004735 /* enable Rx jumbo frames, restrict as needed to support build_skb */
Alexander Duyck45693bc2016-01-06 23:10:39 -08004736 vmolr &= ~E1000_VMOLR_RLPML_MASK;
Alexander Duyckcfbc8712017-02-06 18:26:15 -08004737#if (PAGE_SIZE < 8192)
4738 if (adapter->max_frame_size <= IGB_MAX_FRAME_BUILD_SKB)
4739 vmolr |= IGB_MAX_FRAME_BUILD_SKB;
4740 else
4741#endif
4742 vmolr |= MAX_JUMBO_FRAME_SIZE;
4743 vmolr |= E1000_VMOLR_LPE;
Alexander Duyck45693bc2016-01-06 23:10:39 -08004744
Alexander Duyck68d480c2009-10-05 06:33:08 +00004745 wr32(E1000_VMOLR(vfn), vmolr);
Alexander Duyck45693bc2016-01-06 23:10:39 -08004746
Alexander Duyck28fc06f2009-07-23 18:08:54 +00004747 igb_restore_vf_multicasts(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08004748}
4749
Greg Rose13800462010-11-06 02:08:26 +00004750static void igb_check_wvbr(struct igb_adapter *adapter)
4751{
4752 struct e1000_hw *hw = &adapter->hw;
4753 u32 wvbr = 0;
4754
4755 switch (hw->mac.type) {
4756 case e1000_82576:
4757 case e1000_i350:
Carolyn Wyborny81ad8072014-04-11 01:46:13 +00004758 wvbr = rd32(E1000_WVBR);
4759 if (!wvbr)
Greg Rose13800462010-11-06 02:08:26 +00004760 return;
4761 break;
4762 default:
4763 break;
4764 }
4765
4766 adapter->wvbr |= wvbr;
4767}
4768
4769#define IGB_STAGGERED_QUEUE_OFFSET 8
4770
4771static void igb_spoof_check(struct igb_adapter *adapter)
4772{
4773 int j;
4774
4775 if (!adapter->wvbr)
4776 return;
4777
Carolyn Wyborny9005df32014-04-11 01:45:34 +00004778 for (j = 0; j < adapter->vfs_allocated_count; j++) {
Jacob Kellera51d8c22016-04-13 16:08:28 -07004779 if (adapter->wvbr & BIT(j) ||
4780 adapter->wvbr & BIT(j + IGB_STAGGERED_QUEUE_OFFSET)) {
Greg Rose13800462010-11-06 02:08:26 +00004781 dev_warn(&adapter->pdev->dev,
4782 "Spoof event(s) detected on VF %d\n", j);
4783 adapter->wvbr &=
Jacob Kellera51d8c22016-04-13 16:08:28 -07004784 ~(BIT(j) |
4785 BIT(j + IGB_STAGGERED_QUEUE_OFFSET));
Greg Rose13800462010-11-06 02:08:26 +00004786 }
4787 }
4788}
4789
Auke Kok9d5c8242008-01-24 02:22:38 -08004790/* Need to wait a few seconds after link up to get diagnostic information from
Jeff Kirsherb980ac12013-02-23 07:29:56 +00004791 * the phy
4792 */
Kees Cook26566ea2017-10-16 17:29:35 -07004793static void igb_update_phy_info(struct timer_list *t)
Auke Kok9d5c8242008-01-24 02:22:38 -08004794{
Kees Cook26566ea2017-10-16 17:29:35 -07004795 struct igb_adapter *adapter = from_timer(adapter, t, phy_info_timer);
Alexander Duyckf5f4cf02008-11-21 21:30:24 -08004796 igb_get_phy_info(&adapter->hw);
Auke Kok9d5c8242008-01-24 02:22:38 -08004797}
4798
4799/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00004800 * igb_has_link - check shared code for link and determine up/down
4801 * @adapter: pointer to driver private info
Alexander Duyck4d6b7252009-02-06 23:16:24 +00004802 **/
Nick Nunley31455352010-02-17 01:01:21 +00004803bool igb_has_link(struct igb_adapter *adapter)
Alexander Duyck4d6b7252009-02-06 23:16:24 +00004804{
4805 struct e1000_hw *hw = &adapter->hw;
4806 bool link_active = false;
Alexander Duyck4d6b7252009-02-06 23:16:24 +00004807
4808 /* get_link_status is set on LSC (link status) interrupt or
4809 * rx sequence error interrupt. get_link_status will stay
4810 * false until the e1000_check_for_link establishes link
4811 * for copper adapters ONLY
4812 */
4813 switch (hw->phy.media_type) {
4814 case e1000_media_type_copper:
Akeem G Abodunrine5c33702013-06-06 01:31:09 +00004815 if (!hw->mac.get_link_status)
4816 return true;
Alexander Duyck4d6b7252009-02-06 23:16:24 +00004817 case e1000_media_type_internal_serdes:
Akeem G Abodunrine5c33702013-06-06 01:31:09 +00004818 hw->mac.ops.check_for_link(hw);
4819 link_active = !hw->mac.get_link_status;
Alexander Duyck4d6b7252009-02-06 23:16:24 +00004820 break;
4821 default:
4822 case e1000_media_type_unknown:
4823 break;
4824 }
4825
Akeem G Abodunrinaa9b8cc2013-08-28 02:22:43 +00004826 if (((hw->mac.type == e1000_i210) ||
4827 (hw->mac.type == e1000_i211)) &&
4828 (hw->phy.id == I210_I_PHY_ID)) {
4829 if (!netif_carrier_ok(adapter->netdev)) {
4830 adapter->flags &= ~IGB_FLAG_NEED_LINK_UPDATE;
4831 } else if (!(adapter->flags & IGB_FLAG_NEED_LINK_UPDATE)) {
4832 adapter->flags |= IGB_FLAG_NEED_LINK_UPDATE;
4833 adapter->link_check_timeout = jiffies;
4834 }
4835 }
4836
Alexander Duyck4d6b7252009-02-06 23:16:24 +00004837 return link_active;
4838}
4839
Stefan Assmann563988d2011-04-05 04:27:15 +00004840static bool igb_thermal_sensor_event(struct e1000_hw *hw, u32 event)
4841{
4842 bool ret = false;
4843 u32 ctrl_ext, thstat;
4844
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00004845 /* check for thermal sensor event on i350 copper only */
Stefan Assmann563988d2011-04-05 04:27:15 +00004846 if (hw->mac.type == e1000_i350) {
4847 thstat = rd32(E1000_THSTAT);
4848 ctrl_ext = rd32(E1000_CTRL_EXT);
4849
4850 if ((hw->phy.media_type == e1000_media_type_copper) &&
Akeem G. Abodunrin5c17a202013-01-29 10:15:31 +00004851 !(ctrl_ext & E1000_CTRL_EXT_LINK_MODE_SGMII))
Stefan Assmann563988d2011-04-05 04:27:15 +00004852 ret = !!(thstat & event);
Stefan Assmann563988d2011-04-05 04:27:15 +00004853 }
4854
4855 return ret;
4856}
4857
Alexander Duyck4d6b7252009-02-06 23:16:24 +00004858/**
Carolyn Wyborny1516f0a2014-07-09 04:55:45 +00004859 * igb_check_lvmmc - check for malformed packets received
4860 * and indicated in LVMMC register
4861 * @adapter: pointer to adapter
4862 **/
4863static void igb_check_lvmmc(struct igb_adapter *adapter)
4864{
4865 struct e1000_hw *hw = &adapter->hw;
4866 u32 lvmmc;
4867
4868 lvmmc = rd32(E1000_LVMMC);
4869 if (lvmmc) {
4870 if (unlikely(net_ratelimit())) {
4871 netdev_warn(adapter->netdev,
4872 "malformed Tx packet detected and dropped, LVMMC:0x%08x\n",
4873 lvmmc);
4874 }
4875 }
4876}
4877
4878/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00004879 * igb_watchdog - Timer Call-back
4880 * @data: pointer to adapter cast into an unsigned long
Auke Kok9d5c8242008-01-24 02:22:38 -08004881 **/
Kees Cook26566ea2017-10-16 17:29:35 -07004882static void igb_watchdog(struct timer_list *t)
Auke Kok9d5c8242008-01-24 02:22:38 -08004883{
Kees Cook26566ea2017-10-16 17:29:35 -07004884 struct igb_adapter *adapter = from_timer(adapter, t, watchdog_timer);
Auke Kok9d5c8242008-01-24 02:22:38 -08004885 /* Do the rest outside of interrupt context */
4886 schedule_work(&adapter->watchdog_task);
4887}
4888
4889static void igb_watchdog_task(struct work_struct *work)
4890{
4891 struct igb_adapter *adapter = container_of(work,
Jeff Kirsherb980ac12013-02-23 07:29:56 +00004892 struct igb_adapter,
4893 watchdog_task);
Auke Kok9d5c8242008-01-24 02:22:38 -08004894 struct e1000_hw *hw = &adapter->hw;
Koki Sanagic0ba4772013-01-16 11:05:53 +00004895 struct e1000_phy_info *phy = &hw->phy;
Auke Kok9d5c8242008-01-24 02:22:38 -08004896 struct net_device *netdev = adapter->netdev;
Stefan Assmann563988d2011-04-05 04:27:15 +00004897 u32 link;
Alexander Duyck7a6ea552008-08-26 04:25:03 -07004898 int i;
Carolyn Wyborny56cec242013-10-17 05:36:26 +00004899 u32 connsw;
Takuma Uebab72f3f72015-12-31 14:58:14 +09004900 u16 phy_data, retry_count = 20;
Auke Kok9d5c8242008-01-24 02:22:38 -08004901
Alexander Duyck4d6b7252009-02-06 23:16:24 +00004902 link = igb_has_link(adapter);
Akeem G Abodunrinaa9b8cc2013-08-28 02:22:43 +00004903
4904 if (adapter->flags & IGB_FLAG_NEED_LINK_UPDATE) {
4905 if (time_after(jiffies, (adapter->link_check_timeout + HZ)))
4906 adapter->flags &= ~IGB_FLAG_NEED_LINK_UPDATE;
4907 else
4908 link = false;
4909 }
4910
Carolyn Wyborny56cec242013-10-17 05:36:26 +00004911 /* Force link down if we have fiber to swap to */
4912 if (adapter->flags & IGB_FLAG_MAS_ENABLE) {
4913 if (hw->phy.media_type == e1000_media_type_copper) {
4914 connsw = rd32(E1000_CONNSW);
4915 if (!(connsw & E1000_CONNSW_AUTOSENSE_EN))
4916 link = 0;
4917 }
4918 }
Auke Kok9d5c8242008-01-24 02:22:38 -08004919 if (link) {
Carolyn Wyborny2bdfc4e2013-10-17 05:23:01 +00004920 /* Perform a reset if the media type changed. */
4921 if (hw->dev_spec._82575.media_changed) {
4922 hw->dev_spec._82575.media_changed = false;
4923 adapter->flags |= IGB_FLAG_MEDIA_RESET;
4924 igb_reset(adapter);
4925 }
Yan, Zheng749ab2c2012-01-04 20:23:37 +00004926 /* Cancel scheduled suspend requests. */
4927 pm_runtime_resume(netdev->dev.parent);
4928
Auke Kok9d5c8242008-01-24 02:22:38 -08004929 if (!netif_carrier_ok(netdev)) {
4930 u32 ctrl;
Carolyn Wyborny9005df32014-04-11 01:45:34 +00004931
Alexander Duyck330a6d62009-10-27 23:51:35 +00004932 hw->mac.ops.get_speed_and_duplex(hw,
Jeff Kirsherb980ac12013-02-23 07:29:56 +00004933 &adapter->link_speed,
4934 &adapter->link_duplex);
Auke Kok9d5c8242008-01-24 02:22:38 -08004935
4936 ctrl = rd32(E1000_CTRL);
Alexander Duyck527d47c2008-11-27 00:21:39 -08004937 /* Links status message must follow this format */
Carolyn Wybornyc75c4ed2014-04-11 01:45:17 +00004938 netdev_info(netdev,
4939 "igb: %s NIC Link is Up %d Mbps %s Duplex, Flow Control: %s\n",
Alexander Duyck559e9c42009-10-27 23:52:50 +00004940 netdev->name,
4941 adapter->link_speed,
4942 adapter->link_duplex == FULL_DUPLEX ?
Jeff Kirsher876d2d62011-10-21 20:01:34 +00004943 "Full" : "Half",
4944 (ctrl & E1000_CTRL_TFCE) &&
4945 (ctrl & E1000_CTRL_RFCE) ? "RX/TX" :
4946 (ctrl & E1000_CTRL_RFCE) ? "RX" :
4947 (ctrl & E1000_CTRL_TFCE) ? "TX" : "None");
Auke Kok9d5c8242008-01-24 02:22:38 -08004948
Carolyn Wybornyf4c01e92014-03-12 03:58:22 +00004949 /* disable EEE if enabled */
4950 if ((adapter->flags & IGB_FLAG_EEE) &&
4951 (adapter->link_duplex == HALF_DUPLEX)) {
4952 dev_info(&adapter->pdev->dev,
4953 "EEE Disabled: unsupported at half duplex. Re-enable using ethtool when at full duplex.\n");
4954 adapter->hw.dev_spec._82575.eee_disable = true;
4955 adapter->flags &= ~IGB_FLAG_EEE;
4956 }
4957
Koki Sanagic0ba4772013-01-16 11:05:53 +00004958 /* check if SmartSpeed worked */
4959 igb_check_downshift(hw);
4960 if (phy->speed_downgraded)
4961 netdev_warn(netdev, "Link Speed was downgraded by SmartSpeed\n");
4962
Stefan Assmann563988d2011-04-05 04:27:15 +00004963 /* check for thermal sensor event */
Jeff Kirsher876d2d62011-10-21 20:01:34 +00004964 if (igb_thermal_sensor_event(hw,
Carolyn Wybornyd34a15a2014-04-11 01:45:23 +00004965 E1000_THSTAT_LINK_THROTTLE))
Carolyn Wybornyc75c4ed2014-04-11 01:45:17 +00004966 netdev_info(netdev, "The network adapter link speed was downshifted because it overheated\n");
Stefan Assmann563988d2011-04-05 04:27:15 +00004967
Emil Tantilovd07f3e32010-03-23 18:34:57 +00004968 /* adjust timeout factor according to speed/duplex */
Auke Kok9d5c8242008-01-24 02:22:38 -08004969 adapter->tx_timeout_factor = 1;
4970 switch (adapter->link_speed) {
4971 case SPEED_10:
Auke Kok9d5c8242008-01-24 02:22:38 -08004972 adapter->tx_timeout_factor = 14;
4973 break;
4974 case SPEED_100:
Auke Kok9d5c8242008-01-24 02:22:38 -08004975 /* maybe add some timeout factor ? */
4976 break;
4977 }
4978
Takuma Uebab72f3f72015-12-31 14:58:14 +09004979 if (adapter->link_speed != SPEED_1000)
4980 goto no_wait;
4981
4982 /* wait for Remote receiver status OK */
4983retry_read_status:
4984 if (!igb_read_phy_reg(hw, PHY_1000T_STATUS,
4985 &phy_data)) {
4986 if (!(phy_data & SR_1000T_REMOTE_RX_STATUS) &&
4987 retry_count) {
4988 msleep(100);
4989 retry_count--;
4990 goto retry_read_status;
4991 } else if (!retry_count) {
4992 dev_err(&adapter->pdev->dev, "exceed max 2 second\n");
4993 }
4994 } else {
4995 dev_err(&adapter->pdev->dev, "read 1000Base-T Status Reg\n");
4996 }
4997no_wait:
Auke Kok9d5c8242008-01-24 02:22:38 -08004998 netif_carrier_on(netdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08004999
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005000 igb_ping_all_vfs(adapter);
Lior Levy17dc5662011-02-08 02:28:46 +00005001 igb_check_vf_rate_limit(adapter);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005002
Alexander Duyck4b1a9872009-02-06 23:19:50 +00005003 /* link state has changed, schedule phy info update */
Auke Kok9d5c8242008-01-24 02:22:38 -08005004 if (!test_bit(__IGB_DOWN, &adapter->state))
5005 mod_timer(&adapter->phy_info_timer,
5006 round_jiffies(jiffies + 2 * HZ));
5007 }
5008 } else {
5009 if (netif_carrier_ok(netdev)) {
5010 adapter->link_speed = 0;
5011 adapter->link_duplex = 0;
Stefan Assmann563988d2011-04-05 04:27:15 +00005012
5013 /* check for thermal sensor event */
Jeff Kirsher876d2d62011-10-21 20:01:34 +00005014 if (igb_thermal_sensor_event(hw,
5015 E1000_THSTAT_PWR_DOWN)) {
Carolyn Wybornyc75c4ed2014-04-11 01:45:17 +00005016 netdev_err(netdev, "The network adapter was stopped because it overheated\n");
Carolyn Wyborny7ef5ed12011-03-12 08:59:47 +00005017 }
Stefan Assmann563988d2011-04-05 04:27:15 +00005018
Alexander Duyck527d47c2008-11-27 00:21:39 -08005019 /* Links status message must follow this format */
Carolyn Wybornyc75c4ed2014-04-11 01:45:17 +00005020 netdev_info(netdev, "igb: %s NIC Link is Down\n",
Alexander Duyck527d47c2008-11-27 00:21:39 -08005021 netdev->name);
Auke Kok9d5c8242008-01-24 02:22:38 -08005022 netif_carrier_off(netdev);
Alexander Duyck4b1a9872009-02-06 23:19:50 +00005023
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005024 igb_ping_all_vfs(adapter);
5025
Alexander Duyck4b1a9872009-02-06 23:19:50 +00005026 /* link state has changed, schedule phy info update */
Auke Kok9d5c8242008-01-24 02:22:38 -08005027 if (!test_bit(__IGB_DOWN, &adapter->state))
5028 mod_timer(&adapter->phy_info_timer,
5029 round_jiffies(jiffies + 2 * HZ));
Yan, Zheng749ab2c2012-01-04 20:23:37 +00005030
Carolyn Wyborny56cec242013-10-17 05:36:26 +00005031 /* link is down, time to check for alternate media */
5032 if (adapter->flags & IGB_FLAG_MAS_ENABLE) {
5033 igb_check_swap_media(adapter);
5034 if (adapter->flags & IGB_FLAG_MEDIA_RESET) {
5035 schedule_work(&adapter->reset_task);
5036 /* return immediately */
5037 return;
5038 }
5039 }
Yan, Zheng749ab2c2012-01-04 20:23:37 +00005040 pm_schedule_suspend(netdev->dev.parent,
5041 MSEC_PER_SEC * 5);
Carolyn Wyborny56cec242013-10-17 05:36:26 +00005042
5043 /* also check for alternate media here */
5044 } else if (!netif_carrier_ok(netdev) &&
5045 (adapter->flags & IGB_FLAG_MAS_ENABLE)) {
5046 igb_check_swap_media(adapter);
5047 if (adapter->flags & IGB_FLAG_MEDIA_RESET) {
5048 schedule_work(&adapter->reset_task);
5049 /* return immediately */
5050 return;
5051 }
Auke Kok9d5c8242008-01-24 02:22:38 -08005052 }
5053 }
5054
Eric Dumazet12dcd862010-10-15 17:27:10 +00005055 spin_lock(&adapter->stats64_lock);
Benjamin Poirier81e3f642017-05-16 15:55:16 -07005056 igb_update_stats(adapter);
Eric Dumazet12dcd862010-10-15 17:27:10 +00005057 spin_unlock(&adapter->stats64_lock);
Auke Kok9d5c8242008-01-24 02:22:38 -08005058
Alexander Duyckdbabb062009-11-12 18:38:16 +00005059 for (i = 0; i < adapter->num_tx_queues; i++) {
Alexander Duyck3025a442010-02-17 01:02:39 +00005060 struct igb_ring *tx_ring = adapter->tx_ring[i];
Alexander Duyckdbabb062009-11-12 18:38:16 +00005061 if (!netif_carrier_ok(netdev)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08005062 /* We've lost link, so the controller stops DMA,
5063 * but we've got queued Tx work that's never going
5064 * to get done, so reset controller to flush Tx.
Jeff Kirsherb980ac12013-02-23 07:29:56 +00005065 * (Do the reset outside of interrupt context).
5066 */
Alexander Duyckdbabb062009-11-12 18:38:16 +00005067 if (igb_desc_unused(tx_ring) + 1 < tx_ring->count) {
5068 adapter->tx_timeout_count++;
5069 schedule_work(&adapter->reset_task);
5070 /* return immediately since reset is imminent */
5071 return;
5072 }
Auke Kok9d5c8242008-01-24 02:22:38 -08005073 }
Auke Kok9d5c8242008-01-24 02:22:38 -08005074
Alexander Duyckdbabb062009-11-12 18:38:16 +00005075 /* Force detection of hung controller every watchdog period */
Alexander Duyck6d095fa2011-08-26 07:46:19 +00005076 set_bit(IGB_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags);
Alexander Duyckdbabb062009-11-12 18:38:16 +00005077 }
Alexander Duyckf7ba2052009-10-27 23:48:51 +00005078
Jeff Kirsherb980ac12013-02-23 07:29:56 +00005079 /* Cause software interrupt to ensure Rx ring is cleaned */
Carolyn Wybornycd14ef52013-12-10 07:58:34 +00005080 if (adapter->flags & IGB_FLAG_HAS_MSIX) {
Alexander Duyck047e0032009-10-27 15:49:27 +00005081 u32 eics = 0;
Carolyn Wyborny9005df32014-04-11 01:45:34 +00005082
Alexander Duyck0d1ae7f2011-08-26 07:46:34 +00005083 for (i = 0; i < adapter->num_q_vectors; i++)
5084 eics |= adapter->q_vector[i]->eims_value;
Alexander Duyck7a6ea552008-08-26 04:25:03 -07005085 wr32(E1000_EICS, eics);
5086 } else {
5087 wr32(E1000_ICS, E1000_ICS_RXDMT0);
5088 }
Auke Kok9d5c8242008-01-24 02:22:38 -08005089
Greg Rose13800462010-11-06 02:08:26 +00005090 igb_spoof_check(adapter);
Matthew Vickfc580752012-12-13 07:20:35 +00005091 igb_ptp_rx_hang(adapter);
Jacob Kellere5f36ad2017-05-03 10:29:03 -07005092 igb_ptp_tx_hang(adapter);
Greg Rose13800462010-11-06 02:08:26 +00005093
Carolyn Wyborny1516f0a2014-07-09 04:55:45 +00005094 /* Check LVMMC register on i350/i354 only */
5095 if ((adapter->hw.mac.type == e1000_i350) ||
5096 (adapter->hw.mac.type == e1000_i354))
5097 igb_check_lvmmc(adapter);
5098
Auke Kok9d5c8242008-01-24 02:22:38 -08005099 /* Reset the timer */
Akeem G Abodunrinaa9b8cc2013-08-28 02:22:43 +00005100 if (!test_bit(__IGB_DOWN, &adapter->state)) {
5101 if (adapter->flags & IGB_FLAG_NEED_LINK_UPDATE)
5102 mod_timer(&adapter->watchdog_timer,
5103 round_jiffies(jiffies + HZ));
5104 else
5105 mod_timer(&adapter->watchdog_timer,
5106 round_jiffies(jiffies + 2 * HZ));
5107 }
Auke Kok9d5c8242008-01-24 02:22:38 -08005108}
5109
5110enum latency_range {
5111 lowest_latency = 0,
5112 low_latency = 1,
5113 bulk_latency = 2,
5114 latency_invalid = 255
5115};
5116
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07005117/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00005118 * igb_update_ring_itr - update the dynamic ITR value based on packet size
5119 * @q_vector: pointer to q_vector
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07005120 *
Jeff Kirsherb980ac12013-02-23 07:29:56 +00005121 * Stores a new ITR value based on strictly on packet size. This
5122 * algorithm is less sophisticated than that used in igb_update_itr,
5123 * due to the difficulty of synchronizing statistics across multiple
5124 * receive rings. The divisors and thresholds used by this function
5125 * were determined based on theoretical maximum wire speed and testing
5126 * data, in order to minimize response time while increasing bulk
5127 * throughput.
Fernando Luis Vazquez Cao406d4962014-03-18 00:26:48 -07005128 * This functionality is controlled by ethtool's coalescing settings.
Jeff Kirsherb980ac12013-02-23 07:29:56 +00005129 * NOTE: This function is called only when operating in a multiqueue
5130 * receive environment.
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07005131 **/
Alexander Duyck047e0032009-10-27 15:49:27 +00005132static void igb_update_ring_itr(struct igb_q_vector *q_vector)
Auke Kok9d5c8242008-01-24 02:22:38 -08005133{
Alexander Duyck047e0032009-10-27 15:49:27 +00005134 int new_val = q_vector->itr_val;
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07005135 int avg_wire_size = 0;
Alexander Duyck047e0032009-10-27 15:49:27 +00005136 struct igb_adapter *adapter = q_vector->adapter;
Eric Dumazet12dcd862010-10-15 17:27:10 +00005137 unsigned int packets;
Auke Kok9d5c8242008-01-24 02:22:38 -08005138
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07005139 /* For non-gigabit speeds, just fix the interrupt rate at 4000
5140 * ints/sec - ITR timer value of 120 ticks.
5141 */
5142 if (adapter->link_speed != SPEED_1000) {
Alexander Duyck0ba82992011-08-26 07:45:47 +00005143 new_val = IGB_4K_ITR;
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07005144 goto set_itr_val;
5145 }
Alexander Duyck047e0032009-10-27 15:49:27 +00005146
Alexander Duyck0ba82992011-08-26 07:45:47 +00005147 packets = q_vector->rx.total_packets;
5148 if (packets)
5149 avg_wire_size = q_vector->rx.total_bytes / packets;
Eric Dumazet12dcd862010-10-15 17:27:10 +00005150
Alexander Duyck0ba82992011-08-26 07:45:47 +00005151 packets = q_vector->tx.total_packets;
5152 if (packets)
5153 avg_wire_size = max_t(u32, avg_wire_size,
5154 q_vector->tx.total_bytes / packets);
Alexander Duyck047e0032009-10-27 15:49:27 +00005155
5156 /* if avg_wire_size isn't set no work was done */
5157 if (!avg_wire_size)
5158 goto clear_counts;
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07005159
5160 /* Add 24 bytes to size to account for CRC, preamble, and gap */
5161 avg_wire_size += 24;
5162
5163 /* Don't starve jumbo frames */
5164 avg_wire_size = min(avg_wire_size, 3000);
5165
5166 /* Give a little boost to mid-size frames */
5167 if ((avg_wire_size > 300) && (avg_wire_size < 1200))
5168 new_val = avg_wire_size / 3;
5169 else
5170 new_val = avg_wire_size / 2;
5171
Alexander Duyck0ba82992011-08-26 07:45:47 +00005172 /* conservative mode (itr 3) eliminates the lowest_latency setting */
5173 if (new_val < IGB_20K_ITR &&
5174 ((q_vector->rx.ring && adapter->rx_itr_setting == 3) ||
5175 (!q_vector->rx.ring && adapter->tx_itr_setting == 3)))
5176 new_val = IGB_20K_ITR;
Nick Nunleyabe1c362010-02-17 01:03:19 +00005177
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07005178set_itr_val:
Alexander Duyck047e0032009-10-27 15:49:27 +00005179 if (new_val != q_vector->itr_val) {
5180 q_vector->itr_val = new_val;
5181 q_vector->set_itr = 1;
Auke Kok9d5c8242008-01-24 02:22:38 -08005182 }
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07005183clear_counts:
Alexander Duyck0ba82992011-08-26 07:45:47 +00005184 q_vector->rx.total_bytes = 0;
5185 q_vector->rx.total_packets = 0;
5186 q_vector->tx.total_bytes = 0;
5187 q_vector->tx.total_packets = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08005188}
5189
5190/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00005191 * igb_update_itr - update the dynamic ITR value based on statistics
5192 * @q_vector: pointer to q_vector
5193 * @ring_container: ring info to update the itr for
5194 *
5195 * Stores a new ITR value based on packets and byte
5196 * counts during the last interrupt. The advantage of per interrupt
5197 * computation is faster updates and more accurate ITR for the current
5198 * traffic pattern. Constants in this function were computed
5199 * based on theoretical maximum wire speed and thresholds were set based
5200 * on testing data as well as attempting to minimize response time
5201 * while increasing bulk throughput.
Fernando Luis Vazquez Cao406d4962014-03-18 00:26:48 -07005202 * This functionality is controlled by ethtool's coalescing settings.
Jeff Kirsherb980ac12013-02-23 07:29:56 +00005203 * NOTE: These calculations are only valid when operating in a single-
5204 * queue environment.
Auke Kok9d5c8242008-01-24 02:22:38 -08005205 **/
Alexander Duyck0ba82992011-08-26 07:45:47 +00005206static void igb_update_itr(struct igb_q_vector *q_vector,
5207 struct igb_ring_container *ring_container)
Auke Kok9d5c8242008-01-24 02:22:38 -08005208{
Alexander Duyck0ba82992011-08-26 07:45:47 +00005209 unsigned int packets = ring_container->total_packets;
5210 unsigned int bytes = ring_container->total_bytes;
5211 u8 itrval = ring_container->itr;
Auke Kok9d5c8242008-01-24 02:22:38 -08005212
Alexander Duyck0ba82992011-08-26 07:45:47 +00005213 /* no packets, exit with status unchanged */
Auke Kok9d5c8242008-01-24 02:22:38 -08005214 if (packets == 0)
Alexander Duyck0ba82992011-08-26 07:45:47 +00005215 return;
Auke Kok9d5c8242008-01-24 02:22:38 -08005216
Alexander Duyck0ba82992011-08-26 07:45:47 +00005217 switch (itrval) {
Auke Kok9d5c8242008-01-24 02:22:38 -08005218 case lowest_latency:
5219 /* handle TSO and jumbo frames */
5220 if (bytes/packets > 8000)
Alexander Duyck0ba82992011-08-26 07:45:47 +00005221 itrval = bulk_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08005222 else if ((packets < 5) && (bytes > 512))
Alexander Duyck0ba82992011-08-26 07:45:47 +00005223 itrval = low_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08005224 break;
5225 case low_latency: /* 50 usec aka 20000 ints/s */
5226 if (bytes > 10000) {
5227 /* this if handles the TSO accounting */
Carolyn Wybornyd34a15a2014-04-11 01:45:23 +00005228 if (bytes/packets > 8000)
Alexander Duyck0ba82992011-08-26 07:45:47 +00005229 itrval = bulk_latency;
Carolyn Wybornyd34a15a2014-04-11 01:45:23 +00005230 else if ((packets < 10) || ((bytes/packets) > 1200))
Alexander Duyck0ba82992011-08-26 07:45:47 +00005231 itrval = bulk_latency;
Carolyn Wybornyd34a15a2014-04-11 01:45:23 +00005232 else if ((packets > 35))
Alexander Duyck0ba82992011-08-26 07:45:47 +00005233 itrval = lowest_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08005234 } else if (bytes/packets > 2000) {
Alexander Duyck0ba82992011-08-26 07:45:47 +00005235 itrval = bulk_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08005236 } else if (packets <= 2 && bytes < 512) {
Alexander Duyck0ba82992011-08-26 07:45:47 +00005237 itrval = lowest_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08005238 }
5239 break;
5240 case bulk_latency: /* 250 usec aka 4000 ints/s */
5241 if (bytes > 25000) {
5242 if (packets > 35)
Alexander Duyck0ba82992011-08-26 07:45:47 +00005243 itrval = low_latency;
Alexander Duyck1e5c3d22009-02-12 18:17:21 +00005244 } else if (bytes < 1500) {
Alexander Duyck0ba82992011-08-26 07:45:47 +00005245 itrval = low_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08005246 }
5247 break;
5248 }
5249
Alexander Duyck0ba82992011-08-26 07:45:47 +00005250 /* clear work counters since we have the values we need */
5251 ring_container->total_bytes = 0;
5252 ring_container->total_packets = 0;
5253
5254 /* write updated itr to ring container */
5255 ring_container->itr = itrval;
Auke Kok9d5c8242008-01-24 02:22:38 -08005256}
5257
Alexander Duyck0ba82992011-08-26 07:45:47 +00005258static void igb_set_itr(struct igb_q_vector *q_vector)
Auke Kok9d5c8242008-01-24 02:22:38 -08005259{
Alexander Duyck0ba82992011-08-26 07:45:47 +00005260 struct igb_adapter *adapter = q_vector->adapter;
Alexander Duyck047e0032009-10-27 15:49:27 +00005261 u32 new_itr = q_vector->itr_val;
Alexander Duyck0ba82992011-08-26 07:45:47 +00005262 u8 current_itr = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08005263
5264 /* for non-gigabit speeds, just fix the interrupt rate at 4000 */
5265 if (adapter->link_speed != SPEED_1000) {
5266 current_itr = 0;
Alexander Duyck0ba82992011-08-26 07:45:47 +00005267 new_itr = IGB_4K_ITR;
Auke Kok9d5c8242008-01-24 02:22:38 -08005268 goto set_itr_now;
5269 }
5270
Alexander Duyck0ba82992011-08-26 07:45:47 +00005271 igb_update_itr(q_vector, &q_vector->tx);
5272 igb_update_itr(q_vector, &q_vector->rx);
Auke Kok9d5c8242008-01-24 02:22:38 -08005273
Alexander Duyck0ba82992011-08-26 07:45:47 +00005274 current_itr = max(q_vector->rx.itr, q_vector->tx.itr);
Auke Kok9d5c8242008-01-24 02:22:38 -08005275
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07005276 /* conservative mode (itr 3) eliminates the lowest_latency setting */
Alexander Duyck0ba82992011-08-26 07:45:47 +00005277 if (current_itr == lowest_latency &&
5278 ((q_vector->rx.ring && adapter->rx_itr_setting == 3) ||
5279 (!q_vector->rx.ring && adapter->tx_itr_setting == 3)))
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07005280 current_itr = low_latency;
5281
Auke Kok9d5c8242008-01-24 02:22:38 -08005282 switch (current_itr) {
5283 /* counts and packets in update_itr are dependent on these numbers */
5284 case lowest_latency:
Alexander Duyck0ba82992011-08-26 07:45:47 +00005285 new_itr = IGB_70K_ITR; /* 70,000 ints/sec */
Auke Kok9d5c8242008-01-24 02:22:38 -08005286 break;
5287 case low_latency:
Alexander Duyck0ba82992011-08-26 07:45:47 +00005288 new_itr = IGB_20K_ITR; /* 20,000 ints/sec */
Auke Kok9d5c8242008-01-24 02:22:38 -08005289 break;
5290 case bulk_latency:
Alexander Duyck0ba82992011-08-26 07:45:47 +00005291 new_itr = IGB_4K_ITR; /* 4,000 ints/sec */
Auke Kok9d5c8242008-01-24 02:22:38 -08005292 break;
5293 default:
5294 break;
5295 }
5296
5297set_itr_now:
Alexander Duyck047e0032009-10-27 15:49:27 +00005298 if (new_itr != q_vector->itr_val) {
Auke Kok9d5c8242008-01-24 02:22:38 -08005299 /* this attempts to bias the interrupt rate towards Bulk
5300 * by adding intermediate steps when interrupt rate is
Jeff Kirsherb980ac12013-02-23 07:29:56 +00005301 * increasing
5302 */
Alexander Duyck047e0032009-10-27 15:49:27 +00005303 new_itr = new_itr > q_vector->itr_val ?
Jeff Kirsherb980ac12013-02-23 07:29:56 +00005304 max((new_itr * q_vector->itr_val) /
5305 (new_itr + (q_vector->itr_val >> 2)),
5306 new_itr) : new_itr;
Auke Kok9d5c8242008-01-24 02:22:38 -08005307 /* Don't write the value here; it resets the adapter's
5308 * internal timer, and causes us to delay far longer than
5309 * we should between interrupts. Instead, we write the ITR
5310 * value at the beginning of the next interrupt so the timing
5311 * ends up being correct.
5312 */
Alexander Duyck047e0032009-10-27 15:49:27 +00005313 q_vector->itr_val = new_itr;
5314 q_vector->set_itr = 1;
Auke Kok9d5c8242008-01-24 02:22:38 -08005315 }
Auke Kok9d5c8242008-01-24 02:22:38 -08005316}
5317
Stephen Hemmingerc50b52a2012-01-18 22:13:26 +00005318static void igb_tx_ctxtdesc(struct igb_ring *tx_ring, u32 vlan_macip_lens,
5319 u32 type_tucmd, u32 mss_l4len_idx)
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00005320{
5321 struct e1000_adv_tx_context_desc *context_desc;
5322 u16 i = tx_ring->next_to_use;
5323
5324 context_desc = IGB_TX_CTXTDESC(tx_ring, i);
5325
5326 i++;
5327 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
5328
5329 /* set bits to identify this as an advanced context descriptor */
5330 type_tucmd |= E1000_TXD_CMD_DEXT | E1000_ADVTXD_DTYP_CTXT;
5331
5332 /* For 82575, context index must be unique per ring. */
Alexander Duyck866cff02011-08-26 07:45:36 +00005333 if (test_bit(IGB_RING_FLAG_TX_CTX_IDX, &tx_ring->flags))
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00005334 mss_l4len_idx |= tx_ring->reg_idx << 4;
5335
5336 context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens);
5337 context_desc->seqnum_seed = 0;
5338 context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd);
5339 context_desc->mss_l4len_idx = cpu_to_le32(mss_l4len_idx);
5340}
5341
Alexander Duyck7af40ad92011-08-26 07:45:15 +00005342static int igb_tso(struct igb_ring *tx_ring,
5343 struct igb_tx_buffer *first,
5344 u8 *hdr_len)
Auke Kok9d5c8242008-01-24 02:22:38 -08005345{
Alexander Duycke10715d2016-04-14 17:19:38 -04005346 u32 vlan_macip_lens, type_tucmd, mss_l4len_idx;
Alexander Duyck7af40ad92011-08-26 07:45:15 +00005347 struct sk_buff *skb = first->skb;
Alexander Duycke10715d2016-04-14 17:19:38 -04005348 union {
5349 struct iphdr *v4;
5350 struct ipv6hdr *v6;
5351 unsigned char *hdr;
5352 } ip;
5353 union {
5354 struct tcphdr *tcp;
5355 unsigned char *hdr;
5356 } l4;
5357 u32 paylen, l4_offset;
Francois Romieu06c14e52014-03-30 03:14:11 +00005358 int err;
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00005359
Alexander Duycked6aa102012-11-13 04:03:22 +00005360 if (skb->ip_summed != CHECKSUM_PARTIAL)
5361 return 0;
5362
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00005363 if (!skb_is_gso(skb))
5364 return 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08005365
Francois Romieu06c14e52014-03-30 03:14:11 +00005366 err = skb_cow_head(skb, 0);
5367 if (err < 0)
5368 return err;
Auke Kok9d5c8242008-01-24 02:22:38 -08005369
Alexander Duycke10715d2016-04-14 17:19:38 -04005370 ip.hdr = skb_network_header(skb);
5371 l4.hdr = skb_checksum_start(skb);
5372
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00005373 /* ADV DTYP TUCMD MKRLOC/ISCSIHEDLEN */
5374 type_tucmd = E1000_ADVTXD_TUCMD_L4T_TCP;
Auke Kok9d5c8242008-01-24 02:22:38 -08005375
Alexander Duycke10715d2016-04-14 17:19:38 -04005376 /* initialize outer IP header fields */
5377 if (ip.v4->version == 4) {
Alexander Duyck516165a2016-11-28 10:42:23 -05005378 unsigned char *csum_start = skb_checksum_start(skb);
5379 unsigned char *trans_start = ip.hdr + (ip.v4->ihl * 4);
5380
Alexander Duycke10715d2016-04-14 17:19:38 -04005381 /* IP header will have to cancel out any data that
5382 * is not a part of the outer IP header
5383 */
Alexander Duyck516165a2016-11-28 10:42:23 -05005384 ip.v4->check = csum_fold(csum_partial(trans_start,
5385 csum_start - trans_start,
5386 0));
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00005387 type_tucmd |= E1000_ADVTXD_TUCMD_IPV4;
Alexander Duycke10715d2016-04-14 17:19:38 -04005388
5389 ip.v4->tot_len = 0;
Alexander Duyck7af40ad92011-08-26 07:45:15 +00005390 first->tx_flags |= IGB_TX_FLAGS_TSO |
5391 IGB_TX_FLAGS_CSUM |
5392 IGB_TX_FLAGS_IPV4;
Alexander Duycke10715d2016-04-14 17:19:38 -04005393 } else {
5394 ip.v6->payload_len = 0;
Alexander Duyck7af40ad92011-08-26 07:45:15 +00005395 first->tx_flags |= IGB_TX_FLAGS_TSO |
5396 IGB_TX_FLAGS_CSUM;
Auke Kok9d5c8242008-01-24 02:22:38 -08005397 }
5398
Alexander Duycke10715d2016-04-14 17:19:38 -04005399 /* determine offset of inner transport header */
5400 l4_offset = l4.hdr - skb->data;
5401
5402 /* compute length of segmentation header */
5403 *hdr_len = (l4.tcp->doff * 4) + l4_offset;
5404
5405 /* remove payload length from inner checksum */
5406 paylen = skb->len - l4_offset;
5407 csum_replace_by_diff(&l4.tcp->check, htonl(paylen));
Auke Kok9d5c8242008-01-24 02:22:38 -08005408
Alexander Duyck7af40ad92011-08-26 07:45:15 +00005409 /* update gso size and bytecount with header size */
5410 first->gso_segs = skb_shinfo(skb)->gso_segs;
5411 first->bytecount += (first->gso_segs - 1) * *hdr_len;
5412
Auke Kok9d5c8242008-01-24 02:22:38 -08005413 /* MSS L4LEN IDX */
Alexander Duycke10715d2016-04-14 17:19:38 -04005414 mss_l4len_idx = (*hdr_len - l4_offset) << E1000_ADVTXD_L4LEN_SHIFT;
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00005415 mss_l4len_idx |= skb_shinfo(skb)->gso_size << E1000_ADVTXD_MSS_SHIFT;
Auke Kok9d5c8242008-01-24 02:22:38 -08005416
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00005417 /* VLAN MACLEN IPLEN */
Alexander Duycke10715d2016-04-14 17:19:38 -04005418 vlan_macip_lens = l4.hdr - ip.hdr;
5419 vlan_macip_lens |= (ip.hdr - skb->data) << E1000_ADVTXD_MACLEN_SHIFT;
Alexander Duyck7af40ad92011-08-26 07:45:15 +00005420 vlan_macip_lens |= first->tx_flags & IGB_TX_FLAGS_VLAN_MASK;
Auke Kok9d5c8242008-01-24 02:22:38 -08005421
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00005422 igb_tx_ctxtdesc(tx_ring, vlan_macip_lens, type_tucmd, mss_l4len_idx);
Auke Kok9d5c8242008-01-24 02:22:38 -08005423
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00005424 return 1;
Auke Kok9d5c8242008-01-24 02:22:38 -08005425}
5426
Alexander Duyck6e033702016-01-13 07:31:23 -08005427static inline bool igb_ipv6_csum_is_sctp(struct sk_buff *skb)
5428{
5429 unsigned int offset = 0;
5430
5431 ipv6_find_hdr(skb, &offset, IPPROTO_SCTP, NULL, NULL);
5432
5433 return offset == skb_checksum_start_offset(skb);
5434}
5435
Alexander Duyck7af40ad92011-08-26 07:45:15 +00005436static void igb_tx_csum(struct igb_ring *tx_ring, struct igb_tx_buffer *first)
Auke Kok9d5c8242008-01-24 02:22:38 -08005437{
Alexander Duyck7af40ad92011-08-26 07:45:15 +00005438 struct sk_buff *skb = first->skb;
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00005439 u32 vlan_macip_lens = 0;
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00005440 u32 type_tucmd = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08005441
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00005442 if (skb->ip_summed != CHECKSUM_PARTIAL) {
Alexander Duyck6e033702016-01-13 07:31:23 -08005443csum_failed:
Alexander Duyck7af40ad92011-08-26 07:45:15 +00005444 if (!(first->tx_flags & IGB_TX_FLAGS_VLAN))
5445 return;
Alexander Duyck6e033702016-01-13 07:31:23 -08005446 goto no_csum;
Auke Kok9d5c8242008-01-24 02:22:38 -08005447 }
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00005448
Alexander Duyck6e033702016-01-13 07:31:23 -08005449 switch (skb->csum_offset) {
5450 case offsetof(struct tcphdr, check):
5451 type_tucmd = E1000_ADVTXD_TUCMD_L4T_TCP;
5452 /* fall through */
5453 case offsetof(struct udphdr, check):
5454 break;
5455 case offsetof(struct sctphdr, checksum):
5456 /* validate that this is actually an SCTP request */
5457 if (((first->protocol == htons(ETH_P_IP)) &&
5458 (ip_hdr(skb)->protocol == IPPROTO_SCTP)) ||
5459 ((first->protocol == htons(ETH_P_IPV6)) &&
5460 igb_ipv6_csum_is_sctp(skb))) {
5461 type_tucmd = E1000_ADVTXD_TUCMD_L4T_SCTP;
5462 break;
5463 }
5464 default:
5465 skb_checksum_help(skb);
5466 goto csum_failed;
5467 }
5468
5469 /* update TX checksum flag */
5470 first->tx_flags |= IGB_TX_FLAGS_CSUM;
5471 vlan_macip_lens = skb_checksum_start_offset(skb) -
5472 skb_network_offset(skb);
5473no_csum:
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00005474 vlan_macip_lens |= skb_network_offset(skb) << E1000_ADVTXD_MACLEN_SHIFT;
Alexander Duyck7af40ad92011-08-26 07:45:15 +00005475 vlan_macip_lens |= first->tx_flags & IGB_TX_FLAGS_VLAN_MASK;
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00005476
Alexander Duyck6e033702016-01-13 07:31:23 -08005477 igb_tx_ctxtdesc(tx_ring, vlan_macip_lens, type_tucmd, 0);
Auke Kok9d5c8242008-01-24 02:22:38 -08005478}
5479
Alexander Duyck1d9daf42012-11-13 04:03:23 +00005480#define IGB_SET_FLAG(_input, _flag, _result) \
5481 ((_flag <= _result) ? \
5482 ((u32)(_input & _flag) * (_result / _flag)) : \
5483 ((u32)(_input & _flag) / (_flag / _result)))
5484
5485static u32 igb_tx_cmd_type(struct sk_buff *skb, u32 tx_flags)
Alexander Duycke032afc2011-08-26 07:44:48 +00005486{
5487 /* set type for advanced descriptor with frame checksum insertion */
Alexander Duyck1d9daf42012-11-13 04:03:23 +00005488 u32 cmd_type = E1000_ADVTXD_DTYP_DATA |
5489 E1000_ADVTXD_DCMD_DEXT |
5490 E1000_ADVTXD_DCMD_IFCS;
Alexander Duycke032afc2011-08-26 07:44:48 +00005491
5492 /* set HW vlan bit if vlan is present */
Alexander Duyck1d9daf42012-11-13 04:03:23 +00005493 cmd_type |= IGB_SET_FLAG(tx_flags, IGB_TX_FLAGS_VLAN,
5494 (E1000_ADVTXD_DCMD_VLE));
Alexander Duycke032afc2011-08-26 07:44:48 +00005495
5496 /* set segmentation bits for TSO */
Alexander Duyck1d9daf42012-11-13 04:03:23 +00005497 cmd_type |= IGB_SET_FLAG(tx_flags, IGB_TX_FLAGS_TSO,
5498 (E1000_ADVTXD_DCMD_TSE));
5499
5500 /* set timestamp bit if present */
5501 cmd_type |= IGB_SET_FLAG(tx_flags, IGB_TX_FLAGS_TSTAMP,
5502 (E1000_ADVTXD_MAC_TSTAMP));
5503
5504 /* insert frame checksum */
5505 cmd_type ^= IGB_SET_FLAG(skb->no_fcs, 1, E1000_ADVTXD_DCMD_IFCS);
Alexander Duycke032afc2011-08-26 07:44:48 +00005506
5507 return cmd_type;
5508}
5509
Alexander Duyck7af40ad92011-08-26 07:45:15 +00005510static void igb_tx_olinfo_status(struct igb_ring *tx_ring,
5511 union e1000_adv_tx_desc *tx_desc,
5512 u32 tx_flags, unsigned int paylen)
Alexander Duycke032afc2011-08-26 07:44:48 +00005513{
5514 u32 olinfo_status = paylen << E1000_ADVTXD_PAYLEN_SHIFT;
5515
Alexander Duyck1d9daf42012-11-13 04:03:23 +00005516 /* 82575 requires a unique index per ring */
5517 if (test_bit(IGB_RING_FLAG_TX_CTX_IDX, &tx_ring->flags))
Alexander Duycke032afc2011-08-26 07:44:48 +00005518 olinfo_status |= tx_ring->reg_idx << 4;
5519
5520 /* insert L4 checksum */
Alexander Duyck1d9daf42012-11-13 04:03:23 +00005521 olinfo_status |= IGB_SET_FLAG(tx_flags,
5522 IGB_TX_FLAGS_CSUM,
5523 (E1000_TXD_POPTS_TXSM << 8));
Alexander Duycke032afc2011-08-26 07:44:48 +00005524
Alexander Duyck1d9daf42012-11-13 04:03:23 +00005525 /* insert IPv4 checksum */
5526 olinfo_status |= IGB_SET_FLAG(tx_flags,
5527 IGB_TX_FLAGS_IPV4,
5528 (E1000_TXD_POPTS_IXSM << 8));
Alexander Duycke032afc2011-08-26 07:44:48 +00005529
Alexander Duyck7af40ad92011-08-26 07:45:15 +00005530 tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status);
Alexander Duycke032afc2011-08-26 07:44:48 +00005531}
5532
David S. Miller6f19e122014-08-28 01:39:31 -07005533static int __igb_maybe_stop_tx(struct igb_ring *tx_ring, const u16 size)
5534{
5535 struct net_device *netdev = tx_ring->netdev;
5536
5537 netif_stop_subqueue(netdev, tx_ring->queue_index);
5538
5539 /* Herbert's original patch had:
5540 * smp_mb__after_netif_stop_queue();
5541 * but since that doesn't exist yet, just open code it.
5542 */
5543 smp_mb();
5544
5545 /* We need to check again in a case another CPU has just
5546 * made room available.
5547 */
5548 if (igb_desc_unused(tx_ring) < size)
5549 return -EBUSY;
5550
5551 /* A reprieve! */
5552 netif_wake_subqueue(netdev, tx_ring->queue_index);
5553
5554 u64_stats_update_begin(&tx_ring->tx_syncp2);
5555 tx_ring->tx_stats.restart_queue2++;
5556 u64_stats_update_end(&tx_ring->tx_syncp2);
5557
5558 return 0;
5559}
5560
5561static inline int igb_maybe_stop_tx(struct igb_ring *tx_ring, const u16 size)
5562{
5563 if (igb_desc_unused(tx_ring) >= size)
5564 return 0;
5565 return __igb_maybe_stop_tx(tx_ring, size);
5566}
5567
Jacob Keller74344e32017-05-03 10:28:55 -07005568static int igb_tx_map(struct igb_ring *tx_ring,
5569 struct igb_tx_buffer *first,
5570 const u8 hdr_len)
Auke Kok9d5c8242008-01-24 02:22:38 -08005571{
Alexander Duyck7af40ad92011-08-26 07:45:15 +00005572 struct sk_buff *skb = first->skb;
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00005573 struct igb_tx_buffer *tx_buffer;
Alexander Duyckebe42d12011-08-26 07:45:09 +00005574 union e1000_adv_tx_desc *tx_desc;
Alexander Duyck80d07592012-11-13 04:03:24 +00005575 struct skb_frag_struct *frag;
Alexander Duyckebe42d12011-08-26 07:45:09 +00005576 dma_addr_t dma;
Alexander Duyck80d07592012-11-13 04:03:24 +00005577 unsigned int data_len, size;
Alexander Duyck7af40ad92011-08-26 07:45:15 +00005578 u32 tx_flags = first->tx_flags;
Alexander Duyck1d9daf42012-11-13 04:03:23 +00005579 u32 cmd_type = igb_tx_cmd_type(skb, tx_flags);
Alexander Duyckebe42d12011-08-26 07:45:09 +00005580 u16 i = tx_ring->next_to_use;
Alexander Duyckebe42d12011-08-26 07:45:09 +00005581
5582 tx_desc = IGB_TX_DESC(tx_ring, i);
5583
Alexander Duyck80d07592012-11-13 04:03:24 +00005584 igb_tx_olinfo_status(tx_ring, tx_desc, tx_flags, skb->len - hdr_len);
5585
5586 size = skb_headlen(skb);
5587 data_len = skb->data_len;
Alexander Duyckebe42d12011-08-26 07:45:09 +00005588
5589 dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
Auke Kok9d5c8242008-01-24 02:22:38 -08005590
Alexander Duyck80d07592012-11-13 04:03:24 +00005591 tx_buffer = first;
Alexander Duyck2bbfebe2011-08-26 07:44:59 +00005592
Alexander Duyck80d07592012-11-13 04:03:24 +00005593 for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
5594 if (dma_mapping_error(tx_ring->dev, dma))
5595 goto dma_error;
5596
5597 /* record length, and DMA address */
5598 dma_unmap_len_set(tx_buffer, len, size);
5599 dma_unmap_addr_set(tx_buffer, dma, dma);
5600
5601 tx_desc->read.buffer_addr = cpu_to_le64(dma);
5602
Alexander Duyckebe42d12011-08-26 07:45:09 +00005603 while (unlikely(size > IGB_MAX_DATA_PER_TXD)) {
5604 tx_desc->read.cmd_type_len =
Alexander Duyck1d9daf42012-11-13 04:03:23 +00005605 cpu_to_le32(cmd_type ^ IGB_MAX_DATA_PER_TXD);
Auke Kok9d5c8242008-01-24 02:22:38 -08005606
Alexander Duyckebe42d12011-08-26 07:45:09 +00005607 i++;
5608 tx_desc++;
5609 if (i == tx_ring->count) {
5610 tx_desc = IGB_TX_DESC(tx_ring, 0);
5611 i = 0;
5612 }
Alexander Duyck80d07592012-11-13 04:03:24 +00005613 tx_desc->read.olinfo_status = 0;
Alexander Duyckebe42d12011-08-26 07:45:09 +00005614
5615 dma += IGB_MAX_DATA_PER_TXD;
5616 size -= IGB_MAX_DATA_PER_TXD;
5617
Alexander Duyckebe42d12011-08-26 07:45:09 +00005618 tx_desc->read.buffer_addr = cpu_to_le64(dma);
5619 }
5620
5621 if (likely(!data_len))
5622 break;
5623
Alexander Duyck1d9daf42012-11-13 04:03:23 +00005624 tx_desc->read.cmd_type_len = cpu_to_le32(cmd_type ^ size);
Alexander Duyckebe42d12011-08-26 07:45:09 +00005625
Alexander Duyck65689fe2009-03-20 00:17:43 +00005626 i++;
Alexander Duyckebe42d12011-08-26 07:45:09 +00005627 tx_desc++;
5628 if (i == tx_ring->count) {
5629 tx_desc = IGB_TX_DESC(tx_ring, 0);
Alexander Duyck65689fe2009-03-20 00:17:43 +00005630 i = 0;
Alexander Duyckebe42d12011-08-26 07:45:09 +00005631 }
Alexander Duyck80d07592012-11-13 04:03:24 +00005632 tx_desc->read.olinfo_status = 0;
Alexander Duyck65689fe2009-03-20 00:17:43 +00005633
Eric Dumazet9e903e02011-10-18 21:00:24 +00005634 size = skb_frag_size(frag);
Alexander Duyckebe42d12011-08-26 07:45:09 +00005635 data_len -= size;
5636
5637 dma = skb_frag_dma_map(tx_ring->dev, frag, 0,
Alexander Duyck80d07592012-11-13 04:03:24 +00005638 size, DMA_TO_DEVICE);
Alexander Duyck6366ad32009-12-02 16:47:18 +00005639
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00005640 tx_buffer = &tx_ring->tx_buffer_info[i];
Auke Kok9d5c8242008-01-24 02:22:38 -08005641 }
5642
Alexander Duyckebe42d12011-08-26 07:45:09 +00005643 /* write last descriptor with RS and EOP bits */
Alexander Duyck1d9daf42012-11-13 04:03:23 +00005644 cmd_type |= size | IGB_TXD_DCMD;
5645 tx_desc->read.cmd_type_len = cpu_to_le32(cmd_type);
Alexander Duyck8542db02011-08-26 07:44:43 +00005646
Alexander Duyck80d07592012-11-13 04:03:24 +00005647 netdev_tx_sent_queue(txring_txq(tx_ring), first->bytecount);
5648
Alexander Duyck8542db02011-08-26 07:44:43 +00005649 /* set the timestamp */
5650 first->time_stamp = jiffies;
5651
Jeff Kirsherb980ac12013-02-23 07:29:56 +00005652 /* Force memory writes to complete before letting h/w know there
Alexander Duyckebe42d12011-08-26 07:45:09 +00005653 * are new descriptors to fetch. (Only applicable for weak-ordered
5654 * memory model archs, such as IA-64).
5655 *
5656 * We also need this memory barrier to make certain all of the
5657 * status bits have been updated before next_to_watch is written.
5658 */
Auke Kok9d5c8242008-01-24 02:22:38 -08005659 wmb();
5660
Alexander Duyckebe42d12011-08-26 07:45:09 +00005661 /* set next_to_watch value indicating a packet is present */
5662 first->next_to_watch = tx_desc;
5663
5664 i++;
5665 if (i == tx_ring->count)
5666 i = 0;
5667
Auke Kok9d5c8242008-01-24 02:22:38 -08005668 tx_ring->next_to_use = i;
Alexander Duyckebe42d12011-08-26 07:45:09 +00005669
David S. Miller6f19e122014-08-28 01:39:31 -07005670 /* Make sure there is space in the ring for the next send. */
5671 igb_maybe_stop_tx(tx_ring, DESC_NEEDED);
5672
5673 if (netif_xmit_stopped(txring_txq(tx_ring)) || !skb->xmit_more) {
David S. Miller0b725a22014-08-25 15:51:53 -07005674 writel(i, tx_ring->tail);
5675
5676 /* we need this if more than one processor can write to our tail
5677 * at a time, it synchronizes IO on IA64/Altix systems
5678 */
5679 mmiowb();
5680 }
Jacob Keller74344e32017-05-03 10:28:55 -07005681 return 0;
Alexander Duyckebe42d12011-08-26 07:45:09 +00005682
5683dma_error:
5684 dev_err(tx_ring->dev, "TX DMA map failed\n");
Alexander Duyck7cc6fd42017-02-06 18:26:02 -08005685 tx_buffer = &tx_ring->tx_buffer_info[i];
Alexander Duyckebe42d12011-08-26 07:45:09 +00005686
5687 /* clear dma mappings for failed tx_buffer_info map */
Alexander Duyck7cc6fd42017-02-06 18:26:02 -08005688 while (tx_buffer != first) {
5689 if (dma_unmap_len(tx_buffer, len))
5690 dma_unmap_page(tx_ring->dev,
5691 dma_unmap_addr(tx_buffer, dma),
5692 dma_unmap_len(tx_buffer, len),
5693 DMA_TO_DEVICE);
5694 dma_unmap_len_set(tx_buffer, len, 0);
5695
Jean-Philippe Brucker104ba832017-10-19 20:07:36 +01005696 if (i-- == 0)
Alexander Duyck7cc6fd42017-02-06 18:26:02 -08005697 i += tx_ring->count;
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00005698 tx_buffer = &tx_ring->tx_buffer_info[i];
Alexander Duyckebe42d12011-08-26 07:45:09 +00005699 }
5700
Alexander Duyck7cc6fd42017-02-06 18:26:02 -08005701 if (dma_unmap_len(tx_buffer, len))
5702 dma_unmap_single(tx_ring->dev,
5703 dma_unmap_addr(tx_buffer, dma),
5704 dma_unmap_len(tx_buffer, len),
5705 DMA_TO_DEVICE);
5706 dma_unmap_len_set(tx_buffer, len, 0);
5707
5708 dev_kfree_skb_any(tx_buffer->skb);
5709 tx_buffer->skb = NULL;
5710
Alexander Duyckebe42d12011-08-26 07:45:09 +00005711 tx_ring->next_to_use = i;
Jacob Keller74344e32017-05-03 10:28:55 -07005712
5713 return -1;
Auke Kok9d5c8242008-01-24 02:22:38 -08005714}
5715
Alexander Duyckcd392f52011-08-26 07:43:59 +00005716netdev_tx_t igb_xmit_frame_ring(struct sk_buff *skb,
5717 struct igb_ring *tx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08005718{
Alexander Duyck8542db02011-08-26 07:44:43 +00005719 struct igb_tx_buffer *first;
Alexander Duyckebe42d12011-08-26 07:45:09 +00005720 int tso;
Nick Nunley91d4ee32010-02-17 01:04:56 +00005721 u32 tx_flags = 0;
Alexander Duyck2ee52ad2015-05-06 21:11:45 -07005722 unsigned short f;
Alexander Duyck21ba6fe2013-02-09 04:27:48 +00005723 u16 count = TXD_USE_COUNT(skb_headlen(skb));
Alexander Duyck31f6adb2011-08-26 07:44:53 +00005724 __be16 protocol = vlan_get_protocol(skb);
Nick Nunley91d4ee32010-02-17 01:04:56 +00005725 u8 hdr_len = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08005726
Alexander Duyck21ba6fe2013-02-09 04:27:48 +00005727 /* need: 1 descriptor per page * PAGE_SIZE/IGB_MAX_DATA_PER_TXD,
5728 * + 1 desc for skb_headlen/IGB_MAX_DATA_PER_TXD,
Auke Kok9d5c8242008-01-24 02:22:38 -08005729 * + 2 desc gap to keep tail from touching head,
Auke Kok9d5c8242008-01-24 02:22:38 -08005730 * + 1 desc for context descriptor,
Alexander Duyck21ba6fe2013-02-09 04:27:48 +00005731 * otherwise try next time
5732 */
Alexander Duyck2ee52ad2015-05-06 21:11:45 -07005733 for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
5734 count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size);
Alexander Duyck21ba6fe2013-02-09 04:27:48 +00005735
5736 if (igb_maybe_stop_tx(tx_ring, count + 3)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08005737 /* this is a hard error */
Auke Kok9d5c8242008-01-24 02:22:38 -08005738 return NETDEV_TX_BUSY;
5739 }
Patrick Ohly33af6bc2009-02-12 05:03:43 +00005740
Alexander Duyck7af40ad92011-08-26 07:45:15 +00005741 /* record the location of the first descriptor for this packet */
5742 first = &tx_ring->tx_buffer_info[tx_ring->next_to_use];
5743 first->skb = skb;
5744 first->bytecount = skb->len;
5745 first->gso_segs = 1;
5746
Alexander Duyckb646c222013-02-07 08:55:46 +00005747 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
5748 struct igb_adapter *adapter = netdev_priv(tx_ring->netdev);
Matthew Vick1f6e8172012-08-18 07:26:33 +00005749
Cliff Spradlin26bd4e22017-06-19 13:30:43 -07005750 if (adapter->tstamp_config.tx_type & HWTSTAMP_TX_ON &&
5751 !test_and_set_bit_lock(__IGB_PTP_TX_IN_PROGRESS,
Jakub Kicinskied4420a2014-03-15 14:55:32 +00005752 &adapter->state)) {
Alexander Duyckb646c222013-02-07 08:55:46 +00005753 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
5754 tx_flags |= IGB_TX_FLAGS_TSTAMP;
5755
5756 adapter->ptp_tx_skb = skb_get(skb);
5757 adapter->ptp_tx_start = jiffies;
5758 if (adapter->hw.mac.type == e1000_82576)
5759 schedule_work(&adapter->ptp_tx_work);
Jacob Kellerc3b8f852017-05-03 10:28:59 -07005760 } else {
5761 adapter->tx_hwtstamp_skipped++;
Alexander Duyckb646c222013-02-07 08:55:46 +00005762 }
Patrick Ohly33af6bc2009-02-12 05:03:43 +00005763 }
Auke Kok9d5c8242008-01-24 02:22:38 -08005764
Jakub Kicinskiafc835d2014-03-15 14:55:26 +00005765 skb_tx_timestamp(skb);
5766
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01005767 if (skb_vlan_tag_present(skb)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08005768 tx_flags |= IGB_TX_FLAGS_VLAN;
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01005769 tx_flags |= (skb_vlan_tag_get(skb) << IGB_TX_FLAGS_VLAN_SHIFT);
Auke Kok9d5c8242008-01-24 02:22:38 -08005770 }
5771
Alexander Duyck7af40ad92011-08-26 07:45:15 +00005772 /* record initial flags and protocol */
5773 first->tx_flags = tx_flags;
5774 first->protocol = protocol;
Alexander Duyckcdfd01fc2009-10-27 23:50:57 +00005775
Alexander Duyck7af40ad92011-08-26 07:45:15 +00005776 tso = igb_tso(tx_ring, first, &hdr_len);
5777 if (tso < 0)
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00005778 goto out_drop;
Alexander Duyck7af40ad92011-08-26 07:45:15 +00005779 else if (!tso)
5780 igb_tx_csum(tx_ring, first);
Auke Kok9d5c8242008-01-24 02:22:38 -08005781
Jacob Keller74344e32017-05-03 10:28:55 -07005782 if (igb_tx_map(tx_ring, first, hdr_len))
5783 goto cleanup_tx_tstamp;
Alexander Duyck85ad76b2009-10-27 15:52:46 +00005784
Auke Kok9d5c8242008-01-24 02:22:38 -08005785 return NETDEV_TX_OK;
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00005786
5787out_drop:
Alexander Duyck7cc6fd42017-02-06 18:26:02 -08005788 dev_kfree_skb_any(first->skb);
5789 first->skb = NULL;
Jacob Keller74344e32017-05-03 10:28:55 -07005790cleanup_tx_tstamp:
5791 if (unlikely(tx_flags & IGB_TX_FLAGS_TSTAMP)) {
5792 struct igb_adapter *adapter = netdev_priv(tx_ring->netdev);
5793
5794 dev_kfree_skb_any(adapter->ptp_tx_skb);
5795 adapter->ptp_tx_skb = NULL;
5796 if (adapter->hw.mac.type == e1000_82576)
5797 cancel_work_sync(&adapter->ptp_tx_work);
5798 clear_bit_unlock(__IGB_PTP_TX_IN_PROGRESS, &adapter->state);
5799 }
Alexander Duyck7af40ad92011-08-26 07:45:15 +00005800
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00005801 return NETDEV_TX_OK;
Auke Kok9d5c8242008-01-24 02:22:38 -08005802}
5803
David S. Miller0b725a22014-08-25 15:51:53 -07005804static inline struct igb_ring *igb_tx_queue_mapping(struct igb_adapter *adapter,
5805 struct sk_buff *skb)
Alexander Duyck1cc3bd82011-08-26 07:44:10 +00005806{
David S. Miller0b725a22014-08-25 15:51:53 -07005807 unsigned int r_idx = skb->queue_mapping;
5808
Alexander Duyck1cc3bd82011-08-26 07:44:10 +00005809 if (r_idx >= adapter->num_tx_queues)
5810 r_idx = r_idx % adapter->num_tx_queues;
5811
5812 return adapter->tx_ring[r_idx];
5813}
5814
Alexander Duyckcd392f52011-08-26 07:43:59 +00005815static netdev_tx_t igb_xmit_frame(struct sk_buff *skb,
5816 struct net_device *netdev)
Auke Kok9d5c8242008-01-24 02:22:38 -08005817{
5818 struct igb_adapter *adapter = netdev_priv(netdev);
Alexander Duyckb1a436c2009-10-27 15:54:43 +00005819
Jeff Kirsherb980ac12013-02-23 07:29:56 +00005820 /* The minimum packet size with TCTL.PSP set is 17 so pad the skb
Alexander Duyck1cc3bd82011-08-26 07:44:10 +00005821 * in order to meet this minimum size requirement.
5822 */
Alexander Duycka94d9e22014-12-03 08:17:39 -08005823 if (skb_put_padto(skb, 17))
5824 return NETDEV_TX_OK;
Auke Kok9d5c8242008-01-24 02:22:38 -08005825
Alexander Duyck1cc3bd82011-08-26 07:44:10 +00005826 return igb_xmit_frame_ring(skb, igb_tx_queue_mapping(adapter, skb));
Auke Kok9d5c8242008-01-24 02:22:38 -08005827}
5828
5829/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00005830 * igb_tx_timeout - Respond to a Tx Hang
5831 * @netdev: network interface device structure
Auke Kok9d5c8242008-01-24 02:22:38 -08005832 **/
5833static void igb_tx_timeout(struct net_device *netdev)
5834{
5835 struct igb_adapter *adapter = netdev_priv(netdev);
5836 struct e1000_hw *hw = &adapter->hw;
5837
5838 /* Do the reset outside of interrupt context */
5839 adapter->tx_timeout_count++;
Alexander Duyckf7ba2052009-10-27 23:48:51 +00005840
Alexander Duyck06218a82011-08-26 07:46:55 +00005841 if (hw->mac.type >= e1000_82580)
Alexander Duyck55cac242009-11-19 12:42:21 +00005842 hw->dev_spec._82575.global_device_reset = true;
5843
Auke Kok9d5c8242008-01-24 02:22:38 -08005844 schedule_work(&adapter->reset_task);
Alexander Duyck265de402009-02-06 23:22:52 +00005845 wr32(E1000_EICS,
5846 (adapter->eims_enable_mask & ~adapter->eims_other));
Auke Kok9d5c8242008-01-24 02:22:38 -08005847}
5848
5849static void igb_reset_task(struct work_struct *work)
5850{
5851 struct igb_adapter *adapter;
5852 adapter = container_of(work, struct igb_adapter, reset_task);
5853
Taku Izumic97ec422010-04-27 14:39:30 +00005854 igb_dump(adapter);
5855 netdev_err(adapter->netdev, "Reset adapter\n");
Auke Kok9d5c8242008-01-24 02:22:38 -08005856 igb_reinit_locked(adapter);
5857}
5858
5859/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00005860 * igb_get_stats64 - Get System Network Statistics
5861 * @netdev: network interface device structure
5862 * @stats: rtnl_link_stats64 pointer
Auke Kok9d5c8242008-01-24 02:22:38 -08005863 **/
stephen hemmingerbc1f4472017-01-06 19:12:52 -08005864static void igb_get_stats64(struct net_device *netdev,
5865 struct rtnl_link_stats64 *stats)
Auke Kok9d5c8242008-01-24 02:22:38 -08005866{
Eric Dumazet12dcd862010-10-15 17:27:10 +00005867 struct igb_adapter *adapter = netdev_priv(netdev);
5868
5869 spin_lock(&adapter->stats64_lock);
Benjamin Poirier81e3f642017-05-16 15:55:16 -07005870 igb_update_stats(adapter);
Eric Dumazet12dcd862010-10-15 17:27:10 +00005871 memcpy(stats, &adapter->stats64, sizeof(*stats));
5872 spin_unlock(&adapter->stats64_lock);
Auke Kok9d5c8242008-01-24 02:22:38 -08005873}
5874
5875/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00005876 * igb_change_mtu - Change the Maximum Transfer Unit
5877 * @netdev: network interface device structure
5878 * @new_mtu: new value for maximum frame size
Auke Kok9d5c8242008-01-24 02:22:38 -08005879 *
Jeff Kirsherb980ac12013-02-23 07:29:56 +00005880 * Returns 0 on success, negative on failure
Auke Kok9d5c8242008-01-24 02:22:38 -08005881 **/
5882static int igb_change_mtu(struct net_device *netdev, int new_mtu)
5883{
5884 struct igb_adapter *adapter = netdev_priv(netdev);
Alexander Duyck090b1792009-10-27 23:51:55 +00005885 struct pci_dev *pdev = adapter->pdev;
Alexander Duyck153285f2011-08-26 07:43:32 +00005886 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
Auke Kok9d5c8242008-01-24 02:22:38 -08005887
Alexander Duyck2ccd9942013-07-16 00:20:34 +00005888 /* adjust max frame to be at least the size of a standard frame */
5889 if (max_frame < (ETH_FRAME_LEN + ETH_FCS_LEN))
5890 max_frame = ETH_FRAME_LEN + ETH_FCS_LEN;
5891
Auke Kok9d5c8242008-01-24 02:22:38 -08005892 while (test_and_set_bit(__IGB_RESETTING, &adapter->state))
Carolyn Wyborny0d451e72014-04-11 01:46:40 +00005893 usleep_range(1000, 2000);
Alexander Duyck73cd78f2009-02-12 18:16:59 +00005894
Auke Kok9d5c8242008-01-24 02:22:38 -08005895 /* igb_down has a dependency on max_frame_size */
5896 adapter->max_frame_size = max_frame;
Alexander Duyck559e9c42009-10-27 23:52:50 +00005897
Alexander Duyck4c844852009-10-27 15:52:07 +00005898 if (netif_running(netdev))
5899 igb_down(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08005900
Alexander Duyck090b1792009-10-27 23:51:55 +00005901 dev_info(&pdev->dev, "changing MTU from %d to %d\n",
Auke Kok9d5c8242008-01-24 02:22:38 -08005902 netdev->mtu, new_mtu);
5903 netdev->mtu = new_mtu;
5904
5905 if (netif_running(netdev))
5906 igb_up(adapter);
5907 else
5908 igb_reset(adapter);
5909
5910 clear_bit(__IGB_RESETTING, &adapter->state);
5911
5912 return 0;
5913}
5914
5915/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00005916 * igb_update_stats - Update the board statistics counters
5917 * @adapter: board private structure
Auke Kok9d5c8242008-01-24 02:22:38 -08005918 **/
Benjamin Poirier81e3f642017-05-16 15:55:16 -07005919void igb_update_stats(struct igb_adapter *adapter)
Auke Kok9d5c8242008-01-24 02:22:38 -08005920{
Benjamin Poirier81e3f642017-05-16 15:55:16 -07005921 struct rtnl_link_stats64 *net_stats = &adapter->stats64;
Auke Kok9d5c8242008-01-24 02:22:38 -08005922 struct e1000_hw *hw = &adapter->hw;
5923 struct pci_dev *pdev = adapter->pdev;
Mitch Williamsfa3d9a62010-03-23 18:34:38 +00005924 u32 reg, mpc;
Alexander Duyck3f9c0162009-10-27 23:48:12 +00005925 int i;
5926 u64 bytes, packets;
Eric Dumazet12dcd862010-10-15 17:27:10 +00005927 unsigned int start;
5928 u64 _bytes, _packets;
Auke Kok9d5c8242008-01-24 02:22:38 -08005929
Jeff Kirsherb980ac12013-02-23 07:29:56 +00005930 /* Prevent stats update while adapter is being reset, or if the pci
Auke Kok9d5c8242008-01-24 02:22:38 -08005931 * connection is down.
5932 */
5933 if (adapter->link_speed == 0)
5934 return;
5935 if (pci_channel_offline(pdev))
5936 return;
5937
Alexander Duyck3f9c0162009-10-27 23:48:12 +00005938 bytes = 0;
5939 packets = 0;
Akeem G Abodunrin7f901282013-06-27 09:10:23 +00005940
5941 rcu_read_lock();
Alexander Duyck3f9c0162009-10-27 23:48:12 +00005942 for (i = 0; i < adapter->num_rx_queues; i++) {
Alexander Duyck3025a442010-02-17 01:02:39 +00005943 struct igb_ring *ring = adapter->rx_ring[i];
Todd Fujinakae66c0832014-04-08 05:36:15 +00005944 u32 rqdpc = rd32(E1000_RQDPC(i));
5945 if (hw->mac.type >= e1000_i210)
5946 wr32(E1000_RQDPC(i), 0);
Eric Dumazet12dcd862010-10-15 17:27:10 +00005947
Alexander Duyckae1c07a2012-08-08 05:23:22 +00005948 if (rqdpc) {
5949 ring->rx_stats.drops += rqdpc;
5950 net_stats->rx_fifo_errors += rqdpc;
5951 }
Eric Dumazet12dcd862010-10-15 17:27:10 +00005952
5953 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07005954 start = u64_stats_fetch_begin_irq(&ring->rx_syncp);
Eric Dumazet12dcd862010-10-15 17:27:10 +00005955 _bytes = ring->rx_stats.bytes;
5956 _packets = ring->rx_stats.packets;
Eric W. Biederman57a77442014-03-13 21:26:42 -07005957 } while (u64_stats_fetch_retry_irq(&ring->rx_syncp, start));
Eric Dumazet12dcd862010-10-15 17:27:10 +00005958 bytes += _bytes;
5959 packets += _packets;
Alexander Duyck3f9c0162009-10-27 23:48:12 +00005960 }
5961
Alexander Duyck128e45e2009-11-12 18:37:38 +00005962 net_stats->rx_bytes = bytes;
5963 net_stats->rx_packets = packets;
Alexander Duyck3f9c0162009-10-27 23:48:12 +00005964
5965 bytes = 0;
5966 packets = 0;
5967 for (i = 0; i < adapter->num_tx_queues; i++) {
Alexander Duyck3025a442010-02-17 01:02:39 +00005968 struct igb_ring *ring = adapter->tx_ring[i];
Eric Dumazet12dcd862010-10-15 17:27:10 +00005969 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07005970 start = u64_stats_fetch_begin_irq(&ring->tx_syncp);
Eric Dumazet12dcd862010-10-15 17:27:10 +00005971 _bytes = ring->tx_stats.bytes;
5972 _packets = ring->tx_stats.packets;
Eric W. Biederman57a77442014-03-13 21:26:42 -07005973 } while (u64_stats_fetch_retry_irq(&ring->tx_syncp, start));
Eric Dumazet12dcd862010-10-15 17:27:10 +00005974 bytes += _bytes;
5975 packets += _packets;
Alexander Duyck3f9c0162009-10-27 23:48:12 +00005976 }
Alexander Duyck128e45e2009-11-12 18:37:38 +00005977 net_stats->tx_bytes = bytes;
5978 net_stats->tx_packets = packets;
Akeem G Abodunrin7f901282013-06-27 09:10:23 +00005979 rcu_read_unlock();
Alexander Duyck3f9c0162009-10-27 23:48:12 +00005980
5981 /* read stats registers */
Auke Kok9d5c8242008-01-24 02:22:38 -08005982 adapter->stats.crcerrs += rd32(E1000_CRCERRS);
5983 adapter->stats.gprc += rd32(E1000_GPRC);
5984 adapter->stats.gorc += rd32(E1000_GORCL);
5985 rd32(E1000_GORCH); /* clear GORCL */
5986 adapter->stats.bprc += rd32(E1000_BPRC);
5987 adapter->stats.mprc += rd32(E1000_MPRC);
5988 adapter->stats.roc += rd32(E1000_ROC);
5989
5990 adapter->stats.prc64 += rd32(E1000_PRC64);
5991 adapter->stats.prc127 += rd32(E1000_PRC127);
5992 adapter->stats.prc255 += rd32(E1000_PRC255);
5993 adapter->stats.prc511 += rd32(E1000_PRC511);
5994 adapter->stats.prc1023 += rd32(E1000_PRC1023);
5995 adapter->stats.prc1522 += rd32(E1000_PRC1522);
5996 adapter->stats.symerrs += rd32(E1000_SYMERRS);
5997 adapter->stats.sec += rd32(E1000_SEC);
5998
Mitch Williamsfa3d9a62010-03-23 18:34:38 +00005999 mpc = rd32(E1000_MPC);
6000 adapter->stats.mpc += mpc;
6001 net_stats->rx_fifo_errors += mpc;
Auke Kok9d5c8242008-01-24 02:22:38 -08006002 adapter->stats.scc += rd32(E1000_SCC);
6003 adapter->stats.ecol += rd32(E1000_ECOL);
6004 adapter->stats.mcc += rd32(E1000_MCC);
6005 adapter->stats.latecol += rd32(E1000_LATECOL);
6006 adapter->stats.dc += rd32(E1000_DC);
6007 adapter->stats.rlec += rd32(E1000_RLEC);
6008 adapter->stats.xonrxc += rd32(E1000_XONRXC);
6009 adapter->stats.xontxc += rd32(E1000_XONTXC);
6010 adapter->stats.xoffrxc += rd32(E1000_XOFFRXC);
6011 adapter->stats.xofftxc += rd32(E1000_XOFFTXC);
6012 adapter->stats.fcruc += rd32(E1000_FCRUC);
6013 adapter->stats.gptc += rd32(E1000_GPTC);
6014 adapter->stats.gotc += rd32(E1000_GOTCL);
6015 rd32(E1000_GOTCH); /* clear GOTCL */
Mitch Williamsfa3d9a62010-03-23 18:34:38 +00006016 adapter->stats.rnbc += rd32(E1000_RNBC);
Auke Kok9d5c8242008-01-24 02:22:38 -08006017 adapter->stats.ruc += rd32(E1000_RUC);
6018 adapter->stats.rfc += rd32(E1000_RFC);
6019 adapter->stats.rjc += rd32(E1000_RJC);
6020 adapter->stats.tor += rd32(E1000_TORH);
6021 adapter->stats.tot += rd32(E1000_TOTH);
6022 adapter->stats.tpr += rd32(E1000_TPR);
6023
6024 adapter->stats.ptc64 += rd32(E1000_PTC64);
6025 adapter->stats.ptc127 += rd32(E1000_PTC127);
6026 adapter->stats.ptc255 += rd32(E1000_PTC255);
6027 adapter->stats.ptc511 += rd32(E1000_PTC511);
6028 adapter->stats.ptc1023 += rd32(E1000_PTC1023);
6029 adapter->stats.ptc1522 += rd32(E1000_PTC1522);
6030
6031 adapter->stats.mptc += rd32(E1000_MPTC);
6032 adapter->stats.bptc += rd32(E1000_BPTC);
6033
Nick Nunley2d0b0f62010-02-17 01:02:59 +00006034 adapter->stats.tpt += rd32(E1000_TPT);
6035 adapter->stats.colc += rd32(E1000_COLC);
Auke Kok9d5c8242008-01-24 02:22:38 -08006036
6037 adapter->stats.algnerrc += rd32(E1000_ALGNERRC);
Nick Nunley43915c7c2010-02-17 01:03:58 +00006038 /* read internal phy specific stats */
6039 reg = rd32(E1000_CTRL_EXT);
6040 if (!(reg & E1000_CTRL_EXT_LINK_MODE_MASK)) {
6041 adapter->stats.rxerrc += rd32(E1000_RXERRC);
Carolyn Wyborny3dbdf962012-09-12 04:36:24 +00006042
6043 /* this stat has invalid values on i210/i211 */
6044 if ((hw->mac.type != e1000_i210) &&
6045 (hw->mac.type != e1000_i211))
6046 adapter->stats.tncrs += rd32(E1000_TNCRS);
Nick Nunley43915c7c2010-02-17 01:03:58 +00006047 }
6048
Auke Kok9d5c8242008-01-24 02:22:38 -08006049 adapter->stats.tsctc += rd32(E1000_TSCTC);
6050 adapter->stats.tsctfc += rd32(E1000_TSCTFC);
6051
6052 adapter->stats.iac += rd32(E1000_IAC);
6053 adapter->stats.icrxoc += rd32(E1000_ICRXOC);
6054 adapter->stats.icrxptc += rd32(E1000_ICRXPTC);
6055 adapter->stats.icrxatc += rd32(E1000_ICRXATC);
6056 adapter->stats.ictxptc += rd32(E1000_ICTXPTC);
6057 adapter->stats.ictxatc += rd32(E1000_ICTXATC);
6058 adapter->stats.ictxqec += rd32(E1000_ICTXQEC);
6059 adapter->stats.ictxqmtc += rd32(E1000_ICTXQMTC);
6060 adapter->stats.icrxdmtc += rd32(E1000_ICRXDMTC);
6061
6062 /* Fill out the OS statistics structure */
Alexander Duyck128e45e2009-11-12 18:37:38 +00006063 net_stats->multicast = adapter->stats.mprc;
6064 net_stats->collisions = adapter->stats.colc;
Auke Kok9d5c8242008-01-24 02:22:38 -08006065
6066 /* Rx Errors */
6067
6068 /* RLEC on some newer hardware can be incorrect so build
Jeff Kirsherb980ac12013-02-23 07:29:56 +00006069 * our own version based on RUC and ROC
6070 */
Alexander Duyck128e45e2009-11-12 18:37:38 +00006071 net_stats->rx_errors = adapter->stats.rxerrc +
Auke Kok9d5c8242008-01-24 02:22:38 -08006072 adapter->stats.crcerrs + adapter->stats.algnerrc +
6073 adapter->stats.ruc + adapter->stats.roc +
6074 adapter->stats.cexterr;
Alexander Duyck128e45e2009-11-12 18:37:38 +00006075 net_stats->rx_length_errors = adapter->stats.ruc +
6076 adapter->stats.roc;
6077 net_stats->rx_crc_errors = adapter->stats.crcerrs;
6078 net_stats->rx_frame_errors = adapter->stats.algnerrc;
6079 net_stats->rx_missed_errors = adapter->stats.mpc;
Auke Kok9d5c8242008-01-24 02:22:38 -08006080
6081 /* Tx Errors */
Alexander Duyck128e45e2009-11-12 18:37:38 +00006082 net_stats->tx_errors = adapter->stats.ecol +
6083 adapter->stats.latecol;
6084 net_stats->tx_aborted_errors = adapter->stats.ecol;
6085 net_stats->tx_window_errors = adapter->stats.latecol;
6086 net_stats->tx_carrier_errors = adapter->stats.tncrs;
Auke Kok9d5c8242008-01-24 02:22:38 -08006087
6088 /* Tx Dropped needs to be maintained elsewhere */
6089
Auke Kok9d5c8242008-01-24 02:22:38 -08006090 /* Management Stats */
6091 adapter->stats.mgptc += rd32(E1000_MGTPTC);
6092 adapter->stats.mgprc += rd32(E1000_MGTPRC);
6093 adapter->stats.mgpdc += rd32(E1000_MGTPDC);
Carolyn Wyborny0a915b92011-02-26 07:42:37 +00006094
6095 /* OS2BMC Stats */
6096 reg = rd32(E1000_MANC);
6097 if (reg & E1000_MANC_EN_BMC2OS) {
6098 adapter->stats.o2bgptc += rd32(E1000_O2BGPTC);
6099 adapter->stats.o2bspc += rd32(E1000_O2BSPC);
6100 adapter->stats.b2ospc += rd32(E1000_B2OSPC);
6101 adapter->stats.b2ogprc += rd32(E1000_B2OGPRC);
6102 }
Auke Kok9d5c8242008-01-24 02:22:38 -08006103}
6104
Richard Cochran61d7f752014-11-21 20:51:10 +00006105static void igb_tsync_interrupt(struct igb_adapter *adapter)
6106{
6107 struct e1000_hw *hw = &adapter->hw;
Richard Cochran00c65572014-11-21 20:51:20 +00006108 struct ptp_clock_event event;
Arnd Bergmann40c9b072015-09-30 13:26:33 +02006109 struct timespec64 ts;
Richard Cochran720db4f2014-11-21 20:51:26 +00006110 u32 ack = 0, tsauxc, sec, nsec, tsicr = rd32(E1000_TSICR);
Richard Cochran00c65572014-11-21 20:51:20 +00006111
6112 if (tsicr & TSINTR_SYS_WRAP) {
6113 event.type = PTP_CLOCK_PPS;
6114 if (adapter->ptp_caps.pps)
6115 ptp_clock_event(adapter->ptp_clock, &event);
Richard Cochran00c65572014-11-21 20:51:20 +00006116 ack |= TSINTR_SYS_WRAP;
6117 }
Richard Cochran61d7f752014-11-21 20:51:10 +00006118
6119 if (tsicr & E1000_TSICR_TXTS) {
Richard Cochran61d7f752014-11-21 20:51:10 +00006120 /* retrieve hardware timestamp */
6121 schedule_work(&adapter->ptp_tx_work);
Richard Cochran00c65572014-11-21 20:51:20 +00006122 ack |= E1000_TSICR_TXTS;
Richard Cochran61d7f752014-11-21 20:51:10 +00006123 }
Richard Cochran00c65572014-11-21 20:51:20 +00006124
Richard Cochran720db4f2014-11-21 20:51:26 +00006125 if (tsicr & TSINTR_TT0) {
6126 spin_lock(&adapter->tmreg_lock);
Arnd Bergmann40c9b072015-09-30 13:26:33 +02006127 ts = timespec64_add(adapter->perout[0].start,
6128 adapter->perout[0].period);
6129 /* u32 conversion of tv_sec is safe until y2106 */
Richard Cochran720db4f2014-11-21 20:51:26 +00006130 wr32(E1000_TRGTTIML0, ts.tv_nsec);
Arnd Bergmann40c9b072015-09-30 13:26:33 +02006131 wr32(E1000_TRGTTIMH0, (u32)ts.tv_sec);
Richard Cochran720db4f2014-11-21 20:51:26 +00006132 tsauxc = rd32(E1000_TSAUXC);
6133 tsauxc |= TSAUXC_EN_TT0;
6134 wr32(E1000_TSAUXC, tsauxc);
6135 adapter->perout[0].start = ts;
6136 spin_unlock(&adapter->tmreg_lock);
6137 ack |= TSINTR_TT0;
6138 }
6139
6140 if (tsicr & TSINTR_TT1) {
6141 spin_lock(&adapter->tmreg_lock);
Arnd Bergmann40c9b072015-09-30 13:26:33 +02006142 ts = timespec64_add(adapter->perout[1].start,
6143 adapter->perout[1].period);
Richard Cochran720db4f2014-11-21 20:51:26 +00006144 wr32(E1000_TRGTTIML1, ts.tv_nsec);
Arnd Bergmann40c9b072015-09-30 13:26:33 +02006145 wr32(E1000_TRGTTIMH1, (u32)ts.tv_sec);
Richard Cochran720db4f2014-11-21 20:51:26 +00006146 tsauxc = rd32(E1000_TSAUXC);
6147 tsauxc |= TSAUXC_EN_TT1;
6148 wr32(E1000_TSAUXC, tsauxc);
6149 adapter->perout[1].start = ts;
6150 spin_unlock(&adapter->tmreg_lock);
6151 ack |= TSINTR_TT1;
6152 }
6153
6154 if (tsicr & TSINTR_AUTT0) {
6155 nsec = rd32(E1000_AUXSTMPL0);
6156 sec = rd32(E1000_AUXSTMPH0);
6157 event.type = PTP_CLOCK_EXTTS;
6158 event.index = 0;
6159 event.timestamp = sec * 1000000000ULL + nsec;
6160 ptp_clock_event(adapter->ptp_clock, &event);
6161 ack |= TSINTR_AUTT0;
6162 }
6163
6164 if (tsicr & TSINTR_AUTT1) {
6165 nsec = rd32(E1000_AUXSTMPL1);
6166 sec = rd32(E1000_AUXSTMPH1);
6167 event.type = PTP_CLOCK_EXTTS;
6168 event.index = 1;
6169 event.timestamp = sec * 1000000000ULL + nsec;
6170 ptp_clock_event(adapter->ptp_clock, &event);
6171 ack |= TSINTR_AUTT1;
6172 }
6173
Richard Cochran00c65572014-11-21 20:51:20 +00006174 /* acknowledge the interrupts */
6175 wr32(E1000_TSICR, ack);
Richard Cochran61d7f752014-11-21 20:51:10 +00006176}
6177
Auke Kok9d5c8242008-01-24 02:22:38 -08006178static irqreturn_t igb_msix_other(int irq, void *data)
6179{
Alexander Duyck047e0032009-10-27 15:49:27 +00006180 struct igb_adapter *adapter = data;
Auke Kok9d5c8242008-01-24 02:22:38 -08006181 struct e1000_hw *hw = &adapter->hw;
PJ Waskiewicz844290e2008-06-27 11:00:39 -07006182 u32 icr = rd32(E1000_ICR);
PJ Waskiewicz844290e2008-06-27 11:00:39 -07006183 /* reading ICR causes bit 31 of EICR to be cleared */
Alexander Duyckdda0e082009-02-06 23:19:08 +00006184
Alexander Duyck7f081d42010-01-07 17:41:00 +00006185 if (icr & E1000_ICR_DRSTA)
6186 schedule_work(&adapter->reset_task);
6187
Alexander Duyck047e0032009-10-27 15:49:27 +00006188 if (icr & E1000_ICR_DOUTSYNC) {
Alexander Duyckdda0e082009-02-06 23:19:08 +00006189 /* HW is reporting DMA is out of sync */
6190 adapter->stats.doosync++;
Greg Rose13800462010-11-06 02:08:26 +00006191 /* The DMA Out of Sync is also indication of a spoof event
6192 * in IOV mode. Check the Wrong VM Behavior register to
Jeff Kirsherb980ac12013-02-23 07:29:56 +00006193 * see if it is really a spoof event.
6194 */
Greg Rose13800462010-11-06 02:08:26 +00006195 igb_check_wvbr(adapter);
Alexander Duyckdda0e082009-02-06 23:19:08 +00006196 }
Alexander Duyckeebbbdb2009-02-06 23:19:29 +00006197
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006198 /* Check for a mailbox event */
6199 if (icr & E1000_ICR_VMMB)
6200 igb_msg_task(adapter);
6201
6202 if (icr & E1000_ICR_LSC) {
6203 hw->mac.get_link_status = 1;
6204 /* guard against interrupt when we're going down */
6205 if (!test_bit(__IGB_DOWN, &adapter->state))
6206 mod_timer(&adapter->watchdog_timer, jiffies + 1);
6207 }
6208
Richard Cochran61d7f752014-11-21 20:51:10 +00006209 if (icr & E1000_ICR_TS)
6210 igb_tsync_interrupt(adapter);
Matthew Vick1f6e8172012-08-18 07:26:33 +00006211
PJ Waskiewicz844290e2008-06-27 11:00:39 -07006212 wr32(E1000_EIMS, adapter->eims_other);
Auke Kok9d5c8242008-01-24 02:22:38 -08006213
6214 return IRQ_HANDLED;
6215}
6216
Alexander Duyck047e0032009-10-27 15:49:27 +00006217static void igb_write_itr(struct igb_q_vector *q_vector)
Auke Kok9d5c8242008-01-24 02:22:38 -08006218{
Alexander Duyck26b39272010-02-17 01:00:41 +00006219 struct igb_adapter *adapter = q_vector->adapter;
Alexander Duyck047e0032009-10-27 15:49:27 +00006220 u32 itr_val = q_vector->itr_val & 0x7FFC;
Auke Kok9d5c8242008-01-24 02:22:38 -08006221
Alexander Duyck047e0032009-10-27 15:49:27 +00006222 if (!q_vector->set_itr)
6223 return;
Alexander Duyck73cd78f2009-02-12 18:16:59 +00006224
Alexander Duyck047e0032009-10-27 15:49:27 +00006225 if (!itr_val)
6226 itr_val = 0x4;
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07006227
Alexander Duyck26b39272010-02-17 01:00:41 +00006228 if (adapter->hw.mac.type == e1000_82575)
6229 itr_val |= itr_val << 16;
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07006230 else
Alexander Duyck0ba82992011-08-26 07:45:47 +00006231 itr_val |= E1000_EITR_CNT_IGNR;
Alexander Duyck047e0032009-10-27 15:49:27 +00006232
6233 writel(itr_val, q_vector->itr_register);
6234 q_vector->set_itr = 0;
6235}
6236
6237static irqreturn_t igb_msix_ring(int irq, void *data)
6238{
6239 struct igb_q_vector *q_vector = data;
6240
6241 /* Write the ITR value calculated from the previous interrupt. */
6242 igb_write_itr(q_vector);
6243
6244 napi_schedule(&q_vector->napi);
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07006245
Auke Kok9d5c8242008-01-24 02:22:38 -08006246 return IRQ_HANDLED;
6247}
6248
Jeff Kirsher421e02f2008-10-17 11:08:31 -07006249#ifdef CONFIG_IGB_DCA
Alexander Duyck6a050042012-09-25 00:31:27 +00006250static void igb_update_tx_dca(struct igb_adapter *adapter,
6251 struct igb_ring *tx_ring,
6252 int cpu)
6253{
6254 struct e1000_hw *hw = &adapter->hw;
6255 u32 txctrl = dca3_get_tag(tx_ring->dev, cpu);
6256
6257 if (hw->mac.type != e1000_82575)
6258 txctrl <<= E1000_DCA_TXCTRL_CPUID_SHIFT;
6259
Jeff Kirsherb980ac12013-02-23 07:29:56 +00006260 /* We can enable relaxed ordering for reads, but not writes when
Alexander Duyck6a050042012-09-25 00:31:27 +00006261 * DCA is enabled. This is due to a known issue in some chipsets
6262 * which will cause the DCA tag to be cleared.
6263 */
6264 txctrl |= E1000_DCA_TXCTRL_DESC_RRO_EN |
6265 E1000_DCA_TXCTRL_DATA_RRO_EN |
6266 E1000_DCA_TXCTRL_DESC_DCA_EN;
6267
6268 wr32(E1000_DCA_TXCTRL(tx_ring->reg_idx), txctrl);
6269}
6270
6271static void igb_update_rx_dca(struct igb_adapter *adapter,
6272 struct igb_ring *rx_ring,
6273 int cpu)
6274{
6275 struct e1000_hw *hw = &adapter->hw;
6276 u32 rxctrl = dca3_get_tag(&adapter->pdev->dev, cpu);
6277
6278 if (hw->mac.type != e1000_82575)
6279 rxctrl <<= E1000_DCA_RXCTRL_CPUID_SHIFT;
6280
Jeff Kirsherb980ac12013-02-23 07:29:56 +00006281 /* We can enable relaxed ordering for reads, but not writes when
Alexander Duyck6a050042012-09-25 00:31:27 +00006282 * DCA is enabled. This is due to a known issue in some chipsets
6283 * which will cause the DCA tag to be cleared.
6284 */
6285 rxctrl |= E1000_DCA_RXCTRL_DESC_RRO_EN |
6286 E1000_DCA_RXCTRL_DESC_DCA_EN;
6287
6288 wr32(E1000_DCA_RXCTRL(rx_ring->reg_idx), rxctrl);
6289}
6290
Alexander Duyck047e0032009-10-27 15:49:27 +00006291static void igb_update_dca(struct igb_q_vector *q_vector)
Jeb Cramerfe4506b2008-07-08 15:07:55 -07006292{
Alexander Duyck047e0032009-10-27 15:49:27 +00006293 struct igb_adapter *adapter = q_vector->adapter;
Jeb Cramerfe4506b2008-07-08 15:07:55 -07006294 int cpu = get_cpu();
Jeb Cramerfe4506b2008-07-08 15:07:55 -07006295
Alexander Duyck047e0032009-10-27 15:49:27 +00006296 if (q_vector->cpu == cpu)
6297 goto out_no_update;
6298
Alexander Duyck6a050042012-09-25 00:31:27 +00006299 if (q_vector->tx.ring)
6300 igb_update_tx_dca(adapter, q_vector->tx.ring, cpu);
6301
6302 if (q_vector->rx.ring)
6303 igb_update_rx_dca(adapter, q_vector->rx.ring, cpu);
6304
Alexander Duyck047e0032009-10-27 15:49:27 +00006305 q_vector->cpu = cpu;
6306out_no_update:
Jeb Cramerfe4506b2008-07-08 15:07:55 -07006307 put_cpu();
6308}
6309
6310static void igb_setup_dca(struct igb_adapter *adapter)
6311{
Alexander Duyck7e0e99e2009-05-21 13:06:56 +00006312 struct e1000_hw *hw = &adapter->hw;
Jeb Cramerfe4506b2008-07-08 15:07:55 -07006313 int i;
6314
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07006315 if (!(adapter->flags & IGB_FLAG_DCA_ENABLED))
Jeb Cramerfe4506b2008-07-08 15:07:55 -07006316 return;
6317
Alexander Duyck7e0e99e2009-05-21 13:06:56 +00006318 /* Always use CB2 mode, difference is masked in the CB driver. */
6319 wr32(E1000_DCA_CTRL, E1000_DCA_CTRL_DCA_MODE_CB2);
6320
Alexander Duyck047e0032009-10-27 15:49:27 +00006321 for (i = 0; i < adapter->num_q_vectors; i++) {
Alexander Duyck26b39272010-02-17 01:00:41 +00006322 adapter->q_vector[i]->cpu = -1;
6323 igb_update_dca(adapter->q_vector[i]);
Jeb Cramerfe4506b2008-07-08 15:07:55 -07006324 }
6325}
6326
6327static int __igb_notify_dca(struct device *dev, void *data)
6328{
6329 struct net_device *netdev = dev_get_drvdata(dev);
6330 struct igb_adapter *adapter = netdev_priv(netdev);
Alexander Duyck090b1792009-10-27 23:51:55 +00006331 struct pci_dev *pdev = adapter->pdev;
Jeb Cramerfe4506b2008-07-08 15:07:55 -07006332 struct e1000_hw *hw = &adapter->hw;
6333 unsigned long event = *(unsigned long *)data;
6334
6335 switch (event) {
6336 case DCA_PROVIDER_ADD:
6337 /* if already enabled, don't do it again */
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07006338 if (adapter->flags & IGB_FLAG_DCA_ENABLED)
Jeb Cramerfe4506b2008-07-08 15:07:55 -07006339 break;
Jeb Cramerfe4506b2008-07-08 15:07:55 -07006340 if (dca_add_requester(dev) == 0) {
Alexander Duyckbbd98fe2009-01-31 00:52:30 -08006341 adapter->flags |= IGB_FLAG_DCA_ENABLED;
Alexander Duyck090b1792009-10-27 23:51:55 +00006342 dev_info(&pdev->dev, "DCA enabled\n");
Jeb Cramerfe4506b2008-07-08 15:07:55 -07006343 igb_setup_dca(adapter);
6344 break;
6345 }
6346 /* Fall Through since DCA is disabled. */
6347 case DCA_PROVIDER_REMOVE:
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07006348 if (adapter->flags & IGB_FLAG_DCA_ENABLED) {
Jeb Cramerfe4506b2008-07-08 15:07:55 -07006349 /* without this a class_device is left
Jeff Kirsherb980ac12013-02-23 07:29:56 +00006350 * hanging around in the sysfs model
6351 */
Jeb Cramerfe4506b2008-07-08 15:07:55 -07006352 dca_remove_requester(dev);
Alexander Duyck090b1792009-10-27 23:51:55 +00006353 dev_info(&pdev->dev, "DCA disabled\n");
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07006354 adapter->flags &= ~IGB_FLAG_DCA_ENABLED;
Alexander Duyckcbd347a2009-02-15 23:59:44 -08006355 wr32(E1000_DCA_CTRL, E1000_DCA_CTRL_DCA_MODE_DISABLE);
Jeb Cramerfe4506b2008-07-08 15:07:55 -07006356 }
6357 break;
6358 }
Alexander Duyckbbd98fe2009-01-31 00:52:30 -08006359
Jeb Cramerfe4506b2008-07-08 15:07:55 -07006360 return 0;
6361}
6362
6363static int igb_notify_dca(struct notifier_block *nb, unsigned long event,
Jeff Kirsherb980ac12013-02-23 07:29:56 +00006364 void *p)
Jeb Cramerfe4506b2008-07-08 15:07:55 -07006365{
6366 int ret_val;
6367
6368 ret_val = driver_for_each_device(&igb_driver.driver, NULL, &event,
Jeff Kirsherb980ac12013-02-23 07:29:56 +00006369 __igb_notify_dca);
Jeb Cramerfe4506b2008-07-08 15:07:55 -07006370
6371 return ret_val ? NOTIFY_BAD : NOTIFY_DONE;
6372}
Jeff Kirsher421e02f2008-10-17 11:08:31 -07006373#endif /* CONFIG_IGB_DCA */
Auke Kok9d5c8242008-01-24 02:22:38 -08006374
Greg Rose0224d662011-10-14 02:57:14 +00006375#ifdef CONFIG_PCI_IOV
6376static int igb_vf_configure(struct igb_adapter *adapter, int vf)
6377{
6378 unsigned char mac_addr[ETH_ALEN];
Greg Rose0224d662011-10-14 02:57:14 +00006379
Mitch A Williams5ac6f912013-01-18 08:57:20 +00006380 eth_zero_addr(mac_addr);
Greg Rose0224d662011-10-14 02:57:14 +00006381 igb_set_vf_mac(adapter, vf, mac_addr);
6382
Lior Levy70ea4782013-03-03 20:27:48 +00006383 /* By default spoof check is enabled for all VFs */
6384 adapter->vf_data[vf].spoofchk_enabled = true;
6385
Stefan Assmannf5571472012-08-18 04:06:11 +00006386 return 0;
Greg Rose0224d662011-10-14 02:57:14 +00006387}
6388
Greg Rose0224d662011-10-14 02:57:14 +00006389#endif
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006390static void igb_ping_all_vfs(struct igb_adapter *adapter)
6391{
6392 struct e1000_hw *hw = &adapter->hw;
6393 u32 ping;
6394 int i;
6395
6396 for (i = 0 ; i < adapter->vfs_allocated_count; i++) {
6397 ping = E1000_PF_CONTROL_MSG;
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00006398 if (adapter->vf_data[i].flags & IGB_VF_FLAG_CTS)
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006399 ping |= E1000_VT_MSGTYPE_CTS;
6400 igb_write_mbx(hw, &ping, 1, i);
6401 }
6402}
6403
Alexander Duyck7d5753f2009-10-27 23:47:16 +00006404static int igb_set_vf_promisc(struct igb_adapter *adapter, u32 *msgbuf, u32 vf)
6405{
6406 struct e1000_hw *hw = &adapter->hw;
6407 u32 vmolr = rd32(E1000_VMOLR(vf));
6408 struct vf_data_storage *vf_data = &adapter->vf_data[vf];
6409
Alexander Duyckd85b90042010-09-22 17:56:20 +00006410 vf_data->flags &= ~(IGB_VF_FLAG_UNI_PROMISC |
Jeff Kirsherb980ac12013-02-23 07:29:56 +00006411 IGB_VF_FLAG_MULTI_PROMISC);
Alexander Duyck7d5753f2009-10-27 23:47:16 +00006412 vmolr &= ~(E1000_VMOLR_ROPE | E1000_VMOLR_ROMPE | E1000_VMOLR_MPME);
6413
6414 if (*msgbuf & E1000_VF_SET_PROMISC_MULTICAST) {
6415 vmolr |= E1000_VMOLR_MPME;
Alexander Duyckd85b90042010-09-22 17:56:20 +00006416 vf_data->flags |= IGB_VF_FLAG_MULTI_PROMISC;
Alexander Duyck7d5753f2009-10-27 23:47:16 +00006417 *msgbuf &= ~E1000_VF_SET_PROMISC_MULTICAST;
6418 } else {
Jeff Kirsherb980ac12013-02-23 07:29:56 +00006419 /* if we have hashes and we are clearing a multicast promisc
Alexander Duyck7d5753f2009-10-27 23:47:16 +00006420 * flag we need to write the hashes to the MTA as this step
6421 * was previously skipped
6422 */
6423 if (vf_data->num_vf_mc_hashes > 30) {
6424 vmolr |= E1000_VMOLR_MPME;
6425 } else if (vf_data->num_vf_mc_hashes) {
6426 int j;
Carolyn Wyborny9005df32014-04-11 01:45:34 +00006427
Alexander Duyck7d5753f2009-10-27 23:47:16 +00006428 vmolr |= E1000_VMOLR_ROMPE;
6429 for (j = 0; j < vf_data->num_vf_mc_hashes; j++)
6430 igb_mta_set(hw, vf_data->vf_mc_hashes[j]);
6431 }
6432 }
6433
6434 wr32(E1000_VMOLR(vf), vmolr);
6435
6436 /* there are flags left unprocessed, likely not supported */
6437 if (*msgbuf & E1000_VT_MSGINFO_MASK)
6438 return -EINVAL;
6439
6440 return 0;
Alexander Duyck7d5753f2009-10-27 23:47:16 +00006441}
6442
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006443static int igb_set_vf_multicasts(struct igb_adapter *adapter,
6444 u32 *msgbuf, u32 vf)
6445{
6446 int n = (msgbuf[0] & E1000_VT_MSGINFO_MASK) >> E1000_VT_MSGINFO_SHIFT;
6447 u16 *hash_list = (u16 *)&msgbuf[1];
6448 struct vf_data_storage *vf_data = &adapter->vf_data[vf];
6449 int i;
6450
Alexander Duyck7d5753f2009-10-27 23:47:16 +00006451 /* salt away the number of multicast addresses assigned
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006452 * to this VF for later use to restore when the PF multi cast
6453 * list changes
6454 */
6455 vf_data->num_vf_mc_hashes = n;
6456
Alexander Duyck7d5753f2009-10-27 23:47:16 +00006457 /* only up to 30 hash values supported */
6458 if (n > 30)
6459 n = 30;
6460
6461 /* store the hashes for later use */
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006462 for (i = 0; i < n; i++)
Joe Perchesa419aef2009-08-18 11:18:35 -07006463 vf_data->vf_mc_hashes[i] = hash_list[i];
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006464
6465 /* Flush and reset the mta with the new values */
Alexander Duyckff41f8d2009-09-03 14:48:56 +00006466 igb_set_rx_mode(adapter->netdev);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006467
6468 return 0;
6469}
6470
6471static void igb_restore_vf_multicasts(struct igb_adapter *adapter)
6472{
6473 struct e1000_hw *hw = &adapter->hw;
6474 struct vf_data_storage *vf_data;
6475 int i, j;
6476
6477 for (i = 0; i < adapter->vfs_allocated_count; i++) {
Alexander Duyck7d5753f2009-10-27 23:47:16 +00006478 u32 vmolr = rd32(E1000_VMOLR(i));
Carolyn Wyborny9005df32014-04-11 01:45:34 +00006479
Alexander Duyck7d5753f2009-10-27 23:47:16 +00006480 vmolr &= ~(E1000_VMOLR_ROMPE | E1000_VMOLR_MPME);
6481
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006482 vf_data = &adapter->vf_data[i];
Alexander Duyck7d5753f2009-10-27 23:47:16 +00006483
6484 if ((vf_data->num_vf_mc_hashes > 30) ||
6485 (vf_data->flags & IGB_VF_FLAG_MULTI_PROMISC)) {
6486 vmolr |= E1000_VMOLR_MPME;
6487 } else if (vf_data->num_vf_mc_hashes) {
6488 vmolr |= E1000_VMOLR_ROMPE;
6489 for (j = 0; j < vf_data->num_vf_mc_hashes; j++)
6490 igb_mta_set(hw, vf_data->vf_mc_hashes[j]);
6491 }
6492 wr32(E1000_VMOLR(i), vmolr);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006493 }
6494}
6495
6496static void igb_clear_vf_vfta(struct igb_adapter *adapter, u32 vf)
6497{
6498 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck16903ca2016-01-06 23:11:18 -08006499 u32 pool_mask, vlvf_mask, i;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006500
Alexander Duyck16903ca2016-01-06 23:11:18 -08006501 /* create mask for VF and other pools */
6502 pool_mask = E1000_VLVF_POOLSEL_MASK;
Jacob Kellera51d8c22016-04-13 16:08:28 -07006503 vlvf_mask = BIT(E1000_VLVF_POOLSEL_SHIFT + vf);
Alexander Duyck16903ca2016-01-06 23:11:18 -08006504
6505 /* drop PF from pool bits */
Jacob Kellera51d8c22016-04-13 16:08:28 -07006506 pool_mask &= ~BIT(E1000_VLVF_POOLSEL_SHIFT +
6507 adapter->vfs_allocated_count);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006508
6509 /* Find the vlan filter for this id */
Alexander Duyck16903ca2016-01-06 23:11:18 -08006510 for (i = E1000_VLVF_ARRAY_SIZE; i--;) {
6511 u32 vlvf = rd32(E1000_VLVF(i));
6512 u32 vfta_mask, vid, vfta;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006513
6514 /* remove the vf from the pool */
Alexander Duyck16903ca2016-01-06 23:11:18 -08006515 if (!(vlvf & vlvf_mask))
6516 continue;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006517
Alexander Duyck16903ca2016-01-06 23:11:18 -08006518 /* clear out bit from VLVF */
6519 vlvf ^= vlvf_mask;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006520
Alexander Duyck16903ca2016-01-06 23:11:18 -08006521 /* if other pools are present, just remove ourselves */
6522 if (vlvf & pool_mask)
6523 goto update_vlvfb;
6524
6525 /* if PF is present, leave VFTA */
6526 if (vlvf & E1000_VLVF_POOLSEL_MASK)
6527 goto update_vlvf;
6528
6529 vid = vlvf & E1000_VLVF_VLANID_MASK;
Jacob Kellera51d8c22016-04-13 16:08:28 -07006530 vfta_mask = BIT(vid % 32);
Alexander Duyck16903ca2016-01-06 23:11:18 -08006531
6532 /* clear bit from VFTA */
6533 vfta = adapter->shadow_vfta[vid / 32];
6534 if (vfta & vfta_mask)
6535 hw->mac.ops.write_vfta(hw, vid / 32, vfta ^ vfta_mask);
6536update_vlvf:
6537 /* clear pool selection enable */
6538 if (adapter->flags & IGB_FLAG_VLAN_PROMISC)
6539 vlvf &= E1000_VLVF_POOLSEL_MASK;
6540 else
6541 vlvf = 0;
6542update_vlvfb:
6543 /* clear pool bits */
6544 wr32(E1000_VLVF(i), vlvf);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006545 }
6546}
6547
Alexander Duyck16903ca2016-01-06 23:11:18 -08006548static int igb_find_vlvf_entry(struct e1000_hw *hw, u32 vlan)
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006549{
Alexander Duyck16903ca2016-01-06 23:11:18 -08006550 u32 vlvf;
6551 int idx;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006552
Alexander Duyck16903ca2016-01-06 23:11:18 -08006553 /* short cut the special case */
6554 if (vlan == 0)
6555 return 0;
Alexander Duyck51466232009-10-27 23:47:35 +00006556
Alexander Duyck16903ca2016-01-06 23:11:18 -08006557 /* Search for the VLAN id in the VLVF entries */
6558 for (idx = E1000_VLVF_ARRAY_SIZE; --idx;) {
6559 vlvf = rd32(E1000_VLVF(idx));
6560 if ((vlvf & VLAN_VID_MASK) == vlan)
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006561 break;
6562 }
6563
Alexander Duyck16903ca2016-01-06 23:11:18 -08006564 return idx;
6565}
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006566
Jacob Keller8008f682016-04-13 16:08:29 -07006567static void igb_update_pf_vlvf(struct igb_adapter *adapter, u32 vid)
Alexander Duyck16903ca2016-01-06 23:11:18 -08006568{
6569 struct e1000_hw *hw = &adapter->hw;
6570 u32 bits, pf_id;
6571 int idx;
Alexander Duyckae641bd2009-09-03 14:49:33 +00006572
Alexander Duyck16903ca2016-01-06 23:11:18 -08006573 idx = igb_find_vlvf_entry(hw, vid);
6574 if (!idx)
6575 return;
Alexander Duyckae641bd2009-09-03 14:49:33 +00006576
Alexander Duyck16903ca2016-01-06 23:11:18 -08006577 /* See if any other pools are set for this VLAN filter
6578 * entry other than the PF.
6579 */
6580 pf_id = adapter->vfs_allocated_count + E1000_VLVF_POOLSEL_SHIFT;
Jacob Kellera51d8c22016-04-13 16:08:28 -07006581 bits = ~BIT(pf_id) & E1000_VLVF_POOLSEL_MASK;
Alexander Duyck16903ca2016-01-06 23:11:18 -08006582 bits &= rd32(E1000_VLVF(idx));
Carolyn Wyborny9005df32014-04-11 01:45:34 +00006583
Alexander Duyck16903ca2016-01-06 23:11:18 -08006584 /* Disable the filter so this falls into the default pool. */
6585 if (!bits) {
6586 if (adapter->flags & IGB_FLAG_VLAN_PROMISC)
Jacob Kellera51d8c22016-04-13 16:08:28 -07006587 wr32(E1000_VLVF(idx), BIT(pf_id));
Alexander Duyck16903ca2016-01-06 23:11:18 -08006588 else
6589 wr32(E1000_VLVF(idx), 0);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006590 }
Greg Rose6f3dc3192013-03-26 06:19:41 +00006591}
6592
Alexander Duycka15d9252016-01-06 23:11:11 -08006593static s32 igb_set_vf_vlan(struct igb_adapter *adapter, u32 vid,
6594 bool add, u32 vf)
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006595{
Alexander Duycka15d9252016-01-06 23:11:11 -08006596 int pf_id = adapter->vfs_allocated_count;
Greg Rose6f3dc3192013-03-26 06:19:41 +00006597 struct e1000_hw *hw = &adapter->hw;
Alexander Duycka15d9252016-01-06 23:11:11 -08006598 int err;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006599
Alexander Duycka15d9252016-01-06 23:11:11 -08006600 /* If VLAN overlaps with one the PF is currently monitoring make
6601 * sure that we are able to allocate a VLVF entry. This may be
6602 * redundant but it guarantees PF will maintain visibility to
6603 * the VLAN.
Greg Rose6f3dc3192013-03-26 06:19:41 +00006604 */
Alexander Duyck16903ca2016-01-06 23:11:18 -08006605 if (add && test_bit(vid, adapter->active_vlans)) {
Alexander Duycka15d9252016-01-06 23:11:11 -08006606 err = igb_vfta_set(hw, vid, pf_id, true, false);
6607 if (err)
6608 return err;
6609 }
Greg Rose6f3dc3192013-03-26 06:19:41 +00006610
Alexander Duycka15d9252016-01-06 23:11:11 -08006611 err = igb_vfta_set(hw, vid, vf, add, false);
Greg Rose6f3dc3192013-03-26 06:19:41 +00006612
Alexander Duyck16903ca2016-01-06 23:11:18 -08006613 if (add && !err)
6614 return err;
Greg Rose6f3dc3192013-03-26 06:19:41 +00006615
Alexander Duyck16903ca2016-01-06 23:11:18 -08006616 /* If we failed to add the VF VLAN or we are removing the VF VLAN
6617 * we may need to drop the PF pool bit in order to allow us to free
6618 * up the VLVF resources.
Greg Rose6f3dc3192013-03-26 06:19:41 +00006619 */
Alexander Duyck16903ca2016-01-06 23:11:18 -08006620 if (test_bit(vid, adapter->active_vlans) ||
6621 (adapter->flags & IGB_FLAG_VLAN_PROMISC))
6622 igb_update_pf_vlvf(adapter, vid);
Carolyn Wyborny9005df32014-04-11 01:45:34 +00006623
Greg Rose6f3dc3192013-03-26 06:19:41 +00006624 return err;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006625}
6626
6627static void igb_set_vmvir(struct igb_adapter *adapter, u32 vid, u32 vf)
6628{
6629 struct e1000_hw *hw = &adapter->hw;
6630
6631 if (vid)
6632 wr32(E1000_VMVIR(vf), (vid | E1000_VMVIR_VLANA_DEFAULT));
6633 else
6634 wr32(E1000_VMVIR(vf), 0);
6635}
6636
Alexander Duycka15d9252016-01-06 23:11:11 -08006637static int igb_enable_port_vlan(struct igb_adapter *adapter, int vf,
6638 u16 vlan, u8 qos)
6639{
6640 int err;
6641
6642 err = igb_set_vf_vlan(adapter, vlan, true, vf);
6643 if (err)
6644 return err;
6645
6646 igb_set_vmvir(adapter, vlan | (qos << VLAN_PRIO_SHIFT), vf);
6647 igb_set_vmolr(adapter, vf, !vlan);
6648
6649 /* revoke access to previous VLAN */
6650 if (vlan != adapter->vf_data[vf].pf_vlan)
6651 igb_set_vf_vlan(adapter, adapter->vf_data[vf].pf_vlan,
6652 false, vf);
6653
6654 adapter->vf_data[vf].pf_vlan = vlan;
6655 adapter->vf_data[vf].pf_qos = qos;
Corinna Vinschen030f9f52016-01-28 13:53:23 +01006656 igb_set_vf_vlan_strip(adapter, vf, true);
Alexander Duycka15d9252016-01-06 23:11:11 -08006657 dev_info(&adapter->pdev->dev,
6658 "Setting VLAN %d, QOS 0x%x on VF %d\n", vlan, qos, vf);
6659 if (test_bit(__IGB_DOWN, &adapter->state)) {
6660 dev_warn(&adapter->pdev->dev,
6661 "The VF VLAN has been set, but the PF device is not up.\n");
6662 dev_warn(&adapter->pdev->dev,
6663 "Bring the PF device up before attempting to use the VF device.\n");
6664 }
6665
6666 return err;
6667}
6668
6669static int igb_disable_port_vlan(struct igb_adapter *adapter, int vf)
6670{
6671 /* Restore tagless access via VLAN 0 */
6672 igb_set_vf_vlan(adapter, 0, true, vf);
6673
6674 igb_set_vmvir(adapter, 0, vf);
6675 igb_set_vmolr(adapter, vf, true);
6676
6677 /* Remove any PF assigned VLAN */
6678 if (adapter->vf_data[vf].pf_vlan)
6679 igb_set_vf_vlan(adapter, adapter->vf_data[vf].pf_vlan,
6680 false, vf);
6681
6682 adapter->vf_data[vf].pf_vlan = 0;
6683 adapter->vf_data[vf].pf_qos = 0;
Corinna Vinschen030f9f52016-01-28 13:53:23 +01006684 igb_set_vf_vlan_strip(adapter, vf, false);
Alexander Duycka15d9252016-01-06 23:11:11 -08006685
6686 return 0;
6687}
6688
Moshe Shemesh79aab092016-09-22 12:11:15 +03006689static int igb_ndo_set_vf_vlan(struct net_device *netdev, int vf,
6690 u16 vlan, u8 qos, __be16 vlan_proto)
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006691{
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006692 struct igb_adapter *adapter = netdev_priv(netdev);
6693
6694 if ((vf >= adapter->vfs_allocated_count) || (vlan > 4095) || (qos > 7))
6695 return -EINVAL;
Alexander Duycka15d9252016-01-06 23:11:11 -08006696
Moshe Shemesh79aab092016-09-22 12:11:15 +03006697 if (vlan_proto != htons(ETH_P_8021Q))
6698 return -EPROTONOSUPPORT;
6699
Alexander Duycka15d9252016-01-06 23:11:11 -08006700 return (vlan || qos) ? igb_enable_port_vlan(adapter, vf, vlan, qos) :
6701 igb_disable_port_vlan(adapter, vf);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006702}
6703
Alexander Duycka15d9252016-01-06 23:11:11 -08006704static int igb_set_vf_vlan_msg(struct igb_adapter *adapter, u32 *msgbuf, u32 vf)
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006705{
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006706 int add = (msgbuf[0] & E1000_VT_MSGINFO_MASK) >> E1000_VT_MSGINFO_SHIFT;
6707 int vid = (msgbuf[1] & E1000_VLVF_VLANID_MASK);
Corinna Vinschen030f9f52016-01-28 13:53:23 +01006708 int ret;
Alexander Duyckff41f8d2009-09-03 14:48:56 +00006709
Alexander Duycka15d9252016-01-06 23:11:11 -08006710 if (adapter->vf_data[vf].pf_vlan)
6711 return -1;
Mitch A Williams5ac6f912013-01-18 08:57:20 +00006712
Alexander Duycka15d9252016-01-06 23:11:11 -08006713 /* VLAN 0 is a special case, don't allow it to be removed */
6714 if (!vid && !add)
6715 return 0;
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00006716
Corinna Vinschen030f9f52016-01-28 13:53:23 +01006717 ret = igb_set_vf_vlan(adapter, vid, !!add, vf);
6718 if (!ret)
6719 igb_set_vf_vlan_strip(adapter, vf, !!vid);
6720 return ret;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006721}
6722
6723static inline void igb_vf_reset(struct igb_adapter *adapter, u32 vf)
6724{
Alexander Duycka15d9252016-01-06 23:11:11 -08006725 struct vf_data_storage *vf_data = &adapter->vf_data[vf];
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006726
Alexander Duycka15d9252016-01-06 23:11:11 -08006727 /* clear flags - except flag that indicates PF has set the MAC */
6728 vf_data->flags &= IGB_VF_FLAG_PF_SET_MAC;
6729 vf_data->last_nack = jiffies;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006730
6731 /* reset vlans for device */
6732 igb_clear_vf_vfta(adapter, vf);
Alexander Duycka15d9252016-01-06 23:11:11 -08006733 igb_set_vf_vlan(adapter, vf_data->pf_vlan, true, vf);
6734 igb_set_vmvir(adapter, vf_data->pf_vlan |
6735 (vf_data->pf_qos << VLAN_PRIO_SHIFT), vf);
6736 igb_set_vmolr(adapter, vf, !vf_data->pf_vlan);
Corinna Vinschen030f9f52016-01-28 13:53:23 +01006737 igb_set_vf_vlan_strip(adapter, vf, !!(vf_data->pf_vlan));
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006738
6739 /* reset multicast table array for vf */
6740 adapter->vf_data[vf].num_vf_mc_hashes = 0;
6741
6742 /* Flush and reset the mta with the new values */
6743 igb_set_rx_mode(adapter->netdev);
6744}
6745
6746static void igb_vf_reset_event(struct igb_adapter *adapter, u32 vf)
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00006747{
6748 unsigned char *vf_mac = adapter->vf_data[vf].vf_mac_addresses;
6749
6750 /* clear mac address as we were hotplug removed/added */
6751 if (!(adapter->vf_data[vf].flags & IGB_VF_FLAG_PF_SET_MAC))
6752 eth_zero_addr(vf_mac);
6753
6754 /* process remaining reset events */
6755 igb_vf_reset(adapter, vf);
6756}
6757
6758static void igb_vf_reset_msg(struct igb_adapter *adapter, u32 vf)
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006759{
6760 struct e1000_hw *hw = &adapter->hw;
6761 unsigned char *vf_mac = adapter->vf_data[vf].vf_mac_addresses;
6762 u32 reg, msgbuf[3];
6763 u8 *addr = (u8 *)(&msgbuf[1]);
6764
6765 /* process all the same items cleared in a function level reset */
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00006766 igb_vf_reset(adapter, vf);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006767
6768 /* set vf mac address */
Yury Kylulin83c21332017-03-07 11:20:25 +03006769 igb_set_vf_mac(adapter, vf, vf_mac);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006770
6771 /* enable transmit and receive for vf */
6772 reg = rd32(E1000_VFTE);
Jacob Kellera51d8c22016-04-13 16:08:28 -07006773 wr32(E1000_VFTE, reg | BIT(vf));
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006774 reg = rd32(E1000_VFRE);
Jacob Kellera51d8c22016-04-13 16:08:28 -07006775 wr32(E1000_VFRE, reg | BIT(vf));
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006776
Greg Rose8fa7e0f2010-11-06 05:43:21 +00006777 adapter->vf_data[vf].flags |= IGB_VF_FLAG_CTS;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006778
6779 /* reply to reset with ack and vf mac address */
Alexander Graf6ddbc4c2014-10-09 05:33:55 +00006780 if (!is_zero_ether_addr(vf_mac)) {
6781 msgbuf[0] = E1000_VF_RESET | E1000_VT_MSGTYPE_ACK;
6782 memcpy(addr, vf_mac, ETH_ALEN);
6783 } else {
6784 msgbuf[0] = E1000_VF_RESET | E1000_VT_MSGTYPE_NACK;
6785 }
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006786 igb_write_mbx(hw, msgbuf, 3, vf);
6787}
6788
Yury Kylulin83c21332017-03-07 11:20:25 +03006789static void igb_flush_mac_table(struct igb_adapter *adapter)
6790{
6791 struct e1000_hw *hw = &adapter->hw;
6792 int i;
6793
6794 for (i = 0; i < hw->mac.rar_entry_count; i++) {
6795 adapter->mac_table[i].state &= ~IGB_MAC_STATE_IN_USE;
6796 memset(adapter->mac_table[i].addr, 0, ETH_ALEN);
6797 adapter->mac_table[i].queue = 0;
6798 igb_rar_set_index(adapter, i);
6799 }
6800}
6801
6802static int igb_available_rars(struct igb_adapter *adapter, u8 queue)
6803{
6804 struct e1000_hw *hw = &adapter->hw;
6805 /* do not count rar entries reserved for VFs MAC addresses */
6806 int rar_entries = hw->mac.rar_entry_count -
6807 adapter->vfs_allocated_count;
6808 int i, count = 0;
6809
6810 for (i = 0; i < rar_entries; i++) {
6811 /* do not count default entries */
6812 if (adapter->mac_table[i].state & IGB_MAC_STATE_DEFAULT)
6813 continue;
6814
6815 /* do not count "in use" entries for different queues */
6816 if ((adapter->mac_table[i].state & IGB_MAC_STATE_IN_USE) &&
6817 (adapter->mac_table[i].queue != queue))
6818 continue;
6819
6820 count++;
6821 }
6822
6823 return count;
6824}
6825
6826/* Set default MAC address for the PF in the first RAR entry */
6827static void igb_set_default_mac_filter(struct igb_adapter *adapter)
6828{
6829 struct igb_mac_addr *mac_table = &adapter->mac_table[0];
6830
6831 ether_addr_copy(mac_table->addr, adapter->hw.mac.addr);
6832 mac_table->queue = adapter->vfs_allocated_count;
6833 mac_table->state = IGB_MAC_STATE_DEFAULT | IGB_MAC_STATE_IN_USE;
6834
6835 igb_rar_set_index(adapter, 0);
6836}
6837
Colin Ian Kingb476dea2017-04-27 18:59:11 +01006838static int igb_add_mac_filter(struct igb_adapter *adapter, const u8 *addr,
6839 const u8 queue)
Yury Kylulin83c21332017-03-07 11:20:25 +03006840{
6841 struct e1000_hw *hw = &adapter->hw;
6842 int rar_entries = hw->mac.rar_entry_count -
6843 adapter->vfs_allocated_count;
6844 int i;
6845
6846 if (is_zero_ether_addr(addr))
6847 return -EINVAL;
6848
6849 /* Search for the first empty entry in the MAC table.
6850 * Do not touch entries at the end of the table reserved for the VF MAC
6851 * addresses.
6852 */
6853 for (i = 0; i < rar_entries; i++) {
6854 if (adapter->mac_table[i].state & IGB_MAC_STATE_IN_USE)
6855 continue;
6856
6857 ether_addr_copy(adapter->mac_table[i].addr, addr);
6858 adapter->mac_table[i].queue = queue;
6859 adapter->mac_table[i].state |= IGB_MAC_STATE_IN_USE;
6860
6861 igb_rar_set_index(adapter, i);
6862 return i;
6863 }
6864
6865 return -ENOSPC;
6866}
6867
Colin Ian Kingb476dea2017-04-27 18:59:11 +01006868static int igb_del_mac_filter(struct igb_adapter *adapter, const u8 *addr,
6869 const u8 queue)
Yury Kylulin83c21332017-03-07 11:20:25 +03006870{
6871 struct e1000_hw *hw = &adapter->hw;
6872 int rar_entries = hw->mac.rar_entry_count -
6873 adapter->vfs_allocated_count;
6874 int i;
6875
6876 if (is_zero_ether_addr(addr))
6877 return -EINVAL;
6878
6879 /* Search for matching entry in the MAC table based on given address
6880 * and queue. Do not touch entries at the end of the table reserved
6881 * for the VF MAC addresses.
6882 */
6883 for (i = 0; i < rar_entries; i++) {
6884 if (!(adapter->mac_table[i].state & IGB_MAC_STATE_IN_USE))
6885 continue;
6886 if (adapter->mac_table[i].queue != queue)
6887 continue;
6888 if (!ether_addr_equal(adapter->mac_table[i].addr, addr))
6889 continue;
6890
6891 adapter->mac_table[i].state &= ~IGB_MAC_STATE_IN_USE;
6892 memset(adapter->mac_table[i].addr, 0, ETH_ALEN);
6893 adapter->mac_table[i].queue = 0;
6894
6895 igb_rar_set_index(adapter, i);
6896 return 0;
6897 }
6898
6899 return -ENOENT;
6900}
6901
6902static int igb_uc_sync(struct net_device *netdev, const unsigned char *addr)
6903{
6904 struct igb_adapter *adapter = netdev_priv(netdev);
6905 int ret;
6906
6907 ret = igb_add_mac_filter(adapter, addr, adapter->vfs_allocated_count);
6908
6909 return min_t(int, ret, 0);
6910}
6911
6912static int igb_uc_unsync(struct net_device *netdev, const unsigned char *addr)
6913{
6914 struct igb_adapter *adapter = netdev_priv(netdev);
6915
6916 igb_del_mac_filter(adapter, addr, adapter->vfs_allocated_count);
6917
6918 return 0;
6919}
6920
Colin Ian Kingb476dea2017-04-27 18:59:11 +01006921static int igb_set_vf_mac_filter(struct igb_adapter *adapter, const int vf,
6922 const u32 info, const u8 *addr)
Yury Kylulin4827cc32017-03-07 11:20:26 +03006923{
6924 struct pci_dev *pdev = adapter->pdev;
6925 struct vf_data_storage *vf_data = &adapter->vf_data[vf];
6926 struct list_head *pos;
6927 struct vf_mac_filter *entry = NULL;
6928 int ret = 0;
6929
6930 switch (info) {
6931 case E1000_VF_MAC_FILTER_CLR:
6932 /* remove all unicast MAC filters related to the current VF */
6933 list_for_each(pos, &adapter->vf_macs.l) {
6934 entry = list_entry(pos, struct vf_mac_filter, l);
6935 if (entry->vf == vf) {
6936 entry->vf = -1;
6937 entry->free = true;
6938 igb_del_mac_filter(adapter, entry->vf_mac, vf);
6939 }
6940 }
6941 break;
6942 case E1000_VF_MAC_FILTER_ADD:
6943 if (vf_data->flags & IGB_VF_FLAG_PF_SET_MAC) {
6944 dev_warn(&pdev->dev,
6945 "VF %d requested MAC filter but is administratively denied\n",
6946 vf);
6947 return -EINVAL;
6948 }
6949
6950 if (!is_valid_ether_addr(addr)) {
6951 dev_warn(&pdev->dev,
6952 "VF %d attempted to set invalid MAC filter\n",
6953 vf);
6954 return -EINVAL;
6955 }
6956
6957 /* try to find empty slot in the list */
6958 list_for_each(pos, &adapter->vf_macs.l) {
6959 entry = list_entry(pos, struct vf_mac_filter, l);
6960 if (entry->free)
6961 break;
6962 }
6963
6964 if (entry && entry->free) {
6965 entry->free = false;
6966 entry->vf = vf;
6967 ether_addr_copy(entry->vf_mac, addr);
6968
6969 ret = igb_add_mac_filter(adapter, addr, vf);
6970 ret = min_t(int, ret, 0);
6971 } else {
6972 ret = -ENOSPC;
6973 }
6974
6975 if (ret == -ENOSPC)
6976 dev_warn(&pdev->dev,
6977 "VF %d has requested MAC filter but there is no space for it\n",
6978 vf);
6979 break;
6980 default:
6981 ret = -EINVAL;
6982 break;
6983 }
6984
6985 return ret;
6986}
6987
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006988static int igb_set_vf_mac_addr(struct igb_adapter *adapter, u32 *msg, int vf)
6989{
Yury Kylulin4827cc32017-03-07 11:20:26 +03006990 struct pci_dev *pdev = adapter->pdev;
6991 struct vf_data_storage *vf_data = &adapter->vf_data[vf];
6992 u32 info = msg[0] & E1000_VT_MSGINFO_MASK;
6993
Jeff Kirsherb980ac12013-02-23 07:29:56 +00006994 /* The VF MAC Address is stored in a packed array of bytes
Greg Rosede42edd2010-07-01 13:39:23 +00006995 * starting at the second 32 bit word of the msg array
6996 */
Yury Kylulin4827cc32017-03-07 11:20:26 +03006997 unsigned char *addr = (unsigned char *)&msg[1];
6998 int ret = 0;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006999
Yury Kylulin4827cc32017-03-07 11:20:26 +03007000 if (!info) {
7001 if (vf_data->flags & IGB_VF_FLAG_PF_SET_MAC) {
7002 dev_warn(&pdev->dev,
7003 "VF %d attempted to override administratively set MAC address\nReload the VF driver to resume operations\n",
7004 vf);
7005 return -EINVAL;
7006 }
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007007
Yury Kylulin4827cc32017-03-07 11:20:26 +03007008 if (!is_valid_ether_addr(addr)) {
7009 dev_warn(&pdev->dev,
7010 "VF %d attempted to set invalid MAC\n",
7011 vf);
7012 return -EINVAL;
7013 }
7014
7015 ret = igb_set_vf_mac(adapter, vf, addr);
7016 } else {
7017 ret = igb_set_vf_mac_filter(adapter, vf, info, addr);
7018 }
7019
7020 return ret;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007021}
7022
7023static void igb_rcv_ack_from_vf(struct igb_adapter *adapter, u32 vf)
7024{
7025 struct e1000_hw *hw = &adapter->hw;
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00007026 struct vf_data_storage *vf_data = &adapter->vf_data[vf];
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007027 u32 msg = E1000_VT_MSGTYPE_NACK;
7028
7029 /* if device isn't clear to send it shouldn't be reading either */
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00007030 if (!(vf_data->flags & IGB_VF_FLAG_CTS) &&
7031 time_after(jiffies, vf_data->last_nack + (2 * HZ))) {
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007032 igb_write_mbx(hw, &msg, 1, vf);
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00007033 vf_data->last_nack = jiffies;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007034 }
7035}
7036
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00007037static void igb_rcv_msg_from_vf(struct igb_adapter *adapter, u32 vf)
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007038{
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00007039 struct pci_dev *pdev = adapter->pdev;
7040 u32 msgbuf[E1000_VFMAILBOX_SIZE];
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007041 struct e1000_hw *hw = &adapter->hw;
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00007042 struct vf_data_storage *vf_data = &adapter->vf_data[vf];
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007043 s32 retval;
7044
Greg Edwards46b3bb92017-06-28 09:22:26 -06007045 retval = igb_read_mbx(hw, msgbuf, E1000_VFMAILBOX_SIZE, vf, false);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007046
Alexander Duyckfef45f42009-12-11 22:57:34 -08007047 if (retval) {
7048 /* if receive failed revoke VF CTS stats and restart init */
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00007049 dev_err(&pdev->dev, "Error receiving message from VF\n");
Alexander Duyckfef45f42009-12-11 22:57:34 -08007050 vf_data->flags &= ~IGB_VF_FLAG_CTS;
7051 if (!time_after(jiffies, vf_data->last_nack + (2 * HZ)))
Greg Edwards46b3bb92017-06-28 09:22:26 -06007052 goto unlock;
Alexander Duyckfef45f42009-12-11 22:57:34 -08007053 goto out;
7054 }
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007055
7056 /* this is a message we already processed, do nothing */
7057 if (msgbuf[0] & (E1000_VT_MSGTYPE_ACK | E1000_VT_MSGTYPE_NACK))
Greg Edwards46b3bb92017-06-28 09:22:26 -06007058 goto unlock;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007059
Jeff Kirsherb980ac12013-02-23 07:29:56 +00007060 /* until the vf completes a reset it should not be
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007061 * allowed to start any configuration.
7062 */
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007063 if (msgbuf[0] == E1000_VF_RESET) {
Greg Edwards46b3bb92017-06-28 09:22:26 -06007064 /* unlocks mailbox */
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007065 igb_vf_reset_msg(adapter, vf);
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00007066 return;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007067 }
7068
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00007069 if (!(vf_data->flags & IGB_VF_FLAG_CTS)) {
Alexander Duyckfef45f42009-12-11 22:57:34 -08007070 if (!time_after(jiffies, vf_data->last_nack + (2 * HZ)))
Greg Edwards46b3bb92017-06-28 09:22:26 -06007071 goto unlock;
Alexander Duyckfef45f42009-12-11 22:57:34 -08007072 retval = -1;
7073 goto out;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007074 }
7075
7076 switch ((msgbuf[0] & 0xFFFF)) {
7077 case E1000_VF_SET_MAC_ADDR:
Yury Kylulin4827cc32017-03-07 11:20:26 +03007078 retval = igb_set_vf_mac_addr(adapter, msgbuf, vf);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007079 break;
Alexander Duyck7d5753f2009-10-27 23:47:16 +00007080 case E1000_VF_SET_PROMISC:
7081 retval = igb_set_vf_promisc(adapter, msgbuf, vf);
7082 break;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007083 case E1000_VF_SET_MULTICAST:
7084 retval = igb_set_vf_multicasts(adapter, msgbuf, vf);
7085 break;
7086 case E1000_VF_SET_LPE:
7087 retval = igb_set_vf_rlpml(adapter, msgbuf[1], vf);
7088 break;
7089 case E1000_VF_SET_VLAN:
Greg Rosea6b5ea32010-11-06 05:42:59 +00007090 retval = -1;
7091 if (vf_data->pf_vlan)
7092 dev_warn(&pdev->dev,
Jeff Kirsherb980ac12013-02-23 07:29:56 +00007093 "VF %d attempted to override administratively set VLAN tag\nReload the VF driver to resume operations\n",
7094 vf);
Williams, Mitch A8151d292010-02-10 01:44:24 +00007095 else
Alexander Duycka15d9252016-01-06 23:11:11 -08007096 retval = igb_set_vf_vlan_msg(adapter, msgbuf, vf);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007097 break;
7098 default:
Alexander Duyck090b1792009-10-27 23:51:55 +00007099 dev_err(&pdev->dev, "Unhandled Msg %08x\n", msgbuf[0]);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007100 retval = -1;
7101 break;
7102 }
7103
Alexander Duyckfef45f42009-12-11 22:57:34 -08007104 msgbuf[0] |= E1000_VT_MSGTYPE_CTS;
7105out:
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007106 /* notify the VF of the results of what it sent us */
7107 if (retval)
7108 msgbuf[0] |= E1000_VT_MSGTYPE_NACK;
7109 else
7110 msgbuf[0] |= E1000_VT_MSGTYPE_ACK;
7111
Greg Edwards46b3bb92017-06-28 09:22:26 -06007112 /* unlocks mailbox */
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007113 igb_write_mbx(hw, msgbuf, 1, vf);
Greg Edwards46b3bb92017-06-28 09:22:26 -06007114 return;
7115
7116unlock:
7117 igb_unlock_mbx(hw, vf);
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00007118}
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007119
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00007120static void igb_msg_task(struct igb_adapter *adapter)
7121{
7122 struct e1000_hw *hw = &adapter->hw;
7123 u32 vf;
7124
7125 for (vf = 0; vf < adapter->vfs_allocated_count; vf++) {
7126 /* process any reset requests */
7127 if (!igb_check_for_rst(hw, vf))
7128 igb_vf_reset_event(adapter, vf);
7129
7130 /* process any messages pending */
7131 if (!igb_check_for_msg(hw, vf))
7132 igb_rcv_msg_from_vf(adapter, vf);
7133
7134 /* process any acks */
7135 if (!igb_check_for_ack(hw, vf))
7136 igb_rcv_ack_from_vf(adapter, vf);
7137 }
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007138}
7139
Auke Kok9d5c8242008-01-24 02:22:38 -08007140/**
Alexander Duyck68d480c2009-10-05 06:33:08 +00007141 * igb_set_uta - Set unicast filter table address
7142 * @adapter: board private structure
Alexander Duyckbf456ab2016-01-06 23:11:43 -08007143 * @set: boolean indicating if we are setting or clearing bits
Alexander Duyck68d480c2009-10-05 06:33:08 +00007144 *
7145 * The unicast table address is a register array of 32-bit registers.
7146 * The table is meant to be used in a way similar to how the MTA is used
7147 * however due to certain limitations in the hardware it is necessary to
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007148 * set all the hash bits to 1 and use the VMOLR ROPE bit as a promiscuous
7149 * enable bit to allow vlan tag stripping when promiscuous mode is enabled
Alexander Duyck68d480c2009-10-05 06:33:08 +00007150 **/
Alexander Duyckbf456ab2016-01-06 23:11:43 -08007151static void igb_set_uta(struct igb_adapter *adapter, bool set)
Alexander Duyck68d480c2009-10-05 06:33:08 +00007152{
7153 struct e1000_hw *hw = &adapter->hw;
Alexander Duyckbf456ab2016-01-06 23:11:43 -08007154 u32 uta = set ? ~0 : 0;
Alexander Duyck68d480c2009-10-05 06:33:08 +00007155 int i;
7156
Alexander Duyck68d480c2009-10-05 06:33:08 +00007157 /* we only need to do this if VMDq is enabled */
7158 if (!adapter->vfs_allocated_count)
7159 return;
7160
Alexander Duyckbf456ab2016-01-06 23:11:43 -08007161 for (i = hw->mac.uta_reg_count; i--;)
7162 array_wr32(E1000_UTA, i, uta);
Alexander Duyck68d480c2009-10-05 06:33:08 +00007163}
7164
7165/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00007166 * igb_intr_msi - Interrupt Handler
7167 * @irq: interrupt number
7168 * @data: pointer to a network interface device structure
Auke Kok9d5c8242008-01-24 02:22:38 -08007169 **/
7170static irqreturn_t igb_intr_msi(int irq, void *data)
7171{
Alexander Duyck047e0032009-10-27 15:49:27 +00007172 struct igb_adapter *adapter = data;
7173 struct igb_q_vector *q_vector = adapter->q_vector[0];
Auke Kok9d5c8242008-01-24 02:22:38 -08007174 struct e1000_hw *hw = &adapter->hw;
7175 /* read ICR disables interrupts using IAM */
7176 u32 icr = rd32(E1000_ICR);
7177
Alexander Duyck047e0032009-10-27 15:49:27 +00007178 igb_write_itr(q_vector);
Auke Kok9d5c8242008-01-24 02:22:38 -08007179
Alexander Duyck7f081d42010-01-07 17:41:00 +00007180 if (icr & E1000_ICR_DRSTA)
7181 schedule_work(&adapter->reset_task);
7182
Alexander Duyck047e0032009-10-27 15:49:27 +00007183 if (icr & E1000_ICR_DOUTSYNC) {
Alexander Duyckdda0e082009-02-06 23:19:08 +00007184 /* HW is reporting DMA is out of sync */
7185 adapter->stats.doosync++;
7186 }
7187
Auke Kok9d5c8242008-01-24 02:22:38 -08007188 if (icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) {
7189 hw->mac.get_link_status = 1;
7190 if (!test_bit(__IGB_DOWN, &adapter->state))
7191 mod_timer(&adapter->watchdog_timer, jiffies + 1);
7192 }
7193
Richard Cochran61d7f752014-11-21 20:51:10 +00007194 if (icr & E1000_ICR_TS)
7195 igb_tsync_interrupt(adapter);
Matthew Vick1f6e8172012-08-18 07:26:33 +00007196
Alexander Duyck047e0032009-10-27 15:49:27 +00007197 napi_schedule(&q_vector->napi);
Auke Kok9d5c8242008-01-24 02:22:38 -08007198
7199 return IRQ_HANDLED;
7200}
7201
7202/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00007203 * igb_intr - Legacy Interrupt Handler
7204 * @irq: interrupt number
7205 * @data: pointer to a network interface device structure
Auke Kok9d5c8242008-01-24 02:22:38 -08007206 **/
7207static irqreturn_t igb_intr(int irq, void *data)
7208{
Alexander Duyck047e0032009-10-27 15:49:27 +00007209 struct igb_adapter *adapter = data;
7210 struct igb_q_vector *q_vector = adapter->q_vector[0];
Auke Kok9d5c8242008-01-24 02:22:38 -08007211 struct e1000_hw *hw = &adapter->hw;
7212 /* Interrupt Auto-Mask...upon reading ICR, interrupts are masked. No
Jeff Kirsherb980ac12013-02-23 07:29:56 +00007213 * need for the IMC write
7214 */
Auke Kok9d5c8242008-01-24 02:22:38 -08007215 u32 icr = rd32(E1000_ICR);
Auke Kok9d5c8242008-01-24 02:22:38 -08007216
7217 /* IMS will not auto-mask if INT_ASSERTED is not set, and if it is
Jeff Kirsherb980ac12013-02-23 07:29:56 +00007218 * not set, then the adapter didn't send an interrupt
7219 */
Auke Kok9d5c8242008-01-24 02:22:38 -08007220 if (!(icr & E1000_ICR_INT_ASSERTED))
7221 return IRQ_NONE;
7222
Alexander Duyck0ba82992011-08-26 07:45:47 +00007223 igb_write_itr(q_vector);
7224
Alexander Duyck7f081d42010-01-07 17:41:00 +00007225 if (icr & E1000_ICR_DRSTA)
7226 schedule_work(&adapter->reset_task);
7227
Alexander Duyck047e0032009-10-27 15:49:27 +00007228 if (icr & E1000_ICR_DOUTSYNC) {
Alexander Duyckdda0e082009-02-06 23:19:08 +00007229 /* HW is reporting DMA is out of sync */
7230 adapter->stats.doosync++;
7231 }
7232
Auke Kok9d5c8242008-01-24 02:22:38 -08007233 if (icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) {
7234 hw->mac.get_link_status = 1;
7235 /* guard against interrupt when we're going down */
7236 if (!test_bit(__IGB_DOWN, &adapter->state))
7237 mod_timer(&adapter->watchdog_timer, jiffies + 1);
7238 }
7239
Richard Cochran61d7f752014-11-21 20:51:10 +00007240 if (icr & E1000_ICR_TS)
7241 igb_tsync_interrupt(adapter);
Matthew Vick1f6e8172012-08-18 07:26:33 +00007242
Alexander Duyck047e0032009-10-27 15:49:27 +00007243 napi_schedule(&q_vector->napi);
Auke Kok9d5c8242008-01-24 02:22:38 -08007244
7245 return IRQ_HANDLED;
7246}
7247
Stephen Hemmingerc50b52a2012-01-18 22:13:26 +00007248static void igb_ring_irq_enable(struct igb_q_vector *q_vector)
Alexander Duyck46544252009-02-19 20:39:04 -08007249{
Alexander Duyck047e0032009-10-27 15:49:27 +00007250 struct igb_adapter *adapter = q_vector->adapter;
Alexander Duyck46544252009-02-19 20:39:04 -08007251 struct e1000_hw *hw = &adapter->hw;
7252
Alexander Duyck0ba82992011-08-26 07:45:47 +00007253 if ((q_vector->rx.ring && (adapter->rx_itr_setting & 3)) ||
7254 (!q_vector->rx.ring && (adapter->tx_itr_setting & 3))) {
7255 if ((adapter->num_q_vectors == 1) && !adapter->vf_data)
7256 igb_set_itr(q_vector);
Alexander Duyck46544252009-02-19 20:39:04 -08007257 else
Alexander Duyck047e0032009-10-27 15:49:27 +00007258 igb_update_ring_itr(q_vector);
Alexander Duyck46544252009-02-19 20:39:04 -08007259 }
7260
7261 if (!test_bit(__IGB_DOWN, &adapter->state)) {
Carolyn Wybornycd14ef52013-12-10 07:58:34 +00007262 if (adapter->flags & IGB_FLAG_HAS_MSIX)
Alexander Duyck047e0032009-10-27 15:49:27 +00007263 wr32(E1000_EIMS, q_vector->eims_value);
Alexander Duyck46544252009-02-19 20:39:04 -08007264 else
7265 igb_irq_enable(adapter);
7266 }
7267}
7268
Auke Kok9d5c8242008-01-24 02:22:38 -08007269/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00007270 * igb_poll - NAPI Rx polling callback
7271 * @napi: napi polling structure
7272 * @budget: count of how many packets we should handle
Auke Kok9d5c8242008-01-24 02:22:38 -08007273 **/
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07007274static int igb_poll(struct napi_struct *napi, int budget)
Auke Kok9d5c8242008-01-24 02:22:38 -08007275{
Alexander Duyck047e0032009-10-27 15:49:27 +00007276 struct igb_q_vector *q_vector = container_of(napi,
Jeff Kirsherb980ac12013-02-23 07:29:56 +00007277 struct igb_q_vector,
7278 napi);
Alexander Duyck16eb8812011-08-26 07:43:54 +00007279 bool clean_complete = true;
Jesse Brandeburg32b3e082015-09-24 16:35:47 -07007280 int work_done = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08007281
Jeff Kirsher421e02f2008-10-17 11:08:31 -07007282#ifdef CONFIG_IGB_DCA
Alexander Duyck047e0032009-10-27 15:49:27 +00007283 if (q_vector->adapter->flags & IGB_FLAG_DCA_ENABLED)
7284 igb_update_dca(q_vector);
Jeb Cramerfe4506b2008-07-08 15:07:55 -07007285#endif
Alexander Duyck0ba82992011-08-26 07:45:47 +00007286 if (q_vector->tx.ring)
Alexander Duyck7f0ba842016-03-07 09:30:21 -08007287 clean_complete = igb_clean_tx_irq(q_vector, budget);
Auke Kok9d5c8242008-01-24 02:22:38 -08007288
Jesse Brandeburg32b3e082015-09-24 16:35:47 -07007289 if (q_vector->rx.ring) {
7290 int cleaned = igb_clean_rx_irq(q_vector, budget);
7291
7292 work_done += cleaned;
Alexander Duyck7f0ba842016-03-07 09:30:21 -08007293 if (cleaned >= budget)
7294 clean_complete = false;
Jesse Brandeburg32b3e082015-09-24 16:35:47 -07007295 }
Alexander Duyck047e0032009-10-27 15:49:27 +00007296
Alexander Duyck16eb8812011-08-26 07:43:54 +00007297 /* If all work not completed, return budget and keep polling */
7298 if (!clean_complete)
7299 return budget;
Auke Kok9d5c8242008-01-24 02:22:38 -08007300
Alexander Duyck46544252009-02-19 20:39:04 -08007301 /* If not enough Rx work done, exit the polling mode */
Jesse Brandeburg32b3e082015-09-24 16:35:47 -07007302 napi_complete_done(napi, work_done);
Alexander Duyck16eb8812011-08-26 07:43:54 +00007303 igb_ring_irq_enable(q_vector);
Alexander Duyck46544252009-02-19 20:39:04 -08007304
Alexander Duyck16eb8812011-08-26 07:43:54 +00007305 return 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08007306}
Al Viro6d8126f2008-03-16 22:23:24 +00007307
Patrick Ohly33af6bc2009-02-12 05:03:43 +00007308/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00007309 * igb_clean_tx_irq - Reclaim resources after transmit completes
7310 * @q_vector: pointer to q_vector containing needed info
Alexander Duyck7f0ba842016-03-07 09:30:21 -08007311 * @napi_budget: Used to determine if we are in netpoll
Ben Hutchings49ce9c22012-07-10 10:56:00 +00007312 *
Jeff Kirsherb980ac12013-02-23 07:29:56 +00007313 * returns true if ring is completely cleaned
Auke Kok9d5c8242008-01-24 02:22:38 -08007314 **/
Alexander Duyck7f0ba842016-03-07 09:30:21 -08007315static bool igb_clean_tx_irq(struct igb_q_vector *q_vector, int napi_budget)
Auke Kok9d5c8242008-01-24 02:22:38 -08007316{
Alexander Duyck047e0032009-10-27 15:49:27 +00007317 struct igb_adapter *adapter = q_vector->adapter;
Alexander Duyck0ba82992011-08-26 07:45:47 +00007318 struct igb_ring *tx_ring = q_vector->tx.ring;
Alexander Duyck06034642011-08-26 07:44:22 +00007319 struct igb_tx_buffer *tx_buffer;
Alexander Duyckf4128782012-09-13 06:28:01 +00007320 union e1000_adv_tx_desc *tx_desc;
Auke Kok9d5c8242008-01-24 02:22:38 -08007321 unsigned int total_bytes = 0, total_packets = 0;
Alexander Duyck0ba82992011-08-26 07:45:47 +00007322 unsigned int budget = q_vector->tx.work_limit;
Alexander Duyck8542db02011-08-26 07:44:43 +00007323 unsigned int i = tx_ring->next_to_clean;
Auke Kok9d5c8242008-01-24 02:22:38 -08007324
Alexander Duyck13fde972011-10-05 13:35:24 +00007325 if (test_bit(__IGB_DOWN, &adapter->state))
7326 return true;
Alexander Duyck0e014cb2008-12-26 01:33:18 -08007327
Alexander Duyck06034642011-08-26 07:44:22 +00007328 tx_buffer = &tx_ring->tx_buffer_info[i];
Alexander Duyck13fde972011-10-05 13:35:24 +00007329 tx_desc = IGB_TX_DESC(tx_ring, i);
Alexander Duyck8542db02011-08-26 07:44:43 +00007330 i -= tx_ring->count;
Auke Kok9d5c8242008-01-24 02:22:38 -08007331
Alexander Duyckf4128782012-09-13 06:28:01 +00007332 do {
7333 union e1000_adv_tx_desc *eop_desc = tx_buffer->next_to_watch;
Alexander Duyck8542db02011-08-26 07:44:43 +00007334
7335 /* if next_to_watch is not set then there is no work pending */
7336 if (!eop_desc)
7337 break;
Alexander Duyck13fde972011-10-05 13:35:24 +00007338
Alexander Duyckf4128782012-09-13 06:28:01 +00007339 /* prevent any other reads prior to eop_desc */
Brian Kingc4cb9912017-11-17 11:05:47 -06007340 smp_rmb();
Alexander Duyckf4128782012-09-13 06:28:01 +00007341
Alexander Duyck13fde972011-10-05 13:35:24 +00007342 /* if DD is not set pending work has not been completed */
7343 if (!(eop_desc->wb.status & cpu_to_le32(E1000_TXD_STAT_DD)))
7344 break;
7345
Alexander Duyck8542db02011-08-26 07:44:43 +00007346 /* clear next_to_watch to prevent false hangs */
7347 tx_buffer->next_to_watch = NULL;
Alexander Duyck13fde972011-10-05 13:35:24 +00007348
Alexander Duyckebe42d12011-08-26 07:45:09 +00007349 /* update the statistics for this packet */
7350 total_bytes += tx_buffer->bytecount;
7351 total_packets += tx_buffer->gso_segs;
Alexander Duyck13fde972011-10-05 13:35:24 +00007352
Alexander Duyckebe42d12011-08-26 07:45:09 +00007353 /* free the skb */
Alexander Duyck7f0ba842016-03-07 09:30:21 -08007354 napi_consume_skb(tx_buffer->skb, napi_budget);
Alexander Duyckebe42d12011-08-26 07:45:09 +00007355
7356 /* unmap skb header data */
7357 dma_unmap_single(tx_ring->dev,
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00007358 dma_unmap_addr(tx_buffer, dma),
7359 dma_unmap_len(tx_buffer, len),
Alexander Duyckebe42d12011-08-26 07:45:09 +00007360 DMA_TO_DEVICE);
7361
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00007362 /* clear tx_buffer data */
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00007363 dma_unmap_len_set(tx_buffer, len, 0);
7364
Alexander Duyckebe42d12011-08-26 07:45:09 +00007365 /* clear last DMA location and unmap remaining buffers */
7366 while (tx_desc != eop_desc) {
Alexander Duyck13fde972011-10-05 13:35:24 +00007367 tx_buffer++;
7368 tx_desc++;
Auke Kok9d5c8242008-01-24 02:22:38 -08007369 i++;
Alexander Duyck8542db02011-08-26 07:44:43 +00007370 if (unlikely(!i)) {
7371 i -= tx_ring->count;
Alexander Duyck06034642011-08-26 07:44:22 +00007372 tx_buffer = tx_ring->tx_buffer_info;
Alexander Duyck13fde972011-10-05 13:35:24 +00007373 tx_desc = IGB_TX_DESC(tx_ring, 0);
7374 }
Alexander Duyckebe42d12011-08-26 07:45:09 +00007375
7376 /* unmap any remaining paged data */
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00007377 if (dma_unmap_len(tx_buffer, len)) {
Alexander Duyckebe42d12011-08-26 07:45:09 +00007378 dma_unmap_page(tx_ring->dev,
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00007379 dma_unmap_addr(tx_buffer, dma),
7380 dma_unmap_len(tx_buffer, len),
Alexander Duyckebe42d12011-08-26 07:45:09 +00007381 DMA_TO_DEVICE);
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00007382 dma_unmap_len_set(tx_buffer, len, 0);
Alexander Duyckebe42d12011-08-26 07:45:09 +00007383 }
7384 }
7385
Alexander Duyckebe42d12011-08-26 07:45:09 +00007386 /* move us one more past the eop_desc for start of next pkt */
7387 tx_buffer++;
7388 tx_desc++;
7389 i++;
7390 if (unlikely(!i)) {
7391 i -= tx_ring->count;
7392 tx_buffer = tx_ring->tx_buffer_info;
7393 tx_desc = IGB_TX_DESC(tx_ring, 0);
7394 }
Alexander Duyckf4128782012-09-13 06:28:01 +00007395
7396 /* issue prefetch for next Tx descriptor */
7397 prefetch(tx_desc);
7398
7399 /* update budget accounting */
7400 budget--;
7401 } while (likely(budget));
Alexander Duyck0e014cb2008-12-26 01:33:18 -08007402
Eric Dumazetbdbc0632012-01-04 20:23:36 +00007403 netdev_tx_completed_queue(txring_txq(tx_ring),
7404 total_packets, total_bytes);
Alexander Duyck8542db02011-08-26 07:44:43 +00007405 i += tx_ring->count;
Auke Kok9d5c8242008-01-24 02:22:38 -08007406 tx_ring->next_to_clean = i;
Alexander Duyck13fde972011-10-05 13:35:24 +00007407 u64_stats_update_begin(&tx_ring->tx_syncp);
7408 tx_ring->tx_stats.bytes += total_bytes;
7409 tx_ring->tx_stats.packets += total_packets;
7410 u64_stats_update_end(&tx_ring->tx_syncp);
Alexander Duyck0ba82992011-08-26 07:45:47 +00007411 q_vector->tx.total_bytes += total_bytes;
7412 q_vector->tx.total_packets += total_packets;
Auke Kok9d5c8242008-01-24 02:22:38 -08007413
Alexander Duyck6d095fa2011-08-26 07:46:19 +00007414 if (test_bit(IGB_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags)) {
Alexander Duyck13fde972011-10-05 13:35:24 +00007415 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck13fde972011-10-05 13:35:24 +00007416
Auke Kok9d5c8242008-01-24 02:22:38 -08007417 /* Detect a transmit hang in hardware, this serializes the
Jeff Kirsherb980ac12013-02-23 07:29:56 +00007418 * check with the clearing of time_stamp and movement of i
7419 */
Alexander Duyck6d095fa2011-08-26 07:46:19 +00007420 clear_bit(IGB_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags);
Alexander Duyckf4128782012-09-13 06:28:01 +00007421 if (tx_buffer->next_to_watch &&
Alexander Duyck8542db02011-08-26 07:44:43 +00007422 time_after(jiffies, tx_buffer->time_stamp +
Joe Perches8e95a202009-12-03 07:58:21 +00007423 (adapter->tx_timeout_factor * HZ)) &&
7424 !(rd32(E1000_STATUS) & E1000_STATUS_TXOFF)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08007425
Auke Kok9d5c8242008-01-24 02:22:38 -08007426 /* detected Tx unit hang */
Alexander Duyck59d71982010-04-27 13:09:25 +00007427 dev_err(tx_ring->dev,
Auke Kok9d5c8242008-01-24 02:22:38 -08007428 "Detected Tx Unit Hang\n"
Alexander Duyck2d064c02008-07-08 15:10:12 -07007429 " Tx Queue <%d>\n"
Auke Kok9d5c8242008-01-24 02:22:38 -08007430 " TDH <%x>\n"
7431 " TDT <%x>\n"
7432 " next_to_use <%x>\n"
7433 " next_to_clean <%x>\n"
Auke Kok9d5c8242008-01-24 02:22:38 -08007434 "buffer_info[next_to_clean]\n"
7435 " time_stamp <%lx>\n"
Alexander Duyck8542db02011-08-26 07:44:43 +00007436 " next_to_watch <%p>\n"
Auke Kok9d5c8242008-01-24 02:22:38 -08007437 " jiffies <%lx>\n"
7438 " desc.status <%x>\n",
Alexander Duyck2d064c02008-07-08 15:10:12 -07007439 tx_ring->queue_index,
Alexander Duyck238ac812011-08-26 07:43:48 +00007440 rd32(E1000_TDH(tx_ring->reg_idx)),
Alexander Duyckfce99e32009-10-27 15:51:27 +00007441 readl(tx_ring->tail),
Auke Kok9d5c8242008-01-24 02:22:38 -08007442 tx_ring->next_to_use,
7443 tx_ring->next_to_clean,
Alexander Duyck8542db02011-08-26 07:44:43 +00007444 tx_buffer->time_stamp,
Alexander Duyckf4128782012-09-13 06:28:01 +00007445 tx_buffer->next_to_watch,
Auke Kok9d5c8242008-01-24 02:22:38 -08007446 jiffies,
Alexander Duyckf4128782012-09-13 06:28:01 +00007447 tx_buffer->next_to_watch->wb.status);
Alexander Duyck13fde972011-10-05 13:35:24 +00007448 netif_stop_subqueue(tx_ring->netdev,
7449 tx_ring->queue_index);
7450
7451 /* we are about to reset, no point in enabling stuff */
7452 return true;
Auke Kok9d5c8242008-01-24 02:22:38 -08007453 }
7454 }
Alexander Duyck13fde972011-10-05 13:35:24 +00007455
Alexander Duyck21ba6fe2013-02-09 04:27:48 +00007456#define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
Alexander Duyck13fde972011-10-05 13:35:24 +00007457 if (unlikely(total_packets &&
Jeff Kirsherb980ac12013-02-23 07:29:56 +00007458 netif_carrier_ok(tx_ring->netdev) &&
7459 igb_desc_unused(tx_ring) >= TX_WAKE_THRESHOLD)) {
Alexander Duyck13fde972011-10-05 13:35:24 +00007460 /* Make sure that anybody stopping the queue after this
7461 * sees the new next_to_clean.
7462 */
7463 smp_mb();
7464 if (__netif_subqueue_stopped(tx_ring->netdev,
7465 tx_ring->queue_index) &&
7466 !(test_bit(__IGB_DOWN, &adapter->state))) {
7467 netif_wake_subqueue(tx_ring->netdev,
7468 tx_ring->queue_index);
7469
7470 u64_stats_update_begin(&tx_ring->tx_syncp);
7471 tx_ring->tx_stats.restart_queue++;
7472 u64_stats_update_end(&tx_ring->tx_syncp);
7473 }
7474 }
7475
7476 return !!budget;
Auke Kok9d5c8242008-01-24 02:22:38 -08007477}
7478
Alexander Duyckcbc8e552012-09-25 00:31:02 +00007479/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00007480 * igb_reuse_rx_page - page flip buffer and store it back on the ring
7481 * @rx_ring: rx descriptor ring to store buffers on
7482 * @old_buff: donor buffer to have page reused
Alexander Duyckcbc8e552012-09-25 00:31:02 +00007483 *
Jeff Kirsherb980ac12013-02-23 07:29:56 +00007484 * Synchronizes page for reuse by the adapter
Alexander Duyckcbc8e552012-09-25 00:31:02 +00007485 **/
7486static void igb_reuse_rx_page(struct igb_ring *rx_ring,
7487 struct igb_rx_buffer *old_buff)
7488{
7489 struct igb_rx_buffer *new_buff;
7490 u16 nta = rx_ring->next_to_alloc;
7491
7492 new_buff = &rx_ring->rx_buffer_info[nta];
7493
7494 /* update, and store next to alloc */
7495 nta++;
7496 rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0;
7497
Alexander Duycke0142722017-02-06 18:27:26 -08007498 /* Transfer page from old buffer to new buffer.
7499 * Move each member individually to avoid possible store
7500 * forwarding stalls.
7501 */
7502 new_buff->dma = old_buff->dma;
7503 new_buff->page = old_buff->page;
7504 new_buff->page_offset = old_buff->page_offset;
7505 new_buff->pagecnt_bias = old_buff->pagecnt_bias;
Alexander Duyckcbc8e552012-09-25 00:31:02 +00007506}
7507
Alexander Duyck95dd44b2014-11-14 00:56:19 +00007508static inline bool igb_page_is_reserved(struct page *page)
7509{
Michal Hocko2f064f32015-08-21 14:11:51 -07007510 return (page_to_nid(page) != numa_mem_id()) || page_is_pfmemalloc(page);
Alexander Duyck95dd44b2014-11-14 00:56:19 +00007511}
7512
Alexander Duycke0142722017-02-06 18:27:26 -08007513static bool igb_can_reuse_rx_page(struct igb_rx_buffer *rx_buffer)
Alexander Duyck74e238e2013-02-02 05:07:11 +00007514{
Alexander Duycke0142722017-02-06 18:27:26 -08007515 unsigned int pagecnt_bias = rx_buffer->pagecnt_bias;
7516 struct page *page = rx_buffer->page;
Alexander Duyckbd4171a2016-12-14 15:05:34 -08007517
Alexander Duyck74e238e2013-02-02 05:07:11 +00007518 /* avoid re-using remote pages */
Alexander Duyck95dd44b2014-11-14 00:56:19 +00007519 if (unlikely(igb_page_is_reserved(page)))
Roman Gushchinbc16e472014-10-23 03:32:27 +00007520 return false;
7521
Alexander Duyck74e238e2013-02-02 05:07:11 +00007522#if (PAGE_SIZE < 8192)
7523 /* if we are only owner of page we can reuse it */
Alexander Duycke0142722017-02-06 18:27:26 -08007524 if (unlikely((page_ref_count(page) - pagecnt_bias) > 1))
Alexander Duyck74e238e2013-02-02 05:07:11 +00007525 return false;
Alexander Duyck74e238e2013-02-02 05:07:11 +00007526#else
Alexander Duyck8649aae2017-02-06 18:27:03 -08007527#define IGB_LAST_OFFSET \
7528 (SKB_WITH_OVERHEAD(PAGE_SIZE) - IGB_RXBUFFER_2048)
Alexander Duyck74e238e2013-02-02 05:07:11 +00007529
Alexander Duyck8649aae2017-02-06 18:27:03 -08007530 if (rx_buffer->page_offset > IGB_LAST_OFFSET)
Alexander Duyck74e238e2013-02-02 05:07:11 +00007531 return false;
Alexander Duyck74e238e2013-02-02 05:07:11 +00007532#endif
7533
Alexander Duyckbd4171a2016-12-14 15:05:34 -08007534 /* If we have drained the page fragment pool we need to update
7535 * the pagecnt_bias and page count so that we fully restock the
7536 * number of references the driver holds.
Alexander Duyck95dd44b2014-11-14 00:56:19 +00007537 */
Alexander Duycke0142722017-02-06 18:27:26 -08007538 if (unlikely(!pagecnt_bias)) {
Alexander Duyckbd4171a2016-12-14 15:05:34 -08007539 page_ref_add(page, USHRT_MAX);
7540 rx_buffer->pagecnt_bias = USHRT_MAX;
7541 }
Alexander Duyck95dd44b2014-11-14 00:56:19 +00007542
Alexander Duyck74e238e2013-02-02 05:07:11 +00007543 return true;
7544}
7545
Alexander Duyckcbc8e552012-09-25 00:31:02 +00007546/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00007547 * igb_add_rx_frag - Add contents of Rx buffer to sk_buff
7548 * @rx_ring: rx descriptor ring to transact packets on
7549 * @rx_buffer: buffer containing page to add
Jeff Kirsherb980ac12013-02-23 07:29:56 +00007550 * @skb: sk_buff to place the data into
Alexander Duycke0142722017-02-06 18:27:26 -08007551 * @size: size of buffer to be added
Alexander Duyckcbc8e552012-09-25 00:31:02 +00007552 *
Jeff Kirsherb980ac12013-02-23 07:29:56 +00007553 * This function will add the data contained in rx_buffer->page to the skb.
Alexander Duyckcbc8e552012-09-25 00:31:02 +00007554 **/
Alexander Duycke0142722017-02-06 18:27:26 -08007555static void igb_add_rx_frag(struct igb_ring *rx_ring,
Alexander Duyckcbc8e552012-09-25 00:31:02 +00007556 struct igb_rx_buffer *rx_buffer,
Alexander Duycke0142722017-02-06 18:27:26 -08007557 struct sk_buff *skb,
7558 unsigned int size)
Alexander Duyckcbc8e552012-09-25 00:31:02 +00007559{
Alexander Duyck74e238e2013-02-02 05:07:11 +00007560#if (PAGE_SIZE < 8192)
Alexander Duyck8649aae2017-02-06 18:27:03 -08007561 unsigned int truesize = igb_rx_pg_size(rx_ring) / 2;
Alexander Duyck74e238e2013-02-02 05:07:11 +00007562#else
Alexander Duycke3cdf682017-02-06 18:27:14 -08007563 unsigned int truesize = ring_uses_build_skb(rx_ring) ?
7564 SKB_DATA_ALIGN(IGB_SKB_PAD + size) :
7565 SKB_DATA_ALIGN(size);
Alexander Duyck74e238e2013-02-02 05:07:11 +00007566#endif
Alexander Duycke0142722017-02-06 18:27:26 -08007567 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, rx_buffer->page,
7568 rx_buffer->page_offset, size, truesize);
7569#if (PAGE_SIZE < 8192)
7570 rx_buffer->page_offset ^= truesize;
7571#else
7572 rx_buffer->page_offset += truesize;
7573#endif
7574}
Alexander Duyckcbc8e552012-09-25 00:31:02 +00007575
Alexander Duycke0142722017-02-06 18:27:26 -08007576static struct sk_buff *igb_construct_skb(struct igb_ring *rx_ring,
7577 struct igb_rx_buffer *rx_buffer,
7578 union e1000_adv_rx_desc *rx_desc,
7579 unsigned int size)
7580{
7581 void *va = page_address(rx_buffer->page) + rx_buffer->page_offset;
7582#if (PAGE_SIZE < 8192)
7583 unsigned int truesize = igb_rx_pg_size(rx_ring) / 2;
7584#else
7585 unsigned int truesize = SKB_DATA_ALIGN(size);
7586#endif
7587 unsigned int headlen;
7588 struct sk_buff *skb;
7589
7590 /* prefetch first cache line of first page */
7591 prefetch(va);
7592#if L1_CACHE_BYTES < 128
7593 prefetch(va + L1_CACHE_BYTES);
7594#endif
7595
7596 /* allocate a skb to store the frags */
7597 skb = napi_alloc_skb(&rx_ring->q_vector->napi, IGB_RX_HDR_LEN);
7598 if (unlikely(!skb))
7599 return NULL;
Alexander Duyckcbc8e552012-09-25 00:31:02 +00007600
Alexander Duyckf56e7bb2015-04-22 21:49:17 -07007601 if (unlikely(igb_test_staterr(rx_desc, E1000_RXDADV_STAT_TSIP))) {
7602 igb_ptp_rx_pktstamp(rx_ring->q_vector, va, skb);
7603 va += IGB_TS_HDR_LEN;
7604 size -= IGB_TS_HDR_LEN;
7605 }
Alexander Duyckcbc8e552012-09-25 00:31:02 +00007606
Alexander Duycke0142722017-02-06 18:27:26 -08007607 /* Determine available headroom for copy */
7608 headlen = size;
7609 if (headlen > IGB_RX_HDR_LEN)
7610 headlen = eth_get_headlen(va, IGB_RX_HDR_LEN);
Alexander Duyckf56e7bb2015-04-22 21:49:17 -07007611
7612 /* align pull length to size of long to optimize memcpy performance */
Alexander Duycke0142722017-02-06 18:27:26 -08007613 memcpy(__skb_put(skb, headlen), va, ALIGN(headlen, sizeof(long)));
Alexander Duyckf56e7bb2015-04-22 21:49:17 -07007614
7615 /* update all of the pointers */
Alexander Duycke0142722017-02-06 18:27:26 -08007616 size -= headlen;
7617 if (size) {
7618 skb_add_rx_frag(skb, 0, rx_buffer->page,
7619 (va + headlen) - page_address(rx_buffer->page),
7620 size, truesize);
7621#if (PAGE_SIZE < 8192)
7622 rx_buffer->page_offset ^= truesize;
7623#else
7624 rx_buffer->page_offset += truesize;
Alexander Duyck2e334ee2012-09-25 00:31:07 +00007625#endif
Alexander Duyck2e334ee2012-09-25 00:31:07 +00007626 } else {
Alexander Duycke0142722017-02-06 18:27:26 -08007627 rx_buffer->pagecnt_bias++;
Alexander Duyck2e334ee2012-09-25 00:31:07 +00007628 }
7629
Alexander Duyck2e334ee2012-09-25 00:31:07 +00007630 return skb;
7631}
7632
Alexander Duyckb1bb2eb2017-02-06 18:27:36 -08007633static struct sk_buff *igb_build_skb(struct igb_ring *rx_ring,
7634 struct igb_rx_buffer *rx_buffer,
7635 union e1000_adv_rx_desc *rx_desc,
7636 unsigned int size)
7637{
7638 void *va = page_address(rx_buffer->page) + rx_buffer->page_offset;
7639#if (PAGE_SIZE < 8192)
7640 unsigned int truesize = igb_rx_pg_size(rx_ring) / 2;
7641#else
7642 unsigned int truesize = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) +
7643 SKB_DATA_ALIGN(IGB_SKB_PAD + size);
7644#endif
7645 struct sk_buff *skb;
7646
7647 /* prefetch first cache line of first page */
7648 prefetch(va);
7649#if L1_CACHE_BYTES < 128
7650 prefetch(va + L1_CACHE_BYTES);
7651#endif
7652
Alexander Duyck3a1eb6d2017-02-15 09:15:59 -08007653 /* build an skb around the page buffer */
Alexander Duyckb1bb2eb2017-02-06 18:27:36 -08007654 skb = build_skb(va - IGB_SKB_PAD, truesize);
7655 if (unlikely(!skb))
7656 return NULL;
7657
7658 /* update pointers within the skb to store the data */
7659 skb_reserve(skb, IGB_SKB_PAD);
7660 __skb_put(skb, size);
7661
7662 /* pull timestamp out of packet data */
7663 if (igb_test_staterr(rx_desc, E1000_RXDADV_STAT_TSIP)) {
7664 igb_ptp_rx_pktstamp(rx_ring->q_vector, skb->data, skb);
7665 __skb_pull(skb, IGB_TS_HDR_LEN);
7666 }
7667
7668 /* update buffer offset */
7669#if (PAGE_SIZE < 8192)
7670 rx_buffer->page_offset ^= truesize;
7671#else
7672 rx_buffer->page_offset += truesize;
7673#endif
7674
7675 return skb;
7676}
7677
Alexander Duyckcd392f52011-08-26 07:43:59 +00007678static inline void igb_rx_checksum(struct igb_ring *ring,
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00007679 union e1000_adv_rx_desc *rx_desc,
7680 struct sk_buff *skb)
Auke Kok9d5c8242008-01-24 02:22:38 -08007681{
Eric Dumazetbc8acf22010-09-02 13:07:41 -07007682 skb_checksum_none_assert(skb);
Auke Kok9d5c8242008-01-24 02:22:38 -08007683
Alexander Duyck294e7d72011-08-26 07:45:57 +00007684 /* Ignore Checksum bit is set */
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00007685 if (igb_test_staterr(rx_desc, E1000_RXD_STAT_IXSM))
Alexander Duyck294e7d72011-08-26 07:45:57 +00007686 return;
7687
7688 /* Rx checksum disabled via ethtool */
7689 if (!(ring->netdev->features & NETIF_F_RXCSUM))
Auke Kok9d5c8242008-01-24 02:22:38 -08007690 return;
Alexander Duyck85ad76b2009-10-27 15:52:46 +00007691
Auke Kok9d5c8242008-01-24 02:22:38 -08007692 /* TCP/UDP checksum error bit is set */
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00007693 if (igb_test_staterr(rx_desc,
7694 E1000_RXDEXT_STATERR_TCPE |
7695 E1000_RXDEXT_STATERR_IPE)) {
Jeff Kirsherb980ac12013-02-23 07:29:56 +00007696 /* work around errata with sctp packets where the TCPE aka
Jesse Brandeburgb9473562009-04-27 22:36:13 +00007697 * L4E bit is set incorrectly on 64 byte (60 byte w/o crc)
7698 * packets, (aka let the stack check the crc32c)
7699 */
Alexander Duyck866cff02011-08-26 07:45:36 +00007700 if (!((skb->len == 60) &&
7701 test_bit(IGB_RING_FLAG_RX_SCTP_CSUM, &ring->flags))) {
Eric Dumazet12dcd862010-10-15 17:27:10 +00007702 u64_stats_update_begin(&ring->rx_syncp);
Alexander Duyck04a5fcaa2009-10-27 15:52:27 +00007703 ring->rx_stats.csum_err++;
Eric Dumazet12dcd862010-10-15 17:27:10 +00007704 u64_stats_update_end(&ring->rx_syncp);
7705 }
Auke Kok9d5c8242008-01-24 02:22:38 -08007706 /* let the stack verify checksum errors */
Auke Kok9d5c8242008-01-24 02:22:38 -08007707 return;
7708 }
7709 /* It must be a TCP or UDP packet with a valid checksum */
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00007710 if (igb_test_staterr(rx_desc, E1000_RXD_STAT_TCPCS |
7711 E1000_RXD_STAT_UDPCS))
Auke Kok9d5c8242008-01-24 02:22:38 -08007712 skb->ip_summed = CHECKSUM_UNNECESSARY;
7713
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00007714 dev_dbg(ring->dev, "cksum success: bits %08X\n",
7715 le32_to_cpu(rx_desc->wb.upper.status_error));
Auke Kok9d5c8242008-01-24 02:22:38 -08007716}
7717
Alexander Duyck077887c2011-08-26 07:46:29 +00007718static inline void igb_rx_hash(struct igb_ring *ring,
7719 union e1000_adv_rx_desc *rx_desc,
7720 struct sk_buff *skb)
7721{
7722 if (ring->netdev->features & NETIF_F_RXHASH)
Tom Herbert42bdf082013-12-18 16:46:58 +00007723 skb_set_hash(skb,
7724 le32_to_cpu(rx_desc->wb.lower.hi_dword.rss),
7725 PKT_HASH_TYPE_L3);
Alexander Duyck077887c2011-08-26 07:46:29 +00007726}
7727
Alexander Duyck1a1c2252012-09-25 00:30:52 +00007728/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00007729 * igb_is_non_eop - process handling of non-EOP buffers
7730 * @rx_ring: Rx ring being processed
7731 * @rx_desc: Rx descriptor for current buffer
7732 * @skb: current socket buffer containing buffer in progress
Alexander Duyck2e334ee2012-09-25 00:31:07 +00007733 *
Jeff Kirsherb980ac12013-02-23 07:29:56 +00007734 * This function updates next to clean. If the buffer is an EOP buffer
7735 * this function exits returning false, otherwise it will place the
7736 * sk_buff in the next buffer to be chained and return true indicating
7737 * that this is in fact a non-EOP buffer.
Alexander Duyck2e334ee2012-09-25 00:31:07 +00007738 **/
7739static bool igb_is_non_eop(struct igb_ring *rx_ring,
7740 union e1000_adv_rx_desc *rx_desc)
7741{
7742 u32 ntc = rx_ring->next_to_clean + 1;
7743
7744 /* fetch, update, and store next to clean */
7745 ntc = (ntc < rx_ring->count) ? ntc : 0;
7746 rx_ring->next_to_clean = ntc;
7747
7748 prefetch(IGB_RX_DESC(rx_ring, ntc));
7749
7750 if (likely(igb_test_staterr(rx_desc, E1000_RXD_STAT_EOP)))
7751 return false;
7752
7753 return true;
7754}
7755
7756/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00007757 * igb_cleanup_headers - Correct corrupted or empty headers
7758 * @rx_ring: rx descriptor ring packet is being transacted on
7759 * @rx_desc: pointer to the EOP Rx descriptor
7760 * @skb: pointer to current skb being fixed
Alexander Duyck1a1c2252012-09-25 00:30:52 +00007761 *
Jeff Kirsherb980ac12013-02-23 07:29:56 +00007762 * Address the case where we are pulling data in on pages only
7763 * and as such no data is present in the skb header.
Alexander Duyck1a1c2252012-09-25 00:30:52 +00007764 *
Jeff Kirsherb980ac12013-02-23 07:29:56 +00007765 * In addition if skb is not at least 60 bytes we need to pad it so that
7766 * it is large enough to qualify as a valid Ethernet frame.
Alexander Duyck1a1c2252012-09-25 00:30:52 +00007767 *
Jeff Kirsherb980ac12013-02-23 07:29:56 +00007768 * Returns true if an error was encountered and skb was freed.
Alexander Duyck1a1c2252012-09-25 00:30:52 +00007769 **/
7770static bool igb_cleanup_headers(struct igb_ring *rx_ring,
7771 union e1000_adv_rx_desc *rx_desc,
7772 struct sk_buff *skb)
7773{
Alexander Duyck1a1c2252012-09-25 00:30:52 +00007774 if (unlikely((igb_test_staterr(rx_desc,
7775 E1000_RXDEXT_ERR_FRAME_ERR_MASK)))) {
7776 struct net_device *netdev = rx_ring->netdev;
7777 if (!(netdev->features & NETIF_F_RXALL)) {
7778 dev_kfree_skb_any(skb);
7779 return true;
7780 }
7781 }
7782
Alexander Duycka94d9e22014-12-03 08:17:39 -08007783 /* if eth_skb_pad returns an error the skb was freed */
7784 if (eth_skb_pad(skb))
7785 return true;
Alexander Duyck1a1c2252012-09-25 00:30:52 +00007786
7787 return false;
Alexander Duyck2d94d8a2009-07-23 18:10:06 +00007788}
7789
Alexander Duyckdb2ee5b2012-09-25 00:30:57 +00007790/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00007791 * igb_process_skb_fields - Populate skb header fields from Rx descriptor
7792 * @rx_ring: rx descriptor ring packet is being transacted on
7793 * @rx_desc: pointer to the EOP Rx descriptor
7794 * @skb: pointer to current skb being populated
Alexander Duyckdb2ee5b2012-09-25 00:30:57 +00007795 *
Jeff Kirsherb980ac12013-02-23 07:29:56 +00007796 * This function checks the ring, descriptor, and packet information in
7797 * order to populate the hash, checksum, VLAN, timestamp, protocol, and
7798 * other fields within the skb.
Alexander Duyckdb2ee5b2012-09-25 00:30:57 +00007799 **/
7800static void igb_process_skb_fields(struct igb_ring *rx_ring,
7801 union e1000_adv_rx_desc *rx_desc,
7802 struct sk_buff *skb)
7803{
7804 struct net_device *dev = rx_ring->netdev;
7805
7806 igb_rx_hash(rx_ring, rx_desc, skb);
7807
7808 igb_rx_checksum(rx_ring, rx_desc, skb);
7809
Jakub Kicinski5499a962014-04-02 10:33:33 +00007810 if (igb_test_staterr(rx_desc, E1000_RXDADV_STAT_TS) &&
7811 !igb_test_staterr(rx_desc, E1000_RXDADV_STAT_TSIP))
7812 igb_ptp_rx_rgtstamp(rx_ring->q_vector, skb);
Alexander Duyckdb2ee5b2012-09-25 00:30:57 +00007813
Patrick McHardyf6469682013-04-19 02:04:27 +00007814 if ((dev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
Alexander Duyckdb2ee5b2012-09-25 00:30:57 +00007815 igb_test_staterr(rx_desc, E1000_RXD_STAT_VP)) {
7816 u16 vid;
Carolyn Wyborny9005df32014-04-11 01:45:34 +00007817
Alexander Duyckdb2ee5b2012-09-25 00:30:57 +00007818 if (igb_test_staterr(rx_desc, E1000_RXDEXT_STATERR_LB) &&
7819 test_bit(IGB_RING_FLAG_RX_LB_VLAN_BSWAP, &rx_ring->flags))
7820 vid = be16_to_cpu(rx_desc->wb.upper.vlan);
7821 else
7822 vid = le16_to_cpu(rx_desc->wb.upper.vlan);
7823
Patrick McHardy86a9bad2013-04-19 02:04:30 +00007824 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vid);
Alexander Duyckdb2ee5b2012-09-25 00:30:57 +00007825 }
7826
7827 skb_record_rx_queue(skb, rx_ring->queue_index);
7828
7829 skb->protocol = eth_type_trans(skb, rx_ring->netdev);
7830}
7831
Alexander Duycke0142722017-02-06 18:27:26 -08007832static struct igb_rx_buffer *igb_get_rx_buffer(struct igb_ring *rx_ring,
7833 const unsigned int size)
7834{
7835 struct igb_rx_buffer *rx_buffer;
7836
7837 rx_buffer = &rx_ring->rx_buffer_info[rx_ring->next_to_clean];
7838 prefetchw(rx_buffer->page);
7839
7840 /* we are reusing so sync this buffer for CPU use */
7841 dma_sync_single_range_for_cpu(rx_ring->dev,
7842 rx_buffer->dma,
7843 rx_buffer->page_offset,
7844 size,
7845 DMA_FROM_DEVICE);
7846
7847 rx_buffer->pagecnt_bias--;
7848
7849 return rx_buffer;
7850}
7851
7852static void igb_put_rx_buffer(struct igb_ring *rx_ring,
7853 struct igb_rx_buffer *rx_buffer)
7854{
7855 if (igb_can_reuse_rx_page(rx_buffer)) {
7856 /* hand second half of page back to the ring */
7857 igb_reuse_rx_page(rx_ring, rx_buffer);
7858 } else {
7859 /* We are not reusing the buffer so unmap it and free
7860 * any references we are holding to it
7861 */
7862 dma_unmap_page_attrs(rx_ring->dev, rx_buffer->dma,
7863 igb_rx_pg_size(rx_ring), DMA_FROM_DEVICE,
7864 IGB_RX_DMA_ATTR);
7865 __page_frag_cache_drain(rx_buffer->page,
7866 rx_buffer->pagecnt_bias);
7867 }
7868
7869 /* clear contents of rx_buffer */
7870 rx_buffer->page = NULL;
7871}
7872
Jesse Brandeburg32b3e082015-09-24 16:35:47 -07007873static int igb_clean_rx_irq(struct igb_q_vector *q_vector, const int budget)
Auke Kok9d5c8242008-01-24 02:22:38 -08007874{
Alexander Duyck0ba82992011-08-26 07:45:47 +00007875 struct igb_ring *rx_ring = q_vector->rx.ring;
Alexander Duyck1a1c2252012-09-25 00:30:52 +00007876 struct sk_buff *skb = rx_ring->skb;
Auke Kok9d5c8242008-01-24 02:22:38 -08007877 unsigned int total_bytes = 0, total_packets = 0;
Alexander Duyck16eb8812011-08-26 07:43:54 +00007878 u16 cleaned_count = igb_desc_unused(rx_ring);
Auke Kok9d5c8242008-01-24 02:22:38 -08007879
Eric W. Biederman57ba34c2014-03-14 18:00:06 -07007880 while (likely(total_packets < budget)) {
Alexander Duyck2e334ee2012-09-25 00:31:07 +00007881 union e1000_adv_rx_desc *rx_desc;
Alexander Duycke0142722017-02-06 18:27:26 -08007882 struct igb_rx_buffer *rx_buffer;
7883 unsigned int size;
Auke Kok9d5c8242008-01-24 02:22:38 -08007884
Alexander Duyck2e334ee2012-09-25 00:31:07 +00007885 /* return some buffers to hardware, one at a time is too slow */
7886 if (cleaned_count >= IGB_RX_BUFFER_WRITE) {
7887 igb_alloc_rx_buffers(rx_ring, cleaned_count);
7888 cleaned_count = 0;
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07007889 }
7890
Alexander Duyck2e334ee2012-09-25 00:31:07 +00007891 rx_desc = IGB_RX_DESC(rx_ring, rx_ring->next_to_clean);
Alexander Duycke0142722017-02-06 18:27:26 -08007892 size = le16_to_cpu(rx_desc->wb.upper.length);
7893 if (!size)
Alexander Duyck2e334ee2012-09-25 00:31:07 +00007894 break;
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07007895
Alexander Duyck74e238e2013-02-02 05:07:11 +00007896 /* This memory barrier is needed to keep us from reading
7897 * any other fields out of the rx_desc until we know the
Alexander Duyck124b74c2014-12-11 15:02:28 -08007898 * descriptor has been written back
Alexander Duyck74e238e2013-02-02 05:07:11 +00007899 */
Alexander Duyck124b74c2014-12-11 15:02:28 -08007900 dma_rmb();
Alexander Duyck74e238e2013-02-02 05:07:11 +00007901
Alexander Duycke0142722017-02-06 18:27:26 -08007902 rx_buffer = igb_get_rx_buffer(rx_ring, size);
7903
Alexander Duyck2e334ee2012-09-25 00:31:07 +00007904 /* retrieve a buffer from the ring */
Alexander Duycke0142722017-02-06 18:27:26 -08007905 if (skb)
7906 igb_add_rx_frag(rx_ring, rx_buffer, skb, size);
Alexander Duyckb1bb2eb2017-02-06 18:27:36 -08007907 else if (ring_uses_build_skb(rx_ring))
7908 skb = igb_build_skb(rx_ring, rx_buffer, rx_desc, size);
Alexander Duycke0142722017-02-06 18:27:26 -08007909 else
7910 skb = igb_construct_skb(rx_ring, rx_buffer,
7911 rx_desc, size);
Alexander Duyck16eb8812011-08-26 07:43:54 +00007912
Alexander Duyck2e334ee2012-09-25 00:31:07 +00007913 /* exit if we failed to retrieve a buffer */
Alexander Duycke0142722017-02-06 18:27:26 -08007914 if (!skb) {
7915 rx_ring->rx_stats.alloc_failed++;
7916 rx_buffer->pagecnt_bias++;
Alexander Duyck2e334ee2012-09-25 00:31:07 +00007917 break;
Alexander Duycke0142722017-02-06 18:27:26 -08007918 }
Alexander Duyck2e334ee2012-09-25 00:31:07 +00007919
Alexander Duycke0142722017-02-06 18:27:26 -08007920 igb_put_rx_buffer(rx_ring, rx_buffer);
Alexander Duyck2e334ee2012-09-25 00:31:07 +00007921 cleaned_count++;
7922
7923 /* fetch next buffer in frame if non-eop */
7924 if (igb_is_non_eop(rx_ring, rx_desc))
7925 continue;
Alexander Duyck44390ca2011-08-26 07:43:38 +00007926
Alexander Duyck1a1c2252012-09-25 00:30:52 +00007927 /* verify the packet layout is correct */
7928 if (igb_cleanup_headers(rx_ring, rx_desc, skb)) {
7929 skb = NULL;
7930 continue;
Auke Kok9d5c8242008-01-24 02:22:38 -08007931 }
Auke Kok9d5c8242008-01-24 02:22:38 -08007932
Alexander Duyckdb2ee5b2012-09-25 00:30:57 +00007933 /* probably a little skewed due to removing CRC */
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00007934 total_bytes += skb->len;
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00007935
Alexander Duyckdb2ee5b2012-09-25 00:30:57 +00007936 /* populate checksum, timestamp, VLAN, and protocol */
7937 igb_process_skb_fields(rx_ring, rx_desc, skb);
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00007938
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00007939 napi_gro_receive(&q_vector->napi, skb);
Auke Kok9d5c8242008-01-24 02:22:38 -08007940
Alexander Duyck1a1c2252012-09-25 00:30:52 +00007941 /* reset skb pointer */
7942 skb = NULL;
7943
Alexander Duyck2e334ee2012-09-25 00:31:07 +00007944 /* update budget accounting */
7945 total_packets++;
Eric W. Biederman57ba34c2014-03-14 18:00:06 -07007946 }
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07007947
Alexander Duyck1a1c2252012-09-25 00:30:52 +00007948 /* place incomplete frames back on ring for completion */
7949 rx_ring->skb = skb;
7950
Eric Dumazet12dcd862010-10-15 17:27:10 +00007951 u64_stats_update_begin(&rx_ring->rx_syncp);
Auke Kok9d5c8242008-01-24 02:22:38 -08007952 rx_ring->rx_stats.packets += total_packets;
7953 rx_ring->rx_stats.bytes += total_bytes;
Eric Dumazet12dcd862010-10-15 17:27:10 +00007954 u64_stats_update_end(&rx_ring->rx_syncp);
Alexander Duyck0ba82992011-08-26 07:45:47 +00007955 q_vector->rx.total_packets += total_packets;
7956 q_vector->rx.total_bytes += total_bytes;
Alexander Duyckc023cd82011-08-26 07:43:43 +00007957
7958 if (cleaned_count)
Alexander Duyckcd392f52011-08-26 07:43:59 +00007959 igb_alloc_rx_buffers(rx_ring, cleaned_count);
Alexander Duyckc023cd82011-08-26 07:43:43 +00007960
Jesse Brandeburg32b3e082015-09-24 16:35:47 -07007961 return total_packets;
Auke Kok9d5c8242008-01-24 02:22:38 -08007962}
7963
Alexander Duycke3cdf682017-02-06 18:27:14 -08007964static inline unsigned int igb_rx_offset(struct igb_ring *rx_ring)
7965{
7966 return ring_uses_build_skb(rx_ring) ? IGB_SKB_PAD : 0;
7967}
7968
Alexander Duyck1a1c2252012-09-25 00:30:52 +00007969static bool igb_alloc_mapped_page(struct igb_ring *rx_ring,
7970 struct igb_rx_buffer *bi)
Alexander Duyckc023cd82011-08-26 07:43:43 +00007971{
Alexander Duyck1a1c2252012-09-25 00:30:52 +00007972 struct page *page = bi->page;
Alexander Duyckcbc8e552012-09-25 00:31:02 +00007973 dma_addr_t dma;
Alexander Duyckc023cd82011-08-26 07:43:43 +00007974
Alexander Duyckcbc8e552012-09-25 00:31:02 +00007975 /* since we are recycling buffers we should seldom need to alloc */
7976 if (likely(page))
Alexander Duyckc023cd82011-08-26 07:43:43 +00007977 return true;
7978
Alexander Duyckcbc8e552012-09-25 00:31:02 +00007979 /* alloc new page for storage */
Alexander Duyck8649aae2017-02-06 18:27:03 -08007980 page = dev_alloc_pages(igb_rx_pg_order(rx_ring));
Alexander Duyckcbc8e552012-09-25 00:31:02 +00007981 if (unlikely(!page)) {
7982 rx_ring->rx_stats.alloc_failed++;
7983 return false;
Alexander Duyckc023cd82011-08-26 07:43:43 +00007984 }
7985
Alexander Duyckcbc8e552012-09-25 00:31:02 +00007986 /* map page for use */
Alexander Duyck8649aae2017-02-06 18:27:03 -08007987 dma = dma_map_page_attrs(rx_ring->dev, page, 0,
7988 igb_rx_pg_size(rx_ring),
7989 DMA_FROM_DEVICE,
7990 IGB_RX_DMA_ATTR);
Alexander Duyckc023cd82011-08-26 07:43:43 +00007991
Jeff Kirsherb980ac12013-02-23 07:29:56 +00007992 /* if mapping failed free memory back to system since
Alexander Duyckcbc8e552012-09-25 00:31:02 +00007993 * there isn't much point in holding memory we can't use
7994 */
Alexander Duyckc023cd82011-08-26 07:43:43 +00007995 if (dma_mapping_error(rx_ring->dev, dma)) {
Alexander Duyck8649aae2017-02-06 18:27:03 -08007996 __free_pages(page, igb_rx_pg_order(rx_ring));
Alexander Duyckcbc8e552012-09-25 00:31:02 +00007997
Alexander Duyckc023cd82011-08-26 07:43:43 +00007998 rx_ring->rx_stats.alloc_failed++;
7999 return false;
8000 }
8001
8002 bi->dma = dma;
Alexander Duyckcbc8e552012-09-25 00:31:02 +00008003 bi->page = page;
Alexander Duycke3cdf682017-02-06 18:27:14 -08008004 bi->page_offset = igb_rx_offset(rx_ring);
Alexander Duyckbd4171a2016-12-14 15:05:34 -08008005 bi->pagecnt_bias = 1;
Alexander Duyck1a1c2252012-09-25 00:30:52 +00008006
Alexander Duyckc023cd82011-08-26 07:43:43 +00008007 return true;
8008}
8009
Auke Kok9d5c8242008-01-24 02:22:38 -08008010/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00008011 * igb_alloc_rx_buffers - Replace used receive buffers; packet split
8012 * @adapter: address of board private structure
Auke Kok9d5c8242008-01-24 02:22:38 -08008013 **/
Alexander Duyckcd392f52011-08-26 07:43:59 +00008014void igb_alloc_rx_buffers(struct igb_ring *rx_ring, u16 cleaned_count)
Auke Kok9d5c8242008-01-24 02:22:38 -08008015{
Auke Kok9d5c8242008-01-24 02:22:38 -08008016 union e1000_adv_rx_desc *rx_desc;
Alexander Duyck06034642011-08-26 07:44:22 +00008017 struct igb_rx_buffer *bi;
Alexander Duyckc023cd82011-08-26 07:43:43 +00008018 u16 i = rx_ring->next_to_use;
Alexander Duyck8649aae2017-02-06 18:27:03 -08008019 u16 bufsz;
Auke Kok9d5c8242008-01-24 02:22:38 -08008020
Alexander Duyckcbc8e552012-09-25 00:31:02 +00008021 /* nothing to do */
8022 if (!cleaned_count)
8023 return;
8024
Alexander Duyck601369062011-08-26 07:44:05 +00008025 rx_desc = IGB_RX_DESC(rx_ring, i);
Alexander Duyck06034642011-08-26 07:44:22 +00008026 bi = &rx_ring->rx_buffer_info[i];
Alexander Duyckc023cd82011-08-26 07:43:43 +00008027 i -= rx_ring->count;
Auke Kok9d5c8242008-01-24 02:22:38 -08008028
Alexander Duyck8649aae2017-02-06 18:27:03 -08008029 bufsz = igb_rx_bufsz(rx_ring);
8030
Alexander Duyckcbc8e552012-09-25 00:31:02 +00008031 do {
Alexander Duyck1a1c2252012-09-25 00:30:52 +00008032 if (!igb_alloc_mapped_page(rx_ring, bi))
Alexander Duyckc023cd82011-08-26 07:43:43 +00008033 break;
Auke Kok9d5c8242008-01-24 02:22:38 -08008034
Alexander Duyck5be59552016-12-14 15:05:30 -08008035 /* sync the buffer for use by the device */
8036 dma_sync_single_range_for_device(rx_ring->dev, bi->dma,
Alexander Duyck8649aae2017-02-06 18:27:03 -08008037 bi->page_offset, bufsz,
Alexander Duyck5be59552016-12-14 15:05:30 -08008038 DMA_FROM_DEVICE);
8039
Jeff Kirsherb980ac12013-02-23 07:29:56 +00008040 /* Refresh the desc even if buffer_addrs didn't change
Alexander Duyckcbc8e552012-09-25 00:31:02 +00008041 * because each write-back erases this info.
8042 */
Alexander Duyckf9d40f62013-04-17 20:41:04 +00008043 rx_desc->read.pkt_addr = cpu_to_le64(bi->dma + bi->page_offset);
Auke Kok9d5c8242008-01-24 02:22:38 -08008044
Alexander Duyckc023cd82011-08-26 07:43:43 +00008045 rx_desc++;
8046 bi++;
Auke Kok9d5c8242008-01-24 02:22:38 -08008047 i++;
Alexander Duyckc023cd82011-08-26 07:43:43 +00008048 if (unlikely(!i)) {
Alexander Duyck601369062011-08-26 07:44:05 +00008049 rx_desc = IGB_RX_DESC(rx_ring, 0);
Alexander Duyck06034642011-08-26 07:44:22 +00008050 bi = rx_ring->rx_buffer_info;
Alexander Duyckc023cd82011-08-26 07:43:43 +00008051 i -= rx_ring->count;
8052 }
8053
Alexander Duyck7ec01162017-02-06 18:25:41 -08008054 /* clear the length for the next_to_use descriptor */
8055 rx_desc->wb.upper.length = 0;
Alexander Duyckcbc8e552012-09-25 00:31:02 +00008056
8057 cleaned_count--;
8058 } while (cleaned_count);
Auke Kok9d5c8242008-01-24 02:22:38 -08008059
Alexander Duyckc023cd82011-08-26 07:43:43 +00008060 i += rx_ring->count;
8061
Auke Kok9d5c8242008-01-24 02:22:38 -08008062 if (rx_ring->next_to_use != i) {
Alexander Duyckcbc8e552012-09-25 00:31:02 +00008063 /* record the next descriptor to use */
Auke Kok9d5c8242008-01-24 02:22:38 -08008064 rx_ring->next_to_use = i;
Auke Kok9d5c8242008-01-24 02:22:38 -08008065
Alexander Duyckcbc8e552012-09-25 00:31:02 +00008066 /* update next to alloc since we have filled the ring */
8067 rx_ring->next_to_alloc = i;
8068
Jeff Kirsherb980ac12013-02-23 07:29:56 +00008069 /* Force memory writes to complete before letting h/w
Auke Kok9d5c8242008-01-24 02:22:38 -08008070 * know there are new descriptors to fetch. (Only
8071 * applicable for weak-ordered memory model archs,
Alexander Duyckcbc8e552012-09-25 00:31:02 +00008072 * such as IA-64).
8073 */
Auke Kok9d5c8242008-01-24 02:22:38 -08008074 wmb();
Alexander Duyckfce99e32009-10-27 15:51:27 +00008075 writel(i, rx_ring->tail);
Auke Kok9d5c8242008-01-24 02:22:38 -08008076 }
8077}
8078
8079/**
8080 * igb_mii_ioctl -
8081 * @netdev:
8082 * @ifreq:
8083 * @cmd:
8084 **/
8085static int igb_mii_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
8086{
8087 struct igb_adapter *adapter = netdev_priv(netdev);
8088 struct mii_ioctl_data *data = if_mii(ifr);
8089
8090 if (adapter->hw.phy.media_type != e1000_media_type_copper)
8091 return -EOPNOTSUPP;
8092
8093 switch (cmd) {
8094 case SIOCGMIIPHY:
8095 data->phy_id = adapter->hw.phy.addr;
8096 break;
8097 case SIOCGMIIREG:
Alexander Duyckf5f4cf02008-11-21 21:30:24 -08008098 if (igb_read_phy_reg(&adapter->hw, data->reg_num & 0x1F,
Carolyn Wyborny9005df32014-04-11 01:45:34 +00008099 &data->val_out))
Auke Kok9d5c8242008-01-24 02:22:38 -08008100 return -EIO;
8101 break;
8102 case SIOCSMIIREG:
8103 default:
8104 return -EOPNOTSUPP;
8105 }
8106 return 0;
8107}
8108
8109/**
8110 * igb_ioctl -
8111 * @netdev:
8112 * @ifreq:
8113 * @cmd:
8114 **/
8115static int igb_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
8116{
8117 switch (cmd) {
8118 case SIOCGMIIPHY:
8119 case SIOCGMIIREG:
8120 case SIOCSMIIREG:
8121 return igb_mii_ioctl(netdev, ifr, cmd);
Jacob Keller6ab5f7b2014-01-11 07:20:06 +00008122 case SIOCGHWTSTAMP:
8123 return igb_ptp_get_ts_config(netdev, ifr);
Patrick Ohlyc6cb0902009-02-12 05:03:42 +00008124 case SIOCSHWTSTAMP:
Jacob Keller6ab5f7b2014-01-11 07:20:06 +00008125 return igb_ptp_set_ts_config(netdev, ifr);
Auke Kok9d5c8242008-01-24 02:22:38 -08008126 default:
8127 return -EOPNOTSUPP;
8128 }
8129}
8130
Todd Fujinaka94826482014-07-10 01:47:15 -07008131void igb_read_pci_cfg(struct e1000_hw *hw, u32 reg, u16 *value)
8132{
8133 struct igb_adapter *adapter = hw->back;
8134
8135 pci_read_config_word(adapter->pdev, reg, value);
8136}
8137
8138void igb_write_pci_cfg(struct e1000_hw *hw, u32 reg, u16 *value)
8139{
8140 struct igb_adapter *adapter = hw->back;
8141
8142 pci_write_config_word(adapter->pdev, reg, *value);
8143}
8144
Alexander Duyck009bc062009-07-23 18:08:35 +00008145s32 igb_read_pcie_cap_reg(struct e1000_hw *hw, u32 reg, u16 *value)
8146{
8147 struct igb_adapter *adapter = hw->back;
Alexander Duyck009bc062009-07-23 18:08:35 +00008148
Jiang Liu23d028c2012-08-20 13:32:20 -06008149 if (pcie_capability_read_word(adapter->pdev, reg, value))
Alexander Duyck009bc062009-07-23 18:08:35 +00008150 return -E1000_ERR_CONFIG;
8151
Alexander Duyck009bc062009-07-23 18:08:35 +00008152 return 0;
8153}
8154
8155s32 igb_write_pcie_cap_reg(struct e1000_hw *hw, u32 reg, u16 *value)
8156{
8157 struct igb_adapter *adapter = hw->back;
Alexander Duyck009bc062009-07-23 18:08:35 +00008158
Jiang Liu23d028c2012-08-20 13:32:20 -06008159 if (pcie_capability_write_word(adapter->pdev, reg, *value))
Alexander Duyck009bc062009-07-23 18:08:35 +00008160 return -E1000_ERR_CONFIG;
8161
Alexander Duyck009bc062009-07-23 18:08:35 +00008162 return 0;
8163}
8164
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008165static void igb_vlan_mode(struct net_device *netdev, netdev_features_t features)
Auke Kok9d5c8242008-01-24 02:22:38 -08008166{
8167 struct igb_adapter *adapter = netdev_priv(netdev);
8168 struct e1000_hw *hw = &adapter->hw;
8169 u32 ctrl, rctl;
Patrick McHardyf6469682013-04-19 02:04:27 +00008170 bool enable = !!(features & NETIF_F_HW_VLAN_CTAG_RX);
Auke Kok9d5c8242008-01-24 02:22:38 -08008171
Alexander Duyck5faf0302011-08-26 07:46:08 +00008172 if (enable) {
Auke Kok9d5c8242008-01-24 02:22:38 -08008173 /* enable VLAN tag insert/strip */
8174 ctrl = rd32(E1000_CTRL);
8175 ctrl |= E1000_CTRL_VME;
8176 wr32(E1000_CTRL, ctrl);
8177
Alexander Duyck51466232009-10-27 23:47:35 +00008178 /* Disable CFI check */
Auke Kok9d5c8242008-01-24 02:22:38 -08008179 rctl = rd32(E1000_RCTL);
Auke Kok9d5c8242008-01-24 02:22:38 -08008180 rctl &= ~E1000_RCTL_CFIEN;
8181 wr32(E1000_RCTL, rctl);
Auke Kok9d5c8242008-01-24 02:22:38 -08008182 } else {
8183 /* disable VLAN tag insert/strip */
8184 ctrl = rd32(E1000_CTRL);
8185 ctrl &= ~E1000_CTRL_VME;
8186 wr32(E1000_CTRL, ctrl);
Auke Kok9d5c8242008-01-24 02:22:38 -08008187 }
8188
Corinna Vinschen030f9f52016-01-28 13:53:23 +01008189 igb_set_vf_vlan_strip(adapter, adapter->vfs_allocated_count, enable);
Auke Kok9d5c8242008-01-24 02:22:38 -08008190}
8191
Patrick McHardy80d5c362013-04-19 02:04:28 +00008192static int igb_vlan_rx_add_vid(struct net_device *netdev,
8193 __be16 proto, u16 vid)
Auke Kok9d5c8242008-01-24 02:22:38 -08008194{
8195 struct igb_adapter *adapter = netdev_priv(netdev);
8196 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08008197 int pf_id = adapter->vfs_allocated_count;
Auke Kok9d5c8242008-01-24 02:22:38 -08008198
Alexander Duyck51466232009-10-27 23:47:35 +00008199 /* add the filter since PF can receive vlans w/o entry in vlvf */
Alexander Duyck16903ca2016-01-06 23:11:18 -08008200 if (!vid || !(adapter->flags & IGB_FLAG_VLAN_PROMISC))
8201 igb_vfta_set(hw, vid, pf_id, true, !!vid);
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00008202
8203 set_bit(vid, adapter->active_vlans);
Jiri Pirko8e586132011-12-08 19:52:37 -05008204
8205 return 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08008206}
8207
Patrick McHardy80d5c362013-04-19 02:04:28 +00008208static int igb_vlan_rx_kill_vid(struct net_device *netdev,
8209 __be16 proto, u16 vid)
Auke Kok9d5c8242008-01-24 02:22:38 -08008210{
8211 struct igb_adapter *adapter = netdev_priv(netdev);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08008212 int pf_id = adapter->vfs_allocated_count;
Alexander Duyck8b77c6b2016-01-06 23:11:04 -08008213 struct e1000_hw *hw = &adapter->hw;
Auke Kok9d5c8242008-01-24 02:22:38 -08008214
Alexander Duyck8b77c6b2016-01-06 23:11:04 -08008215 /* remove VID from filter table */
Alexander Duyck16903ca2016-01-06 23:11:18 -08008216 if (vid && !(adapter->flags & IGB_FLAG_VLAN_PROMISC))
8217 igb_vfta_set(hw, vid, pf_id, false, true);
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00008218
8219 clear_bit(vid, adapter->active_vlans);
Jiri Pirko8e586132011-12-08 19:52:37 -05008220
8221 return 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08008222}
8223
8224static void igb_restore_vlan(struct igb_adapter *adapter)
8225{
Alexander Duyck5982a552016-01-06 23:10:54 -08008226 u16 vid = 1;
Auke Kok9d5c8242008-01-24 02:22:38 -08008227
Alexander Duyck5faf0302011-08-26 07:46:08 +00008228 igb_vlan_mode(adapter->netdev, adapter->netdev->features);
Alexander Duyck5982a552016-01-06 23:10:54 -08008229 igb_vlan_rx_add_vid(adapter->netdev, htons(ETH_P_8021Q), 0);
Alexander Duyck5faf0302011-08-26 07:46:08 +00008230
Alexander Duyck5982a552016-01-06 23:10:54 -08008231 for_each_set_bit_from(vid, adapter->active_vlans, VLAN_N_VID)
Patrick McHardy80d5c362013-04-19 02:04:28 +00008232 igb_vlan_rx_add_vid(adapter->netdev, htons(ETH_P_8021Q), vid);
Auke Kok9d5c8242008-01-24 02:22:38 -08008233}
8234
David Decotigny14ad2512011-04-27 18:32:43 +00008235int igb_set_spd_dplx(struct igb_adapter *adapter, u32 spd, u8 dplx)
Auke Kok9d5c8242008-01-24 02:22:38 -08008236{
Alexander Duyck090b1792009-10-27 23:51:55 +00008237 struct pci_dev *pdev = adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08008238 struct e1000_mac_info *mac = &adapter->hw.mac;
8239
8240 mac->autoneg = 0;
8241
David Decotigny14ad2512011-04-27 18:32:43 +00008242 /* Make sure dplx is at most 1 bit and lsb of speed is not set
Jeff Kirsherb980ac12013-02-23 07:29:56 +00008243 * for the switch() below to work
8244 */
David Decotigny14ad2512011-04-27 18:32:43 +00008245 if ((spd & 1) || (dplx & ~1))
8246 goto err_inval;
8247
Akeem G. Abodunrinf502ef72013-04-05 16:49:06 +00008248 /* Fiber NIC's only allow 1000 gbps Full duplex
8249 * and 100Mbps Full duplex for 100baseFx sfp
8250 */
8251 if (adapter->hw.phy.media_type == e1000_media_type_internal_serdes) {
8252 switch (spd + dplx) {
8253 case SPEED_10 + DUPLEX_HALF:
8254 case SPEED_10 + DUPLEX_FULL:
8255 case SPEED_100 + DUPLEX_HALF:
8256 goto err_inval;
8257 default:
8258 break;
8259 }
8260 }
Carolyn Wybornycd2638a2010-10-12 22:27:02 +00008261
David Decotigny14ad2512011-04-27 18:32:43 +00008262 switch (spd + dplx) {
Auke Kok9d5c8242008-01-24 02:22:38 -08008263 case SPEED_10 + DUPLEX_HALF:
8264 mac->forced_speed_duplex = ADVERTISE_10_HALF;
8265 break;
8266 case SPEED_10 + DUPLEX_FULL:
8267 mac->forced_speed_duplex = ADVERTISE_10_FULL;
8268 break;
8269 case SPEED_100 + DUPLEX_HALF:
8270 mac->forced_speed_duplex = ADVERTISE_100_HALF;
8271 break;
8272 case SPEED_100 + DUPLEX_FULL:
8273 mac->forced_speed_duplex = ADVERTISE_100_FULL;
8274 break;
8275 case SPEED_1000 + DUPLEX_FULL:
8276 mac->autoneg = 1;
8277 adapter->hw.phy.autoneg_advertised = ADVERTISE_1000_FULL;
8278 break;
8279 case SPEED_1000 + DUPLEX_HALF: /* not supported */
8280 default:
David Decotigny14ad2512011-04-27 18:32:43 +00008281 goto err_inval;
Auke Kok9d5c8242008-01-24 02:22:38 -08008282 }
Jesse Brandeburg8376dad2012-07-26 02:31:19 +00008283
8284 /* clear MDI, MDI(-X) override is only allowed when autoneg enabled */
8285 adapter->hw.phy.mdix = AUTO_ALL_MODES;
8286
Auke Kok9d5c8242008-01-24 02:22:38 -08008287 return 0;
David Decotigny14ad2512011-04-27 18:32:43 +00008288
8289err_inval:
8290 dev_err(&pdev->dev, "Unsupported Speed/Duplex configuration\n");
8291 return -EINVAL;
Auke Kok9d5c8242008-01-24 02:22:38 -08008292}
8293
Yan, Zheng749ab2c2012-01-04 20:23:37 +00008294static int __igb_shutdown(struct pci_dev *pdev, bool *enable_wake,
8295 bool runtime)
Auke Kok9d5c8242008-01-24 02:22:38 -08008296{
8297 struct net_device *netdev = pci_get_drvdata(pdev);
8298 struct igb_adapter *adapter = netdev_priv(netdev);
8299 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck2d064c02008-07-08 15:10:12 -07008300 u32 ctrl, rctl, status;
Yan, Zheng749ab2c2012-01-04 20:23:37 +00008301 u32 wufc = runtime ? E1000_WUFC_LNKC : adapter->wol;
Auke Kok9d5c8242008-01-24 02:22:38 -08008302#ifdef CONFIG_PM
8303 int retval = 0;
8304#endif
8305
Todd Fujinaka94749332016-11-15 08:54:26 -08008306 rtnl_lock();
Auke Kok9d5c8242008-01-24 02:22:38 -08008307 netif_device_detach(netdev);
8308
Alexander Duycka88f10e2008-07-08 15:13:38 -07008309 if (netif_running(netdev))
Yan, Zheng749ab2c2012-01-04 20:23:37 +00008310 __igb_close(netdev, true);
Alexander Duycka88f10e2008-07-08 15:13:38 -07008311
Jacob Keller8646f7b2016-05-24 13:56:31 -07008312 igb_ptp_suspend(adapter);
8313
Alexander Duyck047e0032009-10-27 15:49:27 +00008314 igb_clear_interrupt_scheme(adapter);
Todd Fujinaka94749332016-11-15 08:54:26 -08008315 rtnl_unlock();
Auke Kok9d5c8242008-01-24 02:22:38 -08008316
8317#ifdef CONFIG_PM
8318 retval = pci_save_state(pdev);
8319 if (retval)
8320 return retval;
8321#endif
8322
8323 status = rd32(E1000_STATUS);
8324 if (status & E1000_STATUS_LU)
8325 wufc &= ~E1000_WUFC_LNKC;
8326
8327 if (wufc) {
8328 igb_setup_rctl(adapter);
Alexander Duyckff41f8d2009-09-03 14:48:56 +00008329 igb_set_rx_mode(netdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08008330
8331 /* turn on all-multi mode if wake on multicast is enabled */
8332 if (wufc & E1000_WUFC_MC) {
8333 rctl = rd32(E1000_RCTL);
8334 rctl |= E1000_RCTL_MPE;
8335 wr32(E1000_RCTL, rctl);
8336 }
8337
8338 ctrl = rd32(E1000_CTRL);
8339 /* advertise wake from D3Cold */
8340 #define E1000_CTRL_ADVD3WUC 0x00100000
8341 /* phy power management enable */
8342 #define E1000_CTRL_EN_PHY_PWR_MGMT 0x00200000
8343 ctrl |= E1000_CTRL_ADVD3WUC;
8344 wr32(E1000_CTRL, ctrl);
8345
Auke Kok9d5c8242008-01-24 02:22:38 -08008346 /* Allow time for pending master requests to run */
Alexander Duyck330a6d62009-10-27 23:51:35 +00008347 igb_disable_pcie_master(hw);
Auke Kok9d5c8242008-01-24 02:22:38 -08008348
8349 wr32(E1000_WUC, E1000_WUC_PME_EN);
8350 wr32(E1000_WUFC, wufc);
Auke Kok9d5c8242008-01-24 02:22:38 -08008351 } else {
8352 wr32(E1000_WUC, 0);
8353 wr32(E1000_WUFC, 0);
Auke Kok9d5c8242008-01-24 02:22:38 -08008354 }
8355
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00008356 *enable_wake = wufc || adapter->en_mng_pt;
8357 if (!*enable_wake)
Nick Nunley88a268c2010-02-17 01:01:59 +00008358 igb_power_down_link(adapter);
8359 else
8360 igb_power_up_link(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08008361
8362 /* Release control of h/w to f/w. If f/w is AMT enabled, this
Jeff Kirsherb980ac12013-02-23 07:29:56 +00008363 * would have already happened in close and is redundant.
8364 */
Auke Kok9d5c8242008-01-24 02:22:38 -08008365 igb_release_hw_control(adapter);
8366
8367 pci_disable_device(pdev);
8368
Auke Kok9d5c8242008-01-24 02:22:38 -08008369 return 0;
8370}
8371
Kim Tatt Chuahb90fa872017-03-27 08:44:35 +08008372static void igb_deliver_wake_packet(struct net_device *netdev)
8373{
8374 struct igb_adapter *adapter = netdev_priv(netdev);
8375 struct e1000_hw *hw = &adapter->hw;
8376 struct sk_buff *skb;
8377 u32 wupl;
8378
8379 wupl = rd32(E1000_WUPL) & E1000_WUPL_MASK;
8380
8381 /* WUPM stores only the first 128 bytes of the wake packet.
8382 * Read the packet only if we have the whole thing.
8383 */
8384 if ((wupl == 0) || (wupl > E1000_WUPM_BYTES))
8385 return;
8386
8387 skb = netdev_alloc_skb_ip_align(netdev, E1000_WUPM_BYTES);
8388 if (!skb)
8389 return;
8390
8391 skb_put(skb, wupl);
8392
8393 /* Ensure reads are 32-bit aligned */
8394 wupl = roundup(wupl, 4);
8395
8396 memcpy_fromio(skb->data, hw->hw_addr + E1000_WUPM_REG(0), wupl);
8397
8398 skb->protocol = eth_type_trans(skb, netdev);
8399 netif_rx(skb);
8400}
8401
Arnd Bergmann000ba1f2017-04-27 21:09:52 +02008402static int __maybe_unused igb_suspend(struct device *dev)
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00008403{
8404 int retval;
8405 bool wake;
Yan, Zheng749ab2c2012-01-04 20:23:37 +00008406 struct pci_dev *pdev = to_pci_dev(dev);
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00008407
Yan, Zheng749ab2c2012-01-04 20:23:37 +00008408 retval = __igb_shutdown(pdev, &wake, 0);
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00008409 if (retval)
8410 return retval;
8411
8412 if (wake) {
8413 pci_prepare_to_sleep(pdev);
8414 } else {
8415 pci_wake_from_d3(pdev, false);
8416 pci_set_power_state(pdev, PCI_D3hot);
8417 }
8418
8419 return 0;
8420}
8421
Arnd Bergmann000ba1f2017-04-27 21:09:52 +02008422static int __maybe_unused igb_resume(struct device *dev)
Auke Kok9d5c8242008-01-24 02:22:38 -08008423{
Yan, Zheng749ab2c2012-01-04 20:23:37 +00008424 struct pci_dev *pdev = to_pci_dev(dev);
Auke Kok9d5c8242008-01-24 02:22:38 -08008425 struct net_device *netdev = pci_get_drvdata(pdev);
8426 struct igb_adapter *adapter = netdev_priv(netdev);
8427 struct e1000_hw *hw = &adapter->hw;
Kim Tatt Chuahb90fa872017-03-27 08:44:35 +08008428 u32 err, val;
Auke Kok9d5c8242008-01-24 02:22:38 -08008429
8430 pci_set_power_state(pdev, PCI_D0);
8431 pci_restore_state(pdev);
Nick Nunleyb94f2d72010-02-17 01:02:19 +00008432 pci_save_state(pdev);
Taku Izumi42bfd33a2008-06-20 12:10:30 +09008433
Carolyn Wyborny17a402a2014-11-21 23:52:54 -08008434 if (!pci_device_is_present(pdev))
8435 return -ENODEV;
Alexander Duyckaed5dec2009-02-06 23:16:04 +00008436 err = pci_enable_device_mem(pdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08008437 if (err) {
8438 dev_err(&pdev->dev,
8439 "igb: Cannot enable PCI device from suspend\n");
8440 return err;
8441 }
8442 pci_set_master(pdev);
8443
8444 pci_enable_wake(pdev, PCI_D3hot, 0);
8445 pci_enable_wake(pdev, PCI_D3cold, 0);
8446
Stefan Assmann53c7d062012-12-04 06:00:12 +00008447 if (igb_init_interrupt_scheme(adapter, true)) {
Alexander Duycka88f10e2008-07-08 15:13:38 -07008448 dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
8449 return -ENOMEM;
Auke Kok9d5c8242008-01-24 02:22:38 -08008450 }
8451
Auke Kok9d5c8242008-01-24 02:22:38 -08008452 igb_reset(adapter);
Alexander Duycka8564f02009-02-06 23:21:10 +00008453
8454 /* let the f/w know that the h/w is now under the control of the
Jeff Kirsherb980ac12013-02-23 07:29:56 +00008455 * driver.
8456 */
Alexander Duycka8564f02009-02-06 23:21:10 +00008457 igb_get_hw_control(adapter);
8458
Kim Tatt Chuahb90fa872017-03-27 08:44:35 +08008459 val = rd32(E1000_WUS);
8460 if (val & WAKE_PKT_WUS)
8461 igb_deliver_wake_packet(netdev);
8462
Auke Kok9d5c8242008-01-24 02:22:38 -08008463 wr32(E1000_WUS, ~0);
8464
Todd Fujinaka94749332016-11-15 08:54:26 -08008465 rtnl_lock();
8466 if (!err && netif_running(netdev))
Yan, Zheng749ab2c2012-01-04 20:23:37 +00008467 err = __igb_open(netdev, true);
Auke Kok9d5c8242008-01-24 02:22:38 -08008468
Todd Fujinaka94749332016-11-15 08:54:26 -08008469 if (!err)
8470 netif_device_attach(netdev);
8471 rtnl_unlock();
8472
8473 return err;
Yan, Zheng749ab2c2012-01-04 20:23:37 +00008474}
8475
Arnd Bergmann000ba1f2017-04-27 21:09:52 +02008476static int __maybe_unused igb_runtime_idle(struct device *dev)
Yan, Zheng749ab2c2012-01-04 20:23:37 +00008477{
8478 struct pci_dev *pdev = to_pci_dev(dev);
8479 struct net_device *netdev = pci_get_drvdata(pdev);
8480 struct igb_adapter *adapter = netdev_priv(netdev);
8481
8482 if (!igb_has_link(adapter))
8483 pm_schedule_suspend(dev, MSEC_PER_SEC * 5);
8484
8485 return -EBUSY;
8486}
8487
Arnd Bergmann000ba1f2017-04-27 21:09:52 +02008488static int __maybe_unused igb_runtime_suspend(struct device *dev)
Yan, Zheng749ab2c2012-01-04 20:23:37 +00008489{
8490 struct pci_dev *pdev = to_pci_dev(dev);
8491 int retval;
8492 bool wake;
8493
8494 retval = __igb_shutdown(pdev, &wake, 1);
8495 if (retval)
8496 return retval;
8497
8498 if (wake) {
8499 pci_prepare_to_sleep(pdev);
8500 } else {
8501 pci_wake_from_d3(pdev, false);
8502 pci_set_power_state(pdev, PCI_D3hot);
8503 }
Auke Kok9d5c8242008-01-24 02:22:38 -08008504
Auke Kok9d5c8242008-01-24 02:22:38 -08008505 return 0;
8506}
Yan, Zheng749ab2c2012-01-04 20:23:37 +00008507
Arnd Bergmann000ba1f2017-04-27 21:09:52 +02008508static int __maybe_unused igb_runtime_resume(struct device *dev)
Yan, Zheng749ab2c2012-01-04 20:23:37 +00008509{
8510 return igb_resume(dev);
8511}
Auke Kok9d5c8242008-01-24 02:22:38 -08008512
8513static void igb_shutdown(struct pci_dev *pdev)
8514{
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00008515 bool wake;
8516
Yan, Zheng749ab2c2012-01-04 20:23:37 +00008517 __igb_shutdown(pdev, &wake, 0);
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00008518
8519 if (system_state == SYSTEM_POWER_OFF) {
8520 pci_wake_from_d3(pdev, wake);
8521 pci_set_power_state(pdev, PCI_D3hot);
8522 }
Auke Kok9d5c8242008-01-24 02:22:38 -08008523}
8524
Greg Rosefa44f2f2013-01-17 01:03:06 -08008525#ifdef CONFIG_PCI_IOV
8526static int igb_sriov_reinit(struct pci_dev *dev)
8527{
8528 struct net_device *netdev = pci_get_drvdata(dev);
8529 struct igb_adapter *adapter = netdev_priv(netdev);
8530 struct pci_dev *pdev = adapter->pdev;
8531
8532 rtnl_lock();
8533
8534 if (netif_running(netdev))
8535 igb_close(netdev);
Stefan Assmann76252722014-07-10 03:29:39 -07008536 else
8537 igb_reset(adapter);
Greg Rosefa44f2f2013-01-17 01:03:06 -08008538
8539 igb_clear_interrupt_scheme(adapter);
8540
8541 igb_init_queue_configuration(adapter);
8542
8543 if (igb_init_interrupt_scheme(adapter, true)) {
Vasily Averinf468adc2015-07-07 18:53:45 +03008544 rtnl_unlock();
Greg Rosefa44f2f2013-01-17 01:03:06 -08008545 dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
8546 return -ENOMEM;
8547 }
8548
8549 if (netif_running(netdev))
8550 igb_open(netdev);
8551
8552 rtnl_unlock();
8553
8554 return 0;
8555}
8556
8557static int igb_pci_disable_sriov(struct pci_dev *dev)
8558{
8559 int err = igb_disable_sriov(dev);
8560
8561 if (!err)
8562 err = igb_sriov_reinit(dev);
8563
8564 return err;
8565}
8566
8567static int igb_pci_enable_sriov(struct pci_dev *dev, int num_vfs)
8568{
8569 int err = igb_enable_sriov(dev, num_vfs);
8570
8571 if (err)
8572 goto out;
8573
8574 err = igb_sriov_reinit(dev);
8575 if (!err)
8576 return num_vfs;
8577
8578out:
8579 return err;
8580}
8581
8582#endif
8583static int igb_pci_sriov_configure(struct pci_dev *dev, int num_vfs)
8584{
8585#ifdef CONFIG_PCI_IOV
8586 if (num_vfs == 0)
8587 return igb_pci_disable_sriov(dev);
8588 else
8589 return igb_pci_enable_sriov(dev, num_vfs);
8590#endif
8591 return 0;
8592}
8593
Auke Kok9d5c8242008-01-24 02:22:38 -08008594#ifdef CONFIG_NET_POLL_CONTROLLER
Jeff Kirsherb980ac12013-02-23 07:29:56 +00008595/* Polling 'interrupt' - used by things like netconsole to send skbs
Auke Kok9d5c8242008-01-24 02:22:38 -08008596 * without having to re-enable interrupts. It's not called while
8597 * the interrupt routine is executing.
8598 */
8599static void igb_netpoll(struct net_device *netdev)
8600{
8601 struct igb_adapter *adapter = netdev_priv(netdev);
Alexander Duyckeebbbdb2009-02-06 23:19:29 +00008602 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck0d1ae7f2011-08-26 07:46:34 +00008603 struct igb_q_vector *q_vector;
Auke Kok9d5c8242008-01-24 02:22:38 -08008604 int i;
Auke Kok9d5c8242008-01-24 02:22:38 -08008605
Alexander Duyck047e0032009-10-27 15:49:27 +00008606 for (i = 0; i < adapter->num_q_vectors; i++) {
Alexander Duyck0d1ae7f2011-08-26 07:46:34 +00008607 q_vector = adapter->q_vector[i];
Carolyn Wybornycd14ef52013-12-10 07:58:34 +00008608 if (adapter->flags & IGB_FLAG_HAS_MSIX)
Alexander Duyck0d1ae7f2011-08-26 07:46:34 +00008609 wr32(E1000_EIMC, q_vector->eims_value);
8610 else
8611 igb_irq_disable(adapter);
Alexander Duyck047e0032009-10-27 15:49:27 +00008612 napi_schedule(&q_vector->napi);
Alexander Duyckeebbbdb2009-02-06 23:19:29 +00008613 }
Auke Kok9d5c8242008-01-24 02:22:38 -08008614}
8615#endif /* CONFIG_NET_POLL_CONTROLLER */
8616
8617/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00008618 * igb_io_error_detected - called when PCI error is detected
8619 * @pdev: Pointer to PCI device
8620 * @state: The current pci connection state
Auke Kok9d5c8242008-01-24 02:22:38 -08008621 *
Jeff Kirsherb980ac12013-02-23 07:29:56 +00008622 * This function is called after a PCI bus error affecting
8623 * this device has been detected.
8624 **/
Auke Kok9d5c8242008-01-24 02:22:38 -08008625static pci_ers_result_t igb_io_error_detected(struct pci_dev *pdev,
8626 pci_channel_state_t state)
8627{
8628 struct net_device *netdev = pci_get_drvdata(pdev);
8629 struct igb_adapter *adapter = netdev_priv(netdev);
8630
8631 netif_device_detach(netdev);
8632
Alexander Duyck59ed6ee2009-06-30 12:46:34 +00008633 if (state == pci_channel_io_perm_failure)
8634 return PCI_ERS_RESULT_DISCONNECT;
8635
Auke Kok9d5c8242008-01-24 02:22:38 -08008636 if (netif_running(netdev))
8637 igb_down(adapter);
8638 pci_disable_device(pdev);
8639
8640 /* Request a slot slot reset. */
8641 return PCI_ERS_RESULT_NEED_RESET;
8642}
8643
8644/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00008645 * igb_io_slot_reset - called after the pci bus has been reset.
8646 * @pdev: Pointer to PCI device
Auke Kok9d5c8242008-01-24 02:22:38 -08008647 *
Jeff Kirsherb980ac12013-02-23 07:29:56 +00008648 * Restart the card from scratch, as if from a cold-boot. Implementation
8649 * resembles the first-half of the igb_resume routine.
8650 **/
Auke Kok9d5c8242008-01-24 02:22:38 -08008651static pci_ers_result_t igb_io_slot_reset(struct pci_dev *pdev)
8652{
8653 struct net_device *netdev = pci_get_drvdata(pdev);
8654 struct igb_adapter *adapter = netdev_priv(netdev);
8655 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck40a914f2008-11-27 00:24:37 -08008656 pci_ers_result_t result;
Taku Izumi42bfd33a2008-06-20 12:10:30 +09008657 int err;
Auke Kok9d5c8242008-01-24 02:22:38 -08008658
Alexander Duyckaed5dec2009-02-06 23:16:04 +00008659 if (pci_enable_device_mem(pdev)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08008660 dev_err(&pdev->dev,
8661 "Cannot re-enable PCI device after reset.\n");
Alexander Duyck40a914f2008-11-27 00:24:37 -08008662 result = PCI_ERS_RESULT_DISCONNECT;
8663 } else {
8664 pci_set_master(pdev);
8665 pci_restore_state(pdev);
Nick Nunleyb94f2d72010-02-17 01:02:19 +00008666 pci_save_state(pdev);
Alexander Duyck40a914f2008-11-27 00:24:37 -08008667
8668 pci_enable_wake(pdev, PCI_D3hot, 0);
8669 pci_enable_wake(pdev, PCI_D3cold, 0);
8670
Guilherme G Piccoli69b97cf2016-11-10 16:46:43 -02008671 /* In case of PCI error, adapter lose its HW address
8672 * so we should re-assign it here.
8673 */
8674 hw->hw_addr = adapter->io_addr;
8675
Alexander Duyck40a914f2008-11-27 00:24:37 -08008676 igb_reset(adapter);
8677 wr32(E1000_WUS, ~0);
8678 result = PCI_ERS_RESULT_RECOVERED;
Auke Kok9d5c8242008-01-24 02:22:38 -08008679 }
Auke Kok9d5c8242008-01-24 02:22:38 -08008680
Jeff Kirsherea943d42008-12-11 20:34:19 -08008681 err = pci_cleanup_aer_uncorrect_error_status(pdev);
8682 if (err) {
Jeff Kirsherb980ac12013-02-23 07:29:56 +00008683 dev_err(&pdev->dev,
8684 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
8685 err);
Jeff Kirsherea943d42008-12-11 20:34:19 -08008686 /* non-fatal, continue */
8687 }
Auke Kok9d5c8242008-01-24 02:22:38 -08008688
Alexander Duyck40a914f2008-11-27 00:24:37 -08008689 return result;
Auke Kok9d5c8242008-01-24 02:22:38 -08008690}
8691
8692/**
Jeff Kirsherb980ac12013-02-23 07:29:56 +00008693 * igb_io_resume - called when traffic can start flowing again.
8694 * @pdev: Pointer to PCI device
Auke Kok9d5c8242008-01-24 02:22:38 -08008695 *
Jeff Kirsherb980ac12013-02-23 07:29:56 +00008696 * This callback is called when the error recovery driver tells us that
8697 * its OK to resume normal operation. Implementation resembles the
8698 * second-half of the igb_resume routine.
Auke Kok9d5c8242008-01-24 02:22:38 -08008699 */
8700static void igb_io_resume(struct pci_dev *pdev)
8701{
8702 struct net_device *netdev = pci_get_drvdata(pdev);
8703 struct igb_adapter *adapter = netdev_priv(netdev);
8704
Auke Kok9d5c8242008-01-24 02:22:38 -08008705 if (netif_running(netdev)) {
8706 if (igb_up(adapter)) {
8707 dev_err(&pdev->dev, "igb_up failed after reset\n");
8708 return;
8709 }
8710 }
8711
8712 netif_device_attach(netdev);
8713
8714 /* let the f/w know that the h/w is now under the control of the
Jeff Kirsherb980ac12013-02-23 07:29:56 +00008715 * driver.
8716 */
Auke Kok9d5c8242008-01-24 02:22:38 -08008717 igb_get_hw_control(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08008718}
8719
Yury Kylulin83c21332017-03-07 11:20:25 +03008720/**
8721 * igb_rar_set_index - Sync RAL[index] and RAH[index] registers with MAC table
8722 * @adapter: Pointer to adapter structure
8723 * @index: Index of the RAR entry which need to be synced with MAC table
8724 **/
8725static void igb_rar_set_index(struct igb_adapter *adapter, u32 index)
Alexander Duyck26ad9172009-10-05 06:32:49 +00008726{
Alexander Duyck26ad9172009-10-05 06:32:49 +00008727 struct e1000_hw *hw = &adapter->hw;
Alexander Duyckc3278582016-01-06 23:10:23 -08008728 u32 rar_low, rar_high;
Yury Kylulin83c21332017-03-07 11:20:25 +03008729 u8 *addr = adapter->mac_table[index].addr;
Alexander Duyck26ad9172009-10-05 06:32:49 +00008730
Alexander Duyck415cd2a2016-03-18 16:06:53 -07008731 /* HW expects these to be in network order when they are plugged
8732 * into the registers which are little endian. In order to guarantee
8733 * that ordering we need to do an leXX_to_cpup here in order to be
8734 * ready for the byteswap that occurs with writel
Alexander Duyck26ad9172009-10-05 06:32:49 +00008735 */
Alexander Duyck415cd2a2016-03-18 16:06:53 -07008736 rar_low = le32_to_cpup((__le32 *)(addr));
8737 rar_high = le16_to_cpup((__le16 *)(addr + 4));
Alexander Duyck26ad9172009-10-05 06:32:49 +00008738
8739 /* Indicate to hardware the Address is Valid. */
Yury Kylulin83c21332017-03-07 11:20:25 +03008740 if (adapter->mac_table[index].state & IGB_MAC_STATE_IN_USE) {
Corinna Vinschen177132d2017-04-10 10:58:14 +02008741 if (is_valid_ether_addr(addr))
8742 rar_high |= E1000_RAH_AV;
Alexander Duyck26ad9172009-10-05 06:32:49 +00008743
Yury Kylulin83c21332017-03-07 11:20:25 +03008744 if (hw->mac.type == e1000_82575)
8745 rar_high |= E1000_RAH_POOL_1 *
8746 adapter->mac_table[index].queue;
8747 else
8748 rar_high |= E1000_RAH_POOL_1 <<
8749 adapter->mac_table[index].queue;
8750 }
Alexander Duyck26ad9172009-10-05 06:32:49 +00008751
8752 wr32(E1000_RAL(index), rar_low);
8753 wrfl();
8754 wr32(E1000_RAH(index), rar_high);
8755 wrfl();
8756}
8757
Alexander Duyck4ae196d2009-02-19 20:40:07 -08008758static int igb_set_vf_mac(struct igb_adapter *adapter,
Jeff Kirsherb980ac12013-02-23 07:29:56 +00008759 int vf, unsigned char *mac_addr)
Alexander Duyck4ae196d2009-02-19 20:40:07 -08008760{
8761 struct e1000_hw *hw = &adapter->hw;
Alexander Duyckff41f8d2009-09-03 14:48:56 +00008762 /* VF MAC addresses start at end of receive addresses and moves
Jeff Kirsherb980ac12013-02-23 07:29:56 +00008763 * towards the first, as a result a collision should not be possible
8764 */
Alexander Duyckff41f8d2009-09-03 14:48:56 +00008765 int rar_entry = hw->mac.rar_entry_count - (vf + 1);
Yury Kylulin83c21332017-03-07 11:20:25 +03008766 unsigned char *vf_mac_addr = adapter->vf_data[vf].vf_mac_addresses;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08008767
Yury Kylulin83c21332017-03-07 11:20:25 +03008768 ether_addr_copy(vf_mac_addr, mac_addr);
8769 ether_addr_copy(adapter->mac_table[rar_entry].addr, mac_addr);
8770 adapter->mac_table[rar_entry].queue = vf;
8771 adapter->mac_table[rar_entry].state |= IGB_MAC_STATE_IN_USE;
8772 igb_rar_set_index(adapter, rar_entry);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08008773
8774 return 0;
8775}
8776
Williams, Mitch A8151d292010-02-10 01:44:24 +00008777static int igb_ndo_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
8778{
8779 struct igb_adapter *adapter = netdev_priv(netdev);
Corinna Vinschen177132d2017-04-10 10:58:14 +02008780
8781 if (vf >= adapter->vfs_allocated_count)
Williams, Mitch A8151d292010-02-10 01:44:24 +00008782 return -EINVAL;
Corinna Vinschen177132d2017-04-10 10:58:14 +02008783
8784 /* Setting the VF MAC to 0 reverts the IGB_VF_FLAG_PF_SET_MAC
8785 * flag and allows to overwrite the MAC via VF netdev. This
8786 * is necessary to allow libvirt a way to restore the original
8787 * MAC after unbinding vfio-pci and reloading igbvf after shutting
8788 * down a VM.
8789 */
8790 if (is_zero_ether_addr(mac)) {
8791 adapter->vf_data[vf].flags &= ~IGB_VF_FLAG_PF_SET_MAC;
8792 dev_info(&adapter->pdev->dev,
8793 "remove administratively set MAC on VF %d\n",
8794 vf);
8795 } else if (is_valid_ether_addr(mac)) {
8796 adapter->vf_data[vf].flags |= IGB_VF_FLAG_PF_SET_MAC;
8797 dev_info(&adapter->pdev->dev, "setting MAC %pM on VF %d\n",
8798 mac, vf);
8799 dev_info(&adapter->pdev->dev,
8800 "Reload the VF driver to make this change effective.");
8801 /* Generate additional warning if PF is down */
8802 if (test_bit(__IGB_DOWN, &adapter->state)) {
8803 dev_warn(&adapter->pdev->dev,
8804 "The VF MAC address has been set, but the PF device is not up.\n");
8805 dev_warn(&adapter->pdev->dev,
8806 "Bring the PF device up before attempting to use the VF device.\n");
8807 }
8808 } else {
8809 return -EINVAL;
Williams, Mitch A8151d292010-02-10 01:44:24 +00008810 }
8811 return igb_set_vf_mac(adapter, vf, mac);
8812}
8813
Lior Levy17dc5662011-02-08 02:28:46 +00008814static int igb_link_mbps(int internal_link_speed)
8815{
8816 switch (internal_link_speed) {
8817 case SPEED_100:
8818 return 100;
8819 case SPEED_1000:
8820 return 1000;
8821 default:
8822 return 0;
8823 }
8824}
8825
8826static void igb_set_vf_rate_limit(struct e1000_hw *hw, int vf, int tx_rate,
8827 int link_speed)
8828{
8829 int rf_dec, rf_int;
8830 u32 bcnrc_val;
8831
8832 if (tx_rate != 0) {
8833 /* Calculate the rate factor values to set */
8834 rf_int = link_speed / tx_rate;
8835 rf_dec = (link_speed - (rf_int * tx_rate));
Jacob Kellera51d8c22016-04-13 16:08:28 -07008836 rf_dec = (rf_dec * BIT(E1000_RTTBCNRC_RF_INT_SHIFT)) /
Jeff Kirsherb980ac12013-02-23 07:29:56 +00008837 tx_rate;
Lior Levy17dc5662011-02-08 02:28:46 +00008838
8839 bcnrc_val = E1000_RTTBCNRC_RS_ENA;
Jeff Kirsherb980ac12013-02-23 07:29:56 +00008840 bcnrc_val |= ((rf_int << E1000_RTTBCNRC_RF_INT_SHIFT) &
8841 E1000_RTTBCNRC_RF_INT_MASK);
Lior Levy17dc5662011-02-08 02:28:46 +00008842 bcnrc_val |= (rf_dec & E1000_RTTBCNRC_RF_DEC_MASK);
8843 } else {
8844 bcnrc_val = 0;
8845 }
8846
8847 wr32(E1000_RTTDQSEL, vf); /* vf X uses queue X */
Jeff Kirsherb980ac12013-02-23 07:29:56 +00008848 /* Set global transmit compensation time to the MMW_SIZE in RTTBCNRM
Lior Levyf00b0da2011-06-04 06:05:03 +00008849 * register. MMW_SIZE=0x014 if 9728-byte jumbo is supported.
8850 */
8851 wr32(E1000_RTTBCNRM, 0x14);
Lior Levy17dc5662011-02-08 02:28:46 +00008852 wr32(E1000_RTTBCNRC, bcnrc_val);
8853}
8854
8855static void igb_check_vf_rate_limit(struct igb_adapter *adapter)
8856{
8857 int actual_link_speed, i;
8858 bool reset_rate = false;
8859
8860 /* VF TX rate limit was not set or not supported */
8861 if ((adapter->vf_rate_link_speed == 0) ||
8862 (adapter->hw.mac.type != e1000_82576))
8863 return;
8864
8865 actual_link_speed = igb_link_mbps(adapter->link_speed);
8866 if (actual_link_speed != adapter->vf_rate_link_speed) {
8867 reset_rate = true;
8868 adapter->vf_rate_link_speed = 0;
8869 dev_info(&adapter->pdev->dev,
Jeff Kirsherb980ac12013-02-23 07:29:56 +00008870 "Link speed has been changed. VF Transmit rate is disabled\n");
Lior Levy17dc5662011-02-08 02:28:46 +00008871 }
8872
8873 for (i = 0; i < adapter->vfs_allocated_count; i++) {
8874 if (reset_rate)
8875 adapter->vf_data[i].tx_rate = 0;
8876
8877 igb_set_vf_rate_limit(&adapter->hw, i,
Jeff Kirsherb980ac12013-02-23 07:29:56 +00008878 adapter->vf_data[i].tx_rate,
8879 actual_link_speed);
Lior Levy17dc5662011-02-08 02:28:46 +00008880 }
8881}
8882
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04008883static int igb_ndo_set_vf_bw(struct net_device *netdev, int vf,
8884 int min_tx_rate, int max_tx_rate)
Williams, Mitch A8151d292010-02-10 01:44:24 +00008885{
Lior Levy17dc5662011-02-08 02:28:46 +00008886 struct igb_adapter *adapter = netdev_priv(netdev);
8887 struct e1000_hw *hw = &adapter->hw;
8888 int actual_link_speed;
8889
8890 if (hw->mac.type != e1000_82576)
8891 return -EOPNOTSUPP;
8892
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04008893 if (min_tx_rate)
8894 return -EINVAL;
8895
Lior Levy17dc5662011-02-08 02:28:46 +00008896 actual_link_speed = igb_link_mbps(adapter->link_speed);
8897 if ((vf >= adapter->vfs_allocated_count) ||
8898 (!(rd32(E1000_STATUS) & E1000_STATUS_LU)) ||
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04008899 (max_tx_rate < 0) ||
8900 (max_tx_rate > actual_link_speed))
Lior Levy17dc5662011-02-08 02:28:46 +00008901 return -EINVAL;
8902
8903 adapter->vf_rate_link_speed = actual_link_speed;
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04008904 adapter->vf_data[vf].tx_rate = (u16)max_tx_rate;
8905 igb_set_vf_rate_limit(hw, vf, max_tx_rate, actual_link_speed);
Lior Levy17dc5662011-02-08 02:28:46 +00008906
8907 return 0;
Williams, Mitch A8151d292010-02-10 01:44:24 +00008908}
8909
Lior Levy70ea4782013-03-03 20:27:48 +00008910static int igb_ndo_set_vf_spoofchk(struct net_device *netdev, int vf,
8911 bool setting)
8912{
8913 struct igb_adapter *adapter = netdev_priv(netdev);
8914 struct e1000_hw *hw = &adapter->hw;
8915 u32 reg_val, reg_offset;
8916
8917 if (!adapter->vfs_allocated_count)
8918 return -EOPNOTSUPP;
8919
8920 if (vf >= adapter->vfs_allocated_count)
8921 return -EINVAL;
8922
8923 reg_offset = (hw->mac.type == e1000_82576) ? E1000_DTXSWC : E1000_TXSWC;
8924 reg_val = rd32(reg_offset);
8925 if (setting)
Jacob Kellera51d8c22016-04-13 16:08:28 -07008926 reg_val |= (BIT(vf) |
8927 BIT(vf + E1000_DTXSWC_VLAN_SPOOF_SHIFT));
Lior Levy70ea4782013-03-03 20:27:48 +00008928 else
Jacob Kellera51d8c22016-04-13 16:08:28 -07008929 reg_val &= ~(BIT(vf) |
8930 BIT(vf + E1000_DTXSWC_VLAN_SPOOF_SHIFT));
Lior Levy70ea4782013-03-03 20:27:48 +00008931 wr32(reg_offset, reg_val);
8932
8933 adapter->vf_data[vf].spoofchk_enabled = setting;
Todd Fujinaka23d87822014-06-04 07:12:15 +00008934 return 0;
Lior Levy70ea4782013-03-03 20:27:48 +00008935}
8936
Williams, Mitch A8151d292010-02-10 01:44:24 +00008937static int igb_ndo_get_vf_config(struct net_device *netdev,
8938 int vf, struct ifla_vf_info *ivi)
8939{
8940 struct igb_adapter *adapter = netdev_priv(netdev);
8941 if (vf >= adapter->vfs_allocated_count)
8942 return -EINVAL;
8943 ivi->vf = vf;
8944 memcpy(&ivi->mac, adapter->vf_data[vf].vf_mac_addresses, ETH_ALEN);
Sucheta Chakrabortyed616682014-05-22 09:59:05 -04008945 ivi->max_tx_rate = adapter->vf_data[vf].tx_rate;
8946 ivi->min_tx_rate = 0;
Williams, Mitch A8151d292010-02-10 01:44:24 +00008947 ivi->vlan = adapter->vf_data[vf].pf_vlan;
8948 ivi->qos = adapter->vf_data[vf].pf_qos;
Lior Levy70ea4782013-03-03 20:27:48 +00008949 ivi->spoofchk = adapter->vf_data[vf].spoofchk_enabled;
Williams, Mitch A8151d292010-02-10 01:44:24 +00008950 return 0;
8951}
8952
Alexander Duyck4ae196d2009-02-19 20:40:07 -08008953static void igb_vmm_control(struct igb_adapter *adapter)
8954{
8955 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck10d8e902009-10-27 15:54:04 +00008956 u32 reg;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08008957
Alexander Duyck52a1dd42010-03-22 14:07:46 +00008958 switch (hw->mac.type) {
8959 case e1000_82575:
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00008960 case e1000_i210:
8961 case e1000_i211:
Carolyn Wybornyceb5f132013-04-18 22:21:30 +00008962 case e1000_i354:
Alexander Duyck52a1dd42010-03-22 14:07:46 +00008963 default:
8964 /* replication is not supported for 82575 */
Alexander Duyck4ae196d2009-02-19 20:40:07 -08008965 return;
Alexander Duyck52a1dd42010-03-22 14:07:46 +00008966 case e1000_82576:
8967 /* notify HW that the MAC is adding vlan tags */
8968 reg = rd32(E1000_DTXCTL);
8969 reg |= E1000_DTXCTL_VLAN_ADDED;
8970 wr32(E1000_DTXCTL, reg);
Carolyn Wybornyb26141d2014-04-17 04:10:13 +00008971 /* Fall through */
Alexander Duyck52a1dd42010-03-22 14:07:46 +00008972 case e1000_82580:
8973 /* enable replication vlan tag stripping */
8974 reg = rd32(E1000_RPLOLR);
8975 reg |= E1000_RPLOLR_STRVLAN;
8976 wr32(E1000_RPLOLR, reg);
Carolyn Wybornyb26141d2014-04-17 04:10:13 +00008977 /* Fall through */
Alexander Duyckd2ba2ed2010-03-22 14:08:06 +00008978 case e1000_i350:
8979 /* none of the above registers are supported by i350 */
Alexander Duyck52a1dd42010-03-22 14:07:46 +00008980 break;
8981 }
Alexander Duyck10d8e902009-10-27 15:54:04 +00008982
Alexander Duyckd4960302009-10-27 15:53:45 +00008983 if (adapter->vfs_allocated_count) {
8984 igb_vmdq_set_loopback_pf(hw, true);
8985 igb_vmdq_set_replication_pf(hw, true);
Greg Rose13800462010-11-06 02:08:26 +00008986 igb_vmdq_set_anti_spoofing_pf(hw, true,
Jeff Kirsherb980ac12013-02-23 07:29:56 +00008987 adapter->vfs_allocated_count);
Alexander Duyckd4960302009-10-27 15:53:45 +00008988 } else {
8989 igb_vmdq_set_loopback_pf(hw, false);
8990 igb_vmdq_set_replication_pf(hw, false);
8991 }
Alexander Duyck4ae196d2009-02-19 20:40:07 -08008992}
8993
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +00008994static void igb_init_dmac(struct igb_adapter *adapter, u32 pba)
8995{
8996 struct e1000_hw *hw = &adapter->hw;
8997 u32 dmac_thr;
8998 u16 hwm;
8999
9000 if (hw->mac.type > e1000_82580) {
9001 if (adapter->flags & IGB_FLAG_DMAC) {
9002 u32 reg;
9003
9004 /* force threshold to 0. */
9005 wr32(E1000_DMCTXTH, 0);
9006
Jeff Kirsherb980ac12013-02-23 07:29:56 +00009007 /* DMA Coalescing high water mark needs to be greater
Matthew Vicke8c626e2011-11-17 08:33:12 +00009008 * than the Rx threshold. Set hwm to PBA - max frame
9009 * size in 16B units, capping it at PBA - 6KB.
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +00009010 */
Alexander Duyck45693bc2016-01-06 23:10:39 -08009011 hwm = 64 * (pba - 6);
Matthew Vicke8c626e2011-11-17 08:33:12 +00009012 reg = rd32(E1000_FCRTC);
9013 reg &= ~E1000_FCRTC_RTH_COAL_MASK;
9014 reg |= ((hwm << E1000_FCRTC_RTH_COAL_SHIFT)
9015 & E1000_FCRTC_RTH_COAL_MASK);
9016 wr32(E1000_FCRTC, reg);
9017
Jeff Kirsherb980ac12013-02-23 07:29:56 +00009018 /* Set the DMA Coalescing Rx threshold to PBA - 2 * max
Matthew Vicke8c626e2011-11-17 08:33:12 +00009019 * frame size, capping it at PBA - 10KB.
9020 */
Alexander Duyck45693bc2016-01-06 23:10:39 -08009021 dmac_thr = pba - 10;
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +00009022 reg = rd32(E1000_DMACR);
9023 reg &= ~E1000_DMACR_DMACTHR_MASK;
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +00009024 reg |= ((dmac_thr << E1000_DMACR_DMACTHR_SHIFT)
9025 & E1000_DMACR_DMACTHR_MASK);
9026
9027 /* transition to L0x or L1 if available..*/
9028 reg |= (E1000_DMACR_DMAC_EN | E1000_DMACR_DMAC_LX_MASK);
9029
9030 /* watchdog timer= +-1000 usec in 32usec intervals */
9031 reg |= (1000 >> 5);
Matthew Vick0c02dd92012-04-14 05:20:32 +00009032
9033 /* Disable BMC-to-OS Watchdog Enable */
Carolyn Wybornyceb5f132013-04-18 22:21:30 +00009034 if (hw->mac.type != e1000_i354)
9035 reg &= ~E1000_DMACR_DC_BMC2OSW_EN;
9036
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +00009037 wr32(E1000_DMACR, reg);
9038
Jeff Kirsherb980ac12013-02-23 07:29:56 +00009039 /* no lower threshold to disable
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +00009040 * coalescing(smart fifb)-UTRESH=0
9041 */
9042 wr32(E1000_DMCRTRH, 0);
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +00009043
9044 reg = (IGB_DMCTLX_DCFLUSH_DIS | 0x4);
9045
9046 wr32(E1000_DMCTLX, reg);
9047
Jeff Kirsherb980ac12013-02-23 07:29:56 +00009048 /* free space in tx packet buffer to wake from
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +00009049 * DMA coal
9050 */
9051 wr32(E1000_DMCTXTH, (IGB_MIN_TXPBSIZE -
9052 (IGB_TX_BUF_4096 + adapter->max_frame_size)) >> 6);
9053
Jeff Kirsherb980ac12013-02-23 07:29:56 +00009054 /* make low power state decision controlled
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +00009055 * by DMA coal
9056 */
9057 reg = rd32(E1000_PCIEMISC);
9058 reg &= ~E1000_PCIEMISC_LX_DECISION;
9059 wr32(E1000_PCIEMISC, reg);
9060 } /* endif adapter->dmac is not disabled */
9061 } else if (hw->mac.type == e1000_82580) {
9062 u32 reg = rd32(E1000_PCIEMISC);
Carolyn Wyborny9005df32014-04-11 01:45:34 +00009063
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +00009064 wr32(E1000_PCIEMISC, reg & ~E1000_PCIEMISC_LX_DECISION);
9065 wr32(E1000_DMACR, 0);
9066 }
9067}
9068
Jeff Kirsherb980ac12013-02-23 07:29:56 +00009069/**
9070 * igb_read_i2c_byte - Reads 8 bit word over I2C
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +00009071 * @hw: pointer to hardware structure
9072 * @byte_offset: byte offset to read
9073 * @dev_addr: device address
9074 * @data: value read
9075 *
9076 * Performs byte read operation over I2C interface at
9077 * a specified device address.
Jeff Kirsherb980ac12013-02-23 07:29:56 +00009078 **/
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +00009079s32 igb_read_i2c_byte(struct e1000_hw *hw, u8 byte_offset,
Jeff Kirsherb980ac12013-02-23 07:29:56 +00009080 u8 dev_addr, u8 *data)
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +00009081{
9082 struct igb_adapter *adapter = container_of(hw, struct igb_adapter, hw);
Carolyn Wyborny603e86f2013-02-20 07:40:55 +00009083 struct i2c_client *this_client = adapter->i2c_client;
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +00009084 s32 status;
9085 u16 swfw_mask = 0;
9086
9087 if (!this_client)
9088 return E1000_ERR_I2C;
9089
9090 swfw_mask = E1000_SWFW_PHY0_SM;
9091
Todd Fujinaka23d87822014-06-04 07:12:15 +00009092 if (hw->mac.ops.acquire_swfw_sync(hw, swfw_mask))
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +00009093 return E1000_ERR_SWFW_SYNC;
9094
9095 status = i2c_smbus_read_byte_data(this_client, byte_offset);
9096 hw->mac.ops.release_swfw_sync(hw, swfw_mask);
9097
9098 if (status < 0)
9099 return E1000_ERR_I2C;
9100 else {
9101 *data = status;
Todd Fujinaka23d87822014-06-04 07:12:15 +00009102 return 0;
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +00009103 }
9104}
9105
Jeff Kirsherb980ac12013-02-23 07:29:56 +00009106/**
9107 * igb_write_i2c_byte - Writes 8 bit word over I2C
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +00009108 * @hw: pointer to hardware structure
9109 * @byte_offset: byte offset to write
9110 * @dev_addr: device address
9111 * @data: value to write
9112 *
9113 * Performs byte write operation over I2C interface at
9114 * a specified device address.
Jeff Kirsherb980ac12013-02-23 07:29:56 +00009115 **/
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +00009116s32 igb_write_i2c_byte(struct e1000_hw *hw, u8 byte_offset,
Jeff Kirsherb980ac12013-02-23 07:29:56 +00009117 u8 dev_addr, u8 data)
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +00009118{
9119 struct igb_adapter *adapter = container_of(hw, struct igb_adapter, hw);
Carolyn Wyborny603e86f2013-02-20 07:40:55 +00009120 struct i2c_client *this_client = adapter->i2c_client;
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +00009121 s32 status;
9122 u16 swfw_mask = E1000_SWFW_PHY0_SM;
9123
9124 if (!this_client)
9125 return E1000_ERR_I2C;
9126
Todd Fujinaka23d87822014-06-04 07:12:15 +00009127 if (hw->mac.ops.acquire_swfw_sync(hw, swfw_mask))
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +00009128 return E1000_ERR_SWFW_SYNC;
9129 status = i2c_smbus_write_byte_data(this_client, byte_offset, data);
9130 hw->mac.ops.release_swfw_sync(hw, swfw_mask);
9131
9132 if (status)
9133 return E1000_ERR_I2C;
9134 else
Todd Fujinaka23d87822014-06-04 07:12:15 +00009135 return 0;
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +00009136
9137}
Laura Mihaela Vasilescu907b7832013-10-01 04:33:56 -07009138
9139int igb_reinit_queues(struct igb_adapter *adapter)
9140{
9141 struct net_device *netdev = adapter->netdev;
9142 struct pci_dev *pdev = adapter->pdev;
9143 int err = 0;
9144
9145 if (netif_running(netdev))
9146 igb_close(netdev);
9147
Carolyn Wyborny02ef6e12013-12-10 07:58:29 +00009148 igb_reset_interrupt_capability(adapter);
Laura Mihaela Vasilescu907b7832013-10-01 04:33:56 -07009149
9150 if (igb_init_interrupt_scheme(adapter, true)) {
9151 dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
9152 return -ENOMEM;
9153 }
9154
9155 if (netif_running(netdev))
9156 err = igb_open(netdev);
9157
9158 return err;
9159}
Gangfeng Huang0e71def2016-07-06 13:22:54 +08009160
9161static void igb_nfc_filter_exit(struct igb_adapter *adapter)
9162{
9163 struct igb_nfc_filter *rule;
9164
9165 spin_lock(&adapter->nfc_lock);
9166
9167 hlist_for_each_entry(rule, &adapter->nfc_filter_list, nfc_node)
9168 igb_erase_filter(adapter, rule);
9169
9170 spin_unlock(&adapter->nfc_lock);
9171}
9172
9173static void igb_nfc_filter_restore(struct igb_adapter *adapter)
9174{
9175 struct igb_nfc_filter *rule;
9176
9177 spin_lock(&adapter->nfc_lock);
9178
9179 hlist_for_each_entry(rule, &adapter->nfc_filter_list, nfc_node)
9180 igb_add_filter(adapter, rule);
9181
9182 spin_unlock(&adapter->nfc_lock);
9183}
Auke Kok9d5c8242008-01-24 02:22:38 -08009184/* igb_main.c */