blob: 0173b61184240452009243ac328bcce958f975ba [file] [log] [blame]
Auke Kok9d5c8242008-01-24 02:22:38 -08001/*******************************************************************************
2
3 Intel(R) Gigabit Ethernet Linux driver
Carolyn Wyborny6e861322012-01-18 22:13:27 +00004 Copyright(c) 2007-2012 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";
Carolyn Wyborny6e861322012-01-18 22:13:27 +000072static const char igb_copyright[] = "Copyright (c) 2007-2012 Intel Corporation.";
Auke Kok9d5c8242008-01-24 02:22:38 -080073
Auke Kok9d5c8242008-01-24 02:22:38 -080074static const struct e1000_info *igb_info_tbl[] = {
75 [board_82575] = &e1000_82575_info,
76};
77
Alexey Dobriyana3aa1882010-01-07 11:58:11 +000078static DEFINE_PCI_DEVICE_TABLE(igb_pci_tbl) = {
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +000079 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I211_COPPER), board_82575 },
80 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I210_COPPER), board_82575 },
81 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I210_FIBER), board_82575 },
82 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I210_SERDES), board_82575 },
83 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I210_SGMII), board_82575 },
Alexander Duyckd2ba2ed2010-03-22 14:08:06 +000084 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I350_COPPER), board_82575 },
85 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I350_FIBER), board_82575 },
86 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I350_SERDES), board_82575 },
87 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I350_SGMII), board_82575 },
Alexander Duyck55cac242009-11-19 12:42:21 +000088 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_COPPER), board_82575 },
89 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_FIBER), board_82575 },
Carolyn Wyborny6493d242011-01-14 05:33:46 +000090 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_QUAD_FIBER), board_82575 },
Alexander Duyck55cac242009-11-19 12:42:21 +000091 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_SERDES), board_82575 },
92 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_SGMII), board_82575 },
93 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_COPPER_DUAL), board_82575 },
Joseph Gasparakis308fb392010-09-22 17:56:44 +000094 { PCI_VDEVICE(INTEL, E1000_DEV_ID_DH89XXCC_SGMII), board_82575 },
95 { PCI_VDEVICE(INTEL, E1000_DEV_ID_DH89XXCC_SERDES), board_82575 },
Gasparakis, Joseph1b5dda32010-12-09 01:41:01 +000096 { PCI_VDEVICE(INTEL, E1000_DEV_ID_DH89XXCC_BACKPLANE), board_82575 },
97 { PCI_VDEVICE(INTEL, E1000_DEV_ID_DH89XXCC_SFP), board_82575 },
Alexander Duyck2d064c02008-07-08 15:10:12 -070098 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576), board_82575 },
Alexander Duyck9eb23412009-03-13 20:42:15 +000099 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_NS), board_82575 },
Alexander Duyck747d49b2009-10-05 06:33:27 +0000100 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_NS_SERDES), board_82575 },
Alexander Duyck2d064c02008-07-08 15:10:12 -0700101 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_FIBER), board_82575 },
102 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_SERDES), board_82575 },
Alexander Duyck4703bf72009-07-23 18:09:48 +0000103 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_SERDES_QUAD), board_82575 },
Carolyn Wybornyb894fa22010-03-19 06:07:48 +0000104 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_QUAD_COPPER_ET2), board_82575 },
Alexander Duyckc8ea5ea2009-03-13 20:42:35 +0000105 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_QUAD_COPPER), board_82575 },
Auke Kok9d5c8242008-01-24 02:22:38 -0800106 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82575EB_COPPER), board_82575 },
107 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82575EB_FIBER_SERDES), board_82575 },
108 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82575GB_QUAD_COPPER), board_82575 },
109 /* required last entry */
110 {0, }
111};
112
113MODULE_DEVICE_TABLE(pci, igb_pci_tbl);
114
115void igb_reset(struct igb_adapter *);
116static int igb_setup_all_tx_resources(struct igb_adapter *);
117static int igb_setup_all_rx_resources(struct igb_adapter *);
118static void igb_free_all_tx_resources(struct igb_adapter *);
119static void igb_free_all_rx_resources(struct igb_adapter *);
Alexander Duyck06cf2662009-10-27 15:53:25 +0000120static void igb_setup_mrqc(struct igb_adapter *);
Auke Kok9d5c8242008-01-24 02:22:38 -0800121static int igb_probe(struct pci_dev *, const struct pci_device_id *);
Bill Pemberton9f9a12f2012-12-03 09:24:25 -0500122static void igb_remove(struct pci_dev *pdev);
Auke Kok9d5c8242008-01-24 02:22:38 -0800123static int igb_sw_init(struct igb_adapter *);
124static int igb_open(struct net_device *);
125static int igb_close(struct net_device *);
Stefan Assmann53c7d062012-12-04 06:00:12 +0000126static void igb_configure(struct igb_adapter *);
Auke Kok9d5c8242008-01-24 02:22:38 -0800127static void igb_configure_tx(struct igb_adapter *);
128static void igb_configure_rx(struct igb_adapter *);
Auke Kok9d5c8242008-01-24 02:22:38 -0800129static void igb_clean_all_tx_rings(struct igb_adapter *);
130static void igb_clean_all_rx_rings(struct igb_adapter *);
Mitch Williams3b644cf2008-06-27 10:59:48 -0700131static void igb_clean_tx_ring(struct igb_ring *);
132static void igb_clean_rx_ring(struct igb_ring *);
Alexander Duyckff41f8d2009-09-03 14:48:56 +0000133static void igb_set_rx_mode(struct net_device *);
Auke Kok9d5c8242008-01-24 02:22:38 -0800134static void igb_update_phy_info(unsigned long);
135static void igb_watchdog(unsigned long);
136static void igb_watchdog_task(struct work_struct *);
Alexander Duyckcd392f52011-08-26 07:43:59 +0000137static netdev_tx_t igb_xmit_frame(struct sk_buff *skb, struct net_device *);
Eric Dumazet12dcd862010-10-15 17:27:10 +0000138static struct rtnl_link_stats64 *igb_get_stats64(struct net_device *dev,
139 struct rtnl_link_stats64 *stats);
Auke Kok9d5c8242008-01-24 02:22:38 -0800140static int igb_change_mtu(struct net_device *, int);
141static int igb_set_mac(struct net_device *, void *);
Alexander Duyck68d480c2009-10-05 06:33:08 +0000142static void igb_set_uta(struct igb_adapter *adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -0800143static irqreturn_t igb_intr(int irq, void *);
144static irqreturn_t igb_intr_msi(int irq, void *);
145static irqreturn_t igb_msix_other(int irq, void *);
Alexander Duyck047e0032009-10-27 15:49:27 +0000146static irqreturn_t igb_msix_ring(int irq, void *);
Jeff Kirsher421e02f2008-10-17 11:08:31 -0700147#ifdef CONFIG_IGB_DCA
Alexander Duyck047e0032009-10-27 15:49:27 +0000148static void igb_update_dca(struct igb_q_vector *);
Jeb Cramerfe4506b2008-07-08 15:07:55 -0700149static void igb_setup_dca(struct igb_adapter *);
Jeff Kirsher421e02f2008-10-17 11:08:31 -0700150#endif /* CONFIG_IGB_DCA */
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -0700151static int igb_poll(struct napi_struct *, int);
Alexander Duyck13fde972011-10-05 13:35:24 +0000152static bool igb_clean_tx_irq(struct igb_q_vector *);
Alexander Duyckcd392f52011-08-26 07:43:59 +0000153static bool igb_clean_rx_irq(struct igb_q_vector *, int);
Auke Kok9d5c8242008-01-24 02:22:38 -0800154static int igb_ioctl(struct net_device *, struct ifreq *, int cmd);
155static void igb_tx_timeout(struct net_device *);
156static void igb_reset_task(struct work_struct *);
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000157static void igb_vlan_mode(struct net_device *netdev, netdev_features_t features);
Jiri Pirko8e586132011-12-08 19:52:37 -0500158static int igb_vlan_rx_add_vid(struct net_device *, u16);
159static int igb_vlan_rx_kill_vid(struct net_device *, u16);
Auke Kok9d5c8242008-01-24 02:22:38 -0800160static void igb_restore_vlan(struct igb_adapter *);
Alexander Duyck26ad9172009-10-05 06:32:49 +0000161static void igb_rar_set_qsel(struct igb_adapter *, u8 *, u32 , u8);
Alexander Duyck4ae196d2009-02-19 20:40:07 -0800162static void igb_ping_all_vfs(struct igb_adapter *);
163static void igb_msg_task(struct igb_adapter *);
Alexander Duyck4ae196d2009-02-19 20:40:07 -0800164static void igb_vmm_control(struct igb_adapter *);
Alexander Duyckf2ca0db2009-10-27 23:46:57 +0000165static int igb_set_vf_mac(struct igb_adapter *, int, unsigned char *);
Alexander Duyck4ae196d2009-02-19 20:40:07 -0800166static void igb_restore_vf_multicasts(struct igb_adapter *adapter);
Williams, Mitch A8151d292010-02-10 01:44:24 +0000167static int igb_ndo_set_vf_mac(struct net_device *netdev, int vf, u8 *mac);
168static int igb_ndo_set_vf_vlan(struct net_device *netdev,
169 int vf, u16 vlan, u8 qos);
170static int igb_ndo_set_vf_bw(struct net_device *netdev, int vf, int tx_rate);
171static int igb_ndo_get_vf_config(struct net_device *netdev, int vf,
172 struct ifla_vf_info *ivi);
Lior Levy17dc5662011-02-08 02:28:46 +0000173static void igb_check_vf_rate_limit(struct igb_adapter *);
RongQing Li46a01692011-10-18 22:52:35 +0000174
175#ifdef CONFIG_PCI_IOV
Greg Rose0224d662011-10-14 02:57:14 +0000176static int igb_vf_configure(struct igb_adapter *adapter, int vf);
Stefan Assmannf5571472012-08-18 04:06:11 +0000177static bool igb_vfs_are_assigned(struct igb_adapter *adapter);
RongQing Li46a01692011-10-18 22:52:35 +0000178#endif
Auke Kok9d5c8242008-01-24 02:22:38 -0800179
Auke Kok9d5c8242008-01-24 02:22:38 -0800180#ifdef CONFIG_PM
Emil Tantilovd9dd9662012-01-28 08:10:35 +0000181#ifdef CONFIG_PM_SLEEP
Yan, Zheng749ab2c2012-01-04 20:23:37 +0000182static int igb_suspend(struct device *);
Emil Tantilovd9dd9662012-01-28 08:10:35 +0000183#endif
Yan, Zheng749ab2c2012-01-04 20:23:37 +0000184static int igb_resume(struct device *);
185#ifdef CONFIG_PM_RUNTIME
186static int igb_runtime_suspend(struct device *dev);
187static int igb_runtime_resume(struct device *dev);
188static int igb_runtime_idle(struct device *dev);
189#endif
190static const struct dev_pm_ops igb_pm_ops = {
191 SET_SYSTEM_SLEEP_PM_OPS(igb_suspend, igb_resume)
192 SET_RUNTIME_PM_OPS(igb_runtime_suspend, igb_runtime_resume,
193 igb_runtime_idle)
194};
Auke Kok9d5c8242008-01-24 02:22:38 -0800195#endif
196static void igb_shutdown(struct pci_dev *);
Greg Rosefa44f2f2013-01-17 01:03:06 -0800197static int igb_pci_sriov_configure(struct pci_dev *dev, int num_vfs);
Jeff Kirsher421e02f2008-10-17 11:08:31 -0700198#ifdef CONFIG_IGB_DCA
Jeb Cramerfe4506b2008-07-08 15:07:55 -0700199static int igb_notify_dca(struct notifier_block *, unsigned long, void *);
200static struct notifier_block dca_notifier = {
201 .notifier_call = igb_notify_dca,
202 .next = NULL,
203 .priority = 0
204};
205#endif
Auke Kok9d5c8242008-01-24 02:22:38 -0800206#ifdef CONFIG_NET_POLL_CONTROLLER
207/* for netdump / net console */
208static void igb_netpoll(struct net_device *);
209#endif
Alexander Duyck37680112009-02-19 20:40:30 -0800210#ifdef CONFIG_PCI_IOV
Alexander Duyck2a3abf62009-04-07 14:37:52 +0000211static unsigned int max_vfs = 0;
212module_param(max_vfs, uint, 0);
213MODULE_PARM_DESC(max_vfs, "Maximum number of virtual functions to allocate "
214 "per physical function");
215#endif /* CONFIG_PCI_IOV */
216
Auke Kok9d5c8242008-01-24 02:22:38 -0800217static pci_ers_result_t igb_io_error_detected(struct pci_dev *,
218 pci_channel_state_t);
219static pci_ers_result_t igb_io_slot_reset(struct pci_dev *);
220static void igb_io_resume(struct pci_dev *);
221
Stephen Hemminger3646f0e2012-09-07 09:33:15 -0700222static const struct pci_error_handlers igb_err_handler = {
Auke Kok9d5c8242008-01-24 02:22:38 -0800223 .error_detected = igb_io_error_detected,
224 .slot_reset = igb_io_slot_reset,
225 .resume = igb_io_resume,
226};
227
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +0000228static void igb_init_dmac(struct igb_adapter *adapter, u32 pba);
Auke Kok9d5c8242008-01-24 02:22:38 -0800229
230static struct pci_driver igb_driver = {
231 .name = igb_driver_name,
232 .id_table = igb_pci_tbl,
233 .probe = igb_probe,
Bill Pemberton9f9a12f2012-12-03 09:24:25 -0500234 .remove = igb_remove,
Auke Kok9d5c8242008-01-24 02:22:38 -0800235#ifdef CONFIG_PM
Yan, Zheng749ab2c2012-01-04 20:23:37 +0000236 .driver.pm = &igb_pm_ops,
Auke Kok9d5c8242008-01-24 02:22:38 -0800237#endif
238 .shutdown = igb_shutdown,
Greg Rosefa44f2f2013-01-17 01:03:06 -0800239 .sriov_configure = igb_pci_sriov_configure,
Auke Kok9d5c8242008-01-24 02:22:38 -0800240 .err_handler = &igb_err_handler
241};
242
243MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
244MODULE_DESCRIPTION("Intel(R) Gigabit Ethernet Network Driver");
245MODULE_LICENSE("GPL");
246MODULE_VERSION(DRV_VERSION);
247
stephen hemmingerb3f4d592012-03-13 06:04:20 +0000248#define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK)
249static int debug = -1;
250module_param(debug, int, 0);
251MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
252
Taku Izumic97ec422010-04-27 14:39:30 +0000253struct igb_reg_info {
254 u32 ofs;
255 char *name;
256};
257
258static const struct igb_reg_info igb_reg_info_tbl[] = {
259
260 /* General Registers */
261 {E1000_CTRL, "CTRL"},
262 {E1000_STATUS, "STATUS"},
263 {E1000_CTRL_EXT, "CTRL_EXT"},
264
265 /* Interrupt Registers */
266 {E1000_ICR, "ICR"},
267
268 /* RX Registers */
269 {E1000_RCTL, "RCTL"},
270 {E1000_RDLEN(0), "RDLEN"},
271 {E1000_RDH(0), "RDH"},
272 {E1000_RDT(0), "RDT"},
273 {E1000_RXDCTL(0), "RXDCTL"},
274 {E1000_RDBAL(0), "RDBAL"},
275 {E1000_RDBAH(0), "RDBAH"},
276
277 /* TX Registers */
278 {E1000_TCTL, "TCTL"},
279 {E1000_TDBAL(0), "TDBAL"},
280 {E1000_TDBAH(0), "TDBAH"},
281 {E1000_TDLEN(0), "TDLEN"},
282 {E1000_TDH(0), "TDH"},
283 {E1000_TDT(0), "TDT"},
284 {E1000_TXDCTL(0), "TXDCTL"},
285 {E1000_TDFH, "TDFH"},
286 {E1000_TDFT, "TDFT"},
287 {E1000_TDFHS, "TDFHS"},
288 {E1000_TDFPC, "TDFPC"},
289
290 /* List Terminator */
291 {}
292};
293
294/*
295 * igb_regdump - register printout routine
296 */
297static void igb_regdump(struct e1000_hw *hw, struct igb_reg_info *reginfo)
298{
299 int n = 0;
300 char rname[16];
301 u32 regs[8];
302
303 switch (reginfo->ofs) {
304 case E1000_RDLEN(0):
305 for (n = 0; n < 4; n++)
306 regs[n] = rd32(E1000_RDLEN(n));
307 break;
308 case E1000_RDH(0):
309 for (n = 0; n < 4; n++)
310 regs[n] = rd32(E1000_RDH(n));
311 break;
312 case E1000_RDT(0):
313 for (n = 0; n < 4; n++)
314 regs[n] = rd32(E1000_RDT(n));
315 break;
316 case E1000_RXDCTL(0):
317 for (n = 0; n < 4; n++)
318 regs[n] = rd32(E1000_RXDCTL(n));
319 break;
320 case E1000_RDBAL(0):
321 for (n = 0; n < 4; n++)
322 regs[n] = rd32(E1000_RDBAL(n));
323 break;
324 case E1000_RDBAH(0):
325 for (n = 0; n < 4; n++)
326 regs[n] = rd32(E1000_RDBAH(n));
327 break;
328 case E1000_TDBAL(0):
329 for (n = 0; n < 4; n++)
330 regs[n] = rd32(E1000_RDBAL(n));
331 break;
332 case E1000_TDBAH(0):
333 for (n = 0; n < 4; n++)
334 regs[n] = rd32(E1000_TDBAH(n));
335 break;
336 case E1000_TDLEN(0):
337 for (n = 0; n < 4; n++)
338 regs[n] = rd32(E1000_TDLEN(n));
339 break;
340 case E1000_TDH(0):
341 for (n = 0; n < 4; n++)
342 regs[n] = rd32(E1000_TDH(n));
343 break;
344 case E1000_TDT(0):
345 for (n = 0; n < 4; n++)
346 regs[n] = rd32(E1000_TDT(n));
347 break;
348 case E1000_TXDCTL(0):
349 for (n = 0; n < 4; n++)
350 regs[n] = rd32(E1000_TXDCTL(n));
351 break;
352 default:
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000353 pr_info("%-15s %08x\n", reginfo->name, rd32(reginfo->ofs));
Taku Izumic97ec422010-04-27 14:39:30 +0000354 return;
355 }
356
357 snprintf(rname, 16, "%s%s", reginfo->name, "[0-3]");
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000358 pr_info("%-15s %08x %08x %08x %08x\n", rname, regs[0], regs[1],
359 regs[2], regs[3]);
Taku Izumic97ec422010-04-27 14:39:30 +0000360}
361
362/*
363 * igb_dump - Print registers, tx-rings and rx-rings
364 */
365static void igb_dump(struct igb_adapter *adapter)
366{
367 struct net_device *netdev = adapter->netdev;
368 struct e1000_hw *hw = &adapter->hw;
369 struct igb_reg_info *reginfo;
Taku Izumic97ec422010-04-27 14:39:30 +0000370 struct igb_ring *tx_ring;
371 union e1000_adv_tx_desc *tx_desc;
372 struct my_u0 { u64 a; u64 b; } *u0;
Taku Izumic97ec422010-04-27 14:39:30 +0000373 struct igb_ring *rx_ring;
374 union e1000_adv_rx_desc *rx_desc;
375 u32 staterr;
Alexander Duyck6ad4edf2011-08-26 07:45:26 +0000376 u16 i, n;
Taku Izumic97ec422010-04-27 14:39:30 +0000377
378 if (!netif_msg_hw(adapter))
379 return;
380
381 /* Print netdevice Info */
382 if (netdev) {
383 dev_info(&adapter->pdev->dev, "Net device Info\n");
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000384 pr_info("Device Name state trans_start "
385 "last_rx\n");
386 pr_info("%-15s %016lX %016lX %016lX\n", netdev->name,
387 netdev->state, netdev->trans_start, netdev->last_rx);
Taku Izumic97ec422010-04-27 14:39:30 +0000388 }
389
390 /* Print Registers */
391 dev_info(&adapter->pdev->dev, "Register Dump\n");
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000392 pr_info(" Register Name Value\n");
Taku Izumic97ec422010-04-27 14:39:30 +0000393 for (reginfo = (struct igb_reg_info *)igb_reg_info_tbl;
394 reginfo->name; reginfo++) {
395 igb_regdump(hw, reginfo);
396 }
397
398 /* Print TX Ring Summary */
399 if (!netdev || !netif_running(netdev))
400 goto exit;
401
402 dev_info(&adapter->pdev->dev, "TX Rings Summary\n");
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000403 pr_info("Queue [NTU] [NTC] [bi(ntc)->dma ] leng ntw timestamp\n");
Taku Izumic97ec422010-04-27 14:39:30 +0000404 for (n = 0; n < adapter->num_tx_queues; n++) {
Alexander Duyck06034642011-08-26 07:44:22 +0000405 struct igb_tx_buffer *buffer_info;
Taku Izumic97ec422010-04-27 14:39:30 +0000406 tx_ring = adapter->tx_ring[n];
Alexander Duyck06034642011-08-26 07:44:22 +0000407 buffer_info = &tx_ring->tx_buffer_info[tx_ring->next_to_clean];
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000408 pr_info(" %5d %5X %5X %016llX %04X %p %016llX\n",
409 n, tx_ring->next_to_use, tx_ring->next_to_clean,
Alexander Duyckc9f14bf32012-09-18 01:56:27 +0000410 (u64)dma_unmap_addr(buffer_info, dma),
411 dma_unmap_len(buffer_info, len),
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000412 buffer_info->next_to_watch,
413 (u64)buffer_info->time_stamp);
Taku Izumic97ec422010-04-27 14:39:30 +0000414 }
415
416 /* Print TX Rings */
417 if (!netif_msg_tx_done(adapter))
418 goto rx_ring_summary;
419
420 dev_info(&adapter->pdev->dev, "TX Rings Dump\n");
421
422 /* Transmit Descriptor Formats
423 *
424 * Advanced Transmit Descriptor
425 * +--------------------------------------------------------------+
426 * 0 | Buffer Address [63:0] |
427 * +--------------------------------------------------------------+
428 * 8 | PAYLEN | PORTS |CC|IDX | STA | DCMD |DTYP|MAC|RSV| DTALEN |
429 * +--------------------------------------------------------------+
430 * 63 46 45 40 39 38 36 35 32 31 24 15 0
431 */
432
433 for (n = 0; n < adapter->num_tx_queues; n++) {
434 tx_ring = adapter->tx_ring[n];
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000435 pr_info("------------------------------------\n");
436 pr_info("TX QUEUE INDEX = %d\n", tx_ring->queue_index);
437 pr_info("------------------------------------\n");
438 pr_info("T [desc] [address 63:0 ] [PlPOCIStDDM Ln] "
439 "[bi->dma ] leng ntw timestamp "
440 "bi->skb\n");
Taku Izumic97ec422010-04-27 14:39:30 +0000441
442 for (i = 0; tx_ring->desc && (i < tx_ring->count); i++) {
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000443 const char *next_desc;
Alexander Duyck06034642011-08-26 07:44:22 +0000444 struct igb_tx_buffer *buffer_info;
Alexander Duyck601369062011-08-26 07:44:05 +0000445 tx_desc = IGB_TX_DESC(tx_ring, i);
Alexander Duyck06034642011-08-26 07:44:22 +0000446 buffer_info = &tx_ring->tx_buffer_info[i];
Taku Izumic97ec422010-04-27 14:39:30 +0000447 u0 = (struct my_u0 *)tx_desc;
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000448 if (i == tx_ring->next_to_use &&
449 i == tx_ring->next_to_clean)
450 next_desc = " NTC/U";
451 else if (i == tx_ring->next_to_use)
452 next_desc = " NTU";
453 else if (i == tx_ring->next_to_clean)
454 next_desc = " NTC";
455 else
456 next_desc = "";
457
458 pr_info("T [0x%03X] %016llX %016llX %016llX"
459 " %04X %p %016llX %p%s\n", i,
Taku Izumic97ec422010-04-27 14:39:30 +0000460 le64_to_cpu(u0->a),
461 le64_to_cpu(u0->b),
Alexander Duyckc9f14bf32012-09-18 01:56:27 +0000462 (u64)dma_unmap_addr(buffer_info, dma),
463 dma_unmap_len(buffer_info, len),
Taku Izumic97ec422010-04-27 14:39:30 +0000464 buffer_info->next_to_watch,
465 (u64)buffer_info->time_stamp,
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000466 buffer_info->skb, next_desc);
Taku Izumic97ec422010-04-27 14:39:30 +0000467
Emil Tantilovb6695882012-07-28 05:07:48 +0000468 if (netif_msg_pktdata(adapter) && buffer_info->skb)
Taku Izumic97ec422010-04-27 14:39:30 +0000469 print_hex_dump(KERN_INFO, "",
470 DUMP_PREFIX_ADDRESS,
Emil Tantilovb6695882012-07-28 05:07:48 +0000471 16, 1, buffer_info->skb->data,
Alexander Duyckc9f14bf32012-09-18 01:56:27 +0000472 dma_unmap_len(buffer_info, len),
473 true);
Taku Izumic97ec422010-04-27 14:39:30 +0000474 }
475 }
476
477 /* Print RX Rings Summary */
478rx_ring_summary:
479 dev_info(&adapter->pdev->dev, "RX Rings Summary\n");
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000480 pr_info("Queue [NTU] [NTC]\n");
Taku Izumic97ec422010-04-27 14:39:30 +0000481 for (n = 0; n < adapter->num_rx_queues; n++) {
482 rx_ring = adapter->rx_ring[n];
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000483 pr_info(" %5d %5X %5X\n",
484 n, rx_ring->next_to_use, rx_ring->next_to_clean);
Taku Izumic97ec422010-04-27 14:39:30 +0000485 }
486
487 /* Print RX Rings */
488 if (!netif_msg_rx_status(adapter))
489 goto exit;
490
491 dev_info(&adapter->pdev->dev, "RX Rings Dump\n");
492
493 /* Advanced Receive Descriptor (Read) Format
494 * 63 1 0
495 * +-----------------------------------------------------+
496 * 0 | Packet Buffer Address [63:1] |A0/NSE|
497 * +----------------------------------------------+------+
498 * 8 | Header Buffer Address [63:1] | DD |
499 * +-----------------------------------------------------+
500 *
501 *
502 * Advanced Receive Descriptor (Write-Back) Format
503 *
504 * 63 48 47 32 31 30 21 20 17 16 4 3 0
505 * +------------------------------------------------------+
506 * 0 | Packet IP |SPH| HDR_LEN | RSV|Packet| RSS |
507 * | Checksum Ident | | | | Type | Type |
508 * +------------------------------------------------------+
509 * 8 | VLAN Tag | Length | Extended Error | Extended Status |
510 * +------------------------------------------------------+
511 * 63 48 47 32 31 20 19 0
512 */
513
514 for (n = 0; n < adapter->num_rx_queues; n++) {
515 rx_ring = adapter->rx_ring[n];
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000516 pr_info("------------------------------------\n");
517 pr_info("RX QUEUE INDEX = %d\n", rx_ring->queue_index);
518 pr_info("------------------------------------\n");
519 pr_info("R [desc] [ PktBuf A0] [ HeadBuf DD] "
520 "[bi->dma ] [bi->skb] <-- Adv Rx Read format\n");
521 pr_info("RWB[desc] [PcsmIpSHl PtRs] [vl er S cks ln] -----"
522 "----------- [bi->skb] <-- Adv Rx Write-Back format\n");
Taku Izumic97ec422010-04-27 14:39:30 +0000523
524 for (i = 0; i < rx_ring->count; i++) {
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000525 const char *next_desc;
Alexander Duyck06034642011-08-26 07:44:22 +0000526 struct igb_rx_buffer *buffer_info;
527 buffer_info = &rx_ring->rx_buffer_info[i];
Alexander Duyck601369062011-08-26 07:44:05 +0000528 rx_desc = IGB_RX_DESC(rx_ring, i);
Taku Izumic97ec422010-04-27 14:39:30 +0000529 u0 = (struct my_u0 *)rx_desc;
530 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000531
532 if (i == rx_ring->next_to_use)
533 next_desc = " NTU";
534 else if (i == rx_ring->next_to_clean)
535 next_desc = " NTC";
536 else
537 next_desc = "";
538
Taku Izumic97ec422010-04-27 14:39:30 +0000539 if (staterr & E1000_RXD_STAT_DD) {
540 /* Descriptor Done */
Alexander Duyck1a1c2252012-09-25 00:30:52 +0000541 pr_info("%s[0x%03X] %016llX %016llX ---------------- %s\n",
542 "RWB", i,
Taku Izumic97ec422010-04-27 14:39:30 +0000543 le64_to_cpu(u0->a),
544 le64_to_cpu(u0->b),
Alexander Duyck1a1c2252012-09-25 00:30:52 +0000545 next_desc);
Taku Izumic97ec422010-04-27 14:39:30 +0000546 } else {
Alexander Duyck1a1c2252012-09-25 00:30:52 +0000547 pr_info("%s[0x%03X] %016llX %016llX %016llX %s\n",
548 "R ", i,
Taku Izumic97ec422010-04-27 14:39:30 +0000549 le64_to_cpu(u0->a),
550 le64_to_cpu(u0->b),
551 (u64)buffer_info->dma,
Alexander Duyck1a1c2252012-09-25 00:30:52 +0000552 next_desc);
Taku Izumic97ec422010-04-27 14:39:30 +0000553
Emil Tantilovb6695882012-07-28 05:07:48 +0000554 if (netif_msg_pktdata(adapter) &&
Alexander Duyck1a1c2252012-09-25 00:30:52 +0000555 buffer_info->dma && buffer_info->page) {
Alexander Duyck44390ca2011-08-26 07:43:38 +0000556 print_hex_dump(KERN_INFO, "",
557 DUMP_PREFIX_ADDRESS,
558 16, 1,
Emil Tantilovb6695882012-07-28 05:07:48 +0000559 page_address(buffer_info->page) +
560 buffer_info->page_offset,
Alexander Duyckde78d1f2012-09-25 00:31:12 +0000561 IGB_RX_BUFSZ, true);
Taku Izumic97ec422010-04-27 14:39:30 +0000562 }
563 }
Taku Izumic97ec422010-04-27 14:39:30 +0000564 }
565 }
566
567exit:
568 return;
569}
570
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +0000571/* igb_get_i2c_data - Reads the I2C SDA data bit
572 * @hw: pointer to hardware structure
573 * @i2cctl: Current value of I2CCTL register
574 *
575 * Returns the I2C data bit value
576 */
577static int igb_get_i2c_data(void *data)
578{
579 struct igb_adapter *adapter = (struct igb_adapter *)data;
580 struct e1000_hw *hw = &adapter->hw;
581 s32 i2cctl = rd32(E1000_I2CPARAMS);
582
583 return ((i2cctl & E1000_I2C_DATA_IN) != 0);
584}
585
586/* igb_set_i2c_data - Sets the I2C data bit
587 * @data: pointer to hardware structure
588 * @state: I2C data value (0 or 1) to set
589 *
590 * Sets the I2C data bit
591 */
592static void igb_set_i2c_data(void *data, int state)
593{
594 struct igb_adapter *adapter = (struct igb_adapter *)data;
595 struct e1000_hw *hw = &adapter->hw;
596 s32 i2cctl = rd32(E1000_I2CPARAMS);
597
598 if (state)
599 i2cctl |= E1000_I2C_DATA_OUT;
600 else
601 i2cctl &= ~E1000_I2C_DATA_OUT;
602
603 i2cctl &= ~E1000_I2C_DATA_OE_N;
604 i2cctl |= E1000_I2C_CLK_OE_N;
605 wr32(E1000_I2CPARAMS, i2cctl);
606 wrfl();
607
608}
609
610/* igb_set_i2c_clk - Sets the I2C SCL clock
611 * @data: pointer to hardware structure
612 * @state: state to set clock
613 *
614 * Sets the I2C clock line to state
615 */
616static void igb_set_i2c_clk(void *data, int state)
617{
618 struct igb_adapter *adapter = (struct igb_adapter *)data;
619 struct e1000_hw *hw = &adapter->hw;
620 s32 i2cctl = rd32(E1000_I2CPARAMS);
621
622 if (state) {
623 i2cctl |= E1000_I2C_CLK_OUT;
624 i2cctl &= ~E1000_I2C_CLK_OE_N;
625 } else {
626 i2cctl &= ~E1000_I2C_CLK_OUT;
627 i2cctl &= ~E1000_I2C_CLK_OE_N;
628 }
629 wr32(E1000_I2CPARAMS, i2cctl);
630 wrfl();
631}
632
633/* igb_get_i2c_clk - Gets the I2C SCL clock state
634 * @data: pointer to hardware structure
635 *
636 * Gets the I2C clock state
637 */
638static int igb_get_i2c_clk(void *data)
639{
640 struct igb_adapter *adapter = (struct igb_adapter *)data;
641 struct e1000_hw *hw = &adapter->hw;
642 s32 i2cctl = rd32(E1000_I2CPARAMS);
643
644 return ((i2cctl & E1000_I2C_CLK_IN) != 0);
645}
646
647static const struct i2c_algo_bit_data igb_i2c_algo = {
648 .setsda = igb_set_i2c_data,
649 .setscl = igb_set_i2c_clk,
650 .getsda = igb_get_i2c_data,
651 .getscl = igb_get_i2c_clk,
652 .udelay = 5,
653 .timeout = 20,
654};
655
Auke Kok9d5c8242008-01-24 02:22:38 -0800656/**
Alexander Duyckc0410762010-03-25 13:10:08 +0000657 * igb_get_hw_dev - return device
Auke Kok9d5c8242008-01-24 02:22:38 -0800658 * used by hardware layer to print debugging information
659 **/
Alexander Duyckc0410762010-03-25 13:10:08 +0000660struct net_device *igb_get_hw_dev(struct e1000_hw *hw)
Auke Kok9d5c8242008-01-24 02:22:38 -0800661{
662 struct igb_adapter *adapter = hw->back;
Alexander Duyckc0410762010-03-25 13:10:08 +0000663 return adapter->netdev;
Auke Kok9d5c8242008-01-24 02:22:38 -0800664}
Patrick Ohly38c845c2009-02-12 05:03:41 +0000665
666/**
Auke Kok9d5c8242008-01-24 02:22:38 -0800667 * igb_init_module - Driver Registration Routine
668 *
669 * igb_init_module is the first routine called when the driver is
670 * loaded. All it does is register with the PCI subsystem.
671 **/
672static int __init igb_init_module(void)
673{
674 int ret;
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000675 pr_info("%s - version %s\n",
Auke Kok9d5c8242008-01-24 02:22:38 -0800676 igb_driver_string, igb_driver_version);
677
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000678 pr_info("%s\n", igb_copyright);
Auke Kok9d5c8242008-01-24 02:22:38 -0800679
Jeff Kirsher421e02f2008-10-17 11:08:31 -0700680#ifdef CONFIG_IGB_DCA
Jeb Cramerfe4506b2008-07-08 15:07:55 -0700681 dca_register_notify(&dca_notifier);
682#endif
Alexander Duyckbbd98fe2009-01-31 00:52:30 -0800683 ret = pci_register_driver(&igb_driver);
Auke Kok9d5c8242008-01-24 02:22:38 -0800684 return ret;
685}
686
687module_init(igb_init_module);
688
689/**
690 * igb_exit_module - Driver Exit Cleanup Routine
691 *
692 * igb_exit_module is called just before the driver is removed
693 * from memory.
694 **/
695static void __exit igb_exit_module(void)
696{
Jeff Kirsher421e02f2008-10-17 11:08:31 -0700697#ifdef CONFIG_IGB_DCA
Jeb Cramerfe4506b2008-07-08 15:07:55 -0700698 dca_unregister_notify(&dca_notifier);
699#endif
Auke Kok9d5c8242008-01-24 02:22:38 -0800700 pci_unregister_driver(&igb_driver);
701}
702
703module_exit(igb_exit_module);
704
Alexander Duyck26bc19e2008-12-26 01:34:11 -0800705#define Q_IDX_82576(i) (((i & 0x1) << 3) + (i >> 1))
706/**
707 * igb_cache_ring_register - Descriptor ring to register mapping
708 * @adapter: board private structure to initialize
709 *
710 * Once we know the feature-set enabled for the device, we'll cache
711 * the register offset the descriptor ring is assigned to.
712 **/
713static void igb_cache_ring_register(struct igb_adapter *adapter)
714{
Alexander Duyckee1b9f02009-10-27 23:49:40 +0000715 int i = 0, j = 0;
Alexander Duyck047e0032009-10-27 15:49:27 +0000716 u32 rbase_offset = adapter->vfs_allocated_count;
Alexander Duyck26bc19e2008-12-26 01:34:11 -0800717
718 switch (adapter->hw.mac.type) {
719 case e1000_82576:
720 /* The queues are allocated for virtualization such that VF 0
721 * is allocated queues 0 and 8, VF 1 queues 1 and 9, etc.
722 * In order to avoid collision we start at the first free queue
723 * and continue consuming queues in the same sequence
724 */
Alexander Duyckee1b9f02009-10-27 23:49:40 +0000725 if (adapter->vfs_allocated_count) {
Alexander Duycka99955f2009-11-12 18:37:19 +0000726 for (; i < adapter->rss_queues; i++)
Alexander Duyck3025a442010-02-17 01:02:39 +0000727 adapter->rx_ring[i]->reg_idx = rbase_offset +
728 Q_IDX_82576(i);
Alexander Duyckee1b9f02009-10-27 23:49:40 +0000729 }
Alexander Duyck26bc19e2008-12-26 01:34:11 -0800730 case e1000_82575:
Alexander Duyck55cac242009-11-19 12:42:21 +0000731 case e1000_82580:
Alexander Duyckd2ba2ed2010-03-22 14:08:06 +0000732 case e1000_i350:
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +0000733 case e1000_i210:
734 case e1000_i211:
Alexander Duyck26bc19e2008-12-26 01:34:11 -0800735 default:
Alexander Duyckee1b9f02009-10-27 23:49:40 +0000736 for (; i < adapter->num_rx_queues; i++)
Alexander Duyck3025a442010-02-17 01:02:39 +0000737 adapter->rx_ring[i]->reg_idx = rbase_offset + i;
Alexander Duyckee1b9f02009-10-27 23:49:40 +0000738 for (; j < adapter->num_tx_queues; j++)
Alexander Duyck3025a442010-02-17 01:02:39 +0000739 adapter->tx_ring[j]->reg_idx = rbase_offset + j;
Alexander Duyck26bc19e2008-12-26 01:34:11 -0800740 break;
741 }
742}
743
Alexander Duyck4be000c2011-08-26 07:45:52 +0000744/**
745 * igb_write_ivar - configure ivar for given MSI-X vector
746 * @hw: pointer to the HW structure
747 * @msix_vector: vector number we are allocating to a given ring
748 * @index: row index of IVAR register to write within IVAR table
749 * @offset: column offset of in IVAR, should be multiple of 8
750 *
751 * This function is intended to handle the writing of the IVAR register
752 * for adapters 82576 and newer. The IVAR table consists of 2 columns,
753 * each containing an cause allocation for an Rx and Tx ring, and a
754 * variable number of rows depending on the number of queues supported.
755 **/
756static void igb_write_ivar(struct e1000_hw *hw, int msix_vector,
757 int index, int offset)
758{
759 u32 ivar = array_rd32(E1000_IVAR0, index);
760
761 /* clear any bits that are currently set */
762 ivar &= ~((u32)0xFF << offset);
763
764 /* write vector and valid bit */
765 ivar |= (msix_vector | E1000_IVAR_VALID) << offset;
766
767 array_wr32(E1000_IVAR0, index, ivar);
768}
769
Auke Kok9d5c8242008-01-24 02:22:38 -0800770#define IGB_N0_QUEUE -1
Alexander Duyck047e0032009-10-27 15:49:27 +0000771static void igb_assign_vector(struct igb_q_vector *q_vector, int msix_vector)
Auke Kok9d5c8242008-01-24 02:22:38 -0800772{
Alexander Duyck047e0032009-10-27 15:49:27 +0000773 struct igb_adapter *adapter = q_vector->adapter;
Auke Kok9d5c8242008-01-24 02:22:38 -0800774 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck047e0032009-10-27 15:49:27 +0000775 int rx_queue = IGB_N0_QUEUE;
776 int tx_queue = IGB_N0_QUEUE;
Alexander Duyck4be000c2011-08-26 07:45:52 +0000777 u32 msixbm = 0;
Alexander Duyck047e0032009-10-27 15:49:27 +0000778
Alexander Duyck0ba82992011-08-26 07:45:47 +0000779 if (q_vector->rx.ring)
780 rx_queue = q_vector->rx.ring->reg_idx;
781 if (q_vector->tx.ring)
782 tx_queue = q_vector->tx.ring->reg_idx;
Alexander Duyck2d064c02008-07-08 15:10:12 -0700783
784 switch (hw->mac.type) {
785 case e1000_82575:
Auke Kok9d5c8242008-01-24 02:22:38 -0800786 /* The 82575 assigns vectors using a bitmask, which matches the
787 bitmask for the EICR/EIMS/EIMC registers. To assign one
788 or more queues to a vector, we write the appropriate bits
789 into the MSIXBM register for that vector. */
Alexander Duyck047e0032009-10-27 15:49:27 +0000790 if (rx_queue > IGB_N0_QUEUE)
Auke Kok9d5c8242008-01-24 02:22:38 -0800791 msixbm = E1000_EICR_RX_QUEUE0 << rx_queue;
Alexander Duyck047e0032009-10-27 15:49:27 +0000792 if (tx_queue > IGB_N0_QUEUE)
Auke Kok9d5c8242008-01-24 02:22:38 -0800793 msixbm |= E1000_EICR_TX_QUEUE0 << tx_queue;
Alexander Duyckfeeb2722010-02-03 21:59:51 +0000794 if (!adapter->msix_entries && msix_vector == 0)
795 msixbm |= E1000_EIMS_OTHER;
Auke Kok9d5c8242008-01-24 02:22:38 -0800796 array_wr32(E1000_MSIXBM(0), msix_vector, msixbm);
Alexander Duyck047e0032009-10-27 15:49:27 +0000797 q_vector->eims_value = msixbm;
Alexander Duyck2d064c02008-07-08 15:10:12 -0700798 break;
799 case e1000_82576:
Alexander Duyck4be000c2011-08-26 07:45:52 +0000800 /*
801 * 82576 uses a table that essentially consists of 2 columns
802 * with 8 rows. The ordering is column-major so we use the
803 * lower 3 bits as the row index, and the 4th bit as the
804 * column offset.
805 */
806 if (rx_queue > IGB_N0_QUEUE)
807 igb_write_ivar(hw, msix_vector,
808 rx_queue & 0x7,
809 (rx_queue & 0x8) << 1);
810 if (tx_queue > IGB_N0_QUEUE)
811 igb_write_ivar(hw, msix_vector,
812 tx_queue & 0x7,
813 ((tx_queue & 0x8) << 1) + 8);
Alexander Duyck047e0032009-10-27 15:49:27 +0000814 q_vector->eims_value = 1 << msix_vector;
Alexander Duyck2d064c02008-07-08 15:10:12 -0700815 break;
Alexander Duyck55cac242009-11-19 12:42:21 +0000816 case e1000_82580:
Alexander Duyckd2ba2ed2010-03-22 14:08:06 +0000817 case e1000_i350:
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +0000818 case e1000_i210:
819 case e1000_i211:
Alexander Duyck4be000c2011-08-26 07:45:52 +0000820 /*
821 * On 82580 and newer adapters the scheme is similar to 82576
822 * however instead of ordering column-major we have things
823 * ordered row-major. So we traverse the table by using
824 * bit 0 as the column offset, and the remaining bits as the
825 * row index.
826 */
827 if (rx_queue > IGB_N0_QUEUE)
828 igb_write_ivar(hw, msix_vector,
829 rx_queue >> 1,
830 (rx_queue & 0x1) << 4);
831 if (tx_queue > IGB_N0_QUEUE)
832 igb_write_ivar(hw, msix_vector,
833 tx_queue >> 1,
834 ((tx_queue & 0x1) << 4) + 8);
Alexander Duyck55cac242009-11-19 12:42:21 +0000835 q_vector->eims_value = 1 << msix_vector;
836 break;
Alexander Duyck2d064c02008-07-08 15:10:12 -0700837 default:
838 BUG();
839 break;
840 }
Alexander Duyck26b39272010-02-17 01:00:41 +0000841
842 /* add q_vector eims value to global eims_enable_mask */
843 adapter->eims_enable_mask |= q_vector->eims_value;
844
845 /* configure q_vector to set itr on first interrupt */
846 q_vector->set_itr = 1;
Auke Kok9d5c8242008-01-24 02:22:38 -0800847}
848
849/**
850 * igb_configure_msix - Configure MSI-X hardware
851 *
852 * igb_configure_msix sets up the hardware to properly
853 * generate MSI-X interrupts.
854 **/
855static void igb_configure_msix(struct igb_adapter *adapter)
856{
857 u32 tmp;
858 int i, vector = 0;
859 struct e1000_hw *hw = &adapter->hw;
860
861 adapter->eims_enable_mask = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -0800862
863 /* set vector for other causes, i.e. link changes */
Alexander Duyck2d064c02008-07-08 15:10:12 -0700864 switch (hw->mac.type) {
865 case e1000_82575:
Auke Kok9d5c8242008-01-24 02:22:38 -0800866 tmp = rd32(E1000_CTRL_EXT);
867 /* enable MSI-X PBA support*/
868 tmp |= E1000_CTRL_EXT_PBA_CLR;
869
870 /* Auto-Mask interrupts upon ICR read. */
871 tmp |= E1000_CTRL_EXT_EIAME;
872 tmp |= E1000_CTRL_EXT_IRCA;
873
874 wr32(E1000_CTRL_EXT, tmp);
Alexander Duyck047e0032009-10-27 15:49:27 +0000875
876 /* enable msix_other interrupt */
877 array_wr32(E1000_MSIXBM(0), vector++,
878 E1000_EIMS_OTHER);
PJ Waskiewicz844290e2008-06-27 11:00:39 -0700879 adapter->eims_other = E1000_EIMS_OTHER;
Auke Kok9d5c8242008-01-24 02:22:38 -0800880
Alexander Duyck2d064c02008-07-08 15:10:12 -0700881 break;
882
883 case e1000_82576:
Alexander Duyck55cac242009-11-19 12:42:21 +0000884 case e1000_82580:
Alexander Duyckd2ba2ed2010-03-22 14:08:06 +0000885 case e1000_i350:
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +0000886 case e1000_i210:
887 case e1000_i211:
Alexander Duyck047e0032009-10-27 15:49:27 +0000888 /* Turn on MSI-X capability first, or our settings
889 * won't stick. And it will take days to debug. */
890 wr32(E1000_GPIE, E1000_GPIE_MSIX_MODE |
891 E1000_GPIE_PBA | E1000_GPIE_EIAME |
892 E1000_GPIE_NSICR);
Alexander Duyck2d064c02008-07-08 15:10:12 -0700893
Alexander Duyck047e0032009-10-27 15:49:27 +0000894 /* enable msix_other interrupt */
895 adapter->eims_other = 1 << vector;
896 tmp = (vector++ | E1000_IVAR_VALID) << 8;
897
898 wr32(E1000_IVAR_MISC, tmp);
Alexander Duyck2d064c02008-07-08 15:10:12 -0700899 break;
900 default:
901 /* do nothing, since nothing else supports MSI-X */
902 break;
903 } /* switch (hw->mac.type) */
Alexander Duyck047e0032009-10-27 15:49:27 +0000904
905 adapter->eims_enable_mask |= adapter->eims_other;
906
Alexander Duyck26b39272010-02-17 01:00:41 +0000907 for (i = 0; i < adapter->num_q_vectors; i++)
908 igb_assign_vector(adapter->q_vector[i], vector++);
Alexander Duyck047e0032009-10-27 15:49:27 +0000909
Auke Kok9d5c8242008-01-24 02:22:38 -0800910 wrfl();
911}
912
913/**
914 * igb_request_msix - Initialize MSI-X interrupts
915 *
916 * igb_request_msix allocates MSI-X vectors and requests interrupts from the
917 * kernel.
918 **/
919static int igb_request_msix(struct igb_adapter *adapter)
920{
921 struct net_device *netdev = adapter->netdev;
Alexander Duyck047e0032009-10-27 15:49:27 +0000922 struct e1000_hw *hw = &adapter->hw;
Stefan Assmann52285b72012-12-04 06:00:17 +0000923 int i, err = 0, vector = 0, free_vector = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -0800924
Auke Kok9d5c8242008-01-24 02:22:38 -0800925 err = request_irq(adapter->msix_entries[vector].vector,
Joe Perchesa0607fd2009-11-18 23:29:17 -0800926 igb_msix_other, 0, netdev->name, adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -0800927 if (err)
Stefan Assmann52285b72012-12-04 06:00:17 +0000928 goto err_out;
Alexander Duyck047e0032009-10-27 15:49:27 +0000929
930 for (i = 0; i < adapter->num_q_vectors; i++) {
931 struct igb_q_vector *q_vector = adapter->q_vector[i];
932
Stefan Assmann52285b72012-12-04 06:00:17 +0000933 vector++;
934
Alexander Duyck047e0032009-10-27 15:49:27 +0000935 q_vector->itr_register = hw->hw_addr + E1000_EITR(vector);
936
Alexander Duyck0ba82992011-08-26 07:45:47 +0000937 if (q_vector->rx.ring && q_vector->tx.ring)
Alexander Duyck047e0032009-10-27 15:49:27 +0000938 sprintf(q_vector->name, "%s-TxRx-%u", netdev->name,
Alexander Duyck0ba82992011-08-26 07:45:47 +0000939 q_vector->rx.ring->queue_index);
940 else if (q_vector->tx.ring)
Alexander Duyck047e0032009-10-27 15:49:27 +0000941 sprintf(q_vector->name, "%s-tx-%u", netdev->name,
Alexander Duyck0ba82992011-08-26 07:45:47 +0000942 q_vector->tx.ring->queue_index);
943 else if (q_vector->rx.ring)
Alexander Duyck047e0032009-10-27 15:49:27 +0000944 sprintf(q_vector->name, "%s-rx-%u", netdev->name,
Alexander Duyck0ba82992011-08-26 07:45:47 +0000945 q_vector->rx.ring->queue_index);
Alexander Duyck047e0032009-10-27 15:49:27 +0000946 else
947 sprintf(q_vector->name, "%s-unused", netdev->name);
948
949 err = request_irq(adapter->msix_entries[vector].vector,
Joe Perchesa0607fd2009-11-18 23:29:17 -0800950 igb_msix_ring, 0, q_vector->name,
Alexander Duyck047e0032009-10-27 15:49:27 +0000951 q_vector);
952 if (err)
Stefan Assmann52285b72012-12-04 06:00:17 +0000953 goto err_free;
Alexander Duyck047e0032009-10-27 15:49:27 +0000954 }
Auke Kok9d5c8242008-01-24 02:22:38 -0800955
Auke Kok9d5c8242008-01-24 02:22:38 -0800956 igb_configure_msix(adapter);
957 return 0;
Stefan Assmann52285b72012-12-04 06:00:17 +0000958
959err_free:
960 /* free already assigned IRQs */
961 free_irq(adapter->msix_entries[free_vector++].vector, adapter);
962
963 vector--;
964 for (i = 0; i < vector; i++) {
965 free_irq(adapter->msix_entries[free_vector++].vector,
966 adapter->q_vector[i]);
967 }
968err_out:
Auke Kok9d5c8242008-01-24 02:22:38 -0800969 return err;
970}
971
972static void igb_reset_interrupt_capability(struct igb_adapter *adapter)
973{
974 if (adapter->msix_entries) {
975 pci_disable_msix(adapter->pdev);
976 kfree(adapter->msix_entries);
977 adapter->msix_entries = NULL;
Alexander Duyck047e0032009-10-27 15:49:27 +0000978 } else if (adapter->flags & IGB_FLAG_HAS_MSI) {
Auke Kok9d5c8242008-01-24 02:22:38 -0800979 pci_disable_msi(adapter->pdev);
Alexander Duyck047e0032009-10-27 15:49:27 +0000980 }
Auke Kok9d5c8242008-01-24 02:22:38 -0800981}
982
Alexander Duyck047e0032009-10-27 15:49:27 +0000983/**
Alexander Duyck5536d212012-09-25 00:31:17 +0000984 * igb_free_q_vector - Free memory allocated for specific interrupt vector
985 * @adapter: board private structure to initialize
986 * @v_idx: Index of vector to be freed
987 *
988 * This function frees the memory allocated to the q_vector. In addition if
989 * NAPI is enabled it will delete any references to the NAPI struct prior
990 * to freeing the q_vector.
991 **/
992static void igb_free_q_vector(struct igb_adapter *adapter, int v_idx)
993{
994 struct igb_q_vector *q_vector = adapter->q_vector[v_idx];
995
996 if (q_vector->tx.ring)
997 adapter->tx_ring[q_vector->tx.ring->queue_index] = NULL;
998
999 if (q_vector->rx.ring)
1000 adapter->tx_ring[q_vector->rx.ring->queue_index] = NULL;
1001
1002 adapter->q_vector[v_idx] = NULL;
1003 netif_napi_del(&q_vector->napi);
1004
1005 /*
1006 * ixgbe_get_stats64() might access the rings on this vector,
1007 * we must wait a grace period before freeing it.
1008 */
1009 kfree_rcu(q_vector, rcu);
1010}
1011
1012/**
Alexander Duyck047e0032009-10-27 15:49:27 +00001013 * igb_free_q_vectors - Free memory allocated for interrupt vectors
1014 * @adapter: board private structure to initialize
1015 *
1016 * This function frees the memory allocated to the q_vectors. In addition if
1017 * NAPI is enabled it will delete any references to the NAPI struct prior
1018 * to freeing the q_vector.
1019 **/
1020static void igb_free_q_vectors(struct igb_adapter *adapter)
1021{
Alexander Duyck5536d212012-09-25 00:31:17 +00001022 int v_idx = adapter->num_q_vectors;
Alexander Duyck047e0032009-10-27 15:49:27 +00001023
Alexander Duyck5536d212012-09-25 00:31:17 +00001024 adapter->num_tx_queues = 0;
1025 adapter->num_rx_queues = 0;
Alexander Duyck047e0032009-10-27 15:49:27 +00001026 adapter->num_q_vectors = 0;
Alexander Duyck5536d212012-09-25 00:31:17 +00001027
1028 while (v_idx--)
1029 igb_free_q_vector(adapter, v_idx);
Alexander Duyck047e0032009-10-27 15:49:27 +00001030}
1031
1032/**
1033 * igb_clear_interrupt_scheme - reset the device to a state of no interrupts
1034 *
1035 * This function resets the device so that it has 0 rx queues, tx queues, and
1036 * MSI-X interrupts allocated.
1037 */
1038static void igb_clear_interrupt_scheme(struct igb_adapter *adapter)
1039{
Alexander Duyck047e0032009-10-27 15:49:27 +00001040 igb_free_q_vectors(adapter);
1041 igb_reset_interrupt_capability(adapter);
1042}
Auke Kok9d5c8242008-01-24 02:22:38 -08001043
1044/**
1045 * igb_set_interrupt_capability - set MSI or MSI-X if supported
1046 *
1047 * Attempt to configure interrupts using the best available
1048 * capabilities of the hardware and kernel.
1049 **/
Stefan Assmann53c7d062012-12-04 06:00:12 +00001050static void igb_set_interrupt_capability(struct igb_adapter *adapter, bool msix)
Auke Kok9d5c8242008-01-24 02:22:38 -08001051{
1052 int err;
1053 int numvecs, i;
1054
Stefan Assmann53c7d062012-12-04 06:00:12 +00001055 if (!msix)
1056 goto msi_only;
1057
Alexander Duyck83b71802009-02-06 23:15:45 +00001058 /* Number of supported queues. */
Alexander Duycka99955f2009-11-12 18:37:19 +00001059 adapter->num_rx_queues = adapter->rss_queues;
Greg Rose5fa85172010-07-01 13:38:16 +00001060 if (adapter->vfs_allocated_count)
1061 adapter->num_tx_queues = 1;
1062 else
1063 adapter->num_tx_queues = adapter->rss_queues;
Alexander Duyck83b71802009-02-06 23:15:45 +00001064
Alexander Duyck047e0032009-10-27 15:49:27 +00001065 /* start with one vector for every rx queue */
1066 numvecs = adapter->num_rx_queues;
1067
Daniel Mack3ad2f3f2010-02-03 08:01:28 +08001068 /* if tx handler is separate add 1 for every tx queue */
Alexander Duycka99955f2009-11-12 18:37:19 +00001069 if (!(adapter->flags & IGB_FLAG_QUEUE_PAIRS))
1070 numvecs += adapter->num_tx_queues;
Alexander Duyck047e0032009-10-27 15:49:27 +00001071
1072 /* store the number of vectors reserved for queues */
1073 adapter->num_q_vectors = numvecs;
1074
1075 /* add 1 vector for link status interrupts */
1076 numvecs++;
Auke Kok9d5c8242008-01-24 02:22:38 -08001077 adapter->msix_entries = kcalloc(numvecs, sizeof(struct msix_entry),
1078 GFP_KERNEL);
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00001079
Auke Kok9d5c8242008-01-24 02:22:38 -08001080 if (!adapter->msix_entries)
1081 goto msi_only;
1082
1083 for (i = 0; i < numvecs; i++)
1084 adapter->msix_entries[i].entry = i;
1085
1086 err = pci_enable_msix(adapter->pdev,
1087 adapter->msix_entries,
1088 numvecs);
1089 if (err == 0)
Alexander Duyck0c2cc022012-09-25 00:31:22 +00001090 return;
Auke Kok9d5c8242008-01-24 02:22:38 -08001091
1092 igb_reset_interrupt_capability(adapter);
1093
1094 /* If we can't do MSI-X, try MSI */
1095msi_only:
Alexander Duyck2a3abf62009-04-07 14:37:52 +00001096#ifdef CONFIG_PCI_IOV
1097 /* disable SR-IOV for non MSI-X configurations */
1098 if (adapter->vf_data) {
1099 struct e1000_hw *hw = &adapter->hw;
1100 /* disable iov and allow time for transactions to clear */
1101 pci_disable_sriov(adapter->pdev);
1102 msleep(500);
1103
1104 kfree(adapter->vf_data);
1105 adapter->vf_data = NULL;
1106 wr32(E1000_IOVCTL, E1000_IOVCTL_REUSE_VFQ);
Jesse Brandeburg945a5152011-07-20 00:56:21 +00001107 wrfl();
Alexander Duyck2a3abf62009-04-07 14:37:52 +00001108 msleep(100);
1109 dev_info(&adapter->pdev->dev, "IOV Disabled\n");
1110 }
1111#endif
Alexander Duyck4fc82ad2009-10-27 23:45:42 +00001112 adapter->vfs_allocated_count = 0;
Alexander Duycka99955f2009-11-12 18:37:19 +00001113 adapter->rss_queues = 1;
Alexander Duyck4fc82ad2009-10-27 23:45:42 +00001114 adapter->flags |= IGB_FLAG_QUEUE_PAIRS;
Auke Kok9d5c8242008-01-24 02:22:38 -08001115 adapter->num_rx_queues = 1;
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07001116 adapter->num_tx_queues = 1;
Alexander Duyck047e0032009-10-27 15:49:27 +00001117 adapter->num_q_vectors = 1;
Auke Kok9d5c8242008-01-24 02:22:38 -08001118 if (!pci_enable_msi(adapter->pdev))
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07001119 adapter->flags |= IGB_FLAG_HAS_MSI;
Auke Kok9d5c8242008-01-24 02:22:38 -08001120}
1121
Alexander Duyck5536d212012-09-25 00:31:17 +00001122static void igb_add_ring(struct igb_ring *ring,
1123 struct igb_ring_container *head)
1124{
1125 head->ring = ring;
1126 head->count++;
1127}
1128
1129/**
1130 * igb_alloc_q_vector - Allocate memory for a single interrupt vector
1131 * @adapter: board private structure to initialize
1132 * @v_count: q_vectors allocated on adapter, used for ring interleaving
1133 * @v_idx: index of vector in adapter struct
1134 * @txr_count: total number of Tx rings to allocate
1135 * @txr_idx: index of first Tx ring to allocate
1136 * @rxr_count: total number of Rx rings to allocate
1137 * @rxr_idx: index of first Rx ring to allocate
1138 *
1139 * We allocate one q_vector. If allocation fails we return -ENOMEM.
1140 **/
1141static int igb_alloc_q_vector(struct igb_adapter *adapter,
1142 int v_count, int v_idx,
1143 int txr_count, int txr_idx,
1144 int rxr_count, int rxr_idx)
1145{
1146 struct igb_q_vector *q_vector;
1147 struct igb_ring *ring;
1148 int ring_count, size;
1149
1150 /* igb only supports 1 Tx and/or 1 Rx queue per vector */
1151 if (txr_count > 1 || rxr_count > 1)
1152 return -ENOMEM;
1153
1154 ring_count = txr_count + rxr_count;
1155 size = sizeof(struct igb_q_vector) +
1156 (sizeof(struct igb_ring) * ring_count);
1157
1158 /* allocate q_vector and rings */
1159 q_vector = kzalloc(size, GFP_KERNEL);
1160 if (!q_vector)
1161 return -ENOMEM;
1162
1163 /* initialize NAPI */
1164 netif_napi_add(adapter->netdev, &q_vector->napi,
1165 igb_poll, 64);
1166
1167 /* tie q_vector and adapter together */
1168 adapter->q_vector[v_idx] = q_vector;
1169 q_vector->adapter = adapter;
1170
1171 /* initialize work limits */
1172 q_vector->tx.work_limit = adapter->tx_work_limit;
1173
1174 /* initialize ITR configuration */
1175 q_vector->itr_register = adapter->hw.hw_addr + E1000_EITR(0);
1176 q_vector->itr_val = IGB_START_ITR;
1177
1178 /* initialize pointer to rings */
1179 ring = q_vector->ring;
1180
1181 if (txr_count) {
1182 /* assign generic ring traits */
1183 ring->dev = &adapter->pdev->dev;
1184 ring->netdev = adapter->netdev;
1185
1186 /* configure backlink on ring */
1187 ring->q_vector = q_vector;
1188
1189 /* update q_vector Tx values */
1190 igb_add_ring(ring, &q_vector->tx);
1191
1192 /* For 82575, context index must be unique per ring. */
1193 if (adapter->hw.mac.type == e1000_82575)
1194 set_bit(IGB_RING_FLAG_TX_CTX_IDX, &ring->flags);
1195
1196 /* apply Tx specific ring traits */
1197 ring->count = adapter->tx_ring_count;
1198 ring->queue_index = txr_idx;
1199
1200 /* assign ring to adapter */
1201 adapter->tx_ring[txr_idx] = ring;
1202
1203 /* push pointer to next ring */
1204 ring++;
1205 }
1206
1207 if (rxr_count) {
1208 /* assign generic ring traits */
1209 ring->dev = &adapter->pdev->dev;
1210 ring->netdev = adapter->netdev;
1211
1212 /* configure backlink on ring */
1213 ring->q_vector = q_vector;
1214
1215 /* update q_vector Rx values */
1216 igb_add_ring(ring, &q_vector->rx);
1217
1218 /* set flag indicating ring supports SCTP checksum offload */
1219 if (adapter->hw.mac.type >= e1000_82576)
1220 set_bit(IGB_RING_FLAG_RX_SCTP_CSUM, &ring->flags);
1221
1222 /*
1223 * On i350, i210, and i211, loopback VLAN packets
1224 * have the tag byte-swapped.
1225 * */
1226 if (adapter->hw.mac.type >= e1000_i350)
1227 set_bit(IGB_RING_FLAG_RX_LB_VLAN_BSWAP, &ring->flags);
1228
1229 /* apply Rx specific ring traits */
1230 ring->count = adapter->rx_ring_count;
1231 ring->queue_index = rxr_idx;
1232
1233 /* assign ring to adapter */
1234 adapter->rx_ring[rxr_idx] = ring;
1235 }
1236
1237 return 0;
1238}
1239
1240
Auke Kok9d5c8242008-01-24 02:22:38 -08001241/**
Alexander Duyck047e0032009-10-27 15:49:27 +00001242 * igb_alloc_q_vectors - Allocate memory for interrupt vectors
1243 * @adapter: board private structure to initialize
1244 *
1245 * We allocate one q_vector per queue interrupt. If allocation fails we
1246 * return -ENOMEM.
1247 **/
1248static int igb_alloc_q_vectors(struct igb_adapter *adapter)
1249{
Alexander Duyck5536d212012-09-25 00:31:17 +00001250 int q_vectors = adapter->num_q_vectors;
1251 int rxr_remaining = adapter->num_rx_queues;
1252 int txr_remaining = adapter->num_tx_queues;
1253 int rxr_idx = 0, txr_idx = 0, v_idx = 0;
1254 int err;
Alexander Duyck047e0032009-10-27 15:49:27 +00001255
Alexander Duyck5536d212012-09-25 00:31:17 +00001256 if (q_vectors >= (rxr_remaining + txr_remaining)) {
1257 for (; rxr_remaining; v_idx++) {
1258 err = igb_alloc_q_vector(adapter, q_vectors, v_idx,
1259 0, 0, 1, rxr_idx);
1260
1261 if (err)
1262 goto err_out;
1263
1264 /* update counts and index */
1265 rxr_remaining--;
1266 rxr_idx++;
1267 }
1268 }
1269
1270 for (; v_idx < q_vectors; v_idx++) {
1271 int rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - v_idx);
1272 int tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - v_idx);
1273 err = igb_alloc_q_vector(adapter, q_vectors, v_idx,
1274 tqpv, txr_idx, rqpv, rxr_idx);
1275
1276 if (err)
Alexander Duyck047e0032009-10-27 15:49:27 +00001277 goto err_out;
Alexander Duyck5536d212012-09-25 00:31:17 +00001278
1279 /* update counts and index */
1280 rxr_remaining -= rqpv;
1281 txr_remaining -= tqpv;
1282 rxr_idx++;
1283 txr_idx++;
Alexander Duyck047e0032009-10-27 15:49:27 +00001284 }
Alexander Duyck81c2fc22011-08-26 07:45:20 +00001285
Alexander Duyck047e0032009-10-27 15:49:27 +00001286 return 0;
1287
1288err_out:
Alexander Duyck5536d212012-09-25 00:31:17 +00001289 adapter->num_tx_queues = 0;
1290 adapter->num_rx_queues = 0;
1291 adapter->num_q_vectors = 0;
1292
1293 while (v_idx--)
1294 igb_free_q_vector(adapter, v_idx);
1295
Alexander Duyck047e0032009-10-27 15:49:27 +00001296 return -ENOMEM;
1297}
1298
Alexander Duyck047e0032009-10-27 15:49:27 +00001299/**
1300 * igb_init_interrupt_scheme - initialize interrupts, allocate queues/vectors
1301 *
1302 * This function initializes the interrupts and allocates all of the queues.
1303 **/
Stefan Assmann53c7d062012-12-04 06:00:12 +00001304static int igb_init_interrupt_scheme(struct igb_adapter *adapter, bool msix)
Alexander Duyck047e0032009-10-27 15:49:27 +00001305{
1306 struct pci_dev *pdev = adapter->pdev;
1307 int err;
1308
Stefan Assmann53c7d062012-12-04 06:00:12 +00001309 igb_set_interrupt_capability(adapter, msix);
Alexander Duyck047e0032009-10-27 15:49:27 +00001310
1311 err = igb_alloc_q_vectors(adapter);
1312 if (err) {
1313 dev_err(&pdev->dev, "Unable to allocate memory for vectors\n");
1314 goto err_alloc_q_vectors;
1315 }
1316
Alexander Duyck5536d212012-09-25 00:31:17 +00001317 igb_cache_ring_register(adapter);
Alexander Duyck047e0032009-10-27 15:49:27 +00001318
1319 return 0;
Alexander Duyck5536d212012-09-25 00:31:17 +00001320
Alexander Duyck047e0032009-10-27 15:49:27 +00001321err_alloc_q_vectors:
1322 igb_reset_interrupt_capability(adapter);
1323 return err;
1324}
1325
1326/**
Auke Kok9d5c8242008-01-24 02:22:38 -08001327 * igb_request_irq - initialize interrupts
1328 *
1329 * Attempts to configure interrupts using the best available
1330 * capabilities of the hardware and kernel.
1331 **/
1332static int igb_request_irq(struct igb_adapter *adapter)
1333{
1334 struct net_device *netdev = adapter->netdev;
Alexander Duyck047e0032009-10-27 15:49:27 +00001335 struct pci_dev *pdev = adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08001336 int err = 0;
1337
1338 if (adapter->msix_entries) {
1339 err = igb_request_msix(adapter);
PJ Waskiewicz844290e2008-06-27 11:00:39 -07001340 if (!err)
Auke Kok9d5c8242008-01-24 02:22:38 -08001341 goto request_done;
Auke Kok9d5c8242008-01-24 02:22:38 -08001342 /* fall back to MSI */
Alexander Duyck5536d212012-09-25 00:31:17 +00001343 igb_free_all_tx_resources(adapter);
1344 igb_free_all_rx_resources(adapter);
Stefan Assmann53c7d062012-12-04 06:00:12 +00001345
Alexander Duyck047e0032009-10-27 15:49:27 +00001346 igb_clear_interrupt_scheme(adapter);
Stefan Assmann53c7d062012-12-04 06:00:12 +00001347 err = igb_init_interrupt_scheme(adapter, false);
1348 if (err)
Alexander Duyck047e0032009-10-27 15:49:27 +00001349 goto request_done;
Stefan Assmann53c7d062012-12-04 06:00:12 +00001350
Alexander Duyck047e0032009-10-27 15:49:27 +00001351 igb_setup_all_tx_resources(adapter);
1352 igb_setup_all_rx_resources(adapter);
Stefan Assmann53c7d062012-12-04 06:00:12 +00001353 igb_configure(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001354 }
PJ Waskiewicz844290e2008-06-27 11:00:39 -07001355
Alexander Duyckc74d5882011-08-26 07:46:45 +00001356 igb_assign_vector(adapter->q_vector[0], 0);
1357
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07001358 if (adapter->flags & IGB_FLAG_HAS_MSI) {
Alexander Duyckc74d5882011-08-26 07:46:45 +00001359 err = request_irq(pdev->irq, igb_intr_msi, 0,
Alexander Duyck047e0032009-10-27 15:49:27 +00001360 netdev->name, adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001361 if (!err)
1362 goto request_done;
Alexander Duyck047e0032009-10-27 15:49:27 +00001363
Auke Kok9d5c8242008-01-24 02:22:38 -08001364 /* fall back to legacy interrupts */
1365 igb_reset_interrupt_capability(adapter);
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07001366 adapter->flags &= ~IGB_FLAG_HAS_MSI;
Auke Kok9d5c8242008-01-24 02:22:38 -08001367 }
1368
Alexander Duyckc74d5882011-08-26 07:46:45 +00001369 err = request_irq(pdev->irq, igb_intr, IRQF_SHARED,
Alexander Duyck047e0032009-10-27 15:49:27 +00001370 netdev->name, adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001371
Andy Gospodarek6cb5e572008-02-15 14:05:25 -08001372 if (err)
Alexander Duyckc74d5882011-08-26 07:46:45 +00001373 dev_err(&pdev->dev, "Error %d getting interrupt\n",
Auke Kok9d5c8242008-01-24 02:22:38 -08001374 err);
Auke Kok9d5c8242008-01-24 02:22:38 -08001375
1376request_done:
1377 return err;
1378}
1379
1380static void igb_free_irq(struct igb_adapter *adapter)
1381{
Auke Kok9d5c8242008-01-24 02:22:38 -08001382 if (adapter->msix_entries) {
1383 int vector = 0, i;
1384
Alexander Duyck047e0032009-10-27 15:49:27 +00001385 free_irq(adapter->msix_entries[vector++].vector, adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001386
Alexander Duyck0d1ae7f2011-08-26 07:46:34 +00001387 for (i = 0; i < adapter->num_q_vectors; i++)
Alexander Duyck047e0032009-10-27 15:49:27 +00001388 free_irq(adapter->msix_entries[vector++].vector,
Alexander Duyck0d1ae7f2011-08-26 07:46:34 +00001389 adapter->q_vector[i]);
Alexander Duyck047e0032009-10-27 15:49:27 +00001390 } else {
1391 free_irq(adapter->pdev->irq, adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001392 }
Auke Kok9d5c8242008-01-24 02:22:38 -08001393}
1394
1395/**
1396 * igb_irq_disable - Mask off interrupt generation on the NIC
1397 * @adapter: board private structure
1398 **/
1399static void igb_irq_disable(struct igb_adapter *adapter)
1400{
1401 struct e1000_hw *hw = &adapter->hw;
1402
Alexander Duyck25568a52009-10-27 23:49:59 +00001403 /*
1404 * we need to be careful when disabling interrupts. The VFs are also
1405 * mapped into these registers and so clearing the bits can cause
1406 * issues on the VF drivers so we only need to clear what we set
1407 */
Auke Kok9d5c8242008-01-24 02:22:38 -08001408 if (adapter->msix_entries) {
Alexander Duyck2dfd1212009-09-03 14:49:15 +00001409 u32 regval = rd32(E1000_EIAM);
1410 wr32(E1000_EIAM, regval & ~adapter->eims_enable_mask);
1411 wr32(E1000_EIMC, adapter->eims_enable_mask);
1412 regval = rd32(E1000_EIAC);
1413 wr32(E1000_EIAC, regval & ~adapter->eims_enable_mask);
Auke Kok9d5c8242008-01-24 02:22:38 -08001414 }
PJ Waskiewicz844290e2008-06-27 11:00:39 -07001415
1416 wr32(E1000_IAM, 0);
Auke Kok9d5c8242008-01-24 02:22:38 -08001417 wr32(E1000_IMC, ~0);
1418 wrfl();
Emil Tantilov81a61852010-08-02 14:40:52 +00001419 if (adapter->msix_entries) {
1420 int i;
1421 for (i = 0; i < adapter->num_q_vectors; i++)
1422 synchronize_irq(adapter->msix_entries[i].vector);
1423 } else {
1424 synchronize_irq(adapter->pdev->irq);
1425 }
Auke Kok9d5c8242008-01-24 02:22:38 -08001426}
1427
1428/**
1429 * igb_irq_enable - Enable default interrupt generation settings
1430 * @adapter: board private structure
1431 **/
1432static void igb_irq_enable(struct igb_adapter *adapter)
1433{
1434 struct e1000_hw *hw = &adapter->hw;
1435
1436 if (adapter->msix_entries) {
Alexander Duyck06218a82011-08-26 07:46:55 +00001437 u32 ims = E1000_IMS_LSC | E1000_IMS_DOUTSYNC | E1000_IMS_DRSTA;
Alexander Duyck2dfd1212009-09-03 14:49:15 +00001438 u32 regval = rd32(E1000_EIAC);
1439 wr32(E1000_EIAC, regval | adapter->eims_enable_mask);
1440 regval = rd32(E1000_EIAM);
1441 wr32(E1000_EIAM, regval | adapter->eims_enable_mask);
PJ Waskiewicz844290e2008-06-27 11:00:39 -07001442 wr32(E1000_EIMS, adapter->eims_enable_mask);
Alexander Duyck25568a52009-10-27 23:49:59 +00001443 if (adapter->vfs_allocated_count) {
Alexander Duyck4ae196d2009-02-19 20:40:07 -08001444 wr32(E1000_MBVFIMR, 0xFF);
Alexander Duyck25568a52009-10-27 23:49:59 +00001445 ims |= E1000_IMS_VMMB;
1446 }
1447 wr32(E1000_IMS, ims);
PJ Waskiewicz844290e2008-06-27 11:00:39 -07001448 } else {
Alexander Duyck55cac242009-11-19 12:42:21 +00001449 wr32(E1000_IMS, IMS_ENABLE_MASK |
1450 E1000_IMS_DRSTA);
1451 wr32(E1000_IAM, IMS_ENABLE_MASK |
1452 E1000_IMS_DRSTA);
PJ Waskiewicz844290e2008-06-27 11:00:39 -07001453 }
Auke Kok9d5c8242008-01-24 02:22:38 -08001454}
1455
1456static void igb_update_mng_vlan(struct igb_adapter *adapter)
1457{
Alexander Duyck51466232009-10-27 23:47:35 +00001458 struct e1000_hw *hw = &adapter->hw;
Auke Kok9d5c8242008-01-24 02:22:38 -08001459 u16 vid = adapter->hw.mng_cookie.vlan_id;
1460 u16 old_vid = adapter->mng_vlan_id;
Auke Kok9d5c8242008-01-24 02:22:38 -08001461
Alexander Duyck51466232009-10-27 23:47:35 +00001462 if (hw->mng_cookie.status & E1000_MNG_DHCP_COOKIE_STATUS_VLAN) {
1463 /* add VID to filter table */
1464 igb_vfta_set(hw, vid, true);
1465 adapter->mng_vlan_id = vid;
1466 } else {
1467 adapter->mng_vlan_id = IGB_MNG_VLAN_NONE;
1468 }
1469
1470 if ((old_vid != (u16)IGB_MNG_VLAN_NONE) &&
1471 (vid != old_vid) &&
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00001472 !test_bit(old_vid, adapter->active_vlans)) {
Alexander Duyck51466232009-10-27 23:47:35 +00001473 /* remove VID from filter table */
1474 igb_vfta_set(hw, old_vid, false);
Auke Kok9d5c8242008-01-24 02:22:38 -08001475 }
1476}
1477
1478/**
1479 * igb_release_hw_control - release control of the h/w to f/w
1480 * @adapter: address of board private structure
1481 *
1482 * igb_release_hw_control resets CTRL_EXT:DRV_LOAD bit.
1483 * For ASF and Pass Through versions of f/w this means that the
1484 * driver is no longer loaded.
1485 *
1486 **/
1487static void igb_release_hw_control(struct igb_adapter *adapter)
1488{
1489 struct e1000_hw *hw = &adapter->hw;
1490 u32 ctrl_ext;
1491
1492 /* Let firmware take over control of h/w */
1493 ctrl_ext = rd32(E1000_CTRL_EXT);
1494 wr32(E1000_CTRL_EXT,
1495 ctrl_ext & ~E1000_CTRL_EXT_DRV_LOAD);
1496}
1497
Auke Kok9d5c8242008-01-24 02:22:38 -08001498/**
1499 * igb_get_hw_control - get control of the h/w from f/w
1500 * @adapter: address of board private structure
1501 *
1502 * igb_get_hw_control sets CTRL_EXT:DRV_LOAD bit.
1503 * For ASF and Pass Through versions of f/w this means that
1504 * the driver is loaded.
1505 *
1506 **/
1507static void igb_get_hw_control(struct igb_adapter *adapter)
1508{
1509 struct e1000_hw *hw = &adapter->hw;
1510 u32 ctrl_ext;
1511
1512 /* Let firmware know the driver has taken over */
1513 ctrl_ext = rd32(E1000_CTRL_EXT);
1514 wr32(E1000_CTRL_EXT,
1515 ctrl_ext | E1000_CTRL_EXT_DRV_LOAD);
1516}
1517
Auke Kok9d5c8242008-01-24 02:22:38 -08001518/**
1519 * igb_configure - configure the hardware for RX and TX
1520 * @adapter: private board structure
1521 **/
1522static void igb_configure(struct igb_adapter *adapter)
1523{
1524 struct net_device *netdev = adapter->netdev;
1525 int i;
1526
1527 igb_get_hw_control(adapter);
Alexander Duyckff41f8d2009-09-03 14:48:56 +00001528 igb_set_rx_mode(netdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08001529
1530 igb_restore_vlan(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001531
Alexander Duyck85b430b2009-10-27 15:50:29 +00001532 igb_setup_tctl(adapter);
Alexander Duyck06cf2662009-10-27 15:53:25 +00001533 igb_setup_mrqc(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001534 igb_setup_rctl(adapter);
Alexander Duyck85b430b2009-10-27 15:50:29 +00001535
1536 igb_configure_tx(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001537 igb_configure_rx(adapter);
Alexander Duyck662d7202008-06-27 11:00:29 -07001538
1539 igb_rx_fifo_flush_82575(&adapter->hw);
1540
Alexander Duyckc493ea42009-03-20 00:16:50 +00001541 /* call igb_desc_unused which always leaves
Auke Kok9d5c8242008-01-24 02:22:38 -08001542 * at least 1 descriptor unused to make sure
1543 * next_to_use != next_to_clean */
1544 for (i = 0; i < adapter->num_rx_queues; i++) {
Alexander Duyck3025a442010-02-17 01:02:39 +00001545 struct igb_ring *ring = adapter->rx_ring[i];
Alexander Duyckcd392f52011-08-26 07:43:59 +00001546 igb_alloc_rx_buffers(ring, igb_desc_unused(ring));
Auke Kok9d5c8242008-01-24 02:22:38 -08001547 }
Auke Kok9d5c8242008-01-24 02:22:38 -08001548}
1549
Nick Nunley88a268c2010-02-17 01:01:59 +00001550/**
1551 * igb_power_up_link - Power up the phy/serdes link
1552 * @adapter: address of board private structure
1553 **/
1554void igb_power_up_link(struct igb_adapter *adapter)
1555{
Akeem G. Abodunrin76886592012-07-17 04:51:18 +00001556 igb_reset_phy(&adapter->hw);
1557
Nick Nunley88a268c2010-02-17 01:01:59 +00001558 if (adapter->hw.phy.media_type == e1000_media_type_copper)
1559 igb_power_up_phy_copper(&adapter->hw);
1560 else
1561 igb_power_up_serdes_link_82575(&adapter->hw);
1562}
1563
1564/**
1565 * igb_power_down_link - Power down the phy/serdes link
1566 * @adapter: address of board private structure
1567 */
1568static void igb_power_down_link(struct igb_adapter *adapter)
1569{
1570 if (adapter->hw.phy.media_type == e1000_media_type_copper)
1571 igb_power_down_phy_copper_82575(&adapter->hw);
1572 else
1573 igb_shutdown_serdes_link_82575(&adapter->hw);
1574}
Auke Kok9d5c8242008-01-24 02:22:38 -08001575
1576/**
1577 * igb_up - Open the interface and prepare it to handle traffic
1578 * @adapter: board private structure
1579 **/
Auke Kok9d5c8242008-01-24 02:22:38 -08001580int igb_up(struct igb_adapter *adapter)
1581{
1582 struct e1000_hw *hw = &adapter->hw;
1583 int i;
1584
1585 /* hardware has been reset, we need to reload some things */
1586 igb_configure(adapter);
1587
1588 clear_bit(__IGB_DOWN, &adapter->state);
1589
Alexander Duyck0d1ae7f2011-08-26 07:46:34 +00001590 for (i = 0; i < adapter->num_q_vectors; i++)
1591 napi_enable(&(adapter->q_vector[i]->napi));
1592
PJ Waskiewicz844290e2008-06-27 11:00:39 -07001593 if (adapter->msix_entries)
Auke Kok9d5c8242008-01-24 02:22:38 -08001594 igb_configure_msix(adapter);
Alexander Duyckfeeb2722010-02-03 21:59:51 +00001595 else
1596 igb_assign_vector(adapter->q_vector[0], 0);
Auke Kok9d5c8242008-01-24 02:22:38 -08001597
1598 /* Clear any pending interrupts. */
1599 rd32(E1000_ICR);
1600 igb_irq_enable(adapter);
1601
Alexander Duyckd4960302009-10-27 15:53:45 +00001602 /* notify VFs that reset has been completed */
1603 if (adapter->vfs_allocated_count) {
1604 u32 reg_data = rd32(E1000_CTRL_EXT);
1605 reg_data |= E1000_CTRL_EXT_PFRSTD;
1606 wr32(E1000_CTRL_EXT, reg_data);
1607 }
1608
Jesse Brandeburg4cb9be72009-04-21 18:42:05 +00001609 netif_tx_start_all_queues(adapter->netdev);
1610
Alexander Duyck25568a52009-10-27 23:49:59 +00001611 /* start the watchdog. */
1612 hw->mac.get_link_status = 1;
1613 schedule_work(&adapter->watchdog_task);
1614
Auke Kok9d5c8242008-01-24 02:22:38 -08001615 return 0;
1616}
1617
1618void igb_down(struct igb_adapter *adapter)
1619{
Auke Kok9d5c8242008-01-24 02:22:38 -08001620 struct net_device *netdev = adapter->netdev;
Alexander Duyck330a6d62009-10-27 23:51:35 +00001621 struct e1000_hw *hw = &adapter->hw;
Auke Kok9d5c8242008-01-24 02:22:38 -08001622 u32 tctl, rctl;
1623 int i;
1624
1625 /* signal that we're down so the interrupt handler does not
1626 * reschedule our watchdog timer */
1627 set_bit(__IGB_DOWN, &adapter->state);
1628
1629 /* disable receives in the hardware */
1630 rctl = rd32(E1000_RCTL);
1631 wr32(E1000_RCTL, rctl & ~E1000_RCTL_EN);
1632 /* flush and sleep below */
1633
David S. Millerfd2ea0a2008-07-17 01:56:23 -07001634 netif_tx_stop_all_queues(netdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08001635
1636 /* disable transmits in the hardware */
1637 tctl = rd32(E1000_TCTL);
1638 tctl &= ~E1000_TCTL_EN;
1639 wr32(E1000_TCTL, tctl);
1640 /* flush both disables and wait for them to finish */
1641 wrfl();
1642 msleep(10);
1643
Alexander Duyck0d1ae7f2011-08-26 07:46:34 +00001644 for (i = 0; i < adapter->num_q_vectors; i++)
1645 napi_disable(&(adapter->q_vector[i]->napi));
Auke Kok9d5c8242008-01-24 02:22:38 -08001646
Auke Kok9d5c8242008-01-24 02:22:38 -08001647 igb_irq_disable(adapter);
1648
1649 del_timer_sync(&adapter->watchdog_timer);
1650 del_timer_sync(&adapter->phy_info_timer);
1651
Auke Kok9d5c8242008-01-24 02:22:38 -08001652 netif_carrier_off(netdev);
Alexander Duyck04fe6352009-02-06 23:22:32 +00001653
1654 /* record the stats before reset*/
Eric Dumazet12dcd862010-10-15 17:27:10 +00001655 spin_lock(&adapter->stats64_lock);
1656 igb_update_stats(adapter, &adapter->stats64);
1657 spin_unlock(&adapter->stats64_lock);
Alexander Duyck04fe6352009-02-06 23:22:32 +00001658
Auke Kok9d5c8242008-01-24 02:22:38 -08001659 adapter->link_speed = 0;
1660 adapter->link_duplex = 0;
1661
Jeff Kirsher30236822008-06-24 17:01:15 -07001662 if (!pci_channel_offline(adapter->pdev))
1663 igb_reset(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001664 igb_clean_all_tx_rings(adapter);
1665 igb_clean_all_rx_rings(adapter);
Alexander Duyck7e0e99e2009-05-21 13:06:56 +00001666#ifdef CONFIG_IGB_DCA
1667
1668 /* since we reset the hardware DCA settings were cleared */
1669 igb_setup_dca(adapter);
1670#endif
Auke Kok9d5c8242008-01-24 02:22:38 -08001671}
1672
1673void igb_reinit_locked(struct igb_adapter *adapter)
1674{
1675 WARN_ON(in_interrupt());
1676 while (test_and_set_bit(__IGB_RESETTING, &adapter->state))
1677 msleep(1);
1678 igb_down(adapter);
1679 igb_up(adapter);
1680 clear_bit(__IGB_RESETTING, &adapter->state);
1681}
1682
1683void igb_reset(struct igb_adapter *adapter)
1684{
Alexander Duyck090b1792009-10-27 23:51:55 +00001685 struct pci_dev *pdev = adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08001686 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck2d064c02008-07-08 15:10:12 -07001687 struct e1000_mac_info *mac = &hw->mac;
1688 struct e1000_fc_info *fc = &hw->fc;
Matthew Vickd48507f2012-11-08 04:03:58 +00001689 u32 pba = 0, tx_space, min_tx_space, min_rx_space, hwm;
Auke Kok9d5c8242008-01-24 02:22:38 -08001690
1691 /* Repartition Pba for greater than 9k mtu
1692 * To take effect CTRL.RST is required.
1693 */
Alexander Duyckfa4dfae2009-02-06 23:21:31 +00001694 switch (mac->type) {
Alexander Duyckd2ba2ed2010-03-22 14:08:06 +00001695 case e1000_i350:
Alexander Duyck55cac242009-11-19 12:42:21 +00001696 case e1000_82580:
1697 pba = rd32(E1000_RXPBS);
1698 pba = igb_rxpbs_adjust_82580(pba);
1699 break;
Alexander Duyckfa4dfae2009-02-06 23:21:31 +00001700 case e1000_82576:
Alexander Duyckd249be52009-10-27 23:46:38 +00001701 pba = rd32(E1000_RXPBS);
1702 pba &= E1000_RXPBS_SIZE_MASK_82576;
Alexander Duyckfa4dfae2009-02-06 23:21:31 +00001703 break;
1704 case e1000_82575:
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00001705 case e1000_i210:
1706 case e1000_i211:
Alexander Duyckfa4dfae2009-02-06 23:21:31 +00001707 default:
1708 pba = E1000_PBA_34K;
1709 break;
Alexander Duyck2d064c02008-07-08 15:10:12 -07001710 }
Auke Kok9d5c8242008-01-24 02:22:38 -08001711
Alexander Duyck2d064c02008-07-08 15:10:12 -07001712 if ((adapter->max_frame_size > ETH_FRAME_LEN + ETH_FCS_LEN) &&
1713 (mac->type < e1000_82576)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08001714 /* adjust PBA for jumbo frames */
1715 wr32(E1000_PBA, pba);
1716
1717 /* To maintain wire speed transmits, the Tx FIFO should be
1718 * large enough to accommodate two full transmit packets,
1719 * rounded up to the next 1KB and expressed in KB. Likewise,
1720 * the Rx FIFO should be large enough to accommodate at least
1721 * one full receive packet and is similarly rounded up and
1722 * expressed in KB. */
1723 pba = rd32(E1000_PBA);
1724 /* upper 16 bits has Tx packet buffer allocation size in KB */
1725 tx_space = pba >> 16;
1726 /* lower 16 bits has Rx packet buffer allocation size in KB */
1727 pba &= 0xffff;
1728 /* the tx fifo also stores 16 bytes of information about the tx
1729 * but don't include ethernet FCS because hardware appends it */
1730 min_tx_space = (adapter->max_frame_size +
Alexander Duyck85e8d002009-02-16 00:00:20 -08001731 sizeof(union e1000_adv_tx_desc) -
Auke Kok9d5c8242008-01-24 02:22:38 -08001732 ETH_FCS_LEN) * 2;
1733 min_tx_space = ALIGN(min_tx_space, 1024);
1734 min_tx_space >>= 10;
1735 /* software strips receive CRC, so leave room for it */
1736 min_rx_space = adapter->max_frame_size;
1737 min_rx_space = ALIGN(min_rx_space, 1024);
1738 min_rx_space >>= 10;
1739
1740 /* If current Tx allocation is less than the min Tx FIFO size,
1741 * and the min Tx FIFO size is less than the current Rx FIFO
1742 * allocation, take space away from current Rx allocation */
1743 if (tx_space < min_tx_space &&
1744 ((min_tx_space - tx_space) < pba)) {
1745 pba = pba - (min_tx_space - tx_space);
1746
1747 /* if short on rx space, rx wins and must trump tx
1748 * adjustment */
1749 if (pba < min_rx_space)
1750 pba = min_rx_space;
1751 }
Alexander Duyck2d064c02008-07-08 15:10:12 -07001752 wr32(E1000_PBA, pba);
Auke Kok9d5c8242008-01-24 02:22:38 -08001753 }
Auke Kok9d5c8242008-01-24 02:22:38 -08001754
1755 /* flow control settings */
1756 /* The high water mark must be low enough to fit one full frame
1757 * (or the size used for early receive) above it in the Rx FIFO.
1758 * Set it to the lower of:
1759 * - 90% of the Rx FIFO size, or
1760 * - the full Rx FIFO size minus one full frame */
1761 hwm = min(((pba << 10) * 9 / 10),
Alexander Duyck2d064c02008-07-08 15:10:12 -07001762 ((pba << 10) - 2 * adapter->max_frame_size));
Auke Kok9d5c8242008-01-24 02:22:38 -08001763
Matthew Vickd48507f2012-11-08 04:03:58 +00001764 fc->high_water = hwm & 0xFFFFFFF0; /* 16-byte granularity */
Alexander Duyckd405ea32009-12-23 13:21:27 +00001765 fc->low_water = fc->high_water - 16;
Auke Kok9d5c8242008-01-24 02:22:38 -08001766 fc->pause_time = 0xFFFF;
1767 fc->send_xon = 1;
Alexander Duyck0cce1192009-07-23 18:10:24 +00001768 fc->current_mode = fc->requested_mode;
Auke Kok9d5c8242008-01-24 02:22:38 -08001769
Alexander Duyck4ae196d2009-02-19 20:40:07 -08001770 /* disable receive for all VFs and wait one second */
1771 if (adapter->vfs_allocated_count) {
1772 int i;
1773 for (i = 0 ; i < adapter->vfs_allocated_count; i++)
Greg Rose8fa7e0f2010-11-06 05:43:21 +00001774 adapter->vf_data[i].flags &= IGB_VF_FLAG_PF_SET_MAC;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08001775
1776 /* ping all the active vfs to let them know we are going down */
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00001777 igb_ping_all_vfs(adapter);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08001778
1779 /* disable transmits and receives */
1780 wr32(E1000_VFRE, 0);
1781 wr32(E1000_VFTE, 0);
1782 }
1783
Auke Kok9d5c8242008-01-24 02:22:38 -08001784 /* Allow time for pending master requests to run */
Alexander Duyck330a6d62009-10-27 23:51:35 +00001785 hw->mac.ops.reset_hw(hw);
Auke Kok9d5c8242008-01-24 02:22:38 -08001786 wr32(E1000_WUC, 0);
1787
Alexander Duyck330a6d62009-10-27 23:51:35 +00001788 if (hw->mac.ops.init_hw(hw))
Alexander Duyck090b1792009-10-27 23:51:55 +00001789 dev_err(&pdev->dev, "Hardware Error\n");
Auke Kok9d5c8242008-01-24 02:22:38 -08001790
Matthew Vicka27416b2012-04-18 02:57:44 +00001791 /*
1792 * Flow control settings reset on hardware reset, so guarantee flow
1793 * control is off when forcing speed.
1794 */
1795 if (!hw->mac.autoneg)
1796 igb_force_mac_fc(hw);
1797
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +00001798 igb_init_dmac(adapter, pba);
Nick Nunley88a268c2010-02-17 01:01:59 +00001799 if (!netif_running(adapter->netdev))
1800 igb_power_down_link(adapter);
1801
Auke Kok9d5c8242008-01-24 02:22:38 -08001802 igb_update_mng_vlan(adapter);
1803
1804 /* Enable h/w to recognize an 802.1Q VLAN Ethernet packet */
1805 wr32(E1000_VET, ETHERNET_IEEE_VLAN_TYPE);
1806
Matthew Vick1f6e8172012-08-18 07:26:33 +00001807 /* Re-enable PTP, where applicable. */
1808 igb_ptp_reset(adapter);
Matthew Vick1f6e8172012-08-18 07:26:33 +00001809
Alexander Duyck330a6d62009-10-27 23:51:35 +00001810 igb_get_phy_info(hw);
Auke Kok9d5c8242008-01-24 02:22:38 -08001811}
1812
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001813static netdev_features_t igb_fix_features(struct net_device *netdev,
1814 netdev_features_t features)
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00001815{
1816 /*
1817 * Since there is no support for separate rx/tx vlan accel
1818 * enable/disable make sure tx flag is always in same state as rx.
1819 */
1820 if (features & NETIF_F_HW_VLAN_RX)
1821 features |= NETIF_F_HW_VLAN_TX;
1822 else
1823 features &= ~NETIF_F_HW_VLAN_TX;
1824
1825 return features;
1826}
1827
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001828static int igb_set_features(struct net_device *netdev,
1829 netdev_features_t features)
Michał Mirosławac52caa2011-06-08 08:38:01 +00001830{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001831 netdev_features_t changed = netdev->features ^ features;
Ben Greear89eaefb2012-03-06 09:41:58 +00001832 struct igb_adapter *adapter = netdev_priv(netdev);
Michał Mirosławac52caa2011-06-08 08:38:01 +00001833
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00001834 if (changed & NETIF_F_HW_VLAN_RX)
1835 igb_vlan_mode(netdev, features);
1836
Ben Greear89eaefb2012-03-06 09:41:58 +00001837 if (!(changed & NETIF_F_RXALL))
1838 return 0;
1839
1840 netdev->features = features;
1841
1842 if (netif_running(netdev))
1843 igb_reinit_locked(adapter);
1844 else
1845 igb_reset(adapter);
1846
Michał Mirosławac52caa2011-06-08 08:38:01 +00001847 return 0;
1848}
1849
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08001850static const struct net_device_ops igb_netdev_ops = {
Alexander Duyck559e9c42009-10-27 23:52:50 +00001851 .ndo_open = igb_open,
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08001852 .ndo_stop = igb_close,
Alexander Duyckcd392f52011-08-26 07:43:59 +00001853 .ndo_start_xmit = igb_xmit_frame,
Eric Dumazet12dcd862010-10-15 17:27:10 +00001854 .ndo_get_stats64 = igb_get_stats64,
Alexander Duyckff41f8d2009-09-03 14:48:56 +00001855 .ndo_set_rx_mode = igb_set_rx_mode,
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08001856 .ndo_set_mac_address = igb_set_mac,
1857 .ndo_change_mtu = igb_change_mtu,
1858 .ndo_do_ioctl = igb_ioctl,
1859 .ndo_tx_timeout = igb_tx_timeout,
1860 .ndo_validate_addr = eth_validate_addr,
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08001861 .ndo_vlan_rx_add_vid = igb_vlan_rx_add_vid,
1862 .ndo_vlan_rx_kill_vid = igb_vlan_rx_kill_vid,
Williams, Mitch A8151d292010-02-10 01:44:24 +00001863 .ndo_set_vf_mac = igb_ndo_set_vf_mac,
1864 .ndo_set_vf_vlan = igb_ndo_set_vf_vlan,
1865 .ndo_set_vf_tx_rate = igb_ndo_set_vf_bw,
1866 .ndo_get_vf_config = igb_ndo_get_vf_config,
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08001867#ifdef CONFIG_NET_POLL_CONTROLLER
1868 .ndo_poll_controller = igb_netpoll,
1869#endif
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00001870 .ndo_fix_features = igb_fix_features,
1871 .ndo_set_features = igb_set_features,
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08001872};
1873
Taku Izumi42bfd33a2008-06-20 12:10:30 +09001874/**
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00001875 * igb_set_fw_version - Configure version string for ethtool
1876 * @adapter: adapter struct
1877 *
1878 **/
1879void igb_set_fw_version(struct igb_adapter *adapter)
1880{
1881 struct e1000_hw *hw = &adapter->hw;
Carolyn Wyborny0b1a6f22012-10-18 07:16:19 +00001882 struct e1000_fw_version fw;
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00001883
Carolyn Wyborny0b1a6f22012-10-18 07:16:19 +00001884 igb_get_fw_version(hw, &fw);
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00001885
Carolyn Wyborny0b1a6f22012-10-18 07:16:19 +00001886 switch (hw->mac.type) {
1887 case e1000_i211:
1888 snprintf(adapter->fw_version, sizeof(adapter->fw_version),
1889 "%2d.%2d-%d",
1890 fw.invm_major, fw.invm_minor, fw.invm_img_type);
1891 break;
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00001892
Carolyn Wyborny0b1a6f22012-10-18 07:16:19 +00001893 default:
1894 /* if option is rom valid, display its version too */
1895 if (fw.or_valid) {
1896 snprintf(adapter->fw_version,
1897 sizeof(adapter->fw_version),
1898 "%d.%d, 0x%08x, %d.%d.%d",
1899 fw.eep_major, fw.eep_minor, fw.etrack_id,
1900 fw.or_major, fw.or_build, fw.or_patch);
1901 /* no option rom */
1902 } else {
1903 snprintf(adapter->fw_version,
1904 sizeof(adapter->fw_version),
1905 "%d.%d, 0x%08x",
1906 fw.eep_major, fw.eep_minor, fw.etrack_id);
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00001907 }
Carolyn Wyborny0b1a6f22012-10-18 07:16:19 +00001908 break;
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00001909 }
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00001910 return;
1911}
1912
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +00001913static const struct i2c_board_info i350_sensor_info = {
1914 I2C_BOARD_INFO("i350bb", 0Xf8),
1915};
1916
1917/* igb_init_i2c - Init I2C interface
1918 * @adapter: pointer to adapter structure
1919 *
1920 */
1921static s32 igb_init_i2c(struct igb_adapter *adapter)
1922{
1923 s32 status = E1000_SUCCESS;
1924
1925 /* I2C interface supported on i350 devices */
1926 if (adapter->hw.mac.type != e1000_i350)
1927 return E1000_SUCCESS;
1928
1929 /* Initialize the i2c bus which is controlled by the registers.
1930 * This bus will use the i2c_algo_bit structue that implements
1931 * the protocol through toggling of the 4 bits in the register.
1932 */
1933 adapter->i2c_adap.owner = THIS_MODULE;
1934 adapter->i2c_algo = igb_i2c_algo;
1935 adapter->i2c_algo.data = adapter;
1936 adapter->i2c_adap.algo_data = &adapter->i2c_algo;
1937 adapter->i2c_adap.dev.parent = &adapter->pdev->dev;
1938 strlcpy(adapter->i2c_adap.name, "igb BB",
1939 sizeof(adapter->i2c_adap.name));
1940 status = i2c_bit_add_bus(&adapter->i2c_adap);
1941 return status;
1942}
1943
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00001944/**
Auke Kok9d5c8242008-01-24 02:22:38 -08001945 * igb_probe - Device Initialization Routine
1946 * @pdev: PCI device information struct
1947 * @ent: entry in igb_pci_tbl
1948 *
1949 * Returns 0 on success, negative on failure
1950 *
1951 * igb_probe initializes an adapter identified by a pci_dev structure.
1952 * The OS initialization, configuring of the adapter private structure,
1953 * and a hardware reset occur.
1954 **/
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00001955static int igb_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Auke Kok9d5c8242008-01-24 02:22:38 -08001956{
1957 struct net_device *netdev;
1958 struct igb_adapter *adapter;
1959 struct e1000_hw *hw;
Alexander Duyck4337e992009-10-27 23:48:31 +00001960 u16 eeprom_data = 0;
Carolyn Wyborny9835fd72010-11-22 17:17:21 +00001961 s32 ret_val;
Alexander Duyck4337e992009-10-27 23:48:31 +00001962 static int global_quad_port_a; /* global quad port a indication */
Auke Kok9d5c8242008-01-24 02:22:38 -08001963 const struct e1000_info *ei = igb_info_tbl[ent->driver_data];
1964 unsigned long mmio_start, mmio_len;
David S. Miller2d6a5e92009-03-17 15:01:30 -07001965 int err, pci_using_dac;
Carolyn Wyborny9835fd72010-11-22 17:17:21 +00001966 u8 part_str[E1000_PBANUM_LENGTH];
Auke Kok9d5c8242008-01-24 02:22:38 -08001967
Andy Gospodarekbded64a2010-07-21 06:40:31 +00001968 /* Catch broken hardware that put the wrong VF device ID in
1969 * the PCIe SR-IOV capability.
1970 */
1971 if (pdev->is_virtfn) {
1972 WARN(1, KERN_ERR "%s (%hx:%hx) should not be a VF!\n",
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00001973 pci_name(pdev), pdev->vendor, pdev->device);
Andy Gospodarekbded64a2010-07-21 06:40:31 +00001974 return -EINVAL;
1975 }
1976
Alexander Duyckaed5dec2009-02-06 23:16:04 +00001977 err = pci_enable_device_mem(pdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08001978 if (err)
1979 return err;
1980
1981 pci_using_dac = 0;
Alexander Duyck59d71982010-04-27 13:09:25 +00001982 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(64));
Auke Kok9d5c8242008-01-24 02:22:38 -08001983 if (!err) {
Alexander Duyck59d71982010-04-27 13:09:25 +00001984 err = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64));
Auke Kok9d5c8242008-01-24 02:22:38 -08001985 if (!err)
1986 pci_using_dac = 1;
1987 } else {
Alexander Duyck59d71982010-04-27 13:09:25 +00001988 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
Auke Kok9d5c8242008-01-24 02:22:38 -08001989 if (err) {
Alexander Duyck59d71982010-04-27 13:09:25 +00001990 err = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
Auke Kok9d5c8242008-01-24 02:22:38 -08001991 if (err) {
1992 dev_err(&pdev->dev, "No usable DMA "
1993 "configuration, aborting\n");
1994 goto err_dma;
1995 }
1996 }
1997 }
1998
Alexander Duyckaed5dec2009-02-06 23:16:04 +00001999 err = pci_request_selected_regions(pdev, pci_select_bars(pdev,
2000 IORESOURCE_MEM),
2001 igb_driver_name);
Auke Kok9d5c8242008-01-24 02:22:38 -08002002 if (err)
2003 goto err_pci_reg;
2004
Frans Pop19d5afd2009-10-02 10:04:12 -07002005 pci_enable_pcie_error_reporting(pdev);
Alexander Duyck40a914f2008-11-27 00:24:37 -08002006
Auke Kok9d5c8242008-01-24 02:22:38 -08002007 pci_set_master(pdev);
Auke Kokc682fc22008-04-23 11:09:34 -07002008 pci_save_state(pdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08002009
2010 err = -ENOMEM;
Alexander Duyck1bfaf072009-02-19 20:39:23 -08002011 netdev = alloc_etherdev_mq(sizeof(struct igb_adapter),
Alexander Duyck1cc3bd82011-08-26 07:44:10 +00002012 IGB_MAX_TX_QUEUES);
Auke Kok9d5c8242008-01-24 02:22:38 -08002013 if (!netdev)
2014 goto err_alloc_etherdev;
2015
2016 SET_NETDEV_DEV(netdev, &pdev->dev);
2017
2018 pci_set_drvdata(pdev, netdev);
2019 adapter = netdev_priv(netdev);
2020 adapter->netdev = netdev;
2021 adapter->pdev = pdev;
2022 hw = &adapter->hw;
2023 hw->back = adapter;
stephen hemmingerb3f4d592012-03-13 06:04:20 +00002024 adapter->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
Auke Kok9d5c8242008-01-24 02:22:38 -08002025
2026 mmio_start = pci_resource_start(pdev, 0);
2027 mmio_len = pci_resource_len(pdev, 0);
2028
2029 err = -EIO;
Alexander Duyck28b07592009-02-06 23:20:31 +00002030 hw->hw_addr = ioremap(mmio_start, mmio_len);
2031 if (!hw->hw_addr)
Auke Kok9d5c8242008-01-24 02:22:38 -08002032 goto err_ioremap;
2033
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08002034 netdev->netdev_ops = &igb_netdev_ops;
Auke Kok9d5c8242008-01-24 02:22:38 -08002035 igb_set_ethtool_ops(netdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08002036 netdev->watchdog_timeo = 5 * HZ;
Auke Kok9d5c8242008-01-24 02:22:38 -08002037
2038 strncpy(netdev->name, pci_name(pdev), sizeof(netdev->name) - 1);
2039
2040 netdev->mem_start = mmio_start;
2041 netdev->mem_end = mmio_start + mmio_len;
2042
Auke Kok9d5c8242008-01-24 02:22:38 -08002043 /* PCI config space info */
2044 hw->vendor_id = pdev->vendor;
2045 hw->device_id = pdev->device;
2046 hw->revision_id = pdev->revision;
2047 hw->subsystem_vendor_id = pdev->subsystem_vendor;
2048 hw->subsystem_device_id = pdev->subsystem_device;
2049
Auke Kok9d5c8242008-01-24 02:22:38 -08002050 /* Copy the default MAC, PHY and NVM function pointers */
2051 memcpy(&hw->mac.ops, ei->mac_ops, sizeof(hw->mac.ops));
2052 memcpy(&hw->phy.ops, ei->phy_ops, sizeof(hw->phy.ops));
2053 memcpy(&hw->nvm.ops, ei->nvm_ops, sizeof(hw->nvm.ops));
2054 /* Initialize skew-specific constants */
2055 err = ei->get_invariants(hw);
2056 if (err)
Alexander Duyck450c87c2009-02-06 23:22:11 +00002057 goto err_sw_init;
Auke Kok9d5c8242008-01-24 02:22:38 -08002058
Alexander Duyck450c87c2009-02-06 23:22:11 +00002059 /* setup the private structure */
Auke Kok9d5c8242008-01-24 02:22:38 -08002060 err = igb_sw_init(adapter);
2061 if (err)
2062 goto err_sw_init;
2063
2064 igb_get_bus_info_pcie(hw);
2065
2066 hw->phy.autoneg_wait_to_complete = false;
Auke Kok9d5c8242008-01-24 02:22:38 -08002067
2068 /* Copper options */
2069 if (hw->phy.media_type == e1000_media_type_copper) {
2070 hw->phy.mdix = AUTO_ALL_MODES;
2071 hw->phy.disable_polarity_correction = false;
2072 hw->phy.ms_type = e1000_ms_hw_default;
2073 }
2074
2075 if (igb_check_reset_block(hw))
2076 dev_info(&pdev->dev,
2077 "PHY reset is blocked due to SOL/IDER session.\n");
2078
Alexander Duyck077887c2011-08-26 07:46:29 +00002079 /*
2080 * features is initialized to 0 in allocation, it might have bits
2081 * set by igb_sw_init so we should use an or instead of an
2082 * assignment.
2083 */
2084 netdev->features |= NETIF_F_SG |
2085 NETIF_F_IP_CSUM |
2086 NETIF_F_IPV6_CSUM |
2087 NETIF_F_TSO |
2088 NETIF_F_TSO6 |
2089 NETIF_F_RXHASH |
2090 NETIF_F_RXCSUM |
2091 NETIF_F_HW_VLAN_RX |
2092 NETIF_F_HW_VLAN_TX;
Michał Mirosławac52caa2011-06-08 08:38:01 +00002093
Alexander Duyck077887c2011-08-26 07:46:29 +00002094 /* copy netdev features into list of user selectable features */
2095 netdev->hw_features |= netdev->features;
Ben Greear89eaefb2012-03-06 09:41:58 +00002096 netdev->hw_features |= NETIF_F_RXALL;
Auke Kok9d5c8242008-01-24 02:22:38 -08002097
Alexander Duyck077887c2011-08-26 07:46:29 +00002098 /* set this bit last since it cannot be part of hw_features */
2099 netdev->features |= NETIF_F_HW_VLAN_FILTER;
2100
2101 netdev->vlan_features |= NETIF_F_TSO |
2102 NETIF_F_TSO6 |
2103 NETIF_F_IP_CSUM |
2104 NETIF_F_IPV6_CSUM |
2105 NETIF_F_SG;
Jeff Kirsher48f29ff2008-06-05 04:06:27 -07002106
Ben Greear6b8f0922012-03-06 09:41:53 +00002107 netdev->priv_flags |= IFF_SUPP_NOFCS;
2108
Yi Zou7b872a52010-09-22 17:57:58 +00002109 if (pci_using_dac) {
Auke Kok9d5c8242008-01-24 02:22:38 -08002110 netdev->features |= NETIF_F_HIGHDMA;
Yi Zou7b872a52010-09-22 17:57:58 +00002111 netdev->vlan_features |= NETIF_F_HIGHDMA;
2112 }
Auke Kok9d5c8242008-01-24 02:22:38 -08002113
Michał Mirosławac52caa2011-06-08 08:38:01 +00002114 if (hw->mac.type >= e1000_82576) {
2115 netdev->hw_features |= NETIF_F_SCTP_CSUM;
Jesse Brandeburgb9473562009-04-27 22:36:13 +00002116 netdev->features |= NETIF_F_SCTP_CSUM;
Michał Mirosławac52caa2011-06-08 08:38:01 +00002117 }
Jesse Brandeburgb9473562009-04-27 22:36:13 +00002118
Jiri Pirko01789342011-08-16 06:29:00 +00002119 netdev->priv_flags |= IFF_UNICAST_FLT;
2120
Alexander Duyck330a6d62009-10-27 23:51:35 +00002121 adapter->en_mng_pt = igb_enable_mng_pass_thru(hw);
Auke Kok9d5c8242008-01-24 02:22:38 -08002122
2123 /* before reading the NVM, reset the controller to put the device in a
2124 * known good starting state */
2125 hw->mac.ops.reset_hw(hw);
2126
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002127 /*
2128 * make sure the NVM is good , i211 parts have special NVM that
2129 * doesn't contain a checksum
2130 */
2131 if (hw->mac.type != e1000_i211) {
2132 if (hw->nvm.ops.validate(hw) < 0) {
2133 dev_err(&pdev->dev, "The NVM Checksum Is Not Valid\n");
2134 err = -EIO;
2135 goto err_eeprom;
2136 }
Auke Kok9d5c8242008-01-24 02:22:38 -08002137 }
2138
2139 /* copy the MAC address out of the NVM */
2140 if (hw->mac.ops.read_mac_addr(hw))
2141 dev_err(&pdev->dev, "NVM Read Error\n");
2142
2143 memcpy(netdev->dev_addr, hw->mac.addr, netdev->addr_len);
Auke Kok9d5c8242008-01-24 02:22:38 -08002144
Jiri Pirkoaaeb6cd2013-01-08 01:38:26 +00002145 if (!is_valid_ether_addr(netdev->dev_addr)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08002146 dev_err(&pdev->dev, "Invalid MAC Address\n");
2147 err = -EIO;
2148 goto err_eeprom;
2149 }
2150
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00002151 /* get firmware version for ethtool -i */
2152 igb_set_fw_version(adapter);
2153
Joe Perchesc061b182010-08-23 18:20:03 +00002154 setup_timer(&adapter->watchdog_timer, igb_watchdog,
Alexander Duyck0e340482009-03-20 00:17:08 +00002155 (unsigned long) adapter);
Joe Perchesc061b182010-08-23 18:20:03 +00002156 setup_timer(&adapter->phy_info_timer, igb_update_phy_info,
Alexander Duyck0e340482009-03-20 00:17:08 +00002157 (unsigned long) adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08002158
2159 INIT_WORK(&adapter->reset_task, igb_reset_task);
2160 INIT_WORK(&adapter->watchdog_task, igb_watchdog_task);
2161
Alexander Duyck450c87c2009-02-06 23:22:11 +00002162 /* Initialize link properties that are user-changeable */
Auke Kok9d5c8242008-01-24 02:22:38 -08002163 adapter->fc_autoneg = true;
2164 hw->mac.autoneg = true;
2165 hw->phy.autoneg_advertised = 0x2f;
2166
Alexander Duyck0cce1192009-07-23 18:10:24 +00002167 hw->fc.requested_mode = e1000_fc_default;
2168 hw->fc.current_mode = e1000_fc_default;
Auke Kok9d5c8242008-01-24 02:22:38 -08002169
Auke Kok9d5c8242008-01-24 02:22:38 -08002170 igb_validate_mdi_setting(hw);
2171
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002172 /* By default, support wake on port A */
Alexander Duycka2cf8b62009-03-13 20:41:17 +00002173 if (hw->bus.func == 0)
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002174 adapter->flags |= IGB_FLAG_WOL_SUPPORTED;
2175
2176 /* Check the NVM for wake support on non-port A ports */
2177 if (hw->mac.type >= e1000_82580)
Alexander Duyck55cac242009-11-19 12:42:21 +00002178 hw->nvm.ops.read(hw, NVM_INIT_CONTROL3_PORT_A +
2179 NVM_82580_LAN_FUNC_OFFSET(hw->bus.func), 1,
2180 &eeprom_data);
Alexander Duycka2cf8b62009-03-13 20:41:17 +00002181 else if (hw->bus.func == 1)
2182 hw->nvm.ops.read(hw, NVM_INIT_CONTROL3_PORT_B, 1, &eeprom_data);
Auke Kok9d5c8242008-01-24 02:22:38 -08002183
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002184 if (eeprom_data & IGB_EEPROM_APME)
2185 adapter->flags |= IGB_FLAG_WOL_SUPPORTED;
Auke Kok9d5c8242008-01-24 02:22:38 -08002186
2187 /* now that we have the eeprom settings, apply the special cases where
2188 * the eeprom may be wrong or the board simply won't support wake on
2189 * lan on a particular port */
2190 switch (pdev->device) {
2191 case E1000_DEV_ID_82575GB_QUAD_COPPER:
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002192 adapter->flags &= ~IGB_FLAG_WOL_SUPPORTED;
Auke Kok9d5c8242008-01-24 02:22:38 -08002193 break;
2194 case E1000_DEV_ID_82575EB_FIBER_SERDES:
Alexander Duyck2d064c02008-07-08 15:10:12 -07002195 case E1000_DEV_ID_82576_FIBER:
2196 case E1000_DEV_ID_82576_SERDES:
Auke Kok9d5c8242008-01-24 02:22:38 -08002197 /* Wake events only supported on port A for dual fiber
2198 * regardless of eeprom setting */
2199 if (rd32(E1000_STATUS) & E1000_STATUS_FUNC_1)
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002200 adapter->flags &= ~IGB_FLAG_WOL_SUPPORTED;
Auke Kok9d5c8242008-01-24 02:22:38 -08002201 break;
Alexander Duyckc8ea5ea2009-03-13 20:42:35 +00002202 case E1000_DEV_ID_82576_QUAD_COPPER:
Stefan Assmannd5aa2252010-04-09 09:51:34 +00002203 case E1000_DEV_ID_82576_QUAD_COPPER_ET2:
Alexander Duyckc8ea5ea2009-03-13 20:42:35 +00002204 /* if quad port adapter, disable WoL on all but port A */
2205 if (global_quad_port_a != 0)
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002206 adapter->flags &= ~IGB_FLAG_WOL_SUPPORTED;
Alexander Duyckc8ea5ea2009-03-13 20:42:35 +00002207 else
2208 adapter->flags |= IGB_FLAG_QUAD_PORT_A;
2209 /* Reset for multiple quad port adapters */
2210 if (++global_quad_port_a == 4)
2211 global_quad_port_a = 0;
2212 break;
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002213 default:
2214 /* If the device can't wake, don't set software support */
2215 if (!device_can_wakeup(&adapter->pdev->dev))
2216 adapter->flags &= ~IGB_FLAG_WOL_SUPPORTED;
Auke Kok9d5c8242008-01-24 02:22:38 -08002217 }
2218
2219 /* initialize the wol settings based on the eeprom settings */
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002220 if (adapter->flags & IGB_FLAG_WOL_SUPPORTED)
2221 adapter->wol |= E1000_WUFC_MAG;
2222
2223 /* Some vendors want WoL disabled by default, but still supported */
2224 if ((hw->mac.type == e1000_i350) &&
2225 (pdev->subsystem_vendor == PCI_VENDOR_ID_HP)) {
2226 adapter->flags |= IGB_FLAG_WOL_SUPPORTED;
2227 adapter->wol = 0;
2228 }
2229
2230 device_set_wakeup_enable(&adapter->pdev->dev,
2231 adapter->flags & IGB_FLAG_WOL_SUPPORTED);
Auke Kok9d5c8242008-01-24 02:22:38 -08002232
2233 /* reset the hardware with the new settings */
2234 igb_reset(adapter);
2235
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +00002236 /* Init the I2C interface */
2237 err = igb_init_i2c(adapter);
2238 if (err) {
2239 dev_err(&pdev->dev, "failed to init i2c interface\n");
2240 goto err_eeprom;
2241 }
2242
Auke Kok9d5c8242008-01-24 02:22:38 -08002243 /* let the f/w know that the h/w is now under the control of the
2244 * driver. */
2245 igb_get_hw_control(adapter);
2246
Auke Kok9d5c8242008-01-24 02:22:38 -08002247 strcpy(netdev->name, "eth%d");
2248 err = register_netdev(netdev);
2249 if (err)
2250 goto err_register;
2251
Jesse Brandeburgb168dfc2009-04-17 20:44:32 +00002252 /* carrier off reporting is important to ethtool even BEFORE open */
2253 netif_carrier_off(netdev);
2254
Jeff Kirsher421e02f2008-10-17 11:08:31 -07002255#ifdef CONFIG_IGB_DCA
Alexander Duyckbbd98fe2009-01-31 00:52:30 -08002256 if (dca_add_requester(&pdev->dev) == 0) {
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07002257 adapter->flags |= IGB_FLAG_DCA_ENABLED;
Jeb Cramerfe4506b2008-07-08 15:07:55 -07002258 dev_info(&pdev->dev, "DCA enabled\n");
Jeb Cramerfe4506b2008-07-08 15:07:55 -07002259 igb_setup_dca(adapter);
2260 }
Alexander Duyckc5b9bd52009-10-27 23:46:01 +00002261
Jeb Cramerfe4506b2008-07-08 15:07:55 -07002262#endif
Matthew Vick3c89f6d2012-08-10 05:40:43 +00002263
Anders Berggren673b8b72011-02-04 07:32:32 +00002264 /* do hw tstamp init after resetting */
Richard Cochran7ebae812012-03-16 10:55:37 +00002265 igb_ptp_init(adapter);
Anders Berggren673b8b72011-02-04 07:32:32 +00002266
Auke Kok9d5c8242008-01-24 02:22:38 -08002267 dev_info(&pdev->dev, "Intel(R) Gigabit Ethernet Network Connection\n");
2268 /* print bus type/speed/width info */
Johannes Berg7c510e42008-10-27 17:47:26 -07002269 dev_info(&pdev->dev, "%s: (PCIe:%s:%s) %pM\n",
Auke Kok9d5c8242008-01-24 02:22:38 -08002270 netdev->name,
Alexander Duyck559e9c42009-10-27 23:52:50 +00002271 ((hw->bus.speed == e1000_bus_speed_2500) ? "2.5Gb/s" :
Alexander Duyckff846f52010-04-27 01:02:40 +00002272 (hw->bus.speed == e1000_bus_speed_5000) ? "5.0Gb/s" :
Alexander Duyck559e9c42009-10-27 23:52:50 +00002273 "unknown"),
Alexander Duyck59c3de82009-03-31 20:38:00 +00002274 ((hw->bus.width == e1000_bus_width_pcie_x4) ? "Width x4" :
2275 (hw->bus.width == e1000_bus_width_pcie_x2) ? "Width x2" :
2276 (hw->bus.width == e1000_bus_width_pcie_x1) ? "Width x1" :
2277 "unknown"),
Johannes Berg7c510e42008-10-27 17:47:26 -07002278 netdev->dev_addr);
Auke Kok9d5c8242008-01-24 02:22:38 -08002279
Carolyn Wyborny9835fd72010-11-22 17:17:21 +00002280 ret_val = igb_read_part_string(hw, part_str, E1000_PBANUM_LENGTH);
2281 if (ret_val)
2282 strcpy(part_str, "Unknown");
2283 dev_info(&pdev->dev, "%s: PBA No: %s\n", netdev->name, part_str);
Auke Kok9d5c8242008-01-24 02:22:38 -08002284 dev_info(&pdev->dev,
2285 "Using %s interrupts. %d rx queue(s), %d tx queue(s)\n",
2286 adapter->msix_entries ? "MSI-X" :
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07002287 (adapter->flags & IGB_FLAG_HAS_MSI) ? "MSI" : "legacy",
Auke Kok9d5c8242008-01-24 02:22:38 -08002288 adapter->num_rx_queues, adapter->num_tx_queues);
Carolyn Wyborny09b068d2011-03-11 20:42:13 -08002289 switch (hw->mac.type) {
2290 case e1000_i350:
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002291 case e1000_i210:
2292 case e1000_i211:
Carolyn Wyborny09b068d2011-03-11 20:42:13 -08002293 igb_set_eee_i350(hw);
2294 break;
2295 default:
2296 break;
2297 }
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002298
2299 pm_runtime_put_noidle(&pdev->dev);
Auke Kok9d5c8242008-01-24 02:22:38 -08002300 return 0;
2301
2302err_register:
2303 igb_release_hw_control(adapter);
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +00002304 memset(&adapter->i2c_adap, 0, sizeof(adapter->i2c_adap));
Auke Kok9d5c8242008-01-24 02:22:38 -08002305err_eeprom:
2306 if (!igb_check_reset_block(hw))
Alexander Duyckf5f4cf02008-11-21 21:30:24 -08002307 igb_reset_phy(hw);
Auke Kok9d5c8242008-01-24 02:22:38 -08002308
2309 if (hw->flash_address)
2310 iounmap(hw->flash_address);
Auke Kok9d5c8242008-01-24 02:22:38 -08002311err_sw_init:
Alexander Duyck047e0032009-10-27 15:49:27 +00002312 igb_clear_interrupt_scheme(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08002313 iounmap(hw->hw_addr);
2314err_ioremap:
2315 free_netdev(netdev);
2316err_alloc_etherdev:
Alexander Duyck559e9c42009-10-27 23:52:50 +00002317 pci_release_selected_regions(pdev,
2318 pci_select_bars(pdev, IORESOURCE_MEM));
Auke Kok9d5c8242008-01-24 02:22:38 -08002319err_pci_reg:
2320err_dma:
2321 pci_disable_device(pdev);
2322 return err;
2323}
2324
Greg Rosefa44f2f2013-01-17 01:03:06 -08002325#ifdef CONFIG_PCI_IOV
2326static int igb_disable_sriov(struct pci_dev *pdev)
2327{
2328 struct net_device *netdev = pci_get_drvdata(pdev);
2329 struct igb_adapter *adapter = netdev_priv(netdev);
2330 struct e1000_hw *hw = &adapter->hw;
2331
2332 /* reclaim resources allocated to VFs */
2333 if (adapter->vf_data) {
2334 /* disable iov and allow time for transactions to clear */
2335 if (igb_vfs_are_assigned(adapter)) {
2336 dev_warn(&pdev->dev,
2337 "Cannot deallocate SR-IOV virtual functions while they are assigned - VFs will not be deallocated\n");
2338 return -EPERM;
2339 } else {
2340 pci_disable_sriov(pdev);
2341 msleep(500);
2342 }
2343
2344 kfree(adapter->vf_data);
2345 adapter->vf_data = NULL;
2346 adapter->vfs_allocated_count = 0;
2347 wr32(E1000_IOVCTL, E1000_IOVCTL_REUSE_VFQ);
2348 wrfl();
2349 msleep(100);
2350 dev_info(&pdev->dev, "IOV Disabled\n");
2351
2352 /* Re-enable DMA Coalescing flag since IOV is turned off */
2353 adapter->flags |= IGB_FLAG_DMAC;
2354 }
2355
2356 return 0;
2357}
2358
2359static int igb_enable_sriov(struct pci_dev *pdev, int num_vfs)
2360{
2361 struct net_device *netdev = pci_get_drvdata(pdev);
2362 struct igb_adapter *adapter = netdev_priv(netdev);
2363 int old_vfs = pci_num_vf(pdev);
2364 int err = 0;
2365 int i;
2366
2367 if (!num_vfs)
2368 goto out;
2369 else if (old_vfs && old_vfs == num_vfs)
2370 goto out;
2371 else if (old_vfs && old_vfs != num_vfs)
2372 err = igb_disable_sriov(pdev);
2373
2374 if (err)
2375 goto out;
2376
2377 if (num_vfs > 7) {
2378 err = -EPERM;
2379 goto out;
2380 }
2381
2382 adapter->vfs_allocated_count = num_vfs;
2383
2384 adapter->vf_data = kcalloc(adapter->vfs_allocated_count,
2385 sizeof(struct vf_data_storage), GFP_KERNEL);
2386
2387 /* if allocation failed then we do not support SR-IOV */
2388 if (!adapter->vf_data) {
2389 adapter->vfs_allocated_count = 0;
2390 dev_err(&pdev->dev,
2391 "Unable to allocate memory for VF Data Storage\n");
2392 err = -ENOMEM;
2393 goto out;
2394 }
2395
2396 err = pci_enable_sriov(pdev, adapter->vfs_allocated_count);
2397 if (err)
2398 goto err_out;
2399
2400 dev_info(&pdev->dev, "%d VFs allocated\n",
2401 adapter->vfs_allocated_count);
2402 for (i = 0; i < adapter->vfs_allocated_count; i++)
2403 igb_vf_configure(adapter, i);
2404
2405 /* DMA Coalescing is not supported in IOV mode. */
2406 adapter->flags &= ~IGB_FLAG_DMAC;
2407 goto out;
2408
2409err_out:
2410 kfree(adapter->vf_data);
2411 adapter->vf_data = NULL;
2412 adapter->vfs_allocated_count = 0;
2413out:
2414 return err;
2415}
2416
2417#endif
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +00002418/*
2419 * igb_remove_i2c - Cleanup I2C interface
2420 * @adapter: pointer to adapter structure
2421 *
2422 */
2423static void igb_remove_i2c(struct igb_adapter *adapter)
2424{
2425
2426 /* free the adapter bus structure */
2427 i2c_del_adapter(&adapter->i2c_adap);
2428}
2429
Auke Kok9d5c8242008-01-24 02:22:38 -08002430/**
2431 * igb_remove - Device Removal Routine
2432 * @pdev: PCI device information struct
2433 *
2434 * igb_remove is called by the PCI subsystem to alert the driver
2435 * that it should release a PCI device. The could be caused by a
2436 * Hot-Plug event, or because the driver is going to be removed from
2437 * memory.
2438 **/
Bill Pemberton9f9a12f2012-12-03 09:24:25 -05002439static void igb_remove(struct pci_dev *pdev)
Auke Kok9d5c8242008-01-24 02:22:38 -08002440{
2441 struct net_device *netdev = pci_get_drvdata(pdev);
2442 struct igb_adapter *adapter = netdev_priv(netdev);
Jeb Cramerfe4506b2008-07-08 15:07:55 -07002443 struct e1000_hw *hw = &adapter->hw;
Auke Kok9d5c8242008-01-24 02:22:38 -08002444
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002445 pm_runtime_get_noresume(&pdev->dev);
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +00002446 igb_remove_i2c(adapter);
2447
Matthew Vicka79f4f82012-08-10 05:40:44 +00002448 igb_ptp_stop(adapter);
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002449
Tejun Heo760141a2010-12-12 16:45:14 +01002450 /*
2451 * The watchdog timer may be rescheduled, so explicitly
2452 * disable watchdog from being rescheduled.
2453 */
Auke Kok9d5c8242008-01-24 02:22:38 -08002454 set_bit(__IGB_DOWN, &adapter->state);
2455 del_timer_sync(&adapter->watchdog_timer);
2456 del_timer_sync(&adapter->phy_info_timer);
2457
Tejun Heo760141a2010-12-12 16:45:14 +01002458 cancel_work_sync(&adapter->reset_task);
2459 cancel_work_sync(&adapter->watchdog_task);
Auke Kok9d5c8242008-01-24 02:22:38 -08002460
Jeff Kirsher421e02f2008-10-17 11:08:31 -07002461#ifdef CONFIG_IGB_DCA
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07002462 if (adapter->flags & IGB_FLAG_DCA_ENABLED) {
Jeb Cramerfe4506b2008-07-08 15:07:55 -07002463 dev_info(&pdev->dev, "DCA disabled\n");
2464 dca_remove_requester(&pdev->dev);
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07002465 adapter->flags &= ~IGB_FLAG_DCA_ENABLED;
Alexander Duyckcbd347a2009-02-15 23:59:44 -08002466 wr32(E1000_DCA_CTRL, E1000_DCA_CTRL_DCA_MODE_DISABLE);
Jeb Cramerfe4506b2008-07-08 15:07:55 -07002467 }
2468#endif
2469
Auke Kok9d5c8242008-01-24 02:22:38 -08002470 /* Release control of h/w to f/w. If f/w is AMT enabled, this
2471 * would have already happened in close and is redundant. */
2472 igb_release_hw_control(adapter);
2473
2474 unregister_netdev(netdev);
2475
Alexander Duyck047e0032009-10-27 15:49:27 +00002476 igb_clear_interrupt_scheme(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08002477
Alexander Duyck37680112009-02-19 20:40:30 -08002478#ifdef CONFIG_PCI_IOV
Greg Rosefa44f2f2013-01-17 01:03:06 -08002479 igb_disable_sriov(pdev);
Alexander Duyck37680112009-02-19 20:40:30 -08002480#endif
Alexander Duyck559e9c42009-10-27 23:52:50 +00002481
Alexander Duyck28b07592009-02-06 23:20:31 +00002482 iounmap(hw->hw_addr);
2483 if (hw->flash_address)
2484 iounmap(hw->flash_address);
Alexander Duyck559e9c42009-10-27 23:52:50 +00002485 pci_release_selected_regions(pdev,
2486 pci_select_bars(pdev, IORESOURCE_MEM));
Auke Kok9d5c8242008-01-24 02:22:38 -08002487
Carolyn Wyborny1128c752011-10-14 00:13:49 +00002488 kfree(adapter->shadow_vfta);
Auke Kok9d5c8242008-01-24 02:22:38 -08002489 free_netdev(netdev);
2490
Frans Pop19d5afd2009-10-02 10:04:12 -07002491 pci_disable_pcie_error_reporting(pdev);
Alexander Duyck40a914f2008-11-27 00:24:37 -08002492
Auke Kok9d5c8242008-01-24 02:22:38 -08002493 pci_disable_device(pdev);
2494}
2495
2496/**
Alexander Duycka6b623e2009-10-27 23:47:53 +00002497 * igb_probe_vfs - Initialize vf data storage and add VFs to pci config space
2498 * @adapter: board private structure to initialize
2499 *
2500 * This function initializes the vf specific data storage and then attempts to
2501 * allocate the VFs. The reason for ordering it this way is because it is much
2502 * mor expensive time wise to disable SR-IOV than it is to allocate and free
2503 * the memory for the VFs.
2504 **/
Bill Pemberton9f9a12f2012-12-03 09:24:25 -05002505static void igb_probe_vfs(struct igb_adapter *adapter)
Alexander Duycka6b623e2009-10-27 23:47:53 +00002506{
2507#ifdef CONFIG_PCI_IOV
2508 struct pci_dev *pdev = adapter->pdev;
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002509 struct e1000_hw *hw = &adapter->hw;
Alexander Duycka6b623e2009-10-27 23:47:53 +00002510
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002511 /* Virtualization features not supported on i210 family. */
2512 if ((hw->mac.type == e1000_i210) || (hw->mac.type == e1000_i211))
2513 return;
2514
Greg Rosefa44f2f2013-01-17 01:03:06 -08002515 igb_enable_sriov(pdev, max_vfs);
2516 pci_sriov_set_totalvfs(pdev, 7);
Alexander Duycka6b623e2009-10-27 23:47:53 +00002517
Alexander Duycka6b623e2009-10-27 23:47:53 +00002518#endif /* CONFIG_PCI_IOV */
2519}
2520
Greg Rosefa44f2f2013-01-17 01:03:06 -08002521static void igb_init_queue_configuration(struct igb_adapter *adapter)
Auke Kok9d5c8242008-01-24 02:22:38 -08002522{
2523 struct e1000_hw *hw = &adapter->hw;
Matthew Vick374a5422012-05-18 04:54:58 +00002524 u32 max_rss_queues;
Auke Kok9d5c8242008-01-24 02:22:38 -08002525
Matthew Vick374a5422012-05-18 04:54:58 +00002526 /* Determine the maximum number of RSS queues supported. */
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002527 switch (hw->mac.type) {
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002528 case e1000_i211:
Matthew Vick374a5422012-05-18 04:54:58 +00002529 max_rss_queues = IGB_MAX_RX_QUEUES_I211;
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002530 break;
Matthew Vick374a5422012-05-18 04:54:58 +00002531 case e1000_82575:
2532 case e1000_i210:
2533 max_rss_queues = IGB_MAX_RX_QUEUES_82575;
2534 break;
2535 case e1000_i350:
2536 /* I350 cannot do RSS and SR-IOV at the same time */
2537 if (!!adapter->vfs_allocated_count) {
2538 max_rss_queues = 1;
2539 break;
2540 }
2541 /* fall through */
2542 case e1000_82576:
2543 if (!!adapter->vfs_allocated_count) {
2544 max_rss_queues = 2;
2545 break;
2546 }
2547 /* fall through */
2548 case e1000_82580:
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002549 default:
Matthew Vick374a5422012-05-18 04:54:58 +00002550 max_rss_queues = IGB_MAX_RX_QUEUES;
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002551 break;
2552 }
Alexander Duycka99955f2009-11-12 18:37:19 +00002553
Matthew Vick374a5422012-05-18 04:54:58 +00002554 adapter->rss_queues = min_t(u32, max_rss_queues, num_online_cpus());
2555
2556 /* Determine if we need to pair queues. */
2557 switch (hw->mac.type) {
2558 case e1000_82575:
2559 case e1000_i211:
2560 /* Device supports enough interrupts without queue pairing. */
2561 break;
2562 case e1000_82576:
2563 /*
2564 * If VFs are going to be allocated with RSS queues then we
2565 * should pair the queues in order to conserve interrupts due
2566 * to limited supply.
2567 */
2568 if ((adapter->rss_queues > 1) &&
2569 (adapter->vfs_allocated_count > 6))
2570 adapter->flags |= IGB_FLAG_QUEUE_PAIRS;
2571 /* fall through */
2572 case e1000_82580:
2573 case e1000_i350:
2574 case e1000_i210:
2575 default:
2576 /*
2577 * If rss_queues > half of max_rss_queues, pair the queues in
2578 * order to conserve interrupts due to limited supply.
2579 */
2580 if (adapter->rss_queues > (max_rss_queues / 2))
2581 adapter->flags |= IGB_FLAG_QUEUE_PAIRS;
2582 break;
2583 }
Greg Rosefa44f2f2013-01-17 01:03:06 -08002584}
2585
2586/**
2587 * igb_sw_init - Initialize general software structures (struct igb_adapter)
2588 * @adapter: board private structure to initialize
2589 *
2590 * igb_sw_init initializes the Adapter private data structure.
2591 * Fields are initialized based on PCI device information and
2592 * OS network device settings (MTU size).
2593 **/
2594static int igb_sw_init(struct igb_adapter *adapter)
2595{
2596 struct e1000_hw *hw = &adapter->hw;
2597 struct net_device *netdev = adapter->netdev;
2598 struct pci_dev *pdev = adapter->pdev;
2599
2600 pci_read_config_word(pdev, PCI_COMMAND, &hw->bus.pci_cmd_word);
2601
2602 /* set default ring sizes */
2603 adapter->tx_ring_count = IGB_DEFAULT_TXD;
2604 adapter->rx_ring_count = IGB_DEFAULT_RXD;
2605
2606 /* set default ITR values */
2607 adapter->rx_itr_setting = IGB_DEFAULT_ITR;
2608 adapter->tx_itr_setting = IGB_DEFAULT_ITR;
2609
2610 /* set default work limits */
2611 adapter->tx_work_limit = IGB_DEFAULT_TX_WORK;
2612
2613 adapter->max_frame_size = netdev->mtu + ETH_HLEN + ETH_FCS_LEN +
2614 VLAN_HLEN;
2615 adapter->min_frame_size = ETH_ZLEN + ETH_FCS_LEN;
2616
2617 spin_lock_init(&adapter->stats64_lock);
2618#ifdef CONFIG_PCI_IOV
2619 switch (hw->mac.type) {
2620 case e1000_82576:
2621 case e1000_i350:
2622 if (max_vfs > 7) {
2623 dev_warn(&pdev->dev,
2624 "Maximum of 7 VFs per PF, using max\n");
2625 adapter->vfs_allocated_count = 7;
2626 } else
2627 adapter->vfs_allocated_count = max_vfs;
2628 if (adapter->vfs_allocated_count)
2629 dev_warn(&pdev->dev,
2630 "Enabling SR-IOV VFs using the module parameter is deprecated - please use the pci sysfs interface.\n");
2631 break;
2632 default:
2633 break;
2634 }
2635#endif /* CONFIG_PCI_IOV */
2636
2637 igb_init_queue_configuration(adapter);
Alexander Duycka99955f2009-11-12 18:37:19 +00002638
Carolyn Wyborny1128c752011-10-14 00:13:49 +00002639 /* Setup and initialize a copy of the hw vlan table array */
2640 adapter->shadow_vfta = kzalloc(sizeof(u32) *
2641 E1000_VLAN_FILTER_TBL_SIZE,
2642 GFP_ATOMIC);
2643
Alexander Duycka6b623e2009-10-27 23:47:53 +00002644 /* This call may decrease the number of queues */
Stefan Assmann53c7d062012-12-04 06:00:12 +00002645 if (igb_init_interrupt_scheme(adapter, true)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08002646 dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
2647 return -ENOMEM;
2648 }
2649
Alexander Duycka6b623e2009-10-27 23:47:53 +00002650 igb_probe_vfs(adapter);
2651
Auke Kok9d5c8242008-01-24 02:22:38 -08002652 /* Explicitly disable IRQ since the NIC can be in any state. */
2653 igb_irq_disable(adapter);
2654
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002655 if (hw->mac.type >= e1000_i350)
Carolyn Wyborny831ec0b2011-03-11 20:43:54 -08002656 adapter->flags &= ~IGB_FLAG_DMAC;
2657
Auke Kok9d5c8242008-01-24 02:22:38 -08002658 set_bit(__IGB_DOWN, &adapter->state);
2659 return 0;
2660}
2661
2662/**
2663 * igb_open - Called when a network interface is made active
2664 * @netdev: network interface device structure
2665 *
2666 * Returns 0 on success, negative value on failure
2667 *
2668 * The open entry point is called when a network interface is made
2669 * active by the system (IFF_UP). At this point all resources needed
2670 * for transmit and receive operations are allocated, the interrupt
2671 * handler is registered with the OS, the watchdog timer is started,
2672 * and the stack is notified that the interface is ready.
2673 **/
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002674static int __igb_open(struct net_device *netdev, bool resuming)
Auke Kok9d5c8242008-01-24 02:22:38 -08002675{
2676 struct igb_adapter *adapter = netdev_priv(netdev);
2677 struct e1000_hw *hw = &adapter->hw;
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002678 struct pci_dev *pdev = adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08002679 int err;
2680 int i;
2681
2682 /* disallow open during test */
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002683 if (test_bit(__IGB_TESTING, &adapter->state)) {
2684 WARN_ON(resuming);
Auke Kok9d5c8242008-01-24 02:22:38 -08002685 return -EBUSY;
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002686 }
2687
2688 if (!resuming)
2689 pm_runtime_get_sync(&pdev->dev);
Auke Kok9d5c8242008-01-24 02:22:38 -08002690
Jesse Brandeburgb168dfc2009-04-17 20:44:32 +00002691 netif_carrier_off(netdev);
2692
Auke Kok9d5c8242008-01-24 02:22:38 -08002693 /* allocate transmit descriptors */
2694 err = igb_setup_all_tx_resources(adapter);
2695 if (err)
2696 goto err_setup_tx;
2697
2698 /* allocate receive descriptors */
2699 err = igb_setup_all_rx_resources(adapter);
2700 if (err)
2701 goto err_setup_rx;
2702
Nick Nunley88a268c2010-02-17 01:01:59 +00002703 igb_power_up_link(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08002704
Auke Kok9d5c8242008-01-24 02:22:38 -08002705 /* before we allocate an interrupt, we must be ready to handle it.
2706 * Setting DEBUG_SHIRQ in the kernel makes it fire an interrupt
2707 * as soon as we call pci_request_irq, so we have to setup our
2708 * clean_rx handler before we do so. */
2709 igb_configure(adapter);
2710
2711 err = igb_request_irq(adapter);
2712 if (err)
2713 goto err_req_irq;
2714
Alexander Duyck0c2cc022012-09-25 00:31:22 +00002715 /* Notify the stack of the actual queue counts. */
2716 err = netif_set_real_num_tx_queues(adapter->netdev,
2717 adapter->num_tx_queues);
2718 if (err)
2719 goto err_set_queues;
2720
2721 err = netif_set_real_num_rx_queues(adapter->netdev,
2722 adapter->num_rx_queues);
2723 if (err)
2724 goto err_set_queues;
2725
Auke Kok9d5c8242008-01-24 02:22:38 -08002726 /* From here on the code is the same as igb_up() */
2727 clear_bit(__IGB_DOWN, &adapter->state);
2728
Alexander Duyck0d1ae7f2011-08-26 07:46:34 +00002729 for (i = 0; i < adapter->num_q_vectors; i++)
2730 napi_enable(&(adapter->q_vector[i]->napi));
Auke Kok9d5c8242008-01-24 02:22:38 -08002731
2732 /* Clear any pending interrupts. */
2733 rd32(E1000_ICR);
PJ Waskiewicz844290e2008-06-27 11:00:39 -07002734
2735 igb_irq_enable(adapter);
2736
Alexander Duyckd4960302009-10-27 15:53:45 +00002737 /* notify VFs that reset has been completed */
2738 if (adapter->vfs_allocated_count) {
2739 u32 reg_data = rd32(E1000_CTRL_EXT);
2740 reg_data |= E1000_CTRL_EXT_PFRSTD;
2741 wr32(E1000_CTRL_EXT, reg_data);
2742 }
2743
Jeff Kirsherd55b53f2008-07-18 04:33:03 -07002744 netif_tx_start_all_queues(netdev);
2745
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002746 if (!resuming)
2747 pm_runtime_put(&pdev->dev);
2748
Alexander Duyck25568a52009-10-27 23:49:59 +00002749 /* start the watchdog. */
2750 hw->mac.get_link_status = 1;
2751 schedule_work(&adapter->watchdog_task);
Auke Kok9d5c8242008-01-24 02:22:38 -08002752
2753 return 0;
2754
Alexander Duyck0c2cc022012-09-25 00:31:22 +00002755err_set_queues:
2756 igb_free_irq(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08002757err_req_irq:
2758 igb_release_hw_control(adapter);
Nick Nunley88a268c2010-02-17 01:01:59 +00002759 igb_power_down_link(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08002760 igb_free_all_rx_resources(adapter);
2761err_setup_rx:
2762 igb_free_all_tx_resources(adapter);
2763err_setup_tx:
2764 igb_reset(adapter);
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002765 if (!resuming)
2766 pm_runtime_put(&pdev->dev);
Auke Kok9d5c8242008-01-24 02:22:38 -08002767
2768 return err;
2769}
2770
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002771static int igb_open(struct net_device *netdev)
2772{
2773 return __igb_open(netdev, false);
2774}
2775
Auke Kok9d5c8242008-01-24 02:22:38 -08002776/**
2777 * igb_close - Disables a network interface
2778 * @netdev: network interface device structure
2779 *
2780 * Returns 0, this is not allowed to fail
2781 *
2782 * The close entry point is called when an interface is de-activated
2783 * by the OS. The hardware is still under the driver's control, but
2784 * needs to be disabled. A global MAC reset is issued to stop the
2785 * hardware, and all transmit and receive resources are freed.
2786 **/
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002787static int __igb_close(struct net_device *netdev, bool suspending)
Auke Kok9d5c8242008-01-24 02:22:38 -08002788{
2789 struct igb_adapter *adapter = netdev_priv(netdev);
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002790 struct pci_dev *pdev = adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08002791
2792 WARN_ON(test_bit(__IGB_RESETTING, &adapter->state));
Auke Kok9d5c8242008-01-24 02:22:38 -08002793
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002794 if (!suspending)
2795 pm_runtime_get_sync(&pdev->dev);
2796
2797 igb_down(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08002798 igb_free_irq(adapter);
2799
2800 igb_free_all_tx_resources(adapter);
2801 igb_free_all_rx_resources(adapter);
2802
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002803 if (!suspending)
2804 pm_runtime_put_sync(&pdev->dev);
Auke Kok9d5c8242008-01-24 02:22:38 -08002805 return 0;
2806}
2807
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002808static int igb_close(struct net_device *netdev)
2809{
2810 return __igb_close(netdev, false);
2811}
2812
Auke Kok9d5c8242008-01-24 02:22:38 -08002813/**
2814 * igb_setup_tx_resources - allocate Tx resources (Descriptors)
Auke Kok9d5c8242008-01-24 02:22:38 -08002815 * @tx_ring: tx descriptor ring (for a specific queue) to setup
2816 *
2817 * Return 0 on success, negative on failure
2818 **/
Alexander Duyck80785292009-10-27 15:51:47 +00002819int igb_setup_tx_resources(struct igb_ring *tx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08002820{
Alexander Duyck59d71982010-04-27 13:09:25 +00002821 struct device *dev = tx_ring->dev;
Auke Kok9d5c8242008-01-24 02:22:38 -08002822 int size;
2823
Alexander Duyck06034642011-08-26 07:44:22 +00002824 size = sizeof(struct igb_tx_buffer) * tx_ring->count;
Alexander Duyckf33005a2012-09-13 06:27:55 +00002825
2826 tx_ring->tx_buffer_info = vzalloc(size);
Alexander Duyck06034642011-08-26 07:44:22 +00002827 if (!tx_ring->tx_buffer_info)
Auke Kok9d5c8242008-01-24 02:22:38 -08002828 goto err;
Auke Kok9d5c8242008-01-24 02:22:38 -08002829
2830 /* round up to nearest 4K */
Alexander Duyck85e8d002009-02-16 00:00:20 -08002831 tx_ring->size = tx_ring->count * sizeof(union e1000_adv_tx_desc);
Auke Kok9d5c8242008-01-24 02:22:38 -08002832 tx_ring->size = ALIGN(tx_ring->size, 4096);
2833
Alexander Duyck5536d212012-09-25 00:31:17 +00002834 tx_ring->desc = dma_alloc_coherent(dev, tx_ring->size,
2835 &tx_ring->dma, GFP_KERNEL);
Auke Kok9d5c8242008-01-24 02:22:38 -08002836 if (!tx_ring->desc)
2837 goto err;
2838
Auke Kok9d5c8242008-01-24 02:22:38 -08002839 tx_ring->next_to_use = 0;
2840 tx_ring->next_to_clean = 0;
Alexander Duyck81c2fc22011-08-26 07:45:20 +00002841
Auke Kok9d5c8242008-01-24 02:22:38 -08002842 return 0;
2843
2844err:
Alexander Duyck06034642011-08-26 07:44:22 +00002845 vfree(tx_ring->tx_buffer_info);
Alexander Duyckf33005a2012-09-13 06:27:55 +00002846 tx_ring->tx_buffer_info = NULL;
2847 dev_err(dev, "Unable to allocate memory for the Tx descriptor ring\n");
Auke Kok9d5c8242008-01-24 02:22:38 -08002848 return -ENOMEM;
2849}
2850
2851/**
2852 * igb_setup_all_tx_resources - wrapper to allocate Tx resources
2853 * (Descriptors) for all queues
2854 * @adapter: board private structure
2855 *
2856 * Return 0 on success, negative on failure
2857 **/
2858static int igb_setup_all_tx_resources(struct igb_adapter *adapter)
2859{
Alexander Duyck439705e2009-10-27 23:49:20 +00002860 struct pci_dev *pdev = adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08002861 int i, err = 0;
2862
2863 for (i = 0; i < adapter->num_tx_queues; i++) {
Alexander Duyck3025a442010-02-17 01:02:39 +00002864 err = igb_setup_tx_resources(adapter->tx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08002865 if (err) {
Alexander Duyck439705e2009-10-27 23:49:20 +00002866 dev_err(&pdev->dev,
Auke Kok9d5c8242008-01-24 02:22:38 -08002867 "Allocation for Tx Queue %u failed\n", i);
2868 for (i--; i >= 0; i--)
Alexander Duyck3025a442010-02-17 01:02:39 +00002869 igb_free_tx_resources(adapter->tx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08002870 break;
2871 }
2872 }
2873
2874 return err;
2875}
2876
2877/**
Alexander Duyck85b430b2009-10-27 15:50:29 +00002878 * igb_setup_tctl - configure the transmit control registers
2879 * @adapter: Board private structure
Auke Kok9d5c8242008-01-24 02:22:38 -08002880 **/
Alexander Duyckd7ee5b32009-10-27 15:54:23 +00002881void igb_setup_tctl(struct igb_adapter *adapter)
Auke Kok9d5c8242008-01-24 02:22:38 -08002882{
Auke Kok9d5c8242008-01-24 02:22:38 -08002883 struct e1000_hw *hw = &adapter->hw;
2884 u32 tctl;
Auke Kok9d5c8242008-01-24 02:22:38 -08002885
Alexander Duyck85b430b2009-10-27 15:50:29 +00002886 /* disable queue 0 which is enabled by default on 82575 and 82576 */
2887 wr32(E1000_TXDCTL(0), 0);
Auke Kok9d5c8242008-01-24 02:22:38 -08002888
2889 /* Program the Transmit Control Register */
Auke Kok9d5c8242008-01-24 02:22:38 -08002890 tctl = rd32(E1000_TCTL);
2891 tctl &= ~E1000_TCTL_CT;
2892 tctl |= E1000_TCTL_PSP | E1000_TCTL_RTLC |
2893 (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
2894
2895 igb_config_collision_dist(hw);
2896
Auke Kok9d5c8242008-01-24 02:22:38 -08002897 /* Enable transmits */
2898 tctl |= E1000_TCTL_EN;
2899
2900 wr32(E1000_TCTL, tctl);
2901}
2902
2903/**
Alexander Duyck85b430b2009-10-27 15:50:29 +00002904 * igb_configure_tx_ring - Configure transmit ring after Reset
2905 * @adapter: board private structure
2906 * @ring: tx ring to configure
2907 *
2908 * Configure a transmit ring after a reset.
2909 **/
Alexander Duyckd7ee5b32009-10-27 15:54:23 +00002910void igb_configure_tx_ring(struct igb_adapter *adapter,
2911 struct igb_ring *ring)
Alexander Duyck85b430b2009-10-27 15:50:29 +00002912{
2913 struct e1000_hw *hw = &adapter->hw;
Alexander Duycka74420e2011-08-26 07:43:27 +00002914 u32 txdctl = 0;
Alexander Duyck85b430b2009-10-27 15:50:29 +00002915 u64 tdba = ring->dma;
2916 int reg_idx = ring->reg_idx;
2917
2918 /* disable the queue */
Alexander Duycka74420e2011-08-26 07:43:27 +00002919 wr32(E1000_TXDCTL(reg_idx), 0);
Alexander Duyck85b430b2009-10-27 15:50:29 +00002920 wrfl();
2921 mdelay(10);
2922
2923 wr32(E1000_TDLEN(reg_idx),
2924 ring->count * sizeof(union e1000_adv_tx_desc));
2925 wr32(E1000_TDBAL(reg_idx),
2926 tdba & 0x00000000ffffffffULL);
2927 wr32(E1000_TDBAH(reg_idx), tdba >> 32);
2928
Alexander Duyckfce99e32009-10-27 15:51:27 +00002929 ring->tail = hw->hw_addr + E1000_TDT(reg_idx);
Alexander Duycka74420e2011-08-26 07:43:27 +00002930 wr32(E1000_TDH(reg_idx), 0);
Alexander Duyckfce99e32009-10-27 15:51:27 +00002931 writel(0, ring->tail);
Alexander Duyck85b430b2009-10-27 15:50:29 +00002932
2933 txdctl |= IGB_TX_PTHRESH;
2934 txdctl |= IGB_TX_HTHRESH << 8;
2935 txdctl |= IGB_TX_WTHRESH << 16;
2936
2937 txdctl |= E1000_TXDCTL_QUEUE_ENABLE;
2938 wr32(E1000_TXDCTL(reg_idx), txdctl);
2939}
2940
2941/**
2942 * igb_configure_tx - Configure transmit Unit after Reset
2943 * @adapter: board private structure
2944 *
2945 * Configure the Tx unit of the MAC after a reset.
2946 **/
2947static void igb_configure_tx(struct igb_adapter *adapter)
2948{
2949 int i;
2950
2951 for (i = 0; i < adapter->num_tx_queues; i++)
Alexander Duyck3025a442010-02-17 01:02:39 +00002952 igb_configure_tx_ring(adapter, adapter->tx_ring[i]);
Alexander Duyck85b430b2009-10-27 15:50:29 +00002953}
2954
2955/**
Auke Kok9d5c8242008-01-24 02:22:38 -08002956 * igb_setup_rx_resources - allocate Rx resources (Descriptors)
Auke Kok9d5c8242008-01-24 02:22:38 -08002957 * @rx_ring: rx descriptor ring (for a specific queue) to setup
2958 *
2959 * Returns 0 on success, negative on failure
2960 **/
Alexander Duyck80785292009-10-27 15:51:47 +00002961int igb_setup_rx_resources(struct igb_ring *rx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08002962{
Alexander Duyck59d71982010-04-27 13:09:25 +00002963 struct device *dev = rx_ring->dev;
Alexander Duyckf33005a2012-09-13 06:27:55 +00002964 int size;
Auke Kok9d5c8242008-01-24 02:22:38 -08002965
Alexander Duyck06034642011-08-26 07:44:22 +00002966 size = sizeof(struct igb_rx_buffer) * rx_ring->count;
Alexander Duyckf33005a2012-09-13 06:27:55 +00002967
2968 rx_ring->rx_buffer_info = vzalloc(size);
Alexander Duyck06034642011-08-26 07:44:22 +00002969 if (!rx_ring->rx_buffer_info)
Auke Kok9d5c8242008-01-24 02:22:38 -08002970 goto err;
Auke Kok9d5c8242008-01-24 02:22:38 -08002971
Auke Kok9d5c8242008-01-24 02:22:38 -08002972 /* Round up to nearest 4K */
Alexander Duyckf33005a2012-09-13 06:27:55 +00002973 rx_ring->size = rx_ring->count * sizeof(union e1000_adv_rx_desc);
Auke Kok9d5c8242008-01-24 02:22:38 -08002974 rx_ring->size = ALIGN(rx_ring->size, 4096);
2975
Alexander Duyck5536d212012-09-25 00:31:17 +00002976 rx_ring->desc = dma_alloc_coherent(dev, rx_ring->size,
2977 &rx_ring->dma, GFP_KERNEL);
Auke Kok9d5c8242008-01-24 02:22:38 -08002978 if (!rx_ring->desc)
2979 goto err;
2980
Alexander Duyckcbc8e552012-09-25 00:31:02 +00002981 rx_ring->next_to_alloc = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08002982 rx_ring->next_to_clean = 0;
2983 rx_ring->next_to_use = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08002984
Auke Kok9d5c8242008-01-24 02:22:38 -08002985 return 0;
2986
2987err:
Alexander Duyck06034642011-08-26 07:44:22 +00002988 vfree(rx_ring->rx_buffer_info);
2989 rx_ring->rx_buffer_info = NULL;
Alexander Duyckf33005a2012-09-13 06:27:55 +00002990 dev_err(dev, "Unable to allocate memory for the Rx descriptor ring\n");
Auke Kok9d5c8242008-01-24 02:22:38 -08002991 return -ENOMEM;
2992}
2993
2994/**
2995 * igb_setup_all_rx_resources - wrapper to allocate Rx resources
2996 * (Descriptors) for all queues
2997 * @adapter: board private structure
2998 *
2999 * Return 0 on success, negative on failure
3000 **/
3001static int igb_setup_all_rx_resources(struct igb_adapter *adapter)
3002{
Alexander Duyck439705e2009-10-27 23:49:20 +00003003 struct pci_dev *pdev = adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08003004 int i, err = 0;
3005
3006 for (i = 0; i < adapter->num_rx_queues; i++) {
Alexander Duyck3025a442010-02-17 01:02:39 +00003007 err = igb_setup_rx_resources(adapter->rx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08003008 if (err) {
Alexander Duyck439705e2009-10-27 23:49:20 +00003009 dev_err(&pdev->dev,
Auke Kok9d5c8242008-01-24 02:22:38 -08003010 "Allocation for Rx Queue %u failed\n", i);
3011 for (i--; i >= 0; i--)
Alexander Duyck3025a442010-02-17 01:02:39 +00003012 igb_free_rx_resources(adapter->rx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08003013 break;
3014 }
3015 }
3016
3017 return err;
3018}
3019
3020/**
Alexander Duyck06cf2662009-10-27 15:53:25 +00003021 * igb_setup_mrqc - configure the multiple receive queue control registers
3022 * @adapter: Board private structure
3023 **/
3024static void igb_setup_mrqc(struct igb_adapter *adapter)
3025{
3026 struct e1000_hw *hw = &adapter->hw;
3027 u32 mrqc, rxcsum;
Alexander Duyck797fd4b2012-09-13 06:28:11 +00003028 u32 j, num_rx_queues, shift = 0;
Alexander Duycka57fe232012-09-13 06:28:16 +00003029 static const u32 rsskey[10] = { 0xDA565A6D, 0xC20E5B25, 0x3D256741,
3030 0xB08FA343, 0xCB2BCAD0, 0xB4307BAE,
3031 0xA32DCB77, 0x0CF23080, 0x3BB7426A,
3032 0xFA01ACBE };
Alexander Duyck06cf2662009-10-27 15:53:25 +00003033
3034 /* Fill out hash function seeds */
Alexander Duycka57fe232012-09-13 06:28:16 +00003035 for (j = 0; j < 10; j++)
3036 wr32(E1000_RSSRK(j), rsskey[j]);
Alexander Duyck06cf2662009-10-27 15:53:25 +00003037
Alexander Duycka99955f2009-11-12 18:37:19 +00003038 num_rx_queues = adapter->rss_queues;
Alexander Duyck06cf2662009-10-27 15:53:25 +00003039
Alexander Duyck797fd4b2012-09-13 06:28:11 +00003040 switch (hw->mac.type) {
3041 case e1000_82575:
3042 shift = 6;
3043 break;
3044 case e1000_82576:
3045 /* 82576 supports 2 RSS queues for SR-IOV */
3046 if (adapter->vfs_allocated_count) {
Alexander Duyck06cf2662009-10-27 15:53:25 +00003047 shift = 3;
3048 num_rx_queues = 2;
Alexander Duyck06cf2662009-10-27 15:53:25 +00003049 }
Alexander Duyck797fd4b2012-09-13 06:28:11 +00003050 break;
3051 default:
3052 break;
Alexander Duyck06cf2662009-10-27 15:53:25 +00003053 }
3054
Alexander Duyck797fd4b2012-09-13 06:28:11 +00003055 /*
3056 * Populate the indirection table 4 entries at a time. To do this
3057 * we are generating the results for n and n+2 and then interleaving
3058 * those with the results with n+1 and n+3.
3059 */
3060 for (j = 0; j < 32; j++) {
3061 /* first pass generates n and n+2 */
3062 u32 base = ((j * 0x00040004) + 0x00020000) * num_rx_queues;
3063 u32 reta = (base & 0x07800780) >> (7 - shift);
3064
3065 /* second pass generates n+1 and n+3 */
3066 base += 0x00010001 * num_rx_queues;
3067 reta |= (base & 0x07800780) << (1 + shift);
3068
3069 wr32(E1000_RETA(j), reta);
Alexander Duyck06cf2662009-10-27 15:53:25 +00003070 }
3071
3072 /*
3073 * Disable raw packet checksumming so that RSS hash is placed in
3074 * descriptor on writeback. No need to enable TCP/UDP/IP checksum
3075 * offloads as they are enabled by default
3076 */
3077 rxcsum = rd32(E1000_RXCSUM);
3078 rxcsum |= E1000_RXCSUM_PCSD;
3079
3080 if (adapter->hw.mac.type >= e1000_82576)
3081 /* Enable Receive Checksum Offload for SCTP */
3082 rxcsum |= E1000_RXCSUM_CRCOFL;
3083
3084 /* Don't need to set TUOFL or IPOFL, they default to 1 */
3085 wr32(E1000_RXCSUM, rxcsum);
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00003086
Akeem G. Abodunrin039454a2012-11-13 04:03:21 +00003087 /* Generate RSS hash based on packet types, TCP/UDP
3088 * port numbers and/or IPv4/v6 src and dst addresses
3089 */
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00003090 mrqc = E1000_MRQC_RSS_FIELD_IPV4 |
3091 E1000_MRQC_RSS_FIELD_IPV4_TCP |
3092 E1000_MRQC_RSS_FIELD_IPV6 |
3093 E1000_MRQC_RSS_FIELD_IPV6_TCP |
3094 E1000_MRQC_RSS_FIELD_IPV6_TCP_EX;
Alexander Duyck06cf2662009-10-27 15:53:25 +00003095
Akeem G. Abodunrin039454a2012-11-13 04:03:21 +00003096 if (adapter->flags & IGB_FLAG_RSS_FIELD_IPV4_UDP)
3097 mrqc |= E1000_MRQC_RSS_FIELD_IPV4_UDP;
3098 if (adapter->flags & IGB_FLAG_RSS_FIELD_IPV6_UDP)
3099 mrqc |= E1000_MRQC_RSS_FIELD_IPV6_UDP;
3100
Alexander Duyck06cf2662009-10-27 15:53:25 +00003101 /* If VMDq is enabled then we set the appropriate mode for that, else
3102 * we default to RSS so that an RSS hash is calculated per packet even
3103 * if we are only using one queue */
3104 if (adapter->vfs_allocated_count) {
3105 if (hw->mac.type > e1000_82575) {
3106 /* Set the default pool for the PF's first queue */
3107 u32 vtctl = rd32(E1000_VT_CTL);
3108 vtctl &= ~(E1000_VT_CTL_DEFAULT_POOL_MASK |
3109 E1000_VT_CTL_DISABLE_DEF_POOL);
3110 vtctl |= adapter->vfs_allocated_count <<
3111 E1000_VT_CTL_DEFAULT_POOL_SHIFT;
3112 wr32(E1000_VT_CTL, vtctl);
3113 }
Alexander Duycka99955f2009-11-12 18:37:19 +00003114 if (adapter->rss_queues > 1)
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00003115 mrqc |= E1000_MRQC_ENABLE_VMDQ_RSS_2Q;
Alexander Duyck06cf2662009-10-27 15:53:25 +00003116 else
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00003117 mrqc |= E1000_MRQC_ENABLE_VMDQ;
Alexander Duyck06cf2662009-10-27 15:53:25 +00003118 } else {
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00003119 if (hw->mac.type != e1000_i211)
3120 mrqc |= E1000_MRQC_ENABLE_RSS_4Q;
Alexander Duyck06cf2662009-10-27 15:53:25 +00003121 }
3122 igb_vmm_control(adapter);
3123
Alexander Duyck06cf2662009-10-27 15:53:25 +00003124 wr32(E1000_MRQC, mrqc);
3125}
3126
3127/**
Auke Kok9d5c8242008-01-24 02:22:38 -08003128 * igb_setup_rctl - configure the receive control registers
3129 * @adapter: Board private structure
3130 **/
Alexander Duyckd7ee5b32009-10-27 15:54:23 +00003131void igb_setup_rctl(struct igb_adapter *adapter)
Auke Kok9d5c8242008-01-24 02:22:38 -08003132{
3133 struct e1000_hw *hw = &adapter->hw;
3134 u32 rctl;
Auke Kok9d5c8242008-01-24 02:22:38 -08003135
3136 rctl = rd32(E1000_RCTL);
3137
3138 rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
Alexander Duyck69d728b2008-11-25 01:04:03 -08003139 rctl &= ~(E1000_RCTL_LBM_TCVR | E1000_RCTL_LBM_MAC);
Auke Kok9d5c8242008-01-24 02:22:38 -08003140
Alexander Duyck69d728b2008-11-25 01:04:03 -08003141 rctl |= E1000_RCTL_EN | E1000_RCTL_BAM | E1000_RCTL_RDMTS_HALF |
Alexander Duyck28b07592009-02-06 23:20:31 +00003142 (hw->mac.mc_filter_type << E1000_RCTL_MO_SHIFT);
Auke Kok9d5c8242008-01-24 02:22:38 -08003143
Auke Kok87cb7e82008-07-08 15:08:29 -07003144 /*
3145 * enable stripping of CRC. It's unlikely this will break BMC
3146 * redirection as it did with e1000. Newer features require
3147 * that the HW strips the CRC.
Alexander Duyck73cd78f2009-02-12 18:16:59 +00003148 */
Auke Kok87cb7e82008-07-08 15:08:29 -07003149 rctl |= E1000_RCTL_SECRC;
Auke Kok9d5c8242008-01-24 02:22:38 -08003150
Alexander Duyck559e9c42009-10-27 23:52:50 +00003151 /* disable store bad packets and clear size bits. */
Alexander Duyckec54d7d2009-01-31 00:52:57 -08003152 rctl &= ~(E1000_RCTL_SBP | E1000_RCTL_SZ_256);
Auke Kok9d5c8242008-01-24 02:22:38 -08003153
Alexander Duyck6ec43fe2009-10-27 15:50:48 +00003154 /* enable LPE to prevent packets larger than max_frame_size */
3155 rctl |= E1000_RCTL_LPE;
Auke Kok9d5c8242008-01-24 02:22:38 -08003156
Alexander Duyck952f72a2009-10-27 15:51:07 +00003157 /* disable queue 0 to prevent tail write w/o re-config */
3158 wr32(E1000_RXDCTL(0), 0);
Auke Kok9d5c8242008-01-24 02:22:38 -08003159
Alexander Duycke1739522009-02-19 20:39:44 -08003160 /* Attention!!! For SR-IOV PF driver operations you must enable
3161 * queue drop for all VF and PF queues to prevent head of line blocking
3162 * if an un-trusted VF does not provide descriptors to hardware.
3163 */
3164 if (adapter->vfs_allocated_count) {
Alexander Duycke1739522009-02-19 20:39:44 -08003165 /* set all queue drop enable bits */
3166 wr32(E1000_QDE, ALL_QUEUES);
Alexander Duycke1739522009-02-19 20:39:44 -08003167 }
3168
Ben Greear89eaefb2012-03-06 09:41:58 +00003169 /* This is useful for sniffing bad packets. */
3170 if (adapter->netdev->features & NETIF_F_RXALL) {
3171 /* UPE and MPE will be handled by normal PROMISC logic
3172 * in e1000e_set_rx_mode */
3173 rctl |= (E1000_RCTL_SBP | /* Receive bad packets */
3174 E1000_RCTL_BAM | /* RX All Bcast Pkts */
3175 E1000_RCTL_PMCF); /* RX All MAC Ctrl Pkts */
3176
3177 rctl &= ~(E1000_RCTL_VFE | /* Disable VLAN filter */
3178 E1000_RCTL_DPF | /* Allow filtered pause */
3179 E1000_RCTL_CFIEN); /* Dis VLAN CFIEN Filter */
3180 /* Do not mess with E1000_CTRL_VME, it affects transmit as well,
3181 * and that breaks VLANs.
3182 */
3183 }
3184
Auke Kok9d5c8242008-01-24 02:22:38 -08003185 wr32(E1000_RCTL, rctl);
3186}
3187
Alexander Duyck7d5753f2009-10-27 23:47:16 +00003188static inline int igb_set_vf_rlpml(struct igb_adapter *adapter, int size,
3189 int vfn)
3190{
3191 struct e1000_hw *hw = &adapter->hw;
3192 u32 vmolr;
3193
3194 /* if it isn't the PF check to see if VFs are enabled and
3195 * increase the size to support vlan tags */
3196 if (vfn < adapter->vfs_allocated_count &&
3197 adapter->vf_data[vfn].vlans_enabled)
3198 size += VLAN_TAG_SIZE;
3199
3200 vmolr = rd32(E1000_VMOLR(vfn));
3201 vmolr &= ~E1000_VMOLR_RLPML_MASK;
3202 vmolr |= size | E1000_VMOLR_LPE;
3203 wr32(E1000_VMOLR(vfn), vmolr);
3204
3205 return 0;
3206}
3207
Auke Kok9d5c8242008-01-24 02:22:38 -08003208/**
Alexander Duycke1739522009-02-19 20:39:44 -08003209 * igb_rlpml_set - set maximum receive packet size
3210 * @adapter: board private structure
3211 *
3212 * Configure maximum receivable packet size.
3213 **/
3214static void igb_rlpml_set(struct igb_adapter *adapter)
3215{
Alexander Duyck153285f2011-08-26 07:43:32 +00003216 u32 max_frame_size = adapter->max_frame_size;
Alexander Duycke1739522009-02-19 20:39:44 -08003217 struct e1000_hw *hw = &adapter->hw;
3218 u16 pf_id = adapter->vfs_allocated_count;
3219
Alexander Duycke1739522009-02-19 20:39:44 -08003220 if (pf_id) {
3221 igb_set_vf_rlpml(adapter, max_frame_size, pf_id);
Alexander Duyck153285f2011-08-26 07:43:32 +00003222 /*
3223 * If we're in VMDQ or SR-IOV mode, then set global RLPML
3224 * to our max jumbo frame size, in case we need to enable
3225 * jumbo frames on one of the rings later.
3226 * This will not pass over-length frames into the default
3227 * queue because it's gated by the VMOLR.RLPML.
3228 */
Alexander Duyck7d5753f2009-10-27 23:47:16 +00003229 max_frame_size = MAX_JUMBO_FRAME_SIZE;
Alexander Duycke1739522009-02-19 20:39:44 -08003230 }
3231
3232 wr32(E1000_RLPML, max_frame_size);
3233}
3234
Williams, Mitch A8151d292010-02-10 01:44:24 +00003235static inline void igb_set_vmolr(struct igb_adapter *adapter,
3236 int vfn, bool aupe)
Alexander Duyck7d5753f2009-10-27 23:47:16 +00003237{
3238 struct e1000_hw *hw = &adapter->hw;
3239 u32 vmolr;
3240
3241 /*
3242 * This register exists only on 82576 and newer so if we are older then
3243 * we should exit and do nothing
3244 */
3245 if (hw->mac.type < e1000_82576)
3246 return;
3247
3248 vmolr = rd32(E1000_VMOLR(vfn));
Williams, Mitch A8151d292010-02-10 01:44:24 +00003249 vmolr |= E1000_VMOLR_STRVLAN; /* Strip vlan tags */
3250 if (aupe)
3251 vmolr |= E1000_VMOLR_AUPE; /* Accept untagged packets */
3252 else
3253 vmolr &= ~(E1000_VMOLR_AUPE); /* Tagged packets ONLY */
Alexander Duyck7d5753f2009-10-27 23:47:16 +00003254
3255 /* clear all bits that might not be set */
3256 vmolr &= ~(E1000_VMOLR_BAM | E1000_VMOLR_RSSE);
3257
Alexander Duycka99955f2009-11-12 18:37:19 +00003258 if (adapter->rss_queues > 1 && vfn == adapter->vfs_allocated_count)
Alexander Duyck7d5753f2009-10-27 23:47:16 +00003259 vmolr |= E1000_VMOLR_RSSE; /* enable RSS */
3260 /*
3261 * for VMDq only allow the VFs and pool 0 to accept broadcast and
3262 * multicast packets
3263 */
3264 if (vfn <= adapter->vfs_allocated_count)
3265 vmolr |= E1000_VMOLR_BAM; /* Accept broadcast */
3266
3267 wr32(E1000_VMOLR(vfn), vmolr);
3268}
3269
Alexander Duycke1739522009-02-19 20:39:44 -08003270/**
Alexander Duyck85b430b2009-10-27 15:50:29 +00003271 * igb_configure_rx_ring - Configure a receive ring after Reset
3272 * @adapter: board private structure
3273 * @ring: receive ring to be configured
3274 *
3275 * Configure the Rx unit of the MAC after a reset.
3276 **/
Alexander Duyckd7ee5b32009-10-27 15:54:23 +00003277void igb_configure_rx_ring(struct igb_adapter *adapter,
3278 struct igb_ring *ring)
Alexander Duyck85b430b2009-10-27 15:50:29 +00003279{
3280 struct e1000_hw *hw = &adapter->hw;
3281 u64 rdba = ring->dma;
3282 int reg_idx = ring->reg_idx;
Alexander Duycka74420e2011-08-26 07:43:27 +00003283 u32 srrctl = 0, rxdctl = 0;
Alexander Duyck85b430b2009-10-27 15:50:29 +00003284
3285 /* disable the queue */
Alexander Duycka74420e2011-08-26 07:43:27 +00003286 wr32(E1000_RXDCTL(reg_idx), 0);
Alexander Duyck85b430b2009-10-27 15:50:29 +00003287
3288 /* Set DMA base address registers */
3289 wr32(E1000_RDBAL(reg_idx),
3290 rdba & 0x00000000ffffffffULL);
3291 wr32(E1000_RDBAH(reg_idx), rdba >> 32);
3292 wr32(E1000_RDLEN(reg_idx),
3293 ring->count * sizeof(union e1000_adv_rx_desc));
3294
3295 /* initialize head and tail */
Alexander Duyckfce99e32009-10-27 15:51:27 +00003296 ring->tail = hw->hw_addr + E1000_RDT(reg_idx);
Alexander Duycka74420e2011-08-26 07:43:27 +00003297 wr32(E1000_RDH(reg_idx), 0);
Alexander Duyckfce99e32009-10-27 15:51:27 +00003298 writel(0, ring->tail);
Alexander Duyck85b430b2009-10-27 15:50:29 +00003299
Alexander Duyck952f72a2009-10-27 15:51:07 +00003300 /* set descriptor configuration */
Alexander Duyck44390ca2011-08-26 07:43:38 +00003301 srrctl = IGB_RX_HDR_LEN << E1000_SRRCTL_BSIZEHDRSIZE_SHIFT;
Alexander Duyckde78d1f2012-09-25 00:31:12 +00003302 srrctl |= IGB_RX_BUFSZ >> E1000_SRRCTL_BSIZEPKT_SHIFT;
Alexander Duyck1a1c2252012-09-25 00:30:52 +00003303 srrctl |= E1000_SRRCTL_DESCTYPE_ADV_ONEBUF;
Alexander Duyck06218a82011-08-26 07:46:55 +00003304 if (hw->mac.type >= e1000_82580)
Nick Nunley757b77e2010-03-26 11:36:47 +00003305 srrctl |= E1000_SRRCTL_TIMESTAMP;
Nick Nunleye6bdb6f2010-02-17 01:03:38 +00003306 /* Only set Drop Enable if we are supporting multiple queues */
3307 if (adapter->vfs_allocated_count || adapter->num_rx_queues > 1)
3308 srrctl |= E1000_SRRCTL_DROP_EN;
Alexander Duyck952f72a2009-10-27 15:51:07 +00003309
3310 wr32(E1000_SRRCTL(reg_idx), srrctl);
3311
Alexander Duyck7d5753f2009-10-27 23:47:16 +00003312 /* set filtering for VMDQ pools */
Williams, Mitch A8151d292010-02-10 01:44:24 +00003313 igb_set_vmolr(adapter, reg_idx & 0x7, true);
Alexander Duyck7d5753f2009-10-27 23:47:16 +00003314
Alexander Duyck85b430b2009-10-27 15:50:29 +00003315 rxdctl |= IGB_RX_PTHRESH;
3316 rxdctl |= IGB_RX_HTHRESH << 8;
3317 rxdctl |= IGB_RX_WTHRESH << 16;
Alexander Duycka74420e2011-08-26 07:43:27 +00003318
3319 /* enable receive descriptor fetching */
3320 rxdctl |= E1000_RXDCTL_QUEUE_ENABLE;
Alexander Duyck85b430b2009-10-27 15:50:29 +00003321 wr32(E1000_RXDCTL(reg_idx), rxdctl);
3322}
3323
3324/**
Auke Kok9d5c8242008-01-24 02:22:38 -08003325 * igb_configure_rx - Configure receive Unit after Reset
3326 * @adapter: board private structure
3327 *
3328 * Configure the Rx unit of the MAC after a reset.
3329 **/
3330static void igb_configure_rx(struct igb_adapter *adapter)
3331{
Hannes Eder91075842009-02-18 19:36:04 -08003332 int i;
Auke Kok9d5c8242008-01-24 02:22:38 -08003333
Alexander Duyck68d480c2009-10-05 06:33:08 +00003334 /* set UTA to appropriate mode */
3335 igb_set_uta(adapter);
3336
Alexander Duyck26ad9172009-10-05 06:32:49 +00003337 /* set the correct pool for the PF default MAC address in entry 0 */
3338 igb_rar_set_qsel(adapter, adapter->hw.mac.addr, 0,
3339 adapter->vfs_allocated_count);
3340
Alexander Duyck06cf2662009-10-27 15:53:25 +00003341 /* Setup the HW Rx Head and Tail Descriptor Pointers and
3342 * the Base and Length of the Rx Descriptor Ring */
3343 for (i = 0; i < adapter->num_rx_queues; i++)
Alexander Duyck3025a442010-02-17 01:02:39 +00003344 igb_configure_rx_ring(adapter, adapter->rx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08003345}
3346
3347/**
3348 * igb_free_tx_resources - Free Tx Resources per Queue
Auke Kok9d5c8242008-01-24 02:22:38 -08003349 * @tx_ring: Tx descriptor ring for a specific queue
3350 *
3351 * Free all transmit software resources
3352 **/
Alexander Duyck68fd9912008-11-20 00:48:10 -08003353void igb_free_tx_resources(struct igb_ring *tx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08003354{
Mitch Williams3b644cf2008-06-27 10:59:48 -07003355 igb_clean_tx_ring(tx_ring);
Auke Kok9d5c8242008-01-24 02:22:38 -08003356
Alexander Duyck06034642011-08-26 07:44:22 +00003357 vfree(tx_ring->tx_buffer_info);
3358 tx_ring->tx_buffer_info = NULL;
Auke Kok9d5c8242008-01-24 02:22:38 -08003359
Alexander Duyck439705e2009-10-27 23:49:20 +00003360 /* if not set, then don't free */
3361 if (!tx_ring->desc)
3362 return;
3363
Alexander Duyck59d71982010-04-27 13:09:25 +00003364 dma_free_coherent(tx_ring->dev, tx_ring->size,
3365 tx_ring->desc, tx_ring->dma);
Auke Kok9d5c8242008-01-24 02:22:38 -08003366
3367 tx_ring->desc = NULL;
3368}
3369
3370/**
3371 * igb_free_all_tx_resources - Free Tx Resources for All Queues
3372 * @adapter: board private structure
3373 *
3374 * Free all transmit software resources
3375 **/
3376static void igb_free_all_tx_resources(struct igb_adapter *adapter)
3377{
3378 int i;
3379
3380 for (i = 0; i < adapter->num_tx_queues; i++)
Alexander Duyck3025a442010-02-17 01:02:39 +00003381 igb_free_tx_resources(adapter->tx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08003382}
3383
Alexander Duyckebe42d12011-08-26 07:45:09 +00003384void igb_unmap_and_free_tx_resource(struct igb_ring *ring,
3385 struct igb_tx_buffer *tx_buffer)
Auke Kok9d5c8242008-01-24 02:22:38 -08003386{
Alexander Duyckebe42d12011-08-26 07:45:09 +00003387 if (tx_buffer->skb) {
3388 dev_kfree_skb_any(tx_buffer->skb);
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00003389 if (dma_unmap_len(tx_buffer, len))
Alexander Duyckebe42d12011-08-26 07:45:09 +00003390 dma_unmap_single(ring->dev,
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00003391 dma_unmap_addr(tx_buffer, dma),
3392 dma_unmap_len(tx_buffer, len),
Alexander Duyckebe42d12011-08-26 07:45:09 +00003393 DMA_TO_DEVICE);
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00003394 } else if (dma_unmap_len(tx_buffer, len)) {
Alexander Duyckebe42d12011-08-26 07:45:09 +00003395 dma_unmap_page(ring->dev,
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00003396 dma_unmap_addr(tx_buffer, dma),
3397 dma_unmap_len(tx_buffer, len),
Alexander Duyckebe42d12011-08-26 07:45:09 +00003398 DMA_TO_DEVICE);
Alexander Duyck6366ad32009-12-02 16:47:18 +00003399 }
Alexander Duyckebe42d12011-08-26 07:45:09 +00003400 tx_buffer->next_to_watch = NULL;
3401 tx_buffer->skb = NULL;
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00003402 dma_unmap_len_set(tx_buffer, len, 0);
Alexander Duyckebe42d12011-08-26 07:45:09 +00003403 /* buffer_info must be completely set up in the transmit path */
Auke Kok9d5c8242008-01-24 02:22:38 -08003404}
3405
3406/**
3407 * igb_clean_tx_ring - Free Tx Buffers
Auke Kok9d5c8242008-01-24 02:22:38 -08003408 * @tx_ring: ring to be cleaned
3409 **/
Mitch Williams3b644cf2008-06-27 10:59:48 -07003410static void igb_clean_tx_ring(struct igb_ring *tx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08003411{
Alexander Duyck06034642011-08-26 07:44:22 +00003412 struct igb_tx_buffer *buffer_info;
Auke Kok9d5c8242008-01-24 02:22:38 -08003413 unsigned long size;
Alexander Duyck6ad4edf2011-08-26 07:45:26 +00003414 u16 i;
Auke Kok9d5c8242008-01-24 02:22:38 -08003415
Alexander Duyck06034642011-08-26 07:44:22 +00003416 if (!tx_ring->tx_buffer_info)
Auke Kok9d5c8242008-01-24 02:22:38 -08003417 return;
3418 /* Free all the Tx ring sk_buffs */
3419
3420 for (i = 0; i < tx_ring->count; i++) {
Alexander Duyck06034642011-08-26 07:44:22 +00003421 buffer_info = &tx_ring->tx_buffer_info[i];
Alexander Duyck80785292009-10-27 15:51:47 +00003422 igb_unmap_and_free_tx_resource(tx_ring, buffer_info);
Auke Kok9d5c8242008-01-24 02:22:38 -08003423 }
3424
John Fastabenddad8a3b2012-04-23 12:22:39 +00003425 netdev_tx_reset_queue(txring_txq(tx_ring));
3426
Alexander Duyck06034642011-08-26 07:44:22 +00003427 size = sizeof(struct igb_tx_buffer) * tx_ring->count;
3428 memset(tx_ring->tx_buffer_info, 0, size);
Auke Kok9d5c8242008-01-24 02:22:38 -08003429
3430 /* Zero out the descriptor ring */
Auke Kok9d5c8242008-01-24 02:22:38 -08003431 memset(tx_ring->desc, 0, tx_ring->size);
3432
3433 tx_ring->next_to_use = 0;
3434 tx_ring->next_to_clean = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08003435}
3436
3437/**
3438 * igb_clean_all_tx_rings - Free Tx Buffers for all queues
3439 * @adapter: board private structure
3440 **/
3441static void igb_clean_all_tx_rings(struct igb_adapter *adapter)
3442{
3443 int i;
3444
3445 for (i = 0; i < adapter->num_tx_queues; i++)
Alexander Duyck3025a442010-02-17 01:02:39 +00003446 igb_clean_tx_ring(adapter->tx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08003447}
3448
3449/**
3450 * igb_free_rx_resources - Free Rx Resources
Auke Kok9d5c8242008-01-24 02:22:38 -08003451 * @rx_ring: ring to clean the resources from
3452 *
3453 * Free all receive software resources
3454 **/
Alexander Duyck68fd9912008-11-20 00:48:10 -08003455void igb_free_rx_resources(struct igb_ring *rx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08003456{
Mitch Williams3b644cf2008-06-27 10:59:48 -07003457 igb_clean_rx_ring(rx_ring);
Auke Kok9d5c8242008-01-24 02:22:38 -08003458
Alexander Duyck06034642011-08-26 07:44:22 +00003459 vfree(rx_ring->rx_buffer_info);
3460 rx_ring->rx_buffer_info = NULL;
Auke Kok9d5c8242008-01-24 02:22:38 -08003461
Alexander Duyck439705e2009-10-27 23:49:20 +00003462 /* if not set, then don't free */
3463 if (!rx_ring->desc)
3464 return;
3465
Alexander Duyck59d71982010-04-27 13:09:25 +00003466 dma_free_coherent(rx_ring->dev, rx_ring->size,
3467 rx_ring->desc, rx_ring->dma);
Auke Kok9d5c8242008-01-24 02:22:38 -08003468
3469 rx_ring->desc = NULL;
3470}
3471
3472/**
3473 * igb_free_all_rx_resources - Free Rx Resources for All Queues
3474 * @adapter: board private structure
3475 *
3476 * Free all receive software resources
3477 **/
3478static void igb_free_all_rx_resources(struct igb_adapter *adapter)
3479{
3480 int i;
3481
3482 for (i = 0; i < adapter->num_rx_queues; i++)
Alexander Duyck3025a442010-02-17 01:02:39 +00003483 igb_free_rx_resources(adapter->rx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08003484}
3485
3486/**
3487 * igb_clean_rx_ring - Free Rx Buffers per Queue
Auke Kok9d5c8242008-01-24 02:22:38 -08003488 * @rx_ring: ring to free buffers from
3489 **/
Mitch Williams3b644cf2008-06-27 10:59:48 -07003490static void igb_clean_rx_ring(struct igb_ring *rx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08003491{
Auke Kok9d5c8242008-01-24 02:22:38 -08003492 unsigned long size;
Alexander Duyckc023cd82011-08-26 07:43:43 +00003493 u16 i;
Auke Kok9d5c8242008-01-24 02:22:38 -08003494
Alexander Duyck1a1c2252012-09-25 00:30:52 +00003495 if (rx_ring->skb)
3496 dev_kfree_skb(rx_ring->skb);
3497 rx_ring->skb = NULL;
3498
Alexander Duyck06034642011-08-26 07:44:22 +00003499 if (!rx_ring->rx_buffer_info)
Auke Kok9d5c8242008-01-24 02:22:38 -08003500 return;
Alexander Duyck439705e2009-10-27 23:49:20 +00003501
Auke Kok9d5c8242008-01-24 02:22:38 -08003502 /* Free all the Rx ring sk_buffs */
3503 for (i = 0; i < rx_ring->count; i++) {
Alexander Duyck06034642011-08-26 07:44:22 +00003504 struct igb_rx_buffer *buffer_info = &rx_ring->rx_buffer_info[i];
Auke Kok9d5c8242008-01-24 02:22:38 -08003505
Alexander Duyckcbc8e552012-09-25 00:31:02 +00003506 if (!buffer_info->page)
3507 continue;
3508
3509 dma_unmap_page(rx_ring->dev,
3510 buffer_info->dma,
3511 PAGE_SIZE,
3512 DMA_FROM_DEVICE);
3513 __free_page(buffer_info->page);
3514
Alexander Duyck1a1c2252012-09-25 00:30:52 +00003515 buffer_info->page = NULL;
Auke Kok9d5c8242008-01-24 02:22:38 -08003516 }
3517
Alexander Duyck06034642011-08-26 07:44:22 +00003518 size = sizeof(struct igb_rx_buffer) * rx_ring->count;
3519 memset(rx_ring->rx_buffer_info, 0, size);
Auke Kok9d5c8242008-01-24 02:22:38 -08003520
3521 /* Zero out the descriptor ring */
3522 memset(rx_ring->desc, 0, rx_ring->size);
3523
Alexander Duyckcbc8e552012-09-25 00:31:02 +00003524 rx_ring->next_to_alloc = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08003525 rx_ring->next_to_clean = 0;
3526 rx_ring->next_to_use = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08003527}
3528
3529/**
3530 * igb_clean_all_rx_rings - Free Rx Buffers for all queues
3531 * @adapter: board private structure
3532 **/
3533static void igb_clean_all_rx_rings(struct igb_adapter *adapter)
3534{
3535 int i;
3536
3537 for (i = 0; i < adapter->num_rx_queues; i++)
Alexander Duyck3025a442010-02-17 01:02:39 +00003538 igb_clean_rx_ring(adapter->rx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08003539}
3540
3541/**
3542 * igb_set_mac - Change the Ethernet Address of the NIC
3543 * @netdev: network interface device structure
3544 * @p: pointer to an address structure
3545 *
3546 * Returns 0 on success, negative on failure
3547 **/
3548static int igb_set_mac(struct net_device *netdev, void *p)
3549{
3550 struct igb_adapter *adapter = netdev_priv(netdev);
Alexander Duyck28b07592009-02-06 23:20:31 +00003551 struct e1000_hw *hw = &adapter->hw;
Auke Kok9d5c8242008-01-24 02:22:38 -08003552 struct sockaddr *addr = p;
3553
3554 if (!is_valid_ether_addr(addr->sa_data))
3555 return -EADDRNOTAVAIL;
3556
3557 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
Alexander Duyck28b07592009-02-06 23:20:31 +00003558 memcpy(hw->mac.addr, addr->sa_data, netdev->addr_len);
Auke Kok9d5c8242008-01-24 02:22:38 -08003559
Alexander Duyck26ad9172009-10-05 06:32:49 +00003560 /* set the correct pool for the new PF MAC address in entry 0 */
3561 igb_rar_set_qsel(adapter, hw->mac.addr, 0,
3562 adapter->vfs_allocated_count);
Alexander Duycke1739522009-02-19 20:39:44 -08003563
Auke Kok9d5c8242008-01-24 02:22:38 -08003564 return 0;
3565}
3566
3567/**
Alexander Duyck68d480c2009-10-05 06:33:08 +00003568 * igb_write_mc_addr_list - write multicast addresses to MTA
3569 * @netdev: network interface device structure
3570 *
3571 * Writes multicast address list to the MTA hash table.
3572 * Returns: -ENOMEM on failure
3573 * 0 on no addresses written
3574 * X on writing X addresses to MTA
3575 **/
3576static int igb_write_mc_addr_list(struct net_device *netdev)
3577{
3578 struct igb_adapter *adapter = netdev_priv(netdev);
3579 struct e1000_hw *hw = &adapter->hw;
Jiri Pirko22bedad32010-04-01 21:22:57 +00003580 struct netdev_hw_addr *ha;
Alexander Duyck68d480c2009-10-05 06:33:08 +00003581 u8 *mta_list;
Alexander Duyck68d480c2009-10-05 06:33:08 +00003582 int i;
3583
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00003584 if (netdev_mc_empty(netdev)) {
Alexander Duyck68d480c2009-10-05 06:33:08 +00003585 /* nothing to program, so clear mc list */
3586 igb_update_mc_addr_list(hw, NULL, 0);
3587 igb_restore_vf_multicasts(adapter);
3588 return 0;
3589 }
3590
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00003591 mta_list = kzalloc(netdev_mc_count(netdev) * 6, GFP_ATOMIC);
Alexander Duyck68d480c2009-10-05 06:33:08 +00003592 if (!mta_list)
3593 return -ENOMEM;
3594
Alexander Duyck68d480c2009-10-05 06:33:08 +00003595 /* The shared function expects a packed array of only addresses. */
Jiri Pirko48e2f182010-02-22 09:22:26 +00003596 i = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +00003597 netdev_for_each_mc_addr(ha, netdev)
3598 memcpy(mta_list + (i++ * ETH_ALEN), ha->addr, ETH_ALEN);
Alexander Duyck68d480c2009-10-05 06:33:08 +00003599
Alexander Duyck68d480c2009-10-05 06:33:08 +00003600 igb_update_mc_addr_list(hw, mta_list, i);
3601 kfree(mta_list);
3602
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00003603 return netdev_mc_count(netdev);
Alexander Duyck68d480c2009-10-05 06:33:08 +00003604}
3605
3606/**
3607 * igb_write_uc_addr_list - write unicast addresses to RAR table
3608 * @netdev: network interface device structure
3609 *
3610 * Writes unicast address list to the RAR table.
3611 * Returns: -ENOMEM on failure/insufficient address space
3612 * 0 on no addresses written
3613 * X on writing X addresses to the RAR table
3614 **/
3615static int igb_write_uc_addr_list(struct net_device *netdev)
3616{
3617 struct igb_adapter *adapter = netdev_priv(netdev);
3618 struct e1000_hw *hw = &adapter->hw;
3619 unsigned int vfn = adapter->vfs_allocated_count;
3620 unsigned int rar_entries = hw->mac.rar_entry_count - (vfn + 1);
3621 int count = 0;
3622
3623 /* return ENOMEM indicating insufficient memory for addresses */
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08003624 if (netdev_uc_count(netdev) > rar_entries)
Alexander Duyck68d480c2009-10-05 06:33:08 +00003625 return -ENOMEM;
3626
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08003627 if (!netdev_uc_empty(netdev) && rar_entries) {
Alexander Duyck68d480c2009-10-05 06:33:08 +00003628 struct netdev_hw_addr *ha;
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08003629
3630 netdev_for_each_uc_addr(ha, netdev) {
Alexander Duyck68d480c2009-10-05 06:33:08 +00003631 if (!rar_entries)
3632 break;
3633 igb_rar_set_qsel(adapter, ha->addr,
3634 rar_entries--,
3635 vfn);
3636 count++;
3637 }
3638 }
3639 /* write the addresses in reverse order to avoid write combining */
3640 for (; rar_entries > 0 ; rar_entries--) {
3641 wr32(E1000_RAH(rar_entries), 0);
3642 wr32(E1000_RAL(rar_entries), 0);
3643 }
3644 wrfl();
3645
3646 return count;
3647}
3648
3649/**
Alexander Duyckff41f8d2009-09-03 14:48:56 +00003650 * igb_set_rx_mode - Secondary Unicast, Multicast and Promiscuous mode set
Auke Kok9d5c8242008-01-24 02:22:38 -08003651 * @netdev: network interface device structure
3652 *
Alexander Duyckff41f8d2009-09-03 14:48:56 +00003653 * The set_rx_mode entry point is called whenever the unicast or multicast
3654 * address lists or the network interface flags are updated. This routine is
3655 * responsible for configuring the hardware for proper unicast, multicast,
Auke Kok9d5c8242008-01-24 02:22:38 -08003656 * promiscuous mode, and all-multi behavior.
3657 **/
Alexander Duyckff41f8d2009-09-03 14:48:56 +00003658static void igb_set_rx_mode(struct net_device *netdev)
Auke Kok9d5c8242008-01-24 02:22:38 -08003659{
3660 struct igb_adapter *adapter = netdev_priv(netdev);
3661 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck68d480c2009-10-05 06:33:08 +00003662 unsigned int vfn = adapter->vfs_allocated_count;
3663 u32 rctl, vmolr = 0;
3664 int count;
Auke Kok9d5c8242008-01-24 02:22:38 -08003665
3666 /* Check for Promiscuous and All Multicast modes */
Auke Kok9d5c8242008-01-24 02:22:38 -08003667 rctl = rd32(E1000_RCTL);
3668
Alexander Duyck68d480c2009-10-05 06:33:08 +00003669 /* clear the effected bits */
3670 rctl &= ~(E1000_RCTL_UPE | E1000_RCTL_MPE | E1000_RCTL_VFE);
3671
Patrick McHardy746b9f02008-07-16 20:15:45 -07003672 if (netdev->flags & IFF_PROMISC) {
Auke Kok9d5c8242008-01-24 02:22:38 -08003673 rctl |= (E1000_RCTL_UPE | E1000_RCTL_MPE);
Alexander Duyck68d480c2009-10-05 06:33:08 +00003674 vmolr |= (E1000_VMOLR_ROPE | E1000_VMOLR_MPME);
Patrick McHardy746b9f02008-07-16 20:15:45 -07003675 } else {
Alexander Duyck68d480c2009-10-05 06:33:08 +00003676 if (netdev->flags & IFF_ALLMULTI) {
Patrick McHardy746b9f02008-07-16 20:15:45 -07003677 rctl |= E1000_RCTL_MPE;
Alexander Duyck68d480c2009-10-05 06:33:08 +00003678 vmolr |= E1000_VMOLR_MPME;
3679 } else {
3680 /*
3681 * Write addresses to the MTA, if the attempt fails
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003682 * then we should just turn on promiscuous mode so
Alexander Duyck68d480c2009-10-05 06:33:08 +00003683 * that we can at least receive multicast traffic
3684 */
3685 count = igb_write_mc_addr_list(netdev);
3686 if (count < 0) {
3687 rctl |= E1000_RCTL_MPE;
3688 vmolr |= E1000_VMOLR_MPME;
3689 } else if (count) {
3690 vmolr |= E1000_VMOLR_ROMPE;
3691 }
3692 }
3693 /*
3694 * Write addresses to available RAR registers, if there is not
3695 * sufficient space to store all the addresses then enable
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003696 * unicast promiscuous mode
Alexander Duyck68d480c2009-10-05 06:33:08 +00003697 */
3698 count = igb_write_uc_addr_list(netdev);
3699 if (count < 0) {
Alexander Duyckff41f8d2009-09-03 14:48:56 +00003700 rctl |= E1000_RCTL_UPE;
Alexander Duyck68d480c2009-10-05 06:33:08 +00003701 vmolr |= E1000_VMOLR_ROPE;
3702 }
Patrick McHardy78ed11a2008-07-16 20:16:14 -07003703 rctl |= E1000_RCTL_VFE;
Patrick McHardy746b9f02008-07-16 20:15:45 -07003704 }
Auke Kok9d5c8242008-01-24 02:22:38 -08003705 wr32(E1000_RCTL, rctl);
3706
Alexander Duyck68d480c2009-10-05 06:33:08 +00003707 /*
3708 * In order to support SR-IOV and eventually VMDq it is necessary to set
3709 * the VMOLR to enable the appropriate modes. Without this workaround
3710 * we will have issues with VLAN tag stripping not being done for frames
3711 * that are only arriving because we are the default pool
3712 */
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00003713 if ((hw->mac.type < e1000_82576) || (hw->mac.type > e1000_i350))
Alexander Duyck28fc06f2009-07-23 18:08:54 +00003714 return;
Alexander Duyck28fc06f2009-07-23 18:08:54 +00003715
Alexander Duyck68d480c2009-10-05 06:33:08 +00003716 vmolr |= rd32(E1000_VMOLR(vfn)) &
3717 ~(E1000_VMOLR_ROPE | E1000_VMOLR_MPME | E1000_VMOLR_ROMPE);
3718 wr32(E1000_VMOLR(vfn), vmolr);
Alexander Duyck28fc06f2009-07-23 18:08:54 +00003719 igb_restore_vf_multicasts(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08003720}
3721
Greg Rose13800462010-11-06 02:08:26 +00003722static void igb_check_wvbr(struct igb_adapter *adapter)
3723{
3724 struct e1000_hw *hw = &adapter->hw;
3725 u32 wvbr = 0;
3726
3727 switch (hw->mac.type) {
3728 case e1000_82576:
3729 case e1000_i350:
3730 if (!(wvbr = rd32(E1000_WVBR)))
3731 return;
3732 break;
3733 default:
3734 break;
3735 }
3736
3737 adapter->wvbr |= wvbr;
3738}
3739
3740#define IGB_STAGGERED_QUEUE_OFFSET 8
3741
3742static void igb_spoof_check(struct igb_adapter *adapter)
3743{
3744 int j;
3745
3746 if (!adapter->wvbr)
3747 return;
3748
3749 for(j = 0; j < adapter->vfs_allocated_count; j++) {
3750 if (adapter->wvbr & (1 << j) ||
3751 adapter->wvbr & (1 << (j + IGB_STAGGERED_QUEUE_OFFSET))) {
3752 dev_warn(&adapter->pdev->dev,
3753 "Spoof event(s) detected on VF %d\n", j);
3754 adapter->wvbr &=
3755 ~((1 << j) |
3756 (1 << (j + IGB_STAGGERED_QUEUE_OFFSET)));
3757 }
3758 }
3759}
3760
Auke Kok9d5c8242008-01-24 02:22:38 -08003761/* Need to wait a few seconds after link up to get diagnostic information from
3762 * the phy */
3763static void igb_update_phy_info(unsigned long data)
3764{
3765 struct igb_adapter *adapter = (struct igb_adapter *) data;
Alexander Duyckf5f4cf02008-11-21 21:30:24 -08003766 igb_get_phy_info(&adapter->hw);
Auke Kok9d5c8242008-01-24 02:22:38 -08003767}
3768
3769/**
Alexander Duyck4d6b7252009-02-06 23:16:24 +00003770 * igb_has_link - check shared code for link and determine up/down
3771 * @adapter: pointer to driver private info
3772 **/
Nick Nunley31455352010-02-17 01:01:21 +00003773bool igb_has_link(struct igb_adapter *adapter)
Alexander Duyck4d6b7252009-02-06 23:16:24 +00003774{
3775 struct e1000_hw *hw = &adapter->hw;
3776 bool link_active = false;
3777 s32 ret_val = 0;
3778
3779 /* get_link_status is set on LSC (link status) interrupt or
3780 * rx sequence error interrupt. get_link_status will stay
3781 * false until the e1000_check_for_link establishes link
3782 * for copper adapters ONLY
3783 */
3784 switch (hw->phy.media_type) {
3785 case e1000_media_type_copper:
3786 if (hw->mac.get_link_status) {
3787 ret_val = hw->mac.ops.check_for_link(hw);
3788 link_active = !hw->mac.get_link_status;
3789 } else {
3790 link_active = true;
3791 }
3792 break;
Alexander Duyck4d6b7252009-02-06 23:16:24 +00003793 case e1000_media_type_internal_serdes:
3794 ret_val = hw->mac.ops.check_for_link(hw);
3795 link_active = hw->mac.serdes_has_link;
3796 break;
3797 default:
3798 case e1000_media_type_unknown:
3799 break;
3800 }
3801
3802 return link_active;
3803}
3804
Stefan Assmann563988d2011-04-05 04:27:15 +00003805static bool igb_thermal_sensor_event(struct e1000_hw *hw, u32 event)
3806{
3807 bool ret = false;
3808 u32 ctrl_ext, thstat;
3809
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00003810 /* check for thermal sensor event on i350 copper only */
Stefan Assmann563988d2011-04-05 04:27:15 +00003811 if (hw->mac.type == e1000_i350) {
3812 thstat = rd32(E1000_THSTAT);
3813 ctrl_ext = rd32(E1000_CTRL_EXT);
3814
3815 if ((hw->phy.media_type == e1000_media_type_copper) &&
3816 !(ctrl_ext & E1000_CTRL_EXT_LINK_MODE_SGMII)) {
3817 ret = !!(thstat & event);
3818 }
3819 }
3820
3821 return ret;
3822}
3823
Alexander Duyck4d6b7252009-02-06 23:16:24 +00003824/**
Auke Kok9d5c8242008-01-24 02:22:38 -08003825 * igb_watchdog - Timer Call-back
3826 * @data: pointer to adapter cast into an unsigned long
3827 **/
3828static void igb_watchdog(unsigned long data)
3829{
3830 struct igb_adapter *adapter = (struct igb_adapter *)data;
3831 /* Do the rest outside of interrupt context */
3832 schedule_work(&adapter->watchdog_task);
3833}
3834
3835static void igb_watchdog_task(struct work_struct *work)
3836{
3837 struct igb_adapter *adapter = container_of(work,
Alexander Duyck559e9c42009-10-27 23:52:50 +00003838 struct igb_adapter,
3839 watchdog_task);
Auke Kok9d5c8242008-01-24 02:22:38 -08003840 struct e1000_hw *hw = &adapter->hw;
Auke Kok9d5c8242008-01-24 02:22:38 -08003841 struct net_device *netdev = adapter->netdev;
Stefan Assmann563988d2011-04-05 04:27:15 +00003842 u32 link;
Alexander Duyck7a6ea552008-08-26 04:25:03 -07003843 int i;
Auke Kok9d5c8242008-01-24 02:22:38 -08003844
Alexander Duyck4d6b7252009-02-06 23:16:24 +00003845 link = igb_has_link(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08003846 if (link) {
Yan, Zheng749ab2c2012-01-04 20:23:37 +00003847 /* Cancel scheduled suspend requests. */
3848 pm_runtime_resume(netdev->dev.parent);
3849
Auke Kok9d5c8242008-01-24 02:22:38 -08003850 if (!netif_carrier_ok(netdev)) {
3851 u32 ctrl;
Alexander Duyck330a6d62009-10-27 23:51:35 +00003852 hw->mac.ops.get_speed_and_duplex(hw,
3853 &adapter->link_speed,
3854 &adapter->link_duplex);
Auke Kok9d5c8242008-01-24 02:22:38 -08003855
3856 ctrl = rd32(E1000_CTRL);
Alexander Duyck527d47c2008-11-27 00:21:39 -08003857 /* Links status message must follow this format */
Jeff Kirsher876d2d62011-10-21 20:01:34 +00003858 printk(KERN_INFO "igb: %s NIC Link is Up %d Mbps %s "
3859 "Duplex, Flow Control: %s\n",
Alexander Duyck559e9c42009-10-27 23:52:50 +00003860 netdev->name,
3861 adapter->link_speed,
3862 adapter->link_duplex == FULL_DUPLEX ?
Jeff Kirsher876d2d62011-10-21 20:01:34 +00003863 "Full" : "Half",
3864 (ctrl & E1000_CTRL_TFCE) &&
3865 (ctrl & E1000_CTRL_RFCE) ? "RX/TX" :
3866 (ctrl & E1000_CTRL_RFCE) ? "RX" :
3867 (ctrl & E1000_CTRL_TFCE) ? "TX" : "None");
Auke Kok9d5c8242008-01-24 02:22:38 -08003868
Stefan Assmann563988d2011-04-05 04:27:15 +00003869 /* check for thermal sensor event */
Jeff Kirsher876d2d62011-10-21 20:01:34 +00003870 if (igb_thermal_sensor_event(hw,
3871 E1000_THSTAT_LINK_THROTTLE)) {
3872 netdev_info(netdev, "The network adapter link "
3873 "speed was downshifted because it "
3874 "overheated\n");
Carolyn Wyborny7ef5ed12011-03-12 08:59:47 +00003875 }
Stefan Assmann563988d2011-04-05 04:27:15 +00003876
Emil Tantilovd07f3e32010-03-23 18:34:57 +00003877 /* adjust timeout factor according to speed/duplex */
Auke Kok9d5c8242008-01-24 02:22:38 -08003878 adapter->tx_timeout_factor = 1;
3879 switch (adapter->link_speed) {
3880 case SPEED_10:
Auke Kok9d5c8242008-01-24 02:22:38 -08003881 adapter->tx_timeout_factor = 14;
3882 break;
3883 case SPEED_100:
Auke Kok9d5c8242008-01-24 02:22:38 -08003884 /* maybe add some timeout factor ? */
3885 break;
3886 }
3887
3888 netif_carrier_on(netdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08003889
Alexander Duyck4ae196d2009-02-19 20:40:07 -08003890 igb_ping_all_vfs(adapter);
Lior Levy17dc5662011-02-08 02:28:46 +00003891 igb_check_vf_rate_limit(adapter);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08003892
Alexander Duyck4b1a9872009-02-06 23:19:50 +00003893 /* link state has changed, schedule phy info update */
Auke Kok9d5c8242008-01-24 02:22:38 -08003894 if (!test_bit(__IGB_DOWN, &adapter->state))
3895 mod_timer(&adapter->phy_info_timer,
3896 round_jiffies(jiffies + 2 * HZ));
3897 }
3898 } else {
3899 if (netif_carrier_ok(netdev)) {
3900 adapter->link_speed = 0;
3901 adapter->link_duplex = 0;
Stefan Assmann563988d2011-04-05 04:27:15 +00003902
3903 /* check for thermal sensor event */
Jeff Kirsher876d2d62011-10-21 20:01:34 +00003904 if (igb_thermal_sensor_event(hw,
3905 E1000_THSTAT_PWR_DOWN)) {
3906 netdev_err(netdev, "The network adapter was "
3907 "stopped because it overheated\n");
Carolyn Wyborny7ef5ed12011-03-12 08:59:47 +00003908 }
Stefan Assmann563988d2011-04-05 04:27:15 +00003909
Alexander Duyck527d47c2008-11-27 00:21:39 -08003910 /* Links status message must follow this format */
3911 printk(KERN_INFO "igb: %s NIC Link is Down\n",
3912 netdev->name);
Auke Kok9d5c8242008-01-24 02:22:38 -08003913 netif_carrier_off(netdev);
Alexander Duyck4b1a9872009-02-06 23:19:50 +00003914
Alexander Duyck4ae196d2009-02-19 20:40:07 -08003915 igb_ping_all_vfs(adapter);
3916
Alexander Duyck4b1a9872009-02-06 23:19:50 +00003917 /* link state has changed, schedule phy info update */
Auke Kok9d5c8242008-01-24 02:22:38 -08003918 if (!test_bit(__IGB_DOWN, &adapter->state))
3919 mod_timer(&adapter->phy_info_timer,
3920 round_jiffies(jiffies + 2 * HZ));
Yan, Zheng749ab2c2012-01-04 20:23:37 +00003921
3922 pm_schedule_suspend(netdev->dev.parent,
3923 MSEC_PER_SEC * 5);
Auke Kok9d5c8242008-01-24 02:22:38 -08003924 }
3925 }
3926
Eric Dumazet12dcd862010-10-15 17:27:10 +00003927 spin_lock(&adapter->stats64_lock);
3928 igb_update_stats(adapter, &adapter->stats64);
3929 spin_unlock(&adapter->stats64_lock);
Auke Kok9d5c8242008-01-24 02:22:38 -08003930
Alexander Duyckdbabb062009-11-12 18:38:16 +00003931 for (i = 0; i < adapter->num_tx_queues; i++) {
Alexander Duyck3025a442010-02-17 01:02:39 +00003932 struct igb_ring *tx_ring = adapter->tx_ring[i];
Alexander Duyckdbabb062009-11-12 18:38:16 +00003933 if (!netif_carrier_ok(netdev)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08003934 /* We've lost link, so the controller stops DMA,
3935 * but we've got queued Tx work that's never going
3936 * to get done, so reset controller to flush Tx.
3937 * (Do the reset outside of interrupt context). */
Alexander Duyckdbabb062009-11-12 18:38:16 +00003938 if (igb_desc_unused(tx_ring) + 1 < tx_ring->count) {
3939 adapter->tx_timeout_count++;
3940 schedule_work(&adapter->reset_task);
3941 /* return immediately since reset is imminent */
3942 return;
3943 }
Auke Kok9d5c8242008-01-24 02:22:38 -08003944 }
Auke Kok9d5c8242008-01-24 02:22:38 -08003945
Alexander Duyckdbabb062009-11-12 18:38:16 +00003946 /* Force detection of hung controller every watchdog period */
Alexander Duyck6d095fa2011-08-26 07:46:19 +00003947 set_bit(IGB_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags);
Alexander Duyckdbabb062009-11-12 18:38:16 +00003948 }
Alexander Duyckf7ba2052009-10-27 23:48:51 +00003949
Auke Kok9d5c8242008-01-24 02:22:38 -08003950 /* Cause software interrupt to ensure rx ring is cleaned */
Alexander Duyck7a6ea552008-08-26 04:25:03 -07003951 if (adapter->msix_entries) {
Alexander Duyck047e0032009-10-27 15:49:27 +00003952 u32 eics = 0;
Alexander Duyck0d1ae7f2011-08-26 07:46:34 +00003953 for (i = 0; i < adapter->num_q_vectors; i++)
3954 eics |= adapter->q_vector[i]->eims_value;
Alexander Duyck7a6ea552008-08-26 04:25:03 -07003955 wr32(E1000_EICS, eics);
3956 } else {
3957 wr32(E1000_ICS, E1000_ICS_RXDMT0);
3958 }
Auke Kok9d5c8242008-01-24 02:22:38 -08003959
Greg Rose13800462010-11-06 02:08:26 +00003960 igb_spoof_check(adapter);
3961
Auke Kok9d5c8242008-01-24 02:22:38 -08003962 /* Reset the timer */
3963 if (!test_bit(__IGB_DOWN, &adapter->state))
3964 mod_timer(&adapter->watchdog_timer,
3965 round_jiffies(jiffies + 2 * HZ));
3966}
3967
3968enum latency_range {
3969 lowest_latency = 0,
3970 low_latency = 1,
3971 bulk_latency = 2,
3972 latency_invalid = 255
3973};
3974
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003975/**
3976 * igb_update_ring_itr - update the dynamic ITR value based on packet size
3977 *
3978 * Stores a new ITR value based on strictly on packet size. This
3979 * algorithm is less sophisticated than that used in igb_update_itr,
3980 * due to the difficulty of synchronizing statistics across multiple
Stefan Weileef35c22010-08-06 21:11:15 +02003981 * receive rings. The divisors and thresholds used by this function
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003982 * were determined based on theoretical maximum wire speed and testing
3983 * data, in order to minimize response time while increasing bulk
3984 * throughput.
3985 * This functionality is controlled by the InterruptThrottleRate module
3986 * parameter (see igb_param.c)
3987 * NOTE: This function is called only when operating in a multiqueue
3988 * receive environment.
Alexander Duyck047e0032009-10-27 15:49:27 +00003989 * @q_vector: pointer to q_vector
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003990 **/
Alexander Duyck047e0032009-10-27 15:49:27 +00003991static void igb_update_ring_itr(struct igb_q_vector *q_vector)
Auke Kok9d5c8242008-01-24 02:22:38 -08003992{
Alexander Duyck047e0032009-10-27 15:49:27 +00003993 int new_val = q_vector->itr_val;
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003994 int avg_wire_size = 0;
Alexander Duyck047e0032009-10-27 15:49:27 +00003995 struct igb_adapter *adapter = q_vector->adapter;
Eric Dumazet12dcd862010-10-15 17:27:10 +00003996 unsigned int packets;
Auke Kok9d5c8242008-01-24 02:22:38 -08003997
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003998 /* For non-gigabit speeds, just fix the interrupt rate at 4000
3999 * ints/sec - ITR timer value of 120 ticks.
4000 */
4001 if (adapter->link_speed != SPEED_1000) {
Alexander Duyck0ba82992011-08-26 07:45:47 +00004002 new_val = IGB_4K_ITR;
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07004003 goto set_itr_val;
4004 }
Alexander Duyck047e0032009-10-27 15:49:27 +00004005
Alexander Duyck0ba82992011-08-26 07:45:47 +00004006 packets = q_vector->rx.total_packets;
4007 if (packets)
4008 avg_wire_size = q_vector->rx.total_bytes / packets;
Eric Dumazet12dcd862010-10-15 17:27:10 +00004009
Alexander Duyck0ba82992011-08-26 07:45:47 +00004010 packets = q_vector->tx.total_packets;
4011 if (packets)
4012 avg_wire_size = max_t(u32, avg_wire_size,
4013 q_vector->tx.total_bytes / packets);
Alexander Duyck047e0032009-10-27 15:49:27 +00004014
4015 /* if avg_wire_size isn't set no work was done */
4016 if (!avg_wire_size)
4017 goto clear_counts;
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07004018
4019 /* Add 24 bytes to size to account for CRC, preamble, and gap */
4020 avg_wire_size += 24;
4021
4022 /* Don't starve jumbo frames */
4023 avg_wire_size = min(avg_wire_size, 3000);
4024
4025 /* Give a little boost to mid-size frames */
4026 if ((avg_wire_size > 300) && (avg_wire_size < 1200))
4027 new_val = avg_wire_size / 3;
4028 else
4029 new_val = avg_wire_size / 2;
4030
Alexander Duyck0ba82992011-08-26 07:45:47 +00004031 /* conservative mode (itr 3) eliminates the lowest_latency setting */
4032 if (new_val < IGB_20K_ITR &&
4033 ((q_vector->rx.ring && adapter->rx_itr_setting == 3) ||
4034 (!q_vector->rx.ring && adapter->tx_itr_setting == 3)))
4035 new_val = IGB_20K_ITR;
Nick Nunleyabe1c362010-02-17 01:03:19 +00004036
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07004037set_itr_val:
Alexander Duyck047e0032009-10-27 15:49:27 +00004038 if (new_val != q_vector->itr_val) {
4039 q_vector->itr_val = new_val;
4040 q_vector->set_itr = 1;
Auke Kok9d5c8242008-01-24 02:22:38 -08004041 }
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07004042clear_counts:
Alexander Duyck0ba82992011-08-26 07:45:47 +00004043 q_vector->rx.total_bytes = 0;
4044 q_vector->rx.total_packets = 0;
4045 q_vector->tx.total_bytes = 0;
4046 q_vector->tx.total_packets = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08004047}
4048
4049/**
4050 * igb_update_itr - update the dynamic ITR value based on statistics
4051 * Stores a new ITR value based on packets and byte
4052 * counts during the last interrupt. The advantage of per interrupt
4053 * computation is faster updates and more accurate ITR for the current
4054 * traffic pattern. Constants in this function were computed
4055 * based on theoretical maximum wire speed and thresholds were set based
4056 * on testing data as well as attempting to minimize response time
4057 * while increasing bulk throughput.
4058 * this functionality is controlled by the InterruptThrottleRate module
4059 * parameter (see igb_param.c)
4060 * NOTE: These calculations are only valid when operating in a single-
4061 * queue environment.
Alexander Duyck0ba82992011-08-26 07:45:47 +00004062 * @q_vector: pointer to q_vector
4063 * @ring_container: ring info to update the itr for
Auke Kok9d5c8242008-01-24 02:22:38 -08004064 **/
Alexander Duyck0ba82992011-08-26 07:45:47 +00004065static void igb_update_itr(struct igb_q_vector *q_vector,
4066 struct igb_ring_container *ring_container)
Auke Kok9d5c8242008-01-24 02:22:38 -08004067{
Alexander Duyck0ba82992011-08-26 07:45:47 +00004068 unsigned int packets = ring_container->total_packets;
4069 unsigned int bytes = ring_container->total_bytes;
4070 u8 itrval = ring_container->itr;
Auke Kok9d5c8242008-01-24 02:22:38 -08004071
Alexander Duyck0ba82992011-08-26 07:45:47 +00004072 /* no packets, exit with status unchanged */
Auke Kok9d5c8242008-01-24 02:22:38 -08004073 if (packets == 0)
Alexander Duyck0ba82992011-08-26 07:45:47 +00004074 return;
Auke Kok9d5c8242008-01-24 02:22:38 -08004075
Alexander Duyck0ba82992011-08-26 07:45:47 +00004076 switch (itrval) {
Auke Kok9d5c8242008-01-24 02:22:38 -08004077 case lowest_latency:
4078 /* handle TSO and jumbo frames */
4079 if (bytes/packets > 8000)
Alexander Duyck0ba82992011-08-26 07:45:47 +00004080 itrval = bulk_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08004081 else if ((packets < 5) && (bytes > 512))
Alexander Duyck0ba82992011-08-26 07:45:47 +00004082 itrval = low_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08004083 break;
4084 case low_latency: /* 50 usec aka 20000 ints/s */
4085 if (bytes > 10000) {
4086 /* this if handles the TSO accounting */
4087 if (bytes/packets > 8000) {
Alexander Duyck0ba82992011-08-26 07:45:47 +00004088 itrval = bulk_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08004089 } else if ((packets < 10) || ((bytes/packets) > 1200)) {
Alexander Duyck0ba82992011-08-26 07:45:47 +00004090 itrval = bulk_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08004091 } else if ((packets > 35)) {
Alexander Duyck0ba82992011-08-26 07:45:47 +00004092 itrval = lowest_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08004093 }
4094 } else if (bytes/packets > 2000) {
Alexander Duyck0ba82992011-08-26 07:45:47 +00004095 itrval = bulk_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08004096 } else if (packets <= 2 && bytes < 512) {
Alexander Duyck0ba82992011-08-26 07:45:47 +00004097 itrval = lowest_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08004098 }
4099 break;
4100 case bulk_latency: /* 250 usec aka 4000 ints/s */
4101 if (bytes > 25000) {
4102 if (packets > 35)
Alexander Duyck0ba82992011-08-26 07:45:47 +00004103 itrval = low_latency;
Alexander Duyck1e5c3d22009-02-12 18:17:21 +00004104 } else if (bytes < 1500) {
Alexander Duyck0ba82992011-08-26 07:45:47 +00004105 itrval = low_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08004106 }
4107 break;
4108 }
4109
Alexander Duyck0ba82992011-08-26 07:45:47 +00004110 /* clear work counters since we have the values we need */
4111 ring_container->total_bytes = 0;
4112 ring_container->total_packets = 0;
4113
4114 /* write updated itr to ring container */
4115 ring_container->itr = itrval;
Auke Kok9d5c8242008-01-24 02:22:38 -08004116}
4117
Alexander Duyck0ba82992011-08-26 07:45:47 +00004118static void igb_set_itr(struct igb_q_vector *q_vector)
Auke Kok9d5c8242008-01-24 02:22:38 -08004119{
Alexander Duyck0ba82992011-08-26 07:45:47 +00004120 struct igb_adapter *adapter = q_vector->adapter;
Alexander Duyck047e0032009-10-27 15:49:27 +00004121 u32 new_itr = q_vector->itr_val;
Alexander Duyck0ba82992011-08-26 07:45:47 +00004122 u8 current_itr = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08004123
4124 /* for non-gigabit speeds, just fix the interrupt rate at 4000 */
4125 if (adapter->link_speed != SPEED_1000) {
4126 current_itr = 0;
Alexander Duyck0ba82992011-08-26 07:45:47 +00004127 new_itr = IGB_4K_ITR;
Auke Kok9d5c8242008-01-24 02:22:38 -08004128 goto set_itr_now;
4129 }
4130
Alexander Duyck0ba82992011-08-26 07:45:47 +00004131 igb_update_itr(q_vector, &q_vector->tx);
4132 igb_update_itr(q_vector, &q_vector->rx);
Auke Kok9d5c8242008-01-24 02:22:38 -08004133
Alexander Duyck0ba82992011-08-26 07:45:47 +00004134 current_itr = max(q_vector->rx.itr, q_vector->tx.itr);
Auke Kok9d5c8242008-01-24 02:22:38 -08004135
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07004136 /* conservative mode (itr 3) eliminates the lowest_latency setting */
Alexander Duyck0ba82992011-08-26 07:45:47 +00004137 if (current_itr == lowest_latency &&
4138 ((q_vector->rx.ring && adapter->rx_itr_setting == 3) ||
4139 (!q_vector->rx.ring && adapter->tx_itr_setting == 3)))
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07004140 current_itr = low_latency;
4141
Auke Kok9d5c8242008-01-24 02:22:38 -08004142 switch (current_itr) {
4143 /* counts and packets in update_itr are dependent on these numbers */
4144 case lowest_latency:
Alexander Duyck0ba82992011-08-26 07:45:47 +00004145 new_itr = IGB_70K_ITR; /* 70,000 ints/sec */
Auke Kok9d5c8242008-01-24 02:22:38 -08004146 break;
4147 case low_latency:
Alexander Duyck0ba82992011-08-26 07:45:47 +00004148 new_itr = IGB_20K_ITR; /* 20,000 ints/sec */
Auke Kok9d5c8242008-01-24 02:22:38 -08004149 break;
4150 case bulk_latency:
Alexander Duyck0ba82992011-08-26 07:45:47 +00004151 new_itr = IGB_4K_ITR; /* 4,000 ints/sec */
Auke Kok9d5c8242008-01-24 02:22:38 -08004152 break;
4153 default:
4154 break;
4155 }
4156
4157set_itr_now:
Alexander Duyck047e0032009-10-27 15:49:27 +00004158 if (new_itr != q_vector->itr_val) {
Auke Kok9d5c8242008-01-24 02:22:38 -08004159 /* this attempts to bias the interrupt rate towards Bulk
4160 * by adding intermediate steps when interrupt rate is
4161 * increasing */
Alexander Duyck047e0032009-10-27 15:49:27 +00004162 new_itr = new_itr > q_vector->itr_val ?
4163 max((new_itr * q_vector->itr_val) /
4164 (new_itr + (q_vector->itr_val >> 2)),
Alexander Duyck0ba82992011-08-26 07:45:47 +00004165 new_itr) :
Auke Kok9d5c8242008-01-24 02:22:38 -08004166 new_itr;
4167 /* Don't write the value here; it resets the adapter's
4168 * internal timer, and causes us to delay far longer than
4169 * we should between interrupts. Instead, we write the ITR
4170 * value at the beginning of the next interrupt so the timing
4171 * ends up being correct.
4172 */
Alexander Duyck047e0032009-10-27 15:49:27 +00004173 q_vector->itr_val = new_itr;
4174 q_vector->set_itr = 1;
Auke Kok9d5c8242008-01-24 02:22:38 -08004175 }
Auke Kok9d5c8242008-01-24 02:22:38 -08004176}
4177
Stephen Hemmingerc50b52a2012-01-18 22:13:26 +00004178static void igb_tx_ctxtdesc(struct igb_ring *tx_ring, u32 vlan_macip_lens,
4179 u32 type_tucmd, u32 mss_l4len_idx)
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004180{
4181 struct e1000_adv_tx_context_desc *context_desc;
4182 u16 i = tx_ring->next_to_use;
4183
4184 context_desc = IGB_TX_CTXTDESC(tx_ring, i);
4185
4186 i++;
4187 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
4188
4189 /* set bits to identify this as an advanced context descriptor */
4190 type_tucmd |= E1000_TXD_CMD_DEXT | E1000_ADVTXD_DTYP_CTXT;
4191
4192 /* For 82575, context index must be unique per ring. */
Alexander Duyck866cff02011-08-26 07:45:36 +00004193 if (test_bit(IGB_RING_FLAG_TX_CTX_IDX, &tx_ring->flags))
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004194 mss_l4len_idx |= tx_ring->reg_idx << 4;
4195
4196 context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens);
4197 context_desc->seqnum_seed = 0;
4198 context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd);
4199 context_desc->mss_l4len_idx = cpu_to_le32(mss_l4len_idx);
4200}
4201
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004202static int igb_tso(struct igb_ring *tx_ring,
4203 struct igb_tx_buffer *first,
4204 u8 *hdr_len)
Auke Kok9d5c8242008-01-24 02:22:38 -08004205{
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004206 struct sk_buff *skb = first->skb;
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004207 u32 vlan_macip_lens, type_tucmd;
4208 u32 mss_l4len_idx, l4len;
4209
Alexander Duycked6aa102012-11-13 04:03:22 +00004210 if (skb->ip_summed != CHECKSUM_PARTIAL)
4211 return 0;
4212
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004213 if (!skb_is_gso(skb))
4214 return 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08004215
4216 if (skb_header_cloned(skb)) {
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004217 int err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
Auke Kok9d5c8242008-01-24 02:22:38 -08004218 if (err)
4219 return err;
4220 }
4221
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004222 /* ADV DTYP TUCMD MKRLOC/ISCSIHEDLEN */
4223 type_tucmd = E1000_ADVTXD_TUCMD_L4T_TCP;
Auke Kok9d5c8242008-01-24 02:22:38 -08004224
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004225 if (first->protocol == __constant_htons(ETH_P_IP)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08004226 struct iphdr *iph = ip_hdr(skb);
4227 iph->tot_len = 0;
4228 iph->check = 0;
4229 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
4230 iph->daddr, 0,
4231 IPPROTO_TCP,
4232 0);
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004233 type_tucmd |= E1000_ADVTXD_TUCMD_IPV4;
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004234 first->tx_flags |= IGB_TX_FLAGS_TSO |
4235 IGB_TX_FLAGS_CSUM |
4236 IGB_TX_FLAGS_IPV4;
Sridhar Samudrala8e1e8a42010-01-23 02:02:21 -08004237 } else if (skb_is_gso_v6(skb)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08004238 ipv6_hdr(skb)->payload_len = 0;
4239 tcp_hdr(skb)->check = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
4240 &ipv6_hdr(skb)->daddr,
4241 0, IPPROTO_TCP, 0);
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004242 first->tx_flags |= IGB_TX_FLAGS_TSO |
4243 IGB_TX_FLAGS_CSUM;
Auke Kok9d5c8242008-01-24 02:22:38 -08004244 }
4245
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004246 /* compute header lengths */
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004247 l4len = tcp_hdrlen(skb);
4248 *hdr_len = skb_transport_offset(skb) + l4len;
Auke Kok9d5c8242008-01-24 02:22:38 -08004249
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004250 /* update gso size and bytecount with header size */
4251 first->gso_segs = skb_shinfo(skb)->gso_segs;
4252 first->bytecount += (first->gso_segs - 1) * *hdr_len;
4253
Auke Kok9d5c8242008-01-24 02:22:38 -08004254 /* MSS L4LEN IDX */
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004255 mss_l4len_idx = l4len << E1000_ADVTXD_L4LEN_SHIFT;
4256 mss_l4len_idx |= skb_shinfo(skb)->gso_size << E1000_ADVTXD_MSS_SHIFT;
Auke Kok9d5c8242008-01-24 02:22:38 -08004257
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004258 /* VLAN MACLEN IPLEN */
4259 vlan_macip_lens = skb_network_header_len(skb);
4260 vlan_macip_lens |= skb_network_offset(skb) << E1000_ADVTXD_MACLEN_SHIFT;
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004261 vlan_macip_lens |= first->tx_flags & IGB_TX_FLAGS_VLAN_MASK;
Auke Kok9d5c8242008-01-24 02:22:38 -08004262
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004263 igb_tx_ctxtdesc(tx_ring, vlan_macip_lens, type_tucmd, mss_l4len_idx);
Auke Kok9d5c8242008-01-24 02:22:38 -08004264
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004265 return 1;
Auke Kok9d5c8242008-01-24 02:22:38 -08004266}
4267
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004268static void igb_tx_csum(struct igb_ring *tx_ring, struct igb_tx_buffer *first)
Auke Kok9d5c8242008-01-24 02:22:38 -08004269{
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004270 struct sk_buff *skb = first->skb;
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004271 u32 vlan_macip_lens = 0;
4272 u32 mss_l4len_idx = 0;
4273 u32 type_tucmd = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08004274
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004275 if (skb->ip_summed != CHECKSUM_PARTIAL) {
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004276 if (!(first->tx_flags & IGB_TX_FLAGS_VLAN))
4277 return;
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004278 } else {
4279 u8 l4_hdr = 0;
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004280 switch (first->protocol) {
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004281 case __constant_htons(ETH_P_IP):
4282 vlan_macip_lens |= skb_network_header_len(skb);
4283 type_tucmd |= E1000_ADVTXD_TUCMD_IPV4;
4284 l4_hdr = ip_hdr(skb)->protocol;
4285 break;
4286 case __constant_htons(ETH_P_IPV6):
4287 vlan_macip_lens |= skb_network_header_len(skb);
4288 l4_hdr = ipv6_hdr(skb)->nexthdr;
4289 break;
4290 default:
4291 if (unlikely(net_ratelimit())) {
4292 dev_warn(tx_ring->dev,
4293 "partial checksum but proto=%x!\n",
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004294 first->protocol);
Arthur Jonesfa4a7ef2009-03-21 16:55:07 -07004295 }
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004296 break;
Auke Kok9d5c8242008-01-24 02:22:38 -08004297 }
4298
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004299 switch (l4_hdr) {
4300 case IPPROTO_TCP:
4301 type_tucmd |= E1000_ADVTXD_TUCMD_L4T_TCP;
4302 mss_l4len_idx = tcp_hdrlen(skb) <<
4303 E1000_ADVTXD_L4LEN_SHIFT;
4304 break;
4305 case IPPROTO_SCTP:
4306 type_tucmd |= E1000_ADVTXD_TUCMD_L4T_SCTP;
4307 mss_l4len_idx = sizeof(struct sctphdr) <<
4308 E1000_ADVTXD_L4LEN_SHIFT;
4309 break;
4310 case IPPROTO_UDP:
4311 mss_l4len_idx = sizeof(struct udphdr) <<
4312 E1000_ADVTXD_L4LEN_SHIFT;
4313 break;
4314 default:
4315 if (unlikely(net_ratelimit())) {
4316 dev_warn(tx_ring->dev,
4317 "partial checksum but l4 proto=%x!\n",
4318 l4_hdr);
4319 }
4320 break;
4321 }
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004322
4323 /* update TX checksum flag */
4324 first->tx_flags |= IGB_TX_FLAGS_CSUM;
Auke Kok9d5c8242008-01-24 02:22:38 -08004325 }
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004326
4327 vlan_macip_lens |= skb_network_offset(skb) << E1000_ADVTXD_MACLEN_SHIFT;
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004328 vlan_macip_lens |= first->tx_flags & IGB_TX_FLAGS_VLAN_MASK;
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004329
4330 igb_tx_ctxtdesc(tx_ring, vlan_macip_lens, type_tucmd, mss_l4len_idx);
Auke Kok9d5c8242008-01-24 02:22:38 -08004331}
4332
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004333#define IGB_SET_FLAG(_input, _flag, _result) \
4334 ((_flag <= _result) ? \
4335 ((u32)(_input & _flag) * (_result / _flag)) : \
4336 ((u32)(_input & _flag) / (_flag / _result)))
4337
4338static u32 igb_tx_cmd_type(struct sk_buff *skb, u32 tx_flags)
Alexander Duycke032afc2011-08-26 07:44:48 +00004339{
4340 /* set type for advanced descriptor with frame checksum insertion */
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004341 u32 cmd_type = E1000_ADVTXD_DTYP_DATA |
4342 E1000_ADVTXD_DCMD_DEXT |
4343 E1000_ADVTXD_DCMD_IFCS;
Alexander Duycke032afc2011-08-26 07:44:48 +00004344
4345 /* set HW vlan bit if vlan is present */
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004346 cmd_type |= IGB_SET_FLAG(tx_flags, IGB_TX_FLAGS_VLAN,
4347 (E1000_ADVTXD_DCMD_VLE));
Alexander Duycke032afc2011-08-26 07:44:48 +00004348
4349 /* set segmentation bits for TSO */
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004350 cmd_type |= IGB_SET_FLAG(tx_flags, IGB_TX_FLAGS_TSO,
4351 (E1000_ADVTXD_DCMD_TSE));
4352
4353 /* set timestamp bit if present */
4354 cmd_type |= IGB_SET_FLAG(tx_flags, IGB_TX_FLAGS_TSTAMP,
4355 (E1000_ADVTXD_MAC_TSTAMP));
4356
4357 /* insert frame checksum */
4358 cmd_type ^= IGB_SET_FLAG(skb->no_fcs, 1, E1000_ADVTXD_DCMD_IFCS);
Alexander Duycke032afc2011-08-26 07:44:48 +00004359
4360 return cmd_type;
4361}
4362
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004363static void igb_tx_olinfo_status(struct igb_ring *tx_ring,
4364 union e1000_adv_tx_desc *tx_desc,
4365 u32 tx_flags, unsigned int paylen)
Alexander Duycke032afc2011-08-26 07:44:48 +00004366{
4367 u32 olinfo_status = paylen << E1000_ADVTXD_PAYLEN_SHIFT;
4368
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004369 /* 82575 requires a unique index per ring */
4370 if (test_bit(IGB_RING_FLAG_TX_CTX_IDX, &tx_ring->flags))
Alexander Duycke032afc2011-08-26 07:44:48 +00004371 olinfo_status |= tx_ring->reg_idx << 4;
4372
4373 /* insert L4 checksum */
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004374 olinfo_status |= IGB_SET_FLAG(tx_flags,
4375 IGB_TX_FLAGS_CSUM,
4376 (E1000_TXD_POPTS_TXSM << 8));
Alexander Duycke032afc2011-08-26 07:44:48 +00004377
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004378 /* insert IPv4 checksum */
4379 olinfo_status |= IGB_SET_FLAG(tx_flags,
4380 IGB_TX_FLAGS_IPV4,
4381 (E1000_TXD_POPTS_IXSM << 8));
Alexander Duycke032afc2011-08-26 07:44:48 +00004382
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004383 tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status);
Alexander Duycke032afc2011-08-26 07:44:48 +00004384}
4385
Alexander Duyckebe42d12011-08-26 07:45:09 +00004386/*
4387 * The largest size we can write to the descriptor is 65535. In order to
4388 * maintain a power of two alignment we have to limit ourselves to 32K.
4389 */
4390#define IGB_MAX_TXD_PWR 15
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004391#define IGB_MAX_DATA_PER_TXD (1<<IGB_MAX_TXD_PWR)
Auke Kok9d5c8242008-01-24 02:22:38 -08004392
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004393static void igb_tx_map(struct igb_ring *tx_ring,
4394 struct igb_tx_buffer *first,
Alexander Duyckebe42d12011-08-26 07:45:09 +00004395 const u8 hdr_len)
Auke Kok9d5c8242008-01-24 02:22:38 -08004396{
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004397 struct sk_buff *skb = first->skb;
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00004398 struct igb_tx_buffer *tx_buffer;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004399 union e1000_adv_tx_desc *tx_desc;
Alexander Duyck80d07592012-11-13 04:03:24 +00004400 struct skb_frag_struct *frag;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004401 dma_addr_t dma;
Alexander Duyck80d07592012-11-13 04:03:24 +00004402 unsigned int data_len, size;
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004403 u32 tx_flags = first->tx_flags;
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004404 u32 cmd_type = igb_tx_cmd_type(skb, tx_flags);
Alexander Duyckebe42d12011-08-26 07:45:09 +00004405 u16 i = tx_ring->next_to_use;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004406
4407 tx_desc = IGB_TX_DESC(tx_ring, i);
4408
Alexander Duyck80d07592012-11-13 04:03:24 +00004409 igb_tx_olinfo_status(tx_ring, tx_desc, tx_flags, skb->len - hdr_len);
4410
4411 size = skb_headlen(skb);
4412 data_len = skb->data_len;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004413
4414 dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
Auke Kok9d5c8242008-01-24 02:22:38 -08004415
Alexander Duyck80d07592012-11-13 04:03:24 +00004416 tx_buffer = first;
Alexander Duyck2bbfebe2011-08-26 07:44:59 +00004417
Alexander Duyck80d07592012-11-13 04:03:24 +00004418 for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
4419 if (dma_mapping_error(tx_ring->dev, dma))
4420 goto dma_error;
4421
4422 /* record length, and DMA address */
4423 dma_unmap_len_set(tx_buffer, len, size);
4424 dma_unmap_addr_set(tx_buffer, dma, dma);
4425
4426 tx_desc->read.buffer_addr = cpu_to_le64(dma);
4427
Alexander Duyckebe42d12011-08-26 07:45:09 +00004428 while (unlikely(size > IGB_MAX_DATA_PER_TXD)) {
4429 tx_desc->read.cmd_type_len =
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004430 cpu_to_le32(cmd_type ^ IGB_MAX_DATA_PER_TXD);
Auke Kok9d5c8242008-01-24 02:22:38 -08004431
Alexander Duyckebe42d12011-08-26 07:45:09 +00004432 i++;
4433 tx_desc++;
4434 if (i == tx_ring->count) {
4435 tx_desc = IGB_TX_DESC(tx_ring, 0);
4436 i = 0;
4437 }
Alexander Duyck80d07592012-11-13 04:03:24 +00004438 tx_desc->read.olinfo_status = 0;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004439
4440 dma += IGB_MAX_DATA_PER_TXD;
4441 size -= IGB_MAX_DATA_PER_TXD;
4442
Alexander Duyckebe42d12011-08-26 07:45:09 +00004443 tx_desc->read.buffer_addr = cpu_to_le64(dma);
4444 }
4445
4446 if (likely(!data_len))
4447 break;
4448
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004449 tx_desc->read.cmd_type_len = cpu_to_le32(cmd_type ^ size);
Alexander Duyckebe42d12011-08-26 07:45:09 +00004450
Alexander Duyck65689fe2009-03-20 00:17:43 +00004451 i++;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004452 tx_desc++;
4453 if (i == tx_ring->count) {
4454 tx_desc = IGB_TX_DESC(tx_ring, 0);
Alexander Duyck65689fe2009-03-20 00:17:43 +00004455 i = 0;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004456 }
Alexander Duyck80d07592012-11-13 04:03:24 +00004457 tx_desc->read.olinfo_status = 0;
Alexander Duyck65689fe2009-03-20 00:17:43 +00004458
Eric Dumazet9e903e02011-10-18 21:00:24 +00004459 size = skb_frag_size(frag);
Alexander Duyckebe42d12011-08-26 07:45:09 +00004460 data_len -= size;
4461
4462 dma = skb_frag_dma_map(tx_ring->dev, frag, 0,
Alexander Duyck80d07592012-11-13 04:03:24 +00004463 size, DMA_TO_DEVICE);
Alexander Duyck6366ad32009-12-02 16:47:18 +00004464
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00004465 tx_buffer = &tx_ring->tx_buffer_info[i];
Auke Kok9d5c8242008-01-24 02:22:38 -08004466 }
4467
Alexander Duyckebe42d12011-08-26 07:45:09 +00004468 /* write last descriptor with RS and EOP bits */
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004469 cmd_type |= size | IGB_TXD_DCMD;
4470 tx_desc->read.cmd_type_len = cpu_to_le32(cmd_type);
Alexander Duyck8542db02011-08-26 07:44:43 +00004471
Alexander Duyck80d07592012-11-13 04:03:24 +00004472 netdev_tx_sent_queue(txring_txq(tx_ring), first->bytecount);
4473
Alexander Duyck8542db02011-08-26 07:44:43 +00004474 /* set the timestamp */
4475 first->time_stamp = jiffies;
4476
Alexander Duyckebe42d12011-08-26 07:45:09 +00004477 /*
4478 * Force memory writes to complete before letting h/w know there
4479 * are new descriptors to fetch. (Only applicable for weak-ordered
4480 * memory model archs, such as IA-64).
4481 *
4482 * We also need this memory barrier to make certain all of the
4483 * status bits have been updated before next_to_watch is written.
4484 */
Auke Kok9d5c8242008-01-24 02:22:38 -08004485 wmb();
4486
Alexander Duyckebe42d12011-08-26 07:45:09 +00004487 /* set next_to_watch value indicating a packet is present */
4488 first->next_to_watch = tx_desc;
4489
4490 i++;
4491 if (i == tx_ring->count)
4492 i = 0;
4493
Auke Kok9d5c8242008-01-24 02:22:38 -08004494 tx_ring->next_to_use = i;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004495
Alexander Duyckfce99e32009-10-27 15:51:27 +00004496 writel(i, tx_ring->tail);
Alexander Duyckebe42d12011-08-26 07:45:09 +00004497
Auke Kok9d5c8242008-01-24 02:22:38 -08004498 /* we need this if more than one processor can write to our tail
4499 * at a time, it syncronizes IO on IA64/Altix systems */
4500 mmiowb();
Alexander Duyckebe42d12011-08-26 07:45:09 +00004501
4502 return;
4503
4504dma_error:
4505 dev_err(tx_ring->dev, "TX DMA map failed\n");
4506
4507 /* clear dma mappings for failed tx_buffer_info map */
4508 for (;;) {
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00004509 tx_buffer = &tx_ring->tx_buffer_info[i];
4510 igb_unmap_and_free_tx_resource(tx_ring, tx_buffer);
4511 if (tx_buffer == first)
Alexander Duyckebe42d12011-08-26 07:45:09 +00004512 break;
4513 if (i == 0)
4514 i = tx_ring->count;
4515 i--;
4516 }
4517
4518 tx_ring->next_to_use = i;
Auke Kok9d5c8242008-01-24 02:22:38 -08004519}
4520
Alexander Duyck6ad4edf2011-08-26 07:45:26 +00004521static int __igb_maybe_stop_tx(struct igb_ring *tx_ring, const u16 size)
Auke Kok9d5c8242008-01-24 02:22:38 -08004522{
Alexander Duycke694e962009-10-27 15:53:06 +00004523 struct net_device *netdev = tx_ring->netdev;
4524
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07004525 netif_stop_subqueue(netdev, tx_ring->queue_index);
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07004526
Auke Kok9d5c8242008-01-24 02:22:38 -08004527 /* Herbert's original patch had:
4528 * smp_mb__after_netif_stop_queue();
4529 * but since that doesn't exist yet, just open code it. */
4530 smp_mb();
4531
4532 /* We need to check again in a case another CPU has just
4533 * made room available. */
Alexander Duyckc493ea42009-03-20 00:16:50 +00004534 if (igb_desc_unused(tx_ring) < size)
Auke Kok9d5c8242008-01-24 02:22:38 -08004535 return -EBUSY;
4536
4537 /* A reprieve! */
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07004538 netif_wake_subqueue(netdev, tx_ring->queue_index);
Eric Dumazet12dcd862010-10-15 17:27:10 +00004539
4540 u64_stats_update_begin(&tx_ring->tx_syncp2);
4541 tx_ring->tx_stats.restart_queue2++;
4542 u64_stats_update_end(&tx_ring->tx_syncp2);
4543
Auke Kok9d5c8242008-01-24 02:22:38 -08004544 return 0;
4545}
4546
Alexander Duyck6ad4edf2011-08-26 07:45:26 +00004547static inline int igb_maybe_stop_tx(struct igb_ring *tx_ring, const u16 size)
Auke Kok9d5c8242008-01-24 02:22:38 -08004548{
Alexander Duyckc493ea42009-03-20 00:16:50 +00004549 if (igb_desc_unused(tx_ring) >= size)
Auke Kok9d5c8242008-01-24 02:22:38 -08004550 return 0;
Alexander Duycke694e962009-10-27 15:53:06 +00004551 return __igb_maybe_stop_tx(tx_ring, size);
Auke Kok9d5c8242008-01-24 02:22:38 -08004552}
4553
Alexander Duyckcd392f52011-08-26 07:43:59 +00004554netdev_tx_t igb_xmit_frame_ring(struct sk_buff *skb,
4555 struct igb_ring *tx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08004556{
Matthew Vick1f6e8172012-08-18 07:26:33 +00004557 struct igb_adapter *adapter = netdev_priv(tx_ring->netdev);
Alexander Duyck8542db02011-08-26 07:44:43 +00004558 struct igb_tx_buffer *first;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004559 int tso;
Nick Nunley91d4ee32010-02-17 01:04:56 +00004560 u32 tx_flags = 0;
Alexander Duyck31f6adb2011-08-26 07:44:53 +00004561 __be16 protocol = vlan_get_protocol(skb);
Nick Nunley91d4ee32010-02-17 01:04:56 +00004562 u8 hdr_len = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08004563
Auke Kok9d5c8242008-01-24 02:22:38 -08004564 /* need: 1 descriptor per page,
4565 * + 2 desc gap to keep tail from touching head,
4566 * + 1 desc for skb->data,
4567 * + 1 desc for context descriptor,
4568 * otherwise try next time */
Alexander Duycke694e962009-10-27 15:53:06 +00004569 if (igb_maybe_stop_tx(tx_ring, skb_shinfo(skb)->nr_frags + 4)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08004570 /* this is a hard error */
Auke Kok9d5c8242008-01-24 02:22:38 -08004571 return NETDEV_TX_BUSY;
4572 }
Patrick Ohly33af6bc2009-02-12 05:03:43 +00004573
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004574 /* record the location of the first descriptor for this packet */
4575 first = &tx_ring->tx_buffer_info[tx_ring->next_to_use];
4576 first->skb = skb;
4577 first->bytecount = skb->len;
4578 first->gso_segs = 1;
4579
Matthew Vick1f6e8172012-08-18 07:26:33 +00004580 if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
4581 !(adapter->ptp_tx_skb))) {
Oliver Hartkopp2244d072010-08-17 08:59:14 +00004582 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
Patrick Ohly33af6bc2009-02-12 05:03:43 +00004583 tx_flags |= IGB_TX_FLAGS_TSTAMP;
Matthew Vick1f6e8172012-08-18 07:26:33 +00004584
4585 adapter->ptp_tx_skb = skb_get(skb);
4586 if (adapter->hw.mac.type == e1000_82576)
4587 schedule_work(&adapter->ptp_tx_work);
Patrick Ohly33af6bc2009-02-12 05:03:43 +00004588 }
Auke Kok9d5c8242008-01-24 02:22:38 -08004589
Jesse Grosseab6d182010-10-20 13:56:03 +00004590 if (vlan_tx_tag_present(skb)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08004591 tx_flags |= IGB_TX_FLAGS_VLAN;
4592 tx_flags |= (vlan_tx_tag_get(skb) << IGB_TX_FLAGS_VLAN_SHIFT);
4593 }
4594
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004595 /* record initial flags and protocol */
4596 first->tx_flags = tx_flags;
4597 first->protocol = protocol;
Alexander Duyckcdfd01fc2009-10-27 23:50:57 +00004598
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004599 tso = igb_tso(tx_ring, first, &hdr_len);
4600 if (tso < 0)
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004601 goto out_drop;
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004602 else if (!tso)
4603 igb_tx_csum(tx_ring, first);
Auke Kok9d5c8242008-01-24 02:22:38 -08004604
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004605 igb_tx_map(tx_ring, first, hdr_len);
Alexander Duyck85ad76b2009-10-27 15:52:46 +00004606
4607 /* Make sure there is space in the ring for the next send. */
Alexander Duycke694e962009-10-27 15:53:06 +00004608 igb_maybe_stop_tx(tx_ring, MAX_SKB_FRAGS + 4);
Alexander Duyck85ad76b2009-10-27 15:52:46 +00004609
Auke Kok9d5c8242008-01-24 02:22:38 -08004610 return NETDEV_TX_OK;
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004611
4612out_drop:
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004613 igb_unmap_and_free_tx_resource(tx_ring, first);
4614
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004615 return NETDEV_TX_OK;
Auke Kok9d5c8242008-01-24 02:22:38 -08004616}
4617
Alexander Duyck1cc3bd82011-08-26 07:44:10 +00004618static inline struct igb_ring *igb_tx_queue_mapping(struct igb_adapter *adapter,
4619 struct sk_buff *skb)
4620{
4621 unsigned int r_idx = skb->queue_mapping;
4622
4623 if (r_idx >= adapter->num_tx_queues)
4624 r_idx = r_idx % adapter->num_tx_queues;
4625
4626 return adapter->tx_ring[r_idx];
4627}
4628
Alexander Duyckcd392f52011-08-26 07:43:59 +00004629static netdev_tx_t igb_xmit_frame(struct sk_buff *skb,
4630 struct net_device *netdev)
Auke Kok9d5c8242008-01-24 02:22:38 -08004631{
4632 struct igb_adapter *adapter = netdev_priv(netdev);
Alexander Duyckb1a436c2009-10-27 15:54:43 +00004633
4634 if (test_bit(__IGB_DOWN, &adapter->state)) {
4635 dev_kfree_skb_any(skb);
4636 return NETDEV_TX_OK;
4637 }
4638
4639 if (skb->len <= 0) {
4640 dev_kfree_skb_any(skb);
4641 return NETDEV_TX_OK;
4642 }
4643
Alexander Duyck1cc3bd82011-08-26 07:44:10 +00004644 /*
4645 * The minimum packet size with TCTL.PSP set is 17 so pad the skb
4646 * in order to meet this minimum size requirement.
4647 */
Tushar Daveea5ceea2012-09-14 03:43:43 +00004648 if (unlikely(skb->len < 17)) {
4649 if (skb_pad(skb, 17 - skb->len))
Alexander Duyck1cc3bd82011-08-26 07:44:10 +00004650 return NETDEV_TX_OK;
4651 skb->len = 17;
Tushar Daveea5ceea2012-09-14 03:43:43 +00004652 skb_set_tail_pointer(skb, 17);
Alexander Duyck1cc3bd82011-08-26 07:44:10 +00004653 }
Auke Kok9d5c8242008-01-24 02:22:38 -08004654
Alexander Duyck1cc3bd82011-08-26 07:44:10 +00004655 return igb_xmit_frame_ring(skb, igb_tx_queue_mapping(adapter, skb));
Auke Kok9d5c8242008-01-24 02:22:38 -08004656}
4657
4658/**
4659 * igb_tx_timeout - Respond to a Tx Hang
4660 * @netdev: network interface device structure
4661 **/
4662static void igb_tx_timeout(struct net_device *netdev)
4663{
4664 struct igb_adapter *adapter = netdev_priv(netdev);
4665 struct e1000_hw *hw = &adapter->hw;
4666
4667 /* Do the reset outside of interrupt context */
4668 adapter->tx_timeout_count++;
Alexander Duyckf7ba2052009-10-27 23:48:51 +00004669
Alexander Duyck06218a82011-08-26 07:46:55 +00004670 if (hw->mac.type >= e1000_82580)
Alexander Duyck55cac242009-11-19 12:42:21 +00004671 hw->dev_spec._82575.global_device_reset = true;
4672
Auke Kok9d5c8242008-01-24 02:22:38 -08004673 schedule_work(&adapter->reset_task);
Alexander Duyck265de402009-02-06 23:22:52 +00004674 wr32(E1000_EICS,
4675 (adapter->eims_enable_mask & ~adapter->eims_other));
Auke Kok9d5c8242008-01-24 02:22:38 -08004676}
4677
4678static void igb_reset_task(struct work_struct *work)
4679{
4680 struct igb_adapter *adapter;
4681 adapter = container_of(work, struct igb_adapter, reset_task);
4682
Taku Izumic97ec422010-04-27 14:39:30 +00004683 igb_dump(adapter);
4684 netdev_err(adapter->netdev, "Reset adapter\n");
Auke Kok9d5c8242008-01-24 02:22:38 -08004685 igb_reinit_locked(adapter);
4686}
4687
4688/**
Eric Dumazet12dcd862010-10-15 17:27:10 +00004689 * igb_get_stats64 - Get System Network Statistics
Auke Kok9d5c8242008-01-24 02:22:38 -08004690 * @netdev: network interface device structure
Eric Dumazet12dcd862010-10-15 17:27:10 +00004691 * @stats: rtnl_link_stats64 pointer
Auke Kok9d5c8242008-01-24 02:22:38 -08004692 *
Auke Kok9d5c8242008-01-24 02:22:38 -08004693 **/
Eric Dumazet12dcd862010-10-15 17:27:10 +00004694static struct rtnl_link_stats64 *igb_get_stats64(struct net_device *netdev,
4695 struct rtnl_link_stats64 *stats)
Auke Kok9d5c8242008-01-24 02:22:38 -08004696{
Eric Dumazet12dcd862010-10-15 17:27:10 +00004697 struct igb_adapter *adapter = netdev_priv(netdev);
4698
4699 spin_lock(&adapter->stats64_lock);
4700 igb_update_stats(adapter, &adapter->stats64);
4701 memcpy(stats, &adapter->stats64, sizeof(*stats));
4702 spin_unlock(&adapter->stats64_lock);
4703
4704 return stats;
Auke Kok9d5c8242008-01-24 02:22:38 -08004705}
4706
4707/**
4708 * igb_change_mtu - Change the Maximum Transfer Unit
4709 * @netdev: network interface device structure
4710 * @new_mtu: new value for maximum frame size
4711 *
4712 * Returns 0 on success, negative on failure
4713 **/
4714static int igb_change_mtu(struct net_device *netdev, int new_mtu)
4715{
4716 struct igb_adapter *adapter = netdev_priv(netdev);
Alexander Duyck090b1792009-10-27 23:51:55 +00004717 struct pci_dev *pdev = adapter->pdev;
Alexander Duyck153285f2011-08-26 07:43:32 +00004718 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
Auke Kok9d5c8242008-01-24 02:22:38 -08004719
Alexander Duyckc809d222009-10-27 23:52:13 +00004720 if ((new_mtu < 68) || (max_frame > MAX_JUMBO_FRAME_SIZE)) {
Alexander Duyck090b1792009-10-27 23:51:55 +00004721 dev_err(&pdev->dev, "Invalid MTU setting\n");
Auke Kok9d5c8242008-01-24 02:22:38 -08004722 return -EINVAL;
4723 }
4724
Alexander Duyck153285f2011-08-26 07:43:32 +00004725#define MAX_STD_JUMBO_FRAME_SIZE 9238
Auke Kok9d5c8242008-01-24 02:22:38 -08004726 if (max_frame > MAX_STD_JUMBO_FRAME_SIZE) {
Alexander Duyck090b1792009-10-27 23:51:55 +00004727 dev_err(&pdev->dev, "MTU > 9216 not supported.\n");
Auke Kok9d5c8242008-01-24 02:22:38 -08004728 return -EINVAL;
4729 }
4730
4731 while (test_and_set_bit(__IGB_RESETTING, &adapter->state))
4732 msleep(1);
Alexander Duyck73cd78f2009-02-12 18:16:59 +00004733
Auke Kok9d5c8242008-01-24 02:22:38 -08004734 /* igb_down has a dependency on max_frame_size */
4735 adapter->max_frame_size = max_frame;
Alexander Duyck559e9c42009-10-27 23:52:50 +00004736
Alexander Duyck4c844852009-10-27 15:52:07 +00004737 if (netif_running(netdev))
4738 igb_down(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08004739
Alexander Duyck090b1792009-10-27 23:51:55 +00004740 dev_info(&pdev->dev, "changing MTU from %d to %d\n",
Auke Kok9d5c8242008-01-24 02:22:38 -08004741 netdev->mtu, new_mtu);
4742 netdev->mtu = new_mtu;
4743
4744 if (netif_running(netdev))
4745 igb_up(adapter);
4746 else
4747 igb_reset(adapter);
4748
4749 clear_bit(__IGB_RESETTING, &adapter->state);
4750
4751 return 0;
4752}
4753
4754/**
4755 * igb_update_stats - Update the board statistics counters
4756 * @adapter: board private structure
4757 **/
4758
Eric Dumazet12dcd862010-10-15 17:27:10 +00004759void igb_update_stats(struct igb_adapter *adapter,
4760 struct rtnl_link_stats64 *net_stats)
Auke Kok9d5c8242008-01-24 02:22:38 -08004761{
4762 struct e1000_hw *hw = &adapter->hw;
4763 struct pci_dev *pdev = adapter->pdev;
Mitch Williamsfa3d9a62010-03-23 18:34:38 +00004764 u32 reg, mpc;
Auke Kok9d5c8242008-01-24 02:22:38 -08004765 u16 phy_tmp;
Alexander Duyck3f9c0162009-10-27 23:48:12 +00004766 int i;
4767 u64 bytes, packets;
Eric Dumazet12dcd862010-10-15 17:27:10 +00004768 unsigned int start;
4769 u64 _bytes, _packets;
Auke Kok9d5c8242008-01-24 02:22:38 -08004770
4771#define PHY_IDLE_ERROR_COUNT_MASK 0x00FF
4772
4773 /*
4774 * Prevent stats update while adapter is being reset, or if the pci
4775 * connection is down.
4776 */
4777 if (adapter->link_speed == 0)
4778 return;
4779 if (pci_channel_offline(pdev))
4780 return;
4781
Alexander Duyck3f9c0162009-10-27 23:48:12 +00004782 bytes = 0;
4783 packets = 0;
4784 for (i = 0; i < adapter->num_rx_queues; i++) {
Alexander Duyckae1c07a2012-08-08 05:23:22 +00004785 u32 rqdpc = rd32(E1000_RQDPC(i));
Alexander Duyck3025a442010-02-17 01:02:39 +00004786 struct igb_ring *ring = adapter->rx_ring[i];
Eric Dumazet12dcd862010-10-15 17:27:10 +00004787
Alexander Duyckae1c07a2012-08-08 05:23:22 +00004788 if (rqdpc) {
4789 ring->rx_stats.drops += rqdpc;
4790 net_stats->rx_fifo_errors += rqdpc;
4791 }
Eric Dumazet12dcd862010-10-15 17:27:10 +00004792
4793 do {
4794 start = u64_stats_fetch_begin_bh(&ring->rx_syncp);
4795 _bytes = ring->rx_stats.bytes;
4796 _packets = ring->rx_stats.packets;
4797 } while (u64_stats_fetch_retry_bh(&ring->rx_syncp, start));
4798 bytes += _bytes;
4799 packets += _packets;
Alexander Duyck3f9c0162009-10-27 23:48:12 +00004800 }
4801
Alexander Duyck128e45e2009-11-12 18:37:38 +00004802 net_stats->rx_bytes = bytes;
4803 net_stats->rx_packets = packets;
Alexander Duyck3f9c0162009-10-27 23:48:12 +00004804
4805 bytes = 0;
4806 packets = 0;
4807 for (i = 0; i < adapter->num_tx_queues; i++) {
Alexander Duyck3025a442010-02-17 01:02:39 +00004808 struct igb_ring *ring = adapter->tx_ring[i];
Eric Dumazet12dcd862010-10-15 17:27:10 +00004809 do {
4810 start = u64_stats_fetch_begin_bh(&ring->tx_syncp);
4811 _bytes = ring->tx_stats.bytes;
4812 _packets = ring->tx_stats.packets;
4813 } while (u64_stats_fetch_retry_bh(&ring->tx_syncp, start));
4814 bytes += _bytes;
4815 packets += _packets;
Alexander Duyck3f9c0162009-10-27 23:48:12 +00004816 }
Alexander Duyck128e45e2009-11-12 18:37:38 +00004817 net_stats->tx_bytes = bytes;
4818 net_stats->tx_packets = packets;
Alexander Duyck3f9c0162009-10-27 23:48:12 +00004819
4820 /* read stats registers */
Auke Kok9d5c8242008-01-24 02:22:38 -08004821 adapter->stats.crcerrs += rd32(E1000_CRCERRS);
4822 adapter->stats.gprc += rd32(E1000_GPRC);
4823 adapter->stats.gorc += rd32(E1000_GORCL);
4824 rd32(E1000_GORCH); /* clear GORCL */
4825 adapter->stats.bprc += rd32(E1000_BPRC);
4826 adapter->stats.mprc += rd32(E1000_MPRC);
4827 adapter->stats.roc += rd32(E1000_ROC);
4828
4829 adapter->stats.prc64 += rd32(E1000_PRC64);
4830 adapter->stats.prc127 += rd32(E1000_PRC127);
4831 adapter->stats.prc255 += rd32(E1000_PRC255);
4832 adapter->stats.prc511 += rd32(E1000_PRC511);
4833 adapter->stats.prc1023 += rd32(E1000_PRC1023);
4834 adapter->stats.prc1522 += rd32(E1000_PRC1522);
4835 adapter->stats.symerrs += rd32(E1000_SYMERRS);
4836 adapter->stats.sec += rd32(E1000_SEC);
4837
Mitch Williamsfa3d9a62010-03-23 18:34:38 +00004838 mpc = rd32(E1000_MPC);
4839 adapter->stats.mpc += mpc;
4840 net_stats->rx_fifo_errors += mpc;
Auke Kok9d5c8242008-01-24 02:22:38 -08004841 adapter->stats.scc += rd32(E1000_SCC);
4842 adapter->stats.ecol += rd32(E1000_ECOL);
4843 adapter->stats.mcc += rd32(E1000_MCC);
4844 adapter->stats.latecol += rd32(E1000_LATECOL);
4845 adapter->stats.dc += rd32(E1000_DC);
4846 adapter->stats.rlec += rd32(E1000_RLEC);
4847 adapter->stats.xonrxc += rd32(E1000_XONRXC);
4848 adapter->stats.xontxc += rd32(E1000_XONTXC);
4849 adapter->stats.xoffrxc += rd32(E1000_XOFFRXC);
4850 adapter->stats.xofftxc += rd32(E1000_XOFFTXC);
4851 adapter->stats.fcruc += rd32(E1000_FCRUC);
4852 adapter->stats.gptc += rd32(E1000_GPTC);
4853 adapter->stats.gotc += rd32(E1000_GOTCL);
4854 rd32(E1000_GOTCH); /* clear GOTCL */
Mitch Williamsfa3d9a62010-03-23 18:34:38 +00004855 adapter->stats.rnbc += rd32(E1000_RNBC);
Auke Kok9d5c8242008-01-24 02:22:38 -08004856 adapter->stats.ruc += rd32(E1000_RUC);
4857 adapter->stats.rfc += rd32(E1000_RFC);
4858 adapter->stats.rjc += rd32(E1000_RJC);
4859 adapter->stats.tor += rd32(E1000_TORH);
4860 adapter->stats.tot += rd32(E1000_TOTH);
4861 adapter->stats.tpr += rd32(E1000_TPR);
4862
4863 adapter->stats.ptc64 += rd32(E1000_PTC64);
4864 adapter->stats.ptc127 += rd32(E1000_PTC127);
4865 adapter->stats.ptc255 += rd32(E1000_PTC255);
4866 adapter->stats.ptc511 += rd32(E1000_PTC511);
4867 adapter->stats.ptc1023 += rd32(E1000_PTC1023);
4868 adapter->stats.ptc1522 += rd32(E1000_PTC1522);
4869
4870 adapter->stats.mptc += rd32(E1000_MPTC);
4871 adapter->stats.bptc += rd32(E1000_BPTC);
4872
Nick Nunley2d0b0f62010-02-17 01:02:59 +00004873 adapter->stats.tpt += rd32(E1000_TPT);
4874 adapter->stats.colc += rd32(E1000_COLC);
Auke Kok9d5c8242008-01-24 02:22:38 -08004875
4876 adapter->stats.algnerrc += rd32(E1000_ALGNERRC);
Nick Nunley43915c7c2010-02-17 01:03:58 +00004877 /* read internal phy specific stats */
4878 reg = rd32(E1000_CTRL_EXT);
4879 if (!(reg & E1000_CTRL_EXT_LINK_MODE_MASK)) {
4880 adapter->stats.rxerrc += rd32(E1000_RXERRC);
Carolyn Wyborny3dbdf962012-09-12 04:36:24 +00004881
4882 /* this stat has invalid values on i210/i211 */
4883 if ((hw->mac.type != e1000_i210) &&
4884 (hw->mac.type != e1000_i211))
4885 adapter->stats.tncrs += rd32(E1000_TNCRS);
Nick Nunley43915c7c2010-02-17 01:03:58 +00004886 }
4887
Auke Kok9d5c8242008-01-24 02:22:38 -08004888 adapter->stats.tsctc += rd32(E1000_TSCTC);
4889 adapter->stats.tsctfc += rd32(E1000_TSCTFC);
4890
4891 adapter->stats.iac += rd32(E1000_IAC);
4892 adapter->stats.icrxoc += rd32(E1000_ICRXOC);
4893 adapter->stats.icrxptc += rd32(E1000_ICRXPTC);
4894 adapter->stats.icrxatc += rd32(E1000_ICRXATC);
4895 adapter->stats.ictxptc += rd32(E1000_ICTXPTC);
4896 adapter->stats.ictxatc += rd32(E1000_ICTXATC);
4897 adapter->stats.ictxqec += rd32(E1000_ICTXQEC);
4898 adapter->stats.ictxqmtc += rd32(E1000_ICTXQMTC);
4899 adapter->stats.icrxdmtc += rd32(E1000_ICRXDMTC);
4900
4901 /* Fill out the OS statistics structure */
Alexander Duyck128e45e2009-11-12 18:37:38 +00004902 net_stats->multicast = adapter->stats.mprc;
4903 net_stats->collisions = adapter->stats.colc;
Auke Kok9d5c8242008-01-24 02:22:38 -08004904
4905 /* Rx Errors */
4906
4907 /* RLEC on some newer hardware can be incorrect so build
Jesper Dangaard Brouer8c0ab702009-05-26 13:50:31 +00004908 * our own version based on RUC and ROC */
Alexander Duyck128e45e2009-11-12 18:37:38 +00004909 net_stats->rx_errors = adapter->stats.rxerrc +
Auke Kok9d5c8242008-01-24 02:22:38 -08004910 adapter->stats.crcerrs + adapter->stats.algnerrc +
4911 adapter->stats.ruc + adapter->stats.roc +
4912 adapter->stats.cexterr;
Alexander Duyck128e45e2009-11-12 18:37:38 +00004913 net_stats->rx_length_errors = adapter->stats.ruc +
4914 adapter->stats.roc;
4915 net_stats->rx_crc_errors = adapter->stats.crcerrs;
4916 net_stats->rx_frame_errors = adapter->stats.algnerrc;
4917 net_stats->rx_missed_errors = adapter->stats.mpc;
Auke Kok9d5c8242008-01-24 02:22:38 -08004918
4919 /* Tx Errors */
Alexander Duyck128e45e2009-11-12 18:37:38 +00004920 net_stats->tx_errors = adapter->stats.ecol +
4921 adapter->stats.latecol;
4922 net_stats->tx_aborted_errors = adapter->stats.ecol;
4923 net_stats->tx_window_errors = adapter->stats.latecol;
4924 net_stats->tx_carrier_errors = adapter->stats.tncrs;
Auke Kok9d5c8242008-01-24 02:22:38 -08004925
4926 /* Tx Dropped needs to be maintained elsewhere */
4927
4928 /* Phy Stats */
4929 if (hw->phy.media_type == e1000_media_type_copper) {
4930 if ((adapter->link_speed == SPEED_1000) &&
Alexander Duyck73cd78f2009-02-12 18:16:59 +00004931 (!igb_read_phy_reg(hw, PHY_1000T_STATUS, &phy_tmp))) {
Auke Kok9d5c8242008-01-24 02:22:38 -08004932 phy_tmp &= PHY_IDLE_ERROR_COUNT_MASK;
4933 adapter->phy_stats.idle_errors += phy_tmp;
4934 }
4935 }
4936
4937 /* Management Stats */
4938 adapter->stats.mgptc += rd32(E1000_MGTPTC);
4939 adapter->stats.mgprc += rd32(E1000_MGTPRC);
4940 adapter->stats.mgpdc += rd32(E1000_MGTPDC);
Carolyn Wyborny0a915b92011-02-26 07:42:37 +00004941
4942 /* OS2BMC Stats */
4943 reg = rd32(E1000_MANC);
4944 if (reg & E1000_MANC_EN_BMC2OS) {
4945 adapter->stats.o2bgptc += rd32(E1000_O2BGPTC);
4946 adapter->stats.o2bspc += rd32(E1000_O2BSPC);
4947 adapter->stats.b2ospc += rd32(E1000_B2OSPC);
4948 adapter->stats.b2ogprc += rd32(E1000_B2OGPRC);
4949 }
Auke Kok9d5c8242008-01-24 02:22:38 -08004950}
4951
Auke Kok9d5c8242008-01-24 02:22:38 -08004952static irqreturn_t igb_msix_other(int irq, void *data)
4953{
Alexander Duyck047e0032009-10-27 15:49:27 +00004954 struct igb_adapter *adapter = data;
Auke Kok9d5c8242008-01-24 02:22:38 -08004955 struct e1000_hw *hw = &adapter->hw;
PJ Waskiewicz844290e2008-06-27 11:00:39 -07004956 u32 icr = rd32(E1000_ICR);
PJ Waskiewicz844290e2008-06-27 11:00:39 -07004957 /* reading ICR causes bit 31 of EICR to be cleared */
Alexander Duyckdda0e082009-02-06 23:19:08 +00004958
Alexander Duyck7f081d42010-01-07 17:41:00 +00004959 if (icr & E1000_ICR_DRSTA)
4960 schedule_work(&adapter->reset_task);
4961
Alexander Duyck047e0032009-10-27 15:49:27 +00004962 if (icr & E1000_ICR_DOUTSYNC) {
Alexander Duyckdda0e082009-02-06 23:19:08 +00004963 /* HW is reporting DMA is out of sync */
4964 adapter->stats.doosync++;
Greg Rose13800462010-11-06 02:08:26 +00004965 /* The DMA Out of Sync is also indication of a spoof event
4966 * in IOV mode. Check the Wrong VM Behavior register to
4967 * see if it is really a spoof event. */
4968 igb_check_wvbr(adapter);
Alexander Duyckdda0e082009-02-06 23:19:08 +00004969 }
Alexander Duyckeebbbdb2009-02-06 23:19:29 +00004970
Alexander Duyck4ae196d2009-02-19 20:40:07 -08004971 /* Check for a mailbox event */
4972 if (icr & E1000_ICR_VMMB)
4973 igb_msg_task(adapter);
4974
4975 if (icr & E1000_ICR_LSC) {
4976 hw->mac.get_link_status = 1;
4977 /* guard against interrupt when we're going down */
4978 if (!test_bit(__IGB_DOWN, &adapter->state))
4979 mod_timer(&adapter->watchdog_timer, jiffies + 1);
4980 }
4981
Matthew Vick1f6e8172012-08-18 07:26:33 +00004982 if (icr & E1000_ICR_TS) {
4983 u32 tsicr = rd32(E1000_TSICR);
4984
4985 if (tsicr & E1000_TSICR_TXTS) {
4986 /* acknowledge the interrupt */
4987 wr32(E1000_TSICR, E1000_TSICR_TXTS);
4988 /* retrieve hardware timestamp */
4989 schedule_work(&adapter->ptp_tx_work);
4990 }
4991 }
Matthew Vick1f6e8172012-08-18 07:26:33 +00004992
PJ Waskiewicz844290e2008-06-27 11:00:39 -07004993 wr32(E1000_EIMS, adapter->eims_other);
Auke Kok9d5c8242008-01-24 02:22:38 -08004994
4995 return IRQ_HANDLED;
4996}
4997
Alexander Duyck047e0032009-10-27 15:49:27 +00004998static void igb_write_itr(struct igb_q_vector *q_vector)
Auke Kok9d5c8242008-01-24 02:22:38 -08004999{
Alexander Duyck26b39272010-02-17 01:00:41 +00005000 struct igb_adapter *adapter = q_vector->adapter;
Alexander Duyck047e0032009-10-27 15:49:27 +00005001 u32 itr_val = q_vector->itr_val & 0x7FFC;
Auke Kok9d5c8242008-01-24 02:22:38 -08005002
Alexander Duyck047e0032009-10-27 15:49:27 +00005003 if (!q_vector->set_itr)
5004 return;
Alexander Duyck73cd78f2009-02-12 18:16:59 +00005005
Alexander Duyck047e0032009-10-27 15:49:27 +00005006 if (!itr_val)
5007 itr_val = 0x4;
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07005008
Alexander Duyck26b39272010-02-17 01:00:41 +00005009 if (adapter->hw.mac.type == e1000_82575)
5010 itr_val |= itr_val << 16;
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07005011 else
Alexander Duyck0ba82992011-08-26 07:45:47 +00005012 itr_val |= E1000_EITR_CNT_IGNR;
Alexander Duyck047e0032009-10-27 15:49:27 +00005013
5014 writel(itr_val, q_vector->itr_register);
5015 q_vector->set_itr = 0;
5016}
5017
5018static irqreturn_t igb_msix_ring(int irq, void *data)
5019{
5020 struct igb_q_vector *q_vector = data;
5021
5022 /* Write the ITR value calculated from the previous interrupt. */
5023 igb_write_itr(q_vector);
5024
5025 napi_schedule(&q_vector->napi);
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07005026
Auke Kok9d5c8242008-01-24 02:22:38 -08005027 return IRQ_HANDLED;
5028}
5029
Jeff Kirsher421e02f2008-10-17 11:08:31 -07005030#ifdef CONFIG_IGB_DCA
Alexander Duyck6a050042012-09-25 00:31:27 +00005031static void igb_update_tx_dca(struct igb_adapter *adapter,
5032 struct igb_ring *tx_ring,
5033 int cpu)
5034{
5035 struct e1000_hw *hw = &adapter->hw;
5036 u32 txctrl = dca3_get_tag(tx_ring->dev, cpu);
5037
5038 if (hw->mac.type != e1000_82575)
5039 txctrl <<= E1000_DCA_TXCTRL_CPUID_SHIFT;
5040
5041 /*
5042 * We can enable relaxed ordering for reads, but not writes when
5043 * DCA is enabled. This is due to a known issue in some chipsets
5044 * which will cause the DCA tag to be cleared.
5045 */
5046 txctrl |= E1000_DCA_TXCTRL_DESC_RRO_EN |
5047 E1000_DCA_TXCTRL_DATA_RRO_EN |
5048 E1000_DCA_TXCTRL_DESC_DCA_EN;
5049
5050 wr32(E1000_DCA_TXCTRL(tx_ring->reg_idx), txctrl);
5051}
5052
5053static void igb_update_rx_dca(struct igb_adapter *adapter,
5054 struct igb_ring *rx_ring,
5055 int cpu)
5056{
5057 struct e1000_hw *hw = &adapter->hw;
5058 u32 rxctrl = dca3_get_tag(&adapter->pdev->dev, cpu);
5059
5060 if (hw->mac.type != e1000_82575)
5061 rxctrl <<= E1000_DCA_RXCTRL_CPUID_SHIFT;
5062
5063 /*
5064 * We can enable relaxed ordering for reads, but not writes when
5065 * DCA is enabled. This is due to a known issue in some chipsets
5066 * which will cause the DCA tag to be cleared.
5067 */
5068 rxctrl |= E1000_DCA_RXCTRL_DESC_RRO_EN |
5069 E1000_DCA_RXCTRL_DESC_DCA_EN;
5070
5071 wr32(E1000_DCA_RXCTRL(rx_ring->reg_idx), rxctrl);
5072}
5073
Alexander Duyck047e0032009-10-27 15:49:27 +00005074static void igb_update_dca(struct igb_q_vector *q_vector)
Jeb Cramerfe4506b2008-07-08 15:07:55 -07005075{
Alexander Duyck047e0032009-10-27 15:49:27 +00005076 struct igb_adapter *adapter = q_vector->adapter;
Jeb Cramerfe4506b2008-07-08 15:07:55 -07005077 int cpu = get_cpu();
Jeb Cramerfe4506b2008-07-08 15:07:55 -07005078
Alexander Duyck047e0032009-10-27 15:49:27 +00005079 if (q_vector->cpu == cpu)
5080 goto out_no_update;
5081
Alexander Duyck6a050042012-09-25 00:31:27 +00005082 if (q_vector->tx.ring)
5083 igb_update_tx_dca(adapter, q_vector->tx.ring, cpu);
5084
5085 if (q_vector->rx.ring)
5086 igb_update_rx_dca(adapter, q_vector->rx.ring, cpu);
5087
Alexander Duyck047e0032009-10-27 15:49:27 +00005088 q_vector->cpu = cpu;
5089out_no_update:
Jeb Cramerfe4506b2008-07-08 15:07:55 -07005090 put_cpu();
5091}
5092
5093static void igb_setup_dca(struct igb_adapter *adapter)
5094{
Alexander Duyck7e0e99e2009-05-21 13:06:56 +00005095 struct e1000_hw *hw = &adapter->hw;
Jeb Cramerfe4506b2008-07-08 15:07:55 -07005096 int i;
5097
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07005098 if (!(adapter->flags & IGB_FLAG_DCA_ENABLED))
Jeb Cramerfe4506b2008-07-08 15:07:55 -07005099 return;
5100
Alexander Duyck7e0e99e2009-05-21 13:06:56 +00005101 /* Always use CB2 mode, difference is masked in the CB driver. */
5102 wr32(E1000_DCA_CTRL, E1000_DCA_CTRL_DCA_MODE_CB2);
5103
Alexander Duyck047e0032009-10-27 15:49:27 +00005104 for (i = 0; i < adapter->num_q_vectors; i++) {
Alexander Duyck26b39272010-02-17 01:00:41 +00005105 adapter->q_vector[i]->cpu = -1;
5106 igb_update_dca(adapter->q_vector[i]);
Jeb Cramerfe4506b2008-07-08 15:07:55 -07005107 }
5108}
5109
5110static int __igb_notify_dca(struct device *dev, void *data)
5111{
5112 struct net_device *netdev = dev_get_drvdata(dev);
5113 struct igb_adapter *adapter = netdev_priv(netdev);
Alexander Duyck090b1792009-10-27 23:51:55 +00005114 struct pci_dev *pdev = adapter->pdev;
Jeb Cramerfe4506b2008-07-08 15:07:55 -07005115 struct e1000_hw *hw = &adapter->hw;
5116 unsigned long event = *(unsigned long *)data;
5117
5118 switch (event) {
5119 case DCA_PROVIDER_ADD:
5120 /* if already enabled, don't do it again */
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07005121 if (adapter->flags & IGB_FLAG_DCA_ENABLED)
Jeb Cramerfe4506b2008-07-08 15:07:55 -07005122 break;
Jeb Cramerfe4506b2008-07-08 15:07:55 -07005123 if (dca_add_requester(dev) == 0) {
Alexander Duyckbbd98fe2009-01-31 00:52:30 -08005124 adapter->flags |= IGB_FLAG_DCA_ENABLED;
Alexander Duyck090b1792009-10-27 23:51:55 +00005125 dev_info(&pdev->dev, "DCA enabled\n");
Jeb Cramerfe4506b2008-07-08 15:07:55 -07005126 igb_setup_dca(adapter);
5127 break;
5128 }
5129 /* Fall Through since DCA is disabled. */
5130 case DCA_PROVIDER_REMOVE:
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07005131 if (adapter->flags & IGB_FLAG_DCA_ENABLED) {
Jeb Cramerfe4506b2008-07-08 15:07:55 -07005132 /* without this a class_device is left
Alexander Duyck047e0032009-10-27 15:49:27 +00005133 * hanging around in the sysfs model */
Jeb Cramerfe4506b2008-07-08 15:07:55 -07005134 dca_remove_requester(dev);
Alexander Duyck090b1792009-10-27 23:51:55 +00005135 dev_info(&pdev->dev, "DCA disabled\n");
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07005136 adapter->flags &= ~IGB_FLAG_DCA_ENABLED;
Alexander Duyckcbd347a2009-02-15 23:59:44 -08005137 wr32(E1000_DCA_CTRL, E1000_DCA_CTRL_DCA_MODE_DISABLE);
Jeb Cramerfe4506b2008-07-08 15:07:55 -07005138 }
5139 break;
5140 }
Alexander Duyckbbd98fe2009-01-31 00:52:30 -08005141
Jeb Cramerfe4506b2008-07-08 15:07:55 -07005142 return 0;
5143}
5144
5145static int igb_notify_dca(struct notifier_block *nb, unsigned long event,
5146 void *p)
5147{
5148 int ret_val;
5149
5150 ret_val = driver_for_each_device(&igb_driver.driver, NULL, &event,
5151 __igb_notify_dca);
5152
5153 return ret_val ? NOTIFY_BAD : NOTIFY_DONE;
5154}
Jeff Kirsher421e02f2008-10-17 11:08:31 -07005155#endif /* CONFIG_IGB_DCA */
Auke Kok9d5c8242008-01-24 02:22:38 -08005156
Greg Rose0224d662011-10-14 02:57:14 +00005157#ifdef CONFIG_PCI_IOV
5158static int igb_vf_configure(struct igb_adapter *adapter, int vf)
5159{
5160 unsigned char mac_addr[ETH_ALEN];
Greg Rose0224d662011-10-14 02:57:14 +00005161
Joe Perches7efd26d2012-07-12 19:33:06 +00005162 eth_random_addr(mac_addr);
Greg Rose0224d662011-10-14 02:57:14 +00005163 igb_set_vf_mac(adapter, vf, mac_addr);
5164
Stefan Assmannf5571472012-08-18 04:06:11 +00005165 return 0;
Greg Rose0224d662011-10-14 02:57:14 +00005166}
5167
Stefan Assmannf5571472012-08-18 04:06:11 +00005168static bool igb_vfs_are_assigned(struct igb_adapter *adapter)
Greg Rose0224d662011-10-14 02:57:14 +00005169{
Greg Rose0224d662011-10-14 02:57:14 +00005170 struct pci_dev *pdev = adapter->pdev;
Stefan Assmannf5571472012-08-18 04:06:11 +00005171 struct pci_dev *vfdev;
5172 int dev_id;
Greg Rose0224d662011-10-14 02:57:14 +00005173
5174 switch (adapter->hw.mac.type) {
5175 case e1000_82576:
Stefan Assmannf5571472012-08-18 04:06:11 +00005176 dev_id = IGB_82576_VF_DEV_ID;
Greg Rose0224d662011-10-14 02:57:14 +00005177 break;
5178 case e1000_i350:
Stefan Assmannf5571472012-08-18 04:06:11 +00005179 dev_id = IGB_I350_VF_DEV_ID;
Greg Rose0224d662011-10-14 02:57:14 +00005180 break;
5181 default:
Stefan Assmannf5571472012-08-18 04:06:11 +00005182 return false;
Greg Rose0224d662011-10-14 02:57:14 +00005183 }
5184
Stefan Assmannf5571472012-08-18 04:06:11 +00005185 /* loop through all the VFs to see if we own any that are assigned */
5186 vfdev = pci_get_device(PCI_VENDOR_ID_INTEL, dev_id, NULL);
5187 while (vfdev) {
5188 /* if we don't own it we don't care */
5189 if (vfdev->is_virtfn && vfdev->physfn == pdev) {
5190 /* if it is assigned we cannot release it */
5191 if (vfdev->dev_flags & PCI_DEV_FLAGS_ASSIGNED)
Greg Rose0224d662011-10-14 02:57:14 +00005192 return true;
5193 }
Stefan Assmannf5571472012-08-18 04:06:11 +00005194
5195 vfdev = pci_get_device(PCI_VENDOR_ID_INTEL, dev_id, vfdev);
Greg Rose0224d662011-10-14 02:57:14 +00005196 }
Stefan Assmannf5571472012-08-18 04:06:11 +00005197
Greg Rose0224d662011-10-14 02:57:14 +00005198 return false;
5199}
5200
5201#endif
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005202static void igb_ping_all_vfs(struct igb_adapter *adapter)
5203{
5204 struct e1000_hw *hw = &adapter->hw;
5205 u32 ping;
5206 int i;
5207
5208 for (i = 0 ; i < adapter->vfs_allocated_count; i++) {
5209 ping = E1000_PF_CONTROL_MSG;
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005210 if (adapter->vf_data[i].flags & IGB_VF_FLAG_CTS)
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005211 ping |= E1000_VT_MSGTYPE_CTS;
5212 igb_write_mbx(hw, &ping, 1, i);
5213 }
5214}
5215
Alexander Duyck7d5753f2009-10-27 23:47:16 +00005216static int igb_set_vf_promisc(struct igb_adapter *adapter, u32 *msgbuf, u32 vf)
5217{
5218 struct e1000_hw *hw = &adapter->hw;
5219 u32 vmolr = rd32(E1000_VMOLR(vf));
5220 struct vf_data_storage *vf_data = &adapter->vf_data[vf];
5221
Alexander Duyckd85b90042010-09-22 17:56:20 +00005222 vf_data->flags &= ~(IGB_VF_FLAG_UNI_PROMISC |
Alexander Duyck7d5753f2009-10-27 23:47:16 +00005223 IGB_VF_FLAG_MULTI_PROMISC);
5224 vmolr &= ~(E1000_VMOLR_ROPE | E1000_VMOLR_ROMPE | E1000_VMOLR_MPME);
5225
5226 if (*msgbuf & E1000_VF_SET_PROMISC_MULTICAST) {
5227 vmolr |= E1000_VMOLR_MPME;
Alexander Duyckd85b90042010-09-22 17:56:20 +00005228 vf_data->flags |= IGB_VF_FLAG_MULTI_PROMISC;
Alexander Duyck7d5753f2009-10-27 23:47:16 +00005229 *msgbuf &= ~E1000_VF_SET_PROMISC_MULTICAST;
5230 } else {
5231 /*
5232 * if we have hashes and we are clearing a multicast promisc
5233 * flag we need to write the hashes to the MTA as this step
5234 * was previously skipped
5235 */
5236 if (vf_data->num_vf_mc_hashes > 30) {
5237 vmolr |= E1000_VMOLR_MPME;
5238 } else if (vf_data->num_vf_mc_hashes) {
5239 int j;
5240 vmolr |= E1000_VMOLR_ROMPE;
5241 for (j = 0; j < vf_data->num_vf_mc_hashes; j++)
5242 igb_mta_set(hw, vf_data->vf_mc_hashes[j]);
5243 }
5244 }
5245
5246 wr32(E1000_VMOLR(vf), vmolr);
5247
5248 /* there are flags left unprocessed, likely not supported */
5249 if (*msgbuf & E1000_VT_MSGINFO_MASK)
5250 return -EINVAL;
5251
5252 return 0;
5253
5254}
5255
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005256static int igb_set_vf_multicasts(struct igb_adapter *adapter,
5257 u32 *msgbuf, u32 vf)
5258{
5259 int n = (msgbuf[0] & E1000_VT_MSGINFO_MASK) >> E1000_VT_MSGINFO_SHIFT;
5260 u16 *hash_list = (u16 *)&msgbuf[1];
5261 struct vf_data_storage *vf_data = &adapter->vf_data[vf];
5262 int i;
5263
Alexander Duyck7d5753f2009-10-27 23:47:16 +00005264 /* salt away the number of multicast addresses assigned
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005265 * to this VF for later use to restore when the PF multi cast
5266 * list changes
5267 */
5268 vf_data->num_vf_mc_hashes = n;
5269
Alexander Duyck7d5753f2009-10-27 23:47:16 +00005270 /* only up to 30 hash values supported */
5271 if (n > 30)
5272 n = 30;
5273
5274 /* store the hashes for later use */
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005275 for (i = 0; i < n; i++)
Joe Perchesa419aef2009-08-18 11:18:35 -07005276 vf_data->vf_mc_hashes[i] = hash_list[i];
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005277
5278 /* Flush and reset the mta with the new values */
Alexander Duyckff41f8d2009-09-03 14:48:56 +00005279 igb_set_rx_mode(adapter->netdev);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005280
5281 return 0;
5282}
5283
5284static void igb_restore_vf_multicasts(struct igb_adapter *adapter)
5285{
5286 struct e1000_hw *hw = &adapter->hw;
5287 struct vf_data_storage *vf_data;
5288 int i, j;
5289
5290 for (i = 0; i < adapter->vfs_allocated_count; i++) {
Alexander Duyck7d5753f2009-10-27 23:47:16 +00005291 u32 vmolr = rd32(E1000_VMOLR(i));
5292 vmolr &= ~(E1000_VMOLR_ROMPE | E1000_VMOLR_MPME);
5293
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005294 vf_data = &adapter->vf_data[i];
Alexander Duyck7d5753f2009-10-27 23:47:16 +00005295
5296 if ((vf_data->num_vf_mc_hashes > 30) ||
5297 (vf_data->flags & IGB_VF_FLAG_MULTI_PROMISC)) {
5298 vmolr |= E1000_VMOLR_MPME;
5299 } else if (vf_data->num_vf_mc_hashes) {
5300 vmolr |= E1000_VMOLR_ROMPE;
5301 for (j = 0; j < vf_data->num_vf_mc_hashes; j++)
5302 igb_mta_set(hw, vf_data->vf_mc_hashes[j]);
5303 }
5304 wr32(E1000_VMOLR(i), vmolr);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005305 }
5306}
5307
5308static void igb_clear_vf_vfta(struct igb_adapter *adapter, u32 vf)
5309{
5310 struct e1000_hw *hw = &adapter->hw;
5311 u32 pool_mask, reg, vid;
5312 int i;
5313
5314 pool_mask = 1 << (E1000_VLVF_POOLSEL_SHIFT + vf);
5315
5316 /* Find the vlan filter for this id */
5317 for (i = 0; i < E1000_VLVF_ARRAY_SIZE; i++) {
5318 reg = rd32(E1000_VLVF(i));
5319
5320 /* remove the vf from the pool */
5321 reg &= ~pool_mask;
5322
5323 /* if pool is empty then remove entry from vfta */
5324 if (!(reg & E1000_VLVF_POOLSEL_MASK) &&
5325 (reg & E1000_VLVF_VLANID_ENABLE)) {
5326 reg = 0;
5327 vid = reg & E1000_VLVF_VLANID_MASK;
5328 igb_vfta_set(hw, vid, false);
5329 }
5330
5331 wr32(E1000_VLVF(i), reg);
5332 }
Alexander Duyckae641bd2009-09-03 14:49:33 +00005333
5334 adapter->vf_data[vf].vlans_enabled = 0;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005335}
5336
5337static s32 igb_vlvf_set(struct igb_adapter *adapter, u32 vid, bool add, u32 vf)
5338{
5339 struct e1000_hw *hw = &adapter->hw;
5340 u32 reg, i;
5341
Alexander Duyck51466232009-10-27 23:47:35 +00005342 /* The vlvf table only exists on 82576 hardware and newer */
5343 if (hw->mac.type < e1000_82576)
5344 return -1;
5345
5346 /* we only need to do this if VMDq is enabled */
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005347 if (!adapter->vfs_allocated_count)
5348 return -1;
5349
5350 /* Find the vlan filter for this id */
5351 for (i = 0; i < E1000_VLVF_ARRAY_SIZE; i++) {
5352 reg = rd32(E1000_VLVF(i));
5353 if ((reg & E1000_VLVF_VLANID_ENABLE) &&
5354 vid == (reg & E1000_VLVF_VLANID_MASK))
5355 break;
5356 }
5357
5358 if (add) {
5359 if (i == E1000_VLVF_ARRAY_SIZE) {
5360 /* Did not find a matching VLAN ID entry that was
5361 * enabled. Search for a free filter entry, i.e.
5362 * one without the enable bit set
5363 */
5364 for (i = 0; i < E1000_VLVF_ARRAY_SIZE; i++) {
5365 reg = rd32(E1000_VLVF(i));
5366 if (!(reg & E1000_VLVF_VLANID_ENABLE))
5367 break;
5368 }
5369 }
5370 if (i < E1000_VLVF_ARRAY_SIZE) {
5371 /* Found an enabled/available entry */
5372 reg |= 1 << (E1000_VLVF_POOLSEL_SHIFT + vf);
5373
5374 /* if !enabled we need to set this up in vfta */
5375 if (!(reg & E1000_VLVF_VLANID_ENABLE)) {
Alexander Duyck51466232009-10-27 23:47:35 +00005376 /* add VID to filter table */
5377 igb_vfta_set(hw, vid, true);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005378 reg |= E1000_VLVF_VLANID_ENABLE;
5379 }
Alexander Duyckcad6d052009-03-13 20:41:37 +00005380 reg &= ~E1000_VLVF_VLANID_MASK;
5381 reg |= vid;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005382 wr32(E1000_VLVF(i), reg);
Alexander Duyckae641bd2009-09-03 14:49:33 +00005383
5384 /* do not modify RLPML for PF devices */
5385 if (vf >= adapter->vfs_allocated_count)
5386 return 0;
5387
5388 if (!adapter->vf_data[vf].vlans_enabled) {
5389 u32 size;
5390 reg = rd32(E1000_VMOLR(vf));
5391 size = reg & E1000_VMOLR_RLPML_MASK;
5392 size += 4;
5393 reg &= ~E1000_VMOLR_RLPML_MASK;
5394 reg |= size;
5395 wr32(E1000_VMOLR(vf), reg);
5396 }
Alexander Duyckae641bd2009-09-03 14:49:33 +00005397
Alexander Duyck51466232009-10-27 23:47:35 +00005398 adapter->vf_data[vf].vlans_enabled++;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005399 }
5400 } else {
5401 if (i < E1000_VLVF_ARRAY_SIZE) {
5402 /* remove vf from the pool */
5403 reg &= ~(1 << (E1000_VLVF_POOLSEL_SHIFT + vf));
5404 /* if pool is empty then remove entry from vfta */
5405 if (!(reg & E1000_VLVF_POOLSEL_MASK)) {
5406 reg = 0;
5407 igb_vfta_set(hw, vid, false);
5408 }
5409 wr32(E1000_VLVF(i), reg);
Alexander Duyckae641bd2009-09-03 14:49:33 +00005410
5411 /* do not modify RLPML for PF devices */
5412 if (vf >= adapter->vfs_allocated_count)
5413 return 0;
5414
5415 adapter->vf_data[vf].vlans_enabled--;
5416 if (!adapter->vf_data[vf].vlans_enabled) {
5417 u32 size;
5418 reg = rd32(E1000_VMOLR(vf));
5419 size = reg & E1000_VMOLR_RLPML_MASK;
5420 size -= 4;
5421 reg &= ~E1000_VMOLR_RLPML_MASK;
5422 reg |= size;
5423 wr32(E1000_VMOLR(vf), reg);
5424 }
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005425 }
5426 }
Williams, Mitch A8151d292010-02-10 01:44:24 +00005427 return 0;
5428}
5429
5430static void igb_set_vmvir(struct igb_adapter *adapter, u32 vid, u32 vf)
5431{
5432 struct e1000_hw *hw = &adapter->hw;
5433
5434 if (vid)
5435 wr32(E1000_VMVIR(vf), (vid | E1000_VMVIR_VLANA_DEFAULT));
5436 else
5437 wr32(E1000_VMVIR(vf), 0);
5438}
5439
5440static int igb_ndo_set_vf_vlan(struct net_device *netdev,
5441 int vf, u16 vlan, u8 qos)
5442{
5443 int err = 0;
5444 struct igb_adapter *adapter = netdev_priv(netdev);
5445
5446 if ((vf >= adapter->vfs_allocated_count) || (vlan > 4095) || (qos > 7))
5447 return -EINVAL;
5448 if (vlan || qos) {
5449 err = igb_vlvf_set(adapter, vlan, !!vlan, vf);
5450 if (err)
5451 goto out;
5452 igb_set_vmvir(adapter, vlan | (qos << VLAN_PRIO_SHIFT), vf);
5453 igb_set_vmolr(adapter, vf, !vlan);
5454 adapter->vf_data[vf].pf_vlan = vlan;
5455 adapter->vf_data[vf].pf_qos = qos;
5456 dev_info(&adapter->pdev->dev,
5457 "Setting VLAN %d, QOS 0x%x on VF %d\n", vlan, qos, vf);
5458 if (test_bit(__IGB_DOWN, &adapter->state)) {
5459 dev_warn(&adapter->pdev->dev,
5460 "The VF VLAN has been set,"
5461 " but the PF device is not up.\n");
5462 dev_warn(&adapter->pdev->dev,
5463 "Bring the PF device up before"
5464 " attempting to use the VF device.\n");
5465 }
5466 } else {
5467 igb_vlvf_set(adapter, adapter->vf_data[vf].pf_vlan,
5468 false, vf);
5469 igb_set_vmvir(adapter, vlan, vf);
5470 igb_set_vmolr(adapter, vf, true);
5471 adapter->vf_data[vf].pf_vlan = 0;
5472 adapter->vf_data[vf].pf_qos = 0;
5473 }
5474out:
5475 return err;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005476}
5477
5478static int igb_set_vf_vlan(struct igb_adapter *adapter, u32 *msgbuf, u32 vf)
5479{
5480 int add = (msgbuf[0] & E1000_VT_MSGINFO_MASK) >> E1000_VT_MSGINFO_SHIFT;
5481 int vid = (msgbuf[1] & E1000_VLVF_VLANID_MASK);
5482
5483 return igb_vlvf_set(adapter, vid, add, vf);
5484}
5485
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005486static inline void igb_vf_reset(struct igb_adapter *adapter, u32 vf)
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005487{
Greg Rose8fa7e0f2010-11-06 05:43:21 +00005488 /* clear flags - except flag that indicates PF has set the MAC */
5489 adapter->vf_data[vf].flags &= IGB_VF_FLAG_PF_SET_MAC;
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005490 adapter->vf_data[vf].last_nack = jiffies;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005491
5492 /* reset offloads to defaults */
Williams, Mitch A8151d292010-02-10 01:44:24 +00005493 igb_set_vmolr(adapter, vf, true);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005494
5495 /* reset vlans for device */
5496 igb_clear_vf_vfta(adapter, vf);
Williams, Mitch A8151d292010-02-10 01:44:24 +00005497 if (adapter->vf_data[vf].pf_vlan)
5498 igb_ndo_set_vf_vlan(adapter->netdev, vf,
5499 adapter->vf_data[vf].pf_vlan,
5500 adapter->vf_data[vf].pf_qos);
5501 else
5502 igb_clear_vf_vfta(adapter, vf);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005503
5504 /* reset multicast table array for vf */
5505 adapter->vf_data[vf].num_vf_mc_hashes = 0;
5506
5507 /* Flush and reset the mta with the new values */
Alexander Duyckff41f8d2009-09-03 14:48:56 +00005508 igb_set_rx_mode(adapter->netdev);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005509}
5510
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005511static void igb_vf_reset_event(struct igb_adapter *adapter, u32 vf)
5512{
5513 unsigned char *vf_mac = adapter->vf_data[vf].vf_mac_addresses;
5514
5515 /* generate a new mac address as we were hotplug removed/added */
Williams, Mitch A8151d292010-02-10 01:44:24 +00005516 if (!(adapter->vf_data[vf].flags & IGB_VF_FLAG_PF_SET_MAC))
Joe Perches7efd26d2012-07-12 19:33:06 +00005517 eth_random_addr(vf_mac);
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005518
5519 /* process remaining reset events */
5520 igb_vf_reset(adapter, vf);
5521}
5522
5523static void igb_vf_reset_msg(struct igb_adapter *adapter, u32 vf)
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005524{
5525 struct e1000_hw *hw = &adapter->hw;
5526 unsigned char *vf_mac = adapter->vf_data[vf].vf_mac_addresses;
Alexander Duyckff41f8d2009-09-03 14:48:56 +00005527 int rar_entry = hw->mac.rar_entry_count - (vf + 1);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005528 u32 reg, msgbuf[3];
5529 u8 *addr = (u8 *)(&msgbuf[1]);
5530
5531 /* process all the same items cleared in a function level reset */
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005532 igb_vf_reset(adapter, vf);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005533
5534 /* set vf mac address */
Alexander Duyck26ad9172009-10-05 06:32:49 +00005535 igb_rar_set_qsel(adapter, vf_mac, rar_entry, vf);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005536
5537 /* enable transmit and receive for vf */
5538 reg = rd32(E1000_VFTE);
5539 wr32(E1000_VFTE, reg | (1 << vf));
5540 reg = rd32(E1000_VFRE);
5541 wr32(E1000_VFRE, reg | (1 << vf));
5542
Greg Rose8fa7e0f2010-11-06 05:43:21 +00005543 adapter->vf_data[vf].flags |= IGB_VF_FLAG_CTS;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005544
5545 /* reply to reset with ack and vf mac address */
5546 msgbuf[0] = E1000_VF_RESET | E1000_VT_MSGTYPE_ACK;
5547 memcpy(addr, vf_mac, 6);
5548 igb_write_mbx(hw, msgbuf, 3, vf);
5549}
5550
5551static int igb_set_vf_mac_addr(struct igb_adapter *adapter, u32 *msg, int vf)
5552{
Greg Rosede42edd2010-07-01 13:39:23 +00005553 /*
5554 * The VF MAC Address is stored in a packed array of bytes
5555 * starting at the second 32 bit word of the msg array
5556 */
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005557 unsigned char *addr = (char *)&msg[1];
5558 int err = -1;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005559
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005560 if (is_valid_ether_addr(addr))
5561 err = igb_set_vf_mac(adapter, vf, addr);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005562
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005563 return err;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005564}
5565
5566static void igb_rcv_ack_from_vf(struct igb_adapter *adapter, u32 vf)
5567{
5568 struct e1000_hw *hw = &adapter->hw;
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005569 struct vf_data_storage *vf_data = &adapter->vf_data[vf];
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005570 u32 msg = E1000_VT_MSGTYPE_NACK;
5571
5572 /* if device isn't clear to send it shouldn't be reading either */
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005573 if (!(vf_data->flags & IGB_VF_FLAG_CTS) &&
5574 time_after(jiffies, vf_data->last_nack + (2 * HZ))) {
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005575 igb_write_mbx(hw, &msg, 1, vf);
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005576 vf_data->last_nack = jiffies;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005577 }
5578}
5579
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005580static void igb_rcv_msg_from_vf(struct igb_adapter *adapter, u32 vf)
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005581{
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005582 struct pci_dev *pdev = adapter->pdev;
5583 u32 msgbuf[E1000_VFMAILBOX_SIZE];
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005584 struct e1000_hw *hw = &adapter->hw;
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005585 struct vf_data_storage *vf_data = &adapter->vf_data[vf];
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005586 s32 retval;
5587
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005588 retval = igb_read_mbx(hw, msgbuf, E1000_VFMAILBOX_SIZE, vf);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005589
Alexander Duyckfef45f42009-12-11 22:57:34 -08005590 if (retval) {
5591 /* if receive failed revoke VF CTS stats and restart init */
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005592 dev_err(&pdev->dev, "Error receiving message from VF\n");
Alexander Duyckfef45f42009-12-11 22:57:34 -08005593 vf_data->flags &= ~IGB_VF_FLAG_CTS;
5594 if (!time_after(jiffies, vf_data->last_nack + (2 * HZ)))
5595 return;
5596 goto out;
5597 }
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005598
5599 /* this is a message we already processed, do nothing */
5600 if (msgbuf[0] & (E1000_VT_MSGTYPE_ACK | E1000_VT_MSGTYPE_NACK))
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005601 return;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005602
5603 /*
5604 * until the vf completes a reset it should not be
5605 * allowed to start any configuration.
5606 */
5607
5608 if (msgbuf[0] == E1000_VF_RESET) {
5609 igb_vf_reset_msg(adapter, vf);
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005610 return;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005611 }
5612
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005613 if (!(vf_data->flags & IGB_VF_FLAG_CTS)) {
Alexander Duyckfef45f42009-12-11 22:57:34 -08005614 if (!time_after(jiffies, vf_data->last_nack + (2 * HZ)))
5615 return;
5616 retval = -1;
5617 goto out;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005618 }
5619
5620 switch ((msgbuf[0] & 0xFFFF)) {
5621 case E1000_VF_SET_MAC_ADDR:
Greg Rosea6b5ea32010-11-06 05:42:59 +00005622 retval = -EINVAL;
5623 if (!(vf_data->flags & IGB_VF_FLAG_PF_SET_MAC))
5624 retval = igb_set_vf_mac_addr(adapter, msgbuf, vf);
5625 else
5626 dev_warn(&pdev->dev,
5627 "VF %d attempted to override administratively "
5628 "set MAC address\nReload the VF driver to "
5629 "resume operations\n", vf);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005630 break;
Alexander Duyck7d5753f2009-10-27 23:47:16 +00005631 case E1000_VF_SET_PROMISC:
5632 retval = igb_set_vf_promisc(adapter, msgbuf, vf);
5633 break;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005634 case E1000_VF_SET_MULTICAST:
5635 retval = igb_set_vf_multicasts(adapter, msgbuf, vf);
5636 break;
5637 case E1000_VF_SET_LPE:
5638 retval = igb_set_vf_rlpml(adapter, msgbuf[1], vf);
5639 break;
5640 case E1000_VF_SET_VLAN:
Greg Rosea6b5ea32010-11-06 05:42:59 +00005641 retval = -1;
5642 if (vf_data->pf_vlan)
5643 dev_warn(&pdev->dev,
5644 "VF %d attempted to override administratively "
5645 "set VLAN tag\nReload the VF driver to "
5646 "resume operations\n", vf);
Williams, Mitch A8151d292010-02-10 01:44:24 +00005647 else
5648 retval = igb_set_vf_vlan(adapter, msgbuf, vf);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005649 break;
5650 default:
Alexander Duyck090b1792009-10-27 23:51:55 +00005651 dev_err(&pdev->dev, "Unhandled Msg %08x\n", msgbuf[0]);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005652 retval = -1;
5653 break;
5654 }
5655
Alexander Duyckfef45f42009-12-11 22:57:34 -08005656 msgbuf[0] |= E1000_VT_MSGTYPE_CTS;
5657out:
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005658 /* notify the VF of the results of what it sent us */
5659 if (retval)
5660 msgbuf[0] |= E1000_VT_MSGTYPE_NACK;
5661 else
5662 msgbuf[0] |= E1000_VT_MSGTYPE_ACK;
5663
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005664 igb_write_mbx(hw, msgbuf, 1, vf);
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005665}
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005666
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005667static void igb_msg_task(struct igb_adapter *adapter)
5668{
5669 struct e1000_hw *hw = &adapter->hw;
5670 u32 vf;
5671
5672 for (vf = 0; vf < adapter->vfs_allocated_count; vf++) {
5673 /* process any reset requests */
5674 if (!igb_check_for_rst(hw, vf))
5675 igb_vf_reset_event(adapter, vf);
5676
5677 /* process any messages pending */
5678 if (!igb_check_for_msg(hw, vf))
5679 igb_rcv_msg_from_vf(adapter, vf);
5680
5681 /* process any acks */
5682 if (!igb_check_for_ack(hw, vf))
5683 igb_rcv_ack_from_vf(adapter, vf);
5684 }
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005685}
5686
Auke Kok9d5c8242008-01-24 02:22:38 -08005687/**
Alexander Duyck68d480c2009-10-05 06:33:08 +00005688 * igb_set_uta - Set unicast filter table address
5689 * @adapter: board private structure
5690 *
5691 * The unicast table address is a register array of 32-bit registers.
5692 * The table is meant to be used in a way similar to how the MTA is used
5693 * however due to certain limitations in the hardware it is necessary to
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005694 * set all the hash bits to 1 and use the VMOLR ROPE bit as a promiscuous
5695 * enable bit to allow vlan tag stripping when promiscuous mode is enabled
Alexander Duyck68d480c2009-10-05 06:33:08 +00005696 **/
5697static void igb_set_uta(struct igb_adapter *adapter)
5698{
5699 struct e1000_hw *hw = &adapter->hw;
5700 int i;
5701
5702 /* The UTA table only exists on 82576 hardware and newer */
5703 if (hw->mac.type < e1000_82576)
5704 return;
5705
5706 /* we only need to do this if VMDq is enabled */
5707 if (!adapter->vfs_allocated_count)
5708 return;
5709
5710 for (i = 0; i < hw->mac.uta_reg_count; i++)
5711 array_wr32(E1000_UTA, i, ~0);
5712}
5713
5714/**
Auke Kok9d5c8242008-01-24 02:22:38 -08005715 * igb_intr_msi - Interrupt Handler
5716 * @irq: interrupt number
5717 * @data: pointer to a network interface device structure
5718 **/
5719static irqreturn_t igb_intr_msi(int irq, void *data)
5720{
Alexander Duyck047e0032009-10-27 15:49:27 +00005721 struct igb_adapter *adapter = data;
5722 struct igb_q_vector *q_vector = adapter->q_vector[0];
Auke Kok9d5c8242008-01-24 02:22:38 -08005723 struct e1000_hw *hw = &adapter->hw;
5724 /* read ICR disables interrupts using IAM */
5725 u32 icr = rd32(E1000_ICR);
5726
Alexander Duyck047e0032009-10-27 15:49:27 +00005727 igb_write_itr(q_vector);
Auke Kok9d5c8242008-01-24 02:22:38 -08005728
Alexander Duyck7f081d42010-01-07 17:41:00 +00005729 if (icr & E1000_ICR_DRSTA)
5730 schedule_work(&adapter->reset_task);
5731
Alexander Duyck047e0032009-10-27 15:49:27 +00005732 if (icr & E1000_ICR_DOUTSYNC) {
Alexander Duyckdda0e082009-02-06 23:19:08 +00005733 /* HW is reporting DMA is out of sync */
5734 adapter->stats.doosync++;
5735 }
5736
Auke Kok9d5c8242008-01-24 02:22:38 -08005737 if (icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) {
5738 hw->mac.get_link_status = 1;
5739 if (!test_bit(__IGB_DOWN, &adapter->state))
5740 mod_timer(&adapter->watchdog_timer, jiffies + 1);
5741 }
5742
Matthew Vick1f6e8172012-08-18 07:26:33 +00005743 if (icr & E1000_ICR_TS) {
5744 u32 tsicr = rd32(E1000_TSICR);
5745
5746 if (tsicr & E1000_TSICR_TXTS) {
5747 /* acknowledge the interrupt */
5748 wr32(E1000_TSICR, E1000_TSICR_TXTS);
5749 /* retrieve hardware timestamp */
5750 schedule_work(&adapter->ptp_tx_work);
5751 }
5752 }
Matthew Vick1f6e8172012-08-18 07:26:33 +00005753
Alexander Duyck047e0032009-10-27 15:49:27 +00005754 napi_schedule(&q_vector->napi);
Auke Kok9d5c8242008-01-24 02:22:38 -08005755
5756 return IRQ_HANDLED;
5757}
5758
5759/**
Alexander Duyck4a3c6432009-02-06 23:20:49 +00005760 * igb_intr - Legacy Interrupt Handler
Auke Kok9d5c8242008-01-24 02:22:38 -08005761 * @irq: interrupt number
5762 * @data: pointer to a network interface device structure
5763 **/
5764static irqreturn_t igb_intr(int irq, void *data)
5765{
Alexander Duyck047e0032009-10-27 15:49:27 +00005766 struct igb_adapter *adapter = data;
5767 struct igb_q_vector *q_vector = adapter->q_vector[0];
Auke Kok9d5c8242008-01-24 02:22:38 -08005768 struct e1000_hw *hw = &adapter->hw;
5769 /* Interrupt Auto-Mask...upon reading ICR, interrupts are masked. No
5770 * need for the IMC write */
5771 u32 icr = rd32(E1000_ICR);
Auke Kok9d5c8242008-01-24 02:22:38 -08005772
5773 /* IMS will not auto-mask if INT_ASSERTED is not set, and if it is
5774 * not set, then the adapter didn't send an interrupt */
5775 if (!(icr & E1000_ICR_INT_ASSERTED))
5776 return IRQ_NONE;
5777
Alexander Duyck0ba82992011-08-26 07:45:47 +00005778 igb_write_itr(q_vector);
5779
Alexander Duyck7f081d42010-01-07 17:41:00 +00005780 if (icr & E1000_ICR_DRSTA)
5781 schedule_work(&adapter->reset_task);
5782
Alexander Duyck047e0032009-10-27 15:49:27 +00005783 if (icr & E1000_ICR_DOUTSYNC) {
Alexander Duyckdda0e082009-02-06 23:19:08 +00005784 /* HW is reporting DMA is out of sync */
5785 adapter->stats.doosync++;
5786 }
5787
Auke Kok9d5c8242008-01-24 02:22:38 -08005788 if (icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) {
5789 hw->mac.get_link_status = 1;
5790 /* guard against interrupt when we're going down */
5791 if (!test_bit(__IGB_DOWN, &adapter->state))
5792 mod_timer(&adapter->watchdog_timer, jiffies + 1);
5793 }
5794
Matthew Vick1f6e8172012-08-18 07:26:33 +00005795 if (icr & E1000_ICR_TS) {
5796 u32 tsicr = rd32(E1000_TSICR);
5797
5798 if (tsicr & E1000_TSICR_TXTS) {
5799 /* acknowledge the interrupt */
5800 wr32(E1000_TSICR, E1000_TSICR_TXTS);
5801 /* retrieve hardware timestamp */
5802 schedule_work(&adapter->ptp_tx_work);
5803 }
5804 }
Matthew Vick1f6e8172012-08-18 07:26:33 +00005805
Alexander Duyck047e0032009-10-27 15:49:27 +00005806 napi_schedule(&q_vector->napi);
Auke Kok9d5c8242008-01-24 02:22:38 -08005807
5808 return IRQ_HANDLED;
5809}
5810
Stephen Hemmingerc50b52a2012-01-18 22:13:26 +00005811static void igb_ring_irq_enable(struct igb_q_vector *q_vector)
Alexander Duyck46544252009-02-19 20:39:04 -08005812{
Alexander Duyck047e0032009-10-27 15:49:27 +00005813 struct igb_adapter *adapter = q_vector->adapter;
Alexander Duyck46544252009-02-19 20:39:04 -08005814 struct e1000_hw *hw = &adapter->hw;
5815
Alexander Duyck0ba82992011-08-26 07:45:47 +00005816 if ((q_vector->rx.ring && (adapter->rx_itr_setting & 3)) ||
5817 (!q_vector->rx.ring && (adapter->tx_itr_setting & 3))) {
5818 if ((adapter->num_q_vectors == 1) && !adapter->vf_data)
5819 igb_set_itr(q_vector);
Alexander Duyck46544252009-02-19 20:39:04 -08005820 else
Alexander Duyck047e0032009-10-27 15:49:27 +00005821 igb_update_ring_itr(q_vector);
Alexander Duyck46544252009-02-19 20:39:04 -08005822 }
5823
5824 if (!test_bit(__IGB_DOWN, &adapter->state)) {
5825 if (adapter->msix_entries)
Alexander Duyck047e0032009-10-27 15:49:27 +00005826 wr32(E1000_EIMS, q_vector->eims_value);
Alexander Duyck46544252009-02-19 20:39:04 -08005827 else
5828 igb_irq_enable(adapter);
5829 }
5830}
5831
Auke Kok9d5c8242008-01-24 02:22:38 -08005832/**
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07005833 * igb_poll - NAPI Rx polling callback
5834 * @napi: napi polling structure
5835 * @budget: count of how many packets we should handle
Auke Kok9d5c8242008-01-24 02:22:38 -08005836 **/
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07005837static int igb_poll(struct napi_struct *napi, int budget)
Auke Kok9d5c8242008-01-24 02:22:38 -08005838{
Alexander Duyck047e0032009-10-27 15:49:27 +00005839 struct igb_q_vector *q_vector = container_of(napi,
5840 struct igb_q_vector,
5841 napi);
Alexander Duyck16eb8812011-08-26 07:43:54 +00005842 bool clean_complete = true;
Auke Kok9d5c8242008-01-24 02:22:38 -08005843
Jeff Kirsher421e02f2008-10-17 11:08:31 -07005844#ifdef CONFIG_IGB_DCA
Alexander Duyck047e0032009-10-27 15:49:27 +00005845 if (q_vector->adapter->flags & IGB_FLAG_DCA_ENABLED)
5846 igb_update_dca(q_vector);
Jeb Cramerfe4506b2008-07-08 15:07:55 -07005847#endif
Alexander Duyck0ba82992011-08-26 07:45:47 +00005848 if (q_vector->tx.ring)
Alexander Duyck13fde972011-10-05 13:35:24 +00005849 clean_complete = igb_clean_tx_irq(q_vector);
Auke Kok9d5c8242008-01-24 02:22:38 -08005850
Alexander Duyck0ba82992011-08-26 07:45:47 +00005851 if (q_vector->rx.ring)
Alexander Duyckcd392f52011-08-26 07:43:59 +00005852 clean_complete &= igb_clean_rx_irq(q_vector, budget);
Alexander Duyck047e0032009-10-27 15:49:27 +00005853
Alexander Duyck16eb8812011-08-26 07:43:54 +00005854 /* If all work not completed, return budget and keep polling */
5855 if (!clean_complete)
5856 return budget;
Auke Kok9d5c8242008-01-24 02:22:38 -08005857
Alexander Duyck46544252009-02-19 20:39:04 -08005858 /* If not enough Rx work done, exit the polling mode */
Alexander Duyck16eb8812011-08-26 07:43:54 +00005859 napi_complete(napi);
5860 igb_ring_irq_enable(q_vector);
Alexander Duyck46544252009-02-19 20:39:04 -08005861
Alexander Duyck16eb8812011-08-26 07:43:54 +00005862 return 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08005863}
Al Viro6d8126f2008-03-16 22:23:24 +00005864
Patrick Ohly33af6bc2009-02-12 05:03:43 +00005865/**
Auke Kok9d5c8242008-01-24 02:22:38 -08005866 * igb_clean_tx_irq - Reclaim resources after transmit completes
Alexander Duyck047e0032009-10-27 15:49:27 +00005867 * @q_vector: pointer to q_vector containing needed info
Ben Hutchings49ce9c22012-07-10 10:56:00 +00005868 *
Auke Kok9d5c8242008-01-24 02:22:38 -08005869 * returns true if ring is completely cleaned
5870 **/
Alexander Duyck047e0032009-10-27 15:49:27 +00005871static bool igb_clean_tx_irq(struct igb_q_vector *q_vector)
Auke Kok9d5c8242008-01-24 02:22:38 -08005872{
Alexander Duyck047e0032009-10-27 15:49:27 +00005873 struct igb_adapter *adapter = q_vector->adapter;
Alexander Duyck0ba82992011-08-26 07:45:47 +00005874 struct igb_ring *tx_ring = q_vector->tx.ring;
Alexander Duyck06034642011-08-26 07:44:22 +00005875 struct igb_tx_buffer *tx_buffer;
Alexander Duyckf4128782012-09-13 06:28:01 +00005876 union e1000_adv_tx_desc *tx_desc;
Auke Kok9d5c8242008-01-24 02:22:38 -08005877 unsigned int total_bytes = 0, total_packets = 0;
Alexander Duyck0ba82992011-08-26 07:45:47 +00005878 unsigned int budget = q_vector->tx.work_limit;
Alexander Duyck8542db02011-08-26 07:44:43 +00005879 unsigned int i = tx_ring->next_to_clean;
Auke Kok9d5c8242008-01-24 02:22:38 -08005880
Alexander Duyck13fde972011-10-05 13:35:24 +00005881 if (test_bit(__IGB_DOWN, &adapter->state))
5882 return true;
Alexander Duyck0e014cb2008-12-26 01:33:18 -08005883
Alexander Duyck06034642011-08-26 07:44:22 +00005884 tx_buffer = &tx_ring->tx_buffer_info[i];
Alexander Duyck13fde972011-10-05 13:35:24 +00005885 tx_desc = IGB_TX_DESC(tx_ring, i);
Alexander Duyck8542db02011-08-26 07:44:43 +00005886 i -= tx_ring->count;
Auke Kok9d5c8242008-01-24 02:22:38 -08005887
Alexander Duyckf4128782012-09-13 06:28:01 +00005888 do {
5889 union e1000_adv_tx_desc *eop_desc = tx_buffer->next_to_watch;
Alexander Duyck8542db02011-08-26 07:44:43 +00005890
5891 /* if next_to_watch is not set then there is no work pending */
5892 if (!eop_desc)
5893 break;
Alexander Duyck13fde972011-10-05 13:35:24 +00005894
Alexander Duyckf4128782012-09-13 06:28:01 +00005895 /* prevent any other reads prior to eop_desc */
5896 rmb();
5897
Alexander Duyck13fde972011-10-05 13:35:24 +00005898 /* if DD is not set pending work has not been completed */
5899 if (!(eop_desc->wb.status & cpu_to_le32(E1000_TXD_STAT_DD)))
5900 break;
5901
Alexander Duyck8542db02011-08-26 07:44:43 +00005902 /* clear next_to_watch to prevent false hangs */
5903 tx_buffer->next_to_watch = NULL;
Alexander Duyck13fde972011-10-05 13:35:24 +00005904
Alexander Duyckebe42d12011-08-26 07:45:09 +00005905 /* update the statistics for this packet */
5906 total_bytes += tx_buffer->bytecount;
5907 total_packets += tx_buffer->gso_segs;
Alexander Duyck13fde972011-10-05 13:35:24 +00005908
Alexander Duyckebe42d12011-08-26 07:45:09 +00005909 /* free the skb */
5910 dev_kfree_skb_any(tx_buffer->skb);
Alexander Duyckebe42d12011-08-26 07:45:09 +00005911
5912 /* unmap skb header data */
5913 dma_unmap_single(tx_ring->dev,
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00005914 dma_unmap_addr(tx_buffer, dma),
5915 dma_unmap_len(tx_buffer, len),
Alexander Duyckebe42d12011-08-26 07:45:09 +00005916 DMA_TO_DEVICE);
5917
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00005918 /* clear tx_buffer data */
5919 tx_buffer->skb = NULL;
5920 dma_unmap_len_set(tx_buffer, len, 0);
5921
Alexander Duyckebe42d12011-08-26 07:45:09 +00005922 /* clear last DMA location and unmap remaining buffers */
5923 while (tx_desc != eop_desc) {
Alexander Duyck13fde972011-10-05 13:35:24 +00005924 tx_buffer++;
5925 tx_desc++;
Auke Kok9d5c8242008-01-24 02:22:38 -08005926 i++;
Alexander Duyck8542db02011-08-26 07:44:43 +00005927 if (unlikely(!i)) {
5928 i -= tx_ring->count;
Alexander Duyck06034642011-08-26 07:44:22 +00005929 tx_buffer = tx_ring->tx_buffer_info;
Alexander Duyck13fde972011-10-05 13:35:24 +00005930 tx_desc = IGB_TX_DESC(tx_ring, 0);
5931 }
Alexander Duyckebe42d12011-08-26 07:45:09 +00005932
5933 /* unmap any remaining paged data */
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00005934 if (dma_unmap_len(tx_buffer, len)) {
Alexander Duyckebe42d12011-08-26 07:45:09 +00005935 dma_unmap_page(tx_ring->dev,
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00005936 dma_unmap_addr(tx_buffer, dma),
5937 dma_unmap_len(tx_buffer, len),
Alexander Duyckebe42d12011-08-26 07:45:09 +00005938 DMA_TO_DEVICE);
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00005939 dma_unmap_len_set(tx_buffer, len, 0);
Alexander Duyckebe42d12011-08-26 07:45:09 +00005940 }
5941 }
5942
Alexander Duyckebe42d12011-08-26 07:45:09 +00005943 /* move us one more past the eop_desc for start of next pkt */
5944 tx_buffer++;
5945 tx_desc++;
5946 i++;
5947 if (unlikely(!i)) {
5948 i -= tx_ring->count;
5949 tx_buffer = tx_ring->tx_buffer_info;
5950 tx_desc = IGB_TX_DESC(tx_ring, 0);
5951 }
Alexander Duyckf4128782012-09-13 06:28:01 +00005952
5953 /* issue prefetch for next Tx descriptor */
5954 prefetch(tx_desc);
5955
5956 /* update budget accounting */
5957 budget--;
5958 } while (likely(budget));
Alexander Duyck0e014cb2008-12-26 01:33:18 -08005959
Eric Dumazetbdbc0632012-01-04 20:23:36 +00005960 netdev_tx_completed_queue(txring_txq(tx_ring),
5961 total_packets, total_bytes);
Alexander Duyck8542db02011-08-26 07:44:43 +00005962 i += tx_ring->count;
Auke Kok9d5c8242008-01-24 02:22:38 -08005963 tx_ring->next_to_clean = i;
Alexander Duyck13fde972011-10-05 13:35:24 +00005964 u64_stats_update_begin(&tx_ring->tx_syncp);
5965 tx_ring->tx_stats.bytes += total_bytes;
5966 tx_ring->tx_stats.packets += total_packets;
5967 u64_stats_update_end(&tx_ring->tx_syncp);
Alexander Duyck0ba82992011-08-26 07:45:47 +00005968 q_vector->tx.total_bytes += total_bytes;
5969 q_vector->tx.total_packets += total_packets;
Auke Kok9d5c8242008-01-24 02:22:38 -08005970
Alexander Duyck6d095fa2011-08-26 07:46:19 +00005971 if (test_bit(IGB_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags)) {
Alexander Duyck13fde972011-10-05 13:35:24 +00005972 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck13fde972011-10-05 13:35:24 +00005973
Auke Kok9d5c8242008-01-24 02:22:38 -08005974 /* Detect a transmit hang in hardware, this serializes the
5975 * check with the clearing of time_stamp and movement of i */
Alexander Duyck6d095fa2011-08-26 07:46:19 +00005976 clear_bit(IGB_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags);
Alexander Duyckf4128782012-09-13 06:28:01 +00005977 if (tx_buffer->next_to_watch &&
Alexander Duyck8542db02011-08-26 07:44:43 +00005978 time_after(jiffies, tx_buffer->time_stamp +
Joe Perches8e95a202009-12-03 07:58:21 +00005979 (adapter->tx_timeout_factor * HZ)) &&
5980 !(rd32(E1000_STATUS) & E1000_STATUS_TXOFF)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08005981
Auke Kok9d5c8242008-01-24 02:22:38 -08005982 /* detected Tx unit hang */
Alexander Duyck59d71982010-04-27 13:09:25 +00005983 dev_err(tx_ring->dev,
Auke Kok9d5c8242008-01-24 02:22:38 -08005984 "Detected Tx Unit Hang\n"
Alexander Duyck2d064c02008-07-08 15:10:12 -07005985 " Tx Queue <%d>\n"
Auke Kok9d5c8242008-01-24 02:22:38 -08005986 " TDH <%x>\n"
5987 " TDT <%x>\n"
5988 " next_to_use <%x>\n"
5989 " next_to_clean <%x>\n"
Auke Kok9d5c8242008-01-24 02:22:38 -08005990 "buffer_info[next_to_clean]\n"
5991 " time_stamp <%lx>\n"
Alexander Duyck8542db02011-08-26 07:44:43 +00005992 " next_to_watch <%p>\n"
Auke Kok9d5c8242008-01-24 02:22:38 -08005993 " jiffies <%lx>\n"
5994 " desc.status <%x>\n",
Alexander Duyck2d064c02008-07-08 15:10:12 -07005995 tx_ring->queue_index,
Alexander Duyck238ac812011-08-26 07:43:48 +00005996 rd32(E1000_TDH(tx_ring->reg_idx)),
Alexander Duyckfce99e32009-10-27 15:51:27 +00005997 readl(tx_ring->tail),
Auke Kok9d5c8242008-01-24 02:22:38 -08005998 tx_ring->next_to_use,
5999 tx_ring->next_to_clean,
Alexander Duyck8542db02011-08-26 07:44:43 +00006000 tx_buffer->time_stamp,
Alexander Duyckf4128782012-09-13 06:28:01 +00006001 tx_buffer->next_to_watch,
Auke Kok9d5c8242008-01-24 02:22:38 -08006002 jiffies,
Alexander Duyckf4128782012-09-13 06:28:01 +00006003 tx_buffer->next_to_watch->wb.status);
Alexander Duyck13fde972011-10-05 13:35:24 +00006004 netif_stop_subqueue(tx_ring->netdev,
6005 tx_ring->queue_index);
6006
6007 /* we are about to reset, no point in enabling stuff */
6008 return true;
Auke Kok9d5c8242008-01-24 02:22:38 -08006009 }
6010 }
Alexander Duyck13fde972011-10-05 13:35:24 +00006011
6012 if (unlikely(total_packets &&
6013 netif_carrier_ok(tx_ring->netdev) &&
6014 igb_desc_unused(tx_ring) >= IGB_TX_QUEUE_WAKE)) {
6015 /* Make sure that anybody stopping the queue after this
6016 * sees the new next_to_clean.
6017 */
6018 smp_mb();
6019 if (__netif_subqueue_stopped(tx_ring->netdev,
6020 tx_ring->queue_index) &&
6021 !(test_bit(__IGB_DOWN, &adapter->state))) {
6022 netif_wake_subqueue(tx_ring->netdev,
6023 tx_ring->queue_index);
6024
6025 u64_stats_update_begin(&tx_ring->tx_syncp);
6026 tx_ring->tx_stats.restart_queue++;
6027 u64_stats_update_end(&tx_ring->tx_syncp);
6028 }
6029 }
6030
6031 return !!budget;
Auke Kok9d5c8242008-01-24 02:22:38 -08006032}
6033
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006034/**
6035 * igb_reuse_rx_page - page flip buffer and store it back on the ring
6036 * @rx_ring: rx descriptor ring to store buffers on
6037 * @old_buff: donor buffer to have page reused
6038 *
6039 * Synchronizes page for reuse by the adapter
6040 **/
6041static void igb_reuse_rx_page(struct igb_ring *rx_ring,
6042 struct igb_rx_buffer *old_buff)
6043{
6044 struct igb_rx_buffer *new_buff;
6045 u16 nta = rx_ring->next_to_alloc;
6046
6047 new_buff = &rx_ring->rx_buffer_info[nta];
6048
6049 /* update, and store next to alloc */
6050 nta++;
6051 rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0;
6052
6053 /* transfer page from old buffer to new buffer */
6054 memcpy(new_buff, old_buff, sizeof(struct igb_rx_buffer));
6055
6056 /* sync the buffer for use by the device */
6057 dma_sync_single_range_for_device(rx_ring->dev, old_buff->dma,
6058 old_buff->page_offset,
Alexander Duyckde78d1f2012-09-25 00:31:12 +00006059 IGB_RX_BUFSZ,
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006060 DMA_FROM_DEVICE);
6061}
6062
6063/**
6064 * igb_add_rx_frag - Add contents of Rx buffer to sk_buff
6065 * @rx_ring: rx descriptor ring to transact packets on
6066 * @rx_buffer: buffer containing page to add
6067 * @rx_desc: descriptor containing length of buffer written by hardware
6068 * @skb: sk_buff to place the data into
6069 *
6070 * This function will add the data contained in rx_buffer->page to the skb.
6071 * This is done either through a direct copy if the data in the buffer is
6072 * less than the skb header size, otherwise it will just attach the page as
6073 * a frag to the skb.
6074 *
6075 * The function will then update the page offset if necessary and return
6076 * true if the buffer can be reused by the adapter.
6077 **/
6078static bool igb_add_rx_frag(struct igb_ring *rx_ring,
6079 struct igb_rx_buffer *rx_buffer,
6080 union e1000_adv_rx_desc *rx_desc,
6081 struct sk_buff *skb)
6082{
6083 struct page *page = rx_buffer->page;
6084 unsigned int size = le16_to_cpu(rx_desc->wb.upper.length);
6085
6086 if ((size <= IGB_RX_HDR_LEN) && !skb_is_nonlinear(skb)) {
6087 unsigned char *va = page_address(page) + rx_buffer->page_offset;
6088
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006089 if (igb_test_staterr(rx_desc, E1000_RXDADV_STAT_TSIP)) {
6090 igb_ptp_rx_pktstamp(rx_ring->q_vector, va, skb);
6091 va += IGB_TS_HDR_LEN;
6092 size -= IGB_TS_HDR_LEN;
6093 }
6094
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006095 memcpy(__skb_put(skb, size), va, ALIGN(size, sizeof(long)));
6096
6097 /* we can reuse buffer as-is, just make sure it is local */
6098 if (likely(page_to_nid(page) == numa_node_id()))
6099 return true;
6100
6101 /* this page cannot be reused so discard it */
6102 put_page(page);
6103 return false;
6104 }
6105
6106 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page,
Alexander Duyckde78d1f2012-09-25 00:31:12 +00006107 rx_buffer->page_offset, size, IGB_RX_BUFSZ);
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006108
6109 /* avoid re-using remote pages */
6110 if (unlikely(page_to_nid(page) != numa_node_id()))
6111 return false;
6112
Alexander Duyckde78d1f2012-09-25 00:31:12 +00006113#if (PAGE_SIZE < 8192)
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006114 /* if we are only owner of page we can reuse it */
6115 if (unlikely(page_count(page) != 1))
6116 return false;
6117
6118 /* flip page offset to other buffer */
Alexander Duyckde78d1f2012-09-25 00:31:12 +00006119 rx_buffer->page_offset ^= IGB_RX_BUFSZ;
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006120
6121 /*
6122 * since we are the only owner of the page and we need to
6123 * increment it, just set the value to 2 in order to avoid
6124 * an unnecessary locked operation
6125 */
6126 atomic_set(&page->_count, 2);
Alexander Duyckde78d1f2012-09-25 00:31:12 +00006127#else
6128 /* move offset up to the next cache line */
6129 rx_buffer->page_offset += SKB_DATA_ALIGN(size);
6130
6131 if (rx_buffer->page_offset > (PAGE_SIZE - IGB_RX_BUFSZ))
6132 return false;
6133
6134 /* bump ref count on page before it is given to the stack */
6135 get_page(page);
6136#endif
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006137
6138 return true;
6139}
6140
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006141static struct sk_buff *igb_fetch_rx_buffer(struct igb_ring *rx_ring,
6142 union e1000_adv_rx_desc *rx_desc,
6143 struct sk_buff *skb)
6144{
6145 struct igb_rx_buffer *rx_buffer;
6146 struct page *page;
6147
6148 rx_buffer = &rx_ring->rx_buffer_info[rx_ring->next_to_clean];
6149
6150 /*
6151 * This memory barrier is needed to keep us from reading
6152 * any other fields out of the rx_desc until we know the
6153 * RXD_STAT_DD bit is set
6154 */
6155 rmb();
6156
6157 page = rx_buffer->page;
6158 prefetchw(page);
6159
6160 if (likely(!skb)) {
6161 void *page_addr = page_address(page) +
6162 rx_buffer->page_offset;
6163
6164 /* prefetch first cache line of first page */
6165 prefetch(page_addr);
6166#if L1_CACHE_BYTES < 128
6167 prefetch(page_addr + L1_CACHE_BYTES);
6168#endif
6169
6170 /* allocate a skb to store the frags */
6171 skb = netdev_alloc_skb_ip_align(rx_ring->netdev,
6172 IGB_RX_HDR_LEN);
6173 if (unlikely(!skb)) {
6174 rx_ring->rx_stats.alloc_failed++;
6175 return NULL;
6176 }
6177
6178 /*
6179 * we will be copying header into skb->data in
6180 * pskb_may_pull so it is in our interest to prefetch
6181 * it now to avoid a possible cache miss
6182 */
6183 prefetchw(skb->data);
6184 }
6185
6186 /* we are reusing so sync this buffer for CPU use */
6187 dma_sync_single_range_for_cpu(rx_ring->dev,
6188 rx_buffer->dma,
6189 rx_buffer->page_offset,
Alexander Duyckde78d1f2012-09-25 00:31:12 +00006190 IGB_RX_BUFSZ,
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006191 DMA_FROM_DEVICE);
6192
6193 /* pull page into skb */
6194 if (igb_add_rx_frag(rx_ring, rx_buffer, rx_desc, skb)) {
6195 /* hand second half of page back to the ring */
6196 igb_reuse_rx_page(rx_ring, rx_buffer);
6197 } else {
6198 /* we are not reusing the buffer so unmap it */
6199 dma_unmap_page(rx_ring->dev, rx_buffer->dma,
6200 PAGE_SIZE, DMA_FROM_DEVICE);
6201 }
6202
6203 /* clear contents of rx_buffer */
6204 rx_buffer->page = NULL;
6205
6206 return skb;
6207}
6208
Alexander Duyckcd392f52011-08-26 07:43:59 +00006209static inline void igb_rx_checksum(struct igb_ring *ring,
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00006210 union e1000_adv_rx_desc *rx_desc,
6211 struct sk_buff *skb)
Auke Kok9d5c8242008-01-24 02:22:38 -08006212{
Eric Dumazetbc8acf22010-09-02 13:07:41 -07006213 skb_checksum_none_assert(skb);
Auke Kok9d5c8242008-01-24 02:22:38 -08006214
Alexander Duyck294e7d72011-08-26 07:45:57 +00006215 /* Ignore Checksum bit is set */
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00006216 if (igb_test_staterr(rx_desc, E1000_RXD_STAT_IXSM))
Alexander Duyck294e7d72011-08-26 07:45:57 +00006217 return;
6218
6219 /* Rx checksum disabled via ethtool */
6220 if (!(ring->netdev->features & NETIF_F_RXCSUM))
Auke Kok9d5c8242008-01-24 02:22:38 -08006221 return;
Alexander Duyck85ad76b2009-10-27 15:52:46 +00006222
Auke Kok9d5c8242008-01-24 02:22:38 -08006223 /* TCP/UDP checksum error bit is set */
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00006224 if (igb_test_staterr(rx_desc,
6225 E1000_RXDEXT_STATERR_TCPE |
6226 E1000_RXDEXT_STATERR_IPE)) {
Jesse Brandeburgb9473562009-04-27 22:36:13 +00006227 /*
6228 * work around errata with sctp packets where the TCPE aka
6229 * L4E bit is set incorrectly on 64 byte (60 byte w/o crc)
6230 * packets, (aka let the stack check the crc32c)
6231 */
Alexander Duyck866cff02011-08-26 07:45:36 +00006232 if (!((skb->len == 60) &&
6233 test_bit(IGB_RING_FLAG_RX_SCTP_CSUM, &ring->flags))) {
Eric Dumazet12dcd862010-10-15 17:27:10 +00006234 u64_stats_update_begin(&ring->rx_syncp);
Alexander Duyck04a5fcaa2009-10-27 15:52:27 +00006235 ring->rx_stats.csum_err++;
Eric Dumazet12dcd862010-10-15 17:27:10 +00006236 u64_stats_update_end(&ring->rx_syncp);
6237 }
Auke Kok9d5c8242008-01-24 02:22:38 -08006238 /* let the stack verify checksum errors */
Auke Kok9d5c8242008-01-24 02:22:38 -08006239 return;
6240 }
6241 /* It must be a TCP or UDP packet with a valid checksum */
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00006242 if (igb_test_staterr(rx_desc, E1000_RXD_STAT_TCPCS |
6243 E1000_RXD_STAT_UDPCS))
Auke Kok9d5c8242008-01-24 02:22:38 -08006244 skb->ip_summed = CHECKSUM_UNNECESSARY;
6245
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00006246 dev_dbg(ring->dev, "cksum success: bits %08X\n",
6247 le32_to_cpu(rx_desc->wb.upper.status_error));
Auke Kok9d5c8242008-01-24 02:22:38 -08006248}
6249
Alexander Duyck077887c2011-08-26 07:46:29 +00006250static inline void igb_rx_hash(struct igb_ring *ring,
6251 union e1000_adv_rx_desc *rx_desc,
6252 struct sk_buff *skb)
6253{
6254 if (ring->netdev->features & NETIF_F_RXHASH)
6255 skb->rxhash = le32_to_cpu(rx_desc->wb.lower.hi_dword.rss);
6256}
6257
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006258/**
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006259 * igb_is_non_eop - process handling of non-EOP buffers
6260 * @rx_ring: Rx ring being processed
6261 * @rx_desc: Rx descriptor for current buffer
6262 * @skb: current socket buffer containing buffer in progress
6263 *
6264 * This function updates next to clean. If the buffer is an EOP buffer
6265 * this function exits returning false, otherwise it will place the
6266 * sk_buff in the next buffer to be chained and return true indicating
6267 * that this is in fact a non-EOP buffer.
6268 **/
6269static bool igb_is_non_eop(struct igb_ring *rx_ring,
6270 union e1000_adv_rx_desc *rx_desc)
6271{
6272 u32 ntc = rx_ring->next_to_clean + 1;
6273
6274 /* fetch, update, and store next to clean */
6275 ntc = (ntc < rx_ring->count) ? ntc : 0;
6276 rx_ring->next_to_clean = ntc;
6277
6278 prefetch(IGB_RX_DESC(rx_ring, ntc));
6279
6280 if (likely(igb_test_staterr(rx_desc, E1000_RXD_STAT_EOP)))
6281 return false;
6282
6283 return true;
6284}
6285
6286/**
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006287 * igb_get_headlen - determine size of header for LRO/GRO
6288 * @data: pointer to the start of the headers
6289 * @max_len: total length of section to find headers in
6290 *
6291 * This function is meant to determine the length of headers that will
6292 * be recognized by hardware for LRO, and GRO offloads. The main
6293 * motivation of doing this is to only perform one pull for IPv4 TCP
6294 * packets so that we can do basic things like calculating the gso_size
6295 * based on the average data per packet.
6296 **/
6297static unsigned int igb_get_headlen(unsigned char *data,
6298 unsigned int max_len)
Alexander Duyck2d94d8a2009-07-23 18:10:06 +00006299{
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006300 union {
6301 unsigned char *network;
6302 /* l2 headers */
6303 struct ethhdr *eth;
6304 struct vlan_hdr *vlan;
6305 /* l3 headers */
6306 struct iphdr *ipv4;
6307 struct ipv6hdr *ipv6;
6308 } hdr;
6309 __be16 protocol;
6310 u8 nexthdr = 0; /* default to not TCP */
6311 u8 hlen;
6312
6313 /* this should never happen, but better safe than sorry */
6314 if (max_len < ETH_HLEN)
6315 return max_len;
6316
6317 /* initialize network frame pointer */
6318 hdr.network = data;
6319
6320 /* set first protocol and move network header forward */
6321 protocol = hdr.eth->h_proto;
6322 hdr.network += ETH_HLEN;
6323
6324 /* handle any vlan tag if present */
6325 if (protocol == __constant_htons(ETH_P_8021Q)) {
6326 if ((hdr.network - data) > (max_len - VLAN_HLEN))
6327 return max_len;
6328
6329 protocol = hdr.vlan->h_vlan_encapsulated_proto;
6330 hdr.network += VLAN_HLEN;
6331 }
6332
6333 /* handle L3 protocols */
6334 if (protocol == __constant_htons(ETH_P_IP)) {
6335 if ((hdr.network - data) > (max_len - sizeof(struct iphdr)))
6336 return max_len;
6337
6338 /* access ihl as a u8 to avoid unaligned access on ia64 */
6339 hlen = (hdr.network[0] & 0x0F) << 2;
6340
6341 /* verify hlen meets minimum size requirements */
6342 if (hlen < sizeof(struct iphdr))
6343 return hdr.network - data;
6344
Alexander Duyckf2fb4ab2012-11-13 01:13:38 +00006345 /* record next protocol if header is present */
6346 if (!hdr.ipv4->frag_off)
6347 nexthdr = hdr.ipv4->protocol;
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006348 } else if (protocol == __constant_htons(ETH_P_IPV6)) {
6349 if ((hdr.network - data) > (max_len - sizeof(struct ipv6hdr)))
6350 return max_len;
6351
6352 /* record next protocol */
6353 nexthdr = hdr.ipv6->nexthdr;
Alexander Duyckf2fb4ab2012-11-13 01:13:38 +00006354 hlen = sizeof(struct ipv6hdr);
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006355 } else {
6356 return hdr.network - data;
6357 }
6358
Alexander Duyckf2fb4ab2012-11-13 01:13:38 +00006359 /* relocate pointer to start of L4 header */
6360 hdr.network += hlen;
6361
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006362 /* finally sort out TCP */
6363 if (nexthdr == IPPROTO_TCP) {
6364 if ((hdr.network - data) > (max_len - sizeof(struct tcphdr)))
6365 return max_len;
6366
6367 /* access doff as a u8 to avoid unaligned access on ia64 */
6368 hlen = (hdr.network[12] & 0xF0) >> 2;
6369
6370 /* verify hlen meets minimum size requirements */
6371 if (hlen < sizeof(struct tcphdr))
6372 return hdr.network - data;
6373
6374 hdr.network += hlen;
6375 } else if (nexthdr == IPPROTO_UDP) {
6376 if ((hdr.network - data) > (max_len - sizeof(struct udphdr)))
6377 return max_len;
6378
6379 hdr.network += sizeof(struct udphdr);
6380 }
6381
6382 /*
6383 * If everything has gone correctly hdr.network should be the
6384 * data section of the packet and will be the end of the header.
6385 * If not then it probably represents the end of the last recognized
6386 * header.
Alexander Duyck2d94d8a2009-07-23 18:10:06 +00006387 */
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006388 if ((hdr.network - data) < max_len)
6389 return hdr.network - data;
6390 else
6391 return max_len;
6392}
6393
6394/**
6395 * igb_pull_tail - igb specific version of skb_pull_tail
6396 * @rx_ring: rx descriptor ring packet is being transacted on
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006397 * @rx_desc: pointer to the EOP Rx descriptor
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006398 * @skb: pointer to current skb being adjusted
6399 *
6400 * This function is an igb specific version of __pskb_pull_tail. The
6401 * main difference between this version and the original function is that
6402 * this function can make several assumptions about the state of things
6403 * that allow for significant optimizations versus the standard function.
6404 * As a result we can do things like drop a frag and maintain an accurate
6405 * truesize for the skb.
6406 */
6407static void igb_pull_tail(struct igb_ring *rx_ring,
6408 union e1000_adv_rx_desc *rx_desc,
6409 struct sk_buff *skb)
6410{
6411 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[0];
6412 unsigned char *va;
6413 unsigned int pull_len;
6414
6415 /*
6416 * it is valid to use page_address instead of kmap since we are
6417 * working with pages allocated out of the lomem pool per
6418 * alloc_page(GFP_ATOMIC)
6419 */
6420 va = skb_frag_address(frag);
6421
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006422 if (igb_test_staterr(rx_desc, E1000_RXDADV_STAT_TSIP)) {
6423 /* retrieve timestamp from buffer */
6424 igb_ptp_rx_pktstamp(rx_ring->q_vector, va, skb);
6425
6426 /* update pointers to remove timestamp header */
6427 skb_frag_size_sub(frag, IGB_TS_HDR_LEN);
6428 frag->page_offset += IGB_TS_HDR_LEN;
6429 skb->data_len -= IGB_TS_HDR_LEN;
6430 skb->len -= IGB_TS_HDR_LEN;
6431
6432 /* move va to start of packet data */
6433 va += IGB_TS_HDR_LEN;
6434 }
6435
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006436 /*
6437 * we need the header to contain the greater of either ETH_HLEN or
6438 * 60 bytes if the skb->len is less than 60 for skb_pad.
6439 */
6440 pull_len = igb_get_headlen(va, IGB_RX_HDR_LEN);
6441
6442 /* align pull length to size of long to optimize memcpy performance */
6443 skb_copy_to_linear_data(skb, va, ALIGN(pull_len, sizeof(long)));
6444
6445 /* update all of the pointers */
6446 skb_frag_size_sub(frag, pull_len);
6447 frag->page_offset += pull_len;
6448 skb->data_len -= pull_len;
6449 skb->tail += pull_len;
6450}
6451
6452/**
6453 * igb_cleanup_headers - Correct corrupted or empty headers
6454 * @rx_ring: rx descriptor ring packet is being transacted on
6455 * @rx_desc: pointer to the EOP Rx descriptor
6456 * @skb: pointer to current skb being fixed
6457 *
6458 * Address the case where we are pulling data in on pages only
6459 * and as such no data is present in the skb header.
6460 *
6461 * In addition if skb is not at least 60 bytes we need to pad it so that
6462 * it is large enough to qualify as a valid Ethernet frame.
6463 *
6464 * Returns true if an error was encountered and skb was freed.
6465 **/
6466static bool igb_cleanup_headers(struct igb_ring *rx_ring,
6467 union e1000_adv_rx_desc *rx_desc,
6468 struct sk_buff *skb)
6469{
6470
6471 if (unlikely((igb_test_staterr(rx_desc,
6472 E1000_RXDEXT_ERR_FRAME_ERR_MASK)))) {
6473 struct net_device *netdev = rx_ring->netdev;
6474 if (!(netdev->features & NETIF_F_RXALL)) {
6475 dev_kfree_skb_any(skb);
6476 return true;
6477 }
6478 }
6479
6480 /* place header in linear portion of buffer */
6481 if (skb_is_nonlinear(skb))
6482 igb_pull_tail(rx_ring, rx_desc, skb);
6483
6484 /* if skb_pad returns an error the skb was freed */
6485 if (unlikely(skb->len < 60)) {
6486 int pad_len = 60 - skb->len;
6487
6488 if (skb_pad(skb, pad_len))
6489 return true;
6490 __skb_put(skb, pad_len);
6491 }
6492
6493 return false;
Alexander Duyck2d94d8a2009-07-23 18:10:06 +00006494}
6495
Alexander Duyckdb2ee5b2012-09-25 00:30:57 +00006496/**
6497 * igb_process_skb_fields - Populate skb header fields from Rx descriptor
6498 * @rx_ring: rx descriptor ring packet is being transacted on
6499 * @rx_desc: pointer to the EOP Rx descriptor
6500 * @skb: pointer to current skb being populated
6501 *
6502 * This function checks the ring, descriptor, and packet information in
6503 * order to populate the hash, checksum, VLAN, timestamp, protocol, and
6504 * other fields within the skb.
6505 **/
6506static void igb_process_skb_fields(struct igb_ring *rx_ring,
6507 union e1000_adv_rx_desc *rx_desc,
6508 struct sk_buff *skb)
6509{
6510 struct net_device *dev = rx_ring->netdev;
6511
6512 igb_rx_hash(rx_ring, rx_desc, skb);
6513
6514 igb_rx_checksum(rx_ring, rx_desc, skb);
6515
Alexander Duyckdb2ee5b2012-09-25 00:30:57 +00006516 igb_ptp_rx_hwtstamp(rx_ring->q_vector, rx_desc, skb);
Alexander Duyckdb2ee5b2012-09-25 00:30:57 +00006517
6518 if ((dev->features & NETIF_F_HW_VLAN_RX) &&
6519 igb_test_staterr(rx_desc, E1000_RXD_STAT_VP)) {
6520 u16 vid;
6521 if (igb_test_staterr(rx_desc, E1000_RXDEXT_STATERR_LB) &&
6522 test_bit(IGB_RING_FLAG_RX_LB_VLAN_BSWAP, &rx_ring->flags))
6523 vid = be16_to_cpu(rx_desc->wb.upper.vlan);
6524 else
6525 vid = le16_to_cpu(rx_desc->wb.upper.vlan);
6526
6527 __vlan_hwaccel_put_tag(skb, vid);
6528 }
6529
6530 skb_record_rx_queue(skb, rx_ring->queue_index);
6531
6532 skb->protocol = eth_type_trans(skb, rx_ring->netdev);
6533}
6534
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006535static bool igb_clean_rx_irq(struct igb_q_vector *q_vector, const int budget)
Auke Kok9d5c8242008-01-24 02:22:38 -08006536{
Alexander Duyck0ba82992011-08-26 07:45:47 +00006537 struct igb_ring *rx_ring = q_vector->rx.ring;
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006538 struct sk_buff *skb = rx_ring->skb;
Auke Kok9d5c8242008-01-24 02:22:38 -08006539 unsigned int total_bytes = 0, total_packets = 0;
Alexander Duyck16eb8812011-08-26 07:43:54 +00006540 u16 cleaned_count = igb_desc_unused(rx_ring);
Auke Kok9d5c8242008-01-24 02:22:38 -08006541
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006542 do {
6543 union e1000_adv_rx_desc *rx_desc;
Auke Kok9d5c8242008-01-24 02:22:38 -08006544
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006545 /* return some buffers to hardware, one at a time is too slow */
6546 if (cleaned_count >= IGB_RX_BUFFER_WRITE) {
6547 igb_alloc_rx_buffers(rx_ring, cleaned_count);
6548 cleaned_count = 0;
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07006549 }
6550
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006551 rx_desc = IGB_RX_DESC(rx_ring, rx_ring->next_to_clean);
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07006552
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006553 if (!igb_test_staterr(rx_desc, E1000_RXD_STAT_DD))
6554 break;
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07006555
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006556 /* retrieve a buffer from the ring */
6557 skb = igb_fetch_rx_buffer(rx_ring, rx_desc, skb);
Alexander Duyck16eb8812011-08-26 07:43:54 +00006558
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006559 /* exit if we failed to retrieve a buffer */
6560 if (!skb)
6561 break;
6562
6563 cleaned_count++;
6564
6565 /* fetch next buffer in frame if non-eop */
6566 if (igb_is_non_eop(rx_ring, rx_desc))
6567 continue;
Alexander Duyck44390ca2011-08-26 07:43:38 +00006568
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006569 /* verify the packet layout is correct */
6570 if (igb_cleanup_headers(rx_ring, rx_desc, skb)) {
6571 skb = NULL;
6572 continue;
Auke Kok9d5c8242008-01-24 02:22:38 -08006573 }
Auke Kok9d5c8242008-01-24 02:22:38 -08006574
Alexander Duyckdb2ee5b2012-09-25 00:30:57 +00006575 /* probably a little skewed due to removing CRC */
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00006576 total_bytes += skb->len;
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00006577
Alexander Duyckdb2ee5b2012-09-25 00:30:57 +00006578 /* populate checksum, timestamp, VLAN, and protocol */
6579 igb_process_skb_fields(rx_ring, rx_desc, skb);
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00006580
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00006581 napi_gro_receive(&q_vector->napi, skb);
Auke Kok9d5c8242008-01-24 02:22:38 -08006582
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006583 /* reset skb pointer */
6584 skb = NULL;
6585
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006586 /* update budget accounting */
6587 total_packets++;
6588 } while (likely(total_packets < budget));
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07006589
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006590 /* place incomplete frames back on ring for completion */
6591 rx_ring->skb = skb;
6592
Eric Dumazet12dcd862010-10-15 17:27:10 +00006593 u64_stats_update_begin(&rx_ring->rx_syncp);
Auke Kok9d5c8242008-01-24 02:22:38 -08006594 rx_ring->rx_stats.packets += total_packets;
6595 rx_ring->rx_stats.bytes += total_bytes;
Eric Dumazet12dcd862010-10-15 17:27:10 +00006596 u64_stats_update_end(&rx_ring->rx_syncp);
Alexander Duyck0ba82992011-08-26 07:45:47 +00006597 q_vector->rx.total_packets += total_packets;
6598 q_vector->rx.total_bytes += total_bytes;
Alexander Duyckc023cd82011-08-26 07:43:43 +00006599
6600 if (cleaned_count)
Alexander Duyckcd392f52011-08-26 07:43:59 +00006601 igb_alloc_rx_buffers(rx_ring, cleaned_count);
Alexander Duyckc023cd82011-08-26 07:43:43 +00006602
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006603 return (total_packets < budget);
Auke Kok9d5c8242008-01-24 02:22:38 -08006604}
6605
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006606static bool igb_alloc_mapped_page(struct igb_ring *rx_ring,
6607 struct igb_rx_buffer *bi)
Alexander Duyckc023cd82011-08-26 07:43:43 +00006608{
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006609 struct page *page = bi->page;
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006610 dma_addr_t dma;
Alexander Duyckc023cd82011-08-26 07:43:43 +00006611
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006612 /* since we are recycling buffers we should seldom need to alloc */
6613 if (likely(page))
Alexander Duyckc023cd82011-08-26 07:43:43 +00006614 return true;
6615
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006616 /* alloc new page for storage */
6617 page = __skb_alloc_page(GFP_ATOMIC | __GFP_COLD, NULL);
6618 if (unlikely(!page)) {
6619 rx_ring->rx_stats.alloc_failed++;
6620 return false;
Alexander Duyckc023cd82011-08-26 07:43:43 +00006621 }
6622
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006623 /* map page for use */
6624 dma = dma_map_page(rx_ring->dev, page, 0, PAGE_SIZE, DMA_FROM_DEVICE);
Alexander Duyckc023cd82011-08-26 07:43:43 +00006625
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006626 /*
6627 * if mapping failed free memory back to system since
6628 * there isn't much point in holding memory we can't use
6629 */
Alexander Duyckc023cd82011-08-26 07:43:43 +00006630 if (dma_mapping_error(rx_ring->dev, dma)) {
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006631 __free_page(page);
6632
Alexander Duyckc023cd82011-08-26 07:43:43 +00006633 rx_ring->rx_stats.alloc_failed++;
6634 return false;
6635 }
6636
6637 bi->dma = dma;
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006638 bi->page = page;
6639 bi->page_offset = 0;
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006640
Alexander Duyckc023cd82011-08-26 07:43:43 +00006641 return true;
6642}
6643
Auke Kok9d5c8242008-01-24 02:22:38 -08006644/**
Alexander Duyckcd392f52011-08-26 07:43:59 +00006645 * igb_alloc_rx_buffers - Replace used receive buffers; packet split
Auke Kok9d5c8242008-01-24 02:22:38 -08006646 * @adapter: address of board private structure
6647 **/
Alexander Duyckcd392f52011-08-26 07:43:59 +00006648void igb_alloc_rx_buffers(struct igb_ring *rx_ring, u16 cleaned_count)
Auke Kok9d5c8242008-01-24 02:22:38 -08006649{
Auke Kok9d5c8242008-01-24 02:22:38 -08006650 union e1000_adv_rx_desc *rx_desc;
Alexander Duyck06034642011-08-26 07:44:22 +00006651 struct igb_rx_buffer *bi;
Alexander Duyckc023cd82011-08-26 07:43:43 +00006652 u16 i = rx_ring->next_to_use;
Auke Kok9d5c8242008-01-24 02:22:38 -08006653
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006654 /* nothing to do */
6655 if (!cleaned_count)
6656 return;
6657
Alexander Duyck601369062011-08-26 07:44:05 +00006658 rx_desc = IGB_RX_DESC(rx_ring, i);
Alexander Duyck06034642011-08-26 07:44:22 +00006659 bi = &rx_ring->rx_buffer_info[i];
Alexander Duyckc023cd82011-08-26 07:43:43 +00006660 i -= rx_ring->count;
Auke Kok9d5c8242008-01-24 02:22:38 -08006661
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006662 do {
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006663 if (!igb_alloc_mapped_page(rx_ring, bi))
Alexander Duyckc023cd82011-08-26 07:43:43 +00006664 break;
Auke Kok9d5c8242008-01-24 02:22:38 -08006665
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006666 /*
6667 * Refresh the desc even if buffer_addrs didn't change
6668 * because each write-back erases this info.
6669 */
6670 rx_desc->read.pkt_addr = cpu_to_le64(bi->dma + bi->page_offset);
Auke Kok9d5c8242008-01-24 02:22:38 -08006671
Alexander Duyckc023cd82011-08-26 07:43:43 +00006672 rx_desc++;
6673 bi++;
Auke Kok9d5c8242008-01-24 02:22:38 -08006674 i++;
Alexander Duyckc023cd82011-08-26 07:43:43 +00006675 if (unlikely(!i)) {
Alexander Duyck601369062011-08-26 07:44:05 +00006676 rx_desc = IGB_RX_DESC(rx_ring, 0);
Alexander Duyck06034642011-08-26 07:44:22 +00006677 bi = rx_ring->rx_buffer_info;
Alexander Duyckc023cd82011-08-26 07:43:43 +00006678 i -= rx_ring->count;
6679 }
6680
6681 /* clear the hdr_addr for the next_to_use descriptor */
6682 rx_desc->read.hdr_addr = 0;
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006683
6684 cleaned_count--;
6685 } while (cleaned_count);
Auke Kok9d5c8242008-01-24 02:22:38 -08006686
Alexander Duyckc023cd82011-08-26 07:43:43 +00006687 i += rx_ring->count;
6688
Auke Kok9d5c8242008-01-24 02:22:38 -08006689 if (rx_ring->next_to_use != i) {
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006690 /* record the next descriptor to use */
Auke Kok9d5c8242008-01-24 02:22:38 -08006691 rx_ring->next_to_use = i;
Auke Kok9d5c8242008-01-24 02:22:38 -08006692
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006693 /* update next to alloc since we have filled the ring */
6694 rx_ring->next_to_alloc = i;
6695
6696 /*
6697 * Force memory writes to complete before letting h/w
Auke Kok9d5c8242008-01-24 02:22:38 -08006698 * know there are new descriptors to fetch. (Only
6699 * applicable for weak-ordered memory model archs,
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006700 * such as IA-64).
6701 */
Auke Kok9d5c8242008-01-24 02:22:38 -08006702 wmb();
Alexander Duyckfce99e32009-10-27 15:51:27 +00006703 writel(i, rx_ring->tail);
Auke Kok9d5c8242008-01-24 02:22:38 -08006704 }
6705}
6706
6707/**
6708 * igb_mii_ioctl -
6709 * @netdev:
6710 * @ifreq:
6711 * @cmd:
6712 **/
6713static int igb_mii_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
6714{
6715 struct igb_adapter *adapter = netdev_priv(netdev);
6716 struct mii_ioctl_data *data = if_mii(ifr);
6717
6718 if (adapter->hw.phy.media_type != e1000_media_type_copper)
6719 return -EOPNOTSUPP;
6720
6721 switch (cmd) {
6722 case SIOCGMIIPHY:
6723 data->phy_id = adapter->hw.phy.addr;
6724 break;
6725 case SIOCGMIIREG:
Alexander Duyckf5f4cf02008-11-21 21:30:24 -08006726 if (igb_read_phy_reg(&adapter->hw, data->reg_num & 0x1F,
6727 &data->val_out))
Auke Kok9d5c8242008-01-24 02:22:38 -08006728 return -EIO;
6729 break;
6730 case SIOCSMIIREG:
6731 default:
6732 return -EOPNOTSUPP;
6733 }
6734 return 0;
6735}
6736
6737/**
6738 * igb_ioctl -
6739 * @netdev:
6740 * @ifreq:
6741 * @cmd:
6742 **/
6743static int igb_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
6744{
6745 switch (cmd) {
6746 case SIOCGMIIPHY:
6747 case SIOCGMIIREG:
6748 case SIOCSMIIREG:
6749 return igb_mii_ioctl(netdev, ifr, cmd);
Patrick Ohlyc6cb0902009-02-12 05:03:42 +00006750 case SIOCSHWTSTAMP:
Matthew Vicka79f4f82012-08-10 05:40:44 +00006751 return igb_ptp_hwtstamp_ioctl(netdev, ifr, cmd);
Auke Kok9d5c8242008-01-24 02:22:38 -08006752 default:
6753 return -EOPNOTSUPP;
6754 }
6755}
6756
Alexander Duyck009bc062009-07-23 18:08:35 +00006757s32 igb_read_pcie_cap_reg(struct e1000_hw *hw, u32 reg, u16 *value)
6758{
6759 struct igb_adapter *adapter = hw->back;
Alexander Duyck009bc062009-07-23 18:08:35 +00006760
Jiang Liu23d028c2012-08-20 13:32:20 -06006761 if (pcie_capability_read_word(adapter->pdev, reg, value))
Alexander Duyck009bc062009-07-23 18:08:35 +00006762 return -E1000_ERR_CONFIG;
6763
Alexander Duyck009bc062009-07-23 18:08:35 +00006764 return 0;
6765}
6766
6767s32 igb_write_pcie_cap_reg(struct e1000_hw *hw, u32 reg, u16 *value)
6768{
6769 struct igb_adapter *adapter = hw->back;
Alexander Duyck009bc062009-07-23 18:08:35 +00006770
Jiang Liu23d028c2012-08-20 13:32:20 -06006771 if (pcie_capability_write_word(adapter->pdev, reg, *value))
Alexander Duyck009bc062009-07-23 18:08:35 +00006772 return -E1000_ERR_CONFIG;
6773
Alexander Duyck009bc062009-07-23 18:08:35 +00006774 return 0;
6775}
6776
Michał Mirosławc8f44af2011-11-15 15:29:55 +00006777static void igb_vlan_mode(struct net_device *netdev, netdev_features_t features)
Auke Kok9d5c8242008-01-24 02:22:38 -08006778{
6779 struct igb_adapter *adapter = netdev_priv(netdev);
6780 struct e1000_hw *hw = &adapter->hw;
6781 u32 ctrl, rctl;
Alexander Duyck5faf0302011-08-26 07:46:08 +00006782 bool enable = !!(features & NETIF_F_HW_VLAN_RX);
Auke Kok9d5c8242008-01-24 02:22:38 -08006783
Alexander Duyck5faf0302011-08-26 07:46:08 +00006784 if (enable) {
Auke Kok9d5c8242008-01-24 02:22:38 -08006785 /* enable VLAN tag insert/strip */
6786 ctrl = rd32(E1000_CTRL);
6787 ctrl |= E1000_CTRL_VME;
6788 wr32(E1000_CTRL, ctrl);
6789
Alexander Duyck51466232009-10-27 23:47:35 +00006790 /* Disable CFI check */
Auke Kok9d5c8242008-01-24 02:22:38 -08006791 rctl = rd32(E1000_RCTL);
Auke Kok9d5c8242008-01-24 02:22:38 -08006792 rctl &= ~E1000_RCTL_CFIEN;
6793 wr32(E1000_RCTL, rctl);
Auke Kok9d5c8242008-01-24 02:22:38 -08006794 } else {
6795 /* disable VLAN tag insert/strip */
6796 ctrl = rd32(E1000_CTRL);
6797 ctrl &= ~E1000_CTRL_VME;
6798 wr32(E1000_CTRL, ctrl);
Auke Kok9d5c8242008-01-24 02:22:38 -08006799 }
6800
Alexander Duycke1739522009-02-19 20:39:44 -08006801 igb_rlpml_set(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08006802}
6803
Jiri Pirko8e586132011-12-08 19:52:37 -05006804static int igb_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
Auke Kok9d5c8242008-01-24 02:22:38 -08006805{
6806 struct igb_adapter *adapter = netdev_priv(netdev);
6807 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006808 int pf_id = adapter->vfs_allocated_count;
Auke Kok9d5c8242008-01-24 02:22:38 -08006809
Alexander Duyck51466232009-10-27 23:47:35 +00006810 /* attempt to add filter to vlvf array */
6811 igb_vlvf_set(adapter, vid, true, pf_id);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006812
Alexander Duyck51466232009-10-27 23:47:35 +00006813 /* add the filter since PF can receive vlans w/o entry in vlvf */
6814 igb_vfta_set(hw, vid, true);
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00006815
6816 set_bit(vid, adapter->active_vlans);
Jiri Pirko8e586132011-12-08 19:52:37 -05006817
6818 return 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08006819}
6820
Jiri Pirko8e586132011-12-08 19:52:37 -05006821static int igb_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
Auke Kok9d5c8242008-01-24 02:22:38 -08006822{
6823 struct igb_adapter *adapter = netdev_priv(netdev);
6824 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006825 int pf_id = adapter->vfs_allocated_count;
Alexander Duyck51466232009-10-27 23:47:35 +00006826 s32 err;
Auke Kok9d5c8242008-01-24 02:22:38 -08006827
Alexander Duyck51466232009-10-27 23:47:35 +00006828 /* remove vlan from VLVF table array */
6829 err = igb_vlvf_set(adapter, vid, false, pf_id);
Auke Kok9d5c8242008-01-24 02:22:38 -08006830
Alexander Duyck51466232009-10-27 23:47:35 +00006831 /* if vid was not present in VLVF just remove it from table */
6832 if (err)
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006833 igb_vfta_set(hw, vid, false);
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00006834
6835 clear_bit(vid, adapter->active_vlans);
Jiri Pirko8e586132011-12-08 19:52:37 -05006836
6837 return 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08006838}
6839
6840static void igb_restore_vlan(struct igb_adapter *adapter)
6841{
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00006842 u16 vid;
Auke Kok9d5c8242008-01-24 02:22:38 -08006843
Alexander Duyck5faf0302011-08-26 07:46:08 +00006844 igb_vlan_mode(adapter->netdev, adapter->netdev->features);
6845
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00006846 for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
6847 igb_vlan_rx_add_vid(adapter->netdev, vid);
Auke Kok9d5c8242008-01-24 02:22:38 -08006848}
6849
David Decotigny14ad2512011-04-27 18:32:43 +00006850int igb_set_spd_dplx(struct igb_adapter *adapter, u32 spd, u8 dplx)
Auke Kok9d5c8242008-01-24 02:22:38 -08006851{
Alexander Duyck090b1792009-10-27 23:51:55 +00006852 struct pci_dev *pdev = adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08006853 struct e1000_mac_info *mac = &adapter->hw.mac;
6854
6855 mac->autoneg = 0;
6856
David Decotigny14ad2512011-04-27 18:32:43 +00006857 /* Make sure dplx is at most 1 bit and lsb of speed is not set
6858 * for the switch() below to work */
6859 if ((spd & 1) || (dplx & ~1))
6860 goto err_inval;
6861
Carolyn Wybornycd2638a2010-10-12 22:27:02 +00006862 /* Fiber NIC's only allow 1000 Gbps Full duplex */
6863 if ((adapter->hw.phy.media_type == e1000_media_type_internal_serdes) &&
David Decotigny14ad2512011-04-27 18:32:43 +00006864 spd != SPEED_1000 &&
6865 dplx != DUPLEX_FULL)
6866 goto err_inval;
Carolyn Wybornycd2638a2010-10-12 22:27:02 +00006867
David Decotigny14ad2512011-04-27 18:32:43 +00006868 switch (spd + dplx) {
Auke Kok9d5c8242008-01-24 02:22:38 -08006869 case SPEED_10 + DUPLEX_HALF:
6870 mac->forced_speed_duplex = ADVERTISE_10_HALF;
6871 break;
6872 case SPEED_10 + DUPLEX_FULL:
6873 mac->forced_speed_duplex = ADVERTISE_10_FULL;
6874 break;
6875 case SPEED_100 + DUPLEX_HALF:
6876 mac->forced_speed_duplex = ADVERTISE_100_HALF;
6877 break;
6878 case SPEED_100 + DUPLEX_FULL:
6879 mac->forced_speed_duplex = ADVERTISE_100_FULL;
6880 break;
6881 case SPEED_1000 + DUPLEX_FULL:
6882 mac->autoneg = 1;
6883 adapter->hw.phy.autoneg_advertised = ADVERTISE_1000_FULL;
6884 break;
6885 case SPEED_1000 + DUPLEX_HALF: /* not supported */
6886 default:
David Decotigny14ad2512011-04-27 18:32:43 +00006887 goto err_inval;
Auke Kok9d5c8242008-01-24 02:22:38 -08006888 }
Jesse Brandeburg8376dad2012-07-26 02:31:19 +00006889
6890 /* clear MDI, MDI(-X) override is only allowed when autoneg enabled */
6891 adapter->hw.phy.mdix = AUTO_ALL_MODES;
6892
Auke Kok9d5c8242008-01-24 02:22:38 -08006893 return 0;
David Decotigny14ad2512011-04-27 18:32:43 +00006894
6895err_inval:
6896 dev_err(&pdev->dev, "Unsupported Speed/Duplex configuration\n");
6897 return -EINVAL;
Auke Kok9d5c8242008-01-24 02:22:38 -08006898}
6899
Yan, Zheng749ab2c2012-01-04 20:23:37 +00006900static int __igb_shutdown(struct pci_dev *pdev, bool *enable_wake,
6901 bool runtime)
Auke Kok9d5c8242008-01-24 02:22:38 -08006902{
6903 struct net_device *netdev = pci_get_drvdata(pdev);
6904 struct igb_adapter *adapter = netdev_priv(netdev);
6905 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck2d064c02008-07-08 15:10:12 -07006906 u32 ctrl, rctl, status;
Yan, Zheng749ab2c2012-01-04 20:23:37 +00006907 u32 wufc = runtime ? E1000_WUFC_LNKC : adapter->wol;
Auke Kok9d5c8242008-01-24 02:22:38 -08006908#ifdef CONFIG_PM
6909 int retval = 0;
6910#endif
6911
6912 netif_device_detach(netdev);
6913
Alexander Duycka88f10e2008-07-08 15:13:38 -07006914 if (netif_running(netdev))
Yan, Zheng749ab2c2012-01-04 20:23:37 +00006915 __igb_close(netdev, true);
Alexander Duycka88f10e2008-07-08 15:13:38 -07006916
Alexander Duyck047e0032009-10-27 15:49:27 +00006917 igb_clear_interrupt_scheme(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08006918
6919#ifdef CONFIG_PM
6920 retval = pci_save_state(pdev);
6921 if (retval)
6922 return retval;
6923#endif
6924
6925 status = rd32(E1000_STATUS);
6926 if (status & E1000_STATUS_LU)
6927 wufc &= ~E1000_WUFC_LNKC;
6928
6929 if (wufc) {
6930 igb_setup_rctl(adapter);
Alexander Duyckff41f8d2009-09-03 14:48:56 +00006931 igb_set_rx_mode(netdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08006932
6933 /* turn on all-multi mode if wake on multicast is enabled */
6934 if (wufc & E1000_WUFC_MC) {
6935 rctl = rd32(E1000_RCTL);
6936 rctl |= E1000_RCTL_MPE;
6937 wr32(E1000_RCTL, rctl);
6938 }
6939
6940 ctrl = rd32(E1000_CTRL);
6941 /* advertise wake from D3Cold */
6942 #define E1000_CTRL_ADVD3WUC 0x00100000
6943 /* phy power management enable */
6944 #define E1000_CTRL_EN_PHY_PWR_MGMT 0x00200000
6945 ctrl |= E1000_CTRL_ADVD3WUC;
6946 wr32(E1000_CTRL, ctrl);
6947
Auke Kok9d5c8242008-01-24 02:22:38 -08006948 /* Allow time for pending master requests to run */
Alexander Duyck330a6d62009-10-27 23:51:35 +00006949 igb_disable_pcie_master(hw);
Auke Kok9d5c8242008-01-24 02:22:38 -08006950
6951 wr32(E1000_WUC, E1000_WUC_PME_EN);
6952 wr32(E1000_WUFC, wufc);
Auke Kok9d5c8242008-01-24 02:22:38 -08006953 } else {
6954 wr32(E1000_WUC, 0);
6955 wr32(E1000_WUFC, 0);
Auke Kok9d5c8242008-01-24 02:22:38 -08006956 }
6957
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00006958 *enable_wake = wufc || adapter->en_mng_pt;
6959 if (!*enable_wake)
Nick Nunley88a268c2010-02-17 01:01:59 +00006960 igb_power_down_link(adapter);
6961 else
6962 igb_power_up_link(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08006963
6964 /* Release control of h/w to f/w. If f/w is AMT enabled, this
6965 * would have already happened in close and is redundant. */
6966 igb_release_hw_control(adapter);
6967
6968 pci_disable_device(pdev);
6969
Auke Kok9d5c8242008-01-24 02:22:38 -08006970 return 0;
6971}
6972
6973#ifdef CONFIG_PM
Emil Tantilovd9dd9662012-01-28 08:10:35 +00006974#ifdef CONFIG_PM_SLEEP
Yan, Zheng749ab2c2012-01-04 20:23:37 +00006975static int igb_suspend(struct device *dev)
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00006976{
6977 int retval;
6978 bool wake;
Yan, Zheng749ab2c2012-01-04 20:23:37 +00006979 struct pci_dev *pdev = to_pci_dev(dev);
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00006980
Yan, Zheng749ab2c2012-01-04 20:23:37 +00006981 retval = __igb_shutdown(pdev, &wake, 0);
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00006982 if (retval)
6983 return retval;
6984
6985 if (wake) {
6986 pci_prepare_to_sleep(pdev);
6987 } else {
6988 pci_wake_from_d3(pdev, false);
6989 pci_set_power_state(pdev, PCI_D3hot);
6990 }
6991
6992 return 0;
6993}
Emil Tantilovd9dd9662012-01-28 08:10:35 +00006994#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00006995
Yan, Zheng749ab2c2012-01-04 20:23:37 +00006996static int igb_resume(struct device *dev)
Auke Kok9d5c8242008-01-24 02:22:38 -08006997{
Yan, Zheng749ab2c2012-01-04 20:23:37 +00006998 struct pci_dev *pdev = to_pci_dev(dev);
Auke Kok9d5c8242008-01-24 02:22:38 -08006999 struct net_device *netdev = pci_get_drvdata(pdev);
7000 struct igb_adapter *adapter = netdev_priv(netdev);
7001 struct e1000_hw *hw = &adapter->hw;
7002 u32 err;
7003
7004 pci_set_power_state(pdev, PCI_D0);
7005 pci_restore_state(pdev);
Nick Nunleyb94f2d72010-02-17 01:02:19 +00007006 pci_save_state(pdev);
Taku Izumi42bfd33a2008-06-20 12:10:30 +09007007
Alexander Duyckaed5dec2009-02-06 23:16:04 +00007008 err = pci_enable_device_mem(pdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08007009 if (err) {
7010 dev_err(&pdev->dev,
7011 "igb: Cannot enable PCI device from suspend\n");
7012 return err;
7013 }
7014 pci_set_master(pdev);
7015
7016 pci_enable_wake(pdev, PCI_D3hot, 0);
7017 pci_enable_wake(pdev, PCI_D3cold, 0);
7018
Stefan Assmann53c7d062012-12-04 06:00:12 +00007019 if (igb_init_interrupt_scheme(adapter, true)) {
Alexander Duycka88f10e2008-07-08 15:13:38 -07007020 dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
7021 return -ENOMEM;
Auke Kok9d5c8242008-01-24 02:22:38 -08007022 }
7023
Auke Kok9d5c8242008-01-24 02:22:38 -08007024 igb_reset(adapter);
Alexander Duycka8564f02009-02-06 23:21:10 +00007025
7026 /* let the f/w know that the h/w is now under the control of the
7027 * driver. */
7028 igb_get_hw_control(adapter);
7029
Auke Kok9d5c8242008-01-24 02:22:38 -08007030 wr32(E1000_WUS, ~0);
7031
Yan, Zheng749ab2c2012-01-04 20:23:37 +00007032 if (netdev->flags & IFF_UP) {
Alexander Duyck0c2cc022012-09-25 00:31:22 +00007033 rtnl_lock();
Yan, Zheng749ab2c2012-01-04 20:23:37 +00007034 err = __igb_open(netdev, true);
Alexander Duyck0c2cc022012-09-25 00:31:22 +00007035 rtnl_unlock();
Alexander Duycka88f10e2008-07-08 15:13:38 -07007036 if (err)
7037 return err;
7038 }
Auke Kok9d5c8242008-01-24 02:22:38 -08007039
7040 netif_device_attach(netdev);
Yan, Zheng749ab2c2012-01-04 20:23:37 +00007041 return 0;
7042}
7043
7044#ifdef CONFIG_PM_RUNTIME
7045static int igb_runtime_idle(struct device *dev)
7046{
7047 struct pci_dev *pdev = to_pci_dev(dev);
7048 struct net_device *netdev = pci_get_drvdata(pdev);
7049 struct igb_adapter *adapter = netdev_priv(netdev);
7050
7051 if (!igb_has_link(adapter))
7052 pm_schedule_suspend(dev, MSEC_PER_SEC * 5);
7053
7054 return -EBUSY;
7055}
7056
7057static int igb_runtime_suspend(struct device *dev)
7058{
7059 struct pci_dev *pdev = to_pci_dev(dev);
7060 int retval;
7061 bool wake;
7062
7063 retval = __igb_shutdown(pdev, &wake, 1);
7064 if (retval)
7065 return retval;
7066
7067 if (wake) {
7068 pci_prepare_to_sleep(pdev);
7069 } else {
7070 pci_wake_from_d3(pdev, false);
7071 pci_set_power_state(pdev, PCI_D3hot);
7072 }
Auke Kok9d5c8242008-01-24 02:22:38 -08007073
Auke Kok9d5c8242008-01-24 02:22:38 -08007074 return 0;
7075}
Yan, Zheng749ab2c2012-01-04 20:23:37 +00007076
7077static int igb_runtime_resume(struct device *dev)
7078{
7079 return igb_resume(dev);
7080}
7081#endif /* CONFIG_PM_RUNTIME */
Auke Kok9d5c8242008-01-24 02:22:38 -08007082#endif
7083
7084static void igb_shutdown(struct pci_dev *pdev)
7085{
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00007086 bool wake;
7087
Yan, Zheng749ab2c2012-01-04 20:23:37 +00007088 __igb_shutdown(pdev, &wake, 0);
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00007089
7090 if (system_state == SYSTEM_POWER_OFF) {
7091 pci_wake_from_d3(pdev, wake);
7092 pci_set_power_state(pdev, PCI_D3hot);
7093 }
Auke Kok9d5c8242008-01-24 02:22:38 -08007094}
7095
Greg Rosefa44f2f2013-01-17 01:03:06 -08007096#ifdef CONFIG_PCI_IOV
7097static int igb_sriov_reinit(struct pci_dev *dev)
7098{
7099 struct net_device *netdev = pci_get_drvdata(dev);
7100 struct igb_adapter *adapter = netdev_priv(netdev);
7101 struct pci_dev *pdev = adapter->pdev;
7102
7103 rtnl_lock();
7104
7105 if (netif_running(netdev))
7106 igb_close(netdev);
7107
7108 igb_clear_interrupt_scheme(adapter);
7109
7110 igb_init_queue_configuration(adapter);
7111
7112 if (igb_init_interrupt_scheme(adapter, true)) {
7113 dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
7114 return -ENOMEM;
7115 }
7116
7117 if (netif_running(netdev))
7118 igb_open(netdev);
7119
7120 rtnl_unlock();
7121
7122 return 0;
7123}
7124
7125static int igb_pci_disable_sriov(struct pci_dev *dev)
7126{
7127 int err = igb_disable_sriov(dev);
7128
7129 if (!err)
7130 err = igb_sriov_reinit(dev);
7131
7132 return err;
7133}
7134
7135static int igb_pci_enable_sriov(struct pci_dev *dev, int num_vfs)
7136{
7137 int err = igb_enable_sriov(dev, num_vfs);
7138
7139 if (err)
7140 goto out;
7141
7142 err = igb_sriov_reinit(dev);
7143 if (!err)
7144 return num_vfs;
7145
7146out:
7147 return err;
7148}
7149
7150#endif
7151static int igb_pci_sriov_configure(struct pci_dev *dev, int num_vfs)
7152{
7153#ifdef CONFIG_PCI_IOV
7154 if (num_vfs == 0)
7155 return igb_pci_disable_sriov(dev);
7156 else
7157 return igb_pci_enable_sriov(dev, num_vfs);
7158#endif
7159 return 0;
7160}
7161
Auke Kok9d5c8242008-01-24 02:22:38 -08007162#ifdef CONFIG_NET_POLL_CONTROLLER
7163/*
7164 * Polling 'interrupt' - used by things like netconsole to send skbs
7165 * without having to re-enable interrupts. It's not called while
7166 * the interrupt routine is executing.
7167 */
7168static void igb_netpoll(struct net_device *netdev)
7169{
7170 struct igb_adapter *adapter = netdev_priv(netdev);
Alexander Duyckeebbbdb2009-02-06 23:19:29 +00007171 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck0d1ae7f2011-08-26 07:46:34 +00007172 struct igb_q_vector *q_vector;
Auke Kok9d5c8242008-01-24 02:22:38 -08007173 int i;
Auke Kok9d5c8242008-01-24 02:22:38 -08007174
Alexander Duyck047e0032009-10-27 15:49:27 +00007175 for (i = 0; i < adapter->num_q_vectors; i++) {
Alexander Duyck0d1ae7f2011-08-26 07:46:34 +00007176 q_vector = adapter->q_vector[i];
7177 if (adapter->msix_entries)
7178 wr32(E1000_EIMC, q_vector->eims_value);
7179 else
7180 igb_irq_disable(adapter);
Alexander Duyck047e0032009-10-27 15:49:27 +00007181 napi_schedule(&q_vector->napi);
Alexander Duyckeebbbdb2009-02-06 23:19:29 +00007182 }
Auke Kok9d5c8242008-01-24 02:22:38 -08007183}
7184#endif /* CONFIG_NET_POLL_CONTROLLER */
7185
7186/**
7187 * igb_io_error_detected - called when PCI error is detected
7188 * @pdev: Pointer to PCI device
7189 * @state: The current pci connection state
7190 *
7191 * This function is called after a PCI bus error affecting
7192 * this device has been detected.
7193 */
7194static pci_ers_result_t igb_io_error_detected(struct pci_dev *pdev,
7195 pci_channel_state_t state)
7196{
7197 struct net_device *netdev = pci_get_drvdata(pdev);
7198 struct igb_adapter *adapter = netdev_priv(netdev);
7199
7200 netif_device_detach(netdev);
7201
Alexander Duyck59ed6ee2009-06-30 12:46:34 +00007202 if (state == pci_channel_io_perm_failure)
7203 return PCI_ERS_RESULT_DISCONNECT;
7204
Auke Kok9d5c8242008-01-24 02:22:38 -08007205 if (netif_running(netdev))
7206 igb_down(adapter);
7207 pci_disable_device(pdev);
7208
7209 /* Request a slot slot reset. */
7210 return PCI_ERS_RESULT_NEED_RESET;
7211}
7212
7213/**
7214 * igb_io_slot_reset - called after the pci bus has been reset.
7215 * @pdev: Pointer to PCI device
7216 *
7217 * Restart the card from scratch, as if from a cold-boot. Implementation
7218 * resembles the first-half of the igb_resume routine.
7219 */
7220static pci_ers_result_t igb_io_slot_reset(struct pci_dev *pdev)
7221{
7222 struct net_device *netdev = pci_get_drvdata(pdev);
7223 struct igb_adapter *adapter = netdev_priv(netdev);
7224 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck40a914f2008-11-27 00:24:37 -08007225 pci_ers_result_t result;
Taku Izumi42bfd33a2008-06-20 12:10:30 +09007226 int err;
Auke Kok9d5c8242008-01-24 02:22:38 -08007227
Alexander Duyckaed5dec2009-02-06 23:16:04 +00007228 if (pci_enable_device_mem(pdev)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08007229 dev_err(&pdev->dev,
7230 "Cannot re-enable PCI device after reset.\n");
Alexander Duyck40a914f2008-11-27 00:24:37 -08007231 result = PCI_ERS_RESULT_DISCONNECT;
7232 } else {
7233 pci_set_master(pdev);
7234 pci_restore_state(pdev);
Nick Nunleyb94f2d72010-02-17 01:02:19 +00007235 pci_save_state(pdev);
Alexander Duyck40a914f2008-11-27 00:24:37 -08007236
7237 pci_enable_wake(pdev, PCI_D3hot, 0);
7238 pci_enable_wake(pdev, PCI_D3cold, 0);
7239
7240 igb_reset(adapter);
7241 wr32(E1000_WUS, ~0);
7242 result = PCI_ERS_RESULT_RECOVERED;
Auke Kok9d5c8242008-01-24 02:22:38 -08007243 }
Auke Kok9d5c8242008-01-24 02:22:38 -08007244
Jeff Kirsherea943d42008-12-11 20:34:19 -08007245 err = pci_cleanup_aer_uncorrect_error_status(pdev);
7246 if (err) {
7247 dev_err(&pdev->dev, "pci_cleanup_aer_uncorrect_error_status "
7248 "failed 0x%0x\n", err);
7249 /* non-fatal, continue */
7250 }
Auke Kok9d5c8242008-01-24 02:22:38 -08007251
Alexander Duyck40a914f2008-11-27 00:24:37 -08007252 return result;
Auke Kok9d5c8242008-01-24 02:22:38 -08007253}
7254
7255/**
7256 * igb_io_resume - called when traffic can start flowing again.
7257 * @pdev: Pointer to PCI device
7258 *
7259 * This callback is called when the error recovery driver tells us that
7260 * its OK to resume normal operation. Implementation resembles the
7261 * second-half of the igb_resume routine.
7262 */
7263static void igb_io_resume(struct pci_dev *pdev)
7264{
7265 struct net_device *netdev = pci_get_drvdata(pdev);
7266 struct igb_adapter *adapter = netdev_priv(netdev);
7267
Auke Kok9d5c8242008-01-24 02:22:38 -08007268 if (netif_running(netdev)) {
7269 if (igb_up(adapter)) {
7270 dev_err(&pdev->dev, "igb_up failed after reset\n");
7271 return;
7272 }
7273 }
7274
7275 netif_device_attach(netdev);
7276
7277 /* let the f/w know that the h/w is now under the control of the
7278 * driver. */
7279 igb_get_hw_control(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08007280}
7281
Alexander Duyck26ad9172009-10-05 06:32:49 +00007282static void igb_rar_set_qsel(struct igb_adapter *adapter, u8 *addr, u32 index,
7283 u8 qsel)
7284{
7285 u32 rar_low, rar_high;
7286 struct e1000_hw *hw = &adapter->hw;
7287
7288 /* HW expects these in little endian so we reverse the byte order
7289 * from network order (big endian) to little endian
7290 */
7291 rar_low = ((u32) addr[0] | ((u32) addr[1] << 8) |
7292 ((u32) addr[2] << 16) | ((u32) addr[3] << 24));
7293 rar_high = ((u32) addr[4] | ((u32) addr[5] << 8));
7294
7295 /* Indicate to hardware the Address is Valid. */
7296 rar_high |= E1000_RAH_AV;
7297
7298 if (hw->mac.type == e1000_82575)
7299 rar_high |= E1000_RAH_POOL_1 * qsel;
7300 else
7301 rar_high |= E1000_RAH_POOL_1 << qsel;
7302
7303 wr32(E1000_RAL(index), rar_low);
7304 wrfl();
7305 wr32(E1000_RAH(index), rar_high);
7306 wrfl();
7307}
7308
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007309static int igb_set_vf_mac(struct igb_adapter *adapter,
7310 int vf, unsigned char *mac_addr)
7311{
7312 struct e1000_hw *hw = &adapter->hw;
Alexander Duyckff41f8d2009-09-03 14:48:56 +00007313 /* VF MAC addresses start at end of receive addresses and moves
7314 * torwards the first, as a result a collision should not be possible */
7315 int rar_entry = hw->mac.rar_entry_count - (vf + 1);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007316
Alexander Duyck37680112009-02-19 20:40:30 -08007317 memcpy(adapter->vf_data[vf].vf_mac_addresses, mac_addr, ETH_ALEN);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007318
Alexander Duyck26ad9172009-10-05 06:32:49 +00007319 igb_rar_set_qsel(adapter, mac_addr, rar_entry, vf);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007320
7321 return 0;
7322}
7323
Williams, Mitch A8151d292010-02-10 01:44:24 +00007324static int igb_ndo_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
7325{
7326 struct igb_adapter *adapter = netdev_priv(netdev);
7327 if (!is_valid_ether_addr(mac) || (vf >= adapter->vfs_allocated_count))
7328 return -EINVAL;
7329 adapter->vf_data[vf].flags |= IGB_VF_FLAG_PF_SET_MAC;
7330 dev_info(&adapter->pdev->dev, "setting MAC %pM on VF %d\n", mac, vf);
7331 dev_info(&adapter->pdev->dev, "Reload the VF driver to make this"
7332 " change effective.");
7333 if (test_bit(__IGB_DOWN, &adapter->state)) {
7334 dev_warn(&adapter->pdev->dev, "The VF MAC address has been set,"
7335 " but the PF device is not up.\n");
7336 dev_warn(&adapter->pdev->dev, "Bring the PF device up before"
7337 " attempting to use the VF device.\n");
7338 }
7339 return igb_set_vf_mac(adapter, vf, mac);
7340}
7341
Lior Levy17dc5662011-02-08 02:28:46 +00007342static int igb_link_mbps(int internal_link_speed)
7343{
7344 switch (internal_link_speed) {
7345 case SPEED_100:
7346 return 100;
7347 case SPEED_1000:
7348 return 1000;
7349 default:
7350 return 0;
7351 }
7352}
7353
7354static void igb_set_vf_rate_limit(struct e1000_hw *hw, int vf, int tx_rate,
7355 int link_speed)
7356{
7357 int rf_dec, rf_int;
7358 u32 bcnrc_val;
7359
7360 if (tx_rate != 0) {
7361 /* Calculate the rate factor values to set */
7362 rf_int = link_speed / tx_rate;
7363 rf_dec = (link_speed - (rf_int * tx_rate));
7364 rf_dec = (rf_dec * (1<<E1000_RTTBCNRC_RF_INT_SHIFT)) / tx_rate;
7365
7366 bcnrc_val = E1000_RTTBCNRC_RS_ENA;
7367 bcnrc_val |= ((rf_int<<E1000_RTTBCNRC_RF_INT_SHIFT) &
7368 E1000_RTTBCNRC_RF_INT_MASK);
7369 bcnrc_val |= (rf_dec & E1000_RTTBCNRC_RF_DEC_MASK);
7370 } else {
7371 bcnrc_val = 0;
7372 }
7373
7374 wr32(E1000_RTTDQSEL, vf); /* vf X uses queue X */
Lior Levyf00b0da2011-06-04 06:05:03 +00007375 /*
7376 * Set global transmit compensation time to the MMW_SIZE in RTTBCNRM
7377 * register. MMW_SIZE=0x014 if 9728-byte jumbo is supported.
7378 */
7379 wr32(E1000_RTTBCNRM, 0x14);
Lior Levy17dc5662011-02-08 02:28:46 +00007380 wr32(E1000_RTTBCNRC, bcnrc_val);
7381}
7382
7383static void igb_check_vf_rate_limit(struct igb_adapter *adapter)
7384{
7385 int actual_link_speed, i;
7386 bool reset_rate = false;
7387
7388 /* VF TX rate limit was not set or not supported */
7389 if ((adapter->vf_rate_link_speed == 0) ||
7390 (adapter->hw.mac.type != e1000_82576))
7391 return;
7392
7393 actual_link_speed = igb_link_mbps(adapter->link_speed);
7394 if (actual_link_speed != adapter->vf_rate_link_speed) {
7395 reset_rate = true;
7396 adapter->vf_rate_link_speed = 0;
7397 dev_info(&adapter->pdev->dev,
7398 "Link speed has been changed. VF Transmit "
7399 "rate is disabled\n");
7400 }
7401
7402 for (i = 0; i < adapter->vfs_allocated_count; i++) {
7403 if (reset_rate)
7404 adapter->vf_data[i].tx_rate = 0;
7405
7406 igb_set_vf_rate_limit(&adapter->hw, i,
7407 adapter->vf_data[i].tx_rate,
7408 actual_link_speed);
7409 }
7410}
7411
Williams, Mitch A8151d292010-02-10 01:44:24 +00007412static int igb_ndo_set_vf_bw(struct net_device *netdev, int vf, int tx_rate)
7413{
Lior Levy17dc5662011-02-08 02:28:46 +00007414 struct igb_adapter *adapter = netdev_priv(netdev);
7415 struct e1000_hw *hw = &adapter->hw;
7416 int actual_link_speed;
7417
7418 if (hw->mac.type != e1000_82576)
7419 return -EOPNOTSUPP;
7420
7421 actual_link_speed = igb_link_mbps(adapter->link_speed);
7422 if ((vf >= adapter->vfs_allocated_count) ||
7423 (!(rd32(E1000_STATUS) & E1000_STATUS_LU)) ||
7424 (tx_rate < 0) || (tx_rate > actual_link_speed))
7425 return -EINVAL;
7426
7427 adapter->vf_rate_link_speed = actual_link_speed;
7428 adapter->vf_data[vf].tx_rate = (u16)tx_rate;
7429 igb_set_vf_rate_limit(hw, vf, tx_rate, actual_link_speed);
7430
7431 return 0;
Williams, Mitch A8151d292010-02-10 01:44:24 +00007432}
7433
7434static int igb_ndo_get_vf_config(struct net_device *netdev,
7435 int vf, struct ifla_vf_info *ivi)
7436{
7437 struct igb_adapter *adapter = netdev_priv(netdev);
7438 if (vf >= adapter->vfs_allocated_count)
7439 return -EINVAL;
7440 ivi->vf = vf;
7441 memcpy(&ivi->mac, adapter->vf_data[vf].vf_mac_addresses, ETH_ALEN);
Lior Levy17dc5662011-02-08 02:28:46 +00007442 ivi->tx_rate = adapter->vf_data[vf].tx_rate;
Williams, Mitch A8151d292010-02-10 01:44:24 +00007443 ivi->vlan = adapter->vf_data[vf].pf_vlan;
7444 ivi->qos = adapter->vf_data[vf].pf_qos;
7445 return 0;
7446}
7447
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007448static void igb_vmm_control(struct igb_adapter *adapter)
7449{
7450 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck10d8e902009-10-27 15:54:04 +00007451 u32 reg;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007452
Alexander Duyck52a1dd42010-03-22 14:07:46 +00007453 switch (hw->mac.type) {
7454 case e1000_82575:
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00007455 case e1000_i210:
7456 case e1000_i211:
Alexander Duyck52a1dd42010-03-22 14:07:46 +00007457 default:
7458 /* replication is not supported for 82575 */
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007459 return;
Alexander Duyck52a1dd42010-03-22 14:07:46 +00007460 case e1000_82576:
7461 /* notify HW that the MAC is adding vlan tags */
7462 reg = rd32(E1000_DTXCTL);
7463 reg |= E1000_DTXCTL_VLAN_ADDED;
7464 wr32(E1000_DTXCTL, reg);
7465 case e1000_82580:
7466 /* enable replication vlan tag stripping */
7467 reg = rd32(E1000_RPLOLR);
7468 reg |= E1000_RPLOLR_STRVLAN;
7469 wr32(E1000_RPLOLR, reg);
Alexander Duyckd2ba2ed2010-03-22 14:08:06 +00007470 case e1000_i350:
7471 /* none of the above registers are supported by i350 */
Alexander Duyck52a1dd42010-03-22 14:07:46 +00007472 break;
7473 }
Alexander Duyck10d8e902009-10-27 15:54:04 +00007474
Alexander Duyckd4960302009-10-27 15:53:45 +00007475 if (adapter->vfs_allocated_count) {
7476 igb_vmdq_set_loopback_pf(hw, true);
7477 igb_vmdq_set_replication_pf(hw, true);
Greg Rose13800462010-11-06 02:08:26 +00007478 igb_vmdq_set_anti_spoofing_pf(hw, true,
7479 adapter->vfs_allocated_count);
Alexander Duyckd4960302009-10-27 15:53:45 +00007480 } else {
7481 igb_vmdq_set_loopback_pf(hw, false);
7482 igb_vmdq_set_replication_pf(hw, false);
7483 }
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007484}
7485
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +00007486static void igb_init_dmac(struct igb_adapter *adapter, u32 pba)
7487{
7488 struct e1000_hw *hw = &adapter->hw;
7489 u32 dmac_thr;
7490 u16 hwm;
7491
7492 if (hw->mac.type > e1000_82580) {
7493 if (adapter->flags & IGB_FLAG_DMAC) {
7494 u32 reg;
7495
7496 /* force threshold to 0. */
7497 wr32(E1000_DMCTXTH, 0);
7498
7499 /*
Matthew Vicke8c626e2011-11-17 08:33:12 +00007500 * DMA Coalescing high water mark needs to be greater
7501 * than the Rx threshold. Set hwm to PBA - max frame
7502 * size in 16B units, capping it at PBA - 6KB.
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +00007503 */
Matthew Vicke8c626e2011-11-17 08:33:12 +00007504 hwm = 64 * pba - adapter->max_frame_size / 16;
7505 if (hwm < 64 * (pba - 6))
7506 hwm = 64 * (pba - 6);
7507 reg = rd32(E1000_FCRTC);
7508 reg &= ~E1000_FCRTC_RTH_COAL_MASK;
7509 reg |= ((hwm << E1000_FCRTC_RTH_COAL_SHIFT)
7510 & E1000_FCRTC_RTH_COAL_MASK);
7511 wr32(E1000_FCRTC, reg);
7512
7513 /*
7514 * Set the DMA Coalescing Rx threshold to PBA - 2 * max
7515 * frame size, capping it at PBA - 10KB.
7516 */
7517 dmac_thr = pba - adapter->max_frame_size / 512;
7518 if (dmac_thr < pba - 10)
7519 dmac_thr = pba - 10;
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +00007520 reg = rd32(E1000_DMACR);
7521 reg &= ~E1000_DMACR_DMACTHR_MASK;
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +00007522 reg |= ((dmac_thr << E1000_DMACR_DMACTHR_SHIFT)
7523 & E1000_DMACR_DMACTHR_MASK);
7524
7525 /* transition to L0x or L1 if available..*/
7526 reg |= (E1000_DMACR_DMAC_EN | E1000_DMACR_DMAC_LX_MASK);
7527
7528 /* watchdog timer= +-1000 usec in 32usec intervals */
7529 reg |= (1000 >> 5);
Matthew Vick0c02dd92012-04-14 05:20:32 +00007530
7531 /* Disable BMC-to-OS Watchdog Enable */
7532 reg &= ~E1000_DMACR_DC_BMC2OSW_EN;
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +00007533 wr32(E1000_DMACR, reg);
7534
7535 /*
7536 * no lower threshold to disable
7537 * coalescing(smart fifb)-UTRESH=0
7538 */
7539 wr32(E1000_DMCRTRH, 0);
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +00007540
7541 reg = (IGB_DMCTLX_DCFLUSH_DIS | 0x4);
7542
7543 wr32(E1000_DMCTLX, reg);
7544
7545 /*
7546 * free space in tx packet buffer to wake from
7547 * DMA coal
7548 */
7549 wr32(E1000_DMCTXTH, (IGB_MIN_TXPBSIZE -
7550 (IGB_TX_BUF_4096 + adapter->max_frame_size)) >> 6);
7551
7552 /*
7553 * make low power state decision controlled
7554 * by DMA coal
7555 */
7556 reg = rd32(E1000_PCIEMISC);
7557 reg &= ~E1000_PCIEMISC_LX_DECISION;
7558 wr32(E1000_PCIEMISC, reg);
7559 } /* endif adapter->dmac is not disabled */
7560 } else if (hw->mac.type == e1000_82580) {
7561 u32 reg = rd32(E1000_PCIEMISC);
7562 wr32(E1000_PCIEMISC, reg & ~E1000_PCIEMISC_LX_DECISION);
7563 wr32(E1000_DMACR, 0);
7564 }
7565}
7566
Carolyn Wyborny441fc6f2012-12-07 03:00:30 +00007567static DEFINE_SPINLOCK(i2c_clients_lock);
7568
7569/* igb_get_i2c_client - returns matching client
7570 * in adapters's client list.
7571 * @adapter: adapter struct
7572 * @dev_addr: device address of i2c needed.
7573 */
7574struct i2c_client *
7575igb_get_i2c_client(struct igb_adapter *adapter, u8 dev_addr)
7576{
7577 ulong flags;
7578 struct igb_i2c_client_list *client_list;
7579 struct i2c_client *client = NULL;
7580 struct i2c_board_info client_info = {
7581 I2C_BOARD_INFO("igb", 0x00),
7582 };
7583
7584 spin_lock_irqsave(&i2c_clients_lock, flags);
7585 client_list = adapter->i2c_clients;
7586
7587 /* See if we already have an i2c_client */
7588 while (client_list) {
7589 if (client_list->client->addr == (dev_addr >> 1)) {
7590 client = client_list->client;
7591 goto exit;
7592 } else {
7593 client_list = client_list->next;
7594 }
7595 }
7596
7597 /* no client_list found, create a new one */
7598 client_list = kzalloc(sizeof(*client_list), GFP_KERNEL);
7599 if (client_list == NULL)
7600 goto exit;
7601
7602 /* dev_addr passed to us is left-shifted by 1 bit
7603 * i2c_new_device call expects it to be flush to the right.
7604 */
7605 client_info.addr = dev_addr >> 1;
7606 client_info.platform_data = adapter;
7607 client_list->client = i2c_new_device(&adapter->i2c_adap, &client_info);
7608 if (client_list->client == NULL) {
7609 dev_info(&adapter->pdev->dev, "Failed to create new i2c device..\n");
7610 goto err_no_client;
7611 }
7612
7613 /* insert new client at head of list */
7614 client_list->next = adapter->i2c_clients;
7615 adapter->i2c_clients = client_list;
7616
7617 spin_unlock_irqrestore(&i2c_clients_lock, flags);
7618
7619 client = client_list->client;
7620 goto exit;
7621
7622err_no_client:
7623 kfree(client_list);
7624exit:
7625 spin_unlock_irqrestore(&i2c_clients_lock, flags);
7626 return client;
7627}
7628
7629/* igb_read_i2c_byte - Reads 8 bit word over I2C
7630 * @hw: pointer to hardware structure
7631 * @byte_offset: byte offset to read
7632 * @dev_addr: device address
7633 * @data: value read
7634 *
7635 * Performs byte read operation over I2C interface at
7636 * a specified device address.
7637 */
7638s32 igb_read_i2c_byte(struct e1000_hw *hw, u8 byte_offset,
7639 u8 dev_addr, u8 *data)
7640{
7641 struct igb_adapter *adapter = container_of(hw, struct igb_adapter, hw);
7642 struct i2c_client *this_client = igb_get_i2c_client(adapter, dev_addr);
7643 s32 status;
7644 u16 swfw_mask = 0;
7645
7646 if (!this_client)
7647 return E1000_ERR_I2C;
7648
7649 swfw_mask = E1000_SWFW_PHY0_SM;
7650
7651 if (hw->mac.ops.acquire_swfw_sync(hw, swfw_mask)
7652 != E1000_SUCCESS)
7653 return E1000_ERR_SWFW_SYNC;
7654
7655 status = i2c_smbus_read_byte_data(this_client, byte_offset);
7656 hw->mac.ops.release_swfw_sync(hw, swfw_mask);
7657
7658 if (status < 0)
7659 return E1000_ERR_I2C;
7660 else {
7661 *data = status;
7662 return E1000_SUCCESS;
7663 }
7664}
7665
7666/* igb_write_i2c_byte - Writes 8 bit word over I2C
7667 * @hw: pointer to hardware structure
7668 * @byte_offset: byte offset to write
7669 * @dev_addr: device address
7670 * @data: value to write
7671 *
7672 * Performs byte write operation over I2C interface at
7673 * a specified device address.
7674 */
7675s32 igb_write_i2c_byte(struct e1000_hw *hw, u8 byte_offset,
7676 u8 dev_addr, u8 data)
7677{
7678 struct igb_adapter *adapter = container_of(hw, struct igb_adapter, hw);
7679 struct i2c_client *this_client = igb_get_i2c_client(adapter, dev_addr);
7680 s32 status;
7681 u16 swfw_mask = E1000_SWFW_PHY0_SM;
7682
7683 if (!this_client)
7684 return E1000_ERR_I2C;
7685
7686 if (hw->mac.ops.acquire_swfw_sync(hw, swfw_mask) != E1000_SUCCESS)
7687 return E1000_ERR_SWFW_SYNC;
7688 status = i2c_smbus_write_byte_data(this_client, byte_offset, data);
7689 hw->mac.ops.release_swfw_sync(hw, swfw_mask);
7690
7691 if (status)
7692 return E1000_ERR_I2C;
7693 else
7694 return E1000_SUCCESS;
7695
7696}
Auke Kok9d5c8242008-01-24 02:22:38 -08007697/* igb_main.c */