blob: 0fe25210629b98060041913142e7546b5348b56e [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 *);
121static void __devexit igb_remove(struct pci_dev *pdev);
122static 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,
231 .remove = __devexit_p(igb_remove),
232#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;
Auke Kok9d5c8242008-01-24 02:22:38 -08001592 u32 pba = 0, tx_space, min_tx_space, min_rx_space;
1593 u16 hwm;
1594
1595 /* Repartition Pba for greater than 9k mtu
1596 * To take effect CTRL.RST is required.
1597 */
Alexander Duyckfa4dfae2009-02-06 23:21:31 +00001598 switch (mac->type) {
Alexander Duyckd2ba2ed2010-03-22 14:08:06 +00001599 case e1000_i350:
Alexander Duyck55cac242009-11-19 12:42:21 +00001600 case e1000_82580:
1601 pba = rd32(E1000_RXPBS);
1602 pba = igb_rxpbs_adjust_82580(pba);
1603 break;
Alexander Duyckfa4dfae2009-02-06 23:21:31 +00001604 case e1000_82576:
Alexander Duyckd249be52009-10-27 23:46:38 +00001605 pba = rd32(E1000_RXPBS);
1606 pba &= E1000_RXPBS_SIZE_MASK_82576;
Alexander Duyckfa4dfae2009-02-06 23:21:31 +00001607 break;
1608 case e1000_82575:
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00001609 case e1000_i210:
1610 case e1000_i211:
Alexander Duyckfa4dfae2009-02-06 23:21:31 +00001611 default:
1612 pba = E1000_PBA_34K;
1613 break;
Alexander Duyck2d064c02008-07-08 15:10:12 -07001614 }
Auke Kok9d5c8242008-01-24 02:22:38 -08001615
Alexander Duyck2d064c02008-07-08 15:10:12 -07001616 if ((adapter->max_frame_size > ETH_FRAME_LEN + ETH_FCS_LEN) &&
1617 (mac->type < e1000_82576)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08001618 /* adjust PBA for jumbo frames */
1619 wr32(E1000_PBA, pba);
1620
1621 /* To maintain wire speed transmits, the Tx FIFO should be
1622 * large enough to accommodate two full transmit packets,
1623 * rounded up to the next 1KB and expressed in KB. Likewise,
1624 * the Rx FIFO should be large enough to accommodate at least
1625 * one full receive packet and is similarly rounded up and
1626 * expressed in KB. */
1627 pba = rd32(E1000_PBA);
1628 /* upper 16 bits has Tx packet buffer allocation size in KB */
1629 tx_space = pba >> 16;
1630 /* lower 16 bits has Rx packet buffer allocation size in KB */
1631 pba &= 0xffff;
1632 /* the tx fifo also stores 16 bytes of information about the tx
1633 * but don't include ethernet FCS because hardware appends it */
1634 min_tx_space = (adapter->max_frame_size +
Alexander Duyck85e8d002009-02-16 00:00:20 -08001635 sizeof(union e1000_adv_tx_desc) -
Auke Kok9d5c8242008-01-24 02:22:38 -08001636 ETH_FCS_LEN) * 2;
1637 min_tx_space = ALIGN(min_tx_space, 1024);
1638 min_tx_space >>= 10;
1639 /* software strips receive CRC, so leave room for it */
1640 min_rx_space = adapter->max_frame_size;
1641 min_rx_space = ALIGN(min_rx_space, 1024);
1642 min_rx_space >>= 10;
1643
1644 /* If current Tx allocation is less than the min Tx FIFO size,
1645 * and the min Tx FIFO size is less than the current Rx FIFO
1646 * allocation, take space away from current Rx allocation */
1647 if (tx_space < min_tx_space &&
1648 ((min_tx_space - tx_space) < pba)) {
1649 pba = pba - (min_tx_space - tx_space);
1650
1651 /* if short on rx space, rx wins and must trump tx
1652 * adjustment */
1653 if (pba < min_rx_space)
1654 pba = min_rx_space;
1655 }
Alexander Duyck2d064c02008-07-08 15:10:12 -07001656 wr32(E1000_PBA, pba);
Auke Kok9d5c8242008-01-24 02:22:38 -08001657 }
Auke Kok9d5c8242008-01-24 02:22:38 -08001658
1659 /* flow control settings */
1660 /* The high water mark must be low enough to fit one full frame
1661 * (or the size used for early receive) above it in the Rx FIFO.
1662 * Set it to the lower of:
1663 * - 90% of the Rx FIFO size, or
1664 * - the full Rx FIFO size minus one full frame */
1665 hwm = min(((pba << 10) * 9 / 10),
Alexander Duyck2d064c02008-07-08 15:10:12 -07001666 ((pba << 10) - 2 * adapter->max_frame_size));
Auke Kok9d5c8242008-01-24 02:22:38 -08001667
Alexander Duyckd405ea32009-12-23 13:21:27 +00001668 fc->high_water = hwm & 0xFFF0; /* 16-byte granularity */
1669 fc->low_water = fc->high_water - 16;
Auke Kok9d5c8242008-01-24 02:22:38 -08001670 fc->pause_time = 0xFFFF;
1671 fc->send_xon = 1;
Alexander Duyck0cce1192009-07-23 18:10:24 +00001672 fc->current_mode = fc->requested_mode;
Auke Kok9d5c8242008-01-24 02:22:38 -08001673
Alexander Duyck4ae196d2009-02-19 20:40:07 -08001674 /* disable receive for all VFs and wait one second */
1675 if (adapter->vfs_allocated_count) {
1676 int i;
1677 for (i = 0 ; i < adapter->vfs_allocated_count; i++)
Greg Rose8fa7e0f2010-11-06 05:43:21 +00001678 adapter->vf_data[i].flags &= IGB_VF_FLAG_PF_SET_MAC;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08001679
1680 /* ping all the active vfs to let them know we are going down */
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00001681 igb_ping_all_vfs(adapter);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08001682
1683 /* disable transmits and receives */
1684 wr32(E1000_VFRE, 0);
1685 wr32(E1000_VFTE, 0);
1686 }
1687
Auke Kok9d5c8242008-01-24 02:22:38 -08001688 /* Allow time for pending master requests to run */
Alexander Duyck330a6d62009-10-27 23:51:35 +00001689 hw->mac.ops.reset_hw(hw);
Auke Kok9d5c8242008-01-24 02:22:38 -08001690 wr32(E1000_WUC, 0);
1691
Alexander Duyck330a6d62009-10-27 23:51:35 +00001692 if (hw->mac.ops.init_hw(hw))
Alexander Duyck090b1792009-10-27 23:51:55 +00001693 dev_err(&pdev->dev, "Hardware Error\n");
Auke Kok9d5c8242008-01-24 02:22:38 -08001694
Matthew Vicka27416b2012-04-18 02:57:44 +00001695 /*
1696 * Flow control settings reset on hardware reset, so guarantee flow
1697 * control is off when forcing speed.
1698 */
1699 if (!hw->mac.autoneg)
1700 igb_force_mac_fc(hw);
1701
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +00001702 igb_init_dmac(adapter, pba);
Nick Nunley88a268c2010-02-17 01:01:59 +00001703 if (!netif_running(adapter->netdev))
1704 igb_power_down_link(adapter);
1705
Auke Kok9d5c8242008-01-24 02:22:38 -08001706 igb_update_mng_vlan(adapter);
1707
1708 /* Enable h/w to recognize an 802.1Q VLAN Ethernet packet */
1709 wr32(E1000_VET, ETHERNET_IEEE_VLAN_TYPE);
1710
Matthew Vick1f6e8172012-08-18 07:26:33 +00001711 /* Re-enable PTP, where applicable. */
1712 igb_ptp_reset(adapter);
Matthew Vick1f6e8172012-08-18 07:26:33 +00001713
Alexander Duyck330a6d62009-10-27 23:51:35 +00001714 igb_get_phy_info(hw);
Auke Kok9d5c8242008-01-24 02:22:38 -08001715}
1716
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001717static netdev_features_t igb_fix_features(struct net_device *netdev,
1718 netdev_features_t features)
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00001719{
1720 /*
1721 * Since there is no support for separate rx/tx vlan accel
1722 * enable/disable make sure tx flag is always in same state as rx.
1723 */
1724 if (features & NETIF_F_HW_VLAN_RX)
1725 features |= NETIF_F_HW_VLAN_TX;
1726 else
1727 features &= ~NETIF_F_HW_VLAN_TX;
1728
1729 return features;
1730}
1731
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001732static int igb_set_features(struct net_device *netdev,
1733 netdev_features_t features)
Michał Mirosławac52caa2011-06-08 08:38:01 +00001734{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001735 netdev_features_t changed = netdev->features ^ features;
Ben Greear89eaefb2012-03-06 09:41:58 +00001736 struct igb_adapter *adapter = netdev_priv(netdev);
Michał Mirosławac52caa2011-06-08 08:38:01 +00001737
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00001738 if (changed & NETIF_F_HW_VLAN_RX)
1739 igb_vlan_mode(netdev, features);
1740
Ben Greear89eaefb2012-03-06 09:41:58 +00001741 if (!(changed & NETIF_F_RXALL))
1742 return 0;
1743
1744 netdev->features = features;
1745
1746 if (netif_running(netdev))
1747 igb_reinit_locked(adapter);
1748 else
1749 igb_reset(adapter);
1750
Michał Mirosławac52caa2011-06-08 08:38:01 +00001751 return 0;
1752}
1753
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08001754static const struct net_device_ops igb_netdev_ops = {
Alexander Duyck559e9c42009-10-27 23:52:50 +00001755 .ndo_open = igb_open,
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08001756 .ndo_stop = igb_close,
Alexander Duyckcd392f52011-08-26 07:43:59 +00001757 .ndo_start_xmit = igb_xmit_frame,
Eric Dumazet12dcd862010-10-15 17:27:10 +00001758 .ndo_get_stats64 = igb_get_stats64,
Alexander Duyckff41f8d2009-09-03 14:48:56 +00001759 .ndo_set_rx_mode = igb_set_rx_mode,
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08001760 .ndo_set_mac_address = igb_set_mac,
1761 .ndo_change_mtu = igb_change_mtu,
1762 .ndo_do_ioctl = igb_ioctl,
1763 .ndo_tx_timeout = igb_tx_timeout,
1764 .ndo_validate_addr = eth_validate_addr,
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08001765 .ndo_vlan_rx_add_vid = igb_vlan_rx_add_vid,
1766 .ndo_vlan_rx_kill_vid = igb_vlan_rx_kill_vid,
Williams, Mitch A8151d292010-02-10 01:44:24 +00001767 .ndo_set_vf_mac = igb_ndo_set_vf_mac,
1768 .ndo_set_vf_vlan = igb_ndo_set_vf_vlan,
1769 .ndo_set_vf_tx_rate = igb_ndo_set_vf_bw,
1770 .ndo_get_vf_config = igb_ndo_get_vf_config,
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08001771#ifdef CONFIG_NET_POLL_CONTROLLER
1772 .ndo_poll_controller = igb_netpoll,
1773#endif
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00001774 .ndo_fix_features = igb_fix_features,
1775 .ndo_set_features = igb_set_features,
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08001776};
1777
Taku Izumi42bfd33a2008-06-20 12:10:30 +09001778/**
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00001779 * igb_set_fw_version - Configure version string for ethtool
1780 * @adapter: adapter struct
1781 *
1782 **/
1783void igb_set_fw_version(struct igb_adapter *adapter)
1784{
1785 struct e1000_hw *hw = &adapter->hw;
Carolyn Wyborny0b1a6f22012-10-18 07:16:19 +00001786 struct e1000_fw_version fw;
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00001787
Carolyn Wyborny0b1a6f22012-10-18 07:16:19 +00001788 igb_get_fw_version(hw, &fw);
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00001789
Carolyn Wyborny0b1a6f22012-10-18 07:16:19 +00001790 switch (hw->mac.type) {
1791 case e1000_i211:
1792 snprintf(adapter->fw_version, sizeof(adapter->fw_version),
1793 "%2d.%2d-%d",
1794 fw.invm_major, fw.invm_minor, fw.invm_img_type);
1795 break;
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00001796
Carolyn Wyborny0b1a6f22012-10-18 07:16:19 +00001797 default:
1798 /* if option is rom valid, display its version too */
1799 if (fw.or_valid) {
1800 snprintf(adapter->fw_version,
1801 sizeof(adapter->fw_version),
1802 "%d.%d, 0x%08x, %d.%d.%d",
1803 fw.eep_major, fw.eep_minor, fw.etrack_id,
1804 fw.or_major, fw.or_build, fw.or_patch);
1805 /* no option rom */
1806 } else {
1807 snprintf(adapter->fw_version,
1808 sizeof(adapter->fw_version),
1809 "%d.%d, 0x%08x",
1810 fw.eep_major, fw.eep_minor, fw.etrack_id);
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00001811 }
Carolyn Wyborny0b1a6f22012-10-18 07:16:19 +00001812 break;
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00001813 }
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00001814 return;
1815}
1816
1817/**
Auke Kok9d5c8242008-01-24 02:22:38 -08001818 * igb_probe - Device Initialization Routine
1819 * @pdev: PCI device information struct
1820 * @ent: entry in igb_pci_tbl
1821 *
1822 * Returns 0 on success, negative on failure
1823 *
1824 * igb_probe initializes an adapter identified by a pci_dev structure.
1825 * The OS initialization, configuring of the adapter private structure,
1826 * and a hardware reset occur.
1827 **/
1828static int __devinit igb_probe(struct pci_dev *pdev,
1829 const struct pci_device_id *ent)
1830{
1831 struct net_device *netdev;
1832 struct igb_adapter *adapter;
1833 struct e1000_hw *hw;
Alexander Duyck4337e992009-10-27 23:48:31 +00001834 u16 eeprom_data = 0;
Carolyn Wyborny9835fd72010-11-22 17:17:21 +00001835 s32 ret_val;
Alexander Duyck4337e992009-10-27 23:48:31 +00001836 static int global_quad_port_a; /* global quad port a indication */
Auke Kok9d5c8242008-01-24 02:22:38 -08001837 const struct e1000_info *ei = igb_info_tbl[ent->driver_data];
1838 unsigned long mmio_start, mmio_len;
David S. Miller2d6a5e92009-03-17 15:01:30 -07001839 int err, pci_using_dac;
Carolyn Wyborny9835fd72010-11-22 17:17:21 +00001840 u8 part_str[E1000_PBANUM_LENGTH];
Auke Kok9d5c8242008-01-24 02:22:38 -08001841
Andy Gospodarekbded64a2010-07-21 06:40:31 +00001842 /* Catch broken hardware that put the wrong VF device ID in
1843 * the PCIe SR-IOV capability.
1844 */
1845 if (pdev->is_virtfn) {
1846 WARN(1, KERN_ERR "%s (%hx:%hx) should not be a VF!\n",
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00001847 pci_name(pdev), pdev->vendor, pdev->device);
Andy Gospodarekbded64a2010-07-21 06:40:31 +00001848 return -EINVAL;
1849 }
1850
Alexander Duyckaed5dec2009-02-06 23:16:04 +00001851 err = pci_enable_device_mem(pdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08001852 if (err)
1853 return err;
1854
1855 pci_using_dac = 0;
Alexander Duyck59d71982010-04-27 13:09:25 +00001856 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(64));
Auke Kok9d5c8242008-01-24 02:22:38 -08001857 if (!err) {
Alexander Duyck59d71982010-04-27 13:09:25 +00001858 err = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64));
Auke Kok9d5c8242008-01-24 02:22:38 -08001859 if (!err)
1860 pci_using_dac = 1;
1861 } else {
Alexander Duyck59d71982010-04-27 13:09:25 +00001862 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
Auke Kok9d5c8242008-01-24 02:22:38 -08001863 if (err) {
Alexander Duyck59d71982010-04-27 13:09:25 +00001864 err = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
Auke Kok9d5c8242008-01-24 02:22:38 -08001865 if (err) {
1866 dev_err(&pdev->dev, "No usable DMA "
1867 "configuration, aborting\n");
1868 goto err_dma;
1869 }
1870 }
1871 }
1872
Alexander Duyckaed5dec2009-02-06 23:16:04 +00001873 err = pci_request_selected_regions(pdev, pci_select_bars(pdev,
1874 IORESOURCE_MEM),
1875 igb_driver_name);
Auke Kok9d5c8242008-01-24 02:22:38 -08001876 if (err)
1877 goto err_pci_reg;
1878
Frans Pop19d5afd2009-10-02 10:04:12 -07001879 pci_enable_pcie_error_reporting(pdev);
Alexander Duyck40a914f2008-11-27 00:24:37 -08001880
Auke Kok9d5c8242008-01-24 02:22:38 -08001881 pci_set_master(pdev);
Auke Kokc682fc22008-04-23 11:09:34 -07001882 pci_save_state(pdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08001883
1884 err = -ENOMEM;
Alexander Duyck1bfaf072009-02-19 20:39:23 -08001885 netdev = alloc_etherdev_mq(sizeof(struct igb_adapter),
Alexander Duyck1cc3bd82011-08-26 07:44:10 +00001886 IGB_MAX_TX_QUEUES);
Auke Kok9d5c8242008-01-24 02:22:38 -08001887 if (!netdev)
1888 goto err_alloc_etherdev;
1889
1890 SET_NETDEV_DEV(netdev, &pdev->dev);
1891
1892 pci_set_drvdata(pdev, netdev);
1893 adapter = netdev_priv(netdev);
1894 adapter->netdev = netdev;
1895 adapter->pdev = pdev;
1896 hw = &adapter->hw;
1897 hw->back = adapter;
stephen hemmingerb3f4d592012-03-13 06:04:20 +00001898 adapter->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
Auke Kok9d5c8242008-01-24 02:22:38 -08001899
1900 mmio_start = pci_resource_start(pdev, 0);
1901 mmio_len = pci_resource_len(pdev, 0);
1902
1903 err = -EIO;
Alexander Duyck28b07592009-02-06 23:20:31 +00001904 hw->hw_addr = ioremap(mmio_start, mmio_len);
1905 if (!hw->hw_addr)
Auke Kok9d5c8242008-01-24 02:22:38 -08001906 goto err_ioremap;
1907
Stephen Hemminger2e5c6922008-11-19 22:20:44 -08001908 netdev->netdev_ops = &igb_netdev_ops;
Auke Kok9d5c8242008-01-24 02:22:38 -08001909 igb_set_ethtool_ops(netdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08001910 netdev->watchdog_timeo = 5 * HZ;
Auke Kok9d5c8242008-01-24 02:22:38 -08001911
1912 strncpy(netdev->name, pci_name(pdev), sizeof(netdev->name) - 1);
1913
1914 netdev->mem_start = mmio_start;
1915 netdev->mem_end = mmio_start + mmio_len;
1916
Auke Kok9d5c8242008-01-24 02:22:38 -08001917 /* PCI config space info */
1918 hw->vendor_id = pdev->vendor;
1919 hw->device_id = pdev->device;
1920 hw->revision_id = pdev->revision;
1921 hw->subsystem_vendor_id = pdev->subsystem_vendor;
1922 hw->subsystem_device_id = pdev->subsystem_device;
1923
Auke Kok9d5c8242008-01-24 02:22:38 -08001924 /* Copy the default MAC, PHY and NVM function pointers */
1925 memcpy(&hw->mac.ops, ei->mac_ops, sizeof(hw->mac.ops));
1926 memcpy(&hw->phy.ops, ei->phy_ops, sizeof(hw->phy.ops));
1927 memcpy(&hw->nvm.ops, ei->nvm_ops, sizeof(hw->nvm.ops));
1928 /* Initialize skew-specific constants */
1929 err = ei->get_invariants(hw);
1930 if (err)
Alexander Duyck450c87c2009-02-06 23:22:11 +00001931 goto err_sw_init;
Auke Kok9d5c8242008-01-24 02:22:38 -08001932
Alexander Duyck450c87c2009-02-06 23:22:11 +00001933 /* setup the private structure */
Auke Kok9d5c8242008-01-24 02:22:38 -08001934 err = igb_sw_init(adapter);
1935 if (err)
1936 goto err_sw_init;
1937
1938 igb_get_bus_info_pcie(hw);
1939
1940 hw->phy.autoneg_wait_to_complete = false;
Auke Kok9d5c8242008-01-24 02:22:38 -08001941
1942 /* Copper options */
1943 if (hw->phy.media_type == e1000_media_type_copper) {
1944 hw->phy.mdix = AUTO_ALL_MODES;
1945 hw->phy.disable_polarity_correction = false;
1946 hw->phy.ms_type = e1000_ms_hw_default;
1947 }
1948
1949 if (igb_check_reset_block(hw))
1950 dev_info(&pdev->dev,
1951 "PHY reset is blocked due to SOL/IDER session.\n");
1952
Alexander Duyck077887c2011-08-26 07:46:29 +00001953 /*
1954 * features is initialized to 0 in allocation, it might have bits
1955 * set by igb_sw_init so we should use an or instead of an
1956 * assignment.
1957 */
1958 netdev->features |= NETIF_F_SG |
1959 NETIF_F_IP_CSUM |
1960 NETIF_F_IPV6_CSUM |
1961 NETIF_F_TSO |
1962 NETIF_F_TSO6 |
1963 NETIF_F_RXHASH |
1964 NETIF_F_RXCSUM |
1965 NETIF_F_HW_VLAN_RX |
1966 NETIF_F_HW_VLAN_TX;
Michał Mirosławac52caa2011-06-08 08:38:01 +00001967
Alexander Duyck077887c2011-08-26 07:46:29 +00001968 /* copy netdev features into list of user selectable features */
1969 netdev->hw_features |= netdev->features;
Ben Greear89eaefb2012-03-06 09:41:58 +00001970 netdev->hw_features |= NETIF_F_RXALL;
Auke Kok9d5c8242008-01-24 02:22:38 -08001971
Alexander Duyck077887c2011-08-26 07:46:29 +00001972 /* set this bit last since it cannot be part of hw_features */
1973 netdev->features |= NETIF_F_HW_VLAN_FILTER;
1974
1975 netdev->vlan_features |= NETIF_F_TSO |
1976 NETIF_F_TSO6 |
1977 NETIF_F_IP_CSUM |
1978 NETIF_F_IPV6_CSUM |
1979 NETIF_F_SG;
Jeff Kirsher48f29ff2008-06-05 04:06:27 -07001980
Ben Greear6b8f0922012-03-06 09:41:53 +00001981 netdev->priv_flags |= IFF_SUPP_NOFCS;
1982
Yi Zou7b872a52010-09-22 17:57:58 +00001983 if (pci_using_dac) {
Auke Kok9d5c8242008-01-24 02:22:38 -08001984 netdev->features |= NETIF_F_HIGHDMA;
Yi Zou7b872a52010-09-22 17:57:58 +00001985 netdev->vlan_features |= NETIF_F_HIGHDMA;
1986 }
Auke Kok9d5c8242008-01-24 02:22:38 -08001987
Michał Mirosławac52caa2011-06-08 08:38:01 +00001988 if (hw->mac.type >= e1000_82576) {
1989 netdev->hw_features |= NETIF_F_SCTP_CSUM;
Jesse Brandeburgb9473562009-04-27 22:36:13 +00001990 netdev->features |= NETIF_F_SCTP_CSUM;
Michał Mirosławac52caa2011-06-08 08:38:01 +00001991 }
Jesse Brandeburgb9473562009-04-27 22:36:13 +00001992
Jiri Pirko01789342011-08-16 06:29:00 +00001993 netdev->priv_flags |= IFF_UNICAST_FLT;
1994
Alexander Duyck330a6d62009-10-27 23:51:35 +00001995 adapter->en_mng_pt = igb_enable_mng_pass_thru(hw);
Auke Kok9d5c8242008-01-24 02:22:38 -08001996
1997 /* before reading the NVM, reset the controller to put the device in a
1998 * known good starting state */
1999 hw->mac.ops.reset_hw(hw);
2000
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002001 /*
2002 * make sure the NVM is good , i211 parts have special NVM that
2003 * doesn't contain a checksum
2004 */
2005 if (hw->mac.type != e1000_i211) {
2006 if (hw->nvm.ops.validate(hw) < 0) {
2007 dev_err(&pdev->dev, "The NVM Checksum Is Not Valid\n");
2008 err = -EIO;
2009 goto err_eeprom;
2010 }
Auke Kok9d5c8242008-01-24 02:22:38 -08002011 }
2012
2013 /* copy the MAC address out of the NVM */
2014 if (hw->mac.ops.read_mac_addr(hw))
2015 dev_err(&pdev->dev, "NVM Read Error\n");
2016
2017 memcpy(netdev->dev_addr, hw->mac.addr, netdev->addr_len);
2018 memcpy(netdev->perm_addr, hw->mac.addr, netdev->addr_len);
2019
2020 if (!is_valid_ether_addr(netdev->perm_addr)) {
2021 dev_err(&pdev->dev, "Invalid MAC Address\n");
2022 err = -EIO;
2023 goto err_eeprom;
2024 }
2025
Carolyn Wybornyd67974f2012-06-14 16:04:19 +00002026 /* get firmware version for ethtool -i */
2027 igb_set_fw_version(adapter);
2028
Joe Perchesc061b182010-08-23 18:20:03 +00002029 setup_timer(&adapter->watchdog_timer, igb_watchdog,
Alexander Duyck0e340482009-03-20 00:17:08 +00002030 (unsigned long) adapter);
Joe Perchesc061b182010-08-23 18:20:03 +00002031 setup_timer(&adapter->phy_info_timer, igb_update_phy_info,
Alexander Duyck0e340482009-03-20 00:17:08 +00002032 (unsigned long) adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08002033
2034 INIT_WORK(&adapter->reset_task, igb_reset_task);
2035 INIT_WORK(&adapter->watchdog_task, igb_watchdog_task);
2036
Alexander Duyck450c87c2009-02-06 23:22:11 +00002037 /* Initialize link properties that are user-changeable */
Auke Kok9d5c8242008-01-24 02:22:38 -08002038 adapter->fc_autoneg = true;
2039 hw->mac.autoneg = true;
2040 hw->phy.autoneg_advertised = 0x2f;
2041
Alexander Duyck0cce1192009-07-23 18:10:24 +00002042 hw->fc.requested_mode = e1000_fc_default;
2043 hw->fc.current_mode = e1000_fc_default;
Auke Kok9d5c8242008-01-24 02:22:38 -08002044
Auke Kok9d5c8242008-01-24 02:22:38 -08002045 igb_validate_mdi_setting(hw);
2046
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002047 /* By default, support wake on port A */
Alexander Duycka2cf8b62009-03-13 20:41:17 +00002048 if (hw->bus.func == 0)
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002049 adapter->flags |= IGB_FLAG_WOL_SUPPORTED;
2050
2051 /* Check the NVM for wake support on non-port A ports */
2052 if (hw->mac.type >= e1000_82580)
Alexander Duyck55cac242009-11-19 12:42:21 +00002053 hw->nvm.ops.read(hw, NVM_INIT_CONTROL3_PORT_A +
2054 NVM_82580_LAN_FUNC_OFFSET(hw->bus.func), 1,
2055 &eeprom_data);
Alexander Duycka2cf8b62009-03-13 20:41:17 +00002056 else if (hw->bus.func == 1)
2057 hw->nvm.ops.read(hw, NVM_INIT_CONTROL3_PORT_B, 1, &eeprom_data);
Auke Kok9d5c8242008-01-24 02:22:38 -08002058
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002059 if (eeprom_data & IGB_EEPROM_APME)
2060 adapter->flags |= IGB_FLAG_WOL_SUPPORTED;
Auke Kok9d5c8242008-01-24 02:22:38 -08002061
2062 /* now that we have the eeprom settings, apply the special cases where
2063 * the eeprom may be wrong or the board simply won't support wake on
2064 * lan on a particular port */
2065 switch (pdev->device) {
2066 case E1000_DEV_ID_82575GB_QUAD_COPPER:
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002067 adapter->flags &= ~IGB_FLAG_WOL_SUPPORTED;
Auke Kok9d5c8242008-01-24 02:22:38 -08002068 break;
2069 case E1000_DEV_ID_82575EB_FIBER_SERDES:
Alexander Duyck2d064c02008-07-08 15:10:12 -07002070 case E1000_DEV_ID_82576_FIBER:
2071 case E1000_DEV_ID_82576_SERDES:
Auke Kok9d5c8242008-01-24 02:22:38 -08002072 /* Wake events only supported on port A for dual fiber
2073 * regardless of eeprom setting */
2074 if (rd32(E1000_STATUS) & E1000_STATUS_FUNC_1)
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002075 adapter->flags &= ~IGB_FLAG_WOL_SUPPORTED;
Auke Kok9d5c8242008-01-24 02:22:38 -08002076 break;
Alexander Duyckc8ea5ea2009-03-13 20:42:35 +00002077 case E1000_DEV_ID_82576_QUAD_COPPER:
Stefan Assmannd5aa2252010-04-09 09:51:34 +00002078 case E1000_DEV_ID_82576_QUAD_COPPER_ET2:
Alexander Duyckc8ea5ea2009-03-13 20:42:35 +00002079 /* if quad port adapter, disable WoL on all but port A */
2080 if (global_quad_port_a != 0)
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002081 adapter->flags &= ~IGB_FLAG_WOL_SUPPORTED;
Alexander Duyckc8ea5ea2009-03-13 20:42:35 +00002082 else
2083 adapter->flags |= IGB_FLAG_QUAD_PORT_A;
2084 /* Reset for multiple quad port adapters */
2085 if (++global_quad_port_a == 4)
2086 global_quad_port_a = 0;
2087 break;
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002088 default:
2089 /* If the device can't wake, don't set software support */
2090 if (!device_can_wakeup(&adapter->pdev->dev))
2091 adapter->flags &= ~IGB_FLAG_WOL_SUPPORTED;
Auke Kok9d5c8242008-01-24 02:22:38 -08002092 }
2093
2094 /* initialize the wol settings based on the eeprom settings */
Matthew Vick63d4a8f2012-11-09 05:49:54 +00002095 if (adapter->flags & IGB_FLAG_WOL_SUPPORTED)
2096 adapter->wol |= E1000_WUFC_MAG;
2097
2098 /* Some vendors want WoL disabled by default, but still supported */
2099 if ((hw->mac.type == e1000_i350) &&
2100 (pdev->subsystem_vendor == PCI_VENDOR_ID_HP)) {
2101 adapter->flags |= IGB_FLAG_WOL_SUPPORTED;
2102 adapter->wol = 0;
2103 }
2104
2105 device_set_wakeup_enable(&adapter->pdev->dev,
2106 adapter->flags & IGB_FLAG_WOL_SUPPORTED);
Auke Kok9d5c8242008-01-24 02:22:38 -08002107
2108 /* reset the hardware with the new settings */
2109 igb_reset(adapter);
2110
2111 /* let the f/w know that the h/w is now under the control of the
2112 * driver. */
2113 igb_get_hw_control(adapter);
2114
Auke Kok9d5c8242008-01-24 02:22:38 -08002115 strcpy(netdev->name, "eth%d");
2116 err = register_netdev(netdev);
2117 if (err)
2118 goto err_register;
2119
Jesse Brandeburgb168dfc2009-04-17 20:44:32 +00002120 /* carrier off reporting is important to ethtool even BEFORE open */
2121 netif_carrier_off(netdev);
2122
Jeff Kirsher421e02f2008-10-17 11:08:31 -07002123#ifdef CONFIG_IGB_DCA
Alexander Duyckbbd98fe2009-01-31 00:52:30 -08002124 if (dca_add_requester(&pdev->dev) == 0) {
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07002125 adapter->flags |= IGB_FLAG_DCA_ENABLED;
Jeb Cramerfe4506b2008-07-08 15:07:55 -07002126 dev_info(&pdev->dev, "DCA enabled\n");
Jeb Cramerfe4506b2008-07-08 15:07:55 -07002127 igb_setup_dca(adapter);
2128 }
Alexander Duyckc5b9bd52009-10-27 23:46:01 +00002129
Jeb Cramerfe4506b2008-07-08 15:07:55 -07002130#endif
Matthew Vick3c89f6d2012-08-10 05:40:43 +00002131
Anders Berggren673b8b72011-02-04 07:32:32 +00002132 /* do hw tstamp init after resetting */
Richard Cochran7ebae812012-03-16 10:55:37 +00002133 igb_ptp_init(adapter);
Anders Berggren673b8b72011-02-04 07:32:32 +00002134
Auke Kok9d5c8242008-01-24 02:22:38 -08002135 dev_info(&pdev->dev, "Intel(R) Gigabit Ethernet Network Connection\n");
2136 /* print bus type/speed/width info */
Johannes Berg7c510e42008-10-27 17:47:26 -07002137 dev_info(&pdev->dev, "%s: (PCIe:%s:%s) %pM\n",
Auke Kok9d5c8242008-01-24 02:22:38 -08002138 netdev->name,
Alexander Duyck559e9c42009-10-27 23:52:50 +00002139 ((hw->bus.speed == e1000_bus_speed_2500) ? "2.5Gb/s" :
Alexander Duyckff846f52010-04-27 01:02:40 +00002140 (hw->bus.speed == e1000_bus_speed_5000) ? "5.0Gb/s" :
Alexander Duyck559e9c42009-10-27 23:52:50 +00002141 "unknown"),
Alexander Duyck59c3de82009-03-31 20:38:00 +00002142 ((hw->bus.width == e1000_bus_width_pcie_x4) ? "Width x4" :
2143 (hw->bus.width == e1000_bus_width_pcie_x2) ? "Width x2" :
2144 (hw->bus.width == e1000_bus_width_pcie_x1) ? "Width x1" :
2145 "unknown"),
Johannes Berg7c510e42008-10-27 17:47:26 -07002146 netdev->dev_addr);
Auke Kok9d5c8242008-01-24 02:22:38 -08002147
Carolyn Wyborny9835fd72010-11-22 17:17:21 +00002148 ret_val = igb_read_part_string(hw, part_str, E1000_PBANUM_LENGTH);
2149 if (ret_val)
2150 strcpy(part_str, "Unknown");
2151 dev_info(&pdev->dev, "%s: PBA No: %s\n", netdev->name, part_str);
Auke Kok9d5c8242008-01-24 02:22:38 -08002152 dev_info(&pdev->dev,
2153 "Using %s interrupts. %d rx queue(s), %d tx queue(s)\n",
2154 adapter->msix_entries ? "MSI-X" :
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07002155 (adapter->flags & IGB_FLAG_HAS_MSI) ? "MSI" : "legacy",
Auke Kok9d5c8242008-01-24 02:22:38 -08002156 adapter->num_rx_queues, adapter->num_tx_queues);
Carolyn Wyborny09b068d2011-03-11 20:42:13 -08002157 switch (hw->mac.type) {
2158 case e1000_i350:
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002159 case e1000_i210:
2160 case e1000_i211:
Carolyn Wyborny09b068d2011-03-11 20:42:13 -08002161 igb_set_eee_i350(hw);
2162 break;
2163 default:
2164 break;
2165 }
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002166
2167 pm_runtime_put_noidle(&pdev->dev);
Auke Kok9d5c8242008-01-24 02:22:38 -08002168 return 0;
2169
2170err_register:
2171 igb_release_hw_control(adapter);
2172err_eeprom:
2173 if (!igb_check_reset_block(hw))
Alexander Duyckf5f4cf02008-11-21 21:30:24 -08002174 igb_reset_phy(hw);
Auke Kok9d5c8242008-01-24 02:22:38 -08002175
2176 if (hw->flash_address)
2177 iounmap(hw->flash_address);
Auke Kok9d5c8242008-01-24 02:22:38 -08002178err_sw_init:
Alexander Duyck047e0032009-10-27 15:49:27 +00002179 igb_clear_interrupt_scheme(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08002180 iounmap(hw->hw_addr);
2181err_ioremap:
2182 free_netdev(netdev);
2183err_alloc_etherdev:
Alexander Duyck559e9c42009-10-27 23:52:50 +00002184 pci_release_selected_regions(pdev,
2185 pci_select_bars(pdev, IORESOURCE_MEM));
Auke Kok9d5c8242008-01-24 02:22:38 -08002186err_pci_reg:
2187err_dma:
2188 pci_disable_device(pdev);
2189 return err;
2190}
2191
2192/**
2193 * igb_remove - Device Removal Routine
2194 * @pdev: PCI device information struct
2195 *
2196 * igb_remove is called by the PCI subsystem to alert the driver
2197 * that it should release a PCI device. The could be caused by a
2198 * Hot-Plug event, or because the driver is going to be removed from
2199 * memory.
2200 **/
2201static void __devexit igb_remove(struct pci_dev *pdev)
2202{
2203 struct net_device *netdev = pci_get_drvdata(pdev);
2204 struct igb_adapter *adapter = netdev_priv(netdev);
Jeb Cramerfe4506b2008-07-08 15:07:55 -07002205 struct e1000_hw *hw = &adapter->hw;
Auke Kok9d5c8242008-01-24 02:22:38 -08002206
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002207 pm_runtime_get_noresume(&pdev->dev);
Matthew Vicka79f4f82012-08-10 05:40:44 +00002208 igb_ptp_stop(adapter);
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002209
Tejun Heo760141a2010-12-12 16:45:14 +01002210 /*
2211 * The watchdog timer may be rescheduled, so explicitly
2212 * disable watchdog from being rescheduled.
2213 */
Auke Kok9d5c8242008-01-24 02:22:38 -08002214 set_bit(__IGB_DOWN, &adapter->state);
2215 del_timer_sync(&adapter->watchdog_timer);
2216 del_timer_sync(&adapter->phy_info_timer);
2217
Tejun Heo760141a2010-12-12 16:45:14 +01002218 cancel_work_sync(&adapter->reset_task);
2219 cancel_work_sync(&adapter->watchdog_task);
Auke Kok9d5c8242008-01-24 02:22:38 -08002220
Jeff Kirsher421e02f2008-10-17 11:08:31 -07002221#ifdef CONFIG_IGB_DCA
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07002222 if (adapter->flags & IGB_FLAG_DCA_ENABLED) {
Jeb Cramerfe4506b2008-07-08 15:07:55 -07002223 dev_info(&pdev->dev, "DCA disabled\n");
2224 dca_remove_requester(&pdev->dev);
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07002225 adapter->flags &= ~IGB_FLAG_DCA_ENABLED;
Alexander Duyckcbd347a2009-02-15 23:59:44 -08002226 wr32(E1000_DCA_CTRL, E1000_DCA_CTRL_DCA_MODE_DISABLE);
Jeb Cramerfe4506b2008-07-08 15:07:55 -07002227 }
2228#endif
2229
Auke Kok9d5c8242008-01-24 02:22:38 -08002230 /* Release control of h/w to f/w. If f/w is AMT enabled, this
2231 * would have already happened in close and is redundant. */
2232 igb_release_hw_control(adapter);
2233
2234 unregister_netdev(netdev);
2235
Alexander Duyck047e0032009-10-27 15:49:27 +00002236 igb_clear_interrupt_scheme(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08002237
Alexander Duyck37680112009-02-19 20:40:30 -08002238#ifdef CONFIG_PCI_IOV
2239 /* reclaim resources allocated to VFs */
2240 if (adapter->vf_data) {
2241 /* disable iov and allow time for transactions to clear */
Stefan Assmannf5571472012-08-18 04:06:11 +00002242 if (igb_vfs_are_assigned(adapter)) {
2243 dev_info(&pdev->dev, "Unloading driver while VFs are assigned - VFs will not be deallocated\n");
2244 } else {
Greg Rose0224d662011-10-14 02:57:14 +00002245 pci_disable_sriov(pdev);
2246 msleep(500);
Greg Rose0224d662011-10-14 02:57:14 +00002247 }
Alexander Duyck37680112009-02-19 20:40:30 -08002248
2249 kfree(adapter->vf_data);
2250 adapter->vf_data = NULL;
2251 wr32(E1000_IOVCTL, E1000_IOVCTL_REUSE_VFQ);
Jesse Brandeburg945a5152011-07-20 00:56:21 +00002252 wrfl();
Alexander Duyck37680112009-02-19 20:40:30 -08002253 msleep(100);
2254 dev_info(&pdev->dev, "IOV Disabled\n");
2255 }
2256#endif
Alexander Duyck559e9c42009-10-27 23:52:50 +00002257
Alexander Duyck28b07592009-02-06 23:20:31 +00002258 iounmap(hw->hw_addr);
2259 if (hw->flash_address)
2260 iounmap(hw->flash_address);
Alexander Duyck559e9c42009-10-27 23:52:50 +00002261 pci_release_selected_regions(pdev,
2262 pci_select_bars(pdev, IORESOURCE_MEM));
Auke Kok9d5c8242008-01-24 02:22:38 -08002263
Carolyn Wyborny1128c752011-10-14 00:13:49 +00002264 kfree(adapter->shadow_vfta);
Auke Kok9d5c8242008-01-24 02:22:38 -08002265 free_netdev(netdev);
2266
Frans Pop19d5afd2009-10-02 10:04:12 -07002267 pci_disable_pcie_error_reporting(pdev);
Alexander Duyck40a914f2008-11-27 00:24:37 -08002268
Auke Kok9d5c8242008-01-24 02:22:38 -08002269 pci_disable_device(pdev);
2270}
2271
2272/**
Alexander Duycka6b623e2009-10-27 23:47:53 +00002273 * igb_probe_vfs - Initialize vf data storage and add VFs to pci config space
2274 * @adapter: board private structure to initialize
2275 *
2276 * This function initializes the vf specific data storage and then attempts to
2277 * allocate the VFs. The reason for ordering it this way is because it is much
2278 * mor expensive time wise to disable SR-IOV than it is to allocate and free
2279 * the memory for the VFs.
2280 **/
2281static void __devinit igb_probe_vfs(struct igb_adapter * adapter)
2282{
2283#ifdef CONFIG_PCI_IOV
2284 struct pci_dev *pdev = adapter->pdev;
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002285 struct e1000_hw *hw = &adapter->hw;
Stefan Assmannf5571472012-08-18 04:06:11 +00002286 int old_vfs = pci_num_vf(adapter->pdev);
Greg Rose0224d662011-10-14 02:57:14 +00002287 int i;
Alexander Duycka6b623e2009-10-27 23:47:53 +00002288
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002289 /* Virtualization features not supported on i210 family. */
2290 if ((hw->mac.type == e1000_i210) || (hw->mac.type == e1000_i211))
2291 return;
2292
Greg Rose0224d662011-10-14 02:57:14 +00002293 if (old_vfs) {
2294 dev_info(&pdev->dev, "%d pre-allocated VFs found - override "
2295 "max_vfs setting of %d\n", old_vfs, max_vfs);
2296 adapter->vfs_allocated_count = old_vfs;
Alexander Duycka6b623e2009-10-27 23:47:53 +00002297 }
2298
Greg Rose0224d662011-10-14 02:57:14 +00002299 if (!adapter->vfs_allocated_count)
2300 return;
2301
2302 adapter->vf_data = kcalloc(adapter->vfs_allocated_count,
2303 sizeof(struct vf_data_storage), GFP_KERNEL);
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002304
Greg Rose0224d662011-10-14 02:57:14 +00002305 /* if allocation failed then we do not support SR-IOV */
2306 if (!adapter->vf_data) {
Alexander Duycka6b623e2009-10-27 23:47:53 +00002307 adapter->vfs_allocated_count = 0;
Greg Rose0224d662011-10-14 02:57:14 +00002308 dev_err(&pdev->dev, "Unable to allocate memory for VF "
2309 "Data Storage\n");
2310 goto out;
Alexander Duycka6b623e2009-10-27 23:47:53 +00002311 }
Greg Rose0224d662011-10-14 02:57:14 +00002312
2313 if (!old_vfs) {
2314 if (pci_enable_sriov(pdev, adapter->vfs_allocated_count))
2315 goto err_out;
2316 }
2317 dev_info(&pdev->dev, "%d VFs allocated\n",
2318 adapter->vfs_allocated_count);
2319 for (i = 0; i < adapter->vfs_allocated_count; i++)
2320 igb_vf_configure(adapter, i);
2321
2322 /* DMA Coalescing is not supported in IOV mode. */
2323 adapter->flags &= ~IGB_FLAG_DMAC;
2324 goto out;
2325err_out:
2326 kfree(adapter->vf_data);
2327 adapter->vf_data = NULL;
2328 adapter->vfs_allocated_count = 0;
2329out:
2330 return;
Alexander Duycka6b623e2009-10-27 23:47:53 +00002331#endif /* CONFIG_PCI_IOV */
2332}
2333
Alexander Duyck115f4592009-11-12 18:37:00 +00002334/**
Auke Kok9d5c8242008-01-24 02:22:38 -08002335 * igb_sw_init - Initialize general software structures (struct igb_adapter)
2336 * @adapter: board private structure to initialize
2337 *
2338 * igb_sw_init initializes the Adapter private data structure.
2339 * Fields are initialized based on PCI device information and
2340 * OS network device settings (MTU size).
2341 **/
2342static int __devinit igb_sw_init(struct igb_adapter *adapter)
2343{
2344 struct e1000_hw *hw = &adapter->hw;
2345 struct net_device *netdev = adapter->netdev;
2346 struct pci_dev *pdev = adapter->pdev;
Matthew Vick374a5422012-05-18 04:54:58 +00002347 u32 max_rss_queues;
Auke Kok9d5c8242008-01-24 02:22:38 -08002348
2349 pci_read_config_word(pdev, PCI_COMMAND, &hw->bus.pci_cmd_word);
2350
Alexander Duyck13fde972011-10-05 13:35:24 +00002351 /* set default ring sizes */
Alexander Duyck68fd9912008-11-20 00:48:10 -08002352 adapter->tx_ring_count = IGB_DEFAULT_TXD;
2353 adapter->rx_ring_count = IGB_DEFAULT_RXD;
Alexander Duyck13fde972011-10-05 13:35:24 +00002354
2355 /* set default ITR values */
Alexander Duyck4fc82ad2009-10-27 23:45:42 +00002356 adapter->rx_itr_setting = IGB_DEFAULT_ITR;
2357 adapter->tx_itr_setting = IGB_DEFAULT_ITR;
2358
Alexander Duyck13fde972011-10-05 13:35:24 +00002359 /* set default work limits */
2360 adapter->tx_work_limit = IGB_DEFAULT_TX_WORK;
2361
Alexander Duyck153285f2011-08-26 07:43:32 +00002362 adapter->max_frame_size = netdev->mtu + ETH_HLEN + ETH_FCS_LEN +
2363 VLAN_HLEN;
Auke Kok9d5c8242008-01-24 02:22:38 -08002364 adapter->min_frame_size = ETH_ZLEN + ETH_FCS_LEN;
2365
Eric Dumazet12dcd862010-10-15 17:27:10 +00002366 spin_lock_init(&adapter->stats64_lock);
Alexander Duycka6b623e2009-10-27 23:47:53 +00002367#ifdef CONFIG_PCI_IOV
Carolyn Wyborny6b78bb12011-01-20 06:40:45 +00002368 switch (hw->mac.type) {
2369 case e1000_82576:
2370 case e1000_i350:
Stefan Assmann9b082d72011-02-24 20:03:31 +00002371 if (max_vfs > 7) {
2372 dev_warn(&pdev->dev,
2373 "Maximum of 7 VFs per PF, using max\n");
2374 adapter->vfs_allocated_count = 7;
2375 } else
2376 adapter->vfs_allocated_count = max_vfs;
Carolyn Wyborny6b78bb12011-01-20 06:40:45 +00002377 break;
2378 default:
2379 break;
2380 }
Alexander Duycka6b623e2009-10-27 23:47:53 +00002381#endif /* CONFIG_PCI_IOV */
Matthew Vick374a5422012-05-18 04:54:58 +00002382
2383 /* Determine the maximum number of RSS queues supported. */
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002384 switch (hw->mac.type) {
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002385 case e1000_i211:
Matthew Vick374a5422012-05-18 04:54:58 +00002386 max_rss_queues = IGB_MAX_RX_QUEUES_I211;
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002387 break;
Matthew Vick374a5422012-05-18 04:54:58 +00002388 case e1000_82575:
2389 case e1000_i210:
2390 max_rss_queues = IGB_MAX_RX_QUEUES_82575;
2391 break;
2392 case e1000_i350:
2393 /* I350 cannot do RSS and SR-IOV at the same time */
2394 if (!!adapter->vfs_allocated_count) {
2395 max_rss_queues = 1;
2396 break;
2397 }
2398 /* fall through */
2399 case e1000_82576:
2400 if (!!adapter->vfs_allocated_count) {
2401 max_rss_queues = 2;
2402 break;
2403 }
2404 /* fall through */
2405 case e1000_82580:
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002406 default:
Matthew Vick374a5422012-05-18 04:54:58 +00002407 max_rss_queues = IGB_MAX_RX_QUEUES;
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002408 break;
2409 }
Alexander Duycka99955f2009-11-12 18:37:19 +00002410
Matthew Vick374a5422012-05-18 04:54:58 +00002411 adapter->rss_queues = min_t(u32, max_rss_queues, num_online_cpus());
2412
2413 /* Determine if we need to pair queues. */
2414 switch (hw->mac.type) {
2415 case e1000_82575:
2416 case e1000_i211:
2417 /* Device supports enough interrupts without queue pairing. */
2418 break;
2419 case e1000_82576:
2420 /*
2421 * If VFs are going to be allocated with RSS queues then we
2422 * should pair the queues in order to conserve interrupts due
2423 * to limited supply.
2424 */
2425 if ((adapter->rss_queues > 1) &&
2426 (adapter->vfs_allocated_count > 6))
2427 adapter->flags |= IGB_FLAG_QUEUE_PAIRS;
2428 /* fall through */
2429 case e1000_82580:
2430 case e1000_i350:
2431 case e1000_i210:
2432 default:
2433 /*
2434 * If rss_queues > half of max_rss_queues, pair the queues in
2435 * order to conserve interrupts due to limited supply.
2436 */
2437 if (adapter->rss_queues > (max_rss_queues / 2))
2438 adapter->flags |= IGB_FLAG_QUEUE_PAIRS;
2439 break;
2440 }
Alexander Duycka99955f2009-11-12 18:37:19 +00002441
Carolyn Wyborny1128c752011-10-14 00:13:49 +00002442 /* Setup and initialize a copy of the hw vlan table array */
2443 adapter->shadow_vfta = kzalloc(sizeof(u32) *
2444 E1000_VLAN_FILTER_TBL_SIZE,
2445 GFP_ATOMIC);
2446
Alexander Duycka6b623e2009-10-27 23:47:53 +00002447 /* This call may decrease the number of queues */
Alexander Duyck047e0032009-10-27 15:49:27 +00002448 if (igb_init_interrupt_scheme(adapter)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08002449 dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
2450 return -ENOMEM;
2451 }
2452
Alexander Duycka6b623e2009-10-27 23:47:53 +00002453 igb_probe_vfs(adapter);
2454
Auke Kok9d5c8242008-01-24 02:22:38 -08002455 /* Explicitly disable IRQ since the NIC can be in any state. */
2456 igb_irq_disable(adapter);
2457
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002458 if (hw->mac.type >= e1000_i350)
Carolyn Wyborny831ec0b2011-03-11 20:43:54 -08002459 adapter->flags &= ~IGB_FLAG_DMAC;
2460
Auke Kok9d5c8242008-01-24 02:22:38 -08002461 set_bit(__IGB_DOWN, &adapter->state);
2462 return 0;
2463}
2464
2465/**
2466 * igb_open - Called when a network interface is made active
2467 * @netdev: network interface device structure
2468 *
2469 * Returns 0 on success, negative value on failure
2470 *
2471 * The open entry point is called when a network interface is made
2472 * active by the system (IFF_UP). At this point all resources needed
2473 * for transmit and receive operations are allocated, the interrupt
2474 * handler is registered with the OS, the watchdog timer is started,
2475 * and the stack is notified that the interface is ready.
2476 **/
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002477static int __igb_open(struct net_device *netdev, bool resuming)
Auke Kok9d5c8242008-01-24 02:22:38 -08002478{
2479 struct igb_adapter *adapter = netdev_priv(netdev);
2480 struct e1000_hw *hw = &adapter->hw;
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002481 struct pci_dev *pdev = adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08002482 int err;
2483 int i;
2484
2485 /* disallow open during test */
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002486 if (test_bit(__IGB_TESTING, &adapter->state)) {
2487 WARN_ON(resuming);
Auke Kok9d5c8242008-01-24 02:22:38 -08002488 return -EBUSY;
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002489 }
2490
2491 if (!resuming)
2492 pm_runtime_get_sync(&pdev->dev);
Auke Kok9d5c8242008-01-24 02:22:38 -08002493
Jesse Brandeburgb168dfc2009-04-17 20:44:32 +00002494 netif_carrier_off(netdev);
2495
Auke Kok9d5c8242008-01-24 02:22:38 -08002496 /* allocate transmit descriptors */
2497 err = igb_setup_all_tx_resources(adapter);
2498 if (err)
2499 goto err_setup_tx;
2500
2501 /* allocate receive descriptors */
2502 err = igb_setup_all_rx_resources(adapter);
2503 if (err)
2504 goto err_setup_rx;
2505
Nick Nunley88a268c2010-02-17 01:01:59 +00002506 igb_power_up_link(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08002507
Auke Kok9d5c8242008-01-24 02:22:38 -08002508 /* before we allocate an interrupt, we must be ready to handle it.
2509 * Setting DEBUG_SHIRQ in the kernel makes it fire an interrupt
2510 * as soon as we call pci_request_irq, so we have to setup our
2511 * clean_rx handler before we do so. */
2512 igb_configure(adapter);
2513
2514 err = igb_request_irq(adapter);
2515 if (err)
2516 goto err_req_irq;
2517
Alexander Duyck0c2cc022012-09-25 00:31:22 +00002518 /* Notify the stack of the actual queue counts. */
2519 err = netif_set_real_num_tx_queues(adapter->netdev,
2520 adapter->num_tx_queues);
2521 if (err)
2522 goto err_set_queues;
2523
2524 err = netif_set_real_num_rx_queues(adapter->netdev,
2525 adapter->num_rx_queues);
2526 if (err)
2527 goto err_set_queues;
2528
Auke Kok9d5c8242008-01-24 02:22:38 -08002529 /* From here on the code is the same as igb_up() */
2530 clear_bit(__IGB_DOWN, &adapter->state);
2531
Alexander Duyck0d1ae7f2011-08-26 07:46:34 +00002532 for (i = 0; i < adapter->num_q_vectors; i++)
2533 napi_enable(&(adapter->q_vector[i]->napi));
Auke Kok9d5c8242008-01-24 02:22:38 -08002534
2535 /* Clear any pending interrupts. */
2536 rd32(E1000_ICR);
PJ Waskiewicz844290e2008-06-27 11:00:39 -07002537
2538 igb_irq_enable(adapter);
2539
Alexander Duyckd4960302009-10-27 15:53:45 +00002540 /* notify VFs that reset has been completed */
2541 if (adapter->vfs_allocated_count) {
2542 u32 reg_data = rd32(E1000_CTRL_EXT);
2543 reg_data |= E1000_CTRL_EXT_PFRSTD;
2544 wr32(E1000_CTRL_EXT, reg_data);
2545 }
2546
Jeff Kirsherd55b53f2008-07-18 04:33:03 -07002547 netif_tx_start_all_queues(netdev);
2548
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002549 if (!resuming)
2550 pm_runtime_put(&pdev->dev);
2551
Alexander Duyck25568a52009-10-27 23:49:59 +00002552 /* start the watchdog. */
2553 hw->mac.get_link_status = 1;
2554 schedule_work(&adapter->watchdog_task);
Auke Kok9d5c8242008-01-24 02:22:38 -08002555
2556 return 0;
2557
Alexander Duyck0c2cc022012-09-25 00:31:22 +00002558err_set_queues:
2559 igb_free_irq(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08002560err_req_irq:
2561 igb_release_hw_control(adapter);
Nick Nunley88a268c2010-02-17 01:01:59 +00002562 igb_power_down_link(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08002563 igb_free_all_rx_resources(adapter);
2564err_setup_rx:
2565 igb_free_all_tx_resources(adapter);
2566err_setup_tx:
2567 igb_reset(adapter);
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002568 if (!resuming)
2569 pm_runtime_put(&pdev->dev);
Auke Kok9d5c8242008-01-24 02:22:38 -08002570
2571 return err;
2572}
2573
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002574static int igb_open(struct net_device *netdev)
2575{
2576 return __igb_open(netdev, false);
2577}
2578
Auke Kok9d5c8242008-01-24 02:22:38 -08002579/**
2580 * igb_close - Disables a network interface
2581 * @netdev: network interface device structure
2582 *
2583 * Returns 0, this is not allowed to fail
2584 *
2585 * The close entry point is called when an interface is de-activated
2586 * by the OS. The hardware is still under the driver's control, but
2587 * needs to be disabled. A global MAC reset is issued to stop the
2588 * hardware, and all transmit and receive resources are freed.
2589 **/
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002590static int __igb_close(struct net_device *netdev, bool suspending)
Auke Kok9d5c8242008-01-24 02:22:38 -08002591{
2592 struct igb_adapter *adapter = netdev_priv(netdev);
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002593 struct pci_dev *pdev = adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08002594
2595 WARN_ON(test_bit(__IGB_RESETTING, &adapter->state));
Auke Kok9d5c8242008-01-24 02:22:38 -08002596
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002597 if (!suspending)
2598 pm_runtime_get_sync(&pdev->dev);
2599
2600 igb_down(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08002601 igb_free_irq(adapter);
2602
2603 igb_free_all_tx_resources(adapter);
2604 igb_free_all_rx_resources(adapter);
2605
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002606 if (!suspending)
2607 pm_runtime_put_sync(&pdev->dev);
Auke Kok9d5c8242008-01-24 02:22:38 -08002608 return 0;
2609}
2610
Yan, Zheng749ab2c2012-01-04 20:23:37 +00002611static int igb_close(struct net_device *netdev)
2612{
2613 return __igb_close(netdev, false);
2614}
2615
Auke Kok9d5c8242008-01-24 02:22:38 -08002616/**
2617 * igb_setup_tx_resources - allocate Tx resources (Descriptors)
Auke Kok9d5c8242008-01-24 02:22:38 -08002618 * @tx_ring: tx descriptor ring (for a specific queue) to setup
2619 *
2620 * Return 0 on success, negative on failure
2621 **/
Alexander Duyck80785292009-10-27 15:51:47 +00002622int igb_setup_tx_resources(struct igb_ring *tx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08002623{
Alexander Duyck59d71982010-04-27 13:09:25 +00002624 struct device *dev = tx_ring->dev;
Auke Kok9d5c8242008-01-24 02:22:38 -08002625 int size;
2626
Alexander Duyck06034642011-08-26 07:44:22 +00002627 size = sizeof(struct igb_tx_buffer) * tx_ring->count;
Alexander Duyckf33005a2012-09-13 06:27:55 +00002628
2629 tx_ring->tx_buffer_info = vzalloc(size);
Alexander Duyck06034642011-08-26 07:44:22 +00002630 if (!tx_ring->tx_buffer_info)
Auke Kok9d5c8242008-01-24 02:22:38 -08002631 goto err;
Auke Kok9d5c8242008-01-24 02:22:38 -08002632
2633 /* round up to nearest 4K */
Alexander Duyck85e8d002009-02-16 00:00:20 -08002634 tx_ring->size = tx_ring->count * sizeof(union e1000_adv_tx_desc);
Auke Kok9d5c8242008-01-24 02:22:38 -08002635 tx_ring->size = ALIGN(tx_ring->size, 4096);
2636
Alexander Duyck5536d212012-09-25 00:31:17 +00002637 tx_ring->desc = dma_alloc_coherent(dev, tx_ring->size,
2638 &tx_ring->dma, GFP_KERNEL);
Auke Kok9d5c8242008-01-24 02:22:38 -08002639 if (!tx_ring->desc)
2640 goto err;
2641
Auke Kok9d5c8242008-01-24 02:22:38 -08002642 tx_ring->next_to_use = 0;
2643 tx_ring->next_to_clean = 0;
Alexander Duyck81c2fc22011-08-26 07:45:20 +00002644
Auke Kok9d5c8242008-01-24 02:22:38 -08002645 return 0;
2646
2647err:
Alexander Duyck06034642011-08-26 07:44:22 +00002648 vfree(tx_ring->tx_buffer_info);
Alexander Duyckf33005a2012-09-13 06:27:55 +00002649 tx_ring->tx_buffer_info = NULL;
2650 dev_err(dev, "Unable to allocate memory for the Tx descriptor ring\n");
Auke Kok9d5c8242008-01-24 02:22:38 -08002651 return -ENOMEM;
2652}
2653
2654/**
2655 * igb_setup_all_tx_resources - wrapper to allocate Tx resources
2656 * (Descriptors) for all queues
2657 * @adapter: board private structure
2658 *
2659 * Return 0 on success, negative on failure
2660 **/
2661static int igb_setup_all_tx_resources(struct igb_adapter *adapter)
2662{
Alexander Duyck439705e2009-10-27 23:49:20 +00002663 struct pci_dev *pdev = adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08002664 int i, err = 0;
2665
2666 for (i = 0; i < adapter->num_tx_queues; i++) {
Alexander Duyck3025a442010-02-17 01:02:39 +00002667 err = igb_setup_tx_resources(adapter->tx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08002668 if (err) {
Alexander Duyck439705e2009-10-27 23:49:20 +00002669 dev_err(&pdev->dev,
Auke Kok9d5c8242008-01-24 02:22:38 -08002670 "Allocation for Tx Queue %u failed\n", i);
2671 for (i--; i >= 0; i--)
Alexander Duyck3025a442010-02-17 01:02:39 +00002672 igb_free_tx_resources(adapter->tx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08002673 break;
2674 }
2675 }
2676
2677 return err;
2678}
2679
2680/**
Alexander Duyck85b430b2009-10-27 15:50:29 +00002681 * igb_setup_tctl - configure the transmit control registers
2682 * @adapter: Board private structure
Auke Kok9d5c8242008-01-24 02:22:38 -08002683 **/
Alexander Duyckd7ee5b32009-10-27 15:54:23 +00002684void igb_setup_tctl(struct igb_adapter *adapter)
Auke Kok9d5c8242008-01-24 02:22:38 -08002685{
Auke Kok9d5c8242008-01-24 02:22:38 -08002686 struct e1000_hw *hw = &adapter->hw;
2687 u32 tctl;
Auke Kok9d5c8242008-01-24 02:22:38 -08002688
Alexander Duyck85b430b2009-10-27 15:50:29 +00002689 /* disable queue 0 which is enabled by default on 82575 and 82576 */
2690 wr32(E1000_TXDCTL(0), 0);
Auke Kok9d5c8242008-01-24 02:22:38 -08002691
2692 /* Program the Transmit Control Register */
Auke Kok9d5c8242008-01-24 02:22:38 -08002693 tctl = rd32(E1000_TCTL);
2694 tctl &= ~E1000_TCTL_CT;
2695 tctl |= E1000_TCTL_PSP | E1000_TCTL_RTLC |
2696 (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
2697
2698 igb_config_collision_dist(hw);
2699
Auke Kok9d5c8242008-01-24 02:22:38 -08002700 /* Enable transmits */
2701 tctl |= E1000_TCTL_EN;
2702
2703 wr32(E1000_TCTL, tctl);
2704}
2705
2706/**
Alexander Duyck85b430b2009-10-27 15:50:29 +00002707 * igb_configure_tx_ring - Configure transmit ring after Reset
2708 * @adapter: board private structure
2709 * @ring: tx ring to configure
2710 *
2711 * Configure a transmit ring after a reset.
2712 **/
Alexander Duyckd7ee5b32009-10-27 15:54:23 +00002713void igb_configure_tx_ring(struct igb_adapter *adapter,
2714 struct igb_ring *ring)
Alexander Duyck85b430b2009-10-27 15:50:29 +00002715{
2716 struct e1000_hw *hw = &adapter->hw;
Alexander Duycka74420e2011-08-26 07:43:27 +00002717 u32 txdctl = 0;
Alexander Duyck85b430b2009-10-27 15:50:29 +00002718 u64 tdba = ring->dma;
2719 int reg_idx = ring->reg_idx;
2720
2721 /* disable the queue */
Alexander Duycka74420e2011-08-26 07:43:27 +00002722 wr32(E1000_TXDCTL(reg_idx), 0);
Alexander Duyck85b430b2009-10-27 15:50:29 +00002723 wrfl();
2724 mdelay(10);
2725
2726 wr32(E1000_TDLEN(reg_idx),
2727 ring->count * sizeof(union e1000_adv_tx_desc));
2728 wr32(E1000_TDBAL(reg_idx),
2729 tdba & 0x00000000ffffffffULL);
2730 wr32(E1000_TDBAH(reg_idx), tdba >> 32);
2731
Alexander Duyckfce99e32009-10-27 15:51:27 +00002732 ring->tail = hw->hw_addr + E1000_TDT(reg_idx);
Alexander Duycka74420e2011-08-26 07:43:27 +00002733 wr32(E1000_TDH(reg_idx), 0);
Alexander Duyckfce99e32009-10-27 15:51:27 +00002734 writel(0, ring->tail);
Alexander Duyck85b430b2009-10-27 15:50:29 +00002735
2736 txdctl |= IGB_TX_PTHRESH;
2737 txdctl |= IGB_TX_HTHRESH << 8;
2738 txdctl |= IGB_TX_WTHRESH << 16;
2739
2740 txdctl |= E1000_TXDCTL_QUEUE_ENABLE;
2741 wr32(E1000_TXDCTL(reg_idx), txdctl);
2742}
2743
2744/**
2745 * igb_configure_tx - Configure transmit Unit after Reset
2746 * @adapter: board private structure
2747 *
2748 * Configure the Tx unit of the MAC after a reset.
2749 **/
2750static void igb_configure_tx(struct igb_adapter *adapter)
2751{
2752 int i;
2753
2754 for (i = 0; i < adapter->num_tx_queues; i++)
Alexander Duyck3025a442010-02-17 01:02:39 +00002755 igb_configure_tx_ring(adapter, adapter->tx_ring[i]);
Alexander Duyck85b430b2009-10-27 15:50:29 +00002756}
2757
2758/**
Auke Kok9d5c8242008-01-24 02:22:38 -08002759 * igb_setup_rx_resources - allocate Rx resources (Descriptors)
Auke Kok9d5c8242008-01-24 02:22:38 -08002760 * @rx_ring: rx descriptor ring (for a specific queue) to setup
2761 *
2762 * Returns 0 on success, negative on failure
2763 **/
Alexander Duyck80785292009-10-27 15:51:47 +00002764int igb_setup_rx_resources(struct igb_ring *rx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08002765{
Alexander Duyck59d71982010-04-27 13:09:25 +00002766 struct device *dev = rx_ring->dev;
Alexander Duyckf33005a2012-09-13 06:27:55 +00002767 int size;
Auke Kok9d5c8242008-01-24 02:22:38 -08002768
Alexander Duyck06034642011-08-26 07:44:22 +00002769 size = sizeof(struct igb_rx_buffer) * rx_ring->count;
Alexander Duyckf33005a2012-09-13 06:27:55 +00002770
2771 rx_ring->rx_buffer_info = vzalloc(size);
Alexander Duyck06034642011-08-26 07:44:22 +00002772 if (!rx_ring->rx_buffer_info)
Auke Kok9d5c8242008-01-24 02:22:38 -08002773 goto err;
Auke Kok9d5c8242008-01-24 02:22:38 -08002774
Auke Kok9d5c8242008-01-24 02:22:38 -08002775 /* Round up to nearest 4K */
Alexander Duyckf33005a2012-09-13 06:27:55 +00002776 rx_ring->size = rx_ring->count * sizeof(union e1000_adv_rx_desc);
Auke Kok9d5c8242008-01-24 02:22:38 -08002777 rx_ring->size = ALIGN(rx_ring->size, 4096);
2778
Alexander Duyck5536d212012-09-25 00:31:17 +00002779 rx_ring->desc = dma_alloc_coherent(dev, rx_ring->size,
2780 &rx_ring->dma, GFP_KERNEL);
Auke Kok9d5c8242008-01-24 02:22:38 -08002781 if (!rx_ring->desc)
2782 goto err;
2783
Alexander Duyckcbc8e552012-09-25 00:31:02 +00002784 rx_ring->next_to_alloc = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08002785 rx_ring->next_to_clean = 0;
2786 rx_ring->next_to_use = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08002787
Auke Kok9d5c8242008-01-24 02:22:38 -08002788 return 0;
2789
2790err:
Alexander Duyck06034642011-08-26 07:44:22 +00002791 vfree(rx_ring->rx_buffer_info);
2792 rx_ring->rx_buffer_info = NULL;
Alexander Duyckf33005a2012-09-13 06:27:55 +00002793 dev_err(dev, "Unable to allocate memory for the Rx descriptor ring\n");
Auke Kok9d5c8242008-01-24 02:22:38 -08002794 return -ENOMEM;
2795}
2796
2797/**
2798 * igb_setup_all_rx_resources - wrapper to allocate Rx resources
2799 * (Descriptors) for all queues
2800 * @adapter: board private structure
2801 *
2802 * Return 0 on success, negative on failure
2803 **/
2804static int igb_setup_all_rx_resources(struct igb_adapter *adapter)
2805{
Alexander Duyck439705e2009-10-27 23:49:20 +00002806 struct pci_dev *pdev = adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08002807 int i, err = 0;
2808
2809 for (i = 0; i < adapter->num_rx_queues; i++) {
Alexander Duyck3025a442010-02-17 01:02:39 +00002810 err = igb_setup_rx_resources(adapter->rx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08002811 if (err) {
Alexander Duyck439705e2009-10-27 23:49:20 +00002812 dev_err(&pdev->dev,
Auke Kok9d5c8242008-01-24 02:22:38 -08002813 "Allocation for Rx Queue %u failed\n", i);
2814 for (i--; i >= 0; i--)
Alexander Duyck3025a442010-02-17 01:02:39 +00002815 igb_free_rx_resources(adapter->rx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08002816 break;
2817 }
2818 }
2819
2820 return err;
2821}
2822
2823/**
Alexander Duyck06cf2662009-10-27 15:53:25 +00002824 * igb_setup_mrqc - configure the multiple receive queue control registers
2825 * @adapter: Board private structure
2826 **/
2827static void igb_setup_mrqc(struct igb_adapter *adapter)
2828{
2829 struct e1000_hw *hw = &adapter->hw;
2830 u32 mrqc, rxcsum;
Alexander Duyck797fd4b2012-09-13 06:28:11 +00002831 u32 j, num_rx_queues, shift = 0;
Alexander Duycka57fe232012-09-13 06:28:16 +00002832 static const u32 rsskey[10] = { 0xDA565A6D, 0xC20E5B25, 0x3D256741,
2833 0xB08FA343, 0xCB2BCAD0, 0xB4307BAE,
2834 0xA32DCB77, 0x0CF23080, 0x3BB7426A,
2835 0xFA01ACBE };
Alexander Duyck06cf2662009-10-27 15:53:25 +00002836
2837 /* Fill out hash function seeds */
Alexander Duycka57fe232012-09-13 06:28:16 +00002838 for (j = 0; j < 10; j++)
2839 wr32(E1000_RSSRK(j), rsskey[j]);
Alexander Duyck06cf2662009-10-27 15:53:25 +00002840
Alexander Duycka99955f2009-11-12 18:37:19 +00002841 num_rx_queues = adapter->rss_queues;
Alexander Duyck06cf2662009-10-27 15:53:25 +00002842
Alexander Duyck797fd4b2012-09-13 06:28:11 +00002843 switch (hw->mac.type) {
2844 case e1000_82575:
2845 shift = 6;
2846 break;
2847 case e1000_82576:
2848 /* 82576 supports 2 RSS queues for SR-IOV */
2849 if (adapter->vfs_allocated_count) {
Alexander Duyck06cf2662009-10-27 15:53:25 +00002850 shift = 3;
2851 num_rx_queues = 2;
Alexander Duyck06cf2662009-10-27 15:53:25 +00002852 }
Alexander Duyck797fd4b2012-09-13 06:28:11 +00002853 break;
2854 default:
2855 break;
Alexander Duyck06cf2662009-10-27 15:53:25 +00002856 }
2857
Alexander Duyck797fd4b2012-09-13 06:28:11 +00002858 /*
2859 * Populate the indirection table 4 entries at a time. To do this
2860 * we are generating the results for n and n+2 and then interleaving
2861 * those with the results with n+1 and n+3.
2862 */
2863 for (j = 0; j < 32; j++) {
2864 /* first pass generates n and n+2 */
2865 u32 base = ((j * 0x00040004) + 0x00020000) * num_rx_queues;
2866 u32 reta = (base & 0x07800780) >> (7 - shift);
2867
2868 /* second pass generates n+1 and n+3 */
2869 base += 0x00010001 * num_rx_queues;
2870 reta |= (base & 0x07800780) << (1 + shift);
2871
2872 wr32(E1000_RETA(j), reta);
Alexander Duyck06cf2662009-10-27 15:53:25 +00002873 }
2874
2875 /*
2876 * Disable raw packet checksumming so that RSS hash is placed in
2877 * descriptor on writeback. No need to enable TCP/UDP/IP checksum
2878 * offloads as they are enabled by default
2879 */
2880 rxcsum = rd32(E1000_RXCSUM);
2881 rxcsum |= E1000_RXCSUM_PCSD;
2882
2883 if (adapter->hw.mac.type >= e1000_82576)
2884 /* Enable Receive Checksum Offload for SCTP */
2885 rxcsum |= E1000_RXCSUM_CRCOFL;
2886
2887 /* Don't need to set TUOFL or IPOFL, they default to 1 */
2888 wr32(E1000_RXCSUM, rxcsum);
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002889
Akeem G. Abodunrin039454a2012-11-13 04:03:21 +00002890 /* Generate RSS hash based on packet types, TCP/UDP
2891 * port numbers and/or IPv4/v6 src and dst addresses
2892 */
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002893 mrqc = E1000_MRQC_RSS_FIELD_IPV4 |
2894 E1000_MRQC_RSS_FIELD_IPV4_TCP |
2895 E1000_MRQC_RSS_FIELD_IPV6 |
2896 E1000_MRQC_RSS_FIELD_IPV6_TCP |
2897 E1000_MRQC_RSS_FIELD_IPV6_TCP_EX;
Alexander Duyck06cf2662009-10-27 15:53:25 +00002898
Akeem G. Abodunrin039454a2012-11-13 04:03:21 +00002899 if (adapter->flags & IGB_FLAG_RSS_FIELD_IPV4_UDP)
2900 mrqc |= E1000_MRQC_RSS_FIELD_IPV4_UDP;
2901 if (adapter->flags & IGB_FLAG_RSS_FIELD_IPV6_UDP)
2902 mrqc |= E1000_MRQC_RSS_FIELD_IPV6_UDP;
2903
Alexander Duyck06cf2662009-10-27 15:53:25 +00002904 /* If VMDq is enabled then we set the appropriate mode for that, else
2905 * we default to RSS so that an RSS hash is calculated per packet even
2906 * if we are only using one queue */
2907 if (adapter->vfs_allocated_count) {
2908 if (hw->mac.type > e1000_82575) {
2909 /* Set the default pool for the PF's first queue */
2910 u32 vtctl = rd32(E1000_VT_CTL);
2911 vtctl &= ~(E1000_VT_CTL_DEFAULT_POOL_MASK |
2912 E1000_VT_CTL_DISABLE_DEF_POOL);
2913 vtctl |= adapter->vfs_allocated_count <<
2914 E1000_VT_CTL_DEFAULT_POOL_SHIFT;
2915 wr32(E1000_VT_CTL, vtctl);
2916 }
Alexander Duycka99955f2009-11-12 18:37:19 +00002917 if (adapter->rss_queues > 1)
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002918 mrqc |= E1000_MRQC_ENABLE_VMDQ_RSS_2Q;
Alexander Duyck06cf2662009-10-27 15:53:25 +00002919 else
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002920 mrqc |= E1000_MRQC_ENABLE_VMDQ;
Alexander Duyck06cf2662009-10-27 15:53:25 +00002921 } else {
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00002922 if (hw->mac.type != e1000_i211)
2923 mrqc |= E1000_MRQC_ENABLE_RSS_4Q;
Alexander Duyck06cf2662009-10-27 15:53:25 +00002924 }
2925 igb_vmm_control(adapter);
2926
Alexander Duyck06cf2662009-10-27 15:53:25 +00002927 wr32(E1000_MRQC, mrqc);
2928}
2929
2930/**
Auke Kok9d5c8242008-01-24 02:22:38 -08002931 * igb_setup_rctl - configure the receive control registers
2932 * @adapter: Board private structure
2933 **/
Alexander Duyckd7ee5b32009-10-27 15:54:23 +00002934void igb_setup_rctl(struct igb_adapter *adapter)
Auke Kok9d5c8242008-01-24 02:22:38 -08002935{
2936 struct e1000_hw *hw = &adapter->hw;
2937 u32 rctl;
Auke Kok9d5c8242008-01-24 02:22:38 -08002938
2939 rctl = rd32(E1000_RCTL);
2940
2941 rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
Alexander Duyck69d728b2008-11-25 01:04:03 -08002942 rctl &= ~(E1000_RCTL_LBM_TCVR | E1000_RCTL_LBM_MAC);
Auke Kok9d5c8242008-01-24 02:22:38 -08002943
Alexander Duyck69d728b2008-11-25 01:04:03 -08002944 rctl |= E1000_RCTL_EN | E1000_RCTL_BAM | E1000_RCTL_RDMTS_HALF |
Alexander Duyck28b07592009-02-06 23:20:31 +00002945 (hw->mac.mc_filter_type << E1000_RCTL_MO_SHIFT);
Auke Kok9d5c8242008-01-24 02:22:38 -08002946
Auke Kok87cb7e82008-07-08 15:08:29 -07002947 /*
2948 * enable stripping of CRC. It's unlikely this will break BMC
2949 * redirection as it did with e1000. Newer features require
2950 * that the HW strips the CRC.
Alexander Duyck73cd78f2009-02-12 18:16:59 +00002951 */
Auke Kok87cb7e82008-07-08 15:08:29 -07002952 rctl |= E1000_RCTL_SECRC;
Auke Kok9d5c8242008-01-24 02:22:38 -08002953
Alexander Duyck559e9c42009-10-27 23:52:50 +00002954 /* disable store bad packets and clear size bits. */
Alexander Duyckec54d7d2009-01-31 00:52:57 -08002955 rctl &= ~(E1000_RCTL_SBP | E1000_RCTL_SZ_256);
Auke Kok9d5c8242008-01-24 02:22:38 -08002956
Alexander Duyck6ec43fe2009-10-27 15:50:48 +00002957 /* enable LPE to prevent packets larger than max_frame_size */
2958 rctl |= E1000_RCTL_LPE;
Auke Kok9d5c8242008-01-24 02:22:38 -08002959
Alexander Duyck952f72a2009-10-27 15:51:07 +00002960 /* disable queue 0 to prevent tail write w/o re-config */
2961 wr32(E1000_RXDCTL(0), 0);
Auke Kok9d5c8242008-01-24 02:22:38 -08002962
Alexander Duycke1739522009-02-19 20:39:44 -08002963 /* Attention!!! For SR-IOV PF driver operations you must enable
2964 * queue drop for all VF and PF queues to prevent head of line blocking
2965 * if an un-trusted VF does not provide descriptors to hardware.
2966 */
2967 if (adapter->vfs_allocated_count) {
Alexander Duycke1739522009-02-19 20:39:44 -08002968 /* set all queue drop enable bits */
2969 wr32(E1000_QDE, ALL_QUEUES);
Alexander Duycke1739522009-02-19 20:39:44 -08002970 }
2971
Ben Greear89eaefb2012-03-06 09:41:58 +00002972 /* This is useful for sniffing bad packets. */
2973 if (adapter->netdev->features & NETIF_F_RXALL) {
2974 /* UPE and MPE will be handled by normal PROMISC logic
2975 * in e1000e_set_rx_mode */
2976 rctl |= (E1000_RCTL_SBP | /* Receive bad packets */
2977 E1000_RCTL_BAM | /* RX All Bcast Pkts */
2978 E1000_RCTL_PMCF); /* RX All MAC Ctrl Pkts */
2979
2980 rctl &= ~(E1000_RCTL_VFE | /* Disable VLAN filter */
2981 E1000_RCTL_DPF | /* Allow filtered pause */
2982 E1000_RCTL_CFIEN); /* Dis VLAN CFIEN Filter */
2983 /* Do not mess with E1000_CTRL_VME, it affects transmit as well,
2984 * and that breaks VLANs.
2985 */
2986 }
2987
Auke Kok9d5c8242008-01-24 02:22:38 -08002988 wr32(E1000_RCTL, rctl);
2989}
2990
Alexander Duyck7d5753f2009-10-27 23:47:16 +00002991static inline int igb_set_vf_rlpml(struct igb_adapter *adapter, int size,
2992 int vfn)
2993{
2994 struct e1000_hw *hw = &adapter->hw;
2995 u32 vmolr;
2996
2997 /* if it isn't the PF check to see if VFs are enabled and
2998 * increase the size to support vlan tags */
2999 if (vfn < adapter->vfs_allocated_count &&
3000 adapter->vf_data[vfn].vlans_enabled)
3001 size += VLAN_TAG_SIZE;
3002
3003 vmolr = rd32(E1000_VMOLR(vfn));
3004 vmolr &= ~E1000_VMOLR_RLPML_MASK;
3005 vmolr |= size | E1000_VMOLR_LPE;
3006 wr32(E1000_VMOLR(vfn), vmolr);
3007
3008 return 0;
3009}
3010
Auke Kok9d5c8242008-01-24 02:22:38 -08003011/**
Alexander Duycke1739522009-02-19 20:39:44 -08003012 * igb_rlpml_set - set maximum receive packet size
3013 * @adapter: board private structure
3014 *
3015 * Configure maximum receivable packet size.
3016 **/
3017static void igb_rlpml_set(struct igb_adapter *adapter)
3018{
Alexander Duyck153285f2011-08-26 07:43:32 +00003019 u32 max_frame_size = adapter->max_frame_size;
Alexander Duycke1739522009-02-19 20:39:44 -08003020 struct e1000_hw *hw = &adapter->hw;
3021 u16 pf_id = adapter->vfs_allocated_count;
3022
Alexander Duycke1739522009-02-19 20:39:44 -08003023 if (pf_id) {
3024 igb_set_vf_rlpml(adapter, max_frame_size, pf_id);
Alexander Duyck153285f2011-08-26 07:43:32 +00003025 /*
3026 * If we're in VMDQ or SR-IOV mode, then set global RLPML
3027 * to our max jumbo frame size, in case we need to enable
3028 * jumbo frames on one of the rings later.
3029 * This will not pass over-length frames into the default
3030 * queue because it's gated by the VMOLR.RLPML.
3031 */
Alexander Duyck7d5753f2009-10-27 23:47:16 +00003032 max_frame_size = MAX_JUMBO_FRAME_SIZE;
Alexander Duycke1739522009-02-19 20:39:44 -08003033 }
3034
3035 wr32(E1000_RLPML, max_frame_size);
3036}
3037
Williams, Mitch A8151d292010-02-10 01:44:24 +00003038static inline void igb_set_vmolr(struct igb_adapter *adapter,
3039 int vfn, bool aupe)
Alexander Duyck7d5753f2009-10-27 23:47:16 +00003040{
3041 struct e1000_hw *hw = &adapter->hw;
3042 u32 vmolr;
3043
3044 /*
3045 * This register exists only on 82576 and newer so if we are older then
3046 * we should exit and do nothing
3047 */
3048 if (hw->mac.type < e1000_82576)
3049 return;
3050
3051 vmolr = rd32(E1000_VMOLR(vfn));
Williams, Mitch A8151d292010-02-10 01:44:24 +00003052 vmolr |= E1000_VMOLR_STRVLAN; /* Strip vlan tags */
3053 if (aupe)
3054 vmolr |= E1000_VMOLR_AUPE; /* Accept untagged packets */
3055 else
3056 vmolr &= ~(E1000_VMOLR_AUPE); /* Tagged packets ONLY */
Alexander Duyck7d5753f2009-10-27 23:47:16 +00003057
3058 /* clear all bits that might not be set */
3059 vmolr &= ~(E1000_VMOLR_BAM | E1000_VMOLR_RSSE);
3060
Alexander Duycka99955f2009-11-12 18:37:19 +00003061 if (adapter->rss_queues > 1 && vfn == adapter->vfs_allocated_count)
Alexander Duyck7d5753f2009-10-27 23:47:16 +00003062 vmolr |= E1000_VMOLR_RSSE; /* enable RSS */
3063 /*
3064 * for VMDq only allow the VFs and pool 0 to accept broadcast and
3065 * multicast packets
3066 */
3067 if (vfn <= adapter->vfs_allocated_count)
3068 vmolr |= E1000_VMOLR_BAM; /* Accept broadcast */
3069
3070 wr32(E1000_VMOLR(vfn), vmolr);
3071}
3072
Alexander Duycke1739522009-02-19 20:39:44 -08003073/**
Alexander Duyck85b430b2009-10-27 15:50:29 +00003074 * igb_configure_rx_ring - Configure a receive ring after Reset
3075 * @adapter: board private structure
3076 * @ring: receive ring to be configured
3077 *
3078 * Configure the Rx unit of the MAC after a reset.
3079 **/
Alexander Duyckd7ee5b32009-10-27 15:54:23 +00003080void igb_configure_rx_ring(struct igb_adapter *adapter,
3081 struct igb_ring *ring)
Alexander Duyck85b430b2009-10-27 15:50:29 +00003082{
3083 struct e1000_hw *hw = &adapter->hw;
3084 u64 rdba = ring->dma;
3085 int reg_idx = ring->reg_idx;
Alexander Duycka74420e2011-08-26 07:43:27 +00003086 u32 srrctl = 0, rxdctl = 0;
Alexander Duyck85b430b2009-10-27 15:50:29 +00003087
3088 /* disable the queue */
Alexander Duycka74420e2011-08-26 07:43:27 +00003089 wr32(E1000_RXDCTL(reg_idx), 0);
Alexander Duyck85b430b2009-10-27 15:50:29 +00003090
3091 /* Set DMA base address registers */
3092 wr32(E1000_RDBAL(reg_idx),
3093 rdba & 0x00000000ffffffffULL);
3094 wr32(E1000_RDBAH(reg_idx), rdba >> 32);
3095 wr32(E1000_RDLEN(reg_idx),
3096 ring->count * sizeof(union e1000_adv_rx_desc));
3097
3098 /* initialize head and tail */
Alexander Duyckfce99e32009-10-27 15:51:27 +00003099 ring->tail = hw->hw_addr + E1000_RDT(reg_idx);
Alexander Duycka74420e2011-08-26 07:43:27 +00003100 wr32(E1000_RDH(reg_idx), 0);
Alexander Duyckfce99e32009-10-27 15:51:27 +00003101 writel(0, ring->tail);
Alexander Duyck85b430b2009-10-27 15:50:29 +00003102
Alexander Duyck952f72a2009-10-27 15:51:07 +00003103 /* set descriptor configuration */
Alexander Duyck44390ca2011-08-26 07:43:38 +00003104 srrctl = IGB_RX_HDR_LEN << E1000_SRRCTL_BSIZEHDRSIZE_SHIFT;
Alexander Duyckde78d1f2012-09-25 00:31:12 +00003105 srrctl |= IGB_RX_BUFSZ >> E1000_SRRCTL_BSIZEPKT_SHIFT;
Alexander Duyck1a1c2252012-09-25 00:30:52 +00003106 srrctl |= E1000_SRRCTL_DESCTYPE_ADV_ONEBUF;
Alexander Duyck06218a82011-08-26 07:46:55 +00003107 if (hw->mac.type >= e1000_82580)
Nick Nunley757b77e2010-03-26 11:36:47 +00003108 srrctl |= E1000_SRRCTL_TIMESTAMP;
Nick Nunleye6bdb6f2010-02-17 01:03:38 +00003109 /* Only set Drop Enable if we are supporting multiple queues */
3110 if (adapter->vfs_allocated_count || adapter->num_rx_queues > 1)
3111 srrctl |= E1000_SRRCTL_DROP_EN;
Alexander Duyck952f72a2009-10-27 15:51:07 +00003112
3113 wr32(E1000_SRRCTL(reg_idx), srrctl);
3114
Alexander Duyck7d5753f2009-10-27 23:47:16 +00003115 /* set filtering for VMDQ pools */
Williams, Mitch A8151d292010-02-10 01:44:24 +00003116 igb_set_vmolr(adapter, reg_idx & 0x7, true);
Alexander Duyck7d5753f2009-10-27 23:47:16 +00003117
Alexander Duyck85b430b2009-10-27 15:50:29 +00003118 rxdctl |= IGB_RX_PTHRESH;
3119 rxdctl |= IGB_RX_HTHRESH << 8;
3120 rxdctl |= IGB_RX_WTHRESH << 16;
Alexander Duycka74420e2011-08-26 07:43:27 +00003121
3122 /* enable receive descriptor fetching */
3123 rxdctl |= E1000_RXDCTL_QUEUE_ENABLE;
Alexander Duyck85b430b2009-10-27 15:50:29 +00003124 wr32(E1000_RXDCTL(reg_idx), rxdctl);
3125}
3126
3127/**
Auke Kok9d5c8242008-01-24 02:22:38 -08003128 * igb_configure_rx - Configure receive Unit after Reset
3129 * @adapter: board private structure
3130 *
3131 * Configure the Rx unit of the MAC after a reset.
3132 **/
3133static void igb_configure_rx(struct igb_adapter *adapter)
3134{
Hannes Eder91075842009-02-18 19:36:04 -08003135 int i;
Auke Kok9d5c8242008-01-24 02:22:38 -08003136
Alexander Duyck68d480c2009-10-05 06:33:08 +00003137 /* set UTA to appropriate mode */
3138 igb_set_uta(adapter);
3139
Alexander Duyck26ad9172009-10-05 06:32:49 +00003140 /* set the correct pool for the PF default MAC address in entry 0 */
3141 igb_rar_set_qsel(adapter, adapter->hw.mac.addr, 0,
3142 adapter->vfs_allocated_count);
3143
Alexander Duyck06cf2662009-10-27 15:53:25 +00003144 /* Setup the HW Rx Head and Tail Descriptor Pointers and
3145 * the Base and Length of the Rx Descriptor Ring */
3146 for (i = 0; i < adapter->num_rx_queues; i++)
Alexander Duyck3025a442010-02-17 01:02:39 +00003147 igb_configure_rx_ring(adapter, adapter->rx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08003148}
3149
3150/**
3151 * igb_free_tx_resources - Free Tx Resources per Queue
Auke Kok9d5c8242008-01-24 02:22:38 -08003152 * @tx_ring: Tx descriptor ring for a specific queue
3153 *
3154 * Free all transmit software resources
3155 **/
Alexander Duyck68fd9912008-11-20 00:48:10 -08003156void igb_free_tx_resources(struct igb_ring *tx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08003157{
Mitch Williams3b644cf2008-06-27 10:59:48 -07003158 igb_clean_tx_ring(tx_ring);
Auke Kok9d5c8242008-01-24 02:22:38 -08003159
Alexander Duyck06034642011-08-26 07:44:22 +00003160 vfree(tx_ring->tx_buffer_info);
3161 tx_ring->tx_buffer_info = NULL;
Auke Kok9d5c8242008-01-24 02:22:38 -08003162
Alexander Duyck439705e2009-10-27 23:49:20 +00003163 /* if not set, then don't free */
3164 if (!tx_ring->desc)
3165 return;
3166
Alexander Duyck59d71982010-04-27 13:09:25 +00003167 dma_free_coherent(tx_ring->dev, tx_ring->size,
3168 tx_ring->desc, tx_ring->dma);
Auke Kok9d5c8242008-01-24 02:22:38 -08003169
3170 tx_ring->desc = NULL;
3171}
3172
3173/**
3174 * igb_free_all_tx_resources - Free Tx Resources for All Queues
3175 * @adapter: board private structure
3176 *
3177 * Free all transmit software resources
3178 **/
3179static void igb_free_all_tx_resources(struct igb_adapter *adapter)
3180{
3181 int i;
3182
3183 for (i = 0; i < adapter->num_tx_queues; i++)
Alexander Duyck3025a442010-02-17 01:02:39 +00003184 igb_free_tx_resources(adapter->tx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08003185}
3186
Alexander Duyckebe42d12011-08-26 07:45:09 +00003187void igb_unmap_and_free_tx_resource(struct igb_ring *ring,
3188 struct igb_tx_buffer *tx_buffer)
Auke Kok9d5c8242008-01-24 02:22:38 -08003189{
Alexander Duyckebe42d12011-08-26 07:45:09 +00003190 if (tx_buffer->skb) {
3191 dev_kfree_skb_any(tx_buffer->skb);
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00003192 if (dma_unmap_len(tx_buffer, len))
Alexander Duyckebe42d12011-08-26 07:45:09 +00003193 dma_unmap_single(ring->dev,
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00003194 dma_unmap_addr(tx_buffer, dma),
3195 dma_unmap_len(tx_buffer, len),
Alexander Duyckebe42d12011-08-26 07:45:09 +00003196 DMA_TO_DEVICE);
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00003197 } else if (dma_unmap_len(tx_buffer, len)) {
Alexander Duyckebe42d12011-08-26 07:45:09 +00003198 dma_unmap_page(ring->dev,
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00003199 dma_unmap_addr(tx_buffer, dma),
3200 dma_unmap_len(tx_buffer, len),
Alexander Duyckebe42d12011-08-26 07:45:09 +00003201 DMA_TO_DEVICE);
Alexander Duyck6366ad32009-12-02 16:47:18 +00003202 }
Alexander Duyckebe42d12011-08-26 07:45:09 +00003203 tx_buffer->next_to_watch = NULL;
3204 tx_buffer->skb = NULL;
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00003205 dma_unmap_len_set(tx_buffer, len, 0);
Alexander Duyckebe42d12011-08-26 07:45:09 +00003206 /* buffer_info must be completely set up in the transmit path */
Auke Kok9d5c8242008-01-24 02:22:38 -08003207}
3208
3209/**
3210 * igb_clean_tx_ring - Free Tx Buffers
Auke Kok9d5c8242008-01-24 02:22:38 -08003211 * @tx_ring: ring to be cleaned
3212 **/
Mitch Williams3b644cf2008-06-27 10:59:48 -07003213static void igb_clean_tx_ring(struct igb_ring *tx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08003214{
Alexander Duyck06034642011-08-26 07:44:22 +00003215 struct igb_tx_buffer *buffer_info;
Auke Kok9d5c8242008-01-24 02:22:38 -08003216 unsigned long size;
Alexander Duyck6ad4edf2011-08-26 07:45:26 +00003217 u16 i;
Auke Kok9d5c8242008-01-24 02:22:38 -08003218
Alexander Duyck06034642011-08-26 07:44:22 +00003219 if (!tx_ring->tx_buffer_info)
Auke Kok9d5c8242008-01-24 02:22:38 -08003220 return;
3221 /* Free all the Tx ring sk_buffs */
3222
3223 for (i = 0; i < tx_ring->count; i++) {
Alexander Duyck06034642011-08-26 07:44:22 +00003224 buffer_info = &tx_ring->tx_buffer_info[i];
Alexander Duyck80785292009-10-27 15:51:47 +00003225 igb_unmap_and_free_tx_resource(tx_ring, buffer_info);
Auke Kok9d5c8242008-01-24 02:22:38 -08003226 }
3227
John Fastabenddad8a3b2012-04-23 12:22:39 +00003228 netdev_tx_reset_queue(txring_txq(tx_ring));
3229
Alexander Duyck06034642011-08-26 07:44:22 +00003230 size = sizeof(struct igb_tx_buffer) * tx_ring->count;
3231 memset(tx_ring->tx_buffer_info, 0, size);
Auke Kok9d5c8242008-01-24 02:22:38 -08003232
3233 /* Zero out the descriptor ring */
Auke Kok9d5c8242008-01-24 02:22:38 -08003234 memset(tx_ring->desc, 0, tx_ring->size);
3235
3236 tx_ring->next_to_use = 0;
3237 tx_ring->next_to_clean = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08003238}
3239
3240/**
3241 * igb_clean_all_tx_rings - Free Tx Buffers for all queues
3242 * @adapter: board private structure
3243 **/
3244static void igb_clean_all_tx_rings(struct igb_adapter *adapter)
3245{
3246 int i;
3247
3248 for (i = 0; i < adapter->num_tx_queues; i++)
Alexander Duyck3025a442010-02-17 01:02:39 +00003249 igb_clean_tx_ring(adapter->tx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08003250}
3251
3252/**
3253 * igb_free_rx_resources - Free Rx Resources
Auke Kok9d5c8242008-01-24 02:22:38 -08003254 * @rx_ring: ring to clean the resources from
3255 *
3256 * Free all receive software resources
3257 **/
Alexander Duyck68fd9912008-11-20 00:48:10 -08003258void igb_free_rx_resources(struct igb_ring *rx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08003259{
Mitch Williams3b644cf2008-06-27 10:59:48 -07003260 igb_clean_rx_ring(rx_ring);
Auke Kok9d5c8242008-01-24 02:22:38 -08003261
Alexander Duyck06034642011-08-26 07:44:22 +00003262 vfree(rx_ring->rx_buffer_info);
3263 rx_ring->rx_buffer_info = NULL;
Auke Kok9d5c8242008-01-24 02:22:38 -08003264
Alexander Duyck439705e2009-10-27 23:49:20 +00003265 /* if not set, then don't free */
3266 if (!rx_ring->desc)
3267 return;
3268
Alexander Duyck59d71982010-04-27 13:09:25 +00003269 dma_free_coherent(rx_ring->dev, rx_ring->size,
3270 rx_ring->desc, rx_ring->dma);
Auke Kok9d5c8242008-01-24 02:22:38 -08003271
3272 rx_ring->desc = NULL;
3273}
3274
3275/**
3276 * igb_free_all_rx_resources - Free Rx Resources for All Queues
3277 * @adapter: board private structure
3278 *
3279 * Free all receive software resources
3280 **/
3281static void igb_free_all_rx_resources(struct igb_adapter *adapter)
3282{
3283 int i;
3284
3285 for (i = 0; i < adapter->num_rx_queues; i++)
Alexander Duyck3025a442010-02-17 01:02:39 +00003286 igb_free_rx_resources(adapter->rx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08003287}
3288
3289/**
3290 * igb_clean_rx_ring - Free Rx Buffers per Queue
Auke Kok9d5c8242008-01-24 02:22:38 -08003291 * @rx_ring: ring to free buffers from
3292 **/
Mitch Williams3b644cf2008-06-27 10:59:48 -07003293static void igb_clean_rx_ring(struct igb_ring *rx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08003294{
Auke Kok9d5c8242008-01-24 02:22:38 -08003295 unsigned long size;
Alexander Duyckc023cd82011-08-26 07:43:43 +00003296 u16 i;
Auke Kok9d5c8242008-01-24 02:22:38 -08003297
Alexander Duyck1a1c2252012-09-25 00:30:52 +00003298 if (rx_ring->skb)
3299 dev_kfree_skb(rx_ring->skb);
3300 rx_ring->skb = NULL;
3301
Alexander Duyck06034642011-08-26 07:44:22 +00003302 if (!rx_ring->rx_buffer_info)
Auke Kok9d5c8242008-01-24 02:22:38 -08003303 return;
Alexander Duyck439705e2009-10-27 23:49:20 +00003304
Auke Kok9d5c8242008-01-24 02:22:38 -08003305 /* Free all the Rx ring sk_buffs */
3306 for (i = 0; i < rx_ring->count; i++) {
Alexander Duyck06034642011-08-26 07:44:22 +00003307 struct igb_rx_buffer *buffer_info = &rx_ring->rx_buffer_info[i];
Auke Kok9d5c8242008-01-24 02:22:38 -08003308
Alexander Duyckcbc8e552012-09-25 00:31:02 +00003309 if (!buffer_info->page)
3310 continue;
3311
3312 dma_unmap_page(rx_ring->dev,
3313 buffer_info->dma,
3314 PAGE_SIZE,
3315 DMA_FROM_DEVICE);
3316 __free_page(buffer_info->page);
3317
Alexander Duyck1a1c2252012-09-25 00:30:52 +00003318 buffer_info->page = NULL;
Auke Kok9d5c8242008-01-24 02:22:38 -08003319 }
3320
Alexander Duyck06034642011-08-26 07:44:22 +00003321 size = sizeof(struct igb_rx_buffer) * rx_ring->count;
3322 memset(rx_ring->rx_buffer_info, 0, size);
Auke Kok9d5c8242008-01-24 02:22:38 -08003323
3324 /* Zero out the descriptor ring */
3325 memset(rx_ring->desc, 0, rx_ring->size);
3326
Alexander Duyckcbc8e552012-09-25 00:31:02 +00003327 rx_ring->next_to_alloc = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08003328 rx_ring->next_to_clean = 0;
3329 rx_ring->next_to_use = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08003330}
3331
3332/**
3333 * igb_clean_all_rx_rings - Free Rx Buffers for all queues
3334 * @adapter: board private structure
3335 **/
3336static void igb_clean_all_rx_rings(struct igb_adapter *adapter)
3337{
3338 int i;
3339
3340 for (i = 0; i < adapter->num_rx_queues; i++)
Alexander Duyck3025a442010-02-17 01:02:39 +00003341 igb_clean_rx_ring(adapter->rx_ring[i]);
Auke Kok9d5c8242008-01-24 02:22:38 -08003342}
3343
3344/**
3345 * igb_set_mac - Change the Ethernet Address of the NIC
3346 * @netdev: network interface device structure
3347 * @p: pointer to an address structure
3348 *
3349 * Returns 0 on success, negative on failure
3350 **/
3351static int igb_set_mac(struct net_device *netdev, void *p)
3352{
3353 struct igb_adapter *adapter = netdev_priv(netdev);
Alexander Duyck28b07592009-02-06 23:20:31 +00003354 struct e1000_hw *hw = &adapter->hw;
Auke Kok9d5c8242008-01-24 02:22:38 -08003355 struct sockaddr *addr = p;
3356
3357 if (!is_valid_ether_addr(addr->sa_data))
3358 return -EADDRNOTAVAIL;
3359
3360 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
Alexander Duyck28b07592009-02-06 23:20:31 +00003361 memcpy(hw->mac.addr, addr->sa_data, netdev->addr_len);
Auke Kok9d5c8242008-01-24 02:22:38 -08003362
Alexander Duyck26ad9172009-10-05 06:32:49 +00003363 /* set the correct pool for the new PF MAC address in entry 0 */
3364 igb_rar_set_qsel(adapter, hw->mac.addr, 0,
3365 adapter->vfs_allocated_count);
Alexander Duycke1739522009-02-19 20:39:44 -08003366
Auke Kok9d5c8242008-01-24 02:22:38 -08003367 return 0;
3368}
3369
3370/**
Alexander Duyck68d480c2009-10-05 06:33:08 +00003371 * igb_write_mc_addr_list - write multicast addresses to MTA
3372 * @netdev: network interface device structure
3373 *
3374 * Writes multicast address list to the MTA hash table.
3375 * Returns: -ENOMEM on failure
3376 * 0 on no addresses written
3377 * X on writing X addresses to MTA
3378 **/
3379static int igb_write_mc_addr_list(struct net_device *netdev)
3380{
3381 struct igb_adapter *adapter = netdev_priv(netdev);
3382 struct e1000_hw *hw = &adapter->hw;
Jiri Pirko22bedad32010-04-01 21:22:57 +00003383 struct netdev_hw_addr *ha;
Alexander Duyck68d480c2009-10-05 06:33:08 +00003384 u8 *mta_list;
Alexander Duyck68d480c2009-10-05 06:33:08 +00003385 int i;
3386
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00003387 if (netdev_mc_empty(netdev)) {
Alexander Duyck68d480c2009-10-05 06:33:08 +00003388 /* nothing to program, so clear mc list */
3389 igb_update_mc_addr_list(hw, NULL, 0);
3390 igb_restore_vf_multicasts(adapter);
3391 return 0;
3392 }
3393
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00003394 mta_list = kzalloc(netdev_mc_count(netdev) * 6, GFP_ATOMIC);
Alexander Duyck68d480c2009-10-05 06:33:08 +00003395 if (!mta_list)
3396 return -ENOMEM;
3397
Alexander Duyck68d480c2009-10-05 06:33:08 +00003398 /* The shared function expects a packed array of only addresses. */
Jiri Pirko48e2f182010-02-22 09:22:26 +00003399 i = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +00003400 netdev_for_each_mc_addr(ha, netdev)
3401 memcpy(mta_list + (i++ * ETH_ALEN), ha->addr, ETH_ALEN);
Alexander Duyck68d480c2009-10-05 06:33:08 +00003402
Alexander Duyck68d480c2009-10-05 06:33:08 +00003403 igb_update_mc_addr_list(hw, mta_list, i);
3404 kfree(mta_list);
3405
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00003406 return netdev_mc_count(netdev);
Alexander Duyck68d480c2009-10-05 06:33:08 +00003407}
3408
3409/**
3410 * igb_write_uc_addr_list - write unicast addresses to RAR table
3411 * @netdev: network interface device structure
3412 *
3413 * Writes unicast address list to the RAR table.
3414 * Returns: -ENOMEM on failure/insufficient address space
3415 * 0 on no addresses written
3416 * X on writing X addresses to the RAR table
3417 **/
3418static int igb_write_uc_addr_list(struct net_device *netdev)
3419{
3420 struct igb_adapter *adapter = netdev_priv(netdev);
3421 struct e1000_hw *hw = &adapter->hw;
3422 unsigned int vfn = adapter->vfs_allocated_count;
3423 unsigned int rar_entries = hw->mac.rar_entry_count - (vfn + 1);
3424 int count = 0;
3425
3426 /* return ENOMEM indicating insufficient memory for addresses */
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08003427 if (netdev_uc_count(netdev) > rar_entries)
Alexander Duyck68d480c2009-10-05 06:33:08 +00003428 return -ENOMEM;
3429
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08003430 if (!netdev_uc_empty(netdev) && rar_entries) {
Alexander Duyck68d480c2009-10-05 06:33:08 +00003431 struct netdev_hw_addr *ha;
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08003432
3433 netdev_for_each_uc_addr(ha, netdev) {
Alexander Duyck68d480c2009-10-05 06:33:08 +00003434 if (!rar_entries)
3435 break;
3436 igb_rar_set_qsel(adapter, ha->addr,
3437 rar_entries--,
3438 vfn);
3439 count++;
3440 }
3441 }
3442 /* write the addresses in reverse order to avoid write combining */
3443 for (; rar_entries > 0 ; rar_entries--) {
3444 wr32(E1000_RAH(rar_entries), 0);
3445 wr32(E1000_RAL(rar_entries), 0);
3446 }
3447 wrfl();
3448
3449 return count;
3450}
3451
3452/**
Alexander Duyckff41f8d2009-09-03 14:48:56 +00003453 * igb_set_rx_mode - Secondary Unicast, Multicast and Promiscuous mode set
Auke Kok9d5c8242008-01-24 02:22:38 -08003454 * @netdev: network interface device structure
3455 *
Alexander Duyckff41f8d2009-09-03 14:48:56 +00003456 * The set_rx_mode entry point is called whenever the unicast or multicast
3457 * address lists or the network interface flags are updated. This routine is
3458 * responsible for configuring the hardware for proper unicast, multicast,
Auke Kok9d5c8242008-01-24 02:22:38 -08003459 * promiscuous mode, and all-multi behavior.
3460 **/
Alexander Duyckff41f8d2009-09-03 14:48:56 +00003461static void igb_set_rx_mode(struct net_device *netdev)
Auke Kok9d5c8242008-01-24 02:22:38 -08003462{
3463 struct igb_adapter *adapter = netdev_priv(netdev);
3464 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck68d480c2009-10-05 06:33:08 +00003465 unsigned int vfn = adapter->vfs_allocated_count;
3466 u32 rctl, vmolr = 0;
3467 int count;
Auke Kok9d5c8242008-01-24 02:22:38 -08003468
3469 /* Check for Promiscuous and All Multicast modes */
Auke Kok9d5c8242008-01-24 02:22:38 -08003470 rctl = rd32(E1000_RCTL);
3471
Alexander Duyck68d480c2009-10-05 06:33:08 +00003472 /* clear the effected bits */
3473 rctl &= ~(E1000_RCTL_UPE | E1000_RCTL_MPE | E1000_RCTL_VFE);
3474
Patrick McHardy746b9f02008-07-16 20:15:45 -07003475 if (netdev->flags & IFF_PROMISC) {
Auke Kok9d5c8242008-01-24 02:22:38 -08003476 rctl |= (E1000_RCTL_UPE | E1000_RCTL_MPE);
Alexander Duyck68d480c2009-10-05 06:33:08 +00003477 vmolr |= (E1000_VMOLR_ROPE | E1000_VMOLR_MPME);
Patrick McHardy746b9f02008-07-16 20:15:45 -07003478 } else {
Alexander Duyck68d480c2009-10-05 06:33:08 +00003479 if (netdev->flags & IFF_ALLMULTI) {
Patrick McHardy746b9f02008-07-16 20:15:45 -07003480 rctl |= E1000_RCTL_MPE;
Alexander Duyck68d480c2009-10-05 06:33:08 +00003481 vmolr |= E1000_VMOLR_MPME;
3482 } else {
3483 /*
3484 * Write addresses to the MTA, if the attempt fails
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003485 * then we should just turn on promiscuous mode so
Alexander Duyck68d480c2009-10-05 06:33:08 +00003486 * that we can at least receive multicast traffic
3487 */
3488 count = igb_write_mc_addr_list(netdev);
3489 if (count < 0) {
3490 rctl |= E1000_RCTL_MPE;
3491 vmolr |= E1000_VMOLR_MPME;
3492 } else if (count) {
3493 vmolr |= E1000_VMOLR_ROMPE;
3494 }
3495 }
3496 /*
3497 * Write addresses to available RAR registers, if there is not
3498 * sufficient space to store all the addresses then enable
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003499 * unicast promiscuous mode
Alexander Duyck68d480c2009-10-05 06:33:08 +00003500 */
3501 count = igb_write_uc_addr_list(netdev);
3502 if (count < 0) {
Alexander Duyckff41f8d2009-09-03 14:48:56 +00003503 rctl |= E1000_RCTL_UPE;
Alexander Duyck68d480c2009-10-05 06:33:08 +00003504 vmolr |= E1000_VMOLR_ROPE;
3505 }
Patrick McHardy78ed11a2008-07-16 20:16:14 -07003506 rctl |= E1000_RCTL_VFE;
Patrick McHardy746b9f02008-07-16 20:15:45 -07003507 }
Auke Kok9d5c8242008-01-24 02:22:38 -08003508 wr32(E1000_RCTL, rctl);
3509
Alexander Duyck68d480c2009-10-05 06:33:08 +00003510 /*
3511 * In order to support SR-IOV and eventually VMDq it is necessary to set
3512 * the VMOLR to enable the appropriate modes. Without this workaround
3513 * we will have issues with VLAN tag stripping not being done for frames
3514 * that are only arriving because we are the default pool
3515 */
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00003516 if ((hw->mac.type < e1000_82576) || (hw->mac.type > e1000_i350))
Alexander Duyck28fc06f2009-07-23 18:08:54 +00003517 return;
Alexander Duyck28fc06f2009-07-23 18:08:54 +00003518
Alexander Duyck68d480c2009-10-05 06:33:08 +00003519 vmolr |= rd32(E1000_VMOLR(vfn)) &
3520 ~(E1000_VMOLR_ROPE | E1000_VMOLR_MPME | E1000_VMOLR_ROMPE);
3521 wr32(E1000_VMOLR(vfn), vmolr);
Alexander Duyck28fc06f2009-07-23 18:08:54 +00003522 igb_restore_vf_multicasts(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08003523}
3524
Greg Rose13800462010-11-06 02:08:26 +00003525static void igb_check_wvbr(struct igb_adapter *adapter)
3526{
3527 struct e1000_hw *hw = &adapter->hw;
3528 u32 wvbr = 0;
3529
3530 switch (hw->mac.type) {
3531 case e1000_82576:
3532 case e1000_i350:
3533 if (!(wvbr = rd32(E1000_WVBR)))
3534 return;
3535 break;
3536 default:
3537 break;
3538 }
3539
3540 adapter->wvbr |= wvbr;
3541}
3542
3543#define IGB_STAGGERED_QUEUE_OFFSET 8
3544
3545static void igb_spoof_check(struct igb_adapter *adapter)
3546{
3547 int j;
3548
3549 if (!adapter->wvbr)
3550 return;
3551
3552 for(j = 0; j < adapter->vfs_allocated_count; j++) {
3553 if (adapter->wvbr & (1 << j) ||
3554 adapter->wvbr & (1 << (j + IGB_STAGGERED_QUEUE_OFFSET))) {
3555 dev_warn(&adapter->pdev->dev,
3556 "Spoof event(s) detected on VF %d\n", j);
3557 adapter->wvbr &=
3558 ~((1 << j) |
3559 (1 << (j + IGB_STAGGERED_QUEUE_OFFSET)));
3560 }
3561 }
3562}
3563
Auke Kok9d5c8242008-01-24 02:22:38 -08003564/* Need to wait a few seconds after link up to get diagnostic information from
3565 * the phy */
3566static void igb_update_phy_info(unsigned long data)
3567{
3568 struct igb_adapter *adapter = (struct igb_adapter *) data;
Alexander Duyckf5f4cf02008-11-21 21:30:24 -08003569 igb_get_phy_info(&adapter->hw);
Auke Kok9d5c8242008-01-24 02:22:38 -08003570}
3571
3572/**
Alexander Duyck4d6b7252009-02-06 23:16:24 +00003573 * igb_has_link - check shared code for link and determine up/down
3574 * @adapter: pointer to driver private info
3575 **/
Nick Nunley31455352010-02-17 01:01:21 +00003576bool igb_has_link(struct igb_adapter *adapter)
Alexander Duyck4d6b7252009-02-06 23:16:24 +00003577{
3578 struct e1000_hw *hw = &adapter->hw;
3579 bool link_active = false;
3580 s32 ret_val = 0;
3581
3582 /* get_link_status is set on LSC (link status) interrupt or
3583 * rx sequence error interrupt. get_link_status will stay
3584 * false until the e1000_check_for_link establishes link
3585 * for copper adapters ONLY
3586 */
3587 switch (hw->phy.media_type) {
3588 case e1000_media_type_copper:
3589 if (hw->mac.get_link_status) {
3590 ret_val = hw->mac.ops.check_for_link(hw);
3591 link_active = !hw->mac.get_link_status;
3592 } else {
3593 link_active = true;
3594 }
3595 break;
Alexander Duyck4d6b7252009-02-06 23:16:24 +00003596 case e1000_media_type_internal_serdes:
3597 ret_val = hw->mac.ops.check_for_link(hw);
3598 link_active = hw->mac.serdes_has_link;
3599 break;
3600 default:
3601 case e1000_media_type_unknown:
3602 break;
3603 }
3604
3605 return link_active;
3606}
3607
Stefan Assmann563988d2011-04-05 04:27:15 +00003608static bool igb_thermal_sensor_event(struct e1000_hw *hw, u32 event)
3609{
3610 bool ret = false;
3611 u32 ctrl_ext, thstat;
3612
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00003613 /* check for thermal sensor event on i350 copper only */
Stefan Assmann563988d2011-04-05 04:27:15 +00003614 if (hw->mac.type == e1000_i350) {
3615 thstat = rd32(E1000_THSTAT);
3616 ctrl_ext = rd32(E1000_CTRL_EXT);
3617
3618 if ((hw->phy.media_type == e1000_media_type_copper) &&
3619 !(ctrl_ext & E1000_CTRL_EXT_LINK_MODE_SGMII)) {
3620 ret = !!(thstat & event);
3621 }
3622 }
3623
3624 return ret;
3625}
3626
Alexander Duyck4d6b7252009-02-06 23:16:24 +00003627/**
Auke Kok9d5c8242008-01-24 02:22:38 -08003628 * igb_watchdog - Timer Call-back
3629 * @data: pointer to adapter cast into an unsigned long
3630 **/
3631static void igb_watchdog(unsigned long data)
3632{
3633 struct igb_adapter *adapter = (struct igb_adapter *)data;
3634 /* Do the rest outside of interrupt context */
3635 schedule_work(&adapter->watchdog_task);
3636}
3637
3638static void igb_watchdog_task(struct work_struct *work)
3639{
3640 struct igb_adapter *adapter = container_of(work,
Alexander Duyck559e9c42009-10-27 23:52:50 +00003641 struct igb_adapter,
3642 watchdog_task);
Auke Kok9d5c8242008-01-24 02:22:38 -08003643 struct e1000_hw *hw = &adapter->hw;
Auke Kok9d5c8242008-01-24 02:22:38 -08003644 struct net_device *netdev = adapter->netdev;
Stefan Assmann563988d2011-04-05 04:27:15 +00003645 u32 link;
Alexander Duyck7a6ea552008-08-26 04:25:03 -07003646 int i;
Auke Kok9d5c8242008-01-24 02:22:38 -08003647
Alexander Duyck4d6b7252009-02-06 23:16:24 +00003648 link = igb_has_link(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08003649 if (link) {
Yan, Zheng749ab2c2012-01-04 20:23:37 +00003650 /* Cancel scheduled suspend requests. */
3651 pm_runtime_resume(netdev->dev.parent);
3652
Auke Kok9d5c8242008-01-24 02:22:38 -08003653 if (!netif_carrier_ok(netdev)) {
3654 u32 ctrl;
Alexander Duyck330a6d62009-10-27 23:51:35 +00003655 hw->mac.ops.get_speed_and_duplex(hw,
3656 &adapter->link_speed,
3657 &adapter->link_duplex);
Auke Kok9d5c8242008-01-24 02:22:38 -08003658
3659 ctrl = rd32(E1000_CTRL);
Alexander Duyck527d47c2008-11-27 00:21:39 -08003660 /* Links status message must follow this format */
Jeff Kirsher876d2d62011-10-21 20:01:34 +00003661 printk(KERN_INFO "igb: %s NIC Link is Up %d Mbps %s "
3662 "Duplex, Flow Control: %s\n",
Alexander Duyck559e9c42009-10-27 23:52:50 +00003663 netdev->name,
3664 adapter->link_speed,
3665 adapter->link_duplex == FULL_DUPLEX ?
Jeff Kirsher876d2d62011-10-21 20:01:34 +00003666 "Full" : "Half",
3667 (ctrl & E1000_CTRL_TFCE) &&
3668 (ctrl & E1000_CTRL_RFCE) ? "RX/TX" :
3669 (ctrl & E1000_CTRL_RFCE) ? "RX" :
3670 (ctrl & E1000_CTRL_TFCE) ? "TX" : "None");
Auke Kok9d5c8242008-01-24 02:22:38 -08003671
Stefan Assmann563988d2011-04-05 04:27:15 +00003672 /* check for thermal sensor event */
Jeff Kirsher876d2d62011-10-21 20:01:34 +00003673 if (igb_thermal_sensor_event(hw,
3674 E1000_THSTAT_LINK_THROTTLE)) {
3675 netdev_info(netdev, "The network adapter link "
3676 "speed was downshifted because it "
3677 "overheated\n");
Carolyn Wyborny7ef5ed12011-03-12 08:59:47 +00003678 }
Stefan Assmann563988d2011-04-05 04:27:15 +00003679
Emil Tantilovd07f3e32010-03-23 18:34:57 +00003680 /* adjust timeout factor according to speed/duplex */
Auke Kok9d5c8242008-01-24 02:22:38 -08003681 adapter->tx_timeout_factor = 1;
3682 switch (adapter->link_speed) {
3683 case SPEED_10:
Auke Kok9d5c8242008-01-24 02:22:38 -08003684 adapter->tx_timeout_factor = 14;
3685 break;
3686 case SPEED_100:
Auke Kok9d5c8242008-01-24 02:22:38 -08003687 /* maybe add some timeout factor ? */
3688 break;
3689 }
3690
3691 netif_carrier_on(netdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08003692
Alexander Duyck4ae196d2009-02-19 20:40:07 -08003693 igb_ping_all_vfs(adapter);
Lior Levy17dc5662011-02-08 02:28:46 +00003694 igb_check_vf_rate_limit(adapter);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08003695
Alexander Duyck4b1a9872009-02-06 23:19:50 +00003696 /* link state has changed, schedule phy info update */
Auke Kok9d5c8242008-01-24 02:22:38 -08003697 if (!test_bit(__IGB_DOWN, &adapter->state))
3698 mod_timer(&adapter->phy_info_timer,
3699 round_jiffies(jiffies + 2 * HZ));
3700 }
3701 } else {
3702 if (netif_carrier_ok(netdev)) {
3703 adapter->link_speed = 0;
3704 adapter->link_duplex = 0;
Stefan Assmann563988d2011-04-05 04:27:15 +00003705
3706 /* check for thermal sensor event */
Jeff Kirsher876d2d62011-10-21 20:01:34 +00003707 if (igb_thermal_sensor_event(hw,
3708 E1000_THSTAT_PWR_DOWN)) {
3709 netdev_err(netdev, "The network adapter was "
3710 "stopped because it overheated\n");
Carolyn Wyborny7ef5ed12011-03-12 08:59:47 +00003711 }
Stefan Assmann563988d2011-04-05 04:27:15 +00003712
Alexander Duyck527d47c2008-11-27 00:21:39 -08003713 /* Links status message must follow this format */
3714 printk(KERN_INFO "igb: %s NIC Link is Down\n",
3715 netdev->name);
Auke Kok9d5c8242008-01-24 02:22:38 -08003716 netif_carrier_off(netdev);
Alexander Duyck4b1a9872009-02-06 23:19:50 +00003717
Alexander Duyck4ae196d2009-02-19 20:40:07 -08003718 igb_ping_all_vfs(adapter);
3719
Alexander Duyck4b1a9872009-02-06 23:19:50 +00003720 /* link state has changed, schedule phy info update */
Auke Kok9d5c8242008-01-24 02:22:38 -08003721 if (!test_bit(__IGB_DOWN, &adapter->state))
3722 mod_timer(&adapter->phy_info_timer,
3723 round_jiffies(jiffies + 2 * HZ));
Yan, Zheng749ab2c2012-01-04 20:23:37 +00003724
3725 pm_schedule_suspend(netdev->dev.parent,
3726 MSEC_PER_SEC * 5);
Auke Kok9d5c8242008-01-24 02:22:38 -08003727 }
3728 }
3729
Eric Dumazet12dcd862010-10-15 17:27:10 +00003730 spin_lock(&adapter->stats64_lock);
3731 igb_update_stats(adapter, &adapter->stats64);
3732 spin_unlock(&adapter->stats64_lock);
Auke Kok9d5c8242008-01-24 02:22:38 -08003733
Alexander Duyckdbabb062009-11-12 18:38:16 +00003734 for (i = 0; i < adapter->num_tx_queues; i++) {
Alexander Duyck3025a442010-02-17 01:02:39 +00003735 struct igb_ring *tx_ring = adapter->tx_ring[i];
Alexander Duyckdbabb062009-11-12 18:38:16 +00003736 if (!netif_carrier_ok(netdev)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08003737 /* We've lost link, so the controller stops DMA,
3738 * but we've got queued Tx work that's never going
3739 * to get done, so reset controller to flush Tx.
3740 * (Do the reset outside of interrupt context). */
Alexander Duyckdbabb062009-11-12 18:38:16 +00003741 if (igb_desc_unused(tx_ring) + 1 < tx_ring->count) {
3742 adapter->tx_timeout_count++;
3743 schedule_work(&adapter->reset_task);
3744 /* return immediately since reset is imminent */
3745 return;
3746 }
Auke Kok9d5c8242008-01-24 02:22:38 -08003747 }
Auke Kok9d5c8242008-01-24 02:22:38 -08003748
Alexander Duyckdbabb062009-11-12 18:38:16 +00003749 /* Force detection of hung controller every watchdog period */
Alexander Duyck6d095fa2011-08-26 07:46:19 +00003750 set_bit(IGB_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags);
Alexander Duyckdbabb062009-11-12 18:38:16 +00003751 }
Alexander Duyckf7ba2052009-10-27 23:48:51 +00003752
Auke Kok9d5c8242008-01-24 02:22:38 -08003753 /* Cause software interrupt to ensure rx ring is cleaned */
Alexander Duyck7a6ea552008-08-26 04:25:03 -07003754 if (adapter->msix_entries) {
Alexander Duyck047e0032009-10-27 15:49:27 +00003755 u32 eics = 0;
Alexander Duyck0d1ae7f2011-08-26 07:46:34 +00003756 for (i = 0; i < adapter->num_q_vectors; i++)
3757 eics |= adapter->q_vector[i]->eims_value;
Alexander Duyck7a6ea552008-08-26 04:25:03 -07003758 wr32(E1000_EICS, eics);
3759 } else {
3760 wr32(E1000_ICS, E1000_ICS_RXDMT0);
3761 }
Auke Kok9d5c8242008-01-24 02:22:38 -08003762
Greg Rose13800462010-11-06 02:08:26 +00003763 igb_spoof_check(adapter);
3764
Auke Kok9d5c8242008-01-24 02:22:38 -08003765 /* Reset the timer */
3766 if (!test_bit(__IGB_DOWN, &adapter->state))
3767 mod_timer(&adapter->watchdog_timer,
3768 round_jiffies(jiffies + 2 * HZ));
3769}
3770
3771enum latency_range {
3772 lowest_latency = 0,
3773 low_latency = 1,
3774 bulk_latency = 2,
3775 latency_invalid = 255
3776};
3777
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003778/**
3779 * igb_update_ring_itr - update the dynamic ITR value based on packet size
3780 *
3781 * Stores a new ITR value based on strictly on packet size. This
3782 * algorithm is less sophisticated than that used in igb_update_itr,
3783 * due to the difficulty of synchronizing statistics across multiple
Stefan Weileef35c22010-08-06 21:11:15 +02003784 * receive rings. The divisors and thresholds used by this function
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003785 * were determined based on theoretical maximum wire speed and testing
3786 * data, in order to minimize response time while increasing bulk
3787 * throughput.
3788 * This functionality is controlled by the InterruptThrottleRate module
3789 * parameter (see igb_param.c)
3790 * NOTE: This function is called only when operating in a multiqueue
3791 * receive environment.
Alexander Duyck047e0032009-10-27 15:49:27 +00003792 * @q_vector: pointer to q_vector
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003793 **/
Alexander Duyck047e0032009-10-27 15:49:27 +00003794static void igb_update_ring_itr(struct igb_q_vector *q_vector)
Auke Kok9d5c8242008-01-24 02:22:38 -08003795{
Alexander Duyck047e0032009-10-27 15:49:27 +00003796 int new_val = q_vector->itr_val;
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003797 int avg_wire_size = 0;
Alexander Duyck047e0032009-10-27 15:49:27 +00003798 struct igb_adapter *adapter = q_vector->adapter;
Eric Dumazet12dcd862010-10-15 17:27:10 +00003799 unsigned int packets;
Auke Kok9d5c8242008-01-24 02:22:38 -08003800
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003801 /* For non-gigabit speeds, just fix the interrupt rate at 4000
3802 * ints/sec - ITR timer value of 120 ticks.
3803 */
3804 if (adapter->link_speed != SPEED_1000) {
Alexander Duyck0ba82992011-08-26 07:45:47 +00003805 new_val = IGB_4K_ITR;
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003806 goto set_itr_val;
3807 }
Alexander Duyck047e0032009-10-27 15:49:27 +00003808
Alexander Duyck0ba82992011-08-26 07:45:47 +00003809 packets = q_vector->rx.total_packets;
3810 if (packets)
3811 avg_wire_size = q_vector->rx.total_bytes / packets;
Eric Dumazet12dcd862010-10-15 17:27:10 +00003812
Alexander Duyck0ba82992011-08-26 07:45:47 +00003813 packets = q_vector->tx.total_packets;
3814 if (packets)
3815 avg_wire_size = max_t(u32, avg_wire_size,
3816 q_vector->tx.total_bytes / packets);
Alexander Duyck047e0032009-10-27 15:49:27 +00003817
3818 /* if avg_wire_size isn't set no work was done */
3819 if (!avg_wire_size)
3820 goto clear_counts;
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003821
3822 /* Add 24 bytes to size to account for CRC, preamble, and gap */
3823 avg_wire_size += 24;
3824
3825 /* Don't starve jumbo frames */
3826 avg_wire_size = min(avg_wire_size, 3000);
3827
3828 /* Give a little boost to mid-size frames */
3829 if ((avg_wire_size > 300) && (avg_wire_size < 1200))
3830 new_val = avg_wire_size / 3;
3831 else
3832 new_val = avg_wire_size / 2;
3833
Alexander Duyck0ba82992011-08-26 07:45:47 +00003834 /* conservative mode (itr 3) eliminates the lowest_latency setting */
3835 if (new_val < IGB_20K_ITR &&
3836 ((q_vector->rx.ring && adapter->rx_itr_setting == 3) ||
3837 (!q_vector->rx.ring && adapter->tx_itr_setting == 3)))
3838 new_val = IGB_20K_ITR;
Nick Nunleyabe1c362010-02-17 01:03:19 +00003839
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003840set_itr_val:
Alexander Duyck047e0032009-10-27 15:49:27 +00003841 if (new_val != q_vector->itr_val) {
3842 q_vector->itr_val = new_val;
3843 q_vector->set_itr = 1;
Auke Kok9d5c8242008-01-24 02:22:38 -08003844 }
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003845clear_counts:
Alexander Duyck0ba82992011-08-26 07:45:47 +00003846 q_vector->rx.total_bytes = 0;
3847 q_vector->rx.total_packets = 0;
3848 q_vector->tx.total_bytes = 0;
3849 q_vector->tx.total_packets = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08003850}
3851
3852/**
3853 * igb_update_itr - update the dynamic ITR value based on statistics
3854 * Stores a new ITR value based on packets and byte
3855 * counts during the last interrupt. The advantage of per interrupt
3856 * computation is faster updates and more accurate ITR for the current
3857 * traffic pattern. Constants in this function were computed
3858 * based on theoretical maximum wire speed and thresholds were set based
3859 * on testing data as well as attempting to minimize response time
3860 * while increasing bulk throughput.
3861 * this functionality is controlled by the InterruptThrottleRate module
3862 * parameter (see igb_param.c)
3863 * NOTE: These calculations are only valid when operating in a single-
3864 * queue environment.
Alexander Duyck0ba82992011-08-26 07:45:47 +00003865 * @q_vector: pointer to q_vector
3866 * @ring_container: ring info to update the itr for
Auke Kok9d5c8242008-01-24 02:22:38 -08003867 **/
Alexander Duyck0ba82992011-08-26 07:45:47 +00003868static void igb_update_itr(struct igb_q_vector *q_vector,
3869 struct igb_ring_container *ring_container)
Auke Kok9d5c8242008-01-24 02:22:38 -08003870{
Alexander Duyck0ba82992011-08-26 07:45:47 +00003871 unsigned int packets = ring_container->total_packets;
3872 unsigned int bytes = ring_container->total_bytes;
3873 u8 itrval = ring_container->itr;
Auke Kok9d5c8242008-01-24 02:22:38 -08003874
Alexander Duyck0ba82992011-08-26 07:45:47 +00003875 /* no packets, exit with status unchanged */
Auke Kok9d5c8242008-01-24 02:22:38 -08003876 if (packets == 0)
Alexander Duyck0ba82992011-08-26 07:45:47 +00003877 return;
Auke Kok9d5c8242008-01-24 02:22:38 -08003878
Alexander Duyck0ba82992011-08-26 07:45:47 +00003879 switch (itrval) {
Auke Kok9d5c8242008-01-24 02:22:38 -08003880 case lowest_latency:
3881 /* handle TSO and jumbo frames */
3882 if (bytes/packets > 8000)
Alexander Duyck0ba82992011-08-26 07:45:47 +00003883 itrval = bulk_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08003884 else if ((packets < 5) && (bytes > 512))
Alexander Duyck0ba82992011-08-26 07:45:47 +00003885 itrval = low_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08003886 break;
3887 case low_latency: /* 50 usec aka 20000 ints/s */
3888 if (bytes > 10000) {
3889 /* this if handles the TSO accounting */
3890 if (bytes/packets > 8000) {
Alexander Duyck0ba82992011-08-26 07:45:47 +00003891 itrval = bulk_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08003892 } else if ((packets < 10) || ((bytes/packets) > 1200)) {
Alexander Duyck0ba82992011-08-26 07:45:47 +00003893 itrval = bulk_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08003894 } else if ((packets > 35)) {
Alexander Duyck0ba82992011-08-26 07:45:47 +00003895 itrval = lowest_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08003896 }
3897 } else if (bytes/packets > 2000) {
Alexander Duyck0ba82992011-08-26 07:45:47 +00003898 itrval = bulk_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08003899 } else if (packets <= 2 && bytes < 512) {
Alexander Duyck0ba82992011-08-26 07:45:47 +00003900 itrval = lowest_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08003901 }
3902 break;
3903 case bulk_latency: /* 250 usec aka 4000 ints/s */
3904 if (bytes > 25000) {
3905 if (packets > 35)
Alexander Duyck0ba82992011-08-26 07:45:47 +00003906 itrval = low_latency;
Alexander Duyck1e5c3d22009-02-12 18:17:21 +00003907 } else if (bytes < 1500) {
Alexander Duyck0ba82992011-08-26 07:45:47 +00003908 itrval = low_latency;
Auke Kok9d5c8242008-01-24 02:22:38 -08003909 }
3910 break;
3911 }
3912
Alexander Duyck0ba82992011-08-26 07:45:47 +00003913 /* clear work counters since we have the values we need */
3914 ring_container->total_bytes = 0;
3915 ring_container->total_packets = 0;
3916
3917 /* write updated itr to ring container */
3918 ring_container->itr = itrval;
Auke Kok9d5c8242008-01-24 02:22:38 -08003919}
3920
Alexander Duyck0ba82992011-08-26 07:45:47 +00003921static void igb_set_itr(struct igb_q_vector *q_vector)
Auke Kok9d5c8242008-01-24 02:22:38 -08003922{
Alexander Duyck0ba82992011-08-26 07:45:47 +00003923 struct igb_adapter *adapter = q_vector->adapter;
Alexander Duyck047e0032009-10-27 15:49:27 +00003924 u32 new_itr = q_vector->itr_val;
Alexander Duyck0ba82992011-08-26 07:45:47 +00003925 u8 current_itr = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08003926
3927 /* for non-gigabit speeds, just fix the interrupt rate at 4000 */
3928 if (adapter->link_speed != SPEED_1000) {
3929 current_itr = 0;
Alexander Duyck0ba82992011-08-26 07:45:47 +00003930 new_itr = IGB_4K_ITR;
Auke Kok9d5c8242008-01-24 02:22:38 -08003931 goto set_itr_now;
3932 }
3933
Alexander Duyck0ba82992011-08-26 07:45:47 +00003934 igb_update_itr(q_vector, &q_vector->tx);
3935 igb_update_itr(q_vector, &q_vector->rx);
Auke Kok9d5c8242008-01-24 02:22:38 -08003936
Alexander Duyck0ba82992011-08-26 07:45:47 +00003937 current_itr = max(q_vector->rx.itr, q_vector->tx.itr);
Auke Kok9d5c8242008-01-24 02:22:38 -08003938
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003939 /* conservative mode (itr 3) eliminates the lowest_latency setting */
Alexander Duyck0ba82992011-08-26 07:45:47 +00003940 if (current_itr == lowest_latency &&
3941 ((q_vector->rx.ring && adapter->rx_itr_setting == 3) ||
3942 (!q_vector->rx.ring && adapter->tx_itr_setting == 3)))
Alexander Duyck6eb5a7f2008-07-08 15:14:44 -07003943 current_itr = low_latency;
3944
Auke Kok9d5c8242008-01-24 02:22:38 -08003945 switch (current_itr) {
3946 /* counts and packets in update_itr are dependent on these numbers */
3947 case lowest_latency:
Alexander Duyck0ba82992011-08-26 07:45:47 +00003948 new_itr = IGB_70K_ITR; /* 70,000 ints/sec */
Auke Kok9d5c8242008-01-24 02:22:38 -08003949 break;
3950 case low_latency:
Alexander Duyck0ba82992011-08-26 07:45:47 +00003951 new_itr = IGB_20K_ITR; /* 20,000 ints/sec */
Auke Kok9d5c8242008-01-24 02:22:38 -08003952 break;
3953 case bulk_latency:
Alexander Duyck0ba82992011-08-26 07:45:47 +00003954 new_itr = IGB_4K_ITR; /* 4,000 ints/sec */
Auke Kok9d5c8242008-01-24 02:22:38 -08003955 break;
3956 default:
3957 break;
3958 }
3959
3960set_itr_now:
Alexander Duyck047e0032009-10-27 15:49:27 +00003961 if (new_itr != q_vector->itr_val) {
Auke Kok9d5c8242008-01-24 02:22:38 -08003962 /* this attempts to bias the interrupt rate towards Bulk
3963 * by adding intermediate steps when interrupt rate is
3964 * increasing */
Alexander Duyck047e0032009-10-27 15:49:27 +00003965 new_itr = new_itr > q_vector->itr_val ?
3966 max((new_itr * q_vector->itr_val) /
3967 (new_itr + (q_vector->itr_val >> 2)),
Alexander Duyck0ba82992011-08-26 07:45:47 +00003968 new_itr) :
Auke Kok9d5c8242008-01-24 02:22:38 -08003969 new_itr;
3970 /* Don't write the value here; it resets the adapter's
3971 * internal timer, and causes us to delay far longer than
3972 * we should between interrupts. Instead, we write the ITR
3973 * value at the beginning of the next interrupt so the timing
3974 * ends up being correct.
3975 */
Alexander Duyck047e0032009-10-27 15:49:27 +00003976 q_vector->itr_val = new_itr;
3977 q_vector->set_itr = 1;
Auke Kok9d5c8242008-01-24 02:22:38 -08003978 }
Auke Kok9d5c8242008-01-24 02:22:38 -08003979}
3980
Stephen Hemmingerc50b52a2012-01-18 22:13:26 +00003981static void igb_tx_ctxtdesc(struct igb_ring *tx_ring, u32 vlan_macip_lens,
3982 u32 type_tucmd, u32 mss_l4len_idx)
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00003983{
3984 struct e1000_adv_tx_context_desc *context_desc;
3985 u16 i = tx_ring->next_to_use;
3986
3987 context_desc = IGB_TX_CTXTDESC(tx_ring, i);
3988
3989 i++;
3990 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
3991
3992 /* set bits to identify this as an advanced context descriptor */
3993 type_tucmd |= E1000_TXD_CMD_DEXT | E1000_ADVTXD_DTYP_CTXT;
3994
3995 /* For 82575, context index must be unique per ring. */
Alexander Duyck866cff02011-08-26 07:45:36 +00003996 if (test_bit(IGB_RING_FLAG_TX_CTX_IDX, &tx_ring->flags))
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00003997 mss_l4len_idx |= tx_ring->reg_idx << 4;
3998
3999 context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens);
4000 context_desc->seqnum_seed = 0;
4001 context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd);
4002 context_desc->mss_l4len_idx = cpu_to_le32(mss_l4len_idx);
4003}
4004
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004005static int igb_tso(struct igb_ring *tx_ring,
4006 struct igb_tx_buffer *first,
4007 u8 *hdr_len)
Auke Kok9d5c8242008-01-24 02:22:38 -08004008{
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004009 struct sk_buff *skb = first->skb;
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004010 u32 vlan_macip_lens, type_tucmd;
4011 u32 mss_l4len_idx, l4len;
4012
Alexander Duycked6aa102012-11-13 04:03:22 +00004013 if (skb->ip_summed != CHECKSUM_PARTIAL)
4014 return 0;
4015
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004016 if (!skb_is_gso(skb))
4017 return 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08004018
4019 if (skb_header_cloned(skb)) {
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004020 int err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
Auke Kok9d5c8242008-01-24 02:22:38 -08004021 if (err)
4022 return err;
4023 }
4024
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004025 /* ADV DTYP TUCMD MKRLOC/ISCSIHEDLEN */
4026 type_tucmd = E1000_ADVTXD_TUCMD_L4T_TCP;
Auke Kok9d5c8242008-01-24 02:22:38 -08004027
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004028 if (first->protocol == __constant_htons(ETH_P_IP)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08004029 struct iphdr *iph = ip_hdr(skb);
4030 iph->tot_len = 0;
4031 iph->check = 0;
4032 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
4033 iph->daddr, 0,
4034 IPPROTO_TCP,
4035 0);
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004036 type_tucmd |= E1000_ADVTXD_TUCMD_IPV4;
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004037 first->tx_flags |= IGB_TX_FLAGS_TSO |
4038 IGB_TX_FLAGS_CSUM |
4039 IGB_TX_FLAGS_IPV4;
Sridhar Samudrala8e1e8a42010-01-23 02:02:21 -08004040 } else if (skb_is_gso_v6(skb)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08004041 ipv6_hdr(skb)->payload_len = 0;
4042 tcp_hdr(skb)->check = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
4043 &ipv6_hdr(skb)->daddr,
4044 0, IPPROTO_TCP, 0);
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004045 first->tx_flags |= IGB_TX_FLAGS_TSO |
4046 IGB_TX_FLAGS_CSUM;
Auke Kok9d5c8242008-01-24 02:22:38 -08004047 }
4048
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004049 /* compute header lengths */
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004050 l4len = tcp_hdrlen(skb);
4051 *hdr_len = skb_transport_offset(skb) + l4len;
Auke Kok9d5c8242008-01-24 02:22:38 -08004052
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004053 /* update gso size and bytecount with header size */
4054 first->gso_segs = skb_shinfo(skb)->gso_segs;
4055 first->bytecount += (first->gso_segs - 1) * *hdr_len;
4056
Auke Kok9d5c8242008-01-24 02:22:38 -08004057 /* MSS L4LEN IDX */
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004058 mss_l4len_idx = l4len << E1000_ADVTXD_L4LEN_SHIFT;
4059 mss_l4len_idx |= skb_shinfo(skb)->gso_size << E1000_ADVTXD_MSS_SHIFT;
Auke Kok9d5c8242008-01-24 02:22:38 -08004060
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004061 /* VLAN MACLEN IPLEN */
4062 vlan_macip_lens = skb_network_header_len(skb);
4063 vlan_macip_lens |= skb_network_offset(skb) << E1000_ADVTXD_MACLEN_SHIFT;
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004064 vlan_macip_lens |= first->tx_flags & IGB_TX_FLAGS_VLAN_MASK;
Auke Kok9d5c8242008-01-24 02:22:38 -08004065
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004066 igb_tx_ctxtdesc(tx_ring, vlan_macip_lens, type_tucmd, mss_l4len_idx);
Auke Kok9d5c8242008-01-24 02:22:38 -08004067
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004068 return 1;
Auke Kok9d5c8242008-01-24 02:22:38 -08004069}
4070
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004071static void igb_tx_csum(struct igb_ring *tx_ring, struct igb_tx_buffer *first)
Auke Kok9d5c8242008-01-24 02:22:38 -08004072{
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004073 struct sk_buff *skb = first->skb;
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004074 u32 vlan_macip_lens = 0;
4075 u32 mss_l4len_idx = 0;
4076 u32 type_tucmd = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08004077
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004078 if (skb->ip_summed != CHECKSUM_PARTIAL) {
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004079 if (!(first->tx_flags & IGB_TX_FLAGS_VLAN))
4080 return;
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004081 } else {
4082 u8 l4_hdr = 0;
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004083 switch (first->protocol) {
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004084 case __constant_htons(ETH_P_IP):
4085 vlan_macip_lens |= skb_network_header_len(skb);
4086 type_tucmd |= E1000_ADVTXD_TUCMD_IPV4;
4087 l4_hdr = ip_hdr(skb)->protocol;
4088 break;
4089 case __constant_htons(ETH_P_IPV6):
4090 vlan_macip_lens |= skb_network_header_len(skb);
4091 l4_hdr = ipv6_hdr(skb)->nexthdr;
4092 break;
4093 default:
4094 if (unlikely(net_ratelimit())) {
4095 dev_warn(tx_ring->dev,
4096 "partial checksum but proto=%x!\n",
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004097 first->protocol);
Arthur Jonesfa4a7ef2009-03-21 16:55:07 -07004098 }
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004099 break;
Auke Kok9d5c8242008-01-24 02:22:38 -08004100 }
4101
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004102 switch (l4_hdr) {
4103 case IPPROTO_TCP:
4104 type_tucmd |= E1000_ADVTXD_TUCMD_L4T_TCP;
4105 mss_l4len_idx = tcp_hdrlen(skb) <<
4106 E1000_ADVTXD_L4LEN_SHIFT;
4107 break;
4108 case IPPROTO_SCTP:
4109 type_tucmd |= E1000_ADVTXD_TUCMD_L4T_SCTP;
4110 mss_l4len_idx = sizeof(struct sctphdr) <<
4111 E1000_ADVTXD_L4LEN_SHIFT;
4112 break;
4113 case IPPROTO_UDP:
4114 mss_l4len_idx = sizeof(struct udphdr) <<
4115 E1000_ADVTXD_L4LEN_SHIFT;
4116 break;
4117 default:
4118 if (unlikely(net_ratelimit())) {
4119 dev_warn(tx_ring->dev,
4120 "partial checksum but l4 proto=%x!\n",
4121 l4_hdr);
4122 }
4123 break;
4124 }
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004125
4126 /* update TX checksum flag */
4127 first->tx_flags |= IGB_TX_FLAGS_CSUM;
Auke Kok9d5c8242008-01-24 02:22:38 -08004128 }
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004129
4130 vlan_macip_lens |= skb_network_offset(skb) << E1000_ADVTXD_MACLEN_SHIFT;
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004131 vlan_macip_lens |= first->tx_flags & IGB_TX_FLAGS_VLAN_MASK;
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004132
4133 igb_tx_ctxtdesc(tx_ring, vlan_macip_lens, type_tucmd, mss_l4len_idx);
Auke Kok9d5c8242008-01-24 02:22:38 -08004134}
4135
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004136#define IGB_SET_FLAG(_input, _flag, _result) \
4137 ((_flag <= _result) ? \
4138 ((u32)(_input & _flag) * (_result / _flag)) : \
4139 ((u32)(_input & _flag) / (_flag / _result)))
4140
4141static u32 igb_tx_cmd_type(struct sk_buff *skb, u32 tx_flags)
Alexander Duycke032afc2011-08-26 07:44:48 +00004142{
4143 /* set type for advanced descriptor with frame checksum insertion */
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004144 u32 cmd_type = E1000_ADVTXD_DTYP_DATA |
4145 E1000_ADVTXD_DCMD_DEXT |
4146 E1000_ADVTXD_DCMD_IFCS;
Alexander Duycke032afc2011-08-26 07:44:48 +00004147
4148 /* set HW vlan bit if vlan is present */
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004149 cmd_type |= IGB_SET_FLAG(tx_flags, IGB_TX_FLAGS_VLAN,
4150 (E1000_ADVTXD_DCMD_VLE));
Alexander Duycke032afc2011-08-26 07:44:48 +00004151
4152 /* set segmentation bits for TSO */
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004153 cmd_type |= IGB_SET_FLAG(tx_flags, IGB_TX_FLAGS_TSO,
4154 (E1000_ADVTXD_DCMD_TSE));
4155
4156 /* set timestamp bit if present */
4157 cmd_type |= IGB_SET_FLAG(tx_flags, IGB_TX_FLAGS_TSTAMP,
4158 (E1000_ADVTXD_MAC_TSTAMP));
4159
4160 /* insert frame checksum */
4161 cmd_type ^= IGB_SET_FLAG(skb->no_fcs, 1, E1000_ADVTXD_DCMD_IFCS);
Alexander Duycke032afc2011-08-26 07:44:48 +00004162
4163 return cmd_type;
4164}
4165
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004166static void igb_tx_olinfo_status(struct igb_ring *tx_ring,
4167 union e1000_adv_tx_desc *tx_desc,
4168 u32 tx_flags, unsigned int paylen)
Alexander Duycke032afc2011-08-26 07:44:48 +00004169{
4170 u32 olinfo_status = paylen << E1000_ADVTXD_PAYLEN_SHIFT;
4171
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004172 /* 82575 requires a unique index per ring */
4173 if (test_bit(IGB_RING_FLAG_TX_CTX_IDX, &tx_ring->flags))
Alexander Duycke032afc2011-08-26 07:44:48 +00004174 olinfo_status |= tx_ring->reg_idx << 4;
4175
4176 /* insert L4 checksum */
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004177 olinfo_status |= IGB_SET_FLAG(tx_flags,
4178 IGB_TX_FLAGS_CSUM,
4179 (E1000_TXD_POPTS_TXSM << 8));
Alexander Duycke032afc2011-08-26 07:44:48 +00004180
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004181 /* insert IPv4 checksum */
4182 olinfo_status |= IGB_SET_FLAG(tx_flags,
4183 IGB_TX_FLAGS_IPV4,
4184 (E1000_TXD_POPTS_IXSM << 8));
Alexander Duycke032afc2011-08-26 07:44:48 +00004185
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004186 tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status);
Alexander Duycke032afc2011-08-26 07:44:48 +00004187}
4188
Alexander Duyckebe42d12011-08-26 07:45:09 +00004189/*
4190 * The largest size we can write to the descriptor is 65535. In order to
4191 * maintain a power of two alignment we have to limit ourselves to 32K.
4192 */
4193#define IGB_MAX_TXD_PWR 15
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004194#define IGB_MAX_DATA_PER_TXD (1<<IGB_MAX_TXD_PWR)
Auke Kok9d5c8242008-01-24 02:22:38 -08004195
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004196static void igb_tx_map(struct igb_ring *tx_ring,
4197 struct igb_tx_buffer *first,
Alexander Duyckebe42d12011-08-26 07:45:09 +00004198 const u8 hdr_len)
Auke Kok9d5c8242008-01-24 02:22:38 -08004199{
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004200 struct sk_buff *skb = first->skb;
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00004201 struct igb_tx_buffer *tx_buffer;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004202 union e1000_adv_tx_desc *tx_desc;
Alexander Duyck80d07592012-11-13 04:03:24 +00004203 struct skb_frag_struct *frag;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004204 dma_addr_t dma;
Alexander Duyck80d07592012-11-13 04:03:24 +00004205 unsigned int data_len, size;
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004206 u32 tx_flags = first->tx_flags;
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004207 u32 cmd_type = igb_tx_cmd_type(skb, tx_flags);
Alexander Duyckebe42d12011-08-26 07:45:09 +00004208 u16 i = tx_ring->next_to_use;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004209
4210 tx_desc = IGB_TX_DESC(tx_ring, i);
4211
Alexander Duyck80d07592012-11-13 04:03:24 +00004212 igb_tx_olinfo_status(tx_ring, tx_desc, tx_flags, skb->len - hdr_len);
4213
4214 size = skb_headlen(skb);
4215 data_len = skb->data_len;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004216
4217 dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
Auke Kok9d5c8242008-01-24 02:22:38 -08004218
Alexander Duyck80d07592012-11-13 04:03:24 +00004219 tx_buffer = first;
Alexander Duyck2bbfebe2011-08-26 07:44:59 +00004220
Alexander Duyck80d07592012-11-13 04:03:24 +00004221 for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
4222 if (dma_mapping_error(tx_ring->dev, dma))
4223 goto dma_error;
4224
4225 /* record length, and DMA address */
4226 dma_unmap_len_set(tx_buffer, len, size);
4227 dma_unmap_addr_set(tx_buffer, dma, dma);
4228
4229 tx_desc->read.buffer_addr = cpu_to_le64(dma);
4230
Alexander Duyckebe42d12011-08-26 07:45:09 +00004231 while (unlikely(size > IGB_MAX_DATA_PER_TXD)) {
4232 tx_desc->read.cmd_type_len =
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004233 cpu_to_le32(cmd_type ^ IGB_MAX_DATA_PER_TXD);
Auke Kok9d5c8242008-01-24 02:22:38 -08004234
Alexander Duyckebe42d12011-08-26 07:45:09 +00004235 i++;
4236 tx_desc++;
4237 if (i == tx_ring->count) {
4238 tx_desc = IGB_TX_DESC(tx_ring, 0);
4239 i = 0;
4240 }
Alexander Duyck80d07592012-11-13 04:03:24 +00004241 tx_desc->read.olinfo_status = 0;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004242
4243 dma += IGB_MAX_DATA_PER_TXD;
4244 size -= IGB_MAX_DATA_PER_TXD;
4245
Alexander Duyckebe42d12011-08-26 07:45:09 +00004246 tx_desc->read.buffer_addr = cpu_to_le64(dma);
4247 }
4248
4249 if (likely(!data_len))
4250 break;
4251
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004252 tx_desc->read.cmd_type_len = cpu_to_le32(cmd_type ^ size);
Alexander Duyckebe42d12011-08-26 07:45:09 +00004253
Alexander Duyck65689fe2009-03-20 00:17:43 +00004254 i++;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004255 tx_desc++;
4256 if (i == tx_ring->count) {
4257 tx_desc = IGB_TX_DESC(tx_ring, 0);
Alexander Duyck65689fe2009-03-20 00:17:43 +00004258 i = 0;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004259 }
Alexander Duyck80d07592012-11-13 04:03:24 +00004260 tx_desc->read.olinfo_status = 0;
Alexander Duyck65689fe2009-03-20 00:17:43 +00004261
Eric Dumazet9e903e02011-10-18 21:00:24 +00004262 size = skb_frag_size(frag);
Alexander Duyckebe42d12011-08-26 07:45:09 +00004263 data_len -= size;
4264
4265 dma = skb_frag_dma_map(tx_ring->dev, frag, 0,
Alexander Duyck80d07592012-11-13 04:03:24 +00004266 size, DMA_TO_DEVICE);
Alexander Duyck6366ad32009-12-02 16:47:18 +00004267
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00004268 tx_buffer = &tx_ring->tx_buffer_info[i];
Auke Kok9d5c8242008-01-24 02:22:38 -08004269 }
4270
Alexander Duyckebe42d12011-08-26 07:45:09 +00004271 /* write last descriptor with RS and EOP bits */
Alexander Duyck1d9daf42012-11-13 04:03:23 +00004272 cmd_type |= size | IGB_TXD_DCMD;
4273 tx_desc->read.cmd_type_len = cpu_to_le32(cmd_type);
Alexander Duyck8542db02011-08-26 07:44:43 +00004274
Alexander Duyck80d07592012-11-13 04:03:24 +00004275 netdev_tx_sent_queue(txring_txq(tx_ring), first->bytecount);
4276
Alexander Duyck8542db02011-08-26 07:44:43 +00004277 /* set the timestamp */
4278 first->time_stamp = jiffies;
4279
Alexander Duyckebe42d12011-08-26 07:45:09 +00004280 /*
4281 * Force memory writes to complete before letting h/w know there
4282 * are new descriptors to fetch. (Only applicable for weak-ordered
4283 * memory model archs, such as IA-64).
4284 *
4285 * We also need this memory barrier to make certain all of the
4286 * status bits have been updated before next_to_watch is written.
4287 */
Auke Kok9d5c8242008-01-24 02:22:38 -08004288 wmb();
4289
Alexander Duyckebe42d12011-08-26 07:45:09 +00004290 /* set next_to_watch value indicating a packet is present */
4291 first->next_to_watch = tx_desc;
4292
4293 i++;
4294 if (i == tx_ring->count)
4295 i = 0;
4296
Auke Kok9d5c8242008-01-24 02:22:38 -08004297 tx_ring->next_to_use = i;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004298
Alexander Duyckfce99e32009-10-27 15:51:27 +00004299 writel(i, tx_ring->tail);
Alexander Duyckebe42d12011-08-26 07:45:09 +00004300
Auke Kok9d5c8242008-01-24 02:22:38 -08004301 /* we need this if more than one processor can write to our tail
4302 * at a time, it syncronizes IO on IA64/Altix systems */
4303 mmiowb();
Alexander Duyckebe42d12011-08-26 07:45:09 +00004304
4305 return;
4306
4307dma_error:
4308 dev_err(tx_ring->dev, "TX DMA map failed\n");
4309
4310 /* clear dma mappings for failed tx_buffer_info map */
4311 for (;;) {
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00004312 tx_buffer = &tx_ring->tx_buffer_info[i];
4313 igb_unmap_and_free_tx_resource(tx_ring, tx_buffer);
4314 if (tx_buffer == first)
Alexander Duyckebe42d12011-08-26 07:45:09 +00004315 break;
4316 if (i == 0)
4317 i = tx_ring->count;
4318 i--;
4319 }
4320
4321 tx_ring->next_to_use = i;
Auke Kok9d5c8242008-01-24 02:22:38 -08004322}
4323
Alexander Duyck6ad4edf2011-08-26 07:45:26 +00004324static int __igb_maybe_stop_tx(struct igb_ring *tx_ring, const u16 size)
Auke Kok9d5c8242008-01-24 02:22:38 -08004325{
Alexander Duycke694e962009-10-27 15:53:06 +00004326 struct net_device *netdev = tx_ring->netdev;
4327
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07004328 netif_stop_subqueue(netdev, tx_ring->queue_index);
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07004329
Auke Kok9d5c8242008-01-24 02:22:38 -08004330 /* Herbert's original patch had:
4331 * smp_mb__after_netif_stop_queue();
4332 * but since that doesn't exist yet, just open code it. */
4333 smp_mb();
4334
4335 /* We need to check again in a case another CPU has just
4336 * made room available. */
Alexander Duyckc493ea42009-03-20 00:16:50 +00004337 if (igb_desc_unused(tx_ring) < size)
Auke Kok9d5c8242008-01-24 02:22:38 -08004338 return -EBUSY;
4339
4340 /* A reprieve! */
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07004341 netif_wake_subqueue(netdev, tx_ring->queue_index);
Eric Dumazet12dcd862010-10-15 17:27:10 +00004342
4343 u64_stats_update_begin(&tx_ring->tx_syncp2);
4344 tx_ring->tx_stats.restart_queue2++;
4345 u64_stats_update_end(&tx_ring->tx_syncp2);
4346
Auke Kok9d5c8242008-01-24 02:22:38 -08004347 return 0;
4348}
4349
Alexander Duyck6ad4edf2011-08-26 07:45:26 +00004350static inline int igb_maybe_stop_tx(struct igb_ring *tx_ring, const u16 size)
Auke Kok9d5c8242008-01-24 02:22:38 -08004351{
Alexander Duyckc493ea42009-03-20 00:16:50 +00004352 if (igb_desc_unused(tx_ring) >= size)
Auke Kok9d5c8242008-01-24 02:22:38 -08004353 return 0;
Alexander Duycke694e962009-10-27 15:53:06 +00004354 return __igb_maybe_stop_tx(tx_ring, size);
Auke Kok9d5c8242008-01-24 02:22:38 -08004355}
4356
Alexander Duyckcd392f52011-08-26 07:43:59 +00004357netdev_tx_t igb_xmit_frame_ring(struct sk_buff *skb,
4358 struct igb_ring *tx_ring)
Auke Kok9d5c8242008-01-24 02:22:38 -08004359{
Matthew Vick1f6e8172012-08-18 07:26:33 +00004360 struct igb_adapter *adapter = netdev_priv(tx_ring->netdev);
Alexander Duyck8542db02011-08-26 07:44:43 +00004361 struct igb_tx_buffer *first;
Alexander Duyckebe42d12011-08-26 07:45:09 +00004362 int tso;
Nick Nunley91d4ee32010-02-17 01:04:56 +00004363 u32 tx_flags = 0;
Alexander Duyck31f6adb2011-08-26 07:44:53 +00004364 __be16 protocol = vlan_get_protocol(skb);
Nick Nunley91d4ee32010-02-17 01:04:56 +00004365 u8 hdr_len = 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08004366
Auke Kok9d5c8242008-01-24 02:22:38 -08004367 /* need: 1 descriptor per page,
4368 * + 2 desc gap to keep tail from touching head,
4369 * + 1 desc for skb->data,
4370 * + 1 desc for context descriptor,
4371 * otherwise try next time */
Alexander Duycke694e962009-10-27 15:53:06 +00004372 if (igb_maybe_stop_tx(tx_ring, skb_shinfo(skb)->nr_frags + 4)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08004373 /* this is a hard error */
Auke Kok9d5c8242008-01-24 02:22:38 -08004374 return NETDEV_TX_BUSY;
4375 }
Patrick Ohly33af6bc2009-02-12 05:03:43 +00004376
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004377 /* record the location of the first descriptor for this packet */
4378 first = &tx_ring->tx_buffer_info[tx_ring->next_to_use];
4379 first->skb = skb;
4380 first->bytecount = skb->len;
4381 first->gso_segs = 1;
4382
Matthew Vick1f6e8172012-08-18 07:26:33 +00004383 if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
4384 !(adapter->ptp_tx_skb))) {
Oliver Hartkopp2244d072010-08-17 08:59:14 +00004385 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
Patrick Ohly33af6bc2009-02-12 05:03:43 +00004386 tx_flags |= IGB_TX_FLAGS_TSTAMP;
Matthew Vick1f6e8172012-08-18 07:26:33 +00004387
4388 adapter->ptp_tx_skb = skb_get(skb);
4389 if (adapter->hw.mac.type == e1000_82576)
4390 schedule_work(&adapter->ptp_tx_work);
Patrick Ohly33af6bc2009-02-12 05:03:43 +00004391 }
Auke Kok9d5c8242008-01-24 02:22:38 -08004392
Jesse Grosseab6d182010-10-20 13:56:03 +00004393 if (vlan_tx_tag_present(skb)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08004394 tx_flags |= IGB_TX_FLAGS_VLAN;
4395 tx_flags |= (vlan_tx_tag_get(skb) << IGB_TX_FLAGS_VLAN_SHIFT);
4396 }
4397
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004398 /* record initial flags and protocol */
4399 first->tx_flags = tx_flags;
4400 first->protocol = protocol;
Alexander Duyckcdfd01fc2009-10-27 23:50:57 +00004401
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004402 tso = igb_tso(tx_ring, first, &hdr_len);
4403 if (tso < 0)
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004404 goto out_drop;
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004405 else if (!tso)
4406 igb_tx_csum(tx_ring, first);
Auke Kok9d5c8242008-01-24 02:22:38 -08004407
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004408 igb_tx_map(tx_ring, first, hdr_len);
Alexander Duyck85ad76b2009-10-27 15:52:46 +00004409
4410 /* Make sure there is space in the ring for the next send. */
Alexander Duycke694e962009-10-27 15:53:06 +00004411 igb_maybe_stop_tx(tx_ring, MAX_SKB_FRAGS + 4);
Alexander Duyck85ad76b2009-10-27 15:52:46 +00004412
Auke Kok9d5c8242008-01-24 02:22:38 -08004413 return NETDEV_TX_OK;
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004414
4415out_drop:
Alexander Duyck7af40ad92011-08-26 07:45:15 +00004416 igb_unmap_and_free_tx_resource(tx_ring, first);
4417
Alexander Duyck7d13a7d2011-08-26 07:44:32 +00004418 return NETDEV_TX_OK;
Auke Kok9d5c8242008-01-24 02:22:38 -08004419}
4420
Alexander Duyck1cc3bd82011-08-26 07:44:10 +00004421static inline struct igb_ring *igb_tx_queue_mapping(struct igb_adapter *adapter,
4422 struct sk_buff *skb)
4423{
4424 unsigned int r_idx = skb->queue_mapping;
4425
4426 if (r_idx >= adapter->num_tx_queues)
4427 r_idx = r_idx % adapter->num_tx_queues;
4428
4429 return adapter->tx_ring[r_idx];
4430}
4431
Alexander Duyckcd392f52011-08-26 07:43:59 +00004432static netdev_tx_t igb_xmit_frame(struct sk_buff *skb,
4433 struct net_device *netdev)
Auke Kok9d5c8242008-01-24 02:22:38 -08004434{
4435 struct igb_adapter *adapter = netdev_priv(netdev);
Alexander Duyckb1a436c2009-10-27 15:54:43 +00004436
4437 if (test_bit(__IGB_DOWN, &adapter->state)) {
4438 dev_kfree_skb_any(skb);
4439 return NETDEV_TX_OK;
4440 }
4441
4442 if (skb->len <= 0) {
4443 dev_kfree_skb_any(skb);
4444 return NETDEV_TX_OK;
4445 }
4446
Alexander Duyck1cc3bd82011-08-26 07:44:10 +00004447 /*
4448 * The minimum packet size with TCTL.PSP set is 17 so pad the skb
4449 * in order to meet this minimum size requirement.
4450 */
Tushar Daveea5ceea2012-09-14 03:43:43 +00004451 if (unlikely(skb->len < 17)) {
4452 if (skb_pad(skb, 17 - skb->len))
Alexander Duyck1cc3bd82011-08-26 07:44:10 +00004453 return NETDEV_TX_OK;
4454 skb->len = 17;
Tushar Daveea5ceea2012-09-14 03:43:43 +00004455 skb_set_tail_pointer(skb, 17);
Alexander Duyck1cc3bd82011-08-26 07:44:10 +00004456 }
Auke Kok9d5c8242008-01-24 02:22:38 -08004457
Alexander Duyck1cc3bd82011-08-26 07:44:10 +00004458 return igb_xmit_frame_ring(skb, igb_tx_queue_mapping(adapter, skb));
Auke Kok9d5c8242008-01-24 02:22:38 -08004459}
4460
4461/**
4462 * igb_tx_timeout - Respond to a Tx Hang
4463 * @netdev: network interface device structure
4464 **/
4465static void igb_tx_timeout(struct net_device *netdev)
4466{
4467 struct igb_adapter *adapter = netdev_priv(netdev);
4468 struct e1000_hw *hw = &adapter->hw;
4469
4470 /* Do the reset outside of interrupt context */
4471 adapter->tx_timeout_count++;
Alexander Duyckf7ba2052009-10-27 23:48:51 +00004472
Alexander Duyck06218a82011-08-26 07:46:55 +00004473 if (hw->mac.type >= e1000_82580)
Alexander Duyck55cac242009-11-19 12:42:21 +00004474 hw->dev_spec._82575.global_device_reset = true;
4475
Auke Kok9d5c8242008-01-24 02:22:38 -08004476 schedule_work(&adapter->reset_task);
Alexander Duyck265de402009-02-06 23:22:52 +00004477 wr32(E1000_EICS,
4478 (adapter->eims_enable_mask & ~adapter->eims_other));
Auke Kok9d5c8242008-01-24 02:22:38 -08004479}
4480
4481static void igb_reset_task(struct work_struct *work)
4482{
4483 struct igb_adapter *adapter;
4484 adapter = container_of(work, struct igb_adapter, reset_task);
4485
Taku Izumic97ec422010-04-27 14:39:30 +00004486 igb_dump(adapter);
4487 netdev_err(adapter->netdev, "Reset adapter\n");
Auke Kok9d5c8242008-01-24 02:22:38 -08004488 igb_reinit_locked(adapter);
4489}
4490
4491/**
Eric Dumazet12dcd862010-10-15 17:27:10 +00004492 * igb_get_stats64 - Get System Network Statistics
Auke Kok9d5c8242008-01-24 02:22:38 -08004493 * @netdev: network interface device structure
Eric Dumazet12dcd862010-10-15 17:27:10 +00004494 * @stats: rtnl_link_stats64 pointer
Auke Kok9d5c8242008-01-24 02:22:38 -08004495 *
Auke Kok9d5c8242008-01-24 02:22:38 -08004496 **/
Eric Dumazet12dcd862010-10-15 17:27:10 +00004497static struct rtnl_link_stats64 *igb_get_stats64(struct net_device *netdev,
4498 struct rtnl_link_stats64 *stats)
Auke Kok9d5c8242008-01-24 02:22:38 -08004499{
Eric Dumazet12dcd862010-10-15 17:27:10 +00004500 struct igb_adapter *adapter = netdev_priv(netdev);
4501
4502 spin_lock(&adapter->stats64_lock);
4503 igb_update_stats(adapter, &adapter->stats64);
4504 memcpy(stats, &adapter->stats64, sizeof(*stats));
4505 spin_unlock(&adapter->stats64_lock);
4506
4507 return stats;
Auke Kok9d5c8242008-01-24 02:22:38 -08004508}
4509
4510/**
4511 * igb_change_mtu - Change the Maximum Transfer Unit
4512 * @netdev: network interface device structure
4513 * @new_mtu: new value for maximum frame size
4514 *
4515 * Returns 0 on success, negative on failure
4516 **/
4517static int igb_change_mtu(struct net_device *netdev, int new_mtu)
4518{
4519 struct igb_adapter *adapter = netdev_priv(netdev);
Alexander Duyck090b1792009-10-27 23:51:55 +00004520 struct pci_dev *pdev = adapter->pdev;
Alexander Duyck153285f2011-08-26 07:43:32 +00004521 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
Auke Kok9d5c8242008-01-24 02:22:38 -08004522
Alexander Duyckc809d222009-10-27 23:52:13 +00004523 if ((new_mtu < 68) || (max_frame > MAX_JUMBO_FRAME_SIZE)) {
Alexander Duyck090b1792009-10-27 23:51:55 +00004524 dev_err(&pdev->dev, "Invalid MTU setting\n");
Auke Kok9d5c8242008-01-24 02:22:38 -08004525 return -EINVAL;
4526 }
4527
Alexander Duyck153285f2011-08-26 07:43:32 +00004528#define MAX_STD_JUMBO_FRAME_SIZE 9238
Auke Kok9d5c8242008-01-24 02:22:38 -08004529 if (max_frame > MAX_STD_JUMBO_FRAME_SIZE) {
Alexander Duyck090b1792009-10-27 23:51:55 +00004530 dev_err(&pdev->dev, "MTU > 9216 not supported.\n");
Auke Kok9d5c8242008-01-24 02:22:38 -08004531 return -EINVAL;
4532 }
4533
4534 while (test_and_set_bit(__IGB_RESETTING, &adapter->state))
4535 msleep(1);
Alexander Duyck73cd78f2009-02-12 18:16:59 +00004536
Auke Kok9d5c8242008-01-24 02:22:38 -08004537 /* igb_down has a dependency on max_frame_size */
4538 adapter->max_frame_size = max_frame;
Alexander Duyck559e9c42009-10-27 23:52:50 +00004539
Alexander Duyck4c844852009-10-27 15:52:07 +00004540 if (netif_running(netdev))
4541 igb_down(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08004542
Alexander Duyck090b1792009-10-27 23:51:55 +00004543 dev_info(&pdev->dev, "changing MTU from %d to %d\n",
Auke Kok9d5c8242008-01-24 02:22:38 -08004544 netdev->mtu, new_mtu);
4545 netdev->mtu = new_mtu;
4546
4547 if (netif_running(netdev))
4548 igb_up(adapter);
4549 else
4550 igb_reset(adapter);
4551
4552 clear_bit(__IGB_RESETTING, &adapter->state);
4553
4554 return 0;
4555}
4556
4557/**
4558 * igb_update_stats - Update the board statistics counters
4559 * @adapter: board private structure
4560 **/
4561
Eric Dumazet12dcd862010-10-15 17:27:10 +00004562void igb_update_stats(struct igb_adapter *adapter,
4563 struct rtnl_link_stats64 *net_stats)
Auke Kok9d5c8242008-01-24 02:22:38 -08004564{
4565 struct e1000_hw *hw = &adapter->hw;
4566 struct pci_dev *pdev = adapter->pdev;
Mitch Williamsfa3d9a62010-03-23 18:34:38 +00004567 u32 reg, mpc;
Auke Kok9d5c8242008-01-24 02:22:38 -08004568 u16 phy_tmp;
Alexander Duyck3f9c0162009-10-27 23:48:12 +00004569 int i;
4570 u64 bytes, packets;
Eric Dumazet12dcd862010-10-15 17:27:10 +00004571 unsigned int start;
4572 u64 _bytes, _packets;
Auke Kok9d5c8242008-01-24 02:22:38 -08004573
4574#define PHY_IDLE_ERROR_COUNT_MASK 0x00FF
4575
4576 /*
4577 * Prevent stats update while adapter is being reset, or if the pci
4578 * connection is down.
4579 */
4580 if (adapter->link_speed == 0)
4581 return;
4582 if (pci_channel_offline(pdev))
4583 return;
4584
Alexander Duyck3f9c0162009-10-27 23:48:12 +00004585 bytes = 0;
4586 packets = 0;
4587 for (i = 0; i < adapter->num_rx_queues; i++) {
Alexander Duyckae1c07a2012-08-08 05:23:22 +00004588 u32 rqdpc = rd32(E1000_RQDPC(i));
Alexander Duyck3025a442010-02-17 01:02:39 +00004589 struct igb_ring *ring = adapter->rx_ring[i];
Eric Dumazet12dcd862010-10-15 17:27:10 +00004590
Alexander Duyckae1c07a2012-08-08 05:23:22 +00004591 if (rqdpc) {
4592 ring->rx_stats.drops += rqdpc;
4593 net_stats->rx_fifo_errors += rqdpc;
4594 }
Eric Dumazet12dcd862010-10-15 17:27:10 +00004595
4596 do {
4597 start = u64_stats_fetch_begin_bh(&ring->rx_syncp);
4598 _bytes = ring->rx_stats.bytes;
4599 _packets = ring->rx_stats.packets;
4600 } while (u64_stats_fetch_retry_bh(&ring->rx_syncp, start));
4601 bytes += _bytes;
4602 packets += _packets;
Alexander Duyck3f9c0162009-10-27 23:48:12 +00004603 }
4604
Alexander Duyck128e45e2009-11-12 18:37:38 +00004605 net_stats->rx_bytes = bytes;
4606 net_stats->rx_packets = packets;
Alexander Duyck3f9c0162009-10-27 23:48:12 +00004607
4608 bytes = 0;
4609 packets = 0;
4610 for (i = 0; i < adapter->num_tx_queues; i++) {
Alexander Duyck3025a442010-02-17 01:02:39 +00004611 struct igb_ring *ring = adapter->tx_ring[i];
Eric Dumazet12dcd862010-10-15 17:27:10 +00004612 do {
4613 start = u64_stats_fetch_begin_bh(&ring->tx_syncp);
4614 _bytes = ring->tx_stats.bytes;
4615 _packets = ring->tx_stats.packets;
4616 } while (u64_stats_fetch_retry_bh(&ring->tx_syncp, start));
4617 bytes += _bytes;
4618 packets += _packets;
Alexander Duyck3f9c0162009-10-27 23:48:12 +00004619 }
Alexander Duyck128e45e2009-11-12 18:37:38 +00004620 net_stats->tx_bytes = bytes;
4621 net_stats->tx_packets = packets;
Alexander Duyck3f9c0162009-10-27 23:48:12 +00004622
4623 /* read stats registers */
Auke Kok9d5c8242008-01-24 02:22:38 -08004624 adapter->stats.crcerrs += rd32(E1000_CRCERRS);
4625 adapter->stats.gprc += rd32(E1000_GPRC);
4626 adapter->stats.gorc += rd32(E1000_GORCL);
4627 rd32(E1000_GORCH); /* clear GORCL */
4628 adapter->stats.bprc += rd32(E1000_BPRC);
4629 adapter->stats.mprc += rd32(E1000_MPRC);
4630 adapter->stats.roc += rd32(E1000_ROC);
4631
4632 adapter->stats.prc64 += rd32(E1000_PRC64);
4633 adapter->stats.prc127 += rd32(E1000_PRC127);
4634 adapter->stats.prc255 += rd32(E1000_PRC255);
4635 adapter->stats.prc511 += rd32(E1000_PRC511);
4636 adapter->stats.prc1023 += rd32(E1000_PRC1023);
4637 adapter->stats.prc1522 += rd32(E1000_PRC1522);
4638 adapter->stats.symerrs += rd32(E1000_SYMERRS);
4639 adapter->stats.sec += rd32(E1000_SEC);
4640
Mitch Williamsfa3d9a62010-03-23 18:34:38 +00004641 mpc = rd32(E1000_MPC);
4642 adapter->stats.mpc += mpc;
4643 net_stats->rx_fifo_errors += mpc;
Auke Kok9d5c8242008-01-24 02:22:38 -08004644 adapter->stats.scc += rd32(E1000_SCC);
4645 adapter->stats.ecol += rd32(E1000_ECOL);
4646 adapter->stats.mcc += rd32(E1000_MCC);
4647 adapter->stats.latecol += rd32(E1000_LATECOL);
4648 adapter->stats.dc += rd32(E1000_DC);
4649 adapter->stats.rlec += rd32(E1000_RLEC);
4650 adapter->stats.xonrxc += rd32(E1000_XONRXC);
4651 adapter->stats.xontxc += rd32(E1000_XONTXC);
4652 adapter->stats.xoffrxc += rd32(E1000_XOFFRXC);
4653 adapter->stats.xofftxc += rd32(E1000_XOFFTXC);
4654 adapter->stats.fcruc += rd32(E1000_FCRUC);
4655 adapter->stats.gptc += rd32(E1000_GPTC);
4656 adapter->stats.gotc += rd32(E1000_GOTCL);
4657 rd32(E1000_GOTCH); /* clear GOTCL */
Mitch Williamsfa3d9a62010-03-23 18:34:38 +00004658 adapter->stats.rnbc += rd32(E1000_RNBC);
Auke Kok9d5c8242008-01-24 02:22:38 -08004659 adapter->stats.ruc += rd32(E1000_RUC);
4660 adapter->stats.rfc += rd32(E1000_RFC);
4661 adapter->stats.rjc += rd32(E1000_RJC);
4662 adapter->stats.tor += rd32(E1000_TORH);
4663 adapter->stats.tot += rd32(E1000_TOTH);
4664 adapter->stats.tpr += rd32(E1000_TPR);
4665
4666 adapter->stats.ptc64 += rd32(E1000_PTC64);
4667 adapter->stats.ptc127 += rd32(E1000_PTC127);
4668 adapter->stats.ptc255 += rd32(E1000_PTC255);
4669 adapter->stats.ptc511 += rd32(E1000_PTC511);
4670 adapter->stats.ptc1023 += rd32(E1000_PTC1023);
4671 adapter->stats.ptc1522 += rd32(E1000_PTC1522);
4672
4673 adapter->stats.mptc += rd32(E1000_MPTC);
4674 adapter->stats.bptc += rd32(E1000_BPTC);
4675
Nick Nunley2d0b0f62010-02-17 01:02:59 +00004676 adapter->stats.tpt += rd32(E1000_TPT);
4677 adapter->stats.colc += rd32(E1000_COLC);
Auke Kok9d5c8242008-01-24 02:22:38 -08004678
4679 adapter->stats.algnerrc += rd32(E1000_ALGNERRC);
Nick Nunley43915c7c2010-02-17 01:03:58 +00004680 /* read internal phy specific stats */
4681 reg = rd32(E1000_CTRL_EXT);
4682 if (!(reg & E1000_CTRL_EXT_LINK_MODE_MASK)) {
4683 adapter->stats.rxerrc += rd32(E1000_RXERRC);
Carolyn Wyborny3dbdf962012-09-12 04:36:24 +00004684
4685 /* this stat has invalid values on i210/i211 */
4686 if ((hw->mac.type != e1000_i210) &&
4687 (hw->mac.type != e1000_i211))
4688 adapter->stats.tncrs += rd32(E1000_TNCRS);
Nick Nunley43915c7c2010-02-17 01:03:58 +00004689 }
4690
Auke Kok9d5c8242008-01-24 02:22:38 -08004691 adapter->stats.tsctc += rd32(E1000_TSCTC);
4692 adapter->stats.tsctfc += rd32(E1000_TSCTFC);
4693
4694 adapter->stats.iac += rd32(E1000_IAC);
4695 adapter->stats.icrxoc += rd32(E1000_ICRXOC);
4696 adapter->stats.icrxptc += rd32(E1000_ICRXPTC);
4697 adapter->stats.icrxatc += rd32(E1000_ICRXATC);
4698 adapter->stats.ictxptc += rd32(E1000_ICTXPTC);
4699 adapter->stats.ictxatc += rd32(E1000_ICTXATC);
4700 adapter->stats.ictxqec += rd32(E1000_ICTXQEC);
4701 adapter->stats.ictxqmtc += rd32(E1000_ICTXQMTC);
4702 adapter->stats.icrxdmtc += rd32(E1000_ICRXDMTC);
4703
4704 /* Fill out the OS statistics structure */
Alexander Duyck128e45e2009-11-12 18:37:38 +00004705 net_stats->multicast = adapter->stats.mprc;
4706 net_stats->collisions = adapter->stats.colc;
Auke Kok9d5c8242008-01-24 02:22:38 -08004707
4708 /* Rx Errors */
4709
4710 /* RLEC on some newer hardware can be incorrect so build
Jesper Dangaard Brouer8c0ab702009-05-26 13:50:31 +00004711 * our own version based on RUC and ROC */
Alexander Duyck128e45e2009-11-12 18:37:38 +00004712 net_stats->rx_errors = adapter->stats.rxerrc +
Auke Kok9d5c8242008-01-24 02:22:38 -08004713 adapter->stats.crcerrs + adapter->stats.algnerrc +
4714 adapter->stats.ruc + adapter->stats.roc +
4715 adapter->stats.cexterr;
Alexander Duyck128e45e2009-11-12 18:37:38 +00004716 net_stats->rx_length_errors = adapter->stats.ruc +
4717 adapter->stats.roc;
4718 net_stats->rx_crc_errors = adapter->stats.crcerrs;
4719 net_stats->rx_frame_errors = adapter->stats.algnerrc;
4720 net_stats->rx_missed_errors = adapter->stats.mpc;
Auke Kok9d5c8242008-01-24 02:22:38 -08004721
4722 /* Tx Errors */
Alexander Duyck128e45e2009-11-12 18:37:38 +00004723 net_stats->tx_errors = adapter->stats.ecol +
4724 adapter->stats.latecol;
4725 net_stats->tx_aborted_errors = adapter->stats.ecol;
4726 net_stats->tx_window_errors = adapter->stats.latecol;
4727 net_stats->tx_carrier_errors = adapter->stats.tncrs;
Auke Kok9d5c8242008-01-24 02:22:38 -08004728
4729 /* Tx Dropped needs to be maintained elsewhere */
4730
4731 /* Phy Stats */
4732 if (hw->phy.media_type == e1000_media_type_copper) {
4733 if ((adapter->link_speed == SPEED_1000) &&
Alexander Duyck73cd78f2009-02-12 18:16:59 +00004734 (!igb_read_phy_reg(hw, PHY_1000T_STATUS, &phy_tmp))) {
Auke Kok9d5c8242008-01-24 02:22:38 -08004735 phy_tmp &= PHY_IDLE_ERROR_COUNT_MASK;
4736 adapter->phy_stats.idle_errors += phy_tmp;
4737 }
4738 }
4739
4740 /* Management Stats */
4741 adapter->stats.mgptc += rd32(E1000_MGTPTC);
4742 adapter->stats.mgprc += rd32(E1000_MGTPRC);
4743 adapter->stats.mgpdc += rd32(E1000_MGTPDC);
Carolyn Wyborny0a915b92011-02-26 07:42:37 +00004744
4745 /* OS2BMC Stats */
4746 reg = rd32(E1000_MANC);
4747 if (reg & E1000_MANC_EN_BMC2OS) {
4748 adapter->stats.o2bgptc += rd32(E1000_O2BGPTC);
4749 adapter->stats.o2bspc += rd32(E1000_O2BSPC);
4750 adapter->stats.b2ospc += rd32(E1000_B2OSPC);
4751 adapter->stats.b2ogprc += rd32(E1000_B2OGPRC);
4752 }
Auke Kok9d5c8242008-01-24 02:22:38 -08004753}
4754
Auke Kok9d5c8242008-01-24 02:22:38 -08004755static irqreturn_t igb_msix_other(int irq, void *data)
4756{
Alexander Duyck047e0032009-10-27 15:49:27 +00004757 struct igb_adapter *adapter = data;
Auke Kok9d5c8242008-01-24 02:22:38 -08004758 struct e1000_hw *hw = &adapter->hw;
PJ Waskiewicz844290e2008-06-27 11:00:39 -07004759 u32 icr = rd32(E1000_ICR);
PJ Waskiewicz844290e2008-06-27 11:00:39 -07004760 /* reading ICR causes bit 31 of EICR to be cleared */
Alexander Duyckdda0e082009-02-06 23:19:08 +00004761
Alexander Duyck7f081d42010-01-07 17:41:00 +00004762 if (icr & E1000_ICR_DRSTA)
4763 schedule_work(&adapter->reset_task);
4764
Alexander Duyck047e0032009-10-27 15:49:27 +00004765 if (icr & E1000_ICR_DOUTSYNC) {
Alexander Duyckdda0e082009-02-06 23:19:08 +00004766 /* HW is reporting DMA is out of sync */
4767 adapter->stats.doosync++;
Greg Rose13800462010-11-06 02:08:26 +00004768 /* The DMA Out of Sync is also indication of a spoof event
4769 * in IOV mode. Check the Wrong VM Behavior register to
4770 * see if it is really a spoof event. */
4771 igb_check_wvbr(adapter);
Alexander Duyckdda0e082009-02-06 23:19:08 +00004772 }
Alexander Duyckeebbbdb2009-02-06 23:19:29 +00004773
Alexander Duyck4ae196d2009-02-19 20:40:07 -08004774 /* Check for a mailbox event */
4775 if (icr & E1000_ICR_VMMB)
4776 igb_msg_task(adapter);
4777
4778 if (icr & E1000_ICR_LSC) {
4779 hw->mac.get_link_status = 1;
4780 /* guard against interrupt when we're going down */
4781 if (!test_bit(__IGB_DOWN, &adapter->state))
4782 mod_timer(&adapter->watchdog_timer, jiffies + 1);
4783 }
4784
Matthew Vick1f6e8172012-08-18 07:26:33 +00004785 if (icr & E1000_ICR_TS) {
4786 u32 tsicr = rd32(E1000_TSICR);
4787
4788 if (tsicr & E1000_TSICR_TXTS) {
4789 /* acknowledge the interrupt */
4790 wr32(E1000_TSICR, E1000_TSICR_TXTS);
4791 /* retrieve hardware timestamp */
4792 schedule_work(&adapter->ptp_tx_work);
4793 }
4794 }
Matthew Vick1f6e8172012-08-18 07:26:33 +00004795
PJ Waskiewicz844290e2008-06-27 11:00:39 -07004796 wr32(E1000_EIMS, adapter->eims_other);
Auke Kok9d5c8242008-01-24 02:22:38 -08004797
4798 return IRQ_HANDLED;
4799}
4800
Alexander Duyck047e0032009-10-27 15:49:27 +00004801static void igb_write_itr(struct igb_q_vector *q_vector)
Auke Kok9d5c8242008-01-24 02:22:38 -08004802{
Alexander Duyck26b39272010-02-17 01:00:41 +00004803 struct igb_adapter *adapter = q_vector->adapter;
Alexander Duyck047e0032009-10-27 15:49:27 +00004804 u32 itr_val = q_vector->itr_val & 0x7FFC;
Auke Kok9d5c8242008-01-24 02:22:38 -08004805
Alexander Duyck047e0032009-10-27 15:49:27 +00004806 if (!q_vector->set_itr)
4807 return;
Alexander Duyck73cd78f2009-02-12 18:16:59 +00004808
Alexander Duyck047e0032009-10-27 15:49:27 +00004809 if (!itr_val)
4810 itr_val = 0x4;
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07004811
Alexander Duyck26b39272010-02-17 01:00:41 +00004812 if (adapter->hw.mac.type == e1000_82575)
4813 itr_val |= itr_val << 16;
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07004814 else
Alexander Duyck0ba82992011-08-26 07:45:47 +00004815 itr_val |= E1000_EITR_CNT_IGNR;
Alexander Duyck047e0032009-10-27 15:49:27 +00004816
4817 writel(itr_val, q_vector->itr_register);
4818 q_vector->set_itr = 0;
4819}
4820
4821static irqreturn_t igb_msix_ring(int irq, void *data)
4822{
4823 struct igb_q_vector *q_vector = data;
4824
4825 /* Write the ITR value calculated from the previous interrupt. */
4826 igb_write_itr(q_vector);
4827
4828 napi_schedule(&q_vector->napi);
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07004829
Auke Kok9d5c8242008-01-24 02:22:38 -08004830 return IRQ_HANDLED;
4831}
4832
Jeff Kirsher421e02f2008-10-17 11:08:31 -07004833#ifdef CONFIG_IGB_DCA
Alexander Duyck6a050042012-09-25 00:31:27 +00004834static void igb_update_tx_dca(struct igb_adapter *adapter,
4835 struct igb_ring *tx_ring,
4836 int cpu)
4837{
4838 struct e1000_hw *hw = &adapter->hw;
4839 u32 txctrl = dca3_get_tag(tx_ring->dev, cpu);
4840
4841 if (hw->mac.type != e1000_82575)
4842 txctrl <<= E1000_DCA_TXCTRL_CPUID_SHIFT;
4843
4844 /*
4845 * We can enable relaxed ordering for reads, but not writes when
4846 * DCA is enabled. This is due to a known issue in some chipsets
4847 * which will cause the DCA tag to be cleared.
4848 */
4849 txctrl |= E1000_DCA_TXCTRL_DESC_RRO_EN |
4850 E1000_DCA_TXCTRL_DATA_RRO_EN |
4851 E1000_DCA_TXCTRL_DESC_DCA_EN;
4852
4853 wr32(E1000_DCA_TXCTRL(tx_ring->reg_idx), txctrl);
4854}
4855
4856static void igb_update_rx_dca(struct igb_adapter *adapter,
4857 struct igb_ring *rx_ring,
4858 int cpu)
4859{
4860 struct e1000_hw *hw = &adapter->hw;
4861 u32 rxctrl = dca3_get_tag(&adapter->pdev->dev, cpu);
4862
4863 if (hw->mac.type != e1000_82575)
4864 rxctrl <<= E1000_DCA_RXCTRL_CPUID_SHIFT;
4865
4866 /*
4867 * We can enable relaxed ordering for reads, but not writes when
4868 * DCA is enabled. This is due to a known issue in some chipsets
4869 * which will cause the DCA tag to be cleared.
4870 */
4871 rxctrl |= E1000_DCA_RXCTRL_DESC_RRO_EN |
4872 E1000_DCA_RXCTRL_DESC_DCA_EN;
4873
4874 wr32(E1000_DCA_RXCTRL(rx_ring->reg_idx), rxctrl);
4875}
4876
Alexander Duyck047e0032009-10-27 15:49:27 +00004877static void igb_update_dca(struct igb_q_vector *q_vector)
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004878{
Alexander Duyck047e0032009-10-27 15:49:27 +00004879 struct igb_adapter *adapter = q_vector->adapter;
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004880 int cpu = get_cpu();
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004881
Alexander Duyck047e0032009-10-27 15:49:27 +00004882 if (q_vector->cpu == cpu)
4883 goto out_no_update;
4884
Alexander Duyck6a050042012-09-25 00:31:27 +00004885 if (q_vector->tx.ring)
4886 igb_update_tx_dca(adapter, q_vector->tx.ring, cpu);
4887
4888 if (q_vector->rx.ring)
4889 igb_update_rx_dca(adapter, q_vector->rx.ring, cpu);
4890
Alexander Duyck047e0032009-10-27 15:49:27 +00004891 q_vector->cpu = cpu;
4892out_no_update:
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004893 put_cpu();
4894}
4895
4896static void igb_setup_dca(struct igb_adapter *adapter)
4897{
Alexander Duyck7e0e99e2009-05-21 13:06:56 +00004898 struct e1000_hw *hw = &adapter->hw;
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004899 int i;
4900
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07004901 if (!(adapter->flags & IGB_FLAG_DCA_ENABLED))
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004902 return;
4903
Alexander Duyck7e0e99e2009-05-21 13:06:56 +00004904 /* Always use CB2 mode, difference is masked in the CB driver. */
4905 wr32(E1000_DCA_CTRL, E1000_DCA_CTRL_DCA_MODE_CB2);
4906
Alexander Duyck047e0032009-10-27 15:49:27 +00004907 for (i = 0; i < adapter->num_q_vectors; i++) {
Alexander Duyck26b39272010-02-17 01:00:41 +00004908 adapter->q_vector[i]->cpu = -1;
4909 igb_update_dca(adapter->q_vector[i]);
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004910 }
4911}
4912
4913static int __igb_notify_dca(struct device *dev, void *data)
4914{
4915 struct net_device *netdev = dev_get_drvdata(dev);
4916 struct igb_adapter *adapter = netdev_priv(netdev);
Alexander Duyck090b1792009-10-27 23:51:55 +00004917 struct pci_dev *pdev = adapter->pdev;
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004918 struct e1000_hw *hw = &adapter->hw;
4919 unsigned long event = *(unsigned long *)data;
4920
4921 switch (event) {
4922 case DCA_PROVIDER_ADD:
4923 /* if already enabled, don't do it again */
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07004924 if (adapter->flags & IGB_FLAG_DCA_ENABLED)
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004925 break;
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004926 if (dca_add_requester(dev) == 0) {
Alexander Duyckbbd98fe2009-01-31 00:52:30 -08004927 adapter->flags |= IGB_FLAG_DCA_ENABLED;
Alexander Duyck090b1792009-10-27 23:51:55 +00004928 dev_info(&pdev->dev, "DCA enabled\n");
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004929 igb_setup_dca(adapter);
4930 break;
4931 }
4932 /* Fall Through since DCA is disabled. */
4933 case DCA_PROVIDER_REMOVE:
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07004934 if (adapter->flags & IGB_FLAG_DCA_ENABLED) {
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004935 /* without this a class_device is left
Alexander Duyck047e0032009-10-27 15:49:27 +00004936 * hanging around in the sysfs model */
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004937 dca_remove_requester(dev);
Alexander Duyck090b1792009-10-27 23:51:55 +00004938 dev_info(&pdev->dev, "DCA disabled\n");
Alexander Duyck7dfc16f2008-07-08 15:10:46 -07004939 adapter->flags &= ~IGB_FLAG_DCA_ENABLED;
Alexander Duyckcbd347a2009-02-15 23:59:44 -08004940 wr32(E1000_DCA_CTRL, E1000_DCA_CTRL_DCA_MODE_DISABLE);
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004941 }
4942 break;
4943 }
Alexander Duyckbbd98fe2009-01-31 00:52:30 -08004944
Jeb Cramerfe4506b2008-07-08 15:07:55 -07004945 return 0;
4946}
4947
4948static int igb_notify_dca(struct notifier_block *nb, unsigned long event,
4949 void *p)
4950{
4951 int ret_val;
4952
4953 ret_val = driver_for_each_device(&igb_driver.driver, NULL, &event,
4954 __igb_notify_dca);
4955
4956 return ret_val ? NOTIFY_BAD : NOTIFY_DONE;
4957}
Jeff Kirsher421e02f2008-10-17 11:08:31 -07004958#endif /* CONFIG_IGB_DCA */
Auke Kok9d5c8242008-01-24 02:22:38 -08004959
Greg Rose0224d662011-10-14 02:57:14 +00004960#ifdef CONFIG_PCI_IOV
4961static int igb_vf_configure(struct igb_adapter *adapter, int vf)
4962{
4963 unsigned char mac_addr[ETH_ALEN];
Greg Rose0224d662011-10-14 02:57:14 +00004964
Joe Perches7efd26d2012-07-12 19:33:06 +00004965 eth_random_addr(mac_addr);
Greg Rose0224d662011-10-14 02:57:14 +00004966 igb_set_vf_mac(adapter, vf, mac_addr);
4967
Stefan Assmannf5571472012-08-18 04:06:11 +00004968 return 0;
Greg Rose0224d662011-10-14 02:57:14 +00004969}
4970
Stefan Assmannf5571472012-08-18 04:06:11 +00004971static bool igb_vfs_are_assigned(struct igb_adapter *adapter)
Greg Rose0224d662011-10-14 02:57:14 +00004972{
Greg Rose0224d662011-10-14 02:57:14 +00004973 struct pci_dev *pdev = adapter->pdev;
Stefan Assmannf5571472012-08-18 04:06:11 +00004974 struct pci_dev *vfdev;
4975 int dev_id;
Greg Rose0224d662011-10-14 02:57:14 +00004976
4977 switch (adapter->hw.mac.type) {
4978 case e1000_82576:
Stefan Assmannf5571472012-08-18 04:06:11 +00004979 dev_id = IGB_82576_VF_DEV_ID;
Greg Rose0224d662011-10-14 02:57:14 +00004980 break;
4981 case e1000_i350:
Stefan Assmannf5571472012-08-18 04:06:11 +00004982 dev_id = IGB_I350_VF_DEV_ID;
Greg Rose0224d662011-10-14 02:57:14 +00004983 break;
4984 default:
Stefan Assmannf5571472012-08-18 04:06:11 +00004985 return false;
Greg Rose0224d662011-10-14 02:57:14 +00004986 }
4987
Stefan Assmannf5571472012-08-18 04:06:11 +00004988 /* loop through all the VFs to see if we own any that are assigned */
4989 vfdev = pci_get_device(PCI_VENDOR_ID_INTEL, dev_id, NULL);
4990 while (vfdev) {
4991 /* if we don't own it we don't care */
4992 if (vfdev->is_virtfn && vfdev->physfn == pdev) {
4993 /* if it is assigned we cannot release it */
4994 if (vfdev->dev_flags & PCI_DEV_FLAGS_ASSIGNED)
Greg Rose0224d662011-10-14 02:57:14 +00004995 return true;
4996 }
Stefan Assmannf5571472012-08-18 04:06:11 +00004997
4998 vfdev = pci_get_device(PCI_VENDOR_ID_INTEL, dev_id, vfdev);
Greg Rose0224d662011-10-14 02:57:14 +00004999 }
Stefan Assmannf5571472012-08-18 04:06:11 +00005000
Greg Rose0224d662011-10-14 02:57:14 +00005001 return false;
5002}
5003
5004#endif
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005005static void igb_ping_all_vfs(struct igb_adapter *adapter)
5006{
5007 struct e1000_hw *hw = &adapter->hw;
5008 u32 ping;
5009 int i;
5010
5011 for (i = 0 ; i < adapter->vfs_allocated_count; i++) {
5012 ping = E1000_PF_CONTROL_MSG;
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005013 if (adapter->vf_data[i].flags & IGB_VF_FLAG_CTS)
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005014 ping |= E1000_VT_MSGTYPE_CTS;
5015 igb_write_mbx(hw, &ping, 1, i);
5016 }
5017}
5018
Alexander Duyck7d5753f2009-10-27 23:47:16 +00005019static int igb_set_vf_promisc(struct igb_adapter *adapter, u32 *msgbuf, u32 vf)
5020{
5021 struct e1000_hw *hw = &adapter->hw;
5022 u32 vmolr = rd32(E1000_VMOLR(vf));
5023 struct vf_data_storage *vf_data = &adapter->vf_data[vf];
5024
Alexander Duyckd85b90042010-09-22 17:56:20 +00005025 vf_data->flags &= ~(IGB_VF_FLAG_UNI_PROMISC |
Alexander Duyck7d5753f2009-10-27 23:47:16 +00005026 IGB_VF_FLAG_MULTI_PROMISC);
5027 vmolr &= ~(E1000_VMOLR_ROPE | E1000_VMOLR_ROMPE | E1000_VMOLR_MPME);
5028
5029 if (*msgbuf & E1000_VF_SET_PROMISC_MULTICAST) {
5030 vmolr |= E1000_VMOLR_MPME;
Alexander Duyckd85b90042010-09-22 17:56:20 +00005031 vf_data->flags |= IGB_VF_FLAG_MULTI_PROMISC;
Alexander Duyck7d5753f2009-10-27 23:47:16 +00005032 *msgbuf &= ~E1000_VF_SET_PROMISC_MULTICAST;
5033 } else {
5034 /*
5035 * if we have hashes and we are clearing a multicast promisc
5036 * flag we need to write the hashes to the MTA as this step
5037 * was previously skipped
5038 */
5039 if (vf_data->num_vf_mc_hashes > 30) {
5040 vmolr |= E1000_VMOLR_MPME;
5041 } else if (vf_data->num_vf_mc_hashes) {
5042 int j;
5043 vmolr |= E1000_VMOLR_ROMPE;
5044 for (j = 0; j < vf_data->num_vf_mc_hashes; j++)
5045 igb_mta_set(hw, vf_data->vf_mc_hashes[j]);
5046 }
5047 }
5048
5049 wr32(E1000_VMOLR(vf), vmolr);
5050
5051 /* there are flags left unprocessed, likely not supported */
5052 if (*msgbuf & E1000_VT_MSGINFO_MASK)
5053 return -EINVAL;
5054
5055 return 0;
5056
5057}
5058
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005059static int igb_set_vf_multicasts(struct igb_adapter *adapter,
5060 u32 *msgbuf, u32 vf)
5061{
5062 int n = (msgbuf[0] & E1000_VT_MSGINFO_MASK) >> E1000_VT_MSGINFO_SHIFT;
5063 u16 *hash_list = (u16 *)&msgbuf[1];
5064 struct vf_data_storage *vf_data = &adapter->vf_data[vf];
5065 int i;
5066
Alexander Duyck7d5753f2009-10-27 23:47:16 +00005067 /* salt away the number of multicast addresses assigned
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005068 * to this VF for later use to restore when the PF multi cast
5069 * list changes
5070 */
5071 vf_data->num_vf_mc_hashes = n;
5072
Alexander Duyck7d5753f2009-10-27 23:47:16 +00005073 /* only up to 30 hash values supported */
5074 if (n > 30)
5075 n = 30;
5076
5077 /* store the hashes for later use */
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005078 for (i = 0; i < n; i++)
Joe Perchesa419aef2009-08-18 11:18:35 -07005079 vf_data->vf_mc_hashes[i] = hash_list[i];
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005080
5081 /* Flush and reset the mta with the new values */
Alexander Duyckff41f8d2009-09-03 14:48:56 +00005082 igb_set_rx_mode(adapter->netdev);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005083
5084 return 0;
5085}
5086
5087static void igb_restore_vf_multicasts(struct igb_adapter *adapter)
5088{
5089 struct e1000_hw *hw = &adapter->hw;
5090 struct vf_data_storage *vf_data;
5091 int i, j;
5092
5093 for (i = 0; i < adapter->vfs_allocated_count; i++) {
Alexander Duyck7d5753f2009-10-27 23:47:16 +00005094 u32 vmolr = rd32(E1000_VMOLR(i));
5095 vmolr &= ~(E1000_VMOLR_ROMPE | E1000_VMOLR_MPME);
5096
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005097 vf_data = &adapter->vf_data[i];
Alexander Duyck7d5753f2009-10-27 23:47:16 +00005098
5099 if ((vf_data->num_vf_mc_hashes > 30) ||
5100 (vf_data->flags & IGB_VF_FLAG_MULTI_PROMISC)) {
5101 vmolr |= E1000_VMOLR_MPME;
5102 } else if (vf_data->num_vf_mc_hashes) {
5103 vmolr |= E1000_VMOLR_ROMPE;
5104 for (j = 0; j < vf_data->num_vf_mc_hashes; j++)
5105 igb_mta_set(hw, vf_data->vf_mc_hashes[j]);
5106 }
5107 wr32(E1000_VMOLR(i), vmolr);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005108 }
5109}
5110
5111static void igb_clear_vf_vfta(struct igb_adapter *adapter, u32 vf)
5112{
5113 struct e1000_hw *hw = &adapter->hw;
5114 u32 pool_mask, reg, vid;
5115 int i;
5116
5117 pool_mask = 1 << (E1000_VLVF_POOLSEL_SHIFT + vf);
5118
5119 /* Find the vlan filter for this id */
5120 for (i = 0; i < E1000_VLVF_ARRAY_SIZE; i++) {
5121 reg = rd32(E1000_VLVF(i));
5122
5123 /* remove the vf from the pool */
5124 reg &= ~pool_mask;
5125
5126 /* if pool is empty then remove entry from vfta */
5127 if (!(reg & E1000_VLVF_POOLSEL_MASK) &&
5128 (reg & E1000_VLVF_VLANID_ENABLE)) {
5129 reg = 0;
5130 vid = reg & E1000_VLVF_VLANID_MASK;
5131 igb_vfta_set(hw, vid, false);
5132 }
5133
5134 wr32(E1000_VLVF(i), reg);
5135 }
Alexander Duyckae641bd2009-09-03 14:49:33 +00005136
5137 adapter->vf_data[vf].vlans_enabled = 0;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005138}
5139
5140static s32 igb_vlvf_set(struct igb_adapter *adapter, u32 vid, bool add, u32 vf)
5141{
5142 struct e1000_hw *hw = &adapter->hw;
5143 u32 reg, i;
5144
Alexander Duyck51466232009-10-27 23:47:35 +00005145 /* The vlvf table only exists on 82576 hardware and newer */
5146 if (hw->mac.type < e1000_82576)
5147 return -1;
5148
5149 /* we only need to do this if VMDq is enabled */
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005150 if (!adapter->vfs_allocated_count)
5151 return -1;
5152
5153 /* Find the vlan filter for this id */
5154 for (i = 0; i < E1000_VLVF_ARRAY_SIZE; i++) {
5155 reg = rd32(E1000_VLVF(i));
5156 if ((reg & E1000_VLVF_VLANID_ENABLE) &&
5157 vid == (reg & E1000_VLVF_VLANID_MASK))
5158 break;
5159 }
5160
5161 if (add) {
5162 if (i == E1000_VLVF_ARRAY_SIZE) {
5163 /* Did not find a matching VLAN ID entry that was
5164 * enabled. Search for a free filter entry, i.e.
5165 * one without the enable bit set
5166 */
5167 for (i = 0; i < E1000_VLVF_ARRAY_SIZE; i++) {
5168 reg = rd32(E1000_VLVF(i));
5169 if (!(reg & E1000_VLVF_VLANID_ENABLE))
5170 break;
5171 }
5172 }
5173 if (i < E1000_VLVF_ARRAY_SIZE) {
5174 /* Found an enabled/available entry */
5175 reg |= 1 << (E1000_VLVF_POOLSEL_SHIFT + vf);
5176
5177 /* if !enabled we need to set this up in vfta */
5178 if (!(reg & E1000_VLVF_VLANID_ENABLE)) {
Alexander Duyck51466232009-10-27 23:47:35 +00005179 /* add VID to filter table */
5180 igb_vfta_set(hw, vid, true);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005181 reg |= E1000_VLVF_VLANID_ENABLE;
5182 }
Alexander Duyckcad6d052009-03-13 20:41:37 +00005183 reg &= ~E1000_VLVF_VLANID_MASK;
5184 reg |= vid;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005185 wr32(E1000_VLVF(i), reg);
Alexander Duyckae641bd2009-09-03 14:49:33 +00005186
5187 /* do not modify RLPML for PF devices */
5188 if (vf >= adapter->vfs_allocated_count)
5189 return 0;
5190
5191 if (!adapter->vf_data[vf].vlans_enabled) {
5192 u32 size;
5193 reg = rd32(E1000_VMOLR(vf));
5194 size = reg & E1000_VMOLR_RLPML_MASK;
5195 size += 4;
5196 reg &= ~E1000_VMOLR_RLPML_MASK;
5197 reg |= size;
5198 wr32(E1000_VMOLR(vf), reg);
5199 }
Alexander Duyckae641bd2009-09-03 14:49:33 +00005200
Alexander Duyck51466232009-10-27 23:47:35 +00005201 adapter->vf_data[vf].vlans_enabled++;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005202 }
5203 } else {
5204 if (i < E1000_VLVF_ARRAY_SIZE) {
5205 /* remove vf from the pool */
5206 reg &= ~(1 << (E1000_VLVF_POOLSEL_SHIFT + vf));
5207 /* if pool is empty then remove entry from vfta */
5208 if (!(reg & E1000_VLVF_POOLSEL_MASK)) {
5209 reg = 0;
5210 igb_vfta_set(hw, vid, false);
5211 }
5212 wr32(E1000_VLVF(i), reg);
Alexander Duyckae641bd2009-09-03 14:49:33 +00005213
5214 /* do not modify RLPML for PF devices */
5215 if (vf >= adapter->vfs_allocated_count)
5216 return 0;
5217
5218 adapter->vf_data[vf].vlans_enabled--;
5219 if (!adapter->vf_data[vf].vlans_enabled) {
5220 u32 size;
5221 reg = rd32(E1000_VMOLR(vf));
5222 size = reg & E1000_VMOLR_RLPML_MASK;
5223 size -= 4;
5224 reg &= ~E1000_VMOLR_RLPML_MASK;
5225 reg |= size;
5226 wr32(E1000_VMOLR(vf), reg);
5227 }
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005228 }
5229 }
Williams, Mitch A8151d292010-02-10 01:44:24 +00005230 return 0;
5231}
5232
5233static void igb_set_vmvir(struct igb_adapter *adapter, u32 vid, u32 vf)
5234{
5235 struct e1000_hw *hw = &adapter->hw;
5236
5237 if (vid)
5238 wr32(E1000_VMVIR(vf), (vid | E1000_VMVIR_VLANA_DEFAULT));
5239 else
5240 wr32(E1000_VMVIR(vf), 0);
5241}
5242
5243static int igb_ndo_set_vf_vlan(struct net_device *netdev,
5244 int vf, u16 vlan, u8 qos)
5245{
5246 int err = 0;
5247 struct igb_adapter *adapter = netdev_priv(netdev);
5248
5249 if ((vf >= adapter->vfs_allocated_count) || (vlan > 4095) || (qos > 7))
5250 return -EINVAL;
5251 if (vlan || qos) {
5252 err = igb_vlvf_set(adapter, vlan, !!vlan, vf);
5253 if (err)
5254 goto out;
5255 igb_set_vmvir(adapter, vlan | (qos << VLAN_PRIO_SHIFT), vf);
5256 igb_set_vmolr(adapter, vf, !vlan);
5257 adapter->vf_data[vf].pf_vlan = vlan;
5258 adapter->vf_data[vf].pf_qos = qos;
5259 dev_info(&adapter->pdev->dev,
5260 "Setting VLAN %d, QOS 0x%x on VF %d\n", vlan, qos, vf);
5261 if (test_bit(__IGB_DOWN, &adapter->state)) {
5262 dev_warn(&adapter->pdev->dev,
5263 "The VF VLAN has been set,"
5264 " but the PF device is not up.\n");
5265 dev_warn(&adapter->pdev->dev,
5266 "Bring the PF device up before"
5267 " attempting to use the VF device.\n");
5268 }
5269 } else {
5270 igb_vlvf_set(adapter, adapter->vf_data[vf].pf_vlan,
5271 false, vf);
5272 igb_set_vmvir(adapter, vlan, vf);
5273 igb_set_vmolr(adapter, vf, true);
5274 adapter->vf_data[vf].pf_vlan = 0;
5275 adapter->vf_data[vf].pf_qos = 0;
5276 }
5277out:
5278 return err;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005279}
5280
5281static int igb_set_vf_vlan(struct igb_adapter *adapter, u32 *msgbuf, u32 vf)
5282{
5283 int add = (msgbuf[0] & E1000_VT_MSGINFO_MASK) >> E1000_VT_MSGINFO_SHIFT;
5284 int vid = (msgbuf[1] & E1000_VLVF_VLANID_MASK);
5285
5286 return igb_vlvf_set(adapter, vid, add, vf);
5287}
5288
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005289static inline void igb_vf_reset(struct igb_adapter *adapter, u32 vf)
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005290{
Greg Rose8fa7e0f2010-11-06 05:43:21 +00005291 /* clear flags - except flag that indicates PF has set the MAC */
5292 adapter->vf_data[vf].flags &= IGB_VF_FLAG_PF_SET_MAC;
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005293 adapter->vf_data[vf].last_nack = jiffies;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005294
5295 /* reset offloads to defaults */
Williams, Mitch A8151d292010-02-10 01:44:24 +00005296 igb_set_vmolr(adapter, vf, true);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005297
5298 /* reset vlans for device */
5299 igb_clear_vf_vfta(adapter, vf);
Williams, Mitch A8151d292010-02-10 01:44:24 +00005300 if (adapter->vf_data[vf].pf_vlan)
5301 igb_ndo_set_vf_vlan(adapter->netdev, vf,
5302 adapter->vf_data[vf].pf_vlan,
5303 adapter->vf_data[vf].pf_qos);
5304 else
5305 igb_clear_vf_vfta(adapter, vf);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005306
5307 /* reset multicast table array for vf */
5308 adapter->vf_data[vf].num_vf_mc_hashes = 0;
5309
5310 /* Flush and reset the mta with the new values */
Alexander Duyckff41f8d2009-09-03 14:48:56 +00005311 igb_set_rx_mode(adapter->netdev);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005312}
5313
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005314static void igb_vf_reset_event(struct igb_adapter *adapter, u32 vf)
5315{
5316 unsigned char *vf_mac = adapter->vf_data[vf].vf_mac_addresses;
5317
5318 /* generate a new mac address as we were hotplug removed/added */
Williams, Mitch A8151d292010-02-10 01:44:24 +00005319 if (!(adapter->vf_data[vf].flags & IGB_VF_FLAG_PF_SET_MAC))
Joe Perches7efd26d2012-07-12 19:33:06 +00005320 eth_random_addr(vf_mac);
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005321
5322 /* process remaining reset events */
5323 igb_vf_reset(adapter, vf);
5324}
5325
5326static void igb_vf_reset_msg(struct igb_adapter *adapter, u32 vf)
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005327{
5328 struct e1000_hw *hw = &adapter->hw;
5329 unsigned char *vf_mac = adapter->vf_data[vf].vf_mac_addresses;
Alexander Duyckff41f8d2009-09-03 14:48:56 +00005330 int rar_entry = hw->mac.rar_entry_count - (vf + 1);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005331 u32 reg, msgbuf[3];
5332 u8 *addr = (u8 *)(&msgbuf[1]);
5333
5334 /* process all the same items cleared in a function level reset */
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005335 igb_vf_reset(adapter, vf);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005336
5337 /* set vf mac address */
Alexander Duyck26ad9172009-10-05 06:32:49 +00005338 igb_rar_set_qsel(adapter, vf_mac, rar_entry, vf);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005339
5340 /* enable transmit and receive for vf */
5341 reg = rd32(E1000_VFTE);
5342 wr32(E1000_VFTE, reg | (1 << vf));
5343 reg = rd32(E1000_VFRE);
5344 wr32(E1000_VFRE, reg | (1 << vf));
5345
Greg Rose8fa7e0f2010-11-06 05:43:21 +00005346 adapter->vf_data[vf].flags |= IGB_VF_FLAG_CTS;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005347
5348 /* reply to reset with ack and vf mac address */
5349 msgbuf[0] = E1000_VF_RESET | E1000_VT_MSGTYPE_ACK;
5350 memcpy(addr, vf_mac, 6);
5351 igb_write_mbx(hw, msgbuf, 3, vf);
5352}
5353
5354static int igb_set_vf_mac_addr(struct igb_adapter *adapter, u32 *msg, int vf)
5355{
Greg Rosede42edd2010-07-01 13:39:23 +00005356 /*
5357 * The VF MAC Address is stored in a packed array of bytes
5358 * starting at the second 32 bit word of the msg array
5359 */
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005360 unsigned char *addr = (char *)&msg[1];
5361 int err = -1;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005362
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005363 if (is_valid_ether_addr(addr))
5364 err = igb_set_vf_mac(adapter, vf, addr);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005365
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005366 return err;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005367}
5368
5369static void igb_rcv_ack_from_vf(struct igb_adapter *adapter, u32 vf)
5370{
5371 struct e1000_hw *hw = &adapter->hw;
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005372 struct vf_data_storage *vf_data = &adapter->vf_data[vf];
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005373 u32 msg = E1000_VT_MSGTYPE_NACK;
5374
5375 /* if device isn't clear to send it shouldn't be reading either */
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005376 if (!(vf_data->flags & IGB_VF_FLAG_CTS) &&
5377 time_after(jiffies, vf_data->last_nack + (2 * HZ))) {
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005378 igb_write_mbx(hw, &msg, 1, vf);
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005379 vf_data->last_nack = jiffies;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005380 }
5381}
5382
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005383static void igb_rcv_msg_from_vf(struct igb_adapter *adapter, u32 vf)
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005384{
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005385 struct pci_dev *pdev = adapter->pdev;
5386 u32 msgbuf[E1000_VFMAILBOX_SIZE];
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005387 struct e1000_hw *hw = &adapter->hw;
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005388 struct vf_data_storage *vf_data = &adapter->vf_data[vf];
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005389 s32 retval;
5390
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005391 retval = igb_read_mbx(hw, msgbuf, E1000_VFMAILBOX_SIZE, vf);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005392
Alexander Duyckfef45f42009-12-11 22:57:34 -08005393 if (retval) {
5394 /* if receive failed revoke VF CTS stats and restart init */
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005395 dev_err(&pdev->dev, "Error receiving message from VF\n");
Alexander Duyckfef45f42009-12-11 22:57:34 -08005396 vf_data->flags &= ~IGB_VF_FLAG_CTS;
5397 if (!time_after(jiffies, vf_data->last_nack + (2 * HZ)))
5398 return;
5399 goto out;
5400 }
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005401
5402 /* this is a message we already processed, do nothing */
5403 if (msgbuf[0] & (E1000_VT_MSGTYPE_ACK | E1000_VT_MSGTYPE_NACK))
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005404 return;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005405
5406 /*
5407 * until the vf completes a reset it should not be
5408 * allowed to start any configuration.
5409 */
5410
5411 if (msgbuf[0] == E1000_VF_RESET) {
5412 igb_vf_reset_msg(adapter, vf);
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005413 return;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005414 }
5415
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005416 if (!(vf_data->flags & IGB_VF_FLAG_CTS)) {
Alexander Duyckfef45f42009-12-11 22:57:34 -08005417 if (!time_after(jiffies, vf_data->last_nack + (2 * HZ)))
5418 return;
5419 retval = -1;
5420 goto out;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005421 }
5422
5423 switch ((msgbuf[0] & 0xFFFF)) {
5424 case E1000_VF_SET_MAC_ADDR:
Greg Rosea6b5ea32010-11-06 05:42:59 +00005425 retval = -EINVAL;
5426 if (!(vf_data->flags & IGB_VF_FLAG_PF_SET_MAC))
5427 retval = igb_set_vf_mac_addr(adapter, msgbuf, vf);
5428 else
5429 dev_warn(&pdev->dev,
5430 "VF %d attempted to override administratively "
5431 "set MAC address\nReload the VF driver to "
5432 "resume operations\n", vf);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005433 break;
Alexander Duyck7d5753f2009-10-27 23:47:16 +00005434 case E1000_VF_SET_PROMISC:
5435 retval = igb_set_vf_promisc(adapter, msgbuf, vf);
5436 break;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005437 case E1000_VF_SET_MULTICAST:
5438 retval = igb_set_vf_multicasts(adapter, msgbuf, vf);
5439 break;
5440 case E1000_VF_SET_LPE:
5441 retval = igb_set_vf_rlpml(adapter, msgbuf[1], vf);
5442 break;
5443 case E1000_VF_SET_VLAN:
Greg Rosea6b5ea32010-11-06 05:42:59 +00005444 retval = -1;
5445 if (vf_data->pf_vlan)
5446 dev_warn(&pdev->dev,
5447 "VF %d attempted to override administratively "
5448 "set VLAN tag\nReload the VF driver to "
5449 "resume operations\n", vf);
Williams, Mitch A8151d292010-02-10 01:44:24 +00005450 else
5451 retval = igb_set_vf_vlan(adapter, msgbuf, vf);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005452 break;
5453 default:
Alexander Duyck090b1792009-10-27 23:51:55 +00005454 dev_err(&pdev->dev, "Unhandled Msg %08x\n", msgbuf[0]);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005455 retval = -1;
5456 break;
5457 }
5458
Alexander Duyckfef45f42009-12-11 22:57:34 -08005459 msgbuf[0] |= E1000_VT_MSGTYPE_CTS;
5460out:
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005461 /* notify the VF of the results of what it sent us */
5462 if (retval)
5463 msgbuf[0] |= E1000_VT_MSGTYPE_NACK;
5464 else
5465 msgbuf[0] |= E1000_VT_MSGTYPE_ACK;
5466
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005467 igb_write_mbx(hw, msgbuf, 1, vf);
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005468}
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005469
Alexander Duyckf2ca0db2009-10-27 23:46:57 +00005470static void igb_msg_task(struct igb_adapter *adapter)
5471{
5472 struct e1000_hw *hw = &adapter->hw;
5473 u32 vf;
5474
5475 for (vf = 0; vf < adapter->vfs_allocated_count; vf++) {
5476 /* process any reset requests */
5477 if (!igb_check_for_rst(hw, vf))
5478 igb_vf_reset_event(adapter, vf);
5479
5480 /* process any messages pending */
5481 if (!igb_check_for_msg(hw, vf))
5482 igb_rcv_msg_from_vf(adapter, vf);
5483
5484 /* process any acks */
5485 if (!igb_check_for_ack(hw, vf))
5486 igb_rcv_ack_from_vf(adapter, vf);
5487 }
Alexander Duyck4ae196d2009-02-19 20:40:07 -08005488}
5489
Auke Kok9d5c8242008-01-24 02:22:38 -08005490/**
Alexander Duyck68d480c2009-10-05 06:33:08 +00005491 * igb_set_uta - Set unicast filter table address
5492 * @adapter: board private structure
5493 *
5494 * The unicast table address is a register array of 32-bit registers.
5495 * The table is meant to be used in a way similar to how the MTA is used
5496 * however due to certain limitations in the hardware it is necessary to
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005497 * set all the hash bits to 1 and use the VMOLR ROPE bit as a promiscuous
5498 * enable bit to allow vlan tag stripping when promiscuous mode is enabled
Alexander Duyck68d480c2009-10-05 06:33:08 +00005499 **/
5500static void igb_set_uta(struct igb_adapter *adapter)
5501{
5502 struct e1000_hw *hw = &adapter->hw;
5503 int i;
5504
5505 /* The UTA table only exists on 82576 hardware and newer */
5506 if (hw->mac.type < e1000_82576)
5507 return;
5508
5509 /* we only need to do this if VMDq is enabled */
5510 if (!adapter->vfs_allocated_count)
5511 return;
5512
5513 for (i = 0; i < hw->mac.uta_reg_count; i++)
5514 array_wr32(E1000_UTA, i, ~0);
5515}
5516
5517/**
Auke Kok9d5c8242008-01-24 02:22:38 -08005518 * igb_intr_msi - Interrupt Handler
5519 * @irq: interrupt number
5520 * @data: pointer to a network interface device structure
5521 **/
5522static irqreturn_t igb_intr_msi(int irq, void *data)
5523{
Alexander Duyck047e0032009-10-27 15:49:27 +00005524 struct igb_adapter *adapter = data;
5525 struct igb_q_vector *q_vector = adapter->q_vector[0];
Auke Kok9d5c8242008-01-24 02:22:38 -08005526 struct e1000_hw *hw = &adapter->hw;
5527 /* read ICR disables interrupts using IAM */
5528 u32 icr = rd32(E1000_ICR);
5529
Alexander Duyck047e0032009-10-27 15:49:27 +00005530 igb_write_itr(q_vector);
Auke Kok9d5c8242008-01-24 02:22:38 -08005531
Alexander Duyck7f081d42010-01-07 17:41:00 +00005532 if (icr & E1000_ICR_DRSTA)
5533 schedule_work(&adapter->reset_task);
5534
Alexander Duyck047e0032009-10-27 15:49:27 +00005535 if (icr & E1000_ICR_DOUTSYNC) {
Alexander Duyckdda0e082009-02-06 23:19:08 +00005536 /* HW is reporting DMA is out of sync */
5537 adapter->stats.doosync++;
5538 }
5539
Auke Kok9d5c8242008-01-24 02:22:38 -08005540 if (icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) {
5541 hw->mac.get_link_status = 1;
5542 if (!test_bit(__IGB_DOWN, &adapter->state))
5543 mod_timer(&adapter->watchdog_timer, jiffies + 1);
5544 }
5545
Matthew Vick1f6e8172012-08-18 07:26:33 +00005546 if (icr & E1000_ICR_TS) {
5547 u32 tsicr = rd32(E1000_TSICR);
5548
5549 if (tsicr & E1000_TSICR_TXTS) {
5550 /* acknowledge the interrupt */
5551 wr32(E1000_TSICR, E1000_TSICR_TXTS);
5552 /* retrieve hardware timestamp */
5553 schedule_work(&adapter->ptp_tx_work);
5554 }
5555 }
Matthew Vick1f6e8172012-08-18 07:26:33 +00005556
Alexander Duyck047e0032009-10-27 15:49:27 +00005557 napi_schedule(&q_vector->napi);
Auke Kok9d5c8242008-01-24 02:22:38 -08005558
5559 return IRQ_HANDLED;
5560}
5561
5562/**
Alexander Duyck4a3c6432009-02-06 23:20:49 +00005563 * igb_intr - Legacy Interrupt Handler
Auke Kok9d5c8242008-01-24 02:22:38 -08005564 * @irq: interrupt number
5565 * @data: pointer to a network interface device structure
5566 **/
5567static irqreturn_t igb_intr(int irq, void *data)
5568{
Alexander Duyck047e0032009-10-27 15:49:27 +00005569 struct igb_adapter *adapter = data;
5570 struct igb_q_vector *q_vector = adapter->q_vector[0];
Auke Kok9d5c8242008-01-24 02:22:38 -08005571 struct e1000_hw *hw = &adapter->hw;
5572 /* Interrupt Auto-Mask...upon reading ICR, interrupts are masked. No
5573 * need for the IMC write */
5574 u32 icr = rd32(E1000_ICR);
Auke Kok9d5c8242008-01-24 02:22:38 -08005575
5576 /* IMS will not auto-mask if INT_ASSERTED is not set, and if it is
5577 * not set, then the adapter didn't send an interrupt */
5578 if (!(icr & E1000_ICR_INT_ASSERTED))
5579 return IRQ_NONE;
5580
Alexander Duyck0ba82992011-08-26 07:45:47 +00005581 igb_write_itr(q_vector);
5582
Alexander Duyck7f081d42010-01-07 17:41:00 +00005583 if (icr & E1000_ICR_DRSTA)
5584 schedule_work(&adapter->reset_task);
5585
Alexander Duyck047e0032009-10-27 15:49:27 +00005586 if (icr & E1000_ICR_DOUTSYNC) {
Alexander Duyckdda0e082009-02-06 23:19:08 +00005587 /* HW is reporting DMA is out of sync */
5588 adapter->stats.doosync++;
5589 }
5590
Auke Kok9d5c8242008-01-24 02:22:38 -08005591 if (icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) {
5592 hw->mac.get_link_status = 1;
5593 /* guard against interrupt when we're going down */
5594 if (!test_bit(__IGB_DOWN, &adapter->state))
5595 mod_timer(&adapter->watchdog_timer, jiffies + 1);
5596 }
5597
Matthew Vick1f6e8172012-08-18 07:26:33 +00005598 if (icr & E1000_ICR_TS) {
5599 u32 tsicr = rd32(E1000_TSICR);
5600
5601 if (tsicr & E1000_TSICR_TXTS) {
5602 /* acknowledge the interrupt */
5603 wr32(E1000_TSICR, E1000_TSICR_TXTS);
5604 /* retrieve hardware timestamp */
5605 schedule_work(&adapter->ptp_tx_work);
5606 }
5607 }
Matthew Vick1f6e8172012-08-18 07:26:33 +00005608
Alexander Duyck047e0032009-10-27 15:49:27 +00005609 napi_schedule(&q_vector->napi);
Auke Kok9d5c8242008-01-24 02:22:38 -08005610
5611 return IRQ_HANDLED;
5612}
5613
Stephen Hemmingerc50b52a2012-01-18 22:13:26 +00005614static void igb_ring_irq_enable(struct igb_q_vector *q_vector)
Alexander Duyck46544252009-02-19 20:39:04 -08005615{
Alexander Duyck047e0032009-10-27 15:49:27 +00005616 struct igb_adapter *adapter = q_vector->adapter;
Alexander Duyck46544252009-02-19 20:39:04 -08005617 struct e1000_hw *hw = &adapter->hw;
5618
Alexander Duyck0ba82992011-08-26 07:45:47 +00005619 if ((q_vector->rx.ring && (adapter->rx_itr_setting & 3)) ||
5620 (!q_vector->rx.ring && (adapter->tx_itr_setting & 3))) {
5621 if ((adapter->num_q_vectors == 1) && !adapter->vf_data)
5622 igb_set_itr(q_vector);
Alexander Duyck46544252009-02-19 20:39:04 -08005623 else
Alexander Duyck047e0032009-10-27 15:49:27 +00005624 igb_update_ring_itr(q_vector);
Alexander Duyck46544252009-02-19 20:39:04 -08005625 }
5626
5627 if (!test_bit(__IGB_DOWN, &adapter->state)) {
5628 if (adapter->msix_entries)
Alexander Duyck047e0032009-10-27 15:49:27 +00005629 wr32(E1000_EIMS, q_vector->eims_value);
Alexander Duyck46544252009-02-19 20:39:04 -08005630 else
5631 igb_irq_enable(adapter);
5632 }
5633}
5634
Auke Kok9d5c8242008-01-24 02:22:38 -08005635/**
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07005636 * igb_poll - NAPI Rx polling callback
5637 * @napi: napi polling structure
5638 * @budget: count of how many packets we should handle
Auke Kok9d5c8242008-01-24 02:22:38 -08005639 **/
Peter P Waskiewicz Jr661086d2008-07-08 15:06:51 -07005640static int igb_poll(struct napi_struct *napi, int budget)
Auke Kok9d5c8242008-01-24 02:22:38 -08005641{
Alexander Duyck047e0032009-10-27 15:49:27 +00005642 struct igb_q_vector *q_vector = container_of(napi,
5643 struct igb_q_vector,
5644 napi);
Alexander Duyck16eb8812011-08-26 07:43:54 +00005645 bool clean_complete = true;
Auke Kok9d5c8242008-01-24 02:22:38 -08005646
Jeff Kirsher421e02f2008-10-17 11:08:31 -07005647#ifdef CONFIG_IGB_DCA
Alexander Duyck047e0032009-10-27 15:49:27 +00005648 if (q_vector->adapter->flags & IGB_FLAG_DCA_ENABLED)
5649 igb_update_dca(q_vector);
Jeb Cramerfe4506b2008-07-08 15:07:55 -07005650#endif
Alexander Duyck0ba82992011-08-26 07:45:47 +00005651 if (q_vector->tx.ring)
Alexander Duyck13fde972011-10-05 13:35:24 +00005652 clean_complete = igb_clean_tx_irq(q_vector);
Auke Kok9d5c8242008-01-24 02:22:38 -08005653
Alexander Duyck0ba82992011-08-26 07:45:47 +00005654 if (q_vector->rx.ring)
Alexander Duyckcd392f52011-08-26 07:43:59 +00005655 clean_complete &= igb_clean_rx_irq(q_vector, budget);
Alexander Duyck047e0032009-10-27 15:49:27 +00005656
Alexander Duyck16eb8812011-08-26 07:43:54 +00005657 /* If all work not completed, return budget and keep polling */
5658 if (!clean_complete)
5659 return budget;
Auke Kok9d5c8242008-01-24 02:22:38 -08005660
Alexander Duyck46544252009-02-19 20:39:04 -08005661 /* If not enough Rx work done, exit the polling mode */
Alexander Duyck16eb8812011-08-26 07:43:54 +00005662 napi_complete(napi);
5663 igb_ring_irq_enable(q_vector);
Alexander Duyck46544252009-02-19 20:39:04 -08005664
Alexander Duyck16eb8812011-08-26 07:43:54 +00005665 return 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08005666}
Al Viro6d8126f2008-03-16 22:23:24 +00005667
Patrick Ohly33af6bc2009-02-12 05:03:43 +00005668/**
Auke Kok9d5c8242008-01-24 02:22:38 -08005669 * igb_clean_tx_irq - Reclaim resources after transmit completes
Alexander Duyck047e0032009-10-27 15:49:27 +00005670 * @q_vector: pointer to q_vector containing needed info
Ben Hutchings49ce9c22012-07-10 10:56:00 +00005671 *
Auke Kok9d5c8242008-01-24 02:22:38 -08005672 * returns true if ring is completely cleaned
5673 **/
Alexander Duyck047e0032009-10-27 15:49:27 +00005674static bool igb_clean_tx_irq(struct igb_q_vector *q_vector)
Auke Kok9d5c8242008-01-24 02:22:38 -08005675{
Alexander Duyck047e0032009-10-27 15:49:27 +00005676 struct igb_adapter *adapter = q_vector->adapter;
Alexander Duyck0ba82992011-08-26 07:45:47 +00005677 struct igb_ring *tx_ring = q_vector->tx.ring;
Alexander Duyck06034642011-08-26 07:44:22 +00005678 struct igb_tx_buffer *tx_buffer;
Alexander Duyckf4128782012-09-13 06:28:01 +00005679 union e1000_adv_tx_desc *tx_desc;
Auke Kok9d5c8242008-01-24 02:22:38 -08005680 unsigned int total_bytes = 0, total_packets = 0;
Alexander Duyck0ba82992011-08-26 07:45:47 +00005681 unsigned int budget = q_vector->tx.work_limit;
Alexander Duyck8542db02011-08-26 07:44:43 +00005682 unsigned int i = tx_ring->next_to_clean;
Auke Kok9d5c8242008-01-24 02:22:38 -08005683
Alexander Duyck13fde972011-10-05 13:35:24 +00005684 if (test_bit(__IGB_DOWN, &adapter->state))
5685 return true;
Alexander Duyck0e014cb2008-12-26 01:33:18 -08005686
Alexander Duyck06034642011-08-26 07:44:22 +00005687 tx_buffer = &tx_ring->tx_buffer_info[i];
Alexander Duyck13fde972011-10-05 13:35:24 +00005688 tx_desc = IGB_TX_DESC(tx_ring, i);
Alexander Duyck8542db02011-08-26 07:44:43 +00005689 i -= tx_ring->count;
Auke Kok9d5c8242008-01-24 02:22:38 -08005690
Alexander Duyckf4128782012-09-13 06:28:01 +00005691 do {
5692 union e1000_adv_tx_desc *eop_desc = tx_buffer->next_to_watch;
Alexander Duyck8542db02011-08-26 07:44:43 +00005693
5694 /* if next_to_watch is not set then there is no work pending */
5695 if (!eop_desc)
5696 break;
Alexander Duyck13fde972011-10-05 13:35:24 +00005697
Alexander Duyckf4128782012-09-13 06:28:01 +00005698 /* prevent any other reads prior to eop_desc */
5699 rmb();
5700
Alexander Duyck13fde972011-10-05 13:35:24 +00005701 /* if DD is not set pending work has not been completed */
5702 if (!(eop_desc->wb.status & cpu_to_le32(E1000_TXD_STAT_DD)))
5703 break;
5704
Alexander Duyck8542db02011-08-26 07:44:43 +00005705 /* clear next_to_watch to prevent false hangs */
5706 tx_buffer->next_to_watch = NULL;
Alexander Duyck13fde972011-10-05 13:35:24 +00005707
Alexander Duyckebe42d12011-08-26 07:45:09 +00005708 /* update the statistics for this packet */
5709 total_bytes += tx_buffer->bytecount;
5710 total_packets += tx_buffer->gso_segs;
Alexander Duyck13fde972011-10-05 13:35:24 +00005711
Alexander Duyckebe42d12011-08-26 07:45:09 +00005712 /* free the skb */
5713 dev_kfree_skb_any(tx_buffer->skb);
Alexander Duyckebe42d12011-08-26 07:45:09 +00005714
5715 /* unmap skb header data */
5716 dma_unmap_single(tx_ring->dev,
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00005717 dma_unmap_addr(tx_buffer, dma),
5718 dma_unmap_len(tx_buffer, len),
Alexander Duyckebe42d12011-08-26 07:45:09 +00005719 DMA_TO_DEVICE);
5720
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00005721 /* clear tx_buffer data */
5722 tx_buffer->skb = NULL;
5723 dma_unmap_len_set(tx_buffer, len, 0);
5724
Alexander Duyckebe42d12011-08-26 07:45:09 +00005725 /* clear last DMA location and unmap remaining buffers */
5726 while (tx_desc != eop_desc) {
Alexander Duyck13fde972011-10-05 13:35:24 +00005727 tx_buffer++;
5728 tx_desc++;
Auke Kok9d5c8242008-01-24 02:22:38 -08005729 i++;
Alexander Duyck8542db02011-08-26 07:44:43 +00005730 if (unlikely(!i)) {
5731 i -= tx_ring->count;
Alexander Duyck06034642011-08-26 07:44:22 +00005732 tx_buffer = tx_ring->tx_buffer_info;
Alexander Duyck13fde972011-10-05 13:35:24 +00005733 tx_desc = IGB_TX_DESC(tx_ring, 0);
5734 }
Alexander Duyckebe42d12011-08-26 07:45:09 +00005735
5736 /* unmap any remaining paged data */
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00005737 if (dma_unmap_len(tx_buffer, len)) {
Alexander Duyckebe42d12011-08-26 07:45:09 +00005738 dma_unmap_page(tx_ring->dev,
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00005739 dma_unmap_addr(tx_buffer, dma),
5740 dma_unmap_len(tx_buffer, len),
Alexander Duyckebe42d12011-08-26 07:45:09 +00005741 DMA_TO_DEVICE);
Alexander Duyckc9f14bf32012-09-18 01:56:27 +00005742 dma_unmap_len_set(tx_buffer, len, 0);
Alexander Duyckebe42d12011-08-26 07:45:09 +00005743 }
5744 }
5745
Alexander Duyckebe42d12011-08-26 07:45:09 +00005746 /* move us one more past the eop_desc for start of next pkt */
5747 tx_buffer++;
5748 tx_desc++;
5749 i++;
5750 if (unlikely(!i)) {
5751 i -= tx_ring->count;
5752 tx_buffer = tx_ring->tx_buffer_info;
5753 tx_desc = IGB_TX_DESC(tx_ring, 0);
5754 }
Alexander Duyckf4128782012-09-13 06:28:01 +00005755
5756 /* issue prefetch for next Tx descriptor */
5757 prefetch(tx_desc);
5758
5759 /* update budget accounting */
5760 budget--;
5761 } while (likely(budget));
Alexander Duyck0e014cb2008-12-26 01:33:18 -08005762
Eric Dumazetbdbc0632012-01-04 20:23:36 +00005763 netdev_tx_completed_queue(txring_txq(tx_ring),
5764 total_packets, total_bytes);
Alexander Duyck8542db02011-08-26 07:44:43 +00005765 i += tx_ring->count;
Auke Kok9d5c8242008-01-24 02:22:38 -08005766 tx_ring->next_to_clean = i;
Alexander Duyck13fde972011-10-05 13:35:24 +00005767 u64_stats_update_begin(&tx_ring->tx_syncp);
5768 tx_ring->tx_stats.bytes += total_bytes;
5769 tx_ring->tx_stats.packets += total_packets;
5770 u64_stats_update_end(&tx_ring->tx_syncp);
Alexander Duyck0ba82992011-08-26 07:45:47 +00005771 q_vector->tx.total_bytes += total_bytes;
5772 q_vector->tx.total_packets += total_packets;
Auke Kok9d5c8242008-01-24 02:22:38 -08005773
Alexander Duyck6d095fa2011-08-26 07:46:19 +00005774 if (test_bit(IGB_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags)) {
Alexander Duyck13fde972011-10-05 13:35:24 +00005775 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck13fde972011-10-05 13:35:24 +00005776
Auke Kok9d5c8242008-01-24 02:22:38 -08005777 /* Detect a transmit hang in hardware, this serializes the
5778 * check with the clearing of time_stamp and movement of i */
Alexander Duyck6d095fa2011-08-26 07:46:19 +00005779 clear_bit(IGB_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags);
Alexander Duyckf4128782012-09-13 06:28:01 +00005780 if (tx_buffer->next_to_watch &&
Alexander Duyck8542db02011-08-26 07:44:43 +00005781 time_after(jiffies, tx_buffer->time_stamp +
Joe Perches8e95a202009-12-03 07:58:21 +00005782 (adapter->tx_timeout_factor * HZ)) &&
5783 !(rd32(E1000_STATUS) & E1000_STATUS_TXOFF)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08005784
Auke Kok9d5c8242008-01-24 02:22:38 -08005785 /* detected Tx unit hang */
Alexander Duyck59d71982010-04-27 13:09:25 +00005786 dev_err(tx_ring->dev,
Auke Kok9d5c8242008-01-24 02:22:38 -08005787 "Detected Tx Unit Hang\n"
Alexander Duyck2d064c02008-07-08 15:10:12 -07005788 " Tx Queue <%d>\n"
Auke Kok9d5c8242008-01-24 02:22:38 -08005789 " TDH <%x>\n"
5790 " TDT <%x>\n"
5791 " next_to_use <%x>\n"
5792 " next_to_clean <%x>\n"
Auke Kok9d5c8242008-01-24 02:22:38 -08005793 "buffer_info[next_to_clean]\n"
5794 " time_stamp <%lx>\n"
Alexander Duyck8542db02011-08-26 07:44:43 +00005795 " next_to_watch <%p>\n"
Auke Kok9d5c8242008-01-24 02:22:38 -08005796 " jiffies <%lx>\n"
5797 " desc.status <%x>\n",
Alexander Duyck2d064c02008-07-08 15:10:12 -07005798 tx_ring->queue_index,
Alexander Duyck238ac812011-08-26 07:43:48 +00005799 rd32(E1000_TDH(tx_ring->reg_idx)),
Alexander Duyckfce99e32009-10-27 15:51:27 +00005800 readl(tx_ring->tail),
Auke Kok9d5c8242008-01-24 02:22:38 -08005801 tx_ring->next_to_use,
5802 tx_ring->next_to_clean,
Alexander Duyck8542db02011-08-26 07:44:43 +00005803 tx_buffer->time_stamp,
Alexander Duyckf4128782012-09-13 06:28:01 +00005804 tx_buffer->next_to_watch,
Auke Kok9d5c8242008-01-24 02:22:38 -08005805 jiffies,
Alexander Duyckf4128782012-09-13 06:28:01 +00005806 tx_buffer->next_to_watch->wb.status);
Alexander Duyck13fde972011-10-05 13:35:24 +00005807 netif_stop_subqueue(tx_ring->netdev,
5808 tx_ring->queue_index);
5809
5810 /* we are about to reset, no point in enabling stuff */
5811 return true;
Auke Kok9d5c8242008-01-24 02:22:38 -08005812 }
5813 }
Alexander Duyck13fde972011-10-05 13:35:24 +00005814
5815 if (unlikely(total_packets &&
5816 netif_carrier_ok(tx_ring->netdev) &&
5817 igb_desc_unused(tx_ring) >= IGB_TX_QUEUE_WAKE)) {
5818 /* Make sure that anybody stopping the queue after this
5819 * sees the new next_to_clean.
5820 */
5821 smp_mb();
5822 if (__netif_subqueue_stopped(tx_ring->netdev,
5823 tx_ring->queue_index) &&
5824 !(test_bit(__IGB_DOWN, &adapter->state))) {
5825 netif_wake_subqueue(tx_ring->netdev,
5826 tx_ring->queue_index);
5827
5828 u64_stats_update_begin(&tx_ring->tx_syncp);
5829 tx_ring->tx_stats.restart_queue++;
5830 u64_stats_update_end(&tx_ring->tx_syncp);
5831 }
5832 }
5833
5834 return !!budget;
Auke Kok9d5c8242008-01-24 02:22:38 -08005835}
5836
Alexander Duyckcbc8e552012-09-25 00:31:02 +00005837/**
5838 * igb_reuse_rx_page - page flip buffer and store it back on the ring
5839 * @rx_ring: rx descriptor ring to store buffers on
5840 * @old_buff: donor buffer to have page reused
5841 *
5842 * Synchronizes page for reuse by the adapter
5843 **/
5844static void igb_reuse_rx_page(struct igb_ring *rx_ring,
5845 struct igb_rx_buffer *old_buff)
5846{
5847 struct igb_rx_buffer *new_buff;
5848 u16 nta = rx_ring->next_to_alloc;
5849
5850 new_buff = &rx_ring->rx_buffer_info[nta];
5851
5852 /* update, and store next to alloc */
5853 nta++;
5854 rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0;
5855
5856 /* transfer page from old buffer to new buffer */
5857 memcpy(new_buff, old_buff, sizeof(struct igb_rx_buffer));
5858
5859 /* sync the buffer for use by the device */
5860 dma_sync_single_range_for_device(rx_ring->dev, old_buff->dma,
5861 old_buff->page_offset,
Alexander Duyckde78d1f2012-09-25 00:31:12 +00005862 IGB_RX_BUFSZ,
Alexander Duyckcbc8e552012-09-25 00:31:02 +00005863 DMA_FROM_DEVICE);
5864}
5865
5866/**
5867 * igb_add_rx_frag - Add contents of Rx buffer to sk_buff
5868 * @rx_ring: rx descriptor ring to transact packets on
5869 * @rx_buffer: buffer containing page to add
5870 * @rx_desc: descriptor containing length of buffer written by hardware
5871 * @skb: sk_buff to place the data into
5872 *
5873 * This function will add the data contained in rx_buffer->page to the skb.
5874 * This is done either through a direct copy if the data in the buffer is
5875 * less than the skb header size, otherwise it will just attach the page as
5876 * a frag to the skb.
5877 *
5878 * The function will then update the page offset if necessary and return
5879 * true if the buffer can be reused by the adapter.
5880 **/
5881static bool igb_add_rx_frag(struct igb_ring *rx_ring,
5882 struct igb_rx_buffer *rx_buffer,
5883 union e1000_adv_rx_desc *rx_desc,
5884 struct sk_buff *skb)
5885{
5886 struct page *page = rx_buffer->page;
5887 unsigned int size = le16_to_cpu(rx_desc->wb.upper.length);
5888
5889 if ((size <= IGB_RX_HDR_LEN) && !skb_is_nonlinear(skb)) {
5890 unsigned char *va = page_address(page) + rx_buffer->page_offset;
5891
Alexander Duyckcbc8e552012-09-25 00:31:02 +00005892 if (igb_test_staterr(rx_desc, E1000_RXDADV_STAT_TSIP)) {
5893 igb_ptp_rx_pktstamp(rx_ring->q_vector, va, skb);
5894 va += IGB_TS_HDR_LEN;
5895 size -= IGB_TS_HDR_LEN;
5896 }
5897
Alexander Duyckcbc8e552012-09-25 00:31:02 +00005898 memcpy(__skb_put(skb, size), va, ALIGN(size, sizeof(long)));
5899
5900 /* we can reuse buffer as-is, just make sure it is local */
5901 if (likely(page_to_nid(page) == numa_node_id()))
5902 return true;
5903
5904 /* this page cannot be reused so discard it */
5905 put_page(page);
5906 return false;
5907 }
5908
5909 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page,
Alexander Duyckde78d1f2012-09-25 00:31:12 +00005910 rx_buffer->page_offset, size, IGB_RX_BUFSZ);
Alexander Duyckcbc8e552012-09-25 00:31:02 +00005911
5912 /* avoid re-using remote pages */
5913 if (unlikely(page_to_nid(page) != numa_node_id()))
5914 return false;
5915
Alexander Duyckde78d1f2012-09-25 00:31:12 +00005916#if (PAGE_SIZE < 8192)
Alexander Duyckcbc8e552012-09-25 00:31:02 +00005917 /* if we are only owner of page we can reuse it */
5918 if (unlikely(page_count(page) != 1))
5919 return false;
5920
5921 /* flip page offset to other buffer */
Alexander Duyckde78d1f2012-09-25 00:31:12 +00005922 rx_buffer->page_offset ^= IGB_RX_BUFSZ;
Alexander Duyckcbc8e552012-09-25 00:31:02 +00005923
5924 /*
5925 * since we are the only owner of the page and we need to
5926 * increment it, just set the value to 2 in order to avoid
5927 * an unnecessary locked operation
5928 */
5929 atomic_set(&page->_count, 2);
Alexander Duyckde78d1f2012-09-25 00:31:12 +00005930#else
5931 /* move offset up to the next cache line */
5932 rx_buffer->page_offset += SKB_DATA_ALIGN(size);
5933
5934 if (rx_buffer->page_offset > (PAGE_SIZE - IGB_RX_BUFSZ))
5935 return false;
5936
5937 /* bump ref count on page before it is given to the stack */
5938 get_page(page);
5939#endif
Alexander Duyckcbc8e552012-09-25 00:31:02 +00005940
5941 return true;
5942}
5943
Alexander Duyck2e334ee2012-09-25 00:31:07 +00005944static struct sk_buff *igb_fetch_rx_buffer(struct igb_ring *rx_ring,
5945 union e1000_adv_rx_desc *rx_desc,
5946 struct sk_buff *skb)
5947{
5948 struct igb_rx_buffer *rx_buffer;
5949 struct page *page;
5950
5951 rx_buffer = &rx_ring->rx_buffer_info[rx_ring->next_to_clean];
5952
5953 /*
5954 * This memory barrier is needed to keep us from reading
5955 * any other fields out of the rx_desc until we know the
5956 * RXD_STAT_DD bit is set
5957 */
5958 rmb();
5959
5960 page = rx_buffer->page;
5961 prefetchw(page);
5962
5963 if (likely(!skb)) {
5964 void *page_addr = page_address(page) +
5965 rx_buffer->page_offset;
5966
5967 /* prefetch first cache line of first page */
5968 prefetch(page_addr);
5969#if L1_CACHE_BYTES < 128
5970 prefetch(page_addr + L1_CACHE_BYTES);
5971#endif
5972
5973 /* allocate a skb to store the frags */
5974 skb = netdev_alloc_skb_ip_align(rx_ring->netdev,
5975 IGB_RX_HDR_LEN);
5976 if (unlikely(!skb)) {
5977 rx_ring->rx_stats.alloc_failed++;
5978 return NULL;
5979 }
5980
5981 /*
5982 * we will be copying header into skb->data in
5983 * pskb_may_pull so it is in our interest to prefetch
5984 * it now to avoid a possible cache miss
5985 */
5986 prefetchw(skb->data);
5987 }
5988
5989 /* we are reusing so sync this buffer for CPU use */
5990 dma_sync_single_range_for_cpu(rx_ring->dev,
5991 rx_buffer->dma,
5992 rx_buffer->page_offset,
Alexander Duyckde78d1f2012-09-25 00:31:12 +00005993 IGB_RX_BUFSZ,
Alexander Duyck2e334ee2012-09-25 00:31:07 +00005994 DMA_FROM_DEVICE);
5995
5996 /* pull page into skb */
5997 if (igb_add_rx_frag(rx_ring, rx_buffer, rx_desc, skb)) {
5998 /* hand second half of page back to the ring */
5999 igb_reuse_rx_page(rx_ring, rx_buffer);
6000 } else {
6001 /* we are not reusing the buffer so unmap it */
6002 dma_unmap_page(rx_ring->dev, rx_buffer->dma,
6003 PAGE_SIZE, DMA_FROM_DEVICE);
6004 }
6005
6006 /* clear contents of rx_buffer */
6007 rx_buffer->page = NULL;
6008
6009 return skb;
6010}
6011
Alexander Duyckcd392f52011-08-26 07:43:59 +00006012static inline void igb_rx_checksum(struct igb_ring *ring,
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00006013 union e1000_adv_rx_desc *rx_desc,
6014 struct sk_buff *skb)
Auke Kok9d5c8242008-01-24 02:22:38 -08006015{
Eric Dumazetbc8acf22010-09-02 13:07:41 -07006016 skb_checksum_none_assert(skb);
Auke Kok9d5c8242008-01-24 02:22:38 -08006017
Alexander Duyck294e7d72011-08-26 07:45:57 +00006018 /* Ignore Checksum bit is set */
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00006019 if (igb_test_staterr(rx_desc, E1000_RXD_STAT_IXSM))
Alexander Duyck294e7d72011-08-26 07:45:57 +00006020 return;
6021
6022 /* Rx checksum disabled via ethtool */
6023 if (!(ring->netdev->features & NETIF_F_RXCSUM))
Auke Kok9d5c8242008-01-24 02:22:38 -08006024 return;
Alexander Duyck85ad76b2009-10-27 15:52:46 +00006025
Auke Kok9d5c8242008-01-24 02:22:38 -08006026 /* TCP/UDP checksum error bit is set */
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00006027 if (igb_test_staterr(rx_desc,
6028 E1000_RXDEXT_STATERR_TCPE |
6029 E1000_RXDEXT_STATERR_IPE)) {
Jesse Brandeburgb9473562009-04-27 22:36:13 +00006030 /*
6031 * work around errata with sctp packets where the TCPE aka
6032 * L4E bit is set incorrectly on 64 byte (60 byte w/o crc)
6033 * packets, (aka let the stack check the crc32c)
6034 */
Alexander Duyck866cff02011-08-26 07:45:36 +00006035 if (!((skb->len == 60) &&
6036 test_bit(IGB_RING_FLAG_RX_SCTP_CSUM, &ring->flags))) {
Eric Dumazet12dcd862010-10-15 17:27:10 +00006037 u64_stats_update_begin(&ring->rx_syncp);
Alexander Duyck04a5fcaa2009-10-27 15:52:27 +00006038 ring->rx_stats.csum_err++;
Eric Dumazet12dcd862010-10-15 17:27:10 +00006039 u64_stats_update_end(&ring->rx_syncp);
6040 }
Auke Kok9d5c8242008-01-24 02:22:38 -08006041 /* let the stack verify checksum errors */
Auke Kok9d5c8242008-01-24 02:22:38 -08006042 return;
6043 }
6044 /* It must be a TCP or UDP packet with a valid checksum */
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00006045 if (igb_test_staterr(rx_desc, E1000_RXD_STAT_TCPCS |
6046 E1000_RXD_STAT_UDPCS))
Auke Kok9d5c8242008-01-24 02:22:38 -08006047 skb->ip_summed = CHECKSUM_UNNECESSARY;
6048
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00006049 dev_dbg(ring->dev, "cksum success: bits %08X\n",
6050 le32_to_cpu(rx_desc->wb.upper.status_error));
Auke Kok9d5c8242008-01-24 02:22:38 -08006051}
6052
Alexander Duyck077887c2011-08-26 07:46:29 +00006053static inline void igb_rx_hash(struct igb_ring *ring,
6054 union e1000_adv_rx_desc *rx_desc,
6055 struct sk_buff *skb)
6056{
6057 if (ring->netdev->features & NETIF_F_RXHASH)
6058 skb->rxhash = le32_to_cpu(rx_desc->wb.lower.hi_dword.rss);
6059}
6060
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006061/**
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006062 * igb_is_non_eop - process handling of non-EOP buffers
6063 * @rx_ring: Rx ring being processed
6064 * @rx_desc: Rx descriptor for current buffer
6065 * @skb: current socket buffer containing buffer in progress
6066 *
6067 * This function updates next to clean. If the buffer is an EOP buffer
6068 * this function exits returning false, otherwise it will place the
6069 * sk_buff in the next buffer to be chained and return true indicating
6070 * that this is in fact a non-EOP buffer.
6071 **/
6072static bool igb_is_non_eop(struct igb_ring *rx_ring,
6073 union e1000_adv_rx_desc *rx_desc)
6074{
6075 u32 ntc = rx_ring->next_to_clean + 1;
6076
6077 /* fetch, update, and store next to clean */
6078 ntc = (ntc < rx_ring->count) ? ntc : 0;
6079 rx_ring->next_to_clean = ntc;
6080
6081 prefetch(IGB_RX_DESC(rx_ring, ntc));
6082
6083 if (likely(igb_test_staterr(rx_desc, E1000_RXD_STAT_EOP)))
6084 return false;
6085
6086 return true;
6087}
6088
6089/**
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006090 * igb_get_headlen - determine size of header for LRO/GRO
6091 * @data: pointer to the start of the headers
6092 * @max_len: total length of section to find headers in
6093 *
6094 * This function is meant to determine the length of headers that will
6095 * be recognized by hardware for LRO, and GRO offloads. The main
6096 * motivation of doing this is to only perform one pull for IPv4 TCP
6097 * packets so that we can do basic things like calculating the gso_size
6098 * based on the average data per packet.
6099 **/
6100static unsigned int igb_get_headlen(unsigned char *data,
6101 unsigned int max_len)
Alexander Duyck2d94d8a2009-07-23 18:10:06 +00006102{
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006103 union {
6104 unsigned char *network;
6105 /* l2 headers */
6106 struct ethhdr *eth;
6107 struct vlan_hdr *vlan;
6108 /* l3 headers */
6109 struct iphdr *ipv4;
6110 struct ipv6hdr *ipv6;
6111 } hdr;
6112 __be16 protocol;
6113 u8 nexthdr = 0; /* default to not TCP */
6114 u8 hlen;
6115
6116 /* this should never happen, but better safe than sorry */
6117 if (max_len < ETH_HLEN)
6118 return max_len;
6119
6120 /* initialize network frame pointer */
6121 hdr.network = data;
6122
6123 /* set first protocol and move network header forward */
6124 protocol = hdr.eth->h_proto;
6125 hdr.network += ETH_HLEN;
6126
6127 /* handle any vlan tag if present */
6128 if (protocol == __constant_htons(ETH_P_8021Q)) {
6129 if ((hdr.network - data) > (max_len - VLAN_HLEN))
6130 return max_len;
6131
6132 protocol = hdr.vlan->h_vlan_encapsulated_proto;
6133 hdr.network += VLAN_HLEN;
6134 }
6135
6136 /* handle L3 protocols */
6137 if (protocol == __constant_htons(ETH_P_IP)) {
6138 if ((hdr.network - data) > (max_len - sizeof(struct iphdr)))
6139 return max_len;
6140
6141 /* access ihl as a u8 to avoid unaligned access on ia64 */
6142 hlen = (hdr.network[0] & 0x0F) << 2;
6143
6144 /* verify hlen meets minimum size requirements */
6145 if (hlen < sizeof(struct iphdr))
6146 return hdr.network - data;
6147
6148 /* record next protocol */
6149 nexthdr = hdr.ipv4->protocol;
6150 hdr.network += hlen;
6151 } else if (protocol == __constant_htons(ETH_P_IPV6)) {
6152 if ((hdr.network - data) > (max_len - sizeof(struct ipv6hdr)))
6153 return max_len;
6154
6155 /* record next protocol */
6156 nexthdr = hdr.ipv6->nexthdr;
6157 hdr.network += sizeof(struct ipv6hdr);
6158 } else {
6159 return hdr.network - data;
6160 }
6161
6162 /* finally sort out TCP */
6163 if (nexthdr == IPPROTO_TCP) {
6164 if ((hdr.network - data) > (max_len - sizeof(struct tcphdr)))
6165 return max_len;
6166
6167 /* access doff as a u8 to avoid unaligned access on ia64 */
6168 hlen = (hdr.network[12] & 0xF0) >> 2;
6169
6170 /* verify hlen meets minimum size requirements */
6171 if (hlen < sizeof(struct tcphdr))
6172 return hdr.network - data;
6173
6174 hdr.network += hlen;
6175 } else if (nexthdr == IPPROTO_UDP) {
6176 if ((hdr.network - data) > (max_len - sizeof(struct udphdr)))
6177 return max_len;
6178
6179 hdr.network += sizeof(struct udphdr);
6180 }
6181
6182 /*
6183 * If everything has gone correctly hdr.network should be the
6184 * data section of the packet and will be the end of the header.
6185 * If not then it probably represents the end of the last recognized
6186 * header.
Alexander Duyck2d94d8a2009-07-23 18:10:06 +00006187 */
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006188 if ((hdr.network - data) < max_len)
6189 return hdr.network - data;
6190 else
6191 return max_len;
6192}
6193
6194/**
6195 * igb_pull_tail - igb specific version of skb_pull_tail
6196 * @rx_ring: rx descriptor ring packet is being transacted on
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006197 * @rx_desc: pointer to the EOP Rx descriptor
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006198 * @skb: pointer to current skb being adjusted
6199 *
6200 * This function is an igb specific version of __pskb_pull_tail. The
6201 * main difference between this version and the original function is that
6202 * this function can make several assumptions about the state of things
6203 * that allow for significant optimizations versus the standard function.
6204 * As a result we can do things like drop a frag and maintain an accurate
6205 * truesize for the skb.
6206 */
6207static void igb_pull_tail(struct igb_ring *rx_ring,
6208 union e1000_adv_rx_desc *rx_desc,
6209 struct sk_buff *skb)
6210{
6211 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[0];
6212 unsigned char *va;
6213 unsigned int pull_len;
6214
6215 /*
6216 * it is valid to use page_address instead of kmap since we are
6217 * working with pages allocated out of the lomem pool per
6218 * alloc_page(GFP_ATOMIC)
6219 */
6220 va = skb_frag_address(frag);
6221
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006222 if (igb_test_staterr(rx_desc, E1000_RXDADV_STAT_TSIP)) {
6223 /* retrieve timestamp from buffer */
6224 igb_ptp_rx_pktstamp(rx_ring->q_vector, va, skb);
6225
6226 /* update pointers to remove timestamp header */
6227 skb_frag_size_sub(frag, IGB_TS_HDR_LEN);
6228 frag->page_offset += IGB_TS_HDR_LEN;
6229 skb->data_len -= IGB_TS_HDR_LEN;
6230 skb->len -= IGB_TS_HDR_LEN;
6231
6232 /* move va to start of packet data */
6233 va += IGB_TS_HDR_LEN;
6234 }
6235
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006236 /*
6237 * we need the header to contain the greater of either ETH_HLEN or
6238 * 60 bytes if the skb->len is less than 60 for skb_pad.
6239 */
6240 pull_len = igb_get_headlen(va, IGB_RX_HDR_LEN);
6241
6242 /* align pull length to size of long to optimize memcpy performance */
6243 skb_copy_to_linear_data(skb, va, ALIGN(pull_len, sizeof(long)));
6244
6245 /* update all of the pointers */
6246 skb_frag_size_sub(frag, pull_len);
6247 frag->page_offset += pull_len;
6248 skb->data_len -= pull_len;
6249 skb->tail += pull_len;
6250}
6251
6252/**
6253 * igb_cleanup_headers - Correct corrupted or empty headers
6254 * @rx_ring: rx descriptor ring packet is being transacted on
6255 * @rx_desc: pointer to the EOP Rx descriptor
6256 * @skb: pointer to current skb being fixed
6257 *
6258 * Address the case where we are pulling data in on pages only
6259 * and as such no data is present in the skb header.
6260 *
6261 * In addition if skb is not at least 60 bytes we need to pad it so that
6262 * it is large enough to qualify as a valid Ethernet frame.
6263 *
6264 * Returns true if an error was encountered and skb was freed.
6265 **/
6266static bool igb_cleanup_headers(struct igb_ring *rx_ring,
6267 union e1000_adv_rx_desc *rx_desc,
6268 struct sk_buff *skb)
6269{
6270
6271 if (unlikely((igb_test_staterr(rx_desc,
6272 E1000_RXDEXT_ERR_FRAME_ERR_MASK)))) {
6273 struct net_device *netdev = rx_ring->netdev;
6274 if (!(netdev->features & NETIF_F_RXALL)) {
6275 dev_kfree_skb_any(skb);
6276 return true;
6277 }
6278 }
6279
6280 /* place header in linear portion of buffer */
6281 if (skb_is_nonlinear(skb))
6282 igb_pull_tail(rx_ring, rx_desc, skb);
6283
6284 /* if skb_pad returns an error the skb was freed */
6285 if (unlikely(skb->len < 60)) {
6286 int pad_len = 60 - skb->len;
6287
6288 if (skb_pad(skb, pad_len))
6289 return true;
6290 __skb_put(skb, pad_len);
6291 }
6292
6293 return false;
Alexander Duyck2d94d8a2009-07-23 18:10:06 +00006294}
6295
Alexander Duyckdb2ee5b2012-09-25 00:30:57 +00006296/**
6297 * igb_process_skb_fields - Populate skb header fields from Rx descriptor
6298 * @rx_ring: rx descriptor ring packet is being transacted on
6299 * @rx_desc: pointer to the EOP Rx descriptor
6300 * @skb: pointer to current skb being populated
6301 *
6302 * This function checks the ring, descriptor, and packet information in
6303 * order to populate the hash, checksum, VLAN, timestamp, protocol, and
6304 * other fields within the skb.
6305 **/
6306static void igb_process_skb_fields(struct igb_ring *rx_ring,
6307 union e1000_adv_rx_desc *rx_desc,
6308 struct sk_buff *skb)
6309{
6310 struct net_device *dev = rx_ring->netdev;
6311
6312 igb_rx_hash(rx_ring, rx_desc, skb);
6313
6314 igb_rx_checksum(rx_ring, rx_desc, skb);
6315
Alexander Duyckdb2ee5b2012-09-25 00:30:57 +00006316 igb_ptp_rx_hwtstamp(rx_ring->q_vector, rx_desc, skb);
Alexander Duyckdb2ee5b2012-09-25 00:30:57 +00006317
6318 if ((dev->features & NETIF_F_HW_VLAN_RX) &&
6319 igb_test_staterr(rx_desc, E1000_RXD_STAT_VP)) {
6320 u16 vid;
6321 if (igb_test_staterr(rx_desc, E1000_RXDEXT_STATERR_LB) &&
6322 test_bit(IGB_RING_FLAG_RX_LB_VLAN_BSWAP, &rx_ring->flags))
6323 vid = be16_to_cpu(rx_desc->wb.upper.vlan);
6324 else
6325 vid = le16_to_cpu(rx_desc->wb.upper.vlan);
6326
6327 __vlan_hwaccel_put_tag(skb, vid);
6328 }
6329
6330 skb_record_rx_queue(skb, rx_ring->queue_index);
6331
6332 skb->protocol = eth_type_trans(skb, rx_ring->netdev);
6333}
6334
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006335static bool igb_clean_rx_irq(struct igb_q_vector *q_vector, const int budget)
Auke Kok9d5c8242008-01-24 02:22:38 -08006336{
Alexander Duyck0ba82992011-08-26 07:45:47 +00006337 struct igb_ring *rx_ring = q_vector->rx.ring;
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006338 struct sk_buff *skb = rx_ring->skb;
Auke Kok9d5c8242008-01-24 02:22:38 -08006339 unsigned int total_bytes = 0, total_packets = 0;
Alexander Duyck16eb8812011-08-26 07:43:54 +00006340 u16 cleaned_count = igb_desc_unused(rx_ring);
Auke Kok9d5c8242008-01-24 02:22:38 -08006341
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006342 do {
6343 union e1000_adv_rx_desc *rx_desc;
Auke Kok9d5c8242008-01-24 02:22:38 -08006344
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006345 /* return some buffers to hardware, one at a time is too slow */
6346 if (cleaned_count >= IGB_RX_BUFFER_WRITE) {
6347 igb_alloc_rx_buffers(rx_ring, cleaned_count);
6348 cleaned_count = 0;
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07006349 }
6350
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006351 rx_desc = IGB_RX_DESC(rx_ring, rx_ring->next_to_clean);
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07006352
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006353 if (!igb_test_staterr(rx_desc, E1000_RXD_STAT_DD))
6354 break;
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07006355
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006356 /* retrieve a buffer from the ring */
6357 skb = igb_fetch_rx_buffer(rx_ring, rx_desc, skb);
Alexander Duyck16eb8812011-08-26 07:43:54 +00006358
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006359 /* exit if we failed to retrieve a buffer */
6360 if (!skb)
6361 break;
6362
6363 cleaned_count++;
6364
6365 /* fetch next buffer in frame if non-eop */
6366 if (igb_is_non_eop(rx_ring, rx_desc))
6367 continue;
Alexander Duyck44390ca2011-08-26 07:43:38 +00006368
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006369 /* verify the packet layout is correct */
6370 if (igb_cleanup_headers(rx_ring, rx_desc, skb)) {
6371 skb = NULL;
6372 continue;
Auke Kok9d5c8242008-01-24 02:22:38 -08006373 }
Auke Kok9d5c8242008-01-24 02:22:38 -08006374
Alexander Duyckdb2ee5b2012-09-25 00:30:57 +00006375 /* probably a little skewed due to removing CRC */
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00006376 total_bytes += skb->len;
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00006377
Alexander Duyckdb2ee5b2012-09-25 00:30:57 +00006378 /* populate checksum, timestamp, VLAN, and protocol */
6379 igb_process_skb_fields(rx_ring, rx_desc, skb);
Alexander Duyck3ceb90f2011-08-26 07:46:03 +00006380
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00006381 napi_gro_receive(&q_vector->napi, skb);
Auke Kok9d5c8242008-01-24 02:22:38 -08006382
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006383 /* reset skb pointer */
6384 skb = NULL;
6385
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006386 /* update budget accounting */
6387 total_packets++;
6388 } while (likely(total_packets < budget));
Alexander Duyckbf36c1a2008-07-08 15:11:40 -07006389
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006390 /* place incomplete frames back on ring for completion */
6391 rx_ring->skb = skb;
6392
Eric Dumazet12dcd862010-10-15 17:27:10 +00006393 u64_stats_update_begin(&rx_ring->rx_syncp);
Auke Kok9d5c8242008-01-24 02:22:38 -08006394 rx_ring->rx_stats.packets += total_packets;
6395 rx_ring->rx_stats.bytes += total_bytes;
Eric Dumazet12dcd862010-10-15 17:27:10 +00006396 u64_stats_update_end(&rx_ring->rx_syncp);
Alexander Duyck0ba82992011-08-26 07:45:47 +00006397 q_vector->rx.total_packets += total_packets;
6398 q_vector->rx.total_bytes += total_bytes;
Alexander Duyckc023cd82011-08-26 07:43:43 +00006399
6400 if (cleaned_count)
Alexander Duyckcd392f52011-08-26 07:43:59 +00006401 igb_alloc_rx_buffers(rx_ring, cleaned_count);
Alexander Duyckc023cd82011-08-26 07:43:43 +00006402
Alexander Duyck2e334ee2012-09-25 00:31:07 +00006403 return (total_packets < budget);
Auke Kok9d5c8242008-01-24 02:22:38 -08006404}
6405
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006406static bool igb_alloc_mapped_page(struct igb_ring *rx_ring,
6407 struct igb_rx_buffer *bi)
Alexander Duyckc023cd82011-08-26 07:43:43 +00006408{
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006409 struct page *page = bi->page;
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006410 dma_addr_t dma;
Alexander Duyckc023cd82011-08-26 07:43:43 +00006411
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006412 /* since we are recycling buffers we should seldom need to alloc */
6413 if (likely(page))
Alexander Duyckc023cd82011-08-26 07:43:43 +00006414 return true;
6415
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006416 /* alloc new page for storage */
6417 page = __skb_alloc_page(GFP_ATOMIC | __GFP_COLD, NULL);
6418 if (unlikely(!page)) {
6419 rx_ring->rx_stats.alloc_failed++;
6420 return false;
Alexander Duyckc023cd82011-08-26 07:43:43 +00006421 }
6422
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006423 /* map page for use */
6424 dma = dma_map_page(rx_ring->dev, page, 0, PAGE_SIZE, DMA_FROM_DEVICE);
Alexander Duyckc023cd82011-08-26 07:43:43 +00006425
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006426 /*
6427 * if mapping failed free memory back to system since
6428 * there isn't much point in holding memory we can't use
6429 */
Alexander Duyckc023cd82011-08-26 07:43:43 +00006430 if (dma_mapping_error(rx_ring->dev, dma)) {
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006431 __free_page(page);
6432
Alexander Duyckc023cd82011-08-26 07:43:43 +00006433 rx_ring->rx_stats.alloc_failed++;
6434 return false;
6435 }
6436
6437 bi->dma = dma;
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006438 bi->page = page;
6439 bi->page_offset = 0;
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006440
Alexander Duyckc023cd82011-08-26 07:43:43 +00006441 return true;
6442}
6443
Auke Kok9d5c8242008-01-24 02:22:38 -08006444/**
Alexander Duyckcd392f52011-08-26 07:43:59 +00006445 * igb_alloc_rx_buffers - Replace used receive buffers; packet split
Auke Kok9d5c8242008-01-24 02:22:38 -08006446 * @adapter: address of board private structure
6447 **/
Alexander Duyckcd392f52011-08-26 07:43:59 +00006448void igb_alloc_rx_buffers(struct igb_ring *rx_ring, u16 cleaned_count)
Auke Kok9d5c8242008-01-24 02:22:38 -08006449{
Auke Kok9d5c8242008-01-24 02:22:38 -08006450 union e1000_adv_rx_desc *rx_desc;
Alexander Duyck06034642011-08-26 07:44:22 +00006451 struct igb_rx_buffer *bi;
Alexander Duyckc023cd82011-08-26 07:43:43 +00006452 u16 i = rx_ring->next_to_use;
Auke Kok9d5c8242008-01-24 02:22:38 -08006453
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006454 /* nothing to do */
6455 if (!cleaned_count)
6456 return;
6457
Alexander Duyck601369062011-08-26 07:44:05 +00006458 rx_desc = IGB_RX_DESC(rx_ring, i);
Alexander Duyck06034642011-08-26 07:44:22 +00006459 bi = &rx_ring->rx_buffer_info[i];
Alexander Duyckc023cd82011-08-26 07:43:43 +00006460 i -= rx_ring->count;
Auke Kok9d5c8242008-01-24 02:22:38 -08006461
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006462 do {
Alexander Duyck1a1c2252012-09-25 00:30:52 +00006463 if (!igb_alloc_mapped_page(rx_ring, bi))
Alexander Duyckc023cd82011-08-26 07:43:43 +00006464 break;
Auke Kok9d5c8242008-01-24 02:22:38 -08006465
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006466 /*
6467 * Refresh the desc even if buffer_addrs didn't change
6468 * because each write-back erases this info.
6469 */
6470 rx_desc->read.pkt_addr = cpu_to_le64(bi->dma + bi->page_offset);
Auke Kok9d5c8242008-01-24 02:22:38 -08006471
Alexander Duyckc023cd82011-08-26 07:43:43 +00006472 rx_desc++;
6473 bi++;
Auke Kok9d5c8242008-01-24 02:22:38 -08006474 i++;
Alexander Duyckc023cd82011-08-26 07:43:43 +00006475 if (unlikely(!i)) {
Alexander Duyck601369062011-08-26 07:44:05 +00006476 rx_desc = IGB_RX_DESC(rx_ring, 0);
Alexander Duyck06034642011-08-26 07:44:22 +00006477 bi = rx_ring->rx_buffer_info;
Alexander Duyckc023cd82011-08-26 07:43:43 +00006478 i -= rx_ring->count;
6479 }
6480
6481 /* clear the hdr_addr for the next_to_use descriptor */
6482 rx_desc->read.hdr_addr = 0;
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006483
6484 cleaned_count--;
6485 } while (cleaned_count);
Auke Kok9d5c8242008-01-24 02:22:38 -08006486
Alexander Duyckc023cd82011-08-26 07:43:43 +00006487 i += rx_ring->count;
6488
Auke Kok9d5c8242008-01-24 02:22:38 -08006489 if (rx_ring->next_to_use != i) {
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006490 /* record the next descriptor to use */
Auke Kok9d5c8242008-01-24 02:22:38 -08006491 rx_ring->next_to_use = i;
Auke Kok9d5c8242008-01-24 02:22:38 -08006492
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006493 /* update next to alloc since we have filled the ring */
6494 rx_ring->next_to_alloc = i;
6495
6496 /*
6497 * Force memory writes to complete before letting h/w
Auke Kok9d5c8242008-01-24 02:22:38 -08006498 * know there are new descriptors to fetch. (Only
6499 * applicable for weak-ordered memory model archs,
Alexander Duyckcbc8e552012-09-25 00:31:02 +00006500 * such as IA-64).
6501 */
Auke Kok9d5c8242008-01-24 02:22:38 -08006502 wmb();
Alexander Duyckfce99e32009-10-27 15:51:27 +00006503 writel(i, rx_ring->tail);
Auke Kok9d5c8242008-01-24 02:22:38 -08006504 }
6505}
6506
6507/**
6508 * igb_mii_ioctl -
6509 * @netdev:
6510 * @ifreq:
6511 * @cmd:
6512 **/
6513static int igb_mii_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
6514{
6515 struct igb_adapter *adapter = netdev_priv(netdev);
6516 struct mii_ioctl_data *data = if_mii(ifr);
6517
6518 if (adapter->hw.phy.media_type != e1000_media_type_copper)
6519 return -EOPNOTSUPP;
6520
6521 switch (cmd) {
6522 case SIOCGMIIPHY:
6523 data->phy_id = adapter->hw.phy.addr;
6524 break;
6525 case SIOCGMIIREG:
Alexander Duyckf5f4cf02008-11-21 21:30:24 -08006526 if (igb_read_phy_reg(&adapter->hw, data->reg_num & 0x1F,
6527 &data->val_out))
Auke Kok9d5c8242008-01-24 02:22:38 -08006528 return -EIO;
6529 break;
6530 case SIOCSMIIREG:
6531 default:
6532 return -EOPNOTSUPP;
6533 }
6534 return 0;
6535}
6536
6537/**
6538 * igb_ioctl -
6539 * @netdev:
6540 * @ifreq:
6541 * @cmd:
6542 **/
6543static int igb_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
6544{
6545 switch (cmd) {
6546 case SIOCGMIIPHY:
6547 case SIOCGMIIREG:
6548 case SIOCSMIIREG:
6549 return igb_mii_ioctl(netdev, ifr, cmd);
Patrick Ohlyc6cb0902009-02-12 05:03:42 +00006550 case SIOCSHWTSTAMP:
Matthew Vicka79f4f82012-08-10 05:40:44 +00006551 return igb_ptp_hwtstamp_ioctl(netdev, ifr, cmd);
Auke Kok9d5c8242008-01-24 02:22:38 -08006552 default:
6553 return -EOPNOTSUPP;
6554 }
6555}
6556
Alexander Duyck009bc062009-07-23 18:08:35 +00006557s32 igb_read_pcie_cap_reg(struct e1000_hw *hw, u32 reg, u16 *value)
6558{
6559 struct igb_adapter *adapter = hw->back;
Alexander Duyck009bc062009-07-23 18:08:35 +00006560
Jiang Liu23d028c2012-08-20 13:32:20 -06006561 if (pcie_capability_read_word(adapter->pdev, reg, value))
Alexander Duyck009bc062009-07-23 18:08:35 +00006562 return -E1000_ERR_CONFIG;
6563
Alexander Duyck009bc062009-07-23 18:08:35 +00006564 return 0;
6565}
6566
6567s32 igb_write_pcie_cap_reg(struct e1000_hw *hw, u32 reg, u16 *value)
6568{
6569 struct igb_adapter *adapter = hw->back;
Alexander Duyck009bc062009-07-23 18:08:35 +00006570
Jiang Liu23d028c2012-08-20 13:32:20 -06006571 if (pcie_capability_write_word(adapter->pdev, reg, *value))
Alexander Duyck009bc062009-07-23 18:08:35 +00006572 return -E1000_ERR_CONFIG;
6573
Alexander Duyck009bc062009-07-23 18:08:35 +00006574 return 0;
6575}
6576
Michał Mirosławc8f44af2011-11-15 15:29:55 +00006577static void igb_vlan_mode(struct net_device *netdev, netdev_features_t features)
Auke Kok9d5c8242008-01-24 02:22:38 -08006578{
6579 struct igb_adapter *adapter = netdev_priv(netdev);
6580 struct e1000_hw *hw = &adapter->hw;
6581 u32 ctrl, rctl;
Alexander Duyck5faf0302011-08-26 07:46:08 +00006582 bool enable = !!(features & NETIF_F_HW_VLAN_RX);
Auke Kok9d5c8242008-01-24 02:22:38 -08006583
Alexander Duyck5faf0302011-08-26 07:46:08 +00006584 if (enable) {
Auke Kok9d5c8242008-01-24 02:22:38 -08006585 /* enable VLAN tag insert/strip */
6586 ctrl = rd32(E1000_CTRL);
6587 ctrl |= E1000_CTRL_VME;
6588 wr32(E1000_CTRL, ctrl);
6589
Alexander Duyck51466232009-10-27 23:47:35 +00006590 /* Disable CFI check */
Auke Kok9d5c8242008-01-24 02:22:38 -08006591 rctl = rd32(E1000_RCTL);
Auke Kok9d5c8242008-01-24 02:22:38 -08006592 rctl &= ~E1000_RCTL_CFIEN;
6593 wr32(E1000_RCTL, rctl);
Auke Kok9d5c8242008-01-24 02:22:38 -08006594 } else {
6595 /* disable VLAN tag insert/strip */
6596 ctrl = rd32(E1000_CTRL);
6597 ctrl &= ~E1000_CTRL_VME;
6598 wr32(E1000_CTRL, ctrl);
Auke Kok9d5c8242008-01-24 02:22:38 -08006599 }
6600
Alexander Duycke1739522009-02-19 20:39:44 -08006601 igb_rlpml_set(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08006602}
6603
Jiri Pirko8e586132011-12-08 19:52:37 -05006604static int igb_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
Auke Kok9d5c8242008-01-24 02:22:38 -08006605{
6606 struct igb_adapter *adapter = netdev_priv(netdev);
6607 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006608 int pf_id = adapter->vfs_allocated_count;
Auke Kok9d5c8242008-01-24 02:22:38 -08006609
Alexander Duyck51466232009-10-27 23:47:35 +00006610 /* attempt to add filter to vlvf array */
6611 igb_vlvf_set(adapter, vid, true, pf_id);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006612
Alexander Duyck51466232009-10-27 23:47:35 +00006613 /* add the filter since PF can receive vlans w/o entry in vlvf */
6614 igb_vfta_set(hw, vid, true);
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00006615
6616 set_bit(vid, adapter->active_vlans);
Jiri Pirko8e586132011-12-08 19:52:37 -05006617
6618 return 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08006619}
6620
Jiri Pirko8e586132011-12-08 19:52:37 -05006621static int igb_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
Auke Kok9d5c8242008-01-24 02:22:38 -08006622{
6623 struct igb_adapter *adapter = netdev_priv(netdev);
6624 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006625 int pf_id = adapter->vfs_allocated_count;
Alexander Duyck51466232009-10-27 23:47:35 +00006626 s32 err;
Auke Kok9d5c8242008-01-24 02:22:38 -08006627
Alexander Duyck51466232009-10-27 23:47:35 +00006628 /* remove vlan from VLVF table array */
6629 err = igb_vlvf_set(adapter, vid, false, pf_id);
Auke Kok9d5c8242008-01-24 02:22:38 -08006630
Alexander Duyck51466232009-10-27 23:47:35 +00006631 /* if vid was not present in VLVF just remove it from table */
6632 if (err)
Alexander Duyck4ae196d2009-02-19 20:40:07 -08006633 igb_vfta_set(hw, vid, false);
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00006634
6635 clear_bit(vid, adapter->active_vlans);
Jiri Pirko8e586132011-12-08 19:52:37 -05006636
6637 return 0;
Auke Kok9d5c8242008-01-24 02:22:38 -08006638}
6639
6640static void igb_restore_vlan(struct igb_adapter *adapter)
6641{
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00006642 u16 vid;
Auke Kok9d5c8242008-01-24 02:22:38 -08006643
Alexander Duyck5faf0302011-08-26 07:46:08 +00006644 igb_vlan_mode(adapter->netdev, adapter->netdev->features);
6645
Jiri Pirkob2cb09b2011-07-21 03:27:27 +00006646 for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
6647 igb_vlan_rx_add_vid(adapter->netdev, vid);
Auke Kok9d5c8242008-01-24 02:22:38 -08006648}
6649
David Decotigny14ad2512011-04-27 18:32:43 +00006650int igb_set_spd_dplx(struct igb_adapter *adapter, u32 spd, u8 dplx)
Auke Kok9d5c8242008-01-24 02:22:38 -08006651{
Alexander Duyck090b1792009-10-27 23:51:55 +00006652 struct pci_dev *pdev = adapter->pdev;
Auke Kok9d5c8242008-01-24 02:22:38 -08006653 struct e1000_mac_info *mac = &adapter->hw.mac;
6654
6655 mac->autoneg = 0;
6656
David Decotigny14ad2512011-04-27 18:32:43 +00006657 /* Make sure dplx is at most 1 bit and lsb of speed is not set
6658 * for the switch() below to work */
6659 if ((spd & 1) || (dplx & ~1))
6660 goto err_inval;
6661
Carolyn Wybornycd2638a2010-10-12 22:27:02 +00006662 /* Fiber NIC's only allow 1000 Gbps Full duplex */
6663 if ((adapter->hw.phy.media_type == e1000_media_type_internal_serdes) &&
David Decotigny14ad2512011-04-27 18:32:43 +00006664 spd != SPEED_1000 &&
6665 dplx != DUPLEX_FULL)
6666 goto err_inval;
Carolyn Wybornycd2638a2010-10-12 22:27:02 +00006667
David Decotigny14ad2512011-04-27 18:32:43 +00006668 switch (spd + dplx) {
Auke Kok9d5c8242008-01-24 02:22:38 -08006669 case SPEED_10 + DUPLEX_HALF:
6670 mac->forced_speed_duplex = ADVERTISE_10_HALF;
6671 break;
6672 case SPEED_10 + DUPLEX_FULL:
6673 mac->forced_speed_duplex = ADVERTISE_10_FULL;
6674 break;
6675 case SPEED_100 + DUPLEX_HALF:
6676 mac->forced_speed_duplex = ADVERTISE_100_HALF;
6677 break;
6678 case SPEED_100 + DUPLEX_FULL:
6679 mac->forced_speed_duplex = ADVERTISE_100_FULL;
6680 break;
6681 case SPEED_1000 + DUPLEX_FULL:
6682 mac->autoneg = 1;
6683 adapter->hw.phy.autoneg_advertised = ADVERTISE_1000_FULL;
6684 break;
6685 case SPEED_1000 + DUPLEX_HALF: /* not supported */
6686 default:
David Decotigny14ad2512011-04-27 18:32:43 +00006687 goto err_inval;
Auke Kok9d5c8242008-01-24 02:22:38 -08006688 }
Jesse Brandeburg8376dad2012-07-26 02:31:19 +00006689
6690 /* clear MDI, MDI(-X) override is only allowed when autoneg enabled */
6691 adapter->hw.phy.mdix = AUTO_ALL_MODES;
6692
Auke Kok9d5c8242008-01-24 02:22:38 -08006693 return 0;
David Decotigny14ad2512011-04-27 18:32:43 +00006694
6695err_inval:
6696 dev_err(&pdev->dev, "Unsupported Speed/Duplex configuration\n");
6697 return -EINVAL;
Auke Kok9d5c8242008-01-24 02:22:38 -08006698}
6699
Yan, Zheng749ab2c2012-01-04 20:23:37 +00006700static int __igb_shutdown(struct pci_dev *pdev, bool *enable_wake,
6701 bool runtime)
Auke Kok9d5c8242008-01-24 02:22:38 -08006702{
6703 struct net_device *netdev = pci_get_drvdata(pdev);
6704 struct igb_adapter *adapter = netdev_priv(netdev);
6705 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck2d064c02008-07-08 15:10:12 -07006706 u32 ctrl, rctl, status;
Yan, Zheng749ab2c2012-01-04 20:23:37 +00006707 u32 wufc = runtime ? E1000_WUFC_LNKC : adapter->wol;
Auke Kok9d5c8242008-01-24 02:22:38 -08006708#ifdef CONFIG_PM
6709 int retval = 0;
6710#endif
6711
6712 netif_device_detach(netdev);
6713
Alexander Duycka88f10e2008-07-08 15:13:38 -07006714 if (netif_running(netdev))
Yan, Zheng749ab2c2012-01-04 20:23:37 +00006715 __igb_close(netdev, true);
Alexander Duycka88f10e2008-07-08 15:13:38 -07006716
Alexander Duyck047e0032009-10-27 15:49:27 +00006717 igb_clear_interrupt_scheme(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08006718
6719#ifdef CONFIG_PM
6720 retval = pci_save_state(pdev);
6721 if (retval)
6722 return retval;
6723#endif
6724
6725 status = rd32(E1000_STATUS);
6726 if (status & E1000_STATUS_LU)
6727 wufc &= ~E1000_WUFC_LNKC;
6728
6729 if (wufc) {
6730 igb_setup_rctl(adapter);
Alexander Duyckff41f8d2009-09-03 14:48:56 +00006731 igb_set_rx_mode(netdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08006732
6733 /* turn on all-multi mode if wake on multicast is enabled */
6734 if (wufc & E1000_WUFC_MC) {
6735 rctl = rd32(E1000_RCTL);
6736 rctl |= E1000_RCTL_MPE;
6737 wr32(E1000_RCTL, rctl);
6738 }
6739
6740 ctrl = rd32(E1000_CTRL);
6741 /* advertise wake from D3Cold */
6742 #define E1000_CTRL_ADVD3WUC 0x00100000
6743 /* phy power management enable */
6744 #define E1000_CTRL_EN_PHY_PWR_MGMT 0x00200000
6745 ctrl |= E1000_CTRL_ADVD3WUC;
6746 wr32(E1000_CTRL, ctrl);
6747
Auke Kok9d5c8242008-01-24 02:22:38 -08006748 /* Allow time for pending master requests to run */
Alexander Duyck330a6d62009-10-27 23:51:35 +00006749 igb_disable_pcie_master(hw);
Auke Kok9d5c8242008-01-24 02:22:38 -08006750
6751 wr32(E1000_WUC, E1000_WUC_PME_EN);
6752 wr32(E1000_WUFC, wufc);
Auke Kok9d5c8242008-01-24 02:22:38 -08006753 } else {
6754 wr32(E1000_WUC, 0);
6755 wr32(E1000_WUFC, 0);
Auke Kok9d5c8242008-01-24 02:22:38 -08006756 }
6757
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00006758 *enable_wake = wufc || adapter->en_mng_pt;
6759 if (!*enable_wake)
Nick Nunley88a268c2010-02-17 01:01:59 +00006760 igb_power_down_link(adapter);
6761 else
6762 igb_power_up_link(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08006763
6764 /* Release control of h/w to f/w. If f/w is AMT enabled, this
6765 * would have already happened in close and is redundant. */
6766 igb_release_hw_control(adapter);
6767
6768 pci_disable_device(pdev);
6769
Auke Kok9d5c8242008-01-24 02:22:38 -08006770 return 0;
6771}
6772
6773#ifdef CONFIG_PM
Emil Tantilovd9dd9662012-01-28 08:10:35 +00006774#ifdef CONFIG_PM_SLEEP
Yan, Zheng749ab2c2012-01-04 20:23:37 +00006775static int igb_suspend(struct device *dev)
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00006776{
6777 int retval;
6778 bool wake;
Yan, Zheng749ab2c2012-01-04 20:23:37 +00006779 struct pci_dev *pdev = to_pci_dev(dev);
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00006780
Yan, Zheng749ab2c2012-01-04 20:23:37 +00006781 retval = __igb_shutdown(pdev, &wake, 0);
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00006782 if (retval)
6783 return retval;
6784
6785 if (wake) {
6786 pci_prepare_to_sleep(pdev);
6787 } else {
6788 pci_wake_from_d3(pdev, false);
6789 pci_set_power_state(pdev, PCI_D3hot);
6790 }
6791
6792 return 0;
6793}
Emil Tantilovd9dd9662012-01-28 08:10:35 +00006794#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00006795
Yan, Zheng749ab2c2012-01-04 20:23:37 +00006796static int igb_resume(struct device *dev)
Auke Kok9d5c8242008-01-24 02:22:38 -08006797{
Yan, Zheng749ab2c2012-01-04 20:23:37 +00006798 struct pci_dev *pdev = to_pci_dev(dev);
Auke Kok9d5c8242008-01-24 02:22:38 -08006799 struct net_device *netdev = pci_get_drvdata(pdev);
6800 struct igb_adapter *adapter = netdev_priv(netdev);
6801 struct e1000_hw *hw = &adapter->hw;
6802 u32 err;
6803
6804 pci_set_power_state(pdev, PCI_D0);
6805 pci_restore_state(pdev);
Nick Nunleyb94f2d72010-02-17 01:02:19 +00006806 pci_save_state(pdev);
Taku Izumi42bfd33a2008-06-20 12:10:30 +09006807
Alexander Duyckaed5dec2009-02-06 23:16:04 +00006808 err = pci_enable_device_mem(pdev);
Auke Kok9d5c8242008-01-24 02:22:38 -08006809 if (err) {
6810 dev_err(&pdev->dev,
6811 "igb: Cannot enable PCI device from suspend\n");
6812 return err;
6813 }
6814 pci_set_master(pdev);
6815
6816 pci_enable_wake(pdev, PCI_D3hot, 0);
6817 pci_enable_wake(pdev, PCI_D3cold, 0);
6818
Benjamin Poiriercfb8c3a2012-05-10 15:38:37 +00006819 if (igb_init_interrupt_scheme(adapter)) {
Alexander Duycka88f10e2008-07-08 15:13:38 -07006820 dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
6821 return -ENOMEM;
Auke Kok9d5c8242008-01-24 02:22:38 -08006822 }
6823
Auke Kok9d5c8242008-01-24 02:22:38 -08006824 igb_reset(adapter);
Alexander Duycka8564f02009-02-06 23:21:10 +00006825
6826 /* let the f/w know that the h/w is now under the control of the
6827 * driver. */
6828 igb_get_hw_control(adapter);
6829
Auke Kok9d5c8242008-01-24 02:22:38 -08006830 wr32(E1000_WUS, ~0);
6831
Yan, Zheng749ab2c2012-01-04 20:23:37 +00006832 if (netdev->flags & IFF_UP) {
Alexander Duyck0c2cc022012-09-25 00:31:22 +00006833 rtnl_lock();
Yan, Zheng749ab2c2012-01-04 20:23:37 +00006834 err = __igb_open(netdev, true);
Alexander Duyck0c2cc022012-09-25 00:31:22 +00006835 rtnl_unlock();
Alexander Duycka88f10e2008-07-08 15:13:38 -07006836 if (err)
6837 return err;
6838 }
Auke Kok9d5c8242008-01-24 02:22:38 -08006839
6840 netif_device_attach(netdev);
Yan, Zheng749ab2c2012-01-04 20:23:37 +00006841 return 0;
6842}
6843
6844#ifdef CONFIG_PM_RUNTIME
6845static int igb_runtime_idle(struct device *dev)
6846{
6847 struct pci_dev *pdev = to_pci_dev(dev);
6848 struct net_device *netdev = pci_get_drvdata(pdev);
6849 struct igb_adapter *adapter = netdev_priv(netdev);
6850
6851 if (!igb_has_link(adapter))
6852 pm_schedule_suspend(dev, MSEC_PER_SEC * 5);
6853
6854 return -EBUSY;
6855}
6856
6857static int igb_runtime_suspend(struct device *dev)
6858{
6859 struct pci_dev *pdev = to_pci_dev(dev);
6860 int retval;
6861 bool wake;
6862
6863 retval = __igb_shutdown(pdev, &wake, 1);
6864 if (retval)
6865 return retval;
6866
6867 if (wake) {
6868 pci_prepare_to_sleep(pdev);
6869 } else {
6870 pci_wake_from_d3(pdev, false);
6871 pci_set_power_state(pdev, PCI_D3hot);
6872 }
Auke Kok9d5c8242008-01-24 02:22:38 -08006873
Auke Kok9d5c8242008-01-24 02:22:38 -08006874 return 0;
6875}
Yan, Zheng749ab2c2012-01-04 20:23:37 +00006876
6877static int igb_runtime_resume(struct device *dev)
6878{
6879 return igb_resume(dev);
6880}
6881#endif /* CONFIG_PM_RUNTIME */
Auke Kok9d5c8242008-01-24 02:22:38 -08006882#endif
6883
6884static void igb_shutdown(struct pci_dev *pdev)
6885{
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00006886 bool wake;
6887
Yan, Zheng749ab2c2012-01-04 20:23:37 +00006888 __igb_shutdown(pdev, &wake, 0);
Rafael J. Wysocki3fe7c4c2009-03-31 21:23:50 +00006889
6890 if (system_state == SYSTEM_POWER_OFF) {
6891 pci_wake_from_d3(pdev, wake);
6892 pci_set_power_state(pdev, PCI_D3hot);
6893 }
Auke Kok9d5c8242008-01-24 02:22:38 -08006894}
6895
6896#ifdef CONFIG_NET_POLL_CONTROLLER
6897/*
6898 * Polling 'interrupt' - used by things like netconsole to send skbs
6899 * without having to re-enable interrupts. It's not called while
6900 * the interrupt routine is executing.
6901 */
6902static void igb_netpoll(struct net_device *netdev)
6903{
6904 struct igb_adapter *adapter = netdev_priv(netdev);
Alexander Duyckeebbbdb2009-02-06 23:19:29 +00006905 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck0d1ae7f2011-08-26 07:46:34 +00006906 struct igb_q_vector *q_vector;
Auke Kok9d5c8242008-01-24 02:22:38 -08006907 int i;
Auke Kok9d5c8242008-01-24 02:22:38 -08006908
Alexander Duyck047e0032009-10-27 15:49:27 +00006909 for (i = 0; i < adapter->num_q_vectors; i++) {
Alexander Duyck0d1ae7f2011-08-26 07:46:34 +00006910 q_vector = adapter->q_vector[i];
6911 if (adapter->msix_entries)
6912 wr32(E1000_EIMC, q_vector->eims_value);
6913 else
6914 igb_irq_disable(adapter);
Alexander Duyck047e0032009-10-27 15:49:27 +00006915 napi_schedule(&q_vector->napi);
Alexander Duyckeebbbdb2009-02-06 23:19:29 +00006916 }
Auke Kok9d5c8242008-01-24 02:22:38 -08006917}
6918#endif /* CONFIG_NET_POLL_CONTROLLER */
6919
6920/**
6921 * igb_io_error_detected - called when PCI error is detected
6922 * @pdev: Pointer to PCI device
6923 * @state: The current pci connection state
6924 *
6925 * This function is called after a PCI bus error affecting
6926 * this device has been detected.
6927 */
6928static pci_ers_result_t igb_io_error_detected(struct pci_dev *pdev,
6929 pci_channel_state_t state)
6930{
6931 struct net_device *netdev = pci_get_drvdata(pdev);
6932 struct igb_adapter *adapter = netdev_priv(netdev);
6933
6934 netif_device_detach(netdev);
6935
Alexander Duyck59ed6ee2009-06-30 12:46:34 +00006936 if (state == pci_channel_io_perm_failure)
6937 return PCI_ERS_RESULT_DISCONNECT;
6938
Auke Kok9d5c8242008-01-24 02:22:38 -08006939 if (netif_running(netdev))
6940 igb_down(adapter);
6941 pci_disable_device(pdev);
6942
6943 /* Request a slot slot reset. */
6944 return PCI_ERS_RESULT_NEED_RESET;
6945}
6946
6947/**
6948 * igb_io_slot_reset - called after the pci bus has been reset.
6949 * @pdev: Pointer to PCI device
6950 *
6951 * Restart the card from scratch, as if from a cold-boot. Implementation
6952 * resembles the first-half of the igb_resume routine.
6953 */
6954static pci_ers_result_t igb_io_slot_reset(struct pci_dev *pdev)
6955{
6956 struct net_device *netdev = pci_get_drvdata(pdev);
6957 struct igb_adapter *adapter = netdev_priv(netdev);
6958 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck40a914f2008-11-27 00:24:37 -08006959 pci_ers_result_t result;
Taku Izumi42bfd33a2008-06-20 12:10:30 +09006960 int err;
Auke Kok9d5c8242008-01-24 02:22:38 -08006961
Alexander Duyckaed5dec2009-02-06 23:16:04 +00006962 if (pci_enable_device_mem(pdev)) {
Auke Kok9d5c8242008-01-24 02:22:38 -08006963 dev_err(&pdev->dev,
6964 "Cannot re-enable PCI device after reset.\n");
Alexander Duyck40a914f2008-11-27 00:24:37 -08006965 result = PCI_ERS_RESULT_DISCONNECT;
6966 } else {
6967 pci_set_master(pdev);
6968 pci_restore_state(pdev);
Nick Nunleyb94f2d72010-02-17 01:02:19 +00006969 pci_save_state(pdev);
Alexander Duyck40a914f2008-11-27 00:24:37 -08006970
6971 pci_enable_wake(pdev, PCI_D3hot, 0);
6972 pci_enable_wake(pdev, PCI_D3cold, 0);
6973
6974 igb_reset(adapter);
6975 wr32(E1000_WUS, ~0);
6976 result = PCI_ERS_RESULT_RECOVERED;
Auke Kok9d5c8242008-01-24 02:22:38 -08006977 }
Auke Kok9d5c8242008-01-24 02:22:38 -08006978
Jeff Kirsherea943d42008-12-11 20:34:19 -08006979 err = pci_cleanup_aer_uncorrect_error_status(pdev);
6980 if (err) {
6981 dev_err(&pdev->dev, "pci_cleanup_aer_uncorrect_error_status "
6982 "failed 0x%0x\n", err);
6983 /* non-fatal, continue */
6984 }
Auke Kok9d5c8242008-01-24 02:22:38 -08006985
Alexander Duyck40a914f2008-11-27 00:24:37 -08006986 return result;
Auke Kok9d5c8242008-01-24 02:22:38 -08006987}
6988
6989/**
6990 * igb_io_resume - called when traffic can start flowing again.
6991 * @pdev: Pointer to PCI device
6992 *
6993 * This callback is called when the error recovery driver tells us that
6994 * its OK to resume normal operation. Implementation resembles the
6995 * second-half of the igb_resume routine.
6996 */
6997static void igb_io_resume(struct pci_dev *pdev)
6998{
6999 struct net_device *netdev = pci_get_drvdata(pdev);
7000 struct igb_adapter *adapter = netdev_priv(netdev);
7001
Auke Kok9d5c8242008-01-24 02:22:38 -08007002 if (netif_running(netdev)) {
7003 if (igb_up(adapter)) {
7004 dev_err(&pdev->dev, "igb_up failed after reset\n");
7005 return;
7006 }
7007 }
7008
7009 netif_device_attach(netdev);
7010
7011 /* let the f/w know that the h/w is now under the control of the
7012 * driver. */
7013 igb_get_hw_control(adapter);
Auke Kok9d5c8242008-01-24 02:22:38 -08007014}
7015
Alexander Duyck26ad9172009-10-05 06:32:49 +00007016static void igb_rar_set_qsel(struct igb_adapter *adapter, u8 *addr, u32 index,
7017 u8 qsel)
7018{
7019 u32 rar_low, rar_high;
7020 struct e1000_hw *hw = &adapter->hw;
7021
7022 /* HW expects these in little endian so we reverse the byte order
7023 * from network order (big endian) to little endian
7024 */
7025 rar_low = ((u32) addr[0] | ((u32) addr[1] << 8) |
7026 ((u32) addr[2] << 16) | ((u32) addr[3] << 24));
7027 rar_high = ((u32) addr[4] | ((u32) addr[5] << 8));
7028
7029 /* Indicate to hardware the Address is Valid. */
7030 rar_high |= E1000_RAH_AV;
7031
7032 if (hw->mac.type == e1000_82575)
7033 rar_high |= E1000_RAH_POOL_1 * qsel;
7034 else
7035 rar_high |= E1000_RAH_POOL_1 << qsel;
7036
7037 wr32(E1000_RAL(index), rar_low);
7038 wrfl();
7039 wr32(E1000_RAH(index), rar_high);
7040 wrfl();
7041}
7042
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007043static int igb_set_vf_mac(struct igb_adapter *adapter,
7044 int vf, unsigned char *mac_addr)
7045{
7046 struct e1000_hw *hw = &adapter->hw;
Alexander Duyckff41f8d2009-09-03 14:48:56 +00007047 /* VF MAC addresses start at end of receive addresses and moves
7048 * torwards the first, as a result a collision should not be possible */
7049 int rar_entry = hw->mac.rar_entry_count - (vf + 1);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007050
Alexander Duyck37680112009-02-19 20:40:30 -08007051 memcpy(adapter->vf_data[vf].vf_mac_addresses, mac_addr, ETH_ALEN);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007052
Alexander Duyck26ad9172009-10-05 06:32:49 +00007053 igb_rar_set_qsel(adapter, mac_addr, rar_entry, vf);
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007054
7055 return 0;
7056}
7057
Williams, Mitch A8151d292010-02-10 01:44:24 +00007058static int igb_ndo_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
7059{
7060 struct igb_adapter *adapter = netdev_priv(netdev);
7061 if (!is_valid_ether_addr(mac) || (vf >= adapter->vfs_allocated_count))
7062 return -EINVAL;
7063 adapter->vf_data[vf].flags |= IGB_VF_FLAG_PF_SET_MAC;
7064 dev_info(&adapter->pdev->dev, "setting MAC %pM on VF %d\n", mac, vf);
7065 dev_info(&adapter->pdev->dev, "Reload the VF driver to make this"
7066 " change effective.");
7067 if (test_bit(__IGB_DOWN, &adapter->state)) {
7068 dev_warn(&adapter->pdev->dev, "The VF MAC address has been set,"
7069 " but the PF device is not up.\n");
7070 dev_warn(&adapter->pdev->dev, "Bring the PF device up before"
7071 " attempting to use the VF device.\n");
7072 }
7073 return igb_set_vf_mac(adapter, vf, mac);
7074}
7075
Lior Levy17dc5662011-02-08 02:28:46 +00007076static int igb_link_mbps(int internal_link_speed)
7077{
7078 switch (internal_link_speed) {
7079 case SPEED_100:
7080 return 100;
7081 case SPEED_1000:
7082 return 1000;
7083 default:
7084 return 0;
7085 }
7086}
7087
7088static void igb_set_vf_rate_limit(struct e1000_hw *hw, int vf, int tx_rate,
7089 int link_speed)
7090{
7091 int rf_dec, rf_int;
7092 u32 bcnrc_val;
7093
7094 if (tx_rate != 0) {
7095 /* Calculate the rate factor values to set */
7096 rf_int = link_speed / tx_rate;
7097 rf_dec = (link_speed - (rf_int * tx_rate));
7098 rf_dec = (rf_dec * (1<<E1000_RTTBCNRC_RF_INT_SHIFT)) / tx_rate;
7099
7100 bcnrc_val = E1000_RTTBCNRC_RS_ENA;
7101 bcnrc_val |= ((rf_int<<E1000_RTTBCNRC_RF_INT_SHIFT) &
7102 E1000_RTTBCNRC_RF_INT_MASK);
7103 bcnrc_val |= (rf_dec & E1000_RTTBCNRC_RF_DEC_MASK);
7104 } else {
7105 bcnrc_val = 0;
7106 }
7107
7108 wr32(E1000_RTTDQSEL, vf); /* vf X uses queue X */
Lior Levyf00b0da2011-06-04 06:05:03 +00007109 /*
7110 * Set global transmit compensation time to the MMW_SIZE in RTTBCNRM
7111 * register. MMW_SIZE=0x014 if 9728-byte jumbo is supported.
7112 */
7113 wr32(E1000_RTTBCNRM, 0x14);
Lior Levy17dc5662011-02-08 02:28:46 +00007114 wr32(E1000_RTTBCNRC, bcnrc_val);
7115}
7116
7117static void igb_check_vf_rate_limit(struct igb_adapter *adapter)
7118{
7119 int actual_link_speed, i;
7120 bool reset_rate = false;
7121
7122 /* VF TX rate limit was not set or not supported */
7123 if ((adapter->vf_rate_link_speed == 0) ||
7124 (adapter->hw.mac.type != e1000_82576))
7125 return;
7126
7127 actual_link_speed = igb_link_mbps(adapter->link_speed);
7128 if (actual_link_speed != adapter->vf_rate_link_speed) {
7129 reset_rate = true;
7130 adapter->vf_rate_link_speed = 0;
7131 dev_info(&adapter->pdev->dev,
7132 "Link speed has been changed. VF Transmit "
7133 "rate is disabled\n");
7134 }
7135
7136 for (i = 0; i < adapter->vfs_allocated_count; i++) {
7137 if (reset_rate)
7138 adapter->vf_data[i].tx_rate = 0;
7139
7140 igb_set_vf_rate_limit(&adapter->hw, i,
7141 adapter->vf_data[i].tx_rate,
7142 actual_link_speed);
7143 }
7144}
7145
Williams, Mitch A8151d292010-02-10 01:44:24 +00007146static int igb_ndo_set_vf_bw(struct net_device *netdev, int vf, int tx_rate)
7147{
Lior Levy17dc5662011-02-08 02:28:46 +00007148 struct igb_adapter *adapter = netdev_priv(netdev);
7149 struct e1000_hw *hw = &adapter->hw;
7150 int actual_link_speed;
7151
7152 if (hw->mac.type != e1000_82576)
7153 return -EOPNOTSUPP;
7154
7155 actual_link_speed = igb_link_mbps(adapter->link_speed);
7156 if ((vf >= adapter->vfs_allocated_count) ||
7157 (!(rd32(E1000_STATUS) & E1000_STATUS_LU)) ||
7158 (tx_rate < 0) || (tx_rate > actual_link_speed))
7159 return -EINVAL;
7160
7161 adapter->vf_rate_link_speed = actual_link_speed;
7162 adapter->vf_data[vf].tx_rate = (u16)tx_rate;
7163 igb_set_vf_rate_limit(hw, vf, tx_rate, actual_link_speed);
7164
7165 return 0;
Williams, Mitch A8151d292010-02-10 01:44:24 +00007166}
7167
7168static int igb_ndo_get_vf_config(struct net_device *netdev,
7169 int vf, struct ifla_vf_info *ivi)
7170{
7171 struct igb_adapter *adapter = netdev_priv(netdev);
7172 if (vf >= adapter->vfs_allocated_count)
7173 return -EINVAL;
7174 ivi->vf = vf;
7175 memcpy(&ivi->mac, adapter->vf_data[vf].vf_mac_addresses, ETH_ALEN);
Lior Levy17dc5662011-02-08 02:28:46 +00007176 ivi->tx_rate = adapter->vf_data[vf].tx_rate;
Williams, Mitch A8151d292010-02-10 01:44:24 +00007177 ivi->vlan = adapter->vf_data[vf].pf_vlan;
7178 ivi->qos = adapter->vf_data[vf].pf_qos;
7179 return 0;
7180}
7181
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007182static void igb_vmm_control(struct igb_adapter *adapter)
7183{
7184 struct e1000_hw *hw = &adapter->hw;
Alexander Duyck10d8e902009-10-27 15:54:04 +00007185 u32 reg;
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007186
Alexander Duyck52a1dd42010-03-22 14:07:46 +00007187 switch (hw->mac.type) {
7188 case e1000_82575:
Carolyn Wybornyf96a8a02012-04-06 23:25:19 +00007189 case e1000_i210:
7190 case e1000_i211:
Alexander Duyck52a1dd42010-03-22 14:07:46 +00007191 default:
7192 /* replication is not supported for 82575 */
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007193 return;
Alexander Duyck52a1dd42010-03-22 14:07:46 +00007194 case e1000_82576:
7195 /* notify HW that the MAC is adding vlan tags */
7196 reg = rd32(E1000_DTXCTL);
7197 reg |= E1000_DTXCTL_VLAN_ADDED;
7198 wr32(E1000_DTXCTL, reg);
7199 case e1000_82580:
7200 /* enable replication vlan tag stripping */
7201 reg = rd32(E1000_RPLOLR);
7202 reg |= E1000_RPLOLR_STRVLAN;
7203 wr32(E1000_RPLOLR, reg);
Alexander Duyckd2ba2ed2010-03-22 14:08:06 +00007204 case e1000_i350:
7205 /* none of the above registers are supported by i350 */
Alexander Duyck52a1dd42010-03-22 14:07:46 +00007206 break;
7207 }
Alexander Duyck10d8e902009-10-27 15:54:04 +00007208
Alexander Duyckd4960302009-10-27 15:53:45 +00007209 if (adapter->vfs_allocated_count) {
7210 igb_vmdq_set_loopback_pf(hw, true);
7211 igb_vmdq_set_replication_pf(hw, true);
Greg Rose13800462010-11-06 02:08:26 +00007212 igb_vmdq_set_anti_spoofing_pf(hw, true,
7213 adapter->vfs_allocated_count);
Alexander Duyckd4960302009-10-27 15:53:45 +00007214 } else {
7215 igb_vmdq_set_loopback_pf(hw, false);
7216 igb_vmdq_set_replication_pf(hw, false);
7217 }
Alexander Duyck4ae196d2009-02-19 20:40:07 -08007218}
7219
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +00007220static void igb_init_dmac(struct igb_adapter *adapter, u32 pba)
7221{
7222 struct e1000_hw *hw = &adapter->hw;
7223 u32 dmac_thr;
7224 u16 hwm;
7225
7226 if (hw->mac.type > e1000_82580) {
7227 if (adapter->flags & IGB_FLAG_DMAC) {
7228 u32 reg;
7229
7230 /* force threshold to 0. */
7231 wr32(E1000_DMCTXTH, 0);
7232
7233 /*
Matthew Vicke8c626e2011-11-17 08:33:12 +00007234 * DMA Coalescing high water mark needs to be greater
7235 * than the Rx threshold. Set hwm to PBA - max frame
7236 * size in 16B units, capping it at PBA - 6KB.
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +00007237 */
Matthew Vicke8c626e2011-11-17 08:33:12 +00007238 hwm = 64 * pba - adapter->max_frame_size / 16;
7239 if (hwm < 64 * (pba - 6))
7240 hwm = 64 * (pba - 6);
7241 reg = rd32(E1000_FCRTC);
7242 reg &= ~E1000_FCRTC_RTH_COAL_MASK;
7243 reg |= ((hwm << E1000_FCRTC_RTH_COAL_SHIFT)
7244 & E1000_FCRTC_RTH_COAL_MASK);
7245 wr32(E1000_FCRTC, reg);
7246
7247 /*
7248 * Set the DMA Coalescing Rx threshold to PBA - 2 * max
7249 * frame size, capping it at PBA - 10KB.
7250 */
7251 dmac_thr = pba - adapter->max_frame_size / 512;
7252 if (dmac_thr < pba - 10)
7253 dmac_thr = pba - 10;
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +00007254 reg = rd32(E1000_DMACR);
7255 reg &= ~E1000_DMACR_DMACTHR_MASK;
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +00007256 reg |= ((dmac_thr << E1000_DMACR_DMACTHR_SHIFT)
7257 & E1000_DMACR_DMACTHR_MASK);
7258
7259 /* transition to L0x or L1 if available..*/
7260 reg |= (E1000_DMACR_DMAC_EN | E1000_DMACR_DMAC_LX_MASK);
7261
7262 /* watchdog timer= +-1000 usec in 32usec intervals */
7263 reg |= (1000 >> 5);
Matthew Vick0c02dd92012-04-14 05:20:32 +00007264
7265 /* Disable BMC-to-OS Watchdog Enable */
7266 reg &= ~E1000_DMACR_DC_BMC2OSW_EN;
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +00007267 wr32(E1000_DMACR, reg);
7268
7269 /*
7270 * no lower threshold to disable
7271 * coalescing(smart fifb)-UTRESH=0
7272 */
7273 wr32(E1000_DMCRTRH, 0);
Carolyn Wybornyb6e0c412011-10-13 17:29:59 +00007274
7275 reg = (IGB_DMCTLX_DCFLUSH_DIS | 0x4);
7276
7277 wr32(E1000_DMCTLX, reg);
7278
7279 /*
7280 * free space in tx packet buffer to wake from
7281 * DMA coal
7282 */
7283 wr32(E1000_DMCTXTH, (IGB_MIN_TXPBSIZE -
7284 (IGB_TX_BUF_4096 + adapter->max_frame_size)) >> 6);
7285
7286 /*
7287 * make low power state decision controlled
7288 * by DMA coal
7289 */
7290 reg = rd32(E1000_PCIEMISC);
7291 reg &= ~E1000_PCIEMISC_LX_DECISION;
7292 wr32(E1000_PCIEMISC, reg);
7293 } /* endif adapter->dmac is not disabled */
7294 } else if (hw->mac.type == e1000_82580) {
7295 u32 reg = rd32(E1000_PCIEMISC);
7296 wr32(E1000_PCIEMISC, reg & ~E1000_PCIEMISC_LX_DECISION);
7297 wr32(E1000_DMACR, 0);
7298 }
7299}
7300
Auke Kok9d5c8242008-01-24 02:22:38 -08007301/* igb_main.c */