blob: de4ebb306530a2e96823ad6543950b557fa7752c [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
Auke Kok9d5c8242008-01-24 02:22:38 -080060#include "igb.h"
61
Carolyn Wyborny200e5fd2012-05-31 23:39:30 +000062#define MAJ 4
63#define MIN 0
Carolyn Wyborny3db73802012-10-17 07:01:56 +000064#define BUILD 17
Carolyn Wyborny0d1fe822011-03-11 20:58:19 -080065#define DRV_VERSION __stringify(MAJ) "." __stringify(MIN) "." \
Carolyn Wyborny929dd042011-05-26 03:02:26 +000066__stringify(BUILD) "-k"
Auke Kok9d5c8242008-01-24 02:22:38 -080067char igb_driver_name[] = "igb";
68char igb_driver_version[] = DRV_VERSION;
69static const char igb_driver_string[] =
70 "Intel(R) Gigabit Ethernet Network Driver";
Carolyn Wyborny6e861322012-01-18 22:13:27 +000071static const char igb_copyright[] = "Copyright (c) 2007-2012 Intel Corporation.";
Auke Kok9d5c8242008-01-24 02:22:38 -080072
Auke Kok9d5c8242008-01-24 02:22:38 -080073static const struct e1000_info *igb_info_tbl[] = {
74 [board_82575] = &e1000_82575_info,
75};
76
Alexey Dobriyana3aa1882010-01-07 11:58:11 +000077static DEFINE_PCI_DEVICE_TABLE(igb_pci_tbl) = {
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +000078 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I211_COPPER), board_82575 },
79 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I210_COPPER), board_82575 },
80 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I210_FIBER), board_82575 },
81 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I210_SERDES), board_82575 },
82 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I210_SGMII), board_82575 },
Alexander Duyckd2ba2ed2010-03-22 14:08:06 +000083 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I350_COPPER), board_82575 },
84 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I350_FIBER), board_82575 },
85 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I350_SERDES), board_82575 },
86 { PCI_VDEVICE(INTEL, E1000_DEV_ID_I350_SGMII), board_82575 },
Alexander Duyck55cac242009-11-19 12:42:21 +000087 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_COPPER), board_82575 },
88 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_FIBER), board_82575 },
Carolyn Wyborny6493d242011-01-14 05:33:46 +000089 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_QUAD_FIBER), board_82575 },
Alexander Duyck55cac242009-11-19 12:42:21 +000090 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_SERDES), board_82575 },
91 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_SGMII), board_82575 },
92 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_COPPER_DUAL), board_82575 },
Joseph Gasparakis308fb392010-09-22 17:56:44 +000093 { PCI_VDEVICE(INTEL, E1000_DEV_ID_DH89XXCC_SGMII), board_82575 },
94 { PCI_VDEVICE(INTEL, E1000_DEV_ID_DH89XXCC_SERDES), board_82575 },
Gasparakis, Joseph1b5dda32010-12-09 01:41:01 +000095 { PCI_VDEVICE(INTEL, E1000_DEV_ID_DH89XXCC_BACKPLANE), board_82575 },
96 { PCI_VDEVICE(INTEL, E1000_DEV_ID_DH89XXCC_SFP), board_82575 },
Alexander Duyck2d064c02008-07-08 15:10:12 -070097 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576), board_82575 },
Alexander Duyck9eb23412009-03-13 20:42:15 +000098 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_NS), board_82575 },
Alexander Duyck747d49b2009-10-05 06:33:27 +000099 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_NS_SERDES), board_82575 },
Alexander Duyck2d064c02008-07-08 15:10:12 -0700100 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_FIBER), board_82575 },
101 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_SERDES), board_82575 },
Alexander Duyck4703bf72009-07-23 18:09:48 +0000102 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_SERDES_QUAD), board_82575 },
Carolyn Wybornyb894fa22010-03-19 06:07:48 +0000103 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_QUAD_COPPER_ET2), board_82575 },
Alexander Duyckc8ea5ea2009-03-13 20:42:35 +0000104 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_QUAD_COPPER), board_82575 },
Auke Kok9d5c8242008-01-24 02:22:38 -0800105 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82575EB_COPPER), board_82575 },
106 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82575EB_FIBER_SERDES), board_82575 },
107 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82575GB_QUAD_COPPER), board_82575 },
108 /* required last entry */
109 {0, }
110};
111
112MODULE_DEVICE_TABLE(pci, igb_pci_tbl);
113
114void igb_reset(struct igb_adapter *);
115static int igb_setup_all_tx_resources(struct igb_adapter *);
116static int igb_setup_all_rx_resources(struct igb_adapter *);
117static void igb_free_all_tx_resources(struct igb_adapter *);
118static void igb_free_all_rx_resources(struct igb_adapter *);
Alexander Duyck06cf2662009-10-27 15:53:25 +0000119static void igb_setup_mrqc(struct igb_adapter *);
Auke Kok9d5c8242008-01-24 02:22:38 -0800120static int igb_probe(struct pci_dev *, const struct pci_device_id *);
Bill Pemberton9f9a12f2012-12-03 09:24:25 -0500121static void igb_remove(struct pci_dev *pdev);
Auke Kok9d5c8242008-01-24 02:22:38 -0800122static int igb_sw_init(struct igb_adapter *);
123static int igb_open(struct net_device *);
124static int igb_close(struct net_device *);
125static void igb_configure_tx(struct igb_adapter *);
126static void igb_configure_rx(struct igb_adapter *);
Auke Kok9d5c8242008-01-24 02:22:38 -0800127static void igb_clean_all_tx_rings(struct igb_adapter *);
128static void igb_clean_all_rx_rings(struct igb_adapter *);
Mitch Williams3b644cf2008-06-27 10:59:48 -0700129static void igb_clean_tx_ring(struct igb_ring *);
130static void igb_clean_rx_ring(struct igb_ring *);
Alexander Duyckff41f8d2009-09-03 14:48:56 +0000131static void igb_set_rx_mode(struct net_device *);
Auke Kok9d5c8242008-01-24 02:22:38 -0800132static void igb_update_phy_info(unsigned long);
133static void igb_watchdog(unsigned long);
134static void igb_watchdog_task(struct work_struct *);
Alexander Duyckcd392f52011-08-26 07:43:59 +0000135static netdev_tx_t igb_xmit_frame(struct sk_buff *skb, struct net_device *);
Eric Dumazet12dcd862010-10-15 17:27:10 +0000136static struct rtnl_link_stats64 *igb_get_stats64(struct net_device *dev,
137 struct rtnl_link_stats64 *stats);
Auke Kok9d5c8242008-01-24 02:22:38 -0800138static int igb_change_mtu(struct net_device *, int);
139static int igb_set_mac(struct net_device *, void *);
Alexander Duyck68d480c2009-10-05 06:33:08 +0000140static void igb_set_uta(struct igb_adapter *adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -0800141static irqreturn_t igb_intr(int irq, void *);
142static irqreturn_t igb_intr_msi(int irq, void *);
143static irqreturn_t igb_msix_other(int irq, void *);
Alexander Duyck047e0032009-10-27 15:49:27 +0000144static irqreturn_t igb_msix_ring(int irq, void *);
Jeff Kirsher421e02f2008-10-17 11:08:31 -0700145#ifdef CONFIG_IGB_DCA
Alexander Duyck047e0032009-10-27 15:49:27 +0000146static void igb_update_dca(struct igb_q_vector *);
Jeb Cramerfe4506b2008-07-08 15:07:55 -0700147static void igb_setup_dca(struct igb_adapter *);
Jeff Kirsher421e02f2008-10-17 11:08:31 -0700148#endif /* CONFIG_IGB_DCA */
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -0700149static int igb_poll(struct napi_struct *, int);
Alexander Duyck13fde972011-10-05 13:35:24 +0000150static bool igb_clean_tx_irq(struct igb_q_vector *);
Alexander Duyckcd392f52011-08-26 07:43:59 +0000151static bool igb_clean_rx_irq(struct igb_q_vector *, int);
Auke Kok9d5c8242008-01-24 02:22:38 -0800152static int igb_ioctl(struct net_device *, struct ifreq *, int cmd);
153static void igb_tx_timeout(struct net_device *);
154static void igb_reset_task(struct work_struct *);
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000155static void igb_vlan_mode(struct net_device *netdev, netdev_features_t features);
Jiri Pirko8e586132011-12-08 19:52:37 -0500156static int igb_vlan_rx_add_vid(struct net_device *, u16);
157static int igb_vlan_rx_kill_vid(struct net_device *, u16);
Auke Kok9d5c8242008-01-24 02:22:38 -0800158static void igb_restore_vlan(struct igb_adapter *);
Alexander Duyck26ad9172009-10-05 06:32:49 +0000159static void igb_rar_set_qsel(struct igb_adapter *, u8 *, u32 , u8);
Alexander Duyck4ae196d2009-02-19 20:40:07 -0800160static void igb_ping_all_vfs(struct igb_adapter *);
161static void igb_msg_task(struct igb_adapter *);
Alexander Duyck4ae196d2009-02-19 20:40:07 -0800162static void igb_vmm_control(struct igb_adapter *);
Alexander Duyckf2ca0db2009-10-27 23:46:57 +0000163static int igb_set_vf_mac(struct igb_adapter *, int, unsigned char *);
Alexander Duyck4ae196d2009-02-19 20:40:07 -0800164static void igb_restore_vf_multicasts(struct igb_adapter *adapter);
Williams, Mitch A8151d292010-02-10 01:44:24 +0000165static int igb_ndo_set_vf_mac(struct net_device *netdev, int vf, u8 *mac);
166static int igb_ndo_set_vf_vlan(struct net_device *netdev,
167 int vf, u16 vlan, u8 qos);
168static int igb_ndo_set_vf_bw(struct net_device *netdev, int vf, int tx_rate);
169static int igb_ndo_get_vf_config(struct net_device *netdev, int vf,
170 struct ifla_vf_info *ivi);
Lior Levy17dc5662011-02-08 02:28:46 +0000171static void igb_check_vf_rate_limit(struct igb_adapter *);
RongQing Li46a01692011-10-18 22:52:35 +0000172
173#ifdef CONFIG_PCI_IOV
Greg Rose0224d662011-10-14 02:57:14 +0000174static int igb_vf_configure(struct igb_adapter *adapter, int vf);
Stefan Assmannf5571472012-08-18 04:06:11 +0000175static bool igb_vfs_are_assigned(struct igb_adapter *adapter);
RongQing Li46a01692011-10-18 22:52:35 +0000176#endif
Auke Kok9d5c8242008-01-24 02:22:38 -0800177
Auke Kok9d5c8242008-01-24 02:22:38 -0800178#ifdef CONFIG_PM
Emil Tantilovd9dd9662012-01-28 08:10:35 +0000179#ifdef CONFIG_PM_SLEEP
Yan, Zheng749ab2c2012-01-04 20:23:37 +0000180static int igb_suspend(struct device *);
Emil Tantilovd9dd9662012-01-28 08:10:35 +0000181#endif
Yan, Zheng749ab2c2012-01-04 20:23:37 +0000182static int igb_resume(struct device *);
183#ifdef CONFIG_PM_RUNTIME
184static int igb_runtime_suspend(struct device *dev);
185static int igb_runtime_resume(struct device *dev);
186static int igb_runtime_idle(struct device *dev);
187#endif
188static const struct dev_pm_ops igb_pm_ops = {
189 SET_SYSTEM_SLEEP_PM_OPS(igb_suspend, igb_resume)
190 SET_RUNTIME_PM_OPS(igb_runtime_suspend, igb_runtime_resume,
191 igb_runtime_idle)
192};
Auke Kok9d5c8242008-01-24 02:22:38 -0800193#endif
194static void igb_shutdown(struct pci_dev *);
Jeff Kirsher421e02f2008-10-17 11:08:31 -0700195#ifdef CONFIG_IGB_DCA
Jeb Cramerfe4506b2008-07-08 15:07:55 -0700196static int igb_notify_dca(struct notifier_block *, unsigned long, void *);
197static struct notifier_block dca_notifier = {
198 .notifier_call = igb_notify_dca,
199 .next = NULL,
200 .priority = 0
201};
202#endif
Auke Kok9d5c8242008-01-24 02:22:38 -0800203#ifdef CONFIG_NET_POLL_CONTROLLER
204/* for netdump / net console */
205static void igb_netpoll(struct net_device *);
206#endif
Alexander Duyck37680112009-02-19 20:40:30 -0800207#ifdef CONFIG_PCI_IOV
Alexander Duyck2a3abf62009-04-07 14:37:52 +0000208static unsigned int max_vfs = 0;
209module_param(max_vfs, uint, 0);
210MODULE_PARM_DESC(max_vfs, "Maximum number of virtual functions to allocate "
211 "per physical function");
212#endif /* CONFIG_PCI_IOV */
213
Auke Kok9d5c8242008-01-24 02:22:38 -0800214static pci_ers_result_t igb_io_error_detected(struct pci_dev *,
215 pci_channel_state_t);
216static pci_ers_result_t igb_io_slot_reset(struct pci_dev *);
217static void igb_io_resume(struct pci_dev *);
218
Stephen Hemminger3646f0e2012-09-07 09:33:15 -0700219static const struct pci_error_handlers igb_err_handler = {
Auke Kok9d5c8242008-01-24 02:22:38 -0800220 .error_detected = igb_io_error_detected,
221 .slot_reset = igb_io_slot_reset,
222 .resume = igb_io_resume,
223};
224
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +0000225static void igb_init_dmac(struct igb_adapter *adapter, u32 pba);
Auke Kok9d5c8242008-01-24 02:22:38 -0800226
227static struct pci_driver igb_driver = {
228 .name = igb_driver_name,
229 .id_table = igb_pci_tbl,
230 .probe = igb_probe,
Bill Pemberton9f9a12f2012-12-03 09:24:25 -0500231 .remove = igb_remove,
Auke Kok9d5c8242008-01-24 02:22:38 -0800232#ifdef CONFIG_PM
Yan, Zheng749ab2c2012-01-04 20:23:37 +0000233 .driver.pm = &igb_pm_ops,
Auke Kok9d5c8242008-01-24 02:22:38 -0800234#endif
235 .shutdown = igb_shutdown,
236 .err_handler = &igb_err_handler
237};
238
239MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
240MODULE_DESCRIPTION("Intel(R) Gigabit Ethernet Network Driver");
241MODULE_LICENSE("GPL");
242MODULE_VERSION(DRV_VERSION);
243
stephen hemmingerb3f4d592012-03-13 06:04:20 +0000244#define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK)
245static int debug = -1;
246module_param(debug, int, 0);
247MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
248
Taku Izumic97ec422010-04-27 14:39:30 +0000249struct igb_reg_info {
250 u32 ofs;
251 char *name;
252};
253
254static const struct igb_reg_info igb_reg_info_tbl[] = {
255
256 /* General Registers */
257 {E1000_CTRL, "CTRL"},
258 {E1000_STATUS, "STATUS"},
259 {E1000_CTRL_EXT, "CTRL_EXT"},
260
261 /* Interrupt Registers */
262 {E1000_ICR, "ICR"},
263
264 /* RX Registers */
265 {E1000_RCTL, "RCTL"},
266 {E1000_RDLEN(0), "RDLEN"},
267 {E1000_RDH(0), "RDH"},
268 {E1000_RDT(0), "RDT"},
269 {E1000_RXDCTL(0), "RXDCTL"},
270 {E1000_RDBAL(0), "RDBAL"},
271 {E1000_RDBAH(0), "RDBAH"},
272
273 /* TX Registers */
274 {E1000_TCTL, "TCTL"},
275 {E1000_TDBAL(0), "TDBAL"},
276 {E1000_TDBAH(0), "TDBAH"},
277 {E1000_TDLEN(0), "TDLEN"},
278 {E1000_TDH(0), "TDH"},
279 {E1000_TDT(0), "TDT"},
280 {E1000_TXDCTL(0), "TXDCTL"},
281 {E1000_TDFH, "TDFH"},
282 {E1000_TDFT, "TDFT"},
283 {E1000_TDFHS, "TDFHS"},
284 {E1000_TDFPC, "TDFPC"},
285
286 /* List Terminator */
287 {}
288};
289
290/*
291 * igb_regdump - register printout routine
292 */
293static void igb_regdump(struct e1000_hw *hw, struct igb_reg_info *reginfo)
294{
295 int n = 0;
296 char rname[16];
297 u32 regs[8];
298
299 switch (reginfo->ofs) {
300 case E1000_RDLEN(0):
301 for (n = 0; n < 4; n++)
302 regs[n] = rd32(E1000_RDLEN(n));
303 break;
304 case E1000_RDH(0):
305 for (n = 0; n < 4; n++)
306 regs[n] = rd32(E1000_RDH(n));
307 break;
308 case E1000_RDT(0):
309 for (n = 0; n < 4; n++)
310 regs[n] = rd32(E1000_RDT(n));
311 break;
312 case E1000_RXDCTL(0):
313 for (n = 0; n < 4; n++)
314 regs[n] = rd32(E1000_RXDCTL(n));
315 break;
316 case E1000_RDBAL(0):
317 for (n = 0; n < 4; n++)
318 regs[n] = rd32(E1000_RDBAL(n));
319 break;
320 case E1000_RDBAH(0):
321 for (n = 0; n < 4; n++)
322 regs[n] = rd32(E1000_RDBAH(n));
323 break;
324 case E1000_TDBAL(0):
325 for (n = 0; n < 4; n++)
326 regs[n] = rd32(E1000_RDBAL(n));
327 break;
328 case E1000_TDBAH(0):
329 for (n = 0; n < 4; n++)
330 regs[n] = rd32(E1000_TDBAH(n));
331 break;
332 case E1000_TDLEN(0):
333 for (n = 0; n < 4; n++)
334 regs[n] = rd32(E1000_TDLEN(n));
335 break;
336 case E1000_TDH(0):
337 for (n = 0; n < 4; n++)
338 regs[n] = rd32(E1000_TDH(n));
339 break;
340 case E1000_TDT(0):
341 for (n = 0; n < 4; n++)
342 regs[n] = rd32(E1000_TDT(n));
343 break;
344 case E1000_TXDCTL(0):
345 for (n = 0; n < 4; n++)
346 regs[n] = rd32(E1000_TXDCTL(n));
347 break;
348 default:
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000349 pr_info("%-15s %08x\n", reginfo->name, rd32(reginfo->ofs));
Taku Izumic97ec422010-04-27 14:39:30 +0000350 return;
351 }
352
353 snprintf(rname, 16, "%s%s", reginfo->name, "[0-3]");
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000354 pr_info("%-15s %08x %08x %08x %08x\n", rname, regs[0], regs[1],
355 regs[2], regs[3]);
Taku Izumic97ec422010-04-27 14:39:30 +0000356}
357
358/*
359 * igb_dump - Print registers, tx-rings and rx-rings
360 */
361static void igb_dump(struct igb_adapter *adapter)
362{
363 struct net_device *netdev = adapter->netdev;
364 struct e1000_hw *hw = &adapter->hw;
365 struct igb_reg_info *reginfo;
Taku Izumic97ec422010-04-27 14:39:30 +0000366 struct igb_ring *tx_ring;
367 union e1000_adv_tx_desc *tx_desc;
368 struct my_u0 { u64 a; u64 b; } *u0;
Taku Izumic97ec422010-04-27 14:39:30 +0000369 struct igb_ring *rx_ring;
370 union e1000_adv_rx_desc *rx_desc;
371 u32 staterr;
Alexander Duyck6ad4edf2011-08-26 07:45:26 +0000372 u16 i, n;
Taku Izumic97ec422010-04-27 14:39:30 +0000373
374 if (!netif_msg_hw(adapter))
375 return;
376
377 /* Print netdevice Info */
378 if (netdev) {
379 dev_info(&adapter->pdev->dev, "Net device Info\n");
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000380 pr_info("Device Name state trans_start "
381 "last_rx\n");
382 pr_info("%-15s %016lX %016lX %016lX\n", netdev->name,
383 netdev->state, netdev->trans_start, netdev->last_rx);
Taku Izumic97ec422010-04-27 14:39:30 +0000384 }
385
386 /* Print Registers */
387 dev_info(&adapter->pdev->dev, "Register Dump\n");
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000388 pr_info(" Register Name Value\n");
Taku Izumic97ec422010-04-27 14:39:30 +0000389 for (reginfo = (struct igb_reg_info *)igb_reg_info_tbl;
390 reginfo->name; reginfo++) {
391 igb_regdump(hw, reginfo);
392 }
393
394 /* Print TX Ring Summary */
395 if (!netdev || !netif_running(netdev))
396 goto exit;
397
398 dev_info(&adapter->pdev->dev, "TX Rings Summary\n");
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000399 pr_info("Queue [NTU] [NTC] [bi(ntc)->dma ] leng ntw timestamp\n");
Taku Izumic97ec422010-04-27 14:39:30 +0000400 for (n = 0; n < adapter->num_tx_queues; n++) {
Alexander Duyck06034642011-08-26 07:44:22 +0000401 struct igb_tx_buffer *buffer_info;
Taku Izumic97ec422010-04-27 14:39:30 +0000402 tx_ring = adapter->tx_ring[n];
Alexander Duyck06034642011-08-26 07:44:22 +0000403 buffer_info = &tx_ring->tx_buffer_info[tx_ring->next_to_clean];
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000404 pr_info(" %5d %5X %5X %016llX %04X %p %016llX\n",
405 n, tx_ring->next_to_use, tx_ring->next_to_clean,
Alexander Duyckc9f14bf32012-09-18 01:56:27 +0000406 (u64)dma_unmap_addr(buffer_info, dma),
407 dma_unmap_len(buffer_info, len),
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000408 buffer_info->next_to_watch,
409 (u64)buffer_info->time_stamp);
Taku Izumic97ec422010-04-27 14:39:30 +0000410 }
411
412 /* Print TX Rings */
413 if (!netif_msg_tx_done(adapter))
414 goto rx_ring_summary;
415
416 dev_info(&adapter->pdev->dev, "TX Rings Dump\n");
417
418 /* Transmit Descriptor Formats
419 *
420 * Advanced Transmit Descriptor
421 * +--------------------------------------------------------------+
422 * 0 | Buffer Address [63:0] |
423 * +--------------------------------------------------------------+
424 * 8 | PAYLEN | PORTS |CC|IDX | STA | DCMD |DTYP|MAC|RSV| DTALEN |
425 * +--------------------------------------------------------------+
426 * 63 46 45 40 39 38 36 35 32 31 24 15 0
427 */
428
429 for (n = 0; n < adapter->num_tx_queues; n++) {
430 tx_ring = adapter->tx_ring[n];
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000431 pr_info("------------------------------------\n");
432 pr_info("TX QUEUE INDEX = %d\n", tx_ring->queue_index);
433 pr_info("------------------------------------\n");
434 pr_info("T [desc] [address 63:0 ] [PlPOCIStDDM Ln] "
435 "[bi->dma ] leng ntw timestamp "
436 "bi->skb\n");
Taku Izumic97ec422010-04-27 14:39:30 +0000437
438 for (i = 0; tx_ring->desc && (i < tx_ring->count); i++) {
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000439 const char *next_desc;
Alexander Duyck06034642011-08-26 07:44:22 +0000440 struct igb_tx_buffer *buffer_info;
Alexander Duyck601369062011-08-26 07:44:05 +0000441 tx_desc = IGB_TX_DESC(tx_ring, i);
Alexander Duyck06034642011-08-26 07:44:22 +0000442 buffer_info = &tx_ring->tx_buffer_info[i];
Taku Izumic97ec422010-04-27 14:39:30 +0000443 u0 = (struct my_u0 *)tx_desc;
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000444 if (i == tx_ring->next_to_use &&
445 i == tx_ring->next_to_clean)
446 next_desc = " NTC/U";
447 else if (i == tx_ring->next_to_use)
448 next_desc = " NTU";
449 else if (i == tx_ring->next_to_clean)
450 next_desc = " NTC";
451 else
452 next_desc = "";
453
454 pr_info("T [0x%03X] %016llX %016llX %016llX"
455 " %04X %p %016llX %p%s\n", i,
Taku Izumic97ec422010-04-27 14:39:30 +0000456 le64_to_cpu(u0->a),
457 le64_to_cpu(u0->b),
Alexander Duyckc9f14bf32012-09-18 01:56:27 +0000458 (u64)dma_unmap_addr(buffer_info, dma),
459 dma_unmap_len(buffer_info, len),
Taku Izumic97ec422010-04-27 14:39:30 +0000460 buffer_info->next_to_watch,
461 (u64)buffer_info->time_stamp,
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000462 buffer_info->skb, next_desc);
Taku Izumic97ec422010-04-27 14:39:30 +0000463
Emil Tantilovb6695882012-07-28 05:07:48 +0000464 if (netif_msg_pktdata(adapter) && buffer_info->skb)
Taku Izumic97ec422010-04-27 14:39:30 +0000465 print_hex_dump(KERN_INFO, "",
466 DUMP_PREFIX_ADDRESS,
Emil Tantilovb6695882012-07-28 05:07:48 +0000467 16, 1, buffer_info->skb->data,
Alexander Duyckc9f14bf32012-09-18 01:56:27 +0000468 dma_unmap_len(buffer_info, len),
469 true);
Taku Izumic97ec422010-04-27 14:39:30 +0000470 }
471 }
472
473 /* Print RX Rings Summary */
474rx_ring_summary:
475 dev_info(&adapter->pdev->dev, "RX Rings Summary\n");
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000476 pr_info("Queue [NTU] [NTC]\n");
Taku Izumic97ec422010-04-27 14:39:30 +0000477 for (n = 0; n < adapter->num_rx_queues; n++) {
478 rx_ring = adapter->rx_ring[n];
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000479 pr_info(" %5d %5X %5X\n",
480 n, rx_ring->next_to_use, rx_ring->next_to_clean);
Taku Izumic97ec422010-04-27 14:39:30 +0000481 }
482
483 /* Print RX Rings */
484 if (!netif_msg_rx_status(adapter))
485 goto exit;
486
487 dev_info(&adapter->pdev->dev, "RX Rings Dump\n");
488
489 /* Advanced Receive Descriptor (Read) Format
490 * 63 1 0
491 * +-----------------------------------------------------+
492 * 0 | Packet Buffer Address [63:1] |A0/NSE|
493 * +----------------------------------------------+------+
494 * 8 | Header Buffer Address [63:1] | DD |
495 * +-----------------------------------------------------+
496 *
497 *
498 * Advanced Receive Descriptor (Write-Back) Format
499 *
500 * 63 48 47 32 31 30 21 20 17 16 4 3 0
501 * +------------------------------------------------------+
502 * 0 | Packet IP |SPH| HDR_LEN | RSV|Packet| RSS |
503 * | Checksum Ident | | | | Type | Type |
504 * +------------------------------------------------------+
505 * 8 | VLAN Tag | Length | Extended Error | Extended Status |
506 * +------------------------------------------------------+
507 * 63 48 47 32 31 20 19 0
508 */
509
510 for (n = 0; n < adapter->num_rx_queues; n++) {
511 rx_ring = adapter->rx_ring[n];
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000512 pr_info("------------------------------------\n");
513 pr_info("RX QUEUE INDEX = %d\n", rx_ring->queue_index);
514 pr_info("------------------------------------\n");
515 pr_info("R [desc] [ PktBuf A0] [ HeadBuf DD] "
516 "[bi->dma ] [bi->skb] <-- Adv Rx Read format\n");
517 pr_info("RWB[desc] [PcsmIpSHl PtRs] [vl er S cks ln] -----"
518 "----------- [bi->skb] <-- Adv Rx Write-Back format\n");
Taku Izumic97ec422010-04-27 14:39:30 +0000519
520 for (i = 0; i < rx_ring->count; i++) {
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000521 const char *next_desc;
Alexander Duyck06034642011-08-26 07:44:22 +0000522 struct igb_rx_buffer *buffer_info;
523 buffer_info = &rx_ring->rx_buffer_info[i];
Alexander Duyck601369062011-08-26 07:44:05 +0000524 rx_desc = IGB_RX_DESC(rx_ring, i);
Taku Izumic97ec422010-04-27 14:39:30 +0000525 u0 = (struct my_u0 *)rx_desc;
526 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000527
528 if (i == rx_ring->next_to_use)
529 next_desc = " NTU";
530 else if (i == rx_ring->next_to_clean)
531 next_desc = " NTC";
532 else
533 next_desc = "";
534
Taku Izumic97ec422010-04-27 14:39:30 +0000535 if (staterr & E1000_RXD_STAT_DD) {
536 /* Descriptor Done */
Alexander Duyck1a1c2252012-09-25 00:30:52 +0000537 pr_info("%s[0x%03X] %016llX %016llX ---------------- %s\n",
538 "RWB", i,
Taku Izumic97ec422010-04-27 14:39:30 +0000539 le64_to_cpu(u0->a),
540 le64_to_cpu(u0->b),
Alexander Duyck1a1c2252012-09-25 00:30:52 +0000541 next_desc);
Taku Izumic97ec422010-04-27 14:39:30 +0000542 } else {
Alexander Duyck1a1c2252012-09-25 00:30:52 +0000543 pr_info("%s[0x%03X] %016llX %016llX %016llX %s\n",
544 "R ", i,
Taku Izumic97ec422010-04-27 14:39:30 +0000545 le64_to_cpu(u0->a),
546 le64_to_cpu(u0->b),
547 (u64)buffer_info->dma,
Alexander Duyck1a1c2252012-09-25 00:30:52 +0000548 next_desc);
Taku Izumic97ec422010-04-27 14:39:30 +0000549
Emil Tantilovb6695882012-07-28 05:07:48 +0000550 if (netif_msg_pktdata(adapter) &&
Alexander Duyck1a1c2252012-09-25 00:30:52 +0000551 buffer_info->dma && buffer_info->page) {
Alexander Duyck44390ca2011-08-26 07:43:38 +0000552 print_hex_dump(KERN_INFO, "",
553 DUMP_PREFIX_ADDRESS,
554 16, 1,
Emil Tantilovb6695882012-07-28 05:07:48 +0000555 page_address(buffer_info->page) +
556 buffer_info->page_offset,
Alexander Duyckde78d1f2012-09-25 00:31:12 +0000557 IGB_RX_BUFSZ, true);
Taku Izumic97ec422010-04-27 14:39:30 +0000558 }
559 }
Taku Izumic97ec422010-04-27 14:39:30 +0000560 }
561 }
562
563exit:
564 return;
565}
566
Auke Kok9d5c8242008-01-24 02:22:38 -0800567/**
Alexander Duyckc0410762010-03-25 13:10:08 +0000568 * igb_get_hw_dev - return device
Auke Kok9d5c8242008-01-24 02:22:38 -0800569 * used by hardware layer to print debugging information
570 **/
Alexander Duyckc0410762010-03-25 13:10:08 +0000571struct net_device *igb_get_hw_dev(struct e1000_hw *hw)
Auke Kok9d5c8242008-01-24 02:22:38 -0800572{
573 struct igb_adapter *adapter = hw->back;
Alexander Duyckc0410762010-03-25 13:10:08 +0000574 return adapter->netdev;
Auke Kok9d5c8242008-01-24 02:22:38 -0800575}
Patrick Ohly38c845c2009-02-12 05:03:41 +0000576
577/**
Auke Kok9d5c8242008-01-24 02:22:38 -0800578 * igb_init_module - Driver Registration Routine
579 *
580 * igb_init_module is the first routine called when the driver is
581 * loaded. All it does is register with the PCI subsystem.
582 **/
583static int __init igb_init_module(void)
584{
585 int ret;
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000586 pr_info("%s - version %s\n",
Auke Kok9d5c8242008-01-24 02:22:38 -0800587 igb_driver_string, igb_driver_version);
588
Jeff Kirsher876d2d62011-10-21 20:01:34 +0000589 pr_info("%s\n", igb_copyright);
Auke Kok9d5c8242008-01-24 02:22:38 -0800590
Jeff Kirsher421e02f2008-10-17 11:08:31 -0700591#ifdef CONFIG_IGB_DCA
Jeb Cramerfe4506b2008-07-08 15:07:55 -0700592 dca_register_notify(&dca_notifier);
593#endif
Alexander Duyckbbd98fe2009-01-31 00:52:30 -0800594 ret = pci_register_driver(&igb_driver);
Auke Kok9d5c8242008-01-24 02:22:38 -0800595 return ret;
596}
597
598module_init(igb_init_module);
599
600/**
601 * igb_exit_module - Driver Exit Cleanup Routine
602 *
603 * igb_exit_module is called just before the driver is removed
604 * from memory.
605 **/
606static void __exit igb_exit_module(void)
607{
Jeff Kirsher421e02f2008-10-17 11:08:31 -0700608#ifdef CONFIG_IGB_DCA
Jeb Cramerfe4506b2008-07-08 15:07:55 -0700609 dca_unregister_notify(&dca_notifier);
610#endif
Auke Kok9d5c8242008-01-24 02:22:38 -0800611 pci_unregister_driver(&igb_driver);
612}
613
614module_exit(igb_exit_module);
615
Alexander Duyck26bc19e2008-12-26 01:34:11 -0800616#define Q_IDX_82576(i) (((i & 0x1) << 3) + (i >> 1))
617/**
618 * igb_cache_ring_register - Descriptor ring to register mapping
619 * @adapter: board private structure to initialize
620 *
621 * Once we know the feature-set enabled for the device, we'll cache
622 * the register offset the descriptor ring is assigned to.
623 **/
624static void igb_cache_ring_register(struct igb_adapter *adapter)
625{
Alexander Duyckee1b9f02009-10-27 23:49:40 +0000626 int i = 0, j = 0;
Alexander Duyck047e0032009-10-27 15:49:27 +0000627 u32 rbase_offset = adapter->vfs_allocated_count;
Alexander Duyck26bc19e2008-12-26 01:34:11 -0800628
629 switch (adapter->hw.mac.type) {
630 case e1000_82576:
631 /* The queues are allocated for virtualization such that VF 0
632 * is allocated queues 0 and 8, VF 1 queues 1 and 9, etc.
633 * In order to avoid collision we start at the first free queue
634 * and continue consuming queues in the same sequence
635 */
Alexander Duyckee1b9f02009-10-27 23:49:40 +0000636 if (adapter->vfs_allocated_count) {
Alexander Duycka99955f2009-11-12 18:37:19 +0000637 for (; i < adapter->rss_queues; i++)
Alexander Duyck3025a442010-02-17 01:02:39 +0000638 adapter->rx_ring[i]->reg_idx = rbase_offset +
639 Q_IDX_82576(i);
Alexander Duyckee1b9f02009-10-27 23:49:40 +0000640 }
Alexander Duyck26bc19e2008-12-26 01:34:11 -0800641 case e1000_82575:
Alexander Duyck55cac242009-11-19 12:42:21 +0000642 case e1000_82580:
Alexander Duyckd2ba2ed2010-03-22 14:08:06 +0000643 case e1000_i350:
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +0000644 case e1000_i210:
645 case e1000_i211:
Alexander Duyck26bc19e2008-12-26 01:34:11 -0800646 default:
Alexander Duyckee1b9f02009-10-27 23:49:40 +0000647 for (; i < adapter->num_rx_queues; i++)
Alexander Duyck3025a442010-02-17 01:02:39 +0000648 adapter->rx_ring[i]->reg_idx = rbase_offset + i;
Alexander Duyckee1b9f02009-10-27 23:49:40 +0000649 for (; j < adapter->num_tx_queues; j++)
Alexander Duyck3025a442010-02-17 01:02:39 +0000650 adapter->tx_ring[j]->reg_idx = rbase_offset + j;
Alexander Duyck26bc19e2008-12-26 01:34:11 -0800651 break;
652 }
653}
654
Alexander Duyck4be000c2011-08-26 07:45:52 +0000655/**
656 * igb_write_ivar - configure ivar for given MSI-X vector
657 * @hw: pointer to the HW structure
658 * @msix_vector: vector number we are allocating to a given ring
659 * @index: row index of IVAR register to write within IVAR table
660 * @offset: column offset of in IVAR, should be multiple of 8
661 *
662 * This function is intended to handle the writing of the IVAR register
663 * for adapters 82576 and newer. The IVAR table consists of 2 columns,
664 * each containing an cause allocation for an Rx and Tx ring, and a
665 * variable number of rows depending on the number of queues supported.
666 **/
667static void igb_write_ivar(struct e1000_hw *hw, int msix_vector,
668 int index, int offset)
669{
670 u32 ivar = array_rd32(E1000_IVAR0, index);
671
672 /* clear any bits that are currently set */
673 ivar &= ~((u32)0xFF << offset);
674
675 /* write vector and valid bit */
676 ivar |= (msix_vector | E1000_IVAR_VALID) << offset;
677
678 array_wr32(E1000_IVAR0, index, ivar);
679}
680
Auke Kok9d5c8242008-01-24 02:22:38 -0800681#define IGB_N0_QUEUE -1
Alexander Duyck047e0032009-10-27 15:49:27 +0000682static void igb_assign_vector(struct igb_q_vector *q_vector, int msix_vector)
Auke Kok9d5c8242008-01-24 02:22:38 -0800683{
Alexander Duyck047e0032009-10-27 15:49:27 +0000684 struct igb_adapter *adapter = q_vector->adapter;
Auke Kok9d5c8242008-01-24 02:22:38 -0800685 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck047e0032009-10-27 15:49:27 +0000686 int rx_queue = IGB_N0_QUEUE;
687 int tx_queue = IGB_N0_QUEUE;
Alexander Duyck4be000c2011-08-26 07:45:52 +0000688 u32 msixbm = 0;
Alexander Duyck047e0032009-10-27 15:49:27 +0000689
Alexander Duyck0ba82992011-08-26 07:45:47 +0000690 if (q_vector->rx.ring)
691 rx_queue = q_vector->rx.ring->reg_idx;
692 if (q_vector->tx.ring)
693 tx_queue = q_vector->tx.ring->reg_idx;
Alexander Duyck2d064c02008-07-08 15:10:12 -0700694
695 switch (hw->mac.type) {
696 case e1000_82575:
Auke Kok9d5c8242008-01-24 02:22:38 -0800697 /* The 82575 assigns vectors using a bitmask, which matches the
698 bitmask for the EICR/EIMS/EIMC registers. To assign one
699 or more queues to a vector, we write the appropriate bits
700 into the MSIXBM register for that vector. */
Alexander Duyck047e0032009-10-27 15:49:27 +0000701 if (rx_queue > IGB_N0_QUEUE)
Auke Kok9d5c8242008-01-24 02:22:38 -0800702 msixbm = E1000_EICR_RX_QUEUE0 << rx_queue;
Alexander Duyck047e0032009-10-27 15:49:27 +0000703 if (tx_queue > IGB_N0_QUEUE)
Auke Kok9d5c8242008-01-24 02:22:38 -0800704 msixbm |= E1000_EICR_TX_QUEUE0 << tx_queue;
Alexander Duyckfeeb2722010-02-03 21:59:51 +0000705 if (!adapter->msix_entries && msix_vector == 0)
706 msixbm |= E1000_EIMS_OTHER;
Auke Kok9d5c8242008-01-24 02:22:38 -0800707 array_wr32(E1000_MSIXBM(0), msix_vector, msixbm);
Alexander Duyck047e0032009-10-27 15:49:27 +0000708 q_vector->eims_value = msixbm;
Alexander Duyck2d064c02008-07-08 15:10:12 -0700709 break;
710 case e1000_82576:
Alexander Duyck4be000c2011-08-26 07:45:52 +0000711 /*
712 * 82576 uses a table that essentially consists of 2 columns
713 * with 8 rows. The ordering is column-major so we use the
714 * lower 3 bits as the row index, and the 4th bit as the
715 * column offset.
716 */
717 if (rx_queue > IGB_N0_QUEUE)
718 igb_write_ivar(hw, msix_vector,
719 rx_queue & 0x7,
720 (rx_queue & 0x8) << 1);
721 if (tx_queue > IGB_N0_QUEUE)
722 igb_write_ivar(hw, msix_vector,
723 tx_queue & 0x7,
724 ((tx_queue & 0x8) << 1) + 8);
Alexander Duyck047e0032009-10-27 15:49:27 +0000725 q_vector->eims_value = 1 << msix_vector;
Alexander Duyck2d064c02008-07-08 15:10:12 -0700726 break;
Alexander Duyck55cac242009-11-19 12:42:21 +0000727 case e1000_82580:
Alexander Duyckd2ba2ed2010-03-22 14:08:06 +0000728 case e1000_i350:
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +0000729 case e1000_i210:
730 case e1000_i211:
Alexander Duyck4be000c2011-08-26 07:45:52 +0000731 /*
732 * On 82580 and newer adapters the scheme is similar to 82576
733 * however instead of ordering column-major we have things
734 * ordered row-major. So we traverse the table by using
735 * bit 0 as the column offset, and the remaining bits as the
736 * row index.
737 */
738 if (rx_queue > IGB_N0_QUEUE)
739 igb_write_ivar(hw, msix_vector,
740 rx_queue >> 1,
741 (rx_queue & 0x1) << 4);
742 if (tx_queue > IGB_N0_QUEUE)
743 igb_write_ivar(hw, msix_vector,
744 tx_queue >> 1,
745 ((tx_queue & 0x1) << 4) + 8);
Alexander Duyck55cac242009-11-19 12:42:21 +0000746 q_vector->eims_value = 1 << msix_vector;
747 break;
Alexander Duyck2d064c02008-07-08 15:10:12 -0700748 default:
749 BUG();
750 break;
751 }
Alexander Duyck26b39272010-02-17 01:00:41 +0000752
753 /* add q_vector eims value to global eims_enable_mask */
754 adapter->eims_enable_mask |= q_vector->eims_value;
755
756 /* configure q_vector to set itr on first interrupt */
757 q_vector->set_itr = 1;
Auke Kok9d5c8242008-01-24 02:22:38 -0800758}
759
760/**
761 * igb_configure_msix - Configure MSI-X hardware
762 *
763 * igb_configure_msix sets up the hardware to properly
764 * generate MSI-X interrupts.
765 **/
766static void igb_configure_msix(struct igb_adapter *adapter)
767{
768 u32 tmp;
769 int i, vector = 0;
770 struct e1000_hw *hw = &adapter->hw;
771
772 adapter->eims_enable_mask = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -0800773
774 /* set vector for other causes, i.e. link changes */
Alexander Duyck2d064c02008-07-08 15:10:12 -0700775 switch (hw->mac.type) {
776 case e1000_82575:
Auke Kok9d5c8242008-01-24 02:22:38 -0800777 tmp = rd32(E1000_CTRL_EXT);
778 /* enable MSI-X PBA support*/
779 tmp |= E1000_CTRL_EXT_PBA_CLR;
780
781 /* Auto-Mask interrupts upon ICR read. */
782 tmp |= E1000_CTRL_EXT_EIAME;
783 tmp |= E1000_CTRL_EXT_IRCA;
784
785 wr32(E1000_CTRL_EXT, tmp);
Alexander Duyck047e0032009-10-27 15:49:27 +0000786
787 /* enable msix_other interrupt */
788 array_wr32(E1000_MSIXBM(0), vector++,
789 E1000_EIMS_OTHER);
PJ Waskiewicz844290e2008-06-27 11:00:39 -0700790 adapter->eims_other = E1000_EIMS_OTHER;
Auke Kok9d5c8242008-01-24 02:22:38 -0800791
Alexander Duyck2d064c02008-07-08 15:10:12 -0700792 break;
793
794 case e1000_82576:
Alexander Duyck55cac242009-11-19 12:42:21 +0000795 case e1000_82580:
Alexander Duyckd2ba2ed2010-03-22 14:08:06 +0000796 case e1000_i350:
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +0000797 case e1000_i210:
798 case e1000_i211:
Alexander Duyck047e0032009-10-27 15:49:27 +0000799 /* Turn on MSI-X capability first, or our settings
800 * won't stick. And it will take days to debug. */
801 wr32(E1000_GPIE, E1000_GPIE_MSIX_MODE |
802 E1000_GPIE_PBA | E1000_GPIE_EIAME |
803 E1000_GPIE_NSICR);
Alexander Duyck2d064c02008-07-08 15:10:12 -0700804
Alexander Duyck047e0032009-10-27 15:49:27 +0000805 /* enable msix_other interrupt */
806 adapter->eims_other = 1 << vector;
807 tmp = (vector++ | E1000_IVAR_VALID) << 8;
808
809 wr32(E1000_IVAR_MISC, tmp);
Alexander Duyck2d064c02008-07-08 15:10:12 -0700810 break;
811 default:
812 /* do nothing, since nothing else supports MSI-X */
813 break;
814 } /* switch (hw->mac.type) */
Alexander Duyck047e0032009-10-27 15:49:27 +0000815
816 adapter->eims_enable_mask |= adapter->eims_other;
817
Alexander Duyck26b39272010-02-17 01:00:41 +0000818 for (i = 0; i < adapter->num_q_vectors; i++)
819 igb_assign_vector(adapter->q_vector[i], vector++);
Alexander Duyck047e0032009-10-27 15:49:27 +0000820
Auke Kok9d5c8242008-01-24 02:22:38 -0800821 wrfl();
822}
823
824/**
825 * igb_request_msix - Initialize MSI-X interrupts
826 *
827 * igb_request_msix allocates MSI-X vectors and requests interrupts from the
828 * kernel.
829 **/
830static int igb_request_msix(struct igb_adapter *adapter)
831{
832 struct net_device *netdev = adapter->netdev;
Alexander Duyck047e0032009-10-27 15:49:27 +0000833 struct e1000_hw *hw = &adapter->hw;
Auke Kok9d5c8242008-01-24 02:22:38 -0800834 int i, err = 0, vector = 0;
835
Auke Kok9d5c8242008-01-24 02:22:38 -0800836 err = request_irq(adapter->msix_entries[vector].vector,
Joe Perchesa0607fd2009-11-18 23:29:17 -0800837 igb_msix_other, 0, netdev->name, adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -0800838 if (err)
839 goto out;
Alexander Duyck047e0032009-10-27 15:49:27 +0000840 vector++;
841
842 for (i = 0; i < adapter->num_q_vectors; i++) {
843 struct igb_q_vector *q_vector = adapter->q_vector[i];
844
845 q_vector->itr_register = hw->hw_addr + E1000_EITR(vector);
846
Alexander Duyck0ba82992011-08-26 07:45:47 +0000847 if (q_vector->rx.ring && q_vector->tx.ring)
Alexander Duyck047e0032009-10-27 15:49:27 +0000848 sprintf(q_vector->name, "%s-TxRx-%u", netdev->name,
Alexander Duyck0ba82992011-08-26 07:45:47 +0000849 q_vector->rx.ring->queue_index);
850 else if (q_vector->tx.ring)
Alexander Duyck047e0032009-10-27 15:49:27 +0000851 sprintf(q_vector->name, "%s-tx-%u", netdev->name,
Alexander Duyck0ba82992011-08-26 07:45:47 +0000852 q_vector->tx.ring->queue_index);
853 else if (q_vector->rx.ring)
Alexander Duyck047e0032009-10-27 15:49:27 +0000854 sprintf(q_vector->name, "%s-rx-%u", netdev->name,
Alexander Duyck0ba82992011-08-26 07:45:47 +0000855 q_vector->rx.ring->queue_index);
Alexander Duyck047e0032009-10-27 15:49:27 +0000856 else
857 sprintf(q_vector->name, "%s-unused", netdev->name);
858
859 err = request_irq(adapter->msix_entries[vector].vector,
Joe Perchesa0607fd2009-11-18 23:29:17 -0800860 igb_msix_ring, 0, q_vector->name,
Alexander Duyck047e0032009-10-27 15:49:27 +0000861 q_vector);
862 if (err)
863 goto out;
864 vector++;
865 }
Auke Kok9d5c8242008-01-24 02:22:38 -0800866
Auke Kok9d5c8242008-01-24 02:22:38 -0800867 igb_configure_msix(adapter);
868 return 0;
869out:
870 return err;
871}
872
873static void igb_reset_interrupt_capability(struct igb_adapter *adapter)
874{
875 if (adapter->msix_entries) {
876 pci_disable_msix(adapter->pdev);
877 kfree(adapter->msix_entries);
878 adapter->msix_entries = NULL;
Alexander Duyck047e0032009-10-27 15:49:27 +0000879 } else if (adapter->flags & IGB_FLAG_HAS_MSI) {
Auke Kok9d5c8242008-01-24 02:22:38 -0800880 pci_disable_msi(adapter->pdev);
Alexander Duyck047e0032009-10-27 15:49:27 +0000881 }
Auke Kok9d5c8242008-01-24 02:22:38 -0800882}
883
Alexander Duyck047e0032009-10-27 15:49:27 +0000884/**
Alexander Duyck5536d212012-09-25 00:31:17 +0000885 * igb_free_q_vector - Free memory allocated for specific interrupt vector
886 * @adapter: board private structure to initialize
887 * @v_idx: Index of vector to be freed
888 *
889 * This function frees the memory allocated to the q_vector. In addition if
890 * NAPI is enabled it will delete any references to the NAPI struct prior
891 * to freeing the q_vector.
892 **/
893static void igb_free_q_vector(struct igb_adapter *adapter, int v_idx)
894{
895 struct igb_q_vector *q_vector = adapter->q_vector[v_idx];
896
897 if (q_vector->tx.ring)
898 adapter->tx_ring[q_vector->tx.ring->queue_index] = NULL;
899
900 if (q_vector->rx.ring)
901 adapter->tx_ring[q_vector->rx.ring->queue_index] = NULL;
902
903 adapter->q_vector[v_idx] = NULL;
904 netif_napi_del(&q_vector->napi);
905
906 /*
907 * ixgbe_get_stats64() might access the rings on this vector,
908 * we must wait a grace period before freeing it.
909 */
910 kfree_rcu(q_vector, rcu);
911}
912
913/**
Alexander Duyck047e0032009-10-27 15:49:27 +0000914 * igb_free_q_vectors - Free memory allocated for interrupt vectors
915 * @adapter: board private structure to initialize
916 *
917 * This function frees the memory allocated to the q_vectors. In addition if
918 * NAPI is enabled it will delete any references to the NAPI struct prior
919 * to freeing the q_vector.
920 **/
921static void igb_free_q_vectors(struct igb_adapter *adapter)
922{
Alexander Duyck5536d212012-09-25 00:31:17 +0000923 int v_idx = adapter->num_q_vectors;
Alexander Duyck047e0032009-10-27 15:49:27 +0000924
Alexander Duyck5536d212012-09-25 00:31:17 +0000925 adapter->num_tx_queues = 0;
926 adapter->num_rx_queues = 0;
Alexander Duyck047e0032009-10-27 15:49:27 +0000927 adapter->num_q_vectors = 0;
Alexander Duyck5536d212012-09-25 00:31:17 +0000928
929 while (v_idx--)
930 igb_free_q_vector(adapter, v_idx);
Alexander Duyck047e0032009-10-27 15:49:27 +0000931}
932
933/**
934 * igb_clear_interrupt_scheme - reset the device to a state of no interrupts
935 *
936 * This function resets the device so that it has 0 rx queues, tx queues, and
937 * MSI-X interrupts allocated.
938 */
939static void igb_clear_interrupt_scheme(struct igb_adapter *adapter)
940{
Alexander Duyck047e0032009-10-27 15:49:27 +0000941 igb_free_q_vectors(adapter);
942 igb_reset_interrupt_capability(adapter);
943}
Auke Kok9d5c8242008-01-24 02:22:38 -0800944
945/**
946 * igb_set_interrupt_capability - set MSI or MSI-X if supported
947 *
948 * Attempt to configure interrupts using the best available
949 * capabilities of the hardware and kernel.
950 **/
Alexander Duyck0c2cc022012-09-25 00:31:22 +0000951static void igb_set_interrupt_capability(struct igb_adapter *adapter)
Auke Kok9d5c8242008-01-24 02:22:38 -0800952{
953 int err;
954 int numvecs, i;
955
Alexander Duyck83b71802009-02-06 23:15:45 +0000956 /* Number of supported queues. */
Alexander Duycka99955f2009-11-12 18:37:19 +0000957 adapter->num_rx_queues = adapter->rss_queues;
Greg Rose5fa85172010-07-01 13:38:16 +0000958 if (adapter->vfs_allocated_count)
959 adapter->num_tx_queues = 1;
960 else
961 adapter->num_tx_queues = adapter->rss_queues;
Alexander Duyck83b71802009-02-06 23:15:45 +0000962
Alexander Duyck047e0032009-10-27 15:49:27 +0000963 /* start with one vector for every rx queue */
964 numvecs = adapter->num_rx_queues;
965
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800966 /* if tx handler is separate add 1 for every tx queue */
Alexander Duycka99955f2009-11-12 18:37:19 +0000967 if (!(adapter->flags & IGB_FLAG_QUEUE_PAIRS))
968 numvecs += adapter->num_tx_queues;
Alexander Duyck047e0032009-10-27 15:49:27 +0000969
970 /* store the number of vectors reserved for queues */
971 adapter->num_q_vectors = numvecs;
972
973 /* add 1 vector for link status interrupts */
974 numvecs++;
Auke Kok9d5c8242008-01-24 02:22:38 -0800975 adapter->msix_entries = kcalloc(numvecs, sizeof(struct msix_entry),
976 GFP_KERNEL);
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +0000977
Auke Kok9d5c8242008-01-24 02:22:38 -0800978 if (!adapter->msix_entries)
979 goto msi_only;
980
981 for (i = 0; i < numvecs; i++)
982 adapter->msix_entries[i].entry = i;
983
984 err = pci_enable_msix(adapter->pdev,
985 adapter->msix_entries,
986 numvecs);
987 if (err == 0)
Alexander Duyck0c2cc022012-09-25 00:31:22 +0000988 return;
Auke Kok9d5c8242008-01-24 02:22:38 -0800989
990 igb_reset_interrupt_capability(adapter);
991
992 /* If we can't do MSI-X, try MSI */
993msi_only:
Alexander Duyck2a3abf62009-04-07 14:37:52 +0000994#ifdef CONFIG_PCI_IOV
995 /* disable SR-IOV for non MSI-X configurations */
996 if (adapter->vf_data) {
997 struct e1000_hw *hw = &adapter->hw;
998 /* disable iov and allow time for transactions to clear */
999 pci_disable_sriov(adapter->pdev);
1000 msleep(500);
1001
1002 kfree(adapter->vf_data);
1003 adapter->vf_data = NULL;
1004 wr32(E1000_IOVCTL, E1000_IOVCTL_REUSE_VFQ);
Jesse Brandeburg945a5152011-07-20 00:56:21 +00001005 wrfl();
Alexander Duyck2a3abf62009-04-07 14:37:52 +00001006 msleep(100);
1007 dev_info(&adapter->pdev->dev, "IOV Disabled\n");
1008 }
1009#endif
Alexander Duyck4fc82ad2009-10-27 23:45:42 +00001010 adapter->vfs_allocated_count = 0;
Alexander Duycka99955f2009-11-12 18:37:19 +00001011 adapter->rss_queues = 1;
Alexander Duyck4fc82ad2009-10-27 23:45:42 +00001012 adapter->flags |= IGB_FLAG_QUEUE_PAIRS;
Auke Kok9d5c8242008-01-24 02:22:38 -08001013 adapter->num_rx_queues = 1;
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07001014 adapter->num_tx_queues = 1;
Alexander Duyck047e0032009-10-27 15:49:27 +00001015 adapter->num_q_vectors = 1;
Auke Kok9d5c8242008-01-24 02:22:38 -08001016 if (!pci_enable_msi(adapter->pdev))
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07001017 adapter->flags |= IGB_FLAG_HAS_MSI;
Auke Kok9d5c8242008-01-24 02:22:38 -08001018}
1019
Alexander Duyck5536d212012-09-25 00:31:17 +00001020static void igb_add_ring(struct igb_ring *ring,
1021 struct igb_ring_container *head)
1022{
1023 head->ring = ring;
1024 head->count++;
1025}
1026
1027/**
1028 * igb_alloc_q_vector - Allocate memory for a single interrupt vector
1029 * @adapter: board private structure to initialize
1030 * @v_count: q_vectors allocated on adapter, used for ring interleaving
1031 * @v_idx: index of vector in adapter struct
1032 * @txr_count: total number of Tx rings to allocate
1033 * @txr_idx: index of first Tx ring to allocate
1034 * @rxr_count: total number of Rx rings to allocate
1035 * @rxr_idx: index of first Rx ring to allocate
1036 *
1037 * We allocate one q_vector. If allocation fails we return -ENOMEM.
1038 **/
1039static int igb_alloc_q_vector(struct igb_adapter *adapter,
1040 int v_count, int v_idx,
1041 int txr_count, int txr_idx,
1042 int rxr_count, int rxr_idx)
1043{
1044 struct igb_q_vector *q_vector;
1045 struct igb_ring *ring;
1046 int ring_count, size;
1047
1048 /* igb only supports 1 Tx and/or 1 Rx queue per vector */
1049 if (txr_count > 1 || rxr_count > 1)
1050 return -ENOMEM;
1051
1052 ring_count = txr_count + rxr_count;
1053 size = sizeof(struct igb_q_vector) +
1054 (sizeof(struct igb_ring) * ring_count);
1055
1056 /* allocate q_vector and rings */
1057 q_vector = kzalloc(size, GFP_KERNEL);
1058 if (!q_vector)
1059 return -ENOMEM;
1060
1061 /* initialize NAPI */
1062 netif_napi_add(adapter->netdev, &q_vector->napi,
1063 igb_poll, 64);
1064
1065 /* tie q_vector and adapter together */
1066 adapter->q_vector[v_idx] = q_vector;
1067 q_vector->adapter = adapter;
1068
1069 /* initialize work limits */
1070 q_vector->tx.work_limit = adapter->tx_work_limit;
1071
1072 /* initialize ITR configuration */
1073 q_vector->itr_register = adapter->hw.hw_addr + E1000_EITR(0);
1074 q_vector->itr_val = IGB_START_ITR;
1075
1076 /* initialize pointer to rings */
1077 ring = q_vector->ring;
1078
1079 if (txr_count) {
1080 /* assign generic ring traits */
1081 ring->dev = &adapter->pdev->dev;
1082 ring->netdev = adapter->netdev;
1083
1084 /* configure backlink on ring */
1085 ring->q_vector = q_vector;
1086
1087 /* update q_vector Tx values */
1088 igb_add_ring(ring, &q_vector->tx);
1089
1090 /* For 82575, context index must be unique per ring. */
1091 if (adapter->hw.mac.type == e1000_82575)
1092 set_bit(IGB_RING_FLAG_TX_CTX_IDX, &ring->flags);
1093
1094 /* apply Tx specific ring traits */
1095 ring->count = adapter->tx_ring_count;
1096 ring->queue_index = txr_idx;
1097
1098 /* assign ring to adapter */
1099 adapter->tx_ring[txr_idx] = ring;
1100
1101 /* push pointer to next ring */
1102 ring++;
1103 }
1104
1105 if (rxr_count) {
1106 /* assign generic ring traits */
1107 ring->dev = &adapter->pdev->dev;
1108 ring->netdev = adapter->netdev;
1109
1110 /* configure backlink on ring */
1111 ring->q_vector = q_vector;
1112
1113 /* update q_vector Rx values */
1114 igb_add_ring(ring, &q_vector->rx);
1115
1116 /* set flag indicating ring supports SCTP checksum offload */
1117 if (adapter->hw.mac.type >= e1000_82576)
1118 set_bit(IGB_RING_FLAG_RX_SCTP_CSUM, &ring->flags);
1119
1120 /*
1121 * On i350, i210, and i211, loopback VLAN packets
1122 * have the tag byte-swapped.
1123 * */
1124 if (adapter->hw.mac.type >= e1000_i350)
1125 set_bit(IGB_RING_FLAG_RX_LB_VLAN_BSWAP, &ring->flags);
1126
1127 /* apply Rx specific ring traits */
1128 ring->count = adapter->rx_ring_count;
1129 ring->queue_index = rxr_idx;
1130
1131 /* assign ring to adapter */
1132 adapter->rx_ring[rxr_idx] = ring;
1133 }
1134
1135 return 0;
1136}
1137
1138
Auke Kok9d5c8242008-01-24 02:22:38 -08001139/**
Alexander Duyck047e0032009-10-27 15:49:27 +00001140 * igb_alloc_q_vectors - Allocate memory for interrupt vectors
1141 * @adapter: board private structure to initialize
1142 *
1143 * We allocate one q_vector per queue interrupt. If allocation fails we
1144 * return -ENOMEM.
1145 **/
1146static int igb_alloc_q_vectors(struct igb_adapter *adapter)
1147{
Alexander Duyck5536d212012-09-25 00:31:17 +00001148 int q_vectors = adapter->num_q_vectors;
1149 int rxr_remaining = adapter->num_rx_queues;
1150 int txr_remaining = adapter->num_tx_queues;
1151 int rxr_idx = 0, txr_idx = 0, v_idx = 0;
1152 int err;
Alexander Duyck047e0032009-10-27 15:49:27 +00001153
Alexander Duyck5536d212012-09-25 00:31:17 +00001154 if (q_vectors >= (rxr_remaining + txr_remaining)) {
1155 for (; rxr_remaining; v_idx++) {
1156 err = igb_alloc_q_vector(adapter, q_vectors, v_idx,
1157 0, 0, 1, rxr_idx);
1158
1159 if (err)
1160 goto err_out;
1161
1162 /* update counts and index */
1163 rxr_remaining--;
1164 rxr_idx++;
1165 }
1166 }
1167
1168 for (; v_idx < q_vectors; v_idx++) {
1169 int rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - v_idx);
1170 int tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - v_idx);
1171 err = igb_alloc_q_vector(adapter, q_vectors, v_idx,
1172 tqpv, txr_idx, rqpv, rxr_idx);
1173
1174 if (err)
Alexander Duyck047e0032009-10-27 15:49:27 +00001175 goto err_out;
Alexander Duyck5536d212012-09-25 00:31:17 +00001176
1177 /* update counts and index */
1178 rxr_remaining -= rqpv;
1179 txr_remaining -= tqpv;
1180 rxr_idx++;
1181 txr_idx++;
Alexander Duyck047e0032009-10-27 15:49:27 +00001182 }
Alexander Duyck81c2fc22011-08-26 07:45:20 +00001183
Alexander Duyck047e0032009-10-27 15:49:27 +00001184 return 0;
1185
1186err_out:
Alexander Duyck5536d212012-09-25 00:31:17 +00001187 adapter->num_tx_queues = 0;
1188 adapter->num_rx_queues = 0;
1189 adapter->num_q_vectors = 0;
1190
1191 while (v_idx--)
1192 igb_free_q_vector(adapter, v_idx);
1193
Alexander Duyck047e0032009-10-27 15:49:27 +00001194 return -ENOMEM;
1195}
1196
Alexander Duyck047e0032009-10-27 15:49:27 +00001197/**
1198 * igb_init_interrupt_scheme - initialize interrupts, allocate queues/vectors
1199 *
1200 * This function initializes the interrupts and allocates all of the queues.
1201 **/
1202static int igb_init_interrupt_scheme(struct igb_adapter *adapter)
1203{
1204 struct pci_dev *pdev = adapter->pdev;
1205 int err;
1206
Alexander Duyck0c2cc022012-09-25 00:31:22 +00001207 igb_set_interrupt_capability(adapter);
Alexander Duyck047e0032009-10-27 15:49:27 +00001208
1209 err = igb_alloc_q_vectors(adapter);
1210 if (err) {
1211 dev_err(&pdev->dev, "Unable to allocate memory for vectors\n");
1212 goto err_alloc_q_vectors;
1213 }
1214
Alexander Duyck5536d212012-09-25 00:31:17 +00001215 igb_cache_ring_register(adapter);
Alexander Duyck047e0032009-10-27 15:49:27 +00001216
1217 return 0;
Alexander Duyck5536d212012-09-25 00:31:17 +00001218
Alexander Duyck047e0032009-10-27 15:49:27 +00001219err_alloc_q_vectors:
1220 igb_reset_interrupt_capability(adapter);
1221 return err;
1222}
1223
1224/**
Auke Kok9d5c8242008-01-24 02:22:38 -08001225 * igb_request_irq - initialize interrupts
1226 *
1227 * Attempts to configure interrupts using the best available
1228 * capabilities of the hardware and kernel.
1229 **/
1230static int igb_request_irq(struct igb_adapter *adapter)
1231{
1232 struct net_device *netdev = adapter->netdev;
Alexander Duyck047e0032009-10-27 15:49:27 +00001233 struct pci_dev *pdev = adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08001234 int err = 0;
1235
1236 if (adapter->msix_entries) {
1237 err = igb_request_msix(adapter);
PJ Waskiewicz844290e2008-06-27 11:00:39 -07001238 if (!err)
Auke Kok9d5c8242008-01-24 02:22:38 -08001239 goto request_done;
Auke Kok9d5c8242008-01-24 02:22:38 -08001240 /* fall back to MSI */
Alexander Duyck5536d212012-09-25 00:31:17 +00001241 igb_free_all_tx_resources(adapter);
1242 igb_free_all_rx_resources(adapter);
Alexander Duyck047e0032009-10-27 15:49:27 +00001243 igb_clear_interrupt_scheme(adapter);
Alexander Duyckc74d5882011-08-26 07:46:45 +00001244 if (!pci_enable_msi(pdev))
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07001245 adapter->flags |= IGB_FLAG_HAS_MSI;
Alexander Duyck047e0032009-10-27 15:49:27 +00001246 adapter->num_tx_queues = 1;
Auke Kok9d5c8242008-01-24 02:22:38 -08001247 adapter->num_rx_queues = 1;
Alexander Duyck047e0032009-10-27 15:49:27 +00001248 adapter->num_q_vectors = 1;
1249 err = igb_alloc_q_vectors(adapter);
1250 if (err) {
1251 dev_err(&pdev->dev,
1252 "Unable to allocate memory for vectors\n");
1253 goto request_done;
1254 }
Alexander Duyck047e0032009-10-27 15:49:27 +00001255 igb_setup_all_tx_resources(adapter);
1256 igb_setup_all_rx_resources(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001257 }
PJ Waskiewicz844290e2008-06-27 11:00:39 -07001258
Alexander Duyckc74d5882011-08-26 07:46:45 +00001259 igb_assign_vector(adapter->q_vector[0], 0);
1260
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07001261 if (adapter->flags & IGB_FLAG_HAS_MSI) {
Alexander Duyckc74d5882011-08-26 07:46:45 +00001262 err = request_irq(pdev->irq, igb_intr_msi, 0,
Alexander Duyck047e0032009-10-27 15:49:27 +00001263 netdev->name, adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001264 if (!err)
1265 goto request_done;
Alexander Duyck047e0032009-10-27 15:49:27 +00001266
Auke Kok9d5c8242008-01-24 02:22:38 -08001267 /* fall back to legacy interrupts */
1268 igb_reset_interrupt_capability(adapter);
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07001269 adapter->flags &= ~IGB_FLAG_HAS_MSI;
Auke Kok9d5c8242008-01-24 02:22:38 -08001270 }
1271
Alexander Duyckc74d5882011-08-26 07:46:45 +00001272 err = request_irq(pdev->irq, igb_intr, IRQF_SHARED,
Alexander Duyck047e0032009-10-27 15:49:27 +00001273 netdev->name, adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001274
Andy Gospodarek6cb5e572008-02-15 14:05:25 -08001275 if (err)
Alexander Duyckc74d5882011-08-26 07:46:45 +00001276 dev_err(&pdev->dev, "Error %d getting interrupt\n",
Auke Kok9d5c8242008-01-24 02:22:38 -08001277 err);
Auke Kok9d5c8242008-01-24 02:22:38 -08001278
1279request_done:
1280 return err;
1281}
1282
1283static void igb_free_irq(struct igb_adapter *adapter)
1284{
Auke Kok9d5c8242008-01-24 02:22:38 -08001285 if (adapter->msix_entries) {
1286 int vector = 0, i;
1287
Alexander Duyck047e0032009-10-27 15:49:27 +00001288 free_irq(adapter->msix_entries[vector++].vector, adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001289
Alexander Duyck0d1ae7f2011-08-26 07:46:34 +00001290 for (i = 0; i < adapter->num_q_vectors; i++)
Alexander Duyck047e0032009-10-27 15:49:27 +00001291 free_irq(adapter->msix_entries[vector++].vector,
Alexander Duyck0d1ae7f2011-08-26 07:46:34 +00001292 adapter->q_vector[i]);
Alexander Duyck047e0032009-10-27 15:49:27 +00001293 } else {
1294 free_irq(adapter->pdev->irq, adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001295 }
Auke Kok9d5c8242008-01-24 02:22:38 -08001296}
1297
1298/**
1299 * igb_irq_disable - Mask off interrupt generation on the NIC
1300 * @adapter: board private structure
1301 **/
1302static void igb_irq_disable(struct igb_adapter *adapter)
1303{
1304 struct e1000_hw *hw = &adapter->hw;
1305
Alexander Duyck25568a52009-10-27 23:49:59 +00001306 /*
1307 * we need to be careful when disabling interrupts. The VFs are also
1308 * mapped into these registers and so clearing the bits can cause
1309 * issues on the VF drivers so we only need to clear what we set
1310 */
Auke Kok9d5c8242008-01-24 02:22:38 -08001311 if (adapter->msix_entries) {
Alexander Duyck2dfd1212009-09-03 14:49:15 +00001312 u32 regval = rd32(E1000_EIAM);
1313 wr32(E1000_EIAM, regval & ~adapter->eims_enable_mask);
1314 wr32(E1000_EIMC, adapter->eims_enable_mask);
1315 regval = rd32(E1000_EIAC);
1316 wr32(E1000_EIAC, regval & ~adapter->eims_enable_mask);
Auke Kok9d5c8242008-01-24 02:22:38 -08001317 }
PJ Waskiewicz844290e2008-06-27 11:00:39 -07001318
1319 wr32(E1000_IAM, 0);
Auke Kok9d5c8242008-01-24 02:22:38 -08001320 wr32(E1000_IMC, ~0);
1321 wrfl();
Emil Tantilov81a61852010-08-02 14:40:52 +00001322 if (adapter->msix_entries) {
1323 int i;
1324 for (i = 0; i < adapter->num_q_vectors; i++)
1325 synchronize_irq(adapter->msix_entries[i].vector);
1326 } else {
1327 synchronize_irq(adapter->pdev->irq);
1328 }
Auke Kok9d5c8242008-01-24 02:22:38 -08001329}
1330
1331/**
1332 * igb_irq_enable - Enable default interrupt generation settings
1333 * @adapter: board private structure
1334 **/
1335static void igb_irq_enable(struct igb_adapter *adapter)
1336{
1337 struct e1000_hw *hw = &adapter->hw;
1338
1339 if (adapter->msix_entries) {
Alexander Duyck06218a82011-08-26 07:46:55 +00001340 u32 ims = E1000_IMS_LSC | E1000_IMS_DOUTSYNC | E1000_IMS_DRSTA;
Alexander Duyck2dfd1212009-09-03 14:49:15 +00001341 u32 regval = rd32(E1000_EIAC);
1342 wr32(E1000_EIAC, regval | adapter->eims_enable_mask);
1343 regval = rd32(E1000_EIAM);
1344 wr32(E1000_EIAM, regval | adapter->eims_enable_mask);
PJ Waskiewicz844290e2008-06-27 11:00:39 -07001345 wr32(E1000_EIMS, adapter->eims_enable_mask);
Alexander Duyck25568a52009-10-27 23:49:59 +00001346 if (adapter->vfs_allocated_count) {
Alexander Duyck4ae196d2009-02-19 20:40:07 -08001347 wr32(E1000_MBVFIMR, 0xFF);
Alexander Duyck25568a52009-10-27 23:49:59 +00001348 ims |= E1000_IMS_VMMB;
1349 }
1350 wr32(E1000_IMS, ims);
PJ Waskiewicz844290e2008-06-27 11:00:39 -07001351 } else {
Alexander Duyck55cac242009-11-19 12:42:21 +00001352 wr32(E1000_IMS, IMS_ENABLE_MASK |
1353 E1000_IMS_DRSTA);
1354 wr32(E1000_IAM, IMS_ENABLE_MASK |
1355 E1000_IMS_DRSTA);
PJ Waskiewicz844290e2008-06-27 11:00:39 -07001356 }
Auke Kok9d5c8242008-01-24 02:22:38 -08001357}
1358
1359static void igb_update_mng_vlan(struct igb_adapter *adapter)
1360{
Alexander Duyck51466232009-10-27 23:47:35 +00001361 struct e1000_hw *hw = &adapter->hw;
Auke Kok9d5c8242008-01-24 02:22:38 -08001362 u16 vid = adapter->hw.mng_cookie.vlan_id;
1363 u16 old_vid = adapter->mng_vlan_id;
Auke Kok9d5c8242008-01-24 02:22:38 -08001364
Alexander Duyck51466232009-10-27 23:47:35 +00001365 if (hw->mng_cookie.status & E1000_MNG_DHCP_COOKIE_STATUS_VLAN) {
1366 /* add VID to filter table */
1367 igb_vfta_set(hw, vid, true);
1368 adapter->mng_vlan_id = vid;
1369 } else {
1370 adapter->mng_vlan_id = IGB_MNG_VLAN_NONE;
1371 }
1372
1373 if ((old_vid != (u16)IGB_MNG_VLAN_NONE) &&
1374 (vid != old_vid) &&
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00001375 !test_bit(old_vid, adapter->active_vlans)) {
Alexander Duyck51466232009-10-27 23:47:35 +00001376 /* remove VID from filter table */
1377 igb_vfta_set(hw, old_vid, false);
Auke Kok9d5c8242008-01-24 02:22:38 -08001378 }
1379}
1380
1381/**
1382 * igb_release_hw_control - release control of the h/w to f/w
1383 * @adapter: address of board private structure
1384 *
1385 * igb_release_hw_control resets CTRL_EXT:DRV_LOAD bit.
1386 * For ASF and Pass Through versions of f/w this means that the
1387 * driver is no longer loaded.
1388 *
1389 **/
1390static void igb_release_hw_control(struct igb_adapter *adapter)
1391{
1392 struct e1000_hw *hw = &adapter->hw;
1393 u32 ctrl_ext;
1394
1395 /* Let firmware take over control of h/w */
1396 ctrl_ext = rd32(E1000_CTRL_EXT);
1397 wr32(E1000_CTRL_EXT,
1398 ctrl_ext & ~E1000_CTRL_EXT_DRV_LOAD);
1399}
1400
Auke Kok9d5c8242008-01-24 02:22:38 -08001401/**
1402 * igb_get_hw_control - get control of the h/w from f/w
1403 * @adapter: address of board private structure
1404 *
1405 * igb_get_hw_control sets CTRL_EXT:DRV_LOAD bit.
1406 * For ASF and Pass Through versions of f/w this means that
1407 * the driver is loaded.
1408 *
1409 **/
1410static void igb_get_hw_control(struct igb_adapter *adapter)
1411{
1412 struct e1000_hw *hw = &adapter->hw;
1413 u32 ctrl_ext;
1414
1415 /* Let firmware know the driver has taken over */
1416 ctrl_ext = rd32(E1000_CTRL_EXT);
1417 wr32(E1000_CTRL_EXT,
1418 ctrl_ext | E1000_CTRL_EXT_DRV_LOAD);
1419}
1420
Auke Kok9d5c8242008-01-24 02:22:38 -08001421/**
1422 * igb_configure - configure the hardware for RX and TX
1423 * @adapter: private board structure
1424 **/
1425static void igb_configure(struct igb_adapter *adapter)
1426{
1427 struct net_device *netdev = adapter->netdev;
1428 int i;
1429
1430 igb_get_hw_control(adapter);
Alexander Duyckff41f8d2009-09-03 14:48:56 +00001431 igb_set_rx_mode(netdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08001432
1433 igb_restore_vlan(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001434
Alexander Duyck85b430b2009-10-27 15:50:29 +00001435 igb_setup_tctl(adapter);
Alexander Duyck06cf2662009-10-27 15:53:25 +00001436 igb_setup_mrqc(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001437 igb_setup_rctl(adapter);
Alexander Duyck85b430b2009-10-27 15:50:29 +00001438
1439 igb_configure_tx(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001440 igb_configure_rx(adapter);
Alexander Duyck662d7202008-06-27 11:00:29 -07001441
1442 igb_rx_fifo_flush_82575(&adapter->hw);
1443
Alexander Duyckc493ea42009-03-20 00:16:50 +00001444 /* call igb_desc_unused which always leaves
Auke Kok9d5c8242008-01-24 02:22:38 -08001445 * at least 1 descriptor unused to make sure
1446 * next_to_use != next_to_clean */
1447 for (i = 0; i < adapter->num_rx_queues; i++) {
Alexander Duyck3025a442010-02-17 01:02:39 +00001448 struct igb_ring *ring = adapter->rx_ring[i];
Alexander Duyckcd392f52011-08-26 07:43:59 +00001449 igb_alloc_rx_buffers(ring, igb_desc_unused(ring));
Auke Kok9d5c8242008-01-24 02:22:38 -08001450 }
Auke Kok9d5c8242008-01-24 02:22:38 -08001451}
1452
Nick Nunley88a268c2010-02-17 01:01:59 +00001453/**
1454 * igb_power_up_link - Power up the phy/serdes link
1455 * @adapter: address of board private structure
1456 **/
1457void igb_power_up_link(struct igb_adapter *adapter)
1458{
Akeem G. Abodunrin76886592012-07-17 04:51:18 +00001459 igb_reset_phy(&adapter->hw);
1460
Nick Nunley88a268c2010-02-17 01:01:59 +00001461 if (adapter->hw.phy.media_type == e1000_media_type_copper)
1462 igb_power_up_phy_copper(&adapter->hw);
1463 else
1464 igb_power_up_serdes_link_82575(&adapter->hw);
1465}
1466
1467/**
1468 * igb_power_down_link - Power down the phy/serdes link
1469 * @adapter: address of board private structure
1470 */
1471static void igb_power_down_link(struct igb_adapter *adapter)
1472{
1473 if (adapter->hw.phy.media_type == e1000_media_type_copper)
1474 igb_power_down_phy_copper_82575(&adapter->hw);
1475 else
1476 igb_shutdown_serdes_link_82575(&adapter->hw);
1477}
Auke Kok9d5c8242008-01-24 02:22:38 -08001478
1479/**
1480 * igb_up - Open the interface and prepare it to handle traffic
1481 * @adapter: board private structure
1482 **/
Auke Kok9d5c8242008-01-24 02:22:38 -08001483int igb_up(struct igb_adapter *adapter)
1484{
1485 struct e1000_hw *hw = &adapter->hw;
1486 int i;
1487
1488 /* hardware has been reset, we need to reload some things */
1489 igb_configure(adapter);
1490
1491 clear_bit(__IGB_DOWN, &adapter->state);
1492
Alexander Duyck0d1ae7f2011-08-26 07:46:34 +00001493 for (i = 0; i < adapter->num_q_vectors; i++)
1494 napi_enable(&(adapter->q_vector[i]->napi));
1495
PJ Waskiewicz844290e2008-06-27 11:00:39 -07001496 if (adapter->msix_entries)
Auke Kok9d5c8242008-01-24 02:22:38 -08001497 igb_configure_msix(adapter);
Alexander Duyckfeeb2722010-02-03 21:59:51 +00001498 else
1499 igb_assign_vector(adapter->q_vector[0], 0);
Auke Kok9d5c8242008-01-24 02:22:38 -08001500
1501 /* Clear any pending interrupts. */
1502 rd32(E1000_ICR);
1503 igb_irq_enable(adapter);
1504
Alexander Duyckd4960302009-10-27 15:53:45 +00001505 /* notify VFs that reset has been completed */
1506 if (adapter->vfs_allocated_count) {
1507 u32 reg_data = rd32(E1000_CTRL_EXT);
1508 reg_data |= E1000_CTRL_EXT_PFRSTD;
1509 wr32(E1000_CTRL_EXT, reg_data);
1510 }
1511
Jesse Brandeburg4cb9be72009-04-21 18:42:05 +00001512 netif_tx_start_all_queues(adapter->netdev);
1513
Alexander Duyck25568a52009-10-27 23:49:59 +00001514 /* start the watchdog. */
1515 hw->mac.get_link_status = 1;
1516 schedule_work(&adapter->watchdog_task);
1517
Auke Kok9d5c8242008-01-24 02:22:38 -08001518 return 0;
1519}
1520
1521void igb_down(struct igb_adapter *adapter)
1522{
Auke Kok9d5c8242008-01-24 02:22:38 -08001523 struct net_device *netdev = adapter->netdev;
Alexander Duyck330a6d62009-10-27 23:51:35 +00001524 struct e1000_hw *hw = &adapter->hw;
Auke Kok9d5c8242008-01-24 02:22:38 -08001525 u32 tctl, rctl;
1526 int i;
1527
1528 /* signal that we're down so the interrupt handler does not
1529 * reschedule our watchdog timer */
1530 set_bit(__IGB_DOWN, &adapter->state);
1531
1532 /* disable receives in the hardware */
1533 rctl = rd32(E1000_RCTL);
1534 wr32(E1000_RCTL, rctl & ~E1000_RCTL_EN);
1535 /* flush and sleep below */
1536
David S. Millerfd2ea0a2008-07-17 01:56:23 -07001537 netif_tx_stop_all_queues(netdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08001538
1539 /* disable transmits in the hardware */
1540 tctl = rd32(E1000_TCTL);
1541 tctl &= ~E1000_TCTL_EN;
1542 wr32(E1000_TCTL, tctl);
1543 /* flush both disables and wait for them to finish */
1544 wrfl();
1545 msleep(10);
1546
Alexander Duyck0d1ae7f2011-08-26 07:46:34 +00001547 for (i = 0; i < adapter->num_q_vectors; i++)
1548 napi_disable(&(adapter->q_vector[i]->napi));
Auke Kok9d5c8242008-01-24 02:22:38 -08001549
Auke Kok9d5c8242008-01-24 02:22:38 -08001550 igb_irq_disable(adapter);
1551
1552 del_timer_sync(&adapter->watchdog_timer);
1553 del_timer_sync(&adapter->phy_info_timer);
1554
Auke Kok9d5c8242008-01-24 02:22:38 -08001555 netif_carrier_off(netdev);
Alexander Duyck04fe6352009-02-06 23:22:32 +00001556
1557 /* record the stats before reset*/
Eric Dumazet12dcd862010-10-15 17:27:10 +00001558 spin_lock(&adapter->stats64_lock);
1559 igb_update_stats(adapter, &adapter->stats64);
1560 spin_unlock(&adapter->stats64_lock);
Alexander Duyck04fe6352009-02-06 23:22:32 +00001561
Auke Kok9d5c8242008-01-24 02:22:38 -08001562 adapter->link_speed = 0;
1563 adapter->link_duplex = 0;
1564
Jeff Kirsher30236822008-06-24 17:01:15 -07001565 if (!pci_channel_offline(adapter->pdev))
1566 igb_reset(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08001567 igb_clean_all_tx_rings(adapter);
1568 igb_clean_all_rx_rings(adapter);
Alexander Duyck7e0e99e2009-05-21 13:06:56 +00001569#ifdef CONFIG_IGB_DCA
1570
1571 /* since we reset the hardware DCA settings were cleared */
1572 igb_setup_dca(adapter);
1573#endif
Auke Kok9d5c8242008-01-24 02:22:38 -08001574}
1575
1576void igb_reinit_locked(struct igb_adapter *adapter)
1577{
1578 WARN_ON(in_interrupt());
1579 while (test_and_set_bit(__IGB_RESETTING, &adapter->state))
1580 msleep(1);
1581 igb_down(adapter);
1582 igb_up(adapter);
1583 clear_bit(__IGB_RESETTING, &adapter->state);
1584}
1585
1586void igb_reset(struct igb_adapter *adapter)
1587{
Alexander Duyck090b1792009-10-27 23:51:55 +00001588 struct pci_dev *pdev = adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08001589 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck2d064c02008-07-08 15:10:12 -07001590 struct e1000_mac_info *mac = &hw->mac;
1591 struct e1000_fc_info *fc = &hw->fc;
Matthew Vickd48507f2012-11-08 04:03:58 +00001592 u32 pba = 0, tx_space, min_tx_space, min_rx_space, hwm;
Auke Kok9d5c8242008-01-24 02:22:38 -08001593
1594 /* Repartition Pba for greater than 9k mtu
1595 * To take effect CTRL.RST is required.
1596 */
Alexander Duyckfa4dfae2009-02-06 23:21:31 +00001597 switch (mac->type) {
Alexander Duyckd2ba2ed2010-03-22 14:08:06 +00001598 case e1000_i350:
Alexander Duyck55cac242009-11-19 12:42:21 +00001599 case e1000_82580:
1600 pba = rd32(E1000_RXPBS);
1601 pba = igb_rxpbs_adjust_82580(pba);
1602 break;
Alexander Duyckfa4dfae2009-02-06 23:21:31 +00001603 case e1000_82576:
Alexander Duyckd249be52009-10-27 23:46:38 +00001604 pba = rd32(E1000_RXPBS);
1605 pba &= E1000_RXPBS_SIZE_MASK_82576;
Alexander Duyckfa4dfae2009-02-06 23:21:31 +00001606 break;
1607 case e1000_82575:
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00001608 case e1000_i210:
1609 case e1000_i211:
Alexander Duyckfa4dfae2009-02-06 23:21:31 +00001610 default:
1611 pba = E1000_PBA_34K;
1612 break;
Alexander Duyck2d064c02008-07-08 15:10:12 -07001613 }
Auke Kok9d5c8242008-01-24 02:22:38 -08001614
Alexander Duyck2d064c02008-07-08 15:10:12 -07001615 if ((adapter->max_frame_size > ETH_FRAME_LEN + ETH_FCS_LEN) &&
1616 (mac->type < e1000_82576)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08001617 /* adjust PBA for jumbo frames */
1618 wr32(E1000_PBA, pba);
1619
1620 /* To maintain wire speed transmits, the Tx FIFO should be
1621 * large enough to accommodate two full transmit packets,
1622 * rounded up to the next 1KB and expressed in KB. Likewise,
1623 * the Rx FIFO should be large enough to accommodate at least
1624 * one full receive packet and is similarly rounded up and
1625 * expressed in KB. */
1626 pba = rd32(E1000_PBA);
1627 /* upper 16 bits has Tx packet buffer allocation size in KB */
1628 tx_space = pba >> 16;
1629 /* lower 16 bits has Rx packet buffer allocation size in KB */
1630 pba &= 0xffff;
1631 /* the tx fifo also stores 16 bytes of information about the tx
1632 * but don't include ethernet FCS because hardware appends it */
1633 min_tx_space = (adapter->max_frame_size +
Alexander Duyck85e8d002009-02-16 00:00:20 -08001634 sizeof(union e1000_adv_tx_desc) -
Auke Kok9d5c8242008-01-24 02:22:38 -08001635 ETH_FCS_LEN) * 2;
1636 min_tx_space = ALIGN(min_tx_space, 1024);
1637 min_tx_space >>= 10;
1638 /* software strips receive CRC, so leave room for it */
1639 min_rx_space = adapter->max_frame_size;
1640 min_rx_space = ALIGN(min_rx_space, 1024);
1641 min_rx_space >>= 10;
1642
1643 /* If current Tx allocation is less than the min Tx FIFO size,
1644 * and the min Tx FIFO size is less than the current Rx FIFO
1645 * allocation, take space away from current Rx allocation */
1646 if (tx_space < min_tx_space &&
1647 ((min_tx_space - tx_space) < pba)) {
1648 pba = pba - (min_tx_space - tx_space);
1649
1650 /* if short on rx space, rx wins and must trump tx
1651 * adjustment */
1652 if (pba < min_rx_space)
1653 pba = min_rx_space;
1654 }
Alexander Duyck2d064c02008-07-08 15:10:12 -07001655 wr32(E1000_PBA, pba);
Auke Kok9d5c8242008-01-24 02:22:38 -08001656 }
Auke Kok9d5c8242008-01-24 02:22:38 -08001657
1658 /* flow control settings */
1659 /* The high water mark must be low enough to fit one full frame
1660 * (or the size used for early receive) above it in the Rx FIFO.
1661 * Set it to the lower of:
1662 * - 90% of the Rx FIFO size, or
1663 * - the full Rx FIFO size minus one full frame */
1664 hwm = min(((pba << 10) * 9 / 10),
Alexander Duyck2d064c02008-07-08 15:10:12 -07001665 ((pba << 10) - 2 * adapter->max_frame_size));
Auke Kok9d5c8242008-01-24 02:22:38 -08001666
Matthew Vickd48507f2012-11-08 04:03:58 +00001667 fc->high_water = hwm & 0xFFFFFFF0; /* 16-byte granularity */
Alexander Duyckd405ea32009-12-23 13:21:27 +00001668 fc->low_water = fc->high_water - 16;
Auke Kok9d5c8242008-01-24 02:22:38 -08001669 fc->pause_time = 0xFFFF;
1670 fc->send_xon = 1;
Alexander Duyck0cce1192009-07-23 18:10:24 +00001671 fc->current_mode = fc->requested_mode;
Auke Kok9d5c8242008-01-24 02:22:38 -08001672
Alexander Duyck4ae196d2009-02-19 20:40:07 -08001673 /* disable receive for all VFs and wait one second */
1674 if (adapter->vfs_allocated_count) {
1675 int i;
1676 for (i = 0 ; i < adapter->vfs_allocated_count; i++)
Greg Rose8fa7e0f2010-11-06 05:43:21 +00001677 adapter->vf_data[i].flags &= IGB_VF_FLAG_PF_SET_MAC;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08001678
1679 /* ping all the active vfs to let them know we are going down */
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00001680 igb_ping_all_vfs(adapter);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08001681
1682 /* disable transmits and receives */
1683 wr32(E1000_VFRE, 0);
1684 wr32(E1000_VFTE, 0);
1685 }
1686
Auke Kok9d5c8242008-01-24 02:22:38 -08001687 /* Allow time for pending master requests to run */
Alexander Duyck330a6d62009-10-27 23:51:35 +00001688 hw->mac.ops.reset_hw(hw);
Auke Kok9d5c8242008-01-24 02:22:38 -08001689 wr32(E1000_WUC, 0);
1690
Alexander Duyck330a6d62009-10-27 23:51:35 +00001691 if (hw->mac.ops.init_hw(hw))
Alexander Duyck090b1792009-10-27 23:51:55 +00001692 dev_err(&pdev->dev, "Hardware Error\n");
Auke Kok9d5c8242008-01-24 02:22:38 -08001693
Matthew Vicka27416b2012-04-18 02:57:44 +00001694 /*
1695 * Flow control settings reset on hardware reset, so guarantee flow
1696 * control is off when forcing speed.
1697 */
1698 if (!hw->mac.autoneg)
1699 igb_force_mac_fc(hw);
1700
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +00001701 igb_init_dmac(adapter, pba);
Nick Nunley88a268c2010-02-17 01:01:59 +00001702 if (!netif_running(adapter->netdev))
1703 igb_power_down_link(adapter);
1704
Auke Kok9d5c8242008-01-24 02:22:38 -08001705 igb_update_mng_vlan(adapter);
1706
1707 /* Enable h/w to recognize an 802.1Q VLAN Ethernet packet */
1708 wr32(E1000_VET, ETHERNET_IEEE_VLAN_TYPE);
1709
Matthew Vick1f6e8172012-08-18 07:26:33 +00001710 /* Re-enable PTP, where applicable. */
1711 igb_ptp_reset(adapter);
Matthew Vick1f6e8172012-08-18 07:26:33 +00001712
Alexander Duyck330a6d62009-10-27 23:51:35 +00001713 igb_get_phy_info(hw);
Auke Kok9d5c8242008-01-24 02:22:38 -08001714}
1715
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001716static netdev_features_t igb_fix_features(struct net_device *netdev,
1717 netdev_features_t features)
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00001718{
1719 /*
1720 * Since there is no support for separate rx/tx vlan accel
1721 * enable/disable make sure tx flag is always in same state as rx.
1722 */
1723 if (features & NETIF_F_HW_VLAN_RX)
1724 features |= NETIF_F_HW_VLAN_TX;
1725 else
1726 features &= ~NETIF_F_HW_VLAN_TX;
1727
1728 return features;
1729}
1730
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001731static int igb_set_features(struct net_device *netdev,
1732 netdev_features_t features)
Michał Mirosławac52caa2011-06-08 08:38:01 +00001733{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001734 netdev_features_t changed = netdev->features ^ features;
Ben Greear89eaefb2012-03-06 09:41:58 +00001735 struct igb_adapter *adapter = netdev_priv(netdev);
Michał Mirosławac52caa2011-06-08 08:38:01 +00001736
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00001737 if (changed & NETIF_F_HW_VLAN_RX)
1738 igb_vlan_mode(netdev, features);
1739
Ben Greear89eaefb2012-03-06 09:41:58 +00001740 if (!(changed & NETIF_F_RXALL))
1741 return 0;
1742
1743 netdev->features = features;
1744
1745 if (netif_running(netdev))
1746 igb_reinit_locked(adapter);
1747 else
1748 igb_reset(adapter);
1749
Michał Mirosławac52caa2011-06-08 08:38:01 +00001750 return 0;
1751}
1752
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08001753static const struct net_device_ops igb_netdev_ops = {
Alexander Duyck559e9c42009-10-27 23:52:50 +00001754 .ndo_open = igb_open,
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08001755 .ndo_stop = igb_close,
Alexander Duyckcd392f52011-08-26 07:43:59 +00001756 .ndo_start_xmit = igb_xmit_frame,
Eric Dumazet12dcd862010-10-15 17:27:10 +00001757 .ndo_get_stats64 = igb_get_stats64,
Alexander Duyckff41f8d2009-09-03 14:48:56 +00001758 .ndo_set_rx_mode = igb_set_rx_mode,
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08001759 .ndo_set_mac_address = igb_set_mac,
1760 .ndo_change_mtu = igb_change_mtu,
1761 .ndo_do_ioctl = igb_ioctl,
1762 .ndo_tx_timeout = igb_tx_timeout,
1763 .ndo_validate_addr = eth_validate_addr,
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08001764 .ndo_vlan_rx_add_vid = igb_vlan_rx_add_vid,
1765 .ndo_vlan_rx_kill_vid = igb_vlan_rx_kill_vid,
Williams, Mitch A8151d292010-02-10 01:44:24 +00001766 .ndo_set_vf_mac = igb_ndo_set_vf_mac,
1767 .ndo_set_vf_vlan = igb_ndo_set_vf_vlan,
1768 .ndo_set_vf_tx_rate = igb_ndo_set_vf_bw,
1769 .ndo_get_vf_config = igb_ndo_get_vf_config,
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08001770#ifdef CONFIG_NET_POLL_CONTROLLER
1771 .ndo_poll_controller = igb_netpoll,
1772#endif
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00001773 .ndo_fix_features = igb_fix_features,
1774 .ndo_set_features = igb_set_features,
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08001775};
1776
Taku Izumi42bfd33a2008-06-20 12:10:30 +09001777/**
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00001778 * igb_set_fw_version - Configure version string for ethtool
1779 * @adapter: adapter struct
1780 *
1781 **/
1782void igb_set_fw_version(struct igb_adapter *adapter)
1783{
1784 struct e1000_hw *hw = &adapter->hw;
Carolyn Wyborny0b1a6f22012-10-18 07:16:19 +00001785 struct e1000_fw_version fw;
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00001786
Carolyn Wyborny0b1a6f22012-10-18 07:16:19 +00001787 igb_get_fw_version(hw, &fw);
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00001788
Carolyn Wyborny0b1a6f22012-10-18 07:16:19 +00001789 switch (hw->mac.type) {
1790 case e1000_i211:
1791 snprintf(adapter->fw_version, sizeof(adapter->fw_version),
1792 "%2d.%2d-%d",
1793 fw.invm_major, fw.invm_minor, fw.invm_img_type);
1794 break;
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00001795
Carolyn Wyborny0b1a6f22012-10-18 07:16:19 +00001796 default:
1797 /* if option is rom valid, display its version too */
1798 if (fw.or_valid) {
1799 snprintf(adapter->fw_version,
1800 sizeof(adapter->fw_version),
1801 "%d.%d, 0x%08x, %d.%d.%d",
1802 fw.eep_major, fw.eep_minor, fw.etrack_id,
1803 fw.or_major, fw.or_build, fw.or_patch);
1804 /* no option rom */
1805 } else {
1806 snprintf(adapter->fw_version,
1807 sizeof(adapter->fw_version),
1808 "%d.%d, 0x%08x",
1809 fw.eep_major, fw.eep_minor, fw.etrack_id);
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00001810 }
Carolyn Wyborny0b1a6f22012-10-18 07:16:19 +00001811 break;
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00001812 }
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00001813 return;
1814}
1815
1816/**
Auke Kok9d5c8242008-01-24 02:22:38 -08001817 * igb_probe - Device Initialization Routine
1818 * @pdev: PCI device information struct
1819 * @ent: entry in igb_pci_tbl
1820 *
1821 * Returns 0 on success, negative on failure
1822 *
1823 * igb_probe initializes an adapter identified by a pci_dev structure.
1824 * The OS initialization, configuring of the adapter private structure,
1825 * and a hardware reset occur.
1826 **/
Bill Pemberton9f9a12f2012-12-03 09:24:25 -05001827static int igb_probe(struct pci_dev *pdev,
Auke Kok9d5c8242008-01-24 02:22:38 -08001828 const struct pci_device_id *ent)
1829{
1830 struct net_device *netdev;
1831 struct igb_adapter *adapter;
1832 struct e1000_hw *hw;
Alexander Duyck4337e992009-10-27 23:48:31 +00001833 u16 eeprom_data = 0;
Carolyn Wyborny9835fd72010-11-22 17:17:21 +00001834 s32 ret_val;
Alexander Duyck4337e992009-10-27 23:48:31 +00001835 static int global_quad_port_a; /* global quad port a indication */
Auke Kok9d5c8242008-01-24 02:22:38 -08001836 const struct e1000_info *ei = igb_info_tbl[ent->driver_data];
1837 unsigned long mmio_start, mmio_len;
David S. Miller2d6a5e92009-03-17 15:01:30 -07001838 int err, pci_using_dac;
Carolyn Wyborny9835fd72010-11-22 17:17:21 +00001839 u8 part_str[E1000_PBANUM_LENGTH];
Auke Kok9d5c8242008-01-24 02:22:38 -08001840
Andy Gospodarekbded64a2010-07-21 06:40:31 +00001841 /* Catch broken hardware that put the wrong VF device ID in
1842 * the PCIe SR-IOV capability.
1843 */
1844 if (pdev->is_virtfn) {
1845 WARN(1, KERN_ERR "%s (%hx:%hx) should not be a VF!\n",
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00001846 pci_name(pdev), pdev->vendor, pdev->device);
Andy Gospodarekbded64a2010-07-21 06:40:31 +00001847 return -EINVAL;
1848 }
1849
Alexander Duyckaed5dec2009-02-06 23:16:04 +00001850 err = pci_enable_device_mem(pdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08001851 if (err)
1852 return err;
1853
1854 pci_using_dac = 0;
Alexander Duyck59d71982010-04-27 13:09:25 +00001855 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(64));
Auke Kok9d5c8242008-01-24 02:22:38 -08001856 if (!err) {
Alexander Duyck59d71982010-04-27 13:09:25 +00001857 err = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64));
Auke Kok9d5c8242008-01-24 02:22:38 -08001858 if (!err)
1859 pci_using_dac = 1;
1860 } else {
Alexander Duyck59d71982010-04-27 13:09:25 +00001861 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
Auke Kok9d5c8242008-01-24 02:22:38 -08001862 if (err) {
Alexander Duyck59d71982010-04-27 13:09:25 +00001863 err = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
Auke Kok9d5c8242008-01-24 02:22:38 -08001864 if (err) {
1865 dev_err(&pdev->dev, "No usable DMA "
1866 "configuration, aborting\n");
1867 goto err_dma;
1868 }
1869 }
1870 }
1871
Alexander Duyckaed5dec2009-02-06 23:16:04 +00001872 err = pci_request_selected_regions(pdev, pci_select_bars(pdev,
1873 IORESOURCE_MEM),
1874 igb_driver_name);
Auke Kok9d5c8242008-01-24 02:22:38 -08001875 if (err)
1876 goto err_pci_reg;
1877
Frans Pop19d5afd2009-10-02 10:04:12 -07001878 pci_enable_pcie_error_reporting(pdev);
Alexander Duyck40a914f2008-11-27 00:24:37 -08001879
Auke Kok9d5c8242008-01-24 02:22:38 -08001880 pci_set_master(pdev);
Auke Kokc682fc22008-04-23 11:09:34 -07001881 pci_save_state(pdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08001882
1883 err = -ENOMEM;
Alexander Duyck1bfaf072009-02-19 20:39:23 -08001884 netdev = alloc_etherdev_mq(sizeof(struct igb_adapter),
Alexander Duyck1cc3bd82011-08-26 07:44:10 +00001885 IGB_MAX_TX_QUEUES);
Auke Kok9d5c8242008-01-24 02:22:38 -08001886 if (!netdev)
1887 goto err_alloc_etherdev;
1888
1889 SET_NETDEV_DEV(netdev, &pdev->dev);
1890
1891 pci_set_drvdata(pdev, netdev);
1892 adapter = netdev_priv(netdev);
1893 adapter->netdev = netdev;
1894 adapter->pdev = pdev;
1895 hw = &adapter->hw;
1896 hw->back = adapter;
stephen hemmingerb3f4d592012-03-13 06:04:20 +00001897 adapter->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
Auke Kok9d5c8242008-01-24 02:22:38 -08001898
1899 mmio_start = pci_resource_start(pdev, 0);
1900 mmio_len = pci_resource_len(pdev, 0);
1901
1902 err = -EIO;
Alexander Duyck28b07592009-02-06 23:20:31 +00001903 hw->hw_addr = ioremap(mmio_start, mmio_len);
1904 if (!hw->hw_addr)
Auke Kok9d5c8242008-01-24 02:22:38 -08001905 goto err_ioremap;
1906
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08001907 netdev->netdev_ops = &igb_netdev_ops;
Auke Kok9d5c8242008-01-24 02:22:38 -08001908 igb_set_ethtool_ops(netdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08001909 netdev->watchdog_timeo = 5 * HZ;
Auke Kok9d5c8242008-01-24 02:22:38 -08001910
1911 strncpy(netdev->name, pci_name(pdev), sizeof(netdev->name) - 1);
1912
1913 netdev->mem_start = mmio_start;
1914 netdev->mem_end = mmio_start + mmio_len;
1915
Auke Kok9d5c8242008-01-24 02:22:38 -08001916 /* PCI config space info */
1917 hw->vendor_id = pdev->vendor;
1918 hw->device_id = pdev->device;
1919 hw->revision_id = pdev->revision;
1920 hw->subsystem_vendor_id = pdev->subsystem_vendor;
1921 hw->subsystem_device_id = pdev->subsystem_device;
1922
Auke Kok9d5c8242008-01-24 02:22:38 -08001923 /* Copy the default MAC, PHY and NVM function pointers */
1924 memcpy(&hw->mac.ops, ei->mac_ops, sizeof(hw->mac.ops));
1925 memcpy(&hw->phy.ops, ei->phy_ops, sizeof(hw->phy.ops));
1926 memcpy(&hw->nvm.ops, ei->nvm_ops, sizeof(hw->nvm.ops));
1927 /* Initialize skew-specific constants */
1928 err = ei->get_invariants(hw);
1929 if (err)
Alexander Duyck450c87c2009-02-06 23:22:11 +00001930 goto err_sw_init;
Auke Kok9d5c8242008-01-24 02:22:38 -08001931
Alexander Duyck450c87c2009-02-06 23:22:11 +00001932 /* setup the private structure */
Auke Kok9d5c8242008-01-24 02:22:38 -08001933 err = igb_sw_init(adapter);
1934 if (err)
1935 goto err_sw_init;
1936
1937 igb_get_bus_info_pcie(hw);
1938
1939 hw->phy.autoneg_wait_to_complete = false;
Auke Kok9d5c8242008-01-24 02:22:38 -08001940
1941 /* Copper options */
1942 if (hw->phy.media_type == e1000_media_type_copper) {
1943 hw->phy.mdix = AUTO_ALL_MODES;
1944 hw->phy.disable_polarity_correction = false;
1945 hw->phy.ms_type = e1000_ms_hw_default;
1946 }
1947
1948 if (igb_check_reset_block(hw))
1949 dev_info(&pdev->dev,
1950 "PHY reset is blocked due to SOL/IDER session.\n");
1951
Alexander Duyck077887c2011-08-26 07:46:29 +00001952 /*
1953 * features is initialized to 0 in allocation, it might have bits
1954 * set by igb_sw_init so we should use an or instead of an
1955 * assignment.
1956 */
1957 netdev->features |= NETIF_F_SG |
1958 NETIF_F_IP_CSUM |
1959 NETIF_F_IPV6_CSUM |
1960 NETIF_F_TSO |
1961 NETIF_F_TSO6 |
1962 NETIF_F_RXHASH |
1963 NETIF_F_RXCSUM |
1964 NETIF_F_HW_VLAN_RX |
1965 NETIF_F_HW_VLAN_TX;
Michał Mirosławac52caa2011-06-08 08:38:01 +00001966
Alexander Duyck077887c2011-08-26 07:46:29 +00001967 /* copy netdev features into list of user selectable features */
1968 netdev->hw_features |= netdev->features;
Ben Greear89eaefb2012-03-06 09:41:58 +00001969 netdev->hw_features |= NETIF_F_RXALL;
Auke Kok9d5c8242008-01-24 02:22:38 -08001970
Alexander Duyck077887c2011-08-26 07:46:29 +00001971 /* set this bit last since it cannot be part of hw_features */
1972 netdev->features |= NETIF_F_HW_VLAN_FILTER;
1973
1974 netdev->vlan_features |= NETIF_F_TSO |
1975 NETIF_F_TSO6 |
1976 NETIF_F_IP_CSUM |
1977 NETIF_F_IPV6_CSUM |
1978 NETIF_F_SG;
Jeff Kirsher48f29ff2008-06-05 04:06:27 -07001979
Ben Greear6b8f0922012-03-06 09:41:53 +00001980 netdev->priv_flags |= IFF_SUPP_NOFCS;
1981
Yi Zou7b872a52010-09-22 17:57:58 +00001982 if (pci_using_dac) {
Auke Kok9d5c8242008-01-24 02:22:38 -08001983 netdev->features |= NETIF_F_HIGHDMA;
Yi Zou7b872a52010-09-22 17:57:58 +00001984 netdev->vlan_features |= NETIF_F_HIGHDMA;
1985 }
Auke Kok9d5c8242008-01-24 02:22:38 -08001986
Michał Mirosławac52caa2011-06-08 08:38:01 +00001987 if (hw->mac.type >= e1000_82576) {
1988 netdev->hw_features |= NETIF_F_SCTP_CSUM;
Jesse Brandeburgb9473562009-04-27 22:36:13 +00001989 netdev->features |= NETIF_F_SCTP_CSUM;
Michał Mirosławac52caa2011-06-08 08:38:01 +00001990 }
Jesse Brandeburgb9473562009-04-27 22:36:13 +00001991
Jiri Pirko01789342011-08-16 06:29:00 +00001992 netdev->priv_flags |= IFF_UNICAST_FLT;
1993
Alexander Duyck330a6d62009-10-27 23:51:35 +00001994 adapter->en_mng_pt = igb_enable_mng_pass_thru(hw);
Auke Kok9d5c8242008-01-24 02:22:38 -08001995
1996 /* before reading the NVM, reset the controller to put the device in a
1997 * known good starting state */
1998 hw->mac.ops.reset_hw(hw);
1999
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002000 /*
2001 * make sure the NVM is good , i211 parts have special NVM that
2002 * doesn't contain a checksum
2003 */
2004 if (hw->mac.type != e1000_i211) {
2005 if (hw->nvm.ops.validate(hw) < 0) {
2006 dev_err(&pdev->dev, "The NVM Checksum Is Not Valid\n");
2007 err = -EIO;
2008 goto err_eeprom;
2009 }
Auke Kok9d5c8242008-01-24 02:22:38 -08002010 }
2011
2012 /* copy the MAC address out of the NVM */
2013 if (hw->mac.ops.read_mac_addr(hw))
2014 dev_err(&pdev->dev, "NVM Read Error\n");
2015
2016 memcpy(netdev->dev_addr, hw->mac.addr, netdev->addr_len);
2017 memcpy(netdev->perm_addr, hw->mac.addr, netdev->addr_len);
2018
2019 if (!is_valid_ether_addr(netdev->perm_addr)) {
2020 dev_err(&pdev->dev, "Invalid MAC Address\n");
2021 err = -EIO;
2022 goto err_eeprom;
2023 }
2024
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00002025 /* get firmware version for ethtool -i */
2026 igb_set_fw_version(adapter);
2027
Joe Perchesc061b182010-08-23 18:20:03 +00002028 setup_timer(&adapter->watchdog_timer, igb_watchdog,
Alexander Duyck0e340482009-03-20 00:17:08 +00002029 (unsigned long) adapter);
Joe Perchesc061b182010-08-23 18:20:03 +00002030 setup_timer(&adapter->phy_info_timer, igb_update_phy_info,
Alexander Duyck0e340482009-03-20 00:17:08 +00002031 (unsigned long) adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08002032
2033 INIT_WORK(&adapter->reset_task, igb_reset_task);
2034 INIT_WORK(&adapter->watchdog_task, igb_watchdog_task);
2035
Alexander Duyck450c87c2009-02-06 23:22:11 +00002036 /* Initialize link properties that are user-changeable */
Auke Kok9d5c8242008-01-24 02:22:38 -08002037 adapter->fc_autoneg = true;
2038 hw->mac.autoneg = true;
2039 hw->phy.autoneg_advertised = 0x2f;
2040
Alexander Duyck0cce1192009-07-23 18:10:24 +00002041 hw->fc.requested_mode = e1000_fc_default;
2042 hw->fc.current_mode = e1000_fc_default;
Auke Kok9d5c8242008-01-24 02:22:38 -08002043
Auke Kok9d5c8242008-01-24 02:22:38 -08002044 igb_validate_mdi_setting(hw);
2045
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002046 /* By default, support wake on port A */
Alexander Duycka2cf8b62009-03-13 20:41:17 +00002047 if (hw->bus.func == 0)
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002048 adapter->flags |= IGB_FLAG_WOL_SUPPORTED;
2049
2050 /* Check the NVM for wake support on non-port A ports */
2051 if (hw->mac.type >= e1000_82580)
Alexander Duyck55cac242009-11-19 12:42:21 +00002052 hw->nvm.ops.read(hw, NVM_INIT_CONTROL3_PORT_A +
2053 NVM_82580_LAN_FUNC_OFFSET(hw->bus.func), 1,
2054 &eeprom_data);
Alexander Duycka2cf8b62009-03-13 20:41:17 +00002055 else if (hw->bus.func == 1)
2056 hw->nvm.ops.read(hw, NVM_INIT_CONTROL3_PORT_B, 1, &eeprom_data);
Auke Kok9d5c8242008-01-24 02:22:38 -08002057
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002058 if (eeprom_data & IGB_EEPROM_APME)
2059 adapter->flags |= IGB_FLAG_WOL_SUPPORTED;
Auke Kok9d5c8242008-01-24 02:22:38 -08002060
2061 /* now that we have the eeprom settings, apply the special cases where
2062 * the eeprom may be wrong or the board simply won't support wake on
2063 * lan on a particular port */
2064 switch (pdev->device) {
2065 case E1000_DEV_ID_82575GB_QUAD_COPPER:
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002066 adapter->flags &= ~IGB_FLAG_WOL_SUPPORTED;
Auke Kok9d5c8242008-01-24 02:22:38 -08002067 break;
2068 case E1000_DEV_ID_82575EB_FIBER_SERDES:
Alexander Duyck2d064c02008-07-08 15:10:12 -07002069 case E1000_DEV_ID_82576_FIBER:
2070 case E1000_DEV_ID_82576_SERDES:
Auke Kok9d5c8242008-01-24 02:22:38 -08002071 /* Wake events only supported on port A for dual fiber
2072 * regardless of eeprom setting */
2073 if (rd32(E1000_STATUS) & E1000_STATUS_FUNC_1)
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002074 adapter->flags &= ~IGB_FLAG_WOL_SUPPORTED;
Auke Kok9d5c8242008-01-24 02:22:38 -08002075 break;
Alexander Duyckc8ea5ea2009-03-13 20:42:35 +00002076 case E1000_DEV_ID_82576_QUAD_COPPER:
Stefan Assmannd5aa2252010-04-09 09:51:34 +00002077 case E1000_DEV_ID_82576_QUAD_COPPER_ET2:
Alexander Duyckc8ea5ea2009-03-13 20:42:35 +00002078 /* if quad port adapter, disable WoL on all but port A */
2079 if (global_quad_port_a != 0)
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002080 adapter->flags &= ~IGB_FLAG_WOL_SUPPORTED;
Alexander Duyckc8ea5ea2009-03-13 20:42:35 +00002081 else
2082 adapter->flags |= IGB_FLAG_QUAD_PORT_A;
2083 /* Reset for multiple quad port adapters */
2084 if (++global_quad_port_a == 4)
2085 global_quad_port_a = 0;
2086 break;
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002087 default:
2088 /* If the device can't wake, don't set software support */
2089 if (!device_can_wakeup(&adapter->pdev->dev))
2090 adapter->flags &= ~IGB_FLAG_WOL_SUPPORTED;
Auke Kok9d5c8242008-01-24 02:22:38 -08002091 }
2092
2093 /* initialize the wol settings based on the eeprom settings */
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002094 if (adapter->flags & IGB_FLAG_WOL_SUPPORTED)
2095 adapter->wol |= E1000_WUFC_MAG;
2096
2097 /* Some vendors want WoL disabled by default, but still supported */
2098 if ((hw->mac.type == e1000_i350) &&
2099 (pdev->subsystem_vendor == PCI_VENDOR_ID_HP)) {
2100 adapter->flags |= IGB_FLAG_WOL_SUPPORTED;
2101 adapter->wol = 0;
2102 }
2103
2104 device_set_wakeup_enable(&adapter->pdev->dev,
2105 adapter->flags & IGB_FLAG_WOL_SUPPORTED);
Auke Kok9d5c8242008-01-24 02:22:38 -08002106
2107 /* reset the hardware with the new settings */
2108 igb_reset(adapter);
2109
2110 /* let the f/w know that the h/w is now under the control of the
2111 * driver. */
2112 igb_get_hw_control(adapter);
2113
Auke Kok9d5c8242008-01-24 02:22:38 -08002114 strcpy(netdev->name, "eth%d");
2115 err = register_netdev(netdev);
2116 if (err)
2117 goto err_register;
2118
Jesse Brandeburgb168dfc2009-04-17 20:44:32 +00002119 /* carrier off reporting is important to ethtool even BEFORE open */
2120 netif_carrier_off(netdev);
2121
Jeff Kirsher421e02f2008-10-17 11:08:31 -07002122#ifdef CONFIG_IGB_DCA
Alexander Duyckbbd98fe2009-01-31 00:52:30 -08002123 if (dca_add_requester(&pdev->dev) == 0) {
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07002124 adapter->flags |= IGB_FLAG_DCA_ENABLED;
Jeb Cramerfe4506b2008-07-08 15:07:55 -07002125 dev_info(&pdev->dev, "DCA enabled\n");
Jeb Cramerfe4506b2008-07-08 15:07:55 -07002126 igb_setup_dca(adapter);
2127 }
Alexander Duyckc5b9bd52009-10-27 23:46:01 +00002128
Jeb Cramerfe4506b2008-07-08 15:07:55 -07002129#endif
Matthew Vick3c89f6d2012-08-10 05:40:43 +00002130
Anders Berggren673b8b72011-02-04 07:32:32 +00002131 /* do hw tstamp init after resetting */
Richard Cochran7ebae812012-03-16 10:55:37 +00002132 igb_ptp_init(adapter);
Anders Berggren673b8b72011-02-04 07:32:32 +00002133
Auke Kok9d5c8242008-01-24 02:22:38 -08002134 dev_info(&pdev->dev, "Intel(R) Gigabit Ethernet Network Connection\n");
2135 /* print bus type/speed/width info */
Johannes Berg7c510e42008-10-27 17:47:26 -07002136 dev_info(&pdev->dev, "%s: (PCIe:%s:%s) %pM\n",
Auke Kok9d5c8242008-01-24 02:22:38 -08002137 netdev->name,
Alexander Duyck559e9c42009-10-27 23:52:50 +00002138 ((hw->bus.speed == e1000_bus_speed_2500) ? "2.5Gb/s" :
Alexander Duyckff846f52010-04-27 01:02:40 +00002139 (hw->bus.speed == e1000_bus_speed_5000) ? "5.0Gb/s" :
Alexander Duyck559e9c42009-10-27 23:52:50 +00002140 "unknown"),
Alexander Duyck59c3de82009-03-31 20:38:00 +00002141 ((hw->bus.width == e1000_bus_width_pcie_x4) ? "Width x4" :
2142 (hw->bus.width == e1000_bus_width_pcie_x2) ? "Width x2" :
2143 (hw->bus.width == e1000_bus_width_pcie_x1) ? "Width x1" :
2144 "unknown"),
Johannes Berg7c510e42008-10-27 17:47:26 -07002145 netdev->dev_addr);
Auke Kok9d5c8242008-01-24 02:22:38 -08002146
Carolyn Wyborny9835fd72010-11-22 17:17:21 +00002147 ret_val = igb_read_part_string(hw, part_str, E1000_PBANUM_LENGTH);
2148 if (ret_val)
2149 strcpy(part_str, "Unknown");
2150 dev_info(&pdev->dev, "%s: PBA No: %s\n", netdev->name, part_str);
Auke Kok9d5c8242008-01-24 02:22:38 -08002151 dev_info(&pdev->dev,
2152 "Using %s interrupts. %d rx queue(s), %d tx queue(s)\n",
2153 adapter->msix_entries ? "MSI-X" :
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07002154 (adapter->flags & IGB_FLAG_HAS_MSI) ? "MSI" : "legacy",
Auke Kok9d5c8242008-01-24 02:22:38 -08002155 adapter->num_rx_queues, adapter->num_tx_queues);
Carolyn Wyborny09b068d2011-03-11 20:42:13 -08002156 switch (hw->mac.type) {
2157 case e1000_i350:
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002158 case e1000_i210:
2159 case e1000_i211:
Carolyn Wyborny09b068d2011-03-11 20:42:13 -08002160 igb_set_eee_i350(hw);
2161 break;
2162 default:
2163 break;
2164 }
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002165
2166 pm_runtime_put_noidle(&pdev->dev);
Auke Kok9d5c8242008-01-24 02:22:38 -08002167 return 0;
2168
2169err_register:
2170 igb_release_hw_control(adapter);
2171err_eeprom:
2172 if (!igb_check_reset_block(hw))
Alexander Duyckf5f4cf02008-11-21 21:30:24 -08002173 igb_reset_phy(hw);
Auke Kok9d5c8242008-01-24 02:22:38 -08002174
2175 if (hw->flash_address)
2176 iounmap(hw->flash_address);
Auke Kok9d5c8242008-01-24 02:22:38 -08002177err_sw_init:
Alexander Duyck047e0032009-10-27 15:49:27 +00002178 igb_clear_interrupt_scheme(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08002179 iounmap(hw->hw_addr);
2180err_ioremap:
2181 free_netdev(netdev);
2182err_alloc_etherdev:
Alexander Duyck559e9c42009-10-27 23:52:50 +00002183 pci_release_selected_regions(pdev,
2184 pci_select_bars(pdev, IORESOURCE_MEM));
Auke Kok9d5c8242008-01-24 02:22:38 -08002185err_pci_reg:
2186err_dma:
2187 pci_disable_device(pdev);
2188 return err;
2189}
2190
2191/**
2192 * igb_remove - Device Removal Routine
2193 * @pdev: PCI device information struct
2194 *
2195 * igb_remove is called by the PCI subsystem to alert the driver
2196 * that it should release a PCI device. The could be caused by a
2197 * Hot-Plug event, or because the driver is going to be removed from
2198 * memory.
2199 **/
Bill Pemberton9f9a12f2012-12-03 09:24:25 -05002200static void igb_remove(struct pci_dev *pdev)
Auke Kok9d5c8242008-01-24 02:22:38 -08002201{
2202 struct net_device *netdev = pci_get_drvdata(pdev);
2203 struct igb_adapter *adapter = netdev_priv(netdev);
Jeb Cramerfe4506b2008-07-08 15:07:55 -07002204 struct e1000_hw *hw = &adapter->hw;
Auke Kok9d5c8242008-01-24 02:22:38 -08002205
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002206 pm_runtime_get_noresume(&pdev->dev);
Matthew Vicka79f4f82012-08-10 05:40:44 +00002207 igb_ptp_stop(adapter);
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002208
Tejun Heo760141a2010-12-12 16:45:14 +01002209 /*
2210 * The watchdog timer may be rescheduled, so explicitly
2211 * disable watchdog from being rescheduled.
2212 */
Auke Kok9d5c8242008-01-24 02:22:38 -08002213 set_bit(__IGB_DOWN, &adapter->state);
2214 del_timer_sync(&adapter->watchdog_timer);
2215 del_timer_sync(&adapter->phy_info_timer);
2216
Tejun Heo760141a2010-12-12 16:45:14 +01002217 cancel_work_sync(&adapter->reset_task);
2218 cancel_work_sync(&adapter->watchdog_task);
Auke Kok9d5c8242008-01-24 02:22:38 -08002219
Jeff Kirsher421e02f2008-10-17 11:08:31 -07002220#ifdef CONFIG_IGB_DCA
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07002221 if (adapter->flags & IGB_FLAG_DCA_ENABLED) {
Jeb Cramerfe4506b2008-07-08 15:07:55 -07002222 dev_info(&pdev->dev, "DCA disabled\n");
2223 dca_remove_requester(&pdev->dev);
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07002224 adapter->flags &= ~IGB_FLAG_DCA_ENABLED;
Alexander Duyckcbd347a2009-02-15 23:59:44 -08002225 wr32(E1000_DCA_CTRL, E1000_DCA_CTRL_DCA_MODE_DISABLE);
Jeb Cramerfe4506b2008-07-08 15:07:55 -07002226 }
2227#endif
2228
Auke Kok9d5c8242008-01-24 02:22:38 -08002229 /* Release control of h/w to f/w. If f/w is AMT enabled, this
2230 * would have already happened in close and is redundant. */
2231 igb_release_hw_control(adapter);
2232
2233 unregister_netdev(netdev);
2234
Alexander Duyck047e0032009-10-27 15:49:27 +00002235 igb_clear_interrupt_scheme(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08002236
Alexander Duyck37680112009-02-19 20:40:30 -08002237#ifdef CONFIG_PCI_IOV
2238 /* reclaim resources allocated to VFs */
2239 if (adapter->vf_data) {
2240 /* disable iov and allow time for transactions to clear */
Stefan Assmannf5571472012-08-18 04:06:11 +00002241 if (igb_vfs_are_assigned(adapter)) {
2242 dev_info(&pdev->dev, "Unloading driver while VFs are assigned - VFs will not be deallocated\n");
2243 } else {
Greg Rose0224d662011-10-14 02:57:14 +00002244 pci_disable_sriov(pdev);
2245 msleep(500);
Greg Rose0224d662011-10-14 02:57:14 +00002246 }
Alexander Duyck37680112009-02-19 20:40:30 -08002247
2248 kfree(adapter->vf_data);
2249 adapter->vf_data = NULL;
2250 wr32(E1000_IOVCTL, E1000_IOVCTL_REUSE_VFQ);
Jesse Brandeburg945a5152011-07-20 00:56:21 +00002251 wrfl();
Alexander Duyck37680112009-02-19 20:40:30 -08002252 msleep(100);
2253 dev_info(&pdev->dev, "IOV Disabled\n");
2254 }
2255#endif
Alexander Duyck559e9c42009-10-27 23:52:50 +00002256
Alexander Duyck28b07592009-02-06 23:20:31 +00002257 iounmap(hw->hw_addr);
2258 if (hw->flash_address)
2259 iounmap(hw->flash_address);
Alexander Duyck559e9c42009-10-27 23:52:50 +00002260 pci_release_selected_regions(pdev,
2261 pci_select_bars(pdev, IORESOURCE_MEM));
Auke Kok9d5c8242008-01-24 02:22:38 -08002262
Carolyn Wyborny1128c752011-10-14 00:13:49 +00002263 kfree(adapter->shadow_vfta);
Auke Kok9d5c8242008-01-24 02:22:38 -08002264 free_netdev(netdev);
2265
Frans Pop19d5afd2009-10-02 10:04:12 -07002266 pci_disable_pcie_error_reporting(pdev);
Alexander Duyck40a914f2008-11-27 00:24:37 -08002267
Auke Kok9d5c8242008-01-24 02:22:38 -08002268 pci_disable_device(pdev);
2269}
2270
2271/**
Alexander Duycka6b623e2009-10-27 23:47:53 +00002272 * igb_probe_vfs - Initialize vf data storage and add VFs to pci config space
2273 * @adapter: board private structure to initialize
2274 *
2275 * This function initializes the vf specific data storage and then attempts to
2276 * allocate the VFs. The reason for ordering it this way is because it is much
2277 * mor expensive time wise to disable SR-IOV than it is to allocate and free
2278 * the memory for the VFs.
2279 **/
Bill Pemberton9f9a12f2012-12-03 09:24:25 -05002280static void igb_probe_vfs(struct igb_adapter *adapter)
Alexander Duycka6b623e2009-10-27 23:47:53 +00002281{
2282#ifdef CONFIG_PCI_IOV
2283 struct pci_dev *pdev = adapter->pdev;
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002284 struct e1000_hw *hw = &adapter->hw;
Stefan Assmannf5571472012-08-18 04:06:11 +00002285 int old_vfs = pci_num_vf(adapter->pdev);
Greg Rose0224d662011-10-14 02:57:14 +00002286 int i;
Alexander Duycka6b623e2009-10-27 23:47:53 +00002287
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002288 /* Virtualization features not supported on i210 family. */
2289 if ((hw->mac.type == e1000_i210) || (hw->mac.type == e1000_i211))
2290 return;
2291
Greg Rose0224d662011-10-14 02:57:14 +00002292 if (old_vfs) {
2293 dev_info(&pdev->dev, "%d pre-allocated VFs found - override "
2294 "max_vfs setting of %d\n", old_vfs, max_vfs);
2295 adapter->vfs_allocated_count = old_vfs;
Alexander Duycka6b623e2009-10-27 23:47:53 +00002296 }
2297
Greg Rose0224d662011-10-14 02:57:14 +00002298 if (!adapter->vfs_allocated_count)
2299 return;
2300
2301 adapter->vf_data = kcalloc(adapter->vfs_allocated_count,
2302 sizeof(struct vf_data_storage), GFP_KERNEL);
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002303
Greg Rose0224d662011-10-14 02:57:14 +00002304 /* if allocation failed then we do not support SR-IOV */
2305 if (!adapter->vf_data) {
Alexander Duycka6b623e2009-10-27 23:47:53 +00002306 adapter->vfs_allocated_count = 0;
Greg Rose0224d662011-10-14 02:57:14 +00002307 dev_err(&pdev->dev, "Unable to allocate memory for VF "
2308 "Data Storage\n");
2309 goto out;
Alexander Duycka6b623e2009-10-27 23:47:53 +00002310 }
Greg Rose0224d662011-10-14 02:57:14 +00002311
2312 if (!old_vfs) {
2313 if (pci_enable_sriov(pdev, adapter->vfs_allocated_count))
2314 goto err_out;
2315 }
2316 dev_info(&pdev->dev, "%d VFs allocated\n",
2317 adapter->vfs_allocated_count);
2318 for (i = 0; i < adapter->vfs_allocated_count; i++)
2319 igb_vf_configure(adapter, i);
2320
2321 /* DMA Coalescing is not supported in IOV mode. */
2322 adapter->flags &= ~IGB_FLAG_DMAC;
2323 goto out;
2324err_out:
2325 kfree(adapter->vf_data);
2326 adapter->vf_data = NULL;
2327 adapter->vfs_allocated_count = 0;
2328out:
2329 return;
Alexander Duycka6b623e2009-10-27 23:47:53 +00002330#endif /* CONFIG_PCI_IOV */
2331}
2332
Alexander Duyck115f4592009-11-12 18:37:00 +00002333/**
Auke Kok9d5c8242008-01-24 02:22:38 -08002334 * igb_sw_init - Initialize general software structures (struct igb_adapter)
2335 * @adapter: board private structure to initialize
2336 *
2337 * igb_sw_init initializes the Adapter private data structure.
2338 * Fields are initialized based on PCI device information and
2339 * OS network device settings (MTU size).
2340 **/
Bill Pemberton9f9a12f2012-12-03 09:24:25 -05002341static int igb_sw_init(struct igb_adapter *adapter)
Auke Kok9d5c8242008-01-24 02:22:38 -08002342{
2343 struct e1000_hw *hw = &adapter->hw;
2344 struct net_device *netdev = adapter->netdev;
2345 struct pci_dev *pdev = adapter->pdev;
Matthew Vick374a5422012-05-18 04:54:58 +00002346 u32 max_rss_queues;
Auke Kok9d5c8242008-01-24 02:22:38 -08002347
2348 pci_read_config_word(pdev, PCI_COMMAND, &hw->bus.pci_cmd_word);
2349
Alexander Duyck13fde972011-10-05 13:35:24 +00002350 /* set default ring sizes */
Alexander Duyck68fd9912008-11-20 00:48:10 -08002351 adapter->tx_ring_count = IGB_DEFAULT_TXD;
2352 adapter->rx_ring_count = IGB_DEFAULT_RXD;
Alexander Duyck13fde972011-10-05 13:35:24 +00002353
2354 /* set default ITR values */
Alexander Duyck4fc82ad2009-10-27 23:45:42 +00002355 adapter->rx_itr_setting = IGB_DEFAULT_ITR;
2356 adapter->tx_itr_setting = IGB_DEFAULT_ITR;
2357
Alexander Duyck13fde972011-10-05 13:35:24 +00002358 /* set default work limits */
2359 adapter->tx_work_limit = IGB_DEFAULT_TX_WORK;
2360
Alexander Duyck153285f2011-08-26 07:43:32 +00002361 adapter->max_frame_size = netdev->mtu + ETH_HLEN + ETH_FCS_LEN +
2362 VLAN_HLEN;
Auke Kok9d5c8242008-01-24 02:22:38 -08002363 adapter->min_frame_size = ETH_ZLEN + ETH_FCS_LEN;
2364
Eric Dumazet12dcd862010-10-15 17:27:10 +00002365 spin_lock_init(&adapter->stats64_lock);
Alexander Duycka6b623e2009-10-27 23:47:53 +00002366#ifdef CONFIG_PCI_IOV
Carolyn Wyborny6b78bb12011-01-20 06:40:45 +00002367 switch (hw->mac.type) {
2368 case e1000_82576:
2369 case e1000_i350:
Stefan Assmann9b082d72011-02-24 20:03:31 +00002370 if (max_vfs > 7) {
2371 dev_warn(&pdev->dev,
2372 "Maximum of 7 VFs per PF, using max\n");
2373 adapter->vfs_allocated_count = 7;
2374 } else
2375 adapter->vfs_allocated_count = max_vfs;
Carolyn Wyborny6b78bb12011-01-20 06:40:45 +00002376 break;
2377 default:
2378 break;
2379 }
Alexander Duycka6b623e2009-10-27 23:47:53 +00002380#endif /* CONFIG_PCI_IOV */
Matthew Vick374a5422012-05-18 04:54:58 +00002381
2382 /* Determine the maximum number of RSS queues supported. */
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002383 switch (hw->mac.type) {
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002384 case e1000_i211:
Matthew Vick374a5422012-05-18 04:54:58 +00002385 max_rss_queues = IGB_MAX_RX_QUEUES_I211;
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002386 break;
Matthew Vick374a5422012-05-18 04:54:58 +00002387 case e1000_82575:
2388 case e1000_i210:
2389 max_rss_queues = IGB_MAX_RX_QUEUES_82575;
2390 break;
2391 case e1000_i350:
2392 /* I350 cannot do RSS and SR-IOV at the same time */
2393 if (!!adapter->vfs_allocated_count) {
2394 max_rss_queues = 1;
2395 break;
2396 }
2397 /* fall through */
2398 case e1000_82576:
2399 if (!!adapter->vfs_allocated_count) {
2400 max_rss_queues = 2;
2401 break;
2402 }
2403 /* fall through */
2404 case e1000_82580:
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002405 default:
Matthew Vick374a5422012-05-18 04:54:58 +00002406 max_rss_queues = IGB_MAX_RX_QUEUES;
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002407 break;
2408 }
Alexander Duycka99955f2009-11-12 18:37:19 +00002409
Matthew Vick374a5422012-05-18 04:54:58 +00002410 adapter->rss_queues = min_t(u32, max_rss_queues, num_online_cpus());
2411
2412 /* Determine if we need to pair queues. */
2413 switch (hw->mac.type) {
2414 case e1000_82575:
2415 case e1000_i211:
2416 /* Device supports enough interrupts without queue pairing. */
2417 break;
2418 case e1000_82576:
2419 /*
2420 * If VFs are going to be allocated with RSS queues then we
2421 * should pair the queues in order to conserve interrupts due
2422 * to limited supply.
2423 */
2424 if ((adapter->rss_queues > 1) &&
2425 (adapter->vfs_allocated_count > 6))
2426 adapter->flags |= IGB_FLAG_QUEUE_PAIRS;
2427 /* fall through */
2428 case e1000_82580:
2429 case e1000_i350:
2430 case e1000_i210:
2431 default:
2432 /*
2433 * If rss_queues > half of max_rss_queues, pair the queues in
2434 * order to conserve interrupts due to limited supply.
2435 */
2436 if (adapter->rss_queues > (max_rss_queues / 2))
2437 adapter->flags |= IGB_FLAG_QUEUE_PAIRS;
2438 break;
2439 }
Alexander Duycka99955f2009-11-12 18:37:19 +00002440
Carolyn Wyborny1128c752011-10-14 00:13:49 +00002441 /* Setup and initialize a copy of the hw vlan table array */
2442 adapter->shadow_vfta = kzalloc(sizeof(u32) *
2443 E1000_VLAN_FILTER_TBL_SIZE,
2444 GFP_ATOMIC);
2445
Alexander Duycka6b623e2009-10-27 23:47:53 +00002446 /* This call may decrease the number of queues */
Alexander Duyck047e0032009-10-27 15:49:27 +00002447 if (igb_init_interrupt_scheme(adapter)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08002448 dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
2449 return -ENOMEM;
2450 }
2451
Alexander Duycka6b623e2009-10-27 23:47:53 +00002452 igb_probe_vfs(adapter);
2453
Auke Kok9d5c8242008-01-24 02:22:38 -08002454 /* Explicitly disable IRQ since the NIC can be in any state. */
2455 igb_irq_disable(adapter);
2456
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002457 if (hw->mac.type >= e1000_i350)
Carolyn Wyborny831ec0b2011-03-11 20:43:54 -08002458 adapter->flags &= ~IGB_FLAG_DMAC;
2459
Auke Kok9d5c8242008-01-24 02:22:38 -08002460 set_bit(__IGB_DOWN, &adapter->state);
2461 return 0;
2462}
2463
2464/**
2465 * igb_open - Called when a network interface is made active
2466 * @netdev: network interface device structure
2467 *
2468 * Returns 0 on success, negative value on failure
2469 *
2470 * The open entry point is called when a network interface is made
2471 * active by the system (IFF_UP). At this point all resources needed
2472 * for transmit and receive operations are allocated, the interrupt
2473 * handler is registered with the OS, the watchdog timer is started,
2474 * and the stack is notified that the interface is ready.
2475 **/
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002476static int __igb_open(struct net_device *netdev, bool resuming)
Auke Kok9d5c8242008-01-24 02:22:38 -08002477{
2478 struct igb_adapter *adapter = netdev_priv(netdev);
2479 struct e1000_hw *hw = &adapter->hw;
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002480 struct pci_dev *pdev = adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08002481 int err;
2482 int i;
2483
2484 /* disallow open during test */
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002485 if (test_bit(__IGB_TESTING, &adapter->state)) {
2486 WARN_ON(resuming);
Auke Kok9d5c8242008-01-24 02:22:38 -08002487 return -EBUSY;
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002488 }
2489
2490 if (!resuming)
2491 pm_runtime_get_sync(&pdev->dev);
Auke Kok9d5c8242008-01-24 02:22:38 -08002492
Jesse Brandeburgb168dfc2009-04-17 20:44:32 +00002493 netif_carrier_off(netdev);
2494
Auke Kok9d5c8242008-01-24 02:22:38 -08002495 /* allocate transmit descriptors */
2496 err = igb_setup_all_tx_resources(adapter);
2497 if (err)
2498 goto err_setup_tx;
2499
2500 /* allocate receive descriptors */
2501 err = igb_setup_all_rx_resources(adapter);
2502 if (err)
2503 goto err_setup_rx;
2504
Nick Nunley88a268c2010-02-17 01:01:59 +00002505 igb_power_up_link(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08002506
Auke Kok9d5c8242008-01-24 02:22:38 -08002507 /* before we allocate an interrupt, we must be ready to handle it.
2508 * Setting DEBUG_SHIRQ in the kernel makes it fire an interrupt
2509 * as soon as we call pci_request_irq, so we have to setup our
2510 * clean_rx handler before we do so. */
2511 igb_configure(adapter);
2512
2513 err = igb_request_irq(adapter);
2514 if (err)
2515 goto err_req_irq;
2516
Alexander Duyck0c2cc022012-09-25 00:31:22 +00002517 /* Notify the stack of the actual queue counts. */
2518 err = netif_set_real_num_tx_queues(adapter->netdev,
2519 adapter->num_tx_queues);
2520 if (err)
2521 goto err_set_queues;
2522
2523 err = netif_set_real_num_rx_queues(adapter->netdev,
2524 adapter->num_rx_queues);
2525 if (err)
2526 goto err_set_queues;
2527
Auke Kok9d5c8242008-01-24 02:22:38 -08002528 /* From here on the code is the same as igb_up() */
2529 clear_bit(__IGB_DOWN, &adapter->state);
2530
Alexander Duyck0d1ae7f2011-08-26 07:46:34 +00002531 for (i = 0; i < adapter->num_q_vectors; i++)
2532 napi_enable(&(adapter->q_vector[i]->napi));
Auke Kok9d5c8242008-01-24 02:22:38 -08002533
2534 /* Clear any pending interrupts. */
2535 rd32(E1000_ICR);
PJ Waskiewicz844290e2008-06-27 11:00:39 -07002536
2537 igb_irq_enable(adapter);
2538
Alexander Duyckd4960302009-10-27 15:53:45 +00002539 /* notify VFs that reset has been completed */
2540 if (adapter->vfs_allocated_count) {
2541 u32 reg_data = rd32(E1000_CTRL_EXT);
2542 reg_data |= E1000_CTRL_EXT_PFRSTD;
2543 wr32(E1000_CTRL_EXT, reg_data);
2544 }
2545
Jeff Kirsherd55b53f2008-07-18 04:33:03 -07002546 netif_tx_start_all_queues(netdev);
2547
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002548 if (!resuming)
2549 pm_runtime_put(&pdev->dev);
2550
Alexander Duyck25568a52009-10-27 23:49:59 +00002551 /* start the watchdog. */
2552 hw->mac.get_link_status = 1;
2553 schedule_work(&adapter->watchdog_task);
Auke Kok9d5c8242008-01-24 02:22:38 -08002554
2555 return 0;
2556
Alexander Duyck0c2cc022012-09-25 00:31:22 +00002557err_set_queues:
2558 igb_free_irq(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08002559err_req_irq:
2560 igb_release_hw_control(adapter);
Nick Nunley88a268c2010-02-17 01:01:59 +00002561 igb_power_down_link(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08002562 igb_free_all_rx_resources(adapter);
2563err_setup_rx:
2564 igb_free_all_tx_resources(adapter);
2565err_setup_tx:
2566 igb_reset(adapter);
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002567 if (!resuming)
2568 pm_runtime_put(&pdev->dev);
Auke Kok9d5c8242008-01-24 02:22:38 -08002569
2570 return err;
2571}
2572
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002573static int igb_open(struct net_device *netdev)
2574{
2575 return __igb_open(netdev, false);
2576}
2577
Auke Kok9d5c8242008-01-24 02:22:38 -08002578/**
2579 * igb_close - Disables a network interface
2580 * @netdev: network interface device structure
2581 *
2582 * Returns 0, this is not allowed to fail
2583 *
2584 * The close entry point is called when an interface is de-activated
2585 * by the OS. The hardware is still under the driver's control, but
2586 * needs to be disabled. A global MAC reset is issued to stop the
2587 * hardware, and all transmit and receive resources are freed.
2588 **/
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002589static int __igb_close(struct net_device *netdev, bool suspending)
Auke Kok9d5c8242008-01-24 02:22:38 -08002590{
2591 struct igb_adapter *adapter = netdev_priv(netdev);
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002592 struct pci_dev *pdev = adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08002593
2594 WARN_ON(test_bit(__IGB_RESETTING, &adapter->state));
Auke Kok9d5c8242008-01-24 02:22:38 -08002595
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002596 if (!suspending)
2597 pm_runtime_get_sync(&pdev->dev);
2598
2599 igb_down(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08002600 igb_free_irq(adapter);
2601
2602 igb_free_all_tx_resources(adapter);
2603 igb_free_all_rx_resources(adapter);
2604
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002605 if (!suspending)
2606 pm_runtime_put_sync(&pdev->dev);
Auke Kok9d5c8242008-01-24 02:22:38 -08002607 return 0;
2608}
2609
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002610static int igb_close(struct net_device *netdev)
2611{
2612 return __igb_close(netdev, false);
2613}
2614
Auke Kok9d5c8242008-01-24 02:22:38 -08002615/**
2616 * igb_setup_tx_resources - allocate Tx resources (Descriptors)
Auke Kok9d5c8242008-01-24 02:22:38 -08002617 * @tx_ring: tx descriptor ring (for a specific queue) to setup
2618 *
2619 * Return 0 on success, negative on failure
2620 **/
Alexander Duyck80785292009-10-27 15:51:47 +00002621int igb_setup_tx_resources(struct igb_ring *tx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08002622{
Alexander Duyck59d71982010-04-27 13:09:25 +00002623 struct device *dev = tx_ring->dev;
Auke Kok9d5c8242008-01-24 02:22:38 -08002624 int size;
2625
Alexander Duyck06034642011-08-26 07:44:22 +00002626 size = sizeof(struct igb_tx_buffer) * tx_ring->count;
Alexander Duyckf33005a2012-09-13 06:27:55 +00002627
2628 tx_ring->tx_buffer_info = vzalloc(size);
Alexander Duyck06034642011-08-26 07:44:22 +00002629 if (!tx_ring->tx_buffer_info)
Auke Kok9d5c8242008-01-24 02:22:38 -08002630 goto err;
Auke Kok9d5c8242008-01-24 02:22:38 -08002631
2632 /* round up to nearest 4K */
Alexander Duyck85e8d002009-02-16 00:00:20 -08002633 tx_ring->size = tx_ring->count * sizeof(union e1000_adv_tx_desc);
Auke Kok9d5c8242008-01-24 02:22:38 -08002634 tx_ring->size = ALIGN(tx_ring->size, 4096);
2635
Alexander Duyck5536d212012-09-25 00:31:17 +00002636 tx_ring->desc = dma_alloc_coherent(dev, tx_ring->size,
2637 &tx_ring->dma, GFP_KERNEL);
Auke Kok9d5c8242008-01-24 02:22:38 -08002638 if (!tx_ring->desc)
2639 goto err;
2640
Auke Kok9d5c8242008-01-24 02:22:38 -08002641 tx_ring->next_to_use = 0;
2642 tx_ring->next_to_clean = 0;
Alexander Duyck81c2fc22011-08-26 07:45:20 +00002643
Auke Kok9d5c8242008-01-24 02:22:38 -08002644 return 0;
2645
2646err:
Alexander Duyck06034642011-08-26 07:44:22 +00002647 vfree(tx_ring->tx_buffer_info);
Alexander Duyckf33005a2012-09-13 06:27:55 +00002648 tx_ring->tx_buffer_info = NULL;
2649 dev_err(dev, "Unable to allocate memory for the Tx descriptor ring\n");
Auke Kok9d5c8242008-01-24 02:22:38 -08002650 return -ENOMEM;
2651}
2652
2653/**
2654 * igb_setup_all_tx_resources - wrapper to allocate Tx resources
2655 * (Descriptors) for all queues
2656 * @adapter: board private structure
2657 *
2658 * Return 0 on success, negative on failure
2659 **/
2660static int igb_setup_all_tx_resources(struct igb_adapter *adapter)
2661{
Alexander Duyck439705e2009-10-27 23:49:20 +00002662 struct pci_dev *pdev = adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08002663 int i, err = 0;
2664
2665 for (i = 0; i < adapter->num_tx_queues; i++) {
Alexander Duyck3025a442010-02-17 01:02:39 +00002666 err = igb_setup_tx_resources(adapter->tx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08002667 if (err) {
Alexander Duyck439705e2009-10-27 23:49:20 +00002668 dev_err(&pdev->dev,
Auke Kok9d5c8242008-01-24 02:22:38 -08002669 "Allocation for Tx Queue %u failed\n", i);
2670 for (i--; i >= 0; i--)
Alexander Duyck3025a442010-02-17 01:02:39 +00002671 igb_free_tx_resources(adapter->tx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08002672 break;
2673 }
2674 }
2675
2676 return err;
2677}
2678
2679/**
Alexander Duyck85b430b2009-10-27 15:50:29 +00002680 * igb_setup_tctl - configure the transmit control registers
2681 * @adapter: Board private structure
Auke Kok9d5c8242008-01-24 02:22:38 -08002682 **/
Alexander Duyckd7ee5b32009-10-27 15:54:23 +00002683void igb_setup_tctl(struct igb_adapter *adapter)
Auke Kok9d5c8242008-01-24 02:22:38 -08002684{
Auke Kok9d5c8242008-01-24 02:22:38 -08002685 struct e1000_hw *hw = &adapter->hw;
2686 u32 tctl;
Auke Kok9d5c8242008-01-24 02:22:38 -08002687
Alexander Duyck85b430b2009-10-27 15:50:29 +00002688 /* disable queue 0 which is enabled by default on 82575 and 82576 */
2689 wr32(E1000_TXDCTL(0), 0);
Auke Kok9d5c8242008-01-24 02:22:38 -08002690
2691 /* Program the Transmit Control Register */
Auke Kok9d5c8242008-01-24 02:22:38 -08002692 tctl = rd32(E1000_TCTL);
2693 tctl &= ~E1000_TCTL_CT;
2694 tctl |= E1000_TCTL_PSP | E1000_TCTL_RTLC |
2695 (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
2696
2697 igb_config_collision_dist(hw);
2698
Auke Kok9d5c8242008-01-24 02:22:38 -08002699 /* Enable transmits */
2700 tctl |= E1000_TCTL_EN;
2701
2702 wr32(E1000_TCTL, tctl);
2703}
2704
2705/**
Alexander Duyck85b430b2009-10-27 15:50:29 +00002706 * igb_configure_tx_ring - Configure transmit ring after Reset
2707 * @adapter: board private structure
2708 * @ring: tx ring to configure
2709 *
2710 * Configure a transmit ring after a reset.
2711 **/
Alexander Duyckd7ee5b32009-10-27 15:54:23 +00002712void igb_configure_tx_ring(struct igb_adapter *adapter,
2713 struct igb_ring *ring)
Alexander Duyck85b430b2009-10-27 15:50:29 +00002714{
2715 struct e1000_hw *hw = &adapter->hw;
Alexander Duycka74420e2011-08-26 07:43:27 +00002716 u32 txdctl = 0;
Alexander Duyck85b430b2009-10-27 15:50:29 +00002717 u64 tdba = ring->dma;
2718 int reg_idx = ring->reg_idx;
2719
2720 /* disable the queue */
Alexander Duycka74420e2011-08-26 07:43:27 +00002721 wr32(E1000_TXDCTL(reg_idx), 0);
Alexander Duyck85b430b2009-10-27 15:50:29 +00002722 wrfl();
2723 mdelay(10);
2724
2725 wr32(E1000_TDLEN(reg_idx),
2726 ring->count * sizeof(union e1000_adv_tx_desc));
2727 wr32(E1000_TDBAL(reg_idx),
2728 tdba & 0x00000000ffffffffULL);
2729 wr32(E1000_TDBAH(reg_idx), tdba >> 32);
2730
Alexander Duyckfce99e32009-10-27 15:51:27 +00002731 ring->tail = hw->hw_addr + E1000_TDT(reg_idx);
Alexander Duycka74420e2011-08-26 07:43:27 +00002732 wr32(E1000_TDH(reg_idx), 0);
Alexander Duyckfce99e32009-10-27 15:51:27 +00002733 writel(0, ring->tail);
Alexander Duyck85b430b2009-10-27 15:50:29 +00002734
2735 txdctl |= IGB_TX_PTHRESH;
2736 txdctl |= IGB_TX_HTHRESH << 8;
2737 txdctl |= IGB_TX_WTHRESH << 16;
2738
2739 txdctl |= E1000_TXDCTL_QUEUE_ENABLE;
2740 wr32(E1000_TXDCTL(reg_idx), txdctl);
2741}
2742
2743/**
2744 * igb_configure_tx - Configure transmit Unit after Reset
2745 * @adapter: board private structure
2746 *
2747 * Configure the Tx unit of the MAC after a reset.
2748 **/
2749static void igb_configure_tx(struct igb_adapter *adapter)
2750{
2751 int i;
2752
2753 for (i = 0; i < adapter->num_tx_queues; i++)
Alexander Duyck3025a442010-02-17 01:02:39 +00002754 igb_configure_tx_ring(adapter, adapter->tx_ring[i]);
Alexander Duyck85b430b2009-10-27 15:50:29 +00002755}
2756
2757/**
Auke Kok9d5c8242008-01-24 02:22:38 -08002758 * igb_setup_rx_resources - allocate Rx resources (Descriptors)
Auke Kok9d5c8242008-01-24 02:22:38 -08002759 * @rx_ring: rx descriptor ring (for a specific queue) to setup
2760 *
2761 * Returns 0 on success, negative on failure
2762 **/
Alexander Duyck80785292009-10-27 15:51:47 +00002763int igb_setup_rx_resources(struct igb_ring *rx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08002764{
Alexander Duyck59d71982010-04-27 13:09:25 +00002765 struct device *dev = rx_ring->dev;
Alexander Duyckf33005a2012-09-13 06:27:55 +00002766 int size;
Auke Kok9d5c8242008-01-24 02:22:38 -08002767
Alexander Duyck06034642011-08-26 07:44:22 +00002768 size = sizeof(struct igb_rx_buffer) * rx_ring->count;
Alexander Duyckf33005a2012-09-13 06:27:55 +00002769
2770 rx_ring->rx_buffer_info = vzalloc(size);
Alexander Duyck06034642011-08-26 07:44:22 +00002771 if (!rx_ring->rx_buffer_info)
Auke Kok9d5c8242008-01-24 02:22:38 -08002772 goto err;
Auke Kok9d5c8242008-01-24 02:22:38 -08002773
Auke Kok9d5c8242008-01-24 02:22:38 -08002774 /* Round up to nearest 4K */
Alexander Duyckf33005a2012-09-13 06:27:55 +00002775 rx_ring->size = rx_ring->count * sizeof(union e1000_adv_rx_desc);
Auke Kok9d5c8242008-01-24 02:22:38 -08002776 rx_ring->size = ALIGN(rx_ring->size, 4096);
2777
Alexander Duyck5536d212012-09-25 00:31:17 +00002778 rx_ring->desc = dma_alloc_coherent(dev, rx_ring->size,
2779 &rx_ring->dma, GFP_KERNEL);
Auke Kok9d5c8242008-01-24 02:22:38 -08002780 if (!rx_ring->desc)
2781 goto err;
2782
Alexander Duyckcbc8e552012-09-25 00:31:02 +00002783 rx_ring->next_to_alloc = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08002784 rx_ring->next_to_clean = 0;
2785 rx_ring->next_to_use = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08002786
Auke Kok9d5c8242008-01-24 02:22:38 -08002787 return 0;
2788
2789err:
Alexander Duyck06034642011-08-26 07:44:22 +00002790 vfree(rx_ring->rx_buffer_info);
2791 rx_ring->rx_buffer_info = NULL;
Alexander Duyckf33005a2012-09-13 06:27:55 +00002792 dev_err(dev, "Unable to allocate memory for the Rx descriptor ring\n");
Auke Kok9d5c8242008-01-24 02:22:38 -08002793 return -ENOMEM;
2794}
2795
2796/**
2797 * igb_setup_all_rx_resources - wrapper to allocate Rx resources
2798 * (Descriptors) for all queues
2799 * @adapter: board private structure
2800 *
2801 * Return 0 on success, negative on failure
2802 **/
2803static int igb_setup_all_rx_resources(struct igb_adapter *adapter)
2804{
Alexander Duyck439705e2009-10-27 23:49:20 +00002805 struct pci_dev *pdev = adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08002806 int i, err = 0;
2807
2808 for (i = 0; i < adapter->num_rx_queues; i++) {
Alexander Duyck3025a442010-02-17 01:02:39 +00002809 err = igb_setup_rx_resources(adapter->rx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08002810 if (err) {
Alexander Duyck439705e2009-10-27 23:49:20 +00002811 dev_err(&pdev->dev,
Auke Kok9d5c8242008-01-24 02:22:38 -08002812 "Allocation for Rx Queue %u failed\n", i);
2813 for (i--; i >= 0; i--)
Alexander Duyck3025a442010-02-17 01:02:39 +00002814 igb_free_rx_resources(adapter->rx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08002815 break;
2816 }
2817 }
2818
2819 return err;
2820}
2821
2822/**
Alexander Duyck06cf2662009-10-27 15:53:25 +00002823 * igb_setup_mrqc - configure the multiple receive queue control registers
2824 * @adapter: Board private structure
2825 **/
2826static void igb_setup_mrqc(struct igb_adapter *adapter)
2827{
2828 struct e1000_hw *hw = &adapter->hw;
2829 u32 mrqc, rxcsum;
Alexander Duyck797fd4b2012-09-13 06:28:11 +00002830 u32 j, num_rx_queues, shift = 0;
Alexander Duycka57fe232012-09-13 06:28:16 +00002831 static const u32 rsskey[10] = { 0xDA565A6D, 0xC20E5B25, 0x3D256741,
2832 0xB08FA343, 0xCB2BCAD0, 0xB4307BAE,
2833 0xA32DCB77, 0x0CF23080, 0x3BB7426A,
2834 0xFA01ACBE };
Alexander Duyck06cf2662009-10-27 15:53:25 +00002835
2836 /* Fill out hash function seeds */
Alexander Duycka57fe232012-09-13 06:28:16 +00002837 for (j = 0; j < 10; j++)
2838 wr32(E1000_RSSRK(j), rsskey[j]);
Alexander Duyck06cf2662009-10-27 15:53:25 +00002839
Alexander Duycka99955f2009-11-12 18:37:19 +00002840 num_rx_queues = adapter->rss_queues;
Alexander Duyck06cf2662009-10-27 15:53:25 +00002841
Alexander Duyck797fd4b2012-09-13 06:28:11 +00002842 switch (hw->mac.type) {
2843 case e1000_82575:
2844 shift = 6;
2845 break;
2846 case e1000_82576:
2847 /* 82576 supports 2 RSS queues for SR-IOV */
2848 if (adapter->vfs_allocated_count) {
Alexander Duyck06cf2662009-10-27 15:53:25 +00002849 shift = 3;
2850 num_rx_queues = 2;
Alexander Duyck06cf2662009-10-27 15:53:25 +00002851 }
Alexander Duyck797fd4b2012-09-13 06:28:11 +00002852 break;
2853 default:
2854 break;
Alexander Duyck06cf2662009-10-27 15:53:25 +00002855 }
2856
Alexander Duyck797fd4b2012-09-13 06:28:11 +00002857 /*
2858 * Populate the indirection table 4 entries at a time. To do this
2859 * we are generating the results for n and n+2 and then interleaving
2860 * those with the results with n+1 and n+3.
2861 */
2862 for (j = 0; j < 32; j++) {
2863 /* first pass generates n and n+2 */
2864 u32 base = ((j * 0x00040004) + 0x00020000) * num_rx_queues;
2865 u32 reta = (base & 0x07800780) >> (7 - shift);
2866
2867 /* second pass generates n+1 and n+3 */
2868 base += 0x00010001 * num_rx_queues;
2869 reta |= (base & 0x07800780) << (1 + shift);
2870
2871 wr32(E1000_RETA(j), reta);
Alexander Duyck06cf2662009-10-27 15:53:25 +00002872 }
2873
2874 /*
2875 * Disable raw packet checksumming so that RSS hash is placed in
2876 * descriptor on writeback. No need to enable TCP/UDP/IP checksum
2877 * offloads as they are enabled by default
2878 */
2879 rxcsum = rd32(E1000_RXCSUM);
2880 rxcsum |= E1000_RXCSUM_PCSD;
2881
2882 if (adapter->hw.mac.type >= e1000_82576)
2883 /* Enable Receive Checksum Offload for SCTP */
2884 rxcsum |= E1000_RXCSUM_CRCOFL;
2885
2886 /* Don't need to set TUOFL or IPOFL, they default to 1 */
2887 wr32(E1000_RXCSUM, rxcsum);
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002888
Akeem G. Abodunrin039454a2012-11-13 04:03:21 +00002889 /* Generate RSS hash based on packet types, TCP/UDP
2890 * port numbers and/or IPv4/v6 src and dst addresses
2891 */
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002892 mrqc = E1000_MRQC_RSS_FIELD_IPV4 |
2893 E1000_MRQC_RSS_FIELD_IPV4_TCP |
2894 E1000_MRQC_RSS_FIELD_IPV6 |
2895 E1000_MRQC_RSS_FIELD_IPV6_TCP |
2896 E1000_MRQC_RSS_FIELD_IPV6_TCP_EX;
Alexander Duyck06cf2662009-10-27 15:53:25 +00002897
Akeem G. Abodunrin039454a2012-11-13 04:03:21 +00002898 if (adapter->flags & IGB_FLAG_RSS_FIELD_IPV4_UDP)
2899 mrqc |= E1000_MRQC_RSS_FIELD_IPV4_UDP;
2900 if (adapter->flags & IGB_FLAG_RSS_FIELD_IPV6_UDP)
2901 mrqc |= E1000_MRQC_RSS_FIELD_IPV6_UDP;
2902
Alexander Duyck06cf2662009-10-27 15:53:25 +00002903 /* If VMDq is enabled then we set the appropriate mode for that, else
2904 * we default to RSS so that an RSS hash is calculated per packet even
2905 * if we are only using one queue */
2906 if (adapter->vfs_allocated_count) {
2907 if (hw->mac.type > e1000_82575) {
2908 /* Set the default pool for the PF's first queue */
2909 u32 vtctl = rd32(E1000_VT_CTL);
2910 vtctl &= ~(E1000_VT_CTL_DEFAULT_POOL_MASK |
2911 E1000_VT_CTL_DISABLE_DEF_POOL);
2912 vtctl |= adapter->vfs_allocated_count <<
2913 E1000_VT_CTL_DEFAULT_POOL_SHIFT;
2914 wr32(E1000_VT_CTL, vtctl);
2915 }
Alexander Duycka99955f2009-11-12 18:37:19 +00002916 if (adapter->rss_queues > 1)
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002917 mrqc |= E1000_MRQC_ENABLE_VMDQ_RSS_2Q;
Alexander Duyck06cf2662009-10-27 15:53:25 +00002918 else
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002919 mrqc |= E1000_MRQC_ENABLE_VMDQ;
Alexander Duyck06cf2662009-10-27 15:53:25 +00002920 } else {
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002921 if (hw->mac.type != e1000_i211)
2922 mrqc |= E1000_MRQC_ENABLE_RSS_4Q;
Alexander Duyck06cf2662009-10-27 15:53:25 +00002923 }
2924 igb_vmm_control(adapter);
2925
Alexander Duyck06cf2662009-10-27 15:53:25 +00002926 wr32(E1000_MRQC, mrqc);
2927}
2928
2929/**
Auke Kok9d5c8242008-01-24 02:22:38 -08002930 * igb_setup_rctl - configure the receive control registers
2931 * @adapter: Board private structure
2932 **/
Alexander Duyckd7ee5b32009-10-27 15:54:23 +00002933void igb_setup_rctl(struct igb_adapter *adapter)
Auke Kok9d5c8242008-01-24 02:22:38 -08002934{
2935 struct e1000_hw *hw = &adapter->hw;
2936 u32 rctl;
Auke Kok9d5c8242008-01-24 02:22:38 -08002937
2938 rctl = rd32(E1000_RCTL);
2939
2940 rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
Alexander Duyck69d728b2008-11-25 01:04:03 -08002941 rctl &= ~(E1000_RCTL_LBM_TCVR | E1000_RCTL_LBM_MAC);
Auke Kok9d5c8242008-01-24 02:22:38 -08002942
Alexander Duyck69d728b2008-11-25 01:04:03 -08002943 rctl |= E1000_RCTL_EN | E1000_RCTL_BAM | E1000_RCTL_RDMTS_HALF |
Alexander Duyck28b07592009-02-06 23:20:31 +00002944 (hw->mac.mc_filter_type << E1000_RCTL_MO_SHIFT);
Auke Kok9d5c8242008-01-24 02:22:38 -08002945
Auke Kok87cb7e82008-07-08 15:08:29 -07002946 /*
2947 * enable stripping of CRC. It's unlikely this will break BMC
2948 * redirection as it did with e1000. Newer features require
2949 * that the HW strips the CRC.
Alexander Duyck73cd78f2009-02-12 18:16:59 +00002950 */
Auke Kok87cb7e82008-07-08 15:08:29 -07002951 rctl |= E1000_RCTL_SECRC;
Auke Kok9d5c8242008-01-24 02:22:38 -08002952
Alexander Duyck559e9c42009-10-27 23:52:50 +00002953 /* disable store bad packets and clear size bits. */
Alexander Duyckec54d7d2009-01-31 00:52:57 -08002954 rctl &= ~(E1000_RCTL_SBP | E1000_RCTL_SZ_256);
Auke Kok9d5c8242008-01-24 02:22:38 -08002955
Alexander Duyck6ec43fe2009-10-27 15:50:48 +00002956 /* enable LPE to prevent packets larger than max_frame_size */
2957 rctl |= E1000_RCTL_LPE;
Auke Kok9d5c8242008-01-24 02:22:38 -08002958
Alexander Duyck952f72a2009-10-27 15:51:07 +00002959 /* disable queue 0 to prevent tail write w/o re-config */
2960 wr32(E1000_RXDCTL(0), 0);
Auke Kok9d5c8242008-01-24 02:22:38 -08002961
Alexander Duycke1739522009-02-19 20:39:44 -08002962 /* Attention!!! For SR-IOV PF driver operations you must enable
2963 * queue drop for all VF and PF queues to prevent head of line blocking
2964 * if an un-trusted VF does not provide descriptors to hardware.
2965 */
2966 if (adapter->vfs_allocated_count) {
Alexander Duycke1739522009-02-19 20:39:44 -08002967 /* set all queue drop enable bits */
2968 wr32(E1000_QDE, ALL_QUEUES);
Alexander Duycke1739522009-02-19 20:39:44 -08002969 }
2970
Ben Greear89eaefb2012-03-06 09:41:58 +00002971 /* This is useful for sniffing bad packets. */
2972 if (adapter->netdev->features & NETIF_F_RXALL) {
2973 /* UPE and MPE will be handled by normal PROMISC logic
2974 * in e1000e_set_rx_mode */
2975 rctl |= (E1000_RCTL_SBP | /* Receive bad packets */
2976 E1000_RCTL_BAM | /* RX All Bcast Pkts */
2977 E1000_RCTL_PMCF); /* RX All MAC Ctrl Pkts */
2978
2979 rctl &= ~(E1000_RCTL_VFE | /* Disable VLAN filter */
2980 E1000_RCTL_DPF | /* Allow filtered pause */
2981 E1000_RCTL_CFIEN); /* Dis VLAN CFIEN Filter */
2982 /* Do not mess with E1000_CTRL_VME, it affects transmit as well,
2983 * and that breaks VLANs.
2984 */
2985 }
2986
Auke Kok9d5c8242008-01-24 02:22:38 -08002987 wr32(E1000_RCTL, rctl);
2988}
2989
Alexander Duyck7d5753f2009-10-27 23:47:16 +00002990static inline int igb_set_vf_rlpml(struct igb_adapter *adapter, int size,
2991 int vfn)
2992{
2993 struct e1000_hw *hw = &adapter->hw;
2994 u32 vmolr;
2995
2996 /* if it isn't the PF check to see if VFs are enabled and
2997 * increase the size to support vlan tags */
2998 if (vfn < adapter->vfs_allocated_count &&
2999 adapter->vf_data[vfn].vlans_enabled)
3000 size += VLAN_TAG_SIZE;
3001
3002 vmolr = rd32(E1000_VMOLR(vfn));
3003 vmolr &= ~E1000_VMOLR_RLPML_MASK;
3004 vmolr |= size | E1000_VMOLR_LPE;
3005 wr32(E1000_VMOLR(vfn), vmolr);
3006
3007 return 0;
3008}
3009
Auke Kok9d5c8242008-01-24 02:22:38 -08003010/**
Alexander Duycke1739522009-02-19 20:39:44 -08003011 * igb_rlpml_set - set maximum receive packet size
3012 * @adapter: board private structure
3013 *
3014 * Configure maximum receivable packet size.
3015 **/
3016static void igb_rlpml_set(struct igb_adapter *adapter)
3017{
Alexander Duyck153285f2011-08-26 07:43:32 +00003018 u32 max_frame_size = adapter->max_frame_size;
Alexander Duycke1739522009-02-19 20:39:44 -08003019 struct e1000_hw *hw = &adapter->hw;
3020 u16 pf_id = adapter->vfs_allocated_count;
3021
Alexander Duycke1739522009-02-19 20:39:44 -08003022 if (pf_id) {
3023 igb_set_vf_rlpml(adapter, max_frame_size, pf_id);
Alexander Duyck153285f2011-08-26 07:43:32 +00003024 /*
3025 * If we're in VMDQ or SR-IOV mode, then set global RLPML
3026 * to our max jumbo frame size, in case we need to enable
3027 * jumbo frames on one of the rings later.
3028 * This will not pass over-length frames into the default
3029 * queue because it's gated by the VMOLR.RLPML.
3030 */
Alexander Duyck7d5753f2009-10-27 23:47:16 +00003031 max_frame_size = MAX_JUMBO_FRAME_SIZE;
Alexander Duycke1739522009-02-19 20:39:44 -08003032 }
3033
3034 wr32(E1000_RLPML, max_frame_size);
3035}
3036
Williams, Mitch A8151d292010-02-10 01:44:24 +00003037static inline void igb_set_vmolr(struct igb_adapter *adapter,
3038 int vfn, bool aupe)
Alexander Duyck7d5753f2009-10-27 23:47:16 +00003039{
3040 struct e1000_hw *hw = &adapter->hw;
3041 u32 vmolr;
3042
3043 /*
3044 * This register exists only on 82576 and newer so if we are older then
3045 * we should exit and do nothing
3046 */
3047 if (hw->mac.type < e1000_82576)
3048 return;
3049
3050 vmolr = rd32(E1000_VMOLR(vfn));
Williams, Mitch A8151d292010-02-10 01:44:24 +00003051 vmolr |= E1000_VMOLR_STRVLAN; /* Strip vlan tags */
3052 if (aupe)
3053 vmolr |= E1000_VMOLR_AUPE; /* Accept untagged packets */
3054 else
3055 vmolr &= ~(E1000_VMOLR_AUPE); /* Tagged packets ONLY */
Alexander Duyck7d5753f2009-10-27 23:47:16 +00003056
3057 /* clear all bits that might not be set */
3058 vmolr &= ~(E1000_VMOLR_BAM | E1000_VMOLR_RSSE);
3059
Alexander Duycka99955f2009-11-12 18:37:19 +00003060 if (adapter->rss_queues > 1 && vfn == adapter->vfs_allocated_count)
Alexander Duyck7d5753f2009-10-27 23:47:16 +00003061 vmolr |= E1000_VMOLR_RSSE; /* enable RSS */
3062 /*
3063 * for VMDq only allow the VFs and pool 0 to accept broadcast and
3064 * multicast packets
3065 */
3066 if (vfn <= adapter->vfs_allocated_count)
3067 vmolr |= E1000_VMOLR_BAM; /* Accept broadcast */
3068
3069 wr32(E1000_VMOLR(vfn), vmolr);
3070}
3071
Alexander Duycke1739522009-02-19 20:39:44 -08003072/**
Alexander Duyck85b430b2009-10-27 15:50:29 +00003073 * igb_configure_rx_ring - Configure a receive ring after Reset
3074 * @adapter: board private structure
3075 * @ring: receive ring to be configured
3076 *
3077 * Configure the Rx unit of the MAC after a reset.
3078 **/
Alexander Duyckd7ee5b32009-10-27 15:54:23 +00003079void igb_configure_rx_ring(struct igb_adapter *adapter,
3080 struct igb_ring *ring)
Alexander Duyck85b430b2009-10-27 15:50:29 +00003081{
3082 struct e1000_hw *hw = &adapter->hw;
3083 u64 rdba = ring->dma;
3084 int reg_idx = ring->reg_idx;
Alexander Duycka74420e2011-08-26 07:43:27 +00003085 u32 srrctl = 0, rxdctl = 0;
Alexander Duyck85b430b2009-10-27 15:50:29 +00003086
3087 /* disable the queue */
Alexander Duycka74420e2011-08-26 07:43:27 +00003088 wr32(E1000_RXDCTL(reg_idx), 0);
Alexander Duyck85b430b2009-10-27 15:50:29 +00003089
3090 /* Set DMA base address registers */
3091 wr32(E1000_RDBAL(reg_idx),
3092 rdba & 0x00000000ffffffffULL);
3093 wr32(E1000_RDBAH(reg_idx), rdba >> 32);
3094 wr32(E1000_RDLEN(reg_idx),
3095 ring->count * sizeof(union e1000_adv_rx_desc));
3096
3097 /* initialize head and tail */
Alexander Duyckfce99e32009-10-27 15:51:27 +00003098 ring->tail = hw->hw_addr + E1000_RDT(reg_idx);
Alexander Duycka74420e2011-08-26 07:43:27 +00003099 wr32(E1000_RDH(reg_idx), 0);
Alexander Duyckfce99e32009-10-27 15:51:27 +00003100 writel(0, ring->tail);
Alexander Duyck85b430b2009-10-27 15:50:29 +00003101
Alexander Duyck952f72a2009-10-27 15:51:07 +00003102 /* set descriptor configuration */
Alexander Duyck44390ca2011-08-26 07:43:38 +00003103 srrctl = IGB_RX_HDR_LEN << E1000_SRRCTL_BSIZEHDRSIZE_SHIFT;
Alexander Duyckde78d1f2012-09-25 00:31:12 +00003104 srrctl |= IGB_RX_BUFSZ >> E1000_SRRCTL_BSIZEPKT_SHIFT;
Alexander Duyck1a1c2252012-09-25 00:30:52 +00003105 srrctl |= E1000_SRRCTL_DESCTYPE_ADV_ONEBUF;
Alexander Duyck06218a82011-08-26 07:46:55 +00003106 if (hw->mac.type >= e1000_82580)
Nick Nunley757b77e2010-03-26 11:36:47 +00003107 srrctl |= E1000_SRRCTL_TIMESTAMP;
Nick Nunleye6bdb6f2010-02-17 01:03:38 +00003108 /* Only set Drop Enable if we are supporting multiple queues */
3109 if (adapter->vfs_allocated_count || adapter->num_rx_queues > 1)
3110 srrctl |= E1000_SRRCTL_DROP_EN;
Alexander Duyck952f72a2009-10-27 15:51:07 +00003111
3112 wr32(E1000_SRRCTL(reg_idx), srrctl);
3113
Alexander Duyck7d5753f2009-10-27 23:47:16 +00003114 /* set filtering for VMDQ pools */
Williams, Mitch A8151d292010-02-10 01:44:24 +00003115 igb_set_vmolr(adapter, reg_idx & 0x7, true);
Alexander Duyck7d5753f2009-10-27 23:47:16 +00003116
Alexander Duyck85b430b2009-10-27 15:50:29 +00003117 rxdctl |= IGB_RX_PTHRESH;
3118 rxdctl |= IGB_RX_HTHRESH << 8;
3119 rxdctl |= IGB_RX_WTHRESH << 16;
Alexander Duycka74420e2011-08-26 07:43:27 +00003120
3121 /* enable receive descriptor fetching */
3122 rxdctl |= E1000_RXDCTL_QUEUE_ENABLE;
Alexander Duyck85b430b2009-10-27 15:50:29 +00003123 wr32(E1000_RXDCTL(reg_idx), rxdctl);
3124}
3125
3126/**
Auke Kok9d5c8242008-01-24 02:22:38 -08003127 * igb_configure_rx - Configure receive Unit after Reset
3128 * @adapter: board private structure
3129 *
3130 * Configure the Rx unit of the MAC after a reset.
3131 **/
3132static void igb_configure_rx(struct igb_adapter *adapter)
3133{
Hannes Eder91075842009-02-18 19:36:04 -08003134 int i;
Auke Kok9d5c8242008-01-24 02:22:38 -08003135
Alexander Duyck68d480c2009-10-05 06:33:08 +00003136 /* set UTA to appropriate mode */
3137 igb_set_uta(adapter);
3138
Alexander Duyck26ad9172009-10-05 06:32:49 +00003139 /* set the correct pool for the PF default MAC address in entry 0 */
3140 igb_rar_set_qsel(adapter, adapter->hw.mac.addr, 0,
3141 adapter->vfs_allocated_count);
3142
Alexander Duyck06cf2662009-10-27 15:53:25 +00003143 /* Setup the HW Rx Head and Tail Descriptor Pointers and
3144 * the Base and Length of the Rx Descriptor Ring */
3145 for (i = 0; i < adapter->num_rx_queues; i++)
Alexander Duyck3025a442010-02-17 01:02:39 +00003146 igb_configure_rx_ring(adapter, adapter->rx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08003147}
3148
3149/**
3150 * igb_free_tx_resources - Free Tx Resources per Queue
Auke Kok9d5c8242008-01-24 02:22:38 -08003151 * @tx_ring: Tx descriptor ring for a specific queue
3152 *
3153 * Free all transmit software resources
3154 **/
Alexander Duyck68fd9912008-11-20 00:48:10 -08003155void igb_free_tx_resources(struct igb_ring *tx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08003156{
Mitch Williams3b644cf2008-06-27 10:59:48 -07003157 igb_clean_tx_ring(tx_ring);
Auke Kok9d5c8242008-01-24 02:22:38 -08003158
Alexander Duyck06034642011-08-26 07:44:22 +00003159 vfree(tx_ring->tx_buffer_info);
3160 tx_ring->tx_buffer_info = NULL;
Auke Kok9d5c8242008-01-24 02:22:38 -08003161
Alexander Duyck439705e2009-10-27 23:49:20 +00003162 /* if not set, then don't free */
3163 if (!tx_ring->desc)
3164 return;
3165
Alexander Duyck59d71982010-04-27 13:09:25 +00003166 dma_free_coherent(tx_ring->dev, tx_ring->size,
3167 tx_ring->desc, tx_ring->dma);
Auke Kok9d5c8242008-01-24 02:22:38 -08003168
3169 tx_ring->desc = NULL;
3170}
3171
3172/**
3173 * igb_free_all_tx_resources - Free Tx Resources for All Queues
3174 * @adapter: board private structure
3175 *
3176 * Free all transmit software resources
3177 **/
3178static void igb_free_all_tx_resources(struct igb_adapter *adapter)
3179{
3180 int i;
3181
3182 for (i = 0; i < adapter->num_tx_queues; i++)
Alexander Duyck3025a442010-02-17 01:02:39 +00003183 igb_free_tx_resources(adapter->tx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08003184}
3185
Alexander Duyckebe42d12011-08-26 07:45:09 +00003186void igb_unmap_and_free_tx_resource(struct igb_ring *ring,
3187 struct igb_tx_buffer *tx_buffer)
Auke Kok9d5c8242008-01-24 02:22:38 -08003188{
Alexander Duyckebe42d12011-08-26 07:45:09 +00003189 if (tx_buffer->skb) {
3190 dev_kfree_skb_any(tx_buffer->skb);
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00003191 if (dma_unmap_len(tx_buffer, len))
Alexander Duyckebe42d12011-08-26 07:45:09 +00003192 dma_unmap_single(ring->dev,
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00003193 dma_unmap_addr(tx_buffer, dma),
3194 dma_unmap_len(tx_buffer, len),
Alexander Duyckebe42d12011-08-26 07:45:09 +00003195 DMA_TO_DEVICE);
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00003196 } else if (dma_unmap_len(tx_buffer, len)) {
Alexander Duyckebe42d12011-08-26 07:45:09 +00003197 dma_unmap_page(ring->dev,
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00003198 dma_unmap_addr(tx_buffer, dma),
3199 dma_unmap_len(tx_buffer, len),
Alexander Duyckebe42d12011-08-26 07:45:09 +00003200 DMA_TO_DEVICE);
Alexander Duyck6366ad32009-12-02 16:47:18 +00003201 }
Alexander Duyckebe42d12011-08-26 07:45:09 +00003202 tx_buffer->next_to_watch = NULL;
3203 tx_buffer->skb = NULL;
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00003204 dma_unmap_len_set(tx_buffer, len, 0);
Alexander Duyckebe42d12011-08-26 07:45:09 +00003205 /* buffer_info must be completely set up in the transmit path */
Auke Kok9d5c8242008-01-24 02:22:38 -08003206}
3207
3208/**
3209 * igb_clean_tx_ring - Free Tx Buffers
Auke Kok9d5c8242008-01-24 02:22:38 -08003210 * @tx_ring: ring to be cleaned
3211 **/
Mitch Williams3b644cf2008-06-27 10:59:48 -07003212static void igb_clean_tx_ring(struct igb_ring *tx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08003213{
Alexander Duyck06034642011-08-26 07:44:22 +00003214 struct igb_tx_buffer *buffer_info;
Auke Kok9d5c8242008-01-24 02:22:38 -08003215 unsigned long size;
Alexander Duyck6ad4edf2011-08-26 07:45:26 +00003216 u16 i;
Auke Kok9d5c8242008-01-24 02:22:38 -08003217
Alexander Duyck06034642011-08-26 07:44:22 +00003218 if (!tx_ring->tx_buffer_info)
Auke Kok9d5c8242008-01-24 02:22:38 -08003219 return;
3220 /* Free all the Tx ring sk_buffs */
3221
3222 for (i = 0; i < tx_ring->count; i++) {
Alexander Duyck06034642011-08-26 07:44:22 +00003223 buffer_info = &tx_ring->tx_buffer_info[i];
Alexander Duyck80785292009-10-27 15:51:47 +00003224 igb_unmap_and_free_tx_resource(tx_ring, buffer_info);
Auke Kok9d5c8242008-01-24 02:22:38 -08003225 }
3226
John Fastabenddad8a3b2012-04-23 12:22:39 +00003227 netdev_tx_reset_queue(txring_txq(tx_ring));
3228
Alexander Duyck06034642011-08-26 07:44:22 +00003229 size = sizeof(struct igb_tx_buffer) * tx_ring->count;
3230 memset(tx_ring->tx_buffer_info, 0, size);
Auke Kok9d5c8242008-01-24 02:22:38 -08003231
3232 /* Zero out the descriptor ring */
Auke Kok9d5c8242008-01-24 02:22:38 -08003233 memset(tx_ring->desc, 0, tx_ring->size);
3234
3235 tx_ring->next_to_use = 0;
3236 tx_ring->next_to_clean = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08003237}
3238
3239/**
3240 * igb_clean_all_tx_rings - Free Tx Buffers for all queues
3241 * @adapter: board private structure
3242 **/
3243static void igb_clean_all_tx_rings(struct igb_adapter *adapter)
3244{
3245 int i;
3246
3247 for (i = 0; i < adapter->num_tx_queues; i++)
Alexander Duyck3025a442010-02-17 01:02:39 +00003248 igb_clean_tx_ring(adapter->tx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08003249}
3250
3251/**
3252 * igb_free_rx_resources - Free Rx Resources
Auke Kok9d5c8242008-01-24 02:22:38 -08003253 * @rx_ring: ring to clean the resources from
3254 *
3255 * Free all receive software resources
3256 **/
Alexander Duyck68fd9912008-11-20 00:48:10 -08003257void igb_free_rx_resources(struct igb_ring *rx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08003258{
Mitch Williams3b644cf2008-06-27 10:59:48 -07003259 igb_clean_rx_ring(rx_ring);
Auke Kok9d5c8242008-01-24 02:22:38 -08003260
Alexander Duyck06034642011-08-26 07:44:22 +00003261 vfree(rx_ring->rx_buffer_info);
3262 rx_ring->rx_buffer_info = NULL;
Auke Kok9d5c8242008-01-24 02:22:38 -08003263
Alexander Duyck439705e2009-10-27 23:49:20 +00003264 /* if not set, then don't free */
3265 if (!rx_ring->desc)
3266 return;
3267
Alexander Duyck59d71982010-04-27 13:09:25 +00003268 dma_free_coherent(rx_ring->dev, rx_ring->size,
3269 rx_ring->desc, rx_ring->dma);
Auke Kok9d5c8242008-01-24 02:22:38 -08003270
3271 rx_ring->desc = NULL;
3272}
3273
3274/**
3275 * igb_free_all_rx_resources - Free Rx Resources for All Queues
3276 * @adapter: board private structure
3277 *
3278 * Free all receive software resources
3279 **/
3280static void igb_free_all_rx_resources(struct igb_adapter *adapter)
3281{
3282 int i;
3283
3284 for (i = 0; i < adapter->num_rx_queues; i++)
Alexander Duyck3025a442010-02-17 01:02:39 +00003285 igb_free_rx_resources(adapter->rx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08003286}
3287
3288/**
3289 * igb_clean_rx_ring - Free Rx Buffers per Queue
Auke Kok9d5c8242008-01-24 02:22:38 -08003290 * @rx_ring: ring to free buffers from
3291 **/
Mitch Williams3b644cf2008-06-27 10:59:48 -07003292static void igb_clean_rx_ring(struct igb_ring *rx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08003293{
Auke Kok9d5c8242008-01-24 02:22:38 -08003294 unsigned long size;
Alexander Duyckc023cd82011-08-26 07:43:43 +00003295 u16 i;
Auke Kok9d5c8242008-01-24 02:22:38 -08003296
Alexander Duyck1a1c2252012-09-25 00:30:52 +00003297 if (rx_ring->skb)
3298 dev_kfree_skb(rx_ring->skb);
3299 rx_ring->skb = NULL;
3300
Alexander Duyck06034642011-08-26 07:44:22 +00003301 if (!rx_ring->rx_buffer_info)
Auke Kok9d5c8242008-01-24 02:22:38 -08003302 return;
Alexander Duyck439705e2009-10-27 23:49:20 +00003303
Auke Kok9d5c8242008-01-24 02:22:38 -08003304 /* Free all the Rx ring sk_buffs */
3305 for (i = 0; i < rx_ring->count; i++) {
Alexander Duyck06034642011-08-26 07:44:22 +00003306 struct igb_rx_buffer *buffer_info = &rx_ring->rx_buffer_info[i];
Auke Kok9d5c8242008-01-24 02:22:38 -08003307
Alexander Duyckcbc8e552012-09-25 00:31:02 +00003308 if (!buffer_info->page)
3309 continue;
3310
3311 dma_unmap_page(rx_ring->dev,
3312 buffer_info->dma,
3313 PAGE_SIZE,
3314 DMA_FROM_DEVICE);
3315 __free_page(buffer_info->page);
3316
Alexander Duyck1a1c2252012-09-25 00:30:52 +00003317 buffer_info->page = NULL;
Auke Kok9d5c8242008-01-24 02:22:38 -08003318 }
3319
Alexander Duyck06034642011-08-26 07:44:22 +00003320 size = sizeof(struct igb_rx_buffer) * rx_ring->count;
3321 memset(rx_ring->rx_buffer_info, 0, size);
Auke Kok9d5c8242008-01-24 02:22:38 -08003322
3323 /* Zero out the descriptor ring */
3324 memset(rx_ring->desc, 0, rx_ring->size);
3325
Alexander Duyckcbc8e552012-09-25 00:31:02 +00003326 rx_ring->next_to_alloc = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08003327 rx_ring->next_to_clean = 0;
3328 rx_ring->next_to_use = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08003329}
3330
3331/**
3332 * igb_clean_all_rx_rings - Free Rx Buffers for all queues
3333 * @adapter: board private structure
3334 **/
3335static void igb_clean_all_rx_rings(struct igb_adapter *adapter)
3336{
3337 int i;
3338
3339 for (i = 0; i < adapter->num_rx_queues; i++)
Alexander Duyck3025a442010-02-17 01:02:39 +00003340 igb_clean_rx_ring(adapter->rx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08003341}
3342
3343/**
3344 * igb_set_mac - Change the Ethernet Address of the NIC
3345 * @netdev: network interface device structure
3346 * @p: pointer to an address structure
3347 *
3348 * Returns 0 on success, negative on failure
3349 **/
3350static int igb_set_mac(struct net_device *netdev, void *p)
3351{
3352 struct igb_adapter *adapter = netdev_priv(netdev);
Alexander Duyck28b07592009-02-06 23:20:31 +00003353 struct e1000_hw *hw = &adapter->hw;
Auke Kok9d5c8242008-01-24 02:22:38 -08003354 struct sockaddr *addr = p;
3355
3356 if (!is_valid_ether_addr(addr->sa_data))
3357 return -EADDRNOTAVAIL;
3358
3359 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
Alexander Duyck28b07592009-02-06 23:20:31 +00003360 memcpy(hw->mac.addr, addr->sa_data, netdev->addr_len);
Auke Kok9d5c8242008-01-24 02:22:38 -08003361
Alexander Duyck26ad9172009-10-05 06:32:49 +00003362 /* set the correct pool for the new PF MAC address in entry 0 */
3363 igb_rar_set_qsel(adapter, hw->mac.addr, 0,
3364 adapter->vfs_allocated_count);
Alexander Duycke1739522009-02-19 20:39:44 -08003365
Auke Kok9d5c8242008-01-24 02:22:38 -08003366 return 0;
3367}
3368
3369/**
Alexander Duyck68d480c2009-10-05 06:33:08 +00003370 * igb_write_mc_addr_list - write multicast addresses to MTA
3371 * @netdev: network interface device structure
3372 *
3373 * Writes multicast address list to the MTA hash table.
3374 * Returns: -ENOMEM on failure
3375 * 0 on no addresses written
3376 * X on writing X addresses to MTA
3377 **/
3378static int igb_write_mc_addr_list(struct net_device *netdev)
3379{
3380 struct igb_adapter *adapter = netdev_priv(netdev);
3381 struct e1000_hw *hw = &adapter->hw;
Jiri Pirko22bedad32010-04-01 21:22:57 +00003382 struct netdev_hw_addr *ha;
Alexander Duyck68d480c2009-10-05 06:33:08 +00003383 u8 *mta_list;
Alexander Duyck68d480c2009-10-05 06:33:08 +00003384 int i;
3385
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00003386 if (netdev_mc_empty(netdev)) {
Alexander Duyck68d480c2009-10-05 06:33:08 +00003387 /* nothing to program, so clear mc list */
3388 igb_update_mc_addr_list(hw, NULL, 0);
3389 igb_restore_vf_multicasts(adapter);
3390 return 0;
3391 }
3392
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00003393 mta_list = kzalloc(netdev_mc_count(netdev) * 6, GFP_ATOMIC);
Alexander Duyck68d480c2009-10-05 06:33:08 +00003394 if (!mta_list)
3395 return -ENOMEM;
3396
Alexander Duyck68d480c2009-10-05 06:33:08 +00003397 /* The shared function expects a packed array of only addresses. */
Jiri Pirko48e2f182010-02-22 09:22:26 +00003398 i = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +00003399 netdev_for_each_mc_addr(ha, netdev)
3400 memcpy(mta_list + (i++ * ETH_ALEN), ha->addr, ETH_ALEN);
Alexander Duyck68d480c2009-10-05 06:33:08 +00003401
Alexander Duyck68d480c2009-10-05 06:33:08 +00003402 igb_update_mc_addr_list(hw, mta_list, i);
3403 kfree(mta_list);
3404
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00003405 return netdev_mc_count(netdev);
Alexander Duyck68d480c2009-10-05 06:33:08 +00003406}
3407
3408/**
3409 * igb_write_uc_addr_list - write unicast addresses to RAR table
3410 * @netdev: network interface device structure
3411 *
3412 * Writes unicast address list to the RAR table.
3413 * Returns: -ENOMEM on failure/insufficient address space
3414 * 0 on no addresses written
3415 * X on writing X addresses to the RAR table
3416 **/
3417static int igb_write_uc_addr_list(struct net_device *netdev)
3418{
3419 struct igb_adapter *adapter = netdev_priv(netdev);
3420 struct e1000_hw *hw = &adapter->hw;
3421 unsigned int vfn = adapter->vfs_allocated_count;
3422 unsigned int rar_entries = hw->mac.rar_entry_count - (vfn + 1);
3423 int count = 0;
3424
3425 /* return ENOMEM indicating insufficient memory for addresses */
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08003426 if (netdev_uc_count(netdev) > rar_entries)
Alexander Duyck68d480c2009-10-05 06:33:08 +00003427 return -ENOMEM;
3428
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08003429 if (!netdev_uc_empty(netdev) && rar_entries) {
Alexander Duyck68d480c2009-10-05 06:33:08 +00003430 struct netdev_hw_addr *ha;
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08003431
3432 netdev_for_each_uc_addr(ha, netdev) {
Alexander Duyck68d480c2009-10-05 06:33:08 +00003433 if (!rar_entries)
3434 break;
3435 igb_rar_set_qsel(adapter, ha->addr,
3436 rar_entries--,
3437 vfn);
3438 count++;
3439 }
3440 }
3441 /* write the addresses in reverse order to avoid write combining */
3442 for (; rar_entries > 0 ; rar_entries--) {
3443 wr32(E1000_RAH(rar_entries), 0);
3444 wr32(E1000_RAL(rar_entries), 0);
3445 }
3446 wrfl();
3447
3448 return count;
3449}
3450
3451/**
Alexander Duyckff41f8d2009-09-03 14:48:56 +00003452 * igb_set_rx_mode - Secondary Unicast, Multicast and Promiscuous mode set
Auke Kok9d5c8242008-01-24 02:22:38 -08003453 * @netdev: network interface device structure
3454 *
Alexander Duyckff41f8d2009-09-03 14:48:56 +00003455 * The set_rx_mode entry point is called whenever the unicast or multicast
3456 * address lists or the network interface flags are updated. This routine is
3457 * responsible for configuring the hardware for proper unicast, multicast,
Auke Kok9d5c8242008-01-24 02:22:38 -08003458 * promiscuous mode, and all-multi behavior.
3459 **/
Alexander Duyckff41f8d2009-09-03 14:48:56 +00003460static void igb_set_rx_mode(struct net_device *netdev)
Auke Kok9d5c8242008-01-24 02:22:38 -08003461{
3462 struct igb_adapter *adapter = netdev_priv(netdev);
3463 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck68d480c2009-10-05 06:33:08 +00003464 unsigned int vfn = adapter->vfs_allocated_count;
3465 u32 rctl, vmolr = 0;
3466 int count;
Auke Kok9d5c8242008-01-24 02:22:38 -08003467
3468 /* Check for Promiscuous and All Multicast modes */
Auke Kok9d5c8242008-01-24 02:22:38 -08003469 rctl = rd32(E1000_RCTL);
3470
Alexander Duyck68d480c2009-10-05 06:33:08 +00003471 /* clear the effected bits */
3472 rctl &= ~(E1000_RCTL_UPE | E1000_RCTL_MPE | E1000_RCTL_VFE);
3473
Patrick McHardy746b9f02008-07-16 20:15:45 -07003474 if (netdev->flags & IFF_PROMISC) {
Auke Kok9d5c8242008-01-24 02:22:38 -08003475 rctl |= (E1000_RCTL_UPE | E1000_RCTL_MPE);
Alexander Duyck68d480c2009-10-05 06:33:08 +00003476 vmolr |= (E1000_VMOLR_ROPE | E1000_VMOLR_MPME);
Patrick McHardy746b9f02008-07-16 20:15:45 -07003477 } else {
Alexander Duyck68d480c2009-10-05 06:33:08 +00003478 if (netdev->flags & IFF_ALLMULTI) {
Patrick McHardy746b9f02008-07-16 20:15:45 -07003479 rctl |= E1000_RCTL_MPE;
Alexander Duyck68d480c2009-10-05 06:33:08 +00003480 vmolr |= E1000_VMOLR_MPME;
3481 } else {
3482 /*
3483 * Write addresses to the MTA, if the attempt fails
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003484 * then we should just turn on promiscuous mode so
Alexander Duyck68d480c2009-10-05 06:33:08 +00003485 * that we can at least receive multicast traffic
3486 */
3487 count = igb_write_mc_addr_list(netdev);
3488 if (count < 0) {
3489 rctl |= E1000_RCTL_MPE;
3490 vmolr |= E1000_VMOLR_MPME;
3491 } else if (count) {
3492 vmolr |= E1000_VMOLR_ROMPE;
3493 }
3494 }
3495 /*
3496 * Write addresses to available RAR registers, if there is not
3497 * sufficient space to store all the addresses then enable
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003498 * unicast promiscuous mode
Alexander Duyck68d480c2009-10-05 06:33:08 +00003499 */
3500 count = igb_write_uc_addr_list(netdev);
3501 if (count < 0) {
Alexander Duyckff41f8d2009-09-03 14:48:56 +00003502 rctl |= E1000_RCTL_UPE;
Alexander Duyck68d480c2009-10-05 06:33:08 +00003503 vmolr |= E1000_VMOLR_ROPE;
3504 }
Patrick McHardy78ed11a2008-07-16 20:16:14 -07003505 rctl |= E1000_RCTL_VFE;
Patrick McHardy746b9f02008-07-16 20:15:45 -07003506 }
Auke Kok9d5c8242008-01-24 02:22:38 -08003507 wr32(E1000_RCTL, rctl);
3508
Alexander Duyck68d480c2009-10-05 06:33:08 +00003509 /*
3510 * In order to support SR-IOV and eventually VMDq it is necessary to set
3511 * the VMOLR to enable the appropriate modes. Without this workaround
3512 * we will have issues with VLAN tag stripping not being done for frames
3513 * that are only arriving because we are the default pool
3514 */
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00003515 if ((hw->mac.type < e1000_82576) || (hw->mac.type > e1000_i350))
Alexander Duyck28fc06f2009-07-23 18:08:54 +00003516 return;
Alexander Duyck28fc06f2009-07-23 18:08:54 +00003517
Alexander Duyck68d480c2009-10-05 06:33:08 +00003518 vmolr |= rd32(E1000_VMOLR(vfn)) &
3519 ~(E1000_VMOLR_ROPE | E1000_VMOLR_MPME | E1000_VMOLR_ROMPE);
3520 wr32(E1000_VMOLR(vfn), vmolr);
Alexander Duyck28fc06f2009-07-23 18:08:54 +00003521 igb_restore_vf_multicasts(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08003522}
3523
Greg Rose13800462010-11-06 02:08:26 +00003524static void igb_check_wvbr(struct igb_adapter *adapter)
3525{
3526 struct e1000_hw *hw = &adapter->hw;
3527 u32 wvbr = 0;
3528
3529 switch (hw->mac.type) {
3530 case e1000_82576:
3531 case e1000_i350:
3532 if (!(wvbr = rd32(E1000_WVBR)))
3533 return;
3534 break;
3535 default:
3536 break;
3537 }
3538
3539 adapter->wvbr |= wvbr;
3540}
3541
3542#define IGB_STAGGERED_QUEUE_OFFSET 8
3543
3544static void igb_spoof_check(struct igb_adapter *adapter)
3545{
3546 int j;
3547
3548 if (!adapter->wvbr)
3549 return;
3550
3551 for(j = 0; j < adapter->vfs_allocated_count; j++) {
3552 if (adapter->wvbr & (1 << j) ||
3553 adapter->wvbr & (1 << (j + IGB_STAGGERED_QUEUE_OFFSET))) {
3554 dev_warn(&adapter->pdev->dev,
3555 "Spoof event(s) detected on VF %d\n", j);
3556 adapter->wvbr &=
3557 ~((1 << j) |
3558 (1 << (j + IGB_STAGGERED_QUEUE_OFFSET)));
3559 }
3560 }
3561}
3562
Auke Kok9d5c8242008-01-24 02:22:38 -08003563/* Need to wait a few seconds after link up to get diagnostic information from
3564 * the phy */
3565static void igb_update_phy_info(unsigned long data)
3566{
3567 struct igb_adapter *adapter = (struct igb_adapter *) data;
Alexander Duyckf5f4cf02008-11-21 21:30:24 -08003568 igb_get_phy_info(&adapter->hw);
Auke Kok9d5c8242008-01-24 02:22:38 -08003569}
3570
3571/**
Alexander Duyck4d6b7252009-02-06 23:16:24 +00003572 * igb_has_link - check shared code for link and determine up/down
3573 * @adapter: pointer to driver private info
3574 **/
Nick Nunley31455352010-02-17 01:01:21 +00003575bool igb_has_link(struct igb_adapter *adapter)
Alexander Duyck4d6b7252009-02-06 23:16:24 +00003576{
3577 struct e1000_hw *hw = &adapter->hw;
3578 bool link_active = false;
3579 s32 ret_val = 0;
3580
3581 /* get_link_status is set on LSC (link status) interrupt or
3582 * rx sequence error interrupt. get_link_status will stay
3583 * false until the e1000_check_for_link establishes link
3584 * for copper adapters ONLY
3585 */
3586 switch (hw->phy.media_type) {
3587 case e1000_media_type_copper:
3588 if (hw->mac.get_link_status) {
3589 ret_val = hw->mac.ops.check_for_link(hw);
3590 link_active = !hw->mac.get_link_status;
3591 } else {
3592 link_active = true;
3593 }
3594 break;
Alexander Duyck4d6b7252009-02-06 23:16:24 +00003595 case e1000_media_type_internal_serdes:
3596 ret_val = hw->mac.ops.check_for_link(hw);
3597 link_active = hw->mac.serdes_has_link;
3598 break;
3599 default:
3600 case e1000_media_type_unknown:
3601 break;
3602 }
3603
3604 return link_active;
3605}
3606
Stefan Assmann563988d2011-04-05 04:27:15 +00003607static bool igb_thermal_sensor_event(struct e1000_hw *hw, u32 event)
3608{
3609 bool ret = false;
3610 u32 ctrl_ext, thstat;
3611
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00003612 /* check for thermal sensor event on i350 copper only */
Stefan Assmann563988d2011-04-05 04:27:15 +00003613 if (hw->mac.type == e1000_i350) {
3614 thstat = rd32(E1000_THSTAT);
3615 ctrl_ext = rd32(E1000_CTRL_EXT);
3616
3617 if ((hw->phy.media_type == e1000_media_type_copper) &&
3618 !(ctrl_ext & E1000_CTRL_EXT_LINK_MODE_SGMII)) {
3619 ret = !!(thstat & event);
3620 }
3621 }
3622
3623 return ret;
3624}
3625
Alexander Duyck4d6b7252009-02-06 23:16:24 +00003626/**
Auke Kok9d5c8242008-01-24 02:22:38 -08003627 * igb_watchdog - Timer Call-back
3628 * @data: pointer to adapter cast into an unsigned long
3629 **/
3630static void igb_watchdog(unsigned long data)
3631{
3632 struct igb_adapter *adapter = (struct igb_adapter *)data;
3633 /* Do the rest outside of interrupt context */
3634 schedule_work(&adapter->watchdog_task);
3635}
3636
3637static void igb_watchdog_task(struct work_struct *work)
3638{
3639 struct igb_adapter *adapter = container_of(work,
Alexander Duyck559e9c42009-10-27 23:52:50 +00003640 struct igb_adapter,
3641 watchdog_task);
Auke Kok9d5c8242008-01-24 02:22:38 -08003642 struct e1000_hw *hw = &adapter->hw;
Auke Kok9d5c8242008-01-24 02:22:38 -08003643 struct net_device *netdev = adapter->netdev;
Stefan Assmann563988d2011-04-05 04:27:15 +00003644 u32 link;
Alexander Duyck7a6ea552008-08-26 04:25:03 -07003645 int i;
Auke Kok9d5c8242008-01-24 02:22:38 -08003646
Alexander Duyck4d6b7252009-02-06 23:16:24 +00003647 link = igb_has_link(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08003648 if (link) {
Yan, Zheng749ab2c2012-01-04 20:23:37 +00003649 /* Cancel scheduled suspend requests. */
3650 pm_runtime_resume(netdev->dev.parent);
3651
Auke Kok9d5c8242008-01-24 02:22:38 -08003652 if (!netif_carrier_ok(netdev)) {
3653 u32 ctrl;
Alexander Duyck330a6d62009-10-27 23:51:35 +00003654 hw->mac.ops.get_speed_and_duplex(hw,
3655 &adapter->link_speed,
3656 &adapter->link_duplex);
Auke Kok9d5c8242008-01-24 02:22:38 -08003657
3658 ctrl = rd32(E1000_CTRL);
Alexander Duyck527d47c2008-11-27 00:21:39 -08003659 /* Links status message must follow this format */
Jeff Kirsher876d2d62011-10-21 20:01:34 +00003660 printk(KERN_INFO "igb: %s NIC Link is Up %d Mbps %s "
3661 "Duplex, Flow Control: %s\n",
Alexander Duyck559e9c42009-10-27 23:52:50 +00003662 netdev->name,
3663 adapter->link_speed,
3664 adapter->link_duplex == FULL_DUPLEX ?
Jeff Kirsher876d2d62011-10-21 20:01:34 +00003665 "Full" : "Half",
3666 (ctrl & E1000_CTRL_TFCE) &&
3667 (ctrl & E1000_CTRL_RFCE) ? "RX/TX" :
3668 (ctrl & E1000_CTRL_RFCE) ? "RX" :
3669 (ctrl & E1000_CTRL_TFCE) ? "TX" : "None");
Auke Kok9d5c8242008-01-24 02:22:38 -08003670
Stefan Assmann563988d2011-04-05 04:27:15 +00003671 /* check for thermal sensor event */
Jeff Kirsher876d2d62011-10-21 20:01:34 +00003672 if (igb_thermal_sensor_event(hw,
3673 E1000_THSTAT_LINK_THROTTLE)) {
3674 netdev_info(netdev, "The network adapter link "
3675 "speed was downshifted because it "
3676 "overheated\n");
Carolyn Wyborny7ef5ed12011-03-12 08:59:47 +00003677 }
Stefan Assmann563988d2011-04-05 04:27:15 +00003678
Emil Tantilovd07f3e32010-03-23 18:34:57 +00003679 /* adjust timeout factor according to speed/duplex */
Auke Kok9d5c8242008-01-24 02:22:38 -08003680 adapter->tx_timeout_factor = 1;
3681 switch (adapter->link_speed) {
3682 case SPEED_10:
Auke Kok9d5c8242008-01-24 02:22:38 -08003683 adapter->tx_timeout_factor = 14;
3684 break;
3685 case SPEED_100:
Auke Kok9d5c8242008-01-24 02:22:38 -08003686 /* maybe add some timeout factor ? */
3687 break;
3688 }
3689
3690 netif_carrier_on(netdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08003691
Alexander Duyck4ae196d2009-02-19 20:40:07 -08003692 igb_ping_all_vfs(adapter);
Lior Levy17dc5662011-02-08 02:28:46 +00003693 igb_check_vf_rate_limit(adapter);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08003694
Alexander Duyck4b1a9872009-02-06 23:19:50 +00003695 /* link state has changed, schedule phy info update */
Auke Kok9d5c8242008-01-24 02:22:38 -08003696 if (!test_bit(__IGB_DOWN, &adapter->state))
3697 mod_timer(&adapter->phy_info_timer,
3698 round_jiffies(jiffies + 2 * HZ));
3699 }
3700 } else {
3701 if (netif_carrier_ok(netdev)) {
3702 adapter->link_speed = 0;
3703 adapter->link_duplex = 0;
Stefan Assmann563988d2011-04-05 04:27:15 +00003704
3705 /* check for thermal sensor event */
Jeff Kirsher876d2d62011-10-21 20:01:34 +00003706 if (igb_thermal_sensor_event(hw,
3707 E1000_THSTAT_PWR_DOWN)) {
3708 netdev_err(netdev, "The network adapter was "
3709 "stopped because it overheated\n");
Carolyn Wyborny7ef5ed12011-03-12 08:59:47 +00003710 }
Stefan Assmann563988d2011-04-05 04:27:15 +00003711
Alexander Duyck527d47c2008-11-27 00:21:39 -08003712 /* Links status message must follow this format */
3713 printk(KERN_INFO "igb: %s NIC Link is Down\n",
3714 netdev->name);
Auke Kok9d5c8242008-01-24 02:22:38 -08003715 netif_carrier_off(netdev);
Alexander Duyck4b1a9872009-02-06 23:19:50 +00003716
Alexander Duyck4ae196d2009-02-19 20:40:07 -08003717 igb_ping_all_vfs(adapter);
3718
Alexander Duyck4b1a9872009-02-06 23:19:50 +00003719 /* link state has changed, schedule phy info update */
Auke Kok9d5c8242008-01-24 02:22:38 -08003720 if (!test_bit(__IGB_DOWN, &adapter->state))
3721 mod_timer(&adapter->phy_info_timer,
3722 round_jiffies(jiffies + 2 * HZ));
Yan, Zheng749ab2c2012-01-04 20:23:37 +00003723
3724 pm_schedule_suspend(netdev->dev.parent,
3725 MSEC_PER_SEC * 5);
Auke Kok9d5c8242008-01-24 02:22:38 -08003726 }
3727 }
3728
Eric Dumazet12dcd862010-10-15 17:27:10 +00003729 spin_lock(&adapter->stats64_lock);
3730 igb_update_stats(adapter, &adapter->stats64);
3731 spin_unlock(&adapter->stats64_lock);
Auke Kok9d5c8242008-01-24 02:22:38 -08003732
Alexander Duyckdbabb062009-11-12 18:38:16 +00003733 for (i = 0; i < adapter->num_tx_queues; i++) {
Alexander Duyck3025a442010-02-17 01:02:39 +00003734 struct igb_ring *tx_ring = adapter->tx_ring[i];
Alexander Duyckdbabb062009-11-12 18:38:16 +00003735 if (!netif_carrier_ok(netdev)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08003736 /* We've lost link, so the controller stops DMA,
3737 * but we've got queued Tx work that's never going
3738 * to get done, so reset controller to flush Tx.
3739 * (Do the reset outside of interrupt context). */
Alexander Duyckdbabb062009-11-12 18:38:16 +00003740 if (igb_desc_unused(tx_ring) + 1 < tx_ring->count) {
3741 adapter->tx_timeout_count++;
3742 schedule_work(&adapter->reset_task);
3743 /* return immediately since reset is imminent */
3744 return;
3745 }
Auke Kok9d5c8242008-01-24 02:22:38 -08003746 }
Auke Kok9d5c8242008-01-24 02:22:38 -08003747
Alexander Duyckdbabb062009-11-12 18:38:16 +00003748 /* Force detection of hung controller every watchdog period */
Alexander Duyck6d095fa2011-08-26 07:46:19 +00003749 set_bit(IGB_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags);
Alexander Duyckdbabb062009-11-12 18:38:16 +00003750 }
Alexander Duyckf7ba2052009-10-27 23:48:51 +00003751
Auke Kok9d5c8242008-01-24 02:22:38 -08003752 /* Cause software interrupt to ensure rx ring is cleaned */
Alexander Duyck7a6ea552008-08-26 04:25:03 -07003753 if (adapter->msix_entries) {
Alexander Duyck047e0032009-10-27 15:49:27 +00003754 u32 eics = 0;
Alexander Duyck0d1ae7f2011-08-26 07:46:34 +00003755 for (i = 0; i < adapter->num_q_vectors; i++)
3756 eics |= adapter->q_vector[i]->eims_value;
Alexander Duyck7a6ea552008-08-26 04:25:03 -07003757 wr32(E1000_EICS, eics);
3758 } else {
3759 wr32(E1000_ICS, E1000_ICS_RXDMT0);
3760 }
Auke Kok9d5c8242008-01-24 02:22:38 -08003761
Greg Rose13800462010-11-06 02:08:26 +00003762 igb_spoof_check(adapter);
3763
Auke Kok9d5c8242008-01-24 02:22:38 -08003764 /* Reset the timer */
3765 if (!test_bit(__IGB_DOWN, &adapter->state))
3766 mod_timer(&adapter->watchdog_timer,
3767 round_jiffies(jiffies + 2 * HZ));
3768}
3769
3770enum latency_range {
3771 lowest_latency = 0,
3772 low_latency = 1,
3773 bulk_latency = 2,
3774 latency_invalid = 255
3775};
3776
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003777/**
3778 * igb_update_ring_itr - update the dynamic ITR value based on packet size
3779 *
3780 * Stores a new ITR value based on strictly on packet size. This
3781 * algorithm is less sophisticated than that used in igb_update_itr,
3782 * due to the difficulty of synchronizing statistics across multiple
Stefan Weileef35c22010-08-06 21:11:15 +02003783 * receive rings. The divisors and thresholds used by this function
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003784 * were determined based on theoretical maximum wire speed and testing
3785 * data, in order to minimize response time while increasing bulk
3786 * throughput.
3787 * This functionality is controlled by the InterruptThrottleRate module
3788 * parameter (see igb_param.c)
3789 * NOTE: This function is called only when operating in a multiqueue
3790 * receive environment.
Alexander Duyck047e0032009-10-27 15:49:27 +00003791 * @q_vector: pointer to q_vector
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003792 **/
Alexander Duyck047e0032009-10-27 15:49:27 +00003793static void igb_update_ring_itr(struct igb_q_vector *q_vector)
Auke Kok9d5c8242008-01-24 02:22:38 -08003794{
Alexander Duyck047e0032009-10-27 15:49:27 +00003795 int new_val = q_vector->itr_val;
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003796 int avg_wire_size = 0;
Alexander Duyck047e0032009-10-27 15:49:27 +00003797 struct igb_adapter *adapter = q_vector->adapter;
Eric Dumazet12dcd862010-10-15 17:27:10 +00003798 unsigned int packets;
Auke Kok9d5c8242008-01-24 02:22:38 -08003799
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003800 /* For non-gigabit speeds, just fix the interrupt rate at 4000
3801 * ints/sec - ITR timer value of 120 ticks.
3802 */
3803 if (adapter->link_speed != SPEED_1000) {
Alexander Duyck0ba82992011-08-26 07:45:47 +00003804 new_val = IGB_4K_ITR;
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003805 goto set_itr_val;
3806 }
Alexander Duyck047e0032009-10-27 15:49:27 +00003807
Alexander Duyck0ba82992011-08-26 07:45:47 +00003808 packets = q_vector->rx.total_packets;
3809 if (packets)
3810 avg_wire_size = q_vector->rx.total_bytes / packets;
Eric Dumazet12dcd862010-10-15 17:27:10 +00003811
Alexander Duyck0ba82992011-08-26 07:45:47 +00003812 packets = q_vector->tx.total_packets;
3813 if (packets)
3814 avg_wire_size = max_t(u32, avg_wire_size,
3815 q_vector->tx.total_bytes / packets);
Alexander Duyck047e0032009-10-27 15:49:27 +00003816
3817 /* if avg_wire_size isn't set no work was done */
3818 if (!avg_wire_size)
3819 goto clear_counts;
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003820
3821 /* Add 24 bytes to size to account for CRC, preamble, and gap */
3822 avg_wire_size += 24;
3823
3824 /* Don't starve jumbo frames */
3825 avg_wire_size = min(avg_wire_size, 3000);
3826
3827 /* Give a little boost to mid-size frames */
3828 if ((avg_wire_size > 300) && (avg_wire_size < 1200))
3829 new_val = avg_wire_size / 3;
3830 else
3831 new_val = avg_wire_size / 2;
3832
Alexander Duyck0ba82992011-08-26 07:45:47 +00003833 /* conservative mode (itr 3) eliminates the lowest_latency setting */
3834 if (new_val < IGB_20K_ITR &&
3835 ((q_vector->rx.ring && adapter->rx_itr_setting == 3) ||
3836 (!q_vector->rx.ring && adapter->tx_itr_setting == 3)))
3837 new_val = IGB_20K_ITR;
Nick Nunleyabe1c362010-02-17 01:03:19 +00003838
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003839set_itr_val:
Alexander Duyck047e0032009-10-27 15:49:27 +00003840 if (new_val != q_vector->itr_val) {
3841 q_vector->itr_val = new_val;
3842 q_vector->set_itr = 1;
Auke Kok9d5c8242008-01-24 02:22:38 -08003843 }
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003844clear_counts:
Alexander Duyck0ba82992011-08-26 07:45:47 +00003845 q_vector->rx.total_bytes = 0;
3846 q_vector->rx.total_packets = 0;
3847 q_vector->tx.total_bytes = 0;
3848 q_vector->tx.total_packets = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08003849}
3850
3851/**
3852 * igb_update_itr - update the dynamic ITR value based on statistics
3853 * Stores a new ITR value based on packets and byte
3854 * counts during the last interrupt. The advantage of per interrupt
3855 * computation is faster updates and more accurate ITR for the current
3856 * traffic pattern. Constants in this function were computed
3857 * based on theoretical maximum wire speed and thresholds were set based
3858 * on testing data as well as attempting to minimize response time
3859 * while increasing bulk throughput.
3860 * this functionality is controlled by the InterruptThrottleRate module
3861 * parameter (see igb_param.c)
3862 * NOTE: These calculations are only valid when operating in a single-
3863 * queue environment.
Alexander Duyck0ba82992011-08-26 07:45:47 +00003864 * @q_vector: pointer to q_vector
3865 * @ring_container: ring info to update the itr for
Auke Kok9d5c8242008-01-24 02:22:38 -08003866 **/
Alexander Duyck0ba82992011-08-26 07:45:47 +00003867static void igb_update_itr(struct igb_q_vector *q_vector,
3868 struct igb_ring_container *ring_container)
Auke Kok9d5c8242008-01-24 02:22:38 -08003869{
Alexander Duyck0ba82992011-08-26 07:45:47 +00003870 unsigned int packets = ring_container->total_packets;
3871 unsigned int bytes = ring_container->total_bytes;
3872 u8 itrval = ring_container->itr;
Auke Kok9d5c8242008-01-24 02:22:38 -08003873
Alexander Duyck0ba82992011-08-26 07:45:47 +00003874 /* no packets, exit with status unchanged */
Auke Kok9d5c8242008-01-24 02:22:38 -08003875 if (packets == 0)
Alexander Duyck0ba82992011-08-26 07:45:47 +00003876 return;
Auke Kok9d5c8242008-01-24 02:22:38 -08003877
Alexander Duyck0ba82992011-08-26 07:45:47 +00003878 switch (itrval) {
Auke Kok9d5c8242008-01-24 02:22:38 -08003879 case lowest_latency:
3880 /* handle TSO and jumbo frames */
3881 if (bytes/packets > 8000)
Alexander Duyck0ba82992011-08-26 07:45:47 +00003882 itrval = bulk_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08003883 else if ((packets < 5) && (bytes > 512))
Alexander Duyck0ba82992011-08-26 07:45:47 +00003884 itrval = low_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08003885 break;
3886 case low_latency: /* 50 usec aka 20000 ints/s */
3887 if (bytes > 10000) {
3888 /* this if handles the TSO accounting */
3889 if (bytes/packets > 8000) {
Alexander Duyck0ba82992011-08-26 07:45:47 +00003890 itrval = bulk_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08003891 } else if ((packets < 10) || ((bytes/packets) > 1200)) {
Alexander Duyck0ba82992011-08-26 07:45:47 +00003892 itrval = bulk_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08003893 } else if ((packets > 35)) {
Alexander Duyck0ba82992011-08-26 07:45:47 +00003894 itrval = lowest_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08003895 }
3896 } else if (bytes/packets > 2000) {
Alexander Duyck0ba82992011-08-26 07:45:47 +00003897 itrval = bulk_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08003898 } else if (packets <= 2 && bytes < 512) {
Alexander Duyck0ba82992011-08-26 07:45:47 +00003899 itrval = lowest_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08003900 }
3901 break;
3902 case bulk_latency: /* 250 usec aka 4000 ints/s */
3903 if (bytes > 25000) {
3904 if (packets > 35)
Alexander Duyck0ba82992011-08-26 07:45:47 +00003905 itrval = low_latency;
Alexander Duyck1e5c3d22009-02-12 18:17:21 +00003906 } else if (bytes < 1500) {
Alexander Duyck0ba82992011-08-26 07:45:47 +00003907 itrval = low_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08003908 }
3909 break;
3910 }
3911
Alexander Duyck0ba82992011-08-26 07:45:47 +00003912 /* clear work counters since we have the values we need */
3913 ring_container->total_bytes = 0;
3914 ring_container->total_packets = 0;
3915
3916 /* write updated itr to ring container */
3917 ring_container->itr = itrval;
Auke Kok9d5c8242008-01-24 02:22:38 -08003918}
3919
Alexander Duyck0ba82992011-08-26 07:45:47 +00003920static void igb_set_itr(struct igb_q_vector *q_vector)
Auke Kok9d5c8242008-01-24 02:22:38 -08003921{
Alexander Duyck0ba82992011-08-26 07:45:47 +00003922 struct igb_adapter *adapter = q_vector->adapter;
Alexander Duyck047e0032009-10-27 15:49:27 +00003923 u32 new_itr = q_vector->itr_val;
Alexander Duyck0ba82992011-08-26 07:45:47 +00003924 u8 current_itr = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08003925
3926 /* for non-gigabit speeds, just fix the interrupt rate at 4000 */
3927 if (adapter->link_speed != SPEED_1000) {
3928 current_itr = 0;
Alexander Duyck0ba82992011-08-26 07:45:47 +00003929 new_itr = IGB_4K_ITR;
Auke Kok9d5c8242008-01-24 02:22:38 -08003930 goto set_itr_now;
3931 }
3932
Alexander Duyck0ba82992011-08-26 07:45:47 +00003933 igb_update_itr(q_vector, &q_vector->tx);
3934 igb_update_itr(q_vector, &q_vector->rx);
Auke Kok9d5c8242008-01-24 02:22:38 -08003935
Alexander Duyck0ba82992011-08-26 07:45:47 +00003936 current_itr = max(q_vector->rx.itr, q_vector->tx.itr);
Auke Kok9d5c8242008-01-24 02:22:38 -08003937
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003938 /* conservative mode (itr 3) eliminates the lowest_latency setting */
Alexander Duyck0ba82992011-08-26 07:45:47 +00003939 if (current_itr == lowest_latency &&
3940 ((q_vector->rx.ring && adapter->rx_itr_setting == 3) ||
3941 (!q_vector->rx.ring && adapter->tx_itr_setting == 3)))
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003942 current_itr = low_latency;
3943
Auke Kok9d5c8242008-01-24 02:22:38 -08003944 switch (current_itr) {
3945 /* counts and packets in update_itr are dependent on these numbers */
3946 case lowest_latency:
Alexander Duyck0ba82992011-08-26 07:45:47 +00003947 new_itr = IGB_70K_ITR; /* 70,000 ints/sec */
Auke Kok9d5c8242008-01-24 02:22:38 -08003948 break;
3949 case low_latency:
Alexander Duyck0ba82992011-08-26 07:45:47 +00003950 new_itr = IGB_20K_ITR; /* 20,000 ints/sec */
Auke Kok9d5c8242008-01-24 02:22:38 -08003951 break;
3952 case bulk_latency:
Alexander Duyck0ba82992011-08-26 07:45:47 +00003953 new_itr = IGB_4K_ITR; /* 4,000 ints/sec */
Auke Kok9d5c8242008-01-24 02:22:38 -08003954 break;
3955 default:
3956 break;
3957 }
3958
3959set_itr_now:
Alexander Duyck047e0032009-10-27 15:49:27 +00003960 if (new_itr != q_vector->itr_val) {
Auke Kok9d5c8242008-01-24 02:22:38 -08003961 /* this attempts to bias the interrupt rate towards Bulk
3962 * by adding intermediate steps when interrupt rate is
3963 * increasing */
Alexander Duyck047e0032009-10-27 15:49:27 +00003964 new_itr = new_itr > q_vector->itr_val ?
3965 max((new_itr * q_vector->itr_val) /
3966 (new_itr + (q_vector->itr_val >> 2)),
Alexander Duyck0ba82992011-08-26 07:45:47 +00003967 new_itr) :
Auke Kok9d5c8242008-01-24 02:22:38 -08003968 new_itr;
3969 /* Don't write the value here; it resets the adapter's
3970 * internal timer, and causes us to delay far longer than
3971 * we should between interrupts. Instead, we write the ITR
3972 * value at the beginning of the next interrupt so the timing
3973 * ends up being correct.
3974 */
Alexander Duyck047e0032009-10-27 15:49:27 +00003975 q_vector->itr_val = new_itr;
3976 q_vector->set_itr = 1;
Auke Kok9d5c8242008-01-24 02:22:38 -08003977 }
Auke Kok9d5c8242008-01-24 02:22:38 -08003978}
3979
Stephen Hemmingerc50b52a2012-01-18 22:13:26 +00003980static void igb_tx_ctxtdesc(struct igb_ring *tx_ring, u32 vlan_macip_lens,
3981 u32 type_tucmd, u32 mss_l4len_idx)
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00003982{
3983 struct e1000_adv_tx_context_desc *context_desc;
3984 u16 i = tx_ring->next_to_use;
3985
3986 context_desc = IGB_TX_CTXTDESC(tx_ring, i);
3987
3988 i++;
3989 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
3990
3991 /* set bits to identify this as an advanced context descriptor */
3992 type_tucmd |= E1000_TXD_CMD_DEXT | E1000_ADVTXD_DTYP_CTXT;
3993
3994 /* For 82575, context index must be unique per ring. */
Alexander Duyck866cff02011-08-26 07:45:36 +00003995 if (test_bit(IGB_RING_FLAG_TX_CTX_IDX, &tx_ring->flags))
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00003996 mss_l4len_idx |= tx_ring->reg_idx << 4;
3997
3998 context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens);
3999 context_desc->seqnum_seed = 0;
4000 context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd);
4001 context_desc->mss_l4len_idx = cpu_to_le32(mss_l4len_idx);
4002}
4003
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004004static int igb_tso(struct igb_ring *tx_ring,
4005 struct igb_tx_buffer *first,
4006 u8 *hdr_len)
Auke Kok9d5c8242008-01-24 02:22:38 -08004007{
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004008 struct sk_buff *skb = first->skb;
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004009 u32 vlan_macip_lens, type_tucmd;
4010 u32 mss_l4len_idx, l4len;
4011
Alexander Duycked6aa102012-11-13 04:03:22 +00004012 if (skb->ip_summed != CHECKSUM_PARTIAL)
4013 return 0;
4014
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004015 if (!skb_is_gso(skb))
4016 return 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08004017
4018 if (skb_header_cloned(skb)) {
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004019 int err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
Auke Kok9d5c8242008-01-24 02:22:38 -08004020 if (err)
4021 return err;
4022 }
4023
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004024 /* ADV DTYP TUCMD MKRLOC/ISCSIHEDLEN */
4025 type_tucmd = E1000_ADVTXD_TUCMD_L4T_TCP;
Auke Kok9d5c8242008-01-24 02:22:38 -08004026
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004027 if (first->protocol == __constant_htons(ETH_P_IP)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08004028 struct iphdr *iph = ip_hdr(skb);
4029 iph->tot_len = 0;
4030 iph->check = 0;
4031 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
4032 iph->daddr, 0,
4033 IPPROTO_TCP,
4034 0);
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004035 type_tucmd |= E1000_ADVTXD_TUCMD_IPV4;
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004036 first->tx_flags |= IGB_TX_FLAGS_TSO |
4037 IGB_TX_FLAGS_CSUM |
4038 IGB_TX_FLAGS_IPV4;
Sridhar Samudrala8e1e8a42010-01-23 02:02:21 -08004039 } else if (skb_is_gso_v6(skb)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08004040 ipv6_hdr(skb)->payload_len = 0;
4041 tcp_hdr(skb)->check = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
4042 &ipv6_hdr(skb)->daddr,
4043 0, IPPROTO_TCP, 0);
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004044 first->tx_flags |= IGB_TX_FLAGS_TSO |
4045 IGB_TX_FLAGS_CSUM;
Auke Kok9d5c8242008-01-24 02:22:38 -08004046 }
4047
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004048 /* compute header lengths */
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004049 l4len = tcp_hdrlen(skb);
4050 *hdr_len = skb_transport_offset(skb) + l4len;
Auke Kok9d5c8242008-01-24 02:22:38 -08004051
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004052 /* update gso size and bytecount with header size */
4053 first->gso_segs = skb_shinfo(skb)->gso_segs;
4054 first->bytecount += (first->gso_segs - 1) * *hdr_len;
4055
Auke Kok9d5c8242008-01-24 02:22:38 -08004056 /* MSS L4LEN IDX */
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004057 mss_l4len_idx = l4len << E1000_ADVTXD_L4LEN_SHIFT;
4058 mss_l4len_idx |= skb_shinfo(skb)->gso_size << E1000_ADVTXD_MSS_SHIFT;
Auke Kok9d5c8242008-01-24 02:22:38 -08004059
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004060 /* VLAN MACLEN IPLEN */
4061 vlan_macip_lens = skb_network_header_len(skb);
4062 vlan_macip_lens |= skb_network_offset(skb) << E1000_ADVTXD_MACLEN_SHIFT;
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004063 vlan_macip_lens |= first->tx_flags & IGB_TX_FLAGS_VLAN_MASK;
Auke Kok9d5c8242008-01-24 02:22:38 -08004064
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004065 igb_tx_ctxtdesc(tx_ring, vlan_macip_lens, type_tucmd, mss_l4len_idx);
Auke Kok9d5c8242008-01-24 02:22:38 -08004066
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004067 return 1;
Auke Kok9d5c8242008-01-24 02:22:38 -08004068}
4069
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004070static void igb_tx_csum(struct igb_ring *tx_ring, struct igb_tx_buffer *first)
Auke Kok9d5c8242008-01-24 02:22:38 -08004071{
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004072 struct sk_buff *skb = first->skb;
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004073 u32 vlan_macip_lens = 0;
4074 u32 mss_l4len_idx = 0;
4075 u32 type_tucmd = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08004076
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004077 if (skb->ip_summed != CHECKSUM_PARTIAL) {
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004078 if (!(first->tx_flags & IGB_TX_FLAGS_VLAN))
4079 return;
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004080 } else {
4081 u8 l4_hdr = 0;
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004082 switch (first->protocol) {
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004083 case __constant_htons(ETH_P_IP):
4084 vlan_macip_lens |= skb_network_header_len(skb);
4085 type_tucmd |= E1000_ADVTXD_TUCMD_IPV4;
4086 l4_hdr = ip_hdr(skb)->protocol;
4087 break;
4088 case __constant_htons(ETH_P_IPV6):
4089 vlan_macip_lens |= skb_network_header_len(skb);
4090 l4_hdr = ipv6_hdr(skb)->nexthdr;
4091 break;
4092 default:
4093 if (unlikely(net_ratelimit())) {
4094 dev_warn(tx_ring->dev,
4095 "partial checksum but proto=%x!\n",
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004096 first->protocol);
Arthur Jonesfa4a7ef2009-03-21 16:55:07 -07004097 }
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004098 break;
Auke Kok9d5c8242008-01-24 02:22:38 -08004099 }
4100
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004101 switch (l4_hdr) {
4102 case IPPROTO_TCP:
4103 type_tucmd |= E1000_ADVTXD_TUCMD_L4T_TCP;
4104 mss_l4len_idx = tcp_hdrlen(skb) <<
4105 E1000_ADVTXD_L4LEN_SHIFT;
4106 break;
4107 case IPPROTO_SCTP:
4108 type_tucmd |= E1000_ADVTXD_TUCMD_L4T_SCTP;
4109 mss_l4len_idx = sizeof(struct sctphdr) <<
4110 E1000_ADVTXD_L4LEN_SHIFT;
4111 break;
4112 case IPPROTO_UDP:
4113 mss_l4len_idx = sizeof(struct udphdr) <<
4114 E1000_ADVTXD_L4LEN_SHIFT;
4115 break;
4116 default:
4117 if (unlikely(net_ratelimit())) {
4118 dev_warn(tx_ring->dev,
4119 "partial checksum but l4 proto=%x!\n",
4120 l4_hdr);
4121 }
4122 break;
4123 }
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004124
4125 /* update TX checksum flag */
4126 first->tx_flags |= IGB_TX_FLAGS_CSUM;
Auke Kok9d5c8242008-01-24 02:22:38 -08004127 }
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004128
4129 vlan_macip_lens |= skb_network_offset(skb) << E1000_ADVTXD_MACLEN_SHIFT;
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004130 vlan_macip_lens |= first->tx_flags & IGB_TX_FLAGS_VLAN_MASK;
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004131
4132 igb_tx_ctxtdesc(tx_ring, vlan_macip_lens, type_tucmd, mss_l4len_idx);
Auke Kok9d5c8242008-01-24 02:22:38 -08004133}
4134
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004135#define IGB_SET_FLAG(_input, _flag, _result) \
4136 ((_flag <= _result) ? \
4137 ((u32)(_input & _flag) * (_result / _flag)) : \
4138 ((u32)(_input & _flag) / (_flag / _result)))
4139
4140static u32 igb_tx_cmd_type(struct sk_buff *skb, u32 tx_flags)
Alexander Duycke032afc2011-08-26 07:44:48 +00004141{
4142 /* set type for advanced descriptor with frame checksum insertion */
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004143 u32 cmd_type = E1000_ADVTXD_DTYP_DATA |
4144 E1000_ADVTXD_DCMD_DEXT |
4145 E1000_ADVTXD_DCMD_IFCS;
Alexander Duycke032afc2011-08-26 07:44:48 +00004146
4147 /* set HW vlan bit if vlan is present */
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004148 cmd_type |= IGB_SET_FLAG(tx_flags, IGB_TX_FLAGS_VLAN,
4149 (E1000_ADVTXD_DCMD_VLE));
Alexander Duycke032afc2011-08-26 07:44:48 +00004150
4151 /* set segmentation bits for TSO */
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004152 cmd_type |= IGB_SET_FLAG(tx_flags, IGB_TX_FLAGS_TSO,
4153 (E1000_ADVTXD_DCMD_TSE));
4154
4155 /* set timestamp bit if present */
4156 cmd_type |= IGB_SET_FLAG(tx_flags, IGB_TX_FLAGS_TSTAMP,
4157 (E1000_ADVTXD_MAC_TSTAMP));
4158
4159 /* insert frame checksum */
4160 cmd_type ^= IGB_SET_FLAG(skb->no_fcs, 1, E1000_ADVTXD_DCMD_IFCS);
Alexander Duycke032afc2011-08-26 07:44:48 +00004161
4162 return cmd_type;
4163}
4164
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004165static void igb_tx_olinfo_status(struct igb_ring *tx_ring,
4166 union e1000_adv_tx_desc *tx_desc,
4167 u32 tx_flags, unsigned int paylen)
Alexander Duycke032afc2011-08-26 07:44:48 +00004168{
4169 u32 olinfo_status = paylen << E1000_ADVTXD_PAYLEN_SHIFT;
4170
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004171 /* 82575 requires a unique index per ring */
4172 if (test_bit(IGB_RING_FLAG_TX_CTX_IDX, &tx_ring->flags))
Alexander Duycke032afc2011-08-26 07:44:48 +00004173 olinfo_status |= tx_ring->reg_idx << 4;
4174
4175 /* insert L4 checksum */
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004176 olinfo_status |= IGB_SET_FLAG(tx_flags,
4177 IGB_TX_FLAGS_CSUM,
4178 (E1000_TXD_POPTS_TXSM << 8));
Alexander Duycke032afc2011-08-26 07:44:48 +00004179
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004180 /* insert IPv4 checksum */
4181 olinfo_status |= IGB_SET_FLAG(tx_flags,
4182 IGB_TX_FLAGS_IPV4,
4183 (E1000_TXD_POPTS_IXSM << 8));
Alexander Duycke032afc2011-08-26 07:44:48 +00004184
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004185 tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status);
Alexander Duycke032afc2011-08-26 07:44:48 +00004186}
4187
Alexander Duyckebe42d12011-08-26 07:45:09 +00004188/*
4189 * The largest size we can write to the descriptor is 65535. In order to
4190 * maintain a power of two alignment we have to limit ourselves to 32K.
4191 */
4192#define IGB_MAX_TXD_PWR 15
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004193#define IGB_MAX_DATA_PER_TXD (1<<IGB_MAX_TXD_PWR)
Auke Kok9d5c8242008-01-24 02:22:38 -08004194
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004195static void igb_tx_map(struct igb_ring *tx_ring,
4196 struct igb_tx_buffer *first,
Alexander Duyckebe42d12011-08-26 07:45:09 +00004197 const u8 hdr_len)
Auke Kok9d5c8242008-01-24 02:22:38 -08004198{
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004199 struct sk_buff *skb = first->skb;
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00004200 struct igb_tx_buffer *tx_buffer;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004201 union e1000_adv_tx_desc *tx_desc;
Alexander Duyck80d07592012-11-13 04:03:24 +00004202 struct skb_frag_struct *frag;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004203 dma_addr_t dma;
Alexander Duyck80d07592012-11-13 04:03:24 +00004204 unsigned int data_len, size;
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004205 u32 tx_flags = first->tx_flags;
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004206 u32 cmd_type = igb_tx_cmd_type(skb, tx_flags);
Alexander Duyckebe42d12011-08-26 07:45:09 +00004207 u16 i = tx_ring->next_to_use;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004208
4209 tx_desc = IGB_TX_DESC(tx_ring, i);
4210
Alexander Duyck80d07592012-11-13 04:03:24 +00004211 igb_tx_olinfo_status(tx_ring, tx_desc, tx_flags, skb->len - hdr_len);
4212
4213 size = skb_headlen(skb);
4214 data_len = skb->data_len;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004215
4216 dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
Auke Kok9d5c8242008-01-24 02:22:38 -08004217
Alexander Duyck80d07592012-11-13 04:03:24 +00004218 tx_buffer = first;
Alexander Duyck2bbfebe2011-08-26 07:44:59 +00004219
Alexander Duyck80d07592012-11-13 04:03:24 +00004220 for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
4221 if (dma_mapping_error(tx_ring->dev, dma))
4222 goto dma_error;
4223
4224 /* record length, and DMA address */
4225 dma_unmap_len_set(tx_buffer, len, size);
4226 dma_unmap_addr_set(tx_buffer, dma, dma);
4227
4228 tx_desc->read.buffer_addr = cpu_to_le64(dma);
4229
Alexander Duyckebe42d12011-08-26 07:45:09 +00004230 while (unlikely(size > IGB_MAX_DATA_PER_TXD)) {
4231 tx_desc->read.cmd_type_len =
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004232 cpu_to_le32(cmd_type ^ IGB_MAX_DATA_PER_TXD);
Auke Kok9d5c8242008-01-24 02:22:38 -08004233
Alexander Duyckebe42d12011-08-26 07:45:09 +00004234 i++;
4235 tx_desc++;
4236 if (i == tx_ring->count) {
4237 tx_desc = IGB_TX_DESC(tx_ring, 0);
4238 i = 0;
4239 }
Alexander Duyck80d07592012-11-13 04:03:24 +00004240 tx_desc->read.olinfo_status = 0;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004241
4242 dma += IGB_MAX_DATA_PER_TXD;
4243 size -= IGB_MAX_DATA_PER_TXD;
4244
Alexander Duyckebe42d12011-08-26 07:45:09 +00004245 tx_desc->read.buffer_addr = cpu_to_le64(dma);
4246 }
4247
4248 if (likely(!data_len))
4249 break;
4250
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004251 tx_desc->read.cmd_type_len = cpu_to_le32(cmd_type ^ size);
Alexander Duyckebe42d12011-08-26 07:45:09 +00004252
Alexander Duyck65689fe2009-03-20 00:17:43 +00004253 i++;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004254 tx_desc++;
4255 if (i == tx_ring->count) {
4256 tx_desc = IGB_TX_DESC(tx_ring, 0);
Alexander Duyck65689fe2009-03-20 00:17:43 +00004257 i = 0;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004258 }
Alexander Duyck80d07592012-11-13 04:03:24 +00004259 tx_desc->read.olinfo_status = 0;
Alexander Duyck65689fe2009-03-20 00:17:43 +00004260
Eric Dumazet9e903e02011-10-18 21:00:24 +00004261 size = skb_frag_size(frag);
Alexander Duyckebe42d12011-08-26 07:45:09 +00004262 data_len -= size;
4263
4264 dma = skb_frag_dma_map(tx_ring->dev, frag, 0,
Alexander Duyck80d07592012-11-13 04:03:24 +00004265 size, DMA_TO_DEVICE);
Alexander Duyck6366ad32009-12-02 16:47:18 +00004266
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00004267 tx_buffer = &tx_ring->tx_buffer_info[i];
Auke Kok9d5c8242008-01-24 02:22:38 -08004268 }
4269
Alexander Duyckebe42d12011-08-26 07:45:09 +00004270 /* write last descriptor with RS and EOP bits */
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004271 cmd_type |= size | IGB_TXD_DCMD;
4272 tx_desc->read.cmd_type_len = cpu_to_le32(cmd_type);
Alexander Duyck8542db02011-08-26 07:44:43 +00004273
Alexander Duyck80d07592012-11-13 04:03:24 +00004274 netdev_tx_sent_queue(txring_txq(tx_ring), first->bytecount);
4275
Alexander Duyck8542db02011-08-26 07:44:43 +00004276 /* set the timestamp */
4277 first->time_stamp = jiffies;
4278
Alexander Duyckebe42d12011-08-26 07:45:09 +00004279 /*
4280 * Force memory writes to complete before letting h/w know there
4281 * are new descriptors to fetch. (Only applicable for weak-ordered
4282 * memory model archs, such as IA-64).
4283 *
4284 * We also need this memory barrier to make certain all of the
4285 * status bits have been updated before next_to_watch is written.
4286 */
Auke Kok9d5c8242008-01-24 02:22:38 -08004287 wmb();
4288
Alexander Duyckebe42d12011-08-26 07:45:09 +00004289 /* set next_to_watch value indicating a packet is present */
4290 first->next_to_watch = tx_desc;
4291
4292 i++;
4293 if (i == tx_ring->count)
4294 i = 0;
4295
Auke Kok9d5c8242008-01-24 02:22:38 -08004296 tx_ring->next_to_use = i;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004297
Alexander Duyckfce99e32009-10-27 15:51:27 +00004298 writel(i, tx_ring->tail);
Alexander Duyckebe42d12011-08-26 07:45:09 +00004299
Auke Kok9d5c8242008-01-24 02:22:38 -08004300 /* we need this if more than one processor can write to our tail
4301 * at a time, it syncronizes IO on IA64/Altix systems */
4302 mmiowb();
Alexander Duyckebe42d12011-08-26 07:45:09 +00004303
4304 return;
4305
4306dma_error:
4307 dev_err(tx_ring->dev, "TX DMA map failed\n");
4308
4309 /* clear dma mappings for failed tx_buffer_info map */
4310 for (;;) {
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00004311 tx_buffer = &tx_ring->tx_buffer_info[i];
4312 igb_unmap_and_free_tx_resource(tx_ring, tx_buffer);
4313 if (tx_buffer == first)
Alexander Duyckebe42d12011-08-26 07:45:09 +00004314 break;
4315 if (i == 0)
4316 i = tx_ring->count;
4317 i--;
4318 }
4319
4320 tx_ring->next_to_use = i;
Auke Kok9d5c8242008-01-24 02:22:38 -08004321}
4322
Alexander Duyck6ad4edf2011-08-26 07:45:26 +00004323static int __igb_maybe_stop_tx(struct igb_ring *tx_ring, const u16 size)
Auke Kok9d5c8242008-01-24 02:22:38 -08004324{
Alexander Duycke694e962009-10-27 15:53:06 +00004325 struct net_device *netdev = tx_ring->netdev;
4326
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07004327 netif_stop_subqueue(netdev, tx_ring->queue_index);
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07004328
Auke Kok9d5c8242008-01-24 02:22:38 -08004329 /* Herbert's original patch had:
4330 * smp_mb__after_netif_stop_queue();
4331 * but since that doesn't exist yet, just open code it. */
4332 smp_mb();
4333
4334 /* We need to check again in a case another CPU has just
4335 * made room available. */
Alexander Duyckc493ea42009-03-20 00:16:50 +00004336 if (igb_desc_unused(tx_ring) < size)
Auke Kok9d5c8242008-01-24 02:22:38 -08004337 return -EBUSY;
4338
4339 /* A reprieve! */
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07004340 netif_wake_subqueue(netdev, tx_ring->queue_index);
Eric Dumazet12dcd862010-10-15 17:27:10 +00004341
4342 u64_stats_update_begin(&tx_ring->tx_syncp2);
4343 tx_ring->tx_stats.restart_queue2++;
4344 u64_stats_update_end(&tx_ring->tx_syncp2);
4345
Auke Kok9d5c8242008-01-24 02:22:38 -08004346 return 0;
4347}
4348
Alexander Duyck6ad4edf2011-08-26 07:45:26 +00004349static inline int igb_maybe_stop_tx(struct igb_ring *tx_ring, const u16 size)
Auke Kok9d5c8242008-01-24 02:22:38 -08004350{
Alexander Duyckc493ea42009-03-20 00:16:50 +00004351 if (igb_desc_unused(tx_ring) >= size)
Auke Kok9d5c8242008-01-24 02:22:38 -08004352 return 0;
Alexander Duycke694e962009-10-27 15:53:06 +00004353 return __igb_maybe_stop_tx(tx_ring, size);
Auke Kok9d5c8242008-01-24 02:22:38 -08004354}
4355
Alexander Duyckcd392f52011-08-26 07:43:59 +00004356netdev_tx_t igb_xmit_frame_ring(struct sk_buff *skb,
4357 struct igb_ring *tx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08004358{
Matthew Vick1f6e8172012-08-18 07:26:33 +00004359 struct igb_adapter *adapter = netdev_priv(tx_ring->netdev);
Alexander Duyck8542db02011-08-26 07:44:43 +00004360 struct igb_tx_buffer *first;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004361 int tso;
Nick Nunley91d4ee32010-02-17 01:04:56 +00004362 u32 tx_flags = 0;
Alexander Duyck31f6adb2011-08-26 07:44:53 +00004363 __be16 protocol = vlan_get_protocol(skb);
Nick Nunley91d4ee32010-02-17 01:04:56 +00004364 u8 hdr_len = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08004365
Auke Kok9d5c8242008-01-24 02:22:38 -08004366 /* need: 1 descriptor per page,
4367 * + 2 desc gap to keep tail from touching head,
4368 * + 1 desc for skb->data,
4369 * + 1 desc for context descriptor,
4370 * otherwise try next time */
Alexander Duycke694e962009-10-27 15:53:06 +00004371 if (igb_maybe_stop_tx(tx_ring, skb_shinfo(skb)->nr_frags + 4)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08004372 /* this is a hard error */
Auke Kok9d5c8242008-01-24 02:22:38 -08004373 return NETDEV_TX_BUSY;
4374 }
Patrick Ohly33af6bc2009-02-12 05:03:43 +00004375
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004376 /* record the location of the first descriptor for this packet */
4377 first = &tx_ring->tx_buffer_info[tx_ring->next_to_use];
4378 first->skb = skb;
4379 first->bytecount = skb->len;
4380 first->gso_segs = 1;
4381
Matthew Vick1f6e8172012-08-18 07:26:33 +00004382 if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
4383 !(adapter->ptp_tx_skb))) {
Oliver Hartkopp2244d072010-08-17 08:59:14 +00004384 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
Patrick Ohly33af6bc2009-02-12 05:03:43 +00004385 tx_flags |= IGB_TX_FLAGS_TSTAMP;
Matthew Vick1f6e8172012-08-18 07:26:33 +00004386
4387 adapter->ptp_tx_skb = skb_get(skb);
4388 if (adapter->hw.mac.type == e1000_82576)
4389 schedule_work(&adapter->ptp_tx_work);
Patrick Ohly33af6bc2009-02-12 05:03:43 +00004390 }
Auke Kok9d5c8242008-01-24 02:22:38 -08004391
Jesse Grosseab6d182010-10-20 13:56:03 +00004392 if (vlan_tx_tag_present(skb)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08004393 tx_flags |= IGB_TX_FLAGS_VLAN;
4394 tx_flags |= (vlan_tx_tag_get(skb) << IGB_TX_FLAGS_VLAN_SHIFT);
4395 }
4396
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004397 /* record initial flags and protocol */
4398 first->tx_flags = tx_flags;
4399 first->protocol = protocol;
Alexander Duyckcdfd01fc2009-10-27 23:50:57 +00004400
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004401 tso = igb_tso(tx_ring, first, &hdr_len);
4402 if (tso < 0)
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004403 goto out_drop;
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004404 else if (!tso)
4405 igb_tx_csum(tx_ring, first);
Auke Kok9d5c8242008-01-24 02:22:38 -08004406
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004407 igb_tx_map(tx_ring, first, hdr_len);
Alexander Duyck85ad76b2009-10-27 15:52:46 +00004408
4409 /* Make sure there is space in the ring for the next send. */
Alexander Duycke694e962009-10-27 15:53:06 +00004410 igb_maybe_stop_tx(tx_ring, MAX_SKB_FRAGS + 4);
Alexander Duyck85ad76b2009-10-27 15:52:46 +00004411
Auke Kok9d5c8242008-01-24 02:22:38 -08004412 return NETDEV_TX_OK;
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004413
4414out_drop:
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004415 igb_unmap_and_free_tx_resource(tx_ring, first);
4416
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004417 return NETDEV_TX_OK;
Auke Kok9d5c8242008-01-24 02:22:38 -08004418}
4419
Alexander Duyck1cc3bd82011-08-26 07:44:10 +00004420static inline struct igb_ring *igb_tx_queue_mapping(struct igb_adapter *adapter,
4421 struct sk_buff *skb)
4422{
4423 unsigned int r_idx = skb->queue_mapping;
4424
4425 if (r_idx >= adapter->num_tx_queues)
4426 r_idx = r_idx % adapter->num_tx_queues;
4427
4428 return adapter->tx_ring[r_idx];
4429}
4430
Alexander Duyckcd392f52011-08-26 07:43:59 +00004431static netdev_tx_t igb_xmit_frame(struct sk_buff *skb,
4432 struct net_device *netdev)
Auke Kok9d5c8242008-01-24 02:22:38 -08004433{
4434 struct igb_adapter *adapter = netdev_priv(netdev);
Alexander Duyckb1a436c2009-10-27 15:54:43 +00004435
4436 if (test_bit(__IGB_DOWN, &adapter->state)) {
4437 dev_kfree_skb_any(skb);
4438 return NETDEV_TX_OK;
4439 }
4440
4441 if (skb->len <= 0) {
4442 dev_kfree_skb_any(skb);
4443 return NETDEV_TX_OK;
4444 }
4445
Alexander Duyck1cc3bd82011-08-26 07:44:10 +00004446 /*
4447 * The minimum packet size with TCTL.PSP set is 17 so pad the skb
4448 * in order to meet this minimum size requirement.
4449 */
Tushar Daveea5ceea2012-09-14 03:43:43 +00004450 if (unlikely(skb->len < 17)) {
4451 if (skb_pad(skb, 17 - skb->len))
Alexander Duyck1cc3bd82011-08-26 07:44:10 +00004452 return NETDEV_TX_OK;
4453 skb->len = 17;
Tushar Daveea5ceea2012-09-14 03:43:43 +00004454 skb_set_tail_pointer(skb, 17);
Alexander Duyck1cc3bd82011-08-26 07:44:10 +00004455 }
Auke Kok9d5c8242008-01-24 02:22:38 -08004456
Alexander Duyck1cc3bd82011-08-26 07:44:10 +00004457 return igb_xmit_frame_ring(skb, igb_tx_queue_mapping(adapter, skb));
Auke Kok9d5c8242008-01-24 02:22:38 -08004458}
4459
4460/**
4461 * igb_tx_timeout - Respond to a Tx Hang
4462 * @netdev: network interface device structure
4463 **/
4464static void igb_tx_timeout(struct net_device *netdev)
4465{
4466 struct igb_adapter *adapter = netdev_priv(netdev);
4467 struct e1000_hw *hw = &adapter->hw;
4468
4469 /* Do the reset outside of interrupt context */
4470 adapter->tx_timeout_count++;
Alexander Duyckf7ba2052009-10-27 23:48:51 +00004471
Alexander Duyck06218a82011-08-26 07:46:55 +00004472 if (hw->mac.type >= e1000_82580)
Alexander Duyck55cac242009-11-19 12:42:21 +00004473 hw->dev_spec._82575.global_device_reset = true;
4474
Auke Kok9d5c8242008-01-24 02:22:38 -08004475 schedule_work(&adapter->reset_task);
Alexander Duyck265de402009-02-06 23:22:52 +00004476 wr32(E1000_EICS,
4477 (adapter->eims_enable_mask & ~adapter->eims_other));
Auke Kok9d5c8242008-01-24 02:22:38 -08004478}
4479
4480static void igb_reset_task(struct work_struct *work)
4481{
4482 struct igb_adapter *adapter;
4483 adapter = container_of(work, struct igb_adapter, reset_task);
4484
Taku Izumic97ec422010-04-27 14:39:30 +00004485 igb_dump(adapter);
4486 netdev_err(adapter->netdev, "Reset adapter\n");
Auke Kok9d5c8242008-01-24 02:22:38 -08004487 igb_reinit_locked(adapter);
4488}
4489
4490/**
Eric Dumazet12dcd862010-10-15 17:27:10 +00004491 * igb_get_stats64 - Get System Network Statistics
Auke Kok9d5c8242008-01-24 02:22:38 -08004492 * @netdev: network interface device structure
Eric Dumazet12dcd862010-10-15 17:27:10 +00004493 * @stats: rtnl_link_stats64 pointer
Auke Kok9d5c8242008-01-24 02:22:38 -08004494 *
Auke Kok9d5c8242008-01-24 02:22:38 -08004495 **/
Eric Dumazet12dcd862010-10-15 17:27:10 +00004496static struct rtnl_link_stats64 *igb_get_stats64(struct net_device *netdev,
4497 struct rtnl_link_stats64 *stats)
Auke Kok9d5c8242008-01-24 02:22:38 -08004498{
Eric Dumazet12dcd862010-10-15 17:27:10 +00004499 struct igb_adapter *adapter = netdev_priv(netdev);
4500
4501 spin_lock(&adapter->stats64_lock);
4502 igb_update_stats(adapter, &adapter->stats64);
4503 memcpy(stats, &adapter->stats64, sizeof(*stats));
4504 spin_unlock(&adapter->stats64_lock);
4505
4506 return stats;
Auke Kok9d5c8242008-01-24 02:22:38 -08004507}
4508
4509/**
4510 * igb_change_mtu - Change the Maximum Transfer Unit
4511 * @netdev: network interface device structure
4512 * @new_mtu: new value for maximum frame size
4513 *
4514 * Returns 0 on success, negative on failure
4515 **/
4516static int igb_change_mtu(struct net_device *netdev, int new_mtu)
4517{
4518 struct igb_adapter *adapter = netdev_priv(netdev);
Alexander Duyck090b1792009-10-27 23:51:55 +00004519 struct pci_dev *pdev = adapter->pdev;
Alexander Duyck153285f2011-08-26 07:43:32 +00004520 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
Auke Kok9d5c8242008-01-24 02:22:38 -08004521
Alexander Duyckc809d222009-10-27 23:52:13 +00004522 if ((new_mtu < 68) || (max_frame > MAX_JUMBO_FRAME_SIZE)) {
Alexander Duyck090b1792009-10-27 23:51:55 +00004523 dev_err(&pdev->dev, "Invalid MTU setting\n");
Auke Kok9d5c8242008-01-24 02:22:38 -08004524 return -EINVAL;
4525 }
4526
Alexander Duyck153285f2011-08-26 07:43:32 +00004527#define MAX_STD_JUMBO_FRAME_SIZE 9238
Auke Kok9d5c8242008-01-24 02:22:38 -08004528 if (max_frame > MAX_STD_JUMBO_FRAME_SIZE) {
Alexander Duyck090b1792009-10-27 23:51:55 +00004529 dev_err(&pdev->dev, "MTU > 9216 not supported.\n");
Auke Kok9d5c8242008-01-24 02:22:38 -08004530 return -EINVAL;
4531 }
4532
4533 while (test_and_set_bit(__IGB_RESETTING, &adapter->state))
4534 msleep(1);
Alexander Duyck73cd78f2009-02-12 18:16:59 +00004535
Auke Kok9d5c8242008-01-24 02:22:38 -08004536 /* igb_down has a dependency on max_frame_size */
4537 adapter->max_frame_size = max_frame;
Alexander Duyck559e9c42009-10-27 23:52:50 +00004538
Alexander Duyck4c844852009-10-27 15:52:07 +00004539 if (netif_running(netdev))
4540 igb_down(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08004541
Alexander Duyck090b1792009-10-27 23:51:55 +00004542 dev_info(&pdev->dev, "changing MTU from %d to %d\n",
Auke Kok9d5c8242008-01-24 02:22:38 -08004543 netdev->mtu, new_mtu);
4544 netdev->mtu = new_mtu;
4545
4546 if (netif_running(netdev))
4547 igb_up(adapter);
4548 else
4549 igb_reset(adapter);
4550
4551 clear_bit(__IGB_RESETTING, &adapter->state);
4552
4553 return 0;
4554}
4555
4556/**
4557 * igb_update_stats - Update the board statistics counters
4558 * @adapter: board private structure
4559 **/
4560
Eric Dumazet12dcd862010-10-15 17:27:10 +00004561void igb_update_stats(struct igb_adapter *adapter,
4562 struct rtnl_link_stats64 *net_stats)
Auke Kok9d5c8242008-01-24 02:22:38 -08004563{
4564 struct e1000_hw *hw = &adapter->hw;
4565 struct pci_dev *pdev = adapter->pdev;
Mitch Williamsfa3d9a62010-03-23 18:34:38 +00004566 u32 reg, mpc;
Auke Kok9d5c8242008-01-24 02:22:38 -08004567 u16 phy_tmp;
Alexander Duyck3f9c0162009-10-27 23:48:12 +00004568 int i;
4569 u64 bytes, packets;
Eric Dumazet12dcd862010-10-15 17:27:10 +00004570 unsigned int start;
4571 u64 _bytes, _packets;
Auke Kok9d5c8242008-01-24 02:22:38 -08004572
4573#define PHY_IDLE_ERROR_COUNT_MASK 0x00FF
4574
4575 /*
4576 * Prevent stats update while adapter is being reset, or if the pci
4577 * connection is down.
4578 */
4579 if (adapter->link_speed == 0)
4580 return;
4581 if (pci_channel_offline(pdev))
4582 return;
4583
Alexander Duyck3f9c0162009-10-27 23:48:12 +00004584 bytes = 0;
4585 packets = 0;
4586 for (i = 0; i < adapter->num_rx_queues; i++) {
Alexander Duyckae1c07a62012-08-08 05:23:22 +00004587 u32 rqdpc = rd32(E1000_RQDPC(i));
Alexander Duyck3025a442010-02-17 01:02:39 +00004588 struct igb_ring *ring = adapter->rx_ring[i];
Eric Dumazet12dcd862010-10-15 17:27:10 +00004589
Alexander Duyckae1c07a62012-08-08 05:23:22 +00004590 if (rqdpc) {
4591 ring->rx_stats.drops += rqdpc;
4592 net_stats->rx_fifo_errors += rqdpc;
4593 }
Eric Dumazet12dcd862010-10-15 17:27:10 +00004594
4595 do {
4596 start = u64_stats_fetch_begin_bh(&ring->rx_syncp);
4597 _bytes = ring->rx_stats.bytes;
4598 _packets = ring->rx_stats.packets;
4599 } while (u64_stats_fetch_retry_bh(&ring->rx_syncp, start));
4600 bytes += _bytes;
4601 packets += _packets;
Alexander Duyck3f9c0162009-10-27 23:48:12 +00004602 }
4603
Alexander Duyck128e45e2009-11-12 18:37:38 +00004604 net_stats->rx_bytes = bytes;
4605 net_stats->rx_packets = packets;
Alexander Duyck3f9c0162009-10-27 23:48:12 +00004606
4607 bytes = 0;
4608 packets = 0;
4609 for (i = 0; i < adapter->num_tx_queues; i++) {
Alexander Duyck3025a442010-02-17 01:02:39 +00004610 struct igb_ring *ring = adapter->tx_ring[i];
Eric Dumazet12dcd862010-10-15 17:27:10 +00004611 do {
4612 start = u64_stats_fetch_begin_bh(&ring->tx_syncp);
4613 _bytes = ring->tx_stats.bytes;
4614 _packets = ring->tx_stats.packets;
4615 } while (u64_stats_fetch_retry_bh(&ring->tx_syncp, start));
4616 bytes += _bytes;
4617 packets += _packets;
Alexander Duyck3f9c0162009-10-27 23:48:12 +00004618 }
Alexander Duyck128e45e2009-11-12 18:37:38 +00004619 net_stats->tx_bytes = bytes;
4620 net_stats->tx_packets = packets;
Alexander Duyck3f9c0162009-10-27 23:48:12 +00004621
4622 /* read stats registers */
Auke Kok9d5c8242008-01-24 02:22:38 -08004623 adapter->stats.crcerrs += rd32(E1000_CRCERRS);
4624 adapter->stats.gprc += rd32(E1000_GPRC);
4625 adapter->stats.gorc += rd32(E1000_GORCL);
4626 rd32(E1000_GORCH); /* clear GORCL */
4627 adapter->stats.bprc += rd32(E1000_BPRC);
4628 adapter->stats.mprc += rd32(E1000_MPRC);
4629 adapter->stats.roc += rd32(E1000_ROC);
4630
4631 adapter->stats.prc64 += rd32(E1000_PRC64);
4632 adapter->stats.prc127 += rd32(E1000_PRC127);
4633 adapter->stats.prc255 += rd32(E1000_PRC255);
4634 adapter->stats.prc511 += rd32(E1000_PRC511);
4635 adapter->stats.prc1023 += rd32(E1000_PRC1023);
4636 adapter->stats.prc1522 += rd32(E1000_PRC1522);
4637 adapter->stats.symerrs += rd32(E1000_SYMERRS);
4638 adapter->stats.sec += rd32(E1000_SEC);
4639
Mitch Williamsfa3d9a62010-03-23 18:34:38 +00004640 mpc = rd32(E1000_MPC);
4641 adapter->stats.mpc += mpc;
4642 net_stats->rx_fifo_errors += mpc;
Auke Kok9d5c8242008-01-24 02:22:38 -08004643 adapter->stats.scc += rd32(E1000_SCC);
4644 adapter->stats.ecol += rd32(E1000_ECOL);
4645 adapter->stats.mcc += rd32(E1000_MCC);
4646 adapter->stats.latecol += rd32(E1000_LATECOL);
4647 adapter->stats.dc += rd32(E1000_DC);
4648 adapter->stats.rlec += rd32(E1000_RLEC);
4649 adapter->stats.xonrxc += rd32(E1000_XONRXC);
4650 adapter->stats.xontxc += rd32(E1000_XONTXC);
4651 adapter->stats.xoffrxc += rd32(E1000_XOFFRXC);
4652 adapter->stats.xofftxc += rd32(E1000_XOFFTXC);
4653 adapter->stats.fcruc += rd32(E1000_FCRUC);
4654 adapter->stats.gptc += rd32(E1000_GPTC);
4655 adapter->stats.gotc += rd32(E1000_GOTCL);
4656 rd32(E1000_GOTCH); /* clear GOTCL */
Mitch Williamsfa3d9a62010-03-23 18:34:38 +00004657 adapter->stats.rnbc += rd32(E1000_RNBC);
Auke Kok9d5c8242008-01-24 02:22:38 -08004658 adapter->stats.ruc += rd32(E1000_RUC);
4659 adapter->stats.rfc += rd32(E1000_RFC);
4660 adapter->stats.rjc += rd32(E1000_RJC);
4661 adapter->stats.tor += rd32(E1000_TORH);
4662 adapter->stats.tot += rd32(E1000_TOTH);
4663 adapter->stats.tpr += rd32(E1000_TPR);
4664
4665 adapter->stats.ptc64 += rd32(E1000_PTC64);
4666 adapter->stats.ptc127 += rd32(E1000_PTC127);
4667 adapter->stats.ptc255 += rd32(E1000_PTC255);
4668 adapter->stats.ptc511 += rd32(E1000_PTC511);
4669 adapter->stats.ptc1023 += rd32(E1000_PTC1023);
4670 adapter->stats.ptc1522 += rd32(E1000_PTC1522);
4671
4672 adapter->stats.mptc += rd32(E1000_MPTC);
4673 adapter->stats.bptc += rd32(E1000_BPTC);
4674
Nick Nunley2d0b0f62010-02-17 01:02:59 +00004675 adapter->stats.tpt += rd32(E1000_TPT);
4676 adapter->stats.colc += rd32(E1000_COLC);
Auke Kok9d5c8242008-01-24 02:22:38 -08004677
4678 adapter->stats.algnerrc += rd32(E1000_ALGNERRC);
Nick Nunley43915c7c2010-02-17 01:03:58 +00004679 /* read internal phy specific stats */
4680 reg = rd32(E1000_CTRL_EXT);
4681 if (!(reg & E1000_CTRL_EXT_LINK_MODE_MASK)) {
4682 adapter->stats.rxerrc += rd32(E1000_RXERRC);
Carolyn Wyborny3dbdf962012-09-12 04:36:24 +00004683
4684 /* this stat has invalid values on i210/i211 */
4685 if ((hw->mac.type != e1000_i210) &&
4686 (hw->mac.type != e1000_i211))
4687 adapter->stats.tncrs += rd32(E1000_TNCRS);
Nick Nunley43915c7c2010-02-17 01:03:58 +00004688 }
4689
Auke Kok9d5c8242008-01-24 02:22:38 -08004690 adapter->stats.tsctc += rd32(E1000_TSCTC);
4691 adapter->stats.tsctfc += rd32(E1000_TSCTFC);
4692
4693 adapter->stats.iac += rd32(E1000_IAC);
4694 adapter->stats.icrxoc += rd32(E1000_ICRXOC);
4695 adapter->stats.icrxptc += rd32(E1000_ICRXPTC);
4696 adapter->stats.icrxatc += rd32(E1000_ICRXATC);
4697 adapter->stats.ictxptc += rd32(E1000_ICTXPTC);
4698 adapter->stats.ictxatc += rd32(E1000_ICTXATC);
4699 adapter->stats.ictxqec += rd32(E1000_ICTXQEC);
4700 adapter->stats.ictxqmtc += rd32(E1000_ICTXQMTC);
4701 adapter->stats.icrxdmtc += rd32(E1000_ICRXDMTC);
4702
4703 /* Fill out the OS statistics structure */
Alexander Duyck128e45e2009-11-12 18:37:38 +00004704 net_stats->multicast = adapter->stats.mprc;
4705 net_stats->collisions = adapter->stats.colc;
Auke Kok9d5c8242008-01-24 02:22:38 -08004706
4707 /* Rx Errors */
4708
4709 /* RLEC on some newer hardware can be incorrect so build
Jesper Dangaard Brouer8c0ab702009-05-26 13:50:31 +00004710 * our own version based on RUC and ROC */
Alexander Duyck128e45e2009-11-12 18:37:38 +00004711 net_stats->rx_errors = adapter->stats.rxerrc +
Auke Kok9d5c8242008-01-24 02:22:38 -08004712 adapter->stats.crcerrs + adapter->stats.algnerrc +
4713 adapter->stats.ruc + adapter->stats.roc +
4714 adapter->stats.cexterr;
Alexander Duyck128e45e2009-11-12 18:37:38 +00004715 net_stats->rx_length_errors = adapter->stats.ruc +
4716 adapter->stats.roc;
4717 net_stats->rx_crc_errors = adapter->stats.crcerrs;
4718 net_stats->rx_frame_errors = adapter->stats.algnerrc;
4719 net_stats->rx_missed_errors = adapter->stats.mpc;
Auke Kok9d5c8242008-01-24 02:22:38 -08004720
4721 /* Tx Errors */
Alexander Duyck128e45e2009-11-12 18:37:38 +00004722 net_stats->tx_errors = adapter->stats.ecol +
4723 adapter->stats.latecol;
4724 net_stats->tx_aborted_errors = adapter->stats.ecol;
4725 net_stats->tx_window_errors = adapter->stats.latecol;
4726 net_stats->tx_carrier_errors = adapter->stats.tncrs;
Auke Kok9d5c8242008-01-24 02:22:38 -08004727
4728 /* Tx Dropped needs to be maintained elsewhere */
4729
4730 /* Phy Stats */
4731 if (hw->phy.media_type == e1000_media_type_copper) {
4732 if ((adapter->link_speed == SPEED_1000) &&
Alexander Duyck73cd78f2009-02-12 18:16:59 +00004733 (!igb_read_phy_reg(hw, PHY_1000T_STATUS, &phy_tmp))) {
Auke Kok9d5c8242008-01-24 02:22:38 -08004734 phy_tmp &= PHY_IDLE_ERROR_COUNT_MASK;
4735 adapter->phy_stats.idle_errors += phy_tmp;
4736 }
4737 }
4738
4739 /* Management Stats */
4740 adapter->stats.mgptc += rd32(E1000_MGTPTC);
4741 adapter->stats.mgprc += rd32(E1000_MGTPRC);
4742 adapter->stats.mgpdc += rd32(E1000_MGTPDC);
Carolyn Wyborny0a915b92011-02-26 07:42:37 +00004743
4744 /* OS2BMC Stats */
4745 reg = rd32(E1000_MANC);
4746 if (reg & E1000_MANC_EN_BMC2OS) {
4747 adapter->stats.o2bgptc += rd32(E1000_O2BGPTC);
4748 adapter->stats.o2bspc += rd32(E1000_O2BSPC);
4749 adapter->stats.b2ospc += rd32(E1000_B2OSPC);
4750 adapter->stats.b2ogprc += rd32(E1000_B2OGPRC);
4751 }
Auke Kok9d5c8242008-01-24 02:22:38 -08004752}
4753
Auke Kok9d5c8242008-01-24 02:22:38 -08004754static irqreturn_t igb_msix_other(int irq, void *data)
4755{
Alexander Duyck047e0032009-10-27 15:49:27 +00004756 struct igb_adapter *adapter = data;
Auke Kok9d5c8242008-01-24 02:22:38 -08004757 struct e1000_hw *hw = &adapter->hw;
PJ Waskiewicz844290e2008-06-27 11:00:39 -07004758 u32 icr = rd32(E1000_ICR);
PJ Waskiewicz844290e2008-06-27 11:00:39 -07004759 /* reading ICR causes bit 31 of EICR to be cleared */
Alexander Duyckdda0e082009-02-06 23:19:08 +00004760
Alexander Duyck7f081d42010-01-07 17:41:00 +00004761 if (icr & E1000_ICR_DRSTA)
4762 schedule_work(&adapter->reset_task);
4763
Alexander Duyck047e0032009-10-27 15:49:27 +00004764 if (icr & E1000_ICR_DOUTSYNC) {
Alexander Duyckdda0e082009-02-06 23:19:08 +00004765 /* HW is reporting DMA is out of sync */
4766 adapter->stats.doosync++;
Greg Rose13800462010-11-06 02:08:26 +00004767 /* The DMA Out of Sync is also indication of a spoof event
4768 * in IOV mode. Check the Wrong VM Behavior register to
4769 * see if it is really a spoof event. */
4770 igb_check_wvbr(adapter);
Alexander Duyckdda0e082009-02-06 23:19:08 +00004771 }
Alexander Duyckeebbbdb2009-02-06 23:19:29 +00004772
Alexander Duyck4ae196d2009-02-19 20:40:07 -08004773 /* Check for a mailbox event */
4774 if (icr & E1000_ICR_VMMB)
4775 igb_msg_task(adapter);
4776
4777 if (icr & E1000_ICR_LSC) {
4778 hw->mac.get_link_status = 1;
4779 /* guard against interrupt when we're going down */
4780 if (!test_bit(__IGB_DOWN, &adapter->state))
4781 mod_timer(&adapter->watchdog_timer, jiffies + 1);
4782 }
4783
Matthew Vick1f6e8172012-08-18 07:26:33 +00004784 if (icr & E1000_ICR_TS) {
4785 u32 tsicr = rd32(E1000_TSICR);
4786
4787 if (tsicr & E1000_TSICR_TXTS) {
4788 /* acknowledge the interrupt */
4789 wr32(E1000_TSICR, E1000_TSICR_TXTS);
4790 /* retrieve hardware timestamp */
4791 schedule_work(&adapter->ptp_tx_work);
4792 }
4793 }
Matthew Vick1f6e8172012-08-18 07:26:33 +00004794
PJ Waskiewicz844290e2008-06-27 11:00:39 -07004795 wr32(E1000_EIMS, adapter->eims_other);
Auke Kok9d5c8242008-01-24 02:22:38 -08004796
4797 return IRQ_HANDLED;
4798}
4799
Alexander Duyck047e0032009-10-27 15:49:27 +00004800static void igb_write_itr(struct igb_q_vector *q_vector)
Auke Kok9d5c8242008-01-24 02:22:38 -08004801{
Alexander Duyck26b39272010-02-17 01:00:41 +00004802 struct igb_adapter *adapter = q_vector->adapter;
Alexander Duyck047e0032009-10-27 15:49:27 +00004803 u32 itr_val = q_vector->itr_val & 0x7FFC;
Auke Kok9d5c8242008-01-24 02:22:38 -08004804
Alexander Duyck047e0032009-10-27 15:49:27 +00004805 if (!q_vector->set_itr)
4806 return;
Alexander Duyck73cd78f2009-02-12 18:16:59 +00004807
Alexander Duyck047e0032009-10-27 15:49:27 +00004808 if (!itr_val)
4809 itr_val = 0x4;
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07004810
Alexander Duyck26b39272010-02-17 01:00:41 +00004811 if (adapter->hw.mac.type == e1000_82575)
4812 itr_val |= itr_val << 16;
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07004813 else
Alexander Duyck0ba82992011-08-26 07:45:47 +00004814 itr_val |= E1000_EITR_CNT_IGNR;
Alexander Duyck047e0032009-10-27 15:49:27 +00004815
4816 writel(itr_val, q_vector->itr_register);
4817 q_vector->set_itr = 0;
4818}
4819
4820static irqreturn_t igb_msix_ring(int irq, void *data)
4821{
4822 struct igb_q_vector *q_vector = data;
4823
4824 /* Write the ITR value calculated from the previous interrupt. */
4825 igb_write_itr(q_vector);
4826
4827 napi_schedule(&q_vector->napi);
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07004828
Auke Kok9d5c8242008-01-24 02:22:38 -08004829 return IRQ_HANDLED;
4830}
4831
Jeff Kirsher421e02f2008-10-17 11:08:31 -07004832#ifdef CONFIG_IGB_DCA
Alexander Duyck6a050042012-09-25 00:31:27 +00004833static void igb_update_tx_dca(struct igb_adapter *adapter,
4834 struct igb_ring *tx_ring,
4835 int cpu)
4836{
4837 struct e1000_hw *hw = &adapter->hw;
4838 u32 txctrl = dca3_get_tag(tx_ring->dev, cpu);
4839
4840 if (hw->mac.type != e1000_82575)
4841 txctrl <<= E1000_DCA_TXCTRL_CPUID_SHIFT;
4842
4843 /*
4844 * We can enable relaxed ordering for reads, but not writes when
4845 * DCA is enabled. This is due to a known issue in some chipsets
4846 * which will cause the DCA tag to be cleared.
4847 */
4848 txctrl |= E1000_DCA_TXCTRL_DESC_RRO_EN |
4849 E1000_DCA_TXCTRL_DATA_RRO_EN |
4850 E1000_DCA_TXCTRL_DESC_DCA_EN;
4851
4852 wr32(E1000_DCA_TXCTRL(tx_ring->reg_idx), txctrl);
4853}
4854
4855static void igb_update_rx_dca(struct igb_adapter *adapter,
4856 struct igb_ring *rx_ring,
4857 int cpu)
4858{
4859 struct e1000_hw *hw = &adapter->hw;
4860 u32 rxctrl = dca3_get_tag(&adapter->pdev->dev, cpu);
4861
4862 if (hw->mac.type != e1000_82575)
4863 rxctrl <<= E1000_DCA_RXCTRL_CPUID_SHIFT;
4864
4865 /*
4866 * We can enable relaxed ordering for reads, but not writes when
4867 * DCA is enabled. This is due to a known issue in some chipsets
4868 * which will cause the DCA tag to be cleared.
4869 */
4870 rxctrl |= E1000_DCA_RXCTRL_DESC_RRO_EN |
4871 E1000_DCA_RXCTRL_DESC_DCA_EN;
4872
4873 wr32(E1000_DCA_RXCTRL(rx_ring->reg_idx), rxctrl);
4874}
4875
Alexander Duyck047e0032009-10-27 15:49:27 +00004876static void igb_update_dca(struct igb_q_vector *q_vector)
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004877{
Alexander Duyck047e0032009-10-27 15:49:27 +00004878 struct igb_adapter *adapter = q_vector->adapter;
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004879 int cpu = get_cpu();
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004880
Alexander Duyck047e0032009-10-27 15:49:27 +00004881 if (q_vector->cpu == cpu)
4882 goto out_no_update;
4883
Alexander Duyck6a050042012-09-25 00:31:27 +00004884 if (q_vector->tx.ring)
4885 igb_update_tx_dca(adapter, q_vector->tx.ring, cpu);
4886
4887 if (q_vector->rx.ring)
4888 igb_update_rx_dca(adapter, q_vector->rx.ring, cpu);
4889
Alexander Duyck047e0032009-10-27 15:49:27 +00004890 q_vector->cpu = cpu;
4891out_no_update:
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004892 put_cpu();
4893}
4894
4895static void igb_setup_dca(struct igb_adapter *adapter)
4896{
Alexander Duyck7e0e99e2009-05-21 13:06:56 +00004897 struct e1000_hw *hw = &adapter->hw;
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004898 int i;
4899
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07004900 if (!(adapter->flags & IGB_FLAG_DCA_ENABLED))
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004901 return;
4902
Alexander Duyck7e0e99e2009-05-21 13:06:56 +00004903 /* Always use CB2 mode, difference is masked in the CB driver. */
4904 wr32(E1000_DCA_CTRL, E1000_DCA_CTRL_DCA_MODE_CB2);
4905
Alexander Duyck047e0032009-10-27 15:49:27 +00004906 for (i = 0; i < adapter->num_q_vectors; i++) {
Alexander Duyck26b39272010-02-17 01:00:41 +00004907 adapter->q_vector[i]->cpu = -1;
4908 igb_update_dca(adapter->q_vector[i]);
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004909 }
4910}
4911
4912static int __igb_notify_dca(struct device *dev, void *data)
4913{
4914 struct net_device *netdev = dev_get_drvdata(dev);
4915 struct igb_adapter *adapter = netdev_priv(netdev);
Alexander Duyck090b1792009-10-27 23:51:55 +00004916 struct pci_dev *pdev = adapter->pdev;
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004917 struct e1000_hw *hw = &adapter->hw;
4918 unsigned long event = *(unsigned long *)data;
4919
4920 switch (event) {
4921 case DCA_PROVIDER_ADD:
4922 /* if already enabled, don't do it again */
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07004923 if (adapter->flags & IGB_FLAG_DCA_ENABLED)
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004924 break;
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004925 if (dca_add_requester(dev) == 0) {
Alexander Duyckbbd98fe2009-01-31 00:52:30 -08004926 adapter->flags |= IGB_FLAG_DCA_ENABLED;
Alexander Duyck090b1792009-10-27 23:51:55 +00004927 dev_info(&pdev->dev, "DCA enabled\n");
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004928 igb_setup_dca(adapter);
4929 break;
4930 }
4931 /* Fall Through since DCA is disabled. */
4932 case DCA_PROVIDER_REMOVE:
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07004933 if (adapter->flags & IGB_FLAG_DCA_ENABLED) {
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004934 /* without this a class_device is left
Alexander Duyck047e0032009-10-27 15:49:27 +00004935 * hanging around in the sysfs model */
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004936 dca_remove_requester(dev);
Alexander Duyck090b1792009-10-27 23:51:55 +00004937 dev_info(&pdev->dev, "DCA disabled\n");
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07004938 adapter->flags &= ~IGB_FLAG_DCA_ENABLED;
Alexander Duyckcbd347a2009-02-15 23:59:44 -08004939 wr32(E1000_DCA_CTRL, E1000_DCA_CTRL_DCA_MODE_DISABLE);
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004940 }
4941 break;
4942 }
Alexander Duyckbbd98fe2009-01-31 00:52:30 -08004943
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004944 return 0;
4945}
4946
4947static int igb_notify_dca(struct notifier_block *nb, unsigned long event,
4948 void *p)
4949{
4950 int ret_val;
4951
4952 ret_val = driver_for_each_device(&igb_driver.driver, NULL, &event,
4953 __igb_notify_dca);
4954
4955 return ret_val ? NOTIFY_BAD : NOTIFY_DONE;
4956}
Jeff Kirsher421e02f2008-10-17 11:08:31 -07004957#endif /* CONFIG_IGB_DCA */
Auke Kok9d5c8242008-01-24 02:22:38 -08004958
Greg Rose0224d662011-10-14 02:57:14 +00004959#ifdef CONFIG_PCI_IOV
4960static int igb_vf_configure(struct igb_adapter *adapter, int vf)
4961{
4962 unsigned char mac_addr[ETH_ALEN];
Greg Rose0224d662011-10-14 02:57:14 +00004963
Joe Perches7efd26d2012-07-12 19:33:06 +00004964 eth_random_addr(mac_addr);
Greg Rose0224d662011-10-14 02:57:14 +00004965 igb_set_vf_mac(adapter, vf, mac_addr);
4966
Stefan Assmannf5571472012-08-18 04:06:11 +00004967 return 0;
Greg Rose0224d662011-10-14 02:57:14 +00004968}
4969
Stefan Assmannf5571472012-08-18 04:06:11 +00004970static bool igb_vfs_are_assigned(struct igb_adapter *adapter)
Greg Rose0224d662011-10-14 02:57:14 +00004971{
Greg Rose0224d662011-10-14 02:57:14 +00004972 struct pci_dev *pdev = adapter->pdev;
Stefan Assmannf5571472012-08-18 04:06:11 +00004973 struct pci_dev *vfdev;
4974 int dev_id;
Greg Rose0224d662011-10-14 02:57:14 +00004975
4976 switch (adapter->hw.mac.type) {
4977 case e1000_82576:
Stefan Assmannf5571472012-08-18 04:06:11 +00004978 dev_id = IGB_82576_VF_DEV_ID;
Greg Rose0224d662011-10-14 02:57:14 +00004979 break;
4980 case e1000_i350:
Stefan Assmannf5571472012-08-18 04:06:11 +00004981 dev_id = IGB_I350_VF_DEV_ID;
Greg Rose0224d662011-10-14 02:57:14 +00004982 break;
4983 default:
Stefan Assmannf5571472012-08-18 04:06:11 +00004984 return false;
Greg Rose0224d662011-10-14 02:57:14 +00004985 }
4986
Stefan Assmannf5571472012-08-18 04:06:11 +00004987 /* loop through all the VFs to see if we own any that are assigned */
4988 vfdev = pci_get_device(PCI_VENDOR_ID_INTEL, dev_id, NULL);
4989 while (vfdev) {
4990 /* if we don't own it we don't care */
4991 if (vfdev->is_virtfn && vfdev->physfn == pdev) {
4992 /* if it is assigned we cannot release it */
4993 if (vfdev->dev_flags & PCI_DEV_FLAGS_ASSIGNED)
Greg Rose0224d662011-10-14 02:57:14 +00004994 return true;
4995 }
Stefan Assmannf5571472012-08-18 04:06:11 +00004996
4997 vfdev = pci_get_device(PCI_VENDOR_ID_INTEL, dev_id, vfdev);
Greg Rose0224d662011-10-14 02:57:14 +00004998 }
Stefan Assmannf5571472012-08-18 04:06:11 +00004999
Greg Rose0224d662011-10-14 02:57:14 +00005000 return false;
5001}
5002
5003#endif
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005004static void igb_ping_all_vfs(struct igb_adapter *adapter)
5005{
5006 struct e1000_hw *hw = &adapter->hw;
5007 u32 ping;
5008 int i;
5009
5010 for (i = 0 ; i < adapter->vfs_allocated_count; i++) {
5011 ping = E1000_PF_CONTROL_MSG;
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005012 if (adapter->vf_data[i].flags & IGB_VF_FLAG_CTS)
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005013 ping |= E1000_VT_MSGTYPE_CTS;
5014 igb_write_mbx(hw, &ping, 1, i);
5015 }
5016}
5017
Alexander Duyck7d5753f2009-10-27 23:47:16 +00005018static int igb_set_vf_promisc(struct igb_adapter *adapter, u32 *msgbuf, u32 vf)
5019{
5020 struct e1000_hw *hw = &adapter->hw;
5021 u32 vmolr = rd32(E1000_VMOLR(vf));
5022 struct vf_data_storage *vf_data = &adapter->vf_data[vf];
5023
Alexander Duyckd85b90042010-09-22 17:56:20 +00005024 vf_data->flags &= ~(IGB_VF_FLAG_UNI_PROMISC |
Alexander Duyck7d5753f2009-10-27 23:47:16 +00005025 IGB_VF_FLAG_MULTI_PROMISC);
5026 vmolr &= ~(E1000_VMOLR_ROPE | E1000_VMOLR_ROMPE | E1000_VMOLR_MPME);
5027
5028 if (*msgbuf & E1000_VF_SET_PROMISC_MULTICAST) {
5029 vmolr |= E1000_VMOLR_MPME;
Alexander Duyckd85b90042010-09-22 17:56:20 +00005030 vf_data->flags |= IGB_VF_FLAG_MULTI_PROMISC;
Alexander Duyck7d5753f2009-10-27 23:47:16 +00005031 *msgbuf &= ~E1000_VF_SET_PROMISC_MULTICAST;
5032 } else {
5033 /*
5034 * if we have hashes and we are clearing a multicast promisc
5035 * flag we need to write the hashes to the MTA as this step
5036 * was previously skipped
5037 */
5038 if (vf_data->num_vf_mc_hashes > 30) {
5039 vmolr |= E1000_VMOLR_MPME;
5040 } else if (vf_data->num_vf_mc_hashes) {
5041 int j;
5042 vmolr |= E1000_VMOLR_ROMPE;
5043 for (j = 0; j < vf_data->num_vf_mc_hashes; j++)
5044 igb_mta_set(hw, vf_data->vf_mc_hashes[j]);
5045 }
5046 }
5047
5048 wr32(E1000_VMOLR(vf), vmolr);
5049
5050 /* there are flags left unprocessed, likely not supported */
5051 if (*msgbuf & E1000_VT_MSGINFO_MASK)
5052 return -EINVAL;
5053
5054 return 0;
5055
5056}
5057
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005058static int igb_set_vf_multicasts(struct igb_adapter *adapter,
5059 u32 *msgbuf, u32 vf)
5060{
5061 int n = (msgbuf[0] & E1000_VT_MSGINFO_MASK) >> E1000_VT_MSGINFO_SHIFT;
5062 u16 *hash_list = (u16 *)&msgbuf[1];
5063 struct vf_data_storage *vf_data = &adapter->vf_data[vf];
5064 int i;
5065
Alexander Duyck7d5753f2009-10-27 23:47:16 +00005066 /* salt away the number of multicast addresses assigned
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005067 * to this VF for later use to restore when the PF multi cast
5068 * list changes
5069 */
5070 vf_data->num_vf_mc_hashes = n;
5071
Alexander Duyck7d5753f2009-10-27 23:47:16 +00005072 /* only up to 30 hash values supported */
5073 if (n > 30)
5074 n = 30;
5075
5076 /* store the hashes for later use */
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005077 for (i = 0; i < n; i++)
Joe Perchesa419aef2009-08-18 11:18:35 -07005078 vf_data->vf_mc_hashes[i] = hash_list[i];
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005079
5080 /* Flush and reset the mta with the new values */
Alexander Duyckff41f8d2009-09-03 14:48:56 +00005081 igb_set_rx_mode(adapter->netdev);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005082
5083 return 0;
5084}
5085
5086static void igb_restore_vf_multicasts(struct igb_adapter *adapter)
5087{
5088 struct e1000_hw *hw = &adapter->hw;
5089 struct vf_data_storage *vf_data;
5090 int i, j;
5091
5092 for (i = 0; i < adapter->vfs_allocated_count; i++) {
Alexander Duyck7d5753f2009-10-27 23:47:16 +00005093 u32 vmolr = rd32(E1000_VMOLR(i));
5094 vmolr &= ~(E1000_VMOLR_ROMPE | E1000_VMOLR_MPME);
5095
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005096 vf_data = &adapter->vf_data[i];
Alexander Duyck7d5753f2009-10-27 23:47:16 +00005097
5098 if ((vf_data->num_vf_mc_hashes > 30) ||
5099 (vf_data->flags & IGB_VF_FLAG_MULTI_PROMISC)) {
5100 vmolr |= E1000_VMOLR_MPME;
5101 } else if (vf_data->num_vf_mc_hashes) {
5102 vmolr |= E1000_VMOLR_ROMPE;
5103 for (j = 0; j < vf_data->num_vf_mc_hashes; j++)
5104 igb_mta_set(hw, vf_data->vf_mc_hashes[j]);
5105 }
5106 wr32(E1000_VMOLR(i), vmolr);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005107 }
5108}
5109
5110static void igb_clear_vf_vfta(struct igb_adapter *adapter, u32 vf)
5111{
5112 struct e1000_hw *hw = &adapter->hw;
5113 u32 pool_mask, reg, vid;
5114 int i;
5115
5116 pool_mask = 1 << (E1000_VLVF_POOLSEL_SHIFT + vf);
5117
5118 /* Find the vlan filter for this id */
5119 for (i = 0; i < E1000_VLVF_ARRAY_SIZE; i++) {
5120 reg = rd32(E1000_VLVF(i));
5121
5122 /* remove the vf from the pool */
5123 reg &= ~pool_mask;
5124
5125 /* if pool is empty then remove entry from vfta */
5126 if (!(reg & E1000_VLVF_POOLSEL_MASK) &&
5127 (reg & E1000_VLVF_VLANID_ENABLE)) {
5128 reg = 0;
5129 vid = reg & E1000_VLVF_VLANID_MASK;
5130 igb_vfta_set(hw, vid, false);
5131 }
5132
5133 wr32(E1000_VLVF(i), reg);
5134 }
Alexander Duyckae641bd2009-09-03 14:49:33 +00005135
5136 adapter->vf_data[vf].vlans_enabled = 0;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005137}
5138
5139static s32 igb_vlvf_set(struct igb_adapter *adapter, u32 vid, bool add, u32 vf)
5140{
5141 struct e1000_hw *hw = &adapter->hw;
5142 u32 reg, i;
5143
Alexander Duyck51466232009-10-27 23:47:35 +00005144 /* The vlvf table only exists on 82576 hardware and newer */
5145 if (hw->mac.type < e1000_82576)
5146 return -1;
5147
5148 /* we only need to do this if VMDq is enabled */
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005149 if (!adapter->vfs_allocated_count)
5150 return -1;
5151
5152 /* Find the vlan filter for this id */
5153 for (i = 0; i < E1000_VLVF_ARRAY_SIZE; i++) {
5154 reg = rd32(E1000_VLVF(i));
5155 if ((reg & E1000_VLVF_VLANID_ENABLE) &&
5156 vid == (reg & E1000_VLVF_VLANID_MASK))
5157 break;
5158 }
5159
5160 if (add) {
5161 if (i == E1000_VLVF_ARRAY_SIZE) {
5162 /* Did not find a matching VLAN ID entry that was
5163 * enabled. Search for a free filter entry, i.e.
5164 * one without the enable bit set
5165 */
5166 for (i = 0; i < E1000_VLVF_ARRAY_SIZE; i++) {
5167 reg = rd32(E1000_VLVF(i));
5168 if (!(reg & E1000_VLVF_VLANID_ENABLE))
5169 break;
5170 }
5171 }
5172 if (i < E1000_VLVF_ARRAY_SIZE) {
5173 /* Found an enabled/available entry */
5174 reg |= 1 << (E1000_VLVF_POOLSEL_SHIFT + vf);
5175
5176 /* if !enabled we need to set this up in vfta */
5177 if (!(reg & E1000_VLVF_VLANID_ENABLE)) {
Alexander Duyck51466232009-10-27 23:47:35 +00005178 /* add VID to filter table */
5179 igb_vfta_set(hw, vid, true);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005180 reg |= E1000_VLVF_VLANID_ENABLE;
5181 }
Alexander Duyckcad6d052009-03-13 20:41:37 +00005182 reg &= ~E1000_VLVF_VLANID_MASK;
5183 reg |= vid;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005184 wr32(E1000_VLVF(i), reg);
Alexander Duyckae641bd2009-09-03 14:49:33 +00005185
5186 /* do not modify RLPML for PF devices */
5187 if (vf >= adapter->vfs_allocated_count)
5188 return 0;
5189
5190 if (!adapter->vf_data[vf].vlans_enabled) {
5191 u32 size;
5192 reg = rd32(E1000_VMOLR(vf));
5193 size = reg & E1000_VMOLR_RLPML_MASK;
5194 size += 4;
5195 reg &= ~E1000_VMOLR_RLPML_MASK;
5196 reg |= size;
5197 wr32(E1000_VMOLR(vf), reg);
5198 }
Alexander Duyckae641bd2009-09-03 14:49:33 +00005199
Alexander Duyck51466232009-10-27 23:47:35 +00005200 adapter->vf_data[vf].vlans_enabled++;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005201 }
5202 } else {
5203 if (i < E1000_VLVF_ARRAY_SIZE) {
5204 /* remove vf from the pool */
5205 reg &= ~(1 << (E1000_VLVF_POOLSEL_SHIFT + vf));
5206 /* if pool is empty then remove entry from vfta */
5207 if (!(reg & E1000_VLVF_POOLSEL_MASK)) {
5208 reg = 0;
5209 igb_vfta_set(hw, vid, false);
5210 }
5211 wr32(E1000_VLVF(i), reg);
Alexander Duyckae641bd2009-09-03 14:49:33 +00005212
5213 /* do not modify RLPML for PF devices */
5214 if (vf >= adapter->vfs_allocated_count)
5215 return 0;
5216
5217 adapter->vf_data[vf].vlans_enabled--;
5218 if (!adapter->vf_data[vf].vlans_enabled) {
5219 u32 size;
5220 reg = rd32(E1000_VMOLR(vf));
5221 size = reg & E1000_VMOLR_RLPML_MASK;
5222 size -= 4;
5223 reg &= ~E1000_VMOLR_RLPML_MASK;
5224 reg |= size;
5225 wr32(E1000_VMOLR(vf), reg);
5226 }
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005227 }
5228 }
Williams, Mitch A8151d292010-02-10 01:44:24 +00005229 return 0;
5230}
5231
5232static void igb_set_vmvir(struct igb_adapter *adapter, u32 vid, u32 vf)
5233{
5234 struct e1000_hw *hw = &adapter->hw;
5235
5236 if (vid)
5237 wr32(E1000_VMVIR(vf), (vid | E1000_VMVIR_VLANA_DEFAULT));
5238 else
5239 wr32(E1000_VMVIR(vf), 0);
5240}
5241
5242static int igb_ndo_set_vf_vlan(struct net_device *netdev,
5243 int vf, u16 vlan, u8 qos)
5244{
5245 int err = 0;
5246 struct igb_adapter *adapter = netdev_priv(netdev);
5247
5248 if ((vf >= adapter->vfs_allocated_count) || (vlan > 4095) || (qos > 7))
5249 return -EINVAL;
5250 if (vlan || qos) {
5251 err = igb_vlvf_set(adapter, vlan, !!vlan, vf);
5252 if (err)
5253 goto out;
5254 igb_set_vmvir(adapter, vlan | (qos << VLAN_PRIO_SHIFT), vf);
5255 igb_set_vmolr(adapter, vf, !vlan);
5256 adapter->vf_data[vf].pf_vlan = vlan;
5257 adapter->vf_data[vf].pf_qos = qos;
5258 dev_info(&adapter->pdev->dev,
5259 "Setting VLAN %d, QOS 0x%x on VF %d\n", vlan, qos, vf);
5260 if (test_bit(__IGB_DOWN, &adapter->state)) {
5261 dev_warn(&adapter->pdev->dev,
5262 "The VF VLAN has been set,"
5263 " but the PF device is not up.\n");
5264 dev_warn(&adapter->pdev->dev,
5265 "Bring the PF device up before"
5266 " attempting to use the VF device.\n");
5267 }
5268 } else {
5269 igb_vlvf_set(adapter, adapter->vf_data[vf].pf_vlan,
5270 false, vf);
5271 igb_set_vmvir(adapter, vlan, vf);
5272 igb_set_vmolr(adapter, vf, true);
5273 adapter->vf_data[vf].pf_vlan = 0;
5274 adapter->vf_data[vf].pf_qos = 0;
5275 }
5276out:
5277 return err;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005278}
5279
5280static int igb_set_vf_vlan(struct igb_adapter *adapter, u32 *msgbuf, u32 vf)
5281{
5282 int add = (msgbuf[0] & E1000_VT_MSGINFO_MASK) >> E1000_VT_MSGINFO_SHIFT;
5283 int vid = (msgbuf[1] & E1000_VLVF_VLANID_MASK);
5284
5285 return igb_vlvf_set(adapter, vid, add, vf);
5286}
5287
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005288static inline void igb_vf_reset(struct igb_adapter *adapter, u32 vf)
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005289{
Greg Rose8fa7e0f2010-11-06 05:43:21 +00005290 /* clear flags - except flag that indicates PF has set the MAC */
5291 adapter->vf_data[vf].flags &= IGB_VF_FLAG_PF_SET_MAC;
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005292 adapter->vf_data[vf].last_nack = jiffies;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005293
5294 /* reset offloads to defaults */
Williams, Mitch A8151d292010-02-10 01:44:24 +00005295 igb_set_vmolr(adapter, vf, true);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005296
5297 /* reset vlans for device */
5298 igb_clear_vf_vfta(adapter, vf);
Williams, Mitch A8151d292010-02-10 01:44:24 +00005299 if (adapter->vf_data[vf].pf_vlan)
5300 igb_ndo_set_vf_vlan(adapter->netdev, vf,
5301 adapter->vf_data[vf].pf_vlan,
5302 adapter->vf_data[vf].pf_qos);
5303 else
5304 igb_clear_vf_vfta(adapter, vf);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005305
5306 /* reset multicast table array for vf */
5307 adapter->vf_data[vf].num_vf_mc_hashes = 0;
5308
5309 /* Flush and reset the mta with the new values */
Alexander Duyckff41f8d2009-09-03 14:48:56 +00005310 igb_set_rx_mode(adapter->netdev);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005311}
5312
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005313static void igb_vf_reset_event(struct igb_adapter *adapter, u32 vf)
5314{
5315 unsigned char *vf_mac = adapter->vf_data[vf].vf_mac_addresses;
5316
5317 /* generate a new mac address as we were hotplug removed/added */
Williams, Mitch A8151d292010-02-10 01:44:24 +00005318 if (!(adapter->vf_data[vf].flags & IGB_VF_FLAG_PF_SET_MAC))
Joe Perches7efd26d2012-07-12 19:33:06 +00005319 eth_random_addr(vf_mac);
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005320
5321 /* process remaining reset events */
5322 igb_vf_reset(adapter, vf);
5323}
5324
5325static void igb_vf_reset_msg(struct igb_adapter *adapter, u32 vf)
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005326{
5327 struct e1000_hw *hw = &adapter->hw;
5328 unsigned char *vf_mac = adapter->vf_data[vf].vf_mac_addresses;
Alexander Duyckff41f8d2009-09-03 14:48:56 +00005329 int rar_entry = hw->mac.rar_entry_count - (vf + 1);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005330 u32 reg, msgbuf[3];
5331 u8 *addr = (u8 *)(&msgbuf[1]);
5332
5333 /* process all the same items cleared in a function level reset */
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005334 igb_vf_reset(adapter, vf);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005335
5336 /* set vf mac address */
Alexander Duyck26ad9172009-10-05 06:32:49 +00005337 igb_rar_set_qsel(adapter, vf_mac, rar_entry, vf);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005338
5339 /* enable transmit and receive for vf */
5340 reg = rd32(E1000_VFTE);
5341 wr32(E1000_VFTE, reg | (1 << vf));
5342 reg = rd32(E1000_VFRE);
5343 wr32(E1000_VFRE, reg | (1 << vf));
5344
Greg Rose8fa7e0f2010-11-06 05:43:21 +00005345 adapter->vf_data[vf].flags |= IGB_VF_FLAG_CTS;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005346
5347 /* reply to reset with ack and vf mac address */
5348 msgbuf[0] = E1000_VF_RESET | E1000_VT_MSGTYPE_ACK;
5349 memcpy(addr, vf_mac, 6);
5350 igb_write_mbx(hw, msgbuf, 3, vf);
5351}
5352
5353static int igb_set_vf_mac_addr(struct igb_adapter *adapter, u32 *msg, int vf)
5354{
Greg Rosede42edd2010-07-01 13:39:23 +00005355 /*
5356 * The VF MAC Address is stored in a packed array of bytes
5357 * starting at the second 32 bit word of the msg array
5358 */
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005359 unsigned char *addr = (char *)&msg[1];
5360 int err = -1;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005361
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005362 if (is_valid_ether_addr(addr))
5363 err = igb_set_vf_mac(adapter, vf, addr);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005364
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005365 return err;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005366}
5367
5368static void igb_rcv_ack_from_vf(struct igb_adapter *adapter, u32 vf)
5369{
5370 struct e1000_hw *hw = &adapter->hw;
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005371 struct vf_data_storage *vf_data = &adapter->vf_data[vf];
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005372 u32 msg = E1000_VT_MSGTYPE_NACK;
5373
5374 /* if device isn't clear to send it shouldn't be reading either */
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005375 if (!(vf_data->flags & IGB_VF_FLAG_CTS) &&
5376 time_after(jiffies, vf_data->last_nack + (2 * HZ))) {
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005377 igb_write_mbx(hw, &msg, 1, vf);
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005378 vf_data->last_nack = jiffies;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005379 }
5380}
5381
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005382static void igb_rcv_msg_from_vf(struct igb_adapter *adapter, u32 vf)
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005383{
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005384 struct pci_dev *pdev = adapter->pdev;
5385 u32 msgbuf[E1000_VFMAILBOX_SIZE];
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005386 struct e1000_hw *hw = &adapter->hw;
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005387 struct vf_data_storage *vf_data = &adapter->vf_data[vf];
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005388 s32 retval;
5389
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005390 retval = igb_read_mbx(hw, msgbuf, E1000_VFMAILBOX_SIZE, vf);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005391
Alexander Duyckfef45f42009-12-11 22:57:34 -08005392 if (retval) {
5393 /* if receive failed revoke VF CTS stats and restart init */
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005394 dev_err(&pdev->dev, "Error receiving message from VF\n");
Alexander Duyckfef45f42009-12-11 22:57:34 -08005395 vf_data->flags &= ~IGB_VF_FLAG_CTS;
5396 if (!time_after(jiffies, vf_data->last_nack + (2 * HZ)))
5397 return;
5398 goto out;
5399 }
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005400
5401 /* this is a message we already processed, do nothing */
5402 if (msgbuf[0] & (E1000_VT_MSGTYPE_ACK | E1000_VT_MSGTYPE_NACK))
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005403 return;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005404
5405 /*
5406 * until the vf completes a reset it should not be
5407 * allowed to start any configuration.
5408 */
5409
5410 if (msgbuf[0] == E1000_VF_RESET) {
5411 igb_vf_reset_msg(adapter, vf);
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005412 return;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005413 }
5414
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005415 if (!(vf_data->flags & IGB_VF_FLAG_CTS)) {
Alexander Duyckfef45f42009-12-11 22:57:34 -08005416 if (!time_after(jiffies, vf_data->last_nack + (2 * HZ)))
5417 return;
5418 retval = -1;
5419 goto out;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005420 }
5421
5422 switch ((msgbuf[0] & 0xFFFF)) {
5423 case E1000_VF_SET_MAC_ADDR:
Greg Rosea6b5ea32010-11-06 05:42:59 +00005424 retval = -EINVAL;
5425 if (!(vf_data->flags & IGB_VF_FLAG_PF_SET_MAC))
5426 retval = igb_set_vf_mac_addr(adapter, msgbuf, vf);
5427 else
5428 dev_warn(&pdev->dev,
5429 "VF %d attempted to override administratively "
5430 "set MAC address\nReload the VF driver to "
5431 "resume operations\n", vf);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005432 break;
Alexander Duyck7d5753f2009-10-27 23:47:16 +00005433 case E1000_VF_SET_PROMISC:
5434 retval = igb_set_vf_promisc(adapter, msgbuf, vf);
5435 break;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005436 case E1000_VF_SET_MULTICAST:
5437 retval = igb_set_vf_multicasts(adapter, msgbuf, vf);
5438 break;
5439 case E1000_VF_SET_LPE:
5440 retval = igb_set_vf_rlpml(adapter, msgbuf[1], vf);
5441 break;
5442 case E1000_VF_SET_VLAN:
Greg Rosea6b5ea32010-11-06 05:42:59 +00005443 retval = -1;
5444 if (vf_data->pf_vlan)
5445 dev_warn(&pdev->dev,
5446 "VF %d attempted to override administratively "
5447 "set VLAN tag\nReload the VF driver to "
5448 "resume operations\n", vf);
Williams, Mitch A8151d292010-02-10 01:44:24 +00005449 else
5450 retval = igb_set_vf_vlan(adapter, msgbuf, vf);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005451 break;
5452 default:
Alexander Duyck090b1792009-10-27 23:51:55 +00005453 dev_err(&pdev->dev, "Unhandled Msg %08x\n", msgbuf[0]);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005454 retval = -1;
5455 break;
5456 }
5457
Alexander Duyckfef45f42009-12-11 22:57:34 -08005458 msgbuf[0] |= E1000_VT_MSGTYPE_CTS;
5459out:
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005460 /* notify the VF of the results of what it sent us */
5461 if (retval)
5462 msgbuf[0] |= E1000_VT_MSGTYPE_NACK;
5463 else
5464 msgbuf[0] |= E1000_VT_MSGTYPE_ACK;
5465
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005466 igb_write_mbx(hw, msgbuf, 1, vf);
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005467}
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005468
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005469static void igb_msg_task(struct igb_adapter *adapter)
5470{
5471 struct e1000_hw *hw = &adapter->hw;
5472 u32 vf;
5473
5474 for (vf = 0; vf < adapter->vfs_allocated_count; vf++) {
5475 /* process any reset requests */
5476 if (!igb_check_for_rst(hw, vf))
5477 igb_vf_reset_event(adapter, vf);
5478
5479 /* process any messages pending */
5480 if (!igb_check_for_msg(hw, vf))
5481 igb_rcv_msg_from_vf(adapter, vf);
5482
5483 /* process any acks */
5484 if (!igb_check_for_ack(hw, vf))
5485 igb_rcv_ack_from_vf(adapter, vf);
5486 }
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005487}
5488
Auke Kok9d5c8242008-01-24 02:22:38 -08005489/**
Alexander Duyck68d480c2009-10-05 06:33:08 +00005490 * igb_set_uta - Set unicast filter table address
5491 * @adapter: board private structure
5492 *
5493 * The unicast table address is a register array of 32-bit registers.
5494 * The table is meant to be used in a way similar to how the MTA is used
5495 * however due to certain limitations in the hardware it is necessary to
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005496 * set all the hash bits to 1 and use the VMOLR ROPE bit as a promiscuous
5497 * enable bit to allow vlan tag stripping when promiscuous mode is enabled
Alexander Duyck68d480c2009-10-05 06:33:08 +00005498 **/
5499static void igb_set_uta(struct igb_adapter *adapter)
5500{
5501 struct e1000_hw *hw = &adapter->hw;
5502 int i;
5503
5504 /* The UTA table only exists on 82576 hardware and newer */
5505 if (hw->mac.type < e1000_82576)
5506 return;
5507
5508 /* we only need to do this if VMDq is enabled */
5509 if (!adapter->vfs_allocated_count)
5510 return;
5511
5512 for (i = 0; i < hw->mac.uta_reg_count; i++)
5513 array_wr32(E1000_UTA, i, ~0);
5514}
5515
5516/**
Auke Kok9d5c8242008-01-24 02:22:38 -08005517 * igb_intr_msi - Interrupt Handler
5518 * @irq: interrupt number
5519 * @data: pointer to a network interface device structure
5520 **/
5521static irqreturn_t igb_intr_msi(int irq, void *data)
5522{
Alexander Duyck047e0032009-10-27 15:49:27 +00005523 struct igb_adapter *adapter = data;
5524 struct igb_q_vector *q_vector = adapter->q_vector[0];
Auke Kok9d5c8242008-01-24 02:22:38 -08005525 struct e1000_hw *hw = &adapter->hw;
5526 /* read ICR disables interrupts using IAM */
5527 u32 icr = rd32(E1000_ICR);
5528
Alexander Duyck047e0032009-10-27 15:49:27 +00005529 igb_write_itr(q_vector);
Auke Kok9d5c8242008-01-24 02:22:38 -08005530
Alexander Duyck7f081d42010-01-07 17:41:00 +00005531 if (icr & E1000_ICR_DRSTA)
5532 schedule_work(&adapter->reset_task);
5533
Alexander Duyck047e0032009-10-27 15:49:27 +00005534 if (icr & E1000_ICR_DOUTSYNC) {
Alexander Duyckdda0e082009-02-06 23:19:08 +00005535 /* HW is reporting DMA is out of sync */
5536 adapter->stats.doosync++;
5537 }
5538
Auke Kok9d5c8242008-01-24 02:22:38 -08005539 if (icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) {
5540 hw->mac.get_link_status = 1;
5541 if (!test_bit(__IGB_DOWN, &adapter->state))
5542 mod_timer(&adapter->watchdog_timer, jiffies + 1);
5543 }
5544
Matthew Vick1f6e8172012-08-18 07:26:33 +00005545 if (icr & E1000_ICR_TS) {
5546 u32 tsicr = rd32(E1000_TSICR);
5547
5548 if (tsicr & E1000_TSICR_TXTS) {
5549 /* acknowledge the interrupt */
5550 wr32(E1000_TSICR, E1000_TSICR_TXTS);
5551 /* retrieve hardware timestamp */
5552 schedule_work(&adapter->ptp_tx_work);
5553 }
5554 }
Matthew Vick1f6e8172012-08-18 07:26:33 +00005555
Alexander Duyck047e0032009-10-27 15:49:27 +00005556 napi_schedule(&q_vector->napi);
Auke Kok9d5c8242008-01-24 02:22:38 -08005557
5558 return IRQ_HANDLED;
5559}
5560
5561/**
Alexander Duyck4a3c6432009-02-06 23:20:49 +00005562 * igb_intr - Legacy Interrupt Handler
Auke Kok9d5c8242008-01-24 02:22:38 -08005563 * @irq: interrupt number
5564 * @data: pointer to a network interface device structure
5565 **/
5566static irqreturn_t igb_intr(int irq, void *data)
5567{
Alexander Duyck047e0032009-10-27 15:49:27 +00005568 struct igb_adapter *adapter = data;
5569 struct igb_q_vector *q_vector = adapter->q_vector[0];
Auke Kok9d5c8242008-01-24 02:22:38 -08005570 struct e1000_hw *hw = &adapter->hw;
5571 /* Interrupt Auto-Mask...upon reading ICR, interrupts are masked. No
5572 * need for the IMC write */
5573 u32 icr = rd32(E1000_ICR);
Auke Kok9d5c8242008-01-24 02:22:38 -08005574
5575 /* IMS will not auto-mask if INT_ASSERTED is not set, and if it is
5576 * not set, then the adapter didn't send an interrupt */
5577 if (!(icr & E1000_ICR_INT_ASSERTED))
5578 return IRQ_NONE;
5579
Alexander Duyck0ba82992011-08-26 07:45:47 +00005580 igb_write_itr(q_vector);
5581
Alexander Duyck7f081d42010-01-07 17:41:00 +00005582 if (icr & E1000_ICR_DRSTA)
5583 schedule_work(&adapter->reset_task);
5584
Alexander Duyck047e0032009-10-27 15:49:27 +00005585 if (icr & E1000_ICR_DOUTSYNC) {
Alexander Duyckdda0e082009-02-06 23:19:08 +00005586 /* HW is reporting DMA is out of sync */
5587 adapter->stats.doosync++;
5588 }
5589
Auke Kok9d5c8242008-01-24 02:22:38 -08005590 if (icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) {
5591 hw->mac.get_link_status = 1;
5592 /* guard against interrupt when we're going down */
5593 if (!test_bit(__IGB_DOWN, &adapter->state))
5594 mod_timer(&adapter->watchdog_timer, jiffies + 1);
5595 }
5596
Matthew Vick1f6e8172012-08-18 07:26:33 +00005597 if (icr & E1000_ICR_TS) {
5598 u32 tsicr = rd32(E1000_TSICR);
5599
5600 if (tsicr & E1000_TSICR_TXTS) {
5601 /* acknowledge the interrupt */
5602 wr32(E1000_TSICR, E1000_TSICR_TXTS);
5603 /* retrieve hardware timestamp */
5604 schedule_work(&adapter->ptp_tx_work);
5605 }
5606 }
Matthew Vick1f6e8172012-08-18 07:26:33 +00005607
Alexander Duyck047e0032009-10-27 15:49:27 +00005608 napi_schedule(&q_vector->napi);
Auke Kok9d5c8242008-01-24 02:22:38 -08005609
5610 return IRQ_HANDLED;
5611}
5612
Stephen Hemmingerc50b52a2012-01-18 22:13:26 +00005613static void igb_ring_irq_enable(struct igb_q_vector *q_vector)
Alexander Duyck46544252009-02-19 20:39:04 -08005614{
Alexander Duyck047e0032009-10-27 15:49:27 +00005615 struct igb_adapter *adapter = q_vector->adapter;
Alexander Duyck46544252009-02-19 20:39:04 -08005616 struct e1000_hw *hw = &adapter->hw;
5617
Alexander Duyck0ba82992011-08-26 07:45:47 +00005618 if ((q_vector->rx.ring && (adapter->rx_itr_setting & 3)) ||
5619 (!q_vector->rx.ring && (adapter->tx_itr_setting & 3))) {
5620 if ((adapter->num_q_vectors == 1) && !adapter->vf_data)
5621 igb_set_itr(q_vector);
Alexander Duyck46544252009-02-19 20:39:04 -08005622 else
Alexander Duyck047e0032009-10-27 15:49:27 +00005623 igb_update_ring_itr(q_vector);
Alexander Duyck46544252009-02-19 20:39:04 -08005624 }
5625
5626 if (!test_bit(__IGB_DOWN, &adapter->state)) {
5627 if (adapter->msix_entries)
Alexander Duyck047e0032009-10-27 15:49:27 +00005628 wr32(E1000_EIMS, q_vector->eims_value);
Alexander Duyck46544252009-02-19 20:39:04 -08005629 else
5630 igb_irq_enable(adapter);
5631 }
5632}
5633
Auke Kok9d5c8242008-01-24 02:22:38 -08005634/**
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07005635 * igb_poll - NAPI Rx polling callback
5636 * @napi: napi polling structure
5637 * @budget: count of how many packets we should handle
Auke Kok9d5c8242008-01-24 02:22:38 -08005638 **/
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07005639static int igb_poll(struct napi_struct *napi, int budget)
Auke Kok9d5c8242008-01-24 02:22:38 -08005640{
Alexander Duyck047e0032009-10-27 15:49:27 +00005641 struct igb_q_vector *q_vector = container_of(napi,
5642 struct igb_q_vector,
5643 napi);
Alexander Duyck16eb8812011-08-26 07:43:54 +00005644 bool clean_complete = true;
Auke Kok9d5c8242008-01-24 02:22:38 -08005645
Jeff Kirsher421e02f2008-10-17 11:08:31 -07005646#ifdef CONFIG_IGB_DCA
Alexander Duyck047e0032009-10-27 15:49:27 +00005647 if (q_vector->adapter->flags & IGB_FLAG_DCA_ENABLED)
5648 igb_update_dca(q_vector);
Jeb Cramerfe4506b2008-07-08 15:07:55 -07005649#endif
Alexander Duyck0ba82992011-08-26 07:45:47 +00005650 if (q_vector->tx.ring)
Alexander Duyck13fde972011-10-05 13:35:24 +00005651 clean_complete = igb_clean_tx_irq(q_vector);
Auke Kok9d5c8242008-01-24 02:22:38 -08005652
Alexander Duyck0ba82992011-08-26 07:45:47 +00005653 if (q_vector->rx.ring)
Alexander Duyckcd392f52011-08-26 07:43:59 +00005654 clean_complete &= igb_clean_rx_irq(q_vector, budget);
Alexander Duyck047e0032009-10-27 15:49:27 +00005655
Alexander Duyck16eb8812011-08-26 07:43:54 +00005656 /* If all work not completed, return budget and keep polling */
5657 if (!clean_complete)
5658 return budget;
Auke Kok9d5c8242008-01-24 02:22:38 -08005659
Alexander Duyck46544252009-02-19 20:39:04 -08005660 /* If not enough Rx work done, exit the polling mode */
Alexander Duyck16eb8812011-08-26 07:43:54 +00005661 napi_complete(napi);
5662 igb_ring_irq_enable(q_vector);
Alexander Duyck46544252009-02-19 20:39:04 -08005663
Alexander Duyck16eb8812011-08-26 07:43:54 +00005664 return 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08005665}
Al Viro6d8126f2008-03-16 22:23:24 +00005666
Patrick Ohly33af6bc2009-02-12 05:03:43 +00005667/**
Auke Kok9d5c8242008-01-24 02:22:38 -08005668 * igb_clean_tx_irq - Reclaim resources after transmit completes
Alexander Duyck047e0032009-10-27 15:49:27 +00005669 * @q_vector: pointer to q_vector containing needed info
Ben Hutchings49ce9c22012-07-10 10:56:00 +00005670 *
Auke Kok9d5c8242008-01-24 02:22:38 -08005671 * returns true if ring is completely cleaned
5672 **/
Alexander Duyck047e0032009-10-27 15:49:27 +00005673static bool igb_clean_tx_irq(struct igb_q_vector *q_vector)
Auke Kok9d5c8242008-01-24 02:22:38 -08005674{
Alexander Duyck047e0032009-10-27 15:49:27 +00005675 struct igb_adapter *adapter = q_vector->adapter;
Alexander Duyck0ba82992011-08-26 07:45:47 +00005676 struct igb_ring *tx_ring = q_vector->tx.ring;
Alexander Duyck06034642011-08-26 07:44:22 +00005677 struct igb_tx_buffer *tx_buffer;
Alexander Duyckf4128782012-09-13 06:28:01 +00005678 union e1000_adv_tx_desc *tx_desc;
Auke Kok9d5c8242008-01-24 02:22:38 -08005679 unsigned int total_bytes = 0, total_packets = 0;
Alexander Duyck0ba82992011-08-26 07:45:47 +00005680 unsigned int budget = q_vector->tx.work_limit;
Alexander Duyck8542db02011-08-26 07:44:43 +00005681 unsigned int i = tx_ring->next_to_clean;
Auke Kok9d5c8242008-01-24 02:22:38 -08005682
Alexander Duyck13fde972011-10-05 13:35:24 +00005683 if (test_bit(__IGB_DOWN, &adapter->state))
5684 return true;
Alexander Duyck0e014cb2008-12-26 01:33:18 -08005685
Alexander Duyck06034642011-08-26 07:44:22 +00005686 tx_buffer = &tx_ring->tx_buffer_info[i];
Alexander Duyck13fde972011-10-05 13:35:24 +00005687 tx_desc = IGB_TX_DESC(tx_ring, i);
Alexander Duyck8542db02011-08-26 07:44:43 +00005688 i -= tx_ring->count;
Auke Kok9d5c8242008-01-24 02:22:38 -08005689
Alexander Duyckf4128782012-09-13 06:28:01 +00005690 do {
5691 union e1000_adv_tx_desc *eop_desc = tx_buffer->next_to_watch;
Alexander Duyck8542db02011-08-26 07:44:43 +00005692
5693 /* if next_to_watch is not set then there is no work pending */
5694 if (!eop_desc)
5695 break;
Alexander Duyck13fde972011-10-05 13:35:24 +00005696
Alexander Duyckf4128782012-09-13 06:28:01 +00005697 /* prevent any other reads prior to eop_desc */
5698 rmb();
5699
Alexander Duyck13fde972011-10-05 13:35:24 +00005700 /* if DD is not set pending work has not been completed */
5701 if (!(eop_desc->wb.status & cpu_to_le32(E1000_TXD_STAT_DD)))
5702 break;
5703
Alexander Duyck8542db02011-08-26 07:44:43 +00005704 /* clear next_to_watch to prevent false hangs */
5705 tx_buffer->next_to_watch = NULL;
Alexander Duyck13fde972011-10-05 13:35:24 +00005706
Alexander Duyckebe42d12011-08-26 07:45:09 +00005707 /* update the statistics for this packet */
5708 total_bytes += tx_buffer->bytecount;
5709 total_packets += tx_buffer->gso_segs;
Alexander Duyck13fde972011-10-05 13:35:24 +00005710
Alexander Duyckebe42d12011-08-26 07:45:09 +00005711 /* free the skb */
5712 dev_kfree_skb_any(tx_buffer->skb);
Alexander Duyckebe42d12011-08-26 07:45:09 +00005713
5714 /* unmap skb header data */
5715 dma_unmap_single(tx_ring->dev,
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00005716 dma_unmap_addr(tx_buffer, dma),
5717 dma_unmap_len(tx_buffer, len),
Alexander Duyckebe42d12011-08-26 07:45:09 +00005718 DMA_TO_DEVICE);
5719
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00005720 /* clear tx_buffer data */
5721 tx_buffer->skb = NULL;
5722 dma_unmap_len_set(tx_buffer, len, 0);
5723
Alexander Duyckebe42d12011-08-26 07:45:09 +00005724 /* clear last DMA location and unmap remaining buffers */
5725 while (tx_desc != eop_desc) {
Alexander Duyck13fde972011-10-05 13:35:24 +00005726 tx_buffer++;
5727 tx_desc++;
Auke Kok9d5c8242008-01-24 02:22:38 -08005728 i++;
Alexander Duyck8542db02011-08-26 07:44:43 +00005729 if (unlikely(!i)) {
5730 i -= tx_ring->count;
Alexander Duyck06034642011-08-26 07:44:22 +00005731 tx_buffer = tx_ring->tx_buffer_info;
Alexander Duyck13fde972011-10-05 13:35:24 +00005732 tx_desc = IGB_TX_DESC(tx_ring, 0);
5733 }
Alexander Duyckebe42d12011-08-26 07:45:09 +00005734
5735 /* unmap any remaining paged data */
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00005736 if (dma_unmap_len(tx_buffer, len)) {
Alexander Duyckebe42d12011-08-26 07:45:09 +00005737 dma_unmap_page(tx_ring->dev,
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00005738 dma_unmap_addr(tx_buffer, dma),
5739 dma_unmap_len(tx_buffer, len),
Alexander Duyckebe42d12011-08-26 07:45:09 +00005740 DMA_TO_DEVICE);
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00005741 dma_unmap_len_set(tx_buffer, len, 0);
Alexander Duyckebe42d12011-08-26 07:45:09 +00005742 }
5743 }
5744
Alexander Duyckebe42d12011-08-26 07:45:09 +00005745 /* move us one more past the eop_desc for start of next pkt */
5746 tx_buffer++;
5747 tx_desc++;
5748 i++;
5749 if (unlikely(!i)) {
5750 i -= tx_ring->count;
5751 tx_buffer = tx_ring->tx_buffer_info;
5752 tx_desc = IGB_TX_DESC(tx_ring, 0);
5753 }
Alexander Duyckf4128782012-09-13 06:28:01 +00005754
5755 /* issue prefetch for next Tx descriptor */
5756 prefetch(tx_desc);
5757
5758 /* update budget accounting */
5759 budget--;
5760 } while (likely(budget));
Alexander Duyck0e014cb2008-12-26 01:33:18 -08005761
Eric Dumazetbdbc0632012-01-04 20:23:36 +00005762 netdev_tx_completed_queue(txring_txq(tx_ring),
5763 total_packets, total_bytes);
Alexander Duyck8542db02011-08-26 07:44:43 +00005764 i += tx_ring->count;
Auke Kok9d5c8242008-01-24 02:22:38 -08005765 tx_ring->next_to_clean = i;
Alexander Duyck13fde972011-10-05 13:35:24 +00005766 u64_stats_update_begin(&tx_ring->tx_syncp);
5767 tx_ring->tx_stats.bytes += total_bytes;
5768 tx_ring->tx_stats.packets += total_packets;
5769 u64_stats_update_end(&tx_ring->tx_syncp);
Alexander Duyck0ba82992011-08-26 07:45:47 +00005770 q_vector->tx.total_bytes += total_bytes;
5771 q_vector->tx.total_packets += total_packets;
Auke Kok9d5c8242008-01-24 02:22:38 -08005772
Alexander Duyck6d095fa2011-08-26 07:46:19 +00005773 if (test_bit(IGB_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags)) {
Alexander Duyck13fde972011-10-05 13:35:24 +00005774 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck13fde972011-10-05 13:35:24 +00005775
Auke Kok9d5c8242008-01-24 02:22:38 -08005776 /* Detect a transmit hang in hardware, this serializes the
5777 * check with the clearing of time_stamp and movement of i */
Alexander Duyck6d095fa2011-08-26 07:46:19 +00005778 clear_bit(IGB_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags);
Alexander Duyckf4128782012-09-13 06:28:01 +00005779 if (tx_buffer->next_to_watch &&
Alexander Duyck8542db02011-08-26 07:44:43 +00005780 time_after(jiffies, tx_buffer->time_stamp +
Joe Perches8e95a202009-12-03 07:58:21 +00005781 (adapter->tx_timeout_factor * HZ)) &&
5782 !(rd32(E1000_STATUS) & E1000_STATUS_TXOFF)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08005783
Auke Kok9d5c8242008-01-24 02:22:38 -08005784 /* detected Tx unit hang */
Alexander Duyck59d71982010-04-27 13:09:25 +00005785 dev_err(tx_ring->dev,
Auke Kok9d5c8242008-01-24 02:22:38 -08005786 "Detected Tx Unit Hang\n"
Alexander Duyck2d064c02008-07-08 15:10:12 -07005787 " Tx Queue <%d>\n"
Auke Kok9d5c8242008-01-24 02:22:38 -08005788 " TDH <%x>\n"
5789 " TDT <%x>\n"
5790 " next_to_use <%x>\n"
5791 " next_to_clean <%x>\n"
Auke Kok9d5c8242008-01-24 02:22:38 -08005792 "buffer_info[next_to_clean]\n"
5793 " time_stamp <%lx>\n"
Alexander Duyck8542db02011-08-26 07:44:43 +00005794 " next_to_watch <%p>\n"
Auke Kok9d5c8242008-01-24 02:22:38 -08005795 " jiffies <%lx>\n"
5796 " desc.status <%x>\n",
Alexander Duyck2d064c02008-07-08 15:10:12 -07005797 tx_ring->queue_index,
Alexander Duyck238ac812011-08-26 07:43:48 +00005798 rd32(E1000_TDH(tx_ring->reg_idx)),
Alexander Duyckfce99e32009-10-27 15:51:27 +00005799 readl(tx_ring->tail),
Auke Kok9d5c8242008-01-24 02:22:38 -08005800 tx_ring->next_to_use,
5801 tx_ring->next_to_clean,
Alexander Duyck8542db02011-08-26 07:44:43 +00005802 tx_buffer->time_stamp,
Alexander Duyckf4128782012-09-13 06:28:01 +00005803 tx_buffer->next_to_watch,
Auke Kok9d5c8242008-01-24 02:22:38 -08005804 jiffies,
Alexander Duyckf4128782012-09-13 06:28:01 +00005805 tx_buffer->next_to_watch->wb.status);
Alexander Duyck13fde972011-10-05 13:35:24 +00005806 netif_stop_subqueue(tx_ring->netdev,
5807 tx_ring->queue_index);
5808
5809 /* we are about to reset, no point in enabling stuff */
5810 return true;
Auke Kok9d5c8242008-01-24 02:22:38 -08005811 }
5812 }
Alexander Duyck13fde972011-10-05 13:35:24 +00005813
5814 if (unlikely(total_packets &&
5815 netif_carrier_ok(tx_ring->netdev) &&
5816 igb_desc_unused(tx_ring) >= IGB_TX_QUEUE_WAKE)) {
5817 /* Make sure that anybody stopping the queue after this
5818 * sees the new next_to_clean.
5819 */
5820 smp_mb();
5821 if (__netif_subqueue_stopped(tx_ring->netdev,
5822 tx_ring->queue_index) &&
5823 !(test_bit(__IGB_DOWN, &adapter->state))) {
5824 netif_wake_subqueue(tx_ring->netdev,
5825 tx_ring->queue_index);
5826
5827 u64_stats_update_begin(&tx_ring->tx_syncp);
5828 tx_ring->tx_stats.restart_queue++;
5829 u64_stats_update_end(&tx_ring->tx_syncp);
5830 }
5831 }
5832
5833 return !!budget;
Auke Kok9d5c8242008-01-24 02:22:38 -08005834}
5835
Alexander Duyckcbc8e552012-09-25 00:31:02 +00005836/**
5837 * igb_reuse_rx_page - page flip buffer and store it back on the ring
5838 * @rx_ring: rx descriptor ring to store buffers on
5839 * @old_buff: donor buffer to have page reused
5840 *
5841 * Synchronizes page for reuse by the adapter
5842 **/
5843static void igb_reuse_rx_page(struct igb_ring *rx_ring,
5844 struct igb_rx_buffer *old_buff)
5845{
5846 struct igb_rx_buffer *new_buff;
5847 u16 nta = rx_ring->next_to_alloc;
5848
5849 new_buff = &rx_ring->rx_buffer_info[nta];
5850
5851 /* update, and store next to alloc */
5852 nta++;
5853 rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0;
5854
5855 /* transfer page from old buffer to new buffer */
5856 memcpy(new_buff, old_buff, sizeof(struct igb_rx_buffer));
5857
5858 /* sync the buffer for use by the device */
5859 dma_sync_single_range_for_device(rx_ring->dev, old_buff->dma,
5860 old_buff->page_offset,
Alexander Duyckde78d1f2012-09-25 00:31:12 +00005861 IGB_RX_BUFSZ,
Alexander Duyckcbc8e552012-09-25 00:31:02 +00005862 DMA_FROM_DEVICE);
5863}
5864
5865/**
5866 * igb_add_rx_frag - Add contents of Rx buffer to sk_buff
5867 * @rx_ring: rx descriptor ring to transact packets on
5868 * @rx_buffer: buffer containing page to add
5869 * @rx_desc: descriptor containing length of buffer written by hardware
5870 * @skb: sk_buff to place the data into
5871 *
5872 * This function will add the data contained in rx_buffer->page to the skb.
5873 * This is done either through a direct copy if the data in the buffer is
5874 * less than the skb header size, otherwise it will just attach the page as
5875 * a frag to the skb.
5876 *
5877 * The function will then update the page offset if necessary and return
5878 * true if the buffer can be reused by the adapter.
5879 **/
5880static bool igb_add_rx_frag(struct igb_ring *rx_ring,
5881 struct igb_rx_buffer *rx_buffer,
5882 union e1000_adv_rx_desc *rx_desc,
5883 struct sk_buff *skb)
5884{
5885 struct page *page = rx_buffer->page;
5886 unsigned int size = le16_to_cpu(rx_desc->wb.upper.length);
5887
5888 if ((size <= IGB_RX_HDR_LEN) && !skb_is_nonlinear(skb)) {
5889 unsigned char *va = page_address(page) + rx_buffer->page_offset;
5890
Alexander Duyckcbc8e552012-09-25 00:31:02 +00005891 if (igb_test_staterr(rx_desc, E1000_RXDADV_STAT_TSIP)) {
5892 igb_ptp_rx_pktstamp(rx_ring->q_vector, va, skb);
5893 va += IGB_TS_HDR_LEN;
5894 size -= IGB_TS_HDR_LEN;
5895 }
5896
Alexander Duyckcbc8e552012-09-25 00:31:02 +00005897 memcpy(__skb_put(skb, size), va, ALIGN(size, sizeof(long)));
5898
5899 /* we can reuse buffer as-is, just make sure it is local */
5900 if (likely(page_to_nid(page) == numa_node_id()))
5901 return true;
5902
5903 /* this page cannot be reused so discard it */
5904 put_page(page);
5905 return false;
5906 }
5907
5908 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page,
Alexander Duyckde78d1f2012-09-25 00:31:12 +00005909 rx_buffer->page_offset, size, IGB_RX_BUFSZ);
Alexander Duyckcbc8e552012-09-25 00:31:02 +00005910
5911 /* avoid re-using remote pages */
5912 if (unlikely(page_to_nid(page) != numa_node_id()))
5913 return false;
5914
Alexander Duyckde78d1f2012-09-25 00:31:12 +00005915#if (PAGE_SIZE < 8192)
Alexander Duyckcbc8e552012-09-25 00:31:02 +00005916 /* if we are only owner of page we can reuse it */
5917 if (unlikely(page_count(page) != 1))
5918 return false;
5919
5920 /* flip page offset to other buffer */
Alexander Duyckde78d1f2012-09-25 00:31:12 +00005921 rx_buffer->page_offset ^= IGB_RX_BUFSZ;
Alexander Duyckcbc8e552012-09-25 00:31:02 +00005922
5923 /*
5924 * since we are the only owner of the page and we need to
5925 * increment it, just set the value to 2 in order to avoid
5926 * an unnecessary locked operation
5927 */
5928 atomic_set(&page->_count, 2);
Alexander Duyckde78d1f2012-09-25 00:31:12 +00005929#else
5930 /* move offset up to the next cache line */
5931 rx_buffer->page_offset += SKB_DATA_ALIGN(size);
5932
5933 if (rx_buffer->page_offset > (PAGE_SIZE - IGB_RX_BUFSZ))
5934 return false;
5935
5936 /* bump ref count on page before it is given to the stack */
5937 get_page(page);
5938#endif
Alexander Duyckcbc8e552012-09-25 00:31:02 +00005939
5940 return true;
5941}
5942
Alexander Duyck2e334ee2012-09-25 00:31:07 +00005943static struct sk_buff *igb_fetch_rx_buffer(struct igb_ring *rx_ring,
5944 union e1000_adv_rx_desc *rx_desc,
5945 struct sk_buff *skb)
5946{
5947 struct igb_rx_buffer *rx_buffer;
5948 struct page *page;
5949
5950 rx_buffer = &rx_ring->rx_buffer_info[rx_ring->next_to_clean];
5951
5952 /*
5953 * This memory barrier is needed to keep us from reading
5954 * any other fields out of the rx_desc until we know the
5955 * RXD_STAT_DD bit is set
5956 */
5957 rmb();
5958
5959 page = rx_buffer->page;
5960 prefetchw(page);
5961
5962 if (likely(!skb)) {
5963 void *page_addr = page_address(page) +
5964 rx_buffer->page_offset;
5965
5966 /* prefetch first cache line of first page */
5967 prefetch(page_addr);
5968#if L1_CACHE_BYTES < 128
5969 prefetch(page_addr + L1_CACHE_BYTES);
5970#endif
5971
5972 /* allocate a skb to store the frags */
5973 skb = netdev_alloc_skb_ip_align(rx_ring->netdev,
5974 IGB_RX_HDR_LEN);
5975 if (unlikely(!skb)) {
5976 rx_ring->rx_stats.alloc_failed++;
5977 return NULL;
5978 }
5979
5980 /*
5981 * we will be copying header into skb->data in
5982 * pskb_may_pull so it is in our interest to prefetch
5983 * it now to avoid a possible cache miss
5984 */
5985 prefetchw(skb->data);
5986 }
5987
5988 /* we are reusing so sync this buffer for CPU use */
5989 dma_sync_single_range_for_cpu(rx_ring->dev,
5990 rx_buffer->dma,
5991 rx_buffer->page_offset,
Alexander Duyckde78d1f2012-09-25 00:31:12 +00005992 IGB_RX_BUFSZ,
Alexander Duyck2e334ee2012-09-25 00:31:07 +00005993 DMA_FROM_DEVICE);
5994
5995 /* pull page into skb */
5996 if (igb_add_rx_frag(rx_ring, rx_buffer, rx_desc, skb)) {
5997 /* hand second half of page back to the ring */
5998 igb_reuse_rx_page(rx_ring, rx_buffer);
5999 } else {
6000 /* we are not reusing the buffer so unmap it */
6001 dma_unmap_page(rx_ring->dev, rx_buffer->dma,
6002 PAGE_SIZE, DMA_FROM_DEVICE);
6003 }
6004
6005 /* clear contents of rx_buffer */
6006 rx_buffer->page = NULL;
6007
6008 return skb;
6009}
6010
Alexander Duyckcd392f52011-08-26 07:43:59 +00006011static inline void igb_rx_checksum(struct igb_ring *ring,
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00006012 union e1000_adv_rx_desc *rx_desc,
6013 struct sk_buff *skb)
Auke Kok9d5c8242008-01-24 02:22:38 -08006014{
Eric Dumazetbc8acf22010-09-02 13:07:41 -07006015 skb_checksum_none_assert(skb);
Auke Kok9d5c8242008-01-24 02:22:38 -08006016
Alexander Duyck294e7d72011-08-26 07:45:57 +00006017 /* Ignore Checksum bit is set */
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00006018 if (igb_test_staterr(rx_desc, E1000_RXD_STAT_IXSM))
Alexander Duyck294e7d72011-08-26 07:45:57 +00006019 return;
6020
6021 /* Rx checksum disabled via ethtool */
6022 if (!(ring->netdev->features & NETIF_F_RXCSUM))
Auke Kok9d5c8242008-01-24 02:22:38 -08006023 return;
Alexander Duyck85ad76b2009-10-27 15:52:46 +00006024
Auke Kok9d5c8242008-01-24 02:22:38 -08006025 /* TCP/UDP checksum error bit is set */
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00006026 if (igb_test_staterr(rx_desc,
6027 E1000_RXDEXT_STATERR_TCPE |
6028 E1000_RXDEXT_STATERR_IPE)) {
Jesse Brandeburgb9473562009-04-27 22:36:13 +00006029 /*
6030 * work around errata with sctp packets where the TCPE aka
6031 * L4E bit is set incorrectly on 64 byte (60 byte w/o crc)
6032 * packets, (aka let the stack check the crc32c)
6033 */
Alexander Duyck866cff02011-08-26 07:45:36 +00006034 if (!((skb->len == 60) &&
6035 test_bit(IGB_RING_FLAG_RX_SCTP_CSUM, &ring->flags))) {
Eric Dumazet12dcd862010-10-15 17:27:10 +00006036 u64_stats_update_begin(&ring->rx_syncp);
Alexander Duyck04a5fcaa2009-10-27 15:52:27 +00006037 ring->rx_stats.csum_err++;
Eric Dumazet12dcd862010-10-15 17:27:10 +00006038 u64_stats_update_end(&ring->rx_syncp);
6039 }
Auke Kok9d5c8242008-01-24 02:22:38 -08006040 /* let the stack verify checksum errors */
Auke Kok9d5c8242008-01-24 02:22:38 -08006041 return;
6042 }
6043 /* It must be a TCP or UDP packet with a valid checksum */
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00006044 if (igb_test_staterr(rx_desc, E1000_RXD_STAT_TCPCS |
6045 E1000_RXD_STAT_UDPCS))
Auke Kok9d5c8242008-01-24 02:22:38 -08006046 skb->ip_summed = CHECKSUM_UNNECESSARY;
6047
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00006048 dev_dbg(ring->dev, "cksum success: bits %08X\n",
6049 le32_to_cpu(rx_desc->wb.upper.status_error));
Auke Kok9d5c8242008-01-24 02:22:38 -08006050}
6051
Alexander Duyck077887c2011-08-26 07:46:29 +00006052static inline void igb_rx_hash(struct igb_ring *ring,
6053 union e1000_adv_rx_desc *rx_desc,
6054 struct sk_buff *skb)
6055{
6056 if (ring->netdev->features & NETIF_F_RXHASH)
6057 skb->rxhash = le32_to_cpu(rx_desc->wb.lower.hi_dword.rss);
6058}
6059
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006060/**
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006061 * igb_is_non_eop - process handling of non-EOP buffers
6062 * @rx_ring: Rx ring being processed
6063 * @rx_desc: Rx descriptor for current buffer
6064 * @skb: current socket buffer containing buffer in progress
6065 *
6066 * This function updates next to clean. If the buffer is an EOP buffer
6067 * this function exits returning false, otherwise it will place the
6068 * sk_buff in the next buffer to be chained and return true indicating
6069 * that this is in fact a non-EOP buffer.
6070 **/
6071static bool igb_is_non_eop(struct igb_ring *rx_ring,
6072 union e1000_adv_rx_desc *rx_desc)
6073{
6074 u32 ntc = rx_ring->next_to_clean + 1;
6075
6076 /* fetch, update, and store next to clean */
6077 ntc = (ntc < rx_ring->count) ? ntc : 0;
6078 rx_ring->next_to_clean = ntc;
6079
6080 prefetch(IGB_RX_DESC(rx_ring, ntc));
6081
6082 if (likely(igb_test_staterr(rx_desc, E1000_RXD_STAT_EOP)))
6083 return false;
6084
6085 return true;
6086}
6087
6088/**
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006089 * igb_get_headlen - determine size of header for LRO/GRO
6090 * @data: pointer to the start of the headers
6091 * @max_len: total length of section to find headers in
6092 *
6093 * This function is meant to determine the length of headers that will
6094 * be recognized by hardware for LRO, and GRO offloads. The main
6095 * motivation of doing this is to only perform one pull for IPv4 TCP
6096 * packets so that we can do basic things like calculating the gso_size
6097 * based on the average data per packet.
6098 **/
6099static unsigned int igb_get_headlen(unsigned char *data,
6100 unsigned int max_len)
Alexander Duyck2d94d8a2009-07-23 18:10:06 +00006101{
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006102 union {
6103 unsigned char *network;
6104 /* l2 headers */
6105 struct ethhdr *eth;
6106 struct vlan_hdr *vlan;
6107 /* l3 headers */
6108 struct iphdr *ipv4;
6109 struct ipv6hdr *ipv6;
6110 } hdr;
6111 __be16 protocol;
6112 u8 nexthdr = 0; /* default to not TCP */
6113 u8 hlen;
6114
6115 /* this should never happen, but better safe than sorry */
6116 if (max_len < ETH_HLEN)
6117 return max_len;
6118
6119 /* initialize network frame pointer */
6120 hdr.network = data;
6121
6122 /* set first protocol and move network header forward */
6123 protocol = hdr.eth->h_proto;
6124 hdr.network += ETH_HLEN;
6125
6126 /* handle any vlan tag if present */
6127 if (protocol == __constant_htons(ETH_P_8021Q)) {
6128 if ((hdr.network - data) > (max_len - VLAN_HLEN))
6129 return max_len;
6130
6131 protocol = hdr.vlan->h_vlan_encapsulated_proto;
6132 hdr.network += VLAN_HLEN;
6133 }
6134
6135 /* handle L3 protocols */
6136 if (protocol == __constant_htons(ETH_P_IP)) {
6137 if ((hdr.network - data) > (max_len - sizeof(struct iphdr)))
6138 return max_len;
6139
6140 /* access ihl as a u8 to avoid unaligned access on ia64 */
6141 hlen = (hdr.network[0] & 0x0F) << 2;
6142
6143 /* verify hlen meets minimum size requirements */
6144 if (hlen < sizeof(struct iphdr))
6145 return hdr.network - data;
6146
Alexander Duyckf2fb4ab2012-11-13 01:13:38 +00006147 /* record next protocol if header is present */
6148 if (!hdr.ipv4->frag_off)
6149 nexthdr = hdr.ipv4->protocol;
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006150 } else if (protocol == __constant_htons(ETH_P_IPV6)) {
6151 if ((hdr.network - data) > (max_len - sizeof(struct ipv6hdr)))
6152 return max_len;
6153
6154 /* record next protocol */
6155 nexthdr = hdr.ipv6->nexthdr;
Alexander Duyckf2fb4ab2012-11-13 01:13:38 +00006156 hlen = sizeof(struct ipv6hdr);
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006157 } else {
6158 return hdr.network - data;
6159 }
6160
Alexander Duyckf2fb4ab2012-11-13 01:13:38 +00006161 /* relocate pointer to start of L4 header */
6162 hdr.network += hlen;
6163
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006164 /* finally sort out TCP */
6165 if (nexthdr == IPPROTO_TCP) {
6166 if ((hdr.network - data) > (max_len - sizeof(struct tcphdr)))
6167 return max_len;
6168
6169 /* access doff as a u8 to avoid unaligned access on ia64 */
6170 hlen = (hdr.network[12] & 0xF0) >> 2;
6171
6172 /* verify hlen meets minimum size requirements */
6173 if (hlen < sizeof(struct tcphdr))
6174 return hdr.network - data;
6175
6176 hdr.network += hlen;
6177 } else if (nexthdr == IPPROTO_UDP) {
6178 if ((hdr.network - data) > (max_len - sizeof(struct udphdr)))
6179 return max_len;
6180
6181 hdr.network += sizeof(struct udphdr);
6182 }
6183
6184 /*
6185 * If everything has gone correctly hdr.network should be the
6186 * data section of the packet and will be the end of the header.
6187 * If not then it probably represents the end of the last recognized
6188 * header.
Alexander Duyck2d94d8a2009-07-23 18:10:06 +00006189 */
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006190 if ((hdr.network - data) < max_len)
6191 return hdr.network - data;
6192 else
6193 return max_len;
6194}
6195
6196/**
6197 * igb_pull_tail - igb specific version of skb_pull_tail
6198 * @rx_ring: rx descriptor ring packet is being transacted on
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006199 * @rx_desc: pointer to the EOP Rx descriptor
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006200 * @skb: pointer to current skb being adjusted
6201 *
6202 * This function is an igb specific version of __pskb_pull_tail. The
6203 * main difference between this version and the original function is that
6204 * this function can make several assumptions about the state of things
6205 * that allow for significant optimizations versus the standard function.
6206 * As a result we can do things like drop a frag and maintain an accurate
6207 * truesize for the skb.
6208 */
6209static void igb_pull_tail(struct igb_ring *rx_ring,
6210 union e1000_adv_rx_desc *rx_desc,
6211 struct sk_buff *skb)
6212{
6213 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[0];
6214 unsigned char *va;
6215 unsigned int pull_len;
6216
6217 /*
6218 * it is valid to use page_address instead of kmap since we are
6219 * working with pages allocated out of the lomem pool per
6220 * alloc_page(GFP_ATOMIC)
6221 */
6222 va = skb_frag_address(frag);
6223
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006224 if (igb_test_staterr(rx_desc, E1000_RXDADV_STAT_TSIP)) {
6225 /* retrieve timestamp from buffer */
6226 igb_ptp_rx_pktstamp(rx_ring->q_vector, va, skb);
6227
6228 /* update pointers to remove timestamp header */
6229 skb_frag_size_sub(frag, IGB_TS_HDR_LEN);
6230 frag->page_offset += IGB_TS_HDR_LEN;
6231 skb->data_len -= IGB_TS_HDR_LEN;
6232 skb->len -= IGB_TS_HDR_LEN;
6233
6234 /* move va to start of packet data */
6235 va += IGB_TS_HDR_LEN;
6236 }
6237
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006238 /*
6239 * we need the header to contain the greater of either ETH_HLEN or
6240 * 60 bytes if the skb->len is less than 60 for skb_pad.
6241 */
6242 pull_len = igb_get_headlen(va, IGB_RX_HDR_LEN);
6243
6244 /* align pull length to size of long to optimize memcpy performance */
6245 skb_copy_to_linear_data(skb, va, ALIGN(pull_len, sizeof(long)));
6246
6247 /* update all of the pointers */
6248 skb_frag_size_sub(frag, pull_len);
6249 frag->page_offset += pull_len;
6250 skb->data_len -= pull_len;
6251 skb->tail += pull_len;
6252}
6253
6254/**
6255 * igb_cleanup_headers - Correct corrupted or empty headers
6256 * @rx_ring: rx descriptor ring packet is being transacted on
6257 * @rx_desc: pointer to the EOP Rx descriptor
6258 * @skb: pointer to current skb being fixed
6259 *
6260 * Address the case where we are pulling data in on pages only
6261 * and as such no data is present in the skb header.
6262 *
6263 * In addition if skb is not at least 60 bytes we need to pad it so that
6264 * it is large enough to qualify as a valid Ethernet frame.
6265 *
6266 * Returns true if an error was encountered and skb was freed.
6267 **/
6268static bool igb_cleanup_headers(struct igb_ring *rx_ring,
6269 union e1000_adv_rx_desc *rx_desc,
6270 struct sk_buff *skb)
6271{
6272
6273 if (unlikely((igb_test_staterr(rx_desc,
6274 E1000_RXDEXT_ERR_FRAME_ERR_MASK)))) {
6275 struct net_device *netdev = rx_ring->netdev;
6276 if (!(netdev->features & NETIF_F_RXALL)) {
6277 dev_kfree_skb_any(skb);
6278 return true;
6279 }
6280 }
6281
6282 /* place header in linear portion of buffer */
6283 if (skb_is_nonlinear(skb))
6284 igb_pull_tail(rx_ring, rx_desc, skb);
6285
6286 /* if skb_pad returns an error the skb was freed */
6287 if (unlikely(skb->len < 60)) {
6288 int pad_len = 60 - skb->len;
6289
6290 if (skb_pad(skb, pad_len))
6291 return true;
6292 __skb_put(skb, pad_len);
6293 }
6294
6295 return false;
Alexander Duyck2d94d8a2009-07-23 18:10:06 +00006296}
6297
Alexander Duyckdb2ee5b2012-09-25 00:30:57 +00006298/**
6299 * igb_process_skb_fields - Populate skb header fields from Rx descriptor
6300 * @rx_ring: rx descriptor ring packet is being transacted on
6301 * @rx_desc: pointer to the EOP Rx descriptor
6302 * @skb: pointer to current skb being populated
6303 *
6304 * This function checks the ring, descriptor, and packet information in
6305 * order to populate the hash, checksum, VLAN, timestamp, protocol, and
6306 * other fields within the skb.
6307 **/
6308static void igb_process_skb_fields(struct igb_ring *rx_ring,
6309 union e1000_adv_rx_desc *rx_desc,
6310 struct sk_buff *skb)
6311{
6312 struct net_device *dev = rx_ring->netdev;
6313
6314 igb_rx_hash(rx_ring, rx_desc, skb);
6315
6316 igb_rx_checksum(rx_ring, rx_desc, skb);
6317
Alexander Duyckdb2ee5b2012-09-25 00:30:57 +00006318 igb_ptp_rx_hwtstamp(rx_ring->q_vector, rx_desc, skb);
Alexander Duyckdb2ee5b2012-09-25 00:30:57 +00006319
6320 if ((dev->features & NETIF_F_HW_VLAN_RX) &&
6321 igb_test_staterr(rx_desc, E1000_RXD_STAT_VP)) {
6322 u16 vid;
6323 if (igb_test_staterr(rx_desc, E1000_RXDEXT_STATERR_LB) &&
6324 test_bit(IGB_RING_FLAG_RX_LB_VLAN_BSWAP, &rx_ring->flags))
6325 vid = be16_to_cpu(rx_desc->wb.upper.vlan);
6326 else
6327 vid = le16_to_cpu(rx_desc->wb.upper.vlan);
6328
6329 __vlan_hwaccel_put_tag(skb, vid);
6330 }
6331
6332 skb_record_rx_queue(skb, rx_ring->queue_index);
6333
6334 skb->protocol = eth_type_trans(skb, rx_ring->netdev);
6335}
6336
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006337static bool igb_clean_rx_irq(struct igb_q_vector *q_vector, const int budget)
Auke Kok9d5c8242008-01-24 02:22:38 -08006338{
Alexander Duyck0ba82992011-08-26 07:45:47 +00006339 struct igb_ring *rx_ring = q_vector->rx.ring;
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006340 struct sk_buff *skb = rx_ring->skb;
Auke Kok9d5c8242008-01-24 02:22:38 -08006341 unsigned int total_bytes = 0, total_packets = 0;
Alexander Duyck16eb8812011-08-26 07:43:54 +00006342 u16 cleaned_count = igb_desc_unused(rx_ring);
Auke Kok9d5c8242008-01-24 02:22:38 -08006343
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006344 do {
6345 union e1000_adv_rx_desc *rx_desc;
Auke Kok9d5c8242008-01-24 02:22:38 -08006346
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006347 /* return some buffers to hardware, one at a time is too slow */
6348 if (cleaned_count >= IGB_RX_BUFFER_WRITE) {
6349 igb_alloc_rx_buffers(rx_ring, cleaned_count);
6350 cleaned_count = 0;
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07006351 }
6352
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006353 rx_desc = IGB_RX_DESC(rx_ring, rx_ring->next_to_clean);
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07006354
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006355 if (!igb_test_staterr(rx_desc, E1000_RXD_STAT_DD))
6356 break;
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07006357
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006358 /* retrieve a buffer from the ring */
6359 skb = igb_fetch_rx_buffer(rx_ring, rx_desc, skb);
Alexander Duyck16eb8812011-08-26 07:43:54 +00006360
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006361 /* exit if we failed to retrieve a buffer */
6362 if (!skb)
6363 break;
6364
6365 cleaned_count++;
6366
6367 /* fetch next buffer in frame if non-eop */
6368 if (igb_is_non_eop(rx_ring, rx_desc))
6369 continue;
Alexander Duyck44390ca2011-08-26 07:43:38 +00006370
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006371 /* verify the packet layout is correct */
6372 if (igb_cleanup_headers(rx_ring, rx_desc, skb)) {
6373 skb = NULL;
6374 continue;
Auke Kok9d5c8242008-01-24 02:22:38 -08006375 }
Auke Kok9d5c8242008-01-24 02:22:38 -08006376
Alexander Duyckdb2ee5b2012-09-25 00:30:57 +00006377 /* probably a little skewed due to removing CRC */
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00006378 total_bytes += skb->len;
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00006379
Alexander Duyckdb2ee5b2012-09-25 00:30:57 +00006380 /* populate checksum, timestamp, VLAN, and protocol */
6381 igb_process_skb_fields(rx_ring, rx_desc, skb);
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00006382
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00006383 napi_gro_receive(&q_vector->napi, skb);
Auke Kok9d5c8242008-01-24 02:22:38 -08006384
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006385 /* reset skb pointer */
6386 skb = NULL;
6387
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006388 /* update budget accounting */
6389 total_packets++;
6390 } while (likely(total_packets < budget));
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07006391
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006392 /* place incomplete frames back on ring for completion */
6393 rx_ring->skb = skb;
6394
Eric Dumazet12dcd862010-10-15 17:27:10 +00006395 u64_stats_update_begin(&rx_ring->rx_syncp);
Auke Kok9d5c8242008-01-24 02:22:38 -08006396 rx_ring->rx_stats.packets += total_packets;
6397 rx_ring->rx_stats.bytes += total_bytes;
Eric Dumazet12dcd862010-10-15 17:27:10 +00006398 u64_stats_update_end(&rx_ring->rx_syncp);
Alexander Duyck0ba82992011-08-26 07:45:47 +00006399 q_vector->rx.total_packets += total_packets;
6400 q_vector->rx.total_bytes += total_bytes;
Alexander Duyckc023cd82011-08-26 07:43:43 +00006401
6402 if (cleaned_count)
Alexander Duyckcd392f52011-08-26 07:43:59 +00006403 igb_alloc_rx_buffers(rx_ring, cleaned_count);
Alexander Duyckc023cd82011-08-26 07:43:43 +00006404
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006405 return (total_packets < budget);
Auke Kok9d5c8242008-01-24 02:22:38 -08006406}
6407
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006408static bool igb_alloc_mapped_page(struct igb_ring *rx_ring,
6409 struct igb_rx_buffer *bi)
Alexander Duyckc023cd82011-08-26 07:43:43 +00006410{
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006411 struct page *page = bi->page;
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006412 dma_addr_t dma;
Alexander Duyckc023cd82011-08-26 07:43:43 +00006413
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006414 /* since we are recycling buffers we should seldom need to alloc */
6415 if (likely(page))
Alexander Duyckc023cd82011-08-26 07:43:43 +00006416 return true;
6417
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006418 /* alloc new page for storage */
6419 page = __skb_alloc_page(GFP_ATOMIC | __GFP_COLD, NULL);
6420 if (unlikely(!page)) {
6421 rx_ring->rx_stats.alloc_failed++;
6422 return false;
Alexander Duyckc023cd82011-08-26 07:43:43 +00006423 }
6424
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006425 /* map page for use */
6426 dma = dma_map_page(rx_ring->dev, page, 0, PAGE_SIZE, DMA_FROM_DEVICE);
Alexander Duyckc023cd82011-08-26 07:43:43 +00006427
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006428 /*
6429 * if mapping failed free memory back to system since
6430 * there isn't much point in holding memory we can't use
6431 */
Alexander Duyckc023cd82011-08-26 07:43:43 +00006432 if (dma_mapping_error(rx_ring->dev, dma)) {
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006433 __free_page(page);
6434
Alexander Duyckc023cd82011-08-26 07:43:43 +00006435 rx_ring->rx_stats.alloc_failed++;
6436 return false;
6437 }
6438
6439 bi->dma = dma;
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006440 bi->page = page;
6441 bi->page_offset = 0;
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006442
Alexander Duyckc023cd82011-08-26 07:43:43 +00006443 return true;
6444}
6445
Auke Kok9d5c8242008-01-24 02:22:38 -08006446/**
Alexander Duyckcd392f52011-08-26 07:43:59 +00006447 * igb_alloc_rx_buffers - Replace used receive buffers; packet split
Auke Kok9d5c8242008-01-24 02:22:38 -08006448 * @adapter: address of board private structure
6449 **/
Alexander Duyckcd392f52011-08-26 07:43:59 +00006450void igb_alloc_rx_buffers(struct igb_ring *rx_ring, u16 cleaned_count)
Auke Kok9d5c8242008-01-24 02:22:38 -08006451{
Auke Kok9d5c8242008-01-24 02:22:38 -08006452 union e1000_adv_rx_desc *rx_desc;
Alexander Duyck06034642011-08-26 07:44:22 +00006453 struct igb_rx_buffer *bi;
Alexander Duyckc023cd82011-08-26 07:43:43 +00006454 u16 i = rx_ring->next_to_use;
Auke Kok9d5c8242008-01-24 02:22:38 -08006455
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006456 /* nothing to do */
6457 if (!cleaned_count)
6458 return;
6459
Alexander Duyck601369062011-08-26 07:44:05 +00006460 rx_desc = IGB_RX_DESC(rx_ring, i);
Alexander Duyck06034642011-08-26 07:44:22 +00006461 bi = &rx_ring->rx_buffer_info[i];
Alexander Duyckc023cd82011-08-26 07:43:43 +00006462 i -= rx_ring->count;
Auke Kok9d5c8242008-01-24 02:22:38 -08006463
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006464 do {
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006465 if (!igb_alloc_mapped_page(rx_ring, bi))
Alexander Duyckc023cd82011-08-26 07:43:43 +00006466 break;
Auke Kok9d5c8242008-01-24 02:22:38 -08006467
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006468 /*
6469 * Refresh the desc even if buffer_addrs didn't change
6470 * because each write-back erases this info.
6471 */
6472 rx_desc->read.pkt_addr = cpu_to_le64(bi->dma + bi->page_offset);
Auke Kok9d5c8242008-01-24 02:22:38 -08006473
Alexander Duyckc023cd82011-08-26 07:43:43 +00006474 rx_desc++;
6475 bi++;
Auke Kok9d5c8242008-01-24 02:22:38 -08006476 i++;
Alexander Duyckc023cd82011-08-26 07:43:43 +00006477 if (unlikely(!i)) {
Alexander Duyck601369062011-08-26 07:44:05 +00006478 rx_desc = IGB_RX_DESC(rx_ring, 0);
Alexander Duyck06034642011-08-26 07:44:22 +00006479 bi = rx_ring->rx_buffer_info;
Alexander Duyckc023cd82011-08-26 07:43:43 +00006480 i -= rx_ring->count;
6481 }
6482
6483 /* clear the hdr_addr for the next_to_use descriptor */
6484 rx_desc->read.hdr_addr = 0;
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006485
6486 cleaned_count--;
6487 } while (cleaned_count);
Auke Kok9d5c8242008-01-24 02:22:38 -08006488
Alexander Duyckc023cd82011-08-26 07:43:43 +00006489 i += rx_ring->count;
6490
Auke Kok9d5c8242008-01-24 02:22:38 -08006491 if (rx_ring->next_to_use != i) {
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006492 /* record the next descriptor to use */
Auke Kok9d5c8242008-01-24 02:22:38 -08006493 rx_ring->next_to_use = i;
Auke Kok9d5c8242008-01-24 02:22:38 -08006494
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006495 /* update next to alloc since we have filled the ring */
6496 rx_ring->next_to_alloc = i;
6497
6498 /*
6499 * Force memory writes to complete before letting h/w
Auke Kok9d5c8242008-01-24 02:22:38 -08006500 * know there are new descriptors to fetch. (Only
6501 * applicable for weak-ordered memory model archs,
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006502 * such as IA-64).
6503 */
Auke Kok9d5c8242008-01-24 02:22:38 -08006504 wmb();
Alexander Duyckfce99e32009-10-27 15:51:27 +00006505 writel(i, rx_ring->tail);
Auke Kok9d5c8242008-01-24 02:22:38 -08006506 }
6507}
6508
6509/**
6510 * igb_mii_ioctl -
6511 * @netdev:
6512 * @ifreq:
6513 * @cmd:
6514 **/
6515static int igb_mii_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
6516{
6517 struct igb_adapter *adapter = netdev_priv(netdev);
6518 struct mii_ioctl_data *data = if_mii(ifr);
6519
6520 if (adapter->hw.phy.media_type != e1000_media_type_copper)
6521 return -EOPNOTSUPP;
6522
6523 switch (cmd) {
6524 case SIOCGMIIPHY:
6525 data->phy_id = adapter->hw.phy.addr;
6526 break;
6527 case SIOCGMIIREG:
Alexander Duyckf5f4cf02008-11-21 21:30:24 -08006528 if (igb_read_phy_reg(&adapter->hw, data->reg_num & 0x1F,
6529 &data->val_out))
Auke Kok9d5c8242008-01-24 02:22:38 -08006530 return -EIO;
6531 break;
6532 case SIOCSMIIREG:
6533 default:
6534 return -EOPNOTSUPP;
6535 }
6536 return 0;
6537}
6538
6539/**
6540 * igb_ioctl -
6541 * @netdev:
6542 * @ifreq:
6543 * @cmd:
6544 **/
6545static int igb_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
6546{
6547 switch (cmd) {
6548 case SIOCGMIIPHY:
6549 case SIOCGMIIREG:
6550 case SIOCSMIIREG:
6551 return igb_mii_ioctl(netdev, ifr, cmd);
Patrick Ohlyc6cb0902009-02-12 05:03:42 +00006552 case SIOCSHWTSTAMP:
Matthew Vicka79f4f82012-08-10 05:40:44 +00006553 return igb_ptp_hwtstamp_ioctl(netdev, ifr, cmd);
Auke Kok9d5c8242008-01-24 02:22:38 -08006554 default:
6555 return -EOPNOTSUPP;
6556 }
6557}
6558
Alexander Duyck009bc062009-07-23 18:08:35 +00006559s32 igb_read_pcie_cap_reg(struct e1000_hw *hw, u32 reg, u16 *value)
6560{
6561 struct igb_adapter *adapter = hw->back;
Alexander Duyck009bc062009-07-23 18:08:35 +00006562
Jiang Liu23d028c2012-08-20 13:32:20 -06006563 if (pcie_capability_read_word(adapter->pdev, reg, value))
Alexander Duyck009bc062009-07-23 18:08:35 +00006564 return -E1000_ERR_CONFIG;
6565
Alexander Duyck009bc062009-07-23 18:08:35 +00006566 return 0;
6567}
6568
6569s32 igb_write_pcie_cap_reg(struct e1000_hw *hw, u32 reg, u16 *value)
6570{
6571 struct igb_adapter *adapter = hw->back;
Alexander Duyck009bc062009-07-23 18:08:35 +00006572
Jiang Liu23d028c2012-08-20 13:32:20 -06006573 if (pcie_capability_write_word(adapter->pdev, reg, *value))
Alexander Duyck009bc062009-07-23 18:08:35 +00006574 return -E1000_ERR_CONFIG;
6575
Alexander Duyck009bc062009-07-23 18:08:35 +00006576 return 0;
6577}
6578
Michał Mirosławc8f44af2011-11-15 15:29:55 +00006579static void igb_vlan_mode(struct net_device *netdev, netdev_features_t features)
Auke Kok9d5c8242008-01-24 02:22:38 -08006580{
6581 struct igb_adapter *adapter = netdev_priv(netdev);
6582 struct e1000_hw *hw = &adapter->hw;
6583 u32 ctrl, rctl;
Alexander Duyck5faf0302011-08-26 07:46:08 +00006584 bool enable = !!(features & NETIF_F_HW_VLAN_RX);
Auke Kok9d5c8242008-01-24 02:22:38 -08006585
Alexander Duyck5faf0302011-08-26 07:46:08 +00006586 if (enable) {
Auke Kok9d5c8242008-01-24 02:22:38 -08006587 /* enable VLAN tag insert/strip */
6588 ctrl = rd32(E1000_CTRL);
6589 ctrl |= E1000_CTRL_VME;
6590 wr32(E1000_CTRL, ctrl);
6591
Alexander Duyck51466232009-10-27 23:47:35 +00006592 /* Disable CFI check */
Auke Kok9d5c8242008-01-24 02:22:38 -08006593 rctl = rd32(E1000_RCTL);
Auke Kok9d5c8242008-01-24 02:22:38 -08006594 rctl &= ~E1000_RCTL_CFIEN;
6595 wr32(E1000_RCTL, rctl);
Auke Kok9d5c8242008-01-24 02:22:38 -08006596 } else {
6597 /* disable VLAN tag insert/strip */
6598 ctrl = rd32(E1000_CTRL);
6599 ctrl &= ~E1000_CTRL_VME;
6600 wr32(E1000_CTRL, ctrl);
Auke Kok9d5c8242008-01-24 02:22:38 -08006601 }
6602
Alexander Duycke1739522009-02-19 20:39:44 -08006603 igb_rlpml_set(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08006604}
6605
Jiri Pirko8e586132011-12-08 19:52:37 -05006606static int igb_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
Auke Kok9d5c8242008-01-24 02:22:38 -08006607{
6608 struct igb_adapter *adapter = netdev_priv(netdev);
6609 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006610 int pf_id = adapter->vfs_allocated_count;
Auke Kok9d5c8242008-01-24 02:22:38 -08006611
Alexander Duyck51466232009-10-27 23:47:35 +00006612 /* attempt to add filter to vlvf array */
6613 igb_vlvf_set(adapter, vid, true, pf_id);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006614
Alexander Duyck51466232009-10-27 23:47:35 +00006615 /* add the filter since PF can receive vlans w/o entry in vlvf */
6616 igb_vfta_set(hw, vid, true);
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00006617
6618 set_bit(vid, adapter->active_vlans);
Jiri Pirko8e586132011-12-08 19:52:37 -05006619
6620 return 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08006621}
6622
Jiri Pirko8e586132011-12-08 19:52:37 -05006623static int igb_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
Auke Kok9d5c8242008-01-24 02:22:38 -08006624{
6625 struct igb_adapter *adapter = netdev_priv(netdev);
6626 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006627 int pf_id = adapter->vfs_allocated_count;
Alexander Duyck51466232009-10-27 23:47:35 +00006628 s32 err;
Auke Kok9d5c8242008-01-24 02:22:38 -08006629
Alexander Duyck51466232009-10-27 23:47:35 +00006630 /* remove vlan from VLVF table array */
6631 err = igb_vlvf_set(adapter, vid, false, pf_id);
Auke Kok9d5c8242008-01-24 02:22:38 -08006632
Alexander Duyck51466232009-10-27 23:47:35 +00006633 /* if vid was not present in VLVF just remove it from table */
6634 if (err)
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006635 igb_vfta_set(hw, vid, false);
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00006636
6637 clear_bit(vid, adapter->active_vlans);
Jiri Pirko8e586132011-12-08 19:52:37 -05006638
6639 return 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08006640}
6641
6642static void igb_restore_vlan(struct igb_adapter *adapter)
6643{
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00006644 u16 vid;
Auke Kok9d5c8242008-01-24 02:22:38 -08006645
Alexander Duyck5faf0302011-08-26 07:46:08 +00006646 igb_vlan_mode(adapter->netdev, adapter->netdev->features);
6647
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00006648 for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
6649 igb_vlan_rx_add_vid(adapter->netdev, vid);
Auke Kok9d5c8242008-01-24 02:22:38 -08006650}
6651
David Decotigny14ad2512011-04-27 18:32:43 +00006652int igb_set_spd_dplx(struct igb_adapter *adapter, u32 spd, u8 dplx)
Auke Kok9d5c8242008-01-24 02:22:38 -08006653{
Alexander Duyck090b1792009-10-27 23:51:55 +00006654 struct pci_dev *pdev = adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08006655 struct e1000_mac_info *mac = &adapter->hw.mac;
6656
6657 mac->autoneg = 0;
6658
David Decotigny14ad2512011-04-27 18:32:43 +00006659 /* Make sure dplx is at most 1 bit and lsb of speed is not set
6660 * for the switch() below to work */
6661 if ((spd & 1) || (dplx & ~1))
6662 goto err_inval;
6663
Carolyn Wybornycd2638a2010-10-12 22:27:02 +00006664 /* Fiber NIC's only allow 1000 Gbps Full duplex */
6665 if ((adapter->hw.phy.media_type == e1000_media_type_internal_serdes) &&
David Decotigny14ad2512011-04-27 18:32:43 +00006666 spd != SPEED_1000 &&
6667 dplx != DUPLEX_FULL)
6668 goto err_inval;
Carolyn Wybornycd2638a2010-10-12 22:27:02 +00006669
David Decotigny14ad2512011-04-27 18:32:43 +00006670 switch (spd + dplx) {
Auke Kok9d5c8242008-01-24 02:22:38 -08006671 case SPEED_10 + DUPLEX_HALF:
6672 mac->forced_speed_duplex = ADVERTISE_10_HALF;
6673 break;
6674 case SPEED_10 + DUPLEX_FULL:
6675 mac->forced_speed_duplex = ADVERTISE_10_FULL;
6676 break;
6677 case SPEED_100 + DUPLEX_HALF:
6678 mac->forced_speed_duplex = ADVERTISE_100_HALF;
6679 break;
6680 case SPEED_100 + DUPLEX_FULL:
6681 mac->forced_speed_duplex = ADVERTISE_100_FULL;
6682 break;
6683 case SPEED_1000 + DUPLEX_FULL:
6684 mac->autoneg = 1;
6685 adapter->hw.phy.autoneg_advertised = ADVERTISE_1000_FULL;
6686 break;
6687 case SPEED_1000 + DUPLEX_HALF: /* not supported */
6688 default:
David Decotigny14ad2512011-04-27 18:32:43 +00006689 goto err_inval;
Auke Kok9d5c8242008-01-24 02:22:38 -08006690 }
Jesse Brandeburg8376dad2012-07-26 02:31:19 +00006691
6692 /* clear MDI, MDI(-X) override is only allowed when autoneg enabled */
6693 adapter->hw.phy.mdix = AUTO_ALL_MODES;
6694
Auke Kok9d5c8242008-01-24 02:22:38 -08006695 return 0;
David Decotigny14ad2512011-04-27 18:32:43 +00006696
6697err_inval:
6698 dev_err(&pdev->dev, "Unsupported Speed/Duplex configuration\n");
6699 return -EINVAL;
Auke Kok9d5c8242008-01-24 02:22:38 -08006700}
6701
Yan, Zheng749ab2c2012-01-04 20:23:37 +00006702static int __igb_shutdown(struct pci_dev *pdev, bool *enable_wake,
6703 bool runtime)
Auke Kok9d5c8242008-01-24 02:22:38 -08006704{
6705 struct net_device *netdev = pci_get_drvdata(pdev);
6706 struct igb_adapter *adapter = netdev_priv(netdev);
6707 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck2d064c02008-07-08 15:10:12 -07006708 u32 ctrl, rctl, status;
Yan, Zheng749ab2c2012-01-04 20:23:37 +00006709 u32 wufc = runtime ? E1000_WUFC_LNKC : adapter->wol;
Auke Kok9d5c8242008-01-24 02:22:38 -08006710#ifdef CONFIG_PM
6711 int retval = 0;
6712#endif
6713
6714 netif_device_detach(netdev);
6715
Alexander Duycka88f10e2008-07-08 15:13:38 -07006716 if (netif_running(netdev))
Yan, Zheng749ab2c2012-01-04 20:23:37 +00006717 __igb_close(netdev, true);
Alexander Duycka88f10e2008-07-08 15:13:38 -07006718
Alexander Duyck047e0032009-10-27 15:49:27 +00006719 igb_clear_interrupt_scheme(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08006720
6721#ifdef CONFIG_PM
6722 retval = pci_save_state(pdev);
6723 if (retval)
6724 return retval;
6725#endif
6726
6727 status = rd32(E1000_STATUS);
6728 if (status & E1000_STATUS_LU)
6729 wufc &= ~E1000_WUFC_LNKC;
6730
6731 if (wufc) {
6732 igb_setup_rctl(adapter);
Alexander Duyckff41f8d2009-09-03 14:48:56 +00006733 igb_set_rx_mode(netdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08006734
6735 /* turn on all-multi mode if wake on multicast is enabled */
6736 if (wufc & E1000_WUFC_MC) {
6737 rctl = rd32(E1000_RCTL);
6738 rctl |= E1000_RCTL_MPE;
6739 wr32(E1000_RCTL, rctl);
6740 }
6741
6742 ctrl = rd32(E1000_CTRL);
6743 /* advertise wake from D3Cold */
6744 #define E1000_CTRL_ADVD3WUC 0x00100000
6745 /* phy power management enable */
6746 #define E1000_CTRL_EN_PHY_PWR_MGMT 0x00200000
6747 ctrl |= E1000_CTRL_ADVD3WUC;
6748 wr32(E1000_CTRL, ctrl);
6749
Auke Kok9d5c8242008-01-24 02:22:38 -08006750 /* Allow time for pending master requests to run */
Alexander Duyck330a6d62009-10-27 23:51:35 +00006751 igb_disable_pcie_master(hw);
Auke Kok9d5c8242008-01-24 02:22:38 -08006752
6753 wr32(E1000_WUC, E1000_WUC_PME_EN);
6754 wr32(E1000_WUFC, wufc);
Auke Kok9d5c8242008-01-24 02:22:38 -08006755 } else {
6756 wr32(E1000_WUC, 0);
6757 wr32(E1000_WUFC, 0);
Auke Kok9d5c8242008-01-24 02:22:38 -08006758 }
6759
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00006760 *enable_wake = wufc || adapter->en_mng_pt;
6761 if (!*enable_wake)
Nick Nunley88a268c2010-02-17 01:01:59 +00006762 igb_power_down_link(adapter);
6763 else
6764 igb_power_up_link(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08006765
6766 /* Release control of h/w to f/w. If f/w is AMT enabled, this
6767 * would have already happened in close and is redundant. */
6768 igb_release_hw_control(adapter);
6769
6770 pci_disable_device(pdev);
6771
Auke Kok9d5c8242008-01-24 02:22:38 -08006772 return 0;
6773}
6774
6775#ifdef CONFIG_PM
Emil Tantilovd9dd9662012-01-28 08:10:35 +00006776#ifdef CONFIG_PM_SLEEP
Yan, Zheng749ab2c2012-01-04 20:23:37 +00006777static int igb_suspend(struct device *dev)
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00006778{
6779 int retval;
6780 bool wake;
Yan, Zheng749ab2c2012-01-04 20:23:37 +00006781 struct pci_dev *pdev = to_pci_dev(dev);
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00006782
Yan, Zheng749ab2c2012-01-04 20:23:37 +00006783 retval = __igb_shutdown(pdev, &wake, 0);
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00006784 if (retval)
6785 return retval;
6786
6787 if (wake) {
6788 pci_prepare_to_sleep(pdev);
6789 } else {
6790 pci_wake_from_d3(pdev, false);
6791 pci_set_power_state(pdev, PCI_D3hot);
6792 }
6793
6794 return 0;
6795}
Emil Tantilovd9dd9662012-01-28 08:10:35 +00006796#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00006797
Yan, Zheng749ab2c2012-01-04 20:23:37 +00006798static int igb_resume(struct device *dev)
Auke Kok9d5c8242008-01-24 02:22:38 -08006799{
Yan, Zheng749ab2c2012-01-04 20:23:37 +00006800 struct pci_dev *pdev = to_pci_dev(dev);
Auke Kok9d5c8242008-01-24 02:22:38 -08006801 struct net_device *netdev = pci_get_drvdata(pdev);
6802 struct igb_adapter *adapter = netdev_priv(netdev);
6803 struct e1000_hw *hw = &adapter->hw;
6804 u32 err;
6805
6806 pci_set_power_state(pdev, PCI_D0);
6807 pci_restore_state(pdev);
Nick Nunleyb94f2d72010-02-17 01:02:19 +00006808 pci_save_state(pdev);
Taku Izumi42bfd33a2008-06-20 12:10:30 +09006809
Alexander Duyckaed5dec2009-02-06 23:16:04 +00006810 err = pci_enable_device_mem(pdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08006811 if (err) {
6812 dev_err(&pdev->dev,
6813 "igb: Cannot enable PCI device from suspend\n");
6814 return err;
6815 }
6816 pci_set_master(pdev);
6817
6818 pci_enable_wake(pdev, PCI_D3hot, 0);
6819 pci_enable_wake(pdev, PCI_D3cold, 0);
6820
Benjamin Poiriercfb8c3a2012-05-10 15:38:37 +00006821 if (igb_init_interrupt_scheme(adapter)) {
Alexander Duycka88f10e2008-07-08 15:13:38 -07006822 dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
6823 return -ENOMEM;
Auke Kok9d5c8242008-01-24 02:22:38 -08006824 }
6825
Auke Kok9d5c8242008-01-24 02:22:38 -08006826 igb_reset(adapter);
Alexander Duycka8564f02009-02-06 23:21:10 +00006827
6828 /* let the f/w know that the h/w is now under the control of the
6829 * driver. */
6830 igb_get_hw_control(adapter);
6831
Auke Kok9d5c8242008-01-24 02:22:38 -08006832 wr32(E1000_WUS, ~0);
6833
Yan, Zheng749ab2c2012-01-04 20:23:37 +00006834 if (netdev->flags & IFF_UP) {
Alexander Duyck0c2cc022012-09-25 00:31:22 +00006835 rtnl_lock();
Yan, Zheng749ab2c2012-01-04 20:23:37 +00006836 err = __igb_open(netdev, true);
Alexander Duyck0c2cc022012-09-25 00:31:22 +00006837 rtnl_unlock();
Alexander Duycka88f10e2008-07-08 15:13:38 -07006838 if (err)
6839 return err;
6840 }
Auke Kok9d5c8242008-01-24 02:22:38 -08006841
6842 netif_device_attach(netdev);
Yan, Zheng749ab2c2012-01-04 20:23:37 +00006843 return 0;
6844}
6845
6846#ifdef CONFIG_PM_RUNTIME
6847static int igb_runtime_idle(struct device *dev)
6848{
6849 struct pci_dev *pdev = to_pci_dev(dev);
6850 struct net_device *netdev = pci_get_drvdata(pdev);
6851 struct igb_adapter *adapter = netdev_priv(netdev);
6852
6853 if (!igb_has_link(adapter))
6854 pm_schedule_suspend(dev, MSEC_PER_SEC * 5);
6855
6856 return -EBUSY;
6857}
6858
6859static int igb_runtime_suspend(struct device *dev)
6860{
6861 struct pci_dev *pdev = to_pci_dev(dev);
6862 int retval;
6863 bool wake;
6864
6865 retval = __igb_shutdown(pdev, &wake, 1);
6866 if (retval)
6867 return retval;
6868
6869 if (wake) {
6870 pci_prepare_to_sleep(pdev);
6871 } else {
6872 pci_wake_from_d3(pdev, false);
6873 pci_set_power_state(pdev, PCI_D3hot);
6874 }
Auke Kok9d5c8242008-01-24 02:22:38 -08006875
Auke Kok9d5c8242008-01-24 02:22:38 -08006876 return 0;
6877}
Yan, Zheng749ab2c2012-01-04 20:23:37 +00006878
6879static int igb_runtime_resume(struct device *dev)
6880{
6881 return igb_resume(dev);
6882}
6883#endif /* CONFIG_PM_RUNTIME */
Auke Kok9d5c8242008-01-24 02:22:38 -08006884#endif
6885
6886static void igb_shutdown(struct pci_dev *pdev)
6887{
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00006888 bool wake;
6889
Yan, Zheng749ab2c2012-01-04 20:23:37 +00006890 __igb_shutdown(pdev, &wake, 0);
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00006891
6892 if (system_state == SYSTEM_POWER_OFF) {
6893 pci_wake_from_d3(pdev, wake);
6894 pci_set_power_state(pdev, PCI_D3hot);
6895 }
Auke Kok9d5c8242008-01-24 02:22:38 -08006896}
6897
6898#ifdef CONFIG_NET_POLL_CONTROLLER
6899/*
6900 * Polling 'interrupt' - used by things like netconsole to send skbs
6901 * without having to re-enable interrupts. It's not called while
6902 * the interrupt routine is executing.
6903 */
6904static void igb_netpoll(struct net_device *netdev)
6905{
6906 struct igb_adapter *adapter = netdev_priv(netdev);
Alexander Duyckeebbbdb2009-02-06 23:19:29 +00006907 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck0d1ae7f2011-08-26 07:46:34 +00006908 struct igb_q_vector *q_vector;
Auke Kok9d5c8242008-01-24 02:22:38 -08006909 int i;
Auke Kok9d5c8242008-01-24 02:22:38 -08006910
Alexander Duyck047e0032009-10-27 15:49:27 +00006911 for (i = 0; i < adapter->num_q_vectors; i++) {
Alexander Duyck0d1ae7f2011-08-26 07:46:34 +00006912 q_vector = adapter->q_vector[i];
6913 if (adapter->msix_entries)
6914 wr32(E1000_EIMC, q_vector->eims_value);
6915 else
6916 igb_irq_disable(adapter);
Alexander Duyck047e0032009-10-27 15:49:27 +00006917 napi_schedule(&q_vector->napi);
Alexander Duyckeebbbdb2009-02-06 23:19:29 +00006918 }
Auke Kok9d5c8242008-01-24 02:22:38 -08006919}
6920#endif /* CONFIG_NET_POLL_CONTROLLER */
6921
6922/**
6923 * igb_io_error_detected - called when PCI error is detected
6924 * @pdev: Pointer to PCI device
6925 * @state: The current pci connection state
6926 *
6927 * This function is called after a PCI bus error affecting
6928 * this device has been detected.
6929 */
6930static pci_ers_result_t igb_io_error_detected(struct pci_dev *pdev,
6931 pci_channel_state_t state)
6932{
6933 struct net_device *netdev = pci_get_drvdata(pdev);
6934 struct igb_adapter *adapter = netdev_priv(netdev);
6935
6936 netif_device_detach(netdev);
6937
Alexander Duyck59ed6ee2009-06-30 12:46:34 +00006938 if (state == pci_channel_io_perm_failure)
6939 return PCI_ERS_RESULT_DISCONNECT;
6940
Auke Kok9d5c8242008-01-24 02:22:38 -08006941 if (netif_running(netdev))
6942 igb_down(adapter);
6943 pci_disable_device(pdev);
6944
6945 /* Request a slot slot reset. */
6946 return PCI_ERS_RESULT_NEED_RESET;
6947}
6948
6949/**
6950 * igb_io_slot_reset - called after the pci bus has been reset.
6951 * @pdev: Pointer to PCI device
6952 *
6953 * Restart the card from scratch, as if from a cold-boot. Implementation
6954 * resembles the first-half of the igb_resume routine.
6955 */
6956static pci_ers_result_t igb_io_slot_reset(struct pci_dev *pdev)
6957{
6958 struct net_device *netdev = pci_get_drvdata(pdev);
6959 struct igb_adapter *adapter = netdev_priv(netdev);
6960 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck40a914f2008-11-27 00:24:37 -08006961 pci_ers_result_t result;
Taku Izumi42bfd33a2008-06-20 12:10:30 +09006962 int err;
Auke Kok9d5c8242008-01-24 02:22:38 -08006963
Alexander Duyckaed5dec2009-02-06 23:16:04 +00006964 if (pci_enable_device_mem(pdev)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08006965 dev_err(&pdev->dev,
6966 "Cannot re-enable PCI device after reset.\n");
Alexander Duyck40a914f2008-11-27 00:24:37 -08006967 result = PCI_ERS_RESULT_DISCONNECT;
6968 } else {
6969 pci_set_master(pdev);
6970 pci_restore_state(pdev);
Nick Nunleyb94f2d72010-02-17 01:02:19 +00006971 pci_save_state(pdev);
Alexander Duyck40a914f2008-11-27 00:24:37 -08006972
6973 pci_enable_wake(pdev, PCI_D3hot, 0);
6974 pci_enable_wake(pdev, PCI_D3cold, 0);
6975
6976 igb_reset(adapter);
6977 wr32(E1000_WUS, ~0);
6978 result = PCI_ERS_RESULT_RECOVERED;
Auke Kok9d5c8242008-01-24 02:22:38 -08006979 }
Auke Kok9d5c8242008-01-24 02:22:38 -08006980
Jeff Kirsherea943d42008-12-11 20:34:19 -08006981 err = pci_cleanup_aer_uncorrect_error_status(pdev);
6982 if (err) {
6983 dev_err(&pdev->dev, "pci_cleanup_aer_uncorrect_error_status "
6984 "failed 0x%0x\n", err);
6985 /* non-fatal, continue */
6986 }
Auke Kok9d5c8242008-01-24 02:22:38 -08006987
Alexander Duyck40a914f2008-11-27 00:24:37 -08006988 return result;
Auke Kok9d5c8242008-01-24 02:22:38 -08006989}
6990
6991/**
6992 * igb_io_resume - called when traffic can start flowing again.
6993 * @pdev: Pointer to PCI device
6994 *
6995 * This callback is called when the error recovery driver tells us that
6996 * its OK to resume normal operation. Implementation resembles the
6997 * second-half of the igb_resume routine.
6998 */
6999static void igb_io_resume(struct pci_dev *pdev)
7000{
7001 struct net_device *netdev = pci_get_drvdata(pdev);
7002 struct igb_adapter *adapter = netdev_priv(netdev);
7003
Auke Kok9d5c8242008-01-24 02:22:38 -08007004 if (netif_running(netdev)) {
7005 if (igb_up(adapter)) {
7006 dev_err(&pdev->dev, "igb_up failed after reset\n");
7007 return;
7008 }
7009 }
7010
7011 netif_device_attach(netdev);
7012
7013 /* let the f/w know that the h/w is now under the control of the
7014 * driver. */
7015 igb_get_hw_control(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08007016}
7017
Alexander Duyck26ad9172009-10-05 06:32:49 +00007018static void igb_rar_set_qsel(struct igb_adapter *adapter, u8 *addr, u32 index,
7019 u8 qsel)
7020{
7021 u32 rar_low, rar_high;
7022 struct e1000_hw *hw = &adapter->hw;
7023
7024 /* HW expects these in little endian so we reverse the byte order
7025 * from network order (big endian) to little endian
7026 */
7027 rar_low = ((u32) addr[0] | ((u32) addr[1] << 8) |
7028 ((u32) addr[2] << 16) | ((u32) addr[3] << 24));
7029 rar_high = ((u32) addr[4] | ((u32) addr[5] << 8));
7030
7031 /* Indicate to hardware the Address is Valid. */
7032 rar_high |= E1000_RAH_AV;
7033
7034 if (hw->mac.type == e1000_82575)
7035 rar_high |= E1000_RAH_POOL_1 * qsel;
7036 else
7037 rar_high |= E1000_RAH_POOL_1 << qsel;
7038
7039 wr32(E1000_RAL(index), rar_low);
7040 wrfl();
7041 wr32(E1000_RAH(index), rar_high);
7042 wrfl();
7043}
7044
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007045static int igb_set_vf_mac(struct igb_adapter *adapter,
7046 int vf, unsigned char *mac_addr)
7047{
7048 struct e1000_hw *hw = &adapter->hw;
Alexander Duyckff41f8d2009-09-03 14:48:56 +00007049 /* VF MAC addresses start at end of receive addresses and moves
7050 * torwards the first, as a result a collision should not be possible */
7051 int rar_entry = hw->mac.rar_entry_count - (vf + 1);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007052
Alexander Duyck37680112009-02-19 20:40:30 -08007053 memcpy(adapter->vf_data[vf].vf_mac_addresses, mac_addr, ETH_ALEN);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007054
Alexander Duyck26ad9172009-10-05 06:32:49 +00007055 igb_rar_set_qsel(adapter, mac_addr, rar_entry, vf);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007056
7057 return 0;
7058}
7059
Williams, Mitch A8151d292010-02-10 01:44:24 +00007060static int igb_ndo_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
7061{
7062 struct igb_adapter *adapter = netdev_priv(netdev);
7063 if (!is_valid_ether_addr(mac) || (vf >= adapter->vfs_allocated_count))
7064 return -EINVAL;
7065 adapter->vf_data[vf].flags |= IGB_VF_FLAG_PF_SET_MAC;
7066 dev_info(&adapter->pdev->dev, "setting MAC %pM on VF %d\n", mac, vf);
7067 dev_info(&adapter->pdev->dev, "Reload the VF driver to make this"
7068 " change effective.");
7069 if (test_bit(__IGB_DOWN, &adapter->state)) {
7070 dev_warn(&adapter->pdev->dev, "The VF MAC address has been set,"
7071 " but the PF device is not up.\n");
7072 dev_warn(&adapter->pdev->dev, "Bring the PF device up before"
7073 " attempting to use the VF device.\n");
7074 }
7075 return igb_set_vf_mac(adapter, vf, mac);
7076}
7077
Lior Levy17dc5662011-02-08 02:28:46 +00007078static int igb_link_mbps(int internal_link_speed)
7079{
7080 switch (internal_link_speed) {
7081 case SPEED_100:
7082 return 100;
7083 case SPEED_1000:
7084 return 1000;
7085 default:
7086 return 0;
7087 }
7088}
7089
7090static void igb_set_vf_rate_limit(struct e1000_hw *hw, int vf, int tx_rate,
7091 int link_speed)
7092{
7093 int rf_dec, rf_int;
7094 u32 bcnrc_val;
7095
7096 if (tx_rate != 0) {
7097 /* Calculate the rate factor values to set */
7098 rf_int = link_speed / tx_rate;
7099 rf_dec = (link_speed - (rf_int * tx_rate));
7100 rf_dec = (rf_dec * (1<<E1000_RTTBCNRC_RF_INT_SHIFT)) / tx_rate;
7101
7102 bcnrc_val = E1000_RTTBCNRC_RS_ENA;
7103 bcnrc_val |= ((rf_int<<E1000_RTTBCNRC_RF_INT_SHIFT) &
7104 E1000_RTTBCNRC_RF_INT_MASK);
7105 bcnrc_val |= (rf_dec & E1000_RTTBCNRC_RF_DEC_MASK);
7106 } else {
7107 bcnrc_val = 0;
7108 }
7109
7110 wr32(E1000_RTTDQSEL, vf); /* vf X uses queue X */
Lior Levyf00b0da2011-06-04 06:05:03 +00007111 /*
7112 * Set global transmit compensation time to the MMW_SIZE in RTTBCNRM
7113 * register. MMW_SIZE=0x014 if 9728-byte jumbo is supported.
7114 */
7115 wr32(E1000_RTTBCNRM, 0x14);
Lior Levy17dc5662011-02-08 02:28:46 +00007116 wr32(E1000_RTTBCNRC, bcnrc_val);
7117}
7118
7119static void igb_check_vf_rate_limit(struct igb_adapter *adapter)
7120{
7121 int actual_link_speed, i;
7122 bool reset_rate = false;
7123
7124 /* VF TX rate limit was not set or not supported */
7125 if ((adapter->vf_rate_link_speed == 0) ||
7126 (adapter->hw.mac.type != e1000_82576))
7127 return;
7128
7129 actual_link_speed = igb_link_mbps(adapter->link_speed);
7130 if (actual_link_speed != adapter->vf_rate_link_speed) {
7131 reset_rate = true;
7132 adapter->vf_rate_link_speed = 0;
7133 dev_info(&adapter->pdev->dev,
7134 "Link speed has been changed. VF Transmit "
7135 "rate is disabled\n");
7136 }
7137
7138 for (i = 0; i < adapter->vfs_allocated_count; i++) {
7139 if (reset_rate)
7140 adapter->vf_data[i].tx_rate = 0;
7141
7142 igb_set_vf_rate_limit(&adapter->hw, i,
7143 adapter->vf_data[i].tx_rate,
7144 actual_link_speed);
7145 }
7146}
7147
Williams, Mitch A8151d292010-02-10 01:44:24 +00007148static int igb_ndo_set_vf_bw(struct net_device *netdev, int vf, int tx_rate)
7149{
Lior Levy17dc5662011-02-08 02:28:46 +00007150 struct igb_adapter *adapter = netdev_priv(netdev);
7151 struct e1000_hw *hw = &adapter->hw;
7152 int actual_link_speed;
7153
7154 if (hw->mac.type != e1000_82576)
7155 return -EOPNOTSUPP;
7156
7157 actual_link_speed = igb_link_mbps(adapter->link_speed);
7158 if ((vf >= adapter->vfs_allocated_count) ||
7159 (!(rd32(E1000_STATUS) & E1000_STATUS_LU)) ||
7160 (tx_rate < 0) || (tx_rate > actual_link_speed))
7161 return -EINVAL;
7162
7163 adapter->vf_rate_link_speed = actual_link_speed;
7164 adapter->vf_data[vf].tx_rate = (u16)tx_rate;
7165 igb_set_vf_rate_limit(hw, vf, tx_rate, actual_link_speed);
7166
7167 return 0;
Williams, Mitch A8151d292010-02-10 01:44:24 +00007168}
7169
7170static int igb_ndo_get_vf_config(struct net_device *netdev,
7171 int vf, struct ifla_vf_info *ivi)
7172{
7173 struct igb_adapter *adapter = netdev_priv(netdev);
7174 if (vf >= adapter->vfs_allocated_count)
7175 return -EINVAL;
7176 ivi->vf = vf;
7177 memcpy(&ivi->mac, adapter->vf_data[vf].vf_mac_addresses, ETH_ALEN);
Lior Levy17dc5662011-02-08 02:28:46 +00007178 ivi->tx_rate = adapter->vf_data[vf].tx_rate;
Williams, Mitch A8151d292010-02-10 01:44:24 +00007179 ivi->vlan = adapter->vf_data[vf].pf_vlan;
7180 ivi->qos = adapter->vf_data[vf].pf_qos;
7181 return 0;
7182}
7183
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007184static void igb_vmm_control(struct igb_adapter *adapter)
7185{
7186 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck10d8e902009-10-27 15:54:04 +00007187 u32 reg;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007188
Alexander Duyck52a1dd42010-03-22 14:07:46 +00007189 switch (hw->mac.type) {
7190 case e1000_82575:
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00007191 case e1000_i210:
7192 case e1000_i211:
Alexander Duyck52a1dd42010-03-22 14:07:46 +00007193 default:
7194 /* replication is not supported for 82575 */
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007195 return;
Alexander Duyck52a1dd42010-03-22 14:07:46 +00007196 case e1000_82576:
7197 /* notify HW that the MAC is adding vlan tags */
7198 reg = rd32(E1000_DTXCTL);
7199 reg |= E1000_DTXCTL_VLAN_ADDED;
7200 wr32(E1000_DTXCTL, reg);
7201 case e1000_82580:
7202 /* enable replication vlan tag stripping */
7203 reg = rd32(E1000_RPLOLR);
7204 reg |= E1000_RPLOLR_STRVLAN;
7205 wr32(E1000_RPLOLR, reg);
Alexander Duyckd2ba2ed2010-03-22 14:08:06 +00007206 case e1000_i350:
7207 /* none of the above registers are supported by i350 */
Alexander Duyck52a1dd42010-03-22 14:07:46 +00007208 break;
7209 }
Alexander Duyck10d8e902009-10-27 15:54:04 +00007210
Alexander Duyckd4960302009-10-27 15:53:45 +00007211 if (adapter->vfs_allocated_count) {
7212 igb_vmdq_set_loopback_pf(hw, true);
7213 igb_vmdq_set_replication_pf(hw, true);
Greg Rose13800462010-11-06 02:08:26 +00007214 igb_vmdq_set_anti_spoofing_pf(hw, true,
7215 adapter->vfs_allocated_count);
Alexander Duyckd4960302009-10-27 15:53:45 +00007216 } else {
7217 igb_vmdq_set_loopback_pf(hw, false);
7218 igb_vmdq_set_replication_pf(hw, false);
7219 }
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007220}
7221
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +00007222static void igb_init_dmac(struct igb_adapter *adapter, u32 pba)
7223{
7224 struct e1000_hw *hw = &adapter->hw;
7225 u32 dmac_thr;
7226 u16 hwm;
7227
7228 if (hw->mac.type > e1000_82580) {
7229 if (adapter->flags & IGB_FLAG_DMAC) {
7230 u32 reg;
7231
7232 /* force threshold to 0. */
7233 wr32(E1000_DMCTXTH, 0);
7234
7235 /*
Matthew Vicke8c626e2011-11-17 08:33:12 +00007236 * DMA Coalescing high water mark needs to be greater
7237 * than the Rx threshold. Set hwm to PBA - max frame
7238 * size in 16B units, capping it at PBA - 6KB.
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +00007239 */
Matthew Vicke8c626e2011-11-17 08:33:12 +00007240 hwm = 64 * pba - adapter->max_frame_size / 16;
7241 if (hwm < 64 * (pba - 6))
7242 hwm = 64 * (pba - 6);
7243 reg = rd32(E1000_FCRTC);
7244 reg &= ~E1000_FCRTC_RTH_COAL_MASK;
7245 reg |= ((hwm << E1000_FCRTC_RTH_COAL_SHIFT)
7246 & E1000_FCRTC_RTH_COAL_MASK);
7247 wr32(E1000_FCRTC, reg);
7248
7249 /*
7250 * Set the DMA Coalescing Rx threshold to PBA - 2 * max
7251 * frame size, capping it at PBA - 10KB.
7252 */
7253 dmac_thr = pba - adapter->max_frame_size / 512;
7254 if (dmac_thr < pba - 10)
7255 dmac_thr = pba - 10;
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +00007256 reg = rd32(E1000_DMACR);
7257 reg &= ~E1000_DMACR_DMACTHR_MASK;
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +00007258 reg |= ((dmac_thr << E1000_DMACR_DMACTHR_SHIFT)
7259 & E1000_DMACR_DMACTHR_MASK);
7260
7261 /* transition to L0x or L1 if available..*/
7262 reg |= (E1000_DMACR_DMAC_EN | E1000_DMACR_DMAC_LX_MASK);
7263
7264 /* watchdog timer= +-1000 usec in 32usec intervals */
7265 reg |= (1000 >> 5);
Matthew Vick0c02dd92012-04-14 05:20:32 +00007266
7267 /* Disable BMC-to-OS Watchdog Enable */
7268 reg &= ~E1000_DMACR_DC_BMC2OSW_EN;
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +00007269 wr32(E1000_DMACR, reg);
7270
7271 /*
7272 * no lower threshold to disable
7273 * coalescing(smart fifb)-UTRESH=0
7274 */
7275 wr32(E1000_DMCRTRH, 0);
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +00007276
7277 reg = (IGB_DMCTLX_DCFLUSH_DIS | 0x4);
7278
7279 wr32(E1000_DMCTLX, reg);
7280
7281 /*
7282 * free space in tx packet buffer to wake from
7283 * DMA coal
7284 */
7285 wr32(E1000_DMCTXTH, (IGB_MIN_TXPBSIZE -
7286 (IGB_TX_BUF_4096 + adapter->max_frame_size)) >> 6);
7287
7288 /*
7289 * make low power state decision controlled
7290 * by DMA coal
7291 */
7292 reg = rd32(E1000_PCIEMISC);
7293 reg &= ~E1000_PCIEMISC_LX_DECISION;
7294 wr32(E1000_PCIEMISC, reg);
7295 } /* endif adapter->dmac is not disabled */
7296 } else if (hw->mac.type == e1000_82580) {
7297 u32 reg = rd32(E1000_PCIEMISC);
7298 wr32(E1000_PCIEMISC, reg & ~E1000_PCIEMISC_LX_DECISION);
7299 wr32(E1000_DMACR, 0);
7300 }
7301}
7302
Auke Kok9d5c8242008-01-24 02:22:38 -08007303/* igb_main.c */