blob: 4d53a17959fad7fb00dc43d1317ad10a29fae326 [file] [log] [blame]
Auke Kok9d5c8242008-01-24 02:22:38 -08001/*******************************************************************************
2
3 Intel(R) Gigabit Ethernet Linux driver
Akeem G. Abodunrin4b9ea462013-01-08 18:31:12 +00004 Copyright(c) 2007-2013 Intel Corporation.
Auke Kok9d5c8242008-01-24 02:22:38 -08005
6 This program is free software; you can redistribute it and/or modify it
7 under the terms and conditions of the GNU General Public License,
8 version 2, as published by the Free Software Foundation.
9
10 This program is distributed in the hope it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 more details.
14
15 You should have received a copy of the GNU General Public License along with
16 this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18
19 The full GNU General Public License is included in this distribution in
20 the file called "COPYING".
21
22 Contact Information:
23 e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
24 Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
25
26*******************************************************************************/
27
Jeff Kirsher876d2d62011-10-21 20:01:34 +000028#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
29
Auke Kok9d5c8242008-01-24 02:22:38 -080030#include <linux/module.h>
31#include <linux/types.h>
32#include <linux/init.h>
Jiri Pirkob2cb09b2011-07-21 03:27:27 +000033#include <linux/bitops.h>
Auke Kok9d5c8242008-01-24 02:22:38 -080034#include <linux/vmalloc.h>
35#include <linux/pagemap.h>
36#include <linux/netdevice.h>
Auke Kok9d5c8242008-01-24 02:22:38 -080037#include <linux/ipv6.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
Auke Kok9d5c8242008-01-24 02:22:38 -080039#include <net/checksum.h>
40#include <net/ip6_checksum.h>
Patrick Ohlyc6cb0902009-02-12 05:03:42 +000041#include <linux/net_tstamp.h>
Auke Kok9d5c8242008-01-24 02:22:38 -080042#include <linux/mii.h>
43#include <linux/ethtool.h>
Jiri Pirko01789342011-08-16 06:29:00 +000044#include <linux/if.h>
Auke Kok9d5c8242008-01-24 02:22:38 -080045#include <linux/if_vlan.h>
46#include <linux/pci.h>
Alexander Duyckc54106b2008-10-16 21:26:57 -070047#include <linux/pci-aspm.h>
Auke Kok9d5c8242008-01-24 02:22:38 -080048#include <linux/delay.h>
49#include <linux/interrupt.h>
Alexander Duyck7d13a7d2011-08-26 07:44:32 +000050#include <linux/ip.h>
51#include <linux/tcp.h>
52#include <linux/sctp.h>
Auke Kok9d5c8242008-01-24 02:22:38 -080053#include <linux/if_ether.h>
Alexander Duyck40a914f2008-11-27 00:24:37 -080054#include <linux/aer.h>
Paul Gortmaker70c71602011-05-22 16:47:17 -040055#include <linux/prefetch.h>
Yan, Zheng749ab2c2012-01-04 20:23:37 +000056#include <linux/pm_runtime.h>
Jeff Kirsher421e02f2008-10-17 11:08:31 -070057#ifdef CONFIG_IGB_DCA
Jeb Cramerfe4506b2008-07-08 15:07:55 -070058#include <linux/dca.h>
59#endif
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +000060#include <linux/i2c.h>
Auke Kok9d5c8242008-01-24 02:22:38 -080061#include "igb.h"
62
Carolyn Wyborny200e5fd2012-05-31 23:39:30 +000063#define MAJ 4
Carolyn Wyborny66999382012-12-05 02:46:05 +000064#define MIN 1
65#define BUILD 2
Carolyn Wyborny0d1fe822011-03-11 20:58:19 -080066#define DRV_VERSION __stringify(MAJ) "." __stringify(MIN) "." \
Carolyn Wyborny929dd042011-05-26 03:02:26 +000067__stringify(BUILD) "-k"
Auke Kok9d5c8242008-01-24 02:22:38 -080068char igb_driver_name[] = "igb";
69char igb_driver_version[] = DRV_VERSION;
70static const char igb_driver_string[] =
71 "Intel(R) Gigabit Ethernet Network Driver";
Akeem G. Abodunrin4b9ea462013-01-08 18:31:12 +000072static const char igb_copyright[] =
73 "Copyright (c) 2007-2013 Intel Corporation.";
Auke Kok9d5c8242008-01-24 02:22:38 -080074
Auke Kok9d5c8242008-01-24 02:22:38 -080075static const struct e1000_info *igb_info_tbl[] = {
76 [board_82575] = &e1000_82575_info,
77};
78
Alexey Dobriyana3aa1882010-01-07 11:58:11 +000079static DEFINE_PCI_DEVICE_TABLE(igb_pci_tbl) = {
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +000080 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I211_COPPER), board_82575 },
81 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I210_COPPER), board_82575 },
82 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I210_FIBER), board_82575 },
83 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I210_SERDES), board_82575 },
84 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I210_SGMII), board_82575 },
Alexander Duyckd2ba2ed2010-03-22 14:08:06 +000085 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I350_COPPER), board_82575 },
86 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I350_FIBER), board_82575 },
87 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I350_SERDES), board_82575 },
88 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I350_SGMII), board_82575 },
Alexander Duyck55cac242009-11-19 12:42:21 +000089 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_COPPER), board_82575 },
90 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_FIBER), board_82575 },
Carolyn Wyborny6493d242011-01-14 05:33:46 +000091 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_QUAD_FIBER), board_82575 },
Alexander Duyck55cac242009-11-19 12:42:21 +000092 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_SERDES), board_82575 },
93 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_SGMII), board_82575 },
94 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_COPPER_DUAL), board_82575 },
Joseph Gasparakis308fb392010-09-22 17:56:44 +000095 { PCI_VDEVICE(INTEL, E1000_DEV_ID_DH89XXCC_SGMII), board_82575 },
96 { PCI_VDEVICE(INTEL, E1000_DEV_ID_DH89XXCC_SERDES), board_82575 },
Gasparakis, Joseph1b5dda32010-12-09 01:41:01 +000097 { PCI_VDEVICE(INTEL, E1000_DEV_ID_DH89XXCC_BACKPLANE), board_82575 },
98 { PCI_VDEVICE(INTEL, E1000_DEV_ID_DH89XXCC_SFP), board_82575 },
Alexander Duyck2d064c02008-07-08 15:10:12 -070099 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576), board_82575 },
Alexander Duyck9eb23412009-03-13 20:42:15 +0000100 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_NS), board_82575 },
Alexander Duyck747d49b2009-10-05 06:33:27 +0000101 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_NS_SERDES), board_82575 },
Alexander Duyck2d064c02008-07-08 15:10:12 -0700102 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_FIBER), board_82575 },
103 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_SERDES), board_82575 },
Alexander Duyck4703bf72009-07-23 18:09:48 +0000104 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_SERDES_QUAD), board_82575 },
Carolyn Wybornyb894fa22010-03-19 06:07:48 +0000105 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_QUAD_COPPER_ET2), board_82575 },
Alexander Duyckc8ea5ea2009-03-13 20:42:35 +0000106 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_QUAD_COPPER), board_82575 },
Auke Kok9d5c8242008-01-24 02:22:38 -0800107 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82575EB_COPPER), board_82575 },
108 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82575EB_FIBER_SERDES), board_82575 },
109 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82575GB_QUAD_COPPER), board_82575 },
110 /* required last entry */
111 {0, }
112};
113
114MODULE_DEVICE_TABLE(pci, igb_pci_tbl);
115
116void igb_reset(struct igb_adapter *);
117static int igb_setup_all_tx_resources(struct igb_adapter *);
118static int igb_setup_all_rx_resources(struct igb_adapter *);
119static void igb_free_all_tx_resources(struct igb_adapter *);
120static void igb_free_all_rx_resources(struct igb_adapter *);
Alexander Duyck06cf2662009-10-27 15:53:25 +0000121static void igb_setup_mrqc(struct igb_adapter *);
Auke Kok9d5c8242008-01-24 02:22:38 -0800122static int igb_probe(struct pci_dev *, const struct pci_device_id *);
Bill Pemberton9f9a12f2012-12-03 09:24:25 -0500123static void igb_remove(struct pci_dev *pdev);
Auke Kok9d5c8242008-01-24 02:22:38 -0800124static int igb_sw_init(struct igb_adapter *);
125static int igb_open(struct net_device *);
126static int igb_close(struct net_device *);
Stefan Assmann53c7d062012-12-04 06:00:12 +0000127static void igb_configure(struct igb_adapter *);
Auke Kok9d5c8242008-01-24 02:22:38 -0800128static void igb_configure_tx(struct igb_adapter *);
129static void igb_configure_rx(struct igb_adapter *);
Auke Kok9d5c8242008-01-24 02:22:38 -0800130static void igb_clean_all_tx_rings(struct igb_adapter *);
131static void igb_clean_all_rx_rings(struct igb_adapter *);
Mitch Williams3b644cf2008-06-27 10:59:48 -0700132static void igb_clean_tx_ring(struct igb_ring *);
133static void igb_clean_rx_ring(struct igb_ring *);
Alexander Duyckff41f8d2009-09-03 14:48:56 +0000134static void igb_set_rx_mode(struct net_device *);
Auke Kok9d5c8242008-01-24 02:22:38 -0800135static void igb_update_phy_info(unsigned long);
136static void igb_watchdog(unsigned long);
137static void igb_watchdog_task(struct work_struct *);
Alexander Duyckcd392f52011-08-26 07:43:59 +0000138static netdev_tx_t igb_xmit_frame(struct sk_buff *skb, struct net_device *);
Eric Dumazet12dcd862010-10-15 17:27:10 +0000139static struct rtnl_link_stats64 *igb_get_stats64(struct net_device *dev,
140 struct rtnl_link_stats64 *stats);
Auke Kok9d5c8242008-01-24 02:22:38 -0800141static int igb_change_mtu(struct net_device *, int);
142static int igb_set_mac(struct net_device *, void *);
Alexander Duyck68d480c2009-10-05 06:33:08 +0000143static void igb_set_uta(struct igb_adapter *adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -0800144static irqreturn_t igb_intr(int irq, void *);
145static irqreturn_t igb_intr_msi(int irq, void *);
146static irqreturn_t igb_msix_other(int irq, void *);
Alexander Duyck047e0032009-10-27 15:49:27 +0000147static irqreturn_t igb_msix_ring(int irq, void *);
Jeff Kirsher421e02f2008-10-17 11:08:31 -0700148#ifdef CONFIG_IGB_DCA
Alexander Duyck047e0032009-10-27 15:49:27 +0000149static void igb_update_dca(struct igb_q_vector *);
Jeb Cramerfe4506b2008-07-08 15:07:55 -0700150static void igb_setup_dca(struct igb_adapter *);
Jeff Kirsher421e02f2008-10-17 11:08:31 -0700151#endif /* CONFIG_IGB_DCA */
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -0700152static int igb_poll(struct napi_struct *, int);
Alexander Duyck13fde972011-10-05 13:35:24 +0000153static bool igb_clean_tx_irq(struct igb_q_vector *);
Alexander Duyckcd392f52011-08-26 07:43:59 +0000154static bool igb_clean_rx_irq(struct igb_q_vector *, int);
Auke Kok9d5c8242008-01-24 02:22:38 -0800155static int igb_ioctl(struct net_device *, struct ifreq *, int cmd);
156static void igb_tx_timeout(struct net_device *);
157static void igb_reset_task(struct work_struct *);
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000158static void igb_vlan_mode(struct net_device *netdev, netdev_features_t features);
Jiri Pirko8e586132011-12-08 19:52:37 -0500159static int igb_vlan_rx_add_vid(struct net_device *, u16);
160static int igb_vlan_rx_kill_vid(struct net_device *, u16);
Auke Kok9d5c8242008-01-24 02:22:38 -0800161static void igb_restore_vlan(struct igb_adapter *);
Alexander Duyck26ad9172009-10-05 06:32:49 +0000162static void igb_rar_set_qsel(struct igb_adapter *, u8 *, u32 , u8);
Alexander Duyck4ae196d2009-02-19 20:40:07 -0800163static void igb_ping_all_vfs(struct igb_adapter *);
164static void igb_msg_task(struct igb_adapter *);
Alexander Duyck4ae196d2009-02-19 20:40:07 -0800165static void igb_vmm_control(struct igb_adapter *);
Alexander Duyckf2ca0db2009-10-27 23:46:57 +0000166static int igb_set_vf_mac(struct igb_adapter *, int, unsigned char *);
Alexander Duyck4ae196d2009-02-19 20:40:07 -0800167static void igb_restore_vf_multicasts(struct igb_adapter *adapter);
Williams, Mitch A8151d292010-02-10 01:44:24 +0000168static int igb_ndo_set_vf_mac(struct net_device *netdev, int vf, u8 *mac);
169static int igb_ndo_set_vf_vlan(struct net_device *netdev,
170 int vf, u16 vlan, u8 qos);
171static int igb_ndo_set_vf_bw(struct net_device *netdev, int vf, int tx_rate);
172static int igb_ndo_get_vf_config(struct net_device *netdev, int vf,
173 struct ifla_vf_info *ivi);
Lior Levy17dc5662011-02-08 02:28:46 +0000174static void igb_check_vf_rate_limit(struct igb_adapter *);
RongQing Li46a01692011-10-18 22:52:35 +0000175
176#ifdef CONFIG_PCI_IOV
Greg Rose0224d662011-10-14 02:57:14 +0000177static int igb_vf_configure(struct igb_adapter *adapter, int vf);
Stefan Assmannf5571472012-08-18 04:06:11 +0000178static bool igb_vfs_are_assigned(struct igb_adapter *adapter);
RongQing Li46a01692011-10-18 22:52:35 +0000179#endif
Auke Kok9d5c8242008-01-24 02:22:38 -0800180
Auke Kok9d5c8242008-01-24 02:22:38 -0800181#ifdef CONFIG_PM
Emil Tantilovd9dd9662012-01-28 08:10:35 +0000182#ifdef CONFIG_PM_SLEEP
Yan, Zheng749ab2c2012-01-04 20:23:37 +0000183static int igb_suspend(struct device *);
Emil Tantilovd9dd9662012-01-28 08:10:35 +0000184#endif
Yan, Zheng749ab2c2012-01-04 20:23:37 +0000185static int igb_resume(struct device *);
186#ifdef CONFIG_PM_RUNTIME
187static int igb_runtime_suspend(struct device *dev);
188static int igb_runtime_resume(struct device *dev);
189static int igb_runtime_idle(struct device *dev);
190#endif
191static const struct dev_pm_ops igb_pm_ops = {
192 SET_SYSTEM_SLEEP_PM_OPS(igb_suspend, igb_resume)
193 SET_RUNTIME_PM_OPS(igb_runtime_suspend, igb_runtime_resume,
194 igb_runtime_idle)
195};
Auke Kok9d5c8242008-01-24 02:22:38 -0800196#endif
197static void igb_shutdown(struct pci_dev *);
Greg Rosefa44f2f2013-01-17 01:03:06 -0800198static int igb_pci_sriov_configure(struct pci_dev *dev, int num_vfs);
Jeff Kirsher421e02f2008-10-17 11:08:31 -0700199#ifdef CONFIG_IGB_DCA
Jeb Cramerfe4506b2008-07-08 15:07:55 -0700200static int igb_notify_dca(struct notifier_block *, unsigned long, void *);
201static struct notifier_block dca_notifier = {
202 .notifier_call = igb_notify_dca,
203 .next = NULL,
204 .priority = 0
205};
206#endif
Auke Kok9d5c8242008-01-24 02:22:38 -0800207#ifdef CONFIG_NET_POLL_CONTROLLER
208/* for netdump / net console */
209static void igb_netpoll(struct net_device *);
210#endif
Alexander Duyck37680112009-02-19 20:40:30 -0800211#ifdef CONFIG_PCI_IOV
Alexander Duyck2a3abf62009-04-07 14:37:52 +0000212static unsigned int max_vfs = 0;
213module_param(max_vfs, uint, 0);
214MODULE_PARM_DESC(max_vfs, "Maximum number of virtual functions to allocate "
215 "per physical function");
216#endif /* CONFIG_PCI_IOV */
217
Auke Kok9d5c8242008-01-24 02:22:38 -0800218static pci_ers_result_t igb_io_error_detected(struct pci_dev *,
219 pci_channel_state_t);
220static pci_ers_result_t igb_io_slot_reset(struct pci_dev *);
221static void igb_io_resume(struct pci_dev *);
222
Stephen Hemminger3646f0e2012-09-07 09:33:15 -0700223static const struct pci_error_handlers igb_err_handler = {
Auke Kok9d5c8242008-01-24 02:22:38 -0800224 .error_detected = igb_io_error_detected,
225 .slot_reset = igb_io_slot_reset,
226 .resume = igb_io_resume,
227};
228
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +0000229static void igb_init_dmac(struct igb_adapter *adapter, u32 pba);
Auke Kok9d5c8242008-01-24 02:22:38 -0800230
231static struct pci_driver igb_driver = {
232 .name = igb_driver_name,
233 .id_table = igb_pci_tbl,
234 .probe = igb_probe,
Bill Pemberton9f9a12f2012-12-03 09:24:25 -0500235 .remove = igb_remove,
Auke Kok9d5c8242008-01-24 02:22:38 -0800236#ifdef CONFIG_PM
Yan, Zheng749ab2c2012-01-04 20:23:37 +0000237 .driver.pm = &igb_pm_ops,
Auke Kok9d5c8242008-01-24 02:22:38 -0800238#endif
239 .shutdown = igb_shutdown,
Greg Rosefa44f2f2013-01-17 01:03:06 -0800240 .sriov_configure = igb_pci_sriov_configure,
Auke Kok9d5c8242008-01-24 02:22:38 -0800241 .err_handler = &igb_err_handler
242};
243
244MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
245MODULE_DESCRIPTION("Intel(R) Gigabit Ethernet Network Driver");
246MODULE_LICENSE("GPL");
247MODULE_VERSION(DRV_VERSION);
248
stephen hemmingerb3f4d592012-03-13 06:04:20 +0000249#define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK)
250static int debug = -1;
251module_param(debug, int, 0);
252MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
253
Taku Izumic97ec422010-04-27 14:39:30 +0000254struct igb_reg_info {
255 u32 ofs;
256 char *name;
257};
258
259static const struct igb_reg_info igb_reg_info_tbl[] = {
260
261 /* General Registers */
262 {E1000_CTRL, "CTRL"},
263 {E1000_STATUS, "STATUS"},
264 {E1000_CTRL_EXT, "CTRL_EXT"},
265
266 /* Interrupt Registers */
267 {E1000_ICR, "ICR"},
268
269 /* RX Registers */
270 {E1000_RCTL, "RCTL"},
271 {E1000_RDLEN(0), "RDLEN"},
272 {E1000_RDH(0), "RDH"},
273 {E1000_RDT(0), "RDT"},
274 {E1000_RXDCTL(0), "RXDCTL"},
275 {E1000_RDBAL(0), "RDBAL"},
276 {E1000_RDBAH(0), "RDBAH"},
277
278 /* TX Registers */
279 {E1000_TCTL, "TCTL"},
280 {E1000_TDBAL(0), "TDBAL"},
281 {E1000_TDBAH(0), "TDBAH"},
282 {E1000_TDLEN(0), "TDLEN"},
283 {E1000_TDH(0), "TDH"},
284 {E1000_TDT(0), "TDT"},
285 {E1000_TXDCTL(0), "TXDCTL"},
286 {E1000_TDFH, "TDFH"},
287 {E1000_TDFT, "TDFT"},
288 {E1000_TDFHS, "TDFHS"},
289 {E1000_TDFPC, "TDFPC"},
290
291 /* List Terminator */
292 {}
293};
294
295/*
296 * igb_regdump - register printout routine
297 */
298static void igb_regdump(struct e1000_hw *hw, struct igb_reg_info *reginfo)
299{
300 int n = 0;
301 char rname[16];
302 u32 regs[8];
303
304 switch (reginfo->ofs) {
305 case E1000_RDLEN(0):
306 for (n = 0; n < 4; n++)
307 regs[n] = rd32(E1000_RDLEN(n));
308 break;
309 case E1000_RDH(0):
310 for (n = 0; n < 4; n++)
311 regs[n] = rd32(E1000_RDH(n));
312 break;
313 case E1000_RDT(0):
314 for (n = 0; n < 4; n++)
315 regs[n] = rd32(E1000_RDT(n));
316 break;
317 case E1000_RXDCTL(0):
318 for (n = 0; n < 4; n++)
319 regs[n] = rd32(E1000_RXDCTL(n));
320 break;
321 case E1000_RDBAL(0):
322 for (n = 0; n < 4; n++)
323 regs[n] = rd32(E1000_RDBAL(n));
324 break;
325 case E1000_RDBAH(0):
326 for (n = 0; n < 4; n++)
327 regs[n] = rd32(E1000_RDBAH(n));
328 break;
329 case E1000_TDBAL(0):
330 for (n = 0; n < 4; n++)
331 regs[n] = rd32(E1000_RDBAL(n));
332 break;
333 case E1000_TDBAH(0):
334 for (n = 0; n < 4; n++)
335 regs[n] = rd32(E1000_TDBAH(n));
336 break;
337 case E1000_TDLEN(0):
338 for (n = 0; n < 4; n++)
339 regs[n] = rd32(E1000_TDLEN(n));
340 break;
341 case E1000_TDH(0):
342 for (n = 0; n < 4; n++)
343 regs[n] = rd32(E1000_TDH(n));
344 break;
345 case E1000_TDT(0):
346 for (n = 0; n < 4; n++)
347 regs[n] = rd32(E1000_TDT(n));
348 break;
349 case E1000_TXDCTL(0):
350 for (n = 0; n < 4; n++)
351 regs[n] = rd32(E1000_TXDCTL(n));
352 break;
353 default:
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000354 pr_info("%-15s %08x\n", reginfo->name, rd32(reginfo->ofs));
Taku Izumic97ec422010-04-27 14:39:30 +0000355 return;
356 }
357
358 snprintf(rname, 16, "%s%s", reginfo->name, "[0-3]");
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000359 pr_info("%-15s %08x %08x %08x %08x\n", rname, regs[0], regs[1],
360 regs[2], regs[3]);
Taku Izumic97ec422010-04-27 14:39:30 +0000361}
362
363/*
364 * igb_dump - Print registers, tx-rings and rx-rings
365 */
366static void igb_dump(struct igb_adapter *adapter)
367{
368 struct net_device *netdev = adapter->netdev;
369 struct e1000_hw *hw = &adapter->hw;
370 struct igb_reg_info *reginfo;
Taku Izumic97ec422010-04-27 14:39:30 +0000371 struct igb_ring *tx_ring;
372 union e1000_adv_tx_desc *tx_desc;
373 struct my_u0 { u64 a; u64 b; } *u0;
Taku Izumic97ec422010-04-27 14:39:30 +0000374 struct igb_ring *rx_ring;
375 union e1000_adv_rx_desc *rx_desc;
376 u32 staterr;
Alexander Duyck6ad4edf2011-08-26 07:45:26 +0000377 u16 i, n;
Taku Izumic97ec422010-04-27 14:39:30 +0000378
379 if (!netif_msg_hw(adapter))
380 return;
381
382 /* Print netdevice Info */
383 if (netdev) {
384 dev_info(&adapter->pdev->dev, "Net device Info\n");
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000385 pr_info("Device Name state trans_start "
386 "last_rx\n");
387 pr_info("%-15s %016lX %016lX %016lX\n", netdev->name,
388 netdev->state, netdev->trans_start, netdev->last_rx);
Taku Izumic97ec422010-04-27 14:39:30 +0000389 }
390
391 /* Print Registers */
392 dev_info(&adapter->pdev->dev, "Register Dump\n");
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000393 pr_info(" Register Name Value\n");
Taku Izumic97ec422010-04-27 14:39:30 +0000394 for (reginfo = (struct igb_reg_info *)igb_reg_info_tbl;
395 reginfo->name; reginfo++) {
396 igb_regdump(hw, reginfo);
397 }
398
399 /* Print TX Ring Summary */
400 if (!netdev || !netif_running(netdev))
401 goto exit;
402
403 dev_info(&adapter->pdev->dev, "TX Rings Summary\n");
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000404 pr_info("Queue [NTU] [NTC] [bi(ntc)->dma ] leng ntw timestamp\n");
Taku Izumic97ec422010-04-27 14:39:30 +0000405 for (n = 0; n < adapter->num_tx_queues; n++) {
Alexander Duyck06034642011-08-26 07:44:22 +0000406 struct igb_tx_buffer *buffer_info;
Taku Izumic97ec422010-04-27 14:39:30 +0000407 tx_ring = adapter->tx_ring[n];
Alexander Duyck06034642011-08-26 07:44:22 +0000408 buffer_info = &tx_ring->tx_buffer_info[tx_ring->next_to_clean];
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000409 pr_info(" %5d %5X %5X %016llX %04X %p %016llX\n",
410 n, tx_ring->next_to_use, tx_ring->next_to_clean,
Alexander Duyckc9f14bf32012-09-18 01:56:27 +0000411 (u64)dma_unmap_addr(buffer_info, dma),
412 dma_unmap_len(buffer_info, len),
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000413 buffer_info->next_to_watch,
414 (u64)buffer_info->time_stamp);
Taku Izumic97ec422010-04-27 14:39:30 +0000415 }
416
417 /* Print TX Rings */
418 if (!netif_msg_tx_done(adapter))
419 goto rx_ring_summary;
420
421 dev_info(&adapter->pdev->dev, "TX Rings Dump\n");
422
423 /* Transmit Descriptor Formats
424 *
425 * Advanced Transmit Descriptor
426 * +--------------------------------------------------------------+
427 * 0 | Buffer Address [63:0] |
428 * +--------------------------------------------------------------+
429 * 8 | PAYLEN | PORTS |CC|IDX | STA | DCMD |DTYP|MAC|RSV| DTALEN |
430 * +--------------------------------------------------------------+
431 * 63 46 45 40 39 38 36 35 32 31 24 15 0
432 */
433
434 for (n = 0; n < adapter->num_tx_queues; n++) {
435 tx_ring = adapter->tx_ring[n];
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000436 pr_info("------------------------------------\n");
437 pr_info("TX QUEUE INDEX = %d\n", tx_ring->queue_index);
438 pr_info("------------------------------------\n");
439 pr_info("T [desc] [address 63:0 ] [PlPOCIStDDM Ln] "
440 "[bi->dma ] leng ntw timestamp "
441 "bi->skb\n");
Taku Izumic97ec422010-04-27 14:39:30 +0000442
443 for (i = 0; tx_ring->desc && (i < tx_ring->count); i++) {
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000444 const char *next_desc;
Alexander Duyck06034642011-08-26 07:44:22 +0000445 struct igb_tx_buffer *buffer_info;
Alexander Duyck601369062011-08-26 07:44:05 +0000446 tx_desc = IGB_TX_DESC(tx_ring, i);
Alexander Duyck06034642011-08-26 07:44:22 +0000447 buffer_info = &tx_ring->tx_buffer_info[i];
Taku Izumic97ec422010-04-27 14:39:30 +0000448 u0 = (struct my_u0 *)tx_desc;
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000449 if (i == tx_ring->next_to_use &&
450 i == tx_ring->next_to_clean)
451 next_desc = " NTC/U";
452 else if (i == tx_ring->next_to_use)
453 next_desc = " NTU";
454 else if (i == tx_ring->next_to_clean)
455 next_desc = " NTC";
456 else
457 next_desc = "";
458
459 pr_info("T [0x%03X] %016llX %016llX %016llX"
460 " %04X %p %016llX %p%s\n", i,
Taku Izumic97ec422010-04-27 14:39:30 +0000461 le64_to_cpu(u0->a),
462 le64_to_cpu(u0->b),
Alexander Duyckc9f14bf32012-09-18 01:56:27 +0000463 (u64)dma_unmap_addr(buffer_info, dma),
464 dma_unmap_len(buffer_info, len),
Taku Izumic97ec422010-04-27 14:39:30 +0000465 buffer_info->next_to_watch,
466 (u64)buffer_info->time_stamp,
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000467 buffer_info->skb, next_desc);
Taku Izumic97ec422010-04-27 14:39:30 +0000468
Emil Tantilovb6695882012-07-28 05:07:48 +0000469 if (netif_msg_pktdata(adapter) && buffer_info->skb)
Taku Izumic97ec422010-04-27 14:39:30 +0000470 print_hex_dump(KERN_INFO, "",
471 DUMP_PREFIX_ADDRESS,
Emil Tantilovb6695882012-07-28 05:07:48 +0000472 16, 1, buffer_info->skb->data,
Alexander Duyckc9f14bf32012-09-18 01:56:27 +0000473 dma_unmap_len(buffer_info, len),
474 true);
Taku Izumic97ec422010-04-27 14:39:30 +0000475 }
476 }
477
478 /* Print RX Rings Summary */
479rx_ring_summary:
480 dev_info(&adapter->pdev->dev, "RX Rings Summary\n");
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000481 pr_info("Queue [NTU] [NTC]\n");
Taku Izumic97ec422010-04-27 14:39:30 +0000482 for (n = 0; n < adapter->num_rx_queues; n++) {
483 rx_ring = adapter->rx_ring[n];
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000484 pr_info(" %5d %5X %5X\n",
485 n, rx_ring->next_to_use, rx_ring->next_to_clean);
Taku Izumic97ec422010-04-27 14:39:30 +0000486 }
487
488 /* Print RX Rings */
489 if (!netif_msg_rx_status(adapter))
490 goto exit;
491
492 dev_info(&adapter->pdev->dev, "RX Rings Dump\n");
493
494 /* Advanced Receive Descriptor (Read) Format
495 * 63 1 0
496 * +-----------------------------------------------------+
497 * 0 | Packet Buffer Address [63:1] |A0/NSE|
498 * +----------------------------------------------+------+
499 * 8 | Header Buffer Address [63:1] | DD |
500 * +-----------------------------------------------------+
501 *
502 *
503 * Advanced Receive Descriptor (Write-Back) Format
504 *
505 * 63 48 47 32 31 30 21 20 17 16 4 3 0
506 * +------------------------------------------------------+
507 * 0 | Packet IP |SPH| HDR_LEN | RSV|Packet| RSS |
508 * | Checksum Ident | | | | Type | Type |
509 * +------------------------------------------------------+
510 * 8 | VLAN Tag | Length | Extended Error | Extended Status |
511 * +------------------------------------------------------+
512 * 63 48 47 32 31 20 19 0
513 */
514
515 for (n = 0; n < adapter->num_rx_queues; n++) {
516 rx_ring = adapter->rx_ring[n];
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000517 pr_info("------------------------------------\n");
518 pr_info("RX QUEUE INDEX = %d\n", rx_ring->queue_index);
519 pr_info("------------------------------------\n");
520 pr_info("R [desc] [ PktBuf A0] [ HeadBuf DD] "
521 "[bi->dma ] [bi->skb] <-- Adv Rx Read format\n");
522 pr_info("RWB[desc] [PcsmIpSHl PtRs] [vl er S cks ln] -----"
523 "----------- [bi->skb] <-- Adv Rx Write-Back format\n");
Taku Izumic97ec422010-04-27 14:39:30 +0000524
525 for (i = 0; i < rx_ring->count; i++) {
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000526 const char *next_desc;
Alexander Duyck06034642011-08-26 07:44:22 +0000527 struct igb_rx_buffer *buffer_info;
528 buffer_info = &rx_ring->rx_buffer_info[i];
Alexander Duyck601369062011-08-26 07:44:05 +0000529 rx_desc = IGB_RX_DESC(rx_ring, i);
Taku Izumic97ec422010-04-27 14:39:30 +0000530 u0 = (struct my_u0 *)rx_desc;
531 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000532
533 if (i == rx_ring->next_to_use)
534 next_desc = " NTU";
535 else if (i == rx_ring->next_to_clean)
536 next_desc = " NTC";
537 else
538 next_desc = "";
539
Taku Izumic97ec422010-04-27 14:39:30 +0000540 if (staterr & E1000_RXD_STAT_DD) {
541 /* Descriptor Done */
Alexander Duyck1a1c2252012-09-25 00:30:52 +0000542 pr_info("%s[0x%03X] %016llX %016llX ---------------- %s\n",
543 "RWB", i,
Taku Izumic97ec422010-04-27 14:39:30 +0000544 le64_to_cpu(u0->a),
545 le64_to_cpu(u0->b),
Alexander Duyck1a1c2252012-09-25 00:30:52 +0000546 next_desc);
Taku Izumic97ec422010-04-27 14:39:30 +0000547 } else {
Alexander Duyck1a1c2252012-09-25 00:30:52 +0000548 pr_info("%s[0x%03X] %016llX %016llX %016llX %s\n",
549 "R ", i,
Taku Izumic97ec422010-04-27 14:39:30 +0000550 le64_to_cpu(u0->a),
551 le64_to_cpu(u0->b),
552 (u64)buffer_info->dma,
Alexander Duyck1a1c2252012-09-25 00:30:52 +0000553 next_desc);
Taku Izumic97ec422010-04-27 14:39:30 +0000554
Emil Tantilovb6695882012-07-28 05:07:48 +0000555 if (netif_msg_pktdata(adapter) &&
Alexander Duyck1a1c2252012-09-25 00:30:52 +0000556 buffer_info->dma && buffer_info->page) {
Alexander Duyck44390ca2011-08-26 07:43:38 +0000557 print_hex_dump(KERN_INFO, "",
558 DUMP_PREFIX_ADDRESS,
559 16, 1,
Emil Tantilovb6695882012-07-28 05:07:48 +0000560 page_address(buffer_info->page) +
561 buffer_info->page_offset,
Alexander Duyckde78d1f2012-09-25 00:31:12 +0000562 IGB_RX_BUFSZ, true);
Taku Izumic97ec422010-04-27 14:39:30 +0000563 }
564 }
Taku Izumic97ec422010-04-27 14:39:30 +0000565 }
566 }
567
568exit:
569 return;
570}
571
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +0000572/* igb_get_i2c_data - Reads the I2C SDA data bit
573 * @hw: pointer to hardware structure
574 * @i2cctl: Current value of I2CCTL register
575 *
576 * Returns the I2C data bit value
577 */
578static int igb_get_i2c_data(void *data)
579{
580 struct igb_adapter *adapter = (struct igb_adapter *)data;
581 struct e1000_hw *hw = &adapter->hw;
582 s32 i2cctl = rd32(E1000_I2CPARAMS);
583
584 return ((i2cctl & E1000_I2C_DATA_IN) != 0);
585}
586
587/* igb_set_i2c_data - Sets the I2C data bit
588 * @data: pointer to hardware structure
589 * @state: I2C data value (0 or 1) to set
590 *
591 * Sets the I2C data bit
592 */
593static void igb_set_i2c_data(void *data, int state)
594{
595 struct igb_adapter *adapter = (struct igb_adapter *)data;
596 struct e1000_hw *hw = &adapter->hw;
597 s32 i2cctl = rd32(E1000_I2CPARAMS);
598
599 if (state)
600 i2cctl |= E1000_I2C_DATA_OUT;
601 else
602 i2cctl &= ~E1000_I2C_DATA_OUT;
603
604 i2cctl &= ~E1000_I2C_DATA_OE_N;
605 i2cctl |= E1000_I2C_CLK_OE_N;
606 wr32(E1000_I2CPARAMS, i2cctl);
607 wrfl();
608
609}
610
611/* igb_set_i2c_clk - Sets the I2C SCL clock
612 * @data: pointer to hardware structure
613 * @state: state to set clock
614 *
615 * Sets the I2C clock line to state
616 */
617static void igb_set_i2c_clk(void *data, int state)
618{
619 struct igb_adapter *adapter = (struct igb_adapter *)data;
620 struct e1000_hw *hw = &adapter->hw;
621 s32 i2cctl = rd32(E1000_I2CPARAMS);
622
623 if (state) {
624 i2cctl |= E1000_I2C_CLK_OUT;
625 i2cctl &= ~E1000_I2C_CLK_OE_N;
626 } else {
627 i2cctl &= ~E1000_I2C_CLK_OUT;
628 i2cctl &= ~E1000_I2C_CLK_OE_N;
629 }
630 wr32(E1000_I2CPARAMS, i2cctl);
631 wrfl();
632}
633
634/* igb_get_i2c_clk - Gets the I2C SCL clock state
635 * @data: pointer to hardware structure
636 *
637 * Gets the I2C clock state
638 */
639static int igb_get_i2c_clk(void *data)
640{
641 struct igb_adapter *adapter = (struct igb_adapter *)data;
642 struct e1000_hw *hw = &adapter->hw;
643 s32 i2cctl = rd32(E1000_I2CPARAMS);
644
645 return ((i2cctl & E1000_I2C_CLK_IN) != 0);
646}
647
648static const struct i2c_algo_bit_data igb_i2c_algo = {
649 .setsda = igb_set_i2c_data,
650 .setscl = igb_set_i2c_clk,
651 .getsda = igb_get_i2c_data,
652 .getscl = igb_get_i2c_clk,
653 .udelay = 5,
654 .timeout = 20,
655};
656
Auke Kok9d5c8242008-01-24 02:22:38 -0800657/**
Alexander Duyckc0410762010-03-25 13:10:08 +0000658 * igb_get_hw_dev - return device
Auke Kok9d5c8242008-01-24 02:22:38 -0800659 * used by hardware layer to print debugging information
660 **/
Alexander Duyckc0410762010-03-25 13:10:08 +0000661struct net_device *igb_get_hw_dev(struct e1000_hw *hw)
Auke Kok9d5c8242008-01-24 02:22:38 -0800662{
663 struct igb_adapter *adapter = hw->back;
Alexander Duyckc0410762010-03-25 13:10:08 +0000664 return adapter->netdev;
Auke Kok9d5c8242008-01-24 02:22:38 -0800665}
Patrick Ohly38c845c2009-02-12 05:03:41 +0000666
667/**
Auke Kok9d5c8242008-01-24 02:22:38 -0800668 * igb_init_module - Driver Registration Routine
669 *
670 * igb_init_module is the first routine called when the driver is
671 * loaded. All it does is register with the PCI subsystem.
672 **/
673static int __init igb_init_module(void)
674{
675 int ret;
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000676 pr_info("%s - version %s\n",
Auke Kok9d5c8242008-01-24 02:22:38 -0800677 igb_driver_string, igb_driver_version);
678
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000679 pr_info("%s\n", igb_copyright);
Auke Kok9d5c8242008-01-24 02:22:38 -0800680
Jeff Kirsher421e02f2008-10-17 11:08:31 -0700681#ifdef CONFIG_IGB_DCA
Jeb Cramerfe4506b2008-07-08 15:07:55 -0700682 dca_register_notify(&dca_notifier);
683#endif
Alexander Duyckbbd98fe2009-01-31 00:52:30 -0800684 ret = pci_register_driver(&igb_driver);
Auke Kok9d5c8242008-01-24 02:22:38 -0800685 return ret;
686}
687
688module_init(igb_init_module);
689
690/**
691 * igb_exit_module - Driver Exit Cleanup Routine
692 *
693 * igb_exit_module is called just before the driver is removed
694 * from memory.
695 **/
696static void __exit igb_exit_module(void)
697{
Jeff Kirsher421e02f2008-10-17 11:08:31 -0700698#ifdef CONFIG_IGB_DCA
Jeb Cramerfe4506b2008-07-08 15:07:55 -0700699 dca_unregister_notify(&dca_notifier);
700#endif
Auke Kok9d5c8242008-01-24 02:22:38 -0800701 pci_unregister_driver(&igb_driver);
702}
703
704module_exit(igb_exit_module);
705
Alexander Duyck26bc19e2008-12-26 01:34:11 -0800706#define Q_IDX_82576(i) (((i & 0x1) << 3) + (i >> 1))
707/**
708 * igb_cache_ring_register - Descriptor ring to register mapping
709 * @adapter: board private structure to initialize
710 *
711 * Once we know the feature-set enabled for the device, we'll cache
712 * the register offset the descriptor ring is assigned to.
713 **/
714static void igb_cache_ring_register(struct igb_adapter *adapter)
715{
Alexander Duyckee1b9f02009-10-27 23:49:40 +0000716 int i = 0, j = 0;
Alexander Duyck047e0032009-10-27 15:49:27 +0000717 u32 rbase_offset = adapter->vfs_allocated_count;
Alexander Duyck26bc19e2008-12-26 01:34:11 -0800718
719 switch (adapter->hw.mac.type) {
720 case e1000_82576:
721 /* The queues are allocated for virtualization such that VF 0
722 * is allocated queues 0 and 8, VF 1 queues 1 and 9, etc.
723 * In order to avoid collision we start at the first free queue
724 * and continue consuming queues in the same sequence
725 */
Alexander Duyckee1b9f02009-10-27 23:49:40 +0000726 if (adapter->vfs_allocated_count) {
Alexander Duycka99955f2009-11-12 18:37:19 +0000727 for (; i < adapter->rss_queues; i++)
Alexander Duyck3025a442010-02-17 01:02:39 +0000728 adapter->rx_ring[i]->reg_idx = rbase_offset +
729 Q_IDX_82576(i);
Alexander Duyckee1b9f02009-10-27 23:49:40 +0000730 }
Alexander Duyck26bc19e2008-12-26 01:34:11 -0800731 case e1000_82575:
Alexander Duyck55cac242009-11-19 12:42:21 +0000732 case e1000_82580:
Alexander Duyckd2ba2ed2010-03-22 14:08:06 +0000733 case e1000_i350:
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +0000734 case e1000_i210:
735 case e1000_i211:
Alexander Duyck26bc19e2008-12-26 01:34:11 -0800736 default:
Alexander Duyckee1b9f02009-10-27 23:49:40 +0000737 for (; i < adapter->num_rx_queues; i++)
Alexander Duyck3025a442010-02-17 01:02:39 +0000738 adapter->rx_ring[i]->reg_idx = rbase_offset + i;
Alexander Duyckee1b9f02009-10-27 23:49:40 +0000739 for (; j < adapter->num_tx_queues; j++)
Alexander Duyck3025a442010-02-17 01:02:39 +0000740 adapter->tx_ring[j]->reg_idx = rbase_offset + j;
Alexander Duyck26bc19e2008-12-26 01:34:11 -0800741 break;
742 }
743}
744
Alexander Duyck4be000c2011-08-26 07:45:52 +0000745/**
746 * igb_write_ivar - configure ivar for given MSI-X vector
747 * @hw: pointer to the HW structure
748 * @msix_vector: vector number we are allocating to a given ring
749 * @index: row index of IVAR register to write within IVAR table
750 * @offset: column offset of in IVAR, should be multiple of 8
751 *
752 * This function is intended to handle the writing of the IVAR register
753 * for adapters 82576 and newer. The IVAR table consists of 2 columns,
754 * each containing an cause allocation for an Rx and Tx ring, and a
755 * variable number of rows depending on the number of queues supported.
756 **/
757static void igb_write_ivar(struct e1000_hw *hw, int msix_vector,
758 int index, int offset)
759{
760 u32 ivar = array_rd32(E1000_IVAR0, index);
761
762 /* clear any bits that are currently set */
763 ivar &= ~((u32)0xFF << offset);
764
765 /* write vector and valid bit */
766 ivar |= (msix_vector | E1000_IVAR_VALID) << offset;
767
768 array_wr32(E1000_IVAR0, index, ivar);
769}
770
Auke Kok9d5c8242008-01-24 02:22:38 -0800771#define IGB_N0_QUEUE -1
Alexander Duyck047e0032009-10-27 15:49:27 +0000772static void igb_assign_vector(struct igb_q_vector *q_vector, int msix_vector)
Auke Kok9d5c8242008-01-24 02:22:38 -0800773{
Alexander Duyck047e0032009-10-27 15:49:27 +0000774 struct igb_adapter *adapter = q_vector->adapter;
Auke Kok9d5c8242008-01-24 02:22:38 -0800775 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck047e0032009-10-27 15:49:27 +0000776 int rx_queue = IGB_N0_QUEUE;
777 int tx_queue = IGB_N0_QUEUE;
Alexander Duyck4be000c2011-08-26 07:45:52 +0000778 u32 msixbm = 0;
Alexander Duyck047e0032009-10-27 15:49:27 +0000779
Alexander Duyck0ba82992011-08-26 07:45:47 +0000780 if (q_vector->rx.ring)
781 rx_queue = q_vector->rx.ring->reg_idx;
782 if (q_vector->tx.ring)
783 tx_queue = q_vector->tx.ring->reg_idx;
Alexander Duyck2d064c02008-07-08 15:10:12 -0700784
785 switch (hw->mac.type) {
786 case e1000_82575:
Auke Kok9d5c8242008-01-24 02:22:38 -0800787 /* The 82575 assigns vectors using a bitmask, which matches the
788 bitmask for the EICR/EIMS/EIMC registers. To assign one
789 or more queues to a vector, we write the appropriate bits
790 into the MSIXBM register for that vector. */
Alexander Duyck047e0032009-10-27 15:49:27 +0000791 if (rx_queue > IGB_N0_QUEUE)
Auke Kok9d5c8242008-01-24 02:22:38 -0800792 msixbm = E1000_EICR_RX_QUEUE0 << rx_queue;
Alexander Duyck047e0032009-10-27 15:49:27 +0000793 if (tx_queue > IGB_N0_QUEUE)
Auke Kok9d5c8242008-01-24 02:22:38 -0800794 msixbm |= E1000_EICR_TX_QUEUE0 << tx_queue;
Alexander Duyckfeeb2722010-02-03 21:59:51 +0000795 if (!adapter->msix_entries && msix_vector == 0)
796 msixbm |= E1000_EIMS_OTHER;
Auke Kok9d5c8242008-01-24 02:22:38 -0800797 array_wr32(E1000_MSIXBM(0), msix_vector, msixbm);
Alexander Duyck047e0032009-10-27 15:49:27 +0000798 q_vector->eims_value = msixbm;
Alexander Duyck2d064c02008-07-08 15:10:12 -0700799 break;
800 case e1000_82576:
Alexander Duyck4be000c2011-08-26 07:45:52 +0000801 /*
802 * 82576 uses a table that essentially consists of 2 columns
803 * with 8 rows. The ordering is column-major so we use the
804 * lower 3 bits as the row index, and the 4th bit as the
805 * column offset.
806 */
807 if (rx_queue > IGB_N0_QUEUE)
808 igb_write_ivar(hw, msix_vector,
809 rx_queue & 0x7,
810 (rx_queue & 0x8) << 1);
811 if (tx_queue > IGB_N0_QUEUE)
812 igb_write_ivar(hw, msix_vector,
813 tx_queue & 0x7,
814 ((tx_queue & 0x8) << 1) + 8);
Alexander Duyck047e0032009-10-27 15:49:27 +0000815 q_vector->eims_value = 1 << msix_vector;
Alexander Duyck2d064c02008-07-08 15:10:12 -0700816 break;
Alexander Duyck55cac242009-11-19 12:42:21 +0000817 case e1000_82580:
Alexander Duyckd2ba2ed2010-03-22 14:08:06 +0000818 case e1000_i350:
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +0000819 case e1000_i210:
820 case e1000_i211:
Alexander Duyck4be000c2011-08-26 07:45:52 +0000821 /*
822 * On 82580 and newer adapters the scheme is similar to 82576
823 * however instead of ordering column-major we have things
824 * ordered row-major. So we traverse the table by using
825 * bit 0 as the column offset, and the remaining bits as the
826 * row index.
827 */
828 if (rx_queue > IGB_N0_QUEUE)
829 igb_write_ivar(hw, msix_vector,
830 rx_queue >> 1,
831 (rx_queue & 0x1) << 4);
832 if (tx_queue > IGB_N0_QUEUE)
833 igb_write_ivar(hw, msix_vector,
834 tx_queue >> 1,
835 ((tx_queue & 0x1) << 4) + 8);
Alexander Duyck55cac242009-11-19 12:42:21 +0000836 q_vector->eims_value = 1 << msix_vector;
837 break;
Alexander Duyck2d064c02008-07-08 15:10:12 -0700838 default:
839 BUG();
840 break;
841 }
Alexander Duyck26b39272010-02-17 01:00:41 +0000842
843 /* add q_vector eims value to global eims_enable_mask */
844 adapter->eims_enable_mask |= q_vector->eims_value;
845
846 /* configure q_vector to set itr on first interrupt */
847 q_vector->set_itr = 1;
Auke Kok9d5c8242008-01-24 02:22:38 -0800848}
849
850/**
851 * igb_configure_msix - Configure MSI-X hardware
852 *
853 * igb_configure_msix sets up the hardware to properly
854 * generate MSI-X interrupts.
855 **/
856static void igb_configure_msix(struct igb_adapter *adapter)
857{
858 u32 tmp;
859 int i, vector = 0;
860 struct e1000_hw *hw = &adapter->hw;
861
862 adapter->eims_enable_mask = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -0800863
864 /* set vector for other causes, i.e. link changes */
Alexander Duyck2d064c02008-07-08 15:10:12 -0700865 switch (hw->mac.type) {
866 case e1000_82575:
Auke Kok9d5c8242008-01-24 02:22:38 -0800867 tmp = rd32(E1000_CTRL_EXT);
868 /* enable MSI-X PBA support*/
869 tmp |= E1000_CTRL_EXT_PBA_CLR;
870
871 /* Auto-Mask interrupts upon ICR read. */
872 tmp |= E1000_CTRL_EXT_EIAME;
873 tmp |= E1000_CTRL_EXT_IRCA;
874
875 wr32(E1000_CTRL_EXT, tmp);
Alexander Duyck047e0032009-10-27 15:49:27 +0000876
877 /* enable msix_other interrupt */
878 array_wr32(E1000_MSIXBM(0), vector++,
879 E1000_EIMS_OTHER);
PJ Waskiewicz844290e2008-06-27 11:00:39 -0700880 adapter->eims_other = E1000_EIMS_OTHER;
Auke Kok9d5c8242008-01-24 02:22:38 -0800881
Alexander Duyck2d064c02008-07-08 15:10:12 -0700882 break;
883
884 case e1000_82576:
Alexander Duyck55cac242009-11-19 12:42:21 +0000885 case e1000_82580:
Alexander Duyckd2ba2ed2010-03-22 14:08:06 +0000886 case e1000_i350:
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +0000887 case e1000_i210:
888 case e1000_i211:
Alexander Duyck047e0032009-10-27 15:49:27 +0000889 /* Turn on MSI-X capability first, or our settings
890 * won't stick. And it will take days to debug. */
891 wr32(E1000_GPIE, E1000_GPIE_MSIX_MODE |
892 E1000_GPIE_PBA | E1000_GPIE_EIAME |
893 E1000_GPIE_NSICR);
Alexander Duyck2d064c02008-07-08 15:10:12 -0700894
Alexander Duyck047e0032009-10-27 15:49:27 +0000895 /* enable msix_other interrupt */
896 adapter->eims_other = 1 << vector;
897 tmp = (vector++ | E1000_IVAR_VALID) << 8;
898
899 wr32(E1000_IVAR_MISC, tmp);
Alexander Duyck2d064c02008-07-08 15:10:12 -0700900 break;
901 default:
902 /* do nothing, since nothing else supports MSI-X */
903 break;
904 } /* switch (hw->mac.type) */
Alexander Duyck047e0032009-10-27 15:49:27 +0000905
906 adapter->eims_enable_mask |= adapter->eims_other;
907
Alexander Duyck26b39272010-02-17 01:00:41 +0000908 for (i = 0; i < adapter->num_q_vectors; i++)
909 igb_assign_vector(adapter->q_vector[i], vector++);
Alexander Duyck047e0032009-10-27 15:49:27 +0000910
Auke Kok9d5c8242008-01-24 02:22:38 -0800911 wrfl();
912}
913
914/**
915 * igb_request_msix - Initialize MSI-X interrupts
916 *
917 * igb_request_msix allocates MSI-X vectors and requests interrupts from the
918 * kernel.
919 **/
920static int igb_request_msix(struct igb_adapter *adapter)
921{
922 struct net_device *netdev = adapter->netdev;
Alexander Duyck047e0032009-10-27 15:49:27 +0000923 struct e1000_hw *hw = &adapter->hw;
Stefan Assmann52285b72012-12-04 06:00:17 +0000924 int i, err = 0, vector = 0, free_vector = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -0800925
Auke Kok9d5c8242008-01-24 02:22:38 -0800926 err = request_irq(adapter->msix_entries[vector].vector,
Joe Perchesa0607fd2009-11-18 23:29:17 -0800927 igb_msix_other, 0, netdev->name, adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -0800928 if (err)
Stefan Assmann52285b72012-12-04 06:00:17 +0000929 goto err_out;
Alexander Duyck047e0032009-10-27 15:49:27 +0000930
931 for (i = 0; i < adapter->num_q_vectors; i++) {
932 struct igb_q_vector *q_vector = adapter->q_vector[i];
933
Stefan Assmann52285b72012-12-04 06:00:17 +0000934 vector++;
935
Alexander Duyck047e0032009-10-27 15:49:27 +0000936 q_vector->itr_register = hw->hw_addr + E1000_EITR(vector);
937
Alexander Duyck0ba82992011-08-26 07:45:47 +0000938 if (q_vector->rx.ring && q_vector->tx.ring)
Alexander Duyck047e0032009-10-27 15:49:27 +0000939 sprintf(q_vector->name, "%s-TxRx-%u", netdev->name,
Alexander Duyck0ba82992011-08-26 07:45:47 +0000940 q_vector->rx.ring->queue_index);
941 else if (q_vector->tx.ring)
Alexander Duyck047e0032009-10-27 15:49:27 +0000942 sprintf(q_vector->name, "%s-tx-%u", netdev->name,
Alexander Duyck0ba82992011-08-26 07:45:47 +0000943 q_vector->tx.ring->queue_index);
944 else if (q_vector->rx.ring)
Alexander Duyck047e0032009-10-27 15:49:27 +0000945 sprintf(q_vector->name, "%s-rx-%u", netdev->name,
Alexander Duyck0ba82992011-08-26 07:45:47 +0000946 q_vector->rx.ring->queue_index);
Alexander Duyck047e0032009-10-27 15:49:27 +0000947 else
948 sprintf(q_vector->name, "%s-unused", netdev->name);
949
950 err = request_irq(adapter->msix_entries[vector].vector,
Joe Perchesa0607fd2009-11-18 23:29:17 -0800951 igb_msix_ring, 0, q_vector->name,
Alexander Duyck047e0032009-10-27 15:49:27 +0000952 q_vector);
953 if (err)
Stefan Assmann52285b72012-12-04 06:00:17 +0000954 goto err_free;
Alexander Duyck047e0032009-10-27 15:49:27 +0000955 }
Auke Kok9d5c8242008-01-24 02:22:38 -0800956
Auke Kok9d5c8242008-01-24 02:22:38 -0800957 igb_configure_msix(adapter);
958 return 0;
Stefan Assmann52285b72012-12-04 06:00:17 +0000959
960err_free:
961 /* free already assigned IRQs */
962 free_irq(adapter->msix_entries[free_vector++].vector, adapter);
963
964 vector--;
965 for (i = 0; i < vector; i++) {
966 free_irq(adapter->msix_entries[free_vector++].vector,
967 adapter->q_vector[i]);
968 }
969err_out:
Auke Kok9d5c8242008-01-24 02:22:38 -0800970 return err;
971}
972
973static void igb_reset_interrupt_capability(struct igb_adapter *adapter)
974{
975 if (adapter->msix_entries) {
976 pci_disable_msix(adapter->pdev);
977 kfree(adapter->msix_entries);
978 adapter->msix_entries = NULL;
Alexander Duyck047e0032009-10-27 15:49:27 +0000979 } else if (adapter->flags & IGB_FLAG_HAS_MSI) {
Auke Kok9d5c8242008-01-24 02:22:38 -0800980 pci_disable_msi(adapter->pdev);
Alexander Duyck047e0032009-10-27 15:49:27 +0000981 }
Auke Kok9d5c8242008-01-24 02:22:38 -0800982}
983
Alexander Duyck047e0032009-10-27 15:49:27 +0000984/**
Alexander Duyck5536d212012-09-25 00:31:17 +0000985 * igb_free_q_vector - Free memory allocated for specific interrupt vector
986 * @adapter: board private structure to initialize
987 * @v_idx: Index of vector to be freed
988 *
989 * This function frees the memory allocated to the q_vector. In addition if
990 * NAPI is enabled it will delete any references to the NAPI struct prior
991 * to freeing the q_vector.
992 **/
993static void igb_free_q_vector(struct igb_adapter *adapter, int v_idx)
994{
995 struct igb_q_vector *q_vector = adapter->q_vector[v_idx];
996
997 if (q_vector->tx.ring)
998 adapter->tx_ring[q_vector->tx.ring->queue_index] = NULL;
999
1000 if (q_vector->rx.ring)
1001 adapter->tx_ring[q_vector->rx.ring->queue_index] = NULL;
1002
1003 adapter->q_vector[v_idx] = NULL;
1004 netif_napi_del(&q_vector->napi);
1005
1006 /*
1007 * ixgbe_get_stats64() might access the rings on this vector,
1008 * we must wait a grace period before freeing it.
1009 */
1010 kfree_rcu(q_vector, rcu);
1011}
1012
1013/**
Alexander Duyck047e0032009-10-27 15:49:27 +00001014 * igb_free_q_vectors - Free memory allocated for interrupt vectors
1015 * @adapter: board private structure to initialize
1016 *
1017 * This function frees the memory allocated to the q_vectors. In addition if
1018 * NAPI is enabled it will delete any references to the NAPI struct prior
1019 * to freeing the q_vector.
1020 **/
1021static void igb_free_q_vectors(struct igb_adapter *adapter)
1022{
Alexander Duyck5536d212012-09-25 00:31:17 +00001023 int v_idx = adapter->num_q_vectors;
Alexander Duyck047e0032009-10-27 15:49:27 +00001024
Alexander Duyck5536d212012-09-25 00:31:17 +00001025 adapter->num_tx_queues = 0;
1026 adapter->num_rx_queues = 0;
Alexander Duyck047e0032009-10-27 15:49:27 +00001027 adapter->num_q_vectors = 0;
Alexander Duyck5536d212012-09-25 00:31:17 +00001028
1029 while (v_idx--)
1030 igb_free_q_vector(adapter, v_idx);
Alexander Duyck047e0032009-10-27 15:49:27 +00001031}
1032
1033/**
1034 * igb_clear_interrupt_scheme - reset the device to a state of no interrupts
1035 *
1036 * This function resets the device so that it has 0 rx queues, tx queues, and
1037 * MSI-X interrupts allocated.
1038 */
1039static void igb_clear_interrupt_scheme(struct igb_adapter *adapter)
1040{
Alexander Duyck047e0032009-10-27 15:49:27 +00001041 igb_free_q_vectors(adapter);
1042 igb_reset_interrupt_capability(adapter);
1043}
Auke Kok9d5c8242008-01-24 02:22:38 -08001044
1045/**
1046 * igb_set_interrupt_capability - set MSI or MSI-X if supported
1047 *
1048 * Attempt to configure interrupts using the best available
1049 * capabilities of the hardware and kernel.
1050 **/
Stefan Assmann53c7d062012-12-04 06:00:12 +00001051static void igb_set_interrupt_capability(struct igb_adapter *adapter, bool msix)
Auke Kok9d5c8242008-01-24 02:22:38 -08001052{
1053 int err;
1054 int numvecs, i;
1055
Stefan Assmann53c7d062012-12-04 06:00:12 +00001056 if (!msix)
1057 goto msi_only;
1058
Alexander Duyck83b71802009-02-06 23:15:45 +00001059 /* Number of supported queues. */
Alexander Duycka99955f2009-11-12 18:37:19 +00001060 adapter->num_rx_queues = adapter->rss_queues;
Greg Rose5fa85172010-07-01 13:38:16 +00001061 if (adapter->vfs_allocated_count)
1062 adapter->num_tx_queues = 1;
1063 else
1064 adapter->num_tx_queues = adapter->rss_queues;
Alexander Duyck83b71802009-02-06 23:15:45 +00001065
Alexander Duyck047e0032009-10-27 15:49:27 +00001066 /* start with one vector for every rx queue */
1067 numvecs = adapter->num_rx_queues;
1068
Daniel Mack3ad2f3f2010-02-03 08:01:28 +08001069 /* if tx handler is separate add 1 for every tx queue */
Alexander Duycka99955f2009-11-12 18:37:19 +00001070 if (!(adapter->flags & IGB_FLAG_QUEUE_PAIRS))
1071 numvecs += adapter->num_tx_queues;
Alexander Duyck047e0032009-10-27 15:49:27 +00001072
1073 /* store the number of vectors reserved for queues */
1074 adapter->num_q_vectors = numvecs;
1075
1076 /* add 1 vector for link status interrupts */
1077 numvecs++;
Auke Kok9d5c8242008-01-24 02:22:38 -08001078 adapter->msix_entries = kcalloc(numvecs, sizeof(struct msix_entry),
1079 GFP_KERNEL);
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00001080
Auke Kok9d5c8242008-01-24 02:22:38 -08001081 if (!adapter->msix_entries)
1082 goto msi_only;
1083
1084 for (i = 0; i < numvecs; i++)
1085 adapter->msix_entries[i].entry = i;
1086
1087 err = pci_enable_msix(adapter->pdev,
1088 adapter->msix_entries,
1089 numvecs);
1090 if (err == 0)
Alexander Duyck0c2cc022012-09-25 00:31:22 +00001091 return;
Auke Kok9d5c8242008-01-24 02:22:38 -08001092
1093 igb_reset_interrupt_capability(adapter);
1094
1095 /* If we can't do MSI-X, try MSI */
1096msi_only:
Alexander Duyck2a3abf62009-04-07 14:37:52 +00001097#ifdef CONFIG_PCI_IOV
1098 /* disable SR-IOV for non MSI-X configurations */
1099 if (adapter->vf_data) {
1100 struct e1000_hw *hw = &adapter->hw;
1101 /* disable iov and allow time for transactions to clear */
1102 pci_disable_sriov(adapter->pdev);
1103 msleep(500);
1104
1105 kfree(adapter->vf_data);
1106 adapter->vf_data = NULL;
1107 wr32(E1000_IOVCTL, E1000_IOVCTL_REUSE_VFQ);
Jesse Brandeburg945a5152011-07-20 00:56:21 +00001108 wrfl();
Alexander Duyck2a3abf62009-04-07 14:37:52 +00001109 msleep(100);
1110 dev_info(&adapter->pdev->dev, "IOV Disabled\n");
1111 }
1112#endif
Alexander Duyck4fc82ad2009-10-27 23:45:42 +00001113 adapter->vfs_allocated_count = 0;
Alexander Duycka99955f2009-11-12 18:37:19 +00001114 adapter->rss_queues = 1;
Alexander Duyck4fc82ad2009-10-27 23:45:42 +00001115 adapter->flags |= IGB_FLAG_QUEUE_PAIRS;
Auke Kok9d5c8242008-01-24 02:22:38 -08001116 adapter->num_rx_queues = 1;
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07001117 adapter->num_tx_queues = 1;
Alexander Duyck047e0032009-10-27 15:49:27 +00001118 adapter->num_q_vectors = 1;
Auke Kok9d5c8242008-01-24 02:22:38 -08001119 if (!pci_enable_msi(adapter->pdev))
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07001120 adapter->flags |= IGB_FLAG_HAS_MSI;
Auke Kok9d5c8242008-01-24 02:22:38 -08001121}
1122
Alexander Duyck5536d212012-09-25 00:31:17 +00001123static void igb_add_ring(struct igb_ring *ring,
1124 struct igb_ring_container *head)
1125{
1126 head->ring = ring;
1127 head->count++;
1128}
1129
1130/**
1131 * igb_alloc_q_vector - Allocate memory for a single interrupt vector
1132 * @adapter: board private structure to initialize
1133 * @v_count: q_vectors allocated on adapter, used for ring interleaving
1134 * @v_idx: index of vector in adapter struct
1135 * @txr_count: total number of Tx rings to allocate
1136 * @txr_idx: index of first Tx ring to allocate
1137 * @rxr_count: total number of Rx rings to allocate
1138 * @rxr_idx: index of first Rx ring to allocate
1139 *
1140 * We allocate one q_vector. If allocation fails we return -ENOMEM.
1141 **/
1142static int igb_alloc_q_vector(struct igb_adapter *adapter,
1143 int v_count, int v_idx,
1144 int txr_count, int txr_idx,
1145 int rxr_count, int rxr_idx)
1146{
1147 struct igb_q_vector *q_vector;
1148 struct igb_ring *ring;
1149 int ring_count, size;
1150
1151 /* igb only supports 1 Tx and/or 1 Rx queue per vector */
1152 if (txr_count > 1 || rxr_count > 1)
1153 return -ENOMEM;
1154
1155 ring_count = txr_count + rxr_count;
1156 size = sizeof(struct igb_q_vector) +
1157 (sizeof(struct igb_ring) * ring_count);
1158
1159 /* allocate q_vector and rings */
1160 q_vector = kzalloc(size, GFP_KERNEL);
1161 if (!q_vector)
1162 return -ENOMEM;
1163
1164 /* initialize NAPI */
1165 netif_napi_add(adapter->netdev, &q_vector->napi,
1166 igb_poll, 64);
1167
1168 /* tie q_vector and adapter together */
1169 adapter->q_vector[v_idx] = q_vector;
1170 q_vector->adapter = adapter;
1171
1172 /* initialize work limits */
1173 q_vector->tx.work_limit = adapter->tx_work_limit;
1174
1175 /* initialize ITR configuration */
1176 q_vector->itr_register = adapter->hw.hw_addr + E1000_EITR(0);
1177 q_vector->itr_val = IGB_START_ITR;
1178
1179 /* initialize pointer to rings */
1180 ring = q_vector->ring;
1181
1182 if (txr_count) {
1183 /* assign generic ring traits */
1184 ring->dev = &adapter->pdev->dev;
1185 ring->netdev = adapter->netdev;
1186
1187 /* configure backlink on ring */
1188 ring->q_vector = q_vector;
1189
1190 /* update q_vector Tx values */
1191 igb_add_ring(ring, &q_vector->tx);
1192
1193 /* For 82575, context index must be unique per ring. */
1194 if (adapter->hw.mac.type == e1000_82575)
1195 set_bit(IGB_RING_FLAG_TX_CTX_IDX, &ring->flags);
1196
1197 /* apply Tx specific ring traits */
1198 ring->count = adapter->tx_ring_count;
1199 ring->queue_index = txr_idx;
1200
1201 /* assign ring to adapter */
1202 adapter->tx_ring[txr_idx] = ring;
1203
1204 /* push pointer to next ring */
1205 ring++;
1206 }
1207
1208 if (rxr_count) {
1209 /* assign generic ring traits */
1210 ring->dev = &adapter->pdev->dev;
1211 ring->netdev = adapter->netdev;
1212
1213 /* configure backlink on ring */
1214 ring->q_vector = q_vector;
1215
1216 /* update q_vector Rx values */
1217 igb_add_ring(ring, &q_vector->rx);
1218
1219 /* set flag indicating ring supports SCTP checksum offload */
1220 if (adapter->hw.mac.type >= e1000_82576)
1221 set_bit(IGB_RING_FLAG_RX_SCTP_CSUM, &ring->flags);
1222
1223 /*
1224 * On i350, i210, and i211, loopback VLAN packets
1225 * have the tag byte-swapped.
1226 * */
1227 if (adapter->hw.mac.type >= e1000_i350)
1228 set_bit(IGB_RING_FLAG_RX_LB_VLAN_BSWAP, &ring->flags);
1229
1230 /* apply Rx specific ring traits */
1231 ring->count = adapter->rx_ring_count;
1232 ring->queue_index = rxr_idx;
1233
1234 /* assign ring to adapter */
1235 adapter->rx_ring[rxr_idx] = ring;
1236 }
1237
1238 return 0;
1239}
1240
1241
Auke Kok9d5c8242008-01-24 02:22:38 -08001242/**
Alexander Duyck047e0032009-10-27 15:49:27 +00001243 * igb_alloc_q_vectors - Allocate memory for interrupt vectors
1244 * @adapter: board private structure to initialize
1245 *
1246 * We allocate one q_vector per queue interrupt. If allocation fails we
1247 * return -ENOMEM.
1248 **/
1249static int igb_alloc_q_vectors(struct igb_adapter *adapter)
1250{
Alexander Duyck5536d212012-09-25 00:31:17 +00001251 int q_vectors = adapter->num_q_vectors;
1252 int rxr_remaining = adapter->num_rx_queues;
1253 int txr_remaining = adapter->num_tx_queues;
1254 int rxr_idx = 0, txr_idx = 0, v_idx = 0;
1255 int err;
Alexander Duyck047e0032009-10-27 15:49:27 +00001256
Alexander Duyck5536d212012-09-25 00:31:17 +00001257 if (q_vectors >= (rxr_remaining + txr_remaining)) {
1258 for (; rxr_remaining; v_idx++) {
1259 err = igb_alloc_q_vector(adapter, q_vectors, v_idx,
1260 0, 0, 1, rxr_idx);
1261
1262 if (err)
1263 goto err_out;
1264
1265 /* update counts and index */
1266 rxr_remaining--;
1267 rxr_idx++;
1268 }
1269 }
1270
1271 for (; v_idx < q_vectors; v_idx++) {
1272 int rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - v_idx);
1273 int tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - v_idx);
1274 err = igb_alloc_q_vector(adapter, q_vectors, v_idx,
1275 tqpv, txr_idx, rqpv, rxr_idx);
1276
1277 if (err)
Alexander Duyck047e0032009-10-27 15:49:27 +00001278 goto err_out;
Alexander Duyck5536d212012-09-25 00:31:17 +00001279
1280 /* update counts and index */
1281 rxr_remaining -= rqpv;
1282 txr_remaining -= tqpv;
1283 rxr_idx++;
1284 txr_idx++;
Alexander Duyck047e0032009-10-27 15:49:27 +00001285 }
Alexander Duyck81c2fc22011-08-26 07:45:20 +00001286
Alexander Duyck047e0032009-10-27 15:49:27 +00001287 return 0;
1288
1289err_out:
Alexander Duyck5536d212012-09-25 00:31:17 +00001290 adapter->num_tx_queues = 0;
1291 adapter->num_rx_queues = 0;
1292 adapter->num_q_vectors = 0;
1293
1294 while (v_idx--)
1295 igb_free_q_vector(adapter, v_idx);
1296
Alexander Duyck047e0032009-10-27 15:49:27 +00001297 return -ENOMEM;
1298}
1299
Alexander Duyck047e0032009-10-27 15:49:27 +00001300/**
1301 * igb_init_interrupt_scheme - initialize interrupts, allocate queues/vectors
1302 *
1303 * This function initializes the interrupts and allocates all of the queues.
1304 **/
Stefan Assmann53c7d062012-12-04 06:00:12 +00001305static int igb_init_interrupt_scheme(struct igb_adapter *adapter, bool msix)
Alexander Duyck047e0032009-10-27 15:49:27 +00001306{
1307 struct pci_dev *pdev = adapter->pdev;
1308 int err;
1309
Stefan Assmann53c7d062012-12-04 06:00:12 +00001310 igb_set_interrupt_capability(adapter, msix);
Alexander Duyck047e0032009-10-27 15:49:27 +00001311
1312 err = igb_alloc_q_vectors(adapter);
1313 if (err) {
1314 dev_err(&pdev->dev, "Unable to allocate memory for vectors\n");
1315 goto err_alloc_q_vectors;
1316 }
1317
Alexander Duyck5536d212012-09-25 00:31:17 +00001318 igb_cache_ring_register(adapter);
Alexander Duyck047e0032009-10-27 15:49:27 +00001319
1320 return 0;
Alexander Duyck5536d212012-09-25 00:31:17 +00001321
Alexander Duyck047e0032009-10-27 15:49:27 +00001322err_alloc_q_vectors:
1323 igb_reset_interrupt_capability(adapter);
1324 return err;
1325}
1326
1327/**
Auke Kok9d5c8242008-01-24 02:22:38 -08001328 * igb_request_irq - initialize interrupts
1329 *
1330 * Attempts to configure interrupts using the best available
1331 * capabilities of the hardware and kernel.
1332 **/
1333static int igb_request_irq(struct igb_adapter *adapter)
1334{
1335 struct net_device *netdev = adapter->netdev;
Alexander Duyck047e0032009-10-27 15:49:27 +00001336 struct pci_dev *pdev = adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08001337 int err = 0;
1338
1339 if (adapter->msix_entries) {
1340 err = igb_request_msix(adapter);
PJ Waskiewicz844290e2008-06-27 11:00:39 -07001341 if (!err)
Auke Kok9d5c8242008-01-24 02:22:38 -08001342 goto request_done;
Auke Kok9d5c8242008-01-24 02:22:38 -08001343 /* fall back to MSI */
Alexander Duyck5536d212012-09-25 00:31:17 +00001344 igb_free_all_tx_resources(adapter);
1345 igb_free_all_rx_resources(adapter);
Stefan Assmann53c7d062012-12-04 06:00:12 +00001346
Alexander Duyck047e0032009-10-27 15:49:27 +00001347 igb_clear_interrupt_scheme(adapter);
Stefan Assmann53c7d062012-12-04 06:00:12 +00001348 err = igb_init_interrupt_scheme(adapter, false);
1349 if (err)
Alexander Duyck047e0032009-10-27 15:49:27 +00001350 goto request_done;
Stefan Assmann53c7d062012-12-04 06:00:12 +00001351
Alexander Duyck047e0032009-10-27 15:49:27 +00001352 igb_setup_all_tx_resources(adapter);
1353 igb_setup_all_rx_resources(adapter);
Stefan Assmann53c7d062012-12-04 06:00:12 +00001354 igb_configure(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001355 }
PJ Waskiewicz844290e2008-06-27 11:00:39 -07001356
Alexander Duyckc74d5882011-08-26 07:46:45 +00001357 igb_assign_vector(adapter->q_vector[0], 0);
1358
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07001359 if (adapter->flags & IGB_FLAG_HAS_MSI) {
Alexander Duyckc74d5882011-08-26 07:46:45 +00001360 err = request_irq(pdev->irq, igb_intr_msi, 0,
Alexander Duyck047e0032009-10-27 15:49:27 +00001361 netdev->name, adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001362 if (!err)
1363 goto request_done;
Alexander Duyck047e0032009-10-27 15:49:27 +00001364
Auke Kok9d5c8242008-01-24 02:22:38 -08001365 /* fall back to legacy interrupts */
1366 igb_reset_interrupt_capability(adapter);
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07001367 adapter->flags &= ~IGB_FLAG_HAS_MSI;
Auke Kok9d5c8242008-01-24 02:22:38 -08001368 }
1369
Alexander Duyckc74d5882011-08-26 07:46:45 +00001370 err = request_irq(pdev->irq, igb_intr, IRQF_SHARED,
Alexander Duyck047e0032009-10-27 15:49:27 +00001371 netdev->name, adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001372
Andy Gospodarek6cb5e572008-02-15 14:05:25 -08001373 if (err)
Alexander Duyckc74d5882011-08-26 07:46:45 +00001374 dev_err(&pdev->dev, "Error %d getting interrupt\n",
Auke Kok9d5c8242008-01-24 02:22:38 -08001375 err);
Auke Kok9d5c8242008-01-24 02:22:38 -08001376
1377request_done:
1378 return err;
1379}
1380
1381static void igb_free_irq(struct igb_adapter *adapter)
1382{
Auke Kok9d5c8242008-01-24 02:22:38 -08001383 if (adapter->msix_entries) {
1384 int vector = 0, i;
1385
Alexander Duyck047e0032009-10-27 15:49:27 +00001386 free_irq(adapter->msix_entries[vector++].vector, adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001387
Alexander Duyck0d1ae7f2011-08-26 07:46:34 +00001388 for (i = 0; i < adapter->num_q_vectors; i++)
Alexander Duyck047e0032009-10-27 15:49:27 +00001389 free_irq(adapter->msix_entries[vector++].vector,
Alexander Duyck0d1ae7f2011-08-26 07:46:34 +00001390 adapter->q_vector[i]);
Alexander Duyck047e0032009-10-27 15:49:27 +00001391 } else {
1392 free_irq(adapter->pdev->irq, adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001393 }
Auke Kok9d5c8242008-01-24 02:22:38 -08001394}
1395
1396/**
1397 * igb_irq_disable - Mask off interrupt generation on the NIC
1398 * @adapter: board private structure
1399 **/
1400static void igb_irq_disable(struct igb_adapter *adapter)
1401{
1402 struct e1000_hw *hw = &adapter->hw;
1403
Alexander Duyck25568a52009-10-27 23:49:59 +00001404 /*
1405 * we need to be careful when disabling interrupts. The VFs are also
1406 * mapped into these registers and so clearing the bits can cause
1407 * issues on the VF drivers so we only need to clear what we set
1408 */
Auke Kok9d5c8242008-01-24 02:22:38 -08001409 if (adapter->msix_entries) {
Alexander Duyck2dfd1212009-09-03 14:49:15 +00001410 u32 regval = rd32(E1000_EIAM);
1411 wr32(E1000_EIAM, regval & ~adapter->eims_enable_mask);
1412 wr32(E1000_EIMC, adapter->eims_enable_mask);
1413 regval = rd32(E1000_EIAC);
1414 wr32(E1000_EIAC, regval & ~adapter->eims_enable_mask);
Auke Kok9d5c8242008-01-24 02:22:38 -08001415 }
PJ Waskiewicz844290e2008-06-27 11:00:39 -07001416
1417 wr32(E1000_IAM, 0);
Auke Kok9d5c8242008-01-24 02:22:38 -08001418 wr32(E1000_IMC, ~0);
1419 wrfl();
Emil Tantilov81a61852010-08-02 14:40:52 +00001420 if (adapter->msix_entries) {
1421 int i;
1422 for (i = 0; i < adapter->num_q_vectors; i++)
1423 synchronize_irq(adapter->msix_entries[i].vector);
1424 } else {
1425 synchronize_irq(adapter->pdev->irq);
1426 }
Auke Kok9d5c8242008-01-24 02:22:38 -08001427}
1428
1429/**
1430 * igb_irq_enable - Enable default interrupt generation settings
1431 * @adapter: board private structure
1432 **/
1433static void igb_irq_enable(struct igb_adapter *adapter)
1434{
1435 struct e1000_hw *hw = &adapter->hw;
1436
1437 if (adapter->msix_entries) {
Alexander Duyck06218a82011-08-26 07:46:55 +00001438 u32 ims = E1000_IMS_LSC | E1000_IMS_DOUTSYNC | E1000_IMS_DRSTA;
Alexander Duyck2dfd1212009-09-03 14:49:15 +00001439 u32 regval = rd32(E1000_EIAC);
1440 wr32(E1000_EIAC, regval | adapter->eims_enable_mask);
1441 regval = rd32(E1000_EIAM);
1442 wr32(E1000_EIAM, regval | adapter->eims_enable_mask);
PJ Waskiewicz844290e2008-06-27 11:00:39 -07001443 wr32(E1000_EIMS, adapter->eims_enable_mask);
Alexander Duyck25568a52009-10-27 23:49:59 +00001444 if (adapter->vfs_allocated_count) {
Alexander Duyck4ae196d2009-02-19 20:40:07 -08001445 wr32(E1000_MBVFIMR, 0xFF);
Alexander Duyck25568a52009-10-27 23:49:59 +00001446 ims |= E1000_IMS_VMMB;
1447 }
1448 wr32(E1000_IMS, ims);
PJ Waskiewicz844290e2008-06-27 11:00:39 -07001449 } else {
Alexander Duyck55cac242009-11-19 12:42:21 +00001450 wr32(E1000_IMS, IMS_ENABLE_MASK |
1451 E1000_IMS_DRSTA);
1452 wr32(E1000_IAM, IMS_ENABLE_MASK |
1453 E1000_IMS_DRSTA);
PJ Waskiewicz844290e2008-06-27 11:00:39 -07001454 }
Auke Kok9d5c8242008-01-24 02:22:38 -08001455}
1456
1457static void igb_update_mng_vlan(struct igb_adapter *adapter)
1458{
Alexander Duyck51466232009-10-27 23:47:35 +00001459 struct e1000_hw *hw = &adapter->hw;
Auke Kok9d5c8242008-01-24 02:22:38 -08001460 u16 vid = adapter->hw.mng_cookie.vlan_id;
1461 u16 old_vid = adapter->mng_vlan_id;
Auke Kok9d5c8242008-01-24 02:22:38 -08001462
Alexander Duyck51466232009-10-27 23:47:35 +00001463 if (hw->mng_cookie.status & E1000_MNG_DHCP_COOKIE_STATUS_VLAN) {
1464 /* add VID to filter table */
1465 igb_vfta_set(hw, vid, true);
1466 adapter->mng_vlan_id = vid;
1467 } else {
1468 adapter->mng_vlan_id = IGB_MNG_VLAN_NONE;
1469 }
1470
1471 if ((old_vid != (u16)IGB_MNG_VLAN_NONE) &&
1472 (vid != old_vid) &&
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00001473 !test_bit(old_vid, adapter->active_vlans)) {
Alexander Duyck51466232009-10-27 23:47:35 +00001474 /* remove VID from filter table */
1475 igb_vfta_set(hw, old_vid, false);
Auke Kok9d5c8242008-01-24 02:22:38 -08001476 }
1477}
1478
1479/**
1480 * igb_release_hw_control - release control of the h/w to f/w
1481 * @adapter: address of board private structure
1482 *
1483 * igb_release_hw_control resets CTRL_EXT:DRV_LOAD bit.
1484 * For ASF and Pass Through versions of f/w this means that the
1485 * driver is no longer loaded.
1486 *
1487 **/
1488static void igb_release_hw_control(struct igb_adapter *adapter)
1489{
1490 struct e1000_hw *hw = &adapter->hw;
1491 u32 ctrl_ext;
1492
1493 /* Let firmware take over control of h/w */
1494 ctrl_ext = rd32(E1000_CTRL_EXT);
1495 wr32(E1000_CTRL_EXT,
1496 ctrl_ext & ~E1000_CTRL_EXT_DRV_LOAD);
1497}
1498
Auke Kok9d5c8242008-01-24 02:22:38 -08001499/**
1500 * igb_get_hw_control - get control of the h/w from f/w
1501 * @adapter: address of board private structure
1502 *
1503 * igb_get_hw_control sets CTRL_EXT:DRV_LOAD bit.
1504 * For ASF and Pass Through versions of f/w this means that
1505 * the driver is loaded.
1506 *
1507 **/
1508static void igb_get_hw_control(struct igb_adapter *adapter)
1509{
1510 struct e1000_hw *hw = &adapter->hw;
1511 u32 ctrl_ext;
1512
1513 /* Let firmware know the driver has taken over */
1514 ctrl_ext = rd32(E1000_CTRL_EXT);
1515 wr32(E1000_CTRL_EXT,
1516 ctrl_ext | E1000_CTRL_EXT_DRV_LOAD);
1517}
1518
Auke Kok9d5c8242008-01-24 02:22:38 -08001519/**
1520 * igb_configure - configure the hardware for RX and TX
1521 * @adapter: private board structure
1522 **/
1523static void igb_configure(struct igb_adapter *adapter)
1524{
1525 struct net_device *netdev = adapter->netdev;
1526 int i;
1527
1528 igb_get_hw_control(adapter);
Alexander Duyckff41f8d2009-09-03 14:48:56 +00001529 igb_set_rx_mode(netdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08001530
1531 igb_restore_vlan(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001532
Alexander Duyck85b430b2009-10-27 15:50:29 +00001533 igb_setup_tctl(adapter);
Alexander Duyck06cf2662009-10-27 15:53:25 +00001534 igb_setup_mrqc(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001535 igb_setup_rctl(adapter);
Alexander Duyck85b430b2009-10-27 15:50:29 +00001536
1537 igb_configure_tx(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001538 igb_configure_rx(adapter);
Alexander Duyck662d7202008-06-27 11:00:29 -07001539
1540 igb_rx_fifo_flush_82575(&adapter->hw);
1541
Alexander Duyckc493ea42009-03-20 00:16:50 +00001542 /* call igb_desc_unused which always leaves
Auke Kok9d5c8242008-01-24 02:22:38 -08001543 * at least 1 descriptor unused to make sure
1544 * next_to_use != next_to_clean */
1545 for (i = 0; i < adapter->num_rx_queues; i++) {
Alexander Duyck3025a442010-02-17 01:02:39 +00001546 struct igb_ring *ring = adapter->rx_ring[i];
Alexander Duyckcd392f52011-08-26 07:43:59 +00001547 igb_alloc_rx_buffers(ring, igb_desc_unused(ring));
Auke Kok9d5c8242008-01-24 02:22:38 -08001548 }
Auke Kok9d5c8242008-01-24 02:22:38 -08001549}
1550
Nick Nunley88a268c2010-02-17 01:01:59 +00001551/**
1552 * igb_power_up_link - Power up the phy/serdes link
1553 * @adapter: address of board private structure
1554 **/
1555void igb_power_up_link(struct igb_adapter *adapter)
1556{
Akeem G. Abodunrin76886592012-07-17 04:51:18 +00001557 igb_reset_phy(&adapter->hw);
1558
Nick Nunley88a268c2010-02-17 01:01:59 +00001559 if (adapter->hw.phy.media_type == e1000_media_type_copper)
1560 igb_power_up_phy_copper(&adapter->hw);
1561 else
1562 igb_power_up_serdes_link_82575(&adapter->hw);
1563}
1564
1565/**
1566 * igb_power_down_link - Power down the phy/serdes link
1567 * @adapter: address of board private structure
1568 */
1569static void igb_power_down_link(struct igb_adapter *adapter)
1570{
1571 if (adapter->hw.phy.media_type == e1000_media_type_copper)
1572 igb_power_down_phy_copper_82575(&adapter->hw);
1573 else
1574 igb_shutdown_serdes_link_82575(&adapter->hw);
1575}
Auke Kok9d5c8242008-01-24 02:22:38 -08001576
1577/**
1578 * igb_up - Open the interface and prepare it to handle traffic
1579 * @adapter: board private structure
1580 **/
Auke Kok9d5c8242008-01-24 02:22:38 -08001581int igb_up(struct igb_adapter *adapter)
1582{
1583 struct e1000_hw *hw = &adapter->hw;
1584 int i;
1585
1586 /* hardware has been reset, we need to reload some things */
1587 igb_configure(adapter);
1588
1589 clear_bit(__IGB_DOWN, &adapter->state);
1590
Alexander Duyck0d1ae7f2011-08-26 07:46:34 +00001591 for (i = 0; i < adapter->num_q_vectors; i++)
1592 napi_enable(&(adapter->q_vector[i]->napi));
1593
PJ Waskiewicz844290e2008-06-27 11:00:39 -07001594 if (adapter->msix_entries)
Auke Kok9d5c8242008-01-24 02:22:38 -08001595 igb_configure_msix(adapter);
Alexander Duyckfeeb2722010-02-03 21:59:51 +00001596 else
1597 igb_assign_vector(adapter->q_vector[0], 0);
Auke Kok9d5c8242008-01-24 02:22:38 -08001598
1599 /* Clear any pending interrupts. */
1600 rd32(E1000_ICR);
1601 igb_irq_enable(adapter);
1602
Alexander Duyckd4960302009-10-27 15:53:45 +00001603 /* notify VFs that reset has been completed */
1604 if (adapter->vfs_allocated_count) {
1605 u32 reg_data = rd32(E1000_CTRL_EXT);
1606 reg_data |= E1000_CTRL_EXT_PFRSTD;
1607 wr32(E1000_CTRL_EXT, reg_data);
1608 }
1609
Jesse Brandeburg4cb9be72009-04-21 18:42:05 +00001610 netif_tx_start_all_queues(adapter->netdev);
1611
Alexander Duyck25568a52009-10-27 23:49:59 +00001612 /* start the watchdog. */
1613 hw->mac.get_link_status = 1;
1614 schedule_work(&adapter->watchdog_task);
1615
Auke Kok9d5c8242008-01-24 02:22:38 -08001616 return 0;
1617}
1618
1619void igb_down(struct igb_adapter *adapter)
1620{
Auke Kok9d5c8242008-01-24 02:22:38 -08001621 struct net_device *netdev = adapter->netdev;
Alexander Duyck330a6d62009-10-27 23:51:35 +00001622 struct e1000_hw *hw = &adapter->hw;
Auke Kok9d5c8242008-01-24 02:22:38 -08001623 u32 tctl, rctl;
1624 int i;
1625
1626 /* signal that we're down so the interrupt handler does not
1627 * reschedule our watchdog timer */
1628 set_bit(__IGB_DOWN, &adapter->state);
1629
1630 /* disable receives in the hardware */
1631 rctl = rd32(E1000_RCTL);
1632 wr32(E1000_RCTL, rctl & ~E1000_RCTL_EN);
1633 /* flush and sleep below */
1634
David S. Millerfd2ea0a2008-07-17 01:56:23 -07001635 netif_tx_stop_all_queues(netdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08001636
1637 /* disable transmits in the hardware */
1638 tctl = rd32(E1000_TCTL);
1639 tctl &= ~E1000_TCTL_EN;
1640 wr32(E1000_TCTL, tctl);
1641 /* flush both disables and wait for them to finish */
1642 wrfl();
1643 msleep(10);
1644
Alexander Duyck0d1ae7f2011-08-26 07:46:34 +00001645 for (i = 0; i < adapter->num_q_vectors; i++)
1646 napi_disable(&(adapter->q_vector[i]->napi));
Auke Kok9d5c8242008-01-24 02:22:38 -08001647
Auke Kok9d5c8242008-01-24 02:22:38 -08001648 igb_irq_disable(adapter);
1649
1650 del_timer_sync(&adapter->watchdog_timer);
1651 del_timer_sync(&adapter->phy_info_timer);
1652
Auke Kok9d5c8242008-01-24 02:22:38 -08001653 netif_carrier_off(netdev);
Alexander Duyck04fe6352009-02-06 23:22:32 +00001654
1655 /* record the stats before reset*/
Eric Dumazet12dcd862010-10-15 17:27:10 +00001656 spin_lock(&adapter->stats64_lock);
1657 igb_update_stats(adapter, &adapter->stats64);
1658 spin_unlock(&adapter->stats64_lock);
Alexander Duyck04fe6352009-02-06 23:22:32 +00001659
Auke Kok9d5c8242008-01-24 02:22:38 -08001660 adapter->link_speed = 0;
1661 adapter->link_duplex = 0;
1662
Jeff Kirsher30236822008-06-24 17:01:15 -07001663 if (!pci_channel_offline(adapter->pdev))
1664 igb_reset(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001665 igb_clean_all_tx_rings(adapter);
1666 igb_clean_all_rx_rings(adapter);
Alexander Duyck7e0e99e2009-05-21 13:06:56 +00001667#ifdef CONFIG_IGB_DCA
1668
1669 /* since we reset the hardware DCA settings were cleared */
1670 igb_setup_dca(adapter);
1671#endif
Auke Kok9d5c8242008-01-24 02:22:38 -08001672}
1673
1674void igb_reinit_locked(struct igb_adapter *adapter)
1675{
1676 WARN_ON(in_interrupt());
1677 while (test_and_set_bit(__IGB_RESETTING, &adapter->state))
1678 msleep(1);
1679 igb_down(adapter);
1680 igb_up(adapter);
1681 clear_bit(__IGB_RESETTING, &adapter->state);
1682}
1683
1684void igb_reset(struct igb_adapter *adapter)
1685{
Alexander Duyck090b1792009-10-27 23:51:55 +00001686 struct pci_dev *pdev = adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08001687 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck2d064c02008-07-08 15:10:12 -07001688 struct e1000_mac_info *mac = &hw->mac;
1689 struct e1000_fc_info *fc = &hw->fc;
Matthew Vickd48507f2012-11-08 04:03:58 +00001690 u32 pba = 0, tx_space, min_tx_space, min_rx_space, hwm;
Auke Kok9d5c8242008-01-24 02:22:38 -08001691
1692 /* Repartition Pba for greater than 9k mtu
1693 * To take effect CTRL.RST is required.
1694 */
Alexander Duyckfa4dfae2009-02-06 23:21:31 +00001695 switch (mac->type) {
Alexander Duyckd2ba2ed2010-03-22 14:08:06 +00001696 case e1000_i350:
Alexander Duyck55cac242009-11-19 12:42:21 +00001697 case e1000_82580:
1698 pba = rd32(E1000_RXPBS);
1699 pba = igb_rxpbs_adjust_82580(pba);
1700 break;
Alexander Duyckfa4dfae2009-02-06 23:21:31 +00001701 case e1000_82576:
Alexander Duyckd249be52009-10-27 23:46:38 +00001702 pba = rd32(E1000_RXPBS);
1703 pba &= E1000_RXPBS_SIZE_MASK_82576;
Alexander Duyckfa4dfae2009-02-06 23:21:31 +00001704 break;
1705 case e1000_82575:
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00001706 case e1000_i210:
1707 case e1000_i211:
Alexander Duyckfa4dfae2009-02-06 23:21:31 +00001708 default:
1709 pba = E1000_PBA_34K;
1710 break;
Alexander Duyck2d064c02008-07-08 15:10:12 -07001711 }
Auke Kok9d5c8242008-01-24 02:22:38 -08001712
Alexander Duyck2d064c02008-07-08 15:10:12 -07001713 if ((adapter->max_frame_size > ETH_FRAME_LEN + ETH_FCS_LEN) &&
1714 (mac->type < e1000_82576)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08001715 /* adjust PBA for jumbo frames */
1716 wr32(E1000_PBA, pba);
1717
1718 /* To maintain wire speed transmits, the Tx FIFO should be
1719 * large enough to accommodate two full transmit packets,
1720 * rounded up to the next 1KB and expressed in KB. Likewise,
1721 * the Rx FIFO should be large enough to accommodate at least
1722 * one full receive packet and is similarly rounded up and
1723 * expressed in KB. */
1724 pba = rd32(E1000_PBA);
1725 /* upper 16 bits has Tx packet buffer allocation size in KB */
1726 tx_space = pba >> 16;
1727 /* lower 16 bits has Rx packet buffer allocation size in KB */
1728 pba &= 0xffff;
1729 /* the tx fifo also stores 16 bytes of information about the tx
1730 * but don't include ethernet FCS because hardware appends it */
1731 min_tx_space = (adapter->max_frame_size +
Alexander Duyck85e8d002009-02-16 00:00:20 -08001732 sizeof(union e1000_adv_tx_desc) -
Auke Kok9d5c8242008-01-24 02:22:38 -08001733 ETH_FCS_LEN) * 2;
1734 min_tx_space = ALIGN(min_tx_space, 1024);
1735 min_tx_space >>= 10;
1736 /* software strips receive CRC, so leave room for it */
1737 min_rx_space = adapter->max_frame_size;
1738 min_rx_space = ALIGN(min_rx_space, 1024);
1739 min_rx_space >>= 10;
1740
1741 /* If current Tx allocation is less than the min Tx FIFO size,
1742 * and the min Tx FIFO size is less than the current Rx FIFO
1743 * allocation, take space away from current Rx allocation */
1744 if (tx_space < min_tx_space &&
1745 ((min_tx_space - tx_space) < pba)) {
1746 pba = pba - (min_tx_space - tx_space);
1747
1748 /* if short on rx space, rx wins and must trump tx
1749 * adjustment */
1750 if (pba < min_rx_space)
1751 pba = min_rx_space;
1752 }
Alexander Duyck2d064c02008-07-08 15:10:12 -07001753 wr32(E1000_PBA, pba);
Auke Kok9d5c8242008-01-24 02:22:38 -08001754 }
Auke Kok9d5c8242008-01-24 02:22:38 -08001755
1756 /* flow control settings */
1757 /* The high water mark must be low enough to fit one full frame
1758 * (or the size used for early receive) above it in the Rx FIFO.
1759 * Set it to the lower of:
1760 * - 90% of the Rx FIFO size, or
1761 * - the full Rx FIFO size minus one full frame */
1762 hwm = min(((pba << 10) * 9 / 10),
Alexander Duyck2d064c02008-07-08 15:10:12 -07001763 ((pba << 10) - 2 * adapter->max_frame_size));
Auke Kok9d5c8242008-01-24 02:22:38 -08001764
Matthew Vickd48507f2012-11-08 04:03:58 +00001765 fc->high_water = hwm & 0xFFFFFFF0; /* 16-byte granularity */
Alexander Duyckd405ea32009-12-23 13:21:27 +00001766 fc->low_water = fc->high_water - 16;
Auke Kok9d5c8242008-01-24 02:22:38 -08001767 fc->pause_time = 0xFFFF;
1768 fc->send_xon = 1;
Alexander Duyck0cce1192009-07-23 18:10:24 +00001769 fc->current_mode = fc->requested_mode;
Auke Kok9d5c8242008-01-24 02:22:38 -08001770
Alexander Duyck4ae196d2009-02-19 20:40:07 -08001771 /* disable receive for all VFs and wait one second */
1772 if (adapter->vfs_allocated_count) {
1773 int i;
1774 for (i = 0 ; i < adapter->vfs_allocated_count; i++)
Greg Rose8fa7e0f2010-11-06 05:43:21 +00001775 adapter->vf_data[i].flags &= IGB_VF_FLAG_PF_SET_MAC;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08001776
1777 /* ping all the active vfs to let them know we are going down */
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00001778 igb_ping_all_vfs(adapter);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08001779
1780 /* disable transmits and receives */
1781 wr32(E1000_VFRE, 0);
1782 wr32(E1000_VFTE, 0);
1783 }
1784
Auke Kok9d5c8242008-01-24 02:22:38 -08001785 /* Allow time for pending master requests to run */
Alexander Duyck330a6d62009-10-27 23:51:35 +00001786 hw->mac.ops.reset_hw(hw);
Auke Kok9d5c8242008-01-24 02:22:38 -08001787 wr32(E1000_WUC, 0);
1788
Alexander Duyck330a6d62009-10-27 23:51:35 +00001789 if (hw->mac.ops.init_hw(hw))
Alexander Duyck090b1792009-10-27 23:51:55 +00001790 dev_err(&pdev->dev, "Hardware Error\n");
Auke Kok9d5c8242008-01-24 02:22:38 -08001791
Matthew Vicka27416b2012-04-18 02:57:44 +00001792 /*
1793 * Flow control settings reset on hardware reset, so guarantee flow
1794 * control is off when forcing speed.
1795 */
1796 if (!hw->mac.autoneg)
1797 igb_force_mac_fc(hw);
1798
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +00001799 igb_init_dmac(adapter, pba);
Carolyn Wybornye4288932012-12-07 03:01:42 +00001800#ifdef CONFIG_IGB_HWMON
1801 /* Re-initialize the thermal sensor on i350 devices. */
1802 if (!test_bit(__IGB_DOWN, &adapter->state)) {
1803 if (mac->type == e1000_i350 && hw->bus.func == 0) {
1804 /* If present, re-initialize the external thermal sensor
1805 * interface.
1806 */
1807 if (adapter->ets)
1808 mac->ops.init_thermal_sensor_thresh(hw);
1809 }
1810 }
1811#endif
Nick Nunley88a268c2010-02-17 01:01:59 +00001812 if (!netif_running(adapter->netdev))
1813 igb_power_down_link(adapter);
1814
Auke Kok9d5c8242008-01-24 02:22:38 -08001815 igb_update_mng_vlan(adapter);
1816
1817 /* Enable h/w to recognize an 802.1Q VLAN Ethernet packet */
1818 wr32(E1000_VET, ETHERNET_IEEE_VLAN_TYPE);
1819
Matthew Vick1f6e8172012-08-18 07:26:33 +00001820 /* Re-enable PTP, where applicable. */
1821 igb_ptp_reset(adapter);
Matthew Vick1f6e8172012-08-18 07:26:33 +00001822
Alexander Duyck330a6d62009-10-27 23:51:35 +00001823 igb_get_phy_info(hw);
Auke Kok9d5c8242008-01-24 02:22:38 -08001824}
1825
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001826static netdev_features_t igb_fix_features(struct net_device *netdev,
1827 netdev_features_t features)
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00001828{
1829 /*
1830 * Since there is no support for separate rx/tx vlan accel
1831 * enable/disable make sure tx flag is always in same state as rx.
1832 */
1833 if (features & NETIF_F_HW_VLAN_RX)
1834 features |= NETIF_F_HW_VLAN_TX;
1835 else
1836 features &= ~NETIF_F_HW_VLAN_TX;
1837
1838 return features;
1839}
1840
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001841static int igb_set_features(struct net_device *netdev,
1842 netdev_features_t features)
Michał Mirosławac52caa2011-06-08 08:38:01 +00001843{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001844 netdev_features_t changed = netdev->features ^ features;
Ben Greear89eaefb2012-03-06 09:41:58 +00001845 struct igb_adapter *adapter = netdev_priv(netdev);
Michał Mirosławac52caa2011-06-08 08:38:01 +00001846
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00001847 if (changed & NETIF_F_HW_VLAN_RX)
1848 igb_vlan_mode(netdev, features);
1849
Ben Greear89eaefb2012-03-06 09:41:58 +00001850 if (!(changed & NETIF_F_RXALL))
1851 return 0;
1852
1853 netdev->features = features;
1854
1855 if (netif_running(netdev))
1856 igb_reinit_locked(adapter);
1857 else
1858 igb_reset(adapter);
1859
Michał Mirosławac52caa2011-06-08 08:38:01 +00001860 return 0;
1861}
1862
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08001863static const struct net_device_ops igb_netdev_ops = {
Alexander Duyck559e9c42009-10-27 23:52:50 +00001864 .ndo_open = igb_open,
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08001865 .ndo_stop = igb_close,
Alexander Duyckcd392f52011-08-26 07:43:59 +00001866 .ndo_start_xmit = igb_xmit_frame,
Eric Dumazet12dcd862010-10-15 17:27:10 +00001867 .ndo_get_stats64 = igb_get_stats64,
Alexander Duyckff41f8d2009-09-03 14:48:56 +00001868 .ndo_set_rx_mode = igb_set_rx_mode,
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08001869 .ndo_set_mac_address = igb_set_mac,
1870 .ndo_change_mtu = igb_change_mtu,
1871 .ndo_do_ioctl = igb_ioctl,
1872 .ndo_tx_timeout = igb_tx_timeout,
1873 .ndo_validate_addr = eth_validate_addr,
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08001874 .ndo_vlan_rx_add_vid = igb_vlan_rx_add_vid,
1875 .ndo_vlan_rx_kill_vid = igb_vlan_rx_kill_vid,
Williams, Mitch A8151d292010-02-10 01:44:24 +00001876 .ndo_set_vf_mac = igb_ndo_set_vf_mac,
1877 .ndo_set_vf_vlan = igb_ndo_set_vf_vlan,
1878 .ndo_set_vf_tx_rate = igb_ndo_set_vf_bw,
1879 .ndo_get_vf_config = igb_ndo_get_vf_config,
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08001880#ifdef CONFIG_NET_POLL_CONTROLLER
1881 .ndo_poll_controller = igb_netpoll,
1882#endif
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00001883 .ndo_fix_features = igb_fix_features,
1884 .ndo_set_features = igb_set_features,
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08001885};
1886
Taku Izumi42bfd33a2008-06-20 12:10:30 +09001887/**
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00001888 * igb_set_fw_version - Configure version string for ethtool
1889 * @adapter: adapter struct
1890 *
1891 **/
1892void igb_set_fw_version(struct igb_adapter *adapter)
1893{
1894 struct e1000_hw *hw = &adapter->hw;
Carolyn Wyborny0b1a6f22012-10-18 07:16:19 +00001895 struct e1000_fw_version fw;
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00001896
Carolyn Wyborny0b1a6f22012-10-18 07:16:19 +00001897 igb_get_fw_version(hw, &fw);
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00001898
Carolyn Wyborny0b1a6f22012-10-18 07:16:19 +00001899 switch (hw->mac.type) {
1900 case e1000_i211:
1901 snprintf(adapter->fw_version, sizeof(adapter->fw_version),
1902 "%2d.%2d-%d",
1903 fw.invm_major, fw.invm_minor, fw.invm_img_type);
1904 break;
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00001905
Carolyn Wyborny0b1a6f22012-10-18 07:16:19 +00001906 default:
1907 /* if option is rom valid, display its version too */
1908 if (fw.or_valid) {
1909 snprintf(adapter->fw_version,
1910 sizeof(adapter->fw_version),
1911 "%d.%d, 0x%08x, %d.%d.%d",
1912 fw.eep_major, fw.eep_minor, fw.etrack_id,
1913 fw.or_major, fw.or_build, fw.or_patch);
1914 /* no option rom */
1915 } else {
1916 snprintf(adapter->fw_version,
1917 sizeof(adapter->fw_version),
1918 "%d.%d, 0x%08x",
1919 fw.eep_major, fw.eep_minor, fw.etrack_id);
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00001920 }
Carolyn Wyborny0b1a6f22012-10-18 07:16:19 +00001921 break;
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00001922 }
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00001923 return;
1924}
1925
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +00001926static const struct i2c_board_info i350_sensor_info = {
1927 I2C_BOARD_INFO("i350bb", 0Xf8),
1928};
1929
1930/* igb_init_i2c - Init I2C interface
1931 * @adapter: pointer to adapter structure
1932 *
1933 */
1934static s32 igb_init_i2c(struct igb_adapter *adapter)
1935{
1936 s32 status = E1000_SUCCESS;
1937
1938 /* I2C interface supported on i350 devices */
1939 if (adapter->hw.mac.type != e1000_i350)
1940 return E1000_SUCCESS;
1941
1942 /* Initialize the i2c bus which is controlled by the registers.
1943 * This bus will use the i2c_algo_bit structue that implements
1944 * the protocol through toggling of the 4 bits in the register.
1945 */
1946 adapter->i2c_adap.owner = THIS_MODULE;
1947 adapter->i2c_algo = igb_i2c_algo;
1948 adapter->i2c_algo.data = adapter;
1949 adapter->i2c_adap.algo_data = &adapter->i2c_algo;
1950 adapter->i2c_adap.dev.parent = &adapter->pdev->dev;
1951 strlcpy(adapter->i2c_adap.name, "igb BB",
1952 sizeof(adapter->i2c_adap.name));
1953 status = i2c_bit_add_bus(&adapter->i2c_adap);
1954 return status;
1955}
1956
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00001957/**
Auke Kok9d5c8242008-01-24 02:22:38 -08001958 * igb_probe - Device Initialization Routine
1959 * @pdev: PCI device information struct
1960 * @ent: entry in igb_pci_tbl
1961 *
1962 * Returns 0 on success, negative on failure
1963 *
1964 * igb_probe initializes an adapter identified by a pci_dev structure.
1965 * The OS initialization, configuring of the adapter private structure,
1966 * and a hardware reset occur.
1967 **/
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00001968static int igb_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Auke Kok9d5c8242008-01-24 02:22:38 -08001969{
1970 struct net_device *netdev;
1971 struct igb_adapter *adapter;
1972 struct e1000_hw *hw;
Alexander Duyck4337e992009-10-27 23:48:31 +00001973 u16 eeprom_data = 0;
Carolyn Wyborny9835fd72010-11-22 17:17:21 +00001974 s32 ret_val;
Alexander Duyck4337e992009-10-27 23:48:31 +00001975 static int global_quad_port_a; /* global quad port a indication */
Auke Kok9d5c8242008-01-24 02:22:38 -08001976 const struct e1000_info *ei = igb_info_tbl[ent->driver_data];
1977 unsigned long mmio_start, mmio_len;
David S. Miller2d6a5e92009-03-17 15:01:30 -07001978 int err, pci_using_dac;
Carolyn Wyborny9835fd72010-11-22 17:17:21 +00001979 u8 part_str[E1000_PBANUM_LENGTH];
Auke Kok9d5c8242008-01-24 02:22:38 -08001980
Andy Gospodarekbded64a2010-07-21 06:40:31 +00001981 /* Catch broken hardware that put the wrong VF device ID in
1982 * the PCIe SR-IOV capability.
1983 */
1984 if (pdev->is_virtfn) {
1985 WARN(1, KERN_ERR "%s (%hx:%hx) should not be a VF!\n",
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00001986 pci_name(pdev), pdev->vendor, pdev->device);
Andy Gospodarekbded64a2010-07-21 06:40:31 +00001987 return -EINVAL;
1988 }
1989
Alexander Duyckaed5dec2009-02-06 23:16:04 +00001990 err = pci_enable_device_mem(pdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08001991 if (err)
1992 return err;
1993
1994 pci_using_dac = 0;
Alexander Duyck59d71982010-04-27 13:09:25 +00001995 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(64));
Auke Kok9d5c8242008-01-24 02:22:38 -08001996 if (!err) {
Alexander Duyck59d71982010-04-27 13:09:25 +00001997 err = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64));
Auke Kok9d5c8242008-01-24 02:22:38 -08001998 if (!err)
1999 pci_using_dac = 1;
2000 } else {
Alexander Duyck59d71982010-04-27 13:09:25 +00002001 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
Auke Kok9d5c8242008-01-24 02:22:38 -08002002 if (err) {
Alexander Duyck59d71982010-04-27 13:09:25 +00002003 err = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
Auke Kok9d5c8242008-01-24 02:22:38 -08002004 if (err) {
2005 dev_err(&pdev->dev, "No usable DMA "
2006 "configuration, aborting\n");
2007 goto err_dma;
2008 }
2009 }
2010 }
2011
Alexander Duyckaed5dec2009-02-06 23:16:04 +00002012 err = pci_request_selected_regions(pdev, pci_select_bars(pdev,
2013 IORESOURCE_MEM),
2014 igb_driver_name);
Auke Kok9d5c8242008-01-24 02:22:38 -08002015 if (err)
2016 goto err_pci_reg;
2017
Frans Pop19d5afd2009-10-02 10:04:12 -07002018 pci_enable_pcie_error_reporting(pdev);
Alexander Duyck40a914f2008-11-27 00:24:37 -08002019
Auke Kok9d5c8242008-01-24 02:22:38 -08002020 pci_set_master(pdev);
Auke Kokc682fc22008-04-23 11:09:34 -07002021 pci_save_state(pdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08002022
2023 err = -ENOMEM;
Alexander Duyck1bfaf072009-02-19 20:39:23 -08002024 netdev = alloc_etherdev_mq(sizeof(struct igb_adapter),
Alexander Duyck1cc3bd82011-08-26 07:44:10 +00002025 IGB_MAX_TX_QUEUES);
Auke Kok9d5c8242008-01-24 02:22:38 -08002026 if (!netdev)
2027 goto err_alloc_etherdev;
2028
2029 SET_NETDEV_DEV(netdev, &pdev->dev);
2030
2031 pci_set_drvdata(pdev, netdev);
2032 adapter = netdev_priv(netdev);
2033 adapter->netdev = netdev;
2034 adapter->pdev = pdev;
2035 hw = &adapter->hw;
2036 hw->back = adapter;
stephen hemmingerb3f4d592012-03-13 06:04:20 +00002037 adapter->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
Auke Kok9d5c8242008-01-24 02:22:38 -08002038
2039 mmio_start = pci_resource_start(pdev, 0);
2040 mmio_len = pci_resource_len(pdev, 0);
2041
2042 err = -EIO;
Alexander Duyck28b07592009-02-06 23:20:31 +00002043 hw->hw_addr = ioremap(mmio_start, mmio_len);
2044 if (!hw->hw_addr)
Auke Kok9d5c8242008-01-24 02:22:38 -08002045 goto err_ioremap;
2046
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08002047 netdev->netdev_ops = &igb_netdev_ops;
Auke Kok9d5c8242008-01-24 02:22:38 -08002048 igb_set_ethtool_ops(netdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08002049 netdev->watchdog_timeo = 5 * HZ;
Auke Kok9d5c8242008-01-24 02:22:38 -08002050
2051 strncpy(netdev->name, pci_name(pdev), sizeof(netdev->name) - 1);
2052
2053 netdev->mem_start = mmio_start;
2054 netdev->mem_end = mmio_start + mmio_len;
2055
Auke Kok9d5c8242008-01-24 02:22:38 -08002056 /* PCI config space info */
2057 hw->vendor_id = pdev->vendor;
2058 hw->device_id = pdev->device;
2059 hw->revision_id = pdev->revision;
2060 hw->subsystem_vendor_id = pdev->subsystem_vendor;
2061 hw->subsystem_device_id = pdev->subsystem_device;
2062
Auke Kok9d5c8242008-01-24 02:22:38 -08002063 /* Copy the default MAC, PHY and NVM function pointers */
2064 memcpy(&hw->mac.ops, ei->mac_ops, sizeof(hw->mac.ops));
2065 memcpy(&hw->phy.ops, ei->phy_ops, sizeof(hw->phy.ops));
2066 memcpy(&hw->nvm.ops, ei->nvm_ops, sizeof(hw->nvm.ops));
2067 /* Initialize skew-specific constants */
2068 err = ei->get_invariants(hw);
2069 if (err)
Alexander Duyck450c87c2009-02-06 23:22:11 +00002070 goto err_sw_init;
Auke Kok9d5c8242008-01-24 02:22:38 -08002071
Alexander Duyck450c87c2009-02-06 23:22:11 +00002072 /* setup the private structure */
Auke Kok9d5c8242008-01-24 02:22:38 -08002073 err = igb_sw_init(adapter);
2074 if (err)
2075 goto err_sw_init;
2076
2077 igb_get_bus_info_pcie(hw);
2078
2079 hw->phy.autoneg_wait_to_complete = false;
Auke Kok9d5c8242008-01-24 02:22:38 -08002080
2081 /* Copper options */
2082 if (hw->phy.media_type == e1000_media_type_copper) {
2083 hw->phy.mdix = AUTO_ALL_MODES;
2084 hw->phy.disable_polarity_correction = false;
2085 hw->phy.ms_type = e1000_ms_hw_default;
2086 }
2087
2088 if (igb_check_reset_block(hw))
2089 dev_info(&pdev->dev,
2090 "PHY reset is blocked due to SOL/IDER session.\n");
2091
Alexander Duyck077887c2011-08-26 07:46:29 +00002092 /*
2093 * features is initialized to 0 in allocation, it might have bits
2094 * set by igb_sw_init so we should use an or instead of an
2095 * assignment.
2096 */
2097 netdev->features |= NETIF_F_SG |
2098 NETIF_F_IP_CSUM |
2099 NETIF_F_IPV6_CSUM |
2100 NETIF_F_TSO |
2101 NETIF_F_TSO6 |
2102 NETIF_F_RXHASH |
2103 NETIF_F_RXCSUM |
2104 NETIF_F_HW_VLAN_RX |
2105 NETIF_F_HW_VLAN_TX;
Michał Mirosławac52caa2011-06-08 08:38:01 +00002106
Alexander Duyck077887c2011-08-26 07:46:29 +00002107 /* copy netdev features into list of user selectable features */
2108 netdev->hw_features |= netdev->features;
Ben Greear89eaefb2012-03-06 09:41:58 +00002109 netdev->hw_features |= NETIF_F_RXALL;
Auke Kok9d5c8242008-01-24 02:22:38 -08002110
Alexander Duyck077887c2011-08-26 07:46:29 +00002111 /* set this bit last since it cannot be part of hw_features */
2112 netdev->features |= NETIF_F_HW_VLAN_FILTER;
2113
2114 netdev->vlan_features |= NETIF_F_TSO |
2115 NETIF_F_TSO6 |
2116 NETIF_F_IP_CSUM |
2117 NETIF_F_IPV6_CSUM |
2118 NETIF_F_SG;
Jeff Kirsher48f29ff2008-06-05 04:06:27 -07002119
Ben Greear6b8f0922012-03-06 09:41:53 +00002120 netdev->priv_flags |= IFF_SUPP_NOFCS;
2121
Yi Zou7b872a52010-09-22 17:57:58 +00002122 if (pci_using_dac) {
Auke Kok9d5c8242008-01-24 02:22:38 -08002123 netdev->features |= NETIF_F_HIGHDMA;
Yi Zou7b872a52010-09-22 17:57:58 +00002124 netdev->vlan_features |= NETIF_F_HIGHDMA;
2125 }
Auke Kok9d5c8242008-01-24 02:22:38 -08002126
Michał Mirosławac52caa2011-06-08 08:38:01 +00002127 if (hw->mac.type >= e1000_82576) {
2128 netdev->hw_features |= NETIF_F_SCTP_CSUM;
Jesse Brandeburgb9473562009-04-27 22:36:13 +00002129 netdev->features |= NETIF_F_SCTP_CSUM;
Michał Mirosławac52caa2011-06-08 08:38:01 +00002130 }
Jesse Brandeburgb9473562009-04-27 22:36:13 +00002131
Jiri Pirko01789342011-08-16 06:29:00 +00002132 netdev->priv_flags |= IFF_UNICAST_FLT;
2133
Alexander Duyck330a6d62009-10-27 23:51:35 +00002134 adapter->en_mng_pt = igb_enable_mng_pass_thru(hw);
Auke Kok9d5c8242008-01-24 02:22:38 -08002135
2136 /* before reading the NVM, reset the controller to put the device in a
2137 * known good starting state */
2138 hw->mac.ops.reset_hw(hw);
2139
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002140 /*
2141 * make sure the NVM is good , i211 parts have special NVM that
2142 * doesn't contain a checksum
2143 */
2144 if (hw->mac.type != e1000_i211) {
2145 if (hw->nvm.ops.validate(hw) < 0) {
2146 dev_err(&pdev->dev, "The NVM Checksum Is Not Valid\n");
2147 err = -EIO;
2148 goto err_eeprom;
2149 }
Auke Kok9d5c8242008-01-24 02:22:38 -08002150 }
2151
2152 /* copy the MAC address out of the NVM */
2153 if (hw->mac.ops.read_mac_addr(hw))
2154 dev_err(&pdev->dev, "NVM Read Error\n");
2155
2156 memcpy(netdev->dev_addr, hw->mac.addr, netdev->addr_len);
Auke Kok9d5c8242008-01-24 02:22:38 -08002157
Jiri Pirkoaaeb6cd2013-01-08 01:38:26 +00002158 if (!is_valid_ether_addr(netdev->dev_addr)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08002159 dev_err(&pdev->dev, "Invalid MAC Address\n");
2160 err = -EIO;
2161 goto err_eeprom;
2162 }
2163
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00002164 /* get firmware version for ethtool -i */
2165 igb_set_fw_version(adapter);
2166
Joe Perchesc061b182010-08-23 18:20:03 +00002167 setup_timer(&adapter->watchdog_timer, igb_watchdog,
Alexander Duyck0e340482009-03-20 00:17:08 +00002168 (unsigned long) adapter);
Joe Perchesc061b182010-08-23 18:20:03 +00002169 setup_timer(&adapter->phy_info_timer, igb_update_phy_info,
Alexander Duyck0e340482009-03-20 00:17:08 +00002170 (unsigned long) adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08002171
2172 INIT_WORK(&adapter->reset_task, igb_reset_task);
2173 INIT_WORK(&adapter->watchdog_task, igb_watchdog_task);
2174
Alexander Duyck450c87c2009-02-06 23:22:11 +00002175 /* Initialize link properties that are user-changeable */
Auke Kok9d5c8242008-01-24 02:22:38 -08002176 adapter->fc_autoneg = true;
2177 hw->mac.autoneg = true;
2178 hw->phy.autoneg_advertised = 0x2f;
2179
Alexander Duyck0cce1192009-07-23 18:10:24 +00002180 hw->fc.requested_mode = e1000_fc_default;
2181 hw->fc.current_mode = e1000_fc_default;
Auke Kok9d5c8242008-01-24 02:22:38 -08002182
Auke Kok9d5c8242008-01-24 02:22:38 -08002183 igb_validate_mdi_setting(hw);
2184
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002185 /* By default, support wake on port A */
Alexander Duycka2cf8b62009-03-13 20:41:17 +00002186 if (hw->bus.func == 0)
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002187 adapter->flags |= IGB_FLAG_WOL_SUPPORTED;
2188
2189 /* Check the NVM for wake support on non-port A ports */
2190 if (hw->mac.type >= e1000_82580)
Alexander Duyck55cac242009-11-19 12:42:21 +00002191 hw->nvm.ops.read(hw, NVM_INIT_CONTROL3_PORT_A +
2192 NVM_82580_LAN_FUNC_OFFSET(hw->bus.func), 1,
2193 &eeprom_data);
Alexander Duycka2cf8b62009-03-13 20:41:17 +00002194 else if (hw->bus.func == 1)
2195 hw->nvm.ops.read(hw, NVM_INIT_CONTROL3_PORT_B, 1, &eeprom_data);
Auke Kok9d5c8242008-01-24 02:22:38 -08002196
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002197 if (eeprom_data & IGB_EEPROM_APME)
2198 adapter->flags |= IGB_FLAG_WOL_SUPPORTED;
Auke Kok9d5c8242008-01-24 02:22:38 -08002199
2200 /* now that we have the eeprom settings, apply the special cases where
2201 * the eeprom may be wrong or the board simply won't support wake on
2202 * lan on a particular port */
2203 switch (pdev->device) {
2204 case E1000_DEV_ID_82575GB_QUAD_COPPER:
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002205 adapter->flags &= ~IGB_FLAG_WOL_SUPPORTED;
Auke Kok9d5c8242008-01-24 02:22:38 -08002206 break;
2207 case E1000_DEV_ID_82575EB_FIBER_SERDES:
Alexander Duyck2d064c02008-07-08 15:10:12 -07002208 case E1000_DEV_ID_82576_FIBER:
2209 case E1000_DEV_ID_82576_SERDES:
Auke Kok9d5c8242008-01-24 02:22:38 -08002210 /* Wake events only supported on port A for dual fiber
2211 * regardless of eeprom setting */
2212 if (rd32(E1000_STATUS) & E1000_STATUS_FUNC_1)
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002213 adapter->flags &= ~IGB_FLAG_WOL_SUPPORTED;
Auke Kok9d5c8242008-01-24 02:22:38 -08002214 break;
Alexander Duyckc8ea5ea2009-03-13 20:42:35 +00002215 case E1000_DEV_ID_82576_QUAD_COPPER:
Stefan Assmannd5aa2252010-04-09 09:51:34 +00002216 case E1000_DEV_ID_82576_QUAD_COPPER_ET2:
Alexander Duyckc8ea5ea2009-03-13 20:42:35 +00002217 /* if quad port adapter, disable WoL on all but port A */
2218 if (global_quad_port_a != 0)
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002219 adapter->flags &= ~IGB_FLAG_WOL_SUPPORTED;
Alexander Duyckc8ea5ea2009-03-13 20:42:35 +00002220 else
2221 adapter->flags |= IGB_FLAG_QUAD_PORT_A;
2222 /* Reset for multiple quad port adapters */
2223 if (++global_quad_port_a == 4)
2224 global_quad_port_a = 0;
2225 break;
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002226 default:
2227 /* If the device can't wake, don't set software support */
2228 if (!device_can_wakeup(&adapter->pdev->dev))
2229 adapter->flags &= ~IGB_FLAG_WOL_SUPPORTED;
Auke Kok9d5c8242008-01-24 02:22:38 -08002230 }
2231
2232 /* initialize the wol settings based on the eeprom settings */
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002233 if (adapter->flags & IGB_FLAG_WOL_SUPPORTED)
2234 adapter->wol |= E1000_WUFC_MAG;
2235
2236 /* Some vendors want WoL disabled by default, but still supported */
2237 if ((hw->mac.type == e1000_i350) &&
2238 (pdev->subsystem_vendor == PCI_VENDOR_ID_HP)) {
2239 adapter->flags |= IGB_FLAG_WOL_SUPPORTED;
2240 adapter->wol = 0;
2241 }
2242
2243 device_set_wakeup_enable(&adapter->pdev->dev,
2244 adapter->flags & IGB_FLAG_WOL_SUPPORTED);
Auke Kok9d5c8242008-01-24 02:22:38 -08002245
2246 /* reset the hardware with the new settings */
2247 igb_reset(adapter);
2248
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +00002249 /* Init the I2C interface */
2250 err = igb_init_i2c(adapter);
2251 if (err) {
2252 dev_err(&pdev->dev, "failed to init i2c interface\n");
2253 goto err_eeprom;
2254 }
2255
Auke Kok9d5c8242008-01-24 02:22:38 -08002256 /* let the f/w know that the h/w is now under the control of the
2257 * driver. */
2258 igb_get_hw_control(adapter);
2259
Auke Kok9d5c8242008-01-24 02:22:38 -08002260 strcpy(netdev->name, "eth%d");
2261 err = register_netdev(netdev);
2262 if (err)
2263 goto err_register;
2264
Jesse Brandeburgb168dfc2009-04-17 20:44:32 +00002265 /* carrier off reporting is important to ethtool even BEFORE open */
2266 netif_carrier_off(netdev);
2267
Jeff Kirsher421e02f2008-10-17 11:08:31 -07002268#ifdef CONFIG_IGB_DCA
Alexander Duyckbbd98fe2009-01-31 00:52:30 -08002269 if (dca_add_requester(&pdev->dev) == 0) {
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07002270 adapter->flags |= IGB_FLAG_DCA_ENABLED;
Jeb Cramerfe4506b2008-07-08 15:07:55 -07002271 dev_info(&pdev->dev, "DCA enabled\n");
Jeb Cramerfe4506b2008-07-08 15:07:55 -07002272 igb_setup_dca(adapter);
2273 }
Alexander Duyckc5b9bd52009-10-27 23:46:01 +00002274
Jeb Cramerfe4506b2008-07-08 15:07:55 -07002275#endif
Carolyn Wybornye4288932012-12-07 03:01:42 +00002276#ifdef CONFIG_IGB_HWMON
2277 /* Initialize the thermal sensor on i350 devices. */
2278 if (hw->mac.type == e1000_i350 && hw->bus.func == 0) {
2279 u16 ets_word;
Matthew Vick3c89f6d2012-08-10 05:40:43 +00002280
Carolyn Wybornye4288932012-12-07 03:01:42 +00002281 /*
2282 * Read the NVM to determine if this i350 device supports an
2283 * external thermal sensor.
2284 */
2285 hw->nvm.ops.read(hw, NVM_ETS_CFG, 1, &ets_word);
2286 if (ets_word != 0x0000 && ets_word != 0xFFFF)
2287 adapter->ets = true;
2288 else
2289 adapter->ets = false;
2290 if (igb_sysfs_init(adapter))
2291 dev_err(&pdev->dev,
2292 "failed to allocate sysfs resources\n");
2293 } else {
2294 adapter->ets = false;
2295 }
2296#endif
Anders Berggren673b8b72011-02-04 07:32:32 +00002297 /* do hw tstamp init after resetting */
Richard Cochran7ebae812012-03-16 10:55:37 +00002298 igb_ptp_init(adapter);
Anders Berggren673b8b72011-02-04 07:32:32 +00002299
Auke Kok9d5c8242008-01-24 02:22:38 -08002300 dev_info(&pdev->dev, "Intel(R) Gigabit Ethernet Network Connection\n");
2301 /* print bus type/speed/width info */
Johannes Berg7c510e42008-10-27 17:47:26 -07002302 dev_info(&pdev->dev, "%s: (PCIe:%s:%s) %pM\n",
Auke Kok9d5c8242008-01-24 02:22:38 -08002303 netdev->name,
Alexander Duyck559e9c42009-10-27 23:52:50 +00002304 ((hw->bus.speed == e1000_bus_speed_2500) ? "2.5Gb/s" :
Alexander Duyckff846f52010-04-27 01:02:40 +00002305 (hw->bus.speed == e1000_bus_speed_5000) ? "5.0Gb/s" :
Alexander Duyck559e9c42009-10-27 23:52:50 +00002306 "unknown"),
Alexander Duyck59c3de82009-03-31 20:38:00 +00002307 ((hw->bus.width == e1000_bus_width_pcie_x4) ? "Width x4" :
2308 (hw->bus.width == e1000_bus_width_pcie_x2) ? "Width x2" :
2309 (hw->bus.width == e1000_bus_width_pcie_x1) ? "Width x1" :
2310 "unknown"),
Johannes Berg7c510e42008-10-27 17:47:26 -07002311 netdev->dev_addr);
Auke Kok9d5c8242008-01-24 02:22:38 -08002312
Carolyn Wyborny9835fd72010-11-22 17:17:21 +00002313 ret_val = igb_read_part_string(hw, part_str, E1000_PBANUM_LENGTH);
2314 if (ret_val)
2315 strcpy(part_str, "Unknown");
2316 dev_info(&pdev->dev, "%s: PBA No: %s\n", netdev->name, part_str);
Auke Kok9d5c8242008-01-24 02:22:38 -08002317 dev_info(&pdev->dev,
2318 "Using %s interrupts. %d rx queue(s), %d tx queue(s)\n",
2319 adapter->msix_entries ? "MSI-X" :
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07002320 (adapter->flags & IGB_FLAG_HAS_MSI) ? "MSI" : "legacy",
Auke Kok9d5c8242008-01-24 02:22:38 -08002321 adapter->num_rx_queues, adapter->num_tx_queues);
Carolyn Wyborny09b068d2011-03-11 20:42:13 -08002322 switch (hw->mac.type) {
2323 case e1000_i350:
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002324 case e1000_i210:
2325 case e1000_i211:
Carolyn Wyborny09b068d2011-03-11 20:42:13 -08002326 igb_set_eee_i350(hw);
2327 break;
2328 default:
2329 break;
2330 }
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002331
2332 pm_runtime_put_noidle(&pdev->dev);
Auke Kok9d5c8242008-01-24 02:22:38 -08002333 return 0;
2334
2335err_register:
2336 igb_release_hw_control(adapter);
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +00002337 memset(&adapter->i2c_adap, 0, sizeof(adapter->i2c_adap));
Auke Kok9d5c8242008-01-24 02:22:38 -08002338err_eeprom:
2339 if (!igb_check_reset_block(hw))
Alexander Duyckf5f4cf02008-11-21 21:30:24 -08002340 igb_reset_phy(hw);
Auke Kok9d5c8242008-01-24 02:22:38 -08002341
2342 if (hw->flash_address)
2343 iounmap(hw->flash_address);
Auke Kok9d5c8242008-01-24 02:22:38 -08002344err_sw_init:
Alexander Duyck047e0032009-10-27 15:49:27 +00002345 igb_clear_interrupt_scheme(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08002346 iounmap(hw->hw_addr);
2347err_ioremap:
2348 free_netdev(netdev);
2349err_alloc_etherdev:
Alexander Duyck559e9c42009-10-27 23:52:50 +00002350 pci_release_selected_regions(pdev,
2351 pci_select_bars(pdev, IORESOURCE_MEM));
Auke Kok9d5c8242008-01-24 02:22:38 -08002352err_pci_reg:
2353err_dma:
2354 pci_disable_device(pdev);
2355 return err;
2356}
2357
Greg Rosefa44f2f2013-01-17 01:03:06 -08002358#ifdef CONFIG_PCI_IOV
2359static int igb_disable_sriov(struct pci_dev *pdev)
2360{
2361 struct net_device *netdev = pci_get_drvdata(pdev);
2362 struct igb_adapter *adapter = netdev_priv(netdev);
2363 struct e1000_hw *hw = &adapter->hw;
2364
2365 /* reclaim resources allocated to VFs */
2366 if (adapter->vf_data) {
2367 /* disable iov and allow time for transactions to clear */
2368 if (igb_vfs_are_assigned(adapter)) {
2369 dev_warn(&pdev->dev,
2370 "Cannot deallocate SR-IOV virtual functions while they are assigned - VFs will not be deallocated\n");
2371 return -EPERM;
2372 } else {
2373 pci_disable_sriov(pdev);
2374 msleep(500);
2375 }
2376
2377 kfree(adapter->vf_data);
2378 adapter->vf_data = NULL;
2379 adapter->vfs_allocated_count = 0;
2380 wr32(E1000_IOVCTL, E1000_IOVCTL_REUSE_VFQ);
2381 wrfl();
2382 msleep(100);
2383 dev_info(&pdev->dev, "IOV Disabled\n");
2384
2385 /* Re-enable DMA Coalescing flag since IOV is turned off */
2386 adapter->flags |= IGB_FLAG_DMAC;
2387 }
2388
2389 return 0;
2390}
2391
2392static int igb_enable_sriov(struct pci_dev *pdev, int num_vfs)
2393{
2394 struct net_device *netdev = pci_get_drvdata(pdev);
2395 struct igb_adapter *adapter = netdev_priv(netdev);
2396 int old_vfs = pci_num_vf(pdev);
2397 int err = 0;
2398 int i;
2399
2400 if (!num_vfs)
2401 goto out;
2402 else if (old_vfs && old_vfs == num_vfs)
2403 goto out;
2404 else if (old_vfs && old_vfs != num_vfs)
2405 err = igb_disable_sriov(pdev);
2406
2407 if (err)
2408 goto out;
2409
2410 if (num_vfs > 7) {
2411 err = -EPERM;
2412 goto out;
2413 }
2414
2415 adapter->vfs_allocated_count = num_vfs;
2416
2417 adapter->vf_data = kcalloc(adapter->vfs_allocated_count,
2418 sizeof(struct vf_data_storage), GFP_KERNEL);
2419
2420 /* if allocation failed then we do not support SR-IOV */
2421 if (!adapter->vf_data) {
2422 adapter->vfs_allocated_count = 0;
2423 dev_err(&pdev->dev,
2424 "Unable to allocate memory for VF Data Storage\n");
2425 err = -ENOMEM;
2426 goto out;
2427 }
2428
2429 err = pci_enable_sriov(pdev, adapter->vfs_allocated_count);
2430 if (err)
2431 goto err_out;
2432
2433 dev_info(&pdev->dev, "%d VFs allocated\n",
2434 adapter->vfs_allocated_count);
2435 for (i = 0; i < adapter->vfs_allocated_count; i++)
2436 igb_vf_configure(adapter, i);
2437
2438 /* DMA Coalescing is not supported in IOV mode. */
2439 adapter->flags &= ~IGB_FLAG_DMAC;
2440 goto out;
2441
2442err_out:
2443 kfree(adapter->vf_data);
2444 adapter->vf_data = NULL;
2445 adapter->vfs_allocated_count = 0;
2446out:
2447 return err;
2448}
2449
2450#endif
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +00002451/*
2452 * igb_remove_i2c - Cleanup I2C interface
2453 * @adapter: pointer to adapter structure
2454 *
2455 */
2456static void igb_remove_i2c(struct igb_adapter *adapter)
2457{
2458
2459 /* free the adapter bus structure */
2460 i2c_del_adapter(&adapter->i2c_adap);
2461}
2462
Auke Kok9d5c8242008-01-24 02:22:38 -08002463/**
2464 * igb_remove - Device Removal Routine
2465 * @pdev: PCI device information struct
2466 *
2467 * igb_remove is called by the PCI subsystem to alert the driver
2468 * that it should release a PCI device. The could be caused by a
2469 * Hot-Plug event, or because the driver is going to be removed from
2470 * memory.
2471 **/
Bill Pemberton9f9a12f2012-12-03 09:24:25 -05002472static void igb_remove(struct pci_dev *pdev)
Auke Kok9d5c8242008-01-24 02:22:38 -08002473{
2474 struct net_device *netdev = pci_get_drvdata(pdev);
2475 struct igb_adapter *adapter = netdev_priv(netdev);
Jeb Cramerfe4506b2008-07-08 15:07:55 -07002476 struct e1000_hw *hw = &adapter->hw;
Auke Kok9d5c8242008-01-24 02:22:38 -08002477
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002478 pm_runtime_get_noresume(&pdev->dev);
Carolyn Wybornye4288932012-12-07 03:01:42 +00002479#ifdef CONFIG_IGB_HWMON
2480 igb_sysfs_exit(adapter);
2481#endif
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +00002482 igb_remove_i2c(adapter);
Matthew Vicka79f4f82012-08-10 05:40:44 +00002483 igb_ptp_stop(adapter);
Tejun Heo760141a2010-12-12 16:45:14 +01002484 /*
2485 * The watchdog timer may be rescheduled, so explicitly
2486 * disable watchdog from being rescheduled.
2487 */
Auke Kok9d5c8242008-01-24 02:22:38 -08002488 set_bit(__IGB_DOWN, &adapter->state);
2489 del_timer_sync(&adapter->watchdog_timer);
2490 del_timer_sync(&adapter->phy_info_timer);
2491
Tejun Heo760141a2010-12-12 16:45:14 +01002492 cancel_work_sync(&adapter->reset_task);
2493 cancel_work_sync(&adapter->watchdog_task);
Auke Kok9d5c8242008-01-24 02:22:38 -08002494
Jeff Kirsher421e02f2008-10-17 11:08:31 -07002495#ifdef CONFIG_IGB_DCA
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07002496 if (adapter->flags & IGB_FLAG_DCA_ENABLED) {
Jeb Cramerfe4506b2008-07-08 15:07:55 -07002497 dev_info(&pdev->dev, "DCA disabled\n");
2498 dca_remove_requester(&pdev->dev);
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07002499 adapter->flags &= ~IGB_FLAG_DCA_ENABLED;
Alexander Duyckcbd347a2009-02-15 23:59:44 -08002500 wr32(E1000_DCA_CTRL, E1000_DCA_CTRL_DCA_MODE_DISABLE);
Jeb Cramerfe4506b2008-07-08 15:07:55 -07002501 }
2502#endif
2503
Auke Kok9d5c8242008-01-24 02:22:38 -08002504 /* Release control of h/w to f/w. If f/w is AMT enabled, this
2505 * would have already happened in close and is redundant. */
2506 igb_release_hw_control(adapter);
2507
2508 unregister_netdev(netdev);
2509
Alexander Duyck047e0032009-10-27 15:49:27 +00002510 igb_clear_interrupt_scheme(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08002511
Alexander Duyck37680112009-02-19 20:40:30 -08002512#ifdef CONFIG_PCI_IOV
Greg Rosefa44f2f2013-01-17 01:03:06 -08002513 igb_disable_sriov(pdev);
Alexander Duyck37680112009-02-19 20:40:30 -08002514#endif
Alexander Duyck559e9c42009-10-27 23:52:50 +00002515
Alexander Duyck28b07592009-02-06 23:20:31 +00002516 iounmap(hw->hw_addr);
2517 if (hw->flash_address)
2518 iounmap(hw->flash_address);
Alexander Duyck559e9c42009-10-27 23:52:50 +00002519 pci_release_selected_regions(pdev,
2520 pci_select_bars(pdev, IORESOURCE_MEM));
Auke Kok9d5c8242008-01-24 02:22:38 -08002521
Carolyn Wyborny1128c752011-10-14 00:13:49 +00002522 kfree(adapter->shadow_vfta);
Auke Kok9d5c8242008-01-24 02:22:38 -08002523 free_netdev(netdev);
2524
Frans Pop19d5afd2009-10-02 10:04:12 -07002525 pci_disable_pcie_error_reporting(pdev);
Alexander Duyck40a914f2008-11-27 00:24:37 -08002526
Auke Kok9d5c8242008-01-24 02:22:38 -08002527 pci_disable_device(pdev);
2528}
2529
2530/**
Alexander Duycka6b623e2009-10-27 23:47:53 +00002531 * igb_probe_vfs - Initialize vf data storage and add VFs to pci config space
2532 * @adapter: board private structure to initialize
2533 *
2534 * This function initializes the vf specific data storage and then attempts to
2535 * allocate the VFs. The reason for ordering it this way is because it is much
2536 * mor expensive time wise to disable SR-IOV than it is to allocate and free
2537 * the memory for the VFs.
2538 **/
Bill Pemberton9f9a12f2012-12-03 09:24:25 -05002539static void igb_probe_vfs(struct igb_adapter *adapter)
Alexander Duycka6b623e2009-10-27 23:47:53 +00002540{
2541#ifdef CONFIG_PCI_IOV
2542 struct pci_dev *pdev = adapter->pdev;
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002543 struct e1000_hw *hw = &adapter->hw;
Alexander Duycka6b623e2009-10-27 23:47:53 +00002544
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002545 /* Virtualization features not supported on i210 family. */
2546 if ((hw->mac.type == e1000_i210) || (hw->mac.type == e1000_i211))
2547 return;
2548
Greg Rosefa44f2f2013-01-17 01:03:06 -08002549 igb_enable_sriov(pdev, max_vfs);
2550 pci_sriov_set_totalvfs(pdev, 7);
Alexander Duycka6b623e2009-10-27 23:47:53 +00002551
Alexander Duycka6b623e2009-10-27 23:47:53 +00002552#endif /* CONFIG_PCI_IOV */
2553}
2554
Greg Rosefa44f2f2013-01-17 01:03:06 -08002555static void igb_init_queue_configuration(struct igb_adapter *adapter)
Auke Kok9d5c8242008-01-24 02:22:38 -08002556{
2557 struct e1000_hw *hw = &adapter->hw;
Matthew Vick374a5422012-05-18 04:54:58 +00002558 u32 max_rss_queues;
Auke Kok9d5c8242008-01-24 02:22:38 -08002559
Matthew Vick374a5422012-05-18 04:54:58 +00002560 /* Determine the maximum number of RSS queues supported. */
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002561 switch (hw->mac.type) {
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002562 case e1000_i211:
Matthew Vick374a5422012-05-18 04:54:58 +00002563 max_rss_queues = IGB_MAX_RX_QUEUES_I211;
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002564 break;
Matthew Vick374a5422012-05-18 04:54:58 +00002565 case e1000_82575:
2566 case e1000_i210:
2567 max_rss_queues = IGB_MAX_RX_QUEUES_82575;
2568 break;
2569 case e1000_i350:
2570 /* I350 cannot do RSS and SR-IOV at the same time */
2571 if (!!adapter->vfs_allocated_count) {
2572 max_rss_queues = 1;
2573 break;
2574 }
2575 /* fall through */
2576 case e1000_82576:
2577 if (!!adapter->vfs_allocated_count) {
2578 max_rss_queues = 2;
2579 break;
2580 }
2581 /* fall through */
2582 case e1000_82580:
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002583 default:
Matthew Vick374a5422012-05-18 04:54:58 +00002584 max_rss_queues = IGB_MAX_RX_QUEUES;
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002585 break;
2586 }
Alexander Duycka99955f2009-11-12 18:37:19 +00002587
Matthew Vick374a5422012-05-18 04:54:58 +00002588 adapter->rss_queues = min_t(u32, max_rss_queues, num_online_cpus());
2589
2590 /* Determine if we need to pair queues. */
2591 switch (hw->mac.type) {
2592 case e1000_82575:
2593 case e1000_i211:
2594 /* Device supports enough interrupts without queue pairing. */
2595 break;
2596 case e1000_82576:
2597 /*
2598 * If VFs are going to be allocated with RSS queues then we
2599 * should pair the queues in order to conserve interrupts due
2600 * to limited supply.
2601 */
2602 if ((adapter->rss_queues > 1) &&
2603 (adapter->vfs_allocated_count > 6))
2604 adapter->flags |= IGB_FLAG_QUEUE_PAIRS;
2605 /* fall through */
2606 case e1000_82580:
2607 case e1000_i350:
2608 case e1000_i210:
2609 default:
2610 /*
2611 * If rss_queues > half of max_rss_queues, pair the queues in
2612 * order to conserve interrupts due to limited supply.
2613 */
2614 if (adapter->rss_queues > (max_rss_queues / 2))
2615 adapter->flags |= IGB_FLAG_QUEUE_PAIRS;
2616 break;
2617 }
Greg Rosefa44f2f2013-01-17 01:03:06 -08002618}
2619
2620/**
2621 * igb_sw_init - Initialize general software structures (struct igb_adapter)
2622 * @adapter: board private structure to initialize
2623 *
2624 * igb_sw_init initializes the Adapter private data structure.
2625 * Fields are initialized based on PCI device information and
2626 * OS network device settings (MTU size).
2627 **/
2628static int igb_sw_init(struct igb_adapter *adapter)
2629{
2630 struct e1000_hw *hw = &adapter->hw;
2631 struct net_device *netdev = adapter->netdev;
2632 struct pci_dev *pdev = adapter->pdev;
2633
2634 pci_read_config_word(pdev, PCI_COMMAND, &hw->bus.pci_cmd_word);
2635
2636 /* set default ring sizes */
2637 adapter->tx_ring_count = IGB_DEFAULT_TXD;
2638 adapter->rx_ring_count = IGB_DEFAULT_RXD;
2639
2640 /* set default ITR values */
2641 adapter->rx_itr_setting = IGB_DEFAULT_ITR;
2642 adapter->tx_itr_setting = IGB_DEFAULT_ITR;
2643
2644 /* set default work limits */
2645 adapter->tx_work_limit = IGB_DEFAULT_TX_WORK;
2646
2647 adapter->max_frame_size = netdev->mtu + ETH_HLEN + ETH_FCS_LEN +
2648 VLAN_HLEN;
2649 adapter->min_frame_size = ETH_ZLEN + ETH_FCS_LEN;
2650
2651 spin_lock_init(&adapter->stats64_lock);
2652#ifdef CONFIG_PCI_IOV
2653 switch (hw->mac.type) {
2654 case e1000_82576:
2655 case e1000_i350:
2656 if (max_vfs > 7) {
2657 dev_warn(&pdev->dev,
2658 "Maximum of 7 VFs per PF, using max\n");
2659 adapter->vfs_allocated_count = 7;
2660 } else
2661 adapter->vfs_allocated_count = max_vfs;
2662 if (adapter->vfs_allocated_count)
2663 dev_warn(&pdev->dev,
2664 "Enabling SR-IOV VFs using the module parameter is deprecated - please use the pci sysfs interface.\n");
2665 break;
2666 default:
2667 break;
2668 }
2669#endif /* CONFIG_PCI_IOV */
2670
2671 igb_init_queue_configuration(adapter);
Alexander Duycka99955f2009-11-12 18:37:19 +00002672
Carolyn Wyborny1128c752011-10-14 00:13:49 +00002673 /* Setup and initialize a copy of the hw vlan table array */
Joe Perchesb2adaca2013-02-03 17:43:58 +00002674 adapter->shadow_vfta = kcalloc(E1000_VLAN_FILTER_TBL_SIZE, sizeof(u32),
2675 GFP_ATOMIC);
Carolyn Wyborny1128c752011-10-14 00:13:49 +00002676
Alexander Duycka6b623e2009-10-27 23:47:53 +00002677 /* This call may decrease the number of queues */
Stefan Assmann53c7d062012-12-04 06:00:12 +00002678 if (igb_init_interrupt_scheme(adapter, true)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08002679 dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
2680 return -ENOMEM;
2681 }
2682
Alexander Duycka6b623e2009-10-27 23:47:53 +00002683 igb_probe_vfs(adapter);
2684
Auke Kok9d5c8242008-01-24 02:22:38 -08002685 /* Explicitly disable IRQ since the NIC can be in any state. */
2686 igb_irq_disable(adapter);
2687
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002688 if (hw->mac.type >= e1000_i350)
Carolyn Wyborny831ec0b2011-03-11 20:43:54 -08002689 adapter->flags &= ~IGB_FLAG_DMAC;
2690
Auke Kok9d5c8242008-01-24 02:22:38 -08002691 set_bit(__IGB_DOWN, &adapter->state);
2692 return 0;
2693}
2694
2695/**
2696 * igb_open - Called when a network interface is made active
2697 * @netdev: network interface device structure
2698 *
2699 * Returns 0 on success, negative value on failure
2700 *
2701 * The open entry point is called when a network interface is made
2702 * active by the system (IFF_UP). At this point all resources needed
2703 * for transmit and receive operations are allocated, the interrupt
2704 * handler is registered with the OS, the watchdog timer is started,
2705 * and the stack is notified that the interface is ready.
2706 **/
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002707static int __igb_open(struct net_device *netdev, bool resuming)
Auke Kok9d5c8242008-01-24 02:22:38 -08002708{
2709 struct igb_adapter *adapter = netdev_priv(netdev);
2710 struct e1000_hw *hw = &adapter->hw;
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002711 struct pci_dev *pdev = adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08002712 int err;
2713 int i;
2714
2715 /* disallow open during test */
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002716 if (test_bit(__IGB_TESTING, &adapter->state)) {
2717 WARN_ON(resuming);
Auke Kok9d5c8242008-01-24 02:22:38 -08002718 return -EBUSY;
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002719 }
2720
2721 if (!resuming)
2722 pm_runtime_get_sync(&pdev->dev);
Auke Kok9d5c8242008-01-24 02:22:38 -08002723
Jesse Brandeburgb168dfc2009-04-17 20:44:32 +00002724 netif_carrier_off(netdev);
2725
Auke Kok9d5c8242008-01-24 02:22:38 -08002726 /* allocate transmit descriptors */
2727 err = igb_setup_all_tx_resources(adapter);
2728 if (err)
2729 goto err_setup_tx;
2730
2731 /* allocate receive descriptors */
2732 err = igb_setup_all_rx_resources(adapter);
2733 if (err)
2734 goto err_setup_rx;
2735
Nick Nunley88a268c2010-02-17 01:01:59 +00002736 igb_power_up_link(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08002737
Auke Kok9d5c8242008-01-24 02:22:38 -08002738 /* before we allocate an interrupt, we must be ready to handle it.
2739 * Setting DEBUG_SHIRQ in the kernel makes it fire an interrupt
2740 * as soon as we call pci_request_irq, so we have to setup our
2741 * clean_rx handler before we do so. */
2742 igb_configure(adapter);
2743
2744 err = igb_request_irq(adapter);
2745 if (err)
2746 goto err_req_irq;
2747
Alexander Duyck0c2cc022012-09-25 00:31:22 +00002748 /* Notify the stack of the actual queue counts. */
2749 err = netif_set_real_num_tx_queues(adapter->netdev,
2750 adapter->num_tx_queues);
2751 if (err)
2752 goto err_set_queues;
2753
2754 err = netif_set_real_num_rx_queues(adapter->netdev,
2755 adapter->num_rx_queues);
2756 if (err)
2757 goto err_set_queues;
2758
Auke Kok9d5c8242008-01-24 02:22:38 -08002759 /* From here on the code is the same as igb_up() */
2760 clear_bit(__IGB_DOWN, &adapter->state);
2761
Alexander Duyck0d1ae7f2011-08-26 07:46:34 +00002762 for (i = 0; i < adapter->num_q_vectors; i++)
2763 napi_enable(&(adapter->q_vector[i]->napi));
Auke Kok9d5c8242008-01-24 02:22:38 -08002764
2765 /* Clear any pending interrupts. */
2766 rd32(E1000_ICR);
PJ Waskiewicz844290e2008-06-27 11:00:39 -07002767
2768 igb_irq_enable(adapter);
2769
Alexander Duyckd4960302009-10-27 15:53:45 +00002770 /* notify VFs that reset has been completed */
2771 if (adapter->vfs_allocated_count) {
2772 u32 reg_data = rd32(E1000_CTRL_EXT);
2773 reg_data |= E1000_CTRL_EXT_PFRSTD;
2774 wr32(E1000_CTRL_EXT, reg_data);
2775 }
2776
Jeff Kirsherd55b53f2008-07-18 04:33:03 -07002777 netif_tx_start_all_queues(netdev);
2778
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002779 if (!resuming)
2780 pm_runtime_put(&pdev->dev);
2781
Alexander Duyck25568a52009-10-27 23:49:59 +00002782 /* start the watchdog. */
2783 hw->mac.get_link_status = 1;
2784 schedule_work(&adapter->watchdog_task);
Auke Kok9d5c8242008-01-24 02:22:38 -08002785
2786 return 0;
2787
Alexander Duyck0c2cc022012-09-25 00:31:22 +00002788err_set_queues:
2789 igb_free_irq(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08002790err_req_irq:
2791 igb_release_hw_control(adapter);
Nick Nunley88a268c2010-02-17 01:01:59 +00002792 igb_power_down_link(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08002793 igb_free_all_rx_resources(adapter);
2794err_setup_rx:
2795 igb_free_all_tx_resources(adapter);
2796err_setup_tx:
2797 igb_reset(adapter);
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002798 if (!resuming)
2799 pm_runtime_put(&pdev->dev);
Auke Kok9d5c8242008-01-24 02:22:38 -08002800
2801 return err;
2802}
2803
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002804static int igb_open(struct net_device *netdev)
2805{
2806 return __igb_open(netdev, false);
2807}
2808
Auke Kok9d5c8242008-01-24 02:22:38 -08002809/**
2810 * igb_close - Disables a network interface
2811 * @netdev: network interface device structure
2812 *
2813 * Returns 0, this is not allowed to fail
2814 *
2815 * The close entry point is called when an interface is de-activated
2816 * by the OS. The hardware is still under the driver's control, but
2817 * needs to be disabled. A global MAC reset is issued to stop the
2818 * hardware, and all transmit and receive resources are freed.
2819 **/
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002820static int __igb_close(struct net_device *netdev, bool suspending)
Auke Kok9d5c8242008-01-24 02:22:38 -08002821{
2822 struct igb_adapter *adapter = netdev_priv(netdev);
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002823 struct pci_dev *pdev = adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08002824
2825 WARN_ON(test_bit(__IGB_RESETTING, &adapter->state));
Auke Kok9d5c8242008-01-24 02:22:38 -08002826
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002827 if (!suspending)
2828 pm_runtime_get_sync(&pdev->dev);
2829
2830 igb_down(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08002831 igb_free_irq(adapter);
2832
2833 igb_free_all_tx_resources(adapter);
2834 igb_free_all_rx_resources(adapter);
2835
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002836 if (!suspending)
2837 pm_runtime_put_sync(&pdev->dev);
Auke Kok9d5c8242008-01-24 02:22:38 -08002838 return 0;
2839}
2840
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002841static int igb_close(struct net_device *netdev)
2842{
2843 return __igb_close(netdev, false);
2844}
2845
Auke Kok9d5c8242008-01-24 02:22:38 -08002846/**
2847 * igb_setup_tx_resources - allocate Tx resources (Descriptors)
Auke Kok9d5c8242008-01-24 02:22:38 -08002848 * @tx_ring: tx descriptor ring (for a specific queue) to setup
2849 *
2850 * Return 0 on success, negative on failure
2851 **/
Alexander Duyck80785292009-10-27 15:51:47 +00002852int igb_setup_tx_resources(struct igb_ring *tx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08002853{
Alexander Duyck59d71982010-04-27 13:09:25 +00002854 struct device *dev = tx_ring->dev;
Auke Kok9d5c8242008-01-24 02:22:38 -08002855 int size;
2856
Alexander Duyck06034642011-08-26 07:44:22 +00002857 size = sizeof(struct igb_tx_buffer) * tx_ring->count;
Alexander Duyckf33005a2012-09-13 06:27:55 +00002858
2859 tx_ring->tx_buffer_info = vzalloc(size);
Alexander Duyck06034642011-08-26 07:44:22 +00002860 if (!tx_ring->tx_buffer_info)
Auke Kok9d5c8242008-01-24 02:22:38 -08002861 goto err;
Auke Kok9d5c8242008-01-24 02:22:38 -08002862
2863 /* round up to nearest 4K */
Alexander Duyck85e8d002009-02-16 00:00:20 -08002864 tx_ring->size = tx_ring->count * sizeof(union e1000_adv_tx_desc);
Auke Kok9d5c8242008-01-24 02:22:38 -08002865 tx_ring->size = ALIGN(tx_ring->size, 4096);
2866
Alexander Duyck5536d212012-09-25 00:31:17 +00002867 tx_ring->desc = dma_alloc_coherent(dev, tx_ring->size,
2868 &tx_ring->dma, GFP_KERNEL);
Auke Kok9d5c8242008-01-24 02:22:38 -08002869 if (!tx_ring->desc)
2870 goto err;
2871
Auke Kok9d5c8242008-01-24 02:22:38 -08002872 tx_ring->next_to_use = 0;
2873 tx_ring->next_to_clean = 0;
Alexander Duyck81c2fc22011-08-26 07:45:20 +00002874
Auke Kok9d5c8242008-01-24 02:22:38 -08002875 return 0;
2876
2877err:
Alexander Duyck06034642011-08-26 07:44:22 +00002878 vfree(tx_ring->tx_buffer_info);
Alexander Duyckf33005a2012-09-13 06:27:55 +00002879 tx_ring->tx_buffer_info = NULL;
2880 dev_err(dev, "Unable to allocate memory for the Tx descriptor ring\n");
Auke Kok9d5c8242008-01-24 02:22:38 -08002881 return -ENOMEM;
2882}
2883
2884/**
2885 * igb_setup_all_tx_resources - wrapper to allocate Tx resources
2886 * (Descriptors) for all queues
2887 * @adapter: board private structure
2888 *
2889 * Return 0 on success, negative on failure
2890 **/
2891static int igb_setup_all_tx_resources(struct igb_adapter *adapter)
2892{
Alexander Duyck439705e2009-10-27 23:49:20 +00002893 struct pci_dev *pdev = adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08002894 int i, err = 0;
2895
2896 for (i = 0; i < adapter->num_tx_queues; i++) {
Alexander Duyck3025a442010-02-17 01:02:39 +00002897 err = igb_setup_tx_resources(adapter->tx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08002898 if (err) {
Alexander Duyck439705e2009-10-27 23:49:20 +00002899 dev_err(&pdev->dev,
Auke Kok9d5c8242008-01-24 02:22:38 -08002900 "Allocation for Tx Queue %u failed\n", i);
2901 for (i--; i >= 0; i--)
Alexander Duyck3025a442010-02-17 01:02:39 +00002902 igb_free_tx_resources(adapter->tx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08002903 break;
2904 }
2905 }
2906
2907 return err;
2908}
2909
2910/**
Alexander Duyck85b430b2009-10-27 15:50:29 +00002911 * igb_setup_tctl - configure the transmit control registers
2912 * @adapter: Board private structure
Auke Kok9d5c8242008-01-24 02:22:38 -08002913 **/
Alexander Duyckd7ee5b32009-10-27 15:54:23 +00002914void igb_setup_tctl(struct igb_adapter *adapter)
Auke Kok9d5c8242008-01-24 02:22:38 -08002915{
Auke Kok9d5c8242008-01-24 02:22:38 -08002916 struct e1000_hw *hw = &adapter->hw;
2917 u32 tctl;
Auke Kok9d5c8242008-01-24 02:22:38 -08002918
Alexander Duyck85b430b2009-10-27 15:50:29 +00002919 /* disable queue 0 which is enabled by default on 82575 and 82576 */
2920 wr32(E1000_TXDCTL(0), 0);
Auke Kok9d5c8242008-01-24 02:22:38 -08002921
2922 /* Program the Transmit Control Register */
Auke Kok9d5c8242008-01-24 02:22:38 -08002923 tctl = rd32(E1000_TCTL);
2924 tctl &= ~E1000_TCTL_CT;
2925 tctl |= E1000_TCTL_PSP | E1000_TCTL_RTLC |
2926 (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
2927
2928 igb_config_collision_dist(hw);
2929
Auke Kok9d5c8242008-01-24 02:22:38 -08002930 /* Enable transmits */
2931 tctl |= E1000_TCTL_EN;
2932
2933 wr32(E1000_TCTL, tctl);
2934}
2935
2936/**
Alexander Duyck85b430b2009-10-27 15:50:29 +00002937 * igb_configure_tx_ring - Configure transmit ring after Reset
2938 * @adapter: board private structure
2939 * @ring: tx ring to configure
2940 *
2941 * Configure a transmit ring after a reset.
2942 **/
Alexander Duyckd7ee5b32009-10-27 15:54:23 +00002943void igb_configure_tx_ring(struct igb_adapter *adapter,
2944 struct igb_ring *ring)
Alexander Duyck85b430b2009-10-27 15:50:29 +00002945{
2946 struct e1000_hw *hw = &adapter->hw;
Alexander Duycka74420e2011-08-26 07:43:27 +00002947 u32 txdctl = 0;
Alexander Duyck85b430b2009-10-27 15:50:29 +00002948 u64 tdba = ring->dma;
2949 int reg_idx = ring->reg_idx;
2950
2951 /* disable the queue */
Alexander Duycka74420e2011-08-26 07:43:27 +00002952 wr32(E1000_TXDCTL(reg_idx), 0);
Alexander Duyck85b430b2009-10-27 15:50:29 +00002953 wrfl();
2954 mdelay(10);
2955
2956 wr32(E1000_TDLEN(reg_idx),
2957 ring->count * sizeof(union e1000_adv_tx_desc));
2958 wr32(E1000_TDBAL(reg_idx),
2959 tdba & 0x00000000ffffffffULL);
2960 wr32(E1000_TDBAH(reg_idx), tdba >> 32);
2961
Alexander Duyckfce99e32009-10-27 15:51:27 +00002962 ring->tail = hw->hw_addr + E1000_TDT(reg_idx);
Alexander Duycka74420e2011-08-26 07:43:27 +00002963 wr32(E1000_TDH(reg_idx), 0);
Alexander Duyckfce99e32009-10-27 15:51:27 +00002964 writel(0, ring->tail);
Alexander Duyck85b430b2009-10-27 15:50:29 +00002965
2966 txdctl |= IGB_TX_PTHRESH;
2967 txdctl |= IGB_TX_HTHRESH << 8;
2968 txdctl |= IGB_TX_WTHRESH << 16;
2969
2970 txdctl |= E1000_TXDCTL_QUEUE_ENABLE;
2971 wr32(E1000_TXDCTL(reg_idx), txdctl);
2972}
2973
2974/**
2975 * igb_configure_tx - Configure transmit Unit after Reset
2976 * @adapter: board private structure
2977 *
2978 * Configure the Tx unit of the MAC after a reset.
2979 **/
2980static void igb_configure_tx(struct igb_adapter *adapter)
2981{
2982 int i;
2983
2984 for (i = 0; i < adapter->num_tx_queues; i++)
Alexander Duyck3025a442010-02-17 01:02:39 +00002985 igb_configure_tx_ring(adapter, adapter->tx_ring[i]);
Alexander Duyck85b430b2009-10-27 15:50:29 +00002986}
2987
2988/**
Auke Kok9d5c8242008-01-24 02:22:38 -08002989 * igb_setup_rx_resources - allocate Rx resources (Descriptors)
Auke Kok9d5c8242008-01-24 02:22:38 -08002990 * @rx_ring: rx descriptor ring (for a specific queue) to setup
2991 *
2992 * Returns 0 on success, negative on failure
2993 **/
Alexander Duyck80785292009-10-27 15:51:47 +00002994int igb_setup_rx_resources(struct igb_ring *rx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08002995{
Alexander Duyck59d71982010-04-27 13:09:25 +00002996 struct device *dev = rx_ring->dev;
Alexander Duyckf33005a2012-09-13 06:27:55 +00002997 int size;
Auke Kok9d5c8242008-01-24 02:22:38 -08002998
Alexander Duyck06034642011-08-26 07:44:22 +00002999 size = sizeof(struct igb_rx_buffer) * rx_ring->count;
Alexander Duyckf33005a2012-09-13 06:27:55 +00003000
3001 rx_ring->rx_buffer_info = vzalloc(size);
Alexander Duyck06034642011-08-26 07:44:22 +00003002 if (!rx_ring->rx_buffer_info)
Auke Kok9d5c8242008-01-24 02:22:38 -08003003 goto err;
Auke Kok9d5c8242008-01-24 02:22:38 -08003004
Auke Kok9d5c8242008-01-24 02:22:38 -08003005 /* Round up to nearest 4K */
Alexander Duyckf33005a2012-09-13 06:27:55 +00003006 rx_ring->size = rx_ring->count * sizeof(union e1000_adv_rx_desc);
Auke Kok9d5c8242008-01-24 02:22:38 -08003007 rx_ring->size = ALIGN(rx_ring->size, 4096);
3008
Alexander Duyck5536d212012-09-25 00:31:17 +00003009 rx_ring->desc = dma_alloc_coherent(dev, rx_ring->size,
3010 &rx_ring->dma, GFP_KERNEL);
Auke Kok9d5c8242008-01-24 02:22:38 -08003011 if (!rx_ring->desc)
3012 goto err;
3013
Alexander Duyckcbc8e552012-09-25 00:31:02 +00003014 rx_ring->next_to_alloc = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08003015 rx_ring->next_to_clean = 0;
3016 rx_ring->next_to_use = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08003017
Auke Kok9d5c8242008-01-24 02:22:38 -08003018 return 0;
3019
3020err:
Alexander Duyck06034642011-08-26 07:44:22 +00003021 vfree(rx_ring->rx_buffer_info);
3022 rx_ring->rx_buffer_info = NULL;
Alexander Duyckf33005a2012-09-13 06:27:55 +00003023 dev_err(dev, "Unable to allocate memory for the Rx descriptor ring\n");
Auke Kok9d5c8242008-01-24 02:22:38 -08003024 return -ENOMEM;
3025}
3026
3027/**
3028 * igb_setup_all_rx_resources - wrapper to allocate Rx resources
3029 * (Descriptors) for all queues
3030 * @adapter: board private structure
3031 *
3032 * Return 0 on success, negative on failure
3033 **/
3034static int igb_setup_all_rx_resources(struct igb_adapter *adapter)
3035{
Alexander Duyck439705e2009-10-27 23:49:20 +00003036 struct pci_dev *pdev = adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08003037 int i, err = 0;
3038
3039 for (i = 0; i < adapter->num_rx_queues; i++) {
Alexander Duyck3025a442010-02-17 01:02:39 +00003040 err = igb_setup_rx_resources(adapter->rx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08003041 if (err) {
Alexander Duyck439705e2009-10-27 23:49:20 +00003042 dev_err(&pdev->dev,
Auke Kok9d5c8242008-01-24 02:22:38 -08003043 "Allocation for Rx Queue %u failed\n", i);
3044 for (i--; i >= 0; i--)
Alexander Duyck3025a442010-02-17 01:02:39 +00003045 igb_free_rx_resources(adapter->rx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08003046 break;
3047 }
3048 }
3049
3050 return err;
3051}
3052
3053/**
Alexander Duyck06cf2662009-10-27 15:53:25 +00003054 * igb_setup_mrqc - configure the multiple receive queue control registers
3055 * @adapter: Board private structure
3056 **/
3057static void igb_setup_mrqc(struct igb_adapter *adapter)
3058{
3059 struct e1000_hw *hw = &adapter->hw;
3060 u32 mrqc, rxcsum;
Alexander Duyck797fd4b2012-09-13 06:28:11 +00003061 u32 j, num_rx_queues, shift = 0;
Alexander Duycka57fe232012-09-13 06:28:16 +00003062 static const u32 rsskey[10] = { 0xDA565A6D, 0xC20E5B25, 0x3D256741,
3063 0xB08FA343, 0xCB2BCAD0, 0xB4307BAE,
3064 0xA32DCB77, 0x0CF23080, 0x3BB7426A,
3065 0xFA01ACBE };
Alexander Duyck06cf2662009-10-27 15:53:25 +00003066
3067 /* Fill out hash function seeds */
Alexander Duycka57fe232012-09-13 06:28:16 +00003068 for (j = 0; j < 10; j++)
3069 wr32(E1000_RSSRK(j), rsskey[j]);
Alexander Duyck06cf2662009-10-27 15:53:25 +00003070
Alexander Duycka99955f2009-11-12 18:37:19 +00003071 num_rx_queues = adapter->rss_queues;
Alexander Duyck06cf2662009-10-27 15:53:25 +00003072
Alexander Duyck797fd4b2012-09-13 06:28:11 +00003073 switch (hw->mac.type) {
3074 case e1000_82575:
3075 shift = 6;
3076 break;
3077 case e1000_82576:
3078 /* 82576 supports 2 RSS queues for SR-IOV */
3079 if (adapter->vfs_allocated_count) {
Alexander Duyck06cf2662009-10-27 15:53:25 +00003080 shift = 3;
3081 num_rx_queues = 2;
Alexander Duyck06cf2662009-10-27 15:53:25 +00003082 }
Alexander Duyck797fd4b2012-09-13 06:28:11 +00003083 break;
3084 default:
3085 break;
Alexander Duyck06cf2662009-10-27 15:53:25 +00003086 }
3087
Alexander Duyck797fd4b2012-09-13 06:28:11 +00003088 /*
3089 * Populate the indirection table 4 entries at a time. To do this
3090 * we are generating the results for n and n+2 and then interleaving
3091 * those with the results with n+1 and n+3.
3092 */
3093 for (j = 0; j < 32; j++) {
3094 /* first pass generates n and n+2 */
3095 u32 base = ((j * 0x00040004) + 0x00020000) * num_rx_queues;
3096 u32 reta = (base & 0x07800780) >> (7 - shift);
3097
3098 /* second pass generates n+1 and n+3 */
3099 base += 0x00010001 * num_rx_queues;
3100 reta |= (base & 0x07800780) << (1 + shift);
3101
3102 wr32(E1000_RETA(j), reta);
Alexander Duyck06cf2662009-10-27 15:53:25 +00003103 }
3104
3105 /*
3106 * Disable raw packet checksumming so that RSS hash is placed in
3107 * descriptor on writeback. No need to enable TCP/UDP/IP checksum
3108 * offloads as they are enabled by default
3109 */
3110 rxcsum = rd32(E1000_RXCSUM);
3111 rxcsum |= E1000_RXCSUM_PCSD;
3112
3113 if (adapter->hw.mac.type >= e1000_82576)
3114 /* Enable Receive Checksum Offload for SCTP */
3115 rxcsum |= E1000_RXCSUM_CRCOFL;
3116
3117 /* Don't need to set TUOFL or IPOFL, they default to 1 */
3118 wr32(E1000_RXCSUM, rxcsum);
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00003119
Akeem G. Abodunrin039454a2012-11-13 04:03:21 +00003120 /* Generate RSS hash based on packet types, TCP/UDP
3121 * port numbers and/or IPv4/v6 src and dst addresses
3122 */
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00003123 mrqc = E1000_MRQC_RSS_FIELD_IPV4 |
3124 E1000_MRQC_RSS_FIELD_IPV4_TCP |
3125 E1000_MRQC_RSS_FIELD_IPV6 |
3126 E1000_MRQC_RSS_FIELD_IPV6_TCP |
3127 E1000_MRQC_RSS_FIELD_IPV6_TCP_EX;
Alexander Duyck06cf2662009-10-27 15:53:25 +00003128
Akeem G. Abodunrin039454a2012-11-13 04:03:21 +00003129 if (adapter->flags & IGB_FLAG_RSS_FIELD_IPV4_UDP)
3130 mrqc |= E1000_MRQC_RSS_FIELD_IPV4_UDP;
3131 if (adapter->flags & IGB_FLAG_RSS_FIELD_IPV6_UDP)
3132 mrqc |= E1000_MRQC_RSS_FIELD_IPV6_UDP;
3133
Alexander Duyck06cf2662009-10-27 15:53:25 +00003134 /* If VMDq is enabled then we set the appropriate mode for that, else
3135 * we default to RSS so that an RSS hash is calculated per packet even
3136 * if we are only using one queue */
3137 if (adapter->vfs_allocated_count) {
3138 if (hw->mac.type > e1000_82575) {
3139 /* Set the default pool for the PF's first queue */
3140 u32 vtctl = rd32(E1000_VT_CTL);
3141 vtctl &= ~(E1000_VT_CTL_DEFAULT_POOL_MASK |
3142 E1000_VT_CTL_DISABLE_DEF_POOL);
3143 vtctl |= adapter->vfs_allocated_count <<
3144 E1000_VT_CTL_DEFAULT_POOL_SHIFT;
3145 wr32(E1000_VT_CTL, vtctl);
3146 }
Alexander Duycka99955f2009-11-12 18:37:19 +00003147 if (adapter->rss_queues > 1)
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00003148 mrqc |= E1000_MRQC_ENABLE_VMDQ_RSS_2Q;
Alexander Duyck06cf2662009-10-27 15:53:25 +00003149 else
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00003150 mrqc |= E1000_MRQC_ENABLE_VMDQ;
Alexander Duyck06cf2662009-10-27 15:53:25 +00003151 } else {
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00003152 if (hw->mac.type != e1000_i211)
3153 mrqc |= E1000_MRQC_ENABLE_RSS_4Q;
Alexander Duyck06cf2662009-10-27 15:53:25 +00003154 }
3155 igb_vmm_control(adapter);
3156
Alexander Duyck06cf2662009-10-27 15:53:25 +00003157 wr32(E1000_MRQC, mrqc);
3158}
3159
3160/**
Auke Kok9d5c8242008-01-24 02:22:38 -08003161 * igb_setup_rctl - configure the receive control registers
3162 * @adapter: Board private structure
3163 **/
Alexander Duyckd7ee5b32009-10-27 15:54:23 +00003164void igb_setup_rctl(struct igb_adapter *adapter)
Auke Kok9d5c8242008-01-24 02:22:38 -08003165{
3166 struct e1000_hw *hw = &adapter->hw;
3167 u32 rctl;
Auke Kok9d5c8242008-01-24 02:22:38 -08003168
3169 rctl = rd32(E1000_RCTL);
3170
3171 rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
Alexander Duyck69d728b2008-11-25 01:04:03 -08003172 rctl &= ~(E1000_RCTL_LBM_TCVR | E1000_RCTL_LBM_MAC);
Auke Kok9d5c8242008-01-24 02:22:38 -08003173
Alexander Duyck69d728b2008-11-25 01:04:03 -08003174 rctl |= E1000_RCTL_EN | E1000_RCTL_BAM | E1000_RCTL_RDMTS_HALF |
Alexander Duyck28b07592009-02-06 23:20:31 +00003175 (hw->mac.mc_filter_type << E1000_RCTL_MO_SHIFT);
Auke Kok9d5c8242008-01-24 02:22:38 -08003176
Auke Kok87cb7e82008-07-08 15:08:29 -07003177 /*
3178 * enable stripping of CRC. It's unlikely this will break BMC
3179 * redirection as it did with e1000. Newer features require
3180 * that the HW strips the CRC.
Alexander Duyck73cd78f2009-02-12 18:16:59 +00003181 */
Auke Kok87cb7e82008-07-08 15:08:29 -07003182 rctl |= E1000_RCTL_SECRC;
Auke Kok9d5c8242008-01-24 02:22:38 -08003183
Alexander Duyck559e9c42009-10-27 23:52:50 +00003184 /* disable store bad packets and clear size bits. */
Alexander Duyckec54d7d2009-01-31 00:52:57 -08003185 rctl &= ~(E1000_RCTL_SBP | E1000_RCTL_SZ_256);
Auke Kok9d5c8242008-01-24 02:22:38 -08003186
Alexander Duyck6ec43fe2009-10-27 15:50:48 +00003187 /* enable LPE to prevent packets larger than max_frame_size */
3188 rctl |= E1000_RCTL_LPE;
Auke Kok9d5c8242008-01-24 02:22:38 -08003189
Alexander Duyck952f72a2009-10-27 15:51:07 +00003190 /* disable queue 0 to prevent tail write w/o re-config */
3191 wr32(E1000_RXDCTL(0), 0);
Auke Kok9d5c8242008-01-24 02:22:38 -08003192
Alexander Duycke1739522009-02-19 20:39:44 -08003193 /* Attention!!! For SR-IOV PF driver operations you must enable
3194 * queue drop for all VF and PF queues to prevent head of line blocking
3195 * if an un-trusted VF does not provide descriptors to hardware.
3196 */
3197 if (adapter->vfs_allocated_count) {
Alexander Duycke1739522009-02-19 20:39:44 -08003198 /* set all queue drop enable bits */
3199 wr32(E1000_QDE, ALL_QUEUES);
Alexander Duycke1739522009-02-19 20:39:44 -08003200 }
3201
Ben Greear89eaefb2012-03-06 09:41:58 +00003202 /* This is useful for sniffing bad packets. */
3203 if (adapter->netdev->features & NETIF_F_RXALL) {
3204 /* UPE and MPE will be handled by normal PROMISC logic
3205 * in e1000e_set_rx_mode */
3206 rctl |= (E1000_RCTL_SBP | /* Receive bad packets */
3207 E1000_RCTL_BAM | /* RX All Bcast Pkts */
3208 E1000_RCTL_PMCF); /* RX All MAC Ctrl Pkts */
3209
3210 rctl &= ~(E1000_RCTL_VFE | /* Disable VLAN filter */
3211 E1000_RCTL_DPF | /* Allow filtered pause */
3212 E1000_RCTL_CFIEN); /* Dis VLAN CFIEN Filter */
3213 /* Do not mess with E1000_CTRL_VME, it affects transmit as well,
3214 * and that breaks VLANs.
3215 */
3216 }
3217
Auke Kok9d5c8242008-01-24 02:22:38 -08003218 wr32(E1000_RCTL, rctl);
3219}
3220
Alexander Duyck7d5753f2009-10-27 23:47:16 +00003221static inline int igb_set_vf_rlpml(struct igb_adapter *adapter, int size,
3222 int vfn)
3223{
3224 struct e1000_hw *hw = &adapter->hw;
3225 u32 vmolr;
3226
3227 /* if it isn't the PF check to see if VFs are enabled and
3228 * increase the size to support vlan tags */
3229 if (vfn < adapter->vfs_allocated_count &&
3230 adapter->vf_data[vfn].vlans_enabled)
3231 size += VLAN_TAG_SIZE;
3232
3233 vmolr = rd32(E1000_VMOLR(vfn));
3234 vmolr &= ~E1000_VMOLR_RLPML_MASK;
3235 vmolr |= size | E1000_VMOLR_LPE;
3236 wr32(E1000_VMOLR(vfn), vmolr);
3237
3238 return 0;
3239}
3240
Auke Kok9d5c8242008-01-24 02:22:38 -08003241/**
Alexander Duycke1739522009-02-19 20:39:44 -08003242 * igb_rlpml_set - set maximum receive packet size
3243 * @adapter: board private structure
3244 *
3245 * Configure maximum receivable packet size.
3246 **/
3247static void igb_rlpml_set(struct igb_adapter *adapter)
3248{
Alexander Duyck153285f2011-08-26 07:43:32 +00003249 u32 max_frame_size = adapter->max_frame_size;
Alexander Duycke1739522009-02-19 20:39:44 -08003250 struct e1000_hw *hw = &adapter->hw;
3251 u16 pf_id = adapter->vfs_allocated_count;
3252
Alexander Duycke1739522009-02-19 20:39:44 -08003253 if (pf_id) {
3254 igb_set_vf_rlpml(adapter, max_frame_size, pf_id);
Alexander Duyck153285f2011-08-26 07:43:32 +00003255 /*
3256 * If we're in VMDQ or SR-IOV mode, then set global RLPML
3257 * to our max jumbo frame size, in case we need to enable
3258 * jumbo frames on one of the rings later.
3259 * This will not pass over-length frames into the default
3260 * queue because it's gated by the VMOLR.RLPML.
3261 */
Alexander Duyck7d5753f2009-10-27 23:47:16 +00003262 max_frame_size = MAX_JUMBO_FRAME_SIZE;
Alexander Duycke1739522009-02-19 20:39:44 -08003263 }
3264
3265 wr32(E1000_RLPML, max_frame_size);
3266}
3267
Williams, Mitch A8151d292010-02-10 01:44:24 +00003268static inline void igb_set_vmolr(struct igb_adapter *adapter,
3269 int vfn, bool aupe)
Alexander Duyck7d5753f2009-10-27 23:47:16 +00003270{
3271 struct e1000_hw *hw = &adapter->hw;
3272 u32 vmolr;
3273
3274 /*
3275 * This register exists only on 82576 and newer so if we are older then
3276 * we should exit and do nothing
3277 */
3278 if (hw->mac.type < e1000_82576)
3279 return;
3280
3281 vmolr = rd32(E1000_VMOLR(vfn));
Williams, Mitch A8151d292010-02-10 01:44:24 +00003282 vmolr |= E1000_VMOLR_STRVLAN; /* Strip vlan tags */
3283 if (aupe)
3284 vmolr |= E1000_VMOLR_AUPE; /* Accept untagged packets */
3285 else
3286 vmolr &= ~(E1000_VMOLR_AUPE); /* Tagged packets ONLY */
Alexander Duyck7d5753f2009-10-27 23:47:16 +00003287
3288 /* clear all bits that might not be set */
3289 vmolr &= ~(E1000_VMOLR_BAM | E1000_VMOLR_RSSE);
3290
Alexander Duycka99955f2009-11-12 18:37:19 +00003291 if (adapter->rss_queues > 1 && vfn == adapter->vfs_allocated_count)
Alexander Duyck7d5753f2009-10-27 23:47:16 +00003292 vmolr |= E1000_VMOLR_RSSE; /* enable RSS */
3293 /*
3294 * for VMDq only allow the VFs and pool 0 to accept broadcast and
3295 * multicast packets
3296 */
3297 if (vfn <= adapter->vfs_allocated_count)
3298 vmolr |= E1000_VMOLR_BAM; /* Accept broadcast */
3299
3300 wr32(E1000_VMOLR(vfn), vmolr);
3301}
3302
Alexander Duycke1739522009-02-19 20:39:44 -08003303/**
Alexander Duyck85b430b2009-10-27 15:50:29 +00003304 * igb_configure_rx_ring - Configure a receive ring after Reset
3305 * @adapter: board private structure
3306 * @ring: receive ring to be configured
3307 *
3308 * Configure the Rx unit of the MAC after a reset.
3309 **/
Alexander Duyckd7ee5b32009-10-27 15:54:23 +00003310void igb_configure_rx_ring(struct igb_adapter *adapter,
3311 struct igb_ring *ring)
Alexander Duyck85b430b2009-10-27 15:50:29 +00003312{
3313 struct e1000_hw *hw = &adapter->hw;
3314 u64 rdba = ring->dma;
3315 int reg_idx = ring->reg_idx;
Alexander Duycka74420e2011-08-26 07:43:27 +00003316 u32 srrctl = 0, rxdctl = 0;
Alexander Duyck85b430b2009-10-27 15:50:29 +00003317
3318 /* disable the queue */
Alexander Duycka74420e2011-08-26 07:43:27 +00003319 wr32(E1000_RXDCTL(reg_idx), 0);
Alexander Duyck85b430b2009-10-27 15:50:29 +00003320
3321 /* Set DMA base address registers */
3322 wr32(E1000_RDBAL(reg_idx),
3323 rdba & 0x00000000ffffffffULL);
3324 wr32(E1000_RDBAH(reg_idx), rdba >> 32);
3325 wr32(E1000_RDLEN(reg_idx),
3326 ring->count * sizeof(union e1000_adv_rx_desc));
3327
3328 /* initialize head and tail */
Alexander Duyckfce99e32009-10-27 15:51:27 +00003329 ring->tail = hw->hw_addr + E1000_RDT(reg_idx);
Alexander Duycka74420e2011-08-26 07:43:27 +00003330 wr32(E1000_RDH(reg_idx), 0);
Alexander Duyckfce99e32009-10-27 15:51:27 +00003331 writel(0, ring->tail);
Alexander Duyck85b430b2009-10-27 15:50:29 +00003332
Alexander Duyck952f72a2009-10-27 15:51:07 +00003333 /* set descriptor configuration */
Alexander Duyck44390ca2011-08-26 07:43:38 +00003334 srrctl = IGB_RX_HDR_LEN << E1000_SRRCTL_BSIZEHDRSIZE_SHIFT;
Alexander Duyckde78d1f2012-09-25 00:31:12 +00003335 srrctl |= IGB_RX_BUFSZ >> E1000_SRRCTL_BSIZEPKT_SHIFT;
Alexander Duyck1a1c2252012-09-25 00:30:52 +00003336 srrctl |= E1000_SRRCTL_DESCTYPE_ADV_ONEBUF;
Alexander Duyck06218a82011-08-26 07:46:55 +00003337 if (hw->mac.type >= e1000_82580)
Nick Nunley757b77e2010-03-26 11:36:47 +00003338 srrctl |= E1000_SRRCTL_TIMESTAMP;
Nick Nunleye6bdb6f2010-02-17 01:03:38 +00003339 /* Only set Drop Enable if we are supporting multiple queues */
3340 if (adapter->vfs_allocated_count || adapter->num_rx_queues > 1)
3341 srrctl |= E1000_SRRCTL_DROP_EN;
Alexander Duyck952f72a2009-10-27 15:51:07 +00003342
3343 wr32(E1000_SRRCTL(reg_idx), srrctl);
3344
Alexander Duyck7d5753f2009-10-27 23:47:16 +00003345 /* set filtering for VMDQ pools */
Williams, Mitch A8151d292010-02-10 01:44:24 +00003346 igb_set_vmolr(adapter, reg_idx & 0x7, true);
Alexander Duyck7d5753f2009-10-27 23:47:16 +00003347
Alexander Duyck85b430b2009-10-27 15:50:29 +00003348 rxdctl |= IGB_RX_PTHRESH;
3349 rxdctl |= IGB_RX_HTHRESH << 8;
3350 rxdctl |= IGB_RX_WTHRESH << 16;
Alexander Duycka74420e2011-08-26 07:43:27 +00003351
3352 /* enable receive descriptor fetching */
3353 rxdctl |= E1000_RXDCTL_QUEUE_ENABLE;
Alexander Duyck85b430b2009-10-27 15:50:29 +00003354 wr32(E1000_RXDCTL(reg_idx), rxdctl);
3355}
3356
Alexander Duyck74e238e2013-02-02 05:07:11 +00003357static void igb_set_rx_buffer_len(struct igb_adapter *adapter,
3358 struct igb_ring *rx_ring)
3359{
3360#define IGB_MAX_BUILD_SKB_SIZE \
3361 (SKB_WITH_OVERHEAD(IGB_RX_BUFSZ) - \
3362 (NET_SKB_PAD + NET_IP_ALIGN + IGB_TS_HDR_LEN))
3363
3364 /* set build_skb flag */
3365 if (adapter->max_frame_size <= IGB_MAX_BUILD_SKB_SIZE)
3366 set_ring_build_skb_enabled(rx_ring);
3367 else
3368 clear_ring_build_skb_enabled(rx_ring);
3369}
3370
Alexander Duyck85b430b2009-10-27 15:50:29 +00003371/**
Auke Kok9d5c8242008-01-24 02:22:38 -08003372 * igb_configure_rx - Configure receive Unit after Reset
3373 * @adapter: board private structure
3374 *
3375 * Configure the Rx unit of the MAC after a reset.
3376 **/
3377static void igb_configure_rx(struct igb_adapter *adapter)
3378{
Hannes Eder91075842009-02-18 19:36:04 -08003379 int i;
Auke Kok9d5c8242008-01-24 02:22:38 -08003380
Alexander Duyck68d480c2009-10-05 06:33:08 +00003381 /* set UTA to appropriate mode */
3382 igb_set_uta(adapter);
3383
Alexander Duyck26ad9172009-10-05 06:32:49 +00003384 /* set the correct pool for the PF default MAC address in entry 0 */
3385 igb_rar_set_qsel(adapter, adapter->hw.mac.addr, 0,
3386 adapter->vfs_allocated_count);
3387
Alexander Duyck06cf2662009-10-27 15:53:25 +00003388 /* Setup the HW Rx Head and Tail Descriptor Pointers and
3389 * the Base and Length of the Rx Descriptor Ring */
Alexander Duyck74e238e2013-02-02 05:07:11 +00003390 for (i = 0; i < adapter->num_rx_queues; i++) {
3391 struct igb_ring *rx_ring = adapter->rx_ring[i];
3392 igb_set_rx_buffer_len(adapter, rx_ring);
3393 igb_configure_rx_ring(adapter, rx_ring);
3394 }
Auke Kok9d5c8242008-01-24 02:22:38 -08003395}
3396
3397/**
3398 * igb_free_tx_resources - Free Tx Resources per Queue
Auke Kok9d5c8242008-01-24 02:22:38 -08003399 * @tx_ring: Tx descriptor ring for a specific queue
3400 *
3401 * Free all transmit software resources
3402 **/
Alexander Duyck68fd9912008-11-20 00:48:10 -08003403void igb_free_tx_resources(struct igb_ring *tx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08003404{
Mitch Williams3b644cf2008-06-27 10:59:48 -07003405 igb_clean_tx_ring(tx_ring);
Auke Kok9d5c8242008-01-24 02:22:38 -08003406
Alexander Duyck06034642011-08-26 07:44:22 +00003407 vfree(tx_ring->tx_buffer_info);
3408 tx_ring->tx_buffer_info = NULL;
Auke Kok9d5c8242008-01-24 02:22:38 -08003409
Alexander Duyck439705e2009-10-27 23:49:20 +00003410 /* if not set, then don't free */
3411 if (!tx_ring->desc)
3412 return;
3413
Alexander Duyck59d71982010-04-27 13:09:25 +00003414 dma_free_coherent(tx_ring->dev, tx_ring->size,
3415 tx_ring->desc, tx_ring->dma);
Auke Kok9d5c8242008-01-24 02:22:38 -08003416
3417 tx_ring->desc = NULL;
3418}
3419
3420/**
3421 * igb_free_all_tx_resources - Free Tx Resources for All Queues
3422 * @adapter: board private structure
3423 *
3424 * Free all transmit software resources
3425 **/
3426static void igb_free_all_tx_resources(struct igb_adapter *adapter)
3427{
3428 int i;
3429
3430 for (i = 0; i < adapter->num_tx_queues; i++)
Alexander Duyck3025a442010-02-17 01:02:39 +00003431 igb_free_tx_resources(adapter->tx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08003432}
3433
Alexander Duyckebe42d12011-08-26 07:45:09 +00003434void igb_unmap_and_free_tx_resource(struct igb_ring *ring,
3435 struct igb_tx_buffer *tx_buffer)
Auke Kok9d5c8242008-01-24 02:22:38 -08003436{
Alexander Duyckebe42d12011-08-26 07:45:09 +00003437 if (tx_buffer->skb) {
3438 dev_kfree_skb_any(tx_buffer->skb);
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00003439 if (dma_unmap_len(tx_buffer, len))
Alexander Duyckebe42d12011-08-26 07:45:09 +00003440 dma_unmap_single(ring->dev,
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00003441 dma_unmap_addr(tx_buffer, dma),
3442 dma_unmap_len(tx_buffer, len),
Alexander Duyckebe42d12011-08-26 07:45:09 +00003443 DMA_TO_DEVICE);
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00003444 } else if (dma_unmap_len(tx_buffer, len)) {
Alexander Duyckebe42d12011-08-26 07:45:09 +00003445 dma_unmap_page(ring->dev,
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00003446 dma_unmap_addr(tx_buffer, dma),
3447 dma_unmap_len(tx_buffer, len),
Alexander Duyckebe42d12011-08-26 07:45:09 +00003448 DMA_TO_DEVICE);
Alexander Duyck6366ad32009-12-02 16:47:18 +00003449 }
Alexander Duyckebe42d12011-08-26 07:45:09 +00003450 tx_buffer->next_to_watch = NULL;
3451 tx_buffer->skb = NULL;
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00003452 dma_unmap_len_set(tx_buffer, len, 0);
Alexander Duyckebe42d12011-08-26 07:45:09 +00003453 /* buffer_info must be completely set up in the transmit path */
Auke Kok9d5c8242008-01-24 02:22:38 -08003454}
3455
3456/**
3457 * igb_clean_tx_ring - Free Tx Buffers
Auke Kok9d5c8242008-01-24 02:22:38 -08003458 * @tx_ring: ring to be cleaned
3459 **/
Mitch Williams3b644cf2008-06-27 10:59:48 -07003460static void igb_clean_tx_ring(struct igb_ring *tx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08003461{
Alexander Duyck06034642011-08-26 07:44:22 +00003462 struct igb_tx_buffer *buffer_info;
Auke Kok9d5c8242008-01-24 02:22:38 -08003463 unsigned long size;
Alexander Duyck6ad4edf2011-08-26 07:45:26 +00003464 u16 i;
Auke Kok9d5c8242008-01-24 02:22:38 -08003465
Alexander Duyck06034642011-08-26 07:44:22 +00003466 if (!tx_ring->tx_buffer_info)
Auke Kok9d5c8242008-01-24 02:22:38 -08003467 return;
3468 /* Free all the Tx ring sk_buffs */
3469
3470 for (i = 0; i < tx_ring->count; i++) {
Alexander Duyck06034642011-08-26 07:44:22 +00003471 buffer_info = &tx_ring->tx_buffer_info[i];
Alexander Duyck80785292009-10-27 15:51:47 +00003472 igb_unmap_and_free_tx_resource(tx_ring, buffer_info);
Auke Kok9d5c8242008-01-24 02:22:38 -08003473 }
3474
John Fastabenddad8a3b2012-04-23 12:22:39 +00003475 netdev_tx_reset_queue(txring_txq(tx_ring));
3476
Alexander Duyck06034642011-08-26 07:44:22 +00003477 size = sizeof(struct igb_tx_buffer) * tx_ring->count;
3478 memset(tx_ring->tx_buffer_info, 0, size);
Auke Kok9d5c8242008-01-24 02:22:38 -08003479
3480 /* Zero out the descriptor ring */
Auke Kok9d5c8242008-01-24 02:22:38 -08003481 memset(tx_ring->desc, 0, tx_ring->size);
3482
3483 tx_ring->next_to_use = 0;
3484 tx_ring->next_to_clean = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08003485}
3486
3487/**
3488 * igb_clean_all_tx_rings - Free Tx Buffers for all queues
3489 * @adapter: board private structure
3490 **/
3491static void igb_clean_all_tx_rings(struct igb_adapter *adapter)
3492{
3493 int i;
3494
3495 for (i = 0; i < adapter->num_tx_queues; i++)
Alexander Duyck3025a442010-02-17 01:02:39 +00003496 igb_clean_tx_ring(adapter->tx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08003497}
3498
3499/**
3500 * igb_free_rx_resources - Free Rx Resources
Auke Kok9d5c8242008-01-24 02:22:38 -08003501 * @rx_ring: ring to clean the resources from
3502 *
3503 * Free all receive software resources
3504 **/
Alexander Duyck68fd9912008-11-20 00:48:10 -08003505void igb_free_rx_resources(struct igb_ring *rx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08003506{
Mitch Williams3b644cf2008-06-27 10:59:48 -07003507 igb_clean_rx_ring(rx_ring);
Auke Kok9d5c8242008-01-24 02:22:38 -08003508
Alexander Duyck06034642011-08-26 07:44:22 +00003509 vfree(rx_ring->rx_buffer_info);
3510 rx_ring->rx_buffer_info = NULL;
Auke Kok9d5c8242008-01-24 02:22:38 -08003511
Alexander Duyck439705e2009-10-27 23:49:20 +00003512 /* if not set, then don't free */
3513 if (!rx_ring->desc)
3514 return;
3515
Alexander Duyck59d71982010-04-27 13:09:25 +00003516 dma_free_coherent(rx_ring->dev, rx_ring->size,
3517 rx_ring->desc, rx_ring->dma);
Auke Kok9d5c8242008-01-24 02:22:38 -08003518
3519 rx_ring->desc = NULL;
3520}
3521
3522/**
3523 * igb_free_all_rx_resources - Free Rx Resources for All Queues
3524 * @adapter: board private structure
3525 *
3526 * Free all receive software resources
3527 **/
3528static void igb_free_all_rx_resources(struct igb_adapter *adapter)
3529{
3530 int i;
3531
3532 for (i = 0; i < adapter->num_rx_queues; i++)
Alexander Duyck3025a442010-02-17 01:02:39 +00003533 igb_free_rx_resources(adapter->rx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08003534}
3535
3536/**
3537 * igb_clean_rx_ring - Free Rx Buffers per Queue
Auke Kok9d5c8242008-01-24 02:22:38 -08003538 * @rx_ring: ring to free buffers from
3539 **/
Mitch Williams3b644cf2008-06-27 10:59:48 -07003540static void igb_clean_rx_ring(struct igb_ring *rx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08003541{
Auke Kok9d5c8242008-01-24 02:22:38 -08003542 unsigned long size;
Alexander Duyckc023cd82011-08-26 07:43:43 +00003543 u16 i;
Auke Kok9d5c8242008-01-24 02:22:38 -08003544
Alexander Duyck1a1c2252012-09-25 00:30:52 +00003545 if (rx_ring->skb)
3546 dev_kfree_skb(rx_ring->skb);
3547 rx_ring->skb = NULL;
3548
Alexander Duyck06034642011-08-26 07:44:22 +00003549 if (!rx_ring->rx_buffer_info)
Auke Kok9d5c8242008-01-24 02:22:38 -08003550 return;
Alexander Duyck439705e2009-10-27 23:49:20 +00003551
Auke Kok9d5c8242008-01-24 02:22:38 -08003552 /* Free all the Rx ring sk_buffs */
3553 for (i = 0; i < rx_ring->count; i++) {
Alexander Duyck06034642011-08-26 07:44:22 +00003554 struct igb_rx_buffer *buffer_info = &rx_ring->rx_buffer_info[i];
Auke Kok9d5c8242008-01-24 02:22:38 -08003555
Alexander Duyckcbc8e552012-09-25 00:31:02 +00003556 if (!buffer_info->page)
3557 continue;
3558
3559 dma_unmap_page(rx_ring->dev,
3560 buffer_info->dma,
3561 PAGE_SIZE,
3562 DMA_FROM_DEVICE);
3563 __free_page(buffer_info->page);
3564
Alexander Duyck1a1c2252012-09-25 00:30:52 +00003565 buffer_info->page = NULL;
Auke Kok9d5c8242008-01-24 02:22:38 -08003566 }
3567
Alexander Duyck06034642011-08-26 07:44:22 +00003568 size = sizeof(struct igb_rx_buffer) * rx_ring->count;
3569 memset(rx_ring->rx_buffer_info, 0, size);
Auke Kok9d5c8242008-01-24 02:22:38 -08003570
3571 /* Zero out the descriptor ring */
3572 memset(rx_ring->desc, 0, rx_ring->size);
3573
Alexander Duyckcbc8e552012-09-25 00:31:02 +00003574 rx_ring->next_to_alloc = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08003575 rx_ring->next_to_clean = 0;
3576 rx_ring->next_to_use = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08003577}
3578
3579/**
3580 * igb_clean_all_rx_rings - Free Rx Buffers for all queues
3581 * @adapter: board private structure
3582 **/
3583static void igb_clean_all_rx_rings(struct igb_adapter *adapter)
3584{
3585 int i;
3586
3587 for (i = 0; i < adapter->num_rx_queues; i++)
Alexander Duyck3025a442010-02-17 01:02:39 +00003588 igb_clean_rx_ring(adapter->rx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08003589}
3590
3591/**
3592 * igb_set_mac - Change the Ethernet Address of the NIC
3593 * @netdev: network interface device structure
3594 * @p: pointer to an address structure
3595 *
3596 * Returns 0 on success, negative on failure
3597 **/
3598static int igb_set_mac(struct net_device *netdev, void *p)
3599{
3600 struct igb_adapter *adapter = netdev_priv(netdev);
Alexander Duyck28b07592009-02-06 23:20:31 +00003601 struct e1000_hw *hw = &adapter->hw;
Auke Kok9d5c8242008-01-24 02:22:38 -08003602 struct sockaddr *addr = p;
3603
3604 if (!is_valid_ether_addr(addr->sa_data))
3605 return -EADDRNOTAVAIL;
3606
3607 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
Alexander Duyck28b07592009-02-06 23:20:31 +00003608 memcpy(hw->mac.addr, addr->sa_data, netdev->addr_len);
Auke Kok9d5c8242008-01-24 02:22:38 -08003609
Alexander Duyck26ad9172009-10-05 06:32:49 +00003610 /* set the correct pool for the new PF MAC address in entry 0 */
3611 igb_rar_set_qsel(adapter, hw->mac.addr, 0,
3612 adapter->vfs_allocated_count);
Alexander Duycke1739522009-02-19 20:39:44 -08003613
Auke Kok9d5c8242008-01-24 02:22:38 -08003614 return 0;
3615}
3616
3617/**
Alexander Duyck68d480c2009-10-05 06:33:08 +00003618 * igb_write_mc_addr_list - write multicast addresses to MTA
3619 * @netdev: network interface device structure
3620 *
3621 * Writes multicast address list to the MTA hash table.
3622 * Returns: -ENOMEM on failure
3623 * 0 on no addresses written
3624 * X on writing X addresses to MTA
3625 **/
3626static int igb_write_mc_addr_list(struct net_device *netdev)
3627{
3628 struct igb_adapter *adapter = netdev_priv(netdev);
3629 struct e1000_hw *hw = &adapter->hw;
Jiri Pirko22bedad32010-04-01 21:22:57 +00003630 struct netdev_hw_addr *ha;
Alexander Duyck68d480c2009-10-05 06:33:08 +00003631 u8 *mta_list;
Alexander Duyck68d480c2009-10-05 06:33:08 +00003632 int i;
3633
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00003634 if (netdev_mc_empty(netdev)) {
Alexander Duyck68d480c2009-10-05 06:33:08 +00003635 /* nothing to program, so clear mc list */
3636 igb_update_mc_addr_list(hw, NULL, 0);
3637 igb_restore_vf_multicasts(adapter);
3638 return 0;
3639 }
3640
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00003641 mta_list = kzalloc(netdev_mc_count(netdev) * 6, GFP_ATOMIC);
Alexander Duyck68d480c2009-10-05 06:33:08 +00003642 if (!mta_list)
3643 return -ENOMEM;
3644
Alexander Duyck68d480c2009-10-05 06:33:08 +00003645 /* The shared function expects a packed array of only addresses. */
Jiri Pirko48e2f182010-02-22 09:22:26 +00003646 i = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +00003647 netdev_for_each_mc_addr(ha, netdev)
3648 memcpy(mta_list + (i++ * ETH_ALEN), ha->addr, ETH_ALEN);
Alexander Duyck68d480c2009-10-05 06:33:08 +00003649
Alexander Duyck68d480c2009-10-05 06:33:08 +00003650 igb_update_mc_addr_list(hw, mta_list, i);
3651 kfree(mta_list);
3652
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00003653 return netdev_mc_count(netdev);
Alexander Duyck68d480c2009-10-05 06:33:08 +00003654}
3655
3656/**
3657 * igb_write_uc_addr_list - write unicast addresses to RAR table
3658 * @netdev: network interface device structure
3659 *
3660 * Writes unicast address list to the RAR table.
3661 * Returns: -ENOMEM on failure/insufficient address space
3662 * 0 on no addresses written
3663 * X on writing X addresses to the RAR table
3664 **/
3665static int igb_write_uc_addr_list(struct net_device *netdev)
3666{
3667 struct igb_adapter *adapter = netdev_priv(netdev);
3668 struct e1000_hw *hw = &adapter->hw;
3669 unsigned int vfn = adapter->vfs_allocated_count;
3670 unsigned int rar_entries = hw->mac.rar_entry_count - (vfn + 1);
3671 int count = 0;
3672
3673 /* return ENOMEM indicating insufficient memory for addresses */
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08003674 if (netdev_uc_count(netdev) > rar_entries)
Alexander Duyck68d480c2009-10-05 06:33:08 +00003675 return -ENOMEM;
3676
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08003677 if (!netdev_uc_empty(netdev) && rar_entries) {
Alexander Duyck68d480c2009-10-05 06:33:08 +00003678 struct netdev_hw_addr *ha;
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08003679
3680 netdev_for_each_uc_addr(ha, netdev) {
Alexander Duyck68d480c2009-10-05 06:33:08 +00003681 if (!rar_entries)
3682 break;
3683 igb_rar_set_qsel(adapter, ha->addr,
3684 rar_entries--,
3685 vfn);
3686 count++;
3687 }
3688 }
3689 /* write the addresses in reverse order to avoid write combining */
3690 for (; rar_entries > 0 ; rar_entries--) {
3691 wr32(E1000_RAH(rar_entries), 0);
3692 wr32(E1000_RAL(rar_entries), 0);
3693 }
3694 wrfl();
3695
3696 return count;
3697}
3698
3699/**
Alexander Duyckff41f8d2009-09-03 14:48:56 +00003700 * igb_set_rx_mode - Secondary Unicast, Multicast and Promiscuous mode set
Auke Kok9d5c8242008-01-24 02:22:38 -08003701 * @netdev: network interface device structure
3702 *
Alexander Duyckff41f8d2009-09-03 14:48:56 +00003703 * The set_rx_mode entry point is called whenever the unicast or multicast
3704 * address lists or the network interface flags are updated. This routine is
3705 * responsible for configuring the hardware for proper unicast, multicast,
Auke Kok9d5c8242008-01-24 02:22:38 -08003706 * promiscuous mode, and all-multi behavior.
3707 **/
Alexander Duyckff41f8d2009-09-03 14:48:56 +00003708static void igb_set_rx_mode(struct net_device *netdev)
Auke Kok9d5c8242008-01-24 02:22:38 -08003709{
3710 struct igb_adapter *adapter = netdev_priv(netdev);
3711 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck68d480c2009-10-05 06:33:08 +00003712 unsigned int vfn = adapter->vfs_allocated_count;
3713 u32 rctl, vmolr = 0;
3714 int count;
Auke Kok9d5c8242008-01-24 02:22:38 -08003715
3716 /* Check for Promiscuous and All Multicast modes */
Auke Kok9d5c8242008-01-24 02:22:38 -08003717 rctl = rd32(E1000_RCTL);
3718
Alexander Duyck68d480c2009-10-05 06:33:08 +00003719 /* clear the effected bits */
3720 rctl &= ~(E1000_RCTL_UPE | E1000_RCTL_MPE | E1000_RCTL_VFE);
3721
Patrick McHardy746b9f02008-07-16 20:15:45 -07003722 if (netdev->flags & IFF_PROMISC) {
Auke Kok9d5c8242008-01-24 02:22:38 -08003723 rctl |= (E1000_RCTL_UPE | E1000_RCTL_MPE);
Alexander Duyck68d480c2009-10-05 06:33:08 +00003724 vmolr |= (E1000_VMOLR_ROPE | E1000_VMOLR_MPME);
Patrick McHardy746b9f02008-07-16 20:15:45 -07003725 } else {
Alexander Duyck68d480c2009-10-05 06:33:08 +00003726 if (netdev->flags & IFF_ALLMULTI) {
Patrick McHardy746b9f02008-07-16 20:15:45 -07003727 rctl |= E1000_RCTL_MPE;
Alexander Duyck68d480c2009-10-05 06:33:08 +00003728 vmolr |= E1000_VMOLR_MPME;
3729 } else {
3730 /*
3731 * Write addresses to the MTA, if the attempt fails
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003732 * then we should just turn on promiscuous mode so
Alexander Duyck68d480c2009-10-05 06:33:08 +00003733 * that we can at least receive multicast traffic
3734 */
3735 count = igb_write_mc_addr_list(netdev);
3736 if (count < 0) {
3737 rctl |= E1000_RCTL_MPE;
3738 vmolr |= E1000_VMOLR_MPME;
3739 } else if (count) {
3740 vmolr |= E1000_VMOLR_ROMPE;
3741 }
3742 }
3743 /*
3744 * Write addresses to available RAR registers, if there is not
3745 * sufficient space to store all the addresses then enable
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003746 * unicast promiscuous mode
Alexander Duyck68d480c2009-10-05 06:33:08 +00003747 */
3748 count = igb_write_uc_addr_list(netdev);
3749 if (count < 0) {
Alexander Duyckff41f8d2009-09-03 14:48:56 +00003750 rctl |= E1000_RCTL_UPE;
Alexander Duyck68d480c2009-10-05 06:33:08 +00003751 vmolr |= E1000_VMOLR_ROPE;
3752 }
Patrick McHardy78ed11a2008-07-16 20:16:14 -07003753 rctl |= E1000_RCTL_VFE;
Patrick McHardy746b9f02008-07-16 20:15:45 -07003754 }
Auke Kok9d5c8242008-01-24 02:22:38 -08003755 wr32(E1000_RCTL, rctl);
3756
Alexander Duyck68d480c2009-10-05 06:33:08 +00003757 /*
3758 * In order to support SR-IOV and eventually VMDq it is necessary to set
3759 * the VMOLR to enable the appropriate modes. Without this workaround
3760 * we will have issues with VLAN tag stripping not being done for frames
3761 * that are only arriving because we are the default pool
3762 */
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00003763 if ((hw->mac.type < e1000_82576) || (hw->mac.type > e1000_i350))
Alexander Duyck28fc06f2009-07-23 18:08:54 +00003764 return;
Alexander Duyck28fc06f2009-07-23 18:08:54 +00003765
Alexander Duyck68d480c2009-10-05 06:33:08 +00003766 vmolr |= rd32(E1000_VMOLR(vfn)) &
3767 ~(E1000_VMOLR_ROPE | E1000_VMOLR_MPME | E1000_VMOLR_ROMPE);
3768 wr32(E1000_VMOLR(vfn), vmolr);
Alexander Duyck28fc06f2009-07-23 18:08:54 +00003769 igb_restore_vf_multicasts(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08003770}
3771
Greg Rose13800462010-11-06 02:08:26 +00003772static void igb_check_wvbr(struct igb_adapter *adapter)
3773{
3774 struct e1000_hw *hw = &adapter->hw;
3775 u32 wvbr = 0;
3776
3777 switch (hw->mac.type) {
3778 case e1000_82576:
3779 case e1000_i350:
3780 if (!(wvbr = rd32(E1000_WVBR)))
3781 return;
3782 break;
3783 default:
3784 break;
3785 }
3786
3787 adapter->wvbr |= wvbr;
3788}
3789
3790#define IGB_STAGGERED_QUEUE_OFFSET 8
3791
3792static void igb_spoof_check(struct igb_adapter *adapter)
3793{
3794 int j;
3795
3796 if (!adapter->wvbr)
3797 return;
3798
3799 for(j = 0; j < adapter->vfs_allocated_count; j++) {
3800 if (adapter->wvbr & (1 << j) ||
3801 adapter->wvbr & (1 << (j + IGB_STAGGERED_QUEUE_OFFSET))) {
3802 dev_warn(&adapter->pdev->dev,
3803 "Spoof event(s) detected on VF %d\n", j);
3804 adapter->wvbr &=
3805 ~((1 << j) |
3806 (1 << (j + IGB_STAGGERED_QUEUE_OFFSET)));
3807 }
3808 }
3809}
3810
Auke Kok9d5c8242008-01-24 02:22:38 -08003811/* Need to wait a few seconds after link up to get diagnostic information from
3812 * the phy */
3813static void igb_update_phy_info(unsigned long data)
3814{
3815 struct igb_adapter *adapter = (struct igb_adapter *) data;
Alexander Duyckf5f4cf02008-11-21 21:30:24 -08003816 igb_get_phy_info(&adapter->hw);
Auke Kok9d5c8242008-01-24 02:22:38 -08003817}
3818
3819/**
Alexander Duyck4d6b7252009-02-06 23:16:24 +00003820 * igb_has_link - check shared code for link and determine up/down
3821 * @adapter: pointer to driver private info
3822 **/
Nick Nunley31455352010-02-17 01:01:21 +00003823bool igb_has_link(struct igb_adapter *adapter)
Alexander Duyck4d6b7252009-02-06 23:16:24 +00003824{
3825 struct e1000_hw *hw = &adapter->hw;
3826 bool link_active = false;
3827 s32 ret_val = 0;
3828
3829 /* get_link_status is set on LSC (link status) interrupt or
3830 * rx sequence error interrupt. get_link_status will stay
3831 * false until the e1000_check_for_link establishes link
3832 * for copper adapters ONLY
3833 */
3834 switch (hw->phy.media_type) {
3835 case e1000_media_type_copper:
3836 if (hw->mac.get_link_status) {
3837 ret_val = hw->mac.ops.check_for_link(hw);
3838 link_active = !hw->mac.get_link_status;
3839 } else {
3840 link_active = true;
3841 }
3842 break;
Alexander Duyck4d6b7252009-02-06 23:16:24 +00003843 case e1000_media_type_internal_serdes:
3844 ret_val = hw->mac.ops.check_for_link(hw);
3845 link_active = hw->mac.serdes_has_link;
3846 break;
3847 default:
3848 case e1000_media_type_unknown:
3849 break;
3850 }
3851
3852 return link_active;
3853}
3854
Stefan Assmann563988d2011-04-05 04:27:15 +00003855static bool igb_thermal_sensor_event(struct e1000_hw *hw, u32 event)
3856{
3857 bool ret = false;
3858 u32 ctrl_ext, thstat;
3859
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00003860 /* check for thermal sensor event on i350 copper only */
Stefan Assmann563988d2011-04-05 04:27:15 +00003861 if (hw->mac.type == e1000_i350) {
3862 thstat = rd32(E1000_THSTAT);
3863 ctrl_ext = rd32(E1000_CTRL_EXT);
3864
3865 if ((hw->phy.media_type == e1000_media_type_copper) &&
3866 !(ctrl_ext & E1000_CTRL_EXT_LINK_MODE_SGMII)) {
3867 ret = !!(thstat & event);
3868 }
3869 }
3870
3871 return ret;
3872}
3873
Alexander Duyck4d6b7252009-02-06 23:16:24 +00003874/**
Auke Kok9d5c8242008-01-24 02:22:38 -08003875 * igb_watchdog - Timer Call-back
3876 * @data: pointer to adapter cast into an unsigned long
3877 **/
3878static void igb_watchdog(unsigned long data)
3879{
3880 struct igb_adapter *adapter = (struct igb_adapter *)data;
3881 /* Do the rest outside of interrupt context */
3882 schedule_work(&adapter->watchdog_task);
3883}
3884
3885static void igb_watchdog_task(struct work_struct *work)
3886{
3887 struct igb_adapter *adapter = container_of(work,
Alexander Duyck559e9c42009-10-27 23:52:50 +00003888 struct igb_adapter,
3889 watchdog_task);
Auke Kok9d5c8242008-01-24 02:22:38 -08003890 struct e1000_hw *hw = &adapter->hw;
Auke Kok9d5c8242008-01-24 02:22:38 -08003891 struct net_device *netdev = adapter->netdev;
Stefan Assmann563988d2011-04-05 04:27:15 +00003892 u32 link;
Alexander Duyck7a6ea552008-08-26 04:25:03 -07003893 int i;
Auke Kok9d5c8242008-01-24 02:22:38 -08003894
Alexander Duyck4d6b7252009-02-06 23:16:24 +00003895 link = igb_has_link(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08003896 if (link) {
Yan, Zheng749ab2c2012-01-04 20:23:37 +00003897 /* Cancel scheduled suspend requests. */
3898 pm_runtime_resume(netdev->dev.parent);
3899
Auke Kok9d5c8242008-01-24 02:22:38 -08003900 if (!netif_carrier_ok(netdev)) {
3901 u32 ctrl;
Alexander Duyck330a6d62009-10-27 23:51:35 +00003902 hw->mac.ops.get_speed_and_duplex(hw,
3903 &adapter->link_speed,
3904 &adapter->link_duplex);
Auke Kok9d5c8242008-01-24 02:22:38 -08003905
3906 ctrl = rd32(E1000_CTRL);
Alexander Duyck527d47c2008-11-27 00:21:39 -08003907 /* Links status message must follow this format */
Jeff Kirsher876d2d62011-10-21 20:01:34 +00003908 printk(KERN_INFO "igb: %s NIC Link is Up %d Mbps %s "
3909 "Duplex, Flow Control: %s\n",
Alexander Duyck559e9c42009-10-27 23:52:50 +00003910 netdev->name,
3911 adapter->link_speed,
3912 adapter->link_duplex == FULL_DUPLEX ?
Jeff Kirsher876d2d62011-10-21 20:01:34 +00003913 "Full" : "Half",
3914 (ctrl & E1000_CTRL_TFCE) &&
3915 (ctrl & E1000_CTRL_RFCE) ? "RX/TX" :
3916 (ctrl & E1000_CTRL_RFCE) ? "RX" :
3917 (ctrl & E1000_CTRL_TFCE) ? "TX" : "None");
Auke Kok9d5c8242008-01-24 02:22:38 -08003918
Stefan Assmann563988d2011-04-05 04:27:15 +00003919 /* check for thermal sensor event */
Jeff Kirsher876d2d62011-10-21 20:01:34 +00003920 if (igb_thermal_sensor_event(hw,
3921 E1000_THSTAT_LINK_THROTTLE)) {
3922 netdev_info(netdev, "The network adapter link "
3923 "speed was downshifted because it "
3924 "overheated\n");
Carolyn Wyborny7ef5ed12011-03-12 08:59:47 +00003925 }
Stefan Assmann563988d2011-04-05 04:27:15 +00003926
Emil Tantilovd07f3e32010-03-23 18:34:57 +00003927 /* adjust timeout factor according to speed/duplex */
Auke Kok9d5c8242008-01-24 02:22:38 -08003928 adapter->tx_timeout_factor = 1;
3929 switch (adapter->link_speed) {
3930 case SPEED_10:
Auke Kok9d5c8242008-01-24 02:22:38 -08003931 adapter->tx_timeout_factor = 14;
3932 break;
3933 case SPEED_100:
Auke Kok9d5c8242008-01-24 02:22:38 -08003934 /* maybe add some timeout factor ? */
3935 break;
3936 }
3937
3938 netif_carrier_on(netdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08003939
Alexander Duyck4ae196d2009-02-19 20:40:07 -08003940 igb_ping_all_vfs(adapter);
Lior Levy17dc5662011-02-08 02:28:46 +00003941 igb_check_vf_rate_limit(adapter);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08003942
Alexander Duyck4b1a9872009-02-06 23:19:50 +00003943 /* link state has changed, schedule phy info update */
Auke Kok9d5c8242008-01-24 02:22:38 -08003944 if (!test_bit(__IGB_DOWN, &adapter->state))
3945 mod_timer(&adapter->phy_info_timer,
3946 round_jiffies(jiffies + 2 * HZ));
3947 }
3948 } else {
3949 if (netif_carrier_ok(netdev)) {
3950 adapter->link_speed = 0;
3951 adapter->link_duplex = 0;
Stefan Assmann563988d2011-04-05 04:27:15 +00003952
3953 /* check for thermal sensor event */
Jeff Kirsher876d2d62011-10-21 20:01:34 +00003954 if (igb_thermal_sensor_event(hw,
3955 E1000_THSTAT_PWR_DOWN)) {
3956 netdev_err(netdev, "The network adapter was "
3957 "stopped because it overheated\n");
Carolyn Wyborny7ef5ed12011-03-12 08:59:47 +00003958 }
Stefan Assmann563988d2011-04-05 04:27:15 +00003959
Alexander Duyck527d47c2008-11-27 00:21:39 -08003960 /* Links status message must follow this format */
3961 printk(KERN_INFO "igb: %s NIC Link is Down\n",
3962 netdev->name);
Auke Kok9d5c8242008-01-24 02:22:38 -08003963 netif_carrier_off(netdev);
Alexander Duyck4b1a9872009-02-06 23:19:50 +00003964
Alexander Duyck4ae196d2009-02-19 20:40:07 -08003965 igb_ping_all_vfs(adapter);
3966
Alexander Duyck4b1a9872009-02-06 23:19:50 +00003967 /* link state has changed, schedule phy info update */
Auke Kok9d5c8242008-01-24 02:22:38 -08003968 if (!test_bit(__IGB_DOWN, &adapter->state))
3969 mod_timer(&adapter->phy_info_timer,
3970 round_jiffies(jiffies + 2 * HZ));
Yan, Zheng749ab2c2012-01-04 20:23:37 +00003971
3972 pm_schedule_suspend(netdev->dev.parent,
3973 MSEC_PER_SEC * 5);
Auke Kok9d5c8242008-01-24 02:22:38 -08003974 }
3975 }
3976
Eric Dumazet12dcd862010-10-15 17:27:10 +00003977 spin_lock(&adapter->stats64_lock);
3978 igb_update_stats(adapter, &adapter->stats64);
3979 spin_unlock(&adapter->stats64_lock);
Auke Kok9d5c8242008-01-24 02:22:38 -08003980
Alexander Duyckdbabb062009-11-12 18:38:16 +00003981 for (i = 0; i < adapter->num_tx_queues; i++) {
Alexander Duyck3025a442010-02-17 01:02:39 +00003982 struct igb_ring *tx_ring = adapter->tx_ring[i];
Alexander Duyckdbabb062009-11-12 18:38:16 +00003983 if (!netif_carrier_ok(netdev)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08003984 /* We've lost link, so the controller stops DMA,
3985 * but we've got queued Tx work that's never going
3986 * to get done, so reset controller to flush Tx.
3987 * (Do the reset outside of interrupt context). */
Alexander Duyckdbabb062009-11-12 18:38:16 +00003988 if (igb_desc_unused(tx_ring) + 1 < tx_ring->count) {
3989 adapter->tx_timeout_count++;
3990 schedule_work(&adapter->reset_task);
3991 /* return immediately since reset is imminent */
3992 return;
3993 }
Auke Kok9d5c8242008-01-24 02:22:38 -08003994 }
Auke Kok9d5c8242008-01-24 02:22:38 -08003995
Alexander Duyckdbabb062009-11-12 18:38:16 +00003996 /* Force detection of hung controller every watchdog period */
Alexander Duyck6d095fa2011-08-26 07:46:19 +00003997 set_bit(IGB_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags);
Alexander Duyckdbabb062009-11-12 18:38:16 +00003998 }
Alexander Duyckf7ba2052009-10-27 23:48:51 +00003999
Auke Kok9d5c8242008-01-24 02:22:38 -08004000 /* Cause software interrupt to ensure rx ring is cleaned */
Alexander Duyck7a6ea552008-08-26 04:25:03 -07004001 if (adapter->msix_entries) {
Alexander Duyck047e0032009-10-27 15:49:27 +00004002 u32 eics = 0;
Alexander Duyck0d1ae7f2011-08-26 07:46:34 +00004003 for (i = 0; i < adapter->num_q_vectors; i++)
4004 eics |= adapter->q_vector[i]->eims_value;
Alexander Duyck7a6ea552008-08-26 04:25:03 -07004005 wr32(E1000_EICS, eics);
4006 } else {
4007 wr32(E1000_ICS, E1000_ICS_RXDMT0);
4008 }
Auke Kok9d5c8242008-01-24 02:22:38 -08004009
Greg Rose13800462010-11-06 02:08:26 +00004010 igb_spoof_check(adapter);
Matthew Vickfc580752012-12-13 07:20:35 +00004011 igb_ptp_rx_hang(adapter);
Greg Rose13800462010-11-06 02:08:26 +00004012
Auke Kok9d5c8242008-01-24 02:22:38 -08004013 /* Reset the timer */
4014 if (!test_bit(__IGB_DOWN, &adapter->state))
4015 mod_timer(&adapter->watchdog_timer,
4016 round_jiffies(jiffies + 2 * HZ));
4017}
4018
4019enum latency_range {
4020 lowest_latency = 0,
4021 low_latency = 1,
4022 bulk_latency = 2,
4023 latency_invalid = 255
4024};
4025
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07004026/**
4027 * igb_update_ring_itr - update the dynamic ITR value based on packet size
4028 *
4029 * Stores a new ITR value based on strictly on packet size. This
4030 * algorithm is less sophisticated than that used in igb_update_itr,
4031 * due to the difficulty of synchronizing statistics across multiple
Stefan Weileef35c22010-08-06 21:11:15 +02004032 * receive rings. The divisors and thresholds used by this function
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07004033 * were determined based on theoretical maximum wire speed and testing
4034 * data, in order to minimize response time while increasing bulk
4035 * throughput.
4036 * This functionality is controlled by the InterruptThrottleRate module
4037 * parameter (see igb_param.c)
4038 * NOTE: This function is called only when operating in a multiqueue
4039 * receive environment.
Alexander Duyck047e0032009-10-27 15:49:27 +00004040 * @q_vector: pointer to q_vector
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07004041 **/
Alexander Duyck047e0032009-10-27 15:49:27 +00004042static void igb_update_ring_itr(struct igb_q_vector *q_vector)
Auke Kok9d5c8242008-01-24 02:22:38 -08004043{
Alexander Duyck047e0032009-10-27 15:49:27 +00004044 int new_val = q_vector->itr_val;
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07004045 int avg_wire_size = 0;
Alexander Duyck047e0032009-10-27 15:49:27 +00004046 struct igb_adapter *adapter = q_vector->adapter;
Eric Dumazet12dcd862010-10-15 17:27:10 +00004047 unsigned int packets;
Auke Kok9d5c8242008-01-24 02:22:38 -08004048
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07004049 /* For non-gigabit speeds, just fix the interrupt rate at 4000
4050 * ints/sec - ITR timer value of 120 ticks.
4051 */
4052 if (adapter->link_speed != SPEED_1000) {
Alexander Duyck0ba82992011-08-26 07:45:47 +00004053 new_val = IGB_4K_ITR;
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07004054 goto set_itr_val;
4055 }
Alexander Duyck047e0032009-10-27 15:49:27 +00004056
Alexander Duyck0ba82992011-08-26 07:45:47 +00004057 packets = q_vector->rx.total_packets;
4058 if (packets)
4059 avg_wire_size = q_vector->rx.total_bytes / packets;
Eric Dumazet12dcd862010-10-15 17:27:10 +00004060
Alexander Duyck0ba82992011-08-26 07:45:47 +00004061 packets = q_vector->tx.total_packets;
4062 if (packets)
4063 avg_wire_size = max_t(u32, avg_wire_size,
4064 q_vector->tx.total_bytes / packets);
Alexander Duyck047e0032009-10-27 15:49:27 +00004065
4066 /* if avg_wire_size isn't set no work was done */
4067 if (!avg_wire_size)
4068 goto clear_counts;
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07004069
4070 /* Add 24 bytes to size to account for CRC, preamble, and gap */
4071 avg_wire_size += 24;
4072
4073 /* Don't starve jumbo frames */
4074 avg_wire_size = min(avg_wire_size, 3000);
4075
4076 /* Give a little boost to mid-size frames */
4077 if ((avg_wire_size > 300) && (avg_wire_size < 1200))
4078 new_val = avg_wire_size / 3;
4079 else
4080 new_val = avg_wire_size / 2;
4081
Alexander Duyck0ba82992011-08-26 07:45:47 +00004082 /* conservative mode (itr 3) eliminates the lowest_latency setting */
4083 if (new_val < IGB_20K_ITR &&
4084 ((q_vector->rx.ring && adapter->rx_itr_setting == 3) ||
4085 (!q_vector->rx.ring && adapter->tx_itr_setting == 3)))
4086 new_val = IGB_20K_ITR;
Nick Nunleyabe1c362010-02-17 01:03:19 +00004087
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07004088set_itr_val:
Alexander Duyck047e0032009-10-27 15:49:27 +00004089 if (new_val != q_vector->itr_val) {
4090 q_vector->itr_val = new_val;
4091 q_vector->set_itr = 1;
Auke Kok9d5c8242008-01-24 02:22:38 -08004092 }
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07004093clear_counts:
Alexander Duyck0ba82992011-08-26 07:45:47 +00004094 q_vector->rx.total_bytes = 0;
4095 q_vector->rx.total_packets = 0;
4096 q_vector->tx.total_bytes = 0;
4097 q_vector->tx.total_packets = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08004098}
4099
4100/**
4101 * igb_update_itr - update the dynamic ITR value based on statistics
4102 * Stores a new ITR value based on packets and byte
4103 * counts during the last interrupt. The advantage of per interrupt
4104 * computation is faster updates and more accurate ITR for the current
4105 * traffic pattern. Constants in this function were computed
4106 * based on theoretical maximum wire speed and thresholds were set based
4107 * on testing data as well as attempting to minimize response time
4108 * while increasing bulk throughput.
4109 * this functionality is controlled by the InterruptThrottleRate module
4110 * parameter (see igb_param.c)
4111 * NOTE: These calculations are only valid when operating in a single-
4112 * queue environment.
Alexander Duyck0ba82992011-08-26 07:45:47 +00004113 * @q_vector: pointer to q_vector
4114 * @ring_container: ring info to update the itr for
Auke Kok9d5c8242008-01-24 02:22:38 -08004115 **/
Alexander Duyck0ba82992011-08-26 07:45:47 +00004116static void igb_update_itr(struct igb_q_vector *q_vector,
4117 struct igb_ring_container *ring_container)
Auke Kok9d5c8242008-01-24 02:22:38 -08004118{
Alexander Duyck0ba82992011-08-26 07:45:47 +00004119 unsigned int packets = ring_container->total_packets;
4120 unsigned int bytes = ring_container->total_bytes;
4121 u8 itrval = ring_container->itr;
Auke Kok9d5c8242008-01-24 02:22:38 -08004122
Alexander Duyck0ba82992011-08-26 07:45:47 +00004123 /* no packets, exit with status unchanged */
Auke Kok9d5c8242008-01-24 02:22:38 -08004124 if (packets == 0)
Alexander Duyck0ba82992011-08-26 07:45:47 +00004125 return;
Auke Kok9d5c8242008-01-24 02:22:38 -08004126
Alexander Duyck0ba82992011-08-26 07:45:47 +00004127 switch (itrval) {
Auke Kok9d5c8242008-01-24 02:22:38 -08004128 case lowest_latency:
4129 /* handle TSO and jumbo frames */
4130 if (bytes/packets > 8000)
Alexander Duyck0ba82992011-08-26 07:45:47 +00004131 itrval = bulk_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08004132 else if ((packets < 5) && (bytes > 512))
Alexander Duyck0ba82992011-08-26 07:45:47 +00004133 itrval = low_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08004134 break;
4135 case low_latency: /* 50 usec aka 20000 ints/s */
4136 if (bytes > 10000) {
4137 /* this if handles the TSO accounting */
4138 if (bytes/packets > 8000) {
Alexander Duyck0ba82992011-08-26 07:45:47 +00004139 itrval = bulk_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08004140 } else if ((packets < 10) || ((bytes/packets) > 1200)) {
Alexander Duyck0ba82992011-08-26 07:45:47 +00004141 itrval = bulk_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08004142 } else if ((packets > 35)) {
Alexander Duyck0ba82992011-08-26 07:45:47 +00004143 itrval = lowest_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08004144 }
4145 } else if (bytes/packets > 2000) {
Alexander Duyck0ba82992011-08-26 07:45:47 +00004146 itrval = bulk_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08004147 } else if (packets <= 2 && bytes < 512) {
Alexander Duyck0ba82992011-08-26 07:45:47 +00004148 itrval = lowest_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08004149 }
4150 break;
4151 case bulk_latency: /* 250 usec aka 4000 ints/s */
4152 if (bytes > 25000) {
4153 if (packets > 35)
Alexander Duyck0ba82992011-08-26 07:45:47 +00004154 itrval = low_latency;
Alexander Duyck1e5c3d22009-02-12 18:17:21 +00004155 } else if (bytes < 1500) {
Alexander Duyck0ba82992011-08-26 07:45:47 +00004156 itrval = low_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08004157 }
4158 break;
4159 }
4160
Alexander Duyck0ba82992011-08-26 07:45:47 +00004161 /* clear work counters since we have the values we need */
4162 ring_container->total_bytes = 0;
4163 ring_container->total_packets = 0;
4164
4165 /* write updated itr to ring container */
4166 ring_container->itr = itrval;
Auke Kok9d5c8242008-01-24 02:22:38 -08004167}
4168
Alexander Duyck0ba82992011-08-26 07:45:47 +00004169static void igb_set_itr(struct igb_q_vector *q_vector)
Auke Kok9d5c8242008-01-24 02:22:38 -08004170{
Alexander Duyck0ba82992011-08-26 07:45:47 +00004171 struct igb_adapter *adapter = q_vector->adapter;
Alexander Duyck047e0032009-10-27 15:49:27 +00004172 u32 new_itr = q_vector->itr_val;
Alexander Duyck0ba82992011-08-26 07:45:47 +00004173 u8 current_itr = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08004174
4175 /* for non-gigabit speeds, just fix the interrupt rate at 4000 */
4176 if (adapter->link_speed != SPEED_1000) {
4177 current_itr = 0;
Alexander Duyck0ba82992011-08-26 07:45:47 +00004178 new_itr = IGB_4K_ITR;
Auke Kok9d5c8242008-01-24 02:22:38 -08004179 goto set_itr_now;
4180 }
4181
Alexander Duyck0ba82992011-08-26 07:45:47 +00004182 igb_update_itr(q_vector, &q_vector->tx);
4183 igb_update_itr(q_vector, &q_vector->rx);
Auke Kok9d5c8242008-01-24 02:22:38 -08004184
Alexander Duyck0ba82992011-08-26 07:45:47 +00004185 current_itr = max(q_vector->rx.itr, q_vector->tx.itr);
Auke Kok9d5c8242008-01-24 02:22:38 -08004186
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07004187 /* conservative mode (itr 3) eliminates the lowest_latency setting */
Alexander Duyck0ba82992011-08-26 07:45:47 +00004188 if (current_itr == lowest_latency &&
4189 ((q_vector->rx.ring && adapter->rx_itr_setting == 3) ||
4190 (!q_vector->rx.ring && adapter->tx_itr_setting == 3)))
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07004191 current_itr = low_latency;
4192
Auke Kok9d5c8242008-01-24 02:22:38 -08004193 switch (current_itr) {
4194 /* counts and packets in update_itr are dependent on these numbers */
4195 case lowest_latency:
Alexander Duyck0ba82992011-08-26 07:45:47 +00004196 new_itr = IGB_70K_ITR; /* 70,000 ints/sec */
Auke Kok9d5c8242008-01-24 02:22:38 -08004197 break;
4198 case low_latency:
Alexander Duyck0ba82992011-08-26 07:45:47 +00004199 new_itr = IGB_20K_ITR; /* 20,000 ints/sec */
Auke Kok9d5c8242008-01-24 02:22:38 -08004200 break;
4201 case bulk_latency:
Alexander Duyck0ba82992011-08-26 07:45:47 +00004202 new_itr = IGB_4K_ITR; /* 4,000 ints/sec */
Auke Kok9d5c8242008-01-24 02:22:38 -08004203 break;
4204 default:
4205 break;
4206 }
4207
4208set_itr_now:
Alexander Duyck047e0032009-10-27 15:49:27 +00004209 if (new_itr != q_vector->itr_val) {
Auke Kok9d5c8242008-01-24 02:22:38 -08004210 /* this attempts to bias the interrupt rate towards Bulk
4211 * by adding intermediate steps when interrupt rate is
4212 * increasing */
Alexander Duyck047e0032009-10-27 15:49:27 +00004213 new_itr = new_itr > q_vector->itr_val ?
4214 max((new_itr * q_vector->itr_val) /
4215 (new_itr + (q_vector->itr_val >> 2)),
Alexander Duyck0ba82992011-08-26 07:45:47 +00004216 new_itr) :
Auke Kok9d5c8242008-01-24 02:22:38 -08004217 new_itr;
4218 /* Don't write the value here; it resets the adapter's
4219 * internal timer, and causes us to delay far longer than
4220 * we should between interrupts. Instead, we write the ITR
4221 * value at the beginning of the next interrupt so the timing
4222 * ends up being correct.
4223 */
Alexander Duyck047e0032009-10-27 15:49:27 +00004224 q_vector->itr_val = new_itr;
4225 q_vector->set_itr = 1;
Auke Kok9d5c8242008-01-24 02:22:38 -08004226 }
Auke Kok9d5c8242008-01-24 02:22:38 -08004227}
4228
Stephen Hemmingerc50b52a2012-01-18 22:13:26 +00004229static void igb_tx_ctxtdesc(struct igb_ring *tx_ring, u32 vlan_macip_lens,
4230 u32 type_tucmd, u32 mss_l4len_idx)
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004231{
4232 struct e1000_adv_tx_context_desc *context_desc;
4233 u16 i = tx_ring->next_to_use;
4234
4235 context_desc = IGB_TX_CTXTDESC(tx_ring, i);
4236
4237 i++;
4238 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
4239
4240 /* set bits to identify this as an advanced context descriptor */
4241 type_tucmd |= E1000_TXD_CMD_DEXT | E1000_ADVTXD_DTYP_CTXT;
4242
4243 /* For 82575, context index must be unique per ring. */
Alexander Duyck866cff02011-08-26 07:45:36 +00004244 if (test_bit(IGB_RING_FLAG_TX_CTX_IDX, &tx_ring->flags))
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004245 mss_l4len_idx |= tx_ring->reg_idx << 4;
4246
4247 context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens);
4248 context_desc->seqnum_seed = 0;
4249 context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd);
4250 context_desc->mss_l4len_idx = cpu_to_le32(mss_l4len_idx);
4251}
4252
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004253static int igb_tso(struct igb_ring *tx_ring,
4254 struct igb_tx_buffer *first,
4255 u8 *hdr_len)
Auke Kok9d5c8242008-01-24 02:22:38 -08004256{
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004257 struct sk_buff *skb = first->skb;
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004258 u32 vlan_macip_lens, type_tucmd;
4259 u32 mss_l4len_idx, l4len;
4260
Alexander Duycked6aa102012-11-13 04:03:22 +00004261 if (skb->ip_summed != CHECKSUM_PARTIAL)
4262 return 0;
4263
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004264 if (!skb_is_gso(skb))
4265 return 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08004266
4267 if (skb_header_cloned(skb)) {
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004268 int err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
Auke Kok9d5c8242008-01-24 02:22:38 -08004269 if (err)
4270 return err;
4271 }
4272
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004273 /* ADV DTYP TUCMD MKRLOC/ISCSIHEDLEN */
4274 type_tucmd = E1000_ADVTXD_TUCMD_L4T_TCP;
Auke Kok9d5c8242008-01-24 02:22:38 -08004275
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004276 if (first->protocol == __constant_htons(ETH_P_IP)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08004277 struct iphdr *iph = ip_hdr(skb);
4278 iph->tot_len = 0;
4279 iph->check = 0;
4280 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
4281 iph->daddr, 0,
4282 IPPROTO_TCP,
4283 0);
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004284 type_tucmd |= E1000_ADVTXD_TUCMD_IPV4;
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004285 first->tx_flags |= IGB_TX_FLAGS_TSO |
4286 IGB_TX_FLAGS_CSUM |
4287 IGB_TX_FLAGS_IPV4;
Sridhar Samudrala8e1e8a42010-01-23 02:02:21 -08004288 } else if (skb_is_gso_v6(skb)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08004289 ipv6_hdr(skb)->payload_len = 0;
4290 tcp_hdr(skb)->check = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
4291 &ipv6_hdr(skb)->daddr,
4292 0, IPPROTO_TCP, 0);
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004293 first->tx_flags |= IGB_TX_FLAGS_TSO |
4294 IGB_TX_FLAGS_CSUM;
Auke Kok9d5c8242008-01-24 02:22:38 -08004295 }
4296
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004297 /* compute header lengths */
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004298 l4len = tcp_hdrlen(skb);
4299 *hdr_len = skb_transport_offset(skb) + l4len;
Auke Kok9d5c8242008-01-24 02:22:38 -08004300
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004301 /* update gso size and bytecount with header size */
4302 first->gso_segs = skb_shinfo(skb)->gso_segs;
4303 first->bytecount += (first->gso_segs - 1) * *hdr_len;
4304
Auke Kok9d5c8242008-01-24 02:22:38 -08004305 /* MSS L4LEN IDX */
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004306 mss_l4len_idx = l4len << E1000_ADVTXD_L4LEN_SHIFT;
4307 mss_l4len_idx |= skb_shinfo(skb)->gso_size << E1000_ADVTXD_MSS_SHIFT;
Auke Kok9d5c8242008-01-24 02:22:38 -08004308
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004309 /* VLAN MACLEN IPLEN */
4310 vlan_macip_lens = skb_network_header_len(skb);
4311 vlan_macip_lens |= skb_network_offset(skb) << E1000_ADVTXD_MACLEN_SHIFT;
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004312 vlan_macip_lens |= first->tx_flags & IGB_TX_FLAGS_VLAN_MASK;
Auke Kok9d5c8242008-01-24 02:22:38 -08004313
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004314 igb_tx_ctxtdesc(tx_ring, vlan_macip_lens, type_tucmd, mss_l4len_idx);
Auke Kok9d5c8242008-01-24 02:22:38 -08004315
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004316 return 1;
Auke Kok9d5c8242008-01-24 02:22:38 -08004317}
4318
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004319static void igb_tx_csum(struct igb_ring *tx_ring, struct igb_tx_buffer *first)
Auke Kok9d5c8242008-01-24 02:22:38 -08004320{
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004321 struct sk_buff *skb = first->skb;
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004322 u32 vlan_macip_lens = 0;
4323 u32 mss_l4len_idx = 0;
4324 u32 type_tucmd = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08004325
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004326 if (skb->ip_summed != CHECKSUM_PARTIAL) {
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004327 if (!(first->tx_flags & IGB_TX_FLAGS_VLAN))
4328 return;
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004329 } else {
4330 u8 l4_hdr = 0;
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004331 switch (first->protocol) {
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004332 case __constant_htons(ETH_P_IP):
4333 vlan_macip_lens |= skb_network_header_len(skb);
4334 type_tucmd |= E1000_ADVTXD_TUCMD_IPV4;
4335 l4_hdr = ip_hdr(skb)->protocol;
4336 break;
4337 case __constant_htons(ETH_P_IPV6):
4338 vlan_macip_lens |= skb_network_header_len(skb);
4339 l4_hdr = ipv6_hdr(skb)->nexthdr;
4340 break;
4341 default:
4342 if (unlikely(net_ratelimit())) {
4343 dev_warn(tx_ring->dev,
4344 "partial checksum but proto=%x!\n",
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004345 first->protocol);
Arthur Jonesfa4a7ef2009-03-21 16:55:07 -07004346 }
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004347 break;
Auke Kok9d5c8242008-01-24 02:22:38 -08004348 }
4349
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004350 switch (l4_hdr) {
4351 case IPPROTO_TCP:
4352 type_tucmd |= E1000_ADVTXD_TUCMD_L4T_TCP;
4353 mss_l4len_idx = tcp_hdrlen(skb) <<
4354 E1000_ADVTXD_L4LEN_SHIFT;
4355 break;
4356 case IPPROTO_SCTP:
4357 type_tucmd |= E1000_ADVTXD_TUCMD_L4T_SCTP;
4358 mss_l4len_idx = sizeof(struct sctphdr) <<
4359 E1000_ADVTXD_L4LEN_SHIFT;
4360 break;
4361 case IPPROTO_UDP:
4362 mss_l4len_idx = sizeof(struct udphdr) <<
4363 E1000_ADVTXD_L4LEN_SHIFT;
4364 break;
4365 default:
4366 if (unlikely(net_ratelimit())) {
4367 dev_warn(tx_ring->dev,
4368 "partial checksum but l4 proto=%x!\n",
4369 l4_hdr);
4370 }
4371 break;
4372 }
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004373
4374 /* update TX checksum flag */
4375 first->tx_flags |= IGB_TX_FLAGS_CSUM;
Auke Kok9d5c8242008-01-24 02:22:38 -08004376 }
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004377
4378 vlan_macip_lens |= skb_network_offset(skb) << E1000_ADVTXD_MACLEN_SHIFT;
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004379 vlan_macip_lens |= first->tx_flags & IGB_TX_FLAGS_VLAN_MASK;
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004380
4381 igb_tx_ctxtdesc(tx_ring, vlan_macip_lens, type_tucmd, mss_l4len_idx);
Auke Kok9d5c8242008-01-24 02:22:38 -08004382}
4383
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004384#define IGB_SET_FLAG(_input, _flag, _result) \
4385 ((_flag <= _result) ? \
4386 ((u32)(_input & _flag) * (_result / _flag)) : \
4387 ((u32)(_input & _flag) / (_flag / _result)))
4388
4389static u32 igb_tx_cmd_type(struct sk_buff *skb, u32 tx_flags)
Alexander Duycke032afc2011-08-26 07:44:48 +00004390{
4391 /* set type for advanced descriptor with frame checksum insertion */
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004392 u32 cmd_type = E1000_ADVTXD_DTYP_DATA |
4393 E1000_ADVTXD_DCMD_DEXT |
4394 E1000_ADVTXD_DCMD_IFCS;
Alexander Duycke032afc2011-08-26 07:44:48 +00004395
4396 /* set HW vlan bit if vlan is present */
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004397 cmd_type |= IGB_SET_FLAG(tx_flags, IGB_TX_FLAGS_VLAN,
4398 (E1000_ADVTXD_DCMD_VLE));
Alexander Duycke032afc2011-08-26 07:44:48 +00004399
4400 /* set segmentation bits for TSO */
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004401 cmd_type |= IGB_SET_FLAG(tx_flags, IGB_TX_FLAGS_TSO,
4402 (E1000_ADVTXD_DCMD_TSE));
4403
4404 /* set timestamp bit if present */
4405 cmd_type |= IGB_SET_FLAG(tx_flags, IGB_TX_FLAGS_TSTAMP,
4406 (E1000_ADVTXD_MAC_TSTAMP));
4407
4408 /* insert frame checksum */
4409 cmd_type ^= IGB_SET_FLAG(skb->no_fcs, 1, E1000_ADVTXD_DCMD_IFCS);
Alexander Duycke032afc2011-08-26 07:44:48 +00004410
4411 return cmd_type;
4412}
4413
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004414static void igb_tx_olinfo_status(struct igb_ring *tx_ring,
4415 union e1000_adv_tx_desc *tx_desc,
4416 u32 tx_flags, unsigned int paylen)
Alexander Duycke032afc2011-08-26 07:44:48 +00004417{
4418 u32 olinfo_status = paylen << E1000_ADVTXD_PAYLEN_SHIFT;
4419
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004420 /* 82575 requires a unique index per ring */
4421 if (test_bit(IGB_RING_FLAG_TX_CTX_IDX, &tx_ring->flags))
Alexander Duycke032afc2011-08-26 07:44:48 +00004422 olinfo_status |= tx_ring->reg_idx << 4;
4423
4424 /* insert L4 checksum */
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004425 olinfo_status |= IGB_SET_FLAG(tx_flags,
4426 IGB_TX_FLAGS_CSUM,
4427 (E1000_TXD_POPTS_TXSM << 8));
Alexander Duycke032afc2011-08-26 07:44:48 +00004428
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004429 /* insert IPv4 checksum */
4430 olinfo_status |= IGB_SET_FLAG(tx_flags,
4431 IGB_TX_FLAGS_IPV4,
4432 (E1000_TXD_POPTS_IXSM << 8));
Alexander Duycke032afc2011-08-26 07:44:48 +00004433
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004434 tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status);
Alexander Duycke032afc2011-08-26 07:44:48 +00004435}
4436
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004437static void igb_tx_map(struct igb_ring *tx_ring,
4438 struct igb_tx_buffer *first,
Alexander Duyckebe42d12011-08-26 07:45:09 +00004439 const u8 hdr_len)
Auke Kok9d5c8242008-01-24 02:22:38 -08004440{
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004441 struct sk_buff *skb = first->skb;
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00004442 struct igb_tx_buffer *tx_buffer;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004443 union e1000_adv_tx_desc *tx_desc;
Alexander Duyck80d07592012-11-13 04:03:24 +00004444 struct skb_frag_struct *frag;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004445 dma_addr_t dma;
Alexander Duyck80d07592012-11-13 04:03:24 +00004446 unsigned int data_len, size;
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004447 u32 tx_flags = first->tx_flags;
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004448 u32 cmd_type = igb_tx_cmd_type(skb, tx_flags);
Alexander Duyckebe42d12011-08-26 07:45:09 +00004449 u16 i = tx_ring->next_to_use;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004450
4451 tx_desc = IGB_TX_DESC(tx_ring, i);
4452
Alexander Duyck80d07592012-11-13 04:03:24 +00004453 igb_tx_olinfo_status(tx_ring, tx_desc, tx_flags, skb->len - hdr_len);
4454
4455 size = skb_headlen(skb);
4456 data_len = skb->data_len;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004457
4458 dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
Auke Kok9d5c8242008-01-24 02:22:38 -08004459
Alexander Duyck80d07592012-11-13 04:03:24 +00004460 tx_buffer = first;
Alexander Duyck2bbfebe2011-08-26 07:44:59 +00004461
Alexander Duyck80d07592012-11-13 04:03:24 +00004462 for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
4463 if (dma_mapping_error(tx_ring->dev, dma))
4464 goto dma_error;
4465
4466 /* record length, and DMA address */
4467 dma_unmap_len_set(tx_buffer, len, size);
4468 dma_unmap_addr_set(tx_buffer, dma, dma);
4469
4470 tx_desc->read.buffer_addr = cpu_to_le64(dma);
4471
Alexander Duyckebe42d12011-08-26 07:45:09 +00004472 while (unlikely(size > IGB_MAX_DATA_PER_TXD)) {
4473 tx_desc->read.cmd_type_len =
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004474 cpu_to_le32(cmd_type ^ IGB_MAX_DATA_PER_TXD);
Auke Kok9d5c8242008-01-24 02:22:38 -08004475
Alexander Duyckebe42d12011-08-26 07:45:09 +00004476 i++;
4477 tx_desc++;
4478 if (i == tx_ring->count) {
4479 tx_desc = IGB_TX_DESC(tx_ring, 0);
4480 i = 0;
4481 }
Alexander Duyck80d07592012-11-13 04:03:24 +00004482 tx_desc->read.olinfo_status = 0;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004483
4484 dma += IGB_MAX_DATA_PER_TXD;
4485 size -= IGB_MAX_DATA_PER_TXD;
4486
Alexander Duyckebe42d12011-08-26 07:45:09 +00004487 tx_desc->read.buffer_addr = cpu_to_le64(dma);
4488 }
4489
4490 if (likely(!data_len))
4491 break;
4492
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004493 tx_desc->read.cmd_type_len = cpu_to_le32(cmd_type ^ size);
Alexander Duyckebe42d12011-08-26 07:45:09 +00004494
Alexander Duyck65689fe2009-03-20 00:17:43 +00004495 i++;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004496 tx_desc++;
4497 if (i == tx_ring->count) {
4498 tx_desc = IGB_TX_DESC(tx_ring, 0);
Alexander Duyck65689fe2009-03-20 00:17:43 +00004499 i = 0;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004500 }
Alexander Duyck80d07592012-11-13 04:03:24 +00004501 tx_desc->read.olinfo_status = 0;
Alexander Duyck65689fe2009-03-20 00:17:43 +00004502
Eric Dumazet9e903e02011-10-18 21:00:24 +00004503 size = skb_frag_size(frag);
Alexander Duyckebe42d12011-08-26 07:45:09 +00004504 data_len -= size;
4505
4506 dma = skb_frag_dma_map(tx_ring->dev, frag, 0,
Alexander Duyck80d07592012-11-13 04:03:24 +00004507 size, DMA_TO_DEVICE);
Alexander Duyck6366ad32009-12-02 16:47:18 +00004508
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00004509 tx_buffer = &tx_ring->tx_buffer_info[i];
Auke Kok9d5c8242008-01-24 02:22:38 -08004510 }
4511
Alexander Duyckebe42d12011-08-26 07:45:09 +00004512 /* write last descriptor with RS and EOP bits */
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004513 cmd_type |= size | IGB_TXD_DCMD;
4514 tx_desc->read.cmd_type_len = cpu_to_le32(cmd_type);
Alexander Duyck8542db02011-08-26 07:44:43 +00004515
Alexander Duyck80d07592012-11-13 04:03:24 +00004516 netdev_tx_sent_queue(txring_txq(tx_ring), first->bytecount);
4517
Alexander Duyck8542db02011-08-26 07:44:43 +00004518 /* set the timestamp */
4519 first->time_stamp = jiffies;
4520
Alexander Duyckebe42d12011-08-26 07:45:09 +00004521 /*
4522 * Force memory writes to complete before letting h/w know there
4523 * are new descriptors to fetch. (Only applicable for weak-ordered
4524 * memory model archs, such as IA-64).
4525 *
4526 * We also need this memory barrier to make certain all of the
4527 * status bits have been updated before next_to_watch is written.
4528 */
Auke Kok9d5c8242008-01-24 02:22:38 -08004529 wmb();
4530
Alexander Duyckebe42d12011-08-26 07:45:09 +00004531 /* set next_to_watch value indicating a packet is present */
4532 first->next_to_watch = tx_desc;
4533
4534 i++;
4535 if (i == tx_ring->count)
4536 i = 0;
4537
Auke Kok9d5c8242008-01-24 02:22:38 -08004538 tx_ring->next_to_use = i;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004539
Alexander Duyckfce99e32009-10-27 15:51:27 +00004540 writel(i, tx_ring->tail);
Alexander Duyckebe42d12011-08-26 07:45:09 +00004541
Auke Kok9d5c8242008-01-24 02:22:38 -08004542 /* we need this if more than one processor can write to our tail
4543 * at a time, it syncronizes IO on IA64/Altix systems */
4544 mmiowb();
Alexander Duyckebe42d12011-08-26 07:45:09 +00004545
4546 return;
4547
4548dma_error:
4549 dev_err(tx_ring->dev, "TX DMA map failed\n");
4550
4551 /* clear dma mappings for failed tx_buffer_info map */
4552 for (;;) {
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00004553 tx_buffer = &tx_ring->tx_buffer_info[i];
4554 igb_unmap_and_free_tx_resource(tx_ring, tx_buffer);
4555 if (tx_buffer == first)
Alexander Duyckebe42d12011-08-26 07:45:09 +00004556 break;
4557 if (i == 0)
4558 i = tx_ring->count;
4559 i--;
4560 }
4561
4562 tx_ring->next_to_use = i;
Auke Kok9d5c8242008-01-24 02:22:38 -08004563}
4564
Alexander Duyck6ad4edf2011-08-26 07:45:26 +00004565static int __igb_maybe_stop_tx(struct igb_ring *tx_ring, const u16 size)
Auke Kok9d5c8242008-01-24 02:22:38 -08004566{
Alexander Duycke694e962009-10-27 15:53:06 +00004567 struct net_device *netdev = tx_ring->netdev;
4568
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07004569 netif_stop_subqueue(netdev, tx_ring->queue_index);
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07004570
Auke Kok9d5c8242008-01-24 02:22:38 -08004571 /* Herbert's original patch had:
4572 * smp_mb__after_netif_stop_queue();
4573 * but since that doesn't exist yet, just open code it. */
4574 smp_mb();
4575
4576 /* We need to check again in a case another CPU has just
4577 * made room available. */
Alexander Duyckc493ea42009-03-20 00:16:50 +00004578 if (igb_desc_unused(tx_ring) < size)
Auke Kok9d5c8242008-01-24 02:22:38 -08004579 return -EBUSY;
4580
4581 /* A reprieve! */
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07004582 netif_wake_subqueue(netdev, tx_ring->queue_index);
Eric Dumazet12dcd862010-10-15 17:27:10 +00004583
4584 u64_stats_update_begin(&tx_ring->tx_syncp2);
4585 tx_ring->tx_stats.restart_queue2++;
4586 u64_stats_update_end(&tx_ring->tx_syncp2);
4587
Auke Kok9d5c8242008-01-24 02:22:38 -08004588 return 0;
4589}
4590
Alexander Duyck6ad4edf2011-08-26 07:45:26 +00004591static inline int igb_maybe_stop_tx(struct igb_ring *tx_ring, const u16 size)
Auke Kok9d5c8242008-01-24 02:22:38 -08004592{
Alexander Duyckc493ea42009-03-20 00:16:50 +00004593 if (igb_desc_unused(tx_ring) >= size)
Auke Kok9d5c8242008-01-24 02:22:38 -08004594 return 0;
Alexander Duycke694e962009-10-27 15:53:06 +00004595 return __igb_maybe_stop_tx(tx_ring, size);
Auke Kok9d5c8242008-01-24 02:22:38 -08004596}
4597
Alexander Duyckcd392f52011-08-26 07:43:59 +00004598netdev_tx_t igb_xmit_frame_ring(struct sk_buff *skb,
4599 struct igb_ring *tx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08004600{
Matthew Vick1f6e8172012-08-18 07:26:33 +00004601 struct igb_adapter *adapter = netdev_priv(tx_ring->netdev);
Alexander Duyck8542db02011-08-26 07:44:43 +00004602 struct igb_tx_buffer *first;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004603 int tso;
Nick Nunley91d4ee32010-02-17 01:04:56 +00004604 u32 tx_flags = 0;
Alexander Duyck21ba6fe2013-02-09 04:27:48 +00004605 u16 count = TXD_USE_COUNT(skb_headlen(skb));
Alexander Duyck31f6adb2011-08-26 07:44:53 +00004606 __be16 protocol = vlan_get_protocol(skb);
Nick Nunley91d4ee32010-02-17 01:04:56 +00004607 u8 hdr_len = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08004608
Alexander Duyck21ba6fe2013-02-09 04:27:48 +00004609 /* need: 1 descriptor per page * PAGE_SIZE/IGB_MAX_DATA_PER_TXD,
4610 * + 1 desc for skb_headlen/IGB_MAX_DATA_PER_TXD,
Auke Kok9d5c8242008-01-24 02:22:38 -08004611 * + 2 desc gap to keep tail from touching head,
Auke Kok9d5c8242008-01-24 02:22:38 -08004612 * + 1 desc for context descriptor,
Alexander Duyck21ba6fe2013-02-09 04:27:48 +00004613 * otherwise try next time
4614 */
4615 if (NETDEV_FRAG_PAGE_MAX_SIZE > IGB_MAX_DATA_PER_TXD) {
4616 unsigned short f;
4617 for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
4618 count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size);
4619 } else {
4620 count += skb_shinfo(skb)->nr_frags;
4621 }
4622
4623 if (igb_maybe_stop_tx(tx_ring, count + 3)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08004624 /* this is a hard error */
Auke Kok9d5c8242008-01-24 02:22:38 -08004625 return NETDEV_TX_BUSY;
4626 }
Patrick Ohly33af6bc2009-02-12 05:03:43 +00004627
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004628 /* record the location of the first descriptor for this packet */
4629 first = &tx_ring->tx_buffer_info[tx_ring->next_to_use];
4630 first->skb = skb;
4631 first->bytecount = skb->len;
4632 first->gso_segs = 1;
4633
Matthew Vickb66e2392012-12-13 07:20:33 +00004634 skb_tx_timestamp(skb);
4635
Matthew Vick1f6e8172012-08-18 07:26:33 +00004636 if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
4637 !(adapter->ptp_tx_skb))) {
Oliver Hartkopp2244d072010-08-17 08:59:14 +00004638 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
Patrick Ohly33af6bc2009-02-12 05:03:43 +00004639 tx_flags |= IGB_TX_FLAGS_TSTAMP;
Matthew Vick1f6e8172012-08-18 07:26:33 +00004640
4641 adapter->ptp_tx_skb = skb_get(skb);
Matthew Vick428f1f72012-12-13 07:20:34 +00004642 adapter->ptp_tx_start = jiffies;
Matthew Vick1f6e8172012-08-18 07:26:33 +00004643 if (adapter->hw.mac.type == e1000_82576)
4644 schedule_work(&adapter->ptp_tx_work);
Patrick Ohly33af6bc2009-02-12 05:03:43 +00004645 }
Auke Kok9d5c8242008-01-24 02:22:38 -08004646
Jesse Grosseab6d182010-10-20 13:56:03 +00004647 if (vlan_tx_tag_present(skb)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08004648 tx_flags |= IGB_TX_FLAGS_VLAN;
4649 tx_flags |= (vlan_tx_tag_get(skb) << IGB_TX_FLAGS_VLAN_SHIFT);
4650 }
4651
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004652 /* record initial flags and protocol */
4653 first->tx_flags = tx_flags;
4654 first->protocol = protocol;
Alexander Duyckcdfd01fc2009-10-27 23:50:57 +00004655
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004656 tso = igb_tso(tx_ring, first, &hdr_len);
4657 if (tso < 0)
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004658 goto out_drop;
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004659 else if (!tso)
4660 igb_tx_csum(tx_ring, first);
Auke Kok9d5c8242008-01-24 02:22:38 -08004661
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004662 igb_tx_map(tx_ring, first, hdr_len);
Alexander Duyck85ad76b2009-10-27 15:52:46 +00004663
4664 /* Make sure there is space in the ring for the next send. */
Alexander Duyck21ba6fe2013-02-09 04:27:48 +00004665 igb_maybe_stop_tx(tx_ring, DESC_NEEDED);
Alexander Duyck85ad76b2009-10-27 15:52:46 +00004666
Auke Kok9d5c8242008-01-24 02:22:38 -08004667 return NETDEV_TX_OK;
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004668
4669out_drop:
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004670 igb_unmap_and_free_tx_resource(tx_ring, first);
4671
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004672 return NETDEV_TX_OK;
Auke Kok9d5c8242008-01-24 02:22:38 -08004673}
4674
Alexander Duyck1cc3bd82011-08-26 07:44:10 +00004675static inline struct igb_ring *igb_tx_queue_mapping(struct igb_adapter *adapter,
4676 struct sk_buff *skb)
4677{
4678 unsigned int r_idx = skb->queue_mapping;
4679
4680 if (r_idx >= adapter->num_tx_queues)
4681 r_idx = r_idx % adapter->num_tx_queues;
4682
4683 return adapter->tx_ring[r_idx];
4684}
4685
Alexander Duyckcd392f52011-08-26 07:43:59 +00004686static netdev_tx_t igb_xmit_frame(struct sk_buff *skb,
4687 struct net_device *netdev)
Auke Kok9d5c8242008-01-24 02:22:38 -08004688{
4689 struct igb_adapter *adapter = netdev_priv(netdev);
Alexander Duyckb1a436c2009-10-27 15:54:43 +00004690
4691 if (test_bit(__IGB_DOWN, &adapter->state)) {
4692 dev_kfree_skb_any(skb);
4693 return NETDEV_TX_OK;
4694 }
4695
4696 if (skb->len <= 0) {
4697 dev_kfree_skb_any(skb);
4698 return NETDEV_TX_OK;
4699 }
4700
Alexander Duyck1cc3bd82011-08-26 07:44:10 +00004701 /*
4702 * The minimum packet size with TCTL.PSP set is 17 so pad the skb
4703 * in order to meet this minimum size requirement.
4704 */
Tushar Daveea5ceea2012-09-14 03:43:43 +00004705 if (unlikely(skb->len < 17)) {
4706 if (skb_pad(skb, 17 - skb->len))
Alexander Duyck1cc3bd82011-08-26 07:44:10 +00004707 return NETDEV_TX_OK;
4708 skb->len = 17;
Tushar Daveea5ceea2012-09-14 03:43:43 +00004709 skb_set_tail_pointer(skb, 17);
Alexander Duyck1cc3bd82011-08-26 07:44:10 +00004710 }
Auke Kok9d5c8242008-01-24 02:22:38 -08004711
Alexander Duyck1cc3bd82011-08-26 07:44:10 +00004712 return igb_xmit_frame_ring(skb, igb_tx_queue_mapping(adapter, skb));
Auke Kok9d5c8242008-01-24 02:22:38 -08004713}
4714
4715/**
4716 * igb_tx_timeout - Respond to a Tx Hang
4717 * @netdev: network interface device structure
4718 **/
4719static void igb_tx_timeout(struct net_device *netdev)
4720{
4721 struct igb_adapter *adapter = netdev_priv(netdev);
4722 struct e1000_hw *hw = &adapter->hw;
4723
4724 /* Do the reset outside of interrupt context */
4725 adapter->tx_timeout_count++;
Alexander Duyckf7ba2052009-10-27 23:48:51 +00004726
Alexander Duyck06218a82011-08-26 07:46:55 +00004727 if (hw->mac.type >= e1000_82580)
Alexander Duyck55cac242009-11-19 12:42:21 +00004728 hw->dev_spec._82575.global_device_reset = true;
4729
Auke Kok9d5c8242008-01-24 02:22:38 -08004730 schedule_work(&adapter->reset_task);
Alexander Duyck265de402009-02-06 23:22:52 +00004731 wr32(E1000_EICS,
4732 (adapter->eims_enable_mask & ~adapter->eims_other));
Auke Kok9d5c8242008-01-24 02:22:38 -08004733}
4734
4735static void igb_reset_task(struct work_struct *work)
4736{
4737 struct igb_adapter *adapter;
4738 adapter = container_of(work, struct igb_adapter, reset_task);
4739
Taku Izumic97ec422010-04-27 14:39:30 +00004740 igb_dump(adapter);
4741 netdev_err(adapter->netdev, "Reset adapter\n");
Auke Kok9d5c8242008-01-24 02:22:38 -08004742 igb_reinit_locked(adapter);
4743}
4744
4745/**
Eric Dumazet12dcd862010-10-15 17:27:10 +00004746 * igb_get_stats64 - Get System Network Statistics
Auke Kok9d5c8242008-01-24 02:22:38 -08004747 * @netdev: network interface device structure
Eric Dumazet12dcd862010-10-15 17:27:10 +00004748 * @stats: rtnl_link_stats64 pointer
Auke Kok9d5c8242008-01-24 02:22:38 -08004749 *
Auke Kok9d5c8242008-01-24 02:22:38 -08004750 **/
Eric Dumazet12dcd862010-10-15 17:27:10 +00004751static struct rtnl_link_stats64 *igb_get_stats64(struct net_device *netdev,
4752 struct rtnl_link_stats64 *stats)
Auke Kok9d5c8242008-01-24 02:22:38 -08004753{
Eric Dumazet12dcd862010-10-15 17:27:10 +00004754 struct igb_adapter *adapter = netdev_priv(netdev);
4755
4756 spin_lock(&adapter->stats64_lock);
4757 igb_update_stats(adapter, &adapter->stats64);
4758 memcpy(stats, &adapter->stats64, sizeof(*stats));
4759 spin_unlock(&adapter->stats64_lock);
4760
4761 return stats;
Auke Kok9d5c8242008-01-24 02:22:38 -08004762}
4763
4764/**
4765 * igb_change_mtu - Change the Maximum Transfer Unit
4766 * @netdev: network interface device structure
4767 * @new_mtu: new value for maximum frame size
4768 *
4769 * Returns 0 on success, negative on failure
4770 **/
4771static int igb_change_mtu(struct net_device *netdev, int new_mtu)
4772{
4773 struct igb_adapter *adapter = netdev_priv(netdev);
Alexander Duyck090b1792009-10-27 23:51:55 +00004774 struct pci_dev *pdev = adapter->pdev;
Alexander Duyck153285f2011-08-26 07:43:32 +00004775 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
Auke Kok9d5c8242008-01-24 02:22:38 -08004776
Alexander Duyckc809d222009-10-27 23:52:13 +00004777 if ((new_mtu < 68) || (max_frame > MAX_JUMBO_FRAME_SIZE)) {
Alexander Duyck090b1792009-10-27 23:51:55 +00004778 dev_err(&pdev->dev, "Invalid MTU setting\n");
Auke Kok9d5c8242008-01-24 02:22:38 -08004779 return -EINVAL;
4780 }
4781
Alexander Duyck153285f2011-08-26 07:43:32 +00004782#define MAX_STD_JUMBO_FRAME_SIZE 9238
Auke Kok9d5c8242008-01-24 02:22:38 -08004783 if (max_frame > MAX_STD_JUMBO_FRAME_SIZE) {
Alexander Duyck090b1792009-10-27 23:51:55 +00004784 dev_err(&pdev->dev, "MTU > 9216 not supported.\n");
Auke Kok9d5c8242008-01-24 02:22:38 -08004785 return -EINVAL;
4786 }
4787
4788 while (test_and_set_bit(__IGB_RESETTING, &adapter->state))
4789 msleep(1);
Alexander Duyck73cd78f2009-02-12 18:16:59 +00004790
Auke Kok9d5c8242008-01-24 02:22:38 -08004791 /* igb_down has a dependency on max_frame_size */
4792 adapter->max_frame_size = max_frame;
Alexander Duyck559e9c42009-10-27 23:52:50 +00004793
Alexander Duyck4c844852009-10-27 15:52:07 +00004794 if (netif_running(netdev))
4795 igb_down(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08004796
Alexander Duyck090b1792009-10-27 23:51:55 +00004797 dev_info(&pdev->dev, "changing MTU from %d to %d\n",
Auke Kok9d5c8242008-01-24 02:22:38 -08004798 netdev->mtu, new_mtu);
4799 netdev->mtu = new_mtu;
4800
4801 if (netif_running(netdev))
4802 igb_up(adapter);
4803 else
4804 igb_reset(adapter);
4805
4806 clear_bit(__IGB_RESETTING, &adapter->state);
4807
4808 return 0;
4809}
4810
4811/**
4812 * igb_update_stats - Update the board statistics counters
4813 * @adapter: board private structure
4814 **/
4815
Eric Dumazet12dcd862010-10-15 17:27:10 +00004816void igb_update_stats(struct igb_adapter *adapter,
4817 struct rtnl_link_stats64 *net_stats)
Auke Kok9d5c8242008-01-24 02:22:38 -08004818{
4819 struct e1000_hw *hw = &adapter->hw;
4820 struct pci_dev *pdev = adapter->pdev;
Mitch Williamsfa3d9a62010-03-23 18:34:38 +00004821 u32 reg, mpc;
Auke Kok9d5c8242008-01-24 02:22:38 -08004822 u16 phy_tmp;
Alexander Duyck3f9c0162009-10-27 23:48:12 +00004823 int i;
4824 u64 bytes, packets;
Eric Dumazet12dcd862010-10-15 17:27:10 +00004825 unsigned int start;
4826 u64 _bytes, _packets;
Auke Kok9d5c8242008-01-24 02:22:38 -08004827
4828#define PHY_IDLE_ERROR_COUNT_MASK 0x00FF
4829
4830 /*
4831 * Prevent stats update while adapter is being reset, or if the pci
4832 * connection is down.
4833 */
4834 if (adapter->link_speed == 0)
4835 return;
4836 if (pci_channel_offline(pdev))
4837 return;
4838
Alexander Duyck3f9c0162009-10-27 23:48:12 +00004839 bytes = 0;
4840 packets = 0;
4841 for (i = 0; i < adapter->num_rx_queues; i++) {
Alexander Duyckae1c07a2012-08-08 05:23:22 +00004842 u32 rqdpc = rd32(E1000_RQDPC(i));
Alexander Duyck3025a442010-02-17 01:02:39 +00004843 struct igb_ring *ring = adapter->rx_ring[i];
Eric Dumazet12dcd862010-10-15 17:27:10 +00004844
Alexander Duyckae1c07a2012-08-08 05:23:22 +00004845 if (rqdpc) {
4846 ring->rx_stats.drops += rqdpc;
4847 net_stats->rx_fifo_errors += rqdpc;
4848 }
Eric Dumazet12dcd862010-10-15 17:27:10 +00004849
4850 do {
4851 start = u64_stats_fetch_begin_bh(&ring->rx_syncp);
4852 _bytes = ring->rx_stats.bytes;
4853 _packets = ring->rx_stats.packets;
4854 } while (u64_stats_fetch_retry_bh(&ring->rx_syncp, start));
4855 bytes += _bytes;
4856 packets += _packets;
Alexander Duyck3f9c0162009-10-27 23:48:12 +00004857 }
4858
Alexander Duyck128e45e2009-11-12 18:37:38 +00004859 net_stats->rx_bytes = bytes;
4860 net_stats->rx_packets = packets;
Alexander Duyck3f9c0162009-10-27 23:48:12 +00004861
4862 bytes = 0;
4863 packets = 0;
4864 for (i = 0; i < adapter->num_tx_queues; i++) {
Alexander Duyck3025a442010-02-17 01:02:39 +00004865 struct igb_ring *ring = adapter->tx_ring[i];
Eric Dumazet12dcd862010-10-15 17:27:10 +00004866 do {
4867 start = u64_stats_fetch_begin_bh(&ring->tx_syncp);
4868 _bytes = ring->tx_stats.bytes;
4869 _packets = ring->tx_stats.packets;
4870 } while (u64_stats_fetch_retry_bh(&ring->tx_syncp, start));
4871 bytes += _bytes;
4872 packets += _packets;
Alexander Duyck3f9c0162009-10-27 23:48:12 +00004873 }
Alexander Duyck128e45e2009-11-12 18:37:38 +00004874 net_stats->tx_bytes = bytes;
4875 net_stats->tx_packets = packets;
Alexander Duyck3f9c0162009-10-27 23:48:12 +00004876
4877 /* read stats registers */
Auke Kok9d5c8242008-01-24 02:22:38 -08004878 adapter->stats.crcerrs += rd32(E1000_CRCERRS);
4879 adapter->stats.gprc += rd32(E1000_GPRC);
4880 adapter->stats.gorc += rd32(E1000_GORCL);
4881 rd32(E1000_GORCH); /* clear GORCL */
4882 adapter->stats.bprc += rd32(E1000_BPRC);
4883 adapter->stats.mprc += rd32(E1000_MPRC);
4884 adapter->stats.roc += rd32(E1000_ROC);
4885
4886 adapter->stats.prc64 += rd32(E1000_PRC64);
4887 adapter->stats.prc127 += rd32(E1000_PRC127);
4888 adapter->stats.prc255 += rd32(E1000_PRC255);
4889 adapter->stats.prc511 += rd32(E1000_PRC511);
4890 adapter->stats.prc1023 += rd32(E1000_PRC1023);
4891 adapter->stats.prc1522 += rd32(E1000_PRC1522);
4892 adapter->stats.symerrs += rd32(E1000_SYMERRS);
4893 adapter->stats.sec += rd32(E1000_SEC);
4894
Mitch Williamsfa3d9a62010-03-23 18:34:38 +00004895 mpc = rd32(E1000_MPC);
4896 adapter->stats.mpc += mpc;
4897 net_stats->rx_fifo_errors += mpc;
Auke Kok9d5c8242008-01-24 02:22:38 -08004898 adapter->stats.scc += rd32(E1000_SCC);
4899 adapter->stats.ecol += rd32(E1000_ECOL);
4900 adapter->stats.mcc += rd32(E1000_MCC);
4901 adapter->stats.latecol += rd32(E1000_LATECOL);
4902 adapter->stats.dc += rd32(E1000_DC);
4903 adapter->stats.rlec += rd32(E1000_RLEC);
4904 adapter->stats.xonrxc += rd32(E1000_XONRXC);
4905 adapter->stats.xontxc += rd32(E1000_XONTXC);
4906 adapter->stats.xoffrxc += rd32(E1000_XOFFRXC);
4907 adapter->stats.xofftxc += rd32(E1000_XOFFTXC);
4908 adapter->stats.fcruc += rd32(E1000_FCRUC);
4909 adapter->stats.gptc += rd32(E1000_GPTC);
4910 adapter->stats.gotc += rd32(E1000_GOTCL);
4911 rd32(E1000_GOTCH); /* clear GOTCL */
Mitch Williamsfa3d9a62010-03-23 18:34:38 +00004912 adapter->stats.rnbc += rd32(E1000_RNBC);
Auke Kok9d5c8242008-01-24 02:22:38 -08004913 adapter->stats.ruc += rd32(E1000_RUC);
4914 adapter->stats.rfc += rd32(E1000_RFC);
4915 adapter->stats.rjc += rd32(E1000_RJC);
4916 adapter->stats.tor += rd32(E1000_TORH);
4917 adapter->stats.tot += rd32(E1000_TOTH);
4918 adapter->stats.tpr += rd32(E1000_TPR);
4919
4920 adapter->stats.ptc64 += rd32(E1000_PTC64);
4921 adapter->stats.ptc127 += rd32(E1000_PTC127);
4922 adapter->stats.ptc255 += rd32(E1000_PTC255);
4923 adapter->stats.ptc511 += rd32(E1000_PTC511);
4924 adapter->stats.ptc1023 += rd32(E1000_PTC1023);
4925 adapter->stats.ptc1522 += rd32(E1000_PTC1522);
4926
4927 adapter->stats.mptc += rd32(E1000_MPTC);
4928 adapter->stats.bptc += rd32(E1000_BPTC);
4929
Nick Nunley2d0b0f62010-02-17 01:02:59 +00004930 adapter->stats.tpt += rd32(E1000_TPT);
4931 adapter->stats.colc += rd32(E1000_COLC);
Auke Kok9d5c8242008-01-24 02:22:38 -08004932
4933 adapter->stats.algnerrc += rd32(E1000_ALGNERRC);
Nick Nunley43915c7c2010-02-17 01:03:58 +00004934 /* read internal phy specific stats */
4935 reg = rd32(E1000_CTRL_EXT);
4936 if (!(reg & E1000_CTRL_EXT_LINK_MODE_MASK)) {
4937 adapter->stats.rxerrc += rd32(E1000_RXERRC);
Carolyn Wyborny3dbdf962012-09-12 04:36:24 +00004938
4939 /* this stat has invalid values on i210/i211 */
4940 if ((hw->mac.type != e1000_i210) &&
4941 (hw->mac.type != e1000_i211))
4942 adapter->stats.tncrs += rd32(E1000_TNCRS);
Nick Nunley43915c7c2010-02-17 01:03:58 +00004943 }
4944
Auke Kok9d5c8242008-01-24 02:22:38 -08004945 adapter->stats.tsctc += rd32(E1000_TSCTC);
4946 adapter->stats.tsctfc += rd32(E1000_TSCTFC);
4947
4948 adapter->stats.iac += rd32(E1000_IAC);
4949 adapter->stats.icrxoc += rd32(E1000_ICRXOC);
4950 adapter->stats.icrxptc += rd32(E1000_ICRXPTC);
4951 adapter->stats.icrxatc += rd32(E1000_ICRXATC);
4952 adapter->stats.ictxptc += rd32(E1000_ICTXPTC);
4953 adapter->stats.ictxatc += rd32(E1000_ICTXATC);
4954 adapter->stats.ictxqec += rd32(E1000_ICTXQEC);
4955 adapter->stats.ictxqmtc += rd32(E1000_ICTXQMTC);
4956 adapter->stats.icrxdmtc += rd32(E1000_ICRXDMTC);
4957
4958 /* Fill out the OS statistics structure */
Alexander Duyck128e45e2009-11-12 18:37:38 +00004959 net_stats->multicast = adapter->stats.mprc;
4960 net_stats->collisions = adapter->stats.colc;
Auke Kok9d5c8242008-01-24 02:22:38 -08004961
4962 /* Rx Errors */
4963
4964 /* RLEC on some newer hardware can be incorrect so build
Jesper Dangaard Brouer8c0ab702009-05-26 13:50:31 +00004965 * our own version based on RUC and ROC */
Alexander Duyck128e45e2009-11-12 18:37:38 +00004966 net_stats->rx_errors = adapter->stats.rxerrc +
Auke Kok9d5c8242008-01-24 02:22:38 -08004967 adapter->stats.crcerrs + adapter->stats.algnerrc +
4968 adapter->stats.ruc + adapter->stats.roc +
4969 adapter->stats.cexterr;
Alexander Duyck128e45e2009-11-12 18:37:38 +00004970 net_stats->rx_length_errors = adapter->stats.ruc +
4971 adapter->stats.roc;
4972 net_stats->rx_crc_errors = adapter->stats.crcerrs;
4973 net_stats->rx_frame_errors = adapter->stats.algnerrc;
4974 net_stats->rx_missed_errors = adapter->stats.mpc;
Auke Kok9d5c8242008-01-24 02:22:38 -08004975
4976 /* Tx Errors */
Alexander Duyck128e45e2009-11-12 18:37:38 +00004977 net_stats->tx_errors = adapter->stats.ecol +
4978 adapter->stats.latecol;
4979 net_stats->tx_aborted_errors = adapter->stats.ecol;
4980 net_stats->tx_window_errors = adapter->stats.latecol;
4981 net_stats->tx_carrier_errors = adapter->stats.tncrs;
Auke Kok9d5c8242008-01-24 02:22:38 -08004982
4983 /* Tx Dropped needs to be maintained elsewhere */
4984
4985 /* Phy Stats */
4986 if (hw->phy.media_type == e1000_media_type_copper) {
4987 if ((adapter->link_speed == SPEED_1000) &&
Alexander Duyck73cd78f2009-02-12 18:16:59 +00004988 (!igb_read_phy_reg(hw, PHY_1000T_STATUS, &phy_tmp))) {
Auke Kok9d5c8242008-01-24 02:22:38 -08004989 phy_tmp &= PHY_IDLE_ERROR_COUNT_MASK;
4990 adapter->phy_stats.idle_errors += phy_tmp;
4991 }
4992 }
4993
4994 /* Management Stats */
4995 adapter->stats.mgptc += rd32(E1000_MGTPTC);
4996 adapter->stats.mgprc += rd32(E1000_MGTPRC);
4997 adapter->stats.mgpdc += rd32(E1000_MGTPDC);
Carolyn Wyborny0a915b92011-02-26 07:42:37 +00004998
4999 /* OS2BMC Stats */
5000 reg = rd32(E1000_MANC);
5001 if (reg & E1000_MANC_EN_BMC2OS) {
5002 adapter->stats.o2bgptc += rd32(E1000_O2BGPTC);
5003 adapter->stats.o2bspc += rd32(E1000_O2BSPC);
5004 adapter->stats.b2ospc += rd32(E1000_B2OSPC);
5005 adapter->stats.b2ogprc += rd32(E1000_B2OGPRC);
5006 }
Auke Kok9d5c8242008-01-24 02:22:38 -08005007}
5008
Auke Kok9d5c8242008-01-24 02:22:38 -08005009static irqreturn_t igb_msix_other(int irq, void *data)
5010{
Alexander Duyck047e0032009-10-27 15:49:27 +00005011 struct igb_adapter *adapter = data;
Auke Kok9d5c8242008-01-24 02:22:38 -08005012 struct e1000_hw *hw = &adapter->hw;
PJ Waskiewicz844290e2008-06-27 11:00:39 -07005013 u32 icr = rd32(E1000_ICR);
PJ Waskiewicz844290e2008-06-27 11:00:39 -07005014 /* reading ICR causes bit 31 of EICR to be cleared */
Alexander Duyckdda0e082009-02-06 23:19:08 +00005015
Alexander Duyck7f081d42010-01-07 17:41:00 +00005016 if (icr & E1000_ICR_DRSTA)
5017 schedule_work(&adapter->reset_task);
5018
Alexander Duyck047e0032009-10-27 15:49:27 +00005019 if (icr & E1000_ICR_DOUTSYNC) {
Alexander Duyckdda0e082009-02-06 23:19:08 +00005020 /* HW is reporting DMA is out of sync */
5021 adapter->stats.doosync++;
Greg Rose13800462010-11-06 02:08:26 +00005022 /* The DMA Out of Sync is also indication of a spoof event
5023 * in IOV mode. Check the Wrong VM Behavior register to
5024 * see if it is really a spoof event. */
5025 igb_check_wvbr(adapter);
Alexander Duyckdda0e082009-02-06 23:19:08 +00005026 }
Alexander Duyckeebbbdb2009-02-06 23:19:29 +00005027
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005028 /* Check for a mailbox event */
5029 if (icr & E1000_ICR_VMMB)
5030 igb_msg_task(adapter);
5031
5032 if (icr & E1000_ICR_LSC) {
5033 hw->mac.get_link_status = 1;
5034 /* guard against interrupt when we're going down */
5035 if (!test_bit(__IGB_DOWN, &adapter->state))
5036 mod_timer(&adapter->watchdog_timer, jiffies + 1);
5037 }
5038
Matthew Vick1f6e8172012-08-18 07:26:33 +00005039 if (icr & E1000_ICR_TS) {
5040 u32 tsicr = rd32(E1000_TSICR);
5041
5042 if (tsicr & E1000_TSICR_TXTS) {
5043 /* acknowledge the interrupt */
5044 wr32(E1000_TSICR, E1000_TSICR_TXTS);
5045 /* retrieve hardware timestamp */
5046 schedule_work(&adapter->ptp_tx_work);
5047 }
5048 }
Matthew Vick1f6e8172012-08-18 07:26:33 +00005049
PJ Waskiewicz844290e2008-06-27 11:00:39 -07005050 wr32(E1000_EIMS, adapter->eims_other);
Auke Kok9d5c8242008-01-24 02:22:38 -08005051
5052 return IRQ_HANDLED;
5053}
5054
Alexander Duyck047e0032009-10-27 15:49:27 +00005055static void igb_write_itr(struct igb_q_vector *q_vector)
Auke Kok9d5c8242008-01-24 02:22:38 -08005056{
Alexander Duyck26b39272010-02-17 01:00:41 +00005057 struct igb_adapter *adapter = q_vector->adapter;
Alexander Duyck047e0032009-10-27 15:49:27 +00005058 u32 itr_val = q_vector->itr_val & 0x7FFC;
Auke Kok9d5c8242008-01-24 02:22:38 -08005059
Alexander Duyck047e0032009-10-27 15:49:27 +00005060 if (!q_vector->set_itr)
5061 return;
Alexander Duyck73cd78f2009-02-12 18:16:59 +00005062
Alexander Duyck047e0032009-10-27 15:49:27 +00005063 if (!itr_val)
5064 itr_val = 0x4;
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07005065
Alexander Duyck26b39272010-02-17 01:00:41 +00005066 if (adapter->hw.mac.type == e1000_82575)
5067 itr_val |= itr_val << 16;
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07005068 else
Alexander Duyck0ba82992011-08-26 07:45:47 +00005069 itr_val |= E1000_EITR_CNT_IGNR;
Alexander Duyck047e0032009-10-27 15:49:27 +00005070
5071 writel(itr_val, q_vector->itr_register);
5072 q_vector->set_itr = 0;
5073}
5074
5075static irqreturn_t igb_msix_ring(int irq, void *data)
5076{
5077 struct igb_q_vector *q_vector = data;
5078
5079 /* Write the ITR value calculated from the previous interrupt. */
5080 igb_write_itr(q_vector);
5081
5082 napi_schedule(&q_vector->napi);
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07005083
Auke Kok9d5c8242008-01-24 02:22:38 -08005084 return IRQ_HANDLED;
5085}
5086
Jeff Kirsher421e02f2008-10-17 11:08:31 -07005087#ifdef CONFIG_IGB_DCA
Alexander Duyck6a050042012-09-25 00:31:27 +00005088static void igb_update_tx_dca(struct igb_adapter *adapter,
5089 struct igb_ring *tx_ring,
5090 int cpu)
5091{
5092 struct e1000_hw *hw = &adapter->hw;
5093 u32 txctrl = dca3_get_tag(tx_ring->dev, cpu);
5094
5095 if (hw->mac.type != e1000_82575)
5096 txctrl <<= E1000_DCA_TXCTRL_CPUID_SHIFT;
5097
5098 /*
5099 * We can enable relaxed ordering for reads, but not writes when
5100 * DCA is enabled. This is due to a known issue in some chipsets
5101 * which will cause the DCA tag to be cleared.
5102 */
5103 txctrl |= E1000_DCA_TXCTRL_DESC_RRO_EN |
5104 E1000_DCA_TXCTRL_DATA_RRO_EN |
5105 E1000_DCA_TXCTRL_DESC_DCA_EN;
5106
5107 wr32(E1000_DCA_TXCTRL(tx_ring->reg_idx), txctrl);
5108}
5109
5110static void igb_update_rx_dca(struct igb_adapter *adapter,
5111 struct igb_ring *rx_ring,
5112 int cpu)
5113{
5114 struct e1000_hw *hw = &adapter->hw;
5115 u32 rxctrl = dca3_get_tag(&adapter->pdev->dev, cpu);
5116
5117 if (hw->mac.type != e1000_82575)
5118 rxctrl <<= E1000_DCA_RXCTRL_CPUID_SHIFT;
5119
5120 /*
5121 * We can enable relaxed ordering for reads, but not writes when
5122 * DCA is enabled. This is due to a known issue in some chipsets
5123 * which will cause the DCA tag to be cleared.
5124 */
5125 rxctrl |= E1000_DCA_RXCTRL_DESC_RRO_EN |
5126 E1000_DCA_RXCTRL_DESC_DCA_EN;
5127
5128 wr32(E1000_DCA_RXCTRL(rx_ring->reg_idx), rxctrl);
5129}
5130
Alexander Duyck047e0032009-10-27 15:49:27 +00005131static void igb_update_dca(struct igb_q_vector *q_vector)
Jeb Cramerfe4506b2008-07-08 15:07:55 -07005132{
Alexander Duyck047e0032009-10-27 15:49:27 +00005133 struct igb_adapter *adapter = q_vector->adapter;
Jeb Cramerfe4506b2008-07-08 15:07:55 -07005134 int cpu = get_cpu();
Jeb Cramerfe4506b2008-07-08 15:07:55 -07005135
Alexander Duyck047e0032009-10-27 15:49:27 +00005136 if (q_vector->cpu == cpu)
5137 goto out_no_update;
5138
Alexander Duyck6a050042012-09-25 00:31:27 +00005139 if (q_vector->tx.ring)
5140 igb_update_tx_dca(adapter, q_vector->tx.ring, cpu);
5141
5142 if (q_vector->rx.ring)
5143 igb_update_rx_dca(adapter, q_vector->rx.ring, cpu);
5144
Alexander Duyck047e0032009-10-27 15:49:27 +00005145 q_vector->cpu = cpu;
5146out_no_update:
Jeb Cramerfe4506b2008-07-08 15:07:55 -07005147 put_cpu();
5148}
5149
5150static void igb_setup_dca(struct igb_adapter *adapter)
5151{
Alexander Duyck7e0e99e2009-05-21 13:06:56 +00005152 struct e1000_hw *hw = &adapter->hw;
Jeb Cramerfe4506b2008-07-08 15:07:55 -07005153 int i;
5154
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07005155 if (!(adapter->flags & IGB_FLAG_DCA_ENABLED))
Jeb Cramerfe4506b2008-07-08 15:07:55 -07005156 return;
5157
Alexander Duyck7e0e99e2009-05-21 13:06:56 +00005158 /* Always use CB2 mode, difference is masked in the CB driver. */
5159 wr32(E1000_DCA_CTRL, E1000_DCA_CTRL_DCA_MODE_CB2);
5160
Alexander Duyck047e0032009-10-27 15:49:27 +00005161 for (i = 0; i < adapter->num_q_vectors; i++) {
Alexander Duyck26b39272010-02-17 01:00:41 +00005162 adapter->q_vector[i]->cpu = -1;
5163 igb_update_dca(adapter->q_vector[i]);
Jeb Cramerfe4506b2008-07-08 15:07:55 -07005164 }
5165}
5166
5167static int __igb_notify_dca(struct device *dev, void *data)
5168{
5169 struct net_device *netdev = dev_get_drvdata(dev);
5170 struct igb_adapter *adapter = netdev_priv(netdev);
Alexander Duyck090b1792009-10-27 23:51:55 +00005171 struct pci_dev *pdev = adapter->pdev;
Jeb Cramerfe4506b2008-07-08 15:07:55 -07005172 struct e1000_hw *hw = &adapter->hw;
5173 unsigned long event = *(unsigned long *)data;
5174
5175 switch (event) {
5176 case DCA_PROVIDER_ADD:
5177 /* if already enabled, don't do it again */
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07005178 if (adapter->flags & IGB_FLAG_DCA_ENABLED)
Jeb Cramerfe4506b2008-07-08 15:07:55 -07005179 break;
Jeb Cramerfe4506b2008-07-08 15:07:55 -07005180 if (dca_add_requester(dev) == 0) {
Alexander Duyckbbd98fe2009-01-31 00:52:30 -08005181 adapter->flags |= IGB_FLAG_DCA_ENABLED;
Alexander Duyck090b1792009-10-27 23:51:55 +00005182 dev_info(&pdev->dev, "DCA enabled\n");
Jeb Cramerfe4506b2008-07-08 15:07:55 -07005183 igb_setup_dca(adapter);
5184 break;
5185 }
5186 /* Fall Through since DCA is disabled. */
5187 case DCA_PROVIDER_REMOVE:
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07005188 if (adapter->flags & IGB_FLAG_DCA_ENABLED) {
Jeb Cramerfe4506b2008-07-08 15:07:55 -07005189 /* without this a class_device is left
Alexander Duyck047e0032009-10-27 15:49:27 +00005190 * hanging around in the sysfs model */
Jeb Cramerfe4506b2008-07-08 15:07:55 -07005191 dca_remove_requester(dev);
Alexander Duyck090b1792009-10-27 23:51:55 +00005192 dev_info(&pdev->dev, "DCA disabled\n");
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07005193 adapter->flags &= ~IGB_FLAG_DCA_ENABLED;
Alexander Duyckcbd347a2009-02-15 23:59:44 -08005194 wr32(E1000_DCA_CTRL, E1000_DCA_CTRL_DCA_MODE_DISABLE);
Jeb Cramerfe4506b2008-07-08 15:07:55 -07005195 }
5196 break;
5197 }
Alexander Duyckbbd98fe2009-01-31 00:52:30 -08005198
Jeb Cramerfe4506b2008-07-08 15:07:55 -07005199 return 0;
5200}
5201
5202static int igb_notify_dca(struct notifier_block *nb, unsigned long event,
5203 void *p)
5204{
5205 int ret_val;
5206
5207 ret_val = driver_for_each_device(&igb_driver.driver, NULL, &event,
5208 __igb_notify_dca);
5209
5210 return ret_val ? NOTIFY_BAD : NOTIFY_DONE;
5211}
Jeff Kirsher421e02f2008-10-17 11:08:31 -07005212#endif /* CONFIG_IGB_DCA */
Auke Kok9d5c8242008-01-24 02:22:38 -08005213
Greg Rose0224d662011-10-14 02:57:14 +00005214#ifdef CONFIG_PCI_IOV
5215static int igb_vf_configure(struct igb_adapter *adapter, int vf)
5216{
5217 unsigned char mac_addr[ETH_ALEN];
Greg Rose0224d662011-10-14 02:57:14 +00005218
Mitch A Williams5ac6f912013-01-18 08:57:20 +00005219 eth_zero_addr(mac_addr);
Greg Rose0224d662011-10-14 02:57:14 +00005220 igb_set_vf_mac(adapter, vf, mac_addr);
5221
Stefan Assmannf5571472012-08-18 04:06:11 +00005222 return 0;
Greg Rose0224d662011-10-14 02:57:14 +00005223}
5224
Stefan Assmannf5571472012-08-18 04:06:11 +00005225static bool igb_vfs_are_assigned(struct igb_adapter *adapter)
Greg Rose0224d662011-10-14 02:57:14 +00005226{
Greg Rose0224d662011-10-14 02:57:14 +00005227 struct pci_dev *pdev = adapter->pdev;
Stefan Assmannf5571472012-08-18 04:06:11 +00005228 struct pci_dev *vfdev;
5229 int dev_id;
Greg Rose0224d662011-10-14 02:57:14 +00005230
5231 switch (adapter->hw.mac.type) {
5232 case e1000_82576:
Stefan Assmannf5571472012-08-18 04:06:11 +00005233 dev_id = IGB_82576_VF_DEV_ID;
Greg Rose0224d662011-10-14 02:57:14 +00005234 break;
5235 case e1000_i350:
Stefan Assmannf5571472012-08-18 04:06:11 +00005236 dev_id = IGB_I350_VF_DEV_ID;
Greg Rose0224d662011-10-14 02:57:14 +00005237 break;
5238 default:
Stefan Assmannf5571472012-08-18 04:06:11 +00005239 return false;
Greg Rose0224d662011-10-14 02:57:14 +00005240 }
5241
Stefan Assmannf5571472012-08-18 04:06:11 +00005242 /* loop through all the VFs to see if we own any that are assigned */
5243 vfdev = pci_get_device(PCI_VENDOR_ID_INTEL, dev_id, NULL);
5244 while (vfdev) {
5245 /* if we don't own it we don't care */
5246 if (vfdev->is_virtfn && vfdev->physfn == pdev) {
5247 /* if it is assigned we cannot release it */
5248 if (vfdev->dev_flags & PCI_DEV_FLAGS_ASSIGNED)
Greg Rose0224d662011-10-14 02:57:14 +00005249 return true;
5250 }
Stefan Assmannf5571472012-08-18 04:06:11 +00005251
5252 vfdev = pci_get_device(PCI_VENDOR_ID_INTEL, dev_id, vfdev);
Greg Rose0224d662011-10-14 02:57:14 +00005253 }
Stefan Assmannf5571472012-08-18 04:06:11 +00005254
Greg Rose0224d662011-10-14 02:57:14 +00005255 return false;
5256}
5257
5258#endif
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005259static void igb_ping_all_vfs(struct igb_adapter *adapter)
5260{
5261 struct e1000_hw *hw = &adapter->hw;
5262 u32 ping;
5263 int i;
5264
5265 for (i = 0 ; i < adapter->vfs_allocated_count; i++) {
5266 ping = E1000_PF_CONTROL_MSG;
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005267 if (adapter->vf_data[i].flags & IGB_VF_FLAG_CTS)
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005268 ping |= E1000_VT_MSGTYPE_CTS;
5269 igb_write_mbx(hw, &ping, 1, i);
5270 }
5271}
5272
Alexander Duyck7d5753f2009-10-27 23:47:16 +00005273static int igb_set_vf_promisc(struct igb_adapter *adapter, u32 *msgbuf, u32 vf)
5274{
5275 struct e1000_hw *hw = &adapter->hw;
5276 u32 vmolr = rd32(E1000_VMOLR(vf));
5277 struct vf_data_storage *vf_data = &adapter->vf_data[vf];
5278
Alexander Duyckd85b90042010-09-22 17:56:20 +00005279 vf_data->flags &= ~(IGB_VF_FLAG_UNI_PROMISC |
Alexander Duyck7d5753f2009-10-27 23:47:16 +00005280 IGB_VF_FLAG_MULTI_PROMISC);
5281 vmolr &= ~(E1000_VMOLR_ROPE | E1000_VMOLR_ROMPE | E1000_VMOLR_MPME);
5282
5283 if (*msgbuf & E1000_VF_SET_PROMISC_MULTICAST) {
5284 vmolr |= E1000_VMOLR_MPME;
Alexander Duyckd85b90042010-09-22 17:56:20 +00005285 vf_data->flags |= IGB_VF_FLAG_MULTI_PROMISC;
Alexander Duyck7d5753f2009-10-27 23:47:16 +00005286 *msgbuf &= ~E1000_VF_SET_PROMISC_MULTICAST;
5287 } else {
5288 /*
5289 * if we have hashes and we are clearing a multicast promisc
5290 * flag we need to write the hashes to the MTA as this step
5291 * was previously skipped
5292 */
5293 if (vf_data->num_vf_mc_hashes > 30) {
5294 vmolr |= E1000_VMOLR_MPME;
5295 } else if (vf_data->num_vf_mc_hashes) {
5296 int j;
5297 vmolr |= E1000_VMOLR_ROMPE;
5298 for (j = 0; j < vf_data->num_vf_mc_hashes; j++)
5299 igb_mta_set(hw, vf_data->vf_mc_hashes[j]);
5300 }
5301 }
5302
5303 wr32(E1000_VMOLR(vf), vmolr);
5304
5305 /* there are flags left unprocessed, likely not supported */
5306 if (*msgbuf & E1000_VT_MSGINFO_MASK)
5307 return -EINVAL;
5308
5309 return 0;
5310
5311}
5312
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005313static int igb_set_vf_multicasts(struct igb_adapter *adapter,
5314 u32 *msgbuf, u32 vf)
5315{
5316 int n = (msgbuf[0] & E1000_VT_MSGINFO_MASK) >> E1000_VT_MSGINFO_SHIFT;
5317 u16 *hash_list = (u16 *)&msgbuf[1];
5318 struct vf_data_storage *vf_data = &adapter->vf_data[vf];
5319 int i;
5320
Alexander Duyck7d5753f2009-10-27 23:47:16 +00005321 /* salt away the number of multicast addresses assigned
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005322 * to this VF for later use to restore when the PF multi cast
5323 * list changes
5324 */
5325 vf_data->num_vf_mc_hashes = n;
5326
Alexander Duyck7d5753f2009-10-27 23:47:16 +00005327 /* only up to 30 hash values supported */
5328 if (n > 30)
5329 n = 30;
5330
5331 /* store the hashes for later use */
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005332 for (i = 0; i < n; i++)
Joe Perchesa419aef2009-08-18 11:18:35 -07005333 vf_data->vf_mc_hashes[i] = hash_list[i];
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005334
5335 /* Flush and reset the mta with the new values */
Alexander Duyckff41f8d2009-09-03 14:48:56 +00005336 igb_set_rx_mode(adapter->netdev);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005337
5338 return 0;
5339}
5340
5341static void igb_restore_vf_multicasts(struct igb_adapter *adapter)
5342{
5343 struct e1000_hw *hw = &adapter->hw;
5344 struct vf_data_storage *vf_data;
5345 int i, j;
5346
5347 for (i = 0; i < adapter->vfs_allocated_count; i++) {
Alexander Duyck7d5753f2009-10-27 23:47:16 +00005348 u32 vmolr = rd32(E1000_VMOLR(i));
5349 vmolr &= ~(E1000_VMOLR_ROMPE | E1000_VMOLR_MPME);
5350
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005351 vf_data = &adapter->vf_data[i];
Alexander Duyck7d5753f2009-10-27 23:47:16 +00005352
5353 if ((vf_data->num_vf_mc_hashes > 30) ||
5354 (vf_data->flags & IGB_VF_FLAG_MULTI_PROMISC)) {
5355 vmolr |= E1000_VMOLR_MPME;
5356 } else if (vf_data->num_vf_mc_hashes) {
5357 vmolr |= E1000_VMOLR_ROMPE;
5358 for (j = 0; j < vf_data->num_vf_mc_hashes; j++)
5359 igb_mta_set(hw, vf_data->vf_mc_hashes[j]);
5360 }
5361 wr32(E1000_VMOLR(i), vmolr);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005362 }
5363}
5364
5365static void igb_clear_vf_vfta(struct igb_adapter *adapter, u32 vf)
5366{
5367 struct e1000_hw *hw = &adapter->hw;
5368 u32 pool_mask, reg, vid;
5369 int i;
5370
5371 pool_mask = 1 << (E1000_VLVF_POOLSEL_SHIFT + vf);
5372
5373 /* Find the vlan filter for this id */
5374 for (i = 0; i < E1000_VLVF_ARRAY_SIZE; i++) {
5375 reg = rd32(E1000_VLVF(i));
5376
5377 /* remove the vf from the pool */
5378 reg &= ~pool_mask;
5379
5380 /* if pool is empty then remove entry from vfta */
5381 if (!(reg & E1000_VLVF_POOLSEL_MASK) &&
5382 (reg & E1000_VLVF_VLANID_ENABLE)) {
5383 reg = 0;
5384 vid = reg & E1000_VLVF_VLANID_MASK;
5385 igb_vfta_set(hw, vid, false);
5386 }
5387
5388 wr32(E1000_VLVF(i), reg);
5389 }
Alexander Duyckae641bd2009-09-03 14:49:33 +00005390
5391 adapter->vf_data[vf].vlans_enabled = 0;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005392}
5393
5394static s32 igb_vlvf_set(struct igb_adapter *adapter, u32 vid, bool add, u32 vf)
5395{
5396 struct e1000_hw *hw = &adapter->hw;
5397 u32 reg, i;
5398
Alexander Duyck51466232009-10-27 23:47:35 +00005399 /* The vlvf table only exists on 82576 hardware and newer */
5400 if (hw->mac.type < e1000_82576)
5401 return -1;
5402
5403 /* we only need to do this if VMDq is enabled */
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005404 if (!adapter->vfs_allocated_count)
5405 return -1;
5406
5407 /* Find the vlan filter for this id */
5408 for (i = 0; i < E1000_VLVF_ARRAY_SIZE; i++) {
5409 reg = rd32(E1000_VLVF(i));
5410 if ((reg & E1000_VLVF_VLANID_ENABLE) &&
5411 vid == (reg & E1000_VLVF_VLANID_MASK))
5412 break;
5413 }
5414
5415 if (add) {
5416 if (i == E1000_VLVF_ARRAY_SIZE) {
5417 /* Did not find a matching VLAN ID entry that was
5418 * enabled. Search for a free filter entry, i.e.
5419 * one without the enable bit set
5420 */
5421 for (i = 0; i < E1000_VLVF_ARRAY_SIZE; i++) {
5422 reg = rd32(E1000_VLVF(i));
5423 if (!(reg & E1000_VLVF_VLANID_ENABLE))
5424 break;
5425 }
5426 }
5427 if (i < E1000_VLVF_ARRAY_SIZE) {
5428 /* Found an enabled/available entry */
5429 reg |= 1 << (E1000_VLVF_POOLSEL_SHIFT + vf);
5430
5431 /* if !enabled we need to set this up in vfta */
5432 if (!(reg & E1000_VLVF_VLANID_ENABLE)) {
Alexander Duyck51466232009-10-27 23:47:35 +00005433 /* add VID to filter table */
5434 igb_vfta_set(hw, vid, true);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005435 reg |= E1000_VLVF_VLANID_ENABLE;
5436 }
Alexander Duyckcad6d052009-03-13 20:41:37 +00005437 reg &= ~E1000_VLVF_VLANID_MASK;
5438 reg |= vid;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005439 wr32(E1000_VLVF(i), reg);
Alexander Duyckae641bd2009-09-03 14:49:33 +00005440
5441 /* do not modify RLPML for PF devices */
5442 if (vf >= adapter->vfs_allocated_count)
5443 return 0;
5444
5445 if (!adapter->vf_data[vf].vlans_enabled) {
5446 u32 size;
5447 reg = rd32(E1000_VMOLR(vf));
5448 size = reg & E1000_VMOLR_RLPML_MASK;
5449 size += 4;
5450 reg &= ~E1000_VMOLR_RLPML_MASK;
5451 reg |= size;
5452 wr32(E1000_VMOLR(vf), reg);
5453 }
Alexander Duyckae641bd2009-09-03 14:49:33 +00005454
Alexander Duyck51466232009-10-27 23:47:35 +00005455 adapter->vf_data[vf].vlans_enabled++;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005456 }
5457 } else {
5458 if (i < E1000_VLVF_ARRAY_SIZE) {
5459 /* remove vf from the pool */
5460 reg &= ~(1 << (E1000_VLVF_POOLSEL_SHIFT + vf));
5461 /* if pool is empty then remove entry from vfta */
5462 if (!(reg & E1000_VLVF_POOLSEL_MASK)) {
5463 reg = 0;
5464 igb_vfta_set(hw, vid, false);
5465 }
5466 wr32(E1000_VLVF(i), reg);
Alexander Duyckae641bd2009-09-03 14:49:33 +00005467
5468 /* do not modify RLPML for PF devices */
5469 if (vf >= adapter->vfs_allocated_count)
5470 return 0;
5471
5472 adapter->vf_data[vf].vlans_enabled--;
5473 if (!adapter->vf_data[vf].vlans_enabled) {
5474 u32 size;
5475 reg = rd32(E1000_VMOLR(vf));
5476 size = reg & E1000_VMOLR_RLPML_MASK;
5477 size -= 4;
5478 reg &= ~E1000_VMOLR_RLPML_MASK;
5479 reg |= size;
5480 wr32(E1000_VMOLR(vf), reg);
5481 }
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005482 }
5483 }
Williams, Mitch A8151d292010-02-10 01:44:24 +00005484 return 0;
5485}
5486
5487static void igb_set_vmvir(struct igb_adapter *adapter, u32 vid, u32 vf)
5488{
5489 struct e1000_hw *hw = &adapter->hw;
5490
5491 if (vid)
5492 wr32(E1000_VMVIR(vf), (vid | E1000_VMVIR_VLANA_DEFAULT));
5493 else
5494 wr32(E1000_VMVIR(vf), 0);
5495}
5496
5497static int igb_ndo_set_vf_vlan(struct net_device *netdev,
5498 int vf, u16 vlan, u8 qos)
5499{
5500 int err = 0;
5501 struct igb_adapter *adapter = netdev_priv(netdev);
5502
5503 if ((vf >= adapter->vfs_allocated_count) || (vlan > 4095) || (qos > 7))
5504 return -EINVAL;
5505 if (vlan || qos) {
5506 err = igb_vlvf_set(adapter, vlan, !!vlan, vf);
5507 if (err)
5508 goto out;
5509 igb_set_vmvir(adapter, vlan | (qos << VLAN_PRIO_SHIFT), vf);
5510 igb_set_vmolr(adapter, vf, !vlan);
5511 adapter->vf_data[vf].pf_vlan = vlan;
5512 adapter->vf_data[vf].pf_qos = qos;
5513 dev_info(&adapter->pdev->dev,
5514 "Setting VLAN %d, QOS 0x%x on VF %d\n", vlan, qos, vf);
5515 if (test_bit(__IGB_DOWN, &adapter->state)) {
5516 dev_warn(&adapter->pdev->dev,
5517 "The VF VLAN has been set,"
5518 " but the PF device is not up.\n");
5519 dev_warn(&adapter->pdev->dev,
5520 "Bring the PF device up before"
5521 " attempting to use the VF device.\n");
5522 }
5523 } else {
5524 igb_vlvf_set(adapter, adapter->vf_data[vf].pf_vlan,
5525 false, vf);
5526 igb_set_vmvir(adapter, vlan, vf);
5527 igb_set_vmolr(adapter, vf, true);
5528 adapter->vf_data[vf].pf_vlan = 0;
5529 adapter->vf_data[vf].pf_qos = 0;
5530 }
5531out:
5532 return err;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005533}
5534
5535static int igb_set_vf_vlan(struct igb_adapter *adapter, u32 *msgbuf, u32 vf)
5536{
5537 int add = (msgbuf[0] & E1000_VT_MSGINFO_MASK) >> E1000_VT_MSGINFO_SHIFT;
5538 int vid = (msgbuf[1] & E1000_VLVF_VLANID_MASK);
5539
5540 return igb_vlvf_set(adapter, vid, add, vf);
5541}
5542
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005543static inline void igb_vf_reset(struct igb_adapter *adapter, u32 vf)
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005544{
Greg Rose8fa7e0f2010-11-06 05:43:21 +00005545 /* clear flags - except flag that indicates PF has set the MAC */
5546 adapter->vf_data[vf].flags &= IGB_VF_FLAG_PF_SET_MAC;
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005547 adapter->vf_data[vf].last_nack = jiffies;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005548
5549 /* reset offloads to defaults */
Williams, Mitch A8151d292010-02-10 01:44:24 +00005550 igb_set_vmolr(adapter, vf, true);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005551
5552 /* reset vlans for device */
5553 igb_clear_vf_vfta(adapter, vf);
Williams, Mitch A8151d292010-02-10 01:44:24 +00005554 if (adapter->vf_data[vf].pf_vlan)
5555 igb_ndo_set_vf_vlan(adapter->netdev, vf,
5556 adapter->vf_data[vf].pf_vlan,
5557 adapter->vf_data[vf].pf_qos);
5558 else
5559 igb_clear_vf_vfta(adapter, vf);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005560
5561 /* reset multicast table array for vf */
5562 adapter->vf_data[vf].num_vf_mc_hashes = 0;
5563
5564 /* Flush and reset the mta with the new values */
Alexander Duyckff41f8d2009-09-03 14:48:56 +00005565 igb_set_rx_mode(adapter->netdev);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005566}
5567
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005568static void igb_vf_reset_event(struct igb_adapter *adapter, u32 vf)
5569{
5570 unsigned char *vf_mac = adapter->vf_data[vf].vf_mac_addresses;
5571
Mitch A Williams5ac6f912013-01-18 08:57:20 +00005572 /* clear mac address as we were hotplug removed/added */
Williams, Mitch A8151d292010-02-10 01:44:24 +00005573 if (!(adapter->vf_data[vf].flags & IGB_VF_FLAG_PF_SET_MAC))
Mitch A Williams5ac6f912013-01-18 08:57:20 +00005574 eth_zero_addr(vf_mac);
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005575
5576 /* process remaining reset events */
5577 igb_vf_reset(adapter, vf);
5578}
5579
5580static void igb_vf_reset_msg(struct igb_adapter *adapter, u32 vf)
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005581{
5582 struct e1000_hw *hw = &adapter->hw;
5583 unsigned char *vf_mac = adapter->vf_data[vf].vf_mac_addresses;
Alexander Duyckff41f8d2009-09-03 14:48:56 +00005584 int rar_entry = hw->mac.rar_entry_count - (vf + 1);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005585 u32 reg, msgbuf[3];
5586 u8 *addr = (u8 *)(&msgbuf[1]);
5587
5588 /* process all the same items cleared in a function level reset */
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005589 igb_vf_reset(adapter, vf);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005590
5591 /* set vf mac address */
Alexander Duyck26ad9172009-10-05 06:32:49 +00005592 igb_rar_set_qsel(adapter, vf_mac, rar_entry, vf);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005593
5594 /* enable transmit and receive for vf */
5595 reg = rd32(E1000_VFTE);
5596 wr32(E1000_VFTE, reg | (1 << vf));
5597 reg = rd32(E1000_VFRE);
5598 wr32(E1000_VFRE, reg | (1 << vf));
5599
Greg Rose8fa7e0f2010-11-06 05:43:21 +00005600 adapter->vf_data[vf].flags |= IGB_VF_FLAG_CTS;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005601
5602 /* reply to reset with ack and vf mac address */
5603 msgbuf[0] = E1000_VF_RESET | E1000_VT_MSGTYPE_ACK;
5604 memcpy(addr, vf_mac, 6);
5605 igb_write_mbx(hw, msgbuf, 3, vf);
5606}
5607
5608static int igb_set_vf_mac_addr(struct igb_adapter *adapter, u32 *msg, int vf)
5609{
Greg Rosede42edd2010-07-01 13:39:23 +00005610 /*
5611 * The VF MAC Address is stored in a packed array of bytes
5612 * starting at the second 32 bit word of the msg array
5613 */
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005614 unsigned char *addr = (char *)&msg[1];
5615 int err = -1;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005616
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005617 if (is_valid_ether_addr(addr))
5618 err = igb_set_vf_mac(adapter, vf, addr);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005619
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005620 return err;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005621}
5622
5623static void igb_rcv_ack_from_vf(struct igb_adapter *adapter, u32 vf)
5624{
5625 struct e1000_hw *hw = &adapter->hw;
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005626 struct vf_data_storage *vf_data = &adapter->vf_data[vf];
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005627 u32 msg = E1000_VT_MSGTYPE_NACK;
5628
5629 /* if device isn't clear to send it shouldn't be reading either */
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005630 if (!(vf_data->flags & IGB_VF_FLAG_CTS) &&
5631 time_after(jiffies, vf_data->last_nack + (2 * HZ))) {
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005632 igb_write_mbx(hw, &msg, 1, vf);
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005633 vf_data->last_nack = jiffies;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005634 }
5635}
5636
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005637static void igb_rcv_msg_from_vf(struct igb_adapter *adapter, u32 vf)
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005638{
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005639 struct pci_dev *pdev = adapter->pdev;
5640 u32 msgbuf[E1000_VFMAILBOX_SIZE];
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005641 struct e1000_hw *hw = &adapter->hw;
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005642 struct vf_data_storage *vf_data = &adapter->vf_data[vf];
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005643 s32 retval;
5644
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005645 retval = igb_read_mbx(hw, msgbuf, E1000_VFMAILBOX_SIZE, vf);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005646
Alexander Duyckfef45f42009-12-11 22:57:34 -08005647 if (retval) {
5648 /* if receive failed revoke VF CTS stats and restart init */
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005649 dev_err(&pdev->dev, "Error receiving message from VF\n");
Alexander Duyckfef45f42009-12-11 22:57:34 -08005650 vf_data->flags &= ~IGB_VF_FLAG_CTS;
5651 if (!time_after(jiffies, vf_data->last_nack + (2 * HZ)))
5652 return;
5653 goto out;
5654 }
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005655
5656 /* this is a message we already processed, do nothing */
5657 if (msgbuf[0] & (E1000_VT_MSGTYPE_ACK | E1000_VT_MSGTYPE_NACK))
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005658 return;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005659
5660 /*
5661 * until the vf completes a reset it should not be
5662 * allowed to start any configuration.
5663 */
5664
5665 if (msgbuf[0] == E1000_VF_RESET) {
5666 igb_vf_reset_msg(adapter, vf);
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005667 return;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005668 }
5669
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005670 if (!(vf_data->flags & IGB_VF_FLAG_CTS)) {
Alexander Duyckfef45f42009-12-11 22:57:34 -08005671 if (!time_after(jiffies, vf_data->last_nack + (2 * HZ)))
5672 return;
5673 retval = -1;
5674 goto out;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005675 }
5676
5677 switch ((msgbuf[0] & 0xFFFF)) {
5678 case E1000_VF_SET_MAC_ADDR:
Greg Rosea6b5ea32010-11-06 05:42:59 +00005679 retval = -EINVAL;
5680 if (!(vf_data->flags & IGB_VF_FLAG_PF_SET_MAC))
5681 retval = igb_set_vf_mac_addr(adapter, msgbuf, vf);
5682 else
5683 dev_warn(&pdev->dev,
5684 "VF %d attempted to override administratively "
5685 "set MAC address\nReload the VF driver to "
5686 "resume operations\n", vf);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005687 break;
Alexander Duyck7d5753f2009-10-27 23:47:16 +00005688 case E1000_VF_SET_PROMISC:
5689 retval = igb_set_vf_promisc(adapter, msgbuf, vf);
5690 break;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005691 case E1000_VF_SET_MULTICAST:
5692 retval = igb_set_vf_multicasts(adapter, msgbuf, vf);
5693 break;
5694 case E1000_VF_SET_LPE:
5695 retval = igb_set_vf_rlpml(adapter, msgbuf[1], vf);
5696 break;
5697 case E1000_VF_SET_VLAN:
Greg Rosea6b5ea32010-11-06 05:42:59 +00005698 retval = -1;
5699 if (vf_data->pf_vlan)
5700 dev_warn(&pdev->dev,
5701 "VF %d attempted to override administratively "
5702 "set VLAN tag\nReload the VF driver to "
5703 "resume operations\n", vf);
Williams, Mitch A8151d292010-02-10 01:44:24 +00005704 else
5705 retval = igb_set_vf_vlan(adapter, msgbuf, vf);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005706 break;
5707 default:
Alexander Duyck090b1792009-10-27 23:51:55 +00005708 dev_err(&pdev->dev, "Unhandled Msg %08x\n", msgbuf[0]);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005709 retval = -1;
5710 break;
5711 }
5712
Alexander Duyckfef45f42009-12-11 22:57:34 -08005713 msgbuf[0] |= E1000_VT_MSGTYPE_CTS;
5714out:
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005715 /* notify the VF of the results of what it sent us */
5716 if (retval)
5717 msgbuf[0] |= E1000_VT_MSGTYPE_NACK;
5718 else
5719 msgbuf[0] |= E1000_VT_MSGTYPE_ACK;
5720
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005721 igb_write_mbx(hw, msgbuf, 1, vf);
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005722}
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005723
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005724static void igb_msg_task(struct igb_adapter *adapter)
5725{
5726 struct e1000_hw *hw = &adapter->hw;
5727 u32 vf;
5728
5729 for (vf = 0; vf < adapter->vfs_allocated_count; vf++) {
5730 /* process any reset requests */
5731 if (!igb_check_for_rst(hw, vf))
5732 igb_vf_reset_event(adapter, vf);
5733
5734 /* process any messages pending */
5735 if (!igb_check_for_msg(hw, vf))
5736 igb_rcv_msg_from_vf(adapter, vf);
5737
5738 /* process any acks */
5739 if (!igb_check_for_ack(hw, vf))
5740 igb_rcv_ack_from_vf(adapter, vf);
5741 }
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005742}
5743
Auke Kok9d5c8242008-01-24 02:22:38 -08005744/**
Alexander Duyck68d480c2009-10-05 06:33:08 +00005745 * igb_set_uta - Set unicast filter table address
5746 * @adapter: board private structure
5747 *
5748 * The unicast table address is a register array of 32-bit registers.
5749 * The table is meant to be used in a way similar to how the MTA is used
5750 * however due to certain limitations in the hardware it is necessary to
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005751 * set all the hash bits to 1 and use the VMOLR ROPE bit as a promiscuous
5752 * enable bit to allow vlan tag stripping when promiscuous mode is enabled
Alexander Duyck68d480c2009-10-05 06:33:08 +00005753 **/
5754static void igb_set_uta(struct igb_adapter *adapter)
5755{
5756 struct e1000_hw *hw = &adapter->hw;
5757 int i;
5758
5759 /* The UTA table only exists on 82576 hardware and newer */
5760 if (hw->mac.type < e1000_82576)
5761 return;
5762
5763 /* we only need to do this if VMDq is enabled */
5764 if (!adapter->vfs_allocated_count)
5765 return;
5766
5767 for (i = 0; i < hw->mac.uta_reg_count; i++)
5768 array_wr32(E1000_UTA, i, ~0);
5769}
5770
5771/**
Auke Kok9d5c8242008-01-24 02:22:38 -08005772 * igb_intr_msi - Interrupt Handler
5773 * @irq: interrupt number
5774 * @data: pointer to a network interface device structure
5775 **/
5776static irqreturn_t igb_intr_msi(int irq, void *data)
5777{
Alexander Duyck047e0032009-10-27 15:49:27 +00005778 struct igb_adapter *adapter = data;
5779 struct igb_q_vector *q_vector = adapter->q_vector[0];
Auke Kok9d5c8242008-01-24 02:22:38 -08005780 struct e1000_hw *hw = &adapter->hw;
5781 /* read ICR disables interrupts using IAM */
5782 u32 icr = rd32(E1000_ICR);
5783
Alexander Duyck047e0032009-10-27 15:49:27 +00005784 igb_write_itr(q_vector);
Auke Kok9d5c8242008-01-24 02:22:38 -08005785
Alexander Duyck7f081d42010-01-07 17:41:00 +00005786 if (icr & E1000_ICR_DRSTA)
5787 schedule_work(&adapter->reset_task);
5788
Alexander Duyck047e0032009-10-27 15:49:27 +00005789 if (icr & E1000_ICR_DOUTSYNC) {
Alexander Duyckdda0e082009-02-06 23:19:08 +00005790 /* HW is reporting DMA is out of sync */
5791 adapter->stats.doosync++;
5792 }
5793
Auke Kok9d5c8242008-01-24 02:22:38 -08005794 if (icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) {
5795 hw->mac.get_link_status = 1;
5796 if (!test_bit(__IGB_DOWN, &adapter->state))
5797 mod_timer(&adapter->watchdog_timer, jiffies + 1);
5798 }
5799
Matthew Vick1f6e8172012-08-18 07:26:33 +00005800 if (icr & E1000_ICR_TS) {
5801 u32 tsicr = rd32(E1000_TSICR);
5802
5803 if (tsicr & E1000_TSICR_TXTS) {
5804 /* acknowledge the interrupt */
5805 wr32(E1000_TSICR, E1000_TSICR_TXTS);
5806 /* retrieve hardware timestamp */
5807 schedule_work(&adapter->ptp_tx_work);
5808 }
5809 }
Matthew Vick1f6e8172012-08-18 07:26:33 +00005810
Alexander Duyck047e0032009-10-27 15:49:27 +00005811 napi_schedule(&q_vector->napi);
Auke Kok9d5c8242008-01-24 02:22:38 -08005812
5813 return IRQ_HANDLED;
5814}
5815
5816/**
Alexander Duyck4a3c6432009-02-06 23:20:49 +00005817 * igb_intr - Legacy Interrupt Handler
Auke Kok9d5c8242008-01-24 02:22:38 -08005818 * @irq: interrupt number
5819 * @data: pointer to a network interface device structure
5820 **/
5821static irqreturn_t igb_intr(int irq, void *data)
5822{
Alexander Duyck047e0032009-10-27 15:49:27 +00005823 struct igb_adapter *adapter = data;
5824 struct igb_q_vector *q_vector = adapter->q_vector[0];
Auke Kok9d5c8242008-01-24 02:22:38 -08005825 struct e1000_hw *hw = &adapter->hw;
5826 /* Interrupt Auto-Mask...upon reading ICR, interrupts are masked. No
5827 * need for the IMC write */
5828 u32 icr = rd32(E1000_ICR);
Auke Kok9d5c8242008-01-24 02:22:38 -08005829
5830 /* IMS will not auto-mask if INT_ASSERTED is not set, and if it is
5831 * not set, then the adapter didn't send an interrupt */
5832 if (!(icr & E1000_ICR_INT_ASSERTED))
5833 return IRQ_NONE;
5834
Alexander Duyck0ba82992011-08-26 07:45:47 +00005835 igb_write_itr(q_vector);
5836
Alexander Duyck7f081d42010-01-07 17:41:00 +00005837 if (icr & E1000_ICR_DRSTA)
5838 schedule_work(&adapter->reset_task);
5839
Alexander Duyck047e0032009-10-27 15:49:27 +00005840 if (icr & E1000_ICR_DOUTSYNC) {
Alexander Duyckdda0e082009-02-06 23:19:08 +00005841 /* HW is reporting DMA is out of sync */
5842 adapter->stats.doosync++;
5843 }
5844
Auke Kok9d5c8242008-01-24 02:22:38 -08005845 if (icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) {
5846 hw->mac.get_link_status = 1;
5847 /* guard against interrupt when we're going down */
5848 if (!test_bit(__IGB_DOWN, &adapter->state))
5849 mod_timer(&adapter->watchdog_timer, jiffies + 1);
5850 }
5851
Matthew Vick1f6e8172012-08-18 07:26:33 +00005852 if (icr & E1000_ICR_TS) {
5853 u32 tsicr = rd32(E1000_TSICR);
5854
5855 if (tsicr & E1000_TSICR_TXTS) {
5856 /* acknowledge the interrupt */
5857 wr32(E1000_TSICR, E1000_TSICR_TXTS);
5858 /* retrieve hardware timestamp */
5859 schedule_work(&adapter->ptp_tx_work);
5860 }
5861 }
Matthew Vick1f6e8172012-08-18 07:26:33 +00005862
Alexander Duyck047e0032009-10-27 15:49:27 +00005863 napi_schedule(&q_vector->napi);
Auke Kok9d5c8242008-01-24 02:22:38 -08005864
5865 return IRQ_HANDLED;
5866}
5867
Stephen Hemmingerc50b52a2012-01-18 22:13:26 +00005868static void igb_ring_irq_enable(struct igb_q_vector *q_vector)
Alexander Duyck46544252009-02-19 20:39:04 -08005869{
Alexander Duyck047e0032009-10-27 15:49:27 +00005870 struct igb_adapter *adapter = q_vector->adapter;
Alexander Duyck46544252009-02-19 20:39:04 -08005871 struct e1000_hw *hw = &adapter->hw;
5872
Alexander Duyck0ba82992011-08-26 07:45:47 +00005873 if ((q_vector->rx.ring && (adapter->rx_itr_setting & 3)) ||
5874 (!q_vector->rx.ring && (adapter->tx_itr_setting & 3))) {
5875 if ((adapter->num_q_vectors == 1) && !adapter->vf_data)
5876 igb_set_itr(q_vector);
Alexander Duyck46544252009-02-19 20:39:04 -08005877 else
Alexander Duyck047e0032009-10-27 15:49:27 +00005878 igb_update_ring_itr(q_vector);
Alexander Duyck46544252009-02-19 20:39:04 -08005879 }
5880
5881 if (!test_bit(__IGB_DOWN, &adapter->state)) {
5882 if (adapter->msix_entries)
Alexander Duyck047e0032009-10-27 15:49:27 +00005883 wr32(E1000_EIMS, q_vector->eims_value);
Alexander Duyck46544252009-02-19 20:39:04 -08005884 else
5885 igb_irq_enable(adapter);
5886 }
5887}
5888
Auke Kok9d5c8242008-01-24 02:22:38 -08005889/**
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07005890 * igb_poll - NAPI Rx polling callback
5891 * @napi: napi polling structure
5892 * @budget: count of how many packets we should handle
Auke Kok9d5c8242008-01-24 02:22:38 -08005893 **/
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07005894static int igb_poll(struct napi_struct *napi, int budget)
Auke Kok9d5c8242008-01-24 02:22:38 -08005895{
Alexander Duyck047e0032009-10-27 15:49:27 +00005896 struct igb_q_vector *q_vector = container_of(napi,
5897 struct igb_q_vector,
5898 napi);
Alexander Duyck16eb8812011-08-26 07:43:54 +00005899 bool clean_complete = true;
Auke Kok9d5c8242008-01-24 02:22:38 -08005900
Jeff Kirsher421e02f2008-10-17 11:08:31 -07005901#ifdef CONFIG_IGB_DCA
Alexander Duyck047e0032009-10-27 15:49:27 +00005902 if (q_vector->adapter->flags & IGB_FLAG_DCA_ENABLED)
5903 igb_update_dca(q_vector);
Jeb Cramerfe4506b2008-07-08 15:07:55 -07005904#endif
Alexander Duyck0ba82992011-08-26 07:45:47 +00005905 if (q_vector->tx.ring)
Alexander Duyck13fde972011-10-05 13:35:24 +00005906 clean_complete = igb_clean_tx_irq(q_vector);
Auke Kok9d5c8242008-01-24 02:22:38 -08005907
Alexander Duyck0ba82992011-08-26 07:45:47 +00005908 if (q_vector->rx.ring)
Alexander Duyckcd392f52011-08-26 07:43:59 +00005909 clean_complete &= igb_clean_rx_irq(q_vector, budget);
Alexander Duyck047e0032009-10-27 15:49:27 +00005910
Alexander Duyck16eb8812011-08-26 07:43:54 +00005911 /* If all work not completed, return budget and keep polling */
5912 if (!clean_complete)
5913 return budget;
Auke Kok9d5c8242008-01-24 02:22:38 -08005914
Alexander Duyck46544252009-02-19 20:39:04 -08005915 /* If not enough Rx work done, exit the polling mode */
Alexander Duyck16eb8812011-08-26 07:43:54 +00005916 napi_complete(napi);
5917 igb_ring_irq_enable(q_vector);
Alexander Duyck46544252009-02-19 20:39:04 -08005918
Alexander Duyck16eb8812011-08-26 07:43:54 +00005919 return 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08005920}
Al Viro6d8126f2008-03-16 22:23:24 +00005921
Patrick Ohly33af6bc2009-02-12 05:03:43 +00005922/**
Auke Kok9d5c8242008-01-24 02:22:38 -08005923 * igb_clean_tx_irq - Reclaim resources after transmit completes
Alexander Duyck047e0032009-10-27 15:49:27 +00005924 * @q_vector: pointer to q_vector containing needed info
Ben Hutchings49ce9c22012-07-10 10:56:00 +00005925 *
Auke Kok9d5c8242008-01-24 02:22:38 -08005926 * returns true if ring is completely cleaned
5927 **/
Alexander Duyck047e0032009-10-27 15:49:27 +00005928static bool igb_clean_tx_irq(struct igb_q_vector *q_vector)
Auke Kok9d5c8242008-01-24 02:22:38 -08005929{
Alexander Duyck047e0032009-10-27 15:49:27 +00005930 struct igb_adapter *adapter = q_vector->adapter;
Alexander Duyck0ba82992011-08-26 07:45:47 +00005931 struct igb_ring *tx_ring = q_vector->tx.ring;
Alexander Duyck06034642011-08-26 07:44:22 +00005932 struct igb_tx_buffer *tx_buffer;
Alexander Duyckf4128782012-09-13 06:28:01 +00005933 union e1000_adv_tx_desc *tx_desc;
Auke Kok9d5c8242008-01-24 02:22:38 -08005934 unsigned int total_bytes = 0, total_packets = 0;
Alexander Duyck0ba82992011-08-26 07:45:47 +00005935 unsigned int budget = q_vector->tx.work_limit;
Alexander Duyck8542db02011-08-26 07:44:43 +00005936 unsigned int i = tx_ring->next_to_clean;
Auke Kok9d5c8242008-01-24 02:22:38 -08005937
Alexander Duyck13fde972011-10-05 13:35:24 +00005938 if (test_bit(__IGB_DOWN, &adapter->state))
5939 return true;
Alexander Duyck0e014cb2008-12-26 01:33:18 -08005940
Alexander Duyck06034642011-08-26 07:44:22 +00005941 tx_buffer = &tx_ring->tx_buffer_info[i];
Alexander Duyck13fde972011-10-05 13:35:24 +00005942 tx_desc = IGB_TX_DESC(tx_ring, i);
Alexander Duyck8542db02011-08-26 07:44:43 +00005943 i -= tx_ring->count;
Auke Kok9d5c8242008-01-24 02:22:38 -08005944
Alexander Duyckf4128782012-09-13 06:28:01 +00005945 do {
5946 union e1000_adv_tx_desc *eop_desc = tx_buffer->next_to_watch;
Alexander Duyck8542db02011-08-26 07:44:43 +00005947
5948 /* if next_to_watch is not set then there is no work pending */
5949 if (!eop_desc)
5950 break;
Alexander Duyck13fde972011-10-05 13:35:24 +00005951
Alexander Duyckf4128782012-09-13 06:28:01 +00005952 /* prevent any other reads prior to eop_desc */
Alexander Duyck70d289b2013-01-08 07:01:03 +00005953 read_barrier_depends();
Alexander Duyckf4128782012-09-13 06:28:01 +00005954
Alexander Duyck13fde972011-10-05 13:35:24 +00005955 /* if DD is not set pending work has not been completed */
5956 if (!(eop_desc->wb.status & cpu_to_le32(E1000_TXD_STAT_DD)))
5957 break;
5958
Alexander Duyck8542db02011-08-26 07:44:43 +00005959 /* clear next_to_watch to prevent false hangs */
5960 tx_buffer->next_to_watch = NULL;
Alexander Duyck13fde972011-10-05 13:35:24 +00005961
Alexander Duyckebe42d12011-08-26 07:45:09 +00005962 /* update the statistics for this packet */
5963 total_bytes += tx_buffer->bytecount;
5964 total_packets += tx_buffer->gso_segs;
Alexander Duyck13fde972011-10-05 13:35:24 +00005965
Alexander Duyckebe42d12011-08-26 07:45:09 +00005966 /* free the skb */
5967 dev_kfree_skb_any(tx_buffer->skb);
Alexander Duyckebe42d12011-08-26 07:45:09 +00005968
5969 /* unmap skb header data */
5970 dma_unmap_single(tx_ring->dev,
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00005971 dma_unmap_addr(tx_buffer, dma),
5972 dma_unmap_len(tx_buffer, len),
Alexander Duyckebe42d12011-08-26 07:45:09 +00005973 DMA_TO_DEVICE);
5974
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00005975 /* clear tx_buffer data */
5976 tx_buffer->skb = NULL;
5977 dma_unmap_len_set(tx_buffer, len, 0);
5978
Alexander Duyckebe42d12011-08-26 07:45:09 +00005979 /* clear last DMA location and unmap remaining buffers */
5980 while (tx_desc != eop_desc) {
Alexander Duyck13fde972011-10-05 13:35:24 +00005981 tx_buffer++;
5982 tx_desc++;
Auke Kok9d5c8242008-01-24 02:22:38 -08005983 i++;
Alexander Duyck8542db02011-08-26 07:44:43 +00005984 if (unlikely(!i)) {
5985 i -= tx_ring->count;
Alexander Duyck06034642011-08-26 07:44:22 +00005986 tx_buffer = tx_ring->tx_buffer_info;
Alexander Duyck13fde972011-10-05 13:35:24 +00005987 tx_desc = IGB_TX_DESC(tx_ring, 0);
5988 }
Alexander Duyckebe42d12011-08-26 07:45:09 +00005989
5990 /* unmap any remaining paged data */
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00005991 if (dma_unmap_len(tx_buffer, len)) {
Alexander Duyckebe42d12011-08-26 07:45:09 +00005992 dma_unmap_page(tx_ring->dev,
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00005993 dma_unmap_addr(tx_buffer, dma),
5994 dma_unmap_len(tx_buffer, len),
Alexander Duyckebe42d12011-08-26 07:45:09 +00005995 DMA_TO_DEVICE);
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00005996 dma_unmap_len_set(tx_buffer, len, 0);
Alexander Duyckebe42d12011-08-26 07:45:09 +00005997 }
5998 }
5999
Alexander Duyckebe42d12011-08-26 07:45:09 +00006000 /* move us one more past the eop_desc for start of next pkt */
6001 tx_buffer++;
6002 tx_desc++;
6003 i++;
6004 if (unlikely(!i)) {
6005 i -= tx_ring->count;
6006 tx_buffer = tx_ring->tx_buffer_info;
6007 tx_desc = IGB_TX_DESC(tx_ring, 0);
6008 }
Alexander Duyckf4128782012-09-13 06:28:01 +00006009
6010 /* issue prefetch for next Tx descriptor */
6011 prefetch(tx_desc);
6012
6013 /* update budget accounting */
6014 budget--;
6015 } while (likely(budget));
Alexander Duyck0e014cb2008-12-26 01:33:18 -08006016
Eric Dumazetbdbc0632012-01-04 20:23:36 +00006017 netdev_tx_completed_queue(txring_txq(tx_ring),
6018 total_packets, total_bytes);
Alexander Duyck8542db02011-08-26 07:44:43 +00006019 i += tx_ring->count;
Auke Kok9d5c8242008-01-24 02:22:38 -08006020 tx_ring->next_to_clean = i;
Alexander Duyck13fde972011-10-05 13:35:24 +00006021 u64_stats_update_begin(&tx_ring->tx_syncp);
6022 tx_ring->tx_stats.bytes += total_bytes;
6023 tx_ring->tx_stats.packets += total_packets;
6024 u64_stats_update_end(&tx_ring->tx_syncp);
Alexander Duyck0ba82992011-08-26 07:45:47 +00006025 q_vector->tx.total_bytes += total_bytes;
6026 q_vector->tx.total_packets += total_packets;
Auke Kok9d5c8242008-01-24 02:22:38 -08006027
Alexander Duyck6d095fa2011-08-26 07:46:19 +00006028 if (test_bit(IGB_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags)) {
Alexander Duyck13fde972011-10-05 13:35:24 +00006029 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck13fde972011-10-05 13:35:24 +00006030
Auke Kok9d5c8242008-01-24 02:22:38 -08006031 /* Detect a transmit hang in hardware, this serializes the
6032 * check with the clearing of time_stamp and movement of i */
Alexander Duyck6d095fa2011-08-26 07:46:19 +00006033 clear_bit(IGB_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags);
Alexander Duyckf4128782012-09-13 06:28:01 +00006034 if (tx_buffer->next_to_watch &&
Alexander Duyck8542db02011-08-26 07:44:43 +00006035 time_after(jiffies, tx_buffer->time_stamp +
Joe Perches8e95a202009-12-03 07:58:21 +00006036 (adapter->tx_timeout_factor * HZ)) &&
6037 !(rd32(E1000_STATUS) & E1000_STATUS_TXOFF)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08006038
Auke Kok9d5c8242008-01-24 02:22:38 -08006039 /* detected Tx unit hang */
Alexander Duyck59d71982010-04-27 13:09:25 +00006040 dev_err(tx_ring->dev,
Auke Kok9d5c8242008-01-24 02:22:38 -08006041 "Detected Tx Unit Hang\n"
Alexander Duyck2d064c02008-07-08 15:10:12 -07006042 " Tx Queue <%d>\n"
Auke Kok9d5c8242008-01-24 02:22:38 -08006043 " TDH <%x>\n"
6044 " TDT <%x>\n"
6045 " next_to_use <%x>\n"
6046 " next_to_clean <%x>\n"
Auke Kok9d5c8242008-01-24 02:22:38 -08006047 "buffer_info[next_to_clean]\n"
6048 " time_stamp <%lx>\n"
Alexander Duyck8542db02011-08-26 07:44:43 +00006049 " next_to_watch <%p>\n"
Auke Kok9d5c8242008-01-24 02:22:38 -08006050 " jiffies <%lx>\n"
6051 " desc.status <%x>\n",
Alexander Duyck2d064c02008-07-08 15:10:12 -07006052 tx_ring->queue_index,
Alexander Duyck238ac812011-08-26 07:43:48 +00006053 rd32(E1000_TDH(tx_ring->reg_idx)),
Alexander Duyckfce99e32009-10-27 15:51:27 +00006054 readl(tx_ring->tail),
Auke Kok9d5c8242008-01-24 02:22:38 -08006055 tx_ring->next_to_use,
6056 tx_ring->next_to_clean,
Alexander Duyck8542db02011-08-26 07:44:43 +00006057 tx_buffer->time_stamp,
Alexander Duyckf4128782012-09-13 06:28:01 +00006058 tx_buffer->next_to_watch,
Auke Kok9d5c8242008-01-24 02:22:38 -08006059 jiffies,
Alexander Duyckf4128782012-09-13 06:28:01 +00006060 tx_buffer->next_to_watch->wb.status);
Alexander Duyck13fde972011-10-05 13:35:24 +00006061 netif_stop_subqueue(tx_ring->netdev,
6062 tx_ring->queue_index);
6063
6064 /* we are about to reset, no point in enabling stuff */
6065 return true;
Auke Kok9d5c8242008-01-24 02:22:38 -08006066 }
6067 }
Alexander Duyck13fde972011-10-05 13:35:24 +00006068
Alexander Duyck21ba6fe2013-02-09 04:27:48 +00006069#define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
Alexander Duyck13fde972011-10-05 13:35:24 +00006070 if (unlikely(total_packets &&
6071 netif_carrier_ok(tx_ring->netdev) &&
Alexander Duyck21ba6fe2013-02-09 04:27:48 +00006072 igb_desc_unused(tx_ring) >= TX_WAKE_THRESHOLD)) {
Alexander Duyck13fde972011-10-05 13:35:24 +00006073 /* Make sure that anybody stopping the queue after this
6074 * sees the new next_to_clean.
6075 */
6076 smp_mb();
6077 if (__netif_subqueue_stopped(tx_ring->netdev,
6078 tx_ring->queue_index) &&
6079 !(test_bit(__IGB_DOWN, &adapter->state))) {
6080 netif_wake_subqueue(tx_ring->netdev,
6081 tx_ring->queue_index);
6082
6083 u64_stats_update_begin(&tx_ring->tx_syncp);
6084 tx_ring->tx_stats.restart_queue++;
6085 u64_stats_update_end(&tx_ring->tx_syncp);
6086 }
6087 }
6088
6089 return !!budget;
Auke Kok9d5c8242008-01-24 02:22:38 -08006090}
6091
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006092/**
6093 * igb_reuse_rx_page - page flip buffer and store it back on the ring
6094 * @rx_ring: rx descriptor ring to store buffers on
6095 * @old_buff: donor buffer to have page reused
6096 *
6097 * Synchronizes page for reuse by the adapter
6098 **/
6099static void igb_reuse_rx_page(struct igb_ring *rx_ring,
6100 struct igb_rx_buffer *old_buff)
6101{
6102 struct igb_rx_buffer *new_buff;
6103 u16 nta = rx_ring->next_to_alloc;
6104
6105 new_buff = &rx_ring->rx_buffer_info[nta];
6106
6107 /* update, and store next to alloc */
6108 nta++;
6109 rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0;
6110
6111 /* transfer page from old buffer to new buffer */
6112 memcpy(new_buff, old_buff, sizeof(struct igb_rx_buffer));
6113
6114 /* sync the buffer for use by the device */
6115 dma_sync_single_range_for_device(rx_ring->dev, old_buff->dma,
6116 old_buff->page_offset,
Alexander Duyckde78d1f2012-09-25 00:31:12 +00006117 IGB_RX_BUFSZ,
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006118 DMA_FROM_DEVICE);
6119}
6120
Alexander Duyck74e238e2013-02-02 05:07:11 +00006121static bool igb_can_reuse_rx_page(struct igb_rx_buffer *rx_buffer,
6122 struct page *page,
6123 unsigned int truesize)
6124{
6125 /* avoid re-using remote pages */
6126 if (unlikely(page_to_nid(page) != numa_node_id()))
6127 return false;
6128
6129#if (PAGE_SIZE < 8192)
6130 /* if we are only owner of page we can reuse it */
6131 if (unlikely(page_count(page) != 1))
6132 return false;
6133
6134 /* flip page offset to other buffer */
6135 rx_buffer->page_offset ^= IGB_RX_BUFSZ;
6136
6137 /* since we are the only owner of the page and we need to
6138 * increment it, just set the value to 2 in order to avoid
6139 * an unnecessary locked operation
6140 */
6141 atomic_set(&page->_count, 2);
6142#else
6143 /* move offset up to the next cache line */
6144 rx_buffer->page_offset += truesize;
6145
6146 if (rx_buffer->page_offset > (PAGE_SIZE - IGB_RX_BUFSZ))
6147 return false;
6148
6149 /* bump ref count on page before it is given to the stack */
6150 get_page(page);
6151#endif
6152
6153 return true;
6154}
6155
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006156/**
6157 * igb_add_rx_frag - Add contents of Rx buffer to sk_buff
6158 * @rx_ring: rx descriptor ring to transact packets on
6159 * @rx_buffer: buffer containing page to add
6160 * @rx_desc: descriptor containing length of buffer written by hardware
6161 * @skb: sk_buff to place the data into
6162 *
6163 * This function will add the data contained in rx_buffer->page to the skb.
6164 * This is done either through a direct copy if the data in the buffer is
6165 * less than the skb header size, otherwise it will just attach the page as
6166 * a frag to the skb.
6167 *
6168 * The function will then update the page offset if necessary and return
6169 * true if the buffer can be reused by the adapter.
6170 **/
6171static bool igb_add_rx_frag(struct igb_ring *rx_ring,
6172 struct igb_rx_buffer *rx_buffer,
6173 union e1000_adv_rx_desc *rx_desc,
6174 struct sk_buff *skb)
6175{
6176 struct page *page = rx_buffer->page;
6177 unsigned int size = le16_to_cpu(rx_desc->wb.upper.length);
Alexander Duyck74e238e2013-02-02 05:07:11 +00006178#if (PAGE_SIZE < 8192)
6179 unsigned int truesize = IGB_RX_BUFSZ;
6180#else
6181 unsigned int truesize = ALIGN(size, L1_CACHE_BYTES);
6182#endif
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006183
6184 if ((size <= IGB_RX_HDR_LEN) && !skb_is_nonlinear(skb)) {
6185 unsigned char *va = page_address(page) + rx_buffer->page_offset;
6186
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006187 if (igb_test_staterr(rx_desc, E1000_RXDADV_STAT_TSIP)) {
6188 igb_ptp_rx_pktstamp(rx_ring->q_vector, va, skb);
6189 va += IGB_TS_HDR_LEN;
6190 size -= IGB_TS_HDR_LEN;
6191 }
6192
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006193 memcpy(__skb_put(skb, size), va, ALIGN(size, sizeof(long)));
6194
6195 /* we can reuse buffer as-is, just make sure it is local */
6196 if (likely(page_to_nid(page) == numa_node_id()))
6197 return true;
6198
6199 /* this page cannot be reused so discard it */
6200 put_page(page);
6201 return false;
6202 }
6203
6204 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page,
Alexander Duyck74e238e2013-02-02 05:07:11 +00006205 rx_buffer->page_offset, size, truesize);
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006206
Alexander Duyck74e238e2013-02-02 05:07:11 +00006207 return igb_can_reuse_rx_page(rx_buffer, page, truesize);
6208}
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006209
Alexander Duyck74e238e2013-02-02 05:07:11 +00006210static struct sk_buff *igb_build_rx_buffer(struct igb_ring *rx_ring,
6211 union e1000_adv_rx_desc *rx_desc)
6212{
6213 struct igb_rx_buffer *rx_buffer;
6214 struct sk_buff *skb;
6215 struct page *page;
6216 void *page_addr;
6217 unsigned int size = le16_to_cpu(rx_desc->wb.upper.length);
Alexander Duyckde78d1f2012-09-25 00:31:12 +00006218#if (PAGE_SIZE < 8192)
Alexander Duyck74e238e2013-02-02 05:07:11 +00006219 unsigned int truesize = IGB_RX_BUFSZ;
Alexander Duyckde78d1f2012-09-25 00:31:12 +00006220#else
Alexander Duyck74e238e2013-02-02 05:07:11 +00006221 unsigned int truesize = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) +
6222 SKB_DATA_ALIGN(NET_SKB_PAD +
6223 NET_IP_ALIGN +
6224 size);
Alexander Duyckde78d1f2012-09-25 00:31:12 +00006225#endif
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006226
Alexander Duyck74e238e2013-02-02 05:07:11 +00006227 /* If we spanned a buffer we have a huge mess so test for it */
6228 BUG_ON(unlikely(!igb_test_staterr(rx_desc, E1000_RXD_STAT_EOP)));
6229
Alexander Duyck74e238e2013-02-02 05:07:11 +00006230 rx_buffer = &rx_ring->rx_buffer_info[rx_ring->next_to_clean];
6231 page = rx_buffer->page;
6232 prefetchw(page);
6233
6234 page_addr = page_address(page) + rx_buffer->page_offset;
6235
6236 /* prefetch first cache line of first page */
6237 prefetch(page_addr + NET_SKB_PAD + NET_IP_ALIGN);
6238#if L1_CACHE_BYTES < 128
6239 prefetch(page_addr + L1_CACHE_BYTES + NET_SKB_PAD + NET_IP_ALIGN);
6240#endif
6241
6242 /* build an skb to around the page buffer */
6243 skb = build_skb(page_addr, truesize);
6244 if (unlikely(!skb)) {
6245 rx_ring->rx_stats.alloc_failed++;
6246 return NULL;
6247 }
6248
6249 /* we are reusing so sync this buffer for CPU use */
6250 dma_sync_single_range_for_cpu(rx_ring->dev,
6251 rx_buffer->dma,
6252 rx_buffer->page_offset,
6253 IGB_RX_BUFSZ,
6254 DMA_FROM_DEVICE);
6255
6256 /* update pointers within the skb to store the data */
6257 skb_reserve(skb, NET_IP_ALIGN + NET_SKB_PAD);
6258 __skb_put(skb, size);
6259
6260 /* pull timestamp out of packet data */
6261 if (igb_test_staterr(rx_desc, E1000_RXDADV_STAT_TSIP)) {
6262 igb_ptp_rx_pktstamp(rx_ring->q_vector, skb->data, skb);
6263 __skb_pull(skb, IGB_TS_HDR_LEN);
6264 }
6265
6266 if (igb_can_reuse_rx_page(rx_buffer, page, truesize)) {
6267 /* hand second half of page back to the ring */
6268 igb_reuse_rx_page(rx_ring, rx_buffer);
6269 } else {
6270 /* we are not reusing the buffer so unmap it */
6271 dma_unmap_page(rx_ring->dev, rx_buffer->dma,
6272 PAGE_SIZE, DMA_FROM_DEVICE);
6273 }
6274
6275 /* clear contents of buffer_info */
6276 rx_buffer->dma = 0;
6277 rx_buffer->page = NULL;
6278
6279 return skb;
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006280}
6281
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006282static struct sk_buff *igb_fetch_rx_buffer(struct igb_ring *rx_ring,
6283 union e1000_adv_rx_desc *rx_desc,
6284 struct sk_buff *skb)
6285{
6286 struct igb_rx_buffer *rx_buffer;
6287 struct page *page;
6288
6289 rx_buffer = &rx_ring->rx_buffer_info[rx_ring->next_to_clean];
6290
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006291 page = rx_buffer->page;
6292 prefetchw(page);
6293
6294 if (likely(!skb)) {
6295 void *page_addr = page_address(page) +
6296 rx_buffer->page_offset;
6297
6298 /* prefetch first cache line of first page */
6299 prefetch(page_addr);
6300#if L1_CACHE_BYTES < 128
6301 prefetch(page_addr + L1_CACHE_BYTES);
6302#endif
6303
6304 /* allocate a skb to store the frags */
6305 skb = netdev_alloc_skb_ip_align(rx_ring->netdev,
6306 IGB_RX_HDR_LEN);
6307 if (unlikely(!skb)) {
6308 rx_ring->rx_stats.alloc_failed++;
6309 return NULL;
6310 }
6311
6312 /*
6313 * we will be copying header into skb->data in
6314 * pskb_may_pull so it is in our interest to prefetch
6315 * it now to avoid a possible cache miss
6316 */
6317 prefetchw(skb->data);
6318 }
6319
6320 /* we are reusing so sync this buffer for CPU use */
6321 dma_sync_single_range_for_cpu(rx_ring->dev,
6322 rx_buffer->dma,
6323 rx_buffer->page_offset,
Alexander Duyckde78d1f2012-09-25 00:31:12 +00006324 IGB_RX_BUFSZ,
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006325 DMA_FROM_DEVICE);
6326
6327 /* pull page into skb */
6328 if (igb_add_rx_frag(rx_ring, rx_buffer, rx_desc, skb)) {
6329 /* hand second half of page back to the ring */
6330 igb_reuse_rx_page(rx_ring, rx_buffer);
6331 } else {
6332 /* we are not reusing the buffer so unmap it */
6333 dma_unmap_page(rx_ring->dev, rx_buffer->dma,
6334 PAGE_SIZE, DMA_FROM_DEVICE);
6335 }
6336
6337 /* clear contents of rx_buffer */
6338 rx_buffer->page = NULL;
6339
6340 return skb;
6341}
6342
Alexander Duyckcd392f52011-08-26 07:43:59 +00006343static inline void igb_rx_checksum(struct igb_ring *ring,
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00006344 union e1000_adv_rx_desc *rx_desc,
6345 struct sk_buff *skb)
Auke Kok9d5c8242008-01-24 02:22:38 -08006346{
Eric Dumazetbc8acf22010-09-02 13:07:41 -07006347 skb_checksum_none_assert(skb);
Auke Kok9d5c8242008-01-24 02:22:38 -08006348
Alexander Duyck294e7d72011-08-26 07:45:57 +00006349 /* Ignore Checksum bit is set */
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00006350 if (igb_test_staterr(rx_desc, E1000_RXD_STAT_IXSM))
Alexander Duyck294e7d72011-08-26 07:45:57 +00006351 return;
6352
6353 /* Rx checksum disabled via ethtool */
6354 if (!(ring->netdev->features & NETIF_F_RXCSUM))
Auke Kok9d5c8242008-01-24 02:22:38 -08006355 return;
Alexander Duyck85ad76b2009-10-27 15:52:46 +00006356
Auke Kok9d5c8242008-01-24 02:22:38 -08006357 /* TCP/UDP checksum error bit is set */
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00006358 if (igb_test_staterr(rx_desc,
6359 E1000_RXDEXT_STATERR_TCPE |
6360 E1000_RXDEXT_STATERR_IPE)) {
Jesse Brandeburgb9473562009-04-27 22:36:13 +00006361 /*
6362 * work around errata with sctp packets where the TCPE aka
6363 * L4E bit is set incorrectly on 64 byte (60 byte w/o crc)
6364 * packets, (aka let the stack check the crc32c)
6365 */
Alexander Duyck866cff02011-08-26 07:45:36 +00006366 if (!((skb->len == 60) &&
6367 test_bit(IGB_RING_FLAG_RX_SCTP_CSUM, &ring->flags))) {
Eric Dumazet12dcd862010-10-15 17:27:10 +00006368 u64_stats_update_begin(&ring->rx_syncp);
Alexander Duyck04a5fcaa2009-10-27 15:52:27 +00006369 ring->rx_stats.csum_err++;
Eric Dumazet12dcd862010-10-15 17:27:10 +00006370 u64_stats_update_end(&ring->rx_syncp);
6371 }
Auke Kok9d5c8242008-01-24 02:22:38 -08006372 /* let the stack verify checksum errors */
Auke Kok9d5c8242008-01-24 02:22:38 -08006373 return;
6374 }
6375 /* It must be a TCP or UDP packet with a valid checksum */
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00006376 if (igb_test_staterr(rx_desc, E1000_RXD_STAT_TCPCS |
6377 E1000_RXD_STAT_UDPCS))
Auke Kok9d5c8242008-01-24 02:22:38 -08006378 skb->ip_summed = CHECKSUM_UNNECESSARY;
6379
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00006380 dev_dbg(ring->dev, "cksum success: bits %08X\n",
6381 le32_to_cpu(rx_desc->wb.upper.status_error));
Auke Kok9d5c8242008-01-24 02:22:38 -08006382}
6383
Alexander Duyck077887c2011-08-26 07:46:29 +00006384static inline void igb_rx_hash(struct igb_ring *ring,
6385 union e1000_adv_rx_desc *rx_desc,
6386 struct sk_buff *skb)
6387{
6388 if (ring->netdev->features & NETIF_F_RXHASH)
6389 skb->rxhash = le32_to_cpu(rx_desc->wb.lower.hi_dword.rss);
6390}
6391
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006392/**
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006393 * igb_is_non_eop - process handling of non-EOP buffers
6394 * @rx_ring: Rx ring being processed
6395 * @rx_desc: Rx descriptor for current buffer
6396 * @skb: current socket buffer containing buffer in progress
6397 *
6398 * This function updates next to clean. If the buffer is an EOP buffer
6399 * this function exits returning false, otherwise it will place the
6400 * sk_buff in the next buffer to be chained and return true indicating
6401 * that this is in fact a non-EOP buffer.
6402 **/
6403static bool igb_is_non_eop(struct igb_ring *rx_ring,
6404 union e1000_adv_rx_desc *rx_desc)
6405{
6406 u32 ntc = rx_ring->next_to_clean + 1;
6407
6408 /* fetch, update, and store next to clean */
6409 ntc = (ntc < rx_ring->count) ? ntc : 0;
6410 rx_ring->next_to_clean = ntc;
6411
6412 prefetch(IGB_RX_DESC(rx_ring, ntc));
6413
6414 if (likely(igb_test_staterr(rx_desc, E1000_RXD_STAT_EOP)))
6415 return false;
6416
6417 return true;
6418}
6419
6420/**
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006421 * igb_get_headlen - determine size of header for LRO/GRO
6422 * @data: pointer to the start of the headers
6423 * @max_len: total length of section to find headers in
6424 *
6425 * This function is meant to determine the length of headers that will
6426 * be recognized by hardware for LRO, and GRO offloads. The main
6427 * motivation of doing this is to only perform one pull for IPv4 TCP
6428 * packets so that we can do basic things like calculating the gso_size
6429 * based on the average data per packet.
6430 **/
6431static unsigned int igb_get_headlen(unsigned char *data,
6432 unsigned int max_len)
Alexander Duyck2d94d8a2009-07-23 18:10:06 +00006433{
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006434 union {
6435 unsigned char *network;
6436 /* l2 headers */
6437 struct ethhdr *eth;
6438 struct vlan_hdr *vlan;
6439 /* l3 headers */
6440 struct iphdr *ipv4;
6441 struct ipv6hdr *ipv6;
6442 } hdr;
6443 __be16 protocol;
6444 u8 nexthdr = 0; /* default to not TCP */
6445 u8 hlen;
6446
6447 /* this should never happen, but better safe than sorry */
6448 if (max_len < ETH_HLEN)
6449 return max_len;
6450
6451 /* initialize network frame pointer */
6452 hdr.network = data;
6453
6454 /* set first protocol and move network header forward */
6455 protocol = hdr.eth->h_proto;
6456 hdr.network += ETH_HLEN;
6457
6458 /* handle any vlan tag if present */
6459 if (protocol == __constant_htons(ETH_P_8021Q)) {
6460 if ((hdr.network - data) > (max_len - VLAN_HLEN))
6461 return max_len;
6462
6463 protocol = hdr.vlan->h_vlan_encapsulated_proto;
6464 hdr.network += VLAN_HLEN;
6465 }
6466
6467 /* handle L3 protocols */
6468 if (protocol == __constant_htons(ETH_P_IP)) {
6469 if ((hdr.network - data) > (max_len - sizeof(struct iphdr)))
6470 return max_len;
6471
6472 /* access ihl as a u8 to avoid unaligned access on ia64 */
6473 hlen = (hdr.network[0] & 0x0F) << 2;
6474
6475 /* verify hlen meets minimum size requirements */
6476 if (hlen < sizeof(struct iphdr))
6477 return hdr.network - data;
6478
Alexander Duyckf2fb4ab2012-11-13 01:13:38 +00006479 /* record next protocol if header is present */
6480 if (!hdr.ipv4->frag_off)
6481 nexthdr = hdr.ipv4->protocol;
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006482 } else if (protocol == __constant_htons(ETH_P_IPV6)) {
6483 if ((hdr.network - data) > (max_len - sizeof(struct ipv6hdr)))
6484 return max_len;
6485
6486 /* record next protocol */
6487 nexthdr = hdr.ipv6->nexthdr;
Alexander Duyckf2fb4ab2012-11-13 01:13:38 +00006488 hlen = sizeof(struct ipv6hdr);
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006489 } else {
6490 return hdr.network - data;
6491 }
6492
Alexander Duyckf2fb4ab2012-11-13 01:13:38 +00006493 /* relocate pointer to start of L4 header */
6494 hdr.network += hlen;
6495
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006496 /* finally sort out TCP */
6497 if (nexthdr == IPPROTO_TCP) {
6498 if ((hdr.network - data) > (max_len - sizeof(struct tcphdr)))
6499 return max_len;
6500
6501 /* access doff as a u8 to avoid unaligned access on ia64 */
6502 hlen = (hdr.network[12] & 0xF0) >> 2;
6503
6504 /* verify hlen meets minimum size requirements */
6505 if (hlen < sizeof(struct tcphdr))
6506 return hdr.network - data;
6507
6508 hdr.network += hlen;
6509 } else if (nexthdr == IPPROTO_UDP) {
6510 if ((hdr.network - data) > (max_len - sizeof(struct udphdr)))
6511 return max_len;
6512
6513 hdr.network += sizeof(struct udphdr);
6514 }
6515
6516 /*
6517 * If everything has gone correctly hdr.network should be the
6518 * data section of the packet and will be the end of the header.
6519 * If not then it probably represents the end of the last recognized
6520 * header.
Alexander Duyck2d94d8a2009-07-23 18:10:06 +00006521 */
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006522 if ((hdr.network - data) < max_len)
6523 return hdr.network - data;
6524 else
6525 return max_len;
6526}
6527
6528/**
6529 * igb_pull_tail - igb specific version of skb_pull_tail
6530 * @rx_ring: rx descriptor ring packet is being transacted on
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006531 * @rx_desc: pointer to the EOP Rx descriptor
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006532 * @skb: pointer to current skb being adjusted
6533 *
6534 * This function is an igb specific version of __pskb_pull_tail. The
6535 * main difference between this version and the original function is that
6536 * this function can make several assumptions about the state of things
6537 * that allow for significant optimizations versus the standard function.
6538 * As a result we can do things like drop a frag and maintain an accurate
6539 * truesize for the skb.
6540 */
6541static void igb_pull_tail(struct igb_ring *rx_ring,
6542 union e1000_adv_rx_desc *rx_desc,
6543 struct sk_buff *skb)
6544{
6545 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[0];
6546 unsigned char *va;
6547 unsigned int pull_len;
6548
6549 /*
6550 * it is valid to use page_address instead of kmap since we are
6551 * working with pages allocated out of the lomem pool per
6552 * alloc_page(GFP_ATOMIC)
6553 */
6554 va = skb_frag_address(frag);
6555
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006556 if (igb_test_staterr(rx_desc, E1000_RXDADV_STAT_TSIP)) {
6557 /* retrieve timestamp from buffer */
6558 igb_ptp_rx_pktstamp(rx_ring->q_vector, va, skb);
6559
6560 /* update pointers to remove timestamp header */
6561 skb_frag_size_sub(frag, IGB_TS_HDR_LEN);
6562 frag->page_offset += IGB_TS_HDR_LEN;
6563 skb->data_len -= IGB_TS_HDR_LEN;
6564 skb->len -= IGB_TS_HDR_LEN;
6565
6566 /* move va to start of packet data */
6567 va += IGB_TS_HDR_LEN;
6568 }
6569
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006570 /*
6571 * we need the header to contain the greater of either ETH_HLEN or
6572 * 60 bytes if the skb->len is less than 60 for skb_pad.
6573 */
6574 pull_len = igb_get_headlen(va, IGB_RX_HDR_LEN);
6575
6576 /* align pull length to size of long to optimize memcpy performance */
6577 skb_copy_to_linear_data(skb, va, ALIGN(pull_len, sizeof(long)));
6578
6579 /* update all of the pointers */
6580 skb_frag_size_sub(frag, pull_len);
6581 frag->page_offset += pull_len;
6582 skb->data_len -= pull_len;
6583 skb->tail += pull_len;
6584}
6585
6586/**
6587 * igb_cleanup_headers - Correct corrupted or empty headers
6588 * @rx_ring: rx descriptor ring packet is being transacted on
6589 * @rx_desc: pointer to the EOP Rx descriptor
6590 * @skb: pointer to current skb being fixed
6591 *
6592 * Address the case where we are pulling data in on pages only
6593 * and as such no data is present in the skb header.
6594 *
6595 * In addition if skb is not at least 60 bytes we need to pad it so that
6596 * it is large enough to qualify as a valid Ethernet frame.
6597 *
6598 * Returns true if an error was encountered and skb was freed.
6599 **/
6600static bool igb_cleanup_headers(struct igb_ring *rx_ring,
6601 union e1000_adv_rx_desc *rx_desc,
6602 struct sk_buff *skb)
6603{
6604
6605 if (unlikely((igb_test_staterr(rx_desc,
6606 E1000_RXDEXT_ERR_FRAME_ERR_MASK)))) {
6607 struct net_device *netdev = rx_ring->netdev;
6608 if (!(netdev->features & NETIF_F_RXALL)) {
6609 dev_kfree_skb_any(skb);
6610 return true;
6611 }
6612 }
6613
6614 /* place header in linear portion of buffer */
6615 if (skb_is_nonlinear(skb))
6616 igb_pull_tail(rx_ring, rx_desc, skb);
6617
6618 /* if skb_pad returns an error the skb was freed */
6619 if (unlikely(skb->len < 60)) {
6620 int pad_len = 60 - skb->len;
6621
6622 if (skb_pad(skb, pad_len))
6623 return true;
6624 __skb_put(skb, pad_len);
6625 }
6626
6627 return false;
Alexander Duyck2d94d8a2009-07-23 18:10:06 +00006628}
6629
Alexander Duyckdb2ee5b2012-09-25 00:30:57 +00006630/**
6631 * igb_process_skb_fields - Populate skb header fields from Rx descriptor
6632 * @rx_ring: rx descriptor ring packet is being transacted on
6633 * @rx_desc: pointer to the EOP Rx descriptor
6634 * @skb: pointer to current skb being populated
6635 *
6636 * This function checks the ring, descriptor, and packet information in
6637 * order to populate the hash, checksum, VLAN, timestamp, protocol, and
6638 * other fields within the skb.
6639 **/
6640static void igb_process_skb_fields(struct igb_ring *rx_ring,
6641 union e1000_adv_rx_desc *rx_desc,
6642 struct sk_buff *skb)
6643{
6644 struct net_device *dev = rx_ring->netdev;
6645
6646 igb_rx_hash(rx_ring, rx_desc, skb);
6647
6648 igb_rx_checksum(rx_ring, rx_desc, skb);
6649
Alexander Duyckdb2ee5b2012-09-25 00:30:57 +00006650 igb_ptp_rx_hwtstamp(rx_ring->q_vector, rx_desc, skb);
Alexander Duyckdb2ee5b2012-09-25 00:30:57 +00006651
6652 if ((dev->features & NETIF_F_HW_VLAN_RX) &&
6653 igb_test_staterr(rx_desc, E1000_RXD_STAT_VP)) {
6654 u16 vid;
6655 if (igb_test_staterr(rx_desc, E1000_RXDEXT_STATERR_LB) &&
6656 test_bit(IGB_RING_FLAG_RX_LB_VLAN_BSWAP, &rx_ring->flags))
6657 vid = be16_to_cpu(rx_desc->wb.upper.vlan);
6658 else
6659 vid = le16_to_cpu(rx_desc->wb.upper.vlan);
6660
6661 __vlan_hwaccel_put_tag(skb, vid);
6662 }
6663
6664 skb_record_rx_queue(skb, rx_ring->queue_index);
6665
6666 skb->protocol = eth_type_trans(skb, rx_ring->netdev);
6667}
6668
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006669static bool igb_clean_rx_irq(struct igb_q_vector *q_vector, const int budget)
Auke Kok9d5c8242008-01-24 02:22:38 -08006670{
Alexander Duyck0ba82992011-08-26 07:45:47 +00006671 struct igb_ring *rx_ring = q_vector->rx.ring;
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006672 struct sk_buff *skb = rx_ring->skb;
Auke Kok9d5c8242008-01-24 02:22:38 -08006673 unsigned int total_bytes = 0, total_packets = 0;
Alexander Duyck16eb8812011-08-26 07:43:54 +00006674 u16 cleaned_count = igb_desc_unused(rx_ring);
Auke Kok9d5c8242008-01-24 02:22:38 -08006675
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006676 do {
6677 union e1000_adv_rx_desc *rx_desc;
Auke Kok9d5c8242008-01-24 02:22:38 -08006678
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006679 /* return some buffers to hardware, one at a time is too slow */
6680 if (cleaned_count >= IGB_RX_BUFFER_WRITE) {
6681 igb_alloc_rx_buffers(rx_ring, cleaned_count);
6682 cleaned_count = 0;
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07006683 }
6684
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006685 rx_desc = IGB_RX_DESC(rx_ring, rx_ring->next_to_clean);
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07006686
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006687 if (!igb_test_staterr(rx_desc, E1000_RXD_STAT_DD))
6688 break;
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07006689
Alexander Duyck74e238e2013-02-02 05:07:11 +00006690 /* This memory barrier is needed to keep us from reading
6691 * any other fields out of the rx_desc until we know the
6692 * RXD_STAT_DD bit is set
6693 */
6694 rmb();
6695
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006696 /* retrieve a buffer from the ring */
Alexander Duyck74e238e2013-02-02 05:07:11 +00006697 if (ring_uses_build_skb(rx_ring))
6698 skb = igb_build_rx_buffer(rx_ring, rx_desc);
6699 else
6700 skb = igb_fetch_rx_buffer(rx_ring, rx_desc, skb);
Alexander Duyck16eb8812011-08-26 07:43:54 +00006701
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006702 /* exit if we failed to retrieve a buffer */
6703 if (!skb)
6704 break;
6705
6706 cleaned_count++;
6707
6708 /* fetch next buffer in frame if non-eop */
6709 if (igb_is_non_eop(rx_ring, rx_desc))
6710 continue;
Alexander Duyck44390ca2011-08-26 07:43:38 +00006711
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006712 /* verify the packet layout is correct */
6713 if (igb_cleanup_headers(rx_ring, rx_desc, skb)) {
6714 skb = NULL;
6715 continue;
Auke Kok9d5c8242008-01-24 02:22:38 -08006716 }
Auke Kok9d5c8242008-01-24 02:22:38 -08006717
Alexander Duyckdb2ee5b2012-09-25 00:30:57 +00006718 /* probably a little skewed due to removing CRC */
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00006719 total_bytes += skb->len;
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00006720
Alexander Duyckdb2ee5b2012-09-25 00:30:57 +00006721 /* populate checksum, timestamp, VLAN, and protocol */
6722 igb_process_skb_fields(rx_ring, rx_desc, skb);
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00006723
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00006724 napi_gro_receive(&q_vector->napi, skb);
Auke Kok9d5c8242008-01-24 02:22:38 -08006725
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006726 /* reset skb pointer */
6727 skb = NULL;
6728
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006729 /* update budget accounting */
6730 total_packets++;
6731 } while (likely(total_packets < budget));
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07006732
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006733 /* place incomplete frames back on ring for completion */
6734 rx_ring->skb = skb;
6735
Eric Dumazet12dcd862010-10-15 17:27:10 +00006736 u64_stats_update_begin(&rx_ring->rx_syncp);
Auke Kok9d5c8242008-01-24 02:22:38 -08006737 rx_ring->rx_stats.packets += total_packets;
6738 rx_ring->rx_stats.bytes += total_bytes;
Eric Dumazet12dcd862010-10-15 17:27:10 +00006739 u64_stats_update_end(&rx_ring->rx_syncp);
Alexander Duyck0ba82992011-08-26 07:45:47 +00006740 q_vector->rx.total_packets += total_packets;
6741 q_vector->rx.total_bytes += total_bytes;
Alexander Duyckc023cd82011-08-26 07:43:43 +00006742
6743 if (cleaned_count)
Alexander Duyckcd392f52011-08-26 07:43:59 +00006744 igb_alloc_rx_buffers(rx_ring, cleaned_count);
Alexander Duyckc023cd82011-08-26 07:43:43 +00006745
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006746 return (total_packets < budget);
Auke Kok9d5c8242008-01-24 02:22:38 -08006747}
6748
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006749static bool igb_alloc_mapped_page(struct igb_ring *rx_ring,
6750 struct igb_rx_buffer *bi)
Alexander Duyckc023cd82011-08-26 07:43:43 +00006751{
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006752 struct page *page = bi->page;
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006753 dma_addr_t dma;
Alexander Duyckc023cd82011-08-26 07:43:43 +00006754
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006755 /* since we are recycling buffers we should seldom need to alloc */
6756 if (likely(page))
Alexander Duyckc023cd82011-08-26 07:43:43 +00006757 return true;
6758
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006759 /* alloc new page for storage */
6760 page = __skb_alloc_page(GFP_ATOMIC | __GFP_COLD, NULL);
6761 if (unlikely(!page)) {
6762 rx_ring->rx_stats.alloc_failed++;
6763 return false;
Alexander Duyckc023cd82011-08-26 07:43:43 +00006764 }
6765
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006766 /* map page for use */
6767 dma = dma_map_page(rx_ring->dev, page, 0, PAGE_SIZE, DMA_FROM_DEVICE);
Alexander Duyckc023cd82011-08-26 07:43:43 +00006768
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006769 /*
6770 * if mapping failed free memory back to system since
6771 * there isn't much point in holding memory we can't use
6772 */
Alexander Duyckc023cd82011-08-26 07:43:43 +00006773 if (dma_mapping_error(rx_ring->dev, dma)) {
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006774 __free_page(page);
6775
Alexander Duyckc023cd82011-08-26 07:43:43 +00006776 rx_ring->rx_stats.alloc_failed++;
6777 return false;
6778 }
6779
6780 bi->dma = dma;
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006781 bi->page = page;
6782 bi->page_offset = 0;
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006783
Alexander Duyckc023cd82011-08-26 07:43:43 +00006784 return true;
6785}
6786
Alexander Duyck74e238e2013-02-02 05:07:11 +00006787static inline unsigned int igb_rx_offset(struct igb_ring *rx_ring)
6788{
6789 if (ring_uses_build_skb(rx_ring))
6790 return NET_SKB_PAD + NET_IP_ALIGN;
6791 else
6792 return 0;
6793}
6794
Auke Kok9d5c8242008-01-24 02:22:38 -08006795/**
Alexander Duyckcd392f52011-08-26 07:43:59 +00006796 * igb_alloc_rx_buffers - Replace used receive buffers; packet split
Auke Kok9d5c8242008-01-24 02:22:38 -08006797 * @adapter: address of board private structure
6798 **/
Alexander Duyckcd392f52011-08-26 07:43:59 +00006799void igb_alloc_rx_buffers(struct igb_ring *rx_ring, u16 cleaned_count)
Auke Kok9d5c8242008-01-24 02:22:38 -08006800{
Auke Kok9d5c8242008-01-24 02:22:38 -08006801 union e1000_adv_rx_desc *rx_desc;
Alexander Duyck06034642011-08-26 07:44:22 +00006802 struct igb_rx_buffer *bi;
Alexander Duyckc023cd82011-08-26 07:43:43 +00006803 u16 i = rx_ring->next_to_use;
Auke Kok9d5c8242008-01-24 02:22:38 -08006804
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006805 /* nothing to do */
6806 if (!cleaned_count)
6807 return;
6808
Alexander Duyck601369062011-08-26 07:44:05 +00006809 rx_desc = IGB_RX_DESC(rx_ring, i);
Alexander Duyck06034642011-08-26 07:44:22 +00006810 bi = &rx_ring->rx_buffer_info[i];
Alexander Duyckc023cd82011-08-26 07:43:43 +00006811 i -= rx_ring->count;
Auke Kok9d5c8242008-01-24 02:22:38 -08006812
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006813 do {
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006814 if (!igb_alloc_mapped_page(rx_ring, bi))
Alexander Duyckc023cd82011-08-26 07:43:43 +00006815 break;
Auke Kok9d5c8242008-01-24 02:22:38 -08006816
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006817 /*
6818 * Refresh the desc even if buffer_addrs didn't change
6819 * because each write-back erases this info.
6820 */
Alexander Duyck74e238e2013-02-02 05:07:11 +00006821 rx_desc->read.pkt_addr = cpu_to_le64(bi->dma +
6822 bi->page_offset +
6823 igb_rx_offset(rx_ring));
Auke Kok9d5c8242008-01-24 02:22:38 -08006824
Alexander Duyckc023cd82011-08-26 07:43:43 +00006825 rx_desc++;
6826 bi++;
Auke Kok9d5c8242008-01-24 02:22:38 -08006827 i++;
Alexander Duyckc023cd82011-08-26 07:43:43 +00006828 if (unlikely(!i)) {
Alexander Duyck601369062011-08-26 07:44:05 +00006829 rx_desc = IGB_RX_DESC(rx_ring, 0);
Alexander Duyck06034642011-08-26 07:44:22 +00006830 bi = rx_ring->rx_buffer_info;
Alexander Duyckc023cd82011-08-26 07:43:43 +00006831 i -= rx_ring->count;
6832 }
6833
6834 /* clear the hdr_addr for the next_to_use descriptor */
6835 rx_desc->read.hdr_addr = 0;
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006836
6837 cleaned_count--;
6838 } while (cleaned_count);
Auke Kok9d5c8242008-01-24 02:22:38 -08006839
Alexander Duyckc023cd82011-08-26 07:43:43 +00006840 i += rx_ring->count;
6841
Auke Kok9d5c8242008-01-24 02:22:38 -08006842 if (rx_ring->next_to_use != i) {
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006843 /* record the next descriptor to use */
Auke Kok9d5c8242008-01-24 02:22:38 -08006844 rx_ring->next_to_use = i;
Auke Kok9d5c8242008-01-24 02:22:38 -08006845
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006846 /* update next to alloc since we have filled the ring */
6847 rx_ring->next_to_alloc = i;
6848
6849 /*
6850 * Force memory writes to complete before letting h/w
Auke Kok9d5c8242008-01-24 02:22:38 -08006851 * know there are new descriptors to fetch. (Only
6852 * applicable for weak-ordered memory model archs,
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006853 * such as IA-64).
6854 */
Auke Kok9d5c8242008-01-24 02:22:38 -08006855 wmb();
Alexander Duyckfce99e32009-10-27 15:51:27 +00006856 writel(i, rx_ring->tail);
Auke Kok9d5c8242008-01-24 02:22:38 -08006857 }
6858}
6859
6860/**
6861 * igb_mii_ioctl -
6862 * @netdev:
6863 * @ifreq:
6864 * @cmd:
6865 **/
6866static int igb_mii_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
6867{
6868 struct igb_adapter *adapter = netdev_priv(netdev);
6869 struct mii_ioctl_data *data = if_mii(ifr);
6870
6871 if (adapter->hw.phy.media_type != e1000_media_type_copper)
6872 return -EOPNOTSUPP;
6873
6874 switch (cmd) {
6875 case SIOCGMIIPHY:
6876 data->phy_id = adapter->hw.phy.addr;
6877 break;
6878 case SIOCGMIIREG:
Alexander Duyckf5f4cf02008-11-21 21:30:24 -08006879 if (igb_read_phy_reg(&adapter->hw, data->reg_num & 0x1F,
6880 &data->val_out))
Auke Kok9d5c8242008-01-24 02:22:38 -08006881 return -EIO;
6882 break;
6883 case SIOCSMIIREG:
6884 default:
6885 return -EOPNOTSUPP;
6886 }
6887 return 0;
6888}
6889
6890/**
6891 * igb_ioctl -
6892 * @netdev:
6893 * @ifreq:
6894 * @cmd:
6895 **/
6896static int igb_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
6897{
6898 switch (cmd) {
6899 case SIOCGMIIPHY:
6900 case SIOCGMIIREG:
6901 case SIOCSMIIREG:
6902 return igb_mii_ioctl(netdev, ifr, cmd);
Patrick Ohlyc6cb0902009-02-12 05:03:42 +00006903 case SIOCSHWTSTAMP:
Matthew Vicka79f4f82012-08-10 05:40:44 +00006904 return igb_ptp_hwtstamp_ioctl(netdev, ifr, cmd);
Auke Kok9d5c8242008-01-24 02:22:38 -08006905 default:
6906 return -EOPNOTSUPP;
6907 }
6908}
6909
Alexander Duyck009bc062009-07-23 18:08:35 +00006910s32 igb_read_pcie_cap_reg(struct e1000_hw *hw, u32 reg, u16 *value)
6911{
6912 struct igb_adapter *adapter = hw->back;
Alexander Duyck009bc062009-07-23 18:08:35 +00006913
Jiang Liu23d028c2012-08-20 13:32:20 -06006914 if (pcie_capability_read_word(adapter->pdev, reg, value))
Alexander Duyck009bc062009-07-23 18:08:35 +00006915 return -E1000_ERR_CONFIG;
6916
Alexander Duyck009bc062009-07-23 18:08:35 +00006917 return 0;
6918}
6919
6920s32 igb_write_pcie_cap_reg(struct e1000_hw *hw, u32 reg, u16 *value)
6921{
6922 struct igb_adapter *adapter = hw->back;
Alexander Duyck009bc062009-07-23 18:08:35 +00006923
Jiang Liu23d028c2012-08-20 13:32:20 -06006924 if (pcie_capability_write_word(adapter->pdev, reg, *value))
Alexander Duyck009bc062009-07-23 18:08:35 +00006925 return -E1000_ERR_CONFIG;
6926
Alexander Duyck009bc062009-07-23 18:08:35 +00006927 return 0;
6928}
6929
Michał Mirosławc8f44af2011-11-15 15:29:55 +00006930static void igb_vlan_mode(struct net_device *netdev, netdev_features_t features)
Auke Kok9d5c8242008-01-24 02:22:38 -08006931{
6932 struct igb_adapter *adapter = netdev_priv(netdev);
6933 struct e1000_hw *hw = &adapter->hw;
6934 u32 ctrl, rctl;
Alexander Duyck5faf0302011-08-26 07:46:08 +00006935 bool enable = !!(features & NETIF_F_HW_VLAN_RX);
Auke Kok9d5c8242008-01-24 02:22:38 -08006936
Alexander Duyck5faf0302011-08-26 07:46:08 +00006937 if (enable) {
Auke Kok9d5c8242008-01-24 02:22:38 -08006938 /* enable VLAN tag insert/strip */
6939 ctrl = rd32(E1000_CTRL);
6940 ctrl |= E1000_CTRL_VME;
6941 wr32(E1000_CTRL, ctrl);
6942
Alexander Duyck51466232009-10-27 23:47:35 +00006943 /* Disable CFI check */
Auke Kok9d5c8242008-01-24 02:22:38 -08006944 rctl = rd32(E1000_RCTL);
Auke Kok9d5c8242008-01-24 02:22:38 -08006945 rctl &= ~E1000_RCTL_CFIEN;
6946 wr32(E1000_RCTL, rctl);
Auke Kok9d5c8242008-01-24 02:22:38 -08006947 } else {
6948 /* disable VLAN tag insert/strip */
6949 ctrl = rd32(E1000_CTRL);
6950 ctrl &= ~E1000_CTRL_VME;
6951 wr32(E1000_CTRL, ctrl);
Auke Kok9d5c8242008-01-24 02:22:38 -08006952 }
6953
Alexander Duycke1739522009-02-19 20:39:44 -08006954 igb_rlpml_set(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08006955}
6956
Jiri Pirko8e586132011-12-08 19:52:37 -05006957static int igb_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
Auke Kok9d5c8242008-01-24 02:22:38 -08006958{
6959 struct igb_adapter *adapter = netdev_priv(netdev);
6960 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006961 int pf_id = adapter->vfs_allocated_count;
Auke Kok9d5c8242008-01-24 02:22:38 -08006962
Alexander Duyck51466232009-10-27 23:47:35 +00006963 /* attempt to add filter to vlvf array */
6964 igb_vlvf_set(adapter, vid, true, pf_id);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006965
Alexander Duyck51466232009-10-27 23:47:35 +00006966 /* add the filter since PF can receive vlans w/o entry in vlvf */
6967 igb_vfta_set(hw, vid, true);
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00006968
6969 set_bit(vid, adapter->active_vlans);
Jiri Pirko8e586132011-12-08 19:52:37 -05006970
6971 return 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08006972}
6973
Jiri Pirko8e586132011-12-08 19:52:37 -05006974static int igb_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
Auke Kok9d5c8242008-01-24 02:22:38 -08006975{
6976 struct igb_adapter *adapter = netdev_priv(netdev);
6977 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006978 int pf_id = adapter->vfs_allocated_count;
Alexander Duyck51466232009-10-27 23:47:35 +00006979 s32 err;
Auke Kok9d5c8242008-01-24 02:22:38 -08006980
Alexander Duyck51466232009-10-27 23:47:35 +00006981 /* remove vlan from VLVF table array */
6982 err = igb_vlvf_set(adapter, vid, false, pf_id);
Auke Kok9d5c8242008-01-24 02:22:38 -08006983
Alexander Duyck51466232009-10-27 23:47:35 +00006984 /* if vid was not present in VLVF just remove it from table */
6985 if (err)
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006986 igb_vfta_set(hw, vid, false);
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00006987
6988 clear_bit(vid, adapter->active_vlans);
Jiri Pirko8e586132011-12-08 19:52:37 -05006989
6990 return 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08006991}
6992
6993static void igb_restore_vlan(struct igb_adapter *adapter)
6994{
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00006995 u16 vid;
Auke Kok9d5c8242008-01-24 02:22:38 -08006996
Alexander Duyck5faf0302011-08-26 07:46:08 +00006997 igb_vlan_mode(adapter->netdev, adapter->netdev->features);
6998
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00006999 for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
7000 igb_vlan_rx_add_vid(adapter->netdev, vid);
Auke Kok9d5c8242008-01-24 02:22:38 -08007001}
7002
David Decotigny14ad2512011-04-27 18:32:43 +00007003int igb_set_spd_dplx(struct igb_adapter *adapter, u32 spd, u8 dplx)
Auke Kok9d5c8242008-01-24 02:22:38 -08007004{
Alexander Duyck090b1792009-10-27 23:51:55 +00007005 struct pci_dev *pdev = adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08007006 struct e1000_mac_info *mac = &adapter->hw.mac;
7007
7008 mac->autoneg = 0;
7009
David Decotigny14ad2512011-04-27 18:32:43 +00007010 /* Make sure dplx is at most 1 bit and lsb of speed is not set
7011 * for the switch() below to work */
7012 if ((spd & 1) || (dplx & ~1))
7013 goto err_inval;
7014
Carolyn Wybornycd2638a2010-10-12 22:27:02 +00007015 /* Fiber NIC's only allow 1000 Gbps Full duplex */
7016 if ((adapter->hw.phy.media_type == e1000_media_type_internal_serdes) &&
David Decotigny14ad2512011-04-27 18:32:43 +00007017 spd != SPEED_1000 &&
7018 dplx != DUPLEX_FULL)
7019 goto err_inval;
Carolyn Wybornycd2638a2010-10-12 22:27:02 +00007020
David Decotigny14ad2512011-04-27 18:32:43 +00007021 switch (spd + dplx) {
Auke Kok9d5c8242008-01-24 02:22:38 -08007022 case SPEED_10 + DUPLEX_HALF:
7023 mac->forced_speed_duplex = ADVERTISE_10_HALF;
7024 break;
7025 case SPEED_10 + DUPLEX_FULL:
7026 mac->forced_speed_duplex = ADVERTISE_10_FULL;
7027 break;
7028 case SPEED_100 + DUPLEX_HALF:
7029 mac->forced_speed_duplex = ADVERTISE_100_HALF;
7030 break;
7031 case SPEED_100 + DUPLEX_FULL:
7032 mac->forced_speed_duplex = ADVERTISE_100_FULL;
7033 break;
7034 case SPEED_1000 + DUPLEX_FULL:
7035 mac->autoneg = 1;
7036 adapter->hw.phy.autoneg_advertised = ADVERTISE_1000_FULL;
7037 break;
7038 case SPEED_1000 + DUPLEX_HALF: /* not supported */
7039 default:
David Decotigny14ad2512011-04-27 18:32:43 +00007040 goto err_inval;
Auke Kok9d5c8242008-01-24 02:22:38 -08007041 }
Jesse Brandeburg8376dad2012-07-26 02:31:19 +00007042
7043 /* clear MDI, MDI(-X) override is only allowed when autoneg enabled */
7044 adapter->hw.phy.mdix = AUTO_ALL_MODES;
7045
Auke Kok9d5c8242008-01-24 02:22:38 -08007046 return 0;
David Decotigny14ad2512011-04-27 18:32:43 +00007047
7048err_inval:
7049 dev_err(&pdev->dev, "Unsupported Speed/Duplex configuration\n");
7050 return -EINVAL;
Auke Kok9d5c8242008-01-24 02:22:38 -08007051}
7052
Yan, Zheng749ab2c2012-01-04 20:23:37 +00007053static int __igb_shutdown(struct pci_dev *pdev, bool *enable_wake,
7054 bool runtime)
Auke Kok9d5c8242008-01-24 02:22:38 -08007055{
7056 struct net_device *netdev = pci_get_drvdata(pdev);
7057 struct igb_adapter *adapter = netdev_priv(netdev);
7058 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck2d064c02008-07-08 15:10:12 -07007059 u32 ctrl, rctl, status;
Yan, Zheng749ab2c2012-01-04 20:23:37 +00007060 u32 wufc = runtime ? E1000_WUFC_LNKC : adapter->wol;
Auke Kok9d5c8242008-01-24 02:22:38 -08007061#ifdef CONFIG_PM
7062 int retval = 0;
7063#endif
7064
7065 netif_device_detach(netdev);
7066
Alexander Duycka88f10e2008-07-08 15:13:38 -07007067 if (netif_running(netdev))
Yan, Zheng749ab2c2012-01-04 20:23:37 +00007068 __igb_close(netdev, true);
Alexander Duycka88f10e2008-07-08 15:13:38 -07007069
Alexander Duyck047e0032009-10-27 15:49:27 +00007070 igb_clear_interrupt_scheme(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08007071
7072#ifdef CONFIG_PM
7073 retval = pci_save_state(pdev);
7074 if (retval)
7075 return retval;
7076#endif
7077
7078 status = rd32(E1000_STATUS);
7079 if (status & E1000_STATUS_LU)
7080 wufc &= ~E1000_WUFC_LNKC;
7081
7082 if (wufc) {
7083 igb_setup_rctl(adapter);
Alexander Duyckff41f8d2009-09-03 14:48:56 +00007084 igb_set_rx_mode(netdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08007085
7086 /* turn on all-multi mode if wake on multicast is enabled */
7087 if (wufc & E1000_WUFC_MC) {
7088 rctl = rd32(E1000_RCTL);
7089 rctl |= E1000_RCTL_MPE;
7090 wr32(E1000_RCTL, rctl);
7091 }
7092
7093 ctrl = rd32(E1000_CTRL);
7094 /* advertise wake from D3Cold */
7095 #define E1000_CTRL_ADVD3WUC 0x00100000
7096 /* phy power management enable */
7097 #define E1000_CTRL_EN_PHY_PWR_MGMT 0x00200000
7098 ctrl |= E1000_CTRL_ADVD3WUC;
7099 wr32(E1000_CTRL, ctrl);
7100
Auke Kok9d5c8242008-01-24 02:22:38 -08007101 /* Allow time for pending master requests to run */
Alexander Duyck330a6d62009-10-27 23:51:35 +00007102 igb_disable_pcie_master(hw);
Auke Kok9d5c8242008-01-24 02:22:38 -08007103
7104 wr32(E1000_WUC, E1000_WUC_PME_EN);
7105 wr32(E1000_WUFC, wufc);
Auke Kok9d5c8242008-01-24 02:22:38 -08007106 } else {
7107 wr32(E1000_WUC, 0);
7108 wr32(E1000_WUFC, 0);
Auke Kok9d5c8242008-01-24 02:22:38 -08007109 }
7110
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00007111 *enable_wake = wufc || adapter->en_mng_pt;
7112 if (!*enable_wake)
Nick Nunley88a268c2010-02-17 01:01:59 +00007113 igb_power_down_link(adapter);
7114 else
7115 igb_power_up_link(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08007116
7117 /* Release control of h/w to f/w. If f/w is AMT enabled, this
7118 * would have already happened in close and is redundant. */
7119 igb_release_hw_control(adapter);
7120
7121 pci_disable_device(pdev);
7122
Auke Kok9d5c8242008-01-24 02:22:38 -08007123 return 0;
7124}
7125
7126#ifdef CONFIG_PM
Emil Tantilovd9dd9662012-01-28 08:10:35 +00007127#ifdef CONFIG_PM_SLEEP
Yan, Zheng749ab2c2012-01-04 20:23:37 +00007128static int igb_suspend(struct device *dev)
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00007129{
7130 int retval;
7131 bool wake;
Yan, Zheng749ab2c2012-01-04 20:23:37 +00007132 struct pci_dev *pdev = to_pci_dev(dev);
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00007133
Yan, Zheng749ab2c2012-01-04 20:23:37 +00007134 retval = __igb_shutdown(pdev, &wake, 0);
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00007135 if (retval)
7136 return retval;
7137
7138 if (wake) {
7139 pci_prepare_to_sleep(pdev);
7140 } else {
7141 pci_wake_from_d3(pdev, false);
7142 pci_set_power_state(pdev, PCI_D3hot);
7143 }
7144
7145 return 0;
7146}
Emil Tantilovd9dd9662012-01-28 08:10:35 +00007147#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00007148
Yan, Zheng749ab2c2012-01-04 20:23:37 +00007149static int igb_resume(struct device *dev)
Auke Kok9d5c8242008-01-24 02:22:38 -08007150{
Yan, Zheng749ab2c2012-01-04 20:23:37 +00007151 struct pci_dev *pdev = to_pci_dev(dev);
Auke Kok9d5c8242008-01-24 02:22:38 -08007152 struct net_device *netdev = pci_get_drvdata(pdev);
7153 struct igb_adapter *adapter = netdev_priv(netdev);
7154 struct e1000_hw *hw = &adapter->hw;
7155 u32 err;
7156
7157 pci_set_power_state(pdev, PCI_D0);
7158 pci_restore_state(pdev);
Nick Nunleyb94f2d72010-02-17 01:02:19 +00007159 pci_save_state(pdev);
Taku Izumi42bfd33a2008-06-20 12:10:30 +09007160
Alexander Duyckaed5dec2009-02-06 23:16:04 +00007161 err = pci_enable_device_mem(pdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08007162 if (err) {
7163 dev_err(&pdev->dev,
7164 "igb: Cannot enable PCI device from suspend\n");
7165 return err;
7166 }
7167 pci_set_master(pdev);
7168
7169 pci_enable_wake(pdev, PCI_D3hot, 0);
7170 pci_enable_wake(pdev, PCI_D3cold, 0);
7171
Stefan Assmann53c7d062012-12-04 06:00:12 +00007172 if (igb_init_interrupt_scheme(adapter, true)) {
Alexander Duycka88f10e2008-07-08 15:13:38 -07007173 dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
7174 return -ENOMEM;
Auke Kok9d5c8242008-01-24 02:22:38 -08007175 }
7176
Auke Kok9d5c8242008-01-24 02:22:38 -08007177 igb_reset(adapter);
Alexander Duycka8564f02009-02-06 23:21:10 +00007178
7179 /* let the f/w know that the h/w is now under the control of the
7180 * driver. */
7181 igb_get_hw_control(adapter);
7182
Auke Kok9d5c8242008-01-24 02:22:38 -08007183 wr32(E1000_WUS, ~0);
7184
Yan, Zheng749ab2c2012-01-04 20:23:37 +00007185 if (netdev->flags & IFF_UP) {
Alexander Duyck0c2cc022012-09-25 00:31:22 +00007186 rtnl_lock();
Yan, Zheng749ab2c2012-01-04 20:23:37 +00007187 err = __igb_open(netdev, true);
Alexander Duyck0c2cc022012-09-25 00:31:22 +00007188 rtnl_unlock();
Alexander Duycka88f10e2008-07-08 15:13:38 -07007189 if (err)
7190 return err;
7191 }
Auke Kok9d5c8242008-01-24 02:22:38 -08007192
7193 netif_device_attach(netdev);
Yan, Zheng749ab2c2012-01-04 20:23:37 +00007194 return 0;
7195}
7196
7197#ifdef CONFIG_PM_RUNTIME
7198static int igb_runtime_idle(struct device *dev)
7199{
7200 struct pci_dev *pdev = to_pci_dev(dev);
7201 struct net_device *netdev = pci_get_drvdata(pdev);
7202 struct igb_adapter *adapter = netdev_priv(netdev);
7203
7204 if (!igb_has_link(adapter))
7205 pm_schedule_suspend(dev, MSEC_PER_SEC * 5);
7206
7207 return -EBUSY;
7208}
7209
7210static int igb_runtime_suspend(struct device *dev)
7211{
7212 struct pci_dev *pdev = to_pci_dev(dev);
7213 int retval;
7214 bool wake;
7215
7216 retval = __igb_shutdown(pdev, &wake, 1);
7217 if (retval)
7218 return retval;
7219
7220 if (wake) {
7221 pci_prepare_to_sleep(pdev);
7222 } else {
7223 pci_wake_from_d3(pdev, false);
7224 pci_set_power_state(pdev, PCI_D3hot);
7225 }
Auke Kok9d5c8242008-01-24 02:22:38 -08007226
Auke Kok9d5c8242008-01-24 02:22:38 -08007227 return 0;
7228}
Yan, Zheng749ab2c2012-01-04 20:23:37 +00007229
7230static int igb_runtime_resume(struct device *dev)
7231{
7232 return igb_resume(dev);
7233}
7234#endif /* CONFIG_PM_RUNTIME */
Auke Kok9d5c8242008-01-24 02:22:38 -08007235#endif
7236
7237static void igb_shutdown(struct pci_dev *pdev)
7238{
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00007239 bool wake;
7240
Yan, Zheng749ab2c2012-01-04 20:23:37 +00007241 __igb_shutdown(pdev, &wake, 0);
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00007242
7243 if (system_state == SYSTEM_POWER_OFF) {
7244 pci_wake_from_d3(pdev, wake);
7245 pci_set_power_state(pdev, PCI_D3hot);
7246 }
Auke Kok9d5c8242008-01-24 02:22:38 -08007247}
7248
Greg Rosefa44f2f2013-01-17 01:03:06 -08007249#ifdef CONFIG_PCI_IOV
7250static int igb_sriov_reinit(struct pci_dev *dev)
7251{
7252 struct net_device *netdev = pci_get_drvdata(dev);
7253 struct igb_adapter *adapter = netdev_priv(netdev);
7254 struct pci_dev *pdev = adapter->pdev;
7255
7256 rtnl_lock();
7257
7258 if (netif_running(netdev))
7259 igb_close(netdev);
7260
7261 igb_clear_interrupt_scheme(adapter);
7262
7263 igb_init_queue_configuration(adapter);
7264
7265 if (igb_init_interrupt_scheme(adapter, true)) {
7266 dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
7267 return -ENOMEM;
7268 }
7269
7270 if (netif_running(netdev))
7271 igb_open(netdev);
7272
7273 rtnl_unlock();
7274
7275 return 0;
7276}
7277
7278static int igb_pci_disable_sriov(struct pci_dev *dev)
7279{
7280 int err = igb_disable_sriov(dev);
7281
7282 if (!err)
7283 err = igb_sriov_reinit(dev);
7284
7285 return err;
7286}
7287
7288static int igb_pci_enable_sriov(struct pci_dev *dev, int num_vfs)
7289{
7290 int err = igb_enable_sriov(dev, num_vfs);
7291
7292 if (err)
7293 goto out;
7294
7295 err = igb_sriov_reinit(dev);
7296 if (!err)
7297 return num_vfs;
7298
7299out:
7300 return err;
7301}
7302
7303#endif
7304static int igb_pci_sriov_configure(struct pci_dev *dev, int num_vfs)
7305{
7306#ifdef CONFIG_PCI_IOV
7307 if (num_vfs == 0)
7308 return igb_pci_disable_sriov(dev);
7309 else
7310 return igb_pci_enable_sriov(dev, num_vfs);
7311#endif
7312 return 0;
7313}
7314
Auke Kok9d5c8242008-01-24 02:22:38 -08007315#ifdef CONFIG_NET_POLL_CONTROLLER
7316/*
7317 * Polling 'interrupt' - used by things like netconsole to send skbs
7318 * without having to re-enable interrupts. It's not called while
7319 * the interrupt routine is executing.
7320 */
7321static void igb_netpoll(struct net_device *netdev)
7322{
7323 struct igb_adapter *adapter = netdev_priv(netdev);
Alexander Duyckeebbbdb2009-02-06 23:19:29 +00007324 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck0d1ae7f2011-08-26 07:46:34 +00007325 struct igb_q_vector *q_vector;
Auke Kok9d5c8242008-01-24 02:22:38 -08007326 int i;
Auke Kok9d5c8242008-01-24 02:22:38 -08007327
Alexander Duyck047e0032009-10-27 15:49:27 +00007328 for (i = 0; i < adapter->num_q_vectors; i++) {
Alexander Duyck0d1ae7f2011-08-26 07:46:34 +00007329 q_vector = adapter->q_vector[i];
7330 if (adapter->msix_entries)
7331 wr32(E1000_EIMC, q_vector->eims_value);
7332 else
7333 igb_irq_disable(adapter);
Alexander Duyck047e0032009-10-27 15:49:27 +00007334 napi_schedule(&q_vector->napi);
Alexander Duyckeebbbdb2009-02-06 23:19:29 +00007335 }
Auke Kok9d5c8242008-01-24 02:22:38 -08007336}
7337#endif /* CONFIG_NET_POLL_CONTROLLER */
7338
7339/**
7340 * igb_io_error_detected - called when PCI error is detected
7341 * @pdev: Pointer to PCI device
7342 * @state: The current pci connection state
7343 *
7344 * This function is called after a PCI bus error affecting
7345 * this device has been detected.
7346 */
7347static pci_ers_result_t igb_io_error_detected(struct pci_dev *pdev,
7348 pci_channel_state_t state)
7349{
7350 struct net_device *netdev = pci_get_drvdata(pdev);
7351 struct igb_adapter *adapter = netdev_priv(netdev);
7352
7353 netif_device_detach(netdev);
7354
Alexander Duyck59ed6ee2009-06-30 12:46:34 +00007355 if (state == pci_channel_io_perm_failure)
7356 return PCI_ERS_RESULT_DISCONNECT;
7357
Auke Kok9d5c8242008-01-24 02:22:38 -08007358 if (netif_running(netdev))
7359 igb_down(adapter);
7360 pci_disable_device(pdev);
7361
7362 /* Request a slot slot reset. */
7363 return PCI_ERS_RESULT_NEED_RESET;
7364}
7365
7366/**
7367 * igb_io_slot_reset - called after the pci bus has been reset.
7368 * @pdev: Pointer to PCI device
7369 *
7370 * Restart the card from scratch, as if from a cold-boot. Implementation
7371 * resembles the first-half of the igb_resume routine.
7372 */
7373static pci_ers_result_t igb_io_slot_reset(struct pci_dev *pdev)
7374{
7375 struct net_device *netdev = pci_get_drvdata(pdev);
7376 struct igb_adapter *adapter = netdev_priv(netdev);
7377 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck40a914f2008-11-27 00:24:37 -08007378 pci_ers_result_t result;
Taku Izumi42bfd33a2008-06-20 12:10:30 +09007379 int err;
Auke Kok9d5c8242008-01-24 02:22:38 -08007380
Alexander Duyckaed5dec2009-02-06 23:16:04 +00007381 if (pci_enable_device_mem(pdev)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08007382 dev_err(&pdev->dev,
7383 "Cannot re-enable PCI device after reset.\n");
Alexander Duyck40a914f2008-11-27 00:24:37 -08007384 result = PCI_ERS_RESULT_DISCONNECT;
7385 } else {
7386 pci_set_master(pdev);
7387 pci_restore_state(pdev);
Nick Nunleyb94f2d72010-02-17 01:02:19 +00007388 pci_save_state(pdev);
Alexander Duyck40a914f2008-11-27 00:24:37 -08007389
7390 pci_enable_wake(pdev, PCI_D3hot, 0);
7391 pci_enable_wake(pdev, PCI_D3cold, 0);
7392
7393 igb_reset(adapter);
7394 wr32(E1000_WUS, ~0);
7395 result = PCI_ERS_RESULT_RECOVERED;
Auke Kok9d5c8242008-01-24 02:22:38 -08007396 }
Auke Kok9d5c8242008-01-24 02:22:38 -08007397
Jeff Kirsherea943d42008-12-11 20:34:19 -08007398 err = pci_cleanup_aer_uncorrect_error_status(pdev);
7399 if (err) {
7400 dev_err(&pdev->dev, "pci_cleanup_aer_uncorrect_error_status "
7401 "failed 0x%0x\n", err);
7402 /* non-fatal, continue */
7403 }
Auke Kok9d5c8242008-01-24 02:22:38 -08007404
Alexander Duyck40a914f2008-11-27 00:24:37 -08007405 return result;
Auke Kok9d5c8242008-01-24 02:22:38 -08007406}
7407
7408/**
7409 * igb_io_resume - called when traffic can start flowing again.
7410 * @pdev: Pointer to PCI device
7411 *
7412 * This callback is called when the error recovery driver tells us that
7413 * its OK to resume normal operation. Implementation resembles the
7414 * second-half of the igb_resume routine.
7415 */
7416static void igb_io_resume(struct pci_dev *pdev)
7417{
7418 struct net_device *netdev = pci_get_drvdata(pdev);
7419 struct igb_adapter *adapter = netdev_priv(netdev);
7420
Auke Kok9d5c8242008-01-24 02:22:38 -08007421 if (netif_running(netdev)) {
7422 if (igb_up(adapter)) {
7423 dev_err(&pdev->dev, "igb_up failed after reset\n");
7424 return;
7425 }
7426 }
7427
7428 netif_device_attach(netdev);
7429
7430 /* let the f/w know that the h/w is now under the control of the
7431 * driver. */
7432 igb_get_hw_control(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08007433}
7434
Alexander Duyck26ad9172009-10-05 06:32:49 +00007435static void igb_rar_set_qsel(struct igb_adapter *adapter, u8 *addr, u32 index,
7436 u8 qsel)
7437{
7438 u32 rar_low, rar_high;
7439 struct e1000_hw *hw = &adapter->hw;
7440
7441 /* HW expects these in little endian so we reverse the byte order
7442 * from network order (big endian) to little endian
7443 */
7444 rar_low = ((u32) addr[0] | ((u32) addr[1] << 8) |
7445 ((u32) addr[2] << 16) | ((u32) addr[3] << 24));
7446 rar_high = ((u32) addr[4] | ((u32) addr[5] << 8));
7447
7448 /* Indicate to hardware the Address is Valid. */
7449 rar_high |= E1000_RAH_AV;
7450
7451 if (hw->mac.type == e1000_82575)
7452 rar_high |= E1000_RAH_POOL_1 * qsel;
7453 else
7454 rar_high |= E1000_RAH_POOL_1 << qsel;
7455
7456 wr32(E1000_RAL(index), rar_low);
7457 wrfl();
7458 wr32(E1000_RAH(index), rar_high);
7459 wrfl();
7460}
7461
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007462static int igb_set_vf_mac(struct igb_adapter *adapter,
7463 int vf, unsigned char *mac_addr)
7464{
7465 struct e1000_hw *hw = &adapter->hw;
Alexander Duyckff41f8d2009-09-03 14:48:56 +00007466 /* VF MAC addresses start at end of receive addresses and moves
7467 * torwards the first, as a result a collision should not be possible */
7468 int rar_entry = hw->mac.rar_entry_count - (vf + 1);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007469
Alexander Duyck37680112009-02-19 20:40:30 -08007470 memcpy(adapter->vf_data[vf].vf_mac_addresses, mac_addr, ETH_ALEN);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007471
Alexander Duyck26ad9172009-10-05 06:32:49 +00007472 igb_rar_set_qsel(adapter, mac_addr, rar_entry, vf);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007473
7474 return 0;
7475}
7476
Williams, Mitch A8151d292010-02-10 01:44:24 +00007477static int igb_ndo_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
7478{
7479 struct igb_adapter *adapter = netdev_priv(netdev);
7480 if (!is_valid_ether_addr(mac) || (vf >= adapter->vfs_allocated_count))
7481 return -EINVAL;
7482 adapter->vf_data[vf].flags |= IGB_VF_FLAG_PF_SET_MAC;
7483 dev_info(&adapter->pdev->dev, "setting MAC %pM on VF %d\n", mac, vf);
7484 dev_info(&adapter->pdev->dev, "Reload the VF driver to make this"
7485 " change effective.");
7486 if (test_bit(__IGB_DOWN, &adapter->state)) {
7487 dev_warn(&adapter->pdev->dev, "The VF MAC address has been set,"
7488 " but the PF device is not up.\n");
7489 dev_warn(&adapter->pdev->dev, "Bring the PF device up before"
7490 " attempting to use the VF device.\n");
7491 }
7492 return igb_set_vf_mac(adapter, vf, mac);
7493}
7494
Lior Levy17dc5662011-02-08 02:28:46 +00007495static int igb_link_mbps(int internal_link_speed)
7496{
7497 switch (internal_link_speed) {
7498 case SPEED_100:
7499 return 100;
7500 case SPEED_1000:
7501 return 1000;
7502 default:
7503 return 0;
7504 }
7505}
7506
7507static void igb_set_vf_rate_limit(struct e1000_hw *hw, int vf, int tx_rate,
7508 int link_speed)
7509{
7510 int rf_dec, rf_int;
7511 u32 bcnrc_val;
7512
7513 if (tx_rate != 0) {
7514 /* Calculate the rate factor values to set */
7515 rf_int = link_speed / tx_rate;
7516 rf_dec = (link_speed - (rf_int * tx_rate));
7517 rf_dec = (rf_dec * (1<<E1000_RTTBCNRC_RF_INT_SHIFT)) / tx_rate;
7518
7519 bcnrc_val = E1000_RTTBCNRC_RS_ENA;
7520 bcnrc_val |= ((rf_int<<E1000_RTTBCNRC_RF_INT_SHIFT) &
7521 E1000_RTTBCNRC_RF_INT_MASK);
7522 bcnrc_val |= (rf_dec & E1000_RTTBCNRC_RF_DEC_MASK);
7523 } else {
7524 bcnrc_val = 0;
7525 }
7526
7527 wr32(E1000_RTTDQSEL, vf); /* vf X uses queue X */
Lior Levyf00b0da2011-06-04 06:05:03 +00007528 /*
7529 * Set global transmit compensation time to the MMW_SIZE in RTTBCNRM
7530 * register. MMW_SIZE=0x014 if 9728-byte jumbo is supported.
7531 */
7532 wr32(E1000_RTTBCNRM, 0x14);
Lior Levy17dc5662011-02-08 02:28:46 +00007533 wr32(E1000_RTTBCNRC, bcnrc_val);
7534}
7535
7536static void igb_check_vf_rate_limit(struct igb_adapter *adapter)
7537{
7538 int actual_link_speed, i;
7539 bool reset_rate = false;
7540
7541 /* VF TX rate limit was not set or not supported */
7542 if ((adapter->vf_rate_link_speed == 0) ||
7543 (adapter->hw.mac.type != e1000_82576))
7544 return;
7545
7546 actual_link_speed = igb_link_mbps(adapter->link_speed);
7547 if (actual_link_speed != adapter->vf_rate_link_speed) {
7548 reset_rate = true;
7549 adapter->vf_rate_link_speed = 0;
7550 dev_info(&adapter->pdev->dev,
7551 "Link speed has been changed. VF Transmit "
7552 "rate is disabled\n");
7553 }
7554
7555 for (i = 0; i < adapter->vfs_allocated_count; i++) {
7556 if (reset_rate)
7557 adapter->vf_data[i].tx_rate = 0;
7558
7559 igb_set_vf_rate_limit(&adapter->hw, i,
7560 adapter->vf_data[i].tx_rate,
7561 actual_link_speed);
7562 }
7563}
7564
Williams, Mitch A8151d292010-02-10 01:44:24 +00007565static int igb_ndo_set_vf_bw(struct net_device *netdev, int vf, int tx_rate)
7566{
Lior Levy17dc5662011-02-08 02:28:46 +00007567 struct igb_adapter *adapter = netdev_priv(netdev);
7568 struct e1000_hw *hw = &adapter->hw;
7569 int actual_link_speed;
7570
7571 if (hw->mac.type != e1000_82576)
7572 return -EOPNOTSUPP;
7573
7574 actual_link_speed = igb_link_mbps(adapter->link_speed);
7575 if ((vf >= adapter->vfs_allocated_count) ||
7576 (!(rd32(E1000_STATUS) & E1000_STATUS_LU)) ||
7577 (tx_rate < 0) || (tx_rate > actual_link_speed))
7578 return -EINVAL;
7579
7580 adapter->vf_rate_link_speed = actual_link_speed;
7581 adapter->vf_data[vf].tx_rate = (u16)tx_rate;
7582 igb_set_vf_rate_limit(hw, vf, tx_rate, actual_link_speed);
7583
7584 return 0;
Williams, Mitch A8151d292010-02-10 01:44:24 +00007585}
7586
7587static int igb_ndo_get_vf_config(struct net_device *netdev,
7588 int vf, struct ifla_vf_info *ivi)
7589{
7590 struct igb_adapter *adapter = netdev_priv(netdev);
7591 if (vf >= adapter->vfs_allocated_count)
7592 return -EINVAL;
7593 ivi->vf = vf;
7594 memcpy(&ivi->mac, adapter->vf_data[vf].vf_mac_addresses, ETH_ALEN);
Lior Levy17dc5662011-02-08 02:28:46 +00007595 ivi->tx_rate = adapter->vf_data[vf].tx_rate;
Williams, Mitch A8151d292010-02-10 01:44:24 +00007596 ivi->vlan = adapter->vf_data[vf].pf_vlan;
7597 ivi->qos = adapter->vf_data[vf].pf_qos;
7598 return 0;
7599}
7600
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007601static void igb_vmm_control(struct igb_adapter *adapter)
7602{
7603 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck10d8e902009-10-27 15:54:04 +00007604 u32 reg;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007605
Alexander Duyck52a1dd42010-03-22 14:07:46 +00007606 switch (hw->mac.type) {
7607 case e1000_82575:
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00007608 case e1000_i210:
7609 case e1000_i211:
Alexander Duyck52a1dd42010-03-22 14:07:46 +00007610 default:
7611 /* replication is not supported for 82575 */
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007612 return;
Alexander Duyck52a1dd42010-03-22 14:07:46 +00007613 case e1000_82576:
7614 /* notify HW that the MAC is adding vlan tags */
7615 reg = rd32(E1000_DTXCTL);
7616 reg |= E1000_DTXCTL_VLAN_ADDED;
7617 wr32(E1000_DTXCTL, reg);
7618 case e1000_82580:
7619 /* enable replication vlan tag stripping */
7620 reg = rd32(E1000_RPLOLR);
7621 reg |= E1000_RPLOLR_STRVLAN;
7622 wr32(E1000_RPLOLR, reg);
Alexander Duyckd2ba2ed2010-03-22 14:08:06 +00007623 case e1000_i350:
7624 /* none of the above registers are supported by i350 */
Alexander Duyck52a1dd42010-03-22 14:07:46 +00007625 break;
7626 }
Alexander Duyck10d8e902009-10-27 15:54:04 +00007627
Alexander Duyckd4960302009-10-27 15:53:45 +00007628 if (adapter->vfs_allocated_count) {
7629 igb_vmdq_set_loopback_pf(hw, true);
7630 igb_vmdq_set_replication_pf(hw, true);
Greg Rose13800462010-11-06 02:08:26 +00007631 igb_vmdq_set_anti_spoofing_pf(hw, true,
7632 adapter->vfs_allocated_count);
Alexander Duyckd4960302009-10-27 15:53:45 +00007633 } else {
7634 igb_vmdq_set_loopback_pf(hw, false);
7635 igb_vmdq_set_replication_pf(hw, false);
7636 }
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007637}
7638
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +00007639static void igb_init_dmac(struct igb_adapter *adapter, u32 pba)
7640{
7641 struct e1000_hw *hw = &adapter->hw;
7642 u32 dmac_thr;
7643 u16 hwm;
7644
7645 if (hw->mac.type > e1000_82580) {
7646 if (adapter->flags & IGB_FLAG_DMAC) {
7647 u32 reg;
7648
7649 /* force threshold to 0. */
7650 wr32(E1000_DMCTXTH, 0);
7651
7652 /*
Matthew Vicke8c626e2011-11-17 08:33:12 +00007653 * DMA Coalescing high water mark needs to be greater
7654 * than the Rx threshold. Set hwm to PBA - max frame
7655 * size in 16B units, capping it at PBA - 6KB.
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +00007656 */
Matthew Vicke8c626e2011-11-17 08:33:12 +00007657 hwm = 64 * pba - adapter->max_frame_size / 16;
7658 if (hwm < 64 * (pba - 6))
7659 hwm = 64 * (pba - 6);
7660 reg = rd32(E1000_FCRTC);
7661 reg &= ~E1000_FCRTC_RTH_COAL_MASK;
7662 reg |= ((hwm << E1000_FCRTC_RTH_COAL_SHIFT)
7663 & E1000_FCRTC_RTH_COAL_MASK);
7664 wr32(E1000_FCRTC, reg);
7665
7666 /*
7667 * Set the DMA Coalescing Rx threshold to PBA - 2 * max
7668 * frame size, capping it at PBA - 10KB.
7669 */
7670 dmac_thr = pba - adapter->max_frame_size / 512;
7671 if (dmac_thr < pba - 10)
7672 dmac_thr = pba - 10;
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +00007673 reg = rd32(E1000_DMACR);
7674 reg &= ~E1000_DMACR_DMACTHR_MASK;
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +00007675 reg |= ((dmac_thr << E1000_DMACR_DMACTHR_SHIFT)
7676 & E1000_DMACR_DMACTHR_MASK);
7677
7678 /* transition to L0x or L1 if available..*/
7679 reg |= (E1000_DMACR_DMAC_EN | E1000_DMACR_DMAC_LX_MASK);
7680
7681 /* watchdog timer= +-1000 usec in 32usec intervals */
7682 reg |= (1000 >> 5);
Matthew Vick0c02dd92012-04-14 05:20:32 +00007683
7684 /* Disable BMC-to-OS Watchdog Enable */
7685 reg &= ~E1000_DMACR_DC_BMC2OSW_EN;
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +00007686 wr32(E1000_DMACR, reg);
7687
7688 /*
7689 * no lower threshold to disable
7690 * coalescing(smart fifb)-UTRESH=0
7691 */
7692 wr32(E1000_DMCRTRH, 0);
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +00007693
7694 reg = (IGB_DMCTLX_DCFLUSH_DIS | 0x4);
7695
7696 wr32(E1000_DMCTLX, reg);
7697
7698 /*
7699 * free space in tx packet buffer to wake from
7700 * DMA coal
7701 */
7702 wr32(E1000_DMCTXTH, (IGB_MIN_TXPBSIZE -
7703 (IGB_TX_BUF_4096 + adapter->max_frame_size)) >> 6);
7704
7705 /*
7706 * make low power state decision controlled
7707 * by DMA coal
7708 */
7709 reg = rd32(E1000_PCIEMISC);
7710 reg &= ~E1000_PCIEMISC_LX_DECISION;
7711 wr32(E1000_PCIEMISC, reg);
7712 } /* endif adapter->dmac is not disabled */
7713 } else if (hw->mac.type == e1000_82580) {
7714 u32 reg = rd32(E1000_PCIEMISC);
7715 wr32(E1000_PCIEMISC, reg & ~E1000_PCIEMISC_LX_DECISION);
7716 wr32(E1000_DMACR, 0);
7717 }
7718}
7719
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +00007720static DEFINE_SPINLOCK(i2c_clients_lock);
7721
7722/* igb_get_i2c_client - returns matching client
7723 * in adapters's client list.
7724 * @adapter: adapter struct
7725 * @dev_addr: device address of i2c needed.
7726 */
Carolyn Wyborny2c7d7722013-02-14 03:28:19 +00007727static struct i2c_client *
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +00007728igb_get_i2c_client(struct igb_adapter *adapter, u8 dev_addr)
7729{
7730 ulong flags;
7731 struct igb_i2c_client_list *client_list;
7732 struct i2c_client *client = NULL;
7733 struct i2c_board_info client_info = {
7734 I2C_BOARD_INFO("igb", 0x00),
7735 };
7736
7737 spin_lock_irqsave(&i2c_clients_lock, flags);
7738 client_list = adapter->i2c_clients;
7739
7740 /* See if we already have an i2c_client */
7741 while (client_list) {
7742 if (client_list->client->addr == (dev_addr >> 1)) {
7743 client = client_list->client;
7744 goto exit;
7745 } else {
7746 client_list = client_list->next;
7747 }
7748 }
7749
Carolyn Wybornyd3f8ef12013-01-23 03:37:58 +00007750 /* no client_list found, create a new one */
Carolyn Wyborny199f6cc2013-01-23 03:43:01 +00007751 client_list = kzalloc(sizeof(*client_list), GFP_ATOMIC);
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +00007752 if (client_list == NULL)
7753 goto exit;
7754
7755 /* dev_addr passed to us is left-shifted by 1 bit
7756 * i2c_new_device call expects it to be flush to the right.
7757 */
7758 client_info.addr = dev_addr >> 1;
7759 client_info.platform_data = adapter;
7760 client_list->client = i2c_new_device(&adapter->i2c_adap, &client_info);
7761 if (client_list->client == NULL) {
Carolyn Wybornye4288932012-12-07 03:01:42 +00007762 dev_info(&adapter->pdev->dev,
7763 "Failed to create new i2c device..\n");
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +00007764 goto err_no_client;
7765 }
7766
7767 /* insert new client at head of list */
7768 client_list->next = adapter->i2c_clients;
7769 adapter->i2c_clients = client_list;
7770
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +00007771 client = client_list->client;
7772 goto exit;
7773
7774err_no_client:
7775 kfree(client_list);
7776exit:
7777 spin_unlock_irqrestore(&i2c_clients_lock, flags);
7778 return client;
7779}
7780
7781/* igb_read_i2c_byte - Reads 8 bit word over I2C
7782 * @hw: pointer to hardware structure
7783 * @byte_offset: byte offset to read
7784 * @dev_addr: device address
7785 * @data: value read
7786 *
7787 * Performs byte read operation over I2C interface at
7788 * a specified device address.
7789 */
7790s32 igb_read_i2c_byte(struct e1000_hw *hw, u8 byte_offset,
7791 u8 dev_addr, u8 *data)
7792{
7793 struct igb_adapter *adapter = container_of(hw, struct igb_adapter, hw);
7794 struct i2c_client *this_client = igb_get_i2c_client(adapter, dev_addr);
7795 s32 status;
7796 u16 swfw_mask = 0;
7797
7798 if (!this_client)
7799 return E1000_ERR_I2C;
7800
7801 swfw_mask = E1000_SWFW_PHY0_SM;
7802
7803 if (hw->mac.ops.acquire_swfw_sync(hw, swfw_mask)
7804 != E1000_SUCCESS)
7805 return E1000_ERR_SWFW_SYNC;
7806
7807 status = i2c_smbus_read_byte_data(this_client, byte_offset);
7808 hw->mac.ops.release_swfw_sync(hw, swfw_mask);
7809
7810 if (status < 0)
7811 return E1000_ERR_I2C;
7812 else {
7813 *data = status;
7814 return E1000_SUCCESS;
7815 }
7816}
7817
7818/* igb_write_i2c_byte - Writes 8 bit word over I2C
7819 * @hw: pointer to hardware structure
7820 * @byte_offset: byte offset to write
7821 * @dev_addr: device address
7822 * @data: value to write
7823 *
7824 * Performs byte write operation over I2C interface at
7825 * a specified device address.
7826 */
7827s32 igb_write_i2c_byte(struct e1000_hw *hw, u8 byte_offset,
7828 u8 dev_addr, u8 data)
7829{
7830 struct igb_adapter *adapter = container_of(hw, struct igb_adapter, hw);
7831 struct i2c_client *this_client = igb_get_i2c_client(adapter, dev_addr);
7832 s32 status;
7833 u16 swfw_mask = E1000_SWFW_PHY0_SM;
7834
7835 if (!this_client)
7836 return E1000_ERR_I2C;
7837
7838 if (hw->mac.ops.acquire_swfw_sync(hw, swfw_mask) != E1000_SUCCESS)
7839 return E1000_ERR_SWFW_SYNC;
7840 status = i2c_smbus_write_byte_data(this_client, byte_offset, data);
7841 hw->mac.ops.release_swfw_sync(hw, swfw_mask);
7842
7843 if (status)
7844 return E1000_ERR_I2C;
7845 else
7846 return E1000_SUCCESS;
7847
7848}
Auke Kok9d5c8242008-01-24 02:22:38 -08007849/* igb_main.c */